Line data Source code
1 : /* Process declarations and variables for -*- 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 242705789 : current_tmpl_spec_kind (int n_class_scopes)
312 : {
313 242705789 : int n_template_parm_scopes = 0;
314 242705789 : int seen_specialization_p = 0;
315 242705789 : int innermost_specialization_p = 0;
316 242705789 : cp_binding_level *b;
317 :
318 : /* Scan through the template parameter scopes. */
319 242705789 : for (b = current_binding_level;
320 302403913 : b->kind == sk_template_parms;
321 59698124 : 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 59698124 : if (b->explicit_spec_p)
333 : {
334 1375379 : if (n_template_parm_scopes == 0)
335 : innermost_specialization_p = 1;
336 : else
337 1150 : seen_specialization_p = 1;
338 : }
339 58322745 : else if (seen_specialization_p == 1)
340 : return tsk_invalid_member_spec;
341 :
342 59698124 : ++n_template_parm_scopes;
343 : }
344 :
345 : /* Handle explicit instantiations. */
346 242705789 : if (processing_explicit_instantiation)
347 : {
348 1915593 : 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 1915593 : return tsk_expl_inst;
358 : }
359 :
360 240790196 : 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 240790196 : 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 51854702 : 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 102797311 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394 : }
395 :
396 : /* Exit the current scope. */
397 :
398 : void
399 94382408 : finish_scope (void)
400 : {
401 94382408 : poplevel (0, 0, 0);
402 94382408 : }
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 20458 : check_label_used (tree label)
409 : {
410 20458 : if (!processing_template_decl)
411 : {
412 20149 : 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 20140 : warn_for_unused_label (label);
424 : }
425 20458 : }
426 :
427 : /* Helper function to sort named label entries in a vector by DECL_UID. */
428 :
429 : static int
430 229893 : sort_labels (const void *a, const void *b)
431 : {
432 229893 : tree label1 = *(tree const *) a;
433 229893 : tree label2 = *(tree const *) b;
434 :
435 : /* DECL_UIDs can never be equal. */
436 229893 : 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 165485895 : pop_labels (tree block)
508 : {
509 165485895 : if (!named_labels)
510 165481290 : 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 4605 : auto_vec<tree, 32> labels (named_labels->elements ());
518 4605 : hash_table<named_label_hash>::iterator end (named_labels->end ());
519 :
520 4605 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
521 681 : && !processing_template_decl)
522 : {
523 6078 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
524 : {
525 2392 : named_label_entry *ent = *iter;
526 2392 : 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 45455 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
537 : {
538 20425 : named_label_entry *ent = *iter;
539 :
540 20425 : gcc_checking_assert (!ent->outer);
541 20425 : if (ent->label_decl)
542 20239 : labels.quick_push (ent->label_decl);
543 20425 : ggc_free (ent);
544 : }
545 4605 : named_labels = NULL;
546 4605 : labels.qsort (sort_labels);
547 :
548 24844 : while (labels.length ())
549 : {
550 20239 : tree label = labels.pop ();
551 :
552 20239 : DECL_CHAIN (label) = BLOCK_VARS (block);
553 20239 : BLOCK_VARS (block) = label;
554 :
555 20239 : check_label_used (label);
556 : }
557 4605 : }
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 4679106 : level_for_constexpr_if (cp_binding_level *b)
618 : {
619 4675044 : return (b->kind == sk_cond && b->this_entity
620 292 : && TREE_CODE (b->this_entity) == IF_STMT
621 4679398 : && 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 4679082 : level_for_consteval_if (cp_binding_level *b)
628 : {
629 4675020 : return (b->kind == sk_cond && b->this_entity
630 268 : && TREE_CODE (b->this_entity) == IF_STMT
631 4679350 : && 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 153619 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
652 : {
653 153619 : named_label_entry *ent = *slot;
654 153619 : cp_binding_level *obl = bl->level_chain;
655 :
656 153619 : if (ent->binding_level == bl)
657 : {
658 10127 : tree decl;
659 :
660 : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
661 : TREE_LISTs representing OVERLOADs, so be careful. */
662 15023 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
663 4969 : ? DECL_CHAIN (decl)
664 73 : : TREE_CHAIN (decl)))
665 4896 : if (decl_jump_unsafe (decl))
666 : {
667 1872 : vec_safe_push (ent->bad_decls, decl);
668 1872 : ent->has_bad_decls = true;
669 : }
670 3024 : else if (decl_instrument_init_bypass_p (decl))
671 435 : vec_safe_push (ent->bad_decls, decl);
672 :
673 10127 : ent->binding_level = obl;
674 10127 : ent->names_in_scope = obl->names;
675 10127 : 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 4100 : case sk_block:
693 4100 : if (level_for_constexpr_if (obl))
694 15 : ent->in_constexpr_if = true;
695 4085 : else if (level_for_consteval_if (obl))
696 30 : ent->in_consteval_if = true;
697 : break;
698 : default:
699 : break;
700 : }
701 : }
702 143492 : else if (ent->uses)
703 : {
704 : struct named_label_use_entry *use;
705 :
706 22845 : for (use = ent->uses; use ; use = use->next)
707 15527 : if (use->binding_level == bl)
708 : {
709 5981 : 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 5981 : use->binding_level = obl;
719 5981 : use->names_in_scope = obl->names;
720 5981 : if (bl->kind == sk_omp)
721 12 : use->in_omp_scope = true;
722 : }
723 : }
724 :
725 153619 : 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 698783734 : poplevel (int keep, int reverse, int functionbody)
749 : {
750 698783734 : tree link;
751 : /* The chain of decls was accumulated in reverse order.
752 : Put it into forward order, just for cleanliness. */
753 698783734 : tree decls;
754 698783734 : tree subblocks;
755 698783734 : tree block;
756 698783734 : tree decl;
757 698783734 : scope_kind kind;
758 :
759 698783734 : auto_cond_timevar tv (TV_NAME_LOOKUP);
760 698784046 : restart:
761 :
762 698784046 : block = NULL_TREE;
763 :
764 698784046 : gcc_assert (current_binding_level->kind != sk_class
765 : && current_binding_level->kind != sk_namespace);
766 :
767 698784046 : if (current_binding_level->kind == sk_cleanup)
768 : functionbody = 0;
769 698784046 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
770 :
771 698784046 : 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 698784046 : gcc_assert (keep == 0 || keep == 1);
777 :
778 698784046 : if (current_binding_level->keep)
779 9826888 : keep = 1;
780 :
781 : /* Any uses of undefined labels, and any defined labels, now operate
782 : under constraints of next binding contour. */
783 698784046 : if (cfun && !functionbody && named_labels)
784 43290 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
785 196909 : (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 698784046 : decls = current_binding_level->names;
792 698784046 : if (reverse)
793 : {
794 394011123 : decls = nreverse (decls);
795 394011123 : 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 698784046 : block = NULL_TREE;
802 : /* Avoid function body block if possible. */
803 741899418 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
804 : keep = 0;
805 655668935 : else if (keep == 1 || functionbody)
806 212266641 : block = make_node (BLOCK);
807 212266641 : if (block != NULL_TREE)
808 : {
809 212266641 : BLOCK_VARS (block) = decls;
810 212266641 : BLOCK_SUBBLOCKS (block) = subblocks;
811 : }
812 :
813 : /* In each subblock, record that this is its superior. */
814 698784046 : if (keep >= 0)
815 788679186 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
816 89895140 : BLOCK_SUPERCONTEXT (link) = block;
817 :
818 : /* Before we remove the declarations first check for unused variables. */
819 698784046 : if ((warn_unused_variable || warn_unused_but_set_variable)
820 6480864 : && current_binding_level->kind != sk_template_parms
821 5475662 : && !processing_template_decl)
822 6365693 : 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 3234568 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
828 3234568 : decl = strip_using_decl (udecl);
829 :
830 3234568 : tree type = TREE_TYPE (decl);
831 3234568 : if (VAR_P (decl)
832 378199 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
833 3402 : && !DECL_IN_SYSTEM_HEADER (udecl)
834 : /* For structured bindings, consider only real variables, not
835 : subobjects. */
836 3390 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
837 3170 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
838 : /* Don't warn about name-independent declarations. */
839 896 : && !name_independent_decl_p (decl)
840 740 : && type != error_mark_node
841 3238664 : && (!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 706 : 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 398 : else if (DECL_CONTEXT (decl) == current_function_decl
862 : // For -Wunused-but-set-variable leave references alone.
863 374 : && !TYPE_REF_P (TREE_TYPE (decl))
864 772 : && errorcount == unused_but_set_errorcount)
865 : {
866 365 : 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 353 : warning_at (DECL_SOURCE_LOCATION (decl),
872 353 : OPT_Wunused_but_set_variable_,
873 : "variable %qD set but not used", decl);
874 365 : unused_but_set_errorcount = errorcount;
875 : }
876 : }
877 : }
878 :
879 : /* Remove declarations for all the DECLs in this level. */
880 1396665962 : for (link = decls; link; link = TREE_CHAIN (link))
881 : {
882 697881916 : tree name;
883 697881916 : if (TREE_CODE (link) == TREE_LIST)
884 : {
885 13133015 : decl = TREE_VALUE (link);
886 13133015 : name = TREE_PURPOSE (link);
887 13133015 : gcc_checking_assert (name);
888 : }
889 : else
890 : {
891 684748901 : decl = link;
892 684748901 : name = DECL_NAME (decl);
893 : }
894 :
895 : /* Remove the binding. */
896 697881916 : if (TREE_CODE (decl) == LABEL_DECL)
897 219 : pop_local_label (name, decl);
898 : else
899 697881697 : pop_local_binding (name, decl);
900 : }
901 :
902 : /* Restore the IDENTIFIER_TYPE_VALUEs. */
903 698784046 : for (link = current_binding_level->type_shadowed;
904 939114294 : link; link = TREE_CHAIN (link))
905 240330248 : 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 698784046 : if (block)
915 : {
916 212266641 : tree* d;
917 :
918 555274628 : for (d = &BLOCK_VARS (block); *d; )
919 : {
920 343007987 : if (TREE_CODE (*d) == TREE_LIST
921 343007987 : || (!processing_template_decl
922 121971858 : && undeduced_auto_decl (*d)))
923 13133067 : *d = TREE_CHAIN (*d);
924 : else
925 329874920 : 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 698784046 : if (functionbody)
932 : {
933 165485895 : 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 122370784 : BLOCK_VARS (block) = 0;
939 122370784 : pop_labels (block);
940 : }
941 : else
942 43115111 : pop_labels (subblocks);
943 : }
944 :
945 698784046 : kind = current_binding_level->kind;
946 698784046 : if (kind == sk_cleanup)
947 : {
948 312 : 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 312 : stmt = pop_stmt_list (current_binding_level->statement_list);
954 312 : stmt = c_build_bind_expr (input_location, block, stmt);
955 312 : add_stmt (stmt);
956 : }
957 :
958 698784046 : leave_scope ();
959 698784046 : if (functionbody)
960 : {
961 : /* The current function is being defined, so its DECL_INITIAL
962 : should be error_mark_node. */
963 165485895 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
964 208601006 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
965 165485895 : if (subblocks)
966 : {
967 82212198 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
968 54454968 : && BLOCK_SUBBLOCKS (subblocks))
969 1796396 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
970 : else
971 41318976 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
972 : }
973 : }
974 533298151 : else if (block)
975 89895857 : current_binding_level->blocks
976 179791714 : = 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 443402294 : 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 698784046 : if (block)
991 212266641 : TREE_USED (block) = 1;
992 :
993 : /* All temporary bindings created for cleanups are popped silently. */
994 698784046 : if (kind == sk_cleanup)
995 312 : goto restart;
996 :
997 1397567468 : return block;
998 698783734 : }
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 141086 : wrapup_namespace_globals ()
1006 : {
1007 141086 : if (vec<tree, va_gc> *statics = static_decls)
1008 : {
1009 17295525 : for (tree decl : *statics)
1010 : {
1011 17179460 : if (warn_unused_function
1012 989807 : && TREE_CODE (decl) == FUNCTION_DECL
1013 742760 : && DECL_INITIAL (decl) == 0
1014 7160 : && DECL_EXTERNAL (decl)
1015 7160 : && !TREE_PUBLIC (decl)
1016 32 : && !DECL_ARTIFICIAL (decl)
1017 32 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1018 9 : && !warning_suppressed_p (decl, OPT_Wunused)
1019 18169267 : && warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_function,
1020 : "%qF declared %<static%> but never defined",
1021 : decl))
1022 6 : suppress_warning (decl, OPT_Wunused);
1023 :
1024 17179460 : if (VAR_P (decl)
1025 5825137 : && DECL_EXTERNAL (decl)
1026 1978196 : && DECL_INLINE_VAR_P (decl)
1027 17179489 : && DECL_ODR_USED (decl))
1028 3 : error_at (DECL_SOURCE_LOCATION (decl),
1029 : "odr-used inline variable %qD is not defined", decl);
1030 : }
1031 :
1032 : /* Clear out the list, so we don't rescan next time. */
1033 116065 : static_decls = NULL;
1034 :
1035 : /* Write out any globals that need to be output. */
1036 116065 : return wrapup_global_declarations (statics->address (),
1037 116065 : statics->length ());
1038 : }
1039 : return 0;
1040 : }
1041 :
1042 : /* In C++, you don't have to write `struct S' to refer to `S'; you
1043 : can just use `S'. We accomplish this by creating a TYPE_DECL as
1044 : if the user had written `typedef struct S S'. Create and return
1045 : the TYPE_DECL for TYPE. */
1046 :
1047 : tree
1048 189153318 : create_implicit_typedef (tree name, tree type)
1049 : {
1050 189153318 : tree decl;
1051 :
1052 189153318 : decl = build_decl (input_location, TYPE_DECL, name, type);
1053 189153318 : DECL_ARTIFICIAL (decl) = 1;
1054 : /* There are other implicit type declarations, like the one *within*
1055 : a class that allows you to write `S::S'. We must distinguish
1056 : amongst these. */
1057 189153318 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1058 189153318 : TYPE_NAME (type) = decl;
1059 189153318 : TYPE_STUB_DECL (type) = decl;
1060 :
1061 189153318 : return decl;
1062 : }
1063 :
1064 : /* Function-scope local entities that need discriminators. Each entry
1065 : is a {decl,name} pair. VAR_DECLs for anon unions get their name
1066 : smashed, so we cannot rely on DECL_NAME. */
1067 :
1068 : static GTY((deletable)) vec<tree, va_gc> *local_entities;
1069 :
1070 : /* Determine the mangling discriminator of local DECL. There are
1071 : generally very few of these in any particular function. */
1072 :
1073 : void
1074 830606 : determine_local_discriminator (tree decl, tree name)
1075 : {
1076 830606 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1077 830606 : retrofit_lang_decl (decl);
1078 830606 : tree ctx = DECL_CONTEXT (decl);
1079 830606 : size_t nelts = vec_safe_length (local_entities);
1080 830606 : if (name == NULL_TREE)
1081 830345 : name = (TREE_CODE (decl) == TYPE_DECL
1082 1328958 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
1083 1490789 : ? NULL_TREE : DECL_NAME (decl));
1084 29161667 : for (size_t i = 0; i < nelts; i += 2)
1085 : {
1086 28331528 : tree *pair = &(*local_entities)[i];
1087 28331528 : tree d = pair[0];
1088 28331528 : tree n = pair[1];
1089 28331528 : gcc_checking_assert (d != decl);
1090 28331528 : if (name == n
1091 4618744 : && TREE_CODE (decl) == TREE_CODE (d)
1092 32950263 : && ctx == DECL_CONTEXT (d))
1093 : {
1094 467 : tree disc = integer_one_node;
1095 467 : if (DECL_DISCRIMINATOR (d))
1096 244 : disc = build_int_cst (TREE_TYPE (disc),
1097 244 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
1098 467 : DECL_DISCRIMINATOR (decl) = disc;
1099 : /* Replace the saved decl. */
1100 467 : pair[0] = decl;
1101 467 : decl = NULL_TREE;
1102 467 : break;
1103 : }
1104 : }
1105 :
1106 830606 : if (decl)
1107 : {
1108 830139 : vec_safe_reserve (local_entities, 2);
1109 830139 : local_entities->quick_push (decl);
1110 830139 : local_entities->quick_push (name);
1111 : }
1112 830606 : }
1113 :
1114 :
1115 : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
1116 :
1117 : A non-template friend declaration with a requires-clause shall be a
1118 : definition. A friend function template with a constraint that depends on a
1119 : template parameter from an enclosing template shall be a definition. Such a
1120 : constrained friend function or function template declaration does not
1121 : declare the same function or function template as a declaration in any other
1122 : scope.
1123 :
1124 : The ABI calls this a "member-like constrained friend" and mangles it like a
1125 : member function to avoid collisions. */
1126 :
1127 : bool
1128 2677030981 : member_like_constrained_friend_p (tree decl)
1129 : {
1130 2677030981 : return (TREE_CODE (decl) == FUNCTION_DECL
1131 904383719 : && DECL_UNIQUE_FRIEND_P (decl)
1132 192279948 : && DECL_FRIEND_CONTEXT (decl)
1133 79161728 : && get_constraints (decl)
1134 6904108 : && CLASSTYPE_IMPLICIT_INSTANTIATION (DECL_FRIEND_CONTEXT (decl))
1135 2680475810 : && (!DECL_TEMPLATE_INFO (decl)
1136 3444829 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1137 6280 : || (uses_outer_template_parms_in_constraints
1138 6280 : (most_general_template (decl)))));
1139 : }
1140 :
1141 : /* Returns true if functions FN1 and FN2 have equivalent trailing
1142 : requires clauses. */
1143 :
1144 : static bool
1145 102240687 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1146 : {
1147 : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1148 : same function as a declaration in any other scope." So no need to
1149 : actually compare the requirements. */
1150 102240687 : if (member_like_constrained_friend_p (newfn)
1151 102240687 : || member_like_constrained_friend_p (oldfn))
1152 95 : return false;
1153 :
1154 : /* Compare only trailing requirements. */
1155 102240592 : tree reqs1 = get_trailing_function_requirements (newfn);
1156 102240592 : tree reqs2 = get_trailing_function_requirements (oldfn);
1157 102240592 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1158 : return false;
1159 :
1160 : /* Substitution is needed when friends are involved. */
1161 102119493 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1162 102119493 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1163 :
1164 102119493 : return cp_tree_equal (reqs1, reqs2);
1165 : }
1166 :
1167 : /* Two functions of the same name correspond [basic.scope.scope] if
1168 :
1169 : + both declare functions with the same non-object-parameter-type-list,
1170 : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1171 : specified in [temp.friend]), and, if both are non-static members, they have
1172 : corresponding object parameters, or
1173 :
1174 : + both declare function templates with equivalent
1175 : non-object-parameter-type-lists, return types (if any), template-heads, and
1176 : trailing requires-clauses (if any), and, if both are non-static members,
1177 : they have corresponding object parameters.
1178 :
1179 : This is a subset of decls_match: it identifies declarations that cannot be
1180 : overloaded with one another. This function does not consider DECL_NAME. */
1181 :
1182 : bool
1183 2729640768 : fns_correspond (tree newdecl, tree olddecl)
1184 : {
1185 2729640768 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1186 : return false;
1187 :
1188 2534934087 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1189 : {
1190 2533578826 : if (!template_heads_equivalent_p (newdecl, olddecl))
1191 : return 0;
1192 611556313 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1193 611556313 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1194 : }
1195 :
1196 612911574 : tree f1 = TREE_TYPE (newdecl);
1197 612911574 : tree f2 = TREE_TYPE (olddecl);
1198 :
1199 612911574 : int rq1 = type_memfn_rqual (f1);
1200 612911574 : int rq2 = type_memfn_rqual (f2);
1201 :
1202 : /* If only one is a non-static member function, ignore ref-quals. */
1203 612911574 : if (TREE_CODE (f1) != TREE_CODE (f2))
1204 : rq1 = rq2;
1205 : /* Two non-static member functions have corresponding object parameters if:
1206 : + exactly one is an implicit object member function with no ref-qualifier
1207 : and the types of their object parameters ([dcl.fct]), after removing
1208 : top-level references, are the same, or
1209 : + their object parameters have the same type. */
1210 : /* ??? We treat member functions of different classes as corresponding even
1211 : though that means the object parameters have different types. */
1212 612911574 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1213 0 : rq1 = rq2;
1214 :
1215 612911574 : bool types_match = rq1 == rq2;
1216 :
1217 612911574 : if (types_match)
1218 : {
1219 612911574 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1220 612911574 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1221 612911574 : types_match = compparms (p1, p2);
1222 : }
1223 :
1224 : /* Two function declarations match if either has a requires-clause
1225 : then both have a requires-clause and their constraints-expressions
1226 : are equivalent. */
1227 612911574 : if (types_match && flag_concepts)
1228 74700957 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1229 :
1230 : return types_match;
1231 : }
1232 :
1233 : /* Subroutine of duplicate_decls: return truthvalue of whether
1234 : or not types of these decls match.
1235 :
1236 : For C++, we must compare the parameter list so that `int' can match
1237 : `int&' in a parameter position, but `int&' is not confused with
1238 : `const int&'. */
1239 :
1240 : int
1241 794545906 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1242 : {
1243 794545906 : int types_match;
1244 :
1245 794545906 : if (newdecl == olddecl)
1246 : return 1;
1247 :
1248 794462113 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1249 : /* If the two DECLs are not even the same kind of thing, we're not
1250 : interested in their types. */
1251 : return 0;
1252 :
1253 408223677 : gcc_assert (DECL_P (newdecl));
1254 :
1255 408223677 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1256 : {
1257 : /* Specializations of different templates are different functions
1258 : even if they have the same type. */
1259 260704170 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1260 260704170 : ? DECL_TI_TEMPLATE (newdecl)
1261 620011 : : NULL_TREE);
1262 260704170 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1263 260704170 : ? DECL_TI_TEMPLATE (olddecl)
1264 3875248 : : NULL_TREE);
1265 260704170 : if (t1 != t2)
1266 : return 0;
1267 :
1268 257448863 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1269 260329524 : && ! (DECL_EXTERN_C_P (newdecl)
1270 2880661 : && DECL_EXTERN_C_P (olddecl)))
1271 : return 0;
1272 :
1273 : /* A new declaration doesn't match a built-in one unless it
1274 : is also extern "C". */
1275 236803878 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1276 236803878 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1277 : return 0;
1278 :
1279 236168375 : tree f1 = TREE_TYPE (newdecl);
1280 236168375 : tree f2 = TREE_TYPE (olddecl);
1281 236168375 : if (TREE_CODE (f1) != TREE_CODE (f2))
1282 : return 0;
1283 :
1284 : /* A declaration with deduced return type should use its pre-deduction
1285 : type for declaration matching. */
1286 236002583 : tree r2 = fndecl_declared_return_type (olddecl);
1287 236002583 : tree r1 = fndecl_declared_return_type (newdecl);
1288 :
1289 : /* For memchr et al, allow const void* return type (as specified by C++)
1290 : when we expect void* (as in C). */
1291 236002583 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1292 6285832 : && DECL_EXTERN_C_P (olddecl)
1293 3894194 : && !same_type_p (r1, r2)
1294 70 : && TREE_CODE (r1) == POINTER_TYPE
1295 15 : && TREE_CODE (r2) == POINTER_TYPE
1296 236002595 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1297 : r2 = r1;
1298 :
1299 236002583 : tree p1 = TYPE_ARG_TYPES (f1);
1300 236002583 : tree p2 = TYPE_ARG_TYPES (f2);
1301 :
1302 236002583 : if (same_type_p (r1, r2))
1303 : {
1304 162115826 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1305 162115798 : && fndecl_built_in_p (olddecl))
1306 : {
1307 50 : types_match = self_promoting_args_p (p1);
1308 50 : if (p1 == void_list_node)
1309 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1310 : }
1311 : else
1312 162115692 : types_match =
1313 162115692 : compparms (p1, p2)
1314 28014093 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1315 190129487 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1316 1182054 : || comp_type_attributes (TREE_TYPE (newdecl),
1317 1182054 : TREE_TYPE (olddecl)) != 0);
1318 : }
1319 : else
1320 : types_match = 0;
1321 :
1322 : /* Two function declarations match if either has a requires-clause
1323 : then both have a requires-clause and their constraints-expressions
1324 : are equivalent. */
1325 28013824 : if (types_match && flag_concepts)
1326 27539701 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1327 :
1328 : /* The decls dont match if they correspond to two different versions
1329 : of the same function. Disallow extern "C" functions to be
1330 : versions for now. */
1331 28013824 : if (types_match
1332 27830541 : && !DECL_EXTERN_C_P (newdecl)
1333 20309070 : && !DECL_EXTERN_C_P (olddecl)
1334 48319074 : && disjoint_version_decls (newdecl, olddecl))
1335 : {
1336 8850 : if (record_versions)
1337 8763 : maybe_version_functions (newdecl, olddecl);
1338 8850 : return 0;
1339 : }
1340 : }
1341 147519507 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1342 : {
1343 145836522 : if (!template_heads_equivalent_p (newdecl, olddecl))
1344 : return 0;
1345 :
1346 48273424 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1347 48273424 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1348 :
1349 48273424 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1350 : return 0;
1351 :
1352 : /* Two template types match if they are the same. Otherwise, compare
1353 : the underlying declarations. */
1354 48273418 : if (TREE_CODE (newres) == TYPE_DECL)
1355 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1356 : else
1357 48273388 : types_match = decls_match (newres, oldres);
1358 : }
1359 : else
1360 : {
1361 : /* Need to check scope for variable declaration (VAR_DECL).
1362 : For typedef (TYPE_DECL), scope is ignored. */
1363 1682985 : if (VAR_P (newdecl)
1364 1148354 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1365 : /* [dcl.link]
1366 : Two declarations for an object with C language linkage
1367 : with the same name (ignoring the namespace that qualify
1368 : it) that appear in different namespace scopes refer to
1369 : the same object. */
1370 1867235 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1371 : return 0;
1372 :
1373 1498783 : if (TREE_TYPE (newdecl) == error_mark_node)
1374 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1375 1498783 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1376 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1377 1498783 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1378 : types_match = 0;
1379 : else
1380 1498783 : types_match = comptypes (TREE_TYPE (newdecl),
1381 1498783 : TREE_TYPE (olddecl),
1382 : COMPARE_REDECLARATION);
1383 : }
1384 :
1385 : return types_match;
1386 : }
1387 :
1388 : /* Mark DECL as versioned if it isn't already. */
1389 :
1390 : void
1391 17616 : maybe_mark_function_versioned (tree decl)
1392 : {
1393 17616 : if (!DECL_FUNCTION_VERSIONED (decl))
1394 : {
1395 : /* We need to insert function version now to make sure the correct
1396 : pre-mangled assembler name is recorded. */
1397 1053 : cgraph_node *node = cgraph_node::get_create (decl);
1398 :
1399 1053 : if (!node->function_version ())
1400 1053 : node->insert_new_function_version ();
1401 :
1402 1053 : DECL_FUNCTION_VERSIONED (decl) = 1;
1403 : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1404 : to include the version marker. */
1405 1053 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1406 1047 : mangle_decl (decl);
1407 : }
1408 17616 : }
1409 :
1410 : /* NEWDECL and OLDDECL have identical signatures. If they are
1411 : different versions adjust them, record function versions, and return
1412 : true. */
1413 :
1414 : bool
1415 195974 : maybe_version_functions (tree newdecl, tree olddecl)
1416 : {
1417 195974 : if (!disjoint_version_decls (newdecl, olddecl))
1418 : return false;
1419 :
1420 8790 : maybe_mark_function_versioned (olddecl);
1421 8790 : if (DECL_LOCAL_DECL_P (olddecl))
1422 : {
1423 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1424 18 : maybe_mark_function_versioned (olddecl);
1425 : }
1426 :
1427 8790 : maybe_mark_function_versioned (newdecl);
1428 8790 : if (DECL_LOCAL_DECL_P (newdecl))
1429 : {
1430 : /* Unfortunately, we can get here before pushdecl naturally calls
1431 : push_local_extern_decl_alias, so we need to call it directly. */
1432 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1433 6 : push_local_extern_decl_alias (newdecl);
1434 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1435 18 : maybe_mark_function_versioned (newdecl);
1436 : }
1437 :
1438 : /* Add the new version to the function version structure. */
1439 8790 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1440 8790 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1441 8790 : if (!fn_v)
1442 15 : fn_v = fn_node->insert_new_function_version ();
1443 :
1444 8790 : cgraph_node::add_function_version (fn_v, newdecl);
1445 :
1446 8790 : return true;
1447 : }
1448 :
1449 : /* If NEWDECL is `static' and an `extern' was seen previously,
1450 : warn about it. OLDDECL is the previous declaration.
1451 :
1452 : Note that this does not apply to the C++ case of declaring
1453 : a variable `extern const' and then later `const'.
1454 :
1455 : Don't complain about built-in functions, since they are beyond
1456 : the user's control. */
1457 :
1458 : void
1459 19095431 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1460 : {
1461 19095431 : if (TREE_CODE (newdecl) == TYPE_DECL
1462 19049322 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1463 15125729 : || TREE_CODE (newdecl) == CONST_DECL
1464 15125729 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1465 19095419 : return;
1466 :
1467 : /* Don't get confused by static member functions; that's a different
1468 : use of `static'. */
1469 15125729 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1470 15125729 : && DECL_STATIC_FUNCTION_P (newdecl))
1471 : return;
1472 :
1473 : /* If the old declaration was `static', or the new one isn't, then
1474 : everything is OK. */
1475 14958057 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1476 : return;
1477 :
1478 : /* It's OK to declare a builtin function as `static'. */
1479 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1480 12 : && DECL_ARTIFICIAL (olddecl))
1481 : return;
1482 :
1483 12 : auto_diagnostic_group d;
1484 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1485 : "%qD was declared %<extern%> and later %<static%>", newdecl))
1486 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1487 : "previous declaration of %qD", olddecl);
1488 12 : }
1489 :
1490 : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1491 : function templates. If their exception specifications do not
1492 : match, issue a diagnostic. */
1493 :
1494 : static void
1495 18567823 : check_redeclaration_exception_specification (tree new_decl,
1496 : tree old_decl)
1497 : {
1498 18567823 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1499 18567823 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1500 :
1501 : /* Two default specs are equivalent, don't force evaluation. */
1502 7085618 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1503 18708413 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1504 : return;
1505 :
1506 18558254 : if (!type_dependent_expression_p (old_decl))
1507 : {
1508 11359393 : maybe_instantiate_noexcept (new_decl);
1509 11359393 : maybe_instantiate_noexcept (old_decl);
1510 : }
1511 18558254 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1512 18558254 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1513 :
1514 : /* [except.spec]
1515 :
1516 : If any declaration of a function has an exception-specification,
1517 : all declarations, including the definition and an explicit
1518 : specialization, of that function shall have an
1519 : exception-specification with the same set of type-ids. */
1520 18558254 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1521 14783340 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1522 33341490 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1523 : {
1524 326 : const char *const msg
1525 : = G_("declaration of %qF has a different exception specifier");
1526 326 : bool complained = true;
1527 326 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1528 326 : auto_diagnostic_group d;
1529 :
1530 : /* Be permissive about C++98 vs C++11 operator new declarations. */
1531 652 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1532 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1533 328 : && (nothrow_spec_p (new_exceptions)
1534 1 : == nothrow_spec_p (old_exceptions)));
1535 :
1536 326 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1537 326 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1538 : /* Don't fuss about the C library; the C library functions are not
1539 : specified to have exception specifications (just behave as if they
1540 : have them), but some implementations include them. */
1541 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1542 93 : else if (!flag_exceptions)
1543 : /* We used to silently permit mismatched eh specs with
1544 : -fno-exceptions, so only complain if -pedantic. */
1545 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1546 90 : else if (!new_exceptions || global_new)
1547 : /* Reduce to pedwarn for omitted exception specification. No warning
1548 : flag for this; silence the warning by correcting the code. */
1549 21 : complained = pedwarn (new_loc, 0, msg, new_decl);
1550 : else
1551 69 : error_at (new_loc, msg, new_decl);
1552 :
1553 326 : if (complained)
1554 96 : inform (DECL_SOURCE_LOCATION (old_decl),
1555 : "from previous declaration %qF", old_decl);
1556 :
1557 : /* Copy the old exception specification if new_decl has none. Unless the
1558 : old decl is extern "C", as obscure code might depend on the type of
1559 : the new declaration (e.g. noexcept-type19.C). */
1560 326 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1561 20 : TREE_TYPE (new_decl)
1562 40 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1563 326 : }
1564 : }
1565 :
1566 : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1567 : Otherwise issue diagnostics. */
1568 :
1569 : bool
1570 19095940 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1571 : {
1572 19095940 : old_decl = STRIP_TEMPLATE (old_decl);
1573 19095940 : new_decl = STRIP_TEMPLATE (new_decl);
1574 19095940 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1575 19049807 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1576 : return true;
1577 19049807 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1578 19049807 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1579 : {
1580 18910385 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1581 : return true;
1582 37136458 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1583 18568229 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1584 : return true;
1585 : }
1586 139428 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1587 : {
1588 : /* With -fimplicit-constexpr, ignore changes in the constexpr
1589 : keyword. */
1590 69 : if (flag_implicit_constexpr
1591 69 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1592 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1593 : return true;
1594 69 : if (fndecl_built_in_p (old_decl))
1595 : {
1596 : /* Hide a built-in declaration. */
1597 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1598 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1599 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1600 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1601 0 : return true;
1602 : }
1603 : /* 7.1.5 [dcl.constexpr]
1604 : Note: An explicit specialization can differ from the template
1605 : declaration with respect to the constexpr specifier. */
1606 69 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1607 69 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1608 : return true;
1609 :
1610 42 : const char *kind = "constexpr";
1611 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1612 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1613 : kind = "consteval";
1614 42 : auto_diagnostic_group d;
1615 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1616 : "redeclaration %qD differs in %qs "
1617 : "from previous declaration", new_decl,
1618 : kind);
1619 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1620 : "previous declaration %qD", old_decl);
1621 42 : return false;
1622 42 : }
1623 : return true;
1624 : }
1625 :
1626 : /* DECL is a redeclaration of a function or function template. If
1627 : it does have default arguments issue a diagnostic. Note: this
1628 : function is used to enforce the requirements in C++11 8.3.6 about
1629 : no default arguments in redeclarations. */
1630 :
1631 : static void
1632 11195773 : check_redeclaration_no_default_args (tree decl)
1633 : {
1634 11195773 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1635 :
1636 11195773 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1637 32923011 : t && t != void_list_node; t = TREE_CHAIN (t))
1638 21727295 : if (TREE_PURPOSE (t))
1639 : {
1640 57 : permerror (DECL_SOURCE_LOCATION (decl),
1641 : "redeclaration of %q#D may not have default "
1642 : "arguments", decl);
1643 57 : return;
1644 : }
1645 : }
1646 :
1647 : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1648 : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1649 : the TEMPLATE_DECLs in case of function templates). This function is used
1650 : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1651 : "If a friend declaration specifies a default argument expression, that
1652 : declaration shall be a definition and shall be the only declaration of
1653 : the function or function template in the translation unit." */
1654 :
1655 : static void
1656 6857378 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1657 : {
1658 13024412 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1659 : return;
1660 :
1661 3541242 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1662 1770621 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1663 4673580 : t1 && t1 != void_list_node;
1664 2902959 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1665 4136355 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1666 8281796 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1667 : {
1668 36 : auto_diagnostic_group d;
1669 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1670 : "friend declaration of %q#D specifies default "
1671 : "arguments and isn%'t the only declaration", newdecl))
1672 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1673 : "previous declaration of %q#D", olddecl);
1674 36 : return;
1675 36 : }
1676 : }
1677 :
1678 : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1679 : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1680 :
1681 : static void
1682 18024154 : merge_attribute_bits (tree newdecl, tree olddecl)
1683 : {
1684 18024154 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1685 18024154 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1686 18024154 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1687 18024154 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1688 18024154 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1689 18024154 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1690 18024154 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1691 18024154 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1692 18024154 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1693 18024154 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1694 18024154 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1695 18024154 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1696 18024154 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1697 18024154 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1698 18024154 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1699 18024154 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1700 18024154 : }
1701 :
1702 : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1703 : && lookup_attribute ("gnu_inline", \
1704 : DECL_ATTRIBUTES (fn)))
1705 :
1706 : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1707 : ambiguates olddecl. Returns true if an error occurs. */
1708 :
1709 : static bool
1710 141912773 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1711 : {
1712 141912773 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1713 141912773 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1714 : /* Function template declarations can be differentiated by parameter
1715 : and return type. */
1716 141912773 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1717 141912773 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1718 141912773 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1719 : TREE_TYPE (TREE_TYPE (olddecl))))
1720 : {
1721 : /* ... and also by their template-heads and requires-clauses. */
1722 6602 : if (template_heads_equivalent_p (newdecl, olddecl)
1723 6602 : && function_requirements_equivalent_p (newres, oldres))
1724 : {
1725 0 : auto_diagnostic_group d;
1726 0 : error ("ambiguating new declaration %q+#D", newdecl);
1727 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1728 : "old declaration %q#D", olddecl);
1729 0 : return true;
1730 0 : }
1731 :
1732 : /* FIXME: The types are the same but the are differences
1733 : in either the template heads or function requirements.
1734 : We should be able to diagnose a set of common errors
1735 : stemming from these declarations. For example:
1736 :
1737 : template<typename T> requires C void f(...);
1738 : template<typename T> void f(...) requires C;
1739 :
1740 : These are functionally equivalent but not equivalent. */
1741 : }
1742 :
1743 : return false;
1744 : }
1745 :
1746 : /* OLD_PARMS is the innermost set of template parameters for some template
1747 : declaration, and NEW_PARMS is the corresponding set of template parameters
1748 : for a redeclaration of that template. Merge the default arguments within
1749 : these two sets of parameters. CLASS_P is true iff the template in
1750 : question is a class template. */
1751 :
1752 : bool
1753 8671417 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1754 : {
1755 8671417 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1756 : == TREE_VEC_LENGTH (old_parms));
1757 24607326 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1758 : {
1759 15935939 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1760 15935939 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1761 15935939 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1762 15935939 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1763 15935939 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1764 : return false;
1765 15935936 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1766 : {
1767 27 : auto_diagnostic_group d;
1768 27 : error ("redefinition of default argument for %q+#D", new_parm);
1769 27 : inform (DECL_SOURCE_LOCATION (old_parm),
1770 : "original definition appeared here");
1771 27 : return false;
1772 27 : }
1773 15935909 : else if (new_default != NULL_TREE)
1774 : /* Update the previous template parameters (which are the ones
1775 : that will really count) with the new default value. */
1776 204741 : old_default = new_default;
1777 15731168 : else if (class_p && old_default != NULL_TREE)
1778 : /* Update the new parameters, too; they'll be used as the
1779 : parameters for any members. */
1780 1133158 : new_default = old_default;
1781 : }
1782 : return true;
1783 : }
1784 :
1785 : /* Helper function for duplicate_decls and push_local_extern_decl_alias.
1786 : Merge parameter attributes and names between NEWDECL and OLDDECL.
1787 : NEW_DEFINES_FUNCTION and TYPES_MATCH argument like variables in
1788 : duplicate_decls, EXTERN_ALIAS false for duplicate_decls and true for
1789 : push_local_extern_decl_alias. */
1790 :
1791 : void
1792 14644663 : merge_decl_arguments (tree newdecl, tree olddecl, bool new_defines_function,
1793 : bool types_match, bool extern_alias)
1794 : {
1795 14644663 : tree oldarg, newarg, type = NULL_TREE;
1796 14644663 : tree first_user_parm = NULL_TREE;
1797 14644663 : if (extern_alias)
1798 178 : first_user_parm = FUNCTION_FIRST_USER_PARM (newdecl);
1799 14644663 : for (oldarg = DECL_ARGUMENTS (olddecl), newarg = DECL_ARGUMENTS (newdecl);
1800 40943925 : oldarg && newarg;
1801 26299262 : oldarg = DECL_CHAIN (oldarg), newarg = DECL_CHAIN (newarg))
1802 : {
1803 26299262 : DECL_ATTRIBUTES (newarg)
1804 26299262 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
1805 26299262 : if (lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (newarg))
1806 26299331 : && !lookup_attribute (NULL, "indeterminate",
1807 69 : DECL_ATTRIBUTES (oldarg)))
1808 : {
1809 18 : auto_diagnostic_group d;
1810 18 : error_at (DECL_SOURCE_LOCATION (newarg),
1811 : "%<indeterminate%> attribute not specified for parameter "
1812 : "%qD on the first declaration of its function", newarg);
1813 18 : inform (DECL_SOURCE_LOCATION (oldarg), "earlier declaration");
1814 18 : }
1815 : /* ??? Should attributes propagate out from a block extern? If so,
1816 : we should do that for the function itself, not just parameters. */
1817 26299262 : if (!extern_alias || flag_reflection)
1818 26299099 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1819 26299262 : if (!flag_reflection)
1820 26138892 : continue;
1821 : /* For extern_alias set DECL_HAS_DEFAULT_ARGUMENT_P on oldarg
1822 : if the local extern has a default argument for that parameter. */
1823 160370 : if (extern_alias)
1824 : {
1825 17 : if (newarg == first_user_parm)
1826 5 : type = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1827 12 : else if (type)
1828 12 : type = TREE_CHAIN (type);
1829 34 : if (type && TREE_PURPOSE (type))
1830 2 : DECL_HAS_DEFAULT_ARGUMENT_P (oldarg) = 1;
1831 : }
1832 : else
1833 : {
1834 : /* Otherwise propagate the flag. */
1835 160353 : if (DECL_HAS_DEFAULT_ARGUMENT_P (oldarg))
1836 0 : DECL_HAS_DEFAULT_ARGUMENT_P (newarg) = 1;
1837 160353 : if (DECL_HAS_DEFAULT_ARGUMENT_P (newarg))
1838 0 : DECL_HAS_DEFAULT_ARGUMENT_P (oldarg) = 1;
1839 : }
1840 : /* Merge names for std::meta::has_identifier and
1841 : std::meta::{,u8}identifier_of purposes. If they are different and
1842 : both oldarg and newarg are named, add flag to force that
1843 : std::meta::has_identifier returns false. If one is named and one is
1844 : unnamed, if neither is a olddecl nor newdecl is definition, propagate
1845 : DECL_NAME to both. Otherwise stash the old name into "old parm name"
1846 : artificial attribute. */
1847 160370 : if (DECL_NAME (oldarg) != DECL_NAME (newarg))
1848 : {
1849 18100 : if (DECL_NAME (oldarg) && DECL_NAME (newarg))
1850 : {
1851 : /* Different names. */
1852 3836 : MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1853 3836 : MULTIPLE_NAMES_PARM_P (newarg) = 1;
1854 : }
1855 14264 : else if (!new_defines_function
1856 14264 : && types_match
1857 14264 : && DECL_INITIAL (olddecl) == NULL_TREE)
1858 : {
1859 : /* For 2 non-definitions with matching types, one is named and
1860 : one unnamed, propagate name to both. */
1861 2723 : if (DECL_NAME (oldarg))
1862 2283 : DECL_NAME (newarg) = DECL_NAME (oldarg);
1863 : else
1864 440 : DECL_NAME (oldarg) = DECL_NAME (newarg);
1865 : }
1866 : /* Depending on which PARM_DECL we'll keep, look at the other
1867 : PARM_DECL's name. */
1868 11541 : else if (tree name = ((new_defines_function || !types_match)
1869 11541 : ? DECL_NAME (oldarg) : DECL_NAME (newarg)))
1870 : {
1871 93 : tree opn = lookup_attribute ("old parm name",
1872 93 : DECL_ATTRIBUTES (oldarg));
1873 93 : if (opn)
1874 : {
1875 0 : if (TREE_VALUE (TREE_VALUE (opn)) == name)
1876 : /* Name already in "old parm name" attribute. */;
1877 : else
1878 : {
1879 : /* Different names. */
1880 0 : MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1881 0 : MULTIPLE_NAMES_PARM_P (newarg) = 1;
1882 : }
1883 : }
1884 : else
1885 : {
1886 : /* Save name into attribute. */
1887 93 : DECL_ATTRIBUTES (newarg)
1888 93 : = tree_cons (get_identifier ("old parm name"),
1889 : tree_cons (NULL_TREE, name, NULL_TREE),
1890 93 : DECL_ATTRIBUTES (newarg));
1891 93 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1892 : }
1893 : }
1894 11448 : else if (extern_alias)
1895 1 : DECL_NAME (newarg) = DECL_NAME (oldarg);
1896 : }
1897 : }
1898 14644663 : }
1899 :
1900 : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1901 : If the redeclaration is invalid, a diagnostic is issued, and the
1902 : error_mark_node is returned. Otherwise, OLDDECL is returned.
1903 :
1904 : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1905 : returned.
1906 :
1907 : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1908 : if the old decl was hidden.
1909 :
1910 : Hidden decls can be anticipated builtins, injected friends, or
1911 : (coming soon) injected from a local-extern decl. */
1912 :
1913 : tree
1914 292708362 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1915 : {
1916 292708362 : unsigned olddecl_uid = DECL_UID (olddecl);
1917 292708362 : int types_match = 0;
1918 292708362 : int new_defines_function = 0;
1919 292708362 : tree new_template_info;
1920 292708362 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1921 292708362 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1922 :
1923 292708362 : if (newdecl == olddecl)
1924 : return olddecl;
1925 :
1926 291897608 : types_match = decls_match (newdecl, olddecl);
1927 :
1928 : /* If either the type of the new decl or the type of the old decl is an
1929 : error_mark_node, then that implies that we have already issued an
1930 : error (earlier) for some bogus type specification, and in that case,
1931 : it is rather pointless to harass the user with yet more error message
1932 : about the same declaration, so just pretend the types match here. */
1933 291897608 : if (TREE_TYPE (newdecl) == error_mark_node
1934 291897608 : || TREE_TYPE (olddecl) == error_mark_node)
1935 : return error_mark_node;
1936 :
1937 : /* Check for redeclaration and other discrepancies. */
1938 291897583 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1939 291897583 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1940 : {
1941 6093101 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1942 : {
1943 : /* Avoid warnings redeclaring built-ins which have not been
1944 : explicitly declared. */
1945 182793 : if (was_hidden)
1946 : {
1947 182751 : if (TREE_PUBLIC (newdecl)
1948 182751 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1949 12 : warning_at (newdecl_loc,
1950 12 : OPT_Wbuiltin_declaration_mismatch,
1951 : "built-in function %qD declared as non-function",
1952 : newdecl);
1953 182751 : return NULL_TREE;
1954 : }
1955 :
1956 : /* If you declare a built-in or predefined function name as static,
1957 : the old definition is overridden, but optionally warn this was a
1958 : bad choice of name. */
1959 42 : if (! TREE_PUBLIC (newdecl))
1960 : {
1961 42 : warning_at (newdecl_loc,
1962 42 : OPT_Wshadow,
1963 42 : fndecl_built_in_p (olddecl)
1964 : ? G_("shadowing built-in function %q#D")
1965 : : G_("shadowing library function %q#D"), olddecl);
1966 : /* Discard the old built-in function. */
1967 42 : return NULL_TREE;
1968 : }
1969 : /* If the built-in is not ansi, then programs can override
1970 : it even globally without an error. */
1971 0 : else if (! fndecl_built_in_p (olddecl))
1972 0 : warning_at (newdecl_loc, 0,
1973 : "library function %q#D redeclared as non-function %q#D",
1974 : olddecl, newdecl);
1975 : else
1976 0 : error_at (newdecl_loc,
1977 : "declaration of %q#D conflicts with built-in "
1978 : "declaration %q#D", newdecl, olddecl);
1979 0 : return NULL_TREE;
1980 : }
1981 5910308 : else if (!types_match)
1982 : {
1983 : /* Avoid warnings redeclaring built-ins which have not been
1984 : explicitly declared. */
1985 2135394 : if (was_hidden)
1986 : {
1987 772935 : tree t1, t2;
1988 :
1989 : /* A new declaration doesn't match a built-in one unless it
1990 : is also extern "C". */
1991 772935 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1992 772935 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1993 772935 : if (!DECL_EXTERN_C_P (newdecl))
1994 : return NULL_TREE;
1995 :
1996 137432 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1997 137432 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1998 236561 : t1 || t2;
1999 99129 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2000 : {
2001 236543 : if (!t1 || !t2)
2002 : break;
2003 : /* FILE, tm types are not known at the time
2004 : we create the builtins. */
2005 731409 : for (unsigned i = 0;
2006 967790 : i < sizeof (builtin_structptr_types)
2007 : / sizeof (builtin_structptr_type);
2008 : ++i)
2009 868655 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
2010 : {
2011 137246 : tree t = TREE_VALUE (t1);
2012 :
2013 137246 : if (TYPE_PTR_P (t)
2014 274492 : && TYPE_IDENTIFIER (TREE_TYPE (t))
2015 137246 : == get_identifier (builtin_structptr_types[i].str)
2016 274477 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
2017 : {
2018 137231 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2019 :
2020 137231 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
2021 137231 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2022 137231 : types_match = decls_match (newdecl, olddecl);
2023 137231 : if (types_match)
2024 137231 : return duplicate_decls (newdecl, olddecl,
2025 137231 : hiding, was_hidden);
2026 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
2027 : }
2028 15 : goto next_arg;
2029 : }
2030 :
2031 99135 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2032 : break;
2033 99129 : next_arg:;
2034 : }
2035 :
2036 201 : warning_at (newdecl_loc,
2037 201 : OPT_Wbuiltin_declaration_mismatch,
2038 : "declaration of %q#D conflicts with built-in "
2039 : "declaration %q#D", newdecl, olddecl);
2040 : }
2041 1362459 : else if ((DECL_EXTERN_C_P (newdecl)
2042 15 : && DECL_EXTERN_C_P (olddecl))
2043 2724903 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2044 1362444 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2045 : {
2046 : /* Don't really override olddecl for __* prefixed builtins
2047 : except for __[^b]*_chk, the compiler might be using those
2048 : explicitly. */
2049 15 : if (fndecl_built_in_p (olddecl))
2050 : {
2051 15 : tree id = DECL_NAME (olddecl);
2052 15 : const char *name = IDENTIFIER_POINTER (id);
2053 15 : size_t len;
2054 :
2055 15 : if (name[0] == '_'
2056 15 : && name[1] == '_'
2057 30 : && (startswith (name + 2, "builtin_")
2058 6 : || (len = strlen (name)) <= strlen ("___chk")
2059 6 : || memcmp (name + len - strlen ("_chk"),
2060 : "_chk", strlen ("_chk") + 1) != 0))
2061 : {
2062 15 : if (DECL_INITIAL (newdecl))
2063 : {
2064 6 : error_at (newdecl_loc,
2065 : "definition of %q#D ambiguates built-in "
2066 : "declaration %q#D", newdecl, olddecl);
2067 6 : return error_mark_node;
2068 : }
2069 9 : auto_diagnostic_group d;
2070 9 : if (permerror (newdecl_loc,
2071 : "new declaration %q#D ambiguates built-in"
2072 : " declaration %q#D", newdecl, olddecl)
2073 9 : && flag_permissive)
2074 3 : inform (newdecl_loc,
2075 : "ignoring the %q#D declaration", newdecl);
2076 9 : return flag_permissive ? olddecl : error_mark_node;
2077 9 : }
2078 : }
2079 :
2080 : /* A near match; override the builtin. */
2081 :
2082 0 : if (TREE_PUBLIC (newdecl))
2083 0 : warning_at (newdecl_loc,
2084 0 : OPT_Wbuiltin_declaration_mismatch,
2085 : "new declaration %q#D ambiguates built-in "
2086 : "declaration %q#D", newdecl, olddecl);
2087 : else
2088 0 : warning (OPT_Wshadow,
2089 : fndecl_built_in_p (olddecl)
2090 : ? G_("shadowing built-in function %q#D")
2091 : : G_("shadowing library function %q#D"), olddecl);
2092 : }
2093 : else
2094 : /* Discard the old built-in function. */
2095 : return NULL_TREE;
2096 :
2097 : /* Replace the old RTL to avoid problems with inlining. */
2098 201 : COPY_DECL_RTL (newdecl, olddecl);
2099 : }
2100 : else
2101 : {
2102 : /* Even if the types match, prefer the new declarations type
2103 : for built-ins which have not been explicitly declared,
2104 : for exception lists, etc... */
2105 3774914 : tree type = TREE_TYPE (newdecl);
2106 3774914 : tree attribs = (*targetm.merge_type_attributes)
2107 3774914 : (TREE_TYPE (olddecl), type);
2108 :
2109 3774914 : type = cp_build_type_attribute_variant (type, attribs);
2110 3774914 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
2111 : }
2112 :
2113 : /* If a function is explicitly declared "throw ()", propagate that to
2114 : the corresponding builtin. */
2115 3775115 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
2116 3619452 : && was_hidden
2117 3619409 : && TREE_NOTHROW (newdecl)
2118 7144094 : && !TREE_NOTHROW (olddecl))
2119 : {
2120 26806 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
2121 26806 : tree tmpdecl = builtin_decl_explicit (fncode);
2122 26806 : if (tmpdecl && tmpdecl != olddecl && types_match)
2123 26806 : TREE_NOTHROW (tmpdecl) = 1;
2124 : }
2125 :
2126 : /* Whether or not the builtin can throw exceptions has no
2127 : bearing on this declarator. */
2128 3775115 : TREE_NOTHROW (olddecl) = 0;
2129 :
2130 3775115 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2131 : {
2132 : /* If a builtin function is redeclared as `static', merge
2133 : the declarations, but make the original one static. */
2134 3 : DECL_THIS_STATIC (olddecl) = 1;
2135 3 : TREE_PUBLIC (olddecl) = 0;
2136 :
2137 : /* Make the old declaration consistent with the new one so
2138 : that all remnants of the builtin-ness of this function
2139 : will be banished. */
2140 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2141 3 : COPY_DECL_RTL (newdecl, olddecl);
2142 : }
2143 : }
2144 285804482 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2145 : {
2146 : /* C++ Standard, 3.3, clause 4:
2147 : "[Note: a namespace name or a class template name must be unique
2148 : in its declarative region (7.3.2, clause 14). ]" */
2149 68995128 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
2150 68995101 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
2151 : /* Namespace conflicts with not namespace. */;
2152 33864582 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
2153 102859671 : || DECL_TYPE_TEMPLATE_P (newdecl))
2154 : /* Class template conflicts. */;
2155 68995083 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2156 33864582 : && DECL_TEMPLATE_RESULT (olddecl)
2157 33864582 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2158 102859659 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
2159 35083587 : && DECL_TEMPLATE_RESULT (newdecl)
2160 35083587 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2161 : /* Variable template conflicts. */;
2162 68995074 : else if (concept_definition_p (olddecl)
2163 137990142 : || concept_definition_p (newdecl))
2164 : /* Concept conflicts. */;
2165 68995062 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2166 33902142 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
2167 69032634 : || (TREE_CODE (olddecl) == FUNCTION_DECL
2168 35083597 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2169 : {
2170 : /* One is a function and the other is a template
2171 : function. */
2172 68948133 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2173 : return NULL_TREE;
2174 :
2175 : /* There can only be one! */
2176 55423 : auto_diagnostic_group d;
2177 55423 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
2178 55423 : && check_raw_literal_operator (olddecl))
2179 3 : error_at (newdecl_loc,
2180 : "literal operator %q#D conflicts with"
2181 : " raw literal operator", newdecl);
2182 55420 : else if (check_raw_literal_operator (newdecl))
2183 3 : error_at (newdecl_loc,
2184 : "raw literal operator %q#D conflicts with"
2185 : " literal operator template", newdecl);
2186 : else
2187 : return NULL_TREE;
2188 :
2189 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2190 6 : return error_mark_node;
2191 55423 : }
2192 46929 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2193 : /* A structured binding must be unique in its declarative region. */;
2194 37720 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2195 46926 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
2196 : /* One is an implicit typedef, that's ok. */
2197 : return NULL_TREE;
2198 :
2199 153 : auto_diagnostic_group d;
2200 153 : error ("%q#D redeclared as different kind of entity", newdecl);
2201 153 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2202 :
2203 153 : return error_mark_node;
2204 153 : }
2205 216809354 : else if (!types_match)
2206 : {
2207 201457332 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2208 : /* These are certainly not duplicate declarations; they're
2209 : from different scopes. */
2210 : return NULL_TREE;
2211 :
2212 201457272 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2213 : {
2214 141912791 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2215 141912791 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
2216 :
2217 : /* The name of a class template may not be declared to refer to
2218 : any other template, class, function, object, namespace, value,
2219 : or type in the same scope. */
2220 141912791 : if (TREE_CODE (oldres) == TYPE_DECL
2221 141912785 : || TREE_CODE (newres) == TYPE_DECL)
2222 : {
2223 9 : auto_diagnostic_group d;
2224 9 : error_at (newdecl_loc,
2225 : "conflicting declaration of template %q#D", newdecl);
2226 9 : inform (olddecl_loc,
2227 : "previous declaration %q#D", olddecl);
2228 9 : return error_mark_node;
2229 9 : }
2230 :
2231 141912782 : else if (TREE_CODE (oldres) == FUNCTION_DECL
2232 141912773 : && TREE_CODE (newres) == FUNCTION_DECL)
2233 : {
2234 141912773 : if (duplicate_function_template_decls (newdecl, olddecl))
2235 0 : return error_mark_node;
2236 : return NULL_TREE;
2237 : }
2238 : return NULL_TREE;
2239 : }
2240 59544481 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2241 : {
2242 59544332 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2243 : {
2244 57 : auto_diagnostic_group d;
2245 57 : error_at (newdecl_loc,
2246 : "conflicting declaration of C function %q#D",
2247 : newdecl);
2248 57 : inform (olddecl_loc,
2249 : "previous declaration %q#D", olddecl);
2250 57 : return error_mark_node;
2251 57 : }
2252 : /* For function versions, params and types match, but they
2253 : are not ambiguous. */
2254 59544275 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2255 59535971 : && !DECL_FUNCTION_VERSIONED (olddecl))
2256 8349 : || !same_type_p (fndecl_declared_return_type (newdecl),
2257 : fndecl_declared_return_type (olddecl)))
2258 : /* Let constrained hidden friends coexist for now, we'll
2259 : check satisfaction later. */
2260 59535926 : && !member_like_constrained_friend_p (newdecl)
2261 56814753 : && !member_like_constrained_friend_p (olddecl)
2262 : // The functions have the same parameter types.
2263 56291038 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2264 56291038 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2265 : // And the same constraints.
2266 59544344 : && equivalently_constrained (newdecl, olddecl))
2267 : {
2268 69 : auto_diagnostic_group d;
2269 69 : error_at (newdecl_loc,
2270 : "ambiguating new declaration of %q#D", newdecl);
2271 69 : inform (olddecl_loc,
2272 : "old declaration %q#D", olddecl);
2273 69 : return error_mark_node;
2274 69 : }
2275 : else
2276 59544206 : return NULL_TREE;
2277 : }
2278 : else
2279 : {
2280 149 : auto_diagnostic_group d;
2281 149 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2282 149 : inform (olddecl_loc,
2283 : "previous declaration as %q#D", olddecl);
2284 149 : return error_mark_node;
2285 149 : }
2286 : }
2287 15352022 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2288 15352022 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2289 : {
2290 : /* OMP UDRs are never duplicates. */
2291 33 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2292 33 : auto_diagnostic_group d;
2293 33 : error_at (newdecl_loc,
2294 : "redeclaration of %<pragma omp declare reduction%>");
2295 33 : inform (olddecl_loc,
2296 : "previous %<pragma omp declare reduction%> declaration");
2297 33 : return error_mark_node;
2298 33 : }
2299 15351989 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2300 15351989 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2301 222 : && (!DECL_TEMPLATE_INFO (newdecl)
2302 222 : || (DECL_TI_TEMPLATE (newdecl)
2303 222 : != DECL_TI_TEMPLATE (olddecl))))
2304 10869652 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2305 543933 : && (!DECL_TEMPLATE_INFO (olddecl)
2306 543933 : || (DECL_TI_TEMPLATE (olddecl)
2307 543933 : != DECL_TI_TEMPLATE (newdecl))))))
2308 : /* It's OK to have a template specialization and a non-template
2309 : with the same type, or to have specializations of two
2310 : different templates with the same type. Note that if one is a
2311 : specialization, and the other is an instantiation of the same
2312 : template, that we do not exit at this point. That situation
2313 : can occur if we instantiate a template class, and then
2314 : specialize one of its methods. This situation is valid, but
2315 : the declarations must be merged in the usual way. */
2316 : return NULL_TREE;
2317 15351989 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2318 15351989 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2319 619719 : && !DECL_USE_TEMPLATE (newdecl))
2320 10869652 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2321 76008 : && !DECL_USE_TEMPLATE (olddecl))))
2322 : /* One of the declarations is a template instantiation, and the
2323 : other is not a template at all. That's OK. */
2324 : return NULL_TREE;
2325 15351989 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2326 : {
2327 : /* In [namespace.alias] we have:
2328 :
2329 : In a declarative region, a namespace-alias-definition can be
2330 : used to redefine a namespace-alias declared in that declarative
2331 : region to refer only to the namespace to which it already
2332 : refers.
2333 :
2334 : Therefore, if we encounter a second alias directive for the same
2335 : alias, we can just ignore the second directive. */
2336 23 : if (DECL_NAMESPACE_ALIAS (newdecl)
2337 23 : && (DECL_NAMESPACE_ALIAS (newdecl)
2338 20 : == DECL_NAMESPACE_ALIAS (olddecl)))
2339 : return olddecl;
2340 :
2341 : /* Leave it to update_binding to merge or report error. */
2342 : return NULL_TREE;
2343 : }
2344 : /* Check if the two decls are non-mergeable versioned decls. */
2345 15351966 : else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2346 : && diagnose_versioned_decls (olddecl, newdecl))
2347 : return error_mark_node;
2348 : else
2349 : {
2350 15351966 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2351 15351966 : if (errmsg)
2352 : {
2353 621 : auto_diagnostic_group d;
2354 621 : error_at (newdecl_loc, errmsg, newdecl);
2355 621 : if (DECL_NAME (olddecl) != NULL_TREE)
2356 621 : inform (olddecl_loc,
2357 621 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2358 : ? G_("%q#D previously defined here")
2359 : : G_("%q#D previously declared here"), olddecl);
2360 621 : if (cxx_dialect >= cxx26
2361 149 : && DECL_NAME (newdecl)
2362 149 : && id_equal (DECL_NAME (newdecl), "_")
2363 634 : && !name_independent_decl_p (newdecl))
2364 : {
2365 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2366 2 : inform (newdecl_loc,
2367 : "parameter declaration is not name-independent");
2368 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2369 : {
2370 3 : if (at_namespace_scope_p ())
2371 2 : inform (newdecl_loc,
2372 : "structured binding at namespace scope is not "
2373 : "name-independent");
2374 1 : else if (TREE_STATIC (newdecl))
2375 1 : inform (newdecl_loc,
2376 : "static structured binding is not "
2377 : "name-independent");
2378 0 : else if (DECL_EXTERNAL (newdecl))
2379 0 : inform (newdecl_loc,
2380 : "extern structured binding is not "
2381 : "name-independent");
2382 : }
2383 8 : else if (at_class_scope_p ()
2384 0 : && VAR_P (newdecl)
2385 8 : && TREE_STATIC (newdecl))
2386 0 : inform (newdecl_loc,
2387 : "static data member is not name-independent");
2388 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2389 3 : inform (newdecl_loc,
2390 : "variable at namespace scope is not name-independent");
2391 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2392 3 : inform (newdecl_loc,
2393 : "static variable is not name-independent");
2394 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2395 0 : inform (newdecl_loc,
2396 : "extern variable is not name-independent");
2397 : }
2398 621 : return error_mark_node;
2399 621 : }
2400 15351345 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2401 10869415 : && DECL_INITIAL (olddecl) != NULL_TREE
2402 11039 : && !prototype_p (TREE_TYPE (olddecl))
2403 15351345 : && prototype_p (TREE_TYPE (newdecl)))
2404 : {
2405 : /* Prototype decl follows defn w/o prototype. */
2406 0 : auto_diagnostic_group d;
2407 0 : if (warning_at (newdecl_loc, 0,
2408 : "prototype specified for %q#D", newdecl))
2409 0 : inform (olddecl_loc,
2410 : "previous non-prototype definition here");
2411 0 : }
2412 14870101 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2413 26220760 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2414 : {
2415 : /* [dcl.link]
2416 : If two declarations of the same function or object
2417 : specify different linkage-specifications ..., the program
2418 : is ill-formed.... Except for functions with C++ linkage,
2419 : a function declaration without a linkage specification
2420 : shall not precede the first linkage specification for
2421 : that function. A function can be declared without a
2422 : linkage specification after an explicit linkage
2423 : specification has been seen; the linkage explicitly
2424 : specified in the earlier declaration is not affected by
2425 : such a function declaration.
2426 :
2427 : DR 563 raises the question why the restrictions on
2428 : functions should not also apply to objects. Older
2429 : versions of G++ silently ignore the linkage-specification
2430 : for this example:
2431 :
2432 : namespace N {
2433 : extern int i;
2434 : extern "C" int i;
2435 : }
2436 :
2437 : which is clearly wrong. Therefore, we now treat objects
2438 : like functions. */
2439 1799 : if (current_lang_depth () == 0)
2440 : {
2441 : /* There is no explicit linkage-specification, so we use
2442 : the linkage from the previous declaration. */
2443 1787 : retrofit_lang_decl (newdecl);
2444 1787 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2445 : }
2446 : else
2447 : {
2448 12 : auto_diagnostic_group d;
2449 12 : error_at (newdecl_loc,
2450 : "conflicting declaration of %q#D with %qL linkage",
2451 12 : newdecl, DECL_LANGUAGE (newdecl));
2452 12 : inform (olddecl_loc,
2453 : "previous declaration with %qL linkage",
2454 12 : DECL_LANGUAGE (olddecl));
2455 12 : }
2456 : }
2457 :
2458 15351345 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2459 : ;
2460 14711560 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2461 : {
2462 : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2463 12780424 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2464 10249483 : && (/* grokfndecl passes member function templates too
2465 : as FUNCTION_DECLs. */
2466 7886086 : DECL_TEMPLATE_INFO (olddecl)
2467 : /* C++11 8.3.6/6.
2468 : Default arguments for a member function of a class
2469 : template shall be specified on the initial declaration
2470 : of the member function within the class template. */
2471 669245 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2472 : {
2473 7293939 : check_redeclaration_no_default_args (newdecl);
2474 :
2475 7293939 : if (DECL_TEMPLATE_INFO (olddecl)
2476 7293939 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2477 : {
2478 2118382 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2479 2118382 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2480 2118382 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2481 2118382 : tree old_parms
2482 2118382 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2483 2118382 : merge_default_template_args (new_parms, old_parms,
2484 : /*class_p=*/false);
2485 : }
2486 : }
2487 : else
2488 : {
2489 2955544 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2490 2955544 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2491 2955544 : int i = 1;
2492 :
2493 7617576 : for (; t1 && t1 != void_list_node;
2494 4662032 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2495 4663845 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2496 : {
2497 57 : if (simple_cst_equal (TREE_PURPOSE (t1),
2498 57 : TREE_PURPOSE (t2)) == 1)
2499 : {
2500 24 : auto_diagnostic_group d;
2501 24 : if (permerror (newdecl_loc,
2502 : "default argument given for parameter "
2503 : "%d of %q#D", i, newdecl))
2504 24 : inform (olddecl_loc,
2505 : "previous specification in %q#D here",
2506 : olddecl);
2507 24 : }
2508 : else
2509 : {
2510 33 : auto_diagnostic_group d;
2511 33 : error_at (newdecl_loc,
2512 : "default argument given for parameter %d "
2513 : "of %q#D", i, newdecl);
2514 33 : inform (olddecl_loc,
2515 : "previous specification in %q#D here",
2516 : olddecl);
2517 33 : }
2518 : }
2519 :
2520 : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2521 : argument expression, that declaration... shall be the only
2522 : declaration of the function or function template in the
2523 : translation unit." */
2524 2955544 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2525 : }
2526 : }
2527 : }
2528 :
2529 : /* Do not merge an implicit typedef with an explicit one. In:
2530 :
2531 : class A;
2532 : ...
2533 : typedef class A A __attribute__ ((foo));
2534 :
2535 : the attribute should apply only to the typedef. */
2536 19126460 : if (TREE_CODE (olddecl) == TYPE_DECL
2537 19126460 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2538 46112 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2539 : return NULL_TREE;
2540 :
2541 19095497 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2542 : return NULL_TREE;
2543 :
2544 19095485 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2545 30 : return error_mark_node;
2546 :
2547 19095455 : if (modules_p ()
2548 45125 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2549 19122192 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2550 : {
2551 26737 : if (!module_may_redeclare (olddecl, newdecl))
2552 24 : return error_mark_node;
2553 :
2554 26713 : if (!hiding)
2555 : {
2556 : /* The old declaration should match the exportingness of the new
2557 : declaration. But hidden friend declarations just keep the
2558 : exportingness of the old declaration; see CWG2588. */
2559 23601 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2560 23601 : if (DECL_LANG_SPECIFIC (not_tmpl)
2561 23556 : && DECL_MODULE_ATTACH_P (not_tmpl)
2562 : /* Typedefs are not entities and so are OK to be redeclared
2563 : as exported: see [module.interface]/p6. */
2564 23792 : && TREE_CODE (olddecl) != TYPE_DECL)
2565 : {
2566 179 : if (DECL_MODULE_EXPORT_P (newdecl)
2567 179 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2568 : {
2569 15 : auto_diagnostic_group d;
2570 15 : error ("conflicting exporting for declaration %qD", newdecl);
2571 15 : inform (olddecl_loc,
2572 : "previously declared here without exporting");
2573 15 : }
2574 : }
2575 23422 : else if (DECL_MODULE_EXPORT_P (newdecl))
2576 10176 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2577 : }
2578 : }
2579 :
2580 : /* We have committed to returning OLDDECL at this point. */
2581 :
2582 : /* If new decl is `static' and an `extern' was seen previously,
2583 : warn about it. */
2584 19095431 : warn_extern_redeclared_static (newdecl, olddecl);
2585 :
2586 : /* True to merge attributes between the declarations, false to
2587 : set OLDDECL's attributes to those of NEWDECL (for template
2588 : explicit specializations that specify their own attributes
2589 : independent of those specified for the primary template). */
2590 19095431 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2591 14644485 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2592 19639349 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2593 :
2594 19095431 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2595 : {
2596 14644485 : if (merge_attr)
2597 : {
2598 14100774 : {
2599 14100774 : auto_diagnostic_group d;
2600 14100774 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2601 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2602 : ? G_("previous definition of %qD here")
2603 : : G_("previous declaration of %qD here"), olddecl);
2604 14100774 : }
2605 :
2606 : /* [dcl.attr.noreturn]: The first declaration of a function shall
2607 : specify the noreturn attribute if any declaration of that function
2608 : specifies the noreturn attribute. */
2609 14100774 : tree a;
2610 14100774 : if (TREE_THIS_VOLATILE (newdecl)
2611 86653 : && !TREE_THIS_VOLATILE (olddecl)
2612 : /* This applies to [[noreturn]] only, not its GNU variants. */
2613 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2614 54 : && cxx11_attribute_p (a)
2615 14100795 : && get_attribute_namespace (a) == NULL_TREE)
2616 : {
2617 9 : auto_diagnostic_group d;
2618 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2619 : "but its first declaration was not", newdecl);
2620 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2621 9 : }
2622 : }
2623 :
2624 : /* Now that functions must hold information normally held
2625 : by field decls, there is extra work to do so that
2626 : declaration information does not get destroyed during
2627 : definition. */
2628 14644485 : if (DECL_VINDEX (olddecl))
2629 153237 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2630 14644485 : if (DECL_CONTEXT (olddecl))
2631 14644485 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2632 14644485 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2633 14644485 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2634 14644485 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2635 14644485 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2636 14644485 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2637 14644485 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2638 14644485 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2639 14644485 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2640 14644485 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2641 14644485 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2642 14644485 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2643 2657960 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2644 5315920 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2645 14644485 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2646 :
2647 14644485 : check_redecl_contract (newdecl, olddecl);
2648 :
2649 : /* Optionally warn about more than one declaration for the same
2650 : name, but don't warn about a function declaration followed by a
2651 : definition. */
2652 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2653 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2654 : /* Don't warn about extern decl followed by definition. */
2655 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2656 : /* Don't warn if at least one is/was hidden. */
2657 27 : && !(hiding || was_hidden)
2658 : /* Don't warn about declaration followed by specialization. */
2659 14644497 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2660 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2661 : {
2662 6 : auto_diagnostic_group d;
2663 6 : if (warning_at (newdecl_loc,
2664 6 : OPT_Wredundant_decls,
2665 : "redundant redeclaration of %qD in same scope",
2666 : newdecl))
2667 6 : inform (olddecl_loc,
2668 : "previous declaration of %qD", olddecl);
2669 6 : }
2670 :
2671 : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2672 : first declaration of the function or, for an explicit specialization
2673 : of a function template, the first declaration of that
2674 : specialization. */
2675 14644485 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2676 619719 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2677 : {
2678 14100774 : if (DECL_DELETED_FN (newdecl))
2679 : {
2680 21 : auto_diagnostic_group d;
2681 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2682 : "is not first declaration", newdecl))
2683 21 : inform (olddecl_loc,
2684 : "previous declaration of %qD", olddecl);
2685 21 : }
2686 14100774 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2687 14100774 : if (DECL_DELETED_FN (olddecl)
2688 6 : && DECL_INITIAL (olddecl)
2689 14100780 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2690 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2691 : }
2692 : }
2693 :
2694 : /* Deal with C++: must preserve virtual function table size. */
2695 19095431 : if (TREE_CODE (olddecl) == TYPE_DECL)
2696 : {
2697 46109 : tree newtype = TREE_TYPE (newdecl);
2698 46109 : tree oldtype = TREE_TYPE (olddecl);
2699 :
2700 46109 : if (newtype != error_mark_node && oldtype != error_mark_node
2701 92218 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2702 126240 : CLASSTYPE_FRIEND_CLASSES (newtype)
2703 42080 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2704 :
2705 46109 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2706 : }
2707 :
2708 : /* Copy all the DECL_... slots specified in the new decl except for
2709 : any that we copy here from the old type. */
2710 19095431 : if (merge_attr)
2711 18551720 : DECL_ATTRIBUTES (newdecl)
2712 37103440 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2713 : else
2714 543711 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2715 :
2716 : /* Transfer purviewness and importingness to the old decl. */
2717 19095431 : transfer_defining_module (olddecl, newdecl);
2718 :
2719 19095431 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2720 : {
2721 3923593 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2722 3923593 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2723 3923593 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2724 :
2725 : /* The new decl should not already have gathered any
2726 : specializations. */
2727 3923593 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2728 :
2729 : /* Make sure the contracts are equivalent. */
2730 3923593 : check_redecl_contract (newdecl, olddecl);
2731 :
2732 3923593 : DECL_ATTRIBUTES (old_result)
2733 3923593 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2734 :
2735 3923593 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2736 : {
2737 3923563 : if (DECL_SOURCE_LOCATION (newdecl)
2738 3923563 : != DECL_SOURCE_LOCATION (olddecl))
2739 : {
2740 : /* Per C++11 8.3.6/4, default arguments cannot be added in
2741 : later declarations of a function template. */
2742 3901834 : check_redeclaration_no_default_args (newdecl);
2743 : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2744 : argument expression, that declaration... shall be the only
2745 : declaration of the function or function template in the
2746 : translation unit." */
2747 3901834 : check_no_redeclaration_friend_default_args
2748 3901834 : (old_result, new_result);
2749 :
2750 3901834 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2751 3901834 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2752 3901834 : merge_default_template_args (new_parms, old_parms,
2753 : /*class_p=*/false);
2754 : }
2755 3923563 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2756 2859360 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2757 :
2758 3923563 : check_default_args (newdecl);
2759 :
2760 7847114 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2761 3923563 : && DECL_INITIAL (new_result))
2762 : {
2763 24 : if (DECL_INITIAL (old_result))
2764 12 : DECL_UNINLINABLE (old_result) = 1;
2765 : else
2766 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2767 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2768 72 : DECL_NOT_REALLY_EXTERN (old_result)
2769 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2770 48 : DECL_INTERFACE_KNOWN (old_result)
2771 24 : = DECL_INTERFACE_KNOWN (new_result);
2772 24 : DECL_DECLARED_INLINE_P (old_result)
2773 24 : = DECL_DECLARED_INLINE_P (new_result);
2774 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2775 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2776 : }
2777 : else
2778 : {
2779 3923539 : DECL_DECLARED_INLINE_P (old_result)
2780 3923539 : |= DECL_DECLARED_INLINE_P (new_result);
2781 3923539 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2782 3923539 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2783 3923539 : check_redeclaration_exception_specification (newdecl, olddecl);
2784 :
2785 3923539 : merge_attribute_bits (new_result, old_result);
2786 : }
2787 : }
2788 :
2789 : /* If the new declaration is a definition, update the file and
2790 : line information on the declaration, and also make
2791 : the old declaration the same definition. */
2792 3923593 : if (DECL_INITIAL (new_result) != NULL_TREE)
2793 : {
2794 5323174 : DECL_SOURCE_LOCATION (olddecl)
2795 5323174 : = DECL_SOURCE_LOCATION (old_result)
2796 2661587 : = DECL_SOURCE_LOCATION (newdecl);
2797 2661587 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2798 2661587 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2799 : {
2800 2661587 : update_contract_arguments (new_result, old_result);
2801 :
2802 2661587 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2803 9601731 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2804 6940144 : DECL_CONTEXT (p) = old_result;
2805 :
2806 5323174 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2807 18 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2808 : }
2809 : }
2810 :
2811 3923593 : return olddecl;
2812 : }
2813 :
2814 15171838 : if (types_match)
2815 : {
2816 15171637 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2817 14644284 : check_redeclaration_exception_specification (newdecl, olddecl);
2818 :
2819 : /* Automatically handles default parameters. */
2820 15171637 : tree oldtype = TREE_TYPE (olddecl);
2821 15171637 : tree newtype;
2822 :
2823 : /* For typedefs use the old type, as the new type's DECL_NAME points
2824 : at newdecl, which will be ggc_freed. */
2825 15171637 : if (TREE_CODE (newdecl) == TYPE_DECL)
2826 : {
2827 : /* But NEWTYPE might have an attribute, honor that. */
2828 46109 : tree tem = TREE_TYPE (newdecl);
2829 46109 : newtype = oldtype;
2830 :
2831 46109 : if (TYPE_USER_ALIGN (tem))
2832 : {
2833 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2834 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2835 21 : TYPE_USER_ALIGN (newtype) = true;
2836 : }
2837 :
2838 : /* And remove the new type from the variants list. */
2839 46109 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2840 : {
2841 24 : tree remove = TREE_TYPE (newdecl);
2842 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2843 : {
2844 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2845 : /* If remove is the main variant, no need to remove that
2846 : from the list. One of the DECL_ORIGINAL_TYPE
2847 : variants, e.g. created for aligned attribute, might still
2848 : refer to the newdecl TYPE_DECL though, so remove that one
2849 : in that case. */
2850 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2851 6 : if (orig != remove)
2852 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2853 0 : t = TYPE_MAIN_VARIANT (t))
2854 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2855 : {
2856 12 : TYPE_NEXT_VARIANT (t)
2857 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2858 6 : break;
2859 : }
2860 : }
2861 : else
2862 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2863 0 : t = TYPE_NEXT_VARIANT (t))
2864 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2865 : {
2866 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2867 15 : break;
2868 : }
2869 : }
2870 : }
2871 15125528 : else if (merge_attr)
2872 14581817 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2873 : else
2874 543711 : newtype = TREE_TYPE (newdecl);
2875 :
2876 15171637 : if (VAR_P (newdecl))
2877 : {
2878 481244 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2879 : /* For already initialized vars, TREE_READONLY could have been
2880 : cleared in cp_finish_decl, because the var needs runtime
2881 : initialization or destruction. Make sure not to set
2882 : TREE_READONLY on it again. */
2883 481244 : if (DECL_INITIALIZED_P (olddecl)
2884 1214 : && !DECL_EXTERNAL (olddecl)
2885 481330 : && !TREE_READONLY (olddecl))
2886 31 : TREE_READONLY (newdecl) = 0;
2887 481244 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2888 962488 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2889 481244 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2890 481244 : if (DECL_DEPENDENT_INIT_P (olddecl))
2891 101148 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2892 962488 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2893 481244 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2894 481244 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2895 481244 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2896 962488 : DECL_DECLARED_CONSTINIT_P (newdecl)
2897 481244 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2898 :
2899 : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2900 481244 : if (DECL_LANG_SPECIFIC (olddecl)
2901 958907 : && CP_DECL_THREADPRIVATE_P (olddecl))
2902 : {
2903 : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2904 41 : retrofit_lang_decl (newdecl);
2905 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2906 : }
2907 : }
2908 :
2909 : /* An explicit specialization of a function template or of a member
2910 : function of a class template can be declared transaction_safe
2911 : independently of whether the corresponding template entity is declared
2912 : transaction_safe. */
2913 1204 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2914 1069 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2915 78 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2916 54 : && tx_safe_fn_type_p (newtype)
2917 15171637 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2918 0 : newtype = tx_unsafe_fn_variant (newtype);
2919 :
2920 15171637 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2921 :
2922 15171637 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2923 14644284 : check_default_args (newdecl);
2924 :
2925 : /* Lay the type out, unless already done. */
2926 15171637 : if (! same_type_p (newtype, oldtype)
2927 366 : && TREE_TYPE (newdecl) != error_mark_node
2928 15172003 : && !(processing_template_decl && uses_template_parms (newdecl)))
2929 285 : layout_type (TREE_TYPE (newdecl));
2930 :
2931 15171637 : if ((VAR_P (newdecl)
2932 14690393 : || TREE_CODE (newdecl) == PARM_DECL
2933 14690393 : || TREE_CODE (newdecl) == RESULT_DECL
2934 14690393 : || TREE_CODE (newdecl) == FIELD_DECL
2935 14690393 : || TREE_CODE (newdecl) == TYPE_DECL)
2936 15217746 : && !(processing_template_decl && uses_template_parms (newdecl)))
2937 371489 : layout_decl (newdecl, 0);
2938 :
2939 : /* Merge deprecatedness. */
2940 15171637 : if (TREE_DEPRECATED (newdecl))
2941 11214 : TREE_DEPRECATED (olddecl) = 1;
2942 :
2943 : /* Merge unavailability. */
2944 15171637 : if (TREE_UNAVAILABLE (newdecl))
2945 3 : TREE_UNAVAILABLE (olddecl) = 1;
2946 :
2947 : /* Preserve function specific target and optimization options */
2948 15171637 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2949 : {
2950 14644284 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2951 14644284 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2952 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2953 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2954 :
2955 14644284 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2956 14644284 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2957 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2958 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2959 :
2960 14644284 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2961 14134539 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2962 : }
2963 : else
2964 : {
2965 : /* Merge the const type qualifier. */
2966 527353 : if (TREE_READONLY (newdecl))
2967 140695 : TREE_READONLY (olddecl) = 1;
2968 : /* Merge the volatile type qualifier. */
2969 527353 : if (TREE_THIS_VOLATILE (newdecl))
2970 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2971 : }
2972 :
2973 : /* Merge the initialization information. */
2974 15171637 : if (DECL_INITIAL (newdecl) == NULL_TREE
2975 15171637 : && DECL_INITIAL (olddecl) != NULL_TREE)
2976 : {
2977 169429 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2978 169429 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2979 169429 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2980 : {
2981 10955 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2982 10955 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2983 : }
2984 : }
2985 :
2986 15171637 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2987 : {
2988 14644284 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2989 14644284 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2990 14644284 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2991 14644284 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2992 52629 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2993 14644284 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2994 14644284 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2995 14644284 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2996 14644284 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2997 :
2998 14644284 : if (merge_attr)
2999 14100573 : merge_attribute_bits (newdecl, olddecl);
3000 : else
3001 : {
3002 : /* Merge the noreturn bit. */
3003 543711 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3004 543711 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3005 543711 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3006 543711 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
3007 543711 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
3008 : }
3009 : /* Keep the old RTL. */
3010 14644284 : COPY_DECL_RTL (olddecl, newdecl);
3011 : }
3012 527353 : else if (VAR_P (newdecl)
3013 527353 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3014 : {
3015 : /* Keep the old RTL. We cannot keep the old RTL if the old
3016 : declaration was for an incomplete object and the new
3017 : declaration is not since many attributes of the RTL will
3018 : change. */
3019 481170 : COPY_DECL_RTL (olddecl, newdecl);
3020 : }
3021 : }
3022 : /* If cannot merge, then use the new type and qualifiers,
3023 : and don't preserve the old rtl. */
3024 : else
3025 : {
3026 : /* Clean out any memory we had of the old declaration. */
3027 201 : tree oldstatic = value_member (olddecl, static_aggregates);
3028 201 : if (oldstatic)
3029 0 : TREE_VALUE (oldstatic) = error_mark_node;
3030 :
3031 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3032 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3033 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3034 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3035 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3036 : }
3037 :
3038 : /* Merge the storage class information. */
3039 15171838 : merge_weak (newdecl, olddecl);
3040 :
3041 15171838 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3042 15171838 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3043 15171838 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3044 15171838 : if (! DECL_EXTERNAL (olddecl))
3045 50748 : DECL_EXTERNAL (newdecl) = 0;
3046 15171838 : if (! DECL_COMDAT (olddecl))
3047 6473817 : DECL_COMDAT (newdecl) = 0;
3048 :
3049 15171838 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
3050 : {
3051 78 : if (!DECL_LOCAL_DECL_P (olddecl))
3052 : /* This can happen if olddecl was brought in from the
3053 : enclosing namespace via a using-decl. The new decl is
3054 : then not a block-scope extern at all. */
3055 3 : DECL_LOCAL_DECL_P (newdecl) = false;
3056 : else
3057 : {
3058 75 : retrofit_lang_decl (newdecl);
3059 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
3060 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
3061 75 : if (alias != error_mark_node)
3062 : {
3063 72 : DECL_ATTRIBUTES (alias)
3064 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
3065 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3066 42 : merge_attribute_bits (newdecl, alias);
3067 : }
3068 : }
3069 : }
3070 :
3071 15171838 : new_template_info = NULL_TREE;
3072 15171838 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3073 : {
3074 15122166 : bool new_redefines_gnu_inline = false;
3075 :
3076 15122166 : if (new_defines_function
3077 15122166 : && ((DECL_INTERFACE_KNOWN (olddecl)
3078 1245 : && TREE_CODE (olddecl) == FUNCTION_DECL)
3079 8898747 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
3080 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3081 : == FUNCTION_DECL))))
3082 2490 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
3083 :
3084 : if (!new_redefines_gnu_inline)
3085 : {
3086 15122088 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3087 15122088 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3088 15122088 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3089 : }
3090 :
3091 15122166 : if (TREE_CODE (newdecl) != TYPE_DECL)
3092 : {
3093 15122102 : DECL_TEMPLATE_INSTANTIATED (newdecl)
3094 15122102 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3095 15122102 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
3096 :
3097 : /* If the OLDDECL is an instantiation and/or specialization,
3098 : then the NEWDECL must be too. But, it may not yet be marked
3099 : as such if the caller has created NEWDECL, but has not yet
3100 : figured out that it is a redeclaration. */
3101 15122102 : if (!DECL_USE_TEMPLATE (newdecl))
3102 14502167 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
3103 :
3104 15122102 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
3105 14578165 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
3106 29156330 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3107 : }
3108 :
3109 : /* Don't really know how much of the language-specific
3110 : values we should copy from old to new. */
3111 15122166 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3112 :
3113 15122166 : if (LANG_DECL_HAS_MIN (newdecl))
3114 : {
3115 15122166 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3116 15122166 : if (DECL_TEMPLATE_INFO (newdecl))
3117 : {
3118 619968 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
3119 619968 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3120 619968 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3121 : /* Remember the presence of explicit specialization args. */
3122 1087428 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3123 1087428 : = TINFO_USED_TEMPLATE_ID (new_template_info);
3124 : }
3125 :
3126 : /* We don't want to copy template info from a non-templated friend
3127 : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3128 15122166 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3129 : || !non_templated_friend_p (olddecl));
3130 15122166 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3131 : }
3132 :
3133 15122166 : if (DECL_DECLARES_FUNCTION_P (newdecl))
3134 : {
3135 : /* Only functions have these fields. */
3136 14644485 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3137 14644485 : DECL_BEFRIENDING_CLASSES (newdecl)
3138 14644485 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3139 14644485 : DECL_BEFRIENDING_CLASSES (olddecl));
3140 : /* DECL_THUNKS is only valid for virtual functions,
3141 : otherwise it is a DECL_FRIEND_CONTEXT. */
3142 14644485 : if (DECL_VIRTUAL_P (newdecl))
3143 609691 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3144 28069588 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3145 367251 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3146 : }
3147 477681 : else if (VAR_P (newdecl))
3148 : {
3149 : /* Only variables have this field. */
3150 477617 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3151 194 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3152 : }
3153 : }
3154 :
3155 15171838 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3156 : {
3157 14644485 : merge_decl_arguments (newdecl, olddecl, new_defines_function,
3158 : types_match, false);
3159 :
3160 14644485 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3161 14644485 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3162 : {
3163 : /* If newdecl is not a specialization, then it is not a
3164 : template-related function at all. And that means that we
3165 : should have exited above, returning 0. */
3166 543711 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3167 :
3168 543711 : if (DECL_ODR_USED (olddecl))
3169 : /* From [temp.expl.spec]:
3170 :
3171 : If a template, a member template or the member of a class
3172 : template is explicitly specialized then that
3173 : specialization shall be declared before the first use of
3174 : that specialization that would cause an implicit
3175 : instantiation to take place, in every translation unit in
3176 : which such a use occurs. */
3177 0 : error ("explicit specialization of %qD after first use",
3178 : olddecl);
3179 :
3180 543711 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3181 543711 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3182 1087269 : && DECL_NONGNU_INLINE_P (newdecl));
3183 :
3184 : /* Don't propagate visibility from the template to the
3185 : specialization here. We'll do that in determine_visibility if
3186 : appropriate. */
3187 543711 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3188 :
3189 : /* [temp.expl.spec/14] We don't inline explicit specialization
3190 : just because the primary template says so. */
3191 543711 : gcc_assert (!merge_attr);
3192 :
3193 543711 : DECL_DECLARED_INLINE_P (olddecl)
3194 543711 : = DECL_DECLARED_INLINE_P (newdecl);
3195 :
3196 543711 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
3197 543711 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3198 :
3199 543711 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3200 : }
3201 14100774 : else if (new_defines_function && DECL_INITIAL (olddecl))
3202 : {
3203 : /* Never inline re-defined extern inline functions.
3204 : FIXME: this could be better handled by keeping both
3205 : function as separate declarations. */
3206 78 : DECL_UNINLINABLE (newdecl) = 1;
3207 : }
3208 : else
3209 : {
3210 14100696 : if (DECL_PENDING_INLINE_P (olddecl))
3211 : {
3212 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
3213 6 : DECL_PENDING_INLINE_INFO (newdecl)
3214 12 : = DECL_PENDING_INLINE_INFO (olddecl);
3215 : }
3216 14100690 : else if (DECL_PENDING_INLINE_P (newdecl))
3217 : ;
3218 14100690 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3219 14100690 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3220 28201380 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3221 :
3222 14100696 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3223 :
3224 42302088 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3225 28201392 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3226 :
3227 28201392 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3228 14100696 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3229 14100696 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3230 27810076 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3231 : }
3232 :
3233 : /* Preserve abstractness on cloned [cd]tors. */
3234 14644485 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3235 :
3236 : /* Update newdecl's parms to point at olddecl. */
3237 46538013 : for (tree parm = DECL_ARGUMENTS (newdecl); parm;
3238 31893528 : parm = DECL_CHAIN (parm))
3239 31893528 : DECL_CONTEXT (parm) = olddecl;
3240 :
3241 14644485 : if (! types_match)
3242 : {
3243 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3244 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3245 201 : COPY_DECL_RTL (newdecl, olddecl);
3246 : }
3247 14644485 : if (! types_match || new_defines_function)
3248 : {
3249 : /* Update the contracts to reflect the new parameter names. */
3250 8900193 : update_contract_arguments (newdecl, olddecl);
3251 :
3252 : /* Mark the old PARM_DECLs in case std::meta::parameters_of has
3253 : been called on the old declaration and reflections of those
3254 : arguments are held across this point and used later.
3255 : Such PARM_DECLs are no longer present in
3256 : DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain. */
3257 8900193 : for (tree oldarg = DECL_ARGUMENTS (olddecl);
3258 31182388 : oldarg; oldarg = DECL_CHAIN (oldarg))
3259 22282195 : OLD_PARM_DECL_P (oldarg) = 1;
3260 :
3261 : /* These need to be copied so that the names are available.
3262 : Note that if the types do match, we'll preserve inline
3263 : info and other bits, but if not, we won't. */
3264 8900193 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3265 8900193 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3266 : }
3267 : /* If redeclaring a builtin function, it stays built in
3268 : if newdecl is a gnu_inline definition, or if newdecl is just
3269 : a declaration. */
3270 14644485 : if (fndecl_built_in_p (olddecl)
3271 14699364 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3272 : {
3273 3861376 : copy_decl_built_in_function (newdecl, olddecl);
3274 : /* If we're keeping the built-in definition, keep the rtl,
3275 : regardless of declaration matches. */
3276 3861376 : COPY_DECL_RTL (olddecl, newdecl);
3277 3861376 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3278 : {
3279 3861370 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3280 3861370 : if (builtin_decl_explicit_p (fncode))
3281 : {
3282 : /* A compatible prototype of these builtin functions
3283 : is seen, assume the runtime implements it with
3284 : the expected semantics. */
3285 3861370 : switch (fncode)
3286 : {
3287 9726 : case BUILT_IN_STPCPY:
3288 9726 : set_builtin_decl_implicit_p (fncode, true);
3289 9726 : break;
3290 3851644 : default:
3291 3851644 : set_builtin_decl_declared_p (fncode, true);
3292 3851644 : break;
3293 : }
3294 : }
3295 :
3296 3861370 : copy_attributes_to_builtin (newdecl);
3297 : }
3298 : }
3299 14644485 : if (new_defines_function)
3300 : /* If defining a function declared with other language
3301 : linkage, use the previously declared language linkage. */
3302 8899992 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3303 5744493 : else if (types_match)
3304 : {
3305 5744292 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3306 : /* Don't clear out the arguments if we're just redeclaring a
3307 : function. */
3308 5744292 : if (DECL_ARGUMENTS (olddecl))
3309 1852673 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3310 : }
3311 : }
3312 527353 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3313 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3314 :
3315 : /* Now preserve various other info from the definition. */
3316 15171838 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3317 15171838 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3318 15171838 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3319 15171838 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3320 :
3321 : /* Warn about conflicting visibility specifications. */
3322 15171838 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3323 5339281 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3324 15183213 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3325 : {
3326 12 : auto_diagnostic_group d;
3327 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3328 : "%qD: visibility attribute ignored because it "
3329 : "conflicts with previous declaration", newdecl))
3330 12 : inform (olddecl_loc,
3331 : "previous declaration of %qD", olddecl);
3332 12 : }
3333 : /* Choose the declaration which specified visibility. */
3334 15171838 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3335 : {
3336 5339281 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3337 5339281 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3338 : }
3339 : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3340 : so keep this behavior. */
3341 15171838 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3342 : {
3343 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3344 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3345 : }
3346 : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3347 15171838 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3348 : {
3349 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3350 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3351 : }
3352 15171784 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3353 15171784 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3354 3 : DECL_USER_ALIGN (newdecl) = 1;
3355 :
3356 15171838 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3357 30343676 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3358 15171838 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3359 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3360 : DECL_WARN_IF_NOT_ALIGN (olddecl));
3361 15171838 : if (TREE_CODE (newdecl) == FIELD_DECL)
3362 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3363 :
3364 : /* Merge module entity mapping information. */
3365 15171838 : if (DECL_LANG_SPECIFIC (olddecl)
3366 30294050 : && (DECL_MODULE_ENTITY_P (olddecl)
3367 15121880 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3368 : {
3369 332 : retrofit_lang_decl (newdecl);
3370 332 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3371 332 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3372 : }
3373 :
3374 : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3375 : with that from NEWDECL below. */
3376 15171838 : if (DECL_LANG_SPECIFIC (olddecl))
3377 : {
3378 15122212 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3379 : != DECL_LANG_SPECIFIC (newdecl));
3380 15122212 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3381 : }
3382 :
3383 : /* Merge the USED information. */
3384 15171838 : if (TREE_USED (olddecl))
3385 476129 : TREE_USED (newdecl) = 1;
3386 14695709 : else if (TREE_USED (newdecl))
3387 54 : TREE_USED (olddecl) = 1;
3388 :
3389 15171838 : if (VAR_P (newdecl))
3390 : {
3391 481244 : if (DECL_READ_P (olddecl))
3392 41148 : DECL_READ_P (newdecl) = 1;
3393 440096 : else if (DECL_READ_P (newdecl))
3394 3 : DECL_READ_P (olddecl) = 1;
3395 : }
3396 :
3397 15171838 : if (DECL_PRESERVE_P (olddecl))
3398 48 : DECL_PRESERVE_P (newdecl) = 1;
3399 15171790 : else if (DECL_PRESERVE_P (newdecl))
3400 63 : DECL_PRESERVE_P (olddecl) = 1;
3401 :
3402 : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3403 : to olddecl and deleted. */
3404 15171838 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3405 29816323 : && DECL_FUNCTION_VERSIONED (olddecl))
3406 : {
3407 : /* Set the flag for newdecl so that it gets copied to olddecl. */
3408 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3409 : /* newdecl will be purged after copying to olddecl and is no longer
3410 : a version. */
3411 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3412 : }
3413 :
3414 15171838 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3415 : {
3416 14644485 : int function_size;
3417 14644485 : struct symtab_node *snode = symtab_node::get (olddecl);
3418 :
3419 14644485 : function_size = sizeof (struct tree_decl_common);
3420 :
3421 14644485 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3422 : (char *) newdecl + sizeof (struct tree_common),
3423 : function_size - sizeof (struct tree_common));
3424 :
3425 14644485 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3426 : (char *) newdecl + sizeof (struct tree_decl_common),
3427 : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3428 :
3429 : /* Preserve symtab node mapping. */
3430 14644485 : olddecl->decl_with_vis.symtab_node = snode;
3431 :
3432 14644485 : if (new_template_info)
3433 : /* If newdecl is a template instantiation, it is possible that
3434 : the following sequence of events has occurred:
3435 :
3436 : o A friend function was declared in a class template. The
3437 : class template was instantiated.
3438 :
3439 : o The instantiation of the friend declaration was
3440 : recorded on the instantiation list, and is newdecl.
3441 :
3442 : o Later, however, instantiate_class_template called pushdecl
3443 : on the newdecl to perform name injection. But, pushdecl in
3444 : turn called duplicate_decls when it discovered that another
3445 : declaration of a global function with the same name already
3446 : existed.
3447 :
3448 : o Here, in duplicate_decls, we decided to clobber newdecl.
3449 :
3450 : If we're going to do that, we'd better make sure that
3451 : olddecl, and not newdecl, is on the list of
3452 : instantiations so that if we try to do the instantiation
3453 : again we won't get the clobbered declaration. */
3454 619959 : reregister_specialization (newdecl,
3455 : new_template_info,
3456 : olddecl);
3457 : }
3458 : else
3459 : {
3460 527353 : size_t size = tree_code_size (TREE_CODE (newdecl));
3461 :
3462 527353 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3463 : (char *) newdecl + sizeof (struct tree_common),
3464 : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3465 :
3466 527353 : switch (TREE_CODE (newdecl))
3467 : {
3468 527353 : case LABEL_DECL:
3469 527353 : case VAR_DECL:
3470 527353 : case RESULT_DECL:
3471 527353 : case PARM_DECL:
3472 527353 : case FIELD_DECL:
3473 527353 : case TYPE_DECL:
3474 527353 : case CONST_DECL:
3475 527353 : {
3476 527353 : struct symtab_node *snode = NULL;
3477 :
3478 527353 : if (VAR_P (newdecl)
3479 527353 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3480 3 : || DECL_EXTERNAL (olddecl)))
3481 481244 : snode = symtab_node::get (olddecl);
3482 527353 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3483 : (char *) newdecl + sizeof (struct tree_decl_common),
3484 : size - sizeof (struct tree_decl_common)
3485 527353 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3486 527353 : if (VAR_P (newdecl))
3487 481244 : olddecl->decl_with_vis.symtab_node = snode;
3488 : }
3489 : break;
3490 0 : default:
3491 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3492 : (char *) newdecl + sizeof (struct tree_decl_common),
3493 : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3494 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3495 0 : break;
3496 : }
3497 : }
3498 :
3499 15171838 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3500 : {
3501 15125729 : if (DECL_EXTERNAL (olddecl)
3502 482614 : || TREE_PUBLIC (olddecl)
3503 15125798 : || TREE_STATIC (olddecl))
3504 : {
3505 : /* Merge the section attribute.
3506 : We want to issue an error if the sections conflict but that must be
3507 : done later in decl_attributes since we are called before attributes
3508 : are assigned. */
3509 15125729 : if (DECL_SECTION_NAME (newdecl) != NULL)
3510 7 : set_decl_section_name (olddecl, newdecl);
3511 :
3512 15125729 : if (DECL_ONE_ONLY (newdecl))
3513 : {
3514 0 : struct symtab_node *oldsym, *newsym;
3515 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3516 0 : oldsym = cgraph_node::get_create (olddecl);
3517 : else
3518 0 : oldsym = varpool_node::get_create (olddecl);
3519 0 : newsym = symtab_node::get (newdecl);
3520 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3521 : }
3522 : }
3523 :
3524 15125729 : if (VAR_P (newdecl)
3525 15125729 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3526 : {
3527 404 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3528 404 : if (!processing_template_decl)
3529 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3530 : }
3531 : }
3532 :
3533 15171838 : DECL_UID (olddecl) = olddecl_uid;
3534 :
3535 : /* NEWDECL contains the merged attribute lists.
3536 : Update OLDDECL to be the same. */
3537 15171838 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3538 :
3539 : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3540 : so that encode_section_info has a chance to look at the new decl
3541 : flags and attributes. */
3542 15171838 : if (DECL_RTL_SET_P (olddecl)
3543 15171838 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3544 0 : || (VAR_P (olddecl)
3545 0 : && TREE_STATIC (olddecl))))
3546 0 : make_decl_rtl (olddecl);
3547 :
3548 : /* The NEWDECL will no longer be needed. Because every out-of-class
3549 : declaration of a member results in a call to duplicate_decls,
3550 : freeing these nodes represents in a significant savings.
3551 :
3552 : Before releasing the node, be sore to remove function from symbol
3553 : table that might have been inserted there to record comdat group.
3554 : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3555 : structure is shared in between newdecl and oldecl. */
3556 15171838 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3557 14644485 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3558 15171838 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3559 : {
3560 15125729 : struct symtab_node *snode = symtab_node::get (newdecl);
3561 15125729 : if (snode)
3562 399 : snode->remove ();
3563 : }
3564 :
3565 15171838 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3566 : {
3567 14644485 : tree clone;
3568 15163905 : FOR_EACH_CLONE (clone, olddecl)
3569 : {
3570 519420 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3571 519420 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3572 : }
3573 : }
3574 :
3575 : /* Remove the associated constraints for newdecl, if any, before
3576 : reclaiming memory. */
3577 15171838 : if (flag_concepts)
3578 14911650 : remove_constraints (newdecl);
3579 :
3580 15171838 : if (flag_contracts)
3581 : /* Remove the specifiers, and then remove the decl from the lookup. */
3582 937603 : remove_decl_with_fn_contracts_specifiers (newdecl);
3583 :
3584 : /* And similarly for any module tracking data. */
3585 15171838 : if (modules_p ())
3586 32389 : remove_defining_module (newdecl);
3587 :
3588 15171838 : ggc_free (newdecl);
3589 :
3590 15171838 : return olddecl;
3591 : }
3592 :
3593 : /* Return zero if the declaration NEWDECL is valid
3594 : when the declaration OLDDECL (assumed to be for the same name)
3595 : has already been seen.
3596 : Otherwise return an error message format string with a %s
3597 : where the identifier should go. */
3598 :
3599 : static const char *
3600 15351966 : redeclaration_error_message (tree newdecl, tree olddecl)
3601 : {
3602 15352002 : if (TREE_CODE (newdecl) == TYPE_DECL)
3603 : {
3604 : /* Because C++ can put things into name space for free,
3605 : constructs like "typedef struct foo { ... } foo"
3606 : would look like an erroneous redeclaration. */
3607 77108 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3608 : return NULL;
3609 : else
3610 : return G_("redefinition of %q#D");
3611 : }
3612 15274894 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3613 : {
3614 : /* If this is a pure function, its olddecl will actually be
3615 : the original initialization to `0' (which we force to call
3616 : abort()). Don't complain about redefinition in this case. */
3617 21739304 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3618 10869670 : && DECL_INITIAL (olddecl) == NULL_TREE)
3619 : return NULL;
3620 :
3621 : /* If both functions come from different namespaces, this is not
3622 : a redeclaration - this is a conflict with a used function. */
3623 21739274 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3624 2540995 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3625 10869640 : && ! decls_match (olddecl, newdecl))
3626 : return G_("%qD conflicts with used function");
3627 :
3628 : /* We'll complain about linkage mismatches in
3629 : warn_extern_redeclared_static. */
3630 :
3631 : /* Defining the same name twice is no good. */
3632 10869637 : if (decl_defined_p (olddecl)
3633 10869637 : && decl_defined_p (newdecl))
3634 : {
3635 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3636 : return G_("%q#D not declared in class");
3637 444 : else if (!GNU_INLINE_P (olddecl)
3638 390 : || GNU_INLINE_P (newdecl))
3639 219 : return G_("redefinition of %q#D");
3640 : }
3641 :
3642 12542812 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3643 : {
3644 1629735 : bool olda = GNU_INLINE_P (olddecl);
3645 1629735 : bool newa = GNU_INLINE_P (newdecl);
3646 :
3647 1629735 : if (olda != newa)
3648 : {
3649 15 : if (newa)
3650 : return G_("%q+D redeclared inline with "
3651 : "%<gnu_inline%> attribute");
3652 : else
3653 : return G_("%q+D redeclared inline without "
3654 : "%<gnu_inline%> attribute");
3655 : }
3656 : }
3657 :
3658 : /* [class.compare.default]: A definition of a comparison operator as
3659 : defaulted that appears in a class shall be the first declaration of
3660 : that function. */
3661 10869403 : special_function_kind sfk = special_function_p (olddecl);
3662 10869403 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3663 : return G_("comparison operator %q+D defaulted after "
3664 : "its first declaration");
3665 :
3666 10869400 : check_abi_tag_redeclaration
3667 10869400 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3668 10869400 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3669 :
3670 10869400 : return NULL;
3671 : }
3672 4405242 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3673 : {
3674 3923632 : tree nt, ot;
3675 :
3676 3923632 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3677 : return G_("redefinition of %q#D");
3678 :
3679 3923626 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3680 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3681 36 : DECL_TEMPLATE_RESULT (olddecl));
3682 :
3683 3923590 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3684 : return NULL;
3685 :
3686 3923578 : nt = DECL_TEMPLATE_RESULT (newdecl);
3687 3923578 : if (DECL_TEMPLATE_INFO (nt))
3688 946940 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3689 3923578 : ot = DECL_TEMPLATE_RESULT (olddecl);
3690 3923578 : if (DECL_TEMPLATE_INFO (ot))
3691 3923578 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3692 6585219 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3693 3923617 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3694 15 : return G_("redefinition of %q#D");
3695 :
3696 6345534 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3697 : {
3698 1720407 : bool olda = GNU_INLINE_P (ot);
3699 1720407 : bool newa = GNU_INLINE_P (nt);
3700 :
3701 1720407 : if (olda != newa)
3702 : {
3703 0 : if (newa)
3704 : return G_("%q+D redeclared inline with "
3705 : "%<gnu_inline%> attribute");
3706 : else
3707 : return G_("%q+D redeclared inline without "
3708 : "%<gnu_inline%> attribute");
3709 : }
3710 : }
3711 :
3712 : /* Core issue #226 (C++11):
3713 :
3714 : If a friend function template declaration specifies a
3715 : default template-argument, that declaration shall be a
3716 : definition and shall be the only declaration of the
3717 : function template in the translation unit. */
3718 3923563 : if ((cxx_dialect != cxx98)
3719 3907606 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3720 4124158 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3721 : /*is_primary=*/true,
3722 : /*is_partial=*/false,
3723 : /*is_friend_decl=*/2))
3724 : return G_("redeclaration of friend %q#D "
3725 : "may not have default template arguments");
3726 :
3727 3923560 : return NULL;
3728 : }
3729 481610 : else if (VAR_P (newdecl)
3730 481526 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3731 481526 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3732 481657 : && (! DECL_LANG_SPECIFIC (olddecl)
3733 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3734 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3735 : {
3736 : /* Only variables can be thread-local, and all declarations must
3737 : agree on this property. */
3738 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3739 : return G_("thread-local declaration of %q#D follows "
3740 : "non-thread-local declaration");
3741 : else
3742 3 : return G_("non-thread-local declaration of %q#D follows "
3743 : "thread-local declaration");
3744 : }
3745 481604 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3746 : {
3747 : /* The objects have been declared at namespace scope. If either
3748 : is a member of an anonymous union, then this is an invalid
3749 : redeclaration. For example:
3750 :
3751 : int i;
3752 : union { int i; };
3753 :
3754 : is invalid. */
3755 481371 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3756 962811 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3757 : return G_("redeclaration of %q#D");
3758 : /* If at least one declaration is a reference, there is no
3759 : conflict. For example:
3760 :
3761 : int i = 3;
3762 : extern int i;
3763 :
3764 : is valid. */
3765 481437 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3766 : return NULL;
3767 :
3768 : /* Static data member declared outside a class definition
3769 : if the variable is defined within the class with constexpr
3770 : specifier is declaration rather than definition (and
3771 : deprecated). */
3772 2824 : if (cxx_dialect >= cxx17
3773 2749 : && VAR_P (olddecl)
3774 2693 : && DECL_CLASS_SCOPE_P (olddecl)
3775 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3776 5461 : && !DECL_INITIAL (newdecl))
3777 : {
3778 2637 : DECL_EXTERNAL (newdecl) = 1;
3779 : /* For now, only warn with explicit -Wdeprecated. */
3780 2637 : if (OPTION_SET_P (warn_deprecated))
3781 : {
3782 40 : auto_diagnostic_group d;
3783 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3784 : "redundant redeclaration of %<constexpr%> "
3785 : "static data member %qD", newdecl))
3786 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3787 : "previous declaration of %qD", olddecl);
3788 40 : }
3789 2637 : return NULL;
3790 : }
3791 :
3792 : /* Reject two definitions. */
3793 : return G_("redefinition of %q#D");
3794 : }
3795 : else
3796 : {
3797 : /* Objects declared with block scope: */
3798 : /* Reject two definitions, and reject a definition
3799 : together with an external reference. */
3800 152 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3801 : return G_("redeclaration of %q#D");
3802 : return NULL;
3803 : }
3804 : }
3805 :
3806 :
3807 : /* Hash and equality functions for the named_label table. */
3808 :
3809 : hashval_t
3810 77707 : named_label_hash::hash (const value_type entry)
3811 : {
3812 77707 : return IDENTIFIER_HASH_VALUE (entry->name);
3813 : }
3814 :
3815 : bool
3816 74813 : named_label_hash::equal (const value_type entry, compare_type name)
3817 : {
3818 74813 : return name == entry->name;
3819 : }
3820 :
3821 : /* Look for a label named ID in the current function. If one cannot
3822 : be found, create one. Return the named_label_entry, or NULL on
3823 : failure. */
3824 :
3825 : static named_label_entry *
3826 23907 : lookup_label_1 (tree id, bool making_local_p)
3827 : {
3828 23907 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3829 :
3830 : /* You can't use labels at global scope. */
3831 23907 : if (current_function_decl == NULL_TREE)
3832 : {
3833 3 : error ("label %qE referenced outside of any function", id);
3834 3 : return NULL;
3835 : }
3836 :
3837 23904 : if (!named_labels)
3838 4614 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3839 :
3840 23904 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3841 23904 : named_label_entry **slot
3842 23904 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3843 23904 : named_label_entry *old = *slot;
3844 :
3845 23904 : if (old && old->label_decl)
3846 : {
3847 3458 : if (!making_local_p)
3848 : return old;
3849 :
3850 21 : if (old->binding_level == current_binding_level)
3851 : {
3852 0 : auto_diagnostic_group d;
3853 0 : error ("local label %qE conflicts with existing label", id);
3854 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3855 0 : return NULL;
3856 0 : }
3857 : }
3858 :
3859 : /* We are making a new decl, create or reuse the named_label_entry */
3860 20488 : named_label_entry *ent = NULL;
3861 33 : if (old && !old->label_decl)
3862 : ent = old;
3863 : else
3864 : {
3865 20455 : ent = ggc_cleared_alloc<named_label_entry> ();
3866 20455 : ent->name = id;
3867 20455 : ent->outer = old;
3868 20455 : *slot = ent;
3869 : }
3870 :
3871 : /* Now create the LABEL_DECL. */
3872 20467 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3873 :
3874 20467 : DECL_CONTEXT (decl) = current_function_decl;
3875 20467 : SET_DECL_MODE (decl, VOIDmode);
3876 20467 : if (making_local_p)
3877 : {
3878 219 : C_DECLARED_LABEL_FLAG (decl) = true;
3879 219 : DECL_CHAIN (decl) = current_binding_level->names;
3880 219 : current_binding_level->names = decl;
3881 : }
3882 :
3883 20467 : ent->label_decl = decl;
3884 :
3885 20467 : return ent;
3886 23907 : }
3887 :
3888 : /* Wrapper for lookup_label_1. */
3889 :
3890 : tree
3891 2952 : lookup_label (tree id)
3892 : {
3893 2952 : named_label_entry *ent = lookup_label_1 (id, false);
3894 2952 : return ent ? ent->label_decl : NULL_TREE;
3895 : }
3896 :
3897 : /* Remember that we've seen &&ID. */
3898 :
3899 : void
3900 253 : mark_label_addressed (tree id)
3901 : {
3902 253 : named_label_entry *ent = lookup_label_1 (id, false);
3903 253 : ent->addressed = true;
3904 253 : }
3905 :
3906 : tree
3907 219 : declare_local_label (tree id)
3908 : {
3909 219 : named_label_entry *ent = lookup_label_1 (id, true);
3910 219 : return ent ? ent->label_decl : NULL_TREE;
3911 : }
3912 :
3913 : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3914 :
3915 : static bool
3916 85071 : decl_jump_unsafe (tree decl)
3917 : {
3918 : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3919 : with automatic storage duration is not in scope to a point where it is
3920 : in scope is ill-formed unless the variable has scalar type, class type
3921 : with a trivial default constructor and a trivial destructor, a
3922 : cv-qualified version of one of these types, or an array of one of the
3923 : preceding types and is declared without an initializer (8.5). */
3924 85071 : tree type = TREE_TYPE (decl);
3925 :
3926 85071 : return (type != error_mark_node
3927 85053 : && VAR_P (decl)
3928 15025 : && !TREE_STATIC (decl)
3929 99960 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3930 12975 : || variably_modified_type_p (type, NULL_TREE)));
3931 : }
3932 :
3933 : /* Returns true if decl is an automatic variable with vacuous initialization
3934 : except when it is [[indeterminate]] or [[gnu::uninitialized]].
3935 : Jumps across such initialization need to be instrumented for
3936 : !!flag_auto_var_init. */
3937 :
3938 : static bool
3939 83148 : decl_instrument_init_bypass_p (tree decl)
3940 : {
3941 83148 : tree type = TREE_TYPE (decl);
3942 :
3943 83148 : return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
3944 5920 : && !processing_template_decl
3945 5674 : && type != error_mark_node
3946 5668 : && VAR_P (decl)
3947 1053 : && !TREE_STATIC (decl)
3948 1032 : && !DECL_EXTERNAL (decl)
3949 2036 : && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3950 1018 : || variably_modified_type_p (type, NULL_TREE))
3951 1018 : && !lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (decl))
3952 1018 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
3953 84166 : && !DECL_HAS_VALUE_EXPR_P (decl));
3954 : }
3955 :
3956 : /* Build .DEFERRED_INIT call for DECL. */
3957 :
3958 : static tree
3959 133 : build_deferred_init_call (tree decl)
3960 : {
3961 133 : tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
3962 266 : tree init_type_arg = build_int_cst (integer_type_node,
3963 133 : (int) flag_auto_var_init);
3964 133 : location_t loc = DECL_SOURCE_LOCATION (decl);
3965 133 : tree decl_name;
3966 :
3967 133 : if (DECL_NAME (decl))
3968 133 : decl_name = build_string_literal (DECL_NAME (decl));
3969 : else
3970 : {
3971 0 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
3972 0 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
3973 0 : decl_name = build_string_literal (decl_name_anonymous);
3974 : }
3975 :
3976 133 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
3977 133 : TREE_TYPE (decl), 3,
3978 : decl_size_arg, init_type_arg,
3979 : decl_name);
3980 133 : tree ret = build2_loc (loc, MODIFY_EXPR, void_type_node, decl, call);
3981 133 : return build_stmt (loc, EXPR_STMT, ret);
3982 : }
3983 :
3984 : /* Emit before ITER (and any labels/case labels before it) code like
3985 : if (0)
3986 : {
3987 : l1:
3988 : v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
3989 : v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
3990 : v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
3991 : v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
3992 : }
3993 : and return l1 label, or if it already exists, assert it has the
3994 : .DEFERRED_INIT calls for the right decls in the right order and
3995 : amend it, either by adding extra labels in between or further
3996 : ,DEFERRED_INIT calls before the first label and extra label before
3997 : that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
3998 : of adding a label. DECLS points to an array of NDECLS VAR_DECLs
3999 : which should be initialized. */
4000 :
4001 : static tree
4002 121 : maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
4003 : tree *decls, unsigned ndecls)
4004 : {
4005 121 : tree lab = NULL_TREE;
4006 185 : for (; !tsi_end_p (iter); tsi_prev (&iter))
4007 : {
4008 179 : switch (TREE_CODE (tsi_stmt (iter)))
4009 : {
4010 64 : case LABEL_EXPR:
4011 64 : case CASE_LABEL_EXPR:
4012 64 : case DEBUG_BEGIN_STMT:
4013 64 : continue;
4014 : default:
4015 : break;
4016 : }
4017 : break;
4018 : }
4019 121 : if (!tsi_end_p (iter)
4020 115 : && TREE_CODE (tsi_stmt (iter)) == IF_STMT
4021 197 : && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
4022 : {
4023 : /* Found IF_STMT added for this or some adjacent
4024 : LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
4025 : The decls are ordered so that we can always reuse it. Sometimes
4026 : by no modifications at all and just returning the right label
4027 : which was added already before, sometimes by adding a label in
4028 : between two previously added .DEFERRED_INIT calls and sometimes
4029 : by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
4030 : before that) before the statements in IF_STMT body. */
4031 70 : tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
4032 70 : iter = tsi_last (then_clause);
4033 70 : bool add = false;
4034 378 : for (unsigned int i = 0; i < ndecls; ++i)
4035 : {
4036 308 : tree decl = decls[i];
4037 308 : if (!add)
4038 : {
4039 : /* Skip over labels/case labels after .DEFERRED_INIT for the
4040 : DECL we are looking for. */
4041 396 : while (!tsi_end_p (iter)
4042 396 : && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
4043 284 : || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
4044 25 : && !case_label)))
4045 118 : tsi_prev (&iter);
4046 278 : if (tsi_end_p (iter))
4047 : {
4048 : /* Reached the start, we'll need to prepend further
4049 : statements. */
4050 19 : add = true;
4051 19 : iter = tsi_start (then_clause);
4052 : }
4053 : else
4054 : {
4055 : /* Found something, assert it is .DEFERRED_INIT for
4056 : DECL. */
4057 259 : tree t = tsi_stmt (iter);
4058 259 : gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
4059 259 : t = EXPR_STMT_EXPR (t);
4060 259 : gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
4061 : && TREE_OPERAND (t, 0) == decl
4062 : && (TREE_CODE (TREE_OPERAND (t, 1))
4063 : == CALL_EXPR));
4064 259 : t = TREE_OPERAND (t, 1);
4065 259 : gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
4066 : && (CALL_EXPR_IFN (t)
4067 : == IFN_DEFERRED_INIT));
4068 259 : tsi_prev (&iter);
4069 : }
4070 : }
4071 278 : if (add)
4072 : {
4073 : /* If reached the start in this or some earlier iteration,
4074 : prepend .DEFERRED_INIT call for DECL. */
4075 49 : tree t = build_deferred_init_call (decl);
4076 49 : STMT_IS_FULL_EXPR_P (t) = 1;
4077 49 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4078 : }
4079 : }
4080 70 : if (!add)
4081 : {
4082 : /* If .DEFERRED_INIT calls for all the decls were already there,
4083 : skip over case labels and if we find a LABEL_EXPR, return
4084 : its label. */
4085 63 : while (!tsi_end_p (iter)
4086 63 : && !case_label
4087 117 : && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4088 12 : tsi_prev (&iter);
4089 51 : if (tsi_end_p (iter))
4090 : {
4091 : /* Only case labels were found and we are looking for normal
4092 : label, we'll need to add it. */
4093 0 : add = true;
4094 0 : iter = tsi_start (then_clause);
4095 : }
4096 51 : else if (!case_label
4097 51 : && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4098 : /* Return existing label. */
4099 21 : lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4100 : else
4101 : {
4102 : /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4103 30 : gcc_checking_assert (case_label
4104 : || (TREE_CODE (tsi_stmt (iter))
4105 : == EXPR_STMT));
4106 30 : add = true;
4107 30 : tsi_next (&iter);
4108 30 : gcc_checking_assert (!tsi_end_p (iter));
4109 : }
4110 : }
4111 21 : if (add)
4112 : {
4113 49 : tree t;
4114 49 : if (case_label)
4115 : t = case_label;
4116 : else
4117 : {
4118 40 : lab = create_artificial_label (UNKNOWN_LOCATION);
4119 40 : t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4120 : }
4121 49 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4122 : }
4123 : }
4124 : else
4125 : {
4126 : /* No IF_STMT created by this function found. Create it all
4127 : from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4128 : followed by .DEFERRED_INIT calls inside of a new if (0). */
4129 51 : tree new_then = push_stmt_list ();
4130 51 : if (!case_label)
4131 : {
4132 22 : lab = create_artificial_label (UNKNOWN_LOCATION);
4133 22 : add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4134 : }
4135 : else
4136 29 : add_stmt (case_label);
4137 135 : for (unsigned int i = ndecls; i; --i)
4138 84 : add_stmt (build_deferred_init_call (decls[i - 1]));
4139 51 : new_then = pop_stmt_list (new_then);
4140 51 : tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4141 : new_then, void_node, NULL_TREE);
4142 51 : IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4143 51 : if (tsi_end_p (iter))
4144 : {
4145 6 : iter = tsi_start (iter.container);
4146 6 : tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4147 : }
4148 : else
4149 45 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4150 : }
4151 121 : return lab;
4152 : }
4153 :
4154 : /* Adjust backward gotos to named label ENT if they jump over vacuous
4155 : initializers if !!flag_auto_var_init. ITER is the location of
4156 : LABEL_EXPR for that named label. */
4157 :
4158 : static void
4159 19 : adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4160 : {
4161 19 : auto_vec<tree, 4> decls;
4162 19 : unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4163 19 : tree decl;
4164 88 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4165 88 : if (!decl_jump_unsafe (decl))
4166 : {
4167 88 : gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4168 88 : decls.safe_push (decl);
4169 176 : if (decls.length () == max_cnt)
4170 : break;
4171 : }
4172 19 : named_label_bck_direct_goto *dgoto;
4173 19 : unsigned last = 0;
4174 19 : tree lab = NULL_TREE;
4175 103 : FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4176 : {
4177 46 : if (dgoto->n_bad_decls != last)
4178 : {
4179 40 : last = dgoto->n_bad_decls;
4180 80 : lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4181 : decls.address (), last);
4182 : }
4183 46 : *dgoto->direct_goto = lab;
4184 : }
4185 19 : }
4186 :
4187 : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4188 : to the user. */
4189 :
4190 : static bool
4191 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
4192 : enum diagnostics::kind diag_kind, bool computed)
4193 : {
4194 286 : if (computed)
4195 43 : diag_kind = diagnostics::kind::warning;
4196 286 : bool complained
4197 373 : = emit_diagnostic (diag_kind, loc, 0,
4198 : decl ? G_("jump to label %qD")
4199 : : G_("jump to case label"), decl);
4200 286 : if (complained && locus)
4201 : {
4202 199 : if (computed)
4203 43 : inform (*locus, " as a possible target of computed goto");
4204 : else
4205 156 : inform (*locus, " from here");
4206 : }
4207 286 : return complained;
4208 : }
4209 :
4210 : /* Check that a single previously seen jump to a newly defined label
4211 : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4212 : the jump context; NAMES are the names in scope in LEVEL at the jump
4213 : context; LOCUS is the source position of the jump or 0. COMPUTED
4214 : is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4215 : vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4216 : initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4217 : CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4218 : Returns non-zero if all is well, 2 if any vacuous initializers were
4219 : crossed. */
4220 :
4221 : static int
4222 4703974 : check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4223 : bool exited_omp, const location_t *locus,
4224 : vec<tree, va_gc> *computed,
4225 : vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4226 : tree case_label)
4227 : {
4228 4703974 : auto_diagnostic_group d;
4229 4703974 : cp_binding_level *b;
4230 4703974 : bool complained = false;
4231 4703974 : int identified = 0;
4232 4703974 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4233 4703974 : bool saw_ceif = false, saw_se = false;
4234 4703974 : auto_vec<tree> vacuous_decls;
4235 4703974 : bool vacuous_inits = false;
4236 :
4237 4703974 : if (exited_omp)
4238 : {
4239 12 : complained = identify_goto (decl, input_location, locus,
4240 : diagnostics::kind::error,
4241 : computed);
4242 12 : if (complained)
4243 12 : inform (input_location, " exits OpenMP structured block");
4244 : saw_omp = true;
4245 : identified = 2;
4246 : }
4247 :
4248 14083227 : for (b = current_binding_level; b ; b = b->level_chain)
4249 : {
4250 9379253 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4251 :
4252 9459150 : for (new_decls = b->names; new_decls != old_decls;
4253 149118 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4254 69221 : : TREE_CHAIN (new_decls)))
4255 : {
4256 79897 : bool problem = decl_jump_unsafe (new_decls);
4257 79897 : if (! problem)
4258 : {
4259 79852 : if (decl_instrument_init_bypass_p (new_decls))
4260 : {
4261 311 : if (direct_goto || case_label)
4262 246 : vacuous_decls.safe_push (new_decls);
4263 : vacuous_inits = true;
4264 : }
4265 79852 : continue;
4266 : }
4267 :
4268 45 : if (!identified)
4269 : {
4270 45 : complained = identify_goto (decl, input_location, locus,
4271 : diagnostics::kind::error,
4272 : computed);
4273 45 : identified = 2;
4274 : }
4275 45 : if (complained)
4276 45 : inform (DECL_SOURCE_LOCATION (new_decls),
4277 : " crosses initialization of %q#D", new_decls);
4278 : }
4279 :
4280 9379253 : if (b == level)
4281 : break;
4282 :
4283 4675279 : const char *inf = NULL;
4284 4675279 : location_t loc = input_location;
4285 4675279 : switch (b->kind)
4286 : {
4287 3 : case sk_try:
4288 3 : if (!saw_eh)
4289 : inf = G_(" enters %<try%> block");
4290 : saw_eh = true;
4291 : break;
4292 :
4293 9 : case sk_catch:
4294 9 : if (!saw_eh)
4295 : inf = G_(" enters %<catch%> block");
4296 : saw_eh = true;
4297 : break;
4298 :
4299 102 : case sk_omp:
4300 102 : if (!saw_omp)
4301 : inf = G_(" enters OpenMP structured block");
4302 : saw_omp = true;
4303 : break;
4304 :
4305 3 : case sk_transaction:
4306 3 : if (!saw_tm)
4307 : inf = G_(" enters synchronized or atomic statement");
4308 : saw_tm = true;
4309 : break;
4310 :
4311 18 : case sk_stmt_expr:
4312 18 : if (!saw_se)
4313 : inf = G_(" enters statement expression");
4314 : saw_se = true;
4315 : break;
4316 :
4317 4675009 : case sk_block:
4318 4675009 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
4319 : {
4320 9 : inf = G_(" enters %<constexpr if%> statement");
4321 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4322 : saw_cxif = true;
4323 : }
4324 4675000 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
4325 : {
4326 18 : inf = G_(" enters %<consteval if%> statement");
4327 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4328 : saw_ceif = true;
4329 : }
4330 : break;
4331 :
4332 : default:
4333 : break;
4334 : }
4335 :
4336 : if (inf)
4337 : {
4338 141 : if (identified < 2)
4339 141 : complained = identify_goto (decl, input_location, locus,
4340 : diagnostics::kind::error,
4341 : computed);
4342 141 : identified = 2;
4343 141 : if (complained)
4344 141 : inform (loc, inf);
4345 : }
4346 : }
4347 :
4348 4703974 : if (!vec_safe_is_empty (computed))
4349 : {
4350 9 : if (!identified)
4351 6 : complained = identify_goto (decl, input_location, locus,
4352 : diagnostics::kind::error,
4353 : computed);
4354 9 : identified = 2;
4355 9 : if (complained)
4356 18 : for (tree d : computed)
4357 : {
4358 9 : if (DECL_P (d))
4359 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4360 3 : else if (d == get_identifier ("catch"))
4361 3 : inform (*locus, " does not clean up handled exception");
4362 : }
4363 : }
4364 :
4365 4704057 : if (!vacuous_decls.is_empty () && !seen_error ())
4366 : {
4367 81 : tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4368 81 : if (case_label)
4369 : {
4370 38 : gcc_checking_assert (tsi_stmt (iter) == case_label);
4371 38 : tsi_delink (&iter);
4372 38 : iter = tsi_last (cur_stmt_list);
4373 : }
4374 81 : tree lab = maybe_add_deferred_init_calls (iter, case_label,
4375 : vacuous_decls.address (),
4376 : vacuous_decls.length ());
4377 81 : if (lab)
4378 : {
4379 : unsigned int i;
4380 : named_label_fwd_direct_goto *dgoto;
4381 167 : FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4382 43 : *dgoto->direct_goto = lab;
4383 : }
4384 : }
4385 :
4386 4703974 : if (identified)
4387 : return 0;
4388 4703770 : return vacuous_inits ? 2 : 1;
4389 4703974 : }
4390 :
4391 : static void
4392 2106 : check_previous_goto (tree decl, struct named_label_use_entry *use)
4393 : {
4394 2106 : check_previous_goto_1 (decl, use->binding_level,
4395 2106 : use->names_in_scope, use->in_omp_scope,
4396 2106 : &use->o_goto_locus, use->computed_goto,
4397 : use->direct_goto, NULL_TREE);
4398 2106 : vec_free (use->direct_goto);
4399 2106 : }
4400 :
4401 : static int
4402 4701868 : check_switch_goto (cp_binding_level *level, tree case_label)
4403 : {
4404 4701868 : return check_previous_goto_1 (NULL_TREE, level, level->names,
4405 4701868 : false, NULL, nullptr, nullptr, case_label);
4406 : }
4407 :
4408 : /* Check that a new jump to a label ENT is OK. DECLP is a pointer
4409 : to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4410 :
4411 : void
4412 2830 : check_goto_1 (named_label_entry *ent, tree *declp)
4413 : {
4414 2830 : auto_diagnostic_group d;
4415 2830 : tree decl = ent->label_decl;
4416 2830 : bool computed = declp == NULL;
4417 :
4418 : /* If the label hasn't been defined yet, defer checking. */
4419 2830 : if (! DECL_INITIAL (decl))
4420 : {
4421 : /* Don't bother creating another use if the last goto had the
4422 : same data, and will therefore create the same set of errors. */
4423 2175 : if (ent->uses
4424 658 : && ent->uses->binding_level == current_binding_level
4425 60 : && ent->uses->names_in_scope == current_binding_level->names)
4426 : {
4427 60 : if (declp
4428 54 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4429 11 : && !processing_template_decl)
4430 9 : vec_safe_push (ent->uses->direct_goto,
4431 9 : named_label_fwd_direct_goto { declp });
4432 60 : return;
4433 : }
4434 :
4435 2115 : named_label_use_entry *new_use
4436 2115 : = ggc_alloc<named_label_use_entry> ();
4437 2115 : new_use->binding_level = current_binding_level;
4438 2115 : new_use->names_in_scope = current_binding_level->names;
4439 2115 : new_use->o_goto_locus = input_location;
4440 2115 : new_use->in_omp_scope = false;
4441 2115 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4442 2115 : new_use->direct_goto = nullptr;
4443 2115 : if (declp
4444 2024 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4445 355 : && !processing_template_decl)
4446 298 : vec_safe_push (new_use->direct_goto,
4447 298 : named_label_fwd_direct_goto { declp });
4448 :
4449 2115 : new_use->next = ent->uses;
4450 2115 : ent->uses = new_use;
4451 2115 : return;
4452 : }
4453 :
4454 655 : bool saw_catch = false, complained = false;
4455 655 : int identified = 0;
4456 655 : tree bad;
4457 655 : unsigned ix;
4458 655 : unsigned n_bad_decls = 0;
4459 :
4460 655 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4461 640 : || ent->in_constexpr_if || ent->in_consteval_if
4462 634 : || ent->in_omp_scope || ent->in_stmt_expr
4463 616 : || ent->has_bad_decls)
4464 : {
4465 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4466 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4467 30 : || ent->in_consteval_if || ent->in_transaction_scope
4468 24 : || ent->in_omp_scope || ent->in_stmt_expr)
4469 39 : diag_kind = diagnostics::kind::error;
4470 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4471 : &input_location, diag_kind, computed);
4472 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
4473 : }
4474 :
4475 845 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4476 : {
4477 190 : bool problem = decl_jump_unsafe (bad);
4478 190 : if (!problem)
4479 : {
4480 184 : gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4481 184 : n_bad_decls++;
4482 184 : continue;
4483 : }
4484 :
4485 6 : if (DECL_ARTIFICIAL (bad))
4486 : {
4487 : /* Can't skip init of __exception_info. */
4488 0 : if (identified == 1)
4489 : {
4490 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4491 : &input_location,
4492 : diagnostics::kind::error,
4493 : computed);
4494 0 : identified = 2;
4495 : }
4496 0 : if (complained)
4497 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4498 : saw_catch = true;
4499 : }
4500 6 : else if (complained)
4501 6 : inform (DECL_SOURCE_LOCATION (bad),
4502 : " skips initialization of %q#D", bad);
4503 : }
4504 :
4505 655 : if (complained)
4506 : {
4507 45 : if (ent->in_try_scope)
4508 9 : inform (input_location, " enters %<try%> block");
4509 36 : else if (ent->in_catch_scope && !saw_catch)
4510 3 : inform (input_location, " enters %<catch%> block");
4511 33 : else if (ent->in_transaction_scope)
4512 3 : inform (input_location, " enters synchronized or atomic statement");
4513 30 : else if (ent->in_constexpr_if)
4514 3 : inform (input_location, " enters %<constexpr if%> statement");
4515 27 : else if (ent->in_consteval_if)
4516 3 : inform (input_location, " enters %<consteval if%> statement");
4517 24 : else if (ent->in_stmt_expr)
4518 15 : inform (input_location, " enters statement expression");
4519 : }
4520 :
4521 655 : if (ent->in_omp_scope)
4522 : {
4523 3 : if (complained)
4524 3 : inform (input_location, " enters OpenMP structured block");
4525 : }
4526 652 : else if (flag_openmp)
4527 69 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4528 : {
4529 42 : if (b == ent->binding_level)
4530 : break;
4531 36 : if (b->kind == sk_omp)
4532 : {
4533 21 : if (identified < 2)
4534 : {
4535 42 : complained = identify_goto (decl,
4536 21 : DECL_SOURCE_LOCATION (decl),
4537 : &input_location,
4538 : diagnostics::kind::error,
4539 : computed);
4540 21 : identified = 2;
4541 : }
4542 21 : if (complained)
4543 21 : inform (input_location, " exits OpenMP structured block");
4544 : break;
4545 : }
4546 : }
4547 :
4548 : /* Warn if a computed goto might involve a local variable going out of scope
4549 : without being cleaned up. */
4550 655 : if (computed)
4551 : {
4552 71 : auto level = ent->binding_level;
4553 71 : auto names = ent->names_in_scope;
4554 71 : for (auto b = current_binding_level; ; b = b->level_chain)
4555 : {
4556 111 : if (b->kind == sk_catch)
4557 : {
4558 6 : if (!identified)
4559 : {
4560 6 : complained
4561 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4562 : &input_location, diagnostics::kind::error,
4563 : computed);
4564 6 : identified = 2;
4565 : }
4566 6 : if (complained)
4567 6 : inform (input_location,
4568 : " does not clean up handled exception");
4569 : }
4570 111 : tree end = b == level ? names : NULL_TREE;
4571 127 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4572 : {
4573 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4574 : {
4575 10 : if (!identified)
4576 : {
4577 10 : complained
4578 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4579 : &input_location, diagnostics::kind::error,
4580 : computed);
4581 10 : identified = 2;
4582 : }
4583 10 : if (complained)
4584 10 : inform (DECL_SOURCE_LOCATION (d),
4585 : " does not destroy %qD", d);
4586 : }
4587 : }
4588 111 : if (b == level)
4589 : break;
4590 40 : }
4591 : }
4592 :
4593 655 : if (n_bad_decls && declp)
4594 46 : vec_safe_push (ent->direct_goto,
4595 46 : named_label_bck_direct_goto { declp, n_bad_decls });
4596 2830 : }
4597 :
4598 : /* Check that a new jump to a label *DECLP is OK. Called by
4599 : finish_goto_stmt. */
4600 :
4601 : void
4602 2790 : check_goto (tree *declp)
4603 : {
4604 2790 : if (!named_labels)
4605 : return;
4606 2775 : tree decl = *declp;
4607 2775 : if (TREE_CODE (decl) != LABEL_DECL)
4608 : {
4609 : /* We don't know where a computed goto is jumping,
4610 : so check all addressable labels. */
4611 312 : for (auto iter = named_labels->begin ();
4612 425 : iter != named_labels->end ();
4613 199 : ++iter)
4614 : {
4615 199 : auto ent = *iter;
4616 199 : if (ent->addressed)
4617 168 : check_goto_1 (ent, NULL);
4618 : }
4619 : }
4620 : else
4621 : {
4622 2662 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4623 2662 : named_label_entry **slot
4624 2662 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4625 2662 : named_label_entry *ent = *slot;
4626 2662 : check_goto_1 (ent, declp);
4627 : }
4628 : }
4629 :
4630 : /* Check that a return is ok wrt OpenMP structured blocks.
4631 : Called by finish_return_stmt. Returns true if all is well. */
4632 :
4633 : bool
4634 278577 : check_omp_return (void)
4635 : {
4636 1104825 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4637 826248 : if (b->kind == sk_omp)
4638 : {
4639 15 : error ("invalid exit from OpenMP structured block");
4640 15 : return false;
4641 : }
4642 826233 : else if (b->kind == sk_function_parms)
4643 : break;
4644 : return true;
4645 : }
4646 :
4647 : /* Define a label, specifying the location in the source file.
4648 : Return the LABEL_DECL node for the label. */
4649 :
4650 : tree
4651 20464 : define_label (location_t location, tree name)
4652 : {
4653 20464 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4654 :
4655 : /* After labels, make any new cleanups in the function go into their
4656 : own new (temporary) binding contour. */
4657 20464 : for (cp_binding_level *p = current_binding_level;
4658 31053 : p->kind != sk_function_parms;
4659 10589 : p = p->level_chain)
4660 10589 : p->more_cleanups_ok = 0;
4661 :
4662 20464 : named_label_entry *ent = lookup_label_1 (name, false);
4663 20464 : tree decl = ent->label_decl;
4664 :
4665 20464 : if (DECL_INITIAL (decl) != NULL_TREE)
4666 : {
4667 6 : error ("duplicate label %qD", decl);
4668 6 : return error_mark_node;
4669 : }
4670 : else
4671 : {
4672 : /* Mark label as having been defined. */
4673 20458 : DECL_INITIAL (decl) = error_mark_node;
4674 : /* Say where in the source. */
4675 20458 : DECL_SOURCE_LOCATION (decl) = location;
4676 :
4677 20458 : ent->binding_level = current_binding_level;
4678 20458 : ent->names_in_scope = current_binding_level->names;
4679 :
4680 22564 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4681 2106 : check_previous_goto (decl, use);
4682 20458 : ent->uses = NULL;
4683 : }
4684 :
4685 20458 : return decl;
4686 20464 : }
4687 :
4688 : struct cp_switch
4689 : {
4690 : cp_binding_level *level;
4691 : struct cp_switch *next;
4692 : /* The SWITCH_STMT being built. */
4693 : tree switch_stmt;
4694 : /* A splay-tree mapping the low element of a case range to the high
4695 : element, or NULL_TREE if there is no high element. Used to
4696 : determine whether or not a new case label duplicates an old case
4697 : label. We need a tree, rather than simply a hash table, because
4698 : of the GNU case range extension. */
4699 : splay_tree cases;
4700 : /* Remember whether a default: case label has been seen. */
4701 : bool has_default_p;
4702 : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4703 : bool break_stmt_seen_p;
4704 : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4705 : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4706 : bool in_loop_body_p;
4707 : };
4708 :
4709 : /* A stack of the currently active switch statements. The innermost
4710 : switch statement is on the top of the stack. There is no need to
4711 : mark the stack for garbage collection because it is only active
4712 : during the processing of the body of a function, and we never
4713 : collect at that point. */
4714 :
4715 : static struct cp_switch *switch_stack;
4716 :
4717 : /* Called right after a switch-statement condition is parsed.
4718 : SWITCH_STMT is the switch statement being parsed. */
4719 :
4720 : void
4721 1033229 : push_switch (tree switch_stmt)
4722 : {
4723 1033229 : struct cp_switch *p = XNEW (struct cp_switch);
4724 1033229 : p->level = current_binding_level;
4725 1033229 : p->next = switch_stack;
4726 1033229 : p->switch_stmt = switch_stmt;
4727 1033229 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4728 1033229 : p->has_default_p = false;
4729 1033229 : p->break_stmt_seen_p = false;
4730 1033229 : p->in_loop_body_p = false;
4731 1033229 : switch_stack = p;
4732 1033229 : }
4733 :
4734 : void
4735 1033229 : pop_switch (void)
4736 : {
4737 1033229 : struct cp_switch *cs = switch_stack;
4738 :
4739 : /* Emit warnings as needed. */
4740 1033229 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4741 1033229 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4742 1033229 : const bool bool_cond_p
4743 1033229 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4744 1033229 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4745 1033229 : if (!processing_template_decl)
4746 628977 : c_do_switch_warnings (cs->cases, switch_location,
4747 628977 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4748 : bool_cond_p);
4749 :
4750 : /* For the benefit of block_may_fallthru remember if the switch body
4751 : case labels cover all possible values and if there are break; stmts. */
4752 1033229 : if (cs->has_default_p
4753 1033229 : || (!processing_template_decl
4754 80418 : && c_switch_covers_all_cases_p (cs->cases,
4755 80418 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4756 813487 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4757 1033229 : if (!cs->break_stmt_seen_p)
4758 474587 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4759 : /* Now that we're done with the switch warnings, set the switch type
4760 : to the type of the condition if the index type was of scoped enum type.
4761 : (Such types don't participate in the integer promotions.) We do this
4762 : because of bit-fields whose declared type is a scoped enum type:
4763 : gimplification will use the lowered index type, but convert the
4764 : case values to SWITCH_STMT_TYPE, which would have been the declared type
4765 : and verify_gimple_switch doesn't accept that. */
4766 1033229 : if (is_bitfield_expr_with_lowered_type (cond))
4767 219435 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4768 1033229 : gcc_assert (!cs->in_loop_body_p);
4769 1033229 : splay_tree_delete (cs->cases);
4770 1033229 : switch_stack = switch_stack->next;
4771 1033229 : free (cs);
4772 1033229 : }
4773 :
4774 : /* Note that a BREAK_STMT is about to be added. If it is inside of
4775 : a SWITCH_STMT and not inside of a loop body inside of it, note
4776 : in switch_stack we've seen a BREAK_STMT. */
4777 :
4778 : void
4779 5726489 : note_break_stmt (void)
4780 : {
4781 5726489 : if (switch_stack && !switch_stack->in_loop_body_p)
4782 3665790 : switch_stack->break_stmt_seen_p = true;
4783 5726489 : }
4784 :
4785 : /* Note the start of processing of an iteration statement's body.
4786 : The note_break_stmt function will do nothing while processing it.
4787 : Return a flag that should be passed to note_iteration_stmt_body_end. */
4788 :
4789 : bool
4790 18038245 : note_iteration_stmt_body_start (void)
4791 : {
4792 18038245 : if (!switch_stack)
4793 : return false;
4794 311965 : bool ret = switch_stack->in_loop_body_p;
4795 311965 : switch_stack->in_loop_body_p = true;
4796 311965 : return ret;
4797 : }
4798 :
4799 : /* Note the end of processing of an iteration statement's body. */
4800 :
4801 : void
4802 18038245 : note_iteration_stmt_body_end (bool prev)
4803 : {
4804 18038245 : if (switch_stack)
4805 311965 : switch_stack->in_loop_body_p = prev;
4806 18038245 : }
4807 :
4808 : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4809 : condition. Note that if TYPE and VALUE are already integral we don't
4810 : really do the conversion because the language-independent
4811 : warning/optimization code will work better that way. */
4812 :
4813 : static tree
4814 9403412 : case_conversion (tree type, tree value)
4815 : {
4816 9403412 : if (value == NULL_TREE)
4817 : return value;
4818 :
4819 4153315 : value = mark_rvalue_use (value);
4820 :
4821 4153315 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4822 1421762 : type = type_promotes_to (type);
4823 :
4824 4153315 : tree ovalue = value;
4825 : /* The constant-expression VALUE shall be a converted constant expression
4826 : of the adjusted type of the switch condition, which doesn't allow
4827 : narrowing conversions. */
4828 4153315 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4829 :
4830 4153315 : if (cxx_dialect >= cxx11
4831 4153315 : && (SCOPED_ENUM_P (type)
4832 1418641 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4833 : /* Use the converted value. */;
4834 : else
4835 : /* The already integral case. */
4836 : value = ovalue;
4837 :
4838 4153315 : return cxx_constant_value (value);
4839 : }
4840 :
4841 : /* Note that we've seen a definition of a case label, and complain if this
4842 : is a bad place for one. */
4843 :
4844 : tree
4845 8541306 : finish_case_label (location_t loc, tree low_value, tree high_value)
4846 : {
4847 8541306 : tree cond, r;
4848 8541306 : cp_binding_level *p;
4849 8541306 : tree type;
4850 :
4851 8541306 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4852 813451 : switch_stack->has_default_p = true;
4853 :
4854 8541306 : if (processing_template_decl)
4855 : {
4856 3839477 : tree label;
4857 :
4858 : /* For templates, just add the case label; we'll do semantic
4859 : analysis at instantiation-time. But diagnose case labels
4860 : in expansion statements with switch outside of it here. */
4861 3839477 : if (in_expansion_stmt)
4862 39 : for (cp_binding_level *b = current_binding_level;
4863 78 : b != switch_stack->level; b = b->level_chain)
4864 63 : if (b->kind == sk_template_for && b->this_entity)
4865 : {
4866 24 : auto_diagnostic_group d;
4867 24 : error ("jump to case label");
4868 24 : inform (EXPR_LOCATION (b->this_entity),
4869 : " enters %<template for%> statement");
4870 24 : return error_mark_node;
4871 24 : }
4872 3839453 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4873 3839453 : return add_stmt (build_case_label (low_value, high_value, label));
4874 : }
4875 :
4876 : /* Find the condition on which this switch statement depends. */
4877 4701829 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4878 4701829 : if (cond && TREE_CODE (cond) == TREE_LIST)
4879 0 : cond = TREE_VALUE (cond);
4880 :
4881 4701829 : int chk_switch_goto = check_switch_goto (switch_stack->level, NULL_TREE);
4882 4701829 : if (!chk_switch_goto)
4883 87 : return error_mark_node;
4884 :
4885 4701742 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4886 4701742 : if (type == error_mark_node)
4887 : return error_mark_node;
4888 :
4889 4701706 : low_value = case_conversion (type, low_value);
4890 4701706 : high_value = case_conversion (type, high_value);
4891 :
4892 4701706 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4893 :
4894 4701706 : if (r != error_mark_node && chk_switch_goto == 2)
4895 39 : check_switch_goto (switch_stack->level, r);
4896 :
4897 : /* After labels, make any new cleanups in the function go into their
4898 : own new (temporary) binding contour. */
4899 4701706 : for (p = current_binding_level;
4900 19498046 : p->kind != sk_function_parms;
4901 14796340 : p = p->level_chain)
4902 14796340 : p->more_cleanups_ok = 0;
4903 :
4904 : return r;
4905 : }
4906 :
4907 : struct typename_info {
4908 : tree scope;
4909 : tree name;
4910 : tree template_id;
4911 : tag_types tag_type;
4912 : };
4913 :
4914 : struct typename_hasher : ggc_ptr_hash<tree_node>
4915 : {
4916 : typedef typename_info *compare_type;
4917 :
4918 : /* Hash a TYPENAME_TYPE. */
4919 :
4920 : static hashval_t
4921 435549060 : hash (tree context, tree fullname)
4922 : {
4923 435549060 : hashval_t hash = 0;
4924 435549060 : hash = iterative_hash_object (context, hash);
4925 435549060 : hash = iterative_hash_object (fullname, hash);
4926 435549060 : return hash;
4927 : }
4928 :
4929 : static hashval_t
4930 61182128 : hash (const typename_info *ti)
4931 : {
4932 61182128 : return typename_hasher::hash (ti->scope, ti->template_id);
4933 : }
4934 :
4935 : static hashval_t
4936 374366932 : hash (tree t)
4937 : {
4938 374366932 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4939 : }
4940 :
4941 : /* Compare two TYPENAME_TYPEs. */
4942 :
4943 : static bool
4944 441300992 : equal (tree t1, const typename_info *t2)
4945 : {
4946 441300992 : return (TYPE_IDENTIFIER (t1) == t2->name
4947 140673919 : && TYPE_CONTEXT (t1) == t2->scope
4948 31509403 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4949 472799658 : && get_typename_tag (t1) == t2->tag_type);
4950 : }
4951 : };
4952 :
4953 : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4954 : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4955 :
4956 : Returns the new TYPENAME_TYPE. */
4957 :
4958 : static GTY (()) hash_table<typename_hasher> *typename_htab;
4959 :
4960 : tree
4961 61182128 : build_typename_type (tree context, tree name, tree fullname,
4962 : enum tag_types tag_type)
4963 : {
4964 61182128 : typename_info ti;
4965 :
4966 61182128 : if (typename_htab == NULL)
4967 18811 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4968 :
4969 61182128 : ti.scope = FROB_CONTEXT (context);
4970 61182128 : ti.name = name;
4971 61182128 : ti.template_id = fullname;
4972 61182128 : ti.tag_type = tag_type;
4973 61182128 : hashval_t hash = typename_hasher::hash (&ti);
4974 :
4975 : /* See if we already have this type. */
4976 61182128 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4977 61182128 : tree t = *e;
4978 61182128 : if (*e)
4979 : t = *e;
4980 : else
4981 : {
4982 : /* Build the TYPENAME_TYPE. */
4983 30716109 : t = cxx_make_type (TYPENAME_TYPE);
4984 30716109 : TYPE_CONTEXT (t) = ti.scope;
4985 30716109 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4986 30716109 : set_typename_tag (t, ti.tag_type);
4987 :
4988 : /* Build the corresponding TYPE_DECL. */
4989 30716109 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4990 30716109 : TYPE_NAME (t) = d;
4991 30716109 : TYPE_STUB_DECL (t) = d;
4992 30716109 : DECL_CONTEXT (d) = ti.scope;
4993 30716109 : DECL_ARTIFICIAL (d) = 1;
4994 :
4995 : /* Store it in the hash table. */
4996 30716109 : *e = t;
4997 :
4998 : /* TYPENAME_TYPEs must always be compared structurally, because
4999 : they may or may not resolve down to another type depending on
5000 : the currently open classes. */
5001 30716109 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5002 : }
5003 :
5004 61182128 : return t;
5005 : }
5006 :
5007 : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
5008 : provided to name the type. Returns an appropriate type, unless an
5009 : error occurs, in which case error_mark_node is returned. If we
5010 : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
5011 : return that, rather than the _TYPE it corresponds to, in other
5012 : cases we look through the type decl. If TF_ERROR is set, complain
5013 : about errors, otherwise be quiet. */
5014 :
5015 : tree
5016 332319983 : make_typename_type (tree context, tree name, enum tag_types tag_type,
5017 : tsubst_flags_t complain)
5018 : {
5019 332319983 : tree fullname;
5020 332319983 : tree t;
5021 332319983 : bool want_template;
5022 :
5023 332319983 : if (name == error_mark_node
5024 332319977 : || context == NULL_TREE
5025 332319977 : || context == error_mark_node)
5026 : return error_mark_node;
5027 :
5028 332319961 : if (TYPE_P (name))
5029 : {
5030 0 : if (!(TYPE_LANG_SPECIFIC (name)
5031 0 : && (CLASSTYPE_IS_TEMPLATE (name)
5032 0 : || CLASSTYPE_USE_TEMPLATE (name))))
5033 0 : name = TYPE_IDENTIFIER (name);
5034 : else
5035 : /* Create a TEMPLATE_ID_EXPR for the type. */
5036 0 : name = build_nt (TEMPLATE_ID_EXPR,
5037 0 : CLASSTYPE_TI_TEMPLATE (name),
5038 0 : CLASSTYPE_TI_ARGS (name));
5039 : }
5040 332319961 : else if (TREE_CODE (name) == TYPE_DECL)
5041 0 : name = DECL_NAME (name);
5042 :
5043 332319961 : fullname = name;
5044 :
5045 332319961 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5046 : {
5047 5226006 : name = TREE_OPERAND (name, 0);
5048 5226006 : if (DECL_TYPE_TEMPLATE_P (name))
5049 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5050 5226006 : if (TREE_CODE (name) != IDENTIFIER_NODE)
5051 : {
5052 0 : if (complain & tf_error)
5053 0 : error ("%qD is not a type", name);
5054 0 : return error_mark_node;
5055 : }
5056 : }
5057 332319961 : if (TREE_CODE (name) == TEMPLATE_DECL)
5058 : {
5059 0 : if (complain & tf_error)
5060 0 : error ("%qD used without template arguments", name);
5061 0 : return error_mark_node;
5062 : }
5063 332319961 : else if (is_overloaded_fn (name))
5064 : {
5065 3 : if (complain & tf_error)
5066 3 : error ("%qD is a function, not a type", name);
5067 3 : return error_mark_node;
5068 : }
5069 332319958 : gcc_assert (identifier_p (name));
5070 332319958 : gcc_assert (TYPE_P (context));
5071 :
5072 332319958 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5073 : /* This can happen for C++17 variadic using (c++/88986). */;
5074 319601923 : else if (!MAYBE_CLASS_TYPE_P (context))
5075 : {
5076 6 : if (complain & tf_error)
5077 6 : error ("%q#T is not a class", context);
5078 6 : return error_mark_node;
5079 : }
5080 :
5081 : /* When the CONTEXT is a dependent type, NAME could refer to a
5082 : dependent base class of CONTEXT. But look inside it anyway
5083 : if CONTEXT is a currently open scope, in case it refers to a
5084 : member of the current instantiation or a non-dependent base;
5085 : lookup will stop when we hit a dependent base. */
5086 332319952 : if (!dependent_scope_p (context))
5087 : {
5088 : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5089 : (as per [temp.res.general]/3), unless
5090 : - the tag corresponds to a class-key or 'enum' so
5091 : [basic.lookup.elab] applies, or
5092 : - the tag corresponds to scope_type or tf_qualifying_scope is
5093 : set so [basic.lookup.qual]/1 applies, or
5094 : - we're inside a base-specifier so [class.derived.general]/2 applies;
5095 : the tag will already be class_type in that case.
5096 : TODO: If we'd set/track the scope_type tag thoroughly on all
5097 : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5098 : tf_qualifying_scope flag. */
5099 551506946 : bool want_type = (tag_type != none_type && tag_type != typename_type)
5100 275753473 : || (complain & tf_qualifying_scope);
5101 275753473 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5102 : }
5103 : else
5104 : t = NULL_TREE;
5105 :
5106 361675696 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5107 56567403 : return build_typename_type (context, name, fullname, tag_type);
5108 :
5109 275752549 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5110 :
5111 275752549 : if (!t)
5112 : {
5113 29354820 : if (complain & tf_error)
5114 : {
5115 223 : if (!COMPLETE_TYPE_P (context))
5116 18 : cxx_incomplete_type_error (NULL_TREE, context);
5117 : else
5118 409 : error (want_template ? G_("no class template named %q#T in %q#T")
5119 : : G_("no type named %q#T in %q#T"), name, context);
5120 : }
5121 29354820 : return error_mark_node;
5122 : }
5123 :
5124 : /* Pull out the template from an injected-class-name (or multiple). */
5125 246397729 : if (want_template)
5126 3415379 : t = maybe_get_template_decl_from_type_decl (t);
5127 :
5128 246397729 : if (TREE_CODE (t) == TREE_LIST)
5129 : {
5130 3 : if (complain & tf_error)
5131 : {
5132 3 : auto_diagnostic_group d;
5133 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
5134 3 : print_candidates (input_location, t);
5135 3 : }
5136 3 : return error_mark_node;
5137 : }
5138 :
5139 246397726 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5140 : {
5141 6 : if (complain & tf_error)
5142 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
5143 : context, name, t);
5144 6 : return error_mark_node;
5145 : }
5146 246397720 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
5147 : {
5148 1469 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5149 2941 : && DECL_TYPE_TEMPLATE_P (t))
5150 : /* The caller permits this typename-specifier to name a template
5151 : (because it appears in a CTAD-enabled context). */;
5152 : else
5153 : {
5154 23 : if (complain & tf_error)
5155 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
5156 : context, name, t);
5157 23 : return error_mark_node;
5158 : }
5159 : }
5160 :
5161 246397697 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5162 : context, complain))
5163 46 : return error_mark_node;
5164 :
5165 246397651 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5166 1458 : return make_template_placeholder (t);
5167 :
5168 246396193 : if (want_template)
5169 : {
5170 3415373 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5171 : NULL_TREE, context,
5172 : complain | tf_user);
5173 3415373 : if (t == error_mark_node)
5174 : return error_mark_node;
5175 3415368 : t = TYPE_NAME (t);
5176 : }
5177 :
5178 246396188 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5179 2232321 : t = TREE_TYPE (t);
5180 :
5181 246396188 : maybe_record_typedef_use (t);
5182 :
5183 246396188 : return t;
5184 : }
5185 :
5186 : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5187 : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5188 : in which case error_mark_node is returned.
5189 :
5190 : If PARM_LIST is non-NULL, also make sure that the template parameter
5191 : list of TEMPLATE_DECL matches.
5192 :
5193 : If COMPLAIN zero, don't complain about any errors that occur. */
5194 :
5195 : tree
5196 172152 : make_unbound_class_template (tree context, tree name, tree parm_list,
5197 : tsubst_flags_t complain)
5198 : {
5199 172152 : if (TYPE_P (name))
5200 0 : name = TYPE_IDENTIFIER (name);
5201 172152 : else if (DECL_P (name))
5202 0 : name = DECL_NAME (name);
5203 172152 : gcc_assert (identifier_p (name));
5204 :
5205 172152 : if (!dependent_type_p (context)
5206 172152 : || currently_open_class (context))
5207 : {
5208 84230 : tree tmpl = NULL_TREE;
5209 :
5210 84230 : if (MAYBE_CLASS_TYPE_P (context))
5211 84224 : tmpl = lookup_field (context, name, 0, false);
5212 :
5213 84224 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5214 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5215 :
5216 84218 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5217 : {
5218 12 : if (complain & tf_error)
5219 9 : error ("no class template named %q#T in %q#T", name, context);
5220 12 : return error_mark_node;
5221 : }
5222 :
5223 84218 : if (parm_list
5224 84218 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5225 : {
5226 0 : if (complain & tf_error)
5227 : {
5228 0 : auto_diagnostic_group d;
5229 0 : error ("template parameters do not match template %qD", tmpl);
5230 0 : inform (DECL_SOURCE_LOCATION (tmpl),
5231 : "%qD declared here", tmpl);
5232 0 : }
5233 0 : return error_mark_node;
5234 : }
5235 :
5236 84218 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5237 : complain))
5238 0 : return error_mark_node;
5239 :
5240 : return tmpl;
5241 : }
5242 :
5243 87922 : return make_unbound_class_template_raw (context, name, parm_list);
5244 : }
5245 :
5246 : /* Build an UNBOUND_CLASS_TEMPLATE. */
5247 :
5248 : tree
5249 87974 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5250 : {
5251 : /* Build the UNBOUND_CLASS_TEMPLATE. */
5252 87974 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5253 87974 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5254 87974 : TREE_TYPE (t) = NULL_TREE;
5255 87974 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5256 :
5257 : /* Build the corresponding TEMPLATE_DECL. */
5258 87974 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5259 87974 : TYPE_NAME (t) = d;
5260 87974 : TYPE_STUB_DECL (t) = d;
5261 87974 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5262 87974 : DECL_ARTIFICIAL (d) = 1;
5263 87974 : DECL_TEMPLATE_PARMS (d) = parm_list;
5264 :
5265 87974 : return t;
5266 : }
5267 :
5268 :
5269 :
5270 : /* Push the declarations of builtin types into the global namespace.
5271 : RID_INDEX is the index of the builtin type in the array
5272 : RID_POINTERS. NAME is the name used when looking up the builtin
5273 : type. TYPE is the _TYPE node for the builtin type.
5274 :
5275 : The calls to set_global_binding below should be
5276 : eliminated. Built-in types should not be looked up name; their
5277 : names are keywords that the parser can recognize. However, there
5278 : is code in c-common.cc that uses identifier_global_value to look up
5279 : built-in types by name. */
5280 :
5281 : void
5282 4064438 : record_builtin_type (enum rid rid_index,
5283 : const char* name,
5284 : tree type)
5285 : {
5286 4064438 : tree decl = NULL_TREE;
5287 :
5288 4064438 : if (name)
5289 : {
5290 2397877 : tree tname = get_identifier (name);
5291 2397877 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5292 2397877 : DECL_ARTIFICIAL (tdecl) = 1;
5293 2397877 : set_global_binding (tdecl);
5294 2397877 : decl = tdecl;
5295 : }
5296 :
5297 4064438 : if ((int) rid_index < (int) RID_MAX)
5298 2495371 : if (tree rname = ridpointers[(int) rid_index])
5299 1907173 : if (!decl || DECL_NAME (decl) != rname)
5300 : {
5301 1372462 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5302 1372462 : DECL_ARTIFICIAL (rdecl) = 1;
5303 1372462 : set_global_binding (rdecl);
5304 1372462 : if (!decl)
5305 : decl = rdecl;
5306 : }
5307 :
5308 3770339 : if (decl)
5309 : {
5310 3476240 : if (!TYPE_NAME (type))
5311 2888042 : TYPE_NAME (type) = decl;
5312 3476240 : debug_hooks->type_decl (decl, 0);
5313 : }
5314 4064438 : }
5315 :
5316 : /* Push a type into the namespace so that the back ends ignore it. */
5317 :
5318 : static void
5319 294099 : record_unknown_type (tree type, const char* name)
5320 : {
5321 294099 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5322 : TYPE_DECL, get_identifier (name), type));
5323 : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5324 294099 : DECL_IGNORED_P (decl) = 1;
5325 294099 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5326 294099 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5327 294099 : SET_TYPE_ALIGN (type, 1);
5328 294099 : TYPE_USER_ALIGN (type) = 0;
5329 294099 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5330 294099 : }
5331 :
5332 : /* Create all the predefined identifiers. */
5333 :
5334 : static void
5335 98033 : initialize_predefined_identifiers (void)
5336 : {
5337 98033 : struct predefined_identifier
5338 : {
5339 : const char *name; /* Name. */
5340 : tree *node; /* Node to store it in. */
5341 : cp_identifier_kind kind; /* Kind of identifier. */
5342 : };
5343 :
5344 : /* A table of identifiers to create at startup. */
5345 98033 : static const predefined_identifier predefined_identifiers[] = {
5346 : {"C++", &lang_name_cplusplus, cik_normal},
5347 : {"C", &lang_name_c, cik_normal},
5348 : /* Some of these names have a trailing space so that it is
5349 : impossible for them to conflict with names written by users. */
5350 : {"__ct ", &ctor_identifier, cik_ctor},
5351 : {"__ct_base ", &base_ctor_identifier, cik_ctor},
5352 : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
5353 : {"__dt ", &dtor_identifier, cik_dtor},
5354 : {"__dt_base ", &base_dtor_identifier, cik_dtor},
5355 : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
5356 : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
5357 : {"__conv_op ", &conv_op_identifier, cik_conv_op},
5358 : {"__in_chrg", &in_charge_identifier, cik_normal},
5359 : {"__as_base ", &as_base_identifier, cik_normal},
5360 : {"this", &this_identifier, cik_normal},
5361 : {"__delta", &delta_identifier, cik_normal},
5362 : {"__pfn", &pfn_identifier, cik_normal},
5363 : {"_vptr", &vptr_identifier, cik_normal},
5364 : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
5365 : {"::", &global_identifier, cik_normal},
5366 : /* The demangler expects anonymous namespaces to be called
5367 : something starting with '_GLOBAL__N_'. It no longer needs
5368 : to be unique to the TU. */
5369 : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
5370 : {"auto", &auto_identifier, cik_normal},
5371 : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
5372 : {"initializer_list", &init_list_identifier, cik_normal},
5373 : {"__for_range ", &for_range__identifier, cik_normal},
5374 : {"__for_begin ", &for_begin__identifier, cik_normal},
5375 : {"__for_end ", &for_end__identifier, cik_normal},
5376 : {"__for_range", &for_range_identifier, cik_normal},
5377 : {"__for_begin", &for_begin_identifier, cik_normal},
5378 : {"__for_end", &for_end_identifier, cik_normal},
5379 : {"abi_tag", &abi_tag_identifier, cik_normal},
5380 : {"aligned", &aligned_identifier, cik_normal},
5381 : {"begin", &begin_identifier, cik_normal},
5382 : {"end", &end_identifier, cik_normal},
5383 : {"get", &get__identifier, cik_normal},
5384 : {"gnu", &gnu_identifier, cik_normal},
5385 : {"tuple_element", &tuple_element_identifier, cik_normal},
5386 : {"tuple_size", &tuple_size_identifier, cik_normal},
5387 : {"type", &type_identifier, cik_normal},
5388 : {"value", &value_identifier, cik_normal},
5389 : {"_FUN", &fun_identifier, cik_normal},
5390 : {"__closure", &closure_identifier, cik_normal},
5391 : {"heap uninit", &heap_uninit_identifier, cik_normal},
5392 : {"heap ", &heap_identifier, cik_normal},
5393 : {"heap deleted", &heap_deleted_identifier, cik_normal},
5394 : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
5395 : {"heap []", &heap_vec_identifier, cik_normal},
5396 : {"omp", &omp_identifier, cik_normal},
5397 : {"internal ", &internal_identifier, cik_normal},
5398 : {"annotation ", &annotation_identifier, cik_normal},
5399 : {NULL, NULL, cik_normal}
5400 : };
5401 :
5402 4803617 : for (const predefined_identifier *pid = predefined_identifiers;
5403 4803617 : pid->name; ++pid)
5404 : {
5405 4705584 : *pid->node = get_identifier (pid->name);
5406 : /* Some of these identifiers already have a special kind. */
5407 4705584 : if (pid->kind != cik_normal)
5408 784264 : set_identifier_kind (*pid->node, pid->kind);
5409 : }
5410 98033 : }
5411 :
5412 : /* Build a specific variant of operator new. */
5413 :
5414 : static void
5415 174993 : cxx_build_operator_new (tree newtype)
5416 : {
5417 174993 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5418 174993 : DECL_IS_MALLOC (opnew) = 1;
5419 174993 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5420 174993 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5421 174993 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5422 174993 : DECL_IS_MALLOC (opnew) = 1;
5423 174993 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5424 174993 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5425 174993 : }
5426 :
5427 : /* Build a specific variant of operator delete. */
5428 :
5429 : static void
5430 330191 : cxx_build_operator_delete (tree deltype)
5431 : {
5432 330191 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5433 330191 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5434 330191 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5435 330191 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5436 330191 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5437 330191 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5438 330191 : }
5439 :
5440 : /* Declare all variants of operator new and delete. */
5441 :
5442 : static void
5443 98033 : cxx_init_operator_new_delete_decls (void)
5444 : {
5445 98033 : tree newattrs, extvisattr;
5446 98033 : tree newtype, deltype;
5447 98033 : tree ptr_ftype_sizetype;
5448 98033 : tree new_eh_spec;
5449 98033 : tree void_ftype_ptr = build_function_type_list (void_type_node,
5450 : ptr_type_node, NULL_TREE);
5451 98033 : void_ftype_ptr
5452 98033 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
5453 :
5454 98033 : ptr_ftype_sizetype
5455 98033 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5456 98033 : if (cxx_dialect == cxx98)
5457 : {
5458 14145 : tree bad_alloc_id;
5459 14145 : tree bad_alloc_type_node;
5460 14145 : tree bad_alloc_decl;
5461 :
5462 14145 : push_nested_namespace (std_node);
5463 14145 : bad_alloc_id = get_identifier ("bad_alloc");
5464 14145 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5465 14145 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5466 14145 : bad_alloc_decl
5467 14145 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5468 14145 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5469 14145 : pop_nested_namespace (std_node);
5470 :
5471 14145 : new_eh_spec
5472 14145 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5473 : }
5474 : else
5475 83888 : new_eh_spec = noexcept_false_spec;
5476 :
5477 98033 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5478 : NULL_TREE);
5479 98033 : newattrs = tree_cons (get_identifier ("alloc_size"),
5480 : build_tree_list (NULL_TREE, integer_one_node),
5481 : extvisattr);
5482 98033 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5483 98033 : newtype = build_exception_variant (newtype, new_eh_spec);
5484 98033 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5485 98033 : deltype = build_exception_variant (deltype, empty_except_spec);
5486 98033 : cxx_build_operator_new (newtype);
5487 98033 : cxx_build_operator_delete (deltype);
5488 98033 : if (flag_sized_deallocation)
5489 : {
5490 : /* Also push the sized deallocation variants:
5491 : void operator delete(void*, std::size_t) throw();
5492 : void operator delete[](void*, std::size_t) throw(); */
5493 78243 : tree void_ftype_ptr_size
5494 78243 : = build_function_type_list (void_type_node, ptr_type_node,
5495 : size_type_node, NULL_TREE);
5496 78243 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5497 : extvisattr);
5498 78243 : deltype = build_exception_variant (deltype, empty_except_spec);
5499 78243 : cxx_build_operator_delete (deltype);
5500 : }
5501 :
5502 98033 : if (aligned_new_threshold)
5503 : {
5504 76960 : push_nested_namespace (std_node);
5505 76960 : tree align_id = get_identifier ("align_val_t");
5506 76960 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5507 : NULL_TREE, /*scoped*/true, NULL);
5508 76960 : pop_nested_namespace (std_node);
5509 :
5510 : /* operator new (size_t, align_val_t); */
5511 76960 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5512 : align_type_node, NULL_TREE);
5513 76960 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5514 76960 : newtype = build_exception_variant (newtype, new_eh_spec);
5515 76960 : cxx_build_operator_new (newtype);
5516 :
5517 : /* operator delete (void *, align_val_t); */
5518 76960 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5519 : align_type_node, NULL_TREE);
5520 76960 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5521 76960 : deltype = build_exception_variant (deltype, empty_except_spec);
5522 76960 : cxx_build_operator_delete (deltype);
5523 :
5524 76960 : if (flag_sized_deallocation)
5525 : {
5526 : /* operator delete (void *, size_t, align_val_t); */
5527 76955 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5528 : size_type_node, align_type_node,
5529 : NULL_TREE);
5530 76955 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5531 76955 : deltype = build_exception_variant (deltype, empty_except_spec);
5532 76955 : cxx_build_operator_delete (deltype);
5533 : }
5534 : }
5535 98033 : }
5536 :
5537 : /* Create the predefined scalar types of C,
5538 : and some nodes representing standard constants (0, 1, (void *)0).
5539 : Initialize the global binding level.
5540 : Make definitions for built-in primitive functions. */
5541 :
5542 : void
5543 98033 : cxx_init_decl_processing (void)
5544 : {
5545 98033 : tree void_ftype;
5546 :
5547 : /* Create all the identifiers we need. */
5548 98033 : initialize_predefined_identifiers ();
5549 :
5550 : /* Create the global variables. */
5551 98033 : push_to_top_level ();
5552 :
5553 98033 : current_function_decl = NULL_TREE;
5554 98033 : current_binding_level = NULL;
5555 : /* Enter the global namespace. */
5556 98033 : gcc_assert (global_namespace == NULL_TREE);
5557 98033 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5558 : void_type_node);
5559 98033 : TREE_PUBLIC (global_namespace) = true;
5560 98033 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5561 98033 : DECL_CONTEXT (global_namespace)
5562 98033 : = build_translation_unit_decl (get_identifier (main_input_filename));
5563 : /* Remember whether we want the empty class passing ABI change warning
5564 : in this TU. */
5565 98033 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5566 122566 : = warn_abi && abi_version_crosses (12);
5567 98033 : debug_hooks->register_main_translation_unit
5568 98033 : (DECL_CONTEXT (global_namespace));
5569 98033 : begin_scope (sk_namespace, global_namespace);
5570 98033 : current_namespace = global_namespace;
5571 :
5572 98033 : if (flag_visibility_ms_compat)
5573 6 : default_visibility = VISIBILITY_HIDDEN;
5574 :
5575 : /* Initially, C. */
5576 98033 : current_lang_name = lang_name_c;
5577 :
5578 : /* Create the `std' namespace. */
5579 98033 : push_namespace (get_identifier ("std"));
5580 98033 : std_node = current_namespace;
5581 98033 : pop_namespace ();
5582 :
5583 98033 : flag_noexcept_type = (cxx_dialect >= cxx17);
5584 :
5585 98033 : c_common_nodes_and_builtins ();
5586 :
5587 98033 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5588 98033 : tree decl
5589 98033 : = add_builtin_function ("__builtin_is_constant_evaluated",
5590 : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5591 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5592 98033 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5593 :
5594 : /* The concrete return type of __builtin_source_location is
5595 : const std::source_location::__impl*, but we can't form the type
5596 : at this point. So we initially declare it with an auto return
5597 : type which we then "deduce" from require_deduced_type upon first use. */
5598 98033 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5599 98033 : decl = add_builtin_function ("__builtin_source_location",
5600 : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5601 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5602 98033 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5603 :
5604 98033 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5605 : NULL_TREE);
5606 98033 : decl
5607 98033 : = add_builtin_function ("__builtin_is_corresponding_member",
5608 : bool_vaftype,
5609 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5610 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5611 98033 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5612 :
5613 98033 : decl
5614 98033 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5615 : bool_vaftype,
5616 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5617 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5618 98033 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5619 :
5620 98033 : if (cxx_dialect >= cxx26)
5621 : {
5622 23587 : tree void_ptrintftype
5623 23587 : = build_function_type_list (void_type_node, ptr_type_node,
5624 : integer_type_node, NULL_TREE);
5625 23587 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5626 : void_ptrintftype,
5627 : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5628 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5629 23587 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5630 : }
5631 :
5632 98033 : decl
5633 98033 : = add_builtin_function ("__builtin_is_string_literal",
5634 : bool_vaftype,
5635 : CP_BUILT_IN_IS_STRING_LITERAL,
5636 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5637 98033 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5638 :
5639 98033 : tree void_vaintftype = build_varargs_function_type_list (void_type_node,
5640 : integer_type_node,
5641 : NULL_TREE);
5642 98033 : decl = add_builtin_function ("__builtin_constexpr_diag",
5643 : void_vaintftype,
5644 : CP_BUILT_IN_CONSTEXPR_DIAG,
5645 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5646 98033 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5647 :
5648 98033 : integer_two_node = build_int_cst (NULL_TREE, 2);
5649 :
5650 : /* Guess at the initial static decls size. */
5651 98033 : vec_alloc (static_decls, 500);
5652 :
5653 : /* ... and keyed classes. */
5654 98033 : vec_alloc (keyed_classes, 100);
5655 :
5656 98033 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5657 98033 : truthvalue_type_node = boolean_type_node;
5658 98033 : truthvalue_false_node = boolean_false_node;
5659 98033 : truthvalue_true_node = boolean_true_node;
5660 :
5661 98033 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5662 98033 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5663 98033 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5664 98033 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5665 : NULL_TREE);
5666 :
5667 : #if 0
5668 : record_builtin_type (RID_MAX, NULL, string_type_node);
5669 : #endif
5670 :
5671 98033 : delta_type_node = ptrdiff_type_node;
5672 98033 : vtable_index_type = ptrdiff_type_node;
5673 :
5674 98033 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5675 98033 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5676 :
5677 : /* Create the conversion operator marker. This operator's DECL_NAME
5678 : is in the identifier table, so we can use identifier equality to
5679 : find it. */
5680 98033 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5681 : void_ftype);
5682 :
5683 : /* C++ extensions */
5684 :
5685 98033 : unknown_type_node = make_node (LANG_TYPE);
5686 98033 : record_unknown_type (unknown_type_node, "unknown type");
5687 :
5688 : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5689 98033 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5690 :
5691 : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5692 : result. */
5693 98033 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5694 98033 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5695 :
5696 98033 : init_list_type_node = make_node (LANG_TYPE);
5697 98033 : record_unknown_type (init_list_type_node, "init list");
5698 :
5699 : /* Used when parsing to distinguish parameter-lists () and (void). */
5700 98033 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5701 :
5702 98033 : {
5703 : /* Make sure we get a unique function type, so we can give
5704 : its pointer type a name. (This wins for gdb.) */
5705 98033 : tree vfunc_type = make_node (FUNCTION_TYPE);
5706 98033 : TREE_TYPE (vfunc_type) = integer_type_node;
5707 98033 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5708 98033 : layout_type (vfunc_type);
5709 :
5710 98033 : vtable_entry_type = build_pointer_type (vfunc_type);
5711 : }
5712 98033 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5713 :
5714 98033 : vtbl_type_node
5715 98033 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5716 98033 : layout_type (vtbl_type_node);
5717 98033 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5718 98033 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5719 98033 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5720 98033 : layout_type (vtbl_ptr_type_node);
5721 98033 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5722 :
5723 98033 : push_namespace (get_identifier ("__cxxabiv1"));
5724 98033 : abi_node = current_namespace;
5725 98033 : pop_namespace ();
5726 :
5727 98033 : any_targ_node = make_node (LANG_TYPE);
5728 98033 : record_unknown_type (any_targ_node, "any type");
5729 :
5730 : /* Now, C++. */
5731 98033 : current_lang_name = lang_name_cplusplus;
5732 :
5733 98033 : if (aligned_new_threshold > 1
5734 98033 : && !pow2p_hwi (aligned_new_threshold))
5735 : {
5736 0 : error ("%<-faligned-new=%d%> is not a power of two",
5737 : aligned_new_threshold);
5738 0 : aligned_new_threshold = 1;
5739 : }
5740 98033 : if (aligned_new_threshold == -1)
5741 119092 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5742 98033 : if (aligned_new_threshold == 1)
5743 76957 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5744 :
5745 : /* Ensure attribs.cc is initialized. */
5746 98033 : init_attributes ();
5747 98033 : cxx_init_operator_new_delete_decls ();
5748 :
5749 : /* C++-specific nullptr initialization. */
5750 98033 : if (abi_version_at_least (9))
5751 97862 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5752 98033 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5753 :
5754 98033 : if (! supports_one_only ())
5755 0 : flag_weak = 0;
5756 :
5757 98033 : abort_fndecl
5758 98033 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5759 : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5760 98033 : if (flag_weak)
5761 : /* If no definition is available, resolve references to NULL. */
5762 98003 : declare_weak (abort_fndecl);
5763 :
5764 : /* Perform other language dependent initializations. */
5765 98033 : init_class_processing ();
5766 98033 : init_rtti_processing ();
5767 98033 : init_template_processing ();
5768 :
5769 98033 : if (flag_exceptions)
5770 96778 : init_exception_processing ();
5771 :
5772 98033 : if (flag_contracts)
5773 23632 : init_contracts ();
5774 :
5775 98033 : if (modules_p ())
5776 4814 : init_modules (parse_in);
5777 :
5778 98033 : if (flag_reflection)
5779 539 : init_reflection ();
5780 :
5781 98033 : make_fname_decl = cp_make_fname_decl;
5782 98033 : start_fname_decls ();
5783 :
5784 : /* Show we use EH for cleanups. */
5785 98033 : if (flag_exceptions)
5786 96778 : using_eh_for_cleanups ();
5787 :
5788 : /* Check that the hardware interference sizes are at least
5789 : alignof(max_align_t), as required by the standard. */
5790 98033 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5791 98033 : if (OPTION_SET_P (param_destruct_interfere_size))
5792 : {
5793 0 : if (param_destruct_interfere_size < max_align)
5794 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5795 : "%d", param_destruct_interfere_size, max_align);
5796 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5797 0 : warning (OPT_Winterference_size,
5798 : "%<--param destructive-interference-size=%d%> "
5799 : "is less than %<--param l1-cache-line-size=%d%>",
5800 : param_destruct_interfere_size, param_l1_cache_line_size);
5801 : }
5802 98033 : else if (param_destruct_interfere_size)
5803 : /* Assume the internal value is OK. */;
5804 0 : else if (param_l1_cache_line_size >= max_align)
5805 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5806 : /* else leave it unset. */
5807 :
5808 98033 : if (OPTION_SET_P (param_construct_interfere_size))
5809 : {
5810 0 : if (param_construct_interfere_size < max_align)
5811 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5812 : "%d", param_construct_interfere_size, max_align);
5813 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5814 0 : && param_l1_cache_line_size >= max_align)
5815 0 : warning (OPT_Winterference_size,
5816 : "%<--param constructive-interference-size=%d%> "
5817 : "is greater than %<--param l1-cache-line-size=%d%>",
5818 : param_construct_interfere_size, param_l1_cache_line_size);
5819 : }
5820 98033 : else if (param_construct_interfere_size)
5821 : /* Assume the internal value is OK. */;
5822 0 : else if (param_l1_cache_line_size >= max_align)
5823 0 : param_construct_interfere_size = param_l1_cache_line_size;
5824 98033 : }
5825 :
5826 : /* Enter an abi node in global-module context. returns a cookie to
5827 : give to pop_abi_namespace. */
5828 :
5829 : unsigned
5830 63236 : push_abi_namespace (tree node)
5831 : {
5832 63236 : push_nested_namespace (node);
5833 63236 : push_visibility ("default", 2);
5834 63236 : unsigned flags = module_kind;
5835 63236 : module_kind = 0;
5836 63236 : return flags;
5837 : }
5838 :
5839 : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5840 : you. */
5841 :
5842 : void
5843 63236 : pop_abi_namespace (unsigned flags, tree node)
5844 : {
5845 63236 : module_kind = flags;
5846 63236 : pop_visibility (2);
5847 63236 : pop_nested_namespace (node);
5848 63236 : }
5849 :
5850 : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5851 : the decl, LOC is the location to give the decl, NAME is the
5852 : initialization string and TYPE_DEP indicates whether NAME depended
5853 : on the type of the function. We make use of that to detect
5854 : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5855 : at the point of first use, so we mustn't push the decl now. */
5856 :
5857 : static tree
5858 168149 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5859 : {
5860 168149 : tree domain = NULL_TREE;
5861 168149 : tree init = NULL_TREE;
5862 :
5863 168149 : if (!(type_dep && current_function_decl && in_template_context))
5864 : {
5865 45958 : const char *name = NULL;
5866 45958 : bool release_name = false;
5867 :
5868 45958 : if (current_function_decl == NULL_TREE)
5869 : name = "top level";
5870 45944 : else if (type_dep == 0)
5871 : {
5872 : /* __FUNCTION__ */
5873 581 : name = fname_as_string (type_dep);
5874 581 : release_name = true;
5875 : }
5876 : else
5877 : {
5878 : /* __PRETTY_FUNCTION__ */
5879 45363 : gcc_checking_assert (type_dep == 1);
5880 45363 : name = cxx_printable_name (current_function_decl, 2);
5881 : }
5882 :
5883 45944 : if (!release_name)
5884 : {
5885 45377 : cpp_string cstr = { 0, 0 }, strname;
5886 45377 : size_t len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5887 45377 : char *namep = XNEWVEC (char, len);
5888 45377 : snprintf (namep, len, "\"%s\"", name);
5889 45377 : strname.text = (unsigned char *) namep;
5890 45377 : strname.len = len - 1;
5891 45377 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
5892 : {
5893 45377 : name = (const char *) cstr.text;
5894 45377 : release_name = true;
5895 : }
5896 :
5897 45377 : XDELETEVEC (namep);
5898 : }
5899 :
5900 45958 : size_t length = strlen (name);
5901 45958 : domain = build_index_type (size_int (length));
5902 45958 : init = build_string (length + 1, name);
5903 45958 : if (release_name)
5904 45958 : free (const_cast<char *> (name));
5905 : }
5906 :
5907 168149 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5908 168149 : type = build_cplus_array_type (type, domain);
5909 :
5910 168149 : if (init)
5911 45958 : TREE_TYPE (init) = type;
5912 : else
5913 122191 : init = error_mark_node;
5914 :
5915 168149 : tree decl = build_decl (loc, VAR_DECL, id, type);
5916 :
5917 168149 : TREE_READONLY (decl) = 1;
5918 168149 : DECL_ARTIFICIAL (decl) = 1;
5919 168149 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5920 168149 : TREE_STATIC (decl) = 1;
5921 :
5922 168149 : TREE_USED (decl) = 1;
5923 :
5924 168149 : SET_DECL_VALUE_EXPR (decl, init);
5925 168149 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5926 : /* For decl_constant_var_p. */
5927 168149 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5928 :
5929 168149 : if (current_function_decl)
5930 : {
5931 168135 : DECL_CONTEXT (decl) = current_function_decl;
5932 168135 : decl = pushdecl_outermost_localscope (decl);
5933 168135 : if (decl != error_mark_node)
5934 168132 : add_decl_expr (decl);
5935 : else
5936 3 : gcc_assert (seen_error ());
5937 : }
5938 : else
5939 : {
5940 14 : DECL_THIS_STATIC (decl) = true;
5941 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5942 : }
5943 :
5944 168149 : return decl;
5945 : }
5946 :
5947 : /* Install DECL as a builtin function at current global scope. Return
5948 : the new decl (if we found an existing version). Also installs it
5949 : into ::std, if it's not '_*'. */
5950 :
5951 : tree
5952 227072383 : cxx_builtin_function (tree decl)
5953 : {
5954 227072383 : retrofit_lang_decl (decl);
5955 :
5956 227072383 : DECL_ARTIFICIAL (decl) = 1;
5957 227072383 : SET_DECL_LANGUAGE (decl, lang_c);
5958 : /* Runtime library routines are, by definition, available in an
5959 : external shared object. */
5960 227072383 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5961 227072383 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5962 :
5963 227072383 : tree id = DECL_NAME (decl);
5964 227072383 : const char *name = IDENTIFIER_POINTER (id);
5965 227072383 : bool hiding = false;
5966 227072383 : if (name[0] != '_' || name[1] != '_')
5967 : /* In the user's namespace, it must be declared before use. */
5968 : hiding = true;
5969 190451837 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5970 190451837 : && !startswith (name + 2, "builtin_")
5971 213273750 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5972 : "_chk", strlen ("_chk") + 1))
5973 : /* Treat __*_chk fortification functions as anticipated as well,
5974 : unless they are __builtin_*_chk. */
5975 : hiding = true;
5976 :
5977 : /* All builtins that don't begin with an '_' should additionally
5978 : go in the 'std' namespace. */
5979 227072383 : if (name[0] != '_')
5980 : {
5981 36486794 : tree std_decl = copy_decl (decl);
5982 :
5983 36486794 : push_nested_namespace (std_node);
5984 36486794 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5985 36486794 : pushdecl (std_decl, hiding);
5986 36486794 : pop_nested_namespace (std_node);
5987 : }
5988 :
5989 227072383 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5990 227072383 : decl = pushdecl (decl, hiding);
5991 :
5992 227072383 : return decl;
5993 : }
5994 :
5995 : /* Like cxx_builtin_function, but guarantee the function is added to the global
5996 : scope. This is to allow function specific options to add new machine
5997 : dependent builtins when the target ISA changes via attribute((target(...)))
5998 : which saves space on program startup if the program does not use non-generic
5999 : ISAs. */
6000 :
6001 : tree
6002 757172 : cxx_builtin_function_ext_scope (tree decl)
6003 : {
6004 757172 : push_nested_namespace (global_namespace);
6005 757172 : decl = cxx_builtin_function (decl);
6006 757172 : pop_nested_namespace (global_namespace);
6007 :
6008 757172 : return decl;
6009 : }
6010 :
6011 : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
6012 :
6013 : tree
6014 0 : cxx_simulate_builtin_function_decl (tree decl)
6015 : {
6016 0 : retrofit_lang_decl (decl);
6017 :
6018 0 : DECL_ARTIFICIAL (decl) = 1;
6019 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
6020 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6021 0 : return pushdecl (decl);
6022 : }
6023 :
6024 : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6025 : function. Not called directly. */
6026 :
6027 : static tree
6028 1582904 : build_library_fn (tree name, enum tree_code operator_code, tree type,
6029 : int ecf_flags)
6030 : {
6031 1582904 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6032 1582904 : DECL_EXTERNAL (fn) = 1;
6033 1582904 : TREE_PUBLIC (fn) = 1;
6034 1582904 : DECL_ARTIFICIAL (fn) = 1;
6035 1582904 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
6036 1582904 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
6037 1582904 : SET_DECL_LANGUAGE (fn, lang_c);
6038 : /* Runtime library routines are, by definition, available in an
6039 : external shared object. */
6040 1582904 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
6041 1582904 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
6042 1582904 : set_call_expr_flags (fn, ecf_flags);
6043 1582904 : return fn;
6044 : }
6045 :
6046 : /* Returns the _DECL for a library function with C++ linkage. */
6047 :
6048 : static tree
6049 1107338 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
6050 : int ecf_flags)
6051 : {
6052 1107338 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
6053 1107338 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6054 1107338 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
6055 1107338 : return fn;
6056 : }
6057 :
6058 : /* Like build_library_fn, but takes a C string instead of an
6059 : IDENTIFIER_NODE. */
6060 :
6061 : tree
6062 109761 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
6063 : {
6064 109761 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
6065 : }
6066 :
6067 : /* Like build_cp_library_fn, but takes a C string instead of an
6068 : IDENTIFIER_NODE. */
6069 :
6070 : tree
6071 96970 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
6072 : {
6073 96970 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
6074 96970 : ecf_flags);
6075 : }
6076 :
6077 : /* Like build_library_fn, but also pushes the function so that we will
6078 : be able to find it via get_global_binding. Also, the function
6079 : may throw exceptions listed in RAISES. */
6080 :
6081 : tree
6082 365805 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6083 : {
6084 365805 : if (raises)
6085 39993 : type = build_exception_variant (type, raises);
6086 :
6087 365805 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
6088 365805 : return pushdecl_top_level (fn);
6089 : }
6090 :
6091 : /* Like build_cp_library_fn, but also pushes the function so that it
6092 : will be found by normal lookup. */
6093 :
6094 : static tree
6095 1010368 : push_cp_library_fn (enum tree_code operator_code, tree type,
6096 : int ecf_flags)
6097 : {
6098 1010368 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
6099 : operator_code, type, ecf_flags);
6100 1010368 : pushdecl (fn);
6101 1010368 : if (flag_tm)
6102 3232 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
6103 1010368 : return fn;
6104 : }
6105 :
6106 : /* Like push_library_fn, but also note that this function throws
6107 : and does not return. Used for __throw_foo and the like. */
6108 :
6109 : tree
6110 107318 : push_throw_library_fn (tree name, tree type)
6111 : {
6112 107318 : tree fn = push_library_fn (name, type, NULL_TREE,
6113 : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6114 107318 : return fn;
6115 : }
6116 :
6117 : /* When we call finish_struct for an anonymous union, we create
6118 : default copy constructors and such. But, an anonymous union
6119 : shouldn't have such things; this function undoes the damage to the
6120 : anonymous union type T.
6121 :
6122 : (The reason that we create the synthesized methods is that we don't
6123 : distinguish `union { int i; }' from `typedef union { int i; } U'.
6124 : The first is an anonymous union; the second is just an ordinary
6125 : union type.) */
6126 :
6127 : void
6128 168974 : fixup_anonymous_aggr (tree t)
6129 : {
6130 : /* Wipe out memory of synthesized methods. */
6131 168974 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6132 168974 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6133 168974 : TYPE_HAS_COPY_CTOR (t) = 0;
6134 168974 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6135 168974 : TYPE_HAS_COPY_ASSIGN (t) = 0;
6136 168974 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6137 :
6138 : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6139 : invalid members. */
6140 989660 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6141 : {
6142 820686 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6143 0 : *prev_p = DECL_CHAIN (probe);
6144 : else
6145 820686 : prev_p = &DECL_CHAIN (probe);
6146 :
6147 820686 : if (DECL_ARTIFICIAL (probe)
6148 820686 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6149 74382 : || TYPE_ANON_P (TREE_TYPE (probe))))
6150 277589 : continue;
6151 :
6152 543097 : if (TREE_CODE (probe) != FIELD_DECL
6153 543043 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6154 : {
6155 : /* We already complained about static data members in
6156 : finish_static_data_member_decl. */
6157 66 : if (!VAR_P (probe))
6158 : {
6159 51 : auto_diagnostic_group d;
6160 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
6161 51 : TREE_CODE (t) == UNION_TYPE
6162 : ? "%q#D invalid; an anonymous union may "
6163 : "only have public non-static data members"
6164 : : "%q#D invalid; an anonymous struct may "
6165 : "only have public non-static data members", probe))
6166 : {
6167 51 : static bool hint;
6168 51 : if (flag_permissive && !hint)
6169 : {
6170 6 : hint = true;
6171 6 : inform (DECL_SOURCE_LOCATION (probe),
6172 : "this flexibility is deprecated and will be "
6173 : "removed");
6174 : }
6175 : }
6176 51 : }
6177 : }
6178 : }
6179 :
6180 : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6181 168974 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6182 168974 : unsigned store = 0;
6183 506331 : for (tree elt : vec)
6184 337357 : if (!is_overloaded_fn (elt))
6185 337336 : (*vec)[store++] = elt;
6186 168974 : vec_safe_truncate (vec, store);
6187 :
6188 : /* Wipe RTTI info. */
6189 168974 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
6190 :
6191 : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6192 : assignment operators (because they cannot have these methods themselves).
6193 : For anonymous unions this is already checked because they are not allowed
6194 : in any union, otherwise we have to check it. */
6195 168974 : if (TREE_CODE (t) != UNION_TYPE)
6196 : {
6197 36521 : tree field, type;
6198 :
6199 36521 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6200 : {
6201 9 : error_at (location_of (t), "anonymous struct with base classes");
6202 : /* Avoid ICE after error on anon-struct9.C. */
6203 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
6204 : }
6205 :
6206 146916 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6207 110395 : if (TREE_CODE (field) == FIELD_DECL)
6208 : {
6209 73773 : type = TREE_TYPE (field);
6210 73773 : if (CLASS_TYPE_P (type))
6211 : {
6212 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
6213 6 : error ("member %q+#D with constructor not allowed "
6214 : "in anonymous aggregate", field);
6215 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6216 0 : error ("member %q+#D with destructor not allowed "
6217 : "in anonymous aggregate", field);
6218 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6219 0 : error ("member %q+#D with copy assignment operator "
6220 : "not allowed in anonymous aggregate", field);
6221 : }
6222 : }
6223 : }
6224 168974 : }
6225 :
6226 : /* Warn for an attribute located at LOCATION that appertains to the
6227 : class type CLASS_TYPE that has not been properly placed after its
6228 : class-key, in it class-specifier. */
6229 :
6230 : void
6231 109 : warn_misplaced_attr_for_class_type (location_t location,
6232 : tree class_type)
6233 : {
6234 109 : gcc_assert (OVERLOAD_TYPE_P (class_type));
6235 :
6236 109 : auto_diagnostic_group d;
6237 109 : if (warning_at (location, OPT_Wattributes,
6238 : "attribute ignored in declaration "
6239 : "of %q#T", class_type))
6240 109 : inform (location,
6241 : "attribute for %q#T must follow the %qs keyword",
6242 : class_type, class_key_or_enum_as_string (class_type));
6243 109 : }
6244 :
6245 : /* Returns the cv-qualifiers that apply to the type specified
6246 : by the DECLSPECS. */
6247 :
6248 : static int
6249 1008671233 : get_type_quals (const cp_decl_specifier_seq *declspecs)
6250 : {
6251 1008671233 : int type_quals = TYPE_UNQUALIFIED;
6252 :
6253 1008671233 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6254 104501042 : type_quals |= TYPE_QUAL_CONST;
6255 1008671233 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6256 1650367 : type_quals |= TYPE_QUAL_VOLATILE;
6257 1008671233 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6258 34 : type_quals |= TYPE_QUAL_RESTRICT;
6259 :
6260 1008671233 : return type_quals;
6261 : }
6262 :
6263 : /* Make sure that a declaration with no declarator is well-formed, i.e.
6264 : just declares a tagged type or anonymous union.
6265 :
6266 : Returns the type declared; or NULL_TREE if none. */
6267 :
6268 : tree
6269 36052697 : check_tag_decl (cp_decl_specifier_seq *declspecs,
6270 : bool explicit_type_instantiation_p)
6271 : {
6272 36052697 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6273 36052697 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6274 : /* If a class, struct, or enum type is declared by the DECLSPECS
6275 : (i.e, if a class-specifier, enum-specifier, or non-typename
6276 : elaborated-type-specifier appears in the DECLSPECS),
6277 : DECLARED_TYPE is set to the corresponding type. */
6278 36052697 : tree declared_type = NULL_TREE;
6279 36052697 : bool error_p = false;
6280 :
6281 36052697 : if (declspecs->multiple_types_p)
6282 26 : error_at (smallest_type_location (declspecs),
6283 : "multiple types in one declaration");
6284 36052671 : else if (declspecs->redefined_builtin_type)
6285 : {
6286 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6287 21 : if (!in_system_header_at (loc))
6288 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
6289 : declspecs->redefined_builtin_type);
6290 21 : return NULL_TREE;
6291 : }
6292 :
6293 36052676 : if (declspecs->type
6294 36052661 : && TYPE_P (declspecs->type)
6295 71777103 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6296 35723667 : && MAYBE_CLASS_TYPE_P (declspecs->type))
6297 1801141 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6298 : declared_type = declspecs->type;
6299 329054 : else if (declspecs->type == error_mark_node)
6300 1499 : error_p = true;
6301 :
6302 36052676 : if (type_uses_auto (declared_type))
6303 : {
6304 8 : error_at (declspecs->locations[ds_type_spec],
6305 : "%<auto%> can only be specified for variables "
6306 : "or function declarations");
6307 8 : return error_mark_node;
6308 : }
6309 :
6310 36052668 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6311 : declared_type = NULL_TREE;
6312 :
6313 36052668 : if (!declared_type && !saw_friend && !error_p)
6314 91 : permerror (input_location, "declaration does not declare anything");
6315 : /* Check for an anonymous union. */
6316 35723587 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6317 104068059 : && TYPE_UNNAMED_P (declared_type))
6318 : {
6319 : /* 7/3 In a simple-declaration, the optional init-declarator-list
6320 : can be omitted only when declaring a class (clause 9) or
6321 : enumeration (7.2), that is, when the decl-specifier-seq contains
6322 : either a class-specifier, an elaborated-type-specifier with
6323 : a class-key (9.1), or an enum-specifier. In these cases and
6324 : whenever a class-specifier or enum-specifier is present in the
6325 : decl-specifier-seq, the identifiers in these specifiers are among
6326 : the names being declared by the declaration (as class-name,
6327 : enum-names, or enumerators, depending on the syntax). In such
6328 : cases, and except for the declaration of an unnamed bit-field (9.6),
6329 : the decl-specifier-seq shall introduce one or more names into the
6330 : program, or shall redeclare a name introduced by a previous
6331 : declaration. [Example:
6332 : enum { }; // ill-formed
6333 : typedef class { }; // ill-formed
6334 : --end example] */
6335 168980 : if (saw_typedef)
6336 : {
6337 6 : error_at (declspecs->locations[ds_typedef],
6338 : "missing type-name in typedef-declaration");
6339 6 : return NULL_TREE;
6340 : }
6341 168974 : /* Anonymous unions are objects, so they can have specifiers. */;
6342 168974 : SET_ANON_AGGR_TYPE_P (declared_type);
6343 :
6344 168974 : if (TREE_CODE (declared_type) != UNION_TYPE)
6345 36521 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6346 36521 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6347 : }
6348 :
6349 : else
6350 : {
6351 35883597 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6352 9 : error_at (declspecs->locations[ds_inline],
6353 : "%<inline%> can only be specified for functions");
6354 35883588 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6355 3 : error_at (declspecs->locations[ds_virtual],
6356 : "%<virtual%> can only be specified for functions");
6357 35883585 : else if (saw_friend
6358 35883585 : && (!current_class_type
6359 2001284 : || current_scope () != current_class_type))
6360 0 : error_at (declspecs->locations[ds_friend],
6361 : "%<friend%> can only be specified inside a class");
6362 35883585 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6363 3 : error_at (declspecs->locations[ds_explicit],
6364 : "%<explicit%> can only be specified for constructors");
6365 35883582 : else if (declspecs->storage_class)
6366 3 : error_at (declspecs->locations[ds_storage_class],
6367 : "a storage class can only be specified for objects "
6368 : "and functions");
6369 35883579 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6370 9 : error_at (declspecs->locations[ds_const],
6371 : "%<const%> can only be specified for objects and "
6372 : "functions");
6373 35883570 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6374 3 : error_at (declspecs->locations[ds_volatile],
6375 : "%<volatile%> can only be specified for objects and "
6376 : "functions");
6377 35883567 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6378 3 : error_at (declspecs->locations[ds_restrict],
6379 : "%<__restrict%> can only be specified for objects and "
6380 : "functions");
6381 35883564 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6382 3 : error_at (declspecs->locations[ds_thread],
6383 : "%<__thread%> can only be specified for objects "
6384 : "and functions");
6385 35883561 : else if (saw_typedef)
6386 24 : warning_at (declspecs->locations[ds_typedef], 0,
6387 : "%<typedef%> was ignored in this declaration");
6388 35883537 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6389 9 : error_at (declspecs->locations[ds_constexpr],
6390 : "%qs cannot be used for type declarations", "constexpr");
6391 35883528 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6392 3 : error_at (declspecs->locations[ds_constinit],
6393 : "%qs cannot be used for type declarations", "constinit");
6394 35883525 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6395 3 : error_at (declspecs->locations[ds_consteval],
6396 : "%qs cannot be used for type declarations", "consteval");
6397 : }
6398 :
6399 36052662 : if (declspecs->attributes && warn_attributes && declared_type)
6400 : {
6401 30 : location_t loc;
6402 24 : if (!CLASS_TYPE_P (declared_type)
6403 54 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6404 : /* For a non-template class, use the name location. */
6405 21 : loc = location_of (declared_type);
6406 : else
6407 : /* For a template class (an explicit instantiation), use the
6408 : current location. */
6409 9 : loc = input_location;
6410 :
6411 30 : if (explicit_type_instantiation_p)
6412 : /* [dcl.attr.grammar]/4:
6413 :
6414 : No attribute-specifier-seq shall appertain to an explicit
6415 : instantiation. */
6416 : {
6417 6 : auto_diagnostic_group d;
6418 6 : if (warning_at (loc, OPT_Wattributes,
6419 : "attribute ignored in explicit instantiation %q#T",
6420 : declared_type))
6421 6 : inform (loc,
6422 : "no attribute can be applied to "
6423 : "an explicit instantiation");
6424 6 : }
6425 : else
6426 24 : warn_misplaced_attr_for_class_type (loc, declared_type);
6427 : }
6428 :
6429 36052662 : if (declspecs->std_attributes
6430 85 : && declared_type
6431 36052744 : && any_nonignored_attribute_p (declspecs->std_attributes))
6432 : {
6433 82 : auto_diagnostic_group d;
6434 82 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6435 : "attribute ignored"))
6436 82 : inform (declspecs->locations[ds_std_attribute],
6437 : "an attribute that appertains to a type-specifier is ignored");
6438 82 : }
6439 :
6440 : return declared_type;
6441 : }
6442 :
6443 : /* Called when a declaration is seen that contains no names to declare.
6444 : If its type is a reference to a structure, union or enum inherited
6445 : from a containing scope, shadow that tag name for the current scope
6446 : with a forward reference.
6447 : If its type defines a new named structure or union
6448 : or defines an enum, it is valid but we need not do anything here.
6449 : Otherwise, it is an error.
6450 :
6451 : C++: may have to grok the declspecs to learn about static,
6452 : complain for anonymous unions.
6453 :
6454 : Returns the TYPE declared -- or NULL_TREE if none. */
6455 :
6456 : tree
6457 31576261 : shadow_tag (cp_decl_specifier_seq *declspecs)
6458 : {
6459 31576261 : tree t = check_tag_decl (declspecs,
6460 : /*explicit_type_instantiation_p=*/false);
6461 :
6462 31576261 : if (!t)
6463 : return NULL_TREE;
6464 :
6465 31574748 : t = maybe_process_partial_specialization (t);
6466 31574748 : if (t == error_mark_node)
6467 : return NULL_TREE;
6468 :
6469 : /* This is where the variables in an anonymous union are
6470 : declared. An anonymous union declaration looks like:
6471 : union { ... } ;
6472 : because there is no declarator after the union, the parser
6473 : sends that declaration here. */
6474 31572910 : if (ANON_AGGR_TYPE_P (t))
6475 : {
6476 313 : fixup_anonymous_aggr (t);
6477 :
6478 313 : if (TYPE_FIELDS (t))
6479 : {
6480 313 : tree decl = grokdeclarator (/*declarator=*/NULL,
6481 : declspecs, NORMAL, 0, NULL);
6482 313 : finish_anon_union (decl);
6483 : }
6484 : }
6485 :
6486 : return t;
6487 : }
6488 :
6489 : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6490 :
6491 : tree
6492 393155466 : groktypename (cp_decl_specifier_seq *type_specifiers,
6493 : const cp_declarator *declarator,
6494 : bool is_template_arg)
6495 : {
6496 393155466 : tree attrs;
6497 393155466 : tree type;
6498 64990349 : enum decl_context context
6499 393155466 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6500 393155466 : attrs = type_specifiers->attributes;
6501 393155466 : type_specifiers->attributes = NULL_TREE;
6502 393155466 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6503 393155466 : if (attrs && type != error_mark_node)
6504 : {
6505 635 : if (CLASS_TYPE_P (type))
6506 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6507 : "outside of definition", type);
6508 629 : else if (MAYBE_CLASS_TYPE_P (type))
6509 : /* A template type parameter or other dependent type. */
6510 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6511 : "type %qT without an associated declaration", type);
6512 : else
6513 620 : cplus_decl_attributes (&type, attrs, 0);
6514 : }
6515 393155466 : return type;
6516 : }
6517 :
6518 : /* Process a DECLARATOR for a function-scope or namespace-scope
6519 : variable or function declaration.
6520 : (Function definitions go through start_function; class member
6521 : declarations appearing in the body of the class go through
6522 : grokfield.) The DECL corresponding to the DECLARATOR is returned.
6523 : If an error occurs, the error_mark_node is returned instead.
6524 :
6525 : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6526 : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6527 : for an explicitly defaulted function, or SD_DELETED for an explicitly
6528 : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6529 : implicitly initialized via a default constructor. It can also be
6530 : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6531 : mark the new decl as DECL_DECOMPOSITION_P.
6532 :
6533 : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6534 : declaration.
6535 :
6536 : The scope represented by the context of the returned DECL is pushed
6537 : (if it is not the global namespace) and is assigned to
6538 : *PUSHED_SCOPE_P. The caller is then responsible for calling
6539 : pop_scope on *PUSHED_SCOPE_P if it is set. */
6540 :
6541 : tree
6542 124053299 : start_decl (const cp_declarator *declarator,
6543 : cp_decl_specifier_seq *declspecs,
6544 : int initialized,
6545 : tree attributes,
6546 : tree prefix_attributes,
6547 : tree *pushed_scope_p)
6548 : {
6549 124053299 : tree decl;
6550 124053299 : tree context;
6551 124053299 : bool was_public;
6552 124053299 : int flags;
6553 124053299 : bool alias;
6554 124053299 : tree initial;
6555 :
6556 124053299 : *pushed_scope_p = NULL_TREE;
6557 :
6558 124053299 : if (prefix_attributes != error_mark_node)
6559 124053297 : attributes = attr_chainon (attributes, prefix_attributes);
6560 :
6561 124053299 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6562 : &attributes);
6563 :
6564 124053299 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6565 124053299 : || decl == error_mark_node
6566 124051148 : || prefix_attributes == error_mark_node)
6567 2153 : return error_mark_node;
6568 :
6569 124051146 : context = CP_DECL_CONTEXT (decl);
6570 124051146 : if (context != global_namespace)
6571 21486707 : *pushed_scope_p = push_scope (context);
6572 :
6573 124051146 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6574 : {
6575 27 : error_at (DECL_SOURCE_LOCATION (decl),
6576 : "typedef %qD is initialized (use %qs instead)",
6577 : decl, "decltype");
6578 27 : return error_mark_node;
6579 : }
6580 :
6581 124051119 : if (flag_contracts
6582 9020600 : && TREE_CODE (decl) == FUNCTION_DECL
6583 2540687 : && !processing_template_decl
6584 2050112 : && DECL_RESULT (decl)
6585 126096892 : && is_auto (TREE_TYPE (DECL_RESULT (decl))))
6586 31 : for (tree ca = get_fn_contract_specifiers (decl); ca; ca = TREE_CHAIN (ca))
6587 6 : if (POSTCONDITION_P (CONTRACT_STATEMENT (ca))
6588 6 : && POSTCONDITION_IDENTIFIER (CONTRACT_STATEMENT (ca)))
6589 : {
6590 2 : error_at (DECL_SOURCE_LOCATION (decl),
6591 : "postconditions with deduced result name types must only"
6592 : " appear on function definitions");
6593 2 : return error_mark_node;
6594 : }
6595 : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6596 : with attribute validation. */
6597 124051117 : initial = DECL_INITIAL (decl);
6598 :
6599 124051117 : if (initialized)
6600 : {
6601 59816498 : if (! toplevel_bindings_p ()
6602 59816498 : && DECL_EXTERNAL (decl))
6603 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6604 : decl);
6605 59816498 : DECL_EXTERNAL (decl) = 0;
6606 59816498 : if (toplevel_bindings_p ())
6607 7051833 : TREE_STATIC (decl) = 1;
6608 : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6609 : even though we might not yet have the initializer expression. */
6610 59816498 : if (!DECL_INITIAL (decl))
6611 59266522 : DECL_INITIAL (decl) = error_mark_node;
6612 : }
6613 124051117 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6614 :
6615 124051117 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6616 3981 : record_key_method_defined (decl);
6617 :
6618 : /* If this is a typedef that names the class for linkage purposes
6619 : (7.1.3p8), apply any attributes directly to the type. */
6620 124051117 : if (TREE_CODE (decl) == TYPE_DECL
6621 124051117 : && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl))
6622 : flags = ATTR_FLAG_TYPE_IN_PLACE;
6623 : else
6624 : flags = 0;
6625 :
6626 : /* Set attributes here so if duplicate decl, will have proper attributes. */
6627 124051117 : cplus_decl_attributes (&decl, attributes, flags);
6628 :
6629 : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6630 : assist with attribute validation. */
6631 124051117 : DECL_INITIAL (decl) = initial;
6632 :
6633 : /* Dllimported symbols cannot be defined. Static data members (which
6634 : can be initialized in-class and dllimported) go through grokfield,
6635 : not here, so we don't need to exclude those decls when checking for
6636 : a definition. */
6637 124051117 : if (initialized && DECL_DLLIMPORT_P (decl))
6638 : {
6639 0 : error_at (DECL_SOURCE_LOCATION (decl),
6640 : "definition of %q#D is marked %<dllimport%>", decl);
6641 0 : DECL_DLLIMPORT_P (decl) = 0;
6642 : }
6643 :
6644 : /* If #pragma weak was used, mark the decl weak now. */
6645 124051117 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6646 45450994 : maybe_apply_pragma_weak (decl);
6647 :
6648 124051117 : if (TREE_CODE (decl) == FUNCTION_DECL
6649 36819815 : && DECL_DECLARED_INLINE_P (decl)
6650 3134887 : && DECL_UNINLINABLE (decl)
6651 124051117 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6652 : {
6653 0 : auto_urlify_attributes sentinel;
6654 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6655 : "inline function %qD given attribute %qs", decl, "noinline");
6656 0 : }
6657 :
6658 124051117 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6659 : {
6660 824058 : bool this_tmpl = (current_template_depth
6661 824058 : > template_class_depth (context));
6662 824058 : if (VAR_P (decl))
6663 : {
6664 477634 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6665 477634 : if (field == NULL_TREE
6666 477634 : || !(VAR_P (field) || variable_template_p (field)))
6667 6 : error ("%q+#D is not a static data member of %q#T", decl, context);
6668 477628 : else if (variable_template_p (field)
6669 477628 : && (DECL_LANG_SPECIFIC (decl)
6670 147 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6671 : /* OK, specialization was already checked. */;
6672 477559 : else if (variable_template_p (field) && !this_tmpl)
6673 : {
6674 3 : auto_diagnostic_group d;
6675 3 : error_at (DECL_SOURCE_LOCATION (decl),
6676 : "non-member-template declaration of %qD", decl);
6677 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6678 : "member template declaration here");
6679 3 : return error_mark_node;
6680 3 : }
6681 : else
6682 : {
6683 477556 : if (variable_template_p (field))
6684 75 : field = DECL_TEMPLATE_RESULT (field);
6685 :
6686 477556 : if (DECL_CONTEXT (field) != context)
6687 : {
6688 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6689 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6690 : "to be defined as %<%T::%D%>",
6691 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6692 3 : context, DECL_NAME (decl));
6693 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6694 : }
6695 :
6696 477556 : if (modules_p () && !module_may_redeclare (field))
6697 30 : return error_mark_node;
6698 :
6699 : /* Static data member are tricky; an in-class initialization
6700 : still doesn't provide a definition, so the in-class
6701 : declaration will have DECL_EXTERNAL set, but will have an
6702 : initialization. Thus, duplicate_decls won't warn
6703 : about this situation, and so we check here. */
6704 477526 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6705 9 : error ("duplicate initialization of %qD", decl);
6706 477526 : field = duplicate_decls (decl, field);
6707 477526 : if (field == error_mark_node)
6708 : return error_mark_node;
6709 477470 : else if (field)
6710 477470 : decl = field;
6711 : }
6712 : }
6713 : else
6714 : {
6715 346436 : tree field = check_classfn (context, decl,
6716 : this_tmpl
6717 12 : ? current_template_parms
6718 : : NULL_TREE);
6719 346349 : if (field && field != error_mark_node
6720 692773 : && duplicate_decls (decl, field))
6721 346349 : decl = field;
6722 : }
6723 :
6724 : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6725 823969 : DECL_IN_AGGR_P (decl) = 0;
6726 : /* Do not mark DECL as an explicit specialization if it was not
6727 : already marked as an instantiation; a declaration should
6728 : never be marked as a specialization unless we know what
6729 : template is being specialized. */
6730 823969 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6731 : {
6732 344591 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6733 344591 : if (TREE_CODE (decl) == FUNCTION_DECL)
6734 324690 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6735 324690 : && DECL_NONGNU_INLINE_P (decl));
6736 : else
6737 19901 : DECL_COMDAT (decl) = false;
6738 :
6739 : /* [temp.expl.spec] An explicit specialization of a static data
6740 : member of a template is a definition if the declaration
6741 : includes an initializer; otherwise, it is a declaration.
6742 :
6743 : We check for processing_specialization so this only applies
6744 : to the new specialization syntax. */
6745 344591 : if (!initialized && processing_specialization)
6746 344389 : DECL_EXTERNAL (decl) = 1;
6747 : }
6748 :
6749 1168406 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6750 : /* Aliases are definitions. */
6751 824005 : && !alias)
6752 : {
6753 9 : permerror (declarator->id_loc,
6754 : "declaration of %q#D outside of class is not definition",
6755 : decl);
6756 : }
6757 : }
6758 :
6759 : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6760 124051028 : if (initialized == SD_DECOMPOSITION)
6761 436426 : fit_decomposition_lang_decl (decl, NULL_TREE);
6762 :
6763 124051028 : was_public = TREE_PUBLIC (decl);
6764 :
6765 211271991 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6766 124601028 : && current_function_decl)
6767 : {
6768 : /* A function-scope decl of some namespace-scope decl. */
6769 59255 : DECL_LOCAL_DECL_P (decl) = true;
6770 59255 : if (named_module_attach_p ())
6771 3 : error_at (declarator->id_loc,
6772 : "block-scope extern declaration %q#D must not be"
6773 : " attached to a named module", decl);
6774 : }
6775 :
6776 : /* Enter this declaration into the symbol table. Don't push the plain
6777 : VAR_DECL for a variable template. */
6778 124051028 : if (!template_parm_scope_p ()
6779 124051028 : || !VAR_P (decl))
6780 120141419 : decl = maybe_push_decl (decl);
6781 :
6782 124051028 : if (processing_template_decl)
6783 78594676 : decl = push_template_decl (decl);
6784 :
6785 124051028 : if (decl == error_mark_node)
6786 : return error_mark_node;
6787 :
6788 124050598 : if (VAR_P (decl)
6789 65881521 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6790 67121 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6791 : /* But not templated variables. */
6792 124078831 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6793 : {
6794 : /* This is a const variable with implicit 'static'. Set
6795 : DECL_THIS_STATIC so we can tell it from variables that are
6796 : !TREE_PUBLIC because of the anonymous namespace. */
6797 28233 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6798 28233 : DECL_THIS_STATIC (decl) = 1;
6799 : }
6800 :
6801 70128154 : if (current_function_decl && VAR_P (decl)
6802 58035510 : && maybe_constexpr_fn (current_function_decl)
6803 138448385 : && cxx_dialect < cxx23)
6804 : {
6805 12367042 : bool ok = false;
6806 12367042 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6807 : {
6808 9 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6809 18 : error_at (DECL_SOURCE_LOCATION (decl),
6810 : "%qD defined %<thread_local%> in %qs function only "
6811 : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6812 9 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6813 : ? "consteval" : "constexpr");
6814 : }
6815 12367033 : else if (TREE_STATIC (decl))
6816 : {
6817 50 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6818 72 : error_at (DECL_SOURCE_LOCATION (decl),
6819 : "%qD defined %<static%> in %qs function only available "
6820 : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6821 36 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6822 : ? "consteval" : "constexpr");
6823 : }
6824 : else
6825 : ok = true;
6826 45 : if (!ok)
6827 59 : cp_function_chain->invalid_constexpr = true;
6828 : }
6829 :
6830 124050598 : if (!processing_template_decl && VAR_P (decl))
6831 8303264 : start_decl_1 (decl, initialized);
6832 :
6833 124050595 : return decl;
6834 : }
6835 :
6836 : /* Process the declaration of a variable DECL. INITIALIZED is true
6837 : iff DECL is explicitly initialized. (INITIALIZED is false if the
6838 : variable is initialized via an implicitly-called constructor.)
6839 : This function must be called for ordinary variables (including, for
6840 : example, implicit instantiations of templates), but must not be
6841 : called for template declarations. */
6842 :
6843 : void
6844 8311422 : start_decl_1 (tree decl, bool initialized)
6845 : {
6846 8311422 : gcc_checking_assert (!processing_template_decl);
6847 :
6848 8311422 : if (error_operand_p (decl))
6849 : return;
6850 :
6851 8311407 : gcc_checking_assert (VAR_P (decl));
6852 :
6853 8311407 : tree type = TREE_TYPE (decl);
6854 8311407 : bool complete_p = COMPLETE_TYPE_P (type);
6855 8311407 : bool aggregate_definition_p
6856 8311407 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6857 :
6858 : /* If an explicit initializer is present, or if this is a definition
6859 : of an aggregate, then we need a complete type at this point.
6860 : (Scalars are always complete types, so there is nothing to
6861 : check.) This code just sets COMPLETE_P; errors (if necessary)
6862 : are issued below. */
6863 4299336 : if ((initialized || aggregate_definition_p)
6864 7308944 : && !complete_p
6865 5901567 : && COMPLETE_TYPE_P (complete_type (type)))
6866 : {
6867 325966 : complete_p = true;
6868 : /* We will not yet have set TREE_READONLY on DECL if the type
6869 : was "const", but incomplete, before this point. But, now, we
6870 : have a complete type, so we can try again. */
6871 325966 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6872 : }
6873 :
6874 8311404 : if (initialized)
6875 : /* Is it valid for this decl to have an initializer at all? */
6876 : {
6877 : /* Don't allow initializations for incomplete types except for
6878 : arrays which might be completed by the initialization. */
6879 6581381 : if (complete_p)
6880 : ; /* A complete type is ok. */
6881 1276028 : else if (type_uses_auto (type))
6882 : ; /* An auto type is ok. */
6883 118894 : else if (TREE_CODE (type) != ARRAY_TYPE)
6884 : {
6885 27 : auto_diagnostic_group d;
6886 27 : error ("variable %q#D has initializer but incomplete type", decl);
6887 48 : maybe_suggest_missing_header (input_location,
6888 27 : TYPE_IDENTIFIER (type),
6889 27 : CP_TYPE_CONTEXT (type));
6890 27 : type = TREE_TYPE (decl) = error_mark_node;
6891 27 : }
6892 118867 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6893 : {
6894 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6895 0 : error ("elements of array %q#D have incomplete type", decl);
6896 : /* else we already gave an error in start_decl. */
6897 : }
6898 : }
6899 1730023 : else if (aggregate_definition_p && !complete_p)
6900 : {
6901 234 : if (type_uses_auto (type))
6902 69 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6903 : else
6904 : {
6905 165 : auto_diagnostic_group d;
6906 165 : error ("aggregate %q#D has incomplete type and cannot be defined",
6907 : decl);
6908 285 : maybe_suggest_missing_header (input_location,
6909 165 : TYPE_IDENTIFIER (type),
6910 165 : CP_TYPE_CONTEXT (type));
6911 : /* Change the type so that assemble_variable will give
6912 : DECL an rtl we can live with: (mem (const_int 0)). */
6913 165 : type = TREE_TYPE (decl) = error_mark_node;
6914 165 : }
6915 : }
6916 :
6917 : /* Create a new scope to hold this declaration if necessary.
6918 : Whether or not a new scope is necessary cannot be determined
6919 : until after the type has been completed; if the type is a
6920 : specialization of a class template it is not until after
6921 : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6922 : will be set correctly. */
6923 8311404 : maybe_push_cleanup_level (type);
6924 : }
6925 :
6926 : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6927 : C++20 P0960. TYPE is the type of the object we're initializing. */
6928 :
6929 : tree
6930 295 : do_aggregate_paren_init (tree init, tree type)
6931 : {
6932 295 : tree val = TREE_VALUE (init);
6933 :
6934 295 : if (TREE_CHAIN (init) == NULL_TREE)
6935 : {
6936 : /* If the list has a single element and it's a string literal,
6937 : then it's the initializer for the array as a whole. */
6938 179 : if (TREE_CODE (type) == ARRAY_TYPE
6939 179 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6940 225 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6941 : == STRING_CST)
6942 : return val;
6943 : /* Handle non-standard extensions like compound literals. This also
6944 : prevents triggering aggregate parenthesized-initialization in
6945 : compiler-generated code for =default. */
6946 140 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6947 140 : TREE_TYPE (val)))
6948 : return val;
6949 : }
6950 :
6951 200 : init = build_constructor_from_list (init_list_type_node, init);
6952 200 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6953 200 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6954 200 : return init;
6955 : }
6956 :
6957 : /* Handle initialization of references. DECL, TYPE, and INIT have the
6958 : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6959 : but will be set to a new CLEANUP_STMT if a temporary is created
6960 : that must be destroyed subsequently.
6961 :
6962 : Returns an initializer expression to use to initialize DECL, or
6963 : NULL if the initialization can be performed statically.
6964 :
6965 : Quotes on semantics can be found in ARM 8.4.3. */
6966 :
6967 : static tree
6968 512462 : grok_reference_init (tree decl, tree type, tree init, int flags)
6969 : {
6970 512462 : if (init == NULL_TREE)
6971 : {
6972 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6973 3 : || DECL_IN_AGGR_P (decl) == 0)
6974 18 : && ! DECL_THIS_EXTERN (decl))
6975 15 : error_at (DECL_SOURCE_LOCATION (decl),
6976 : "%qD declared as reference but not initialized", decl);
6977 15 : return NULL_TREE;
6978 : }
6979 :
6980 512447 : tree ttype = TREE_TYPE (type);
6981 512447 : if (TREE_CODE (init) == TREE_LIST)
6982 : {
6983 : /* This handles (C++20 only) code like
6984 :
6985 : const A& r(1, 2, 3);
6986 :
6987 : where we treat the parenthesized list as a CONSTRUCTOR. */
6988 1161 : if (TREE_TYPE (init) == NULL_TREE
6989 1161 : && CP_AGGREGATE_TYPE_P (ttype)
6990 69 : && !DECL_DECOMPOSITION_P (decl)
6991 1227 : && (cxx_dialect >= cxx20))
6992 : {
6993 : /* We don't know yet if we should treat const A& r(1) as
6994 : const A& r{1}. */
6995 60 : if (list_length (init) == 1)
6996 : {
6997 39 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6998 39 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6999 : tf_warning_or_error);
7000 : }
7001 : /* If the list had more than one element, the code is ill-formed
7002 : pre-C++20, so we can build a constructor right away. */
7003 : else
7004 21 : init = do_aggregate_paren_init (init, ttype);
7005 : }
7006 : else
7007 1101 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7008 : tf_warning_or_error);
7009 : }
7010 :
7011 512447 : if (TREE_CODE (ttype) != ARRAY_TYPE
7012 512447 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7013 : /* Note: default conversion is only called in very special cases. */
7014 12 : init = decay_conversion (init, tf_warning_or_error);
7015 :
7016 : /* check_initializer handles this for non-reference variables, but for
7017 : references we need to do it here or the initializer will get the
7018 : incomplete array type and confuse later calls to
7019 : cp_complete_array_type. */
7020 512447 : if (TREE_CODE (ttype) == ARRAY_TYPE
7021 22339 : && TYPE_DOMAIN (ttype) == NULL_TREE
7022 512530 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
7023 50 : || TREE_CODE (init) == STRING_CST))
7024 : {
7025 33 : cp_complete_array_type (&ttype, init, false);
7026 33 : if (ttype != TREE_TYPE (type))
7027 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
7028 : }
7029 :
7030 : /* Convert INIT to the reference type TYPE. This may involve the
7031 : creation of a temporary, whose lifetime must be the same as that
7032 : of the reference. If so, a DECL_EXPR for the temporary will be
7033 : added just after the DECL_EXPR for DECL. That's why we don't set
7034 : DECL_INITIAL for local references (instead assigning to them
7035 : explicitly); we need to allow the temporary to be initialized
7036 : first. */
7037 512447 : return initialize_reference (type, init, flags,
7038 512447 : tf_warning_or_error);
7039 : }
7040 :
7041 : /* Designated initializers in arrays are not supported in GNU C++.
7042 : The parser cannot detect this error since it does not know whether
7043 : a given brace-enclosed initializer is for a class type or for an
7044 : array. This function checks that CE does not use a designated
7045 : initializer. If it does, an error is issued. Returns true if CE
7046 : is valid, i.e., does not have a designated initializer. */
7047 :
7048 : bool
7049 147277660 : check_array_designated_initializer (constructor_elt *ce,
7050 : unsigned HOST_WIDE_INT index)
7051 : {
7052 : /* Designated initializers for array elements are not supported. */
7053 147277660 : if (ce->index)
7054 : {
7055 : /* The parser only allows identifiers as designated
7056 : initializers. */
7057 99370244 : if (ce->index == error_mark_node)
7058 : {
7059 0 : error ("name used in a GNU-style designated "
7060 : "initializer for an array");
7061 0 : return false;
7062 : }
7063 99370244 : else if (identifier_p (ce->index))
7064 : {
7065 19 : error ("name %qD used in a GNU-style designated "
7066 : "initializer for an array", ce->index);
7067 19 : ce->index = error_mark_node;
7068 19 : return false;
7069 : }
7070 :
7071 99370225 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7072 : ce->index, true);
7073 99370225 : if (ce_index
7074 99370222 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
7075 198740444 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
7076 : == INTEGER_CST))
7077 : {
7078 : /* A C99 designator is OK if it matches the current index. */
7079 99370219 : if (wi::to_wide (ce_index) == index)
7080 : {
7081 99370213 : ce->index = ce_index;
7082 99370213 : return true;
7083 : }
7084 : else
7085 6 : sorry ("non-trivial designated initializers not supported");
7086 : }
7087 : else
7088 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
7089 : "C99 designator %qE is not an integral constant-expression",
7090 : ce->index);
7091 :
7092 12 : return false;
7093 : }
7094 :
7095 : return true;
7096 : }
7097 :
7098 : /* When parsing `int a[] = {1, 2};' we don't know the size of the
7099 : array until we finish parsing the initializer. If that's the
7100 : situation we're in, update DECL accordingly. */
7101 :
7102 : static void
7103 82884183 : maybe_deduce_size_from_array_init (tree decl, tree init)
7104 : {
7105 82884183 : tree type = TREE_TYPE (decl);
7106 :
7107 82884183 : if (TREE_CODE (type) == ARRAY_TYPE
7108 1003779 : && TYPE_DOMAIN (type) == NULL_TREE
7109 83543419 : && TREE_CODE (decl) != TYPE_DECL)
7110 : {
7111 : /* do_default is really a C-ism to deal with tentative definitions.
7112 : But let's leave it here to ease the eventual merge. */
7113 659236 : int do_default = !DECL_EXTERNAL (decl);
7114 659236 : tree initializer = init ? init : DECL_INITIAL (decl);
7115 659236 : int failure = 0;
7116 :
7117 : /* Check that there are no designated initializers in INIT, as
7118 : those are not supported in GNU C++, and as the middle-end
7119 : will crash if presented with a non-numeric designated
7120 : initializer. */
7121 659236 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7122 : {
7123 140161 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7124 140161 : constructor_elt *ce;
7125 140161 : HOST_WIDE_INT i, j = 0;
7126 46169127 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7127 : {
7128 46029696 : if (instantiation_dependent_expression_p (ce->index))
7129 82884183 : return;
7130 46029690 : if (!check_array_designated_initializer (ce, j))
7131 1 : failure = 1;
7132 : /* If an un-designated initializer is type-dependent, we can't
7133 : check brace elision yet. */
7134 46029690 : if (ce->index == NULL_TREE
7135 46029690 : && type_dependent_expression_p (ce->value))
7136 : return;
7137 46028966 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7138 197 : j += RAW_DATA_LENGTH (ce->value);
7139 : else
7140 46028769 : ++j;
7141 : }
7142 : }
7143 :
7144 139431 : if (failure)
7145 1 : TREE_TYPE (decl) = error_mark_node;
7146 : else
7147 : {
7148 658505 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7149 : do_default);
7150 658505 : if (failure == 1)
7151 : {
7152 33 : error_at (cp_expr_loc_or_loc (initializer,
7153 30 : DECL_SOURCE_LOCATION (decl)),
7154 : "initializer fails to determine size of %qD", decl);
7155 : }
7156 658475 : else if (failure == 2)
7157 : {
7158 30 : if (do_default)
7159 : {
7160 6 : error_at (DECL_SOURCE_LOCATION (decl),
7161 : "array size missing in %qD", decl);
7162 : }
7163 : /* If a `static' var's size isn't known, make it extern as
7164 : well as static, so it does not get allocated. If it's not
7165 : `static', then don't mark it extern; finish_incomplete_decl
7166 : will give it a default size and it will get allocated. */
7167 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7168 0 : DECL_EXTERNAL (decl) = 1;
7169 : }
7170 658445 : else if (failure == 3)
7171 : {
7172 6 : error_at (DECL_SOURCE_LOCATION (decl),
7173 : "zero-size array %qD", decl);
7174 : }
7175 : }
7176 :
7177 658506 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7178 :
7179 658506 : relayout_decl (decl);
7180 :
7181 : /* Update the type of the corresponding TEMPLATE_DECL to match. */
7182 658506 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7183 : {
7184 208 : tree tmpl = template_for_substitution (decl);
7185 208 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
7186 50 : TREE_TYPE (tmpl) = TREE_TYPE (decl);
7187 : }
7188 : }
7189 : }
7190 :
7191 : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7192 : any appropriate error messages regarding the layout. */
7193 :
7194 : static void
7195 73683481 : layout_var_decl (tree decl)
7196 : {
7197 73683481 : tree type;
7198 :
7199 73683481 : type = TREE_TYPE (decl);
7200 73683481 : if (type == error_mark_node)
7201 : return;
7202 :
7203 : /* If we haven't already laid out this declaration, do so now.
7204 : Note that we must not call complete type for an external object
7205 : because it's type might involve templates that we are not
7206 : supposed to instantiate yet. (And it's perfectly valid to say
7207 : `extern X x' for some incomplete type `X'.) */
7208 73683434 : if (!DECL_EXTERNAL (decl))
7209 63342044 : complete_type (type);
7210 73683434 : if (!DECL_SIZE (decl)
7211 594663 : && TREE_TYPE (decl) != error_mark_node
7212 74278097 : && complete_or_array_type_p (type))
7213 453349 : layout_decl (decl, 0);
7214 :
7215 73683434 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7216 : {
7217 : /* An automatic variable with an incomplete type: that is an error.
7218 : Don't talk about array types here, since we took care of that
7219 : message in grokdeclarator. */
7220 17 : error_at (DECL_SOURCE_LOCATION (decl),
7221 : "storage size of %qD isn%'t known", decl);
7222 17 : TREE_TYPE (decl) = error_mark_node;
7223 : }
7224 : #if 0
7225 : /* Keep this code around in case we later want to control debug info
7226 : based on whether a type is "used". (jason 1999-11-11) */
7227 :
7228 : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7229 : /* Let debugger know it should output info for this type. */
7230 : note_debug_info_needed (ttype);
7231 :
7232 : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7233 : note_debug_info_needed (DECL_CONTEXT (decl));
7234 : #endif
7235 :
7236 137025478 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7237 42644063 : && DECL_SIZE (decl) != NULL_TREE
7238 116059219 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
7239 : {
7240 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7241 6 : && !DECL_LOCAL_DECL_P (decl))
7242 0 : constant_expression_warning (DECL_SIZE (decl));
7243 : else
7244 : {
7245 6 : error_at (DECL_SOURCE_LOCATION (decl),
7246 : "storage size of %qD isn%'t constant", decl);
7247 6 : TREE_TYPE (decl) = error_mark_node;
7248 6 : type = error_mark_node;
7249 : }
7250 : }
7251 :
7252 : /* If the final element initializes a flexible array field, adjust
7253 : the size of the DECL with the initializer based on whether the
7254 : DECL is a union or a structure. */
7255 73683434 : if (type != error_mark_node
7256 73683428 : && DECL_INITIAL (decl)
7257 42601418 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7258 5956175 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7259 4120309 : && DECL_SIZE (decl) != NULL_TREE
7260 4120309 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7261 4120309 : && COMPLETE_TYPE_P (type)
7262 4120309 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7263 77803743 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7264 : {
7265 4120309 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7266 4120309 : if (elt.index)
7267 : {
7268 1913433 : tree itype = TREE_TYPE (elt.index);
7269 1913433 : tree vtype = TREE_TYPE (elt.value);
7270 1913433 : if (TREE_CODE (itype) == ARRAY_TYPE
7271 79472 : && TYPE_DOMAIN (itype) == NULL
7272 275 : && TREE_CODE (vtype) == ARRAY_TYPE
7273 1913708 : && COMPLETE_TYPE_P (vtype))
7274 : {
7275 : /* For a structure, add the size of the initializer to the DECL's
7276 : size. */
7277 275 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7278 : {
7279 260 : DECL_SIZE (decl)
7280 260 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7281 : TYPE_SIZE (vtype));
7282 260 : DECL_SIZE_UNIT (decl)
7283 520 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7284 : TYPE_SIZE_UNIT (vtype));
7285 : }
7286 : /* For a union, the DECL's size is the maximum of the current size
7287 : and the size of the initializer. */
7288 : else
7289 : {
7290 15 : DECL_SIZE (decl)
7291 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
7292 : TYPE_SIZE (vtype));
7293 15 : DECL_SIZE_UNIT (decl)
7294 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7295 : TYPE_SIZE_UNIT (vtype));
7296 : }
7297 : }
7298 : }
7299 : }
7300 : }
7301 :
7302 : /* If a local static variable is declared in an inline function, or if
7303 : we have a weak definition, we must endeavor to create only one
7304 : instance of the variable at link-time. */
7305 :
7306 : void
7307 92337262 : maybe_commonize_var (tree decl)
7308 : {
7309 : /* Don't mess with __FUNCTION__ and similar. But do handle structured
7310 : bindings. */
7311 92337262 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7312 : return;
7313 :
7314 : /* Static data in a function with comdat linkage also has comdat
7315 : linkage. */
7316 86885772 : if ((TREE_STATIC (decl)
7317 55645843 : && DECL_FUNCTION_SCOPE_P (decl)
7318 167599 : && vague_linkage_p (DECL_CONTEXT (decl)))
7319 198291637 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7320 : {
7321 49049912 : if (flag_weak)
7322 : {
7323 : /* With weak symbols, we simply make the variable COMDAT;
7324 : that will cause copies in multiple translations units to
7325 : be merged. */
7326 49049885 : comdat_linkage (decl);
7327 : }
7328 : else
7329 : {
7330 27 : if (DECL_INITIAL (decl) == NULL_TREE
7331 27 : || DECL_INITIAL (decl) == error_mark_node)
7332 : {
7333 : /* Without weak symbols, we can use COMMON to merge
7334 : uninitialized variables. */
7335 24 : TREE_PUBLIC (decl) = 1;
7336 24 : DECL_COMMON (decl) = 1;
7337 : }
7338 : else
7339 : {
7340 : /* While for initialized variables, we must use internal
7341 : linkage -- which means that multiple copies will not
7342 : be merged. */
7343 3 : TREE_PUBLIC (decl) = 0;
7344 3 : DECL_COMMON (decl) = 0;
7345 3 : DECL_INTERFACE_KNOWN (decl) = 1;
7346 3 : const char *msg;
7347 3 : if (DECL_INLINE_VAR_P (decl))
7348 : msg = G_("sorry: semantics of inline variable "
7349 : "%q#D are wrong (you%'ll wind up with "
7350 : "multiple copies)");
7351 : else
7352 : msg = G_("sorry: semantics of inline function "
7353 : "static data %q#D are wrong (you%'ll wind "
7354 : "up with multiple copies)");
7355 3 : auto_diagnostic_group d;
7356 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7357 : msg, decl))
7358 3 : inform (DECL_SOURCE_LOCATION (decl),
7359 : "you can work around this by removing the initializer");
7360 3 : }
7361 : }
7362 : }
7363 : }
7364 :
7365 : /* Issue an error message if DECL is an uninitialized const variable.
7366 : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7367 : context from potential_constant_expression. Returns true if all is well,
7368 : false otherwise. */
7369 :
7370 : bool
7371 11862765 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7372 : tsubst_flags_t complain)
7373 : {
7374 11862765 : tree type = strip_array_types (TREE_TYPE (decl));
7375 :
7376 : /* ``Unless explicitly declared extern, a const object does not have
7377 : external linkage and must be initialized. ($8.4; $12.1)'' ARM
7378 : 7.1.6 */
7379 11862765 : if (VAR_P (decl)
7380 11862765 : && !TYPE_REF_P (type)
7381 11803119 : && (CP_TYPE_CONST_P (type)
7382 : /* C++20 permits trivial default initialization in constexpr
7383 : context (P1331R2). */
7384 9566765 : || (cxx_dialect < cxx20
7385 136056 : && (constexpr_context_p
7386 122761 : || var_in_constexpr_fn (decl))))
7387 14114493 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7388 : {
7389 620721 : tree field = default_init_uninitialized_part (type);
7390 620721 : if (!field)
7391 : return true;
7392 :
7393 3899 : auto_diagnostic_group d;
7394 3899 : bool show_notes = true;
7395 :
7396 3899 : if (!constexpr_context_p || cxx_dialect >= cxx20)
7397 : {
7398 173 : if (CP_TYPE_CONST_P (type))
7399 : {
7400 90 : if (complain & tf_error)
7401 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7402 : "uninitialized %<const %D%>", decl);
7403 : }
7404 : else
7405 : {
7406 83 : if (!is_instantiation_of_constexpr (current_function_decl)
7407 83 : && (complain & tf_error))
7408 21 : error_at (DECL_SOURCE_LOCATION (decl),
7409 : "uninitialized variable %qD in %<constexpr%> "
7410 : "function", decl);
7411 : else
7412 : show_notes = false;
7413 83 : cp_function_chain->invalid_constexpr = true;
7414 : }
7415 : }
7416 3726 : else if (complain & tf_error)
7417 2 : error_at (DECL_SOURCE_LOCATION (decl),
7418 : "uninitialized variable %qD in %<constexpr%> context",
7419 : decl);
7420 :
7421 3899 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7422 : {
7423 62 : tree defaulted_ctor;
7424 :
7425 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7426 : "%q#T has no user-provided default constructor", type);
7427 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
7428 62 : if (defaulted_ctor)
7429 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7430 : "constructor is not user-provided because it is "
7431 : "explicitly defaulted in the class body");
7432 62 : inform (DECL_SOURCE_LOCATION (field),
7433 : "and the implicitly-defined constructor does not "
7434 : "initialize %q#D", field);
7435 : }
7436 :
7437 3899 : return false;
7438 3899 : }
7439 :
7440 : return true;
7441 : }
7442 :
7443 : /* Structure holding the current initializer being processed by reshape_init.
7444 : CUR is a pointer to the current element being processed, END is a pointer
7445 : after the last element present in the initializer and RAW_IDX is index into
7446 : RAW_DATA_CST if that is CUR elt. */
7447 : struct reshape_iter
7448 : {
7449 : constructor_elt *cur;
7450 : constructor_elt *end;
7451 : unsigned raw_idx;
7452 : };
7453 :
7454 : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7455 :
7456 : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7457 : returned is the next FIELD_DECL (possibly FIELD itself) that can be
7458 : initialized as if for an aggregate class. If there are no more such fields,
7459 : the return value will be NULL. */
7460 :
7461 : tree
7462 16354374 : next_aggregate_field (tree field)
7463 : {
7464 16354374 : while (field
7465 65691788 : && (TREE_CODE (field) != FIELD_DECL
7466 10692538 : || DECL_UNNAMED_BIT_FIELD (field)
7467 10692383 : || (DECL_ARTIFICIAL (field)
7468 : /* In C++17, aggregates can have bases. */
7469 177199 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7470 49337414 : field = DECL_CHAIN (field);
7471 :
7472 16354374 : return field;
7473 : }
7474 :
7475 : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7476 : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7477 : to a subobject. If there are no more such fields, the return value will be
7478 : NULL. */
7479 :
7480 : tree
7481 76285338 : next_subobject_field (tree field)
7482 : {
7483 76285338 : while (field
7484 973744829 : && (TREE_CODE (field) != FIELD_DECL
7485 36354508 : || DECL_UNNAMED_BIT_FIELD (field)
7486 36354376 : || (DECL_ARTIFICIAL (field)
7487 8683657 : && !DECL_FIELD_IS_BASE (field)
7488 1817269 : && !DECL_VIRTUAL_P (field))))
7489 897459491 : field = DECL_CHAIN (field);
7490 :
7491 76285338 : return field;
7492 : }
7493 :
7494 : /* Return true for [dcl.init.list] direct-list-initialization from
7495 : single element of enumeration with a fixed underlying type. */
7496 :
7497 : bool
7498 197430906 : is_direct_enum_init (tree type, tree init)
7499 : {
7500 197430906 : if (cxx_dialect >= cxx17
7501 195405024 : && TREE_CODE (type) == ENUMERAL_TYPE
7502 8652470 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7503 6330664 : && TREE_CODE (init) == CONSTRUCTOR
7504 528237 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
7505 324906 : && CONSTRUCTOR_NELTS (init) == 1
7506 216 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7507 : /* DR 2374: The single element needs to be implicitly
7508 : convertible to the underlying type of the enum. */
7509 216 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7510 197431120 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7511 214 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7512 214 : CONSTRUCTOR_ELT (init, 0)->value,
7513 : LOOKUP_IMPLICIT, tf_none))
7514 : return true;
7515 : return false;
7516 : }
7517 :
7518 : /* Helper function for reshape_init*. Split first element of
7519 : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7520 : to true if the whole d->cur has been consumed. */
7521 :
7522 : static tree
7523 59929997 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7524 : {
7525 59929997 : *inc_cur = true;
7526 59929997 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7527 : return NULL_TREE;
7528 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7529 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7530 30588 : *inc_cur = false;
7531 : else
7532 132 : d->raw_idx = 0;
7533 : return ret;
7534 : }
7535 :
7536 : /* Wrapper around that which for RAW_DATA_CST in INIT
7537 : (as well as in D->cur->value) peels off the first element
7538 : of the raw data and returns it, otherwise increments
7539 : D->cur and returns INIT. */
7540 :
7541 : static tree
7542 59929981 : consume_init (tree init, reshape_iter *d)
7543 : {
7544 59929981 : bool inc_cur;
7545 59929981 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7546 30720 : init = raw_init;
7547 59929981 : if (inc_cur)
7548 59899393 : d->cur++;
7549 59929981 : return init;
7550 : }
7551 :
7552 : /* Subroutine of reshape_init_array and reshape_init_vector, which does
7553 : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7554 : INTEGER_CST representing the size of the array minus one (the maximum index),
7555 : or NULL_TREE if the array was declared without specifying the size. D is
7556 : the iterator within the constructor. */
7557 :
7558 : static tree
7559 470719 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7560 : tree first_initializer_p, bool vector_p,
7561 : tsubst_flags_t complain)
7562 : {
7563 470719 : tree new_init;
7564 470719 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7565 470719 : unsigned HOST_WIDE_INT max_index_cst = 0;
7566 470719 : unsigned HOST_WIDE_INT index;
7567 :
7568 : /* The initializer for an array is always a CONSTRUCTOR. If this is the
7569 : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7570 : to build a new one. But don't reuse if not complaining; if this is
7571 : tentative, we might also reshape to another type (95319). */
7572 470719 : bool reuse = (first_initializer_p
7573 415877 : && (complain & tf_error)
7574 414631 : && !CP_AGGREGATE_TYPE_P (elt_type)
7575 824104 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7576 121871 : if (reuse)
7577 : new_init = first_initializer_p;
7578 : else
7579 121871 : new_init = build_constructor (init_list_type_node, NULL);
7580 :
7581 470719 : if (sized_array_p)
7582 : {
7583 291033 : poly_uint64 midx;
7584 : /* Minus 1 is used for zero sized arrays. */
7585 291033 : if (integer_all_onesp (max_index))
7586 24 : return new_init;
7587 :
7588 291009 : if (tree_fits_poly_uint64_p (max_index))
7589 291009 : midx = tree_to_poly_uint64 (max_index);
7590 : /* sizetype is sign extended, not zero extended. */
7591 : else
7592 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7593 :
7594 : /* For VLA vectors, we restict the number of elements in the constructor
7595 : to lower bound of the VLA elements. */
7596 291009 : max_index_cst = constant_lower_bound (midx);
7597 : }
7598 :
7599 470695 : constructor_elt *first_cur = d->cur;
7600 : /* Loop until there are no more initializers. */
7601 53868148 : for (index = 0;
7602 53868148 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7603 : ++index)
7604 : {
7605 53397633 : tree elt_init;
7606 53397633 : constructor_elt *old_cur = d->cur;
7607 53397633 : unsigned int old_raw_idx = d->raw_idx;
7608 53397633 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7609 :
7610 53397633 : if (d->cur->index)
7611 5511194 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7612 53397633 : check_array_designated_initializer (d->cur, index);
7613 53397633 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7614 32708 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7615 10233 : || is_byte_access_type (elt_type))
7616 22475 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7617 1814 : && (!sized_array_p || index < max_index_cst)
7618 53399435 : && !vector_p)
7619 : {
7620 482 : elt_init = d->cur->value;
7621 482 : unsigned int off = d->raw_idx;
7622 482 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7623 482 : if (!sized_array_p || len <= max_index_cst - index + 1)
7624 : {
7625 473 : d->cur++;
7626 473 : d->raw_idx = 0;
7627 : }
7628 : else
7629 : {
7630 9 : len = max_index_cst - index + 1;
7631 9 : d->raw_idx += len;
7632 : }
7633 482 : if (!reuse || off || d->cur == old_cur)
7634 : {
7635 27 : elt_init = copy_node (elt_init);
7636 27 : RAW_DATA_LENGTH (elt_init) = len;
7637 27 : RAW_DATA_POINTER (elt_init) += off;
7638 : }
7639 482 : TREE_TYPE (elt_init) = elt_type;
7640 : }
7641 : else
7642 53397151 : elt_init = reshape_init_r (elt_type, d,
7643 : /*first_initializer_p=*/NULL_TREE,
7644 : complain);
7645 53397633 : if (elt_init == error_mark_node)
7646 : return error_mark_node;
7647 53397456 : tree idx = size_int (index);
7648 53397456 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7649 : {
7650 : /* We need to stop reusing as some RAW_DATA_CST in the original
7651 : ctor had to be split. */
7652 72 : new_init = build_constructor (init_list_type_node, NULL);
7653 72 : if (index)
7654 : {
7655 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7656 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7657 72 : (d->cur - first_cur)
7658 : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7659 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7660 : {
7661 : unsigned int j;
7662 : tree nidx, nval;
7663 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7664 : j, nidx, nval)
7665 4 : if (nidx)
7666 : {
7667 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7668 4 : (void) nval;
7669 4 : break;
7670 : }
7671 : }
7672 : }
7673 : reuse = false;
7674 : }
7675 53397384 : if (reuse)
7676 : {
7677 52225750 : old_cur->index = idx;
7678 52225750 : old_cur->value = elt_init;
7679 : }
7680 : else
7681 1171706 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7682 : idx, elt_init);
7683 53397456 : if (!TREE_CONSTANT (elt_init))
7684 201957 : TREE_CONSTANT (new_init) = false;
7685 :
7686 : /* This can happen with an invalid initializer (c++/54501). */
7687 53397456 : if (d->cur == old_cur
7688 32082 : && !sized_array_p
7689 14085 : && d->raw_idx == old_raw_idx)
7690 : break;
7691 :
7692 53397453 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7693 482 : index += RAW_DATA_LENGTH (elt_init) - 1;
7694 : }
7695 :
7696 : return new_init;
7697 : }
7698 :
7699 : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7700 : Parameters are the same of reshape_init_r. */
7701 :
7702 : static tree
7703 422768 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7704 : tsubst_flags_t complain)
7705 : {
7706 422768 : tree max_index = NULL_TREE;
7707 :
7708 422768 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7709 :
7710 422768 : if (TYPE_DOMAIN (type))
7711 243130 : max_index = array_type_nelts_minus_one (type);
7712 :
7713 422768 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7714 422768 : first_initializer_p, false, complain);
7715 : }
7716 :
7717 : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7718 : Parameters are the same of reshape_init_r. */
7719 :
7720 : static tree
7721 47951 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7722 : {
7723 47951 : tree max_index = NULL_TREE;
7724 :
7725 47951 : gcc_assert (VECTOR_TYPE_P (type));
7726 :
7727 47951 : if (COMPOUND_LITERAL_P (d->cur->value))
7728 : {
7729 0 : tree value = d->cur->value;
7730 0 : if (!same_type_p (TREE_TYPE (value), type))
7731 : {
7732 0 : if (complain & tf_error)
7733 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7734 0 : TREE_TYPE (d->cur->value), type);
7735 0 : value = error_mark_node;
7736 : }
7737 0 : ++d->cur;
7738 0 : return value;
7739 : }
7740 :
7741 : /* For a vector, we initialize it as an array of the appropriate size. */
7742 47951 : if (VECTOR_TYPE_P (type))
7743 47951 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7744 :
7745 47951 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7746 47951 : NULL_TREE, true, complain);
7747 : }
7748 :
7749 : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7750 : INIT, in isolation from any designator or other initializers. */
7751 :
7752 : static tree
7753 1448102 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7754 : {
7755 : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7756 : calling reshape_init, but this way can just live on the stack. */
7757 1448102 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7758 1448102 : reshape_iter iter = { &elt, &elt + 1, 0 };
7759 1448102 : return reshape_init_r (type, &iter,
7760 : /*first_initializer_p=*/NULL_TREE,
7761 1448102 : complain);
7762 : }
7763 :
7764 : /* Subroutine of reshape_init_r, processes the initializers for classes
7765 : or union. Parameters are the same of reshape_init_r. */
7766 :
7767 : static tree
7768 3725435 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7769 : tsubst_flags_t complain)
7770 : {
7771 3725435 : tree field;
7772 3725435 : tree new_init;
7773 :
7774 3725435 : gcc_assert (CLASS_TYPE_P (type));
7775 :
7776 : /* The initializer for a class is always a CONSTRUCTOR. */
7777 3725435 : new_init = build_constructor (init_list_type_node, NULL);
7778 :
7779 3725435 : int binfo_idx = -1;
7780 3725435 : tree binfo = TYPE_BINFO (type);
7781 3725435 : tree base_binfo = NULL_TREE;
7782 3725435 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7783 : {
7784 : /* We get here from maybe_aggr_guide for C++20 class template argument
7785 : deduction. In this case we need to look through the binfo because a
7786 : template doesn't have base fields. */
7787 1193 : binfo_idx = 0;
7788 1193 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7789 : }
7790 67 : if (base_binfo)
7791 : field = base_binfo;
7792 : else
7793 3725368 : field = next_aggregate_field (TYPE_FIELDS (type));
7794 :
7795 3725435 : if (!field)
7796 : {
7797 : /* [dcl.init.aggr]
7798 :
7799 : An initializer for an aggregate member that is an
7800 : empty class shall have the form of an empty
7801 : initializer-list {}. */
7802 322 : if (!first_initializer_p)
7803 : {
7804 268 : if (complain & tf_error)
7805 13 : error ("initializer for %qT must be brace-enclosed", type);
7806 268 : return error_mark_node;
7807 : }
7808 : return new_init;
7809 : }
7810 :
7811 : /* For C++20 CTAD, handle pack expansions in the base list. */
7812 : tree last_was_pack_expansion = NULL_TREE;
7813 :
7814 : /* Loop through the initializable fields, gathering initializers. */
7815 11438636 : while (d->cur != d->end)
7816 : {
7817 7817064 : tree field_init;
7818 7817064 : constructor_elt *old_cur = d->cur;
7819 7817064 : unsigned old_raw_idx = d->raw_idx;
7820 7817064 : bool direct_desig = false;
7821 :
7822 : /* Handle C++20 designated initializers. */
7823 7817064 : if (d->cur->index)
7824 : {
7825 1448268 : if (d->cur->index == error_mark_node)
7826 : return error_mark_node;
7827 :
7828 1448262 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7829 : {
7830 : /* We already reshaped this; we should have returned early from
7831 : reshape_init. */
7832 0 : gcc_checking_assert (false);
7833 : if (field != d->cur->index)
7834 : {
7835 : if (tree id = DECL_NAME (d->cur->index))
7836 : gcc_checking_assert (d->cur->index
7837 : == get_class_binding (type, id));
7838 : field = d->cur->index;
7839 : }
7840 : }
7841 1448262 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7842 : {
7843 1448256 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7844 1448256 : field = get_class_binding (type, d->cur->index);
7845 1448256 : direct_desig = true;
7846 : }
7847 : else
7848 : {
7849 6 : if (complain & tf_error)
7850 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7851 : " for class %qT", d->cur->index, type);
7852 6 : return error_mark_node;
7853 : }
7854 :
7855 1448256 : if (!field && ANON_AGGR_TYPE_P (type))
7856 : /* Apparently the designator isn't for a member of this anonymous
7857 : struct, so head back to the enclosing class. */
7858 : break;
7859 :
7860 1448253 : if (!field || TREE_CODE (field) != FIELD_DECL)
7861 : {
7862 18 : if (complain & tf_error)
7863 : {
7864 6 : if (field && TREE_CODE (field) == TREE_LIST)
7865 : {
7866 3 : auto_diagnostic_group g;
7867 3 : error ("request for member %qD is ambiguous",
7868 3 : d->cur->index);
7869 3 : print_candidates (input_location, field);
7870 3 : }
7871 : else
7872 3 : error ("%qT has no non-static data member named %qD", type,
7873 3 : d->cur->index);
7874 : }
7875 18 : return error_mark_node;
7876 : }
7877 :
7878 : /* If the element is an anonymous union object and the initializer
7879 : list is a designated-initializer-list, the anonymous union object
7880 : is initialized by the designated-initializer-list { D }, where D
7881 : is the designated-initializer-clause naming a member of the
7882 : anonymous union object. */
7883 1448235 : tree ictx = DECL_CONTEXT (field);
7884 1448235 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7885 : {
7886 : /* Find the anon aggr that is a direct member of TYPE. */
7887 146 : while (ANON_AGGR_TYPE_P (ictx))
7888 : {
7889 143 : tree cctx = TYPE_CONTEXT (ictx);
7890 143 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7891 130 : goto found;
7892 : ictx = cctx;
7893 : }
7894 :
7895 : /* Not found, e.g. FIELD is a member of a base class. */
7896 3 : if (complain & tf_error)
7897 3 : error ("%qD is not a direct member of %qT", field, type);
7898 3 : return error_mark_node;
7899 :
7900 130 : found:
7901 : /* Now find the TYPE member with that anon aggr type. */
7902 130 : tree aafield = TYPE_FIELDS (type);
7903 1110 : for (; aafield; aafield = TREE_CHAIN (aafield))
7904 1110 : if (TREE_TYPE (aafield) == ictx)
7905 : break;
7906 0 : gcc_assert (aafield);
7907 : field = aafield;
7908 : direct_desig = false;
7909 : }
7910 : }
7911 :
7912 : /* If we processed all the member of the class, we are done. */
7913 7816898 : if (!field)
7914 : break;
7915 :
7916 15633135 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7917 7816577 : ? field : NULL_TREE);
7918 7816577 : if (last_was_pack_expansion)
7919 : /* Each non-trailing aggregate element that is a pack expansion is
7920 : assumed to correspond to no elements of the initializer list. */
7921 19 : goto continue_;
7922 :
7923 7816558 : if (direct_desig)
7924 : {
7925 : /* The designated field F is initialized from this one element.
7926 :
7927 : Note that we don't want to do this if we found the designator
7928 : inside an anon aggr above; we use the normal code to implement:
7929 :
7930 : "If the element is an anonymous union member and the initializer
7931 : list is a brace-enclosed designated- initializer-list, the element
7932 : is initialized by the designated-initializer-list { D }, where D
7933 : is the designated- initializer-clause naming a member of the
7934 : anonymous union member." */
7935 1448102 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7936 1448102 : field_init = reshape_single_init (TREE_TYPE (field),
7937 : d->cur->value, complain);
7938 1448102 : d->cur++;
7939 : }
7940 : else
7941 6368456 : field_init = reshape_init_r (TREE_TYPE (field), d,
7942 : /*first_initializer_p=*/NULL_TREE,
7943 : complain);
7944 :
7945 7816558 : if (field_init == error_mark_node)
7946 : return error_mark_node;
7947 :
7948 7816271 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7949 : {
7950 : /* This can happen with an invalid initializer for a flexible
7951 : array member (c++/54441). */
7952 0 : if (complain & tf_error)
7953 0 : error ("invalid initializer for %q#D", field);
7954 0 : return error_mark_node;
7955 : }
7956 :
7957 7816271 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7958 :
7959 : /* [dcl.init.aggr]
7960 :
7961 : When a union is initialized with a brace-enclosed
7962 : initializer, the braces shall only contain an
7963 : initializer for the first member of the union. */
7964 7816271 : if (TREE_CODE (type) == UNION_TYPE)
7965 : break;
7966 :
7967 7713504 : continue_:
7968 7713523 : if (base_binfo)
7969 : {
7970 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7971 : field = base_binfo;
7972 : else
7973 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7974 : }
7975 : else
7976 7713441 : field = next_aggregate_field (DECL_CHAIN (field));
7977 : }
7978 :
7979 : /* A trailing aggregate element that is a pack expansion is assumed to
7980 : correspond to all remaining elements of the initializer list (if any). */
7981 3724793 : if (last_was_pack_expansion)
7982 : {
7983 16 : tree init = d->cur->value;
7984 16 : bool inc_cur;
7985 16 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7986 0 : init = raw_init;
7987 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7988 : last_was_pack_expansion, init);
7989 46 : while (d->cur != d->end)
7990 30 : d->cur++;
7991 : }
7992 :
7993 : return new_init;
7994 : }
7995 :
7996 : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7997 : designators are not valid; either complain or return true to indicate
7998 : that reshape_init_r should return error_mark_node. */
7999 :
8000 : static bool
8001 238455 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
8002 : {
8003 238455 : if (d->cur->index)
8004 : {
8005 30 : if (complain & tf_error)
8006 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
8007 : "C99 designator %qE outside aggregate initializer",
8008 : d->cur->index);
8009 : else
8010 : return true;
8011 : }
8012 : return false;
8013 : }
8014 :
8015 : /* Subroutine of reshape_init, which processes a single initializer (part of
8016 : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
8017 : iterator within the CONSTRUCTOR which points to the initializer to process.
8018 : If this is the first initializer of the outermost CONSTRUCTOR node,
8019 : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
8020 :
8021 : static tree
8022 65640466 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
8023 : tsubst_flags_t complain)
8024 : {
8025 65640466 : tree init = d->cur->value;
8026 :
8027 65640466 : if (error_operand_p (init))
8028 126 : return error_mark_node;
8029 :
8030 4426634 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
8031 65878616 : && has_designator_problem (d, complain))
8032 0 : return error_mark_node;
8033 :
8034 65640340 : tree stripped_init = tree_strip_any_location_wrapper (init);
8035 :
8036 65640340 : if (TREE_CODE (type) == COMPLEX_TYPE)
8037 : {
8038 : /* A complex type can be initialized from one or two initializers,
8039 : but braces are not elided. */
8040 678 : init = consume_init (init, d);
8041 678 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
8042 : {
8043 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
8044 : {
8045 0 : if (complain & tf_error)
8046 0 : error ("too many initializers for %qT", type);
8047 : else
8048 0 : return error_mark_node;
8049 : }
8050 : }
8051 669 : else if (first_initializer_p && d->cur != d->end)
8052 : {
8053 36 : if (error_operand_p (d->cur->value)
8054 36 : || has_designator_problem (d, complain))
8055 0 : return error_mark_node;
8056 36 : vec<constructor_elt, va_gc> *v = 0;
8057 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8058 36 : init = consume_init (d->cur->value, d);
8059 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8060 36 : init = build_constructor (init_list_type_node, v);
8061 : }
8062 678 : return init;
8063 : }
8064 :
8065 : /* A non-aggregate type is always initialized with a single
8066 : initializer. */
8067 131222569 : if (!CP_AGGREGATE_TYPE_P (type)
8068 : /* As is an array with dependent bound, which we can see
8069 : during C++20 aggregate CTAD. */
8070 71297487 : || (cxx_dialect >= cxx20
8071 5609712 : && TREE_CODE (type) == ARRAY_TYPE
8072 512886 : && uses_template_parms (TYPE_DOMAIN (type))))
8073 : {
8074 : /* It is invalid to initialize a non-aggregate type with a
8075 : brace-enclosed initializer before C++0x.
8076 : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
8077 : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
8078 : a CONSTRUCTOR (with a record type). */
8079 59925093 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
8080 : /* Don't complain about a capture-init. */
8081 290585 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
8082 60205916 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
8083 : {
8084 280784 : if (SCALAR_TYPE_P (type))
8085 : {
8086 477 : if (cxx_dialect < cxx11)
8087 : {
8088 16 : if (complain & tf_error)
8089 16 : error ("braces around scalar initializer for type %qT",
8090 : type);
8091 16 : init = error_mark_node;
8092 : }
8093 461 : else if (first_initializer_p
8094 461 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
8095 296 : && (BRACE_ENCLOSED_INITIALIZER_P
8096 : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8097 : {
8098 13 : if (complain & tf_error)
8099 13 : error ("too many braces around scalar initializer "
8100 : "for type %qT", type);
8101 13 : init = error_mark_node;
8102 : }
8103 : }
8104 : else
8105 280307 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8106 : }
8107 59925093 : return consume_init (init, d);
8108 : }
8109 :
8110 : /* "If T is a class type and the initializer list has a single element of
8111 : type cv U, where U is T or a class derived from T, the object is
8112 : initialized from that element." Even if T is an aggregate. */
8113 5690693 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8114 5152297 : && first_initializer_p
8115 : /* But not if it's a designated init. */
8116 3757958 : && !d->cur->index
8117 3186348 : && d->end - d->cur == 1
8118 575537 : && TREE_CODE (init) != RAW_DATA_CST
8119 6290106 : && reference_related_p (type, TREE_TYPE (init)))
8120 : {
8121 416 : d->cur++;
8122 416 : return init;
8123 : }
8124 :
8125 : /* [dcl.init.aggr]
8126 :
8127 : All implicit type conversions (clause _conv_) are considered when
8128 : initializing the aggregate member with an initializer from an
8129 : initializer-list. If the initializer can initialize a member,
8130 : the member is initialized. Otherwise, if the member is itself a
8131 : non-empty subaggregate, brace elision is assumed and the
8132 : initializer is considered for the initialization of the first
8133 : member of the subaggregate. */
8134 2185156 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8135 : /* But don't try this for the first initializer, since that would be
8136 : looking through the outermost braces; A a2 = { a1 }; is not a
8137 : valid aggregate initialization. */
8138 3529060 : && !first_initializer_p
8139 5727434 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8140 9143 : || can_convert_arg (type, TREE_TYPE (init),
8141 9143 : TREE_CODE (init) == RAW_DATA_CST
8142 5715759 : ? build_int_cst (integer_type_node,
8143 1606 : *(const unsigned char *)
8144 1606 : RAW_DATA_POINTER (init))
8145 : : init,
8146 : LOOKUP_NORMAL, complain)))
8147 4174 : return consume_init (init, d);
8148 :
8149 : /* [dcl.init.string]
8150 :
8151 : A char array (whether plain char, signed char, or unsigned char)
8152 : can be initialized by a string-literal (optionally enclosed in
8153 : braces); a wchar_t array can be initialized by a wide
8154 : string-literal (optionally enclosed in braces). */
8155 5709979 : if (TREE_CODE (type) == ARRAY_TYPE
8156 5709979 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8157 : {
8158 138309 : tree str_init = init;
8159 138309 : tree stripped_str_init = stripped_init;
8160 138309 : reshape_iter stripd = {};
8161 :
8162 : /* Strip one level of braces if and only if they enclose a single
8163 : element (as allowed by [dcl.init.string]). */
8164 138309 : if (!first_initializer_p
8165 11748 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8166 148921 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8167 : {
8168 392 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8169 392 : str_init = stripd.cur->value;
8170 392 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
8171 : }
8172 :
8173 : /* If it's a string literal, then it's the initializer for the array
8174 : as a whole. Otherwise, continue with normal initialization for
8175 : array types (one value per array element). */
8176 138309 : if (TREE_CODE (stripped_str_init) == STRING_CST)
8177 : {
8178 72 : if ((first_initializer_p && has_designator_problem (d, complain))
8179 1062 : || (stripd.cur && has_designator_problem (&stripd, complain)))
8180 990 : return error_mark_node;
8181 990 : d->cur++;
8182 990 : return str_init;
8183 : }
8184 : }
8185 :
8186 : /* The following cases are about aggregates. If we are not within a full
8187 : initializer already, and there is not a CONSTRUCTOR, it means that there
8188 : is a missing set of braces (that is, we are processing the case for
8189 : which reshape_init exists). */
8190 5708989 : bool braces_elided_p = false;
8191 5708989 : if (!first_initializer_p)
8192 : {
8193 1521119 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
8194 : {
8195 1512866 : tree init_type = TREE_TYPE (init);
8196 1512866 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8197 : /* There is no need to call reshape_init for pointer-to-member
8198 : function initializers, as they are always constructed correctly
8199 : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8200 : which is missing outermost braces. We should warn below, and
8201 : one of the routines below will wrap it in additional { }. */;
8202 : /* For a nested compound literal, proceed to specialized routines,
8203 : to handle initialization of arrays and similar. */
8204 1512857 : else if (COMPOUND_LITERAL_P (stripped_init))
8205 7 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8206 : /* If we have an unresolved designator, we need to find the member it
8207 : designates within TYPE, so proceed to the routines below. For
8208 : FIELD_DECL or INTEGER_CST designators, we're already initializing
8209 : the designated element. */
8210 1512850 : else if (d->cur->index
8211 30 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
8212 : /* Brace elision with designators is only permitted for anonymous
8213 : aggregates. */
8214 15 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
8215 : /* A CONSTRUCTOR of the target's type is a previously
8216 : digested initializer. */
8217 1512835 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8218 : {
8219 0 : ++d->cur;
8220 0 : return init;
8221 : }
8222 : else
8223 : {
8224 : /* Something that hasn't been reshaped yet. */
8225 1512835 : ++d->cur;
8226 1512835 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8227 1512835 : return reshape_init (type, init, complain);
8228 : }
8229 : }
8230 :
8231 8284 : if (complain & tf_warning)
8232 7130 : warning (OPT_Wmissing_braces,
8233 : "missing braces around initializer for %qT",
8234 : type);
8235 : braces_elided_p = true;
8236 : }
8237 :
8238 : /* Dispatch to specialized routines. */
8239 4196154 : tree new_init;
8240 4196154 : if (CLASS_TYPE_P (type))
8241 3725435 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
8242 470719 : else if (TREE_CODE (type) == ARRAY_TYPE)
8243 422768 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
8244 47951 : else if (VECTOR_TYPE_P (type))
8245 47951 : new_init = reshape_init_vector (type, d, complain);
8246 : else
8247 0 : gcc_unreachable ();
8248 :
8249 4196154 : if (braces_elided_p
8250 8284 : && TREE_CODE (new_init) == CONSTRUCTOR)
8251 8010 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8252 :
8253 : return new_init;
8254 : }
8255 :
8256 : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
8257 : brace-enclosed aggregate initializer.
8258 :
8259 : INIT is the CONSTRUCTOR containing the list of initializers describing
8260 : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8261 : It may not presently match the shape of the TYPE; for example:
8262 :
8263 : struct S { int a; int b; };
8264 : struct S a[] = { 1, 2, 3, 4 };
8265 :
8266 : Here INIT will hold a vector of four elements, rather than a
8267 : vector of two elements, each itself a vector of two elements. This
8268 : routine transforms INIT from the former form into the latter. The
8269 : revised CONSTRUCTOR node is returned. */
8270 :
8271 : tree
8272 12820963 : reshape_init (tree type, tree init, tsubst_flags_t complain)
8273 : {
8274 12820963 : vec<constructor_elt, va_gc> *v;
8275 12820963 : reshape_iter d;
8276 12820963 : tree new_init;
8277 :
8278 12820963 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8279 :
8280 12820963 : v = CONSTRUCTOR_ELTS (init);
8281 :
8282 : /* An empty constructor does not need reshaping, and it is always a valid
8283 : initializer. */
8284 12821708 : if (vec_safe_is_empty (v))
8285 : return init;
8286 :
8287 4427710 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8288 : /* Already reshaped. */
8289 : return init;
8290 :
8291 : /* Brace elision is not performed for a CONSTRUCTOR representing
8292 : parenthesized aggregate initialization. */
8293 4427564 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
8294 : {
8295 633 : tree elt = (*v)[0].value;
8296 : /* If we're initializing a char array from a string-literal that is
8297 : enclosed in braces, unwrap it here. */
8298 633 : if (TREE_CODE (type) == ARRAY_TYPE
8299 266 : && vec_safe_length (v) == 1
8300 144 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8301 672 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8302 : return elt;
8303 599 : return init;
8304 : }
8305 :
8306 : /* Handle [dcl.init.list] direct-list-initialization from
8307 : single element of enumeration with a fixed underlying type. */
8308 4426931 : if (is_direct_enum_init (type, init))
8309 : {
8310 174 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8311 174 : type = cv_unqualified (type);
8312 174 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8313 : {
8314 159 : warning_sentinel w (warn_useless_cast);
8315 159 : warning_sentinel w2 (warn_ignored_qualifiers);
8316 159 : return cp_build_c_cast (input_location, type, elt,
8317 : tf_warning_or_error);
8318 159 : }
8319 : else
8320 15 : return error_mark_node;
8321 : }
8322 :
8323 : /* Recurse on this CONSTRUCTOR. */
8324 4426757 : d.cur = &(*v)[0];
8325 4426757 : d.end = d.cur + v->length ();
8326 4426757 : d.raw_idx = 0;
8327 :
8328 4426757 : new_init = reshape_init_r (type, &d, init, complain);
8329 4426757 : if (new_init == error_mark_node)
8330 : return error_mark_node;
8331 :
8332 : /* Make sure all the element of the constructor were used. Otherwise,
8333 : issue an error about exceeding initializers. */
8334 4426137 : if (d.cur != d.end)
8335 : {
8336 207 : if (complain & tf_error)
8337 63 : error ("too many initializers for %qT", type);
8338 207 : return error_mark_node;
8339 : }
8340 :
8341 4425930 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8342 4425930 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8343 948428 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8344 4425930 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8345 4425930 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8346 51112 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8347 : || seen_error ());
8348 :
8349 : return new_init;
8350 : }
8351 :
8352 : /* Verify array initializer. Returns true if errors have been reported. */
8353 :
8354 : bool
8355 1358613 : check_array_initializer (tree decl, tree type, tree init)
8356 : {
8357 1358613 : tree element_type = TREE_TYPE (type);
8358 :
8359 : /* Structured binding when initialized with an array type needs
8360 : to have complete type. */
8361 1358613 : if (decl
8362 1347799 : && DECL_DECOMPOSITION_P (decl)
8363 3267 : && DECL_DECOMP_IS_BASE (decl)
8364 1361880 : && !COMPLETE_TYPE_P (type))
8365 : {
8366 0 : error_at (DECL_SOURCE_LOCATION (decl),
8367 : "structured binding has incomplete type %qT", type);
8368 0 : TREE_TYPE (decl) = error_mark_node;
8369 0 : return true;
8370 : }
8371 :
8372 : /* The array type itself need not be complete, because the
8373 : initializer may tell us how many elements are in the array.
8374 : But, the elements of the array must be complete. */
8375 1358613 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
8376 : {
8377 15 : if (decl)
8378 9 : error_at (DECL_SOURCE_LOCATION (decl),
8379 : "elements of array %q#D have incomplete type", decl);
8380 : else
8381 6 : error ("elements of array %q#T have incomplete type", type);
8382 15 : return true;
8383 : }
8384 :
8385 1358598 : location_t loc = (decl ? location_of (decl) : input_location);
8386 1358598 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8387 : return true;
8388 :
8389 : /* A compound literal can't have variable size. */
8390 1358598 : if (init && !decl
8391 1358598 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8392 10805 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8393 : {
8394 3 : error ("variable-sized compound literal");
8395 3 : return true;
8396 : }
8397 : return false;
8398 : }
8399 :
8400 : /* Subroutine of check_initializer; args are passed down from that function.
8401 : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8402 :
8403 : static tree
8404 4966778 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
8405 : {
8406 4966778 : gcc_assert (stmts_are_full_exprs_p ());
8407 4966778 : if (init)
8408 3780108 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8409 4966778 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
8410 : }
8411 :
8412 : /* Verify INIT (the initializer for DECL), and record the
8413 : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8414 : grok_reference_init.
8415 :
8416 : If the return value is non-NULL, it is an expression that must be
8417 : evaluated dynamically to initialize DECL. */
8418 :
8419 : static tree
8420 66723931 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8421 : {
8422 66723931 : tree type;
8423 66723931 : tree init_code = NULL;
8424 66723931 : tree core_type;
8425 :
8426 : /* Things that are going to be initialized need to have complete
8427 : type. */
8428 66723931 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8429 :
8430 66723931 : if (DECL_HAS_VALUE_EXPR_P (decl))
8431 : {
8432 : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8433 : it doesn't have storage to be initialized. */
8434 108 : gcc_assert (init == NULL_TREE);
8435 : return NULL_TREE;
8436 : }
8437 :
8438 66723823 : if (type == error_mark_node)
8439 : /* We will have already complained. */
8440 : return NULL_TREE;
8441 :
8442 66723823 : if (TREE_CODE (type) == ARRAY_TYPE)
8443 : {
8444 1347799 : if (check_array_initializer (decl, type, init))
8445 : return NULL_TREE;
8446 : }
8447 65376024 : else if (!COMPLETE_TYPE_P (type))
8448 : {
8449 37 : error_at (DECL_SOURCE_LOCATION (decl),
8450 : "%q#D has incomplete type", decl);
8451 37 : TREE_TYPE (decl) = error_mark_node;
8452 37 : return NULL_TREE;
8453 : }
8454 : else
8455 : /* There is no way to make a variable-sized class type in GNU C++. */
8456 65375987 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8457 :
8458 66723777 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8459 : {
8460 3337403 : int init_len = CONSTRUCTOR_NELTS (init);
8461 3337403 : if (SCALAR_TYPE_P (type))
8462 : {
8463 187385 : if (init_len == 0)
8464 : {
8465 156261 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8466 156261 : init = build_zero_init (type, NULL_TREE, false);
8467 : }
8468 31124 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8469 : {
8470 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8471 : "scalar object %qD requires one element in "
8472 : "initializer", decl);
8473 6 : TREE_TYPE (decl) = error_mark_node;
8474 6 : return NULL_TREE;
8475 : }
8476 : }
8477 : }
8478 :
8479 66723771 : if (TREE_CODE (decl) == CONST_DECL)
8480 : {
8481 0 : gcc_assert (!TYPE_REF_P (type));
8482 :
8483 0 : DECL_INITIAL (decl) = init;
8484 :
8485 0 : gcc_assert (init != NULL_TREE);
8486 : init = NULL_TREE;
8487 : }
8488 66723771 : else if (!init && DECL_REALLY_EXTERN (decl))
8489 : ;
8490 66723771 : else if (flag_openmp
8491 188057 : && VAR_P (decl)
8492 188057 : && DECL_LANG_SPECIFIC (decl)
8493 110470 : && DECL_OMP_DECLARE_MAPPER_P (decl)
8494 66723771 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8495 : return NULL_TREE;
8496 5412213 : else if (init || type_build_ctor_call (type)
8497 70949311 : || TYPE_REF_P (type))
8498 : {
8499 62498246 : if (TYPE_REF_P (type))
8500 : {
8501 512462 : init = grok_reference_init (decl, type, init, flags);
8502 512462 : flags |= LOOKUP_ALREADY_DIGESTED;
8503 : }
8504 61985784 : else if (!init)
8505 1186673 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8506 : tf_warning_or_error);
8507 : /* Do not reshape constructors of vectors (they don't need to be
8508 : reshaped. */
8509 60799111 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8510 : {
8511 3180796 : if (is_std_init_list (type))
8512 : {
8513 333 : init = perform_implicit_conversion (type, init,
8514 : tf_warning_or_error);
8515 333 : flags |= LOOKUP_ALREADY_DIGESTED;
8516 : }
8517 3180463 : else if (TYPE_NON_AGGREGATE_CLASS (type))
8518 : {
8519 : /* Don't reshape if the class has constructors. */
8520 444021 : if (cxx_dialect == cxx98)
8521 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8522 : "in C++98 %qD must be initialized by "
8523 : "constructor, not by %<{...}%>",
8524 : decl);
8525 : }
8526 2736442 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8527 : {
8528 0 : error ("opaque vector types cannot be initialized");
8529 0 : init = error_mark_node;
8530 : }
8531 : else
8532 : {
8533 2736442 : init = reshape_init (type, init, tf_warning_or_error);
8534 2736442 : flags |= LOOKUP_NO_NARROWING;
8535 : }
8536 : }
8537 : /* [dcl.init] "Otherwise, if the destination type is an array, the object
8538 : is initialized as follows..." So handle things like
8539 :
8540 : int a[](1, 2, 3);
8541 :
8542 : which is permitted in C++20 by P0960. */
8543 57618315 : else if (TREE_CODE (init) == TREE_LIST
8544 1167524 : && TREE_TYPE (init) == NULL_TREE
8545 1167524 : && TREE_CODE (type) == ARRAY_TYPE
8546 174 : && !DECL_DECOMPOSITION_P (decl)
8547 57618459 : && (cxx_dialect >= cxx20))
8548 134 : init = do_aggregate_paren_init (init, type);
8549 57618181 : else if (TREE_CODE (init) == TREE_LIST
8550 1167390 : && TREE_TYPE (init) != unknown_type_node
8551 58785571 : && !MAYBE_CLASS_TYPE_P (type))
8552 : {
8553 126084 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8554 :
8555 : /* We get here with code like `int a (2);' */
8556 126084 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8557 : tf_warning_or_error);
8558 : }
8559 :
8560 : /* If DECL has an array type without a specific bound, deduce the
8561 : array size from the initializer. */
8562 62498246 : maybe_deduce_size_from_array_init (decl, init);
8563 62498246 : type = TREE_TYPE (decl);
8564 62498246 : if (type == error_mark_node)
8565 : return NULL_TREE;
8566 :
8567 119512771 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8568 7438825 : && !(flags & LOOKUP_ALREADY_DIGESTED)
8569 7438492 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
8570 2920172 : && CP_AGGREGATE_TYPE_P (type)
8571 2476151 : && (CLASS_TYPE_P (type)
8572 : /* The call to build_aggr_init below could end up
8573 : calling build_vec_init, which may break when we
8574 : are processing a template. */
8575 1388 : || processing_template_decl
8576 1319 : || !TYPE_NEEDS_CONSTRUCTING (type)
8577 1287 : || type_has_extended_temps (type))))
8578 120032916 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8579 : {
8580 4966778 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8581 :
8582 : /* A constructor call is a non-trivial initializer even if
8583 : it isn't explicitly written. */
8584 4966778 : if (TREE_SIDE_EFFECTS (init_code))
8585 4944733 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8586 :
8587 : /* If this is a constexpr initializer, expand_default_init will
8588 : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8589 : case, pull the initializer back out and pass it down into
8590 : store_init_value. */
8591 13229216 : while (true)
8592 : {
8593 13229216 : if (TREE_CODE (init_code) == EXPR_STMT
8594 8289817 : || TREE_CODE (init_code) == STMT_EXPR
8595 8253783 : || TREE_CODE (init_code) == CONVERT_EXPR)
8596 8226489 : init_code = TREE_OPERAND (init_code, 0);
8597 5002727 : else if (TREE_CODE (init_code) == BIND_EXPR)
8598 35949 : init_code = BIND_EXPR_BODY (init_code);
8599 : else
8600 : break;
8601 : }
8602 4966778 : if (TREE_CODE (init_code) == INIT_EXPR)
8603 : {
8604 : /* In C++20, the call to build_aggr_init could have created
8605 : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8606 : A(1, 2). */
8607 3272957 : tree rhs = TREE_OPERAND (init_code, 1);
8608 3272957 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8609 : /* Avoid leaking TARGET_EXPR into template trees. */
8610 18007 : rhs = build_implicit_conv_flags (type, init, flags);
8611 3272957 : init = rhs;
8612 :
8613 3272957 : init_code = NULL_TREE;
8614 : /* Don't call digest_init; it's unnecessary and will complain
8615 : about aggregate initialization of non-aggregate classes. */
8616 3272933 : flags |= LOOKUP_ALREADY_DIGESTED;
8617 : }
8618 1693821 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8619 3369365 : || DECL_DECLARED_CONSTINIT_P (decl))
8620 : {
8621 : /* Declared constexpr or constinit, but no suitable initializer;
8622 : massage init appropriately so we can pass it into
8623 : store_init_value for the error. */
8624 18295 : tree new_init = NULL_TREE;
8625 18295 : if (!processing_template_decl
8626 291 : && TREE_CODE (init_code) == CALL_EXPR)
8627 232 : new_init = build_cplus_new (type, init_code, tf_none);
8628 18039 : else if (CLASS_TYPE_P (type)
8629 36102 : && (!init || TREE_CODE (init) == TREE_LIST))
8630 17941 : new_init = build_functional_cast (input_location, type,
8631 : init, tf_none);
8632 18173 : if (new_init)
8633 : {
8634 18173 : init = new_init;
8635 18173 : if (TREE_CODE (init) == TARGET_EXPR
8636 257 : && !(flags & LOOKUP_ONLYCONVERTING))
8637 257 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8638 : }
8639 : init_code = NULL_TREE;
8640 : }
8641 : else
8642 : init = NULL_TREE;
8643 : }
8644 :
8645 60822716 : if (init && TREE_CODE (init) != TREE_VEC)
8646 : {
8647 60822701 : init_code = store_init_value (decl, init, cleanups, flags);
8648 :
8649 60820004 : if (DECL_INITIAL (decl)
8650 42640550 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8651 64605416 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8652 : {
8653 1949415 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8654 1949415 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8655 1949415 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8656 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8657 : }
8658 :
8659 1704517 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8660 55501 : && DECL_INITIAL (decl)
8661 54977 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8662 60871497 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8663 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8664 3 : DECL_SOURCE_LOCATION (decl)),
8665 3 : 0, "array %qD initialized by parenthesized "
8666 : "string literal %qE",
8667 3 : decl, DECL_INITIAL (decl));
8668 : init = NULL_TREE;
8669 : }
8670 : }
8671 4225525 : else if (!init && REFLECTION_TYPE_P (type))
8672 : {
8673 : /* [dcl.init.general]: To default-initialize an object of type
8674 : std::meta::info means that the object is zero-initialized. */
8675 76 : DECL_INITIAL (decl)
8676 76 : = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
8677 76 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8678 76 : TREE_CONSTANT (decl) = true;
8679 76 : init = NULL_TREE;
8680 : }
8681 : else
8682 : {
8683 8450898 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8684 4935251 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8685 709789 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8686 19 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8687 : /*complain=*/true);
8688 :
8689 4225449 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8690 : tf_warning_or_error);
8691 : }
8692 :
8693 76 : if (init && init != error_mark_node)
8694 0 : init_code = cp_build_init_expr (decl, init);
8695 :
8696 20071298 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8697 66775097 : && init_code != error_mark_node)
8698 : init_code = NULL_TREE;
8699 :
8700 66669297 : if (init_code)
8701 : {
8702 : /* We might have set these in cp_finish_decl. */
8703 20019525 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8704 20019525 : TREE_CONSTANT (decl) = false;
8705 : }
8706 :
8707 20019525 : if (init_code
8708 20019525 : && DECL_IN_AGGR_P (decl)
8709 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8710 : {
8711 0 : static int explained = 0;
8712 :
8713 0 : auto_diagnostic_group d;
8714 0 : if (cxx_dialect < cxx11)
8715 0 : error ("initializer invalid for static member with constructor");
8716 0 : else if (cxx_dialect < cxx17)
8717 0 : error ("non-constant in-class initialization invalid for static "
8718 : "member %qD", decl);
8719 : else
8720 0 : error ("non-constant in-class initialization invalid for non-inline "
8721 : "static member %qD", decl);
8722 0 : if (!explained)
8723 : {
8724 0 : inform (input_location,
8725 : "(an out of class initialization is required)");
8726 0 : explained = 1;
8727 : }
8728 0 : return NULL_TREE;
8729 0 : }
8730 :
8731 : return init_code;
8732 : }
8733 :
8734 : /* If DECL is not a local variable, give it RTL. */
8735 :
8736 : static void
8737 129626766 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8738 : {
8739 129626766 : int toplev = toplevel_bindings_p ();
8740 129626766 : int defer_p;
8741 :
8742 : /* Set the DECL_ASSEMBLER_NAME for the object. */
8743 129626766 : if (asmspec)
8744 : {
8745 : /* The `register' keyword, when used together with an
8746 : asm-specification, indicates that the variable should be
8747 : placed in a particular register. */
8748 963057 : if (VAR_P (decl) && DECL_REGISTER (decl))
8749 : {
8750 87 : if (TREE_ADDRESSABLE (decl))
8751 3 : error_at (DECL_SOURCE_LOCATION (decl),
8752 : "address of explicit register variable %qD requested",
8753 : decl);
8754 : else
8755 : {
8756 84 : set_user_assembler_name (decl, asmspec);
8757 84 : DECL_HARD_REGISTER (decl) = 1;
8758 : }
8759 : }
8760 : else
8761 : {
8762 962970 : if (TREE_CODE (decl) == FUNCTION_DECL
8763 962970 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8764 75912 : set_builtin_user_assembler_name (decl, asmspec);
8765 962970 : set_user_assembler_name (decl, asmspec);
8766 962970 : if (DECL_LOCAL_DECL_P (decl))
8767 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8768 : /* We have to propagate the name to the ns-alias.
8769 : This is horrible, as we're affecting a
8770 : possibly-shared decl. Again, a one-true-decl
8771 : model breaks down. */
8772 6 : if (ns_decl != error_mark_node)
8773 6 : set_user_assembler_name (ns_decl, asmspec);
8774 : }
8775 : }
8776 :
8777 : /* Handle non-variables up front. */
8778 129626766 : if (!VAR_P (decl))
8779 : {
8780 56083032 : rest_of_decl_compilation (decl, toplev, at_eof);
8781 56083032 : return;
8782 : }
8783 :
8784 : /* If we see a class member here, it should be a static data
8785 : member. */
8786 73543734 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8787 : {
8788 5513314 : gcc_assert (TREE_STATIC (decl));
8789 : /* An in-class declaration of a static data member should be
8790 : external; it is only a declaration, and not a definition. */
8791 5513314 : if (init == NULL_TREE)
8792 5513286 : gcc_assert (DECL_EXTERNAL (decl)
8793 : || !TREE_PUBLIC (decl));
8794 : }
8795 :
8796 : /* We don't create any RTL for local variables. */
8797 73543734 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8798 : return;
8799 :
8800 : /* Don't output reflection variables. */
8801 42504493 : if (consteval_only_p (decl))
8802 : {
8803 : /* Disable assemble_variable. */
8804 727 : DECL_EXTERNAL (decl) = true;
8805 : /* Undo make_decl_one_only. */
8806 727 : if (DECL_COMDAT_GROUP (decl))
8807 : {
8808 125 : symtab_node *node = symtab_node::get (decl);
8809 125 : node->set_comdat_group (NULL);
8810 125 : node->dissolve_same_comdat_group_list ();
8811 : }
8812 727 : return;
8813 : }
8814 :
8815 : /* We defer emission of local statics until the corresponding
8816 : DECL_EXPR is expanded. But with constexpr its function might never
8817 : be expanded, so go ahead and tell cgraph about the variable now. */
8818 85007532 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8819 168122 : && !var_in_maybe_constexpr_fn (decl))
8820 84840065 : || DECL_VIRTUAL_P (decl));
8821 :
8822 : /* Defer template instantiations. */
8823 42503766 : if (DECL_LANG_SPECIFIC (decl)
8824 42503766 : && DECL_IMPLICIT_INSTANTIATION (decl))
8825 : defer_p = 1;
8826 :
8827 : /* If we're not deferring, go ahead and assemble the variable. */
8828 19416144 : if (!defer_p)
8829 17002879 : rest_of_decl_compilation (decl, toplev, at_eof);
8830 : }
8831 :
8832 : /* walk_tree helper for wrap_temporary_cleanups, below. */
8833 :
8834 : static tree
8835 15954564 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8836 : {
8837 : /* Stop at types or full-expression boundaries. */
8838 15954564 : if (TYPE_P (*stmt_p)
8839 15954352 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8840 : {
8841 442 : *walk_subtrees = 0;
8842 442 : return NULL_TREE;
8843 : }
8844 :
8845 15954122 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8846 : {
8847 450158 : tree guard = (tree)data;
8848 450158 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8849 :
8850 208455 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8851 638794 : && !expr_noexcept_p (tcleanup, tf_none))
8852 : {
8853 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8854 : /* Tell honor_protect_cleanup_actions to handle this as a separate
8855 : cleanup. */
8856 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8857 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8858 : }
8859 : }
8860 :
8861 : return NULL_TREE;
8862 : }
8863 :
8864 : /* We're initializing a local variable which has a cleanup GUARD. If there
8865 : are any temporaries used in the initializer INIT of this variable, we
8866 : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8867 : variable will be cleaned up properly if one of them throws.
8868 :
8869 : Unfortunately, there's no way to express this properly in terms of
8870 : nesting, as the regions for the temporaries overlap the region for the
8871 : variable itself; if there are two temporaries, the variable needs to be
8872 : the first thing destroyed if either of the temporary destructors throws.
8873 : However, we only want to run the variable's cleanup if it actually got
8874 : constructed. So we need to guard the temporary cleanups with the
8875 : variable's cleanup if they are run on the normal path, but not if they
8876 : are run on the exceptional path. We implement this by telling
8877 : honor_protect_cleanup_actions to strip the variable cleanup from the
8878 : exceptional path.
8879 :
8880 : Another approach could be to make the variable cleanup region enclose
8881 : initialization, but depend on a flag to indicate that the variable is
8882 : initialized; that's effectively what we do for arrays. But the current
8883 : approach works fine for non-arrays, and has no code overhead in the usual
8884 : case where the temporary destructors are noexcept. */
8885 :
8886 : static void
8887 1373735 : wrap_temporary_cleanups (tree init, tree guard)
8888 : {
8889 1373735 : if (TREE_CODE (guard) == BIND_EXPR)
8890 : {
8891 : /* An array cleanup region already encloses any temporary cleanups,
8892 : don't wrap it around them again. */
8893 407 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8894 : return;
8895 : }
8896 1373328 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8897 : }
8898 :
8899 : /* Generate code to initialize DECL (a local variable). */
8900 :
8901 : static void
8902 31039223 : initialize_local_var (tree decl, tree init, bool decomp)
8903 : {
8904 31039223 : tree type = TREE_TYPE (decl);
8905 31039223 : tree cleanup;
8906 31039223 : int already_used;
8907 :
8908 31039223 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8909 31039223 : gcc_assert (!TREE_STATIC (decl));
8910 :
8911 31039223 : if (DECL_SIZE (decl) == NULL_TREE)
8912 : {
8913 : /* If we used it already as memory, it must stay in memory. */
8914 0 : DECL_INITIAL (decl) = NULL_TREE;
8915 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8916 0 : return;
8917 : }
8918 :
8919 31039223 : if (type == error_mark_node)
8920 : return;
8921 :
8922 : /* Compute and store the initial value. */
8923 31039223 : already_used = TREE_USED (decl) || TREE_USED (type);
8924 31039223 : if (TREE_USED (type))
8925 32 : DECL_READ_P (decl) = 1;
8926 :
8927 : /* Generate a cleanup, if necessary. */
8928 31039223 : cleanup = (decomp ? NULL_TREE
8929 30974066 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8930 :
8931 : /* Perform the initialization. */
8932 31039223 : if (init)
8933 : {
8934 20005509 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8935 20005509 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8936 17668023 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8937 26925529 : && TREE_OPERAND (init, 0) == decl)
8938 : {
8939 : /* Stick simple initializers in DECL_INITIAL so that
8940 : -Wno-init-self works (c++/34772). */
8941 9257506 : DECL_INITIAL (decl) = rinit;
8942 :
8943 9257506 : if (warn_init_self && TYPE_REF_P (type))
8944 : {
8945 2442 : STRIP_NOPS (rinit);
8946 2442 : if (rinit == decl)
8947 3 : warning_at (DECL_SOURCE_LOCATION (decl),
8948 3 : OPT_Winit_self,
8949 : "reference %qD is initialized with itself", decl);
8950 : }
8951 : }
8952 : else
8953 : {
8954 10748003 : int saved_stmts_are_full_exprs_p;
8955 :
8956 : /* If we're only initializing a single object, guard the
8957 : destructors of any temporaries used in its initializer with
8958 : its destructor. */
8959 10748003 : if (cleanup)
8960 1354466 : wrap_temporary_cleanups (init, cleanup);
8961 :
8962 10748003 : gcc_assert (building_stmt_list_p ());
8963 10748003 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8964 : /* Avoid CLEANUP_POINT_EXPR for the structured binding
8965 : bases, those will have CLEANUP_POINT_EXPR at the end of
8966 : code emitted by cp_finish_decomp. */
8967 10748003 : if (decomp)
8968 64711 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8969 : else
8970 10683292 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8971 10748003 : finish_expr_stmt (init);
8972 10748003 : current_stmt_tree ()->stmts_are_full_exprs_p
8973 10748003 : = saved_stmts_are_full_exprs_p;
8974 : }
8975 : }
8976 :
8977 : /* Set this to 0 so we can tell whether an aggregate which was
8978 : initialized was ever used. Don't do this if it has a
8979 : destructor, so we don't complain about the 'resource
8980 : allocation is initialization' idiom. Now set
8981 : attribute((unused)) on types so decls of that type will be
8982 : marked used. (see TREE_USED, above.) */
8983 31039223 : if (TYPE_NEEDS_CONSTRUCTING (type)
8984 3844186 : && ! already_used
8985 22067 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8986 31042578 : && DECL_NAME (decl))
8987 3309 : TREE_USED (decl) = 0;
8988 31035914 : else if (already_used)
8989 28122498 : TREE_USED (decl) = 1;
8990 :
8991 31039223 : if (cleanup)
8992 1367421 : finish_decl_cleanup (decl, cleanup);
8993 : }
8994 :
8995 : /* DECL is a VAR_DECL for a compiler-generated variable with static
8996 : storage duration (like a virtual table) whose initializer is a
8997 : compile-time constant. Initialize the variable and provide it to the
8998 : back end. */
8999 :
9000 : void
9001 2206876 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
9002 : {
9003 2206876 : tree init;
9004 2206876 : gcc_assert (DECL_ARTIFICIAL (decl));
9005 2206876 : init = build_constructor (TREE_TYPE (decl), v);
9006 2206876 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
9007 2206876 : DECL_INITIAL (decl) = init;
9008 2206876 : DECL_INITIALIZED_P (decl) = 1;
9009 : /* Mark the decl as constexpr so that we can access its content
9010 : at compile time. */
9011 2206876 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9012 2206876 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
9013 2206876 : determine_visibility (decl);
9014 2206876 : layout_var_decl (decl);
9015 2206876 : maybe_commonize_var (decl);
9016 2206876 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
9017 2206876 : }
9018 :
9019 : /* INIT is the initializer for a variable, as represented by the
9020 : parser. Returns true iff INIT is value-dependent. */
9021 :
9022 : static bool
9023 9255692 : value_dependent_init_p (tree init)
9024 : {
9025 9255692 : if (TREE_CODE (init) == TREE_LIST)
9026 : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
9027 19945 : return any_value_dependent_elements_p (init);
9028 9235747 : else if (TREE_CODE (init) == CONSTRUCTOR)
9029 : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
9030 : {
9031 68481 : if (dependent_type_p (TREE_TYPE (init)))
9032 : return true;
9033 :
9034 68475 : vec<constructor_elt, va_gc> *elts;
9035 68475 : size_t nelts;
9036 68475 : size_t i;
9037 :
9038 68475 : elts = CONSTRUCTOR_ELTS (init);
9039 68475 : nelts = vec_safe_length (elts);
9040 595443 : for (i = 0; i < nelts; ++i)
9041 554572 : if (value_dependent_init_p ((*elts)[i].value))
9042 : return true;
9043 : }
9044 : else
9045 : /* It must be a simple expression, e.g., int i = 3; */
9046 9167266 : return value_dependent_expression_p (init);
9047 :
9048 : return false;
9049 : }
9050 :
9051 : /* A helper function to be called via walk_tree. If any label exists
9052 : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
9053 :
9054 : static tree
9055 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
9056 : {
9057 337 : if (TYPE_P (*tp))
9058 0 : *walk_subtrees = 0;
9059 337 : if (TREE_CODE (*tp) == LABEL_DECL)
9060 6 : cfun->has_forced_label_in_static = 1;
9061 337 : return NULL_TREE;
9062 : }
9063 :
9064 : /* Return true if DECL has either a trivial destructor, or for C++20
9065 : is constexpr and has a constexpr destructor. */
9066 :
9067 : static bool
9068 69551997 : decl_maybe_constant_destruction (tree decl, tree type)
9069 : {
9070 69551997 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
9071 69551997 : || (cxx_dialect >= cxx20
9072 4896354 : && VAR_P (decl)
9073 4896044 : && DECL_DECLARED_CONSTEXPR_P (decl)
9074 364 : && type_has_constexpr_destructor (strip_array_types (type))));
9075 : }
9076 :
9077 : static tree declare_simd_adjust_this (tree *, int *, void *);
9078 :
9079 : /* Helper function of omp_declare_variant_finalize. Finalize one
9080 : "omp declare variant base" attribute. Return true if it should be
9081 : removed. */
9082 :
9083 : static bool
9084 1751 : omp_declare_variant_finalize_one (tree decl, tree attr)
9085 : {
9086 1751 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9087 : {
9088 164 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
9089 : DECL_ARGUMENTS (decl), NULL);
9090 164 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
9091 : DECL_ARGUMENTS (decl), NULL);
9092 : }
9093 :
9094 1751 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
9095 1751 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
9096 : OMP_TRAIT_CONSTRUCT_SIMD);
9097 1751 : if (simd)
9098 : {
9099 51 : TREE_VALUE (simd)
9100 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
9101 51 : OMP_TS_PROPERTIES (simd));
9102 : /* FIXME, adjusting simd args unimplemented. */
9103 51 : return true;
9104 : }
9105 :
9106 1700 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
9107 1700 : location_t varid_loc
9108 1700 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
9109 1700 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
9110 1700 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
9111 1700 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
9112 :
9113 1700 : location_t save_loc = input_location;
9114 1700 : input_location = varid_loc;
9115 :
9116 1700 : releasing_vec args;
9117 1700 : tree parm = DECL_ARGUMENTS (decl);
9118 1700 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9119 164 : parm = DECL_CHAIN (parm);
9120 3022 : for (; parm; parm = DECL_CHAIN (parm))
9121 1322 : vec_safe_push (args, forward_parm (parm));
9122 :
9123 1700 : unsigned nappend_args = 0;
9124 1700 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9125 1700 : if (append_args_list)
9126 : {
9127 354 : append_args_list = TREE_VALUE (append_args_list);
9128 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9129 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
9130 : : NULL_TREE);
9131 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9132 387 : nappend_args++;
9133 354 : if (nappend_args)
9134 : {
9135 237 : tree type;
9136 237 : if ((type = lookup_qualified_name (global_namespace,
9137 : "omp_interop_t",
9138 : LOOK_want::NORMAL,
9139 : /*complain*/false)) == NULL_TREE
9140 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
9141 : {
9142 24 : location_t loc = input_location;
9143 24 : variant = tree_strip_any_location_wrapper (variant);
9144 24 : if (!identifier_p (variant))
9145 : {
9146 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9147 21 : variant = OVL_FIRST (variant);
9148 21 : loc = EXPR_LOC_OR_LOC (variant,
9149 : DECL_SOURCE_LOCATION (variant));
9150 : }
9151 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9152 24 : args->length () + 1, variant);
9153 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9154 : "%<append_args%> specified here");
9155 24 : return true;
9156 : }
9157 564 : for (unsigned i = 0; i < nappend_args; i++)
9158 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
9159 : }
9160 : }
9161 :
9162 1676 : bool koenig_p = false;
9163 1676 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9164 : {
9165 1606 : if (identifier_p (variant)
9166 : /* In C++20, we may need to perform ADL for a template
9167 : name. */
9168 1582 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9169 18 : && identifier_p (TREE_OPERAND (variant, 0))))
9170 : {
9171 24 : if (!args->is_empty ())
9172 : {
9173 24 : koenig_p = true;
9174 24 : if (!any_type_dependent_arguments_p (args))
9175 21 : variant = perform_koenig_lookup (variant, args,
9176 : tf_warning_or_error);
9177 : }
9178 : else
9179 0 : variant = unqualified_fn_lookup_error (variant);
9180 : }
9181 1582 : else if (!args->is_empty () && is_overloaded_fn (variant))
9182 : {
9183 830 : tree fn = get_first_fn (variant);
9184 830 : fn = STRIP_TEMPLATE (fn);
9185 830 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9186 830 : || DECL_FUNCTION_MEMBER_P (fn)
9187 717 : || DECL_LOCAL_DECL_P (fn)))
9188 : {
9189 717 : koenig_p = true;
9190 717 : if (!any_type_dependent_arguments_p (args))
9191 666 : variant = perform_koenig_lookup (variant, args,
9192 : tf_warning_or_error);
9193 : }
9194 : }
9195 : }
9196 :
9197 1676 : if (idk == CP_ID_KIND_QUALIFIED)
9198 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9199 : koenig_p, tf_warning_or_error);
9200 1670 : else if (idk == CP_ID_KIND_NONE
9201 64 : && TREE_CODE (variant) == FUNCTION_DECL
9202 64 : && DECL_IOBJ_MEMBER_FUNCTION_P (variant)
9203 1718 : && CLASS_TYPE_P (DECL_CONTEXT (decl)))
9204 : {
9205 48 : tree saved_ccp = current_class_ptr;
9206 48 : tree saved_ccr = current_class_ref;
9207 48 : current_class_ptr = NULL_TREE;
9208 48 : current_class_ref = NULL_TREE;
9209 48 : inject_this_parameter (DECL_CONTEXT (decl), TYPE_UNQUALIFIED);
9210 48 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9211 : koenig_p, tf_warning_or_error);
9212 48 : current_class_ptr = saved_ccp;
9213 48 : current_class_ref = saved_ccr;
9214 : }
9215 : else
9216 1622 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9217 : koenig_p, tf_warning_or_error);
9218 1676 : if (variant == error_mark_node && !processing_template_decl)
9219 : return true;
9220 :
9221 1610 : if (TREE_CODE (variant) == TARGET_EXPR)
9222 9 : variant = TARGET_EXPR_INITIAL (variant);
9223 :
9224 3211 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9225 1610 : input_location = save_loc;
9226 :
9227 1610 : if (variant == decl)
9228 : {
9229 0 : error_at (varid_loc, "variant %qD is the same as base function",
9230 : variant);
9231 0 : return true;
9232 : }
9233 :
9234 1610 : if (variant)
9235 : {
9236 1526 : bool fail;
9237 1526 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9238 1526 : if (!nappend_args)
9239 1367 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9240 : COMPARE_STRICT);
9241 : else
9242 : {
9243 159 : unsigned nbase_args = 0;
9244 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9245 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9246 240 : nbase_args++;
9247 159 : tree vargs, varg;
9248 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9249 399 : for (unsigned i = 0; i < nbase_args && varg;
9250 240 : i++, varg = TREE_CHAIN (varg))
9251 240 : vargs = varg;
9252 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
9253 258 : varg = TREE_CHAIN (varg);
9254 159 : tree saved_vargs;
9255 159 : int saved_no_named_args_stdarg = 0;
9256 159 : if (nbase_args)
9257 : {
9258 111 : saved_vargs = TREE_CHAIN (vargs);
9259 111 : TREE_CHAIN (vargs) = varg;
9260 : }
9261 : else
9262 : {
9263 48 : saved_vargs = vargs;
9264 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9265 48 : saved_no_named_args_stdarg
9266 48 : = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9267 48 : if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9268 48 : && varg == NULL_TREE)
9269 2 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9270 : }
9271 : /* Skip assert check that TYPE_CANONICAL is the same. */
9272 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9273 : COMPARE_STRUCTURAL);
9274 159 : if (nbase_args)
9275 111 : TREE_CHAIN (vargs) = saved_vargs;
9276 : else
9277 : {
9278 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9279 48 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9280 96 : = saved_no_named_args_stdarg;
9281 : }
9282 159 : varg = saved_vargs;
9283 159 : if (!fail && !processing_template_decl)
9284 402 : for (unsigned i = 0; i < nappend_args;
9285 246 : i++, varg = TREE_CHAIN (varg))
9286 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9287 : {
9288 9 : error_at (DECL_SOURCE_LOCATION (variant),
9289 : "argument %d of %qD must be of %<omp_interop_t%>",
9290 9 : nbase_args + i + 1, variant);
9291 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9292 : "%<append_args%> specified here");
9293 9 : break;
9294 : }
9295 : }
9296 1526 : if (fail)
9297 : {
9298 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
9299 : "types", variant, decl);
9300 69 : return true;
9301 : }
9302 1457 : if (fndecl_built_in_p (variant)
9303 1457 : && (startswith (varname, "__builtin_")
9304 0 : || startswith (varname, "__sync_")
9305 0 : || startswith (varname, "__atomic_")))
9306 : {
9307 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
9308 6 : return true;
9309 : }
9310 : else
9311 : {
9312 1451 : tree construct
9313 1451 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9314 1451 : omp_mark_declare_variant (match_loc, variant, construct);
9315 1451 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9316 : return true;
9317 1039 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9318 :
9319 : // Prepend adjust_args list to variant attributes
9320 1039 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9321 1039 : if (adjust_args_list != NULL_TREE)
9322 : {
9323 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
9324 285 : && TREE_VALUE (adjust_args_list))
9325 : {
9326 : /* Shift arg position for the added 'this' pointer. */
9327 : /* Handle need_device_ptr */
9328 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9329 51 : t; t = TREE_CHAIN (t))
9330 30 : TREE_VALUE (t)
9331 30 : = build_int_cst (TREE_TYPE (t),
9332 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
9333 : }
9334 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9335 : {
9336 : /* Shift likewise the number of args after which the
9337 : interop object should be added. */
9338 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9339 6 : TREE_PURPOSE (nargs)
9340 6 : = build_int_cst (TREE_TYPE (nargs),
9341 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9342 : }
9343 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9344 264 : TREE_VALUE (t)
9345 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9346 528 : DECL_ATTRIBUTES (variant) = tree_cons (
9347 : get_identifier ("omp declare variant variant args"),
9348 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9349 : }
9350 : }
9351 : }
9352 84 : else if (!processing_template_decl)
9353 : {
9354 0 : error_at (varid_loc, "could not find variant declaration");
9355 0 : return true;
9356 : }
9357 :
9358 : return false;
9359 1700 : }
9360 :
9361 : /* Helper function, finish up "omp declare variant base" attribute
9362 : now that there is a DECL. ATTR is the first "omp declare variant base"
9363 : attribute. */
9364 :
9365 : void
9366 1546 : omp_declare_variant_finalize (tree decl, tree attr)
9367 : {
9368 1546 : size_t attr_len = strlen ("omp declare variant base");
9369 1546 : tree *list = &DECL_ATTRIBUTES (decl);
9370 1546 : bool remove_all = false;
9371 1546 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
9372 1546 : if (TREE_CHAIN (TREE_VALUE (attr))
9373 1546 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9374 3092 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9375 1546 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9376 3092 : if (DECL_CONSTRUCTOR_P (decl))
9377 : {
9378 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9379 27 : remove_all = true;
9380 : }
9381 1519 : else if (DECL_DESTRUCTOR_P (decl))
9382 : {
9383 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9384 9 : remove_all = true;
9385 : }
9386 1510 : else if (DECL_DEFAULTED_FN (decl))
9387 : {
9388 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9389 2 : remove_all = true;
9390 : }
9391 1508 : else if (DECL_DELETED_FN (decl))
9392 : {
9393 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9394 2 : remove_all = true;
9395 : }
9396 1506 : else if (DECL_VIRTUAL_P (decl))
9397 : {
9398 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9399 0 : remove_all = true;
9400 : }
9401 : /* This loop is like private_lookup_attribute, except that it works
9402 : with tree * rather than tree, as we might want to remove the
9403 : attributes that are diagnosed as errorneous. */
9404 3349 : while (*list)
9405 : {
9406 1803 : tree attr = get_attribute_name (*list);
9407 1803 : size_t ident_len = IDENTIFIER_LENGTH (attr);
9408 4729 : if (cmp_attribs ("omp declare variant base", attr_len,
9409 1803 : IDENTIFIER_POINTER (attr), ident_len))
9410 : {
9411 1791 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
9412 : {
9413 668 : *list = TREE_CHAIN (*list);
9414 668 : continue;
9415 : }
9416 : }
9417 1135 : list = &TREE_CHAIN (*list);
9418 : }
9419 1546 : }
9420 :
9421 : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9422 :
9423 : /* Finish processing of a declaration;
9424 : install its line number and initial value.
9425 : If the length of an array type is not known before,
9426 : it must be determined now, from the initial value, or it is an error.
9427 :
9428 : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9429 : true, then INIT is an integral constant expression.
9430 :
9431 : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9432 : if the (init) syntax was used.
9433 :
9434 : DECOMP is first identifier's DECL and identifier count in a structured
9435 : bindings, nullptr if not a structured binding. */
9436 :
9437 : void
9438 329229724 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9439 : tree asmspec_tree, int flags, cp_decomp *decomp)
9440 : {
9441 329229724 : vec<tree, va_gc> *cleanups = NULL;
9442 329229724 : const char *asmspec = NULL;
9443 329229724 : int was_readonly = 0;
9444 329229724 : bool var_definition_p = false;
9445 329229724 : tree auto_node;
9446 329229724 : auto_vec<tree> extra_cleanups;
9447 329229724 : tree aggregates1 = NULL_TREE;
9448 329229724 : struct decomp_cleanup {
9449 : tree decl;
9450 : cp_decomp *&decomp;
9451 329227027 : ~decomp_cleanup ()
9452 : {
9453 329227027 : if (decomp && DECL_DECOMPOSITION_P (decl))
9454 212197 : cp_finish_decomp (decl, decomp);
9455 329227027 : }
9456 329229724 : } decomp_cl = { decl, decomp };
9457 :
9458 329229724 : if (decl == error_mark_node)
9459 : return;
9460 329229684 : else if (! decl)
9461 : {
9462 0 : if (init)
9463 0 : error ("assignment (not initialization) in declaration");
9464 0 : return;
9465 : }
9466 :
9467 329229684 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9468 : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9469 329229684 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
9470 :
9471 329229684 : tree type = TREE_TYPE (decl);
9472 329229684 : if (type == error_mark_node)
9473 : return;
9474 :
9475 329229101 : if (VAR_P (decl) && is_copy_initialization (init))
9476 103423980 : flags |= LOOKUP_ONLYCONVERTING;
9477 :
9478 : /* Warn about register storage specifiers except when in GNU global
9479 : or local register variable extension. */
9480 329229101 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9481 : {
9482 1931 : if (cxx_dialect >= cxx17)
9483 1282 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9484 : "ISO C++17 does not allow %<register%> storage "
9485 : "class specifier");
9486 : else
9487 649 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9488 : "%<register%> storage class specifier used");
9489 : }
9490 :
9491 : /* If a name was specified, get the string. */
9492 329229101 : if (at_namespace_scope_p ())
9493 72545841 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9494 329229101 : if (asmspec_tree && asmspec_tree != error_mark_node)
9495 963066 : asmspec = TREE_STRING_POINTER (asmspec_tree);
9496 :
9497 329229101 : bool in_class_decl
9498 329229101 : = (current_class_type
9499 206519943 : && CP_DECL_CONTEXT (decl) == current_class_type
9500 132458345 : && TYPE_BEING_DEFINED (current_class_type)
9501 459476084 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9502 :
9503 125086526 : if (in_class_decl
9504 125086526 : && (DECL_INITIAL (decl) || init))
9505 91892032 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9506 :
9507 329229101 : if (VAR_P (decl)
9508 329229101 : && (auto_node = type_uses_auto (type)))
9509 : {
9510 14493335 : tree d_init;
9511 14493335 : if (init == NULL_TREE)
9512 : {
9513 2721 : if (DECL_LANG_SPECIFIC (decl)
9514 2638 : && DECL_TEMPLATE_INSTANTIATION (decl)
9515 5356 : && !DECL_TEMPLATE_INSTANTIATED (decl))
9516 : {
9517 : /* init is null because we're deferring instantiating the
9518 : initializer until we need it. Well, we need it now. */
9519 2629 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9520 2629 : return;
9521 : }
9522 :
9523 92 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9524 : /* Class deduction with no initializer is OK. */;
9525 : else
9526 : {
9527 : /* Ordinary auto deduction without an initializer, a situation
9528 : which grokdeclarator already detects and rejects for the most
9529 : part. But we can still get here if we're instantiating a
9530 : variable template before we've fully parsed (and attached) its
9531 : initializer, e.g. template<class> auto x = x<int>; */
9532 3 : error_at (DECL_SOURCE_LOCATION (decl),
9533 : "declaration of %q#D has no initializer", decl);
9534 3 : TREE_TYPE (decl) = error_mark_node;
9535 3 : return;
9536 : }
9537 : }
9538 14490703 : d_init = init;
9539 14490703 : if (d_init)
9540 : {
9541 14490614 : if (TREE_CODE (d_init) == TREE_LIST
9542 14541402 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9543 27608 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9544 : tf_warning_or_error);
9545 14490614 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9546 : /* Force auto deduction now. Use tf_none to avoid redundant warnings
9547 : on deprecated-14.C. */
9548 14490614 : mark_single_function (d_init, tf_none);
9549 : }
9550 14490703 : enum auto_deduction_context adc = adc_variable_type;
9551 14490703 : if (DECL_DECOMPOSITION_P (decl))
9552 : adc = adc_decomp_type;
9553 14490703 : tree outer_targs = NULL_TREE;
9554 14490703 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9555 303 : && DECL_LANG_SPECIFIC (decl)
9556 126 : && DECL_TEMPLATE_INFO (decl)
9557 14490760 : && !DECL_FUNCTION_SCOPE_P (decl))
9558 : /* The outer template arguments might be needed for satisfaction.
9559 : (For function scope variables, do_auto_deduction will obtain the
9560 : outer template arguments from current_function_decl.) */
9561 57 : outer_targs = DECL_TI_ARGS (decl);
9562 14490703 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9563 : tf_warning_or_error, adc,
9564 : outer_targs, flags);
9565 14490703 : if (type == error_mark_node)
9566 : return;
9567 14489742 : if (TREE_CODE (type) == FUNCTION_TYPE)
9568 : {
9569 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
9570 : "did you forget the %<()%>?", decl);
9571 3 : TREE_TYPE (decl) = error_mark_node;
9572 3 : return;
9573 : }
9574 : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9575 14489739 : if (!processing_template_decl
9576 4445353 : && !type_uses_auto (type)
9577 18935085 : && !COMPLETE_TYPE_P (complete_type (type)))
9578 : {
9579 18 : auto_diagnostic_group d;
9580 18 : error_at (location_of (decl),
9581 : "deduced type %qT for %qD is incomplete", type, decl);
9582 18 : cxx_incomplete_type_inform (type);
9583 18 : TREE_TYPE (decl) = error_mark_node;
9584 18 : return;
9585 18 : }
9586 :
9587 : /* Now that we have a type, try these again. */
9588 14489721 : layout_decl (decl, 0);
9589 14489721 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9590 :
9591 : /* Update the type of the corresponding TEMPLATE_DECL to match. */
9592 14489721 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9593 : {
9594 34584 : tree tmpl = template_for_substitution (decl);
9595 34584 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9596 30987 : TREE_TYPE (tmpl) = type;
9597 : }
9598 : }
9599 :
9600 329225487 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9601 : {
9602 65 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9603 65 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9604 : {
9605 6 : init = NULL_TREE;
9606 6 : DECL_EXTERNAL (decl) = 1;
9607 : }
9608 : }
9609 :
9610 329225487 : if (VAR_P (decl)
9611 131128181 : && DECL_CLASS_SCOPE_P (decl)
9612 19576162 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
9613 : TCTX_STATIC_STORAGE, type)
9614 348801649 : && DECL_INITIALIZED_IN_CLASS_P (decl))
9615 18276707 : check_static_variable_definition (decl, type);
9616 :
9617 329225487 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
9618 : {
9619 40300169 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9620 40300169 : ? TCTX_THREAD_STORAGE
9621 : : TCTX_STATIC_STORAGE);
9622 40300169 : verify_type_context (input_location, context, TREE_TYPE (decl));
9623 : }
9624 :
9625 329225487 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
9626 : {
9627 549985 : tree clone;
9628 549985 : if (init == ridpointers[(int)RID_DELETE]
9629 549985 : || (TREE_CODE (init) == STRING_CST
9630 30 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9631 : {
9632 : /* FIXME check this is 1st decl. */
9633 528245 : if (UNLIKELY (DECL_MAIN_P (decl)))
9634 : {
9635 : /* [basic.start.main]/3: A program that defines main as deleted
9636 : is ill-formed. */
9637 3 : error ("%<::main%> cannot be deleted");
9638 3 : DECL_INITIAL (decl) = NULL_TREE;
9639 : }
9640 : else
9641 : {
9642 528242 : DECL_DELETED_FN (decl) = 1;
9643 528242 : DECL_DECLARED_INLINE_P (decl) = 1;
9644 528242 : DECL_INITIAL (decl)
9645 528242 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9646 528260 : FOR_EACH_CLONE (clone, decl)
9647 : {
9648 18 : DECL_DELETED_FN (clone) = 1;
9649 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9650 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9651 : }
9652 : }
9653 528245 : init = NULL_TREE;
9654 : }
9655 21740 : else if (init == ridpointers[(int)RID_DEFAULT])
9656 : {
9657 21728 : if (defaultable_fn_check (decl))
9658 21704 : DECL_DEFAULTED_FN (decl) = 1;
9659 : else
9660 24 : DECL_INITIAL (decl) = NULL_TREE;
9661 : }
9662 : }
9663 :
9664 329225487 : if (init && VAR_P (decl))
9665 : {
9666 113811100 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9667 : /* If DECL is a reference, then we want to know whether init is a
9668 : reference constant; init_const_expr_p as passed tells us whether
9669 : it's an rvalue constant. */
9670 113811100 : if (TYPE_REF_P (type))
9671 2385480 : init_const_expr_p = potential_constant_expression (init);
9672 113811100 : if (init_const_expr_p)
9673 : {
9674 : /* Set these flags now for templates. We'll update the flags in
9675 : store_init_value for instantiations. */
9676 99474834 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9677 99474834 : if (decl_maybe_constant_var_p (decl)
9678 : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9679 99474834 : && !TYPE_REF_P (type))
9680 44644776 : TREE_CONSTANT (decl) = true;
9681 : }
9682 : /* This is handled mostly by gimplify.cc, but we have to deal with
9683 : not warning about int x = x; as it is a GCC extension to turn off
9684 : this warning but only if warn_init_self is zero. */
9685 113811100 : if (!DECL_EXTERNAL (decl)
9686 112253780 : && !TREE_STATIC (decl)
9687 74990805 : && decl == tree_strip_any_location_wrapper (init)
9688 113814920 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9689 3796 : suppress_warning (decl, OPT_Winit_self);
9690 : }
9691 215414387 : else if (VAR_P (decl)
9692 17317081 : && COMPLETE_TYPE_P (type)
9693 13904965 : && !TYPE_REF_P (type)
9694 13904600 : && !dependent_type_p (type)
9695 229027803 : && is_really_empty_class (type, /*ignore_vptr*/false))
9696 : /* We have no initializer but there's nothing to initialize anyway.
9697 : Treat DECL as constant due to c++/109876. */
9698 567312 : TREE_CONSTANT (decl) = true;
9699 :
9700 329225487 : if (flag_openmp
9701 841797 : && TREE_CODE (decl) == FUNCTION_DECL
9702 : /* #pragma omp declare variant on methods handled in finish_struct
9703 : instead. */
9704 329588347 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9705 139420 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9706 223903 : if (tree attr = lookup_attribute ("omp declare variant base",
9707 223903 : DECL_ATTRIBUTES (decl)))
9708 970 : omp_declare_variant_finalize (decl, attr);
9709 :
9710 329225487 : if (processing_template_decl)
9711 : {
9712 181149091 : bool type_dependent_p;
9713 :
9714 : /* Add this declaration to the statement-tree. */
9715 181149091 : if (at_function_scope_p ())
9716 65498230 : add_decl_expr (decl);
9717 :
9718 181149091 : type_dependent_p = dependent_type_p (type);
9719 :
9720 181149091 : if (check_for_bare_parameter_packs (init))
9721 : {
9722 3 : init = NULL_TREE;
9723 3 : DECL_INITIAL (decl) = NULL_TREE;
9724 : }
9725 :
9726 : /* Generally, initializers in templates are expanded when the
9727 : template is instantiated. But, if DECL is a variable constant
9728 : then it can be used in future constant expressions, so its value
9729 : must be available. */
9730 :
9731 181149091 : bool dep_init = false;
9732 :
9733 181149091 : if (!VAR_P (decl) || type_dependent_p)
9734 : /* We can't do anything if the decl has dependent type. */;
9735 22633447 : else if (init
9736 20990298 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9737 17077181 : && !TYPE_REF_P (type)
9738 17045813 : && decl_maybe_constant_var_p (decl)
9739 31334567 : && !(dep_init = value_dependent_init_p (init)))
9740 : {
9741 : /* This variable seems to be a non-dependent constant, so process
9742 : its initializer. If check_initializer returns non-null the
9743 : initialization wasn't constant after all. */
9744 2247510 : tree init_code;
9745 2247510 : cleanups = make_tree_vector ();
9746 2247510 : init_code = check_initializer (decl, init, flags, &cleanups);
9747 2247510 : if (init_code == NULL_TREE)
9748 2247510 : init = NULL_TREE;
9749 2247510 : release_tree_vector (cleanups);
9750 : }
9751 : else
9752 : {
9753 20385937 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9754 : /* Try to deduce array size. */
9755 20385937 : maybe_deduce_size_from_array_init (decl, init);
9756 : /* And complain about multiple initializers. */
9757 18742788 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9758 20501376 : && !MAYBE_CLASS_TYPE_P (type))
9759 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9760 : tf_warning_or_error);
9761 : }
9762 :
9763 181149091 : if (init)
9764 54151731 : DECL_INITIAL (decl) = init;
9765 :
9766 181149091 : if (dep_init)
9767 : {
9768 6453610 : retrofit_lang_decl (decl);
9769 6453610 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9770 : }
9771 :
9772 181149091 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9773 : {
9774 9 : if (TREE_ADDRESSABLE (decl))
9775 3 : error_at (DECL_SOURCE_LOCATION (decl),
9776 : "address of explicit register variable %qD requested",
9777 : decl);
9778 : else
9779 : {
9780 6 : set_user_assembler_name (decl, asmspec);
9781 6 : DECL_HARD_REGISTER (decl) = 1;
9782 : }
9783 : }
9784 181149091 : return;
9785 : }
9786 :
9787 : /* Just store non-static data member initializers for later. */
9788 148076396 : if (init && TREE_CODE (decl) == FIELD_DECL)
9789 578788 : DECL_INITIAL (decl) = init;
9790 :
9791 : /* Take care of TYPE_DECLs up front. */
9792 148076396 : if (TREE_CODE (decl) == TYPE_DECL)
9793 : {
9794 11678660 : if (type != error_mark_node
9795 11678660 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9796 : {
9797 5842158 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9798 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9799 5842158 : set_identifier_type_value (DECL_NAME (decl), decl);
9800 : }
9801 :
9802 : /* If we have installed this as the canonical typedef for this
9803 : type, and that type has not been defined yet, delay emitting
9804 : the debug information for it, as we will emit it later. */
9805 11678660 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9806 11678660 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9807 656186 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9808 :
9809 11678660 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9810 : at_eof);
9811 11678660 : return;
9812 : }
9813 :
9814 : /* A reference will be modified here, as it is initialized. */
9815 136397736 : if (! DECL_EXTERNAL (decl)
9816 72468912 : && TREE_READONLY (decl)
9817 178285525 : && TYPE_REF_P (type))
9818 : {
9819 300653 : was_readonly = 1;
9820 300653 : TREE_READONLY (decl) = 0;
9821 : }
9822 :
9823 : /* This needs to happen before extend_ref_init_temps. */
9824 136397736 : if (VAR_OR_FUNCTION_DECL_P (decl))
9825 : {
9826 127422611 : if (VAR_P (decl))
9827 71339579 : maybe_commonize_var (decl);
9828 127422611 : determine_visibility (decl);
9829 : }
9830 :
9831 136397736 : if (VAR_P (decl))
9832 : {
9833 71339579 : duration_kind dk = decl_storage_duration (decl);
9834 : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9835 : only to a declaration of a variable with static or thread storage
9836 : duration." */
9837 71339579 : if (DECL_DECLARED_CONSTINIT_P (decl)
9838 71339579 : && !(dk == dk_thread || dk == dk_static))
9839 : {
9840 24 : error_at (DECL_SOURCE_LOCATION (decl),
9841 : "%<constinit%> can only be applied to a variable with "
9842 : "static or thread storage duration");
9843 24 : return;
9844 : }
9845 :
9846 71339555 : if (decomp)
9847 : {
9848 75345 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9849 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9850 : "%<constinit%> can be applied to structured binding "
9851 : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
9852 75345 : cp_maybe_mangle_decomp (decl, decomp);
9853 75345 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9854 : {
9855 500 : if (CP_DECL_THREAD_LOCAL_P (decl))
9856 37 : aggregates1 = tls_aggregates;
9857 : else
9858 463 : aggregates1 = static_aggregates;
9859 : }
9860 : }
9861 :
9862 : /* Detect stuff like 'info r = ^^int;' outside a manifestly
9863 : constant-evaluated context. */
9864 71339555 : check_out_of_consteval_use (decl);
9865 :
9866 : /* If this is a local variable that will need a mangled name,
9867 : register it now. We must do this before processing the
9868 : initializer for the variable, since the initialization might
9869 : require a guard variable, and since the mangled name of the
9870 : guard variable will depend on the mangled name of this
9871 : variable. */
9872 142679110 : if (DECL_FUNCTION_SCOPE_P (decl)
9873 31207448 : && TREE_STATIC (decl)
9874 168207 : && !DECL_ARTIFICIAL (decl)
9875 71506833 : && !consteval_only_p (decl))
9876 : {
9877 : /* The variable holding an anonymous union will have had its
9878 : discriminator set in finish_anon_union, after which it's
9879 : NAME will have been cleared. */
9880 167211 : if (DECL_NAME (decl))
9881 167166 : determine_local_discriminator (decl);
9882 : /* Normally has_forced_label_in_static is set during GIMPLE
9883 : lowering, but [cd]tors are never actually compiled directly.
9884 : We need to set this early so we can deal with the label
9885 : address extension. */
9886 167211 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
9887 167182 : || DECL_DESTRUCTOR_P (current_function_decl))
9888 167221 : && init)
9889 : {
9890 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
9891 29 : add_local_decl (cfun, decl);
9892 : }
9893 : /* And make sure it's in the symbol table for
9894 : c_parse_final_cleanups to find. */
9895 167211 : varpool_node::get_create (decl);
9896 : }
9897 :
9898 71339555 : if (flag_openmp
9899 192639 : && VAR_P (decl)
9900 192639 : && DECL_LANG_SPECIFIC (decl)
9901 115338 : && DECL_OMP_DECLARE_MAPPER_P (decl)
9902 71339559 : && init)
9903 : {
9904 3 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9905 3 : DECL_INITIAL (decl) = init;
9906 : }
9907 : /* Convert the initializer to the type of DECL, if we have not
9908 : already initialized DECL. */
9909 71339552 : else if (!DECL_INITIALIZED_P (decl)
9910 : /* If !DECL_EXTERNAL then DECL is being defined. In the
9911 : case of a static data member initialized inside the
9912 : class-specifier, there can be an initializer even if DECL
9913 : is *not* defined. */
9914 71339552 : && (!DECL_EXTERNAL (decl) || init))
9915 : {
9916 64476421 : cleanups = make_tree_vector ();
9917 64476421 : init = check_initializer (decl, init, flags, &cleanups);
9918 :
9919 : /* Handle:
9920 :
9921 : [dcl.init]
9922 :
9923 : The memory occupied by any object of static storage
9924 : duration is zero-initialized at program startup before
9925 : any other initialization takes place.
9926 :
9927 : We cannot create an appropriate initializer until after
9928 : the type of DECL is finalized. If DECL_INITIAL is set,
9929 : then the DECL is statically initialized, and any
9930 : necessary zero-initialization has already been performed. */
9931 64473724 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9932 497271 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9933 : /*nelts=*/NULL_TREE,
9934 : /*static_storage_p=*/true);
9935 : /* Remember that the initialization for this variable has
9936 : taken place. */
9937 64473724 : DECL_INITIALIZED_P (decl) = 1;
9938 : /* This declaration is the definition of this variable,
9939 : unless we are initializing a static data member within
9940 : the class specifier. */
9941 64473724 : if (!DECL_EXTERNAL (decl))
9942 71336858 : var_definition_p = true;
9943 : }
9944 : /* If the variable has an array type, lay out the type, even if
9945 : there is no initializer. It is valid to index through the
9946 : array, and we must get TYPE_ALIGN set correctly on the array
9947 : type. */
9948 6863131 : else if (TREE_CODE (type) == ARRAY_TYPE)
9949 207909 : layout_type (type);
9950 :
9951 71336858 : if (TREE_STATIC (decl)
9952 39754991 : && !at_function_scope_p ()
9953 110923642 : && current_function_decl == NULL)
9954 : /* So decl is a global variable or a static member of a
9955 : non local class. Record the types it uses
9956 : so that we can decide later to emit debug info for them. */
9957 39586772 : record_types_used_by_current_var_decl (decl);
9958 : }
9959 :
9960 : /* Add this declaration to the statement-tree. This needs to happen
9961 : after the call to check_initializer so that the DECL_EXPR for a
9962 : reference temp is added before the DECL_EXPR for the reference itself. */
9963 136395015 : if (DECL_FUNCTION_SCOPE_P (decl))
9964 : {
9965 : /* If we're building a variable sized type, and we might be
9966 : reachable other than via the top of the current binding
9967 : level, then create a new BIND_EXPR so that we deallocate
9968 : the object at the right time. */
9969 31207448 : if (VAR_P (decl)
9970 31207448 : && DECL_SIZE (decl)
9971 31148285 : && !TREE_CONSTANT (DECL_SIZE (decl))
9972 31208478 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9973 : {
9974 3 : tree bind;
9975 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9976 3 : TREE_SIDE_EFFECTS (bind) = 1;
9977 3 : add_stmt (bind);
9978 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
9979 : }
9980 31207448 : add_decl_expr (decl);
9981 : }
9982 :
9983 136395015 : tree decomp_init = NULL_TREE;
9984 : /* Let the middle end know about variables and functions -- but not
9985 : static data members in uninstantiated class templates. */
9986 136395015 : if (VAR_OR_FUNCTION_DECL_P (decl))
9987 : {
9988 127419890 : if (VAR_P (decl))
9989 : {
9990 71336858 : layout_var_decl (decl);
9991 71336858 : if (!flag_weak)
9992 : /* Check again now that we have an initializer. */
9993 57 : maybe_commonize_var (decl);
9994 : /* A class-scope constexpr variable with an out-of-class declaration.
9995 : C++17 makes them implicitly inline, but still force it out. */
9996 93286009 : if (DECL_INLINE_VAR_P (decl)
9997 31760908 : && !DECL_VAR_DECLARED_INLINE_P (decl)
9998 12704726 : && !DECL_TEMPLATE_INSTANTIATION (decl)
9999 49505685 : && !in_class_decl)
10000 103 : mark_needed (decl);
10001 : }
10002 :
10003 127419890 : if (var_definition_p
10004 : /* With -fmerge-all-constants, gimplify_init_constructor
10005 : might add TREE_STATIC to aggregate variables. */
10006 63340800 : && (TREE_STATIC (decl)
10007 31039386 : || (flag_merge_constants >= 2
10008 38 : && AGGREGATE_TYPE_P (type))))
10009 : {
10010 : /* If a TREE_READONLY variable needs initialization
10011 : at runtime, it is no longer readonly and we need to
10012 : avoid MEM_READONLY_P being set on RTL created for it. */
10013 32301417 : if (init)
10014 : {
10015 14008 : if (TREE_READONLY (decl))
10016 510 : TREE_READONLY (decl) = 0;
10017 : was_readonly = 0;
10018 : }
10019 32287409 : else if (was_readonly)
10020 2331 : TREE_READONLY (decl) = 1;
10021 :
10022 : /* Likewise if it needs destruction. */
10023 32301417 : if (!decl_maybe_constant_destruction (decl, type))
10024 3280 : TREE_READONLY (decl) = 0;
10025 : }
10026 95118473 : else if (VAR_P (decl)
10027 39035441 : && CP_DECL_THREAD_LOCAL_P (decl)
10028 18508 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
10029 18508 : && (was_readonly || TREE_READONLY (decl))
10030 95118488 : && var_needs_tls_wrapper (decl))
10031 : {
10032 : /* TLS variables need dynamic initialization by the TLS wrapper
10033 : function, we don't want to hoist accesses to it before the
10034 : wrapper. */
10035 6 : was_readonly = 0;
10036 6 : TREE_READONLY (decl) = 0;
10037 : }
10038 :
10039 127419890 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
10040 :
10041 : /* Check for abstractness of the type. */
10042 127419890 : if (var_definition_p)
10043 63340800 : abstract_virtuals_error (decl, type);
10044 :
10045 127419890 : if (decomp && !cp_finish_decomp (decl, decomp, true))
10046 9943 : decomp = NULL;
10047 :
10048 127419890 : if (TREE_TYPE (decl) == error_mark_node)
10049 : /* No initialization required. */
10050 : ;
10051 127419820 : else if (TREE_CODE (decl) == FUNCTION_DECL)
10052 : {
10053 56083032 : if (init)
10054 : {
10055 584 : if (init == ridpointers[(int)RID_DEFAULT])
10056 : {
10057 : /* An out-of-class default definition is defined at
10058 : the point where it is explicitly defaulted. */
10059 572 : if (DECL_DELETED_FN (decl))
10060 6 : maybe_explain_implicit_delete (decl);
10061 566 : else if (DECL_INITIAL (decl) == error_mark_node)
10062 542 : synthesize_method (decl);
10063 : }
10064 : else
10065 24 : error_at (cp_expr_loc_or_loc (init,
10066 12 : DECL_SOURCE_LOCATION (decl)),
10067 : "function %q#D is initialized like a variable",
10068 : decl);
10069 : }
10070 : /* else no initialization required. */
10071 : }
10072 71336788 : else if (DECL_EXTERNAL (decl)
10073 71336788 : && ! (DECL_LANG_SPECIFIC (decl)
10074 7696624 : && DECL_NOT_REALLY_EXTERN (decl)))
10075 : {
10076 : /* check_initializer will have done any constant initialization. */
10077 : }
10078 : /* A variable definition. */
10079 65263041 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
10080 : {
10081 : /* Initialize the local variable. */
10082 31039223 : if (!decomp)
10083 30974066 : initialize_local_var (decl, init, false);
10084 : else
10085 : {
10086 65157 : tree cleanup = NULL_TREE;
10087 65157 : if (DECL_SIZE (decl))
10088 65157 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10089 : /* If cp_finish_decomp needs to emit any code, we need to emit that
10090 : code after code emitted by initialize_local_var in a single
10091 : CLEANUP_POINT_EXPR, so that temporaries are destructed only
10092 : after the cp_finish_decomp emitted code.
10093 : If there are any cleanups, either extend_ref_init_temps
10094 : created ones or e.g. array destruction, push those first
10095 : with the cleanups guarded on a bool temporary, initially
10096 : set to false and set to true after initialize_local_var
10097 : emitted code. */
10098 65157 : tree guard = NULL_TREE;
10099 65157 : if (cleanups || cleanup)
10100 : {
10101 65157 : guard = get_internal_target_expr (boolean_false_node);
10102 65157 : add_stmt (guard);
10103 65157 : guard = TARGET_EXPR_SLOT (guard);
10104 : }
10105 65157 : tree sl = push_stmt_list ();
10106 65157 : initialize_local_var (decl, init, true);
10107 65157 : if (guard)
10108 : {
10109 65157 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
10110 : guard, boolean_true_node));
10111 65175 : for (tree &t : *cleanups)
10112 18 : t = build3 (COND_EXPR, void_type_node,
10113 : guard, t, void_node);
10114 65157 : if (cleanup)
10115 25 : cleanup = build3 (COND_EXPR, void_type_node,
10116 : guard, cleanup, void_node);
10117 : }
10118 65157 : unsigned before = stmt_list_stack->length ();
10119 65157 : cp_finish_decomp (decl, decomp);
10120 65157 : decomp = NULL;
10121 65157 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
10122 65157 : sl = pop_stmt_list (sl);
10123 65157 : if (n_extra_cleanups)
10124 : {
10125 : /* If cp_finish_decomp needs any cleanups, such as for
10126 : extend_ref_init_temps created vars, pop_stmt_list
10127 : popped that all, so push those extra cleanups around
10128 : the whole sequence with a guard variable. */
10129 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
10130 15 : guard = get_internal_target_expr (integer_zero_node);
10131 15 : add_stmt (guard);
10132 15 : guard = TARGET_EXPR_SLOT (guard);
10133 63 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
10134 : {
10135 48 : tree_stmt_iterator tsi = tsi_last (sl);
10136 48 : gcc_assert (!tsi_end_p (tsi));
10137 48 : tree last = tsi_stmt (tsi);
10138 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
10139 : && !CLEANUP_EH_ONLY (last));
10140 48 : tree cst = build_int_cst (integer_type_node, i + 1);
10141 96 : tree cl = build3 (COND_EXPR, void_type_node,
10142 : build2 (GE_EXPR, boolean_type_node,
10143 : guard, cst),
10144 48 : CLEANUP_EXPR (last), void_node);
10145 48 : extra_cleanups.safe_push (cl);
10146 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
10147 : integer_type_node,
10148 : guard, cst),
10149 : TSI_SAME_STMT);
10150 48 : tree sl2 = CLEANUP_BODY (last);
10151 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10152 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10153 48 : tsi_delink (&tsi);
10154 : }
10155 : }
10156 65157 : decomp_init = maybe_cleanup_point_expr_void (sl);
10157 65157 : if (cleanup)
10158 25 : finish_decl_cleanup (decl, cleanup);
10159 : }
10160 : }
10161 :
10162 : /* If a variable is defined, and then a subsequent
10163 : definition with external linkage is encountered, we will
10164 : get here twice for the same variable. We want to avoid
10165 : calling expand_static_init more than once. For variables
10166 : that are not static data members, we can call
10167 : expand_static_init only when we actually process the
10168 : initializer. It is not legal to redeclare a static data
10169 : member, so this issue does not arise in that case. */
10170 34223818 : else if (var_definition_p && TREE_STATIC (decl))
10171 : {
10172 32300674 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10173 : {
10174 100 : tree sl = push_stmt_list ();
10175 100 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10176 100 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10177 100 : expand_static_init (decl, init);
10178 100 : current_stmt_tree ()->stmts_are_full_exprs_p
10179 100 : = saved_stmts_are_full_exprs_p;
10180 100 : cp_finish_decomp (decl, decomp);
10181 100 : decomp = NULL;
10182 100 : sl = pop_stmt_list (sl);
10183 100 : sl = maybe_cleanup_point_expr_void (sl);
10184 100 : add_stmt (sl);
10185 : }
10186 : else
10187 32300574 : expand_static_init (decl, init);
10188 : }
10189 : }
10190 :
10191 : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10192 : reference, insert it in the statement-tree now. */
10193 136395015 : if (cleanups)
10194 : {
10195 64492993 : for (tree t : *cleanups)
10196 : {
10197 19269 : push_cleanup (NULL_TREE, t, false);
10198 : /* As in initialize_local_var. */
10199 19269 : wrap_temporary_cleanups (init, t);
10200 : }
10201 64473724 : release_tree_vector (cleanups);
10202 : }
10203 :
10204 136395093 : for (tree t : &extra_cleanups)
10205 48 : push_cleanup (NULL_TREE, t, false);
10206 :
10207 136395015 : if (decomp_init)
10208 65157 : add_stmt (decomp_init);
10209 :
10210 136395015 : if (decomp
10211 145 : && var_definition_p
10212 145 : && TREE_STATIC (decl)
10213 136395160 : && !DECL_FUNCTION_SCOPE_P (decl))
10214 : {
10215 145 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10216 145 : ? tls_aggregates : static_aggregates);
10217 145 : tree aggregates2 = aggregates3;
10218 145 : if (aggregates2 != aggregates1)
10219 : {
10220 66 : cp_finish_decomp (decl, decomp);
10221 66 : decomp = NULL;
10222 66 : if (aggregates3 != aggregates2)
10223 : {
10224 : /* If there are dynamic initializers for the structured
10225 : binding base or associated extended ref temps and also
10226 : dynamic initializers for the structured binding non-base
10227 : vars, mark them. */
10228 315 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10229 252 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10230 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10231 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
10232 : }
10233 : }
10234 : }
10235 :
10236 136395015 : if (was_readonly)
10237 300072 : TREE_READONLY (decl) = 1;
10238 :
10239 136395015 : if (flag_openmp
10240 435750 : && VAR_P (decl)
10241 136587654 : && lookup_attribute ("omp declare target implicit",
10242 192639 : DECL_ATTRIBUTES (decl)))
10243 : {
10244 52 : DECL_ATTRIBUTES (decl)
10245 52 : = remove_attribute ("omp declare target implicit",
10246 52 : DECL_ATTRIBUTES (decl));
10247 52 : complete_type (TREE_TYPE (decl));
10248 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
10249 : {
10250 30 : auto_diagnostic_group d;
10251 30 : error ("%q+D in declare target directive does not have mappable"
10252 : " type", decl);
10253 30 : if (TREE_TYPE (decl) != error_mark_node
10254 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10255 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
10256 30 : }
10257 22 : else if (!lookup_attribute ("omp declare target",
10258 22 : DECL_ATTRIBUTES (decl))
10259 44 : && !lookup_attribute ("omp declare target link",
10260 22 : DECL_ATTRIBUTES (decl)))
10261 : {
10262 22 : DECL_ATTRIBUTES (decl)
10263 22 : = tree_cons (get_identifier ("omp declare target"),
10264 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
10265 22 : symtab_node *node = symtab_node::get (decl);
10266 22 : if (node != NULL)
10267 : {
10268 19 : node->offloadable = 1;
10269 19 : if (ENABLE_OFFLOADING)
10270 : {
10271 : g->have_offload = true;
10272 : if (is_a <varpool_node *> (node))
10273 : vec_safe_push (offload_vars, decl);
10274 : }
10275 : }
10276 : }
10277 : }
10278 :
10279 : /* This is the last point we can lower alignment so give the target the
10280 : chance to do so. */
10281 136395015 : if (VAR_P (decl)
10282 71336858 : && !is_global_var (decl)
10283 167434401 : && !DECL_HARD_REGISTER (decl))
10284 31039333 : targetm.lower_local_decl_alignment (decl);
10285 :
10286 136395015 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
10287 329227027 : }
10288 :
10289 : /* For class TYPE return itself or some its bases that contain
10290 : any direct non-static data members. Return error_mark_node if an
10291 : error has been diagnosed. */
10292 :
10293 : static tree
10294 15496 : find_decomp_class_base (location_t loc, tree type, tree ret,
10295 : tsubst_flags_t complain)
10296 : {
10297 30992 : if (LAMBDA_TYPE_P (type))
10298 : {
10299 12 : if (complain & tf_error)
10300 : {
10301 12 : auto_diagnostic_group d;
10302 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
10303 12 : inform (location_of (type), "lambda declared here");
10304 12 : }
10305 12 : return error_mark_node;
10306 : }
10307 :
10308 15484 : bool member_seen = false;
10309 145830 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10310 229130 : if (TREE_CODE (field) != FIELD_DECL
10311 31770 : || DECL_ARTIFICIAL (field)
10312 162061 : || DECL_UNNAMED_BIT_FIELD (field))
10313 98745 : continue;
10314 31640 : else if (ret)
10315 : return type;
10316 31614 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10317 : {
10318 8 : if ((complain & tf_error) == 0)
10319 0 : return error_mark_node;
10320 8 : auto_diagnostic_group d;
10321 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10322 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10323 : "anonymous struct member", type);
10324 : else
10325 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10326 : "anonymous union member", type);
10327 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
10328 8 : return error_mark_node;
10329 8 : }
10330 31606 : else if (!accessible_p (type, field, true))
10331 : {
10332 5 : if ((complain & tf_error) == 0)
10333 0 : return error_mark_node;
10334 5 : auto_diagnostic_group d;
10335 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10336 : field, type);
10337 5 : inform (DECL_SOURCE_LOCATION (field),
10338 5 : TREE_PRIVATE (field)
10339 : ? G_("declared private here")
10340 : : G_("declared protected here"));
10341 5 : return error_mark_node;
10342 5 : }
10343 : else
10344 : member_seen = true;
10345 :
10346 15445 : tree base_binfo, binfo;
10347 15445 : tree orig_ret = ret;
10348 15445 : int i;
10349 15445 : if (member_seen)
10350 15348 : ret = type;
10351 15503 : for (binfo = TYPE_BINFO (type), i = 0;
10352 15503 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10353 : {
10354 78 : auto_diagnostic_group d;
10355 78 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10356 : complain);
10357 78 : if (t == error_mark_node)
10358 : {
10359 3 : if (complain & tf_error)
10360 3 : inform (location_of (type), "in base class of %qT", type);
10361 3 : return error_mark_node;
10362 : }
10363 75 : if (t != NULL_TREE && t != ret)
10364 : {
10365 48 : if (ret == type)
10366 : {
10367 10 : if (complain & tf_error)
10368 10 : error_at (loc, "cannot decompose class type %qT: both it and "
10369 : "its base class %qT have non-static data "
10370 : "members", type, t);
10371 10 : return error_mark_node;
10372 : }
10373 38 : else if (orig_ret != NULL_TREE)
10374 : return t;
10375 38 : else if (ret != NULL_TREE)
10376 : {
10377 7 : if (complain & tf_error)
10378 7 : error_at (loc, "cannot decompose class type %qT: its base "
10379 : "classes %qT and %qT have non-static data "
10380 : "members", type, ret, t);
10381 7 : return error_mark_node;
10382 : }
10383 : else
10384 : ret = t;
10385 : }
10386 78 : }
10387 : return ret;
10388 : }
10389 :
10390 : /* Return std::tuple_size<TYPE>::value. */
10391 :
10392 : static tree
10393 155683 : get_tuple_size (tree type)
10394 : {
10395 155683 : tree args = make_tree_vec (1);
10396 155683 : TREE_VEC_ELT (args, 0) = type;
10397 155683 : tree inst = lookup_template_class (tuple_size_identifier, args,
10398 : /*in_decl*/NULL_TREE,
10399 : /*context*/std_node,
10400 : tf_none);
10401 155683 : inst = complete_type (inst);
10402 155683 : if (inst == error_mark_node
10403 154224 : || !COMPLETE_TYPE_P (inst)
10404 295906 : || !CLASS_TYPE_P (inst))
10405 : return NULL_TREE;
10406 140223 : tree val = lookup_qualified_name (inst, value_identifier,
10407 : LOOK_want::NORMAL, /*complain*/false);
10408 140223 : if (val == error_mark_node)
10409 : return NULL_TREE;
10410 140217 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10411 140217 : val = maybe_constant_value (val, NULL_TREE, mce_true);
10412 140217 : if (TREE_CODE (val) == INTEGER_CST)
10413 : return val;
10414 : else
10415 6 : return error_mark_node;
10416 : }
10417 :
10418 : /* Return std::tuple_element<I,TYPE>::type. */
10419 :
10420 : static tree
10421 150388 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10422 : {
10423 150388 : tree args = make_tree_vec (2);
10424 150388 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10425 150388 : TREE_VEC_ELT (args, 1) = type;
10426 150388 : tree inst = lookup_template_class (tuple_element_identifier, args,
10427 : /*in_decl*/NULL_TREE,
10428 : /*context*/std_node,
10429 : tf_warning_or_error);
10430 150388 : return make_typename_type (inst, type_identifier,
10431 150388 : none_type, tf_warning_or_error);
10432 : }
10433 :
10434 : /* Return e.get<i>() or get<i>(e). */
10435 :
10436 : static tree
10437 150397 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10438 : {
10439 150397 : tree targs = make_tree_vec (1);
10440 150397 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10441 :
10442 150397 : tree etype = TREE_TYPE (decl);
10443 150397 : tree e = convert_from_reference (decl);
10444 :
10445 : /* [The id-expression] e is an lvalue if the type of the entity e is an
10446 : lvalue reference and an xvalue otherwise. */
10447 150397 : if (!TYPE_REF_P (etype)
10448 150397 : || TYPE_REF_IS_RVALUE (etype))
10449 149102 : e = move (e);
10450 :
10451 150397 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10452 : LOOK_want::NORMAL, /*complain*/false);
10453 150397 : bool use_member_get = false;
10454 :
10455 : /* To use a member get, member lookup must find at least one
10456 : declaration that is a function template
10457 : whose first template parameter is a non-type parameter. */
10458 448726 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10459 : {
10460 150397 : tree fn = *iter;
10461 150397 : if (TREE_CODE (fn) == TEMPLATE_DECL)
10462 : {
10463 2468 : tree tparms = DECL_TEMPLATE_PARMS (fn);
10464 2468 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10465 2468 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10466 : {
10467 : use_member_get = true;
10468 : break;
10469 : }
10470 : }
10471 : }
10472 :
10473 150397 : if (use_member_get)
10474 : {
10475 2465 : fns = lookup_template_function (fns, targs);
10476 2465 : return build_new_method_call (e, fns, /*args*/NULL,
10477 : /*path*/NULL_TREE, LOOKUP_NORMAL,
10478 2465 : /*fn_p*/NULL, tf_warning_or_error);
10479 : }
10480 : else
10481 : {
10482 147932 : releasing_vec args (make_tree_vector_single (e));
10483 147932 : fns = lookup_template_function (get__identifier, targs);
10484 147932 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10485 147932 : return finish_call_expr (fns, &args, /*novirt*/false,
10486 : /*koenig*/true, tf_warning_or_error);
10487 147932 : }
10488 : }
10489 :
10490 : /* It's impossible to recover the decltype of a tuple decomposition variable
10491 : based on the actual type of the variable, so store it in a hash table. */
10492 :
10493 : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10494 :
10495 : tree
10496 587 : lookup_decomp_type (tree v)
10497 : {
10498 587 : if (decomp_type_table)
10499 460 : if (tree *slot = decomp_type_table->get (v))
10500 396 : return *slot;
10501 : return NULL_TREE;
10502 : }
10503 :
10504 : /* Mangle a decomposition declaration if needed. Arguments like
10505 : in cp_finish_decomp. */
10506 :
10507 : static void
10508 75345 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10509 : {
10510 75345 : if (!processing_template_decl
10511 75345 : && !error_operand_p (decl)
10512 150690 : && TREE_STATIC (decl))
10513 : {
10514 761 : auto_vec<tree, 16> v;
10515 761 : v.safe_grow (decomp->count, true);
10516 761 : tree d = decomp->decl;
10517 2633 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10518 1872 : v[decomp->count - i - 1] = d;
10519 761 : if (DECL_FUNCTION_SCOPE_P (decl))
10520 : {
10521 : size_t sz = 3;
10522 915 : for (unsigned int i = 0; i < decomp->count; ++i)
10523 654 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10524 261 : char *name = XALLOCAVEC (char, sz);
10525 261 : name[0] = 'D';
10526 261 : name[1] = 'C';
10527 261 : char *p = name + 2;
10528 915 : for (unsigned int i = 0; i < decomp->count; ++i)
10529 : {
10530 654 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10531 654 : *p++ = ' ';
10532 654 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
10533 654 : p += len;
10534 : }
10535 261 : *p = '\0';
10536 261 : determine_local_discriminator (decl, get_identifier (name));
10537 : }
10538 761 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10539 761 : maybe_apply_pragma_weak (decl);
10540 761 : }
10541 75345 : }
10542 :
10543 : /* Append #i to DECL_NAME (decl) or for name independent decls
10544 : clear DECL_NAME (decl). */
10545 :
10546 : static void
10547 14776 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10548 : {
10549 14776 : if (name_independent_decl_p (decl))
10550 : /* Only "_" names are treated as name independent, "_#0" etc. is not and
10551 : because we pushdecl the individual decl elements of structured binding
10552 : pack, we could get redeclaration errors if there are 2 or more name
10553 : independent structured binding packs in the same scope. */
10554 312 : DECL_NAME (decl) = NULL_TREE;
10555 : else
10556 : {
10557 14464 : tree name = DECL_NAME (decl);
10558 14464 : size_t len = IDENTIFIER_LENGTH (name) + 22;
10559 14464 : char *n = XALLOCAVEC (char, len);
10560 14464 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10561 14464 : IDENTIFIER_POINTER (name), i);
10562 14464 : DECL_NAME (decl) = get_identifier (n);
10563 : }
10564 14776 : }
10565 :
10566 : /* Return structured binding size of TYPE or -1 if erroneous. */
10567 :
10568 : HOST_WIDE_INT
10569 512 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10570 : {
10571 512 : if (TYPE_REF_P (type))
10572 : {
10573 0 : type = complete_type (TREE_TYPE (type));
10574 0 : if (type == error_mark_node)
10575 : return -1;
10576 0 : if (!COMPLETE_TYPE_P (type))
10577 : {
10578 0 : if (complain & tf_error)
10579 0 : error_at (loc, "structured binding refers to incomplete type %qT",
10580 : type);
10581 0 : return -1;
10582 : }
10583 : }
10584 :
10585 512 : unsigned HOST_WIDE_INT eltscnt = 0;
10586 512 : if (TREE_CODE (type) == ARRAY_TYPE)
10587 : {
10588 306 : if (TYPE_DOMAIN (type) == NULL_TREE)
10589 : {
10590 9 : if (complain & tf_error)
10591 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
10592 : type);
10593 9 : return -1;
10594 : }
10595 297 : tree nelts = array_type_nelts_top (type);
10596 297 : if (nelts == error_mark_node)
10597 : return -1;
10598 297 : if (!tree_fits_shwi_p (nelts))
10599 : {
10600 3 : if (complain & tf_error)
10601 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10602 3 : return -1;
10603 : }
10604 294 : return tree_to_shwi (nelts);
10605 : }
10606 : /* 2 GNU extensions. */
10607 206 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10608 : return 2;
10609 200 : else if (TREE_CODE (type) == VECTOR_TYPE)
10610 : {
10611 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10612 : {
10613 : if (complain & tf_error)
10614 : error_at (loc, "cannot decompose variable length vector %qT", type);
10615 : return -1;
10616 : }
10617 12 : return eltscnt;
10618 : }
10619 188 : else if (tree tsize = get_tuple_size (type))
10620 : {
10621 29 : if (tsize == error_mark_node
10622 23 : || !tree_fits_shwi_p (tsize)
10623 52 : || tree_int_cst_sgn (tsize) < 0)
10624 : {
10625 9 : if (complain & tf_error)
10626 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10627 : "constant expression", type);
10628 9 : return -1;
10629 : }
10630 20 : return tree_to_shwi (tsize);
10631 : }
10632 159 : else if (TREE_CODE (type) == UNION_TYPE)
10633 : {
10634 3 : if (complain & tf_error)
10635 3 : error_at (loc, "cannot decompose union type %qT", type);
10636 3 : return -1;
10637 : }
10638 156 : else if (!CLASS_TYPE_P (type))
10639 : {
10640 21 : if (complain & tf_error)
10641 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10642 21 : return -1;
10643 : }
10644 135 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10645 : return -1;
10646 135 : else if (!COMPLETE_TYPE_P (type))
10647 : {
10648 3 : if (complain & tf_error)
10649 3 : error_at (loc, "structured binding refers to incomplete class type "
10650 : "%qT", type);
10651 3 : return -1;
10652 : }
10653 : else
10654 : {
10655 132 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10656 132 : if (btype == error_mark_node)
10657 : return -1;
10658 111 : else if (btype == NULL_TREE)
10659 : return 0;
10660 1034 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10661 1573 : if (TREE_CODE (field) != FIELD_DECL
10662 329 : || DECL_ARTIFICIAL (field)
10663 1274 : || DECL_UNNAMED_BIT_FIELD (field))
10664 628 : continue;
10665 : else
10666 317 : eltscnt++;
10667 89 : return eltscnt;
10668 : }
10669 : }
10670 :
10671 : /* Finish a decomposition declaration. DECL is the underlying declaration
10672 : "e", FIRST is the head of a chain of decls for the individual identifiers
10673 : chained through DECL_CHAIN in reverse order and COUNT is the number of
10674 : those decls. If TEST_P is true, return true if any code would need to be
10675 : actually emitted but don't emit it. Return false otherwise. */
10676 :
10677 : bool
10678 353704 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10679 : {
10680 353704 : tree first = decomp->decl;
10681 353704 : unsigned count = decomp->count;
10682 353704 : if (error_operand_p (decl))
10683 : {
10684 63 : error_out:
10685 616 : while (count--)
10686 : {
10687 420 : TREE_TYPE (first) = error_mark_node;
10688 420 : if (DECL_HAS_VALUE_EXPR_P (first))
10689 : {
10690 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10691 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10692 : }
10693 420 : first = DECL_CHAIN (first);
10694 : }
10695 196 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
10696 51 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10697 196 : return false;
10698 : }
10699 :
10700 353641 : location_t loc = DECL_SOURCE_LOCATION (decl);
10701 353641 : if (type_dependent_expression_p (decl)
10702 : /* This happens for range for when not in templates.
10703 : Still add the DECL_VALUE_EXPRs for later processing. */
10704 353641 : || (!processing_template_decl
10705 141021 : && type_uses_auto (TREE_TYPE (decl))))
10706 : {
10707 582460 : for (unsigned int i = 0; i < count; i++)
10708 : {
10709 388239 : if (!DECL_HAS_VALUE_EXPR_P (first))
10710 : {
10711 388239 : tree v = build_nt (ARRAY_REF, decl,
10712 388239 : size_int (count - i - 1),
10713 : NULL_TREE, NULL_TREE);
10714 388239 : SET_DECL_VALUE_EXPR (first, v);
10715 388239 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10716 : }
10717 388239 : if (processing_template_decl)
10718 387615 : fit_decomposition_lang_decl (first, decl);
10719 388239 : first = DECL_CHAIN (first);
10720 : }
10721 : return false;
10722 : }
10723 :
10724 159420 : auto_vec<tree, 16> v;
10725 159420 : v.safe_grow (count, true);
10726 159420 : tree d = first;
10727 159420 : int pack = -1;
10728 477287 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10729 : {
10730 317867 : v[count - i - 1] = d;
10731 317867 : fit_decomposition_lang_decl (d, decl);
10732 317867 : if (DECL_PACK_P (d))
10733 3853 : pack = count - i - 1;
10734 : }
10735 :
10736 159420 : tree type = TREE_TYPE (decl);
10737 159420 : tree dexp = decl;
10738 :
10739 159420 : if (TYPE_REF_P (type))
10740 : {
10741 1848 : dexp = convert_from_reference (dexp);
10742 1848 : type = complete_type (TREE_TYPE (type));
10743 1848 : if (type == error_mark_node)
10744 133 : goto error_out;
10745 1848 : if (!COMPLETE_TYPE_P (type))
10746 : {
10747 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10748 : type);
10749 3 : goto error_out;
10750 : }
10751 : }
10752 :
10753 159417 : tree eltype = NULL_TREE;
10754 159417 : unsigned HOST_WIDE_INT eltscnt = 0;
10755 : /* Structured binding packs when initializer is non-dependent should
10756 : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10757 : of that TREE_VEC are the base and index, what is normally represented
10758 : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10759 : of the pack first element. The remaining elements of the TREE_VEC
10760 : are VAR_DECLs for the pack elements. */
10761 159417 : tree packv = NULL_TREE;
10762 :
10763 159417 : if (TREE_CODE (type) == ARRAY_TYPE)
10764 : {
10765 3784 : tree nelts;
10766 3784 : nelts = array_type_nelts_top (type);
10767 3784 : if (nelts == error_mark_node)
10768 0 : goto error_out;
10769 3784 : if (DECL_DECOMP_BASE (decl))
10770 : {
10771 9 : error_at (loc, "array initializer for structured binding "
10772 : "declaration in condition");
10773 9 : goto error_out;
10774 : }
10775 3775 : if (!tree_fits_uhwi_p (nelts))
10776 : {
10777 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10778 3 : goto error_out;
10779 : }
10780 3772 : eltscnt = tree_to_uhwi (nelts);
10781 3772 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10782 : {
10783 11 : cnt_mismatch:
10784 38 : auto_diagnostic_group d;
10785 38 : if (count > eltscnt)
10786 27 : error_n (loc, count,
10787 : "%u name provided for structured binding",
10788 : "%u names provided for structured binding", count);
10789 : else
10790 11 : error_n (loc, count,
10791 : "only %u name provided for structured binding",
10792 : "only %u names provided for structured binding", count);
10793 38 : inform_n (loc, eltscnt,
10794 : "while %qT decomposes into %wu element",
10795 : "while %qT decomposes into %wu elements",
10796 : type, eltscnt);
10797 38 : goto error_out;
10798 : }
10799 3761 : eltype = TREE_TYPE (type);
10800 8942 : for (unsigned int i = 0; i < count; i++)
10801 : {
10802 5181 : if ((unsigned) pack == i)
10803 : {
10804 2955 : packv = make_tree_vec (eltscnt - count + 3);
10805 15739 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10806 : {
10807 12784 : tree t;
10808 12784 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10809 12784 : set_sb_pack_name (t, j);
10810 12784 : maybe_push_decl (t);
10811 12784 : TREE_TYPE (t) = eltype;
10812 12784 : layout_decl (t, 0);
10813 12784 : if (!processing_template_decl)
10814 : {
10815 12697 : tree a = unshare_expr (dexp);
10816 12697 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10817 : NULL_TREE, NULL_TREE);
10818 12697 : SET_DECL_VALUE_EXPR (t, a);
10819 12697 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10820 : }
10821 : }
10822 5181 : continue;
10823 2955 : }
10824 2226 : TREE_TYPE (v[i]) = eltype;
10825 2226 : layout_decl (v[i], 0);
10826 2226 : if (processing_template_decl)
10827 136 : continue;
10828 2090 : tree t = unshare_expr (dexp);
10829 2090 : unsigned HOST_WIDE_INT j = i;
10830 2090 : if (pack != -1 && (unsigned) pack < i)
10831 64 : j = i + eltscnt - count;
10832 2090 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10833 2090 : SET_DECL_VALUE_EXPR (v[i], t);
10834 2090 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10835 : }
10836 : }
10837 : /* 2 GNU extensions. */
10838 155633 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10839 : {
10840 78 : eltscnt = 2;
10841 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10842 3 : goto cnt_mismatch;
10843 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10844 213 : for (unsigned int i = 0; i < count; i++)
10845 : {
10846 138 : if ((unsigned) pack == i)
10847 : {
10848 60 : packv = make_tree_vec (eltscnt - count + 3);
10849 132 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10850 : {
10851 72 : tree t;
10852 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10853 72 : set_sb_pack_name (t, j);
10854 72 : maybe_push_decl (t);
10855 72 : TREE_TYPE (t) = eltype;
10856 72 : layout_decl (t, 0);
10857 72 : if (!processing_template_decl)
10858 : {
10859 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
10860 : unshare_expr (dexp));
10861 48 : SET_DECL_VALUE_EXPR (t, a);
10862 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10863 : }
10864 : }
10865 60 : continue;
10866 60 : }
10867 78 : TREE_TYPE (v[i]) = eltype;
10868 78 : layout_decl (v[i], 0);
10869 78 : if (processing_template_decl)
10870 16 : continue;
10871 62 : tree t = unshare_expr (dexp);
10872 62 : unsigned HOST_WIDE_INT j = i;
10873 62 : if (pack != -1 && (unsigned) pack < i)
10874 16 : j = i + eltscnt - count;
10875 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
10876 62 : SET_DECL_VALUE_EXPR (v[i], t);
10877 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10878 : }
10879 : }
10880 155555 : else if (TREE_CODE (type) == VECTOR_TYPE)
10881 : {
10882 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10883 : {
10884 : error_at (loc, "cannot decompose variable length vector %qT", type);
10885 : goto error_out;
10886 : }
10887 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10888 3 : goto cnt_mismatch;
10889 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10890 249 : for (unsigned int i = 0; i < count; i++)
10891 : {
10892 192 : if ((unsigned) pack == i)
10893 : {
10894 48 : packv = make_tree_vec (eltscnt - count + 3);
10895 708 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10896 : {
10897 660 : tree t;
10898 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10899 660 : set_sb_pack_name (t, j);
10900 660 : maybe_push_decl (t);
10901 660 : TREE_TYPE (t) = eltype;
10902 660 : layout_decl (t, 0);
10903 660 : if (!processing_template_decl)
10904 : {
10905 440 : tree a = unshare_expr (dexp);
10906 440 : location_t loc = DECL_SOURCE_LOCATION (t);
10907 440 : tree s = size_int (j + pack);
10908 440 : convert_vector_to_array_for_subscript (loc, &a, s);
10909 440 : a = build4 (ARRAY_REF, eltype, a, s,
10910 : NULL_TREE, NULL_TREE);
10911 440 : SET_DECL_VALUE_EXPR (t, a);
10912 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10913 : }
10914 : }
10915 84 : continue;
10916 48 : }
10917 144 : TREE_TYPE (v[i]) = eltype;
10918 144 : layout_decl (v[i], 0);
10919 144 : if (processing_template_decl)
10920 36 : continue;
10921 108 : tree t = unshare_expr (dexp);
10922 108 : unsigned HOST_WIDE_INT j = i;
10923 108 : if (pack != -1 && (unsigned) pack < i)
10924 24 : j = i + eltscnt - count;
10925 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10926 108 : &t, size_int (j));
10927 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10928 108 : SET_DECL_VALUE_EXPR (v[i], t);
10929 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10930 : }
10931 : }
10932 155495 : else if (tree tsize = get_tuple_size (type))
10933 : {
10934 140188 : if (tsize == error_mark_node)
10935 : {
10936 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10937 : "constant expression", type);
10938 0 : goto error_out;
10939 : }
10940 140188 : if (!tree_fits_uhwi_p (tsize))
10941 : {
10942 12 : auto_diagnostic_group d;
10943 12 : error_n (loc, count,
10944 : "%u name provided for structured binding",
10945 : "%u names provided for structured binding", count);
10946 12 : inform (loc, "while %qT decomposes into %E elements",
10947 : type, tsize);
10948 12 : goto error_out;
10949 12 : }
10950 140176 : eltscnt = tree_to_uhwi (tsize);
10951 140176 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10952 6 : goto cnt_mismatch;
10953 140170 : if (test_p && eltscnt)
10954 159287 : return true;
10955 74768 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
10956 : {
10957 : /* For structured bindings used in conditions we need to evaluate
10958 : the conversion of decl (aka e in the standard) to bool or
10959 : integral/enumeral type (the latter for switch conditions)
10960 : before the get methods. */
10961 86 : tree cond = convert_from_reference (decl);
10962 86 : if (integer_onep (DECL_DECOMP_BASE (decl)))
10963 : /* switch condition. */
10964 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10965 : cond, true);
10966 : else
10967 : /* if/while/for condition. */
10968 68 : cond = contextual_conv_bool (cond, tf_warning_or_error);
10969 86 : if (cond && !error_operand_p (cond))
10970 : {
10971 : /* Wrap that value into a TARGET_EXPR, emit it right
10972 : away and save for later uses in the cp_parse_condition
10973 : or its instantiation. */
10974 86 : cond = get_internal_target_expr (cond);
10975 86 : add_stmt (cond);
10976 86 : DECL_DECOMP_BASE (decl) = cond;
10977 : }
10978 : }
10979 74768 : int save_read = DECL_READ_P (decl);
10980 224676 : for (unsigned i = 0; i < count; ++i)
10981 : {
10982 149929 : location_t sloc = input_location;
10983 149929 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10984 :
10985 149929 : if ((unsigned) pack == i)
10986 : {
10987 304 : packv = make_tree_vec (eltscnt - count + 3);
10988 1076 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10989 : {
10990 772 : tree t;
10991 772 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10992 772 : set_sb_pack_name (t, j);
10993 772 : input_location = dloc;
10994 772 : tree init = get_tuple_decomp_init (decl, j + pack);
10995 772 : tree eltype = (init == error_mark_node ? error_mark_node
10996 772 : : get_tuple_element_type (type, j + pack));
10997 772 : input_location = sloc;
10998 :
10999 772 : if (VOID_TYPE_P (eltype))
11000 : {
11001 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
11002 : "%<void%>", j + pack, type);
11003 0 : eltype = error_mark_node;
11004 : }
11005 772 : if (init == error_mark_node || eltype == error_mark_node)
11006 : {
11007 0 : inform (dloc, "in initialization of structured binding "
11008 0 : "pack %qD", v[pack]);
11009 0 : goto error_out;
11010 : }
11011 772 : maybe_push_decl (t);
11012 : /* Save the decltype away before reference collapse. */
11013 772 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
11014 772 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11015 772 : TREE_TYPE (t) = eltype;
11016 772 : layout_decl (t, 0);
11017 772 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11018 772 : if (!processing_template_decl)
11019 : {
11020 564 : copy_linkage (t, decl);
11021 564 : tree name = DECL_NAME (t);
11022 564 : if (TREE_STATIC (decl))
11023 36 : DECL_NAME (t) = DECL_NAME (v[pack]);
11024 564 : cp_finish_decl (t, init, /*constexpr*/false,
11025 : /*asm*/NULL_TREE, LOOKUP_NORMAL);
11026 564 : if (TREE_STATIC (decl))
11027 : {
11028 36 : DECL_ASSEMBLER_NAME (t);
11029 36 : DECL_NAME (t) = name;
11030 : }
11031 : }
11032 : }
11033 304 : continue;
11034 304 : }
11035 :
11036 149625 : unsigned HOST_WIDE_INT j = i;
11037 149625 : if (pack != -1 && (unsigned) pack < i)
11038 238 : j = i + eltscnt - count;
11039 149625 : input_location = dloc;
11040 149625 : tree init = get_tuple_decomp_init (decl, j);
11041 149625 : tree eltype = (init == error_mark_node ? error_mark_node
11042 149616 : : get_tuple_element_type (type, j));
11043 149625 : input_location = sloc;
11044 :
11045 149625 : if (VOID_TYPE_P (eltype))
11046 : {
11047 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
11048 : j, type);
11049 3 : eltype = error_mark_node;
11050 : }
11051 149625 : if (init == error_mark_node || eltype == error_mark_node)
11052 : {
11053 21 : inform (dloc, "in initialization of structured binding "
11054 21 : "variable %qD", v[i]);
11055 21 : goto error_out;
11056 : }
11057 : /* Save the decltype away before reference collapse. */
11058 149604 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
11059 149604 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11060 149604 : TREE_TYPE (v[i]) = eltype;
11061 149604 : layout_decl (v[i], 0);
11062 149604 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
11063 : {
11064 : /* In this case the names are variables, not just proxies. */
11065 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
11066 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
11067 : }
11068 149604 : if (!processing_template_decl)
11069 : {
11070 130876 : copy_linkage (v[i], decl);
11071 130876 : cp_finish_decl (v[i], init, /*constexpr*/false,
11072 : /*asm*/NULL_TREE, LOOKUP_NORMAL);
11073 : }
11074 : }
11075 : /* Ignore reads from the underlying decl performed during initialization
11076 : of the individual variables. If those will be read, we'll mark
11077 : the underlying decl as read at that point. */
11078 74747 : DECL_READ_P (decl) = save_read;
11079 : }
11080 15307 : else if (TREE_CODE (type) == UNION_TYPE)
11081 : {
11082 3 : error_at (loc, "cannot decompose union type %qT", type);
11083 3 : goto error_out;
11084 : }
11085 15304 : else if (!CLASS_TYPE_P (type))
11086 : {
11087 16 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
11088 16 : goto error_out;
11089 : }
11090 15288 : else if (processing_template_decl && complete_type (type) == error_mark_node)
11091 0 : goto error_out;
11092 15288 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
11093 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
11094 : type);
11095 : else
11096 : {
11097 15286 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
11098 : tf_warning_or_error);
11099 15286 : if (btype == error_mark_node)
11100 21 : goto error_out;
11101 15265 : else if (btype == NULL_TREE)
11102 : {
11103 23 : if (pack == 0 && count == 1)
11104 : {
11105 16 : eltscnt = 0;
11106 16 : packv = make_tree_vec (2);
11107 16 : goto done;
11108 : }
11109 7 : error_at (loc, "cannot decompose class type %qT without non-static "
11110 : "data members", type);
11111 7 : goto error_out;
11112 : }
11113 143168 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11114 224588 : if (TREE_CODE (field) != FIELD_DECL
11115 31280 : || DECL_ARTIFICIAL (field)
11116 159202 : || DECL_UNNAMED_BIT_FIELD (field))
11117 96662 : continue;
11118 : else
11119 31264 : eltscnt++;
11120 15242 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11121 15 : goto cnt_mismatch;
11122 15227 : tree t = dexp;
11123 15227 : if (type != btype)
11124 : {
11125 18 : t = convert_to_base (t, btype, /*check_access*/true,
11126 : /*nonnull*/false, tf_warning_or_error);
11127 18 : type = btype;
11128 : }
11129 15227 : unsigned HOST_WIDE_INT j = 0;
11130 15227 : unsigned int i = 0;
11131 142957 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11132 224233 : if (TREE_CODE (field) != FIELD_DECL
11133 31243 : || DECL_ARTIFICIAL (field)
11134 158969 : || DECL_UNNAMED_BIT_FIELD (field))
11135 96503 : continue;
11136 : else
11137 : {
11138 31227 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
11139 : NULL_TREE);
11140 31227 : if (REFERENCE_REF_P (tt))
11141 30 : tt = TREE_OPERAND (tt, 0);
11142 31227 : if (pack != -1 && j >= (unsigned) pack)
11143 : {
11144 669 : if (j == (unsigned) pack)
11145 : {
11146 223 : packv = make_tree_vec (eltscnt - count + 3);
11147 223 : i++;
11148 : }
11149 669 : if (j < (unsigned) pack + eltscnt - (count - 1))
11150 : {
11151 488 : tree t;
11152 488 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11153 488 : set_sb_pack_name (t, j + 1 - i);
11154 488 : maybe_push_decl (t);
11155 488 : TREE_TYPE (t) = TREE_TYPE (tt);
11156 488 : layout_decl (t, 0);
11157 488 : if (!processing_template_decl)
11158 : {
11159 367 : SET_DECL_VALUE_EXPR (t, tt);
11160 367 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11161 : }
11162 : else
11163 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11164 488 : j++;
11165 488 : continue;
11166 488 : }
11167 : }
11168 30739 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
11169 30739 : layout_decl (v[i], 0);
11170 30739 : if (!processing_template_decl)
11171 : {
11172 12447 : SET_DECL_VALUE_EXPR (v[i], tt);
11173 12447 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11174 : }
11175 30739 : i++;
11176 30739 : j++;
11177 : }
11178 15227 : if (pack != -1 && j == (unsigned) pack)
11179 : {
11180 20 : gcc_checking_assert (eltscnt == count - 1);
11181 20 : packv = make_tree_vec (2);
11182 : }
11183 : }
11184 93885 : done:
11185 93885 : if (packv)
11186 : {
11187 3626 : gcc_checking_assert (pack != -1);
11188 3626 : TREE_VEC_ELT (packv, 0) = decl;
11189 3626 : TREE_VEC_ELT (packv, 1) = size_int (pack);
11190 3626 : SET_DECL_VALUE_EXPR (v[pack], packv);
11191 3626 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11192 3626 : DECL_IGNORED_P (v[pack]) = 1;
11193 3626 : if (!processing_template_decl)
11194 17485 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11195 14116 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
11196 : }
11197 93885 : if (processing_template_decl)
11198 : {
11199 56106 : for (unsigned int i = 0; i < count; i++)
11200 37467 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11201 : {
11202 37063 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
11203 : NULL_TREE, NULL_TREE);
11204 37063 : SET_DECL_VALUE_EXPR (v[i], a);
11205 37063 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11206 : }
11207 : }
11208 75246 : else if (DECL_NAMESPACE_SCOPE_P (decl) && !seen_error ())
11209 : {
11210 366 : tree attr = NULL_TREE, *pa = &attr;
11211 1277 : for (unsigned int i = 0; i < count; i++)
11212 911 : if ((unsigned) pack != i
11213 911 : && DECL_HAS_VALUE_EXPR_P (v[i])
11214 1482 : && !DECL_IGNORED_P (v[i]))
11215 : {
11216 571 : (*debug_hooks->early_global_decl) (v[i]);
11217 571 : *pa = build_tree_list (NULL_TREE, v[i]);
11218 571 : pa = &TREE_CHAIN (*pa);
11219 : }
11220 366 : if (attr)
11221 251 : DECL_ATTRIBUTES (decl)
11222 502 : = tree_cons (get_identifier ("structured bindings"),
11223 251 : attr, DECL_ATTRIBUTES (decl));
11224 : }
11225 : return false;
11226 : }
11227 :
11228 : /* Returns a declaration for a VAR_DECL as if:
11229 :
11230 : extern "C" TYPE NAME;
11231 :
11232 : had been seen. Used to create compiler-generated global
11233 : variables. */
11234 :
11235 : static tree
11236 2137 : declare_global_var (tree name, tree type)
11237 : {
11238 2137 : auto cookie = push_abi_namespace (global_namespace);
11239 2137 : tree decl = build_decl (input_location, VAR_DECL, name, type);
11240 2137 : TREE_PUBLIC (decl) = 1;
11241 2137 : DECL_EXTERNAL (decl) = 1;
11242 2137 : DECL_ARTIFICIAL (decl) = 1;
11243 2137 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11244 : /* If the user has explicitly declared this variable (perhaps
11245 : because the code we are compiling is part of a low-level runtime
11246 : library), then it is possible that our declaration will be merged
11247 : with theirs by pushdecl. */
11248 2137 : decl = pushdecl (decl);
11249 2137 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
11250 2137 : pop_abi_namespace (cookie, global_namespace);
11251 :
11252 2137 : return decl;
11253 : }
11254 :
11255 : /* Returns the type for the argument to "atexit" corresponding to the function
11256 : to be called when the program exits. */
11257 :
11258 : static tree
11259 24 : get_atexit_fn_ptr_type ()
11260 : {
11261 24 : if (!atexit_fn_ptr_type_node)
11262 : {
11263 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11264 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11265 : }
11266 :
11267 24 : return atexit_fn_ptr_type_node;
11268 : }
11269 :
11270 : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11271 : or "__cxa_throw" corresponding to the destructor to be called when the
11272 : program exits. */
11273 :
11274 : tree
11275 18068 : get_cxa_atexit_fn_ptr_type ()
11276 : {
11277 18068 : if (!cleanup_type)
11278 : {
11279 13597 : tree fntype = build_function_type_list (void_type_node,
11280 : ptr_type_node, NULL_TREE);
11281 13597 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11282 13597 : cleanup_type = build_pointer_type (fntype);
11283 : }
11284 :
11285 18068 : return cleanup_type;
11286 : }
11287 :
11288 : /* Returns a pointer to the `atexit' function. Note that if
11289 : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11290 : `__cxa_atexit' function specified in the IA64 C++ ABI. */
11291 :
11292 : static tree
11293 3277 : get_atexit_node (void)
11294 : {
11295 3277 : tree atexit_fndecl;
11296 3277 : tree fn_type;
11297 3277 : tree fn_ptr_type;
11298 3277 : const char *name;
11299 3277 : bool use_aeabi_atexit;
11300 3277 : tree ctx = global_namespace;
11301 :
11302 3277 : if (atexit_node)
11303 : return atexit_node;
11304 :
11305 2103 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11306 : {
11307 : /* The declaration for `__cxa_atexit' is:
11308 :
11309 : int __cxa_atexit (void (*)(void *), void *, void *)
11310 :
11311 : We build up the argument types and then the function type
11312 : itself. */
11313 2091 : tree argtype0, argtype1, argtype2;
11314 :
11315 2091 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11316 : /* First, build the pointer-to-function type for the first
11317 : argument. */
11318 2091 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11319 : /* Then, build the rest of the argument types. */
11320 2091 : argtype2 = ptr_type_node;
11321 2091 : if (use_aeabi_atexit)
11322 : {
11323 : argtype1 = fn_ptr_type;
11324 : argtype0 = ptr_type_node;
11325 : }
11326 : else
11327 : {
11328 2091 : argtype1 = ptr_type_node;
11329 2091 : argtype0 = fn_ptr_type;
11330 : }
11331 : /* And the final __cxa_atexit type. */
11332 2091 : fn_type = build_function_type_list (integer_type_node,
11333 : argtype0, argtype1, argtype2,
11334 : NULL_TREE);
11335 : /* ... which needs noexcept. */
11336 2091 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11337 2091 : if (use_aeabi_atexit)
11338 : {
11339 0 : name = "__aeabi_atexit";
11340 0 : push_to_top_level ();
11341 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
11342 0 : ctx = current_namespace;
11343 0 : while (n--)
11344 0 : pop_namespace ();
11345 0 : pop_from_top_level ();
11346 : }
11347 : else
11348 : {
11349 2091 : name = "__cxa_atexit";
11350 2091 : ctx = abi_node;
11351 : }
11352 : }
11353 : else
11354 : {
11355 : /* The declaration for `atexit' is:
11356 :
11357 : int atexit (void (*)());
11358 :
11359 : We build up the argument types and then the function type
11360 : itself. */
11361 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
11362 : /* Build the final atexit type. */
11363 12 : fn_type = build_function_type_list (integer_type_node,
11364 : fn_ptr_type, NULL_TREE);
11365 : /* ... which needs noexcept. */
11366 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11367 12 : name = "atexit";
11368 : }
11369 :
11370 : /* Now, build the function declaration. */
11371 2103 : push_lang_context (lang_name_c);
11372 2103 : auto cookie = push_abi_namespace (ctx);
11373 2103 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
11374 2103 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11375 : /* Install as hidden builtin so we're (a) more relaxed about
11376 : exception spec matching and (b) will not give a confusing location
11377 : in diagnostic and (c) won't magically appear in user-visible name
11378 : lookups. */
11379 2103 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11380 2103 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11381 2103 : pop_abi_namespace (cookie, ctx);
11382 2103 : mark_used (atexit_fndecl);
11383 2103 : pop_lang_context ();
11384 2103 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11385 :
11386 2103 : return atexit_node;
11387 : }
11388 :
11389 : /* Like get_atexit_node, but for thread-local cleanups. */
11390 :
11391 : static tree
11392 86 : get_thread_atexit_node (void)
11393 : {
11394 86 : if (thread_atexit_node)
11395 : return thread_atexit_node;
11396 :
11397 : /* The declaration for `__cxa_thread_atexit' is:
11398 :
11399 : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11400 56 : tree fn_type = build_function_type_list (integer_type_node,
11401 : get_cxa_atexit_fn_ptr_type (),
11402 : ptr_type_node, ptr_type_node,
11403 : NULL_TREE);
11404 :
11405 : /* Now, build the function declaration, as with __cxa_atexit. */
11406 56 : unsigned flags = push_abi_namespace ();
11407 56 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
11408 : ECF_LEAF | ECF_NOTHROW);
11409 56 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11410 56 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11411 56 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11412 56 : pop_abi_namespace (flags);
11413 56 : mark_used (atexit_fndecl);
11414 56 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11415 :
11416 56 : return thread_atexit_node;
11417 : }
11418 :
11419 : /* Returns the __dso_handle VAR_DECL. */
11420 :
11421 : static tree
11422 3351 : get_dso_handle_node (void)
11423 : {
11424 3351 : if (dso_handle_node)
11425 : return dso_handle_node;
11426 :
11427 : /* Declare the variable. */
11428 2137 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11429 : ptr_type_node);
11430 :
11431 : #ifdef HAVE_GAS_HIDDEN
11432 2137 : if (dso_handle_node != error_mark_node)
11433 : {
11434 2134 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11435 2134 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11436 : }
11437 : #endif
11438 :
11439 2137 : return dso_handle_node;
11440 : }
11441 :
11442 : /* Begin a new function with internal linkage whose job will be simply
11443 : to destroy some particular DECL. OB_PARM is true if object pointer
11444 : is passed to the cleanup function, otherwise no argument is passed. */
11445 :
11446 : static tree
11447 370 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11448 : {
11449 370 : push_to_top_level ();
11450 :
11451 : /* No need to mangle this. */
11452 370 : push_lang_context (lang_name_c);
11453 :
11454 : /* Build the name of the function. */
11455 370 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11456 370 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11457 370 : dname = targetm.strip_name_encoding (dname);
11458 737 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11459 :
11460 370 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11461 : : get_atexit_fn_ptr_type ());
11462 : /* Build the function declaration. */
11463 370 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11464 370 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11465 : /* It's a function with internal linkage, generated by the
11466 : compiler. */
11467 370 : TREE_PUBLIC (fndecl) = 0;
11468 370 : DECL_ARTIFICIAL (fndecl) = 1;
11469 : /* Make the function `inline' so that it is only emitted if it is
11470 : actually needed. It is unlikely that it will be inlined, since
11471 : it is only called via a function pointer, but we avoid unnecessary
11472 : emissions this way. */
11473 370 : DECL_DECLARED_INLINE_P (fndecl) = 1;
11474 370 : DECL_INTERFACE_KNOWN (fndecl) = 1;
11475 370 : if (ob_parm)
11476 : {
11477 : /* Build the parameter. */
11478 358 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11479 358 : TREE_USED (parmdecl) = 1;
11480 358 : DECL_READ_P (parmdecl) = 1;
11481 358 : DECL_ARGUMENTS (fndecl) = parmdecl;
11482 : }
11483 :
11484 370 : fndecl = pushdecl (fndecl, /*hidden=*/true);
11485 370 : if (omp_target)
11486 : {
11487 3 : DECL_ATTRIBUTES (fndecl)
11488 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11489 3 : DECL_ATTRIBUTES (fndecl));
11490 3 : DECL_ATTRIBUTES (fndecl)
11491 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11492 3 : DECL_ATTRIBUTES (fndecl));
11493 : }
11494 370 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11495 :
11496 370 : pop_lang_context ();
11497 :
11498 370 : return current_function_decl;
11499 : }
11500 :
11501 : /* Finish the cleanup function begun by start_cleanup_fn. */
11502 :
11503 : static void
11504 370 : end_cleanup_fn (void)
11505 : {
11506 370 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
11507 :
11508 370 : pop_from_top_level ();
11509 370 : }
11510 :
11511 : /* Generate code to handle the destruction of DECL, an object with
11512 : static storage duration. */
11513 :
11514 : tree
11515 14863 : register_dtor_fn (tree decl, bool omp_target)
11516 : {
11517 14863 : tree cleanup;
11518 14863 : tree addr;
11519 14863 : tree compound_stmt;
11520 14863 : tree fcall;
11521 14863 : tree type;
11522 14863 : bool ob_parm, dso_parm, use_dtor;
11523 14863 : tree arg0, arg1, arg2;
11524 14863 : tree atex_node;
11525 :
11526 14863 : type = TREE_TYPE (decl);
11527 14863 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11528 11500 : return void_node;
11529 :
11530 3363 : if (decl_maybe_constant_destruction (decl, type)
11531 3363 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11532 : {
11533 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11534 0 : return void_node;
11535 : }
11536 :
11537 : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11538 : "__aeabi_atexit"), and DECL is a class object, we can just pass the
11539 : destructor to "__cxa_atexit"; we don't have to build a temporary
11540 : function to do the cleanup. */
11541 6726 : dso_parm = (flag_use_cxa_atexit
11542 3363 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
11543 3363 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11544 3351 : use_dtor = ob_parm && CLASS_TYPE_P (type);
11545 3363 : if (use_dtor)
11546 : {
11547 2993 : cleanup = get_class_binding (type, complete_dtor_identifier);
11548 :
11549 : /* Make sure it is accessible. */
11550 2993 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11551 : tf_warning_or_error);
11552 : }
11553 : else
11554 : {
11555 : /* Call build_cleanup before we enter the anonymous function so
11556 : that any access checks will be done relative to the current
11557 : scope, rather than the scope of the anonymous function. */
11558 370 : build_cleanup (decl);
11559 :
11560 : /* Now start the function. */
11561 370 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11562 :
11563 : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11564 : to the original function, rather than the anonymous one. That
11565 : will make the back end think that nested functions are in use,
11566 : which causes confusion. */
11567 370 : push_deferring_access_checks (dk_no_check);
11568 370 : fcall = build_cleanup (decl);
11569 370 : pop_deferring_access_checks ();
11570 :
11571 : /* Create the body of the anonymous function. */
11572 370 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11573 370 : finish_expr_stmt (fcall);
11574 370 : finish_compound_stmt (compound_stmt);
11575 370 : end_cleanup_fn ();
11576 : }
11577 :
11578 : /* Call atexit with the cleanup function. */
11579 3363 : mark_used (cleanup);
11580 3363 : cleanup = build_address (cleanup);
11581 :
11582 3363 : if (CP_DECL_THREAD_LOCAL_P (decl))
11583 86 : atex_node = get_thread_atexit_node ();
11584 : else
11585 3277 : atex_node = get_atexit_node ();
11586 :
11587 3363 : if (use_dtor)
11588 : {
11589 : /* We must convert CLEANUP to the type that "__cxa_atexit"
11590 : expects. */
11591 2993 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11592 : /* "__cxa_atexit" will pass the address of DECL to the
11593 : cleanup function. */
11594 2993 : mark_used (decl);
11595 2993 : addr = build_address (decl);
11596 : /* The declared type of the parameter to "__cxa_atexit" is
11597 : "void *". For plain "T*", we could just let the
11598 : machinery in cp_build_function_call convert it -- but if the
11599 : type is "cv-qualified T *", then we need to convert it
11600 : before passing it in, to avoid spurious errors. */
11601 2993 : addr = build_nop (ptr_type_node, addr);
11602 : }
11603 : else
11604 : /* Since the cleanup functions we build ignore the address
11605 : they're given, there's no reason to pass the actual address
11606 : in, and, in general, it's cheaper to pass NULL than any
11607 : other value. */
11608 370 : addr = null_pointer_node;
11609 :
11610 3363 : if (dso_parm)
11611 3351 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
11612 : tf_warning_or_error);
11613 12 : else if (ob_parm)
11614 : /* Just pass NULL to the dso handle parm if we don't actually
11615 : have a DSO handle on this target. */
11616 0 : arg2 = null_pointer_node;
11617 : else
11618 : arg2 = NULL_TREE;
11619 :
11620 3351 : if (ob_parm)
11621 : {
11622 3351 : if (!CP_DECL_THREAD_LOCAL_P (decl)
11623 3351 : && targetm.cxx.use_aeabi_atexit ())
11624 : {
11625 : arg1 = cleanup;
11626 : arg0 = addr;
11627 : }
11628 : else
11629 : {
11630 : arg1 = addr;
11631 : arg0 = cleanup;
11632 : }
11633 : }
11634 : else
11635 : {
11636 : arg0 = cleanup;
11637 : arg1 = NULL_TREE;
11638 : }
11639 3363 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11640 3363 : arg0, arg1, arg2, NULL_TREE);
11641 : }
11642 :
11643 : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
11644 : is its initializer. Generate code to handle the construction
11645 : and destruction of DECL. */
11646 :
11647 : static void
11648 32300674 : expand_static_init (tree decl, tree init)
11649 : {
11650 32300674 : gcc_assert (VAR_P (decl));
11651 32300674 : gcc_assert (TREE_STATIC (decl));
11652 :
11653 : /* Some variables require no dynamic initialization. */
11654 32300674 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11655 : {
11656 : /* Make sure the destructor is callable. */
11657 32297397 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11658 32297397 : if (!init)
11659 : return;
11660 : }
11661 :
11662 15673 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11663 14750 : && !DECL_FUNCTION_SCOPE_P (decl))
11664 : {
11665 15 : auto_diagnostic_group d;
11666 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11667 15 : if (init)
11668 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11669 : "needs dynamic initialization", decl);
11670 : else
11671 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11672 : "has a non-trivial destructor", decl);
11673 15 : static bool informed;
11674 15 : if (!informed)
11675 : {
11676 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11677 : "initialization and destruction");
11678 6 : informed = true;
11679 : }
11680 15 : return;
11681 15 : }
11682 :
11683 14706 : if (DECL_FUNCTION_SCOPE_P (decl))
11684 : {
11685 : /* Emit code to perform this initialization but once. */
11686 3785 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11687 3785 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11688 3785 : tree guard, guard_addr;
11689 3785 : tree flag, begin;
11690 : /* We don't need thread-safety code for thread-local vars. */
11691 3785 : bool thread_guard = (flag_threadsafe_statics
11692 3785 : && !CP_DECL_THREAD_LOCAL_P (decl));
11693 :
11694 : /* Emit code to perform this initialization but once. This code
11695 : looks like:
11696 :
11697 : static <type> guard;
11698 : if (!__atomic_load (guard.first_byte)) {
11699 : if (__cxa_guard_acquire (&guard)) {
11700 : bool flag = false;
11701 : try {
11702 : // Do initialization.
11703 : flag = true; __cxa_guard_release (&guard);
11704 : // Register variable for destruction at end of program.
11705 : } catch {
11706 : if (!flag) __cxa_guard_abort (&guard);
11707 : }
11708 : }
11709 : }
11710 :
11711 : Note that the `flag' variable is only set to 1 *after* the
11712 : initialization is complete. This ensures that an exception,
11713 : thrown during the construction, will cause the variable to
11714 : reinitialized when we pass through this code again, as per:
11715 :
11716 : [stmt.dcl]
11717 :
11718 : If the initialization exits by throwing an exception, the
11719 : initialization is not complete, so it will be tried again
11720 : the next time control enters the declaration.
11721 :
11722 : This process should be thread-safe, too; multiple threads
11723 : should not be able to initialize the variable more than
11724 : once. */
11725 :
11726 : /* Create the guard variable. */
11727 3785 : guard = get_guard (decl);
11728 :
11729 : /* Begin the conditional initialization. */
11730 3785 : if_stmt = begin_if_stmt ();
11731 :
11732 3785 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11733 3785 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11734 :
11735 3785 : if (thread_guard)
11736 : {
11737 3640 : tree vfntype = NULL_TREE;
11738 3640 : tree acquire_name, release_name, abort_name;
11739 3640 : tree acquire_fn, release_fn, abort_fn;
11740 3640 : guard_addr = build_address (guard);
11741 :
11742 3640 : acquire_name = get_identifier ("__cxa_guard_acquire");
11743 3640 : release_name = get_identifier ("__cxa_guard_release");
11744 3640 : abort_name = get_identifier ("__cxa_guard_abort");
11745 3640 : acquire_fn = get_global_binding (acquire_name);
11746 3640 : release_fn = get_global_binding (release_name);
11747 3640 : abort_fn = get_global_binding (abort_name);
11748 3640 : if (!acquire_fn)
11749 2037 : acquire_fn = push_library_fn
11750 2037 : (acquire_name, build_function_type_list (integer_type_node,
11751 2037 : TREE_TYPE (guard_addr),
11752 : NULL_TREE),
11753 : NULL_TREE, ECF_NOTHROW);
11754 3640 : if (!release_fn || !abort_fn)
11755 2040 : vfntype = build_function_type_list (void_type_node,
11756 2040 : TREE_TYPE (guard_addr),
11757 : NULL_TREE);
11758 2040 : if (!release_fn)
11759 2040 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11760 : ECF_NOTHROW);
11761 3640 : if (!abort_fn)
11762 2040 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11763 : ECF_NOTHROW | ECF_LEAF);
11764 :
11765 3640 : inner_if_stmt = begin_if_stmt ();
11766 3640 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11767 : inner_if_stmt);
11768 :
11769 3640 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11770 3640 : begin = get_internal_target_expr (boolean_false_node);
11771 3640 : flag = TARGET_EXPR_SLOT (begin);
11772 :
11773 3640 : TARGET_EXPR_CLEANUP (begin)
11774 3640 : = build3 (COND_EXPR, void_type_node, flag,
11775 : void_node,
11776 : build_call_n (abort_fn, 1, guard_addr));
11777 3640 : CLEANUP_EH_ONLY (begin) = 1;
11778 :
11779 : /* Do the initialization itself. */
11780 3640 : init = add_stmt_to_compound (begin, init);
11781 3640 : init = add_stmt_to_compound (init,
11782 : build2 (MODIFY_EXPR, void_type_node,
11783 : flag, boolean_true_node));
11784 :
11785 : /* Use atexit to register a function for destroying this static
11786 : variable. Do this before calling __cxa_guard_release. */
11787 3640 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11788 :
11789 3640 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11790 : guard_addr));
11791 : }
11792 : else
11793 : {
11794 145 : init = add_stmt_to_compound (init, set_guard (guard));
11795 :
11796 : /* Use atexit to register a function for destroying this static
11797 : variable. */
11798 145 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11799 : }
11800 :
11801 3785 : finish_expr_stmt (init);
11802 :
11803 3785 : if (thread_guard)
11804 : {
11805 3640 : finish_compound_stmt (inner_then_clause);
11806 3640 : finish_then_clause (inner_if_stmt);
11807 3640 : finish_if_stmt (inner_if_stmt);
11808 : }
11809 :
11810 3785 : finish_compound_stmt (then_clause);
11811 3785 : finish_then_clause (if_stmt);
11812 3785 : finish_if_stmt (if_stmt);
11813 : }
11814 10921 : else if (CP_DECL_THREAD_LOCAL_P (decl))
11815 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
11816 : else
11817 10126 : static_aggregates = tree_cons (init, decl, static_aggregates);
11818 : }
11819 :
11820 :
11821 : /* Make TYPE a complete type based on INITIAL_VALUE.
11822 : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11823 : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11824 : 3 if the initializer list is empty (in pedantic mode). */
11825 :
11826 : int
11827 660223 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11828 : {
11829 660223 : int failure;
11830 660223 : tree type, elt_type;
11831 :
11832 : /* Don't get confused by a CONSTRUCTOR for some other type. */
11833 660193 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11834 141091 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11835 660553 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11836 : return 1;
11837 :
11838 660220 : if (initial_value)
11839 : {
11840 : /* An array of character type can be initialized from a
11841 : brace-enclosed string constant so call reshape_init to
11842 : remove the optional braces from a braced string literal. */
11843 660190 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
11844 660190 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
11845 48194 : initial_value = reshape_init (*ptype, initial_value,
11846 : tf_warning_or_error);
11847 :
11848 : /* If any of the elements are parameter packs, we can't actually
11849 : complete this type now because the array size is dependent. */
11850 660190 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
11851 46457926 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
11852 46035069 : if (PACK_EXPANSION_P (e.value))
11853 : return 0;
11854 : }
11855 :
11856 660220 : failure = complete_array_type (ptype, initial_value, do_default);
11857 :
11858 : /* We can create the array before the element type is complete, which
11859 : means that we didn't have these two bits set in the original type
11860 : either. In completing the type, we are expected to propagate these
11861 : bits. See also complete_type which does the same thing for arrays
11862 : of fixed size. */
11863 660220 : type = *ptype;
11864 1320437 : if (type != error_mark_node && TYPE_DOMAIN (type))
11865 : {
11866 660193 : elt_type = TREE_TYPE (type);
11867 660193 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
11868 1320386 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11869 660193 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
11870 : }
11871 :
11872 : return failure;
11873 : }
11874 :
11875 : /* As above, but either give an error or reject zero-size arrays, depending
11876 : on COMPLAIN. */
11877 :
11878 : int
11879 1192 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
11880 : bool do_default, tsubst_flags_t complain)
11881 : {
11882 1192 : int failure;
11883 1192 : bool sfinae = !(complain & tf_error);
11884 : /* In SFINAE context we can't be lenient about zero-size arrays. */
11885 1192 : if (sfinae)
11886 3 : ++pedantic;
11887 1192 : failure = cp_complete_array_type (ptype, initial_value, do_default);
11888 1192 : if (sfinae)
11889 3 : --pedantic;
11890 1192 : if (failure)
11891 : {
11892 3 : if (sfinae)
11893 : /* Not an error. */;
11894 0 : else if (failure == 1)
11895 0 : error ("initializer fails to determine size of %qT", *ptype);
11896 0 : else if (failure == 2)
11897 : {
11898 0 : if (do_default)
11899 0 : error ("array size missing in %qT", *ptype);
11900 : }
11901 0 : else if (failure == 3)
11902 0 : error ("zero-size array %qT", *ptype);
11903 3 : *ptype = error_mark_node;
11904 : }
11905 1192 : return failure;
11906 : }
11907 :
11908 : /* Return zero if something is declared to be a member of type
11909 : CTYPE when in the context of CUR_TYPE. STRING is the error
11910 : message to print in that case. Otherwise, quietly return 1. */
11911 :
11912 : static int
11913 25734922 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
11914 : {
11915 25734922 : if (ctype && ctype != cur_type)
11916 : {
11917 0 : if (flags == DTOR_FLAG)
11918 0 : error ("destructor for alien class %qT cannot be a member", ctype);
11919 : else
11920 0 : error ("constructor for alien class %qT cannot be a member", ctype);
11921 0 : return 0;
11922 : }
11923 : return 1;
11924 : }
11925 :
11926 : /* Subroutine of `grokdeclarator'. */
11927 :
11928 : /* Generate errors possibly applicable for a given set of specifiers.
11929 : This is for ARM $7.1.2. */
11930 :
11931 : static void
11932 429887275 : bad_specifiers (tree object,
11933 : enum bad_spec_place type,
11934 : int virtualp,
11935 : int quals,
11936 : int inlinep,
11937 : int friendp,
11938 : int raises,
11939 : const location_t* locations)
11940 : {
11941 429887275 : switch (type)
11942 : {
11943 66349790 : case BSP_VAR:
11944 66349790 : if (virtualp)
11945 0 : error_at (locations[ds_virtual],
11946 : "%qD declared as a %<virtual%> variable", object);
11947 66349790 : if (quals)
11948 3 : error ("%<const%> and %<volatile%> function specifiers on "
11949 : "%qD invalid in variable declaration", object);
11950 : break;
11951 285869371 : case BSP_PARM:
11952 285869371 : if (virtualp)
11953 0 : error_at (locations[ds_virtual],
11954 : "%qD declared as a %<virtual%> parameter", object);
11955 285869371 : if (inlinep)
11956 6 : error_at (locations[ds_inline],
11957 : "%qD declared as an %<inline%> parameter", object);
11958 285869371 : if (quals)
11959 3 : error ("%<const%> and %<volatile%> function specifiers on "
11960 : "%qD invalid in parameter declaration", object);
11961 : break;
11962 45834831 : case BSP_TYPE:
11963 45834831 : if (virtualp)
11964 0 : error_at (locations[ds_virtual],
11965 : "%qD declared as a %<virtual%> type", object);
11966 45834831 : if (inlinep)
11967 6 : error_at (locations[ds_inline],
11968 : "%qD declared as an %<inline%> type", object);
11969 45834831 : if (quals)
11970 0 : error ("%<const%> and %<volatile%> function specifiers on "
11971 : "%qD invalid in type declaration", object);
11972 : break;
11973 31833283 : case BSP_FIELD:
11974 31833283 : if (virtualp)
11975 6 : error_at (locations[ds_virtual],
11976 : "%qD declared as a %<virtual%> field", object);
11977 31833283 : if (inlinep)
11978 6 : error_at (locations[ds_inline],
11979 : "%qD declared as an %<inline%> field", object);
11980 31833283 : if (quals)
11981 0 : error ("%<const%> and %<volatile%> function specifiers on "
11982 : "%qD invalid in field declaration", object);
11983 : break;
11984 0 : default:
11985 0 : gcc_unreachable();
11986 : }
11987 429887275 : if (friendp)
11988 0 : error ("%q+D declared as a friend", object);
11989 429887275 : if (raises
11990 113 : && !flag_noexcept_type
11991 429887296 : && (TREE_CODE (object) == TYPE_DECL
11992 15 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
11993 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
11994 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
11995 6 : error ("%q+D declared with an exception specification", object);
11996 429887275 : }
11997 :
11998 : /* DECL is a member function or static data member and is presently
11999 : being defined. Check that the definition is taking place in a
12000 : valid namespace. */
12001 :
12002 : static void
12003 94911854 : check_class_member_definition_namespace (tree decl)
12004 : {
12005 : /* These checks only apply to member functions and static data
12006 : members. */
12007 94911854 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
12008 : /* We check for problems with specializations in pt.cc in
12009 : check_specialization_namespace, where we can issue better
12010 : diagnostics. */
12011 94911854 : if (processing_specialization)
12012 : return;
12013 : /* We check this in check_explicit_instantiation_namespace. */
12014 94774105 : if (processing_explicit_instantiation)
12015 : return;
12016 : /* [class.mfct]
12017 :
12018 : A member function definition that appears outside of the
12019 : class definition shall appear in a namespace scope enclosing
12020 : the class definition.
12021 :
12022 : [class.static.data]
12023 :
12024 : The definition for a static data member shall appear in a
12025 : namespace scope enclosing the member's class definition. */
12026 94774015 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
12027 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
12028 18 : decl, DECL_CONTEXT (decl));
12029 : }
12030 :
12031 : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
12032 : METHOD_TYPE for a non-static member function; QUALS are the
12033 : cv-qualifiers that apply to the function. */
12034 :
12035 : tree
12036 235776820 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
12037 : {
12038 235776820 : tree this_type;
12039 235776820 : tree qual_type;
12040 235776820 : tree parm;
12041 235776820 : cp_cv_quals this_quals;
12042 :
12043 235776820 : if (CLASS_TYPE_P (type))
12044 : {
12045 101391190 : this_type
12046 101391190 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
12047 101391190 : this_type = build_pointer_type (this_type);
12048 : }
12049 : else
12050 134385630 : this_type = type_of_this_parm (type);
12051 : /* The `this' parameter is implicitly `const'; it cannot be
12052 : assigned to. */
12053 235776820 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
12054 235776820 : qual_type = cp_build_qualified_type (this_type, this_quals);
12055 235776820 : parm = build_artificial_parm (fn, this_identifier, qual_type);
12056 235776820 : cp_apply_type_quals_to_decl (this_quals, parm);
12057 235776820 : return parm;
12058 : }
12059 :
12060 : /* DECL is a static member function. Complain if it was declared
12061 : with function-cv-quals. */
12062 :
12063 : static void
12064 10098611 : check_static_quals (tree decl, cp_cv_quals quals)
12065 : {
12066 0 : if (quals != TYPE_UNQUALIFIED)
12067 6 : error ("static member function %q#D declared with type qualifiers",
12068 : decl);
12069 0 : }
12070 :
12071 : /* Helper function. Replace the temporary this parameter injected
12072 : during cp_finish_omp_declare_simd with the real this parameter. */
12073 :
12074 : static tree
12075 2692 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
12076 : {
12077 2692 : tree this_parm = (tree) data;
12078 2692 : if (TREE_CODE (*tp) == PARM_DECL
12079 450 : && DECL_NAME (*tp) == this_identifier
12080 2965 : && *tp != this_parm)
12081 273 : *tp = this_parm;
12082 2419 : else if (TYPE_P (*tp))
12083 33 : *walk_subtrees = 0;
12084 2692 : return NULL_TREE;
12085 : }
12086 :
12087 : /* CTYPE is class type, or null if non-class.
12088 : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
12089 : or METHOD_TYPE.
12090 : DECLARATOR is the function's name.
12091 : PARMS is a chain of PARM_DECLs for the function.
12092 : VIRTUALP is truthvalue of whether the function is virtual or not.
12093 : FLAGS are to be passed through to `grokclassfn'.
12094 : QUALS are qualifiers indicating whether the function is `const'
12095 : or `volatile'.
12096 : RAISES is a list of exceptions that this function can raise.
12097 : CHECK is 1 if we must find this method in CTYPE, 0 if we should
12098 : not look, and -1 if we should not call `grokclassfn' at all.
12099 :
12100 : SFK is the kind of special function (if any) for the new function.
12101 :
12102 : Returns `NULL_TREE' if something goes wrong, after issuing
12103 : applicable error messages. */
12104 :
12105 : static tree
12106 183182615 : grokfndecl (tree ctype,
12107 : tree type,
12108 : tree declarator,
12109 : tree parms,
12110 : tree orig_declarator,
12111 : const cp_decl_specifier_seq *declspecs,
12112 : tree decl_reqs,
12113 : int virtualp,
12114 : enum overload_flags flags,
12115 : cp_cv_quals quals,
12116 : cp_ref_qualifier rqual,
12117 : tree raises,
12118 : int check,
12119 : int friendp,
12120 : int publicp,
12121 : int inlinep,
12122 : int initialized,
12123 : bool xobj_func_p,
12124 : special_function_kind sfk,
12125 : bool funcdef_flag,
12126 : bool late_return_type_p,
12127 : int template_count,
12128 : tree in_namespace,
12129 : tree* attrlist,
12130 : tree contract_specifiers,
12131 : location_t location)
12132 : {
12133 183182615 : tree decl;
12134 183182615 : tree t;
12135 :
12136 183182615 : if (location == UNKNOWN_LOCATION)
12137 0 : location = input_location;
12138 :
12139 : /* Was the concept specifier present? */
12140 183182615 : bool concept_p = inlinep & 4;
12141 :
12142 183182615 : if (concept_p)
12143 : {
12144 15 : error_at (location, "function concepts are no longer supported");
12145 15 : return NULL_TREE;
12146 : }
12147 :
12148 183182600 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
12149 :
12150 183182600 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
12151 :
12152 : /* Set the constraints on the declaration. */
12153 183182600 : if (flag_concepts)
12154 : {
12155 179018486 : tree tmpl_reqs = NULL_TREE;
12156 179018486 : tree ctx = friendp ? current_class_type : ctype;
12157 179018486 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
12158 179018486 : bool memtmpl = (!block_local
12159 179018486 : && (current_template_depth
12160 178963690 : > template_class_depth (ctx)));
12161 49200677 : if (memtmpl)
12162 : {
12163 49200677 : if (!current_template_parms)
12164 : /* If there are no template parameters, something must have
12165 : gone wrong. */
12166 0 : gcc_assert (seen_error ());
12167 : else
12168 49200677 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12169 : }
12170 179018486 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
12171 : /* C++20 CA378: Remove non-templated constrained functions. */
12172 : /* [temp.friend]/9 A non-template friend declaration with a
12173 : requires-clause shall be a definition. A friend function template with
12174 : a constraint that depends on a template parameter from an enclosing
12175 : template shall be a definition. */
12176 179018486 : if (ci
12177 9163600 : && (block_local
12178 9163597 : || !processing_template_decl
12179 9163537 : || (friendp && !memtmpl && !funcdef_flag)))
12180 : {
12181 72 : if (!friendp || !processing_template_decl)
12182 63 : error_at (location, "constraints on a non-templated function");
12183 : else
12184 9 : error_at (location, "constrained non-template friend declaration"
12185 : " must be a definition");
12186 : ci = NULL_TREE;
12187 : }
12188 179018486 : set_constraints (decl, ci);
12189 1130212 : if (ci && friendp && memtmpl && !funcdef_flag
12190 179054406 : && uses_outer_template_parms_in_constraints (decl, ctx))
12191 6 : error_at (location, "friend function template with constraints that "
12192 : "depend on outer template parameters must be a definition");
12193 : }
12194 :
12195 183182600 : if (TREE_CODE (type) == METHOD_TYPE)
12196 : {
12197 99272451 : tree parm = build_this_parm (decl, type, quals);
12198 99272451 : DECL_CHAIN (parm) = parms;
12199 99272451 : parms = parm;
12200 :
12201 : /* Allocate space to hold the vptr bit if needed. */
12202 99272451 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12203 : }
12204 :
12205 183182600 : DECL_ARGUMENTS (decl) = parms;
12206 553244842 : for (t = parms; t; t = DECL_CHAIN (t))
12207 370062242 : DECL_CONTEXT (t) = decl;
12208 :
12209 : /* Propagate volatile out from type to decl. */
12210 183182600 : if (TYPE_VOLATILE (type))
12211 0 : TREE_THIS_VOLATILE (decl) = 1;
12212 :
12213 : /* Setup decl according to sfk. */
12214 183182600 : switch (sfk)
12215 : {
12216 23503936 : case sfk_constructor:
12217 23503936 : case sfk_copy_constructor:
12218 23503936 : case sfk_move_constructor:
12219 23503936 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12220 23503936 : DECL_NAME (decl) = ctor_identifier;
12221 23503936 : break;
12222 3166307 : case sfk_destructor:
12223 3166307 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
12224 3166307 : DECL_NAME (decl) = dtor_identifier;
12225 3166307 : break;
12226 1166031 : case sfk_deduction_guide:
12227 : /* Give deduction guides a definition even though they don't really
12228 : have one: the restriction that you can't repeat a deduction guide
12229 : makes them more like a definition anyway. */
12230 1166031 : DECL_INITIAL (decl) = void_node;
12231 : /* But to ensure that external-linkage deduction guides in header units
12232 : don't fall afoul of [module.import] p6, mark them as inline. */
12233 1166031 : DECL_DECLARED_INLINE_P (decl) = true;
12234 1166031 : break;
12235 : default:
12236 : break;
12237 : }
12238 :
12239 183182600 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12240 : {
12241 89178 : if (funcdef_flag)
12242 3 : error_at (location,
12243 : "defining explicit specialization %qD in friend declaration",
12244 : orig_declarator);
12245 : else
12246 : {
12247 89175 : tree fns = TREE_OPERAND (orig_declarator, 0);
12248 89175 : tree args = TREE_OPERAND (orig_declarator, 1);
12249 :
12250 89175 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12251 : {
12252 : /* Something like `template <class T> friend void f<T>()'. */
12253 6 : error_at (location,
12254 : "invalid use of template-id %qD in declaration "
12255 : "of primary template",
12256 : orig_declarator);
12257 6 : return NULL_TREE;
12258 : }
12259 :
12260 : /* A friend declaration of the form friend void f<>(). Record
12261 : the information in the TEMPLATE_ID_EXPR. */
12262 89169 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
12263 :
12264 89169 : gcc_assert (identifier_p (fns) || OVL_P (fns));
12265 89169 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12266 :
12267 : /* Remember the befriending class like push_template_decl does for
12268 : template friends. */
12269 89169 : gcc_checking_assert (!DECL_CHAIN (decl));
12270 89169 : DECL_CHAIN (decl) = current_scope ();
12271 :
12272 288910 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12273 199746 : if (TREE_PURPOSE (t)
12274 199746 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12275 : {
12276 5 : error_at (defparse_location (TREE_PURPOSE (t)),
12277 : "default arguments are not allowed in declaration "
12278 : "of friend template specialization %qD",
12279 : decl);
12280 5 : return NULL_TREE;
12281 : }
12282 :
12283 89164 : if (inlinep & 1)
12284 : {
12285 5 : error_at (declspecs->locations[ds_inline],
12286 : "%<inline%> is not allowed in declaration of friend "
12287 : "template specialization %qD",
12288 : decl);
12289 5 : return NULL_TREE;
12290 : }
12291 : }
12292 : }
12293 :
12294 : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12295 : expression, that declaration shall be a definition..." */
12296 183182584 : if (friendp && !funcdef_flag)
12297 : {
12298 1767631 : bool has_errored = false;
12299 1767631 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12300 4965099 : t && t != void_list_node; t = TREE_CHAIN (t))
12301 3197468 : if (TREE_PURPOSE (t))
12302 : {
12303 51 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12304 : /* For templates, mark the default argument as erroneous and give a
12305 : hard error. */
12306 51 : if (processing_template_decl)
12307 : {
12308 33 : diag_kind = diagnostics::kind::error;
12309 33 : TREE_PURPOSE (t) = error_mark_node;
12310 : }
12311 51 : if (!has_errored)
12312 : {
12313 48 : has_errored = true;
12314 48 : emit_diagnostic (diag_kind,
12315 48 : DECL_SOURCE_LOCATION (decl),
12316 48 : /*diagnostics::option_id=*/0,
12317 : "friend declaration of %qD specifies default "
12318 : "arguments and isn%'t a definition", decl);
12319 : }
12320 : }
12321 : }
12322 :
12323 : /* If this decl has namespace scope, set that up. */
12324 183182584 : if (in_namespace)
12325 219167 : set_decl_namespace (decl, in_namespace, friendp);
12326 182963417 : else if (ctype)
12327 109282682 : DECL_CONTEXT (decl) = ctype;
12328 : else
12329 73680735 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12330 :
12331 : /* `main' and builtins have implicit 'C' linkage. */
12332 183182584 : if (ctype == NULL_TREE
12333 73899902 : && DECL_FILE_SCOPE_P (decl)
12334 30723738 : && current_lang_name == lang_name_cplusplus
12335 186107745 : && (MAIN_NAME_P (declarator)
12336 2891563 : || (IDENTIFIER_LENGTH (declarator) > 10
12337 2077927 : && IDENTIFIER_POINTER (declarator)[0] == '_'
12338 1627311 : && IDENTIFIER_POINTER (declarator)[1] == '_'
12339 335132 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
12340 : "builtin_"))
12341 2891545 : || (targetcm.cxx_implicit_extern_c
12342 0 : && (targetcm.cxx_implicit_extern_c
12343 0 : (IDENTIFIER_POINTER (declarator))))))
12344 33616 : SET_DECL_LANGUAGE (decl, lang_c);
12345 :
12346 183182584 : DECL_STATIC_FUNCTION_P (decl)
12347 183182584 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12348 183182584 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12349 :
12350 183182584 : if (initialized == SD_DELETED)
12351 4060686 : DECL_DELETED_FN (decl) = 1;
12352 :
12353 183182584 : if (ctype && funcdef_flag)
12354 94434121 : check_class_member_definition_namespace (decl);
12355 :
12356 183182584 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12357 : {
12358 33604 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
12359 9 : error_at (location, "cannot declare %<::main%> to be a template");
12360 33604 : if (inlinep & 1)
12361 3 : error_at (declspecs->locations[ds_inline],
12362 : "cannot declare %<::main%> to be inline");
12363 33604 : if (inlinep & 2)
12364 6 : error_at (declspecs->locations[ds_constexpr],
12365 : "cannot declare %<::main%> to be %qs", "constexpr");
12366 33604 : if (inlinep & 8)
12367 3 : error_at (declspecs->locations[ds_consteval],
12368 : "cannot declare %<::main%> to be %qs", "consteval");
12369 33604 : if (!publicp)
12370 0 : error_at (location, "cannot declare %<::main%> to be static");
12371 33604 : if (current_lang_name != lang_name_cplusplus)
12372 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12373 : " linkage specification other than %<extern \"C++\"%>");
12374 33604 : if (module_attach_p ())
12375 : {
12376 3 : auto_diagnostic_group adg;
12377 3 : error_at (location, "cannot attach %<::main%> to a named module");
12378 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
12379 : "global module instead");
12380 3 : }
12381 : inlinep = 0;
12382 : publicp = 1;
12383 : }
12384 :
12385 : /* Members of anonymous types and local classes have no linkage; make
12386 : them internal. If a typedef is made later, this will be changed. */
12387 183182584 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12388 : publicp = 0;
12389 183169683 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12390 : /* But members of local classes in a module CMI should have their
12391 : definitions exported, in case they are (directly or indirectly)
12392 : used by an importer. We don't just use module_has_cmi_p here
12393 : because for entities in the GMF we don't yet know whether this
12394 : module will have a CMI, so we'll conservatively assume it might. */
12395 2458239 : publicp = module_maybe_has_cmi_p ();
12396 :
12397 183169683 : if (publicp && cxx_dialect == cxx98)
12398 : {
12399 : /* [basic.link]: A name with no linkage (notably, the name of a class
12400 : or enumeration declared in a local scope) shall not be used to
12401 : declare an entity with linkage.
12402 :
12403 : DR 757 relaxes this restriction for C++0x. */
12404 1032454 : no_linkage_error (decl);
12405 : }
12406 :
12407 183182584 : TREE_PUBLIC (decl) = publicp;
12408 183182584 : if (! publicp)
12409 : {
12410 2572804 : DECL_INTERFACE_KNOWN (decl) = 1;
12411 2572804 : DECL_NOT_REALLY_EXTERN (decl) = 1;
12412 : }
12413 :
12414 : /* If the declaration was declared inline, mark it as such. */
12415 183182584 : if (inlinep)
12416 : {
12417 68949193 : DECL_DECLARED_INLINE_P (decl) = 1;
12418 68949193 : if (publicp)
12419 68715556 : DECL_COMDAT (decl) = 1;
12420 : }
12421 68949193 : if (inlinep & 2)
12422 50347523 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12423 132835061 : else if (inlinep & 8)
12424 : {
12425 935402 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12426 935402 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12427 : }
12428 :
12429 183182584 : DECL_EXTERNAL (decl) = 1;
12430 183182584 : if (TREE_CODE (type) == FUNCTION_TYPE)
12431 : {
12432 83910133 : if (quals || rqual)
12433 272 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12434 : TYPE_UNQUALIFIED,
12435 : REF_QUAL_NONE);
12436 83910133 : auto_diagnostic_group d;
12437 83910133 : if (quals)
12438 423 : error (!ctype
12439 : ? G_("non-member function %qD cannot have cv-qualifier")
12440 : : !xobj_func_p
12441 198 : ? G_("static member function %qD cannot have cv-qualifier")
12442 : : G_("explicit object member function "
12443 : "%qD cannot have cv-qualifier"),
12444 : decl);
12445 83910133 : if (rqual)
12446 355 : error (!ctype
12447 : ? G_("non-member function %qD cannot have ref-qualifier")
12448 : : !xobj_func_p
12449 176 : ? G_("static member function %qD cannot have ref-qualifier")
12450 : : G_("explicit object member function "
12451 : "%qD cannot have ref-qualifier"),
12452 : decl);
12453 :
12454 83910133 : if (xobj_func_p && (quals || rqual))
12455 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12456 : "explicit object parameter declared here");
12457 83910133 : quals = TYPE_UNQUALIFIED;
12458 83910133 : rqual = REF_QUAL_NONE;
12459 :
12460 83910133 : }
12461 :
12462 183182584 : if (deduction_guide_p (decl))
12463 : {
12464 1166031 : tree type = TREE_TYPE (DECL_NAME (decl));
12465 1166031 : if (in_namespace == NULL_TREE
12466 1166031 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12467 : {
12468 3 : auto_diagnostic_group d;
12469 3 : error_at (location, "deduction guide %qD must be declared in the "
12470 : "same scope as %qT", decl, type);
12471 3 : inform (location_of (type), " declared here");
12472 3 : return NULL_TREE;
12473 3 : }
12474 2332056 : if (DECL_CLASS_SCOPE_P (decl)
12475 1166068 : && current_access_specifier != declared_access (TYPE_NAME (type)))
12476 : {
12477 3 : auto_diagnostic_group d;
12478 3 : error_at (location, "deduction guide %qD must have the same access "
12479 : "as %qT", decl, type);
12480 3 : inform (location_of (type), " declared here");
12481 3 : }
12482 1166028 : if (funcdef_flag)
12483 3 : error_at (location,
12484 : "deduction guide %qD must not have a function body", decl);
12485 : }
12486 339321305 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12487 193522602 : && !grok_op_properties (decl, /*complain=*/true))
12488 : return NULL_TREE;
12489 182016406 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
12490 : {
12491 297153 : bool long_long_unsigned_p;
12492 297153 : bool long_double_p;
12493 297153 : const char *suffix = NULL;
12494 : /* [over.literal]/6: Literal operators shall not have C linkage. */
12495 297153 : if (DECL_LANGUAGE (decl) == lang_c)
12496 : {
12497 6 : auto_diagnostic_group d;
12498 6 : error_at (location, "literal operator with C linkage");
12499 6 : maybe_show_extern_c_location ();
12500 6 : return NULL_TREE;
12501 6 : }
12502 :
12503 297147 : if (DECL_NAMESPACE_SCOPE_P (decl))
12504 : {
12505 297141 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12506 : &long_double_p))
12507 : {
12508 42 : error_at (location, "%qD has invalid argument list", decl);
12509 42 : return NULL_TREE;
12510 : }
12511 :
12512 297099 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12513 297099 : if (long_long_unsigned_p)
12514 : {
12515 45062 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
12516 68 : warning_at (location, 0, "integer suffix %qs"
12517 : " shadowed by implementation", suffix);
12518 : }
12519 252037 : else if (long_double_p)
12520 : {
12521 82588 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
12522 80 : warning_at (location, 0, "floating-point suffix %qs"
12523 : " shadowed by implementation", suffix);
12524 : }
12525 : /* 17.6.3.3.5 */
12526 297099 : if (suffix[0] != '_'
12527 296524 : && !current_function_decl && !(friendp && !funcdef_flag))
12528 296518 : warning_at (location, OPT_Wliteral_suffix,
12529 : "literal operator suffixes not preceded by %<_%>"
12530 : " are reserved for future standardization");
12531 : }
12532 : else
12533 : {
12534 6 : error_at (location, "%qD must be a non-member function", decl);
12535 6 : return NULL_TREE;
12536 : }
12537 : }
12538 :
12539 182016355 : if (funcdef_flag)
12540 : /* Make the init_value nonzero so pushdecl knows this is not
12541 : tentative. error_mark_node is replaced later with the BLOCK. */
12542 129110014 : DECL_INITIAL (decl) = error_mark_node;
12543 :
12544 183182380 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12545 69371173 : TREE_NOTHROW (decl) = 1;
12546 :
12547 183182380 : if (flag_openmp || flag_openmp_simd)
12548 : {
12549 : /* Adjust "omp declare simd" attributes. */
12550 473823 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
12551 473823 : if (ods)
12552 : {
12553 : tree attr;
12554 2033 : for (attr = ods; attr;
12555 1078 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
12556 : {
12557 1078 : if (TREE_CODE (type) == METHOD_TYPE)
12558 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12559 : DECL_ARGUMENTS (decl), NULL);
12560 1078 : if (TREE_VALUE (attr) != NULL_TREE)
12561 : {
12562 872 : tree cl = TREE_VALUE (TREE_VALUE (attr));
12563 872 : cl = c_omp_declare_simd_clauses_to_numbers
12564 872 : (DECL_ARGUMENTS (decl), cl);
12565 872 : if (cl)
12566 872 : TREE_VALUE (TREE_VALUE (attr)) = cl;
12567 : else
12568 0 : TREE_VALUE (attr) = NULL_TREE;
12569 : }
12570 : }
12571 : }
12572 : }
12573 :
12574 : /* Caller will do the rest of this. */
12575 183182380 : if (check < 0)
12576 : {
12577 6195010 : if (decl && decl != error_mark_node && contract_specifiers)
12578 16 : set_fn_contract_specifiers (decl, contract_specifiers);
12579 6195010 : return decl;
12580 : }
12581 :
12582 176987370 : if (ctype != NULL_TREE)
12583 109282376 : grokclassfn (ctype, decl, flags);
12584 :
12585 : /* 12.4/3 */
12586 176987370 : if (cxx_dialect >= cxx11
12587 351925548 : && DECL_DESTRUCTOR_P (decl)
12588 3147576 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12589 177147892 : && !processing_template_decl)
12590 81946 : deduce_noexcept_on_destructor (decl);
12591 :
12592 176987370 : set_originating_module (decl);
12593 :
12594 176987370 : decl = check_explicit_specialization (orig_declarator, decl,
12595 : template_count,
12596 176987370 : 2 * funcdef_flag +
12597 176987370 : 4 * (friendp != 0),
12598 : *attrlist);
12599 176987370 : if (decl == error_mark_node)
12600 : return NULL_TREE;
12601 :
12602 176987123 : if (DECL_STATIC_FUNCTION_P (decl))
12603 9931162 : check_static_quals (decl, quals);
12604 :
12605 176987123 : if (attrlist)
12606 : {
12607 176987123 : cplus_decl_attributes (&decl, *attrlist, 0);
12608 176987123 : *attrlist = NULL_TREE;
12609 : }
12610 :
12611 : /* Update now we have a decl and maybe know the return type. */
12612 176987123 : if (contract_specifiers)
12613 : {
12614 545 : tree t = decl;
12615 545 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12616 2 : t = DECL_TEMPLATE_RESULT (decl);
12617 545 : set_fn_contract_specifiers (t, contract_specifiers);
12618 545 : rebuild_postconditions (t);
12619 : }
12620 :
12621 : /* Check main's type after attributes have been applied. */
12622 176987123 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12623 : {
12624 33592 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12625 : integer_type_node))
12626 : {
12627 19 : tree dtype = TREE_TYPE (decl);
12628 19 : tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12629 19 : tree newtype;
12630 19 : error_at (declspecs->locations[ds_type_spec],
12631 : "%<::main%> must return %<int%>");
12632 57 : newtype = build_function_type (integer_type_node, oldtypeargs,
12633 19 : TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12634 19 : TREE_TYPE (decl) = newtype;
12635 : }
12636 33592 : if (warn_main)
12637 33570 : check_main_parameter_types (decl);
12638 : }
12639 :
12640 176987123 : if (ctype != NULL_TREE && check)
12641 : {
12642 8733899 : tree old_decl = check_classfn (ctype, decl,
12643 8733899 : (current_template_depth
12644 8733899 : > template_class_depth (ctype))
12645 2119430 : ? current_template_parms
12646 : : NULL_TREE);
12647 :
12648 8733899 : if (old_decl == error_mark_node)
12649 : return NULL_TREE;
12650 :
12651 8733791 : if (old_decl)
12652 : {
12653 8350536 : tree ok;
12654 8350536 : tree pushed_scope;
12655 :
12656 8350536 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12657 : /* Because grokfndecl is always supposed to return a
12658 : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12659 : here. We depend on our callers to figure out that its
12660 : really a template that's being returned. */
12661 2119418 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
12662 :
12663 8350536 : if (DECL_STATIC_FUNCTION_P (old_decl)
12664 8350536 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12665 : {
12666 : /* Remove the `this' parm added by grokclassfn. */
12667 167449 : revert_static_member_fn (decl);
12668 167449 : check_static_quals (decl, quals);
12669 : }
12670 8350536 : if (DECL_ARTIFICIAL (old_decl))
12671 : {
12672 9 : error ("definition of implicitly-declared %qD", old_decl);
12673 9 : return NULL_TREE;
12674 : }
12675 8350527 : else if (DECL_DEFAULTED_FN (old_decl))
12676 : {
12677 3 : auto_diagnostic_group d;
12678 3 : error ("definition of explicitly-defaulted %q+D", decl);
12679 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12680 : "%q#D explicitly defaulted here", old_decl);
12681 3 : return NULL_TREE;
12682 3 : }
12683 :
12684 8350524 : if (modules_p () && !module_may_redeclare (old_decl))
12685 : return NULL_TREE;
12686 :
12687 : /* Since we've smashed OLD_DECL to its
12688 : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12689 8350464 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12690 1012 : decl = DECL_TEMPLATE_RESULT (decl);
12691 :
12692 : /* Attempt to merge the declarations. This can fail, in
12693 : the case of some invalid specialization declarations. */
12694 8350464 : pushed_scope = push_scope (ctype);
12695 8350464 : ok = duplicate_decls (decl, old_decl);
12696 8350464 : if (pushed_scope)
12697 8350464 : pop_scope (pushed_scope);
12698 8350464 : if (!ok)
12699 : {
12700 0 : error ("no %q#D member function declared in class %qT",
12701 : decl, ctype);
12702 0 : return NULL_TREE;
12703 : }
12704 8350464 : if (ok == error_mark_node)
12705 : return NULL_TREE;
12706 : return old_decl;
12707 : }
12708 : }
12709 :
12710 337272958 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12711 : return NULL_TREE;
12712 :
12713 : /* Don't call check_consteval_only_fn for defaulted functions. Those are
12714 : immediate-escalating functions but at this point DECL_DEFAULTED_P has
12715 : not been set. */
12716 168636476 : if (initialized != SD_DEFAULTED)
12717 162259310 : check_consteval_only_fn (decl);
12718 :
12719 168636476 : if (ctype == NULL_TREE || check)
12720 68088059 : return decl;
12721 :
12722 100548417 : if (virtualp)
12723 4022207 : DECL_VIRTUAL_P (decl) = 1;
12724 :
12725 100548417 : return decl;
12726 : }
12727 :
12728 : /* decl is a FUNCTION_DECL.
12729 : specifiers are the parsed virt-specifiers.
12730 :
12731 : Set flags to reflect the virt-specifiers.
12732 :
12733 : Returns decl. */
12734 :
12735 : static tree
12736 106743524 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12737 : {
12738 106743524 : if (decl == NULL_TREE)
12739 : return decl;
12740 106743427 : if (specifiers & VIRT_SPEC_OVERRIDE)
12741 318580 : DECL_OVERRIDE_P (decl) = 1;
12742 106743427 : if (specifiers & VIRT_SPEC_FINAL)
12743 9530 : DECL_FINAL_P (decl) = 1;
12744 : return decl;
12745 : }
12746 :
12747 : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12748 : the linkage that DECL will receive in the object file. */
12749 :
12750 : static void
12751 12992265 : set_linkage_for_static_data_member (tree decl)
12752 : {
12753 : /* A static data member always has static storage duration and
12754 : external linkage. Note that static data members are forbidden in
12755 : local classes -- the only situation in which a class has
12756 : non-external linkage. */
12757 12992265 : TREE_PUBLIC (decl) = 1;
12758 12992265 : TREE_STATIC (decl) = 1;
12759 : /* For non-template classes, static data members are always put
12760 : out in exactly those files where they are defined, just as
12761 : with ordinary namespace-scope variables. */
12762 12992265 : if (!processing_template_decl)
12763 9869876 : DECL_INTERFACE_KNOWN (decl) = 1;
12764 12992265 : }
12765 :
12766 : /* Create a VAR_DECL named NAME with the indicated TYPE.
12767 :
12768 : If SCOPE is non-NULL, it is the class type or namespace containing
12769 : the variable. If SCOPE is NULL, the variable should is created in
12770 : the innermost enclosing scope. */
12771 :
12772 : static tree
12773 66349842 : grokvardecl (tree type,
12774 : tree name,
12775 : tree orig_declarator,
12776 : const cp_decl_specifier_seq *declspecs,
12777 : int initialized,
12778 : int type_quals,
12779 : int inlinep,
12780 : bool conceptp,
12781 : int template_count,
12782 : tree scope,
12783 : location_t location)
12784 : {
12785 66349842 : tree decl;
12786 66349842 : tree explicit_scope;
12787 :
12788 66349842 : gcc_assert (!name || identifier_p (name));
12789 :
12790 66349842 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12791 66349842 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12792 :
12793 : /* Compute the scope in which to place the variable, but remember
12794 : whether or not that scope was explicitly specified by the user. */
12795 66349842 : explicit_scope = scope;
12796 66349842 : if (!scope)
12797 : {
12798 : /* An explicit "extern" specifier indicates a namespace-scope
12799 : variable. */
12800 65858354 : if (declspecs->storage_class == sc_extern)
12801 540785 : scope = current_decl_namespace ();
12802 65317569 : else if (!at_function_scope_p ())
12803 6815463 : scope = current_scope ();
12804 : }
12805 :
12806 65858354 : if (scope
12807 7847736 : && (/* If the variable is a namespace-scope variable declared in a
12808 : template, we need DECL_LANG_SPECIFIC. */
12809 7847736 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
12810 : /* Similarly for namespace-scope variables with language linkage
12811 : other than C++. */
12812 7370003 : || (TREE_CODE (scope) == NAMESPACE_DECL
12813 4270658 : && current_lang_name != lang_name_cplusplus)
12814 : /* Similarly for static data members. */
12815 4523340 : || TYPE_P (scope)
12816 : /* Similarly for explicit specializations. */
12817 4045607 : || (orig_declarator
12818 4044949 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
12819 4612604 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
12820 : else
12821 61737238 : decl = build_decl (location, VAR_DECL, name, type);
12822 :
12823 66349842 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
12824 13755 : set_decl_namespace (decl, explicit_scope, 0);
12825 : else
12826 66336087 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
12827 :
12828 66349842 : if (declspecs->storage_class == sc_extern)
12829 : {
12830 540785 : DECL_THIS_EXTERN (decl) = 1;
12831 540785 : DECL_EXTERNAL (decl) = !initialized;
12832 : }
12833 :
12834 66349842 : if (DECL_CLASS_SCOPE_P (decl))
12835 : {
12836 477733 : set_linkage_for_static_data_member (decl);
12837 : /* This function is only called with out-of-class definitions. */
12838 477733 : DECL_EXTERNAL (decl) = 0;
12839 477733 : check_class_member_definition_namespace (decl);
12840 : }
12841 : /* At top level, either `static' or no s.c. makes a definition
12842 : (perhaps tentative), and absence of `static' makes it public. */
12843 65872109 : else if (toplevel_bindings_p ())
12844 : {
12845 14737706 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
12846 7368853 : && (DECL_THIS_EXTERN (decl)
12847 6790227 : || ! constp
12848 6790227 : || volatilep
12849 6713965 : || inlinep
12850 513116 : || in_template_context
12851 28467 : || processing_specialization
12852 28398 : || module_attach_p ()));
12853 7368853 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
12854 : }
12855 : /* Not at top level, only `static' makes a static definition. */
12856 : else
12857 : {
12858 58503256 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
12859 58503256 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
12860 : }
12861 :
12862 66349842 : set_originating_module (decl);
12863 :
12864 66349842 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
12865 : {
12866 19308 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
12867 : {
12868 19300 : CP_DECL_THREAD_LOCAL_P (decl) = true;
12869 : // NB: Set a tentative TLS model to avoid tls_model attribute
12870 : // warnings due to lack of thread storage duration. It will
12871 : // be updated by cplus_decl_attributes later.
12872 19300 : if (!processing_template_decl)
12873 19176 : set_decl_tls_model (decl, TLS_MODEL_REAL);
12874 : }
12875 19308 : if (declspecs->gnu_thread_keyword_p)
12876 18312 : SET_DECL_GNU_TLS_P (decl);
12877 : }
12878 :
12879 : /* If the type of the decl has no linkage, make sure that we'll
12880 : notice that in mark_used. */
12881 66349842 : if (cxx_dialect > cxx98
12882 66080546 : && decl_linkage (decl) != lk_none
12883 65415801 : && DECL_LANG_SPECIFIC (decl) == NULL
12884 60724662 : && !DECL_EXTERN_C_P (decl)
12885 127074504 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
12886 4250 : retrofit_lang_decl (decl);
12887 :
12888 66349842 : if (TREE_PUBLIC (decl))
12889 : {
12890 : /* [basic.link]: A name with no linkage (notably, the name of a class
12891 : or enumeration declared in a local scope) shall not be used to
12892 : declare an entity with linkage.
12893 :
12894 : DR 757 relaxes this restriction for C++0x. */
12895 7780354 : if (cxx_dialect < cxx11)
12896 21344 : no_linkage_error (decl);
12897 : }
12898 : else
12899 58569488 : DECL_INTERFACE_KNOWN (decl) = 1;
12900 :
12901 66349842 : if (DECL_NAME (decl)
12902 66349842 : && MAIN_NAME_P (DECL_NAME (decl)))
12903 : {
12904 12 : if (scope == global_namespace)
12905 6 : error_at (DECL_SOURCE_LOCATION (decl),
12906 : "cannot declare %<::main%> to be a global variable");
12907 6 : else if (DECL_EXTERN_C_P (decl))
12908 3 : error_at (DECL_SOURCE_LOCATION (decl),
12909 : "an entity named %<main%> cannot be declared with "
12910 : "C language linkage");
12911 : }
12912 :
12913 : /* Check if a variable is being declared as a concept. */
12914 66349842 : if (conceptp)
12915 : {
12916 30 : if (!processing_template_decl)
12917 13 : error_at (declspecs->locations[ds_concept],
12918 : "a non-template variable cannot be %<concept%>");
12919 17 : else if (!at_namespace_scope_p ())
12920 2 : error_at (declspecs->locations[ds_concept],
12921 : "concept must be defined at namespace scope");
12922 : else
12923 15 : error_at (declspecs->locations[ds_concept],
12924 : "variable concepts are no longer supported");
12925 30 : return NULL_TREE;
12926 : }
12927 66349812 : else if (flag_concepts
12928 66349812 : && current_template_depth > template_class_depth (scope))
12929 : {
12930 57062158 : tree ci = current_template_constraints ();
12931 57062158 : set_constraints (decl, ci);
12932 : }
12933 :
12934 : // Handle explicit specializations and instantiations of variable templates.
12935 66349812 : if (orig_declarator)
12936 65677383 : decl = check_explicit_specialization (orig_declarator, decl,
12937 : template_count, 0);
12938 :
12939 66349812 : return decl != error_mark_node ? decl : NULL_TREE;
12940 : }
12941 :
12942 : /* Create and return a canonical pointer to member function type, for
12943 : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
12944 :
12945 : tree
12946 3072321 : build_ptrmemfunc_type (tree type)
12947 : {
12948 3072321 : tree field, fields;
12949 3072321 : tree t;
12950 :
12951 3072321 : if (type == error_mark_node)
12952 : return type;
12953 :
12954 : /* Make sure that we always have the unqualified pointer-to-member
12955 : type first. */
12956 3072321 : if (cp_cv_quals quals = cp_type_quals (type))
12957 : {
12958 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
12959 0 : return cp_build_qualified_type (unqual, quals);
12960 : }
12961 :
12962 : /* If a canonical type already exists for this type, use it. We use
12963 : this method instead of type_hash_canon, because it only does a
12964 : simple equality check on the list of field members. */
12965 :
12966 3072321 : t = TYPE_PTRMEMFUNC_TYPE (type);
12967 3072321 : if (t)
12968 : return t;
12969 :
12970 1015669 : t = make_node (RECORD_TYPE);
12971 :
12972 : /* Let the front end know this is a pointer to member function. */
12973 1015669 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
12974 :
12975 1015669 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
12976 1015669 : DECL_NONADDRESSABLE_P (field) = 1;
12977 1015669 : fields = field;
12978 :
12979 1015669 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
12980 : delta_type_node);
12981 1015669 : DECL_NONADDRESSABLE_P (field) = 1;
12982 1015669 : DECL_CHAIN (field) = fields;
12983 1015669 : fields = field;
12984 :
12985 1015669 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
12986 :
12987 : /* Zap out the name so that the back end will give us the debugging
12988 : information for this anonymous RECORD_TYPE. */
12989 1015669 : TYPE_NAME (t) = NULL_TREE;
12990 :
12991 : /* Cache this pointer-to-member type so that we can find it again
12992 : later. */
12993 1015669 : TYPE_PTRMEMFUNC_TYPE (type) = t;
12994 :
12995 1015669 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
12996 494784 : SET_TYPE_STRUCTURAL_EQUALITY (t);
12997 520885 : else if (TYPE_CANONICAL (type) != type)
12998 338601 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
12999 :
13000 : return t;
13001 : }
13002 :
13003 : /* Create and return a pointer to data member type. */
13004 :
13005 : tree
13006 315324 : build_ptrmem_type (tree class_type, tree member_type)
13007 : {
13008 315324 : if (TREE_CODE (member_type) == METHOD_TYPE)
13009 : {
13010 57965 : cp_cv_quals quals = type_memfn_quals (member_type);
13011 57965 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
13012 57965 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
13013 57965 : return build_ptrmemfunc_type (build_pointer_type (member_type));
13014 : }
13015 : else
13016 : {
13017 257359 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
13018 257359 : return build_offset_type (class_type, member_type);
13019 : }
13020 : }
13021 :
13022 : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
13023 : Check to see that the definition is valid. Issue appropriate error
13024 : messages. */
13025 :
13026 : static void
13027 18276707 : check_static_variable_definition (tree decl, tree type)
13028 : {
13029 : /* Avoid redundant diagnostics on out-of-class definitions. */
13030 18276707 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
13031 : ;
13032 : /* Can't check yet if we don't know the type. */
13033 16732185 : else if (dependent_type_p (type))
13034 : ;
13035 : /* If DECL is declared constexpr, we'll do the appropriate checks
13036 : in check_initializer. Similarly for inline static data members. */
13037 16212278 : else if (DECL_P (decl)
13038 16212278 : && (DECL_DECLARED_CONSTEXPR_P (decl)
13039 2616913 : || DECL_VAR_DECLARED_INLINE_P (decl)))
13040 : ;
13041 2616437 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13042 : {
13043 56 : if (!COMPLETE_TYPE_P (type))
13044 11 : error_at (DECL_SOURCE_LOCATION (decl),
13045 : "in-class initialization of static data member %q#D of "
13046 : "incomplete type", decl);
13047 45 : else if (literal_type_p (type))
13048 39 : permerror (DECL_SOURCE_LOCATION (decl),
13049 : "%<constexpr%> needed for in-class initialization of "
13050 : "static data member %q#D of non-integral type", decl);
13051 : else
13052 6 : error_at (DECL_SOURCE_LOCATION (decl),
13053 : "in-class initialization of static data member %q#D of "
13054 : "non-literal type", decl);
13055 : }
13056 : /* Motion 10 at San Diego: If a static const integral data member is
13057 : initialized with an integral constant expression, the initializer
13058 : may appear either in the declaration (within the class), or in
13059 : the definition, but not both. If it appears in the class, the
13060 : member is a member constant. The file-scope definition is always
13061 : required. */
13062 2616381 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
13063 11 : error_at (DECL_SOURCE_LOCATION (decl),
13064 : "invalid in-class initialization of static data member "
13065 : "of non-integral type %qT",
13066 : type);
13067 2616370 : else if (!CP_TYPE_CONST_P (type))
13068 5 : error_at (DECL_SOURCE_LOCATION (decl),
13069 : "ISO C++ forbids in-class initialization of non-const "
13070 : "static member %qD",
13071 : decl);
13072 2616365 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13073 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13074 : "ISO C++ forbids initialization of member constant "
13075 : "%qD of non-integral type %qT", decl, type);
13076 18276707 : }
13077 :
13078 : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
13079 : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
13080 : expressions out into temporary variables so that walk_tree doesn't
13081 : step into them (c++/15764). */
13082 :
13083 : static tree
13084 3404 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
13085 : {
13086 3404 : hash_set<tree> *pset = (hash_set<tree> *)data;
13087 3404 : tree expr = *expr_p;
13088 3404 : if (TREE_CODE (expr) == SAVE_EXPR)
13089 : {
13090 1483 : tree op = TREE_OPERAND (expr, 0);
13091 1483 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
13092 1483 : if (TREE_SIDE_EFFECTS (op))
13093 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
13094 1483 : *walk_subtrees = 0;
13095 : }
13096 1921 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
13097 1746 : *walk_subtrees = 0;
13098 3404 : return NULL;
13099 : }
13100 :
13101 : /* Entry point for the above. */
13102 :
13103 : static void
13104 1542 : stabilize_vla_size (tree size)
13105 : {
13106 1542 : hash_set<tree> pset;
13107 : /* Break out any function calls into temporary variables. */
13108 1542 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
13109 1542 : }
13110 :
13111 : /* Reduce a SIZEOF_EXPR to its value. */
13112 :
13113 : tree
13114 14284679 : fold_sizeof_expr (tree t)
13115 : {
13116 14284679 : tree r;
13117 14284679 : if (SIZEOF_EXPR_TYPE_P (t))
13118 13071005 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13119 13071005 : TREE_TYPE (TREE_OPERAND (t, 0)),
13120 : SIZEOF_EXPR, false, false);
13121 1213674 : else if (TYPE_P (TREE_OPERAND (t, 0)))
13122 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13123 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
13124 : false, false);
13125 : else
13126 1213674 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
13127 1213674 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
13128 : false, false);
13129 14284679 : if (r == error_mark_node)
13130 67 : r = size_one_node;
13131 14284679 : r = cp_fold_convert (size_type_node, r);
13132 14284679 : return r;
13133 : }
13134 :
13135 : /* Given the SIZE (i.e., number of elements) in an array, compute
13136 : an appropriate index type for the array. If non-NULL, NAME is
13137 : the name of the entity being declared. */
13138 :
13139 : static tree
13140 3603531 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
13141 : tsubst_flags_t complain)
13142 : {
13143 3603531 : if (error_operand_p (size))
13144 32 : return error_mark_node;
13145 :
13146 : /* The type of the index being computed. */
13147 3603499 : tree itype;
13148 :
13149 : /* The original numeric size as seen in the source code before
13150 : conversion to size_t. */
13151 3603499 : tree origsize = size;
13152 :
13153 3603499 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
13154 :
13155 3603499 : if (!type_dependent_expression_p (size))
13156 : {
13157 3559117 : origsize = size = mark_rvalue_use (size);
13158 :
13159 35967 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
13160 3559277 : && TREE_SIDE_EFFECTS (size))
13161 : /* In C++98, we mark a non-constant array bound with a magic
13162 : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
13163 : else
13164 : {
13165 3559116 : size = build_converted_constant_expr (size_type_node, size, complain);
13166 : /* Pedantically a constant expression is required here and so
13167 : __builtin_is_constant_evaluated () should fold to true if it
13168 : is successfully folded into a constant. */
13169 3559116 : size = fold_non_dependent_expr (size, complain,
13170 : /*manifestly_const_eval=*/true);
13171 :
13172 3559116 : if (!TREE_CONSTANT (size))
13173 124855 : size = origsize;
13174 : }
13175 :
13176 3559117 : if (error_operand_p (size))
13177 3 : return error_mark_node;
13178 :
13179 : /* The array bound must be an integer type. */
13180 3559114 : tree type = TREE_TYPE (size);
13181 3559114 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
13182 : {
13183 21 : if (!(complain & tf_error))
13184 0 : return error_mark_node;
13185 21 : if (name)
13186 18 : error_at (loc, "size of array %qD has non-integral type %qT",
13187 : name, type);
13188 : else
13189 3 : error_at (loc, "size of array has non-integral type %qT", type);
13190 21 : size = integer_one_node;
13191 : }
13192 : }
13193 :
13194 : /* A type is dependent if it is...an array type constructed from any
13195 : dependent type or whose size is specified by a constant expression
13196 : that is value-dependent. */
13197 : /* We can only call value_dependent_expression_p on integral constant
13198 : expressions. */
13199 3603496 : if (processing_template_decl
13200 1508775 : && potential_constant_expression (size)
13201 5112246 : && value_dependent_expression_p (size))
13202 : {
13203 : /* Just build the index type and mark that it requires
13204 : structural equality checks. */
13205 893108 : in_template:
13206 893108 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13207 : size, size_one_node));
13208 893108 : TYPE_DEPENDENT_P (itype) = 1;
13209 893108 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13210 893108 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
13211 893108 : return itype;
13212 : }
13213 :
13214 2721270 : if (TREE_CODE (size) != INTEGER_CST)
13215 : {
13216 12802 : tree folded = cp_fully_fold (size);
13217 12802 : if (TREE_CODE (folded) == INTEGER_CST)
13218 : {
13219 224 : if (name)
13220 150 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13221 : "integral constant-expression", name);
13222 : else
13223 74 : pedwarn (loc, OPT_Wpedantic,
13224 : "size of array is not an integral constant-expression");
13225 : }
13226 12802 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13227 : /* We might have lost the TREE_CONSTANT flag e.g. when we are
13228 : folding a conversion from a pointer to integral type. In that
13229 : case issue an error below and don't treat this as a VLA. */;
13230 : else
13231 : /* Use the folded result for VLAs, too; it will have resolved
13232 : SIZEOF_EXPR. */
13233 : size = folded;
13234 : }
13235 :
13236 : /* Normally, the array-bound will be a constant. */
13237 2721270 : if (TREE_CODE (size) == INTEGER_CST)
13238 : {
13239 : /* The size to use in diagnostics that reflects the constant
13240 : size used in the source, rather than SIZE massaged above. */
13241 2708692 : tree diagsize = size;
13242 :
13243 : /* If the original size before conversion to size_t was signed
13244 : and negative, convert it to ssizetype to restore the sign. */
13245 2708692 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13246 : && TREE_CODE (size) == INTEGER_CST
13247 2708692 : && tree_int_cst_sign_bit (size))
13248 : {
13249 243 : diagsize = fold_convert (ssizetype, size);
13250 :
13251 : /* Clear the overflow bit that may have been set as a result
13252 : of the conversion from the sizetype of the new size to
13253 : ssizetype. */
13254 243 : TREE_OVERFLOW (diagsize) = false;
13255 : }
13256 :
13257 : /* Verify that the array has a positive number of elements
13258 : and issue the appropriate diagnostic if it doesn't. */
13259 2708692 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13260 : {
13261 474 : if (!(complain & tf_error))
13262 15 : return error_mark_node;
13263 459 : size = integer_one_node;
13264 : }
13265 : /* As an extension we allow zero-sized arrays. */
13266 2708218 : else if (integer_zerop (size))
13267 : {
13268 3178 : if (!(complain & tf_error))
13269 : /* We must fail if performing argument deduction (as
13270 : indicated by the state of complain), so that
13271 : another substitution can be found. */
13272 8 : return error_mark_node;
13273 3170 : else if (name)
13274 2891 : pedwarn (loc, OPT_Wpedantic,
13275 : "ISO C++ forbids zero-size array %qD", name);
13276 : else
13277 279 : pedwarn (loc, OPT_Wpedantic,
13278 : "ISO C++ forbids zero-size array");
13279 : }
13280 : }
13281 12578 : else if (TREE_CONSTANT (size)
13282 : /* We don't allow VLAs at non-function scopes, or during
13283 : tentative template substitution. */
13284 12563 : || !at_function_scope_p ()
13285 25008 : || !(complain & tf_error))
13286 : {
13287 154 : if (!(complain & tf_error))
13288 109 : return error_mark_node;
13289 : /* `(int) &fn' is not a valid array bound. */
13290 45 : if (name)
13291 37 : error_at (loc,
13292 : "size of array %qD is not an integral constant-expression",
13293 : name);
13294 : else
13295 8 : error_at (loc, "size of array is not an integral constant-expression");
13296 45 : size = integer_one_node;
13297 : }
13298 12424 : else if (pedantic && warn_vla != 0)
13299 : {
13300 30 : if (name)
13301 15 : pedwarn (name_loc, OPT_Wvla,
13302 : "ISO C++ forbids variable length array %qD", name);
13303 : else
13304 15 : pedwarn (input_location, OPT_Wvla,
13305 : "ISO C++ forbids variable length array");
13306 : }
13307 12394 : else if (warn_vla > 0)
13308 : {
13309 3 : if (name)
13310 3 : warning_at (name_loc, OPT_Wvla,
13311 : "variable length array %qD is used", name);
13312 : else
13313 0 : warning (OPT_Wvla,
13314 : "variable length array is used");
13315 : }
13316 :
13317 2721138 : if (processing_template_decl && !TREE_CONSTANT (size))
13318 10882 : goto in_template;
13319 : else
13320 : {
13321 2710256 : if (!TREE_CONSTANT (size))
13322 : {
13323 : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13324 : of the MINUS_EXPR, which allows the -1 to get folded with the +1
13325 : that happens when building TYPE_SIZE. */
13326 1542 : size = variable_size (size);
13327 1542 : stabilize_vla_size (size);
13328 : }
13329 :
13330 : /* Compute the index of the largest element in the array. It is
13331 : one less than the number of elements in the array. We save
13332 : and restore PROCESSING_TEMPLATE_DECL so that computations in
13333 : cp_build_binary_op will be appropriately folded. */
13334 2710256 : {
13335 2710256 : processing_template_decl_sentinel s;
13336 2710256 : itype = cp_build_binary_op (input_location,
13337 : MINUS_EXPR,
13338 : cp_convert (ssizetype, size, complain),
13339 : cp_convert (ssizetype, integer_one_node,
13340 : complain),
13341 : complain);
13342 2710256 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13343 2710256 : }
13344 :
13345 2710256 : if (!TREE_CONSTANT (itype))
13346 : {
13347 1542 : if (sanitize_flags_p (SANITIZE_VLA)
13348 1542 : && current_function_decl != NULL_TREE)
13349 : {
13350 : /* We have to add 1 -- in the ubsan routine we generate
13351 : LE_EXPR rather than LT_EXPR. */
13352 78 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13353 : build_one_cst (TREE_TYPE (itype)));
13354 78 : t = ubsan_instrument_vla (input_location, t);
13355 78 : finish_expr_stmt (t);
13356 : }
13357 : }
13358 : /* Make sure that there was no overflow when creating to a signed
13359 : index type. (For example, on a 32-bit machine, an array with
13360 : size 2^32 - 1 is too big.) */
13361 2708714 : else if (TREE_CODE (itype) == INTEGER_CST
13362 2708714 : && TREE_OVERFLOW (itype))
13363 : {
13364 0 : if (!(complain & tf_error))
13365 0 : return error_mark_node;
13366 0 : error ("overflow in array dimension");
13367 0 : TREE_OVERFLOW (itype) = 0;
13368 : }
13369 : }
13370 :
13371 : /* Create and return the appropriate index type. */
13372 2710256 : itype = build_index_type (itype);
13373 :
13374 : /* If the index type were dependent, we would have returned early, so
13375 : remember that it isn't. */
13376 2710256 : TYPE_DEPENDENT_P (itype) = 0;
13377 2710256 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13378 2710256 : return itype;
13379 : }
13380 :
13381 : tree
13382 922549 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13383 : {
13384 922549 : return compute_array_index_type_loc (input_location, name, size, complain);
13385 : }
13386 :
13387 : /* Returns the scope (if any) in which the entity declared by
13388 : DECLARATOR will be located. If the entity was declared with an
13389 : unqualified name, NULL_TREE is returned. */
13390 :
13391 : tree
13392 729744935 : get_scope_of_declarator (const cp_declarator *declarator)
13393 : {
13394 845647628 : while (declarator && declarator->kind != cdk_id)
13395 115902693 : declarator = declarator->declarator;
13396 :
13397 : /* If the declarator-id is a SCOPE_REF, the scope in which the
13398 : declaration occurs is the first operand. */
13399 729744935 : if (declarator
13400 728551810 : && declarator->u.id.qualifying_scope)
13401 : return declarator->u.id.qualifying_scope;
13402 :
13403 : /* Otherwise, the declarator is not a qualified name; the entity will
13404 : be declared in the current scope. */
13405 : return NULL_TREE;
13406 : }
13407 :
13408 : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
13409 : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13410 : with this type. */
13411 :
13412 : static tree
13413 3461479 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13414 : {
13415 3461479 : tree itype = NULL_TREE;
13416 :
13417 : /* If things have already gone awry, bail now. */
13418 3461479 : if (type == error_mark_node || size == error_mark_node)
13419 : return error_mark_node;
13420 :
13421 : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13422 : for a deduced class type. */
13423 3461416 : if (template_placeholder_p (type))
13424 : {
13425 6 : if (name)
13426 0 : error_at (loc, "%qD declared as array of template placeholder "
13427 : "type %qT", name, type);
13428 : else
13429 6 : error ("creating array of template placeholder type %qT", type);
13430 6 : return error_mark_node;
13431 : }
13432 :
13433 : /* If there are some types which cannot be array elements,
13434 : issue an error-message and return. */
13435 3461410 : switch (TREE_CODE (type))
13436 : {
13437 3 : case VOID_TYPE:
13438 3 : if (name)
13439 0 : error_at (loc, "declaration of %qD as array of void", name);
13440 : else
13441 3 : error ("creating array of void");
13442 3 : return error_mark_node;
13443 :
13444 6 : case FUNCTION_TYPE:
13445 6 : if (name)
13446 6 : error_at (loc, "declaration of %qD as array of functions", name);
13447 : else
13448 0 : error ("creating array of functions");
13449 6 : return error_mark_node;
13450 :
13451 21 : case REFERENCE_TYPE:
13452 21 : if (name)
13453 15 : error_at (loc, "declaration of %qD as array of references", name);
13454 : else
13455 6 : error ("creating array of references");
13456 21 : return error_mark_node;
13457 :
13458 0 : case METHOD_TYPE:
13459 0 : if (name)
13460 0 : error_at (loc, "declaration of %qD as array of function members",
13461 : name);
13462 : else
13463 0 : error ("creating array of function members");
13464 0 : return error_mark_node;
13465 :
13466 3461380 : default:
13467 3461380 : break;
13468 : }
13469 :
13470 3461380 : if (!verify_type_context (name ? loc : input_location,
13471 : TCTX_ARRAY_ELEMENT, type))
13472 0 : return error_mark_node;
13473 :
13474 : /* [dcl.array]
13475 :
13476 : The constant expressions that specify the bounds of the arrays
13477 : can be omitted only for the first member of the sequence. */
13478 3461380 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13479 : {
13480 30 : if (name)
13481 24 : error_at (loc, "declaration of %qD as multidimensional array must "
13482 : "have bounds for all dimensions except the first",
13483 : name);
13484 : else
13485 6 : error ("multidimensional array must have bounds for all "
13486 : "dimensions except the first");
13487 :
13488 30 : return error_mark_node;
13489 : }
13490 :
13491 : /* Figure out the index type for the array. */
13492 3461350 : if (size)
13493 : {
13494 2680982 : itype = compute_array_index_type_loc (loc, name, size,
13495 : tf_warning_or_error);
13496 2680982 : if (type_uses_auto (type)
13497 2680982 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13498 : {
13499 9 : sorry_at (loc, "variable-length array of %<auto%>");
13500 9 : return error_mark_node;
13501 : }
13502 : }
13503 :
13504 3461341 : return build_cplus_array_type (type, itype);
13505 : }
13506 :
13507 : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
13508 :
13509 : static location_t
13510 1010321823 : min_location (location_t loca, location_t locb)
13511 : {
13512 1010321823 : if (loca == UNKNOWN_LOCATION
13513 1010321823 : || (locb != UNKNOWN_LOCATION
13514 105910305 : && linemap_location_before_p (line_table, locb, loca)))
13515 905552520 : return locb;
13516 : return loca;
13517 : }
13518 :
13519 : /* Returns the smallest location != UNKNOWN_LOCATION among the
13520 : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13521 : and LOCATIONS[ds_restrict]. */
13522 :
13523 : static location_t
13524 1008671335 : smallest_type_quals_location (int type_quals, const location_t* locations)
13525 : {
13526 1008671335 : location_t loc = UNKNOWN_LOCATION;
13527 :
13528 1008671335 : if (type_quals & TYPE_QUAL_CONST)
13529 104501075 : loc = locations[ds_const];
13530 :
13531 1008671335 : if (type_quals & TYPE_QUAL_VOLATILE)
13532 1650397 : loc = min_location (loc, locations[ds_volatile]);
13533 :
13534 1008671335 : if (type_quals & TYPE_QUAL_RESTRICT)
13535 43 : loc = min_location (loc, locations[ds_restrict]);
13536 :
13537 1008671335 : return loc;
13538 : }
13539 :
13540 : /* Returns the smallest among the latter and locations[ds_type_spec]. */
13541 :
13542 : static location_t
13543 1008671314 : smallest_type_location (int type_quals, const location_t* locations)
13544 : {
13545 1008671314 : location_t loc = smallest_type_quals_location (type_quals, locations);
13546 1008671314 : return min_location (loc, locations[ds_type_spec]);
13547 : }
13548 :
13549 : static location_t
13550 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
13551 : {
13552 26 : int type_quals = get_type_quals (declspecs);
13553 26 : return smallest_type_location (type_quals, declspecs->locations);
13554 : }
13555 :
13556 : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
13557 : strip out the pointer/reference declarator(s). */
13558 :
13559 : static bool
13560 28823534 : maybe_strip_indirect_ref (const cp_declarator** declarator)
13561 : {
13562 28823534 : bool indirect_ref_p = false;
13563 57647215 : while (declarator && *declarator
13564 28823681 : && ((*declarator)->kind == cdk_pointer
13565 28823681 : || (*declarator)->kind == cdk_reference))
13566 : {
13567 147 : indirect_ref_p = true;
13568 147 : *declarator = (*declarator)->declarator;
13569 : }
13570 28823534 : return indirect_ref_p;
13571 : }
13572 :
13573 : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13574 : and DECLARATOR. SFK indicates the kind of special function (if any) that
13575 : this function is. OPTYPE is the type given in a conversion operator
13576 : declaration, or the class type for a constructor/destructor.
13577 : Returns the actual return type of the function; that may be different
13578 : than TYPE if an error occurs, or for certain special functions. */
13579 :
13580 : static tree
13581 28823534 : check_special_function_return_type (special_function_kind sfk,
13582 : tree type,
13583 : tree optype,
13584 : int type_quals,
13585 : const cp_declarator** declarator,
13586 : const location_t* locations)
13587 : {
13588 28823534 : gcc_assert (declarator);
13589 28823534 : location_t rettype_loc = (type
13590 28823534 : ? smallest_type_location (type_quals, locations)
13591 28823453 : : (*declarator)->id_loc);
13592 28823534 : switch (sfk)
13593 : {
13594 23503978 : case sfk_constructor:
13595 23503978 : if (maybe_strip_indirect_ref (declarator) || type)
13596 81 : error_at (rettype_loc,
13597 : "return type specification for constructor invalid");
13598 23503897 : else if (type_quals != TYPE_UNQUALIFIED)
13599 15 : error_at (smallest_type_quals_location (type_quals, locations),
13600 : "qualifiers are not allowed on constructor declaration");
13601 :
13602 23503978 : if (targetm.cxx.cdtor_returns_this ())
13603 0 : type = build_pointer_type (optype);
13604 : else
13605 23503978 : type = void_type_node;
13606 : break;
13607 :
13608 3166358 : case sfk_destructor:
13609 3166358 : if (maybe_strip_indirect_ref (declarator) || type)
13610 45 : error_at (rettype_loc,
13611 : "return type specification for destructor invalid");
13612 3166313 : else if (type_quals != TYPE_UNQUALIFIED)
13613 3 : error_at (smallest_type_quals_location (type_quals, locations),
13614 : "qualifiers are not allowed on destructor declaration");
13615 :
13616 : /* We can't use the proper return type here because we run into
13617 : problems with ambiguous bases and covariant returns. */
13618 3166358 : if (targetm.cxx.cdtor_returns_this ())
13619 0 : type = build_pointer_type (void_type_node);
13620 : else
13621 3166358 : type = void_type_node;
13622 : break;
13623 :
13624 987161 : case sfk_conversion:
13625 987161 : if (maybe_strip_indirect_ref (declarator) || type)
13626 42 : error_at (rettype_loc,
13627 : "return type specified for %<operator %T%>", optype);
13628 987119 : else if (type_quals != TYPE_UNQUALIFIED)
13629 3 : error_at (smallest_type_quals_location (type_quals, locations),
13630 : "qualifiers are not allowed on declaration of "
13631 : "%<operator %T%>", optype);
13632 :
13633 : type = optype;
13634 : break;
13635 :
13636 1166037 : case sfk_deduction_guide:
13637 1166037 : if (maybe_strip_indirect_ref (declarator) || type)
13638 0 : error_at (rettype_loc,
13639 : "return type specified for deduction guide");
13640 1166037 : else if (type_quals != TYPE_UNQUALIFIED)
13641 0 : error_at (smallest_type_quals_location (type_quals, locations),
13642 : "qualifiers are not allowed on declaration of "
13643 : "deduction guide");
13644 1166037 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13645 : {
13646 3 : error ("template template parameter %qT in declaration of "
13647 : "deduction guide", optype);
13648 3 : type = error_mark_node;
13649 : }
13650 : else
13651 1166034 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13652 31482999 : for (int i = 0; i < ds_last; ++i)
13653 30316962 : if (i != ds_explicit && locations[i])
13654 3 : error_at (locations[i],
13655 : "%<decl-specifier%> in declaration of deduction guide");
13656 : break;
13657 :
13658 0 : default:
13659 0 : gcc_unreachable ();
13660 : }
13661 :
13662 28823534 : return type;
13663 : }
13664 :
13665 : /* A variable or data member (whose unqualified name is IDENTIFIER)
13666 : has been declared with the indicated TYPE. If the TYPE is not
13667 : acceptable, issue an error message and return a type to use for
13668 : error-recovery purposes. */
13669 :
13670 : tree
13671 329890088 : check_var_type (tree identifier, tree type, location_t loc)
13672 : {
13673 329890088 : if (VOID_TYPE_P (type))
13674 : {
13675 86 : if (!identifier)
13676 0 : error_at (loc, "unnamed variable or field declared void");
13677 86 : else if (identifier_p (identifier))
13678 : {
13679 86 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13680 86 : error_at (loc, "variable or field %qE declared void",
13681 : identifier);
13682 : }
13683 : else
13684 0 : error_at (loc, "variable or field declared void");
13685 86 : type = error_mark_node;
13686 : }
13687 :
13688 329890088 : return type;
13689 : }
13690 :
13691 : /* Handle declaring DECL as an inline variable. */
13692 :
13693 : static void
13694 6372684 : mark_inline_variable (tree decl, location_t loc)
13695 : {
13696 6372684 : bool inlinep = true;
13697 6372684 : if (! toplevel_bindings_p ())
13698 : {
13699 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13700 : "%qD declared at block scope", decl);
13701 24 : inlinep = false;
13702 : }
13703 6372660 : else if (cxx_dialect < cxx17)
13704 648 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13705 : "with %<-std=c++17%> or %<-std=gnu++17%>");
13706 672 : if (inlinep)
13707 : {
13708 6372660 : retrofit_lang_decl (decl);
13709 6372660 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13710 : }
13711 6372684 : }
13712 :
13713 :
13714 : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13715 : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13716 : ORIG is the anonymous TYPE_NAME before that change. */
13717 :
13718 : static bool
13719 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13720 : {
13721 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13722 42 : tree name = DECL_NAME (TYPE_NAME (type));
13723 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13724 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13725 : "anonymous non-C-compatible type given name for linkage "
13726 42 : "purposes by %<typedef%> declaration");
13727 42 : }
13728 :
13729 : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13730 : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13731 : to TYPE but during recursion can be set to nested classes. */
13732 :
13733 : static bool
13734 499038 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13735 : {
13736 499038 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13737 : {
13738 6 : auto_diagnostic_group d;
13739 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13740 9 : inform (type == t ? DECL_SOURCE_LOCATION (orig)
13741 3 : : DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13742 : "type is not C-compatible because it has a base class");
13743 6 : return true;
13744 6 : }
13745 2350524 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13746 1851553 : switch (TREE_CODE (field))
13747 : {
13748 9 : case VAR_DECL:
13749 : /* static data members have been diagnosed already. */
13750 9 : continue;
13751 1232263 : case FIELD_DECL:
13752 1232263 : if (DECL_INITIAL (field))
13753 : {
13754 4 : auto_diagnostic_group d;
13755 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13756 4 : inform (DECL_SOURCE_LOCATION (field),
13757 : "type is not C-compatible because %qD has default "
13758 : "member initializer", field);
13759 4 : return true;
13760 4 : }
13761 1232259 : continue;
13762 9 : case CONST_DECL:
13763 9 : continue;
13764 619195 : case TYPE_DECL:
13765 619195 : if (DECL_SELF_REFERENCE_P (field))
13766 499006 : continue;
13767 120189 : if (DECL_IMPLICIT_TYPEDEF_P (field))
13768 : {
13769 120179 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13770 3 : continue;
13771 120176 : if (CLASS_TYPE_P (TREE_TYPE (field)))
13772 : {
13773 120176 : tree tf = TREE_TYPE (field);
13774 120176 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13775 : tf))
13776 : return true;
13777 120151 : continue;
13778 120151 : }
13779 : }
13780 : /* FALLTHRU */
13781 32 : case FUNCTION_DECL:
13782 32 : case TEMPLATE_DECL:
13783 32 : {
13784 32 : auto_diagnostic_group d;
13785 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13786 32 : inform (DECL_SOURCE_LOCATION (field),
13787 : "type is not C-compatible because it contains %qD "
13788 : "declaration", field);
13789 32 : return true;
13790 32 : }
13791 : default:
13792 : break;
13793 1232277 : }
13794 : return false;
13795 : }
13796 :
13797 : /* Assign a typedef-given name to a class or enumeration type declared
13798 : as anonymous at first. This was split out of grokdeclarator
13799 : because it is also used in libcc1. */
13800 :
13801 : void
13802 380655 : name_unnamed_type (tree type, tree decl)
13803 : {
13804 1141965 : gcc_assert (TYPE_UNNAMED_P (type)
13805 : || enum_with_enumerator_for_linkage_p (type));
13806 :
13807 380655 : tree orig = TYPE_NAME (type);
13808 380655 : if (flag_reflection)
13809 : {
13810 : /* For -freflection for typedef struct { ... } S; ^^S needs to be
13811 : a reflection of a type alias. So, TREE_TYPE (DECL) can't be
13812 : TYPE. Instead of what we do below, override DECL_NAME (orig). */
13813 6564 : DECL_NAME (orig) = DECL_NAME (decl);
13814 6564 : TYPE_DECL_FOR_LINKAGE_PURPOSES_P (orig) = 1;
13815 : }
13816 : else
13817 : /* Replace the anonymous decl with the real decl. Be careful not to
13818 : rename other typedefs (such as the self-reference) of type. */
13819 1120547 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13820 746456 : if (TYPE_NAME (t) == orig)
13821 374091 : TYPE_NAME (t) = decl;
13822 380655 : TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl) = 1;
13823 :
13824 : /* If this is a typedef within a template class, the nested
13825 : type is a (non-primary) template. The name for the
13826 : template needs updating as well. */
13827 380655 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
13828 64 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
13829 :
13830 : /* Adjust linkage now that we aren't unnamed anymore. */
13831 380655 : reset_type_linkage (type);
13832 :
13833 380655 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
13834 378862 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
13835 :
13836 : /* FIXME remangle member functions; member functions of a
13837 : type with external linkage have external linkage. */
13838 :
13839 : /* Check that our job is done, and that it would fail if we
13840 : attempted to do it again. */
13841 761310 : gcc_assert (!TYPE_UNNAMED_P (type)
13842 : && !enum_with_enumerator_for_linkage_p (type));
13843 380655 : }
13844 :
13845 : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
13846 : is allowed. TYPE might contain a decltype(auto). Returns true if
13847 : there was a problem, false otherwise. */
13848 :
13849 : static bool
13850 615223091 : check_decltype_auto (location_t loc, tree type)
13851 : {
13852 615223091 : if (tree a = type_uses_auto (type))
13853 : {
13854 17214939 : if (AUTO_IS_DECLTYPE (a))
13855 : {
13856 585057 : if (a != type)
13857 : {
13858 33 : error_at (loc, "%qT as type rather than plain "
13859 : "%<decltype(auto)%>", type);
13860 33 : return true;
13861 : }
13862 585024 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
13863 : {
13864 37 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
13865 37 : return true;
13866 : }
13867 : }
13868 : }
13869 : return false;
13870 : }
13871 :
13872 : /* Issue an error about two mutually incompatible declspecs
13873 : with the given names and locations
13874 : e.g. "error: `signed' and `unsigned' specified together" */
13875 :
13876 : static void
13877 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
13878 : const char *name_b, location_t loc_b)
13879 : {
13880 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
13881 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
13882 : nullptr, highlight_colors::rhs);
13883 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
13884 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
13885 66 : error_at (&richloc, "%e and %e specified together",
13886 : &e_name_a, &e_name_b);
13887 66 : }
13888 :
13889 : /* Given declspecs and a declarator (abstract or otherwise), determine
13890 : the name and type of the object declared and construct a DECL node
13891 : for it.
13892 :
13893 : DECLSPECS points to the representation of declaration-specifier
13894 : sequence that precedes declarator.
13895 :
13896 : DECL_CONTEXT says which syntactic context this declaration is in:
13897 : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
13898 : FUNCDEF for a function definition. Like NORMAL but a few different
13899 : error messages in each case. Return value may be zero meaning
13900 : this definition is too screwy to try to parse.
13901 : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
13902 : handle member functions (which have FIELD context).
13903 : Return value may be zero meaning this definition is too screwy to
13904 : try to parse.
13905 : PARM for a parameter declaration (either within a function prototype
13906 : or before a function body). Make a PARM_DECL, or return void_type_node.
13907 : TPARM for a template parameter declaration.
13908 : CATCHPARM for a parameter declaration before a catch clause.
13909 : TYPENAME if for a typename (in a cast or sizeof).
13910 : Don't make a DECL node; just return the ..._TYPE node.
13911 : FIELD for a struct or union field; make a FIELD_DECL.
13912 : BITFIELD for a field with specified width.
13913 :
13914 : INITIALIZED is as for start_decl.
13915 :
13916 : ATTRLIST is a pointer to the list of attributes, which may be NULL
13917 : if there are none; *ATTRLIST may be modified if attributes from inside
13918 : the declarator should be applied to the declaration.
13919 :
13920 : When this function is called, scoping variables (such as
13921 : CURRENT_CLASS_TYPE) should reflect the scope in which the
13922 : declaration occurs, not the scope in which the new declaration will
13923 : be placed. For example, on:
13924 :
13925 : void S::f() { ... }
13926 :
13927 : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
13928 : should not be `S'.
13929 :
13930 : Returns a DECL (if a declarator is present), a TYPE (if there is no
13931 : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
13932 : error occurs. */
13933 :
13934 : tree
13935 1008671207 : grokdeclarator (const cp_declarator *declarator,
13936 : cp_decl_specifier_seq *declspecs,
13937 : enum decl_context decl_context,
13938 : int initialized,
13939 : tree* attrlist)
13940 : {
13941 1008671207 : tree type = NULL_TREE;
13942 1008671207 : int longlong = 0;
13943 1008671207 : int explicit_intN = 0;
13944 1008671207 : int int_n_alt = 0;
13945 1008671207 : int virtualp, explicitp, friendp, inlinep, staticp;
13946 1008671207 : int explicit_int = 0;
13947 1008671207 : int explicit_char = 0;
13948 1008671207 : int defaulted_int = 0;
13949 :
13950 1008671207 : tree typedef_decl = NULL_TREE;
13951 1008671207 : const char *name = NULL;
13952 1008671207 : tree typedef_type = NULL_TREE;
13953 : /* True if this declarator is a function definition. */
13954 1008671207 : bool funcdef_flag = false;
13955 1008671207 : cp_declarator_kind innermost_code = cdk_error;
13956 1008671207 : int bitfield = 0;
13957 : #if 0
13958 : /* See the code below that used this. */
13959 : tree decl_attr = NULL_TREE;
13960 : #endif
13961 :
13962 : /* Keep track of what sort of function is being processed
13963 : so that we can warn about default return values, or explicit
13964 : return values which do not match prescribed defaults. */
13965 1008671207 : special_function_kind sfk = sfk_none;
13966 :
13967 1008671207 : tree dname = NULL_TREE;
13968 1008671207 : tree ctor_return_type = NULL_TREE;
13969 1008671207 : enum overload_flags flags = NO_SPECIAL;
13970 : /* cv-qualifiers that apply to the declarator, for a declaration of
13971 : a member function. */
13972 1008671207 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
13973 : /* virt-specifiers that apply to the declarator, for a declaration of
13974 : a member function. */
13975 1008671207 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
13976 : /* ref-qualifier that applies to the declarator, for a declaration of
13977 : a member function. */
13978 1008671207 : cp_ref_qualifier rqual = REF_QUAL_NONE;
13979 : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
13980 1008671207 : int type_quals = get_type_quals (declspecs);
13981 1008671207 : tree raises = NULL_TREE;
13982 1008671207 : int template_count = 0;
13983 1008671207 : tree returned_attrs = NULL_TREE;
13984 1008671207 : tree contract_specifiers = NULL_TREE;
13985 1008671207 : tree parms = NULL_TREE;
13986 1008671207 : const cp_declarator *id_declarator;
13987 : /* The unqualified name of the declarator; either an
13988 : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
13989 1008671207 : tree unqualified_id;
13990 : /* The class type, if any, in which this entity is located,
13991 : or NULL_TREE if none. Note that this value may be different from
13992 : the current class type; for example if an attempt is made to declare
13993 : "A::f" inside "B", this value will be "A". */
13994 1008671207 : tree ctype = current_class_type;
13995 : /* The NAMESPACE_DECL for the namespace in which this entity is
13996 : located. If an unqualified name is used to declare the entity,
13997 : this value will be NULL_TREE, even if the entity is located at
13998 : namespace scope. */
13999 1008671207 : tree in_namespace = NULL_TREE;
14000 1008671207 : cp_storage_class storage_class;
14001 1008671207 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
14002 1008671207 : bool type_was_error_mark_node = false;
14003 1008671207 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
14004 1008671207 : bool template_type_arg = false;
14005 1008671207 : bool template_parm_flag = false;
14006 1008671207 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
14007 1008671207 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
14008 1008671207 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
14009 1008671207 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
14010 1008671207 : bool late_return_type_p = false;
14011 1008671207 : bool array_parameter_p = false;
14012 1008671207 : tree reqs = NULL_TREE;
14013 :
14014 1008671207 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
14015 1008671207 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
14016 1008671207 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
14017 1008671207 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
14018 1008671207 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
14019 1008671207 : explicit_intN = declspecs->explicit_intN_p;
14020 1008671207 : int_n_alt = declspecs->int_n_alt;
14021 1008671207 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
14022 :
14023 : // Was concept_p specified? Note that ds_concept
14024 : // implies ds_constexpr!
14025 1008671207 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
14026 1008671207 : if (concept_p)
14027 117 : constexpr_p = true;
14028 :
14029 1008671207 : if (decl_context == FUNCDEF)
14030 : funcdef_flag = true, decl_context = NORMAL;
14031 : else if (decl_context == MEMFUNCDEF)
14032 : funcdef_flag = true, decl_context = FIELD;
14033 : else if (decl_context == BITFIELD)
14034 : bitfield = 1, decl_context = FIELD;
14035 : else if (decl_context == TEMPLATE_TYPE_ARG)
14036 : template_type_arg = true, decl_context = TYPENAME;
14037 : else if (decl_context == TPARM)
14038 10644398 : template_parm_flag = true, decl_context = PARM;
14039 :
14040 1008671207 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
14041 10645833 : funcdef_flag = true;
14042 :
14043 1008671207 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
14044 : (type_quals,
14045 1008671207 : declspecs->locations));
14046 1008671207 : location_t id_loc;
14047 1008671207 : location_t init_loc;
14048 1008671207 : if (declarator)
14049 : {
14050 609453612 : id_loc = loc_or_input_loc (declarator->id_loc);
14051 609453612 : init_loc = loc_or_input_loc (declarator->init_loc);
14052 : }
14053 : else
14054 399217595 : init_loc = id_loc = input_location;
14055 :
14056 : /* Look inside a declarator for the name being declared
14057 : and get it as a string, for an error message. */
14058 1008671207 : for (id_declarator = declarator;
14059 1385986183 : id_declarator;
14060 377314976 : id_declarator = id_declarator->declarator)
14061 : {
14062 946520326 : if (id_declarator->kind != cdk_id)
14063 377315001 : innermost_code = id_declarator->kind;
14064 :
14065 946520326 : switch (id_declarator->kind)
14066 : {
14067 185472687 : case cdk_function:
14068 185472687 : if (id_declarator->declarator
14069 185110682 : && id_declarator->declarator->kind == cdk_id)
14070 : {
14071 183319519 : sfk = id_declarator->declarator->u.id.sfk;
14072 183319519 : if (sfk == sfk_destructor)
14073 3166361 : flags = DTOR_FLAG;
14074 : }
14075 : break;
14076 :
14077 569205325 : case cdk_id:
14078 569205325 : {
14079 569205325 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
14080 569205325 : tree decl = id_declarator->u.id.unqualified_name;
14081 569205325 : if (!decl)
14082 : break;
14083 569205325 : if (qualifying_scope)
14084 : {
14085 9445098 : if (check_for_bare_parameter_packs (qualifying_scope,
14086 9445098 : id_declarator->id_loc))
14087 6 : return error_mark_node;
14088 9445092 : if (at_function_scope_p ())
14089 : {
14090 : /* [dcl.meaning]
14091 :
14092 : A declarator-id shall not be qualified except
14093 : for ...
14094 :
14095 : None of the cases are permitted in block
14096 : scope. */
14097 0 : if (qualifying_scope == global_namespace)
14098 0 : error ("invalid use of qualified-name %<::%D%>",
14099 : decl);
14100 0 : else if (TYPE_P (qualifying_scope))
14101 0 : error ("invalid use of qualified-name %<%T::%D%>",
14102 : qualifying_scope, decl);
14103 : else
14104 0 : error ("invalid use of qualified-name %<%D::%D%>",
14105 : qualifying_scope, decl);
14106 0 : return error_mark_node;
14107 : }
14108 9445092 : else if (TYPE_P (qualifying_scope))
14109 : {
14110 9212128 : ctype = qualifying_scope;
14111 9212128 : if (!MAYBE_CLASS_TYPE_P (ctype))
14112 : {
14113 6 : error_at (id_declarator->id_loc,
14114 : "%q#T is not a class or namespace", ctype);
14115 6 : ctype = NULL_TREE;
14116 : }
14117 9212122 : else if (innermost_code != cdk_function
14118 477784 : && current_class_type
14119 9212137 : && !uniquely_derived_from_p (ctype,
14120 : current_class_type))
14121 : {
14122 9 : error_at (id_declarator->id_loc,
14123 : "invalid use of qualified-name %<%T::%D%>",
14124 : qualifying_scope, decl);
14125 9 : return error_mark_node;
14126 : }
14127 : }
14128 232964 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
14129 569205310 : in_namespace = qualifying_scope;
14130 : }
14131 569205310 : switch (TREE_CODE (decl))
14132 : {
14133 3166376 : case BIT_NOT_EXPR:
14134 3166376 : {
14135 3166376 : if (innermost_code != cdk_function)
14136 : {
14137 6 : error_at (EXPR_LOCATION (decl),
14138 : "declaration of %qE as non-function", decl);
14139 6 : return error_mark_node;
14140 : }
14141 3166370 : else if (!qualifying_scope
14142 3166370 : && !(current_class_type && at_class_scope_p ()))
14143 : {
14144 9 : error_at (EXPR_LOCATION (decl),
14145 : "declaration of %qE as non-member", decl);
14146 9 : return error_mark_node;
14147 : }
14148 :
14149 3166361 : tree type = TREE_OPERAND (decl, 0);
14150 3166361 : if (TYPE_P (type))
14151 3166358 : type = constructor_name (type);
14152 3166361 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
14153 3166361 : dname = decl;
14154 : }
14155 3166361 : break;
14156 :
14157 2649545 : case TEMPLATE_ID_EXPR:
14158 2649545 : {
14159 2649545 : tree fns = TREE_OPERAND (decl, 0);
14160 :
14161 2649545 : dname = fns;
14162 3555408 : if (!identifier_p (dname))
14163 2649521 : dname = OVL_NAME (dname);
14164 : }
14165 : /* Fall through. */
14166 :
14167 566038934 : case IDENTIFIER_NODE:
14168 566038934 : if (identifier_p (decl))
14169 563389389 : dname = decl;
14170 :
14171 566038934 : if (IDENTIFIER_KEYWORD_P (dname))
14172 : {
14173 0 : error ("declarator-id missing; using reserved word %qD",
14174 : dname);
14175 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14176 : }
14177 566038934 : else if (!IDENTIFIER_CONV_OP_P (dname))
14178 565051770 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14179 : else
14180 : {
14181 987164 : gcc_assert (flags == NO_SPECIAL);
14182 987164 : flags = TYPENAME_FLAG;
14183 987164 : sfk = sfk_conversion;
14184 987164 : tree glob = get_global_binding (dname);
14185 987164 : if (glob && TREE_CODE (glob) == TYPE_DECL)
14186 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14187 : else
14188 : name = "<invalid operator>";
14189 : }
14190 : break;
14191 :
14192 0 : default:
14193 0 : gcc_unreachable ();
14194 : }
14195 : break;
14196 : }
14197 :
14198 : case cdk_array:
14199 : case cdk_pointer:
14200 : case cdk_reference:
14201 : case cdk_ptrmem:
14202 : break;
14203 :
14204 : case cdk_decomp:
14205 946520271 : name = "structured binding";
14206 : break;
14207 :
14208 25 : case cdk_error:
14209 25 : return error_mark_node;
14210 :
14211 0 : default:
14212 0 : gcc_unreachable ();
14213 : }
14214 946520271 : if (id_declarator->kind == cdk_id)
14215 : break;
14216 : }
14217 :
14218 : /* [dcl.fct.edf]
14219 :
14220 : The declarator in a function-definition shall have the form
14221 : D1 ( parameter-declaration-clause) ... */
14222 1008671152 : if (funcdef_flag && innermost_code != cdk_function)
14223 : {
14224 3 : error_at (id_loc, "function definition does not declare parameters");
14225 3 : return error_mark_node;
14226 : }
14227 :
14228 1008671149 : if (flags == TYPENAME_FLAG
14229 1008671149 : && innermost_code != cdk_function
14230 3 : && ! (ctype && !declspecs->any_specifiers_p))
14231 : {
14232 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
14233 3 : return error_mark_node;
14234 : }
14235 :
14236 1008671146 : if (dname && identifier_p (dname))
14237 : {
14238 566038928 : if (UDLIT_OPER_P (dname)
14239 566038928 : && innermost_code != cdk_function)
14240 : {
14241 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
14242 6 : return error_mark_node;
14243 : }
14244 :
14245 566038922 : if (IDENTIFIER_ANY_OP_P (dname))
14246 : {
14247 36217909 : if (typedef_p)
14248 : {
14249 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14250 3 : return error_mark_node;
14251 : }
14252 36217906 : else if (decl_context == PARM || decl_context == CATCHPARM)
14253 : {
14254 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
14255 15 : return error_mark_node;
14256 : }
14257 : }
14258 : }
14259 :
14260 : /* Anything declared one level down from the top level
14261 : must be one of the parameters of a function
14262 : (because the body is at least two levels down). */
14263 :
14264 : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14265 : by not allowing C++ class definitions to specify their parameters
14266 : with xdecls (must be spec.d in the parmlist).
14267 :
14268 : Since we now wait to push a class scope until we are sure that
14269 : we are in a legitimate method context, we must set oldcname
14270 : explicitly (since current_class_name is not yet alive).
14271 :
14272 : We also want to avoid calling this a PARM if it is in a namespace. */
14273 :
14274 1008671122 : if (decl_context == NORMAL && !toplevel_bindings_p ())
14275 : {
14276 70128635 : cp_binding_level *b = current_binding_level;
14277 70128635 : current_binding_level = b->level_chain;
14278 70128635 : if (current_binding_level != 0 && toplevel_bindings_p ())
14279 : decl_context = PARM;
14280 70128635 : current_binding_level = b;
14281 : }
14282 :
14283 1008671122 : if (name == NULL)
14284 439247968 : name = decl_context == PARM ? "parameter" : "type name";
14285 :
14286 1008671122 : if (consteval_p && constexpr_p)
14287 : {
14288 6 : error_at (declspecs->locations[ds_consteval],
14289 : "both %qs and %qs specified", "constexpr", "consteval");
14290 6 : return error_mark_node;
14291 : }
14292 :
14293 1008671116 : if (concept_p && typedef_p)
14294 : {
14295 9 : error_at (declspecs->locations[ds_concept],
14296 : "%qs cannot appear in a typedef declaration", "concept");
14297 9 : return error_mark_node;
14298 : }
14299 :
14300 1008671107 : if (constexpr_p && typedef_p)
14301 : {
14302 3 : error_at (declspecs->locations[ds_constexpr],
14303 : "%qs cannot appear in a typedef declaration", "constexpr");
14304 3 : return error_mark_node;
14305 : }
14306 :
14307 1008671104 : if (consteval_p && typedef_p)
14308 : {
14309 3 : error_at (declspecs->locations[ds_consteval],
14310 : "%qs cannot appear in a typedef declaration", "consteval");
14311 3 : return error_mark_node;
14312 : }
14313 :
14314 1008671101 : if (constinit_p && typedef_p)
14315 : {
14316 3 : error_at (declspecs->locations[ds_constinit],
14317 : "%qs cannot appear in a typedef declaration", "constinit");
14318 3 : return error_mark_node;
14319 : }
14320 :
14321 : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14322 : keywords shall appear in a decl-specifier-seq." */
14323 1008671098 : if (constinit_p && constexpr_p)
14324 : {
14325 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14326 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14327 6 : error_at (&richloc,
14328 : "can use at most one of the %<constinit%> and %<constexpr%> "
14329 : "specifiers");
14330 6 : return error_mark_node;
14331 6 : }
14332 :
14333 : /* If there were multiple types specified in the decl-specifier-seq,
14334 : issue an error message. */
14335 1008671092 : if (declspecs->multiple_types_p)
14336 : {
14337 1501 : error_at (typespec_loc,
14338 : "two or more data types in declaration of %qs", name);
14339 1501 : return error_mark_node;
14340 : }
14341 :
14342 1008669591 : if (declspecs->conflicting_specifiers_p)
14343 31 : return error_mark_node;
14344 :
14345 : /* Extract the basic type from the decl-specifier-seq. */
14346 1008669560 : type = declspecs->type;
14347 1008669560 : if (type == error_mark_node)
14348 : {
14349 1188 : type = NULL_TREE;
14350 1188 : type_was_error_mark_node = true;
14351 : }
14352 :
14353 : /* Ignore erroneous attributes. */
14354 1008669560 : if (attrlist && *attrlist == error_mark_node)
14355 12 : *attrlist = NULL_TREE;
14356 :
14357 : /* An object declared as __attribute__((unavailable)) suppresses
14358 : any reports of being declared with unavailable or deprecated
14359 : items. An object declared as __attribute__((deprecated))
14360 : suppresses warnings of uses of other deprecated items. */
14361 1008669560 : auto ds = make_temp_override (deprecated_state);
14362 1008669560 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
14363 171 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14364 1008669389 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
14365 772641 : deprecated_state = DEPRECATED_SUPPRESS;
14366 :
14367 1008669560 : cp_handle_deprecated_or_unavailable (type);
14368 1008669560 : if (type && TREE_CODE (type) == TYPE_DECL)
14369 : {
14370 702130181 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14371 702130181 : typedef_decl = type;
14372 702130181 : type = TREE_TYPE (typedef_decl);
14373 702130181 : if (DECL_ARTIFICIAL (typedef_decl))
14374 510573800 : cp_handle_deprecated_or_unavailable (type);
14375 : }
14376 : /* No type at all: default to `int', and set DEFAULTED_INT
14377 : because it was not a user-defined typedef. */
14378 1008669560 : if (type == NULL_TREE)
14379 : {
14380 37656172 : if (signed_p || unsigned_p || long_p || short_p)
14381 : {
14382 : /* These imply 'int'. */
14383 8831050 : type = integer_type_node;
14384 8831050 : defaulted_int = 1;
14385 : }
14386 : /* If we just have "complex", it is equivalent to "complex double". */
14387 28825122 : else if (!longlong && !explicit_intN
14388 28825122 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14389 : {
14390 81 : type = double_type_node;
14391 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14392 : "ISO C++ does not support plain %<complex%> meaning "
14393 : "%<double complex%>");
14394 : }
14395 : }
14396 : /* Gather flags. */
14397 1008669560 : explicit_int = declspecs->explicit_int_p;
14398 1008669560 : explicit_char = declspecs->explicit_char_p;
14399 :
14400 : #if 0
14401 : /* See the code below that used this. */
14402 : if (typedef_decl)
14403 : decl_attr = DECL_ATTRIBUTES (typedef_decl);
14404 : #endif
14405 1008669560 : typedef_type = type;
14406 :
14407 1008669560 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14408 2153198 : ctor_return_type = TREE_TYPE (dname);
14409 : else
14410 : ctor_return_type = ctype;
14411 :
14412 1008669560 : if (sfk != sfk_none)
14413 : {
14414 28823534 : type = check_special_function_return_type (sfk, type,
14415 : ctor_return_type,
14416 : type_quals,
14417 : &declarator,
14418 : declspecs->locations);
14419 28823534 : type_quals = TYPE_UNQUALIFIED;
14420 : }
14421 979846026 : else if (type == NULL_TREE)
14422 : {
14423 1588 : int is_main;
14424 :
14425 1588 : explicit_int = -1;
14426 :
14427 : /* We handle `main' specially here, because 'main () { }' is so
14428 : common. With no options, it is allowed. With -Wreturn-type,
14429 : it is a warning. It is only an error with -pedantic-errors. */
14430 3176 : is_main = (funcdef_flag
14431 3410 : && dname && identifier_p (dname)
14432 234 : && MAIN_NAME_P (dname)
14433 68 : && ctype == NULL_TREE
14434 68 : && in_namespace == NULL_TREE
14435 1656 : && current_namespace == global_namespace);
14436 :
14437 1588 : if (type_was_error_mark_node)
14438 : /* We've already issued an error, don't complain more. */;
14439 400 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
14440 : /* Allow it, sigh. */;
14441 338 : else if (! is_main)
14442 276 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14443 : name);
14444 62 : else if (pedantic)
14445 4 : pedwarn (id_loc, OPT_Wpedantic,
14446 : "ISO C++ forbids declaration of %qs with no type", name);
14447 : else
14448 58 : warning_at (id_loc, OPT_Wreturn_type,
14449 : "ISO C++ forbids declaration of %qs with no type", name);
14450 :
14451 1588 : if (type_was_error_mark_node && template_parm_flag)
14452 : /* FIXME we should be able to propagate the error_mark_node as is
14453 : for other contexts too. */
14454 149 : type = error_mark_node;
14455 : else
14456 1439 : type = integer_type_node;
14457 : }
14458 :
14459 1008669560 : ctype = NULL_TREE;
14460 :
14461 1008669560 : if (explicit_intN)
14462 : {
14463 1060314 : if (! int_n_enabled_p[declspecs->int_n_idx])
14464 : {
14465 0 : error_at (declspecs->locations[ds_type_spec],
14466 : "%<__int%d%> is not supported by this target",
14467 0 : int_n_data[declspecs->int_n_idx].bitsize);
14468 0 : explicit_intN = false;
14469 : }
14470 : /* Don't pedwarn if the alternate "__intN__" form has been used instead
14471 : of "__intN". */
14472 1060314 : else if (!int_n_alt && pedantic)
14473 6595 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14474 : "ISO C++ does not support %<__int%d%> for %qs",
14475 6595 : int_n_data[declspecs->int_n_idx].bitsize, name);
14476 : }
14477 :
14478 : /* Now process the modifiers that were specified
14479 : and check for invalid combinations. */
14480 :
14481 : /* Long double is a special combination. */
14482 1008669560 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14483 : {
14484 8620748 : long_p = false;
14485 8620748 : type = cp_build_qualified_type (long_double_type_node,
14486 : cp_type_quals (type));
14487 : }
14488 :
14489 : /* Check all other uses of type modifiers. */
14490 :
14491 1008669560 : if (unsigned_p || signed_p || long_p || short_p)
14492 : {
14493 20465337 : location_t loc;
14494 20465337 : const char *key;
14495 20465337 : if (unsigned_p)
14496 : {
14497 13905320 : key = "unsigned";
14498 13905320 : loc = declspecs->locations[ds_unsigned];
14499 : }
14500 6560017 : else if (signed_p)
14501 : {
14502 1006916 : key = "signed";
14503 1006916 : loc = declspecs->locations[ds_signed];
14504 : }
14505 5553101 : else if (longlong)
14506 : {
14507 2166688 : key = "long long";
14508 2166688 : loc = declspecs->locations[ds_long_long];
14509 : }
14510 3386413 : else if (long_p)
14511 : {
14512 2849218 : key = "long";
14513 2849218 : loc = declspecs->locations[ds_long];
14514 : }
14515 : else /* if (short_p) */
14516 : {
14517 537195 : key = "short";
14518 537195 : loc = declspecs->locations[ds_short];
14519 : }
14520 :
14521 20465337 : int ok = 0;
14522 :
14523 20465337 : if (signed_p && unsigned_p)
14524 54 : complain_about_incompatible_declspecs
14525 54 : ("signed", declspecs->locations[ds_signed],
14526 : "unsigned", declspecs->locations[ds_unsigned]);
14527 20465283 : else if (long_p && short_p)
14528 12 : complain_about_incompatible_declspecs
14529 12 : ("long", declspecs->locations[ds_long],
14530 : "short", declspecs->locations[ds_short]);
14531 20465271 : else if (TREE_CODE (type) != INTEGER_TYPE
14532 20465258 : || type == char8_type_node
14533 20465234 : || type == char16_type_node
14534 20465210 : || type == char32_type_node
14535 20465186 : || ((long_p || short_p)
14536 10719937 : && (explicit_char || explicit_intN)))
14537 418 : error_at (loc, "%qs specified with %qT", key, type);
14538 20464853 : else if (!explicit_int && !defaulted_int
14539 20464853 : && !explicit_char && !explicit_intN)
14540 : {
14541 30 : if (typedef_decl)
14542 : {
14543 27 : pedwarn (loc, OPT_Wpedantic,
14544 : "%qs specified with typedef-name %qD",
14545 : key, typedef_decl);
14546 27 : ok = !flag_pedantic_errors;
14547 : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14548 : and is not built as a typedef. */
14549 27 : if (is_typedef_decl (typedef_decl))
14550 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
14551 : }
14552 3 : else if (declspecs->decltype_p)
14553 0 : error_at (loc, "%qs specified with %<decltype%>", key);
14554 : else
14555 3 : error_at (loc, "%qs specified with %<typeof%>", key);
14556 : }
14557 : else
14558 : ok = 1;
14559 :
14560 : /* Discard the type modifiers if they are invalid. */
14561 514 : if (! ok)
14562 : {
14563 : unsigned_p = false;
14564 : signed_p = false;
14565 : long_p = false;
14566 : short_p = false;
14567 : longlong = 0;
14568 : }
14569 : }
14570 :
14571 : /* Decide whether an integer type is signed or not.
14572 : Optionally treat bitfields as signed by default. */
14573 1008669070 : if (unsigned_p
14574 : /* [class.bit]
14575 :
14576 : It is implementation-defined whether a plain (neither
14577 : explicitly signed or unsigned) char, short, int, or long
14578 : bit-field is signed or unsigned.
14579 :
14580 : Naturally, we extend this to long long as well. Note that
14581 : this does not include wchar_t. */
14582 1008669560 : || (bitfield && !flag_signed_bitfields
14583 18 : && !signed_p
14584 : /* A typedef for plain `int' without `signed' can be
14585 : controlled just like plain `int', but a typedef for
14586 : `signed int' cannot be so controlled. */
14587 18 : && !(typedef_decl
14588 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14589 13 : && TREE_CODE (type) == INTEGER_TYPE
14590 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14591 : {
14592 13905105 : if (explicit_intN)
14593 501538 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14594 13403567 : else if (longlong)
14595 1371688 : type = long_long_unsigned_type_node;
14596 12031879 : else if (long_p)
14597 2230158 : type = long_unsigned_type_node;
14598 9801721 : else if (short_p)
14599 1363690 : type = short_unsigned_type_node;
14600 8438031 : else if (type == char_type_node)
14601 1843370 : type = unsigned_char_type_node;
14602 6594661 : else if (typedef_decl)
14603 25 : type = c_common_unsigned_type (type);
14604 : else
14605 6594636 : type = unsigned_type_node;
14606 : }
14607 994764455 : else if (signed_p && type == char_type_node)
14608 665559 : type = signed_char_type_node;
14609 994098896 : else if (explicit_intN)
14610 558776 : type = int_n_trees[declspecs->int_n_idx].signed_type;
14611 993540120 : else if (longlong)
14612 2227725 : type = long_long_integer_type_node;
14613 991312395 : else if (long_p)
14614 2914052 : type = long_integer_type_node;
14615 988398343 : else if (short_p)
14616 612291 : type = short_integer_type_node;
14617 987786052 : else if (signed_p && typedef_decl)
14618 9 : type = c_common_signed_type (type);
14619 :
14620 1008669560 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14621 : {
14622 1167163 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14623 3 : error_at (declspecs->locations[ds_complex],
14624 : "complex invalid for %qs", name);
14625 : /* If a modifier is specified, the resulting complex is the complex
14626 : form of TYPE. E.g, "complex short" is "complex short int". */
14627 1167160 : else if (type == integer_type_node)
14628 171 : type = complex_integer_type_node;
14629 1166989 : else if (type == float_type_node)
14630 333629 : type = complex_float_type_node;
14631 833360 : else if (type == double_type_node)
14632 334166 : type = complex_double_type_node;
14633 499194 : else if (type == long_double_type_node)
14634 333670 : type = complex_long_double_type_node;
14635 : else
14636 165524 : type = build_complex_type (type);
14637 : }
14638 :
14639 : /* If we're using the injected-class-name to form a compound type or a
14640 : declaration, replace it with the underlying class so we don't get
14641 : redundant typedefs in the debug output. But if we are returning the
14642 : type unchanged, leave it alone so that it's available to
14643 : maybe_get_template_decl_from_type_decl. */
14644 198589348 : if (CLASS_TYPE_P (type)
14645 198527944 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14646 33726951 : && type == TREE_TYPE (TYPE_NAME (type))
14647 1042396511 : && (declarator || type_quals))
14648 32275130 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14649 :
14650 1008669560 : type_quals |= cp_type_quals (type);
14651 2017339120 : type = cp_build_qualified_type
14652 2017339120 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14653 817113179 : || declspecs->decltype_p)
14654 : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14655 : /* We might have ignored or rejected some of the qualifiers. */
14656 1008669560 : type_quals = cp_type_quals (type);
14657 :
14658 993656758 : if (cxx_dialect >= cxx17 && type && is_auto (type)
14659 22029352 : && innermost_code != cdk_function
14660 : /* Placeholder in parm gets a better error below. */
14661 13557630 : && !(decl_context == PARM || decl_context == CATCHPARM)
14662 1022195874 : && id_declarator && declarator != id_declarator)
14663 1116291 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14664 : {
14665 16 : auto_diagnostic_group g;
14666 16 : gcc_rich_location richloc (typespec_loc);
14667 16 : richloc.add_fixit_insert_after ("<>");
14668 16 : error_at (&richloc, "missing template argument list after %qE; "
14669 : "for deduction, template placeholder must be followed "
14670 : "by a simple declarator-id", tmpl);
14671 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14672 16 : type = error_mark_node;
14673 16 : }
14674 :
14675 1008669560 : staticp = 0;
14676 1008669560 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14677 1008669560 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14678 1008669560 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14679 :
14680 1008669560 : storage_class = declspecs->storage_class;
14681 1008669560 : if (storage_class == sc_static)
14682 22754946 : staticp = 1 + (decl_context == FIELD);
14683 985914614 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14684 : /* Treat class-scope deduction guides as static member functions
14685 : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14686 22438387 : staticp = 2;
14687 :
14688 1008669560 : if (virtualp)
14689 : {
14690 4022330 : if (staticp == 2)
14691 : {
14692 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14693 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
14694 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14695 : "and %<static%>", dname);
14696 21 : storage_class = sc_none;
14697 21 : staticp = 0;
14698 21 : }
14699 4022330 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14700 : {
14701 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14702 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14703 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14704 : "declared both %<virtual%> and %<constexpr%> only in "
14705 : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14706 6 : }
14707 : }
14708 1008669560 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14709 :
14710 : /* Issue errors about use of storage classes for parameters. */
14711 1008669560 : if (decl_context == PARM)
14712 : {
14713 285869522 : if (typedef_p)
14714 : {
14715 24 : error_at (declspecs->locations[ds_typedef],
14716 : "typedef declaration invalid in parameter declaration");
14717 24 : return error_mark_node;
14718 : }
14719 285869498 : else if (template_parm_flag && storage_class != sc_none)
14720 : {
14721 16 : error_at (min_location (declspecs->locations[ds_thread],
14722 : declspecs->locations[ds_storage_class]),
14723 : "storage class specified for template parameter %qs",
14724 : name);
14725 16 : return error_mark_node;
14726 : }
14727 285869482 : else if (storage_class == sc_static
14728 285869482 : || storage_class == sc_extern
14729 285869479 : || thread_p)
14730 : {
14731 9 : error_at (min_location (declspecs->locations[ds_thread],
14732 : declspecs->locations[ds_storage_class]),
14733 : "storage class specified for parameter %qs", name);
14734 9 : return error_mark_node;
14735 : }
14736 :
14737 : /* Function parameters cannot be concept. */
14738 285869473 : if (concept_p)
14739 : {
14740 9 : error_at (declspecs->locations[ds_concept],
14741 : "a parameter cannot be declared %qs", "concept");
14742 9 : concept_p = 0;
14743 9 : constexpr_p = 0;
14744 : }
14745 : /* Function parameters cannot be constexpr. If we saw one, moan
14746 : and pretend it wasn't there. */
14747 285869464 : else if (constexpr_p)
14748 : {
14749 9 : error_at (declspecs->locations[ds_constexpr],
14750 : "a parameter cannot be declared %qs", "constexpr");
14751 9 : constexpr_p = 0;
14752 : }
14753 285869473 : if (constinit_p)
14754 : {
14755 6 : error_at (declspecs->locations[ds_constinit],
14756 : "a parameter cannot be declared %qs", "constinit");
14757 6 : constinit_p = 0;
14758 : }
14759 285869473 : if (consteval_p)
14760 : {
14761 3 : error_at (declspecs->locations[ds_consteval],
14762 : "a parameter cannot be declared %qs", "consteval");
14763 3 : consteval_p = 0;
14764 : }
14765 : }
14766 :
14767 : /* Give error if `virtual' is used outside of class declaration. */
14768 1008669511 : if (virtualp
14769 4022330 : && (current_class_name == NULL_TREE || decl_context != FIELD))
14770 : {
14771 68 : error_at (declspecs->locations[ds_virtual],
14772 : "%<virtual%> outside class declaration");
14773 68 : virtualp = 0;
14774 : }
14775 :
14776 1008669511 : if (innermost_code == cdk_decomp)
14777 : {
14778 208189 : location_t loc = (declarator->kind == cdk_reference
14779 217889 : ? declarator->declarator->id_loc : declarator->id_loc);
14780 217889 : if (inlinep)
14781 29 : error_at (declspecs->locations[ds_inline],
14782 : "structured binding declaration cannot be %qs", "inline");
14783 217889 : if (typedef_p)
14784 29 : error_at (declspecs->locations[ds_typedef],
14785 : "structured binding declaration cannot be %qs", "typedef");
14786 217889 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14787 96 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14788 : "structured binding declaration can be %qs only with "
14789 : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14790 217889 : if (consteval_p)
14791 13 : error_at (declspecs->locations[ds_consteval], "structured "
14792 : "binding declaration cannot be %qs", "consteval");
14793 217889 : if (thread_p && cxx_dialect < cxx20)
14794 75 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14795 : "structured binding declaration can be %qs only in "
14796 : "%<-std=c++20%> or %<-std=gnu++20%>",
14797 75 : declspecs->gnu_thread_keyword_p
14798 : ? "__thread" : "thread_local");
14799 217889 : if (concept_p)
14800 : {
14801 0 : error_at (declspecs->locations[ds_concept],
14802 : "structured binding declaration cannot be %qs", "concept");
14803 0 : constexpr_p = 0;
14804 : }
14805 : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
14806 217889 : if (type_quals & TYPE_QUAL_VOLATILE)
14807 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
14808 : "%<volatile%>-qualified structured binding is deprecated");
14809 217889 : switch (storage_class)
14810 : {
14811 : case sc_none:
14812 : break;
14813 28 : case sc_register:
14814 28 : error_at (loc, "structured binding declaration cannot be %qs",
14815 : "register");
14816 28 : break;
14817 244 : case sc_static:
14818 244 : if (cxx_dialect < cxx20)
14819 185 : pedwarn (loc, OPT_Wc__20_extensions,
14820 : "structured binding declaration can be %qs only in "
14821 : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
14822 : break;
14823 29 : case sc_extern:
14824 29 : error_at (loc, "structured binding declaration cannot be %qs",
14825 : "extern");
14826 29 : break;
14827 28 : case sc_mutable:
14828 28 : error_at (loc, "structured binding declaration cannot be %qs",
14829 : "mutable");
14830 28 : break;
14831 0 : case sc_auto:
14832 0 : error_at (loc, "structured binding declaration cannot be "
14833 : "C++98 %<auto%>");
14834 0 : break;
14835 0 : default:
14836 0 : gcc_unreachable ();
14837 : }
14838 217889 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
14839 217889 : || TYPE_IDENTIFIER (type) != auto_identifier)
14840 : {
14841 3 : if (type != error_mark_node)
14842 : {
14843 3 : auto_diagnostic_group d;
14844 3 : error_at (loc, "structured binding declaration cannot have "
14845 : "type %qT", type);
14846 3 : inform (loc,
14847 : "type must be cv-qualified %<auto%> or reference to "
14848 : "cv-qualified %<auto%>");
14849 3 : }
14850 3 : type = build_qualified_type (make_auto (), type_quals);
14851 3 : declspecs->type = type;
14852 : }
14853 217886 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
14854 51 : pedwarn (loc, OPT_Wpedantic,
14855 : "structured binding declaration cannot have constrained "
14856 : "%<auto%> type %qT", type);
14857 217889 : inlinep = 0;
14858 217889 : typedef_p = 0;
14859 217889 : consteval_p = 0;
14860 217889 : concept_p = 0;
14861 217889 : if (storage_class != sc_static)
14862 : {
14863 217645 : storage_class = sc_none;
14864 217645 : declspecs->storage_class = sc_none;
14865 : }
14866 : }
14867 :
14868 : /* Static anonymous unions are dealt with here. */
14869 1008669511 : if (staticp && decl_context == TYPENAME
14870 0 : && declspecs->type
14871 1008669511 : && ANON_AGGR_TYPE_P (declspecs->type))
14872 : decl_context = FIELD;
14873 :
14874 : /* Warn about storage classes that are invalid for certain
14875 : kinds of declarations (parameters, typenames, etc.). */
14876 1008669511 : if (thread_p
14877 19753 : && ((storage_class
14878 19753 : && storage_class != sc_extern
14879 526 : && storage_class != sc_static)
14880 19742 : || typedef_p))
14881 : {
14882 17 : location_t loc
14883 17 : = min_location (declspecs->locations[ds_thread],
14884 : declspecs->locations[ds_storage_class]);
14885 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
14886 17 : thread_p = false;
14887 : }
14888 1008669511 : if (decl_context != NORMAL
14889 844997982 : && ((storage_class != sc_none
14890 844997982 : && storage_class != sc_mutable)
14891 822559608 : || thread_p))
14892 : {
14893 22438377 : if ((decl_context == PARM || decl_context == CATCHPARM)
14894 36 : && (storage_class == sc_register
14895 36 : || storage_class == sc_auto))
14896 : ;
14897 22438341 : else if (typedef_p)
14898 : ;
14899 22438341 : else if (decl_context == FIELD
14900 : /* C++ allows static class elements. */
14901 22438341 : && storage_class == sc_static)
14902 : /* C++ also allows inlines and signed and unsigned elements,
14903 : but in those cases we don't come in here. */
14904 : ;
14905 : else
14906 : {
14907 15 : location_t loc
14908 15 : = min_location (declspecs->locations[ds_thread],
14909 : declspecs->locations[ds_storage_class]);
14910 15 : if (decl_context == FIELD)
14911 15 : error_at (loc, "storage class specified for %qs", name);
14912 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
14913 0 : error_at (loc, "storage class specified for parameter %qs", name);
14914 : else
14915 0 : error_at (loc, "storage class specified for typename");
14916 15 : if (storage_class == sc_register
14917 15 : || storage_class == sc_auto
14918 15 : || storage_class == sc_extern
14919 3 : || thread_p)
14920 15 : storage_class = sc_none;
14921 : }
14922 : }
14923 986231134 : else if (storage_class == sc_extern && funcdef_flag
14924 986231134 : && ! toplevel_bindings_p ())
14925 0 : error ("nested function %qs declared %<extern%>", name);
14926 986231134 : else if (toplevel_bindings_p ())
14927 : {
14928 458789464 : if (storage_class == sc_auto)
14929 2 : error_at (declspecs->locations[ds_storage_class],
14930 : "top-level declaration of %qs specifies %<auto%>", name);
14931 : }
14932 527441670 : else if (thread_p
14933 527441670 : && storage_class != sc_extern
14934 237 : && storage_class != sc_static)
14935 : {
14936 204 : if (declspecs->gnu_thread_keyword_p)
14937 49 : pedwarn (declspecs->locations[ds_thread],
14938 49 : 0, "function-scope %qs implicitly auto and "
14939 : "declared %<__thread%>", name);
14940 :
14941 : /* When thread_local is applied to a variable of block scope the
14942 : storage-class-specifier static is implied if it does not appear
14943 : explicitly. */
14944 204 : storage_class = declspecs->storage_class = sc_static;
14945 204 : staticp = 1;
14946 : }
14947 :
14948 1008669511 : if (storage_class && friendp)
14949 : {
14950 12 : error_at (min_location (declspecs->locations[ds_thread],
14951 : declspecs->locations[ds_storage_class]),
14952 : "storage class specifiers invalid in friend function "
14953 : "declarations");
14954 12 : storage_class = sc_none;
14955 12 : staticp = 0;
14956 : }
14957 :
14958 1008669511 : if (!id_declarator)
14959 : unqualified_id = NULL_TREE;
14960 : else
14961 : {
14962 569203734 : unqualified_id = id_declarator->u.id.unqualified_name;
14963 569203734 : switch (TREE_CODE (unqualified_id))
14964 : {
14965 3166361 : case BIT_NOT_EXPR:
14966 3166361 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
14967 3166361 : if (TYPE_P (unqualified_id))
14968 3166358 : unqualified_id = constructor_name (unqualified_id);
14969 : break;
14970 :
14971 : case IDENTIFIER_NODE:
14972 : case TEMPLATE_ID_EXPR:
14973 : break;
14974 :
14975 0 : default:
14976 0 : gcc_unreachable ();
14977 : }
14978 : }
14979 :
14980 1008669511 : if (declspecs->std_attributes)
14981 : {
14982 723 : location_t attr_loc = declspecs->locations[ds_std_attribute];
14983 723 : auto_diagnostic_group d;
14984 723 : if (any_nonignored_attribute_p (declspecs->std_attributes)
14985 723 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
14986 720 : inform (attr_loc, "an attribute that appertains to a type-specifier "
14987 : "is ignored");
14988 723 : }
14989 :
14990 : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
14991 : bool is_xobj_member_function = false;
14992 : /* Determine the type of the entity declared by recurring on the
14993 : declarator. */
14994 1385764586 : for (; declarator; declarator = declarator->declarator)
14995 : {
14996 946516724 : const cp_declarator *inner_declarator;
14997 946516724 : tree attrs;
14998 :
14999 946516724 : if (type == error_mark_node)
15000 604 : return error_mark_node;
15001 :
15002 946516397 : attrs = declarator->attributes;
15003 946516397 : if (attrs)
15004 : {
15005 13279 : int attr_flags;
15006 :
15007 13279 : attr_flags = 0;
15008 13279 : if (declarator->kind == cdk_id)
15009 12574 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
15010 13279 : if (declarator->kind == cdk_function)
15011 688 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
15012 13279 : if (declarator->kind == cdk_array)
15013 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
15014 13279 : tree late_attrs = NULL_TREE;
15015 13279 : if (decl_context != PARM && decl_context != TYPENAME)
15016 : /* Assume that any attributes that get applied late to
15017 : templates will DTRT when applied to the declaration
15018 : as a whole. */
15019 13267 : late_attrs = splice_template_attributes (&attrs, type);
15020 13279 : returned_attrs = decl_attributes (&type,
15021 : attr_chainon (returned_attrs,
15022 : attrs),
15023 : attr_flags);
15024 13279 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
15025 : }
15026 :
15027 946516397 : inner_declarator = declarator->declarator;
15028 :
15029 : /* We don't want to warn in parameter context because we don't
15030 : yet know if the parse will succeed, and this might turn out
15031 : to be a constructor call. */
15032 946516397 : if (decl_context != PARM
15033 946516397 : && decl_context != TYPENAME
15034 549781514 : && !typedef_p
15035 502872356 : && declarator->parenthesized != UNKNOWN_LOCATION
15036 : /* If the type is class-like and the inner name used a
15037 : global namespace qualifier, we need the parens.
15038 : Unfortunately all we can tell is whether a qualified name
15039 : was used or not. */
15040 946516578 : && !(inner_declarator
15041 126 : && inner_declarator->kind == cdk_id
15042 108 : && inner_declarator->u.id.qualifying_scope
15043 18 : && (MAYBE_CLASS_TYPE_P (type)
15044 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
15045 : {
15046 169 : auto_diagnostic_group d;
15047 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
15048 : "unnecessary parentheses in declaration of %qs",
15049 : name))
15050 : {
15051 42 : gcc_rich_location iloc (declarator->parenthesized);
15052 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
15053 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
15054 42 : inform (&iloc, "remove parentheses");
15055 42 : }
15056 169 : }
15057 946516397 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
15058 : break;
15059 :
15060 377095352 : switch (declarator->kind)
15061 : {
15062 3461479 : case cdk_array:
15063 6922958 : type = create_array_type_for_decl (dname, type,
15064 3461479 : declarator->u.array.bounds,
15065 3461479 : declarator->id_loc);
15066 3461479 : if (!valid_array_size_p (dname
15067 : ? declarator->id_loc : input_location,
15068 : type, dname))
15069 267 : type = error_mark_node;
15070 :
15071 3461479 : if (declarator->std_attributes)
15072 : /* [dcl.array]/1:
15073 :
15074 : The optional attribute-specifier-seq appertains to the
15075 : array type. */
15076 193 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15077 : break;
15078 :
15079 185472651 : case cdk_function:
15080 185472651 : {
15081 185472651 : tree arg_types;
15082 185472651 : int funcdecl_p;
15083 :
15084 : /* Declaring a function type. */
15085 :
15086 : /* Pick up type qualifiers which should be applied to `this'. */
15087 185472651 : memfn_quals = declarator->u.function.qualifiers;
15088 : /* Pick up virt-specifiers. */
15089 185472651 : virt_specifiers = declarator->u.function.virt_specifiers;
15090 : /* And ref-qualifier, too */
15091 185472651 : rqual = declarator->u.function.ref_qualifier;
15092 : /* And tx-qualifier. */
15093 185472651 : tree tx_qual = declarator->u.function.tx_qualifier;
15094 : /* Pick up the exception specifications. */
15095 185472651 : raises = declarator->u.function.exception_specification;
15096 : /* If the exception-specification is ill-formed, let's pretend
15097 : there wasn't one. */
15098 185472651 : if (raises == error_mark_node)
15099 9 : raises = NULL_TREE;
15100 :
15101 370945302 : auto find_xobj_parm = [](tree parm_list)
15102 : {
15103 : /* There is no need to iterate over the list,
15104 : only the first parm can be a valid xobj parm. */
15105 370729274 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
15106 : return NULL_TREE;
15107 : /* If we make it here, we are looking at an xobj parm.
15108 :
15109 : Non-null 'purpose' usually means the parm has a default
15110 : argument, we don't want to violate this assumption. */
15111 80220 : TREE_PURPOSE (parm_list) = NULL_TREE;
15112 80220 : return TREE_VALUE (parm_list);
15113 : };
15114 :
15115 185472651 : tree xobj_parm
15116 185472651 : = find_xobj_parm (declarator->u.function.parameters);
15117 185472651 : is_xobj_member_function = xobj_parm;
15118 :
15119 185472651 : if (xobj_parm && cxx_dialect < cxx23)
15120 41723 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
15121 : "explicit object member function only available "
15122 : "with %<-std=c++23%> or %<-std=gnu++23%>");
15123 :
15124 185472651 : if (xobj_parm && decl_context == TYPENAME)
15125 : {
15126 : /* We inform in every case, just differently depending on what
15127 : case it is. */
15128 6 : auto_diagnostic_group d;
15129 6 : bool ptr_type = true;
15130 : /* If declarator->kind is cdk_function and we are at the end of
15131 : the declarator chain, we are looking at a function type. */
15132 6 : if (!declarator->declarator)
15133 : {
15134 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15135 : "a function type cannot "
15136 : "have an explicit object parameter");
15137 2 : ptr_type = false;
15138 : }
15139 4 : else if (declarator->declarator->kind == cdk_pointer)
15140 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15141 : "a pointer to function type cannot "
15142 : "have an explicit object parameter");
15143 2 : else if (declarator->declarator->kind == cdk_ptrmem)
15144 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15145 : "a pointer to member function type "
15146 : "cannot have an explicit object parameter");
15147 : else
15148 0 : gcc_unreachable ();
15149 :
15150 : /* The locations being used here are probably not correct. */
15151 4 : if (ptr_type)
15152 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15153 : "the type of a pointer to explicit object member "
15154 : "function is a regular pointer to function type");
15155 : else
15156 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15157 : "the type of an explicit object "
15158 : "member function is a regular function type");
15159 : /* Ideally we should synthesize the correct syntax
15160 : for the user, perhaps this could be added later. */
15161 6 : }
15162 : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
15163 : the first parm node will never erroneously be detected here. */
15164 185472651 : {
15165 185472651 : auto_diagnostic_group d;
15166 185472651 : bool bad_xobj_parm_encountered = false;
15167 185472651 : for (tree parm = declarator->u.function.parameters;
15168 459833483 : parm && parm != void_list_node;
15169 274360832 : parm = TREE_CHAIN (parm))
15170 : {
15171 274360832 : if (TREE_PURPOSE (parm) != this_identifier)
15172 274360826 : continue;
15173 6 : bad_xobj_parm_encountered = true;
15174 6 : TREE_PURPOSE (parm) = NULL_TREE;
15175 6 : gcc_rich_location bad_xobj_parm
15176 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
15177 6 : error_at (&bad_xobj_parm,
15178 : "only the first parameter of a member function "
15179 : "can be declared as an explicit object parameter");
15180 6 : }
15181 185472651 : if (bad_xobj_parm_encountered && xobj_parm)
15182 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15183 : "valid explicit object parameter declared here");
15184 185472651 : }
15185 :
15186 185472651 : if (reqs)
15187 3 : error_at (location_of (reqs), "requires-clause on return type");
15188 185472651 : reqs = declarator->u.function.requires_clause;
15189 :
15190 : /* Say it's a definition only for the CALL_EXPR
15191 : closest to the identifier. */
15192 185472651 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15193 :
15194 : /* Handle a late-specified return type. */
15195 185472651 : tree late_return_type = declarator->u.function.late_return_type;
15196 185472651 : if (tree auto_node = type_uses_auto (type))
15197 : {
15198 8483168 : if (!late_return_type)
15199 : {
15200 5021352 : if (!funcdecl_p)
15201 : /* auto (*fp)() = f; is OK. */;
15202 5021318 : else if (current_class_type
15203 9275308 : && LAMBDA_TYPE_P (current_class_type))
15204 : /* OK for C++11 lambdas. */;
15205 3085807 : else if (cxx_dialect < cxx14)
15206 : {
15207 7 : auto_diagnostic_group d;
15208 7 : error_at (typespec_loc, "%qs function uses "
15209 : "%<auto%> type specifier without "
15210 : "trailing return type", name);
15211 7 : inform (typespec_loc,
15212 : "deduced return type only available "
15213 : "with %<-std=c++14%> or %<-std=gnu++14%>");
15214 7 : }
15215 3085800 : else if (virtualp)
15216 : {
15217 6 : error_at (typespec_loc, "virtual function "
15218 : "cannot have deduced return type");
15219 6 : virtualp = false;
15220 : }
15221 : }
15222 3461816 : else if (!is_auto (type) && sfk != sfk_conversion)
15223 : {
15224 15 : error_at (typespec_loc, "%qs function with trailing "
15225 : "return type has %qT as its type rather "
15226 : "than plain %<auto%>", name, type);
15227 274 : return error_mark_node;
15228 : }
15229 3461801 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15230 : {
15231 9 : if (funcdecl_p)
15232 3 : error_at (typespec_loc,
15233 : "%qs function with trailing return type "
15234 : "has %<decltype(auto)%> as its type "
15235 : "rather than plain %<auto%>", name);
15236 : else
15237 6 : error_at (typespec_loc,
15238 : "invalid use of %<decltype(auto)%>");
15239 9 : return error_mark_node;
15240 : }
15241 3461792 : else if (is_constrained_auto (type))
15242 : {
15243 6 : if (funcdecl_p)
15244 3 : error_at (typespec_loc,
15245 : "%qs function with trailing return type "
15246 : "has constrained %<auto%> type specifier "
15247 : "rather than plain %<auto%>",
15248 : name);
15249 : else
15250 3 : error_at (typespec_loc,
15251 : "invalid use of constrained %<auto%> type");
15252 6 : return error_mark_node;
15253 : }
15254 8483138 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15255 8483138 : if (!tmpl)
15256 7317097 : if (tree late_auto = type_uses_auto (late_return_type))
15257 35092 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15258 7317097 : if (tmpl)
15259 : {
15260 1166046 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
15261 : {
15262 12 : auto_diagnostic_group g;
15263 12 : error_at (typespec_loc, "deduced class "
15264 : "type %qD in function return type",
15265 12 : DECL_NAME (tmpl));
15266 12 : inform (DECL_SOURCE_LOCATION (tmpl),
15267 : "%qD declared here", tmpl);
15268 12 : return error_mark_node;
15269 12 : }
15270 1166034 : else if (!late_return_type)
15271 : {
15272 3 : auto_diagnostic_group d;
15273 3 : error_at (declarator->id_loc, "deduction guide "
15274 : "for %qT must have trailing return "
15275 3 : "type", TREE_TYPE (tmpl));
15276 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15277 : "%qD declared here", tmpl);
15278 3 : return error_mark_node;
15279 3 : }
15280 1166031 : else if (CLASS_TYPE_P (late_return_type)
15281 1166031 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15282 2332062 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15283 : == tmpl))
15284 : /* OK */;
15285 : else
15286 0 : error ("trailing return type %qT of deduction guide "
15287 : "is not a specialization of %qT",
15288 0 : late_return_type, TREE_TYPE (tmpl));
15289 : }
15290 : }
15291 176989483 : else if (late_return_type
15292 176989483 : && sfk != sfk_conversion)
15293 : {
15294 37 : if (late_return_type == error_mark_node)
15295 : return error_mark_node;
15296 37 : if (cxx_dialect < cxx11)
15297 : /* Not using maybe_warn_cpp0x because this should
15298 : always be an error. */
15299 1 : error_at (typespec_loc,
15300 : "trailing return type only available "
15301 : "with %<-std=c++11%> or %<-std=gnu++11%>");
15302 : else
15303 36 : error_at (typespec_loc, "%qs function with trailing "
15304 : "return type not declared with %<auto%> "
15305 : "type specifier", name);
15306 37 : return error_mark_node;
15307 : }
15308 185472569 : if (late_return_type && sfk == sfk_conversion)
15309 : {
15310 14 : error ("a conversion function cannot have a trailing return type");
15311 14 : return error_mark_node;
15312 : }
15313 185472555 : type = splice_late_return_type (type, late_return_type);
15314 185472555 : if (type == error_mark_node)
15315 : return error_mark_node;
15316 :
15317 185472478 : if (late_return_type)
15318 : {
15319 3461696 : late_return_type_p = true;
15320 3461696 : type_quals = cp_type_quals (type);
15321 : }
15322 :
15323 185472478 : if (type_quals != TYPE_UNQUALIFIED)
15324 : {
15325 : /* It's wrong, for instance, to issue a -Wignored-qualifiers
15326 : warning for
15327 : static_assert(!is_same_v<void(*)(), const void(*)()>);
15328 : because there the qualifier matters. */
15329 11656 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15330 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15331 : "qualifiers ignored on function return type");
15332 : /* [dcl.fct] "A volatile-qualified return type is
15333 : deprecated." */
15334 11656 : if (type_quals & TYPE_QUAL_VOLATILE)
15335 599 : warning_at (typespec_loc, OPT_Wvolatile,
15336 : "%<volatile%>-qualified return type is "
15337 : "deprecated");
15338 :
15339 : /* We now know that the TYPE_QUALS don't apply to the
15340 : decl, but to its return type. */
15341 : type_quals = TYPE_UNQUALIFIED;
15342 : }
15343 :
15344 : /* Error about some types functions can't return. */
15345 :
15346 185472478 : if (TREE_CODE (type) == FUNCTION_TYPE)
15347 : {
15348 30 : error_at (typespec_loc, "%qs declared as function returning "
15349 : "a function", name);
15350 30 : return error_mark_node;
15351 : }
15352 185472448 : if (TREE_CODE (type) == ARRAY_TYPE)
15353 : {
15354 6 : error_at (typespec_loc, "%qs declared as function returning "
15355 : "an array", name);
15356 6 : return error_mark_node;
15357 : }
15358 185472442 : if (constinit_p && funcdecl_p)
15359 : {
15360 15 : error_at (declspecs->locations[ds_constinit],
15361 : "%<constinit%> on function return type is not "
15362 : "allowed");
15363 15 : return error_mark_node;
15364 : }
15365 :
15366 185472427 : if (check_decltype_auto (typespec_loc, type))
15367 50 : return error_mark_node;
15368 :
15369 185472377 : if (ctype == NULL_TREE
15370 185472377 : && decl_context == FIELD
15371 : && funcdecl_p
15372 107049064 : && friendp == 0)
15373 100548700 : ctype = current_class_type;
15374 :
15375 185472377 : if (ctype && (sfk == sfk_constructor
15376 100548700 : || sfk == sfk_destructor))
15377 : {
15378 : /* We are within a class's scope. If our declarator name
15379 : is the same as the class name, and we are defining
15380 : a function, then it is a constructor/destructor, and
15381 : therefore returns a void type. */
15382 :
15383 : /* ISO C++ 12.4/2. A destructor may not be declared
15384 : const or volatile. A destructor may not be static.
15385 : A destructor may not be declared with ref-qualifier.
15386 :
15387 : ISO C++ 12.1. A constructor may not be declared
15388 : const or volatile. A constructor may not be
15389 : virtual. A constructor may not be static.
15390 : A constructor may not be declared with ref-qualifier. */
15391 25734922 : if (staticp == 2)
15392 9 : error_at (declspecs->locations[ds_storage_class],
15393 : (flags == DTOR_FLAG)
15394 : ? G_("destructor cannot be static member "
15395 : "function")
15396 : : G_("constructor cannot be static member "
15397 : "function"));
15398 25734922 : if (memfn_quals)
15399 : {
15400 9 : error ((flags == DTOR_FLAG)
15401 : ? G_("destructors may not be cv-qualified")
15402 : : G_("constructors may not be cv-qualified"));
15403 9 : memfn_quals = TYPE_UNQUALIFIED;
15404 : }
15405 :
15406 25734922 : if (rqual)
15407 : {
15408 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
15409 6 : error ((flags == DTOR_FLAG)
15410 : ? G_("destructors may not be ref-qualified")
15411 : : G_("constructors may not be ref-qualified"));
15412 6 : rqual = REF_QUAL_NONE;
15413 : }
15414 :
15415 25734922 : if (decl_context == FIELD
15416 25734922 : && !member_function_or_else (ctype,
15417 : current_class_type,
15418 : flags))
15419 0 : return error_mark_node;
15420 :
15421 25734922 : if (flags != DTOR_FLAG)
15422 : {
15423 : /* It's a constructor. */
15424 22730460 : if (explicitp == 1)
15425 5346517 : explicitp = 2;
15426 22730460 : if (virtualp)
15427 : {
15428 3 : permerror (declspecs->locations[ds_virtual],
15429 : "constructors cannot be declared %<virtual%>");
15430 3 : virtualp = 0;
15431 : }
15432 22730460 : if (decl_context == FIELD
15433 22730460 : && sfk != sfk_constructor)
15434 0 : return error_mark_node;
15435 : }
15436 25734922 : if (decl_context == FIELD)
15437 25734922 : staticp = 0;
15438 : }
15439 159737455 : else if (friendp)
15440 : {
15441 6195101 : if (virtualp)
15442 : {
15443 : /* Cannot be both friend and virtual. */
15444 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15445 12 : richloc.add_range (declspecs->locations[ds_friend]);
15446 12 : error_at (&richloc, "virtual functions cannot be friends");
15447 12 : friendp = 0;
15448 12 : }
15449 6195101 : if (decl_context == NORMAL)
15450 0 : error_at (declarator->id_loc,
15451 : "friend declaration not in class definition");
15452 6195101 : if (current_function_decl && funcdef_flag)
15453 : {
15454 9 : error_at (declarator->id_loc,
15455 : "cannot define friend function %qs in a local "
15456 : "class definition", name);
15457 9 : friendp = 0;
15458 : }
15459 : /* [class.friend]/6: A function can be defined in a friend
15460 : declaration if the function name is unqualified. */
15461 6195101 : if (funcdef_flag && in_namespace)
15462 : {
15463 6 : if (in_namespace == global_namespace)
15464 3 : error_at (declarator->id_loc,
15465 : "friend function definition %qs cannot have "
15466 : "a name qualified with %<::%>", name);
15467 : else
15468 3 : error_at (declarator->id_loc,
15469 : "friend function definition %qs cannot have "
15470 : "a name qualified with %<%D::%>", name,
15471 : in_namespace);
15472 : }
15473 : }
15474 153542354 : else if (ctype && sfk == sfk_conversion)
15475 : {
15476 986736 : if (explicitp == 1)
15477 : {
15478 461878 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
15479 461878 : explicitp = 2;
15480 : }
15481 : }
15482 152555618 : else if (sfk == sfk_deduction_guide)
15483 : {
15484 1166031 : if (explicitp == 1)
15485 185472377 : explicitp = 2;
15486 : }
15487 :
15488 185472377 : if (xobj_parm)
15489 : {
15490 80220 : if (!ctype
15491 80220 : && decl_context == NORMAL
15492 49 : && (in_namespace
15493 49 : || !declarator->declarator->u.id.qualifying_scope))
15494 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15495 : "a non-member function cannot have "
15496 : "an explicit object parameter");
15497 : else
15498 : {
15499 80216 : if (virtualp)
15500 : {
15501 16 : auto_diagnostic_group d;
15502 16 : error_at (declspecs->locations[ds_virtual],
15503 : "an explicit object member function cannot "
15504 : "be %<virtual%>");
15505 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15506 : "explicit object parameter declared here");
15507 16 : virtualp = false;
15508 16 : }
15509 80216 : if (staticp >= 2)
15510 : {
15511 2 : auto_diagnostic_group d;
15512 2 : error_at (declspecs->locations[ds_storage_class],
15513 : "an explicit object member function cannot "
15514 : "be %<static%>");
15515 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15516 : "explicit object parameter declared here");
15517 2 : }
15518 80216 : if (unqualified_id
15519 185552587 : && identifier_p (unqualified_id)
15520 160426 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15521 8 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15522 : "%qD cannot be an explicit object member "
15523 : "function", unqualified_id);
15524 : }
15525 : }
15526 185472377 : tree pushed_scope = NULL_TREE;
15527 185472377 : if (funcdecl_p
15528 185472377 : && decl_context != FIELD
15529 76575455 : && inner_declarator->u.id.qualifying_scope
15530 194207692 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15531 8733992 : pushed_scope
15532 8733992 : = push_scope (inner_declarator->u.id.qualifying_scope);
15533 :
15534 185472377 : arg_types = grokparms (declarator->u.function.parameters, &parms);
15535 :
15536 185472377 : if (pushed_scope)
15537 8733992 : pop_scope (pushed_scope);
15538 :
15539 185472377 : if (inner_declarator
15540 185110389 : && inner_declarator->kind == cdk_id
15541 183319256 : && inner_declarator->u.id.sfk == sfk_destructor
15542 3166352 : && arg_types != void_list_node)
15543 : {
15544 20 : error_at (declarator->id_loc,
15545 : "destructors may not have parameters");
15546 20 : arg_types = void_list_node;
15547 20 : parms = NULL_TREE;
15548 20 : is_xobj_member_function = false;
15549 : }
15550 :
15551 185472377 : type = cp_build_function_type (type, arg_types);
15552 :
15553 185472377 : tree attrs = declarator->std_attributes;
15554 185472377 : if (tx_qual)
15555 : {
15556 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
15557 : /* transaction_safe applies to the type, but
15558 : transaction_safe_dynamic applies to the function. */
15559 242 : if (is_attribute_p ("transaction_safe", tx_qual))
15560 213 : attrs = attr_chainon (attrs, att);
15561 : else
15562 29 : returned_attrs = attr_chainon (returned_attrs, att);
15563 : }
15564 :
15565 : /* Actually apply the contract attributes to the declaration. */
15566 185472377 : if (flag_contracts)
15567 14082177 : contract_specifiers
15568 14082177 : = attr_chainon (contract_specifiers,
15569 14082177 : declarator->u.function.contract_specifiers);
15570 :
15571 185472377 : if (attrs)
15572 : /* [dcl.fct]/2:
15573 :
15574 : The optional attribute-specifier-seq appertains to
15575 : the function type. */
15576 497 : cplus_decl_attributes (&type, attrs, 0);
15577 :
15578 185472377 : if (raises)
15579 73748726 : type = build_exception_variant (type, raises);
15580 : }
15581 185472377 : break;
15582 :
15583 188161222 : case cdk_pointer:
15584 188161222 : case cdk_reference:
15585 188161222 : case cdk_ptrmem:
15586 : /* Filter out pointers-to-references and references-to-references.
15587 : We can get these if a TYPE_DECL is used. */
15588 :
15589 188161222 : if (TYPE_REF_P (type))
15590 : {
15591 239 : if (declarator->kind != cdk_reference)
15592 : {
15593 3 : error ("cannot declare pointer to %q#T", type);
15594 3 : type = TREE_TYPE (type);
15595 : }
15596 :
15597 : /* In C++0x, we allow reference to reference declarations
15598 : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15599 : and template type arguments [14.3.1/4 temp.arg.type]. The
15600 : check for direct reference to reference declarations, which
15601 : are still forbidden, occurs below. Reasoning behind the change
15602 : can be found in DR106, DR540, and the rvalue reference
15603 : proposals. */
15604 236 : else if (cxx_dialect == cxx98)
15605 : {
15606 0 : error ("cannot declare reference to %q#T", type);
15607 0 : type = TREE_TYPE (type);
15608 : }
15609 : }
15610 188160983 : else if (VOID_TYPE_P (type))
15611 : {
15612 4917086 : if (declarator->kind == cdk_reference)
15613 4 : error ("cannot declare reference to %q#T", type);
15614 4917082 : else if (declarator->kind == cdk_ptrmem)
15615 3 : error ("cannot declare pointer to %q#T member", type);
15616 : }
15617 :
15618 : /* We now know that the TYPE_QUALS don't apply to the decl,
15619 : but to the target of the pointer. */
15620 188161222 : type_quals = TYPE_UNQUALIFIED;
15621 :
15622 : /* This code used to handle METHOD_TYPE, but I don't think it's
15623 : possible to get it here anymore. */
15624 188161222 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15625 188161222 : if (declarator->kind == cdk_ptrmem
15626 1078912 : && TREE_CODE (type) == FUNCTION_TYPE)
15627 : {
15628 873681 : memfn_quals |= type_memfn_quals (type);
15629 1747362 : type = build_memfn_type (type,
15630 873681 : declarator->u.pointer.class_type,
15631 : memfn_quals,
15632 : rqual);
15633 873681 : if (type == error_mark_node)
15634 : return error_mark_node;
15635 :
15636 : rqual = REF_QUAL_NONE;
15637 : memfn_quals = TYPE_UNQUALIFIED;
15638 : }
15639 :
15640 188161219 : if (TREE_CODE (type) == FUNCTION_TYPE
15641 188161219 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15642 987914 : || type_memfn_rqual (type) != REF_QUAL_NONE))
15643 15 : error (declarator->kind == cdk_reference
15644 : ? G_("cannot declare reference to qualified function type %qT")
15645 : : G_("cannot declare pointer to qualified function type %qT"),
15646 : type);
15647 :
15648 : /* When the pointed-to type involves components of variable size,
15649 : care must be taken to ensure that the size evaluation code is
15650 : emitted early enough to dominate all the possible later uses
15651 : and late enough for the variables on which it depends to have
15652 : been assigned.
15653 :
15654 : This is expected to happen automatically when the pointed-to
15655 : type has a name/declaration of it's own, but special attention
15656 : is required if the type is anonymous.
15657 :
15658 : We handle the NORMAL and FIELD contexts here by inserting a
15659 : dummy statement that just evaluates the size at a safe point
15660 : and ensures it is not deferred until e.g. within a deeper
15661 : conditional context (c++/43555).
15662 :
15663 : We expect nothing to be needed here for PARM or TYPENAME.
15664 : Evaluating the size at this point for TYPENAME would
15665 : actually be incorrect, as we might be in the middle of an
15666 : expression with side effects on the pointed-to type size
15667 : "arguments" prior to the pointer declaration point and the
15668 : size evaluation could end up prior to the side effects. */
15669 :
15670 188161219 : if (!TYPE_NAME (type)
15671 5322119 : && (decl_context == NORMAL || decl_context == FIELD)
15672 595833 : && at_function_scope_p ()
15673 188200579 : && variably_modified_type_p (type, NULL_TREE))
15674 : {
15675 119 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15676 : NULL_TREE, type);
15677 119 : add_decl_expr (TYPE_NAME (type));
15678 : }
15679 :
15680 188161219 : if (declarator->kind == cdk_reference)
15681 : {
15682 : /* In C++0x, the type we are creating a reference to might be
15683 : a typedef which is itself a reference type. In that case,
15684 : we follow the reference collapsing rules in
15685 : [7.1.3/8 dcl.typedef] to create the final reference type:
15686 :
15687 : "If a typedef TD names a type that is a reference to a type
15688 : T, an attempt to create the type 'lvalue reference to cv TD'
15689 : creates the type 'lvalue reference to T,' while an attempt
15690 : to create the type "rvalue reference to cv TD' creates the
15691 : type TD."
15692 : */
15693 126079983 : if (VOID_TYPE_P (type))
15694 : /* We already gave an error. */;
15695 126079979 : else if (TYPE_REF_P (type))
15696 : {
15697 236 : if (declarator->u.reference.rvalue_ref)
15698 : /* Leave type alone. */;
15699 : else
15700 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15701 : }
15702 : else
15703 126079743 : type = cp_build_reference_type
15704 126079743 : (type, declarator->u.reference.rvalue_ref);
15705 :
15706 : /* In C++0x, we need this check for direct reference to
15707 : reference declarations, which are forbidden by
15708 : [8.3.2/5 dcl.ref]. Reference to reference declarations
15709 : are only allowed indirectly through typedefs and template
15710 : type arguments. Example:
15711 :
15712 : void foo(int & &); // invalid ref-to-ref decl
15713 :
15714 : typedef int & int_ref;
15715 : void foo(int_ref &); // valid ref-to-ref decl
15716 : */
15717 126079983 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15718 1 : error ("cannot declare reference to %q#T, which is not "
15719 : "a typedef or a template type argument", type);
15720 : }
15721 62081236 : else if (TREE_CODE (type) == METHOD_TYPE)
15722 873678 : type = build_ptrmemfunc_type (build_pointer_type (type));
15723 61207558 : else if (declarator->kind == cdk_ptrmem)
15724 : {
15725 205231 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15726 : != NAMESPACE_DECL);
15727 205231 : if (declarator->u.pointer.class_type == error_mark_node)
15728 : /* We will already have complained. */
15729 3 : type = error_mark_node;
15730 : else
15731 205228 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15732 : type);
15733 : }
15734 : else
15735 61002327 : type = build_pointer_type (type);
15736 :
15737 : /* Process a list of type modifier keywords (such as
15738 : const or volatile) that were given inside the `*' or `&'. */
15739 :
15740 188161219 : if (declarator->u.pointer.qualifiers)
15741 : {
15742 8218315 : type
15743 8218315 : = cp_build_qualified_type (type,
15744 : declarator->u.pointer.qualifiers);
15745 8218315 : type_quals = cp_type_quals (type);
15746 : }
15747 :
15748 : /* Apply C++11 attributes to the pointer, and not to the
15749 : type pointed to. This is unlike what is done for GNU
15750 : attributes above. It is to comply with [dcl.ptr]/1:
15751 :
15752 : [the optional attribute-specifier-seq (7.6.1) appertains
15753 : to the pointer and not to the object pointed to]. */
15754 188161219 : if (declarator->std_attributes)
15755 173 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15756 :
15757 : ctype = NULL_TREE;
15758 : break;
15759 :
15760 : case cdk_error:
15761 : break;
15762 :
15763 0 : default:
15764 0 : gcc_unreachable ();
15765 : }
15766 : }
15767 :
15768 1008668907 : id_loc = declarator ? declarator->id_loc : input_location;
15769 :
15770 1008668907 : if (innermost_code != cdk_function
15771 : /* Don't check this if it can be the artifical decltype(auto)
15772 : we created when building a constraint in a compound-requirement:
15773 : that the type-constraint is plain is going to be checked in
15774 : cp_parser_compound_requirement. */
15775 1008668907 : && decl_context != TYPENAME
15776 1008668907 : && check_decltype_auto (id_loc, type))
15777 20 : return error_mark_node;
15778 :
15779 : /* A `constexpr' specifier used in an object declaration declares
15780 : the object as `const'. */
15781 1008668887 : if (constexpr_p && innermost_code != cdk_function)
15782 : {
15783 : /* DR1688 says that a `constexpr' specifier in combination with
15784 : `volatile' is valid. */
15785 :
15786 18821692 : if (!TYPE_REF_P (type))
15787 : {
15788 18809534 : type_quals |= TYPE_QUAL_CONST;
15789 18809534 : type = cp_build_qualified_type (type, type_quals);
15790 : }
15791 : }
15792 :
15793 569203139 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15794 2649543 : && !FUNC_OR_METHOD_TYPE_P (type)
15795 1010412543 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15796 : {
15797 0 : error ("template-id %qD used as a declarator",
15798 : unqualified_id);
15799 0 : unqualified_id = dname;
15800 : }
15801 :
15802 : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
15803 : qualified with a class-name, turn it into a METHOD_TYPE, unless
15804 : we know that the function is static. We take advantage of this
15805 : opportunity to do other processing that pertains to entities
15806 : explicitly declared to be class members. Note that if DECLARATOR
15807 : is non-NULL, we know it is a cdk_id declarator; otherwise, we
15808 : would not have exited the loop above. */
15809 1008668887 : if (declarator
15810 569421028 : && declarator->kind == cdk_id
15811 569203139 : && declarator->u.id.qualifying_scope
15812 1018113970 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
15813 : {
15814 9212113 : ctype = declarator->u.id.qualifying_scope;
15815 9212113 : ctype = TYPE_MAIN_VARIANT (ctype);
15816 9212113 : template_count = num_template_headers_for_class (ctype);
15817 :
15818 9212113 : if (ctype == current_class_type)
15819 : {
15820 54 : if (friendp)
15821 : {
15822 15 : permerror (declspecs->locations[ds_friend],
15823 : "member functions are implicitly "
15824 : "friends of their class");
15825 15 : friendp = 0;
15826 : }
15827 : else
15828 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
15829 : ctype, name);
15830 : }
15831 9212059 : else if (/* If the qualifying type is already complete, then we
15832 : can skip the following checks. */
15833 9212059 : !COMPLETE_TYPE_P (ctype)
15834 154 : && (/* If the function is being defined, then
15835 : qualifying type must certainly be complete. */
15836 : funcdef_flag
15837 : /* A friend declaration of "T::f" is OK, even if
15838 : "T" is a template parameter. But, if this
15839 : function is not a friend, the qualifying type
15840 : must be a class. */
15841 117 : || (!friendp && !CLASS_TYPE_P (ctype))
15842 : /* For a declaration, the type need not be
15843 : complete, if either it is dependent (since there
15844 : is no meaningful definition of complete in that
15845 : case) or the qualifying class is currently being
15846 : defined. */
15847 129 : || !(dependent_type_p (ctype)
15848 24 : || currently_open_class (ctype)))
15849 : /* Check that the qualifying type is complete. */
15850 9212123 : && !complete_type_or_else (ctype, NULL_TREE))
15851 64 : return error_mark_node;
15852 9211995 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15853 : {
15854 8734241 : if (current_class_type
15855 276 : && (!friendp || funcdef_flag || initialized))
15856 : {
15857 57 : error_at (id_loc, funcdef_flag || initialized
15858 : ? G_("cannot define member function %<%T::%s%> "
15859 : "within %qT")
15860 : : G_("cannot declare member function %<%T::%s%> "
15861 : "within %qT"),
15862 : ctype, name, current_class_type);
15863 48 : return error_mark_node;
15864 : }
15865 : }
15866 477754 : else if (typedef_p && current_class_type)
15867 : {
15868 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
15869 : ctype, name, current_class_type);
15870 0 : return error_mark_node;
15871 : }
15872 : }
15873 :
15874 1008668775 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
15875 56318498 : ctype = current_class_type;
15876 :
15877 : /* Now TYPE has the actual type. */
15878 :
15879 1008668775 : if (returned_attrs)
15880 : {
15881 2935 : if (attrlist)
15882 2935 : *attrlist = attr_chainon (returned_attrs, *attrlist);
15883 : else
15884 : attrlist = &returned_attrs;
15885 : }
15886 :
15887 1008668775 : if (declarator
15888 569420916 : && declarator->kind == cdk_id
15889 569203027 : && declarator->std_attributes
15890 548078 : && attrlist != NULL)
15891 : {
15892 : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
15893 : a declarator-id appertains to the entity that is declared. */
15894 548075 : if (declarator->std_attributes != error_mark_node)
15895 : {
15896 548057 : if (flag_reflection
15897 : && declarator->std_attributes != error_mark_node
15898 11974 : && lookup_attribute ("internal ", "annotation ",
15899 : declarator->std_attributes)
15900 12 : && *attrlist != error_mark_node
15901 548069 : && lookup_attribute ("internal ", "annotation ", *attrlist))
15902 : /* If there are annotations in both lists, ensure
15903 : declarator->std_attributes go after *attrlist. See
15904 : PR124399. */
15905 7 : *attrlist = chainon (copy_list (*attrlist),
15906 7 : declarator->std_attributes);
15907 : else
15908 548050 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
15909 : }
15910 : else
15911 : /* We should have already diagnosed the issue (c++/78344). */
15912 18 : gcc_assert (seen_error ());
15913 : }
15914 :
15915 : /* Handle parameter packs. */
15916 1008668775 : if (parameter_pack_p)
15917 : {
15918 2961191 : if (decl_context == PARM)
15919 : /* Turn the type into a pack expansion.*/
15920 2961191 : type = make_pack_expansion (type);
15921 : else
15922 0 : error ("non-parameter %qs cannot be a parameter pack", name);
15923 : }
15924 :
15925 1008668775 : if ((decl_context == FIELD || decl_context == PARM)
15926 448931628 : && !processing_template_decl
15927 1152379125 : && variably_modified_type_p (type, NULL_TREE))
15928 : {
15929 12 : if (decl_context == FIELD)
15930 6 : error_at (id_loc,
15931 : "data member may not have variably modified type %qT", type);
15932 : else
15933 6 : error_at (id_loc,
15934 : "parameter may not have variably modified type %qT", type);
15935 12 : type = error_mark_node;
15936 : }
15937 :
15938 1008668775 : if (explicitp == 1 || (explicitp && friendp))
15939 : {
15940 : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
15941 : in the declaration of a constructor or conversion function within
15942 : a class definition. */
15943 74 : if (!current_class_type)
15944 59 : error_at (declspecs->locations[ds_explicit],
15945 : "%<explicit%> outside class declaration");
15946 15 : else if (friendp)
15947 12 : error_at (declspecs->locations[ds_explicit],
15948 : "%<explicit%> in friend declaration");
15949 : else
15950 3 : error_at (declspecs->locations[ds_explicit],
15951 : "only declarations of constructors and conversion operators "
15952 : "can be %<explicit%>");
15953 : explicitp = 0;
15954 : }
15955 :
15956 1008668775 : if (storage_class == sc_mutable)
15957 : {
15958 144737 : location_t sloc = declspecs->locations[ds_storage_class];
15959 144737 : if (decl_context != FIELD || friendp)
15960 : {
15961 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
15962 : name);
15963 30 : storage_class = sc_none;
15964 : }
15965 144707 : else if (decl_context == TYPENAME || typedef_p)
15966 : {
15967 0 : error_at (sloc,
15968 : "non-object member %qs cannot be declared %<mutable%>",
15969 : name);
15970 0 : storage_class = sc_none;
15971 : }
15972 144707 : else if (FUNC_OR_METHOD_TYPE_P (type))
15973 : {
15974 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
15975 : name);
15976 3 : storage_class = sc_none;
15977 : }
15978 144704 : else if (staticp)
15979 : {
15980 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
15981 : name);
15982 0 : storage_class = sc_none;
15983 : }
15984 144704 : else if (type_quals & TYPE_QUAL_CONST)
15985 : {
15986 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
15987 : name);
15988 9 : storage_class = sc_none;
15989 : }
15990 144695 : else if (TYPE_REF_P (type))
15991 : {
15992 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
15993 : name);
15994 6 : storage_class = sc_none;
15995 : }
15996 : }
15997 :
15998 : /* If this is declaring a typedef name, return a TYPE_DECL. */
15999 1008668775 : if (typedef_p && decl_context != TYPENAME)
16000 : {
16001 45834837 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
16002 45834837 : tree decl;
16003 :
16004 45834837 : if (funcdef_flag)
16005 : {
16006 6 : if (decl_context == NORMAL)
16007 3 : error_at (id_loc,
16008 : "typedef may not be a function definition");
16009 : else
16010 3 : error_at (id_loc,
16011 : "typedef may not be a member function definition");
16012 6 : return error_mark_node;
16013 : }
16014 :
16015 : /* This declaration:
16016 :
16017 : typedef void f(int) const;
16018 :
16019 : declares a function type which is not a member of any
16020 : particular class, but which is cv-qualified; for
16021 : example "f S::*" declares a pointer to a const-qualified
16022 : member function of S. We record the cv-qualification in the
16023 : function type. */
16024 45834831 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
16025 : {
16026 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
16027 :
16028 : /* We have now dealt with these qualifiers. */
16029 62 : memfn_quals = TYPE_UNQUALIFIED;
16030 62 : rqual = REF_QUAL_NONE;
16031 : }
16032 :
16033 45834831 : if (type_uses_auto (type))
16034 : {
16035 62 : if (alias_p)
16036 20 : error_at (declspecs->locations[ds_type_spec],
16037 : "%<auto%> not allowed in alias declaration");
16038 : else
16039 42 : error_at (declspecs->locations[ds_type_spec],
16040 : "typedef declared %<auto%>");
16041 62 : type = error_mark_node;
16042 : }
16043 :
16044 45834831 : if (reqs)
16045 3 : error_at (location_of (reqs), "requires-clause on typedef");
16046 :
16047 45834831 : if (id_declarator && declarator->u.id.qualifying_scope)
16048 : {
16049 21 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
16050 21 : type = error_mark_node;
16051 : }
16052 :
16053 45834831 : if (decl_context == FIELD)
16054 24485354 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
16055 : else
16056 21349477 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
16057 :
16058 45834831 : if (decl_context != FIELD)
16059 : {
16060 21349477 : if (!current_function_decl)
16061 9314899 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
16062 12034578 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
16063 : /* The TYPE_DECL is "abstract" because there will be
16064 : clones of this constructor/destructor, and there will
16065 : be copies of this TYPE_DECL generated in those
16066 : clones. The decloning optimization (for space) may
16067 : revert this subsequently if it determines that
16068 : the clones should share a common implementation. */
16069 156492 : DECL_ABSTRACT_P (decl) = true;
16070 :
16071 21349477 : set_originating_module (decl);
16072 : }
16073 24485354 : else if (current_class_type
16074 24485354 : && constructor_name_p (unqualified_id, current_class_type))
16075 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
16076 : "as enclosing class",
16077 : unqualified_id);
16078 :
16079 : /* If the user declares "typedef struct {...} foo" then the
16080 : struct will have an anonymous name. Fill that name in now.
16081 : Nothing can refer to it, so nothing needs know about the name
16082 : change. */
16083 45834831 : if (type != error_mark_node
16084 45834748 : && unqualified_id
16085 45834745 : && TYPE_NAME (type)
16086 42657617 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16087 85196510 : && (TYPE_UNNAMED_P (type)
16088 : /* An enum may have previously used an enumerator for linkage
16089 : purposes, but we want the typedef name to take priority. */
16090 42277284 : || enum_with_enumerator_for_linkage_p (type))
16091 380746 : && declspecs->type_definition_p
16092 380676 : && attributes_naming_typedef_ok (*attrlist)
16093 46215504 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
16094 380655 : name_unnamed_type (type, decl);
16095 :
16096 45834831 : if (signed_p
16097 45834831 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
16098 364964 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
16099 :
16100 45834831 : bad_specifiers (decl, BSP_TYPE, virtualp,
16101 : memfn_quals != TYPE_UNQUALIFIED,
16102 : inlinep, friendp, raises != NULL_TREE,
16103 : declspecs->locations);
16104 :
16105 45834831 : if (alias_p)
16106 : /* Acknowledge that this was written:
16107 : `using analias = atype;'. */
16108 20512839 : TYPE_DECL_ALIAS_P (decl) = 1;
16109 :
16110 45834831 : return decl;
16111 : }
16112 :
16113 : /* Detect the case of an array type of unspecified size
16114 : which came, as such, direct from a typedef name.
16115 : We must copy the type, so that the array's domain can be
16116 : individually set by the object's initializer. */
16117 :
16118 962833938 : if (type && typedef_type
16119 934009096 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
16120 963409718 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
16121 81 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16122 :
16123 : /* Detect where we're using a typedef of function type to declare a
16124 : function. PARMS will not be set, so we must create it now. */
16125 :
16126 962833938 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
16127 : {
16128 15091 : tree decls = NULL_TREE;
16129 15091 : tree args;
16130 :
16131 15091 : for (args = TYPE_ARG_TYPES (type);
16132 32649 : args && args != void_list_node;
16133 17558 : args = TREE_CHAIN (args))
16134 : {
16135 17558 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
16136 17558 : TREE_VALUE (args));
16137 :
16138 17558 : DECL_CHAIN (decl) = decls;
16139 17558 : decls = decl;
16140 : }
16141 :
16142 15091 : parms = nreverse (decls);
16143 :
16144 15091 : if (decl_context != TYPENAME)
16145 : {
16146 : /* The qualifiers on the function type become the qualifiers on
16147 : the non-static member function. */
16148 14376 : memfn_quals |= type_memfn_quals (type);
16149 14376 : rqual = type_memfn_rqual (type);
16150 14376 : type_quals = TYPE_UNQUALIFIED;
16151 14376 : raises = TYPE_RAISES_EXCEPTIONS (type);
16152 : }
16153 : }
16154 :
16155 : /* If this is a type name (such as, in a cast or sizeof),
16156 : compute the type and return it now. */
16157 :
16158 962833223 : if (decl_context == TYPENAME)
16159 : {
16160 : /* Note that here we don't care about type_quals. */
16161 :
16162 : /* Special case: "friend class foo" looks like a TYPENAME context. */
16163 395598649 : if (friendp)
16164 : {
16165 0 : if (inlinep)
16166 : {
16167 0 : error ("%<inline%> specified for friend class declaration");
16168 0 : inlinep = 0;
16169 : }
16170 :
16171 0 : if (!current_aggr)
16172 : {
16173 : /* Don't allow friend declaration without a class-key. */
16174 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16175 0 : permerror (input_location, "template parameters cannot be friends");
16176 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
16177 0 : permerror (input_location, "friend declaration requires class-key, "
16178 : "i.e. %<friend class %T::%D%>",
16179 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
16180 : else
16181 0 : permerror (input_location, "friend declaration requires class-key, "
16182 : "i.e. %<friend %#T%>",
16183 : type);
16184 : }
16185 :
16186 : /* Only try to do this stuff if we didn't already give up. */
16187 0 : if (type != integer_type_node)
16188 : {
16189 : /* A friendly class? */
16190 0 : if (current_class_type)
16191 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
16192 : /*complain=*/true);
16193 : else
16194 0 : error ("trying to make class %qT a friend of global scope",
16195 : type);
16196 :
16197 0 : type = void_type_node;
16198 : }
16199 : }
16200 395598649 : else if (memfn_quals || rqual)
16201 : {
16202 10330 : if (ctype == NULL_TREE
16203 10330 : && TREE_CODE (type) == METHOD_TYPE)
16204 0 : ctype = TYPE_METHOD_BASETYPE (type);
16205 :
16206 10330 : if (ctype)
16207 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
16208 : /* Core issue #547: need to allow this in template type args.
16209 : Allow it in general in C++11 for alias-declarations. */
16210 10330 : else if ((template_type_arg || cxx_dialect >= cxx11)
16211 10328 : && TREE_CODE (type) == FUNCTION_TYPE)
16212 10326 : type = apply_memfn_quals (type, memfn_quals, rqual);
16213 : else
16214 4 : error ("invalid qualifiers on non-member function type");
16215 : }
16216 :
16217 395598649 : if (reqs)
16218 3 : error_at (location_of (reqs), "requires-clause on type-id");
16219 :
16220 395598649 : return type;
16221 : }
16222 567235289 : else if (unqualified_id == NULL_TREE && decl_context != PARM
16223 801716 : && decl_context != CATCHPARM
16224 347480 : && TREE_CODE (type) != UNION_TYPE
16225 347176 : && ! bitfield
16226 347176 : && innermost_code != cdk_decomp)
16227 : {
16228 9 : error ("abstract declarator %qT used as declaration", type);
16229 9 : return error_mark_node;
16230 : }
16231 :
16232 567235280 : if (!FUNC_OR_METHOD_TYPE_P (type))
16233 : {
16234 : /* Only functions may be declared using an operator-function-id. */
16235 383953697 : if (dname && IDENTIFIER_ANY_OP_P (dname))
16236 : {
16237 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
16238 8 : return error_mark_node;
16239 : }
16240 :
16241 383953689 : if (reqs)
16242 15 : error_at (location_of (reqs),
16243 : "requires-clause on declaration of non-function type %qT",
16244 : type);
16245 : }
16246 :
16247 : /* We don't check parameter types here because we can emit a better
16248 : error message later. */
16249 567235272 : if (decl_context != PARM)
16250 : {
16251 281365901 : type = check_var_type (unqualified_id, type, id_loc);
16252 281365901 : if (type == error_mark_node)
16253 : return error_mark_node;
16254 : }
16255 :
16256 : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16257 : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16258 :
16259 567235192 : if (decl_context == PARM || decl_context == CATCHPARM)
16260 : {
16261 286336779 : if (ctype || in_namespace)
16262 0 : error ("cannot use %<::%> in parameter declaration");
16263 :
16264 286336779 : tree auto_node = type_uses_auto (type);
16265 286336779 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16266 : {
16267 120 : bool err_p = true;
16268 120 : if (cxx_dialect >= cxx14)
16269 : {
16270 212 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16271 3 : error_at (typespec_loc,
16272 : "cannot declare a parameter with %<decltype(auto)%>");
16273 109 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16274 : {
16275 5 : auto_diagnostic_group g;
16276 5 : gcc_rich_location richloc (typespec_loc);
16277 5 : richloc.add_fixit_insert_after ("<>");
16278 5 : error_at (&richloc,
16279 : "missing template argument list after %qE; template "
16280 : "placeholder not permitted in parameter", c);
16281 5 : if (decl_context == PARM && cxx_dialect >= cxx20)
16282 4 : inform (typespec_loc, "or use %<auto%> for an "
16283 : "abbreviated function template");
16284 5 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16285 5 : }
16286 104 : else if (decl_context == CATCHPARM || template_parm_flag)
16287 14 : error_at (typespec_loc,
16288 : "%<auto%> parameter not permitted in this context");
16289 : else
16290 : /* Do not issue an error while tentatively parsing a function
16291 : parameter: for T t(auto(a), 42);, when we just saw the 1st
16292 : parameter, we don't know yet that this construct won't be
16293 : a function declaration. Defer the checking to
16294 : cp_parser_parameter_declaration_clause. */
16295 : err_p = false;
16296 : }
16297 : else
16298 8 : error_at (typespec_loc, "parameter declared %<auto%>");
16299 30 : if (err_p)
16300 30 : type = error_mark_node;
16301 : }
16302 :
16303 : /* A parameter declared as an array of T is really a pointer to T.
16304 : One declared as a function is really a pointer to a function.
16305 : One declared as a member is really a pointer to member. */
16306 :
16307 286336779 : if (TREE_CODE (type) == ARRAY_TYPE)
16308 : {
16309 : /* Transfer const-ness of array into that of type pointed to. */
16310 737802 : type = build_pointer_type (TREE_TYPE (type));
16311 737802 : type_quals = TYPE_UNQUALIFIED;
16312 737802 : array_parameter_p = true;
16313 : }
16314 285598977 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16315 98908 : type = build_pointer_type (type);
16316 : }
16317 :
16318 141593749 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16319 : /* Don't convert xobj member functions to METHOD_TYPE. */
16320 109282727 : && !is_xobj_member_function
16321 666513903 : && !(unqualified_id
16322 198550823 : && identifier_p (unqualified_id)
16323 99278327 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16324 : {
16325 99272496 : cp_cv_quals real_quals = memfn_quals;
16326 99272496 : if (cxx_dialect < cxx14 && constexpr_p
16327 650371 : && sfk != sfk_constructor && sfk != sfk_destructor)
16328 3820 : real_quals |= TYPE_QUAL_CONST;
16329 99272496 : type = build_memfn_type (type, ctype, real_quals, rqual);
16330 : }
16331 :
16332 567235192 : {
16333 567235192 : tree decl = NULL_TREE;
16334 :
16335 567235192 : if (decl_context == PARM)
16336 : {
16337 285869371 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16338 285869371 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16339 :
16340 285869371 : bad_specifiers (decl, BSP_PARM, virtualp,
16341 : memfn_quals != TYPE_UNQUALIFIED,
16342 : inlinep, friendp, raises != NULL_TREE,
16343 : declspecs->locations);
16344 : }
16345 281365821 : else if (decl_context == FIELD)
16346 : {
16347 138576888 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16348 19318757 : if (tree auto_node = type_uses_auto (type))
16349 : {
16350 24 : location_t tloc = declspecs->locations[ds_type_spec];
16351 24 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16352 6 : error_at (tloc, "invalid use of template-name %qE without an "
16353 : "argument list",
16354 6 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16355 : else
16356 18 : error_at (tloc, "non-static data member declared with "
16357 : "placeholder %qT", auto_node);
16358 24 : type = error_mark_node;
16359 : }
16360 :
16361 : /* The C99 flexible array extension. */
16362 116138546 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16363 139663215 : && TYPE_DOMAIN (type) == NULL_TREE)
16364 : {
16365 1399 : if (ctype
16366 1399 : && (TREE_CODE (ctype) == UNION_TYPE
16367 1399 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16368 84 : pedwarn (id_loc, OPT_Wpedantic,
16369 : "flexible array member in union is a GCC extension");
16370 :
16371 : else
16372 : {
16373 : /* Array is a flexible member. */
16374 1315 : if (name)
16375 1315 : pedwarn (id_loc, OPT_Wpedantic,
16376 : "ISO C++ forbids flexible array member %qs", name);
16377 : else
16378 : pedwarn (input_location, OPT_Wpedantic,
16379 : "ISO C++ forbids flexible array members");
16380 :
16381 : /* Flexible array member has a null domain. */
16382 1315 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16383 : }
16384 : }
16385 :
16386 138576888 : if (type == error_mark_node)
16387 : {
16388 : /* Happens when declaring arrays of sizes which
16389 : are error_mark_node, for example. */
16390 24 : decl = NULL_TREE;
16391 : }
16392 138576864 : else if (in_namespace && !friendp)
16393 : {
16394 : /* Something like struct S { int N::j; }; */
16395 6 : error_at (id_loc, "invalid use of %<::%>");
16396 6 : return error_mark_node;
16397 : }
16398 138576858 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16399 : {
16400 106743578 : int publicp = 0;
16401 106743578 : tree function_context;
16402 :
16403 106743578 : if (friendp == 0)
16404 : {
16405 : /* This should never happen in pure C++ (the check
16406 : could be an assert). It could happen in
16407 : Objective-C++ if someone writes invalid code that
16408 : uses a function declaration for an instance
16409 : variable or property (instance variables and
16410 : properties are parsed as FIELD_DECLs, but they are
16411 : part of an Objective-C class, not a C++ class).
16412 : That code is invalid and is caught by this
16413 : check. */
16414 100548534 : if (!ctype)
16415 : {
16416 0 : error ("declaration of function %qD in invalid context",
16417 : unqualified_id);
16418 0 : return error_mark_node;
16419 : }
16420 :
16421 : /* ``A union may [ ... ] not [ have ] virtual functions.''
16422 : ARM 9.5 */
16423 100548534 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16424 : {
16425 3 : error_at (declspecs->locations[ds_virtual],
16426 : "function %qD declared %<virtual%> inside a union",
16427 : unqualified_id);
16428 3 : return error_mark_node;
16429 : }
16430 :
16431 4022216 : if (virtualp
16432 110765791 : && identifier_p (unqualified_id)
16433 4022216 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16434 : {
16435 9 : error_at (declspecs->locations[ds_virtual],
16436 : "%qD cannot be declared %<virtual%>, since it "
16437 : "is always static", unqualified_id);
16438 9 : virtualp = 0;
16439 : }
16440 : }
16441 :
16442 : /* Check that the name used for a destructor makes sense. */
16443 106743575 : if (sfk == sfk_destructor)
16444 : {
16445 3004498 : tree uqname = id_declarator->u.id.unqualified_name;
16446 :
16447 3004498 : if (!ctype)
16448 : {
16449 12 : gcc_assert (friendp);
16450 12 : error_at (id_loc, "expected qualified name in friend "
16451 : "declaration for destructor %qD", uqname);
16452 12 : return error_mark_node;
16453 : }
16454 :
16455 3004486 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16456 : {
16457 9 : error_at (id_loc, "declaration of %qD as member of %qT",
16458 : uqname, ctype);
16459 9 : return error_mark_node;
16460 : }
16461 3004477 : if (concept_p)
16462 : {
16463 9 : error_at (declspecs->locations[ds_concept],
16464 : "a destructor cannot be %qs", "concept");
16465 9 : return error_mark_node;
16466 : }
16467 3004468 : if (constexpr_p && cxx_dialect < cxx20)
16468 : {
16469 6 : error_at (declspecs->locations[ds_constexpr],
16470 : "%<constexpr%> destructors only available"
16471 : " with %<-std=c++20%> or %<-std=gnu++20%>");
16472 6 : return error_mark_node;
16473 : }
16474 3004462 : if (consteval_p)
16475 : {
16476 3 : error_at (declspecs->locations[ds_consteval],
16477 : "a destructor cannot be %qs", "consteval");
16478 3 : return error_mark_node;
16479 : }
16480 : }
16481 103739077 : else if (sfk == sfk_constructor && friendp && !ctype)
16482 : {
16483 0 : error ("expected qualified name in friend declaration "
16484 : "for constructor %qD",
16485 0 : id_declarator->u.id.unqualified_name);
16486 0 : return error_mark_node;
16487 : }
16488 106743536 : if (sfk == sfk_constructor)
16489 22730484 : if (concept_p)
16490 : {
16491 9 : error_at (declspecs->locations[ds_concept],
16492 : "a constructor cannot be %<concept%>");
16493 9 : return error_mark_node;
16494 : }
16495 84013052 : if (concept_p)
16496 : {
16497 9 : error_at (declspecs->locations[ds_concept],
16498 : "a concept cannot be a member function");
16499 9 : concept_p = false;
16500 : }
16501 106743518 : else if (consteval_p
16502 107466365 : && identifier_p (unqualified_id)
16503 107466356 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16504 : {
16505 9 : error_at (declspecs->locations[ds_consteval],
16506 : "%qD cannot be %qs", unqualified_id, "consteval");
16507 9 : consteval_p = false;
16508 : }
16509 :
16510 106743527 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16511 : {
16512 89199 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
16513 89199 : if (variable_template_p (tmpl))
16514 : {
16515 3 : auto_diagnostic_group d;
16516 3 : error_at (id_loc, "specialization of variable template "
16517 : "%qD declared as function", tmpl);
16518 3 : inform (DECL_SOURCE_LOCATION (tmpl),
16519 : "variable template declared here");
16520 3 : return error_mark_node;
16521 3 : }
16522 : }
16523 :
16524 : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16525 106743524 : function_context
16526 : = (ctype != NULL_TREE
16527 106743524 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16528 213487048 : publicp = ((! friendp || ! staticp)
16529 106743524 : && function_context == NULL_TREE);
16530 :
16531 106743524 : decl = grokfndecl (ctype, type,
16532 106743524 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16533 : ? unqualified_id : dname,
16534 : parms,
16535 : unqualified_id,
16536 : declspecs,
16537 : reqs,
16538 : virtualp, flags, memfn_quals, rqual, raises,
16539 : friendp ? -1 : 0, friendp, publicp,
16540 106743524 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16541 106743524 : | (8 * consteval_p),
16542 : initialized,
16543 : is_xobj_member_function, sfk,
16544 : funcdef_flag, late_return_type_p,
16545 : template_count, in_namespace,
16546 : attrlist, contract_specifiers, id_loc);
16547 106743524 : decl = set_virt_specifiers (decl, virt_specifiers);
16548 106743524 : if (decl == NULL_TREE)
16549 97 : return error_mark_node;
16550 : #if 0
16551 : /* This clobbers the attrs stored in `decl' from `attrlist'. */
16552 : /* The decl and setting of decl_attr is also turned off. */
16553 : decl = build_decl_attribute_variant (decl, decl_attr);
16554 : #endif
16555 :
16556 : /* [class.conv.ctor]
16557 :
16558 : A constructor declared without the function-specifier
16559 : explicit that can be called with a single parameter
16560 : specifies a conversion from the type of its first
16561 : parameter to the type of its class. Such a constructor
16562 : is called a converting constructor. */
16563 106743427 : if (explicitp == 2)
16564 5808395 : DECL_NONCONVERTING_P (decl) = 1;
16565 :
16566 106743427 : if (declspecs->explicit_specifier)
16567 384937 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16568 : }
16569 31833280 : else if (!staticp
16570 31833280 : && ((current_class_type
16571 19318748 : && same_type_p (TYPE_MAIN_VARIANT (type),
16572 : current_class_type))
16573 19318652 : || (!dependent_type_p (type)
16574 11726450 : && !COMPLETE_TYPE_P (complete_type (type))
16575 1447 : && (!complete_or_array_type_p (type)
16576 1372 : || initialized == SD_UNINITIALIZED))))
16577 : {
16578 1510 : if (TREE_CODE (type) != ARRAY_TYPE
16579 1510 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16580 : {
16581 171 : if (unqualified_id)
16582 : {
16583 126 : auto_diagnostic_group d;
16584 126 : error_at (id_loc, "field %qD has incomplete type %qT",
16585 : unqualified_id, type);
16586 126 : cxx_incomplete_type_inform (strip_array_types (type));
16587 126 : }
16588 : else
16589 45 : error ("name %qT has incomplete type", type);
16590 :
16591 171 : type = error_mark_node;
16592 171 : decl = NULL_TREE;
16593 : }
16594 : }
16595 51148996 : else if (!verify_type_context (input_location,
16596 : staticp
16597 : ? TCTX_STATIC_STORAGE
16598 : : TCTX_FIELD, type))
16599 : {
16600 0 : type = error_mark_node;
16601 0 : decl = NULL_TREE;
16602 : }
16603 : else
16604 : {
16605 31831764 : if (friendp)
16606 : {
16607 12 : if (unqualified_id)
16608 9 : error_at (id_loc,
16609 : "%qE is neither function nor member function; "
16610 : "cannot be declared friend", unqualified_id);
16611 : else
16612 3 : error ("unnamed field is neither function nor member "
16613 : "function; cannot be declared friend");
16614 12 : return error_mark_node;
16615 : }
16616 31831752 : decl = NULL_TREE;
16617 : }
16618 :
16619 138576713 : if (friendp)
16620 : {
16621 : /* Packages tend to use GNU attributes on friends, so we only
16622 : warn for standard attributes. */
16623 6195013 : if (attrlist
16624 6195013 : && !funcdef_flag
16625 6195013 : && cxx11_attribute_p (*attrlist))
16626 : {
16627 27 : *attrlist = NULL_TREE;
16628 27 : auto_diagnostic_group d;
16629 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16630 27 : inform (id_loc, "an attribute that appertains to a friend "
16631 : "declaration that is not a definition is ignored");
16632 27 : }
16633 : /* Friends are treated specially. */
16634 6195013 : if (ctype == current_class_type)
16635 : ; /* We already issued a permerror. */
16636 6195013 : else if (decl && DECL_NAME (decl))
16637 : {
16638 6195010 : set_originating_module (decl, true);
16639 :
16640 6195010 : if (initialized)
16641 : /* Kludge: We need funcdef_flag to be true in do_friend for
16642 : in-class defaulted functions, but that breaks grokfndecl.
16643 : So set it here. */
16644 205744 : funcdef_flag = true;
16645 :
16646 6195010 : cplus_decl_attributes (&decl, *attrlist, 0);
16647 6195010 : *attrlist = NULL_TREE;
16648 :
16649 6195010 : tree scope = ctype ? ctype : in_namespace;
16650 6195010 : decl = do_friend (scope, unqualified_id, decl,
16651 : flags, funcdef_flag);
16652 6195010 : return decl;
16653 : }
16654 : else
16655 3 : return error_mark_node;
16656 : }
16657 :
16658 : /* Structure field. It may not be a function, except for C++. */
16659 :
16660 132381700 : if (decl == NULL_TREE)
16661 : {
16662 31833283 : if (staticp)
16663 : {
16664 : /* C++ allows static class members. All other work
16665 : for this is done by grokfield. */
16666 12514532 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
16667 : dname, type);
16668 12514532 : if (unqualified_id
16669 12514529 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16670 : {
16671 41036 : decl = check_explicit_specialization (unqualified_id, decl,
16672 : template_count,
16673 : concept_p * 8);
16674 41036 : if (decl == error_mark_node)
16675 : return error_mark_node;
16676 : }
16677 12514532 : set_linkage_for_static_data_member (decl);
16678 12514532 : if (concept_p)
16679 15 : error_at (declspecs->locations[ds_concept],
16680 : "static data member %qE declared %qs",
16681 : unqualified_id, "concept");
16682 12514517 : else if (constexpr_p && !initialized)
16683 : {
16684 9 : error_at (DECL_SOURCE_LOCATION (decl),
16685 : "%<constexpr%> static data member %qD must "
16686 : "have an initializer", decl);
16687 9 : constexpr_p = false;
16688 : }
16689 12514532 : if (consteval_p)
16690 3 : error_at (declspecs->locations[ds_consteval],
16691 : "static data member %qE declared %qs",
16692 : unqualified_id, "consteval");
16693 :
16694 12514532 : if (inlinep)
16695 21584 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16696 :
16697 12514532 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16698 12514532 : && !(cxx_dialect >= cxx17 && constexpr_p))
16699 : /* Even if there is an in-class initialization, DECL
16700 : is considered undefined until an out-of-class
16701 : definition is provided, unless this is an inline
16702 : variable. */
16703 1771771 : DECL_EXTERNAL (decl) = 1;
16704 :
16705 12514532 : if (thread_p)
16706 : {
16707 421 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16708 421 : if (!processing_template_decl)
16709 375 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16710 421 : if (declspecs->gnu_thread_keyword_p)
16711 34 : SET_DECL_GNU_TLS_P (decl);
16712 : }
16713 :
16714 : /* Set the constraints on the declaration. */
16715 12514532 : bool memtmpl = (current_template_depth
16716 12514532 : > template_class_depth (current_class_type));
16717 12514532 : if (memtmpl)
16718 : {
16719 362915 : tree ci = current_template_constraints ();
16720 362915 : set_constraints (decl, ci);
16721 : }
16722 : }
16723 : else
16724 : {
16725 19318751 : if (concept_p)
16726 : {
16727 12 : error_at (declspecs->locations[ds_concept],
16728 : "non-static data member %qE declared %qs",
16729 : unqualified_id, "concept");
16730 12 : concept_p = false;
16731 12 : constexpr_p = false;
16732 : }
16733 19318739 : else if (constexpr_p)
16734 : {
16735 9 : error_at (declspecs->locations[ds_constexpr],
16736 : "non-static data member %qE declared %qs",
16737 : unqualified_id, "constexpr");
16738 9 : constexpr_p = false;
16739 : }
16740 19318751 : if (constinit_p)
16741 : {
16742 6 : error_at (declspecs->locations[ds_constinit],
16743 : "non-static data member %qE declared %qs",
16744 : unqualified_id, "constinit");
16745 6 : constinit_p = false;
16746 : }
16747 19318751 : if (consteval_p)
16748 : {
16749 3 : error_at (declspecs->locations[ds_consteval],
16750 : "non-static data member %qE declared %qs",
16751 : unqualified_id, "consteval");
16752 3 : consteval_p = false;
16753 : }
16754 19318751 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16755 19318751 : DECL_NONADDRESSABLE_P (decl) = bitfield;
16756 19318751 : if (bitfield && !unqualified_id)
16757 129269 : DECL_PADDING_P (decl) = 1;
16758 :
16759 19318751 : if (storage_class == sc_mutable)
16760 : {
16761 144689 : DECL_MUTABLE_P (decl) = 1;
16762 144689 : storage_class = sc_none;
16763 : }
16764 :
16765 19318751 : if (initialized)
16766 : {
16767 : /* An attempt is being made to initialize a non-static
16768 : member. This is new in C++11. */
16769 2209946 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
16770 :
16771 : /* If this has been parsed with static storage class, but
16772 : errors forced staticp to be cleared, ensure NSDMI is
16773 : not present. */
16774 2209946 : if (declspecs->storage_class == sc_static)
16775 3 : DECL_INITIAL (decl) = error_mark_node;
16776 : }
16777 : }
16778 :
16779 51152034 : bad_specifiers (decl, BSP_FIELD, virtualp,
16780 : memfn_quals != TYPE_UNQUALIFIED,
16781 : staticp ? false : inlinep, friendp,
16782 : raises != NULL_TREE,
16783 : declspecs->locations);
16784 : }
16785 : }
16786 142788933 : else if (FUNC_OR_METHOD_TYPE_P (type))
16787 : {
16788 76439091 : tree original_name;
16789 76439091 : int publicp = 0;
16790 :
16791 76439091 : if (!unqualified_id)
16792 0 : return error_mark_node;
16793 :
16794 76439091 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16795 : original_name = dname;
16796 : else
16797 75622409 : original_name = unqualified_id;
16798 : // FIXME:gcc_assert (original_name == dname);
16799 :
16800 76439091 : if (storage_class == sc_auto)
16801 1 : error_at (declspecs->locations[ds_storage_class],
16802 : "storage class %<auto%> invalid for function %qs", name);
16803 76439090 : else if (storage_class == sc_register)
16804 7 : error_at (declspecs->locations[ds_storage_class],
16805 : "storage class %<register%> invalid for function %qs",
16806 : name);
16807 76439083 : else if (thread_p)
16808 : {
16809 12 : if (declspecs->gnu_thread_keyword_p)
16810 12 : error_at (declspecs->locations[ds_thread],
16811 : "storage class %<__thread%> invalid for function %qs",
16812 : name);
16813 : else
16814 0 : error_at (declspecs->locations[ds_thread],
16815 : "storage class %<thread_local%> invalid for "
16816 : "function %qs", name);
16817 : }
16818 76439071 : else if (constinit_p)
16819 3 : error_at (declspecs->locations[ds_constinit],
16820 : "%<constinit%> specifier invalid for function %qs", name);
16821 :
16822 76439091 : if (virt_specifiers)
16823 6 : error ("virt-specifiers in %qs not allowed outside a class "
16824 : "definition", name);
16825 : /* Function declaration not at top level.
16826 : Storage classes other than `extern' are not allowed
16827 : and `extern' makes no difference. */
16828 76439091 : if (! toplevel_bindings_p ()
16829 58129 : && (storage_class == sc_static
16830 58120 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
16831 76439103 : && pedantic)
16832 : {
16833 9 : if (storage_class == sc_static)
16834 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
16835 : "%<static%> specifier invalid for function %qs "
16836 : "declared out of global scope", name);
16837 : else
16838 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
16839 : "%<inline%> specifier invalid for function %qs "
16840 : "declared out of global scope", name);
16841 : }
16842 :
16843 76439091 : if (ctype == NULL_TREE)
16844 : {
16845 67705132 : if (virtualp)
16846 : {
16847 0 : error ("virtual non-class function %qs", name);
16848 0 : virtualp = 0;
16849 : }
16850 67705132 : else if (sfk == sfk_constructor
16851 67705132 : || sfk == sfk_destructor)
16852 : {
16853 3 : error (funcdef_flag
16854 : ? G_("%qs defined in a non-class scope")
16855 : : G_("%qs declared in a non-class scope"), name);
16856 3 : sfk = sfk_none;
16857 : }
16858 : }
16859 76439091 : if (consteval_p
16860 76650813 : && identifier_p (unqualified_id)
16861 76650813 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16862 : {
16863 9 : error_at (declspecs->locations[ds_consteval],
16864 : "%qD cannot be %qs", unqualified_id, "consteval");
16865 9 : consteval_p = false;
16866 : }
16867 :
16868 : /* Record whether the function is public. */
16869 76439091 : publicp = (ctype != NULL_TREE
16870 76439091 : || storage_class != sc_static);
16871 :
16872 152878182 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
16873 : declspecs, reqs, virtualp, flags, memfn_quals, rqual, raises,
16874 : 1, friendp,
16875 : publicp,
16876 76439091 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16877 76439091 : | (8 * consteval_p),
16878 : initialized,
16879 : is_xobj_member_function, sfk,
16880 : funcdef_flag,
16881 : late_return_type_p,
16882 : template_count, in_namespace, attrlist,
16883 : contract_specifiers, id_loc);
16884 76439091 : if (decl == NULL_TREE)
16885 592 : return error_mark_node;
16886 :
16887 76438499 : if (explicitp == 2)
16888 10335 : DECL_NONCONVERTING_P (decl) = 1;
16889 76438499 : if (staticp == 1)
16890 : {
16891 108592 : int invalid_static = 0;
16892 :
16893 : /* Don't allow a static member function in a class, and forbid
16894 : declaring main to be static. */
16895 108592 : if (TREE_CODE (type) == METHOD_TYPE)
16896 : {
16897 0 : permerror (input_location, "cannot declare member function %qD to have "
16898 : "static linkage", decl);
16899 0 : invalid_static = 1;
16900 : }
16901 108592 : else if (current_function_decl)
16902 : {
16903 : /* 7.1.1: There can be no static function declarations within a
16904 : block. */
16905 9 : error_at (declspecs->locations[ds_storage_class],
16906 : "cannot declare static function inside another function");
16907 9 : invalid_static = 1;
16908 : }
16909 :
16910 9 : if (invalid_static)
16911 : {
16912 76438499 : staticp = 0;
16913 : storage_class = sc_none;
16914 : }
16915 : }
16916 76438499 : if (declspecs->explicit_specifier)
16917 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16918 : }
16919 : else
16920 : {
16921 : /* It's a variable. */
16922 :
16923 : /* An uninitialized decl with `extern' is a reference. */
16924 132221951 : decl = grokvardecl (type, dname, unqualified_id,
16925 : declspecs,
16926 : initialized,
16927 : type_quals,
16928 : inlinep,
16929 : concept_p,
16930 : template_count,
16931 : ctype ? ctype : in_namespace,
16932 : id_loc);
16933 66349842 : if (decl == NULL_TREE)
16934 52 : return error_mark_node;
16935 :
16936 66349790 : bad_specifiers (decl, BSP_VAR, virtualp,
16937 : memfn_quals != TYPE_UNQUALIFIED,
16938 : inlinep, friendp, raises != NULL_TREE,
16939 : declspecs->locations);
16940 :
16941 66349790 : if (ctype)
16942 : {
16943 477727 : DECL_CONTEXT (decl) = ctype;
16944 477727 : if (staticp == 1)
16945 : {
16946 6 : permerror (declspecs->locations[ds_storage_class],
16947 : "%<static%> may not be used when defining "
16948 : "(as opposed to declaring) a static data member");
16949 6 : staticp = 0;
16950 6 : storage_class = sc_none;
16951 : }
16952 477727 : if (storage_class == sc_register && TREE_STATIC (decl))
16953 : {
16954 0 : error ("static member %qD declared %<register%>", decl);
16955 0 : storage_class = sc_none;
16956 : }
16957 477727 : if (storage_class == sc_extern && pedantic)
16958 : {
16959 0 : pedwarn (input_location, OPT_Wpedantic,
16960 : "cannot explicitly declare member %q#D to have "
16961 : "extern linkage", decl);
16962 0 : storage_class = sc_none;
16963 : }
16964 : }
16965 65872063 : else if (constexpr_p && DECL_EXTERNAL (decl))
16966 : {
16967 12 : error_at (DECL_SOURCE_LOCATION (decl),
16968 : "declaration of %<constexpr%> variable %qD "
16969 : "is not a definition", decl);
16970 12 : constexpr_p = false;
16971 : }
16972 66349790 : if (consteval_p)
16973 : {
16974 13 : error_at (DECL_SOURCE_LOCATION (decl),
16975 : "a variable cannot be declared %<consteval%>");
16976 13 : consteval_p = false;
16977 : }
16978 :
16979 66349790 : if (inlinep)
16980 6351100 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16981 66349790 : if (innermost_code == cdk_decomp)
16982 : {
16983 217889 : gcc_assert (declarator && declarator->kind == cdk_decomp);
16984 217889 : DECL_SOURCE_LOCATION (decl) = id_loc;
16985 217889 : DECL_ARTIFICIAL (decl) = 1;
16986 217889 : fit_decomposition_lang_decl (decl, NULL_TREE);
16987 : }
16988 : }
16989 :
16990 561039360 : if (VAR_P (decl) && !initialized)
16991 7290283 : if (tree auto_node = type_uses_auto (type))
16992 99 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16993 : {
16994 18 : location_t loc = declspecs->locations[ds_type_spec];
16995 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
16996 18 : TREE_TYPE (decl) = error_mark_node;
16997 : }
16998 :
16999 561039360 : if (storage_class == sc_extern && initialized && !funcdef_flag)
17000 : {
17001 267 : if (toplevel_bindings_p ())
17002 : {
17003 : /* It's common practice (and completely valid) to have a const
17004 : be initialized and declared extern. */
17005 243 : if (!(type_quals & TYPE_QUAL_CONST))
17006 19 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
17007 : "%qs initialized and declared %<extern%>", name);
17008 : }
17009 : else
17010 : {
17011 24 : error_at (DECL_SOURCE_LOCATION (decl),
17012 : "%qs has both %<extern%> and initializer", name);
17013 24 : return error_mark_node;
17014 : }
17015 : }
17016 :
17017 : /* Record `register' declaration for warnings on &
17018 : and in case doing stupid register allocation. */
17019 :
17020 561039336 : if (storage_class == sc_register)
17021 : {
17022 2069 : DECL_REGISTER (decl) = 1;
17023 : /* Warn about register storage specifiers on PARM_DECLs. */
17024 2069 : if (TREE_CODE (decl) == PARM_DECL)
17025 : {
17026 35 : if (cxx_dialect >= cxx17)
17027 24 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
17028 : "ISO C++17 does not allow %<register%> storage "
17029 : "class specifier");
17030 : else
17031 11 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
17032 : "%<register%> storage class specifier used");
17033 : }
17034 : }
17035 561037267 : else if (storage_class == sc_extern)
17036 30190138 : DECL_THIS_EXTERN (decl) = 1;
17037 530847129 : else if (storage_class == sc_static)
17038 22755051 : DECL_THIS_STATIC (decl) = 1;
17039 :
17040 561039336 : if (VAR_P (decl))
17041 : {
17042 : /* Set constexpr flag on vars (functions got it in grokfndecl). */
17043 78864298 : if (constexpr_p)
17044 18821620 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
17045 : /* And the constinit flag (which only applies to variables). */
17046 60042678 : else if (constinit_p)
17047 438 : DECL_DECLARED_CONSTINIT_P (decl) = true;
17048 : }
17049 482175038 : else if (TREE_CODE (decl) == FUNCTION_DECL)
17050 : {
17051 : /* If we saw a return type, record its location. */
17052 176986916 : location_t loc = declspecs->locations[ds_type_spec];
17053 176986916 : if (loc == UNKNOWN_LOCATION)
17054 : /* Build DECL_RESULT in start_preparsed_function. */;
17055 144616388 : else if (!DECL_RESULT (decl))
17056 : {
17057 144580605 : tree restype = TREE_TYPE (TREE_TYPE (decl));
17058 144580605 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
17059 144580605 : DECL_ARTIFICIAL (resdecl) = 1;
17060 144580605 : DECL_IGNORED_P (resdecl) = 1;
17061 144580605 : DECL_RESULT (decl) = resdecl;
17062 : }
17063 35783 : else if (funcdef_flag)
17064 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
17065 : }
17066 :
17067 : /* Record constancy and volatility on the DECL itself . There's
17068 : no need to do this when processing a template; we'll do this
17069 : for the instantiated declaration based on the type of DECL. */
17070 561039336 : if (!processing_template_decl
17071 : /* Don't do it for instantiated variable templates either,
17072 : cp_apply_type_quals_to_decl should have been called on it
17073 : already and might have been overridden in cp_finish_decl
17074 : if initializer needs runtime initialization. */
17075 561039336 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
17076 188056657 : cp_apply_type_quals_to_decl (type_quals, decl);
17077 :
17078 561039336 : return decl;
17079 : }
17080 1008669554 : }
17081 :
17082 : /* Subroutine of start_function. Ensure that each of the parameter
17083 : types (as listed in PARMS) is complete, as is required for a
17084 : function definition. */
17085 :
17086 : static void
17087 165485931 : require_complete_types_for_parms (tree parms)
17088 : {
17089 488562061 : for (; parms; parms = DECL_CHAIN (parms))
17090 : {
17091 323076130 : if (dependent_type_p (TREE_TYPE (parms)))
17092 172239986 : continue;
17093 150836144 : if (!VOID_TYPE_P (TREE_TYPE (parms))
17094 150836144 : && complete_type_or_else (TREE_TYPE (parms), parms))
17095 : {
17096 150836063 : relayout_decl (parms);
17097 150836063 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
17098 :
17099 150836063 : abstract_virtuals_error (parms, TREE_TYPE (parms));
17100 150836063 : maybe_warn_parm_abi (TREE_TYPE (parms),
17101 150836063 : DECL_SOURCE_LOCATION (parms));
17102 : }
17103 : else
17104 : /* grokparms or complete_type_or_else will have already issued
17105 : an error. */
17106 81 : TREE_TYPE (parms) = error_mark_node;
17107 : }
17108 165485931 : }
17109 :
17110 : /* Returns nonzero if T is a local variable. */
17111 :
17112 : int
17113 240574267 : local_variable_p (const_tree t)
17114 : {
17115 240574267 : if ((VAR_P (t)
17116 99419966 : && (DECL_LOCAL_DECL_P (t)
17117 99419966 : || !DECL_CONTEXT (t)
17118 99379505 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
17119 246809559 : || (TREE_CODE (t) == PARM_DECL))
17120 218385236 : return 1;
17121 :
17122 : return 0;
17123 : }
17124 :
17125 : /* Like local_variable_p, but suitable for use as a tree-walking
17126 : function. */
17127 :
17128 : static tree
17129 5790024 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
17130 : void * /*data*/)
17131 : {
17132 5790024 : if (unevaluated_p (TREE_CODE (*tp)))
17133 : {
17134 : /* DR 2082 permits local variables in unevaluated contexts
17135 : within a default argument. */
17136 20 : *walk_subtrees = 0;
17137 20 : return NULL_TREE;
17138 : }
17139 :
17140 5790004 : if (local_variable_p (*tp)
17141 5790004 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
17142 : return *tp;
17143 5790004 : else if (TYPE_P (*tp))
17144 30 : *walk_subtrees = 0;
17145 :
17146 : return NULL_TREE;
17147 : }
17148 :
17149 : /* Check that ARG, which is a default-argument expression for a
17150 : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
17151 : something goes wrong. DECL may also be a _TYPE node, rather than a
17152 : DECL, if there is no DECL available. */
17153 :
17154 : tree
17155 2783216 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
17156 : {
17157 2783216 : tree var;
17158 2783216 : tree decl_type;
17159 :
17160 2783216 : if (TREE_CODE (arg) == DEFERRED_PARSE)
17161 : /* We get a DEFERRED_PARSE when looking at an in-class declaration
17162 : with a default argument. Ignore the argument for now; we'll
17163 : deal with it after the class is complete. */
17164 : return arg;
17165 :
17166 2004665 : if (TYPE_P (decl))
17167 : {
17168 : decl_type = decl;
17169 : decl = NULL_TREE;
17170 : }
17171 : else
17172 553737 : decl_type = TREE_TYPE (decl);
17173 :
17174 2004665 : if (arg == error_mark_node
17175 2004545 : || decl == error_mark_node
17176 2004536 : || TREE_TYPE (arg) == error_mark_node
17177 4009201 : || decl_type == error_mark_node)
17178 : /* Something already went wrong. There's no need to check
17179 : further. */
17180 : return error_mark_node;
17181 :
17182 : /* [dcl.fct.default]
17183 :
17184 : A default argument expression is implicitly converted to the
17185 : parameter type. */
17186 2004536 : ++cp_unevaluated_operand;
17187 : /* Avoid digest_init clobbering the initializer. */
17188 2004536 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
17189 2004536 : perform_implicit_conversion_flags (decl_type, carg, complain,
17190 : LOOKUP_IMPLICIT);
17191 2004536 : --cp_unevaluated_operand;
17192 :
17193 : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
17194 : the call sites. */
17195 1607386 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17196 397165 : && null_ptr_cst_p (arg)
17197 : /* Don't lose side-effects as in PR90473. */
17198 2380518 : && !TREE_SIDE_EFFECTS (arg))
17199 375979 : return nullptr_node;
17200 :
17201 : /* [dcl.fct.default]
17202 :
17203 : Local variables shall not be used in default argument
17204 : expressions.
17205 :
17206 : The keyword `this' shall not be used in a default argument of a
17207 : member function. */
17208 1628557 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17209 1628557 : if (var)
17210 : {
17211 0 : if (complain & tf_warning_or_error)
17212 : {
17213 0 : if (DECL_NAME (var) == this_identifier)
17214 0 : permerror (input_location, "default argument %qE uses %qD",
17215 : arg, var);
17216 : else
17217 0 : error ("default argument %qE uses local variable %qD", arg, var);
17218 : }
17219 0 : return error_mark_node;
17220 : }
17221 :
17222 : /* All is well. */
17223 1628557 : return arg;
17224 : }
17225 :
17226 : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17227 :
17228 : static tree
17229 273445365 : type_is_deprecated (tree type)
17230 : {
17231 403227432 : enum tree_code code;
17232 403227432 : if (TREE_DEPRECATED (type))
17233 : return type;
17234 403188239 : if (TYPE_NAME (type))
17235 : {
17236 269347155 : if (TREE_DEPRECATED (TYPE_NAME (type)))
17237 : return type;
17238 : else
17239 : {
17240 269347149 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17241 269347149 : return NULL_TREE;
17242 : }
17243 : }
17244 :
17245 : /* Do warn about using typedefs to a deprecated class. */
17246 133841084 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17247 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17248 :
17249 133841084 : code = TREE_CODE (type);
17250 :
17251 133841084 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17252 133841084 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17253 : || code == METHOD_TYPE || code == ARRAY_TYPE)
17254 129670330 : return type_is_deprecated (TREE_TYPE (type));
17255 :
17256 111737 : if (TYPE_PTRMEMFUNC_P (type))
17257 111737 : return type_is_deprecated
17258 111737 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17259 :
17260 : return NULL_TREE;
17261 : }
17262 :
17263 : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17264 :
17265 : static tree
17266 274623990 : type_is_unavailable (tree type)
17267 : {
17268 405107702 : enum tree_code code;
17269 405107702 : if (TREE_UNAVAILABLE (type))
17270 : return type;
17271 405107696 : if (TYPE_NAME (type))
17272 : {
17273 270564913 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17274 : return type;
17275 : else
17276 : {
17277 270564907 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17278 270564907 : return NULL_TREE;
17279 : }
17280 : }
17281 :
17282 : /* Do warn about using typedefs to a deprecated class. */
17283 134542783 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17284 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17285 :
17286 134542783 : code = TREE_CODE (type);
17287 :
17288 134542783 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17289 134542783 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17290 : || code == METHOD_TYPE || code == ARRAY_TYPE)
17291 130266719 : return type_is_unavailable (TREE_TYPE (type));
17292 :
17293 216993 : if (TYPE_PTRMEMFUNC_P (type))
17294 216993 : return type_is_unavailable
17295 216993 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17296 :
17297 : return NULL_TREE;
17298 : }
17299 :
17300 : /* Decode the list of parameter types for a function type.
17301 : Given the list of things declared inside the parens,
17302 : return a list of types.
17303 :
17304 : If this parameter does not end with an ellipsis, we append
17305 : void_list_node.
17306 :
17307 : *PARMS is set to the chain of PARM_DECLs created. */
17308 :
17309 : tree
17310 188234071 : grokparms (tree parmlist, tree *parms)
17311 : {
17312 188234071 : tree result = NULL_TREE;
17313 188234071 : tree decls = NULL_TREE;
17314 188234071 : tree parm;
17315 188234071 : int any_error = 0;
17316 :
17317 463458184 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17318 : {
17319 461138513 : tree type = NULL_TREE;
17320 461138513 : tree init = TREE_PURPOSE (parm);
17321 461138513 : tree decl = TREE_VALUE (parm);
17322 :
17323 461138513 : if (parm == void_list_node || parm == explicit_void_list_node)
17324 : break;
17325 :
17326 275224127 : if (! decl || TREE_TYPE (decl) == error_mark_node)
17327 : {
17328 186 : any_error = 1;
17329 186 : continue;
17330 : }
17331 :
17332 275223941 : type = TREE_TYPE (decl);
17333 275223941 : if (VOID_TYPE_P (type))
17334 : {
17335 80 : if (same_type_p (type, void_type_node)
17336 71 : && !init
17337 59 : && !DECL_NAME (decl) && !result
17338 106 : && TREE_CHAIN (parm) == void_list_node)
17339 : /* DR 577: A parameter list consisting of a single
17340 : unnamed parameter of non-dependent type 'void'. */
17341 : break;
17342 66 : else if (cv_qualified_p (type))
17343 9 : error_at (DECL_SOURCE_LOCATION (decl),
17344 : "invalid use of cv-qualified type %qT in "
17345 : "parameter declaration", type);
17346 : else
17347 57 : error_at (DECL_SOURCE_LOCATION (decl),
17348 : "invalid use of type %<void%> in parameter "
17349 : "declaration");
17350 : /* It's not a good idea to actually create parameters of
17351 : type `void'; other parts of the compiler assume that a
17352 : void type terminates the parameter list. */
17353 66 : type = error_mark_node;
17354 66 : TREE_TYPE (decl) = error_mark_node;
17355 : }
17356 :
17357 275223927 : if (type != error_mark_node)
17358 : {
17359 275223861 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17360 : {
17361 274623990 : tree unavailtype = type_is_unavailable (type);
17362 274623990 : if (unavailtype)
17363 12 : cp_handle_deprecated_or_unavailable (unavailtype);
17364 : }
17365 275223861 : if (deprecated_state != DEPRECATED_SUPPRESS
17366 275223861 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17367 : {
17368 273445365 : tree deptype = type_is_deprecated (type);
17369 273445365 : if (deptype)
17370 39199 : cp_handle_deprecated_or_unavailable (deptype);
17371 : }
17372 :
17373 : /* [dcl.fct] "A parameter with volatile-qualified type is
17374 : deprecated." */
17375 275223861 : if (CP_TYPE_VOLATILE_P (type))
17376 504 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17377 : "%<volatile%>-qualified parameter is "
17378 : "deprecated");
17379 :
17380 : /* Top-level qualifiers on the parameters are
17381 : ignored for function types. */
17382 275223861 : type = cp_build_qualified_type (type, 0);
17383 275223861 : if (TREE_CODE (type) == METHOD_TYPE)
17384 : {
17385 0 : error ("parameter %qD invalidly declared method type", decl);
17386 0 : type = build_pointer_type (type);
17387 0 : TREE_TYPE (decl) = type;
17388 : }
17389 275223861 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17390 : {
17391 : /* Before C++17 DR 393:
17392 : [dcl.fct]/6, parameter types cannot contain pointers
17393 : (references) to arrays of unknown bound. */
17394 1618104 : tree t = TREE_TYPE (type);
17395 1618104 : int ptr = TYPE_PTR_P (type);
17396 :
17397 1775126 : while (1)
17398 : {
17399 1696615 : if (TYPE_PTR_P (t))
17400 : ptr = 1;
17401 1621227 : else if (TREE_CODE (t) != ARRAY_TYPE)
17402 : break;
17403 3133 : else if (!TYPE_DOMAIN (t))
17404 : break;
17405 78511 : t = TREE_TYPE (t);
17406 : }
17407 1618104 : if (TREE_CODE (t) == ARRAY_TYPE)
17408 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17409 : ptr
17410 : ? G_("parameter %qD includes pointer to array of "
17411 : "unknown bound %qT")
17412 : : G_("parameter %qD includes reference to array of "
17413 : "unknown bound %qT"),
17414 : decl, t);
17415 : }
17416 :
17417 275223861 : if (init && !processing_template_decl)
17418 1332279 : init = check_default_argument (decl, init, tf_warning_or_error);
17419 : }
17420 :
17421 275223927 : DECL_CHAIN (decl) = decls;
17422 275223927 : decls = decl;
17423 275223927 : result = tree_cons (init, type, result);
17424 : }
17425 188234071 : decls = nreverse (decls);
17426 188234071 : result = nreverse (result);
17427 188234071 : if (parm)
17428 185914400 : result = chainon (result, void_list_node);
17429 188234071 : *parms = decls;
17430 188234071 : if (any_error)
17431 172 : result = NULL_TREE;
17432 :
17433 172 : if (any_error)
17434 : /* We had parm errors, recover by giving the function (...) type. */
17435 172 : result = NULL_TREE;
17436 :
17437 188234071 : return result;
17438 : }
17439 :
17440 :
17441 : /* D is a constructor or overloaded `operator='.
17442 :
17443 : Let T be the class in which D is declared. Then, this function
17444 : returns:
17445 :
17446 : -1 if D's is an ill-formed constructor or copy assignment operator
17447 : whose first parameter is of type `T'.
17448 : 0 if D is not a copy constructor or copy assignment
17449 : operator.
17450 : 1 if D is a copy constructor or copy assignment operator whose
17451 : first parameter is a reference to non-const qualified T.
17452 : 2 if D is a copy constructor or copy assignment operator whose
17453 : first parameter is a reference to const qualified T.
17454 :
17455 : This function can be used as a predicate. Positive values indicate
17456 : a copy constructor and nonzero values indicate a copy assignment
17457 : operator. */
17458 :
17459 : int
17460 378706313 : copy_fn_p (const_tree d)
17461 : {
17462 378706313 : tree args;
17463 378706313 : tree arg_type;
17464 378706313 : int result = 1;
17465 :
17466 378706313 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17467 :
17468 378706313 : if (TREE_CODE (d) == TEMPLATE_DECL
17469 378706313 : || (DECL_TEMPLATE_INFO (d)
17470 169898283 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17471 : /* Instantiations of template member functions are never copy
17472 : functions. Note that member functions of templated classes are
17473 : represented as template functions internally, and we must
17474 : accept those as copy functions. */
17475 : return 0;
17476 :
17477 324252905 : if (!DECL_CONSTRUCTOR_P (d)
17478 324252905 : && DECL_NAME (d) != assign_op_identifier)
17479 : return 0;
17480 :
17481 323464531 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17482 : {
17483 126 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17484 126 : if (!TYPE_REF_P (object_param)
17485 120 : || TYPE_REF_IS_RVALUE (object_param)
17486 : /* Reject unrelated object parameters. */
17487 108 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17488 234 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17489 24 : return 0;
17490 102 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17491 : }
17492 : else
17493 323464405 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17494 323464507 : if (!args)
17495 : return 0;
17496 :
17497 323456983 : arg_type = TREE_VALUE (args);
17498 323456983 : if (arg_type == error_mark_node)
17499 : return 0;
17500 :
17501 323456948 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17502 : {
17503 : /* Pass by value copy assignment operator. */
17504 : result = -1;
17505 : }
17506 323455709 : else if (TYPE_REF_P (arg_type)
17507 207386347 : && !TYPE_REF_IS_RVALUE (arg_type)
17508 457720107 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17509 : {
17510 111768629 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17511 111605146 : result = 2;
17512 : }
17513 : else
17514 : return 0;
17515 :
17516 111769868 : args = TREE_CHAIN (args);
17517 :
17518 114490376 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17519 : /* There are more non-optional args. */
17520 : return 0;
17521 :
17522 : return result;
17523 : }
17524 :
17525 : /* D is a constructor or overloaded `operator='.
17526 :
17527 : Let T be the class in which D is declared. Then, this function
17528 : returns true when D is a move constructor or move assignment
17529 : operator, false otherwise. */
17530 :
17531 : bool
17532 175431004 : move_fn_p (const_tree d)
17533 : {
17534 175431004 : if (cxx_dialect == cxx98)
17535 : /* There are no move constructors if we are in C++98 mode. */
17536 : return false;
17537 :
17538 175365805 : if (TREE_CODE (d) == TEMPLATE_DECL
17539 175365805 : || (DECL_TEMPLATE_INFO (d)
17540 73066148 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17541 : /* Instantiations of template member functions are never move
17542 : functions. Note that member functions of templated classes are
17543 : represented as template functions internally, and we must
17544 : accept those as move functions. */
17545 : return 0;
17546 :
17547 144328560 : return move_signature_fn_p (d);
17548 : }
17549 :
17550 : /* D is a constructor or overloaded `operator='.
17551 :
17552 : Then, this function returns true when D has the same signature as a move
17553 : constructor or move assignment operator (because either it is such a
17554 : ctor/op= or it is a template specialization with the same signature),
17555 : false otherwise. */
17556 :
17557 : bool
17558 144328560 : move_signature_fn_p (const_tree d)
17559 : {
17560 144328560 : tree args;
17561 144328560 : tree arg_type;
17562 144328560 : bool result = false;
17563 :
17564 144328560 : if (!DECL_CONSTRUCTOR_P (d)
17565 144328560 : && DECL_NAME (d) != assign_op_identifier)
17566 : return 0;
17567 :
17568 144328491 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17569 : {
17570 68 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17571 68 : if (!TYPE_REF_P (object_param)
17572 64 : || TYPE_REF_IS_RVALUE (object_param)
17573 : /* Reject unrelated object parameters. */
17574 56 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17575 124 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17576 16 : return 0;
17577 52 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17578 : }
17579 : else
17580 144328423 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17581 144328475 : if (!args)
17582 : return 0;
17583 :
17584 144325685 : arg_type = TREE_VALUE (args);
17585 144325685 : if (arg_type == error_mark_node)
17586 : return 0;
17587 :
17588 144325677 : if (TYPE_REF_P (arg_type)
17589 71717382 : && TYPE_REF_IS_RVALUE (arg_type)
17590 201801717 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17591 : DECL_CONTEXT (d)))
17592 : result = true;
17593 :
17594 144325677 : args = TREE_CHAIN (args);
17595 :
17596 159631151 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17597 : /* There are more non-optional args. */
17598 : return false;
17599 :
17600 : return result;
17601 : }
17602 :
17603 : /* Remember any special properties of member function DECL. */
17604 :
17605 : void
17606 360970906 : grok_special_member_properties (tree decl)
17607 : {
17608 360970906 : tree class_type;
17609 :
17610 360970906 : if (TREE_CODE (decl) == USING_DECL
17611 360970906 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17612 : return;
17613 :
17614 339889794 : class_type = DECL_CONTEXT (decl);
17615 339889794 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17616 : {
17617 155704553 : int ctor = copy_fn_p (decl);
17618 :
17619 155704553 : if (!DECL_ARTIFICIAL (decl))
17620 103757804 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17621 :
17622 155704553 : if (ctor > 0)
17623 : {
17624 : /* [class.copy]
17625 :
17626 : A non-template constructor for class X is a copy
17627 : constructor if its first parameter is of type X&, const
17628 : X&, volatile X& or const volatile X&, and either there
17629 : are no other parameters or else all other parameters have
17630 : default arguments. */
17631 32851233 : TYPE_HAS_COPY_CTOR (class_type) = 1;
17632 32851233 : if (ctor > 1)
17633 32775224 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17634 : }
17635 :
17636 155704553 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17637 33602183 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17638 :
17639 155704553 : if (is_list_ctor (decl))
17640 1480116 : TYPE_HAS_LIST_CTOR (class_type) = 1;
17641 :
17642 155704553 : if (maybe_constexpr_fn (decl)
17643 155704553 : && !ctor && !move_fn_p (decl))
17644 55036541 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17645 : }
17646 184185241 : else if (DECL_NAME (decl) == assign_op_identifier)
17647 : {
17648 : /* [class.copy]
17649 :
17650 : A non-template assignment operator for class X is a copy
17651 : assignment operator if its parameter is of type X, X&, const
17652 : X&, volatile X& or const volatile X&. */
17653 :
17654 15047209 : int assop = copy_fn_p (decl);
17655 :
17656 15047209 : if (assop)
17657 : {
17658 7689619 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17659 7689619 : if (assop != 1)
17660 7662066 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17661 : }
17662 : }
17663 169138032 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17664 2823485 : TYPE_HAS_CONVERSION (class_type) = true;
17665 :
17666 : /* Destructors are handled in check_methods. */
17667 : }
17668 :
17669 : /* Check a constructor DECL has the correct form. Complains
17670 : if the class has a constructor of the form X(X). */
17671 :
17672 : bool
17673 47716457 : grok_ctor_properties (const_tree ctype, const_tree decl)
17674 : {
17675 47716457 : int ctor_parm = copy_fn_p (decl);
17676 :
17677 47716457 : if (ctor_parm < 0)
17678 : {
17679 : /* [class.copy]
17680 :
17681 : A declaration of a constructor for a class X is ill-formed if
17682 : its first parameter is of type (optionally cv-qualified) X
17683 : and either there are no other parameters or else all other
17684 : parameters have default arguments.
17685 :
17686 : We *don't* complain about member template instantiations that
17687 : have this form, though; they can occur as we try to decide
17688 : what constructor to use during overload resolution. Since
17689 : overload resolution will never prefer such a constructor to
17690 : the non-template copy constructor (which is either explicitly
17691 : or implicitly defined), there's no need to worry about their
17692 : existence. Theoretically, they should never even be
17693 : instantiated, but that's hard to forestall. */
17694 9 : error_at (DECL_SOURCE_LOCATION (decl),
17695 : "invalid constructor; you probably meant %<%T (const %T&)%>",
17696 : ctype, ctype);
17697 9 : return false;
17698 : }
17699 :
17700 : return true;
17701 : }
17702 :
17703 : /* DECL is a declaration for an overloaded or conversion operator. If
17704 : COMPLAIN is true, errors are issued for invalid declarations. */
17705 :
17706 : bool
17707 60233080 : grok_op_properties (tree decl, bool complain)
17708 : {
17709 60233080 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17710 60233080 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17711 60233080 : tree name = DECL_NAME (decl);
17712 60233080 : location_t loc = DECL_SOURCE_LOCATION (decl);
17713 :
17714 60233080 : tree class_type = DECL_CONTEXT (decl);
17715 60233080 : if (class_type && !CLASS_TYPE_P (class_type))
17716 : class_type = NULL_TREE;
17717 :
17718 60233080 : tree_code operator_code;
17719 60233080 : unsigned op_flags;
17720 60233080 : if (IDENTIFIER_CONV_OP_P (name))
17721 : {
17722 : /* Conversion operators are TYPE_EXPR for the purposes of this
17723 : function. */
17724 : operator_code = TYPE_EXPR;
17725 : op_flags = OVL_OP_FLAG_UNARY;
17726 : }
17727 : else
17728 : {
17729 57795484 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17730 :
17731 57795484 : operator_code = ovl_op->tree_code;
17732 57795484 : op_flags = ovl_op->flags;
17733 57795484 : gcc_checking_assert (operator_code != ERROR_MARK);
17734 57795484 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17735 : }
17736 :
17737 57795484 : if (op_flags & OVL_OP_FLAG_ALLOC)
17738 : {
17739 : /* operator new and operator delete are quite special. */
17740 323153 : if (class_type)
17741 7367 : switch (op_flags)
17742 : {
17743 4965 : case OVL_OP_FLAG_ALLOC:
17744 4965 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17745 4965 : break;
17746 :
17747 2131 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17748 2131 : TYPE_GETS_DELETE (class_type) |= 1;
17749 2131 : break;
17750 :
17751 132 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17752 132 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17753 132 : break;
17754 :
17755 139 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17756 139 : TYPE_GETS_DELETE (class_type) |= 2;
17757 139 : break;
17758 :
17759 0 : default:
17760 0 : gcc_unreachable ();
17761 : }
17762 :
17763 : /* [basic.std.dynamic.allocation]/1:
17764 :
17765 : A program is ill-formed if an allocation function is declared
17766 : in a namespace scope other than global scope or declared
17767 : static in global scope.
17768 :
17769 : The same also holds true for deallocation functions. */
17770 323153 : if (DECL_NAMESPACE_SCOPE_P (decl))
17771 : {
17772 315786 : if (CP_DECL_CONTEXT (decl) != global_namespace)
17773 : {
17774 6 : error_at (loc, "%qD may not be declared within a namespace",
17775 : decl);
17776 6 : return false;
17777 : }
17778 :
17779 315780 : if (!TREE_PUBLIC (decl))
17780 : {
17781 6 : error_at (loc, "%qD may not be declared as static", decl);
17782 6 : return false;
17783 : }
17784 : }
17785 :
17786 : /* Check for replaceable global new/delete operators with
17787 : const std::nothrow_t & last argument, other replaceable global
17788 : new/delete operators are marked in cxx_init_decl_processing. */
17789 323141 : if (CP_DECL_CONTEXT (decl) == global_namespace)
17790 : {
17791 315774 : tree args = argtypes;
17792 315774 : if (args
17793 315762 : && args != void_list_node
17794 947280 : && same_type_p (TREE_VALUE (args),
17795 : (op_flags & OVL_OP_FLAG_DELETE)
17796 : ? ptr_type_node : size_type_node))
17797 : {
17798 315732 : args = TREE_CHAIN (args);
17799 315732 : if (aligned_allocation_fn_p (decl))
17800 101692 : args = TREE_CHAIN (args);
17801 315732 : if (args
17802 315726 : && args != void_list_node
17803 526473 : && TREE_CHAIN (args) == void_list_node)
17804 : {
17805 185274 : tree t = TREE_VALUE (args);
17806 185274 : if (TYPE_REF_P (t)
17807 105099 : && !TYPE_REF_IS_RVALUE (t)
17808 105099 : && (t = TREE_TYPE (t))
17809 105099 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
17810 289913 : && is_std_class (t, "nothrow_t"))
17811 104639 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
17812 : }
17813 : }
17814 : }
17815 :
17816 323141 : if (op_flags & OVL_OP_FLAG_DELETE)
17817 : {
17818 184988 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
17819 184988 : coerce_delete_type (decl, loc);
17820 : }
17821 : else
17822 : {
17823 138153 : DECL_SET_IS_OPERATOR_NEW (decl, true);
17824 138153 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
17825 : }
17826 :
17827 323141 : return true;
17828 : }
17829 :
17830 : /* An operator function must either be a non-static member function
17831 : or have at least one parameter of a class, a reference to a class,
17832 : an enumeration, or a reference to an enumeration. 13.4.0.6 */
17833 59909927 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17834 : {
17835 14968123 : if (operator_code == TYPE_EXPR
17836 14968123 : || operator_code == COMPONENT_REF
17837 14968102 : || operator_code == NOP_EXPR)
17838 : {
17839 33 : error_at (loc, "%qD must be a non-static member function", decl);
17840 33 : return false;
17841 : }
17842 :
17843 14968090 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
17844 : {
17845 120598 : if (! DECL_STATIC_FUNCTION_P (decl))
17846 : {
17847 12 : error_at (loc, "%qD must be a member function", decl);
17848 12 : return false;
17849 : }
17850 120586 : if (cxx_dialect < cxx23
17851 : /* For lambdas we diagnose static lambda specifier elsewhere. */
17852 206284 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
17853 : /* For instantiations, we have diagnosed this already. */
17854 223714 : && ! DECL_USE_TEMPLATE (decl))
17855 103122 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
17856 : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
17857 : decl);
17858 120586 : if (operator_code == ARRAY_REF)
17859 : /* static operator[] should have exactly one argument
17860 : for C++20 and earlier, so that it isn't multidimensional. */
17861 : op_flags = OVL_OP_FLAG_UNARY;
17862 : }
17863 14847492 : else if (DECL_STATIC_FUNCTION_P (decl))
17864 : {
17865 9 : error_at (loc, "%qD must be either a non-static member "
17866 : "function or a non-member function", decl);
17867 9 : return false;
17868 : }
17869 : else
17870 412063 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
17871 : {
17872 15259546 : if (!arg || arg == void_list_node)
17873 : {
17874 18 : if (complain)
17875 15 : error_at (loc, "%qD must have an argument of class or "
17876 : "enumerated type", decl);
17877 18 : return false;
17878 : }
17879 :
17880 15259528 : tree type = non_reference (TREE_VALUE (arg));
17881 15259528 : if (type == error_mark_node)
17882 : return false;
17883 :
17884 : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
17885 : because these checks are performed even on template
17886 : functions. */
17887 15259525 : if (MAYBE_CLASS_TYPE_P (type)
17888 1377444 : || TREE_CODE (type) == ENUMERAL_TYPE)
17889 : break;
17890 412063 : }
17891 : }
17892 :
17893 59909814 : if (operator_code == CALL_EXPR)
17894 : /* There are no further restrictions on the arguments to an overloaded
17895 : "operator ()". */
17896 : return true;
17897 :
17898 : /* C++23 allows an arbitrary number of parameters and default arguments for
17899 : operator[], and none of the other checks below apply. */
17900 48896172 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
17901 : return true;
17902 :
17903 48685380 : if (operator_code == COND_EXPR)
17904 : {
17905 : /* 13.4.0.3 */
17906 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
17907 6 : return false;
17908 : }
17909 :
17910 : /* Count the number of arguments and check for ellipsis. */
17911 : int arity = 0;
17912 138216741 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
17913 : {
17914 89531382 : if (!arg)
17915 : {
17916 15 : error_at (loc, "%qD must not have variable number of arguments",
17917 : decl);
17918 15 : return false;
17919 : }
17920 89531367 : ++arity;
17921 : }
17922 : /* FIXME: We need tests for these errors with xobj member functions. */
17923 : /* Verify correct number of arguments. */
17924 48685359 : switch (op_flags)
17925 : {
17926 10113654 : case OVL_OP_FLAG_AMBIARY:
17927 10113654 : if (arity == 1)
17928 : {
17929 : /* We have a unary instance of an ambi-ary op. Remap to the
17930 : unary one. */
17931 4377103 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
17932 4377103 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
17933 4377103 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
17934 4377103 : operator_code = ovl_op->tree_code;
17935 4377103 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17936 : }
17937 5736551 : else if (arity != 2)
17938 : {
17939 : /* This was an ambiguous operator but is invalid. */
17940 3 : error_at (loc,
17941 : methodp
17942 : ? G_("%qD must have either zero or one argument")
17943 : : G_("%qD must have either one or two arguments"), decl);
17944 3 : return false;
17945 : }
17946 5736548 : else if ((operator_code == POSTINCREMENT_EXPR
17947 5736548 : || operator_code == POSTDECREMENT_EXPR)
17948 2693710 : && ! processing_template_decl
17949 : /* x++ and x--'s second argument must be an int. */
17950 7540570 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
17951 : integer_type_node))
17952 : {
17953 15 : error_at (loc,
17954 : methodp
17955 : ? G_("postfix %qD must have %<int%> as its argument")
17956 : : G_("postfix %qD must have %<int%> as its second argument"),
17957 : decl);
17958 15 : return false;
17959 : }
17960 : break;
17961 :
17962 3462278 : case OVL_OP_FLAG_UNARY:
17963 3462278 : if (arity != 1)
17964 : {
17965 9 : error_at (loc,
17966 : methodp
17967 : ? G_("%qD must have no arguments")
17968 : : G_("%qD must have exactly one argument"), decl);
17969 9 : return false;
17970 : }
17971 : break;
17972 :
17973 35109427 : case OVL_OP_FLAG_BINARY:
17974 35109427 : if (arity != 2)
17975 : {
17976 3 : error_at (loc,
17977 : methodp
17978 : ? G_("%qD must have exactly one argument")
17979 : : G_("%qD must have exactly two arguments"), decl);
17980 3 : return false;
17981 : }
17982 : break;
17983 :
17984 0 : default:
17985 0 : gcc_unreachable ();
17986 : }
17987 :
17988 : /* There can be no default arguments. */
17989 138216600 : for (tree arg = argtypes; arg && arg != void_list_node;
17990 89531271 : arg = TREE_CHAIN (arg))
17991 89531286 : if (TREE_PURPOSE (arg))
17992 : {
17993 15 : TREE_PURPOSE (arg) = NULL_TREE;
17994 15 : error_at (loc, "%qD cannot have default arguments", decl);
17995 15 : return false;
17996 : }
17997 :
17998 : /* At this point the declaration is well-formed. It may not be
17999 : sensible though. */
18000 :
18001 : /* Check member function warnings only on the in-class declaration.
18002 : There's no point warning on an out-of-class definition. */
18003 48685314 : if (class_type && class_type != current_class_type)
18004 : return true;
18005 :
18006 : /* Warn about conversion operators that will never be used. */
18007 46859027 : if (IDENTIFIER_CONV_OP_P (name)
18008 2437176 : && ! DECL_TEMPLATE_INFO (decl)
18009 47845754 : && warn_class_conversion)
18010 : {
18011 986694 : tree t = TREE_TYPE (name);
18012 986694 : int ref = TYPE_REF_P (t);
18013 :
18014 986694 : if (ref)
18015 20856 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
18016 :
18017 986694 : if (VOID_TYPE_P (t))
18018 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
18019 : "will never use a type conversion operator", class_type);
18020 986685 : else if (class_type)
18021 : {
18022 986685 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
18023 24 : warning_at (loc, OPT_Wclass_conversion,
18024 : ref
18025 : ? G_("converting %qT to a reference to the same type "
18026 : "will never use a type conversion operator")
18027 : : G_("converting %qT to the same type "
18028 : "will never use a type conversion operator"),
18029 : class_type);
18030 : /* Don't force t to be complete here. */
18031 986670 : else if (MAYBE_CLASS_TYPE_P (t)
18032 564588 : && COMPLETE_TYPE_P (t)
18033 151312 : && DERIVED_FROM_P (t, class_type))
18034 30 : warning_at (loc, OPT_Wclass_conversion,
18035 : ref
18036 : ? G_("converting %qT to a reference to a base class "
18037 : "%qT will never use a type conversion operator")
18038 : : G_("converting %qT to a base class %qT "
18039 : "will never use a type conversion operator"),
18040 : class_type, t);
18041 : }
18042 : }
18043 :
18044 46859027 : if (!warn_ecpp)
18045 : return true;
18046 :
18047 : /* Effective C++ rules below. */
18048 :
18049 : /* More Effective C++ rule 7. */
18050 54 : if (operator_code == TRUTH_ANDIF_EXPR
18051 54 : || operator_code == TRUTH_ORIF_EXPR
18052 54 : || operator_code == COMPOUND_EXPR)
18053 0 : warning_at (loc, OPT_Weffc__,
18054 : "user-defined %qD always evaluates both arguments", decl);
18055 :
18056 : /* More Effective C++ rule 6. */
18057 54 : if (operator_code == POSTINCREMENT_EXPR
18058 : || operator_code == POSTDECREMENT_EXPR
18059 : || operator_code == PREINCREMENT_EXPR
18060 54 : || operator_code == PREDECREMENT_EXPR)
18061 : {
18062 0 : tree arg = TREE_VALUE (argtypes);
18063 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
18064 0 : if (methodp || TYPE_REF_P (arg))
18065 0 : arg = TREE_TYPE (arg);
18066 0 : arg = TYPE_MAIN_VARIANT (arg);
18067 :
18068 0 : if (operator_code == PREINCREMENT_EXPR
18069 : || operator_code == PREDECREMENT_EXPR)
18070 : {
18071 0 : if (!TYPE_REF_P (ret)
18072 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
18073 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
18074 : build_reference_type (arg));
18075 : }
18076 : else
18077 : {
18078 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
18079 0 : warning_at (loc, OPT_Weffc__,
18080 : "postfix %qD should return %qT", decl, arg);
18081 : }
18082 : }
18083 :
18084 : /* Effective C++ rule 23. */
18085 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
18086 : && (operator_code == PLUS_EXPR
18087 9 : || operator_code == MINUS_EXPR
18088 9 : || operator_code == TRUNC_DIV_EXPR
18089 : || operator_code == MULT_EXPR
18090 : || operator_code == TRUNC_MOD_EXPR)
18091 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
18092 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
18093 :
18094 : return true;
18095 : }
18096 :
18097 : /* Return a string giving the keyword associate with CODE. */
18098 :
18099 : const char *
18100 3363 : tag_name (enum tag_types code)
18101 : {
18102 3363 : switch (code)
18103 : {
18104 : case record_type:
18105 : return "struct";
18106 107 : case class_type:
18107 107 : return "class";
18108 3 : case union_type:
18109 3 : return "union";
18110 9 : case enum_type:
18111 9 : return "enum";
18112 3155 : case typename_type:
18113 3155 : case none_type:
18114 3155 : return "typename";
18115 29 : case scope_type:
18116 29 : return nullptr;
18117 : }
18118 0 : gcc_unreachable ();
18119 : }
18120 :
18121 : /* Name lookup in an elaborated-type-specifier (after the keyword
18122 : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
18123 : elaborated-type-specifier is invalid, issue a diagnostic and return
18124 : error_mark_node; otherwise, return the *_TYPE to which it referred.
18125 : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
18126 :
18127 : tree
18128 11793579 : check_elaborated_type_specifier (enum tag_types tag_code,
18129 : tree decl,
18130 : bool allow_template_p)
18131 : {
18132 11793579 : tree type;
18133 :
18134 : /* In the case of:
18135 :
18136 : struct S { struct S *p; };
18137 :
18138 : name lookup will find the TYPE_DECL for the implicit "S::S"
18139 : typedef. Adjust for that here. */
18140 11793579 : if (DECL_SELF_REFERENCE_P (decl))
18141 398247 : decl = TYPE_NAME (TREE_TYPE (decl));
18142 :
18143 11793579 : type = TREE_TYPE (decl);
18144 :
18145 : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
18146 : is false for this case as well. */
18147 11793579 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18148 : {
18149 9 : error ("using template type parameter %qT after %qs",
18150 : type, tag_name (tag_code));
18151 9 : return error_mark_node;
18152 : }
18153 : /* Accept template template parameters. */
18154 11793570 : else if (allow_template_p
18155 6598802 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
18156 6598790 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
18157 : ;
18158 : /* [dcl.type.elab]
18159 :
18160 : If the identifier resolves to a typedef-name or the
18161 : simple-template-id resolves to an alias template
18162 : specialization, the elaborated-type-specifier is ill-formed.
18163 :
18164 : In other words, the only legitimate declaration to use in the
18165 : elaborated type specifier is the implicit typedef created when
18166 : the type is declared. */
18167 11793555 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
18168 398371 : && !DECL_SELF_REFERENCE_P (decl)
18169 11793679 : && tag_code != typename_type)
18170 : {
18171 78 : auto_diagnostic_group d;
18172 78 : if (alias_template_specialization_p (type, nt_opaque))
18173 21 : error ("using alias template specialization %qT after %qs",
18174 : type, tag_name (tag_code));
18175 : else
18176 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
18177 78 : inform (DECL_SOURCE_LOCATION (decl),
18178 : "%qD has a previous declaration here", decl);
18179 78 : return error_mark_node;
18180 78 : }
18181 11793477 : else if (TREE_CODE (type) != RECORD_TYPE
18182 11793477 : && TREE_CODE (type) != UNION_TYPE
18183 : && tag_code != enum_type
18184 69348 : && tag_code != typename_type)
18185 : {
18186 6 : auto_diagnostic_group d;
18187 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
18188 6 : inform (location_of (type), "%qT has a previous declaration here", type);
18189 6 : return error_mark_node;
18190 6 : }
18191 11793471 : else if (TREE_CODE (type) != ENUMERAL_TYPE
18192 11724165 : && tag_code == enum_type)
18193 : {
18194 3 : auto_diagnostic_group d;
18195 3 : error ("%qT referred to as enum", type);
18196 3 : inform (location_of (type), "%qT has a previous declaration here", type);
18197 3 : return error_mark_node;
18198 3 : }
18199 11793468 : else if (!allow_template_p
18200 5194708 : && TREE_CODE (type) == RECORD_TYPE
18201 16867637 : && CLASSTYPE_IS_TEMPLATE (type))
18202 : {
18203 : /* If a class template appears as elaborated type specifier
18204 : without a template header such as:
18205 :
18206 : template <class T> class C {};
18207 : void f(class C); // No template header here
18208 :
18209 : then the required template argument is missing. */
18210 18 : error ("template argument required for %<%s %T%>",
18211 : tag_name (tag_code),
18212 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18213 18 : return error_mark_node;
18214 : }
18215 :
18216 : return type;
18217 : }
18218 :
18219 : /* Lookup NAME of an elaborated type specifier according to SCOPE and
18220 : issue diagnostics if necessary. Return *_TYPE node upon success,
18221 : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18222 : error. */
18223 :
18224 : static tree
18225 25416593 : lookup_and_check_tag (enum tag_types tag_code, tree name,
18226 : TAG_how how, bool template_header_p)
18227 : {
18228 25416593 : tree decl;
18229 25416593 : if (how == TAG_how::GLOBAL)
18230 : {
18231 : /* First try ordinary name lookup, ignoring hidden class name
18232 : injected via friend declaration. */
18233 2071476 : decl = lookup_name (name, LOOK_want::TYPE);
18234 : /* If that fails, the name will be placed in the smallest
18235 : non-class, non-function-prototype scope according to 3.3.1/5.
18236 : We may already have a hidden name declared as friend in this
18237 : scope. So lookup again but not ignoring hidden names.
18238 : If we find one, that name will be made visible rather than
18239 : creating a new tag. */
18240 2071476 : if (!decl)
18241 17828 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18242 : }
18243 : else
18244 23345117 : decl = lookup_elaborated_type (name, how);
18245 :
18246 23362945 : if (!decl)
18247 : /* We found nothing. */
18248 : return NULL_TREE;
18249 :
18250 5774079 : if (TREE_CODE (decl) == TREE_LIST)
18251 : {
18252 3 : auto_diagnostic_group d;
18253 3 : error ("reference to %qD is ambiguous", name);
18254 3 : print_candidates (input_location, decl);
18255 3 : return error_mark_node;
18256 3 : }
18257 :
18258 2322137 : if (DECL_CLASS_TEMPLATE_P (decl)
18259 2322131 : && !template_header_p
18260 8096207 : && how == TAG_how::CURRENT_ONLY)
18261 : {
18262 12 : auto_diagnostic_group d;
18263 12 : error ("class template %qD redeclared as non-template", name);
18264 12 : inform (location_of (decl), "previous declaration here");
18265 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18266 12 : return error_mark_node;
18267 12 : }
18268 :
18269 2322125 : if (DECL_CLASS_TEMPLATE_P (decl)
18270 : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18271 : so ignore a template template parameter. */
18272 5774070 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18273 2322122 : decl = DECL_TEMPLATE_RESULT (decl);
18274 :
18275 5774064 : if (TREE_CODE (decl) != TYPE_DECL)
18276 : /* Found not-a-type. */
18277 : return NULL_TREE;
18278 :
18279 : /* Look for invalid nested type:
18280 : class C {
18281 : class C {};
18282 : }; */
18283 5774043 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18284 : {
18285 6 : error ("%qD has the same name as the class in which it is "
18286 : "declared", decl);
18287 6 : return error_mark_node;
18288 : }
18289 :
18290 : /* Two cases we need to consider when deciding if a class
18291 : template is allowed as an elaborated type specifier:
18292 : 1. It is a self reference to its own class.
18293 : 2. It comes with a template header.
18294 :
18295 : For example:
18296 :
18297 : template <class T> class C {
18298 : class C *c1; // DECL_SELF_REFERENCE_P is true
18299 : class D;
18300 : };
18301 : template <class U> class C; // template_header_p is true
18302 : template <class T> class C<T>::D {
18303 : class C *c2; // DECL_SELF_REFERENCE_P is true
18304 : }; */
18305 :
18306 17322111 : tree t = check_elaborated_type_specifier (tag_code, decl,
18307 : template_header_p
18308 11149842 : | DECL_SELF_REFERENCE_P (decl));
18309 2491456 : if (template_header_p && t && CLASS_TYPE_P (t)
18310 8265475 : && (!CLASSTYPE_TEMPLATE_INFO (t)
18311 2491420 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18312 : {
18313 21 : auto_diagnostic_group d;
18314 21 : error ("%qT is not a template", t);
18315 21 : inform (location_of (t), "previous declaration here");
18316 42 : if (TYPE_CLASS_SCOPE_P (t)
18317 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18318 3 : inform (input_location,
18319 : "perhaps you want to explicitly add %<%T::%>",
18320 3 : TYPE_CONTEXT (t));
18321 21 : return error_mark_node;
18322 21 : }
18323 :
18324 : return t;
18325 : }
18326 :
18327 : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18328 : Define the tag as a forward-reference if it is not defined.
18329 :
18330 : If a declaration is given, process it here, and report an error if
18331 : multiple declarations are not identical.
18332 :
18333 : SCOPE is TS_CURRENT when this is also a definition. Only look in
18334 : the current frame for the name (since C++ allows new names in any
18335 : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18336 : declaration. Only look beginning from the current scope outward up
18337 : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18338 :
18339 : TEMPLATE_HEADER_P is true when this declaration is preceded by
18340 : a set of template parameters. */
18341 :
18342 : tree
18343 25584972 : xref_tag (enum tag_types tag_code, tree name,
18344 : TAG_how how, bool template_header_p)
18345 : {
18346 25584972 : enum tree_code code;
18347 25584972 : tree context = NULL_TREE;
18348 :
18349 25584972 : auto_cond_timevar tv (TV_NAME_LOOKUP);
18350 :
18351 25584972 : gcc_assert (identifier_p (name));
18352 :
18353 25584972 : switch (tag_code)
18354 : {
18355 : case record_type:
18356 : case class_type:
18357 : code = RECORD_TYPE;
18358 : break;
18359 : case union_type:
18360 : code = UNION_TYPE;
18361 : break;
18362 : case enum_type:
18363 : code = ENUMERAL_TYPE;
18364 : break;
18365 0 : default:
18366 0 : gcc_unreachable ();
18367 : }
18368 :
18369 : /* In case of anonymous name, xref_tag is only called to
18370 : make type node and push name. Name lookup is not required. */
18371 25584972 : tree t = NULL_TREE;
18372 25584972 : if (!IDENTIFIER_ANON_P (name))
18373 23092851 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18374 :
18375 25584972 : if (t == error_mark_node)
18376 : return error_mark_node;
18377 :
18378 2771251 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18379 1322176 : && template_class_depth (current_class_type)
18380 26173535 : && template_header_p)
18381 : {
18382 347233 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18383 : return t;
18384 :
18385 : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18386 : a definition of this tag. Since, in addition, we are
18387 : currently processing a (member) template declaration of a
18388 : template class, we must be very careful; consider:
18389 :
18390 : template <class X> struct S1
18391 :
18392 : template <class U> struct S2
18393 : {
18394 : template <class V> friend struct S1;
18395 : };
18396 :
18397 : Here, the S2::S1 declaration should not be confused with the
18398 : outer declaration. In particular, the inner version should
18399 : have a template parameter of level 2, not level 1.
18400 :
18401 : On the other hand, when presented with:
18402 :
18403 : template <class T> struct S1
18404 : {
18405 : template <class U> struct S2 {};
18406 : template <class U> friend struct S2;
18407 : };
18408 :
18409 : the friend must find S1::S2 eventually. We accomplish this
18410 : by making sure that the new type we create to represent this
18411 : declaration has the right TYPE_CONTEXT. */
18412 347230 : context = TYPE_CONTEXT (t);
18413 347230 : t = NULL_TREE;
18414 : }
18415 :
18416 25584858 : if (! t)
18417 : {
18418 : /* If no such tag is yet defined, create a forward-reference node
18419 : and record it as the "definition".
18420 : When a real declaration of this type is found,
18421 : the forward-reference will be altered into a real type. */
18422 20191839 : if (code == ENUMERAL_TYPE)
18423 : {
18424 13 : error ("use of enum %q#D without previous declaration", name);
18425 13 : return error_mark_node;
18426 : }
18427 :
18428 20191826 : t = make_class_type (code);
18429 20191826 : TYPE_CONTEXT (t) = context;
18430 20191826 : if (IDENTIFIER_LAMBDA_P (name))
18431 : /* Mark it as a lambda type right now. Our caller will
18432 : correct the value. */
18433 1781654 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18434 20191826 : t = pushtag (name, t, how);
18435 : }
18436 : else
18437 : {
18438 5393019 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18439 : {
18440 : /* Check that we aren't trying to overload a class with different
18441 : constraints. */
18442 2144187 : if (!redeclare_class_template (t, current_template_parms,
18443 : current_template_constraints ()))
18444 60 : return error_mark_node;
18445 : }
18446 3248832 : else if (!processing_template_decl
18447 2915684 : && CLASS_TYPE_P (t)
18448 6138252 : && CLASSTYPE_IS_TEMPLATE (t))
18449 : {
18450 0 : auto_diagnostic_group d;
18451 0 : error ("redeclaration of %qT as a non-template", t);
18452 0 : inform (location_of (t), "previous declaration %qD", t);
18453 0 : return error_mark_node;
18454 0 : }
18455 :
18456 5392959 : if (modules_p ()
18457 5392959 : && how == TAG_how::CURRENT_ONLY)
18458 : {
18459 9251 : tree decl = TYPE_NAME (t);
18460 9251 : if (!module_may_redeclare (decl))
18461 15 : return error_mark_node;
18462 :
18463 9236 : tree not_tmpl = STRIP_TEMPLATE (decl);
18464 9236 : if (DECL_LANG_SPECIFIC (not_tmpl)
18465 4401 : && DECL_MODULE_ATTACH_P (not_tmpl)
18466 54 : && !DECL_MODULE_EXPORT_P (not_tmpl)
18467 9278 : && module_exporting_p ())
18468 : {
18469 6 : auto_diagnostic_group d;
18470 6 : error ("conflicting exporting for declaration %qD", decl);
18471 6 : inform (DECL_SOURCE_LOCATION (decl),
18472 : "previously declared here without exporting");
18473 6 : }
18474 :
18475 9236 : tree maybe_tmpl = decl;
18476 9236 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18477 6860 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18478 :
18479 : /* FIXME: we should do a more precise check for redefinitions
18480 : of a conflicting using-declaration here, as these diagnostics
18481 : are not ideal. */
18482 9236 : if (DECL_LANG_SPECIFIC (decl)
18483 4401 : && DECL_MODULE_IMPORT_P (decl)
18484 9288 : && CP_DECL_CONTEXT (decl) == current_namespace)
18485 : {
18486 : /* Push it into this TU's symbol slot. */
18487 49 : if (maybe_tmpl != decl)
18488 : /* We're in the template parm binding level.
18489 : Pushtag has logic to slide under that, but we're
18490 : not pushing a *new* type. */
18491 20 : push_nested_namespace (CP_DECL_CONTEXT (decl));
18492 :
18493 49 : pushdecl (maybe_tmpl);
18494 49 : if (maybe_tmpl != decl)
18495 20 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
18496 : }
18497 :
18498 9236 : set_instantiating_module (maybe_tmpl);
18499 : }
18500 : }
18501 :
18502 : return t;
18503 25584972 : }
18504 :
18505 : /* Create the binfo hierarchy for REF with (possibly NULL) base list
18506 : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18507 : access_* node, and the TREE_VALUE is the type of the base-class.
18508 : Non-NULL TREE_TYPE indicates virtual inheritance. */
18509 :
18510 : void
18511 74229837 : xref_basetypes (tree ref, tree base_list)
18512 : {
18513 74229837 : tree *basep;
18514 74229837 : tree binfo, base_binfo;
18515 74229837 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18516 74229837 : unsigned max_bases = 0; /* Maximum direct bases. */
18517 74229837 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18518 : /* Highest direct base index with annotations. */
18519 74229837 : unsigned max_annotated_base = 0;
18520 74229837 : int i;
18521 74229837 : tree default_access;
18522 74229837 : tree igo_prev; /* Track Inheritance Graph Order. */
18523 :
18524 74229837 : if (ref == error_mark_node)
18525 12 : return;
18526 :
18527 : /* The base of a derived class is private by default, all others are
18528 : public. */
18529 74229837 : default_access = (TREE_CODE (ref) == RECORD_TYPE
18530 73633779 : && CLASSTYPE_DECLARED_CLASS (ref)
18531 74229837 : ? access_private_node : access_public_node);
18532 :
18533 : /* First, make sure that any templates in base-classes are
18534 : instantiated. This ensures that if we call ourselves recursively
18535 : we do not get confused about which classes are marked and which
18536 : are not. */
18537 74229837 : basep = &base_list;
18538 108501406 : while (*basep)
18539 : {
18540 34271569 : tree basetype = TREE_VALUE (*basep);
18541 :
18542 : /* The dependent_type_p call below should really be dependent_scope_p
18543 : so that we give a hard error about using an incomplete type as a
18544 : base, but we allow it with a pedwarn for backward
18545 : compatibility. */
18546 34271569 : if (processing_template_decl
18547 34271569 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18548 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
18549 : diagnostics::kind::pedwarn);
18550 34271569 : if (!dependent_type_p (basetype)
18551 34271569 : && !complete_type_or_else (basetype, NULL))
18552 : /* An incomplete type. Remove it from the list. */
18553 15 : *basep = TREE_CHAIN (*basep);
18554 : else
18555 : {
18556 34271554 : max_bases++;
18557 34271554 : if (TREE_CODE (TREE_PURPOSE (*basep)) == TREE_LIST)
18558 14 : max_annotated_base = max_bases;
18559 34271554 : if (TREE_TYPE (*basep))
18560 66275 : max_dvbases++;
18561 34271554 : if (CLASS_TYPE_P (basetype))
18562 33504204 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18563 34271554 : basep = &TREE_CHAIN (*basep);
18564 : }
18565 : }
18566 74229837 : max_vbases += max_dvbases;
18567 :
18568 74229837 : TYPE_MARKED_P (ref) = 1;
18569 :
18570 : /* The binfo slot should be empty, unless this is an (ill-formed)
18571 : redefinition. */
18572 74229837 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18573 :
18574 74229837 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18575 :
18576 74229837 : binfo = make_tree_binfo (max_bases);
18577 :
18578 74229837 : TYPE_BINFO (ref) = binfo;
18579 74229837 : BINFO_OFFSET (binfo) = size_zero_node;
18580 74229837 : BINFO_TYPE (binfo) = ref;
18581 :
18582 : /* Apply base-class info set up to the variants of this type. */
18583 74229837 : fixup_type_variants (ref);
18584 :
18585 74229837 : if (max_bases)
18586 : {
18587 33383288 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases + max_annotated_base);
18588 33383288 : BINFO_BASE_ACCESSES (binfo)->quick_grow (max_bases + max_annotated_base);
18589 : /* A C++98 POD cannot have base classes. */
18590 33383288 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18591 :
18592 33383288 : if (TREE_CODE (ref) == UNION_TYPE)
18593 : {
18594 12 : error ("derived union %qT invalid", ref);
18595 12 : return;
18596 : }
18597 : }
18598 :
18599 33383276 : if (max_bases > 1)
18600 703243 : warning (OPT_Wmultiple_inheritance,
18601 : "%qT defined with multiple direct bases", ref);
18602 :
18603 74229825 : if (max_vbases)
18604 : {
18605 : /* An aggregate can't have virtual base classes. */
18606 198222 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18607 :
18608 198222 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
18609 :
18610 198222 : if (max_dvbases)
18611 65068 : warning (OPT_Wvirtual_inheritance,
18612 : "%qT defined with direct virtual base", ref);
18613 : }
18614 :
18615 108501367 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18616 : {
18617 34271542 : tree access = TREE_PURPOSE (base_list);
18618 34271542 : tree annotations = NULL_TREE;
18619 34271542 : if (TREE_CODE (access) == TREE_LIST)
18620 : {
18621 14 : annotations = TREE_VALUE (access);
18622 14 : access = TREE_PURPOSE (access);
18623 39 : for (tree *d = &annotations; *d; )
18624 : {
18625 25 : if (annotation_p (*d))
18626 : {
18627 25 : tree name = get_attribute_name (*d);
18628 25 : tree args = TREE_VALUE (*d);
18629 25 : const attribute_spec *as
18630 25 : = lookup_attribute_spec (TREE_PURPOSE (*d));
18631 25 : bool no_add_attrs = false;
18632 25 : as->handler (&binfo, name, args, 0, &no_add_attrs);
18633 25 : if (no_add_attrs)
18634 : {
18635 1 : *d = TREE_CHAIN (*d);
18636 1 : continue;
18637 : }
18638 : }
18639 24 : d = &TREE_CHAIN (*d);
18640 : }
18641 : }
18642 34271542 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18643 34271542 : tree basetype = TREE_VALUE (base_list);
18644 :
18645 34271542 : if (access == access_default_node)
18646 4347049 : access = default_access;
18647 :
18648 : /* Before C++17, an aggregate cannot have base classes. In C++17, an
18649 : aggregate can't have virtual, private, or protected base classes. */
18650 34271542 : if (cxx_dialect < cxx17
18651 34061051 : || access != access_public_node
18652 33082580 : || via_virtual)
18653 1252187 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18654 :
18655 34271542 : if (PACK_EXPANSION_P (basetype))
18656 28159 : basetype = PACK_EXPANSION_PATTERN (basetype);
18657 34271542 : if (TREE_CODE (basetype) == TYPE_DECL)
18658 0 : basetype = TREE_TYPE (basetype);
18659 34271542 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18660 : {
18661 24 : error ("base type %qT fails to be a struct or class type",
18662 : basetype);
18663 24 : goto dropped_base;
18664 : }
18665 :
18666 34271518 : base_binfo = NULL_TREE;
18667 34271518 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18668 : {
18669 27582254 : base_binfo = TYPE_BINFO (basetype);
18670 : /* The original basetype could have been a typedef'd type. */
18671 27582254 : basetype = BINFO_TYPE (base_binfo);
18672 :
18673 : /* Inherit flags from the base. */
18674 82746762 : TYPE_HAS_NEW_OPERATOR (ref)
18675 27582254 : |= TYPE_HAS_NEW_OPERATOR (basetype);
18676 82746762 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18677 27582254 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18678 27582254 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18679 27582254 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18680 82746762 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
18681 27582254 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18682 82746762 : CLASSTYPE_REPEATED_BASE_P (ref)
18683 27582254 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
18684 : }
18685 :
18686 : /* We must do this test after we've seen through a typedef
18687 : type. */
18688 34271518 : if (TYPE_MARKED_P (basetype))
18689 : {
18690 18 : if (basetype == ref)
18691 0 : error ("recursive type %qT undefined", basetype);
18692 : else
18693 18 : error ("duplicate base type %qT invalid", basetype);
18694 18 : goto dropped_base;
18695 : }
18696 :
18697 34271500 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18698 : /* Regenerate the pack expansion for the bases. */
18699 28159 : basetype = make_pack_expansion (basetype);
18700 :
18701 34271500 : TYPE_MARKED_P (basetype) = 1;
18702 :
18703 34271500 : base_binfo = copy_binfo (base_binfo, basetype, ref,
18704 : &igo_prev, via_virtual);
18705 34271500 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
18706 34205240 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
18707 :
18708 34271500 : unsigned len;
18709 34271500 : len = BINFO_N_BASE_BINFOS (binfo);
18710 34271500 : BINFO_BASE_APPEND (binfo, base_binfo);
18711 34271500 : BINFO_BASE_ACCESS (binfo, len) = access;
18712 34271500 : if (len < max_annotated_base)
18713 17 : BINFO_BASE_ACCESS (binfo, max_bases + len) = annotations;
18714 34271500 : continue;
18715 :
18716 42 : dropped_base:
18717 : /* Update max_vbases to reflect the reality that we are dropping
18718 : this base: if it reaches zero we want to undo the vec_alloc
18719 : above to avoid inconsistencies during error-recovery: eg, in
18720 : build_special_member_call, CLASSTYPE_VBASECLASSES non null
18721 : and vtt null (c++/27952). */
18722 42 : if (via_virtual)
18723 12 : max_vbases--;
18724 42 : if (CLASS_TYPE_P (basetype))
18725 30 : max_vbases
18726 33 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18727 34271500 : }
18728 :
18729 74229825 : unsigned len = BINFO_N_BASE_BINFOS (binfo);
18730 74229825 : if (len < max_bases)
18731 : {
18732 42 : if (len && max_annotated_base)
18733 0 : memmove (&BINFO_BASE_ACCESS (binfo, len),
18734 0 : &BINFO_BASE_ACCESS (binfo, max_bases),
18735 0 : MIN (max_annotated_base, len) * sizeof (tree));
18736 42 : BINFO_BASE_ACCESSES (binfo)->truncate (len + MIN (max_annotated_base,
18737 : len));
18738 : }
18739 :
18740 74229825 : if (CLASSTYPE_VBASECLASSES (ref)
18741 74229825 : && max_vbases == 0)
18742 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
18743 :
18744 74428035 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18745 : /* If we didn't get max_vbases vbases, we must have shared at
18746 : least one of them, and are therefore diamond shaped. */
18747 21442 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18748 :
18749 : /* Unmark all the types. */
18750 108501325 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18751 34271500 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18752 74229825 : TYPE_MARKED_P (ref) = 0;
18753 :
18754 : /* Now see if we have a repeated base type. */
18755 74229825 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
18756 : {
18757 186815959 : for (base_binfo = binfo; base_binfo;
18758 112586568 : base_binfo = TREE_CHAIN (base_binfo))
18759 : {
18760 112587285 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18761 : {
18762 717 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18763 717 : break;
18764 : }
18765 112586568 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18766 : }
18767 186815959 : for (base_binfo = binfo; base_binfo;
18768 112586568 : base_binfo = TREE_CHAIN (base_binfo))
18769 112587285 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18770 112586568 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18771 : else
18772 : break;
18773 : }
18774 : }
18775 :
18776 :
18777 : /* Copies the enum-related properties from type SRC to type DST.
18778 : Used with the underlying type of an enum and the enum itself. */
18779 : static void
18780 1982476 : copy_type_enum (tree dst, tree src)
18781 : {
18782 1982476 : tree t;
18783 3964970 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
18784 : {
18785 1982494 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
18786 1982494 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
18787 1982494 : TYPE_SIZE (t) = TYPE_SIZE (src);
18788 1982494 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
18789 1982494 : SET_TYPE_MODE (dst, TYPE_MODE (src));
18790 1982494 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
18791 1982494 : unsigned valign = TYPE_ALIGN (src);
18792 1982494 : if (TYPE_USER_ALIGN (t))
18793 12 : valign = MAX (valign, TYPE_ALIGN (t));
18794 : else
18795 1982482 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
18796 1982494 : SET_TYPE_ALIGN (t, valign);
18797 1982494 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
18798 : }
18799 1982476 : }
18800 :
18801 : /* Begin compiling the definition of an enumeration type.
18802 : NAME is its name,
18803 :
18804 : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
18805 :
18806 : UNDERLYING_TYPE is the type that will be used as the storage for
18807 : the enumeration type. This should be NULL_TREE if no storage type
18808 : was specified.
18809 :
18810 : ATTRIBUTES are any attributes specified after the enum-key.
18811 :
18812 : SCOPED_ENUM_P is true if this is a scoped enumeration type.
18813 :
18814 : if IS_NEW is not NULL, gets TRUE iff a new type is created.
18815 :
18816 : Returns the type object, as yet incomplete.
18817 : Also records info about it so that build_enumerator
18818 : may be used to declare the individual values as they are read. */
18819 :
18820 : tree
18821 2323872 : start_enum (tree name, tree enumtype, tree underlying_type,
18822 : tree attributes, bool scoped_enum_p, bool *is_new)
18823 : {
18824 2323872 : tree prevtype = NULL_TREE;
18825 2323872 : gcc_assert (identifier_p (name));
18826 :
18827 2323872 : if (is_new)
18828 1816875 : *is_new = false;
18829 : /* [C++0x dcl.enum]p5:
18830 :
18831 : If not explicitly specified, the underlying type of a scoped
18832 : enumeration type is int. */
18833 2323872 : if (!underlying_type && scoped_enum_p)
18834 166513 : underlying_type = integer_type_node;
18835 :
18836 2323872 : if (underlying_type)
18837 573927 : underlying_type = cv_unqualified (underlying_type);
18838 :
18839 : /* If this is the real definition for a previous forward reference,
18840 : fill in the contents in the same object that used to be the
18841 : forward reference. */
18842 2323872 : if (!enumtype)
18843 2323742 : enumtype = lookup_and_check_tag (enum_type, name,
18844 : /*tag_scope=*/TAG_how::CURRENT_ONLY,
18845 : /*template_header_p=*/false);
18846 :
18847 : /* In case of a template_decl, the only check that should be deferred
18848 : to instantiation time is the comparison of underlying types. */
18849 2323872 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
18850 : {
18851 : /* Attempt to set the declaring module. */
18852 33822 : if (modules_p ())
18853 : {
18854 172 : tree decl = TYPE_NAME (enumtype);
18855 172 : if (!module_may_redeclare (decl))
18856 3 : enumtype = error_mark_node;
18857 : else
18858 169 : set_instantiating_module (decl);
18859 : }
18860 :
18861 33822 : if (enumtype == error_mark_node)
18862 : ;
18863 34020 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
18864 : {
18865 6 : auto_diagnostic_group d;
18866 6 : error_at (input_location, "scoped/unscoped mismatch "
18867 : "in enum %q#T", enumtype);
18868 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18869 : "previous definition here");
18870 6 : enumtype = error_mark_node;
18871 6 : }
18872 33813 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
18873 : {
18874 0 : auto_diagnostic_group d;
18875 0 : error_at (input_location, "underlying type mismatch "
18876 : "in enum %q#T", enumtype);
18877 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18878 : "previous definition here");
18879 0 : enumtype = error_mark_node;
18880 0 : }
18881 33798 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
18882 67611 : && !same_type_p (underlying_type,
18883 : ENUM_UNDERLYING_TYPE (enumtype)))
18884 : {
18885 26 : auto_diagnostic_group d;
18886 26 : error_at (input_location, "different underlying type "
18887 : "in enum %q#T", enumtype);
18888 26 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18889 : "previous definition here");
18890 26 : underlying_type = NULL_TREE;
18891 26 : }
18892 : }
18893 :
18894 2323872 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
18895 33813 : || processing_template_decl)
18896 : {
18897 : /* In case of error, make a dummy enum to allow parsing to
18898 : continue. */
18899 2290149 : if (enumtype == error_mark_node)
18900 : {
18901 9 : name = make_anon_name ();
18902 9 : enumtype = NULL_TREE;
18903 : }
18904 :
18905 : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
18906 : of an opaque enum, or an opaque enum of an already defined
18907 : enumeration (C++11).
18908 : In any other case, it'll be NULL_TREE. */
18909 2290149 : if (!enumtype)
18910 : {
18911 2290056 : if (is_new)
18912 1783062 : *is_new = true;
18913 : }
18914 2290149 : prevtype = enumtype;
18915 :
18916 : /* Do not push the decl more than once. */
18917 2290149 : if (!enumtype
18918 93 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
18919 : {
18920 2290059 : enumtype = cxx_make_type (ENUMERAL_TYPE);
18921 2290059 : enumtype = pushtag (name, enumtype);
18922 :
18923 2290059 : if (enumtype != error_mark_node)
18924 : {
18925 : /* The enum is considered opaque until the opening '{' of the
18926 : enumerator list. */
18927 2290041 : SET_OPAQUE_ENUM_P (enumtype, true);
18928 2290041 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
18929 :
18930 : /* std::byte aliases anything. */
18931 2290041 : if (TYPE_CONTEXT (enumtype) == std_node
18932 2634533 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
18933 12553 : TYPE_ALIAS_SET (enumtype) = 0;
18934 : }
18935 : }
18936 : else
18937 90 : enumtype = xref_tag (enum_type, name);
18938 :
18939 2290149 : if (enumtype == error_mark_node)
18940 : return error_mark_node;
18941 : }
18942 :
18943 2323854 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
18944 :
18945 2323854 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18946 :
18947 2323854 : if (underlying_type)
18948 : {
18949 573898 : if (ENUM_UNDERLYING_TYPE (enumtype))
18950 : /* We already checked that it matches, don't change it to a different
18951 : typedef variant. */;
18952 540126 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
18953 : {
18954 540060 : copy_type_enum (enumtype, underlying_type);
18955 540060 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18956 : }
18957 66 : else if (dependent_type_p (underlying_type))
18958 53 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18959 : else
18960 : {
18961 13 : error ("underlying type %qT of %qT must be an integral type",
18962 : underlying_type, enumtype);
18963 13 : copy_type_enum (enumtype, integer_type_node);
18964 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
18965 : }
18966 : }
18967 :
18968 : /* If into a template class, the returned enum is always the first
18969 : declaration (opaque or not) seen. This way all the references to
18970 : this type will be to the same declaration. The following ones are used
18971 : only to check for definition errors. */
18972 2323854 : if (prevtype && processing_template_decl)
18973 : return prevtype;
18974 : else
18975 2323764 : return enumtype;
18976 : }
18977 :
18978 : /* Returns true if TYPE is an enum that uses an enumerator name for
18979 : linkage purposes at namespace scope. The term is defined in [dcl.enum]/12
18980 : for all enums, not just those at namespace scope, but for backward ABI
18981 : compatibility we want to treat those not at namespace scope the old way
18982 : and e.g. mangle the class scope ones based on their position within the
18983 : class rather than the first enumerator. */
18984 :
18985 : bool
18986 430632103 : enum_with_enumerator_for_linkage_p (tree type)
18987 : {
18988 430632103 : return (cxx_dialect >= cxx20
18989 426207496 : && UNSCOPED_ENUM_P (type)
18990 19678448 : && TYPE_ANON_P (type)
18991 3076691 : && TYPE_VALUES (type)
18992 432534999 : && TYPE_NAMESPACE_SCOPE_P (type));
18993 : }
18994 :
18995 : /* After processing and defining all the values of an enumeration type,
18996 : install their decls in the enumeration type.
18997 : ENUMTYPE is the type object. */
18998 :
18999 : void
19000 2223454 : finish_enum_value_list (tree enumtype)
19001 : {
19002 2223454 : tree values;
19003 2223454 : tree underlying_type;
19004 2223454 : tree decl;
19005 2223454 : tree value;
19006 2223454 : tree minnode, maxnode;
19007 2223454 : tree t;
19008 :
19009 2223454 : ENUM_BEING_DEFINED_P (enumtype) = 0;
19010 :
19011 2223454 : bool fixed_underlying_type_p
19012 2223454 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
19013 :
19014 : /* We built up the VALUES in reverse order. */
19015 2223454 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
19016 :
19017 : /* For an enum defined in a template, just set the type of the values;
19018 : all further processing is postponed until the template is
19019 : instantiated. We need to set the type so that tsubst of a CONST_DECL
19020 : works. */
19021 2223454 : if (processing_template_decl)
19022 : {
19023 426832 : for (values = TYPE_VALUES (enumtype);
19024 761662 : values;
19025 426832 : values = TREE_CHAIN (values))
19026 426832 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
19027 : return;
19028 : }
19029 :
19030 : /* Determine the minimum and maximum values of the enumerators. */
19031 1888624 : if (TYPE_VALUES (enumtype))
19032 : {
19033 : minnode = maxnode = NULL_TREE;
19034 :
19035 11072112 : for (values = TYPE_VALUES (enumtype);
19036 12926264 : values;
19037 11072112 : values = TREE_CHAIN (values))
19038 : {
19039 11072112 : decl = TREE_VALUE (values);
19040 :
19041 : /* [dcl.enum]: Following the closing brace of an enum-specifier,
19042 : each enumerator has the type of its enumeration. Prior to the
19043 : closing brace, the type of each enumerator is the type of its
19044 : initializing value. */
19045 11072112 : TREE_TYPE (decl) = enumtype;
19046 :
19047 : /* Update the minimum and maximum values, if appropriate. */
19048 11072112 : value = DECL_INITIAL (decl);
19049 11072112 : if (TREE_CODE (value) != INTEGER_CST)
19050 4 : value = integer_zero_node;
19051 : /* Figure out what the minimum and maximum values of the
19052 : enumerators are. */
19053 11072112 : if (!minnode)
19054 : minnode = maxnode = value;
19055 9217960 : else if (tree_int_cst_lt (maxnode, value))
19056 : maxnode = value;
19057 1432177 : else if (tree_int_cst_lt (value, minnode))
19058 161262 : minnode = value;
19059 : }
19060 : }
19061 : else
19062 : /* [dcl.enum]
19063 :
19064 : If the enumerator-list is empty, the underlying type is as if
19065 : the enumeration had a single enumerator with value 0. */
19066 34472 : minnode = maxnode = integer_zero_node;
19067 :
19068 1888624 : if (!fixed_underlying_type_p)
19069 : {
19070 : /* Compute the number of bits require to represent all values of the
19071 : enumeration. We must do this before the type of MINNODE and
19072 : MAXNODE are transformed, since tree_int_cst_min_precision relies
19073 : on the TREE_TYPE of the value it is passed. */
19074 1442403 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
19075 1442403 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
19076 1442403 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
19077 1442403 : int precision = MAX (lowprec, highprec);
19078 1442403 : unsigned int itk;
19079 1442403 : bool use_short_enum;
19080 :
19081 : /* Determine the underlying type of the enumeration.
19082 :
19083 : [dcl.enum]
19084 :
19085 : The underlying type of an enumeration is an integral type that
19086 : can represent all the enumerator values defined in the
19087 : enumeration. It is implementation-defined which integral type is
19088 : used as the underlying type for an enumeration except that the
19089 : underlying type shall not be larger than int unless the value of
19090 : an enumerator cannot fit in an int or unsigned int.
19091 :
19092 : We use "int" or an "unsigned int" as the underlying type, even if
19093 : a smaller integral type would work, unless the user has
19094 : explicitly requested that we use the smallest possible type. The
19095 : user can request that for all enumerations with a command line
19096 : flag, or for just one enumeration with an attribute. */
19097 :
19098 2884806 : use_short_enum = flag_short_enums
19099 1442403 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
19100 :
19101 : /* If the precision of the type was specified with an attribute and it
19102 : was too small, give an error. Otherwise, use it. */
19103 1442403 : if (TYPE_PRECISION (enumtype))
19104 : {
19105 11 : if (precision > TYPE_PRECISION (enumtype))
19106 0 : error ("specified mode too small for enumerated values");
19107 : else
19108 : {
19109 11 : use_short_enum = true;
19110 11 : precision = TYPE_PRECISION (enumtype);
19111 : }
19112 : }
19113 :
19114 2807906 : for (itk = (use_short_enum ? itk_char : itk_int);
19115 2807906 : itk != itk_none;
19116 : itk++)
19117 : {
19118 2807903 : underlying_type = integer_types[itk];
19119 2807903 : if (underlying_type != NULL_TREE
19120 2807885 : && TYPE_PRECISION (underlying_type) >= precision
19121 5613727 : && TYPE_SIGN (underlying_type) == sgn)
19122 : break;
19123 : }
19124 1442403 : if (itk == itk_none)
19125 : {
19126 : /* DR 377
19127 :
19128 : IF no integral type can represent all the enumerator values, the
19129 : enumeration is ill-formed. */
19130 3 : error ("no integral type can represent all of the enumerator values "
19131 : "for %qT", enumtype);
19132 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
19133 3 : underlying_type = integer_types[itk_unsigned_long_long];
19134 : }
19135 :
19136 : /* [dcl.enum]
19137 :
19138 : The value of sizeof() applied to an enumeration type, an object
19139 : of an enumeration type, or an enumerator, is the value of sizeof()
19140 : applied to the underlying type. */
19141 1442403 : copy_type_enum (enumtype, underlying_type);
19142 :
19143 : /* Compute the minimum and maximum values for the type.
19144 :
19145 : [dcl.enum]
19146 :
19147 : For an enumeration where emin is the smallest enumerator and emax
19148 : is the largest, the values of the enumeration are the values of the
19149 : underlying type in the range bmin to bmax, where bmin and bmax are,
19150 : respectively, the smallest and largest values of the smallest bit-
19151 : field that can store emin and emax. */
19152 :
19153 : /* The middle-end currently assumes that types with TYPE_PRECISION
19154 : narrower than their underlying type are suitably zero or sign
19155 : extended to fill their mode. Similarly, it assumes that the front
19156 : end assures that a value of a particular type must be within
19157 : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
19158 :
19159 : We used to set these fields based on bmin and bmax, but that led
19160 : to invalid assumptions like optimizing away bounds checking. So
19161 : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
19162 : TYPE_MAX_VALUE to the values for the mode above and only restrict
19163 : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
19164 1442403 : ENUM_UNDERLYING_TYPE (enumtype)
19165 1442403 : = build_distinct_type_copy (underlying_type);
19166 1442403 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
19167 1442403 : set_min_and_max_values_for_integral_type
19168 1442403 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
19169 :
19170 : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
19171 1442403 : if (flag_strict_enums)
19172 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
19173 :
19174 1442403 : if (use_short_enum)
19175 : {
19176 122 : TYPE_PACKED (enumtype) = use_short_enum;
19177 122 : fixup_attribute_variants (enumtype);
19178 : }
19179 : }
19180 : else
19181 446221 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
19182 :
19183 : /* If the enum is exported, mark the consts too. */
19184 1888624 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
19185 1516129 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
19186 1889207 : && at_namespace_scope_p ());
19187 :
19188 : /* Convert each of the enumerators to the type of the underlying
19189 : type of the enumeration. */
19190 12960736 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
19191 : {
19192 11072112 : decl = TREE_VALUE (values);
19193 11072112 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
19194 11072112 : if (fixed_underlying_type_p)
19195 : /* If the enumeration type has a fixed underlying type, we
19196 : already checked all of the enumerator values. */
19197 3792504 : value = DECL_INITIAL (decl);
19198 : else
19199 7279608 : value = perform_implicit_conversion (underlying_type,
19200 7279608 : DECL_INITIAL (decl),
19201 : tf_warning_or_error);
19202 : /* Do not clobber shared ints. But do share identical enumerators. */
19203 11072112 : value = fold_convert (enumtype, value);
19204 :
19205 11072112 : DECL_INITIAL (decl) = value;
19206 11072112 : if (export_p)
19207 3570 : DECL_MODULE_EXPORT_P (decl) = true;
19208 11072112 : }
19209 :
19210 : /* Fix up all variant types of this enum type. */
19211 3777277 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
19212 1888653 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
19213 :
19214 1888624 : if (at_class_scope_p ()
19215 1038922 : && COMPLETE_TYPE_P (current_class_type)
19216 1888685 : && UNSCOPED_ENUM_P (enumtype))
19217 : {
19218 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
19219 : /* TYPE_FIELDS needs fixup. */
19220 42 : fixup_type_variants (current_class_type);
19221 : }
19222 :
19223 : /* P2115: An unnamed enum uses the name of its first enumerator for
19224 : linkage purposes; reset the type linkage if that is the case. */
19225 1888624 : if (enum_with_enumerator_for_linkage_p (enumtype))
19226 325098 : reset_type_linkage (enumtype);
19227 :
19228 : /* Finish debugging output for this type. */
19229 1888624 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
19230 :
19231 : /* Each enumerator now has the type of its enumeration. Clear the cache
19232 : so that this change in types doesn't confuse us later on. */
19233 1888624 : clear_cv_and_fold_caches ();
19234 : }
19235 :
19236 : /* Finishes the enum type. This is called only the first time an
19237 : enumeration is seen, be it opaque or odinary.
19238 : ENUMTYPE is the type object. */
19239 :
19240 : void
19241 2213039 : finish_enum (tree enumtype)
19242 : {
19243 2213039 : if (processing_template_decl)
19244 : {
19245 334858 : if (at_function_scope_p ())
19246 9113 : add_stmt (build_min (TAG_DEFN, enumtype));
19247 334858 : return;
19248 : }
19249 :
19250 : /* If this is a forward declaration, there should not be any variants,
19251 : though we can get a variant in the middle of an enum-specifier with
19252 : wacky code like 'enum E { e = sizeof(const E*) };' */
19253 3756362 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19254 : && (TYPE_VALUES (enumtype)
19255 : || !TYPE_NEXT_VARIANT (enumtype)));
19256 : }
19257 :
19258 : /* Build and install a CONST_DECL for an enumeration constant of the
19259 : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19260 : Apply ATTRIBUTES if available. LOC is the location of NAME.
19261 : Assignment of sequential values by default is handled here. */
19262 :
19263 : tree
19264 11498921 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19265 : location_t loc)
19266 : {
19267 11498921 : tree decl;
19268 11498921 : tree context;
19269 11498921 : tree type;
19270 :
19271 : /* scalar_constant_value will pull out this expression, so make sure
19272 : it's folded as appropriate.
19273 :
19274 : Creating a TARGET_EXPR in a template breaks when substituting, and
19275 : here we would create it for instance when using a class prvalue with
19276 : a user-defined conversion function. So don't use such a tree. We
19277 : instantiate VALUE here to get errors about bad enumerators even in
19278 : a template that does not get instantiated. */
19279 11498921 : if (processing_template_decl)
19280 426832 : value = maybe_fold_non_dependent_expr (value);
19281 :
19282 : /* If the VALUE was erroneous, pretend it wasn't there; that will
19283 : result in the enum being assigned the next value in sequence. */
19284 11498921 : if (value == error_mark_node)
19285 : value = NULL_TREE;
19286 :
19287 : /* Remove no-op casts from the value. */
19288 11498883 : if (value)
19289 8588676 : STRIP_TYPE_NOPS (value);
19290 :
19291 11498921 : if (! processing_template_decl)
19292 : {
19293 : /* Validate and default VALUE. */
19294 11072089 : if (value != NULL_TREE)
19295 : {
19296 4958148 : if (!ENUM_UNDERLYING_TYPE (enumtype))
19297 : {
19298 2607262 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19299 : value, true);
19300 2607262 : if (tmp_value)
19301 4958148 : value = tmp_value;
19302 : }
19303 2350886 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19304 : (TREE_TYPE (value)))
19305 39 : value = perform_implicit_conversion_flags
19306 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19307 : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19308 :
19309 4958148 : if (value == error_mark_node)
19310 : value = NULL_TREE;
19311 :
19312 4958127 : if (value != NULL_TREE)
19313 : {
19314 4958127 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19315 : (TREE_TYPE (value)))
19316 : {
19317 17 : error_at (cp_expr_loc_or_input_loc (value),
19318 : "enumerator value for %qD must have integral or "
19319 : "unscoped enumeration type", name);
19320 17 : value = NULL_TREE;
19321 : }
19322 : else
19323 : {
19324 4958110 : value = cxx_constant_value (value);
19325 :
19326 4958110 : if (TREE_CODE (value) != INTEGER_CST)
19327 : {
19328 47 : error ("enumerator value for %qD is not an integer "
19329 : "constant", name);
19330 47 : value = NULL_TREE;
19331 : }
19332 : }
19333 : }
19334 : }
19335 :
19336 : /* Default based on previous value. */
19337 64 : if (value == NULL_TREE)
19338 : {
19339 6114026 : if (TYPE_VALUES (enumtype))
19340 : {
19341 5590051 : tree prev_value;
19342 :
19343 : /* C++03 7.2/4: If no initializer is specified for the first
19344 : enumerator, the type is an unspecified integral
19345 : type. Otherwise the type is the same as the type of the
19346 : initializing value of the preceding enumerator unless the
19347 : incremented value is not representable in that type, in
19348 : which case the type is an unspecified integral type
19349 : sufficient to contain the incremented value. */
19350 5590051 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19351 5590051 : if (TREE_CODE (prev_value) != INTEGER_CST)
19352 2 : value = error_mark_node;
19353 : else
19354 : {
19355 5590049 : wi::overflow_type overflowed;
19356 5590049 : tree type = TREE_TYPE (prev_value);
19357 5590049 : signop sgn = TYPE_SIGN (type);
19358 5590049 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
19359 5590049 : &overflowed);
19360 5590049 : if (!overflowed)
19361 : {
19362 5590049 : bool pos = !wi::neg_p (wi, sgn);
19363 5590049 : if (!wi::fits_to_tree_p (wi, type))
19364 : {
19365 : unsigned int itk;
19366 53 : for (itk = itk_int; itk != itk_none; itk++)
19367 : {
19368 53 : type = integer_types[itk];
19369 53 : if (type != NULL_TREE
19370 53 : && (pos || !TYPE_UNSIGNED (type))
19371 106 : && wi::fits_to_tree_p (wi, type))
19372 : break;
19373 : }
19374 17 : if (type && cxx_dialect < cxx11
19375 6 : && itk > itk_unsigned_long)
19376 3 : pedwarn (input_location, OPT_Wlong_long,
19377 : pos ? G_("\
19378 : incremented enumerator value is too large for %<unsigned long%>") : G_("\
19379 : incremented enumerator value is too large for %<long%>"));
19380 : }
19381 5590049 : if (type == NULL_TREE)
19382 0 : overflowed = wi::OVF_UNKNOWN;
19383 : else
19384 5590049 : value = wide_int_to_tree (type, wi);
19385 : }
19386 :
19387 5590049 : if (overflowed)
19388 : {
19389 0 : error ("overflow in enumeration values at %qD", name);
19390 0 : value = error_mark_node;
19391 : }
19392 5590049 : }
19393 : }
19394 : else
19395 523975 : value = integer_zero_node;
19396 : }
19397 :
19398 : /* Remove no-op casts from the value. */
19399 11072089 : STRIP_TYPE_NOPS (value);
19400 :
19401 : /* If the underlying type of the enum is fixed, check whether
19402 : the enumerator values fits in the underlying type. If it
19403 : does not fit, the program is ill-formed [C++0x dcl.enum]. */
19404 11072089 : if (ENUM_UNDERLYING_TYPE (enumtype)
19405 : && value
19406 11072089 : && TREE_CODE (value) == INTEGER_CST)
19407 : {
19408 3792479 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19409 9 : error ("enumerator value %qE is outside the range of underlying "
19410 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19411 :
19412 : /* Convert the value to the appropriate type. */
19413 3792479 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19414 : }
19415 : }
19416 :
19417 : /* C++ associates enums with global, function, or class declarations. */
19418 11498921 : context = current_scope ();
19419 :
19420 : /* Build the actual enumeration constant. Note that the enumeration
19421 : constants have the underlying type of the enum (if it is fixed)
19422 : or the type of their initializer (if the underlying type of the
19423 : enum is not fixed):
19424 :
19425 : [ C++0x dcl.enum ]
19426 :
19427 : If the underlying type is fixed, the type of each enumerator
19428 : prior to the closing brace is the underlying type; if the
19429 : initializing value of an enumerator cannot be represented by
19430 : the underlying type, the program is ill-formed. If the
19431 : underlying type is not fixed, the type of each enumerator is
19432 : the type of its initializing value.
19433 :
19434 : If the underlying type is not fixed, it will be computed by
19435 : finish_enum and we will reset the type of this enumerator. Of
19436 : course, if we're processing a template, there may be no value. */
19437 11498921 : type = value ? TREE_TYPE (value) : NULL_TREE;
19438 :
19439 11498921 : decl = build_decl (loc, CONST_DECL, name, type);
19440 :
19441 11498921 : DECL_CONTEXT (decl) = enumtype;
19442 11498921 : TREE_CONSTANT (decl) = 1;
19443 11498921 : TREE_READONLY (decl) = 1;
19444 11498921 : DECL_INITIAL (decl) = value;
19445 :
19446 11498921 : if (attributes)
19447 120220 : cplus_decl_attributes (&decl, attributes, 0);
19448 :
19449 11498921 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19450 : {
19451 : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19452 : on the TYPE_FIELDS list for `S'. (That's so that you can say
19453 : things like `S::i' later.) */
19454 :
19455 : /* The enumerator may be getting declared outside of its enclosing
19456 : class, like so:
19457 :
19458 : class S { public: enum E : int; }; enum S::E : int { i = 7; };
19459 :
19460 : For which case we need to make sure that the access of `S::i'
19461 : matches the access of `S::E'. */
19462 2052037 : auto cas = make_temp_override (current_access_specifier);
19463 2052037 : set_current_access_from_decl (TYPE_NAME (enumtype));
19464 2052037 : finish_member_declaration (decl);
19465 2052037 : }
19466 : else
19467 9446884 : pushdecl (decl);
19468 :
19469 : /* Add this enumeration constant to the list for this type. */
19470 11498921 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19471 :
19472 11498921 : return decl;
19473 : }
19474 :
19475 : /* Look for an enumerator with the given NAME within the enumeration
19476 : type ENUMTYPE. This routine is used primarily for qualified name
19477 : lookup into an enumerator in C++0x, e.g.,
19478 :
19479 : enum class Color { Red, Green, Blue };
19480 :
19481 : Color color = Color::Red;
19482 :
19483 : Returns the value corresponding to the enumerator, or
19484 : NULL_TREE if no such enumerator was found. */
19485 : tree
19486 26835451 : lookup_enumerator (tree enumtype, tree name)
19487 : {
19488 26835451 : tree e;
19489 26835451 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19490 :
19491 26835451 : e = purpose_member (name, TYPE_VALUES (enumtype));
19492 53670805 : return e? TREE_VALUE (e) : NULL_TREE;
19493 : }
19494 :
19495 : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19496 :
19497 : tree
19498 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
19499 : vec<string_int_pair> *values)
19500 : {
19501 0 : location_t saved_loc = input_location;
19502 0 : input_location = loc;
19503 :
19504 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19505 : NULL_TREE, false, NULL);
19506 0 : if (!OPAQUE_ENUM_P (enumtype))
19507 : {
19508 0 : auto_diagnostic_group d;
19509 0 : error_at (loc, "multiple definition of %q#T", enumtype);
19510 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19511 : "previous definition here");
19512 0 : return enumtype;
19513 0 : }
19514 0 : SET_OPAQUE_ENUM_P (enumtype, false);
19515 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19516 :
19517 0 : for (const string_int_pair &value : values)
19518 0 : build_enumerator (get_identifier (value.first),
19519 0 : build_int_cst (integer_type_node, value.second),
19520 : enumtype, NULL_TREE, loc);
19521 :
19522 0 : finish_enum_value_list (enumtype);
19523 0 : finish_enum (enumtype);
19524 :
19525 0 : input_location = saved_loc;
19526 0 : return enumtype;
19527 : }
19528 :
19529 : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19530 :
19531 : tree
19532 0 : cxx_simulate_record_decl (location_t loc, const char *name,
19533 : array_slice<const tree> fields)
19534 : {
19535 0 : iloc_sentinel ils (loc);
19536 :
19537 0 : tree ident = get_identifier (name);
19538 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
19539 0 : if (type != error_mark_node
19540 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19541 : {
19542 0 : error ("redefinition of %q#T", type);
19543 0 : type = error_mark_node;
19544 : }
19545 0 : if (type == error_mark_node)
19546 0 : return lhd_simulate_record_decl (loc, name, fields);
19547 :
19548 0 : xref_basetypes (type, NULL_TREE);
19549 0 : type = begin_class_definition (type);
19550 0 : if (type == error_mark_node)
19551 0 : return lhd_simulate_record_decl (loc, name, fields);
19552 :
19553 0 : for (tree field : fields)
19554 0 : finish_member_declaration (field);
19555 :
19556 0 : type = finish_struct (type, NULL_TREE);
19557 :
19558 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
19559 0 : set_underlying_type (decl);
19560 0 : lang_hooks.decls.pushdecl (decl);
19561 :
19562 0 : return type;
19563 0 : }
19564 :
19565 : /* We're defining DECL. Make sure that its type is OK. */
19566 :
19567 : static void
19568 165485931 : check_function_type (tree decl, tree current_function_parms)
19569 : {
19570 165485931 : tree fntype = TREE_TYPE (decl);
19571 165485931 : tree return_type = complete_type (TREE_TYPE (fntype));
19572 :
19573 : /* In a function definition, arg types must be complete. */
19574 165485931 : require_complete_types_for_parms (current_function_parms);
19575 :
19576 165485931 : if (dependent_type_p (return_type)
19577 165485931 : || type_uses_auto (return_type))
19578 50722376 : return;
19579 114763555 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19580 : {
19581 12 : tree args = TYPE_ARG_TYPES (fntype);
19582 :
19583 12 : error ("return type %q#T is incomplete", return_type);
19584 :
19585 : /* Make it return void instead. */
19586 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
19587 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19588 : void_type_node,
19589 3 : TREE_CHAIN (args));
19590 : else
19591 18 : fntype = build_function_type (void_type_node, args,
19592 9 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19593 12 : fntype = (cp_build_type_attribute_variant
19594 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19595 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19596 12 : TREE_TYPE (decl) = fntype;
19597 : }
19598 : else
19599 : {
19600 114763543 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
19601 114763543 : maybe_warn_parm_abi (TREE_TYPE (fntype),
19602 114763543 : DECL_SOURCE_LOCATION (decl));
19603 : }
19604 : }
19605 :
19606 : /* True iff FN is an implicitly-defined default constructor. */
19607 :
19608 : static bool
19609 18849035 : implicit_default_ctor_p (tree fn)
19610 : {
19611 18849035 : return (DECL_CONSTRUCTOR_P (fn)
19612 18849035 : && !user_provided_p (fn)
19613 21797985 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19614 : }
19615 :
19616 : /* Clobber the contents of *this to let the back end know that the object
19617 : storage is dead when we enter the constructor or leave the destructor. */
19618 :
19619 : static tree
19620 1549480 : build_clobber_this (clobber_kind kind)
19621 : {
19622 : /* Clobbering an empty base is pointless, and harmful if its one byte
19623 : TYPE_SIZE overlays real data. */
19624 1549480 : if (is_empty_class (current_class_type))
19625 0 : return void_node;
19626 :
19627 : /* If we have virtual bases, clobber the whole object, but only if we're in
19628 : charge. If we don't have virtual bases, clobber the as-base type so we
19629 : don't mess with tail padding. */
19630 1549480 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19631 :
19632 1549480 : tree ctype = current_class_type;
19633 1549480 : if (!vbases)
19634 1542679 : ctype = CLASSTYPE_AS_BASE (ctype);
19635 :
19636 1549480 : tree clobber = build_clobber (ctype, kind);
19637 :
19638 1549480 : tree thisref = current_class_ref;
19639 1549480 : if (ctype != current_class_type)
19640 : {
19641 165728 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19642 165728 : thisref = convert_from_reference (thisref);
19643 : }
19644 :
19645 1549480 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19646 1549480 : if (kind == CLOBBER_OBJECT_BEGIN)
19647 0 : TREE_SET_CODE (exprstmt, INIT_EXPR);
19648 1549480 : if (vbases)
19649 6801 : exprstmt = build_if_in_charge (exprstmt);
19650 :
19651 : return exprstmt;
19652 : }
19653 :
19654 : /* Create the FUNCTION_DECL for a function definition.
19655 : DECLSPECS and DECLARATOR are the parts of the declaration;
19656 : they describe the function's name and the type it returns,
19657 : but twisted together in a fashion that parallels the syntax of C.
19658 :
19659 : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19660 : DECLARATOR is really the DECL for the function we are about to
19661 : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19662 : indicating that the function is an inline defined in-class.
19663 :
19664 : This function creates a binding context for the function body
19665 : as well as setting up the FUNCTION_DECL in current_function_decl.
19666 :
19667 : For C++, we must first check whether that datum makes any sense.
19668 : For example, "class A local_a(1,2);" means that variable local_a
19669 : is an aggregate of type A, which should have a constructor
19670 : applied to it with the argument list [1, 2].
19671 :
19672 : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19673 : or may be a BLOCK if the function has been defined previously
19674 : in this translation unit. On exit, DECL_INITIAL (decl1) will be
19675 : error_mark_node if the function has never been defined, or
19676 : a BLOCK if the function has been defined somewhere. */
19677 :
19678 : bool
19679 165485967 : start_preparsed_function (tree decl1, tree attrs, int flags)
19680 : {
19681 165485967 : tree ctype = NULL_TREE;
19682 165485967 : bool doing_friend = false;
19683 :
19684 : /* Sanity check. */
19685 165485967 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19686 165485967 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19687 :
19688 165485967 : tree fntype = TREE_TYPE (decl1);
19689 165485967 : if (DECL_CLASS_SCOPE_P (decl1))
19690 123423061 : ctype = DECL_CONTEXT (decl1);
19691 : else
19692 : {
19693 84125812 : ctype = DECL_FRIEND_CONTEXT (decl1);
19694 :
19695 42062905 : if (ctype)
19696 165485967 : doing_friend = true;
19697 : }
19698 :
19699 : /* Adjust for #pragma target/optimize if this is an artificial function that
19700 : (probably) didn't go through grokfndecl. We particularly don't want this
19701 : for deferred instantiations, which should match their template. */
19702 165485967 : if (DECL_ARTIFICIAL (decl1))
19703 4630613 : decl_attributes (&decl1, NULL_TREE, 0);
19704 :
19705 165485967 : if (DECL_DECLARED_INLINE_P (decl1)
19706 165485967 : && lookup_attribute ("noinline", attrs))
19707 : {
19708 0 : auto_urlify_attributes sentinel;
19709 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
19710 : "inline function %qD given attribute %qs", decl1, "noinline");
19711 0 : }
19712 :
19713 : /* Handle gnu_inline attribute. */
19714 165485967 : if (GNU_INLINE_P (decl1))
19715 : {
19716 1877691 : DECL_EXTERNAL (decl1) = 1;
19717 1877691 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
19718 1877691 : DECL_INTERFACE_KNOWN (decl1) = 1;
19719 1877691 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
19720 : }
19721 :
19722 165485967 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
19723 : /* This is a constructor, we must ensure that any default args
19724 : introduced by this definition are propagated to the clones
19725 : now. The clones are used directly in overload resolution. */
19726 20431342 : adjust_clone_args (decl1);
19727 :
19728 : /* Sometimes we don't notice that a function is a static member, and
19729 : build a METHOD_TYPE for it. Fix that up now. */
19730 165485967 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
19731 : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
19732 :
19733 : /* Set up current_class_type, and enter the scope of the class, if
19734 : appropriate. */
19735 165485967 : if (ctype)
19736 127836658 : push_nested_class (ctype);
19737 :
19738 : /* Now that we have entered the scope of the class, we must restore
19739 : the bindings for any template parameters surrounding DECL1, if it
19740 : is an inline member template. (Order is important; consider the
19741 : case where a template parameter has the same name as a field of
19742 : the class.) It is not until after this point that
19743 : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19744 165485967 : if (flags & SF_INCLASS_INLINE)
19745 80614238 : maybe_begin_member_template_processing (decl1);
19746 :
19747 : /* Effective C++ rule 15. */
19748 165485967 : if (warn_ecpp
19749 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19750 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19751 165485997 : && VOID_TYPE_P (TREE_TYPE (fntype)))
19752 0 : warning (OPT_Weffc__,
19753 : "%<operator=%> should return a reference to %<*this%>");
19754 :
19755 : /* Make the init_value nonzero so pushdecl knows this is not tentative.
19756 : error_mark_node is replaced below (in poplevel) with the BLOCK. */
19757 165485967 : if (!DECL_INITIAL (decl1))
19758 42877490 : DECL_INITIAL (decl1) = error_mark_node;
19759 :
19760 : /* This function exists in static storage.
19761 : (This does not mean `static' in the C sense!) */
19762 165485967 : TREE_STATIC (decl1) = 1;
19763 :
19764 : /* We must call push_template_decl after current_class_type is set
19765 : up. (If we are processing inline definitions after exiting a
19766 : class scope, current_class_type will be NULL_TREE until set above
19767 : by push_nested_class.) */
19768 165485967 : if (processing_template_decl)
19769 : {
19770 91963004 : tree newdecl1 = push_template_decl (decl1, doing_friend);
19771 91963004 : if (newdecl1 == error_mark_node)
19772 : {
19773 36 : if (ctype)
19774 9 : pop_nested_class ();
19775 36 : return false;
19776 : }
19777 91962968 : decl1 = newdecl1;
19778 : }
19779 :
19780 : /* Make sure the parameter and return types are reasonable. When
19781 : you declare a function, these types can be incomplete, but they
19782 : must be complete when you define the function. */
19783 165485931 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
19784 :
19785 : /* Build the return declaration for the function. */
19786 165485931 : tree restype = TREE_TYPE (fntype);
19787 :
19788 165485931 : if (DECL_RESULT (decl1) == NULL_TREE)
19789 : {
19790 : /* In a template instantiation, copy the return type location. When
19791 : parsing, the location will be set in grokdeclarator. */
19792 73323742 : location_t loc = input_location;
19793 73323742 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
19794 : {
19795 38793606 : tree tmpl = template_for_substitution (decl1);
19796 38793606 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
19797 36985172 : loc = DECL_SOURCE_LOCATION (res);
19798 : }
19799 :
19800 73323742 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
19801 73323742 : DECL_ARTIFICIAL (resdecl) = 1;
19802 73323742 : DECL_IGNORED_P (resdecl) = 1;
19803 73323742 : DECL_RESULT (decl1) = resdecl;
19804 :
19805 73323742 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
19806 : }
19807 :
19808 : /* Record the decl so that the function name is defined.
19809 : If we already have a decl for this name, and it is a FUNCTION_DECL,
19810 : use the old decl. */
19811 165485931 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
19812 : {
19813 : /* A specialization is not used to guide overload resolution. */
19814 19439065 : if (!DECL_FUNCTION_MEMBER_P (decl1)
19815 19419978 : && !(DECL_USE_TEMPLATE (decl1) &&
19816 12290 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
19817 : {
19818 9304436 : tree olddecl = pushdecl (decl1);
19819 :
19820 9304436 : if (olddecl == error_mark_node)
19821 : /* If something went wrong when registering the declaration,
19822 : use DECL1; we have to have a FUNCTION_DECL to use when
19823 : parsing the body of the function. */
19824 : ;
19825 : else
19826 : {
19827 : /* Otherwise, OLDDECL is either a previous declaration
19828 : of the same function or DECL1 itself. */
19829 :
19830 9304226 : if (warn_missing_declarations
19831 21 : && olddecl == decl1
19832 15 : && !DECL_MAIN_P (decl1)
19833 15 : && TREE_PUBLIC (decl1)
19834 9304238 : && !DECL_DECLARED_INLINE_P (decl1))
19835 : {
19836 9 : tree context;
19837 :
19838 : /* Check whether DECL1 is in an anonymous
19839 : namespace. */
19840 9 : for (context = DECL_CONTEXT (decl1);
19841 18 : context;
19842 9 : context = DECL_CONTEXT (context))
19843 : {
19844 12 : if (TREE_CODE (context) == NAMESPACE_DECL
19845 12 : && DECL_NAME (context) == NULL_TREE)
19846 : break;
19847 : }
19848 :
19849 9 : if (context == NULL)
19850 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
19851 6 : OPT_Wmissing_declarations,
19852 : "no previous declaration for %qD", decl1);
19853 : }
19854 :
19855 9304226 : decl1 = olddecl;
19856 : }
19857 : }
19858 : else
19859 : {
19860 : /* We need to set the DECL_CONTEXT. */
19861 798816 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
19862 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
19863 : }
19864 10103252 : fntype = TREE_TYPE (decl1);
19865 10103252 : restype = TREE_TYPE (fntype);
19866 :
19867 : /* If #pragma weak applies, mark the decl appropriately now.
19868 : The pragma only applies to global functions. Because
19869 : determining whether or not the #pragma applies involves
19870 : computing the mangled name for the declaration, we cannot
19871 : apply the pragma until after we have merged this declaration
19872 : with any previous declarations; if the original declaration
19873 : has a linkage specification, that specification applies to
19874 : the definition as well, and may affect the mangled name. */
19875 10103252 : if (DECL_FILE_SCOPE_P (decl1))
19876 2484206 : maybe_apply_pragma_weak (decl1);
19877 : }
19878 :
19879 : /* We are now in the scope of the function being defined. */
19880 165485931 : current_function_decl = decl1;
19881 :
19882 : /* Save the parm names or decls from this function's declarator
19883 : where store_parm_decls will find them. */
19884 165485931 : tree current_function_parms = DECL_ARGUMENTS (decl1);
19885 :
19886 : /* Let the user know we're compiling this function. */
19887 165485931 : announce_function (decl1);
19888 :
19889 165485931 : gcc_assert (DECL_INITIAL (decl1));
19890 :
19891 : /* This function may already have been parsed, in which case just
19892 : return; our caller will skip over the body without parsing. */
19893 165485931 : if (DECL_INITIAL (decl1) != error_mark_node)
19894 : return true;
19895 :
19896 : /* Initialize RTL machinery. We cannot do this until
19897 : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
19898 : even when processing a template; this is how we get
19899 : CFUN set up, and our per-function variables initialized.
19900 : FIXME factor out the non-RTL stuff. */
19901 165485931 : cp_binding_level *bl = current_binding_level;
19902 165485931 : allocate_struct_function (decl1, processing_template_decl);
19903 :
19904 : /* Initialize the language data structures. Whenever we start
19905 : a new function, we destroy temporaries in the usual way. */
19906 165485931 : cfun->language = ggc_cleared_alloc<language_function> ();
19907 165485931 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
19908 165485931 : current_binding_level = bl;
19909 :
19910 : /* If we are (erroneously) defining a function that we have already
19911 : defined before, wipe out what we knew before. */
19912 165485931 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
19913 165485931 : FNDECL_USED_AUTO (decl1) = false;
19914 165485931 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
19915 :
19916 165485931 : if (!processing_template_decl && type_uses_auto (restype))
19917 : {
19918 2239288 : FNDECL_USED_AUTO (decl1) = true;
19919 2239288 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
19920 : }
19921 :
19922 : /* Start the statement-tree, start the tree now. */
19923 165485931 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
19924 :
19925 165485931 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
19926 : {
19927 : /* We know that this was set up by `grokclassfn'. We do not
19928 : wait until `store_parm_decls', since evil parse errors may
19929 : never get us to that point. Here we keep the consistency
19930 : between `current_class_type' and `current_class_ptr'. */
19931 109239905 : tree t = DECL_ARGUMENTS (decl1);
19932 :
19933 109239905 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
19934 109239905 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
19935 :
19936 109239905 : cp_function_chain->x_current_class_ref
19937 109239905 : = cp_build_fold_indirect_ref (t);
19938 : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
19939 109239905 : cp_function_chain->x_current_class_ptr = t;
19940 :
19941 : /* Constructors and destructors need to know whether they're "in
19942 : charge" of initializing virtual base classes. */
19943 109239905 : t = DECL_CHAIN (t);
19944 109239905 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
19945 : {
19946 16085 : current_in_charge_parm = t;
19947 16085 : t = DECL_CHAIN (t);
19948 : }
19949 109239905 : if (DECL_HAS_VTT_PARM_P (decl1))
19950 : {
19951 32213 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
19952 32213 : current_vtt_parm = t;
19953 : }
19954 : }
19955 :
19956 292130366 : bool honor_interface = (!DECL_TEMPLOID_INSTANTIATION (decl1)
19957 : /* Implicitly-defined methods (like the
19958 : destructor for a class in which no destructor
19959 : is explicitly declared) must not be defined
19960 : until their definition is needed. So, we
19961 : ignore interface specifications for
19962 : compiler-generated functions. */
19963 200028592 : && !DECL_ARTIFICIAL (decl1));
19964 165485931 : struct c_fileinfo *finfo
19965 165485931 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
19966 :
19967 165485931 : if (processing_template_decl)
19968 : /* Don't mess with interface flags. */;
19969 73522963 : else if (DECL_INTERFACE_KNOWN (decl1))
19970 : {
19971 21016445 : tree ctx = decl_function_context (decl1);
19972 :
19973 21016445 : if (DECL_NOT_REALLY_EXTERN (decl1))
19974 17469552 : DECL_EXTERNAL (decl1) = 0;
19975 :
19976 21016445 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
19977 : /* This is a function in a local class in an extern inline
19978 : or template function. */
19979 3994956 : comdat_linkage (decl1);
19980 : }
19981 : /* If this function belongs to an interface, it is public.
19982 : If it belongs to someone else's interface, it is also external.
19983 : This only affects inlines and template instantiations. */
19984 52506518 : else if (!finfo->interface_unknown && honor_interface)
19985 : {
19986 106 : if (DECL_DECLARED_INLINE_P (decl1)
19987 106 : || DECL_TEMPLOID_INSTANTIATION (decl1))
19988 : {
19989 82 : DECL_EXTERNAL (decl1)
19990 164 : = (finfo->interface_only
19991 82 : || (DECL_DECLARED_INLINE_P (decl1)
19992 36 : && ! flag_implement_inlines
19993 0 : && !DECL_VINDEX (decl1)));
19994 :
19995 : /* For WIN32 we also want to put these in linkonce sections. */
19996 82 : maybe_make_one_only (decl1);
19997 : }
19998 : else
19999 24 : DECL_EXTERNAL (decl1) = 0;
20000 106 : DECL_INTERFACE_KNOWN (decl1) = 1;
20001 : /* If this function is in an interface implemented in this file,
20002 : make sure that the back end knows to emit this function
20003 : here. */
20004 106 : if (!DECL_EXTERNAL (decl1))
20005 60 : mark_needed (decl1);
20006 : }
20007 52506412 : else if (finfo->interface_unknown && finfo->interface_only
20008 0 : && honor_interface)
20009 : {
20010 : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
20011 : interface, we will have both finfo->interface_unknown and
20012 : finfo->interface_only set. In that case, we don't want to
20013 : use the normal heuristics because someone will supply a
20014 : #pragma implementation elsewhere, and deducing it here would
20015 : produce a conflict. */
20016 0 : comdat_linkage (decl1);
20017 0 : DECL_EXTERNAL (decl1) = 0;
20018 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
20019 0 : DECL_DEFER_OUTPUT (decl1) = 1;
20020 : }
20021 : else
20022 : {
20023 : /* This is a definition, not a reference.
20024 : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
20025 52506412 : if (!GNU_INLINE_P (decl1))
20026 52506409 : DECL_EXTERNAL (decl1) = 0;
20027 :
20028 52506412 : if ((DECL_DECLARED_INLINE_P (decl1)
20029 1999476 : || DECL_TEMPLOID_INSTANTIATION (decl1))
20030 54269792 : && ! DECL_INTERFACE_KNOWN (decl1))
20031 52270316 : DECL_DEFER_OUTPUT (decl1) = 1;
20032 : else
20033 236096 : DECL_INTERFACE_KNOWN (decl1) = 1;
20034 : }
20035 :
20036 : /* Determine the ELF visibility attribute for the function. We must not
20037 : do this before calling "pushdecl", as we must allow "duplicate_decls"
20038 : to merge any attributes appropriately. We also need to wait until
20039 : linkage is set. */
20040 165485931 : if (!DECL_CLONED_FUNCTION_P (decl1))
20041 148018705 : determine_visibility (decl1);
20042 :
20043 165485931 : if (!processing_template_decl)
20044 73522963 : maybe_instantiate_noexcept (decl1);
20045 :
20046 165485931 : begin_scope (sk_function_parms, decl1);
20047 :
20048 165485931 : ++function_depth;
20049 :
20050 165485931 : start_fname_decls ();
20051 :
20052 165485931 : store_parm_decls (current_function_parms);
20053 :
20054 165485931 : start_function_contracts (decl1);
20055 :
20056 165485931 : if (!processing_template_decl
20057 73522963 : && flag_lifetime_dse > 1
20058 147036614 : && DECL_CONSTRUCTOR_P (decl1)
20059 : /* Clobbering an empty base is harmful if it overlays real data. */
20060 20689403 : && !is_empty_class (current_class_type)
20061 : /* We can't clobber safely for an implicitly-defined default constructor
20062 : because part of the initialization might happen before we enter the
20063 : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
20064 18848401 : && !implicit_default_ctor_p (decl1)
20065 182157733 : && !lookup_attribute ("clobber *this",
20066 16671802 : DECL_ATTRIBUTES (current_class_ptr)))
20067 16663514 : DECL_ATTRIBUTES (current_class_ptr)
20068 33327028 : = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
20069 16663514 : DECL_ATTRIBUTES (current_class_ptr));
20070 :
20071 165485931 : if (!processing_template_decl
20072 147045926 : && DECL_CONSTRUCTOR_P (decl1)
20073 20690645 : && sanitize_flags_p (SANITIZE_VPTR)
20074 1902 : && !DECL_CLONED_FUNCTION_P (decl1)
20075 165486565 : && !implicit_default_ctor_p (decl1))
20076 480 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
20077 :
20078 165485931 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
20079 165485362 : start_lambda_scope (decl1);
20080 :
20081 : return true;
20082 : }
20083 :
20084 :
20085 : /* Like start_preparsed_function, except that instead of a
20086 : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
20087 :
20088 : Returns true on success. If the DECLARATOR is not suitable
20089 : for a function, we return false, which tells the parser to
20090 : skip the entire function. */
20091 :
20092 : bool
20093 37703810 : start_function (cp_decl_specifier_seq *declspecs,
20094 : const cp_declarator *declarator,
20095 : tree attrs)
20096 : {
20097 37703810 : tree decl1;
20098 :
20099 37703810 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
20100 37703810 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
20101 37703810 : if (decl1 == error_mark_node)
20102 : return false;
20103 :
20104 37703445 : if (DECL_MAIN_P (decl1))
20105 : /* main must return int. grokfndecl should have corrected it
20106 : (and issued a diagnostic) if the user got it wrong. */
20107 33547 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
20108 : integer_type_node));
20109 :
20110 37703445 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
20111 : }
20112 :
20113 : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
20114 : FN. */
20115 :
20116 : bool
20117 317165849 : use_eh_spec_block (tree fn)
20118 : {
20119 315230970 : return (flag_exceptions && flag_enforce_eh_specs
20120 315230970 : && !processing_template_decl
20121 : /* We insert the EH_SPEC_BLOCK only in the original
20122 : function; then, it is copied automatically to the
20123 : clones. */
20124 130087815 : && !DECL_CLONED_FUNCTION_P (fn)
20125 : /* Implicitly-generated constructors and destructors have
20126 : exception specifications. However, those specifications
20127 : are the union of the possible exceptions specified by the
20128 : constructors/destructors for bases and members, so no
20129 : unallowed exception will ever reach this function. By
20130 : not creating the EH_SPEC_BLOCK we save a little memory,
20131 : and we avoid spurious warnings about unreachable
20132 : code. */
20133 112661826 : && !DECL_DEFAULTED_FN (fn)
20134 426649375 : && !type_throw_all_p (TREE_TYPE (fn)));
20135 : }
20136 :
20137 : /* Helper function to push ARGS into the current lexical scope. DECL
20138 : is the function declaration. NONPARMS is used to handle enum
20139 : constants. */
20140 :
20141 : void
20142 160422910 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
20143 : {
20144 : /* If we're doing semantic analysis, then we'll call pushdecl
20145 : for each of these. We must do them in reverse order so that
20146 : they end in the correct forward order. */
20147 160422910 : args = nreverse (args);
20148 :
20149 160422910 : tree next;
20150 490426439 : for (tree parm = args; parm; parm = next)
20151 : {
20152 330003529 : next = DECL_CHAIN (parm);
20153 330003529 : if (TREE_CODE (parm) == PARM_DECL)
20154 330003529 : pushdecl (parm);
20155 0 : else if (nonparms)
20156 : {
20157 : /* If we find an enum constant or a type tag, put it aside for
20158 : the moment. */
20159 0 : TREE_CHAIN (parm) = NULL_TREE;
20160 0 : *nonparms = chainon (*nonparms, parm);
20161 : }
20162 : }
20163 :
20164 : /* Get the decls in their original chain order and record in the
20165 : function. This is all and only the PARM_DECLs that were
20166 : pushed into scope by the loop above. */
20167 160422910 : DECL_ARGUMENTS (decl) = get_local_decls ();
20168 160422910 : }
20169 :
20170 : /* Store the parameter declarations into the current function declaration.
20171 : This is called after parsing the parameter declarations, before
20172 : digesting the body of the function.
20173 :
20174 : Also install to binding contour return value identifier, if any. */
20175 :
20176 : static void
20177 165485931 : store_parm_decls (tree current_function_parms)
20178 : {
20179 165485931 : tree fndecl = current_function_decl;
20180 :
20181 : /* This is a chain of any other decls that came in among the parm
20182 : declarations. If a parm is declared with enum {foo, bar} x;
20183 : then CONST_DECLs for foo and bar are put here. */
20184 165485931 : tree nonparms = NULL_TREE;
20185 :
20186 165485931 : if (current_function_parms)
20187 : {
20188 : /* This case is when the function was defined with an ANSI prototype.
20189 : The parms already have decls, so we need not do anything here
20190 : except record them as in effect
20191 : and complain if any redundant old-style parm decls were written. */
20192 :
20193 157149309 : tree specparms = current_function_parms;
20194 :
20195 : /* Must clear this because it might contain TYPE_DECLs declared
20196 : at class level. */
20197 157149309 : current_binding_level->names = NULL;
20198 :
20199 157149309 : do_push_parm_decls (fndecl, specparms, &nonparms);
20200 : }
20201 : else
20202 8336622 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
20203 :
20204 : /* Now store the final chain of decls for the arguments
20205 : as the decl-chain of the current lexical scope.
20206 : Put the enumerators in as well, at the front so that
20207 : DECL_ARGUMENTS is not modified. */
20208 165485931 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
20209 :
20210 165485931 : if (use_eh_spec_block (current_function_decl))
20211 25447496 : current_eh_spec_block = begin_eh_spec_block ();
20212 165485931 : }
20213 :
20214 :
20215 : /* Mark CDTOR's implicit THIS argument for returning, if required by
20216 : the ABI.. Return the decl for THIS, if it is to be returned, and
20217 : NULL otherwise. */
20218 :
20219 : tree
20220 146591161 : maybe_prepare_return_this (tree cdtor)
20221 : {
20222 146591161 : if (targetm.cxx.cdtor_returns_this ())
20223 0 : if (tree val = DECL_ARGUMENTS (cdtor))
20224 : {
20225 0 : suppress_warning (val, OPT_Wuse_after_free);
20226 0 : return val;
20227 : }
20228 :
20229 : return NULL_TREE;
20230 : }
20231 :
20232 : /* Set the return value of the [cd]tor if the ABI wants that. */
20233 :
20234 : void
20235 8873706 : maybe_return_this ()
20236 : {
20237 8873706 : if (tree val = maybe_prepare_return_this (current_function_decl))
20238 : {
20239 : /* Return the address of the object. */
20240 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
20241 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
20242 0 : DECL_RESULT (current_function_decl), val);
20243 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
20244 0 : add_stmt (exprstmt);
20245 : }
20246 8873706 : }
20247 :
20248 : /* Do all the processing for the beginning of a destructor; set up the
20249 : vtable pointers and cleanups for bases and members. */
20250 :
20251 : static void
20252 1684531 : begin_destructor_body (void)
20253 : {
20254 1684531 : tree compound_stmt;
20255 :
20256 : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20257 : issued an error message. We still want to try to process the
20258 : body of the function, but initialize_vtbl_ptrs will crash if
20259 : TYPE_BINFO is NULL. */
20260 1684531 : if (COMPLETE_TYPE_P (current_class_type))
20261 : {
20262 1684531 : compound_stmt = begin_compound_stmt (0);
20263 : /* Make all virtual function table pointers in non-virtual base
20264 : classes point to CURRENT_CLASS_TYPE's virtual function
20265 : tables. */
20266 1684531 : initialize_vtbl_ptrs (current_class_ptr);
20267 1684531 : finish_compound_stmt (compound_stmt);
20268 :
20269 1684531 : if (flag_lifetime_dse
20270 : /* Clobbering an empty base is harmful if it overlays real data. */
20271 1684531 : && !is_empty_class (current_class_type))
20272 : {
20273 1549543 : if (sanitize_flags_p (SANITIZE_VPTR)
20274 236 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20275 1549606 : && TYPE_CONTAINS_VPTR_P (current_class_type))
20276 : {
20277 63 : tree binfo = TYPE_BINFO (current_class_type);
20278 63 : tree ref
20279 63 : = cp_build_fold_indirect_ref (current_class_ptr);
20280 :
20281 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20282 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20283 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20284 : NOP_EXPR, vtbl,
20285 : tf_warning_or_error);
20286 : /* If the vptr is shared with some virtual nearly empty base,
20287 : don't clear it if not in charge, the dtor of the virtual
20288 : nearly empty base will do that later. */
20289 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
20290 : {
20291 : tree c = current_class_type;
20292 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
20293 : {
20294 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20295 : {
20296 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
20297 : tf_warning_or_error);
20298 36 : stmt = build_if_in_charge (stmt);
20299 36 : break;
20300 : }
20301 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20302 : }
20303 : }
20304 63 : finish_decl_cleanup (NULL_TREE, stmt);
20305 : }
20306 : else
20307 1549480 : finish_decl_cleanup (NULL_TREE,
20308 : build_clobber_this (CLOBBER_OBJECT_END));
20309 : }
20310 :
20311 : /* And insert cleanups for our bases and members so that they
20312 : will be properly destroyed if we throw. */
20313 1684531 : push_base_cleanups ();
20314 : }
20315 1684531 : }
20316 :
20317 : /* Do the necessary processing for the beginning of a function body, which
20318 : in this case includes member-initializers, but not the catch clauses of
20319 : a function-try-block. Currently, this means opening a binding level
20320 : for the member-initializers (in a ctor), member cleanups (in a dtor),
20321 : and capture proxies (in a lambda operator()). */
20322 :
20323 : tree
20324 124959386 : begin_function_body (void)
20325 : {
20326 257852826 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20327 : return NULL_TREE;
20328 :
20329 25814038 : if (processing_template_decl)
20330 : /* Do nothing now. */;
20331 : else
20332 : /* Always keep the BLOCK node associated with the outermost pair of
20333 : curly braces of a function. These are needed for correct
20334 : operation of dwarfout.c. */
20335 9770963 : keep_next_level (true);
20336 :
20337 25814038 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
20338 25814038 : current_binding_level->artificial = 1;
20339 :
20340 25814038 : if (processing_template_decl)
20341 : /* Do nothing now. */;
20342 19541926 : else if (DECL_DESTRUCTOR_P (current_function_decl))
20343 1684531 : begin_destructor_body ();
20344 :
20345 : return stmt;
20346 : }
20347 :
20348 : /* Do the processing for the end of a function body. Currently, this means
20349 : closing out the cleanups for fully-constructed bases and members, and in
20350 : the case of the destructor, deleting the object if desired. Again, this
20351 : is only meaningful for [cd]tors, since they are the only functions where
20352 : there is a significant distinction between the main body and any
20353 : function catch clauses. Handling, say, main() return semantics here
20354 : would be wrong, as flowing off the end of a function catch clause for
20355 : main() would also need to return 0. */
20356 :
20357 : void
20358 124959362 : finish_function_body (tree compstmt)
20359 : {
20360 124959362 : if (compstmt == NULL_TREE)
20361 : return;
20362 :
20363 : /* Close the block. */
20364 25814038 : finish_compound_stmt (compstmt);
20365 :
20366 25814038 : if (processing_template_decl)
20367 : /* Do nothing now. */;
20368 9770963 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
20369 9770963 : || DECL_DESTRUCTOR_P (current_function_decl))
20370 8577348 : maybe_return_this ();
20371 : }
20372 :
20373 : /* Given a function, returns the BLOCK corresponding to the outermost level
20374 : of curly braces, skipping the artificial block created for constructor
20375 : initializers. */
20376 :
20377 : tree
20378 385926 : outer_curly_brace_block (tree fndecl)
20379 : {
20380 385926 : tree block = DECL_INITIAL (fndecl);
20381 385926 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20382 : return block;
20383 1029 : block = BLOCK_SUBBLOCKS (block);
20384 1029 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20385 : return block;
20386 0 : block = BLOCK_SUBBLOCKS (block);
20387 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20388 : return block;
20389 : }
20390 :
20391 : /* If FNDECL is a class's key method, add the class to the list of
20392 : keyed classes that should be emitted. */
20393 :
20394 : static void
20395 165489876 : record_key_method_defined (tree fndecl)
20396 : {
20397 221739823 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20398 109320896 : && DECL_VIRTUAL_P (fndecl)
20399 168374292 : && !processing_template_decl)
20400 : {
20401 1583733 : tree fnclass = DECL_CONTEXT (fndecl);
20402 1583733 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20403 : {
20404 20436 : tree classdecl = TYPE_NAME (fnclass);
20405 : /* Classes attached to a named module are already handled. */
20406 20436 : if (!DECL_LANG_SPECIFIC (classdecl)
20407 20763 : || !DECL_MODULE_ATTACH_P (classdecl))
20408 20391 : vec_safe_push (keyed_classes, fnclass);
20409 : }
20410 : }
20411 165489876 : }
20412 :
20413 : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20414 : of "return *this;" immediately before its location, using FNDECL's
20415 : first statement (if any) to give the indentation, if appropriate. */
20416 :
20417 : static void
20418 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20419 : {
20420 18 : location_t indent = UNKNOWN_LOCATION;
20421 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20422 18 : if (stmts)
20423 9 : indent = EXPR_LOCATION (stmts);
20424 18 : richloc->add_fixit_insert_formatted ("return *this;",
20425 : richloc->get_loc (),
20426 : indent);
20427 18 : }
20428 :
20429 : /* Finish up a function declaration and compile that function
20430 : all the way to assembler language output. The free the storage
20431 : for the function definition. INLINE_P is TRUE if we just
20432 : finished processing the body of an in-class inline function
20433 : definition. (This processing will have taken place after the
20434 : class definition is complete.) */
20435 :
20436 : tree
20437 165485895 : finish_function (bool inline_p)
20438 : {
20439 165485895 : tree fndecl = current_function_decl;
20440 165485895 : tree fntype, ctype = NULL_TREE;
20441 165485895 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
20442 :
20443 : /* When we get some parse errors, we can end up without a
20444 : current_function_decl, so cope. */
20445 165485895 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
20446 0 : return error_mark_node;
20447 :
20448 165485895 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20449 165485326 : finish_lambda_scope ();
20450 :
20451 165485895 : if (c_dialect_objc ())
20452 0 : objc_finish_function ();
20453 :
20454 165485895 : record_key_method_defined (fndecl);
20455 :
20456 165485895 : fntype = TREE_TYPE (fndecl);
20457 :
20458 : /* TREE_READONLY (fndecl) = 1;
20459 : This caused &foo to be of type ptr-to-const-function
20460 : which then got a warning when stored in a ptr-to-function variable. */
20461 :
20462 165485895 : gcc_assert (building_stmt_list_p ());
20463 : /* The current function is being defined, so its DECL_INITIAL should
20464 : be set, and unless there's a multiple definition, it should be
20465 : error_mark_node. */
20466 165485895 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20467 :
20468 165485895 : cp_coroutine_transform *coroutine = nullptr;
20469 165485895 : if (flag_coroutines
20470 162787309 : && !processing_template_decl
20471 71765816 : && DECL_COROUTINE_P (fndecl)
20472 165490746 : && !DECL_RAMP_FN (fndecl))
20473 : {
20474 1673 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20475 : && !DECL_DEFAULTED_FN (fndecl));
20476 1673 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
20477 1673 : if (coroutine && coroutine->cp_valid_coroutine ())
20478 1673 : coroutine->apply_transforms ();
20479 :
20480 : /* We should handle coroutine IFNs in middle end lowering. */
20481 1673 : cfun->coroutine_component = true;
20482 :
20483 : /* Do not try to process the ramp's EH unless outlining succeeded. */
20484 1673 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
20485 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20486 : (TREE_TYPE (fndecl)),
20487 360 : current_eh_spec_block);
20488 :
20489 : /* If outlining succeeded, then add contracts handling if needed. */
20490 1673 : if (coroutine->cp_valid_coroutine ())
20491 1589 : maybe_apply_function_contracts (fndecl);
20492 : }
20493 : else
20494 : /* For a cloned function, we've already got all the code we need;
20495 : there's no need to add any extra bits. */
20496 165484222 : if (!DECL_CLONED_FUNCTION_P (fndecl))
20497 : {
20498 : /* Make it so that `main' always returns 0 by default. */
20499 148016996 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20500 148050528 : && !TREE_THIS_VOLATILE (current_function_decl))
20501 33532 : finish_return_stmt (integer_zero_node);
20502 :
20503 148016996 : if (use_eh_spec_block (current_function_decl))
20504 25447133 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20505 : (TREE_TYPE (current_function_decl)),
20506 25447133 : current_eh_spec_block);
20507 :
20508 148016996 : maybe_apply_function_contracts (current_function_decl);
20509 :
20510 : }
20511 :
20512 : /* If we're saving up tree structure, tie off the function now. */
20513 165485895 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20514 :
20515 165485895 : finish_fname_decls ();
20516 :
20517 : /* This must come after expand_function_end because cleanups might
20518 : have declarations (from inline functions) that need to go into
20519 : this function's blocks. */
20520 :
20521 : /* If the current binding level isn't the outermost binding level
20522 : for this function, either there is a bug, or we have experienced
20523 : syntax errors and the statement tree is malformed. */
20524 165485895 : if (current_binding_level->kind != sk_function_parms)
20525 : {
20526 : /* Make sure we have already experienced errors. */
20527 12 : gcc_assert (errorcount);
20528 :
20529 : /* Throw away the broken statement tree and extra binding
20530 : levels. */
20531 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20532 :
20533 27 : while (current_binding_level->kind != sk_function_parms)
20534 : {
20535 15 : if (current_binding_level->kind == sk_class)
20536 0 : pop_nested_class ();
20537 : else
20538 15 : poplevel (0, 0, 0);
20539 : }
20540 : }
20541 165485895 : poplevel (1, 0, 1);
20542 :
20543 : /* Statements should always be full-expressions at the outermost set
20544 : of curly braces for a function. */
20545 165485895 : gcc_assert (stmts_are_full_exprs_p ());
20546 :
20547 : /* If there are no return statements in a function with auto return type,
20548 : the return type is void. But if the declared type is something like
20549 : auto*, this is an error. */
20550 73522927 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20551 167725183 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20552 : {
20553 649401 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20554 649386 : && !current_function_returns_value
20555 1298582 : && !current_function_returns_null)
20556 : {
20557 : /* We haven't applied return type deduction because we haven't
20558 : seen any return statements. Do that now. */
20559 649181 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20560 649181 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20561 : void_node, node, tf_warning_or_error,
20562 : adc_return_type);
20563 :
20564 649181 : apply_deduced_return_type (fndecl, void_type_node);
20565 649181 : fntype = TREE_TYPE (fndecl);
20566 : }
20567 220 : else if (!current_function_returns_value
20568 6 : && !current_function_returns_null)
20569 : {
20570 6 : auto_diagnostic_group d;
20571 12 : error ("no return statements in function returning %qT",
20572 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20573 6 : inform (input_location, "only plain %<auto%> return type can be "
20574 : "deduced to %<void%>");
20575 6 : }
20576 : }
20577 :
20578 165485895 : if (FNDECL_USED_AUTO (fndecl)
20579 165485895 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20580 2239068 : if (location_t fcloc = failed_completion_location (fndecl))
20581 : {
20582 6 : auto_diagnostic_group adg;
20583 6 : if (warning (OPT_Wsfinae_incomplete_,
20584 : "defining %qD, which previously failed to be deduced "
20585 : "in a SFINAE context", fndecl)
20586 6 : && warn_sfinae_incomplete == 1)
20587 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
20588 : "-Wsfinae-incomplete=2");
20589 6 : }
20590 :
20591 : /* Remember that we were in class scope. */
20592 165485895 : if (current_class_name)
20593 127836646 : ctype = current_class_type;
20594 :
20595 165485895 : if (DECL_DELETED_FN (fndecl))
20596 : {
20597 90 : DECL_INITIAL (fndecl) = error_mark_node;
20598 90 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
20599 90 : goto cleanup;
20600 : }
20601 :
20602 165485805 : if (flag_openmp)
20603 323376 : if (tree attr = lookup_attribute ("omp declare variant base",
20604 323376 : DECL_ATTRIBUTES (fndecl)))
20605 265 : omp_declare_variant_finalize (fndecl, attr);
20606 :
20607 : /* Complain if there's just no return statement. */
20608 165485805 : if ((warn_return_type
20609 47006 : || (cxx_dialect >= cxx14
20610 38999 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20611 165456048 : && !VOID_TYPE_P (TREE_TYPE (fntype))
20612 103283618 : && !dependent_type_p (TREE_TYPE (fntype))
20613 54805547 : && !current_function_returns_value && !current_function_returns_null
20614 : /* Don't complain if we abort or throw. */
20615 187236 : && !current_function_returns_abnormally
20616 : /* Don't complain if there's an infinite loop. */
20617 945 : && !current_function_infinite_loop
20618 : /* Don't complain if we are declared noreturn. */
20619 877 : && !TREE_THIS_VOLATILE (fndecl)
20620 871 : && !DECL_NAME (DECL_RESULT (fndecl))
20621 871 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20622 : /* Structor return values (if any) are set by the compiler. */
20623 1254 : && !DECL_CONSTRUCTOR_P (fndecl)
20624 627 : && !DECL_DESTRUCTOR_P (fndecl)
20625 165486432 : && targetm.warn_func_return (fndecl))
20626 : {
20627 621 : gcc_rich_location richloc (input_location);
20628 : /* Potentially add a "return *this;" fix-it hint for
20629 : assignment operators. */
20630 621 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20631 : {
20632 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20633 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
20634 32 : && current_class_ref
20635 26 : && same_type_ignoring_top_level_qualifiers_p
20636 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20637 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
20638 18 : add_return_star_this_fixit (&richloc, fndecl);
20639 : }
20640 621 : if (cxx_dialect >= cxx14
20641 621 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
20642 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
20643 : "returning non-void");
20644 619 : else if (warning_at (&richloc, OPT_Wreturn_type,
20645 : "no return statement in function returning "
20646 : "non-void"))
20647 169 : suppress_warning (fndecl, OPT_Wreturn_type);
20648 621 : }
20649 :
20650 : /* Lambda closure members are implicitly constexpr if possible. */
20651 165485805 : if (cxx_dialect >= cxx17
20652 287735636 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20653 2593470 : DECL_DECLARED_CONSTEXPR_P (fndecl)
20654 2593470 : = ((processing_template_decl
20655 1366153 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
20656 5186050 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20657 :
20658 : /* Invoke the pre-genericize plugin before we start munging things. */
20659 165485805 : if (!processing_template_decl)
20660 73522837 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
20661 :
20662 : /* Fold away non-ODR usages of constants so that we don't need to
20663 : try and stream them in modules if they're internal. */
20664 165485805 : if (!processing_template_decl)
20665 73522837 : cp_fold_function_non_odr_use (fndecl);
20666 :
20667 : /* Save constexpr function body before it gets munged by
20668 : the NRV transformation. */
20669 165485805 : maybe_save_constexpr_fundef (fndecl);
20670 :
20671 : /* Perform delayed folding before NRV transformation. */
20672 165485805 : if (!processing_template_decl
20673 147045674 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20674 237581935 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20675 72095691 : cp_fold_function (fndecl);
20676 :
20677 : /* Set up the named return value optimization, if we can. Candidate
20678 : variables are selected in check_return_expr. */
20679 165485805 : if (tree r = current_function_return_value)
20680 : {
20681 45899153 : if (r != error_mark_node)
20682 252341 : finalize_nrv (fndecl, r);
20683 45899153 : current_function_return_value = NULL_TREE;
20684 : }
20685 :
20686 : /* Must mark the RESULT_DECL as being in this function. */
20687 165485805 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
20688 :
20689 : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
20690 : to the FUNCTION_DECL node itself. */
20691 165485805 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
20692 :
20693 : /* Store the end of the function, so that we get good line number
20694 : info for the epilogue. */
20695 165485805 : cfun->function_end_locus = input_location;
20696 :
20697 : /* Complain about parameters that are only set, but never otherwise used. */
20698 165485805 : if (warn_unused_but_set_parameter
20699 937428 : && !processing_template_decl
20700 483088 : && errorcount == unused_but_set_errorcount
20701 165968890 : && !DECL_CLONED_FUNCTION_P (fndecl))
20702 : {
20703 378245 : tree decl;
20704 :
20705 378245 : for (decl = DECL_ARGUMENTS (fndecl);
20706 1010667 : decl;
20707 632422 : decl = DECL_CHAIN (decl))
20708 632422 : if (TREE_USED (decl)
20709 524671 : && TREE_CODE (decl) == PARM_DECL
20710 524671 : && !DECL_READ_P (decl)
20711 225 : && DECL_NAME (decl)
20712 225 : && !DECL_ARTIFICIAL (decl)
20713 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
20714 225 : && !DECL_IN_SYSTEM_HEADER (decl)
20715 225 : && TREE_TYPE (decl) != error_mark_node
20716 225 : && !TYPE_REF_P (TREE_TYPE (decl))
20717 632647 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
20718 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
20719 225 : warning_at (DECL_SOURCE_LOCATION (decl),
20720 225 : OPT_Wunused_but_set_parameter_,
20721 : "parameter %qD set but not used", decl);
20722 378245 : unused_but_set_errorcount = errorcount;
20723 : }
20724 :
20725 : /* Complain about locally defined typedefs that are not used in this
20726 : function. */
20727 165485805 : maybe_warn_unused_local_typedefs ();
20728 :
20729 : /* Possibly warn about unused parameters. */
20730 165485805 : if (warn_unused_parameter
20731 668050 : && !processing_template_decl
20732 165811501 : && !DECL_CLONED_FUNCTION_P (fndecl))
20733 250706 : do_warn_unused_parameter (fndecl);
20734 :
20735 : /* Genericize before inlining. */
20736 165485805 : if (!processing_template_decl
20737 147045674 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20738 237580432 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20739 72094188 : cp_genericize (fndecl);
20740 :
20741 : /* If this function can't throw any exceptions, remember that. */
20742 165485805 : if (!processing_template_decl
20743 73522837 : && !cp_function_chain->can_throw
20744 54109852 : && !flag_non_call_exceptions
20745 54101099 : && !decl_replaceable_p (fndecl,
20746 54101099 : opt_for_fn (fndecl, flag_semantic_interposition))
20747 219350128 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
20748 53861835 : TREE_NOTHROW (fndecl) = 1;
20749 :
20750 165485895 : cleanup:
20751 :
20752 : /* We're leaving the context of this function, so zap cfun. It's still in
20753 : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
20754 165485895 : set_cfun (NULL);
20755 165485895 : current_function_decl = NULL;
20756 :
20757 : /* If this is an in-class inline definition, we may have to pop the
20758 : bindings for the template parameters that we added in
20759 : maybe_begin_member_template_processing when start_function was
20760 : called. */
20761 165485895 : if (inline_p)
20762 80614666 : maybe_end_member_template_processing ();
20763 :
20764 : /* Leave the scope of the class. */
20765 165485895 : if (ctype)
20766 127836646 : pop_nested_class ();
20767 :
20768 165485895 : --function_depth;
20769 :
20770 : /* Clean up. */
20771 165485895 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
20772 :
20773 : /* Build outlined functions for coroutines and contracts. */
20774 :
20775 165485895 : if (coroutine)
20776 : {
20777 : /* Emit the resumer and destroyer functions now, providing that we have
20778 : not encountered some fatal error. */
20779 1673 : if (coroutine->cp_valid_coroutine ())
20780 : {
20781 1589 : coroutine->finish_transforms ();
20782 1589 : resumer = coroutine->get_resumer ();
20783 1589 : destroyer = coroutine->get_destroyer ();
20784 1589 : expand_or_defer_fn (resumer);
20785 1589 : expand_or_defer_fn (destroyer);
20786 : }
20787 1673 : delete coroutine;
20788 : }
20789 :
20790 : /* If we have used outlined contracts checking functions, build and emit
20791 : them here. */
20792 165485895 : finish_function_outlined_contracts (fndecl);
20793 :
20794 165485895 : return fndecl;
20795 : }
20796 :
20797 : /* Create the FUNCTION_DECL for a function definition.
20798 : DECLSPECS and DECLARATOR are the parts of the declaration;
20799 : they describe the return type and the name of the function,
20800 : but twisted together in a fashion that parallels the syntax of C.
20801 :
20802 : This function creates a binding context for the function body
20803 : as well as setting up the FUNCTION_DECL in current_function_decl.
20804 :
20805 : Returns a FUNCTION_DECL on success.
20806 :
20807 : If the DECLARATOR is not suitable for a function (it defines a datum
20808 : instead), we return 0, which tells yyparse to report a parse error.
20809 :
20810 : May return void_type_node indicating that this method is actually
20811 : a friend. See grokfield for more details.
20812 :
20813 : Came here with a `.pushlevel' .
20814 :
20815 : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
20816 : CHANGES TO CODE IN `grokfield'. */
20817 :
20818 : tree
20819 80760636 : grokmethod (cp_decl_specifier_seq *declspecs,
20820 : const cp_declarator *declarator, tree attrlist)
20821 : {
20822 80760636 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
20823 80760636 : &attrlist);
20824 :
20825 80760636 : if (fndecl == error_mark_node)
20826 : return error_mark_node;
20827 :
20828 80760532 : if (attrlist)
20829 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
20830 :
20831 : /* Pass friends other than inline friend functions back. */
20832 80760532 : if (fndecl == void_type_node)
20833 : return fndecl;
20834 :
20835 80760532 : if (DECL_IN_AGGR_P (fndecl))
20836 : {
20837 0 : if (DECL_CLASS_SCOPE_P (fndecl))
20838 0 : error ("%qD is already defined in class %qT", fndecl,
20839 0 : DECL_CONTEXT (fndecl));
20840 0 : return error_mark_node;
20841 : }
20842 :
20843 80760532 : check_template_shadow (fndecl);
20844 :
20845 : /* p1779 ABI-Isolation makes inline not a default for in-class
20846 : definitions attached to a named module. If the user explicitly
20847 : made it inline, grokdeclarator will already have done the right
20848 : things. */
20849 80760532 : if ((!named_module_attach_p ()
20850 824 : || flag_module_implicit_inline
20851 : /* Lambda's operator function remains inline. */
20852 1612 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
20853 : /* If the user explicitly asked for this to be inline, we don't
20854 : need to do more, but more importantly we want to warn if we
20855 : can't inline it. */
20856 80760081 : && !DECL_DECLARED_INLINE_P (fndecl))
20857 : {
20858 46633309 : if (TREE_PUBLIC (fndecl))
20859 44344731 : DECL_COMDAT (fndecl) = 1;
20860 46633309 : DECL_DECLARED_INLINE_P (fndecl) = 1;
20861 : /* It's ok if we can't inline this. */
20862 46633309 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
20863 : }
20864 :
20865 : /* We process method specializations in finish_struct_1. */
20866 80760532 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
20867 : {
20868 : /* Avoid calling decl_spec_seq... until we have to. */
20869 64610961 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
20870 64610961 : fndecl = push_template_decl (fndecl, friendp);
20871 64610961 : if (fndecl == error_mark_node)
20872 : return fndecl;
20873 : }
20874 :
20875 80760524 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
20876 : {
20877 0 : fndecl = copy_node (fndecl);
20878 0 : TREE_CHAIN (fndecl) = NULL_TREE;
20879 : }
20880 :
20881 80760524 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
20882 :
20883 80760524 : DECL_IN_AGGR_P (fndecl) = 1;
20884 80760524 : return fndecl;
20885 : }
20886 :
20887 :
20888 : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
20889 : we can lay it out later, when and if its type becomes complete.
20890 :
20891 : Also handle constexpr variables where the initializer involves
20892 : an unlowered PTRMEM_CST because the class isn't complete yet. */
20893 :
20894 : void
20895 109895835 : maybe_register_incomplete_var (tree var)
20896 : {
20897 109895835 : gcc_assert (VAR_P (var));
20898 :
20899 : /* Keep track of variables with incomplete types. */
20900 53876886 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
20901 163771024 : && DECL_EXTERNAL (var))
20902 : {
20903 6580735 : tree inner_type = TREE_TYPE (var);
20904 :
20905 6730204 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
20906 149469 : inner_type = TREE_TYPE (inner_type);
20907 6580735 : inner_type = TYPE_MAIN_VARIANT (inner_type);
20908 :
20909 6721666 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
20910 : /* RTTI TD entries are created while defining the type_info. */
20911 6580735 : || (TYPE_LANG_SPECIFIC (inner_type)
20912 2343690 : && TYPE_BEING_DEFINED (inner_type)))
20913 : {
20914 140931 : incomplete_var iv = {var, inner_type};
20915 140931 : vec_safe_push (incomplete_vars, iv);
20916 : }
20917 12582400 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
20918 3475458 : && DECL_CLASS_SCOPE_P (var)
20919 1054614 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
20920 1054614 : && decl_constant_var_p (var)
20921 7115540 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
20922 : {
20923 : /* When the outermost open class is complete we can resolve any
20924 : pointers-to-members. */
20925 20 : tree context = outermost_open_class ();
20926 20 : incomplete_var iv = {var, context};
20927 20 : vec_safe_push (incomplete_vars, iv);
20928 : }
20929 : }
20930 109895835 : }
20931 :
20932 : /* Called when a class type (given by TYPE) is defined. If there are
20933 : any existing VAR_DECLs whose type has been completed by this
20934 : declaration, update them now. */
20935 :
20936 : void
20937 74098355 : complete_vars (tree type)
20938 : {
20939 74098355 : unsigned ix;
20940 74098355 : incomplete_var *iv;
20941 :
20942 74622681 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
20943 : {
20944 524326 : if (same_type_p (type, iv->incomplete_type))
20945 : {
20946 139785 : tree var = iv->decl;
20947 139785 : tree type = TREE_TYPE (var);
20948 :
20949 139785 : if (type != error_mark_node
20950 139785 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
20951 139782 : == iv->incomplete_type))
20952 : {
20953 : /* Complete the type of the variable. */
20954 139762 : complete_type (type);
20955 139762 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
20956 139762 : if (COMPLETE_TYPE_P (type))
20957 139747 : layout_var_decl (var);
20958 : }
20959 :
20960 : /* Remove this entry from the list. */
20961 139785 : incomplete_vars->unordered_remove (ix);
20962 : }
20963 : else
20964 384541 : ix++;
20965 : }
20966 74098355 : }
20967 :
20968 : /* If DECL is of a type which needs a cleanup, build and return an
20969 : expression to perform that cleanup here. Return NULL_TREE if no
20970 : cleanup need be done. DECL can also be a _REF when called from
20971 : split_nonconstant_init_1. */
20972 :
20973 : tree
20974 111364509 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
20975 : {
20976 111364509 : tree type;
20977 111364509 : tree attr;
20978 111364509 : tree cleanup;
20979 :
20980 : /* Assume no cleanup is required. */
20981 111364509 : cleanup = NULL_TREE;
20982 :
20983 111364509 : if (!decl || error_operand_p (decl))
20984 : return cleanup;
20985 :
20986 : /* Handle "__attribute__((cleanup))". We run the cleanup function
20987 : before the destructor since the destructor is what actually
20988 : terminates the lifetime of the object. */
20989 111364509 : if (DECL_P (decl))
20990 110369223 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
20991 : else
20992 : attr = NULL_TREE;
20993 110369223 : if (attr)
20994 : {
20995 159 : tree id;
20996 159 : tree fn;
20997 159 : tree arg;
20998 :
20999 : /* Get the name specified by the user for the cleanup function. */
21000 159 : id = TREE_VALUE (TREE_VALUE (attr));
21001 : /* Look up the name to find the cleanup function to call. It is
21002 : important to use lookup_name here because that is what is
21003 : used in c-common.cc:handle_cleanup_attribute when performing
21004 : initial checks on the attribute. Note that those checks
21005 : include ensuring that the function found is not an overloaded
21006 : function, or an object with an overloaded call operator,
21007 : etc.; we can rely on the fact that the function found is an
21008 : ordinary FUNCTION_DECL. */
21009 159 : fn = lookup_name (id);
21010 159 : arg = build_address (decl);
21011 159 : if (!mark_used (decl, complain) && !(complain & tf_error))
21012 0 : return error_mark_node;
21013 159 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
21014 159 : if (cleanup == error_mark_node)
21015 : return error_mark_node;
21016 : }
21017 : /* Handle ordinary C++ destructors. */
21018 111364497 : type = TREE_TYPE (decl);
21019 111364497 : if (type_build_dtor_call (type))
21020 : {
21021 7634228 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
21022 7634228 : tree addr;
21023 7634228 : tree call;
21024 :
21025 7634228 : if (TREE_CODE (type) == ARRAY_TYPE)
21026 : addr = decl;
21027 : else
21028 7630940 : addr = build_address (decl);
21029 :
21030 7634228 : call = build_delete (input_location, TREE_TYPE (addr), addr,
21031 : sfk_complete_destructor, flags, 0, complain);
21032 7634228 : if (call == error_mark_node)
21033 : cleanup = error_mark_node;
21034 7634183 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
21035 : /* Discard the call. */;
21036 4946543 : else if (decl_maybe_constant_destruction (decl, type)
21037 4946698 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
21038 155 : cxx_constant_dtor (call, decl);
21039 4946388 : else if (cleanup)
21040 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
21041 : else
21042 : cleanup = call;
21043 : }
21044 :
21045 : /* build_delete sets the location of the destructor call to the
21046 : current location, even though the destructor is going to be
21047 : called later, at the end of the current scope. This can lead to
21048 : a "jumpy" behavior for users of debuggers when they step around
21049 : the end of the block. So let's unset the location of the
21050 : destructor call instead. */
21051 111364497 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
21052 111364497 : if (cleanup && CONVERT_EXPR_P (cleanup))
21053 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
21054 :
21055 0 : if (cleanup
21056 4946577 : && DECL_P (decl)
21057 4946138 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
21058 : /* Treat objects with destructors as used; the destructor may do
21059 : something substantive. */
21060 4946132 : && !mark_used (decl, complain) && !(complain & tf_error))
21061 0 : return error_mark_node;
21062 :
21063 4946577 : if (cleanup && cfun && !processing_template_decl
21064 115836539 : && !expr_noexcept_p (cleanup, tf_none))
21065 5113 : cp_function_chain->throwing_cleanup = true;
21066 :
21067 : return cleanup;
21068 : }
21069 :
21070 :
21071 : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
21072 : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
21073 : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
21074 :
21075 : tree
21076 5937170 : static_fn_type (tree memfntype)
21077 : {
21078 5937170 : tree fntype;
21079 5937170 : tree args;
21080 :
21081 5937170 : if (TYPE_PTRMEMFUNC_P (memfntype))
21082 19951 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
21083 5937170 : if (INDIRECT_TYPE_P (memfntype)
21084 5250123 : || TREE_CODE (memfntype) == FUNCTION_DECL)
21085 5772392 : memfntype = TREE_TYPE (memfntype);
21086 5937170 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
21087 : return memfntype;
21088 894507 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
21089 894507 : args = TYPE_ARG_TYPES (memfntype);
21090 894507 : fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
21091 894507 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
21092 894507 : fntype = (cp_build_type_attribute_variant
21093 894507 : (fntype, TYPE_ATTRIBUTES (memfntype)));
21094 894507 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
21095 894507 : return fntype;
21096 : }
21097 :
21098 : /* DECL was originally constructed as a non-static member function,
21099 : but turned out to be static. Update it accordingly. */
21100 :
21101 : void
21102 168546 : revert_static_member_fn (tree decl)
21103 : {
21104 168546 : tree stype = static_fn_type (decl);
21105 168546 : cp_cv_quals quals = type_memfn_quals (stype);
21106 168546 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
21107 :
21108 168546 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
21109 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
21110 :
21111 168546 : TREE_TYPE (decl) = stype;
21112 :
21113 168546 : if (DECL_ARGUMENTS (decl))
21114 168546 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
21115 168546 : DECL_STATIC_FUNCTION_P (decl) = 1;
21116 168546 : }
21117 :
21118 : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
21119 : one of the language-independent trees. */
21120 :
21121 : enum cp_tree_node_structure_enum
21122 37975286945 : cp_tree_node_structure (union lang_tree_node * t)
21123 : {
21124 37975286945 : switch (TREE_CODE (&t->generic))
21125 : {
21126 : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
21127 157503552 : case BASELINK: return TS_CP_BASELINK;
21128 78585469 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
21129 12078091 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
21130 5780102 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
21131 1979219318 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
21132 3190485 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
21133 250385 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
21134 714103050 : case OVERLOAD: return TS_CP_OVERLOAD;
21135 1104 : case PTRMEM_CST: return TS_CP_PTRMEM;
21136 15372508 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
21137 667193520 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
21138 1569999532 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
21139 673951093 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
21140 14615816 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
21141 6 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
21142 3494406 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
21143 32079948508 : default: return TS_CP_GENERIC;
21144 : }
21145 : }
21146 :
21147 : bool
21148 8995 : cp_missing_noreturn_ok_p (tree decl)
21149 : {
21150 : /* A missing noreturn is ok for the `main' function. */
21151 8995 : return DECL_MAIN_P (decl);
21152 : }
21153 :
21154 : /* Return the decl used to identify the COMDAT group into which DECL should
21155 : be placed. */
21156 :
21157 : tree
21158 143553999 : cxx_comdat_group (tree decl)
21159 : {
21160 : /* Virtual tables, construction virtual tables, and virtual table
21161 : tables all go in a single COMDAT group, named after the primary
21162 : virtual table. */
21163 143553999 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
21164 591372 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
21165 : /* For all other DECLs, the COMDAT group is the mangled name of the
21166 : declaration itself. */
21167 : else
21168 : {
21169 142962627 : while (DECL_THUNK_P (decl))
21170 : {
21171 : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
21172 : into the same section as the target function. In that case
21173 : we must return target's name. */
21174 0 : tree target = THUNK_TARGET (decl);
21175 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
21176 0 : && DECL_SECTION_NAME (target) != NULL
21177 0 : && DECL_ONE_ONLY (target))
21178 : decl = target;
21179 : else
21180 : break;
21181 : }
21182 : /* If a ctor/dtor has already set the comdat group by
21183 : maybe_clone_body, don't override it. */
21184 142962627 : if (SUPPORTS_ONE_ONLY
21185 142962627 : && TREE_CODE (decl) == FUNCTION_DECL
21186 142962627 : && DECL_CLONED_FUNCTION_P (decl))
21187 19933161 : if (tree comdat = DECL_COMDAT_GROUP (decl))
21188 : return comdat;
21189 : }
21190 :
21191 : return decl;
21192 : }
21193 :
21194 : /* Returns the return type for FN as written by the user, which may include
21195 : a placeholder for a deduced return type. */
21196 :
21197 : tree
21198 480306500 : fndecl_declared_return_type (tree fn)
21199 : {
21200 480306500 : fn = STRIP_TEMPLATE (fn);
21201 480306500 : if (FNDECL_USED_AUTO (fn))
21202 117305 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
21203 :
21204 480189195 : return TREE_TYPE (TREE_TYPE (fn));
21205 : }
21206 :
21207 : /* Returns true iff DECL is a variable or function declared with an auto type
21208 : that has not yet been deduced to a real type. */
21209 :
21210 : bool
21211 1205680815 : undeduced_auto_decl (tree decl)
21212 : {
21213 1205680815 : if (cxx_dialect < cxx11)
21214 : return false;
21215 1203333313 : STRIP_ANY_LOCATION_WRAPPER (decl);
21216 587819493 : return ((VAR_OR_FUNCTION_DECL_P (decl)
21217 107372807 : || TREE_CODE (decl) == TEMPLATE_DECL)
21218 1683915565 : && type_uses_auto (TREE_TYPE (decl)));
21219 : }
21220 :
21221 : /* Complain if DECL has an undeduced return type. */
21222 :
21223 : bool
21224 426970505 : require_deduced_type (tree decl, tsubst_flags_t complain)
21225 : {
21226 426970505 : if (undeduced_auto_decl (decl))
21227 : {
21228 9999 : if (TREE_CODE (decl) == FUNCTION_DECL
21229 9972 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
21230 19370 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
21231 : {
21232 : /* Set the return type of __builtin_source_location. */
21233 9371 : tree type = get_source_location_impl_type ();
21234 9371 : if (type == error_mark_node)
21235 : {
21236 33 : inform (input_location, "using %qs", "__builtin_source_location");
21237 33 : return false;
21238 : }
21239 9338 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
21240 9338 : type = build_pointer_type (type);
21241 9338 : apply_deduced_return_type (decl, type);
21242 9338 : return true;
21243 : }
21244 :
21245 628 : if (warning_suppressed_p (decl) && seen_error ())
21246 : /* We probably already complained about deduction failure. */;
21247 475 : else if (complain & tf_error)
21248 70 : error ("use of %qD before deduction of %<auto%>", decl);
21249 628 : note_failed_type_completion (decl, complain);
21250 628 : return false;
21251 : }
21252 : return true;
21253 : }
21254 :
21255 : /* Create a representation of the explicit-specifier with
21256 : constant-expression of EXPR. COMPLAIN is as for tsubst. */
21257 :
21258 : tree
21259 8080969 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
21260 : {
21261 8080969 : if (check_for_bare_parameter_packs (expr))
21262 3 : return error_mark_node;
21263 :
21264 8080966 : if (instantiation_dependent_expression_p (expr))
21265 : /* Wait for instantiation, tsubst_function_decl will handle it. */
21266 : return expr;
21267 :
21268 6549439 : expr = build_converted_constant_bool_expr (expr, complain);
21269 6549439 : expr = instantiate_non_dependent_expr (expr, complain);
21270 6549439 : expr = cxx_constant_value (expr, complain);
21271 6549439 : return expr;
21272 : }
21273 :
21274 : #include "gt-cp-decl.h"
|