Line data Source code
1 : /* Process declarations and variables for the C++ compiler.
2 : Copyright (C) 1988-2026 Free Software Foundation, Inc.
3 : Contributed by Michael Tiemann (tiemann@cygnus.com)
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 :
22 : /* Process declarations and symbol lookup for C++ front end.
23 : Also constructs types; the standard scalar types at initialization,
24 : and structure, union, array and enum types when they are declared. */
25 :
26 : /* ??? not all decl nodes are given the most useful possible
27 : line numbers. For example, the CONST_DECLs for enum values. */
28 :
29 : #include "config.h"
30 : #include "system.h"
31 : #include "coretypes.h"
32 : #include "target.h"
33 : #include "c-family/c-target.h"
34 : #include "cp-tree.h"
35 : #include "timevar.h"
36 : #include "stringpool.h"
37 : #include "cgraph.h"
38 : #include "stor-layout.h"
39 : #include "varasm.h"
40 : #include "attribs.h"
41 : #include "flags.h"
42 : #include "tree-iterator.h"
43 : #include "decl.h"
44 : #include "intl.h"
45 : #include "toplev.h"
46 : #include "c-family/c-objc.h"
47 : #include "c-family/c-pragma.h"
48 : #include "c-family/c-ubsan.h"
49 : #include "cp/cp-name-hint.h"
50 : #include "debug.h"
51 : #include "plugin.h"
52 : #include "builtins.h"
53 : #include "gimplify.h"
54 : #include "asan.h"
55 : #include "gcc-rich-location.h"
56 : #include "langhooks.h"
57 : #include "context.h" /* For 'g'. */
58 : #include "omp-general.h"
59 : #include "omp-offload.h" /* For offload_vars. */
60 : #include "opts.h"
61 : #include "langhooks-def.h" /* For lhd_simulate_record_decl */
62 : #include "coroutines.h"
63 : #include "contracts.h"
64 : #include "gcc-urlifier.h"
65 : #include "diagnostic-highlight-colors.h"
66 : #include "pretty-print-markup.h"
67 :
68 : /* Possible cases of bad specifiers type used by bad_specifiers. */
69 : enum bad_spec_place {
70 : BSP_VAR, /* variable */
71 : BSP_PARM, /* parameter */
72 : BSP_TYPE, /* type */
73 : BSP_FIELD /* field */
74 : };
75 :
76 : static const char *redeclaration_error_message (tree, tree);
77 :
78 : static bool decl_jump_unsafe (tree);
79 : static bool decl_instrument_init_bypass_p (tree);
80 : static void require_complete_types_for_parms (tree);
81 : static tree grok_reference_init (tree, tree, tree, int);
82 : static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
83 : int, int, int, bool, int, tree, location_t);
84 : static void check_static_variable_definition (tree, tree);
85 : static void record_unknown_type (tree, const char *);
86 : static int member_function_or_else (tree, tree, enum overload_flags);
87 : static tree local_variable_p_walkfn (tree *, int *, void *);
88 : static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
89 : static void maybe_deduce_size_from_array_init (tree, tree);
90 : static void layout_var_decl (tree);
91 : static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
92 : static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
93 : static void copy_type_enum (tree , tree);
94 : static void check_function_type (tree, tree);
95 : static void begin_destructor_body (void);
96 : static void record_key_method_defined (tree);
97 : static tree create_array_type_for_decl (tree, tree, tree, location_t);
98 : static tree get_atexit_node (void);
99 : static tree get_dso_handle_node (void);
100 : static tree start_cleanup_fn (tree, bool, bool);
101 : static void end_cleanup_fn (void);
102 : static tree cp_make_fname_decl (location_t, tree, int);
103 : static void initialize_predefined_identifiers (void);
104 : static tree check_special_function_return_type
105 : (special_function_kind, tree, tree, int, const cp_declarator**,
106 : const location_t*);
107 : static tree push_cp_library_fn (enum tree_code, tree, int);
108 : static tree build_cp_library_fn (tree, enum tree_code, tree, int);
109 : static void store_parm_decls (tree);
110 : static void initialize_local_var (tree, tree, bool);
111 : static void expand_static_init (tree, tree);
112 : static location_t smallest_type_location (const cp_decl_specifier_seq*);
113 : static bool identify_goto (tree, location_t, const location_t *,
114 : enum diagnostics::kind, bool);
115 :
116 : /* The following symbols are subsumed in the cp_global_trees array, and
117 : listed here individually for documentation purposes.
118 :
119 : C++ extensions
120 : tree wchar_decl_node;
121 :
122 : tree vtable_entry_type;
123 : tree delta_type_node;
124 : tree __t_desc_type_node;
125 :
126 : tree class_type_node;
127 : tree unknown_type_node;
128 :
129 : Array type `vtable_entry_type[]'
130 :
131 : tree vtbl_type_node;
132 : tree vtbl_ptr_type_node;
133 :
134 : Namespaces,
135 :
136 : tree std_node;
137 : tree std_meta_node;
138 : tree abi_node;
139 :
140 : A FUNCTION_DECL which can call `abort'. Not necessarily the
141 : one that the user will declare, but sufficient to be called
142 : by routines that want to abort the program.
143 :
144 : tree abort_fndecl;
145 :
146 : Used by RTTI
147 : tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
148 : tree tinfo_var_id; */
149 :
150 : tree cp_global_trees[CPTI_MAX];
151 :
152 : /* A list of objects which have constructors or destructors
153 : which reside in namespace scope. The decl is stored in
154 : the TREE_VALUE slot and the initializer is stored
155 : in the TREE_PURPOSE slot. */
156 : tree static_aggregates;
157 :
158 : /* Like static_aggregates, but for thread_local variables. */
159 : tree tls_aggregates;
160 :
161 : /* A hash-map mapping from variable decls to the dynamic initializer for
162 : the decl. This is currently only used by OpenMP. */
163 : decl_tree_map *dynamic_initializers;
164 :
165 : /* -- end of C++ */
166 :
167 : /* A node for the integer constant 2. */
168 :
169 : tree integer_two_node;
170 :
171 : /* vector of static decls. */
172 : vec<tree, va_gc> *static_decls;
173 :
174 : /* vector of keyed classes. */
175 : vec<tree, va_gc> *keyed_classes;
176 :
177 : /* Used in the direct_goto vector of named_label_use_entry for
178 : addresses of the LABEL_DECLs within GOTO_EXPR or asm goto
179 : for forward jumps. */
180 :
181 : struct GTY(()) named_label_fwd_direct_goto {
182 : tree *GTY((skip)) direct_goto;
183 : };
184 :
185 : /* Used in the direct_goto vector of named_label_use_entry for
186 : addresses of the LABEL_DECLs within GOTO_EXPR or asm goto
187 : for backward jumps. */
188 :
189 : struct GTY(()) named_label_bck_direct_goto {
190 : tree *GTY((skip)) direct_goto;
191 : /* Number of the decl_instrument_init_bypass_p decls in bad_decls vector
192 : at the time this backward goto has been seen. */
193 : unsigned n_bad_decls;
194 : };
195 :
196 : /* Used only for jumps to as-yet undefined labels, since jumps to
197 : defined labels can have their validity checked immediately. */
198 :
199 : struct GTY((chain_next ("%h.next"))) named_label_use_entry {
200 : struct named_label_use_entry *next;
201 : /* The binding level to which this entry is *currently* attached.
202 : This is initially the binding level in which the goto appeared,
203 : but is modified as scopes are closed. */
204 : cp_binding_level *binding_level;
205 : /* The head of the names list that was current when the goto appeared,
206 : or the inner scope popped. These are the decls that will *not* be
207 : skipped when jumping to the label. */
208 : tree names_in_scope;
209 : /* If the use is a possible destination of a computed goto, a vec of decls
210 : that aren't destroyed, filled in by poplevel_named_label_1. */
211 : vec<tree, va_gc> *computed_goto;
212 : /* If the use is a destination of normal goto, a vec of addresses of
213 : LABEL_DECLs that might need changing for !!flag_auto_var_init
214 : forward jumps across vacuous initializers. */
215 : vec<named_label_fwd_direct_goto, va_gc> *direct_goto;
216 : /* The location of the goto, for error reporting. */
217 : location_t o_goto_locus;
218 : /* True if an OpenMP structured block scope has been closed since
219 : the goto appeared. This means that the branch from the label will
220 : illegally exit an OpenMP scope. */
221 : bool in_omp_scope;
222 : };
223 :
224 : /* A list of all LABEL_DECLs in the function that have names. Here so
225 : we can clear out their names' definitions at the end of the
226 : function, and so we can check the validity of jumps to these labels. */
227 :
228 : struct GTY((for_user)) named_label_entry {
229 :
230 : tree name; /* Name of decl. */
231 :
232 : tree label_decl; /* LABEL_DECL, unless deleted local label. */
233 :
234 : named_label_entry *outer; /* Outer shadowed chain. */
235 :
236 : /* The binding level to which the label is *currently* attached.
237 : This is initially set to the binding level in which the label
238 : is defined, but is modified as scopes are closed. */
239 : cp_binding_level *binding_level;
240 :
241 : /* The head of the names list that was current when the label was
242 : defined, or the inner scope popped. These are the decls that will
243 : be skipped when jumping to the label. */
244 : tree names_in_scope;
245 :
246 : /* A vector of all decls from all binding levels that would be
247 : crossed by a backward branch to the label. */
248 : vec<tree, va_gc> *bad_decls;
249 :
250 : /* A list of uses of the label, before the label is defined. */
251 : named_label_use_entry *uses;
252 :
253 : /* If the use is a destination of normal goto, a vec of addresses of
254 : LABEL_DECLs that might need changing for !!flag_auto_var_init
255 : backward jumps across vacuous initializers. */
256 : vec<named_label_bck_direct_goto, va_gc> *direct_goto;
257 :
258 : /* True if we've seen &&label. Apparently we can't use TREE_ADDRESSABLE for
259 : this, it has a more specific meaning for LABEL_DECL. */
260 : bool addressed : 1;
261 :
262 : /* The following bits are set after the label is defined, and are
263 : updated as scopes are popped. They indicate that a jump to the
264 : label will illegally enter a scope of the given flavor. */
265 : bool in_try_scope : 1;
266 : bool in_catch_scope : 1;
267 : bool in_omp_scope : 1;
268 : bool in_transaction_scope : 1;
269 : bool in_constexpr_if : 1;
270 : bool in_consteval_if : 1;
271 : bool in_stmt_expr : 1;
272 :
273 : /* True if bad_decls chain contains any decl_jump_unsafe decls
274 : (rather than just decl_instrument_init_bypass_p). */
275 : bool has_bad_decls : 1;
276 : };
277 :
278 : #define named_labels cp_function_chain->x_named_labels
279 :
280 : /* The number of function bodies which we are currently processing.
281 : (Zero if we are at namespace scope, one inside the body of a
282 : function, two inside the body of a function in a local class, etc.) */
283 : int function_depth;
284 :
285 : /* Whether the exception-specifier is part of a function type (i.e. C++17). */
286 : bool flag_noexcept_type;
287 :
288 : /* States indicating how grokdeclarator() should handle declspecs marked
289 : with __attribute__((deprecated)). An object declared as
290 : __attribute__((deprecated)) suppresses warnings of uses of other
291 : deprecated items. */
292 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
293 :
294 :
295 : /* A list of VAR_DECLs whose type was incomplete at the time the
296 : variable was declared. */
297 :
298 : struct GTY(()) incomplete_var {
299 : tree decl;
300 : tree incomplete_type;
301 : };
302 :
303 :
304 : static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
305 :
306 : /* Returns the kind of template specialization we are currently
307 : processing, given that it's declaration contained N_CLASS_SCOPES
308 : explicit scope qualifications. */
309 :
310 : tmpl_spec_kind
311 262300053 : current_tmpl_spec_kind (int n_class_scopes)
312 : {
313 262300053 : int n_template_parm_scopes = 0;
314 262300053 : int seen_specialization_p = 0;
315 262300053 : int innermost_specialization_p = 0;
316 262300053 : cp_binding_level *b;
317 :
318 : /* Scan through the template parameter scopes. */
319 262300053 : for (b = current_binding_level;
320 327220874 : b->kind == sk_template_parms;
321 64920821 : b = b->level_chain)
322 : {
323 : /* If we see a specialization scope inside a parameter scope,
324 : then something is wrong. That corresponds to a declaration
325 : like:
326 :
327 : template <class T> template <> ...
328 :
329 : which is always invalid since [temp.expl.spec] forbids the
330 : specialization of a class member template if the enclosing
331 : class templates are not explicitly specialized as well. */
332 64920821 : if (b->explicit_spec_p)
333 : {
334 1559017 : if (n_template_parm_scopes == 0)
335 : innermost_specialization_p = 1;
336 : else
337 1349 : seen_specialization_p = 1;
338 : }
339 63361804 : else if (seen_specialization_p == 1)
340 : return tsk_invalid_member_spec;
341 :
342 64920821 : ++n_template_parm_scopes;
343 : }
344 :
345 : /* Handle explicit instantiations. */
346 262300053 : if (processing_explicit_instantiation)
347 : {
348 2063592 : if (n_template_parm_scopes != 0)
349 : /* We've seen a template parameter list during an explicit
350 : instantiation. For example:
351 :
352 : template <class T> template void f(int);
353 :
354 : This is erroneous. */
355 : return tsk_invalid_expl_inst;
356 : else
357 2063592 : return tsk_expl_inst;
358 : }
359 :
360 260236461 : if (n_template_parm_scopes < n_class_scopes)
361 : /* We've not seen enough template headers to match all the
362 : specialized classes present. For example:
363 :
364 : template <class T> void R<T>::S<T>::f(int);
365 :
366 : This is invalid; there needs to be one set of template
367 : parameters for each class. */
368 : return tsk_insufficient_parms;
369 260236461 : else if (n_template_parm_scopes == n_class_scopes)
370 : /* We're processing a non-template declaration (even though it may
371 : be a member of a template class.) For example:
372 :
373 : template <class T> void S<T>::f(int);
374 :
375 : The `class T' matches the `S<T>', leaving no template headers
376 : corresponding to the `f'. */
377 : return tsk_none;
378 56476289 : else if (n_template_parm_scopes > n_class_scopes + 1)
379 : /* We've got too many template headers. For example:
380 :
381 : template <> template <class T> void f (T);
382 :
383 : There need to be more enclosing classes. */
384 : return tsk_excessive_parms;
385 : else
386 : /* This must be a template. It's of the form:
387 :
388 : template <class T> template <class U> void S<T>::f(U);
389 :
390 : This is a specialization if the innermost level was a
391 : specialization; otherwise it's just a definition of the
392 : template. */
393 56476289 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394 : }
395 :
396 : /* Exit the current scope. */
397 :
398 : void
399 103092795 : finish_scope (void)
400 : {
401 103092795 : poplevel (0, 0, 0);
402 103092795 : }
403 :
404 : /* When a label goes out of scope, check to see if that label was used
405 : in a valid manner, and issue any appropriate warnings or errors. */
406 :
407 : static void
408 21153 : check_label_used (tree label)
409 : {
410 21153 : if (!processing_template_decl)
411 : {
412 20844 : if (DECL_INITIAL (label) == NULL_TREE)
413 : {
414 9 : location_t location;
415 :
416 9 : error ("label %q+D used but not defined", label);
417 9 : location = input_location;
418 : /* FIXME want (LOCATION_FILE (input_location), (line)0) */
419 : /* Avoid crashing later. */
420 9 : define_label (location, DECL_NAME (label));
421 : }
422 : else
423 20835 : warn_for_unused_label (label);
424 : }
425 21153 : }
426 :
427 : /* Helper function to sort named label entries in a vector by DECL_UID. */
428 :
429 : static int
430 237748 : sort_labels (const void *a, const void *b)
431 : {
432 237748 : tree label1 = *(tree const *) a;
433 237748 : tree label2 = *(tree const *) b;
434 :
435 : /* DECL_UIDs can never be equal. */
436 237748 : return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
437 : }
438 :
439 : static void adjust_backward_goto (named_label_entry *, tree_stmt_iterator);
440 : static named_label_entry *lookup_label_1 (tree, bool);
441 :
442 : /* Helper of pop_labels, called through cp_walk_tree. Adjust
443 : LABEL_EXPRs of named labels, if they are targets of backwards
444 : gotos jumping across vacuous initialization for
445 : !!flag_auto_var_init. */
446 :
447 : static tree
448 2909 : adjust_backward_gotos (tree *tp, int *walk_subtrees, void *data)
449 : {
450 2909 : tree t = *tp;
451 2909 : switch (TREE_CODE (t))
452 : {
453 3 : case LABEL_EXPR:
454 : /* In rare cases LABEL_EXPR can appear as the only substatement
455 : of some other statement, e.g. if body etc. In that case, we know
456 : there can't be an older if (0) wrapper with artificial initializers
457 : before it. Replace the LABEL_EXPR statement with a STATEMENT_LIST
458 : and insert the LABEL_EXPR into it, later on if (0) will be added
459 : before that. */
460 3 : if (DECL_NAME (LABEL_EXPR_LABEL (t)))
461 : {
462 3 : named_label_entry *ent
463 3 : = lookup_label_1 (DECL_NAME (LABEL_EXPR_LABEL (t)), false);
464 3 : if (ent->direct_goto)
465 : {
466 3 : *tp = alloc_stmt_list ();
467 3 : append_to_statement_list_force (t, tp);
468 3 : adjust_backward_goto (ent, tsi_last (*tp));
469 : }
470 : }
471 3 : *walk_subtrees = 0;
472 3 : break;
473 141 : case STATEMENT_LIST:
474 141 : {
475 141 : tree_stmt_iterator i;
476 141 : *walk_subtrees = 0;
477 : /* In the common case, LABEL_EXPRs appear inside of a STATEMENT_LIST.
478 : In that case pass the stmt iterator to adjust_backward_goto, so
479 : that it can insert if (0) wrapper artificial initializers before
480 : it or reuse the existing ones. */
481 915 : for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
482 774 : if (TREE_CODE (tsi_stmt (i)) != LABEL_EXPR)
483 737 : cp_walk_tree (tsi_stmt_ptr (i), adjust_backward_gotos,
484 : data, (hash_set<tree> *) data);
485 37 : else if (DECL_NAME (LABEL_EXPR_LABEL (tsi_stmt (i))))
486 : {
487 16 : named_label_entry *ent
488 16 : = lookup_label_1 (DECL_NAME (LABEL_EXPR_LABEL (tsi_stmt (i))),
489 : false);
490 16 : if (ent->direct_goto)
491 16 : adjust_backward_goto (ent, i);
492 : }
493 141 : break;
494 : }
495 2765 : default:
496 2765 : if (TYPE_P (t))
497 4 : *walk_subtrees = 0;
498 : }
499 2909 : return NULL_TREE;
500 : }
501 :
502 : /* At the end of a function, all labels declared within the function
503 : go out of scope. BLOCK is the top-level block for the
504 : function. */
505 :
506 : static void
507 169652882 : pop_labels (tree block)
508 : {
509 169652882 : if (!named_labels)
510 169648129 : return;
511 :
512 : /* We need to add the labels to the block chain, so debug
513 : information is emitted. But, we want the order to be stable so
514 : need to sort them first. Otherwise the debug output could be
515 : randomly ordered. I guess it's mostly stable, unless the hash
516 : table implementation changes. */
517 4753 : auto_vec<tree, 32> labels (named_labels->elements ());
518 4753 : hash_table<named_label_hash>::iterator end (named_labels->end ());
519 :
520 4753 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
521 732 : && !processing_template_decl)
522 : {
523 4006 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
524 : {
525 2606 : named_label_entry *ent = *iter;
526 2606 : if (ent->direct_goto)
527 : {
528 4 : hash_set<tree> pset;
529 4 : cp_walk_tree (&DECL_SAVED_TREE (current_function_decl),
530 : adjust_backward_gotos, &pset, &pset);
531 4 : break;
532 4 : }
533 : }
534 : }
535 :
536 25873 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
537 : {
538 21120 : named_label_entry *ent = *iter;
539 :
540 21120 : gcc_checking_assert (!ent->outer);
541 21120 : if (ent->label_decl)
542 20934 : labels.quick_push (ent->label_decl);
543 21120 : ggc_free (ent);
544 : }
545 4753 : named_labels = NULL;
546 4753 : labels.qsort (sort_labels);
547 :
548 25687 : while (labels.length ())
549 : {
550 20934 : tree label = labels.pop ();
551 :
552 20934 : DECL_CHAIN (label) = BLOCK_VARS (block);
553 20934 : BLOCK_VARS (block) = label;
554 :
555 20934 : check_label_used (label);
556 : }
557 4753 : }
558 :
559 : /* At the end of a block with local labels, restore the outer definition. */
560 :
561 : static void
562 219 : pop_local_label (tree id, tree label)
563 : {
564 219 : check_label_used (label);
565 219 : named_label_entry **slot = named_labels->find_slot_with_hash
566 219 : (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
567 219 : named_label_entry *ent = *slot;
568 :
569 219 : if (ent->outer)
570 : ent = ent->outer;
571 : else
572 : {
573 198 : ent = ggc_cleared_alloc<named_label_entry> ();
574 198 : ent->name = id;
575 : }
576 219 : *slot = ent;
577 219 : }
578 :
579 : /* The following two routines are used to interface to Objective-C++.
580 : The binding level is purposely treated as an opaque type. */
581 :
582 : void *
583 0 : objc_get_current_scope (void)
584 : {
585 0 : return current_binding_level;
586 : }
587 :
588 : /* The following routine is used by the NeXT-style SJLJ exceptions;
589 : variables get marked 'volatile' so as to not be clobbered by
590 : _setjmp()/_longjmp() calls. All variables in the current scope,
591 : as well as parent scopes up to (but not including) ENCLOSING_BLK
592 : shall be thusly marked. */
593 :
594 : void
595 0 : objc_mark_locals_volatile (void *enclosing_blk)
596 : {
597 0 : cp_binding_level *scope;
598 :
599 0 : for (scope = current_binding_level;
600 0 : scope && scope != enclosing_blk;
601 0 : scope = scope->level_chain)
602 : {
603 0 : tree decl;
604 :
605 0 : for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
606 0 : objc_volatilize_decl (decl);
607 :
608 : /* Do not climb up past the current function. */
609 0 : if (scope->kind == sk_function_parms)
610 : break;
611 : }
612 0 : }
613 :
614 : /* True if B is the level for the condition of a constexpr if. */
615 :
616 : static bool
617 1810434 : level_for_constexpr_if (cp_binding_level *b)
618 : {
619 1806294 : return (b->kind == sk_cond && b->this_entity
620 302 : && TREE_CODE (b->this_entity) == IF_STMT
621 1810736 : && IF_STMT_CONSTEXPR_P (b->this_entity));
622 : }
623 :
624 : /* True if B is the level for the condition of a consteval if. */
625 :
626 : static bool
627 1810410 : level_for_consteval_if (cp_binding_level *b)
628 : {
629 1806270 : return (b->kind == sk_cond && b->this_entity
630 278 : && TREE_CODE (b->this_entity) == IF_STMT
631 1810688 : && IF_STMT_CONSTEVAL_P (b->this_entity));
632 : }
633 :
634 : /* True if T is a non-static VAR_DECL that has a non-trivial destructor.
635 : See [stmt.dcl]/2. */
636 :
637 : static bool
638 25 : automatic_var_with_nontrivial_dtor_p (const_tree t)
639 : {
640 25 : if (error_operand_p (t))
641 : return false;
642 :
643 19 : return (VAR_P (t)
644 19 : && decl_storage_duration (const_cast<tree> (t)) == dk_auto
645 38 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)));
646 : }
647 :
648 : /* Update data for defined and undefined labels when leaving a scope. */
649 :
650 : int
651 158056 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
652 : {
653 158056 : named_label_entry *ent = *slot;
654 158056 : cp_binding_level *obl = bl->level_chain;
655 :
656 158056 : if (ent->binding_level == bl)
657 : {
658 10391 : tree decl;
659 :
660 : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
661 : TREE_LISTs representing OVERLOADs, so be careful. */
662 15348 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
663 5030 : ? DECL_CHAIN (decl)
664 73 : : TREE_CHAIN (decl)))
665 4957 : if (decl_jump_unsafe (decl))
666 : {
667 1906 : vec_safe_push (ent->bad_decls, decl);
668 1906 : ent->has_bad_decls = true;
669 : }
670 3051 : else if (decl_instrument_init_bypass_p (decl))
671 442 : vec_safe_push (ent->bad_decls, decl);
672 :
673 10391 : ent->binding_level = obl;
674 10391 : ent->names_in_scope = obl->names;
675 10391 : switch (bl->kind)
676 : {
677 42 : case sk_try:
678 42 : ent->in_try_scope = true;
679 42 : break;
680 27 : case sk_catch:
681 27 : ent->in_catch_scope = true;
682 27 : break;
683 324 : case sk_omp:
684 324 : ent->in_omp_scope = true;
685 324 : break;
686 3 : case sk_transaction:
687 3 : ent->in_transaction_scope = true;
688 3 : break;
689 45 : case sk_stmt_expr:
690 45 : ent->in_stmt_expr = true;
691 45 : break;
692 4188 : case sk_block:
693 4188 : if (level_for_constexpr_if (obl))
694 15 : ent->in_constexpr_if = true;
695 4173 : else if (level_for_consteval_if (obl))
696 30 : ent->in_consteval_if = true;
697 : break;
698 : default:
699 : break;
700 : }
701 : }
702 147665 : else if (ent->uses)
703 : {
704 : struct named_label_use_entry *use;
705 :
706 23019 : for (use = ent->uses; use ; use = use->next)
707 15614 : if (use->binding_level == bl)
708 : {
709 6045 : if (auto &cg = use->computed_goto)
710 : {
711 57 : if (bl->kind == sk_catch)
712 3 : vec_safe_push (cg, get_identifier ("catch"));
713 66 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
714 9 : if (automatic_var_with_nontrivial_dtor_p (d))
715 6 : vec_safe_push (cg, d);
716 : }
717 :
718 6045 : use->binding_level = obl;
719 6045 : use->names_in_scope = obl->names;
720 6045 : if (bl->kind == sk_omp)
721 12 : use->in_omp_scope = true;
722 : }
723 : }
724 :
725 158056 : return 1;
726 : }
727 :
728 : /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
729 : when errors were reported, except for -Werror-unused-but-set-*. */
730 : static int unused_but_set_errorcount;
731 :
732 : /* Exit a binding level.
733 : Pop the level off, and restore the state of the identifier-decl mappings
734 : that were in effect when this level was entered.
735 :
736 : If KEEP == 1, this level had explicit declarations, so
737 : and create a "block" (a BLOCK node) for the level
738 : to record its declarations and subblocks for symbol table output.
739 :
740 : If FUNCTIONBODY is nonzero, this level is the body of a function,
741 : so create a block as if KEEP were set and also clear out all
742 : label names.
743 :
744 : If REVERSE is nonzero, reverse the order of decls before putting
745 : them into the BLOCK. */
746 :
747 : tree
748 713577756 : poplevel (int keep, int reverse, int functionbody)
749 : {
750 713577756 : tree link;
751 : /* The chain of decls was accumulated in reverse order.
752 : Put it into forward order, just for cleanliness. */
753 713577756 : tree decls;
754 713577756 : tree subblocks;
755 713577756 : tree block;
756 713577756 : tree decl;
757 713577756 : scope_kind kind;
758 :
759 713577756 : auto_cond_timevar tv (TV_NAME_LOOKUP);
760 713578205 : restart:
761 :
762 713578205 : block = NULL_TREE;
763 :
764 713578205 : gcc_assert (current_binding_level->kind != sk_class
765 : && current_binding_level->kind != sk_namespace);
766 :
767 713578205 : if (current_binding_level->kind == sk_cleanup)
768 : functionbody = 0;
769 713578205 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
770 :
771 713578205 : gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
772 :
773 : /* We used to use KEEP == 2 to indicate that the new block should go
774 : at the beginning of the list of blocks at this binding level,
775 : rather than the end. This hack is no longer used. */
776 713578205 : gcc_assert (keep == 0 || keep == 1);
777 :
778 713578205 : if (current_binding_level->keep)
779 8788982 : keep = 1;
780 :
781 : /* Any uses of undefined labels, and any defined labels, now operate
782 : under constraints of next binding contour. */
783 713578205 : if (cfun && !functionbody && named_labels)
784 44185 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
785 202241 : (current_binding_level);
786 :
787 : /* Get the decls in the order they were written.
788 : Usually current_binding_level->names is in reverse order.
789 : But parameter decls were previously put in forward order. */
790 :
791 713578205 : decls = current_binding_level->names;
792 713578205 : if (reverse)
793 : {
794 391841034 : decls = nreverse (decls);
795 391841034 : current_binding_level->names = decls;
796 : }
797 :
798 : /* If there were any declarations or structure tags in that level,
799 : or if this level is a function body,
800 : create a BLOCK to record them for the life of this function. */
801 713578205 : block = NULL_TREE;
802 : /* Avoid function body block if possible. */
803 756814297 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
804 : keep = 0;
805 670342377 : else if (keep == 1 || functionbody)
806 214228504 : block = make_node (BLOCK);
807 214228504 : if (block != NULL_TREE)
808 : {
809 214228504 : BLOCK_VARS (block) = decls;
810 214228504 : BLOCK_SUBBLOCKS (block) = subblocks;
811 : }
812 :
813 : /* In each subblock, record that this is its superior. */
814 713578205 : if (keep >= 0)
815 801388920 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
816 87810715 : BLOCK_SUPERCONTEXT (link) = block;
817 :
818 : /* Before we remove the declarations first check for unused variables. */
819 713578205 : if ((warn_unused_variable || warn_unused_but_set_variable)
820 6789589 : && current_binding_level->kind != sk_template_parms
821 5724178 : && !processing_template_decl)
822 6566629 : for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
823 : {
824 : /* There are cases where D itself is a TREE_LIST. See in
825 : push_local_binding where the list of decls returned by
826 : getdecls is built. */
827 3327757 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
828 3327757 : decl = strip_using_decl (udecl);
829 :
830 3327757 : tree type = TREE_TYPE (decl);
831 3327757 : if (VAR_P (decl)
832 388591 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
833 3613 : && !DECL_IN_SYSTEM_HEADER (udecl)
834 : /* For structured bindings, consider only real variables, not
835 : subobjects. */
836 3601 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
837 3378 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
838 : /* Don't warn about name-independent declarations. */
839 899 : && !name_independent_decl_p (decl)
840 743 : && type != error_mark_node
841 3332067 : && (!CLASS_TYPE_P (type)
842 51 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
843 3 : || lookup_attribute ("warn_unused",
844 3 : TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
845 : {
846 709 : if (!TREE_USED (decl))
847 : {
848 308 : if (TREE_CODE (udecl) == USING_DECL)
849 3 : warning_at (DECL_SOURCE_LOCATION (udecl),
850 3 : OPT_Wunused_variable,
851 : "unused using-declaration %qD", udecl);
852 305 : else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
853 3 : warning_at (DECL_SOURCE_LOCATION (decl),
854 3 : OPT_Wunused_variable,
855 : "unused structured binding declaration");
856 : else
857 302 : warning_at (DECL_SOURCE_LOCATION (decl),
858 302 : OPT_Wunused_variable, "unused variable %qD", decl);
859 308 : suppress_warning (decl, OPT_Wunused_variable);
860 : }
861 401 : else if (DECL_CONTEXT (decl) == current_function_decl
862 : // For -Wunused-but-set-variable leave references alone.
863 377 : && !TYPE_REF_P (TREE_TYPE (decl))
864 778 : && errorcount == unused_but_set_errorcount)
865 : {
866 368 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
867 12 : warning_at (DECL_SOURCE_LOCATION (decl),
868 12 : OPT_Wunused_but_set_variable_, "structured "
869 : "binding declaration set but not used");
870 : else
871 356 : warning_at (DECL_SOURCE_LOCATION (decl),
872 356 : OPT_Wunused_but_set_variable_,
873 : "variable %qD set but not used", decl);
874 368 : unused_but_set_errorcount = errorcount;
875 : }
876 : }
877 : }
878 :
879 : /* Remove declarations for all the DECLs in this level. */
880 1441762123 : for (link = decls; link; link = TREE_CHAIN (link))
881 : {
882 728183918 : tree name;
883 728183918 : if (TREE_CODE (link) == TREE_LIST)
884 : {
885 13963518 : decl = TREE_VALUE (link);
886 13963518 : name = TREE_PURPOSE (link);
887 13963518 : gcc_checking_assert (name);
888 : }
889 : else
890 : {
891 714220400 : decl = link;
892 714220400 : name = DECL_NAME (decl);
893 : }
894 :
895 : /* Remove the binding. */
896 728183918 : if (TREE_CODE (decl) == LABEL_DECL)
897 219 : pop_local_label (name, decl);
898 : else
899 728183699 : pop_local_binding (name, decl);
900 : }
901 :
902 : /* Restore the IDENTIFIER_TYPE_VALUEs. */
903 713578205 : for (link = current_binding_level->type_shadowed;
904 972251181 : link; link = TREE_CHAIN (link))
905 258672976 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
906 :
907 : /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
908 : list if a `using' declaration put them there. The debugging
909 : back ends won't understand OVERLOAD, so we remove them here.
910 : Because the BLOCK_VARS are (temporarily) shared with
911 : CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
912 : popped all the bindings. Also remove undeduced 'auto' decls,
913 : which LTO doesn't understand, and can't have been used by anything. */
914 713578205 : if (block)
915 : {
916 214228504 : tree* d;
917 :
918 563803699 : for (d = &BLOCK_VARS (block); *d; )
919 : {
920 349575195 : if (TREE_CODE (*d) == TREE_LIST
921 349575195 : || (!processing_template_decl
922 110553793 : && undeduced_auto_decl (*d)))
923 13963570 : *d = TREE_CHAIN (*d);
924 : else
925 335611625 : d = &DECL_CHAIN (*d);
926 : }
927 : }
928 :
929 : /* If the level being exited is the top level of a function,
930 : check over all the labels. */
931 713578205 : if (functionbody)
932 : {
933 169652882 : if (block)
934 : {
935 : /* Since this is the top level block of a function, the vars are
936 : the function's parameters. Don't leave them in the BLOCK
937 : because they are found in the FUNCTION_DECL instead. */
938 126417054 : BLOCK_VARS (block) = 0;
939 126417054 : pop_labels (block);
940 : }
941 : else
942 43235828 : pop_labels (subblocks);
943 : }
944 :
945 713578205 : kind = current_binding_level->kind;
946 713578205 : if (kind == sk_cleanup)
947 : {
948 449 : tree stmt;
949 :
950 : /* If this is a temporary binding created for a cleanup, then we'll
951 : have pushed a statement list level. Pop that, create a new
952 : BIND_EXPR for the block, and insert it into the stream. */
953 449 : stmt = pop_stmt_list (current_binding_level->statement_list);
954 449 : stmt = c_build_bind_expr (input_location, block, stmt);
955 449 : add_stmt (stmt);
956 : }
957 :
958 713578205 : leave_scope ();
959 713578205 : if (functionbody)
960 : {
961 : /* The current function is being defined, so its DECL_INITIAL
962 : should be error_mark_node. */
963 169652882 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
964 212888710 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
965 169652882 : if (subblocks)
966 : {
967 82271628 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
968 53731044 : && BLOCK_SUBBLOCKS (subblocks))
969 1399790 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
970 : else
971 41836302 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
972 : }
973 : }
974 543925323 : else if (block)
975 87811450 : current_binding_level->blocks
976 175622900 : = block_chainon (current_binding_level->blocks, block);
977 :
978 : /* If we did not make a block for the level just exited,
979 : any blocks made for inner levels
980 : (since they cannot be recorded as subblocks in that level)
981 : must be carried forward so they will later become subblocks
982 : of something else. */
983 456113873 : else if (subblocks)
984 18 : current_binding_level->blocks
985 36 : = block_chainon (current_binding_level->blocks, subblocks);
986 :
987 : /* Each and every BLOCK node created here in `poplevel' is important
988 : (e.g. for proper debugging information) so if we created one
989 : earlier, mark it as "used". */
990 713578205 : if (block)
991 214228504 : TREE_USED (block) = 1;
992 :
993 : /* All temporary bindings created for cleanups are popped silently. */
994 713578205 : if (kind == sk_cleanup)
995 449 : goto restart;
996 :
997 1427155512 : return block;
998 713577756 : }
999 :
1000 : /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
1001 : /* Diagnose odr-used extern inline variables without definitions
1002 : in the current TU. */
1003 :
1004 : int
1005 147599 : wrapup_namespace_globals ()
1006 : {
1007 147599 : if (vec<tree, va_gc> *statics = static_decls)
1008 : {
1009 18532817 : for (tree decl : *statics)
1010 : {
1011 : /* Rewrite the REFLECT_EXPR with 0 so that the ME can process it. */
1012 18412243 : if (flag_reflection && DECL_INITIAL (decl))
1013 305579 : rewrite_null_reflection (DECL_INITIAL (decl));
1014 :
1015 18412243 : if (warn_unused_function
1016 1014867 : && TREE_CODE (decl) == FUNCTION_DECL
1017 765494 : && DECL_INITIAL (decl) == 0
1018 7626 : && DECL_EXTERNAL (decl)
1019 7614 : && !TREE_PUBLIC (decl)
1020 34 : && !DECL_ARTIFICIAL (decl)
1021 34 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1022 9 : && !warning_suppressed_p (decl, OPT_Wunused)
1023 19427110 : && warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_function,
1024 : "%qF declared %<static%> but never defined",
1025 : decl))
1026 6 : suppress_warning (decl, OPT_Wunused);
1027 :
1028 18412243 : if (VAR_P (decl)
1029 6082204 : && DECL_EXTERNAL (decl)
1030 : /* We mark consteval-only variables DECL_EXTERNAL, but
1031 : extern constexpr inline std::meta::info i{};
1032 : is a definition (the extern is redundant). */
1033 2092383 : && !DECL_INITIAL (decl)
1034 2091212 : && DECL_INLINE_VAR_P (decl)
1035 18412272 : && DECL_ODR_USED (decl))
1036 3 : error_at (DECL_SOURCE_LOCATION (decl),
1037 : "odr-used inline variable %qD is not defined", decl);
1038 : }
1039 :
1040 : /* Clear out the list, so we don't rescan next time. */
1041 120574 : static_decls = NULL;
1042 :
1043 : /* Write out any globals that need to be output. */
1044 120574 : return wrapup_global_declarations (statics->address (),
1045 120574 : statics->length ());
1046 : }
1047 : return 0;
1048 : }
1049 :
1050 : /* In C++, you don't have to write `struct S' to refer to `S'; you
1051 : can just use `S'. We accomplish this by creating a TYPE_DECL as
1052 : if the user had written `typedef struct S S'. Create and return
1053 : the TYPE_DECL for TYPE. */
1054 :
1055 : tree
1056 191553973 : create_implicit_typedef (tree name, tree type)
1057 : {
1058 191553973 : tree decl;
1059 :
1060 191553973 : decl = build_decl (input_location, TYPE_DECL, name, type);
1061 191553973 : DECL_ARTIFICIAL (decl) = 1;
1062 : /* There are other implicit type declarations, like the one *within*
1063 : a class that allows you to write `S::S'. We must distinguish
1064 : amongst these. */
1065 191553973 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1066 191553973 : TYPE_NAME (type) = decl;
1067 191553973 : TYPE_STUB_DECL (type) = decl;
1068 :
1069 191553973 : return decl;
1070 : }
1071 :
1072 : /* Function-scope local entities that need discriminators. Each entry
1073 : is a {decl,name} pair. VAR_DECLs for anon unions get their name
1074 : smashed, so we cannot rely on DECL_NAME. */
1075 :
1076 : static GTY((deletable)) vec<tree, va_gc> *local_entities;
1077 :
1078 : /* Determine the mangling discriminator of local DECL. There are
1079 : generally very few of these in any particular function. */
1080 :
1081 : void
1082 780911 : determine_local_discriminator (tree decl, tree name)
1083 : {
1084 780911 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1085 780911 : retrofit_lang_decl (decl);
1086 780911 : tree ctx = DECL_CONTEXT (decl);
1087 780911 : size_t nelts = vec_safe_length (local_entities);
1088 780911 : if (name == NULL_TREE)
1089 780642 : name = (TREE_CODE (decl) == TYPE_DECL
1090 1277369 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
1091 1414903 : ? NULL_TREE : DECL_NAME (decl));
1092 23001999 : for (size_t i = 0; i < nelts; i += 2)
1093 : {
1094 22221768 : tree *pair = &(*local_entities)[i];
1095 22221768 : tree d = pair[0];
1096 22221768 : tree n = pair[1];
1097 22221768 : gcc_checking_assert (d != decl);
1098 22221768 : if (name == n
1099 4017083 : && TREE_CODE (decl) == TREE_CODE (d)
1100 26238842 : && ctx == DECL_CONTEXT (d))
1101 : {
1102 680 : tree disc = integer_one_node;
1103 680 : if (DECL_DISCRIMINATOR (d))
1104 375 : disc = build_int_cst (TREE_TYPE (disc),
1105 375 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
1106 680 : DECL_DISCRIMINATOR (decl) = disc;
1107 : /* Replace the saved decl. */
1108 680 : pair[0] = decl;
1109 680 : decl = NULL_TREE;
1110 680 : break;
1111 : }
1112 : }
1113 :
1114 780911 : if (decl)
1115 : {
1116 780231 : vec_safe_reserve (local_entities, 2);
1117 780231 : local_entities->quick_push (decl);
1118 780231 : local_entities->quick_push (name);
1119 : }
1120 780911 : }
1121 :
1122 :
1123 : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
1124 :
1125 : A non-template friend declaration with a requires-clause shall be a
1126 : definition. A friend function template with a constraint that depends on a
1127 : template parameter from an enclosing template shall be a definition. Such a
1128 : constrained friend function or function template declaration does not
1129 : declare the same function or function template as a declaration in any other
1130 : scope.
1131 :
1132 : The ABI calls this a "member-like constrained friend" and mangles it like a
1133 : member function to avoid collisions. */
1134 :
1135 : bool
1136 2624744789 : member_like_constrained_friend_p (tree decl)
1137 : {
1138 2624744789 : return (TREE_CODE (decl) == FUNCTION_DECL
1139 900372209 : && DECL_UNIQUE_FRIEND_P (decl)
1140 295376172 : && DECL_FRIEND_CONTEXT (decl)
1141 129816973 : && get_constraints (decl)
1142 18766526 : && CLASSTYPE_IMPLICIT_INSTANTIATION (DECL_FRIEND_CONTEXT (decl))
1143 2634106206 : && (!DECL_TEMPLATE_INFO (decl)
1144 9361417 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1145 8723 : || (uses_outer_template_parms_in_constraints
1146 8723 : (most_general_template (decl)))));
1147 : }
1148 :
1149 : /* Returns true if functions FN1 and FN2 have equivalent trailing
1150 : requires clauses. */
1151 :
1152 : static bool
1153 73396926 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1154 : {
1155 : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1156 : same function as a declaration in any other scope." So no need to
1157 : actually compare the requirements. */
1158 73396926 : if (member_like_constrained_friend_p (newfn)
1159 73396926 : || member_like_constrained_friend_p (oldfn))
1160 : return false;
1161 :
1162 : /* Compare only trailing requirements. */
1163 73396805 : tree reqs1 = get_trailing_function_requirements (newfn);
1164 73396805 : tree reqs2 = get_trailing_function_requirements (oldfn);
1165 73396805 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1166 : return false;
1167 :
1168 : /* Substitution is needed when friends are involved. */
1169 73258490 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1170 73258490 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1171 :
1172 73258490 : return cp_tree_equal (reqs1, reqs2);
1173 : }
1174 :
1175 : /* Two functions of the same name correspond [basic.scope.scope] if
1176 :
1177 : + both declare functions with the same non-object-parameter-type-list,
1178 : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1179 : specified in [temp.friend]), and, if both are non-static members, they have
1180 : corresponding object parameters, or
1181 :
1182 : + both declare function templates with equivalent
1183 : non-object-parameter-type-lists, return types (if any), template-heads, and
1184 : trailing requires-clauses (if any), and, if both are non-static members,
1185 : they have corresponding object parameters.
1186 :
1187 : This is a subset of decls_match: it identifies declarations that cannot be
1188 : overloaded with one another. This function does not consider DECL_NAME. */
1189 :
1190 : bool
1191 1585673271 : fns_correspond (tree newdecl, tree olddecl)
1192 : {
1193 1585673271 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1194 : return false;
1195 :
1196 1474242848 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1197 : {
1198 1472878942 : if (!template_heads_equivalent_p (newdecl, olddecl))
1199 : return 0;
1200 350015309 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1201 350015309 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1202 : }
1203 :
1204 351379215 : tree f1 = TREE_TYPE (newdecl);
1205 351379215 : tree f2 = TREE_TYPE (olddecl);
1206 :
1207 351379215 : int rq1 = type_memfn_rqual (f1);
1208 351379215 : int rq2 = type_memfn_rqual (f2);
1209 :
1210 : /* If only one is a non-static member function, ignore ref-quals. */
1211 351379215 : if (TREE_CODE (f1) != TREE_CODE (f2))
1212 : rq1 = rq2;
1213 : /* Two non-static member functions have corresponding object parameters if:
1214 : + exactly one is an implicit object member function with no ref-qualifier
1215 : and the types of their object parameters ([dcl.fct]), after removing
1216 : top-level references, are the same, or
1217 : + their object parameters have the same type. */
1218 : /* ??? We treat member functions of different classes as corresponding even
1219 : though that means the object parameters have different types. */
1220 351379215 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1221 0 : rq1 = rq2;
1222 :
1223 351379215 : bool types_match = rq1 == rq2;
1224 :
1225 351379215 : if (types_match)
1226 : {
1227 351379215 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1228 351379215 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1229 351379215 : types_match = compparms (p1, p2);
1230 : }
1231 :
1232 : /* Two function declarations match if either has a requires-clause
1233 : then both have a requires-clause and their constraints-expressions
1234 : are equivalent. */
1235 351379215 : if (types_match && flag_concepts)
1236 43356920 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1237 :
1238 : return types_match;
1239 : }
1240 :
1241 : /* Subroutine of duplicate_decls: return truthvalue of whether
1242 : or not types of these decls match.
1243 :
1244 : For C++, we must compare the parameter list so that `int' can match
1245 : `int&' in a parameter position, but `int&' is not confused with
1246 : `const int&'. */
1247 :
1248 : int
1249 830027072 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1250 : {
1251 880998942 : int types_match;
1252 :
1253 880998942 : if (newdecl == olddecl)
1254 : return 1;
1255 :
1256 880878018 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1257 : /* If the two DECLs are not even the same kind of thing, we're not
1258 : interested in their types. */
1259 : return 0;
1260 :
1261 464696615 : gcc_assert (DECL_P (newdecl));
1262 :
1263 464696615 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1264 : {
1265 : /* Specializations of different templates are different functions
1266 : even if they have the same type. */
1267 307146003 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1268 307146003 : ? DECL_TI_TEMPLATE (newdecl)
1269 667933 : : NULL_TREE);
1270 307146003 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1271 307146003 : ? DECL_TI_TEMPLATE (olddecl)
1272 4255561 : : NULL_TREE);
1273 307146003 : if (t1 != t2)
1274 : return 0;
1275 :
1276 303558292 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1277 303558292 : && ! (DECL_EXTERN_C_P (newdecl)
1278 3033650 : && DECL_EXTERN_C_P (olddecl)))
1279 : return 0;
1280 :
1281 : /* A new declaration doesn't match a built-in one unless it
1282 : is also extern "C". */
1283 281432413 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1284 281432413 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1285 : return 0;
1286 :
1287 280763756 : tree f1 = TREE_TYPE (newdecl);
1288 280763756 : tree f2 = TREE_TYPE (olddecl);
1289 280763756 : if (TREE_CODE (f1) != TREE_CODE (f2))
1290 : return 0;
1291 :
1292 : /* A declaration with deduced return type should use its pre-deduction
1293 : type for declaration matching. */
1294 280580044 : tree r2 = fndecl_declared_return_type (olddecl);
1295 280580044 : tree r1 = fndecl_declared_return_type (newdecl);
1296 :
1297 : /* For memchr et al, allow const void* return type (as specified by C++)
1298 : when we expect void* (as in C). */
1299 280580044 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1300 6745747 : && DECL_EXTERN_C_P (olddecl)
1301 4151934 : && !same_type_p (r1, r2)
1302 70 : && TREE_CODE (r1) == POINTER_TYPE
1303 15 : && TREE_CODE (r2) == POINTER_TYPE
1304 280580056 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1305 : r2 = r1;
1306 :
1307 280580044 : tree p1 = TYPE_ARG_TYPES (f1);
1308 280580044 : tree p2 = TYPE_ARG_TYPES (f2);
1309 :
1310 280580044 : if (same_type_p (r1, r2))
1311 : {
1312 180214345 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1313 180214313 : && fndecl_built_in_p (olddecl))
1314 : {
1315 50 : types_match = self_promoting_args_p (p1);
1316 50 : if (p1 == void_list_node)
1317 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1318 : }
1319 : else
1320 180214207 : types_match =
1321 180214207 : compparms (p1, p2)
1322 30551036 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1323 210764847 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1324 1283325 : || comp_type_attributes (TREE_TYPE (newdecl),
1325 1283325 : TREE_TYPE (olddecl)) != 0);
1326 : }
1327 : else
1328 : types_match = 0;
1329 :
1330 : /* Two function declarations match if either has a requires-clause
1331 : then both have a requires-clause and their constraints-expressions
1332 : are equivalent. */
1333 30550669 : if (types_match && flag_concepts)
1334 30039974 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1335 :
1336 : /* The decls dont match if they correspond to two different versions
1337 : of the same function. Disallow extern "C" functions to be
1338 : versions for now. */
1339 30550669 : if (types_match
1340 30344187 : && !DECL_EXTERN_C_P (newdecl)
1341 21817052 : && !DECL_EXTERN_C_P (olddecl)
1342 52363718 : && disjoint_version_decls (newdecl, olddecl))
1343 : {
1344 8880 : if (record_versions)
1345 8793 : maybe_version_functions (newdecl, olddecl);
1346 : return 0;
1347 : }
1348 : }
1349 157550612 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1350 : {
1351 155692135 : if (!template_heads_equivalent_p (newdecl, olddecl))
1352 : return 0;
1353 :
1354 50973435 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1355 50973435 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1356 :
1357 50973435 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1358 : return 0;
1359 :
1360 : /* Two template types match if they are the same. Otherwise, compare
1361 : the underlying declarations. */
1362 50973426 : if (TREE_CODE (newres) == TYPE_DECL)
1363 1556 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1364 : else
1365 : types_match = decls_match (newres, oldres);
1366 : }
1367 : else
1368 : {
1369 : /* Need to check scope for variable declaration (VAR_DECL).
1370 : For typedef (TYPE_DECL), scope is ignored. */
1371 1858477 : if (VAR_P (newdecl)
1372 1252197 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1373 : /* [dcl.link]
1374 : Two declarations for an object with C language linkage
1375 : with the same name (ignoring the namespace that qualify
1376 : it) that appear in different namespace scopes refer to
1377 : the same object. */
1378 2052582 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1379 : return 0;
1380 :
1381 1664420 : if (TREE_TYPE (newdecl) == error_mark_node)
1382 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1383 1664420 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1384 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1385 1664420 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1386 : types_match = 0;
1387 : else
1388 1664420 : types_match = comptypes (TREE_TYPE (newdecl),
1389 1664420 : TREE_TYPE (olddecl),
1390 : COMPARE_REDECLARATION);
1391 : }
1392 :
1393 : return types_match;
1394 : }
1395 :
1396 : /* Mark DECL as versioned if it isn't already. */
1397 :
1398 : void
1399 17676 : maybe_mark_function_versioned (tree decl)
1400 : {
1401 17676 : if (!DECL_FUNCTION_VERSIONED (decl))
1402 : {
1403 : /* We need to insert function version now to make sure the correct
1404 : pre-mangled assembler name is recorded. */
1405 1068 : cgraph_node *node = cgraph_node::get_create (decl);
1406 :
1407 1068 : if (!node->function_version ())
1408 1068 : node->insert_new_function_version ();
1409 :
1410 1068 : DECL_FUNCTION_VERSIONED (decl) = 1;
1411 : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1412 : to include the version marker. */
1413 1068 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1414 1062 : mangle_decl (decl);
1415 : }
1416 17676 : }
1417 :
1418 : /* NEWDECL and OLDDECL have identical signatures. If they are
1419 : different versions adjust them, record function versions, and return
1420 : true. */
1421 :
1422 : bool
1423 209681 : maybe_version_functions (tree newdecl, tree olddecl)
1424 : {
1425 209681 : if (!disjoint_version_decls (newdecl, olddecl))
1426 : return false;
1427 :
1428 8820 : maybe_mark_function_versioned (olddecl);
1429 8820 : if (DECL_LOCAL_DECL_P (olddecl))
1430 : {
1431 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1432 18 : maybe_mark_function_versioned (olddecl);
1433 : }
1434 :
1435 8820 : maybe_mark_function_versioned (newdecl);
1436 8820 : if (DECL_LOCAL_DECL_P (newdecl))
1437 : {
1438 : /* Unfortunately, we can get here before pushdecl naturally calls
1439 : push_local_extern_decl_alias, so we need to call it directly. */
1440 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1441 6 : push_local_extern_decl_alias (newdecl);
1442 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1443 18 : maybe_mark_function_versioned (newdecl);
1444 : }
1445 :
1446 : /* Add the new version to the function version structure. */
1447 8820 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1448 8820 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1449 8820 : if (!fn_v)
1450 15 : fn_v = fn_node->insert_new_function_version ();
1451 :
1452 8820 : cgraph_node::add_function_version (fn_v, newdecl);
1453 :
1454 8820 : return true;
1455 : }
1456 :
1457 : /* If NEWDECL is `static' and an `extern' was seen previously,
1458 : warn about it. OLDDECL is the previous declaration.
1459 :
1460 : Note that this does not apply to the C++ case of declaring
1461 : a variable `extern const' and then later `const'.
1462 :
1463 : Don't complain about built-in functions, since they are beyond
1464 : the user's control. */
1465 :
1466 : void
1467 20668362 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1468 : {
1469 20668362 : if (TREE_CODE (newdecl) == TYPE_DECL
1470 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1471 : || TREE_CODE (newdecl) == CONST_DECL
1472 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1473 20668350 : return;
1474 :
1475 : /* Don't get confused by static member functions; that's a different
1476 : use of `static'. */
1477 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1478 15881095 : && DECL_STATIC_FUNCTION_P (newdecl))
1479 : return;
1480 :
1481 : /* If the old declaration was `static', or the new one isn't, then
1482 : everything is OK. */
1483 16226103 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1484 : return;
1485 :
1486 : /* It's OK to declare a builtin function as `static'. */
1487 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1488 12 : && DECL_ARTIFICIAL (olddecl))
1489 : return;
1490 :
1491 12 : auto_diagnostic_group d;
1492 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1493 : "%qD was declared %<extern%> and later %<static%>", newdecl))
1494 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1495 : "previous declaration of %qD", olddecl);
1496 12 : }
1497 :
1498 : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1499 : function templates. If their exception specifications do not
1500 : match, issue a diagnostic. */
1501 :
1502 : static void
1503 20087929 : check_redeclaration_exception_specification (tree new_decl,
1504 : tree old_decl)
1505 : {
1506 20087929 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1507 20087929 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1508 :
1509 : /* Two default specs are equivalent, don't force evaluation. */
1510 7755813 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1511 20237835 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1512 : return;
1513 :
1514 20077851 : if (!type_dependent_expression_p (old_decl))
1515 : {
1516 12327975 : maybe_instantiate_noexcept (new_decl);
1517 12327975 : maybe_instantiate_noexcept (old_decl);
1518 : }
1519 20077851 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1520 20077851 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1521 :
1522 : /* [except.spec]
1523 :
1524 : If any declaration of a function has an exception-specification,
1525 : all declarations, including the definition and an explicit
1526 : specialization, of that function shall have an
1527 : exception-specification with the same set of type-ids. */
1528 20077851 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1529 16050665 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1530 36128371 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1531 : {
1532 329 : const char *const msg
1533 : = G_("declaration of %qF has a different exception specifier");
1534 329 : bool complained = true;
1535 329 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1536 329 : auto_diagnostic_group d;
1537 :
1538 : /* Be permissive about C++98 vs C++11 operator new declarations. */
1539 658 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1540 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1541 331 : && (nothrow_spec_p (new_exceptions)
1542 1 : == nothrow_spec_p (old_exceptions)));
1543 :
1544 329 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1545 329 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1546 : /* Don't fuss about the C library; the C library functions are not
1547 : specified to have exception specifications (just behave as if they
1548 : have them), but some implementations include them. */
1549 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1550 96 : else if (!flag_exceptions)
1551 : /* We used to silently permit mismatched eh specs with
1552 : -fno-exceptions, so only complain if -pedantic. */
1553 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1554 93 : else if (!new_exceptions || global_new)
1555 : /* Reduce to pedwarn for omitted exception specification. No warning
1556 : flag for this; silence the warning by correcting the code. */
1557 18 : complained = pedwarn (new_loc, 0, msg, new_decl);
1558 : else
1559 75 : error_at (new_loc, msg, new_decl);
1560 :
1561 329 : if (complained)
1562 99 : inform (DECL_SOURCE_LOCATION (old_decl),
1563 : "from previous declaration %qF", old_decl);
1564 :
1565 : /* Copy the old exception specification if new_decl has none. Unless the
1566 : old decl is extern "C", as obscure code might depend on the type of
1567 : the new declaration (e.g. noexcept-type19.C). */
1568 329 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1569 17 : TREE_TYPE (new_decl)
1570 34 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1571 329 : }
1572 : }
1573 :
1574 : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1575 : Otherwise issue diagnostics. */
1576 :
1577 : bool
1578 20668888 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1579 : {
1580 20668888 : old_decl = STRIP_TEMPLATE (old_decl);
1581 20668888 : new_decl = STRIP_TEMPLATE (new_decl);
1582 20668888 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1583 20617181 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1584 : return true;
1585 20617181 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1586 20617181 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1587 : {
1588 20460515 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1589 : return true;
1590 40176688 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1591 20088344 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1592 : return true;
1593 : }
1594 156672 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1595 : {
1596 : /* With -fimplicit-constexpr, ignore changes in the constexpr
1597 : keyword. */
1598 71 : if (flag_implicit_constexpr
1599 71 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1600 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1601 : return true;
1602 71 : if (fndecl_built_in_p (old_decl))
1603 : {
1604 : /* Hide a built-in declaration. */
1605 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1606 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1607 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1608 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1609 : return true;
1610 : }
1611 : /* 7.1.5 [dcl.constexpr]
1612 : Note: An explicit specialization can differ from the template
1613 : declaration with respect to the constexpr specifier. */
1614 71 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1615 71 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1616 : return true;
1617 :
1618 42 : const char *kind = "constexpr";
1619 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1620 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1621 : kind = "consteval";
1622 42 : auto_diagnostic_group d;
1623 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1624 : "redeclaration %qD differs in %qs "
1625 : "from previous declaration", new_decl,
1626 : kind);
1627 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1628 : "previous declaration %qD", old_decl);
1629 42 : return false;
1630 42 : }
1631 : return true;
1632 : }
1633 :
1634 : /* DECL is a redeclaration of a function or function template. If
1635 : it does have default arguments issue a diagnostic. Note: this
1636 : function is used to enforce the requirements in C++11 8.3.6 about
1637 : no default arguments in redeclarations. */
1638 :
1639 : static void
1640 12029388 : check_redeclaration_no_default_args (tree decl)
1641 : {
1642 12029388 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1643 :
1644 12029388 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1645 35415309 : t && t != void_list_node; t = TREE_CHAIN (t))
1646 23385978 : if (TREE_PURPOSE (t))
1647 : {
1648 57 : permerror (DECL_SOURCE_LOCATION (decl),
1649 : "redeclaration of %q#D may not have default "
1650 : "arguments", decl);
1651 57 : return;
1652 : }
1653 : }
1654 :
1655 : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1656 : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1657 : the TEMPLATE_DECLs in case of function templates). This function is used
1658 : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1659 : "If a friend declaration specifies a default argument expression, that
1660 : declaration shall be a definition and shall be the only declaration of
1661 : the function or function template in the translation unit." */
1662 :
1663 : static void
1664 7519394 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1665 : {
1666 14309952 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1667 : return;
1668 :
1669 1878307 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1670 1878307 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1671 4967837 : t1 && t1 != void_list_node;
1672 3089530 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1673 4392312 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1674 8814851 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1675 : {
1676 36 : auto_diagnostic_group d;
1677 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1678 : "friend declaration of %q#D specifies default "
1679 : "arguments and isn%'t the only declaration", newdecl))
1680 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1681 : "previous declaration of %q#D", olddecl);
1682 36 : return;
1683 36 : }
1684 : }
1685 :
1686 : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1687 : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1688 :
1689 : static void
1690 19505728 : merge_attribute_bits (tree newdecl, tree olddecl)
1691 : {
1692 19505728 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1693 19505728 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1694 19505728 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1695 19505728 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1696 19505728 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1697 19505728 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1698 19505728 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1699 19505728 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1700 19505728 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1701 19505728 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1702 19505728 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1703 19505728 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1704 19505728 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1705 19505728 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1706 19505728 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1707 19505728 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1708 19505728 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1709 19505728 : DECL_NO_LIMIT_STACK (olddecl) |= DECL_NO_LIMIT_STACK (newdecl);
1710 39011456 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1711 19505728 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1712 39011456 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl)
1713 19505728 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl);
1714 19505728 : DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1715 19505728 : DECL_IS_NOVOPS (olddecl) |= DECL_IS_NOVOPS (newdecl);
1716 19505728 : }
1717 :
1718 : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1719 : && lookup_attribute ("gnu_inline", \
1720 : DECL_ATTRIBUTES (fn)))
1721 :
1722 : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1723 : ambiguates olddecl. Returns true if an error occurs. */
1724 :
1725 : static bool
1726 151482337 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1727 : {
1728 151482337 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1729 151482337 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1730 : /* Function template declarations can be differentiated by parameter
1731 : and return type. */
1732 151482337 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1733 151482337 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1734 151482337 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1735 : TREE_TYPE (TREE_TYPE (olddecl))))
1736 : {
1737 : /* ... and also by their template-heads and requires-clauses. */
1738 7360 : if (template_heads_equivalent_p (newdecl, olddecl)
1739 7360 : && function_requirements_equivalent_p (newres, oldres))
1740 : {
1741 0 : auto_diagnostic_group d;
1742 0 : error ("ambiguating new declaration %q+#D", newdecl);
1743 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1744 : "old declaration %q#D", olddecl);
1745 0 : return true;
1746 0 : }
1747 :
1748 : /* FIXME: The types are the same but the are differences
1749 : in either the template heads or function requirements.
1750 : We should be able to diagnose a set of common errors
1751 : stemming from these declarations. For example:
1752 :
1753 : template<typename T> requires C void f(...);
1754 : template<typename T> void f(...) requires C;
1755 :
1756 : These are functionally equivalent but not equivalent. */
1757 : }
1758 :
1759 : return false;
1760 : }
1761 :
1762 : /* OLD_PARMS is the innermost set of template parameters for some template
1763 : declaration, and NEW_PARMS is the corresponding set of template parameters
1764 : for a redeclaration of that template. Merge the default arguments within
1765 : these two sets of parameters. OLDDECL is the origin of OLD_PARMS. */
1766 :
1767 : bool
1768 9399599 : merge_default_template_args (tree new_parms, tree old_parms, tree olddecl)
1769 : {
1770 9399599 : bool class_p = DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (olddecl));
1771 :
1772 9399599 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1773 : == TREE_VEC_LENGTH (old_parms));
1774 26536290 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1775 : {
1776 17136715 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1777 17136715 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1778 17136715 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1779 17136715 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1780 17136715 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1781 : return false;
1782 241542 : if (new_default != NULL_TREE && old_default != NULL_TREE
1783 17137817 : && !(redefinable_import_p (olddecl)
1784 542 : && cp_tree_equal (new_default, old_default)))
1785 : {
1786 21 : auto_diagnostic_group d;
1787 21 : error ("redefinition of default argument for %q+#D", new_parm);
1788 21 : inform (DECL_SOURCE_LOCATION (old_parm),
1789 : "original definition appeared here");
1790 21 : return false;
1791 21 : }
1792 17136691 : else if (new_default != NULL_TREE)
1793 : /* Update the previous template parameters (which are the ones
1794 : that will really count) with the new default value. */
1795 241521 : old_default = new_default;
1796 16895170 : else if (class_p && old_default != NULL_TREE)
1797 : /* Update the new parameters, too; they'll be used as the
1798 : parameters for any members. */
1799 1045263 : new_default = old_default;
1800 : }
1801 : return true;
1802 : }
1803 :
1804 : /* Helper function for duplicate_decls and push_local_extern_decl_alias.
1805 : Merge parameter attributes and names between NEWDECL and OLDDECL.
1806 : NEW_DEFINES_FUNCTION and TYPES_MATCH argument like variables in
1807 : duplicate_decls, EXTERN_ALIAS false for duplicate_decls and true for
1808 : push_local_extern_decl_alias. */
1809 :
1810 : void
1811 15881284 : merge_decl_arguments (tree newdecl, tree olddecl, bool new_defines_function,
1812 : bool types_match, bool extern_alias)
1813 : {
1814 15881284 : tree oldarg, newarg, type = NULL_TREE;
1815 15881284 : tree first_user_parm = NULL_TREE;
1816 15881284 : if (extern_alias)
1817 189 : first_user_parm = FUNCTION_FIRST_USER_PARM (newdecl);
1818 15881284 : for (oldarg = DECL_ARGUMENTS (olddecl), newarg = DECL_ARGUMENTS (newdecl);
1819 44579300 : oldarg && newarg;
1820 28698016 : oldarg = DECL_CHAIN (oldarg), newarg = DECL_CHAIN (newarg))
1821 : {
1822 28698016 : DECL_ATTRIBUTES (newarg)
1823 28698016 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
1824 28698016 : if (lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (newarg))
1825 28698085 : && !lookup_attribute (NULL, "indeterminate",
1826 69 : DECL_ATTRIBUTES (oldarg)))
1827 : {
1828 18 : auto_diagnostic_group d;
1829 18 : error_at (DECL_SOURCE_LOCATION (newarg),
1830 : "%<indeterminate%> attribute not specified for parameter "
1831 : "%qD on the first declaration of its function", newarg);
1832 18 : inform (DECL_SOURCE_LOCATION (oldarg), "earlier declaration");
1833 18 : }
1834 : /* ??? Should attributes propagate out from a block extern? If so,
1835 : we should do that for the function itself, not just parameters. */
1836 28698016 : if (!extern_alias || flag_reflection)
1837 28697849 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1838 28698016 : if (!flag_reflection)
1839 28286652 : continue;
1840 : /* For extern_alias set DECL_HAS_DEFAULT_ARGUMENT_P on oldarg
1841 : if the local extern has a default argument for that parameter. */
1842 411364 : if (extern_alias)
1843 : {
1844 36 : if (newarg == first_user_parm)
1845 12 : type = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1846 24 : else if (type)
1847 24 : type = TREE_CHAIN (type);
1848 72 : if (type && TREE_PURPOSE (type))
1849 4 : DECL_HAS_DEFAULT_ARGUMENT_P (oldarg) = 1;
1850 : }
1851 : else
1852 : {
1853 : /* Otherwise propagate the flag. */
1854 411328 : if (DECL_HAS_DEFAULT_ARGUMENT_P (oldarg))
1855 0 : DECL_HAS_DEFAULT_ARGUMENT_P (newarg) = 1;
1856 411328 : if (DECL_HAS_DEFAULT_ARGUMENT_P (newarg))
1857 0 : DECL_HAS_DEFAULT_ARGUMENT_P (oldarg) = 1;
1858 : }
1859 : /* Merge names for std::meta::has_identifier and
1860 : std::meta::{,u8}identifier_of purposes. If they are different and
1861 : both oldarg and newarg are named, add flag to force that
1862 : std::meta::has_identifier returns false. If one is named and one is
1863 : unnamed, if neither is a olddecl nor newdecl is definition, propagate
1864 : DECL_NAME to both. Otherwise stash the old name into "old parm name"
1865 : artificial attribute. */
1866 411364 : if (DECL_NAME (oldarg) != DECL_NAME (newarg))
1867 : {
1868 45850 : if (DECL_NAME (oldarg) && DECL_NAME (newarg))
1869 : {
1870 : /* Different names. */
1871 10338 : MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1872 10338 : MULTIPLE_NAMES_PARM_P (newarg) = 1;
1873 : }
1874 35512 : else if (!new_defines_function
1875 35512 : && types_match
1876 35512 : && DECL_INITIAL (olddecl) == NULL_TREE)
1877 : {
1878 : /* For 2 non-definitions with matching types, one is named and
1879 : one unnamed, propagate name to both. */
1880 6708 : if (DECL_NAME (oldarg))
1881 5628 : DECL_NAME (newarg) = DECL_NAME (oldarg);
1882 : else
1883 1080 : DECL_NAME (oldarg) = DECL_NAME (newarg);
1884 : }
1885 : /* Depending on which PARM_DECL we'll keep, look at the other
1886 : PARM_DECL's name. */
1887 28804 : else if (tree name = ((new_defines_function || !types_match)
1888 28804 : ? DECL_NAME (oldarg) : DECL_NAME (newarg)))
1889 : {
1890 234 : tree opn = lookup_attribute ("old parm name",
1891 234 : DECL_ATTRIBUTES (oldarg));
1892 234 : if (opn)
1893 : {
1894 4 : if (TREE_VALUE (TREE_VALUE (opn)) == name)
1895 : /* Name already in "old parm name" attribute. */;
1896 : else
1897 : {
1898 : /* Different names. */
1899 0 : MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1900 0 : MULTIPLE_NAMES_PARM_P (newarg) = 1;
1901 : }
1902 : }
1903 : else
1904 : {
1905 : /* Save name into attribute. */
1906 230 : DECL_ATTRIBUTES (newarg)
1907 230 : = tree_cons (get_identifier ("old parm name"),
1908 : tree_cons (NULL_TREE, name, NULL_TREE),
1909 230 : DECL_ATTRIBUTES (newarg));
1910 230 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1911 : }
1912 : }
1913 28570 : else if (extern_alias)
1914 2 : DECL_NAME (newarg) = DECL_NAME (oldarg);
1915 : }
1916 : }
1917 15881284 : }
1918 :
1919 : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1920 : If the redeclaration is invalid, a diagnostic is issued, and the
1921 : error_mark_node is returned. Otherwise, OLDDECL is returned.
1922 :
1923 : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1924 : returned.
1925 :
1926 : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1927 : if the old decl was hidden.
1928 :
1929 : Hidden decls can be anticipated builtins, injected friends, or
1930 : (coming soon) injected from a local-extern decl. */
1931 :
1932 : tree
1933 339014566 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1934 : {
1935 339163892 : unsigned olddecl_uid = DECL_UID (olddecl);
1936 339163892 : int types_match = 0;
1937 339163892 : int new_defines_function = 0;
1938 339163892 : tree new_template_info;
1939 339163892 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1940 339163892 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1941 :
1942 339163892 : if (newdecl == olddecl)
1943 : return olddecl;
1944 :
1945 338297841 : types_match = decls_match (newdecl, olddecl);
1946 :
1947 : /* If either the type of the new decl or the type of the old decl is an
1948 : error_mark_node, then that implies that we have already issued an
1949 : error (earlier) for some bogus type specification, and in that case,
1950 : it is rather pointless to harass the user with yet more error message
1951 : about the same declaration, so just pretend the types match here. */
1952 338297841 : if (TREE_TYPE (newdecl) == error_mark_node
1953 338297841 : || TREE_TYPE (olddecl) == error_mark_node)
1954 : return error_mark_node;
1955 :
1956 338297815 : bool mod_redefinable = redefinable_import_p (olddecl);
1957 :
1958 : /* Check for redeclaration and other discrepancies. */
1959 338297815 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1960 338297815 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1961 : {
1962 6489227 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1963 : {
1964 : /* Avoid warnings redeclaring built-ins which have not been
1965 : explicitly declared. */
1966 203719 : if (was_hidden)
1967 : {
1968 203561 : if (TREE_PUBLIC (newdecl)
1969 203561 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1970 18 : warning_at (newdecl_loc,
1971 18 : OPT_Wbuiltin_declaration_mismatch,
1972 : "built-in function %qD declared as non-function",
1973 : newdecl);
1974 : return NULL_TREE;
1975 : }
1976 :
1977 : /* If you declare a built-in or predefined function name as static,
1978 : the old definition is overridden, but optionally warn this was a
1979 : bad choice of name. */
1980 158 : if (! TREE_PUBLIC (newdecl))
1981 : {
1982 158 : warning_at (newdecl_loc,
1983 158 : OPT_Wshadow,
1984 158 : fndecl_built_in_p (olddecl)
1985 : ? G_("shadowing built-in function %q#D")
1986 : : G_("shadowing library function %q#D"), olddecl);
1987 : /* Discard the old built-in function. */
1988 158 : return NULL_TREE;
1989 : }
1990 : /* If the built-in is not ansi, then programs can override
1991 : it even globally without an error. */
1992 0 : else if (! fndecl_built_in_p (olddecl))
1993 0 : warning_at (newdecl_loc, 0,
1994 : "library function %q#D redeclared as non-function %q#D",
1995 : olddecl, newdecl);
1996 : else
1997 0 : error_at (newdecl_loc,
1998 : "declaration of %q#D conflicts with built-in "
1999 : "declaration %q#D", newdecl, olddecl);
2000 : return NULL_TREE;
2001 : }
2002 6285508 : else if (!types_match)
2003 : {
2004 : /* Avoid warnings redeclaring built-ins which have not been
2005 : explicitly declared. */
2006 2258322 : if (was_hidden)
2007 : {
2008 818184 : tree t1, t2;
2009 :
2010 : /* A new declaration doesn't match a built-in one unless it
2011 : is also extern "C". */
2012 818184 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
2013 818184 : gcc_assert (DECL_EXTERN_C_P (olddecl));
2014 818184 : if (!DECL_EXTERN_C_P (newdecl))
2015 : return NULL_TREE;
2016 :
2017 149527 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2018 149527 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2019 258072 : t1 || t2;
2020 108545 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2021 : {
2022 258054 : if (!t1 || !t2)
2023 : break;
2024 : /* FILE, tm types are not known at the time
2025 : we create the builtins. */
2026 795290 : for (unsigned i = 0;
2027 1053182 : i < sizeof (builtin_structptr_types)
2028 : / sizeof (builtin_structptr_type);
2029 : ++i)
2030 944631 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
2031 : {
2032 149341 : tree t = TREE_VALUE (t1);
2033 :
2034 149341 : if (TYPE_PTR_P (t)
2035 298682 : && TYPE_IDENTIFIER (TREE_TYPE (t))
2036 149341 : == get_identifier (builtin_structptr_types[i].str)
2037 298667 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
2038 : {
2039 149326 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2040 :
2041 149326 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
2042 149326 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2043 149326 : types_match = decls_match (newdecl, olddecl);
2044 149326 : if (types_match)
2045 : return duplicate_decls (newdecl, olddecl,
2046 : hiding, was_hidden);
2047 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
2048 : }
2049 15 : goto next_arg;
2050 : }
2051 :
2052 108551 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2053 : break;
2054 108545 : next_arg:;
2055 : }
2056 :
2057 201 : warning_at (newdecl_loc,
2058 201 : OPT_Wbuiltin_declaration_mismatch,
2059 : "declaration of %q#D conflicts with built-in "
2060 : "declaration %q#D", newdecl, olddecl);
2061 : }
2062 1440138 : else if ((DECL_EXTERN_C_P (newdecl)
2063 15 : && DECL_EXTERN_C_P (olddecl))
2064 2880261 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2065 1440123 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2066 : {
2067 : /* Don't really override olddecl for __* prefixed builtins
2068 : except for __[^b]*_chk, the compiler might be using those
2069 : explicitly. */
2070 15 : if (fndecl_built_in_p (olddecl))
2071 : {
2072 15 : tree id = DECL_NAME (olddecl);
2073 15 : const char *name = IDENTIFIER_POINTER (id);
2074 15 : size_t len;
2075 :
2076 15 : if (name[0] == '_'
2077 15 : && name[1] == '_'
2078 30 : && (startswith (name + 2, "builtin_")
2079 6 : || (len = strlen (name)) <= strlen ("___chk")
2080 6 : || memcmp (name + len - strlen ("_chk"),
2081 : "_chk", strlen ("_chk") + 1) != 0))
2082 : {
2083 15 : if (DECL_INITIAL (newdecl))
2084 : {
2085 6 : error_at (newdecl_loc,
2086 : "definition of %q#D ambiguates built-in "
2087 : "declaration %q#D", newdecl, olddecl);
2088 6 : return error_mark_node;
2089 : }
2090 9 : auto_diagnostic_group d;
2091 9 : if (permerror (newdecl_loc,
2092 : "new declaration %q#D ambiguates built-in"
2093 : " declaration %q#D", newdecl, olddecl)
2094 9 : && flag_permissive)
2095 3 : inform (newdecl_loc,
2096 : "ignoring the %q#D declaration", newdecl);
2097 9 : return flag_permissive ? olddecl : error_mark_node;
2098 9 : }
2099 : }
2100 :
2101 : /* A near match; override the builtin. */
2102 :
2103 0 : if (TREE_PUBLIC (newdecl))
2104 0 : warning_at (newdecl_loc,
2105 0 : OPT_Wbuiltin_declaration_mismatch,
2106 : "new declaration %q#D ambiguates built-in "
2107 : "declaration %q#D", newdecl, olddecl);
2108 : else
2109 0 : warning (OPT_Wshadow,
2110 : fndecl_built_in_p (olddecl)
2111 : ? G_("shadowing built-in function %q#D")
2112 : : G_("shadowing library function %q#D"), olddecl);
2113 : }
2114 : else
2115 : /* Discard the old built-in function. */
2116 : return NULL_TREE;
2117 :
2118 : /* Replace the old RTL to avoid problems with inlining. */
2119 201 : COPY_DECL_RTL (newdecl, olddecl);
2120 : }
2121 : else
2122 : {
2123 : /* Even if the types match, prefer the new declarations type
2124 : for built-ins which have not been explicitly declared,
2125 : for exception lists, etc... */
2126 4027186 : tree type = TREE_TYPE (newdecl);
2127 4027186 : tree attribs = (*targetm.merge_type_attributes)
2128 4027186 : (TREE_TYPE (olddecl), type);
2129 :
2130 4027186 : type = cp_build_type_attribute_variant (type, attribs);
2131 4027186 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
2132 : }
2133 :
2134 : /* If a function is explicitly declared "throw ()", propagate that to
2135 : the corresponding builtin. */
2136 4027387 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
2137 3853002 : && was_hidden
2138 3852959 : && TREE_NOTHROW (newdecl)
2139 7606548 : && !TREE_NOTHROW (olddecl))
2140 : {
2141 28191 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
2142 28191 : tree tmpdecl = builtin_decl_explicit (fncode);
2143 28191 : if (tmpdecl && tmpdecl != olddecl && types_match)
2144 28191 : TREE_NOTHROW (tmpdecl) = 1;
2145 : }
2146 :
2147 : /* Whether or not the builtin can throw exceptions has no
2148 : bearing on this declarator. */
2149 4027387 : TREE_NOTHROW (olddecl) = 0;
2150 :
2151 4027387 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2152 : {
2153 : /* If a builtin function is redeclared as `static', merge
2154 : the declarations, but make the original one static. */
2155 3 : DECL_THIS_STATIC (olddecl) = 1;
2156 3 : TREE_PUBLIC (olddecl) = 0;
2157 :
2158 : /* Make the old declaration consistent with the new one so
2159 : that all remnants of the builtin-ness of this function
2160 : will be banished. */
2161 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2162 3 : COPY_DECL_RTL (newdecl, olddecl);
2163 : }
2164 : }
2165 331808588 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2166 : {
2167 : /* C++ Standard, 3.3, clause 4:
2168 : "[Note: a namespace name or a class template name must be unique
2169 : in its declarative region (7.3.2, clause 14). ]" */
2170 74095768 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
2171 74095741 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
2172 : /* Namespace conflicts with not namespace. */;
2173 36246719 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
2174 110342448 : || DECL_TYPE_TEMPLATE_P (newdecl))
2175 : /* Class template conflicts. */;
2176 74095723 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2177 36246719 : && DECL_TEMPLATE_RESULT (olddecl)
2178 36246719 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2179 110342436 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
2180 37799552 : && DECL_TEMPLATE_RESULT (newdecl)
2181 37799552 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2182 : /* Variable template conflicts. */;
2183 74095714 : else if (concept_definition_p (olddecl)
2184 148191422 : || concept_definition_p (newdecl))
2185 : /* Concept conflicts. */;
2186 74095702 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2187 36286303 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
2188 74135298 : || (TREE_CODE (olddecl) == FUNCTION_DECL
2189 37799565 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2190 : {
2191 : /* One is a function and the other is a template
2192 : function. */
2193 74046235 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2194 : return NULL_TREE;
2195 :
2196 : /* There can only be one! */
2197 58477 : auto_diagnostic_group d;
2198 58477 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
2199 58477 : && check_raw_literal_operator (olddecl))
2200 3 : error_at (newdecl_loc,
2201 : "literal operator %q#D conflicts with"
2202 : " raw literal operator", newdecl);
2203 58474 : else if (check_raw_literal_operator (newdecl))
2204 3 : error_at (newdecl_loc,
2205 : "raw literal operator %q#D conflicts with"
2206 : " literal operator template", newdecl);
2207 : else
2208 : return NULL_TREE;
2209 :
2210 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2211 6 : return error_mark_node;
2212 58477 : }
2213 49467 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2214 : /* A structured binding must be unique in its declarative region. */;
2215 39749 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2216 49464 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
2217 : /* One is an implicit typedef, that's ok. */
2218 : return NULL_TREE;
2219 :
2220 153 : auto_diagnostic_group d;
2221 153 : error ("%q#D redeclared as different kind of entity", newdecl);
2222 153 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2223 :
2224 153 : return error_mark_node;
2225 153 : }
2226 257712820 : else if (!types_match)
2227 : {
2228 241037278 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2229 : /* These are certainly not duplicate declarations; they're
2230 : from different scopes. */
2231 : return NULL_TREE;
2232 :
2233 241036632 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2234 : {
2235 151483414 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2236 151483414 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
2237 :
2238 : /* The name of a class template may not be declared to refer to
2239 : any other template, class, function, object, namespace, value,
2240 : or type in the same scope. */
2241 151483414 : if (TREE_CODE (oldres) == TYPE_DECL
2242 151482349 : || TREE_CODE (newres) == TYPE_DECL)
2243 : {
2244 : /* E.g. cstddef __byte_operand. Alternatively we could find a
2245 : mergeable definition in lookup_elaborated_type, but we'd still
2246 : need to push it into scope in the current TU. */
2247 1068 : if (mod_redefinable
2248 2127 : && (DECL_IMPLICIT_TYPEDEF_P (oldres)
2249 1059 : == DECL_IMPLICIT_TYPEDEF_P (newres)))
2250 1053 : goto return_old;
2251 :
2252 15 : auto_diagnostic_group d;
2253 15 : error_at (newdecl_loc,
2254 : "conflicting declaration of template %q#D", newdecl);
2255 15 : inform (olddecl_loc,
2256 : "previous declaration %q#D", olddecl);
2257 15 : return error_mark_node;
2258 15 : }
2259 :
2260 151482346 : else if (TREE_CODE (oldres) == FUNCTION_DECL
2261 151482337 : && TREE_CODE (newres) == FUNCTION_DECL)
2262 : {
2263 151482337 : if (duplicate_function_template_decls (newdecl, olddecl))
2264 0 : return error_mark_node;
2265 : return NULL_TREE;
2266 : }
2267 : return NULL_TREE;
2268 : }
2269 89553218 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2270 : {
2271 89552458 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2272 : {
2273 57 : auto_diagnostic_group d;
2274 57 : error_at (newdecl_loc,
2275 : "conflicting declaration of C function %q#D",
2276 : newdecl);
2277 57 : inform (olddecl_loc,
2278 : "previous declaration %q#D", olddecl);
2279 57 : return error_mark_node;
2280 57 : }
2281 : /* For function versions, params and types match, but they
2282 : are not ambiguous. */
2283 89552401 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2284 89544067 : && !DECL_FUNCTION_VERSIONED (olddecl))
2285 8379 : || !same_type_p (fndecl_declared_return_type (newdecl),
2286 : fndecl_declared_return_type (olddecl)))
2287 : /* Let constrained hidden friends coexist for now, we'll
2288 : check satisfaction later. */
2289 89544022 : && !member_like_constrained_friend_p (newdecl)
2290 81991587 : && !member_like_constrained_friend_p (olddecl)
2291 : // The functions have the same parameter types.
2292 80250684 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2293 80250684 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2294 : // And the same constraints.
2295 89552470 : && equivalently_constrained (newdecl, olddecl))
2296 : {
2297 69 : auto_diagnostic_group d;
2298 69 : error_at (newdecl_loc,
2299 : "ambiguating new declaration of %q#D", newdecl);
2300 69 : inform (olddecl_loc,
2301 : "old declaration %q#D", olddecl);
2302 69 : return error_mark_node;
2303 69 : }
2304 : else
2305 : return NULL_TREE;
2306 : }
2307 760 : else if (mod_redefinable
2308 606 : && TREE_CODE (newdecl) == TYPE_DECL
2309 1964 : && (DECL_IMPLICIT_TYPEDEF_P (newdecl)
2310 602 : == DECL_IMPLICIT_TYPEDEF_P (olddecl)))
2311 : /* Can also get here with e.g. typedef class {...} foo. */
2312 602 : goto return_old;
2313 158 : else if (mod_redefinable
2314 4 : && TREE_CODE (newdecl) == VAR_DECL
2315 4 : && undeduced_auto_decl (newdecl)
2316 162 : && !undeduced_auto_decl (olddecl))
2317 : /* We don't have fndecl_declared_return_type for vars. */
2318 4 : goto return_old;
2319 : else
2320 : {
2321 154 : auto_diagnostic_group d;
2322 154 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2323 154 : inform (olddecl_loc,
2324 : "previous declaration as %q#D", olddecl);
2325 154 : return error_mark_node;
2326 154 : }
2327 : }
2328 16675542 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2329 16675542 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2330 : {
2331 : /* OMP UDRs are never duplicates. */
2332 33 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2333 33 : auto_diagnostic_group d;
2334 33 : error_at (newdecl_loc,
2335 : "redeclaration of %<pragma omp declare reduction%>");
2336 33 : inform (olddecl_loc,
2337 : "previous %<pragma omp declare reduction%> declaration");
2338 33 : return error_mark_node;
2339 33 : }
2340 16675509 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2341 16675509 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2342 281 : && (!DECL_TEMPLATE_INFO (newdecl)
2343 281 : || (DECL_TI_TEMPLATE (newdecl)
2344 281 : != DECL_TI_TEMPLATE (olddecl))))
2345 11853993 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2346 582524 : && (!DECL_TEMPLATE_INFO (olddecl)
2347 582524 : || (DECL_TI_TEMPLATE (olddecl)
2348 582524 : != DECL_TI_TEMPLATE (newdecl))))))
2349 : /* It's OK to have a template specialization and a non-template
2350 : with the same type, or to have specializations of two
2351 : different templates with the same type. Note that if one is a
2352 : specialization, and the other is an instantiation of the same
2353 : template, that we do not exit at this point. That situation
2354 : can occur if we instantiate a template class, and then
2355 : specialize one of its methods. This situation is valid, but
2356 : the declarations must be merged in the usual way. */
2357 : return NULL_TREE;
2358 16675509 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2359 16675509 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2360 667569 : && !DECL_USE_TEMPLATE (newdecl))
2361 11853993 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2362 85326 : && !DECL_USE_TEMPLATE (olddecl))))
2363 : /* One of the declarations is a template instantiation, and the
2364 : other is not a template at all. That's OK. */
2365 : return NULL_TREE;
2366 16675509 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2367 : {
2368 : /* In [namespace.alias] we have:
2369 :
2370 : In a declarative region, a namespace-alias-definition can be
2371 : used to redefine a namespace-alias declared in that declarative
2372 : region to refer only to the namespace to which it already
2373 : refers.
2374 :
2375 : Therefore, if we encounter a second alias directive for the same
2376 : alias, we can just ignore the second directive. */
2377 31 : if (DECL_NAMESPACE_ALIAS (newdecl)
2378 31 : && (DECL_NAMESPACE_ALIAS (newdecl)
2379 28 : == DECL_NAMESPACE_ALIAS (olddecl)))
2380 22 : goto return_old;
2381 :
2382 : /* Leave it to update_binding to merge or report error. */
2383 : return NULL_TREE;
2384 : }
2385 : /* Check if the two decls are non-mergeable versioned decls. */
2386 16675478 : else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2387 : && diagnose_versioned_decls (olddecl, newdecl))
2388 : return error_mark_node;
2389 : else
2390 : {
2391 16675478 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2392 16675478 : if (errmsg)
2393 : {
2394 621 : auto_diagnostic_group d;
2395 621 : error_at (newdecl_loc, errmsg, newdecl);
2396 621 : if (DECL_NAME (olddecl) != NULL_TREE)
2397 621 : inform (olddecl_loc,
2398 621 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2399 : ? G_("%q#D previously defined here")
2400 : : G_("%q#D previously declared here"), olddecl);
2401 621 : if (cxx_dialect >= cxx26
2402 147 : && DECL_NAME (newdecl)
2403 147 : && id_equal (DECL_NAME (newdecl), "_")
2404 634 : && !name_independent_decl_p (newdecl))
2405 : {
2406 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2407 2 : inform (newdecl_loc,
2408 : "parameter declaration is not name-independent");
2409 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2410 : {
2411 3 : if (at_namespace_scope_p ())
2412 2 : inform (newdecl_loc,
2413 : "structured binding at namespace scope is not "
2414 : "name-independent");
2415 1 : else if (TREE_STATIC (newdecl))
2416 1 : inform (newdecl_loc,
2417 : "static structured binding is not "
2418 : "name-independent");
2419 0 : else if (DECL_EXTERNAL (newdecl))
2420 0 : inform (newdecl_loc,
2421 : "extern structured binding is not "
2422 : "name-independent");
2423 : }
2424 8 : else if (at_class_scope_p ()
2425 0 : && VAR_P (newdecl)
2426 8 : && TREE_STATIC (newdecl))
2427 0 : inform (newdecl_loc,
2428 : "static data member is not name-independent");
2429 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2430 3 : inform (newdecl_loc,
2431 : "variable at namespace scope is not name-independent");
2432 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2433 3 : inform (newdecl_loc,
2434 : "static variable is not name-independent");
2435 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2436 0 : inform (newdecl_loc,
2437 : "extern variable is not name-independent");
2438 : }
2439 621 : return error_mark_node;
2440 621 : }
2441 16674857 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2442 11853753 : && DECL_INITIAL (olddecl) != NULL_TREE
2443 19936 : && !prototype_p (TREE_TYPE (olddecl))
2444 16674857 : && prototype_p (TREE_TYPE (newdecl)))
2445 : {
2446 : /* Prototype decl follows defn w/o prototype. */
2447 0 : auto_diagnostic_group d;
2448 0 : if (warning_at (newdecl_loc, 0,
2449 : "prototype specified for %q#D", newdecl))
2450 0 : inform (olddecl_loc,
2451 : "previous non-prototype definition here");
2452 0 : }
2453 16146700 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2454 28528610 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2455 : {
2456 : /* [dcl.link]
2457 : If two declarations of the same function or object
2458 : specify different linkage-specifications ..., the program
2459 : is ill-formed.... Except for functions with C++ linkage,
2460 : a function declaration without a linkage specification
2461 : shall not precede the first linkage specification for
2462 : that function. A function can be declared without a
2463 : linkage specification after an explicit linkage
2464 : specification has been seen; the linkage explicitly
2465 : specified in the earlier declaration is not affected by
2466 : such a function declaration.
2467 :
2468 : DR 563 raises the question why the restrictions on
2469 : functions should not also apply to objects. Older
2470 : versions of G++ silently ignore the linkage-specification
2471 : for this example:
2472 :
2473 : namespace N {
2474 : extern int i;
2475 : extern "C" int i;
2476 : }
2477 :
2478 : which is clearly wrong. Therefore, we now treat objects
2479 : like functions. */
2480 1889 : if (current_lang_depth () == 0)
2481 : {
2482 : /* There is no explicit linkage-specification, so we use
2483 : the linkage from the previous declaration. */
2484 1877 : retrofit_lang_decl (newdecl);
2485 1877 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2486 : }
2487 : else
2488 : {
2489 12 : auto_diagnostic_group d;
2490 12 : error_at (newdecl_loc,
2491 : "conflicting declaration of %q#D with %qL linkage",
2492 12 : newdecl, DECL_LANGUAGE (newdecl));
2493 12 : inform (olddecl_loc,
2494 : "previous declaration with %qL linkage",
2495 12 : DECL_LANGUAGE (olddecl));
2496 12 : }
2497 : }
2498 :
2499 16674857 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2500 : ;
2501 15985545 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2502 : {
2503 : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2504 14072449 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2505 11185912 : && (/* grokfndecl passes member function templates too
2506 : as FUNCTION_DECLs. */
2507 8482416 : DECL_TEMPLATE_INFO (olddecl)
2508 : /* C++11 8.3.6/6.
2509 : Default arguments for a member function of a class
2510 : template shall be specified on the initial declaration
2511 : of the member function within the class template. */
2512 713514 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2513 : {
2514 7850154 : if (!mod_redefinable)
2515 7849268 : check_redeclaration_no_default_args (newdecl);
2516 :
2517 7850154 : if (DECL_TEMPLATE_INFO (olddecl)
2518 7850154 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2519 : {
2520 2340459 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2521 2340459 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2522 2340459 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2523 2340459 : tree old_parms
2524 2340459 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2525 2340459 : merge_default_template_args (new_parms, old_parms, olddecl);
2526 : }
2527 : }
2528 : else
2529 : {
2530 3335758 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2531 3335758 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2532 3335758 : int i = 1;
2533 :
2534 12033743 : for (; t1 && t1 != void_list_node;
2535 5362227 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2536 5364296 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2537 : {
2538 111 : if (!cp_tree_equal (TREE_PURPOSE (t1),
2539 111 : TREE_PURPOSE (t2)))
2540 : {
2541 30 : auto_diagnostic_group d;
2542 30 : error_at (newdecl_loc,
2543 : "default argument given for parameter %d "
2544 : "of %q#D", i, newdecl);
2545 30 : inform (olddecl_loc,
2546 : "previous specification in %q#D here",
2547 : olddecl);
2548 30 : }
2549 81 : else if (!mod_redefinable)
2550 : {
2551 24 : auto_diagnostic_group d;
2552 24 : if (permerror (newdecl_loc,
2553 : "default argument given for parameter "
2554 : "%d of %q#D", i, newdecl))
2555 24 : inform (olddecl_loc,
2556 : "previous specification in %q#D here",
2557 : olddecl);
2558 24 : }
2559 : }
2560 :
2561 : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2562 : argument expression, that declaration... shall be the only
2563 : declaration of the function or function template in the
2564 : translation unit." */
2565 3335758 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2566 : }
2567 : }
2568 : }
2569 :
2570 : /* Do not merge an implicit typedef with an explicit one. In:
2571 :
2572 : class A;
2573 : ...
2574 : typedef class A A __attribute__ ((foo));
2575 :
2576 : the attribute should apply only to the typedef. */
2577 20702244 : if (TREE_CODE (olddecl) == TYPE_DECL
2578 20702244 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2579 50694 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2580 : return NULL_TREE;
2581 :
2582 20668428 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2583 : return NULL_TREE;
2584 :
2585 20668416 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2586 30 : return error_mark_node;
2587 :
2588 20668386 : if (modules_p ()
2589 74876 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2590 20715011 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2591 : {
2592 46625 : if (!module_may_redeclare (olddecl, newdecl))
2593 24 : return error_mark_node;
2594 :
2595 46601 : if (!hiding)
2596 : {
2597 : /* The old declaration should match the exportingness of the new
2598 : declaration. But hidden friend declarations just keep the
2599 : exportingness of the old declaration; see CWG2588. */
2600 41929 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2601 41929 : if (DECL_LANG_SPECIFIC (not_tmpl)
2602 41805 : && DECL_MODULE_ATTACH_P (not_tmpl)
2603 : /* Typedefs are not entities and so are OK to be redeclared
2604 : as exported: see [module.interface]/p6. */
2605 42120 : && TREE_CODE (olddecl) != TYPE_DECL)
2606 : {
2607 179 : if (DECL_MODULE_EXPORT_P (newdecl)
2608 179 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2609 : {
2610 15 : auto_diagnostic_group d;
2611 15 : error ("conflicting exporting for declaration %qD", newdecl);
2612 15 : inform (olddecl_loc,
2613 : "previously declared here without exporting");
2614 15 : }
2615 : }
2616 41750 : else if (DECL_MODULE_EXPORT_P (newdecl))
2617 10476 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2618 : }
2619 : }
2620 :
2621 : /* We have committed to returning OLDDECL at this point. */
2622 :
2623 : /* If new decl is `static' and an `extern' was seen previously,
2624 : warn about it. */
2625 20668362 : warn_extern_redeclared_static (newdecl, olddecl);
2626 :
2627 : /* True to merge attributes between the declarations, false to
2628 : set OLDDECL's attributes to those of NEWDECL (for template
2629 : explicit specializations that specify their own attributes
2630 : independent of those specified for the primary template). */
2631 20668362 : bool merge_attr;
2632 41336724 : merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2633 15881095 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2634 21250871 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2635 :
2636 20668362 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2637 : {
2638 15881095 : if (merge_attr)
2639 : {
2640 15298852 : {
2641 15298852 : auto_diagnostic_group d;
2642 15298852 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2643 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2644 : ? G_("previous definition of %qD here")
2645 : : G_("previous declaration of %qD here"), olddecl);
2646 15298852 : }
2647 :
2648 : /* [dcl.attr.noreturn]: The first declaration of a function shall
2649 : specify the noreturn attribute if any declaration of that function
2650 : specifies the noreturn attribute. */
2651 15298852 : tree a;
2652 15298852 : if (TREE_THIS_VOLATILE (newdecl)
2653 100449 : && !TREE_THIS_VOLATILE (olddecl)
2654 : /* This applies to [[noreturn]] only, not its GNU variants. */
2655 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2656 54 : && cxx11_attribute_p (a)
2657 15298873 : && get_attribute_namespace (a) == NULL_TREE)
2658 : {
2659 9 : auto_diagnostic_group d;
2660 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2661 : "but its first declaration was not", newdecl);
2662 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2663 9 : }
2664 : }
2665 :
2666 : /* Now that functions must hold information normally held
2667 : by field decls, there is extra work to do so that
2668 : declaration information does not get destroyed during
2669 : definition. */
2670 15881095 : if (DECL_VINDEX (olddecl))
2671 163420 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2672 15881095 : if (DECL_CONTEXT (olddecl))
2673 15881095 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2674 15881095 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2675 15881095 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2676 15881095 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2677 15881095 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2678 15881095 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2679 15881095 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2680 15881095 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2681 15881095 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2682 15881095 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2683 15881095 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2684 15881095 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2685 2811213 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2686 5622426 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2687 15881095 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2688 :
2689 15881095 : if (new_defines_function && mod_redefinable
2690 15881095 : && DECL_INITIAL (olddecl))
2691 : {
2692 4082 : if (GNU_INLINE_P (STRIP_TEMPLATE (olddecl))
2693 2297 : && !GNU_INLINE_P (STRIP_TEMPLATE (newdecl)))
2694 : /* Normal out-of-line redefinition of gnu_inline. */
2695 : mod_redefinable = false;
2696 : else
2697 : new_defines_function = false;
2698 : }
2699 :
2700 15881095 : check_redecl_contract (newdecl, olddecl);
2701 :
2702 : /* Optionally warn about more than one declaration for the same
2703 : name, but don't warn about a function declaration followed by a
2704 : definition. */
2705 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2706 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2707 : /* Don't warn about extern decl followed by definition. */
2708 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2709 : /* Don't warn if at least one is/was hidden. */
2710 27 : && !(hiding || was_hidden)
2711 : /* Don't warn about declaration followed by specialization. */
2712 15881107 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2713 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2714 : {
2715 6 : auto_diagnostic_group d;
2716 6 : if (warning_at (newdecl_loc,
2717 6 : OPT_Wredundant_decls,
2718 : "redundant redeclaration of %qD in same scope",
2719 : newdecl))
2720 6 : inform (olddecl_loc,
2721 : "previous declaration of %qD", olddecl);
2722 6 : }
2723 :
2724 : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2725 : first declaration of the function or, for an explicit specialization
2726 : of a function template, the first declaration of that
2727 : specialization. */
2728 15881095 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2729 667569 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2730 : {
2731 15298852 : if (DECL_DELETED_FN (newdecl)
2732 15298852 : && !(mod_redefinable
2733 22 : && DECL_DELETED_FN (olddecl)))
2734 : {
2735 21 : auto_diagnostic_group d;
2736 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2737 : "is not first declaration", newdecl))
2738 21 : inform (olddecl_loc,
2739 : "previous declaration of %qD", olddecl);
2740 21 : }
2741 15298852 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2742 15298852 : if (DECL_DELETED_FN (olddecl)
2743 28 : && DECL_INITIAL (olddecl)
2744 15298858 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2745 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2746 : }
2747 : }
2748 :
2749 : /* Deal with C++: must preserve virtual function table size. */
2750 20668362 : if (TREE_CODE (olddecl) == TYPE_DECL)
2751 : {
2752 50691 : tree newtype = TREE_TYPE (newdecl);
2753 50691 : tree oldtype = TREE_TYPE (olddecl);
2754 :
2755 50691 : if (newtype != error_mark_node && oldtype != error_mark_node
2756 101382 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2757 139611 : CLASSTYPE_FRIEND_CLASSES (newtype)
2758 46537 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2759 :
2760 50691 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2761 : }
2762 :
2763 : /* Copy all the DECL_... slots specified in the new decl except for
2764 : any that we copy here from the old type. */
2765 20668362 : if (merge_attr)
2766 20086119 : DECL_ATTRIBUTES (newdecl)
2767 40172238 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2768 : else
2769 582243 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2770 :
2771 : /* Transfer purviewness and importingness to the old decl. */
2772 20668362 : transfer_defining_module (olddecl, newdecl);
2773 :
2774 20668362 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2775 : {
2776 4208419 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2777 4208419 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2778 4208419 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2779 :
2780 : /* The new decl should not already have gathered any
2781 : specializations. */
2782 4208419 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2783 :
2784 : /* Make sure the contracts are equivalent. */
2785 4208419 : check_redecl_contract (newdecl, olddecl);
2786 :
2787 4208419 : DECL_ATTRIBUTES (old_result)
2788 4208419 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2789 :
2790 4208419 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2791 : {
2792 4207059 : if (DECL_SOURCE_LOCATION (newdecl)
2793 4207059 : != DECL_SOURCE_LOCATION (olddecl))
2794 : {
2795 : /* Per C++11 8.3.6/4, default arguments cannot be added in
2796 : later declarations of a function template. */
2797 4183636 : if (!mod_redefinable)
2798 4180120 : check_redeclaration_no_default_args (newdecl);
2799 : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2800 : argument expression, that declaration... shall be the only
2801 : declaration of the function or function template in the
2802 : translation unit." */
2803 4183636 : check_no_redeclaration_friend_default_args
2804 4183636 : (old_result, new_result);
2805 :
2806 4183636 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2807 4183636 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2808 4183636 : merge_default_template_args (new_parms, old_parms, olddecl);
2809 : }
2810 4207059 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2811 3084288 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2812 :
2813 4207059 : check_default_args (newdecl);
2814 :
2815 8414106 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2816 4207059 : && DECL_INITIAL (new_result))
2817 : {
2818 24 : mod_redefinable = false;
2819 24 : if (DECL_INITIAL (old_result))
2820 12 : DECL_UNINLINABLE (old_result) = 1;
2821 : else
2822 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2823 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2824 72 : DECL_NOT_REALLY_EXTERN (old_result)
2825 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2826 48 : DECL_INTERFACE_KNOWN (old_result)
2827 24 : = DECL_INTERFACE_KNOWN (new_result);
2828 24 : DECL_DECLARED_INLINE_P (old_result)
2829 24 : = DECL_DECLARED_INLINE_P (new_result);
2830 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2831 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2832 : }
2833 : else
2834 : {
2835 4207035 : DECL_DECLARED_INLINE_P (old_result)
2836 4207035 : |= DECL_DECLARED_INLINE_P (new_result);
2837 4207035 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2838 4207035 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2839 4207035 : check_redeclaration_exception_specification (newdecl, olddecl);
2840 :
2841 4207035 : merge_attribute_bits (new_result, old_result);
2842 : }
2843 : }
2844 :
2845 : /* If the new declaration is a definition, update the file and
2846 : line information on the declaration, and also make
2847 : the old declaration the same definition. */
2848 4208419 : if (DECL_INITIAL (new_result) != NULL_TREE
2849 4208419 : && !(DECL_INITIAL (old_result)
2850 : && mod_redefinable))
2851 : {
2852 5717324 : DECL_SOURCE_LOCATION (olddecl)
2853 5717324 : = DECL_SOURCE_LOCATION (old_result)
2854 2858662 : = DECL_SOURCE_LOCATION (newdecl);
2855 2858662 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2856 2858662 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2857 : {
2858 2858662 : update_contract_arguments (new_result, old_result);
2859 :
2860 2858662 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2861 10300376 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2862 7441714 : DECL_CONTEXT (p) = old_result;
2863 :
2864 5717324 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2865 18 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2866 : }
2867 : }
2868 :
2869 : /* FIXME we can't goto return_old here without removing newdecl from more
2870 : hash tables. */
2871 : return olddecl;
2872 : }
2873 :
2874 16459943 : if (types_match)
2875 : {
2876 16459742 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2877 15880894 : check_redeclaration_exception_specification (newdecl, olddecl);
2878 :
2879 : /* Automatically handles default parameters. */
2880 16459742 : tree oldtype = TREE_TYPE (olddecl);
2881 16459742 : tree newtype;
2882 :
2883 : /* For typedefs use the old type, as the new type's DECL_NAME points
2884 : at newdecl, which will be ggc_freed. */
2885 16459742 : if (TREE_CODE (newdecl) == TYPE_DECL)
2886 : {
2887 : /* But NEWTYPE might have an attribute, honor that. */
2888 50691 : tree tem = TREE_TYPE (newdecl);
2889 50691 : newtype = oldtype;
2890 :
2891 50691 : if (TYPE_USER_ALIGN (tem))
2892 : {
2893 70 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2894 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2895 70 : TYPE_USER_ALIGN (newtype) = true;
2896 : }
2897 :
2898 : /* And remove the new type from the variants list. */
2899 50691 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2900 : {
2901 24 : tree remove = TREE_TYPE (newdecl);
2902 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2903 : {
2904 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2905 : /* If remove is the main variant, no need to remove that
2906 : from the list. One of the DECL_ORIGINAL_TYPE
2907 : variants, e.g. created for aligned attribute, might still
2908 : refer to the newdecl TYPE_DECL though, so remove that one
2909 : in that case. */
2910 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2911 6 : if (orig != remove)
2912 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2913 0 : t = TYPE_MAIN_VARIANT (t))
2914 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2915 : {
2916 12 : TYPE_NEXT_VARIANT (t)
2917 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2918 6 : break;
2919 : }
2920 : }
2921 : else
2922 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2923 0 : t = TYPE_NEXT_VARIANT (t))
2924 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2925 : {
2926 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2927 15 : break;
2928 : }
2929 : }
2930 : }
2931 16409051 : else if (merge_attr)
2932 15826808 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2933 : else
2934 582243 : newtype = TREE_TYPE (newdecl);
2935 :
2936 16459742 : if (VAR_P (newdecl))
2937 : {
2938 528157 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2939 : /* For already initialized vars, TREE_READONLY could have been
2940 : cleared in cp_finish_decl, because the var needs runtime
2941 : initialization or destruction. Make sure not to set
2942 : TREE_READONLY on it again. */
2943 528157 : if (DECL_INITIALIZED_P (olddecl)
2944 1958 : && !DECL_EXTERNAL (olddecl)
2945 528918 : && !TREE_READONLY (olddecl))
2946 167 : TREE_READONLY (newdecl) = 0;
2947 528157 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2948 1056314 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2949 528157 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2950 528157 : if (DECL_DEPENDENT_INIT_P (olddecl))
2951 111554 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2952 1056314 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2953 528157 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2954 528157 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2955 528157 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2956 1056314 : DECL_DECLARED_CONSTINIT_P (newdecl)
2957 528157 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2958 :
2959 : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2960 528157 : if (DECL_LANG_SPECIFIC (olddecl)
2961 1052716 : && CP_DECL_THREADPRIVATE_P (olddecl))
2962 : {
2963 : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2964 41 : retrofit_lang_decl (newdecl);
2965 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2966 : }
2967 : }
2968 :
2969 : /* An explicit specialization of a function template or of a member
2970 : function of a class template can be declared transaction_safe
2971 : independently of whether the corresponding template entity is declared
2972 : transaction_safe. */
2973 1207 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2974 1072 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2975 78 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2976 54 : && tx_safe_fn_type_p (newtype)
2977 16459742 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2978 0 : newtype = tx_unsafe_fn_variant (newtype);
2979 :
2980 16459742 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2981 :
2982 16459742 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2983 15880894 : check_default_args (newdecl);
2984 :
2985 : /* Lay the type out, unless already done. */
2986 16459742 : if (! same_type_p (newtype, oldtype)
2987 370 : && TREE_TYPE (newdecl) != error_mark_node
2988 16460112 : && !(processing_template_decl && uses_template_parms (newdecl)))
2989 287 : layout_type (TREE_TYPE (newdecl));
2990 :
2991 16459742 : if ((VAR_P (newdecl)
2992 15931585 : || TREE_CODE (newdecl) == PARM_DECL
2993 15931585 : || TREE_CODE (newdecl) == RESULT_DECL
2994 15931585 : || TREE_CODE (newdecl) == FIELD_DECL
2995 15931585 : || TREE_CODE (newdecl) == TYPE_DECL)
2996 16510433 : && !(processing_template_decl && uses_template_parms (newdecl)))
2997 406928 : layout_decl (newdecl, 0);
2998 :
2999 : /* Merge deprecatedness. */
3000 16459742 : if (TREE_DEPRECATED (newdecl))
3001 12075 : TREE_DEPRECATED (olddecl) = 1;
3002 :
3003 : /* Merge unavailability. */
3004 16459742 : if (TREE_UNAVAILABLE (newdecl))
3005 3 : TREE_UNAVAILABLE (olddecl) = 1;
3006 :
3007 : /* Preserve function specific target and optimization options */
3008 16459742 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3009 : {
3010 15880894 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
3011 15880894 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
3012 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
3013 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
3014 :
3015 15880894 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
3016 15880894 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
3017 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
3018 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
3019 :
3020 15880894 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
3021 15343632 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
3022 : }
3023 : else
3024 : {
3025 : /* Merge the const type qualifier. */
3026 578848 : if (TREE_READONLY (newdecl))
3027 158142 : TREE_READONLY (olddecl) = 1;
3028 : /* Merge the volatile type qualifier. */
3029 578848 : if (TREE_THIS_VOLATILE (newdecl))
3030 5 : TREE_THIS_VOLATILE (olddecl) = 1;
3031 : }
3032 :
3033 : /* Merge the initialization information. */
3034 16459742 : if (DECL_INITIAL (olddecl) != NULL_TREE
3035 16459742 : && (DECL_INITIAL (newdecl) == NULL_TREE
3036 2325 : || mod_redefinable))
3037 : {
3038 195303 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3039 195303 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3040 195303 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3041 : {
3042 19852 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3043 19852 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
3044 : }
3045 : }
3046 :
3047 16459742 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3048 : {
3049 15880894 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3050 15880894 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3051 15880894 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3052 15880894 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
3053 58924 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
3054 15880894 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
3055 15880894 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
3056 15880894 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
3057 15880894 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
3058 :
3059 15880894 : if (merge_attr)
3060 15298651 : merge_attribute_bits (newdecl, olddecl);
3061 : else
3062 : {
3063 : /* Merge the noreturn bit. */
3064 582243 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3065 582243 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3066 582243 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3067 582243 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
3068 582243 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
3069 : }
3070 : /* Keep the old RTL. */
3071 15880894 : COPY_DECL_RTL (olddecl, newdecl);
3072 : }
3073 578848 : else if (VAR_P (newdecl)
3074 578848 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3075 : {
3076 : /* Keep the old RTL. We cannot keep the old RTL if the old
3077 : declaration was for an incomplete object and the new
3078 : declaration is not since many attributes of the RTL will
3079 : change. */
3080 528083 : COPY_DECL_RTL (olddecl, newdecl);
3081 : }
3082 : }
3083 : /* If cannot merge, then use the new type and qualifiers,
3084 : and don't preserve the old rtl. */
3085 : else
3086 : {
3087 : /* Clean out any memory we had of the old declaration. */
3088 201 : tree oldstatic = value_member (olddecl, static_aggregates);
3089 201 : if (oldstatic)
3090 0 : TREE_VALUE (oldstatic) = error_mark_node;
3091 :
3092 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3093 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3094 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3095 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3096 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3097 : }
3098 :
3099 : /* Merge the storage class information. */
3100 16459943 : merge_weak (newdecl, olddecl);
3101 :
3102 16459943 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3103 16459943 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3104 16459943 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3105 16459943 : if (! DECL_EXTERNAL (olddecl))
3106 56071 : DECL_EXTERNAL (newdecl) = 0;
3107 16459943 : if (! DECL_COMDAT (olddecl))
3108 7082465 : DECL_COMDAT (newdecl) = 0;
3109 :
3110 16459943 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
3111 : {
3112 78 : if (!DECL_LOCAL_DECL_P (olddecl))
3113 : /* This can happen if olddecl was brought in from the
3114 : enclosing namespace via a using-decl. The new decl is
3115 : then not a block-scope extern at all. */
3116 3 : DECL_LOCAL_DECL_P (newdecl) = false;
3117 : else
3118 : {
3119 75 : retrofit_lang_decl (newdecl);
3120 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
3121 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
3122 75 : if (alias != error_mark_node)
3123 : {
3124 72 : DECL_ATTRIBUTES (alias)
3125 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
3126 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3127 42 : merge_attribute_bits (newdecl, alias);
3128 : }
3129 : }
3130 : }
3131 :
3132 16459943 : new_template_info = NULL_TREE;
3133 16459943 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3134 : {
3135 16405663 : bool new_redefines_gnu_inline = false;
3136 :
3137 16405663 : if (new_defines_function
3138 16405663 : && ((DECL_INTERFACE_KNOWN (olddecl)
3139 1242 : && TREE_CODE (olddecl) == FUNCTION_DECL)
3140 9561796 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
3141 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3142 : == FUNCTION_DECL))))
3143 2484 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
3144 :
3145 : if (!new_redefines_gnu_inline)
3146 : {
3147 16405585 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3148 16405585 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3149 16405585 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3150 : }
3151 :
3152 16405663 : if (TREE_CODE (newdecl) != TYPE_DECL)
3153 : {
3154 16405599 : DECL_TEMPLATE_INSTANTIATED (newdecl)
3155 16405599 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3156 16405599 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
3157 :
3158 : /* If the OLDDECL is an instantiation and/or specialization,
3159 : then the NEWDECL must be too. But, it may not yet be marked
3160 : as such if the caller has created NEWDECL, but has not yet
3161 : figured out that it is a redeclaration. */
3162 16405599 : if (!DECL_USE_TEMPLATE (newdecl))
3163 15737413 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
3164 :
3165 16405599 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
3166 15822727 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
3167 31645454 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3168 : }
3169 :
3170 : /* Don't really know how much of the language-specific
3171 : values we should copy from old to new. */
3172 16405663 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3173 :
3174 16405663 : if (LANG_DECL_HAS_MIN (newdecl))
3175 : {
3176 16405663 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3177 16405663 : if (DECL_TEMPLATE_INFO (newdecl))
3178 : {
3179 668219 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
3180 668219 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3181 668219 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3182 : /* Remember the presence of explicit specialization args. */
3183 1164492 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3184 1164492 : = TINFO_USED_TEMPLATE_ID (new_template_info);
3185 : }
3186 :
3187 : /* We don't want to copy template info from a non-templated friend
3188 : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3189 16405663 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3190 : || !non_templated_friend_p (olddecl));
3191 16405663 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3192 : }
3193 :
3194 16405663 : if (DECL_DECLARES_FUNCTION_P (newdecl))
3195 : {
3196 : /* Only functions have these fields. */
3197 15881095 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3198 15881095 : DECL_BEFRIENDING_CLASSES (newdecl)
3199 15881095 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3200 15881095 : DECL_BEFRIENDING_CLASSES (olddecl));
3201 : /* DECL_THUNKS is only valid for virtual functions,
3202 : otherwise it is a DECL_FRIEND_CONTEXT. */
3203 15881095 : if (DECL_VIRTUAL_P (newdecl))
3204 650158 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3205 30461874 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3206 386088 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3207 : }
3208 524568 : else if (VAR_P (newdecl))
3209 : {
3210 : /* Only variables have this field. */
3211 524504 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3212 198 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3213 : }
3214 : }
3215 :
3216 16459943 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3217 : {
3218 15881095 : merge_decl_arguments (newdecl, olddecl, new_defines_function,
3219 : types_match, false);
3220 :
3221 15881095 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3222 15881095 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3223 : {
3224 : /* If newdecl is not a specialization, then it is not a
3225 : template-related function at all. And that means that we
3226 : should have exited above, returning 0. */
3227 582243 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3228 :
3229 582243 : if (DECL_ODR_USED (olddecl))
3230 : /* From [temp.expl.spec]:
3231 :
3232 : If a template, a member template or the member of a class
3233 : template is explicitly specialized then that
3234 : specialization shall be declared before the first use of
3235 : that specialization that would cause an implicit
3236 : instantiation to take place, in every translation unit in
3237 : which such a use occurs. */
3238 0 : error ("explicit specialization of %qD after first use",
3239 : olddecl);
3240 :
3241 582243 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3242 582243 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3243 1164333 : && DECL_NONGNU_INLINE_P (newdecl));
3244 :
3245 : /* Don't propagate visibility from the template to the
3246 : specialization here. We'll do that in determine_visibility if
3247 : appropriate. */
3248 582243 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3249 :
3250 : /* [temp.expl.spec/14] We don't inline explicit specialization
3251 : just because the primary template says so. */
3252 582243 : gcc_assert (!merge_attr);
3253 :
3254 582243 : DECL_DECLARED_INLINE_P (olddecl)
3255 582243 : = DECL_DECLARED_INLINE_P (newdecl);
3256 :
3257 582243 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
3258 582243 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3259 :
3260 582243 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3261 : }
3262 15298852 : else if (new_defines_function && DECL_INITIAL (olddecl))
3263 : {
3264 : /* Never inline re-defined extern inline functions.
3265 : FIXME: this could be better handled by keeping both
3266 : function as separate declarations. */
3267 78 : DECL_UNINLINABLE (newdecl) = 1;
3268 : }
3269 : else
3270 : {
3271 15298774 : if (DECL_PENDING_INLINE_P (olddecl))
3272 : {
3273 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
3274 6 : DECL_PENDING_INLINE_INFO (newdecl)
3275 12 : = DECL_PENDING_INLINE_INFO (olddecl);
3276 : }
3277 15298768 : else if (DECL_PENDING_INLINE_P (newdecl))
3278 : ;
3279 15298768 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3280 15298768 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3281 30597536 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3282 :
3283 15298774 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3284 :
3285 45896322 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3286 30597548 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3287 :
3288 30597548 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3289 15298774 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3290 15298774 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3291 30109036 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3292 : }
3293 :
3294 : /* Preserve abstractness on cloned [cd]tors. */
3295 15881095 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3296 :
3297 : /* Update newdecl's parms to point at olddecl. */
3298 50549131 : for (tree parm = DECL_ARGUMENTS (newdecl); parm;
3299 34668036 : parm = DECL_CHAIN (parm))
3300 34668036 : DECL_CONTEXT (parm) = olddecl;
3301 :
3302 15881095 : if (! types_match)
3303 : {
3304 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3305 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3306 201 : COPY_DECL_RTL (newdecl, olddecl);
3307 : }
3308 15881095 : if (! types_match || new_defines_function)
3309 : {
3310 : /* Update the contracts to reflect the new parameter names. */
3311 9563239 : update_contract_arguments (newdecl, olddecl);
3312 :
3313 : /* Mark the old PARM_DECLs in case std::meta::parameters_of has
3314 : been called on the old declaration and reflections of those
3315 : arguments are held across this point and used later.
3316 : Such PARM_DECLs are no longer present in
3317 : DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain. */
3318 9563239 : for (tree oldarg = DECL_ARGUMENTS (olddecl);
3319 33570343 : oldarg; oldarg = DECL_CHAIN (oldarg))
3320 24007104 : OLD_PARM_DECL_P (oldarg) = 1;
3321 :
3322 : /* These need to be copied so that the names are available.
3323 : Note that if the types do match, we'll preserve inline
3324 : info and other bits, but if not, we won't. */
3325 9563239 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3326 9563239 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3327 : }
3328 : /* If redeclaring a builtin function, it stays built in
3329 : if newdecl is a gnu_inline definition, or if newdecl is just
3330 : a declaration. */
3331 15881095 : if (fndecl_built_in_p (olddecl)
3332 15939928 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3333 : {
3334 4112727 : copy_decl_built_in_function (newdecl, olddecl);
3335 : /* If we're keeping the built-in definition, keep the rtl,
3336 : regardless of declaration matches. */
3337 4112727 : COPY_DECL_RTL (olddecl, newdecl);
3338 4112727 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3339 : {
3340 4112721 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3341 4112721 : if (builtin_decl_explicit_p (fncode))
3342 : {
3343 : /* A compatible prototype of these builtin functions
3344 : is seen, assume the runtime implements it with
3345 : the expected semantics. */
3346 4112721 : switch (fncode)
3347 : {
3348 10184 : case BUILT_IN_STPCPY:
3349 10184 : set_builtin_decl_implicit_p (fncode, true);
3350 10184 : break;
3351 4102537 : default:
3352 4102537 : set_builtin_decl_declared_p (fncode, true);
3353 4102537 : break;
3354 : }
3355 : }
3356 :
3357 4112721 : copy_attributes_to_builtin (newdecl);
3358 : }
3359 : }
3360 15881095 : if (new_defines_function)
3361 : /* If defining a function declared with other language
3362 : linkage, use the previously declared language linkage. */
3363 9563038 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3364 6318057 : else if (types_match)
3365 : {
3366 6317856 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3367 : /* Don't clear out the arguments if we're just redeclaring a
3368 : function. */
3369 6317856 : if (DECL_ARGUMENTS (olddecl))
3370 2165809 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3371 : }
3372 : }
3373 578848 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3374 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3375 :
3376 : /* Now preserve various other info from the definition. */
3377 16459943 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3378 16459943 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3379 16459943 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3380 16459943 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3381 :
3382 : /* Warn about conflicting visibility specifications. */
3383 16459943 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3384 5732986 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3385 16472215 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3386 : {
3387 12 : auto_diagnostic_group d;
3388 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3389 : "%qD: visibility attribute ignored because it "
3390 : "conflicts with previous declaration", newdecl))
3391 12 : inform (olddecl_loc,
3392 : "previous declaration of %qD", olddecl);
3393 12 : }
3394 : /* Choose the declaration which specified visibility. */
3395 16459943 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3396 : {
3397 5732986 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3398 5732986 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3399 : }
3400 : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3401 : so keep this behavior. */
3402 16459943 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3403 : {
3404 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3405 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3406 : }
3407 : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3408 16459943 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3409 : {
3410 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3411 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3412 : }
3413 16459889 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3414 16459889 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3415 3 : DECL_USER_ALIGN (newdecl) = 1;
3416 :
3417 16459943 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3418 32919886 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3419 16459943 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3420 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3421 : DECL_WARN_IF_NOT_ALIGN (olddecl));
3422 16459943 : if (TREE_CODE (newdecl) == FIELD_DECL)
3423 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3424 :
3425 : /* Merge module entity mapping information. */
3426 16459943 : if (DECL_LANG_SPECIFIC (olddecl)
3427 32866055 : && (DECL_MODULE_ENTITY_P (olddecl)
3428 16401533 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3429 : {
3430 4579 : retrofit_lang_decl (newdecl);
3431 4579 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3432 4579 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3433 : }
3434 :
3435 : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3436 : with that from NEWDECL below. */
3437 16459943 : if (DECL_LANG_SPECIFIC (olddecl))
3438 : {
3439 16406112 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3440 : != DECL_LANG_SPECIFIC (newdecl));
3441 16406112 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3442 : }
3443 :
3444 : /* Merge the USED information. */
3445 16459943 : if (TREE_USED (olddecl))
3446 515389 : TREE_USED (newdecl) = 1;
3447 15944554 : else if (TREE_USED (newdecl))
3448 62 : TREE_USED (olddecl) = 1;
3449 :
3450 16459943 : if (VAR_P (newdecl))
3451 : {
3452 528157 : if (DECL_READ_P (olddecl))
3453 45466 : DECL_READ_P (newdecl) = 1;
3454 482691 : else if (DECL_READ_P (newdecl))
3455 3 : DECL_READ_P (olddecl) = 1;
3456 : }
3457 :
3458 16459943 : if (DECL_PRESERVE_P (olddecl))
3459 48 : DECL_PRESERVE_P (newdecl) = 1;
3460 16459895 : else if (DECL_PRESERVE_P (newdecl))
3461 75 : DECL_PRESERVE_P (olddecl) = 1;
3462 :
3463 : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3464 : to olddecl and deleted. */
3465 16459943 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3466 32341038 : && DECL_FUNCTION_VERSIONED (olddecl))
3467 : {
3468 : /* Set the flag for newdecl so that it gets copied to olddecl. */
3469 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3470 : /* newdecl will be purged after copying to olddecl and is no longer
3471 : a version. */
3472 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3473 : }
3474 :
3475 16459943 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3476 : {
3477 15881095 : int function_size;
3478 15881095 : struct symtab_node *snode = symtab_node::get (olddecl);
3479 :
3480 15881095 : function_size = sizeof (struct tree_decl_common);
3481 :
3482 15881095 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3483 : (char *) newdecl + sizeof (struct tree_common),
3484 : function_size - sizeof (struct tree_common));
3485 :
3486 15881095 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3487 : (char *) newdecl + sizeof (struct tree_decl_common),
3488 : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3489 :
3490 : /* Preserve symtab node mapping. */
3491 15881095 : olddecl->decl_with_vis.symtab_node = snode;
3492 :
3493 15881095 : if (new_template_info)
3494 : /* If newdecl is a template instantiation, it is possible that
3495 : the following sequence of events has occurred:
3496 :
3497 : o A friend function was declared in a class template. The
3498 : class template was instantiated.
3499 :
3500 : o The instantiation of the friend declaration was
3501 : recorded on the instantiation list, and is newdecl.
3502 :
3503 : o Later, however, instantiate_class_template called pushdecl
3504 : on the newdecl to perform name injection. But, pushdecl in
3505 : turn called duplicate_decls when it discovered that another
3506 : declaration of a global function with the same name already
3507 : existed.
3508 :
3509 : o Here, in duplicate_decls, we decided to clobber newdecl.
3510 :
3511 : If we're going to do that, we'd better make sure that
3512 : olddecl, and not newdecl, is on the list of
3513 : instantiations so that if we try to do the instantiation
3514 : again we won't get the clobbered declaration. */
3515 667868 : reregister_specialization (newdecl,
3516 : new_template_info,
3517 : olddecl);
3518 : }
3519 : else
3520 : {
3521 578848 : size_t size = tree_code_size (TREE_CODE (newdecl));
3522 :
3523 578848 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3524 : (char *) newdecl + sizeof (struct tree_common),
3525 : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3526 :
3527 578848 : switch (TREE_CODE (newdecl))
3528 : {
3529 578848 : case LABEL_DECL:
3530 578848 : case VAR_DECL:
3531 578848 : case RESULT_DECL:
3532 578848 : case PARM_DECL:
3533 578848 : case FIELD_DECL:
3534 578848 : case TYPE_DECL:
3535 578848 : case CONST_DECL:
3536 578848 : {
3537 578848 : struct symtab_node *snode = NULL;
3538 :
3539 578848 : if (VAR_P (newdecl)
3540 578848 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3541 3 : || DECL_EXTERNAL (olddecl)))
3542 528157 : snode = symtab_node::get (olddecl);
3543 578848 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3544 : (char *) newdecl + sizeof (struct tree_decl_common),
3545 : size - sizeof (struct tree_decl_common)
3546 578848 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3547 578848 : if (VAR_P (newdecl))
3548 528157 : olddecl->decl_with_vis.symtab_node = snode;
3549 : }
3550 : break;
3551 0 : default:
3552 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3553 : (char *) newdecl + sizeof (struct tree_decl_common),
3554 : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3555 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3556 0 : break;
3557 : }
3558 : }
3559 :
3560 16459943 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3561 : {
3562 16409252 : if (DECL_EXTERNAL (olddecl)
3563 529547 : || TREE_PUBLIC (olddecl)
3564 16409322 : || TREE_STATIC (olddecl))
3565 : {
3566 : /* Merge the section attribute.
3567 : We want to issue an error if the sections conflict but that must be
3568 : done later in decl_attributes since we are called before attributes
3569 : are assigned. */
3570 16409252 : if (DECL_SECTION_NAME (newdecl) != NULL)
3571 7 : set_decl_section_name (olddecl, newdecl);
3572 :
3573 16409252 : if (DECL_ONE_ONLY (newdecl))
3574 : {
3575 0 : struct symtab_node *oldsym, *newsym;
3576 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3577 0 : oldsym = cgraph_node::get_create (olddecl);
3578 : else
3579 0 : oldsym = varpool_node::get_create (olddecl);
3580 0 : newsym = symtab_node::get (newdecl);
3581 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3582 : }
3583 : }
3584 :
3585 16409252 : if (VAR_P (newdecl)
3586 16409252 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3587 : {
3588 406 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3589 406 : if (!processing_template_decl)
3590 373 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3591 : }
3592 : }
3593 :
3594 16459943 : DECL_UID (olddecl) = olddecl_uid;
3595 :
3596 : /* NEWDECL contains the merged attribute lists.
3597 : Update OLDDECL to be the same. */
3598 16459943 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3599 :
3600 : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3601 : so that encode_section_info has a chance to look at the new decl
3602 : flags and attributes. */
3603 16459943 : if (DECL_RTL_SET_P (olddecl)
3604 16459958 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3605 15 : || (VAR_P (olddecl)
3606 15 : && TREE_STATIC (olddecl))))
3607 15 : make_decl_rtl (olddecl);
3608 :
3609 : /* The NEWDECL will no longer be needed. Because every out-of-class
3610 : declaration of a member results in a call to duplicate_decls,
3611 : freeing these nodes represents in a significant savings.
3612 :
3613 : Before releasing the node, be sore to remove function from symbol
3614 : table that might have been inserted there to record comdat group.
3615 : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3616 : structure is shared in between newdecl and oldecl. */
3617 16459943 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3618 15881095 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3619 16459943 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3620 : {
3621 16409252 : struct symtab_node *snode = symtab_node::get (newdecl);
3622 16409252 : if (snode)
3623 401 : snode->remove ();
3624 : }
3625 :
3626 16459943 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3627 : {
3628 15881095 : tree clone;
3629 16435738 : FOR_EACH_CLONE (clone, olddecl)
3630 : {
3631 554643 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3632 554643 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3633 : }
3634 : }
3635 :
3636 578848 : return_old:
3637 : /* Remove the associated constraints for newdecl, if any, before
3638 : reclaiming memory. */
3639 16461624 : if (flag_concepts)
3640 16182382 : remove_constraints (newdecl);
3641 :
3642 16461624 : if (flag_contracts)
3643 : /* Remove the specifiers, and then remove the decl from the lookup. */
3644 1557276 : remove_decl_with_fn_contracts_specifiers (newdecl);
3645 :
3646 : /* And similarly for any module tracking data. */
3647 16461624 : if (modules_p ())
3648 53016 : remove_defining_module (newdecl);
3649 :
3650 16461624 : ggc_free (newdecl);
3651 :
3652 16461624 : return olddecl;
3653 : }
3654 :
3655 : /* Return zero if the declaration NEWDECL is valid
3656 : when the declaration OLDDECL (assumed to be for the same name)
3657 : has already been seen.
3658 : Otherwise return an error message format string with a %s
3659 : where the identifier should go. */
3660 :
3661 : static const char *
3662 16675478 : redeclaration_error_message (tree newdecl, tree olddecl)
3663 : {
3664 16675981 : if (redefinable_import_p (olddecl))
3665 : return NULL;
3666 :
3667 16667560 : if (TREE_CODE (newdecl) == TYPE_DECL)
3668 : {
3669 : /* Because C++ can put things into name space for free,
3670 : constructs like "typedef struct foo { ... } foo"
3671 : would look like an erroneous redeclaration. */
3672 85010 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3673 : return NULL;
3674 : else
3675 0 : return G_("redefinition of %q#D");
3676 : }
3677 16582550 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3678 : {
3679 : /* If this is a pure function, its olddecl will actually be
3680 : the original initialization to `0' (which we force to call
3681 : abort()). Don't complain about redefinition in this case. */
3682 23701582 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3683 11850809 : && DECL_INITIAL (olddecl) == NULL_TREE)
3684 : return NULL;
3685 :
3686 : /* If both functions come from different namespaces, this is not
3687 : a redeclaration - this is a conflict with a used function. */
3688 23701552 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3689 2897969 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3690 11850779 : && ! decls_match (olddecl, newdecl))
3691 : return G_("%qD conflicts with used function");
3692 :
3693 : /* We'll complain about linkage mismatches in
3694 : warn_extern_redeclared_static. */
3695 :
3696 : /* Defining the same name twice is no good. */
3697 11850776 : if (decl_defined_p (olddecl)
3698 11850776 : && decl_defined_p (newdecl))
3699 : {
3700 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3701 : return G_("%q#D not declared in class");
3702 441 : else if (!GNU_INLINE_P (olddecl)
3703 387 : || GNU_INLINE_P (newdecl))
3704 : return G_("redefinition of %q#D");
3705 : }
3706 :
3707 13749664 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3708 : {
3709 1846045 : bool olda = GNU_INLINE_P (olddecl);
3710 1846045 : bool newa = GNU_INLINE_P (newdecl);
3711 :
3712 1846045 : if (olda != newa)
3713 : {
3714 15 : if (newa)
3715 : return G_("%q+D redeclared inline with "
3716 : "%<gnu_inline%> attribute");
3717 : else
3718 9 : return G_("%q+D redeclared inline without "
3719 : "%<gnu_inline%> attribute");
3720 : }
3721 : }
3722 :
3723 : /* [class.compare.default]: A definition of a comparison operator as
3724 : defaulted that appears in a class shall be the first declaration of
3725 : that function. */
3726 11850539 : special_function_kind sfk = special_function_p (olddecl);
3727 11850539 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3728 : return G_("comparison operator %q+D defaulted after "
3729 : "its first declaration");
3730 :
3731 11850536 : check_abi_tag_redeclaration
3732 11850536 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3733 11850536 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3734 :
3735 11850536 : return NULL;
3736 : }
3737 4731759 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3738 : {
3739 4204076 : tree nt, ot;
3740 :
3741 4204076 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3742 : return G_("redefinition of %q#D");
3743 :
3744 4204070 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3745 503 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3746 503 : DECL_TEMPLATE_RESULT (olddecl));
3747 :
3748 4203567 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3749 : return NULL;
3750 :
3751 4203555 : nt = DECL_TEMPLATE_RESULT (newdecl);
3752 4203555 : if (DECL_TEMPLATE_INFO (nt))
3753 1004904 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3754 4203555 : ot = DECL_TEMPLATE_RESULT (olddecl);
3755 4203555 : if (DECL_TEMPLATE_INFO (ot))
3756 4203555 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3757 7061949 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3758 4203594 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3759 : return G_("redefinition of %q#D");
3760 :
3761 6822856 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3762 : {
3763 1913206 : bool olda = GNU_INLINE_P (ot);
3764 1913206 : bool newa = GNU_INLINE_P (nt);
3765 :
3766 1913206 : if (olda != newa)
3767 : {
3768 0 : if (newa)
3769 : return G_("%q+D redeclared inline with "
3770 : "%<gnu_inline%> attribute");
3771 : else
3772 0 : return G_("%q+D redeclared inline without "
3773 : "%<gnu_inline%> attribute");
3774 : }
3775 : }
3776 :
3777 : /* Core issue #226 (C++11):
3778 :
3779 : If a friend function template declaration specifies a
3780 : default template-argument, that declaration shall be a
3781 : definition and shall be the only declaration of the
3782 : function template in the translation unit. */
3783 4203540 : if ((cxx_dialect != cxx98)
3784 4184756 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3785 4416472 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3786 : /*is_primary=*/true,
3787 : /*is_partial=*/false,
3788 : /*is_friend_decl=*/2))
3789 3 : return G_("redeclaration of friend %q#D "
3790 : "may not have default template arguments");
3791 :
3792 : return NULL;
3793 : }
3794 527683 : else if (VAR_P (newdecl)
3795 527599 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3796 527599 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3797 527730 : && (! DECL_LANG_SPECIFIC (olddecl)
3798 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3799 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3800 : {
3801 : /* Only variables can be thread-local, and all declarations must
3802 : agree on this property. */
3803 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3804 : return G_("thread-local declaration of %q#D follows "
3805 : "non-thread-local declaration");
3806 : else
3807 3 : return G_("non-thread-local declaration of %q#D follows "
3808 : "thread-local declaration");
3809 : }
3810 527677 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3811 : {
3812 : /* The objects have been declared at namespace scope. If either
3813 : is a member of an anonymous union, then this is an invalid
3814 : redeclaration. For example:
3815 :
3816 : int i;
3817 : union { int i; };
3818 :
3819 : is invalid. */
3820 527443 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3821 1054955 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3822 : return G_("redeclaration of %q#D");
3823 : /* If at least one declaration is a reference, there is no
3824 : conflict. For example:
3825 :
3826 : int i = 3;
3827 : extern int i;
3828 :
3829 : is valid. */
3830 527509 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3831 : return NULL;
3832 :
3833 : /* Static data member declared outside a class definition
3834 : if the variable is defined within the class with constexpr
3835 : specifier is declaration rather than definition (and
3836 : deprecated). */
3837 2827 : if (cxx_dialect >= cxx17
3838 2754 : && VAR_P (olddecl)
3839 2698 : && DECL_CLASS_SCOPE_P (olddecl)
3840 2656 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3841 5471 : && !DECL_INITIAL (newdecl))
3842 : {
3843 2644 : DECL_EXTERNAL (newdecl) = 1;
3844 : /* For now, only warn with explicit -Wdeprecated. */
3845 2644 : if (OPTION_SET_P (warn_deprecated))
3846 : {
3847 40 : auto_diagnostic_group d;
3848 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3849 : "redundant redeclaration of %<constexpr%> "
3850 : "static data member %qD", newdecl))
3851 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3852 : "previous declaration of %qD", olddecl);
3853 40 : }
3854 : return NULL;
3855 : }
3856 :
3857 : /* Reject two definitions. */
3858 : return G_("redefinition of %q#D");
3859 : }
3860 : else
3861 : {
3862 : /* Objects declared with block scope: */
3863 : /* Reject two definitions, and reject a definition
3864 : together with an external reference. */
3865 153 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3866 153 : return G_("redeclaration of %q#D");
3867 : return NULL;
3868 : }
3869 : }
3870 :
3871 :
3872 : /* Hash and equality functions for the named_label table. */
3873 :
3874 : hashval_t
3875 80141 : named_label_hash::hash (const value_type entry)
3876 : {
3877 80141 : return IDENTIFIER_HASH_VALUE (entry->name);
3878 : }
3879 :
3880 : bool
3881 77051 : named_label_hash::equal (const value_type entry, compare_type name)
3882 : {
3883 77051 : return name == entry->name;
3884 : }
3885 :
3886 : /* Look for a label named ID in the current function. If one cannot
3887 : be found, create one. Return the named_label_entry, or NULL on
3888 : failure. */
3889 :
3890 : static named_label_entry *
3891 24665 : lookup_label_1 (tree id, bool making_local_p)
3892 : {
3893 24665 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3894 :
3895 : /* You can't use labels at global scope. */
3896 24665 : if (current_function_decl == NULL_TREE)
3897 : {
3898 3 : error ("label %qE referenced outside of any function", id);
3899 3 : return NULL;
3900 : }
3901 :
3902 24662 : if (!named_labels)
3903 4762 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3904 :
3905 24662 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3906 24662 : named_label_entry **slot
3907 24662 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3908 24662 : named_label_entry *old = *slot;
3909 :
3910 24662 : if (old && old->label_decl)
3911 : {
3912 3521 : if (!making_local_p)
3913 : return old;
3914 :
3915 21 : if (old->binding_level == current_binding_level)
3916 : {
3917 0 : auto_diagnostic_group d;
3918 0 : error ("local label %qE conflicts with existing label", id);
3919 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3920 0 : return NULL;
3921 0 : }
3922 : }
3923 :
3924 : /* We are making a new decl, create or reuse the named_label_entry */
3925 21183 : named_label_entry *ent = NULL;
3926 33 : if (old && !old->label_decl)
3927 : ent = old;
3928 : else
3929 : {
3930 21150 : ent = ggc_cleared_alloc<named_label_entry> ();
3931 21150 : ent->name = id;
3932 21150 : ent->outer = old;
3933 21150 : *slot = ent;
3934 : }
3935 :
3936 : /* Now create the LABEL_DECL. */
3937 21162 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3938 :
3939 21162 : DECL_CONTEXT (decl) = current_function_decl;
3940 21162 : SET_DECL_MODE (decl, VOIDmode);
3941 21162 : if (making_local_p)
3942 : {
3943 219 : C_DECLARED_LABEL_FLAG (decl) = true;
3944 219 : DECL_CHAIN (decl) = current_binding_level->names;
3945 219 : current_binding_level->names = decl;
3946 : }
3947 :
3948 21162 : ent->label_decl = decl;
3949 :
3950 21162 : return ent;
3951 24665 : }
3952 :
3953 : /* Wrapper for lookup_label_1. */
3954 :
3955 : tree
3956 3006 : lookup_label (tree id)
3957 : {
3958 3006 : named_label_entry *ent = lookup_label_1 (id, false);
3959 3006 : return ent ? ent->label_decl : NULL_TREE;
3960 : }
3961 :
3962 : /* Remember that we've seen &&ID. */
3963 :
3964 : void
3965 262 : mark_label_addressed (tree id)
3966 : {
3967 262 : named_label_entry *ent = lookup_label_1 (id, false);
3968 262 : ent->addressed = true;
3969 262 : }
3970 :
3971 : tree
3972 219 : declare_local_label (tree id)
3973 : {
3974 219 : named_label_entry *ent = lookup_label_1 (id, true);
3975 219 : return ent ? ent->label_decl : NULL_TREE;
3976 : }
3977 :
3978 : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3979 :
3980 : static bool
3981 2982632 : decl_jump_unsafe (tree decl)
3982 : {
3983 : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3984 : with automatic storage duration is not in scope to a point where it is
3985 : in scope is ill-formed unless the variable has scalar type, class type
3986 : with a trivial default constructor and a trivial destructor, a
3987 : cv-qualified version of one of these types, or an array of one of the
3988 : preceding types and is declared without an initializer (8.5). */
3989 2982632 : tree type = TREE_TYPE (decl);
3990 :
3991 2982632 : return (type != error_mark_node
3992 2982614 : && VAR_P (decl)
3993 15082 : && !TREE_STATIC (decl)
3994 2997578 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3995 12998 : || variably_modified_type_p (type, NULL_TREE)));
3996 : }
3997 :
3998 : /* Returns true if decl is an automatic variable with vacuous initialization
3999 : except when it is [[indeterminate]] or [[gnu::uninitialized]].
4000 : Jumps across such initialization need to be instrumented for
4001 : !!flag_auto_var_init. */
4002 :
4003 : static bool
4004 2980675 : decl_instrument_init_bypass_p (tree decl)
4005 : {
4006 2980675 : tree type = TREE_TYPE (decl);
4007 :
4008 2980675 : return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4009 2220875 : && !processing_template_decl
4010 2220629 : && type != error_mark_node
4011 2220623 : && VAR_P (decl)
4012 1072 : && !TREE_STATIC (decl)
4013 1051 : && !DECL_EXTERNAL (decl)
4014 2074 : && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
4015 1037 : || variably_modified_type_p (type, NULL_TREE))
4016 1037 : && !lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (decl))
4017 1037 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
4018 2981712 : && !DECL_HAS_VALUE_EXPR_P (decl));
4019 : }
4020 :
4021 : /* Build .DEFERRED_INIT call for DECL. */
4022 :
4023 : static tree
4024 140 : build_deferred_init_call (tree decl)
4025 : {
4026 140 : tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
4027 280 : tree init_type_arg = build_int_cst (integer_type_node,
4028 140 : (int) flag_auto_var_init);
4029 140 : location_t loc = DECL_SOURCE_LOCATION (decl);
4030 140 : tree decl_name;
4031 :
4032 140 : if (DECL_NAME (decl))
4033 140 : decl_name = build_string_literal (DECL_NAME (decl));
4034 : else
4035 : {
4036 0 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
4037 0 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
4038 0 : decl_name = build_string_literal (decl_name_anonymous);
4039 : }
4040 :
4041 140 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
4042 140 : TREE_TYPE (decl), 3,
4043 : decl_size_arg, init_type_arg,
4044 : decl_name);
4045 140 : tree ret = build2_loc (loc, MODIFY_EXPR, void_type_node, decl, call);
4046 140 : return build_stmt (loc, EXPR_STMT, ret);
4047 : }
4048 :
4049 : /* Emit before ITER (and any labels/case labels before it) code like
4050 : if (0)
4051 : {
4052 : l1:
4053 : v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
4054 : v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
4055 : v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
4056 : v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
4057 : }
4058 : and return l1 label, or if it already exists, assert it has the
4059 : .DEFERRED_INIT calls for the right decls in the right order and
4060 : amend it, either by adding extra labels in between or further
4061 : ,DEFERRED_INIT calls before the first label and extra label before
4062 : that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
4063 : of adding a label. DECLS points to an array of NDECLS VAR_DECLs
4064 : which should be initialized. */
4065 :
4066 : static tree
4067 127 : maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
4068 : tree *decls, unsigned ndecls)
4069 : {
4070 127 : tree lab = NULL_TREE;
4071 191 : for (; !tsi_end_p (iter); tsi_prev (&iter))
4072 : {
4073 185 : switch (TREE_CODE (tsi_stmt (iter)))
4074 : {
4075 64 : case LABEL_EXPR:
4076 64 : case CASE_LABEL_EXPR:
4077 64 : case DEBUG_BEGIN_STMT:
4078 64 : continue;
4079 : default:
4080 : break;
4081 : }
4082 : break;
4083 : }
4084 127 : if (!tsi_end_p (iter)
4085 121 : && TREE_CODE (tsi_stmt (iter)) == IF_STMT
4086 204 : && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
4087 : {
4088 : /* Found IF_STMT added for this or some adjacent
4089 : LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
4090 : The decls are ordered so that we can always reuse it. Sometimes
4091 : by no modifications at all and just returning the right label
4092 : which was added already before, sometimes by adding a label in
4093 : between two previously added .DEFERRED_INIT calls and sometimes
4094 : by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
4095 : before that) before the statements in IF_STMT body. */
4096 71 : tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
4097 71 : iter = tsi_last (then_clause);
4098 71 : bool add = false;
4099 452 : for (unsigned int i = 0; i < ndecls; ++i)
4100 : {
4101 310 : tree decl = decls[i];
4102 310 : if (!add)
4103 : {
4104 : /* Skip over labels/case labels after .DEFERRED_INIT for the
4105 : DECL we are looking for. */
4106 399 : while (!tsi_end_p (iter)
4107 399 : && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
4108 286 : || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
4109 26 : && !case_label)))
4110 119 : tsi_prev (&iter);
4111 280 : if (tsi_end_p (iter))
4112 : {
4113 : /* Reached the start, we'll need to prepend further
4114 : statements. */
4115 20 : add = true;
4116 20 : iter = tsi_start (then_clause);
4117 : }
4118 : else
4119 : {
4120 : /* Found something, assert it is .DEFERRED_INIT for
4121 : DECL. */
4122 260 : tree t = tsi_stmt (iter);
4123 260 : gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
4124 260 : t = EXPR_STMT_EXPR (t);
4125 260 : gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
4126 : && TREE_OPERAND (t, 0) == decl
4127 : && (TREE_CODE (TREE_OPERAND (t, 1))
4128 : == CALL_EXPR));
4129 260 : t = TREE_OPERAND (t, 1);
4130 260 : gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
4131 : && (CALL_EXPR_IFN (t)
4132 : == IFN_DEFERRED_INIT));
4133 260 : tsi_prev (&iter);
4134 : }
4135 : }
4136 280 : if (add)
4137 : {
4138 : /* If reached the start in this or some earlier iteration,
4139 : prepend .DEFERRED_INIT call for DECL. */
4140 50 : tree t = build_deferred_init_call (decl);
4141 50 : STMT_IS_FULL_EXPR_P (t) = 1;
4142 50 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4143 : }
4144 : }
4145 71 : if (!add)
4146 : {
4147 : /* If .DEFERRED_INIT calls for all the decls were already there,
4148 : skip over case labels and if we find a LABEL_EXPR, return
4149 : its label. */
4150 63 : while (!tsi_end_p (iter)
4151 63 : && !case_label
4152 117 : && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4153 12 : tsi_prev (&iter);
4154 51 : if (tsi_end_p (iter))
4155 : {
4156 : /* Only case labels were found and we are looking for normal
4157 : label, we'll need to add it. */
4158 0 : add = true;
4159 0 : iter = tsi_start (then_clause);
4160 : }
4161 51 : else if (!case_label
4162 51 : && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4163 : /* Return existing label. */
4164 21 : lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4165 : else
4166 : {
4167 : /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4168 30 : gcc_checking_assert (case_label
4169 : || (TREE_CODE (tsi_stmt (iter))
4170 : == EXPR_STMT));
4171 30 : add = true;
4172 30 : tsi_next (&iter);
4173 30 : gcc_checking_assert (!tsi_end_p (iter));
4174 : }
4175 : }
4176 21 : if (add)
4177 : {
4178 50 : tree t;
4179 50 : if (case_label)
4180 : t = case_label;
4181 : else
4182 : {
4183 41 : lab = create_artificial_label (UNKNOWN_LOCATION);
4184 41 : t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4185 : }
4186 50 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4187 : }
4188 : }
4189 : else
4190 : {
4191 : /* No IF_STMT created by this function found. Create it all
4192 : from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4193 : followed by .DEFERRED_INIT calls inside of a new if (0). */
4194 56 : tree new_then = push_stmt_list ();
4195 56 : if (!case_label)
4196 : {
4197 23 : lab = create_artificial_label (UNKNOWN_LOCATION);
4198 23 : add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4199 : }
4200 : else
4201 33 : add_stmt (case_label);
4202 146 : for (unsigned int i = ndecls; i; --i)
4203 90 : add_stmt (build_deferred_init_call (decls[i - 1]));
4204 56 : new_then = pop_stmt_list (new_then);
4205 56 : tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4206 : new_then, void_node, NULL_TREE);
4207 56 : IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4208 56 : if (tsi_end_p (iter))
4209 : {
4210 6 : iter = tsi_start (iter.container);
4211 6 : tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4212 : }
4213 : else
4214 50 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4215 : }
4216 127 : return lab;
4217 : }
4218 :
4219 : /* Adjust backward gotos to named label ENT if they jump over vacuous
4220 : initializers if !!flag_auto_var_init. ITER is the location of
4221 : LABEL_EXPR for that named label. */
4222 :
4223 : static void
4224 19 : adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4225 : {
4226 19 : auto_vec<tree, 4> decls;
4227 19 : unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4228 19 : tree decl;
4229 107 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4230 88 : if (!decl_jump_unsafe (decl))
4231 : {
4232 88 : gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4233 88 : decls.safe_push (decl);
4234 176 : if (decls.length () == max_cnt)
4235 : break;
4236 : }
4237 19 : named_label_bck_direct_goto *dgoto;
4238 19 : unsigned last = 0;
4239 19 : tree lab = NULL_TREE;
4240 103 : FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4241 : {
4242 46 : if (dgoto->n_bad_decls != last)
4243 : {
4244 40 : last = dgoto->n_bad_decls;
4245 80 : lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4246 : decls.address (), last);
4247 : }
4248 46 : *dgoto->direct_goto = lab;
4249 : }
4250 19 : }
4251 :
4252 : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4253 : to the user. */
4254 :
4255 : static bool
4256 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
4257 : enum diagnostics::kind diag_kind, bool computed)
4258 : {
4259 286 : if (computed)
4260 43 : diag_kind = diagnostics::kind::warning;
4261 286 : auto_diagnostic_group d;
4262 286 : bool complained
4263 373 : = emit_diagnostic (diag_kind, loc, 0,
4264 : decl ? G_("jump to label %qD")
4265 : : G_("jump to case label"), decl);
4266 286 : if (complained && locus)
4267 : {
4268 199 : if (computed)
4269 43 : inform (*locus, " as a possible target of computed goto");
4270 : else
4271 156 : inform (*locus, " from here");
4272 : }
4273 572 : return complained;
4274 286 : }
4275 :
4276 : /* Check that a single previously seen jump to a newly defined label
4277 : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4278 : the jump context; NAMES are the names in scope in LEVEL at the jump
4279 : context; LOCUS is the source position of the jump or 0. COMPUTED
4280 : is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4281 : vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4282 : initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4283 : CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4284 : Returns non-zero if all is well, 2 if any vacuous initializers were
4285 : crossed. */
4286 :
4287 : static int
4288 1836225 : check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4289 : bool exited_omp, const location_t *locus,
4290 : vec<tree, va_gc> *computed,
4291 : vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4292 : tree case_label)
4293 : {
4294 1836225 : auto_diagnostic_group d;
4295 1836225 : cp_binding_level *b;
4296 1836225 : bool complained = false;
4297 1836225 : int identified = 0;
4298 1836225 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4299 1836225 : bool saw_ceif = false, saw_se = false;
4300 1836225 : auto_vec<tree> vacuous_decls;
4301 1836225 : bool vacuous_inits = false;
4302 :
4303 1836225 : if (exited_omp)
4304 : {
4305 12 : complained = identify_goto (decl, input_location, locus,
4306 : diagnostics::kind::error,
4307 : computed);
4308 12 : if (complained)
4309 12 : inform (input_location, " exits OpenMP structured block");
4310 : saw_omp = true;
4311 : identified = 2;
4312 : }
4313 :
4314 3642749 : for (b = current_binding_level; b ; b = b->level_chain)
4315 : {
4316 3642749 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4317 :
4318 6620146 : for (new_decls = b->names; new_decls != old_decls;
4319 5944106 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4320 2966709 : : TREE_CHAIN (new_decls)))
4321 : {
4322 2977397 : bool problem = decl_jump_unsafe (new_decls);
4323 2977397 : if (! problem)
4324 : {
4325 2977352 : if (decl_instrument_init_bypass_p (new_decls))
4326 : {
4327 323 : if (direct_goto || case_label)
4328 254 : vacuous_decls.safe_push (new_decls);
4329 : vacuous_inits = true;
4330 : }
4331 2977352 : continue;
4332 : }
4333 :
4334 45 : if (!identified)
4335 : {
4336 45 : complained = identify_goto (decl, input_location, locus,
4337 : diagnostics::kind::error,
4338 : computed);
4339 45 : identified = 2;
4340 : }
4341 45 : if (complained)
4342 45 : inform (DECL_SOURCE_LOCATION (new_decls),
4343 : " crosses initialization of %q#D", new_decls);
4344 : }
4345 :
4346 3642749 : if (b == level)
4347 : break;
4348 :
4349 1806524 : const char *inf = NULL;
4350 1806524 : location_t loc = input_location;
4351 1806524 : switch (b->kind)
4352 : {
4353 3 : case sk_try:
4354 3 : if (!saw_eh)
4355 : inf = G_(" enters %<try%> block");
4356 : saw_eh = true;
4357 : break;
4358 :
4359 9 : case sk_catch:
4360 9 : if (!saw_eh)
4361 : inf = G_(" enters %<catch%> block");
4362 : saw_eh = true;
4363 : break;
4364 :
4365 102 : case sk_omp:
4366 102 : if (!saw_omp)
4367 : inf = G_(" enters OpenMP structured block");
4368 : saw_omp = true;
4369 : break;
4370 :
4371 3 : case sk_transaction:
4372 3 : if (!saw_tm)
4373 : inf = G_(" enters synchronized or atomic statement");
4374 : saw_tm = true;
4375 : break;
4376 :
4377 18 : case sk_stmt_expr:
4378 18 : if (!saw_se)
4379 : inf = G_(" enters statement expression");
4380 : saw_se = true;
4381 : break;
4382 :
4383 1806249 : case sk_block:
4384 1806249 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
4385 : {
4386 9 : inf = G_(" enters %<constexpr if%> statement");
4387 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4388 : saw_cxif = true;
4389 : }
4390 1806240 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
4391 : {
4392 18 : inf = G_(" enters %<consteval if%> statement");
4393 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4394 : saw_ceif = true;
4395 : }
4396 : break;
4397 :
4398 : default:
4399 : break;
4400 : }
4401 :
4402 27 : if (inf)
4403 : {
4404 141 : if (identified < 2)
4405 141 : complained = identify_goto (decl, input_location, locus,
4406 : diagnostics::kind::error,
4407 : computed);
4408 141 : identified = 2;
4409 141 : if (complained)
4410 141 : inform (loc, inf);
4411 : }
4412 : }
4413 :
4414 1836225 : if (!vec_safe_is_empty (computed))
4415 : {
4416 9 : if (!identified)
4417 6 : complained = identify_goto (decl, input_location, locus,
4418 : diagnostics::kind::error,
4419 : computed);
4420 9 : identified = 2;
4421 9 : if (complained)
4422 18 : for (tree d : computed)
4423 : {
4424 9 : if (DECL_P (d))
4425 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4426 3 : else if (d == get_identifier ("catch"))
4427 3 : inform (*locus, " does not clean up handled exception");
4428 : }
4429 : }
4430 :
4431 1836314 : if (!vacuous_decls.is_empty () && !seen_error ())
4432 : {
4433 87 : tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4434 87 : if (case_label)
4435 : {
4436 42 : gcc_checking_assert (tsi_stmt (iter) == case_label);
4437 42 : tsi_delink (&iter);
4438 42 : iter = tsi_last (cur_stmt_list);
4439 : }
4440 87 : tree lab = maybe_add_deferred_init_calls (iter, case_label,
4441 : vacuous_decls.address (),
4442 : vacuous_decls.length ());
4443 87 : if (lab)
4444 : {
4445 : unsigned int i;
4446 : named_label_fwd_direct_goto *dgoto;
4447 177 : FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4448 45 : *dgoto->direct_goto = lab;
4449 : }
4450 : }
4451 :
4452 1836225 : if (identified)
4453 : return 0;
4454 1836021 : return vacuous_inits ? 2 : 1;
4455 1836225 : }
4456 :
4457 : static void
4458 2142 : check_previous_goto (tree decl, struct named_label_use_entry *use)
4459 : {
4460 2142 : check_previous_goto_1 (decl, use->binding_level,
4461 : use->names_in_scope, use->in_omp_scope,
4462 2142 : &use->o_goto_locus, use->computed_goto,
4463 : use->direct_goto, NULL_TREE);
4464 2142 : vec_free (use->direct_goto);
4465 2142 : }
4466 :
4467 : static int
4468 1834083 : check_switch_goto (cp_binding_level *level, tree case_label)
4469 : {
4470 1834083 : return check_previous_goto_1 (NULL_TREE, level, level->names,
4471 1834083 : false, NULL, nullptr, nullptr, case_label);
4472 : }
4473 :
4474 : /* Check that a new jump to a label ENT is OK. DECLP is a pointer
4475 : to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4476 :
4477 : void
4478 2884 : check_goto_1 (named_label_entry *ent, tree *declp)
4479 : {
4480 2884 : auto_diagnostic_group d;
4481 2884 : tree decl = ent->label_decl;
4482 2884 : bool computed = declp == NULL;
4483 :
4484 : /* If the label hasn't been defined yet, defer checking. */
4485 2884 : if (! DECL_INITIAL (decl))
4486 : {
4487 : /* Don't bother creating another use if the last goto had the
4488 : same data, and will therefore create the same set of errors. */
4489 2214 : if (ent->uses
4490 661 : && ent->uses->binding_level == current_binding_level
4491 63 : && ent->uses->names_in_scope == current_binding_level->names)
4492 : {
4493 63 : if (declp
4494 57 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4495 12 : && !processing_template_decl)
4496 10 : vec_safe_push (ent->uses->direct_goto,
4497 10 : named_label_fwd_direct_goto { declp });
4498 : return;
4499 : }
4500 :
4501 2151 : named_label_use_entry *new_use
4502 2151 : = ggc_alloc<named_label_use_entry> ();
4503 2151 : new_use->binding_level = current_binding_level;
4504 2151 : new_use->names_in_scope = current_binding_level->names;
4505 2151 : new_use->o_goto_locus = input_location;
4506 2151 : new_use->in_omp_scope = false;
4507 2151 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4508 2151 : new_use->direct_goto = nullptr;
4509 2151 : if (declp
4510 2060 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4511 368 : && !processing_template_decl)
4512 311 : vec_safe_push (new_use->direct_goto,
4513 311 : named_label_fwd_direct_goto { declp });
4514 :
4515 2151 : new_use->next = ent->uses;
4516 2151 : ent->uses = new_use;
4517 2151 : return;
4518 : }
4519 :
4520 670 : bool saw_catch = false, complained = false;
4521 670 : int identified = 0;
4522 670 : tree bad;
4523 670 : unsigned ix;
4524 670 : unsigned n_bad_decls = 0;
4525 :
4526 670 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4527 655 : || ent->in_constexpr_if || ent->in_consteval_if
4528 649 : || ent->in_omp_scope || ent->in_stmt_expr
4529 631 : || ent->has_bad_decls)
4530 : {
4531 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4532 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4533 30 : || ent->in_consteval_if || ent->in_transaction_scope
4534 24 : || ent->in_omp_scope || ent->in_stmt_expr)
4535 39 : diag_kind = diagnostics::kind::error;
4536 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4537 : &input_location, diag_kind, computed);
4538 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
4539 : }
4540 :
4541 860 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4542 : {
4543 190 : bool problem = decl_jump_unsafe (bad);
4544 190 : if (!problem)
4545 : {
4546 184 : gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4547 184 : n_bad_decls++;
4548 184 : continue;
4549 : }
4550 :
4551 6 : if (DECL_ARTIFICIAL (bad))
4552 : {
4553 : /* Can't skip init of __exception_info. */
4554 0 : if (identified == 1)
4555 : {
4556 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4557 : &input_location,
4558 : diagnostics::kind::error,
4559 : computed);
4560 0 : identified = 2;
4561 : }
4562 0 : if (complained)
4563 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4564 : saw_catch = true;
4565 : }
4566 6 : else if (complained)
4567 6 : inform (DECL_SOURCE_LOCATION (bad),
4568 : " skips initialization of %q#D", bad);
4569 : }
4570 :
4571 670 : if (complained)
4572 : {
4573 45 : if (ent->in_try_scope)
4574 9 : inform (input_location, " enters %<try%> block");
4575 36 : else if (ent->in_catch_scope && !saw_catch)
4576 3 : inform (input_location, " enters %<catch%> block");
4577 33 : else if (ent->in_transaction_scope)
4578 3 : inform (input_location, " enters synchronized or atomic statement");
4579 30 : else if (ent->in_constexpr_if)
4580 3 : inform (input_location, " enters %<constexpr if%> statement");
4581 27 : else if (ent->in_consteval_if)
4582 3 : inform (input_location, " enters %<consteval if%> statement");
4583 24 : else if (ent->in_stmt_expr)
4584 15 : inform (input_location, " enters statement expression");
4585 : }
4586 :
4587 670 : if (ent->in_omp_scope)
4588 : {
4589 3 : if (complained)
4590 3 : inform (input_location, " enters OpenMP structured block");
4591 : }
4592 667 : else if (flag_openmp)
4593 42 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4594 : {
4595 42 : if (b == ent->binding_level)
4596 : break;
4597 36 : if (b->kind == sk_omp)
4598 : {
4599 21 : if (identified < 2)
4600 : {
4601 42 : complained = identify_goto (decl,
4602 21 : DECL_SOURCE_LOCATION (decl),
4603 : &input_location,
4604 : diagnostics::kind::error,
4605 : computed);
4606 21 : identified = 2;
4607 : }
4608 21 : if (complained)
4609 21 : inform (input_location, " exits OpenMP structured block");
4610 : break;
4611 : }
4612 : }
4613 :
4614 : /* Warn if a computed goto might involve a local variable going out of scope
4615 : without being cleaned up. */
4616 670 : if (computed)
4617 : {
4618 80 : auto level = ent->binding_level;
4619 80 : auto names = ent->names_in_scope;
4620 80 : for (auto b = current_binding_level; ; b = b->level_chain)
4621 : {
4622 138 : if (b->kind == sk_catch)
4623 : {
4624 6 : if (!identified)
4625 : {
4626 6 : complained
4627 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4628 : &input_location, diagnostics::kind::error,
4629 : computed);
4630 6 : identified = 2;
4631 : }
4632 6 : if (complained)
4633 6 : inform (input_location,
4634 : " does not clean up handled exception");
4635 : }
4636 138 : tree end = b == level ? names : NULL_TREE;
4637 154 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4638 : {
4639 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4640 : {
4641 10 : if (!identified)
4642 : {
4643 10 : complained
4644 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4645 : &input_location, diagnostics::kind::error,
4646 : computed);
4647 10 : identified = 2;
4648 : }
4649 10 : if (complained)
4650 10 : inform (DECL_SOURCE_LOCATION (d),
4651 : " does not destroy %qD", d);
4652 : }
4653 : }
4654 138 : if (b == level)
4655 : break;
4656 58 : }
4657 : }
4658 :
4659 670 : if (n_bad_decls && declp)
4660 46 : vec_safe_push (ent->direct_goto,
4661 46 : named_label_bck_direct_goto { declp, n_bad_decls });
4662 2884 : }
4663 :
4664 : /* Check that a new jump to a label *DECLP is OK. Called by
4665 : finish_goto_stmt. */
4666 :
4667 : void
4668 2844 : check_goto (tree *declp)
4669 : {
4670 2844 : if (!named_labels)
4671 : return;
4672 2829 : tree decl = *declp;
4673 2829 : if (TREE_CODE (decl) != LABEL_DECL)
4674 : {
4675 : /* We don't know where a computed goto is jumping,
4676 : so check all addressable labels. */
4677 330 : for (auto iter = named_labels->begin ();
4678 452 : iter != named_labels->end ();
4679 208 : ++iter)
4680 : {
4681 208 : auto ent = *iter;
4682 208 : if (ent->addressed)
4683 177 : check_goto_1 (ent, NULL);
4684 : }
4685 : }
4686 : else
4687 : {
4688 2707 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4689 2707 : named_label_entry **slot
4690 2707 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4691 2707 : named_label_entry *ent = *slot;
4692 2707 : check_goto_1 (ent, declp);
4693 : }
4694 : }
4695 :
4696 : /* Check that a return is ok wrt OpenMP structured blocks.
4697 : Called by finish_return_stmt. Returns true if all is well. */
4698 :
4699 : bool
4700 281836 : check_omp_return (void)
4701 : {
4702 837411 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4703 837411 : if (b->kind == sk_omp)
4704 : {
4705 15 : error ("invalid exit from OpenMP structured block");
4706 15 : return false;
4707 : }
4708 837396 : else if (b->kind == sk_function_parms)
4709 : break;
4710 : return true;
4711 : }
4712 :
4713 : /* Define a label, specifying the location in the source file.
4714 : Return the LABEL_DECL node for the label. */
4715 :
4716 : tree
4717 21159 : define_label (location_t location, tree name)
4718 : {
4719 21159 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4720 :
4721 : /* After labels, make any new cleanups in the function go into their
4722 : own new (temporary) binding contour. */
4723 21159 : for (cp_binding_level *p = current_binding_level;
4724 32012 : p->kind != sk_function_parms;
4725 10853 : p = p->level_chain)
4726 10853 : p->more_cleanups_ok = 0;
4727 :
4728 21159 : named_label_entry *ent = lookup_label_1 (name, false);
4729 21159 : tree decl = ent->label_decl;
4730 :
4731 21159 : if (DECL_INITIAL (decl) != NULL_TREE)
4732 : {
4733 6 : error ("duplicate label %qD", decl);
4734 6 : return error_mark_node;
4735 : }
4736 : else
4737 : {
4738 : /* Mark label as having been defined. */
4739 21153 : DECL_INITIAL (decl) = error_mark_node;
4740 : /* Say where in the source. */
4741 21153 : DECL_SOURCE_LOCATION (decl) = location;
4742 :
4743 21153 : ent->binding_level = current_binding_level;
4744 21153 : ent->names_in_scope = current_binding_level->names;
4745 :
4746 23295 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4747 2142 : check_previous_goto (decl, use);
4748 21153 : ent->uses = NULL;
4749 : }
4750 :
4751 21153 : return decl;
4752 21159 : }
4753 :
4754 : struct cp_switch
4755 : {
4756 : cp_binding_level *level;
4757 : struct cp_switch *next;
4758 : /* The SWITCH_STMT being built. */
4759 : tree switch_stmt;
4760 : /* A splay-tree mapping the low element of a case range to the high
4761 : element, or NULL_TREE if there is no high element. Used to
4762 : determine whether or not a new case label duplicates an old case
4763 : label. We need a tree, rather than simply a hash table, because
4764 : of the GNU case range extension. */
4765 : splay_tree cases;
4766 : /* Remember whether a default: case label has been seen. */
4767 : bool has_default_p;
4768 : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4769 : bool break_stmt_seen_p;
4770 : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4771 : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4772 : bool in_loop_body_p;
4773 : };
4774 :
4775 : /* A stack of the currently active switch statements. The innermost
4776 : switch statement is on the top of the stack. There is no need to
4777 : mark the stack for garbage collection because it is only active
4778 : during the processing of the body of a function, and we never
4779 : collect at that point. */
4780 :
4781 : static struct cp_switch *switch_stack;
4782 :
4783 : /* Called right after a switch-statement condition is parsed.
4784 : SWITCH_STMT is the switch statement being parsed. */
4785 :
4786 : void
4787 742547 : push_switch (tree switch_stmt)
4788 : {
4789 742547 : struct cp_switch *p = XNEW (struct cp_switch);
4790 742547 : p->level = current_binding_level;
4791 742547 : p->next = switch_stack;
4792 742547 : p->switch_stmt = switch_stmt;
4793 742547 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4794 742547 : p->has_default_p = false;
4795 742547 : p->break_stmt_seen_p = false;
4796 742547 : p->in_loop_body_p = false;
4797 742547 : switch_stack = p;
4798 742547 : }
4799 :
4800 : void
4801 742547 : pop_switch (void)
4802 : {
4803 742547 : struct cp_switch *cs = switch_stack;
4804 :
4805 : /* Emit warnings as needed. */
4806 742547 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4807 742547 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4808 742547 : const bool bool_cond_p
4809 742547 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4810 742547 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4811 742547 : if (!processing_template_decl)
4812 303480 : c_do_switch_warnings (cs->cases, switch_location,
4813 303480 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4814 : bool_cond_p);
4815 :
4816 : /* For the benefit of block_may_fallthru remember if the switch body
4817 : case labels cover all possible values and if there are break; stmts. */
4818 742547 : if (cs->has_default_p
4819 742547 : || (!processing_template_decl
4820 42798 : && c_switch_covers_all_cases_p (cs->cases,
4821 42798 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4822 552151 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4823 742547 : if (!cs->break_stmt_seen_p)
4824 386057 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4825 : /* Now that we're done with the switch warnings, set the switch type
4826 : to the type of the condition if the index type was of scoped enum type.
4827 : (Such types don't participate in the integer promotions.) We do this
4828 : because of bit-fields whose declared type is a scoped enum type:
4829 : gimplification will use the lowered index type, but convert the
4830 : case values to SWITCH_STMT_TYPE, which would have been the declared type
4831 : and verify_gimple_switch doesn't accept that. */
4832 742547 : if (is_bitfield_expr_with_lowered_type (cond))
4833 33034 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4834 742547 : gcc_assert (!cs->in_loop_body_p);
4835 742547 : splay_tree_delete (cs->cases);
4836 742547 : switch_stack = switch_stack->next;
4837 742547 : free (cs);
4838 742547 : }
4839 :
4840 : /* Note that a BREAK_STMT is about to be added. If it is inside of
4841 : a SWITCH_STMT and not inside of a loop body inside of it, note
4842 : in switch_stack we've seen a BREAK_STMT. */
4843 :
4844 : void
4845 4958025 : note_break_stmt (void)
4846 : {
4847 4958025 : if (switch_stack && !switch_stack->in_loop_body_p)
4848 2786241 : switch_stack->break_stmt_seen_p = true;
4849 4958025 : }
4850 :
4851 : /* Note the start of processing of an iteration statement's body.
4852 : The note_break_stmt function will do nothing while processing it.
4853 : Return a flag that should be passed to note_iteration_stmt_body_end. */
4854 :
4855 : bool
4856 18738425 : note_iteration_stmt_body_start (void)
4857 : {
4858 18738425 : if (!switch_stack)
4859 : return false;
4860 198823 : bool ret = switch_stack->in_loop_body_p;
4861 198823 : switch_stack->in_loop_body_p = true;
4862 198823 : return ret;
4863 : }
4864 :
4865 : /* Note the end of processing of an iteration statement's body. */
4866 :
4867 : void
4868 18738425 : note_iteration_stmt_body_end (bool prev)
4869 : {
4870 18738425 : if (switch_stack)
4871 198823 : switch_stack->in_loop_body_p = prev;
4872 18738425 : }
4873 :
4874 : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4875 : condition. Note that if TYPE and VALUE are already integral we don't
4876 : really do the conversion because the language-independent
4877 : warning/optimization code will work better that way. */
4878 :
4879 : static tree
4880 3667834 : case_conversion (tree type, tree value)
4881 : {
4882 3667834 : if (value == NULL_TREE)
4883 : return value;
4884 :
4885 1573408 : value = mark_rvalue_use (value);
4886 :
4887 1573408 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4888 1128801 : type = type_promotes_to (type);
4889 :
4890 1573408 : tree ovalue = value;
4891 : /* The constant-expression VALUE shall be a converted constant expression
4892 : of the adjusted type of the switch condition, which doesn't allow
4893 : narrowing conversions. */
4894 1573408 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4895 :
4896 1573408 : if (cxx_dialect >= cxx11
4897 1573408 : && (SCOPED_ENUM_P (type)
4898 1125694 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4899 : /* Use the converted value. */;
4900 : else
4901 : /* The already integral case. */
4902 : value = ovalue;
4903 :
4904 1573408 : return cxx_constant_value (value);
4905 : }
4906 :
4907 : /* Note that we've seen a definition of a case label, and complain if this
4908 : is a bad place for one. */
4909 :
4910 : tree
4911 6039314 : finish_case_label (location_t loc, tree low_value, tree high_value)
4912 : {
4913 6039314 : tree cond, r;
4914 6039314 : cp_binding_level *p;
4915 6039314 : tree type;
4916 :
4917 6039314 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4918 552115 : switch_stack->has_default_p = true;
4919 :
4920 6039314 : if (processing_template_decl)
4921 : {
4922 4205274 : tree label;
4923 :
4924 : /* For templates, just add the case label; we'll do semantic
4925 : analysis at instantiation-time. But diagnose case labels
4926 : in expansion statements with switch outside of it here. */
4927 4205274 : if (in_expansion_stmt)
4928 39 : for (cp_binding_level *b = current_binding_level;
4929 78 : b != switch_stack->level; b = b->level_chain)
4930 63 : if (b->kind == sk_template_for && b->this_entity)
4931 : {
4932 24 : auto_diagnostic_group d;
4933 24 : error ("jump to case label");
4934 24 : inform (EXPR_LOCATION (b->this_entity),
4935 : " enters %<template for%> statement");
4936 24 : return error_mark_node;
4937 24 : }
4938 4205250 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4939 4205250 : return add_stmt (build_case_label (low_value, high_value, label));
4940 : }
4941 :
4942 : /* Find the condition on which this switch statement depends. */
4943 1834040 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4944 1834040 : if (cond && TREE_CODE (cond) == TREE_LIST)
4945 0 : cond = TREE_VALUE (cond);
4946 :
4947 1834040 : int chk_switch_goto = check_switch_goto (switch_stack->level, NULL_TREE);
4948 1834040 : if (!chk_switch_goto)
4949 87 : return error_mark_node;
4950 :
4951 1833953 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4952 1833953 : if (type == error_mark_node)
4953 : return error_mark_node;
4954 :
4955 1833917 : low_value = case_conversion (type, low_value);
4956 1833917 : high_value = case_conversion (type, high_value);
4957 :
4958 1833917 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4959 :
4960 1833917 : if (r != error_mark_node && chk_switch_goto == 2)
4961 43 : check_switch_goto (switch_stack->level, r);
4962 :
4963 : /* After labels, make any new cleanups in the function go into their
4964 : own new (temporary) binding contour. */
4965 1833917 : for (p = current_binding_level;
4966 7703721 : p->kind != sk_function_parms;
4967 5869804 : p = p->level_chain)
4968 5869804 : p->more_cleanups_ok = 0;
4969 :
4970 : return r;
4971 : }
4972 :
4973 : struct typename_info {
4974 : tree scope;
4975 : tree name;
4976 : tree template_id;
4977 : tag_types tag_type;
4978 : };
4979 :
4980 : struct typename_hasher : ggc_ptr_hash<tree_node>
4981 : {
4982 : typedef typename_info *compare_type;
4983 :
4984 : /* Hash a TYPENAME_TYPE. */
4985 :
4986 : static hashval_t
4987 470550497 : hash (tree context, tree fullname)
4988 : {
4989 470550497 : hashval_t hash = 0;
4990 470550497 : hash = iterative_hash_hashval_t (TYPE_HASH (context), hash);
4991 : /* FULLNAME could be a template-id, so use iterative_hash_template_arg here.
4992 : And might as well set comparing_specializations for stronger hashing. */
4993 470550497 : ++comparing_specializations;
4994 470550497 : hash = iterative_hash_template_arg (fullname, hash);
4995 470550497 : --comparing_specializations;
4996 470550497 : return hash;
4997 : }
4998 :
4999 : static hashval_t
5000 66040166 : hash (const typename_info *ti)
5001 : {
5002 66040166 : return typename_hasher::hash (ti->scope, ti->template_id);
5003 : }
5004 :
5005 : static hashval_t
5006 404510331 : hash (tree t)
5007 : {
5008 404510331 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
5009 : }
5010 :
5011 : /* Compare two TYPENAME_TYPEs. */
5012 :
5013 : static bool
5014 478368230 : equal (tree t1, const typename_info *t2)
5015 : {
5016 478368230 : return (TYPE_IDENTIFIER (t1) == t2->name
5017 157659788 : && TYPE_CONTEXT (t1) == t2->scope
5018 34416931 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
5019 512066144 : && get_typename_tag (t1) == t2->tag_type);
5020 : }
5021 : };
5022 :
5023 : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
5024 : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
5025 :
5026 : Returns the new TYPENAME_TYPE. */
5027 :
5028 : static GTY (()) hash_table<typename_hasher> *typename_htab;
5029 :
5030 : tree
5031 66040166 : build_typename_type (tree context, tree name, tree fullname,
5032 : enum tag_types tag_type)
5033 : {
5034 66040166 : typename_info ti;
5035 :
5036 66040166 : if (typename_htab == NULL)
5037 20563 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
5038 :
5039 66040166 : ti.scope = FROB_CONTEXT (context);
5040 66040166 : ti.name = name;
5041 66040166 : ti.template_id = fullname;
5042 66040166 : ti.tag_type = tag_type;
5043 66040166 : hashval_t hash = typename_hasher::hash (&ti);
5044 :
5045 : /* See if we already have this type. */
5046 66040166 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
5047 66040166 : tree t = *e;
5048 66040166 : if (*e)
5049 : t = *e;
5050 : else
5051 : {
5052 : /* Build the TYPENAME_TYPE. */
5053 33344268 : t = cxx_make_type (TYPENAME_TYPE);
5054 33344268 : TYPE_CONTEXT (t) = ti.scope;
5055 33344268 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
5056 33344268 : set_typename_tag (t, ti.tag_type);
5057 :
5058 : /* Build the corresponding TYPE_DECL. */
5059 33344268 : tree d = build_decl (input_location, TYPE_DECL, name, t);
5060 33344268 : TYPE_NAME (t) = d;
5061 33344268 : TYPE_STUB_DECL (t) = d;
5062 33344268 : DECL_CONTEXT (d) = ti.scope;
5063 33344268 : DECL_ARTIFICIAL (d) = 1;
5064 :
5065 : /* Store it in the hash table. */
5066 33344268 : *e = t;
5067 :
5068 : /* TYPENAME_TYPEs must always be compared structurally, because
5069 : they may or may not resolve down to another type depending on
5070 : the currently open classes. */
5071 33344268 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5072 : }
5073 :
5074 66040166 : return t;
5075 : }
5076 :
5077 : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
5078 : provided to name the type. Returns an appropriate type, unless an
5079 : error occurs, in which case error_mark_node is returned. If we
5080 : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
5081 : return that, rather than the _TYPE it corresponds to, in other
5082 : cases we look through the type decl. If TF_ERROR is set, complain
5083 : about errors, otherwise be quiet. */
5084 :
5085 : tree
5086 279788266 : make_typename_type (tree context, tree name, enum tag_types tag_type,
5087 : tsubst_flags_t complain)
5088 : {
5089 279788266 : tree fullname;
5090 279788266 : tree t;
5091 279788266 : bool want_template;
5092 :
5093 279788266 : if (name == error_mark_node
5094 279788260 : || context == NULL_TREE
5095 279788260 : || context == error_mark_node)
5096 : return error_mark_node;
5097 :
5098 279788244 : if (TYPE_P (name))
5099 : {
5100 0 : if (!(TYPE_LANG_SPECIFIC (name)
5101 0 : && (CLASSTYPE_IS_TEMPLATE (name)
5102 0 : || CLASSTYPE_USE_TEMPLATE (name))))
5103 0 : name = TYPE_IDENTIFIER (name);
5104 : else
5105 : /* Create a TEMPLATE_ID_EXPR for the type. */
5106 0 : name = build_nt (TEMPLATE_ID_EXPR,
5107 0 : CLASSTYPE_TI_TEMPLATE (name),
5108 0 : CLASSTYPE_TI_ARGS (name));
5109 : }
5110 279788244 : else if (TREE_CODE (name) == TYPE_DECL)
5111 0 : name = DECL_NAME (name);
5112 :
5113 279788244 : fullname = name;
5114 :
5115 279788244 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5116 : {
5117 5457517 : name = TREE_OPERAND (name, 0);
5118 5457517 : if (DECL_TYPE_TEMPLATE_P (name))
5119 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5120 5457517 : if (TREE_CODE (name) != IDENTIFIER_NODE)
5121 : {
5122 0 : if (complain & tf_error)
5123 0 : error ("%qD is not a type", name);
5124 0 : return error_mark_node;
5125 : }
5126 : }
5127 279788244 : if (TREE_CODE (name) == TEMPLATE_DECL)
5128 : {
5129 0 : if (complain & tf_error)
5130 0 : error ("%qD used without template arguments", name);
5131 0 : return error_mark_node;
5132 : }
5133 279788244 : else if (is_overloaded_fn (name))
5134 : {
5135 3 : if (complain & tf_error)
5136 3 : error ("%qD is a function, not a type", name);
5137 3 : return error_mark_node;
5138 : }
5139 279788241 : gcc_assert (identifier_p (name));
5140 279788241 : gcc_assert (TYPE_P (context));
5141 :
5142 279788241 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5143 : /* This can happen for C++17 variadic using (c++/88986). */;
5144 266194623 : else if (!MAYBE_CLASS_TYPE_P (context))
5145 : {
5146 6 : if (complain & tf_error)
5147 6 : error ("%q#T is not a class", context);
5148 6 : return error_mark_node;
5149 : }
5150 :
5151 : /* When the CONTEXT is a dependent type, NAME could refer to a
5152 : dependent base class of CONTEXT. But look inside it anyway
5153 : if CONTEXT is a currently open scope, in case it refers to a
5154 : member of the current instantiation or a non-dependent base;
5155 : lookup will stop when we hit a dependent base. */
5156 279788235 : if (!dependent_scope_p (context))
5157 : {
5158 : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5159 : (as per [temp.res.general]/3), unless
5160 : - the tag corresponds to a class-key or 'enum' so
5161 : [basic.lookup.elab] applies, or
5162 : - the tag corresponds to scope_type or tf_qualifying_scope is
5163 : set so [basic.lookup.qual]/1 applies, or
5164 : - we're inside a base-specifier so [class.derived.general]/2 applies;
5165 : the tag will already be class_type in that case.
5166 : TODO: If we'd set/track the scope_type tag thoroughly on all
5167 : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5168 : tf_qualifying_scope flag. */
5169 437428968 : bool want_type = (tag_type != none_type && tag_type != typename_type)
5170 218714484 : || (complain & tf_qualifying_scope);
5171 218714484 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5172 : }
5173 : else
5174 : t = NULL_TREE;
5175 :
5176 279788235 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5177 61074772 : return build_typename_type (context, name, fullname, tag_type);
5178 :
5179 218713463 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5180 :
5181 218713463 : if (!t)
5182 : {
5183 19851216 : if (complain & tf_error)
5184 : {
5185 223 : if (!COMPLETE_TYPE_P (context))
5186 18 : cxx_incomplete_type_error (NULL_TREE, context);
5187 : else
5188 409 : error (want_template ? G_("no class template named %q#T in %q#T")
5189 : : G_("no type named %q#T in %q#T"), name, context);
5190 : }
5191 19851216 : return error_mark_node;
5192 : }
5193 :
5194 : /* Pull out the template from an injected-class-name (or multiple). */
5195 198862247 : if (want_template)
5196 3470665 : t = maybe_get_template_decl_from_type_decl (t);
5197 :
5198 198862247 : if (TREE_CODE (t) == TREE_LIST)
5199 : {
5200 3 : if (complain & tf_error)
5201 : {
5202 3 : auto_diagnostic_group d;
5203 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
5204 3 : print_candidates (input_location, t);
5205 3 : }
5206 3 : return error_mark_node;
5207 : }
5208 :
5209 198862244 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5210 : {
5211 6 : if (complain & tf_error)
5212 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
5213 : context, name, t);
5214 6 : return error_mark_node;
5215 : }
5216 198862238 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
5217 : {
5218 2187 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5219 4377 : && DECL_TYPE_TEMPLATE_P (t))
5220 : /* The caller permits this typename-specifier to name a template
5221 : (because it appears in a CTAD-enabled context). */;
5222 : else
5223 : {
5224 23 : if (complain & tf_error)
5225 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
5226 : context, name, t);
5227 23 : return error_mark_node;
5228 : }
5229 : }
5230 :
5231 198862215 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5232 : context, complain))
5233 42 : return error_mark_node;
5234 :
5235 198862173 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5236 2176 : return make_template_placeholder (t);
5237 :
5238 198859997 : if (want_template)
5239 : {
5240 3470659 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5241 : NULL_TREE, context,
5242 : complain | tf_user);
5243 3470659 : if (t == error_mark_node)
5244 : return error_mark_node;
5245 3470654 : t = TYPE_NAME (t);
5246 : }
5247 :
5248 198859992 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5249 2418027 : t = TREE_TYPE (t);
5250 :
5251 198859992 : maybe_record_typedef_use (t);
5252 :
5253 198859992 : return t;
5254 : }
5255 :
5256 : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5257 : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5258 : in which case error_mark_node is returned.
5259 :
5260 : If PARM_LIST is non-NULL, also make sure that the template parameter
5261 : list of TEMPLATE_DECL matches.
5262 :
5263 : If COMPLAIN zero, don't complain about any errors that occur. */
5264 :
5265 : tree
5266 189881 : make_unbound_class_template (tree context, tree name, tree parm_list,
5267 : tsubst_flags_t complain)
5268 : {
5269 189881 : if (TYPE_P (name))
5270 0 : name = TYPE_IDENTIFIER (name);
5271 189881 : else if (DECL_P (name))
5272 0 : name = DECL_NAME (name);
5273 189881 : gcc_assert (identifier_p (name));
5274 :
5275 189881 : if (!dependent_type_p (context)
5276 189881 : || currently_open_class (context))
5277 : {
5278 93688 : tree tmpl = NULL_TREE;
5279 :
5280 93688 : if (MAYBE_CLASS_TYPE_P (context))
5281 93682 : tmpl = lookup_field (context, name, 0, false);
5282 :
5283 93682 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5284 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5285 :
5286 93676 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5287 : {
5288 12 : if (complain & tf_error)
5289 9 : error ("no class template named %q#T in %q#T", name, context);
5290 12 : return error_mark_node;
5291 : }
5292 :
5293 93676 : if (parm_list
5294 93676 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5295 : {
5296 0 : if (complain & tf_error)
5297 : {
5298 0 : auto_diagnostic_group d;
5299 0 : error ("template parameters do not match template %qD", tmpl);
5300 0 : inform (DECL_SOURCE_LOCATION (tmpl),
5301 : "%qD declared here", tmpl);
5302 0 : }
5303 0 : return error_mark_node;
5304 : }
5305 :
5306 93676 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5307 : complain))
5308 0 : return error_mark_node;
5309 :
5310 : return tmpl;
5311 : }
5312 :
5313 96193 : return make_unbound_class_template_raw (context, name, parm_list);
5314 : }
5315 :
5316 : /* Build an UNBOUND_CLASS_TEMPLATE. */
5317 :
5318 : tree
5319 96245 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5320 : {
5321 : /* Build the UNBOUND_CLASS_TEMPLATE. */
5322 96245 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5323 96245 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5324 96245 : TREE_TYPE (t) = NULL_TREE;
5325 96245 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5326 :
5327 : /* Build the corresponding TEMPLATE_DECL. */
5328 96245 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5329 96245 : TYPE_NAME (t) = d;
5330 96245 : TYPE_STUB_DECL (t) = d;
5331 96245 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5332 96245 : DECL_ARTIFICIAL (d) = 1;
5333 96245 : DECL_TEMPLATE_PARMS (d) = parm_list;
5334 :
5335 96245 : return t;
5336 : }
5337 :
5338 :
5339 :
5340 : /* Push the declarations of builtin types into the global namespace.
5341 : RID_INDEX is the index of the builtin type in the array
5342 : RID_POINTERS. NAME is the name used when looking up the builtin
5343 : type. TYPE is the _TYPE node for the builtin type.
5344 :
5345 : The calls to set_global_binding below should be
5346 : eliminated. Built-in types should not be looked up name; their
5347 : names are keywords that the parser can recognize. However, there
5348 : is code in c-common.cc that uses identifier_global_value to look up
5349 : built-in types by name. */
5350 :
5351 : void
5352 4209598 : record_builtin_type (enum rid rid_index,
5353 : const char* name,
5354 : tree type)
5355 : {
5356 4209598 : tree decl = NULL_TREE;
5357 :
5358 4209598 : if (name)
5359 : {
5360 2484285 : tree tname = get_identifier (name);
5361 2484285 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5362 2484285 : DECL_ARTIFICIAL (tdecl) = 1;
5363 2484285 : set_global_binding (tdecl);
5364 2484285 : decl = tdecl;
5365 : }
5366 :
5367 4209598 : if ((int) rid_index < (int) RID_MAX)
5368 2584416 : if (tree rname = ridpointers[(int) rid_index])
5369 1975482 : if (!decl || DECL_NAME (decl) != rname)
5370 : {
5371 1420846 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5372 1420846 : DECL_ARTIFICIAL (rdecl) = 1;
5373 1420846 : set_global_binding (rdecl);
5374 1420846 : if (!decl)
5375 : decl = rdecl;
5376 : }
5377 :
5378 3905131 : if (decl)
5379 : {
5380 3600664 : if (!TYPE_NAME (type))
5381 2991730 : TYPE_NAME (type) = decl;
5382 3600664 : debug_hooks->type_decl (decl, 0);
5383 : }
5384 4209598 : }
5385 :
5386 : /* Push a type into the namespace so that the back ends ignore it. */
5387 :
5388 : static void
5389 304467 : record_unknown_type (tree type, const char* name)
5390 : {
5391 304467 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5392 : TYPE_DECL, get_identifier (name), type));
5393 : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5394 304467 : DECL_IGNORED_P (decl) = 1;
5395 304467 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5396 304467 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5397 304467 : SET_TYPE_ALIGN (type, 1);
5398 304467 : TYPE_USER_ALIGN (type) = 0;
5399 304467 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5400 304467 : }
5401 :
5402 : /* Create all the predefined identifiers. */
5403 :
5404 : static void
5405 101489 : initialize_predefined_identifiers (void)
5406 : {
5407 101489 : struct predefined_identifier
5408 : {
5409 : const char *name; /* Name. */
5410 : tree *node; /* Node to store it in. */
5411 : cp_identifier_kind kind; /* Kind of identifier. */
5412 : };
5413 :
5414 : /* A table of identifiers to create at startup. */
5415 101489 : static const predefined_identifier predefined_identifiers[] = {
5416 : {"C++", &lang_name_cplusplus, cik_normal},
5417 : {"C", &lang_name_c, cik_normal},
5418 : /* Some of these names have a trailing space so that it is
5419 : impossible for them to conflict with names written by users. */
5420 : {"__ct ", &ctor_identifier, cik_ctor},
5421 : {"__ct_base ", &base_ctor_identifier, cik_ctor},
5422 : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
5423 : {"__dt ", &dtor_identifier, cik_dtor},
5424 : {"__dt_base ", &base_dtor_identifier, cik_dtor},
5425 : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
5426 : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
5427 : {"__conv_op ", &conv_op_identifier, cik_conv_op},
5428 : {"__in_chrg", &in_charge_identifier, cik_normal},
5429 : {"__as_base ", &as_base_identifier, cik_normal},
5430 : {"this", &this_identifier, cik_normal},
5431 : {"__delta", &delta_identifier, cik_normal},
5432 : {"__pfn", &pfn_identifier, cik_normal},
5433 : {"_vptr", &vptr_identifier, cik_normal},
5434 : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
5435 : {"::", &global_identifier, cik_normal},
5436 : /* The demangler expects anonymous namespaces to be called
5437 : something starting with '_GLOBAL__N_'. It no longer needs
5438 : to be unique to the TU. */
5439 : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
5440 : {"auto", &auto_identifier, cik_normal},
5441 : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
5442 : {"initializer_list", &init_list_identifier, cik_normal},
5443 : {"__for_range ", &for_range__identifier, cik_normal},
5444 : {"__for_begin ", &for_begin__identifier, cik_normal},
5445 : {"__for_end ", &for_end__identifier, cik_normal},
5446 : {"__for_range", &for_range_identifier, cik_normal},
5447 : {"__for_begin", &for_begin_identifier, cik_normal},
5448 : {"__for_end", &for_end_identifier, cik_normal},
5449 : {"abi_tag", &abi_tag_identifier, cik_normal},
5450 : {"aligned", &aligned_identifier, cik_normal},
5451 : {"begin", &begin_identifier, cik_normal},
5452 : {"end", &end_identifier, cik_normal},
5453 : {"get", &get__identifier, cik_normal},
5454 : {"gnu", &gnu_identifier, cik_normal},
5455 : {"tuple_element", &tuple_element_identifier, cik_normal},
5456 : {"tuple_size", &tuple_size_identifier, cik_normal},
5457 : {"type", &type_identifier, cik_normal},
5458 : {"value", &value_identifier, cik_normal},
5459 : {"_FUN", &fun_identifier, cik_normal},
5460 : {"__closure", &closure_identifier, cik_normal},
5461 : {"heap uninit", &heap_uninit_identifier, cik_normal},
5462 : {"heap ", &heap_identifier, cik_normal},
5463 : {"heap deleted", &heap_deleted_identifier, cik_normal},
5464 : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
5465 : {"heap []", &heap_vec_identifier, cik_normal},
5466 : {"omp", &omp_identifier, cik_normal},
5467 : {"internal ", &internal_identifier, cik_normal},
5468 : {"annotation ", &annotation_identifier, cik_normal},
5469 : {NULL, NULL, cik_normal}
5470 : };
5471 :
5472 4972961 : for (const predefined_identifier *pid = predefined_identifiers;
5473 4972961 : pid->name; ++pid)
5474 : {
5475 4871472 : *pid->node = get_identifier (pid->name);
5476 : /* Some of these identifiers already have a special kind. */
5477 4871472 : if (pid->kind != cik_normal)
5478 811912 : set_identifier_kind (*pid->node, pid->kind);
5479 : }
5480 101489 : }
5481 :
5482 : /* Build a specific variant of operator new. */
5483 :
5484 : static void
5485 181549 : cxx_build_operator_new (tree newtype)
5486 : {
5487 181549 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5488 181549 : DECL_IS_MALLOC (opnew) = 1;
5489 181549 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5490 181549 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5491 181549 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5492 181549 : DECL_IS_MALLOC (opnew) = 1;
5493 181549 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5494 181549 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5495 181549 : }
5496 :
5497 : /* Build a specific variant of operator delete. */
5498 :
5499 : static void
5500 342994 : cxx_build_operator_delete (tree deltype)
5501 : {
5502 342994 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5503 342994 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5504 342994 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5505 342994 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5506 342994 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5507 342994 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5508 342994 : }
5509 :
5510 : /* Declare all variants of operator new and delete. */
5511 :
5512 : static void
5513 101489 : cxx_init_operator_new_delete_decls (void)
5514 : {
5515 101489 : tree newattrs, extvisattr;
5516 101489 : tree newtype, deltype;
5517 101489 : tree ptr_ftype_sizetype;
5518 101489 : tree new_eh_spec;
5519 101489 : tree void_ftype_ptr = build_function_type_list (void_type_node,
5520 : ptr_type_node, NULL_TREE);
5521 101489 : void_ftype_ptr
5522 101489 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
5523 :
5524 101489 : ptr_ftype_sizetype
5525 101489 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5526 101489 : if (cxx_dialect == cxx98)
5527 : {
5528 14327 : tree bad_alloc_id;
5529 14327 : tree bad_alloc_type_node;
5530 14327 : tree bad_alloc_decl;
5531 :
5532 14327 : push_nested_namespace (std_node);
5533 14327 : bad_alloc_id = get_identifier ("bad_alloc");
5534 14327 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5535 14327 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5536 14327 : bad_alloc_decl
5537 14327 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5538 14327 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5539 14327 : pop_nested_namespace (std_node);
5540 :
5541 14327 : new_eh_spec
5542 14327 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5543 : }
5544 : else
5545 87162 : new_eh_spec = noexcept_false_spec;
5546 :
5547 101489 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5548 : NULL_TREE);
5549 101489 : newattrs = tree_cons (get_identifier ("alloc_size"),
5550 : build_tree_list (NULL_TREE, integer_one_node),
5551 : extvisattr);
5552 101489 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5553 101489 : newtype = build_exception_variant (newtype, new_eh_spec);
5554 101489 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5555 101489 : deltype = build_exception_variant (deltype, empty_except_spec);
5556 101489 : cxx_build_operator_new (newtype);
5557 101489 : cxx_build_operator_delete (deltype);
5558 101489 : if (flag_sized_deallocation)
5559 : {
5560 : /* Also push the sized deallocation variants:
5561 : void operator delete(void*, std::size_t) throw();
5562 : void operator delete[](void*, std::size_t) throw(); */
5563 81390 : tree void_ftype_ptr_size
5564 81390 : = build_function_type_list (void_type_node, ptr_type_node,
5565 : size_type_node, NULL_TREE);
5566 81390 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5567 : extvisattr);
5568 81390 : deltype = build_exception_variant (deltype, empty_except_spec);
5569 81390 : cxx_build_operator_delete (deltype);
5570 : }
5571 :
5572 101489 : if (aligned_new_threshold)
5573 : {
5574 80060 : push_nested_namespace (std_node);
5575 80060 : tree align_id = get_identifier ("align_val_t");
5576 80060 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5577 : NULL_TREE, /*scoped*/true, NULL);
5578 80060 : pop_nested_namespace (std_node);
5579 :
5580 : /* operator new (size_t, align_val_t); */
5581 80060 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5582 : align_type_node, NULL_TREE);
5583 80060 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5584 80060 : newtype = build_exception_variant (newtype, new_eh_spec);
5585 80060 : cxx_build_operator_new (newtype);
5586 :
5587 : /* operator delete (void *, align_val_t); */
5588 80060 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5589 : align_type_node, NULL_TREE);
5590 80060 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5591 80060 : deltype = build_exception_variant (deltype, empty_except_spec);
5592 80060 : cxx_build_operator_delete (deltype);
5593 :
5594 80060 : if (flag_sized_deallocation)
5595 : {
5596 : /* operator delete (void *, size_t, align_val_t); */
5597 80055 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5598 : size_type_node, align_type_node,
5599 : NULL_TREE);
5600 80055 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5601 80055 : deltype = build_exception_variant (deltype, empty_except_spec);
5602 80055 : cxx_build_operator_delete (deltype);
5603 : }
5604 : }
5605 101489 : }
5606 :
5607 : /* Create the predefined scalar types of C,
5608 : and some nodes representing standard constants (0, 1, (void *)0).
5609 : Initialize the global binding level.
5610 : Make definitions for built-in primitive functions. */
5611 :
5612 : void
5613 101489 : cxx_init_decl_processing (void)
5614 : {
5615 101489 : tree void_ftype;
5616 :
5617 : /* Create all the identifiers we need. */
5618 101489 : initialize_predefined_identifiers ();
5619 :
5620 : /* Create the global variables. */
5621 101489 : push_to_top_level ();
5622 :
5623 101489 : current_function_decl = NULL_TREE;
5624 101489 : current_binding_level = NULL;
5625 : /* Enter the global namespace. */
5626 101489 : gcc_assert (global_namespace == NULL_TREE);
5627 101489 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5628 : void_type_node);
5629 101489 : TREE_PUBLIC (global_namespace) = true;
5630 101489 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5631 101489 : DECL_CONTEXT (global_namespace)
5632 101489 : = build_translation_unit_decl (get_identifier (main_input_filename));
5633 : /* Remember whether we want the empty class passing ABI change warning
5634 : in this TU. */
5635 101489 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5636 128447 : = warn_abi && abi_version_crosses (12);
5637 101489 : debug_hooks->register_main_translation_unit
5638 101489 : (DECL_CONTEXT (global_namespace));
5639 101489 : begin_scope (sk_namespace, global_namespace);
5640 101489 : current_namespace = global_namespace;
5641 :
5642 101489 : if (flag_visibility_ms_compat)
5643 6 : default_visibility = VISIBILITY_HIDDEN;
5644 :
5645 : /* Initially, C. */
5646 101489 : current_lang_name = lang_name_c;
5647 :
5648 : /* Create the `std' namespace. */
5649 101489 : push_namespace (get_identifier ("std"));
5650 101489 : std_node = current_namespace;
5651 101489 : pop_namespace ();
5652 :
5653 101489 : flag_noexcept_type = (cxx_dialect >= cxx17);
5654 :
5655 101489 : c_common_nodes_and_builtins ();
5656 :
5657 : /* Call the target stack_protect_guard hook if the stack protection
5658 : guard is declared as a global symbol. */
5659 101489 : if (targetm.stack_protect_guard_symbol_p ())
5660 24 : pushdecl (targetm.stack_protect_guard ());
5661 :
5662 101489 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5663 101489 : tree decl
5664 101489 : = add_builtin_function ("__builtin_is_constant_evaluated",
5665 : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5666 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5667 101489 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5668 :
5669 : /* The concrete return type of __builtin_source_location is
5670 : const std::source_location::__impl*, but we can't form the type
5671 : at this point. So we initially declare it with an auto return
5672 : type which we then "deduce" from require_deduced_type upon first use. */
5673 101489 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5674 101489 : decl = add_builtin_function ("__builtin_source_location",
5675 : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5676 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5677 101489 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5678 :
5679 101489 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5680 : NULL_TREE);
5681 101489 : decl
5682 101489 : = add_builtin_function ("__builtin_is_corresponding_member",
5683 : bool_vaftype,
5684 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5685 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5686 101489 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5687 :
5688 101489 : decl
5689 101489 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5690 : bool_vaftype,
5691 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5692 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5693 101489 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5694 :
5695 101489 : if (cxx_dialect >= cxx26)
5696 : {
5697 25613 : tree void_ptrintftype
5698 25613 : = build_function_type_list (void_type_node, ptr_type_node,
5699 : integer_type_node, NULL_TREE);
5700 25613 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5701 : void_ptrintftype,
5702 : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5703 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5704 25613 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5705 :
5706 : /* Similar case to __builtin_source_location above. The concrete
5707 : return type is std::exception_ptr, but we can't form the type
5708 : at this point, so it is deduced later. */
5709 25613 : decl = add_builtin_function ("__builtin_current_exception",
5710 : auto_ftype, CP_BUILT_IN_CURRENT_EXCEPTION,
5711 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5712 25613 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5713 :
5714 25613 : tree int_ftype = build_function_type_list (integer_type_node, NULL_TREE);
5715 25613 : decl = add_builtin_function ("__builtin_uncaught_exceptions",
5716 : int_ftype, CP_BUILT_IN_UNCAUGHT_EXCEPTIONS,
5717 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5718 25613 : set_call_expr_flags (decl, ECF_PURE | ECF_NOTHROW | ECF_LEAF);
5719 : }
5720 :
5721 101489 : decl
5722 101489 : = add_builtin_function ("__builtin_is_string_literal",
5723 : bool_vaftype,
5724 : CP_BUILT_IN_IS_STRING_LITERAL,
5725 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5726 101489 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5727 :
5728 101489 : tree void_vaintftype = build_varargs_function_type_list (void_type_node,
5729 : integer_type_node,
5730 : NULL_TREE);
5731 101489 : decl = add_builtin_function ("__builtin_constexpr_diag",
5732 : void_vaintftype,
5733 : CP_BUILT_IN_CONSTEXPR_DIAG,
5734 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5735 101489 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5736 :
5737 101489 : decl = add_builtin_function ("__builtin_is_within_lifetime",
5738 : bool_vaftype, CP_BUILT_IN_IS_WITHIN_LIFETIME,
5739 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5740 101489 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5741 101489 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
5742 :
5743 101489 : tree void_vaftype = build_varargs_function_type_list (void_type_node,
5744 : NULL_TREE);
5745 101489 : decl = add_builtin_function ("__builtin_start_lifetime",
5746 : void_vaftype, CP_BUILT_IN_START_LIFETIME,
5747 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5748 101489 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5749 :
5750 101489 : integer_two_node = build_int_cst (integer_type_node, 2);
5751 :
5752 : /* Guess at the initial static decls size. */
5753 101489 : vec_alloc (static_decls, 500);
5754 :
5755 : /* ... and keyed classes. */
5756 101489 : vec_alloc (keyed_classes, 100);
5757 :
5758 101489 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5759 101489 : truthvalue_type_node = boolean_type_node;
5760 101489 : truthvalue_false_node = boolean_false_node;
5761 101489 : truthvalue_true_node = boolean_true_node;
5762 :
5763 101489 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5764 101489 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5765 101489 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5766 101489 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5767 : NULL_TREE);
5768 :
5769 : #if 0
5770 : record_builtin_type (RID_MAX, NULL, string_type_node);
5771 : #endif
5772 :
5773 101489 : delta_type_node = ptrdiff_type_node;
5774 101489 : vtable_index_type = ptrdiff_type_node;
5775 :
5776 101489 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5777 101489 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5778 :
5779 : /* Create the conversion operator marker. This operator's DECL_NAME
5780 : is in the identifier table, so we can use identifier equality to
5781 : find it. */
5782 101489 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5783 : void_ftype);
5784 :
5785 : /* C++ extensions */
5786 :
5787 101489 : unknown_type_node = make_node (LANG_TYPE);
5788 101489 : record_unknown_type (unknown_type_node, "unknown type");
5789 :
5790 : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5791 101489 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5792 :
5793 : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5794 : result. */
5795 101489 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5796 101489 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5797 :
5798 101489 : init_list_type_node = make_node (LANG_TYPE);
5799 101489 : record_unknown_type (init_list_type_node, "init list");
5800 :
5801 : /* Used when parsing to distinguish parameter-lists () and (void). */
5802 101489 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5803 :
5804 101489 : {
5805 : /* Make sure we get a unique function type, so we can give
5806 : its pointer type a name. (This wins for gdb.) */
5807 101489 : tree vfunc_type = make_node (FUNCTION_TYPE);
5808 101489 : TREE_TYPE (vfunc_type) = integer_type_node;
5809 101489 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5810 101489 : layout_type (vfunc_type);
5811 :
5812 101489 : vtable_entry_type = build_pointer_type (vfunc_type);
5813 : }
5814 101489 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5815 :
5816 101489 : vtbl_type_node
5817 101489 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5818 101489 : layout_type (vtbl_type_node);
5819 101489 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5820 101489 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5821 101489 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5822 101489 : layout_type (vtbl_ptr_type_node);
5823 101489 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5824 :
5825 101489 : push_namespace (get_identifier ("__cxxabiv1"));
5826 101489 : abi_node = current_namespace;
5827 101489 : pop_namespace ();
5828 :
5829 101489 : any_targ_node = make_node (LANG_TYPE);
5830 101489 : record_unknown_type (any_targ_node, "any type");
5831 :
5832 : /* Now, C++. */
5833 101489 : current_lang_name = lang_name_cplusplus;
5834 :
5835 101489 : if (aligned_new_threshold > 1
5836 101489 : && !pow2p_hwi (aligned_new_threshold))
5837 : {
5838 0 : error ("%<-faligned-new=%d%> is not a power of two",
5839 : aligned_new_threshold);
5840 0 : aligned_new_threshold = 1;
5841 : }
5842 101489 : if (aligned_new_threshold == -1)
5843 122904 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5844 101489 : if (aligned_new_threshold == 1)
5845 80057 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5846 :
5847 : /* Ensure attribs.cc is initialized. */
5848 101489 : init_attributes ();
5849 101489 : cxx_init_operator_new_delete_decls ();
5850 :
5851 : /* C++-specific nullptr initialization. */
5852 101489 : if (abi_version_at_least (9))
5853 101318 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5854 101489 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5855 :
5856 101489 : if (! supports_one_only ())
5857 0 : flag_weak = 0;
5858 :
5859 101489 : abort_fndecl
5860 101489 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5861 : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5862 101489 : if (flag_weak)
5863 : /* If no definition is available, resolve references to NULL. */
5864 101459 : declare_weak (abort_fndecl);
5865 :
5866 : /* Perform other language dependent initializations. */
5867 101489 : init_class_processing ();
5868 101489 : init_rtti_processing ();
5869 101489 : init_template_processing ();
5870 :
5871 101489 : if (flag_exceptions)
5872 100232 : init_exception_processing ();
5873 :
5874 101489 : if (flag_contracts)
5875 25658 : init_contracts ();
5876 :
5877 101489 : if (modules_p ())
5878 5033 : init_modules (parse_in);
5879 :
5880 101489 : if (flag_reflection)
5881 1358 : init_reflection ();
5882 :
5883 101489 : make_fname_decl = cp_make_fname_decl;
5884 101489 : start_fname_decls ();
5885 :
5886 : /* Show we use EH for cleanups. */
5887 101489 : if (flag_exceptions)
5888 100232 : using_eh_for_cleanups ();
5889 :
5890 : /* Check that the hardware interference sizes are at least
5891 : alignof(max_align_t), as required by the standard. */
5892 101489 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5893 101489 : if (OPTION_SET_P (param_destruct_interfere_size))
5894 : {
5895 0 : if (param_destruct_interfere_size < max_align)
5896 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5897 : "%d", param_destruct_interfere_size, max_align);
5898 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5899 0 : warning (OPT_Winterference_size,
5900 : "%<--param destructive-interference-size=%d%> "
5901 : "is less than %<--param l1-cache-line-size=%d%>",
5902 : param_destruct_interfere_size, param_l1_cache_line_size);
5903 : }
5904 101489 : else if (param_destruct_interfere_size)
5905 : /* Assume the internal value is OK. */;
5906 0 : else if (param_l1_cache_line_size >= max_align)
5907 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5908 : /* else leave it unset. */
5909 :
5910 101489 : if (OPTION_SET_P (param_construct_interfere_size))
5911 : {
5912 0 : if (param_construct_interfere_size < max_align)
5913 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5914 : "%d", param_construct_interfere_size, max_align);
5915 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5916 0 : && param_l1_cache_line_size >= max_align)
5917 0 : warning (OPT_Winterference_size,
5918 : "%<--param constructive-interference-size=%d%> "
5919 : "is greater than %<--param l1-cache-line-size=%d%>",
5920 : param_construct_interfere_size, param_l1_cache_line_size);
5921 : }
5922 101489 : else if (param_construct_interfere_size)
5923 : /* Assume the internal value is OK. */;
5924 0 : else if (param_l1_cache_line_size >= max_align)
5925 0 : param_construct_interfere_size = param_l1_cache_line_size;
5926 101489 : }
5927 :
5928 : /* Enter an abi node in global-module context. returns a cookie to
5929 : give to pop_abi_namespace. */
5930 :
5931 : unsigned
5932 60261 : push_abi_namespace (tree node)
5933 : {
5934 60261 : push_nested_namespace (node);
5935 60261 : push_visibility ("default", 2);
5936 60261 : unsigned flags = module_kind;
5937 60261 : module_kind = 0;
5938 60261 : return flags;
5939 : }
5940 :
5941 : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5942 : you. */
5943 :
5944 : void
5945 60261 : pop_abi_namespace (unsigned flags, tree node)
5946 : {
5947 60261 : module_kind = flags;
5948 60261 : pop_visibility (2);
5949 60261 : pop_nested_namespace (node);
5950 60261 : }
5951 :
5952 : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5953 : the decl, LOC is the location to give the decl, NAME is the
5954 : initialization string and TYPE_DEP indicates whether NAME depended
5955 : on the type of the function. We make use of that to detect
5956 : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5957 : at the point of first use, so we mustn't push the decl now. */
5958 :
5959 : static tree
5960 244723 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5961 : {
5962 244723 : tree domain = NULL_TREE;
5963 244723 : tree init = NULL_TREE;
5964 :
5965 244723 : if (!(type_dep && current_function_decl && in_template_context))
5966 : {
5967 60463 : const char *name = NULL;
5968 60463 : bool release_name = false;
5969 :
5970 60463 : if (current_function_decl == NULL_TREE)
5971 : name = "top level";
5972 60449 : else if (type_dep == 0)
5973 : {
5974 : /* __FUNCTION__ */
5975 838 : name = fname_as_string (type_dep);
5976 838 : release_name = true;
5977 : }
5978 : else
5979 : {
5980 : /* __PRETTY_FUNCTION__ */
5981 59611 : gcc_checking_assert (type_dep == 1);
5982 59611 : name = cxx_printable_name (current_function_decl, 2);
5983 : }
5984 :
5985 60449 : if (!release_name)
5986 : {
5987 59625 : cpp_string cstr = { 0, 0 }, strname;
5988 59625 : strname.text
5989 59625 : = const_cast <unsigned char *> ((const unsigned char *) name);
5990 59625 : strname.len = strlen (name) + 1;
5991 59625 : if (cpp_translate_string (parse_in, &strname, &cstr, CPP_STRING,
5992 : false))
5993 : {
5994 59625 : name = (const char *) cstr.text;
5995 59625 : release_name = true;
5996 : }
5997 : }
5998 :
5999 60463 : size_t length = strlen (name);
6000 60463 : domain = build_index_type (size_int (length));
6001 60463 : init = build_string (length + 1, name);
6002 60463 : if (release_name)
6003 60463 : free (const_cast<char *> (name));
6004 : }
6005 :
6006 244723 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6007 244723 : type = build_cplus_array_type (type, domain);
6008 :
6009 244723 : if (init)
6010 60463 : TREE_TYPE (init) = type;
6011 : else
6012 184260 : init = error_mark_node;
6013 :
6014 244723 : tree decl = build_decl (loc, VAR_DECL, id, type);
6015 :
6016 244723 : TREE_READONLY (decl) = 1;
6017 244723 : DECL_ARTIFICIAL (decl) = 1;
6018 244723 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
6019 244723 : TREE_STATIC (decl) = 1;
6020 :
6021 244723 : TREE_USED (decl) = 1;
6022 :
6023 244723 : SET_DECL_VALUE_EXPR (decl, init);
6024 244723 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
6025 : /* For decl_constant_var_p. */
6026 244723 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6027 :
6028 244723 : if (current_function_decl)
6029 : {
6030 244709 : DECL_CONTEXT (decl) = current_function_decl;
6031 244709 : decl = pushdecl_outermost_localscope (decl);
6032 244709 : if (decl != error_mark_node)
6033 244706 : add_decl_expr (decl);
6034 : else
6035 3 : gcc_assert (seen_error ());
6036 : }
6037 : else
6038 : {
6039 14 : DECL_THIS_STATIC (decl) = true;
6040 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
6041 : }
6042 :
6043 244723 : return decl;
6044 : }
6045 :
6046 : /* Install DECL as a builtin function at current global scope. Return
6047 : the new decl (if we found an existing version). Also installs it
6048 : into ::std, if it's not '_*'. */
6049 :
6050 : tree
6051 236289810 : cxx_builtin_function (tree decl)
6052 : {
6053 236289810 : retrofit_lang_decl (decl);
6054 :
6055 236289810 : DECL_ARTIFICIAL (decl) = 1;
6056 236289810 : SET_DECL_LANGUAGE (decl, lang_c);
6057 : /* Runtime library routines are, by definition, available in an
6058 : external shared object. */
6059 236289810 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
6060 236289810 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
6061 :
6062 236289810 : tree id = DECL_NAME (decl);
6063 236289810 : const char *name = IDENTIFIER_POINTER (id);
6064 236289810 : bool hiding = false;
6065 236289810 : if (name[0] != '_' || name[1] != '_')
6066 : /* In the user's namespace, it must be declared before use. */
6067 : hiding = true;
6068 198356062 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
6069 198356062 : && !startswith (name + 2, "builtin_")
6070 221928771 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
6071 : "_chk", strlen ("_chk") + 1))
6072 : /* Treat __*_chk fortification functions as anticipated as well,
6073 : unless they are __builtin_*_chk. */
6074 : hiding = true;
6075 :
6076 : /* All builtins that don't begin with an '_' should additionally
6077 : go in the 'std' namespace. */
6078 236289810 : if (name[0] != '_')
6079 : {
6080 37795331 : tree std_decl = copy_decl (decl);
6081 :
6082 37795331 : push_nested_namespace (std_node);
6083 37795331 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
6084 37795331 : pushdecl (std_decl, hiding);
6085 37795331 : pop_nested_namespace (std_node);
6086 : }
6087 :
6088 236289810 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6089 236289810 : decl = pushdecl (decl, hiding);
6090 :
6091 236289810 : return decl;
6092 : }
6093 :
6094 : /* Like cxx_builtin_function, but guarantee the function is added to the global
6095 : scope. This is to allow function specific options to add new machine
6096 : dependent builtins when the target ISA changes via attribute((target(...)))
6097 : which saves space on program startup if the program does not use non-generic
6098 : ISAs. */
6099 :
6100 : tree
6101 832321 : cxx_builtin_function_ext_scope (tree decl)
6102 : {
6103 832321 : push_nested_namespace (global_namespace);
6104 832321 : decl = cxx_builtin_function (decl);
6105 832321 : pop_nested_namespace (global_namespace);
6106 :
6107 832321 : return decl;
6108 : }
6109 :
6110 : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
6111 :
6112 : tree
6113 0 : cxx_simulate_builtin_function_decl (tree decl)
6114 : {
6115 0 : retrofit_lang_decl (decl);
6116 :
6117 0 : DECL_ARTIFICIAL (decl) = 1;
6118 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
6119 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6120 0 : return pushdecl (decl);
6121 : }
6122 :
6123 : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6124 : function. Not called directly. */
6125 :
6126 : static tree
6127 1639802 : build_library_fn (tree name, enum tree_code operator_code, tree type,
6128 : int ecf_flags)
6129 : {
6130 1639802 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6131 1639802 : DECL_EXTERNAL (fn) = 1;
6132 1639802 : TREE_PUBLIC (fn) = 1;
6133 1639802 : DECL_ARTIFICIAL (fn) = 1;
6134 1639802 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
6135 1639802 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
6136 1639802 : SET_DECL_LANGUAGE (fn, lang_c);
6137 : /* Runtime library routines are, by definition, available in an
6138 : external shared object. */
6139 1639802 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
6140 1639802 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
6141 1639802 : set_call_expr_flags (fn, ecf_flags);
6142 1639802 : return fn;
6143 : }
6144 :
6145 : /* Returns the _DECL for a library function with C++ linkage. */
6146 :
6147 : static tree
6148 1149578 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
6149 : int ecf_flags)
6150 : {
6151 1149578 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
6152 1149578 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6153 1149578 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
6154 1149578 : return fn;
6155 : }
6156 :
6157 : /* Like build_library_fn, but takes a C string instead of an
6158 : IDENTIFIER_NODE. */
6159 :
6160 : tree
6161 106665 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
6162 : {
6163 106665 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
6164 : }
6165 :
6166 : /* Like build_cp_library_fn, but takes a C string instead of an
6167 : IDENTIFIER_NODE. */
6168 :
6169 : tree
6170 100492 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
6171 : {
6172 100492 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
6173 100492 : ecf_flags);
6174 : }
6175 :
6176 : /* Like build_library_fn, but also pushes the function so that we will
6177 : be able to find it via get_global_binding. Also, the function
6178 : may throw exceptions listed in RAISES. */
6179 :
6180 : tree
6181 383559 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6182 : {
6183 383559 : if (raises)
6184 43516 : type = build_exception_variant (type, raises);
6185 :
6186 383559 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
6187 383559 : return pushdecl_top_level (fn);
6188 : }
6189 :
6190 : /* Like build_cp_library_fn, but also pushes the function so that it
6191 : will be found by normal lookup. */
6192 :
6193 : static tree
6194 1049086 : push_cp_library_fn (enum tree_code operator_code, tree type,
6195 : int ecf_flags)
6196 : {
6197 1049086 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
6198 : operator_code, type, ecf_flags);
6199 1049086 : pushdecl (fn);
6200 1049086 : if (flag_tm)
6201 2992 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
6202 1049086 : return fn;
6203 : }
6204 :
6205 : /* Like push_library_fn, but also note that this function throws
6206 : and does not return. Used for __throw_foo and the like. */
6207 :
6208 : tree
6209 111451 : push_throw_library_fn (tree name, tree type)
6210 : {
6211 111451 : tree fn = push_library_fn (name, type, NULL_TREE,
6212 : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6213 111451 : return fn;
6214 : }
6215 :
6216 : /* When we call finish_struct for an anonymous union, we create
6217 : default copy constructors and such. But, an anonymous union
6218 : shouldn't have such things; this function undoes the damage to the
6219 : anonymous union type T.
6220 :
6221 : (The reason that we create the synthesized methods is that we don't
6222 : distinguish `union { int i; }' from `typedef union { int i; } U'.
6223 : The first is an anonymous union; the second is just an ordinary
6224 : union type.) */
6225 :
6226 : void
6227 181836 : fixup_anonymous_aggr (tree t)
6228 : {
6229 : /* Wipe out memory of synthesized methods. */
6230 181836 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6231 181836 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6232 181836 : TYPE_HAS_COPY_CTOR (t) = 0;
6233 181836 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6234 181836 : TYPE_HAS_COPY_ASSIGN (t) = 0;
6235 181836 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6236 :
6237 : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6238 : invalid members. */
6239 1061370 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6240 : {
6241 879534 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6242 0 : *prev_p = DECL_CHAIN (probe);
6243 : else
6244 879534 : prev_p = &DECL_CHAIN (probe);
6245 :
6246 879534 : if (DECL_ARTIFICIAL (probe)
6247 879534 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6248 78730 : || TYPE_ANON_P (TREE_TYPE (probe))))
6249 298667 : continue;
6250 :
6251 580867 : if (TREE_CODE (probe) != FIELD_DECL
6252 580813 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6253 : {
6254 : /* We already complained about static data members in
6255 : finish_static_data_member_decl. */
6256 66 : if (!VAR_P (probe))
6257 : {
6258 51 : auto_diagnostic_group d;
6259 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
6260 51 : TREE_CODE (t) == UNION_TYPE
6261 : ? "%q#D invalid; an anonymous union may "
6262 : "only have public non-static data members"
6263 : : "%q#D invalid; an anonymous struct may "
6264 : "only have public non-static data members", probe))
6265 : {
6266 51 : static bool hint;
6267 51 : if (flag_permissive && !hint)
6268 : {
6269 6 : hint = true;
6270 6 : inform (DECL_SOURCE_LOCATION (probe),
6271 : "this flexibility is deprecated and will be "
6272 : "removed");
6273 : }
6274 : }
6275 51 : }
6276 : }
6277 : }
6278 :
6279 : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6280 181836 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6281 181836 : unsigned store = 0;
6282 536960 : for (tree elt : vec)
6283 355124 : if (!is_overloaded_fn (elt))
6284 355103 : (*vec)[store++] = elt;
6285 181836 : vec_safe_truncate (vec, store);
6286 :
6287 : /* Wipe RTTI info. */
6288 181836 : SET_CLASSTYPE_TYPEINFO_VAR (t, NULL_TREE);
6289 :
6290 : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6291 : assignment operators (because they cannot have these methods themselves).
6292 : For anonymous unions this is already checked because they are not allowed
6293 : in any union, otherwise we have to check it. */
6294 181836 : if (TREE_CODE (t) != UNION_TYPE)
6295 : {
6296 38377 : tree field, type;
6297 :
6298 38377 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6299 : {
6300 9 : error_at (location_of (t), "anonymous struct with base classes");
6301 : /* Avoid ICE after error on anon-struct9.C. */
6302 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
6303 : }
6304 :
6305 154352 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6306 115975 : if (TREE_CODE (field) == FIELD_DECL)
6307 : {
6308 77497 : type = TREE_TYPE (field);
6309 77497 : if (CLASS_TYPE_P (type))
6310 : {
6311 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
6312 6 : error ("member %q+#D with constructor not allowed "
6313 : "in anonymous aggregate", field);
6314 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6315 0 : error ("member %q+#D with destructor not allowed "
6316 : "in anonymous aggregate", field);
6317 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6318 0 : error ("member %q+#D with copy assignment operator "
6319 : "not allowed in anonymous aggregate", field);
6320 : }
6321 : }
6322 : }
6323 181836 : }
6324 :
6325 : /* Warn for an attribute located at LOCATION that appertains to the
6326 : class type CLASS_TYPE that has not been properly placed after its
6327 : class-key, in it class-specifier. */
6328 :
6329 : void
6330 117 : warn_misplaced_attr_for_class_type (location_t location,
6331 : tree class_type)
6332 : {
6333 117 : gcc_assert (OVERLOAD_TYPE_P (class_type));
6334 :
6335 117 : auto_diagnostic_group d;
6336 117 : if (warning_at (location, OPT_Wattributes,
6337 : "attribute ignored in declaration "
6338 : "of %q#T", class_type))
6339 117 : inform (location,
6340 : "attribute for %q#T must follow the %qs keyword",
6341 : class_type, class_key_or_enum_as_string (class_type));
6342 117 : }
6343 :
6344 : /* Returns the cv-qualifiers that apply to the type specified
6345 : by the DECLSPECS. */
6346 :
6347 : static int
6348 1093942693 : get_type_quals (const cp_decl_specifier_seq *declspecs)
6349 : {
6350 1093942693 : int type_quals = TYPE_UNQUALIFIED;
6351 :
6352 1093942693 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6353 113047019 : type_quals |= TYPE_QUAL_CONST;
6354 1093942693 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6355 1824814 : type_quals |= TYPE_QUAL_VOLATILE;
6356 1093942693 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6357 34 : type_quals |= TYPE_QUAL_RESTRICT;
6358 :
6359 1093942693 : return type_quals;
6360 : }
6361 :
6362 : /* Make sure that a declaration with no declarator is well-formed, i.e.
6363 : just declares a tagged type or anonymous union.
6364 :
6365 : Returns the type declared; or NULL_TREE if none. */
6366 :
6367 : tree
6368 39481412 : check_tag_decl (cp_decl_specifier_seq *declspecs,
6369 : bool explicit_type_instantiation_p)
6370 : {
6371 39481412 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6372 39481412 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6373 : /* If a class, struct, or enum type is declared by the DECLSPECS
6374 : (i.e, if a class-specifier, enum-specifier, or non-typename
6375 : elaborated-type-specifier appears in the DECLSPECS),
6376 : DECLARED_TYPE is set to the corresponding type. */
6377 39481412 : tree declared_type = NULL_TREE;
6378 39481412 : bool error_p = false;
6379 :
6380 39481412 : if (declspecs->multiple_types_p)
6381 26 : error_at (smallest_type_location (declspecs),
6382 : "multiple types in one declaration");
6383 39481386 : else if (declspecs->redefined_builtin_type)
6384 : {
6385 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6386 21 : if (!in_system_header_at (loc))
6387 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
6388 : declspecs->redefined_builtin_type);
6389 : return NULL_TREE;
6390 : }
6391 :
6392 39481391 : if (declspecs->type
6393 39481376 : && TYPE_P (declspecs->type)
6394 78578925 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6395 39096480 : && MAYBE_CLASS_TYPE_P (declspecs->type))
6396 1973339 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6397 : declared_type = declspecs->type;
6398 384956 : else if (declspecs->type == error_mark_node)
6399 1496 : error_p = true;
6400 :
6401 39481391 : if (type_uses_auto (declared_type))
6402 : {
6403 8 : error_at (declspecs->locations[ds_type_spec],
6404 : "%<auto%> can only be specified for variables "
6405 : "or function declarations");
6406 8 : return error_mark_node;
6407 : }
6408 :
6409 39481383 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6410 : declared_type = NULL_TREE;
6411 :
6412 39481383 : if (!declared_type && !saw_friend && !error_p)
6413 91 : permerror (input_location, "declaration does not declare anything");
6414 : /* Check for an anonymous union. */
6415 39096400 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6416 113911454 : && TYPE_UNNAMED_P (declared_type))
6417 : {
6418 : /* 7/3 In a simple-declaration, the optional init-declarator-list
6419 : can be omitted only when declaring a class (clause 9) or
6420 : enumeration (7.2), that is, when the decl-specifier-seq contains
6421 : either a class-specifier, an elaborated-type-specifier with
6422 : a class-key (9.1), or an enum-specifier. In these cases and
6423 : whenever a class-specifier or enum-specifier is present in the
6424 : decl-specifier-seq, the identifiers in these specifiers are among
6425 : the names being declared by the declaration (as class-name,
6426 : enum-names, or enumerators, depending on the syntax). In such
6427 : cases, and except for the declaration of an unnamed bit-field (9.6),
6428 : the decl-specifier-seq shall introduce one or more names into the
6429 : program, or shall redeclare a name introduced by a previous
6430 : declaration. [Example:
6431 : enum { }; // ill-formed
6432 : typedef class { }; // ill-formed
6433 : --end example] */
6434 181842 : if (saw_typedef)
6435 : {
6436 6 : error_at (declspecs->locations[ds_typedef],
6437 : "missing type-name in typedef-declaration");
6438 6 : return NULL_TREE;
6439 : }
6440 181836 : /* Anonymous unions are objects, so they can have specifiers. */;
6441 181836 : SET_ANON_AGGR_TYPE_P (declared_type);
6442 :
6443 181836 : if (TREE_CODE (declared_type) != UNION_TYPE)
6444 38377 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6445 38377 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6446 : }
6447 :
6448 : else
6449 : {
6450 39299450 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6451 9 : error_at (declspecs->locations[ds_inline],
6452 : "%<inline%> can only be specified for functions");
6453 39299441 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6454 3 : error_at (declspecs->locations[ds_virtual],
6455 : "%<virtual%> can only be specified for functions");
6456 39299438 : else if (saw_friend
6457 39299438 : && (!current_class_type
6458 2202565 : || current_scope () != current_class_type))
6459 0 : error_at (declspecs->locations[ds_friend],
6460 : "%<friend%> can only be specified inside a class");
6461 39299438 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6462 3 : error_at (declspecs->locations[ds_explicit],
6463 : "%<explicit%> can only be specified for constructors");
6464 39299435 : else if (declspecs->storage_class)
6465 3 : error_at (declspecs->locations[ds_storage_class],
6466 : "a storage class can only be specified for objects "
6467 : "and functions");
6468 39299432 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6469 9 : error_at (declspecs->locations[ds_const],
6470 : "%<const%> can only be specified for objects and "
6471 : "functions");
6472 39299423 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6473 3 : error_at (declspecs->locations[ds_volatile],
6474 : "%<volatile%> can only be specified for objects and "
6475 : "functions");
6476 39299420 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6477 3 : error_at (declspecs->locations[ds_restrict],
6478 : "%<__restrict%> can only be specified for objects and "
6479 : "functions");
6480 39299417 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6481 3 : error_at (declspecs->locations[ds_thread],
6482 : "%<__thread%> can only be specified for objects "
6483 : "and functions");
6484 39299414 : else if (saw_typedef)
6485 24 : warning_at (declspecs->locations[ds_typedef], 0,
6486 : "%<typedef%> was ignored in this declaration");
6487 39299390 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6488 9 : error_at (declspecs->locations[ds_constexpr],
6489 : "%qs cannot be used for type declarations", "constexpr");
6490 39299381 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6491 3 : error_at (declspecs->locations[ds_constinit],
6492 : "%qs cannot be used for type declarations", "constinit");
6493 39299378 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6494 3 : error_at (declspecs->locations[ds_consteval],
6495 : "%qs cannot be used for type declarations", "consteval");
6496 : }
6497 :
6498 39481377 : if (declspecs->attributes && warn_attributes && declared_type)
6499 : {
6500 30 : location_t loc;
6501 24 : if (!CLASS_TYPE_P (declared_type)
6502 54 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6503 : /* For a non-template class, use the name location. */
6504 21 : loc = location_of (declared_type);
6505 : else
6506 : /* For a template class (an explicit instantiation), use the
6507 : current location. */
6508 9 : loc = input_location;
6509 :
6510 30 : if (explicit_type_instantiation_p)
6511 : /* [dcl.attr.grammar]/4:
6512 :
6513 : No attribute-specifier-seq shall appertain to an explicit
6514 : instantiation. */
6515 : {
6516 6 : auto_diagnostic_group d;
6517 6 : if (warning_at (loc, OPT_Wattributes,
6518 : "attribute ignored in explicit instantiation %q#T",
6519 : declared_type))
6520 6 : inform (loc,
6521 : "no attribute can be applied to "
6522 : "an explicit instantiation");
6523 6 : }
6524 : else
6525 24 : warn_misplaced_attr_for_class_type (loc, declared_type);
6526 : }
6527 :
6528 39481377 : if (declspecs->std_attributes
6529 93 : && declared_type
6530 39481467 : && any_nonignored_attribute_p (declspecs->std_attributes))
6531 : {
6532 90 : auto_diagnostic_group d;
6533 90 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6534 : "attribute ignored"))
6535 90 : inform (declspecs->locations[ds_std_attribute],
6536 : "an attribute that appertains to a type-specifier is ignored");
6537 90 : }
6538 :
6539 : return declared_type;
6540 : }
6541 :
6542 : /* Called when a declaration is seen that contains no names to declare.
6543 : If its type is a reference to a structure, union or enum inherited
6544 : from a containing scope, shadow that tag name for the current scope
6545 : with a forward reference.
6546 : If its type defines a new named structure or union
6547 : or defines an enum, it is valid but we need not do anything here.
6548 : Otherwise, it is an error.
6549 :
6550 : C++: may have to grok the declspecs to learn about static,
6551 : complain for anonymous unions.
6552 :
6553 : Returns the TYPE declared -- or NULL_TREE if none. */
6554 :
6555 : tree
6556 34601735 : shadow_tag (cp_decl_specifier_seq *declspecs)
6557 : {
6558 34601735 : tree t = check_tag_decl (declspecs,
6559 : /*explicit_type_instantiation_p=*/false);
6560 :
6561 34601735 : if (!t)
6562 : return NULL_TREE;
6563 :
6564 34600223 : t = maybe_process_partial_specialization (t);
6565 34600223 : if (t == error_mark_node)
6566 : return NULL_TREE;
6567 :
6568 : /* This is where the variables in an anonymous union are
6569 : declared. An anonymous union declaration looks like:
6570 : union { ... } ;
6571 : because there is no declarator after the union, the parser
6572 : sends that declaration here. */
6573 34598312 : if (ANON_AGGR_TYPE_P (t))
6574 : {
6575 392 : fixup_anonymous_aggr (t);
6576 :
6577 392 : if (TYPE_FIELDS (t))
6578 : {
6579 392 : tree decl = grokdeclarator (/*declarator=*/NULL,
6580 : declspecs, NORMAL, 0, NULL);
6581 392 : finish_anon_union (decl);
6582 : }
6583 : }
6584 :
6585 : return t;
6586 : }
6587 :
6588 : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6589 :
6590 : tree
6591 427706633 : groktypename (cp_decl_specifier_seq *type_specifiers,
6592 : const cp_declarator *declarator,
6593 : bool is_template_arg)
6594 : {
6595 427706633 : tree attrs;
6596 427706633 : tree type;
6597 70914314 : enum decl_context context
6598 427706633 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6599 427706633 : attrs = type_specifiers->attributes;
6600 427706633 : type_specifiers->attributes = NULL_TREE;
6601 427706633 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6602 427706633 : if (attrs && type != error_mark_node)
6603 : {
6604 673 : if (CLASS_TYPE_P (type))
6605 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6606 : "outside of definition", type);
6607 667 : else if (MAYBE_CLASS_TYPE_P (type))
6608 : /* A template type parameter or other dependent type. */
6609 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6610 : "type %qT without an associated declaration", type);
6611 : else
6612 658 : cplus_decl_attributes (&type, attrs, 0);
6613 : }
6614 427706633 : return type;
6615 : }
6616 :
6617 : /* Process a DECLARATOR for a function-scope or namespace-scope
6618 : variable or function declaration.
6619 : (Function definitions go through start_function; class member
6620 : declarations appearing in the body of the class go through
6621 : grokfield.) The DECL corresponding to the DECLARATOR is returned.
6622 : If an error occurs, the error_mark_node is returned instead.
6623 :
6624 : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6625 : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6626 : for an explicitly defaulted function, or SD_DELETED for an explicitly
6627 : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6628 : implicitly initialized via a default constructor. It can also be
6629 : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6630 : mark the new decl as DECL_DECOMPOSITION_P.
6631 :
6632 : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6633 : declaration.
6634 :
6635 : The scope represented by the context of the returned DECL is pushed
6636 : (if it is not the global namespace) and is assigned to
6637 : *PUSHED_SCOPE_P. The caller is then responsible for calling
6638 : pop_scope on *PUSHED_SCOPE_P if it is set. */
6639 :
6640 : tree
6641 134301777 : start_decl (const cp_declarator *declarator,
6642 : cp_decl_specifier_seq *declspecs,
6643 : int initialized,
6644 : tree attributes,
6645 : tree prefix_attributes,
6646 : tree *pushed_scope_p)
6647 : {
6648 134301777 : tree decl;
6649 134301777 : tree context;
6650 134301777 : bool was_public;
6651 134301777 : int flags;
6652 134301777 : bool alias;
6653 134301777 : tree initial;
6654 :
6655 134301777 : *pushed_scope_p = NULL_TREE;
6656 :
6657 134301777 : if (prefix_attributes != error_mark_node)
6658 134301775 : attributes = attr_chainon (attributes, prefix_attributes);
6659 :
6660 134301777 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6661 : &attributes);
6662 :
6663 134301777 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6664 134301777 : || decl == error_mark_node
6665 134299622 : || prefix_attributes == error_mark_node)
6666 2157 : return error_mark_node;
6667 :
6668 134299620 : context = CP_DECL_CONTEXT (decl);
6669 134299620 : if (context != global_namespace)
6670 23564563 : *pushed_scope_p = push_scope (context);
6671 :
6672 134299620 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6673 : {
6674 27 : error_at (DECL_SOURCE_LOCATION (decl),
6675 : "typedef %qD is initialized (use %qs instead)",
6676 : decl, "decltype");
6677 27 : return error_mark_node;
6678 : }
6679 :
6680 134299593 : if (flag_contracts
6681 14777624 : && TREE_CODE (decl) == FUNCTION_DECL
6682 4146896 : && !processing_template_decl
6683 3356270 : && DECL_RESULT (decl)
6684 137649367 : && is_auto (TREE_TYPE (DECL_RESULT (decl))))
6685 36 : if (tree specs = get_fn_contract_specifiers (decl))
6686 12 : for (tree ca : tree_vec_range (specs))
6687 9 : if (POSTCONDITION_P (ca) && POSTCONDITION_IDENTIFIER (ca))
6688 : {
6689 3 : error_at (DECL_SOURCE_LOCATION (decl),
6690 : "postconditions with deduced result name types must only"
6691 : " appear on function definitions");
6692 3 : return error_mark_node;
6693 : }
6694 : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6695 : with attribute validation. */
6696 134299590 : initial = DECL_INITIAL (decl);
6697 :
6698 134299590 : if (initialized)
6699 : {
6700 65152855 : if (! toplevel_bindings_p ()
6701 65152855 : && DECL_EXTERNAL (decl))
6702 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6703 : decl);
6704 65152855 : DECL_EXTERNAL (decl) = 0;
6705 65152855 : if (toplevel_bindings_p ())
6706 7832917 : TREE_STATIC (decl) = 1;
6707 : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6708 : even though we might not yet have the initializer expression. */
6709 65152855 : if (!DECL_INITIAL (decl))
6710 64552912 : DECL_INITIAL (decl) = error_mark_node;
6711 : }
6712 134299590 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6713 :
6714 134299590 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6715 3984 : record_key_method_defined (decl);
6716 :
6717 : /* If this is a typedef that names the class for linkage purposes
6718 : (7.1.3p8), apply any attributes directly to the type. */
6719 134299590 : if (TREE_CODE (decl) == TYPE_DECL
6720 134299590 : && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl))
6721 : flags = ATTR_FLAG_TYPE_IN_PLACE;
6722 : else
6723 : flags = 0;
6724 :
6725 : /* Set attributes here so if duplicate decl, will have proper attributes. */
6726 134299590 : cplus_decl_attributes (&decl, attributes, flags);
6727 :
6728 : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6729 : assist with attribute validation. */
6730 134299590 : DECL_INITIAL (decl) = initial;
6731 :
6732 : /* Dllimported symbols cannot be defined. Static data members (which
6733 : can be initialized in-class and dllimported) go through grokfield,
6734 : not here, so we don't need to exclude those decls when checking for
6735 : a definition. */
6736 134299590 : if (initialized && DECL_DLLIMPORT_P (decl))
6737 : {
6738 0 : error_at (DECL_SOURCE_LOCATION (decl),
6739 : "definition of %q#D is marked %<dllimport%>", decl);
6740 0 : DECL_DLLIMPORT_P (decl) = 0;
6741 : }
6742 :
6743 : /* If #pragma weak was used, mark the decl weak now. */
6744 134299590 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6745 49017949 : maybe_apply_pragma_weak (decl);
6746 :
6747 134299590 : if (TREE_CODE (decl) == FUNCTION_DECL
6748 39740811 : && DECL_DECLARED_INLINE_P (decl)
6749 3589350 : && DECL_UNINLINABLE (decl)
6750 134299590 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6751 : {
6752 0 : auto_urlify_attributes sentinel;
6753 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6754 : "inline function %qD given attribute %qs", decl, "noinline");
6755 0 : }
6756 :
6757 134299590 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6758 : {
6759 894086 : bool this_tmpl = (current_template_depth
6760 894086 : > template_class_depth (context));
6761 894086 : if (VAR_P (decl))
6762 : {
6763 523845 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6764 523845 : if (field == NULL_TREE
6765 523845 : || !(VAR_P (field) || variable_template_p (field)))
6766 6 : error ("%q+#D is not a static data member of %q#T", decl, context);
6767 523839 : else if (variable_template_p (field)
6768 523839 : && (DECL_LANG_SPECIFIC (decl)
6769 149 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6770 : /* OK, specialization was already checked. */;
6771 523768 : else if (variable_template_p (field) && !this_tmpl)
6772 : {
6773 3 : auto_diagnostic_group d;
6774 3 : error_at (DECL_SOURCE_LOCATION (decl),
6775 : "non-member-template declaration of %qD", decl);
6776 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6777 : "member template declaration here");
6778 3 : return error_mark_node;
6779 3 : }
6780 : else
6781 : {
6782 523765 : if (variable_template_p (field))
6783 75 : field = DECL_TEMPLATE_RESULT (field);
6784 :
6785 523765 : if (DECL_CONTEXT (field) != context)
6786 : {
6787 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6788 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6789 : "to be defined as %<%T::%D%>",
6790 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6791 3 : context, DECL_NAME (decl));
6792 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6793 : }
6794 :
6795 523765 : if (modules_p () && !module_may_redeclare (field))
6796 30 : return error_mark_node;
6797 :
6798 : /* Static data member are tricky; an in-class initialization
6799 : still doesn't provide a definition, so the in-class
6800 : declaration will have DECL_EXTERNAL set, but will have an
6801 : initialization. Thus, duplicate_decls won't warn
6802 : about this situation, and so we check here. */
6803 523735 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6804 9 : error ("duplicate initialization of %qD", decl);
6805 523735 : field = duplicate_decls (decl, field);
6806 523735 : if (field == error_mark_node)
6807 : return error_mark_node;
6808 523679 : else if (field)
6809 523679 : decl = field;
6810 : }
6811 : }
6812 : else
6813 : {
6814 370253 : tree field = check_classfn (context, decl,
6815 : this_tmpl
6816 12 : ? current_template_parms
6817 : : NULL_TREE);
6818 370166 : if (field && field != error_mark_node
6819 740407 : && duplicate_decls (decl, field))
6820 370166 : decl = field;
6821 : }
6822 :
6823 : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6824 893997 : DECL_IN_AGGR_P (decl) = 0;
6825 : /* Do not mark DECL as an explicit specialization if it was not
6826 : already marked as an instantiation; a declaration should
6827 : never be marked as a specialization unless we know what
6828 : template is being specialized. */
6829 893997 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6830 : {
6831 368063 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6832 368063 : if (TREE_CODE (decl) == FUNCTION_DECL)
6833 346884 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6834 346884 : && DECL_NONGNU_INLINE_P (decl));
6835 : else
6836 21179 : DECL_COMDAT (decl) = false;
6837 :
6838 : /* [temp.expl.spec] An explicit specialization of a static data
6839 : member of a template is a definition if the declaration
6840 : includes an initializer; otherwise, it is a declaration.
6841 :
6842 : We check for processing_specialization so this only applies
6843 : to the new specialization syntax. */
6844 368063 : if (!initialized && processing_specialization)
6845 367859 : DECL_EXTERNAL (decl) = 1;
6846 : }
6847 :
6848 1261904 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6849 : /* Aliases are definitions. */
6850 894033 : && !alias)
6851 : {
6852 9 : permerror (declarator->id_loc,
6853 : "declaration of %q#D outside of class is not definition",
6854 : decl);
6855 : }
6856 : }
6857 :
6858 : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6859 134299501 : if (initialized == SD_DECOMPOSITION)
6860 496695 : fit_decomposition_lang_decl (decl, NULL_TREE);
6861 :
6862 134299501 : was_public = TREE_PUBLIC (decl);
6863 :
6864 228869561 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6865 134899320 : && current_function_decl)
6866 : {
6867 : /* A function-scope decl of some namespace-scope decl. */
6868 63825 : DECL_LOCAL_DECL_P (decl) = true;
6869 63825 : if (named_module_attach_p ())
6870 3 : error_at (declarator->id_loc,
6871 : "block-scope extern declaration %q#D must not be"
6872 : " attached to a named module", decl);
6873 : }
6874 :
6875 : /* Enter this declaration into the symbol table. Don't push the plain
6876 : VAR_DECL for a variable template. */
6877 134299501 : if (!template_parm_scope_p ()
6878 134299501 : || !VAR_P (decl))
6879 129868392 : decl = maybe_push_decl (decl);
6880 :
6881 134299501 : if (processing_template_decl)
6882 85273438 : decl = push_template_decl (decl);
6883 :
6884 134299501 : if (decl == error_mark_node)
6885 : return error_mark_node;
6886 :
6887 134299055 : if (VAR_P (decl)
6888 71560155 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6889 70669 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6890 : /* But not templated variables. */
6891 134330518 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6892 : {
6893 : /* This is a const variable with implicit 'static'. Set
6894 : DECL_THIS_STATIC so we can tell it from variables that are
6895 : !TREE_PUBLIC because of the anonymous namespace. */
6896 31463 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6897 31463 : DECL_THIS_STATIC (decl) = 1;
6898 : }
6899 :
6900 75812511 : if (current_function_decl && VAR_P (decl)
6901 62898243 : && maybe_constexpr_fn (current_function_decl)
6902 150810322 : && cxx_dialect < cxx23)
6903 : {
6904 12986551 : bool ok = false;
6905 12986551 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6906 : {
6907 9 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6908 18 : error_at (DECL_SOURCE_LOCATION (decl),
6909 : "%qD defined %<thread_local%> in %qs function only "
6910 : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6911 9 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6912 : ? "consteval" : "constexpr");
6913 : }
6914 12986542 : else if (TREE_STATIC (decl))
6915 : {
6916 50 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6917 72 : error_at (DECL_SOURCE_LOCATION (decl),
6918 : "%qD defined %<static%> in %qs function only available "
6919 : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6920 36 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6921 : ? "consteval" : "constexpr");
6922 : }
6923 : else
6924 : ok = true;
6925 45 : if (!ok)
6926 59 : cp_function_chain->invalid_constexpr = true;
6927 : }
6928 :
6929 134299055 : if (!processing_template_decl && VAR_P (decl))
6930 8950158 : start_decl_1 (decl, initialized);
6931 :
6932 134299052 : return decl;
6933 : }
6934 :
6935 : /* Process the declaration of a variable DECL. INITIALIZED is true
6936 : iff DECL is explicitly initialized. (INITIALIZED is false if the
6937 : variable is initialized via an implicitly-called constructor.)
6938 : This function must be called for ordinary variables (including, for
6939 : example, implicit instantiations of templates), but must not be
6940 : called for template declarations. */
6941 :
6942 : void
6943 8960215 : start_decl_1 (tree decl, bool initialized)
6944 : {
6945 8960215 : gcc_checking_assert (!processing_template_decl);
6946 :
6947 8960215 : if (error_operand_p (decl))
6948 : return;
6949 :
6950 8960200 : gcc_checking_assert (VAR_P (decl));
6951 :
6952 8960200 : tree type = TREE_TYPE (decl);
6953 8960200 : bool complete_p = COMPLETE_TYPE_P (type);
6954 8960200 : bool aggregate_definition_p
6955 8960200 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6956 :
6957 : /* If an explicit initializer is present, or if this is a definition
6958 : of an aggregate, then we need a complete type at this point.
6959 : (Scalars are always complete types, so there is nothing to
6960 : check.) This code just sets COMPLETE_P; errors (if necessary)
6961 : are issued below. */
6962 4648630 : if ((initialized || aggregate_definition_p)
6963 7904817 : && !complete_p
6964 6328248 : && COMPLETE_TYPE_P (complete_type (type)))
6965 : {
6966 343510 : complete_p = true;
6967 : /* We will not yet have set TREE_READONLY on DECL if the type
6968 : was "const", but incomplete, before this point. But, now, we
6969 : have a complete type, so we can try again. */
6970 343510 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6971 : }
6972 :
6973 8960197 : if (initialized)
6974 : /* Is it valid for this decl to have an initializer at all? */
6975 : {
6976 : /* Don't allow initializations for incomplete types except for
6977 : arrays which might be completed by the initialization. */
6978 7125207 : if (complete_p)
6979 : ; /* A complete type is ok. */
6980 1335867 : else if (type_uses_auto (type))
6981 : ; /* An auto type is ok. */
6982 123848 : else if (TREE_CODE (type) != ARRAY_TYPE)
6983 : {
6984 27 : auto_diagnostic_group d;
6985 27 : error ("variable %q#D has initializer but incomplete type", decl);
6986 48 : maybe_suggest_missing_header (input_location,
6987 27 : TYPE_IDENTIFIER (type),
6988 27 : CP_TYPE_CONTEXT (type));
6989 27 : type = TREE_TYPE (decl) = error_mark_node;
6990 27 : }
6991 123821 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6992 : {
6993 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6994 0 : error ("elements of array %q#D have incomplete type", decl);
6995 : /* else we already gave an error in start_decl. */
6996 : }
6997 : }
6998 1834990 : else if (aggregate_definition_p && !complete_p)
6999 : {
7000 238 : if (type_uses_auto (type))
7001 71 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
7002 : else
7003 : {
7004 167 : auto_diagnostic_group d;
7005 167 : error ("aggregate %q#D has incomplete type and cannot be defined",
7006 : decl);
7007 289 : maybe_suggest_missing_header (input_location,
7008 167 : TYPE_IDENTIFIER (type),
7009 167 : CP_TYPE_CONTEXT (type));
7010 : /* Change the type so that assemble_variable will give
7011 : DECL an rtl we can live with: (mem (const_int 0)). */
7012 167 : type = TREE_TYPE (decl) = error_mark_node;
7013 167 : }
7014 : }
7015 :
7016 : /* Create a new scope to hold this declaration if necessary.
7017 : Whether or not a new scope is necessary cannot be determined
7018 : until after the type has been completed; if the type is a
7019 : specialization of a class template it is not until after
7020 : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
7021 : will be set correctly. */
7022 8960197 : maybe_push_cleanup_level (type);
7023 : }
7024 :
7025 : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
7026 : C++20 P0960. TYPE is the type of the object we're initializing. */
7027 :
7028 : tree
7029 309 : do_aggregate_paren_init (tree init, tree type)
7030 : {
7031 309 : tree val = TREE_VALUE (init);
7032 :
7033 309 : if (TREE_CHAIN (init) == NULL_TREE)
7034 : {
7035 : /* If the list has a single element and it's a string literal,
7036 : then it's the initializer for the array as a whole. */
7037 193 : if (TREE_CODE (type) == ARRAY_TYPE
7038 193 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7039 239 : && TREE_CODE (tree_strip_any_location_wrapper (val))
7040 : == STRING_CST)
7041 : return val;
7042 : /* Handle non-standard extensions like compound literals. This also
7043 : prevents triggering aggregate parenthesized-initialization in
7044 : compiler-generated code for =default. */
7045 154 : else if (same_type_ignoring_top_level_qualifiers_p (type,
7046 154 : TREE_TYPE (val)))
7047 : return val;
7048 : }
7049 :
7050 208 : init = build_constructor_from_list (init_list_type_node, init);
7051 208 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
7052 208 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
7053 208 : return init;
7054 : }
7055 :
7056 : /* Handle initialization of references. DECL, TYPE, and INIT have the
7057 : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
7058 : but will be set to a new CLEANUP_STMT if a temporary is created
7059 : that must be destroyed subsequently.
7060 :
7061 : Returns an initializer expression to use to initialize DECL, or
7062 : NULL if the initialization can be performed statically.
7063 :
7064 : Quotes on semantics can be found in ARM 8.4.3. */
7065 :
7066 : static tree
7067 388318 : grok_reference_init (tree decl, tree type, tree init, int flags)
7068 : {
7069 388318 : if (init == NULL_TREE)
7070 : {
7071 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
7072 3 : || DECL_IN_AGGR_P (decl) == 0)
7073 18 : && ! DECL_THIS_EXTERN (decl))
7074 15 : error_at (DECL_SOURCE_LOCATION (decl),
7075 : "%qD declared as reference but not initialized", decl);
7076 : return NULL_TREE;
7077 : }
7078 :
7079 388303 : tree ttype = TREE_TYPE (type);
7080 388303 : if (TREE_CODE (init) == TREE_LIST)
7081 : {
7082 : /* This handles (C++20 only) code like
7083 :
7084 : const A& r(1, 2, 3);
7085 :
7086 : where we treat the parenthesized list as a CONSTRUCTOR. */
7087 1262 : if (TREE_TYPE (init) == NULL_TREE
7088 1262 : && CP_AGGREGATE_TYPE_P (ttype)
7089 102 : && !DECL_DECOMPOSITION_P (decl)
7090 1361 : && (cxx_dialect >= cxx20))
7091 : {
7092 : /* We don't know yet if we should treat const A& r(1) as
7093 : const A& r{1}. */
7094 93 : if (list_length (init) == 1)
7095 : {
7096 72 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
7097 72 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7098 : tf_warning_or_error);
7099 : }
7100 : /* If the list had more than one element, the code is ill-formed
7101 : pre-C++20, so we can build a constructor right away. */
7102 : else
7103 21 : init = do_aggregate_paren_init (init, ttype);
7104 : }
7105 : else
7106 1169 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7107 : tf_warning_or_error);
7108 : }
7109 :
7110 388303 : if (TREE_CODE (ttype) != ARRAY_TYPE
7111 388303 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7112 : /* Note: default conversion is only called in very special cases. */
7113 12 : init = decay_conversion (init, tf_warning_or_error);
7114 :
7115 : /* check_initializer handles this for non-reference variables, but for
7116 : references we need to do it here or the initializer will get the
7117 : incomplete array type and confuse later calls to
7118 : cp_complete_array_type. */
7119 388303 : if (TREE_CODE (ttype) == ARRAY_TYPE
7120 22714 : && TYPE_DOMAIN (ttype) == NULL_TREE
7121 388386 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
7122 50 : || TREE_CODE (init) == STRING_CST))
7123 : {
7124 33 : cp_complete_array_type (&ttype, init, false);
7125 33 : if (ttype != TREE_TYPE (type))
7126 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
7127 : }
7128 :
7129 : /* Convert INIT to the reference type TYPE. This may involve the
7130 : creation of a temporary, whose lifetime must be the same as that
7131 : of the reference. If so, a DECL_EXPR for the temporary will be
7132 : added just after the DECL_EXPR for DECL. That's why we don't set
7133 : DECL_INITIAL for local references (instead assigning to them
7134 : explicitly); we need to allow the temporary to be initialized
7135 : first. */
7136 388303 : return initialize_reference (type, init, flags,
7137 388303 : tf_warning_or_error);
7138 : }
7139 :
7140 : /* Designated initializers in arrays are not supported in GNU C++.
7141 : The parser cannot detect this error since it does not know whether
7142 : a given brace-enclosed initializer is for a class type or for an
7143 : array. This function checks that CE does not use a designated
7144 : initializer. If it does, an error is issued. Returns true if CE
7145 : is valid, i.e., does not have a designated initializer. */
7146 :
7147 : bool
7148 157571919 : check_array_designated_initializer (constructor_elt *ce,
7149 : unsigned HOST_WIDE_INT index)
7150 : {
7151 : /* Designated initializers for array elements are not supported. */
7152 157571919 : if (ce->index)
7153 : {
7154 : /* The parser only allows identifiers as designated
7155 : initializers. */
7156 106373151 : if (ce->index == error_mark_node)
7157 : {
7158 0 : error ("name used in a GNU-style designated "
7159 : "initializer for an array");
7160 0 : return false;
7161 : }
7162 106373151 : else if (identifier_p (ce->index))
7163 : {
7164 19 : error ("name %qD used in a GNU-style designated "
7165 : "initializer for an array", ce->index);
7166 19 : ce->index = error_mark_node;
7167 19 : return false;
7168 : }
7169 :
7170 106373132 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7171 : ce->index, true);
7172 106373132 : if (ce_index
7173 106373129 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
7174 212746258 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
7175 : == INTEGER_CST))
7176 : {
7177 : /* A C99 designator is OK if it matches the current index. */
7178 106373126 : if (wi::to_wide (ce_index) == index)
7179 : {
7180 106373120 : ce->index = ce_index;
7181 106373120 : return true;
7182 : }
7183 : else
7184 6 : sorry ("non-trivial designated initializers not supported");
7185 : }
7186 : else
7187 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
7188 : "C99 designator %qE is not an integral constant-expression",
7189 : ce->index);
7190 :
7191 : return false;
7192 : }
7193 :
7194 : return true;
7195 : }
7196 :
7197 : /* When parsing `int a[] = {1, 2};' we don't know the size of the
7198 : array until we finish parsing the initializer. If that's the
7199 : situation we're in, update DECL accordingly. */
7200 :
7201 : static void
7202 77760734 : maybe_deduce_size_from_array_init (tree decl, tree init)
7203 : {
7204 77760734 : tree type = TREE_TYPE (decl);
7205 :
7206 77760734 : if (TREE_CODE (type) == ARRAY_TYPE
7207 996977 : && TYPE_DOMAIN (type) == NULL_TREE
7208 78392300 : && TREE_CODE (decl) != TYPE_DECL)
7209 : {
7210 : /* do_default is really a C-ism to deal with tentative definitions.
7211 : But let's leave it here to ease the eventual merge. */
7212 631566 : int do_default = !DECL_EXTERNAL (decl);
7213 631566 : tree initializer = init ? init : DECL_INITIAL (decl);
7214 631566 : int failure = 0;
7215 :
7216 : /* Check that there are no designated initializers in INIT, as
7217 : those are not supported in GNU C++, and as the middle-end
7218 : will crash if presented with a non-numeric designated
7219 : initializer. */
7220 631566 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7221 : {
7222 150273 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7223 150273 : constructor_elt *ce;
7224 150273 : HOST_WIDE_INT i, j = 0;
7225 49441271 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7226 : {
7227 49292038 : if (instantiation_dependent_expression_p (ce->index))
7228 77760734 : return;
7229 49292032 : if (!check_array_designated_initializer (ce, j))
7230 1 : failure = 1;
7231 : /* If an un-designated initializer is type-dependent, we can't
7232 : check brace elision yet. */
7233 49292032 : if (ce->index == NULL_TREE
7234 49292032 : && type_dependent_expression_p (ce->value))
7235 : return;
7236 49290998 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7237 210 : j += RAW_DATA_LENGTH (ce->value);
7238 : else
7239 49290788 : ++j;
7240 : }
7241 : }
7242 :
7243 149233 : if (failure)
7244 1 : TREE_TYPE (decl) = error_mark_node;
7245 : else
7246 : {
7247 630525 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7248 : do_default);
7249 630525 : if (failure == 1)
7250 : {
7251 33 : error_at (cp_expr_loc_or_loc (initializer,
7252 30 : DECL_SOURCE_LOCATION (decl)),
7253 : "initializer fails to determine size of %qD", decl);
7254 : }
7255 630495 : else if (failure == 2)
7256 : {
7257 30 : if (do_default)
7258 : {
7259 6 : error_at (DECL_SOURCE_LOCATION (decl),
7260 : "array size missing in %qD", decl);
7261 : }
7262 : /* If a `static' var's size isn't known, make it extern as
7263 : well as static, so it does not get allocated. If it's not
7264 : `static', then don't mark it extern; finish_incomplete_decl
7265 : will give it a default size and it will get allocated. */
7266 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7267 0 : DECL_EXTERNAL (decl) = 1;
7268 : }
7269 630465 : else if (failure == 3)
7270 : {
7271 6 : error_at (DECL_SOURCE_LOCATION (decl),
7272 : "zero-size array %qD", decl);
7273 : }
7274 : }
7275 :
7276 630526 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7277 :
7278 630526 : relayout_decl (decl);
7279 :
7280 : /* Update the type of the corresponding TEMPLATE_DECL to match. */
7281 630526 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7282 : {
7283 212 : tree tmpl = template_for_substitution (decl);
7284 212 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
7285 50 : TREE_TYPE (tmpl) = TREE_TYPE (decl);
7286 : }
7287 : }
7288 : }
7289 :
7290 : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7291 : any appropriate error messages regarding the layout. */
7292 :
7293 : static void
7294 66428458 : layout_var_decl (tree decl)
7295 : {
7296 66428458 : tree type;
7297 :
7298 66428458 : type = TREE_TYPE (decl);
7299 66428458 : if (type == error_mark_node)
7300 : return;
7301 :
7302 : /* If we haven't already laid out this declaration, do so now.
7303 : Note that we must not call complete type for an external object
7304 : because it's type might involve templates that we are not
7305 : supposed to instantiate yet. (And it's perfectly valid to say
7306 : `extern X x' for some incomplete type `X'.) */
7307 66428411 : if (!DECL_EXTERNAL (decl))
7308 55726392 : complete_type (type);
7309 66428411 : if (!DECL_SIZE (decl)
7310 638901 : && TREE_TYPE (decl) != error_mark_node
7311 67067312 : && complete_or_array_type_p (type))
7312 480426 : layout_decl (decl, 0);
7313 :
7314 66428411 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7315 : {
7316 : /* An automatic variable with an incomplete type: that is an error.
7317 : Don't talk about array types here, since we took care of that
7318 : message in grokdeclarator. */
7319 17 : error_at (DECL_SOURCE_LOCATION (decl),
7320 : "storage size of %qD isn%'t known", decl);
7321 17 : TREE_TYPE (decl) = error_mark_node;
7322 : }
7323 : #if 0
7324 : /* Keep this code around in case we later want to control debug info
7325 : based on whether a type is "used". (jason 1999-11-11) */
7326 :
7327 : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7328 : /* Let debugger know it should output info for this type. */
7329 : note_debug_info_needed (ttype);
7330 :
7331 : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7332 : note_debug_info_needed (DECL_CONTEXT (decl));
7333 : #endif
7334 :
7335 122154803 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7336 41001501 : && DECL_SIZE (decl) != NULL_TREE
7337 107134975 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
7338 : {
7339 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7340 6 : && !DECL_LOCAL_DECL_P (decl))
7341 0 : constant_expression_warning (DECL_SIZE (decl));
7342 : else
7343 : {
7344 6 : error_at (DECL_SOURCE_LOCATION (decl),
7345 : "storage size of %qD isn%'t constant", decl);
7346 6 : TREE_TYPE (decl) = error_mark_node;
7347 6 : type = error_mark_node;
7348 : }
7349 : }
7350 :
7351 : /* If the final element initializes a flexible array field, adjust
7352 : the size of the DECL with the initializer based on whether the
7353 : DECL is a union or a structure. */
7354 66428411 : if (type != error_mark_node
7355 66428405 : && DECL_INITIAL (decl)
7356 39366865 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7357 5407124 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7358 3635630 : && DECL_SIZE (decl) != NULL_TREE
7359 3635630 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7360 3635630 : && COMPLETE_TYPE_P (type)
7361 3635630 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7362 70064041 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7363 : {
7364 3635630 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7365 3635630 : if (elt.index)
7366 : {
7367 1363677 : tree itype = TREE_TYPE (elt.index);
7368 1363677 : tree vtype = TREE_TYPE (elt.value);
7369 1363677 : if (TREE_CODE (itype) == ARRAY_TYPE
7370 86853 : && TYPE_DOMAIN (itype) == NULL
7371 275 : && TREE_CODE (vtype) == ARRAY_TYPE
7372 1363952 : && COMPLETE_TYPE_P (vtype))
7373 : {
7374 : /* For a structure, add the size of the initializer to the DECL's
7375 : size. */
7376 275 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7377 : {
7378 260 : DECL_SIZE (decl)
7379 260 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7380 : TYPE_SIZE (vtype));
7381 260 : DECL_SIZE_UNIT (decl)
7382 520 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7383 : TYPE_SIZE_UNIT (vtype));
7384 : }
7385 : /* For a union, the DECL's size is the maximum of the current size
7386 : and the size of the initializer. */
7387 : else
7388 : {
7389 15 : DECL_SIZE (decl)
7390 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
7391 : TYPE_SIZE (vtype));
7392 15 : DECL_SIZE_UNIT (decl)
7393 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7394 : TYPE_SIZE_UNIT (vtype));
7395 : }
7396 : }
7397 : }
7398 : }
7399 : }
7400 :
7401 : /* If a local static variable is declared in an inline function, or if
7402 : we have a weak definition, we must endeavor to create only one
7403 : instance of the variable at link-time. */
7404 :
7405 : void
7406 81796748 : maybe_commonize_var (tree decl)
7407 : {
7408 : /* Don't mess with __FUNCTION__ and similar. But do handle structured
7409 : bindings. */
7410 81796748 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7411 : return;
7412 :
7413 : /* Static data in a function with comdat linkage also has comdat
7414 : linkage. */
7415 76355663 : if ((TREE_STATIC (decl)
7416 50650962 : && DECL_FUNCTION_SCOPE_P (decl)
7417 142643 : && vague_linkage_p (DECL_CONTEXT (decl)))
7418 177840943 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7419 : {
7420 44386229 : if (flag_weak)
7421 : {
7422 : /* With weak symbols, we simply make the variable COMDAT;
7423 : that will cause copies in multiple translations units to
7424 : be merged. */
7425 44386202 : comdat_linkage (decl);
7426 : }
7427 : else
7428 : {
7429 27 : if (DECL_INITIAL (decl) == NULL_TREE
7430 27 : || DECL_INITIAL (decl) == error_mark_node)
7431 : {
7432 : /* Without weak symbols, we can use COMMON to merge
7433 : uninitialized variables. */
7434 24 : TREE_PUBLIC (decl) = 1;
7435 24 : DECL_COMMON (decl) = 1;
7436 : }
7437 : else
7438 : {
7439 : /* While for initialized variables, we must use internal
7440 : linkage -- which means that multiple copies will not
7441 : be merged. */
7442 3 : TREE_PUBLIC (decl) = 0;
7443 3 : DECL_COMMON (decl) = 0;
7444 3 : DECL_INTERFACE_KNOWN (decl) = 1;
7445 3 : const char *msg;
7446 3 : if (DECL_INLINE_VAR_P (decl))
7447 : msg = G_("sorry: semantics of inline variable "
7448 : "%q#D are wrong (you%'ll wind up with "
7449 : "multiple copies)");
7450 : else
7451 : msg = G_("sorry: semantics of inline function "
7452 : "static data %q#D are wrong (you%'ll wind "
7453 : "up with multiple copies)");
7454 3 : auto_diagnostic_group d;
7455 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7456 : msg, decl))
7457 3 : inform (DECL_SOURCE_LOCATION (decl),
7458 : "you can work around this by removing the initializer");
7459 3 : }
7460 : }
7461 : }
7462 : }
7463 :
7464 : /* Issue an error message if DECL is an uninitialized const variable.
7465 : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7466 : context from potential_constant_expression. Returns true if all is well,
7467 : false otherwise. */
7468 :
7469 : bool
7470 9441346 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7471 : tsubst_flags_t complain)
7472 : {
7473 9441346 : tree type = strip_array_types (TREE_TYPE (decl));
7474 :
7475 : /* ``Unless explicitly declared extern, a const object does not have
7476 : external linkage and must be initialized. ($8.4; $12.1)'' ARM
7477 : 7.1.6 */
7478 9441346 : if (VAR_P (decl)
7479 9441346 : && !TYPE_REF_P (type)
7480 9392504 : && (CP_TYPE_CONST_P (type)
7481 : /* C++20 permits trivial default initialization in constexpr
7482 : context (P1331R2). */
7483 7303320 : || (cxx_dialect < cxx20
7484 139255 : && (constexpr_context_p
7485 124495 : || var_in_constexpr_fn (decl))))
7486 11547406 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7487 : {
7488 672175 : tree field = default_init_uninitialized_part (type);
7489 672175 : if (!field)
7490 : return true;
7491 :
7492 5124 : auto_diagnostic_group d;
7493 5124 : bool show_notes = true;
7494 :
7495 5124 : if (!constexpr_context_p || cxx_dialect >= cxx20)
7496 : {
7497 188 : if (CP_TYPE_CONST_P (type))
7498 : {
7499 90 : if (complain & tf_error)
7500 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7501 : "uninitialized %<const %D%>", decl);
7502 : }
7503 : else
7504 : {
7505 98 : if (!is_instantiation_of_constexpr (current_function_decl)
7506 98 : && (complain & tf_error))
7507 21 : error_at (DECL_SOURCE_LOCATION (decl),
7508 : "uninitialized variable %qD in %<constexpr%> "
7509 : "function", decl);
7510 : else
7511 : show_notes = false;
7512 98 : cp_function_chain->invalid_constexpr = true;
7513 : }
7514 : }
7515 4936 : else if (complain & tf_error)
7516 2 : error_at (DECL_SOURCE_LOCATION (decl),
7517 : "uninitialized variable %qD in %<constexpr%> context",
7518 : decl);
7519 :
7520 5124 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7521 : {
7522 62 : tree defaulted_ctor;
7523 :
7524 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7525 : "%q#T has no user-provided default constructor", type);
7526 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
7527 62 : if (defaulted_ctor)
7528 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7529 : "constructor is not user-provided because it is "
7530 : "explicitly defaulted in the class body");
7531 62 : inform (DECL_SOURCE_LOCATION (field),
7532 : "and the implicitly-defined constructor does not "
7533 : "initialize %q#D", field);
7534 : }
7535 :
7536 5124 : return false;
7537 5124 : }
7538 :
7539 : return true;
7540 : }
7541 :
7542 : /* Structure holding the current initializer being processed by reshape_init.
7543 : CUR is a pointer to the current element being processed, END is a pointer
7544 : after the last element present in the initializer and RAW_IDX is index into
7545 : RAW_DATA_CST if that is CUR elt. */
7546 : struct reshape_iter
7547 : {
7548 : constructor_elt *cur;
7549 : constructor_elt *end;
7550 : unsigned raw_idx;
7551 : };
7552 :
7553 : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7554 : static tree reshape_single_init (tree, tree, tsubst_flags_t);
7555 :
7556 : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7557 : returned is the next FIELD_DECL (possibly FIELD itself) that can be
7558 : initialized as if for an aggregate class. If there are no more such fields,
7559 : the return value will be NULL. */
7560 :
7561 : tree
7562 15762004 : next_aggregate_field (tree field)
7563 : {
7564 15762004 : while (field
7565 65344533 : && (TREE_CODE (field) != FIELD_DECL
7566 9880687 : || DECL_UNNAMED_BIT_FIELD (field)
7567 9880526 : || (DECL_ARTIFICIAL (field)
7568 : /* In C++17, aggregates can have bases. */
7569 217893 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7570 49582529 : field = DECL_CHAIN (field);
7571 :
7572 15762004 : return field;
7573 : }
7574 :
7575 : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7576 : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7577 : to a subobject. If there are no more such fields, the return value will be
7578 : NULL. */
7579 :
7580 : tree
7581 68777217 : next_subobject_field (tree field)
7582 : {
7583 68777217 : while (field
7584 893538162 : && (TREE_CODE (field) != FIELD_DECL
7585 30578835 : || DECL_UNNAMED_BIT_FIELD (field)
7586 30578696 : || (DECL_ARTIFICIAL (field)
7587 8500729 : && !DECL_FIELD_IS_BASE (field)
7588 1840704 : && !DECL_VIRTUAL_P (field))))
7589 824760945 : field = DECL_CHAIN (field);
7590 :
7591 68777217 : return field;
7592 : }
7593 :
7594 : /* Return true for [dcl.init.list] direct-list-initialization from
7595 : single element of enumeration with a fixed underlying type. */
7596 :
7597 : bool
7598 191545473 : is_direct_enum_init (tree type, tree init)
7599 : {
7600 191545473 : if (cxx_dialect >= cxx17
7601 189453383 : && TREE_CODE (type) == ENUMERAL_TYPE
7602 7756062 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7603 5294595 : && TREE_CODE (init) == CONSTRUCTOR
7604 455878 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
7605 231813 : && CONSTRUCTOR_NELTS (init) == 1
7606 217 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7607 : /* DR 2374: The single element needs to be implicitly
7608 : convertible to the underlying type of the enum. */
7609 217 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7610 191545688 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7611 215 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7612 215 : CONSTRUCTOR_ELT (init, 0)->value,
7613 : LOOKUP_IMPLICIT, tf_none))
7614 : return true;
7615 : return false;
7616 : }
7617 :
7618 : /* Helper function for reshape_init*. Split first element of
7619 : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7620 : to true if the whole d->cur has been consumed. */
7621 :
7622 : static tree
7623 63215104 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7624 : {
7625 63215104 : *inc_cur = true;
7626 63215104 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7627 : return NULL_TREE;
7628 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7629 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7630 30588 : *inc_cur = false;
7631 : else
7632 132 : d->raw_idx = 0;
7633 : return ret;
7634 : }
7635 :
7636 : /* Wrapper around that which for RAW_DATA_CST in INIT
7637 : (as well as in D->cur->value) peels off the first element
7638 : of the raw data and returns it, otherwise increments
7639 : D->cur and returns INIT. */
7640 :
7641 : static tree
7642 63215087 : consume_init (tree init, reshape_iter *d)
7643 : {
7644 63215087 : bool inc_cur;
7645 63215087 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7646 30720 : init = raw_init;
7647 63215087 : if (inc_cur)
7648 63184499 : d->cur++;
7649 63215087 : return init;
7650 : }
7651 :
7652 : /* Subroutine of reshape_init_array and reshape_init_vector, which does
7653 : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7654 : INTEGER_CST representing the size of the array minus one (the maximum index),
7655 : or NULL_TREE if the array was declared without specifying the size. D is
7656 : the iterator within the constructor. */
7657 :
7658 : static tree
7659 472172 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7660 : tree first_initializer_p, bool vector_p,
7661 : tsubst_flags_t complain)
7662 : {
7663 472172 : tree new_init;
7664 472172 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7665 472172 : unsigned HOST_WIDE_INT max_index_cst = 0;
7666 472172 : unsigned HOST_WIDE_INT index;
7667 :
7668 : /* The initializer for an array is always a CONSTRUCTOR. If this is the
7669 : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7670 : to build a new one. But don't reuse if not complaining; if this is
7671 : tentative, we might also reshape to another type (95319). */
7672 472172 : bool reuse = (first_initializer_p
7673 413124 : && (complain & tf_error)
7674 411839 : && !CP_AGGREGATE_TYPE_P (elt_type)
7675 818456 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7676 129642 : if (reuse)
7677 : new_init = first_initializer_p;
7678 : else
7679 129642 : new_init = build_constructor (init_list_type_node, NULL);
7680 :
7681 472172 : if (sized_array_p)
7682 : {
7683 281232 : poly_uint64 midx;
7684 : /* Minus 1 is used for zero sized arrays. */
7685 281232 : if (integer_all_onesp (max_index))
7686 24 : return new_init;
7687 :
7688 281208 : if (tree_fits_poly_uint64_p (max_index))
7689 281208 : midx = tree_to_poly_uint64 (max_index);
7690 : /* sizetype is sign extended, not zero extended. */
7691 : else
7692 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7693 :
7694 : /* For VLA vectors, we restrict the number of elements in the constructor
7695 : to lower bound of the VLA elements. */
7696 281208 : max_index_cst = constant_lower_bound (midx);
7697 : }
7698 :
7699 472148 : constructor_elt *first_cur = d->cur;
7700 : /* Loop until there are no more initializers. */
7701 57591254 : for (index = 0;
7702 57591254 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7703 : ++index)
7704 : {
7705 57119287 : tree elt_init;
7706 57119287 : constructor_elt *old_cur = d->cur;
7707 57119287 : unsigned int old_raw_idx = d->raw_idx;
7708 57119287 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7709 :
7710 57119287 : if (d->cur->index)
7711 5948415 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7712 57119287 : check_array_designated_initializer (d->cur, index);
7713 57119287 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7714 32752 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7715 10233 : || is_byte_access_type (elt_type))
7716 22519 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7717 1858 : && (!sized_array_p || index < max_index_cst)
7718 57121133 : && !vector_p)
7719 : {
7720 526 : elt_init = d->cur->value;
7721 526 : unsigned int off = d->raw_idx;
7722 526 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7723 526 : if (!sized_array_p || len <= max_index_cst - index + 1)
7724 : {
7725 517 : d->cur++;
7726 517 : d->raw_idx = 0;
7727 : }
7728 : else
7729 : {
7730 9 : len = max_index_cst - index + 1;
7731 9 : d->raw_idx += len;
7732 : }
7733 526 : if (!reuse || off || d->cur == old_cur)
7734 : {
7735 27 : elt_init = copy_node (elt_init);
7736 27 : RAW_DATA_LENGTH (elt_init) = len;
7737 27 : RAW_DATA_POINTER (elt_init) += off;
7738 : }
7739 526 : TREE_TYPE (elt_init) = elt_type;
7740 : }
7741 57118761 : else if (d->cur->index)
7742 : {
7743 5948190 : elt_init = reshape_single_init (elt_type, d->cur->value, complain);
7744 5948190 : d->cur++;
7745 : }
7746 : else
7747 51170571 : elt_init = reshape_init_r (elt_type, d,
7748 : /*first_initializer_p=*/NULL_TREE,
7749 : complain);
7750 57119287 : if (elt_init == error_mark_node)
7751 : return error_mark_node;
7752 57119109 : tree idx = size_int (index);
7753 57119109 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7754 : {
7755 : /* We need to stop reusing as some RAW_DATA_CST in the original
7756 : ctor had to be split. */
7757 72 : new_init = build_constructor (init_list_type_node, NULL);
7758 72 : if (index)
7759 : {
7760 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7761 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7762 72 : (d->cur - first_cur)
7763 : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7764 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7765 : {
7766 : unsigned int j;
7767 : tree nidx, nval;
7768 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7769 : j, nidx, nval)
7770 4 : if (nidx)
7771 : {
7772 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7773 4 : (void) nval;
7774 4 : break;
7775 : }
7776 : }
7777 : }
7778 : reuse = false;
7779 : }
7780 57119041 : if (reuse)
7781 : {
7782 55601958 : old_cur->index = idx;
7783 55601958 : old_cur->value = elt_init;
7784 : }
7785 : else
7786 1517151 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7787 : idx, elt_init);
7788 57119109 : if (!TREE_CONSTANT (elt_init))
7789 196740 : TREE_CONSTANT (new_init) = false;
7790 :
7791 : /* This can happen with an invalid initializer (c++/54501). */
7792 57119109 : if (d->cur == old_cur
7793 32082 : && !sized_array_p
7794 14085 : && d->raw_idx == old_raw_idx)
7795 : break;
7796 :
7797 57119106 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7798 526 : index += RAW_DATA_LENGTH (elt_init) - 1;
7799 : }
7800 :
7801 : return new_init;
7802 : }
7803 :
7804 : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7805 : Parameters are the same of reshape_init_r. */
7806 :
7807 : static tree
7808 422269 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7809 : tsubst_flags_t complain)
7810 : {
7811 422269 : tree max_index = NULL_TREE;
7812 :
7813 422269 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7814 :
7815 422269 : if (TYPE_DOMAIN (type))
7816 231375 : max_index = array_type_nelts_minus_one (type);
7817 :
7818 422269 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7819 422269 : first_initializer_p, false, complain);
7820 : }
7821 :
7822 : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7823 : Parameters are the same of reshape_init_r. */
7824 :
7825 : static tree
7826 49903 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7827 : {
7828 49903 : tree max_index = NULL_TREE;
7829 :
7830 49903 : gcc_assert (VECTOR_TYPE_P (type));
7831 :
7832 49903 : if (COMPOUND_LITERAL_P (d->cur->value))
7833 : {
7834 0 : tree value = d->cur->value;
7835 0 : if (!same_type_p (TREE_TYPE (value), type))
7836 : {
7837 0 : if (complain & tf_error)
7838 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7839 0 : TREE_TYPE (d->cur->value), type);
7840 0 : value = error_mark_node;
7841 : }
7842 0 : ++d->cur;
7843 0 : return value;
7844 : }
7845 :
7846 : /* For a vector, we initialize it as an array of the appropriate size. */
7847 49903 : if (VECTOR_TYPE_P (type))
7848 49903 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7849 :
7850 49903 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7851 49903 : NULL_TREE, true, complain);
7852 : }
7853 :
7854 : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7855 : INIT, in isolation from any designator or other initializers. */
7856 :
7857 : static tree
7858 6606243 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7859 : {
7860 : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7861 : calling reshape_init, but this way can just live on the stack. */
7862 6606243 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7863 6606243 : reshape_iter iter = { &elt, &elt + 1, 0 };
7864 6606243 : return reshape_init_r (type, &iter,
7865 : /*first_initializer_p=*/NULL_TREE,
7866 6606243 : complain);
7867 : }
7868 :
7869 : /* Subroutine of reshape_init_r, processes the initializers for classes
7870 : or union. Parameters are the same of reshape_init_r. */
7871 :
7872 : static tree
7873 3798355 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7874 : tsubst_flags_t complain)
7875 : {
7876 3798355 : tree field;
7877 3798355 : tree new_init;
7878 :
7879 3798355 : gcc_assert (CLASS_TYPE_P (type));
7880 :
7881 : /* The initializer for a class is always a CONSTRUCTOR. */
7882 3798355 : new_init = build_constructor (init_list_type_node, NULL);
7883 :
7884 3798355 : int binfo_idx = -1;
7885 3798355 : tree binfo = TYPE_BINFO (type);
7886 3798355 : tree base_binfo = NULL_TREE;
7887 3798355 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7888 : {
7889 : /* We get here from maybe_aggr_guide for C++20 class template argument
7890 : deduction. In this case we need to look through the binfo because a
7891 : template doesn't have base fields. */
7892 2781 : binfo_idx = 0;
7893 2781 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7894 : }
7895 86 : if (base_binfo)
7896 : field = base_binfo;
7897 : else
7898 3798269 : field = next_aggregate_field (TYPE_FIELDS (type));
7899 :
7900 3798269 : if (!field)
7901 : {
7902 : /* [dcl.init.aggr]
7903 :
7904 : An initializer for an aggregate member that is an
7905 : empty class shall have the form of an empty
7906 : initializer-list {}. */
7907 322 : if (!first_initializer_p)
7908 : {
7909 268 : if (complain & tf_error)
7910 13 : error ("initializer for %qT must be brace-enclosed", type);
7911 268 : return error_mark_node;
7912 : }
7913 : return new_init;
7914 : }
7915 :
7916 : /* For C++29 designated initializers we do modify d->cur->index in place
7917 : to cache name lookup results. Make sure to undo it before returning. */
7918 3798033 : struct designator_undo {
7919 : constructor_elt *start, *end;
7920 3798094 : void undo ()
7921 : {
7922 3798245 : while (start != end)
7923 : {
7924 151 : start->index = DECL_NAME (start->index);
7925 151 : ++start;
7926 : }
7927 3798094 : start = end = nullptr;
7928 3798094 : }
7929 3798033 : ~designator_undo () { undo (); }
7930 3798033 : } desig_undo = { nullptr, nullptr };
7931 :
7932 : /* For C++20 CTAD, handle pack expansions in the base list. */
7933 3798033 : tree last_was_pack_expansion = NULL_TREE;
7934 3798033 : bool first_desig = true;
7935 :
7936 : /* Loop through the initializable fields, gathering initializers. */
7937 11359596 : while (d->cur != d->end)
7938 : {
7939 7670716 : tree field_init;
7940 7670716 : constructor_elt *old_cur = d->cur;
7941 7670716 : unsigned old_raw_idx = d->raw_idx;
7942 7670716 : bool direct_desig = false;
7943 :
7944 : /* Handle C++20 designated initializers. */
7945 7670716 : if (d->cur->index)
7946 : {
7947 658387 : if (d->cur->index == error_mark_node)
7948 : return error_mark_node;
7949 :
7950 658380 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7951 : {
7952 181 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7953 181 : direct_desig = true;
7954 181 : field = d->cur->index;
7955 : }
7956 658199 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7957 : {
7958 658193 : if (first_desig && cxx_dialect >= cxx20)
7959 : {
7960 312484 : if (CONSTRUCTOR_NELTS (new_init))
7961 : {
7962 115 : constructor_elt *last
7963 115 : = &CONSTRUCTOR_ELTS (new_init)->last ();
7964 115 : if (last->index == NULL_TREE
7965 115 : || TREE_CODE (last->index) != FIELD_DECL
7966 230 : || !DECL_FIELD_IS_BASE (last->index))
7967 : {
7968 23 : if (complain & tf_error)
7969 23 : error ("last non-designated initializer clause "
7970 : "does not appertain to a base class "
7971 : "subobject");
7972 23 : return error_mark_node;
7973 : }
7974 : }
7975 : first_desig = false;
7976 : }
7977 658170 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7978 658170 : field = get_class_binding (type, d->cur->index);
7979 658170 : direct_desig = true;
7980 658170 : if (!field && cxx_dialect >= cxx29)
7981 44 : field = lookup_member (type, d->cur->index, /*protect=*/2,
7982 : /*want_type=*/false, complain);
7983 : }
7984 : else
7985 : {
7986 6 : if (complain & tf_error)
7987 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7988 : " for class %qT", d->cur->index, type);
7989 6 : return error_mark_node;
7990 : }
7991 :
7992 257 : if (!field && ANON_AGGR_TYPE_P (type))
7993 : /* Apparently the designator isn't for a member of this anonymous
7994 : struct, so head back to the enclosing class. */
7995 : break;
7996 :
7997 658348 : if (!field || TREE_CODE (field) != FIELD_DECL)
7998 : {
7999 43 : if (complain & tf_error)
8000 : {
8001 28 : if (field && TREE_CODE (field) == TREE_LIST)
8002 : {
8003 5 : auto_diagnostic_group g;
8004 5 : error ("request for member %qD is ambiguous",
8005 5 : d->cur->index);
8006 5 : print_candidates (input_location, field);
8007 5 : }
8008 : else
8009 23 : error ("%qT has no non-static data member named %qD", type,
8010 23 : d->cur->index);
8011 : }
8012 43 : return error_mark_node;
8013 : }
8014 :
8015 : /* If the element is an anonymous union object and the initializer
8016 : list is a designated-initializer-list, the anonymous union object
8017 : is initialized by the designated-initializer-list { D }, where D
8018 : is the designated-initializer-clause naming a member of the
8019 : anonymous union object. */
8020 658305 : tree ictx = DECL_CONTEXT (field);
8021 658305 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
8022 : {
8023 : /* Find the anon aggr that is a direct member of TYPE. */
8024 266 : while (ANON_AGGR_TYPE_P (ictx))
8025 : {
8026 154 : tree cctx = TYPE_CONTEXT (ictx);
8027 154 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
8028 140 : goto found;
8029 : ictx = cctx;
8030 : }
8031 :
8032 : /* In C++29 a designator can name a member of a base; in that
8033 : case, go through the designators and replace ids with _DECLs
8034 : to record the lookup for the most-derived class. */
8035 112 : if (cxx_dialect >= cxx29)
8036 : {
8037 110 : tree ibinfo = lookup_base (type, ictx, ba_unique, NULL,
8038 : complain);
8039 110 : if (!ibinfo)
8040 : /* The designator names a field outside this base class,
8041 : so we're done. */
8042 : break;
8043 61 : else if (ibinfo != error_mark_node)
8044 : {
8045 74 : while (BINFO_INHERITANCE_CHAIN (ibinfo) != binfo)
8046 : ibinfo = BINFO_INHERITANCE_CHAIN (ibinfo);
8047 61 : ictx = TREE_TYPE (ibinfo);
8048 :
8049 61 : desig_undo.undo ();
8050 :
8051 61 : if (d->cur->index != field)
8052 : {
8053 47 : d->cur->index = field;
8054 47 : desig_undo.start = d->cur;
8055 : }
8056 61 : constructor_elt *e = d->cur + 1;
8057 228 : for (; e != d->end; ++e)
8058 : {
8059 173 : if (e->index == NULL_TREE
8060 173 : || e->index == error_mark_node)
8061 : break;
8062 167 : if (desig_undo.start)
8063 : {
8064 104 : gcc_assert (TREE_CODE (e->index)
8065 : == IDENTIFIER_NODE);
8066 104 : field = lookup_member (type, e->index,
8067 : /*protect=*/2,
8068 : /*want_type=*/false,
8069 : tf_none);
8070 104 : if (!field || TREE_CODE (field) != FIELD_DECL)
8071 : break;
8072 : }
8073 : else
8074 : {
8075 63 : gcc_assert (TREE_CODE (e->index) == FIELD_DECL);
8076 : field = e->index;
8077 : }
8078 :
8079 167 : if (desig_undo.start)
8080 104 : e->index = field;
8081 : }
8082 61 : if (desig_undo.start)
8083 47 : desig_undo.end = e;
8084 61 : goto found;
8085 : }
8086 : }
8087 :
8088 : /* Not found, e.g. FIELD is a member of a base class. */
8089 2 : if (complain & tf_error)
8090 2 : error ("%qD is not a direct member of %qT", field, type);
8091 2 : return error_mark_node;
8092 :
8093 201 : found:
8094 : /* Now find the TYPE member with that anon aggr type. */
8095 201 : tree aafield = TYPE_FIELDS (type);
8096 1622 : for (; aafield; aafield = TREE_CHAIN (aafield))
8097 1622 : if (TREE_TYPE (aafield) == ictx)
8098 : break;
8099 0 : gcc_assert (aafield);
8100 : field = aafield;
8101 : direct_desig = false;
8102 : }
8103 : }
8104 :
8105 : /* If we processed all the member of the class, we are done. */
8106 7670382 : if (!field)
8107 : break;
8108 :
8109 15340196 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
8110 7670108 : ? field : NULL_TREE);
8111 20 : if (last_was_pack_expansion)
8112 : /* Each non-trailing aggregate element that is a pack expansion is
8113 : assumed to correspond to no elements of the initializer list. */
8114 20 : goto continue_;
8115 :
8116 7670088 : if (direct_desig)
8117 : {
8118 : /* The designated field F is initialized from this one element.
8119 :
8120 : Note that we don't want to do this if we found the designator
8121 : inside an anon aggr above; we use the normal code to implement:
8122 :
8123 : "If the element is an anonymous union member and the initializer
8124 : list is a brace-enclosed designated- initializer-list, the element
8125 : is initialized by the designated-initializer-list { D }, where D
8126 : is the designated- initializer-clause naming a member of the
8127 : anonymous union member." */
8128 658053 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
8129 658053 : field_init = reshape_single_init (TREE_TYPE (field),
8130 : d->cur->value, complain);
8131 658053 : d->cur++;
8132 : }
8133 : else
8134 7012035 : field_init = reshape_init_r (TREE_TYPE (field), d,
8135 : /*first_initializer_p=*/NULL_TREE,
8136 : complain);
8137 :
8138 7670088 : if (field_init == error_mark_node)
8139 : return error_mark_node;
8140 :
8141 7669800 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
8142 : {
8143 : /* This can happen with an invalid initializer for a flexible
8144 : array member (c++/54441). */
8145 0 : if (complain & tf_error)
8146 0 : error ("invalid initializer for %q#D", field);
8147 0 : return error_mark_node;
8148 : }
8149 :
8150 7669800 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
8151 :
8152 : /* [dcl.init.aggr]
8153 :
8154 : When a union is initialized with a brace-enclosed
8155 : initializer, the braces shall only contain an
8156 : initializer for the first member of the union. */
8157 7669800 : if (TREE_CODE (type) == UNION_TYPE)
8158 : break;
8159 :
8160 7561543 : continue_:
8161 7561563 : if (base_binfo)
8162 : {
8163 101 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
8164 : field = base_binfo;
8165 : else
8166 38 : field = next_aggregate_field (TYPE_FIELDS (type));
8167 : }
8168 : else
8169 7561462 : field = next_aggregate_field (DECL_CHAIN (field));
8170 : }
8171 :
8172 : /* A trailing aggregate element that is a pack expansion is assumed to
8173 : correspond to all remaining elements of the initializer list (if any). */
8174 3797664 : if (last_was_pack_expansion)
8175 : {
8176 17 : tree init = d->cur->value;
8177 17 : bool inc_cur;
8178 17 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
8179 0 : init = raw_init;
8180 17 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
8181 : last_was_pack_expansion, init);
8182 49 : while (d->cur != d->end)
8183 32 : d->cur++;
8184 : }
8185 :
8186 : return new_init;
8187 3798033 : }
8188 :
8189 : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
8190 : designators are not valid; either complain or return true to indicate
8191 : that reshape_init_r should return error_mark_node. */
8192 :
8193 : static bool
8194 252428 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
8195 : {
8196 252428 : if (d->cur->index)
8197 : {
8198 30 : if (complain & tf_error)
8199 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
8200 : "C99 designator %qE outside aggregate initializer",
8201 : d->cur->index);
8202 : else
8203 : return true;
8204 : }
8205 : return false;
8206 : }
8207 :
8208 : /* Subroutine of reshape_init, which processes a single initializer (part of
8209 : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
8210 : iterator within the CONSTRUCTOR which points to the initializer to process.
8211 : If this is the first initializer of the outermost CONSTRUCTOR node,
8212 : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
8213 :
8214 : static tree
8215 69301569 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
8216 : tsubst_flags_t complain)
8217 : {
8218 69301569 : tree init = d->cur->value;
8219 :
8220 69301569 : if (error_operand_p (init))
8221 131 : return error_mark_node;
8222 :
8223 4512592 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
8224 69553687 : && has_designator_problem (d, complain))
8225 0 : return error_mark_node;
8226 :
8227 69301438 : tree stripped_init = tree_strip_any_location_wrapper (init);
8228 :
8229 69301438 : if (TREE_CODE (type) == COMPLEX_TYPE)
8230 : {
8231 : /* A complex type can be initialized from one or two initializers,
8232 : but braces are not elided. */
8233 672 : init = consume_init (init, d);
8234 672 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
8235 : {
8236 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
8237 : {
8238 0 : if (complain & tf_error)
8239 0 : error ("too many initializers for %qT", type);
8240 : else
8241 0 : return error_mark_node;
8242 : }
8243 : }
8244 663 : else if (first_initializer_p && d->cur != d->end)
8245 : {
8246 36 : if (error_operand_p (d->cur->value)
8247 36 : || has_designator_problem (d, complain))
8248 0 : return error_mark_node;
8249 36 : vec<constructor_elt, va_gc> *v = 0;
8250 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8251 36 : init = consume_init (d->cur->value, d);
8252 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8253 36 : init = build_constructor (init_list_type_node, v);
8254 : }
8255 : return init;
8256 : }
8257 :
8258 : /* If we have a designator, d doesn't initialize TYPE directly, it
8259 : initializes an element, with brace elision if !first_initializer_p. But
8260 : if TYPE is non-aggregate (and we didn't already return error_mark_node),
8261 : we should have errored about the designator in has_designator_problem, so
8262 : now ignore it for error recovery. */
8263 69300766 : if (d->cur->index)
8264 : {
8265 : /* Deliberately not CP_AGGREGATE_TYPE_P to get a better diagnostic for
8266 : trying to designate a member of a non-aggregate class. */
8267 369917 : if (AGGREGATE_TYPE_P (type))
8268 369911 : goto skip_single;
8269 : else
8270 6 : gcc_checking_assert (seen_error ());
8271 : }
8272 :
8273 : /* A non-aggregate type is always initialized with a single
8274 : initializer. */
8275 137803009 : if (!CP_AGGREGATE_TYPE_P (type)
8276 : /* As is an array with dependent bound, which we can see
8277 : during C++20 aggregate CTAD. */
8278 74594334 : || (cxx_dialect >= cxx20
8279 5621774 : && TREE_CODE (type) == ARRAY_TYPE
8280 457604 : && uses_template_parms (TYPE_DOMAIN (type))))
8281 : {
8282 : /* It is invalid to initialize a non-aggregate type with a
8283 : brace-enclosed initializer before C++0x.
8284 : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
8285 : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
8286 : a CONSTRUCTOR (with a record type). */
8287 63208686 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
8288 : /* Don't complain about a capture-init. */
8289 305211 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
8290 63503084 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
8291 : {
8292 294359 : if (SCALAR_TYPE_P (type))
8293 : {
8294 490 : if (cxx_dialect < cxx11)
8295 : {
8296 16 : if (complain & tf_error)
8297 16 : error ("braces around scalar initializer for type %qT",
8298 : type);
8299 16 : init = error_mark_node;
8300 : }
8301 474 : else if (first_initializer_p
8302 474 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
8303 296 : && (BRACE_ENCLOSED_INITIALIZER_P
8304 : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8305 : {
8306 13 : if (complain & tf_error)
8307 13 : error ("too many braces around scalar initializer "
8308 : "for type %qT", type);
8309 13 : init = error_mark_node;
8310 : }
8311 : }
8312 : else
8313 293869 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8314 : }
8315 63208686 : return consume_init (init, d);
8316 : }
8317 :
8318 : /* "If T is a class type and the initializer list has a single element of
8319 : type cv U, where U is T or a class derived from T, the object is
8320 : initialized from that element." Even if T is an aggregate. */
8321 5698324 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8322 5215210 : && first_initializer_p
8323 3515131 : && d->end - d->cur == 1
8324 610500 : && TREE_CODE (init) != RAW_DATA_CST
8325 6332669 : && reference_related_p (type, TREE_TYPE (init)))
8326 : {
8327 426 : d->cur++;
8328 426 : return init;
8329 : }
8330 :
8331 : /* [dcl.init.aggr]
8332 :
8333 : All implicit type conversions (clause _conv_) are considered when
8334 : initializing the aggregate member with an initializer from an
8335 : initializer-list. If the initializer can initialize a member,
8336 : the member is initialized. Otherwise, if the member is itself a
8337 : non-empty subaggregate, brace elision is assumed and the
8338 : initializer is considered for the initialization of the first
8339 : member of the subaggregate. */
8340 2459762 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8341 : /* But don't try this for the first initializer, since that would be
8342 : looking through the outermost braces; A a2 = { a1 }; is not a
8343 : valid aggregate initialization. */
8344 3262047 : && !first_initializer_p
8345 5738755 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8346 11355 : || can_convert_arg (type, TREE_TYPE (init),
8347 11355 : TREE_CODE (init) == RAW_DATA_CST
8348 5721743 : ? build_int_cst (integer_type_node,
8349 1606 : *(const unsigned char *)
8350 1606 : RAW_DATA_POINTER (init))
8351 : : init,
8352 : LOOKUP_NORMAL, complain)))
8353 5693 : return consume_init (init, d);
8354 :
8355 6085961 : skip_single:
8356 :
8357 : /* [dcl.init.string]
8358 :
8359 : A char array (whether plain char, signed char, or unsigned char)
8360 : can be initialized by a string-literal (optionally enclosed in
8361 : braces); a wchar_t array can be initialized by a wide
8362 : string-literal (optionally enclosed in braces). */
8363 6085961 : if (TREE_CODE (type) == ARRAY_TYPE
8364 6085961 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8365 : {
8366 112356 : tree str_init = init;
8367 112356 : tree stripped_str_init = stripped_init;
8368 112356 : reshape_iter stripd = {};
8369 :
8370 : /* Strip one level of braces if and only if they enclose a single
8371 : element (as allowed by [dcl.init.string]). */
8372 112356 : if (!first_initializer_p
8373 2780 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8374 113982 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8375 : {
8376 399 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8377 399 : str_init = stripd.cur->value;
8378 399 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
8379 : }
8380 :
8381 : /* If it's a string literal, then it's the initializer for the array
8382 : as a whole. Otherwise, continue with normal initialization for
8383 : array types (one value per array element). */
8384 112356 : if (TREE_CODE (stripped_str_init) == STRING_CST)
8385 : {
8386 72 : if ((first_initializer_p && has_designator_problem (d, complain))
8387 1062 : || (stripd.cur && has_designator_problem (&stripd, complain)))
8388 990 : return error_mark_node;
8389 990 : d->cur++;
8390 990 : return str_init;
8391 : }
8392 : }
8393 :
8394 : /* The following cases are about aggregates. If we are not within a full
8395 : initializer already, and there is not a CONSTRUCTOR, it means that there
8396 : is a missing set of braces (that is, we are processing the case for
8397 : which reshape_init exists). */
8398 6084971 : bool braces_elided_p = false;
8399 6084971 : if (!first_initializer_p)
8400 : {
8401 1825126 : if (TREE_CODE (stripped_init) == CONSTRUCTOR && !d->cur->index)
8402 : {
8403 1814461 : tree init_type = TREE_TYPE (init);
8404 1814461 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8405 : /* There is no need to call reshape_init for pointer-to-member
8406 : function initializers, as they are always constructed correctly
8407 : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8408 : which is missing outermost braces. We should warn below, and
8409 : one of the routines below will wrap it in additional { }. */;
8410 : /* For a nested compound literal, proceed to specialized routines,
8411 : to handle initialization of arrays and similar. */
8412 1814452 : else if (COMPOUND_LITERAL_P (stripped_init))
8413 8 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8414 : /* A CONSTRUCTOR of the target's type is a previously
8415 : digested initializer. */
8416 1814444 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8417 : {
8418 0 : ++d->cur;
8419 0 : return init;
8420 : }
8421 : else
8422 : {
8423 : /* Something that hasn't been reshaped yet. */
8424 1814444 : ++d->cur;
8425 1814444 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8426 1814444 : return reshape_init (type, init, complain);
8427 : }
8428 : }
8429 :
8430 10682 : if (complain & tf_warning)
8431 9492 : warning (OPT_Wmissing_braces,
8432 : "missing braces around initializer for %qT",
8433 : type);
8434 : braces_elided_p = true;
8435 : }
8436 :
8437 : /* Dispatch to specialized routines. */
8438 4270527 : tree new_init;
8439 4270527 : if (CLASS_TYPE_P (type))
8440 3798355 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
8441 472172 : else if (TREE_CODE (type) == ARRAY_TYPE)
8442 422269 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
8443 49903 : else if (VECTOR_TYPE_P (type))
8444 49903 : new_init = reshape_init_vector (type, d, complain);
8445 : else
8446 0 : gcc_unreachable ();
8447 :
8448 4270527 : if (braces_elided_p
8449 10682 : && TREE_CODE (new_init) == CONSTRUCTOR)
8450 10407 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8451 :
8452 : return new_init;
8453 : }
8454 :
8455 : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
8456 : brace-enclosed aggregate initializer.
8457 :
8458 : INIT is the CONSTRUCTOR containing the list of initializers describing
8459 : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8460 : It may not presently match the shape of the TYPE; for example:
8461 :
8462 : struct S { int a; int b; };
8463 : struct S a[] = { 1, 2, 3, 4 };
8464 :
8465 : Here INIT will hold a vector of four elements, rather than a
8466 : vector of two elements, each itself a vector of two elements. This
8467 : routine transforms INIT from the former form into the latter. The
8468 : revised CONSTRUCTOR node is returned. */
8469 :
8470 : tree
8471 12691876 : reshape_init (tree type, tree init, tsubst_flags_t complain)
8472 : {
8473 12691876 : vec<constructor_elt, va_gc> *v;
8474 12691876 : reshape_iter d;
8475 12691876 : tree new_init;
8476 :
8477 12691876 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8478 :
8479 12691876 : v = CONSTRUCTOR_ELTS (init);
8480 :
8481 : /* An empty constructor does not need reshaping, and it is always a valid
8482 : initializer. */
8483 12691876 : if (vec_safe_is_empty (v))
8484 : return init;
8485 :
8486 4513893 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8487 : /* Already reshaped. */
8488 : return init;
8489 :
8490 : /* Brace elision is not performed for a CONSTRUCTOR representing
8491 : parenthesized aggregate initialization. */
8492 4513744 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
8493 : {
8494 849 : tree elt = (*v)[0].value;
8495 : /* If we're initializing a char array from a string-literal that is
8496 : enclosed in braces, unwrap it here. */
8497 849 : if (TREE_CODE (type) == ARRAY_TYPE
8498 277 : && vec_safe_length (v) == 1
8499 149 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8500 888 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8501 34 : return elt;
8502 : return init;
8503 : }
8504 :
8505 : /* Handle [dcl.init.list] direct-list-initialization from
8506 : single element of enumeration with a fixed underlying type. */
8507 4512895 : if (is_direct_enum_init (type, init))
8508 : {
8509 175 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8510 175 : type = cv_unqualified (type);
8511 175 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8512 : {
8513 160 : warning_sentinel w (warn_useless_cast);
8514 160 : warning_sentinel w2 (warn_ignored_qualifiers);
8515 160 : return cp_build_c_cast (input_location, type, elt,
8516 : tf_warning_or_error);
8517 160 : }
8518 : else
8519 15 : return error_mark_node;
8520 : }
8521 :
8522 : /* Recurse on this CONSTRUCTOR. */
8523 4512720 : d.cur = &(*v)[0];
8524 4512720 : d.end = d.cur + v->length ();
8525 4512720 : d.raw_idx = 0;
8526 :
8527 4512720 : new_init = reshape_init_r (type, &d, init, complain);
8528 4512720 : if (new_init == error_mark_node)
8529 : return error_mark_node;
8530 :
8531 : /* Make sure all the element of the constructor were used. Otherwise,
8532 : issue an error about exceeding initializers. */
8533 4512046 : if (d.cur != d.end)
8534 : {
8535 207 : if (complain & tf_error)
8536 64 : error ("too many initializers for %qT", type);
8537 207 : return error_mark_node;
8538 : }
8539 :
8540 4511839 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8541 4511839 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8542 626774 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8543 4511839 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8544 4511839 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8545 75200 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8546 : || seen_error ());
8547 :
8548 : return new_init;
8549 : }
8550 :
8551 : /* Verify array initializer. Returns true if errors have been reported. */
8552 :
8553 : bool
8554 1039659 : check_array_initializer (tree decl, tree type, tree init)
8555 : {
8556 1039659 : tree element_type = TREE_TYPE (type);
8557 :
8558 : /* Structured binding when initialized with an array type needs
8559 : to have complete type. */
8560 1039659 : if (decl
8561 1022615 : && DECL_DECOMPOSITION_P (decl)
8562 10451 : && DECL_DECOMP_IS_BASE (decl)
8563 1050110 : && !COMPLETE_TYPE_P (type))
8564 : {
8565 0 : error_at (DECL_SOURCE_LOCATION (decl),
8566 : "structured binding has incomplete type %qT", type);
8567 0 : TREE_TYPE (decl) = error_mark_node;
8568 0 : return true;
8569 : }
8570 :
8571 : /* The array type itself need not be complete, because the
8572 : initializer may tell us how many elements are in the array.
8573 : But, the elements of the array must be complete. */
8574 1039659 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
8575 : {
8576 15 : if (decl)
8577 9 : error_at (DECL_SOURCE_LOCATION (decl),
8578 : "elements of array %q#D have incomplete type", decl);
8579 : else
8580 6 : error ("elements of array %q#T have incomplete type", type);
8581 : return true;
8582 : }
8583 :
8584 1039644 : location_t loc = (decl ? location_of (decl) : input_location);
8585 1039644 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8586 : return true;
8587 :
8588 : /* A compound literal can't have variable size. */
8589 1039644 : if (init && !decl
8590 1039644 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8591 17035 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8592 : {
8593 3 : error ("variable-sized compound literal");
8594 3 : return true;
8595 : }
8596 : return false;
8597 : }
8598 :
8599 : /* Subroutine of check_initializer; args are passed down from that function.
8600 : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8601 :
8602 : static tree
8603 3298595 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
8604 : {
8605 3298595 : gcc_assert (stmts_are_full_exprs_p ());
8606 3298595 : if (init)
8607 2685627 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8608 3298595 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
8609 : }
8610 :
8611 : /* Verify INIT (the initializer for DECL), and record the
8612 : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8613 : grok_reference_init.
8614 :
8615 : If the return value is non-NULL, it is an expression that must be
8616 : evaluated dynamically to initialize DECL. */
8617 :
8618 : static tree
8619 59443078 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8620 : {
8621 59443078 : tree type;
8622 59443078 : tree init_code = NULL;
8623 59443078 : tree core_type;
8624 :
8625 : /* Things that are going to be initialized need to have complete
8626 : type. */
8627 59443078 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8628 :
8629 59443078 : if (DECL_HAS_VALUE_EXPR_P (decl))
8630 : {
8631 : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8632 : it doesn't have storage to be initialized. */
8633 708 : gcc_assert (init == NULL_TREE);
8634 : return NULL_TREE;
8635 : }
8636 :
8637 59442370 : if (type == error_mark_node)
8638 : /* We will have already complained. */
8639 : return NULL_TREE;
8640 :
8641 59442370 : if (TREE_CODE (type) == ARRAY_TYPE)
8642 : {
8643 1022615 : if (check_array_initializer (decl, type, init))
8644 : return NULL_TREE;
8645 : }
8646 58419755 : else if (!COMPLETE_TYPE_P (type))
8647 : {
8648 37 : error_at (DECL_SOURCE_LOCATION (decl),
8649 : "%q#D has incomplete type", decl);
8650 37 : TREE_TYPE (decl) = error_mark_node;
8651 37 : return NULL_TREE;
8652 : }
8653 : else
8654 : /* There is no way to make a variable-sized class type in GNU C++. */
8655 58419718 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8656 :
8657 59442324 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8658 : {
8659 2980553 : int init_len = CONSTRUCTOR_NELTS (init);
8660 2980553 : if (SCALAR_TYPE_P (type))
8661 : {
8662 76312 : if (init_len == 0)
8663 : {
8664 42771 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8665 42771 : init = build_zero_init (type, NULL_TREE, false);
8666 : }
8667 33541 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8668 : {
8669 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8670 : "scalar object %qD requires one element in "
8671 : "initializer", decl);
8672 6 : TREE_TYPE (decl) = error_mark_node;
8673 6 : return NULL_TREE;
8674 : }
8675 : }
8676 : }
8677 :
8678 59442318 : if (TREE_CODE (decl) == CONST_DECL)
8679 : {
8680 0 : gcc_assert (!TYPE_REF_P (type));
8681 :
8682 0 : DECL_INITIAL (decl) = init;
8683 :
8684 0 : gcc_assert (init != NULL_TREE);
8685 : init = NULL_TREE;
8686 : }
8687 59442318 : else if (!init && DECL_REALLY_EXTERN (decl))
8688 : ;
8689 59442318 : else if (flag_openmp
8690 190517 : && VAR_P (decl)
8691 190517 : && DECL_LANG_SPECIFIC (decl)
8692 111301 : && DECL_OMP_DECLARE_MAPPER_P (decl)
8693 59442318 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8694 : return NULL_TREE;
8695 4403170 : else if (init || type_build_ctor_call (type)
8696 63232517 : || TYPE_REF_P (type))
8697 : {
8698 55652134 : if (TYPE_REF_P (type))
8699 : {
8700 388318 : init = grok_reference_init (decl, type, init, flags);
8701 388318 : flags |= LOOKUP_ALREADY_DIGESTED;
8702 : }
8703 55263816 : else if (!init)
8704 612971 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8705 : tf_warning_or_error);
8706 : /* Do not reshape constructors of vectors (they don't need to be
8707 : reshaped. */
8708 54650845 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8709 : {
8710 2937395 : if (is_std_init_list (type))
8711 : {
8712 410 : init = perform_implicit_conversion (type, init,
8713 : tf_warning_or_error);
8714 410 : flags |= LOOKUP_ALREADY_DIGESTED;
8715 : }
8716 2936985 : else if (TYPE_NON_AGGREGATE_CLASS (type))
8717 : {
8718 : /* Don't reshape if the class has constructors. */
8719 456013 : if (cxx_dialect == cxx98)
8720 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8721 : "in C++98 %qD must be initialized by "
8722 : "constructor, not by %<{...}%>",
8723 : decl);
8724 : }
8725 2480972 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8726 : {
8727 0 : error ("opaque vector types cannot be initialized");
8728 0 : init = error_mark_node;
8729 : }
8730 : else
8731 : {
8732 2480972 : init = reshape_init (type, init, tf_warning_or_error);
8733 2480972 : flags |= LOOKUP_NO_NARROWING;
8734 : }
8735 : }
8736 : /* [dcl.init] "Otherwise, if the destination type is an array, the object
8737 : is initialized as follows..." So handle things like
8738 :
8739 : int a[](1, 2, 3);
8740 :
8741 : which is permitted in C++20 by P0960. */
8742 51713450 : else if (TREE_CODE (init) == TREE_LIST
8743 782557 : && TREE_TYPE (init) == NULL_TREE
8744 782557 : && TREE_CODE (type) == ARRAY_TYPE
8745 182 : && !DECL_DECOMPOSITION_P (decl)
8746 51713602 : && (cxx_dialect >= cxx20))
8747 142 : init = do_aggregate_paren_init (init, type);
8748 51713308 : else if (TREE_CODE (init) == TREE_LIST
8749 782415 : && TREE_TYPE (init) != unknown_type_node
8750 52495723 : && !MAYBE_CLASS_TYPE_P (type))
8751 : {
8752 133918 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8753 :
8754 : /* We get here with code like `int a (2);' */
8755 133918 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8756 : tf_warning_or_error);
8757 : }
8758 :
8759 : /* If DECL has an array type without a specific bound, deduce the
8760 : array size from the initializer. */
8761 55652134 : maybe_deduce_size_from_array_init (decl, init);
8762 55652134 : type = TREE_TYPE (decl);
8763 55652134 : if (type == error_mark_node)
8764 : return NULL_TREE;
8765 :
8766 107745118 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8767 5517121 : && !(flags & LOOKUP_ALREADY_DIGESTED)
8768 8201237 : && !(init
8769 4903743 : && BRACE_ENCLOSED_INITIALIZER_P (init)
8770 5369052 : && CP_AGGREGATE_TYPE_P (type)))
8771 108016062 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8772 : {
8773 3298595 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8774 :
8775 : /* A constructor call is a non-trivial initializer even if
8776 : it isn't explicitly written. */
8777 3298595 : if (TREE_SIDE_EFFECTS (init_code))
8778 3274869 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8779 :
8780 : /* If this is a constexpr initializer, expand_default_init will
8781 : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8782 : case, pull the initializer back out and pass it down into
8783 : store_init_value. */
8784 8772137 : while (true)
8785 : {
8786 8772137 : if (TREE_CODE (init_code) == EXPR_STMT
8787 5508675 : || TREE_CODE (init_code) == STMT_EXPR
8788 5470645 : || TREE_CODE (init_code) == CONVERT_EXPR)
8789 5435677 : init_code = TREE_OPERAND (init_code, 0);
8790 3336460 : else if (TREE_CODE (init_code) == BIND_EXPR)
8791 37865 : init_code = BIND_EXPR_BODY (init_code);
8792 : else
8793 : break;
8794 : }
8795 3298595 : if (TREE_CODE (init_code) == INIT_EXPR)
8796 : {
8797 : /* In C++20, the call to build_aggr_init could have created
8798 : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8799 : A(1, 2). */
8800 2163852 : init = TREE_OPERAND (init_code, 1);
8801 2163852 : init_code = NULL_TREE;
8802 : /* Don't call digest_init; it's unnecessary and will complain
8803 : about aggregate initialization of non-aggregate classes. */
8804 2163852 : flags |= LOOKUP_ALREADY_DIGESTED;
8805 : }
8806 1134743 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8807 2250230 : || DECL_DECLARED_CONSTINIT_P (decl))
8808 : {
8809 : /* Declared constexpr or constinit, but no suitable initializer;
8810 : massage init appropriately so we can pass it into
8811 : store_init_value for the error. */
8812 19274 : tree new_init = NULL_TREE;
8813 19274 : if (!processing_template_decl
8814 274 : && TREE_CODE (init_code) == CALL_EXPR)
8815 232 : new_init = build_cplus_new (type, init_code, tf_none);
8816 19042 : else if (CLASS_TYPE_P (type)
8817 38084 : && (!init || TREE_CODE (init) == TREE_LIST))
8818 18861 : new_init = build_functional_cast (input_location, type,
8819 : init, tf_none);
8820 19093 : if (new_init)
8821 : {
8822 19093 : init = new_init;
8823 19093 : if (TREE_CODE (init) == TARGET_EXPR
8824 261 : && !(flags & LOOKUP_ONLYCONVERTING))
8825 261 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8826 : }
8827 : init_code = NULL_TREE;
8828 : }
8829 : else
8830 : init = NULL_TREE;
8831 : }
8832 :
8833 54536661 : if (init && TREE_CODE (init) != TREE_VEC)
8834 : {
8835 54536646 : init_code = store_init_value (decl, init, cleanups, flags);
8836 :
8837 54533949 : if (DECL_INITIAL (decl)
8838 39594056 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8839 57707010 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8840 : {
8841 1401516 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8842 1401516 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8843 1401516 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8844 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8845 : }
8846 :
8847 2635358 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8848 66434 : && DECL_INITIAL (decl)
8849 65695 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8850 54595234 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8851 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8852 3 : DECL_SOURCE_LOCATION (decl)),
8853 3 : 0, "array %qD initialized by parenthesized "
8854 : "string literal %qE",
8855 3 : decl, DECL_INITIAL (decl));
8856 : init = NULL_TREE;
8857 : }
8858 : }
8859 3790184 : else if (!init && REFLECTION_TYPE_P (type))
8860 : {
8861 : /* [dcl.init.general]: To default-initialize an object of type
8862 : std::meta::info means that the object is zero-initialized. */
8863 164 : DECL_INITIAL (decl)
8864 164 : = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
8865 164 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8866 164 : TREE_CONSTANT (decl) = true;
8867 164 : init = NULL_TREE;
8868 : }
8869 : else
8870 : {
8871 7580040 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8872 4616829 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8873 826794 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8874 21 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8875 : /*complain=*/true);
8876 :
8877 3790020 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8878 : tf_warning_or_error);
8879 : }
8880 :
8881 167 : if (init && init != error_mark_node)
8882 0 : init_code = cp_build_init_expr (decl, init);
8883 :
8884 16212774 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8885 59499425 : && init_code != error_mark_node)
8886 : init_code = NULL_TREE;
8887 :
8888 59382094 : if (init_code)
8889 : {
8890 : /* We might have set these in cp_finish_decl. */
8891 16155251 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8892 16155251 : TREE_CONSTANT (decl) = false;
8893 : }
8894 :
8895 16155251 : if (init_code
8896 16155251 : && DECL_IN_AGGR_P (decl)
8897 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8898 : {
8899 0 : static int explained = 0;
8900 :
8901 0 : auto_diagnostic_group d;
8902 0 : if (cxx_dialect < cxx11)
8903 0 : error ("initializer invalid for static member with constructor");
8904 0 : else if (cxx_dialect < cxx17)
8905 0 : error ("non-constant in-class initialization invalid for static "
8906 : "member %qD", decl);
8907 : else
8908 0 : error ("non-constant in-class initialization invalid for non-inline "
8909 : "static member %qD", decl);
8910 0 : if (!explained)
8911 : {
8912 0 : inform (input_location,
8913 : "(an out of class initialization is required)");
8914 0 : explained = 1;
8915 : }
8916 0 : return NULL_TREE;
8917 0 : }
8918 :
8919 : return init_code;
8920 : }
8921 :
8922 : /* If DECL is not a local variable, give it RTL. */
8923 :
8924 : static void
8925 126423873 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8926 : {
8927 126423873 : int toplev = toplevel_bindings_p ();
8928 126423873 : int defer_p;
8929 :
8930 : /* Set the DECL_ASSEMBLER_NAME for the object. */
8931 126423873 : if (asmspec)
8932 : {
8933 : /* The `register' keyword, when used together with an
8934 : asm-specification, indicates that the variable should be
8935 : placed in a particular register. */
8936 1047125 : if (VAR_P (decl) && DECL_REGISTER (decl))
8937 : {
8938 87 : if (TREE_ADDRESSABLE (decl))
8939 3 : error_at (DECL_SOURCE_LOCATION (decl),
8940 : "address of explicit register variable %qD requested",
8941 : decl);
8942 : else
8943 : {
8944 84 : set_user_assembler_name (decl, asmspec);
8945 84 : DECL_HARD_REGISTER (decl) = 1;
8946 : }
8947 : }
8948 : else
8949 : {
8950 1047038 : if (TREE_CODE (decl) == FUNCTION_DECL
8951 1047038 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8952 83904 : set_builtin_user_assembler_name (decl, asmspec);
8953 1047038 : set_user_assembler_name (decl, asmspec);
8954 1047038 : if (DECL_LOCAL_DECL_P (decl))
8955 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8956 : /* We have to propagate the name to the ns-alias.
8957 : This is horrible, as we're affecting a
8958 : possibly-shared decl. Again, a one-true-decl
8959 : model breaks down. */
8960 6 : if (ns_decl != error_mark_node)
8961 6 : set_user_assembler_name (ns_decl, asmspec);
8962 : }
8963 : }
8964 :
8965 : /* Handle non-variables up front. */
8966 126423873 : if (!VAR_P (decl))
8967 : {
8968 60152261 : rest_of_decl_compilation (decl, toplev, at_eof);
8969 60152261 : return;
8970 : }
8971 :
8972 : /* If we see a class member here, it should be a static data
8973 : member. */
8974 66271612 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8975 : {
8976 5714839 : gcc_assert (TREE_STATIC (decl));
8977 : /* An in-class declaration of a static data member should be
8978 : external; it is only a declaration, and not a definition. */
8979 5714839 : if (init == NULL_TREE)
8980 5714814 : gcc_assert (DECL_EXTERNAL (decl)
8981 : || !TREE_PUBLIC (decl));
8982 : }
8983 :
8984 : /* We don't create any RTL for local variables. */
8985 66271612 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8986 : return;
8987 :
8988 : /* Don't output reflection variables. */
8989 40844815 : if (consteval_only_p (decl))
8990 : {
8991 : /* Disable assemble_variable. */
8992 1540 : DECL_EXTERNAL (decl) = true;
8993 : /* Undo make_decl_one_only. */
8994 1540 : if (DECL_COMDAT_GROUP (decl))
8995 : {
8996 262 : symtab_node *node = symtab_node::get (decl);
8997 262 : node->set_comdat_group (NULL);
8998 262 : node->dissolve_same_comdat_group_list ();
8999 : }
9000 : return;
9001 : }
9002 :
9003 : /* We defer emission of local statics until the corresponding
9004 : DECL_EXPR is expanded. But with constexpr its function might never
9005 : be expanded, so go ahead and tell cgraph about the variable now. */
9006 81686550 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
9007 143458 : && !var_in_maybe_constexpr_fn (decl))
9008 81547816 : || DECL_VIRTUAL_P (decl));
9009 :
9010 : /* Defer template instantiations. */
9011 40843275 : if (DECL_LANG_SPECIFIC (decl)
9012 40843275 : && DECL_IMPLICIT_INSTANTIATION (decl))
9013 : defer_p = 1;
9014 :
9015 : /* If we're not deferring, go ahead and assemble the variable. */
9016 20928204 : if (!defer_p)
9017 18475979 : rest_of_decl_compilation (decl, toplev, at_eof);
9018 : }
9019 :
9020 : /* walk_tree helper for wrap_temporary_cleanups, below. */
9021 :
9022 : static tree
9023 11617788 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
9024 : {
9025 : /* Stop at types or full-expression boundaries. */
9026 11617788 : if (TYPE_P (*stmt_p)
9027 11617560 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
9028 : {
9029 488 : *walk_subtrees = 0;
9030 488 : return NULL_TREE;
9031 : }
9032 :
9033 11617300 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
9034 : {
9035 252168 : tree guard = (tree)data;
9036 252168 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
9037 :
9038 126104 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
9039 374258 : && !expr_noexcept_p (tcleanup, tf_none))
9040 : {
9041 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
9042 : /* Tell honor_protect_cleanup_actions to handle this as a separate
9043 : cleanup. */
9044 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
9045 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
9046 : }
9047 : }
9048 :
9049 : return NULL_TREE;
9050 : }
9051 :
9052 : /* We're initializing a local variable which has a cleanup GUARD. If there
9053 : are any temporaries used in the initializer INIT of this variable, we
9054 : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
9055 : variable will be cleaned up properly if one of them throws.
9056 :
9057 : Unfortunately, there's no way to express this properly in terms of
9058 : nesting, as the regions for the temporaries overlap the region for the
9059 : variable itself; if there are two temporaries, the variable needs to be
9060 : the first thing destroyed if either of the temporary destructors throws.
9061 : However, we only want to run the variable's cleanup if it actually got
9062 : constructed. So we need to guard the temporary cleanups with the
9063 : variable's cleanup if they are run on the normal path, but not if they
9064 : are run on the exceptional path. We implement this by telling
9065 : honor_protect_cleanup_actions to strip the variable cleanup from the
9066 : exceptional path.
9067 :
9068 : Another approach could be to make the variable cleanup region enclose
9069 : initialization, but depend on a flag to indicate that the variable is
9070 : initialized; that's effectively what we do for arrays. But the current
9071 : approach works fine for non-arrays, and has no code overhead in the usual
9072 : case where the temporary destructors are noexcept. */
9073 :
9074 : static void
9075 1047865 : wrap_temporary_cleanups (tree init, tree guard)
9076 : {
9077 1047865 : if (TREE_CODE (guard) == BIND_EXPR)
9078 : {
9079 : /* An array cleanup region already encloses any temporary cleanups,
9080 : don't wrap it around them again. */
9081 422 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
9082 : return;
9083 : }
9084 1047443 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
9085 : }
9086 :
9087 : /* Generate code to initialize DECL (a local variable). */
9088 :
9089 : static void
9090 25426770 : initialize_local_var (tree decl, tree init, bool decomp)
9091 : {
9092 25426770 : tree type = TREE_TYPE (decl);
9093 25426770 : tree cleanup;
9094 25426770 : int already_used;
9095 :
9096 25426770 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
9097 25426770 : gcc_assert (!TREE_STATIC (decl));
9098 :
9099 25426770 : if (DECL_SIZE (decl) == NULL_TREE)
9100 : {
9101 : /* If we used it already as memory, it must stay in memory. */
9102 0 : DECL_INITIAL (decl) = NULL_TREE;
9103 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
9104 0 : return;
9105 : }
9106 :
9107 25426770 : if (type == error_mark_node)
9108 : return;
9109 :
9110 : /* Compute and store the initial value. */
9111 25426770 : already_used = TREE_USED (decl) || TREE_USED (type);
9112 25426770 : if (TREE_USED (type))
9113 32 : DECL_READ_P (decl) = 1;
9114 :
9115 : /* Generate a cleanup, if necessary. */
9116 25426770 : cleanup = (decomp ? NULL_TREE
9117 25391126 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
9118 :
9119 : /* Perform the initialization. */
9120 25426770 : if (init)
9121 : {
9122 16140616 : tree rinit = (TREE_CODE (init) == INIT_EXPR
9123 16140616 : ? TREE_OPERAND (init, 1) : NULL_TREE);
9124 14731555 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
9125 22812033 : && TREE_OPERAND (init, 0) == decl)
9126 : {
9127 : /* Stick simple initializers in DECL_INITIAL so that
9128 : -Wno-init-self works (c++/34772). */
9129 8080478 : DECL_INITIAL (decl) = rinit;
9130 :
9131 8080478 : if (warn_init_self && TYPE_REF_P (type))
9132 : {
9133 2469 : STRIP_NOPS (rinit);
9134 2469 : if (rinit == decl)
9135 3 : warning_at (DECL_SOURCE_LOCATION (decl),
9136 3 : OPT_Winit_self,
9137 : "reference %qD is initialized with itself", decl);
9138 : }
9139 : }
9140 : else
9141 : {
9142 8060138 : int saved_stmts_are_full_exprs_p;
9143 :
9144 : /* If we're only initializing a single object, guard the
9145 : destructors of any temporaries used in its initializer with
9146 : its destructor. */
9147 8060138 : if (cleanup)
9148 1044421 : wrap_temporary_cleanups (init, cleanup);
9149 :
9150 8060138 : gcc_assert (building_stmt_list_p ());
9151 8060138 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
9152 : /* Avoid CLEANUP_POINT_EXPR for the structured binding
9153 : bases, those will have CLEANUP_POINT_EXPR at the end of
9154 : code emitted by cp_finish_decomp. */
9155 8060138 : if (decomp)
9156 35144 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
9157 : else
9158 8024994 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
9159 8060138 : finish_expr_stmt (init);
9160 8060138 : current_stmt_tree ()->stmts_are_full_exprs_p
9161 8060138 : = saved_stmts_are_full_exprs_p;
9162 : }
9163 : }
9164 :
9165 : /* Set this to 0 so we can tell whether an aggregate which was
9166 : initialized was ever used. Don't do this if it has a
9167 : destructor, so we don't complain about the 'resource
9168 : allocation is initialization' idiom. Now set
9169 : attribute((unused)) on types so decls of that type will be
9170 : marked used. (see TREE_USED, above.) */
9171 25426770 : if (TYPE_NEEDS_CONSTRUCTING (type)
9172 2274758 : && ! already_used
9173 25451 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
9174 25449334 : && DECL_NAME (decl))
9175 22516 : TREE_USED (decl) = 0;
9176 25404254 : else if (already_used)
9177 22327202 : TREE_USED (decl) = 1;
9178 :
9179 25426770 : if (cleanup)
9180 1058571 : finish_decl_cleanup (decl, cleanup);
9181 : }
9182 :
9183 : /* DECL is a VAR_DECL for a compiler-generated variable with static
9184 : storage duration (like a virtual table) whose initializer is a
9185 : compile-time constant. Initialize the variable and provide it to the
9186 : back end. */
9187 :
9188 : void
9189 2271953 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
9190 : {
9191 2271953 : tree init;
9192 2271953 : gcc_assert (DECL_ARTIFICIAL (decl));
9193 2271953 : init = build_constructor (TREE_TYPE (decl), v);
9194 2271953 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
9195 2271953 : DECL_INITIAL (decl) = init;
9196 2271953 : DECL_INITIALIZED_P (decl) = 1;
9197 : /* Mark the decl as constexpr so that we can access its content
9198 : at compile time. */
9199 2271953 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9200 2271953 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
9201 2271953 : determine_visibility (decl);
9202 2271953 : layout_var_decl (decl);
9203 2271953 : maybe_commonize_var (decl);
9204 2271953 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
9205 2271953 : }
9206 :
9207 : /* INIT is the initializer for a variable, as represented by the
9208 : parser. Returns true iff INIT is value-dependent. */
9209 :
9210 : static bool
9211 10298898 : value_dependent_init_p (tree init)
9212 : {
9213 10298898 : if (TREE_CODE (init) == TREE_LIST)
9214 : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
9215 21750 : return any_value_dependent_elements_p (init);
9216 10277148 : else if (TREE_CODE (init) == CONSTRUCTOR)
9217 : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
9218 : {
9219 74425 : if (dependent_type_p (TREE_TYPE (init)))
9220 : return true;
9221 :
9222 74419 : vec<constructor_elt, va_gc> *elts;
9223 74419 : size_t nelts;
9224 74419 : size_t i;
9225 :
9226 74419 : elts = CONSTRUCTOR_ELTS (init);
9227 74419 : nelts = vec_safe_length (elts);
9228 656038 : for (i = 0; i < nelts; ++i)
9229 611163 : if (value_dependent_init_p ((*elts)[i].value))
9230 : return true;
9231 : }
9232 : else
9233 : /* It must be a simple expression, e.g., int i = 3; */
9234 10202723 : return value_dependent_expression_p (init);
9235 :
9236 : return false;
9237 : }
9238 :
9239 : /* A helper function to be called via walk_tree. If any label exists
9240 : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
9241 :
9242 : static tree
9243 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
9244 : {
9245 337 : if (TYPE_P (*tp))
9246 0 : *walk_subtrees = 0;
9247 337 : if (TREE_CODE (*tp) == LABEL_DECL)
9248 6 : cfun->has_forced_label_in_static = 1;
9249 337 : return NULL_TREE;
9250 : }
9251 :
9252 : /* Return true if DECL has either a trivial destructor, or for C++20
9253 : is constexpr and has a constexpr destructor. */
9254 :
9255 : static bool
9256 64908741 : decl_maybe_constant_destruction (tree decl, tree type)
9257 : {
9258 64908741 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
9259 64908741 : || (cxx_dialect >= cxx20
9260 4260177 : && VAR_P (decl)
9261 4259822 : && DECL_DECLARED_CONSTEXPR_P (decl)
9262 488 : && type_has_constexpr_destructor (strip_array_types (type))));
9263 : }
9264 :
9265 : static tree declare_simd_adjust_this (tree *, int *, void *);
9266 :
9267 : /* Helper function of omp_declare_variant_finalize. Finalize one
9268 : "omp declare variant base" attribute. Return true if it should be
9269 : removed. */
9270 :
9271 : static bool
9272 1773 : omp_declare_variant_finalize_one (tree decl, tree attr)
9273 : {
9274 1773 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9275 : {
9276 164 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
9277 : DECL_ARGUMENTS (decl), NULL);
9278 164 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
9279 : DECL_ARGUMENTS (decl), NULL);
9280 : }
9281 :
9282 1773 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
9283 1773 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
9284 : OMP_TRAIT_CONSTRUCT_SIMD);
9285 1773 : if (simd)
9286 : {
9287 69 : TREE_VALUE (simd)
9288 69 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
9289 69 : OMP_TS_PROPERTIES (simd));
9290 : /* FIXME, adjusting simd args unimplemented. */
9291 : }
9292 :
9293 1773 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
9294 1773 : location_t varid_loc
9295 1773 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
9296 1773 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
9297 1773 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
9298 1773 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
9299 :
9300 1773 : location_t save_loc = input_location;
9301 1773 : input_location = varid_loc;
9302 :
9303 1773 : releasing_vec args;
9304 1773 : tree parm = DECL_ARGUMENTS (decl);
9305 1773 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9306 164 : parm = DECL_CHAIN (parm);
9307 3239 : for (; parm; parm = DECL_CHAIN (parm))
9308 1466 : vec_safe_push (args, forward_parm (parm));
9309 :
9310 1773 : unsigned nappend_args = 0;
9311 1773 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9312 1773 : if (append_args_list)
9313 : {
9314 354 : append_args_list = TREE_VALUE (append_args_list);
9315 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9316 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
9317 : : NULL_TREE);
9318 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9319 387 : nappend_args++;
9320 354 : if (nappend_args)
9321 : {
9322 237 : tree type;
9323 237 : if ((type = lookup_qualified_name (global_namespace,
9324 : "omp_interop_t",
9325 : LOOK_want::NORMAL,
9326 : /*complain*/false)) == NULL_TREE
9327 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
9328 : {
9329 24 : location_t loc = input_location;
9330 24 : variant = tree_strip_any_location_wrapper (variant);
9331 24 : if (!identifier_p (variant))
9332 : {
9333 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9334 21 : variant = OVL_FIRST (variant);
9335 21 : loc = EXPR_LOC_OR_LOC (variant,
9336 : DECL_SOURCE_LOCATION (variant));
9337 : }
9338 24 : auto_diagnostic_group d;
9339 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9340 24 : args->length () + 1, variant);
9341 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9342 : "%<append_args%> specified here");
9343 24 : return true;
9344 24 : }
9345 564 : for (unsigned i = 0; i < nappend_args; i++)
9346 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
9347 : }
9348 : }
9349 :
9350 1749 : bool koenig_p = false;
9351 1749 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9352 : {
9353 1679 : if (identifier_p (variant)
9354 : /* In C++20, we may need to perform ADL for a template
9355 : name. */
9356 1655 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9357 18 : && identifier_p (TREE_OPERAND (variant, 0))))
9358 : {
9359 24 : if (!args->is_empty ())
9360 : {
9361 24 : koenig_p = true;
9362 24 : if (!any_type_dependent_arguments_p (args))
9363 21 : variant = perform_koenig_lookup (variant, args,
9364 : tf_warning_or_error);
9365 : }
9366 : else
9367 0 : variant = unqualified_fn_lookup_error (variant);
9368 : }
9369 1655 : else if (!args->is_empty () && is_overloaded_fn (variant))
9370 : {
9371 896 : tree fn = get_first_fn (variant);
9372 896 : fn = STRIP_TEMPLATE (fn);
9373 896 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9374 896 : || DECL_FUNCTION_MEMBER_P (fn)
9375 783 : || DECL_LOCAL_DECL_P (fn)))
9376 : {
9377 783 : koenig_p = true;
9378 783 : if (!any_type_dependent_arguments_p (args))
9379 729 : variant = perform_koenig_lookup (variant, args,
9380 : tf_warning_or_error);
9381 : }
9382 : }
9383 : }
9384 :
9385 1749 : if (idk == CP_ID_KIND_QUALIFIED)
9386 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9387 : koenig_p, tf_warning_or_error);
9388 1743 : else if (idk == CP_ID_KIND_NONE
9389 64 : && TREE_CODE (variant) == FUNCTION_DECL
9390 64 : && DECL_IOBJ_MEMBER_FUNCTION_P (variant)
9391 1791 : && CLASS_TYPE_P (DECL_CONTEXT (decl)))
9392 : {
9393 48 : tree saved_ccp = current_class_ptr;
9394 48 : tree saved_ccr = current_class_ref;
9395 48 : current_class_ptr = NULL_TREE;
9396 48 : current_class_ref = NULL_TREE;
9397 48 : inject_this_parameter (DECL_CONTEXT (decl), TYPE_UNQUALIFIED);
9398 48 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9399 : koenig_p, tf_warning_or_error);
9400 48 : current_class_ptr = saved_ccp;
9401 48 : current_class_ref = saved_ccr;
9402 : }
9403 : else
9404 1695 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9405 : koenig_p, tf_warning_or_error);
9406 1749 : if (variant == error_mark_node && !processing_template_decl)
9407 : return true;
9408 :
9409 1635 : if (TREE_CODE (variant) == TARGET_EXPR)
9410 9 : variant = TARGET_EXPR_INITIAL (variant);
9411 :
9412 3261 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9413 1635 : input_location = save_loc;
9414 :
9415 1635 : if (variant == decl)
9416 : {
9417 0 : error_at (varid_loc, "variant %qD is the same as base function",
9418 : variant);
9419 0 : return true;
9420 : }
9421 :
9422 1635 : if (variant)
9423 : {
9424 1548 : bool fail;
9425 1548 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9426 1548 : if (!nappend_args)
9427 1389 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9428 : COMPARE_STRICT);
9429 : else
9430 : {
9431 159 : unsigned nbase_args = 0;
9432 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9433 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9434 240 : nbase_args++;
9435 159 : tree vargs, varg;
9436 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9437 399 : for (unsigned i = 0; i < nbase_args && varg;
9438 240 : i++, varg = TREE_CHAIN (varg))
9439 240 : vargs = varg;
9440 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
9441 258 : varg = TREE_CHAIN (varg);
9442 159 : tree saved_vargs;
9443 159 : int saved_no_named_args_stdarg = 0;
9444 159 : if (nbase_args)
9445 : {
9446 111 : saved_vargs = TREE_CHAIN (vargs);
9447 111 : TREE_CHAIN (vargs) = varg;
9448 : }
9449 : else
9450 : {
9451 48 : saved_vargs = vargs;
9452 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9453 48 : saved_no_named_args_stdarg
9454 48 : = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9455 48 : if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9456 48 : && varg == NULL_TREE)
9457 2 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9458 : }
9459 : /* Skip assert check that TYPE_CANONICAL is the same. */
9460 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9461 : COMPARE_STRUCTURAL);
9462 159 : if (nbase_args)
9463 111 : TREE_CHAIN (vargs) = saved_vargs;
9464 : else
9465 : {
9466 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9467 48 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9468 96 : = saved_no_named_args_stdarg;
9469 : }
9470 159 : varg = saved_vargs;
9471 159 : if (!fail && !processing_template_decl)
9472 402 : for (unsigned i = 0; i < nappend_args;
9473 246 : i++, varg = TREE_CHAIN (varg))
9474 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9475 : {
9476 9 : auto_diagnostic_group d;
9477 9 : error_at (DECL_SOURCE_LOCATION (variant),
9478 : "argument %d of %qD must be of %<omp_interop_t%>",
9479 9 : nbase_args + i + 1, variant);
9480 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9481 : "%<append_args%> specified here");
9482 9 : break;
9483 9 : }
9484 : }
9485 1548 : if (fail)
9486 : {
9487 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
9488 : "types", variant, decl);
9489 69 : return true;
9490 : }
9491 1479 : if (fndecl_built_in_p (variant)
9492 1479 : && (startswith (varname, "__builtin_")
9493 0 : || startswith (varname, "__sync_")
9494 0 : || startswith (varname, "__atomic_")))
9495 : {
9496 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
9497 6 : return true;
9498 : }
9499 : else
9500 : {
9501 1473 : tree construct
9502 1473 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9503 1473 : omp_mark_declare_variant (match_loc, variant, construct);
9504 1473 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9505 : return true;
9506 1057 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9507 :
9508 : // Prepend adjust_args list to variant attributes
9509 1057 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9510 1057 : if (adjust_args_list != NULL_TREE)
9511 : {
9512 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
9513 285 : && TREE_VALUE (adjust_args_list))
9514 : {
9515 : /* Shift arg position for the added 'this' pointer. */
9516 : /* Handle need_device_ptr */
9517 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9518 51 : t; t = TREE_CHAIN (t))
9519 30 : TREE_VALUE (t)
9520 30 : = build_int_cst (TREE_TYPE (t),
9521 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
9522 : }
9523 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9524 : {
9525 : /* Shift likewise the number of args after which the
9526 : interop object should be added. */
9527 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9528 6 : TREE_PURPOSE (nargs)
9529 6 : = build_int_cst (TREE_TYPE (nargs),
9530 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9531 : }
9532 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9533 264 : TREE_VALUE (t)
9534 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9535 528 : DECL_ATTRIBUTES (variant) = tree_cons (
9536 : get_identifier ("omp declare variant variant args"),
9537 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9538 : }
9539 : }
9540 : }
9541 87 : else if (!processing_template_decl)
9542 : {
9543 0 : error_at (varid_loc, "could not find variant declaration");
9544 0 : return true;
9545 : }
9546 :
9547 : return false;
9548 1773 : }
9549 :
9550 : /* Helper function, finish up "omp declare variant base" attribute
9551 : now that there is a DECL. ATTR is the first "omp declare variant base"
9552 : attribute. */
9553 :
9554 : void
9555 1560 : omp_declare_variant_finalize (tree decl, tree attr)
9556 : {
9557 1560 : size_t attr_len = strlen ("omp declare variant base");
9558 1560 : tree *list = &DECL_ATTRIBUTES (decl);
9559 1560 : bool remove_all = false;
9560 1560 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
9561 1560 : if (TREE_CHAIN (TREE_VALUE (attr))
9562 1560 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9563 3120 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9564 1560 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9565 3120 : if (DECL_CONSTRUCTOR_P (decl))
9566 : {
9567 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9568 27 : remove_all = true;
9569 : }
9570 1533 : else if (DECL_DESTRUCTOR_P (decl))
9571 : {
9572 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9573 9 : remove_all = true;
9574 : }
9575 1524 : else if (DECL_DEFAULTED_FN (decl))
9576 : {
9577 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9578 2 : remove_all = true;
9579 : }
9580 1522 : else if (DECL_DELETED_FN (decl))
9581 : {
9582 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9583 2 : remove_all = true;
9584 : }
9585 1520 : else if (DECL_VIRTUAL_P (decl))
9586 : {
9587 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9588 0 : remove_all = true;
9589 : }
9590 : /* This loop is like private_lookup_attribute, except that it works
9591 : with tree * rather than tree, as we might want to remove the
9592 : attributes that are diagnosed as erroneous. */
9593 3385 : while (*list)
9594 : {
9595 1825 : tree attr = get_attribute_name (*list);
9596 1825 : size_t ident_len = IDENTIFIER_LENGTH (attr);
9597 4794 : if (cmp_attribs ("omp declare variant base", attr_len,
9598 1825 : IDENTIFIER_POINTER (attr), ident_len))
9599 : {
9600 1813 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
9601 : {
9602 669 : *list = TREE_CHAIN (*list);
9603 669 : continue;
9604 : }
9605 : }
9606 1156 : list = &TREE_CHAIN (*list);
9607 : }
9608 1560 : }
9609 :
9610 : /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
9611 : have a potentially throwing exception specification. */
9612 :
9613 : void
9614 212318875 : maybe_diagnose_deallocation_noexcept_false (tree decl)
9615 : {
9616 212318875 : if (cxx_dialect >= cxx29
9617 16859618 : && DECL_NAME (decl)
9618 16859618 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
9619 212359322 : && !IDENTIFIER_NEW_OP_P (DECL_NAME (decl)))
9620 : {
9621 22200 : tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
9622 22200 : if (spec && spec == noexcept_false_spec)
9623 15 : error_at (DECL_SOURCE_LOCATION (decl),
9624 : "deallocation function %qD declared possibly throwing",
9625 : decl);
9626 : }
9627 212318875 : }
9628 :
9629 : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9630 :
9631 : /* Finish processing of a declaration;
9632 : install its line number and initial value.
9633 : If the length of an array type is not known before,
9634 : it must be determined now, from the initial value, or it is an error.
9635 :
9636 : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9637 : true, then INIT is an integral constant expression.
9638 :
9639 : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9640 : if the (init) syntax was used.
9641 :
9642 : DECOMP is first identifier's DECL and identifier count in a structured
9643 : bindings, nullptr if not a structured binding. */
9644 :
9645 : void
9646 341861521 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9647 : tree asmspec_tree, int flags, cp_decomp *decomp)
9648 : {
9649 341861521 : vec<tree, va_gc> *cleanups = NULL;
9650 341861521 : const char *asmspec = NULL;
9651 341861521 : int was_readonly = 0;
9652 341861521 : bool var_definition_p = false;
9653 341861521 : tree auto_node;
9654 341861521 : auto_vec<tree> extra_cleanups;
9655 341861521 : tree aggregates1 = NULL_TREE;
9656 341861521 : struct decomp_cleanup {
9657 : tree decl;
9658 : cp_decomp *&decomp;
9659 341858824 : ~decomp_cleanup ()
9660 : {
9661 341858824 : if (decomp && DECL_DECOMPOSITION_P (decl))
9662 239935 : cp_finish_decomp (decl, decomp);
9663 341858824 : }
9664 341861521 : } decomp_cl = { decl, decomp };
9665 :
9666 341861521 : if (decl == error_mark_node)
9667 : return;
9668 341861481 : else if (! decl)
9669 : {
9670 0 : if (init)
9671 0 : error ("assignment (not initialization) in declaration");
9672 : return;
9673 : }
9674 :
9675 341861481 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9676 : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9677 341861481 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
9678 :
9679 341861481 : tree type = TREE_TYPE (decl);
9680 341861481 : if (type == error_mark_node)
9681 : return;
9682 :
9683 341860838 : if (VAR_P (decl) && is_copy_initialization (init))
9684 101922869 : flags |= LOOKUP_ONLYCONVERTING;
9685 :
9686 : /* Warn about register storage specifiers except when in GNU global
9687 : or local register variable extension. */
9688 341860838 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9689 : {
9690 1931 : if (cxx_dialect >= cxx17)
9691 1282 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9692 : "ISO C++17 does not allow %<register%> storage "
9693 : "class specifier");
9694 : else
9695 649 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9696 : "%<register%> storage class specifier used");
9697 : }
9698 :
9699 : /* If a name was specified, get the string. */
9700 341860838 : if (at_namespace_scope_p ())
9701 73871133 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9702 341860838 : if (asmspec_tree && asmspec_tree != error_mark_node)
9703 1047134 : asmspec = TREE_STRING_POINTER (asmspec_tree);
9704 :
9705 341860838 : bool in_class_decl
9706 341860838 : = (current_class_type
9707 215884971 : && CP_DECL_CONTEXT (decl) == current_class_type
9708 143097126 : && TYPE_BEING_DEFINED (current_class_type)
9709 482821181 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9710 :
9711 135673839 : if (in_class_decl
9712 135673839 : && (DECL_INITIAL (decl) || init))
9713 99571468 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9714 :
9715 341860838 : if (VAR_P (decl)
9716 341860838 : && (auto_node = type_uses_auto (type)))
9717 : {
9718 14216245 : tree d_init;
9719 14216245 : if (init == NULL_TREE)
9720 : {
9721 4610 : if (DECL_LANG_SPECIFIC (decl)
9722 4525 : && DECL_TEMPLATE_INSTANTIATION (decl)
9723 9132 : && !DECL_TEMPLATE_INSTANTIATED (decl))
9724 : {
9725 : /* init is null because we're deferring instantiating the
9726 : initializer until we need it. Well, we need it now. */
9727 4516 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9728 4516 : return;
9729 : }
9730 :
9731 94 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9732 : /* Class deduction with no initializer is OK. */;
9733 : else
9734 : {
9735 : /* Ordinary auto deduction without an initializer, a situation
9736 : which grokdeclarator already detects and rejects for the most
9737 : part. But we can still get here if we're instantiating a
9738 : variable template before we've fully parsed (and attached) its
9739 : initializer, e.g. template<class> auto x = x<int>; */
9740 3 : error_at (DECL_SOURCE_LOCATION (decl),
9741 : "declaration of %q#D has no initializer", decl);
9742 3 : TREE_TYPE (decl) = error_mark_node;
9743 3 : return;
9744 : }
9745 : }
9746 14211726 : d_init = init;
9747 14211726 : if (d_init)
9748 : {
9749 14211635 : if (TREE_CODE (d_init) == TREE_LIST
9750 14286478 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9751 48054 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9752 : tf_warning_or_error);
9753 14211635 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9754 : /* Force auto deduction now. Use tf_none to avoid redundant warnings
9755 : on deprecated-14.C. */
9756 14211635 : mark_single_function (d_init, tf_none);
9757 : }
9758 14211726 : enum auto_deduction_context adc = adc_variable_type;
9759 14211726 : if (DECL_DECOMPOSITION_P (decl))
9760 : adc = adc_decomp_type;
9761 14211726 : tree outer_targs = NULL_TREE;
9762 14211726 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9763 325 : && DECL_LANG_SPECIFIC (decl)
9764 126 : && DECL_TEMPLATE_INFO (decl)
9765 14211783 : && !DECL_FUNCTION_SCOPE_P (decl))
9766 : /* The outer template arguments might be needed for satisfaction.
9767 : (For function scope variables, do_auto_deduction will obtain the
9768 : outer template arguments from current_function_decl.) */
9769 57 : outer_targs = DECL_TI_ARGS (decl);
9770 14211726 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9771 : tf_warning_or_error, adc,
9772 : outer_targs, flags);
9773 14211726 : if (type == error_mark_node)
9774 : return;
9775 14210547 : if (TREE_CODE (type) == FUNCTION_TYPE)
9776 : {
9777 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
9778 : "did you forget the %<()%>?", decl);
9779 3 : TREE_TYPE (decl) = error_mark_node;
9780 3 : return;
9781 : }
9782 : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9783 14210544 : if (!processing_template_decl
9784 3194639 : && !type_uses_auto (type)
9785 17405176 : && !COMPLETE_TYPE_P (complete_type (type)))
9786 : {
9787 18 : auto_diagnostic_group d;
9788 18 : error_at (location_of (decl),
9789 : "deduced type %qT for %qD is incomplete", type, decl);
9790 18 : cxx_incomplete_type_inform (type);
9791 18 : TREE_TYPE (decl) = error_mark_node;
9792 18 : return;
9793 18 : }
9794 :
9795 : /* Now that we have a type, try these again. */
9796 14210526 : layout_decl (decl, 0);
9797 14210526 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9798 :
9799 : /* Update the type of the corresponding TEMPLATE_DECL to match. */
9800 14210526 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9801 : {
9802 40933 : tree tmpl = template_for_substitution (decl);
9803 40933 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9804 34403 : TREE_TYPE (tmpl) = type;
9805 : }
9806 : }
9807 :
9808 341855119 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9809 : {
9810 65 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9811 65 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9812 : {
9813 6 : init = NULL_TREE;
9814 6 : DECL_EXTERNAL (decl) = 1;
9815 : }
9816 : }
9817 :
9818 341855119 : if (VAR_P (decl)
9819 128920244 : && DECL_CLASS_SCOPE_P (decl)
9820 20919704 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
9821 : TCTX_STATIC_STORAGE, type)
9822 362774823 : && DECL_INITIALIZED_IN_CLASS_P (decl))
9823 19514223 : check_static_variable_definition (decl, type);
9824 :
9825 341855119 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
9826 : {
9827 38575431 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9828 38575431 : ? TCTX_THREAD_STORAGE
9829 : : TCTX_STATIC_STORAGE);
9830 38575431 : verify_type_context (input_location, context, TREE_TYPE (decl));
9831 : }
9832 :
9833 341855119 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
9834 : {
9835 599801 : tree clone;
9836 599801 : if (init == ridpointers[(int)RID_DELETE]
9837 599801 : || (TREE_CODE (init) == STRING_CST
9838 36 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9839 : {
9840 : /* FIXME check this is 1st decl. */
9841 576441 : if (UNLIKELY (DECL_MAIN_P (decl)))
9842 : {
9843 : /* [basic.start.main]/3: A program that defines main as deleted
9844 : is ill-formed. */
9845 6 : error ("%<::main%> cannot be deleted");
9846 6 : DECL_INITIAL (decl) = error_mark_node;
9847 : }
9848 : else
9849 : {
9850 576435 : DECL_DELETED_FN (decl) = 1;
9851 576435 : DECL_DECLARED_INLINE_P (decl) = 1;
9852 576435 : DECL_INITIAL (decl)
9853 576435 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9854 576453 : FOR_EACH_CLONE (clone, decl)
9855 : {
9856 18 : DECL_DELETED_FN (clone) = 1;
9857 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9858 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9859 : }
9860 : }
9861 576441 : init = NULL_TREE;
9862 : }
9863 23360 : else if (init == ridpointers[(int)RID_DEFAULT])
9864 : {
9865 23348 : if (defaultable_fn_check (decl))
9866 23324 : DECL_DEFAULTED_FN (decl) = 1;
9867 : else
9868 24 : DECL_INITIAL (decl) = NULL_TREE;
9869 : }
9870 : }
9871 :
9872 341855119 : if (init && VAR_P (decl))
9873 : {
9874 112116740 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9875 : /* If DECL is a reference, then we want to know whether init is a
9876 : reference constant; init_const_expr_p as passed tells us whether
9877 : it's an rvalue constant. */
9878 112116740 : if (TYPE_REF_P (type))
9879 2419902 : init_const_expr_p = potential_constant_expression (init);
9880 112116740 : if (init_const_expr_p)
9881 : {
9882 : /* Set these flags now for templates. We'll update the flags in
9883 : store_init_value for instantiations. */
9884 98750492 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9885 98750492 : if (decl_maybe_constant_var_p (decl)
9886 : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9887 98750492 : && !TYPE_REF_P (type))
9888 43522901 : TREE_CONSTANT (decl) = true;
9889 : }
9890 : /* This is handled mostly by gimplify.cc, but we have to deal with
9891 : not warning about int x = x; as it is a GCC extension to turn off
9892 : this warning but only if warn_init_self is zero. */
9893 112116740 : if (!DECL_EXTERNAL (decl)
9894 110429804 : && !TREE_STATIC (decl)
9895 74620801 : && decl == tree_strip_any_location_wrapper (init)
9896 222546544 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9897 3760 : suppress_warning (decl, OPT_Winit_self);
9898 : }
9899 229738379 : else if (VAR_P (decl)
9900 16803504 : && COMPLETE_TYPE_P (type)
9901 13224358 : && !TYPE_REF_P (type)
9902 13223650 : && !dependent_type_p (type)
9903 242623675 : && is_really_empty_class (type, /*ignore_vptr*/false))
9904 : /* We have no initializer but there's nothing to initialize anyway.
9905 : Treat DECL as constant due to c++/109876. */
9906 604798 : TREE_CONSTANT (decl) = true;
9907 :
9908 341855119 : if (flag_openmp
9909 855628 : && TREE_CODE (decl) == FUNCTION_DECL
9910 : /* #pragma omp declare variant on methods handled in finish_struct
9911 : instead. */
9912 342226355 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9913 140861 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9914 230838 : if (tree attr = lookup_attribute ("omp declare variant base",
9915 230838 : DECL_ATTRIBUTES (decl)))
9916 970 : omp_declare_variant_finalize (decl, attr);
9917 :
9918 341855119 : if (processing_template_decl)
9919 : {
9920 196705001 : bool type_dependent_p;
9921 :
9922 : /* Add this declaration to the statement-tree. */
9923 196705001 : if (at_function_scope_p ())
9924 70755703 : add_decl_expr (decl);
9925 :
9926 196705001 : type_dependent_p = dependent_type_p (type);
9927 :
9928 196705001 : if (check_for_bare_parameter_packs (init))
9929 : {
9930 3 : init = NULL_TREE;
9931 3 : DECL_INITIAL (decl) = NULL_TREE;
9932 : }
9933 :
9934 : /* Generally, initializers in templates are expanded when the
9935 : template is instantiated. But, if DECL is a variable constant
9936 : then it can be used in future constant expressions, so its value
9937 : must be available. */
9938 :
9939 196705001 : bool dep_init = false;
9940 :
9941 196705001 : if (!VAR_P (decl) || type_dependent_p)
9942 : /* We can't do anything if the decl has dependent type. */;
9943 24609817 : else if (init
9944 22854312 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9945 18646271 : && !TYPE_REF_P (type)
9946 18611781 : && decl_maybe_constant_var_p (decl)
9947 34297552 : && !(dep_init = value_dependent_init_p (init)))
9948 : {
9949 : /* This variable seems to be a non-dependent constant, so process
9950 : its initializer. If check_initializer returns non-null the
9951 : initialization wasn't constant after all. */
9952 2501217 : tree init_code;
9953 2501217 : cleanups = make_tree_vector ();
9954 2501217 : init_code = check_initializer (decl, init, flags, &cleanups);
9955 2501217 : if (init_code == NULL_TREE)
9956 2501217 : init = NULL_TREE;
9957 2501217 : release_tree_vector (cleanups);
9958 : }
9959 : else
9960 : {
9961 22108600 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9962 : /* Try to deduce array size. */
9963 22108600 : maybe_deduce_size_from_array_init (decl, init);
9964 : /* And complain about multiple initializers. */
9965 20353095 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9966 22216198 : && !MAYBE_CLASS_TYPE_P (type))
9967 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9968 : tf_warning_or_error);
9969 : }
9970 :
9971 196705001 : if (init)
9972 58909103 : DECL_INITIAL (decl) = init;
9973 :
9974 196705001 : if (dep_init)
9975 : {
9976 7186518 : retrofit_lang_decl (decl);
9977 7186518 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9978 : }
9979 :
9980 196705001 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9981 : {
9982 9 : if (TREE_ADDRESSABLE (decl))
9983 3 : error_at (DECL_SOURCE_LOCATION (decl),
9984 : "address of explicit register variable %qD requested",
9985 : decl);
9986 : else
9987 : {
9988 6 : set_user_assembler_name (decl, asmspec);
9989 6 : DECL_HARD_REGISTER (decl) = 1;
9990 : }
9991 : }
9992 : return;
9993 : }
9994 :
9995 : /* Just store non-static data member initializers for later. */
9996 145150118 : if (init && TREE_CODE (decl) == FIELD_DECL)
9997 620237 : DECL_INITIAL (decl) = init;
9998 :
9999 : /* Take care of TYPE_DECLs up front. */
10000 145150118 : if (TREE_CODE (decl) == TYPE_DECL)
10001 : {
10002 11322466 : if (type != error_mark_node
10003 11322466 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
10004 : {
10005 5397925 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
10006 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
10007 5397925 : set_identifier_type_value (DECL_NAME (decl), decl);
10008 : }
10009 :
10010 : /* If we have installed this as the canonical typedef for this
10011 : type, and that type has not been defined yet, delay emitting
10012 : the debug information for it, as we will emit it later. */
10013 11322466 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
10014 11322466 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10015 629489 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
10016 :
10017 11322466 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
10018 : at_eof);
10019 11322466 : return;
10020 : }
10021 :
10022 : /* A reference will be modified here, as it is initialized. */
10023 133827652 : if (! DECL_EXTERNAL (decl)
10024 65567791 : && TREE_READONLY (decl)
10025 173032226 : && TYPE_REF_P (type))
10026 : {
10027 255644 : was_readonly = 1;
10028 255644 : TREE_READONLY (decl) = 0;
10029 : }
10030 :
10031 : /* This needs to happen before extend_ref_init_temps. */
10032 133827652 : if (VAR_OR_FUNCTION_DECL_P (decl))
10033 : {
10034 124154641 : if (VAR_P (decl))
10035 64002380 : maybe_commonize_var (decl);
10036 124154641 : determine_visibility (decl);
10037 : }
10038 :
10039 133827652 : if (VAR_P (decl))
10040 : {
10041 64002380 : duration_kind dk = decl_storage_duration (decl);
10042 : /* [dcl.constinit]/1 "The constinit specifier shall be applied
10043 : only to a declaration of a variable with static or thread storage
10044 : duration." */
10045 64002380 : if (DECL_DECLARED_CONSTINIT_P (decl)
10046 64002380 : && !(dk == dk_thread || dk == dk_static))
10047 : {
10048 24 : error_at (DECL_SOURCE_LOCATION (decl),
10049 : "%<constinit%> can only be applied to a variable with "
10050 : "static or thread storage duration");
10051 24 : return;
10052 : }
10053 :
10054 64002356 : if (decomp)
10055 : {
10056 68921 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
10057 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
10058 : "%<constinit%> can be applied to structured binding "
10059 : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
10060 68921 : cp_maybe_mangle_decomp (decl, decomp);
10061 68921 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
10062 : {
10063 628 : if (CP_DECL_THREAD_LOCAL_P (decl))
10064 37 : aggregates1 = tls_aggregates;
10065 : else
10066 591 : aggregates1 = static_aggregates;
10067 : }
10068 : }
10069 :
10070 : /* If this is a local variable that will need a mangled name,
10071 : register it now. We must do this before processing the
10072 : initializer for the variable, since the initialization might
10073 : require a guard variable, and since the mangled name of the
10074 : guard variable will depend on the mangled name of this
10075 : variable. */
10076 64002356 : if (DECL_FUNCTION_SCOPE_P (decl) && TREE_STATIC (decl))
10077 : {
10078 : /* The variable holding an anonymous union will have had its
10079 : discriminator set in finish_anon_union, after which it's
10080 : NAME will have been cleared. */
10081 143704 : if (DECL_NAME (decl))
10082 143385 : determine_local_discriminator (decl);
10083 : /* Normally has_forced_label_in_static is set during GIMPLE
10084 : lowering, but [cd]tors are never actually compiled directly.
10085 : We need to set this early so we can deal with the label
10086 : address extension. */
10087 143704 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
10088 143663 : || DECL_DESTRUCTOR_P (current_function_decl))
10089 143720 : && init)
10090 : {
10091 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
10092 29 : add_local_decl (cfun, decl);
10093 : }
10094 : /* And make sure it's in the symbol table for
10095 : c_parse_final_cleanups to find. */
10096 143704 : gcc_checking_assert (!consteval_only_p (decl));
10097 143704 : varpool_node::get_create (decl);
10098 : }
10099 :
10100 64002356 : if (flag_openmp
10101 194869 : && VAR_P (decl)
10102 194869 : && DECL_LANG_SPECIFIC (decl)
10103 115947 : && DECL_OMP_DECLARE_MAPPER_P (decl)
10104 64002369 : && init)
10105 : {
10106 12 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
10107 12 : DECL_INITIAL (decl) = init;
10108 : }
10109 : /* Convert the initializer to the type of DECL, if we have not
10110 : already initialized DECL. */
10111 64002344 : else if (!DECL_INITIALIZED_P (decl)
10112 : /* If !DECL_EXTERNAL then DECL is being defined. In the
10113 : case of a static data member initialized inside the
10114 : class-specifier, there can be an initializer even if DECL
10115 : is *not* defined. */
10116 64002344 : && (!DECL_EXTERNAL (decl) || init))
10117 : {
10118 56941861 : cleanups = make_tree_vector ();
10119 56941861 : init = check_initializer (decl, init, flags, &cleanups);
10120 :
10121 : /* Handle:
10122 :
10123 : [dcl.init]
10124 :
10125 : The memory occupied by any object of static storage
10126 : duration is zero-initialized at program startup before
10127 : any other initialization takes place.
10128 :
10129 : We cannot create an appropriate initializer until after
10130 : the type of DECL is finalized. If DECL_INITIAL is set,
10131 : then the DECL is statically initialized, and any
10132 : necessary zero-initialization has already been performed. */
10133 56939164 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
10134 528235 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
10135 : /*nelts=*/NULL_TREE,
10136 : /*static_storage_p=*/true);
10137 : /* Remember that the initialization for this variable has
10138 : taken place. */
10139 56939164 : DECL_INITIALIZED_P (decl) = 1;
10140 : /* This declaration is the definition of this variable,
10141 : unless we are initializing a static data member within
10142 : the class specifier. */
10143 56939164 : if (!DECL_EXTERNAL (decl))
10144 63999659 : var_definition_p = true;
10145 : }
10146 : /* If the variable has an array type, lay out the type, even if
10147 : there is no initializer. It is valid to index through the
10148 : array, and we must get TYPE_ALIGN set correctly on the array
10149 : type. */
10150 7060483 : else if (TREE_CODE (type) == ARRAY_TYPE)
10151 222667 : layout_type (type);
10152 :
10153 63999659 : if (TREE_STATIC (decl)
10154 38003160 : && !at_function_scope_p ()
10155 101859115 : && current_function_decl == NULL)
10156 : /* So decl is a global variable or a static member of a
10157 : non local class. Record the types it uses
10158 : so that we can decide later to emit debug info for them. */
10159 37859444 : record_types_used_by_current_var_decl (decl);
10160 : }
10161 :
10162 : /* Add this declaration to the statement-tree. This needs to happen
10163 : after the call to check_initializer so that the DECL_EXPR for a
10164 : reference temp is added before the DECL_EXPR for the reference itself. */
10165 133824931 : if (DECL_FUNCTION_SCOPE_P (decl))
10166 : {
10167 : /* If we're building a variable sized type, and we might be
10168 : reachable other than via the top of the current binding
10169 : level, then create a new BIND_EXPR so that we deallocate
10170 : the object at the right time. */
10171 25570501 : if (VAR_P (decl)
10172 25570501 : && DECL_SIZE (decl)
10173 25507827 : && !TREE_CONSTANT (DECL_SIZE (decl))
10174 25571532 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
10175 : {
10176 3 : tree bind;
10177 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
10178 3 : TREE_SIDE_EFFECTS (bind) = 1;
10179 3 : add_stmt (bind);
10180 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
10181 : }
10182 25570501 : add_decl_expr (decl);
10183 : }
10184 :
10185 133824931 : tree decomp_init = NULL_TREE;
10186 : /* Let the middle end know about variables and functions -- but not
10187 : static data members in uninstantiated class templates. */
10188 133824931 : if (VAR_OR_FUNCTION_DECL_P (decl))
10189 : {
10190 124151920 : if (VAR_P (decl))
10191 : {
10192 63999659 : layout_var_decl (decl);
10193 63999659 : if (!flag_weak)
10194 : /* Check again now that we have an initializer. */
10195 57 : maybe_commonize_var (decl);
10196 : /* A class-scope constexpr variable with an out-of-class declaration.
10197 : C++17 makes them implicitly inline, but still force it out. */
10198 86400540 : if (DECL_INLINE_VAR_P (decl)
10199 30114320 : && !DECL_VAR_DECLARED_INLINE_P (decl)
10200 13584229 : && !DECL_TEMPLATE_INSTANTIATION (decl)
10201 48415478 : && !in_class_decl)
10202 108 : mark_needed (decl);
10203 : }
10204 :
10205 124151920 : if (var_definition_p
10206 : /* With -fmerge-all-constants, gimplify_init_constructor
10207 : might add TREE_STATIC to aggregate variables. */
10208 55724617 : && (TREE_STATIC (decl)
10209 25426925 : || (flag_merge_constants >= 2
10210 38 : && AGGREGATE_TYPE_P (type))))
10211 : {
10212 : /* If a TREE_READONLY variable needs initialization
10213 : at runtime, it is no longer readonly and we need to
10214 : avoid MEM_READONLY_P being set on RTL created for it. */
10215 30297695 : if (init)
10216 : {
10217 14624 : if (TREE_READONLY (decl))
10218 346 : TREE_READONLY (decl) = 0;
10219 : was_readonly = 0;
10220 : }
10221 30283071 : else if (was_readonly)
10222 2676 : TREE_READONLY (decl) = 1;
10223 :
10224 : /* Likewise if it needs destruction. */
10225 30297695 : if (!decl_maybe_constant_destruction (decl, type))
10226 3677 : TREE_READONLY (decl) = 0;
10227 : }
10228 93854225 : else if (VAR_P (decl)
10229 33701964 : && CP_DECL_THREAD_LOCAL_P (decl)
10230 19433 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
10231 19433 : && (was_readonly || TREE_READONLY (decl))
10232 93854240 : && var_needs_tls_wrapper (decl))
10233 : {
10234 : /* TLS variables need dynamic initialization by the TLS wrapper
10235 : function, we don't want to hoist accesses to it before the
10236 : wrapper. */
10237 6 : was_readonly = 0;
10238 6 : TREE_READONLY (decl) = 0;
10239 : }
10240 :
10241 124151920 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
10242 :
10243 : /* Check for abstractness of the type. */
10244 124151920 : if (var_definition_p)
10245 55724617 : abstract_virtuals_error (decl, type);
10246 :
10247 124151920 : if (decomp && !cp_finish_decomp (decl, decomp, true))
10248 33023 : decomp = NULL;
10249 :
10250 124151920 : if (TREE_TYPE (decl) == error_mark_node)
10251 : /* No initialization required. */
10252 : ;
10253 124151827 : else if (TREE_CODE (decl) == FUNCTION_DECL)
10254 : {
10255 60152261 : if (init)
10256 : {
10257 602 : if (init == ridpointers[(int)RID_DEFAULT])
10258 : {
10259 : /* An out-of-class default definition is defined at
10260 : the point where it is explicitly defaulted. */
10261 590 : if (DECL_DELETED_FN (decl))
10262 17 : maybe_explain_implicit_delete (decl);
10263 573 : else if (DECL_INITIAL (decl) == error_mark_node)
10264 549 : synthesize_method (decl);
10265 : }
10266 : else
10267 24 : error_at (cp_expr_loc_or_loc (init,
10268 12 : DECL_SOURCE_LOCATION (decl)),
10269 : "function %q#D is initialized like a variable",
10270 : decl);
10271 : }
10272 : /* else no initialization required. */
10273 : }
10274 63999566 : else if (DECL_EXTERNAL (decl)
10275 63999566 : && ! (DECL_LANG_SPECIFIC (decl)
10276 7968236 : && DECL_NOT_REALLY_EXTERN (decl)))
10277 : {
10278 : /* check_initializer will have done any constant initialization. */
10279 : }
10280 : /* A variable definition. */
10281 57695286 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
10282 : {
10283 : /* Initialize the local variable. */
10284 25426770 : if (!decomp)
10285 25391126 : initialize_local_var (decl, init, false);
10286 : else
10287 : {
10288 35644 : tree cleanup = NULL_TREE;
10289 35644 : if (DECL_SIZE (decl))
10290 35644 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10291 : /* If cp_finish_decomp needs to emit any code, we need to emit that
10292 : code after code emitted by initialize_local_var in a single
10293 : CLEANUP_POINT_EXPR, so that temporaries are destructed only
10294 : after the cp_finish_decomp emitted code.
10295 : If there are any cleanups, either extend_ref_init_temps
10296 : created ones or e.g. array destruction, push those first
10297 : with the cleanups guarded on a bool temporary, initially
10298 : set to false and set to true after initialize_local_var
10299 : emitted code. */
10300 35644 : tree guard = NULL_TREE;
10301 35644 : if (cleanups || cleanup)
10302 : {
10303 : /* Since the CLEANUP_STMT will refer to the guard, we need it
10304 : to be a variable with the same lifetime. ??? It might be
10305 : better to use wrap_temporary_cleanups. */
10306 35644 : guard = get_temp_regvar (boolean_type_node, boolean_false_node);
10307 : /* And make sure register_local_var_uses sees it. */
10308 35644 : pushdecl (guard);
10309 : }
10310 :
10311 35644 : tree sl = push_stmt_list ();
10312 35644 : initialize_local_var (decl, init, true);
10313 35644 : if (guard)
10314 : {
10315 35644 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
10316 : guard, boolean_true_node));
10317 35662 : for (tree &t : *cleanups)
10318 18 : t = build3 (COND_EXPR, void_type_node,
10319 : guard, t, void_node);
10320 35644 : if (cleanup)
10321 32 : cleanup = build3 (COND_EXPR, void_type_node,
10322 : guard, cleanup, void_node);
10323 : }
10324 35644 : unsigned before = stmt_list_stack->length ();
10325 35644 : cp_finish_decomp (decl, decomp);
10326 35644 : decomp = NULL;
10327 35644 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
10328 35644 : sl = pop_stmt_list (sl);
10329 35644 : if (n_extra_cleanups)
10330 : {
10331 : /* If cp_finish_decomp needs any cleanups, such as for
10332 : extend_ref_init_temps created vars, pop_stmt_list
10333 : popped that all, so push those extra cleanups around
10334 : the whole sequence with a guard variable. */
10335 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
10336 15 : guard = get_temp_regvar (integer_type_node, integer_zero_node);
10337 15 : pushdecl (guard);
10338 78 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
10339 : {
10340 48 : tree_stmt_iterator tsi = tsi_last (sl);
10341 48 : gcc_assert (!tsi_end_p (tsi));
10342 48 : tree last = tsi_stmt (tsi);
10343 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
10344 : && !CLEANUP_EH_ONLY (last));
10345 48 : tree cst = build_int_cst (integer_type_node, i + 1);
10346 96 : tree cl = build3 (COND_EXPR, void_type_node,
10347 : build2 (GE_EXPR, boolean_type_node,
10348 : guard, cst),
10349 48 : CLEANUP_EXPR (last), void_node);
10350 48 : extra_cleanups.safe_push (cl);
10351 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
10352 : integer_type_node,
10353 : guard, cst),
10354 : TSI_SAME_STMT);
10355 48 : tree sl2 = CLEANUP_BODY (last);
10356 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10357 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10358 48 : tsi_delink (&tsi);
10359 : }
10360 : }
10361 35644 : decomp_init = maybe_cleanup_point_expr_void (sl);
10362 35644 : if (cleanup)
10363 32 : finish_decl_cleanup (decl, cleanup);
10364 : }
10365 : }
10366 :
10367 : /* If a variable is defined, and then a subsequent
10368 : definition with external linkage is encountered, we will
10369 : get here twice for the same variable. We want to avoid
10370 : calling expand_static_init more than once. For variables
10371 : that are not static data members, we can call
10372 : expand_static_init only when we actually process the
10373 : initializer. It is not legal to redeclare a static data
10374 : member, so this issue does not arise in that case. */
10375 32268516 : else if (var_definition_p && TREE_STATIC (decl))
10376 : {
10377 30296152 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10378 : {
10379 106 : tree sl = push_stmt_list ();
10380 106 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10381 106 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10382 106 : expand_static_init (decl, init);
10383 106 : current_stmt_tree ()->stmts_are_full_exprs_p
10384 106 : = saved_stmts_are_full_exprs_p;
10385 106 : cp_finish_decomp (decl, decomp);
10386 106 : decomp = NULL;
10387 106 : sl = pop_stmt_list (sl);
10388 106 : sl = maybe_cleanup_point_expr_void (sl);
10389 106 : add_stmt (sl);
10390 : }
10391 : else
10392 30296046 : expand_static_init (decl, init);
10393 : }
10394 : }
10395 :
10396 : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10397 : reference, insert it in the statement-tree now. */
10398 133824931 : if (cleanups)
10399 : {
10400 56942608 : for (tree t : *cleanups)
10401 : {
10402 3444 : push_cleanup (NULL_TREE, t, false);
10403 : /* As in initialize_local_var. */
10404 3444 : wrap_temporary_cleanups (init, t);
10405 : }
10406 56939164 : release_tree_vector (cleanups);
10407 : }
10408 :
10409 133825009 : for (tree t : &extra_cleanups)
10410 48 : push_cleanup (NULL_TREE, t, false);
10411 :
10412 133824931 : if (decomp_init)
10413 35644 : add_stmt (decomp_init);
10414 :
10415 133824931 : if (decomp
10416 148 : && var_definition_p
10417 148 : && TREE_STATIC (decl)
10418 133825079 : && !DECL_FUNCTION_SCOPE_P (decl))
10419 : {
10420 146 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10421 146 : ? tls_aggregates : static_aggregates);
10422 146 : tree aggregates2 = aggregates3;
10423 146 : if (aggregates2 != aggregates1)
10424 : {
10425 66 : cp_finish_decomp (decl, decomp);
10426 66 : decomp = NULL;
10427 66 : if (aggregates3 != aggregates2)
10428 : {
10429 : /* If there are dynamic initializers for the structured
10430 : binding base or associated extended ref temps and also
10431 : dynamic initializers for the structured binding non-base
10432 : vars, mark them. */
10433 267 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10434 204 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10435 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10436 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
10437 : }
10438 : }
10439 : }
10440 :
10441 133824931 : if (was_readonly)
10442 255074 : TREE_READONLY (decl) = 1;
10443 :
10444 133824931 : if (flag_openmp
10445 444017 : && VAR_P (decl)
10446 134019800 : && lookup_attribute ("omp declare target implicit",
10447 194869 : DECL_ATTRIBUTES (decl)))
10448 : {
10449 52 : DECL_ATTRIBUTES (decl)
10450 52 : = remove_attribute ("omp declare target implicit",
10451 52 : DECL_ATTRIBUTES (decl));
10452 52 : complete_type (TREE_TYPE (decl));
10453 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
10454 : {
10455 30 : auto_diagnostic_group d;
10456 30 : error ("%q+D in declare target directive does not have mappable"
10457 : " type", decl);
10458 30 : if (TREE_TYPE (decl) != error_mark_node
10459 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10460 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
10461 30 : }
10462 22 : else if (!lookup_attribute ("omp declare target",
10463 22 : DECL_ATTRIBUTES (decl))
10464 44 : && !lookup_attribute ("omp declare target link",
10465 22 : DECL_ATTRIBUTES (decl)))
10466 : {
10467 22 : DECL_ATTRIBUTES (decl)
10468 22 : = tree_cons (get_identifier ("omp declare target"),
10469 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
10470 22 : symtab_node *node = symtab_node::get (decl);
10471 22 : if (node != NULL)
10472 : {
10473 19 : node->offloadable = 1;
10474 19 : if (ENABLE_OFFLOADING)
10475 : {
10476 : g->have_offload = true;
10477 : if (is_a <varpool_node *> (node))
10478 : vec_safe_push (offload_vars, decl);
10479 : }
10480 : }
10481 : }
10482 : }
10483 :
10484 : /* This is the last point we can lower alignment so give the target the
10485 : chance to do so. */
10486 133824931 : if (VAR_P (decl)
10487 63999659 : && !is_global_var (decl)
10488 159251856 : && !DECL_HARD_REGISTER (decl))
10489 25426872 : targetm.lower_local_decl_alignment (decl);
10490 :
10491 133824931 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
10492 341858824 : }
10493 :
10494 : /* For class TYPE return itself or some its bases that contain
10495 : any direct non-static data members. Return error_mark_node if an
10496 : error has been diagnosed. */
10497 :
10498 : static tree
10499 31618 : find_decomp_class_base (location_t loc, tree type, tree ret,
10500 : tsubst_flags_t complain)
10501 : {
10502 63236 : if (LAMBDA_TYPE_P (type))
10503 : {
10504 12 : if (complain & tf_error)
10505 : {
10506 12 : auto_diagnostic_group d;
10507 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
10508 12 : inform (location_of (type), "lambda declared here");
10509 12 : }
10510 12 : return error_mark_node;
10511 : }
10512 :
10513 31606 : bool member_seen = false;
10514 401779 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10515 676509 : if (TREE_CODE (field) != FIELD_DECL
10516 64051 : || DECL_ARTIFICIAL (field)
10517 434163 : || DECL_UNNAMED_BIT_FIELD (field))
10518 306297 : continue;
10519 63915 : else if (ret)
10520 : return type;
10521 63889 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10522 : {
10523 8 : if ((complain & tf_error) == 0)
10524 0 : return error_mark_node;
10525 8 : auto_diagnostic_group d;
10526 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10527 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10528 : "anonymous struct member", type);
10529 : else
10530 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10531 : "anonymous union member", type);
10532 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
10533 8 : return error_mark_node;
10534 8 : }
10535 63881 : else if (!accessible_p (type, field, true))
10536 : {
10537 5 : if ((complain & tf_error) == 0)
10538 0 : return error_mark_node;
10539 5 : auto_diagnostic_group d;
10540 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10541 : field, type);
10542 5 : inform (DECL_SOURCE_LOCATION (field),
10543 5 : TREE_PRIVATE (field)
10544 : ? G_("declared private here")
10545 : : G_("declared protected here"));
10546 5 : return error_mark_node;
10547 5 : }
10548 : else
10549 : member_seen = true;
10550 :
10551 31567 : tree base_binfo, binfo;
10552 31567 : tree orig_ret = ret;
10553 31567 : int i;
10554 31567 : if (member_seen)
10555 31464 : ret = type;
10556 31631 : for (binfo = TYPE_BINFO (type), i = 0;
10557 31631 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10558 : {
10559 84 : auto_diagnostic_group d;
10560 84 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10561 : complain);
10562 84 : if (t == error_mark_node)
10563 : {
10564 3 : if (complain & tf_error)
10565 3 : inform (location_of (type), "in base class of %qT", type);
10566 3 : return error_mark_node;
10567 : }
10568 81 : if (t != NULL_TREE && t != ret)
10569 : {
10570 54 : if (ret == type)
10571 : {
10572 10 : if (complain & tf_error)
10573 10 : error_at (loc, "cannot decompose class type %qT: both it and "
10574 : "its base class %qT have non-static data "
10575 : "members", type, t);
10576 10 : return error_mark_node;
10577 : }
10578 44 : else if (orig_ret != NULL_TREE)
10579 : return t;
10580 44 : else if (ret != NULL_TREE)
10581 : {
10582 7 : if (complain & tf_error)
10583 7 : error_at (loc, "cannot decompose class type %qT: its base "
10584 : "classes %qT and %qT have non-static data "
10585 : "members", type, ret, t);
10586 7 : return error_mark_node;
10587 : }
10588 : else
10589 : ret = t;
10590 : }
10591 84 : }
10592 : return ret;
10593 : }
10594 :
10595 : /* Return std::tuple_size<TYPE>::value. */
10596 :
10597 : static tree
10598 113368 : get_tuple_size (tree type)
10599 : {
10600 113368 : tree args = make_tree_vec (1);
10601 113368 : TREE_VEC_ELT (args, 0) = type;
10602 113368 : tree inst = lookup_template_class (tuple_size_identifier, args,
10603 : /*in_decl*/NULL_TREE,
10604 : /*context*/std_node,
10605 : tf_none);
10606 113368 : inst = complete_type (inst);
10607 113368 : if (inst == error_mark_node
10608 111854 : || !COMPLETE_TYPE_P (inst)
10609 195160 : || !CLASS_TYPE_P (inst))
10610 : return NULL_TREE;
10611 81792 : tree val = lookup_qualified_name (inst, value_identifier,
10612 : LOOK_want::NORMAL, /*complain*/false);
10613 81792 : if (val == error_mark_node)
10614 : return NULL_TREE;
10615 81786 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10616 81786 : val = maybe_constant_value (val, NULL_TREE, mce_true);
10617 81786 : if (TREE_CODE (val) == INTEGER_CST)
10618 : return val;
10619 : else
10620 6 : return error_mark_node;
10621 : }
10622 :
10623 : /* Return std::tuple_element<I,TYPE>::type. */
10624 :
10625 : static tree
10626 92343 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10627 : {
10628 92343 : tree args = make_tree_vec (2);
10629 92343 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10630 92343 : TREE_VEC_ELT (args, 1) = type;
10631 92343 : tree inst = lookup_template_class (tuple_element_identifier, args,
10632 : /*in_decl*/NULL_TREE,
10633 : /*context*/std_node,
10634 : tf_warning_or_error);
10635 92343 : return make_typename_type (inst, type_identifier,
10636 92343 : none_type, tf_warning_or_error);
10637 : }
10638 :
10639 : /* Return e.get<i>() or get<i>(e). */
10640 :
10641 : static tree
10642 92352 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10643 : {
10644 92352 : tree targs = make_tree_vec (1);
10645 92352 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10646 :
10647 92352 : tree etype = TREE_TYPE (decl);
10648 92352 : tree e = convert_from_reference (decl);
10649 :
10650 : /* [The id-expression] e is an lvalue if the type of the entity e is an
10651 : lvalue reference and an xvalue otherwise. */
10652 92352 : if (!TYPE_REF_P (etype)
10653 92352 : || TYPE_REF_IS_RVALUE (etype))
10654 90980 : e = move (e);
10655 :
10656 92352 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10657 : LOOK_want::NORMAL, /*complain*/false);
10658 92352 : bool use_member_get = false;
10659 :
10660 : /* To use a member get, member lookup must find at least one
10661 : declaration that is a function template
10662 : whose first template parameter is a non-type parameter. */
10663 274570 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10664 : {
10665 92352 : tree fn = *iter;
10666 92352 : if (TREE_CODE (fn) == TEMPLATE_DECL)
10667 : {
10668 2489 : tree tparms = DECL_TEMPLATE_PARMS (fn);
10669 2489 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10670 2489 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10671 : {
10672 : use_member_get = true;
10673 : break;
10674 : }
10675 : }
10676 : }
10677 :
10678 92352 : if (use_member_get)
10679 : {
10680 2486 : fns = lookup_template_function (fns, targs);
10681 2486 : return build_new_method_call (e, fns, /*args*/NULL,
10682 : /*path*/NULL_TREE, LOOKUP_NORMAL,
10683 2486 : /*fn_p*/NULL, tf_warning_or_error);
10684 : }
10685 : else
10686 : {
10687 89866 : releasing_vec args (make_tree_vector_single (e));
10688 89866 : fns = lookup_template_function (get__identifier, targs);
10689 89866 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10690 89866 : return finish_call_expr (fns, &args, /*novirt*/false,
10691 : /*koenig*/true, tf_warning_or_error);
10692 89866 : }
10693 : }
10694 :
10695 : /* It's impossible to recover the decltype of a tuple decomposition variable
10696 : based on the actual type of the variable, so store it in a hash table. */
10697 :
10698 : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10699 :
10700 : tree
10701 662 : lookup_decomp_type (tree v)
10702 : {
10703 662 : if (decomp_type_table)
10704 535 : if (tree *slot = decomp_type_table->get (v))
10705 471 : return *slot;
10706 : return NULL_TREE;
10707 : }
10708 :
10709 : /* Mangle a decomposition declaration if needed. Arguments like
10710 : in cp_finish_decomp. */
10711 :
10712 : static void
10713 68921 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10714 : {
10715 68921 : if (!processing_template_decl
10716 68921 : && !error_operand_p (decl)
10717 137842 : && TREE_STATIC (decl))
10718 : {
10719 897 : auto_vec<tree, 16> v;
10720 897 : v.safe_grow (decomp->count, true);
10721 897 : tree d = decomp->decl;
10722 2966 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10723 2069 : v[decomp->count - i - 1] = d;
10724 897 : if (DECL_FUNCTION_SCOPE_P (decl))
10725 : {
10726 : size_t sz = 3;
10727 933 : for (unsigned int i = 0; i < decomp->count; ++i)
10728 664 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10729 269 : char *name = XALLOCAVEC (char, sz);
10730 269 : name[0] = 'D';
10731 269 : name[1] = 'C';
10732 269 : char *p = name + 2;
10733 933 : for (unsigned int i = 0; i < decomp->count; ++i)
10734 : {
10735 664 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10736 664 : *p++ = ' ';
10737 664 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
10738 664 : p += len;
10739 : }
10740 269 : *p = '\0';
10741 269 : determine_local_discriminator (decl, get_identifier (name));
10742 : }
10743 897 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10744 897 : maybe_apply_pragma_weak (decl);
10745 897 : }
10746 68921 : }
10747 :
10748 : /* Append #i to DECL_NAME (decl) or for name independent decls
10749 : clear DECL_NAME (decl). */
10750 :
10751 : static void
10752 33811 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10753 : {
10754 33811 : if (name_independent_decl_p (decl))
10755 : /* Only "_" names are treated as name independent, "_#0" etc. is not and
10756 : because we pushdecl the individual decl elements of structured binding
10757 : pack, we could get redeclaration errors if there are 2 or more name
10758 : independent structured binding packs in the same scope. */
10759 312 : DECL_NAME (decl) = NULL_TREE;
10760 : else
10761 : {
10762 33499 : tree name = DECL_NAME (decl);
10763 33499 : size_t len = IDENTIFIER_LENGTH (name) + 22;
10764 33499 : char *n = XALLOCAVEC (char, len);
10765 33499 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10766 33499 : IDENTIFIER_POINTER (name), i);
10767 33499 : DECL_NAME (decl) = get_identifier (n);
10768 : }
10769 33811 : }
10770 :
10771 : /* Return structured binding size of TYPE or -1 if erroneous. */
10772 :
10773 : HOST_WIDE_INT
10774 957 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10775 : {
10776 957 : if (TYPE_REF_P (type))
10777 : {
10778 0 : type = complete_type (TREE_TYPE (type));
10779 0 : if (type == error_mark_node)
10780 : return -1;
10781 0 : if (!COMPLETE_TYPE_P (type))
10782 : {
10783 0 : if (complain & tf_error)
10784 0 : error_at (loc, "structured binding refers to incomplete type %qT",
10785 : type);
10786 : return -1;
10787 : }
10788 : }
10789 :
10790 957 : unsigned HOST_WIDE_INT eltscnt = 0;
10791 957 : if (TREE_CODE (type) == ARRAY_TYPE)
10792 : {
10793 724 : if (TYPE_DOMAIN (type) == NULL_TREE)
10794 : {
10795 9 : if (complain & tf_error)
10796 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
10797 : type);
10798 : return -1;
10799 : }
10800 715 : tree nelts = array_type_nelts_top (type);
10801 715 : if (nelts == error_mark_node)
10802 : return -1;
10803 715 : if (!tree_fits_shwi_p (nelts))
10804 : {
10805 3 : if (complain & tf_error)
10806 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10807 : return -1;
10808 : }
10809 712 : return tree_to_shwi (nelts);
10810 : }
10811 : /* 2 GNU extensions. */
10812 233 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10813 : return 2;
10814 227 : else if (TREE_CODE (type) == VECTOR_TYPE)
10815 : {
10816 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10817 : {
10818 : if (complain & tf_error)
10819 : error_at (loc, "cannot decompose variable length vector %qT", type);
10820 : return -1;
10821 : }
10822 12 : return eltscnt;
10823 : }
10824 215 : else if (tree tsize = get_tuple_size (type))
10825 : {
10826 38 : if (tsize == error_mark_node
10827 32 : || !tree_fits_shwi_p (tsize)
10828 70 : || tree_int_cst_sgn (tsize) < 0)
10829 : {
10830 9 : if (complain & tf_error)
10831 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10832 : "constant expression", type);
10833 : return -1;
10834 : }
10835 29 : return tree_to_shwi (tsize);
10836 : }
10837 177 : else if (TREE_CODE (type) == UNION_TYPE)
10838 : {
10839 3 : if (complain & tf_error)
10840 3 : error_at (loc, "cannot decompose union type %qT", type);
10841 : return -1;
10842 : }
10843 174 : else if (!CLASS_TYPE_P (type))
10844 : {
10845 21 : if (complain & tf_error)
10846 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10847 : return -1;
10848 : }
10849 153 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10850 : return -1;
10851 153 : else if (!COMPLETE_TYPE_P (type))
10852 : {
10853 0 : if (complain & tf_error)
10854 0 : error_at (loc, "structured binding refers to incomplete class type "
10855 : "%qT", type);
10856 : return -1;
10857 : }
10858 : else
10859 : {
10860 153 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10861 153 : if (btype == error_mark_node)
10862 : return -1;
10863 132 : else if (btype == NULL_TREE)
10864 : return 0;
10865 111 : if (btype != type)
10866 : {
10867 6 : tree binfo = lookup_base (type, btype, ba_check, NULL, complain);
10868 6 : if (binfo == NULL_TREE || binfo == error_mark_node)
10869 : return -1;
10870 : }
10871 1150 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10872 1730 : if (TREE_CODE (field) != FIELD_DECL
10873 372 : || DECL_ARTIFICIAL (field)
10874 1417 : || DECL_UNNAMED_BIT_FIELD (field))
10875 685 : continue;
10876 : else
10877 360 : eltscnt++;
10878 105 : return eltscnt;
10879 : }
10880 : }
10881 :
10882 : /* Finish a decomposition declaration. DECL is the underlying declaration
10883 : "e", FIRST is the head of a chain of decls for the individual identifiers
10884 : chained through DECL_CHAIN in reverse order and COUNT is the number of
10885 : those decls. If TEST_P is true, return true if any code would need to be
10886 : actually emitted but don't emit it. Return false otherwise. */
10887 :
10888 : bool
10889 345518 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10890 : {
10891 345518 : tree first = decomp->decl;
10892 345518 : unsigned count = decomp->count;
10893 345518 : if (error_operand_p (decl))
10894 : {
10895 200 : error_out:
10896 625 : while (count--)
10897 : {
10898 425 : TREE_TYPE (first) = error_mark_node;
10899 425 : if (DECL_HAS_VALUE_EXPR_P (first))
10900 : {
10901 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10902 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10903 : }
10904 425 : first = DECL_CHAIN (first);
10905 : }
10906 200 : if (DECL_P (decl) && TREE_STATIC (decl))
10907 54 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10908 : return false;
10909 : }
10910 :
10911 345455 : location_t loc = DECL_SOURCE_LOCATION (decl);
10912 345455 : if (type_dependent_expression_p (decl)
10913 : /* This happens for range for when not in templates.
10914 : Still add the DECL_VALUE_EXPRs for later processing. */
10915 345455 : || (!processing_template_decl
10916 105100 : && type_uses_auto (TREE_TYPE (decl))))
10917 : {
10918 662069 : for (unsigned int i = 0; i < count; i++)
10919 : {
10920 441142 : if (!DECL_HAS_VALUE_EXPR_P (first))
10921 : {
10922 441142 : tree v = build_nt (ARRAY_REF, decl,
10923 441142 : size_int (count - i - 1),
10924 : NULL_TREE, NULL_TREE);
10925 441142 : SET_DECL_VALUE_EXPR (first, v);
10926 441142 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10927 : }
10928 441142 : if (processing_template_decl)
10929 440506 : fit_decomposition_lang_decl (first, decl);
10930 441142 : first = DECL_CHAIN (first);
10931 : }
10932 : return false;
10933 : }
10934 :
10935 124528 : auto_vec<tree, 16> v;
10936 124528 : v.safe_grow (count, true);
10937 124528 : tree d = first;
10938 124528 : int pack = -1;
10939 489063 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10940 : {
10941 240007 : v[count - i - 1] = d;
10942 240007 : fit_decomposition_lang_decl (d, decl);
10943 240007 : if (DECL_PACK_P (d))
10944 11117 : pack = count - i - 1;
10945 : }
10946 :
10947 124528 : tree type = TREE_TYPE (decl);
10948 124528 : tree dexp = decl;
10949 :
10950 124528 : if (TYPE_REF_P (type))
10951 : {
10952 2206 : dexp = convert_from_reference (dexp);
10953 2206 : type = complete_type (TREE_TYPE (type));
10954 2206 : if (type == error_mark_node)
10955 137 : goto error_out;
10956 2206 : if (!COMPLETE_TYPE_P (type))
10957 : {
10958 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10959 : type);
10960 3 : goto error_out;
10961 : }
10962 : }
10963 :
10964 124525 : tree eltype = NULL_TREE;
10965 124525 : unsigned HOST_WIDE_INT eltscnt = 0;
10966 : /* Structured binding packs when initializer is non-dependent should
10967 : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10968 : of that TREE_VEC are the base and index, what is normally represented
10969 : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10970 : of the pack first element. The remaining elements of the TREE_VEC
10971 : are VAR_DECLs for the pack elements. */
10972 124525 : tree packv = NULL_TREE;
10973 :
10974 124525 : if (TREE_CODE (type) == ARRAY_TYPE)
10975 : {
10976 11234 : tree nelts;
10977 11234 : nelts = array_type_nelts_top (type);
10978 11234 : if (nelts == error_mark_node)
10979 0 : goto error_out;
10980 11234 : if (DECL_DECOMP_BASE (decl))
10981 : {
10982 9 : error_at (loc, "array initializer for structured binding "
10983 : "declaration in condition");
10984 9 : goto error_out;
10985 : }
10986 11225 : if (!tree_fits_uhwi_p (nelts))
10987 : {
10988 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10989 3 : goto error_out;
10990 : }
10991 11222 : eltscnt = tree_to_uhwi (nelts);
10992 11222 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10993 : {
10994 11 : cnt_mismatch:
10995 38 : auto_diagnostic_group d;
10996 38 : if (count > eltscnt)
10997 27 : error_n (loc, count,
10998 : "%u name provided for structured binding",
10999 : "%u names provided for structured binding", count);
11000 : else
11001 11 : error_n (loc, count,
11002 : "only %u name provided for structured binding",
11003 : "only %u names provided for structured binding", count);
11004 38 : inform_n (loc, eltscnt,
11005 : "while %qT decomposes into %wu element",
11006 : "while %qT decomposes into %wu elements",
11007 : type, eltscnt);
11008 38 : goto error_out;
11009 : }
11010 11211 : eltype = TREE_TYPE (type);
11011 24136 : for (unsigned int i = 0; i < count; i++)
11012 : {
11013 12925 : if ((unsigned) pack == i)
11014 : {
11015 10060 : packv = make_tree_vec (eltscnt - count + 3);
11016 51506 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11017 : {
11018 31386 : tree t;
11019 31386 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11020 31386 : set_sb_pack_name (t, j);
11021 31386 : maybe_push_decl (t);
11022 31386 : TREE_TYPE (t) = eltype;
11023 31386 : layout_decl (t, 0);
11024 31386 : if (!processing_template_decl)
11025 : {
11026 31288 : tree a = unshare_expr (dexp);
11027 31288 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
11028 : NULL_TREE, NULL_TREE);
11029 31288 : SET_DECL_VALUE_EXPR (t, a);
11030 31288 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11031 : }
11032 : }
11033 12925 : continue;
11034 10060 : }
11035 2865 : TREE_TYPE (v[i]) = eltype;
11036 2865 : layout_decl (v[i], 0);
11037 2865 : if (processing_template_decl)
11038 140 : continue;
11039 2725 : tree t = unshare_expr (dexp);
11040 2725 : unsigned HOST_WIDE_INT j = i;
11041 2725 : if (pack != -1 && (unsigned) pack < i)
11042 64 : j = i + eltscnt - count;
11043 2725 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
11044 2725 : SET_DECL_VALUE_EXPR (v[i], t);
11045 2725 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11046 : }
11047 : }
11048 : /* 2 GNU extensions. */
11049 113291 : else if (TREE_CODE (type) == COMPLEX_TYPE)
11050 : {
11051 78 : eltscnt = 2;
11052 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11053 3 : goto cnt_mismatch;
11054 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11055 288 : for (unsigned int i = 0; i < count; i++)
11056 : {
11057 138 : if ((unsigned) pack == i)
11058 : {
11059 60 : packv = make_tree_vec (eltscnt - count + 3);
11060 192 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11061 : {
11062 72 : tree t;
11063 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11064 72 : set_sb_pack_name (t, j);
11065 72 : maybe_push_decl (t);
11066 72 : TREE_TYPE (t) = eltype;
11067 72 : layout_decl (t, 0);
11068 72 : if (!processing_template_decl)
11069 : {
11070 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
11071 : unshare_expr (dexp));
11072 48 : SET_DECL_VALUE_EXPR (t, a);
11073 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11074 : }
11075 : }
11076 60 : continue;
11077 60 : }
11078 78 : TREE_TYPE (v[i]) = eltype;
11079 78 : layout_decl (v[i], 0);
11080 78 : if (processing_template_decl)
11081 16 : continue;
11082 62 : tree t = unshare_expr (dexp);
11083 62 : unsigned HOST_WIDE_INT j = i;
11084 62 : if (pack != -1 && (unsigned) pack < i)
11085 16 : j = i + eltscnt - count;
11086 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
11087 62 : SET_DECL_VALUE_EXPR (v[i], t);
11088 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11089 : }
11090 : }
11091 113213 : else if (TREE_CODE (type) == VECTOR_TYPE)
11092 : {
11093 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
11094 : {
11095 : error_at (loc, "cannot decompose variable length vector %qT", type);
11096 : goto error_out;
11097 : }
11098 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11099 3 : goto cnt_mismatch;
11100 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11101 306 : for (unsigned int i = 0; i < count; i++)
11102 : {
11103 192 : if ((unsigned) pack == i)
11104 : {
11105 48 : packv = make_tree_vec (eltscnt - count + 3);
11106 756 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11107 : {
11108 660 : tree t;
11109 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11110 660 : set_sb_pack_name (t, j);
11111 660 : maybe_push_decl (t);
11112 660 : TREE_TYPE (t) = eltype;
11113 660 : layout_decl (t, 0);
11114 660 : if (!processing_template_decl)
11115 : {
11116 440 : tree a = unshare_expr (dexp);
11117 440 : location_t loc = DECL_SOURCE_LOCATION (t);
11118 440 : tree s = size_int (j + pack);
11119 440 : convert_vector_to_array_for_subscript (loc, &a, s);
11120 440 : a = build4 (ARRAY_REF, eltype, a, s,
11121 : NULL_TREE, NULL_TREE);
11122 440 : SET_DECL_VALUE_EXPR (t, a);
11123 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11124 : }
11125 : }
11126 84 : continue;
11127 48 : }
11128 144 : TREE_TYPE (v[i]) = eltype;
11129 144 : layout_decl (v[i], 0);
11130 144 : if (processing_template_decl)
11131 36 : continue;
11132 108 : tree t = unshare_expr (dexp);
11133 108 : unsigned HOST_WIDE_INT j = i;
11134 108 : if (pack != -1 && (unsigned) pack < i)
11135 24 : j = i + eltscnt - count;
11136 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
11137 108 : &t, size_int (j));
11138 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
11139 108 : SET_DECL_VALUE_EXPR (v[i], t);
11140 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11141 : }
11142 : }
11143 113153 : else if (tree tsize = get_tuple_size (type))
11144 : {
11145 81748 : if (tsize == error_mark_node)
11146 : {
11147 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
11148 : "constant expression", type);
11149 0 : goto error_out;
11150 : }
11151 81748 : if (!tree_fits_uhwi_p (tsize))
11152 : {
11153 12 : auto_diagnostic_group d;
11154 12 : error_n (loc, count,
11155 : "%u name provided for structured binding",
11156 : "%u names provided for structured binding", count);
11157 12 : inform (loc, "while %qT decomposes into %E elements",
11158 : type, tsize);
11159 12 : goto error_out;
11160 12 : }
11161 81736 : eltscnt = tree_to_uhwi (tsize);
11162 81736 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11163 6 : goto cnt_mismatch;
11164 81730 : if (test_p && eltscnt)
11165 124391 : return true;
11166 45832 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
11167 : {
11168 : /* For structured bindings used in conditions we need to evaluate
11169 : the conversion of decl (aka e in the standard) to bool or
11170 : integral/enumeral type (the latter for switch conditions)
11171 : before the get methods, as [dcl.struct.bind]/7 requires that:
11172 : "The initialization of e and any conversion of e considered as
11173 : a decision variable is sequenced before the initialization of
11174 : any r_i." */
11175 89 : tree cond = convert_from_reference (decl);
11176 89 : if (integer_onep (DECL_DECOMP_BASE (decl)))
11177 : /* switch condition. */
11178 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
11179 : cond, true);
11180 : else
11181 : /* if/while/for condition. */
11182 71 : cond = contextual_conv_bool (cond, tf_warning_or_error);
11183 89 : if (cond && !error_operand_p (cond))
11184 : {
11185 89 : cond = get_temp_regvar (TREE_TYPE (cond), cond);
11186 89 : pushdecl (cond);
11187 : /* Set DECL_DECOMP_BASE to cond VAR_DECL wrapped in
11188 : NON_LVALUE_EXPR, such that it is considered to be
11189 : the condition of a structured binding rather than
11190 : structured binding's base variable. */
11191 89 : DECL_DECOMP_BASE (decl)
11192 178 : = build1 (NON_LVALUE_EXPR, TREE_TYPE (cond), cond);
11193 : }
11194 : }
11195 45832 : int save_read = DECL_READ_P (decl);
11196 137379 : for (unsigned i = 0; i < count; ++i)
11197 : {
11198 91568 : location_t sloc = input_location;
11199 91568 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
11200 :
11201 91568 : if ((unsigned) pack == i)
11202 : {
11203 405 : packv = make_tree_vec (eltscnt - count + 3);
11204 1999 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11205 : {
11206 1189 : tree t;
11207 1189 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11208 1189 : set_sb_pack_name (t, j);
11209 1189 : input_location = dloc;
11210 1189 : tree init = get_tuple_decomp_init (decl, j + pack);
11211 1189 : tree eltype = (init == error_mark_node ? error_mark_node
11212 1189 : : get_tuple_element_type (type, j + pack));
11213 1189 : input_location = sloc;
11214 :
11215 1189 : if (VOID_TYPE_P (eltype))
11216 : {
11217 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
11218 : "%<void%>", j + pack, type);
11219 0 : eltype = error_mark_node;
11220 : }
11221 1189 : if (init == error_mark_node || eltype == error_mark_node)
11222 : {
11223 0 : inform (dloc, "in initialization of structured binding "
11224 0 : "pack %qD", v[pack]);
11225 0 : goto error_out;
11226 : }
11227 1189 : maybe_push_decl (t);
11228 : /* Save the decltype away before reference collapse. */
11229 1189 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
11230 1189 : if (glvalue_p (init))
11231 1087 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11232 1189 : TREE_TYPE (t) = eltype;
11233 1189 : layout_decl (t, 0);
11234 1189 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11235 1189 : if (!processing_template_decl)
11236 : {
11237 797 : copy_linkage (t, decl);
11238 797 : tree name = DECL_NAME (t);
11239 797 : if (TREE_STATIC (decl))
11240 54 : DECL_NAME (t) = DECL_NAME (v[pack]);
11241 797 : cp_finish_decl (t, init, /*constexpr*/false,
11242 : /*asm*/NULL_TREE, LOOKUP_NORMAL);
11243 797 : if (TREE_STATIC (decl))
11244 : {
11245 54 : DECL_ASSEMBLER_NAME (t);
11246 54 : DECL_NAME (t) = name;
11247 : }
11248 : }
11249 : }
11250 405 : continue;
11251 405 : }
11252 :
11253 91163 : unsigned HOST_WIDE_INT j = i;
11254 91163 : if (pack != -1 && (unsigned) pack < i)
11255 235 : j = i + eltscnt - count;
11256 91163 : input_location = dloc;
11257 91163 : tree init = get_tuple_decomp_init (decl, j);
11258 91163 : tree eltype = (init == error_mark_node ? error_mark_node
11259 91154 : : get_tuple_element_type (type, j));
11260 91163 : input_location = sloc;
11261 :
11262 91163 : if (VOID_TYPE_P (eltype))
11263 : {
11264 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
11265 : j, type);
11266 3 : eltype = error_mark_node;
11267 : }
11268 91163 : if (init == error_mark_node || eltype == error_mark_node)
11269 : {
11270 21 : inform (dloc, "in initialization of structured binding "
11271 21 : "variable %qD", v[i]);
11272 21 : goto error_out;
11273 : }
11274 : /* Save the decltype away before reference collapse. */
11275 91142 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
11276 91142 : if (glvalue_p (init))
11277 90791 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11278 91142 : TREE_TYPE (v[i]) = eltype;
11279 91142 : layout_decl (v[i], 0);
11280 91142 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
11281 : {
11282 : /* In this case the names are variables, not just proxies. */
11283 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
11284 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
11285 : }
11286 91142 : if (!processing_template_decl)
11287 : {
11288 71372 : copy_linkage (v[i], decl);
11289 71372 : cp_finish_decl (v[i], init, /*constexpr*/false,
11290 : /*asm*/NULL_TREE, LOOKUP_NORMAL);
11291 : }
11292 : }
11293 : /* Ignore reads from the underlying decl performed during initialization
11294 : of the individual variables. If those will be read, we'll mark
11295 : the underlying decl as read at that point. */
11296 45811 : DECL_READ_P (decl) = save_read;
11297 : }
11298 31405 : else if (TREE_CODE (type) == UNION_TYPE)
11299 : {
11300 3 : error_at (loc, "cannot decompose union type %qT", type);
11301 3 : goto error_out;
11302 : }
11303 31402 : else if (!CLASS_TYPE_P (type))
11304 : {
11305 19 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
11306 19 : goto error_out;
11307 : }
11308 31383 : else if (processing_template_decl && complete_type (type) == error_mark_node)
11309 0 : goto error_out;
11310 31383 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
11311 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
11312 : type);
11313 : else
11314 : {
11315 31381 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
11316 : tf_warning_or_error);
11317 31381 : if (btype == error_mark_node)
11318 21 : goto error_out;
11319 31360 : else if (btype == NULL_TREE)
11320 : {
11321 24 : if (pack == 0 && count == 1)
11322 : {
11323 16 : eltscnt = 0;
11324 16 : packv = make_tree_vec (2);
11325 16 : goto done;
11326 : }
11327 8 : error_at (loc, "cannot decompose class type %qT without non-static "
11328 : "data members", type);
11329 8 : goto error_out;
11330 : }
11331 398946 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11332 671742 : if (TREE_CODE (field) != FIELD_DECL
11333 63494 : || DECL_ARTIFICIAL (field)
11334 431100 : || DECL_UNNAMED_BIT_FIELD (field))
11335 304132 : continue;
11336 : else
11337 63478 : eltscnt++;
11338 31336 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11339 15 : goto cnt_mismatch;
11340 31321 : tree t = dexp;
11341 31321 : if (type != btype)
11342 : {
11343 18 : t = convert_to_base (t, btype, /*check_access*/true,
11344 : /*nonnull*/false, tf_warning_or_error);
11345 18 : type = btype;
11346 : }
11347 31321 : unsigned HOST_WIDE_INT j = 0;
11348 31321 : unsigned int i = 0;
11349 398735 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11350 671387 : if (TREE_CODE (field) != FIELD_DECL
11351 63457 : || DECL_ARTIFICIAL (field)
11352 430867 : || DECL_UNNAMED_BIT_FIELD (field))
11353 303973 : continue;
11354 : else
11355 : {
11356 63441 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
11357 : NULL_TREE);
11358 63441 : if (REFERENCE_REF_P (tt))
11359 30 : tt = TREE_OPERAND (tt, 0);
11360 63441 : if (pack != -1 && j >= (unsigned) pack)
11361 : {
11362 685 : if (j == (unsigned) pack)
11363 : {
11364 227 : packv = make_tree_vec (eltscnt - count + 3);
11365 227 : i++;
11366 : }
11367 685 : if (j < (unsigned) pack + eltscnt - (count - 1))
11368 : {
11369 504 : tree t;
11370 504 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11371 504 : set_sb_pack_name (t, j + 1 - i);
11372 504 : maybe_push_decl (t);
11373 504 : TREE_TYPE (t) = TREE_TYPE (tt);
11374 504 : layout_decl (t, 0);
11375 504 : if (!processing_template_decl)
11376 : {
11377 383 : SET_DECL_VALUE_EXPR (t, tt);
11378 383 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11379 : }
11380 : else
11381 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11382 504 : j++;
11383 504 : continue;
11384 504 : }
11385 : }
11386 62937 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
11387 62937 : layout_decl (v[i], 0);
11388 62937 : if (!processing_template_decl)
11389 : {
11390 43717 : SET_DECL_VALUE_EXPR (v[i], tt);
11391 43717 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11392 : }
11393 62937 : i++;
11394 62937 : j++;
11395 : }
11396 31321 : if (pack != -1 && j == (unsigned) pack)
11397 : {
11398 20 : gcc_checking_assert (eltscnt == count - 1);
11399 20 : packv = make_tree_vec (2);
11400 : }
11401 : }
11402 88493 : done:
11403 88493 : if (packv)
11404 : {
11405 10836 : gcc_checking_assert (pack != -1);
11406 10836 : TREE_VEC_ELT (packv, 0) = decl;
11407 10836 : TREE_VEC_ELT (packv, 1) = size_int (pack);
11408 10836 : SET_DECL_VALUE_EXPR (v[pack], packv);
11409 10836 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11410 10836 : DECL_IGNORED_P (v[pack]) = 1;
11411 10836 : if (!processing_template_decl)
11412 43485 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11413 32956 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
11414 : }
11415 88493 : if (processing_template_decl)
11416 : {
11417 59163 : for (unsigned int i = 0; i < count; i++)
11418 39491 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11419 : {
11420 39037 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
11421 : NULL_TREE, NULL_TREE);
11422 39037 : SET_DECL_VALUE_EXPR (v[i], a);
11423 39037 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11424 : }
11425 : }
11426 68821 : else if (DECL_NAMESPACE_SCOPE_P (decl) && !seen_error ())
11427 : {
11428 480 : tree attr = NULL_TREE, *pa = &attr;
11429 1550 : for (unsigned int i = 0; i < count; i++)
11430 1070 : if ((unsigned) pack != i
11431 1070 : && DECL_HAS_VALUE_EXPR_P (v[i])
11432 1796 : && !DECL_IGNORED_P (v[i]))
11433 : {
11434 726 : (*debug_hooks->early_global_decl) (v[i]);
11435 726 : *pa = build_tree_list (NULL_TREE, v[i]);
11436 726 : pa = &TREE_CHAIN (*pa);
11437 : }
11438 480 : if (attr)
11439 364 : DECL_ATTRIBUTES (decl)
11440 728 : = tree_cons (get_identifier ("structured bindings"),
11441 364 : attr, DECL_ATTRIBUTES (decl));
11442 : }
11443 : return false;
11444 : }
11445 :
11446 : /* Returns a declaration for a VAR_DECL as if:
11447 :
11448 : extern "C" TYPE NAME;
11449 :
11450 : had been seen. Used to create compiler-generated global
11451 : variables. */
11452 :
11453 : static tree
11454 2325 : declare_global_var (tree name, tree type)
11455 : {
11456 2325 : auto cookie = push_abi_namespace (global_namespace);
11457 2325 : tree decl = build_decl (input_location, VAR_DECL, name, type);
11458 2325 : TREE_PUBLIC (decl) = 1;
11459 2325 : DECL_EXTERNAL (decl) = 1;
11460 2325 : DECL_ARTIFICIAL (decl) = 1;
11461 2325 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11462 : /* If the user has explicitly declared this variable (perhaps
11463 : because the code we are compiling is part of a low-level runtime
11464 : library), then it is possible that our declaration will be merged
11465 : with theirs by pushdecl. */
11466 2325 : decl = pushdecl (decl);
11467 2325 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
11468 2325 : pop_abi_namespace (cookie, global_namespace);
11469 :
11470 2325 : return decl;
11471 : }
11472 :
11473 : /* Returns the type for the argument to "atexit" corresponding to the function
11474 : to be called when the program exits. */
11475 :
11476 : static tree
11477 24 : get_atexit_fn_ptr_type ()
11478 : {
11479 24 : if (!atexit_fn_ptr_type_node)
11480 : {
11481 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11482 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11483 : }
11484 :
11485 24 : return atexit_fn_ptr_type_node;
11486 : }
11487 :
11488 : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11489 : or "__cxa_throw" corresponding to the destructor to be called when the
11490 : program exits. */
11491 :
11492 : tree
11493 19788 : get_cxa_atexit_fn_ptr_type ()
11494 : {
11495 19788 : if (!cleanup_type)
11496 : {
11497 14900 : tree fntype = build_function_type_list (void_type_node,
11498 : ptr_type_node, NULL_TREE);
11499 14900 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11500 14900 : cleanup_type = build_pointer_type (fntype);
11501 : }
11502 :
11503 19788 : return cleanup_type;
11504 : }
11505 :
11506 : /* Returns a pointer to the `atexit' function. Note that if
11507 : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11508 : `__cxa_atexit' function specified in the IA64 C++ ABI. */
11509 :
11510 : static tree
11511 3611 : get_atexit_node (void)
11512 : {
11513 3611 : tree atexit_fndecl;
11514 3611 : tree fn_type;
11515 3611 : tree fn_ptr_type;
11516 3611 : const char *name;
11517 3611 : bool use_aeabi_atexit;
11518 3611 : tree ctx = global_namespace;
11519 :
11520 3611 : if (atexit_node)
11521 : return atexit_node;
11522 :
11523 2288 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11524 : {
11525 : /* The declaration for `__cxa_atexit' is:
11526 :
11527 : int __cxa_atexit (void (*)(void *), void *, void *)
11528 :
11529 : We build up the argument types and then the function type
11530 : itself. */
11531 2276 : tree argtype0, argtype1, argtype2;
11532 :
11533 2276 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11534 : /* First, build the pointer-to-function type for the first
11535 : argument. */
11536 2276 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11537 : /* Then, build the rest of the argument types. */
11538 2276 : argtype2 = ptr_type_node;
11539 2276 : if (use_aeabi_atexit)
11540 : {
11541 : argtype1 = fn_ptr_type;
11542 : argtype0 = ptr_type_node;
11543 : }
11544 : else
11545 : {
11546 2276 : argtype1 = ptr_type_node;
11547 2276 : argtype0 = fn_ptr_type;
11548 : }
11549 : /* And the final __cxa_atexit type. */
11550 2276 : fn_type = build_function_type_list (integer_type_node,
11551 : argtype0, argtype1, argtype2,
11552 : NULL_TREE);
11553 : /* ... which needs noexcept. */
11554 2276 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11555 2276 : if (use_aeabi_atexit)
11556 : {
11557 0 : name = "__aeabi_atexit";
11558 0 : push_to_top_level ();
11559 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
11560 0 : ctx = current_namespace;
11561 0 : while (n--)
11562 0 : pop_namespace ();
11563 0 : pop_from_top_level ();
11564 : }
11565 : else
11566 : {
11567 2276 : name = "__cxa_atexit";
11568 2276 : ctx = abi_node;
11569 : }
11570 : }
11571 : else
11572 : {
11573 : /* The declaration for `atexit' is:
11574 :
11575 : int atexit (void (*)());
11576 :
11577 : We build up the argument types and then the function type
11578 : itself. */
11579 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
11580 : /* Build the final atexit type. */
11581 12 : fn_type = build_function_type_list (integer_type_node,
11582 : fn_ptr_type, NULL_TREE);
11583 : /* ... which needs noexcept. */
11584 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11585 12 : name = "atexit";
11586 : }
11587 :
11588 : /* Now, build the function declaration. */
11589 2288 : push_lang_context (lang_name_c);
11590 2288 : auto cookie = push_abi_namespace (ctx);
11591 2288 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
11592 2288 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11593 : /* Install as hidden builtin so we're (a) more relaxed about
11594 : exception spec matching and (b) will not give a confusing location
11595 : in diagnostic and (c) won't magically appear in user-visible name
11596 : lookups. */
11597 2288 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11598 2288 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11599 2288 : pop_abi_namespace (cookie, ctx);
11600 2288 : mark_used (atexit_fndecl);
11601 2288 : pop_lang_context ();
11602 2288 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11603 :
11604 2288 : return atexit_node;
11605 : }
11606 :
11607 : /* Like get_atexit_node, but for thread-local cleanups. */
11608 :
11609 : static tree
11610 98 : get_thread_atexit_node (void)
11611 : {
11612 98 : if (thread_atexit_node)
11613 : return thread_atexit_node;
11614 :
11615 : /* The declaration for `__cxa_thread_atexit' is:
11616 :
11617 : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11618 59 : tree fn_type = build_function_type_list (integer_type_node,
11619 : get_cxa_atexit_fn_ptr_type (),
11620 : ptr_type_node, ptr_type_node,
11621 : NULL_TREE);
11622 :
11623 : /* Now, build the function declaration, as with __cxa_atexit. */
11624 59 : unsigned flags = push_abi_namespace ();
11625 59 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
11626 : ECF_LEAF | ECF_NOTHROW);
11627 59 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11628 59 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11629 59 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11630 59 : pop_abi_namespace (flags);
11631 59 : mark_used (atexit_fndecl);
11632 59 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11633 :
11634 59 : return thread_atexit_node;
11635 : }
11636 :
11637 : /* Returns the __dso_handle VAR_DECL. */
11638 :
11639 : static tree
11640 3697 : get_dso_handle_node (void)
11641 : {
11642 3697 : if (dso_handle_node)
11643 : return dso_handle_node;
11644 :
11645 : /* Declare the variable. */
11646 2325 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11647 : ptr_type_node);
11648 :
11649 : #ifdef HAVE_GAS_HIDDEN
11650 2325 : if (dso_handle_node != error_mark_node)
11651 : {
11652 2322 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11653 2322 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11654 : }
11655 : #endif
11656 :
11657 2325 : return dso_handle_node;
11658 : }
11659 :
11660 : /* Begin a new function with internal linkage whose job will be simply
11661 : to destroy some particular DECL. OB_PARM is true if object pointer
11662 : is passed to the cleanup function, otherwise no argument is passed. */
11663 :
11664 : static tree
11665 383 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11666 : {
11667 383 : push_to_top_level ();
11668 :
11669 : /* No need to mangle this. */
11670 383 : push_lang_context (lang_name_c);
11671 :
11672 : /* Build the name of the function. */
11673 383 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11674 383 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11675 383 : dname = targetm.strip_name_encoding (dname);
11676 763 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11677 :
11678 383 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11679 : : get_atexit_fn_ptr_type ());
11680 : /* Build the function declaration. */
11681 383 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11682 383 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11683 : /* It's a function with internal linkage, generated by the
11684 : compiler. */
11685 383 : TREE_PUBLIC (fndecl) = 0;
11686 383 : DECL_ARTIFICIAL (fndecl) = 1;
11687 : /* Make the function `inline' so that it is only emitted if it is
11688 : actually needed. It is unlikely that it will be inlined, since
11689 : it is only called via a function pointer, but we avoid unnecessary
11690 : emissions this way. */
11691 383 : DECL_DECLARED_INLINE_P (fndecl) = 1;
11692 383 : DECL_INTERFACE_KNOWN (fndecl) = 1;
11693 383 : if (ob_parm)
11694 : {
11695 : /* Build the parameter. */
11696 371 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11697 371 : TREE_USED (parmdecl) = 1;
11698 371 : DECL_READ_P (parmdecl) = 1;
11699 371 : DECL_ARGUMENTS (fndecl) = parmdecl;
11700 : }
11701 :
11702 383 : fndecl = pushdecl (fndecl, /*hidden=*/true);
11703 383 : if (omp_target)
11704 : {
11705 3 : DECL_ATTRIBUTES (fndecl)
11706 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11707 3 : DECL_ATTRIBUTES (fndecl));
11708 3 : DECL_ATTRIBUTES (fndecl)
11709 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11710 3 : DECL_ATTRIBUTES (fndecl));
11711 : }
11712 383 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11713 :
11714 383 : pop_lang_context ();
11715 :
11716 383 : return current_function_decl;
11717 : }
11718 :
11719 : /* Finish the cleanup function begun by start_cleanup_fn. */
11720 :
11721 : static void
11722 383 : end_cleanup_fn (void)
11723 : {
11724 383 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
11725 :
11726 383 : pop_from_top_level ();
11727 383 : }
11728 :
11729 : /* Generate code to handle the destruction of DECL, an object with
11730 : static storage duration. */
11731 :
11732 : tree
11733 15673 : register_dtor_fn (tree decl, bool omp_target)
11734 : {
11735 15673 : tree cleanup;
11736 15673 : tree addr;
11737 15673 : tree compound_stmt;
11738 15673 : tree fcall;
11739 15673 : tree type;
11740 15673 : bool ob_parm, dso_parm, use_dtor;
11741 15673 : tree arg0, arg1, arg2;
11742 15673 : tree atex_node;
11743 :
11744 15673 : type = TREE_TYPE (decl);
11745 15673 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11746 11964 : return void_node;
11747 :
11748 3709 : if (decl_maybe_constant_destruction (decl, type)
11749 3709 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11750 : {
11751 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11752 0 : return void_node;
11753 : }
11754 :
11755 : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11756 : "__aeabi_atexit"), and DECL is a class object, we can just pass the
11757 : destructor to "__cxa_atexit"; we don't have to build a temporary
11758 : function to do the cleanup. */
11759 7418 : dso_parm = (flag_use_cxa_atexit
11760 3709 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
11761 3709 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11762 3697 : use_dtor = ob_parm && CLASS_TYPE_P (type);
11763 3709 : if (use_dtor)
11764 : {
11765 3326 : cleanup = get_class_binding (type, complete_dtor_identifier);
11766 :
11767 : /* Make sure it is accessible. */
11768 3326 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11769 : tf_warning_or_error);
11770 : }
11771 : else
11772 : {
11773 : /* Call build_cleanup before we enter the anonymous function so
11774 : that any access checks will be done relative to the current
11775 : scope, rather than the scope of the anonymous function. */
11776 383 : build_cleanup (decl);
11777 :
11778 : /* Now start the function. */
11779 383 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11780 :
11781 : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11782 : to the original function, rather than the anonymous one. That
11783 : will make the back end think that nested functions are in use,
11784 : which causes confusion. */
11785 383 : push_deferring_access_checks (dk_no_check);
11786 383 : fcall = build_cleanup (decl);
11787 383 : pop_deferring_access_checks ();
11788 :
11789 : /* Create the body of the anonymous function. */
11790 383 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11791 383 : finish_expr_stmt (fcall);
11792 383 : finish_compound_stmt (compound_stmt);
11793 383 : end_cleanup_fn ();
11794 : }
11795 :
11796 : /* Call atexit with the cleanup function. */
11797 3709 : mark_used (cleanup);
11798 3709 : cleanup = build_address (cleanup);
11799 :
11800 3709 : if (CP_DECL_THREAD_LOCAL_P (decl))
11801 98 : atex_node = get_thread_atexit_node ();
11802 : else
11803 3611 : atex_node = get_atexit_node ();
11804 :
11805 3709 : if (use_dtor)
11806 : {
11807 : /* We must convert CLEANUP to the type that "__cxa_atexit"
11808 : expects. */
11809 3326 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11810 : /* "__cxa_atexit" will pass the address of DECL to the
11811 : cleanup function. */
11812 3326 : mark_used (decl);
11813 3326 : addr = build_address (decl);
11814 : /* The declared type of the parameter to "__cxa_atexit" is
11815 : "void *". For plain "T*", we could just let the
11816 : machinery in cp_build_function_call convert it -- but if the
11817 : type is "cv-qualified T *", then we need to convert it
11818 : before passing it in, to avoid spurious errors. */
11819 3326 : addr = build_nop (ptr_type_node, addr);
11820 : }
11821 : else
11822 : /* Since the cleanup functions we build ignore the address
11823 : they're given, there's no reason to pass the actual address
11824 : in, and, in general, it's cheaper to pass NULL than any
11825 : other value. */
11826 383 : addr = null_pointer_node;
11827 :
11828 3709 : if (dso_parm)
11829 3697 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
11830 : tf_warning_or_error);
11831 12 : else if (ob_parm)
11832 : /* Just pass NULL to the dso handle parm if we don't actually
11833 : have a DSO handle on this target. */
11834 0 : arg2 = null_pointer_node;
11835 : else
11836 : arg2 = NULL_TREE;
11837 :
11838 3697 : if (ob_parm)
11839 : {
11840 3697 : if (!CP_DECL_THREAD_LOCAL_P (decl)
11841 3697 : && targetm.cxx.use_aeabi_atexit ())
11842 : {
11843 : arg1 = cleanup;
11844 : arg0 = addr;
11845 : }
11846 : else
11847 : {
11848 : arg1 = addr;
11849 : arg0 = cleanup;
11850 : }
11851 : }
11852 : else
11853 : {
11854 : arg0 = cleanup;
11855 : arg1 = NULL_TREE;
11856 : }
11857 3709 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11858 3709 : arg0, arg1, arg2, NULL_TREE);
11859 : }
11860 :
11861 : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
11862 : is its initializer. Generate code to handle the construction
11863 : and destruction of DECL. */
11864 :
11865 : static void
11866 30296152 : expand_static_init (tree decl, tree init)
11867 : {
11868 30296152 : gcc_assert (VAR_P (decl));
11869 30296152 : gcc_assert (TREE_STATIC (decl));
11870 :
11871 : /* Some variables require no dynamic initialization. */
11872 30296152 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11873 : {
11874 : /* Make sure the destructor is callable. */
11875 30292478 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11876 30292478 : if (!init)
11877 : return;
11878 : }
11879 :
11880 16537 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11881 15611 : && !DECL_FUNCTION_SCOPE_P (decl))
11882 : {
11883 15 : auto_diagnostic_group d;
11884 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11885 15 : if (init)
11886 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11887 : "needs dynamic initialization", decl);
11888 : else
11889 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11890 : "has a non-trivial destructor", decl);
11891 15 : static bool informed;
11892 15 : if (!informed)
11893 : {
11894 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11895 : "initialization and destruction");
11896 6 : informed = true;
11897 : }
11898 15 : return;
11899 15 : }
11900 :
11901 15567 : if (DECL_FUNCTION_SCOPE_P (decl))
11902 : {
11903 : /* Emit code to perform this initialization but once. */
11904 4030 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11905 4030 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11906 4030 : tree guard, guard_addr;
11907 4030 : tree flag, begin;
11908 : /* We don't need thread-safety code for thread-local vars. */
11909 4030 : bool thread_guard = (flag_threadsafe_statics
11910 4030 : && !CP_DECL_THREAD_LOCAL_P (decl));
11911 :
11912 : /* Emit code to perform this initialization but once. This code
11913 : looks like:
11914 :
11915 : static <type> guard;
11916 : if (!__atomic_load (guard.first_byte)) {
11917 : if (__cxa_guard_acquire (&guard)) {
11918 : bool flag = false;
11919 : try {
11920 : // Do initialization.
11921 : flag = true; __cxa_guard_release (&guard);
11922 : // Register variable for destruction at end of program.
11923 : } catch {
11924 : if (!flag) __cxa_guard_abort (&guard);
11925 : }
11926 : }
11927 : }
11928 :
11929 : Note that the `flag' variable is only set to 1 *after* the
11930 : initialization is complete. This ensures that an exception,
11931 : thrown during the construction, will cause the variable to
11932 : reinitialized when we pass through this code again, as per:
11933 :
11934 : [stmt.dcl]
11935 :
11936 : If the initialization exits by throwing an exception, the
11937 : initialization is not complete, so it will be tried again
11938 : the next time control enters the declaration.
11939 :
11940 : This process should be thread-safe, too; multiple threads
11941 : should not be able to initialize the variable more than
11942 : once. */
11943 :
11944 : /* Create the guard variable. */
11945 4030 : guard = get_guard (decl);
11946 :
11947 : /* Begin the conditional initialization. */
11948 4030 : if_stmt = begin_if_stmt ();
11949 :
11950 4030 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11951 4030 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11952 :
11953 4030 : if (thread_guard)
11954 : {
11955 3882 : tree vfntype = NULL_TREE;
11956 3882 : tree acquire_name, release_name, abort_name;
11957 3882 : tree acquire_fn, release_fn, abort_fn;
11958 3882 : guard_addr = build_address (guard);
11959 :
11960 3882 : acquire_name = get_identifier ("__cxa_guard_acquire");
11961 3882 : release_name = get_identifier ("__cxa_guard_release");
11962 3882 : abort_name = get_identifier ("__cxa_guard_abort");
11963 3882 : acquire_fn = get_global_binding (acquire_name);
11964 3882 : release_fn = get_global_binding (release_name);
11965 3882 : abort_fn = get_global_binding (abort_name);
11966 3882 : if (!acquire_fn)
11967 2247 : acquire_fn = push_library_fn
11968 2247 : (acquire_name, build_function_type_list (integer_type_node,
11969 2247 : TREE_TYPE (guard_addr),
11970 : NULL_TREE),
11971 : NULL_TREE, ECF_NOTHROW);
11972 3882 : if (!release_fn || !abort_fn)
11973 2250 : vfntype = build_function_type_list (void_type_node,
11974 2250 : TREE_TYPE (guard_addr),
11975 : NULL_TREE);
11976 2250 : if (!release_fn)
11977 2250 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11978 : ECF_NOTHROW);
11979 3882 : if (!abort_fn)
11980 2250 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11981 : ECF_NOTHROW | ECF_LEAF);
11982 :
11983 3882 : inner_if_stmt = begin_if_stmt ();
11984 3882 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11985 : inner_if_stmt);
11986 :
11987 3882 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11988 3882 : begin = get_internal_target_expr (boolean_false_node);
11989 3882 : flag = TARGET_EXPR_SLOT (begin);
11990 :
11991 3882 : TARGET_EXPR_CLEANUP (begin)
11992 3882 : = build3 (COND_EXPR, void_type_node, flag,
11993 : void_node,
11994 : build_call_n (abort_fn, 1, guard_addr));
11995 3882 : CLEANUP_EH_ONLY (begin) = 1;
11996 :
11997 : /* Do the initialization itself. */
11998 3882 : init = add_stmt_to_compound (begin, init);
11999 3882 : init = add_stmt_to_compound (init,
12000 : build2 (MODIFY_EXPR, void_type_node,
12001 : flag, boolean_true_node));
12002 :
12003 : /* Use atexit to register a function for destroying this static
12004 : variable. Do this before calling __cxa_guard_release. */
12005 3882 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
12006 :
12007 3882 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
12008 : guard_addr));
12009 : }
12010 : else
12011 : {
12012 148 : init = add_stmt_to_compound (init, set_guard (guard));
12013 :
12014 : /* Use atexit to register a function for destroying this static
12015 : variable. */
12016 148 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
12017 : }
12018 :
12019 4030 : finish_expr_stmt (init);
12020 :
12021 4030 : if (thread_guard)
12022 : {
12023 3882 : finish_compound_stmt (inner_then_clause);
12024 3882 : finish_then_clause (inner_if_stmt);
12025 3882 : finish_if_stmt (inner_if_stmt);
12026 : }
12027 :
12028 4030 : finish_compound_stmt (then_clause);
12029 4030 : finish_then_clause (if_stmt);
12030 4030 : finish_if_stmt (if_stmt);
12031 : }
12032 11537 : else if (CP_DECL_THREAD_LOCAL_P (decl))
12033 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
12034 : else
12035 10742 : static_aggregates = tree_cons (init, decl, static_aggregates);
12036 : }
12037 :
12038 :
12039 : /* Make TYPE a complete type based on INITIAL_VALUE.
12040 : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
12041 : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
12042 : 3 if the initializer list is empty (in pedantic mode). */
12043 :
12044 : int
12045 632318 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
12046 : {
12047 632318 : int failure;
12048 632318 : tree type, elt_type;
12049 :
12050 : /* Don't get confused by a CONSTRUCTOR for some other type. */
12051 632288 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
12052 150968 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
12053 632650 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
12054 : return 1;
12055 :
12056 632315 : if (initial_value)
12057 : {
12058 : /* An array of character type can be initialized from a
12059 : brace-enclosed string constant so call reshape_init to
12060 : remove the optional braces from a braced string literal. */
12061 632285 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
12062 632285 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
12063 51921 : initial_value = reshape_init (*ptype, initial_value,
12064 : tf_warning_or_error);
12065 :
12066 : /* If any of the elements are parameter packs, we can't actually
12067 : complete this type now because the array size is dependent. */
12068 632285 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
12069 49749826 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
12070 49297318 : if (PACK_EXPANSION_P (e.value))
12071 : return 0;
12072 : }
12073 :
12074 632315 : failure = complete_array_type (ptype, initial_value, do_default);
12075 :
12076 : /* We can create the array before the element type is complete, which
12077 : means that we didn't have these two bits set in the original type
12078 : either. In completing the type, we are expected to propagate these
12079 : bits. See also complete_type which does the same thing for arrays
12080 : of fixed size. */
12081 632315 : type = *ptype;
12082 1264627 : if (type != error_mark_node && TYPE_DOMAIN (type))
12083 : {
12084 632288 : elt_type = TREE_TYPE (type);
12085 632288 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
12086 1264576 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
12087 632288 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
12088 : }
12089 :
12090 : return failure;
12091 : }
12092 :
12093 : /* As above, but either give an error or reject zero-size arrays, depending
12094 : on COMPLAIN. */
12095 :
12096 : int
12097 1253 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
12098 : bool do_default, tsubst_flags_t complain)
12099 : {
12100 1253 : int failure;
12101 1253 : bool sfinae = !(complain & tf_error);
12102 : /* In SFINAE context we can't be lenient about zero-size arrays. */
12103 1253 : if (sfinae)
12104 3 : ++pedantic;
12105 1253 : failure = cp_complete_array_type (ptype, initial_value, do_default);
12106 1253 : if (sfinae)
12107 3 : --pedantic;
12108 1253 : if (failure)
12109 : {
12110 3 : if (sfinae)
12111 : /* Not an error. */;
12112 0 : else if (failure == 1)
12113 0 : error ("initializer fails to determine size of %qT", *ptype);
12114 0 : else if (failure == 2)
12115 : {
12116 0 : if (do_default)
12117 0 : error ("array size missing in %qT", *ptype);
12118 : }
12119 0 : else if (failure == 3)
12120 0 : error ("zero-size array %qT", *ptype);
12121 3 : *ptype = error_mark_node;
12122 : }
12123 1253 : return failure;
12124 : }
12125 :
12126 : /* Return zero if something is declared to be a member of type
12127 : CTYPE when in the context of CUR_TYPE. STRING is the error
12128 : message to print in that case. Otherwise, quietly return 1. */
12129 :
12130 : static int
12131 27730509 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
12132 : {
12133 27730509 : if (ctype && ctype != cur_type)
12134 : {
12135 0 : if (flags == DTOR_FLAG)
12136 0 : error ("destructor for alien class %qT cannot be a member", ctype);
12137 : else
12138 0 : error ("constructor for alien class %qT cannot be a member", ctype);
12139 : return 0;
12140 : }
12141 : return 1;
12142 : }
12143 :
12144 : /* Subroutine of `grokdeclarator'. */
12145 :
12146 : /* Generate errors possibly applicable for a given set of specifiers.
12147 : This is for ARM $7.1.2. */
12148 :
12149 : static void
12150 465783492 : bad_specifiers (tree object,
12151 : enum bad_spec_place type,
12152 : int virtualp,
12153 : int quals,
12154 : int inlinep,
12155 : int friendp,
12156 : int raises,
12157 : const location_t* locations)
12158 : {
12159 465783492 : switch (type)
12160 : {
12161 72071729 : case BSP_VAR:
12162 72071729 : if (virtualp)
12163 0 : error_at (locations[ds_virtual],
12164 : "%qD declared as a %<virtual%> variable", object);
12165 72071729 : if (quals)
12166 3 : error ("%<const%> and %<volatile%> function specifiers on "
12167 : "%qD invalid in variable declaration", object);
12168 : break;
12169 309314349 : case BSP_PARM:
12170 309314349 : if (virtualp)
12171 0 : error_at (locations[ds_virtual],
12172 : "%qD declared as a %<virtual%> parameter", object);
12173 309314349 : if (inlinep)
12174 6 : error_at (locations[ds_inline],
12175 : "%qD declared as an %<inline%> parameter", object);
12176 309314349 : if (quals)
12177 3 : error ("%<const%> and %<volatile%> function specifiers on "
12178 : "%qD invalid in parameter declaration", object);
12179 : break;
12180 49694397 : case BSP_TYPE:
12181 49694397 : if (virtualp)
12182 0 : error_at (locations[ds_virtual],
12183 : "%qD declared as a %<virtual%> type", object);
12184 49694397 : if (inlinep)
12185 6 : error_at (locations[ds_inline],
12186 : "%qD declared as an %<inline%> type", object);
12187 49694397 : if (quals)
12188 0 : error ("%<const%> and %<volatile%> function specifiers on "
12189 : "%qD invalid in type declaration", object);
12190 : break;
12191 34703017 : case BSP_FIELD:
12192 34703017 : if (virtualp)
12193 6 : error_at (locations[ds_virtual],
12194 : "%qD declared as a %<virtual%> field", object);
12195 34703017 : if (inlinep)
12196 6 : error_at (locations[ds_inline],
12197 : "%qD declared as an %<inline%> field", object);
12198 34703017 : if (quals)
12199 0 : error ("%<const%> and %<volatile%> function specifiers on "
12200 : "%qD invalid in field declaration", object);
12201 : break;
12202 0 : default:
12203 0 : gcc_unreachable();
12204 : }
12205 465783492 : if (friendp)
12206 0 : error ("%q+D declared as a friend", object);
12207 465783492 : if (raises
12208 131 : && !flag_noexcept_type
12209 465783517 : && (TREE_CODE (object) == TYPE_DECL
12210 19 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
12211 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
12212 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
12213 6 : error ("%q+D declared with an exception specification", object);
12214 465783492 : }
12215 :
12216 : /* DECL is a member function or static data member and is presently
12217 : being defined. Check that the definition is taking place in a
12218 : valid namespace. */
12219 :
12220 : static void
12221 102507069 : check_class_member_definition_namespace (tree decl)
12222 : {
12223 : /* These checks only apply to member functions and static data
12224 : members. */
12225 102507069 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
12226 : /* We check for problems with specializations in pt.cc in
12227 : check_specialization_namespace, where we can issue better
12228 : diagnostics. */
12229 102507069 : if (processing_specialization)
12230 : return;
12231 : /* We check this in check_explicit_instantiation_namespace. */
12232 102361375 : if (processing_explicit_instantiation)
12233 : return;
12234 : /* [class.mfct]
12235 :
12236 : A member function definition that appears outside of the
12237 : class definition shall appear in a namespace scope enclosing
12238 : the class definition.
12239 :
12240 : [class.static.data]
12241 :
12242 : The definition for a static data member shall appear in a
12243 : namespace scope enclosing the member's class definition. */
12244 102361295 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
12245 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
12246 18 : decl, DECL_CONTEXT (decl));
12247 : }
12248 :
12249 : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
12250 : METHOD_TYPE for a non-static member function; QUALS are the
12251 : cv-qualifiers that apply to the function. */
12252 :
12253 : tree
12254 248138898 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
12255 : {
12256 248138898 : tree this_type;
12257 248138898 : tree qual_type;
12258 248138898 : tree parm;
12259 248138898 : cp_cv_quals this_quals;
12260 :
12261 248138898 : if (CLASS_TYPE_P (type))
12262 : {
12263 108326818 : this_type
12264 108326818 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
12265 108326818 : this_type = build_pointer_type (this_type);
12266 : }
12267 : else
12268 139812080 : this_type = type_of_this_parm (type);
12269 : /* The `this' parameter is implicitly `const'; it cannot be
12270 : assigned to. */
12271 248138898 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
12272 248138898 : qual_type = cp_build_qualified_type (this_type, this_quals);
12273 248138898 : parm = build_artificial_parm (fn, this_identifier, qual_type);
12274 248138898 : cp_apply_type_quals_to_decl (this_quals, parm);
12275 248138898 : return parm;
12276 : }
12277 :
12278 : /* DECL is a static member function. Complain if it was declared
12279 : with function-cv-quals. */
12280 :
12281 : static void
12282 11188253 : check_static_quals (tree decl, cp_cv_quals quals)
12283 : {
12284 0 : if (quals != TYPE_UNQUALIFIED)
12285 6 : error ("static member function %q#D declared with type qualifiers",
12286 : decl);
12287 0 : }
12288 :
12289 : /* Helper function. Replace the temporary this parameter injected
12290 : during cp_finish_omp_declare_simd with the real this parameter. */
12291 :
12292 : static tree
12293 2692 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
12294 : {
12295 2692 : tree this_parm = (tree) data;
12296 2692 : if (TREE_CODE (*tp) == PARM_DECL
12297 450 : && DECL_NAME (*tp) == this_identifier
12298 2965 : && *tp != this_parm)
12299 273 : *tp = this_parm;
12300 2419 : else if (TYPE_P (*tp))
12301 33 : *walk_subtrees = 0;
12302 2692 : return NULL_TREE;
12303 : }
12304 :
12305 : /* CTYPE is class type, or null if non-class.
12306 : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
12307 : or METHOD_TYPE.
12308 : DECLARATOR is the function's name.
12309 : PARMS is a chain of PARM_DECLs for the function.
12310 : VIRTUALP is truthvalue of whether the function is virtual or not.
12311 : FLAGS are to be passed through to `grokclassfn'.
12312 : QUALS are qualifiers indicating whether the function is `const'
12313 : or `volatile'.
12314 : RAISES is a list of exceptions that this function can raise.
12315 : CHECK is 1 if we must find this method in CTYPE, 0 if we should
12316 : not look, and -1 if we should not call `grokclassfn' at all.
12317 :
12318 : SFK is the kind of special function (if any) for the new function.
12319 :
12320 : Returns `NULL_TREE' if something goes wrong, after issuing
12321 : applicable error messages. */
12322 :
12323 : static tree
12324 197812220 : grokfndecl (tree ctype,
12325 : tree type,
12326 : tree declarator,
12327 : tree parms,
12328 : tree orig_declarator,
12329 : const cp_decl_specifier_seq *declspecs,
12330 : tree decl_reqs,
12331 : int virtualp,
12332 : enum overload_flags flags,
12333 : cp_cv_quals quals,
12334 : cp_ref_qualifier rqual,
12335 : tree raises,
12336 : int check,
12337 : int friendp,
12338 : int publicp,
12339 : int inlinep,
12340 : int initialized,
12341 : bool xobj_func_p,
12342 : special_function_kind sfk,
12343 : bool funcdef_flag,
12344 : bool late_return_type_p,
12345 : int template_count,
12346 : tree in_namespace,
12347 : tree* attrlist,
12348 : tree contract_specifiers,
12349 : location_t location)
12350 : {
12351 197812220 : tree decl;
12352 197812220 : tree t;
12353 :
12354 197812220 : if (location == UNKNOWN_LOCATION)
12355 0 : location = input_location;
12356 :
12357 : /* Was the concept specifier present? */
12358 197812220 : bool concept_p = inlinep & 4;
12359 :
12360 197812220 : if (concept_p)
12361 : {
12362 15 : error_at (location, "function concepts are no longer supported");
12363 15 : return NULL_TREE;
12364 : }
12365 :
12366 : /* [except.spec]/9 - A deallocation function with no explicit noexcept-specifier
12367 : has a non-throwing exception specification. */
12368 197812205 : if (raises == NULL_TREE
12369 118391591 : && cxx_dialect >= cxx11
12370 142937484 : && IDENTIFIER_NEWDEL_OP_P (declarator)
12371 197878358 : && !IDENTIFIER_NEW_OP_P (declarator))
12372 919 : raises = noexcept_true_spec;
12373 :
12374 197812205 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
12375 :
12376 197812205 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
12377 :
12378 : /* Set the constraints on the declaration. */
12379 197812205 : if (flag_concepts)
12380 : {
12381 193431397 : tree tmpl_reqs = NULL_TREE;
12382 193431397 : tree ctx = friendp ? current_class_type : ctype;
12383 193431397 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
12384 193431397 : bool memtmpl = (!block_local
12385 193431397 : && (current_template_depth
12386 193372136 : > template_class_depth (ctx)));
12387 53458238 : if (memtmpl)
12388 : {
12389 53458238 : if (!current_template_parms)
12390 : /* If there are no template parameters, something must have
12391 : gone wrong. */
12392 0 : gcc_assert (seen_error ());
12393 : else
12394 53458238 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12395 : }
12396 193431397 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
12397 : /* C++20 CA378: Remove non-templated constrained functions. */
12398 : /* [temp.friend]/9 A non-template friend declaration with a
12399 : requires-clause shall be a definition. A friend function template with
12400 : a constraint that depends on a template parameter from an enclosing
12401 : template shall be a definition. */
12402 193431397 : if (ci
12403 10432434 : && (block_local
12404 10432431 : || !processing_template_decl
12405 10432370 : || (friendp && !memtmpl && !funcdef_flag)))
12406 : {
12407 73 : if (!friendp || !processing_template_decl)
12408 64 : error_at (location, "constraints on a non-templated function");
12409 : else
12410 9 : error_at (location, "constrained non-template friend declaration"
12411 : " must be a definition");
12412 : ci = NULL_TREE;
12413 : }
12414 193431397 : set_constraints (decl, ci);
12415 1300959 : if (ci && friendp && memtmpl && !funcdef_flag
12416 193469259 : && uses_outer_template_parms_in_constraints (decl, ctx))
12417 6 : error_at (location, "friend function template with constraints that "
12418 : "depend on outer template parameters must be a definition");
12419 : }
12420 :
12421 197812205 : if (TREE_CODE (type) == METHOD_TYPE)
12422 : {
12423 106782767 : tree parm = build_this_parm (decl, type, quals);
12424 106782767 : DECL_CHAIN (parm) = parms;
12425 106782767 : parms = parm;
12426 :
12427 : /* Allocate space to hold the vptr bit if needed. */
12428 106782767 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12429 : }
12430 :
12431 197812205 : DECL_ARGUMENTS (decl) = parms;
12432 597373995 : for (t = parms; t; t = DECL_CHAIN (t))
12433 : {
12434 399561790 : DECL_CONTEXT (t) = decl;
12435 399561790 : if (flag_reflection
12436 8043849 : && initialized == SD_INITIALIZED
12437 401181183 : && DECL_ATTRIBUTES (t))
12438 3668 : for (tree a = DECL_ATTRIBUTES (t);
12439 3672 : (a = lookup_annotation (a));
12440 4 : a = TREE_CHAIN (a))
12441 : {
12442 4 : gcc_checking_assert (TREE_CODE (TREE_VALUE (a)) == TREE_LIST);
12443 : /* Mark TREE_PURPOSE of the value that it is an annotation
12444 : on an argument of a function definition (rather than
12445 : annotation from function declaration). For function parameter
12446 : reflection all annotations are listed, while for variable_of
12447 : only those marked here. Annotation is marked as coming from
12448 : function definition's argument if it has TREE_PURPOSE
12449 : void_node or INTEGER_CST with signed type. */
12450 4 : tree val = TREE_VALUE (a);
12451 4 : gcc_assert (TREE_PURPOSE (val) == NULL_TREE);
12452 4 : TREE_PURPOSE (val) = void_node;
12453 : }
12454 : }
12455 :
12456 : /* Propagate volatile out from type to decl. */
12457 197812205 : if (TYPE_VOLATILE (type))
12458 0 : TREE_THIS_VOLATILE (decl) = 1;
12459 :
12460 : /* Setup decl according to sfk. */
12461 197812205 : switch (sfk)
12462 : {
12463 25333322 : case sfk_constructor:
12464 25333322 : case sfk_copy_constructor:
12465 25333322 : case sfk_move_constructor:
12466 25333322 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12467 25333322 : DECL_NAME (decl) = ctor_identifier;
12468 25333322 : break;
12469 3392405 : case sfk_destructor:
12470 3392405 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
12471 3392405 : DECL_NAME (decl) = dtor_identifier;
12472 3392405 : break;
12473 1275335 : case sfk_deduction_guide:
12474 : /* Give deduction guides a definition even though they don't really
12475 : have one: the restriction that you can't repeat a deduction guide
12476 : makes them more like a definition anyway. */
12477 1275335 : DECL_INITIAL (decl) = void_node;
12478 : /* But to ensure that external-linkage deduction guides in header units
12479 : don't fall afoul of [module.import] p6, mark them as inline. */
12480 1275335 : DECL_DECLARED_INLINE_P (decl) = true;
12481 1275335 : break;
12482 : default:
12483 : break;
12484 : }
12485 :
12486 197812205 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12487 : {
12488 113493 : if (funcdef_flag)
12489 3 : error_at (location,
12490 : "defining explicit specialization %qD in friend declaration",
12491 : orig_declarator);
12492 : else
12493 : {
12494 113490 : tree fns = TREE_OPERAND (orig_declarator, 0);
12495 113490 : tree args = TREE_OPERAND (orig_declarator, 1);
12496 :
12497 113490 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12498 : {
12499 : /* Something like `template <class T> friend void f<T>()'. */
12500 6 : error_at (location,
12501 : "invalid use of template-id %qD in declaration "
12502 : "of primary template",
12503 : orig_declarator);
12504 6 : return NULL_TREE;
12505 : }
12506 :
12507 : /* A friend declaration of the form friend void f<>(). Record
12508 : the information in the TEMPLATE_ID_EXPR. */
12509 113484 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
12510 :
12511 113484 : gcc_assert (identifier_p (fns) || OVL_P (fns));
12512 113484 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12513 :
12514 : /* Remember the befriending class like push_template_decl does for
12515 : template friends. */
12516 113484 : gcc_checking_assert (!DECL_CHAIN (decl));
12517 113484 : DECL_CHAIN (decl) = current_scope ();
12518 :
12519 376269 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12520 262790 : if (TREE_PURPOSE (t)
12521 262790 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12522 : {
12523 5 : error_at (defparse_location (TREE_PURPOSE (t)),
12524 : "default arguments are not allowed in declaration "
12525 : "of friend template specialization %qD",
12526 : decl);
12527 5 : return NULL_TREE;
12528 : }
12529 :
12530 113479 : if (inlinep & 1)
12531 : {
12532 5 : error_at (declspecs->locations[ds_inline],
12533 : "%<inline%> is not allowed in declaration of friend "
12534 : "template specialization %qD",
12535 : decl);
12536 5 : return NULL_TREE;
12537 : }
12538 : }
12539 : }
12540 :
12541 : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12542 : expression, that declaration shall be a definition..." */
12543 197812189 : if (friendp && !funcdef_flag)
12544 : {
12545 1924593 : bool has_errored = false;
12546 1924593 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12547 5396116 : t && t != void_list_node; t = TREE_CHAIN (t))
12548 3471523 : if (TREE_PURPOSE (t))
12549 : {
12550 51 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12551 : /* For templates, mark the default argument as erroneous and give a
12552 : hard error. */
12553 51 : if (processing_template_decl)
12554 : {
12555 33 : diag_kind = diagnostics::kind::error;
12556 33 : TREE_PURPOSE (t) = error_mark_node;
12557 : }
12558 51 : if (!has_errored)
12559 : {
12560 48 : has_errored = true;
12561 48 : emit_diagnostic (diag_kind,
12562 48 : DECL_SOURCE_LOCATION (decl),
12563 48 : /*diagnostics::option_id=*/0,
12564 : "friend declaration of %qD specifies default "
12565 : "arguments and isn%'t a definition", decl);
12566 : }
12567 : }
12568 : }
12569 :
12570 : /* If this decl has namespace scope, set that up. */
12571 197812189 : if (in_namespace)
12572 265774 : set_decl_namespace (decl, in_namespace, friendp);
12573 197546415 : else if (ctype)
12574 117881663 : DECL_CONTEXT (decl) = ctype;
12575 : else
12576 79664752 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12577 :
12578 : /* `main' and builtins have implicit 'C' linkage. */
12579 197812189 : if (ctype == NULL_TREE
12580 79930526 : && DECL_FILE_SCOPE_P (decl)
12581 32911399 : && current_lang_name == lang_name_cplusplus
12582 200875910 : && (MAIN_NAME_P (declarator)
12583 3029600 : || (IDENTIFIER_LENGTH (declarator) > 10
12584 2174149 : && IDENTIFIER_POINTER (declarator)[0] == '_'
12585 1678497 : && IDENTIFIER_POINTER (declarator)[1] == '_'
12586 361156 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
12587 : "builtin_"))
12588 3029582 : || (targetcm.cxx_implicit_extern_c
12589 0 : && (targetcm.cxx_implicit_extern_c
12590 0 : (IDENTIFIER_POINTER (declarator))))))
12591 34139 : SET_DECL_LANGUAGE (decl, lang_c);
12592 :
12593 197812189 : DECL_STATIC_FUNCTION_P (decl)
12594 197812189 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12595 197812189 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12596 :
12597 197812189 : if (initialized == SD_DELETED)
12598 4374397 : DECL_DELETED_FN (decl) = 1;
12599 :
12600 197812189 : if (ctype && funcdef_flag)
12601 101983135 : check_class_member_definition_namespace (decl);
12602 :
12603 197812189 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12604 : {
12605 34127 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
12606 9 : error_at (location, "cannot declare %<::main%> to be a template");
12607 34127 : if (inlinep & 1)
12608 3 : error_at (declspecs->locations[ds_inline],
12609 : "cannot declare %<::main%> to be inline");
12610 34127 : if (inlinep & 2)
12611 6 : error_at (declspecs->locations[ds_constexpr],
12612 : "cannot declare %<::main%> to be %qs", "constexpr");
12613 34127 : if (inlinep & 8)
12614 3 : error_at (declspecs->locations[ds_consteval],
12615 : "cannot declare %<::main%> to be %qs", "consteval");
12616 34127 : if (!publicp)
12617 0 : error_at (location, "cannot declare %<::main%> to be static");
12618 34127 : if (current_lang_name != lang_name_cplusplus)
12619 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12620 : " linkage specification other than %<extern \"C++\"%>");
12621 34127 : if (module_attach_p ())
12622 : {
12623 3 : auto_diagnostic_group adg;
12624 3 : error_at (location, "cannot attach %<::main%> to a named module");
12625 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
12626 : "global module instead");
12627 3 : }
12628 : inlinep = 0;
12629 : publicp = 1;
12630 : }
12631 :
12632 : /* Members of anonymous types and local classes have no linkage; make
12633 : them internal. If a typedef is made later, this will be changed. */
12634 197812189 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12635 : publicp = 0;
12636 197797570 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12637 : /* But members of local classes in a module CMI should have their
12638 : definitions exported, in case they are (directly or indirectly)
12639 : used by an importer. We don't just use module_has_cmi_p here
12640 : because for entities in the GMF we don't yet know whether this
12641 : module will have a CMI, so we'll conservatively assume it might. */
12642 2458855 : publicp = module_maybe_has_cmi_p ();
12643 :
12644 197797570 : if (publicp && cxx_dialect == cxx98)
12645 : {
12646 : /* [basic.link]: A name with no linkage (notably, the name of a class
12647 : or enumeration declared in a local scope) shall not be used to
12648 : declare an entity with linkage.
12649 :
12650 : DR 757 relaxes this restriction for C++0x. */
12651 1165017 : no_linkage_error (decl);
12652 : }
12653 :
12654 197812189 : TREE_PUBLIC (decl) = publicp;
12655 197812189 : if (! publicp)
12656 : {
12657 2579924 : DECL_INTERFACE_KNOWN (decl) = 1;
12658 2579924 : DECL_NOT_REALLY_EXTERN (decl) = 1;
12659 : }
12660 :
12661 : /* If the declaration was declared inline, mark it as such. */
12662 197812189 : if (inlinep)
12663 : {
12664 76458892 : DECL_DECLARED_INLINE_P (decl) = 1;
12665 76458892 : if (publicp)
12666 76203491 : DECL_COMDAT (decl) = 1;
12667 : }
12668 76458892 : if (inlinep & 2)
12669 56535039 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12670 141277150 : else if (inlinep & 8)
12671 : {
12672 1174028 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12673 1174028 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12674 : }
12675 :
12676 197812189 : DECL_EXTERNAL (decl) = 1;
12677 197812189 : if (TREE_CODE (type) == FUNCTION_TYPE)
12678 : {
12679 91029422 : if (quals || rqual)
12680 393 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12681 : TYPE_UNQUALIFIED,
12682 : REF_QUAL_NONE);
12683 91029422 : auto_diagnostic_group d;
12684 91029422 : if (quals)
12685 621 : error (!ctype
12686 : ? G_("non-member function %qD cannot have cv-qualifier")
12687 : : !xobj_func_p
12688 297 : ? G_("static member function %qD cannot have cv-qualifier")
12689 : : G_("explicit object member function "
12690 : "%qD cannot have cv-qualifier"),
12691 : decl);
12692 91029422 : if (rqual)
12693 531 : error (!ctype
12694 : ? G_("non-member function %qD cannot have ref-qualifier")
12695 : : !xobj_func_p
12696 264 : ? G_("static member function %qD cannot have ref-qualifier")
12697 : : G_("explicit object member function "
12698 : "%qD cannot have ref-qualifier"),
12699 : decl);
12700 :
12701 91029422 : if (xobj_func_p && (quals || rqual))
12702 363 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12703 : "explicit object parameter declared here");
12704 91029422 : quals = TYPE_UNQUALIFIED;
12705 91029422 : rqual = REF_QUAL_NONE;
12706 :
12707 91029422 : }
12708 :
12709 197812189 : if (deduction_guide_p (decl))
12710 : {
12711 1275335 : tree type = TREE_TYPE (DECL_NAME (decl));
12712 1275335 : if (in_namespace == NULL_TREE
12713 1275335 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12714 : {
12715 3 : auto_diagnostic_group d;
12716 3 : error_at (location, "deduction guide %qD must be declared in the "
12717 : "same scope as %qT", decl, type);
12718 3 : inform (location_of (type), " declared here");
12719 3 : return NULL_TREE;
12720 3 : }
12721 2550664 : if (DECL_CLASS_SCOPE_P (decl)
12722 1275373 : && current_access_specifier != declared_access (TYPE_NAME (type)))
12723 : {
12724 3 : auto_diagnostic_group d;
12725 3 : error_at (location, "deduction guide %qD must have the same access "
12726 : "as %qT", decl, type);
12727 3 : inform (location_of (type), " declared here");
12728 3 : }
12729 1275332 : if (funcdef_flag)
12730 3 : error_at (location,
12731 : "deduction guide %qD must not have a function body", decl);
12732 : }
12733 366428631 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12734 208910915 : && !grok_op_properties (decl, /*complain=*/true))
12735 : return NULL_TREE;
12736 196536707 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
12737 : {
12738 320492 : bool long_long_unsigned_p;
12739 320492 : bool long_double_p;
12740 320492 : const char *suffix = NULL;
12741 : /* [over.literal]/6: Literal operators shall not have C linkage. */
12742 320492 : if (DECL_LANGUAGE (decl) == lang_c)
12743 : {
12744 6 : auto_diagnostic_group d;
12745 6 : error_at (location, "literal operator with C linkage");
12746 6 : maybe_show_extern_c_location ();
12747 6 : return NULL_TREE;
12748 6 : }
12749 :
12750 320486 : if (DECL_NAMESPACE_SCOPE_P (decl))
12751 : {
12752 320480 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12753 : &long_double_p))
12754 : {
12755 42 : error_at (location, "%qD has invalid argument list", decl);
12756 42 : return NULL_TREE;
12757 : }
12758 :
12759 320438 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12760 320438 : if (long_long_unsigned_p)
12761 : {
12762 47369 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
12763 68 : warning_at (location, 0, "integer suffix %qs"
12764 : " shadowed by implementation", suffix);
12765 : }
12766 273069 : else if (long_double_p)
12767 : {
12768 87026 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
12769 80 : warning_at (location, 0, "floating-point suffix %qs"
12770 : " shadowed by implementation", suffix);
12771 : }
12772 : /* 17.6.3.3.5 */
12773 320438 : if (suffix[0] != '_'
12774 319831 : && !current_function_decl && !(friendp && !funcdef_flag))
12775 319825 : warning_at (location, OPT_Wliteral_suffix,
12776 : "literal operator suffixes not preceded by %<_%>"
12777 : " are reserved for future standardization");
12778 : }
12779 : else
12780 : {
12781 6 : error_at (location, "%qD must be a non-member function", decl);
12782 6 : return NULL_TREE;
12783 : }
12784 : }
12785 :
12786 196536656 : if (funcdef_flag)
12787 : /* Make the init_value nonzero so pushdecl knows this is not
12788 : tentative. error_mark_node is replaced later with the BLOCK. */
12789 139562500 : DECL_INITIAL (decl) = error_mark_node;
12790 :
12791 197811985 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12792 74878524 : TREE_NOTHROW (decl) = 1;
12793 :
12794 197811985 : if (flag_openmp || flag_openmp_simd)
12795 : {
12796 : /* Adjust "omp declare simd" attributes. */
12797 483066 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
12798 483066 : if (ods)
12799 : {
12800 : tree attr;
12801 2081 : for (attr = ods; attr;
12802 1102 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
12803 : {
12804 1102 : if (TREE_CODE (type) == METHOD_TYPE)
12805 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12806 : DECL_ARGUMENTS (decl), NULL);
12807 1102 : if (TREE_VALUE (attr) != NULL_TREE)
12808 : {
12809 896 : tree cl = TREE_VALUE (TREE_VALUE (attr));
12810 896 : cl = c_omp_declare_simd_clauses_to_numbers
12811 896 : (DECL_ARGUMENTS (decl), cl);
12812 896 : if (cl)
12813 896 : TREE_VALUE (TREE_VALUE (attr)) = cl;
12814 : else
12815 0 : TREE_VALUE (attr) = NULL_TREE;
12816 : }
12817 : }
12818 : }
12819 : }
12820 :
12821 : /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
12822 : have a potentially throwing exception specification. */
12823 197811985 : maybe_diagnose_deallocation_noexcept_false (decl);
12824 :
12825 : /* Caller will do the rest of this. */
12826 197811985 : if (check < 0)
12827 : {
12828 6884038 : if (decl && decl != error_mark_node && contract_specifiers)
12829 24 : set_fn_contract_specifiers (decl, contract_specifiers);
12830 6884038 : return decl;
12831 : }
12832 :
12833 190927947 : if (ctype != NULL_TREE)
12834 117881344 : grokclassfn (ctype, decl, flags);
12835 :
12836 : /* 12.4/3 */
12837 190927947 : if (cxx_dialect >= cxx11
12838 379545340 : && DECL_DESTRUCTOR_P (decl)
12839 3370710 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12840 191099829 : && !processing_template_decl)
12841 87977 : deduce_noexcept_on_destructor (decl);
12842 :
12843 190927947 : set_originating_module (decl);
12844 :
12845 190927947 : decl = check_explicit_specialization (orig_declarator, decl,
12846 : template_count,
12847 190927947 : 2 * funcdef_flag +
12848 190927947 : 4 * (friendp != 0),
12849 : *attrlist);
12850 190927947 : if (decl == error_mark_node)
12851 : return NULL_TREE;
12852 :
12853 190927673 : if (DECL_STATIC_FUNCTION_P (decl))
12854 11005327 : check_static_quals (decl, quals);
12855 :
12856 190927673 : if (attrlist)
12857 : {
12858 190927673 : cplus_decl_attributes (&decl, *attrlist, 0);
12859 190927673 : *attrlist = NULL_TREE;
12860 : }
12861 :
12862 : /* Update now we have a decl and maybe know the return type. */
12863 190927673 : if (contract_specifiers)
12864 : {
12865 912 : tree t = decl;
12866 912 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12867 7 : t = DECL_TEMPLATE_RESULT (decl);
12868 912 : set_fn_contract_specifiers (t, contract_specifiers);
12869 912 : rebuild_postconditions (t);
12870 : }
12871 :
12872 : /* Check main's type after attributes have been applied. */
12873 190927673 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12874 : {
12875 34115 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12876 : integer_type_node))
12877 : {
12878 19 : tree dtype = TREE_TYPE (decl);
12879 19 : tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12880 19 : tree newtype;
12881 19 : error_at (declspecs->locations[ds_type_spec],
12882 : "%<::main%> must return %<int%>");
12883 57 : newtype = build_function_type (integer_type_node, oldtypeargs,
12884 19 : TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12885 19 : TREE_TYPE (decl) = newtype;
12886 : }
12887 34115 : if (warn_main)
12888 34093 : check_main_parameter_types (decl);
12889 : }
12890 :
12891 190927673 : if (ctype != NULL_TREE && check)
12892 : {
12893 9385947 : tree old_decl = check_classfn (ctype, decl,
12894 9385947 : (current_template_depth
12895 9385947 : > template_class_depth (ctype))
12896 2341704 : ? current_template_parms
12897 : : NULL_TREE);
12898 :
12899 9385947 : if (old_decl == error_mark_node)
12900 : return NULL_TREE;
12901 :
12902 9385839 : if (old_decl)
12903 : {
12904 8976997 : tree ok;
12905 8976997 : tree pushed_scope;
12906 :
12907 8976997 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12908 : /* Because grokfndecl is always supposed to return a
12909 : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12910 : here. We depend on our callers to figure out that its
12911 : really a template that's being returned. */
12912 2341692 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
12913 :
12914 8976997 : if (DECL_STATIC_FUNCTION_P (old_decl)
12915 8976997 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12916 : {
12917 : /* Remove the `this' parm added by grokclassfn. */
12918 182926 : revert_static_member_fn (decl);
12919 182926 : check_static_quals (decl, quals);
12920 : }
12921 8976997 : if (DECL_ARTIFICIAL (old_decl))
12922 : {
12923 9 : error ("definition of implicitly-declared %qD", old_decl);
12924 9 : return NULL_TREE;
12925 : }
12926 8976988 : else if (DECL_DEFAULTED_FN (old_decl)
12927 8976990 : && !(initialized == SD_DEFAULTED
12928 2 : && redefinable_import_p (old_decl)))
12929 : {
12930 3 : auto_diagnostic_group d;
12931 3 : error ("definition of explicitly-defaulted %q+D", decl);
12932 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12933 : "%q#D explicitly defaulted here", old_decl);
12934 3 : return NULL_TREE;
12935 3 : }
12936 :
12937 8976985 : if (modules_p () && !module_may_redeclare (old_decl))
12938 : return NULL_TREE;
12939 :
12940 : /* Since we've smashed OLD_DECL to its
12941 : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12942 8976925 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12943 1209 : decl = DECL_TEMPLATE_RESULT (decl);
12944 :
12945 : /* Attempt to merge the declarations. This can fail, in
12946 : the case of some invalid specialization declarations. */
12947 8976925 : pushed_scope = push_scope (ctype);
12948 8976925 : ok = duplicate_decls (decl, old_decl);
12949 8976925 : if (pushed_scope)
12950 8976925 : pop_scope (pushed_scope);
12951 8976925 : if (!ok)
12952 : {
12953 0 : error ("no %q#D member function declared in class %qT",
12954 : decl, ctype);
12955 0 : return NULL_TREE;
12956 : }
12957 8976925 : if (ok == error_mark_node)
12958 : return NULL_TREE;
12959 8976901 : return old_decl;
12960 : }
12961 : }
12962 :
12963 363901136 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12964 : return NULL_TREE;
12965 :
12966 181950565 : if (ctype == NULL_TREE || check)
12967 73455240 : return decl;
12968 :
12969 108495325 : if (virtualp)
12970 4303402 : DECL_VIRTUAL_P (decl) = 1;
12971 :
12972 108495325 : return decl;
12973 : }
12974 :
12975 : /* decl is a FUNCTION_DECL.
12976 : specifiers are the parsed virt-specifiers.
12977 :
12978 : Set flags to reflect the virt-specifiers.
12979 :
12980 : Returns decl. */
12981 :
12982 : static tree
12983 115379460 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12984 : {
12985 115379460 : if (decl == NULL_TREE)
12986 : return decl;
12987 115379363 : if (specifiers & VIRT_SPEC_OVERRIDE)
12988 396229 : DECL_OVERRIDE_P (decl) = 1;
12989 115379363 : if (specifiers & VIRT_SPEC_FINAL)
12990 10032 : DECL_FINAL_P (decl) = 1;
12991 : return decl;
12992 : }
12993 :
12994 : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12995 : the linkage that DECL will receive in the object file. */
12996 :
12997 : static void
12998 14353626 : set_linkage_for_static_data_member (tree decl)
12999 : {
13000 : /* A static data member always has static storage duration and
13001 : external linkage. Note that static data members are forbidden in
13002 : local classes -- the only situation in which a class has
13003 : non-external linkage. */
13004 14353626 : TREE_PUBLIC (decl) = 1;
13005 14353626 : TREE_STATIC (decl) = 1;
13006 : /* For non-template classes, static data members are always put
13007 : out in exactly those files where they are defined, just as
13008 : with ordinary namespace-scope variables. */
13009 14353626 : if (!processing_template_decl)
13010 10967157 : DECL_INTERFACE_KNOWN (decl) = 1;
13011 14353626 : }
13012 :
13013 : /* Create a VAR_DECL named NAME with the indicated TYPE.
13014 :
13015 : If SCOPE is non-NULL, it is the class type or namespace containing
13016 : the variable. If SCOPE is NULL, the variable should is created in
13017 : the innermost enclosing scope. */
13018 :
13019 : static tree
13020 72071821 : grokvardecl (tree type,
13021 : tree name,
13022 : tree orig_declarator,
13023 : const cp_decl_specifier_seq *declspecs,
13024 : int initialized,
13025 : int type_quals,
13026 : int inlinep,
13027 : bool conceptp,
13028 : int template_count,
13029 : tree scope,
13030 : location_t location)
13031 : {
13032 72071821 : tree decl;
13033 72071821 : tree explicit_scope;
13034 :
13035 72071821 : gcc_assert (!name || identifier_p (name));
13036 :
13037 72071821 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
13038 72071821 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
13039 :
13040 : /* Compute the scope in which to place the variable, but remember
13041 : whether or not that scope was explicitly specified by the user. */
13042 72071821 : explicit_scope = scope;
13043 72071821 : if (!scope)
13044 : {
13045 : /* An explicit "extern" specifier indicates a namespace-scope
13046 : variable. */
13047 71531158 : if (declspecs->storage_class == sc_extern)
13048 567717 : scope = current_decl_namespace ();
13049 70963441 : else if (!at_function_scope_p ())
13050 7555452 : scope = current_scope ();
13051 : }
13052 :
13053 71531158 : if (scope
13054 8663832 : && (/* If the variable is a namespace-scope variable declared in a
13055 : template, we need DECL_LANG_SPECIFIC. */
13056 8663832 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
13057 : /* Similarly for namespace-scope variables with language linkage
13058 : other than C++. */
13059 8139898 : || (TREE_CODE (scope) == NAMESPACE_DECL
13060 4662619 : && current_lang_name != lang_name_cplusplus)
13061 : /* Similarly for static data members. */
13062 4942521 : || TYPE_P (scope)
13063 : /* Similarly for explicit specializations. */
13064 4418587 : || (orig_declarator
13065 4417737 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
13066 5199408 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
13067 : else
13068 66872413 : decl = build_decl (location, VAR_DECL, name, type);
13069 :
13070 72071821 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
13071 16729 : set_decl_namespace (decl, explicit_scope, 0);
13072 : else
13073 72055092 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
13074 :
13075 72071821 : if (declspecs->storage_class == sc_extern)
13076 : {
13077 567717 : DECL_THIS_EXTERN (decl) = 1;
13078 567717 : DECL_EXTERNAL (decl) = !initialized;
13079 : }
13080 :
13081 72071821 : if (DECL_CLASS_SCOPE_P (decl))
13082 : {
13083 523934 : set_linkage_for_static_data_member (decl);
13084 : /* This function is only called with out-of-class definitions. */
13085 523934 : DECL_EXTERNAL (decl) = 0;
13086 523934 : check_class_member_definition_namespace (decl);
13087 : }
13088 : /* At top level, either `static' or no s.c. makes a definition
13089 : (perhaps tentative), and absence of `static' makes it public. */
13090 71547887 : else if (toplevel_bindings_p ())
13091 : {
13092 16277468 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
13093 8138734 : && (DECL_THIS_EXTERN (decl)
13094 7532810 : || ! constp
13095 7532810 : || volatilep
13096 7453309 : || inlinep
13097 610403 : || in_template_context
13098 31744 : || processing_specialization
13099 31661 : || module_attach_p ()));
13100 8138734 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
13101 : }
13102 : /* Not at top level, only `static' makes a static definition. */
13103 : else
13104 : {
13105 63409153 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
13106 63409153 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
13107 : }
13108 :
13109 72071821 : set_originating_module (decl);
13110 :
13111 72071821 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
13112 : {
13113 20220 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
13114 : {
13115 20212 : CP_DECL_THREAD_LOCAL_P (decl) = true;
13116 : // NB: Set a tentative TLS model to avoid tls_model attribute
13117 : // warnings due to lack of thread storage duration. It will
13118 : // be updated by cplus_decl_attributes later.
13119 20212 : if (!processing_template_decl)
13120 20087 : set_decl_tls_model (decl, TLS_MODEL_REAL);
13121 : }
13122 20220 : if (declspecs->gnu_thread_keyword_p)
13123 19213 : SET_DECL_GNU_TLS_P (decl);
13124 : }
13125 :
13126 : /* If the type of the decl has no linkage, make sure that we'll
13127 : notice that in mark_used. */
13128 72071821 : if (cxx_dialect > cxx98
13129 71765103 : && decl_linkage (decl) != lk_none
13130 71029933 : && DECL_LANG_SPECIFIC (decl) == NULL
13131 65750761 : && !DECL_EXTERN_C_P (decl)
13132 137822582 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
13133 4720 : retrofit_lang_decl (decl);
13134 :
13135 72071821 : if (TREE_PUBLIC (decl))
13136 : {
13137 : /* [basic.link]: A name with no linkage (notably, the name of a class
13138 : or enumeration declared in a local scope) shall not be used to
13139 : declare an entity with linkage.
13140 :
13141 : DR 757 relaxes this restriction for C++0x. */
13142 8592808 : if (cxx_dialect < cxx11)
13143 23924 : no_linkage_error (decl);
13144 : }
13145 : else
13146 63479013 : DECL_INTERFACE_KNOWN (decl) = 1;
13147 :
13148 72071821 : if (DECL_NAME (decl)
13149 72071821 : && MAIN_NAME_P (DECL_NAME (decl)))
13150 : {
13151 12 : if (scope == global_namespace)
13152 6 : error_at (DECL_SOURCE_LOCATION (decl),
13153 : "cannot declare %<::main%> to be a global variable");
13154 6 : else if (DECL_EXTERN_C_P (decl))
13155 3 : error_at (DECL_SOURCE_LOCATION (decl),
13156 : "an entity named %<main%> cannot be declared with "
13157 : "C language linkage");
13158 : }
13159 :
13160 : /* Check if a variable is being declared as a concept. */
13161 72071821 : if (conceptp)
13162 : {
13163 30 : if (!processing_template_decl)
13164 13 : error_at (declspecs->locations[ds_concept],
13165 : "a non-template variable cannot be %<concept%>");
13166 17 : else if (!at_namespace_scope_p ())
13167 2 : error_at (declspecs->locations[ds_concept],
13168 : "concept must be defined at namespace scope");
13169 : else
13170 15 : error_at (declspecs->locations[ds_concept],
13171 : "variable concepts are no longer supported");
13172 : return NULL_TREE;
13173 : }
13174 72071791 : else if (flag_concepts
13175 72071791 : && current_template_depth > template_class_depth (scope))
13176 : {
13177 62058225 : tree ci = current_template_constraints ();
13178 62058225 : set_constraints (decl, ci);
13179 : }
13180 :
13181 : // Handle explicit specializations and instantiations of variable templates.
13182 72071791 : if (orig_declarator)
13183 71327265 : decl = check_explicit_specialization (orig_declarator, decl,
13184 : template_count, 0);
13185 :
13186 72071791 : return decl != error_mark_node ? decl : NULL_TREE;
13187 : }
13188 :
13189 : /* Create and return a canonical pointer to member function type, for
13190 : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
13191 :
13192 : tree
13193 3162231 : build_ptrmemfunc_type (tree type)
13194 : {
13195 3162231 : tree field, fields;
13196 3162231 : tree t;
13197 :
13198 3162231 : if (type == error_mark_node)
13199 : return type;
13200 :
13201 : /* Make sure that we always have the unqualified pointer-to-member
13202 : type first. */
13203 3162231 : if (cp_cv_quals quals = cp_type_quals (type))
13204 : {
13205 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
13206 0 : return cp_build_qualified_type (unqual, quals);
13207 : }
13208 :
13209 : /* If a canonical type already exists for this type, use it. We use
13210 : this method instead of type_hash_canon, because it only does a
13211 : simple equality check on the list of field members. */
13212 :
13213 3162231 : t = TYPE_PTRMEMFUNC_TYPE (type);
13214 3162231 : if (t)
13215 : return t;
13216 :
13217 1059046 : t = make_node (RECORD_TYPE);
13218 :
13219 : /* Let the front end know this is a pointer to member function. */
13220 1059046 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
13221 :
13222 1059046 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
13223 1059046 : DECL_NONADDRESSABLE_P (field) = 1;
13224 1059046 : fields = field;
13225 :
13226 1059046 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
13227 : delta_type_node);
13228 1059046 : DECL_NONADDRESSABLE_P (field) = 1;
13229 1059046 : DECL_CHAIN (field) = fields;
13230 1059046 : fields = field;
13231 :
13232 1059046 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
13233 :
13234 : /* Zap out the name so that the back end will give us the debugging
13235 : information for this anonymous RECORD_TYPE. */
13236 1059046 : TYPE_NAME (t) = NULL_TREE;
13237 :
13238 : /* Cache this pointer-to-member type so that we can find it again
13239 : later. */
13240 1059046 : TYPE_PTRMEMFUNC_TYPE (type) = t;
13241 :
13242 1059046 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
13243 524276 : SET_TYPE_STRUCTURAL_EQUALITY (t);
13244 534770 : else if (TYPE_CANONICAL (type) != type)
13245 339699 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
13246 :
13247 : return t;
13248 : }
13249 :
13250 : /* Create and return a pointer to data member type. */
13251 :
13252 : tree
13253 337692 : build_ptrmem_type (tree class_type, tree member_type)
13254 : {
13255 337692 : if (TREE_CODE (member_type) == METHOD_TYPE)
13256 : {
13257 60935 : cp_cv_quals quals = type_memfn_quals (member_type);
13258 60935 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
13259 60935 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
13260 60935 : return build_ptrmemfunc_type (build_pointer_type (member_type));
13261 : }
13262 : else
13263 : {
13264 276757 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
13265 276757 : return build_offset_type (class_type, member_type);
13266 : }
13267 : }
13268 :
13269 : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
13270 : Check to see that the definition is valid. Issue appropriate error
13271 : messages. */
13272 :
13273 : static void
13274 19514223 : check_static_variable_definition (tree decl, tree type)
13275 : {
13276 : /* Avoid redundant diagnostics on out-of-class definitions. */
13277 19514223 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
13278 : ;
13279 : /* Can't check yet if we don't know the type. */
13280 18098139 : else if (dependent_type_p (type))
13281 : ;
13282 : /* If DECL is declared constexpr, we'll do the appropriate checks
13283 : in check_initializer. Similarly for inline static data members. */
13284 17545371 : else if (DECL_P (decl)
13285 17545371 : && (DECL_DECLARED_CONSTEXPR_P (decl)
13286 2802332 : || DECL_VAR_DECLARED_INLINE_P (decl)))
13287 : ;
13288 2801833 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13289 : {
13290 60 : if (!COMPLETE_TYPE_P (type))
13291 11 : error_at (DECL_SOURCE_LOCATION (decl),
13292 : "in-class initialization of static data member %q#D of "
13293 : "incomplete type", decl);
13294 49 : else if (literal_type_p (type))
13295 43 : permerror (DECL_SOURCE_LOCATION (decl),
13296 : "%<constexpr%> needed for in-class initialization of "
13297 : "static data member %q#D of non-integral type", decl);
13298 : else
13299 6 : error_at (DECL_SOURCE_LOCATION (decl),
13300 : "in-class initialization of static data member %q#D of "
13301 : "non-literal type", decl);
13302 : }
13303 : /* Motion 10 at San Diego: If a static const integral data member is
13304 : initialized with an integral constant expression, the initializer
13305 : may appear either in the declaration (within the class), or in
13306 : the definition, but not both. If it appears in the class, the
13307 : member is a member constant. The file-scope definition is always
13308 : required. */
13309 2801773 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
13310 11 : error_at (DECL_SOURCE_LOCATION (decl),
13311 : "invalid in-class initialization of static data member "
13312 : "of non-integral type %qT",
13313 : type);
13314 2801762 : else if (!CP_TYPE_CONST_P (type))
13315 5 : error_at (DECL_SOURCE_LOCATION (decl),
13316 : "ISO C++ forbids in-class initialization of non-const "
13317 : "static member %qD",
13318 : decl);
13319 2801757 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13320 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13321 : "ISO C++ forbids initialization of member constant "
13322 : "%qD of non-integral type %qT", decl, type);
13323 19514223 : }
13324 :
13325 : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
13326 : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
13327 : expressions out into temporary variables so that walk_tree doesn't
13328 : step into them (c++/15764). */
13329 :
13330 : static tree
13331 3380 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
13332 : {
13333 3380 : hash_set<tree> *pset = (hash_set<tree> *)data;
13334 3380 : tree expr = *expr_p;
13335 3380 : if (TREE_CODE (expr) == SAVE_EXPR)
13336 : {
13337 1471 : tree op = TREE_OPERAND (expr, 0);
13338 1471 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
13339 1471 : if (TREE_SIDE_EFFECTS (op))
13340 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
13341 1471 : *walk_subtrees = 0;
13342 : }
13343 1909 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
13344 1734 : *walk_subtrees = 0;
13345 3380 : return NULL;
13346 : }
13347 :
13348 : /* Entry point for the above. */
13349 :
13350 : static void
13351 1530 : stabilize_vla_size (tree size)
13352 : {
13353 1530 : hash_set<tree> pset;
13354 : /* Break out any function calls into temporary variables. */
13355 1530 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
13356 1530 : }
13357 :
13358 : /* Reduce a SIZEOF_EXPR to its value. */
13359 :
13360 : tree
13361 12640070 : fold_sizeof_expr (tree t)
13362 : {
13363 12640070 : tree r;
13364 12640070 : if (SIZEOF_EXPR_TYPE_P (t))
13365 12019077 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13366 12019077 : TREE_TYPE (TREE_OPERAND (t, 0)),
13367 : SIZEOF_EXPR, false, false);
13368 620993 : else if (TYPE_P (TREE_OPERAND (t, 0)))
13369 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13370 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
13371 : false, false);
13372 : else
13373 620993 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
13374 620993 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
13375 : false, false);
13376 12640070 : if (r == error_mark_node)
13377 94 : r = size_one_node;
13378 12640070 : r = cp_fold_convert (size_type_node, r);
13379 12640070 : return r;
13380 : }
13381 :
13382 : /* Given the SIZE (i.e., number of elements) in an array, compute
13383 : an appropriate index type for the array. If non-NULL, NAME is
13384 : the name of the entity being declared. */
13385 :
13386 : static tree
13387 3574022 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
13388 : tsubst_flags_t complain)
13389 : {
13390 3574022 : if (error_operand_p (size))
13391 32 : return error_mark_node;
13392 :
13393 : /* The type of the index being computed. */
13394 3573990 : tree itype;
13395 :
13396 : /* The original numeric size as seen in the source code before
13397 : conversion to size_t. */
13398 3573990 : tree origsize = size;
13399 :
13400 3573990 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
13401 :
13402 3573990 : if (!type_dependent_expression_p (size))
13403 : {
13404 3523582 : origsize = size = mark_rvalue_use (size);
13405 :
13406 38382 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
13407 3523742 : && TREE_SIDE_EFFECTS (size))
13408 : /* In C++98, we mark a non-constant array bound with a magic
13409 : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
13410 : else
13411 : {
13412 3523581 : size = build_converted_constant_expr (size_type_node, size, complain);
13413 : /* Pedantically a constant expression is required here and so
13414 : __builtin_is_constant_evaluated () should fold to true if it
13415 : is successfully folded into a constant. */
13416 3523581 : size = fold_non_dependent_expr (size, complain,
13417 : /*manifestly_const_eval=*/true);
13418 :
13419 3523581 : if (!TREE_CONSTANT (size))
13420 149061 : size = origsize;
13421 : }
13422 :
13423 3523582 : if (error_operand_p (size))
13424 3 : return error_mark_node;
13425 :
13426 : /* The array bound must be an integer type. */
13427 3523579 : tree type = TREE_TYPE (size);
13428 3523579 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
13429 : {
13430 21 : if (!(complain & tf_error))
13431 0 : return error_mark_node;
13432 21 : if (name)
13433 18 : error_at (loc, "size of array %qD has non-integral type %qT",
13434 : name, type);
13435 : else
13436 3 : error_at (loc, "size of array has non-integral type %qT", type);
13437 21 : size = integer_one_node;
13438 : }
13439 : }
13440 :
13441 : /* A type is dependent if it is...an array type constructed from any
13442 : dependent type or whose size is specified by a constant expression
13443 : that is value-dependent. */
13444 : /* We can only call value_dependent_expression_p on integral constant
13445 : expressions. */
13446 3573987 : if (processing_template_decl
13447 1639126 : && potential_constant_expression (size)
13448 5213088 : && value_dependent_expression_p (size))
13449 : {
13450 : /* Just build the index type and mark that it requires
13451 : structural equality checks. */
13452 918043 : in_template:
13453 918043 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13454 : size, size_one_node));
13455 918043 : TYPE_DEPENDENT_P (itype) = 1;
13456 918043 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13457 918043 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
13458 918043 : return itype;
13459 : }
13460 :
13461 2672867 : if (TREE_CODE (size) != INTEGER_CST)
13462 : {
13463 19029 : tree folded = cp_fully_fold (size);
13464 19029 : if (TREE_CODE (folded) == INTEGER_CST)
13465 : {
13466 212 : if (name)
13467 138 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13468 : "integral constant-expression", name);
13469 : else
13470 74 : pedwarn (loc, OPT_Wpedantic,
13471 : "size of array is not an integral constant-expression");
13472 : }
13473 19029 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13474 : /* We might have lost the TREE_CONSTANT flag e.g. when we are
13475 : folding a conversion from a pointer to integral type. In that
13476 : case issue an error below and don't treat this as a VLA. */;
13477 : else
13478 : /* Use the folded result for VLAs, too; it will have resolved
13479 : SIZEOF_EXPR. */
13480 : size = folded;
13481 : }
13482 :
13483 : /* Normally, the array-bound will be a constant. */
13484 2672867 : if (TREE_CODE (size) == INTEGER_CST)
13485 : {
13486 : /* The size to use in diagnostics that reflects the constant
13487 : size used in the source, rather than SIZE massaged above. */
13488 2654050 : tree diagsize = size;
13489 :
13490 : /* If the original size before conversion to size_t was signed
13491 : and negative, convert it to ssizetype to restore the sign. */
13492 2654050 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13493 : && TREE_CODE (size) == INTEGER_CST
13494 2654050 : && tree_int_cst_sign_bit (size))
13495 : {
13496 231 : diagsize = fold_convert (ssizetype, size);
13497 :
13498 : /* Clear the overflow bit that may have been set as a result
13499 : of the conversion from the sizetype of the new size to
13500 : ssizetype. */
13501 231 : TREE_OVERFLOW (diagsize) = false;
13502 : }
13503 :
13504 : /* Verify that the array has a positive number of elements
13505 : and issue the appropriate diagnostic if it doesn't. */
13506 2654050 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13507 : {
13508 462 : if (!(complain & tf_error))
13509 15 : return error_mark_node;
13510 447 : size = integer_one_node;
13511 : }
13512 : /* As an extension we allow zero-sized arrays. */
13513 2653588 : else if (integer_zerop (size))
13514 : {
13515 3217 : if (!(complain & tf_error))
13516 : /* We must fail if performing argument deduction (as
13517 : indicated by the state of complain), so that
13518 : another substitution can be found. */
13519 8 : return error_mark_node;
13520 3209 : else if (name)
13521 2914 : pedwarn (loc, OPT_Wpedantic,
13522 : "ISO C++ forbids zero-size array %qD", name);
13523 : else
13524 295 : pedwarn (loc, OPT_Wpedantic,
13525 : "ISO C++ forbids zero-size array");
13526 : }
13527 : }
13528 18817 : else if (TREE_CONSTANT (size)
13529 : /* We don't allow VLAs at non-function scopes, or during
13530 : tentative template substitution. */
13531 18802 : || !at_function_scope_p ()
13532 37276 : || !(complain & tf_error))
13533 : {
13534 364 : if (!(complain & tf_error))
13535 313 : return error_mark_node;
13536 : /* `(int) &fn' is not a valid array bound. */
13537 51 : if (name)
13538 37 : error_at (loc,
13539 : "size of array %qD is not an integral constant-expression",
13540 : name);
13541 : else
13542 14 : error_at (loc, "size of array is not an integral constant-expression");
13543 51 : size = integer_one_node;
13544 : }
13545 18453 : else if (pedantic && warn_vla != 0)
13546 : {
13547 30 : if (name)
13548 15 : pedwarn (name_loc, OPT_Wvla,
13549 : "ISO C++ forbids variable length array %qD", name);
13550 : else
13551 15 : pedwarn (input_location, OPT_Wvla,
13552 : "ISO C++ forbids variable length array");
13553 : }
13554 18423 : else if (warn_vla > 0)
13555 : {
13556 3 : if (name)
13557 3 : warning_at (name_loc, OPT_Wvla,
13558 : "variable length array %qD is used", name);
13559 : else
13560 0 : warning (OPT_Wvla,
13561 : "variable length array is used");
13562 : }
13563 :
13564 2672531 : if (processing_template_decl && !TREE_CONSTANT (size))
13565 16923 : goto in_template;
13566 : else
13567 : {
13568 2655608 : if (!TREE_CONSTANT (size))
13569 : {
13570 : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13571 : of the MINUS_EXPR, which allows the -1 to get folded with the +1
13572 : that happens when building TYPE_SIZE. */
13573 1530 : size = variable_size (size);
13574 1530 : stabilize_vla_size (size);
13575 : }
13576 :
13577 : /* Compute the index of the largest element in the array. It is
13578 : one less than the number of elements in the array. We save
13579 : and restore PROCESSING_TEMPLATE_DECL so that computations in
13580 : cp_build_binary_op will be appropriately folded. */
13581 2655608 : {
13582 2655608 : processing_template_decl_sentinel s;
13583 2655608 : itype = cp_build_binary_op (input_location,
13584 : MINUS_EXPR,
13585 : cp_convert (ssizetype, size, complain),
13586 : cp_convert (ssizetype, integer_one_node,
13587 : complain),
13588 : complain);
13589 2655608 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13590 2655608 : }
13591 :
13592 2655608 : if (!TREE_CONSTANT (itype))
13593 : {
13594 1530 : if (sanitize_flags_p (SANITIZE_VLA)
13595 1530 : && current_function_decl != NULL_TREE)
13596 : {
13597 : /* We have to add 1 -- in the ubsan routine we generate
13598 : LE_EXPR rather than LT_EXPR. */
13599 78 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13600 : build_one_cst (TREE_TYPE (itype)));
13601 78 : t = ubsan_instrument_vla (input_location, t);
13602 78 : finish_expr_stmt (t);
13603 : }
13604 : }
13605 : /* Make sure that there was no overflow when creating to a signed
13606 : index type. (For example, on a 32-bit machine, an array with
13607 : size 2^32 - 1 is too big.) */
13608 2654078 : else if (TREE_CODE (itype) == INTEGER_CST
13609 2654078 : && TREE_OVERFLOW (itype))
13610 : {
13611 0 : if (!(complain & tf_error))
13612 0 : return error_mark_node;
13613 0 : error ("overflow in array dimension");
13614 0 : TREE_OVERFLOW (itype) = 0;
13615 : }
13616 : }
13617 :
13618 : /* Create and return the appropriate index type. */
13619 2655608 : itype = build_index_type (itype);
13620 :
13621 : /* If the index type were dependent, we would have returned early, so
13622 : remember that it isn't. */
13623 2655608 : TYPE_DEPENDENT_P (itype) = 0;
13624 2655608 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13625 2655608 : return itype;
13626 : }
13627 :
13628 : tree
13629 625061 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13630 : {
13631 625061 : return compute_array_index_type_loc (input_location, name, size, complain);
13632 : }
13633 :
13634 : /* Returns the scope (if any) in which the entity declared by
13635 : DECLARATOR will be located. If the entity was declared with an
13636 : unqualified name, NULL_TREE is returned. */
13637 :
13638 : tree
13639 789603194 : get_scope_of_declarator (const cp_declarator *declarator)
13640 : {
13641 914897544 : while (declarator && declarator->kind != cdk_id)
13642 125294350 : declarator = declarator->declarator;
13643 :
13644 : /* If the declarator-id is a SCOPE_REF, the scope in which the
13645 : declaration occurs is the first operand. */
13646 789603194 : if (declarator
13647 788323154 : && declarator->u.id.qualifying_scope)
13648 : return declarator->u.id.qualifying_scope;
13649 :
13650 : /* Otherwise, the declarator is not a qualified name; the entity will
13651 : be declared in the current scope. */
13652 : return NULL_TREE;
13653 : }
13654 :
13655 : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
13656 : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13657 : with this type. */
13658 :
13659 : static tree
13660 3789175 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13661 : {
13662 3789175 : tree itype = NULL_TREE;
13663 :
13664 : /* If things have already gone awry, bail now. */
13665 3789175 : if (type == error_mark_node || size == error_mark_node)
13666 : return error_mark_node;
13667 :
13668 : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13669 : for a deduced class type. */
13670 3789112 : if (template_placeholder_p (type))
13671 : {
13672 6 : if (name)
13673 0 : error_at (loc, "%qD declared as array of template placeholder "
13674 : "type %qT", name, type);
13675 : else
13676 6 : error ("creating array of template placeholder type %qT", type);
13677 6 : return error_mark_node;
13678 : }
13679 :
13680 : /* If there are some types which cannot be array elements,
13681 : issue an error-message and return. */
13682 3789106 : switch (TREE_CODE (type))
13683 : {
13684 3 : case VOID_TYPE:
13685 3 : if (name)
13686 0 : error_at (loc, "declaration of %qD as array of void", name);
13687 : else
13688 3 : error ("creating array of void");
13689 3 : return error_mark_node;
13690 :
13691 6 : case FUNCTION_TYPE:
13692 6 : if (name)
13693 6 : error_at (loc, "declaration of %qD as array of functions", name);
13694 : else
13695 0 : error ("creating array of functions");
13696 6 : return error_mark_node;
13697 :
13698 21 : case REFERENCE_TYPE:
13699 21 : if (name)
13700 15 : error_at (loc, "declaration of %qD as array of references", name);
13701 : else
13702 6 : error ("creating array of references");
13703 21 : return error_mark_node;
13704 :
13705 0 : case METHOD_TYPE:
13706 0 : if (name)
13707 0 : error_at (loc, "declaration of %qD as array of function members",
13708 : name);
13709 : else
13710 0 : error ("creating array of function members");
13711 0 : return error_mark_node;
13712 :
13713 3789076 : default:
13714 3789076 : break;
13715 : }
13716 :
13717 3789076 : if (!verify_type_context (name ? loc : input_location,
13718 : TCTX_ARRAY_ELEMENT, type))
13719 0 : return error_mark_node;
13720 :
13721 : /* [dcl.array]
13722 :
13723 : The constant expressions that specify the bounds of the arrays
13724 : can be omitted only for the first member of the sequence. */
13725 3789076 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13726 : {
13727 30 : if (name)
13728 24 : error_at (loc, "declaration of %qD as multidimensional array must "
13729 : "have bounds for all dimensions except the first",
13730 : name);
13731 : else
13732 6 : error ("multidimensional array must have bounds for all "
13733 : "dimensions except the first");
13734 :
13735 30 : return error_mark_node;
13736 : }
13737 :
13738 : /* Figure out the index type for the array. */
13739 3789046 : if (size)
13740 : {
13741 2948961 : itype = compute_array_index_type_loc (loc, name, size,
13742 : tf_warning_or_error);
13743 2948961 : if (type_uses_auto (type)
13744 2948961 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13745 : {
13746 9 : sorry_at (loc, "variable-length array of %<auto%>");
13747 9 : return error_mark_node;
13748 : }
13749 : }
13750 :
13751 3789037 : return build_cplus_array_type (type, itype);
13752 : }
13753 :
13754 : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
13755 :
13756 : static location_t
13757 1095767730 : min_location (location_t loca, location_t locb)
13758 : {
13759 1095767730 : if (loca == UNKNOWN_LOCATION
13760 1095767730 : || (locb != UNKNOWN_LOCATION
13761 114611631 : && linemap_location_before_p (line_table, locb, loca)))
13762 982416748 : return locb;
13763 : return loca;
13764 : }
13765 :
13766 : /* Returns the smallest location != UNKNOWN_LOCATION among the
13767 : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13768 : and LOCATIONS[ds_restrict]. */
13769 :
13770 : static location_t
13771 1093942795 : smallest_type_quals_location (int type_quals, const location_t* locations)
13772 : {
13773 1093942795 : location_t loc = UNKNOWN_LOCATION;
13774 :
13775 1093942795 : if (type_quals & TYPE_QUAL_CONST)
13776 113047052 : loc = locations[ds_const];
13777 :
13778 1093942795 : if (type_quals & TYPE_QUAL_VOLATILE)
13779 1824844 : loc = min_location (loc, locations[ds_volatile]);
13780 :
13781 1093942795 : if (type_quals & TYPE_QUAL_RESTRICT)
13782 43 : loc = min_location (loc, locations[ds_restrict]);
13783 :
13784 1093942795 : return loc;
13785 : }
13786 :
13787 : /* Returns the smallest among the latter and locations[ds_type_spec]. */
13788 :
13789 : static location_t
13790 1093942774 : smallest_type_location (int type_quals, const location_t* locations)
13791 : {
13792 1093942774 : location_t loc = smallest_type_quals_location (type_quals, locations);
13793 1093942774 : return min_location (loc, locations[ds_type_spec]);
13794 : }
13795 :
13796 : static location_t
13797 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
13798 : {
13799 26 : int type_quals = get_type_quals (declspecs);
13800 26 : return smallest_type_location (type_quals, declspecs->locations);
13801 : }
13802 :
13803 : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
13804 : strip out the pointer/reference declarator(s). */
13805 :
13806 : static bool
13807 31070100 : maybe_strip_indirect_ref (const cp_declarator** declarator)
13808 : {
13809 31070100 : bool indirect_ref_p = false;
13810 62140347 : while (declarator && *declarator
13811 31070247 : && ((*declarator)->kind == cdk_pointer
13812 31070247 : || (*declarator)->kind == cdk_reference))
13813 : {
13814 147 : indirect_ref_p = true;
13815 147 : *declarator = (*declarator)->declarator;
13816 : }
13817 31070100 : return indirect_ref_p;
13818 : }
13819 :
13820 : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13821 : and DECLARATOR. SFK indicates the kind of special function (if any) that
13822 : this function is. OPTYPE is the type given in a conversion operator
13823 : declaration, or the class type for a constructor/destructor.
13824 : Returns the actual return type of the function; that may be different
13825 : than TYPE if an error occurs, or for certain special functions. */
13826 :
13827 : static tree
13828 31070100 : check_special_function_return_type (special_function_kind sfk,
13829 : tree type,
13830 : tree optype,
13831 : int type_quals,
13832 : const cp_declarator** declarator,
13833 : const location_t* locations)
13834 : {
13835 31070100 : gcc_assert (declarator);
13836 31070100 : location_t rettype_loc = (type
13837 31070100 : ? smallest_type_location (type_quals, locations)
13838 31070019 : : (*declarator)->id_loc);
13839 31070100 : switch (sfk)
13840 : {
13841 25333364 : case sfk_constructor:
13842 25333364 : if (maybe_strip_indirect_ref (declarator) || type)
13843 81 : error_at (rettype_loc,
13844 : "return type specification for constructor invalid");
13845 25333283 : else if (type_quals != TYPE_UNQUALIFIED)
13846 15 : error_at (smallest_type_quals_location (type_quals, locations),
13847 : "qualifiers are not allowed on constructor declaration");
13848 :
13849 25333364 : if (targetm.cxx.cdtor_returns_this ())
13850 0 : type = build_pointer_type (optype);
13851 : else
13852 25333364 : type = void_type_node;
13853 : break;
13854 :
13855 3392456 : case sfk_destructor:
13856 3392456 : if (maybe_strip_indirect_ref (declarator) || type)
13857 45 : error_at (rettype_loc,
13858 : "return type specification for destructor invalid");
13859 3392411 : else if (type_quals != TYPE_UNQUALIFIED)
13860 3 : error_at (smallest_type_quals_location (type_quals, locations),
13861 : "qualifiers are not allowed on destructor declaration");
13862 :
13863 : /* We can't use the proper return type here because we run into
13864 : problems with ambiguous bases and covariant returns. */
13865 3392456 : if (targetm.cxx.cdtor_returns_this ())
13866 0 : type = build_pointer_type (void_type_node);
13867 : else
13868 3392456 : type = void_type_node;
13869 : break;
13870 :
13871 1068939 : case sfk_conversion:
13872 1068939 : if (maybe_strip_indirect_ref (declarator) || type)
13873 42 : error_at (rettype_loc,
13874 : "return type specified for %<operator %T%>", optype);
13875 1068897 : else if (type_quals != TYPE_UNQUALIFIED)
13876 3 : error_at (smallest_type_quals_location (type_quals, locations),
13877 : "qualifiers are not allowed on declaration of "
13878 : "%<operator %T%>", optype);
13879 :
13880 : type = optype;
13881 : break;
13882 :
13883 1275341 : case sfk_deduction_guide:
13884 1275341 : if (maybe_strip_indirect_ref (declarator) || type)
13885 0 : error_at (rettype_loc,
13886 : "return type specified for deduction guide");
13887 1275341 : else if (type_quals != TYPE_UNQUALIFIED)
13888 0 : error_at (smallest_type_quals_location (type_quals, locations),
13889 : "qualifiers are not allowed on declaration of "
13890 : "deduction guide");
13891 1275341 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13892 : {
13893 3 : error ("template template parameter %qT in declaration of "
13894 : "deduction guide", optype);
13895 3 : type = error_mark_node;
13896 : }
13897 : else
13898 1275338 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13899 34434207 : for (int i = 0; i < ds_last; ++i)
13900 33158866 : if (i != ds_explicit && locations[i])
13901 3 : error_at (locations[i],
13902 : "%<decl-specifier%> in declaration of deduction guide");
13903 : break;
13904 :
13905 0 : default:
13906 0 : gcc_unreachable ();
13907 : }
13908 :
13909 31070100 : return type;
13910 : }
13911 :
13912 : /* A variable or data member (whose unqualified name is IDENTIFIER)
13913 : has been declared with the indicated TYPE. If the TYPE is not
13914 : acceptable, issue an error message and return a type to use for
13915 : error-recovery purposes. */
13916 :
13917 : tree
13918 343560107 : check_var_type (tree identifier, tree type, location_t loc)
13919 : {
13920 343560107 : if (VOID_TYPE_P (type))
13921 : {
13922 92 : if (!identifier)
13923 0 : error_at (loc, "unnamed variable or field declared void");
13924 92 : else if (identifier_p (identifier))
13925 : {
13926 92 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13927 92 : error_at (loc, "variable or field %qE declared void",
13928 : identifier);
13929 : }
13930 : else
13931 0 : error_at (loc, "variable or field declared void");
13932 92 : type = error_mark_node;
13933 : }
13934 :
13935 343560107 : return type;
13936 : }
13937 :
13938 : /* Handle declaring DECL as an inline variable. */
13939 :
13940 : static void
13941 7035219 : mark_inline_variable (tree decl, location_t loc)
13942 : {
13943 7035219 : bool inlinep = true;
13944 7035219 : if (! toplevel_bindings_p ())
13945 : {
13946 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13947 : "%qD declared at block scope", decl);
13948 24 : inlinep = false;
13949 : }
13950 7035195 : else if (cxx_dialect < cxx17)
13951 660 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13952 : "with %<-std=c++17%> or %<-std=gnu++17%>");
13953 684 : if (inlinep)
13954 : {
13955 7035195 : retrofit_lang_decl (decl);
13956 7035195 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13957 : }
13958 7035219 : }
13959 :
13960 :
13961 : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13962 : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13963 : ORIG is the anonymous TYPE_NAME before that change. */
13964 :
13965 : static bool
13966 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13967 : {
13968 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13969 42 : tree name = DECL_NAME (TYPE_NAME (type));
13970 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13971 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13972 : "anonymous non-C-compatible type given name for linkage "
13973 42 : "purposes by %<typedef%> declaration");
13974 42 : }
13975 :
13976 : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13977 : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13978 : to TYPE but during recursion can be set to nested classes. */
13979 :
13980 : static bool
13981 540397 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13982 : {
13983 540397 : if (!COMPLETE_TYPE_P (t))
13984 : return false;
13985 540394 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13986 : {
13987 6 : auto_diagnostic_group d;
13988 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13989 9 : inform (type == t ? DECL_SOURCE_LOCATION (orig)
13990 3 : : DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13991 : "type is not C-compatible because it has a base class");
13992 6 : return true;
13993 6 : }
13994 2533705 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13995 1993378 : switch (TREE_CODE (field))
13996 : {
13997 9 : case VAR_DECL:
13998 : /* static data members have been diagnosed already. */
13999 9 : continue;
14000 1325097 : case FIELD_DECL:
14001 1325097 : if (DECL_INITIAL (field))
14002 : {
14003 4 : auto_diagnostic_group d;
14004 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
14005 4 : inform (DECL_SOURCE_LOCATION (field),
14006 : "type is not C-compatible because %qD has default "
14007 : "member initializer", field);
14008 4 : return true;
14009 4 : }
14010 1325093 : continue;
14011 9 : case CONST_DECL:
14012 9 : continue;
14013 668182 : case TYPE_DECL:
14014 668182 : if (DECL_SELF_REFERENCE_P (field))
14015 540362 : continue;
14016 127820 : if (DECL_IMPLICIT_TYPEDEF_P (field))
14017 : {
14018 127810 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
14019 3 : continue;
14020 127807 : if (CLASS_TYPE_P (TREE_TYPE (field)))
14021 : {
14022 127807 : tree tf = TREE_TYPE (field);
14023 127807 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
14024 : tf))
14025 : return true;
14026 127782 : continue;
14027 127782 : }
14028 : }
14029 : /* FALLTHRU */
14030 32 : case FUNCTION_DECL:
14031 32 : case TEMPLATE_DECL:
14032 32 : {
14033 32 : auto_diagnostic_group d;
14034 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
14035 32 : inform (DECL_SOURCE_LOCATION (field),
14036 : "type is not C-compatible because it contains %qD "
14037 : "declaration", field);
14038 32 : return true;
14039 32 : }
14040 : default:
14041 : break;
14042 1325111 : }
14043 : return false;
14044 : }
14045 :
14046 : /* Assign a typedef-given name to a class or enumeration type declared
14047 : as anonymous at first. This was split out of grokdeclarator
14048 : because it is also used in libcc1. */
14049 :
14050 : void
14051 414387 : name_unnamed_type (tree type, tree decl)
14052 : {
14053 1243161 : gcc_assert (TYPE_UNNAMED_P (type)
14054 : || enum_with_enumerator_for_linkage_p (type));
14055 :
14056 414387 : tree orig = TYPE_NAME (type);
14057 414387 : if (flag_reflection)
14058 : {
14059 : /* For -freflection for typedef struct { ... } S; ^^S needs to be
14060 : a reflection of a type alias. So, TREE_TYPE (DECL) can't be
14061 : TYPE. Instead of what we do below, override DECL_NAME (orig). */
14062 16096 : DECL_NAME (orig) = DECL_NAME (decl);
14063 16096 : TYPE_DECL_FOR_LINKAGE_PURPOSES_P (orig) = 1;
14064 : }
14065 : else
14066 : /* Replace the anonymous decl with the real decl. Be careful not to
14067 : rename other typedefs (such as the self-reference) of type. */
14068 1193157 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14069 794866 : if (TYPE_NAME (t) == orig)
14070 398291 : TYPE_NAME (t) = decl;
14071 414387 : TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl) = 1;
14072 :
14073 : /* If this is a typedef within a template class, the nested
14074 : type is a (non-primary) template. The name for the
14075 : template needs updating as well. */
14076 414387 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
14077 68 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
14078 :
14079 : /* Adjust linkage now that we aren't unnamed anymore. */
14080 414387 : reset_type_linkage (type);
14081 :
14082 414387 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
14083 412590 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
14084 :
14085 : /* FIXME remangle member functions; member functions of a
14086 : type with external linkage have external linkage. */
14087 :
14088 : /* Check that our job is done, and that it would fail if we
14089 : attempted to do it again. */
14090 828774 : gcc_assert (!TYPE_UNNAMED_P (type)
14091 : && !enum_with_enumerator_for_linkage_p (type));
14092 414387 : }
14093 :
14094 : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
14095 : is allowed. TYPE might contain a decltype(auto). Returns true if
14096 : there was a problem, false otherwise. */
14097 :
14098 : static bool
14099 665920296 : check_decltype_auto (location_t loc, tree type)
14100 : {
14101 665920296 : if (tree a = type_uses_auto (type))
14102 : {
14103 18680113 : if (AUTO_IS_DECLTYPE (a))
14104 : {
14105 640668 : if (a != type)
14106 : {
14107 33 : error_at (loc, "%qT as type rather than plain "
14108 : "%<decltype(auto)%>", type);
14109 33 : return true;
14110 : }
14111 640635 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
14112 : {
14113 37 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
14114 37 : return true;
14115 : }
14116 : }
14117 : }
14118 : return false;
14119 : }
14120 :
14121 : /* Issue an error about two mutually incompatible declspecs
14122 : with the given names and locations
14123 : e.g. "error: `signed' and `unsigned' specified together" */
14124 :
14125 : static void
14126 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
14127 : const char *name_b, location_t loc_b)
14128 : {
14129 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
14130 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
14131 : nullptr, highlight_colors::rhs);
14132 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
14133 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
14134 66 : error_at (&richloc, "%e and %e specified together",
14135 : &e_name_a, &e_name_b);
14136 66 : }
14137 :
14138 : /* Given declspecs and a declarator (abstract or otherwise), determine
14139 : the name and type of the object declared and construct a DECL node
14140 : for it.
14141 :
14142 : DECLSPECS points to the representation of declaration-specifier
14143 : sequence that precedes declarator.
14144 :
14145 : DECL_CONTEXT says which syntactic context this declaration is in:
14146 : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
14147 : FUNCDEF for a function definition. Like NORMAL but a few different
14148 : error messages in each case. Return value may be zero meaning
14149 : this definition is too screwy to try to parse.
14150 : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
14151 : handle member functions (which have FIELD context).
14152 : Return value may be zero meaning this definition is too screwy to
14153 : try to parse.
14154 : PARM for a parameter declaration (either within a function prototype
14155 : or before a function body). Make a PARM_DECL, or return void_type_node.
14156 : TPARM for a template parameter declaration.
14157 : CATCHPARM for a parameter declaration before a catch clause.
14158 : TYPENAME if for a typename (in a cast or sizeof).
14159 : Don't make a DECL node; just return the ..._TYPE node.
14160 : FIELD for a struct or union field; make a FIELD_DECL.
14161 : BITFIELD for a field with specified width.
14162 :
14163 : INITIALIZED is as for start_decl.
14164 :
14165 : ATTRLIST is a pointer to the list of attributes, which may be NULL
14166 : if there are none; *ATTRLIST may be modified if attributes from inside
14167 : the declarator should be applied to the declaration.
14168 :
14169 : When this function is called, scoping variables (such as
14170 : CURRENT_CLASS_TYPE) should reflect the scope in which the
14171 : declaration occurs, not the scope in which the new declaration will
14172 : be placed. For example, on:
14173 :
14174 : void S::f() { ... }
14175 :
14176 : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
14177 : should not be `S'.
14178 :
14179 : Returns a DECL (if a declarator is present), a TYPE (if there is no
14180 : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
14181 : error occurs. */
14182 :
14183 : tree
14184 1093942667 : grokdeclarator (const cp_declarator *declarator,
14185 : cp_decl_specifier_seq *declspecs,
14186 : enum decl_context decl_context,
14187 : int initialized,
14188 : tree* attrlist)
14189 : {
14190 1093942667 : tree type = NULL_TREE;
14191 1093942667 : int longlong = 0;
14192 1093942667 : int explicit_intN = 0;
14193 1093942667 : int int_n_alt = 0;
14194 1093942667 : int virtualp, explicitp, friendp, inlinep, staticp;
14195 1093942667 : int explicit_int = 0;
14196 1093942667 : int explicit_char = 0;
14197 1093942667 : int defaulted_int = 0;
14198 :
14199 1093942667 : tree typedef_decl = NULL_TREE;
14200 1093942667 : const char *name = NULL;
14201 1093942667 : tree typedef_type = NULL_TREE;
14202 : /* True if this declarator is a function definition. */
14203 1093942667 : bool funcdef_flag = false;
14204 1093942667 : cp_declarator_kind innermost_code = cdk_error;
14205 1093942667 : int bitfield = 0;
14206 : #if 0
14207 : /* See the code below that used this. */
14208 : tree decl_attr = NULL_TREE;
14209 : #endif
14210 :
14211 : /* Keep track of what sort of function is being processed
14212 : so that we can warn about default return values, or explicit
14213 : return values which do not match prescribed defaults. */
14214 1093942667 : special_function_kind sfk = sfk_none;
14215 :
14216 1093942667 : tree dname = NULL_TREE;
14217 1093942667 : tree ctor_return_type = NULL_TREE;
14218 1093942667 : enum overload_flags flags = NO_SPECIAL;
14219 : /* cv-qualifiers that apply to the declarator, for a declaration of
14220 : a member function. */
14221 1093942667 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
14222 : /* virt-specifiers that apply to the declarator, for a declaration of
14223 : a member function. */
14224 1093942667 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
14225 : /* ref-qualifier that applies to the declarator, for a declaration of
14226 : a member function. */
14227 1093942667 : cp_ref_qualifier rqual = REF_QUAL_NONE;
14228 : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
14229 1093942667 : int type_quals = get_type_quals (declspecs);
14230 1093942667 : tree raises = NULL_TREE;
14231 1093942667 : int template_count = 0;
14232 1093942667 : tree returned_attrs = NULL_TREE;
14233 1093942667 : tree contract_specifiers = NULL_TREE;
14234 1093942667 : tree parms = NULL_TREE;
14235 1093942667 : const cp_declarator *id_declarator;
14236 : /* The unqualified name of the declarator; either an
14237 : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
14238 1093942667 : tree unqualified_id;
14239 : /* The class type, if any, in which this entity is located,
14240 : or NULL_TREE if none. Note that this value may be different from
14241 : the current class type; for example if an attempt is made to declare
14242 : "A::f" inside "B", this value will be "A". */
14243 1093942667 : tree ctype = current_class_type;
14244 : /* The NAMESPACE_DECL for the namespace in which this entity is
14245 : located. If an unqualified name is used to declare the entity,
14246 : this value will be NULL_TREE, even if the entity is located at
14247 : namespace scope. */
14248 1093942667 : tree in_namespace = NULL_TREE;
14249 1093942667 : cp_storage_class storage_class;
14250 1093942667 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
14251 1093942667 : bool type_was_error_mark_node = false;
14252 1093942667 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
14253 1093942667 : bool template_type_arg = false;
14254 1093942667 : bool template_parm_flag = false;
14255 1093942667 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
14256 1093942667 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
14257 1093942667 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
14258 1093942667 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
14259 1093942667 : bool late_return_type_p = false;
14260 1093942667 : bool array_parameter_p = false;
14261 1093942667 : tree reqs = NULL_TREE;
14262 :
14263 1093942667 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
14264 1093942667 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
14265 1093942667 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
14266 1093942667 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
14267 1093942667 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
14268 1093942667 : explicit_intN = declspecs->explicit_intN_p;
14269 1093942667 : int_n_alt = declspecs->int_n_alt;
14270 1093942667 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
14271 :
14272 : // Was concept_p specified? Note that ds_concept
14273 : // implies ds_constexpr!
14274 1093942667 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
14275 1093942667 : if (concept_p)
14276 117 : constexpr_p = true;
14277 :
14278 1093942667 : if (decl_context == FUNCDEF)
14279 : funcdef_flag = true, decl_context = NORMAL;
14280 : else if (decl_context == MEMFUNCDEF)
14281 : funcdef_flag = true, decl_context = FIELD;
14282 : else if (decl_context == BITFIELD)
14283 : bitfield = 1, decl_context = FIELD;
14284 : else if (decl_context == TEMPLATE_TYPE_ARG)
14285 : template_type_arg = true, decl_context = TYPENAME;
14286 : else if (decl_context == TPARM)
14287 11688745 : template_parm_flag = true, decl_context = PARM;
14288 :
14289 1093942667 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
14290 11502149 : funcdef_flag = true;
14291 :
14292 1093942667 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
14293 : (type_quals,
14294 1093942667 : declspecs->locations));
14295 1093942667 : location_t id_loc;
14296 1093942667 : location_t init_loc;
14297 1093942667 : if (declarator)
14298 : {
14299 659950133 : id_loc = loc_or_input_loc (declarator->id_loc);
14300 659950133 : init_loc = loc_or_input_loc (declarator->init_loc);
14301 : }
14302 : else
14303 433992534 : init_loc = id_loc = input_location;
14304 :
14305 : /* Look inside a declarator for the name being declared
14306 : and get it as a string, for an error message. */
14307 1093942667 : for (id_declarator = declarator;
14308 1502055229 : id_declarator;
14309 408112562 : id_declarator = id_declarator->declarator)
14310 : {
14311 1023977087 : if (id_declarator->kind != cdk_id)
14312 408112587 : innermost_code = id_declarator->kind;
14313 :
14314 1023977087 : switch (id_declarator->kind)
14315 : {
14316 200282672 : case cdk_function:
14317 200282672 : if (id_declarator->declarator
14318 199888607 : && id_declarator->declarator->kind == cdk_id)
14319 : {
14320 197958161 : sfk = id_declarator->declarator->u.id.sfk;
14321 197958161 : if (sfk == sfk_destructor)
14322 3392459 : flags = DTOR_FLAG;
14323 : }
14324 : break;
14325 :
14326 615864500 : case cdk_id:
14327 615864500 : {
14328 615864500 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
14329 615864500 : tree decl = id_declarator->u.id.unqualified_name;
14330 615864500 : if (!decl)
14331 : break;
14332 615864500 : if (qualifying_scope)
14333 : {
14334 10192953 : if (check_for_bare_parameter_packs (qualifying_scope,
14335 10192953 : id_declarator->id_loc))
14336 6 : return error_mark_node;
14337 10192947 : if (at_function_scope_p ())
14338 : {
14339 : /* [dcl.meaning]
14340 :
14341 : A declarator-id shall not be qualified except
14342 : for ...
14343 :
14344 : None of the cases are permitted in block
14345 : scope. */
14346 0 : if (qualifying_scope == global_namespace)
14347 0 : error ("invalid use of qualified-name %<::%D%>",
14348 : decl);
14349 0 : else if (TYPE_P (qualifying_scope))
14350 0 : error ("invalid use of qualified-name %<%T::%D%>",
14351 : qualifying_scope, decl);
14352 : else
14353 0 : error ("invalid use of qualified-name %<%D::%D%>",
14354 : qualifying_scope, decl);
14355 0 : return error_mark_node;
14356 : }
14357 10192947 : else if (TYPE_P (qualifying_scope))
14358 : {
14359 9910402 : ctype = qualifying_scope;
14360 9910402 : if (!MAYBE_CLASS_TYPE_P (ctype))
14361 : {
14362 6 : error_at (id_declarator->id_loc,
14363 : "%q#T is not a class or namespace", ctype);
14364 6 : ctype = NULL_TREE;
14365 : }
14366 9910396 : else if (innermost_code != cdk_function
14367 523985 : && current_class_type
14368 9910411 : && !uniquely_derived_from_p (ctype,
14369 : current_class_type))
14370 : {
14371 9 : error_at (id_declarator->id_loc,
14372 : "invalid use of qualified-name %<%T::%D%>",
14373 : qualifying_scope, decl);
14374 9 : return error_mark_node;
14375 : }
14376 : }
14377 282545 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
14378 615864485 : in_namespace = qualifying_scope;
14379 : }
14380 615864485 : switch (TREE_CODE (decl))
14381 : {
14382 3392474 : case BIT_NOT_EXPR:
14383 3392474 : {
14384 3392474 : if (innermost_code != cdk_function)
14385 : {
14386 6 : error_at (EXPR_LOCATION (decl),
14387 : "declaration of %qE as non-function", decl);
14388 6 : return error_mark_node;
14389 : }
14390 3392468 : else if (!qualifying_scope
14391 3392468 : && !(current_class_type && at_class_scope_p ()))
14392 : {
14393 9 : error_at (EXPR_LOCATION (decl),
14394 : "declaration of %qE as non-member", decl);
14395 9 : return error_mark_node;
14396 : }
14397 :
14398 3392459 : tree type = TREE_OPERAND (decl, 0);
14399 3392459 : if (TYPE_P (type))
14400 3392456 : type = constructor_name (type);
14401 3392459 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
14402 3392459 : dname = decl;
14403 : }
14404 3392459 : break;
14405 :
14406 3011399 : case TEMPLATE_ID_EXPR:
14407 3011399 : {
14408 3011399 : tree fns = TREE_OPERAND (decl, 0);
14409 :
14410 3011399 : dname = fns;
14411 4014973 : if (!identifier_p (dname))
14412 3011372 : dname = OVL_NAME (dname);
14413 : }
14414 : /* Fall through. */
14415 :
14416 612472011 : case IDENTIFIER_NODE:
14417 612472011 : if (identifier_p (decl))
14418 609460612 : dname = decl;
14419 :
14420 612472011 : if (IDENTIFIER_KEYWORD_P (dname))
14421 : {
14422 0 : error ("declarator-id missing; using reserved word %qD",
14423 : dname);
14424 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14425 : }
14426 612472011 : else if (!IDENTIFIER_CONV_OP_P (dname))
14427 611403069 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14428 : else
14429 : {
14430 1068942 : gcc_assert (flags == NO_SPECIAL);
14431 1068942 : flags = TYPENAME_FLAG;
14432 1068942 : sfk = sfk_conversion;
14433 1068942 : tree glob = get_global_binding (dname);
14434 1068942 : if (glob && TREE_CODE (glob) == TYPE_DECL)
14435 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14436 : else
14437 : name = "<invalid operator>";
14438 : }
14439 : break;
14440 :
14441 0 : default:
14442 0 : gcc_unreachable ();
14443 : }
14444 : break;
14445 : }
14446 :
14447 : case cdk_array:
14448 : case cdk_pointer:
14449 : case cdk_reference:
14450 : case cdk_ptrmem:
14451 : break;
14452 :
14453 : case cdk_decomp:
14454 1023977032 : name = "structured binding";
14455 : break;
14456 :
14457 25 : case cdk_error:
14458 25 : return error_mark_node;
14459 :
14460 0 : default:
14461 0 : gcc_unreachable ();
14462 : }
14463 1023977032 : if (id_declarator->kind == cdk_id)
14464 : break;
14465 : }
14466 :
14467 : /* [dcl.fct.edf]
14468 :
14469 : The declarator in a function-definition shall have the form
14470 : D1 ( parameter-declaration-clause) ... */
14471 1093942612 : if (funcdef_flag && innermost_code != cdk_function)
14472 : {
14473 3 : error_at (id_loc, "function definition does not declare parameters");
14474 3 : return error_mark_node;
14475 : }
14476 :
14477 1093942609 : if (flags == TYPENAME_FLAG
14478 1093942609 : && innermost_code != cdk_function
14479 3 : && ! (ctype && !declspecs->any_specifiers_p))
14480 : {
14481 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
14482 3 : return error_mark_node;
14483 : }
14484 :
14485 1093942606 : if (dname && identifier_p (dname))
14486 : {
14487 612472005 : if (UDLIT_OPER_P (dname)
14488 612472005 : && innermost_code != cdk_function)
14489 : {
14490 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
14491 6 : return error_mark_node;
14492 : }
14493 :
14494 612471999 : if (IDENTIFIER_ANY_OP_P (dname))
14495 : {
14496 39019198 : if (typedef_p)
14497 : {
14498 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14499 3 : return error_mark_node;
14500 : }
14501 39019195 : else if (decl_context == PARM || decl_context == CATCHPARM)
14502 : {
14503 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
14504 15 : return error_mark_node;
14505 : }
14506 : }
14507 : }
14508 :
14509 : /* Anything declared one level down from the top level
14510 : must be one of the parameters of a function
14511 : (because the body is at least two levels down). */
14512 :
14513 : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14514 : by not allowing C++ class definitions to specify their parameters
14515 : with xdecls (must be spec.d in the parmlist).
14516 :
14517 : Since we now wait to push a class scope until we are sure that
14518 : we are in a legitimate method context, we must set oldcname
14519 : explicitly (since current_class_name is not yet alive).
14520 :
14521 : We also want to avoid calling this a PARM if it is in a namespace. */
14522 :
14523 1093942582 : if (decl_context == NORMAL && !toplevel_bindings_p ())
14524 : {
14525 75813010 : cp_binding_level *b = current_binding_level;
14526 75813010 : current_binding_level = b->level_chain;
14527 75813010 : if (current_binding_level != 0 && toplevel_bindings_p ())
14528 : decl_context = PARM;
14529 75813010 : current_binding_level = b;
14530 : }
14531 :
14532 1093942582 : if (name == NULL)
14533 477829822 : name = decl_context == PARM ? "parameter" : "type name";
14534 :
14535 1093942582 : if (consteval_p && constexpr_p)
14536 : {
14537 6 : error_at (declspecs->locations[ds_consteval],
14538 : "both %qs and %qs specified", "constexpr", "consteval");
14539 6 : return error_mark_node;
14540 : }
14541 :
14542 1093942576 : if (concept_p && typedef_p)
14543 : {
14544 9 : error_at (declspecs->locations[ds_concept],
14545 : "%qs cannot appear in a typedef declaration", "concept");
14546 9 : return error_mark_node;
14547 : }
14548 :
14549 1093942567 : if (constexpr_p && typedef_p)
14550 : {
14551 3 : error_at (declspecs->locations[ds_constexpr],
14552 : "%qs cannot appear in a typedef declaration", "constexpr");
14553 3 : return error_mark_node;
14554 : }
14555 :
14556 1093942564 : if (consteval_p && typedef_p)
14557 : {
14558 3 : error_at (declspecs->locations[ds_consteval],
14559 : "%qs cannot appear in a typedef declaration", "consteval");
14560 3 : return error_mark_node;
14561 : }
14562 :
14563 1093942561 : if (constinit_p && typedef_p)
14564 : {
14565 3 : error_at (declspecs->locations[ds_constinit],
14566 : "%qs cannot appear in a typedef declaration", "constinit");
14567 3 : return error_mark_node;
14568 : }
14569 :
14570 : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14571 : keywords shall appear in a decl-specifier-seq." */
14572 1093942558 : if (constinit_p && constexpr_p)
14573 : {
14574 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14575 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14576 6 : error_at (&richloc,
14577 : "can use at most one of the %<constinit%> and %<constexpr%> "
14578 : "specifiers");
14579 6 : return error_mark_node;
14580 6 : }
14581 :
14582 : /* If there were multiple types specified in the decl-specifier-seq,
14583 : issue an error message. */
14584 1093942552 : if (declspecs->multiple_types_p)
14585 : {
14586 1501 : error_at (typespec_loc,
14587 : "two or more data types in declaration of %qs", name);
14588 1501 : return error_mark_node;
14589 : }
14590 :
14591 1093941051 : if (declspecs->conflicting_specifiers_p)
14592 31 : return error_mark_node;
14593 :
14594 : /* Extract the basic type from the decl-specifier-seq. */
14595 1093941020 : type = declspecs->type;
14596 1093941020 : if (type == error_mark_node)
14597 : {
14598 1243 : type = NULL_TREE;
14599 1243 : type_was_error_mark_node = true;
14600 : }
14601 :
14602 : /* Ignore erroneous attributes. */
14603 1093941020 : if (attrlist && *attrlist == error_mark_node)
14604 12 : *attrlist = NULL_TREE;
14605 :
14606 : /* An object declared as __attribute__((unavailable)) suppresses
14607 : any reports of being declared with unavailable or deprecated
14608 : items. An object declared as __attribute__((deprecated))
14609 : suppresses warnings of uses of other deprecated items. */
14610 1093941020 : auto ds = make_temp_override (deprecated_state);
14611 1093941020 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
14612 171 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14613 1093940849 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
14614 979874 : deprecated_state = DEPRECATED_SUPPRESS;
14615 :
14616 1093941020 : cp_handle_deprecated_or_unavailable (type);
14617 1093941020 : if (type && TREE_CODE (type) == TYPE_DECL)
14618 : {
14619 761870721 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14620 761870721 : typedef_decl = type;
14621 761870721 : type = TREE_TYPE (typedef_decl);
14622 761870721 : if (DECL_ARTIFICIAL (typedef_decl))
14623 553419922 : cp_handle_deprecated_or_unavailable (type);
14624 : }
14625 : /* No type at all: default to `int', and set DEFAULTED_INT
14626 : because it was not a user-defined typedef. */
14627 1093941020 : if (type == NULL_TREE)
14628 : {
14629 40684215 : if (signed_p || unsigned_p || long_p || short_p)
14630 : {
14631 : /* These imply 'int'. */
14632 9612472 : type = integer_type_node;
14633 9612472 : defaulted_int = 1;
14634 : }
14635 : /* If we just have "complex", it is equivalent to "complex double". */
14636 31071743 : else if (!longlong && !explicit_intN
14637 31071743 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14638 : {
14639 81 : type = double_type_node;
14640 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14641 : "ISO C++ does not support plain %<complex%> meaning "
14642 : "%<double complex%>");
14643 : }
14644 : }
14645 : /* Gather flags. */
14646 1093941020 : explicit_int = declspecs->explicit_int_p;
14647 1093941020 : explicit_char = declspecs->explicit_char_p;
14648 :
14649 : #if 0
14650 : /* See the code below that used this. */
14651 : if (typedef_decl)
14652 : decl_attr = DECL_ATTRIBUTES (typedef_decl);
14653 : #endif
14654 1093941020 : typedef_type = type;
14655 :
14656 1093941020 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14657 2344280 : ctor_return_type = TREE_TYPE (dname);
14658 : else
14659 : ctor_return_type = ctype;
14660 :
14661 1093941020 : if (sfk != sfk_none)
14662 : {
14663 31070100 : type = check_special_function_return_type (sfk, type,
14664 : ctor_return_type,
14665 : type_quals,
14666 : &declarator,
14667 : declspecs->locations);
14668 31070100 : type_quals = TYPE_UNQUALIFIED;
14669 : }
14670 1062870920 : else if (type == NULL_TREE)
14671 : {
14672 1643 : int is_main;
14673 :
14674 1643 : explicit_int = -1;
14675 :
14676 : /* We handle `main' specially here, because 'main () { }' is so
14677 : common. With no options, it is allowed. With -Wreturn-type,
14678 : it is a warning. It is only an error with -pedantic-errors. */
14679 3286 : is_main = (funcdef_flag
14680 3520 : && dname && identifier_p (dname)
14681 234 : && MAIN_NAME_P (dname)
14682 68 : && ctype == NULL_TREE
14683 68 : && in_namespace == NULL_TREE
14684 1711 : && current_namespace == global_namespace);
14685 :
14686 1643 : if (type_was_error_mark_node)
14687 : /* We've already issued an error, don't complain more. */;
14688 400 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
14689 : /* Allow it, sigh. */;
14690 338 : else if (! is_main)
14691 276 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14692 : name);
14693 62 : else if (pedantic)
14694 4 : pedwarn (id_loc, OPT_Wpedantic,
14695 : "ISO C++ forbids declaration of %qs with no type", name);
14696 : else
14697 58 : warning_at (id_loc, OPT_Wreturn_type,
14698 : "ISO C++ forbids declaration of %qs with no type", name);
14699 :
14700 1643 : if (type_was_error_mark_node && template_parm_flag)
14701 : /* FIXME we should be able to propagate the error_mark_node as is
14702 : for other contexts too. */
14703 162 : type = error_mark_node;
14704 : else
14705 1481 : type = integer_type_node;
14706 : }
14707 :
14708 1093941020 : ctype = NULL_TREE;
14709 :
14710 1093941020 : if (explicit_intN)
14711 : {
14712 1158124 : if (! int_n_enabled_p[declspecs->int_n_idx])
14713 : {
14714 0 : error_at (declspecs->locations[ds_type_spec],
14715 : "%<__int%d%> is not supported by this target",
14716 0 : int_n_data[declspecs->int_n_idx].bitsize);
14717 0 : explicit_intN = false;
14718 : }
14719 : /* Don't pedwarn if the alternate "__intN__" form has been used instead
14720 : of "__intN". */
14721 1158124 : else if (!int_n_alt && pedantic)
14722 10016 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14723 : "ISO C++ does not support %<__int%d%> for %qs",
14724 10016 : int_n_data[declspecs->int_n_idx].bitsize, name);
14725 : }
14726 :
14727 : /* Now process the modifiers that were specified
14728 : and check for invalid combinations. */
14729 :
14730 : /* Long double is a special combination. */
14731 1093941020 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14732 : {
14733 9253437 : long_p = false;
14734 9253437 : type = cp_build_qualified_type (long_double_type_node,
14735 : cp_type_quals (type));
14736 : }
14737 :
14738 : /* Check all other uses of type modifiers. */
14739 :
14740 1093941020 : if (unsigned_p || signed_p || long_p || short_p)
14741 : {
14742 22156979 : location_t loc;
14743 22156979 : const char *key;
14744 22156979 : if (unsigned_p)
14745 : {
14746 15073408 : key = "unsigned";
14747 15073408 : loc = declspecs->locations[ds_unsigned];
14748 : }
14749 7083571 : else if (signed_p)
14750 : {
14751 1079374 : key = "signed";
14752 1079374 : loc = declspecs->locations[ds_signed];
14753 : }
14754 6004197 : else if (longlong)
14755 : {
14756 2341017 : key = "long long";
14757 2341017 : loc = declspecs->locations[ds_long_long];
14758 : }
14759 3663180 : else if (long_p)
14760 : {
14761 3078759 : key = "long";
14762 3078759 : loc = declspecs->locations[ds_long];
14763 : }
14764 : else /* if (short_p) */
14765 : {
14766 584421 : key = "short";
14767 584421 : loc = declspecs->locations[ds_short];
14768 : }
14769 :
14770 22156979 : int ok = 0;
14771 :
14772 22156979 : if (signed_p && unsigned_p)
14773 54 : complain_about_incompatible_declspecs
14774 54 : ("signed", declspecs->locations[ds_signed],
14775 : "unsigned", declspecs->locations[ds_unsigned]);
14776 22156925 : else if (long_p && short_p)
14777 12 : complain_about_incompatible_declspecs
14778 12 : ("long", declspecs->locations[ds_long],
14779 : "short", declspecs->locations[ds_short]);
14780 22156913 : else if (TREE_CODE (type) != INTEGER_TYPE
14781 22156900 : || type == char8_type_node
14782 22156876 : || type == char16_type_node
14783 22156852 : || type == char32_type_node
14784 22156828 : || ((long_p || short_p)
14785 11646437 : && (explicit_char || explicit_intN)))
14786 418 : error_at (loc, "%qs specified with %qT", key, type);
14787 22156495 : else if (!explicit_int && !defaulted_int
14788 22156495 : && !explicit_char && !explicit_intN)
14789 : {
14790 30 : if (typedef_decl)
14791 : {
14792 27 : pedwarn (loc, OPT_Wpedantic,
14793 : "%qs specified with typedef-name %qD",
14794 : key, typedef_decl);
14795 27 : ok = !flag_pedantic_errors;
14796 : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14797 : and is not built as a typedef. */
14798 27 : if (is_typedef_decl (typedef_decl))
14799 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
14800 : }
14801 3 : else if (declspecs->decltype_p)
14802 0 : error_at (loc, "%qs specified with %<decltype%>", key);
14803 : else
14804 3 : error_at (loc, "%qs specified with %<typeof%>", key);
14805 : }
14806 : else
14807 : ok = 1;
14808 :
14809 : /* Discard the type modifiers if they are invalid. */
14810 514 : if (! ok)
14811 : {
14812 : unsigned_p = false;
14813 : signed_p = false;
14814 : long_p = false;
14815 : short_p = false;
14816 : longlong = 0;
14817 : }
14818 : }
14819 :
14820 : /* Decide whether an integer type is signed or not.
14821 : Optionally treat bitfields as signed by default. */
14822 1093940530 : if (unsigned_p
14823 : /* [class.bit]
14824 :
14825 : It is implementation-defined whether a plain (neither
14826 : explicitly signed or unsigned) char, short, int, or long
14827 : bit-field is signed or unsigned.
14828 :
14829 : Naturally, we extend this to long long as well. Note that
14830 : this does not include wchar_t. */
14831 1093941020 : || (bitfield && !flag_signed_bitfields
14832 18 : && !signed_p
14833 : /* A typedef for plain `int' without `signed' can be
14834 : controlled just like plain `int', but a typedef for
14835 : `signed int' cannot be so controlled. */
14836 18 : && !(typedef_decl
14837 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14838 13 : && TREE_CODE (type) == INTEGER_TYPE
14839 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14840 : {
14841 15073193 : if (explicit_intN)
14842 541938 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14843 14531255 : else if (longlong)
14844 1492025 : type = long_long_unsigned_type_node;
14845 13039230 : else if (long_p)
14846 2450755 : type = long_unsigned_type_node;
14847 10588475 : else if (short_p)
14848 1482026 : type = short_unsigned_type_node;
14849 9106449 : else if (type == char_type_node)
14850 1984726 : type = unsigned_char_type_node;
14851 7121723 : else if (typedef_decl)
14852 25 : type = c_common_unsigned_type (type);
14853 : else
14854 7121698 : type = unsigned_type_node;
14855 : }
14856 1078867827 : else if (signed_p && type == char_type_node)
14857 711861 : type = signed_char_type_node;
14858 1078155966 : else if (explicit_intN)
14859 616186 : type = int_n_trees[declspecs->int_n_idx].signed_type;
14860 1077539780 : else if (longlong)
14861 2406677 : type = long_long_integer_type_node;
14862 1075133103 : else if (long_p)
14863 3148907 : type = long_integer_type_node;
14864 1071984196 : else if (short_p)
14865 665714 : type = short_integer_type_node;
14866 1071318482 : else if (signed_p && typedef_decl)
14867 9 : type = c_common_signed_type (type);
14868 :
14869 1093941020 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14870 : {
14871 1283835 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14872 3 : error_at (declspecs->locations[ds_complex],
14873 : "complex invalid for %qs", name);
14874 : /* If a modifier is specified, the resulting complex is the complex
14875 : form of TYPE. E.g, "complex short" is "complex short int". */
14876 1283832 : else if (type == integer_type_node)
14877 171 : type = complex_integer_type_node;
14878 1283661 : else if (type == float_type_node)
14879 350356 : type = complex_float_type_node;
14880 933305 : else if (type == double_type_node)
14881 350907 : type = complex_double_type_node;
14882 582398 : else if (type == long_double_type_node)
14883 350397 : type = complex_long_double_type_node;
14884 : else
14885 232001 : type = build_complex_type (type);
14886 : }
14887 :
14888 : /* If we're using the injected-class-name to form a compound type or a
14889 : declaration, replace it with the underlying class so we don't get
14890 : redundant typedefs in the debug output. But if we are returning the
14891 : type unchanged, leave it alone so that it's available to
14892 : maybe_get_template_decl_from_type_decl. */
14893 214051132 : if (CLASS_TYPE_P (type)
14894 213985724 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14895 36855949 : && type == TREE_TYPE (TYPE_NAME (type))
14896 1130796969 : && (declarator || type_quals))
14897 35202230 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14898 :
14899 1093941020 : type_quals |= cp_type_quals (type);
14900 2187882040 : type = cp_build_qualified_type
14901 2187882040 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14902 885490221 : || declspecs->decltype_p)
14903 : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14904 : /* We might have ignored or rejected some of the qualifiers. */
14905 1093941020 : type_quals = cp_type_quals (type);
14906 :
14907 1077985749 : if (cxx_dialect >= cxx17 && type && is_auto (type)
14908 23968467 : && innermost_code != cdk_function
14909 : /* Placeholder in parm gets a better error below. */
14910 14895175 : && !(decl_context == PARM || decl_context == CATCHPARM)
14911 1108797299 : && id_declarator && declarator != id_declarator)
14912 1224685 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14913 : {
14914 16 : auto_diagnostic_group g;
14915 16 : gcc_rich_location richloc (typespec_loc);
14916 16 : richloc.add_fixit_insert_after ("<>");
14917 16 : error_at (&richloc, "missing template argument list after %qE; "
14918 : "for deduction, template placeholder must be followed "
14919 : "by a simple declarator-id", tmpl);
14920 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14921 16 : type = error_mark_node;
14922 16 : }
14923 :
14924 1093941020 : staticp = 0;
14925 1093941020 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14926 1093941020 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14927 1093941020 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14928 :
14929 1093941020 : storage_class = declspecs->storage_class;
14930 1093941020 : if (storage_class == sc_static)
14931 25180145 : staticp = 1 + (decl_context == FIELD);
14932 1068760875 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14933 : /* Treat class-scope deduction guides as static member functions
14934 : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14935 24825140 : staticp = 2;
14936 :
14937 1093941020 : if (virtualp)
14938 : {
14939 4303533 : if (staticp == 2)
14940 : {
14941 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14942 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
14943 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14944 : "and %<static%>", dname);
14945 21 : storage_class = sc_none;
14946 21 : staticp = 0;
14947 21 : }
14948 4303533 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14949 : {
14950 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14951 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14952 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14953 : "declared both %<virtual%> and %<constexpr%> only in "
14954 : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14955 6 : }
14956 : }
14957 1093941020 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14958 :
14959 : /* Issue errors about use of storage classes for parameters. */
14960 1093941020 : if (decl_context == PARM)
14961 : {
14962 309314501 : if (typedef_p)
14963 : {
14964 24 : error_at (declspecs->locations[ds_typedef],
14965 : "typedef declaration invalid in parameter declaration");
14966 24 : return error_mark_node;
14967 : }
14968 309314477 : else if (template_parm_flag && storage_class != sc_none)
14969 : {
14970 16 : error_at (min_location (declspecs->locations[ds_thread],
14971 : declspecs->locations[ds_storage_class]),
14972 : "storage class specified for template parameter %qs",
14973 : name);
14974 16 : return error_mark_node;
14975 : }
14976 309314461 : else if (storage_class == sc_static
14977 309314461 : || storage_class == sc_extern
14978 309314458 : || thread_p)
14979 : {
14980 9 : error_at (min_location (declspecs->locations[ds_thread],
14981 : declspecs->locations[ds_storage_class]),
14982 : "storage class specified for parameter %qs", name);
14983 9 : return error_mark_node;
14984 : }
14985 :
14986 : /* Function parameters cannot be concept. */
14987 309314452 : if (concept_p)
14988 : {
14989 9 : error_at (declspecs->locations[ds_concept],
14990 : "a parameter cannot be declared %qs", "concept");
14991 9 : concept_p = 0;
14992 9 : constexpr_p = 0;
14993 : }
14994 : /* Function parameters cannot be constexpr. If we saw one, moan
14995 : and pretend it wasn't there. */
14996 309314443 : else if (constexpr_p)
14997 : {
14998 9 : error_at (declspecs->locations[ds_constexpr],
14999 : "a parameter cannot be declared %qs", "constexpr");
15000 9 : constexpr_p = 0;
15001 : }
15002 309314452 : if (constinit_p)
15003 : {
15004 6 : error_at (declspecs->locations[ds_constinit],
15005 : "a parameter cannot be declared %qs", "constinit");
15006 6 : constinit_p = 0;
15007 : }
15008 309314452 : if (consteval_p)
15009 : {
15010 3 : error_at (declspecs->locations[ds_consteval],
15011 : "a parameter cannot be declared %qs", "consteval");
15012 3 : consteval_p = 0;
15013 : }
15014 : }
15015 :
15016 : /* Give error if `virtual' is used outside of class declaration. */
15017 1093940971 : if (virtualp
15018 4303533 : && (current_class_name == NULL_TREE || decl_context != FIELD))
15019 : {
15020 68 : error_at (declspecs->locations[ds_virtual],
15021 : "%<virtual%> outside class declaration");
15022 68 : virtualp = 0;
15023 : }
15024 :
15025 1093940971 : if (innermost_code == cdk_decomp)
15026 : {
15027 237986 : location_t loc = (declarator->kind == cdk_reference
15028 248320 : ? declarator->declarator->id_loc : declarator->id_loc);
15029 248320 : if (inlinep)
15030 29 : error_at (declspecs->locations[ds_inline],
15031 : "structured binding declaration cannot be %qs", "inline");
15032 248320 : if (typedef_p)
15033 29 : error_at (declspecs->locations[ds_typedef],
15034 : "structured binding declaration cannot be %qs", "typedef");
15035 248320 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
15036 101 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
15037 : "structured binding declaration can be %qs only with "
15038 : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
15039 248320 : if (consteval_p)
15040 13 : error_at (declspecs->locations[ds_consteval], "structured "
15041 : "binding declaration cannot be %qs", "consteval");
15042 248320 : if (thread_p && cxx_dialect < cxx20)
15043 75 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
15044 : "structured binding declaration can be %qs only in "
15045 : "%<-std=c++20%> or %<-std=gnu++20%>",
15046 75 : declspecs->gnu_thread_keyword_p
15047 : ? "__thread" : "thread_local");
15048 248320 : if (concept_p)
15049 : {
15050 0 : error_at (declspecs->locations[ds_concept],
15051 : "structured binding declaration cannot be %qs", "concept");
15052 0 : constexpr_p = 0;
15053 : }
15054 : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
15055 248320 : if (type_quals & TYPE_QUAL_VOLATILE)
15056 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
15057 : "%<volatile%>-qualified structured binding is deprecated");
15058 248320 : switch (storage_class)
15059 : {
15060 : case sc_none:
15061 : break;
15062 28 : case sc_register:
15063 28 : error_at (loc, "structured binding declaration cannot be %qs",
15064 : "register");
15065 28 : break;
15066 254 : case sc_static:
15067 254 : if (cxx_dialect < cxx20)
15068 185 : pedwarn (loc, OPT_Wc__20_extensions,
15069 : "structured binding declaration can be %qs only in "
15070 : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
15071 : break;
15072 29 : case sc_extern:
15073 29 : error_at (loc, "structured binding declaration cannot be %qs",
15074 : "extern");
15075 29 : break;
15076 28 : case sc_mutable:
15077 28 : error_at (loc, "structured binding declaration cannot be %qs",
15078 : "mutable");
15079 28 : break;
15080 0 : case sc_auto:
15081 0 : error_at (loc, "structured binding declaration cannot be "
15082 : "C++98 %<auto%>");
15083 0 : break;
15084 0 : default:
15085 0 : gcc_unreachable ();
15086 : }
15087 248320 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
15088 248320 : || TYPE_IDENTIFIER (type) != auto_identifier)
15089 : {
15090 3 : if (type != error_mark_node)
15091 : {
15092 3 : auto_diagnostic_group d;
15093 3 : error_at (loc, "structured binding declaration cannot have "
15094 : "type %qT", type);
15095 3 : inform (loc,
15096 : "type must be cv-qualified %<auto%> or reference to "
15097 : "cv-qualified %<auto%>");
15098 3 : }
15099 3 : type = build_qualified_type (make_auto (), type_quals);
15100 3 : declspecs->type = type;
15101 : }
15102 248317 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
15103 51 : pedwarn (loc, OPT_Wpedantic,
15104 : "structured binding declaration cannot have constrained "
15105 : "%<auto%> type %qT", type);
15106 248320 : inlinep = 0;
15107 248320 : typedef_p = 0;
15108 248320 : consteval_p = 0;
15109 248320 : concept_p = 0;
15110 248320 : if (storage_class != sc_static)
15111 : {
15112 248066 : storage_class = sc_none;
15113 248066 : declspecs->storage_class = sc_none;
15114 : }
15115 : }
15116 :
15117 : /* Static anonymous unions are dealt with here. */
15118 1093940971 : if (staticp && decl_context == TYPENAME
15119 0 : && declspecs->type
15120 1093940971 : && ANON_AGGR_TYPE_P (declspecs->type))
15121 : decl_context = FIELD;
15122 :
15123 : /* Warn about storage classes that are invalid for certain
15124 : kinds of declarations (parameters, typenames, etc.). */
15125 1093940971 : if (thread_p
15126 20666 : && ((storage_class
15127 20666 : && storage_class != sc_extern
15128 528 : && storage_class != sc_static)
15129 20655 : || typedef_p))
15130 : {
15131 17 : location_t loc
15132 17 : = min_location (declspecs->locations[ds_thread],
15133 : declspecs->locations[ds_storage_class]);
15134 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
15135 17 : thread_p = false;
15136 : }
15137 1093940971 : if (decl_context != NORMAL
15138 916948104 : && ((storage_class != sc_none
15139 916948104 : && storage_class != sc_mutable)
15140 892122978 : || thread_p))
15141 : {
15142 24825129 : if ((decl_context == PARM || decl_context == CATCHPARM)
15143 36 : && (storage_class == sc_register
15144 36 : || storage_class == sc_auto))
15145 : ;
15146 24825093 : else if (typedef_p)
15147 : ;
15148 24825093 : else if (decl_context == FIELD
15149 : /* C++ allows static class elements. */
15150 24825093 : && storage_class == sc_static)
15151 : /* C++ also allows inlines and signed and unsigned elements,
15152 : but in those cases we don't come in here. */
15153 : ;
15154 : else
15155 : {
15156 15 : location_t loc
15157 15 : = min_location (declspecs->locations[ds_thread],
15158 : declspecs->locations[ds_storage_class]);
15159 15 : if (decl_context == FIELD)
15160 15 : error_at (loc, "storage class specified for %qs", name);
15161 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
15162 0 : error_at (loc, "storage class specified for parameter %qs", name);
15163 : else
15164 0 : error_at (loc, "storage class specified for typename");
15165 15 : if (storage_class == sc_register
15166 15 : || storage_class == sc_auto
15167 15 : || storage_class == sc_extern
15168 3 : || thread_p)
15169 15 : storage_class = sc_none;
15170 : }
15171 : }
15172 1069115842 : else if (storage_class == sc_extern && funcdef_flag
15173 1069115842 : && ! toplevel_bindings_p ())
15174 0 : error ("nested function %qs declared %<extern%>", name);
15175 1069115842 : else if (toplevel_bindings_p ())
15176 : {
15177 498584511 : if (storage_class == sc_auto)
15178 2 : error_at (declspecs->locations[ds_storage_class],
15179 : "top-level declaration of %qs specifies %<auto%>", name);
15180 : }
15181 570531331 : else if (thread_p
15182 570531331 : && storage_class != sc_extern
15183 243 : && storage_class != sc_static)
15184 : {
15185 210 : if (declspecs->gnu_thread_keyword_p)
15186 49 : pedwarn (declspecs->locations[ds_thread],
15187 49 : 0, "function-scope %qs implicitly auto and "
15188 : "declared %<__thread%>", name);
15189 :
15190 : /* When thread_local is applied to a variable of block scope the
15191 : storage-class-specifier static is implied if it does not appear
15192 : explicitly. */
15193 210 : storage_class = declspecs->storage_class = sc_static;
15194 210 : staticp = 1;
15195 : }
15196 :
15197 1093940971 : if (storage_class && friendp)
15198 : {
15199 12 : error_at (min_location (declspecs->locations[ds_thread],
15200 : declspecs->locations[ds_storage_class]),
15201 : "storage class specifiers invalid in friend function "
15202 : "declarations");
15203 12 : storage_class = sc_none;
15204 12 : staticp = 0;
15205 : }
15206 :
15207 1093940971 : if (!id_declarator)
15208 : unqualified_id = NULL_TREE;
15209 : else
15210 : {
15211 615862909 : unqualified_id = id_declarator->u.id.unqualified_name;
15212 615862909 : switch (TREE_CODE (unqualified_id))
15213 : {
15214 3392459 : case BIT_NOT_EXPR:
15215 3392459 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
15216 3392459 : if (TYPE_P (unqualified_id))
15217 3392456 : unqualified_id = constructor_name (unqualified_id);
15218 : break;
15219 :
15220 : case IDENTIFIER_NODE:
15221 : case TEMPLATE_ID_EXPR:
15222 : break;
15223 :
15224 0 : default:
15225 0 : gcc_unreachable ();
15226 : }
15227 : }
15228 :
15229 1093940971 : if (declspecs->std_attributes)
15230 : {
15231 795 : location_t attr_loc = declspecs->locations[ds_std_attribute];
15232 795 : auto_diagnostic_group d;
15233 795 : if (any_nonignored_attribute_p (declspecs->std_attributes)
15234 795 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
15235 792 : inform (attr_loc, "an attribute that appertains to a type-specifier "
15236 : "is ignored");
15237 795 : }
15238 :
15239 : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
15240 1093940971 : bool is_xobj_member_function = false;
15241 : /* Determine the type of the entity declared by recurring on the
15242 : declarator. */
15243 1501803199 : for (; declarator; declarator = declarator->declarator)
15244 : {
15245 1023973483 : const cp_declarator *inner_declarator;
15246 1023973483 : tree attrs;
15247 :
15248 1023973483 : if (type == error_mark_node)
15249 607 : return error_mark_node;
15250 :
15251 1023973155 : attrs = declarator->attributes;
15252 1023973155 : if (attrs)
15253 : {
15254 13796 : int attr_flags;
15255 :
15256 13796 : attr_flags = 0;
15257 13796 : if (declarator->kind == cdk_id)
15258 13088 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
15259 13796 : if (declarator->kind == cdk_function)
15260 691 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
15261 13796 : if (declarator->kind == cdk_array)
15262 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
15263 13796 : tree late_attrs = NULL_TREE;
15264 13796 : if (decl_context != PARM && decl_context != TYPENAME)
15265 : /* Assume that any attributes that get applied late to
15266 : templates will DTRT when applied to the declaration
15267 : as a whole. */
15268 13784 : late_attrs = splice_template_attributes (&attrs, type);
15269 13796 : returned_attrs = decl_attributes (&type,
15270 : attr_chainon (returned_attrs,
15271 : attrs),
15272 : attr_flags);
15273 13796 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
15274 : }
15275 :
15276 1023973155 : inner_declarator = declarator->declarator;
15277 :
15278 : /* We don't want to warn in parameter context because we don't
15279 : yet know if the parse will succeed, and this might turn out
15280 : to be a constructor call. */
15281 1023973155 : if (decl_context != PARM
15282 1023973155 : && decl_context != TYPENAME
15283 594750344 : && !typedef_p
15284 543902153 : && declarator->parenthesized != UNKNOWN_LOCATION
15285 : /* If the type is class-like and the inner name used a
15286 : global namespace qualifier, we need the parens.
15287 : Unfortunately all we can tell is whether a qualified name
15288 : was used or not. */
15289 1023973336 : && !(inner_declarator
15290 126 : && inner_declarator->kind == cdk_id
15291 108 : && inner_declarator->u.id.qualifying_scope
15292 18 : && (MAYBE_CLASS_TYPE_P (type)
15293 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
15294 : {
15295 169 : auto_diagnostic_group d;
15296 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
15297 : "unnecessary parentheses in declaration of %qs",
15298 : name))
15299 : {
15300 42 : gcc_rich_location iloc (declarator->parenthesized);
15301 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
15302 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
15303 42 : inform (&iloc, "remove parentheses");
15304 42 : }
15305 169 : }
15306 1023973155 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
15307 : break;
15308 :
15309 407862507 : switch (declarator->kind)
15310 : {
15311 3789175 : case cdk_array:
15312 7578350 : type = create_array_type_for_decl (dname, type,
15313 3789175 : declarator->u.array.bounds,
15314 3789175 : declarator->id_loc);
15315 3789175 : if (!valid_array_size_p (dname
15316 : ? declarator->id_loc : input_location,
15317 : type, dname))
15318 267 : type = error_mark_node;
15319 :
15320 3789175 : if (declarator->std_attributes)
15321 : /* [dcl.array]/1:
15322 :
15323 : The optional attribute-specifier-seq appertains to the
15324 : array type. */
15325 209 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15326 : break;
15327 :
15328 200282636 : case cdk_function:
15329 200282636 : {
15330 200282636 : tree arg_types;
15331 200282636 : int funcdecl_p;
15332 :
15333 : /* Declaring a function type. */
15334 :
15335 : /* Pick up type qualifiers which should be applied to `this'. */
15336 200282636 : memfn_quals = declarator->u.function.qualifiers;
15337 : /* Pick up virt-specifiers. */
15338 200282636 : virt_specifiers = declarator->u.function.virt_specifiers;
15339 : /* And ref-qualifier, too */
15340 200282636 : rqual = declarator->u.function.ref_qualifier;
15341 : /* And tx-qualifier. */
15342 200282636 : tree tx_qual = declarator->u.function.tx_qualifier;
15343 : /* Pick up the exception specifications. */
15344 200282636 : raises = declarator->u.function.exception_specification;
15345 : /* If the exception-specification is ill-formed, let's pretend
15346 : there wasn't one. */
15347 200282636 : if (raises == error_mark_node)
15348 9 : raises = NULL_TREE;
15349 :
15350 400565272 : auto find_xobj_parm = [](tree parm_list)
15351 : {
15352 : /* There is no need to iterate over the list,
15353 : only the first parm can be a valid xobj parm. */
15354 400325777 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
15355 : return NULL_TREE;
15356 : /* If we make it here, we are looking at an xobj parm.
15357 :
15358 : Non-null 'purpose' usually means the parm has a default
15359 : argument, we don't want to violate this assumption. */
15360 94928 : TREE_PURPOSE (parm_list) = NULL_TREE;
15361 94928 : return TREE_VALUE (parm_list);
15362 : };
15363 :
15364 200282636 : tree xobj_parm
15365 200282636 : = find_xobj_parm (declarator->u.function.parameters);
15366 200282636 : is_xobj_member_function = xobj_parm;
15367 :
15368 200282636 : if (xobj_parm && cxx_dialect < cxx23)
15369 42554 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
15370 : "explicit object member function only available "
15371 : "with %<-std=c++23%> or %<-std=gnu++23%>");
15372 :
15373 200282636 : if (xobj_parm && decl_context == TYPENAME)
15374 : {
15375 : /* We inform in every case, just differently depending on what
15376 : case it is. */
15377 9 : auto_diagnostic_group d;
15378 9 : bool ptr_type = true;
15379 : /* If declarator->kind is cdk_function and we are at the end of
15380 : the declarator chain, we are looking at a function type. */
15381 9 : if (!declarator->declarator)
15382 : {
15383 3 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15384 : "a function type cannot "
15385 : "have an explicit object parameter");
15386 3 : ptr_type = false;
15387 : }
15388 6 : else if (declarator->declarator->kind == cdk_pointer)
15389 3 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15390 : "a pointer to function type cannot "
15391 : "have an explicit object parameter");
15392 3 : else if (declarator->declarator->kind == cdk_ptrmem)
15393 3 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15394 : "a pointer to member function type "
15395 : "cannot have an explicit object parameter");
15396 : else
15397 0 : gcc_unreachable ();
15398 :
15399 : /* The locations being used here are probably not correct. */
15400 6 : if (ptr_type)
15401 6 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15402 : "the type of a pointer to explicit object member "
15403 : "function is a regular pointer to function type");
15404 : else
15405 3 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15406 : "the type of an explicit object "
15407 : "member function is a regular function type");
15408 : /* Ideally we should synthesize the correct syntax
15409 : for the user, perhaps this could be added later. */
15410 9 : }
15411 : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
15412 : the first parm node will never erroneously be detected here. */
15413 200282636 : {
15414 200282636 : auto_diagnostic_group d;
15415 200282636 : bool bad_xobj_parm_encountered = false;
15416 200282636 : for (tree parm = declarator->u.function.parameters;
15417 496904299 : parm && parm != void_list_node;
15418 296621663 : parm = TREE_CHAIN (parm))
15419 : {
15420 296621663 : if (TREE_PURPOSE (parm) != this_identifier)
15421 296621654 : continue;
15422 9 : bad_xobj_parm_encountered = true;
15423 9 : TREE_PURPOSE (parm) = NULL_TREE;
15424 9 : gcc_rich_location bad_xobj_parm
15425 9 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
15426 9 : error_at (&bad_xobj_parm,
15427 : "only the first parameter of a member function "
15428 : "can be declared as an explicit object parameter");
15429 9 : }
15430 200282636 : if (bad_xobj_parm_encountered && xobj_parm)
15431 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15432 : "valid explicit object parameter declared here");
15433 200282636 : }
15434 :
15435 200282636 : if (reqs)
15436 3 : error_at (location_of (reqs), "requires-clause on return type");
15437 200282636 : reqs = declarator->u.function.requires_clause;
15438 :
15439 : /* Say it's a definition only for the CALL_EXPR
15440 : closest to the identifier. */
15441 200282636 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15442 :
15443 : /* Handle a late-specified return type. */
15444 200282636 : tree late_return_type = declarator->u.function.late_return_type;
15445 200282636 : if (tree auto_node = type_uses_auto (type))
15446 : {
15447 9085228 : if (!late_return_type)
15448 : {
15449 5307741 : if (!funcdecl_p)
15450 : /* auto (*fp)() = f; is OK. */;
15451 5307707 : else if (current_class_type
15452 9762633 : && LAMBDA_TYPE_P (current_class_type))
15453 : /* OK for C++11 lambdas. */;
15454 3415743 : else if (cxx_dialect < cxx14)
15455 : {
15456 7 : auto_diagnostic_group d;
15457 7 : error_at (typespec_loc, "%qs function uses "
15458 : "%<auto%> type specifier without "
15459 : "trailing return type", name);
15460 7 : inform (typespec_loc,
15461 : "deduced return type only available "
15462 : "with %<-std=c++14%> or %<-std=gnu++14%>");
15463 7 : }
15464 3415736 : else if (virtualp)
15465 : {
15466 6 : error_at (typespec_loc, "virtual function "
15467 : "cannot have deduced return type");
15468 6 : virtualp = false;
15469 : }
15470 : }
15471 3777487 : else if (!is_auto (type) && sfk != sfk_conversion)
15472 : {
15473 15 : error_at (typespec_loc, "%qs function with trailing "
15474 : "return type has %qT as its type rather "
15475 : "than plain %<auto%>", name, type);
15476 276 : return error_mark_node;
15477 : }
15478 3777472 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15479 : {
15480 9 : if (funcdecl_p)
15481 3 : error_at (typespec_loc,
15482 : "%qs function with trailing return type "
15483 : "has %<decltype(auto)%> as its type "
15484 : "rather than plain %<auto%>", name);
15485 : else
15486 6 : error_at (typespec_loc,
15487 : "invalid use of %<decltype(auto)%>");
15488 9 : return error_mark_node;
15489 : }
15490 3777463 : else if (is_constrained_auto (type))
15491 : {
15492 6 : if (funcdecl_p)
15493 3 : error_at (typespec_loc,
15494 : "%qs function with trailing return type "
15495 : "has constrained %<auto%> type specifier "
15496 : "rather than plain %<auto%>",
15497 : name);
15498 : else
15499 3 : error_at (typespec_loc,
15500 : "invalid use of constrained %<auto%> type");
15501 6 : return error_mark_node;
15502 : }
15503 9085198 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15504 9085198 : if (!tmpl)
15505 7809853 : if (tree late_auto = type_uses_auto (late_return_type))
15506 40339 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15507 7809853 : if (tmpl)
15508 : {
15509 1275351 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
15510 : {
15511 13 : auto_diagnostic_group g;
15512 13 : error_at (typespec_loc, "deduced class "
15513 : "type %qD in function return type",
15514 13 : DECL_NAME (tmpl));
15515 13 : inform (DECL_SOURCE_LOCATION (tmpl),
15516 : "%qD declared here", tmpl);
15517 13 : return error_mark_node;
15518 13 : }
15519 1275338 : else if (!late_return_type)
15520 : {
15521 3 : auto_diagnostic_group d;
15522 3 : error_at (declarator->id_loc, "deduction guide "
15523 : "for %qT must have trailing return "
15524 3 : "type", TREE_TYPE (tmpl));
15525 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15526 : "%qD declared here", tmpl);
15527 3 : return error_mark_node;
15528 3 : }
15529 1275335 : else if (CLASS_TYPE_P (late_return_type)
15530 1275335 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15531 2550670 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15532 : == tmpl))
15533 : /* OK */;
15534 : else
15535 0 : error ("trailing return type %qT of deduction guide "
15536 : "is not a specialization of %qT",
15537 0 : late_return_type, TREE_TYPE (tmpl));
15538 : }
15539 : }
15540 191197408 : else if (late_return_type
15541 191197408 : && sfk != sfk_conversion)
15542 : {
15543 37 : if (late_return_type == error_mark_node)
15544 : return error_mark_node;
15545 37 : if (cxx_dialect < cxx11)
15546 : /* Not using maybe_warn_cpp0x because this should
15547 : always be an error. */
15548 1 : error_at (typespec_loc,
15549 : "trailing return type only available "
15550 : "with %<-std=c++11%> or %<-std=gnu++11%>");
15551 : else
15552 36 : error_at (typespec_loc, "%qs function with trailing "
15553 : "return type not declared with %<auto%> "
15554 : "type specifier", name);
15555 37 : return error_mark_node;
15556 : }
15557 200282553 : if (late_return_type && sfk == sfk_conversion)
15558 : {
15559 14 : error ("a conversion function cannot have a trailing return type");
15560 14 : return error_mark_node;
15561 : }
15562 200282539 : type = splice_late_return_type (type, late_return_type);
15563 200282539 : if (type == error_mark_node)
15564 : return error_mark_node;
15565 :
15566 200282461 : if (late_return_type)
15567 : {
15568 3777365 : late_return_type_p = true;
15569 3777365 : type_quals = cp_type_quals (type);
15570 : }
15571 :
15572 200282461 : if (type_quals != TYPE_UNQUALIFIED)
15573 : {
15574 : /* It's wrong, for instance, to issue a -Wignored-qualifiers
15575 : warning for
15576 : static_assert(!is_same_v<void(*)(), const void(*)()>);
15577 : because there the qualifier matters. */
15578 12445 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15579 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15580 : "qualifiers ignored on function return type");
15581 : /* [dcl.fct] "A volatile-qualified return type is
15582 : deprecated." */
15583 12445 : if (type_quals & TYPE_QUAL_VOLATILE)
15584 643 : warning_at (typespec_loc, OPT_Wvolatile,
15585 : "%<volatile%>-qualified return type is "
15586 : "deprecated");
15587 :
15588 : /* We now know that the TYPE_QUALS don't apply to the
15589 : decl, but to its return type. */
15590 : type_quals = TYPE_UNQUALIFIED;
15591 : }
15592 :
15593 : /* Error about some types functions can't return. */
15594 :
15595 200282461 : if (TREE_CODE (type) == FUNCTION_TYPE)
15596 : {
15597 30 : error_at (typespec_loc, "%qs declared as function returning "
15598 : "a function", name);
15599 30 : return error_mark_node;
15600 : }
15601 200282431 : if (TREE_CODE (type) == ARRAY_TYPE)
15602 : {
15603 6 : error_at (typespec_loc, "%qs declared as function returning "
15604 : "an array", name);
15605 6 : return error_mark_node;
15606 : }
15607 200282425 : if (constinit_p && funcdecl_p)
15608 : {
15609 15 : error_at (declspecs->locations[ds_constinit],
15610 : "%<constinit%> on function return type is not "
15611 : "allowed");
15612 15 : return error_mark_node;
15613 : }
15614 :
15615 200282410 : if (check_decltype_auto (typespec_loc, type))
15616 50 : return error_mark_node;
15617 :
15618 200282360 : if (ctype == NULL_TREE
15619 200282360 : && decl_context == FIELD
15620 : && funcdecl_p
15621 115709822 : && friendp == 0)
15622 108495619 : ctype = current_class_type;
15623 :
15624 200282360 : if (ctype && (sfk == sfk_constructor
15625 108495619 : || sfk == sfk_destructor))
15626 : {
15627 : /* We are within a class's scope. If our declarator name
15628 : is the same as the class name, and we are defining
15629 : a function, then it is a constructor/destructor, and
15630 : therefore returns a void type. */
15631 :
15632 : /* ISO C++ 12.4/2. A destructor may not be declared
15633 : const or volatile. A destructor may not be static.
15634 : A destructor may not be declared with ref-qualifier.
15635 :
15636 : ISO C++ 12.1. A constructor may not be declared
15637 : const or volatile. A constructor may not be
15638 : virtual. A constructor may not be static.
15639 : A constructor may not be declared with ref-qualifier. */
15640 27730509 : if (staticp == 2)
15641 9 : error_at (declspecs->locations[ds_storage_class],
15642 : (flags == DTOR_FLAG)
15643 : ? G_("destructor cannot be static member "
15644 : "function")
15645 : : G_("constructor cannot be static member "
15646 : "function"));
15647 27730509 : if (memfn_quals)
15648 : {
15649 9 : error ((flags == DTOR_FLAG)
15650 : ? G_("destructors may not be cv-qualified")
15651 : : G_("constructors may not be cv-qualified"));
15652 9 : memfn_quals = TYPE_UNQUALIFIED;
15653 : }
15654 :
15655 27730509 : if (rqual)
15656 : {
15657 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
15658 6 : error ((flags == DTOR_FLAG)
15659 : ? G_("destructors may not be ref-qualified")
15660 : : G_("constructors may not be ref-qualified"));
15661 6 : rqual = REF_QUAL_NONE;
15662 : }
15663 :
15664 27730509 : if (decl_context == FIELD
15665 27730509 : && !member_function_or_else (ctype,
15666 : current_class_type,
15667 : flags))
15668 0 : return error_mark_node;
15669 :
15670 27730509 : if (flags != DTOR_FLAG)
15671 : {
15672 : /* It's a constructor. */
15673 24511461 : if (explicitp == 1)
15674 5738320 : explicitp = 2;
15675 24511461 : if (virtualp)
15676 : {
15677 3 : permerror (declspecs->locations[ds_virtual],
15678 : "constructors cannot be declared %<virtual%>");
15679 3 : virtualp = 0;
15680 : }
15681 24511461 : if (decl_context == FIELD
15682 24511461 : && sfk != sfk_constructor)
15683 0 : return error_mark_node;
15684 : }
15685 27730509 : if (decl_context == FIELD)
15686 27730509 : staticp = 0;
15687 : }
15688 172551851 : else if (friendp)
15689 : {
15690 6884129 : if (virtualp)
15691 : {
15692 : /* Cannot be both friend and virtual. */
15693 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15694 12 : richloc.add_range (declspecs->locations[ds_friend]);
15695 12 : error_at (&richloc, "virtual functions cannot be friends");
15696 12 : friendp = 0;
15697 12 : }
15698 6884129 : if (decl_context == NORMAL)
15699 0 : error_at (declarator->id_loc,
15700 : "friend declaration not in class definition");
15701 6884129 : if (current_function_decl && funcdef_flag)
15702 : {
15703 9 : error_at (declarator->id_loc,
15704 : "cannot define friend function %qs in a local "
15705 : "class definition", name);
15706 9 : friendp = 0;
15707 : }
15708 : /* [class.friend]/6: A function can be defined in a friend
15709 : declaration if the function name is unqualified. */
15710 6884129 : if (funcdef_flag && in_namespace)
15711 : {
15712 6 : if (in_namespace == global_namespace)
15713 3 : error_at (declarator->id_loc,
15714 : "friend function definition %qs cannot have "
15715 : "a name qualified with %<::%>", name);
15716 : else
15717 3 : error_at (declarator->id_loc,
15718 : "friend function definition %qs cannot have "
15719 : "a name qualified with %<%D::%>", name,
15720 : in_namespace);
15721 : }
15722 : }
15723 165667722 : else if (ctype && sfk == sfk_conversion)
15724 : {
15725 1068468 : if (explicitp == 1)
15726 : {
15727 499912 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
15728 499912 : explicitp = 2;
15729 : }
15730 : }
15731 164599254 : else if (sfk == sfk_deduction_guide)
15732 : {
15733 1275335 : if (explicitp == 1)
15734 200282360 : explicitp = 2;
15735 : }
15736 :
15737 200282360 : if (xobj_parm)
15738 : {
15739 94928 : if (!ctype
15740 110 : && (decl_context == NORMAL || friendp)
15741 101 : && (in_namespace
15742 101 : || !declarator->declarator->u.id.qualifying_scope))
15743 : {
15744 24 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15745 : "a non-member function cannot have "
15746 : "an explicit object parameter");
15747 24 : is_xobj_member_function = false;
15748 : }
15749 : else
15750 : {
15751 94904 : if (virtualp)
15752 : {
15753 24 : auto_diagnostic_group d;
15754 24 : error_at (declspecs->locations[ds_virtual],
15755 : "an explicit object member function cannot "
15756 : "be %<virtual%>");
15757 24 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15758 : "explicit object parameter declared here");
15759 24 : virtualp = false;
15760 24 : }
15761 94904 : if (staticp >= 2)
15762 : {
15763 3 : auto_diagnostic_group d;
15764 3 : error_at (declspecs->locations[ds_storage_class],
15765 : "an explicit object member function cannot "
15766 : "be %<static%>");
15767 3 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15768 : "explicit object parameter declared here");
15769 3 : }
15770 94904 : if (unqualified_id
15771 200377255 : && identifier_p (unqualified_id)
15772 189799 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15773 12 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15774 : "%qD cannot be an explicit object member "
15775 : "function", unqualified_id);
15776 : }
15777 : }
15778 200282360 : tree pushed_scope = NULL_TREE;
15779 200282360 : if (funcdecl_p
15780 200282360 : && decl_context != FIELD
15781 82578148 : && inner_declarator->u.id.qualifying_scope
15782 209669840 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15783 9386052 : pushed_scope
15784 9386052 : = push_scope (inner_declarator->u.id.qualifying_scope);
15785 :
15786 200282360 : arg_types = grokparms (declarator->u.function.parameters, &parms);
15787 :
15788 200282360 : if (pushed_scope)
15789 9386052 : pop_scope (pushed_scope);
15790 :
15791 200282360 : if (inner_declarator
15792 199888312 : && inner_declarator->kind == cdk_id
15793 197957896 : && inner_declarator->u.id.sfk == sfk_destructor
15794 3392450 : && arg_types != void_list_node)
15795 : {
15796 21 : error_at (declarator->id_loc,
15797 : "destructors may not have parameters");
15798 21 : arg_types = void_list_node;
15799 21 : parms = NULL_TREE;
15800 21 : is_xobj_member_function = false;
15801 : }
15802 :
15803 200282360 : type = cp_build_function_type (type, arg_types);
15804 :
15805 200282360 : tree attrs = declarator->std_attributes;
15806 200282360 : if (tx_qual)
15807 : {
15808 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
15809 : /* transaction_safe applies to the type, but
15810 : transaction_safe_dynamic applies to the function. */
15811 242 : if (is_attribute_p ("transaction_safe", tx_qual))
15812 213 : attrs = attr_chainon (attrs, att);
15813 : else
15814 29 : returned_attrs = attr_chainon (returned_attrs, att);
15815 : }
15816 :
15817 : /* Actually apply the contract specifiers to the declaration. */
15818 200282360 : if (flag_contracts)
15819 23094300 : contract_specifiers
15820 : = contract_specifiers_concat
15821 23094300 : (contract_specifiers,
15822 23094300 : declarator->u.function.contract_specifiers);
15823 :
15824 200282360 : if (attrs)
15825 : /* [dcl.fct]/2:
15826 :
15827 : The optional attribute-specifier-seq appertains to
15828 : the function type. */
15829 513 : cplus_decl_attributes (&type, attrs, 0);
15830 :
15831 200282360 : if (raises)
15832 79761383 : type = build_exception_variant (type, raises);
15833 : }
15834 200282360 : break;
15835 :
15836 203790696 : case cdk_pointer:
15837 203790696 : case cdk_reference:
15838 203790696 : case cdk_ptrmem:
15839 : /* Filter out pointers-to-references and references-to-references.
15840 : We can get these if a TYPE_DECL is used. */
15841 :
15842 203790696 : if (TYPE_REF_P (type))
15843 : {
15844 240 : if (declarator->kind != cdk_reference)
15845 : {
15846 3 : error ("cannot declare pointer to %q#T", type);
15847 3 : type = TREE_TYPE (type);
15848 : }
15849 :
15850 : /* In C++0x, we allow reference to reference declarations
15851 : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15852 : and template type arguments [14.3.1/4 temp.arg.type]. The
15853 : check for direct reference to reference declarations, which
15854 : are still forbidden, occurs below. Reasoning behind the change
15855 : can be found in DR106, DR540, and the rvalue reference
15856 : proposals. */
15857 237 : else if (cxx_dialect == cxx98)
15858 : {
15859 0 : error ("cannot declare reference to %q#T", type);
15860 0 : type = TREE_TYPE (type);
15861 : }
15862 : }
15863 203790456 : else if (VOID_TYPE_P (type))
15864 : {
15865 5440682 : if (declarator->kind == cdk_reference)
15866 4 : error ("cannot declare reference to %q#T", type);
15867 5440678 : else if (declarator->kind == cdk_ptrmem)
15868 3 : error ("cannot declare pointer to %q#T member", type);
15869 : }
15870 :
15871 : /* We now know that the TYPE_QUALS don't apply to the decl,
15872 : but to the target of the pointer. */
15873 203790696 : type_quals = TYPE_UNQUALIFIED;
15874 :
15875 : /* This code used to handle METHOD_TYPE, but I don't think it's
15876 : possible to get it here anymore. */
15877 203790696 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15878 203790696 : if (declarator->kind == cdk_ptrmem
15879 1163342 : && TREE_CODE (type) == FUNCTION_TYPE)
15880 : {
15881 941076 : memfn_quals |= type_memfn_quals (type);
15882 941076 : if (rqual == REF_QUAL_NONE)
15883 600082 : rqual = type_memfn_rqual (type);
15884 1882152 : type = build_memfn_type (type,
15885 941076 : declarator->u.pointer.class_type,
15886 : memfn_quals,
15887 : rqual);
15888 941076 : if (type == error_mark_node)
15889 : return error_mark_node;
15890 :
15891 : rqual = REF_QUAL_NONE;
15892 : memfn_quals = TYPE_UNQUALIFIED;
15893 : }
15894 :
15895 203790693 : if (TREE_CODE (type) == FUNCTION_TYPE
15896 203790693 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15897 1066072 : || type_memfn_rqual (type) != REF_QUAL_NONE))
15898 15 : error (declarator->kind == cdk_reference
15899 : ? G_("cannot declare reference to qualified function type %qT")
15900 : : G_("cannot declare pointer to qualified function type %qT"),
15901 : type);
15902 :
15903 : /* When the pointed-to type involves components of variable size,
15904 : care must be taken to ensure that the size evaluation code is
15905 : emitted early enough to dominate all the possible later uses
15906 : and late enough for the variables on which it depends to have
15907 : been assigned.
15908 :
15909 : This is expected to happen automatically when the pointed-to
15910 : type has a name/declaration of it's own, but special attention
15911 : is required if the type is anonymous.
15912 :
15913 : We handle the NORMAL and FIELD contexts here by inserting a
15914 : dummy statement that just evaluates the size at a safe point
15915 : and ensures it is not deferred until e.g. within a deeper
15916 : conditional context (c++/43555).
15917 :
15918 : We expect nothing to be needed here for PARM or TYPENAME.
15919 : Evaluating the size at this point for TYPENAME would
15920 : actually be incorrect, as we might be in the middle of an
15921 : expression with side effects on the pointed-to type size
15922 : "arguments" prior to the pointer declaration point and the
15923 : size evaluation could end up prior to the side effects. */
15924 :
15925 203790693 : if (!TYPE_NAME (type)
15926 5755929 : && (decl_context == NORMAL || decl_context == FIELD)
15927 644747 : && at_function_scope_p ()
15928 203832548 : && variably_modified_type_p (type, NULL_TREE))
15929 : {
15930 119 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15931 : NULL_TREE, type);
15932 119 : add_decl_expr (TYPE_NAME (type));
15933 : }
15934 :
15935 203790693 : if (declarator->kind == cdk_reference)
15936 : {
15937 : /* In C++0x, the type we are creating a reference to might be
15938 : a typedef which is itself a reference type. In that case,
15939 : we follow the reference collapsing rules in
15940 : [7.1.3/8 dcl.typedef] to create the final reference type:
15941 :
15942 : "If a typedef TD names a type that is a reference to a type
15943 : T, an attempt to create the type 'lvalue reference to cv TD'
15944 : creates the type 'lvalue reference to T,' while an attempt
15945 : to create the type "rvalue reference to cv TD' creates the
15946 : type TD."
15947 : */
15948 136403516 : if (VOID_TYPE_P (type))
15949 : /* We already gave an error. */;
15950 136403512 : else if (TYPE_REF_P (type))
15951 : {
15952 237 : if (declarator->u.reference.rvalue_ref)
15953 : /* Leave type alone. */;
15954 : else
15955 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15956 : }
15957 : else
15958 136403275 : type = cp_build_reference_type
15959 136403275 : (type, declarator->u.reference.rvalue_ref);
15960 :
15961 : /* In C++0x, we need this check for direct reference to
15962 : reference declarations, which are forbidden by
15963 : [8.3.2/5 dcl.ref]. Reference to reference declarations
15964 : are only allowed indirectly through typedefs and template
15965 : type arguments. Example:
15966 :
15967 : void foo(int & &); // invalid ref-to-ref decl
15968 :
15969 : typedef int & int_ref;
15970 : void foo(int_ref &); // valid ref-to-ref decl
15971 : */
15972 136403516 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15973 2 : error ("cannot declare reference to %q#T, which is not "
15974 : "a typedef or a template type argument", type);
15975 : }
15976 67387177 : else if (TREE_CODE (type) == METHOD_TYPE)
15977 941073 : type = build_ptrmemfunc_type (build_pointer_type (type));
15978 66446104 : else if (declarator->kind == cdk_ptrmem)
15979 : {
15980 222266 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15981 : != NAMESPACE_DECL);
15982 222266 : if (declarator->u.pointer.class_type == error_mark_node)
15983 : /* We will already have complained. */
15984 3 : type = error_mark_node;
15985 : else
15986 222263 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15987 : type);
15988 : }
15989 : else
15990 66223838 : type = build_pointer_type (type);
15991 :
15992 : /* Process a list of type modifier keywords (such as
15993 : const or volatile) that were given inside the `*' or `&'. */
15994 :
15995 203790693 : if (declarator->u.pointer.qualifiers)
15996 : {
15997 8957153 : type
15998 8957153 : = cp_build_qualified_type (type,
15999 : declarator->u.pointer.qualifiers);
16000 8957153 : type_quals = cp_type_quals (type);
16001 : }
16002 :
16003 : /* Apply C++11 attributes to the pointer, and not to the
16004 : type pointed to. This is unlike what is done for GNU
16005 : attributes above. It is to comply with [dcl.ptr]/1:
16006 :
16007 : [the optional attribute-specifier-seq (7.6.1) appertains
16008 : to the pointer and not to the object pointed to]. */
16009 203790693 : if (declarator->std_attributes)
16010 189 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
16011 :
16012 : ctype = NULL_TREE;
16013 : break;
16014 :
16015 : case cdk_error:
16016 : break;
16017 :
16018 0 : default:
16019 0 : gcc_unreachable ();
16020 : }
16021 : }
16022 :
16023 1093940364 : id_loc = declarator ? declarator->id_loc : input_location;
16024 :
16025 1093940364 : if (innermost_code != cdk_function
16026 : /* Don't check this if it can be the artificial decltype(auto)
16027 : we created when building a constraint in a compound-requirement:
16028 : that the type-constraint is plain is going to be checked in
16029 : cp_parser_compound_requirement. */
16030 1093940364 : && decl_context != TYPENAME
16031 1093940364 : && check_decltype_auto (id_loc, type))
16032 20 : return error_mark_node;
16033 :
16034 : /* A `constexpr' specifier used in an object declaration declares
16035 : the object as `const'. */
16036 1093940344 : if (constexpr_p && innermost_code != cdk_function)
16037 : {
16038 : /* DR1688 says that a `constexpr' specifier in combination with
16039 : `volatile' is valid. */
16040 :
16041 20936990 : if (!TYPE_REF_P (type))
16042 : {
16043 20925073 : type_quals |= TYPE_QUAL_CONST;
16044 20925073 : type = cp_build_qualified_type (type, type_quals);
16045 : }
16046 : }
16047 :
16048 615862311 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
16049 3011397 : && !FUNC_OR_METHOD_TYPE_P (type)
16050 1095948140 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
16051 : {
16052 0 : error ("template-id %qD used as a declarator",
16053 : unqualified_id);
16054 0 : unqualified_id = dname;
16055 : }
16056 :
16057 : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
16058 : qualified with a class-name, turn it into a METHOD_TYPE, unless
16059 : we know that the function is static. We take advantage of this
16060 : opportunity to do other processing that pertains to entities
16061 : explicitly declared to be class members. Note that if DECLARATOR
16062 : is non-NULL, we know it is a cdk_id declarator; otherwise, we
16063 : would not have exited the loop above. */
16064 1093940344 : if (declarator
16065 616110631 : && declarator->kind == cdk_id
16066 615862311 : && declarator->u.id.qualifying_scope
16067 1104133282 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
16068 : {
16069 9910387 : ctype = declarator->u.id.qualifying_scope;
16070 9910387 : ctype = TYPE_MAIN_VARIANT (ctype);
16071 9910387 : template_count = num_template_headers_for_class (ctype);
16072 :
16073 9910387 : if (ctype == current_class_type)
16074 : {
16075 54 : if (friendp)
16076 : {
16077 15 : permerror (declspecs->locations[ds_friend],
16078 : "member functions are implicitly "
16079 : "friends of their class");
16080 15 : friendp = 0;
16081 : }
16082 : else
16083 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
16084 : ctype, name);
16085 : }
16086 9910333 : else if (/* If the qualifying type is already complete, then we
16087 : can skip the following checks. */
16088 9910333 : !COMPLETE_TYPE_P (ctype)
16089 154 : && (/* If the function is being defined, then
16090 : qualifying type must certainly be complete. */
16091 : funcdef_flag
16092 : /* A friend declaration of "T::f" is OK, even if
16093 : "T" is a template parameter. But, if this
16094 : function is not a friend, the qualifying type
16095 : must be a class. */
16096 117 : || (!friendp && !CLASS_TYPE_P (ctype))
16097 : /* For a declaration, the type need not be
16098 : complete, if either it is dependent (since there
16099 : is no meaningful definition of complete in that
16100 : case) or the qualifying class is currently being
16101 : defined. */
16102 129 : || !(dependent_type_p (ctype)
16103 24 : || currently_open_class (ctype)))
16104 : /* Check that the qualifying type is complete. */
16105 9910397 : && !complete_type_or_else (ctype, NULL_TREE))
16106 64 : return error_mark_node;
16107 9910269 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16108 : {
16109 9386314 : if (current_class_type
16110 289 : && (!friendp || funcdef_flag || initialized))
16111 : {
16112 57 : error_at (id_loc, funcdef_flag || initialized
16113 : ? G_("cannot define member function %<%T::%s%> "
16114 : "within %qT")
16115 : : G_("cannot declare member function %<%T::%s%> "
16116 : "within %qT"),
16117 : ctype, name, current_class_type);
16118 48 : return error_mark_node;
16119 : }
16120 : }
16121 523955 : else if (typedef_p && current_class_type)
16122 : {
16123 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
16124 : ctype, name, current_class_type);
16125 0 : return error_mark_node;
16126 : }
16127 : }
16128 :
16129 1093940232 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
16130 61398956 : ctype = current_class_type;
16131 :
16132 : /* Now TYPE has the actual type. */
16133 :
16134 1093940232 : if (returned_attrs)
16135 : {
16136 2970 : if (attrlist)
16137 2970 : *attrlist = attr_chainon (returned_attrs, *attrlist);
16138 : else
16139 : attrlist = &returned_attrs;
16140 : }
16141 :
16142 1093940232 : if (declarator
16143 616110519 : && declarator->kind == cdk_id
16144 615862199 : && declarator->std_attributes
16145 603502 : && attrlist != NULL)
16146 : {
16147 : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
16148 : a declarator-id appertains to the entity that is declared. */
16149 603499 : if (declarator->std_attributes != error_mark_node)
16150 : {
16151 603481 : if (flag_reflection
16152 : && declarator->std_attributes != error_mark_node
16153 29054 : && lookup_annotation (declarator->std_attributes)
16154 40 : && *attrlist != error_mark_node
16155 603521 : && lookup_annotation (*attrlist))
16156 : /* If there are annotations in both lists, ensure
16157 : declarator->std_attributes go after *attrlist. See
16158 : PR124399. */
16159 14 : *attrlist = chainon (copy_list (*attrlist),
16160 14 : declarator->std_attributes);
16161 : else
16162 603467 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
16163 : }
16164 : else
16165 : /* We should have already diagnosed the issue (c++/78344). */
16166 18 : gcc_assert (seen_error ());
16167 : }
16168 :
16169 : /* Handle parameter packs. */
16170 1093940232 : if (parameter_pack_p)
16171 : {
16172 3234627 : if (decl_context == PARM)
16173 : /* Turn the type into a pack expansion.*/
16174 3234627 : type = make_pack_expansion (type);
16175 : else
16176 0 : error ("non-parameter %qs cannot be a parameter pack", name);
16177 : }
16178 :
16179 1093940232 : if ((decl_context == FIELD || decl_context == PARM)
16180 486093011 : && !processing_template_decl
16181 1248949505 : && variably_modified_type_p (type, NULL_TREE))
16182 : {
16183 12 : if (decl_context == FIELD)
16184 6 : error_at (id_loc,
16185 : "data member may not have variably modified type %qT", type);
16186 : else
16187 6 : error_at (id_loc,
16188 : "parameter may not have variably modified type %qT", type);
16189 12 : type = error_mark_node;
16190 : }
16191 :
16192 1093940232 : if (explicitp == 1 || (explicitp && friendp))
16193 : {
16194 : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
16195 : in the declaration of a constructor or conversion function within
16196 : a class definition. */
16197 74 : if (!current_class_type)
16198 59 : error_at (declspecs->locations[ds_explicit],
16199 : "%<explicit%> outside class declaration");
16200 15 : else if (friendp)
16201 12 : error_at (declspecs->locations[ds_explicit],
16202 : "%<explicit%> in friend declaration");
16203 : else
16204 3 : error_at (declspecs->locations[ds_explicit],
16205 : "only declarations of constructors and conversion operators "
16206 : "can be %<explicit%>");
16207 : explicitp = 0;
16208 : }
16209 :
16210 1093940232 : if (storage_class == sc_mutable)
16211 : {
16212 142843 : location_t sloc = declspecs->locations[ds_storage_class];
16213 142843 : if (decl_context != FIELD || friendp)
16214 : {
16215 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
16216 : name);
16217 30 : storage_class = sc_none;
16218 : }
16219 142813 : else if (decl_context == TYPENAME || typedef_p)
16220 : {
16221 0 : error_at (sloc,
16222 : "non-object member %qs cannot be declared %<mutable%>",
16223 : name);
16224 0 : storage_class = sc_none;
16225 : }
16226 142813 : else if (FUNC_OR_METHOD_TYPE_P (type))
16227 : {
16228 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
16229 : name);
16230 3 : storage_class = sc_none;
16231 : }
16232 142810 : else if (staticp)
16233 : {
16234 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
16235 : name);
16236 0 : storage_class = sc_none;
16237 : }
16238 142810 : else if (type_quals & TYPE_QUAL_CONST)
16239 : {
16240 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
16241 : name);
16242 9 : storage_class = sc_none;
16243 : }
16244 142801 : else if (TYPE_REF_P (type))
16245 : {
16246 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
16247 : name);
16248 6 : storage_class = sc_none;
16249 : }
16250 : }
16251 :
16252 : /* If this is declaring a typedef name, return a TYPE_DECL. */
16253 1093940232 : if (typedef_p && decl_context != TYPENAME)
16254 : {
16255 49694403 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
16256 49694403 : tree decl;
16257 :
16258 49694403 : if (funcdef_flag)
16259 : {
16260 6 : if (decl_context == NORMAL)
16261 3 : error_at (id_loc,
16262 : "typedef may not be a function definition");
16263 : else
16264 3 : error_at (id_loc,
16265 : "typedef may not be a member function definition");
16266 6 : return error_mark_node;
16267 : }
16268 :
16269 : /* This declaration:
16270 :
16271 : typedef void f(int) const;
16272 :
16273 : declares a function type which is not a member of any
16274 : particular class, but which is cv-qualified; for
16275 : example "f S::*" declares a pointer to a const-qualified
16276 : member function of S. We record the cv-qualification in the
16277 : function type. */
16278 49694397 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
16279 : {
16280 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
16281 :
16282 : /* We have now dealt with these qualifiers. */
16283 62 : memfn_quals = TYPE_UNQUALIFIED;
16284 62 : rqual = REF_QUAL_NONE;
16285 : }
16286 :
16287 49694397 : if (type_uses_auto (type))
16288 : {
16289 62 : if (alias_p)
16290 20 : error_at (declspecs->locations[ds_type_spec],
16291 : "%<auto%> not allowed in alias declaration");
16292 : else
16293 42 : error_at (declspecs->locations[ds_type_spec],
16294 : "typedef declared %<auto%>");
16295 62 : type = error_mark_node;
16296 : }
16297 :
16298 49694397 : if (reqs)
16299 3 : error_at (location_of (reqs), "requires-clause on typedef");
16300 :
16301 49694397 : if (id_declarator && declarator->u.id.qualifying_scope)
16302 : {
16303 21 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
16304 21 : type = error_mark_node;
16305 : }
16306 :
16307 49694397 : if (decl_context == FIELD)
16308 26696089 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
16309 : else
16310 22998308 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
16311 :
16312 49694397 : if (decl_context != FIELD)
16313 : {
16314 22998308 : if (!current_function_decl)
16315 10146662 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
16316 12851646 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
16317 : /* The TYPE_DECL is "abstract" because there will be
16318 : clones of this constructor/destructor, and there will
16319 : be copies of this TYPE_DECL generated in those
16320 : clones. The decloning optimization (for space) may
16321 : revert this subsequently if it determines that
16322 : the clones should share a common implementation. */
16323 163984 : DECL_ABSTRACT_P (decl) = true;
16324 :
16325 22998308 : set_originating_module (decl);
16326 : }
16327 26696089 : else if (current_class_type
16328 26696089 : && constructor_name_p (unqualified_id, current_class_type))
16329 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
16330 : "as enclosing class",
16331 : unqualified_id);
16332 :
16333 : /* If the user declares "typedef struct {...} foo" then the
16334 : struct will have an anonymous name. Fill that name in now.
16335 : Nothing can refer to it, so nothing needs know about the name
16336 : change. */
16337 49694397 : if (type != error_mark_node
16338 49694314 : && unqualified_id
16339 49694311 : && TYPE_NAME (type)
16340 46189411 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16341 92239805 : && (TYPE_UNNAMED_P (type)
16342 : /* An enum may have previously used an enumerator for linkage
16343 : purposes, but we want the typedef name to take priority. */
16344 45775342 : || enum_with_enumerator_for_linkage_p (type))
16345 414491 : && declspecs->type_definition_p
16346 414408 : && attributes_naming_typedef_ok (*attrlist)
16347 50108802 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
16348 414387 : name_unnamed_type (type, decl);
16349 :
16350 49694397 : if (signed_p
16351 49694397 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
16352 395110 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
16353 :
16354 49694397 : bad_specifiers (decl, BSP_TYPE, virtualp,
16355 : memfn_quals != TYPE_UNQUALIFIED,
16356 : inlinep, friendp, raises != NULL_TREE,
16357 : declspecs->locations);
16358 :
16359 49694397 : if (alias_p)
16360 : /* Acknowledge that this was written:
16361 : `using analias = atype;'. */
16362 22571339 : TYPE_DECL_ALIAS_P (decl) = 1;
16363 :
16364 : /* We use a flag so that dependent_opaque_alias_p doesn't have to
16365 : recompute the answer every single time. */
16366 49694397 : TYPE_DECL_OPAQUE_ALIAS_P (decl) = any_lambdas_p (type);
16367 :
16368 49694397 : return decl;
16369 : }
16370 :
16371 : /* Detect the case of an array type of unspecified size
16372 : which came, as such, direct from a typedef name.
16373 : We must copy the type, so that the array's domain can be
16374 : individually set by the object's initializer. */
16375 :
16376 1044245829 : if (type && typedef_type
16377 1013174367 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
16378 1044865326 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
16379 83 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16380 :
16381 : /* Detect where we're using a typedef of function type to declare a
16382 : function. PARMS will not be set, so we must create it now. */
16383 :
16384 1044245829 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
16385 : {
16386 16562 : tree decls = NULL_TREE;
16387 16562 : tree args;
16388 :
16389 16562 : for (args = TYPE_ARG_TYPES (type);
16390 35589 : args && args != void_list_node;
16391 19027 : args = TREE_CHAIN (args))
16392 : {
16393 19027 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
16394 19027 : TREE_VALUE (args));
16395 :
16396 19027 : DECL_CHAIN (decl) = decls;
16397 19027 : decls = decl;
16398 : }
16399 :
16400 16562 : parms = nreverse (decls);
16401 :
16402 16562 : if (decl_context != TYPENAME)
16403 : {
16404 : /* The qualifiers on the function type become the qualifiers on
16405 : the non-static member function. */
16406 15778 : memfn_quals |= type_memfn_quals (type);
16407 15778 : rqual = type_memfn_rqual (type);
16408 15778 : type_quals = TYPE_UNQUALIFIED;
16409 15778 : raises = TYPE_RAISES_EXCEPTIONS (type);
16410 : }
16411 : }
16412 :
16413 : /* If this is a type name (such as, in a cast or sizeof),
16414 : compute the type and return it now. */
16415 :
16416 1044245045 : if (decl_context == TYPENAME)
16417 : {
16418 : /* Note that here we don't care about type_quals. */
16419 :
16420 : /* Special case: "friend class foo" looks like a TYPENAME context. */
16421 430344240 : if (friendp)
16422 : {
16423 0 : if (inlinep)
16424 : {
16425 0 : error ("%<inline%> specified for friend class declaration");
16426 0 : inlinep = 0;
16427 : }
16428 :
16429 0 : if (!current_aggr)
16430 : {
16431 : /* Don't allow friend declaration without a class-key. */
16432 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16433 0 : permerror (input_location, "template parameters cannot be friends");
16434 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
16435 0 : permerror (input_location, "friend declaration requires class-key, "
16436 : "i.e. %<friend class %T::%D%>",
16437 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
16438 : else
16439 0 : permerror (input_location, "friend declaration requires class-key, "
16440 : "i.e. %<friend %#T%>",
16441 : type);
16442 : }
16443 :
16444 : /* Only try to do this stuff if we didn't already give up. */
16445 0 : if (type != integer_type_node)
16446 : {
16447 : /* A friendly class? */
16448 0 : if (current_class_type)
16449 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
16450 : /*complain=*/true);
16451 : else
16452 0 : error ("trying to make class %qT a friend of global scope",
16453 : type);
16454 :
16455 0 : type = void_type_node;
16456 : }
16457 : }
16458 430344240 : else if (memfn_quals || rqual)
16459 : {
16460 14046 : if (ctype == NULL_TREE
16461 14046 : && TREE_CODE (type) == METHOD_TYPE)
16462 0 : ctype = TYPE_METHOD_BASETYPE (type);
16463 :
16464 14046 : if (ctype)
16465 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
16466 : /* Core issue #547: need to allow this in template type args.
16467 : Allow it in general in C++11 for alias-declarations. */
16468 14046 : else if ((template_type_arg || cxx_dialect >= cxx11)
16469 14044 : && TREE_CODE (type) == FUNCTION_TYPE)
16470 14042 : type = apply_memfn_quals (type, memfn_quals, rqual);
16471 : else
16472 4 : error ("invalid qualifiers on non-member function type");
16473 : }
16474 :
16475 430344240 : if (reqs)
16476 3 : error_at (location_of (reqs), "requires-clause on type-id");
16477 :
16478 430344240 : return type;
16479 : }
16480 613901589 : else if (unqualified_id == NULL_TREE && decl_context != PARM
16481 883964 : && decl_context != CATCHPARM
16482 388141 : && TREE_CODE (type) != UNION_TYPE
16483 387758 : && ! bitfield
16484 387758 : && innermost_code != cdk_decomp)
16485 : {
16486 9 : error ("abstract declarator %qT used as declaration", type);
16487 9 : return error_mark_node;
16488 : }
16489 :
16490 613901580 : if (!FUNC_OR_METHOD_TYPE_P (type))
16491 : {
16492 : /* Only functions may be declared using an operator-function-id. */
16493 415985295 : if (dname && IDENTIFIER_ANY_OP_P (dname))
16494 : {
16495 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
16496 8 : return error_mark_node;
16497 : }
16498 :
16499 415985287 : if (reqs)
16500 15 : error_at (location_of (reqs),
16501 : "requires-clause on declaration of non-function type %qT",
16502 : type);
16503 : }
16504 :
16505 : /* We don't check parameter types here because we can emit a better
16506 : error message later. */
16507 613901572 : if (decl_context != PARM)
16508 : {
16509 304587223 : type = check_var_type (unqualified_id, type, id_loc);
16510 304587223 : if (type == error_mark_node)
16511 : return error_mark_node;
16512 : }
16513 :
16514 : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16515 : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16516 :
16517 613901488 : if (decl_context == PARM || decl_context == CATCHPARM)
16518 : {
16519 309824903 : if (ctype || in_namespace)
16520 0 : error ("cannot use %<::%> in parameter declaration");
16521 :
16522 309824903 : tree auto_node = type_uses_auto (type);
16523 309824903 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16524 : {
16525 126 : bool err_p = true;
16526 126 : if (cxx_dialect >= cxx14)
16527 : {
16528 224 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16529 3 : error_at (typespec_loc,
16530 : "cannot declare a parameter with %<decltype(auto)%>");
16531 115 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16532 : {
16533 7 : auto_diagnostic_group g;
16534 7 : gcc_rich_location richloc (typespec_loc);
16535 7 : richloc.add_fixit_insert_after ("<>");
16536 7 : error_at (&richloc,
16537 : "missing template argument list after %qE; template "
16538 : "placeholder not permitted in parameter", c);
16539 7 : if (decl_context == PARM && cxx_dialect >= cxx20)
16540 6 : inform (typespec_loc, "or use %<auto%> for an "
16541 : "abbreviated function template");
16542 7 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16543 7 : }
16544 108 : else if (decl_context == CATCHPARM || template_parm_flag)
16545 14 : error_at (typespec_loc,
16546 : "%<auto%> parameter not permitted in this context");
16547 : else
16548 : /* Do not issue an error while tentatively parsing a function
16549 : parameter: for T t(auto(a), 42);, when we just saw the 1st
16550 : parameter, we don't know yet that this construct won't be
16551 : a function declaration. Defer the checking to
16552 : cp_parser_parameter_declaration_clause. */
16553 : err_p = false;
16554 : }
16555 : else
16556 8 : error_at (typespec_loc, "parameter declared %<auto%>");
16557 32 : if (err_p)
16558 32 : type = error_mark_node;
16559 : }
16560 :
16561 : /* A parameter declared as an array of T is really a pointer to T.
16562 : One declared as a function is really a pointer to a function.
16563 : One declared as a member is really a pointer to member. */
16564 :
16565 309824903 : if (TREE_CODE (type) == ARRAY_TYPE)
16566 : {
16567 : /* Transfer const-ness of array into that of type pointed to. */
16568 804871 : type = build_pointer_type (TREE_TYPE (type));
16569 804871 : type_quals = TYPE_UNQUALIFIED;
16570 804871 : array_parameter_p = true;
16571 : }
16572 309020032 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16573 104005 : type = build_pointer_type (type);
16574 : }
16575 :
16576 153108665 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16577 : /* Don't convert xobj member functions to METHOD_TYPE. */
16578 117881708 : && !is_xobj_member_function
16579 720692904 : && !(unqualified_id
16580 213573828 : && identifier_p (unqualified_id)
16581 106791016 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16582 : {
16583 106782812 : cp_cv_quals real_quals = memfn_quals;
16584 106782812 : if (cxx_dialect < cxx14 && constexpr_p
16585 724982 : && sfk != sfk_constructor && sfk != sfk_destructor)
16586 3730 : real_quals |= TYPE_QUAL_CONST;
16587 106782812 : type = build_memfn_type (type, ctype, real_quals, rqual);
16588 : }
16589 :
16590 613901488 : {
16591 613901488 : tree decl = NULL_TREE;
16592 :
16593 613901488 : if (decl_context == PARM)
16594 : {
16595 309314349 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16596 309314349 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16597 :
16598 309314349 : bad_specifiers (decl, BSP_PARM, virtualp,
16599 : memfn_quals != TYPE_UNQUALIFIED,
16600 : inlinep, friendp, raises != NULL_TREE,
16601 : declspecs->locations);
16602 : }
16603 304587139 : else if (decl_context == FIELD)
16604 : {
16605 150082558 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16606 20873331 : if (tree auto_node = type_uses_auto (type))
16607 : {
16608 24 : location_t tloc = declspecs->locations[ds_type_spec];
16609 24 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16610 6 : error_at (tloc, "invalid use of template-name %qE without an "
16611 : "argument list",
16612 6 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16613 : else
16614 18 : error_at (tloc, "non-static data member declared with "
16615 : "placeholder %qT", auto_node);
16616 24 : type = error_mark_node;
16617 : }
16618 :
16619 : /* The C99 flexible array extension. */
16620 125257463 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16621 151240565 : && TYPE_DOMAIN (type) == NULL_TREE)
16622 : {
16623 1400 : if (ctype
16624 1400 : && (TREE_CODE (ctype) == UNION_TYPE
16625 1400 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16626 84 : pedwarn (id_loc, OPT_Wpedantic,
16627 : "flexible array member in union is a GCC extension");
16628 :
16629 : else
16630 : {
16631 : /* Array is a flexible member. */
16632 1316 : if (name)
16633 1316 : pedwarn (id_loc, OPT_Wpedantic,
16634 : "ISO C++ forbids flexible array member %qs", name);
16635 : else
16636 : pedwarn (input_location, OPT_Wpedantic,
16637 : "ISO C++ forbids flexible array members");
16638 :
16639 : /* Flexible array member has a null domain. */
16640 1316 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16641 : }
16642 : }
16643 :
16644 150082558 : if (type == error_mark_node)
16645 : {
16646 : /* Happens when declaring arrays of sizes which
16647 : are error_mark_node, for example. */
16648 24 : decl = NULL_TREE;
16649 : }
16650 150082534 : else if (in_namespace && !friendp)
16651 : {
16652 : /* Something like struct S { int N::j; }; */
16653 6 : error_at (id_loc, "invalid use of %<::%>");
16654 6 : return error_mark_node;
16655 : }
16656 150082528 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16657 : {
16658 115379514 : int publicp = 0;
16659 115379514 : tree function_context;
16660 :
16661 115379514 : if (friendp == 0)
16662 : {
16663 : /* This should never happen in pure C++ (the check
16664 : could be an assert). It could happen in
16665 : Objective-C++ if someone writes invalid code that
16666 : uses a function declaration for an instance
16667 : variable or property (instance variables and
16668 : properties are parsed as FIELD_DECLs, but they are
16669 : part of an Objective-C class, not a C++ class).
16670 : That code is invalid and is caught by this
16671 : check. */
16672 108495442 : if (!ctype)
16673 : {
16674 0 : error ("declaration of function %qD in invalid context",
16675 : unqualified_id);
16676 0 : return error_mark_node;
16677 : }
16678 :
16679 : /* ``A union may [ ... ] not [ have ] virtual functions.''
16680 : ARM 9.5 */
16681 108495442 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16682 : {
16683 3 : error_at (declspecs->locations[ds_virtual],
16684 : "function %qD declared %<virtual%> inside a union",
16685 : unqualified_id);
16686 3 : return error_mark_node;
16687 : }
16688 :
16689 4303411 : if (virtualp
16690 119682922 : && identifier_p (unqualified_id)
16691 4303411 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16692 : {
16693 9 : error_at (declspecs->locations[ds_virtual],
16694 : "%qD cannot be declared %<virtual%>, since it "
16695 : "is always static", unqualified_id);
16696 9 : virtualp = 0;
16697 : }
16698 : }
16699 :
16700 : /* Check that the name used for a destructor makes sense. */
16701 115379511 : if (sfk == sfk_destructor)
16702 : {
16703 3219084 : tree uqname = id_declarator->u.id.unqualified_name;
16704 :
16705 3219084 : if (!ctype)
16706 : {
16707 12 : gcc_assert (friendp);
16708 12 : error_at (id_loc, "expected qualified name in friend "
16709 : "declaration for destructor %qD", uqname);
16710 12 : return error_mark_node;
16711 : }
16712 :
16713 3219072 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16714 : {
16715 9 : error_at (id_loc, "declaration of %qD as member of %qT",
16716 : uqname, ctype);
16717 9 : return error_mark_node;
16718 : }
16719 3219063 : if (concept_p)
16720 : {
16721 9 : error_at (declspecs->locations[ds_concept],
16722 : "a destructor cannot be %qs", "concept");
16723 9 : return error_mark_node;
16724 : }
16725 3219054 : if (constexpr_p && cxx_dialect < cxx20)
16726 : {
16727 6 : error_at (declspecs->locations[ds_constexpr],
16728 : "%<constexpr%> destructors only available"
16729 : " with %<-std=c++20%> or %<-std=gnu++20%>");
16730 6 : return error_mark_node;
16731 : }
16732 3219048 : if (consteval_p)
16733 : {
16734 3 : error_at (declspecs->locations[ds_consteval],
16735 : "a destructor cannot be %qs", "consteval");
16736 3 : return error_mark_node;
16737 : }
16738 : }
16739 112160427 : else if (sfk == sfk_constructor && friendp && !ctype)
16740 : {
16741 0 : error ("expected qualified name in friend declaration "
16742 : "for constructor %qD",
16743 0 : id_declarator->u.id.unqualified_name);
16744 0 : return error_mark_node;
16745 : }
16746 115379472 : if (sfk == sfk_constructor)
16747 24511485 : if (concept_p)
16748 : {
16749 9 : error_at (declspecs->locations[ds_concept],
16750 : "a constructor cannot be %<concept%>");
16751 9 : return error_mark_node;
16752 : }
16753 90867987 : if (concept_p)
16754 : {
16755 9 : error_at (declspecs->locations[ds_concept],
16756 : "a concept cannot be a member function");
16757 9 : concept_p = false;
16758 : }
16759 115379454 : else if (consteval_p
16760 116187204 : && identifier_p (unqualified_id)
16761 116187195 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16762 : {
16763 9 : error_at (declspecs->locations[ds_consteval],
16764 : "%qD cannot be %qs", unqualified_id, "consteval");
16765 9 : consteval_p = false;
16766 : }
16767 :
16768 115379463 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16769 : {
16770 113514 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
16771 113514 : if (variable_template_p (tmpl))
16772 : {
16773 3 : auto_diagnostic_group d;
16774 3 : error_at (id_loc, "specialization of variable template "
16775 : "%qD declared as function", tmpl);
16776 3 : inform (DECL_SOURCE_LOCATION (tmpl),
16777 : "variable template declared here");
16778 3 : return error_mark_node;
16779 3 : }
16780 : }
16781 :
16782 : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16783 115379460 : function_context
16784 : = (ctype != NULL_TREE
16785 115379460 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16786 230758920 : publicp = ((! friendp || ! staticp)
16787 115379460 : && function_context == NULL_TREE);
16788 :
16789 115379460 : decl = grokfndecl (ctype, type,
16790 115379460 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16791 : ? unqualified_id : dname,
16792 : parms,
16793 : unqualified_id,
16794 : declspecs,
16795 : reqs,
16796 : virtualp, flags, memfn_quals, rqual, raises,
16797 : friendp ? -1 : 0, friendp, publicp,
16798 115379460 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16799 115379460 : | (8 * consteval_p),
16800 : initialized,
16801 : is_xobj_member_function, sfk,
16802 : funcdef_flag, late_return_type_p,
16803 : template_count, in_namespace,
16804 : attrlist, contract_specifiers, id_loc);
16805 115379460 : decl = set_virt_specifiers (decl, virt_specifiers);
16806 115379460 : if (decl == NULL_TREE)
16807 97 : return error_mark_node;
16808 : #if 0
16809 : /* This clobbers the attrs stored in `decl' from `attrlist'. */
16810 : /* The decl and setting of decl_attr is also turned off. */
16811 : decl = build_decl_attribute_variant (decl, decl_attr);
16812 : #endif
16813 :
16814 : /* [class.conv.ctor]
16815 :
16816 : A constructor declared without the function-specifier
16817 : explicit that can be called with a single parameter
16818 : specifies a conversion from the type of its first
16819 : parameter to the type of its class. Such a constructor
16820 : is called a converting constructor. */
16821 115379363 : if (explicitp == 2)
16822 6238232 : DECL_NONCONVERTING_P (decl) = 1;
16823 :
16824 115379363 : if (declspecs->explicit_specifier)
16825 456425 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16826 : }
16827 34703014 : else if (!staticp
16828 34703014 : && ((current_class_type
16829 20873322 : && same_type_p (TYPE_MAIN_VARIANT (type),
16830 : current_class_type))
16831 20873226 : || (!dependent_type_p (type)
16832 12591901 : && !COMPLETE_TYPE_P (complete_type (type))
16833 1448 : && (!complete_or_array_type_p (type)
16834 1373 : || initialized == SD_UNINITIALIZED))))
16835 : {
16836 1511 : if (TREE_CODE (type) != ARRAY_TYPE
16837 1511 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16838 : {
16839 171 : if (unqualified_id)
16840 : {
16841 126 : auto_diagnostic_group d;
16842 126 : error_at (id_loc, "field %qD has incomplete type %qT",
16843 : unqualified_id, type);
16844 126 : cxx_incomplete_type_inform (strip_array_types (type));
16845 126 : }
16846 : else
16847 45 : error ("name %qT has incomplete type", type);
16848 :
16849 171 : type = error_mark_node;
16850 171 : decl = NULL_TREE;
16851 : }
16852 : }
16853 55573302 : else if (!verify_type_context (input_location,
16854 : staticp
16855 : ? TCTX_STATIC_STORAGE
16856 : : TCTX_FIELD, type))
16857 : {
16858 0 : type = error_mark_node;
16859 0 : decl = NULL_TREE;
16860 : }
16861 : else
16862 : {
16863 34701497 : if (friendp)
16864 : {
16865 12 : if (unqualified_id)
16866 9 : error_at (id_loc,
16867 : "%qE is neither function nor member function; "
16868 : "cannot be declared friend", unqualified_id);
16869 : else
16870 3 : error ("unnamed field is neither function nor member "
16871 : "function; cannot be declared friend");
16872 12 : return error_mark_node;
16873 : }
16874 34701485 : decl = NULL_TREE;
16875 : }
16876 :
16877 150082383 : if (friendp)
16878 : {
16879 6884041 : if (flag_reflection && !funcdef_flag && decl)
16880 : {
16881 17016 : if (attrlist && lookup_annotation (*attrlist))
16882 : {
16883 : /* Remove the annotations to avoid spurious warning
16884 : below. */
16885 8 : *attrlist = remove_attribute ("internal ", "annotation ",
16886 : *attrlist);
16887 8 : error_at (id_loc, "annotation applied to non-defining "
16888 : "friend declaration %qD", decl);
16889 : }
16890 17016 : for (tree arg = DECL_ARGUMENTS (decl);
16891 39675 : arg; arg = DECL_CHAIN (arg))
16892 22659 : if (lookup_annotation (DECL_ATTRIBUTES (arg)))
16893 8 : error_at (DECL_SOURCE_LOCATION (arg),
16894 : "annotation applied to parameter %qD of "
16895 : "non-defining friend declaration", arg);
16896 : }
16897 : /* Packages tend to use GNU attributes on friends, so we only
16898 : warn for standard attributes. */
16899 6884041 : if (attrlist
16900 6884041 : && !funcdef_flag
16901 6884041 : && cxx11_attribute_p (*attrlist))
16902 : {
16903 27 : *attrlist = NULL_TREE;
16904 27 : auto_diagnostic_group d;
16905 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16906 27 : inform (id_loc, "an attribute that appertains to a friend "
16907 : "declaration that is not a definition is ignored");
16908 27 : }
16909 : /* Friends are treated specially. */
16910 6884041 : if (ctype == current_class_type)
16911 : ; /* We already issued a permerror. */
16912 6884041 : else if (decl && DECL_NAME (decl))
16913 : {
16914 6884038 : set_originating_module (decl, true);
16915 :
16916 6884038 : if (initialized)
16917 : /* Kludge: We need funcdef_flag to be true in do_friend for
16918 : in-class defaulted functions, but that breaks grokfndecl.
16919 : So set it here. */
16920 221819 : funcdef_flag = true;
16921 :
16922 6884038 : cplus_decl_attributes (&decl, *attrlist, 0);
16923 6884038 : *attrlist = NULL_TREE;
16924 :
16925 6884038 : tree scope = ctype ? ctype : in_namespace;
16926 6884038 : decl = do_friend (scope, unqualified_id, decl,
16927 : flags, funcdef_flag);
16928 6884038 : return decl;
16929 : }
16930 : else
16931 3 : return error_mark_node;
16932 : }
16933 :
16934 : /* Structure field. It may not be a function, except for C++. */
16935 :
16936 143198342 : if (decl == NULL_TREE)
16937 : {
16938 34703017 : if (staticp)
16939 : {
16940 : /* C++ allows static class members. All other work
16941 : for this is done by grokfield. */
16942 13829692 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
16943 : dname, type);
16944 13829692 : if (unqualified_id
16945 13829689 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16946 : {
16947 44841 : decl = check_explicit_specialization (unqualified_id, decl,
16948 : template_count,
16949 : concept_p * 8);
16950 44841 : if (decl == error_mark_node)
16951 : return error_mark_node;
16952 : }
16953 13829692 : set_linkage_for_static_data_member (decl);
16954 13829692 : if (concept_p)
16955 15 : error_at (declspecs->locations[ds_concept],
16956 : "static data member %qE declared %qs",
16957 : unqualified_id, "concept");
16958 13829677 : else if (constexpr_p && !initialized)
16959 : {
16960 9 : error_at (DECL_SOURCE_LOCATION (decl),
16961 : "%<constexpr%> static data member %qD must "
16962 : "have an initializer", decl);
16963 9 : constexpr_p = false;
16964 : }
16965 13829692 : if (consteval_p)
16966 3 : error_at (declspecs->locations[ds_consteval],
16967 : "static data member %qE declared %qs",
16968 : unqualified_id, "consteval");
16969 :
16970 13829692 : if (inlinep)
16971 24423 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16972 :
16973 13829692 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16974 13829692 : && !(cxx_dialect >= cxx17 && constexpr_p))
16975 : /* Even if there is an in-class initialization, DECL
16976 : is considered undefined until an out-of-class
16977 : definition is provided, unless this is an inline
16978 : variable. */
16979 1923756 : DECL_EXTERNAL (decl) = 1;
16980 :
16981 13829692 : if (thread_p)
16982 : {
16983 422 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16984 422 : if (!processing_template_decl)
16985 376 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16986 422 : if (declspecs->gnu_thread_keyword_p)
16987 34 : SET_DECL_GNU_TLS_P (decl);
16988 : }
16989 :
16990 : /* Set the constraints on the declaration. */
16991 13829692 : bool memtmpl = (current_template_depth
16992 13829692 : > template_class_depth (current_class_type));
16993 13829692 : if (memtmpl)
16994 : {
16995 402101 : tree ci = current_template_constraints ();
16996 402101 : set_constraints (decl, ci);
16997 : }
16998 : }
16999 : else
17000 : {
17001 20873325 : if (concept_p)
17002 : {
17003 12 : error_at (declspecs->locations[ds_concept],
17004 : "non-static data member %qE declared %qs",
17005 : unqualified_id, "concept");
17006 12 : concept_p = false;
17007 12 : constexpr_p = false;
17008 : }
17009 20873313 : else if (constexpr_p)
17010 : {
17011 9 : error_at (declspecs->locations[ds_constexpr],
17012 : "non-static data member %qE declared %qs",
17013 : unqualified_id, "constexpr");
17014 9 : constexpr_p = false;
17015 : }
17016 20873325 : if (constinit_p)
17017 : {
17018 6 : error_at (declspecs->locations[ds_constinit],
17019 : "non-static data member %qE declared %qs",
17020 : unqualified_id, "constinit");
17021 6 : constinit_p = false;
17022 : }
17023 20873325 : if (consteval_p)
17024 : {
17025 3 : error_at (declspecs->locations[ds_consteval],
17026 : "non-static data member %qE declared %qs",
17027 : unqualified_id, "consteval");
17028 3 : consteval_p = false;
17029 : }
17030 20873325 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
17031 20873325 : DECL_NONADDRESSABLE_P (decl) = bitfield;
17032 20873325 : if (bitfield && !unqualified_id)
17033 139420 : DECL_PADDING_P (decl) = 1;
17034 :
17035 20873325 : if (storage_class == sc_mutable)
17036 : {
17037 142795 : DECL_MUTABLE_P (decl) = 1;
17038 142795 : storage_class = sc_none;
17039 : }
17040 :
17041 20873325 : if (initialized)
17042 : {
17043 : /* An attempt is being made to initialize a non-static
17044 : member. This is new in C++11. */
17045 2429596 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
17046 :
17047 : /* If this has been parsed with static storage class, but
17048 : errors forced staticp to be cleared, ensure NSDMI is
17049 : not present. */
17050 2429596 : if (declspecs->storage_class == sc_static)
17051 3 : DECL_INITIAL (decl) = error_mark_node;
17052 : }
17053 : }
17054 :
17055 55576342 : bad_specifiers (decl, BSP_FIELD, virtualp,
17056 : memfn_quals != TYPE_UNQUALIFIED,
17057 : staticp ? false : inlinep, friendp,
17058 : raises != NULL_TREE,
17059 : declspecs->locations);
17060 : }
17061 : }
17062 154504581 : else if (FUNC_OR_METHOD_TYPE_P (type))
17063 : {
17064 82432760 : tree original_name;
17065 82432760 : int publicp = 0;
17066 :
17067 82432760 : if (!unqualified_id)
17068 0 : return error_mark_node;
17069 :
17070 82432760 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
17071 : original_name = dname;
17072 : else
17073 81542679 : original_name = unqualified_id;
17074 : // FIXME:gcc_assert (original_name == dname);
17075 :
17076 82432760 : if (storage_class == sc_auto)
17077 1 : error_at (declspecs->locations[ds_storage_class],
17078 : "storage class %<auto%> invalid for function %qs", name);
17079 82432759 : else if (storage_class == sc_register)
17080 7 : error_at (declspecs->locations[ds_storage_class],
17081 : "storage class %<register%> invalid for function %qs",
17082 : name);
17083 82432752 : else if (thread_p)
17084 : {
17085 12 : if (declspecs->gnu_thread_keyword_p)
17086 12 : error_at (declspecs->locations[ds_thread],
17087 : "storage class %<__thread%> invalid for function %qs",
17088 : name);
17089 : else
17090 0 : error_at (declspecs->locations[ds_thread],
17091 : "storage class %<thread_local%> invalid for "
17092 : "function %qs", name);
17093 : }
17094 82432740 : else if (constinit_p)
17095 3 : error_at (declspecs->locations[ds_constinit],
17096 : "%<constinit%> specifier invalid for function %qs", name);
17097 :
17098 82432760 : if (virt_specifiers)
17099 6 : error ("virt-specifiers in %qs not allowed outside a class "
17100 : "definition", name);
17101 : /* Function declaration not at top level.
17102 : Storage classes other than `extern' are not allowed
17103 : and `extern' makes no difference. */
17104 82432760 : if (! toplevel_bindings_p ()
17105 62685 : && (storage_class == sc_static
17106 62676 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
17107 82432772 : && pedantic)
17108 : {
17109 9 : if (storage_class == sc_static)
17110 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
17111 : "%<static%> specifier invalid for function %qs "
17112 : "declared out of global scope", name);
17113 : else
17114 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
17115 : "%<inline%> specifier invalid for function %qs "
17116 : "declared out of global scope", name);
17117 : }
17118 :
17119 82432760 : if (ctype == NULL_TREE)
17120 : {
17121 73046741 : if (virtualp)
17122 : {
17123 0 : error ("virtual non-class function %qs", name);
17124 0 : virtualp = 0;
17125 : }
17126 73046741 : else if (sfk == sfk_constructor
17127 73046741 : || sfk == sfk_destructor)
17128 : {
17129 3 : error (funcdef_flag
17130 : ? G_("%qs defined in a non-class scope")
17131 : : G_("%qs declared in a non-class scope"), name);
17132 3 : sfk = sfk_none;
17133 : }
17134 : }
17135 82432760 : if (consteval_p
17136 82797640 : && identifier_p (unqualified_id)
17137 82797640 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
17138 : {
17139 9 : error_at (declspecs->locations[ds_consteval],
17140 : "%qD cannot be %qs", unqualified_id, "consteval");
17141 9 : consteval_p = false;
17142 : }
17143 :
17144 : /* Record whether the function is public. */
17145 82432760 : publicp = (ctype != NULL_TREE
17146 82432760 : || storage_class != sc_static);
17147 :
17148 164865520 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
17149 : declspecs, reqs, virtualp, flags, memfn_quals, rqual, raises,
17150 : 1, friendp,
17151 : publicp,
17152 82432760 : inlinep | (2 * constexpr_p) | (4 * concept_p)
17153 82432760 : | (8 * consteval_p),
17154 : initialized,
17155 : is_xobj_member_function, sfk,
17156 : funcdef_flag,
17157 : late_return_type_p,
17158 : template_count, in_namespace, attrlist,
17159 : contract_specifiers, id_loc);
17160 82432760 : if (decl == NULL_TREE)
17161 619 : return error_mark_node;
17162 :
17163 82432141 : if (explicitp == 2)
17164 11442 : DECL_NONCONVERTING_P (decl) = 1;
17165 82432141 : if (staticp == 1)
17166 : {
17167 118603 : int invalid_static = 0;
17168 :
17169 : /* Don't allow a static member function in a class, and forbid
17170 : declaring main to be static. */
17171 118603 : if (TREE_CODE (type) == METHOD_TYPE)
17172 : {
17173 0 : permerror (input_location, "cannot declare member function %qD to have "
17174 : "static linkage", decl);
17175 0 : invalid_static = 1;
17176 : }
17177 118603 : else if (current_function_decl)
17178 : {
17179 : /* 7.1.1: There can be no static function declarations within a
17180 : block. */
17181 9 : error_at (declspecs->locations[ds_storage_class],
17182 : "cannot declare static function inside another function");
17183 9 : invalid_static = 1;
17184 : }
17185 :
17186 9 : if (invalid_static)
17187 : {
17188 82432141 : staticp = 0;
17189 : storage_class = sc_none;
17190 : }
17191 : }
17192 82432141 : if (declspecs->explicit_specifier)
17193 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
17194 : }
17195 : else
17196 : {
17197 : /* It's a variable. */
17198 :
17199 : /* An uninitialized decl with `extern' is a reference. */
17200 143619708 : decl = grokvardecl (type, dname, unqualified_id,
17201 : declspecs,
17202 : initialized,
17203 : type_quals,
17204 : inlinep,
17205 : concept_p,
17206 : template_count,
17207 : ctype ? ctype : in_namespace,
17208 : id_loc);
17209 72071821 : if (decl == NULL_TREE)
17210 92 : return error_mark_node;
17211 :
17212 72071729 : bad_specifiers (decl, BSP_VAR, virtualp,
17213 : memfn_quals != TYPE_UNQUALIFIED,
17214 : inlinep, friendp, raises != NULL_TREE,
17215 : declspecs->locations);
17216 :
17217 72071729 : if (ctype)
17218 : {
17219 523928 : DECL_CONTEXT (decl) = ctype;
17220 523928 : if (staticp == 1)
17221 : {
17222 6 : permerror (declspecs->locations[ds_storage_class],
17223 : "%<static%> may not be used when defining "
17224 : "(as opposed to declaring) a static data member");
17225 6 : staticp = 0;
17226 6 : storage_class = sc_none;
17227 : }
17228 523928 : if (storage_class == sc_register && TREE_STATIC (decl))
17229 : {
17230 0 : error ("static member %qD declared %<register%>", decl);
17231 0 : storage_class = sc_none;
17232 : }
17233 523928 : if (storage_class == sc_extern && pedantic)
17234 : {
17235 0 : pedwarn (input_location, OPT_Wpedantic,
17236 : "cannot explicitly declare member %q#D to have "
17237 : "extern linkage", decl);
17238 0 : storage_class = sc_none;
17239 : }
17240 : }
17241 71547801 : else if (constexpr_p && DECL_EXTERNAL (decl))
17242 : {
17243 14 : error_at (DECL_SOURCE_LOCATION (decl),
17244 : "declaration of %<constexpr%> variable %qD "
17245 : "is not a definition", decl);
17246 14 : constexpr_p = false;
17247 : }
17248 72071729 : if (consteval_p)
17249 : {
17250 13 : error_at (DECL_SOURCE_LOCATION (decl),
17251 : "a variable cannot be declared %<consteval%>");
17252 13 : consteval_p = false;
17253 : }
17254 :
17255 72071729 : if (inlinep)
17256 7010796 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
17257 72071729 : if (innermost_code == cdk_decomp)
17258 : {
17259 248320 : gcc_assert (declarator && declarator->kind == cdk_decomp);
17260 248320 : DECL_SOURCE_LOCATION (decl) = id_loc;
17261 248320 : DECL_ARTIFICIAL (decl) = 1;
17262 248320 : fit_decomposition_lang_decl (decl, NULL_TREE);
17263 : }
17264 : }
17265 :
17266 607016561 : if (VAR_P (decl) && !initialized)
17267 7737650 : if (tree auto_node = type_uses_auto (type))
17268 104 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
17269 : {
17270 21 : location_t loc = declspecs->locations[ds_type_spec];
17271 21 : error_at (loc, "declaration of %q#D has no initializer", decl);
17272 21 : TREE_TYPE (decl) = error_mark_node;
17273 : }
17274 :
17275 607016561 : if (storage_class == sc_extern && initialized && !funcdef_flag)
17276 : {
17277 276 : if (toplevel_bindings_p ())
17278 : {
17279 : /* It's common practice (and completely valid) to have a const
17280 : be initialized and declared extern. */
17281 252 : if (!(type_quals & TYPE_QUAL_CONST))
17282 19 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
17283 : "%qs initialized and declared %<extern%>", name);
17284 : }
17285 : else
17286 : {
17287 24 : error_at (DECL_SOURCE_LOCATION (decl),
17288 : "%qs has both %<extern%> and initializer", name);
17289 24 : return error_mark_node;
17290 : }
17291 : }
17292 :
17293 : /* Record `register' declaration for warnings on &
17294 : and in case doing stupid register allocation. */
17295 :
17296 607016537 : if (storage_class == sc_register)
17297 : {
17298 2069 : DECL_REGISTER (decl) = 1;
17299 : /* Warn about register storage specifiers on PARM_DECLs. */
17300 2069 : if (TREE_CODE (decl) == PARM_DECL)
17301 : {
17302 35 : if (cxx_dialect >= cxx17)
17303 24 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
17304 : "ISO C++17 does not allow %<register%> storage "
17305 : "class specifier");
17306 : else
17307 11 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
17308 : "%<register%> storage class specifier used");
17309 : }
17310 : }
17311 607014468 : else if (storage_class == sc_extern)
17312 32324017 : DECL_THIS_EXTERN (decl) = 1;
17313 574690451 : else if (storage_class == sc_static)
17314 25180256 : DECL_THIS_STATIC (decl) = 1;
17315 :
17316 607016537 : if (VAR_P (decl))
17317 : {
17318 : /* Set constexpr flag on vars (functions got it in grokfndecl). */
17319 85901397 : if (constexpr_p)
17320 20936916 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
17321 : /* And the constinit flag (which only applies to variables). */
17322 64964481 : else if (constinit_p)
17323 453 : DECL_DECLARED_CONSTINIT_P (decl) = true;
17324 : }
17325 521115140 : else if (TREE_CODE (decl) == FUNCTION_DECL)
17326 : {
17327 : /* If we saw a return type, record its location. */
17328 190927466 : location_t loc = declspecs->locations[ds_type_spec];
17329 190927466 : if (loc == UNKNOWN_LOCATION)
17330 : /* Build DECL_RESULT in start_preparsed_function. */;
17331 156197794 : else if (!DECL_RESULT (decl))
17332 : {
17333 156156889 : tree restype = TREE_TYPE (TREE_TYPE (decl));
17334 156156889 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
17335 156156889 : DECL_ARTIFICIAL (resdecl) = 1;
17336 156156889 : DECL_IGNORED_P (resdecl) = 1;
17337 156156889 : DECL_RESULT (decl) = resdecl;
17338 : }
17339 40905 : else if (funcdef_flag)
17340 869 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
17341 : }
17342 :
17343 : /* Record constancy and volatility on the DECL itself . There's
17344 : no need to do this when processing a template; we'll do this
17345 : for the instantiated declaration based on the type of DECL. */
17346 607016537 : if (!processing_template_decl
17347 : /* Don't do it for instantiated variable templates either,
17348 : cp_apply_type_quals_to_decl should have been called on it
17349 : already and might have been overridden in cp_finish_decl
17350 : if initializer needs runtime initialization. */
17351 607016537 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
17352 202729189 : cp_apply_type_quals_to_decl (type_quals, decl);
17353 :
17354 607016537 : return decl;
17355 : }
17356 1093941014 : }
17357 :
17358 : /* Subroutine of start_function. Ensure that each of the parameter
17359 : types (as listed in PARMS) is complete, as is required for a
17360 : function definition. */
17361 :
17362 : static void
17363 169657740 : require_complete_types_for_parms (tree parms)
17364 : {
17365 502243152 : for (; parms; parms = DECL_CHAIN (parms))
17366 : {
17367 332585412 : if (dependent_type_p (TREE_TYPE (parms)))
17368 186664613 : continue;
17369 145920799 : if (!VOID_TYPE_P (TREE_TYPE (parms))
17370 145920799 : && complete_type_or_else (TREE_TYPE (parms), parms))
17371 : {
17372 145920718 : relayout_decl (parms);
17373 145920718 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
17374 :
17375 145920718 : abstract_virtuals_error (parms, TREE_TYPE (parms));
17376 145920718 : maybe_warn_parm_abi (TREE_TYPE (parms),
17377 145920718 : DECL_SOURCE_LOCATION (parms));
17378 : }
17379 : else
17380 : /* grokparms or complete_type_or_else will have already issued
17381 : an error. */
17382 81 : TREE_TYPE (parms) = error_mark_node;
17383 : }
17384 169657740 : }
17385 :
17386 : /* Returns nonzero if T is a local variable. */
17387 :
17388 : int
17389 181940137 : local_variable_p (const_tree t)
17390 : {
17391 181940137 : if ((VAR_P (t)
17392 70319981 : && (DECL_LOCAL_DECL_P (t)
17393 70319981 : || !DECL_CONTEXT (t)
17394 70276274 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
17395 187856400 : || (TREE_CODE (t) == PARM_DECL))
17396 161859369 : return 1;
17397 :
17398 : return 0;
17399 : }
17400 :
17401 : /* Like local_variable_p, but suitable for use as a tree-walking
17402 : function. */
17403 :
17404 : static tree
17405 5772024 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
17406 : void * /*data*/)
17407 : {
17408 5772024 : if (unevaluated_p (TREE_CODE (*tp)))
17409 : {
17410 : /* DR 2082 permits local variables in unevaluated contexts
17411 : within a default argument. */
17412 70 : *walk_subtrees = 0;
17413 70 : return NULL_TREE;
17414 : }
17415 :
17416 5771954 : if (local_variable_p (*tp)
17417 5771954 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
17418 : return *tp;
17419 5771954 : else if (TYPE_P (*tp))
17420 30 : *walk_subtrees = 0;
17421 :
17422 : return NULL_TREE;
17423 : }
17424 :
17425 : /* Check that ARG, which is a default-argument expression for a
17426 : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
17427 : something goes wrong. DECL may also be a _TYPE node, rather than a
17428 : DECL, if there is no DECL available. */
17429 :
17430 : tree
17431 2851148 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
17432 : {
17433 2851148 : tree var;
17434 2851148 : tree decl_type;
17435 :
17436 2851148 : if (TREE_CODE (arg) == DEFERRED_PARSE)
17437 : /* We get a DEFERRED_PARSE when looking at an in-class declaration
17438 : with a default argument. Ignore the argument for now; we'll
17439 : deal with it after the class is complete. */
17440 : return arg;
17441 :
17442 2020479 : if (TYPE_P (decl))
17443 : {
17444 : decl_type = decl;
17445 : decl = NULL_TREE;
17446 : }
17447 : else
17448 615561 : decl_type = TREE_TYPE (decl);
17449 :
17450 2020479 : if (arg == error_mark_node
17451 2020338 : || decl == error_mark_node
17452 2020329 : || TREE_TYPE (arg) == error_mark_node
17453 4040808 : || decl_type == error_mark_node)
17454 : /* Something already went wrong. There's no need to check
17455 : further. */
17456 : return error_mark_node;
17457 :
17458 : /* [dcl.fct.default]
17459 :
17460 : A default argument expression is implicitly converted to the
17461 : parameter type. */
17462 2020329 : ++cp_unevaluated_operand;
17463 : /* Avoid digest_init clobbering the initializer. */
17464 2020329 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
17465 2020329 : perform_implicit_conversion_flags (decl_type, carg, complain,
17466 : LOOKUP_IMPLICIT);
17467 2020329 : --cp_unevaluated_operand;
17468 :
17469 : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
17470 : the call sites. */
17471 1606540 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17472 413804 : && null_ptr_cst_p (arg)
17473 : /* Don't lose side-effects as in PR90473. */
17474 2399103 : && !TREE_SIDE_EFFECTS (arg))
17475 378771 : return nullptr_node;
17476 :
17477 : /* [dcl.fct.default]
17478 :
17479 : Local variables shall not be used in default argument
17480 : expressions.
17481 :
17482 : The keyword `this' shall not be used in a default argument of a
17483 : member function. */
17484 1641558 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17485 1641558 : if (var)
17486 : {
17487 0 : if (complain & tf_warning_or_error)
17488 : {
17489 0 : if (DECL_NAME (var) == this_identifier)
17490 0 : permerror (input_location, "default argument %qE uses %qD",
17491 : arg, var);
17492 : else
17493 0 : error ("default argument %qE uses local variable %qD", arg, var);
17494 : }
17495 0 : return error_mark_node;
17496 : }
17497 :
17498 : /* All is well. */
17499 1641558 : return arg;
17500 : }
17501 :
17502 : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17503 :
17504 : static tree
17505 295709706 : type_is_deprecated (tree type)
17506 : {
17507 435784574 : enum tree_code code;
17508 435784574 : if (TREE_DEPRECATED (type))
17509 : return type;
17510 435742845 : if (TYPE_NAME (type))
17511 : {
17512 291239762 : if (TREE_DEPRECATED (TYPE_NAME (type)))
17513 : return type;
17514 : else
17515 : {
17516 291239756 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17517 291239756 : return NULL_TREE;
17518 : }
17519 : }
17520 :
17521 : /* Do warn about using typedefs to a deprecated class. */
17522 144503083 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17523 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17524 :
17525 144503083 : code = TREE_CODE (type);
17526 :
17527 144503083 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17528 144503083 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17529 : || code == METHOD_TYPE || code == ARRAY_TYPE)
17530 139951216 : return type_is_deprecated (TREE_TYPE (type));
17531 :
17532 123652 : if (TYPE_PTRMEMFUNC_P (type))
17533 123652 : return type_is_deprecated
17534 123652 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17535 :
17536 : return NULL_TREE;
17537 : }
17538 :
17539 : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17540 :
17541 : static tree
17542 296970707 : type_is_unavailable (tree type)
17543 : {
17544 437799907 : enum tree_code code;
17545 437799907 : if (TREE_UNAVAILABLE (type))
17546 : return type;
17547 437799901 : if (TYPE_NAME (type))
17548 : {
17549 292542429 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17550 : return type;
17551 : else
17552 : {
17553 292542423 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17554 292542423 : return NULL_TREE;
17555 : }
17556 : }
17557 :
17558 : /* Do warn about using typedefs to a deprecated class. */
17559 145257472 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17560 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17561 :
17562 145257472 : code = TREE_CODE (type);
17563 :
17564 145257472 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17565 145257472 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17566 : || code == METHOD_TYPE || code == ARRAY_TYPE)
17567 140589164 : return type_is_unavailable (TREE_TYPE (type));
17568 :
17569 240036 : if (TYPE_PTRMEMFUNC_P (type))
17570 240036 : return type_is_unavailable
17571 240036 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17572 :
17573 : return NULL_TREE;
17574 : }
17575 :
17576 : /* Decode the list of parameter types for a function type.
17577 : Given the list of things declared inside the parens,
17578 : return a list of types.
17579 :
17580 : If this parameter does not end with an ellipsis, we append
17581 : void_list_node.
17582 :
17583 : *PARMS is set to the chain of PARM_DECLs created. */
17584 :
17585 : tree
17586 203393686 : grokparms (tree parmlist, tree *parms)
17587 : {
17588 203393686 : tree result = NULL_TREE;
17589 203393686 : tree decls = NULL_TREE;
17590 203393686 : tree parm;
17591 203393686 : int any_error = 0;
17592 :
17593 501018105 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17594 : {
17595 498419017 : tree type = NULL_TREE;
17596 498419017 : tree init = TREE_PURPOSE (parm);
17597 498419017 : tree decl = TREE_VALUE (parm);
17598 :
17599 498419017 : if (parm == void_list_node || parm == explicit_void_list_node)
17600 : break;
17601 :
17602 297624435 : if (! decl || TREE_TYPE (decl) == error_mark_node)
17603 : {
17604 198 : any_error = 1;
17605 198 : continue;
17606 : }
17607 :
17608 297624237 : type = TREE_TYPE (decl);
17609 297624237 : if (VOID_TYPE_P (type))
17610 : {
17611 82 : if (same_type_p (type, void_type_node)
17612 73 : && !init
17613 61 : && !DECL_NAME (decl) && !result
17614 110 : && TREE_CHAIN (parm) == void_list_node)
17615 : /* DR 577: A parameter list consisting of a single
17616 : unnamed parameter of non-dependent type 'void'. */
17617 : break;
17618 66 : else if (cv_qualified_p (type))
17619 9 : error_at (DECL_SOURCE_LOCATION (decl),
17620 : "invalid use of cv-qualified type %qT in "
17621 : "parameter declaration", type);
17622 : else
17623 57 : error_at (DECL_SOURCE_LOCATION (decl),
17624 : "invalid use of type %<void%> in parameter "
17625 : "declaration");
17626 : /* It's not a good idea to actually create parameters of
17627 : type `void'; other parts of the compiler assume that a
17628 : void type terminates the parameter list. */
17629 66 : type = error_mark_node;
17630 66 : TREE_TYPE (decl) = error_mark_node;
17631 : }
17632 :
17633 297624221 : if (type != error_mark_node)
17634 : {
17635 297624155 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17636 : {
17637 296970707 : tree unavailtype = type_is_unavailable (type);
17638 296970707 : if (unavailtype)
17639 12 : cp_handle_deprecated_or_unavailable (unavailtype);
17640 : }
17641 297624155 : if (deprecated_state != DEPRECATED_SUPPRESS
17642 297624155 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17643 : {
17644 295709706 : tree deptype = type_is_deprecated (type);
17645 295709706 : if (deptype)
17646 41735 : cp_handle_deprecated_or_unavailable (deptype);
17647 : }
17648 :
17649 : /* [dcl.fct] "A parameter with volatile-qualified type is
17650 : deprecated." */
17651 297624155 : if (CP_TYPE_VOLATILE_P (type))
17652 564 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17653 : "%<volatile%>-qualified parameter is "
17654 : "deprecated");
17655 :
17656 : /* Top-level qualifiers on the parameters are
17657 : ignored for function types. */
17658 297624155 : type = cp_build_qualified_type (type, 0);
17659 297624155 : if (TREE_CODE (type) == METHOD_TYPE)
17660 : {
17661 0 : error ("parameter %qD invalidly declared method type", decl);
17662 0 : type = build_pointer_type (type);
17663 0 : TREE_TYPE (decl) = type;
17664 : }
17665 297624155 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17666 : {
17667 : /* Before C++17 DR 393:
17668 : [dcl.fct]/6, parameter types cannot contain pointers
17669 : (references) to arrays of unknown bound. */
17670 1769091 : tree t = TREE_TYPE (type);
17671 1769091 : int ptr = TYPE_PTR_P (type);
17672 :
17673 1938187 : while (1)
17674 : {
17675 1853639 : if (TYPE_PTR_P (t))
17676 : ptr = 1;
17677 1772381 : else if (TREE_CODE (t) != ARRAY_TYPE)
17678 : break;
17679 3300 : else if (!TYPE_DOMAIN (t))
17680 : break;
17681 84548 : t = TREE_TYPE (t);
17682 : }
17683 1769091 : if (TREE_CODE (t) == ARRAY_TYPE)
17684 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17685 : ptr
17686 : ? G_("parameter %qD includes pointer to array of "
17687 : "unknown bound %qT")
17688 : : G_("parameter %qD includes reference to array of "
17689 : "unknown bound %qT"),
17690 : decl, t);
17691 : }
17692 :
17693 297624155 : if (init && !processing_template_decl)
17694 1446221 : init = check_default_argument (decl, init, tf_warning_or_error);
17695 : }
17696 :
17697 297624221 : DECL_CHAIN (decl) = decls;
17698 297624221 : decls = decl;
17699 297624221 : result = tree_cons (init, type, result);
17700 : }
17701 203393686 : decls = nreverse (decls);
17702 203393686 : result = nreverse (result);
17703 203393686 : if (parm)
17704 200794598 : result = chainon (result, void_list_node);
17705 203393686 : *parms = decls;
17706 203393686 : if (any_error)
17707 183 : result = NULL_TREE;
17708 :
17709 183 : if (any_error)
17710 : /* We had parm errors, recover by giving the function (...) type. */
17711 183 : result = NULL_TREE;
17712 :
17713 203393686 : return result;
17714 : }
17715 :
17716 :
17717 : /* D is a constructor or overloaded `operator='.
17718 :
17719 : Let T be the class in which D is declared. Then, this function
17720 : returns:
17721 :
17722 : -1 if D's is an ill-formed constructor or copy assignment operator
17723 : whose first parameter is of type `T'.
17724 : 0 if D is not a copy constructor or copy assignment
17725 : operator.
17726 : 1 if D is a copy constructor or copy assignment operator whose
17727 : first parameter is a reference to non-const qualified T.
17728 : 2 if D is a copy constructor or copy assignment operator whose
17729 : first parameter is a reference to const qualified T.
17730 :
17731 : This function can be used as a predicate. Positive values indicate
17732 : a copy constructor and nonzero values indicate a copy assignment
17733 : operator. */
17734 :
17735 : int
17736 357989674 : copy_fn_p (const_tree d)
17737 : {
17738 357989674 : tree args;
17739 357989674 : tree arg_type;
17740 357989674 : int result = 1;
17741 :
17742 357989674 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17743 :
17744 357989674 : if (TREE_CODE (d) == TEMPLATE_DECL
17745 357989674 : || (DECL_TEMPLATE_INFO (d)
17746 158620661 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17747 : /* Instantiations of template member functions are never copy
17748 : functions. Note that member functions of templated classes are
17749 : represented as template functions internally, and we must
17750 : accept those as copy functions. */
17751 : return 0;
17752 :
17753 302182214 : if (!DECL_CONSTRUCTOR_P (d)
17754 302182214 : && DECL_NAME (d) != assign_op_identifier)
17755 : return 0;
17756 :
17757 301347844 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17758 : {
17759 282 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17760 282 : if (!TYPE_REF_P (object_param)
17761 255 : || TYPE_REF_IS_RVALUE (object_param)
17762 : /* Reject unrelated object parameters. */
17763 237 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17764 519 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17765 : return 0;
17766 228 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17767 : }
17768 : else
17769 301347562 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17770 301347790 : if (!args)
17771 : return 0;
17772 :
17773 301339941 : arg_type = TREE_VALUE (args);
17774 301339941 : if (arg_type == error_mark_node)
17775 : return 0;
17776 :
17777 301339906 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17778 : {
17779 : /* Pass by value copy assignment operator. */
17780 : result = -1;
17781 : }
17782 301338355 : else if (TYPE_REF_P (arg_type)
17783 190909009 : && !TYPE_REF_IS_RVALUE (arg_type)
17784 422242630 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17785 : {
17786 97450469 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17787 97278447 : result = 2;
17788 : }
17789 : else
17790 : return 0;
17791 :
17792 97452020 : args = TREE_CHAIN (args);
17793 :
17794 100415400 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17795 : /* There are more non-optional args. */
17796 2962866 : return 0;
17797 :
17798 : return result;
17799 : }
17800 :
17801 : /* D is a constructor or overloaded `operator='.
17802 :
17803 : Let T be the class in which D is declared. Then, this function
17804 : returns true when D is a move constructor or move assignment
17805 : operator, false otherwise. */
17806 :
17807 : bool
17808 166235198 : move_fn_p (const_tree d)
17809 : {
17810 166235198 : if (cxx_dialect == cxx98)
17811 : /* There are no move constructors if we are in C++98 mode. */
17812 : return false;
17813 :
17814 166158502 : if (TREE_CODE (d) == TEMPLATE_DECL
17815 166158502 : || (DECL_TEMPLATE_INFO (d)
17816 67150019 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17817 : /* Instantiations of template member functions are never move
17818 : functions. Note that member functions of templated classes are
17819 : represented as template functions internally, and we must
17820 : accept those as move functions. */
17821 : return 0;
17822 :
17823 134872503 : return move_signature_fn_p (d);
17824 : }
17825 :
17826 : /* D is a constructor or overloaded `operator='.
17827 :
17828 : Then, this function returns true when D has the same signature as a move
17829 : constructor or move assignment operator (because either it is such a
17830 : ctor/op= or it is a template specialization with the same signature),
17831 : false otherwise. */
17832 :
17833 : bool
17834 134872503 : move_signature_fn_p (const_tree d)
17835 : {
17836 134872503 : tree args;
17837 134872503 : tree arg_type;
17838 134872503 : bool result = false;
17839 :
17840 134872503 : if (!DECL_CONSTRUCTOR_P (d)
17841 134872503 : && DECL_NAME (d) != assign_op_identifier)
17842 : return 0;
17843 :
17844 134872277 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17845 : {
17846 126 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17847 126 : if (!TYPE_REF_P (object_param)
17848 102 : || TYPE_REF_IS_RVALUE (object_param)
17849 : /* Reject unrelated object parameters. */
17850 90 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17851 216 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17852 : return 0;
17853 84 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17854 : }
17855 : else
17856 134872151 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17857 134872235 : if (!args)
17858 : return 0;
17859 :
17860 134869251 : arg_type = TREE_VALUE (args);
17861 134869251 : if (arg_type == error_mark_node)
17862 : return 0;
17863 :
17864 134869243 : if (TYPE_REF_P (arg_type)
17865 68602151 : && TYPE_REF_IS_RVALUE (arg_type)
17866 188639905 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17867 : DECL_CONTEXT (d)))
17868 : result = true;
17869 :
17870 134869243 : args = TREE_CHAIN (args);
17871 :
17872 150671214 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17873 : /* There are more non-optional args. */
17874 12550291 : return false;
17875 :
17876 : return result;
17877 : }
17878 :
17879 : /* Remember any special properties of member function DECL. */
17880 :
17881 : void
17882 368126825 : grok_special_member_properties (tree decl)
17883 : {
17884 368126825 : tree class_type;
17885 :
17886 368126825 : if (TREE_CODE (decl) == USING_DECL
17887 368126825 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17888 : return;
17889 :
17890 345034450 : class_type = DECL_CONTEXT (decl);
17891 345034450 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17892 : {
17893 155779683 : int ctor = copy_fn_p (decl);
17894 :
17895 155779683 : if (!DECL_ARTIFICIAL (decl))
17896 108088167 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17897 :
17898 155779683 : if (ctor > 0)
17899 : {
17900 : /* [class.copy]
17901 :
17902 : A non-template constructor for class X is a copy
17903 : constructor if its first parameter is of type X&, const
17904 : X&, volatile X& or const volatile X&, and either there
17905 : are no other parameters or else all other parameters have
17906 : default arguments. */
17907 31759990 : TYPE_HAS_COPY_CTOR (class_type) = 1;
17908 31759990 : if (ctor > 1)
17909 31680217 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17910 : }
17911 :
17912 155779683 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17913 33247501 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17914 :
17915 155779683 : if (is_list_ctor (decl))
17916 1585047 : TYPE_HAS_LIST_CTOR (class_type) = 1;
17917 :
17918 155779683 : if (maybe_constexpr_fn (decl)
17919 155779683 : && !ctor && !move_fn_p (decl))
17920 56859050 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17921 : }
17922 189254767 : else if (DECL_NAME (decl) == assign_op_identifier)
17923 : {
17924 : /* [class.copy]
17925 :
17926 : A non-template assignment operator for class X is a copy
17927 : assignment operator if its parameter is of type X, X&, const
17928 : X&, volatile X& or const volatile X&. */
17929 :
17930 15731345 : int assop = copy_fn_p (decl);
17931 :
17932 15731345 : if (assop)
17933 : {
17934 8001598 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17935 8001598 : if (assop != 1)
17936 7972598 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17937 : }
17938 : }
17939 173523422 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17940 2839517 : TYPE_HAS_CONVERSION (class_type) = true;
17941 :
17942 : /* Destructors are handled in check_methods. */
17943 : }
17944 :
17945 : /* Check a constructor DECL has the correct form. Complains
17946 : if the class has a constructor of the form X(X). */
17947 :
17948 : bool
17949 50049916 : grok_ctor_properties (const_tree ctype, const_tree decl)
17950 : {
17951 50049916 : int ctor_parm = copy_fn_p (decl);
17952 :
17953 50049916 : if (ctor_parm < 0)
17954 : {
17955 : /* [class.copy]
17956 :
17957 : A declaration of a constructor for a class X is ill-formed if
17958 : its first parameter is of type (optionally cv-qualified) X
17959 : and either there are no other parameters or else all other
17960 : parameters have default arguments.
17961 :
17962 : We *don't* complain about member template instantiations that
17963 : have this form, though; they can occur as we try to decide
17964 : what constructor to use during overload resolution. Since
17965 : overload resolution will never prefer such a constructor to
17966 : the non-template copy constructor (which is either explicitly
17967 : or implicitly defined), there's no need to worry about their
17968 : existence. Theoretically, they should never even be
17969 : instantiated, but that's hard to forestall. */
17970 9 : error_at (DECL_SOURCE_LOCATION (decl),
17971 : "invalid constructor; you probably meant %<%T (const %T&)%>",
17972 : ctype, ctype);
17973 9 : return false;
17974 : }
17975 :
17976 : return true;
17977 : }
17978 :
17979 : /* DECL is a declaration for an overloaded or conversion operator. If
17980 : COMPLAIN is true, errors are issued for invalid declarations. */
17981 :
17982 : bool
17983 62365712 : grok_op_properties (tree decl, bool complain)
17984 : {
17985 62365712 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17986 62365712 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17987 62365712 : tree name = DECL_NAME (decl);
17988 62365712 : location_t loc = DECL_SOURCE_LOCATION (decl);
17989 :
17990 62365712 : tree class_type = DECL_CONTEXT (decl);
17991 62365712 : if (class_type && !CLASS_TYPE_P (class_type))
17992 : class_type = NULL_TREE;
17993 :
17994 62365712 : tree_code operator_code;
17995 62365712 : unsigned op_flags;
17996 62365712 : if (IDENTIFIER_CONV_OP_P (name))
17997 : {
17998 : /* Conversion operators are TYPE_EXPR for the purposes of this
17999 : function. */
18000 : operator_code = TYPE_EXPR;
18001 : op_flags = OVL_OP_FLAG_UNARY;
18002 : }
18003 : else
18004 : {
18005 59923876 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
18006 :
18007 59923876 : operator_code = ovl_op->tree_code;
18008 59923876 : op_flags = ovl_op->flags;
18009 59923876 : gcc_checking_assert (operator_code != ERROR_MARK);
18010 59923876 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
18011 : }
18012 :
18013 59923876 : if (op_flags & OVL_OP_FLAG_ALLOC)
18014 : {
18015 : /* operator new and operator delete are quite special. */
18016 363582 : if (class_type)
18017 9858 : switch (op_flags)
18018 : {
18019 6749 : case OVL_OP_FLAG_ALLOC:
18020 6749 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
18021 6749 : break;
18022 :
18023 2793 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
18024 2793 : TYPE_GETS_DELETE (class_type) |= 1;
18025 2793 : break;
18026 :
18027 135 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
18028 135 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
18029 135 : break;
18030 :
18031 181 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
18032 181 : TYPE_GETS_DELETE (class_type) |= 2;
18033 181 : break;
18034 :
18035 0 : default:
18036 0 : gcc_unreachable ();
18037 : }
18038 :
18039 : /* [basic.std.dynamic.allocation]/1:
18040 :
18041 : A program is ill-formed if an allocation function is declared
18042 : in a namespace scope other than global scope or declared
18043 : static in global scope.
18044 :
18045 : The same also holds true for deallocation functions. */
18046 363582 : if (DECL_NAMESPACE_SCOPE_P (decl))
18047 : {
18048 353724 : if (CP_DECL_CONTEXT (decl) != global_namespace)
18049 : {
18050 6 : error_at (loc, "%qD may not be declared within a namespace",
18051 : decl);
18052 6 : return false;
18053 : }
18054 :
18055 353718 : if (!TREE_PUBLIC (decl))
18056 : {
18057 6 : error_at (loc, "%qD may not be declared as static", decl);
18058 6 : return false;
18059 : }
18060 : }
18061 :
18062 : /* Check for replaceable global new/delete operators with
18063 : const std::nothrow_t & last argument, other replaceable global
18064 : new/delete operators are marked in cxx_init_decl_processing. */
18065 363570 : if (CP_DECL_CONTEXT (decl) == global_namespace)
18066 : {
18067 353712 : tree args = argtypes;
18068 353712 : if (args
18069 353700 : && args != void_list_node
18070 1061094 : && same_type_p (TREE_VALUE (args),
18071 : (op_flags & OVL_OP_FLAG_DELETE)
18072 : ? ptr_type_node : size_type_node))
18073 : {
18074 353670 : args = TREE_CHAIN (args);
18075 353670 : if (aligned_allocation_fn_p (decl))
18076 114044 : args = TREE_CHAIN (args);
18077 353670 : if (args
18078 353664 : && args != void_list_node
18079 589777 : && TREE_CHAIN (args) == void_list_node)
18080 : {
18081 207441 : tree t = TREE_VALUE (args);
18082 207441 : if (TYPE_REF_P (t)
18083 117705 : && !TYPE_REF_IS_RVALUE (t)
18084 117705 : && (t = TREE_TYPE (t))
18085 117705 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
18086 324648 : && is_std_class (t, "nothrow_t"))
18087 117207 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
18088 : }
18089 : }
18090 : }
18091 :
18092 363570 : if (op_flags & OVL_OP_FLAG_DELETE)
18093 : {
18094 207790 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
18095 207790 : coerce_delete_type (decl, loc);
18096 : }
18097 : else
18098 : {
18099 155780 : DECL_SET_IS_OPERATOR_NEW (decl, true);
18100 155780 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
18101 : }
18102 :
18103 : return true;
18104 : }
18105 :
18106 : /* An operator function must either be a non-static member function
18107 : or have at least one parameter of a class, a reference to a class,
18108 : an enumeration, or a reference to an enumeration. 13.4.0.6 */
18109 62002130 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
18110 : {
18111 15936453 : if (operator_code == TYPE_EXPR
18112 15936453 : || operator_code == COMPONENT_REF
18113 15936432 : || operator_code == NOP_EXPR)
18114 : {
18115 33 : error_at (loc, "%qD must be a non-static member function", decl);
18116 33 : return false;
18117 : }
18118 :
18119 15936420 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
18120 : {
18121 142140 : if (! DECL_STATIC_FUNCTION_P (decl))
18122 : {
18123 12 : error_at (loc, "%qD must be a member function", decl);
18124 12 : return false;
18125 : }
18126 142128 : if (cxx_dialect < cxx23
18127 : /* For lambdas we diagnose static lambda specifier elsewhere. */
18128 217814 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
18129 : /* For instantiations, we have diagnosed this already. */
18130 251021 : && ! DECL_USE_TEMPLATE (decl))
18131 108887 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
18132 : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
18133 : decl);
18134 142128 : if (operator_code == ARRAY_REF)
18135 : /* static operator[] should have exactly one argument
18136 : for C++20 and earlier, so that it isn't multidimensional. */
18137 : op_flags = OVL_OP_FLAG_UNARY;
18138 : }
18139 15794280 : else if (DECL_STATIC_FUNCTION_P (decl))
18140 : {
18141 9 : error_at (loc, "%qD must be either a non-static member "
18142 : "function or a non-member function", decl);
18143 9 : return false;
18144 : }
18145 : else
18146 424922 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
18147 : {
18148 16219193 : if (!arg || arg == void_list_node)
18149 : {
18150 18 : if (complain)
18151 15 : error_at (loc, "%qD must have an argument of class or "
18152 : "enumerated type", decl);
18153 : return false;
18154 : }
18155 :
18156 16219175 : tree type = non_reference (TREE_VALUE (arg));
18157 16219175 : if (type == error_mark_node)
18158 : return false;
18159 :
18160 : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
18161 : because these checks are performed even on template
18162 : functions. */
18163 16219172 : if (MAYBE_CLASS_TYPE_P (type)
18164 1458095 : || TREE_CODE (type) == ENUMERAL_TYPE)
18165 : break;
18166 424922 : }
18167 : }
18168 :
18169 61998730 : if (operator_code == CALL_EXPR)
18170 : /* There are no further restrictions on the arguments to an overloaded
18171 : "operator ()". */
18172 : return true;
18173 :
18174 : /* C++23 allows an arbitrary number of parameters and default arguments for
18175 : operator[], and none of the other checks below apply. */
18176 51611647 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
18177 : return true;
18178 :
18179 51292082 : if (operator_code == COND_EXPR)
18180 : {
18181 : /* 13.4.0.3 */
18182 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
18183 6 : return false;
18184 : }
18185 :
18186 : /* Count the number of arguments and check for ellipsis. */
18187 : int arity = 0;
18188 145772080 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
18189 : {
18190 94480019 : if (!arg)
18191 : {
18192 15 : error_at (loc, "%qD must not have variable number of arguments",
18193 : decl);
18194 15 : return false;
18195 : }
18196 94480004 : ++arity;
18197 : }
18198 : /* FIXME: We need tests for these errors with xobj member functions. */
18199 : /* Verify correct number of arguments. */
18200 51292061 : switch (op_flags)
18201 : {
18202 10652831 : case OVL_OP_FLAG_AMBIARY:
18203 10652831 : if (arity == 1)
18204 : {
18205 : /* We have a unary instance of an ambi-ary op. Remap to the
18206 : unary one. */
18207 4587521 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
18208 4587521 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
18209 4587521 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
18210 4587521 : operator_code = ovl_op->tree_code;
18211 4587521 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
18212 : }
18213 6065310 : else if (arity != 2)
18214 : {
18215 : /* This was an ambiguous operator but is invalid. */
18216 3 : error_at (loc,
18217 : methodp
18218 : ? G_("%qD must have either zero or one argument")
18219 : : G_("%qD must have either one or two arguments"), decl);
18220 3 : return false;
18221 : }
18222 6065307 : else if ((operator_code == POSTINCREMENT_EXPR
18223 6065307 : || operator_code == POSTDECREMENT_EXPR)
18224 2813049 : && ! processing_template_decl
18225 : /* x++ and x--'s second argument must be an int. */
18226 7888850 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
18227 : integer_type_node))
18228 : {
18229 15 : error_at (loc,
18230 : methodp
18231 : ? G_("postfix %qD must have %<int%> as its argument")
18232 : : G_("postfix %qD must have %<int%> as its second argument"),
18233 : decl);
18234 15 : return false;
18235 : }
18236 : break;
18237 :
18238 3516627 : case OVL_OP_FLAG_UNARY:
18239 3516627 : if (arity != 1)
18240 : {
18241 9 : error_at (loc,
18242 : methodp
18243 : ? G_("%qD must have no arguments")
18244 : : G_("%qD must have exactly one argument"), decl);
18245 9 : return false;
18246 : }
18247 : break;
18248 :
18249 37122603 : case OVL_OP_FLAG_BINARY:
18250 37122603 : if (arity != 2)
18251 : {
18252 3 : error_at (loc,
18253 : methodp
18254 : ? G_("%qD must have exactly one argument")
18255 : : G_("%qD must have exactly two arguments"), decl);
18256 3 : return false;
18257 : }
18258 : break;
18259 :
18260 0 : default:
18261 0 : gcc_unreachable ();
18262 : }
18263 :
18264 : /* There can be no default arguments. */
18265 145771939 : for (tree arg = argtypes; arg && arg != void_list_node;
18266 94479908 : arg = TREE_CHAIN (arg))
18267 94479923 : if (TREE_PURPOSE (arg))
18268 : {
18269 15 : TREE_PURPOSE (arg) = NULL_TREE;
18270 15 : error_at (loc, "%qD cannot have default arguments", decl);
18271 15 : return false;
18272 : }
18273 :
18274 : /* At this point the declaration is well-formed. It may not be
18275 : sensible though. */
18276 :
18277 : /* Check member function warnings only on the in-class declaration.
18278 : There's no point warning on an out-of-class definition. */
18279 51292016 : if (class_type && class_type != current_class_type)
18280 : return true;
18281 :
18282 : /* Warn about conversion operators that will never be used. */
18283 49372761 : if (IDENTIFIER_CONV_OP_P (name)
18284 2441370 : && ! DECL_TEMPLATE_INFO (decl)
18285 50441220 : && warn_class_conversion)
18286 : {
18287 1068426 : tree t = TREE_TYPE (name);
18288 1068426 : int ref = TYPE_REF_P (t);
18289 :
18290 1068426 : if (ref)
18291 21955 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
18292 :
18293 1068426 : if (VOID_TYPE_P (t))
18294 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
18295 : "will never use a type conversion operator", class_type);
18296 1068417 : else if (class_type)
18297 : {
18298 1068417 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
18299 24 : warning_at (loc, OPT_Wclass_conversion,
18300 : ref
18301 : ? G_("converting %qT to a reference to the same type "
18302 : "will never use a type conversion operator")
18303 : : G_("converting %qT to the same type "
18304 : "will never use a type conversion operator"),
18305 : class_type);
18306 : /* Don't force t to be complete here. */
18307 1068402 : else if (MAYBE_CLASS_TYPE_P (t)
18308 609445 : && COMPLETE_TYPE_P (t)
18309 162544 : && DERIVED_FROM_P (t, class_type))
18310 30 : warning_at (loc, OPT_Wclass_conversion,
18311 : ref
18312 : ? G_("converting %qT to a reference to a base class "
18313 : "%qT will never use a type conversion operator")
18314 : : G_("converting %qT to a base class %qT "
18315 : "will never use a type conversion operator"),
18316 : class_type, t);
18317 : }
18318 : }
18319 :
18320 49372761 : if (!warn_ecpp)
18321 : return true;
18322 :
18323 : /* Effective C++ rules below. */
18324 :
18325 : /* More Effective C++ rule 7. */
18326 54 : if (operator_code == TRUTH_ANDIF_EXPR
18327 54 : || operator_code == TRUTH_ORIF_EXPR
18328 54 : || operator_code == COMPOUND_EXPR)
18329 0 : warning_at (loc, OPT_Weffc__,
18330 : "user-defined %qD always evaluates both arguments", decl);
18331 :
18332 : /* More Effective C++ rule 6. */
18333 54 : if (operator_code == POSTINCREMENT_EXPR
18334 : || operator_code == POSTDECREMENT_EXPR
18335 : || operator_code == PREINCREMENT_EXPR
18336 54 : || operator_code == PREDECREMENT_EXPR)
18337 : {
18338 0 : tree arg = TREE_VALUE (argtypes);
18339 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
18340 0 : if (methodp || TYPE_REF_P (arg))
18341 0 : arg = TREE_TYPE (arg);
18342 0 : arg = TYPE_MAIN_VARIANT (arg);
18343 :
18344 0 : if (operator_code == PREINCREMENT_EXPR
18345 : || operator_code == PREDECREMENT_EXPR)
18346 : {
18347 0 : if (!TYPE_REF_P (ret)
18348 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
18349 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
18350 : build_reference_type (arg));
18351 : }
18352 : else
18353 : {
18354 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
18355 0 : warning_at (loc, OPT_Weffc__,
18356 : "postfix %qD should return %qT", decl, arg);
18357 : }
18358 : }
18359 :
18360 : /* Effective C++ rule 23. */
18361 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
18362 : && (operator_code == PLUS_EXPR
18363 9 : || operator_code == MINUS_EXPR
18364 9 : || operator_code == TRUNC_DIV_EXPR
18365 : || operator_code == MULT_EXPR
18366 : || operator_code == TRUNC_MOD_EXPR)
18367 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
18368 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
18369 :
18370 : return true;
18371 : }
18372 :
18373 : /* Return a string giving the keyword associate with CODE. */
18374 :
18375 : const char *
18376 3578 : tag_name (enum tag_types code)
18377 : {
18378 3578 : switch (code)
18379 : {
18380 : case record_type:
18381 : return "struct";
18382 114 : case class_type:
18383 114 : return "class";
18384 3 : case union_type:
18385 3 : return "union";
18386 9 : case enum_type:
18387 9 : return "enum";
18388 3363 : case typename_type:
18389 3363 : case none_type:
18390 3363 : return "typename";
18391 29 : case scope_type:
18392 29 : return nullptr;
18393 : }
18394 0 : gcc_unreachable ();
18395 : }
18396 :
18397 : /* Name lookup in an elaborated-type-specifier (after the keyword
18398 : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
18399 : elaborated-type-specifier is invalid, issue a diagnostic and return
18400 : error_mark_node; otherwise, return the *_TYPE to which it referred.
18401 : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
18402 :
18403 : tree
18404 12677025 : check_elaborated_type_specifier (enum tag_types tag_code,
18405 : tree decl,
18406 : bool allow_template_p)
18407 : {
18408 12677025 : tree type;
18409 :
18410 : /* In the case of:
18411 :
18412 : struct S { struct S *p; };
18413 :
18414 : name lookup will find the TYPE_DECL for the implicit "S::S"
18415 : typedef. Adjust for that here. */
18416 12677025 : if (DECL_SELF_REFERENCE_P (decl))
18417 438915 : decl = TYPE_NAME (TREE_TYPE (decl));
18418 :
18419 12677025 : type = TREE_TYPE (decl);
18420 :
18421 : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
18422 : is false for this case as well. */
18423 12677025 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18424 : {
18425 9 : error ("using template type parameter %qT after %qs",
18426 : type, tag_name (tag_code));
18427 9 : return error_mark_node;
18428 : }
18429 : /* Accept template template parameters. */
18430 12677016 : else if (allow_template_p
18431 7169111 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
18432 7169099 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
18433 : ;
18434 : /* [dcl.type.elab]
18435 :
18436 : If the identifier resolves to a typedef-name or the
18437 : simple-template-id resolves to an alias template
18438 : specialization, the elaborated-type-specifier is ill-formed.
18439 :
18440 : In other words, the only legitimate declaration to use in the
18441 : elaborated type specifier is the implicit typedef created when
18442 : the type is declared. */
18443 12677001 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
18444 439044 : && !DECL_SELF_REFERENCE_P (decl)
18445 12677130 : && tag_code != typename_type)
18446 : {
18447 78 : auto_diagnostic_group d;
18448 78 : if (alias_template_specialization_p (type, nt_opaque))
18449 21 : error ("using alias template specialization %qT after %qs",
18450 : type, tag_name (tag_code));
18451 : else
18452 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
18453 78 : inform (DECL_SOURCE_LOCATION (decl),
18454 : "%qD has a previous declaration here", decl);
18455 78 : return error_mark_node;
18456 78 : }
18457 12676923 : else if (TREE_CODE (type) != RECORD_TYPE
18458 12676923 : && TREE_CODE (type) != UNION_TYPE
18459 : && tag_code != enum_type
18460 73413 : && tag_code != typename_type)
18461 : {
18462 6 : auto_diagnostic_group d;
18463 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
18464 6 : inform (location_of (type), "%qT has a previous declaration here", type);
18465 6 : return error_mark_node;
18466 6 : }
18467 12676917 : else if (TREE_CODE (type) != ENUMERAL_TYPE
18468 12603549 : && tag_code == enum_type)
18469 : {
18470 3 : auto_diagnostic_group d;
18471 3 : error ("%qT referred to as enum", type);
18472 3 : inform (location_of (type), "%qT has a previous declaration here", type);
18473 3 : return error_mark_node;
18474 3 : }
18475 12676914 : else if (!allow_template_p
18476 5507845 : && TREE_CODE (type) == RECORD_TYPE
18477 18057349 : && CLASSTYPE_IS_TEMPLATE (type))
18478 : {
18479 : /* If a class template appears as elaborated type specifier
18480 : without a template header such as:
18481 :
18482 : template <class T> class C {};
18483 : void f(class C); // No template header here
18484 :
18485 : then the required template argument is missing. */
18486 18 : error ("template argument required for %<%s %T%>",
18487 : tag_name (tag_code),
18488 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18489 18 : return error_mark_node;
18490 : }
18491 :
18492 : return type;
18493 : }
18494 :
18495 : /* Lookup NAME of an elaborated type specifier according to SCOPE and
18496 : issue diagnostics if necessary. Return *_TYPE node upon success,
18497 : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18498 : error. */
18499 :
18500 : static tree
18501 27632336 : lookup_and_check_tag (enum tag_types tag_code, tree name,
18502 : TAG_how how, bool template_header_p)
18503 : {
18504 27632336 : tree decl;
18505 27632336 : if (how == TAG_how::GLOBAL)
18506 : {
18507 : /* First try ordinary name lookup, ignoring hidden class name
18508 : injected via friend declaration. */
18509 2215139 : decl = lookup_name (name, LOOK_want::TYPE);
18510 : /* If that fails, the name will be placed in the smallest
18511 : non-class, non-function-prototype scope according to 3.3.1/5.
18512 : We may already have a hidden name declared as friend in this
18513 : scope. So lookup again but not ignoring hidden names.
18514 : If we find one, that name will be made visible rather than
18515 : creating a new tag. */
18516 2215139 : if (!decl)
18517 19343 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18518 : }
18519 : else
18520 25417197 : decl = lookup_elaborated_type (name, how);
18521 :
18522 25436540 : if (!decl)
18523 : /* We found nothing. */
18524 : return NULL_TREE;
18525 :
18526 6248131 : if (TREE_CODE (decl) == TREE_LIST)
18527 : {
18528 3 : auto_diagnostic_group d;
18529 3 : error ("reference to %qD is ambiguous", name);
18530 3 : print_candidates (input_location, decl);
18531 3 : return error_mark_node;
18532 3 : }
18533 :
18534 2556174 : if (DECL_CLASS_TEMPLATE_P (decl)
18535 2556168 : && !template_header_p
18536 8804296 : && how == TAG_how::CURRENT_ONLY)
18537 : {
18538 12 : auto_diagnostic_group d;
18539 12 : error ("class template %qD redeclared as non-template", name);
18540 12 : inform (location_of (decl), "previous declaration here");
18541 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18542 12 : return error_mark_node;
18543 12 : }
18544 :
18545 2556162 : if (DECL_CLASS_TEMPLATE_P (decl)
18546 : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18547 : so ignore a template template parameter. */
18548 6248122 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18549 2556159 : decl = DECL_TEMPLATE_RESULT (decl);
18550 :
18551 6248116 : if (TREE_CODE (decl) == USING_DECL)
18552 : {
18553 24 : tree u = decl;
18554 24 : decl = strip_using_decl (decl);
18555 24 : if (how == TAG_how::CURRENT_ONLY
18556 24 : && TREE_CODE (decl) == TYPE_DECL
18557 51 : && !(DECL_IMPLICIT_TYPEDEF_P (decl)
18558 24 : && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (u)
18559 3 : && redefinable_import_p (decl)))
18560 : {
18561 21 : auto_diagnostic_group _;
18562 21 : error ("%qD conflicts with a previous declaration", name);
18563 21 : inform (location_of (u), "previous declaration %qD", u);
18564 21 : inform (location_of (decl), "%qD declared here", decl);
18565 21 : return error_mark_node;
18566 21 : }
18567 : }
18568 :
18569 6248095 : if (TREE_CODE (decl) != TYPE_DECL)
18570 : /* Found not-a-type. */
18571 : return NULL_TREE;
18572 :
18573 : /* Look for invalid nested type:
18574 : class C {
18575 : class C {};
18576 : }; */
18577 6248074 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18578 : {
18579 6 : error ("%qD has the same name as the class in which it is "
18580 : "declared", decl);
18581 6 : return error_mark_node;
18582 : }
18583 :
18584 : /* Two cases we need to consider when deciding if a class
18585 : template is allowed as an elaborated type specifier:
18586 : 1. It is a self reference to its own class.
18587 : 2. It comes with a template header.
18588 :
18589 : For example:
18590 :
18591 : template <class T> class C {
18592 : class C *c1; // DECL_SELF_REFERENCE_P is true
18593 : class D;
18594 : };
18595 : template <class U> class C; // template_header_p is true
18596 : template <class T> class C<T>::D {
18597 : class C *c2; // DECL_SELF_REFERENCE_P is true
18598 : }; */
18599 :
18600 18744204 : tree t = check_elaborated_type_specifier (tag_code, decl,
18601 : template_header_p
18602 12057236 : | DECL_SELF_REFERENCE_P (decl));
18603 2742904 : if (template_header_p && t && CLASS_TYPE_P (t)
18604 8990960 : && (!CLASSTYPE_TEMPLATE_INFO (t)
18605 2742874 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18606 : {
18607 21 : auto_diagnostic_group d;
18608 21 : error ("%qT is not a template", t);
18609 21 : inform (location_of (t), "previous declaration here");
18610 42 : if (TYPE_CLASS_SCOPE_P (t)
18611 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18612 3 : inform (input_location,
18613 : "perhaps you want to explicitly add %<%T::%>",
18614 3 : TYPE_CONTEXT (t));
18615 21 : return error_mark_node;
18616 21 : }
18617 :
18618 : return t;
18619 : }
18620 :
18621 : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18622 : Define the tag as a forward-reference if it is not defined.
18623 :
18624 : If a declaration is given, process it here, and report an error if
18625 : multiple declarations are not identical.
18626 :
18627 : SCOPE is TS_CURRENT when this is also a definition. Only look in
18628 : the current frame for the name (since C++ allows new names in any
18629 : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18630 : declaration. Only look beginning from the current scope outward up
18631 : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18632 :
18633 : TEMPLATE_HEADER_P is true when this declaration is preceded by
18634 : a set of template parameters. */
18635 :
18636 : tree
18637 27617602 : xref_tag (enum tag_types tag_code, tree name,
18638 : TAG_how how, bool template_header_p)
18639 : {
18640 27617602 : enum tree_code code;
18641 27617602 : tree context = NULL_TREE;
18642 :
18643 27617602 : auto_cond_timevar tv (TV_NAME_LOOKUP);
18644 :
18645 27617602 : gcc_assert (identifier_p (name));
18646 :
18647 27617602 : switch (tag_code)
18648 : {
18649 : case record_type:
18650 : case class_type:
18651 : code = RECORD_TYPE;
18652 : break;
18653 : case union_type:
18654 : code = UNION_TYPE;
18655 : break;
18656 : case enum_type:
18657 : code = ENUMERAL_TYPE;
18658 : break;
18659 0 : default:
18660 0 : gcc_unreachable ();
18661 : }
18662 :
18663 : /* In case of anonymous name, xref_tag is only called to
18664 : make type node and push name. Name lookup is not required. */
18665 27617602 : tree t = NULL_TREE;
18666 27617602 : if (!IDENTIFIER_ANON_P (name))
18667 25166398 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18668 :
18669 27617602 : if (t == error_mark_node)
18670 : return error_mark_node;
18671 :
18672 2958630 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18673 1408596 : && template_class_depth (current_class_type)
18674 28240024 : && template_header_p)
18675 : {
18676 363004 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18677 : return t;
18678 :
18679 : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18680 : a definition of this tag. Since, in addition, we are
18681 : currently processing a (member) template declaration of a
18682 : template class, we must be very careful; consider:
18683 :
18684 : template <class X> struct S1
18685 :
18686 : template <class U> struct S2
18687 : {
18688 : template <class V> friend struct S1;
18689 : };
18690 :
18691 : Here, the S2::S1 declaration should not be confused with the
18692 : outer declaration. In particular, the inner version should
18693 : have a template parameter of level 2, not level 1.
18694 :
18695 : On the other hand, when presented with:
18696 :
18697 : template <class T> struct S1
18698 : {
18699 : template <class U> struct S2 {};
18700 : template <class U> friend struct S2;
18701 : };
18702 :
18703 : the friend must find S1::S2 eventually. We accomplish this
18704 : by making sure that the new type we create to represent this
18705 : declaration has the right TYPE_CONTEXT. */
18706 363001 : context = TYPE_CONTEXT (t);
18707 363001 : t = NULL_TREE;
18708 : }
18709 :
18710 27617476 : if (! t)
18711 : {
18712 : /* If no such tag is yet defined, create a forward-reference node
18713 : and record it as the "definition".
18714 : When a real declaration of this type is found,
18715 : the forward-reference will be altered into a real type. */
18716 21769605 : if (code == ENUMERAL_TYPE)
18717 : {
18718 13 : error ("use of enum %q#D without previous declaration", name);
18719 13 : return error_mark_node;
18720 : }
18721 :
18722 21769592 : t = make_class_type (code);
18723 21769592 : TYPE_CONTEXT (t) = context;
18724 21769592 : if (IDENTIFIER_LAMBDA_P (name))
18725 : /* Mark it as a lambda type right now. Our caller will
18726 : correct the value. */
18727 1684701 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18728 21769592 : t = pushtag (name, t, how);
18729 : }
18730 : else
18731 : {
18732 5847871 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18733 : {
18734 : /* Check that we aren't trying to overload a class with different
18735 : constraints. */
18736 2379870 : if (!redeclare_class_template (t, current_template_parms,
18737 : current_template_constraints ()))
18738 60 : return error_mark_node;
18739 : }
18740 3468001 : else if (!processing_template_decl
18741 3120861 : && CLASS_TYPE_P (t)
18742 6562443 : && CLASSTYPE_IS_TEMPLATE (t))
18743 : {
18744 0 : auto_diagnostic_group d;
18745 0 : error ("redeclaration of %qT as a non-template", t);
18746 0 : inform (location_of (t), "previous declaration %qD", t);
18747 0 : return error_mark_node;
18748 0 : }
18749 :
18750 5847811 : if (modules_p ()
18751 5847811 : && how == TAG_how::CURRENT_ONLY)
18752 : {
18753 13730 : tree decl = TYPE_NAME (t);
18754 13730 : if (!module_may_redeclare (decl))
18755 15 : return error_mark_node;
18756 :
18757 13715 : tree not_tmpl = STRIP_TEMPLATE (decl);
18758 13715 : if (DECL_LANG_SPECIFIC (not_tmpl)
18759 5503 : && DECL_MODULE_ATTACH_P (not_tmpl)
18760 57 : && !DECL_MODULE_EXPORT_P (not_tmpl)
18761 13757 : && module_exporting_p ())
18762 : {
18763 6 : auto_diagnostic_group d;
18764 6 : error ("conflicting exporting for declaration %qD", decl);
18765 6 : inform (DECL_SOURCE_LOCATION (decl),
18766 : "previously declared here without exporting");
18767 6 : }
18768 :
18769 13715 : tree maybe_tmpl = decl;
18770 13715 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18771 10223 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18772 :
18773 : /* FIXME: we should do a more precise check for redefinitions
18774 : of a conflicting using-declaration here, as these diagnostics
18775 : are not ideal. */
18776 13715 : if (DECL_LANG_SPECIFIC (decl)
18777 5503 : && DECL_MODULE_IMPORT_P (decl)
18778 14470 : && CP_DECL_CONTEXT (decl) == current_namespace)
18779 : {
18780 : /* Push it into this TU's symbol slot. */
18781 755 : if (maybe_tmpl != decl)
18782 : /* We're in the template parm binding level.
18783 : Pushtag has logic to slide under that, but we're
18784 : not pushing a *new* type. */
18785 547 : push_nested_namespace (CP_DECL_CONTEXT (decl));
18786 :
18787 755 : pushdecl (maybe_tmpl);
18788 755 : if (maybe_tmpl != decl)
18789 547 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
18790 : }
18791 :
18792 13715 : set_instantiating_module (maybe_tmpl);
18793 : }
18794 : }
18795 :
18796 : return t;
18797 27617602 : }
18798 :
18799 : /* Create the binfo hierarchy for REF with (possibly NULL) base list
18800 : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18801 : access_* node, and the TREE_VALUE is the type of the base-class.
18802 : Non-NULL TREE_TYPE indicates virtual inheritance. */
18803 :
18804 : void
18805 74094758 : xref_basetypes (tree ref, tree base_list)
18806 : {
18807 74094758 : tree *basep;
18808 74094758 : tree binfo, base_binfo;
18809 74094758 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18810 74094758 : unsigned max_bases = 0; /* Maximum direct bases. */
18811 74094758 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18812 : /* Highest direct base index with annotations. */
18813 74094758 : unsigned max_annotated_base = 0;
18814 74094758 : int i;
18815 74094758 : tree default_access;
18816 74094758 : tree igo_prev; /* Track Inheritance Graph Order. */
18817 :
18818 74094758 : if (ref == error_mark_node)
18819 12 : return;
18820 :
18821 : /* The base of a derived class is private by default, all others are
18822 : public. */
18823 74094758 : default_access = (TREE_CODE (ref) == RECORD_TYPE
18824 73448113 : && CLASSTYPE_DECLARED_CLASS (ref)
18825 74094758 : ? access_private_node : access_public_node);
18826 :
18827 : /* First, make sure that any templates in base-classes are
18828 : instantiated. This ensures that if we call ourselves recursively
18829 : we do not get confused about which classes are marked and which
18830 : are not. */
18831 74094758 : basep = &base_list;
18832 108505999 : while (*basep)
18833 : {
18834 34411241 : tree basetype = TREE_VALUE (*basep);
18835 :
18836 : /* The dependent_type_p call below should really be dependent_scope_p
18837 : so that we give a hard error about using an incomplete type as a
18838 : base, but we allow it with a pedwarn for backward
18839 : compatibility. */
18840 34411241 : if (processing_template_decl
18841 34411241 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18842 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
18843 : diagnostics::kind::pedwarn);
18844 34411241 : if (!dependent_type_p (basetype)
18845 34411241 : && !complete_type_or_else (basetype, NULL))
18846 : /* An incomplete type. Remove it from the list. */
18847 15 : *basep = TREE_CHAIN (*basep);
18848 : else
18849 : {
18850 34411226 : max_bases++;
18851 34411226 : if (TREE_CODE (TREE_PURPOSE (*basep)) == TREE_LIST)
18852 28 : max_annotated_base = max_bases;
18853 34411226 : if (TREE_TYPE (*basep))
18854 70850 : max_dvbases++;
18855 34411226 : if (CLASS_TYPE_P (basetype))
18856 33581355 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18857 34411226 : basep = &TREE_CHAIN (*basep);
18858 : }
18859 : }
18860 74094758 : max_vbases += max_dvbases;
18861 :
18862 74094758 : TYPE_MARKED_P (ref) = 1;
18863 :
18864 : /* The binfo slot should be empty, unless this is an (ill-formed)
18865 : redefinition. */
18866 74094758 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18867 :
18868 74094758 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18869 :
18870 74094758 : binfo = make_tree_binfo (max_bases);
18871 :
18872 74094758 : TYPE_BINFO (ref) = binfo;
18873 74094758 : BINFO_OFFSET (binfo) = size_zero_node;
18874 74094758 : BINFO_TYPE (binfo) = ref;
18875 :
18876 : /* Apply base-class info set up to the variants of this type. */
18877 74094758 : fixup_type_variants (ref);
18878 :
18879 74094758 : if (max_bases)
18880 : {
18881 33471164 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases + max_annotated_base);
18882 33471164 : BINFO_BASE_ACCESSES (binfo)->quick_grow (max_bases + max_annotated_base);
18883 : /* A C++98 POD cannot have base classes. */
18884 33471164 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18885 :
18886 33471164 : if (TREE_CODE (ref) == UNION_TYPE)
18887 : {
18888 12 : error ("derived union %qT invalid", ref);
18889 12 : return;
18890 : }
18891 : }
18892 :
18893 33471152 : if (max_bases > 1)
18894 742556 : warning (OPT_Wmultiple_inheritance,
18895 : "%qT defined with multiple direct bases", ref);
18896 :
18897 74094746 : if (max_vbases)
18898 : {
18899 : /* An aggregate can't have virtual base classes. */
18900 210444 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18901 :
18902 210444 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
18903 :
18904 210444 : if (max_dvbases)
18905 69423 : warning (OPT_Wvirtual_inheritance,
18906 : "%qT defined with direct virtual base", ref);
18907 : }
18908 :
18909 108505960 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18910 : {
18911 34411214 : tree access = TREE_PURPOSE (base_list);
18912 34411214 : tree annotations = NULL_TREE;
18913 34411214 : if (TREE_CODE (access) == TREE_LIST)
18914 : {
18915 28 : annotations = TREE_VALUE (access);
18916 28 : access = TREE_PURPOSE (access);
18917 78 : for (tree *d = &annotations; *d; )
18918 : {
18919 50 : if (annotation_p (*d))
18920 : {
18921 50 : tree name = get_attribute_name (*d);
18922 50 : tree args = TREE_VALUE (*d);
18923 50 : const attribute_spec *as
18924 50 : = lookup_attribute_spec (TREE_PURPOSE (*d));
18925 50 : bool no_add_attrs = false;
18926 50 : as->handler (&binfo, name, args, 0, &no_add_attrs);
18927 50 : if (no_add_attrs)
18928 : {
18929 2 : *d = TREE_CHAIN (*d);
18930 2 : continue;
18931 : }
18932 : }
18933 48 : d = &TREE_CHAIN (*d);
18934 : }
18935 : }
18936 34411214 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18937 34411214 : tree basetype = TREE_VALUE (base_list);
18938 :
18939 34411214 : if (access == access_default_node)
18940 4675284 : access = default_access;
18941 :
18942 : /* Before C++17, an aggregate cannot have base classes. In C++17, an
18943 : aggregate can't have virtual, private, or protected base classes. */
18944 34411214 : if (cxx_dialect < cxx17
18945 34186809 : || access != access_public_node
18946 33209892 : || via_virtual)
18947 1268608 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18948 :
18949 34411214 : if (PACK_EXPANSION_P (basetype))
18950 30041 : basetype = PACK_EXPANSION_PATTERN (basetype);
18951 34411214 : if (TREE_CODE (basetype) == TYPE_DECL)
18952 0 : basetype = TREE_TYPE (basetype);
18953 34411214 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18954 : {
18955 24 : error ("base type %qT fails to be a struct or class type",
18956 : basetype);
18957 24 : goto dropped_base;
18958 : }
18959 34411190 : else if (ANON_AGGR_TYPE_P (basetype))
18960 : {
18961 2 : error ("base type %qT is anonymous struct type", basetype);
18962 2 : goto dropped_base;
18963 : }
18964 :
18965 34411188 : base_binfo = NULL_TREE;
18966 34411188 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18967 : {
18968 27148370 : base_binfo = TYPE_BINFO (basetype);
18969 : /* The original basetype could have been a typedef'd type. */
18970 27148370 : basetype = BINFO_TYPE (base_binfo);
18971 :
18972 : /* Inherit flags from the base. */
18973 81445110 : TYPE_HAS_NEW_OPERATOR (ref)
18974 27148370 : |= TYPE_HAS_NEW_OPERATOR (basetype);
18975 81445110 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18976 27148370 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18977 27148370 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18978 27148370 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18979 81445110 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
18980 27148370 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18981 81445110 : CLASSTYPE_REPEATED_BASE_P (ref)
18982 27148370 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
18983 : }
18984 :
18985 : /* We must do this test after we've seen through a typedef
18986 : type. */
18987 34411188 : if (TYPE_MARKED_P (basetype))
18988 : {
18989 18 : if (basetype == ref)
18990 0 : error ("recursive type %qT undefined", basetype);
18991 : else
18992 18 : error ("duplicate base type %qT invalid", basetype);
18993 18 : goto dropped_base;
18994 : }
18995 :
18996 34411170 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18997 : /* Regenerate the pack expansion for the bases. */
18998 30041 : basetype = make_pack_expansion (basetype);
18999 :
19000 34411170 : TYPE_MARKED_P (basetype) = 1;
19001 :
19002 34411170 : base_binfo = copy_binfo (base_binfo, basetype, ref,
19003 : &igo_prev, via_virtual);
19004 34411170 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
19005 34340335 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
19006 :
19007 34411170 : unsigned len;
19008 34411170 : len = BINFO_N_BASE_BINFOS (binfo);
19009 34411170 : BINFO_BASE_APPEND (binfo, base_binfo);
19010 34411170 : BINFO_BASE_ACCESS (binfo, len) = access;
19011 34411170 : if (len < max_annotated_base)
19012 34 : BINFO_BASE_ACCESS (binfo, max_bases + len) = annotations;
19013 34411170 : continue;
19014 :
19015 44 : dropped_base:
19016 : /* Update max_vbases to reflect the reality that we are dropping
19017 : this base: if it reaches zero we want to undo the vec_alloc
19018 : above to avoid inconsistencies during error-recovery: eg, in
19019 : build_special_member_call, CLASSTYPE_VBASECLASSES non null
19020 : and vtt null (c++/27952). */
19021 44 : if (via_virtual)
19022 12 : max_vbases--;
19023 44 : if (CLASS_TYPE_P (basetype))
19024 32 : max_vbases
19025 35 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
19026 34411170 : }
19027 :
19028 74094746 : unsigned len = BINFO_N_BASE_BINFOS (binfo);
19029 74094746 : if (len < max_bases)
19030 : {
19031 44 : if (len && max_annotated_base)
19032 0 : memmove (&BINFO_BASE_ACCESS (binfo, len),
19033 0 : &BINFO_BASE_ACCESS (binfo, max_bases),
19034 0 : MIN (max_annotated_base, len) * sizeof (tree));
19035 44 : BINFO_BASE_ACCESSES (binfo)->truncate (len + MIN (max_annotated_base,
19036 : len));
19037 : }
19038 :
19039 74094746 : if (CLASSTYPE_VBASECLASSES (ref)
19040 74094746 : && max_vbases == 0)
19041 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
19042 :
19043 74305178 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
19044 : /* If we didn't get max_vbases vbases, we must have shared at
19045 : least one of them, and are therefore diamond shaped. */
19046 22946 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
19047 :
19048 : /* Unmark all the types. */
19049 108505916 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
19050 34411170 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
19051 74094746 : TYPE_MARKED_P (ref) = 0;
19052 :
19053 : /* Now see if we have a repeated base type. */
19054 74094746 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
19055 : {
19056 186866670 : for (base_binfo = binfo; base_binfo;
19057 112772360 : base_binfo = TREE_CHAIN (base_binfo))
19058 : {
19059 112773109 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
19060 : {
19061 749 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
19062 749 : break;
19063 : }
19064 112772360 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
19065 : }
19066 186866670 : for (base_binfo = binfo; base_binfo;
19067 112772360 : base_binfo = TREE_CHAIN (base_binfo))
19068 112773109 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
19069 112772360 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
19070 : else
19071 : break;
19072 : }
19073 : }
19074 :
19075 :
19076 : /* Copies the enum-related properties from type SRC to type DST.
19077 : Used with the underlying type of an enum and the enum itself. */
19078 : static void
19079 2089162 : copy_type_enum (tree dst, tree src)
19080 : {
19081 2089162 : tree t;
19082 4178348 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
19083 : {
19084 2089186 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
19085 2089186 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
19086 2089186 : TYPE_SIZE (t) = TYPE_SIZE (src);
19087 2089186 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
19088 2089186 : SET_TYPE_MODE (dst, TYPE_MODE (src));
19089 2089186 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
19090 2089186 : unsigned valign = TYPE_ALIGN (src);
19091 2089186 : if (TYPE_USER_ALIGN (t))
19092 12 : valign = MAX (valign, TYPE_ALIGN (t));
19093 : else
19094 2089174 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
19095 2089186 : SET_TYPE_ALIGN (t, valign);
19096 2089186 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
19097 : }
19098 2089162 : }
19099 :
19100 : /* Begin compiling the definition of an enumeration type.
19101 : NAME is its name,
19102 :
19103 : if ENUMTYPE is not NULL_TREE then the type has already been found.
19104 :
19105 : UNDERLYING_TYPE is the type that will be used as the storage for
19106 : the enumeration type. This should be NULL_TREE if no storage type
19107 : was specified.
19108 :
19109 : ATTRIBUTES are any attributes specified after the enum-key.
19110 :
19111 : SCOPED_ENUM_P is true if this is a scoped enumeration type.
19112 :
19113 : if IS_NEW is not NULL, gets TRUE iff a new type is created.
19114 :
19115 : Returns the type object, as yet incomplete.
19116 : Also records info about it so that build_enumerator
19117 : may be used to declare the individual values as they are read. */
19118 :
19119 : tree
19120 2466080 : start_enum (tree name, tree enumtype, tree underlying_type,
19121 : tree attributes, bool scoped_enum_p, bool *is_new)
19122 : {
19123 2466080 : tree prevtype = NULL_TREE;
19124 2466080 : gcc_assert (identifier_p (name));
19125 :
19126 2466080 : if (is_new)
19127 1990064 : *is_new = false;
19128 : /* [C++0x dcl.enum]p5:
19129 :
19130 : If not explicitly specified, the underlying type of a scoped
19131 : enumeration type is int. */
19132 2466080 : if (!underlying_type && scoped_enum_p)
19133 182117 : underlying_type = integer_type_node;
19134 :
19135 2466080 : if (underlying_type)
19136 615492 : underlying_type = cv_unqualified (underlying_type);
19137 :
19138 : /* If this is the real definition for a previous forward reference,
19139 : fill in the contents in the same object that used to be the
19140 : forward reference. */
19141 2466080 : if (!enumtype)
19142 2465938 : enumtype = lookup_and_check_tag (enum_type, name,
19143 : /*tag_scope=*/TAG_how::CURRENT_ONLY,
19144 : /*template_header_p=*/false);
19145 :
19146 : /* In case of a template_decl, the only check that should be deferred
19147 : to instantiation time is the comparison of underlying types. */
19148 2466080 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
19149 : {
19150 : /* Attempt to set the declaring module. */
19151 37242 : if (modules_p ())
19152 : {
19153 252 : tree decl = TYPE_NAME (enumtype);
19154 252 : if (!module_may_redeclare (decl))
19155 3 : enumtype = error_mark_node;
19156 249 : else if (COMPLETE_TYPE_P (enumtype)
19157 249 : && redefinable_import_p (decl))
19158 36 : return enumtype;
19159 : else
19160 213 : set_instantiating_module (decl);
19161 : }
19162 :
19163 37206 : if (enumtype == error_mark_node)
19164 : ;
19165 37416 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
19166 : {
19167 6 : auto_diagnostic_group d;
19168 6 : error_at (input_location, "scoped/unscoped mismatch "
19169 : "in enum %q#T", enumtype);
19170 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19171 : "previous definition here");
19172 6 : enumtype = error_mark_node;
19173 6 : }
19174 37197 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
19175 : {
19176 0 : auto_diagnostic_group d;
19177 0 : error_at (input_location, "underlying type mismatch "
19178 : "in enum %q#T", enumtype);
19179 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19180 : "previous definition here");
19181 0 : enumtype = error_mark_node;
19182 0 : }
19183 37191 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
19184 74388 : && !same_type_p (underlying_type,
19185 : ENUM_UNDERLYING_TYPE (enumtype)))
19186 : {
19187 29 : auto_diagnostic_group d;
19188 29 : error_at (input_location, "different underlying type "
19189 : "in enum %q#T", enumtype);
19190 29 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19191 : "previous definition here");
19192 29 : underlying_type = NULL_TREE;
19193 29 : }
19194 : }
19195 :
19196 2466044 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
19197 37197 : || processing_template_decl)
19198 : {
19199 : /* In case of error, make a dummy enum to allow parsing to
19200 : continue. */
19201 2428948 : if (enumtype == error_mark_node)
19202 : {
19203 18 : name = make_anon_name ();
19204 18 : enumtype = NULL_TREE;
19205 : }
19206 :
19207 : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
19208 : of an opaque enum, or an opaque enum of an already defined
19209 : enumeration (C++11).
19210 : In any other case, it'll be NULL_TREE. */
19211 2428948 : if (!enumtype)
19212 : {
19213 2428844 : if (is_new)
19214 1952828 : *is_new = true;
19215 : }
19216 2428948 : prevtype = enumtype;
19217 :
19218 : /* Do not push the decl more than once. */
19219 2428948 : if (!enumtype
19220 104 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
19221 : {
19222 2428847 : enumtype = cxx_make_type (ENUMERAL_TYPE);
19223 2428847 : enumtype = pushtag (name, enumtype);
19224 :
19225 2428847 : if (enumtype != error_mark_node)
19226 : {
19227 : /* ??? We wouldn't need to repeat this if lookup_elaborated_type
19228 : found (and pushed) mergeable types. */
19229 2428829 : if (COMPLETE_TYPE_P (enumtype)
19230 2428829 : && redefinable_import_p (TYPE_NAME (enumtype)))
19231 42 : return enumtype;
19232 :
19233 : /* The enum is considered opaque until the opening '{' of the
19234 : enumerator list. */
19235 2428787 : SET_OPAQUE_ENUM_P (enumtype, true);
19236 2428787 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
19237 :
19238 : /* std::byte aliases anything. */
19239 2428787 : if (TYPE_CONTEXT (enumtype) == std_node
19240 2798163 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
19241 13942 : TYPE_ALIAS_SET (enumtype) = 0;
19242 : }
19243 : }
19244 : else
19245 101 : enumtype = xref_tag (enum_type, name);
19246 :
19247 2428906 : if (enumtype == error_mark_node)
19248 : return error_mark_node;
19249 : }
19250 :
19251 2465984 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
19252 :
19253 2465984 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19254 :
19255 2465984 : if (underlying_type)
19256 : {
19257 615398 : if (ENUM_UNDERLYING_TYPE (enumtype))
19258 : /* We already checked that it matches, don't change it to a different
19259 : typedef variant. */;
19260 578236 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
19261 : {
19262 578162 : copy_type_enum (enumtype, underlying_type);
19263 578162 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
19264 : }
19265 74 : else if (dependent_type_p (underlying_type))
19266 61 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
19267 : else
19268 : {
19269 13 : error ("underlying type %qT of %qT must be an integral type",
19270 : underlying_type, enumtype);
19271 13 : copy_type_enum (enumtype, integer_type_node);
19272 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
19273 : }
19274 : }
19275 :
19276 : /* If into a template class, the returned enum is always the first
19277 : declaration (opaque or not) seen. This way all the references to
19278 : this type will be to the same declaration. The following ones are used
19279 : only to check for definition errors. */
19280 2465984 : if (prevtype && processing_template_decl)
19281 : return prevtype;
19282 : else
19283 2465883 : return enumtype;
19284 : }
19285 :
19286 : /* Returns true if TYPE is an enum that uses an enumerator name for
19287 : linkage purposes at namespace scope. The term is defined in [dcl.enum]/12
19288 : for all enums, not just those at namespace scope, but for backward ABI
19289 : compatibility we want to treat those not at namespace scope the old way
19290 : and e.g. mangle the class scope ones based on their position within the
19291 : class rather than the first enumerator. */
19292 :
19293 : bool
19294 422855116 : enum_with_enumerator_for_linkage_p (tree type)
19295 : {
19296 422855116 : return (cxx_dialect >= cxx20
19297 418168305 : && UNSCOPED_ENUM_P (type)
19298 20472264 : && TYPE_ANON_P (type)
19299 3170894 : && TYPE_VALUES (type)
19300 424802850 : && TYPE_NAMESPACE_SCOPE_P (type));
19301 : }
19302 :
19303 : /* After processing and defining all the values of an enumeration type,
19304 : install their decls in the enumeration type.
19305 : ENUMTYPE is the type object. */
19306 :
19307 : void
19308 2360564 : finish_enum_value_list (tree enumtype)
19309 : {
19310 2360564 : tree values;
19311 2360564 : tree underlying_type;
19312 2360564 : tree decl;
19313 2360564 : tree value;
19314 2360564 : tree minnode, maxnode;
19315 2360564 : tree t;
19316 :
19317 2360564 : ENUM_BEING_DEFINED_P (enumtype) = 0;
19318 :
19319 2360564 : bool fixed_underlying_type_p
19320 2360564 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
19321 :
19322 : /* We built up the VALUES in reverse order. */
19323 2360564 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
19324 :
19325 : /* For an enum defined in a template, just set the type of the values;
19326 : all further processing is postponed until the template is
19327 : instantiated. We need to set the type so that tsubst of a CONST_DECL
19328 : works. */
19329 2360564 : if (processing_template_decl)
19330 : {
19331 447357 : for (values = TYPE_VALUES (enumtype);
19332 806522 : values;
19333 447357 : values = TREE_CHAIN (values))
19334 447357 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
19335 : return;
19336 : }
19337 :
19338 : /* Determine the minimum and maximum values of the enumerators. */
19339 2001399 : if (TYPE_VALUES (enumtype))
19340 : {
19341 : minnode = maxnode = NULL_TREE;
19342 :
19343 11975236 : for (values = TYPE_VALUES (enumtype);
19344 13938769 : values;
19345 11975236 : values = TREE_CHAIN (values))
19346 : {
19347 11975236 : decl = TREE_VALUE (values);
19348 :
19349 : /* [dcl.enum]: Following the closing brace of an enum-specifier,
19350 : each enumerator has the type of its enumeration. Prior to the
19351 : closing brace, the type of each enumerator is the type of its
19352 : initializing value. */
19353 11975236 : TREE_TYPE (decl) = enumtype;
19354 :
19355 : /* Update the minimum and maximum values, if appropriate. */
19356 11975236 : value = DECL_INITIAL (decl);
19357 11975236 : if (!value || TREE_CODE (value) != INTEGER_CST)
19358 7 : value = integer_zero_node;
19359 : /* Figure out what the minimum and maximum values of the
19360 : enumerators are. */
19361 11975236 : if (!minnode)
19362 : minnode = maxnode = value;
19363 10011703 : else if (tree_int_cst_lt (maxnode, value))
19364 : maxnode = value;
19365 1550115 : else if (tree_int_cst_lt (value, minnode))
19366 176009 : minnode = value;
19367 : }
19368 : }
19369 : else
19370 : /* [dcl.enum]
19371 :
19372 : If the enumerator-list is empty, the underlying type is as if
19373 : the enumeration had a single enumerator with value 0. */
19374 37866 : minnode = maxnode = integer_zero_node;
19375 :
19376 2001399 : if (!fixed_underlying_type_p)
19377 : {
19378 : /* Compute the number of bits require to represent all values of the
19379 : enumeration. We must do this before the type of MINNODE and
19380 : MAXNODE are transformed, since tree_int_cst_min_precision relies
19381 : on the TREE_TYPE of the value it is passed. */
19382 1510987 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
19383 1510987 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
19384 1510987 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
19385 1510987 : int precision = MAX (lowprec, highprec);
19386 1510987 : unsigned int itk;
19387 1510987 : bool use_short_enum;
19388 :
19389 : /* Determine the underlying type of the enumeration.
19390 :
19391 : [dcl.enum]
19392 :
19393 : The underlying type of an enumeration is an integral type that
19394 : can represent all the enumerator values defined in the
19395 : enumeration. It is implementation-defined which integral type is
19396 : used as the underlying type for an enumeration except that the
19397 : underlying type shall not be larger than int unless the value of
19398 : an enumerator cannot fit in an int or unsigned int.
19399 :
19400 : We use "int" or an "unsigned int" as the underlying type, even if
19401 : a smaller integral type would work, unless the user has
19402 : explicitly requested that we use the smallest possible type. The
19403 : user can request that for all enumerations with a command line
19404 : flag, or for just one enumeration with an attribute. */
19405 :
19406 3021974 : use_short_enum = flag_short_enums
19407 1510987 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
19408 :
19409 : /* If the precision of the type was specified with an attribute and it
19410 : was too small, give an error. Otherwise, use it. */
19411 1510987 : if (TYPE_PRECISION (enumtype))
19412 : {
19413 11 : if (precision > TYPE_PRECISION (enumtype))
19414 0 : error ("specified mode too small for enumerated values");
19415 : else
19416 : {
19417 11 : use_short_enum = true;
19418 11 : precision = TYPE_PRECISION (enumtype);
19419 : }
19420 : }
19421 :
19422 2938150 : for (itk = (use_short_enum ? itk_char : itk_int);
19423 2938150 : itk != itk_none;
19424 : itk++)
19425 : {
19426 2938147 : underlying_type = integer_types[itk];
19427 2938147 : if (underlying_type != NULL_TREE
19428 2938129 : && TYPE_PRECISION (underlying_type) >= precision
19429 5874171 : && TYPE_SIGN (underlying_type) == sgn)
19430 : break;
19431 : }
19432 1510987 : if (itk == itk_none)
19433 : {
19434 : /* DR 377
19435 :
19436 : IF no integral type can represent all the enumerator values, the
19437 : enumeration is ill-formed. */
19438 3 : error ("no integral type can represent all of the enumerator values "
19439 : "for %qT", enumtype);
19440 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
19441 3 : underlying_type = integer_types[itk_unsigned_long_long];
19442 : }
19443 :
19444 : /* [dcl.enum]
19445 :
19446 : The value of sizeof() applied to an enumeration type, an object
19447 : of an enumeration type, or an enumerator, is the value of sizeof()
19448 : applied to the underlying type. */
19449 1510987 : copy_type_enum (enumtype, underlying_type);
19450 :
19451 : /* Compute the minimum and maximum values for the type.
19452 :
19453 : [dcl.enum]
19454 :
19455 : For an enumeration where emin is the smallest enumerator and emax
19456 : is the largest, the values of the enumeration are the values of the
19457 : underlying type in the range bmin to bmax, where bmin and bmax are,
19458 : respectively, the smallest and largest values of the smallest bit-
19459 : field that can store emin and emax. */
19460 :
19461 : /* The middle-end currently assumes that types with TYPE_PRECISION
19462 : narrower than their underlying type are suitably zero or sign
19463 : extended to fill their mode. Similarly, it assumes that the front
19464 : end assures that a value of a particular type must be within
19465 : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
19466 :
19467 : We used to set these fields based on bmin and bmax, but that led
19468 : to invalid assumptions like optimizing away bounds checking. So
19469 : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
19470 : TYPE_MAX_VALUE to the values for the mode above and only restrict
19471 : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
19472 1510987 : ENUM_UNDERLYING_TYPE (enumtype)
19473 1510987 : = build_distinct_type_copy (underlying_type);
19474 1510987 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
19475 1510987 : set_min_and_max_values_for_integral_type
19476 1510987 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
19477 :
19478 : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
19479 1510987 : if (flag_strict_enums)
19480 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
19481 :
19482 1510987 : if (use_short_enum)
19483 : {
19484 126 : TYPE_PACKED (enumtype) = use_short_enum;
19485 126 : fixup_attribute_variants (enumtype);
19486 : }
19487 : }
19488 : else
19489 490412 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
19490 :
19491 : /* If the enum is exported, mark the consts too. */
19492 2001399 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
19493 1588546 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
19494 2001987 : && at_namespace_scope_p ());
19495 :
19496 : /* Convert each of the enumerators to the type of the underlying
19497 : type of the enumeration. */
19498 13976635 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
19499 : {
19500 11975236 : decl = TREE_VALUE (values);
19501 11975236 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
19502 11975236 : if (fixed_underlying_type_p)
19503 : /* If the enumeration type has a fixed underlying type, we
19504 : already checked all of the enumerator values. */
19505 4201582 : value = DECL_INITIAL (decl);
19506 : else
19507 7773654 : value = perform_implicit_conversion (underlying_type,
19508 7773654 : DECL_INITIAL (decl),
19509 : tf_warning_or_error);
19510 11975236 : if (!value)
19511 3 : value = integer_zero_node;
19512 : /* Do not clobber shared ints. But do share identical enumerators. */
19513 11975236 : value = fold_convert (enumtype, value);
19514 :
19515 11975236 : DECL_INITIAL (decl) = value;
19516 11975236 : if (export_p)
19517 3594 : DECL_MODULE_EXPORT_P (decl) = true;
19518 11975236 : }
19519 :
19520 : /* Fix up all variant types of this enum type. */
19521 4002844 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
19522 2001445 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
19523 :
19524 2001399 : if (at_class_scope_p ()
19525 1070955 : && COMPLETE_TYPE_P (current_class_type)
19526 2001466 : && UNSCOPED_ENUM_P (enumtype))
19527 : {
19528 48 : insert_late_enum_def_bindings (current_class_type, enumtype);
19529 : /* TYPE_FIELDS needs fixup. */
19530 48 : fixup_type_variants (current_class_type);
19531 : }
19532 :
19533 : /* P2115: An unnamed enum uses the name of its first enumerator for
19534 : linkage purposes; reset the type linkage if that is the case. */
19535 2001399 : if (enum_with_enumerator_for_linkage_p (enumtype))
19536 356298 : reset_type_linkage (enumtype);
19537 :
19538 : /* Finish debugging output for this type. */
19539 2001399 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
19540 :
19541 : /* Each enumerator now has the type of its enumeration. Clear the cache
19542 : so that this change in types doesn't confuse us later on. */
19543 2001399 : clear_cv_and_fold_caches ();
19544 : }
19545 :
19546 : /* Finishes the enum type. This is called only the first time an
19547 : enumeration is seen, be it opaque or odinary.
19548 : ENUMTYPE is the type object. */
19549 :
19550 : void
19551 2348682 : finish_enum (tree enumtype)
19552 : {
19553 2348682 : if (processing_template_decl)
19554 : {
19555 359193 : if (at_function_scope_p ())
19556 9670 : add_stmt (build_min (TAG_DEFN, enumtype));
19557 : return;
19558 : }
19559 :
19560 : /* If this is a forward declaration, there should not be any variants,
19561 : though we can get a variant in the middle of an enum-specifier with
19562 : wacky code like 'enum E { e = sizeof(const E*) };' */
19563 3978978 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19564 : && (TYPE_VALUES (enumtype)
19565 : || !TYPE_NEXT_VARIANT (enumtype)));
19566 : }
19567 :
19568 : /* Build and install a CONST_DECL for an enumeration constant of the
19569 : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19570 : Apply ATTRIBUTES if available. LOC is the location of NAME.
19571 : Assignment of sequential values by default is handled here. */
19572 :
19573 : tree
19574 12422576 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19575 : location_t loc)
19576 : {
19577 12422576 : tree decl;
19578 12422576 : tree context;
19579 12422576 : tree type;
19580 :
19581 : /* scalar_constant_value will pull out this expression, so make sure
19582 : it's folded as appropriate.
19583 :
19584 : Creating a TARGET_EXPR in a template breaks when substituting, and
19585 : here we would create it for instance when using a class prvalue with
19586 : a user-defined conversion function. So don't use such a tree. We
19587 : instantiate VALUE here to get errors about bad enumerators even in
19588 : a template that does not get instantiated. */
19589 12422576 : if (processing_template_decl)
19590 447357 : value = maybe_fold_non_dependent_expr (value);
19591 :
19592 : /* If the VALUE was erroneous, pretend it wasn't there; that will
19593 : result in the enum being assigned the next value in sequence. */
19594 12422576 : if (value == error_mark_node)
19595 : value = NULL_TREE;
19596 :
19597 : /* Remove no-op casts from the value. */
19598 12422538 : if (value)
19599 9528468 : STRIP_TYPE_NOPS (value);
19600 :
19601 12422576 : if (! processing_template_decl)
19602 : {
19603 : /* Validate and default VALUE. */
19604 11975219 : if (value != NULL_TREE)
19605 : {
19606 5448674 : if (!ENUM_UNDERLYING_TYPE (enumtype))
19607 : {
19608 2767600 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19609 : value, true);
19610 2767600 : if (tmp_value)
19611 5448674 : value = tmp_value;
19612 : }
19613 2681074 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19614 : (TREE_TYPE (value)))
19615 39 : value = perform_implicit_conversion_flags
19616 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19617 : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19618 :
19619 5448674 : if (value == error_mark_node)
19620 : value = NULL_TREE;
19621 :
19622 5448653 : if (value != NULL_TREE)
19623 : {
19624 5448653 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19625 : (TREE_TYPE (value)))
19626 : {
19627 17 : error_at (cp_expr_loc_or_input_loc (value),
19628 : "enumerator value for %qD must have integral or "
19629 : "unscoped enumeration type", name);
19630 17 : value = NULL_TREE;
19631 : }
19632 : else
19633 : {
19634 5448636 : value = cxx_constant_value (value);
19635 :
19636 5448636 : if (TREE_CODE (value) != INTEGER_CST)
19637 : {
19638 48 : error ("enumerator value for %qD is not an integer "
19639 : "constant", name);
19640 48 : value = NULL_TREE;
19641 : }
19642 : }
19643 : }
19644 : }
19645 :
19646 : /* Default based on previous value. */
19647 65 : if (value == NULL_TREE)
19648 : {
19649 6526631 : if (TYPE_VALUES (enumtype))
19650 : {
19651 5975204 : tree prev_value;
19652 :
19653 : /* C++03 7.2/4: If no initializer is specified for the first
19654 : enumerator, the type is an unspecified integral
19655 : type. Otherwise the type is the same as the type of the
19656 : initializing value of the preceding enumerator unless the
19657 : incremented value is not representable in that type, in
19658 : which case the type is an unspecified integral type
19659 : sufficient to contain the incremented value. */
19660 5975204 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19661 5975204 : if (TREE_CODE (prev_value) != INTEGER_CST)
19662 2 : value = error_mark_node;
19663 : else
19664 : {
19665 5975202 : wi::overflow_type overflowed;
19666 5975202 : tree type = TREE_TYPE (prev_value);
19667 5975202 : signop sgn = TYPE_SIGN (type);
19668 5975202 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
19669 5975202 : &overflowed);
19670 5975202 : if (!overflowed)
19671 : {
19672 5975202 : bool pos = !wi::neg_p (wi, sgn);
19673 5975202 : if (!wi::fits_to_tree_p (wi, type))
19674 : {
19675 : unsigned int itk;
19676 53 : for (itk = itk_int; itk != itk_none; itk++)
19677 : {
19678 53 : type = integer_types[itk];
19679 53 : if (type != NULL_TREE
19680 53 : && (pos || !TYPE_UNSIGNED (type))
19681 106 : && wi::fits_to_tree_p (wi, type))
19682 : break;
19683 : }
19684 17 : if (type && cxx_dialect < cxx11
19685 6 : && itk > itk_unsigned_long)
19686 3 : pedwarn (input_location, OPT_Wlong_long,
19687 : pos ? G_("\
19688 : incremented enumerator value is too large for %<unsigned long%>") : G_("\
19689 : incremented enumerator value is too large for %<long%>"));
19690 : }
19691 5975202 : if (type == NULL_TREE)
19692 0 : overflowed = wi::OVF_UNKNOWN;
19693 : else
19694 5975202 : value = wide_int_to_tree (type, wi);
19695 : }
19696 :
19697 5975202 : if (overflowed)
19698 : {
19699 0 : error ("overflow in enumeration values at %qD", name);
19700 0 : value = error_mark_node;
19701 : }
19702 5975202 : }
19703 : }
19704 : else
19705 551427 : value = integer_zero_node;
19706 : }
19707 :
19708 : /* Remove no-op casts from the value. */
19709 11975219 : STRIP_TYPE_NOPS (value);
19710 :
19711 : /* If the underlying type of the enum is fixed, check whether
19712 : the enumerator values fits in the underlying type. If it
19713 : does not fit, the program is ill-formed [C++0x dcl.enum]. */
19714 11975219 : if (ENUM_UNDERLYING_TYPE (enumtype)
19715 : && value
19716 11975219 : && TREE_CODE (value) == INTEGER_CST)
19717 : {
19718 4201563 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19719 9 : error ("enumerator value %qE is outside the range of underlying "
19720 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19721 :
19722 : /* Convert the value to the appropriate type. */
19723 4201563 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19724 : }
19725 : }
19726 :
19727 : /* C++ associates enums with global, function, or class declarations. */
19728 12422576 : context = current_scope ();
19729 :
19730 : /* Build the actual enumeration constant. Note that the enumeration
19731 : constants have the underlying type of the enum (if it is fixed)
19732 : or the type of their initializer (if the underlying type of the
19733 : enum is not fixed):
19734 :
19735 : [ C++0x dcl.enum ]
19736 :
19737 : If the underlying type is fixed, the type of each enumerator
19738 : prior to the closing brace is the underlying type; if the
19739 : initializing value of an enumerator cannot be represented by
19740 : the underlying type, the program is ill-formed. If the
19741 : underlying type is not fixed, the type of each enumerator is
19742 : the type of its initializing value.
19743 :
19744 : If the underlying type is not fixed, it will be computed by
19745 : finish_enum and we will reset the type of this enumerator. Of
19746 : course, if we're processing a template, there may be no value. */
19747 12422576 : type = value ? TREE_TYPE (value) : NULL_TREE;
19748 :
19749 12422576 : decl = build_decl (loc, CONST_DECL, name, type);
19750 :
19751 12422576 : DECL_CONTEXT (decl) = enumtype;
19752 12422576 : TREE_CONSTANT (decl) = 1;
19753 12422576 : TREE_READONLY (decl) = 1;
19754 12422576 : DECL_INITIAL (decl) = value;
19755 :
19756 12422576 : if (attributes)
19757 146492 : cplus_decl_attributes (&decl, attributes, 0);
19758 :
19759 12422576 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19760 : {
19761 : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19762 : on the TYPE_FIELDS list for `S'. (That's so that you can say
19763 : things like `S::i' later.) */
19764 :
19765 : /* The enumerator may be getting declared outside of its enclosing
19766 : class, like so:
19767 :
19768 : class S { public: enum E : int; }; enum S::E : int { i = 7; };
19769 :
19770 : For which case we need to make sure that the access of `S::i'
19771 : matches the access of `S::E'. */
19772 2159336 : auto cas = make_temp_override (current_access_specifier);
19773 2159336 : set_current_access_from_decl (TYPE_NAME (enumtype));
19774 2159336 : finish_member_declaration (decl);
19775 2159336 : }
19776 : else
19777 10263240 : pushdecl (decl);
19778 :
19779 : /* Add this enumeration constant to the list for this type. */
19780 12422576 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19781 :
19782 12422576 : return decl;
19783 : }
19784 :
19785 : /* Look for an enumerator with the given NAME within the enumeration
19786 : type ENUMTYPE. This routine is used primarily for qualified name
19787 : lookup into an enumerator in C++0x, e.g.,
19788 :
19789 : enum class Color { Red, Green, Blue };
19790 :
19791 : Color color = Color::Red;
19792 :
19793 : Returns the value corresponding to the enumerator, or
19794 : NULL_TREE if no such enumerator was found. */
19795 : tree
19796 29784863 : lookup_enumerator (tree enumtype, tree name)
19797 : {
19798 29784863 : tree e;
19799 29784863 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19800 :
19801 29784863 : e = purpose_member (name, TYPE_VALUES (enumtype));
19802 59569628 : return e? TREE_VALUE (e) : NULL_TREE;
19803 : }
19804 :
19805 : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19806 :
19807 : tree
19808 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
19809 : vec<string_int_pair> *values)
19810 : {
19811 0 : location_t saved_loc = input_location;
19812 0 : input_location = loc;
19813 :
19814 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19815 : NULL_TREE, false, NULL);
19816 0 : if (!OPAQUE_ENUM_P (enumtype))
19817 : {
19818 0 : auto_diagnostic_group d;
19819 0 : error_at (loc, "multiple definition of %q#T", enumtype);
19820 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19821 : "previous definition here");
19822 0 : return enumtype;
19823 0 : }
19824 0 : SET_OPAQUE_ENUM_P (enumtype, false);
19825 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19826 :
19827 0 : for (const string_int_pair &value : values)
19828 0 : build_enumerator (get_identifier (value.first),
19829 0 : build_int_cst (integer_type_node, value.second),
19830 : enumtype, NULL_TREE, loc);
19831 :
19832 0 : finish_enum_value_list (enumtype);
19833 0 : finish_enum (enumtype);
19834 :
19835 0 : input_location = saved_loc;
19836 0 : return enumtype;
19837 : }
19838 :
19839 : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19840 :
19841 : tree
19842 0 : cxx_simulate_record_decl (location_t loc, const char *name,
19843 : array_slice<const tree> fields)
19844 : {
19845 0 : iloc_sentinel ils (loc);
19846 :
19847 0 : tree ident = get_identifier (name);
19848 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
19849 0 : if (type != error_mark_node
19850 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19851 : {
19852 0 : error ("redefinition of %q#T", type);
19853 0 : type = error_mark_node;
19854 : }
19855 0 : if (type == error_mark_node)
19856 0 : return lhd_simulate_record_decl (loc, name, fields);
19857 :
19858 0 : xref_basetypes (type, NULL_TREE);
19859 0 : type = begin_class_definition (type);
19860 0 : if (type == error_mark_node)
19861 0 : return lhd_simulate_record_decl (loc, name, fields);
19862 :
19863 0 : for (tree field : fields)
19864 0 : finish_member_declaration (field);
19865 :
19866 0 : type = finish_struct (type, NULL_TREE);
19867 :
19868 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
19869 0 : set_underlying_type (decl);
19870 0 : lang_hooks.decls.pushdecl (decl);
19871 :
19872 0 : return type;
19873 0 : }
19874 :
19875 : /* We're defining DECL. Make sure that its type is OK. */
19876 :
19877 : static void
19878 169657740 : check_function_type (tree decl, tree current_function_parms)
19879 : {
19880 169657740 : tree fntype = TREE_TYPE (decl);
19881 169657740 : tree return_type = complete_type (TREE_TYPE (fntype));
19882 :
19883 : /* In a function definition, arg types must be complete. */
19884 169657740 : require_complete_types_for_parms (current_function_parms);
19885 :
19886 169657740 : if (dependent_type_p (return_type)
19887 169657740 : || type_uses_auto (return_type))
19888 : return;
19889 115881527 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19890 : {
19891 12 : tree args = TYPE_ARG_TYPES (fntype);
19892 :
19893 12 : error ("return type %q#T is incomplete", return_type);
19894 :
19895 : /* Make it return void instead. */
19896 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
19897 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19898 : void_type_node,
19899 3 : TREE_CHAIN (args));
19900 : else
19901 18 : fntype = build_function_type (void_type_node, args,
19902 9 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19903 12 : fntype = (cp_build_type_attribute_variant
19904 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19905 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19906 12 : TREE_TYPE (decl) = fntype;
19907 : }
19908 : else
19909 : {
19910 115881515 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
19911 115881515 : maybe_warn_parm_abi (TREE_TYPE (fntype),
19912 115881515 : DECL_SOURCE_LOCATION (decl));
19913 : }
19914 : }
19915 :
19916 : /* True iff FN is an implicitly-defined default constructor. */
19917 :
19918 : static bool
19919 17674648 : implicit_default_ctor_p (tree fn)
19920 : {
19921 17674648 : return (DECL_CONSTRUCTOR_P (fn)
19922 17674648 : && !user_provided_p (fn)
19923 19667022 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19924 : }
19925 :
19926 : /* Clobber the contents of *this to let the back end know that the object
19927 : storage is dead when we enter the constructor or leave the destructor. */
19928 :
19929 : static tree
19930 1545395 : build_clobber_this (clobber_kind kind)
19931 : {
19932 : /* Clobbering an empty base is pointless, and harmful if its one byte
19933 : TYPE_SIZE overlays real data. */
19934 1545395 : if (is_empty_class (current_class_type))
19935 0 : return void_node;
19936 :
19937 : /* If we have virtual bases, clobber the whole object, but only if we're in
19938 : charge. If we don't have virtual bases, clobber the as-base type so we
19939 : don't mess with tail padding. */
19940 1545395 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19941 :
19942 1545395 : tree ctype = current_class_type;
19943 1545395 : if (!vbases)
19944 1537496 : ctype = CLASSTYPE_AS_BASE (ctype);
19945 :
19946 1545395 : tree clobber = build_clobber (ctype, kind);
19947 :
19948 1545395 : tree thisref = current_class_ref;
19949 1545395 : if (ctype != current_class_type)
19950 : {
19951 176912 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19952 176912 : thisref = convert_from_reference (thisref);
19953 : }
19954 :
19955 1545395 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19956 1545395 : if (kind == CLOBBER_OBJECT_BEGIN)
19957 0 : TREE_SET_CODE (exprstmt, INIT_EXPR);
19958 1545395 : if (vbases)
19959 7899 : exprstmt = build_if_in_charge (exprstmt);
19960 :
19961 : return exprstmt;
19962 : }
19963 :
19964 : /* Create the FUNCTION_DECL for a function definition.
19965 : DECLSPECS and DECLARATOR are the parts of the declaration;
19966 : they describe the function's name and the type it returns,
19967 : but twisted together in a fashion that parallels the syntax of C.
19968 :
19969 : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19970 : DECLARATOR is really the DECL for the function we are about to
19971 : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19972 : indicating that the function is an inline defined in-class.
19973 :
19974 : This function creates a binding context for the function body
19975 : as well as setting up the FUNCTION_DECL in current_function_decl.
19976 :
19977 : For C++, we must first check whether that datum makes any sense.
19978 : For example, "class A local_a(1,2);" means that variable local_a
19979 : is an aggregate of type A, which should have a constructor
19980 : applied to it with the argument list [1, 2].
19981 :
19982 : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19983 : or may be a BLOCK if the function has been defined previously
19984 : in this translation unit. On exit, DECL_INITIAL (decl1) will be
19985 : error_mark_node if the function has never been defined, or
19986 : a BLOCK if the function has been defined somewhere. */
19987 :
19988 : bool
19989 169657776 : start_preparsed_function (tree decl1, tree attrs, int flags)
19990 : {
19991 169657776 : tree ctype = NULL_TREE;
19992 169657776 : bool doing_friend = false;
19993 :
19994 : /* Sanity check. */
19995 169657776 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19996 169657776 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19997 :
19998 169657776 : tree fntype = TREE_TYPE (decl1);
19999 169657776 : if (DECL_CLASS_SCOPE_P (decl1))
20000 125179558 : ctype = DECL_CONTEXT (decl1);
20001 : else
20002 : {
20003 88956436 : ctype = DECL_FRIEND_CONTEXT (decl1);
20004 :
20005 44478217 : if (ctype)
20006 169657776 : doing_friend = true;
20007 : }
20008 :
20009 : /* Adjust for #pragma target/optimize if this is an artificial function that
20010 : (probably) didn't go through grokfndecl. We particularly don't want this
20011 : for deferred instantiations, which should match their template. */
20012 169657776 : if (DECL_ARTIFICIAL (decl1))
20013 3988841 : decl_attributes (&decl1, NULL_TREE, 0);
20014 :
20015 169657776 : if (DECL_DECLARED_INLINE_P (decl1)
20016 169657776 : && lookup_attribute ("noinline", attrs))
20017 : {
20018 0 : auto_urlify_attributes sentinel;
20019 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
20020 : "inline function %qD given attribute %qs", decl1, "noinline");
20021 0 : }
20022 :
20023 : /* Handle gnu_inline attribute. */
20024 169657776 : if (GNU_INLINE_P (decl1))
20025 : {
20026 2017670 : DECL_EXTERNAL (decl1) = 1;
20027 2017670 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
20028 2017670 : DECL_INTERFACE_KNOWN (decl1) = 1;
20029 2017670 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
20030 : }
20031 :
20032 169657776 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
20033 : /* This is a constructor, we must ensure that any default args
20034 : introduced by this definition are propagated to the clones
20035 : now. The clones are used directly in overload resolution. */
20036 21134501 : adjust_clone_args (decl1);
20037 :
20038 : /* Sometimes we don't notice that a function is a static member, and
20039 : build a METHOD_TYPE for it. Fix that up now. */
20040 169657776 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
20041 : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
20042 :
20043 : /* Set up current_class_type, and enter the scope of the class, if
20044 : appropriate. */
20045 169657776 : if (ctype)
20046 130024244 : push_nested_class (ctype);
20047 :
20048 : /* Now that we have entered the scope of the class, we must restore
20049 : the bindings for any template parameters surrounding DECL1, if it
20050 : is an inline member template. (Order is important; consider the
20051 : case where a template parameter has the same name as a field of
20052 : the class.) It is not until after this point that
20053 : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
20054 169657776 : if (flags & SF_INCLASS_INLINE)
20055 87205438 : maybe_begin_member_template_processing (decl1);
20056 :
20057 : /* Effective C++ rule 15. */
20058 169657776 : if (warn_ecpp
20059 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
20060 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
20061 169657806 : && VOID_TYPE_P (TREE_TYPE (fntype)))
20062 0 : warning (OPT_Weffc__,
20063 : "%<operator=%> should return a reference to %<*this%>");
20064 :
20065 : /* Make the init_value nonzero so pushdecl knows this is not tentative.
20066 : error_mark_node is replaced below (in poplevel) with the BLOCK. */
20067 169657776 : if (!DECL_INITIAL (decl1))
20068 36822324 : DECL_INITIAL (decl1) = error_mark_node;
20069 :
20070 : /* This function exists in static storage.
20071 : (This does not mean `static' in the C sense!) */
20072 169657776 : TREE_STATIC (decl1) = 1;
20073 :
20074 : /* We must call push_template_decl after current_class_type is set
20075 : up. (If we are processing inline definitions after exiting a
20076 : class scope, current_class_type will be NULL_TREE until set above
20077 : by push_nested_class.) */
20078 169657776 : if (processing_template_decl)
20079 : {
20080 99629692 : tree newdecl1 = push_template_decl (decl1, doing_friend);
20081 99629692 : if (newdecl1 == error_mark_node)
20082 : {
20083 36 : if (ctype)
20084 9 : pop_nested_class ();
20085 : return false;
20086 : }
20087 99629656 : decl1 = newdecl1;
20088 : }
20089 :
20090 : /* Make sure the parameter and return types are reasonable. When
20091 : you declare a function, these types can be incomplete, but they
20092 : must be complete when you define the function. */
20093 169657740 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
20094 :
20095 : /* Build the return declaration for the function. */
20096 169657740 : tree restype = TREE_TYPE (fntype);
20097 :
20098 169657740 : if (DECL_RESULT (decl1) == NULL_TREE)
20099 : {
20100 : /* In a template instantiation, copy the return type location. When
20101 : parsing, the location will be set in grokdeclarator. */
20102 70006380 : location_t loc = input_location;
20103 70006380 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
20104 : {
20105 33156026 : tree tmpl = template_for_substitution (decl1);
20106 33156026 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
20107 32192808 : loc = DECL_SOURCE_LOCATION (res);
20108 : }
20109 :
20110 70006380 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
20111 70006380 : DECL_ARTIFICIAL (resdecl) = 1;
20112 70006380 : DECL_IGNORED_P (resdecl) = 1;
20113 70006380 : DECL_RESULT (decl1) = resdecl;
20114 :
20115 70006380 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
20116 : }
20117 :
20118 : /* Record the decl so that the function name is defined.
20119 : If we already have a decl for this name, and it is a FUNCTION_DECL,
20120 : use the old decl. */
20121 169657740 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
20122 : {
20123 : /* A specialization is not used to guide overload resolution. */
20124 20941634 : if (!DECL_FUNCTION_MEMBER_P (decl1)
20125 20921609 : && !(DECL_USE_TEMPLATE (decl1) &&
20126 13738 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
20127 : {
20128 10028347 : tree olddecl = pushdecl (decl1);
20129 :
20130 10028347 : if (olddecl == error_mark_node)
20131 : /* If something went wrong when registering the declaration,
20132 : use DECL1; we have to have a FUNCTION_DECL to use when
20133 : parsing the body of the function. */
20134 : ;
20135 : else
20136 : {
20137 : /* Otherwise, OLDDECL is either a previous declaration
20138 : of the same function or DECL1 itself. */
20139 :
20140 10028134 : if (warn_missing_declarations
20141 21 : && olddecl == decl1
20142 15 : && !DECL_MAIN_P (decl1)
20143 15 : && TREE_PUBLIC (decl1)
20144 10028146 : && !DECL_DECLARED_INLINE_P (decl1))
20145 : {
20146 9 : tree context;
20147 :
20148 : /* Check whether DECL1 is in an anonymous
20149 : namespace. */
20150 9 : for (context = DECL_CONTEXT (decl1);
20151 18 : context;
20152 9 : context = DECL_CONTEXT (context))
20153 : {
20154 12 : if (TREE_CODE (context) == NAMESPACE_DECL
20155 12 : && DECL_NAME (context) == NULL_TREE)
20156 : break;
20157 : }
20158 :
20159 9 : if (context == NULL)
20160 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
20161 6 : OPT_Wmissing_declarations,
20162 : "no previous declaration for %qD", decl1);
20163 : }
20164 :
20165 10028134 : decl1 = olddecl;
20166 : }
20167 : }
20168 : else
20169 : {
20170 : /* We need to set the DECL_CONTEXT. */
20171 851177 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
20172 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
20173 : }
20174 10879524 : fntype = TREE_TYPE (decl1);
20175 10879524 : restype = TREE_TYPE (fntype);
20176 :
20177 : /* If #pragma weak applies, mark the decl appropriately now.
20178 : The pragma only applies to global functions. Because
20179 : determining whether or not the #pragma applies involves
20180 : computing the mangled name for the declaration, we cannot
20181 : apply the pragma until after we have merged this declaration
20182 : with any previous declarations; if the original declaration
20183 : has a linkage specification, that specification applies to
20184 : the definition as well, and may affect the mangled name. */
20185 10879524 : if (DECL_FILE_SCOPE_P (decl1))
20186 2582210 : maybe_apply_pragma_weak (decl1);
20187 : }
20188 :
20189 : /* We are now in the scope of the function being defined. */
20190 169657740 : current_function_decl = decl1;
20191 :
20192 : /* Save the parm names or decls from this function's declarator
20193 : where store_parm_decls will find them. */
20194 169657740 : tree current_function_parms = DECL_ARGUMENTS (decl1);
20195 :
20196 : /* Let the user know we're compiling this function. */
20197 169657740 : announce_function (decl1);
20198 :
20199 169657740 : gcc_assert (DECL_INITIAL (decl1));
20200 :
20201 : /* This function may already have been parsed, in which case just
20202 : return; our caller will skip over the body without parsing. */
20203 169657740 : if (DECL_INITIAL (decl1) != error_mark_node)
20204 : return true;
20205 :
20206 : /* Initialize RTL machinery. We cannot do this until
20207 : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
20208 : even when processing a template; this is how we get
20209 : CFUN set up, and our per-function variables initialized.
20210 : FIXME factor out the non-RTL stuff. */
20211 169652918 : cp_binding_level *bl = current_binding_level;
20212 169652918 : allocate_struct_function (decl1, processing_template_decl);
20213 :
20214 : /* Initialize the language data structures. Whenever we start
20215 : a new function, we destroy temporaries in the usual way. */
20216 169652918 : cfun->language = ggc_cleared_alloc<language_function> ();
20217 169652918 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
20218 169652918 : current_binding_level = bl;
20219 :
20220 : /* If we are (erroneously) defining a function that we have already
20221 : defined before, wipe out what we knew before. */
20222 169652918 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
20223 169652918 : FNDECL_USED_AUTO (decl1) = false;
20224 169652918 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
20225 :
20226 169652918 : if (!processing_template_decl && type_uses_auto (restype))
20227 : {
20228 1067637 : FNDECL_USED_AUTO (decl1) = true;
20229 1067637 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
20230 : }
20231 :
20232 : /* Start the statement-tree, start the tree now. */
20233 169652918 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
20234 :
20235 169652918 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
20236 : {
20237 : /* We know that this was set up by `grokclassfn'. We do not
20238 : wait until `store_parm_decls', since evil parse errors may
20239 : never get us to that point. Here we keep the consistency
20240 : between `current_class_type' and `current_class_ptr'. */
20241 110116369 : tree t = DECL_ARGUMENTS (decl1);
20242 :
20243 110116369 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
20244 110116369 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
20245 :
20246 110116369 : cp_function_chain->x_current_class_ref
20247 110116369 : = cp_build_fold_indirect_ref (t);
20248 : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
20249 110116369 : cp_function_chain->x_current_class_ptr = t;
20250 :
20251 : /* Constructors and destructors need to know whether they're "in
20252 : charge" of initializing virtual base classes. */
20253 110116369 : t = DECL_CHAIN (t);
20254 110116369 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
20255 : {
20256 18549 : current_in_charge_parm = t;
20257 18549 : t = DECL_CHAIN (t);
20258 : }
20259 110116369 : if (DECL_HAS_VTT_PARM_P (decl1))
20260 : {
20261 37153 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
20262 37153 : current_vtt_parm = t;
20263 : }
20264 : }
20265 :
20266 306131302 : bool honor_interface = (!DECL_TEMPLOID_INSTANTIATION (decl1)
20267 : /* Implicitly-defined methods (like the
20268 : destructor for a class in which no destructor
20269 : is explicitly declared) must not be defined
20270 : until their definition is needed. So, we
20271 : ignore interface specifications for
20272 : compiler-generated functions. */
20273 206437085 : && !DECL_ARTIFICIAL (decl1));
20274 169652918 : struct c_fileinfo *finfo
20275 169652918 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
20276 :
20277 169652918 : if (processing_template_decl)
20278 : /* Don't mess with interface flags. */;
20279 70026710 : else if (DECL_INTERFACE_KNOWN (decl1))
20280 : {
20281 20458920 : tree ctx = decl_function_context (decl1);
20282 :
20283 20458920 : if (DECL_NOT_REALLY_EXTERN (decl1))
20284 16298951 : DECL_EXTERNAL (decl1) = 0;
20285 :
20286 20458920 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
20287 : /* This is a function in a local class in an extern inline
20288 : or template function. */
20289 3219212 : comdat_linkage (decl1);
20290 : }
20291 : /* If this function belongs to an interface, it is public.
20292 : If it belongs to someone else's interface, it is also external.
20293 : This only affects inlines and template instantiations. */
20294 49567790 : else if (!finfo->interface_unknown && honor_interface)
20295 : {
20296 106 : if (DECL_DECLARED_INLINE_P (decl1)
20297 106 : || DECL_TEMPLOID_INSTANTIATION (decl1))
20298 : {
20299 82 : DECL_EXTERNAL (decl1)
20300 164 : = (finfo->interface_only
20301 82 : || (DECL_DECLARED_INLINE_P (decl1)
20302 36 : && ! flag_implement_inlines
20303 0 : && !DECL_VINDEX (decl1)));
20304 :
20305 : /* For WIN32 we also want to put these in linkonce sections. */
20306 82 : maybe_make_one_only (decl1);
20307 : }
20308 : else
20309 24 : DECL_EXTERNAL (decl1) = 0;
20310 106 : DECL_INTERFACE_KNOWN (decl1) = 1;
20311 : /* If this function is in an interface implemented in this file,
20312 : make sure that the back end knows to emit this function
20313 : here. */
20314 106 : if (!DECL_EXTERNAL (decl1))
20315 60 : mark_needed (decl1);
20316 : }
20317 49567684 : else if (finfo->interface_unknown && finfo->interface_only
20318 0 : && honor_interface)
20319 : {
20320 : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
20321 : interface, we will have both finfo->interface_unknown and
20322 : finfo->interface_only set. In that case, we don't want to
20323 : use the normal heuristics because someone will supply a
20324 : #pragma implementation elsewhere, and deducing it here would
20325 : produce a conflict. */
20326 0 : comdat_linkage (decl1);
20327 0 : DECL_EXTERNAL (decl1) = 0;
20328 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
20329 0 : DECL_DEFER_OUTPUT (decl1) = 1;
20330 : }
20331 : else
20332 : {
20333 : /* This is a definition, not a reference.
20334 : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
20335 49567684 : if (!GNU_INLINE_P (decl1))
20336 49567681 : DECL_EXTERNAL (decl1) = 0;
20337 :
20338 49567684 : if ((DECL_DECLARED_INLINE_P (decl1)
20339 1931375 : || DECL_TEMPLOID_INSTANTIATION (decl1))
20340 51255417 : && ! DECL_INTERFACE_KNOWN (decl1))
20341 49324042 : DECL_DEFER_OUTPUT (decl1) = 1;
20342 : else
20343 243642 : DECL_INTERFACE_KNOWN (decl1) = 1;
20344 : }
20345 :
20346 : /* Determine the ELF visibility attribute for the function. We must not
20347 : do this before calling "pushdecl", as we must allow "duplicate_decls"
20348 : to merge any attributes appropriately. We also need to wait until
20349 : linkage is set. */
20350 169652918 : if (!DECL_CLONED_FUNCTION_P (decl1))
20351 152842876 : determine_visibility (decl1);
20352 :
20353 169652918 : if (!processing_template_decl)
20354 70026710 : maybe_instantiate_noexcept (decl1);
20355 :
20356 169652918 : begin_scope (sk_function_parms, decl1);
20357 :
20358 169652918 : ++function_depth;
20359 :
20360 169652918 : start_fname_decls ();
20361 :
20362 169652918 : store_parm_decls (current_function_parms);
20363 :
20364 169652918 : start_function_contracts (decl1);
20365 :
20366 169652918 : if (!processing_template_decl
20367 70026710 : && flag_lifetime_dse > 1
20368 140044038 : && DECL_CONSTRUCTOR_P (decl1)
20369 : /* Clobbering an empty base is harmful if it overlays real data. */
20370 19632458 : && !is_empty_class (current_class_type)
20371 : /* We can't clobber safely for an implicitly-defined default constructor
20372 : because part of the initialization might happen before we enter the
20373 : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
20374 17674003 : && !implicit_default_ctor_p (decl1)
20375 186101159 : && !lookup_attribute ("clobber *this",
20376 16448241 : DECL_ATTRIBUTES (current_class_ptr)))
20377 16438973 : DECL_ATTRIBUTES (current_class_ptr)
20378 32877946 : = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
20379 16438973 : DECL_ATTRIBUTES (current_class_ptr));
20380 :
20381 169652918 : if (!processing_template_decl
20382 140053420 : && DECL_CONSTRUCTOR_P (decl1)
20383 19633706 : && sanitize_flags_p (SANITIZE_VPTR)
20384 1935 : && !DECL_CLONED_FUNCTION_P (decl1)
20385 169653563 : && !implicit_default_ctor_p (decl1))
20386 485 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
20387 :
20388 169652918 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
20389 169652345 : start_lambda_scope (decl1);
20390 :
20391 : return true;
20392 : }
20393 :
20394 :
20395 : /* Like start_preparsed_function, except that instead of a
20396 : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
20397 :
20398 : Returns true on success. If the DECLARATOR is not suitable
20399 : for a function, we return false, which tells the parser to
20400 : skip the entire function. */
20401 :
20402 : bool
20403 40628592 : start_function (cp_decl_specifier_seq *declspecs,
20404 : const cp_declarator *declarator,
20405 : tree attrs)
20406 : {
20407 40628592 : tree decl1;
20408 :
20409 40628592 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
20410 40628592 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
20411 40628592 : if (decl1 == error_mark_node)
20412 : return false;
20413 :
20414 40628226 : if (DECL_MAIN_P (decl1))
20415 : /* main must return int. grokfndecl should have corrected it
20416 : (and issued a diagnostic) if the user got it wrong. */
20417 34067 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
20418 : integer_type_node));
20419 :
20420 40628226 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
20421 : }
20422 :
20423 : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
20424 : FN. */
20425 :
20426 : bool
20427 324919103 : use_eh_spec_block (tree fn)
20428 : {
20429 322939935 : return (flag_exceptions && flag_enforce_eh_specs
20430 322939935 : && !processing_template_decl
20431 : /* We insert the EH_SPEC_BLOCK only in the original
20432 : function; then, it is copied automatically to the
20433 : clones. */
20434 122324798 : && !DECL_CLONED_FUNCTION_P (fn)
20435 : /* Implicitly-generated constructors and destructors have
20436 : exception specifications. However, those specifications
20437 : are the union of the possible exceptions specified by the
20438 : constructors/destructors for bases and members, so no
20439 : unallowed exception will ever reach this function. By
20440 : not creating the EH_SPEC_BLOCK we save a little memory,
20441 : and we avoid spurious warnings about unreachable
20442 : code. */
20443 105556535 : && !DECL_DEFAULTED_FN (fn)
20444 427878084 : && !type_throw_all_p (TREE_TYPE (fn)));
20445 : }
20446 :
20447 : /* Helper function to push ARGS into the current lexical scope. DECL
20448 : is the function declaration. NONPARMS is used to handle enum
20449 : constants. */
20450 :
20451 : void
20452 164637210 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
20453 : {
20454 : /* If we're doing semantic analysis, then we'll call pushdecl
20455 : for each of these. We must do them in reverse order so that
20456 : they end in the correct forward order. */
20457 164637210 : args = nreverse (args);
20458 :
20459 164637210 : tree next;
20460 669618332 : for (tree parm = args; parm; parm = next)
20461 : {
20462 340343912 : next = DECL_CHAIN (parm);
20463 340343912 : if (TREE_CODE (parm) == PARM_DECL)
20464 340343912 : pushdecl (parm);
20465 0 : else if (nonparms)
20466 : {
20467 : /* If we find an enum constant or a type tag, put it aside for
20468 : the moment. */
20469 0 : TREE_CHAIN (parm) = NULL_TREE;
20470 0 : *nonparms = chainon (*nonparms, parm);
20471 : }
20472 : }
20473 :
20474 : /* Get the decls in their original chain order and record in the
20475 : function. This is all and only the PARM_DECLs that were
20476 : pushed into scope by the loop above. */
20477 164637210 : DECL_ARGUMENTS (decl) = get_local_decls ();
20478 164637210 : }
20479 :
20480 : /* Store the parameter declarations into the current function declaration.
20481 : This is called after parsing the parameter declarations, before
20482 : digesting the body of the function.
20483 :
20484 : Also install to binding contour return value identifier, if any. */
20485 :
20486 : static void
20487 169652918 : store_parm_decls (tree current_function_parms)
20488 : {
20489 169652918 : tree fndecl = current_function_decl;
20490 :
20491 : /* This is a chain of any other decls that came in among the parm
20492 : declarations. If a parm is declared with enum {foo, bar} x;
20493 : then CONST_DECLs for foo and bar are put here. */
20494 169652918 : tree nonparms = NULL_TREE;
20495 :
20496 169652918 : if (current_function_parms)
20497 : {
20498 : /* This case is when the function was defined with an ANSI prototype.
20499 : The parms already have decls, so we need not do anything here
20500 : except record them as in effect
20501 : and complain if any redundant old-style parm decls were written. */
20502 :
20503 160963963 : tree specparms = current_function_parms;
20504 :
20505 : /* Must clear this because it might contain TYPE_DECLs declared
20506 : at class level. */
20507 160963963 : current_binding_level->names = NULL;
20508 :
20509 160963963 : do_push_parm_decls (fndecl, specparms, &nonparms);
20510 : }
20511 : else
20512 8688955 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
20513 :
20514 : /* Now store the final chain of decls for the arguments
20515 : as the decl-chain of the current lexical scope.
20516 : Put the enumerators in as well, at the front so that
20517 : DECL_ARGUMENTS is not modified. */
20518 169652918 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
20519 :
20520 : /* Register cleanups for parameters with trivial_abi attribute, the cleanup
20521 : of which is the callee's responsibility. */
20522 169652918 : if (!processing_template_decl && !DECL_CLONED_FUNCTION_P (fndecl))
20523 137540327 : for (tree parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
20524 : {
20525 84323659 : if (TREE_CODE (parm) == PARM_DECL)
20526 : {
20527 84323659 : tree parm_type = TREE_TYPE (parm);
20528 84323659 : if (has_trivial_abi_attribute (parm_type))
20529 : {
20530 18 : tree cleanup
20531 18 : = cxx_maybe_build_cleanup (parm, tf_warning_or_error);
20532 18 : if (cleanup && cleanup != error_mark_node)
20533 18 : finish_decl_cleanup (parm, cleanup);
20534 : }
20535 : }
20536 : }
20537 :
20538 169652918 : if (use_eh_spec_block (current_function_decl))
20539 25847351 : current_eh_spec_block = begin_eh_spec_block ();
20540 169652918 : }
20541 :
20542 :
20543 : /* Mark CDTOR's implicit THIS argument for returning, if required by
20544 : the ABI.. Return the decl for THIS, if it is to be returned, and
20545 : NULL otherwise. */
20546 :
20547 : tree
20548 145315936 : maybe_prepare_return_this (tree cdtor)
20549 : {
20550 145315936 : if (targetm.cxx.cdtor_returns_this ())
20551 0 : if (tree val = DECL_ARGUMENTS (cdtor))
20552 : {
20553 0 : suppress_warning (val, OPT_Wuse_after_free_);
20554 0 : return val;
20555 : }
20556 :
20557 : return NULL_TREE;
20558 : }
20559 :
20560 : /* Set the return value of the [cd]tor if the ABI wants that. */
20561 :
20562 : void
20563 8559328 : maybe_return_this ()
20564 : {
20565 8559328 : if (tree val = maybe_prepare_return_this (current_function_decl))
20566 : {
20567 : /* Return the address of the object. */
20568 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
20569 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
20570 0 : DECL_RESULT (current_function_decl), val);
20571 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
20572 0 : add_stmt (exprstmt);
20573 : }
20574 8559328 : }
20575 :
20576 : /* Do all the processing for the beginning of a destructor; set up the
20577 : vtable pointers and cleanups for bases and members. */
20578 :
20579 : static void
20580 1692764 : begin_destructor_body (void)
20581 : {
20582 1692764 : tree compound_stmt;
20583 :
20584 : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20585 : issued an error message. We still want to try to process the
20586 : body of the function, but initialize_vtbl_ptrs will crash if
20587 : TYPE_BINFO is NULL. */
20588 1692764 : if (COMPLETE_TYPE_P (current_class_type))
20589 : {
20590 1692764 : compound_stmt = begin_compound_stmt (0);
20591 : /* Make all virtual function table pointers in non-virtual base
20592 : classes point to CURRENT_CLASS_TYPE's virtual function
20593 : tables. */
20594 1692764 : initialize_vtbl_ptrs (current_class_ptr);
20595 1692764 : finish_compound_stmt (compound_stmt);
20596 :
20597 1692764 : if (flag_lifetime_dse
20598 : /* Clobbering an empty base is harmful if it overlays real data. */
20599 1692764 : && !is_empty_class (current_class_type))
20600 : {
20601 1545458 : if (sanitize_flags_p (SANITIZE_VPTR)
20602 239 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20603 1545521 : && TYPE_CONTAINS_VPTR_P (current_class_type))
20604 : {
20605 63 : tree binfo = TYPE_BINFO (current_class_type);
20606 63 : tree ref
20607 63 : = cp_build_fold_indirect_ref (current_class_ptr);
20608 :
20609 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20610 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20611 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20612 : NOP_EXPR, vtbl,
20613 : tf_warning_or_error);
20614 : /* If the vptr is shared with some virtual nearly empty base,
20615 : don't clear it if not in charge, the dtor of the virtual
20616 : nearly empty base will do that later. */
20617 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
20618 : {
20619 : tree c = current_class_type;
20620 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
20621 : {
20622 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20623 : {
20624 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
20625 : tf_warning_or_error);
20626 36 : stmt = build_if_in_charge (stmt);
20627 36 : break;
20628 : }
20629 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20630 : }
20631 : }
20632 63 : finish_decl_cleanup (NULL_TREE, stmt);
20633 : }
20634 : else
20635 1545395 : finish_decl_cleanup (NULL_TREE,
20636 : build_clobber_this (CLOBBER_OBJECT_END));
20637 : }
20638 :
20639 : /* And insert cleanups for our bases and members so that they
20640 : will be properly destroyed if we throw. */
20641 1692764 : push_base_cleanups ();
20642 : }
20643 1692764 : }
20644 :
20645 : /* Do the necessary processing for the beginning of a function body, which
20646 : in this case includes member-initializers, but not the catch clauses of
20647 : a function-try-block. Currently, this means opening a binding level
20648 : for the member-initializers (in a ctor), member cleanups (in a dtor),
20649 : and capture proxies (in a lambda operator()). */
20650 :
20651 : tree
20652 133478088 : begin_function_body (void)
20653 : {
20654 274812695 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20655 : return NULL_TREE;
20656 :
20657 26076766 : if (processing_template_decl)
20658 : /* Do nothing now. */;
20659 : else
20660 : /* Always keep the BLOCK node associated with the outermost pair of
20661 : curly braces of a function. These are needed for correct
20662 : operation of dwarfout.c. */
20663 8731711 : keep_next_level (true);
20664 :
20665 26076766 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
20666 26076766 : current_binding_level->artificial = 1;
20667 :
20668 26076766 : if (processing_template_decl)
20669 : /* Do nothing now. */;
20670 17463422 : else if (DECL_DESTRUCTOR_P (current_function_decl))
20671 1692764 : begin_destructor_body ();
20672 :
20673 : return stmt;
20674 : }
20675 :
20676 : /* Do the processing for the end of a function body. Currently, this means
20677 : closing out the cleanups for fully-constructed bases and members, and in
20678 : the case of the destructor, deleting the object if desired. Again, this
20679 : is only meaningful for [cd]tors, since they are the only functions where
20680 : there is a significant distinction between the main body and any
20681 : function catch clauses. Handling, say, main() return semantics here
20682 : would be wrong, as flowing off the end of a function catch clause for
20683 : main() would also need to return 0. */
20684 :
20685 : void
20686 133478064 : finish_function_body (tree compstmt)
20687 : {
20688 133478064 : if (compstmt == NULL_TREE)
20689 : return;
20690 :
20691 : /* Close the block. */
20692 26076766 : finish_compound_stmt (compstmt);
20693 :
20694 26076766 : if (processing_template_decl)
20695 : /* Do nothing now. */;
20696 8731711 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
20697 8731711 : || DECL_DESTRUCTOR_P (current_function_decl))
20698 8232828 : maybe_return_this ();
20699 : }
20700 :
20701 : /* Given a function, returns the BLOCK corresponding to the outermost level
20702 : of curly braces, skipping the artificial block created for constructor
20703 : initializers. */
20704 :
20705 : tree
20706 366340 : outer_curly_brace_block (tree fndecl)
20707 : {
20708 366340 : tree block = DECL_INITIAL (fndecl);
20709 366340 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20710 : return block;
20711 1223 : block = BLOCK_SUBBLOCKS (block);
20712 1223 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20713 : return block;
20714 0 : block = BLOCK_SUBBLOCKS (block);
20715 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20716 : return block;
20717 : }
20718 :
20719 : /* If FNDECL is a class's key method, add the class to the list of
20720 : keyed classes that should be emitted. */
20721 :
20722 : static void
20723 169656866 : record_key_method_defined (tree fndecl)
20724 : {
20725 229197339 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20726 110212396 : && DECL_VIRTUAL_P (fndecl)
20727 172546062 : && !processing_template_decl)
20728 : {
20729 1502325 : tree fnclass = DECL_CONTEXT (fndecl);
20730 1502325 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20731 : {
20732 31032 : tree classdecl = TYPE_NAME (fnclass);
20733 : /* Classes attached to a named module are already handled. */
20734 31032 : if (!DECL_LANG_SPECIFIC (classdecl)
20735 31355 : || !DECL_MODULE_ATTACH_P (classdecl))
20736 30987 : vec_safe_push (keyed_classes, fnclass);
20737 : }
20738 : }
20739 169656866 : }
20740 :
20741 : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20742 : of "return *this;" immediately before its location, using FNDECL's
20743 : first statement (if any) to give the indentation, if appropriate. */
20744 :
20745 : static void
20746 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20747 : {
20748 18 : location_t indent = UNKNOWN_LOCATION;
20749 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20750 18 : if (stmts)
20751 9 : indent = EXPR_LOCATION (stmts);
20752 18 : richloc->add_fixit_insert_formatted ("return *this;",
20753 : richloc->get_loc (),
20754 : indent);
20755 18 : }
20756 :
20757 : /* Finish up a function declaration and compile that function
20758 : all the way to assembler language output. The free the storage
20759 : for the function definition. INLINE_P is TRUE if we just
20760 : finished processing the body of an in-class inline function
20761 : definition. (This processing will have taken place after the
20762 : class definition is complete.) */
20763 :
20764 : tree
20765 169652882 : finish_function (bool inline_p)
20766 : {
20767 169652882 : tree fndecl = current_function_decl;
20768 169652882 : tree fntype, ctype = NULL_TREE;
20769 169652882 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
20770 :
20771 : /* When we get some parse errors, we can end up without a
20772 : current_function_decl, so cope. */
20773 169652882 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
20774 0 : return error_mark_node;
20775 :
20776 169652882 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20777 169652309 : finish_lambda_scope ();
20778 :
20779 169652882 : if (c_dialect_objc ())
20780 0 : objc_finish_function ();
20781 :
20782 169652882 : record_key_method_defined (fndecl);
20783 :
20784 169652882 : fntype = TREE_TYPE (fndecl);
20785 :
20786 : /* TREE_READONLY (fndecl) = 1;
20787 : This caused &foo to be of type ptr-to-const-function
20788 : which then got a warning when stored in a ptr-to-function variable. */
20789 :
20790 169652882 : gcc_assert (building_stmt_list_p ());
20791 : /* The current function is being defined, so its DECL_INITIAL should
20792 : be set, and unless there's a multiple definition, it should be
20793 : error_mark_node. */
20794 169652882 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20795 :
20796 169652882 : cp_coroutine_transform *coroutine = nullptr;
20797 169652882 : if (flag_coroutines
20798 166800997 : && !processing_template_decl
20799 68206054 : && DECL_COROUTINE_P (fndecl)
20800 169657892 : && !DECL_RAMP_FN (fndecl))
20801 : {
20802 1722 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20803 : && !DECL_DEFAULTED_FN (fndecl));
20804 1722 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
20805 1722 : if (coroutine && coroutine->cp_valid_coroutine ())
20806 1722 : coroutine->apply_transforms ();
20807 :
20808 : /* We should handle coroutine IFNs in middle end lowering. */
20809 1722 : cfun->coroutine_component = true;
20810 :
20811 : /* Do not try to process the ramp's EH unless outlining succeeded. */
20812 1722 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
20813 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20814 : (TREE_TYPE (fndecl)),
20815 360 : current_eh_spec_block);
20816 :
20817 : /* If outlining succeeded, then add contracts handling if needed. */
20818 1722 : if (coroutine->cp_valid_coroutine ())
20819 1644 : maybe_apply_function_contracts (fndecl);
20820 : }
20821 : else
20822 : /* For a cloned function, we've already got all the code we need;
20823 : there's no need to add any extra bits. */
20824 169651160 : if (!DECL_CLONED_FUNCTION_P (fndecl))
20825 : {
20826 : /* Make it so that `main' always returns 0 by default. */
20827 152841118 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20828 152875170 : && !TREE_THIS_VOLATILE (current_function_decl))
20829 34052 : finish_return_stmt (integer_zero_node);
20830 :
20831 152841118 : if (use_eh_spec_block (current_function_decl))
20832 25846988 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20833 : (TREE_TYPE (current_function_decl)),
20834 25846988 : current_eh_spec_block);
20835 :
20836 152841118 : maybe_apply_function_contracts (current_function_decl);
20837 :
20838 : }
20839 :
20840 : /* If we're saving up tree structure, tie off the function now. */
20841 169652882 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20842 :
20843 169652882 : finish_fname_decls ();
20844 :
20845 : /* This must come after expand_function_end because cleanups might
20846 : have declarations (from inline functions) that need to go into
20847 : this function's blocks. */
20848 :
20849 : /* If the current binding level isn't the outermost binding level
20850 : for this function, either there is a bug, or we have experienced
20851 : syntax errors and the statement tree is malformed. */
20852 169652882 : if (current_binding_level->kind != sk_function_parms)
20853 : {
20854 : /* Make sure we have already experienced errors. */
20855 12 : gcc_assert (errorcount);
20856 :
20857 : /* Throw away the broken statement tree and extra binding
20858 : levels. */
20859 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20860 :
20861 27 : while (current_binding_level->kind != sk_function_parms)
20862 : {
20863 15 : if (current_binding_level->kind == sk_class)
20864 0 : pop_nested_class ();
20865 : else
20866 15 : poplevel (0, 0, 0);
20867 : }
20868 : }
20869 169652882 : poplevel (1, 0, 1);
20870 :
20871 : /* Statements should always be full-expressions at the outermost set
20872 : of curly braces for a function. */
20873 169652882 : gcc_assert (stmts_are_full_exprs_p ());
20874 :
20875 : /* If there are no return statements in a function with auto return type,
20876 : the return type is void. But if the declared type is something like
20877 : auto*, this is an error. */
20878 70026674 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20879 170720519 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20880 : {
20881 148484 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20882 148469 : && !current_function_returns_value
20883 296716 : && !current_function_returns_null)
20884 : {
20885 : /* We haven't applied return type deduction because we haven't
20886 : seen any return statements. Do that now. */
20887 148232 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20888 148232 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20889 : void_node, node, tf_warning_or_error,
20890 : adc_return_type);
20891 :
20892 148232 : apply_deduced_return_type (fndecl, void_type_node);
20893 148232 : fntype = TREE_TYPE (fndecl);
20894 : }
20895 252 : else if (!current_function_returns_value
20896 6 : && !current_function_returns_null)
20897 : {
20898 6 : auto_diagnostic_group d;
20899 12 : error ("no return statements in function returning %qT",
20900 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20901 6 : inform (input_location, "only plain %<auto%> return type can be "
20902 : "deduced to %<void%>");
20903 6 : }
20904 : }
20905 :
20906 169652882 : if (FNDECL_USED_AUTO (fndecl)
20907 169652882 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20908 1067385 : if (location_t fcloc = failed_completion_location (fndecl))
20909 : {
20910 6 : auto_diagnostic_group adg;
20911 6 : if (warning (OPT_Wsfinae_incomplete_,
20912 : "defining %qD, which previously failed to be deduced "
20913 : "in a SFINAE context", fndecl)
20914 6 : && warn_sfinae_incomplete == 1)
20915 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
20916 : "-Wsfinae-incomplete=2");
20917 6 : }
20918 :
20919 : /* Remember that we were in class scope. */
20920 169652882 : if (current_class_name)
20921 130023267 : ctype = current_class_type;
20922 :
20923 169652882 : if (DECL_DELETED_FN (fndecl))
20924 : {
20925 93 : DECL_INITIAL (fndecl) = error_mark_node;
20926 93 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
20927 93 : goto cleanup;
20928 : }
20929 :
20930 169652789 : if (flag_openmp)
20931 326990 : if (tree attr = lookup_attribute ("omp declare variant base",
20932 326990 : DECL_ATTRIBUTES (fndecl)))
20933 276 : omp_declare_variant_finalize (fndecl, attr);
20934 :
20935 : /* Complain if there's just no return statement. */
20936 169652789 : if ((warn_return_type
20937 47381 : || (cxx_dialect >= cxx14
20938 39348 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20939 169623417 : && !VOID_TYPE_P (TREE_TYPE (fntype))
20940 106751319 : && !dependent_type_p (TREE_TYPE (fntype))
20941 54049765 : && !current_function_returns_value && !current_function_returns_null
20942 : /* Don't complain if we abort or throw. */
20943 37914 : && !current_function_returns_abnormally
20944 : /* Don't complain if there's an infinite loop. */
20945 947 : && !current_function_infinite_loop
20946 : /* Don't complain if we are declared noreturn. */
20947 879 : && !TREE_THIS_VOLATILE (fndecl)
20948 873 : && !DECL_NAME (DECL_RESULT (fndecl))
20949 873 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20950 : /* Structor return values (if any) are set by the compiler. */
20951 1270 : && !DECL_CONSTRUCTOR_P (fndecl)
20952 635 : && !DECL_DESTRUCTOR_P (fndecl)
20953 169653424 : && targetm.warn_func_return (fndecl))
20954 : {
20955 629 : gcc_rich_location richloc (input_location);
20956 : /* Potentially add a "return *this;" fix-it hint for
20957 : assignment operators. */
20958 629 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20959 : {
20960 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20961 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
20962 32 : && current_class_ref
20963 26 : && same_type_ignoring_top_level_qualifiers_p
20964 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20965 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
20966 18 : add_return_star_this_fixit (&richloc, fndecl);
20967 : }
20968 629 : if (cxx_dialect >= cxx14
20969 629 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
20970 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
20971 : "returning non-void");
20972 627 : else if (warning_at (&richloc, OPT_Wreturn_type,
20973 : "no return statement in function returning "
20974 : "non-void"))
20975 169 : suppress_warning (fndecl, OPT_Wreturn_type);
20976 629 : }
20977 :
20978 : /* Lambda closure members are implicitly constexpr if possible. */
20979 169652789 : if (cxx_dialect >= cxx17
20980 293539527 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20981 2043784 : DECL_DECLARED_CONSTEXPR_P (fndecl)
20982 2043784 : = ((processing_template_decl
20983 660740 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
20984 4086688 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20985 :
20986 : /* Invoke the pre-genericize plugin before we start munging things. */
20987 169652789 : if (!processing_template_decl)
20988 70026581 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
20989 :
20990 : /* Fold away non-ODR usages of constants so that we don't need to
20991 : try and stream them in modules if they're internal. */
20992 169652789 : if (!processing_template_decl)
20993 70026581 : cp_fold_function_non_odr_use (fndecl);
20994 :
20995 : /* Save constexpr function body before it gets munged by
20996 : the NRV transformation. */
20997 169652789 : maybe_save_constexpr_fundef (fndecl);
20998 :
20999 : /* Perform delayed folding before NRV transformation. */
21000 169652789 : if (!processing_template_decl
21001 140053162 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
21002 238625636 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
21003 68972404 : cp_fold_function (fndecl);
21004 :
21005 : /* Set up the named return value optimization, if we can. Candidate
21006 : variables are selected in check_return_expr. */
21007 169652789 : if (tree r = current_function_return_value)
21008 : {
21009 44659881 : if (r != error_mark_node)
21010 234827 : finalize_nrv (fndecl, r);
21011 44659881 : current_function_return_value = NULL_TREE;
21012 : }
21013 :
21014 : /* Must mark the RESULT_DECL as being in this function. */
21015 169652789 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
21016 :
21017 : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
21018 : to the FUNCTION_DECL node itself. */
21019 169652789 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
21020 :
21021 : /* Store the end of the function, so that we get good line number
21022 : info for the epilogue. */
21023 169652789 : cfun->function_end_locus = input_location;
21024 :
21025 : /* Complain about parameters that are only set, but never otherwise used. */
21026 169652789 : if (warn_unused_but_set_parameter
21027 1000986 : && !processing_template_decl
21028 511189 : && errorcount == unused_but_set_errorcount
21029 170163975 : && !DECL_CLONED_FUNCTION_P (fndecl))
21030 : {
21031 399343 : tree decl;
21032 :
21033 399343 : for (decl = DECL_ARGUMENTS (fndecl);
21034 1064486 : decl;
21035 665143 : decl = DECL_CHAIN (decl))
21036 665143 : if (TREE_USED (decl)
21037 552098 : && TREE_CODE (decl) == PARM_DECL
21038 552098 : && !DECL_READ_P (decl)
21039 225 : && DECL_NAME (decl)
21040 225 : && !DECL_ARTIFICIAL (decl)
21041 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
21042 225 : && !DECL_IN_SYSTEM_HEADER (decl)
21043 225 : && TREE_TYPE (decl) != error_mark_node
21044 225 : && !TYPE_REF_P (TREE_TYPE (decl))
21045 665368 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
21046 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
21047 225 : warning_at (DECL_SOURCE_LOCATION (decl),
21048 225 : OPT_Wunused_but_set_parameter_,
21049 : "parameter %qD set but not used", decl);
21050 399343 : unused_but_set_errorcount = errorcount;
21051 : }
21052 :
21053 : /* Complain about locally defined typedefs that are not used in this
21054 : function. */
21055 169652789 : maybe_warn_unused_local_typedefs ();
21056 :
21057 : /* Possibly warn about unused parameters. */
21058 169652789 : if (warn_unused_parameter
21059 731600 : && !processing_template_decl
21060 170006578 : && !DECL_CLONED_FUNCTION_P (fndecl))
21061 271796 : do_warn_unused_parameter (fndecl);
21062 :
21063 : /* Genericize before inlining. */
21064 169652789 : if (!processing_template_decl
21065 140053162 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
21066 238625148 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
21067 68971916 : cp_genericize (fndecl);
21068 :
21069 : /* If this function can't throw any exceptions, remember that. */
21070 169652789 : if (!processing_template_decl
21071 70026581 : && !cp_function_chain->can_throw
21072 53392027 : && !flag_non_call_exceptions
21073 53383244 : && !decl_replaceable_p (fndecl,
21074 53383244 : opt_for_fn (fndecl, flag_semantic_interposition))
21075 222785860 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
21076 53130216 : TREE_NOTHROW (fndecl) = 1;
21077 :
21078 169652882 : cleanup:
21079 :
21080 : /* We're leaving the context of this function, so zap cfun. It's still in
21081 : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
21082 169652882 : set_cfun (NULL);
21083 169652882 : current_function_decl = NULL;
21084 :
21085 : /* If this is an in-class inline definition, we may have to pop the
21086 : bindings for the template parameters that we added in
21087 : maybe_begin_member_template_processing when start_function was
21088 : called. */
21089 169652882 : if (inline_p)
21090 87205876 : maybe_end_member_template_processing ();
21091 :
21092 : /* Leave the scope of the class. */
21093 169652882 : if (ctype)
21094 130023267 : pop_nested_class ();
21095 :
21096 169652882 : --function_depth;
21097 :
21098 : /* Clean up. */
21099 169652882 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
21100 :
21101 : /* Build outlined functions for coroutines and contracts. */
21102 :
21103 169652882 : if (coroutine)
21104 : {
21105 : /* Emit the resumer and destroyer functions now, providing that we have
21106 : not encountered some fatal error. */
21107 1722 : if (coroutine->cp_valid_coroutine ())
21108 : {
21109 1644 : coroutine->finish_transforms ();
21110 1644 : resumer = coroutine->get_resumer ();
21111 1644 : destroyer = coroutine->get_destroyer ();
21112 1644 : expand_or_defer_fn (resumer);
21113 1644 : expand_or_defer_fn (destroyer);
21114 : }
21115 1722 : delete coroutine;
21116 : }
21117 :
21118 : /* If we have used outlined contracts checking functions, build and emit
21119 : them here. */
21120 169652882 : finish_function_outlined_contracts (fndecl);
21121 :
21122 169652882 : return fndecl;
21123 : }
21124 :
21125 : /* Create the FUNCTION_DECL for a function definition.
21126 : DECLSPECS and DECLARATOR are the parts of the declaration;
21127 : they describe the return type and the name of the function,
21128 : but twisted together in a fashion that parallels the syntax of C.
21129 :
21130 : This function creates a binding context for the function body
21131 : as well as setting up the FUNCTION_DECL in current_function_decl.
21132 :
21133 : Returns a FUNCTION_DECL on success.
21134 :
21135 : If the DECLARATOR is not suitable for a function (it defines a datum
21136 : instead), we return 0, which tells yyparse to report a parse error.
21137 :
21138 : May return void_type_node indicating that this method is actually
21139 : a friend. See grokfield for more details.
21140 :
21141 : Came here with a `.pushlevel' .
21142 :
21143 : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
21144 : CHANGES TO CODE IN `grokfield'. */
21145 :
21146 : tree
21147 87432026 : grokmethod (cp_decl_specifier_seq *declspecs,
21148 : const cp_declarator *declarator, tree attrlist)
21149 : {
21150 87432026 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
21151 87432026 : &attrlist);
21152 :
21153 87432026 : if (fndecl == error_mark_node)
21154 : return error_mark_node;
21155 :
21156 87431921 : if (attrlist)
21157 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
21158 :
21159 : /* Pass friends other than inline friend functions back. */
21160 87431921 : if (fndecl == void_type_node)
21161 : return fndecl;
21162 :
21163 87431921 : if (DECL_IN_AGGR_P (fndecl))
21164 : {
21165 0 : if (DECL_CLASS_SCOPE_P (fndecl))
21166 0 : error ("%qD is already defined in class %qT", fndecl,
21167 0 : DECL_CONTEXT (fndecl));
21168 0 : return error_mark_node;
21169 : }
21170 :
21171 87431921 : check_template_shadow (fndecl);
21172 :
21173 : /* p1779 ABI-Isolation makes inline not a default for in-class
21174 : definitions attached to a named module. If the user explicitly
21175 : made it inline, grokdeclarator will already have done the right
21176 : things. */
21177 87431921 : if ((!named_module_attach_p ()
21178 1132 : || flag_module_implicit_inline
21179 : /* Lambda's operator function remains inline. */
21180 2228 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
21181 : /* If the user explicitly asked for this to be inline, we don't
21182 : need to do more, but more importantly we want to warn if we
21183 : can't inline it. */
21184 87431458 : && !DECL_DECLARED_INLINE_P (fndecl))
21185 : {
21186 49121774 : if (TREE_PUBLIC (fndecl))
21187 46852363 : DECL_COMDAT (fndecl) = 1;
21188 49121774 : DECL_DECLARED_INLINE_P (fndecl) = 1;
21189 : /* It's ok if we can't inline this. */
21190 49121774 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
21191 : }
21192 :
21193 : /* We process method specializations in finish_struct_1. */
21194 87431921 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
21195 : {
21196 : /* Avoid calling decl_spec_seq... until we have to. */
21197 70214125 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
21198 70214125 : fndecl = push_template_decl (fndecl, friendp);
21199 70214125 : if (fndecl == error_mark_node)
21200 : return fndecl;
21201 : }
21202 :
21203 87431913 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
21204 : {
21205 0 : fndecl = copy_node (fndecl);
21206 0 : TREE_CHAIN (fndecl) = NULL_TREE;
21207 : }
21208 :
21209 87431913 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
21210 :
21211 87431913 : DECL_IN_AGGR_P (fndecl) = 1;
21212 87431913 : return fndecl;
21213 : }
21214 :
21215 :
21216 : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
21217 : we can lay it out later, when and if its type becomes complete.
21218 :
21219 : Also handle constexpr variables where the initializer involves
21220 : an unlowered PTRMEM_CST because the class isn't complete yet. */
21221 :
21222 : void
21223 108459629 : maybe_register_incomplete_var (tree var)
21224 : {
21225 108459629 : gcc_assert (VAR_P (var));
21226 :
21227 : /* Keep track of variables with incomplete types. */
21228 47561492 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
21229 156019326 : && DECL_EXTERNAL (var))
21230 : {
21231 6796794 : tree inner_type = TREE_TYPE (var);
21232 :
21233 6957134 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
21234 160340 : inner_type = TREE_TYPE (inner_type);
21235 6796794 : inner_type = TYPE_MAIN_VARIANT (inner_type);
21236 :
21237 6954889 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
21238 : /* RTTI TD entries are created while defining the type_info. */
21239 6796794 : || (TYPE_LANG_SPECIFIC (inner_type)
21240 2421234 : && TYPE_BEING_DEFINED (inner_type)))
21241 : {
21242 158095 : incomplete_var iv = {var, inner_type};
21243 158095 : vec_safe_push (incomplete_vars, iv);
21244 : }
21245 12973065 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
21246 3624647 : && DECL_CLASS_SCOPE_P (var)
21247 1130733 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
21248 1130733 : && decl_constant_var_p (var)
21249 7363363 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
21250 : {
21251 : /* When the outermost open class is complete we can resolve any
21252 : pointers-to-members. */
21253 27 : tree context = outermost_open_class ();
21254 27 : incomplete_var iv = {var, context};
21255 27 : vec_safe_push (incomplete_vars, iv);
21256 : }
21257 : }
21258 108459629 : }
21259 :
21260 : /* Called when a class type (given by TYPE) is defined. If there are
21261 : any existing VAR_DECLs whose type has been completed by this
21262 : declaration, update them now. */
21263 :
21264 : void
21265 73953077 : complete_vars (tree type)
21266 : {
21267 73953077 : unsigned ix;
21268 73953077 : incomplete_var *iv;
21269 :
21270 74628004 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
21271 : {
21272 674927 : if (same_type_p (type, iv->incomplete_type))
21273 : {
21274 156891 : tree var = iv->decl;
21275 156891 : tree type = TREE_TYPE (var);
21276 :
21277 156891 : if (type != error_mark_node
21278 156891 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
21279 156888 : == iv->incomplete_type))
21280 : {
21281 : /* Complete the type of the variable. */
21282 156861 : complete_type (type);
21283 156861 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
21284 156861 : if (COMPLETE_TYPE_P (type))
21285 156846 : layout_var_decl (var);
21286 : }
21287 :
21288 : /* Remove this entry from the list. */
21289 156891 : incomplete_vars->unordered_remove (ix);
21290 : }
21291 : else
21292 518036 : ix++;
21293 : }
21294 73953077 : }
21295 :
21296 : /* If DECL is of a type which needs a cleanup, build and return an
21297 : expression to perform that cleanup here. Return NULL_TREE if no
21298 : cleanup need be done. DECL can also be a _REF when called from
21299 : split_nonconstant_init_1. */
21300 :
21301 : tree
21302 93032978 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
21303 : {
21304 93032978 : tree type;
21305 93032978 : tree attr;
21306 93032978 : tree cleanup;
21307 :
21308 : /* Assume no cleanup is required. */
21309 93032978 : cleanup = NULL_TREE;
21310 :
21311 93032978 : if (!decl || error_operand_p (decl))
21312 : return cleanup;
21313 :
21314 : /* Handle "__attribute__((cleanup))". We run the cleanup function
21315 : before the destructor since the destructor is what actually
21316 : terminates the lifetime of the object. */
21317 93032978 : if (DECL_P (decl))
21318 92672750 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
21319 : else
21320 : attr = NULL_TREE;
21321 92672750 : if (attr)
21322 : {
21323 159 : tree id;
21324 159 : tree fn;
21325 159 : tree arg;
21326 :
21327 : /* Get the name specified by the user for the cleanup function. */
21328 159 : id = TREE_VALUE (TREE_VALUE (attr));
21329 : /* Look up the name to find the cleanup function to call. It is
21330 : important to use lookup_name here because that is what is
21331 : used in c-common.cc:handle_cleanup_attribute when performing
21332 : initial checks on the attribute. Note that those checks
21333 : include ensuring that the function found is not an overloaded
21334 : function, or an object with an overloaded call operator,
21335 : etc.; we can rely on the fact that the function found is an
21336 : ordinary FUNCTION_DECL. */
21337 159 : fn = lookup_name (id);
21338 159 : arg = build_address (decl);
21339 159 : if (!mark_used (decl, complain) && !(complain & tf_error))
21340 0 : return error_mark_node;
21341 159 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
21342 159 : if (cleanup == error_mark_node)
21343 : return error_mark_node;
21344 : }
21345 : /* Handle ordinary C++ destructors. */
21346 93032966 : type = TREE_TYPE (decl);
21347 93032966 : if (type_build_dtor_call (type))
21348 : {
21349 7027689 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
21350 7027689 : tree addr;
21351 7027689 : tree call;
21352 :
21353 7027689 : if (TREE_CODE (type) == ARRAY_TYPE)
21354 : addr = decl;
21355 : else
21356 7022429 : addr = build_address (decl);
21357 :
21358 7027689 : call = build_delete (input_location, TREE_TYPE (addr), addr,
21359 : sfk_complete_destructor, flags, 0, complain);
21360 7027689 : if (call == error_mark_node)
21361 : cleanup = error_mark_node;
21362 7027626 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
21363 : /* Discard the call. */;
21364 4311185 : else if (decl_maybe_constant_destruction (decl, type)
21365 4311387 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
21366 202 : cxx_constant_dtor (call, decl);
21367 4310983 : else if (cleanup)
21368 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
21369 : else
21370 : cleanup = call;
21371 : }
21372 :
21373 : /* build_delete sets the location of the destructor call to the
21374 : current location, even though the destructor is going to be
21375 : called later, at the end of the current scope. This can lead to
21376 : a "jumpy" behavior for users of debuggers when they step around
21377 : the end of the block. So let's unset the location of the
21378 : destructor call instead. */
21379 93032966 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
21380 93032966 : if (cleanup && CONVERT_EXPR_P (cleanup))
21381 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
21382 :
21383 0 : if (cleanup
21384 4311190 : && DECL_P (decl)
21385 4310724 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
21386 : /* Treat objects with destructors as used; the destructor may do
21387 : something substantive. */
21388 4310718 : && !mark_used (decl, complain) && !(complain & tf_error))
21389 0 : return error_mark_node;
21390 :
21391 4311190 : if (cleanup && cfun && !processing_template_decl
21392 96900963 : && !expr_noexcept_p (cleanup, tf_none))
21393 5877 : cp_function_chain->throwing_cleanup = true;
21394 :
21395 : return cleanup;
21396 : }
21397 :
21398 :
21399 : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
21400 : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
21401 : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
21402 :
21403 : tree
21404 811997 : static_fn_type (tree memfntype)
21405 : {
21406 811997 : tree fntype;
21407 811997 : tree args;
21408 :
21409 811997 : if (TYPE_PTRMEMFUNC_P (memfntype))
21410 20975 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
21411 811997 : if (INDIRECT_TYPE_P (memfntype)
21412 688577 : || TREE_CODE (memfntype) == FUNCTION_DECL)
21413 638641 : memfntype = TREE_TYPE (memfntype);
21414 811997 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
21415 : return memfntype;
21416 628659 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
21417 628659 : args = TYPE_ARG_TYPES (memfntype);
21418 628659 : fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
21419 628659 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
21420 628659 : fntype = (cp_build_type_attribute_variant
21421 628659 : (fntype, TYPE_ATTRIBUTES (memfntype)));
21422 628659 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
21423 628659 : return fntype;
21424 : }
21425 :
21426 : /* DECL was originally constructed as a non-static member function,
21427 : but turned out to be static. Update it accordingly. */
21428 :
21429 : void
21430 184207 : revert_static_member_fn (tree decl)
21431 : {
21432 184207 : tree stype = static_fn_type (decl);
21433 184207 : cp_cv_quals quals = type_memfn_quals (stype);
21434 184207 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
21435 :
21436 184207 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
21437 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
21438 :
21439 184207 : TREE_TYPE (decl) = stype;
21440 :
21441 184207 : if (DECL_ARGUMENTS (decl))
21442 184207 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
21443 184207 : DECL_STATIC_FUNCTION_P (decl) = 1;
21444 184207 : }
21445 :
21446 : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
21447 : one of the language-independent trees. */
21448 :
21449 : enum cp_tree_node_structure_enum
21450 39802677517 : cp_tree_node_structure (union lang_tree_node * t)
21451 : {
21452 39802677517 : switch (TREE_CODE (&t->generic))
21453 : {
21454 : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
21455 163387641 : case BASELINK: return TS_CP_BASELINK;
21456 85275540 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
21457 11911014 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
21458 6298972 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
21459 2147550577 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
21460 3576631 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
21461 213143 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
21462 739499439 : case OVERLOAD: return TS_CP_OVERLOAD;
21463 743 : case PTRMEM_CST: return TS_CP_PTRMEM;
21464 16478435 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
21465 701934563 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
21466 1632384706 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
21467 709869277 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
21468 16000245 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
21469 6 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
21470 3675566 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
21471 14983828 : case REQUIRES_EXPR: return TS_CP_REQUIRES_EXPR;
21472 33549637191 : default: return TS_CP_GENERIC;
21473 : }
21474 : }
21475 :
21476 : bool
21477 9173 : cp_missing_noreturn_ok_p (tree decl)
21478 : {
21479 : /* A missing noreturn is ok for the `main' function. */
21480 9173 : return DECL_MAIN_P (decl);
21481 : }
21482 :
21483 : /* Return the decl used to identify the COMDAT group into which DECL should
21484 : be placed. */
21485 :
21486 : tree
21487 130640824 : cxx_comdat_group (tree decl)
21488 : {
21489 : /* Virtual tables, construction virtual tables, and virtual table
21490 : tables all go in a single COMDAT group, named after the primary
21491 : virtual table. */
21492 130640824 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
21493 542086 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
21494 : /* For all other DECLs, the COMDAT group is the mangled name of the
21495 : declaration itself. */
21496 : else
21497 : {
21498 130098738 : while (DECL_THUNK_P (decl))
21499 : {
21500 : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
21501 : into the same section as the target function. In that case
21502 : we must return target's name. */
21503 0 : tree target = THUNK_TARGET (decl);
21504 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
21505 0 : && DECL_SECTION_NAME (target) != NULL
21506 0 : && DECL_ONE_ONLY (target))
21507 : decl = target;
21508 : else
21509 : break;
21510 : }
21511 : /* If a ctor/dtor has already set the comdat group by
21512 : maybe_clone_body, don't override it. */
21513 130098738 : if (SUPPORTS_ONE_ONLY
21514 130098738 : && TREE_CODE (decl) == FUNCTION_DECL
21515 130098738 : && DECL_CLONED_FUNCTION_P (decl))
21516 18922959 : if (tree comdat = DECL_COMDAT_GROUP (decl))
21517 2774737 : return comdat;
21518 : }
21519 :
21520 : return decl;
21521 : }
21522 :
21523 : /* Returns the return type for FN as written by the user, which may include
21524 : a placeholder for a deduced return type. */
21525 :
21526 : tree
21527 569951526 : fndecl_declared_return_type (tree fn)
21528 : {
21529 569951526 : fn = STRIP_TEMPLATE (fn);
21530 569951526 : if (FNDECL_USED_AUTO (fn))
21531 403115 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
21532 :
21533 569548411 : return TREE_TYPE (TREE_TYPE (fn));
21534 : }
21535 :
21536 : /* Returns true iff DECL is a variable or function declared with an auto type
21537 : that has not yet been deduced to a real type. */
21538 :
21539 : bool
21540 1024352604 : undeduced_auto_decl (tree decl)
21541 : {
21542 1024352604 : if (cxx_dialect < cxx11)
21543 : return false;
21544 1021736070 : STRIP_ANY_LOCATION_WRAPPER (decl);
21545 465253329 : return ((VAR_OR_FUNCTION_DECL_P (decl)
21546 98147845 : || TREE_CODE (decl) == TEMPLATE_DECL)
21547 1389111271 : && type_uses_auto (TREE_TYPE (decl)));
21548 : }
21549 :
21550 : /* Complain if DECL has an undeduced return type. */
21551 :
21552 : bool
21553 339291416 : require_deduced_type (tree decl, tsubst_flags_t complain)
21554 : {
21555 339291416 : if (undeduced_auto_decl (decl))
21556 : {
21557 14445 : if (TREE_CODE (decl) == FUNCTION_DECL
21558 14445 : && fndecl_built_in_p (decl, CP_BUILT_IN_SOURCE_LOCATION,
21559 : BUILT_IN_FRONTEND))
21560 : {
21561 : /* Set the return type of __builtin_source_location. */
21562 10351 : tree type = get_source_location_impl_type ();
21563 10351 : if (type == error_mark_node)
21564 : {
21565 33 : inform (input_location, "using %qs", "__builtin_source_location");
21566 33 : return false;
21567 : }
21568 10318 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
21569 10318 : type = build_pointer_type (type);
21570 10318 : apply_deduced_return_type (decl, type);
21571 10318 : return true;
21572 : }
21573 :
21574 4094 : if (TREE_CODE (decl) == FUNCTION_DECL
21575 4094 : && fndecl_built_in_p (decl, CP_BUILT_IN_CURRENT_EXCEPTION,
21576 : BUILT_IN_FRONTEND))
21577 : {
21578 : /* Set the return type of __builtin_current_exception. */
21579 2096 : tree name = get_identifier ("exception_ptr");
21580 2096 : tree eptr = lookup_qualified_name (std_node, name);
21581 2096 : tree fld;
21582 2096 : if (TREE_CODE (eptr) != TYPE_DECL
21583 2096 : || !CLASS_TYPE_P (TREE_TYPE (eptr))
21584 2096 : || !COMPLETE_TYPE_P (TREE_TYPE (eptr))
21585 2096 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (eptr))))
21586 2096 : || DECL_ARTIFICIAL (fld)
21587 2096 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
21588 2096 : || next_aggregate_field (DECL_CHAIN (fld))
21589 4192 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (eptr)),
21590 2096 : TYPE_SIZE (TREE_TYPE (fld))))
21591 : {
21592 0 : error ("%qs used without %qs declaration",
21593 : "__builtin_current_exception", "std::exception_ptr");
21594 0 : return false;
21595 : }
21596 :
21597 2096 : apply_deduced_return_type (decl, TREE_TYPE (eptr));
21598 2096 : return true;
21599 : }
21600 :
21601 1998 : if (warning_suppressed_p (decl) && seen_error ())
21602 : /* We probably already complained about deduction failure. */;
21603 1815 : else if (complain & tf_error)
21604 74 : error ("use of %qD before deduction of %<auto%>", decl);
21605 1998 : note_failed_type_completion (decl, complain);
21606 1998 : return false;
21607 : }
21608 : return true;
21609 : }
21610 :
21611 : /* Create a representation of the explicit-specifier with
21612 : constant-expression of EXPR. COMPLAIN is as for tsubst. */
21613 :
21614 : tree
21615 8609999 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
21616 : {
21617 8609999 : if (check_for_bare_parameter_packs (expr))
21618 3 : return error_mark_node;
21619 :
21620 8609996 : if (instantiation_dependent_expression_p (expr))
21621 : /* Wait for instantiation, tsubst_function_decl will handle it. */
21622 : return expr;
21623 :
21624 6918121 : expr = build_converted_constant_bool_expr (expr, complain);
21625 6918121 : expr = instantiate_non_dependent_expr (expr, complain);
21626 6918121 : expr = cxx_constant_value (expr, complain);
21627 6918121 : return expr;
21628 : }
21629 :
21630 : #include "gt-cp-decl.h"
|