Line data Source code
1 : /* Process declarations and variables for the C++ compiler.
2 : Copyright (C) 1988-2026 Free Software Foundation, Inc.
3 : Contributed by Michael Tiemann (tiemann@cygnus.com)
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 :
22 : /* Process declarations and symbol lookup for C++ front end.
23 : Also constructs types; the standard scalar types at initialization,
24 : and structure, union, array and enum types when they are declared. */
25 :
26 : /* ??? not all decl nodes are given the most useful possible
27 : line numbers. For example, the CONST_DECLs for enum values. */
28 :
29 : #include "config.h"
30 : #include "system.h"
31 : #include "coretypes.h"
32 : #include "target.h"
33 : #include "c-family/c-target.h"
34 : #include "cp-tree.h"
35 : #include "timevar.h"
36 : #include "stringpool.h"
37 : #include "cgraph.h"
38 : #include "stor-layout.h"
39 : #include "varasm.h"
40 : #include "attribs.h"
41 : #include "flags.h"
42 : #include "tree-iterator.h"
43 : #include "decl.h"
44 : #include "intl.h"
45 : #include "toplev.h"
46 : #include "c-family/c-objc.h"
47 : #include "c-family/c-pragma.h"
48 : #include "c-family/c-ubsan.h"
49 : #include "cp/cp-name-hint.h"
50 : #include "debug.h"
51 : #include "plugin.h"
52 : #include "builtins.h"
53 : #include "gimplify.h"
54 : #include "asan.h"
55 : #include "gcc-rich-location.h"
56 : #include "langhooks.h"
57 : #include "context.h" /* For 'g'. */
58 : #include "omp-general.h"
59 : #include "omp-offload.h" /* For offload_vars. */
60 : #include "opts.h"
61 : #include "langhooks-def.h" /* For lhd_simulate_record_decl */
62 : #include "coroutines.h"
63 : #include "contracts.h"
64 : #include "gcc-urlifier.h"
65 : #include "diagnostic-highlight-colors.h"
66 : #include "pretty-print-markup.h"
67 :
68 : /* Possible cases of bad specifiers type used by bad_specifiers. */
69 : enum bad_spec_place {
70 : BSP_VAR, /* variable */
71 : BSP_PARM, /* parameter */
72 : BSP_TYPE, /* type */
73 : BSP_FIELD /* field */
74 : };
75 :
76 : static const char *redeclaration_error_message (tree, tree);
77 :
78 : static bool decl_jump_unsafe (tree);
79 : static bool decl_instrument_init_bypass_p (tree);
80 : static void require_complete_types_for_parms (tree);
81 : static tree grok_reference_init (tree, tree, tree, int);
82 : static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
83 : int, int, int, bool, int, tree, location_t);
84 : static void check_static_variable_definition (tree, tree);
85 : static void record_unknown_type (tree, const char *);
86 : static int member_function_or_else (tree, tree, enum overload_flags);
87 : static tree local_variable_p_walkfn (tree *, int *, void *);
88 : static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
89 : static void maybe_deduce_size_from_array_init (tree, tree);
90 : static void layout_var_decl (tree);
91 : static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
92 : static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
93 : static void copy_type_enum (tree , tree);
94 : static void check_function_type (tree, tree);
95 : static void begin_destructor_body (void);
96 : static void record_key_method_defined (tree);
97 : static tree create_array_type_for_decl (tree, tree, tree, location_t);
98 : static tree get_atexit_node (void);
99 : static tree get_dso_handle_node (void);
100 : static tree start_cleanup_fn (tree, bool, bool);
101 : static void end_cleanup_fn (void);
102 : static tree cp_make_fname_decl (location_t, tree, int);
103 : static void initialize_predefined_identifiers (void);
104 : static tree check_special_function_return_type
105 : (special_function_kind, tree, tree, int, const cp_declarator**,
106 : const location_t*);
107 : static tree push_cp_library_fn (enum tree_code, tree, int);
108 : static tree build_cp_library_fn (tree, enum tree_code, tree, int);
109 : static void store_parm_decls (tree);
110 : static void initialize_local_var (tree, tree, bool);
111 : static void expand_static_init (tree, tree);
112 : static location_t smallest_type_location (const cp_decl_specifier_seq*);
113 : static bool identify_goto (tree, location_t, const location_t *,
114 : enum diagnostics::kind, bool);
115 :
116 : /* The following symbols are subsumed in the cp_global_trees array, and
117 : listed here individually for documentation purposes.
118 :
119 : C++ extensions
120 : tree wchar_decl_node;
121 :
122 : tree vtable_entry_type;
123 : tree delta_type_node;
124 : tree __t_desc_type_node;
125 :
126 : tree class_type_node;
127 : tree unknown_type_node;
128 :
129 : Array type `vtable_entry_type[]'
130 :
131 : tree vtbl_type_node;
132 : tree vtbl_ptr_type_node;
133 :
134 : Namespaces,
135 :
136 : tree std_node;
137 : tree std_meta_node;
138 : tree abi_node;
139 :
140 : A FUNCTION_DECL which can call `abort'. Not necessarily the
141 : one that the user will declare, but sufficient to be called
142 : by routines that want to abort the program.
143 :
144 : tree abort_fndecl;
145 :
146 : Used by RTTI
147 : tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
148 : tree tinfo_var_id; */
149 :
150 : tree cp_global_trees[CPTI_MAX];
151 :
152 : /* A list of objects which have constructors or destructors
153 : which reside in namespace scope. The decl is stored in
154 : the TREE_VALUE slot and the initializer is stored
155 : in the TREE_PURPOSE slot. */
156 : tree static_aggregates;
157 :
158 : /* Like static_aggregates, but for thread_local variables. */
159 : tree tls_aggregates;
160 :
161 : /* A hash-map mapping from variable decls to the dynamic initializer for
162 : the decl. This is currently only used by OpenMP. */
163 : decl_tree_map *dynamic_initializers;
164 :
165 : /* -- end of C++ */
166 :
167 : /* A node for the integer constant 2. */
168 :
169 : tree integer_two_node;
170 :
171 : /* vector of static decls. */
172 : vec<tree, va_gc> *static_decls;
173 :
174 : /* vector of keyed classes. */
175 : vec<tree, va_gc> *keyed_classes;
176 :
177 : /* Used in the direct_goto vector of named_label_use_entry for
178 : addresses of the LABEL_DECLs within GOTO_EXPR or asm goto
179 : for forward jumps. */
180 :
181 : struct GTY(()) named_label_fwd_direct_goto {
182 : tree *GTY((skip)) direct_goto;
183 : };
184 :
185 : /* Used in the direct_goto vector of named_label_use_entry for
186 : addresses of the LABEL_DECLs within GOTO_EXPR or asm goto
187 : for backward jumps. */
188 :
189 : struct GTY(()) named_label_bck_direct_goto {
190 : tree *GTY((skip)) direct_goto;
191 : /* Number of the decl_instrument_init_bypass_p decls in bad_decls vector
192 : at the time this backward goto has been seen. */
193 : unsigned n_bad_decls;
194 : };
195 :
196 : /* Used only for jumps to as-yet undefined labels, since jumps to
197 : defined labels can have their validity checked immediately. */
198 :
199 : struct GTY((chain_next ("%h.next"))) named_label_use_entry {
200 : struct named_label_use_entry *next;
201 : /* The binding level to which this entry is *currently* attached.
202 : This is initially the binding level in which the goto appeared,
203 : but is modified as scopes are closed. */
204 : cp_binding_level *binding_level;
205 : /* The head of the names list that was current when the goto appeared,
206 : or the inner scope popped. These are the decls that will *not* be
207 : skipped when jumping to the label. */
208 : tree names_in_scope;
209 : /* If the use is a possible destination of a computed goto, a vec of decls
210 : that aren't destroyed, filled in by poplevel_named_label_1. */
211 : vec<tree, va_gc> *computed_goto;
212 : /* If the use is a destination of normal goto, a vec of addresses of
213 : LABEL_DECLs that might need changing for !!flag_auto_var_init
214 : forward jumps across vacuous initializers. */
215 : vec<named_label_fwd_direct_goto, va_gc> *direct_goto;
216 : /* The location of the goto, for error reporting. */
217 : location_t o_goto_locus;
218 : /* True if an OpenMP structured block scope has been closed since
219 : the goto appeared. This means that the branch from the label will
220 : illegally exit an OpenMP scope. */
221 : bool in_omp_scope;
222 : };
223 :
224 : /* A list of all LABEL_DECLs in the function that have names. Here so
225 : we can clear out their names' definitions at the end of the
226 : function, and so we can check the validity of jumps to these labels. */
227 :
228 : struct GTY((for_user)) named_label_entry {
229 :
230 : tree name; /* Name of decl. */
231 :
232 : tree label_decl; /* LABEL_DECL, unless deleted local label. */
233 :
234 : named_label_entry *outer; /* Outer shadowed chain. */
235 :
236 : /* The binding level to which the label is *currently* attached.
237 : This is initially set to the binding level in which the label
238 : is defined, but is modified as scopes are closed. */
239 : cp_binding_level *binding_level;
240 :
241 : /* The head of the names list that was current when the label was
242 : defined, or the inner scope popped. These are the decls that will
243 : be skipped when jumping to the label. */
244 : tree names_in_scope;
245 :
246 : /* A vector of all decls from all binding levels that would be
247 : crossed by a backward branch to the label. */
248 : vec<tree, va_gc> *bad_decls;
249 :
250 : /* A list of uses of the label, before the label is defined. */
251 : named_label_use_entry *uses;
252 :
253 : /* If the use is a destination of normal goto, a vec of addresses of
254 : LABEL_DECLs that might need changing for !!flag_auto_var_init
255 : backward jumps across vacuous initializers. */
256 : vec<named_label_bck_direct_goto, va_gc> *direct_goto;
257 :
258 : /* True if we've seen &&label. Apparently we can't use TREE_ADDRESSABLE for
259 : this, it has a more specific meaning for LABEL_DECL. */
260 : bool addressed : 1;
261 :
262 : /* The following bits are set after the label is defined, and are
263 : updated as scopes are popped. They indicate that a jump to the
264 : label will illegally enter a scope of the given flavor. */
265 : bool in_try_scope : 1;
266 : bool in_catch_scope : 1;
267 : bool in_omp_scope : 1;
268 : bool in_transaction_scope : 1;
269 : bool in_constexpr_if : 1;
270 : bool in_consteval_if : 1;
271 : bool in_stmt_expr : 1;
272 :
273 : /* True if bad_decls chain contains any decl_jump_unsafe decls
274 : (rather than just decl_instrument_init_bypass_p). */
275 : bool has_bad_decls : 1;
276 : };
277 :
278 : #define named_labels cp_function_chain->x_named_labels
279 :
280 : /* The number of function bodies which we are currently processing.
281 : (Zero if we are at namespace scope, one inside the body of a
282 : function, two inside the body of a function in a local class, etc.) */
283 : int function_depth;
284 :
285 : /* Whether the exception-specifier is part of a function type (i.e. C++17). */
286 : bool flag_noexcept_type;
287 :
288 : /* States indicating how grokdeclarator() should handle declspecs marked
289 : with __attribute__((deprecated)). An object declared as
290 : __attribute__((deprecated)) suppresses warnings of uses of other
291 : deprecated items. */
292 : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
293 :
294 :
295 : /* A list of VAR_DECLs whose type was incomplete at the time the
296 : variable was declared. */
297 :
298 : struct GTY(()) incomplete_var {
299 : tree decl;
300 : tree incomplete_type;
301 : };
302 :
303 :
304 : static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
305 :
306 : /* Returns the kind of template specialization we are currently
307 : processing, given that it's declaration contained N_CLASS_SCOPES
308 : explicit scope qualifications. */
309 :
310 : tmpl_spec_kind
311 260764034 : current_tmpl_spec_kind (int n_class_scopes)
312 : {
313 260764034 : int n_template_parm_scopes = 0;
314 260764034 : int seen_specialization_p = 0;
315 260764034 : int innermost_specialization_p = 0;
316 260764034 : cp_binding_level *b;
317 :
318 : /* Scan through the template parameter scopes. */
319 260764034 : for (b = current_binding_level;
320 325276018 : b->kind == sk_template_parms;
321 64511984 : 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 64511984 : if (b->explicit_spec_p)
333 : {
334 1553104 : if (n_template_parm_scopes == 0)
335 : innermost_specialization_p = 1;
336 : else
337 1351 : seen_specialization_p = 1;
338 : }
339 62958880 : else if (seen_specialization_p == 1)
340 : return tsk_invalid_member_spec;
341 :
342 64511984 : ++n_template_parm_scopes;
343 : }
344 :
345 : /* Handle explicit instantiations. */
346 260764034 : if (processing_explicit_instantiation)
347 : {
348 2059194 : 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 2059194 : return tsk_expl_inst;
358 : }
359 :
360 258704840 : 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 258704840 : 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 56120761 : 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 56120761 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394 : }
395 :
396 : /* Exit the current scope. */
397 :
398 : void
399 102432808 : finish_scope (void)
400 : {
401 102432808 : poplevel (0, 0, 0);
402 102432808 : }
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 20967 : check_label_used (tree label)
409 : {
410 20967 : if (!processing_template_decl)
411 : {
412 20658 : 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 20649 : warn_for_unused_label (label);
424 : }
425 20967 : }
426 :
427 : /* Helper function to sort named label entries in a vector by DECL_UID. */
428 :
429 : static int
430 235747 : sort_labels (const void *a, const void *b)
431 : {
432 235747 : tree label1 = *(tree const *) a;
433 235747 : tree label2 = *(tree const *) b;
434 :
435 : /* DECL_UIDs can never be equal. */
436 235747 : 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 168707804 : pop_labels (tree block)
508 : {
509 168707804 : if (!named_labels)
510 168703087 : 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 4717 : auto_vec<tree, 32> labels (named_labels->elements ());
518 4717 : hash_table<named_label_hash>::iterator end (named_labels->end ());
519 :
520 4717 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
521 720 : && !processing_template_decl)
522 : {
523 3920 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
524 : {
525 2544 : named_label_entry *ent = *iter;
526 2544 : 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 25651 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
537 : {
538 20934 : named_label_entry *ent = *iter;
539 :
540 20934 : gcc_checking_assert (!ent->outer);
541 20934 : if (ent->label_decl)
542 20748 : labels.quick_push (ent->label_decl);
543 20934 : ggc_free (ent);
544 : }
545 4717 : named_labels = NULL;
546 4717 : labels.qsort (sort_labels);
547 :
548 25465 : while (labels.length ())
549 : {
550 20748 : tree label = labels.pop ();
551 :
552 20748 : DECL_CHAIN (label) = BLOCK_VARS (block);
553 20748 : BLOCK_VARS (block) = label;
554 :
555 20748 : check_label_used (label);
556 : }
557 4717 : }
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 1799879 : level_for_constexpr_if (cp_binding_level *b)
618 : {
619 1795739 : return (b->kind == sk_cond && b->this_entity
620 302 : && TREE_CODE (b->this_entity) == IF_STMT
621 1800181 : && 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 1799855 : level_for_consteval_if (cp_binding_level *b)
628 : {
629 1795715 : return (b->kind == sk_cond && b->this_entity
630 278 : && TREE_CODE (b->this_entity) == IF_STMT
631 1800133 : && 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 156964 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
652 : {
653 156964 : named_label_entry *ent = *slot;
654 156964 : cp_binding_level *obl = bl->level_chain;
655 :
656 156964 : if (ent->binding_level == bl)
657 : {
658 10337 : tree decl;
659 :
660 : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
661 : TREE_LISTs representing OVERLOADs, so be careful. */
662 15294 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
663 5030 : ? DECL_CHAIN (decl)
664 73 : : TREE_CHAIN (decl)))
665 4957 : if (decl_jump_unsafe (decl))
666 : {
667 1906 : vec_safe_push (ent->bad_decls, decl);
668 1906 : ent->has_bad_decls = true;
669 : }
670 3051 : else if (decl_instrument_init_bypass_p (decl))
671 442 : vec_safe_push (ent->bad_decls, decl);
672 :
673 10337 : ent->binding_level = obl;
674 10337 : ent->names_in_scope = obl->names;
675 10337 : switch (bl->kind)
676 : {
677 42 : case sk_try:
678 42 : ent->in_try_scope = true;
679 42 : break;
680 27 : case sk_catch:
681 27 : ent->in_catch_scope = true;
682 27 : break;
683 324 : case sk_omp:
684 324 : ent->in_omp_scope = true;
685 324 : break;
686 3 : case sk_transaction:
687 3 : ent->in_transaction_scope = true;
688 3 : break;
689 45 : case sk_stmt_expr:
690 45 : ent->in_stmt_expr = true;
691 45 : break;
692 4188 : case sk_block:
693 4188 : if (level_for_constexpr_if (obl))
694 15 : ent->in_constexpr_if = true;
695 4173 : else if (level_for_consteval_if (obl))
696 30 : ent->in_consteval_if = true;
697 : break;
698 : default:
699 : break;
700 : }
701 : }
702 146627 : else if (ent->uses)
703 : {
704 : struct named_label_use_entry *use;
705 :
706 23019 : for (use = ent->uses; use ; use = use->next)
707 15614 : if (use->binding_level == bl)
708 : {
709 6045 : if (auto &cg = use->computed_goto)
710 : {
711 57 : if (bl->kind == sk_catch)
712 3 : vec_safe_push (cg, get_identifier ("catch"));
713 66 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
714 9 : if (automatic_var_with_nontrivial_dtor_p (d))
715 6 : vec_safe_push (cg, d);
716 : }
717 :
718 6045 : use->binding_level = obl;
719 6045 : use->names_in_scope = obl->names;
720 6045 : if (bl->kind == sk_omp)
721 12 : use->in_omp_scope = true;
722 : }
723 : }
724 :
725 156964 : 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 708934716 : poplevel (int keep, int reverse, int functionbody)
749 : {
750 708934716 : tree link;
751 : /* The chain of decls was accumulated in reverse order.
752 : Put it into forward order, just for cleanliness. */
753 708934716 : tree decls;
754 708934716 : tree subblocks;
755 708934716 : tree block;
756 708934716 : tree decl;
757 708934716 : scope_kind kind;
758 :
759 708934716 : auto_cond_timevar tv (TV_NAME_LOOKUP);
760 708935172 : restart:
761 :
762 708935172 : block = NULL_TREE;
763 :
764 708935172 : gcc_assert (current_binding_level->kind != sk_class
765 : && current_binding_level->kind != sk_namespace);
766 :
767 708935172 : if (current_binding_level->kind == sk_cleanup)
768 : functionbody = 0;
769 708935172 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
770 :
771 708935172 : 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 708935172 : gcc_assert (keep == 0 || keep == 1);
777 :
778 708935172 : if (current_binding_level->keep)
779 8751335 : keep = 1;
780 :
781 : /* Any uses of undefined labels, and any defined labels, now operate
782 : under constraints of next binding contour. */
783 708935172 : if (cfun && !functionbody && named_labels)
784 43999 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
785 200963 : (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 708935172 : decls = current_binding_level->names;
792 708935172 : if (reverse)
793 : {
794 389050122 : decls = nreverse (decls);
795 389050122 : 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 708935172 : block = NULL_TREE;
802 : /* Avoid function body block if possible. */
803 751862254 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
804 : keep = 0;
805 666008354 : else if (keep == 1 || functionbody)
806 212915005 : block = make_node (BLOCK);
807 212915005 : if (block != NULL_TREE)
808 : {
809 212915005 : BLOCK_VARS (block) = decls;
810 212915005 : BLOCK_SUBBLOCKS (block) = subblocks;
811 : }
812 :
813 : /* In each subblock, record that this is its superior. */
814 708935172 : if (keep >= 0)
815 796068456 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
816 87133284 : BLOCK_SUPERCONTEXT (link) = block;
817 :
818 : /* Before we remove the declarations first check for unused variables. */
819 708935172 : if ((warn_unused_variable || warn_unused_but_set_variable)
820 6778163 : && current_binding_level->kind != sk_template_parms
821 5714412 : && !processing_template_decl)
822 6565691 : 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 3327304 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
828 3327304 : decl = strip_using_decl (udecl);
829 :
830 3327304 : tree type = TREE_TYPE (decl);
831 3327304 : if (VAR_P (decl)
832 388571 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
833 3613 : && !DECL_IN_SYSTEM_HEADER (udecl)
834 : /* For structured bindings, consider only real variables, not
835 : subobjects. */
836 3601 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
837 3378 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
838 : /* Don't warn about name-independent declarations. */
839 899 : && !name_independent_decl_p (decl)
840 743 : && type != error_mark_node
841 3331614 : && (!CLASS_TYPE_P (type)
842 51 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
843 3 : || lookup_attribute ("warn_unused",
844 3 : TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
845 : {
846 709 : if (!TREE_USED (decl))
847 : {
848 308 : if (TREE_CODE (udecl) == USING_DECL)
849 3 : warning_at (DECL_SOURCE_LOCATION (udecl),
850 3 : OPT_Wunused_variable,
851 : "unused using-declaration %qD", udecl);
852 305 : else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
853 3 : warning_at (DECL_SOURCE_LOCATION (decl),
854 3 : OPT_Wunused_variable,
855 : "unused structured binding declaration");
856 : else
857 302 : warning_at (DECL_SOURCE_LOCATION (decl),
858 302 : OPT_Wunused_variable, "unused variable %qD", decl);
859 308 : suppress_warning (decl, OPT_Wunused_variable);
860 : }
861 401 : else if (DECL_CONTEXT (decl) == current_function_decl
862 : // For -Wunused-but-set-variable leave references alone.
863 377 : && !TYPE_REF_P (TREE_TYPE (decl))
864 778 : && errorcount == unused_but_set_errorcount)
865 : {
866 368 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
867 12 : warning_at (DECL_SOURCE_LOCATION (decl),
868 12 : OPT_Wunused_but_set_variable_, "structured "
869 : "binding declaration set but not used");
870 : else
871 356 : warning_at (DECL_SOURCE_LOCATION (decl),
872 356 : OPT_Wunused_but_set_variable_,
873 : "variable %qD set but not used", decl);
874 368 : unused_but_set_errorcount = errorcount;
875 : }
876 : }
877 : }
878 :
879 : /* Remove declarations for all the DECLs in this level. */
880 1433098927 : for (link = decls; link; link = TREE_CHAIN (link))
881 : {
882 724163755 : tree name;
883 724163755 : if (TREE_CODE (link) == TREE_LIST)
884 : {
885 13926541 : decl = TREE_VALUE (link);
886 13926541 : name = TREE_PURPOSE (link);
887 13926541 : gcc_checking_assert (name);
888 : }
889 : else
890 : {
891 710237214 : decl = link;
892 710237214 : name = DECL_NAME (decl);
893 : }
894 :
895 : /* Remove the binding. */
896 724163755 : if (TREE_CODE (decl) == LABEL_DECL)
897 219 : pop_local_label (name, decl);
898 : else
899 724163536 : pop_local_binding (name, decl);
900 : }
901 :
902 : /* Restore the IDENTIFIER_TYPE_VALUEs. */
903 708935172 : for (link = current_binding_level->type_shadowed;
904 966419397 : link; link = TREE_CHAIN (link))
905 257484225 : 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 708935172 : if (block)
915 : {
916 212915005 : tree* d;
917 :
918 560713433 : for (d = &BLOCK_VARS (block); *d; )
919 : {
920 347798428 : if (TREE_CODE (*d) == TREE_LIST
921 347798428 : || (!processing_template_decl
922 110175967 : && undeduced_auto_decl (*d)))
923 13926593 : *d = TREE_CHAIN (*d);
924 : else
925 333871835 : 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 708935172 : if (functionbody)
932 : {
933 168707804 : 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 125780986 : BLOCK_VARS (block) = 0;
939 125780986 : pop_labels (block);
940 : }
941 : else
942 42926818 : pop_labels (subblocks);
943 : }
944 :
945 708935172 : kind = current_binding_level->kind;
946 708935172 : if (kind == sk_cleanup)
947 : {
948 456 : 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 456 : stmt = pop_stmt_list (current_binding_level->statement_list);
954 456 : stmt = c_build_bind_expr (input_location, block, stmt);
955 456 : add_stmt (stmt);
956 : }
957 :
958 708935172 : leave_scope ();
959 708935172 : if (functionbody)
960 : {
961 : /* The current function is being defined, so its DECL_INITIAL
962 : should be error_mark_node. */
963 168707804 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
964 211634622 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
965 168707804 : if (subblocks)
966 : {
967 81654904 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
968 53367941 : && BLOCK_SUBBLOCKS (subblocks))
969 1395366 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
970 : else
971 41531716 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
972 : }
973 : }
974 540227368 : else if (block)
975 87134019 : current_binding_level->blocks
976 174268038 : = 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 453093349 : 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 708935172 : if (block)
991 212915005 : TREE_USED (block) = 1;
992 :
993 : /* All temporary bindings created for cleanups are popped silently. */
994 708935172 : if (kind == sk_cleanup)
995 456 : goto restart;
996 :
997 1417869432 : return block;
998 708934716 : }
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 147387 : wrapup_namespace_globals ()
1006 : {
1007 147387 : if (vec<tree, va_gc> *statics = static_decls)
1008 : {
1009 18480066 : for (tree decl : *statics)
1010 : {
1011 18359611 : if (warn_unused_function
1012 1014807 : && TREE_CODE (decl) == FUNCTION_DECL
1013 765465 : && DECL_INITIAL (decl) == 0
1014 7626 : && DECL_EXTERNAL (decl)
1015 7614 : && !TREE_PUBLIC (decl)
1016 34 : && !DECL_ARTIFICIAL (decl)
1017 34 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1018 9 : && !warning_suppressed_p (decl, OPT_Wunused)
1019 19374418 : && 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 18359611 : if (VAR_P (decl)
1025 6064629 : && DECL_EXTERNAL (decl)
1026 : /* We mark consteval-only variables DECL_EXTERNAL, but
1027 : extern constexpr inline std::meta::info i{};
1028 : is a definition (the extern is redundant). */
1029 2087722 : && !DECL_INITIAL (decl)
1030 2086309 : && DECL_INLINE_VAR_P (decl)
1031 18359640 : && DECL_ODR_USED (decl))
1032 3 : error_at (DECL_SOURCE_LOCATION (decl),
1033 : "odr-used inline variable %qD is not defined", decl);
1034 : }
1035 :
1036 : /* Clear out the list, so we don't rescan next time. */
1037 120455 : static_decls = NULL;
1038 :
1039 : /* Write out any globals that need to be output. */
1040 120455 : return wrapup_global_declarations (statics->address (),
1041 120455 : statics->length ());
1042 : }
1043 : return 0;
1044 : }
1045 :
1046 : /* In C++, you don't have to write `struct S' to refer to `S'; you
1047 : can just use `S'. We accomplish this by creating a TYPE_DECL as
1048 : if the user had written `typedef struct S S'. Create and return
1049 : the TYPE_DECL for TYPE. */
1050 :
1051 : tree
1052 190785287 : create_implicit_typedef (tree name, tree type)
1053 : {
1054 190785287 : tree decl;
1055 :
1056 190785287 : decl = build_decl (input_location, TYPE_DECL, name, type);
1057 190785287 : DECL_ARTIFICIAL (decl) = 1;
1058 : /* There are other implicit type declarations, like the one *within*
1059 : a class that allows you to write `S::S'. We must distinguish
1060 : amongst these. */
1061 190785287 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1062 190785287 : TYPE_NAME (type) = decl;
1063 190785287 : TYPE_STUB_DECL (type) = decl;
1064 :
1065 190785287 : return decl;
1066 : }
1067 :
1068 : /* Function-scope local entities that need discriminators. Each entry
1069 : is a {decl,name} pair. VAR_DECLs for anon unions get their name
1070 : smashed, so we cannot rely on DECL_NAME. */
1071 :
1072 : static GTY((deletable)) vec<tree, va_gc> *local_entities;
1073 :
1074 : /* Determine the mangling discriminator of local DECL. There are
1075 : generally very few of these in any particular function. */
1076 :
1077 : void
1078 777626 : determine_local_discriminator (tree decl, tree name)
1079 : {
1080 777626 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1081 777626 : retrofit_lang_decl (decl);
1082 777626 : tree ctx = DECL_CONTEXT (decl);
1083 777626 : size_t nelts = vec_safe_length (local_entities);
1084 777626 : if (name == NULL_TREE)
1085 777359 : name = (TREE_CODE (decl) == TYPE_DECL
1086 1272849 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
1087 1409714 : ? NULL_TREE : DECL_NAME (decl));
1088 22841423 : for (size_t i = 0; i < nelts; i += 2)
1089 : {
1090 22064477 : tree *pair = &(*local_entities)[i];
1091 22064477 : tree d = pair[0];
1092 22064477 : tree n = pair[1];
1093 22064477 : gcc_checking_assert (d != decl);
1094 22064477 : if (name == n
1095 3978237 : && TREE_CODE (decl) == TREE_CODE (d)
1096 26042705 : && ctx == DECL_CONTEXT (d))
1097 : {
1098 680 : tree disc = integer_one_node;
1099 680 : if (DECL_DISCRIMINATOR (d))
1100 375 : disc = build_int_cst (TREE_TYPE (disc),
1101 375 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
1102 680 : DECL_DISCRIMINATOR (decl) = disc;
1103 : /* Replace the saved decl. */
1104 680 : pair[0] = decl;
1105 680 : decl = NULL_TREE;
1106 680 : break;
1107 : }
1108 : }
1109 :
1110 777626 : if (decl)
1111 : {
1112 776946 : vec_safe_reserve (local_entities, 2);
1113 776946 : local_entities->quick_push (decl);
1114 776946 : local_entities->quick_push (name);
1115 : }
1116 777626 : }
1117 :
1118 :
1119 : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
1120 :
1121 : A non-template friend declaration with a requires-clause shall be a
1122 : definition. A friend function template with a constraint that depends on a
1123 : template parameter from an enclosing template shall be a definition. Such a
1124 : constrained friend function or function template declaration does not
1125 : declare the same function or function template as a declaration in any other
1126 : scope.
1127 :
1128 : The ABI calls this a "member-like constrained friend" and mangles it like a
1129 : member function to avoid collisions. */
1130 :
1131 : bool
1132 2613046991 : member_like_constrained_friend_p (tree decl)
1133 : {
1134 2613046991 : return (TREE_CODE (decl) == FUNCTION_DECL
1135 893452005 : && DECL_UNIQUE_FRIEND_P (decl)
1136 294877180 : && DECL_FRIEND_CONTEXT (decl)
1137 129613222 : && get_constraints (decl)
1138 18764464 : && CLASSTYPE_IMPLICIT_INSTANTIATION (DECL_FRIEND_CONTEXT (decl))
1139 2622407377 : && (!DECL_TEMPLATE_INFO (decl)
1140 9360386 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1141 8729 : || (uses_outer_template_parms_in_constraints
1142 8729 : (most_general_template (decl)))));
1143 : }
1144 :
1145 : /* Returns true if functions FN1 and FN2 have equivalent trailing
1146 : requires clauses. */
1147 :
1148 : static bool
1149 70714615 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1150 : {
1151 : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1152 : same function as a declaration in any other scope." So no need to
1153 : actually compare the requirements. */
1154 70714615 : if (member_like_constrained_friend_p (newfn)
1155 70714615 : || member_like_constrained_friend_p (oldfn))
1156 : return false;
1157 :
1158 : /* Compare only trailing requirements. */
1159 70714494 : tree reqs1 = get_trailing_function_requirements (newfn);
1160 70714494 : tree reqs2 = get_trailing_function_requirements (oldfn);
1161 70714494 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1162 : return false;
1163 :
1164 : /* Substitution is needed when friends are involved. */
1165 70576707 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1166 70576707 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1167 :
1168 70576707 : return cp_tree_equal (reqs1, reqs2);
1169 : }
1170 :
1171 : /* Two functions of the same name correspond [basic.scope.scope] if
1172 :
1173 : + both declare functions with the same non-object-parameter-type-list,
1174 : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1175 : specified in [temp.friend]), and, if both are non-static members, they have
1176 : corresponding object parameters, or
1177 :
1178 : + both declare function templates with equivalent
1179 : non-object-parameter-type-lists, return types (if any), template-heads, and
1180 : trailing requires-clauses (if any), and, if both are non-static members,
1181 : they have corresponding object parameters.
1182 :
1183 : This is a subset of decls_match: it identifies declarations that cannot be
1184 : overloaded with one another. This function does not consider DECL_NAME. */
1185 :
1186 : bool
1187 1510622242 : fns_correspond (tree newdecl, tree olddecl)
1188 : {
1189 1510622242 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1190 : return false;
1191 :
1192 1403603542 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1193 : {
1194 1402245912 : if (!template_heads_equivalent_p (newdecl, olddecl))
1195 : return 0;
1196 332619250 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1197 332619250 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1198 : }
1199 :
1200 333976880 : tree f1 = TREE_TYPE (newdecl);
1201 333976880 : tree f2 = TREE_TYPE (olddecl);
1202 :
1203 333976880 : int rq1 = type_memfn_rqual (f1);
1204 333976880 : int rq2 = type_memfn_rqual (f2);
1205 :
1206 : /* If only one is a non-static member function, ignore ref-quals. */
1207 333976880 : if (TREE_CODE (f1) != TREE_CODE (f2))
1208 : rq1 = rq2;
1209 : /* Two non-static member functions have corresponding object parameters if:
1210 : + exactly one is an implicit object member function with no ref-qualifier
1211 : and the types of their object parameters ([dcl.fct]), after removing
1212 : top-level references, are the same, or
1213 : + their object parameters have the same type. */
1214 : /* ??? We treat member functions of different classes as corresponding even
1215 : though that means the object parameters have different types. */
1216 333976880 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1217 0 : rq1 = rq2;
1218 :
1219 333976880 : bool types_match = rq1 == rq2;
1220 :
1221 333976880 : if (types_match)
1222 : {
1223 333976880 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1224 333976880 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1225 333976880 : types_match = compparms (p1, p2);
1226 : }
1227 :
1228 : /* Two function declarations match if either has a requires-clause
1229 : then both have a requires-clause and their constraints-expressions
1230 : are equivalent. */
1231 333976880 : if (types_match && flag_concepts)
1232 41239828 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1233 :
1234 : return types_match;
1235 : }
1236 :
1237 : /* Subroutine of duplicate_decls: return truthvalue of whether
1238 : or not types of these decls match.
1239 :
1240 : For C++, we must compare the parameter list so that `int' can match
1241 : `int&' in a parameter position, but `int&' is not confused with
1242 : `const int&'. */
1243 :
1244 : int
1245 826707672 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1246 : {
1247 877515658 : int types_match;
1248 :
1249 877515658 : if (newdecl == olddecl)
1250 : return 1;
1251 :
1252 877394954 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1253 : /* If the two DECLs are not even the same kind of thing, we're not
1254 : interested in their types. */
1255 : return 0;
1256 :
1257 462806973 : gcc_assert (DECL_P (newdecl));
1258 :
1259 462806973 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1260 : {
1261 : /* Specializations of different templates are different functions
1262 : even if they have the same type. */
1263 305773991 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1264 305773991 : ? DECL_TI_TEMPLATE (newdecl)
1265 666198 : : NULL_TREE);
1266 305773991 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1267 305773991 : ? DECL_TI_TEMPLATE (olddecl)
1268 4242657 : : NULL_TREE);
1269 305773991 : if (t1 != t2)
1270 : return 0;
1271 :
1272 302197449 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1273 302197449 : && ! (DECL_EXTERN_C_P (newdecl)
1274 3026111 : && DECL_EXTERN_C_P (olddecl)))
1275 : return 0;
1276 :
1277 : /* A new declaration doesn't match a built-in one unless it
1278 : is also extern "C". */
1279 280118757 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1280 280118757 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1281 : return 0;
1282 :
1283 279452102 : tree f1 = TREE_TYPE (newdecl);
1284 279452102 : tree f2 = TREE_TYPE (olddecl);
1285 279452102 : if (TREE_CODE (f1) != TREE_CODE (f2))
1286 : return 0;
1287 :
1288 : /* A declaration with deduced return type should use its pre-deduction
1289 : type for declaration matching. */
1290 279270026 : tree r2 = fndecl_declared_return_type (olddecl);
1291 279270026 : tree r1 = fndecl_declared_return_type (newdecl);
1292 :
1293 : /* For memchr et al, allow const void* return type (as specified by C++)
1294 : when we expect void* (as in C). */
1295 279270026 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1296 6731624 : && DECL_EXTERN_C_P (olddecl)
1297 4141873 : && !same_type_p (r1, r2)
1298 70 : && TREE_CODE (r1) == POINTER_TYPE
1299 15 : && TREE_CODE (r2) == POINTER_TYPE
1300 279270038 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1301 : r2 = r1;
1302 :
1303 279270026 : tree p1 = TYPE_ARG_TYPES (f1);
1304 279270026 : tree p2 = TYPE_ARG_TYPES (f2);
1305 :
1306 279270026 : if (same_type_p (r1, r2))
1307 : {
1308 179152700 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1309 179152672 : && fndecl_built_in_p (olddecl))
1310 : {
1311 50 : types_match = self_promoting_args_p (p1);
1312 50 : if (p1 == void_list_node)
1313 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1314 : }
1315 : else
1316 179152566 : types_match =
1317 179152566 : compparms (p1, p2)
1318 29982470 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1319 209134640 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1320 1280041 : || comp_type_attributes (TREE_TYPE (newdecl),
1321 1280041 : TREE_TYPE (olddecl)) != 0);
1322 : }
1323 : else
1324 : types_match = 0;
1325 :
1326 : /* Two function declarations match if either has a requires-clause
1327 : then both have a requires-clause and their constraints-expressions
1328 : are equivalent. */
1329 29982103 : if (types_match && flag_concepts)
1330 29474755 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1331 :
1332 : /* The decls dont match if they correspond to two different versions
1333 : of the same function. Disallow extern "C" functions to be
1334 : versions for now. */
1335 29982103 : if (types_match
1336 29776363 : && !DECL_EXTERN_C_P (newdecl)
1337 21747447 : && !DECL_EXTERN_C_P (olddecl)
1338 51725546 : && disjoint_version_decls (newdecl, olddecl))
1339 : {
1340 8880 : if (record_versions)
1341 8793 : maybe_version_functions (newdecl, olddecl);
1342 : return 0;
1343 : }
1344 : }
1345 157032982 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1346 : {
1347 155212882 : if (!template_heads_equivalent_p (newdecl, olddecl))
1348 : return 0;
1349 :
1350 50808022 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1351 50808022 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1352 :
1353 50808022 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1354 : return 0;
1355 :
1356 : /* Two template types match if they are the same. Otherwise, compare
1357 : the underlying declarations. */
1358 50808016 : if (TREE_CODE (newres) == TYPE_DECL)
1359 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1360 : else
1361 : types_match = decls_match (newres, oldres);
1362 : }
1363 : else
1364 : {
1365 : /* Need to check scope for variable declaration (VAR_DECL).
1366 : For typedef (TYPE_DECL), scope is ignored. */
1367 1820100 : if (VAR_P (newdecl)
1368 1245918 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1369 : /* [dcl.link]
1370 : Two declarations for an object with C language linkage
1371 : with the same name (ignoring the namespace that qualify
1372 : it) that appear in different namespace scopes refer to
1373 : the same object. */
1374 2013727 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1375 : return 0;
1376 :
1377 1626521 : if (TREE_TYPE (newdecl) == error_mark_node)
1378 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1379 1626521 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1380 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1381 1626521 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1382 : types_match = 0;
1383 : else
1384 1626521 : types_match = comptypes (TREE_TYPE (newdecl),
1385 1626521 : TREE_TYPE (olddecl),
1386 : COMPARE_REDECLARATION);
1387 : }
1388 :
1389 : return types_match;
1390 : }
1391 :
1392 : /* Mark DECL as versioned if it isn't already. */
1393 :
1394 : void
1395 17676 : maybe_mark_function_versioned (tree decl)
1396 : {
1397 17676 : if (!DECL_FUNCTION_VERSIONED (decl))
1398 : {
1399 : /* We need to insert function version now to make sure the correct
1400 : pre-mangled assembler name is recorded. */
1401 1068 : cgraph_node *node = cgraph_node::get_create (decl);
1402 :
1403 1068 : if (!node->function_version ())
1404 1068 : node->insert_new_function_version ();
1405 :
1406 1068 : DECL_FUNCTION_VERSIONED (decl) = 1;
1407 : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1408 : to include the version marker. */
1409 1068 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1410 1062 : mangle_decl (decl);
1411 : }
1412 17676 : }
1413 :
1414 : /* NEWDECL and OLDDECL have identical signatures. If they are
1415 : different versions adjust them, record function versions, and return
1416 : true. */
1417 :
1418 : bool
1419 209194 : maybe_version_functions (tree newdecl, tree olddecl)
1420 : {
1421 209194 : if (!disjoint_version_decls (newdecl, olddecl))
1422 : return false;
1423 :
1424 8820 : maybe_mark_function_versioned (olddecl);
1425 8820 : if (DECL_LOCAL_DECL_P (olddecl))
1426 : {
1427 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1428 18 : maybe_mark_function_versioned (olddecl);
1429 : }
1430 :
1431 8820 : maybe_mark_function_versioned (newdecl);
1432 8820 : if (DECL_LOCAL_DECL_P (newdecl))
1433 : {
1434 : /* Unfortunately, we can get here before pushdecl naturally calls
1435 : push_local_extern_decl_alias, so we need to call it directly. */
1436 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1437 6 : push_local_extern_decl_alias (newdecl);
1438 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1439 18 : maybe_mark_function_versioned (newdecl);
1440 : }
1441 :
1442 : /* Add the new version to the function version structure. */
1443 8820 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1444 8820 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1445 8820 : if (!fn_v)
1446 15 : fn_v = fn_node->insert_new_function_version ();
1447 :
1448 8820 : cgraph_node::add_function_version (fn_v, newdecl);
1449 :
1450 8820 : return true;
1451 : }
1452 :
1453 : /* If NEWDECL is `static' and an `extern' was seen previously,
1454 : warn about it. OLDDECL is the previous declaration.
1455 :
1456 : Note that this does not apply to the C++ case of declaring
1457 : a variable `extern const' and then later `const'.
1458 :
1459 : Don't complain about built-in functions, since they are beyond
1460 : the user's control. */
1461 :
1462 : void
1463 20404970 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1464 : {
1465 20404970 : if (TREE_CODE (newdecl) == TYPE_DECL
1466 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1467 : || TREE_CODE (newdecl) == CONST_DECL
1468 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1469 20404958 : return;
1470 :
1471 : /* Don't get confused by static member functions; that's a different
1472 : use of `static'. */
1473 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1474 15649230 : && DECL_STATIC_FUNCTION_P (newdecl))
1475 : return;
1476 :
1477 : /* If the old declaration was `static', or the new one isn't, then
1478 : everything is OK. */
1479 15992415 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1480 : return;
1481 :
1482 : /* It's OK to declare a builtin function as `static'. */
1483 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1484 12 : && DECL_ARTIFICIAL (olddecl))
1485 : return;
1486 :
1487 12 : auto_diagnostic_group d;
1488 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1489 : "%qD was declared %<extern%> and later %<static%>", newdecl))
1490 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1491 : "previous declaration of %qD", olddecl);
1492 12 : }
1493 :
1494 : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1495 : function templates. If their exception specifications do not
1496 : match, issue a diagnostic. */
1497 :
1498 : static void
1499 19829339 : check_redeclaration_exception_specification (tree new_decl,
1500 : tree old_decl)
1501 : {
1502 19829339 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1503 19829339 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1504 :
1505 : /* Two default specs are equivalent, don't force evaluation. */
1506 7575724 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1507 19978921 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1508 : return;
1509 :
1510 19819282 : if (!type_dependent_expression_p (old_decl))
1511 : {
1512 12119951 : maybe_instantiate_noexcept (new_decl);
1513 12119951 : maybe_instantiate_noexcept (old_decl);
1514 : }
1515 19819282 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1516 19819282 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1517 :
1518 : /* [except.spec]
1519 :
1520 : If any declaration of a function has an exception-specification,
1521 : all declarations, including the definition and an explicit
1522 : specialization, of that function shall have an
1523 : exception-specification with the same set of type-ids. */
1524 19819282 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1525 15802061 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1526 35621196 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1527 : {
1528 329 : const char *const msg
1529 : = G_("declaration of %qF has a different exception specifier");
1530 329 : bool complained = true;
1531 329 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1532 329 : auto_diagnostic_group d;
1533 :
1534 : /* Be permissive about C++98 vs C++11 operator new declarations. */
1535 658 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1536 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1537 331 : && (nothrow_spec_p (new_exceptions)
1538 1 : == nothrow_spec_p (old_exceptions)));
1539 :
1540 329 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1541 329 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1542 : /* Don't fuss about the C library; the C library functions are not
1543 : specified to have exception specifications (just behave as if they
1544 : have them), but some implementations include them. */
1545 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1546 96 : else if (!flag_exceptions)
1547 : /* We used to silently permit mismatched eh specs with
1548 : -fno-exceptions, so only complain if -pedantic. */
1549 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1550 93 : else if (!new_exceptions || global_new)
1551 : /* Reduce to pedwarn for omitted exception specification. No warning
1552 : flag for this; silence the warning by correcting the code. */
1553 18 : complained = pedwarn (new_loc, 0, msg, new_decl);
1554 : else
1555 75 : error_at (new_loc, msg, new_decl);
1556 :
1557 329 : if (complained)
1558 99 : inform (DECL_SOURCE_LOCATION (old_decl),
1559 : "from previous declaration %qF", old_decl);
1560 :
1561 : /* Copy the old exception specification if new_decl has none. Unless the
1562 : old decl is extern "C", as obscure code might depend on the type of
1563 : the new declaration (e.g. noexcept-type19.C). */
1564 329 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1565 17 : TREE_TYPE (new_decl)
1566 34 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1567 329 : }
1568 : }
1569 :
1570 : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1571 : Otherwise issue diagnostics. */
1572 :
1573 : bool
1574 20405496 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1575 : {
1576 20405496 : old_decl = STRIP_TEMPLATE (old_decl);
1577 20405496 : new_decl = STRIP_TEMPLATE (new_decl);
1578 20405496 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1579 20355343 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1580 : return true;
1581 20355343 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1582 20355343 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1583 : {
1584 20199301 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1585 : return true;
1586 39659508 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1587 19829754 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1588 : return true;
1589 : }
1590 156048 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1591 : {
1592 : /* With -fimplicit-constexpr, ignore changes in the constexpr
1593 : keyword. */
1594 71 : if (flag_implicit_constexpr
1595 71 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1596 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1597 : return true;
1598 71 : if (fndecl_built_in_p (old_decl))
1599 : {
1600 : /* Hide a built-in declaration. */
1601 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1602 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1603 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1604 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1605 : return true;
1606 : }
1607 : /* 7.1.5 [dcl.constexpr]
1608 : Note: An explicit specialization can differ from the template
1609 : declaration with respect to the constexpr specifier. */
1610 71 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1611 71 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1612 : return true;
1613 :
1614 42 : const char *kind = "constexpr";
1615 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1616 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1617 : kind = "consteval";
1618 42 : auto_diagnostic_group d;
1619 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1620 : "redeclaration %qD differs in %qs "
1621 : "from previous declaration", new_decl,
1622 : kind);
1623 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1624 : "previous declaration %qD", old_decl);
1625 42 : return false;
1626 42 : }
1627 : return true;
1628 : }
1629 :
1630 : /* DECL is a redeclaration of a function or function template. If
1631 : it does have default arguments issue a diagnostic. Note: this
1632 : function is used to enforce the requirements in C++11 8.3.6 about
1633 : no default arguments in redeclarations. */
1634 :
1635 : static void
1636 11956237 : check_redeclaration_no_default_args (tree decl)
1637 : {
1638 11956237 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1639 :
1640 11956237 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1641 35209979 : t && t != void_list_node; t = TREE_CHAIN (t))
1642 23253799 : if (TREE_PURPOSE (t))
1643 : {
1644 57 : permerror (DECL_SOURCE_LOCATION (decl),
1645 : "redeclaration of %q#D may not have default "
1646 : "arguments", decl);
1647 57 : return;
1648 : }
1649 : }
1650 :
1651 : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1652 : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1653 : the TEMPLATE_DECLs in case of function templates). This function is used
1654 : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1655 : "If a friend declaration specifies a default argument expression, that
1656 : declaration shall be a definition and shall be the only declaration of
1657 : the function or function template in the translation unit." */
1658 :
1659 : static void
1660 7323416 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1661 : {
1662 13919814 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1663 : return;
1664 :
1665 1873446 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1666 1873446 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1667 4954936 : t1 && t1 != void_list_node;
1668 3081490 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1669 4381030 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1670 8791852 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1671 : {
1672 36 : auto_diagnostic_group d;
1673 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1674 : "friend declaration of %q#D specifies default "
1675 : "arguments and isn%'t the only declaration", newdecl))
1676 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1677 : "previous declaration of %q#D", olddecl);
1678 36 : return;
1679 36 : }
1680 : }
1681 :
1682 : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1683 : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1684 :
1685 : static void
1686 19248558 : merge_attribute_bits (tree newdecl, tree olddecl)
1687 : {
1688 19248558 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1689 19248558 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1690 19248558 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1691 19248558 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1692 19248558 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1693 19248558 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1694 19248558 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1695 19248558 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1696 19248558 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1697 19248558 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1698 19248558 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1699 19248558 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1700 19248558 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1701 19248558 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1702 19248558 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1703 19248558 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1704 19248558 : }
1705 :
1706 : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1707 : && lookup_attribute ("gnu_inline", \
1708 : DECL_ATTRIBUTES (fn)))
1709 :
1710 : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1711 : ambiguates olddecl. Returns true if an error occurs. */
1712 :
1713 : static bool
1714 151032362 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1715 : {
1716 151032362 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1717 151032362 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1718 : /* Function template declarations can be differentiated by parameter
1719 : and return type. */
1720 151032362 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1721 151032362 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1722 151032362 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1723 : TREE_TYPE (TREE_TYPE (olddecl))))
1724 : {
1725 : /* ... and also by their template-heads and requires-clauses. */
1726 7335 : if (template_heads_equivalent_p (newdecl, olddecl)
1727 7335 : && function_requirements_equivalent_p (newres, oldres))
1728 : {
1729 0 : auto_diagnostic_group d;
1730 0 : error ("ambiguating new declaration %q+#D", newdecl);
1731 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1732 : "old declaration %q#D", olddecl);
1733 0 : return true;
1734 0 : }
1735 :
1736 : /* FIXME: The types are the same but the are differences
1737 : in either the template heads or function requirements.
1738 : We should be able to diagnose a set of common errors
1739 : stemming from these declarations. For example:
1740 :
1741 : template<typename T> requires C void f(...);
1742 : template<typename T> void f(...) requires C;
1743 :
1744 : These are functionally equivalent but not equivalent. */
1745 : }
1746 :
1747 : return false;
1748 : }
1749 :
1750 : /* OLD_PARMS is the innermost set of template parameters for some template
1751 : declaration, and NEW_PARMS is the corresponding set of template parameters
1752 : for a redeclaration of that template. Merge the default arguments within
1753 : these two sets of parameters. CLASS_P is true iff the template in
1754 : question is a class template. */
1755 :
1756 : bool
1757 9259116 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1758 : {
1759 9259116 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1760 : == TREE_VEC_LENGTH (old_parms));
1761 26194297 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1762 : {
1763 16935211 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1764 16935211 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1765 16935211 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1766 16935211 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1767 16935211 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1768 : return false;
1769 16935208 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1770 : {
1771 27 : auto_diagnostic_group d;
1772 27 : error ("redefinition of default argument for %q+#D", new_parm);
1773 27 : inform (DECL_SOURCE_LOCATION (old_parm),
1774 : "original definition appeared here");
1775 27 : return false;
1776 27 : }
1777 16935181 : else if (new_default != NULL_TREE)
1778 : /* Update the previous template parameters (which are the ones
1779 : that will really count) with the new default value. */
1780 240264 : old_default = new_default;
1781 16694917 : else if (class_p && old_default != NULL_TREE)
1782 : /* Update the new parameters, too; they'll be used as the
1783 : parameters for any members. */
1784 1042371 : new_default = old_default;
1785 : }
1786 : return true;
1787 : }
1788 :
1789 : /* Helper function for duplicate_decls and push_local_extern_decl_alias.
1790 : Merge parameter attributes and names between NEWDECL and OLDDECL.
1791 : NEW_DEFINES_FUNCTION and TYPES_MATCH argument like variables in
1792 : duplicate_decls, EXTERN_ALIAS false for duplicate_decls and true for
1793 : push_local_extern_decl_alias. */
1794 :
1795 : void
1796 15649419 : merge_decl_arguments (tree newdecl, tree olddecl, bool new_defines_function,
1797 : bool types_match, bool extern_alias)
1798 : {
1799 15649419 : tree oldarg, newarg, type = NULL_TREE;
1800 15649419 : tree first_user_parm = NULL_TREE;
1801 15649419 : if (extern_alias)
1802 189 : first_user_parm = FUNCTION_FIRST_USER_PARM (newdecl);
1803 15649419 : for (oldarg = DECL_ARGUMENTS (olddecl), newarg = DECL_ARGUMENTS (newdecl);
1804 43873691 : oldarg && newarg;
1805 28224272 : oldarg = DECL_CHAIN (oldarg), newarg = DECL_CHAIN (newarg))
1806 : {
1807 28224272 : DECL_ATTRIBUTES (newarg)
1808 28224272 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
1809 28224272 : if (lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (newarg))
1810 28224341 : && !lookup_attribute (NULL, "indeterminate",
1811 69 : DECL_ATTRIBUTES (oldarg)))
1812 : {
1813 18 : auto_diagnostic_group d;
1814 18 : error_at (DECL_SOURCE_LOCATION (newarg),
1815 : "%<indeterminate%> attribute not specified for parameter "
1816 : "%qD on the first declaration of its function", newarg);
1817 18 : inform (DECL_SOURCE_LOCATION (oldarg), "earlier declaration");
1818 18 : }
1819 : /* ??? Should attributes propagate out from a block extern? If so,
1820 : we should do that for the function itself, not just parameters. */
1821 28224272 : if (!extern_alias || flag_reflection)
1822 28224105 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1823 28224272 : if (!flag_reflection)
1824 27819832 : continue;
1825 : /* For extern_alias set DECL_HAS_DEFAULT_ARGUMENT_P on oldarg
1826 : if the local extern has a default argument for that parameter. */
1827 404440 : if (extern_alias)
1828 : {
1829 36 : if (newarg == first_user_parm)
1830 12 : type = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1831 24 : else if (type)
1832 24 : type = TREE_CHAIN (type);
1833 72 : if (type && TREE_PURPOSE (type))
1834 4 : DECL_HAS_DEFAULT_ARGUMENT_P (oldarg) = 1;
1835 : }
1836 : else
1837 : {
1838 : /* Otherwise propagate the flag. */
1839 404404 : if (DECL_HAS_DEFAULT_ARGUMENT_P (oldarg))
1840 0 : DECL_HAS_DEFAULT_ARGUMENT_P (newarg) = 1;
1841 404404 : if (DECL_HAS_DEFAULT_ARGUMENT_P (newarg))
1842 0 : DECL_HAS_DEFAULT_ARGUMENT_P (oldarg) = 1;
1843 : }
1844 : /* Merge names for std::meta::has_identifier and
1845 : std::meta::{,u8}identifier_of purposes. If they are different and
1846 : both oldarg and newarg are named, add flag to force that
1847 : std::meta::has_identifier returns false. If one is named and one is
1848 : unnamed, if neither is a olddecl nor newdecl is definition, propagate
1849 : DECL_NAME to both. Otherwise stash the old name into "old parm name"
1850 : artificial attribute. */
1851 404440 : if (DECL_NAME (oldarg) != DECL_NAME (newarg))
1852 : {
1853 45142 : if (DECL_NAME (oldarg) && DECL_NAME (newarg))
1854 : {
1855 : /* Different names. */
1856 10186 : MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1857 10186 : MULTIPLE_NAMES_PARM_P (newarg) = 1;
1858 : }
1859 34956 : else if (!new_defines_function
1860 34956 : && types_match
1861 34956 : && DECL_INITIAL (olddecl) == NULL_TREE)
1862 : {
1863 : /* For 2 non-definitions with matching types, one is named and
1864 : one unnamed, propagate name to both. */
1865 6606 : if (DECL_NAME (oldarg))
1866 5538 : DECL_NAME (newarg) = DECL_NAME (oldarg);
1867 : else
1868 1068 : DECL_NAME (oldarg) = DECL_NAME (newarg);
1869 : }
1870 : /* Depending on which PARM_DECL we'll keep, look at the other
1871 : PARM_DECL's name. */
1872 28350 : else if (tree name = ((new_defines_function || !types_match)
1873 28350 : ? DECL_NAME (oldarg) : DECL_NAME (newarg)))
1874 : {
1875 234 : tree opn = lookup_attribute ("old parm name",
1876 234 : DECL_ATTRIBUTES (oldarg));
1877 234 : if (opn)
1878 : {
1879 4 : if (TREE_VALUE (TREE_VALUE (opn)) == name)
1880 : /* Name already in "old parm name" attribute. */;
1881 : else
1882 : {
1883 : /* Different names. */
1884 0 : MULTIPLE_NAMES_PARM_P (oldarg) = 1;
1885 0 : MULTIPLE_NAMES_PARM_P (newarg) = 1;
1886 : }
1887 : }
1888 : else
1889 : {
1890 : /* Save name into attribute. */
1891 230 : DECL_ATTRIBUTES (newarg)
1892 230 : = tree_cons (get_identifier ("old parm name"),
1893 : tree_cons (NULL_TREE, name, NULL_TREE),
1894 230 : DECL_ATTRIBUTES (newarg));
1895 230 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1896 : }
1897 : }
1898 28116 : else if (extern_alias)
1899 2 : DECL_NAME (newarg) = DECL_NAME (oldarg);
1900 : }
1901 : }
1902 15649419 : }
1903 :
1904 : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1905 : If the redeclaration is invalid, a diagnostic is issued, and the
1906 : error_mark_node is returned. Otherwise, OLDDECL is returned.
1907 :
1908 : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1909 : returned.
1910 :
1911 : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1912 : if the old decl was hidden.
1913 :
1914 : Hidden decls can be anticipated builtins, injected friends, or
1915 : (coming soon) injected from a local-extern decl. */
1916 :
1917 : tree
1918 337859863 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1919 : {
1920 338008897 : unsigned olddecl_uid = DECL_UID (olddecl);
1921 338008897 : int types_match = 0;
1922 338008897 : int new_defines_function = 0;
1923 338008897 : tree new_template_info;
1924 338008897 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1925 338008897 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1926 :
1927 338008897 : if (newdecl == olddecl)
1928 : return olddecl;
1929 :
1930 337146313 : types_match = decls_match (newdecl, olddecl);
1931 :
1932 : /* If either the type of the new decl or the type of the old decl is an
1933 : error_mark_node, then that implies that we have already issued an
1934 : error (earlier) for some bogus type specification, and in that case,
1935 : it is rather pointless to harass the user with yet more error message
1936 : about the same declaration, so just pretend the types match here. */
1937 337146313 : if (TREE_TYPE (newdecl) == error_mark_node
1938 337146313 : || TREE_TYPE (olddecl) == error_mark_node)
1939 : return error_mark_node;
1940 :
1941 : /* Check for redeclaration and other discrepancies. */
1942 337146287 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1943 337146287 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1944 : {
1945 6475617 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1946 : {
1947 : /* Avoid warnings redeclaring built-ins which have not been
1948 : explicitly declared. */
1949 202894 : if (was_hidden)
1950 : {
1951 202852 : if (TREE_PUBLIC (newdecl)
1952 202852 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1953 12 : warning_at (newdecl_loc,
1954 12 : OPT_Wbuiltin_declaration_mismatch,
1955 : "built-in function %qD declared as non-function",
1956 : newdecl);
1957 : return NULL_TREE;
1958 : }
1959 :
1960 : /* If you declare a built-in or predefined function name as static,
1961 : the old definition is overridden, but optionally warn this was a
1962 : bad choice of name. */
1963 42 : if (! TREE_PUBLIC (newdecl))
1964 : {
1965 42 : warning_at (newdecl_loc,
1966 42 : OPT_Wshadow,
1967 42 : fndecl_built_in_p (olddecl)
1968 : ? G_("shadowing built-in function %q#D")
1969 : : G_("shadowing library function %q#D"), olddecl);
1970 : /* Discard the old built-in function. */
1971 42 : return NULL_TREE;
1972 : }
1973 : /* If the built-in is not ansi, then programs can override
1974 : it even globally without an error. */
1975 0 : else if (! fndecl_built_in_p (olddecl))
1976 0 : warning_at (newdecl_loc, 0,
1977 : "library function %q#D redeclared as non-function %q#D",
1978 : olddecl, newdecl);
1979 : else
1980 0 : error_at (newdecl_loc,
1981 : "declaration of %q#D conflicts with built-in "
1982 : "declaration %q#D", newdecl, olddecl);
1983 : return NULL_TREE;
1984 : }
1985 6272723 : else if (!types_match)
1986 : {
1987 : /* Avoid warnings redeclaring built-ins which have not been
1988 : explicitly declared. */
1989 2255502 : if (was_hidden)
1990 : {
1991 815890 : tree t1, t2;
1992 :
1993 : /* A new declaration doesn't match a built-in one unless it
1994 : is also extern "C". */
1995 815890 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1996 815890 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1997 815890 : if (!DECL_EXTERN_C_P (newdecl))
1998 : return NULL_TREE;
1999 :
2000 149235 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2001 149235 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2002 257636 : t1 || t2;
2003 108401 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2004 : {
2005 257618 : if (!t1 || !t2)
2006 : break;
2007 : /* FILE, tm types are not known at the time
2008 : we create the builtins. */
2009 794073 : for (unsigned i = 0;
2010 1051529 : i < sizeof (builtin_structptr_types)
2011 : / sizeof (builtin_structptr_type);
2012 : ++i)
2013 943122 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
2014 : {
2015 149049 : tree t = TREE_VALUE (t1);
2016 :
2017 149049 : if (TYPE_PTR_P (t)
2018 298098 : && TYPE_IDENTIFIER (TREE_TYPE (t))
2019 149049 : == get_identifier (builtin_structptr_types[i].str)
2020 298083 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
2021 : {
2022 149034 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2023 :
2024 149034 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
2025 149034 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2026 149034 : types_match = decls_match (newdecl, olddecl);
2027 149034 : if (types_match)
2028 : return duplicate_decls (newdecl, olddecl,
2029 : hiding, was_hidden);
2030 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
2031 : }
2032 15 : goto next_arg;
2033 : }
2034 :
2035 108407 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2036 : break;
2037 108401 : next_arg:;
2038 : }
2039 :
2040 201 : warning_at (newdecl_loc,
2041 201 : OPT_Wbuiltin_declaration_mismatch,
2042 : "declaration of %q#D conflicts with built-in "
2043 : "declaration %q#D", newdecl, olddecl);
2044 : }
2045 1439612 : else if ((DECL_EXTERN_C_P (newdecl)
2046 15 : && DECL_EXTERN_C_P (olddecl))
2047 2879209 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2048 1439597 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2049 : {
2050 : /* Don't really override olddecl for __* prefixed builtins
2051 : except for __[^b]*_chk, the compiler might be using those
2052 : explicitly. */
2053 15 : if (fndecl_built_in_p (olddecl))
2054 : {
2055 15 : tree id = DECL_NAME (olddecl);
2056 15 : const char *name = IDENTIFIER_POINTER (id);
2057 15 : size_t len;
2058 :
2059 15 : if (name[0] == '_'
2060 15 : && name[1] == '_'
2061 30 : && (startswith (name + 2, "builtin_")
2062 6 : || (len = strlen (name)) <= strlen ("___chk")
2063 6 : || memcmp (name + len - strlen ("_chk"),
2064 : "_chk", strlen ("_chk") + 1) != 0))
2065 : {
2066 15 : if (DECL_INITIAL (newdecl))
2067 : {
2068 6 : error_at (newdecl_loc,
2069 : "definition of %q#D ambiguates built-in "
2070 : "declaration %q#D", newdecl, olddecl);
2071 6 : return error_mark_node;
2072 : }
2073 9 : auto_diagnostic_group d;
2074 9 : if (permerror (newdecl_loc,
2075 : "new declaration %q#D ambiguates built-in"
2076 : " declaration %q#D", newdecl, olddecl)
2077 9 : && flag_permissive)
2078 3 : inform (newdecl_loc,
2079 : "ignoring the %q#D declaration", newdecl);
2080 9 : return flag_permissive ? olddecl : error_mark_node;
2081 9 : }
2082 : }
2083 :
2084 : /* A near match; override the builtin. */
2085 :
2086 0 : if (TREE_PUBLIC (newdecl))
2087 0 : warning_at (newdecl_loc,
2088 0 : OPT_Wbuiltin_declaration_mismatch,
2089 : "new declaration %q#D ambiguates built-in "
2090 : "declaration %q#D", newdecl, olddecl);
2091 : else
2092 0 : warning (OPT_Wshadow,
2093 : fndecl_built_in_p (olddecl)
2094 : ? G_("shadowing built-in function %q#D")
2095 : : G_("shadowing library function %q#D"), olddecl);
2096 : }
2097 : else
2098 : /* Discard the old built-in function. */
2099 : return NULL_TREE;
2100 :
2101 : /* Replace the old RTL to avoid problems with inlining. */
2102 201 : COPY_DECL_RTL (newdecl, olddecl);
2103 : }
2104 : else
2105 : {
2106 : /* Even if the types match, prefer the new declarations type
2107 : for built-ins which have not been explicitly declared,
2108 : for exception lists, etc... */
2109 4017221 : tree type = TREE_TYPE (newdecl);
2110 4017221 : tree attribs = (*targetm.merge_type_attributes)
2111 4017221 : (TREE_TYPE (olddecl), type);
2112 :
2113 4017221 : type = cp_build_type_attribute_variant (type, attribs);
2114 4017221 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
2115 : }
2116 :
2117 : /* If a function is explicitly declared "throw ()", propagate that to
2118 : the corresponding builtin. */
2119 4017422 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
2120 3843525 : && was_hidden
2121 3843482 : && TREE_NOTHROW (newdecl)
2122 7587678 : && !TREE_NOTHROW (olddecl))
2123 : {
2124 28117 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
2125 28117 : tree tmpdecl = builtin_decl_explicit (fncode);
2126 28117 : if (tmpdecl && tmpdecl != olddecl && types_match)
2127 28117 : TREE_NOTHROW (tmpdecl) = 1;
2128 : }
2129 :
2130 : /* Whether or not the builtin can throw exceptions has no
2131 : bearing on this declarator. */
2132 4017422 : TREE_NOTHROW (olddecl) = 0;
2133 :
2134 4017422 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2135 : {
2136 : /* If a builtin function is redeclared as `static', merge
2137 : the declarations, but make the original one static. */
2138 3 : DECL_THIS_STATIC (olddecl) = 1;
2139 3 : TREE_PUBLIC (olddecl) = 0;
2140 :
2141 : /* Make the old declaration consistent with the new one so
2142 : that all remnants of the builtin-ness of this function
2143 : will be banished. */
2144 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2145 3 : COPY_DECL_RTL (newdecl, olddecl);
2146 : }
2147 : }
2148 330670670 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2149 : {
2150 : /* C++ Standard, 3.3, clause 4:
2151 : "[Note: a namespace name or a class template name must be unique
2152 : in its declarative region (7.3.2, clause 14). ]" */
2153 73851330 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
2154 73851303 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
2155 : /* Namespace conflicts with not namespace. */;
2156 36121107 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
2157 109972398 : || DECL_TYPE_TEMPLATE_P (newdecl))
2158 : /* Class template conflicts. */;
2159 73851285 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2160 36121107 : && DECL_TEMPLATE_RESULT (olddecl)
2161 36121107 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2162 109972386 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
2163 37680820 : && DECL_TEMPLATE_RESULT (newdecl)
2164 37680820 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2165 : /* Variable template conflicts. */;
2166 73851276 : else if (concept_definition_p (olddecl)
2167 147702546 : || concept_definition_p (newdecl))
2168 : /* Concept conflicts. */;
2169 73851264 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2170 36160623 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
2171 73890792 : || (TREE_CODE (olddecl) == FUNCTION_DECL
2172 37680833 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2173 : {
2174 : /* One is a function and the other is a template
2175 : function. */
2176 73801891 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2177 : return NULL_TREE;
2178 :
2179 : /* There can only be one! */
2180 58333 : auto_diagnostic_group d;
2181 58333 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
2182 58333 : && check_raw_literal_operator (olddecl))
2183 3 : error_at (newdecl_loc,
2184 : "literal operator %q#D conflicts with"
2185 : " raw literal operator", newdecl);
2186 58330 : else if (check_raw_literal_operator (newdecl))
2187 3 : error_at (newdecl_loc,
2188 : "raw literal operator %q#D conflicts with"
2189 : " literal operator template", newdecl);
2190 : else
2191 : return NULL_TREE;
2192 :
2193 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2194 6 : return error_mark_node;
2195 58333 : }
2196 49373 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2197 : /* A structured binding must be unique in its declarative region. */;
2198 39678 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2199 49370 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
2200 : /* One is an implicit typedef, that's ok. */
2201 : return NULL_TREE;
2202 :
2203 153 : auto_diagnostic_group d;
2204 153 : error ("%q#D redeclared as different kind of entity", newdecl);
2205 153 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2206 :
2207 153 : return error_mark_node;
2208 153 : }
2209 256819340 : else if (!types_match)
2210 : {
2211 240397421 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2212 : /* These are certainly not duplicate declarations; they're
2213 : from different scopes. */
2214 : return NULL_TREE;
2215 :
2216 240397361 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2217 : {
2218 151032380 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2219 151032380 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
2220 :
2221 : /* The name of a class template may not be declared to refer to
2222 : any other template, class, function, object, namespace, value,
2223 : or type in the same scope. */
2224 151032380 : if (TREE_CODE (oldres) == TYPE_DECL
2225 151032374 : || TREE_CODE (newres) == TYPE_DECL)
2226 : {
2227 9 : auto_diagnostic_group d;
2228 9 : error_at (newdecl_loc,
2229 : "conflicting declaration of template %q#D", newdecl);
2230 9 : inform (olddecl_loc,
2231 : "previous declaration %q#D", olddecl);
2232 9 : return error_mark_node;
2233 9 : }
2234 :
2235 151032371 : else if (TREE_CODE (oldres) == FUNCTION_DECL
2236 151032362 : && TREE_CODE (newres) == FUNCTION_DECL)
2237 : {
2238 151032362 : if (duplicate_function_template_decls (newdecl, olddecl))
2239 0 : return error_mark_node;
2240 : return NULL_TREE;
2241 : }
2242 : return NULL_TREE;
2243 : }
2244 89364981 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2245 : {
2246 89364824 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2247 : {
2248 57 : auto_diagnostic_group d;
2249 57 : error_at (newdecl_loc,
2250 : "conflicting declaration of C function %q#D",
2251 : newdecl);
2252 57 : inform (olddecl_loc,
2253 : "previous declaration %q#D", olddecl);
2254 57 : return error_mark_node;
2255 57 : }
2256 : /* For function versions, params and types match, but they
2257 : are not ambiguous. */
2258 89364767 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2259 89356433 : && !DECL_FUNCTION_VERSIONED (olddecl))
2260 8379 : || !same_type_p (fndecl_declared_return_type (newdecl),
2261 : fndecl_declared_return_type (olddecl)))
2262 : /* Let constrained hidden friends coexist for now, we'll
2263 : check satisfaction later. */
2264 89356388 : && !member_like_constrained_friend_p (newdecl)
2265 81805126 : && !member_like_constrained_friend_p (olddecl)
2266 : // The functions have the same parameter types.
2267 80063799 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2268 80063799 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2269 : // And the same constraints.
2270 89364836 : && equivalently_constrained (newdecl, olddecl))
2271 : {
2272 69 : auto_diagnostic_group d;
2273 69 : error_at (newdecl_loc,
2274 : "ambiguating new declaration of %q#D", newdecl);
2275 69 : inform (olddecl_loc,
2276 : "old declaration %q#D", olddecl);
2277 69 : return error_mark_node;
2278 69 : }
2279 : else
2280 : return NULL_TREE;
2281 : }
2282 : else
2283 : {
2284 157 : auto_diagnostic_group d;
2285 157 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2286 157 : inform (olddecl_loc,
2287 : "previous declaration as %q#D", olddecl);
2288 157 : return error_mark_node;
2289 157 : }
2290 : }
2291 16421919 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2292 16421919 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2293 : {
2294 : /* OMP UDRs are never duplicates. */
2295 33 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2296 33 : auto_diagnostic_group d;
2297 33 : error_at (newdecl_loc,
2298 : "redeclaration of %<pragma omp declare reduction%>");
2299 33 : inform (olddecl_loc,
2300 : "previous %<pragma omp declare reduction%> declaration");
2301 33 : return error_mark_node;
2302 33 : }
2303 16421886 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2304 16421886 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2305 224 : && (!DECL_TEMPLATE_INFO (newdecl)
2306 224 : || (DECL_TI_TEMPLATE (newdecl)
2307 224 : != DECL_TI_TEMPLATE (olddecl))))
2308 11632093 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2309 581047 : && (!DECL_TEMPLATE_INFO (olddecl)
2310 581047 : || (DECL_TI_TEMPLATE (olddecl)
2311 581047 : != DECL_TI_TEMPLATE (newdecl))))))
2312 : /* It's OK to have a template specialization and a non-template
2313 : with the same type, or to have specializations of two
2314 : different templates with the same type. Note that if one is a
2315 : specialization, and the other is an instantiation of the same
2316 : template, that we do not exit at this point. That situation
2317 : can occur if we instantiate a template class, and then
2318 : specialize one of its methods. This situation is valid, but
2319 : the declarations must be merged in the usual way. */
2320 : return NULL_TREE;
2321 16421886 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2322 16421886 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2323 665891 : && !DECL_USE_TEMPLATE (newdecl))
2324 11632093 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2325 85068 : && !DECL_USE_TEMPLATE (olddecl))))
2326 : /* One of the declarations is a template instantiation, and the
2327 : other is not a template at all. That's OK. */
2328 : return NULL_TREE;
2329 16421886 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2330 : {
2331 : /* In [namespace.alias] we have:
2332 :
2333 : In a declarative region, a namespace-alias-definition can be
2334 : used to redefine a namespace-alias declared in that declarative
2335 : region to refer only to the namespace to which it already
2336 : refers.
2337 :
2338 : Therefore, if we encounter a second alias directive for the same
2339 : alias, we can just ignore the second directive. */
2340 30 : if (DECL_NAMESPACE_ALIAS (newdecl)
2341 30 : && (DECL_NAMESPACE_ALIAS (newdecl)
2342 27 : == DECL_NAMESPACE_ALIAS (olddecl)))
2343 21 : return olddecl;
2344 :
2345 : /* Leave it to update_binding to merge or report error. */
2346 : return NULL_TREE;
2347 : }
2348 : /* Check if the two decls are non-mergeable versioned decls. */
2349 16421856 : else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2350 : && diagnose_versioned_decls (olddecl, newdecl))
2351 : return error_mark_node;
2352 : else
2353 : {
2354 16421856 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2355 16421856 : if (errmsg)
2356 : {
2357 621 : auto_diagnostic_group d;
2358 621 : error_at (newdecl_loc, errmsg, newdecl);
2359 621 : if (DECL_NAME (olddecl) != NULL_TREE)
2360 621 : inform (olddecl_loc,
2361 621 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2362 : ? G_("%q#D previously defined here")
2363 : : G_("%q#D previously declared here"), olddecl);
2364 621 : if (cxx_dialect >= cxx26
2365 147 : && DECL_NAME (newdecl)
2366 147 : && id_equal (DECL_NAME (newdecl), "_")
2367 634 : && !name_independent_decl_p (newdecl))
2368 : {
2369 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2370 2 : inform (newdecl_loc,
2371 : "parameter declaration is not name-independent");
2372 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2373 : {
2374 3 : if (at_namespace_scope_p ())
2375 2 : inform (newdecl_loc,
2376 : "structured binding at namespace scope is not "
2377 : "name-independent");
2378 1 : else if (TREE_STATIC (newdecl))
2379 1 : inform (newdecl_loc,
2380 : "static structured binding is not "
2381 : "name-independent");
2382 0 : else if (DECL_EXTERNAL (newdecl))
2383 0 : inform (newdecl_loc,
2384 : "extern structured binding is not "
2385 : "name-independent");
2386 : }
2387 8 : else if (at_class_scope_p ()
2388 0 : && VAR_P (newdecl)
2389 8 : && TREE_STATIC (newdecl))
2390 0 : inform (newdecl_loc,
2391 : "static data member is not name-independent");
2392 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2393 3 : inform (newdecl_loc,
2394 : "variable at namespace scope is not name-independent");
2395 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2396 3 : inform (newdecl_loc,
2397 : "static variable is not name-independent");
2398 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2399 0 : inform (newdecl_loc,
2400 : "extern variable is not name-independent");
2401 : }
2402 621 : return error_mark_node;
2403 621 : }
2404 16421235 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2405 11631853 : && DECL_INITIAL (olddecl) != NULL_TREE
2406 17566 : && !prototype_p (TREE_TYPE (olddecl))
2407 16421235 : && prototype_p (TREE_TYPE (newdecl)))
2408 : {
2409 : /* Prototype decl follows defn w/o prototype. */
2410 0 : auto_diagnostic_group d;
2411 0 : if (warning_at (newdecl_loc, 0,
2412 : "prototype specified for %q#D", newdecl))
2413 0 : inform (olddecl_loc,
2414 : "previous non-prototype definition here");
2415 0 : }
2416 15895988 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2417 28053088 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2418 : {
2419 : /* [dcl.link]
2420 : If two declarations of the same function or object
2421 : specify different linkage-specifications ..., the program
2422 : is ill-formed.... Except for functions with C++ linkage,
2423 : a function declaration without a linkage specification
2424 : shall not precede the first linkage specification for
2425 : that function. A function can be declared without a
2426 : linkage specification after an explicit linkage
2427 : specification has been seen; the linkage explicitly
2428 : specified in the earlier declaration is not affected by
2429 : such a function declaration.
2430 :
2431 : DR 563 raises the question why the restrictions on
2432 : functions should not also apply to objects. Older
2433 : versions of G++ silently ignore the linkage-specification
2434 : for this example:
2435 :
2436 : namespace N {
2437 : extern int i;
2438 : extern "C" int i;
2439 : }
2440 :
2441 : which is clearly wrong. Therefore, we now treat objects
2442 : like functions. */
2443 1891 : if (current_lang_depth () == 0)
2444 : {
2445 : /* There is no explicit linkage-specification, so we use
2446 : the linkage from the previous declaration. */
2447 1879 : retrofit_lang_decl (newdecl);
2448 1879 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2449 : }
2450 : else
2451 : {
2452 12 : auto_diagnostic_group d;
2453 12 : error_at (newdecl_loc,
2454 : "conflicting declaration of %q#D with %qL linkage",
2455 12 : newdecl, DECL_LANGUAGE (newdecl));
2456 12 : inform (olddecl_loc,
2457 : "previous declaration with %qL linkage",
2458 12 : DECL_LANGUAGE (olddecl));
2459 12 : }
2460 : }
2461 :
2462 16421235 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2463 : ;
2464 15734050 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2465 : {
2466 : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2467 13683650 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2468 10965747 : && (/* grokfndecl passes member function templates too
2469 : as FUNCTION_DECLs. */
2470 8429798 : DECL_TEMPLATE_INFO (olddecl)
2471 : /* C++11 8.3.6/6.
2472 : Default arguments for a member function of a class
2473 : template shall be specified on the initial declaration
2474 : of the member function within the class template. */
2475 711616 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2476 : {
2477 7799284 : check_redeclaration_no_default_args (newdecl);
2478 :
2479 7799284 : if (DECL_TEMPLATE_INFO (olddecl)
2480 7799284 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2481 : {
2482 2267720 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2483 2267720 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2484 2267720 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2485 2267720 : tree old_parms
2486 2267720 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2487 2267720 : merge_default_template_args (new_parms, old_parms,
2488 : /*class_p=*/false);
2489 : }
2490 : }
2491 : else
2492 : {
2493 3166463 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2494 3166463 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2495 3166463 : int i = 1;
2496 :
2497 11360605 : for (; t1 && t1 != void_list_node;
2498 5027679 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2499 5029692 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2500 : {
2501 57 : if (simple_cst_equal (TREE_PURPOSE (t1),
2502 57 : TREE_PURPOSE (t2)) == 1)
2503 : {
2504 24 : auto_diagnostic_group d;
2505 24 : if (permerror (newdecl_loc,
2506 : "default argument given for parameter "
2507 : "%d of %q#D", i, newdecl))
2508 24 : inform (olddecl_loc,
2509 : "previous specification in %q#D here",
2510 : olddecl);
2511 24 : }
2512 : else
2513 : {
2514 33 : auto_diagnostic_group d;
2515 33 : error_at (newdecl_loc,
2516 : "default argument given for parameter %d "
2517 : "of %q#D", i, newdecl);
2518 33 : inform (olddecl_loc,
2519 : "previous specification in %q#D here",
2520 : olddecl);
2521 33 : }
2522 : }
2523 :
2524 : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2525 : argument expression, that declaration... shall be the only
2526 : declaration of the function or function template in the
2527 : translation unit." */
2528 3166463 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2529 : }
2530 : }
2531 : }
2532 :
2533 : /* Do not merge an implicit typedef with an explicit one. In:
2534 :
2535 : class A;
2536 : ...
2537 : typedef class A A __attribute__ ((foo));
2538 :
2539 : the attribute should apply only to the typedef. */
2540 20438657 : if (TREE_CODE (olddecl) == TYPE_DECL
2541 20438657 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2542 50132 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2543 : return NULL_TREE;
2544 :
2545 20405036 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2546 : return NULL_TREE;
2547 :
2548 20405024 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2549 30 : return error_mark_node;
2550 :
2551 20404994 : if (modules_p ()
2552 63565 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2553 20442228 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2554 : {
2555 37234 : if (!module_may_redeclare (olddecl, newdecl))
2556 24 : return error_mark_node;
2557 :
2558 37210 : if (!hiding)
2559 : {
2560 : /* The old declaration should match the exportingness of the new
2561 : declaration. But hidden friend declarations just keep the
2562 : exportingness of the old declaration; see CWG2588. */
2563 32696 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2564 32696 : if (DECL_LANG_SPECIFIC (not_tmpl)
2565 32584 : && DECL_MODULE_ATTACH_P (not_tmpl)
2566 : /* Typedefs are not entities and so are OK to be redeclared
2567 : as exported: see [module.interface]/p6. */
2568 32887 : && TREE_CODE (olddecl) != TYPE_DECL)
2569 : {
2570 179 : if (DECL_MODULE_EXPORT_P (newdecl)
2571 179 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2572 : {
2573 15 : auto_diagnostic_group d;
2574 15 : error ("conflicting exporting for declaration %qD", newdecl);
2575 15 : inform (olddecl_loc,
2576 : "previously declared here without exporting");
2577 15 : }
2578 : }
2579 32517 : else if (DECL_MODULE_EXPORT_P (newdecl))
2580 10333 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2581 : }
2582 : }
2583 :
2584 : /* We have committed to returning OLDDECL at this point. */
2585 :
2586 : /* If new decl is `static' and an `extern' was seen previously,
2587 : warn about it. */
2588 20404970 : warn_extern_redeclared_static (newdecl, olddecl);
2589 :
2590 : /* True to merge attributes between the declarations, false to
2591 : set OLDDECL's attributes to those of NEWDECL (for template
2592 : explicit specializations that specify their own attributes
2593 : independent of those specified for the primary template). */
2594 20404970 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2595 15649230 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2596 20986002 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2597 :
2598 20404970 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2599 : {
2600 15649230 : if (merge_attr)
2601 : {
2602 15068407 : {
2603 15068407 : auto_diagnostic_group d;
2604 15068407 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2605 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2606 : ? G_("previous definition of %qD here")
2607 : : G_("previous declaration of %qD here"), olddecl);
2608 15068407 : }
2609 :
2610 : /* [dcl.attr.noreturn]: The first declaration of a function shall
2611 : specify the noreturn attribute if any declaration of that function
2612 : specifies the noreturn attribute. */
2613 15068407 : tree a;
2614 15068407 : if (TREE_THIS_VOLATILE (newdecl)
2615 99668 : && !TREE_THIS_VOLATILE (olddecl)
2616 : /* This applies to [[noreturn]] only, not its GNU variants. */
2617 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2618 54 : && cxx11_attribute_p (a)
2619 15068428 : && get_attribute_namespace (a) == NULL_TREE)
2620 : {
2621 9 : auto_diagnostic_group d;
2622 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2623 : "but its first declaration was not", newdecl);
2624 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2625 9 : }
2626 : }
2627 :
2628 : /* Now that functions must hold information normally held
2629 : by field decls, there is extra work to do so that
2630 : declaration information does not get destroyed during
2631 : definition. */
2632 15649230 : if (DECL_VINDEX (olddecl))
2633 163085 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2634 15649230 : if (DECL_CONTEXT (olddecl))
2635 15649230 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2636 15649230 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2637 15649230 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2638 15649230 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2639 15649230 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2640 15649230 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2641 15649230 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2642 15649230 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2643 15649230 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2644 15649230 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2645 15649230 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2646 15649230 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2647 2803217 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2648 5606434 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2649 15649230 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2650 :
2651 15649230 : check_redecl_contract (newdecl, olddecl);
2652 :
2653 : /* Optionally warn about more than one declaration for the same
2654 : name, but don't warn about a function declaration followed by a
2655 : definition. */
2656 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2657 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2658 : /* Don't warn about extern decl followed by definition. */
2659 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2660 : /* Don't warn if at least one is/was hidden. */
2661 27 : && !(hiding || was_hidden)
2662 : /* Don't warn about declaration followed by specialization. */
2663 15649242 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2664 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2665 : {
2666 6 : auto_diagnostic_group d;
2667 6 : if (warning_at (newdecl_loc,
2668 6 : OPT_Wredundant_decls,
2669 : "redundant redeclaration of %qD in same scope",
2670 : newdecl))
2671 6 : inform (olddecl_loc,
2672 : "previous declaration of %qD", olddecl);
2673 6 : }
2674 :
2675 : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2676 : first declaration of the function or, for an explicit specialization
2677 : of a function template, the first declaration of that
2678 : specialization. */
2679 15649230 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2680 665891 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2681 : {
2682 15068407 : if (DECL_DELETED_FN (newdecl))
2683 : {
2684 21 : auto_diagnostic_group d;
2685 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2686 : "is not first declaration", newdecl))
2687 21 : inform (olddecl_loc,
2688 : "previous declaration of %qD", olddecl);
2689 21 : }
2690 15068407 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2691 15068407 : if (DECL_DELETED_FN (olddecl)
2692 6 : && DECL_INITIAL (olddecl)
2693 15068413 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2694 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2695 : }
2696 : }
2697 :
2698 : /* Deal with C++: must preserve virtual function table size. */
2699 20404970 : if (TREE_CODE (olddecl) == TYPE_DECL)
2700 : {
2701 50129 : tree newtype = TREE_TYPE (newdecl);
2702 50129 : tree oldtype = TREE_TYPE (olddecl);
2703 :
2704 50129 : if (newtype != error_mark_node && oldtype != error_mark_node
2705 100258 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2706 138351 : CLASSTYPE_FRIEND_CLASSES (newtype)
2707 46117 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2708 :
2709 50129 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2710 : }
2711 :
2712 : /* Copy all the DECL_... slots specified in the new decl except for
2713 : any that we copy here from the old type. */
2714 20404970 : if (merge_attr)
2715 19824147 : DECL_ATTRIBUTES (newdecl)
2716 39648294 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2717 : else
2718 580823 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2719 :
2720 : /* Transfer purviewness and importingness to the old decl. */
2721 20404970 : transfer_defining_module (olddecl, newdecl);
2722 :
2723 20404970 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2724 : {
2725 4180364 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2726 4180364 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2727 4180364 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2728 :
2729 : /* The new decl should not already have gathered any
2730 : specializations. */
2731 4180364 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2732 :
2733 : /* Make sure the contracts are equivalent. */
2734 4180364 : check_redecl_contract (newdecl, olddecl);
2735 :
2736 4180364 : DECL_ATTRIBUTES (old_result)
2737 4180364 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2738 :
2739 4180364 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2740 : {
2741 4180334 : if (DECL_SOURCE_LOCATION (newdecl)
2742 4180334 : != DECL_SOURCE_LOCATION (olddecl))
2743 : {
2744 : /* Per C++11 8.3.6/4, default arguments cannot be added in
2745 : later declarations of a function template. */
2746 4156953 : check_redeclaration_no_default_args (newdecl);
2747 : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2748 : argument expression, that declaration... shall be the only
2749 : declaration of the function or function template in the
2750 : translation unit." */
2751 4156953 : check_no_redeclaration_friend_default_args
2752 4156953 : (old_result, new_result);
2753 :
2754 4156953 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2755 4156953 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2756 4156953 : merge_default_template_args (new_parms, old_parms,
2757 : /*class_p=*/false);
2758 : }
2759 4180334 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2760 3060498 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2761 :
2762 4180334 : check_default_args (newdecl);
2763 :
2764 8360656 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2765 4180334 : && DECL_INITIAL (new_result))
2766 : {
2767 24 : if (DECL_INITIAL (old_result))
2768 12 : DECL_UNINLINABLE (old_result) = 1;
2769 : else
2770 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2771 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2772 72 : DECL_NOT_REALLY_EXTERN (old_result)
2773 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2774 48 : DECL_INTERFACE_KNOWN (old_result)
2775 24 : = DECL_INTERFACE_KNOWN (new_result);
2776 24 : DECL_DECLARED_INLINE_P (old_result)
2777 24 : = DECL_DECLARED_INLINE_P (new_result);
2778 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2779 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2780 : }
2781 : else
2782 : {
2783 4180310 : DECL_DECLARED_INLINE_P (old_result)
2784 4180310 : |= DECL_DECLARED_INLINE_P (new_result);
2785 4180310 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2786 4180310 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2787 4180310 : check_redeclaration_exception_specification (newdecl, olddecl);
2788 :
2789 4180310 : merge_attribute_bits (new_result, old_result);
2790 : }
2791 : }
2792 :
2793 : /* If the new declaration is a definition, update the file and
2794 : line information on the declaration, and also make
2795 : the old declaration the same definition. */
2796 4180364 : if (DECL_INITIAL (new_result) != NULL_TREE)
2797 : {
2798 5677038 : DECL_SOURCE_LOCATION (olddecl)
2799 5677038 : = DECL_SOURCE_LOCATION (old_result)
2800 2838519 : = DECL_SOURCE_LOCATION (newdecl);
2801 2838519 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2802 2838519 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2803 : {
2804 2838519 : update_contract_arguments (new_result, old_result);
2805 :
2806 2838519 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2807 10236131 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2808 7397612 : DECL_CONTEXT (p) = old_result;
2809 :
2810 5677038 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2811 18 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2812 : }
2813 : }
2814 :
2815 : return olddecl;
2816 : }
2817 :
2818 16224606 : if (types_match)
2819 : {
2820 16224405 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2821 15649029 : check_redeclaration_exception_specification (newdecl, olddecl);
2822 :
2823 : /* Automatically handles default parameters. */
2824 16224405 : tree oldtype = TREE_TYPE (olddecl);
2825 16224405 : tree newtype;
2826 :
2827 : /* For typedefs use the old type, as the new type's DECL_NAME points
2828 : at newdecl, which will be ggc_freed. */
2829 16224405 : if (TREE_CODE (newdecl) == TYPE_DECL)
2830 : {
2831 : /* But NEWTYPE might have an attribute, honor that. */
2832 50129 : tree tem = TREE_TYPE (newdecl);
2833 50129 : newtype = oldtype;
2834 :
2835 50129 : if (TYPE_USER_ALIGN (tem))
2836 : {
2837 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2838 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2839 21 : TYPE_USER_ALIGN (newtype) = true;
2840 : }
2841 :
2842 : /* And remove the new type from the variants list. */
2843 50129 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2844 : {
2845 24 : tree remove = TREE_TYPE (newdecl);
2846 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2847 : {
2848 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2849 : /* If remove is the main variant, no need to remove that
2850 : from the list. One of the DECL_ORIGINAL_TYPE
2851 : variants, e.g. created for aligned attribute, might still
2852 : refer to the newdecl TYPE_DECL though, so remove that one
2853 : in that case. */
2854 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2855 6 : if (orig != remove)
2856 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2857 0 : t = TYPE_MAIN_VARIANT (t))
2858 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2859 : {
2860 12 : TYPE_NEXT_VARIANT (t)
2861 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2862 6 : break;
2863 : }
2864 : }
2865 : else
2866 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2867 0 : t = TYPE_NEXT_VARIANT (t))
2868 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2869 : {
2870 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2871 15 : break;
2872 : }
2873 : }
2874 : }
2875 16174276 : else if (merge_attr)
2876 15593453 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2877 : else
2878 580823 : newtype = TREE_TYPE (newdecl);
2879 :
2880 16224405 : if (VAR_P (newdecl))
2881 : {
2882 525247 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2883 : /* For already initialized vars, TREE_READONLY could have been
2884 : cleared in cp_finish_decl, because the var needs runtime
2885 : initialization or destruction. Make sure not to set
2886 : TREE_READONLY on it again. */
2887 525247 : if (DECL_INITIALIZED_P (olddecl)
2888 1217 : && !DECL_EXTERNAL (olddecl)
2889 525334 : && !TREE_READONLY (olddecl))
2890 32 : TREE_READONLY (newdecl) = 0;
2891 525247 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2892 1050494 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2893 525247 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2894 525247 : if (DECL_DEPENDENT_INIT_P (olddecl))
2895 110995 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2896 1050494 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2897 525247 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2898 525247 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2899 525247 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2900 1050494 : DECL_DECLARED_CONSTINIT_P (newdecl)
2901 525247 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2902 :
2903 : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2904 525247 : if (DECL_LANG_SPECIFIC (olddecl)
2905 1046896 : && CP_DECL_THREADPRIVATE_P (olddecl))
2906 : {
2907 : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2908 41 : retrofit_lang_decl (newdecl);
2909 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2910 : }
2911 : }
2912 :
2913 : /* An explicit specialization of a function template or of a member
2914 : function of a class template can be declared transaction_safe
2915 : independently of whether the corresponding template entity is declared
2916 : transaction_safe. */
2917 1207 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2918 1072 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2919 78 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2920 54 : && tx_safe_fn_type_p (newtype)
2921 16224405 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2922 0 : newtype = tx_unsafe_fn_variant (newtype);
2923 :
2924 16224405 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2925 :
2926 16224405 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2927 15649029 : check_default_args (newdecl);
2928 :
2929 : /* Lay the type out, unless already done. */
2930 16224405 : if (! same_type_p (newtype, oldtype)
2931 370 : && TREE_TYPE (newdecl) != error_mark_node
2932 16224775 : && !(processing_template_decl && uses_template_parms (newdecl)))
2933 287 : layout_type (TREE_TYPE (newdecl));
2934 :
2935 16224405 : if ((VAR_P (newdecl)
2936 15699158 : || TREE_CODE (newdecl) == PARM_DECL
2937 15699158 : || TREE_CODE (newdecl) == RESULT_DECL
2938 15699158 : || TREE_CODE (newdecl) == FIELD_DECL
2939 15699158 : || TREE_CODE (newdecl) == TYPE_DECL)
2940 16274534 : && !(processing_template_decl && uses_template_parms (newdecl)))
2941 404835 : layout_decl (newdecl, 0);
2942 :
2943 : /* Merge deprecatedness. */
2944 16224405 : if (TREE_DEPRECATED (newdecl))
2945 12067 : TREE_DEPRECATED (olddecl) = 1;
2946 :
2947 : /* Merge unavailability. */
2948 16224405 : if (TREE_UNAVAILABLE (newdecl))
2949 3 : TREE_UNAVAILABLE (olddecl) = 1;
2950 :
2951 : /* Preserve function specific target and optimization options */
2952 16224405 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2953 : {
2954 15649029 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2955 15649029 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2956 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2957 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2958 :
2959 15649029 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2960 15649029 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2961 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2962 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2963 :
2964 15649029 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2965 15113134 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2966 : }
2967 : else
2968 : {
2969 : /* Merge the const type qualifier. */
2970 575376 : if (TREE_READONLY (newdecl))
2971 157313 : TREE_READONLY (olddecl) = 1;
2972 : /* Merge the volatile type qualifier. */
2973 575376 : if (TREE_THIS_VOLATILE (newdecl))
2974 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2975 : }
2976 :
2977 : /* Merge the initialization information. */
2978 16224405 : if (DECL_INITIAL (newdecl) == NULL_TREE
2979 16224405 : && DECL_INITIAL (olddecl) != NULL_TREE)
2980 : {
2981 191416 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2982 191416 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2983 191416 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2984 : {
2985 17482 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2986 17482 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2987 : }
2988 : }
2989 :
2990 16224405 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2991 : {
2992 15649029 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2993 15649029 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2994 15649029 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2995 15649029 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2996 58746 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2997 15649029 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2998 15649029 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2999 15649029 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
3000 15649029 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
3001 :
3002 15649029 : if (merge_attr)
3003 15068206 : merge_attribute_bits (newdecl, olddecl);
3004 : else
3005 : {
3006 : /* Merge the noreturn bit. */
3007 580823 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3008 580823 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3009 580823 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3010 580823 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
3011 580823 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
3012 : }
3013 : /* Keep the old RTL. */
3014 15649029 : COPY_DECL_RTL (olddecl, newdecl);
3015 : }
3016 575376 : else if (VAR_P (newdecl)
3017 575376 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3018 : {
3019 : /* Keep the old RTL. We cannot keep the old RTL if the old
3020 : declaration was for an incomplete object and the new
3021 : declaration is not since many attributes of the RTL will
3022 : change. */
3023 525173 : COPY_DECL_RTL (olddecl, newdecl);
3024 : }
3025 : }
3026 : /* If cannot merge, then use the new type and qualifiers,
3027 : and don't preserve the old rtl. */
3028 : else
3029 : {
3030 : /* Clean out any memory we had of the old declaration. */
3031 201 : tree oldstatic = value_member (olddecl, static_aggregates);
3032 201 : if (oldstatic)
3033 0 : TREE_VALUE (oldstatic) = error_mark_node;
3034 :
3035 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3036 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3037 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3038 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
3039 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3040 : }
3041 :
3042 : /* Merge the storage class information. */
3043 16224606 : merge_weak (newdecl, olddecl);
3044 :
3045 16224606 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3046 16224606 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3047 16224606 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3048 16224606 : if (! DECL_EXTERNAL (olddecl))
3049 54787 : DECL_EXTERNAL (newdecl) = 0;
3050 16224606 : if (! DECL_COMDAT (olddecl))
3051 6905121 : DECL_COMDAT (newdecl) = 0;
3052 :
3053 16224606 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
3054 : {
3055 78 : if (!DECL_LOCAL_DECL_P (olddecl))
3056 : /* This can happen if olddecl was brought in from the
3057 : enclosing namespace via a using-decl. The new decl is
3058 : then not a block-scope extern at all. */
3059 3 : DECL_LOCAL_DECL_P (newdecl) = false;
3060 : else
3061 : {
3062 75 : retrofit_lang_decl (newdecl);
3063 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
3064 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
3065 75 : if (alias != error_mark_node)
3066 : {
3067 72 : DECL_ATTRIBUTES (alias)
3068 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
3069 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3070 42 : merge_attribute_bits (newdecl, alias);
3071 : }
3072 : }
3073 : }
3074 :
3075 16224606 : new_template_info = NULL_TREE;
3076 16224606 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3077 : {
3078 16170897 : bool new_redefines_gnu_inline = false;
3079 :
3080 16170897 : if (new_defines_function
3081 16170897 : && ((DECL_INTERFACE_KNOWN (olddecl)
3082 1249 : && TREE_CODE (olddecl) == FUNCTION_DECL)
3083 9507270 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
3084 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3085 : == FUNCTION_DECL))))
3086 2498 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
3087 :
3088 : if (!new_redefines_gnu_inline)
3089 : {
3090 16170819 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3091 16170819 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3092 16170819 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3093 : }
3094 :
3095 16170897 : if (TREE_CODE (newdecl) != TYPE_DECL)
3096 : {
3097 16170833 : DECL_TEMPLATE_INSTANTIATED (newdecl)
3098 16170833 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3099 16170833 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
3100 :
3101 : /* If the OLDDECL is an instantiation and/or specialization,
3102 : then the NEWDECL must be too. But, it may not yet be marked
3103 : as such if the caller has created NEWDECL, but has not yet
3104 : figured out that it is a redeclaration. */
3105 16170833 : if (!DECL_USE_TEMPLATE (newdecl))
3106 15504724 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
3107 :
3108 16170833 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
3109 15589782 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
3110 31179564 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3111 : }
3112 :
3113 : /* Don't really know how much of the language-specific
3114 : values we should copy from old to new. */
3115 16170897 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3116 :
3117 16170897 : if (LANG_DECL_HAS_MIN (newdecl))
3118 : {
3119 16170897 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3120 16170897 : if (DECL_TEMPLATE_INFO (newdecl))
3121 : {
3122 666142 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
3123 666142 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3124 666142 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3125 : /* Remember the presence of explicit specialization args. */
3126 1161652 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3127 1161652 : = TINFO_USED_TEMPLATE_ID (new_template_info);
3128 : }
3129 :
3130 : /* We don't want to copy template info from a non-templated friend
3131 : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3132 16170897 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3133 : || !non_templated_friend_p (olddecl));
3134 16170897 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3135 : }
3136 :
3137 16170897 : if (DECL_DECLARES_FUNCTION_P (newdecl))
3138 : {
3139 : /* Only functions have these fields. */
3140 15649230 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3141 15649230 : DECL_BEFRIENDING_CLASSES (newdecl)
3142 15649230 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3143 15649230 : DECL_BEFRIENDING_CLASSES (olddecl));
3144 : /* DECL_THUNKS is only valid for virtual functions,
3145 : otherwise it is a DECL_FRIEND_CONTEXT. */
3146 15649230 : if (DECL_VIRTUAL_P (newdecl))
3147 648630 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3148 30001200 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3149 385102 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3150 : }
3151 521667 : else if (VAR_P (newdecl))
3152 : {
3153 : /* Only variables have this field. */
3154 521603 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3155 198 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3156 : }
3157 : }
3158 :
3159 16224606 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3160 : {
3161 15649230 : merge_decl_arguments (newdecl, olddecl, new_defines_function,
3162 : types_match, false);
3163 :
3164 15649230 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3165 15649230 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3166 : {
3167 : /* If newdecl is not a specialization, then it is not a
3168 : template-related function at all. And that means that we
3169 : should have exited above, returning 0. */
3170 580823 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3171 :
3172 580823 : if (DECL_ODR_USED (olddecl))
3173 : /* From [temp.expl.spec]:
3174 :
3175 : If a template, a member template or the member of a class
3176 : template is explicitly specialized then that
3177 : specialization shall be declared before the first use of
3178 : that specialization that would cause an implicit
3179 : instantiation to take place, in every translation unit in
3180 : which such a use occurs. */
3181 0 : error ("explicit specialization of %qD after first use",
3182 : olddecl);
3183 :
3184 580823 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3185 580823 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3186 1161493 : && DECL_NONGNU_INLINE_P (newdecl));
3187 :
3188 : /* Don't propagate visibility from the template to the
3189 : specialization here. We'll do that in determine_visibility if
3190 : appropriate. */
3191 580823 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3192 :
3193 : /* [temp.expl.spec/14] We don't inline explicit specialization
3194 : just because the primary template says so. */
3195 580823 : gcc_assert (!merge_attr);
3196 :
3197 580823 : DECL_DECLARED_INLINE_P (olddecl)
3198 580823 : = DECL_DECLARED_INLINE_P (newdecl);
3199 :
3200 580823 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
3201 580823 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3202 :
3203 580823 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3204 : }
3205 15068407 : else if (new_defines_function && DECL_INITIAL (olddecl))
3206 : {
3207 : /* Never inline re-defined extern inline functions.
3208 : FIXME: this could be better handled by keeping both
3209 : function as separate declarations. */
3210 78 : DECL_UNINLINABLE (newdecl) = 1;
3211 : }
3212 : else
3213 : {
3214 15068329 : if (DECL_PENDING_INLINE_P (olddecl))
3215 : {
3216 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
3217 6 : DECL_PENDING_INLINE_INFO (newdecl)
3218 12 : = DECL_PENDING_INLINE_INFO (olddecl);
3219 : }
3220 15068323 : else if (DECL_PENDING_INLINE_P (newdecl))
3221 : ;
3222 15068323 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3223 15068323 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3224 30136646 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3225 :
3226 15068329 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3227 :
3228 45204987 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3229 30136658 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3230 :
3231 30136658 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3232 15068329 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3233 15068329 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3234 29714958 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3235 : }
3236 :
3237 : /* Preserve abstractness on cloned [cd]tors. */
3238 15649230 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3239 :
3240 : /* Update newdecl's parms to point at olddecl. */
3241 49828356 : for (tree parm = DECL_ARGUMENTS (newdecl); parm;
3242 34179126 : parm = DECL_CHAIN (parm))
3243 34179126 : DECL_CONTEXT (parm) = olddecl;
3244 :
3245 15649230 : if (! types_match)
3246 : {
3247 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3248 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3249 201 : COPY_DECL_RTL (newdecl, olddecl);
3250 : }
3251 15649230 : if (! types_match || new_defines_function)
3252 : {
3253 : /* Update the contracts to reflect the new parameter names. */
3254 9508720 : update_contract_arguments (newdecl, olddecl);
3255 :
3256 : /* Mark the old PARM_DECLs in case std::meta::parameters_of has
3257 : been called on the old declaration and reflections of those
3258 : arguments are held across this point and used later.
3259 : Such PARM_DECLs are no longer present in
3260 : DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain. */
3261 9508720 : for (tree oldarg = DECL_ARGUMENTS (olddecl);
3262 33376615 : oldarg; oldarg = DECL_CHAIN (oldarg))
3263 23867895 : OLD_PARM_DECL_P (oldarg) = 1;
3264 :
3265 : /* These need to be copied so that the names are available.
3266 : Note that if the types do match, we'll preserve inline
3267 : info and other bits, but if not, we won't. */
3268 9508720 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3269 9508720 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3270 : }
3271 : /* If redeclaring a builtin function, it stays built in
3272 : if newdecl is a gnu_inline definition, or if newdecl is just
3273 : a declaration. */
3274 15649230 : if (fndecl_built_in_p (olddecl)
3275 15707964 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3276 : {
3277 4102448 : copy_decl_built_in_function (newdecl, olddecl);
3278 : /* If we're keeping the built-in definition, keep the rtl,
3279 : regardless of declaration matches. */
3280 4102448 : COPY_DECL_RTL (olddecl, newdecl);
3281 4102448 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3282 : {
3283 4102442 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3284 4102442 : if (builtin_decl_explicit_p (fncode))
3285 : {
3286 : /* A compatible prototype of these builtin functions
3287 : is seen, assume the runtime implements it with
3288 : the expected semantics. */
3289 4102442 : switch (fncode)
3290 : {
3291 10164 : case BUILT_IN_STPCPY:
3292 10164 : set_builtin_decl_implicit_p (fncode, true);
3293 10164 : break;
3294 4092278 : default:
3295 4092278 : set_builtin_decl_declared_p (fncode, true);
3296 4092278 : break;
3297 : }
3298 : }
3299 :
3300 4102442 : copy_attributes_to_builtin (newdecl);
3301 : }
3302 : }
3303 15649230 : if (new_defines_function)
3304 : /* If defining a function declared with other language
3305 : linkage, use the previously declared language linkage. */
3306 9508519 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3307 6140711 : else if (types_match)
3308 : {
3309 6140510 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3310 : /* Don't clear out the arguments if we're just redeclaring a
3311 : function. */
3312 6140510 : if (DECL_ARGUMENTS (olddecl))
3313 1999067 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3314 : }
3315 : }
3316 575376 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3317 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3318 :
3319 : /* Now preserve various other info from the definition. */
3320 16224606 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3321 16224606 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3322 16224606 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3323 16224606 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3324 :
3325 : /* Warn about conflicting visibility specifications. */
3326 16224606 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3327 5715372 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3328 16236850 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3329 : {
3330 12 : auto_diagnostic_group d;
3331 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3332 : "%qD: visibility attribute ignored because it "
3333 : "conflicts with previous declaration", newdecl))
3334 12 : inform (olddecl_loc,
3335 : "previous declaration of %qD", olddecl);
3336 12 : }
3337 : /* Choose the declaration which specified visibility. */
3338 16224606 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3339 : {
3340 5715372 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3341 5715372 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3342 : }
3343 : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3344 : so keep this behavior. */
3345 16224606 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3346 : {
3347 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3348 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3349 : }
3350 : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3351 16224606 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3352 : {
3353 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3354 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3355 : }
3356 16224552 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3357 16224552 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3358 3 : DECL_USER_ALIGN (newdecl) = 1;
3359 :
3360 16224606 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3361 32449212 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3362 16224606 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3363 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3364 : DECL_WARN_IF_NOT_ALIGN (olddecl));
3365 16224606 : if (TREE_CODE (newdecl) == FIELD_DECL)
3366 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3367 :
3368 : /* Merge module entity mapping information. */
3369 16224606 : if (DECL_LANG_SPECIFIC (olddecl)
3370 32395549 : && (DECL_MODULE_ENTITY_P (olddecl)
3371 16170607 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3372 : {
3373 336 : retrofit_lang_decl (newdecl);
3374 336 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3375 336 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3376 : }
3377 :
3378 : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3379 : with that from NEWDECL below. */
3380 16224606 : if (DECL_LANG_SPECIFIC (olddecl))
3381 : {
3382 16170943 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3383 : != DECL_LANG_SPECIFIC (newdecl));
3384 16170943 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3385 : }
3386 :
3387 : /* Merge the USED information. */
3388 16224606 : if (TREE_USED (olddecl))
3389 512860 : TREE_USED (newdecl) = 1;
3390 15711746 : else if (TREE_USED (newdecl))
3391 62 : TREE_USED (olddecl) = 1;
3392 :
3393 16224606 : if (VAR_P (newdecl))
3394 : {
3395 525247 : if (DECL_READ_P (olddecl))
3396 45149 : DECL_READ_P (newdecl) = 1;
3397 480098 : else if (DECL_READ_P (newdecl))
3398 3 : DECL_READ_P (olddecl) = 1;
3399 : }
3400 :
3401 16224606 : if (DECL_PRESERVE_P (olddecl))
3402 48 : DECL_PRESERVE_P (newdecl) = 1;
3403 16224558 : else if (DECL_PRESERVE_P (newdecl))
3404 75 : DECL_PRESERVE_P (olddecl) = 1;
3405 :
3406 : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3407 : to olddecl and deleted. */
3408 16224606 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3409 31873836 : && DECL_FUNCTION_VERSIONED (olddecl))
3410 : {
3411 : /* Set the flag for newdecl so that it gets copied to olddecl. */
3412 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3413 : /* newdecl will be purged after copying to olddecl and is no longer
3414 : a version. */
3415 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3416 : }
3417 :
3418 16224606 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3419 : {
3420 15649230 : int function_size;
3421 15649230 : struct symtab_node *snode = symtab_node::get (olddecl);
3422 :
3423 15649230 : function_size = sizeof (struct tree_decl_common);
3424 :
3425 15649230 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3426 : (char *) newdecl + sizeof (struct tree_common),
3427 : function_size - sizeof (struct tree_common));
3428 :
3429 15649230 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3430 : (char *) newdecl + sizeof (struct tree_decl_common),
3431 : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3432 :
3433 : /* Preserve symtab node mapping. */
3434 15649230 : olddecl->decl_with_vis.symtab_node = snode;
3435 :
3436 15649230 : if (new_template_info)
3437 : /* If newdecl is a template instantiation, it is possible that
3438 : the following sequence of events has occurred:
3439 :
3440 : o A friend function was declared in a class template. The
3441 : class template was instantiated.
3442 :
3443 : o The instantiation of the friend declaration was
3444 : recorded on the instantiation list, and is newdecl.
3445 :
3446 : o Later, however, instantiate_class_template called pushdecl
3447 : on the newdecl to perform name injection. But, pushdecl in
3448 : turn called duplicate_decls when it discovered that another
3449 : declaration of a global function with the same name already
3450 : existed.
3451 :
3452 : o Here, in duplicate_decls, we decided to clobber newdecl.
3453 :
3454 : If we're going to do that, we'd better make sure that
3455 : olddecl, and not newdecl, is on the list of
3456 : instantiations so that if we try to do the instantiation
3457 : again we won't get the clobbered declaration. */
3458 666133 : reregister_specialization (newdecl,
3459 : new_template_info,
3460 : olddecl);
3461 : }
3462 : else
3463 : {
3464 575376 : size_t size = tree_code_size (TREE_CODE (newdecl));
3465 :
3466 575376 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3467 : (char *) newdecl + sizeof (struct tree_common),
3468 : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3469 :
3470 575376 : switch (TREE_CODE (newdecl))
3471 : {
3472 575376 : case LABEL_DECL:
3473 575376 : case VAR_DECL:
3474 575376 : case RESULT_DECL:
3475 575376 : case PARM_DECL:
3476 575376 : case FIELD_DECL:
3477 575376 : case TYPE_DECL:
3478 575376 : case CONST_DECL:
3479 575376 : {
3480 575376 : struct symtab_node *snode = NULL;
3481 :
3482 575376 : if (VAR_P (newdecl)
3483 575376 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3484 3 : || DECL_EXTERNAL (olddecl)))
3485 525247 : snode = symtab_node::get (olddecl);
3486 575376 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3487 : (char *) newdecl + sizeof (struct tree_decl_common),
3488 : size - sizeof (struct tree_decl_common)
3489 575376 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3490 575376 : if (VAR_P (newdecl))
3491 525247 : olddecl->decl_with_vis.symtab_node = snode;
3492 : }
3493 : break;
3494 0 : default:
3495 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3496 : (char *) newdecl + sizeof (struct tree_decl_common),
3497 : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3498 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3499 0 : break;
3500 : }
3501 : }
3502 :
3503 16224606 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3504 : {
3505 16174477 : if (DECL_EXTERNAL (olddecl)
3506 526624 : || TREE_PUBLIC (olddecl)
3507 16174546 : || TREE_STATIC (olddecl))
3508 : {
3509 : /* Merge the section attribute.
3510 : We want to issue an error if the sections conflict but that must be
3511 : done later in decl_attributes since we are called before attributes
3512 : are assigned. */
3513 16174477 : if (DECL_SECTION_NAME (newdecl) != NULL)
3514 7 : set_decl_section_name (olddecl, newdecl);
3515 :
3516 16174477 : if (DECL_ONE_ONLY (newdecl))
3517 : {
3518 0 : struct symtab_node *oldsym, *newsym;
3519 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3520 0 : oldsym = cgraph_node::get_create (olddecl);
3521 : else
3522 0 : oldsym = varpool_node::get_create (olddecl);
3523 0 : newsym = symtab_node::get (newdecl);
3524 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3525 : }
3526 : }
3527 :
3528 16174477 : if (VAR_P (newdecl)
3529 16174477 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3530 : {
3531 404 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3532 404 : if (!processing_template_decl)
3533 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3534 : }
3535 : }
3536 :
3537 16224606 : DECL_UID (olddecl) = olddecl_uid;
3538 :
3539 : /* NEWDECL contains the merged attribute lists.
3540 : Update OLDDECL to be the same. */
3541 16224606 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3542 :
3543 : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3544 : so that encode_section_info has a chance to look at the new decl
3545 : flags and attributes. */
3546 16224606 : if (DECL_RTL_SET_P (olddecl)
3547 16224621 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3548 15 : || (VAR_P (olddecl)
3549 15 : && TREE_STATIC (olddecl))))
3550 15 : make_decl_rtl (olddecl);
3551 :
3552 : /* The NEWDECL will no longer be needed. Because every out-of-class
3553 : declaration of a member results in a call to duplicate_decls,
3554 : freeing these nodes represents in a significant savings.
3555 :
3556 : Before releasing the node, be sore to remove function from symbol
3557 : table that might have been inserted there to record comdat group.
3558 : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3559 : structure is shared in between newdecl and oldecl. */
3560 16224606 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3561 15649230 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3562 16224606 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3563 : {
3564 16174477 : struct symtab_node *snode = symtab_node::get (newdecl);
3565 16174477 : if (snode)
3566 399 : snode->remove ();
3567 : }
3568 :
3569 16224606 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3570 : {
3571 15649230 : tree clone;
3572 16202545 : FOR_EACH_CLONE (clone, olddecl)
3573 : {
3574 553315 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3575 553315 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3576 : }
3577 : }
3578 :
3579 : /* Remove the associated constraints for newdecl, if any, before
3580 : reclaiming memory. */
3581 16224606 : if (flag_concepts)
3582 15946466 : remove_constraints (newdecl);
3583 :
3584 16224606 : if (flag_contracts)
3585 : /* Remove the specifiers, and then remove the decl from the lookup. */
3586 1523343 : remove_decl_with_fn_contracts_specifiers (newdecl);
3587 :
3588 : /* And similarly for any module tracking data. */
3589 16224606 : if (modules_p ())
3590 45274 : remove_defining_module (newdecl);
3591 :
3592 16224606 : ggc_free (newdecl);
3593 :
3594 16224606 : return olddecl;
3595 : }
3596 :
3597 : /* Return zero if the declaration NEWDECL is valid
3598 : when the declaration OLDDECL (assumed to be for the same name)
3599 : has already been seen.
3600 : Otherwise return an error message format string with a %s
3601 : where the identifier should go. */
3602 :
3603 : static const char *
3604 16421856 : redeclaration_error_message (tree newdecl, tree olddecl)
3605 : {
3606 16421892 : if (TREE_CODE (newdecl) == TYPE_DECL)
3607 : {
3608 : /* Because C++ can put things into name space for free,
3609 : constructs like "typedef struct foo { ... } foo"
3610 : would look like an erroneous redeclaration. */
3611 83786 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3612 : return NULL;
3613 : else
3614 0 : return G_("redefinition of %q#D");
3615 : }
3616 16338106 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3617 : {
3618 : /* If this is a pure function, its olddecl will actually be
3619 : the original initialization to `0' (which we force to call
3620 : abort()). Don't complain about redefinition in this case. */
3621 23264186 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3622 11632111 : && DECL_INITIAL (olddecl) == NULL_TREE)
3623 : return NULL;
3624 :
3625 : /* If both functions come from different namespaces, this is not
3626 : a redeclaration - this is a conflict with a used function. */
3627 23264156 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3628 2732091 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3629 11632081 : && ! decls_match (olddecl, newdecl))
3630 : return G_("%qD conflicts with used function");
3631 :
3632 : /* We'll complain about linkage mismatches in
3633 : warn_extern_redeclared_static. */
3634 :
3635 : /* Defining the same name twice is no good. */
3636 11632078 : if (decl_defined_p (olddecl)
3637 11632078 : && decl_defined_p (newdecl))
3638 : {
3639 306 : if (DECL_NAME (olddecl) == NULL_TREE)
3640 : return G_("%q#D not declared in class");
3641 447 : else if (!GNU_INLINE_P (olddecl)
3642 393 : || GNU_INLINE_P (newdecl))
3643 : return G_("redefinition of %q#D");
3644 : }
3645 :
3646 13519660 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3647 : {
3648 1834954 : bool olda = GNU_INLINE_P (olddecl);
3649 1834954 : bool newa = GNU_INLINE_P (newdecl);
3650 :
3651 1834954 : if (olda != newa)
3652 : {
3653 15 : if (newa)
3654 : return G_("%q+D redeclared inline with "
3655 : "%<gnu_inline%> attribute");
3656 : else
3657 9 : return G_("%q+D redeclared inline without "
3658 : "%<gnu_inline%> attribute");
3659 : }
3660 : }
3661 :
3662 : /* [class.compare.default]: A definition of a comparison operator as
3663 : defaulted that appears in a class shall be the first declaration of
3664 : that function. */
3665 11631841 : special_function_kind sfk = special_function_p (olddecl);
3666 11631841 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3667 : return G_("comparison operator %q+D defaulted after "
3668 : "its first declaration");
3669 :
3670 11631838 : check_abi_tag_redeclaration
3671 11631838 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3672 11631838 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3673 :
3674 11631838 : return NULL;
3675 : }
3676 4706013 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3677 : {
3678 4180403 : tree nt, ot;
3679 :
3680 4180403 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3681 : return G_("redefinition of %q#D");
3682 :
3683 4180397 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3684 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3685 36 : DECL_TEMPLATE_RESULT (olddecl));
3686 :
3687 4180361 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3688 : return NULL;
3689 :
3690 4180349 : nt = DECL_TEMPLATE_RESULT (newdecl);
3691 4180349 : if (DECL_TEMPLATE_INFO (nt))
3692 1002269 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3693 4180349 : ot = DECL_TEMPLATE_RESULT (olddecl);
3694 4180349 : if (DECL_TEMPLATE_INFO (ot))
3695 4180349 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3696 7018922 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3697 4180388 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3698 : return G_("redefinition of %q#D");
3699 :
3700 6779891 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3701 : {
3702 1895198 : bool olda = GNU_INLINE_P (ot);
3703 1895198 : bool newa = GNU_INLINE_P (nt);
3704 :
3705 1895198 : if (olda != newa)
3706 : {
3707 0 : if (newa)
3708 : return G_("%q+D redeclared inline with "
3709 : "%<gnu_inline%> attribute");
3710 : else
3711 0 : return G_("%q+D redeclared inline without "
3712 : "%<gnu_inline%> attribute");
3713 : }
3714 : }
3715 :
3716 : /* Core issue #226 (C++11):
3717 :
3718 : If a friend function template declaration specifies a
3719 : default template-argument, that declaration shall be a
3720 : definition and shall be the only declaration of the
3721 : function template in the translation unit. */
3722 4180334 : if ((cxx_dialect != cxx98)
3723 4161440 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3724 4392762 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3725 : /*is_primary=*/true,
3726 : /*is_partial=*/false,
3727 : /*is_friend_decl=*/2))
3728 3 : return G_("redeclaration of friend %q#D "
3729 : "may not have default template arguments");
3730 :
3731 : return NULL;
3732 : }
3733 525610 : else if (VAR_P (newdecl)
3734 525526 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3735 525526 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3736 525657 : && (! DECL_LANG_SPECIFIC (olddecl)
3737 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3738 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3739 : {
3740 : /* Only variables can be thread-local, and all declarations must
3741 : agree on this property. */
3742 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3743 : return G_("thread-local declaration of %q#D follows "
3744 : "non-thread-local declaration");
3745 : else
3746 3 : return G_("non-thread-local declaration of %q#D follows "
3747 : "thread-local declaration");
3748 : }
3749 525604 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3750 : {
3751 : /* The objects have been declared at namespace scope. If either
3752 : is a member of an anonymous union, then this is an invalid
3753 : redeclaration. For example:
3754 :
3755 : int i;
3756 : union { int i; };
3757 :
3758 : is invalid. */
3759 525370 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3760 1050809 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3761 : return G_("redeclaration of %q#D");
3762 : /* If at least one declaration is a reference, there is no
3763 : conflict. For example:
3764 :
3765 : int i = 3;
3766 : extern int i;
3767 :
3768 : is valid. */
3769 525436 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3770 : return NULL;
3771 :
3772 : /* Static data member declared outside a class definition
3773 : if the variable is defined within the class with constexpr
3774 : specifier is declaration rather than definition (and
3775 : deprecated). */
3776 2820 : if (cxx_dialect >= cxx17
3777 2747 : && VAR_P (olddecl)
3778 2691 : && DECL_CLASS_SCOPE_P (olddecl)
3779 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3780 5457 : && !DECL_INITIAL (newdecl))
3781 : {
3782 2637 : DECL_EXTERNAL (newdecl) = 1;
3783 : /* For now, only warn with explicit -Wdeprecated. */
3784 2637 : if (OPTION_SET_P (warn_deprecated))
3785 : {
3786 40 : auto_diagnostic_group d;
3787 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3788 : "redundant redeclaration of %<constexpr%> "
3789 : "static data member %qD", newdecl))
3790 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3791 : "previous declaration of %qD", olddecl);
3792 40 : }
3793 : return NULL;
3794 : }
3795 :
3796 : /* Reject two definitions. */
3797 : return G_("redefinition of %q#D");
3798 : }
3799 : else
3800 : {
3801 : /* Objects declared with block scope: */
3802 : /* Reject two definitions, and reject a definition
3803 : together with an external reference. */
3804 153 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3805 153 : return G_("redeclaration of %q#D");
3806 : return NULL;
3807 : }
3808 : }
3809 :
3810 :
3811 : /* Hash and equality functions for the named_label table. */
3812 :
3813 : hashval_t
3814 79607 : named_label_hash::hash (const value_type entry)
3815 : {
3816 79607 : return IDENTIFIER_HASH_VALUE (entry->name);
3817 : }
3818 :
3819 : bool
3820 76526 : named_label_hash::equal (const value_type entry, compare_type name)
3821 : {
3822 76526 : return name == entry->name;
3823 : }
3824 :
3825 : /* Look for a label named ID in the current function. If one cannot
3826 : be found, create one. Return the named_label_entry, or NULL on
3827 : failure. */
3828 :
3829 : static named_label_entry *
3830 24479 : lookup_label_1 (tree id, bool making_local_p)
3831 : {
3832 24479 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3833 :
3834 : /* You can't use labels at global scope. */
3835 24479 : if (current_function_decl == NULL_TREE)
3836 : {
3837 3 : error ("label %qE referenced outside of any function", id);
3838 3 : return NULL;
3839 : }
3840 :
3841 24476 : if (!named_labels)
3842 4726 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3843 :
3844 24476 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3845 24476 : named_label_entry **slot
3846 24476 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3847 24476 : named_label_entry *old = *slot;
3848 :
3849 24476 : if (old && old->label_decl)
3850 : {
3851 3521 : if (!making_local_p)
3852 : return old;
3853 :
3854 21 : if (old->binding_level == current_binding_level)
3855 : {
3856 0 : auto_diagnostic_group d;
3857 0 : error ("local label %qE conflicts with existing label", id);
3858 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3859 0 : return NULL;
3860 0 : }
3861 : }
3862 :
3863 : /* We are making a new decl, create or reuse the named_label_entry */
3864 20997 : named_label_entry *ent = NULL;
3865 33 : if (old && !old->label_decl)
3866 : ent = old;
3867 : else
3868 : {
3869 20964 : ent = ggc_cleared_alloc<named_label_entry> ();
3870 20964 : ent->name = id;
3871 20964 : ent->outer = old;
3872 20964 : *slot = ent;
3873 : }
3874 :
3875 : /* Now create the LABEL_DECL. */
3876 20976 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3877 :
3878 20976 : DECL_CONTEXT (decl) = current_function_decl;
3879 20976 : SET_DECL_MODE (decl, VOIDmode);
3880 20976 : if (making_local_p)
3881 : {
3882 219 : C_DECLARED_LABEL_FLAG (decl) = true;
3883 219 : DECL_CHAIN (decl) = current_binding_level->names;
3884 219 : current_binding_level->names = decl;
3885 : }
3886 :
3887 20976 : ent->label_decl = decl;
3888 :
3889 20976 : return ent;
3890 24479 : }
3891 :
3892 : /* Wrapper for lookup_label_1. */
3893 :
3894 : tree
3895 3006 : lookup_label (tree id)
3896 : {
3897 3006 : named_label_entry *ent = lookup_label_1 (id, false);
3898 3006 : return ent ? ent->label_decl : NULL_TREE;
3899 : }
3900 :
3901 : /* Remember that we've seen &&ID. */
3902 :
3903 : void
3904 262 : mark_label_addressed (tree id)
3905 : {
3906 262 : named_label_entry *ent = lookup_label_1 (id, false);
3907 262 : ent->addressed = true;
3908 262 : }
3909 :
3910 : tree
3911 219 : declare_local_label (tree id)
3912 : {
3913 219 : named_label_entry *ent = lookup_label_1 (id, true);
3914 219 : return ent ? ent->label_decl : NULL_TREE;
3915 : }
3916 :
3917 : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3918 :
3919 : static bool
3920 2966660 : decl_jump_unsafe (tree decl)
3921 : {
3922 : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3923 : with automatic storage duration is not in scope to a point where it is
3924 : in scope is ill-formed unless the variable has scalar type, class type
3925 : with a trivial default constructor and a trivial destructor, a
3926 : cv-qualified version of one of these types, or an array of one of the
3927 : preceding types and is declared without an initializer (8.5). */
3928 2966660 : tree type = TREE_TYPE (decl);
3929 :
3930 2966660 : return (type != error_mark_node
3931 2966642 : && VAR_P (decl)
3932 15082 : && !TREE_STATIC (decl)
3933 2981606 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3934 12998 : || variably_modified_type_p (type, NULL_TREE)));
3935 : }
3936 :
3937 : /* Returns true if decl is an automatic variable with vacuous initialization
3938 : except when it is [[indeterminate]] or [[gnu::uninitialized]].
3939 : Jumps across such initialization need to be instrumented for
3940 : !!flag_auto_var_init. */
3941 :
3942 : static bool
3943 2964703 : decl_instrument_init_bypass_p (tree decl)
3944 : {
3945 2964703 : tree type = TREE_TYPE (decl);
3946 :
3947 2964703 : return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
3948 2209743 : && !processing_template_decl
3949 2209497 : && type != error_mark_node
3950 2209491 : && VAR_P (decl)
3951 1072 : && !TREE_STATIC (decl)
3952 1051 : && !DECL_EXTERNAL (decl)
3953 2074 : && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3954 1037 : || variably_modified_type_p (type, NULL_TREE))
3955 1037 : && !lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (decl))
3956 1037 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
3957 2965740 : && !DECL_HAS_VALUE_EXPR_P (decl));
3958 : }
3959 :
3960 : /* Build .DEFERRED_INIT call for DECL. */
3961 :
3962 : static tree
3963 140 : build_deferred_init_call (tree decl)
3964 : {
3965 140 : tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
3966 280 : tree init_type_arg = build_int_cst (integer_type_node,
3967 140 : (int) flag_auto_var_init);
3968 140 : location_t loc = DECL_SOURCE_LOCATION (decl);
3969 140 : tree decl_name;
3970 :
3971 140 : if (DECL_NAME (decl))
3972 140 : decl_name = build_string_literal (DECL_NAME (decl));
3973 : else
3974 : {
3975 0 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
3976 0 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
3977 0 : decl_name = build_string_literal (decl_name_anonymous);
3978 : }
3979 :
3980 140 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
3981 140 : TREE_TYPE (decl), 3,
3982 : decl_size_arg, init_type_arg,
3983 : decl_name);
3984 140 : tree ret = build2_loc (loc, MODIFY_EXPR, void_type_node, decl, call);
3985 140 : return build_stmt (loc, EXPR_STMT, ret);
3986 : }
3987 :
3988 : /* Emit before ITER (and any labels/case labels before it) code like
3989 : if (0)
3990 : {
3991 : l1:
3992 : v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
3993 : v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
3994 : v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
3995 : v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
3996 : }
3997 : and return l1 label, or if it already exists, assert it has the
3998 : .DEFERRED_INIT calls for the right decls in the right order and
3999 : amend it, either by adding extra labels in between or further
4000 : ,DEFERRED_INIT calls before the first label and extra label before
4001 : that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
4002 : of adding a label. DECLS points to an array of NDECLS VAR_DECLs
4003 : which should be initialized. */
4004 :
4005 : static tree
4006 127 : maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
4007 : tree *decls, unsigned ndecls)
4008 : {
4009 127 : tree lab = NULL_TREE;
4010 191 : for (; !tsi_end_p (iter); tsi_prev (&iter))
4011 : {
4012 185 : switch (TREE_CODE (tsi_stmt (iter)))
4013 : {
4014 64 : case LABEL_EXPR:
4015 64 : case CASE_LABEL_EXPR:
4016 64 : case DEBUG_BEGIN_STMT:
4017 64 : continue;
4018 : default:
4019 : break;
4020 : }
4021 : break;
4022 : }
4023 127 : if (!tsi_end_p (iter)
4024 121 : && TREE_CODE (tsi_stmt (iter)) == IF_STMT
4025 204 : && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
4026 : {
4027 : /* Found IF_STMT added for this or some adjacent
4028 : LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
4029 : The decls are ordered so that we can always reuse it. Sometimes
4030 : by no modifications at all and just returning the right label
4031 : which was added already before, sometimes by adding a label in
4032 : between two previously added .DEFERRED_INIT calls and sometimes
4033 : by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
4034 : before that) before the statements in IF_STMT body. */
4035 71 : tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
4036 71 : iter = tsi_last (then_clause);
4037 71 : bool add = false;
4038 452 : for (unsigned int i = 0; i < ndecls; ++i)
4039 : {
4040 310 : tree decl = decls[i];
4041 310 : if (!add)
4042 : {
4043 : /* Skip over labels/case labels after .DEFERRED_INIT for the
4044 : DECL we are looking for. */
4045 399 : while (!tsi_end_p (iter)
4046 399 : && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
4047 286 : || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
4048 26 : && !case_label)))
4049 119 : tsi_prev (&iter);
4050 280 : if (tsi_end_p (iter))
4051 : {
4052 : /* Reached the start, we'll need to prepend further
4053 : statements. */
4054 20 : add = true;
4055 20 : iter = tsi_start (then_clause);
4056 : }
4057 : else
4058 : {
4059 : /* Found something, assert it is .DEFERRED_INIT for
4060 : DECL. */
4061 260 : tree t = tsi_stmt (iter);
4062 260 : gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
4063 260 : t = EXPR_STMT_EXPR (t);
4064 260 : gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
4065 : && TREE_OPERAND (t, 0) == decl
4066 : && (TREE_CODE (TREE_OPERAND (t, 1))
4067 : == CALL_EXPR));
4068 260 : t = TREE_OPERAND (t, 1);
4069 260 : gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
4070 : && (CALL_EXPR_IFN (t)
4071 : == IFN_DEFERRED_INIT));
4072 260 : tsi_prev (&iter);
4073 : }
4074 : }
4075 280 : if (add)
4076 : {
4077 : /* If reached the start in this or some earlier iteration,
4078 : prepend .DEFERRED_INIT call for DECL. */
4079 50 : tree t = build_deferred_init_call (decl);
4080 50 : STMT_IS_FULL_EXPR_P (t) = 1;
4081 50 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4082 : }
4083 : }
4084 71 : if (!add)
4085 : {
4086 : /* If .DEFERRED_INIT calls for all the decls were already there,
4087 : skip over case labels and if we find a LABEL_EXPR, return
4088 : its label. */
4089 63 : while (!tsi_end_p (iter)
4090 63 : && !case_label
4091 117 : && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4092 12 : tsi_prev (&iter);
4093 51 : if (tsi_end_p (iter))
4094 : {
4095 : /* Only case labels were found and we are looking for normal
4096 : label, we'll need to add it. */
4097 0 : add = true;
4098 0 : iter = tsi_start (then_clause);
4099 : }
4100 51 : else if (!case_label
4101 51 : && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4102 : /* Return existing label. */
4103 21 : lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4104 : else
4105 : {
4106 : /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4107 30 : gcc_checking_assert (case_label
4108 : || (TREE_CODE (tsi_stmt (iter))
4109 : == EXPR_STMT));
4110 30 : add = true;
4111 30 : tsi_next (&iter);
4112 30 : gcc_checking_assert (!tsi_end_p (iter));
4113 : }
4114 : }
4115 21 : if (add)
4116 : {
4117 50 : tree t;
4118 50 : if (case_label)
4119 : t = case_label;
4120 : else
4121 : {
4122 41 : lab = create_artificial_label (UNKNOWN_LOCATION);
4123 41 : t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4124 : }
4125 50 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4126 : }
4127 : }
4128 : else
4129 : {
4130 : /* No IF_STMT created by this function found. Create it all
4131 : from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4132 : followed by .DEFERRED_INIT calls inside of a new if (0). */
4133 56 : tree new_then = push_stmt_list ();
4134 56 : if (!case_label)
4135 : {
4136 23 : lab = create_artificial_label (UNKNOWN_LOCATION);
4137 23 : add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4138 : }
4139 : else
4140 33 : add_stmt (case_label);
4141 146 : for (unsigned int i = ndecls; i; --i)
4142 90 : add_stmt (build_deferred_init_call (decls[i - 1]));
4143 56 : new_then = pop_stmt_list (new_then);
4144 56 : tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4145 : new_then, void_node, NULL_TREE);
4146 56 : IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4147 56 : if (tsi_end_p (iter))
4148 : {
4149 6 : iter = tsi_start (iter.container);
4150 6 : tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4151 : }
4152 : else
4153 50 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4154 : }
4155 127 : return lab;
4156 : }
4157 :
4158 : /* Adjust backward gotos to named label ENT if they jump over vacuous
4159 : initializers if !!flag_auto_var_init. ITER is the location of
4160 : LABEL_EXPR for that named label. */
4161 :
4162 : static void
4163 19 : adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4164 : {
4165 19 : auto_vec<tree, 4> decls;
4166 19 : unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4167 19 : tree decl;
4168 107 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4169 88 : if (!decl_jump_unsafe (decl))
4170 : {
4171 88 : gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4172 88 : decls.safe_push (decl);
4173 176 : if (decls.length () == max_cnt)
4174 : break;
4175 : }
4176 19 : named_label_bck_direct_goto *dgoto;
4177 19 : unsigned last = 0;
4178 19 : tree lab = NULL_TREE;
4179 103 : FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4180 : {
4181 46 : if (dgoto->n_bad_decls != last)
4182 : {
4183 40 : last = dgoto->n_bad_decls;
4184 80 : lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4185 : decls.address (), last);
4186 : }
4187 46 : *dgoto->direct_goto = lab;
4188 : }
4189 19 : }
4190 :
4191 : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4192 : to the user. */
4193 :
4194 : static bool
4195 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
4196 : enum diagnostics::kind diag_kind, bool computed)
4197 : {
4198 286 : if (computed)
4199 43 : diag_kind = diagnostics::kind::warning;
4200 286 : auto_diagnostic_group d;
4201 286 : bool complained
4202 373 : = emit_diagnostic (diag_kind, loc, 0,
4203 : decl ? G_("jump to label %qD")
4204 : : G_("jump to case label"), decl);
4205 286 : if (complained && locus)
4206 : {
4207 199 : if (computed)
4208 43 : inform (*locus, " as a possible target of computed goto");
4209 : else
4210 156 : inform (*locus, " from here");
4211 : }
4212 572 : return complained;
4213 286 : }
4214 :
4215 : /* Check that a single previously seen jump to a newly defined label
4216 : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4217 : the jump context; NAMES are the names in scope in LEVEL at the jump
4218 : context; LOCUS is the source position of the jump or 0. COMPUTED
4219 : is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4220 : vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4221 : initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4222 : CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4223 : Returns non-zero if all is well, 2 if any vacuous initializers were
4224 : crossed. */
4225 :
4226 : static int
4227 1825403 : check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4228 : bool exited_omp, const location_t *locus,
4229 : vec<tree, va_gc> *computed,
4230 : vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4231 : tree case_label)
4232 : {
4233 1825403 : auto_diagnostic_group d;
4234 1825403 : cp_binding_level *b;
4235 1825403 : bool complained = false;
4236 1825403 : int identified = 0;
4237 1825403 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4238 1825403 : bool saw_ceif = false, saw_se = false;
4239 1825403 : auto_vec<tree> vacuous_decls;
4240 1825403 : bool vacuous_inits = false;
4241 :
4242 1825403 : if (exited_omp)
4243 : {
4244 12 : complained = identify_goto (decl, input_location, locus,
4245 : diagnostics::kind::error,
4246 : computed);
4247 12 : if (complained)
4248 12 : inform (input_location, " exits OpenMP structured block");
4249 : saw_omp = true;
4250 : identified = 2;
4251 : }
4252 :
4253 3621372 : for (b = current_binding_level; b ; b = b->level_chain)
4254 : {
4255 3621372 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4256 :
4257 6582797 : for (new_decls = b->names; new_decls != old_decls;
4258 5912162 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4259 2950737 : : TREE_CHAIN (new_decls)))
4260 : {
4261 2961425 : bool problem = decl_jump_unsafe (new_decls);
4262 2961425 : if (! problem)
4263 : {
4264 2961380 : if (decl_instrument_init_bypass_p (new_decls))
4265 : {
4266 323 : if (direct_goto || case_label)
4267 254 : vacuous_decls.safe_push (new_decls);
4268 : vacuous_inits = true;
4269 : }
4270 2961380 : continue;
4271 : }
4272 :
4273 45 : if (!identified)
4274 : {
4275 45 : complained = identify_goto (decl, input_location, locus,
4276 : diagnostics::kind::error,
4277 : computed);
4278 45 : identified = 2;
4279 : }
4280 45 : if (complained)
4281 45 : inform (DECL_SOURCE_LOCATION (new_decls),
4282 : " crosses initialization of %q#D", new_decls);
4283 : }
4284 :
4285 3621372 : if (b == level)
4286 : break;
4287 :
4288 1795969 : const char *inf = NULL;
4289 1795969 : location_t loc = input_location;
4290 1795969 : switch (b->kind)
4291 : {
4292 3 : case sk_try:
4293 3 : if (!saw_eh)
4294 : inf = G_(" enters %<try%> block");
4295 : saw_eh = true;
4296 : break;
4297 :
4298 9 : case sk_catch:
4299 9 : if (!saw_eh)
4300 : inf = G_(" enters %<catch%> block");
4301 : saw_eh = true;
4302 : break;
4303 :
4304 102 : case sk_omp:
4305 102 : if (!saw_omp)
4306 : inf = G_(" enters OpenMP structured block");
4307 : saw_omp = true;
4308 : break;
4309 :
4310 3 : case sk_transaction:
4311 3 : if (!saw_tm)
4312 : inf = G_(" enters synchronized or atomic statement");
4313 : saw_tm = true;
4314 : break;
4315 :
4316 18 : case sk_stmt_expr:
4317 18 : if (!saw_se)
4318 : inf = G_(" enters statement expression");
4319 : saw_se = true;
4320 : break;
4321 :
4322 1795694 : case sk_block:
4323 1795694 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
4324 : {
4325 9 : inf = G_(" enters %<constexpr if%> statement");
4326 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4327 : saw_cxif = true;
4328 : }
4329 1795685 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
4330 : {
4331 18 : inf = G_(" enters %<consteval if%> statement");
4332 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4333 : saw_ceif = true;
4334 : }
4335 : break;
4336 :
4337 : default:
4338 : break;
4339 : }
4340 :
4341 27 : if (inf)
4342 : {
4343 141 : if (identified < 2)
4344 141 : complained = identify_goto (decl, input_location, locus,
4345 : diagnostics::kind::error,
4346 : computed);
4347 141 : identified = 2;
4348 141 : if (complained)
4349 141 : inform (loc, inf);
4350 : }
4351 : }
4352 :
4353 1825403 : if (!vec_safe_is_empty (computed))
4354 : {
4355 9 : if (!identified)
4356 6 : complained = identify_goto (decl, input_location, locus,
4357 : diagnostics::kind::error,
4358 : computed);
4359 9 : identified = 2;
4360 9 : if (complained)
4361 18 : for (tree d : computed)
4362 : {
4363 9 : if (DECL_P (d))
4364 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4365 3 : else if (d == get_identifier ("catch"))
4366 3 : inform (*locus, " does not clean up handled exception");
4367 : }
4368 : }
4369 :
4370 1825492 : if (!vacuous_decls.is_empty () && !seen_error ())
4371 : {
4372 87 : tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4373 87 : if (case_label)
4374 : {
4375 42 : gcc_checking_assert (tsi_stmt (iter) == case_label);
4376 42 : tsi_delink (&iter);
4377 42 : iter = tsi_last (cur_stmt_list);
4378 : }
4379 87 : tree lab = maybe_add_deferred_init_calls (iter, case_label,
4380 : vacuous_decls.address (),
4381 : vacuous_decls.length ());
4382 87 : if (lab)
4383 : {
4384 : unsigned int i;
4385 : named_label_fwd_direct_goto *dgoto;
4386 177 : FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4387 45 : *dgoto->direct_goto = lab;
4388 : }
4389 : }
4390 :
4391 1825403 : if (identified)
4392 : return 0;
4393 1825199 : return vacuous_inits ? 2 : 1;
4394 1825403 : }
4395 :
4396 : static void
4397 2142 : check_previous_goto (tree decl, struct named_label_use_entry *use)
4398 : {
4399 2142 : check_previous_goto_1 (decl, use->binding_level,
4400 : use->names_in_scope, use->in_omp_scope,
4401 2142 : &use->o_goto_locus, use->computed_goto,
4402 : use->direct_goto, NULL_TREE);
4403 2142 : vec_free (use->direct_goto);
4404 2142 : }
4405 :
4406 : static int
4407 1823261 : check_switch_goto (cp_binding_level *level, tree case_label)
4408 : {
4409 1823261 : return check_previous_goto_1 (NULL_TREE, level, level->names,
4410 1823261 : false, NULL, nullptr, nullptr, case_label);
4411 : }
4412 :
4413 : /* Check that a new jump to a label ENT is OK. DECLP is a pointer
4414 : to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4415 :
4416 : void
4417 2884 : check_goto_1 (named_label_entry *ent, tree *declp)
4418 : {
4419 2884 : auto_diagnostic_group d;
4420 2884 : tree decl = ent->label_decl;
4421 2884 : bool computed = declp == NULL;
4422 :
4423 : /* If the label hasn't been defined yet, defer checking. */
4424 2884 : if (! DECL_INITIAL (decl))
4425 : {
4426 : /* Don't bother creating another use if the last goto had the
4427 : same data, and will therefore create the same set of errors. */
4428 2214 : if (ent->uses
4429 661 : && ent->uses->binding_level == current_binding_level
4430 63 : && ent->uses->names_in_scope == current_binding_level->names)
4431 : {
4432 63 : if (declp
4433 57 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4434 12 : && !processing_template_decl)
4435 10 : vec_safe_push (ent->uses->direct_goto,
4436 10 : named_label_fwd_direct_goto { declp });
4437 : return;
4438 : }
4439 :
4440 2151 : named_label_use_entry *new_use
4441 2151 : = ggc_alloc<named_label_use_entry> ();
4442 2151 : new_use->binding_level = current_binding_level;
4443 2151 : new_use->names_in_scope = current_binding_level->names;
4444 2151 : new_use->o_goto_locus = input_location;
4445 2151 : new_use->in_omp_scope = false;
4446 2151 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4447 2151 : new_use->direct_goto = nullptr;
4448 2151 : if (declp
4449 2060 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4450 368 : && !processing_template_decl)
4451 311 : vec_safe_push (new_use->direct_goto,
4452 311 : named_label_fwd_direct_goto { declp });
4453 :
4454 2151 : new_use->next = ent->uses;
4455 2151 : ent->uses = new_use;
4456 2151 : return;
4457 : }
4458 :
4459 670 : bool saw_catch = false, complained = false;
4460 670 : int identified = 0;
4461 670 : tree bad;
4462 670 : unsigned ix;
4463 670 : unsigned n_bad_decls = 0;
4464 :
4465 670 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4466 655 : || ent->in_constexpr_if || ent->in_consteval_if
4467 649 : || ent->in_omp_scope || ent->in_stmt_expr
4468 631 : || ent->has_bad_decls)
4469 : {
4470 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4471 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4472 30 : || ent->in_consteval_if || ent->in_transaction_scope
4473 24 : || ent->in_omp_scope || ent->in_stmt_expr)
4474 39 : diag_kind = diagnostics::kind::error;
4475 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4476 : &input_location, diag_kind, computed);
4477 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
4478 : }
4479 :
4480 860 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4481 : {
4482 190 : bool problem = decl_jump_unsafe (bad);
4483 190 : if (!problem)
4484 : {
4485 184 : gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4486 184 : n_bad_decls++;
4487 184 : continue;
4488 : }
4489 :
4490 6 : if (DECL_ARTIFICIAL (bad))
4491 : {
4492 : /* Can't skip init of __exception_info. */
4493 0 : if (identified == 1)
4494 : {
4495 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4496 : &input_location,
4497 : diagnostics::kind::error,
4498 : computed);
4499 0 : identified = 2;
4500 : }
4501 0 : if (complained)
4502 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4503 : saw_catch = true;
4504 : }
4505 6 : else if (complained)
4506 6 : inform (DECL_SOURCE_LOCATION (bad),
4507 : " skips initialization of %q#D", bad);
4508 : }
4509 :
4510 670 : if (complained)
4511 : {
4512 45 : if (ent->in_try_scope)
4513 9 : inform (input_location, " enters %<try%> block");
4514 36 : else if (ent->in_catch_scope && !saw_catch)
4515 3 : inform (input_location, " enters %<catch%> block");
4516 33 : else if (ent->in_transaction_scope)
4517 3 : inform (input_location, " enters synchronized or atomic statement");
4518 30 : else if (ent->in_constexpr_if)
4519 3 : inform (input_location, " enters %<constexpr if%> statement");
4520 27 : else if (ent->in_consteval_if)
4521 3 : inform (input_location, " enters %<consteval if%> statement");
4522 24 : else if (ent->in_stmt_expr)
4523 15 : inform (input_location, " enters statement expression");
4524 : }
4525 :
4526 670 : if (ent->in_omp_scope)
4527 : {
4528 3 : if (complained)
4529 3 : inform (input_location, " enters OpenMP structured block");
4530 : }
4531 667 : else if (flag_openmp)
4532 42 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4533 : {
4534 42 : if (b == ent->binding_level)
4535 : break;
4536 36 : if (b->kind == sk_omp)
4537 : {
4538 21 : if (identified < 2)
4539 : {
4540 42 : complained = identify_goto (decl,
4541 21 : DECL_SOURCE_LOCATION (decl),
4542 : &input_location,
4543 : diagnostics::kind::error,
4544 : computed);
4545 21 : identified = 2;
4546 : }
4547 21 : if (complained)
4548 21 : inform (input_location, " exits OpenMP structured block");
4549 : break;
4550 : }
4551 : }
4552 :
4553 : /* Warn if a computed goto might involve a local variable going out of scope
4554 : without being cleaned up. */
4555 670 : if (computed)
4556 : {
4557 80 : auto level = ent->binding_level;
4558 80 : auto names = ent->names_in_scope;
4559 80 : for (auto b = current_binding_level; ; b = b->level_chain)
4560 : {
4561 138 : if (b->kind == sk_catch)
4562 : {
4563 6 : if (!identified)
4564 : {
4565 6 : complained
4566 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4567 : &input_location, diagnostics::kind::error,
4568 : computed);
4569 6 : identified = 2;
4570 : }
4571 6 : if (complained)
4572 6 : inform (input_location,
4573 : " does not clean up handled exception");
4574 : }
4575 138 : tree end = b == level ? names : NULL_TREE;
4576 154 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4577 : {
4578 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4579 : {
4580 10 : if (!identified)
4581 : {
4582 10 : complained
4583 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4584 : &input_location, diagnostics::kind::error,
4585 : computed);
4586 10 : identified = 2;
4587 : }
4588 10 : if (complained)
4589 10 : inform (DECL_SOURCE_LOCATION (d),
4590 : " does not destroy %qD", d);
4591 : }
4592 : }
4593 138 : if (b == level)
4594 : break;
4595 58 : }
4596 : }
4597 :
4598 670 : if (n_bad_decls && declp)
4599 46 : vec_safe_push (ent->direct_goto,
4600 46 : named_label_bck_direct_goto { declp, n_bad_decls });
4601 2884 : }
4602 :
4603 : /* Check that a new jump to a label *DECLP is OK. Called by
4604 : finish_goto_stmt. */
4605 :
4606 : void
4607 2844 : check_goto (tree *declp)
4608 : {
4609 2844 : if (!named_labels)
4610 : return;
4611 2829 : tree decl = *declp;
4612 2829 : if (TREE_CODE (decl) != LABEL_DECL)
4613 : {
4614 : /* We don't know where a computed goto is jumping,
4615 : so check all addressable labels. */
4616 330 : for (auto iter = named_labels->begin ();
4617 452 : iter != named_labels->end ();
4618 208 : ++iter)
4619 : {
4620 208 : auto ent = *iter;
4621 208 : if (ent->addressed)
4622 177 : check_goto_1 (ent, NULL);
4623 : }
4624 : }
4625 : else
4626 : {
4627 2707 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4628 2707 : named_label_entry **slot
4629 2707 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4630 2707 : named_label_entry *ent = *slot;
4631 2707 : check_goto_1 (ent, declp);
4632 : }
4633 : }
4634 :
4635 : /* Check that a return is ok wrt OpenMP structured blocks.
4636 : Called by finish_return_stmt. Returns true if all is well. */
4637 :
4638 : bool
4639 280015 : check_omp_return (void)
4640 : {
4641 830443 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4642 830443 : if (b->kind == sk_omp)
4643 : {
4644 15 : error ("invalid exit from OpenMP structured block");
4645 15 : return false;
4646 : }
4647 830428 : else if (b->kind == sk_function_parms)
4648 : break;
4649 : return true;
4650 : }
4651 :
4652 : /* Define a label, specifying the location in the source file.
4653 : Return the LABEL_DECL node for the label. */
4654 :
4655 : tree
4656 20973 : define_label (location_t location, tree name)
4657 : {
4658 20973 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4659 :
4660 : /* After labels, make any new cleanups in the function go into their
4661 : own new (temporary) binding contour. */
4662 20973 : for (cp_binding_level *p = current_binding_level;
4663 31772 : p->kind != sk_function_parms;
4664 10799 : p = p->level_chain)
4665 10799 : p->more_cleanups_ok = 0;
4666 :
4667 20973 : named_label_entry *ent = lookup_label_1 (name, false);
4668 20973 : tree decl = ent->label_decl;
4669 :
4670 20973 : if (DECL_INITIAL (decl) != NULL_TREE)
4671 : {
4672 6 : error ("duplicate label %qD", decl);
4673 6 : return error_mark_node;
4674 : }
4675 : else
4676 : {
4677 : /* Mark label as having been defined. */
4678 20967 : DECL_INITIAL (decl) = error_mark_node;
4679 : /* Say where in the source. */
4680 20967 : DECL_SOURCE_LOCATION (decl) = location;
4681 :
4682 20967 : ent->binding_level = current_binding_level;
4683 20967 : ent->names_in_scope = current_binding_level->names;
4684 :
4685 23109 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4686 2142 : check_previous_goto (decl, use);
4687 20967 : ent->uses = NULL;
4688 : }
4689 :
4690 20967 : return decl;
4691 20973 : }
4692 :
4693 : struct cp_switch
4694 : {
4695 : cp_binding_level *level;
4696 : struct cp_switch *next;
4697 : /* The SWITCH_STMT being built. */
4698 : tree switch_stmt;
4699 : /* A splay-tree mapping the low element of a case range to the high
4700 : element, or NULL_TREE if there is no high element. Used to
4701 : determine whether or not a new case label duplicates an old case
4702 : label. We need a tree, rather than simply a hash table, because
4703 : of the GNU case range extension. */
4704 : splay_tree cases;
4705 : /* Remember whether a default: case label has been seen. */
4706 : bool has_default_p;
4707 : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4708 : bool break_stmt_seen_p;
4709 : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4710 : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4711 : bool in_loop_body_p;
4712 : };
4713 :
4714 : /* A stack of the currently active switch statements. The innermost
4715 : switch statement is on the top of the stack. There is no need to
4716 : mark the stack for garbage collection because it is only active
4717 : during the processing of the body of a function, and we never
4718 : collect at that point. */
4719 :
4720 : static struct cp_switch *switch_stack;
4721 :
4722 : /* Called right after a switch-statement condition is parsed.
4723 : SWITCH_STMT is the switch statement being parsed. */
4724 :
4725 : void
4726 730167 : push_switch (tree switch_stmt)
4727 : {
4728 730167 : struct cp_switch *p = XNEW (struct cp_switch);
4729 730167 : p->level = current_binding_level;
4730 730167 : p->next = switch_stack;
4731 730167 : p->switch_stmt = switch_stmt;
4732 730167 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4733 730167 : p->has_default_p = false;
4734 730167 : p->break_stmt_seen_p = false;
4735 730167 : p->in_loop_body_p = false;
4736 730167 : switch_stack = p;
4737 730167 : }
4738 :
4739 : void
4740 730167 : pop_switch (void)
4741 : {
4742 730167 : struct cp_switch *cs = switch_stack;
4743 :
4744 : /* Emit warnings as needed. */
4745 730167 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4746 730167 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4747 730167 : const bool bool_cond_p
4748 730167 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4749 730167 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4750 730167 : if (!processing_template_decl)
4751 301907 : c_do_switch_warnings (cs->cases, switch_location,
4752 301907 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4753 : bool_cond_p);
4754 :
4755 : /* For the benefit of block_may_fallthru remember if the switch body
4756 : case labels cover all possible values and if there are break; stmts. */
4757 730167 : if (cs->has_default_p
4758 730167 : || (!processing_template_decl
4759 42583 : && c_switch_covers_all_cases_p (cs->cases,
4760 42583 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4761 540385 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4762 730167 : if (!cs->break_stmt_seen_p)
4763 375286 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4764 : /* Now that we're done with the switch warnings, set the switch type
4765 : to the type of the condition if the index type was of scoped enum type.
4766 : (Such types don't participate in the integer promotions.) We do this
4767 : because of bit-fields whose declared type is a scoped enum type:
4768 : gimplification will use the lowered index type, but convert the
4769 : case values to SWITCH_STMT_TYPE, which would have been the declared type
4770 : and verify_gimple_switch doesn't accept that. */
4771 730167 : if (is_bitfield_expr_with_lowered_type (cond))
4772 32866 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4773 730167 : gcc_assert (!cs->in_loop_body_p);
4774 730167 : splay_tree_delete (cs->cases);
4775 730167 : switch_stack = switch_stack->next;
4776 730167 : free (cs);
4777 730167 : }
4778 :
4779 : /* Note that a BREAK_STMT is about to be added. If it is inside of
4780 : a SWITCH_STMT and not inside of a loop body inside of it, note
4781 : in switch_stack we've seen a BREAK_STMT. */
4782 :
4783 : void
4784 4932602 : note_break_stmt (void)
4785 : {
4786 4932602 : if (switch_stack && !switch_stack->in_loop_body_p)
4787 2775512 : switch_stack->break_stmt_seen_p = true;
4788 4932602 : }
4789 :
4790 : /* Note the start of processing of an iteration statement's body.
4791 : The note_break_stmt function will do nothing while processing it.
4792 : Return a flag that should be passed to note_iteration_stmt_body_end. */
4793 :
4794 : bool
4795 18617346 : note_iteration_stmt_body_start (void)
4796 : {
4797 18617346 : if (!switch_stack)
4798 : return false;
4799 197583 : bool ret = switch_stack->in_loop_body_p;
4800 197583 : switch_stack->in_loop_body_p = true;
4801 197583 : return ret;
4802 : }
4803 :
4804 : /* Note the end of processing of an iteration statement's body. */
4805 :
4806 : void
4807 18617346 : note_iteration_stmt_body_end (bool prev)
4808 : {
4809 18617346 : if (switch_stack)
4810 197583 : switch_stack->in_loop_body_p = prev;
4811 18617346 : }
4812 :
4813 : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4814 : condition. Note that if TYPE and VALUE are already integral we don't
4815 : really do the conversion because the language-independent
4816 : warning/optimization code will work better that way. */
4817 :
4818 : static tree
4819 3646190 : case_conversion (tree type, tree value)
4820 : {
4821 3646190 : if (value == NULL_TREE)
4822 : return value;
4823 :
4824 1563944 : value = mark_rvalue_use (value);
4825 :
4826 1563944 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4827 1121674 : type = type_promotes_to (type);
4828 :
4829 1563944 : tree ovalue = value;
4830 : /* The constant-expression VALUE shall be a converted constant expression
4831 : of the adjusted type of the switch condition, which doesn't allow
4832 : narrowing conversions. */
4833 1563944 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4834 :
4835 1563944 : if (cxx_dialect >= cxx11
4836 1563944 : && (SCOPED_ENUM_P (type)
4837 1118576 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4838 : /* Use the converted value. */;
4839 : else
4840 : /* The already integral case. */
4841 : value = ovalue;
4842 :
4843 1563944 : return cxx_constant_value (value);
4844 : }
4845 :
4846 : /* Note that we've seen a definition of a case label, and complain if this
4847 : is a bad place for one. */
4848 :
4849 : tree
4850 5894573 : finish_case_label (location_t loc, tree low_value, tree high_value)
4851 : {
4852 5894573 : tree cond, r;
4853 5894573 : cp_binding_level *p;
4854 5894573 : tree type;
4855 :
4856 5894573 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4857 540349 : switch_stack->has_default_p = true;
4858 :
4859 5894573 : if (processing_template_decl)
4860 : {
4861 4071355 : tree label;
4862 :
4863 : /* For templates, just add the case label; we'll do semantic
4864 : analysis at instantiation-time. But diagnose case labels
4865 : in expansion statements with switch outside of it here. */
4866 4071355 : if (in_expansion_stmt)
4867 39 : for (cp_binding_level *b = current_binding_level;
4868 78 : b != switch_stack->level; b = b->level_chain)
4869 63 : if (b->kind == sk_template_for && b->this_entity)
4870 : {
4871 24 : auto_diagnostic_group d;
4872 24 : error ("jump to case label");
4873 24 : inform (EXPR_LOCATION (b->this_entity),
4874 : " enters %<template for%> statement");
4875 24 : return error_mark_node;
4876 24 : }
4877 4071331 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4878 4071331 : return add_stmt (build_case_label (low_value, high_value, label));
4879 : }
4880 :
4881 : /* Find the condition on which this switch statement depends. */
4882 1823218 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4883 1823218 : if (cond && TREE_CODE (cond) == TREE_LIST)
4884 0 : cond = TREE_VALUE (cond);
4885 :
4886 1823218 : int chk_switch_goto = check_switch_goto (switch_stack->level, NULL_TREE);
4887 1823218 : if (!chk_switch_goto)
4888 87 : return error_mark_node;
4889 :
4890 1823131 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4891 1823131 : if (type == error_mark_node)
4892 : return error_mark_node;
4893 :
4894 1823095 : low_value = case_conversion (type, low_value);
4895 1823095 : high_value = case_conversion (type, high_value);
4896 :
4897 1823095 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4898 :
4899 1823095 : if (r != error_mark_node && chk_switch_goto == 2)
4900 43 : check_switch_goto (switch_stack->level, r);
4901 :
4902 : /* After labels, make any new cleanups in the function go into their
4903 : own new (temporary) binding contour. */
4904 1823095 : for (p = current_binding_level;
4905 7653288 : p->kind != sk_function_parms;
4906 5830193 : p = p->level_chain)
4907 5830193 : p->more_cleanups_ok = 0;
4908 :
4909 : return r;
4910 : }
4911 :
4912 : struct typename_info {
4913 : tree scope;
4914 : tree name;
4915 : tree template_id;
4916 : tag_types tag_type;
4917 : };
4918 :
4919 : struct typename_hasher : ggc_ptr_hash<tree_node>
4920 : {
4921 : typedef typename_info *compare_type;
4922 :
4923 : /* Hash a TYPENAME_TYPE. */
4924 :
4925 : static hashval_t
4926 494049042 : hash (tree context, tree fullname)
4927 : {
4928 494049042 : hashval_t hash = 0;
4929 494049042 : hash = iterative_hash_hashval_t (TYPE_HASH (context), hash);
4930 : /* FULLNAME could be a template-id, so use iterative_hash_template_arg here.
4931 : And might as well set comparing_specializations for stronger hashing. */
4932 494049042 : ++comparing_specializations;
4933 494049042 : hash = iterative_hash_template_arg (fullname, hash);
4934 494049042 : --comparing_specializations;
4935 494049042 : return hash;
4936 : }
4937 :
4938 : static hashval_t
4939 65637292 : hash (const typename_info *ti)
4940 : {
4941 65637292 : return typename_hasher::hash (ti->scope, ti->template_id);
4942 : }
4943 :
4944 : static hashval_t
4945 428411750 : hash (tree t)
4946 : {
4947 428411750 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4948 : }
4949 :
4950 : /* Compare two TYPENAME_TYPEs. */
4951 :
4952 : static bool
4953 499720526 : equal (tree t1, const typename_info *t2)
4954 : {
4955 499720526 : return (TYPE_IDENTIFIER (t1) == t2->name
4956 163042805 : && TYPE_CONTEXT (t1) == t2->scope
4957 34412399 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4958 533414607 : && get_typename_tag (t1) == t2->tag_type);
4959 : }
4960 : };
4961 :
4962 : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4963 : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4964 :
4965 : Returns the new TYPENAME_TYPE. */
4966 :
4967 : static GTY (()) hash_table<typename_hasher> *typename_htab;
4968 :
4969 : tree
4970 65637292 : build_typename_type (tree context, tree name, tree fullname,
4971 : enum tag_types tag_type)
4972 : {
4973 65637292 : typename_info ti;
4974 :
4975 65637292 : if (typename_htab == NULL)
4976 20507 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4977 :
4978 65637292 : ti.scope = FROB_CONTEXT (context);
4979 65637292 : ti.name = name;
4980 65637292 : ti.template_id = fullname;
4981 65637292 : ti.tag_type = tag_type;
4982 65637292 : hashval_t hash = typename_hasher::hash (&ti);
4983 :
4984 : /* See if we already have this type. */
4985 65637292 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4986 65637292 : tree t = *e;
4987 65637292 : if (*e)
4988 : t = *e;
4989 : else
4990 : {
4991 : /* Build the TYPENAME_TYPE. */
4992 33124911 : t = cxx_make_type (TYPENAME_TYPE);
4993 33124911 : TYPE_CONTEXT (t) = ti.scope;
4994 33124911 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4995 33124911 : set_typename_tag (t, ti.tag_type);
4996 :
4997 : /* Build the corresponding TYPE_DECL. */
4998 33124911 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4999 33124911 : TYPE_NAME (t) = d;
5000 33124911 : TYPE_STUB_DECL (t) = d;
5001 33124911 : DECL_CONTEXT (d) = ti.scope;
5002 33124911 : DECL_ARTIFICIAL (d) = 1;
5003 :
5004 : /* Store it in the hash table. */
5005 33124911 : *e = t;
5006 :
5007 : /* TYPENAME_TYPEs must always be compared structurally, because
5008 : they may or may not resolve down to another type depending on
5009 : the currently open classes. */
5010 33124911 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5011 : }
5012 :
5013 65637292 : return t;
5014 : }
5015 :
5016 : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
5017 : provided to name the type. Returns an appropriate type, unless an
5018 : error occurs, in which case error_mark_node is returned. If we
5019 : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
5020 : return that, rather than the _TYPE it corresponds to, in other
5021 : cases we look through the type decl. If TF_ERROR is set, complain
5022 : about errors, otherwise be quiet. */
5023 :
5024 : tree
5025 278045633 : make_typename_type (tree context, tree name, enum tag_types tag_type,
5026 : tsubst_flags_t complain)
5027 : {
5028 278045633 : tree fullname;
5029 278045633 : tree t;
5030 278045633 : bool want_template;
5031 :
5032 278045633 : if (name == error_mark_node
5033 278045627 : || context == NULL_TREE
5034 278045627 : || context == error_mark_node)
5035 : return error_mark_node;
5036 :
5037 278045611 : if (TYPE_P (name))
5038 : {
5039 0 : if (!(TYPE_LANG_SPECIFIC (name)
5040 0 : && (CLASSTYPE_IS_TEMPLATE (name)
5041 0 : || CLASSTYPE_USE_TEMPLATE (name))))
5042 0 : name = TYPE_IDENTIFIER (name);
5043 : else
5044 : /* Create a TEMPLATE_ID_EXPR for the type. */
5045 0 : name = build_nt (TEMPLATE_ID_EXPR,
5046 0 : CLASSTYPE_TI_TEMPLATE (name),
5047 0 : CLASSTYPE_TI_ARGS (name));
5048 : }
5049 278045611 : else if (TREE_CODE (name) == TYPE_DECL)
5050 0 : name = DECL_NAME (name);
5051 :
5052 278045611 : fullname = name;
5053 :
5054 278045611 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5055 : {
5056 5431834 : name = TREE_OPERAND (name, 0);
5057 5431834 : if (DECL_TYPE_TEMPLATE_P (name))
5058 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5059 5431834 : if (TREE_CODE (name) != IDENTIFIER_NODE)
5060 : {
5061 0 : if (complain & tf_error)
5062 0 : error ("%qD is not a type", name);
5063 0 : return error_mark_node;
5064 : }
5065 : }
5066 278045611 : if (TREE_CODE (name) == TEMPLATE_DECL)
5067 : {
5068 0 : if (complain & tf_error)
5069 0 : error ("%qD used without template arguments", name);
5070 0 : return error_mark_node;
5071 : }
5072 278045611 : else if (is_overloaded_fn (name))
5073 : {
5074 3 : if (complain & tf_error)
5075 3 : error ("%qD is a function, not a type", name);
5076 3 : return error_mark_node;
5077 : }
5078 278045608 : gcc_assert (identifier_p (name));
5079 278045608 : gcc_assert (TYPE_P (context));
5080 :
5081 278045608 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5082 : /* This can happen for C++17 variadic using (c++/88986). */;
5083 264507096 : else if (!MAYBE_CLASS_TYPE_P (context))
5084 : {
5085 6 : if (complain & tf_error)
5086 6 : error ("%q#T is not a class", context);
5087 6 : return error_mark_node;
5088 : }
5089 :
5090 : /* When the CONTEXT is a dependent type, NAME could refer to a
5091 : dependent base class of CONTEXT. But look inside it anyway
5092 : if CONTEXT is a currently open scope, in case it refers to a
5093 : member of the current instantiation or a non-dependent base;
5094 : lookup will stop when we hit a dependent base. */
5095 278045602 : if (!dependent_scope_p (context))
5096 : {
5097 : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5098 : (as per [temp.res.general]/3), unless
5099 : - the tag corresponds to a class-key or 'enum' so
5100 : [basic.lookup.elab] applies, or
5101 : - the tag corresponds to scope_type or tf_qualifying_scope is
5102 : set so [basic.lookup.qual]/1 applies, or
5103 : - we're inside a base-specifier so [class.derived.general]/2 applies;
5104 : the tag will already be class_type in that case.
5105 : TODO: If we'd set/track the scope_type tag thoroughly on all
5106 : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5107 : tf_qualifying_scope flag. */
5108 434693200 : bool want_type = (tag_type != none_type && tag_type != typename_type)
5109 217346600 : || (complain & tf_qualifying_scope);
5110 217346600 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5111 : }
5112 : else
5113 : t = NULL_TREE;
5114 :
5115 278045602 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5116 60699927 : return build_typename_type (context, name, fullname, tag_type);
5117 :
5118 217345675 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5119 :
5120 217345675 : if (!t)
5121 : {
5122 19172761 : if (complain & tf_error)
5123 : {
5124 223 : if (!COMPLETE_TYPE_P (context))
5125 18 : cxx_incomplete_type_error (NULL_TREE, context);
5126 : else
5127 409 : error (want_template ? G_("no class template named %q#T in %q#T")
5128 : : G_("no type named %q#T in %q#T"), name, context);
5129 : }
5130 19172761 : return error_mark_node;
5131 : }
5132 :
5133 : /* Pull out the template from an injected-class-name (or multiple). */
5134 198172914 : if (want_template)
5135 3453142 : t = maybe_get_template_decl_from_type_decl (t);
5136 :
5137 198172914 : if (TREE_CODE (t) == TREE_LIST)
5138 : {
5139 3 : if (complain & tf_error)
5140 : {
5141 3 : auto_diagnostic_group d;
5142 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
5143 3 : print_candidates (input_location, t);
5144 3 : }
5145 3 : return error_mark_node;
5146 : }
5147 :
5148 198172911 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5149 : {
5150 6 : if (complain & tf_error)
5151 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
5152 : context, name, t);
5153 6 : return error_mark_node;
5154 : }
5155 198172905 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
5156 : {
5157 2187 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5158 4377 : && DECL_TYPE_TEMPLATE_P (t))
5159 : /* The caller permits this typename-specifier to name a template
5160 : (because it appears in a CTAD-enabled context). */;
5161 : else
5162 : {
5163 23 : if (complain & tf_error)
5164 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
5165 : context, name, t);
5166 23 : return error_mark_node;
5167 : }
5168 : }
5169 :
5170 198172882 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5171 : context, complain))
5172 42 : return error_mark_node;
5173 :
5174 198172840 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5175 2176 : return make_template_placeholder (t);
5176 :
5177 198170664 : if (want_template)
5178 : {
5179 3453136 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5180 : NULL_TREE, context,
5181 : complain | tf_user);
5182 3453136 : if (t == error_mark_node)
5183 : return error_mark_node;
5184 3453131 : t = TYPE_NAME (t);
5185 : }
5186 :
5187 198170659 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5188 2398625 : t = TREE_TYPE (t);
5189 :
5190 198170659 : maybe_record_typedef_use (t);
5191 :
5192 198170659 : return t;
5193 : }
5194 :
5195 : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5196 : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5197 : in which case error_mark_node is returned.
5198 :
5199 : If PARM_LIST is non-NULL, also make sure that the template parameter
5200 : list of TEMPLATE_DECL matches.
5201 :
5202 : If COMPLAIN zero, don't complain about any errors that occur. */
5203 :
5204 : tree
5205 189277 : make_unbound_class_template (tree context, tree name, tree parm_list,
5206 : tsubst_flags_t complain)
5207 : {
5208 189277 : if (TYPE_P (name))
5209 0 : name = TYPE_IDENTIFIER (name);
5210 189277 : else if (DECL_P (name))
5211 0 : name = DECL_NAME (name);
5212 189277 : gcc_assert (identifier_p (name));
5213 :
5214 189277 : if (!dependent_type_p (context)
5215 189277 : || currently_open_class (context))
5216 : {
5217 93396 : tree tmpl = NULL_TREE;
5218 :
5219 93396 : if (MAYBE_CLASS_TYPE_P (context))
5220 93390 : tmpl = lookup_field (context, name, 0, false);
5221 :
5222 93390 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5223 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5224 :
5225 93384 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5226 : {
5227 12 : if (complain & tf_error)
5228 9 : error ("no class template named %q#T in %q#T", name, context);
5229 12 : return error_mark_node;
5230 : }
5231 :
5232 93384 : if (parm_list
5233 93384 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5234 : {
5235 0 : if (complain & tf_error)
5236 : {
5237 0 : auto_diagnostic_group d;
5238 0 : error ("template parameters do not match template %qD", tmpl);
5239 0 : inform (DECL_SOURCE_LOCATION (tmpl),
5240 : "%qD declared here", tmpl);
5241 0 : }
5242 0 : return error_mark_node;
5243 : }
5244 :
5245 93384 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5246 : complain))
5247 0 : return error_mark_node;
5248 :
5249 : return tmpl;
5250 : }
5251 :
5252 95881 : return make_unbound_class_template_raw (context, name, parm_list);
5253 : }
5254 :
5255 : /* Build an UNBOUND_CLASS_TEMPLATE. */
5256 :
5257 : tree
5258 95933 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5259 : {
5260 : /* Build the UNBOUND_CLASS_TEMPLATE. */
5261 95933 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5262 95933 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5263 95933 : TREE_TYPE (t) = NULL_TREE;
5264 95933 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5265 :
5266 : /* Build the corresponding TEMPLATE_DECL. */
5267 95933 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5268 95933 : TYPE_NAME (t) = d;
5269 95933 : TYPE_STUB_DECL (t) = d;
5270 95933 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5271 95933 : DECL_ARTIFICIAL (d) = 1;
5272 95933 : DECL_TEMPLATE_PARMS (d) = parm_list;
5273 :
5274 95933 : return t;
5275 : }
5276 :
5277 :
5278 :
5279 : /* Push the declarations of builtin types into the global namespace.
5280 : RID_INDEX is the index of the builtin type in the array
5281 : RID_POINTERS. NAME is the name used when looking up the builtin
5282 : type. TYPE is the _TYPE node for the builtin type.
5283 :
5284 : The calls to set_global_binding below should be
5285 : eliminated. Built-in types should not be looked up name; their
5286 : names are keywords that the parser can recognize. However, there
5287 : is code in c-common.cc that uses identifier_global_value to look up
5288 : built-in types by name. */
5289 :
5290 : void
5291 4209239 : record_builtin_type (enum rid rid_index,
5292 : const char* name,
5293 : tree type)
5294 : {
5295 4209239 : tree decl = NULL_TREE;
5296 :
5297 4209239 : if (name)
5298 : {
5299 2484096 : tree tname = get_identifier (name);
5300 2484096 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5301 2484096 : DECL_ARTIFICIAL (tdecl) = 1;
5302 2484096 : set_global_binding (tdecl);
5303 2484096 : decl = tdecl;
5304 : }
5305 :
5306 4209239 : if ((int) rid_index < (int) RID_MAX)
5307 2584275 : if (tree rname = ridpointers[(int) rid_index])
5308 1975401 : if (!decl || DECL_NAME (decl) != rname)
5309 : {
5310 1420706 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5311 1420706 : DECL_ARTIFICIAL (rdecl) = 1;
5312 1420706 : set_global_binding (rdecl);
5313 1420706 : if (!decl)
5314 : decl = rdecl;
5315 : }
5316 :
5317 3904802 : if (decl)
5318 : {
5319 3600365 : if (!TYPE_NAME (type))
5320 2991491 : TYPE_NAME (type) = decl;
5321 3600365 : debug_hooks->type_decl (decl, 0);
5322 : }
5323 4209239 : }
5324 :
5325 : /* Push a type into the namespace so that the back ends ignore it. */
5326 :
5327 : static void
5328 304437 : record_unknown_type (tree type, const char* name)
5329 : {
5330 304437 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5331 : TYPE_DECL, get_identifier (name), type));
5332 : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5333 304437 : DECL_IGNORED_P (decl) = 1;
5334 304437 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5335 304437 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5336 304437 : SET_TYPE_ALIGN (type, 1);
5337 304437 : TYPE_USER_ALIGN (type) = 0;
5338 304437 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5339 304437 : }
5340 :
5341 : /* Create all the predefined identifiers. */
5342 :
5343 : static void
5344 101479 : initialize_predefined_identifiers (void)
5345 : {
5346 101479 : struct predefined_identifier
5347 : {
5348 : const char *name; /* Name. */
5349 : tree *node; /* Node to store it in. */
5350 : cp_identifier_kind kind; /* Kind of identifier. */
5351 : };
5352 :
5353 : /* A table of identifiers to create at startup. */
5354 101479 : static const predefined_identifier predefined_identifiers[] = {
5355 : {"C++", &lang_name_cplusplus, cik_normal},
5356 : {"C", &lang_name_c, cik_normal},
5357 : /* Some of these names have a trailing space so that it is
5358 : impossible for them to conflict with names written by users. */
5359 : {"__ct ", &ctor_identifier, cik_ctor},
5360 : {"__ct_base ", &base_ctor_identifier, cik_ctor},
5361 : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
5362 : {"__dt ", &dtor_identifier, cik_dtor},
5363 : {"__dt_base ", &base_dtor_identifier, cik_dtor},
5364 : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
5365 : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
5366 : {"__conv_op ", &conv_op_identifier, cik_conv_op},
5367 : {"__in_chrg", &in_charge_identifier, cik_normal},
5368 : {"__as_base ", &as_base_identifier, cik_normal},
5369 : {"this", &this_identifier, cik_normal},
5370 : {"__delta", &delta_identifier, cik_normal},
5371 : {"__pfn", &pfn_identifier, cik_normal},
5372 : {"_vptr", &vptr_identifier, cik_normal},
5373 : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
5374 : {"::", &global_identifier, cik_normal},
5375 : /* The demangler expects anonymous namespaces to be called
5376 : something starting with '_GLOBAL__N_'. It no longer needs
5377 : to be unique to the TU. */
5378 : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
5379 : {"auto", &auto_identifier, cik_normal},
5380 : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
5381 : {"initializer_list", &init_list_identifier, cik_normal},
5382 : {"__for_range ", &for_range__identifier, cik_normal},
5383 : {"__for_begin ", &for_begin__identifier, cik_normal},
5384 : {"__for_end ", &for_end__identifier, cik_normal},
5385 : {"__for_range", &for_range_identifier, cik_normal},
5386 : {"__for_begin", &for_begin_identifier, cik_normal},
5387 : {"__for_end", &for_end_identifier, cik_normal},
5388 : {"abi_tag", &abi_tag_identifier, cik_normal},
5389 : {"aligned", &aligned_identifier, cik_normal},
5390 : {"begin", &begin_identifier, cik_normal},
5391 : {"end", &end_identifier, cik_normal},
5392 : {"get", &get__identifier, cik_normal},
5393 : {"gnu", &gnu_identifier, cik_normal},
5394 : {"tuple_element", &tuple_element_identifier, cik_normal},
5395 : {"tuple_size", &tuple_size_identifier, cik_normal},
5396 : {"type", &type_identifier, cik_normal},
5397 : {"value", &value_identifier, cik_normal},
5398 : {"_FUN", &fun_identifier, cik_normal},
5399 : {"__closure", &closure_identifier, cik_normal},
5400 : {"heap uninit", &heap_uninit_identifier, cik_normal},
5401 : {"heap ", &heap_identifier, cik_normal},
5402 : {"heap deleted", &heap_deleted_identifier, cik_normal},
5403 : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
5404 : {"heap []", &heap_vec_identifier, cik_normal},
5405 : {"omp", &omp_identifier, cik_normal},
5406 : {"internal ", &internal_identifier, cik_normal},
5407 : {"annotation ", &annotation_identifier, cik_normal},
5408 : {NULL, NULL, cik_normal}
5409 : };
5410 :
5411 4972471 : for (const predefined_identifier *pid = predefined_identifiers;
5412 4972471 : pid->name; ++pid)
5413 : {
5414 4870992 : *pid->node = get_identifier (pid->name);
5415 : /* Some of these identifiers already have a special kind. */
5416 4870992 : if (pid->kind != cik_normal)
5417 811832 : set_identifier_kind (*pid->node, pid->kind);
5418 : }
5419 101479 : }
5420 :
5421 : /* Build a specific variant of operator new. */
5422 :
5423 : static void
5424 181575 : cxx_build_operator_new (tree newtype)
5425 : {
5426 181575 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5427 181575 : DECL_IS_MALLOC (opnew) = 1;
5428 181575 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5429 181575 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5430 181575 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5431 181575 : DECL_IS_MALLOC (opnew) = 1;
5432 181575 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5433 181575 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5434 181575 : }
5435 :
5436 : /* Build a specific variant of operator delete. */
5437 :
5438 : static void
5439 343086 : cxx_build_operator_delete (tree deltype)
5440 : {
5441 343086 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5442 343086 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5443 343086 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5444 343086 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5445 343086 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5446 343086 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5447 343086 : }
5448 :
5449 : /* Declare all variants of operator new and delete. */
5450 :
5451 : static void
5452 101479 : cxx_init_operator_new_delete_decls (void)
5453 : {
5454 101479 : tree newattrs, extvisattr;
5455 101479 : tree newtype, deltype;
5456 101479 : tree ptr_ftype_sizetype;
5457 101479 : tree new_eh_spec;
5458 101479 : tree void_ftype_ptr = build_function_type_list (void_type_node,
5459 : ptr_type_node, NULL_TREE);
5460 101479 : void_ftype_ptr
5461 101479 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
5462 :
5463 101479 : ptr_ftype_sizetype
5464 101479 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5465 101479 : if (cxx_dialect == cxx98)
5466 : {
5467 14301 : tree bad_alloc_id;
5468 14301 : tree bad_alloc_type_node;
5469 14301 : tree bad_alloc_decl;
5470 :
5471 14301 : push_nested_namespace (std_node);
5472 14301 : bad_alloc_id = get_identifier ("bad_alloc");
5473 14301 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5474 14301 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5475 14301 : bad_alloc_decl
5476 14301 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5477 14301 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5478 14301 : pop_nested_namespace (std_node);
5479 :
5480 14301 : new_eh_spec
5481 14301 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5482 : }
5483 : else
5484 87178 : new_eh_spec = noexcept_false_spec;
5485 :
5486 101479 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5487 : NULL_TREE);
5488 101479 : newattrs = tree_cons (get_identifier ("alloc_size"),
5489 : build_tree_list (NULL_TREE, integer_one_node),
5490 : extvisattr);
5491 101479 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5492 101479 : newtype = build_exception_variant (newtype, new_eh_spec);
5493 101479 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5494 101479 : deltype = build_exception_variant (deltype, empty_except_spec);
5495 101479 : cxx_build_operator_new (newtype);
5496 101479 : cxx_build_operator_delete (deltype);
5497 101479 : if (flag_sized_deallocation)
5498 : {
5499 : /* Also push the sized deallocation variants:
5500 : void operator delete(void*, std::size_t) throw();
5501 : void operator delete[](void*, std::size_t) throw(); */
5502 81420 : tree void_ftype_ptr_size
5503 81420 : = build_function_type_list (void_type_node, ptr_type_node,
5504 : size_type_node, NULL_TREE);
5505 81420 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5506 : extvisattr);
5507 81420 : deltype = build_exception_variant (deltype, empty_except_spec);
5508 81420 : cxx_build_operator_delete (deltype);
5509 : }
5510 :
5511 101479 : if (aligned_new_threshold)
5512 : {
5513 80096 : push_nested_namespace (std_node);
5514 80096 : tree align_id = get_identifier ("align_val_t");
5515 80096 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5516 : NULL_TREE, /*scoped*/true, NULL);
5517 80096 : pop_nested_namespace (std_node);
5518 :
5519 : /* operator new (size_t, align_val_t); */
5520 80096 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5521 : align_type_node, NULL_TREE);
5522 80096 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5523 80096 : newtype = build_exception_variant (newtype, new_eh_spec);
5524 80096 : cxx_build_operator_new (newtype);
5525 :
5526 : /* operator delete (void *, align_val_t); */
5527 80096 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5528 : align_type_node, NULL_TREE);
5529 80096 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5530 80096 : deltype = build_exception_variant (deltype, empty_except_spec);
5531 80096 : cxx_build_operator_delete (deltype);
5532 :
5533 80096 : if (flag_sized_deallocation)
5534 : {
5535 : /* operator delete (void *, size_t, align_val_t); */
5536 80091 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5537 : size_type_node, align_type_node,
5538 : NULL_TREE);
5539 80091 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5540 80091 : deltype = build_exception_variant (deltype, empty_except_spec);
5541 80091 : cxx_build_operator_delete (deltype);
5542 : }
5543 : }
5544 101479 : }
5545 :
5546 : /* Create the predefined scalar types of C,
5547 : and some nodes representing standard constants (0, 1, (void *)0).
5548 : Initialize the global binding level.
5549 : Make definitions for built-in primitive functions. */
5550 :
5551 : void
5552 101479 : cxx_init_decl_processing (void)
5553 : {
5554 101479 : tree void_ftype;
5555 :
5556 : /* Create all the identifiers we need. */
5557 101479 : initialize_predefined_identifiers ();
5558 :
5559 : /* Create the global variables. */
5560 101479 : push_to_top_level ();
5561 :
5562 101479 : current_function_decl = NULL_TREE;
5563 101479 : current_binding_level = NULL;
5564 : /* Enter the global namespace. */
5565 101479 : gcc_assert (global_namespace == NULL_TREE);
5566 101479 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5567 : void_type_node);
5568 101479 : TREE_PUBLIC (global_namespace) = true;
5569 101479 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5570 101479 : DECL_CONTEXT (global_namespace)
5571 101479 : = build_translation_unit_decl (get_identifier (main_input_filename));
5572 : /* Remember whether we want the empty class passing ABI change warning
5573 : in this TU. */
5574 101479 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5575 128319 : = warn_abi && abi_version_crosses (12);
5576 101479 : debug_hooks->register_main_translation_unit
5577 101479 : (DECL_CONTEXT (global_namespace));
5578 101479 : begin_scope (sk_namespace, global_namespace);
5579 101479 : current_namespace = global_namespace;
5580 :
5581 101479 : if (flag_visibility_ms_compat)
5582 6 : default_visibility = VISIBILITY_HIDDEN;
5583 :
5584 : /* Initially, C. */
5585 101479 : current_lang_name = lang_name_c;
5586 :
5587 : /* Create the `std' namespace. */
5588 101479 : push_namespace (get_identifier ("std"));
5589 101479 : std_node = current_namespace;
5590 101479 : pop_namespace ();
5591 :
5592 101479 : flag_noexcept_type = (cxx_dialect >= cxx17);
5593 :
5594 101479 : c_common_nodes_and_builtins ();
5595 :
5596 : /* Call the target stack_protect_guard hook if the stack protection
5597 : guard is declared as a global symbol. */
5598 101479 : if (targetm.stack_protect_guard_symbol_p ())
5599 24 : pushdecl (targetm.stack_protect_guard ());
5600 :
5601 101479 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5602 101479 : tree decl
5603 101479 : = add_builtin_function ("__builtin_is_constant_evaluated",
5604 : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5605 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5606 101479 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5607 :
5608 : /* The concrete return type of __builtin_source_location is
5609 : const std::source_location::__impl*, but we can't form the type
5610 : at this point. So we initially declare it with an auto return
5611 : type which we then "deduce" from require_deduced_type upon first use. */
5612 101479 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5613 101479 : decl = add_builtin_function ("__builtin_source_location",
5614 : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5615 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5616 101479 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5617 :
5618 101479 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5619 : NULL_TREE);
5620 101479 : decl
5621 101479 : = add_builtin_function ("__builtin_is_corresponding_member",
5622 : bool_vaftype,
5623 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5624 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5625 101479 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5626 :
5627 101479 : decl
5628 101479 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5629 : bool_vaftype,
5630 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5631 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5632 101479 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5633 :
5634 101479 : if (cxx_dialect >= cxx26)
5635 : {
5636 25415 : tree void_ptrintftype
5637 25415 : = build_function_type_list (void_type_node, ptr_type_node,
5638 : integer_type_node, NULL_TREE);
5639 25415 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5640 : void_ptrintftype,
5641 : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5642 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5643 25415 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5644 :
5645 : /* Similar case to __builtin_source_location above. The concrete
5646 : return type is std::exception_ptr, but we can't form the type
5647 : at this point, so it is deduced later. */
5648 25415 : decl = add_builtin_function ("__builtin_current_exception",
5649 : auto_ftype, CP_BUILT_IN_CURRENT_EXCEPTION,
5650 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5651 25415 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5652 :
5653 25415 : tree int_ftype = build_function_type_list (integer_type_node, NULL_TREE);
5654 25415 : decl = add_builtin_function ("__builtin_uncaught_exceptions",
5655 : int_ftype, CP_BUILT_IN_UNCAUGHT_EXCEPTIONS,
5656 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5657 25415 : set_call_expr_flags (decl, ECF_PURE | ECF_NOTHROW | ECF_LEAF);
5658 : }
5659 :
5660 101479 : decl
5661 101479 : = add_builtin_function ("__builtin_is_string_literal",
5662 : bool_vaftype,
5663 : CP_BUILT_IN_IS_STRING_LITERAL,
5664 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5665 101479 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5666 :
5667 101479 : tree void_vaintftype = build_varargs_function_type_list (void_type_node,
5668 : integer_type_node,
5669 : NULL_TREE);
5670 101479 : decl = add_builtin_function ("__builtin_constexpr_diag",
5671 : void_vaintftype,
5672 : CP_BUILT_IN_CONSTEXPR_DIAG,
5673 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5674 101479 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5675 :
5676 101479 : decl = add_builtin_function ("__builtin_is_within_lifetime",
5677 : bool_vaftype, CP_BUILT_IN_IS_WITHIN_LIFETIME,
5678 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5679 101479 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5680 101479 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
5681 :
5682 101479 : tree void_vaftype = build_varargs_function_type_list (void_type_node,
5683 : NULL_TREE);
5684 101479 : decl = add_builtin_function ("__builtin_start_lifetime",
5685 : void_vaftype, CP_BUILT_IN_START_LIFETIME,
5686 : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5687 101479 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5688 :
5689 101479 : integer_two_node = build_int_cst (integer_type_node, 2);
5690 :
5691 : /* Guess at the initial static decls size. */
5692 101479 : vec_alloc (static_decls, 500);
5693 :
5694 : /* ... and keyed classes. */
5695 101479 : vec_alloc (keyed_classes, 100);
5696 :
5697 101479 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5698 101479 : truthvalue_type_node = boolean_type_node;
5699 101479 : truthvalue_false_node = boolean_false_node;
5700 101479 : truthvalue_true_node = boolean_true_node;
5701 :
5702 101479 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5703 101479 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5704 101479 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5705 101479 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5706 : NULL_TREE);
5707 :
5708 : #if 0
5709 : record_builtin_type (RID_MAX, NULL, string_type_node);
5710 : #endif
5711 :
5712 101479 : delta_type_node = ptrdiff_type_node;
5713 101479 : vtable_index_type = ptrdiff_type_node;
5714 :
5715 101479 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5716 101479 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5717 :
5718 : /* Create the conversion operator marker. This operator's DECL_NAME
5719 : is in the identifier table, so we can use identifier equality to
5720 : find it. */
5721 101479 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5722 : void_ftype);
5723 :
5724 : /* C++ extensions */
5725 :
5726 101479 : unknown_type_node = make_node (LANG_TYPE);
5727 101479 : record_unknown_type (unknown_type_node, "unknown type");
5728 :
5729 : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5730 101479 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5731 :
5732 : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5733 : result. */
5734 101479 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5735 101479 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5736 :
5737 101479 : init_list_type_node = make_node (LANG_TYPE);
5738 101479 : record_unknown_type (init_list_type_node, "init list");
5739 :
5740 : /* Used when parsing to distinguish parameter-lists () and (void). */
5741 101479 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5742 :
5743 101479 : {
5744 : /* Make sure we get a unique function type, so we can give
5745 : its pointer type a name. (This wins for gdb.) */
5746 101479 : tree vfunc_type = make_node (FUNCTION_TYPE);
5747 101479 : TREE_TYPE (vfunc_type) = integer_type_node;
5748 101479 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5749 101479 : layout_type (vfunc_type);
5750 :
5751 101479 : vtable_entry_type = build_pointer_type (vfunc_type);
5752 : }
5753 101479 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5754 :
5755 101479 : vtbl_type_node
5756 101479 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5757 101479 : layout_type (vtbl_type_node);
5758 101479 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5759 101479 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5760 101479 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5761 101479 : layout_type (vtbl_ptr_type_node);
5762 101479 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5763 :
5764 101479 : push_namespace (get_identifier ("__cxxabiv1"));
5765 101479 : abi_node = current_namespace;
5766 101479 : pop_namespace ();
5767 :
5768 101479 : any_targ_node = make_node (LANG_TYPE);
5769 101479 : record_unknown_type (any_targ_node, "any type");
5770 :
5771 : /* Now, C++. */
5772 101479 : current_lang_name = lang_name_cplusplus;
5773 :
5774 101479 : if (aligned_new_threshold > 1
5775 101479 : && !pow2p_hwi (aligned_new_threshold))
5776 : {
5777 0 : error ("%<-faligned-new=%d%> is not a power of two",
5778 : aligned_new_threshold);
5779 0 : aligned_new_threshold = 1;
5780 : }
5781 101479 : if (aligned_new_threshold == -1)
5782 122848 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5783 101479 : if (aligned_new_threshold == 1)
5784 80093 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5785 :
5786 : /* Ensure attribs.cc is initialized. */
5787 101479 : init_attributes ();
5788 101479 : cxx_init_operator_new_delete_decls ();
5789 :
5790 : /* C++-specific nullptr initialization. */
5791 101479 : if (abi_version_at_least (9))
5792 101308 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5793 101479 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5794 :
5795 101479 : if (! supports_one_only ())
5796 0 : flag_weak = 0;
5797 :
5798 101479 : abort_fndecl
5799 101479 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5800 : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5801 101479 : if (flag_weak)
5802 : /* If no definition is available, resolve references to NULL. */
5803 101449 : declare_weak (abort_fndecl);
5804 :
5805 : /* Perform other language dependent initializations. */
5806 101479 : init_class_processing ();
5807 101479 : init_rtti_processing ();
5808 101479 : init_template_processing ();
5809 :
5810 101479 : if (flag_exceptions)
5811 100222 : init_exception_processing ();
5812 :
5813 101479 : if (flag_contracts)
5814 25461 : init_contracts ();
5815 :
5816 101479 : if (modules_p ())
5817 4973 : init_modules (parse_in);
5818 :
5819 101479 : if (flag_reflection)
5820 1300 : init_reflection ();
5821 :
5822 101479 : make_fname_decl = cp_make_fname_decl;
5823 101479 : start_fname_decls ();
5824 :
5825 : /* Show we use EH for cleanups. */
5826 101479 : if (flag_exceptions)
5827 100222 : using_eh_for_cleanups ();
5828 :
5829 : /* Check that the hardware interference sizes are at least
5830 : alignof(max_align_t), as required by the standard. */
5831 101479 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5832 101479 : if (OPTION_SET_P (param_destruct_interfere_size))
5833 : {
5834 0 : if (param_destruct_interfere_size < max_align)
5835 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5836 : "%d", param_destruct_interfere_size, max_align);
5837 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5838 0 : warning (OPT_Winterference_size,
5839 : "%<--param destructive-interference-size=%d%> "
5840 : "is less than %<--param l1-cache-line-size=%d%>",
5841 : param_destruct_interfere_size, param_l1_cache_line_size);
5842 : }
5843 101479 : else if (param_destruct_interfere_size)
5844 : /* Assume the internal value is OK. */;
5845 0 : else if (param_l1_cache_line_size >= max_align)
5846 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5847 : /* else leave it unset. */
5848 :
5849 101479 : if (OPTION_SET_P (param_construct_interfere_size))
5850 : {
5851 0 : if (param_construct_interfere_size < max_align)
5852 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5853 : "%d", param_construct_interfere_size, max_align);
5854 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5855 0 : && param_l1_cache_line_size >= max_align)
5856 0 : warning (OPT_Winterference_size,
5857 : "%<--param constructive-interference-size=%d%> "
5858 : "is greater than %<--param l1-cache-line-size=%d%>",
5859 : param_construct_interfere_size, param_l1_cache_line_size);
5860 : }
5861 101479 : else if (param_construct_interfere_size)
5862 : /* Assume the internal value is OK. */;
5863 0 : else if (param_l1_cache_line_size >= max_align)
5864 0 : param_construct_interfere_size = param_l1_cache_line_size;
5865 101479 : }
5866 :
5867 : /* Enter an abi node in global-module context. returns a cookie to
5868 : give to pop_abi_namespace. */
5869 :
5870 : unsigned
5871 60096 : push_abi_namespace (tree node)
5872 : {
5873 60096 : push_nested_namespace (node);
5874 60096 : push_visibility ("default", 2);
5875 60096 : unsigned flags = module_kind;
5876 60096 : module_kind = 0;
5877 60096 : return flags;
5878 : }
5879 :
5880 : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5881 : you. */
5882 :
5883 : void
5884 60096 : pop_abi_namespace (unsigned flags, tree node)
5885 : {
5886 60096 : module_kind = flags;
5887 60096 : pop_visibility (2);
5888 60096 : pop_nested_namespace (node);
5889 60096 : }
5890 :
5891 : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5892 : the decl, LOC is the location to give the decl, NAME is the
5893 : initialization string and TYPE_DEP indicates whether NAME depended
5894 : on the type of the function. We make use of that to detect
5895 : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5896 : at the point of first use, so we mustn't push the decl now. */
5897 :
5898 : static tree
5899 237064 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5900 : {
5901 237064 : tree domain = NULL_TREE;
5902 237064 : tree init = NULL_TREE;
5903 :
5904 237064 : if (!(type_dep && current_function_decl && in_template_context))
5905 : {
5906 59800 : const char *name = NULL;
5907 59800 : bool release_name = false;
5908 :
5909 59800 : if (current_function_decl == NULL_TREE)
5910 : name = "top level";
5911 59786 : else if (type_dep == 0)
5912 : {
5913 : /* __FUNCTION__ */
5914 594 : name = fname_as_string (type_dep);
5915 594 : release_name = true;
5916 : }
5917 : else
5918 : {
5919 : /* __PRETTY_FUNCTION__ */
5920 59192 : gcc_checking_assert (type_dep == 1);
5921 59192 : name = cxx_printable_name (current_function_decl, 2);
5922 : }
5923 :
5924 59786 : if (!release_name)
5925 : {
5926 59206 : cpp_string cstr = { 0, 0 }, strname;
5927 59206 : strname.text
5928 59206 : = const_cast <unsigned char *> ((const unsigned char *) name);
5929 59206 : strname.len = strlen (name) + 1;
5930 59206 : if (cpp_translate_string (parse_in, &strname, &cstr, CPP_STRING,
5931 : false))
5932 : {
5933 59206 : name = (const char *) cstr.text;
5934 59206 : release_name = true;
5935 : }
5936 : }
5937 :
5938 59800 : size_t length = strlen (name);
5939 59800 : domain = build_index_type (size_int (length));
5940 59800 : init = build_string (length + 1, name);
5941 59800 : if (release_name)
5942 59800 : free (const_cast<char *> (name));
5943 : }
5944 :
5945 237064 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5946 237064 : type = build_cplus_array_type (type, domain);
5947 :
5948 237064 : if (init)
5949 59800 : TREE_TYPE (init) = type;
5950 : else
5951 177264 : init = error_mark_node;
5952 :
5953 237064 : tree decl = build_decl (loc, VAR_DECL, id, type);
5954 :
5955 237064 : TREE_READONLY (decl) = 1;
5956 237064 : DECL_ARTIFICIAL (decl) = 1;
5957 237064 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5958 237064 : TREE_STATIC (decl) = 1;
5959 :
5960 237064 : TREE_USED (decl) = 1;
5961 :
5962 237064 : SET_DECL_VALUE_EXPR (decl, init);
5963 237064 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5964 : /* For decl_constant_var_p. */
5965 237064 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5966 :
5967 237064 : if (current_function_decl)
5968 : {
5969 237050 : DECL_CONTEXT (decl) = current_function_decl;
5970 237050 : decl = pushdecl_outermost_localscope (decl);
5971 237050 : if (decl != error_mark_node)
5972 237047 : add_decl_expr (decl);
5973 : else
5974 3 : gcc_assert (seen_error ());
5975 : }
5976 : else
5977 : {
5978 14 : DECL_THIS_STATIC (decl) = true;
5979 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5980 : }
5981 :
5982 237064 : return decl;
5983 : }
5984 :
5985 : /* Install DECL as a builtin function at current global scope. Return
5986 : the new decl (if we found an existing version). Also installs it
5987 : into ::std, if it's not '_*'. */
5988 :
5989 : tree
5990 236335398 : cxx_builtin_function (tree decl)
5991 : {
5992 236335398 : retrofit_lang_decl (decl);
5993 :
5994 236335398 : DECL_ARTIFICIAL (decl) = 1;
5995 236335398 : SET_DECL_LANGUAGE (decl, lang_c);
5996 : /* Runtime library routines are, by definition, available in an
5997 : external shared object. */
5998 236335398 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5999 236335398 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
6000 :
6001 236335398 : tree id = DECL_NAME (decl);
6002 236335398 : const char *name = IDENTIFIER_POINTER (id);
6003 236335398 : bool hiding = false;
6004 236335398 : if (name[0] != '_' || name[1] != '_')
6005 : /* In the user's namespace, it must be declared before use. */
6006 : hiding = true;
6007 198368932 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
6008 198368932 : && !startswith (name + 2, "builtin_")
6009 221958316 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
6010 : "_chk", strlen ("_chk") + 1))
6011 : /* Treat __*_chk fortification functions as anticipated as well,
6012 : unless they are __builtin_*_chk. */
6013 : hiding = true;
6014 :
6015 : /* All builtins that don't begin with an '_' should additionally
6016 : go in the 'std' namespace. */
6017 236335398 : if (name[0] != '_')
6018 : {
6019 37827827 : tree std_decl = copy_decl (decl);
6020 :
6021 37827827 : push_nested_namespace (std_node);
6022 37827827 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
6023 37827827 : pushdecl (std_decl, hiding);
6024 37827827 : pop_nested_namespace (std_node);
6025 : }
6026 :
6027 236335398 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6028 236335398 : decl = pushdecl (decl, hiding);
6029 :
6030 236335398 : return decl;
6031 : }
6032 :
6033 : /* Like cxx_builtin_function, but guarantee the function is added to the global
6034 : scope. This is to allow function specific options to add new machine
6035 : dependent builtins when the target ISA changes via attribute((target(...)))
6036 : which saves space on program startup if the program does not use non-generic
6037 : ISAs. */
6038 :
6039 : tree
6040 832312 : cxx_builtin_function_ext_scope (tree decl)
6041 : {
6042 832312 : push_nested_namespace (global_namespace);
6043 832312 : decl = cxx_builtin_function (decl);
6044 832312 : pop_nested_namespace (global_namespace);
6045 :
6046 832312 : return decl;
6047 : }
6048 :
6049 : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
6050 :
6051 : tree
6052 0 : cxx_simulate_builtin_function_decl (tree decl)
6053 : {
6054 0 : retrofit_lang_decl (decl);
6055 :
6056 0 : DECL_ARTIFICIAL (decl) = 1;
6057 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
6058 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6059 0 : return pushdecl (decl);
6060 : }
6061 :
6062 : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6063 : function. Not called directly. */
6064 :
6065 : static tree
6066 1639784 : build_library_fn (tree name, enum tree_code operator_code, tree type,
6067 : int ecf_flags)
6068 : {
6069 1639784 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6070 1639784 : DECL_EXTERNAL (fn) = 1;
6071 1639784 : TREE_PUBLIC (fn) = 1;
6072 1639784 : DECL_ARTIFICIAL (fn) = 1;
6073 1639784 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
6074 1639784 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
6075 1639784 : SET_DECL_LANGUAGE (fn, lang_c);
6076 : /* Runtime library routines are, by definition, available in an
6077 : external shared object. */
6078 1639784 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
6079 1639784 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
6080 1639784 : set_call_expr_flags (fn, ecf_flags);
6081 1639784 : return fn;
6082 : }
6083 :
6084 : /* Returns the _DECL for a library function with C++ linkage. */
6085 :
6086 : static tree
6087 1149802 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
6088 : int ecf_flags)
6089 : {
6090 1149802 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
6091 1149802 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6092 1149802 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
6093 1149802 : return fn;
6094 : }
6095 :
6096 : /* Like build_library_fn, but takes a C string instead of an
6097 : IDENTIFIER_NODE. */
6098 :
6099 : tree
6100 106642 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
6101 : {
6102 106642 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
6103 : }
6104 :
6105 : /* Like build_cp_library_fn, but takes a C string instead of an
6106 : IDENTIFIER_NODE. */
6107 :
6108 : tree
6109 100480 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
6110 : {
6111 100480 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
6112 100480 : ecf_flags);
6113 : }
6114 :
6115 : /* Like build_library_fn, but also pushes the function so that we will
6116 : be able to find it via get_global_binding. Also, the function
6117 : may throw exceptions listed in RAISES. */
6118 :
6119 : tree
6120 383340 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6121 : {
6122 383340 : if (raises)
6123 43444 : type = build_exception_variant (type, raises);
6124 :
6125 383340 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
6126 383340 : return pushdecl_top_level (fn);
6127 : }
6128 :
6129 : /* Like build_cp_library_fn, but also pushes the function so that it
6130 : will be found by normal lookup. */
6131 :
6132 : static tree
6133 1049322 : push_cp_library_fn (enum tree_code operator_code, tree type,
6134 : int ecf_flags)
6135 : {
6136 1049322 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
6137 : operator_code, type, ecf_flags);
6138 1049322 : pushdecl (fn);
6139 1049322 : if (flag_tm)
6140 3100 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
6141 1049322 : return fn;
6142 : }
6143 :
6144 : /* Like push_library_fn, but also note that this function throws
6145 : and does not return. Used for __throw_foo and the like. */
6146 :
6147 : tree
6148 111419 : push_throw_library_fn (tree name, tree type)
6149 : {
6150 111419 : tree fn = push_library_fn (name, type, NULL_TREE,
6151 : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6152 111419 : return fn;
6153 : }
6154 :
6155 : /* When we call finish_struct for an anonymous union, we create
6156 : default copy constructors and such. But, an anonymous union
6157 : shouldn't have such things; this function undoes the damage to the
6158 : anonymous union type T.
6159 :
6160 : (The reason that we create the synthesized methods is that we don't
6161 : distinguish `union { int i; }' from `typedef union { int i; } U'.
6162 : The first is an anonymous union; the second is just an ordinary
6163 : union type.) */
6164 :
6165 : void
6166 181255 : fixup_anonymous_aggr (tree t)
6167 : {
6168 : /* Wipe out memory of synthesized methods. */
6169 181255 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6170 181255 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6171 181255 : TYPE_HAS_COPY_CTOR (t) = 0;
6172 181255 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6173 181255 : TYPE_HAS_COPY_ASSIGN (t) = 0;
6174 181255 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6175 :
6176 : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6177 : invalid members. */
6178 1058155 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6179 : {
6180 876900 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6181 0 : *prev_p = DECL_CHAIN (probe);
6182 : else
6183 876900 : prev_p = &DECL_CHAIN (probe);
6184 :
6185 876900 : if (DECL_ARTIFICIAL (probe)
6186 876900 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6187 78518 : || TYPE_ANON_P (TREE_TYPE (probe))))
6188 297758 : continue;
6189 :
6190 579142 : if (TREE_CODE (probe) != FIELD_DECL
6191 579088 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6192 : {
6193 : /* We already complained about static data members in
6194 : finish_static_data_member_decl. */
6195 66 : if (!VAR_P (probe))
6196 : {
6197 51 : auto_diagnostic_group d;
6198 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
6199 51 : TREE_CODE (t) == UNION_TYPE
6200 : ? "%q#D invalid; an anonymous union may "
6201 : "only have public non-static data members"
6202 : : "%q#D invalid; an anonymous struct may "
6203 : "only have public non-static data members", probe))
6204 : {
6205 51 : static bool hint;
6206 51 : if (flag_permissive && !hint)
6207 : {
6208 6 : hint = true;
6209 6 : inform (DECL_SOURCE_LOCATION (probe),
6210 : "this flexibility is deprecated and will be "
6211 : "removed");
6212 : }
6213 : }
6214 51 : }
6215 : }
6216 : }
6217 :
6218 : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6219 181255 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6220 181255 : unsigned store = 0;
6221 535491 : for (tree elt : vec)
6222 354236 : if (!is_overloaded_fn (elt))
6223 354215 : (*vec)[store++] = elt;
6224 181255 : vec_safe_truncate (vec, store);
6225 :
6226 : /* Wipe RTTI info. */
6227 181255 : SET_CLASSTYPE_TYPEINFO_VAR (t, NULL_TREE);
6228 :
6229 : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6230 : assignment operators (because they cannot have these methods themselves).
6231 : For anonymous unions this is already checked because they are not allowed
6232 : in any union, otherwise we have to check it. */
6233 181255 : if (TREE_CODE (t) != UNION_TYPE)
6234 : {
6235 38280 : tree field, type;
6236 :
6237 38280 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6238 : {
6239 9 : error_at (location_of (t), "anonymous struct with base classes");
6240 : /* Avoid ICE after error on anon-struct9.C. */
6241 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
6242 : }
6243 :
6244 153963 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6245 115683 : if (TREE_CODE (field) == FIELD_DECL)
6246 : {
6247 77302 : type = TREE_TYPE (field);
6248 77302 : if (CLASS_TYPE_P (type))
6249 : {
6250 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
6251 6 : error ("member %q+#D with constructor not allowed "
6252 : "in anonymous aggregate", field);
6253 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6254 0 : error ("member %q+#D with destructor not allowed "
6255 : "in anonymous aggregate", field);
6256 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6257 0 : error ("member %q+#D with copy assignment operator "
6258 : "not allowed in anonymous aggregate", field);
6259 : }
6260 : }
6261 : }
6262 181255 : }
6263 :
6264 : /* Warn for an attribute located at LOCATION that appertains to the
6265 : class type CLASS_TYPE that has not been properly placed after its
6266 : class-key, in it class-specifier. */
6267 :
6268 : void
6269 117 : warn_misplaced_attr_for_class_type (location_t location,
6270 : tree class_type)
6271 : {
6272 117 : gcc_assert (OVERLOAD_TYPE_P (class_type));
6273 :
6274 117 : auto_diagnostic_group d;
6275 117 : if (warning_at (location, OPT_Wattributes,
6276 : "attribute ignored in declaration "
6277 : "of %q#T", class_type))
6278 117 : inform (location,
6279 : "attribute for %q#T must follow the %qs keyword",
6280 : class_type, class_key_or_enum_as_string (class_type));
6281 117 : }
6282 :
6283 : /* Returns the cv-qualifiers that apply to the type specified
6284 : by the DECLSPECS. */
6285 :
6286 : static int
6287 1087571128 : get_type_quals (const cp_decl_specifier_seq *declspecs)
6288 : {
6289 1087571128 : int type_quals = TYPE_UNQUALIFIED;
6290 :
6291 1087571128 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6292 112553193 : type_quals |= TYPE_QUAL_CONST;
6293 1087571128 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6294 1816241 : type_quals |= TYPE_QUAL_VOLATILE;
6295 1087571128 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6296 34 : type_quals |= TYPE_QUAL_RESTRICT;
6297 :
6298 1087571128 : return type_quals;
6299 : }
6300 :
6301 : /* Make sure that a declaration with no declarator is well-formed, i.e.
6302 : just declares a tagged type or anonymous union.
6303 :
6304 : Returns the type declared; or NULL_TREE if none. */
6305 :
6306 : tree
6307 39225769 : check_tag_decl (cp_decl_specifier_seq *declspecs,
6308 : bool explicit_type_instantiation_p)
6309 : {
6310 39225769 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6311 39225769 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6312 : /* If a class, struct, or enum type is declared by the DECLSPECS
6313 : (i.e, if a class-specifier, enum-specifier, or non-typename
6314 : elaborated-type-specifier appears in the DECLSPECS),
6315 : DECLARED_TYPE is set to the corresponding type. */
6316 39225769 : tree declared_type = NULL_TREE;
6317 39225769 : bool error_p = false;
6318 :
6319 39225769 : if (declspecs->multiple_types_p)
6320 26 : error_at (smallest_type_location (declspecs),
6321 : "multiple types in one declaration");
6322 39225743 : else if (declspecs->redefined_builtin_type)
6323 : {
6324 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6325 21 : if (!in_system_header_at (loc))
6326 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
6327 : declspecs->redefined_builtin_type);
6328 : return NULL_TREE;
6329 : }
6330 :
6331 39225748 : if (declspecs->type
6332 39225733 : && TYPE_P (declspecs->type)
6333 78067790 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6334 38840997 : && MAYBE_CLASS_TYPE_P (declspecs->type))
6335 1966951 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6336 : declared_type = declspecs->type;
6337 384796 : else if (declspecs->type == error_mark_node)
6338 1502 : error_p = true;
6339 :
6340 39225748 : if (type_uses_auto (declared_type))
6341 : {
6342 8 : error_at (declspecs->locations[ds_type_spec],
6343 : "%<auto%> can only be specified for variables "
6344 : "or function declarations");
6345 8 : return error_mark_node;
6346 : }
6347 :
6348 39225740 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6349 : declared_type = NULL_TREE;
6350 :
6351 39225740 : if (!declared_type && !saw_friend && !error_p)
6352 91 : permerror (input_location, "declaration does not declare anything");
6353 : /* Check for an anonymous union. */
6354 38840917 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6355 113157022 : && TYPE_UNNAMED_P (declared_type))
6356 : {
6357 : /* 7/3 In a simple-declaration, the optional init-declarator-list
6358 : can be omitted only when declaring a class (clause 9) or
6359 : enumeration (7.2), that is, when the decl-specifier-seq contains
6360 : either a class-specifier, an elaborated-type-specifier with
6361 : a class-key (9.1), or an enum-specifier. In these cases and
6362 : whenever a class-specifier or enum-specifier is present in the
6363 : decl-specifier-seq, the identifiers in these specifiers are among
6364 : the names being declared by the declaration (as class-name,
6365 : enum-names, or enumerators, depending on the syntax). In such
6366 : cases, and except for the declaration of an unnamed bit-field (9.6),
6367 : the decl-specifier-seq shall introduce one or more names into the
6368 : program, or shall redeclare a name introduced by a previous
6369 : declaration. [Example:
6370 : enum { }; // ill-formed
6371 : typedef class { }; // ill-formed
6372 : --end example] */
6373 181261 : if (saw_typedef)
6374 : {
6375 6 : error_at (declspecs->locations[ds_typedef],
6376 : "missing type-name in typedef-declaration");
6377 6 : return NULL_TREE;
6378 : }
6379 181255 : /* Anonymous unions are objects, so they can have specifiers. */;
6380 181255 : SET_ANON_AGGR_TYPE_P (declared_type);
6381 :
6382 181255 : if (TREE_CODE (declared_type) != UNION_TYPE)
6383 38280 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6384 38280 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6385 : }
6386 :
6387 : else
6388 : {
6389 39044388 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6390 9 : error_at (declspecs->locations[ds_inline],
6391 : "%<inline%> can only be specified for functions");
6392 39044379 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6393 3 : error_at (declspecs->locations[ds_virtual],
6394 : "%<virtual%> can only be specified for functions");
6395 39044376 : else if (saw_friend
6396 39044376 : && (!current_class_type
6397 2174629 : || current_scope () != current_class_type))
6398 0 : error_at (declspecs->locations[ds_friend],
6399 : "%<friend%> can only be specified inside a class");
6400 39044376 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6401 3 : error_at (declspecs->locations[ds_explicit],
6402 : "%<explicit%> can only be specified for constructors");
6403 39044373 : else if (declspecs->storage_class)
6404 3 : error_at (declspecs->locations[ds_storage_class],
6405 : "a storage class can only be specified for objects "
6406 : "and functions");
6407 39044370 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6408 9 : error_at (declspecs->locations[ds_const],
6409 : "%<const%> can only be specified for objects and "
6410 : "functions");
6411 39044361 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6412 3 : error_at (declspecs->locations[ds_volatile],
6413 : "%<volatile%> can only be specified for objects and "
6414 : "functions");
6415 39044358 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6416 3 : error_at (declspecs->locations[ds_restrict],
6417 : "%<__restrict%> can only be specified for objects and "
6418 : "functions");
6419 39044355 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6420 3 : error_at (declspecs->locations[ds_thread],
6421 : "%<__thread%> can only be specified for objects "
6422 : "and functions");
6423 39044352 : else if (saw_typedef)
6424 24 : warning_at (declspecs->locations[ds_typedef], 0,
6425 : "%<typedef%> was ignored in this declaration");
6426 39044328 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6427 9 : error_at (declspecs->locations[ds_constexpr],
6428 : "%qs cannot be used for type declarations", "constexpr");
6429 39044319 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6430 3 : error_at (declspecs->locations[ds_constinit],
6431 : "%qs cannot be used for type declarations", "constinit");
6432 39044316 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6433 3 : error_at (declspecs->locations[ds_consteval],
6434 : "%qs cannot be used for type declarations", "consteval");
6435 : }
6436 :
6437 39225734 : if (declspecs->attributes && warn_attributes && declared_type)
6438 : {
6439 30 : location_t loc;
6440 24 : if (!CLASS_TYPE_P (declared_type)
6441 54 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6442 : /* For a non-template class, use the name location. */
6443 21 : loc = location_of (declared_type);
6444 : else
6445 : /* For a template class (an explicit instantiation), use the
6446 : current location. */
6447 9 : loc = input_location;
6448 :
6449 30 : if (explicit_type_instantiation_p)
6450 : /* [dcl.attr.grammar]/4:
6451 :
6452 : No attribute-specifier-seq shall appertain to an explicit
6453 : instantiation. */
6454 : {
6455 6 : auto_diagnostic_group d;
6456 6 : if (warning_at (loc, OPT_Wattributes,
6457 : "attribute ignored in explicit instantiation %q#T",
6458 : declared_type))
6459 6 : inform (loc,
6460 : "no attribute can be applied to "
6461 : "an explicit instantiation");
6462 6 : }
6463 : else
6464 24 : warn_misplaced_attr_for_class_type (loc, declared_type);
6465 : }
6466 :
6467 39225734 : if (declspecs->std_attributes
6468 93 : && declared_type
6469 39225824 : && any_nonignored_attribute_p (declspecs->std_attributes))
6470 : {
6471 90 : auto_diagnostic_group d;
6472 90 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6473 : "attribute ignored"))
6474 90 : inform (declspecs->locations[ds_std_attribute],
6475 : "an attribute that appertains to a type-specifier is ignored");
6476 90 : }
6477 :
6478 : return declared_type;
6479 : }
6480 :
6481 : /* Called when a declaration is seen that contains no names to declare.
6482 : If its type is a reference to a structure, union or enum inherited
6483 : from a containing scope, shadow that tag name for the current scope
6484 : with a forward reference.
6485 : If its type defines a new named structure or union
6486 : or defines an enum, it is valid but we need not do anything here.
6487 : Otherwise, it is an error.
6488 :
6489 : C++: may have to grok the declspecs to learn about static,
6490 : complain for anonymous unions.
6491 :
6492 : Returns the TYPE declared -- or NULL_TREE if none. */
6493 :
6494 : tree
6495 34371731 : shadow_tag (cp_decl_specifier_seq *declspecs)
6496 : {
6497 34371731 : tree t = check_tag_decl (declspecs,
6498 : /*explicit_type_instantiation_p=*/false);
6499 :
6500 34371731 : if (!t)
6501 : return NULL_TREE;
6502 :
6503 34370216 : t = maybe_process_partial_specialization (t);
6504 34370216 : if (t == error_mark_node)
6505 : return NULL_TREE;
6506 :
6507 : /* This is where the variables in an anonymous union are
6508 : declared. An anonymous union declaration looks like:
6509 : union { ... } ;
6510 : because there is no declarator after the union, the parser
6511 : sends that declaration here. */
6512 34368305 : if (ANON_AGGR_TYPE_P (t))
6513 : {
6514 392 : fixup_anonymous_aggr (t);
6515 :
6516 392 : if (TYPE_FIELDS (t))
6517 : {
6518 392 : tree decl = grokdeclarator (/*declarator=*/NULL,
6519 : declspecs, NORMAL, 0, NULL);
6520 392 : finish_anon_union (decl);
6521 : }
6522 : }
6523 :
6524 : return t;
6525 : }
6526 :
6527 : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6528 :
6529 : tree
6530 425356186 : groktypename (cp_decl_specifier_seq *type_specifiers,
6531 : const cp_declarator *declarator,
6532 : bool is_template_arg)
6533 : {
6534 425356186 : tree attrs;
6535 425356186 : tree type;
6536 70414746 : enum decl_context context
6537 425356186 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6538 425356186 : attrs = type_specifiers->attributes;
6539 425356186 : type_specifiers->attributes = NULL_TREE;
6540 425356186 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6541 425356186 : if (attrs && type != error_mark_node)
6542 : {
6543 673 : if (CLASS_TYPE_P (type))
6544 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6545 : "outside of definition", type);
6546 667 : else if (MAYBE_CLASS_TYPE_P (type))
6547 : /* A template type parameter or other dependent type. */
6548 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6549 : "type %qT without an associated declaration", type);
6550 : else
6551 658 : cplus_decl_attributes (&type, attrs, 0);
6552 : }
6553 425356186 : return type;
6554 : }
6555 :
6556 : /* Process a DECLARATOR for a function-scope or namespace-scope
6557 : variable or function declaration.
6558 : (Function definitions go through start_function; class member
6559 : declarations appearing in the body of the class go through
6560 : grokfield.) The DECL corresponding to the DECLARATOR is returned.
6561 : If an error occurs, the error_mark_node is returned instead.
6562 :
6563 : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6564 : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6565 : for an explicitly defaulted function, or SD_DELETED for an explicitly
6566 : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6567 : implicitly initialized via a default constructor. It can also be
6568 : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6569 : mark the new decl as DECL_DECOMPOSITION_P.
6570 :
6571 : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6572 : declaration.
6573 :
6574 : The scope represented by the context of the returned DECL is pushed
6575 : (if it is not the global namespace) and is assigned to
6576 : *PUSHED_SCOPE_P. The caller is then responsible for calling
6577 : pop_scope on *PUSHED_SCOPE_P if it is set. */
6578 :
6579 : tree
6580 133444058 : start_decl (const cp_declarator *declarator,
6581 : cp_decl_specifier_seq *declspecs,
6582 : int initialized,
6583 : tree attributes,
6584 : tree prefix_attributes,
6585 : tree *pushed_scope_p)
6586 : {
6587 133444058 : tree decl;
6588 133444058 : tree context;
6589 133444058 : bool was_public;
6590 133444058 : int flags;
6591 133444058 : bool alias;
6592 133444058 : tree initial;
6593 :
6594 133444058 : *pushed_scope_p = NULL_TREE;
6595 :
6596 133444058 : if (prefix_attributes != error_mark_node)
6597 133444056 : attributes = attr_chainon (attributes, prefix_attributes);
6598 :
6599 133444058 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6600 : &attributes);
6601 :
6602 133444058 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6603 133444058 : || decl == error_mark_node
6604 133441903 : || prefix_attributes == error_mark_node)
6605 2157 : return error_mark_node;
6606 :
6607 133441901 : context = CP_DECL_CONTEXT (decl);
6608 133441901 : if (context != global_namespace)
6609 23476671 : *pushed_scope_p = push_scope (context);
6610 :
6611 133441901 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6612 : {
6613 27 : error_at (DECL_SOURCE_LOCATION (decl),
6614 : "typedef %qD is initialized (use %qs instead)",
6615 : decl, "decltype");
6616 27 : return error_mark_node;
6617 : }
6618 :
6619 133441874 : if (flag_contracts
6620 14559769 : && TREE_CODE (decl) == FUNCTION_DECL
6621 4087980 : && !processing_template_decl
6622 3308006 : && DECL_RESULT (decl)
6623 136743442 : && is_auto (TREE_TYPE (DECL_RESULT (decl))))
6624 36 : if (tree specs = get_fn_contract_specifiers (decl))
6625 12 : for (tree ca : tree_vec_range (specs))
6626 9 : if (POSTCONDITION_P (ca) && POSTCONDITION_IDENTIFIER (ca))
6627 : {
6628 3 : error_at (DECL_SOURCE_LOCATION (decl),
6629 : "postconditions with deduced result name types must only"
6630 : " appear on function definitions");
6631 3 : return error_mark_node;
6632 : }
6633 : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6634 : with attribute validation. */
6635 133441871 : initial = DECL_INITIAL (decl);
6636 :
6637 133441871 : if (initialized)
6638 : {
6639 64719982 : if (! toplevel_bindings_p ()
6640 64719982 : && DECL_EXTERNAL (decl))
6641 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6642 : decl);
6643 64719982 : DECL_EXTERNAL (decl) = 0;
6644 64719982 : if (toplevel_bindings_p ())
6645 7806076 : TREE_STATIC (decl) = 1;
6646 : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6647 : even though we might not yet have the initializer expression. */
6648 64719982 : if (!DECL_INITIAL (decl))
6649 64121856 : DECL_INITIAL (decl) = error_mark_node;
6650 : }
6651 133441871 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6652 :
6653 133441871 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6654 3985 : record_key_method_defined (decl);
6655 :
6656 : /* If this is a typedef that names the class for linkage purposes
6657 : (7.1.3p8), apply any attributes directly to the type. */
6658 133441871 : if (TREE_CODE (decl) == TYPE_DECL
6659 133441871 : && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl))
6660 : flags = ATTR_FLAG_TYPE_IN_PLACE;
6661 : else
6662 : flags = 0;
6663 :
6664 : /* Set attributes here so if duplicate decl, will have proper attributes. */
6665 133441871 : cplus_decl_attributes (&decl, attributes, flags);
6666 :
6667 : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6668 : assist with attribute validation. */
6669 133441871 : DECL_INITIAL (decl) = initial;
6670 :
6671 : /* Dllimported symbols cannot be defined. Static data members (which
6672 : can be initialized in-class and dllimported) go through grokfield,
6673 : not here, so we don't need to exclude those decls when checking for
6674 : a definition. */
6675 133441871 : if (initialized && DECL_DLLIMPORT_P (decl))
6676 : {
6677 0 : error_at (DECL_SOURCE_LOCATION (decl),
6678 : "definition of %q#D is marked %<dllimport%>", decl);
6679 0 : DECL_DLLIMPORT_P (decl) = 0;
6680 : }
6681 :
6682 : /* If #pragma weak was used, mark the decl weak now. */
6683 133441871 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6684 48710630 : maybe_apply_pragma_weak (decl);
6685 :
6686 133441871 : if (TREE_CODE (decl) == FUNCTION_DECL
6687 39456469 : && DECL_DECLARED_INLINE_P (decl)
6688 3563048 : && DECL_UNINLINABLE (decl)
6689 133441871 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6690 : {
6691 0 : auto_urlify_attributes sentinel;
6692 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6693 : "inline function %qD given attribute %qs", decl, "noinline");
6694 0 : }
6695 :
6696 133441871 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6697 : {
6698 891029 : bool this_tmpl = (current_template_depth
6699 891029 : > template_class_depth (context));
6700 891029 : if (VAR_P (decl))
6701 : {
6702 521619 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6703 521619 : if (field == NULL_TREE
6704 521619 : || !(VAR_P (field) || variable_template_p (field)))
6705 6 : error ("%q+#D is not a static data member of %q#T", decl, context);
6706 521613 : else if (variable_template_p (field)
6707 521613 : && (DECL_LANG_SPECIFIC (decl)
6708 149 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6709 : /* OK, specialization was already checked. */;
6710 521542 : else if (variable_template_p (field) && !this_tmpl)
6711 : {
6712 3 : auto_diagnostic_group d;
6713 3 : error_at (DECL_SOURCE_LOCATION (decl),
6714 : "non-member-template declaration of %qD", decl);
6715 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6716 : "member template declaration here");
6717 3 : return error_mark_node;
6718 3 : }
6719 : else
6720 : {
6721 521539 : if (variable_template_p (field))
6722 75 : field = DECL_TEMPLATE_RESULT (field);
6723 :
6724 521539 : if (DECL_CONTEXT (field) != context)
6725 : {
6726 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6727 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6728 : "to be defined as %<%T::%D%>",
6729 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6730 3 : context, DECL_NAME (decl));
6731 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6732 : }
6733 :
6734 521539 : if (modules_p () && !module_may_redeclare (field))
6735 30 : return error_mark_node;
6736 :
6737 : /* Static data member are tricky; an in-class initialization
6738 : still doesn't provide a definition, so the in-class
6739 : declaration will have DECL_EXTERNAL set, but will have an
6740 : initialization. Thus, duplicate_decls won't warn
6741 : about this situation, and so we check here. */
6742 521509 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6743 9 : error ("duplicate initialization of %qD", decl);
6744 521509 : field = duplicate_decls (decl, field);
6745 521509 : if (field == error_mark_node)
6746 : return error_mark_node;
6747 521453 : else if (field)
6748 521453 : decl = field;
6749 : }
6750 : }
6751 : else
6752 : {
6753 369422 : tree field = check_classfn (context, decl,
6754 : this_tmpl
6755 12 : ? current_template_parms
6756 : : NULL_TREE);
6757 369335 : if (field && field != error_mark_node
6758 738745 : && duplicate_decls (decl, field))
6759 369335 : decl = field;
6760 : }
6761 :
6762 : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6763 890940 : DECL_IN_AGGR_P (decl) = 0;
6764 : /* Do not mark DECL as an explicit specialization if it was not
6765 : already marked as an instantiation; a declaration should
6766 : never be marked as a specialization unless we know what
6767 : template is being specialized. */
6768 890940 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6769 : {
6770 367232 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6771 367232 : if (TREE_CODE (decl) == FUNCTION_DECL)
6772 346103 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6773 346103 : && DECL_NONGNU_INLINE_P (decl));
6774 : else
6775 21129 : DECL_COMDAT (decl) = false;
6776 :
6777 : /* [temp.expl.spec] An explicit specialization of a static data
6778 : member of a template is a definition if the declaration
6779 : includes an initializer; otherwise, it is a declaration.
6780 :
6781 : We check for processing_specialization so this only applies
6782 : to the new specialization syntax. */
6783 367232 : if (!initialized && processing_specialization)
6784 367028 : DECL_EXTERNAL (decl) = 1;
6785 : }
6786 :
6787 1258016 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6788 : /* Aliases are definitions. */
6789 890976 : && !alias)
6790 : {
6791 9 : permerror (declarator->id_loc,
6792 : "declaration of %q#D outside of class is not definition",
6793 : decl);
6794 : }
6795 : }
6796 :
6797 : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6798 133441782 : if (initialized == SD_DECOMPOSITION)
6799 494837 : fit_decomposition_lang_decl (decl, NULL_TREE);
6800 :
6801 133441782 : was_public = TREE_PUBLIC (decl);
6802 :
6803 227438148 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6804 134039935 : && current_function_decl)
6805 : {
6806 : /* A function-scope decl of some namespace-scope decl. */
6807 63554 : DECL_LOCAL_DECL_P (decl) = true;
6808 63554 : if (named_module_attach_p ())
6809 3 : error_at (declarator->id_loc,
6810 : "block-scope extern declaration %q#D must not be"
6811 : " attached to a named module", decl);
6812 : }
6813 :
6814 : /* Enter this declaration into the symbol table. Don't push the plain
6815 : VAR_DECL for a variable template. */
6816 133441782 : if (!template_parm_scope_p ()
6817 133441782 : || !VAR_P (decl))
6818 129027561 : decl = maybe_push_decl (decl);
6819 :
6820 133441782 : if (processing_template_decl)
6821 84723128 : decl = push_template_decl (decl);
6822 :
6823 133441782 : if (decl == error_mark_node)
6824 : return error_mark_node;
6825 :
6826 133441342 : if (VAR_P (decl)
6827 71099101 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6828 70386 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6829 : /* But not templated variables. */
6830 133472582 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6831 : {
6832 : /* This is a const variable with implicit 'static'. Set
6833 : DECL_THIS_STATIC so we can tell it from variables that are
6834 : !TREE_PUBLIC because of the anonymous namespace. */
6835 31240 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6836 31240 : DECL_THIS_STATIC (decl) = 1;
6837 : }
6838 :
6839 75306592 : if (current_function_decl && VAR_P (decl)
6840 62466812 : && maybe_constexpr_fn (current_function_decl)
6841 149847888 : && cxx_dialect < cxx23)
6842 : {
6843 12947433 : bool ok = false;
6844 12947433 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6845 : {
6846 9 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6847 18 : error_at (DECL_SOURCE_LOCATION (decl),
6848 : "%qD defined %<thread_local%> in %qs function only "
6849 : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6850 9 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6851 : ? "consteval" : "constexpr");
6852 : }
6853 12947424 : else if (TREE_STATIC (decl))
6854 : {
6855 50 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6856 72 : error_at (DECL_SOURCE_LOCATION (decl),
6857 : "%qD defined %<static%> in %qs function only available "
6858 : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6859 36 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6860 : ? "consteval" : "constexpr");
6861 : }
6862 : else
6863 : ok = true;
6864 45 : if (!ok)
6865 59 : cp_function_chain->invalid_constexpr = true;
6866 : }
6867 :
6868 133441342 : if (!processing_template_decl && VAR_P (decl))
6869 8923793 : start_decl_1 (decl, initialized);
6870 :
6871 133441339 : return decl;
6872 : }
6873 :
6874 : /* Process the declaration of a variable DECL. INITIALIZED is true
6875 : iff DECL is explicitly initialized. (INITIALIZED is false if the
6876 : variable is initialized via an implicitly-called constructor.)
6877 : This function must be called for ordinary variables (including, for
6878 : example, implicit instantiations of templates), but must not be
6879 : called for template declarations. */
6880 :
6881 : void
6882 8933372 : start_decl_1 (tree decl, bool initialized)
6883 : {
6884 8933372 : gcc_checking_assert (!processing_template_decl);
6885 :
6886 8933372 : if (error_operand_p (decl))
6887 : return;
6888 :
6889 8933357 : gcc_checking_assert (VAR_P (decl));
6890 :
6891 8933357 : tree type = TREE_TYPE (decl);
6892 8933357 : bool complete_p = COMPLETE_TYPE_P (type);
6893 8933357 : bool aggregate_definition_p
6894 8933357 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6895 :
6896 : /* If an explicit initializer is present, or if this is a definition
6897 : of an aggregate, then we need a complete type at this point.
6898 : (Scalars are always complete types, so there is nothing to
6899 : check.) This code just sets COMPLETE_P; errors (if necessary)
6900 : are issued below. */
6901 4635176 : if ((initialized || aggregate_definition_p)
6902 7880140 : && !complete_p
6903 6309337 : && COMPLETE_TYPE_P (complete_type (type)))
6904 : {
6905 342567 : complete_p = true;
6906 : /* We will not yet have set TREE_READONLY on DECL if the type
6907 : was "const", but incomplete, before this point. But, now, we
6908 : have a complete type, so we can try again. */
6909 342567 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6910 : }
6911 :
6912 8933354 : if (initialized)
6913 : /* Is it valid for this decl to have an initializer at all? */
6914 : {
6915 : /* Don't allow initializations for incomplete types except for
6916 : arrays which might be completed by the initialization. */
6917 7102612 : if (complete_p)
6918 : ; /* A complete type is ok. */
6919 1331353 : else if (type_uses_auto (type))
6920 : ; /* An auto type is ok. */
6921 123579 : else if (TREE_CODE (type) != ARRAY_TYPE)
6922 : {
6923 27 : auto_diagnostic_group d;
6924 27 : error ("variable %q#D has initializer but incomplete type", decl);
6925 48 : maybe_suggest_missing_header (input_location,
6926 27 : TYPE_IDENTIFIER (type),
6927 27 : CP_TYPE_CONTEXT (type));
6928 27 : type = TREE_TYPE (decl) = error_mark_node;
6929 27 : }
6930 123552 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6931 : {
6932 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6933 0 : error ("elements of array %q#D have incomplete type", decl);
6934 : /* else we already gave an error in start_decl. */
6935 : }
6936 : }
6937 1830742 : else if (aggregate_definition_p && !complete_p)
6938 : {
6939 238 : if (type_uses_auto (type))
6940 71 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6941 : else
6942 : {
6943 167 : auto_diagnostic_group d;
6944 167 : error ("aggregate %q#D has incomplete type and cannot be defined",
6945 : decl);
6946 289 : maybe_suggest_missing_header (input_location,
6947 167 : TYPE_IDENTIFIER (type),
6948 167 : CP_TYPE_CONTEXT (type));
6949 : /* Change the type so that assemble_variable will give
6950 : DECL an rtl we can live with: (mem (const_int 0)). */
6951 167 : type = TREE_TYPE (decl) = error_mark_node;
6952 167 : }
6953 : }
6954 :
6955 : /* Create a new scope to hold this declaration if necessary.
6956 : Whether or not a new scope is necessary cannot be determined
6957 : until after the type has been completed; if the type is a
6958 : specialization of a class template it is not until after
6959 : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6960 : will be set correctly. */
6961 8933354 : maybe_push_cleanup_level (type);
6962 : }
6963 :
6964 : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6965 : C++20 P0960. TYPE is the type of the object we're initializing. */
6966 :
6967 : tree
6968 309 : do_aggregate_paren_init (tree init, tree type)
6969 : {
6970 309 : tree val = TREE_VALUE (init);
6971 :
6972 309 : if (TREE_CHAIN (init) == NULL_TREE)
6973 : {
6974 : /* If the list has a single element and it's a string literal,
6975 : then it's the initializer for the array as a whole. */
6976 193 : if (TREE_CODE (type) == ARRAY_TYPE
6977 193 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6978 239 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6979 : == STRING_CST)
6980 : return val;
6981 : /* Handle non-standard extensions like compound literals. This also
6982 : prevents triggering aggregate parenthesized-initialization in
6983 : compiler-generated code for =default. */
6984 154 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6985 154 : TREE_TYPE (val)))
6986 : return val;
6987 : }
6988 :
6989 208 : init = build_constructor_from_list (init_list_type_node, init);
6990 208 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6991 208 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6992 208 : return init;
6993 : }
6994 :
6995 : /* Handle initialization of references. DECL, TYPE, and INIT have the
6996 : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6997 : but will be set to a new CLEANUP_STMT if a temporary is created
6998 : that must be destroyed subsequently.
6999 :
7000 : Returns an initializer expression to use to initialize DECL, or
7001 : NULL if the initialization can be performed statically.
7002 :
7003 : Quotes on semantics can be found in ARM 8.4.3. */
7004 :
7005 : static tree
7006 386027 : grok_reference_init (tree decl, tree type, tree init, int flags)
7007 : {
7008 386027 : if (init == NULL_TREE)
7009 : {
7010 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
7011 3 : || DECL_IN_AGGR_P (decl) == 0)
7012 18 : && ! DECL_THIS_EXTERN (decl))
7013 15 : error_at (DECL_SOURCE_LOCATION (decl),
7014 : "%qD declared as reference but not initialized", decl);
7015 : return NULL_TREE;
7016 : }
7017 :
7018 386012 : tree ttype = TREE_TYPE (type);
7019 386012 : if (TREE_CODE (init) == TREE_LIST)
7020 : {
7021 : /* This handles (C++20 only) code like
7022 :
7023 : const A& r(1, 2, 3);
7024 :
7025 : where we treat the parenthesized list as a CONSTRUCTOR. */
7026 1262 : if (TREE_TYPE (init) == NULL_TREE
7027 1262 : && CP_AGGREGATE_TYPE_P (ttype)
7028 102 : && !DECL_DECOMPOSITION_P (decl)
7029 1361 : && (cxx_dialect >= cxx20))
7030 : {
7031 : /* We don't know yet if we should treat const A& r(1) as
7032 : const A& r{1}. */
7033 93 : if (list_length (init) == 1)
7034 : {
7035 72 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
7036 72 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7037 : tf_warning_or_error);
7038 : }
7039 : /* If the list had more than one element, the code is ill-formed
7040 : pre-C++20, so we can build a constructor right away. */
7041 : else
7042 21 : init = do_aggregate_paren_init (init, ttype);
7043 : }
7044 : else
7045 1169 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7046 : tf_warning_or_error);
7047 : }
7048 :
7049 386012 : if (TREE_CODE (ttype) != ARRAY_TYPE
7050 386012 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7051 : /* Note: default conversion is only called in very special cases. */
7052 12 : init = decay_conversion (init, tf_warning_or_error);
7053 :
7054 : /* check_initializer handles this for non-reference variables, but for
7055 : references we need to do it here or the initializer will get the
7056 : incomplete array type and confuse later calls to
7057 : cp_complete_array_type. */
7058 386012 : if (TREE_CODE (ttype) == ARRAY_TYPE
7059 22663 : && TYPE_DOMAIN (ttype) == NULL_TREE
7060 386095 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
7061 50 : || TREE_CODE (init) == STRING_CST))
7062 : {
7063 33 : cp_complete_array_type (&ttype, init, false);
7064 33 : if (ttype != TREE_TYPE (type))
7065 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
7066 : }
7067 :
7068 : /* Convert INIT to the reference type TYPE. This may involve the
7069 : creation of a temporary, whose lifetime must be the same as that
7070 : of the reference. If so, a DECL_EXPR for the temporary will be
7071 : added just after the DECL_EXPR for DECL. That's why we don't set
7072 : DECL_INITIAL for local references (instead assigning to them
7073 : explicitly); we need to allow the temporary to be initialized
7074 : first. */
7075 386012 : return initialize_reference (type, init, flags,
7076 386012 : tf_warning_or_error);
7077 : }
7078 :
7079 : /* Designated initializers in arrays are not supported in GNU C++.
7080 : The parser cannot detect this error since it does not know whether
7081 : a given brace-enclosed initializer is for a class type or for an
7082 : array. This function checks that CE does not use a designated
7083 : initializer. If it does, an error is issued. Returns true if CE
7084 : is valid, i.e., does not have a designated initializer. */
7085 :
7086 : bool
7087 156380638 : check_array_designated_initializer (constructor_elt *ce,
7088 : unsigned HOST_WIDE_INT index)
7089 : {
7090 : /* Designated initializers for array elements are not supported. */
7091 156380638 : if (ce->index)
7092 : {
7093 : /* The parser only allows identifiers as designated
7094 : initializers. */
7095 105540028 : if (ce->index == error_mark_node)
7096 : {
7097 0 : error ("name used in a GNU-style designated "
7098 : "initializer for an array");
7099 0 : return false;
7100 : }
7101 105540028 : else if (identifier_p (ce->index))
7102 : {
7103 19 : error ("name %qD used in a GNU-style designated "
7104 : "initializer for an array", ce->index);
7105 19 : ce->index = error_mark_node;
7106 19 : return false;
7107 : }
7108 :
7109 105540009 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7110 : ce->index, true);
7111 105540009 : if (ce_index
7112 105540006 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
7113 211080012 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
7114 : == INTEGER_CST))
7115 : {
7116 : /* A C99 designator is OK if it matches the current index. */
7117 105540003 : if (wi::to_wide (ce_index) == index)
7118 : {
7119 105539997 : ce->index = ce_index;
7120 105539997 : return true;
7121 : }
7122 : else
7123 6 : sorry ("non-trivial designated initializers not supported");
7124 : }
7125 : else
7126 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
7127 : "C99 designator %qE is not an integral constant-expression",
7128 : ce->index);
7129 :
7130 : return false;
7131 : }
7132 :
7133 : return true;
7134 : }
7135 :
7136 : /* When parsing `int a[] = {1, 2};' we don't know the size of the
7137 : array until we finish parsing the initializer. If that's the
7138 : situation we're in, update DECL accordingly. */
7139 :
7140 : static void
7141 77414612 : maybe_deduce_size_from_array_init (tree decl, tree init)
7142 : {
7143 77414612 : tree type = TREE_TYPE (decl);
7144 :
7145 77414612 : if (TREE_CODE (type) == ARRAY_TYPE
7146 993578 : && TYPE_DOMAIN (type) == NULL_TREE
7147 78043836 : && TREE_CODE (decl) != TYPE_DECL)
7148 : {
7149 : /* do_default is really a C-ism to deal with tentative definitions.
7150 : But let's leave it here to ease the eventual merge. */
7151 629224 : int do_default = !DECL_EXTERNAL (decl);
7152 629224 : tree initializer = init ? init : DECL_INITIAL (decl);
7153 629224 : int failure = 0;
7154 :
7155 : /* Check that there are no designated initializers in INIT, as
7156 : those are not supported in GNU C++, and as the middle-end
7157 : will crash if presented with a non-numeric designated
7158 : initializer. */
7159 629224 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7160 : {
7161 149826 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7162 149826 : constructor_elt *ce;
7163 149826 : HOST_WIDE_INT i, j = 0;
7164 49069891 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7165 : {
7166 48921098 : if (instantiation_dependent_expression_p (ce->index))
7167 77414612 : return;
7168 48921092 : if (!check_array_designated_initializer (ce, j))
7169 1 : failure = 1;
7170 : /* If an un-designated initializer is type-dependent, we can't
7171 : check brace elision yet. */
7172 48921092 : if (ce->index == NULL_TREE
7173 48921092 : && type_dependent_expression_p (ce->value))
7174 : return;
7175 48920065 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7176 210 : j += RAW_DATA_LENGTH (ce->value);
7177 : else
7178 48919855 : ++j;
7179 : }
7180 : }
7181 :
7182 148793 : if (failure)
7183 1 : TREE_TYPE (decl) = error_mark_node;
7184 : else
7185 : {
7186 628190 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7187 : do_default);
7188 628190 : if (failure == 1)
7189 : {
7190 33 : error_at (cp_expr_loc_or_loc (initializer,
7191 30 : DECL_SOURCE_LOCATION (decl)),
7192 : "initializer fails to determine size of %qD", decl);
7193 : }
7194 628160 : else if (failure == 2)
7195 : {
7196 30 : if (do_default)
7197 : {
7198 6 : error_at (DECL_SOURCE_LOCATION (decl),
7199 : "array size missing in %qD", decl);
7200 : }
7201 : /* If a `static' var's size isn't known, make it extern as
7202 : well as static, so it does not get allocated. If it's not
7203 : `static', then don't mark it extern; finish_incomplete_decl
7204 : will give it a default size and it will get allocated. */
7205 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7206 0 : DECL_EXTERNAL (decl) = 1;
7207 : }
7208 628130 : else if (failure == 3)
7209 : {
7210 6 : error_at (DECL_SOURCE_LOCATION (decl),
7211 : "zero-size array %qD", decl);
7212 : }
7213 : }
7214 :
7215 628191 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7216 :
7217 628191 : relayout_decl (decl);
7218 :
7219 : /* Update the type of the corresponding TEMPLATE_DECL to match. */
7220 628191 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7221 : {
7222 212 : tree tmpl = template_for_substitution (decl);
7223 212 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
7224 50 : TREE_TYPE (tmpl) = TREE_TYPE (decl);
7225 : }
7226 : }
7227 : }
7228 :
7229 : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7230 : any appropriate error messages regarding the layout. */
7231 :
7232 : static void
7233 66275676 : layout_var_decl (tree decl)
7234 : {
7235 66275676 : tree type;
7236 :
7237 66275676 : type = TREE_TYPE (decl);
7238 66275676 : if (type == error_mark_node)
7239 : return;
7240 :
7241 : /* If we haven't already laid out this declaration, do so now.
7242 : Note that we must not call complete type for an external object
7243 : because it's type might involve templates that we are not
7244 : supposed to instantiate yet. (And it's perfectly valid to say
7245 : `extern X x' for some incomplete type `X'.) */
7246 66275629 : if (!DECL_EXTERNAL (decl))
7247 55572713 : complete_type (type);
7248 66275629 : if (!DECL_SIZE (decl)
7249 637186 : && TREE_TYPE (decl) != error_mark_node
7250 66912815 : && complete_or_array_type_p (type))
7251 479184 : layout_decl (decl, 0);
7252 :
7253 66275629 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7254 : {
7255 : /* An automatic variable with an incomplete type: that is an error.
7256 : Don't talk about array types here, since we took care of that
7257 : message in grokdeclarator. */
7258 17 : error_at (DECL_SOURCE_LOCATION (decl),
7259 : "storage size of %qD isn%'t known", decl);
7260 17 : TREE_TYPE (decl) = error_mark_node;
7261 : }
7262 : #if 0
7263 : /* Keep this code around in case we later want to control debug info
7264 : based on whether a type is "used". (jason 1999-11-11) */
7265 :
7266 : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7267 : /* Let debugger know it should output info for this type. */
7268 : note_debug_info_needed (ttype);
7269 :
7270 : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7271 : note_debug_info_needed (DECL_CONTEXT (decl));
7272 : #endif
7273 :
7274 121848342 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7275 40930492 : && DECL_SIZE (decl) != NULL_TREE
7276 106911956 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
7277 : {
7278 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7279 6 : && !DECL_LOCAL_DECL_P (decl))
7280 0 : constant_expression_warning (DECL_SIZE (decl));
7281 : else
7282 : {
7283 6 : error_at (DECL_SOURCE_LOCATION (decl),
7284 : "storage size of %qD isn%'t constant", decl);
7285 6 : TREE_TYPE (decl) = error_mark_node;
7286 6 : type = error_mark_node;
7287 : }
7288 : }
7289 :
7290 : /* If the final element initializes a flexible array field, adjust
7291 : the size of the DECL with the initializer based on whether the
7292 : DECL is a union or a structure. */
7293 66275629 : if (type != error_mark_node
7294 66275623 : && DECL_INITIAL (decl)
7295 39270031 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7296 5391068 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7297 3624998 : && DECL_SIZE (decl) != NULL_TREE
7298 3624998 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7299 3624998 : && COMPLETE_TYPE_P (type)
7300 3624998 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7301 69900627 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7302 : {
7303 3624998 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7304 3624998 : if (elt.index)
7305 : {
7306 1358771 : tree itype = TREE_TYPE (elt.index);
7307 1358771 : tree vtype = TREE_TYPE (elt.value);
7308 1358771 : if (TREE_CODE (itype) == ARRAY_TYPE
7309 86389 : && TYPE_DOMAIN (itype) == NULL
7310 275 : && TREE_CODE (vtype) == ARRAY_TYPE
7311 1359046 : && COMPLETE_TYPE_P (vtype))
7312 : {
7313 : /* For a structure, add the size of the initializer to the DECL's
7314 : size. */
7315 275 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7316 : {
7317 260 : DECL_SIZE (decl)
7318 260 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7319 : TYPE_SIZE (vtype));
7320 260 : DECL_SIZE_UNIT (decl)
7321 520 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7322 : TYPE_SIZE_UNIT (vtype));
7323 : }
7324 : /* For a union, the DECL's size is the maximum of the current size
7325 : and the size of the initializer. */
7326 : else
7327 : {
7328 15 : DECL_SIZE (decl)
7329 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
7330 : TYPE_SIZE (vtype));
7331 15 : DECL_SIZE_UNIT (decl)
7332 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7333 : TYPE_SIZE_UNIT (vtype));
7334 : }
7335 : }
7336 : }
7337 : }
7338 : }
7339 :
7340 : /* If a local static variable is declared in an inline function, or if
7341 : we have a weak definition, we must endeavor to create only one
7342 : instance of the variable at link-time. */
7343 :
7344 : void
7345 81621082 : maybe_commonize_var (tree decl)
7346 : {
7347 : /* Don't mess with __FUNCTION__ and similar. But do handle structured
7348 : bindings. */
7349 81621082 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7350 : return;
7351 :
7352 : /* Static data in a function with comdat linkage also has comdat
7353 : linkage. */
7354 76196917 : if ((TREE_STATIC (decl)
7355 50574015 : && DECL_FUNCTION_SCOPE_P (decl)
7356 142100 : && vague_linkage_p (DECL_CONTEXT (decl)))
7357 177528328 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7358 : {
7359 44326728 : if (flag_weak)
7360 : {
7361 : /* With weak symbols, we simply make the variable COMDAT;
7362 : that will cause copies in multiple translations units to
7363 : be merged. */
7364 44326701 : comdat_linkage (decl);
7365 : }
7366 : else
7367 : {
7368 27 : if (DECL_INITIAL (decl) == NULL_TREE
7369 27 : || DECL_INITIAL (decl) == error_mark_node)
7370 : {
7371 : /* Without weak symbols, we can use COMMON to merge
7372 : uninitialized variables. */
7373 24 : TREE_PUBLIC (decl) = 1;
7374 24 : DECL_COMMON (decl) = 1;
7375 : }
7376 : else
7377 : {
7378 : /* While for initialized variables, we must use internal
7379 : linkage -- which means that multiple copies will not
7380 : be merged. */
7381 3 : TREE_PUBLIC (decl) = 0;
7382 3 : DECL_COMMON (decl) = 0;
7383 3 : DECL_INTERFACE_KNOWN (decl) = 1;
7384 3 : const char *msg;
7385 3 : if (DECL_INLINE_VAR_P (decl))
7386 : msg = G_("sorry: semantics of inline variable "
7387 : "%q#D are wrong (you%'ll wind up with "
7388 : "multiple copies)");
7389 : else
7390 : msg = G_("sorry: semantics of inline function "
7391 : "static data %q#D are wrong (you%'ll wind "
7392 : "up with multiple copies)");
7393 3 : auto_diagnostic_group d;
7394 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7395 : msg, decl))
7396 3 : inform (DECL_SOURCE_LOCATION (decl),
7397 : "you can work around this by removing the initializer");
7398 3 : }
7399 : }
7400 : }
7401 : }
7402 :
7403 : /* Issue an error message if DECL is an uninitialized const variable.
7404 : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7405 : context from potential_constant_expression. Returns true if all is well,
7406 : false otherwise. */
7407 :
7408 : bool
7409 9412575 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7410 : tsubst_flags_t complain)
7411 : {
7412 9412575 : tree type = strip_array_types (TREE_TYPE (decl));
7413 :
7414 : /* ``Unless explicitly declared extern, a const object does not have
7415 : external linkage and must be initialized. ($8.4; $12.1)'' ARM
7416 : 7.1.6 */
7417 9412575 : if (VAR_P (decl)
7418 9412575 : && !TYPE_REF_P (type)
7419 9363866 : && (CP_TYPE_CONST_P (type)
7420 : /* C++20 permits trivial default initialization in constexpr
7421 : context (P1331R2). */
7422 7280933 : || (cxx_dialect < cxx20
7423 139202 : && (constexpr_context_p
7424 124448 : || var_in_constexpr_fn (decl))))
7425 11512375 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7426 : {
7427 670204 : tree field = default_init_uninitialized_part (type);
7428 670204 : if (!field)
7429 : return true;
7430 :
7431 5124 : auto_diagnostic_group d;
7432 5124 : bool show_notes = true;
7433 :
7434 5124 : if (!constexpr_context_p || cxx_dialect >= cxx20)
7435 : {
7436 188 : if (CP_TYPE_CONST_P (type))
7437 : {
7438 90 : if (complain & tf_error)
7439 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7440 : "uninitialized %<const %D%>", decl);
7441 : }
7442 : else
7443 : {
7444 98 : if (!is_instantiation_of_constexpr (current_function_decl)
7445 98 : && (complain & tf_error))
7446 21 : error_at (DECL_SOURCE_LOCATION (decl),
7447 : "uninitialized variable %qD in %<constexpr%> "
7448 : "function", decl);
7449 : else
7450 : show_notes = false;
7451 98 : cp_function_chain->invalid_constexpr = true;
7452 : }
7453 : }
7454 4936 : else if (complain & tf_error)
7455 2 : error_at (DECL_SOURCE_LOCATION (decl),
7456 : "uninitialized variable %qD in %<constexpr%> context",
7457 : decl);
7458 :
7459 5124 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7460 : {
7461 62 : tree defaulted_ctor;
7462 :
7463 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7464 : "%q#T has no user-provided default constructor", type);
7465 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
7466 62 : if (defaulted_ctor)
7467 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7468 : "constructor is not user-provided because it is "
7469 : "explicitly defaulted in the class body");
7470 62 : inform (DECL_SOURCE_LOCATION (field),
7471 : "and the implicitly-defined constructor does not "
7472 : "initialize %q#D", field);
7473 : }
7474 :
7475 5124 : return false;
7476 5124 : }
7477 :
7478 : return true;
7479 : }
7480 :
7481 : /* Structure holding the current initializer being processed by reshape_init.
7482 : CUR is a pointer to the current element being processed, END is a pointer
7483 : after the last element present in the initializer and RAW_IDX is index into
7484 : RAW_DATA_CST if that is CUR elt. */
7485 : struct reshape_iter
7486 : {
7487 : constructor_elt *cur;
7488 : constructor_elt *end;
7489 : unsigned raw_idx;
7490 : };
7491 :
7492 : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7493 : static tree reshape_single_init (tree, tree, tsubst_flags_t);
7494 :
7495 : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7496 : returned is the next FIELD_DECL (possibly FIELD itself) that can be
7497 : initialized as if for an aggregate class. If there are no more such fields,
7498 : the return value will be NULL. */
7499 :
7500 : tree
7501 15693396 : next_aggregate_field (tree field)
7502 : {
7503 15693396 : while (field
7504 65104682 : && (TREE_CODE (field) != FIELD_DECL
7505 9837895 : || DECL_UNNAMED_BIT_FIELD (field)
7506 9837734 : || (DECL_ARTIFICIAL (field)
7507 : /* In C++17, aggregates can have bases. */
7508 217367 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7509 49411286 : field = DECL_CHAIN (field);
7510 :
7511 15693396 : return field;
7512 : }
7513 :
7514 : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7515 : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7516 : to a subobject. If there are no more such fields, the return value will be
7517 : NULL. */
7518 :
7519 : tree
7520 68670301 : next_subobject_field (tree field)
7521 : {
7522 68670301 : while (field
7523 890854751 : && (TREE_CODE (field) != FIELD_DECL
7524 30522155 : || DECL_UNNAMED_BIT_FIELD (field)
7525 30522016 : || (DECL_ARTIFICIAL (field)
7526 8503649 : && !DECL_FIELD_IS_BASE (field)
7527 1835966 : && !DECL_VIRTUAL_P (field))))
7528 822184450 : field = DECL_CHAIN (field);
7529 :
7530 68670301 : return field;
7531 : }
7532 :
7533 : /* Return true for [dcl.init.list] direct-list-initialization from
7534 : single element of enumeration with a fixed underlying type. */
7535 :
7536 : bool
7537 190440157 : is_direct_enum_init (tree type, tree init)
7538 : {
7539 190440157 : if (cxx_dialect >= cxx17
7540 188347660 : && TREE_CODE (type) == ENUMERAL_TYPE
7541 7733337 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7542 5278476 : && TREE_CODE (init) == CONSTRUCTOR
7543 453948 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
7544 230727 : && CONSTRUCTOR_NELTS (init) == 1
7545 217 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7546 : /* DR 2374: The single element needs to be implicitly
7547 : convertible to the underlying type of the enum. */
7548 217 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7549 190440372 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7550 215 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7551 215 : CONSTRUCTOR_ELT (init, 0)->value,
7552 : LOOKUP_IMPLICIT, tf_none))
7553 : return true;
7554 : return false;
7555 : }
7556 :
7557 : /* Helper function for reshape_init*. Split first element of
7558 : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7559 : to true if the whole d->cur has been consumed. */
7560 :
7561 : static tree
7562 62725493 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7563 : {
7564 62725493 : *inc_cur = true;
7565 62725493 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7566 : return NULL_TREE;
7567 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7568 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7569 30588 : *inc_cur = false;
7570 : else
7571 132 : d->raw_idx = 0;
7572 : return ret;
7573 : }
7574 :
7575 : /* Wrapper around that which for RAW_DATA_CST in INIT
7576 : (as well as in D->cur->value) peels off the first element
7577 : of the raw data and returns it, otherwise increments
7578 : D->cur and returns INIT. */
7579 :
7580 : static tree
7581 62725476 : consume_init (tree init, reshape_iter *d)
7582 : {
7583 62725476 : bool inc_cur;
7584 62725476 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7585 30720 : init = raw_init;
7586 62725476 : if (inc_cur)
7587 62694888 : d->cur++;
7588 62725476 : return init;
7589 : }
7590 :
7591 : /* Subroutine of reshape_init_array and reshape_init_vector, which does
7592 : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7593 : INTEGER_CST representing the size of the array minus one (the maximum index),
7594 : or NULL_TREE if the array was declared without specifying the size. D is
7595 : the iterator within the constructor. */
7596 :
7597 : static tree
7598 472437 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7599 : tree first_initializer_p, bool vector_p,
7600 : tsubst_flags_t complain)
7601 : {
7602 472437 : tree new_init;
7603 472437 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7604 472437 : unsigned HOST_WIDE_INT max_index_cst = 0;
7605 472437 : unsigned HOST_WIDE_INT index;
7606 :
7607 : /* The initializer for an array is always a CONSTRUCTOR. If this is the
7608 : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7609 : to build a new one. But don't reuse if not complaining; if this is
7610 : tentative, we might also reshape to another type (95319). */
7611 472437 : bool reuse = (first_initializer_p
7612 413487 : && (complain & tf_error)
7613 412202 : && !CP_AGGREGATE_TYPE_P (elt_type)
7614 819146 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7615 129464 : if (reuse)
7616 : new_init = first_initializer_p;
7617 : else
7618 129464 : new_init = build_constructor (init_list_type_node, NULL);
7619 :
7620 472437 : if (sized_array_p)
7621 : {
7622 282062 : poly_uint64 midx;
7623 : /* Minus 1 is used for zero sized arrays. */
7624 282062 : if (integer_all_onesp (max_index))
7625 24 : return new_init;
7626 :
7627 282038 : if (tree_fits_poly_uint64_p (max_index))
7628 282038 : midx = tree_to_poly_uint64 (max_index);
7629 : /* sizetype is sign extended, not zero extended. */
7630 : else
7631 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7632 :
7633 : /* For VLA vectors, we restrict the number of elements in the constructor
7634 : to lower bound of the VLA elements. */
7635 282038 : max_index_cst = constant_lower_bound (midx);
7636 : }
7637 :
7638 472413 : constructor_elt *first_cur = d->cur;
7639 : /* Loop until there are no more initializers. */
7640 57129224 : for (index = 0;
7641 57129224 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7642 : ++index)
7643 : {
7644 56656992 : tree elt_init;
7645 56656992 : constructor_elt *old_cur = d->cur;
7646 56656992 : unsigned int old_raw_idx = d->raw_idx;
7647 56656992 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7648 :
7649 56656992 : if (d->cur->index)
7650 5844181 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7651 56656992 : check_array_designated_initializer (d->cur, index);
7652 56656992 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7653 32752 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7654 10233 : || is_byte_access_type (elt_type))
7655 22519 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7656 1858 : && (!sized_array_p || index < max_index_cst)
7657 56658838 : && !vector_p)
7658 : {
7659 526 : elt_init = d->cur->value;
7660 526 : unsigned int off = d->raw_idx;
7661 526 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7662 526 : if (!sized_array_p || len <= max_index_cst - index + 1)
7663 : {
7664 517 : d->cur++;
7665 517 : d->raw_idx = 0;
7666 : }
7667 : else
7668 : {
7669 9 : len = max_index_cst - index + 1;
7670 9 : d->raw_idx += len;
7671 : }
7672 526 : if (!reuse || off || d->cur == old_cur)
7673 : {
7674 27 : elt_init = copy_node (elt_init);
7675 27 : RAW_DATA_LENGTH (elt_init) = len;
7676 27 : RAW_DATA_POINTER (elt_init) += off;
7677 : }
7678 526 : TREE_TYPE (elt_init) = elt_type;
7679 : }
7680 56656466 : else if (d->cur->index)
7681 : {
7682 5843956 : elt_init = reshape_single_init (elt_type, d->cur->value, complain);
7683 5843956 : d->cur++;
7684 : }
7685 : else
7686 50812510 : elt_init = reshape_init_r (elt_type, d,
7687 : /*first_initializer_p=*/NULL_TREE,
7688 : complain);
7689 56656992 : if (elt_init == error_mark_node)
7690 : return error_mark_node;
7691 56656814 : tree idx = size_int (index);
7692 56656814 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7693 : {
7694 : /* We need to stop reusing as some RAW_DATA_CST in the original
7695 : ctor had to be split. */
7696 72 : new_init = build_constructor (init_list_type_node, NULL);
7697 72 : if (index)
7698 : {
7699 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7700 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7701 72 : (d->cur - first_cur)
7702 : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7703 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7704 : {
7705 : unsigned int j;
7706 : tree nidx, nval;
7707 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7708 : j, nidx, nval)
7709 4 : if (nidx)
7710 : {
7711 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7712 4 : (void) nval;
7713 4 : break;
7714 : }
7715 : }
7716 : }
7717 : reuse = false;
7718 : }
7719 56656746 : if (reuse)
7720 : {
7721 55148399 : old_cur->index = idx;
7722 55148399 : old_cur->value = elt_init;
7723 : }
7724 : else
7725 1508415 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7726 : idx, elt_init);
7727 56656814 : if (!TREE_CONSTANT (elt_init))
7728 196635 : TREE_CONSTANT (new_init) = false;
7729 :
7730 : /* This can happen with an invalid initializer (c++/54501). */
7731 56656814 : if (d->cur == old_cur
7732 32082 : && !sized_array_p
7733 14085 : && d->raw_idx == old_raw_idx)
7734 : break;
7735 :
7736 56656811 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7737 526 : index += RAW_DATA_LENGTH (elt_init) - 1;
7738 : }
7739 :
7740 : return new_init;
7741 : }
7742 :
7743 : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7744 : Parameters are the same of reshape_init_r. */
7745 :
7746 : static tree
7747 422540 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7748 : tsubst_flags_t complain)
7749 : {
7750 422540 : tree max_index = NULL_TREE;
7751 :
7752 422540 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7753 :
7754 422540 : if (TYPE_DOMAIN (type))
7755 232211 : max_index = array_type_nelts_minus_one (type);
7756 :
7757 422540 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7758 422540 : first_initializer_p, false, complain);
7759 : }
7760 :
7761 : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7762 : Parameters are the same of reshape_init_r. */
7763 :
7764 : static tree
7765 49897 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7766 : {
7767 49897 : tree max_index = NULL_TREE;
7768 :
7769 49897 : gcc_assert (VECTOR_TYPE_P (type));
7770 :
7771 49897 : if (COMPOUND_LITERAL_P (d->cur->value))
7772 : {
7773 0 : tree value = d->cur->value;
7774 0 : if (!same_type_p (TREE_TYPE (value), type))
7775 : {
7776 0 : if (complain & tf_error)
7777 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7778 0 : TREE_TYPE (d->cur->value), type);
7779 0 : value = error_mark_node;
7780 : }
7781 0 : ++d->cur;
7782 0 : return value;
7783 : }
7784 :
7785 : /* For a vector, we initialize it as an array of the appropriate size. */
7786 49897 : if (VECTOR_TYPE_P (type))
7787 49897 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7788 :
7789 49897 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7790 49897 : NULL_TREE, true, complain);
7791 : }
7792 :
7793 : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7794 : INIT, in isolation from any designator or other initializers. */
7795 :
7796 : static tree
7797 6499367 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7798 : {
7799 : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7800 : calling reshape_init, but this way can just live on the stack. */
7801 6499367 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7802 6499367 : reshape_iter iter = { &elt, &elt + 1, 0 };
7803 6499367 : return reshape_init_r (type, &iter,
7804 : /*first_initializer_p=*/NULL_TREE,
7805 6499367 : complain);
7806 : }
7807 :
7808 : /* Subroutine of reshape_init_r, processes the initializers for classes
7809 : or union. Parameters are the same of reshape_init_r. */
7810 :
7811 : static tree
7812 3778976 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7813 : tsubst_flags_t complain)
7814 : {
7815 3778976 : tree field;
7816 3778976 : tree new_init;
7817 :
7818 3778976 : gcc_assert (CLASS_TYPE_P (type));
7819 :
7820 : /* The initializer for a class is always a CONSTRUCTOR. */
7821 3778976 : new_init = build_constructor (init_list_type_node, NULL);
7822 :
7823 3778976 : int binfo_idx = -1;
7824 3778976 : tree binfo = TYPE_BINFO (type);
7825 3778976 : tree base_binfo = NULL_TREE;
7826 3778976 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7827 : {
7828 : /* We get here from maybe_aggr_guide for C++20 class template argument
7829 : deduction. In this case we need to look through the binfo because a
7830 : template doesn't have base fields. */
7831 2775 : binfo_idx = 0;
7832 2775 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7833 : }
7834 86 : if (base_binfo)
7835 : field = base_binfo;
7836 : else
7837 3778890 : field = next_aggregate_field (TYPE_FIELDS (type));
7838 :
7839 3778890 : if (!field)
7840 : {
7841 : /* [dcl.init.aggr]
7842 :
7843 : An initializer for an aggregate member that is an
7844 : empty class shall have the form of an empty
7845 : initializer-list {}. */
7846 322 : if (!first_initializer_p)
7847 : {
7848 268 : if (complain & tf_error)
7849 13 : error ("initializer for %qT must be brace-enclosed", type);
7850 268 : return error_mark_node;
7851 : }
7852 : return new_init;
7853 : }
7854 :
7855 : /* For C++29 designated initializers we do modify d->cur->index in place
7856 : to cache name lookup results. Make sure to undo it before returning. */
7857 3778654 : struct designator_undo {
7858 : constructor_elt *start, *end;
7859 3778715 : void undo ()
7860 : {
7861 3778866 : while (start != end)
7862 : {
7863 151 : start->index = DECL_NAME (start->index);
7864 151 : ++start;
7865 : }
7866 3778715 : start = end = nullptr;
7867 3778715 : }
7868 3778654 : ~designator_undo () { undo (); }
7869 3778654 : } desig_undo = { nullptr, nullptr };
7870 :
7871 : /* For C++20 CTAD, handle pack expansions in the base list. */
7872 3778654 : tree last_was_pack_expansion = NULL_TREE;
7873 3778654 : bool first_desig = true;
7874 :
7875 : /* Loop through the initializable fields, gathering initializers. */
7876 11302537 : while (d->cur != d->end)
7877 : {
7878 7632743 : tree field_init;
7879 7632743 : constructor_elt *old_cur = d->cur;
7880 7632743 : unsigned old_raw_idx = d->raw_idx;
7881 7632743 : bool direct_desig = false;
7882 :
7883 : /* Handle C++20 designated initializers. */
7884 7632743 : if (d->cur->index)
7885 : {
7886 655745 : if (d->cur->index == error_mark_node)
7887 : return error_mark_node;
7888 :
7889 655738 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7890 : {
7891 181 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7892 181 : direct_desig = true;
7893 181 : field = d->cur->index;
7894 : }
7895 655557 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7896 : {
7897 655551 : if (first_desig && cxx_dialect >= cxx20)
7898 : {
7899 311103 : if (CONSTRUCTOR_NELTS (new_init))
7900 : {
7901 115 : constructor_elt *last
7902 115 : = &CONSTRUCTOR_ELTS (new_init)->last ();
7903 115 : if (last->index == NULL_TREE
7904 115 : || TREE_CODE (last->index) != FIELD_DECL
7905 230 : || !DECL_FIELD_IS_BASE (last->index))
7906 : {
7907 23 : if (complain & tf_error)
7908 23 : error ("last non-designated initializer clause "
7909 : "does not appertain to a base class "
7910 : "subobject");
7911 23 : return error_mark_node;
7912 : }
7913 : }
7914 : first_desig = false;
7915 : }
7916 655528 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7917 655528 : field = get_class_binding (type, d->cur->index);
7918 655528 : direct_desig = true;
7919 655528 : if (!field && cxx_dialect >= cxx29)
7920 44 : field = lookup_member (type, d->cur->index, /*protect=*/2,
7921 : /*want_type=*/false, complain);
7922 : }
7923 : else
7924 : {
7925 6 : if (complain & tf_error)
7926 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7927 : " for class %qT", d->cur->index, type);
7928 6 : return error_mark_node;
7929 : }
7930 :
7931 257 : if (!field && ANON_AGGR_TYPE_P (type))
7932 : /* Apparently the designator isn't for a member of this anonymous
7933 : struct, so head back to the enclosing class. */
7934 : break;
7935 :
7936 655706 : if (!field || TREE_CODE (field) != FIELD_DECL)
7937 : {
7938 43 : if (complain & tf_error)
7939 : {
7940 28 : if (field && TREE_CODE (field) == TREE_LIST)
7941 : {
7942 5 : auto_diagnostic_group g;
7943 5 : error ("request for member %qD is ambiguous",
7944 5 : d->cur->index);
7945 5 : print_candidates (input_location, field);
7946 5 : }
7947 : else
7948 23 : error ("%qT has no non-static data member named %qD", type,
7949 23 : d->cur->index);
7950 : }
7951 43 : return error_mark_node;
7952 : }
7953 :
7954 : /* If the element is an anonymous union object and the initializer
7955 : list is a designated-initializer-list, the anonymous union object
7956 : is initialized by the designated-initializer-list { D }, where D
7957 : is the designated-initializer-clause naming a member of the
7958 : anonymous union object. */
7959 655663 : tree ictx = DECL_CONTEXT (field);
7960 655663 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7961 : {
7962 : /* Find the anon aggr that is a direct member of TYPE. */
7963 266 : while (ANON_AGGR_TYPE_P (ictx))
7964 : {
7965 154 : tree cctx = TYPE_CONTEXT (ictx);
7966 154 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7967 140 : goto found;
7968 : ictx = cctx;
7969 : }
7970 :
7971 : /* In C++29 a designator can name a member of a base; in that
7972 : case, go through the designators and replace ids with _DECLs
7973 : to record the lookup for the most-derived class. */
7974 112 : if (cxx_dialect >= cxx29)
7975 : {
7976 110 : tree ibinfo = lookup_base (type, ictx, ba_unique, NULL,
7977 : complain);
7978 110 : if (!ibinfo)
7979 : /* The designator names a field outside this base class,
7980 : so we're done. */
7981 : break;
7982 61 : else if (ibinfo != error_mark_node)
7983 : {
7984 74 : while (BINFO_INHERITANCE_CHAIN (ibinfo) != binfo)
7985 : ibinfo = BINFO_INHERITANCE_CHAIN (ibinfo);
7986 61 : ictx = TREE_TYPE (ibinfo);
7987 :
7988 61 : desig_undo.undo ();
7989 :
7990 61 : if (d->cur->index != field)
7991 : {
7992 47 : d->cur->index = field;
7993 47 : desig_undo.start = d->cur;
7994 : }
7995 61 : constructor_elt *e = d->cur + 1;
7996 228 : for (; e != d->end; ++e)
7997 : {
7998 173 : if (e->index == NULL_TREE
7999 173 : || e->index == error_mark_node)
8000 : break;
8001 167 : if (desig_undo.start)
8002 : {
8003 104 : gcc_assert (TREE_CODE (e->index)
8004 : == IDENTIFIER_NODE);
8005 104 : field = lookup_member (type, e->index,
8006 : /*protect=*/2,
8007 : /*want_type=*/false,
8008 : tf_none);
8009 104 : if (!field || TREE_CODE (field) != FIELD_DECL)
8010 : break;
8011 : }
8012 : else
8013 : {
8014 63 : gcc_assert (TREE_CODE (e->index) == FIELD_DECL);
8015 : field = e->index;
8016 : }
8017 :
8018 167 : if (desig_undo.start)
8019 104 : e->index = field;
8020 : }
8021 61 : if (desig_undo.start)
8022 47 : desig_undo.end = e;
8023 61 : goto found;
8024 : }
8025 : }
8026 :
8027 : /* Not found, e.g. FIELD is a member of a base class. */
8028 2 : if (complain & tf_error)
8029 2 : error ("%qD is not a direct member of %qT", field, type);
8030 2 : return error_mark_node;
8031 :
8032 201 : found:
8033 : /* Now find the TYPE member with that anon aggr type. */
8034 201 : tree aafield = TYPE_FIELDS (type);
8035 1622 : for (; aafield; aafield = TREE_CHAIN (aafield))
8036 1622 : if (TREE_TYPE (aafield) == ictx)
8037 : break;
8038 0 : gcc_assert (aafield);
8039 : field = aafield;
8040 : direct_desig = false;
8041 : }
8042 : }
8043 :
8044 : /* If we processed all the member of the class, we are done. */
8045 7632409 : if (!field)
8046 : break;
8047 :
8048 15264250 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
8049 7632135 : ? field : NULL_TREE);
8050 20 : if (last_was_pack_expansion)
8051 : /* Each non-trailing aggregate element that is a pack expansion is
8052 : assumed to correspond to no elements of the initializer list. */
8053 20 : goto continue_;
8054 :
8055 7632115 : if (direct_desig)
8056 : {
8057 : /* The designated field F is initialized from this one element.
8058 :
8059 : Note that we don't want to do this if we found the designator
8060 : inside an anon aggr above; we use the normal code to implement:
8061 :
8062 : "If the element is an anonymous union member and the initializer
8063 : list is a brace-enclosed designated- initializer-list, the element
8064 : is initialized by the designated-initializer-list { D }, where D
8065 : is the designated- initializer-clause naming a member of the
8066 : anonymous union member." */
8067 655411 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
8068 655411 : field_init = reshape_single_init (TREE_TYPE (field),
8069 : d->cur->value, complain);
8070 655411 : d->cur++;
8071 : }
8072 : else
8073 6976704 : field_init = reshape_init_r (TREE_TYPE (field), d,
8074 : /*first_initializer_p=*/NULL_TREE,
8075 : complain);
8076 :
8077 7632115 : if (field_init == error_mark_node)
8078 : return error_mark_node;
8079 :
8080 7631827 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
8081 : {
8082 : /* This can happen with an invalid initializer for a flexible
8083 : array member (c++/54441). */
8084 0 : if (complain & tf_error)
8085 0 : error ("invalid initializer for %q#D", field);
8086 0 : return error_mark_node;
8087 : }
8088 :
8089 7631827 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
8090 :
8091 : /* [dcl.init.aggr]
8092 :
8093 : When a union is initialized with a brace-enclosed
8094 : initializer, the braces shall only contain an
8095 : initializer for the first member of the union. */
8096 7631827 : if (TREE_CODE (type) == UNION_TYPE)
8097 : break;
8098 :
8099 7523863 : continue_:
8100 7523883 : if (base_binfo)
8101 : {
8102 101 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
8103 : field = base_binfo;
8104 : else
8105 38 : field = next_aggregate_field (TYPE_FIELDS (type));
8106 : }
8107 : else
8108 7523782 : field = next_aggregate_field (DECL_CHAIN (field));
8109 : }
8110 :
8111 : /* A trailing aggregate element that is a pack expansion is assumed to
8112 : correspond to all remaining elements of the initializer list (if any). */
8113 3778285 : if (last_was_pack_expansion)
8114 : {
8115 17 : tree init = d->cur->value;
8116 17 : bool inc_cur;
8117 17 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
8118 0 : init = raw_init;
8119 17 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
8120 : last_was_pack_expansion, init);
8121 49 : while (d->cur != d->end)
8122 32 : d->cur++;
8123 : }
8124 :
8125 : return new_init;
8126 3778654 : }
8127 :
8128 : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
8129 : designators are not valid; either complain or return true to indicate
8130 : that reshape_init_r should return error_mark_node. */
8131 :
8132 : static bool
8133 251786 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
8134 : {
8135 251786 : if (d->cur->index)
8136 : {
8137 30 : if (complain & tf_error)
8138 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
8139 : "C99 designator %qE outside aggregate initializer",
8140 : d->cur->index);
8141 : else
8142 : return true;
8143 : }
8144 : return false;
8145 : }
8146 :
8147 : /* Subroutine of reshape_init, which processes a single initializer (part of
8148 : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
8149 : iterator within the CONSTRUCTOR which points to the initializer to process.
8150 : If this is the first initializer of the outermost CONSTRUCTOR node,
8151 : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
8152 :
8153 : static tree
8154 68781646 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
8155 : tsubst_flags_t complain)
8156 : {
8157 68781646 : tree init = d->cur->value;
8158 :
8159 68781646 : if (error_operand_p (init))
8160 128 : return error_mark_node;
8161 :
8162 4492940 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
8163 69033125 : && has_designator_problem (d, complain))
8164 0 : return error_mark_node;
8165 :
8166 68781518 : tree stripped_init = tree_strip_any_location_wrapper (init);
8167 :
8168 68781518 : if (TREE_CODE (type) == COMPLEX_TYPE)
8169 : {
8170 : /* A complex type can be initialized from one or two initializers,
8171 : but braces are not elided. */
8172 672 : init = consume_init (init, d);
8173 672 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
8174 : {
8175 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
8176 : {
8177 0 : if (complain & tf_error)
8178 0 : error ("too many initializers for %qT", type);
8179 : else
8180 0 : return error_mark_node;
8181 : }
8182 : }
8183 663 : else if (first_initializer_p && d->cur != d->end)
8184 : {
8185 36 : if (error_operand_p (d->cur->value)
8186 36 : || has_designator_problem (d, complain))
8187 0 : return error_mark_node;
8188 36 : vec<constructor_elt, va_gc> *v = 0;
8189 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8190 36 : init = consume_init (d->cur->value, d);
8191 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
8192 36 : init = build_constructor (init_list_type_node, v);
8193 : }
8194 : return init;
8195 : }
8196 :
8197 : /* If we have a designator, d doesn't initialize TYPE directly, it
8198 : initializes an element, with brace elision if !first_initializer_p. But
8199 : if TYPE is non-aggregate (and we didn't already return error_mark_node),
8200 : we should have errored about the designator in has_designator_problem, so
8201 : now ignore it for error recovery. */
8202 68780846 : if (d->cur->index)
8203 : {
8204 : /* Deliberately not CP_AGGREGATE_TYPE_P to get a better diagnostic for
8205 : trying to designate a member of a non-aggregate class. */
8206 368355 : if (AGGREGATE_TYPE_P (type))
8207 368349 : goto skip_single;
8208 : else
8209 6 : gcc_checking_assert (seen_error ());
8210 : }
8211 :
8212 : /* A non-aggregate type is always initialized with a single
8213 : initializer. */
8214 136766299 : if (!CP_AGGREGATE_TYPE_P (type)
8215 : /* As is an array with dependent bound, which we can see
8216 : during C++20 aggregate CTAD. */
8217 74047235 : || (cxx_dialect >= cxx20
8218 5593089 : && TREE_CODE (type) == ARRAY_TYPE
8219 457611 : && uses_template_parms (TYPE_DOMAIN (type))))
8220 : {
8221 : /* It is invalid to initialize a non-aggregate type with a
8222 : brace-enclosed initializer before C++0x.
8223 : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
8224 : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
8225 : a CONSTRUCTOR (with a record type). */
8226 62719075 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
8227 : /* Don't complain about a capture-init. */
8228 304454 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
8229 63012766 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
8230 : {
8231 293652 : if (SCALAR_TYPE_P (type))
8232 : {
8233 486 : if (cxx_dialect < cxx11)
8234 : {
8235 16 : if (complain & tf_error)
8236 16 : error ("braces around scalar initializer for type %qT",
8237 : type);
8238 16 : init = error_mark_node;
8239 : }
8240 470 : else if (first_initializer_p
8241 470 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
8242 296 : && (BRACE_ENCLOSED_INITIALIZER_P
8243 : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8244 : {
8245 13 : if (complain & tf_error)
8246 13 : error ("too many braces around scalar initializer "
8247 : "for type %qT", type);
8248 13 : init = error_mark_node;
8249 : }
8250 : }
8251 : else
8252 293166 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8253 : }
8254 62719075 : return consume_init (init, d);
8255 : }
8256 :
8257 : /* "If T is a class type and the initializer list has a single element of
8258 : type cv U, where U is T or a class derived from T, the object is
8259 : initialized from that element." Even if T is an aggregate. */
8260 5669582 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8261 5186470 : && first_initializer_p
8262 3497161 : && d->end - d->cur == 1
8263 608573 : && TREE_CODE (init) != RAW_DATA_CST
8264 6301995 : && reference_related_p (type, TREE_TYPE (init)))
8265 : {
8266 426 : d->cur++;
8267 426 : return init;
8268 : }
8269 :
8270 : /* [dcl.init.aggr]
8271 :
8272 : All implicit type conversions (clause _conv_) are considered when
8273 : initializing the aggregate member with an initializer from an
8274 : initializer-list. If the initializer can initialize a member,
8275 : the member is initialized. Otherwise, if the member is itself a
8276 : non-empty subaggregate, brace elision is assumed and the
8277 : initializer is considered for the initialization of the first
8278 : member of the subaggregate. */
8279 2446165 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8280 : /* But don't try this for the first initializer, since that would be
8281 : looking through the outermost braces; A a2 = { a1 }; is not a
8282 : valid aggregate initialization. */
8283 3246897 : && !first_initializer_p
8284 5709904 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8285 11251 : || can_convert_arg (type, TREE_TYPE (init),
8286 11251 : TREE_CODE (init) == RAW_DATA_CST
8287 5692996 : ? build_int_cst (integer_type_node,
8288 1606 : *(const unsigned char *)
8289 1606 : RAW_DATA_POINTER (init))
8290 : : init,
8291 : LOOKUP_NORMAL, complain)))
8292 5693 : return consume_init (init, d);
8293 :
8294 6055652 : skip_single:
8295 :
8296 : /* [dcl.init.string]
8297 :
8298 : A char array (whether plain char, signed char, or unsigned char)
8299 : can be initialized by a string-literal (optionally enclosed in
8300 : braces); a wchar_t array can be initialized by a wide
8301 : string-literal (optionally enclosed in braces). */
8302 6055652 : if (TREE_CODE (type) == ARRAY_TYPE
8303 6055652 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8304 : {
8305 111933 : tree str_init = init;
8306 111933 : tree stripped_str_init = stripped_init;
8307 111933 : reshape_iter stripd = {};
8308 :
8309 : /* Strip one level of braces if and only if they enclose a single
8310 : element (as allowed by [dcl.init.string]). */
8311 111933 : if (!first_initializer_p
8312 2742 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8313 113521 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8314 : {
8315 392 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8316 392 : str_init = stripd.cur->value;
8317 392 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
8318 : }
8319 :
8320 : /* If it's a string literal, then it's the initializer for the array
8321 : as a whole. Otherwise, continue with normal initialization for
8322 : array types (one value per array element). */
8323 111933 : if (TREE_CODE (stripped_str_init) == STRING_CST)
8324 : {
8325 72 : if ((first_initializer_p && has_designator_problem (d, complain))
8326 1062 : || (stripd.cur && has_designator_problem (&stripd, complain)))
8327 990 : return error_mark_node;
8328 990 : d->cur++;
8329 990 : return str_init;
8330 : }
8331 : }
8332 :
8333 : /* The following cases are about aggregates. If we are not within a full
8334 : initializer already, and there is not a CONSTRUCTOR, it means that there
8335 : is a missing set of braces (that is, we are processing the case for
8336 : which reshape_init exists). */
8337 6054662 : bool braces_elided_p = false;
8338 6054662 : if (!first_initializer_p)
8339 : {
8340 1813827 : if (TREE_CODE (stripped_init) == CONSTRUCTOR && !d->cur->index)
8341 : {
8342 1803266 : tree init_type = TREE_TYPE (init);
8343 1803266 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8344 : /* There is no need to call reshape_init for pointer-to-member
8345 : function initializers, as they are always constructed correctly
8346 : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8347 : which is missing outermost braces. We should warn below, and
8348 : one of the routines below will wrap it in additional { }. */;
8349 : /* For a nested compound literal, proceed to specialized routines,
8350 : to handle initialization of arrays and similar. */
8351 1803257 : else if (COMPOUND_LITERAL_P (stripped_init))
8352 8 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8353 : /* A CONSTRUCTOR of the target's type is a previously
8354 : digested initializer. */
8355 1803249 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8356 : {
8357 0 : ++d->cur;
8358 0 : return init;
8359 : }
8360 : else
8361 : {
8362 : /* Something that hasn't been reshaped yet. */
8363 1803249 : ++d->cur;
8364 1803249 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8365 1803249 : return reshape_init (type, init, complain);
8366 : }
8367 : }
8368 :
8369 10578 : if (complain & tf_warning)
8370 9388 : warning (OPT_Wmissing_braces,
8371 : "missing braces around initializer for %qT",
8372 : type);
8373 : braces_elided_p = true;
8374 : }
8375 :
8376 : /* Dispatch to specialized routines. */
8377 4251413 : tree new_init;
8378 4251413 : if (CLASS_TYPE_P (type))
8379 3778976 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
8380 472437 : else if (TREE_CODE (type) == ARRAY_TYPE)
8381 422540 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
8382 49897 : else if (VECTOR_TYPE_P (type))
8383 49897 : new_init = reshape_init_vector (type, d, complain);
8384 : else
8385 0 : gcc_unreachable ();
8386 :
8387 4251413 : if (braces_elided_p
8388 10578 : && TREE_CODE (new_init) == CONSTRUCTOR)
8389 10303 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8390 :
8391 : return new_init;
8392 : }
8393 :
8394 : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
8395 : brace-enclosed aggregate initializer.
8396 :
8397 : INIT is the CONSTRUCTOR containing the list of initializers describing
8398 : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8399 : It may not presently match the shape of the TYPE; for example:
8400 :
8401 : struct S { int a; int b; };
8402 : struct S a[] = { 1, 2, 3, 4 };
8403 :
8404 : Here INIT will hold a vector of four elements, rather than a
8405 : vector of two elements, each itself a vector of two elements. This
8406 : routine transforms INIT from the former form into the latter. The
8407 : revised CONSTRUCTOR node is returned. */
8408 :
8409 : tree
8410 12646580 : reshape_init (tree type, tree init, tsubst_flags_t complain)
8411 : {
8412 12646580 : vec<constructor_elt, va_gc> *v;
8413 12646580 : reshape_iter d;
8414 12646580 : tree new_init;
8415 :
8416 12646580 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8417 :
8418 12646580 : v = CONSTRUCTOR_ELTS (init);
8419 :
8420 : /* An empty constructor does not need reshaping, and it is always a valid
8421 : initializer. */
8422 12646580 : if (vec_safe_is_empty (v))
8423 : return init;
8424 :
8425 4494238 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8426 : /* Already reshaped. */
8427 : return init;
8428 :
8429 : /* Brace elision is not performed for a CONSTRUCTOR representing
8430 : parenthesized aggregate initialization. */
8431 4494089 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
8432 : {
8433 849 : tree elt = (*v)[0].value;
8434 : /* If we're initializing a char array from a string-literal that is
8435 : enclosed in braces, unwrap it here. */
8436 849 : if (TREE_CODE (type) == ARRAY_TYPE
8437 277 : && vec_safe_length (v) == 1
8438 149 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8439 888 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8440 34 : return elt;
8441 : return init;
8442 : }
8443 :
8444 : /* Handle [dcl.init.list] direct-list-initialization from
8445 : single element of enumeration with a fixed underlying type. */
8446 4493240 : if (is_direct_enum_init (type, init))
8447 : {
8448 175 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8449 175 : type = cv_unqualified (type);
8450 175 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8451 : {
8452 160 : warning_sentinel w (warn_useless_cast);
8453 160 : warning_sentinel w2 (warn_ignored_qualifiers);
8454 160 : return cp_build_c_cast (input_location, type, elt,
8455 : tf_warning_or_error);
8456 160 : }
8457 : else
8458 15 : return error_mark_node;
8459 : }
8460 :
8461 : /* Recurse on this CONSTRUCTOR. */
8462 4493065 : d.cur = &(*v)[0];
8463 4493065 : d.end = d.cur + v->length ();
8464 4493065 : d.raw_idx = 0;
8465 :
8466 4493065 : new_init = reshape_init_r (type, &d, init, complain);
8467 4493065 : if (new_init == error_mark_node)
8468 : return error_mark_node;
8469 :
8470 : /* Make sure all the element of the constructor were used. Otherwise,
8471 : issue an error about exceeding initializers. */
8472 4492394 : if (d.cur != d.end)
8473 : {
8474 207 : if (complain & tf_error)
8475 64 : error ("too many initializers for %qT", type);
8476 207 : return error_mark_node;
8477 : }
8478 :
8479 4492187 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8480 4492187 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8481 624262 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8482 4492187 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8483 4492187 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8484 74931 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8485 : || seen_error ());
8486 :
8487 : return new_init;
8488 : }
8489 :
8490 : /* Verify array initializer. Returns true if errors have been reported. */
8491 :
8492 : bool
8493 1037466 : check_array_initializer (tree decl, tree type, tree init)
8494 : {
8495 1037466 : tree element_type = TREE_TYPE (type);
8496 :
8497 : /* Structured binding when initialized with an array type needs
8498 : to have complete type. */
8499 1037466 : if (decl
8500 1019025 : && DECL_DECOMPOSITION_P (decl)
8501 10451 : && DECL_DECOMP_IS_BASE (decl)
8502 1047917 : && !COMPLETE_TYPE_P (type))
8503 : {
8504 0 : error_at (DECL_SOURCE_LOCATION (decl),
8505 : "structured binding has incomplete type %qT", type);
8506 0 : TREE_TYPE (decl) = error_mark_node;
8507 0 : return true;
8508 : }
8509 :
8510 : /* The array type itself need not be complete, because the
8511 : initializer may tell us how many elements are in the array.
8512 : But, the elements of the array must be complete. */
8513 1037466 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
8514 : {
8515 15 : if (decl)
8516 9 : error_at (DECL_SOURCE_LOCATION (decl),
8517 : "elements of array %q#D have incomplete type", decl);
8518 : else
8519 6 : error ("elements of array %q#T have incomplete type", type);
8520 : return true;
8521 : }
8522 :
8523 1037451 : location_t loc = (decl ? location_of (decl) : input_location);
8524 1037451 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8525 : return true;
8526 :
8527 : /* A compound literal can't have variable size. */
8528 1037451 : if (init && !decl
8529 1037451 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8530 18432 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8531 : {
8532 3 : error ("variable-sized compound literal");
8533 3 : return true;
8534 : }
8535 : return false;
8536 : }
8537 :
8538 : /* Subroutine of check_initializer; args are passed down from that function.
8539 : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8540 :
8541 : static tree
8542 3283720 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
8543 : {
8544 3283720 : gcc_assert (stmts_are_full_exprs_p ());
8545 3283720 : if (init)
8546 2673393 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8547 3283720 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
8548 : }
8549 :
8550 : /* Verify INIT (the initializer for DECL), and record the
8551 : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8552 : grok_reference_init.
8553 :
8554 : If the return value is non-NULL, it is an expression that must be
8555 : evaluated dynamically to initialize DECL. */
8556 :
8557 : static tree
8558 59270046 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8559 : {
8560 59270046 : tree type;
8561 59270046 : tree init_code = NULL;
8562 59270046 : tree core_type;
8563 :
8564 : /* Things that are going to be initialized need to have complete
8565 : type. */
8566 59270046 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8567 :
8568 59270046 : if (DECL_HAS_VALUE_EXPR_P (decl))
8569 : {
8570 : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8571 : it doesn't have storage to be initialized. */
8572 108 : gcc_assert (init == NULL_TREE);
8573 : return NULL_TREE;
8574 : }
8575 :
8576 59269938 : if (type == error_mark_node)
8577 : /* We will have already complained. */
8578 : return NULL_TREE;
8579 :
8580 59269938 : if (TREE_CODE (type) == ARRAY_TYPE)
8581 : {
8582 1019025 : if (check_array_initializer (decl, type, init))
8583 : return NULL_TREE;
8584 : }
8585 58250913 : else if (!COMPLETE_TYPE_P (type))
8586 : {
8587 37 : error_at (DECL_SOURCE_LOCATION (decl),
8588 : "%q#D has incomplete type", decl);
8589 37 : TREE_TYPE (decl) = error_mark_node;
8590 37 : return NULL_TREE;
8591 : }
8592 : else
8593 : /* There is no way to make a variable-sized class type in GNU C++. */
8594 58250876 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8595 :
8596 59269892 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8597 : {
8598 2971313 : int init_len = CONSTRUCTOR_NELTS (init);
8599 2971313 : if (SCALAR_TYPE_P (type))
8600 : {
8601 76047 : if (init_len == 0)
8602 : {
8603 42596 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8604 42596 : init = build_zero_init (type, NULL_TREE, false);
8605 : }
8606 33451 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8607 : {
8608 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8609 : "scalar object %qD requires one element in "
8610 : "initializer", decl);
8611 6 : TREE_TYPE (decl) = error_mark_node;
8612 6 : return NULL_TREE;
8613 : }
8614 : }
8615 : }
8616 :
8617 59269886 : if (TREE_CODE (decl) == CONST_DECL)
8618 : {
8619 0 : gcc_assert (!TYPE_REF_P (type));
8620 :
8621 0 : DECL_INITIAL (decl) = init;
8622 :
8623 0 : gcc_assert (init != NULL_TREE);
8624 : init = NULL_TREE;
8625 : }
8626 59269886 : else if (!init && DECL_REALLY_EXTERN (decl))
8627 : ;
8628 59269886 : else if (flag_openmp
8629 190436 : && VAR_P (decl)
8630 190436 : && DECL_LANG_SPECIFIC (decl)
8631 111296 : && DECL_OMP_DECLARE_MAPPER_P (decl)
8632 59269886 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8633 : return NULL_TREE;
8634 4390445 : else if (init || type_build_ctor_call (type)
8635 63050001 : || TYPE_REF_P (type))
8636 : {
8637 55489786 : if (TYPE_REF_P (type))
8638 : {
8639 386027 : init = grok_reference_init (decl, type, init, flags);
8640 386027 : flags |= LOOKUP_ALREADY_DIGESTED;
8641 : }
8642 55103759 : else if (!init)
8643 610330 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8644 : tf_warning_or_error);
8645 : /* Do not reshape constructors of vectors (they don't need to be
8646 : reshaped. */
8647 54493429 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8648 : {
8649 2928330 : if (is_std_init_list (type))
8650 : {
8651 410 : init = perform_implicit_conversion (type, init,
8652 : tf_warning_or_error);
8653 410 : flags |= LOOKUP_ALREADY_DIGESTED;
8654 : }
8655 2927920 : else if (TYPE_NON_AGGREGATE_CLASS (type))
8656 : {
8657 : /* Don't reshape if the class has constructors. */
8658 454745 : if (cxx_dialect == cxx98)
8659 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8660 : "in C++98 %qD must be initialized by "
8661 : "constructor, not by %<{...}%>",
8662 : decl);
8663 : }
8664 2473175 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8665 : {
8666 0 : error ("opaque vector types cannot be initialized");
8667 0 : init = error_mark_node;
8668 : }
8669 : else
8670 : {
8671 2473175 : init = reshape_init (type, init, tf_warning_or_error);
8672 2473175 : flags |= LOOKUP_NO_NARROWING;
8673 : }
8674 : }
8675 : /* [dcl.init] "Otherwise, if the destination type is an array, the object
8676 : is initialized as follows..." So handle things like
8677 :
8678 : int a[](1, 2, 3);
8679 :
8680 : which is permitted in C++20 by P0960. */
8681 51565099 : else if (TREE_CODE (init) == TREE_LIST
8682 778816 : && TREE_TYPE (init) == NULL_TREE
8683 778816 : && TREE_CODE (type) == ARRAY_TYPE
8684 182 : && !DECL_DECOMPOSITION_P (decl)
8685 51565251 : && (cxx_dialect >= cxx20))
8686 142 : init = do_aggregate_paren_init (init, type);
8687 51564957 : else if (TREE_CODE (init) == TREE_LIST
8688 778674 : && TREE_TYPE (init) != unknown_type_node
8689 52343631 : && !MAYBE_CLASS_TYPE_P (type))
8690 : {
8691 133533 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8692 :
8693 : /* We get here with code like `int a (2);' */
8694 133533 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8695 : tf_warning_or_error);
8696 : }
8697 :
8698 : /* If DECL has an array type without a specific bound, deduce the
8699 : array size from the initializer. */
8700 55489786 : maybe_deduce_size_from_array_init (decl, init);
8701 55489786 : type = TREE_TYPE (decl);
8702 55489786 : if (type == error_mark_node)
8703 : return NULL_TREE;
8704 :
8705 107436386 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8706 5495181 : && !(flags & LOOKUP_ALREADY_DIGESTED)
8707 8170964 : && !(init
8708 4884444 : && BRACE_ENCLOSED_INITIALIZER_P (init)
8709 5352386 : && CP_AGGREGATE_TYPE_P (type)))
8710 107706241 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8711 : {
8712 3283720 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8713 :
8714 : /* A constructor call is a non-trivial initializer even if
8715 : it isn't explicitly written. */
8716 3283720 : if (TREE_SIDE_EFFECTS (init_code))
8717 3260058 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8718 :
8719 : /* If this is a constexpr initializer, expand_default_init will
8720 : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8721 : case, pull the initializer back out and pass it down into
8722 : store_init_value. */
8723 8732590 : while (true)
8724 : {
8725 8732590 : if (TREE_CODE (init_code) == EXPR_STMT
8726 5483938 : || TREE_CODE (init_code) == STMT_EXPR
8727 5446011 : || TREE_CODE (init_code) == CONVERT_EXPR)
8728 5411106 : init_code = TREE_OPERAND (init_code, 0);
8729 3321484 : else if (TREE_CODE (init_code) == BIND_EXPR)
8730 37764 : init_code = BIND_EXPR_BODY (init_code);
8731 : else
8732 : break;
8733 : }
8734 3283720 : if (TREE_CODE (init_code) == INIT_EXPR)
8735 : {
8736 : /* In C++20, the call to build_aggr_init could have created
8737 : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8738 : A(1, 2). */
8739 2154143 : init = TREE_OPERAND (init_code, 1);
8740 2154143 : init_code = NULL_TREE;
8741 : /* Don't call digest_init; it's unnecessary and will complain
8742 : about aggregate initialization of non-aggregate classes. */
8743 2154143 : flags |= LOOKUP_ALREADY_DIGESTED;
8744 : }
8745 1129577 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8746 2239951 : || DECL_DECLARED_CONSTINIT_P (decl))
8747 : {
8748 : /* Declared constexpr or constinit, but no suitable initializer;
8749 : massage init appropriately so we can pass it into
8750 : store_init_value for the error. */
8751 19221 : tree new_init = NULL_TREE;
8752 19221 : if (!processing_template_decl
8753 271 : && TREE_CODE (init_code) == CALL_EXPR)
8754 232 : new_init = build_cplus_new (type, init_code, tf_none);
8755 18989 : else if (CLASS_TYPE_P (type)
8756 37978 : && (!init || TREE_CODE (init) == TREE_LIST))
8757 18813 : new_init = build_functional_cast (input_location, type,
8758 : init, tf_none);
8759 19045 : if (new_init)
8760 : {
8761 19045 : init = new_init;
8762 19045 : if (TREE_CODE (init) == TARGET_EXPR
8763 261 : && !(flags & LOOKUP_ONLYCONVERTING))
8764 261 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8765 : }
8766 : init_code = NULL_TREE;
8767 : }
8768 : else
8769 : init = NULL_TREE;
8770 : }
8771 :
8772 54379426 : if (init && TREE_CODE (init) != TREE_VEC)
8773 : {
8774 54379411 : init_code = store_init_value (decl, init, cleanups, flags);
8775 :
8776 54376714 : if (DECL_INITIAL (decl)
8777 39485937 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8778 57539594 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8779 : {
8780 1396557 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8781 1396557 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8782 1396557 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8783 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8784 : }
8785 :
8786 2614682 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8787 66120 : && DECL_INITIAL (decl)
8788 65377 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8789 54437749 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8790 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8791 3 : DECL_SOURCE_LOCATION (decl)),
8792 3 : 0, "array %qD initialized by parenthesized "
8793 : "string literal %qE",
8794 3 : decl, DECL_INITIAL (decl));
8795 : init = NULL_TREE;
8796 : }
8797 : }
8798 3780100 : else if (!init && REFLECTION_TYPE_P (type))
8799 : {
8800 : /* [dcl.init.general]: To default-initialize an object of type
8801 : std::meta::info means that the object is zero-initialized. */
8802 154 : DECL_INITIAL (decl)
8803 154 : = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
8804 154 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8805 154 : TREE_CONSTANT (decl) = true;
8806 154 : init = NULL_TREE;
8807 : }
8808 : else
8809 : {
8810 7559892 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8811 4604412 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8812 824451 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8813 21 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8814 : /*complain=*/true);
8815 :
8816 3779946 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8817 : tf_warning_or_error);
8818 : }
8819 :
8820 157 : if (init && init != error_mark_node)
8821 0 : init_code = cp_build_init_expr (decl, init);
8822 :
8823 16158019 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8824 59326848 : && init_code != error_mark_node)
8825 : init_code = NULL_TREE;
8826 :
8827 59209807 : if (init_code)
8828 : {
8829 : /* We might have set these in cp_finish_decl. */
8830 16100641 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8831 16100641 : TREE_CONSTANT (decl) = false;
8832 : }
8833 :
8834 16100641 : if (init_code
8835 16100641 : && DECL_IN_AGGR_P (decl)
8836 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8837 : {
8838 0 : static int explained = 0;
8839 :
8840 0 : auto_diagnostic_group d;
8841 0 : if (cxx_dialect < cxx11)
8842 0 : error ("initializer invalid for static member with constructor");
8843 0 : else if (cxx_dialect < cxx17)
8844 0 : error ("non-constant in-class initialization invalid for static "
8845 : "member %qD", decl);
8846 : else
8847 0 : error ("non-constant in-class initialization invalid for non-inline "
8848 : "static member %qD", decl);
8849 0 : if (!explained)
8850 : {
8851 0 : inform (input_location,
8852 : "(an out of class initialization is required)");
8853 0 : explained = 1;
8854 : }
8855 0 : return NULL_TREE;
8856 0 : }
8857 :
8858 : return init_code;
8859 : }
8860 :
8861 : /* If DECL is not a local variable, give it RTL. */
8862 :
8863 : static void
8864 125961372 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8865 : {
8866 125961372 : int toplev = toplevel_bindings_p ();
8867 125961372 : int defer_p;
8868 :
8869 : /* Set the DECL_ASSEMBLER_NAME for the object. */
8870 125961372 : if (asmspec)
8871 : {
8872 : /* The `register' keyword, when used together with an
8873 : asm-specification, indicates that the variable should be
8874 : placed in a particular register. */
8875 1044723 : if (VAR_P (decl) && DECL_REGISTER (decl))
8876 : {
8877 87 : if (TREE_ADDRESSABLE (decl))
8878 3 : error_at (DECL_SOURCE_LOCATION (decl),
8879 : "address of explicit register variable %qD requested",
8880 : decl);
8881 : else
8882 : {
8883 84 : set_user_assembler_name (decl, asmspec);
8884 84 : DECL_HARD_REGISTER (decl) = 1;
8885 : }
8886 : }
8887 : else
8888 : {
8889 1044636 : if (TREE_CODE (decl) == FUNCTION_DECL
8890 1044636 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8891 83742 : set_builtin_user_assembler_name (decl, asmspec);
8892 1044636 : set_user_assembler_name (decl, asmspec);
8893 1044636 : if (DECL_LOCAL_DECL_P (decl))
8894 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8895 : /* We have to propagate the name to the ns-alias.
8896 : This is horrible, as we're affecting a
8897 : possibly-shared decl. Again, a one-true-decl
8898 : model breaks down. */
8899 6 : if (ns_decl != error_mark_node)
8900 6 : set_user_assembler_name (ns_decl, asmspec);
8901 : }
8902 : }
8903 :
8904 : /* Handle non-variables up front. */
8905 125961372 : if (!VAR_P (decl))
8906 : {
8907 59842069 : rest_of_decl_compilation (decl, toplev, at_eof);
8908 59842069 : return;
8909 : }
8910 :
8911 : /* If we see a class member here, it should be a static data
8912 : member. */
8913 66119303 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8914 : {
8915 5728566 : gcc_assert (TREE_STATIC (decl));
8916 : /* An in-class declaration of a static data member should be
8917 : external; it is only a declaration, and not a definition. */
8918 5728566 : if (init == NULL_TREE)
8919 5728541 : gcc_assert (DECL_EXTERNAL (decl)
8920 : || !TREE_PUBLIC (decl));
8921 : }
8922 :
8923 : /* We don't create any RTL for local variables. */
8924 66119303 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8925 : return;
8926 :
8927 : /* Don't output reflection variables. */
8928 40774279 : if (consteval_only_p (decl))
8929 : {
8930 : /* Disable assemble_variable. */
8931 1834 : DECL_EXTERNAL (decl) = true;
8932 : /* Undo make_decl_one_only. */
8933 1834 : if (DECL_COMDAT_GROUP (decl))
8934 : {
8935 326 : symtab_node *node = symtab_node::get (decl);
8936 326 : node->set_comdat_group (NULL);
8937 326 : node->dissolve_same_comdat_group_list ();
8938 : }
8939 : return;
8940 : }
8941 :
8942 : /* We defer emission of local statics until the corresponding
8943 : DECL_EXPR is expanded. But with constexpr its function might never
8944 : be expanded, so go ahead and tell cgraph about the variable now. */
8945 81544890 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8946 142329 : && !var_in_maybe_constexpr_fn (decl))
8947 81407240 : || DECL_VIRTUAL_P (decl));
8948 :
8949 : /* Defer template instantiations. */
8950 40772445 : if (DECL_LANG_SPECIFIC (decl)
8951 40772445 : && DECL_IMPLICIT_INSTANTIATION (decl))
8952 : defer_p = 1;
8953 :
8954 : /* If we're not deferring, go ahead and assemble the variable. */
8955 20863367 : if (!defer_p)
8956 18418072 : rest_of_decl_compilation (decl, toplev, at_eof);
8957 : }
8958 :
8959 : /* walk_tree helper for wrap_temporary_cleanups, below. */
8960 :
8961 : static tree
8962 11545999 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8963 : {
8964 : /* Stop at types or full-expression boundaries. */
8965 11545999 : if (TYPE_P (*stmt_p)
8966 11545773 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8967 : {
8968 486 : *walk_subtrees = 0;
8969 486 : return NULL_TREE;
8970 : }
8971 :
8972 11545513 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8973 : {
8974 250118 : tree guard = (tree)data;
8975 250118 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8976 :
8977 124402 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8978 370523 : && !expr_noexcept_p (tcleanup, tf_none))
8979 : {
8980 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8981 : /* Tell honor_protect_cleanup_actions to handle this as a separate
8982 : cleanup. */
8983 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8984 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8985 : }
8986 : }
8987 :
8988 : return NULL_TREE;
8989 : }
8990 :
8991 : /* We're initializing a local variable which has a cleanup GUARD. If there
8992 : are any temporaries used in the initializer INIT of this variable, we
8993 : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8994 : variable will be cleaned up properly if one of them throws.
8995 :
8996 : Unfortunately, there's no way to express this properly in terms of
8997 : nesting, as the regions for the temporaries overlap the region for the
8998 : variable itself; if there are two temporaries, the variable needs to be
8999 : the first thing destroyed if either of the temporary destructors throws.
9000 : However, we only want to run the variable's cleanup if it actually got
9001 : constructed. So we need to guard the temporary cleanups with the
9002 : variable's cleanup if they are run on the normal path, but not if they
9003 : are run on the exceptional path. We implement this by telling
9004 : honor_protect_cleanup_actions to strip the variable cleanup from the
9005 : exceptional path.
9006 :
9007 : Another approach could be to make the variable cleanup region enclose
9008 : initialization, but depend on a flag to indicate that the variable is
9009 : initialized; that's effectively what we do for arrays. But the current
9010 : approach works fine for non-arrays, and has no code overhead in the usual
9011 : case where the temporary destructors are noexcept. */
9012 :
9013 : static void
9014 1043191 : wrap_temporary_cleanups (tree init, tree guard)
9015 : {
9016 1043191 : if (TREE_CODE (guard) == BIND_EXPR)
9017 : {
9018 : /* An array cleanup region already encloses any temporary cleanups,
9019 : don't wrap it around them again. */
9020 422 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
9021 : return;
9022 : }
9023 1042769 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
9024 : }
9025 :
9026 : /* Generate code to initialize DECL (a local variable). */
9027 :
9028 : static void
9029 25344997 : initialize_local_var (tree decl, tree init, bool decomp)
9030 : {
9031 25344997 : tree type = TREE_TYPE (decl);
9032 25344997 : tree cleanup;
9033 25344997 : int already_used;
9034 :
9035 25344997 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
9036 25344997 : gcc_assert (!TREE_STATIC (decl));
9037 :
9038 25344997 : if (DECL_SIZE (decl) == NULL_TREE)
9039 : {
9040 : /* If we used it already as memory, it must stay in memory. */
9041 0 : DECL_INITIAL (decl) = NULL_TREE;
9042 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
9043 0 : return;
9044 : }
9045 :
9046 25344997 : if (type == error_mark_node)
9047 : return;
9048 :
9049 : /* Compute and store the initial value. */
9050 25344997 : already_used = TREE_USED (decl) || TREE_USED (type);
9051 25344997 : if (TREE_USED (type))
9052 32 : DECL_READ_P (decl) = 1;
9053 :
9054 : /* Generate a cleanup, if necessary. */
9055 25344997 : cleanup = (decomp ? NULL_TREE
9056 25309466 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
9057 :
9058 : /* Perform the initialization. */
9059 25344997 : if (init)
9060 : {
9061 16086163 : tree rinit = (TREE_CODE (init) == INIT_EXPR
9062 16086163 : ? TREE_OPERAND (init, 1) : NULL_TREE);
9063 14683459 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
9064 22739515 : && TREE_OPERAND (init, 0) == decl)
9065 : {
9066 : /* Stick simple initializers in DECL_INITIAL so that
9067 : -Wno-init-self works (c++/34772). */
9068 8056056 : DECL_INITIAL (decl) = rinit;
9069 :
9070 8056056 : if (warn_init_self && TYPE_REF_P (type))
9071 : {
9072 2469 : STRIP_NOPS (rinit);
9073 2469 : if (rinit == decl)
9074 3 : warning_at (DECL_SOURCE_LOCATION (decl),
9075 3 : OPT_Winit_self,
9076 : "reference %qD is initialized with itself", decl);
9077 : }
9078 : }
9079 : else
9080 : {
9081 8030107 : int saved_stmts_are_full_exprs_p;
9082 :
9083 : /* If we're only initializing a single object, guard the
9084 : destructors of any temporaries used in its initializer with
9085 : its destructor. */
9086 8030107 : if (cleanup)
9087 1039764 : wrap_temporary_cleanups (init, cleanup);
9088 :
9089 8030107 : gcc_assert (building_stmt_list_p ());
9090 8030107 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
9091 : /* Avoid CLEANUP_POINT_EXPR for the structured binding
9092 : bases, those will have CLEANUP_POINT_EXPR at the end of
9093 : code emitted by cp_finish_decomp. */
9094 8030107 : if (decomp)
9095 35031 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
9096 : else
9097 7995076 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
9098 8030107 : finish_expr_stmt (init);
9099 8030107 : current_stmt_tree ()->stmts_are_full_exprs_p
9100 8030107 : = saved_stmts_are_full_exprs_p;
9101 : }
9102 : }
9103 :
9104 : /* Set this to 0 so we can tell whether an aggregate which was
9105 : initialized was ever used. Don't do this if it has a
9106 : destructor, so we don't complain about the 'resource
9107 : allocation is initialization' idiom. Now set
9108 : attribute((unused)) on types so decls of that type will be
9109 : marked used. (see TREE_USED, above.) */
9110 25344997 : if (TYPE_NEEDS_CONSTRUCTING (type)
9111 2263777 : && ! already_used
9112 25352 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
9113 25367494 : && DECL_NAME (decl))
9114 22449 : TREE_USED (decl) = 0;
9115 25322548 : else if (already_used)
9116 22252722 : TREE_USED (decl) = 1;
9117 :
9118 25344997 : if (cleanup)
9119 1053890 : finish_decl_cleanup (decl, cleanup);
9120 : }
9121 :
9122 : /* DECL is a VAR_DECL for a compiler-generated variable with static
9123 : storage duration (like a virtual table) whose initializer is a
9124 : compile-time constant. Initialize the variable and provide it to the
9125 : back end. */
9126 :
9127 : void
9128 2266227 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
9129 : {
9130 2266227 : tree init;
9131 2266227 : gcc_assert (DECL_ARTIFICIAL (decl));
9132 2266227 : init = build_constructor (TREE_TYPE (decl), v);
9133 2266227 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
9134 2266227 : DECL_INITIAL (decl) = init;
9135 2266227 : DECL_INITIALIZED_P (decl) = 1;
9136 : /* Mark the decl as constexpr so that we can access its content
9137 : at compile time. */
9138 2266227 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9139 2266227 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
9140 2266227 : determine_visibility (decl);
9141 2266227 : layout_var_decl (decl);
9142 2266227 : maybe_commonize_var (decl);
9143 2266227 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
9144 2266227 : }
9145 :
9146 : /* INIT is the initializer for a variable, as represented by the
9147 : parser. Returns true iff INIT is value-dependent. */
9148 :
9149 : static bool
9150 10218193 : value_dependent_init_p (tree init)
9151 : {
9152 10218193 : if (TREE_CODE (init) == TREE_LIST)
9153 : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
9154 21674 : return any_value_dependent_elements_p (init);
9155 10196519 : else if (TREE_CODE (init) == CONSTRUCTOR)
9156 : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
9157 : {
9158 74159 : if (dependent_type_p (TREE_TYPE (init)))
9159 : return true;
9160 :
9161 74153 : vec<constructor_elt, va_gc> *elts;
9162 74153 : size_t nelts;
9163 74153 : size_t i;
9164 :
9165 74153 : elts = CONSTRUCTOR_ELTS (init);
9166 74153 : nelts = vec_safe_length (elts);
9167 653564 : for (i = 0; i < nelts; ++i)
9168 608855 : if (value_dependent_init_p ((*elts)[i].value))
9169 : return true;
9170 : }
9171 : else
9172 : /* It must be a simple expression, e.g., int i = 3; */
9173 10122360 : return value_dependent_expression_p (init);
9174 :
9175 : return false;
9176 : }
9177 :
9178 : /* A helper function to be called via walk_tree. If any label exists
9179 : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
9180 :
9181 : static tree
9182 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
9183 : {
9184 337 : if (TYPE_P (*tp))
9185 0 : *walk_subtrees = 0;
9186 337 : if (TREE_CODE (*tp) == LABEL_DECL)
9187 6 : cfun->has_forced_label_in_static = 1;
9188 337 : return NULL_TREE;
9189 : }
9190 :
9191 : /* Return true if DECL has either a trivial destructor, or for C++20
9192 : is constexpr and has a constexpr destructor. */
9193 :
9194 : static bool
9195 64744765 : decl_maybe_constant_destruction (tree decl, tree type)
9196 : {
9197 64744765 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
9198 64744765 : || (cxx_dialect >= cxx20
9199 4238995 : && VAR_P (decl)
9200 4238650 : && DECL_DECLARED_CONSTEXPR_P (decl)
9201 484 : && type_has_constexpr_destructor (strip_array_types (type))));
9202 : }
9203 :
9204 : static tree declare_simd_adjust_this (tree *, int *, void *);
9205 :
9206 : /* Helper function of omp_declare_variant_finalize. Finalize one
9207 : "omp declare variant base" attribute. Return true if it should be
9208 : removed. */
9209 :
9210 : static bool
9211 1755 : omp_declare_variant_finalize_one (tree decl, tree attr)
9212 : {
9213 1755 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9214 : {
9215 164 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
9216 : DECL_ARGUMENTS (decl), NULL);
9217 164 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
9218 : DECL_ARGUMENTS (decl), NULL);
9219 : }
9220 :
9221 1755 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
9222 1755 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
9223 : OMP_TRAIT_CONSTRUCT_SIMD);
9224 1755 : if (simd)
9225 : {
9226 51 : TREE_VALUE (simd)
9227 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
9228 51 : OMP_TS_PROPERTIES (simd));
9229 : /* FIXME, adjusting simd args unimplemented. */
9230 51 : return true;
9231 : }
9232 :
9233 1704 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
9234 1704 : location_t varid_loc
9235 1704 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
9236 1704 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
9237 1704 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
9238 1704 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
9239 :
9240 1704 : location_t save_loc = input_location;
9241 1704 : input_location = varid_loc;
9242 :
9243 1704 : releasing_vec args;
9244 1704 : tree parm = DECL_ARGUMENTS (decl);
9245 1704 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9246 164 : parm = DECL_CHAIN (parm);
9247 3026 : for (; parm; parm = DECL_CHAIN (parm))
9248 1322 : vec_safe_push (args, forward_parm (parm));
9249 :
9250 1704 : unsigned nappend_args = 0;
9251 1704 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9252 1704 : if (append_args_list)
9253 : {
9254 354 : append_args_list = TREE_VALUE (append_args_list);
9255 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9256 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
9257 : : NULL_TREE);
9258 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9259 387 : nappend_args++;
9260 354 : if (nappend_args)
9261 : {
9262 237 : tree type;
9263 237 : if ((type = lookup_qualified_name (global_namespace,
9264 : "omp_interop_t",
9265 : LOOK_want::NORMAL,
9266 : /*complain*/false)) == NULL_TREE
9267 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
9268 : {
9269 24 : location_t loc = input_location;
9270 24 : variant = tree_strip_any_location_wrapper (variant);
9271 24 : if (!identifier_p (variant))
9272 : {
9273 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9274 21 : variant = OVL_FIRST (variant);
9275 21 : loc = EXPR_LOC_OR_LOC (variant,
9276 : DECL_SOURCE_LOCATION (variant));
9277 : }
9278 24 : auto_diagnostic_group d;
9279 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9280 24 : args->length () + 1, variant);
9281 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9282 : "%<append_args%> specified here");
9283 24 : return true;
9284 24 : }
9285 564 : for (unsigned i = 0; i < nappend_args; i++)
9286 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
9287 : }
9288 : }
9289 :
9290 1680 : bool koenig_p = false;
9291 1680 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9292 : {
9293 1610 : if (identifier_p (variant)
9294 : /* In C++20, we may need to perform ADL for a template
9295 : name. */
9296 1586 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9297 18 : && identifier_p (TREE_OPERAND (variant, 0))))
9298 : {
9299 24 : if (!args->is_empty ())
9300 : {
9301 24 : koenig_p = true;
9302 24 : if (!any_type_dependent_arguments_p (args))
9303 21 : variant = perform_koenig_lookup (variant, args,
9304 : tf_warning_or_error);
9305 : }
9306 : else
9307 0 : variant = unqualified_fn_lookup_error (variant);
9308 : }
9309 1586 : else if (!args->is_empty () && is_overloaded_fn (variant))
9310 : {
9311 830 : tree fn = get_first_fn (variant);
9312 830 : fn = STRIP_TEMPLATE (fn);
9313 830 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9314 830 : || DECL_FUNCTION_MEMBER_P (fn)
9315 717 : || DECL_LOCAL_DECL_P (fn)))
9316 : {
9317 717 : koenig_p = true;
9318 717 : if (!any_type_dependent_arguments_p (args))
9319 666 : variant = perform_koenig_lookup (variant, args,
9320 : tf_warning_or_error);
9321 : }
9322 : }
9323 : }
9324 :
9325 1680 : if (idk == CP_ID_KIND_QUALIFIED)
9326 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9327 : koenig_p, tf_warning_or_error);
9328 1674 : else if (idk == CP_ID_KIND_NONE
9329 64 : && TREE_CODE (variant) == FUNCTION_DECL
9330 64 : && DECL_IOBJ_MEMBER_FUNCTION_P (variant)
9331 1722 : && CLASS_TYPE_P (DECL_CONTEXT (decl)))
9332 : {
9333 48 : tree saved_ccp = current_class_ptr;
9334 48 : tree saved_ccr = current_class_ref;
9335 48 : current_class_ptr = NULL_TREE;
9336 48 : current_class_ref = NULL_TREE;
9337 48 : inject_this_parameter (DECL_CONTEXT (decl), TYPE_UNQUALIFIED);
9338 48 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9339 : koenig_p, tf_warning_or_error);
9340 48 : current_class_ptr = saved_ccp;
9341 48 : current_class_ref = saved_ccr;
9342 : }
9343 : else
9344 1626 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9345 : koenig_p, tf_warning_or_error);
9346 1680 : if (variant == error_mark_node && !processing_template_decl)
9347 : return true;
9348 :
9349 1614 : if (TREE_CODE (variant) == TARGET_EXPR)
9350 9 : variant = TARGET_EXPR_INITIAL (variant);
9351 :
9352 3219 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9353 1614 : input_location = save_loc;
9354 :
9355 1614 : if (variant == decl)
9356 : {
9357 0 : error_at (varid_loc, "variant %qD is the same as base function",
9358 : variant);
9359 0 : return true;
9360 : }
9361 :
9362 1614 : if (variant)
9363 : {
9364 1530 : bool fail;
9365 1530 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9366 1530 : if (!nappend_args)
9367 1371 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9368 : COMPARE_STRICT);
9369 : else
9370 : {
9371 159 : unsigned nbase_args = 0;
9372 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9373 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9374 240 : nbase_args++;
9375 159 : tree vargs, varg;
9376 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9377 399 : for (unsigned i = 0; i < nbase_args && varg;
9378 240 : i++, varg = TREE_CHAIN (varg))
9379 240 : vargs = varg;
9380 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
9381 258 : varg = TREE_CHAIN (varg);
9382 159 : tree saved_vargs;
9383 159 : int saved_no_named_args_stdarg = 0;
9384 159 : if (nbase_args)
9385 : {
9386 111 : saved_vargs = TREE_CHAIN (vargs);
9387 111 : TREE_CHAIN (vargs) = varg;
9388 : }
9389 : else
9390 : {
9391 48 : saved_vargs = vargs;
9392 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9393 48 : saved_no_named_args_stdarg
9394 48 : = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9395 48 : if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9396 48 : && varg == NULL_TREE)
9397 2 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9398 : }
9399 : /* Skip assert check that TYPE_CANONICAL is the same. */
9400 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9401 : COMPARE_STRUCTURAL);
9402 159 : if (nbase_args)
9403 111 : TREE_CHAIN (vargs) = saved_vargs;
9404 : else
9405 : {
9406 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9407 48 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9408 96 : = saved_no_named_args_stdarg;
9409 : }
9410 159 : varg = saved_vargs;
9411 159 : if (!fail && !processing_template_decl)
9412 402 : for (unsigned i = 0; i < nappend_args;
9413 246 : i++, varg = TREE_CHAIN (varg))
9414 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9415 : {
9416 9 : auto_diagnostic_group d;
9417 9 : error_at (DECL_SOURCE_LOCATION (variant),
9418 : "argument %d of %qD must be of %<omp_interop_t%>",
9419 9 : nbase_args + i + 1, variant);
9420 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9421 : "%<append_args%> specified here");
9422 9 : break;
9423 9 : }
9424 : }
9425 1530 : if (fail)
9426 : {
9427 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
9428 : "types", variant, decl);
9429 69 : return true;
9430 : }
9431 1461 : if (fndecl_built_in_p (variant)
9432 1461 : && (startswith (varname, "__builtin_")
9433 0 : || startswith (varname, "__sync_")
9434 0 : || startswith (varname, "__atomic_")))
9435 : {
9436 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
9437 6 : return true;
9438 : }
9439 : else
9440 : {
9441 1455 : tree construct
9442 1455 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9443 1455 : omp_mark_declare_variant (match_loc, variant, construct);
9444 1455 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9445 : return true;
9446 1039 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9447 :
9448 : // Prepend adjust_args list to variant attributes
9449 1039 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9450 1039 : if (adjust_args_list != NULL_TREE)
9451 : {
9452 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
9453 285 : && TREE_VALUE (adjust_args_list))
9454 : {
9455 : /* Shift arg position for the added 'this' pointer. */
9456 : /* Handle need_device_ptr */
9457 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9458 51 : t; t = TREE_CHAIN (t))
9459 30 : TREE_VALUE (t)
9460 30 : = build_int_cst (TREE_TYPE (t),
9461 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
9462 : }
9463 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9464 : {
9465 : /* Shift likewise the number of args after which the
9466 : interop object should be added. */
9467 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9468 6 : TREE_PURPOSE (nargs)
9469 6 : = build_int_cst (TREE_TYPE (nargs),
9470 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9471 : }
9472 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9473 264 : TREE_VALUE (t)
9474 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9475 528 : DECL_ATTRIBUTES (variant) = tree_cons (
9476 : get_identifier ("omp declare variant variant args"),
9477 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9478 : }
9479 : }
9480 : }
9481 84 : else if (!processing_template_decl)
9482 : {
9483 0 : error_at (varid_loc, "could not find variant declaration");
9484 0 : return true;
9485 : }
9486 :
9487 : return false;
9488 1704 : }
9489 :
9490 : /* Helper function, finish up "omp declare variant base" attribute
9491 : now that there is a DECL. ATTR is the first "omp declare variant base"
9492 : attribute. */
9493 :
9494 : void
9495 1548 : omp_declare_variant_finalize (tree decl, tree attr)
9496 : {
9497 1548 : size_t attr_len = strlen ("omp declare variant base");
9498 1548 : tree *list = &DECL_ATTRIBUTES (decl);
9499 1548 : bool remove_all = false;
9500 1548 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
9501 1548 : if (TREE_CHAIN (TREE_VALUE (attr))
9502 1548 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9503 3096 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9504 1548 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9505 3096 : if (DECL_CONSTRUCTOR_P (decl))
9506 : {
9507 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9508 27 : remove_all = true;
9509 : }
9510 1521 : else if (DECL_DESTRUCTOR_P (decl))
9511 : {
9512 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9513 9 : remove_all = true;
9514 : }
9515 1512 : else if (DECL_DEFAULTED_FN (decl))
9516 : {
9517 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9518 2 : remove_all = true;
9519 : }
9520 1510 : else if (DECL_DELETED_FN (decl))
9521 : {
9522 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9523 2 : remove_all = true;
9524 : }
9525 1508 : else if (DECL_VIRTUAL_P (decl))
9526 : {
9527 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9528 0 : remove_all = true;
9529 : }
9530 : /* This loop is like private_lookup_attribute, except that it works
9531 : with tree * rather than tree, as we might want to remove the
9532 : attributes that are diagnosed as erroneous. */
9533 3355 : while (*list)
9534 : {
9535 1807 : tree attr = get_attribute_name (*list);
9536 1807 : size_t ident_len = IDENTIFIER_LENGTH (attr);
9537 4737 : if (cmp_attribs ("omp declare variant base", attr_len,
9538 1807 : IDENTIFIER_POINTER (attr), ident_len))
9539 : {
9540 1795 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
9541 : {
9542 672 : *list = TREE_CHAIN (*list);
9543 672 : continue;
9544 : }
9545 : }
9546 1135 : list = &TREE_CHAIN (*list);
9547 : }
9548 1548 : }
9549 :
9550 : /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
9551 : have a potentially throwing exception specification. */
9552 :
9553 : void
9554 211087918 : maybe_diagnose_deallocation_noexcept_false (tree decl)
9555 : {
9556 211087918 : if (cxx_dialect >= cxx29
9557 16599286 : && DECL_NAME (decl)
9558 16599286 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
9559 211127861 : && !IDENTIFIER_NEW_OP_P (DECL_NAME (decl)))
9560 : {
9561 21925 : tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
9562 21925 : if (spec && spec == noexcept_false_spec)
9563 15 : error_at (DECL_SOURCE_LOCATION (decl),
9564 : "deallocation function %qD declared possibly throwing",
9565 : decl);
9566 : }
9567 211087918 : }
9568 :
9569 : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9570 :
9571 : /* Finish processing of a declaration;
9572 : install its line number and initial value.
9573 : If the length of an array type is not known before,
9574 : it must be determined now, from the initial value, or it is an error.
9575 :
9576 : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9577 : true, then INIT is an integral constant expression.
9578 :
9579 : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9580 : if the (init) syntax was used.
9581 :
9582 : DECOMP is first identifier's DECL and identifier count in a structured
9583 : bindings, nullptr if not a structured binding. */
9584 :
9585 : void
9586 339910250 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9587 : tree asmspec_tree, int flags, cp_decomp *decomp)
9588 : {
9589 339910250 : vec<tree, va_gc> *cleanups = NULL;
9590 339910250 : const char *asmspec = NULL;
9591 339910250 : int was_readonly = 0;
9592 339910250 : bool var_definition_p = false;
9593 339910250 : tree auto_node;
9594 339910250 : auto_vec<tree> extra_cleanups;
9595 339910250 : tree aggregates1 = NULL_TREE;
9596 339910250 : struct decomp_cleanup {
9597 : tree decl;
9598 : cp_decomp *&decomp;
9599 339907553 : ~decomp_cleanup ()
9600 : {
9601 339907553 : if (decomp && DECL_DECOMPOSITION_P (decl))
9602 239093 : cp_finish_decomp (decl, decomp);
9603 339907553 : }
9604 339910250 : } decomp_cl = { decl, decomp };
9605 :
9606 339910250 : if (decl == error_mark_node)
9607 : return;
9608 339910210 : else if (! decl)
9609 : {
9610 0 : if (init)
9611 0 : error ("assignment (not initialization) in declaration");
9612 : return;
9613 : }
9614 :
9615 339910210 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9616 : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9617 339910210 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
9618 :
9619 339910210 : tree type = TREE_TYPE (decl);
9620 339910210 : if (type == error_mark_node)
9621 : return;
9622 :
9623 339909585 : if (VAR_P (decl) && is_copy_initialization (init))
9624 101396002 : flags |= LOOKUP_ONLYCONVERTING;
9625 :
9626 : /* Warn about register storage specifiers except when in GNU global
9627 : or local register variable extension. */
9628 339909585 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9629 : {
9630 1931 : if (cxx_dialect >= cxx17)
9631 1282 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9632 : "ISO C++17 does not allow %<register%> storage "
9633 : "class specifier");
9634 : else
9635 649 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9636 : "%<register%> storage class specifier used");
9637 : }
9638 :
9639 : /* If a name was specified, get the string. */
9640 339909585 : if (at_namespace_scope_p ())
9641 73468727 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9642 339909585 : if (asmspec_tree && asmspec_tree != error_mark_node)
9643 1044732 : asmspec = TREE_STRING_POINTER (asmspec_tree);
9644 :
9645 339909585 : bool in_class_decl
9646 339909585 : = (current_class_type
9647 214520298 : && CP_DECL_CONTEXT (decl) == current_class_type
9648 142337259 : && TYPE_BEING_DEFINED (current_class_type)
9649 480088618 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9650 :
9651 134878602 : if (in_class_decl
9652 134878602 : && (DECL_INITIAL (decl) || init))
9653 98982026 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9654 :
9655 339909585 : if (VAR_P (decl)
9656 339909585 : && (auto_node = type_uses_auto (type)))
9657 : {
9658 14083670 : tree d_init;
9659 14083670 : if (init == NULL_TREE)
9660 : {
9661 4598 : if (DECL_LANG_SPECIFIC (decl)
9662 4513 : && DECL_TEMPLATE_INSTANTIATION (decl)
9663 9108 : && !DECL_TEMPLATE_INSTANTIATED (decl))
9664 : {
9665 : /* init is null because we're deferring instantiating the
9666 : initializer until we need it. Well, we need it now. */
9667 4504 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9668 4504 : return;
9669 : }
9670 :
9671 94 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9672 : /* Class deduction with no initializer is OK. */;
9673 : else
9674 : {
9675 : /* Ordinary auto deduction without an initializer, a situation
9676 : which grokdeclarator already detects and rejects for the most
9677 : part. But we can still get here if we're instantiating a
9678 : variable template before we've fully parsed (and attached) its
9679 : initializer, e.g. template<class> auto x = x<int>; */
9680 3 : error_at (DECL_SOURCE_LOCATION (decl),
9681 : "declaration of %q#D has no initializer", decl);
9682 3 : TREE_TYPE (decl) = error_mark_node;
9683 3 : return;
9684 : }
9685 : }
9686 14079163 : d_init = init;
9687 14079163 : if (d_init)
9688 : {
9689 14079072 : if (TREE_CODE (d_init) == TREE_LIST
9690 14153673 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9691 47929 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9692 : tf_warning_or_error);
9693 14079072 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9694 : /* Force auto deduction now. Use tf_none to avoid redundant warnings
9695 : on deprecated-14.C. */
9696 14079072 : mark_single_function (d_init, tf_none);
9697 : }
9698 14079163 : enum auto_deduction_context adc = adc_variable_type;
9699 14079163 : if (DECL_DECOMPOSITION_P (decl))
9700 : adc = adc_decomp_type;
9701 14079163 : tree outer_targs = NULL_TREE;
9702 14079163 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9703 325 : && DECL_LANG_SPECIFIC (decl)
9704 126 : && DECL_TEMPLATE_INFO (decl)
9705 14079220 : && !DECL_FUNCTION_SCOPE_P (decl))
9706 : /* The outer template arguments might be needed for satisfaction.
9707 : (For function scope variables, do_auto_deduction will obtain the
9708 : outer template arguments from current_function_decl.) */
9709 57 : outer_targs = DECL_TI_ARGS (decl);
9710 14079163 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9711 : tf_warning_or_error, adc,
9712 : outer_targs, flags);
9713 14079163 : if (type == error_mark_node)
9714 : return;
9715 14077999 : if (TREE_CODE (type) == FUNCTION_TYPE)
9716 : {
9717 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
9718 : "did you forget the %<()%>?", decl);
9719 3 : TREE_TYPE (decl) = error_mark_node;
9720 3 : return;
9721 : }
9722 : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9723 14077996 : if (!processing_template_decl
9724 3183071 : && !type_uses_auto (type)
9725 17261060 : && !COMPLETE_TYPE_P (complete_type (type)))
9726 : {
9727 18 : auto_diagnostic_group d;
9728 18 : error_at (location_of (decl),
9729 : "deduced type %qT for %qD is incomplete", type, decl);
9730 18 : cxx_incomplete_type_inform (type);
9731 18 : TREE_TYPE (decl) = error_mark_node;
9732 18 : return;
9733 18 : }
9734 :
9735 : /* Now that we have a type, try these again. */
9736 14077978 : layout_decl (decl, 0);
9737 14077978 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9738 :
9739 : /* Update the type of the corresponding TEMPLATE_DECL to match. */
9740 14077978 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9741 : {
9742 40753 : tree tmpl = template_for_substitution (decl);
9743 40753 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9744 34241 : TREE_TYPE (tmpl) = type;
9745 : }
9746 : }
9747 :
9748 339903893 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9749 : {
9750 65 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9751 65 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9752 : {
9753 6 : init = NULL_TREE;
9754 6 : DECL_EXTERNAL (decl) = 1;
9755 : }
9756 : }
9757 :
9758 339903893 : if (VAR_P (decl)
9759 128322615 : && DECL_CLASS_SCOPE_P (decl)
9760 20903957 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
9761 : TCTX_STATIC_STORAGE, type)
9762 360807850 : && DECL_INITIALIZED_IN_CLASS_P (decl))
9763 19502920 : check_static_variable_definition (decl, type);
9764 :
9765 339903893 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
9766 : {
9767 38510621 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9768 38510621 : ? TCTX_THREAD_STORAGE
9769 : : TCTX_STATIC_STORAGE);
9770 38510621 : verify_type_context (input_location, context, TREE_TYPE (decl));
9771 : }
9772 :
9773 339903893 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
9774 : {
9775 598135 : tree clone;
9776 598135 : if (init == ridpointers[(int)RID_DELETE]
9777 598135 : || (TREE_CODE (init) == STRING_CST
9778 36 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9779 : {
9780 : /* FIXME check this is 1st decl. */
9781 574825 : if (UNLIKELY (DECL_MAIN_P (decl)))
9782 : {
9783 : /* [basic.start.main]/3: A program that defines main as deleted
9784 : is ill-formed. */
9785 6 : error ("%<::main%> cannot be deleted");
9786 6 : DECL_INITIAL (decl) = error_mark_node;
9787 : }
9788 : else
9789 : {
9790 574819 : DECL_DELETED_FN (decl) = 1;
9791 574819 : DECL_DECLARED_INLINE_P (decl) = 1;
9792 574819 : DECL_INITIAL (decl)
9793 574819 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9794 574837 : FOR_EACH_CLONE (clone, decl)
9795 : {
9796 18 : DECL_DELETED_FN (clone) = 1;
9797 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9798 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9799 : }
9800 : }
9801 574825 : init = NULL_TREE;
9802 : }
9803 23310 : else if (init == ridpointers[(int)RID_DEFAULT])
9804 : {
9805 23298 : if (defaultable_fn_check (decl))
9806 23274 : DECL_DEFAULTED_FN (decl) = 1;
9807 : else
9808 24 : DECL_INITIAL (decl) = NULL_TREE;
9809 : }
9810 : }
9811 :
9812 339903893 : if (init && VAR_P (decl))
9813 : {
9814 111549509 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9815 : /* If DECL is a reference, then we want to know whether init is a
9816 : reference constant; init_const_expr_p as passed tells us whether
9817 : it's an rvalue constant. */
9818 111549509 : if (TYPE_REF_P (type))
9819 2393430 : init_const_expr_p = potential_constant_expression (init);
9820 111549509 : if (init_const_expr_p)
9821 : {
9822 : /* Set these flags now for templates. We'll update the flags in
9823 : store_init_value for instantiations. */
9824 98242591 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9825 98242591 : if (decl_maybe_constant_var_p (decl)
9826 : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9827 98242591 : && !TYPE_REF_P (type))
9828 43338267 : TREE_CONSTANT (decl) = true;
9829 : }
9830 : /* This is handled mostly by gimplify.cc, but we have to deal with
9831 : not warning about int x = x; as it is a GCC extension to turn off
9832 : this warning but only if warn_init_self is zero. */
9833 111549509 : if (!DECL_EXTERNAL (decl)
9834 109866643 : && !TREE_STATIC (decl)
9835 74175119 : && decl == tree_strip_any_location_wrapper (init)
9836 221416152 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9837 3760 : suppress_warning (decl, OPT_Winit_self);
9838 : }
9839 228354384 : else if (VAR_P (decl)
9840 16773106 : && COMPLETE_TYPE_P (type)
9841 13206069 : && !TYPE_REF_P (type)
9842 13205361 : && !dependent_type_p (type)
9843 241222479 : && is_really_empty_class (type, /*ignore_vptr*/false))
9844 : /* We have no initializer but there's nothing to initialize anyway.
9845 : Treat DECL as constant due to c++/109876. */
9846 602748 : TREE_CONSTANT (decl) = true;
9847 :
9848 339903893 : if (flag_openmp
9849 852564 : && TREE_CODE (decl) == FUNCTION_DECL
9850 : /* #pragma omp declare variant on methods handled in finish_struct
9851 : instead. */
9852 340273171 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9853 140078 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9854 229663 : if (tree attr = lookup_attribute ("omp declare variant base",
9855 229663 : DECL_ATTRIBUTES (decl)))
9856 970 : omp_declare_variant_finalize (decl, attr);
9857 :
9858 339903893 : if (processing_template_decl)
9859 : {
9860 195265803 : bool type_dependent_p;
9861 :
9862 : /* Add this declaration to the statement-tree. */
9863 195265803 : if (at_function_scope_p ())
9864 70263875 : add_decl_expr (decl);
9865 :
9866 195265803 : type_dependent_p = dependent_type_p (type);
9867 :
9868 195265803 : if (check_for_bare_parameter_packs (init))
9869 : {
9870 3 : init = NULL_TREE;
9871 3 : DECL_INITIAL (decl) = NULL_TREE;
9872 : }
9873 :
9874 : /* Generally, initializers in templates are expanded when the
9875 : template is instantiated. But, if DECL is a variable constant
9876 : then it can be used in future constant expressions, so its value
9877 : must be available. */
9878 :
9879 195265803 : bool dep_init = false;
9880 :
9881 195265803 : if (!VAR_P (decl) || type_dependent_p)
9882 : /* We can't do anything if the decl has dependent type. */;
9883 24408543 : else if (init
9884 22667208 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9885 18488295 : && !TYPE_REF_P (type)
9886 18453905 : && decl_maybe_constant_var_p (decl)
9887 34017881 : && !(dep_init = value_dependent_init_p (init)))
9888 : {
9889 : /* This variable seems to be a non-dependent constant, so process
9890 : its initializer. If check_initializer returns non-null the
9891 : initialization wasn't constant after all. */
9892 2483717 : tree init_code;
9893 2483717 : cleanups = make_tree_vector ();
9894 2483717 : init_code = check_initializer (decl, init, flags, &cleanups);
9895 2483717 : if (init_code == NULL_TREE)
9896 2483717 : init = NULL_TREE;
9897 2483717 : release_tree_vector (cleanups);
9898 : }
9899 : else
9900 : {
9901 21924826 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9902 : /* Try to deduce array size. */
9903 21924826 : maybe_deduce_size_from_array_init (decl, init);
9904 : /* And complain about multiple initializers. */
9905 20183491 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9906 22032081 : && !MAYBE_CLASS_TYPE_P (type))
9907 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9908 : tf_warning_or_error);
9909 : }
9910 :
9911 195265803 : if (init)
9912 58444083 : DECL_INITIAL (decl) = init;
9913 :
9914 195265803 : if (dep_init)
9915 : {
9916 7125621 : retrofit_lang_decl (decl);
9917 7125621 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9918 : }
9919 :
9920 195265803 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9921 : {
9922 9 : if (TREE_ADDRESSABLE (decl))
9923 3 : error_at (DECL_SOURCE_LOCATION (decl),
9924 : "address of explicit register variable %qD requested",
9925 : decl);
9926 : else
9927 : {
9928 6 : set_user_assembler_name (decl, asmspec);
9929 6 : DECL_HARD_REGISTER (decl) = 1;
9930 : }
9931 : }
9932 : return;
9933 : }
9934 :
9935 : /* Just store non-static data member initializers for later. */
9936 144638090 : if (init && TREE_CODE (decl) == FIELD_DECL)
9937 616350 : DECL_INITIAL (decl) = init;
9938 :
9939 : /* Take care of TYPE_DECLs up front. */
9940 144638090 : if (TREE_CODE (decl) == TYPE_DECL)
9941 : {
9942 11279601 : if (type != error_mark_node
9943 11279601 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9944 : {
9945 5380706 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9946 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9947 5380706 : set_identifier_type_value (DECL_NAME (decl), decl);
9948 : }
9949 :
9950 : /* If we have installed this as the canonical typedef for this
9951 : type, and that type has not been defined yet, delay emitting
9952 : the debug information for it, as we will emit it later. */
9953 11279601 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9954 11279601 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9955 627615 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9956 :
9957 11279601 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9958 : at_eof);
9959 11279601 : return;
9960 : }
9961 :
9962 : /* A reference will be modified here, as it is initialized. */
9963 133358489 : if (! DECL_EXTERNAL (decl)
9964 65401191 : && TREE_READONLY (decl)
9965 172464473 : && TYPE_REF_P (type))
9966 : {
9967 253755 : was_readonly = 1;
9968 253755 : TREE_READONLY (decl) = 0;
9969 : }
9970 :
9971 : /* This needs to happen before extend_ref_init_temps. */
9972 133358489 : if (VAR_OR_FUNCTION_DECL_P (decl))
9973 : {
9974 123697866 : if (VAR_P (decl))
9975 63855797 : maybe_commonize_var (decl);
9976 123697866 : determine_visibility (decl);
9977 : }
9978 :
9979 133358489 : if (VAR_P (decl))
9980 : {
9981 63855797 : duration_kind dk = decl_storage_duration (decl);
9982 : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9983 : only to a declaration of a variable with static or thread storage
9984 : duration." */
9985 63855797 : if (DECL_DECLARED_CONSTINIT_P (decl)
9986 63855797 : && !(dk == dk_thread || dk == dk_static))
9987 : {
9988 24 : error_at (DECL_SOURCE_LOCATION (decl),
9989 : "%<constinit%> can only be applied to a variable with "
9990 : "static or thread storage duration");
9991 24 : return;
9992 : }
9993 :
9994 63855773 : if (decomp)
9995 : {
9996 68579 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9997 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9998 : "%<constinit%> can be applied to structured binding "
9999 : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
10000 68579 : cp_maybe_mangle_decomp (decl, decomp);
10001 68579 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
10002 : {
10003 622 : if (CP_DECL_THREAD_LOCAL_P (decl))
10004 37 : aggregates1 = tls_aggregates;
10005 : else
10006 585 : aggregates1 = static_aggregates;
10007 : }
10008 : }
10009 :
10010 : /* Detect stuff like 'info r = ^^int;' outside a manifestly
10011 : constant-evaluated context. */
10012 63855773 : check_out_of_consteval_use (decl);
10013 :
10014 : /* If this is a local variable that will need a mangled name,
10015 : register it now. We must do this before processing the
10016 : initializer for the variable, since the initialization might
10017 : require a guard variable, and since the mangled name of the
10018 : guard variable will depend on the mangled name of this
10019 : variable. */
10020 63855773 : if (DECL_FUNCTION_SCOPE_P (decl) && TREE_STATIC (decl))
10021 : {
10022 : /* The variable holding an anonymous union will have had its
10023 : discriminator set in finish_anon_union, after which it's
10024 : NAME will have been cleared. */
10025 142561 : if (DECL_NAME (decl))
10026 142244 : determine_local_discriminator (decl);
10027 : /* Normally has_forced_label_in_static is set during GIMPLE
10028 : lowering, but [cd]tors are never actually compiled directly.
10029 : We need to set this early so we can deal with the label
10030 : address extension. */
10031 142561 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
10032 142520 : || DECL_DESTRUCTOR_P (current_function_decl))
10033 142577 : && init)
10034 : {
10035 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
10036 29 : add_local_decl (cfun, decl);
10037 : }
10038 142561 : if (!consteval_only_p (decl))
10039 : /* And make sure it's in the symbol table for
10040 : c_parse_final_cleanups to find. */
10041 142329 : varpool_node::get_create (decl);
10042 : }
10043 :
10044 63855773 : if (flag_openmp
10045 194790 : && VAR_P (decl)
10046 194790 : && DECL_LANG_SPECIFIC (decl)
10047 115944 : && DECL_OMP_DECLARE_MAPPER_P (decl)
10048 63855786 : && init)
10049 : {
10050 12 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
10051 12 : DECL_INITIAL (decl) = init;
10052 : }
10053 : /* Convert the initializer to the type of DECL, if we have not
10054 : already initialized DECL. */
10055 63855761 : else if (!DECL_INITIALIZED_P (decl)
10056 : /* If !DECL_EXTERNAL then DECL is being defined. In the
10057 : case of a static data member initialized inside the
10058 : class-specifier, there can be an initializer even if DECL
10059 : is *not* defined. */
10060 63855761 : && (!DECL_EXTERNAL (decl) || init))
10061 : {
10062 56786329 : cleanups = make_tree_vector ();
10063 56786329 : init = check_initializer (decl, init, flags, &cleanups);
10064 :
10065 : /* Handle:
10066 :
10067 : [dcl.init]
10068 :
10069 : The memory occupied by any object of static storage
10070 : duration is zero-initialized at program startup before
10071 : any other initialization takes place.
10072 :
10073 : We cannot create an appropriate initializer until after
10074 : the type of DECL is finalized. If DECL_INITIAL is set,
10075 : then the DECL is statically initialized, and any
10076 : necessary zero-initialization has already been performed. */
10077 56783632 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
10078 525910 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
10079 : /*nelts=*/NULL_TREE,
10080 : /*static_storage_p=*/true);
10081 : /* Remember that the initialization for this variable has
10082 : taken place. */
10083 56783632 : DECL_INITIALIZED_P (decl) = 1;
10084 : /* This declaration is the definition of this variable,
10085 : unless we are initializing a static data member within
10086 : the class specifier. */
10087 56783632 : if (!DECL_EXTERNAL (decl))
10088 63853076 : var_definition_p = true;
10089 : }
10090 : /* If the variable has an array type, lay out the type, even if
10091 : there is no initializer. It is valid to index through the
10092 : array, and we must get TYPE_ALIGN set correctly on the array
10093 : type. */
10094 7069432 : else if (TREE_CODE (type) == ARRAY_TYPE)
10095 222236 : layout_type (type);
10096 :
10097 63853076 : if (TREE_STATIC (decl)
10098 37939656 : && !at_function_scope_p ()
10099 101650171 : && current_function_decl == NULL)
10100 : /* So decl is a global variable or a static member of a
10101 : non local class. Record the types it uses
10102 : so that we can decide later to emit debug info for them. */
10103 37797083 : record_types_used_by_current_var_decl (decl);
10104 : }
10105 :
10106 : /* Add this declaration to the statement-tree. This needs to happen
10107 : after the call to check_initializer so that the DECL_EXPR for a
10108 : reference temp is added before the DECL_EXPR for the reference itself. */
10109 133355768 : if (DECL_FUNCTION_SCOPE_P (decl))
10110 : {
10111 : /* If we're building a variable sized type, and we might be
10112 : reachable other than via the top of the current binding
10113 : level, then create a new BIND_EXPR so that we deallocate
10114 : the object at the right time. */
10115 25487585 : if (VAR_P (decl)
10116 25487585 : && DECL_SIZE (decl)
10117 25425096 : && !TREE_CONSTANT (DECL_SIZE (decl))
10118 25488616 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
10119 : {
10120 3 : tree bind;
10121 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
10122 3 : TREE_SIDE_EFFECTS (bind) = 1;
10123 3 : add_stmt (bind);
10124 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
10125 : }
10126 25487585 : add_decl_expr (decl);
10127 : }
10128 :
10129 133355768 : tree decomp_init = NULL_TREE;
10130 : /* Let the middle end know about variables and functions -- but not
10131 : static data members in uninstantiated class templates. */
10132 133355768 : if (VAR_OR_FUNCTION_DECL_P (decl))
10133 : {
10134 123695145 : if (VAR_P (decl))
10135 : {
10136 63853076 : layout_var_decl (decl);
10137 63853076 : if (!flag_weak)
10138 : /* Check again now that we have an initializer. */
10139 57 : maybe_commonize_var (decl);
10140 : /* A class-scope constexpr variable with an out-of-class declaration.
10141 : C++17 makes them implicitly inline, but still force it out. */
10142 86244558 : if (DECL_INLINE_VAR_P (decl)
10143 30074176 : && !DECL_VAR_DECLARED_INLINE_P (decl)
10144 13598868 : && !DECL_TEMPLATE_INSTANTIATION (decl)
10145 48320732 : && !in_class_decl)
10146 108 : mark_needed (decl);
10147 : }
10148 :
10149 123695145 : if (var_definition_p
10150 : /* With -fmerge-all-constants, gimplify_init_constructor
10151 : might add TREE_STATIC to aggregate variables. */
10152 55571455 : && (TREE_STATIC (decl)
10153 25345152 : || (flag_merge_constants >= 2
10154 38 : && AGGREGATE_TYPE_P (type))))
10155 : {
10156 : /* If a TREE_READONLY variable needs initialization
10157 : at runtime, it is no longer readonly and we need to
10158 : avoid MEM_READONLY_P being set on RTL created for it. */
10159 30226306 : if (init)
10160 : {
10161 14467 : if (TREE_READONLY (decl))
10162 348 : TREE_READONLY (decl) = 0;
10163 : was_readonly = 0;
10164 : }
10165 30211839 : else if (was_readonly)
10166 2668 : TREE_READONLY (decl) = 1;
10167 :
10168 : /* Likewise if it needs destruction. */
10169 30226306 : if (!decl_maybe_constant_destruction (decl, type))
10170 3580 : TREE_READONLY (decl) = 0;
10171 : }
10172 93468839 : else if (VAR_P (decl)
10173 33626770 : && CP_DECL_THREAD_LOCAL_P (decl)
10174 19383 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
10175 19383 : && (was_readonly || TREE_READONLY (decl))
10176 93468854 : && var_needs_tls_wrapper (decl))
10177 : {
10178 : /* TLS variables need dynamic initialization by the TLS wrapper
10179 : function, we don't want to hoist accesses to it before the
10180 : wrapper. */
10181 6 : was_readonly = 0;
10182 6 : TREE_READONLY (decl) = 0;
10183 : }
10184 :
10185 123695145 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
10186 :
10187 : /* Check for abstractness of the type. */
10188 123695145 : if (var_definition_p)
10189 55571455 : abstract_virtuals_error (decl, type);
10190 :
10191 123695145 : if (decomp && !cp_finish_decomp (decl, decomp, true))
10192 32796 : decomp = NULL;
10193 :
10194 123695145 : if (TREE_TYPE (decl) == error_mark_node)
10195 : /* No initialization required. */
10196 : ;
10197 123695052 : else if (TREE_CODE (decl) == FUNCTION_DECL)
10198 : {
10199 59842069 : if (init)
10200 : {
10201 600 : if (init == ridpointers[(int)RID_DEFAULT])
10202 : {
10203 : /* An out-of-class default definition is defined at
10204 : the point where it is explicitly defaulted. */
10205 588 : if (DECL_DELETED_FN (decl))
10206 17 : maybe_explain_implicit_delete (decl);
10207 571 : else if (DECL_INITIAL (decl) == error_mark_node)
10208 547 : synthesize_method (decl);
10209 : }
10210 : else
10211 24 : error_at (cp_expr_loc_or_loc (init,
10212 12 : DECL_SOURCE_LOCATION (decl)),
10213 : "function %q#D is initialized like a variable",
10214 : decl);
10215 : }
10216 : /* else no initialization required. */
10217 : }
10218 63852983 : else if (DECL_EXTERNAL (decl)
10219 63852983 : && ! (DECL_LANG_SPECIFIC (decl)
10220 7975540 : && DECL_NOT_REALLY_EXTERN (decl)))
10221 : {
10222 : /* check_initializer will have done any constant initialization. */
10223 : }
10224 : /* A variable definition. */
10225 57536092 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
10226 : {
10227 : /* Initialize the local variable. */
10228 25344997 : if (!decomp)
10229 25309466 : initialize_local_var (decl, init, false);
10230 : else
10231 : {
10232 35531 : tree cleanup = NULL_TREE;
10233 35531 : if (DECL_SIZE (decl))
10234 35531 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10235 : /* If cp_finish_decomp needs to emit any code, we need to emit that
10236 : code after code emitted by initialize_local_var in a single
10237 : CLEANUP_POINT_EXPR, so that temporaries are destructed only
10238 : after the cp_finish_decomp emitted code.
10239 : If there are any cleanups, either extend_ref_init_temps
10240 : created ones or e.g. array destruction, push those first
10241 : with the cleanups guarded on a bool temporary, initially
10242 : set to false and set to true after initialize_local_var
10243 : emitted code. */
10244 35531 : tree guard = NULL_TREE;
10245 35531 : if (cleanups || cleanup)
10246 : {
10247 : /* Since the CLEANUP_STMT will refer to the guard, we need it
10248 : to be a variable with the same lifetime. ??? It might be
10249 : better to use wrap_temporary_cleanups. */
10250 35531 : guard = get_temp_regvar (boolean_type_node, boolean_false_node);
10251 : /* And make sure register_local_var_uses sees it. */
10252 35531 : pushdecl (guard);
10253 : }
10254 :
10255 35531 : tree sl = push_stmt_list ();
10256 35531 : initialize_local_var (decl, init, true);
10257 35531 : if (guard)
10258 : {
10259 35531 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
10260 : guard, boolean_true_node));
10261 35549 : for (tree &t : *cleanups)
10262 18 : t = build3 (COND_EXPR, void_type_node,
10263 : guard, t, void_node);
10264 35531 : if (cleanup)
10265 32 : cleanup = build3 (COND_EXPR, void_type_node,
10266 : guard, cleanup, void_node);
10267 : }
10268 35531 : unsigned before = stmt_list_stack->length ();
10269 35531 : cp_finish_decomp (decl, decomp);
10270 35531 : decomp = NULL;
10271 35531 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
10272 35531 : sl = pop_stmt_list (sl);
10273 35531 : if (n_extra_cleanups)
10274 : {
10275 : /* If cp_finish_decomp needs any cleanups, such as for
10276 : extend_ref_init_temps created vars, pop_stmt_list
10277 : popped that all, so push those extra cleanups around
10278 : the whole sequence with a guard variable. */
10279 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
10280 15 : guard = get_temp_regvar (integer_type_node, integer_zero_node);
10281 15 : pushdecl (guard);
10282 78 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
10283 : {
10284 48 : tree_stmt_iterator tsi = tsi_last (sl);
10285 48 : gcc_assert (!tsi_end_p (tsi));
10286 48 : tree last = tsi_stmt (tsi);
10287 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
10288 : && !CLEANUP_EH_ONLY (last));
10289 48 : tree cst = build_int_cst (integer_type_node, i + 1);
10290 96 : tree cl = build3 (COND_EXPR, void_type_node,
10291 : build2 (GE_EXPR, boolean_type_node,
10292 : guard, cst),
10293 48 : CLEANUP_EXPR (last), void_node);
10294 48 : extra_cleanups.safe_push (cl);
10295 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
10296 : integer_type_node,
10297 : guard, cst),
10298 : TSI_SAME_STMT);
10299 48 : tree sl2 = CLEANUP_BODY (last);
10300 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10301 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10302 48 : tsi_delink (&tsi);
10303 : }
10304 : }
10305 35531 : decomp_init = maybe_cleanup_point_expr_void (sl);
10306 35531 : if (cleanup)
10307 32 : finish_decl_cleanup (decl, cleanup);
10308 : }
10309 : }
10310 :
10311 : /* If a variable is defined, and then a subsequent
10312 : definition with external linkage is encountered, we will
10313 : get here twice for the same variable. We want to avoid
10314 : calling expand_static_init more than once. For variables
10315 : that are not static data members, we can call
10316 : expand_static_init only when we actually process the
10317 : initializer. It is not legal to redeclare a static data
10318 : member, so this issue does not arise in that case. */
10319 32191095 : else if (var_definition_p && TREE_STATIC (decl))
10320 : {
10321 30224523 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10322 : {
10323 106 : tree sl = push_stmt_list ();
10324 106 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10325 106 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10326 106 : expand_static_init (decl, init);
10327 106 : current_stmt_tree ()->stmts_are_full_exprs_p
10328 106 : = saved_stmts_are_full_exprs_p;
10329 106 : cp_finish_decomp (decl, decomp);
10330 106 : decomp = NULL;
10331 106 : sl = pop_stmt_list (sl);
10332 106 : sl = maybe_cleanup_point_expr_void (sl);
10333 106 : add_stmt (sl);
10334 : }
10335 : else
10336 30224417 : expand_static_init (decl, init);
10337 : }
10338 : }
10339 :
10340 : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10341 : reference, insert it in the statement-tree now. */
10342 133355768 : if (cleanups)
10343 : {
10344 56787059 : for (tree t : *cleanups)
10345 : {
10346 3427 : push_cleanup (NULL_TREE, t, false);
10347 : /* As in initialize_local_var. */
10348 3427 : wrap_temporary_cleanups (init, t);
10349 : }
10350 56783632 : release_tree_vector (cleanups);
10351 : }
10352 :
10353 133355846 : for (tree t : &extra_cleanups)
10354 48 : push_cleanup (NULL_TREE, t, false);
10355 :
10356 133355768 : if (decomp_init)
10357 35531 : add_stmt (decomp_init);
10358 :
10359 133355768 : if (decomp
10360 146 : && var_definition_p
10361 146 : && TREE_STATIC (decl)
10362 133355914 : && !DECL_FUNCTION_SCOPE_P (decl))
10363 : {
10364 146 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10365 146 : ? tls_aggregates : static_aggregates);
10366 146 : tree aggregates2 = aggregates3;
10367 146 : if (aggregates2 != aggregates1)
10368 : {
10369 66 : cp_finish_decomp (decl, decomp);
10370 66 : decomp = NULL;
10371 66 : if (aggregates3 != aggregates2)
10372 : {
10373 : /* If there are dynamic initializers for the structured
10374 : binding base or associated extended ref temps and also
10375 : dynamic initializers for the structured binding non-base
10376 : vars, mark them. */
10377 267 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10378 204 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10379 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10380 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
10381 : }
10382 : }
10383 : }
10384 :
10385 133355768 : if (was_readonly)
10386 253185 : TREE_READONLY (decl) = 1;
10387 :
10388 133355768 : if (flag_openmp
10389 443317 : && VAR_P (decl)
10390 133550558 : && lookup_attribute ("omp declare target implicit",
10391 194790 : DECL_ATTRIBUTES (decl)))
10392 : {
10393 52 : DECL_ATTRIBUTES (decl)
10394 52 : = remove_attribute ("omp declare target implicit",
10395 52 : DECL_ATTRIBUTES (decl));
10396 52 : complete_type (TREE_TYPE (decl));
10397 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
10398 : {
10399 30 : auto_diagnostic_group d;
10400 30 : error ("%q+D in declare target directive does not have mappable"
10401 : " type", decl);
10402 30 : if (TREE_TYPE (decl) != error_mark_node
10403 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10404 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
10405 30 : }
10406 22 : else if (!lookup_attribute ("omp declare target",
10407 22 : DECL_ATTRIBUTES (decl))
10408 44 : && !lookup_attribute ("omp declare target link",
10409 22 : DECL_ATTRIBUTES (decl)))
10410 : {
10411 22 : DECL_ATTRIBUTES (decl)
10412 22 : = tree_cons (get_identifier ("omp declare target"),
10413 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
10414 22 : symtab_node *node = symtab_node::get (decl);
10415 22 : if (node != NULL)
10416 : {
10417 19 : node->offloadable = 1;
10418 19 : if (ENABLE_OFFLOADING)
10419 : {
10420 : g->have_offload = true;
10421 : if (is_a <varpool_node *> (node))
10422 : vec_safe_push (offload_vars, decl);
10423 : }
10424 : }
10425 : }
10426 : }
10427 :
10428 : /* This is the last point we can lower alignment so give the target the
10429 : chance to do so. */
10430 133355768 : if (VAR_P (decl)
10431 63853076 : && !is_global_var (decl)
10432 158700920 : && !DECL_HARD_REGISTER (decl))
10433 25345099 : targetm.lower_local_decl_alignment (decl);
10434 :
10435 133355768 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
10436 339907553 : }
10437 :
10438 : /* For class TYPE return itself or some its bases that contain
10439 : any direct non-static data members. Return error_mark_node if an
10440 : error has been diagnosed. */
10441 :
10442 : static tree
10443 31361 : find_decomp_class_base (location_t loc, tree type, tree ret,
10444 : tsubst_flags_t complain)
10445 : {
10446 62722 : if (LAMBDA_TYPE_P (type))
10447 : {
10448 12 : if (complain & tf_error)
10449 : {
10450 12 : auto_diagnostic_group d;
10451 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
10452 12 : inform (location_of (type), "lambda declared here");
10453 12 : }
10454 12 : return error_mark_node;
10455 : }
10456 :
10457 31349 : bool member_seen = false;
10458 397802 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10459 669586 : if (TREE_CODE (field) != FIELD_DECL
10460 63534 : || DECL_ARTIFICIAL (field)
10461 429926 : || DECL_UNNAMED_BIT_FIELD (field))
10462 303094 : continue;
10463 63398 : else if (ret)
10464 : return type;
10465 63372 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10466 : {
10467 8 : if ((complain & tf_error) == 0)
10468 0 : return error_mark_node;
10469 8 : auto_diagnostic_group d;
10470 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10471 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10472 : "anonymous struct member", type);
10473 : else
10474 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10475 : "anonymous union member", type);
10476 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
10477 8 : return error_mark_node;
10478 8 : }
10479 63364 : else if (!accessible_p (type, field, true))
10480 : {
10481 5 : if ((complain & tf_error) == 0)
10482 0 : return error_mark_node;
10483 5 : auto_diagnostic_group d;
10484 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10485 : field, type);
10486 5 : inform (DECL_SOURCE_LOCATION (field),
10487 5 : TREE_PRIVATE (field)
10488 : ? G_("declared private here")
10489 : : G_("declared protected here"));
10490 5 : return error_mark_node;
10491 5 : }
10492 : else
10493 : member_seen = true;
10494 :
10495 31310 : tree base_binfo, binfo;
10496 31310 : tree orig_ret = ret;
10497 31310 : int i;
10498 31310 : if (member_seen)
10499 31207 : ret = type;
10500 31374 : for (binfo = TYPE_BINFO (type), i = 0;
10501 31374 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10502 : {
10503 84 : auto_diagnostic_group d;
10504 84 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10505 : complain);
10506 84 : if (t == error_mark_node)
10507 : {
10508 3 : if (complain & tf_error)
10509 3 : inform (location_of (type), "in base class of %qT", type);
10510 3 : return error_mark_node;
10511 : }
10512 81 : if (t != NULL_TREE && t != ret)
10513 : {
10514 54 : if (ret == type)
10515 : {
10516 10 : if (complain & tf_error)
10517 10 : error_at (loc, "cannot decompose class type %qT: both it and "
10518 : "its base class %qT have non-static data "
10519 : "members", type, t);
10520 10 : return error_mark_node;
10521 : }
10522 44 : else if (orig_ret != NULL_TREE)
10523 : return t;
10524 44 : else if (ret != NULL_TREE)
10525 : {
10526 7 : if (complain & tf_error)
10527 7 : error_at (loc, "cannot decompose class type %qT: its base "
10528 : "classes %qT and %qT have non-static data "
10529 : "members", type, ret, t);
10530 7 : return error_mark_node;
10531 : }
10532 : else
10533 : ret = t;
10534 : }
10535 84 : }
10536 : return ret;
10537 : }
10538 :
10539 : /* Return std::tuple_size<TYPE>::value. */
10540 :
10541 : static tree
10542 112858 : get_tuple_size (tree type)
10543 : {
10544 112858 : tree args = make_tree_vec (1);
10545 112858 : TREE_VEC_ELT (args, 0) = type;
10546 112858 : tree inst = lookup_template_class (tuple_size_identifier, args,
10547 : /*in_decl*/NULL_TREE,
10548 : /*context*/std_node,
10549 : tf_none);
10550 112858 : inst = complete_type (inst);
10551 112858 : if (inst == error_mark_node
10552 111368 : || !COMPLETE_TYPE_P (inst)
10553 194394 : || !CLASS_TYPE_P (inst))
10554 : return NULL_TREE;
10555 81536 : tree val = lookup_qualified_name (inst, value_identifier,
10556 : LOOK_want::NORMAL, /*complain*/false);
10557 81536 : if (val == error_mark_node)
10558 : return NULL_TREE;
10559 81530 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10560 81530 : val = maybe_constant_value (val, NULL_TREE, mce_true);
10561 81530 : if (TREE_CODE (val) == INTEGER_CST)
10562 : return val;
10563 : else
10564 6 : return error_mark_node;
10565 : }
10566 :
10567 : /* Return std::tuple_element<I,TYPE>::type. */
10568 :
10569 : static tree
10570 92065 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10571 : {
10572 92065 : tree args = make_tree_vec (2);
10573 92065 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10574 92065 : TREE_VEC_ELT (args, 1) = type;
10575 92065 : tree inst = lookup_template_class (tuple_element_identifier, args,
10576 : /*in_decl*/NULL_TREE,
10577 : /*context*/std_node,
10578 : tf_warning_or_error);
10579 92065 : return make_typename_type (inst, type_identifier,
10580 92065 : none_type, tf_warning_or_error);
10581 : }
10582 :
10583 : /* Return e.get<i>() or get<i>(e). */
10584 :
10585 : static tree
10586 92074 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10587 : {
10588 92074 : tree targs = make_tree_vec (1);
10589 92074 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10590 :
10591 92074 : tree etype = TREE_TYPE (decl);
10592 92074 : tree e = convert_from_reference (decl);
10593 :
10594 : /* [The id-expression] e is an lvalue if the type of the entity e is an
10595 : lvalue reference and an xvalue otherwise. */
10596 92074 : if (!TYPE_REF_P (etype)
10597 92074 : || TYPE_REF_IS_RVALUE (etype))
10598 90702 : e = move (e);
10599 :
10600 92074 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10601 : LOOK_want::NORMAL, /*complain*/false);
10602 92074 : bool use_member_get = false;
10603 :
10604 : /* To use a member get, member lookup must find at least one
10605 : declaration that is a function template
10606 : whose first template parameter is a non-type parameter. */
10607 273736 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10608 : {
10609 92074 : tree fn = *iter;
10610 92074 : if (TREE_CODE (fn) == TEMPLATE_DECL)
10611 : {
10612 2489 : tree tparms = DECL_TEMPLATE_PARMS (fn);
10613 2489 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10614 2489 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10615 : {
10616 : use_member_get = true;
10617 : break;
10618 : }
10619 : }
10620 : }
10621 :
10622 92074 : if (use_member_get)
10623 : {
10624 2486 : fns = lookup_template_function (fns, targs);
10625 2486 : return build_new_method_call (e, fns, /*args*/NULL,
10626 : /*path*/NULL_TREE, LOOKUP_NORMAL,
10627 2486 : /*fn_p*/NULL, tf_warning_or_error);
10628 : }
10629 : else
10630 : {
10631 89588 : releasing_vec args (make_tree_vector_single (e));
10632 89588 : fns = lookup_template_function (get__identifier, targs);
10633 89588 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10634 89588 : return finish_call_expr (fns, &args, /*novirt*/false,
10635 : /*koenig*/true, tf_warning_or_error);
10636 89588 : }
10637 : }
10638 :
10639 : /* It's impossible to recover the decltype of a tuple decomposition variable
10640 : based on the actual type of the variable, so store it in a hash table. */
10641 :
10642 : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10643 :
10644 : tree
10645 662 : lookup_decomp_type (tree v)
10646 : {
10647 662 : if (decomp_type_table)
10648 535 : if (tree *slot = decomp_type_table->get (v))
10649 471 : return *slot;
10650 : return NULL_TREE;
10651 : }
10652 :
10653 : /* Mangle a decomposition declaration if needed. Arguments like
10654 : in cp_finish_decomp. */
10655 :
10656 : static void
10657 68579 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10658 : {
10659 68579 : if (!processing_template_decl
10660 68579 : && !error_operand_p (decl)
10661 137158 : && TREE_STATIC (decl))
10662 : {
10663 889 : auto_vec<tree, 16> v;
10664 889 : v.safe_grow (decomp->count, true);
10665 889 : tree d = decomp->decl;
10666 2944 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10667 2055 : v[decomp->count - i - 1] = d;
10668 889 : if (DECL_FUNCTION_SCOPE_P (decl))
10669 : {
10670 : size_t sz = 3;
10671 929 : for (unsigned int i = 0; i < decomp->count; ++i)
10672 662 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10673 267 : char *name = XALLOCAVEC (char, sz);
10674 267 : name[0] = 'D';
10675 267 : name[1] = 'C';
10676 267 : char *p = name + 2;
10677 929 : for (unsigned int i = 0; i < decomp->count; ++i)
10678 : {
10679 662 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10680 662 : *p++ = ' ';
10681 662 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
10682 662 : p += len;
10683 : }
10684 267 : *p = '\0';
10685 267 : determine_local_discriminator (decl, get_identifier (name));
10686 : }
10687 889 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10688 889 : maybe_apply_pragma_weak (decl);
10689 889 : }
10690 68579 : }
10691 :
10692 : /* Append #i to DECL_NAME (decl) or for name independent decls
10693 : clear DECL_NAME (decl). */
10694 :
10695 : static void
10696 33807 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10697 : {
10698 33807 : if (name_independent_decl_p (decl))
10699 : /* Only "_" names are treated as name independent, "_#0" etc. is not and
10700 : because we pushdecl the individual decl elements of structured binding
10701 : pack, we could get redeclaration errors if there are 2 or more name
10702 : independent structured binding packs in the same scope. */
10703 312 : DECL_NAME (decl) = NULL_TREE;
10704 : else
10705 : {
10706 33495 : tree name = DECL_NAME (decl);
10707 33495 : size_t len = IDENTIFIER_LENGTH (name) + 22;
10708 33495 : char *n = XALLOCAVEC (char, len);
10709 33495 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10710 33495 : IDENTIFIER_POINTER (name), i);
10711 33495 : DECL_NAME (decl) = get_identifier (n);
10712 : }
10713 33807 : }
10714 :
10715 : /* Return structured binding size of TYPE or -1 if erroneous. */
10716 :
10717 : HOST_WIDE_INT
10718 954 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10719 : {
10720 954 : if (TYPE_REF_P (type))
10721 : {
10722 0 : type = complete_type (TREE_TYPE (type));
10723 0 : if (type == error_mark_node)
10724 : return -1;
10725 0 : if (!COMPLETE_TYPE_P (type))
10726 : {
10727 0 : if (complain & tf_error)
10728 0 : error_at (loc, "structured binding refers to incomplete type %qT",
10729 : type);
10730 : return -1;
10731 : }
10732 : }
10733 :
10734 954 : unsigned HOST_WIDE_INT eltscnt = 0;
10735 954 : if (TREE_CODE (type) == ARRAY_TYPE)
10736 : {
10737 724 : if (TYPE_DOMAIN (type) == NULL_TREE)
10738 : {
10739 9 : if (complain & tf_error)
10740 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
10741 : type);
10742 : return -1;
10743 : }
10744 715 : tree nelts = array_type_nelts_top (type);
10745 715 : if (nelts == error_mark_node)
10746 : return -1;
10747 715 : if (!tree_fits_shwi_p (nelts))
10748 : {
10749 3 : if (complain & tf_error)
10750 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10751 : return -1;
10752 : }
10753 712 : return tree_to_shwi (nelts);
10754 : }
10755 : /* 2 GNU extensions. */
10756 230 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10757 : return 2;
10758 224 : else if (TREE_CODE (type) == VECTOR_TYPE)
10759 : {
10760 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10761 : {
10762 : if (complain & tf_error)
10763 : error_at (loc, "cannot decompose variable length vector %qT", type);
10764 : return -1;
10765 : }
10766 12 : return eltscnt;
10767 : }
10768 212 : else if (tree tsize = get_tuple_size (type))
10769 : {
10770 38 : if (tsize == error_mark_node
10771 32 : || !tree_fits_shwi_p (tsize)
10772 70 : || tree_int_cst_sgn (tsize) < 0)
10773 : {
10774 9 : if (complain & tf_error)
10775 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10776 : "constant expression", type);
10777 : return -1;
10778 : }
10779 29 : return tree_to_shwi (tsize);
10780 : }
10781 174 : else if (TREE_CODE (type) == UNION_TYPE)
10782 : {
10783 3 : if (complain & tf_error)
10784 3 : error_at (loc, "cannot decompose union type %qT", type);
10785 : return -1;
10786 : }
10787 171 : else if (!CLASS_TYPE_P (type))
10788 : {
10789 21 : if (complain & tf_error)
10790 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10791 : return -1;
10792 : }
10793 150 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10794 : return -1;
10795 150 : else if (!COMPLETE_TYPE_P (type))
10796 : {
10797 3 : if (complain & tf_error)
10798 3 : error_at (loc, "structured binding refers to incomplete class type "
10799 : "%qT", type);
10800 : return -1;
10801 : }
10802 : else
10803 : {
10804 147 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10805 147 : if (btype == error_mark_node)
10806 : return -1;
10807 126 : else if (btype == NULL_TREE)
10808 : return 0;
10809 105 : if (btype != type)
10810 : {
10811 6 : tree binfo = lookup_base (type, btype, ba_check, NULL, complain);
10812 6 : if (binfo == NULL_TREE || binfo == error_mark_node)
10813 : return -1;
10814 : }
10815 1123 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10816 1703 : if (TREE_CODE (field) != FIELD_DECL
10817 357 : || DECL_ARTIFICIAL (field)
10818 1381 : || DECL_UNNAMED_BIT_FIELD (field))
10819 679 : continue;
10820 : else
10821 345 : eltscnt++;
10822 99 : return eltscnt;
10823 : }
10824 : }
10825 :
10826 : /* Finish a decomposition declaration. DECL is the underlying declaration
10827 : "e", FIRST is the head of a chain of decls for the individual identifiers
10828 : chained through DECL_CHAIN in reverse order and COUNT is the number of
10829 : those decls. If TEST_P is true, return true if any code would need to be
10830 : actually emitted but don't emit it. Return false otherwise. */
10831 :
10832 : bool
10833 344221 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10834 : {
10835 344221 : tree first = decomp->decl;
10836 344221 : unsigned count = decomp->count;
10837 344221 : if (error_operand_p (decl))
10838 : {
10839 200 : error_out:
10840 625 : while (count--)
10841 : {
10842 425 : TREE_TYPE (first) = error_mark_node;
10843 425 : if (DECL_HAS_VALUE_EXPR_P (first))
10844 : {
10845 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10846 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10847 : }
10848 425 : first = DECL_CHAIN (first);
10849 : }
10850 200 : if (DECL_P (decl) && TREE_STATIC (decl))
10851 54 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10852 : return false;
10853 : }
10854 :
10855 344158 : location_t loc = DECL_SOURCE_LOCATION (decl);
10856 344158 : if (type_dependent_expression_p (decl)
10857 : /* This happens for range for when not in templates.
10858 : Still add the DECL_VALUE_EXPRs for later processing. */
10859 344158 : || (!processing_template_decl
10860 104643 : && type_uses_auto (TREE_TYPE (decl))))
10861 : {
10862 659699 : for (unsigned int i = 0; i < count; i++)
10863 : {
10864 439562 : if (!DECL_HAS_VALUE_EXPR_P (first))
10865 : {
10866 439562 : tree v = build_nt (ARRAY_REF, decl,
10867 439562 : size_int (count - i - 1),
10868 : NULL_TREE, NULL_TREE);
10869 439562 : SET_DECL_VALUE_EXPR (first, v);
10870 439562 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10871 : }
10872 439562 : if (processing_template_decl)
10873 438926 : fit_decomposition_lang_decl (first, decl);
10874 439562 : first = DECL_CHAIN (first);
10875 : }
10876 : return false;
10877 : }
10878 :
10879 124021 : auto_vec<tree, 16> v;
10880 124021 : v.safe_grow (count, true);
10881 124021 : tree d = first;
10882 124021 : int pack = -1;
10883 487041 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10884 : {
10885 238999 : v[count - i - 1] = d;
10886 238999 : fit_decomposition_lang_decl (d, decl);
10887 238999 : if (DECL_PACK_P (d))
10888 11111 : pack = count - i - 1;
10889 : }
10890 :
10891 124021 : tree type = TREE_TYPE (decl);
10892 124021 : tree dexp = decl;
10893 :
10894 124021 : if (TYPE_REF_P (type))
10895 : {
10896 2206 : dexp = convert_from_reference (dexp);
10897 2206 : type = complete_type (TREE_TYPE (type));
10898 2206 : if (type == error_mark_node)
10899 137 : goto error_out;
10900 2206 : if (!COMPLETE_TYPE_P (type))
10901 : {
10902 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10903 : type);
10904 3 : goto error_out;
10905 : }
10906 : }
10907 :
10908 124018 : tree eltype = NULL_TREE;
10909 124018 : unsigned HOST_WIDE_INT eltscnt = 0;
10910 : /* Structured binding packs when initializer is non-dependent should
10911 : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10912 : of that TREE_VEC are the base and index, what is normally represented
10913 : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10914 : of the pack first element. The remaining elements of the TREE_VEC
10915 : are VAR_DECLs for the pack elements. */
10916 124018 : tree packv = NULL_TREE;
10917 :
10918 124018 : if (TREE_CODE (type) == ARRAY_TYPE)
10919 : {
10920 11234 : tree nelts;
10921 11234 : nelts = array_type_nelts_top (type);
10922 11234 : if (nelts == error_mark_node)
10923 0 : goto error_out;
10924 11234 : if (DECL_DECOMP_BASE (decl))
10925 : {
10926 9 : error_at (loc, "array initializer for structured binding "
10927 : "declaration in condition");
10928 9 : goto error_out;
10929 : }
10930 11225 : if (!tree_fits_uhwi_p (nelts))
10931 : {
10932 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10933 3 : goto error_out;
10934 : }
10935 11222 : eltscnt = tree_to_uhwi (nelts);
10936 11222 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10937 : {
10938 11 : cnt_mismatch:
10939 38 : auto_diagnostic_group d;
10940 38 : if (count > eltscnt)
10941 27 : error_n (loc, count,
10942 : "%u name provided for structured binding",
10943 : "%u names provided for structured binding", count);
10944 : else
10945 11 : error_n (loc, count,
10946 : "only %u name provided for structured binding",
10947 : "only %u names provided for structured binding", count);
10948 38 : inform_n (loc, eltscnt,
10949 : "while %qT decomposes into %wu element",
10950 : "while %qT decomposes into %wu elements",
10951 : type, eltscnt);
10952 38 : goto error_out;
10953 : }
10954 11211 : eltype = TREE_TYPE (type);
10955 24136 : for (unsigned int i = 0; i < count; i++)
10956 : {
10957 12925 : if ((unsigned) pack == i)
10958 : {
10959 10060 : packv = make_tree_vec (eltscnt - count + 3);
10960 51506 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10961 : {
10962 31386 : tree t;
10963 31386 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10964 31386 : set_sb_pack_name (t, j);
10965 31386 : maybe_push_decl (t);
10966 31386 : TREE_TYPE (t) = eltype;
10967 31386 : layout_decl (t, 0);
10968 31386 : if (!processing_template_decl)
10969 : {
10970 31288 : tree a = unshare_expr (dexp);
10971 31288 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10972 : NULL_TREE, NULL_TREE);
10973 31288 : SET_DECL_VALUE_EXPR (t, a);
10974 31288 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10975 : }
10976 : }
10977 12925 : continue;
10978 10060 : }
10979 2865 : TREE_TYPE (v[i]) = eltype;
10980 2865 : layout_decl (v[i], 0);
10981 2865 : if (processing_template_decl)
10982 140 : continue;
10983 2725 : tree t = unshare_expr (dexp);
10984 2725 : unsigned HOST_WIDE_INT j = i;
10985 2725 : if (pack != -1 && (unsigned) pack < i)
10986 64 : j = i + eltscnt - count;
10987 2725 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10988 2725 : SET_DECL_VALUE_EXPR (v[i], t);
10989 2725 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10990 : }
10991 : }
10992 : /* 2 GNU extensions. */
10993 112784 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10994 : {
10995 78 : eltscnt = 2;
10996 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10997 3 : goto cnt_mismatch;
10998 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10999 288 : for (unsigned int i = 0; i < count; i++)
11000 : {
11001 138 : if ((unsigned) pack == i)
11002 : {
11003 60 : packv = make_tree_vec (eltscnt - count + 3);
11004 192 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11005 : {
11006 72 : tree t;
11007 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11008 72 : set_sb_pack_name (t, j);
11009 72 : maybe_push_decl (t);
11010 72 : TREE_TYPE (t) = eltype;
11011 72 : layout_decl (t, 0);
11012 72 : if (!processing_template_decl)
11013 : {
11014 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
11015 : unshare_expr (dexp));
11016 48 : SET_DECL_VALUE_EXPR (t, a);
11017 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11018 : }
11019 : }
11020 60 : continue;
11021 60 : }
11022 78 : TREE_TYPE (v[i]) = eltype;
11023 78 : layout_decl (v[i], 0);
11024 78 : if (processing_template_decl)
11025 16 : continue;
11026 62 : tree t = unshare_expr (dexp);
11027 62 : unsigned HOST_WIDE_INT j = i;
11028 62 : if (pack != -1 && (unsigned) pack < i)
11029 16 : j = i + eltscnt - count;
11030 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
11031 62 : SET_DECL_VALUE_EXPR (v[i], t);
11032 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11033 : }
11034 : }
11035 112706 : else if (TREE_CODE (type) == VECTOR_TYPE)
11036 : {
11037 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
11038 : {
11039 : error_at (loc, "cannot decompose variable length vector %qT", type);
11040 : goto error_out;
11041 : }
11042 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11043 3 : goto cnt_mismatch;
11044 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11045 306 : for (unsigned int i = 0; i < count; i++)
11046 : {
11047 192 : if ((unsigned) pack == i)
11048 : {
11049 48 : packv = make_tree_vec (eltscnt - count + 3);
11050 756 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11051 : {
11052 660 : tree t;
11053 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11054 660 : set_sb_pack_name (t, j);
11055 660 : maybe_push_decl (t);
11056 660 : TREE_TYPE (t) = eltype;
11057 660 : layout_decl (t, 0);
11058 660 : if (!processing_template_decl)
11059 : {
11060 440 : tree a = unshare_expr (dexp);
11061 440 : location_t loc = DECL_SOURCE_LOCATION (t);
11062 440 : tree s = size_int (j + pack);
11063 440 : convert_vector_to_array_for_subscript (loc, &a, s);
11064 440 : a = build4 (ARRAY_REF, eltype, a, s,
11065 : NULL_TREE, NULL_TREE);
11066 440 : SET_DECL_VALUE_EXPR (t, a);
11067 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11068 : }
11069 : }
11070 84 : continue;
11071 48 : }
11072 144 : TREE_TYPE (v[i]) = eltype;
11073 144 : layout_decl (v[i], 0);
11074 144 : if (processing_template_decl)
11075 36 : continue;
11076 108 : tree t = unshare_expr (dexp);
11077 108 : unsigned HOST_WIDE_INT j = i;
11078 108 : if (pack != -1 && (unsigned) pack < i)
11079 24 : j = i + eltscnt - count;
11080 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
11081 108 : &t, size_int (j));
11082 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
11083 108 : SET_DECL_VALUE_EXPR (v[i], t);
11084 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11085 : }
11086 : }
11087 112646 : else if (tree tsize = get_tuple_size (type))
11088 : {
11089 81492 : if (tsize == error_mark_node)
11090 : {
11091 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
11092 : "constant expression", type);
11093 0 : goto error_out;
11094 : }
11095 81492 : if (!tree_fits_uhwi_p (tsize))
11096 : {
11097 12 : auto_diagnostic_group d;
11098 12 : error_n (loc, count,
11099 : "%u name provided for structured binding",
11100 : "%u names provided for structured binding", count);
11101 12 : inform (loc, "while %qT decomposes into %E elements",
11102 : type, tsize);
11103 12 : goto error_out;
11104 12 : }
11105 81480 : eltscnt = tree_to_uhwi (tsize);
11106 81480 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11107 6 : goto cnt_mismatch;
11108 81474 : if (test_p && eltscnt)
11109 123884 : return true;
11110 45691 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
11111 : {
11112 : /* For structured bindings used in conditions we need to evaluate
11113 : the conversion of decl (aka e in the standard) to bool or
11114 : integral/enumeral type (the latter for switch conditions)
11115 : before the get methods, as [dcl.struct.bind]/7 requires that:
11116 : "The initialization of e and any conversion of e considered as
11117 : a decision variable is sequenced before the initialization of
11118 : any r_i." */
11119 89 : tree cond = convert_from_reference (decl);
11120 89 : if (integer_onep (DECL_DECOMP_BASE (decl)))
11121 : /* switch condition. */
11122 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
11123 : cond, true);
11124 : else
11125 : /* if/while/for condition. */
11126 71 : cond = contextual_conv_bool (cond, tf_warning_or_error);
11127 89 : if (cond && !error_operand_p (cond))
11128 : {
11129 89 : cond = get_temp_regvar (TREE_TYPE (cond), cond);
11130 89 : pushdecl (cond);
11131 : /* Set DECL_DECOMP_BASE to cond VAR_DECL wrapped in
11132 : NON_LVALUE_EXPR, such that it is considered to be
11133 : the condition of a structured binding rather than
11134 : structured binding's base variable. */
11135 89 : DECL_DECOMP_BASE (decl)
11136 178 : = build1 (NON_LVALUE_EXPR, TREE_TYPE (cond), cond);
11137 : }
11138 : }
11139 45691 : int save_read = DECL_READ_P (decl);
11140 136960 : for (unsigned i = 0; i < count; ++i)
11141 : {
11142 91290 : location_t sloc = input_location;
11143 91290 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
11144 :
11145 91290 : if ((unsigned) pack == i)
11146 : {
11147 401 : packv = make_tree_vec (eltscnt - count + 3);
11148 1987 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
11149 : {
11150 1185 : tree t;
11151 1185 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
11152 1185 : set_sb_pack_name (t, j);
11153 1185 : input_location = dloc;
11154 1185 : tree init = get_tuple_decomp_init (decl, j + pack);
11155 1185 : tree eltype = (init == error_mark_node ? error_mark_node
11156 1185 : : get_tuple_element_type (type, j + pack));
11157 1185 : input_location = sloc;
11158 :
11159 1185 : if (VOID_TYPE_P (eltype))
11160 : {
11161 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
11162 : "%<void%>", j + pack, type);
11163 0 : eltype = error_mark_node;
11164 : }
11165 1185 : if (init == error_mark_node || eltype == error_mark_node)
11166 : {
11167 0 : inform (dloc, "in initialization of structured binding "
11168 0 : "pack %qD", v[pack]);
11169 0 : goto error_out;
11170 : }
11171 1185 : maybe_push_decl (t);
11172 : /* Save the decltype away before reference collapse. */
11173 1185 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
11174 1185 : if (glvalue_p (init))
11175 1083 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11176 1185 : TREE_TYPE (t) = eltype;
11177 1185 : layout_decl (t, 0);
11178 1185 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11179 1185 : if (!processing_template_decl)
11180 : {
11181 795 : copy_linkage (t, decl);
11182 795 : tree name = DECL_NAME (t);
11183 795 : if (TREE_STATIC (decl))
11184 52 : DECL_NAME (t) = DECL_NAME (v[pack]);
11185 795 : cp_finish_decl (t, init, /*constexpr*/false,
11186 : /*asm*/NULL_TREE, LOOKUP_NORMAL);
11187 795 : if (TREE_STATIC (decl))
11188 : {
11189 52 : DECL_ASSEMBLER_NAME (t);
11190 52 : DECL_NAME (t) = name;
11191 : }
11192 : }
11193 : }
11194 401 : continue;
11195 401 : }
11196 :
11197 90889 : unsigned HOST_WIDE_INT j = i;
11198 90889 : if (pack != -1 && (unsigned) pack < i)
11199 235 : j = i + eltscnt - count;
11200 90889 : input_location = dloc;
11201 90889 : tree init = get_tuple_decomp_init (decl, j);
11202 90889 : tree eltype = (init == error_mark_node ? error_mark_node
11203 90880 : : get_tuple_element_type (type, j));
11204 90889 : input_location = sloc;
11205 :
11206 90889 : if (VOID_TYPE_P (eltype))
11207 : {
11208 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
11209 : j, type);
11210 3 : eltype = error_mark_node;
11211 : }
11212 90889 : if (init == error_mark_node || eltype == error_mark_node)
11213 : {
11214 21 : inform (dloc, "in initialization of structured binding "
11215 21 : "variable %qD", v[i]);
11216 21 : goto error_out;
11217 : }
11218 : /* Save the decltype away before reference collapse. */
11219 90868 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
11220 90868 : if (glvalue_p (init))
11221 90517 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
11222 90868 : TREE_TYPE (v[i]) = eltype;
11223 90868 : layout_decl (v[i], 0);
11224 90868 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
11225 : {
11226 : /* In this case the names are variables, not just proxies. */
11227 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
11228 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
11229 : }
11230 90868 : if (!processing_template_decl)
11231 : {
11232 71146 : copy_linkage (v[i], decl);
11233 71146 : cp_finish_decl (v[i], init, /*constexpr*/false,
11234 : /*asm*/NULL_TREE, LOOKUP_NORMAL);
11235 : }
11236 : }
11237 : /* Ignore reads from the underlying decl performed during initialization
11238 : of the individual variables. If those will be read, we'll mark
11239 : the underlying decl as read at that point. */
11240 45670 : DECL_READ_P (decl) = save_read;
11241 : }
11242 31154 : else if (TREE_CODE (type) == UNION_TYPE)
11243 : {
11244 3 : error_at (loc, "cannot decompose union type %qT", type);
11245 3 : goto error_out;
11246 : }
11247 31151 : else if (!CLASS_TYPE_P (type))
11248 : {
11249 19 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
11250 19 : goto error_out;
11251 : }
11252 31132 : else if (processing_template_decl && complete_type (type) == error_mark_node)
11253 0 : goto error_out;
11254 31132 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
11255 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
11256 : type);
11257 : else
11258 : {
11259 31130 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
11260 : tf_warning_or_error);
11261 31130 : if (btype == error_mark_node)
11262 21 : goto error_out;
11263 31109 : else if (btype == NULL_TREE)
11264 : {
11265 24 : if (pack == 0 && count == 1)
11266 : {
11267 16 : eltscnt = 0;
11268 16 : packv = make_tree_vec (2);
11269 16 : goto done;
11270 : }
11271 8 : error_at (loc, "cannot decompose class type %qT without non-static "
11272 : "data members", type);
11273 8 : goto error_out;
11274 : }
11275 394996 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11276 664846 : if (TREE_CODE (field) != FIELD_DECL
11277 62992 : || DECL_ARTIFICIAL (field)
11278 426899 : || DECL_UNNAMED_BIT_FIELD (field))
11279 300935 : continue;
11280 : else
11281 62976 : eltscnt++;
11282 31085 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11283 15 : goto cnt_mismatch;
11284 31070 : tree t = dexp;
11285 31070 : if (type != btype)
11286 : {
11287 18 : t = convert_to_base (t, btype, /*check_access*/true,
11288 : /*nonnull*/false, tf_warning_or_error);
11289 18 : type = btype;
11290 : }
11291 31070 : unsigned HOST_WIDE_INT j = 0;
11292 31070 : unsigned int i = 0;
11293 394785 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11294 664491 : if (TREE_CODE (field) != FIELD_DECL
11295 62955 : || DECL_ARTIFICIAL (field)
11296 426666 : || DECL_UNNAMED_BIT_FIELD (field))
11297 300776 : continue;
11298 : else
11299 : {
11300 62939 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
11301 : NULL_TREE);
11302 62939 : if (REFERENCE_REF_P (tt))
11303 30 : tt = TREE_OPERAND (tt, 0);
11304 62939 : if (pack != -1 && j >= (unsigned) pack)
11305 : {
11306 685 : if (j == (unsigned) pack)
11307 : {
11308 227 : packv = make_tree_vec (eltscnt - count + 3);
11309 227 : i++;
11310 : }
11311 685 : if (j < (unsigned) pack + eltscnt - (count - 1))
11312 : {
11313 504 : tree t;
11314 504 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11315 504 : set_sb_pack_name (t, j + 1 - i);
11316 504 : maybe_push_decl (t);
11317 504 : TREE_TYPE (t) = TREE_TYPE (tt);
11318 504 : layout_decl (t, 0);
11319 504 : if (!processing_template_decl)
11320 : {
11321 383 : SET_DECL_VALUE_EXPR (t, tt);
11322 383 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11323 : }
11324 : else
11325 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11326 504 : j++;
11327 504 : continue;
11328 504 : }
11329 : }
11330 62435 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
11331 62435 : layout_decl (v[i], 0);
11332 62435 : if (!processing_template_decl)
11333 : {
11334 43263 : SET_DECL_VALUE_EXPR (v[i], tt);
11335 43263 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11336 : }
11337 62435 : i++;
11338 62435 : j++;
11339 : }
11340 31070 : if (pack != -1 && j == (unsigned) pack)
11341 : {
11342 20 : gcc_checking_assert (eltscnt == count - 1);
11343 20 : packv = make_tree_vec (2);
11344 : }
11345 : }
11346 88101 : done:
11347 88101 : if (packv)
11348 : {
11349 10832 : gcc_checking_assert (pack != -1);
11350 10832 : TREE_VEC_ELT (packv, 0) = decl;
11351 10832 : TREE_VEC_ELT (packv, 1) = size_int (pack);
11352 10832 : SET_DECL_VALUE_EXPR (v[pack], packv);
11353 10832 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11354 10832 : DECL_IGNORED_P (v[pack]) = 1;
11355 10832 : if (!processing_template_decl)
11356 43481 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11357 32954 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
11358 : }
11359 88101 : if (processing_template_decl)
11360 : {
11361 59015 : for (unsigned int i = 0; i < count; i++)
11362 39393 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11363 : {
11364 38941 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
11365 : NULL_TREE, NULL_TREE);
11366 38941 : SET_DECL_VALUE_EXPR (v[i], a);
11367 38941 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11368 : }
11369 : }
11370 68479 : else if (DECL_NAMESPACE_SCOPE_P (decl) && !seen_error ())
11371 : {
11372 480 : tree attr = NULL_TREE, *pa = &attr;
11373 1550 : for (unsigned int i = 0; i < count; i++)
11374 1070 : if ((unsigned) pack != i
11375 1070 : && DECL_HAS_VALUE_EXPR_P (v[i])
11376 1796 : && !DECL_IGNORED_P (v[i]))
11377 : {
11378 726 : (*debug_hooks->early_global_decl) (v[i]);
11379 726 : *pa = build_tree_list (NULL_TREE, v[i]);
11380 726 : pa = &TREE_CHAIN (*pa);
11381 : }
11382 480 : if (attr)
11383 364 : DECL_ATTRIBUTES (decl)
11384 728 : = tree_cons (get_identifier ("structured bindings"),
11385 364 : attr, DECL_ATTRIBUTES (decl));
11386 : }
11387 : return false;
11388 : }
11389 :
11390 : /* Returns a declaration for a VAR_DECL as if:
11391 :
11392 : extern "C" TYPE NAME;
11393 :
11394 : had been seen. Used to create compiler-generated global
11395 : variables. */
11396 :
11397 : static tree
11398 2318 : declare_global_var (tree name, tree type)
11399 : {
11400 2318 : auto cookie = push_abi_namespace (global_namespace);
11401 2318 : tree decl = build_decl (input_location, VAR_DECL, name, type);
11402 2318 : TREE_PUBLIC (decl) = 1;
11403 2318 : DECL_EXTERNAL (decl) = 1;
11404 2318 : DECL_ARTIFICIAL (decl) = 1;
11405 2318 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11406 : /* If the user has explicitly declared this variable (perhaps
11407 : because the code we are compiling is part of a low-level runtime
11408 : library), then it is possible that our declaration will be merged
11409 : with theirs by pushdecl. */
11410 2318 : decl = pushdecl (decl);
11411 2318 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
11412 2318 : pop_abi_namespace (cookie, global_namespace);
11413 :
11414 2318 : return decl;
11415 : }
11416 :
11417 : /* Returns the type for the argument to "atexit" corresponding to the function
11418 : to be called when the program exits. */
11419 :
11420 : static tree
11421 24 : get_atexit_fn_ptr_type ()
11422 : {
11423 24 : if (!atexit_fn_ptr_type_node)
11424 : {
11425 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11426 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11427 : }
11428 :
11429 24 : return atexit_fn_ptr_type_node;
11430 : }
11431 :
11432 : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11433 : or "__cxa_throw" corresponding to the destructor to be called when the
11434 : program exits. */
11435 :
11436 : tree
11437 19668 : get_cxa_atexit_fn_ptr_type ()
11438 : {
11439 19668 : if (!cleanup_type)
11440 : {
11441 14888 : tree fntype = build_function_type_list (void_type_node,
11442 : ptr_type_node, NULL_TREE);
11443 14888 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11444 14888 : cleanup_type = build_pointer_type (fntype);
11445 : }
11446 :
11447 19668 : return cleanup_type;
11448 : }
11449 :
11450 : /* Returns a pointer to the `atexit' function. Note that if
11451 : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11452 : `__cxa_atexit' function specified in the IA64 C++ ABI. */
11453 :
11454 : static tree
11455 3514 : get_atexit_node (void)
11456 : {
11457 3514 : tree atexit_fndecl;
11458 3514 : tree fn_type;
11459 3514 : tree fn_ptr_type;
11460 3514 : const char *name;
11461 3514 : bool use_aeabi_atexit;
11462 3514 : tree ctx = global_namespace;
11463 :
11464 3514 : if (atexit_node)
11465 : return atexit_node;
11466 :
11467 2281 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11468 : {
11469 : /* The declaration for `__cxa_atexit' is:
11470 :
11471 : int __cxa_atexit (void (*)(void *), void *, void *)
11472 :
11473 : We build up the argument types and then the function type
11474 : itself. */
11475 2269 : tree argtype0, argtype1, argtype2;
11476 :
11477 2269 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11478 : /* First, build the pointer-to-function type for the first
11479 : argument. */
11480 2269 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11481 : /* Then, build the rest of the argument types. */
11482 2269 : argtype2 = ptr_type_node;
11483 2269 : if (use_aeabi_atexit)
11484 : {
11485 : argtype1 = fn_ptr_type;
11486 : argtype0 = ptr_type_node;
11487 : }
11488 : else
11489 : {
11490 2269 : argtype1 = ptr_type_node;
11491 2269 : argtype0 = fn_ptr_type;
11492 : }
11493 : /* And the final __cxa_atexit type. */
11494 2269 : fn_type = build_function_type_list (integer_type_node,
11495 : argtype0, argtype1, argtype2,
11496 : NULL_TREE);
11497 : /* ... which needs noexcept. */
11498 2269 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11499 2269 : if (use_aeabi_atexit)
11500 : {
11501 0 : name = "__aeabi_atexit";
11502 0 : push_to_top_level ();
11503 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
11504 0 : ctx = current_namespace;
11505 0 : while (n--)
11506 0 : pop_namespace ();
11507 0 : pop_from_top_level ();
11508 : }
11509 : else
11510 : {
11511 2269 : name = "__cxa_atexit";
11512 2269 : ctx = abi_node;
11513 : }
11514 : }
11515 : else
11516 : {
11517 : /* The declaration for `atexit' is:
11518 :
11519 : int atexit (void (*)());
11520 :
11521 : We build up the argument types and then the function type
11522 : itself. */
11523 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
11524 : /* Build the final atexit type. */
11525 12 : fn_type = build_function_type_list (integer_type_node,
11526 : fn_ptr_type, NULL_TREE);
11527 : /* ... which needs noexcept. */
11528 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11529 12 : name = "atexit";
11530 : }
11531 :
11532 : /* Now, build the function declaration. */
11533 2281 : push_lang_context (lang_name_c);
11534 2281 : auto cookie = push_abi_namespace (ctx);
11535 2281 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
11536 2281 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11537 : /* Install as hidden builtin so we're (a) more relaxed about
11538 : exception spec matching and (b) will not give a confusing location
11539 : in diagnostic and (c) won't magically appear in user-visible name
11540 : lookups. */
11541 2281 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11542 2281 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11543 2281 : pop_abi_namespace (cookie, ctx);
11544 2281 : mark_used (atexit_fndecl);
11545 2281 : pop_lang_context ();
11546 2281 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11547 :
11548 2281 : return atexit_node;
11549 : }
11550 :
11551 : /* Like get_atexit_node, but for thread-local cleanups. */
11552 :
11553 : static tree
11554 98 : get_thread_atexit_node (void)
11555 : {
11556 98 : if (thread_atexit_node)
11557 : return thread_atexit_node;
11558 :
11559 : /* The declaration for `__cxa_thread_atexit' is:
11560 :
11561 : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11562 59 : tree fn_type = build_function_type_list (integer_type_node,
11563 : get_cxa_atexit_fn_ptr_type (),
11564 : ptr_type_node, ptr_type_node,
11565 : NULL_TREE);
11566 :
11567 : /* Now, build the function declaration, as with __cxa_atexit. */
11568 59 : unsigned flags = push_abi_namespace ();
11569 59 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
11570 : ECF_LEAF | ECF_NOTHROW);
11571 59 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11572 59 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11573 59 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11574 59 : pop_abi_namespace (flags);
11575 59 : mark_used (atexit_fndecl);
11576 59 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11577 :
11578 59 : return thread_atexit_node;
11579 : }
11580 :
11581 : /* Returns the __dso_handle VAR_DECL. */
11582 :
11583 : static tree
11584 3600 : get_dso_handle_node (void)
11585 : {
11586 3600 : if (dso_handle_node)
11587 : return dso_handle_node;
11588 :
11589 : /* Declare the variable. */
11590 2318 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11591 : ptr_type_node);
11592 :
11593 : #ifdef HAVE_GAS_HIDDEN
11594 2318 : if (dso_handle_node != error_mark_node)
11595 : {
11596 2315 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11597 2315 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11598 : }
11599 : #endif
11600 :
11601 2318 : return dso_handle_node;
11602 : }
11603 :
11604 : /* Begin a new function with internal linkage whose job will be simply
11605 : to destroy some particular DECL. OB_PARM is true if object pointer
11606 : is passed to the cleanup function, otherwise no argument is passed. */
11607 :
11608 : static tree
11609 383 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11610 : {
11611 383 : push_to_top_level ();
11612 :
11613 : /* No need to mangle this. */
11614 383 : push_lang_context (lang_name_c);
11615 :
11616 : /* Build the name of the function. */
11617 383 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11618 383 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11619 383 : dname = targetm.strip_name_encoding (dname);
11620 763 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11621 :
11622 383 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11623 : : get_atexit_fn_ptr_type ());
11624 : /* Build the function declaration. */
11625 383 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11626 383 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11627 : /* It's a function with internal linkage, generated by the
11628 : compiler. */
11629 383 : TREE_PUBLIC (fndecl) = 0;
11630 383 : DECL_ARTIFICIAL (fndecl) = 1;
11631 : /* Make the function `inline' so that it is only emitted if it is
11632 : actually needed. It is unlikely that it will be inlined, since
11633 : it is only called via a function pointer, but we avoid unnecessary
11634 : emissions this way. */
11635 383 : DECL_DECLARED_INLINE_P (fndecl) = 1;
11636 383 : DECL_INTERFACE_KNOWN (fndecl) = 1;
11637 383 : if (ob_parm)
11638 : {
11639 : /* Build the parameter. */
11640 371 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11641 371 : TREE_USED (parmdecl) = 1;
11642 371 : DECL_READ_P (parmdecl) = 1;
11643 371 : DECL_ARGUMENTS (fndecl) = parmdecl;
11644 : }
11645 :
11646 383 : fndecl = pushdecl (fndecl, /*hidden=*/true);
11647 383 : if (omp_target)
11648 : {
11649 3 : DECL_ATTRIBUTES (fndecl)
11650 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11651 3 : DECL_ATTRIBUTES (fndecl));
11652 3 : DECL_ATTRIBUTES (fndecl)
11653 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11654 3 : DECL_ATTRIBUTES (fndecl));
11655 : }
11656 383 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11657 :
11658 383 : pop_lang_context ();
11659 :
11660 383 : return current_function_decl;
11661 : }
11662 :
11663 : /* Finish the cleanup function begun by start_cleanup_fn. */
11664 :
11665 : static void
11666 383 : end_cleanup_fn (void)
11667 : {
11668 383 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
11669 :
11670 383 : pop_from_top_level ();
11671 383 : }
11672 :
11673 : /* Generate code to handle the destruction of DECL, an object with
11674 : static storage duration. */
11675 :
11676 : tree
11677 15514 : register_dtor_fn (tree decl, bool omp_target)
11678 : {
11679 15514 : tree cleanup;
11680 15514 : tree addr;
11681 15514 : tree compound_stmt;
11682 15514 : tree fcall;
11683 15514 : tree type;
11684 15514 : bool ob_parm, dso_parm, use_dtor;
11685 15514 : tree arg0, arg1, arg2;
11686 15514 : tree atex_node;
11687 :
11688 15514 : type = TREE_TYPE (decl);
11689 15514 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11690 11902 : return void_node;
11691 :
11692 3612 : if (decl_maybe_constant_destruction (decl, type)
11693 3612 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11694 : {
11695 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11696 0 : return void_node;
11697 : }
11698 :
11699 : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11700 : "__aeabi_atexit"), and DECL is a class object, we can just pass the
11701 : destructor to "__cxa_atexit"; we don't have to build a temporary
11702 : function to do the cleanup. */
11703 7224 : dso_parm = (flag_use_cxa_atexit
11704 3612 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
11705 3612 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11706 3600 : use_dtor = ob_parm && CLASS_TYPE_P (type);
11707 3612 : if (use_dtor)
11708 : {
11709 3229 : cleanup = get_class_binding (type, complete_dtor_identifier);
11710 :
11711 : /* Make sure it is accessible. */
11712 3229 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11713 : tf_warning_or_error);
11714 : }
11715 : else
11716 : {
11717 : /* Call build_cleanup before we enter the anonymous function so
11718 : that any access checks will be done relative to the current
11719 : scope, rather than the scope of the anonymous function. */
11720 383 : build_cleanup (decl);
11721 :
11722 : /* Now start the function. */
11723 383 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11724 :
11725 : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11726 : to the original function, rather than the anonymous one. That
11727 : will make the back end think that nested functions are in use,
11728 : which causes confusion. */
11729 383 : push_deferring_access_checks (dk_no_check);
11730 383 : fcall = build_cleanup (decl);
11731 383 : pop_deferring_access_checks ();
11732 :
11733 : /* Create the body of the anonymous function. */
11734 383 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11735 383 : finish_expr_stmt (fcall);
11736 383 : finish_compound_stmt (compound_stmt);
11737 383 : end_cleanup_fn ();
11738 : }
11739 :
11740 : /* Call atexit with the cleanup function. */
11741 3612 : mark_used (cleanup);
11742 3612 : cleanup = build_address (cleanup);
11743 :
11744 3612 : if (CP_DECL_THREAD_LOCAL_P (decl))
11745 98 : atex_node = get_thread_atexit_node ();
11746 : else
11747 3514 : atex_node = get_atexit_node ();
11748 :
11749 3612 : if (use_dtor)
11750 : {
11751 : /* We must convert CLEANUP to the type that "__cxa_atexit"
11752 : expects. */
11753 3229 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11754 : /* "__cxa_atexit" will pass the address of DECL to the
11755 : cleanup function. */
11756 3229 : mark_used (decl);
11757 3229 : addr = build_address (decl);
11758 : /* The declared type of the parameter to "__cxa_atexit" is
11759 : "void *". For plain "T*", we could just let the
11760 : machinery in cp_build_function_call convert it -- but if the
11761 : type is "cv-qualified T *", then we need to convert it
11762 : before passing it in, to avoid spurious errors. */
11763 3229 : addr = build_nop (ptr_type_node, addr);
11764 : }
11765 : else
11766 : /* Since the cleanup functions we build ignore the address
11767 : they're given, there's no reason to pass the actual address
11768 : in, and, in general, it's cheaper to pass NULL than any
11769 : other value. */
11770 383 : addr = null_pointer_node;
11771 :
11772 3612 : if (dso_parm)
11773 3600 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
11774 : tf_warning_or_error);
11775 12 : else if (ob_parm)
11776 : /* Just pass NULL to the dso handle parm if we don't actually
11777 : have a DSO handle on this target. */
11778 0 : arg2 = null_pointer_node;
11779 : else
11780 : arg2 = NULL_TREE;
11781 :
11782 3600 : if (ob_parm)
11783 : {
11784 3600 : if (!CP_DECL_THREAD_LOCAL_P (decl)
11785 3600 : && targetm.cxx.use_aeabi_atexit ())
11786 : {
11787 : arg1 = cleanup;
11788 : arg0 = addr;
11789 : }
11790 : else
11791 : {
11792 : arg1 = addr;
11793 : arg0 = cleanup;
11794 : }
11795 : }
11796 : else
11797 : {
11798 : arg0 = cleanup;
11799 : arg1 = NULL_TREE;
11800 : }
11801 3612 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11802 3612 : arg0, arg1, arg2, NULL_TREE);
11803 : }
11804 :
11805 : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
11806 : is its initializer. Generate code to handle the construction
11807 : and destruction of DECL. */
11808 :
11809 : static void
11810 30224523 : expand_static_init (tree decl, tree init)
11811 : {
11812 30224523 : gcc_assert (VAR_P (decl));
11813 30224523 : gcc_assert (TREE_STATIC (decl));
11814 :
11815 : /* Some variables require no dynamic initialization. */
11816 30224523 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11817 : {
11818 : /* Make sure the destructor is callable. */
11819 30220946 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11820 30220946 : if (!init)
11821 : return;
11822 : }
11823 :
11824 16378 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11825 15452 : && !DECL_FUNCTION_SCOPE_P (decl))
11826 : {
11827 15 : auto_diagnostic_group d;
11828 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11829 15 : if (init)
11830 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11831 : "needs dynamic initialization", decl);
11832 : else
11833 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11834 : "has a non-trivial destructor", decl);
11835 15 : static bool informed;
11836 15 : if (!informed)
11837 : {
11838 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11839 : "initialization and destruction");
11840 6 : informed = true;
11841 : }
11842 15 : return;
11843 15 : }
11844 :
11845 15408 : if (DECL_FUNCTION_SCOPE_P (decl))
11846 : {
11847 : /* Emit code to perform this initialization but once. */
11848 3991 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11849 3991 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11850 3991 : tree guard, guard_addr;
11851 3991 : tree flag, begin;
11852 : /* We don't need thread-safety code for thread-local vars. */
11853 3991 : bool thread_guard = (flag_threadsafe_statics
11854 3991 : && !CP_DECL_THREAD_LOCAL_P (decl));
11855 :
11856 : /* Emit code to perform this initialization but once. This code
11857 : looks like:
11858 :
11859 : static <type> guard;
11860 : if (!__atomic_load (guard.first_byte)) {
11861 : if (__cxa_guard_acquire (&guard)) {
11862 : bool flag = false;
11863 : try {
11864 : // Do initialization.
11865 : flag = true; __cxa_guard_release (&guard);
11866 : // Register variable for destruction at end of program.
11867 : } catch {
11868 : if (!flag) __cxa_guard_abort (&guard);
11869 : }
11870 : }
11871 : }
11872 :
11873 : Note that the `flag' variable is only set to 1 *after* the
11874 : initialization is complete. This ensures that an exception,
11875 : thrown during the construction, will cause the variable to
11876 : reinitialized when we pass through this code again, as per:
11877 :
11878 : [stmt.dcl]
11879 :
11880 : If the initialization exits by throwing an exception, the
11881 : initialization is not complete, so it will be tried again
11882 : the next time control enters the declaration.
11883 :
11884 : This process should be thread-safe, too; multiple threads
11885 : should not be able to initialize the variable more than
11886 : once. */
11887 :
11888 : /* Create the guard variable. */
11889 3991 : guard = get_guard (decl);
11890 :
11891 : /* Begin the conditional initialization. */
11892 3991 : if_stmt = begin_if_stmt ();
11893 :
11894 3991 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11895 3991 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11896 :
11897 3991 : if (thread_guard)
11898 : {
11899 3843 : tree vfntype = NULL_TREE;
11900 3843 : tree acquire_name, release_name, abort_name;
11901 3843 : tree acquire_fn, release_fn, abort_fn;
11902 3843 : guard_addr = build_address (guard);
11903 :
11904 3843 : acquire_name = get_identifier ("__cxa_guard_acquire");
11905 3843 : release_name = get_identifier ("__cxa_guard_release");
11906 3843 : abort_name = get_identifier ("__cxa_guard_abort");
11907 3843 : acquire_fn = get_global_binding (acquire_name);
11908 3843 : release_fn = get_global_binding (release_name);
11909 3843 : abort_fn = get_global_binding (abort_name);
11910 3843 : if (!acquire_fn)
11911 2239 : acquire_fn = push_library_fn
11912 2239 : (acquire_name, build_function_type_list (integer_type_node,
11913 2239 : TREE_TYPE (guard_addr),
11914 : NULL_TREE),
11915 : NULL_TREE, ECF_NOTHROW);
11916 3843 : if (!release_fn || !abort_fn)
11917 2242 : vfntype = build_function_type_list (void_type_node,
11918 2242 : TREE_TYPE (guard_addr),
11919 : NULL_TREE);
11920 2242 : if (!release_fn)
11921 2242 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11922 : ECF_NOTHROW);
11923 3843 : if (!abort_fn)
11924 2242 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11925 : ECF_NOTHROW | ECF_LEAF);
11926 :
11927 3843 : inner_if_stmt = begin_if_stmt ();
11928 3843 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11929 : inner_if_stmt);
11930 :
11931 3843 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11932 3843 : begin = get_internal_target_expr (boolean_false_node);
11933 3843 : flag = TARGET_EXPR_SLOT (begin);
11934 :
11935 3843 : TARGET_EXPR_CLEANUP (begin)
11936 3843 : = build3 (COND_EXPR, void_type_node, flag,
11937 : void_node,
11938 : build_call_n (abort_fn, 1, guard_addr));
11939 3843 : CLEANUP_EH_ONLY (begin) = 1;
11940 :
11941 : /* Do the initialization itself. */
11942 3843 : init = add_stmt_to_compound (begin, init);
11943 3843 : init = add_stmt_to_compound (init,
11944 : build2 (MODIFY_EXPR, void_type_node,
11945 : flag, boolean_true_node));
11946 :
11947 : /* Use atexit to register a function for destroying this static
11948 : variable. Do this before calling __cxa_guard_release. */
11949 3843 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11950 :
11951 3843 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11952 : guard_addr));
11953 : }
11954 : else
11955 : {
11956 148 : init = add_stmt_to_compound (init, set_guard (guard));
11957 :
11958 : /* Use atexit to register a function for destroying this static
11959 : variable. */
11960 148 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11961 : }
11962 :
11963 3991 : finish_expr_stmt (init);
11964 :
11965 3991 : if (thread_guard)
11966 : {
11967 3843 : finish_compound_stmt (inner_then_clause);
11968 3843 : finish_then_clause (inner_if_stmt);
11969 3843 : finish_if_stmt (inner_if_stmt);
11970 : }
11971 :
11972 3991 : finish_compound_stmt (then_clause);
11973 3991 : finish_then_clause (if_stmt);
11974 3991 : finish_if_stmt (if_stmt);
11975 : }
11976 11417 : else if (CP_DECL_THREAD_LOCAL_P (decl))
11977 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
11978 : else
11979 10622 : static_aggregates = tree_cons (init, decl, static_aggregates);
11980 : }
11981 :
11982 :
11983 : /* Make TYPE a complete type based on INITIAL_VALUE.
11984 : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11985 : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11986 : 3 if the initializer list is empty (in pedantic mode). */
11987 :
11988 : int
11989 629983 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11990 : {
11991 629983 : int failure;
11992 629983 : tree type, elt_type;
11993 :
11994 : /* Don't get confused by a CONSTRUCTOR for some other type. */
11995 629953 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11996 150528 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11997 630315 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11998 : return 1;
11999 :
12000 629980 : if (initial_value)
12001 : {
12002 : /* An array of character type can be initialized from a
12003 : brace-enclosed string constant so call reshape_init to
12004 : remove the optional braces from a braced string literal. */
12005 629950 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
12006 629950 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
12007 51758 : initial_value = reshape_init (*ptype, initial_value,
12008 : tf_warning_or_error);
12009 :
12010 : /* If any of the elements are parameter packs, we can't actually
12011 : complete this type now because the array size is dependent. */
12012 629950 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
12013 49377573 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
12014 48926385 : if (PACK_EXPANSION_P (e.value))
12015 : return 0;
12016 : }
12017 :
12018 629980 : failure = complete_array_type (ptype, initial_value, do_default);
12019 :
12020 : /* We can create the array before the element type is complete, which
12021 : means that we didn't have these two bits set in the original type
12022 : either. In completing the type, we are expected to propagate these
12023 : bits. See also complete_type which does the same thing for arrays
12024 : of fixed size. */
12025 629980 : type = *ptype;
12026 1259957 : if (type != error_mark_node && TYPE_DOMAIN (type))
12027 : {
12028 629953 : elt_type = TREE_TYPE (type);
12029 629953 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
12030 1259906 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
12031 629953 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
12032 : }
12033 :
12034 : return failure;
12035 : }
12036 :
12037 : /* As above, but either give an error or reject zero-size arrays, depending
12038 : on COMPLAIN. */
12039 :
12040 : int
12041 1253 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
12042 : bool do_default, tsubst_flags_t complain)
12043 : {
12044 1253 : int failure;
12045 1253 : bool sfinae = !(complain & tf_error);
12046 : /* In SFINAE context we can't be lenient about zero-size arrays. */
12047 1253 : if (sfinae)
12048 3 : ++pedantic;
12049 1253 : failure = cp_complete_array_type (ptype, initial_value, do_default);
12050 1253 : if (sfinae)
12051 3 : --pedantic;
12052 1253 : if (failure)
12053 : {
12054 3 : if (sfinae)
12055 : /* Not an error. */;
12056 0 : else if (failure == 1)
12057 0 : error ("initializer fails to determine size of %qT", *ptype);
12058 0 : else if (failure == 2)
12059 : {
12060 0 : if (do_default)
12061 0 : error ("array size missing in %qT", *ptype);
12062 : }
12063 0 : else if (failure == 3)
12064 0 : error ("zero-size array %qT", *ptype);
12065 3 : *ptype = error_mark_node;
12066 : }
12067 1253 : return failure;
12068 : }
12069 :
12070 : /* Return zero if something is declared to be a member of type
12071 : CTYPE when in the context of CUR_TYPE. STRING is the error
12072 : message to print in that case. Otherwise, quietly return 1. */
12073 :
12074 : static int
12075 27590054 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
12076 : {
12077 27590054 : if (ctype && ctype != cur_type)
12078 : {
12079 0 : if (flags == DTOR_FLAG)
12080 0 : error ("destructor for alien class %qT cannot be a member", ctype);
12081 : else
12082 0 : error ("constructor for alien class %qT cannot be a member", ctype);
12083 : return 0;
12084 : }
12085 : return 1;
12086 : }
12087 :
12088 : /* Subroutine of `grokdeclarator'. */
12089 :
12090 : /* Generate errors possibly applicable for a given set of specifiers.
12091 : This is for ARM $7.1.2. */
12092 :
12093 : static void
12094 462951507 : bad_specifiers (tree object,
12095 : enum bad_spec_place type,
12096 : int virtualp,
12097 : int quals,
12098 : int inlinep,
12099 : int friendp,
12100 : int raises,
12101 : const location_t* locations)
12102 : {
12103 462951507 : switch (type)
12104 : {
12105 71599851 : case BSP_VAR:
12106 71599851 : if (virtualp)
12107 0 : error_at (locations[ds_virtual],
12108 : "%qD declared as a %<virtual%> variable", object);
12109 71599851 : if (quals)
12110 3 : error ("%<const%> and %<volatile%> function specifiers on "
12111 : "%qD invalid in variable declaration", object);
12112 : break;
12113 307419765 : case BSP_PARM:
12114 307419765 : if (virtualp)
12115 0 : error_at (locations[ds_virtual],
12116 : "%qD declared as a %<virtual%> parameter", object);
12117 307419765 : if (inlinep)
12118 6 : error_at (locations[ds_inline],
12119 : "%qD declared as an %<inline%> parameter", object);
12120 307419765 : if (quals)
12121 3 : error ("%<const%> and %<volatile%> function specifiers on "
12122 : "%qD invalid in parameter declaration", object);
12123 : break;
12124 49397395 : case BSP_TYPE:
12125 49397395 : if (virtualp)
12126 0 : error_at (locations[ds_virtual],
12127 : "%qD declared as a %<virtual%> type", object);
12128 49397395 : if (inlinep)
12129 6 : error_at (locations[ds_inline],
12130 : "%qD declared as an %<inline%> type", object);
12131 49397395 : if (quals)
12132 0 : error ("%<const%> and %<volatile%> function specifiers on "
12133 : "%qD invalid in type declaration", object);
12134 : break;
12135 34534496 : case BSP_FIELD:
12136 34534496 : if (virtualp)
12137 6 : error_at (locations[ds_virtual],
12138 : "%qD declared as a %<virtual%> field", object);
12139 34534496 : if (inlinep)
12140 6 : error_at (locations[ds_inline],
12141 : "%qD declared as an %<inline%> field", object);
12142 34534496 : if (quals)
12143 0 : error ("%<const%> and %<volatile%> function specifiers on "
12144 : "%qD invalid in field declaration", object);
12145 : break;
12146 0 : default:
12147 0 : gcc_unreachable();
12148 : }
12149 462951507 : if (friendp)
12150 0 : error ("%q+D declared as a friend", object);
12151 462951507 : if (raises
12152 131 : && !flag_noexcept_type
12153 462951532 : && (TREE_CODE (object) == TYPE_DECL
12154 19 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
12155 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
12156 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
12157 6 : error ("%q+D declared with an exception specification", object);
12158 462951507 : }
12159 :
12160 : /* DECL is a member function or static data member and is presently
12161 : being defined. Check that the definition is taking place in a
12162 : valid namespace. */
12163 :
12164 : static void
12165 101879623 : check_class_member_definition_namespace (tree decl)
12166 : {
12167 : /* These checks only apply to member functions and static data
12168 : members. */
12169 101879623 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
12170 : /* We check for problems with specializations in pt.cc in
12171 : check_specialization_namespace, where we can issue better
12172 : diagnostics. */
12173 101879623 : if (processing_specialization)
12174 : return;
12175 : /* We check this in check_explicit_instantiation_namespace. */
12176 101734426 : if (processing_explicit_instantiation)
12177 : return;
12178 : /* [class.mfct]
12179 :
12180 : A member function definition that appears outside of the
12181 : class definition shall appear in a namespace scope enclosing
12182 : the class definition.
12183 :
12184 : [class.static.data]
12185 :
12186 : The definition for a static data member shall appear in a
12187 : namespace scope enclosing the member's class definition. */
12188 101734346 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
12189 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
12190 18 : decl, DECL_CONTEXT (decl));
12191 : }
12192 :
12193 : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
12194 : METHOD_TYPE for a non-static member function; QUALS are the
12195 : cv-qualifiers that apply to the function. */
12196 :
12197 : tree
12198 246764990 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
12199 : {
12200 246764990 : tree this_type;
12201 246764990 : tree qual_type;
12202 246764990 : tree parm;
12203 246764990 : cp_cv_quals this_quals;
12204 :
12205 246764990 : if (CLASS_TYPE_P (type))
12206 : {
12207 107684066 : this_type
12208 107684066 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
12209 107684066 : this_type = build_pointer_type (this_type);
12210 : }
12211 : else
12212 139080924 : this_type = type_of_this_parm (type);
12213 : /* The `this' parameter is implicitly `const'; it cannot be
12214 : assigned to. */
12215 246764990 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
12216 246764990 : qual_type = cp_build_qualified_type (this_type, this_quals);
12217 246764990 : parm = build_artificial_parm (fn, this_identifier, qual_type);
12218 246764990 : cp_apply_type_quals_to_decl (this_quals, parm);
12219 246764990 : return parm;
12220 : }
12221 :
12222 : /* DECL is a static member function. Complain if it was declared
12223 : with function-cv-quals. */
12224 :
12225 : static void
12226 11148601 : check_static_quals (tree decl, cp_cv_quals quals)
12227 : {
12228 0 : if (quals != TYPE_UNQUALIFIED)
12229 6 : error ("static member function %q#D declared with type qualifiers",
12230 : decl);
12231 0 : }
12232 :
12233 : /* Helper function. Replace the temporary this parameter injected
12234 : during cp_finish_omp_declare_simd with the real this parameter. */
12235 :
12236 : static tree
12237 2692 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
12238 : {
12239 2692 : tree this_parm = (tree) data;
12240 2692 : if (TREE_CODE (*tp) == PARM_DECL
12241 450 : && DECL_NAME (*tp) == this_identifier
12242 2965 : && *tp != this_parm)
12243 273 : *tp = this_parm;
12244 2419 : else if (TYPE_P (*tp))
12245 33 : *walk_subtrees = 0;
12246 2692 : return NULL_TREE;
12247 : }
12248 :
12249 : /* CTYPE is class type, or null if non-class.
12250 : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
12251 : or METHOD_TYPE.
12252 : DECLARATOR is the function's name.
12253 : PARMS is a chain of PARM_DECLs for the function.
12254 : VIRTUALP is truthvalue of whether the function is virtual or not.
12255 : FLAGS are to be passed through to `grokclassfn'.
12256 : QUALS are qualifiers indicating whether the function is `const'
12257 : or `volatile'.
12258 : RAISES is a list of exceptions that this function can raise.
12259 : CHECK is 1 if we must find this method in CTYPE, 0 if we should
12260 : not look, and -1 if we should not call `grokclassfn' at all.
12261 :
12262 : SFK is the kind of special function (if any) for the new function.
12263 :
12264 : Returns `NULL_TREE' if something goes wrong, after issuing
12265 : applicable error messages. */
12266 :
12267 : static tree
12268 196630825 : grokfndecl (tree ctype,
12269 : tree type,
12270 : tree declarator,
12271 : tree parms,
12272 : tree orig_declarator,
12273 : const cp_decl_specifier_seq *declspecs,
12274 : tree decl_reqs,
12275 : int virtualp,
12276 : enum overload_flags flags,
12277 : cp_cv_quals quals,
12278 : cp_ref_qualifier rqual,
12279 : tree raises,
12280 : int check,
12281 : int friendp,
12282 : int publicp,
12283 : int inlinep,
12284 : int initialized,
12285 : bool xobj_func_p,
12286 : special_function_kind sfk,
12287 : bool funcdef_flag,
12288 : bool late_return_type_p,
12289 : int template_count,
12290 : tree in_namespace,
12291 : tree* attrlist,
12292 : tree contract_specifiers,
12293 : location_t location)
12294 : {
12295 196630825 : tree decl;
12296 196630825 : tree t;
12297 :
12298 196630825 : if (location == UNKNOWN_LOCATION)
12299 0 : location = input_location;
12300 :
12301 : /* Was the concept specifier present? */
12302 196630825 : bool concept_p = inlinep & 4;
12303 :
12304 196630825 : if (concept_p)
12305 : {
12306 15 : error_at (location, "function concepts are no longer supported");
12307 15 : return NULL_TREE;
12308 : }
12309 :
12310 : /* [except.spec]/9 - A deallocation function with no explicit noexcept-specifier
12311 : has a non-throwing exception specification. */
12312 196630810 : if (raises == NULL_TREE
12313 117656728 : && cxx_dialect >= cxx11
12314 141978947 : && IDENTIFIER_NEWDEL_OP_P (declarator)
12315 196696627 : && !IDENTIFIER_NEW_OP_P (declarator))
12316 852 : raises = noexcept_true_spec;
12317 :
12318 196630810 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
12319 :
12320 196630810 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
12321 :
12322 : /* Set the constraints on the declaration. */
12323 196630810 : if (flag_concepts)
12324 : {
12325 192247327 : tree tmpl_reqs = NULL_TREE;
12326 192247327 : tree ctx = friendp ? current_class_type : ctype;
12327 192247327 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
12328 192247327 : bool memtmpl = (!block_local
12329 192247327 : && (current_template_depth
12330 192188338 : > template_class_depth (ctx)));
12331 53112228 : if (memtmpl)
12332 : {
12333 53112228 : if (!current_template_parms)
12334 : /* If there are no template parameters, something must have
12335 : gone wrong. */
12336 0 : gcc_assert (seen_error ());
12337 : else
12338 53112228 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12339 : }
12340 192247327 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
12341 : /* C++20 CA378: Remove non-templated constrained functions. */
12342 : /* [temp.friend]/9 A non-template friend declaration with a
12343 : requires-clause shall be a definition. A friend function template with
12344 : a constraint that depends on a template parameter from an enclosing
12345 : template shall be a definition. */
12346 192247327 : if (ci
12347 10374275 : && (block_local
12348 10374272 : || !processing_template_decl
12349 10374211 : || (friendp && !memtmpl && !funcdef_flag)))
12350 : {
12351 73 : if (!friendp || !processing_template_decl)
12352 64 : error_at (location, "constraints on a non-templated function");
12353 : else
12354 9 : error_at (location, "constrained non-template friend declaration"
12355 : " must be a definition");
12356 : ci = NULL_TREE;
12357 : }
12358 192247327 : set_constraints (decl, ci);
12359 1295318 : if (ci && friendp && memtmpl && !funcdef_flag
12360 192285089 : && uses_outer_template_parms_in_constraints (decl, ctx))
12361 6 : error_at (location, "friend function template with constraints that "
12362 : "depend on outer template parameters must be a definition");
12363 : }
12364 :
12365 196630810 : if (TREE_CODE (type) == METHOD_TYPE)
12366 : {
12367 106154354 : tree parm = build_this_parm (decl, type, quals);
12368 106154354 : DECL_CHAIN (parm) = parms;
12369 106154354 : parms = parm;
12370 :
12371 : /* Allocate space to hold the vptr bit if needed. */
12372 106154354 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12373 : }
12374 :
12375 196630810 : DECL_ARGUMENTS (decl) = parms;
12376 593875731 : for (t = parms; t; t = DECL_CHAIN (t))
12377 : {
12378 397244921 : DECL_CONTEXT (t) = decl;
12379 397244921 : if (flag_reflection
12380 7885170 : && initialized == SD_INITIALIZED
12381 398833189 : && DECL_ATTRIBUTES (t))
12382 3606 : for (tree a = DECL_ATTRIBUTES (t);
12383 3610 : (a = lookup_annotation (a));
12384 4 : a = TREE_CHAIN (a))
12385 : {
12386 4 : gcc_checking_assert (TREE_CODE (TREE_VALUE (a)) == TREE_LIST);
12387 : /* Mark TREE_PURPOSE of the value that it is an annotation
12388 : on an argument of a function definition (rather than
12389 : annotation from function declaration). For function parameter
12390 : reflection all annotations are listed, while for variable_of
12391 : only those marked here. Annotation is marked as coming from
12392 : function definition's argument if it has TREE_PURPOSE
12393 : void_node or INTEGER_CST with signed type. */
12394 4 : tree val = TREE_VALUE (a);
12395 4 : gcc_assert (TREE_PURPOSE (val) == NULL_TREE);
12396 4 : TREE_PURPOSE (val) = void_node;
12397 : }
12398 : }
12399 :
12400 : /* Propagate volatile out from type to decl. */
12401 196630810 : if (TYPE_VOLATILE (type))
12402 0 : TREE_THIS_VOLATILE (decl) = 1;
12403 :
12404 : /* Setup decl according to sfk. */
12405 196630810 : switch (sfk)
12406 : {
12407 25191621 : case sfk_constructor:
12408 25191621 : case sfk_copy_constructor:
12409 25191621 : case sfk_move_constructor:
12410 25191621 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12411 25191621 : DECL_NAME (decl) = ctor_identifier;
12412 25191621 : break;
12413 3389632 : case sfk_destructor:
12414 3389632 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
12415 3389632 : DECL_NAME (decl) = dtor_identifier;
12416 3389632 : break;
12417 1270839 : case sfk_deduction_guide:
12418 : /* Give deduction guides a definition even though they don't really
12419 : have one: the restriction that you can't repeat a deduction guide
12420 : makes them more like a definition anyway. */
12421 1270839 : DECL_INITIAL (decl) = void_node;
12422 : /* But to ensure that external-linkage deduction guides in header units
12423 : don't fall afoul of [module.import] p6, mark them as inline. */
12424 1270839 : DECL_DECLARED_INLINE_P (decl) = true;
12425 1270839 : break;
12426 : default:
12427 : break;
12428 : }
12429 :
12430 196630810 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12431 : {
12432 100191 : if (funcdef_flag)
12433 3 : error_at (location,
12434 : "defining explicit specialization %qD in friend declaration",
12435 : orig_declarator);
12436 : else
12437 : {
12438 100188 : tree fns = TREE_OPERAND (orig_declarator, 0);
12439 100188 : tree args = TREE_OPERAND (orig_declarator, 1);
12440 :
12441 100188 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12442 : {
12443 : /* Something like `template <class T> friend void f<T>()'. */
12444 6 : error_at (location,
12445 : "invalid use of template-id %qD in declaration "
12446 : "of primary template",
12447 : orig_declarator);
12448 6 : return NULL_TREE;
12449 : }
12450 :
12451 : /* A friend declaration of the form friend void f<>(). Record
12452 : the information in the TEMPLATE_ID_EXPR. */
12453 100182 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
12454 :
12455 100182 : gcc_assert (identifier_p (fns) || OVL_P (fns));
12456 100182 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12457 :
12458 : /* Remember the befriending class like push_template_decl does for
12459 : template friends. */
12460 100182 : gcc_checking_assert (!DECL_CHAIN (decl));
12461 100182 : DECL_CHAIN (decl) = current_scope ();
12462 :
12463 323335 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12464 223158 : if (TREE_PURPOSE (t)
12465 223158 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12466 : {
12467 5 : error_at (defparse_location (TREE_PURPOSE (t)),
12468 : "default arguments are not allowed in declaration "
12469 : "of friend template specialization %qD",
12470 : decl);
12471 5 : return NULL_TREE;
12472 : }
12473 :
12474 100177 : if (inlinep & 1)
12475 : {
12476 5 : error_at (declspecs->locations[ds_inline],
12477 : "%<inline%> is not allowed in declaration of friend "
12478 : "template specialization %qD",
12479 : decl);
12480 5 : return NULL_TREE;
12481 : }
12482 : }
12483 : }
12484 :
12485 : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12486 : expression, that declaration shall be a definition..." */
12487 196630794 : if (friendp && !funcdef_flag)
12488 : {
12489 1906321 : bool has_errored = false;
12490 1906321 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12491 5342709 : t && t != void_list_node; t = TREE_CHAIN (t))
12492 3436388 : if (TREE_PURPOSE (t))
12493 : {
12494 51 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12495 : /* For templates, mark the default argument as erroneous and give a
12496 : hard error. */
12497 51 : if (processing_template_decl)
12498 : {
12499 33 : diag_kind = diagnostics::kind::error;
12500 33 : TREE_PURPOSE (t) = error_mark_node;
12501 : }
12502 51 : if (!has_errored)
12503 : {
12504 48 : has_errored = true;
12505 48 : emit_diagnostic (diag_kind,
12506 48 : DECL_SOURCE_LOCATION (decl),
12507 48 : /*diagnostics::option_id=*/0,
12508 : "friend declaration of %qD specifies default "
12509 : "arguments and isn%'t a definition", decl);
12510 : }
12511 : }
12512 : }
12513 :
12514 : /* If this decl has namespace scope, set that up. */
12515 196630794 : if (in_namespace)
12516 264748 : set_decl_namespace (decl, in_namespace, friendp);
12517 196366046 : else if (ctype)
12518 117214102 : DECL_CONTEXT (decl) = ctype;
12519 : else
12520 79151944 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12521 :
12522 : /* `main' and builtins have implicit 'C' linkage. */
12523 196630794 : if (ctype == NULL_TREE
12524 79416692 : && DECL_FILE_SCOPE_P (decl)
12525 32666196 : && current_lang_name == lang_name_cplusplus
12526 199690837 : && (MAIN_NAME_P (declarator)
12527 3025786 : || (IDENTIFIER_LENGTH (declarator) > 10
12528 2172201 : && IDENTIFIER_POINTER (declarator)[0] == '_'
12529 1677908 : && IDENTIFIER_POINTER (declarator)[1] == '_'
12530 360554 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
12531 : "builtin_"))
12532 3025768 : || (targetcm.cxx_implicit_extern_c
12533 0 : && (targetcm.cxx_implicit_extern_c
12534 0 : (IDENTIFIER_POINTER (declarator))))))
12535 34275 : SET_DECL_LANGUAGE (decl, lang_c);
12536 :
12537 196630794 : DECL_STATIC_FUNCTION_P (decl)
12538 196630794 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12539 196630794 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12540 :
12541 196630794 : if (initialized == SD_DELETED)
12542 4361804 : DECL_DELETED_FN (decl) = 1;
12543 :
12544 196630794 : if (ctype && funcdef_flag)
12545 101357915 : check_class_member_definition_namespace (decl);
12546 :
12547 196630794 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12548 : {
12549 34263 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
12550 9 : error_at (location, "cannot declare %<::main%> to be a template");
12551 34263 : if (inlinep & 1)
12552 3 : error_at (declspecs->locations[ds_inline],
12553 : "cannot declare %<::main%> to be inline");
12554 34263 : if (inlinep & 2)
12555 6 : error_at (declspecs->locations[ds_constexpr],
12556 : "cannot declare %<::main%> to be %qs", "constexpr");
12557 34263 : if (inlinep & 8)
12558 3 : error_at (declspecs->locations[ds_consteval],
12559 : "cannot declare %<::main%> to be %qs", "consteval");
12560 34263 : if (!publicp)
12561 0 : error_at (location, "cannot declare %<::main%> to be static");
12562 34263 : if (current_lang_name != lang_name_cplusplus)
12563 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12564 : " linkage specification other than %<extern \"C++\"%>");
12565 34263 : if (module_attach_p ())
12566 : {
12567 3 : auto_diagnostic_group adg;
12568 3 : error_at (location, "cannot attach %<::main%> to a named module");
12569 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
12570 : "global module instead");
12571 3 : }
12572 : inlinep = 0;
12573 : publicp = 1;
12574 : }
12575 :
12576 : /* Members of anonymous types and local classes have no linkage; make
12577 : them internal. If a typedef is made later, this will be changed. */
12578 196630794 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12579 : publicp = 0;
12580 196616299 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12581 : /* But members of local classes in a module CMI should have their
12582 : definitions exported, in case they are (directly or indirectly)
12583 : used by an importer. We don't just use module_has_cmi_p here
12584 : because for entities in the GMF we don't yet know whether this
12585 : module will have a CMI, so we'll conservatively assume it might. */
12586 2449135 : publicp = module_maybe_has_cmi_p ();
12587 :
12588 196616299 : if (publicp && cxx_dialect == cxx98)
12589 : {
12590 : /* [basic.link]: A name with no linkage (notably, the name of a class
12591 : or enumeration declared in a local scope) shall not be used to
12592 : declare an entity with linkage.
12593 :
12594 : DR 757 relaxes this restriction for C++0x. */
12595 1169381 : no_linkage_error (decl);
12596 : }
12597 :
12598 196630794 : TREE_PUBLIC (decl) = publicp;
12599 196630794 : if (! publicp)
12600 : {
12601 2569444 : DECL_INTERFACE_KNOWN (decl) = 1;
12602 2569444 : DECL_NOT_REALLY_EXTERN (decl) = 1;
12603 : }
12604 :
12605 : /* If the declaration was declared inline, mark it as such. */
12606 196630794 : if (inlinep)
12607 : {
12608 75894591 : DECL_DECLARED_INLINE_P (decl) = 1;
12609 75894591 : if (publicp)
12610 75631186 : DECL_COMDAT (decl) = 1;
12611 : }
12612 75894591 : if (inlinep & 2)
12613 56110282 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12614 140520512 : else if (inlinep & 8)
12615 : {
12616 1168704 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12617 1168704 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12618 : }
12619 :
12620 196630794 : DECL_EXTERNAL (decl) = 1;
12621 196630794 : if (TREE_CODE (type) == FUNCTION_TYPE)
12622 : {
12623 90476440 : if (quals || rqual)
12624 393 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12625 : TYPE_UNQUALIFIED,
12626 : REF_QUAL_NONE);
12627 90476440 : auto_diagnostic_group d;
12628 90476440 : if (quals)
12629 621 : error (!ctype
12630 : ? G_("non-member function %qD cannot have cv-qualifier")
12631 : : !xobj_func_p
12632 297 : ? G_("static member function %qD cannot have cv-qualifier")
12633 : : G_("explicit object member function "
12634 : "%qD cannot have cv-qualifier"),
12635 : decl);
12636 90476440 : if (rqual)
12637 531 : error (!ctype
12638 : ? G_("non-member function %qD cannot have ref-qualifier")
12639 : : !xobj_func_p
12640 264 : ? G_("static member function %qD cannot have ref-qualifier")
12641 : : G_("explicit object member function "
12642 : "%qD cannot have ref-qualifier"),
12643 : decl);
12644 :
12645 90476440 : if (xobj_func_p && (quals || rqual))
12646 363 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12647 : "explicit object parameter declared here");
12648 90476440 : quals = TYPE_UNQUALIFIED;
12649 90476440 : rqual = REF_QUAL_NONE;
12650 :
12651 90476440 : }
12652 :
12653 196630794 : if (deduction_guide_p (decl))
12654 : {
12655 1270839 : tree type = TREE_TYPE (DECL_NAME (decl));
12656 1270839 : if (in_namespace == NULL_TREE
12657 1270839 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12658 : {
12659 3 : auto_diagnostic_group d;
12660 3 : error_at (location, "deduction guide %qD must be declared in the "
12661 : "same scope as %qT", decl, type);
12662 3 : inform (location_of (type), " declared here");
12663 3 : return NULL_TREE;
12664 3 : }
12665 2541672 : if (DECL_CLASS_SCOPE_P (decl)
12666 1270877 : && current_access_specifier != declared_access (TYPE_NAME (type)))
12667 : {
12668 3 : auto_diagnostic_group d;
12669 3 : error_at (location, "deduction guide %qD must have the same access "
12670 : "as %qT", decl, type);
12671 3 : inform (location_of (type), " declared here");
12672 3 : }
12673 1270836 : if (funcdef_flag)
12674 3 : error_at (location,
12675 : "deduction guide %qD must not have a function body", decl);
12676 : }
12677 364309070 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12678 207684700 : && !grok_op_properties (decl, /*complain=*/true))
12679 : return NULL_TREE;
12680 195359808 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
12681 : {
12682 319601 : bool long_long_unsigned_p;
12683 319601 : bool long_double_p;
12684 319601 : const char *suffix = NULL;
12685 : /* [over.literal]/6: Literal operators shall not have C linkage. */
12686 319601 : if (DECL_LANGUAGE (decl) == lang_c)
12687 : {
12688 6 : auto_diagnostic_group d;
12689 6 : error_at (location, "literal operator with C linkage");
12690 6 : maybe_show_extern_c_location ();
12691 6 : return NULL_TREE;
12692 6 : }
12693 :
12694 319595 : if (DECL_NAMESPACE_SCOPE_P (decl))
12695 : {
12696 319589 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12697 : &long_double_p))
12698 : {
12699 42 : error_at (location, "%qD has invalid argument list", decl);
12700 42 : return NULL_TREE;
12701 : }
12702 :
12703 319547 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12704 319547 : if (long_long_unsigned_p)
12705 : {
12706 47244 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
12707 68 : warning_at (location, 0, "integer suffix %qs"
12708 : " shadowed by implementation", suffix);
12709 : }
12710 272303 : else if (long_double_p)
12711 : {
12712 86813 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
12713 80 : warning_at (location, 0, "floating-point suffix %qs"
12714 : " shadowed by implementation", suffix);
12715 : }
12716 : /* 17.6.3.3.5 */
12717 319547 : if (suffix[0] != '_'
12718 318940 : && !current_function_decl && !(friendp && !funcdef_flag))
12719 318934 : warning_at (location, OPT_Wliteral_suffix,
12720 : "literal operator suffixes not preceded by %<_%>"
12721 : " are reserved for future standardization");
12722 : }
12723 : else
12724 : {
12725 6 : error_at (location, "%qD must be a non-member function", decl);
12726 6 : return NULL_TREE;
12727 : }
12728 : }
12729 :
12730 195359757 : if (funcdef_flag)
12731 : /* Make the init_value nonzero so pushdecl knows this is not
12732 : tentative. error_mark_node is replaced later with the BLOCK. */
12733 138727249 : DECL_INITIAL (decl) = error_mark_node;
12734 :
12735 196630590 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12736 74447553 : TREE_NOTHROW (decl) = 1;
12737 :
12738 196630590 : if (flag_openmp || flag_openmp_simd)
12739 : {
12740 : /* Adjust "omp declare simd" attributes. */
12741 480753 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
12742 480753 : if (ods)
12743 : {
12744 : tree attr;
12745 2033 : for (attr = ods; attr;
12746 1078 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
12747 : {
12748 1078 : if (TREE_CODE (type) == METHOD_TYPE)
12749 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12750 : DECL_ARGUMENTS (decl), NULL);
12751 1078 : if (TREE_VALUE (attr) != NULL_TREE)
12752 : {
12753 872 : tree cl = TREE_VALUE (TREE_VALUE (attr));
12754 872 : cl = c_omp_declare_simd_clauses_to_numbers
12755 872 : (DECL_ARGUMENTS (decl), cl);
12756 872 : if (cl)
12757 872 : TREE_VALUE (TREE_VALUE (attr)) = cl;
12758 : else
12759 0 : TREE_VALUE (attr) = NULL_TREE;
12760 : }
12761 : }
12762 : }
12763 : }
12764 :
12765 : /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
12766 : have a potentially throwing exception specification. */
12767 196630590 : maybe_diagnose_deallocation_noexcept_false (decl);
12768 :
12769 : /* Caller will do the rest of this. */
12770 196630590 : if (check < 0)
12771 : {
12772 6778352 : if (decl && decl != error_mark_node && contract_specifiers)
12773 24 : set_fn_contract_specifiers (decl, contract_specifiers);
12774 6778352 : return decl;
12775 : }
12776 :
12777 189852238 : if (ctype != NULL_TREE)
12778 117213783 : grokclassfn (ctype, decl, flags);
12779 :
12780 : /* 12.4/3 */
12781 189852238 : if (cxx_dialect >= cxx11
12782 377385342 : && DECL_DESTRUCTOR_P (decl)
12783 3367811 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12784 190023476 : && !processing_template_decl)
12785 87778 : deduce_noexcept_on_destructor (decl);
12786 :
12787 189852238 : set_originating_module (decl);
12788 :
12789 189852238 : decl = check_explicit_specialization (orig_declarator, decl,
12790 : template_count,
12791 189852238 : 2 * funcdef_flag +
12792 189852238 : 4 * (friendp != 0),
12793 : *attrlist);
12794 189852238 : if (decl == error_mark_node)
12795 : return NULL_TREE;
12796 :
12797 189851964 : if (DECL_STATIC_FUNCTION_P (decl))
12798 10966762 : check_static_quals (decl, quals);
12799 :
12800 189851964 : if (attrlist)
12801 : {
12802 189851964 : cplus_decl_attributes (&decl, *attrlist, 0);
12803 189851964 : *attrlist = NULL_TREE;
12804 : }
12805 :
12806 : /* Update now we have a decl and maybe know the return type. */
12807 189851964 : if (contract_specifiers)
12808 : {
12809 900 : tree t = decl;
12810 900 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12811 7 : t = DECL_TEMPLATE_RESULT (decl);
12812 900 : set_fn_contract_specifiers (t, contract_specifiers);
12813 900 : rebuild_postconditions (t);
12814 : }
12815 :
12816 : /* Check main's type after attributes have been applied. */
12817 189851964 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12818 : {
12819 34251 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12820 : integer_type_node))
12821 : {
12822 19 : tree dtype = TREE_TYPE (decl);
12823 19 : tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12824 19 : tree newtype;
12825 19 : error_at (declspecs->locations[ds_type_spec],
12826 : "%<::main%> must return %<int%>");
12827 57 : newtype = build_function_type (integer_type_node, oldtypeargs,
12828 19 : TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12829 19 : TREE_TYPE (decl) = newtype;
12830 : }
12831 34251 : if (warn_main)
12832 34229 : check_main_parameter_types (decl);
12833 : }
12834 :
12835 189851964 : if (ctype != NULL_TREE && check)
12836 : {
12837 9331341 : tree old_decl = check_classfn (ctype, decl,
12838 9331341 : (current_template_depth
12839 9331341 : > template_class_depth (ctype))
12840 2268967 : ? current_template_parms
12841 : : NULL_TREE);
12842 :
12843 9331341 : if (old_decl == error_mark_node)
12844 : return NULL_TREE;
12845 :
12846 9331233 : if (old_decl)
12847 : {
12848 8923075 : tree ok;
12849 8923075 : tree pushed_scope;
12850 :
12851 8923075 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12852 : /* Because grokfndecl is always supposed to return a
12853 : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12854 : here. We depend on our callers to figure out that its
12855 : really a template that's being returned. */
12856 2268955 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
12857 :
12858 8923075 : if (DECL_STATIC_FUNCTION_P (old_decl)
12859 8923075 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12860 : {
12861 : /* Remove the `this' parm added by grokclassfn. */
12862 181839 : revert_static_member_fn (decl);
12863 181839 : check_static_quals (decl, quals);
12864 : }
12865 8923075 : if (DECL_ARTIFICIAL (old_decl))
12866 : {
12867 9 : error ("definition of implicitly-declared %qD", old_decl);
12868 9 : return NULL_TREE;
12869 : }
12870 8923066 : else if (DECL_DEFAULTED_FN (old_decl))
12871 : {
12872 3 : auto_diagnostic_group d;
12873 3 : error ("definition of explicitly-defaulted %q+D", decl);
12874 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12875 : "%q#D explicitly defaulted here", old_decl);
12876 3 : return NULL_TREE;
12877 3 : }
12878 :
12879 8923063 : if (modules_p () && !module_may_redeclare (old_decl))
12880 : return NULL_TREE;
12881 :
12882 : /* Since we've smashed OLD_DECL to its
12883 : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12884 8923003 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12885 1211 : decl = DECL_TEMPLATE_RESULT (decl);
12886 :
12887 : /* Attempt to merge the declarations. This can fail, in
12888 : the case of some invalid specialization declarations. */
12889 8923003 : pushed_scope = push_scope (ctype);
12890 8923003 : ok = duplicate_decls (decl, old_decl);
12891 8923003 : if (pushed_scope)
12892 8923003 : pop_scope (pushed_scope);
12893 8923003 : if (!ok)
12894 : {
12895 0 : error ("no %q#D member function declared in class %qT",
12896 : decl, ctype);
12897 0 : return NULL_TREE;
12898 : }
12899 8923003 : if (ok == error_mark_node)
12900 : return NULL_TREE;
12901 8922979 : return old_decl;
12902 : }
12903 : }
12904 :
12905 361857562 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12906 : return NULL_TREE;
12907 :
12908 : /* Don't call check_consteval_only_fn for defaulted functions. Those are
12909 : immediate-escalating functions but at this point DECL_DEFAULTED_P has
12910 : not been set. */
12911 180928778 : if (initialized != SD_DEFAULTED)
12912 174057659 : check_consteval_only_fn (decl);
12913 :
12914 180928778 : if (ctype == NULL_TREE || check)
12915 73046408 : return decl;
12916 :
12917 107882370 : if (virtualp)
12918 4293494 : DECL_VIRTUAL_P (decl) = 1;
12919 :
12920 107882370 : return decl;
12921 : }
12922 :
12923 : /* decl is a FUNCTION_DECL.
12924 : specifiers are the parsed virt-specifiers.
12925 :
12926 : Set flags to reflect the virt-specifiers.
12927 :
12928 : Returns decl. */
12929 :
12930 : static tree
12931 114660819 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12932 : {
12933 114660819 : if (decl == NULL_TREE)
12934 : return decl;
12935 114660722 : if (specifiers & VIRT_SPEC_OVERRIDE)
12936 337478 : DECL_OVERRIDE_P (decl) = 1;
12937 114660722 : if (specifiers & VIRT_SPEC_FINAL)
12938 10008 : DECL_FINAL_P (decl) = 1;
12939 : return decl;
12940 : }
12941 :
12942 : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12943 : the linkage that DECL will receive in the object file. */
12944 :
12945 : static void
12946 14299560 : set_linkage_for_static_data_member (tree decl)
12947 : {
12948 : /* A static data member always has static storage duration and
12949 : external linkage. Note that static data members are forbidden in
12950 : local classes -- the only situation in which a class has
12951 : non-external linkage. */
12952 14299560 : TREE_PUBLIC (decl) = 1;
12953 14299560 : TREE_STATIC (decl) = 1;
12954 : /* For non-template classes, static data members are always put
12955 : out in exactly those files where they are defined, just as
12956 : with ordinary namespace-scope variables. */
12957 14299560 : if (!processing_template_decl)
12958 10933620 : DECL_INTERFACE_KNOWN (decl) = 1;
12959 14299560 : }
12960 :
12961 : /* Create a VAR_DECL named NAME with the indicated TYPE.
12962 :
12963 : If SCOPE is non-NULL, it is the class type or namespace containing
12964 : the variable. If SCOPE is NULL, the variable should is created in
12965 : the innermost enclosing scope. */
12966 :
12967 : static tree
12968 71599943 : grokvardecl (tree type,
12969 : tree name,
12970 : tree orig_declarator,
12971 : const cp_decl_specifier_seq *declspecs,
12972 : int initialized,
12973 : int type_quals,
12974 : int inlinep,
12975 : bool conceptp,
12976 : int template_count,
12977 : tree scope,
12978 : location_t location)
12979 : {
12980 71599943 : tree decl;
12981 71599943 : tree explicit_scope;
12982 :
12983 71599943 : gcc_assert (!name || identifier_p (name));
12984 :
12985 71599943 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12986 71599943 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12987 :
12988 : /* Compute the scope in which to place the variable, but remember
12989 : whether or not that scope was explicitly specified by the user. */
12990 71599943 : explicit_scope = scope;
12991 71599943 : if (!scope)
12992 : {
12993 : /* An explicit "extern" specifier indicates a namespace-scope
12994 : variable. */
12995 71061587 : if (declspecs->storage_class == sc_extern)
12996 566418 : scope = current_decl_namespace ();
12997 70495169 : else if (!at_function_scope_p ())
12998 7529432 : scope = current_scope ();
12999 : }
13000 :
13001 71061587 : if (scope
13002 8634206 : && (/* If the variable is a namespace-scope variable declared in a
13003 : template, we need DECL_LANG_SPECIFIC. */
13004 8634206 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
13005 : /* Similarly for namespace-scope variables with language linkage
13006 : other than C++. */
13007 8112498 : || (TREE_CODE (scope) == NAMESPACE_DECL
13008 4647719 : && current_lang_name != lang_name_cplusplus)
13009 : /* Similarly for static data members. */
13010 4926446 : || TYPE_P (scope)
13011 : /* Similarly for explicit specializations. */
13012 4404738 : || (orig_declarator
13013 4403894 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
13014 5179243 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
13015 : else
13016 66420700 : decl = build_decl (location, VAR_DECL, name, type);
13017 :
13018 71599943 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
13019 16648 : set_decl_namespace (decl, explicit_scope, 0);
13020 : else
13021 71583295 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
13022 :
13023 71599943 : if (declspecs->storage_class == sc_extern)
13024 : {
13025 566418 : DECL_THIS_EXTERN (decl) = 1;
13026 566418 : DECL_EXTERNAL (decl) = !initialized;
13027 : }
13028 :
13029 71599943 : if (DECL_CLASS_SCOPE_P (decl))
13030 : {
13031 521708 : set_linkage_for_static_data_member (decl);
13032 : /* This function is only called with out-of-class definitions. */
13033 521708 : DECL_EXTERNAL (decl) = 0;
13034 521708 : check_class_member_definition_namespace (decl);
13035 : }
13036 : /* At top level, either `static' or no s.c. makes a definition
13037 : (perhaps tentative), and absence of `static' makes it public. */
13038 71078235 : else if (toplevel_bindings_p ())
13039 : {
13040 16222668 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
13041 8111334 : && (DECL_THIS_EXTERN (decl)
13042 7506767 : || ! constp
13043 7506767 : || volatilep
13044 7427537 : || inlinep
13045 607958 : || in_template_context
13046 31519 : || processing_specialization
13047 31436 : || module_attach_p ()));
13048 8111334 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
13049 : }
13050 : /* Not at top level, only `static' makes a static definition. */
13051 : else
13052 : {
13053 62966901 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
13054 62966901 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
13055 : }
13056 :
13057 71599943 : set_originating_module (decl);
13058 :
13059 71599943 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
13060 : {
13061 20180 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
13062 : {
13063 20172 : CP_DECL_THREAD_LOCAL_P (decl) = true;
13064 : // NB: Set a tentative TLS model to avoid tls_model attribute
13065 : // warnings due to lack of thread storage duration. It will
13066 : // be updated by cplus_decl_attributes later.
13067 20172 : if (!processing_template_decl)
13068 20047 : set_decl_tls_model (decl, TLS_MODEL_REAL);
13069 : }
13070 20180 : if (declspecs->gnu_thread_keyword_p)
13071 19173 : SET_DECL_GNU_TLS_P (decl);
13072 : }
13073 :
13074 : /* If the type of the decl has no linkage, make sure that we'll
13075 : notice that in mark_used. */
13076 71599943 : if (cxx_dialect > cxx98
13077 71291535 : && decl_linkage (decl) != lk_none
13078 70568141 : && DECL_LANG_SPECIFIC (decl) == NULL
13079 65309825 : && !DECL_EXTERN_C_P (decl)
13080 136909768 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
13081 4661 : retrofit_lang_decl (decl);
13082 :
13083 71599943 : if (TREE_PUBLIC (decl))
13084 : {
13085 : /* [basic.link]: A name with no linkage (notably, the name of a class
13086 : or enumeration declared in a local scope) shall not be used to
13087 : declare an entity with linkage.
13088 :
13089 : DR 757 relaxes this restriction for C++0x. */
13090 8563465 : if (cxx_dialect < cxx11)
13091 23975 : no_linkage_error (decl);
13092 : }
13093 : else
13094 63036478 : DECL_INTERFACE_KNOWN (decl) = 1;
13095 :
13096 71599943 : if (DECL_NAME (decl)
13097 71599943 : && MAIN_NAME_P (DECL_NAME (decl)))
13098 : {
13099 12 : if (scope == global_namespace)
13100 6 : error_at (DECL_SOURCE_LOCATION (decl),
13101 : "cannot declare %<::main%> to be a global variable");
13102 6 : else if (DECL_EXTERN_C_P (decl))
13103 3 : error_at (DECL_SOURCE_LOCATION (decl),
13104 : "an entity named %<main%> cannot be declared with "
13105 : "C language linkage");
13106 : }
13107 :
13108 : /* Check if a variable is being declared as a concept. */
13109 71599943 : if (conceptp)
13110 : {
13111 30 : if (!processing_template_decl)
13112 13 : error_at (declspecs->locations[ds_concept],
13113 : "a non-template variable cannot be %<concept%>");
13114 17 : else if (!at_namespace_scope_p ())
13115 2 : error_at (declspecs->locations[ds_concept],
13116 : "concept must be defined at namespace scope");
13117 : else
13118 15 : error_at (declspecs->locations[ds_concept],
13119 : "variable concepts are no longer supported");
13120 : return NULL_TREE;
13121 : }
13122 71599913 : else if (flag_concepts
13123 71599913 : && current_template_depth > template_class_depth (scope))
13124 : {
13125 61613005 : tree ci = current_template_constraints ();
13126 61613005 : set_constraints (decl, ci);
13127 : }
13128 :
13129 : // Handle explicit specializations and instantiations of variable templates.
13130 71599913 : if (orig_declarator)
13131 70867093 : decl = check_explicit_specialization (orig_declarator, decl,
13132 : template_count, 0);
13133 :
13134 71599913 : return decl != error_mark_node ? decl : NULL_TREE;
13135 : }
13136 :
13137 : /* Create and return a canonical pointer to member function type, for
13138 : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
13139 :
13140 : tree
13141 3152561 : build_ptrmemfunc_type (tree type)
13142 : {
13143 3152561 : tree field, fields;
13144 3152561 : tree t;
13145 :
13146 3152561 : if (type == error_mark_node)
13147 : return type;
13148 :
13149 : /* Make sure that we always have the unqualified pointer-to-member
13150 : type first. */
13151 3152561 : if (cp_cv_quals quals = cp_type_quals (type))
13152 : {
13153 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
13154 0 : return cp_build_qualified_type (unqual, quals);
13155 : }
13156 :
13157 : /* If a canonical type already exists for this type, use it. We use
13158 : this method instead of type_hash_canon, because it only does a
13159 : simple equality check on the list of field members. */
13160 :
13161 3152561 : t = TYPE_PTRMEMFUNC_TYPE (type);
13162 3152561 : if (t)
13163 : return t;
13164 :
13165 1055734 : t = make_node (RECORD_TYPE);
13166 :
13167 : /* Let the front end know this is a pointer to member function. */
13168 1055734 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
13169 :
13170 1055734 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
13171 1055734 : DECL_NONADDRESSABLE_P (field) = 1;
13172 1055734 : fields = field;
13173 :
13174 1055734 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
13175 : delta_type_node);
13176 1055734 : DECL_NONADDRESSABLE_P (field) = 1;
13177 1055734 : DECL_CHAIN (field) = fields;
13178 1055734 : fields = field;
13179 :
13180 1055734 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
13181 :
13182 : /* Zap out the name so that the back end will give us the debugging
13183 : information for this anonymous RECORD_TYPE. */
13184 1055734 : TYPE_NAME (t) = NULL_TREE;
13185 :
13186 : /* Cache this pointer-to-member type so that we can find it again
13187 : later. */
13188 1055734 : TYPE_PTRMEMFUNC_TYPE (type) = t;
13189 :
13190 1055734 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
13191 522552 : SET_TYPE_STRUCTURAL_EQUALITY (t);
13192 533182 : else if (TYPE_CANONICAL (type) != type)
13193 338666 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
13194 :
13195 : return t;
13196 : }
13197 :
13198 : /* Create and return a pointer to data member type. */
13199 :
13200 : tree
13201 336426 : build_ptrmem_type (tree class_type, tree member_type)
13202 : {
13203 336426 : if (TREE_CODE (member_type) == METHOD_TYPE)
13204 : {
13205 60759 : cp_cv_quals quals = type_memfn_quals (member_type);
13206 60759 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
13207 60759 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
13208 60759 : return build_ptrmemfunc_type (build_pointer_type (member_type));
13209 : }
13210 : else
13211 : {
13212 275667 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
13213 275667 : return build_offset_type (class_type, member_type);
13214 : }
13215 : }
13216 :
13217 : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
13218 : Check to see that the definition is valid. Issue appropriate error
13219 : messages. */
13220 :
13221 : static void
13222 19502920 : check_static_variable_definition (tree decl, tree type)
13223 : {
13224 : /* Avoid redundant diagnostics on out-of-class definitions. */
13225 19502920 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
13226 : ;
13227 : /* Can't check yet if we don't know the type. */
13228 18063057 : else if (dependent_type_p (type))
13229 : ;
13230 : /* If DECL is declared constexpr, we'll do the appropriate checks
13231 : in check_initializer. Similarly for inline static data members. */
13232 17511880 : else if (DECL_P (decl)
13233 17511880 : && (DECL_DECLARED_CONSTEXPR_P (decl)
13234 2794921 : || DECL_VAR_DECLARED_INLINE_P (decl)))
13235 : ;
13236 2794422 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13237 : {
13238 60 : if (!COMPLETE_TYPE_P (type))
13239 11 : error_at (DECL_SOURCE_LOCATION (decl),
13240 : "in-class initialization of static data member %q#D of "
13241 : "incomplete type", decl);
13242 49 : else if (literal_type_p (type))
13243 43 : permerror (DECL_SOURCE_LOCATION (decl),
13244 : "%<constexpr%> needed for in-class initialization of "
13245 : "static data member %q#D of non-integral type", decl);
13246 : else
13247 6 : error_at (DECL_SOURCE_LOCATION (decl),
13248 : "in-class initialization of static data member %q#D of "
13249 : "non-literal type", decl);
13250 : }
13251 : /* Motion 10 at San Diego: If a static const integral data member is
13252 : initialized with an integral constant expression, the initializer
13253 : may appear either in the declaration (within the class), or in
13254 : the definition, but not both. If it appears in the class, the
13255 : member is a member constant. The file-scope definition is always
13256 : required. */
13257 2794362 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
13258 11 : error_at (DECL_SOURCE_LOCATION (decl),
13259 : "invalid in-class initialization of static data member "
13260 : "of non-integral type %qT",
13261 : type);
13262 2794351 : else if (!CP_TYPE_CONST_P (type))
13263 5 : error_at (DECL_SOURCE_LOCATION (decl),
13264 : "ISO C++ forbids in-class initialization of non-const "
13265 : "static member %qD",
13266 : decl);
13267 2794346 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
13268 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13269 : "ISO C++ forbids initialization of member constant "
13270 : "%qD of non-integral type %qT", decl, type);
13271 19502920 : }
13272 :
13273 : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
13274 : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
13275 : expressions out into temporary variables so that walk_tree doesn't
13276 : step into them (c++/15764). */
13277 :
13278 : static tree
13279 3380 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
13280 : {
13281 3380 : hash_set<tree> *pset = (hash_set<tree> *)data;
13282 3380 : tree expr = *expr_p;
13283 3380 : if (TREE_CODE (expr) == SAVE_EXPR)
13284 : {
13285 1471 : tree op = TREE_OPERAND (expr, 0);
13286 1471 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
13287 1471 : if (TREE_SIDE_EFFECTS (op))
13288 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
13289 1471 : *walk_subtrees = 0;
13290 : }
13291 1909 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
13292 1734 : *walk_subtrees = 0;
13293 3380 : return NULL;
13294 : }
13295 :
13296 : /* Entry point for the above. */
13297 :
13298 : static void
13299 1530 : stabilize_vla_size (tree size)
13300 : {
13301 1530 : hash_set<tree> pset;
13302 : /* Break out any function calls into temporary variables. */
13303 1530 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
13304 1530 : }
13305 :
13306 : /* Reduce a SIZEOF_EXPR to its value. */
13307 :
13308 : tree
13309 12593982 : fold_sizeof_expr (tree t)
13310 : {
13311 12593982 : tree r;
13312 12593982 : if (SIZEOF_EXPR_TYPE_P (t))
13313 11975098 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13314 11975098 : TREE_TYPE (TREE_OPERAND (t, 0)),
13315 : SIZEOF_EXPR, false, false);
13316 618884 : else if (TYPE_P (TREE_OPERAND (t, 0)))
13317 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
13318 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
13319 : false, false);
13320 : else
13321 618884 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
13322 618884 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
13323 : false, false);
13324 12593982 : if (r == error_mark_node)
13325 67 : r = size_one_node;
13326 12593982 : r = cp_fold_convert (size_type_node, r);
13327 12593982 : return r;
13328 : }
13329 :
13330 : /* Given the SIZE (i.e., number of elements) in an array, compute
13331 : an appropriate index type for the array. If non-NULL, NAME is
13332 : the name of the entity being declared. */
13333 :
13334 : static tree
13335 3552977 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
13336 : tsubst_flags_t complain)
13337 : {
13338 3552977 : if (error_operand_p (size))
13339 32 : return error_mark_node;
13340 :
13341 : /* The type of the index being computed. */
13342 3552945 : tree itype;
13343 :
13344 : /* The original numeric size as seen in the source code before
13345 : conversion to size_t. */
13346 3552945 : tree origsize = size;
13347 :
13348 3552945 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
13349 :
13350 3552945 : if (!type_dependent_expression_p (size))
13351 : {
13352 3504072 : origsize = size = mark_rvalue_use (size);
13353 :
13354 38487 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
13355 3504232 : && TREE_SIDE_EFFECTS (size))
13356 : /* In C++98, we mark a non-constant array bound with a magic
13357 : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
13358 : else
13359 : {
13360 3504071 : size = build_converted_constant_expr (size_type_node, size, complain);
13361 : /* Pedantically a constant expression is required here and so
13362 : __builtin_is_constant_evaluated () should fold to true if it
13363 : is successfully folded into a constant. */
13364 3504071 : size = fold_non_dependent_expr (size, complain,
13365 : /*manifestly_const_eval=*/true);
13366 :
13367 3504071 : if (!TREE_CONSTANT (size))
13368 139122 : size = origsize;
13369 : }
13370 :
13371 3504072 : if (error_operand_p (size))
13372 3 : return error_mark_node;
13373 :
13374 : /* The array bound must be an integer type. */
13375 3504069 : tree type = TREE_TYPE (size);
13376 3504069 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
13377 : {
13378 21 : if (!(complain & tf_error))
13379 0 : return error_mark_node;
13380 21 : if (name)
13381 18 : error_at (loc, "size of array %qD has non-integral type %qT",
13382 : name, type);
13383 : else
13384 3 : error_at (loc, "size of array has non-integral type %qT", type);
13385 21 : size = integer_one_node;
13386 : }
13387 : }
13388 :
13389 : /* A type is dependent if it is...an array type constructed from any
13390 : dependent type or whose size is specified by a constant expression
13391 : that is value-dependent. */
13392 : /* We can only call value_dependent_expression_p on integral constant
13393 : expressions. */
13394 3552942 : if (processing_template_decl
13395 1623630 : && potential_constant_expression (size)
13396 5176547 : && value_dependent_expression_p (size))
13397 : {
13398 : /* Just build the index type and mark that it requires
13399 : structural equality checks. */
13400 904353 : in_template:
13401 904353 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13402 : size, size_one_node));
13403 904353 : TYPE_DEPENDENT_P (itype) = 1;
13404 904353 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13405 904353 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
13406 904353 : return itype;
13407 : }
13408 :
13409 2665323 : if (TREE_CODE (size) != INTEGER_CST)
13410 : {
13411 18829 : tree folded = cp_fully_fold (size);
13412 18829 : if (TREE_CODE (folded) == INTEGER_CST)
13413 : {
13414 219 : if (name)
13415 145 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13416 : "integral constant-expression", name);
13417 : else
13418 74 : pedwarn (loc, OPT_Wpedantic,
13419 : "size of array is not an integral constant-expression");
13420 : }
13421 18829 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13422 : /* We might have lost the TREE_CONSTANT flag e.g. when we are
13423 : folding a conversion from a pointer to integral type. In that
13424 : case issue an error below and don't treat this as a VLA. */;
13425 : else
13426 : /* Use the folded result for VLAs, too; it will have resolved
13427 : SIZEOF_EXPR. */
13428 : size = folded;
13429 : }
13430 :
13431 : /* Normally, the array-bound will be a constant. */
13432 2665323 : if (TREE_CODE (size) == INTEGER_CST)
13433 : {
13434 : /* The size to use in diagnostics that reflects the constant
13435 : size used in the source, rather than SIZE massaged above. */
13436 2646713 : tree diagsize = size;
13437 :
13438 : /* If the original size before conversion to size_t was signed
13439 : and negative, convert it to ssizetype to restore the sign. */
13440 2646713 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13441 : && TREE_CODE (size) == INTEGER_CST
13442 2646713 : && tree_int_cst_sign_bit (size))
13443 : {
13444 238 : diagsize = fold_convert (ssizetype, size);
13445 :
13446 : /* Clear the overflow bit that may have been set as a result
13447 : of the conversion from the sizetype of the new size to
13448 : ssizetype. */
13449 238 : TREE_OVERFLOW (diagsize) = false;
13450 : }
13451 :
13452 : /* Verify that the array has a positive number of elements
13453 : and issue the appropriate diagnostic if it doesn't. */
13454 2646713 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13455 : {
13456 469 : if (!(complain & tf_error))
13457 15 : return error_mark_node;
13458 454 : size = integer_one_node;
13459 : }
13460 : /* As an extension we allow zero-sized arrays. */
13461 2646244 : else if (integer_zerop (size))
13462 : {
13463 3217 : if (!(complain & tf_error))
13464 : /* We must fail if performing argument deduction (as
13465 : indicated by the state of complain), so that
13466 : another substitution can be found. */
13467 8 : return error_mark_node;
13468 3209 : else if (name)
13469 2915 : pedwarn (loc, OPT_Wpedantic,
13470 : "ISO C++ forbids zero-size array %qD", name);
13471 : else
13472 294 : pedwarn (loc, OPT_Wpedantic,
13473 : "ISO C++ forbids zero-size array");
13474 : }
13475 : }
13476 18610 : else if (TREE_CONSTANT (size)
13477 : /* We don't allow VLAs at non-function scopes, or during
13478 : tentative template substitution. */
13479 18595 : || !at_function_scope_p ()
13480 36880 : || !(complain & tf_error))
13481 : {
13482 346 : if (!(complain & tf_error))
13483 295 : return error_mark_node;
13484 : /* `(int) &fn' is not a valid array bound. */
13485 51 : if (name)
13486 37 : error_at (loc,
13487 : "size of array %qD is not an integral constant-expression",
13488 : name);
13489 : else
13490 14 : error_at (loc, "size of array is not an integral constant-expression");
13491 51 : size = integer_one_node;
13492 : }
13493 18264 : else if (pedantic && warn_vla != 0)
13494 : {
13495 30 : if (name)
13496 15 : pedwarn (name_loc, OPT_Wvla,
13497 : "ISO C++ forbids variable length array %qD", name);
13498 : else
13499 15 : pedwarn (input_location, OPT_Wvla,
13500 : "ISO C++ forbids variable length array");
13501 : }
13502 18234 : else if (warn_vla > 0)
13503 : {
13504 3 : if (name)
13505 3 : warning_at (name_loc, OPT_Wvla,
13506 : "variable length array %qD is used", name);
13507 : else
13508 0 : warning (OPT_Wvla,
13509 : "variable length array is used");
13510 : }
13511 :
13512 2665005 : if (processing_template_decl && !TREE_CONSTANT (size))
13513 16734 : goto in_template;
13514 : else
13515 : {
13516 2648271 : if (!TREE_CONSTANT (size))
13517 : {
13518 : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13519 : of the MINUS_EXPR, which allows the -1 to get folded with the +1
13520 : that happens when building TYPE_SIZE. */
13521 1530 : size = variable_size (size);
13522 1530 : stabilize_vla_size (size);
13523 : }
13524 :
13525 : /* Compute the index of the largest element in the array. It is
13526 : one less than the number of elements in the array. We save
13527 : and restore PROCESSING_TEMPLATE_DECL so that computations in
13528 : cp_build_binary_op will be appropriately folded. */
13529 2648271 : {
13530 2648271 : processing_template_decl_sentinel s;
13531 2648271 : itype = cp_build_binary_op (input_location,
13532 : MINUS_EXPR,
13533 : cp_convert (ssizetype, size, complain),
13534 : cp_convert (ssizetype, integer_one_node,
13535 : complain),
13536 : complain);
13537 2648271 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13538 2648271 : }
13539 :
13540 2648271 : if (!TREE_CONSTANT (itype))
13541 : {
13542 1530 : if (sanitize_flags_p (SANITIZE_VLA)
13543 1530 : && current_function_decl != NULL_TREE)
13544 : {
13545 : /* We have to add 1 -- in the ubsan routine we generate
13546 : LE_EXPR rather than LT_EXPR. */
13547 78 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13548 : build_one_cst (TREE_TYPE (itype)));
13549 78 : t = ubsan_instrument_vla (input_location, t);
13550 78 : finish_expr_stmt (t);
13551 : }
13552 : }
13553 : /* Make sure that there was no overflow when creating to a signed
13554 : index type. (For example, on a 32-bit machine, an array with
13555 : size 2^32 - 1 is too big.) */
13556 2646741 : else if (TREE_CODE (itype) == INTEGER_CST
13557 2646741 : && TREE_OVERFLOW (itype))
13558 : {
13559 0 : if (!(complain & tf_error))
13560 0 : return error_mark_node;
13561 0 : error ("overflow in array dimension");
13562 0 : TREE_OVERFLOW (itype) = 0;
13563 : }
13564 : }
13565 :
13566 : /* Create and return the appropriate index type. */
13567 2648271 : itype = build_index_type (itype);
13568 :
13569 : /* If the index type were dependent, we would have returned early, so
13570 : remember that it isn't. */
13571 2648271 : TYPE_DEPENDENT_P (itype) = 0;
13572 2648271 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13573 2648271 : return itype;
13574 : }
13575 :
13576 : tree
13577 623517 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13578 : {
13579 623517 : return compute_array_index_type_loc (input_location, name, size, complain);
13580 : }
13581 :
13582 : /* Returns the scope (if any) in which the entity declared by
13583 : DECLARATOR will be located. If the entity was declared with an
13584 : unqualified name, NULL_TREE is returned. */
13585 :
13586 : tree
13587 784989629 : get_scope_of_declarator (const cp_declarator *declarator)
13588 : {
13589 909609230 : while (declarator && declarator->kind != cdk_id)
13590 124619601 : declarator = declarator->declarator;
13591 :
13592 : /* If the declarator-id is a SCOPE_REF, the scope in which the
13593 : declaration occurs is the first operand. */
13594 784989629 : if (declarator
13595 783713619 : && declarator->u.id.qualifying_scope)
13596 : return declarator->u.id.qualifying_scope;
13597 :
13598 : /* Otherwise, the declarator is not a qualified name; the entity will
13599 : be declared in the current scope. */
13600 : return NULL_TREE;
13601 : }
13602 :
13603 : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
13604 : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13605 : with this type. */
13606 :
13607 : static tree
13608 3765748 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13609 : {
13610 3765748 : tree itype = NULL_TREE;
13611 :
13612 : /* If things have already gone awry, bail now. */
13613 3765748 : if (type == error_mark_node || size == error_mark_node)
13614 : return error_mark_node;
13615 :
13616 : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13617 : for a deduced class type. */
13618 3765685 : if (template_placeholder_p (type))
13619 : {
13620 6 : if (name)
13621 0 : error_at (loc, "%qD declared as array of template placeholder "
13622 : "type %qT", name, type);
13623 : else
13624 6 : error ("creating array of template placeholder type %qT", type);
13625 6 : return error_mark_node;
13626 : }
13627 :
13628 : /* If there are some types which cannot be array elements,
13629 : issue an error-message and return. */
13630 3765679 : switch (TREE_CODE (type))
13631 : {
13632 3 : case VOID_TYPE:
13633 3 : if (name)
13634 0 : error_at (loc, "declaration of %qD as array of void", name);
13635 : else
13636 3 : error ("creating array of void");
13637 3 : return error_mark_node;
13638 :
13639 6 : case FUNCTION_TYPE:
13640 6 : if (name)
13641 6 : error_at (loc, "declaration of %qD as array of functions", name);
13642 : else
13643 0 : error ("creating array of functions");
13644 6 : return error_mark_node;
13645 :
13646 21 : case REFERENCE_TYPE:
13647 21 : if (name)
13648 15 : error_at (loc, "declaration of %qD as array of references", name);
13649 : else
13650 6 : error ("creating array of references");
13651 21 : return error_mark_node;
13652 :
13653 0 : case METHOD_TYPE:
13654 0 : if (name)
13655 0 : error_at (loc, "declaration of %qD as array of function members",
13656 : name);
13657 : else
13658 0 : error ("creating array of function members");
13659 0 : return error_mark_node;
13660 :
13661 3765649 : default:
13662 3765649 : break;
13663 : }
13664 :
13665 3765649 : if (!verify_type_context (name ? loc : input_location,
13666 : TCTX_ARRAY_ELEMENT, type))
13667 0 : return error_mark_node;
13668 :
13669 : /* [dcl.array]
13670 :
13671 : The constant expressions that specify the bounds of the arrays
13672 : can be omitted only for the first member of the sequence. */
13673 3765649 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13674 : {
13675 30 : if (name)
13676 24 : error_at (loc, "declaration of %qD as multidimensional array must "
13677 : "have bounds for all dimensions except the first",
13678 : name);
13679 : else
13680 6 : error ("multidimensional array must have bounds for all "
13681 : "dimensions except the first");
13682 :
13683 30 : return error_mark_node;
13684 : }
13685 :
13686 : /* Figure out the index type for the array. */
13687 3765619 : if (size)
13688 : {
13689 2929460 : itype = compute_array_index_type_loc (loc, name, size,
13690 : tf_warning_or_error);
13691 2929460 : if (type_uses_auto (type)
13692 2929460 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13693 : {
13694 9 : sorry_at (loc, "variable-length array of %<auto%>");
13695 9 : return error_mark_node;
13696 : }
13697 : }
13698 :
13699 3765610 : return build_cplus_array_type (type, itype);
13700 : }
13701 :
13702 : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
13703 :
13704 : static location_t
13705 1089387592 : min_location (location_t loca, location_t locb)
13706 : {
13707 1089387592 : if (loca == UNKNOWN_LOCATION
13708 1089387592 : || (locb != UNKNOWN_LOCATION
13709 114109979 : && linemap_location_before_p (line_table, locb, loca)))
13710 976534496 : return locb;
13711 : return loca;
13712 : }
13713 :
13714 : /* Returns the smallest location != UNKNOWN_LOCATION among the
13715 : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13716 : and LOCATIONS[ds_restrict]. */
13717 :
13718 : static location_t
13719 1087571230 : smallest_type_quals_location (int type_quals, const location_t* locations)
13720 : {
13721 1087571230 : location_t loc = UNKNOWN_LOCATION;
13722 :
13723 1087571230 : if (type_quals & TYPE_QUAL_CONST)
13724 112553226 : loc = locations[ds_const];
13725 :
13726 1087571230 : if (type_quals & TYPE_QUAL_VOLATILE)
13727 1816271 : loc = min_location (loc, locations[ds_volatile]);
13728 :
13729 1087571230 : if (type_quals & TYPE_QUAL_RESTRICT)
13730 43 : loc = min_location (loc, locations[ds_restrict]);
13731 :
13732 1087571230 : return loc;
13733 : }
13734 :
13735 : /* Returns the smallest among the latter and locations[ds_type_spec]. */
13736 :
13737 : static location_t
13738 1087571209 : smallest_type_location (int type_quals, const location_t* locations)
13739 : {
13740 1087571209 : location_t loc = smallest_type_quals_location (type_quals, locations);
13741 1087571209 : return min_location (loc, locations[ds_type_spec]);
13742 : }
13743 :
13744 : static location_t
13745 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
13746 : {
13747 26 : int type_quals = get_type_quals (declspecs);
13748 26 : return smallest_type_location (type_quals, declspecs->locations);
13749 : }
13750 :
13751 : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
13752 : strip out the pointer/reference declarator(s). */
13753 :
13754 : static bool
13755 30917955 : maybe_strip_indirect_ref (const cp_declarator** declarator)
13756 : {
13757 30917955 : bool indirect_ref_p = false;
13758 61836057 : while (declarator && *declarator
13759 30918102 : && ((*declarator)->kind == cdk_pointer
13760 30918102 : || (*declarator)->kind == cdk_reference))
13761 : {
13762 147 : indirect_ref_p = true;
13763 147 : *declarator = (*declarator)->declarator;
13764 : }
13765 30917955 : return indirect_ref_p;
13766 : }
13767 :
13768 : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13769 : and DECLARATOR. SFK indicates the kind of special function (if any) that
13770 : this function is. OPTYPE is the type given in a conversion operator
13771 : declaration, or the class type for a constructor/destructor.
13772 : Returns the actual return type of the function; that may be different
13773 : than TYPE if an error occurs, or for certain special functions. */
13774 :
13775 : static tree
13776 30917955 : check_special_function_return_type (special_function_kind sfk,
13777 : tree type,
13778 : tree optype,
13779 : int type_quals,
13780 : const cp_declarator** declarator,
13781 : const location_t* locations)
13782 : {
13783 30917955 : gcc_assert (declarator);
13784 30917955 : location_t rettype_loc = (type
13785 30917955 : ? smallest_type_location (type_quals, locations)
13786 30917874 : : (*declarator)->id_loc);
13787 30917955 : switch (sfk)
13788 : {
13789 25191663 : case sfk_constructor:
13790 25191663 : if (maybe_strip_indirect_ref (declarator) || type)
13791 81 : error_at (rettype_loc,
13792 : "return type specification for constructor invalid");
13793 25191582 : else if (type_quals != TYPE_UNQUALIFIED)
13794 15 : error_at (smallest_type_quals_location (type_quals, locations),
13795 : "qualifiers are not allowed on constructor declaration");
13796 :
13797 25191663 : if (targetm.cxx.cdtor_returns_this ())
13798 0 : type = build_pointer_type (optype);
13799 : else
13800 25191663 : type = void_type_node;
13801 : break;
13802 :
13803 3389683 : case sfk_destructor:
13804 3389683 : if (maybe_strip_indirect_ref (declarator) || type)
13805 45 : error_at (rettype_loc,
13806 : "return type specification for destructor invalid");
13807 3389638 : else if (type_quals != TYPE_UNQUALIFIED)
13808 3 : error_at (smallest_type_quals_location (type_quals, locations),
13809 : "qualifiers are not allowed on destructor declaration");
13810 :
13811 : /* We can't use the proper return type here because we run into
13812 : problems with ambiguous bases and covariant returns. */
13813 3389683 : if (targetm.cxx.cdtor_returns_this ())
13814 0 : type = build_pointer_type (void_type_node);
13815 : else
13816 3389683 : type = void_type_node;
13817 : break;
13818 :
13819 1065764 : case sfk_conversion:
13820 1065764 : if (maybe_strip_indirect_ref (declarator) || type)
13821 42 : error_at (rettype_loc,
13822 : "return type specified for %<operator %T%>", optype);
13823 1065722 : else if (type_quals != TYPE_UNQUALIFIED)
13824 3 : error_at (smallest_type_quals_location (type_quals, locations),
13825 : "qualifiers are not allowed on declaration of "
13826 : "%<operator %T%>", optype);
13827 :
13828 : type = optype;
13829 : break;
13830 :
13831 1270845 : case sfk_deduction_guide:
13832 1270845 : if (maybe_strip_indirect_ref (declarator) || type)
13833 0 : error_at (rettype_loc,
13834 : "return type specified for deduction guide");
13835 1270845 : else if (type_quals != TYPE_UNQUALIFIED)
13836 0 : error_at (smallest_type_quals_location (type_quals, locations),
13837 : "qualifiers are not allowed on declaration of "
13838 : "deduction guide");
13839 1270845 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13840 : {
13841 3 : error ("template template parameter %qT in declaration of "
13842 : "deduction guide", optype);
13843 3 : type = error_mark_node;
13844 : }
13845 : else
13846 1270842 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13847 34312815 : for (int i = 0; i < ds_last; ++i)
13848 33041970 : if (i != ds_explicit && locations[i])
13849 3 : error_at (locations[i],
13850 : "%<decl-specifier%> in declaration of deduction guide");
13851 : break;
13852 :
13853 0 : default:
13854 0 : gcc_unreachable ();
13855 : }
13856 :
13857 30917955 : return type;
13858 : }
13859 :
13860 : /* A variable or data member (whose unqualified name is IDENTIFIER)
13861 : has been declared with the indicated TYPE. If the TYPE is not
13862 : acceptable, issue an error message and return a type to use for
13863 : error-recovery purposes. */
13864 :
13865 : tree
13866 341638625 : check_var_type (tree identifier, tree type, location_t loc)
13867 : {
13868 341638625 : if (VOID_TYPE_P (type))
13869 : {
13870 92 : if (!identifier)
13871 0 : error_at (loc, "unnamed variable or field declared void");
13872 92 : else if (identifier_p (identifier))
13873 : {
13874 92 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13875 92 : error_at (loc, "variable or field %qE declared void",
13876 : identifier);
13877 : }
13878 : else
13879 0 : error_at (loc, "variable or field declared void");
13880 92 : type = error_mark_node;
13881 : }
13882 :
13883 341638625 : return type;
13884 : }
13885 :
13886 : /* Handle declaring DECL as an inline variable. */
13887 :
13888 : static void
13889 7011258 : mark_inline_variable (tree decl, location_t loc)
13890 : {
13891 7011258 : bool inlinep = true;
13892 7011258 : if (! toplevel_bindings_p ())
13893 : {
13894 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13895 : "%qD declared at block scope", decl);
13896 24 : inlinep = false;
13897 : }
13898 7011234 : else if (cxx_dialect < cxx17)
13899 658 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13900 : "with %<-std=c++17%> or %<-std=gnu++17%>");
13901 682 : if (inlinep)
13902 : {
13903 7011234 : retrofit_lang_decl (decl);
13904 7011234 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13905 : }
13906 7011258 : }
13907 :
13908 :
13909 : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13910 : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13911 : ORIG is the anonymous TYPE_NAME before that change. */
13912 :
13913 : static bool
13914 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13915 : {
13916 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13917 42 : tree name = DECL_NAME (TYPE_NAME (type));
13918 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13919 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13920 : "anonymous non-C-compatible type given name for linkage "
13921 42 : "purposes by %<typedef%> declaration");
13922 42 : }
13923 :
13924 : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13925 : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13926 : to TYPE but during recursion can be set to nested classes. */
13927 :
13928 : static bool
13929 537967 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13930 : {
13931 537967 : if (!COMPLETE_TYPE_P (t))
13932 : return false;
13933 537964 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13934 : {
13935 6 : auto_diagnostic_group d;
13936 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13937 9 : inform (type == t ? DECL_SOURCE_LOCATION (orig)
13938 3 : : DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13939 : "type is not C-compatible because it has a base class");
13940 6 : return true;
13941 6 : }
13942 2522953 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13943 1985056 : switch (TREE_CODE (field))
13944 : {
13945 9 : case VAR_DECL:
13946 : /* static data members have been diagnosed already. */
13947 9 : continue;
13948 1319616 : case FIELD_DECL:
13949 1319616 : if (DECL_INITIAL (field))
13950 : {
13951 4 : auto_diagnostic_group d;
13952 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13953 4 : inform (DECL_SOURCE_LOCATION (field),
13954 : "type is not C-compatible because %qD has default "
13955 : "member initializer", field);
13956 4 : return true;
13957 4 : }
13958 1319612 : continue;
13959 9 : case CONST_DECL:
13960 9 : continue;
13961 665341 : case TYPE_DECL:
13962 665341 : if (DECL_SELF_REFERENCE_P (field))
13963 537932 : continue;
13964 127409 : if (DECL_IMPLICIT_TYPEDEF_P (field))
13965 : {
13966 127399 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13967 3 : continue;
13968 127396 : if (CLASS_TYPE_P (TREE_TYPE (field)))
13969 : {
13970 127396 : tree tf = TREE_TYPE (field);
13971 127396 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13972 : tf))
13973 : return true;
13974 127371 : continue;
13975 127371 : }
13976 : }
13977 : /* FALLTHRU */
13978 32 : case FUNCTION_DECL:
13979 32 : case TEMPLATE_DECL:
13980 32 : {
13981 32 : auto_diagnostic_group d;
13982 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13983 32 : inform (DECL_SOURCE_LOCATION (field),
13984 : "type is not C-compatible because it contains %qD "
13985 : "declaration", field);
13986 32 : return true;
13987 32 : }
13988 : default:
13989 : break;
13990 1319630 : }
13991 : return false;
13992 : }
13993 :
13994 : /* Assign a typedef-given name to a class or enumeration type declared
13995 : as anonymous at first. This was split out of grokdeclarator
13996 : because it is also used in libcc1. */
13997 :
13998 : void
13999 412368 : name_unnamed_type (tree type, tree decl)
14000 : {
14001 1237104 : gcc_assert (TYPE_UNNAMED_P (type)
14002 : || enum_with_enumerator_for_linkage_p (type));
14003 :
14004 412368 : tree orig = TYPE_NAME (type);
14005 412368 : if (flag_reflection)
14006 : {
14007 : /* For -freflection for typedef struct { ... } S; ^^S needs to be
14008 : a reflection of a type alias. So, TREE_TYPE (DECL) can't be
14009 : TYPE. Instead of what we do below, override DECL_NAME (orig). */
14010 15818 : DECL_NAME (orig) = DECL_NAME (decl);
14011 15818 : TYPE_DECL_FOR_LINKAGE_PURPOSES_P (orig) = 1;
14012 : }
14013 : else
14014 : /* Replace the anonymous decl with the real decl. Be careful not to
14015 : rename other typedefs (such as the self-reference) of type. */
14016 1187934 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14017 791384 : if (TYPE_NAME (t) == orig)
14018 396550 : TYPE_NAME (t) = decl;
14019 412368 : TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl) = 1;
14020 :
14021 : /* If this is a typedef within a template class, the nested
14022 : type is a (non-primary) template. The name for the
14023 : template needs updating as well. */
14024 412368 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
14025 68 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
14026 :
14027 : /* Adjust linkage now that we aren't unnamed anymore. */
14028 412368 : reset_type_linkage (type);
14029 :
14030 412368 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
14031 410571 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
14032 :
14033 : /* FIXME remangle member functions; member functions of a
14034 : type with external linkage have external linkage. */
14035 :
14036 : /* Check that our job is done, and that it would fail if we
14037 : attempted to do it again. */
14038 824736 : gcc_assert (!TYPE_UNNAMED_P (type)
14039 : && !enum_with_enumerator_for_linkage_p (type));
14040 412368 : }
14041 :
14042 : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
14043 : is allowed. TYPE might contain a decltype(auto). Returns true if
14044 : there was a problem, false otherwise. */
14045 :
14046 : static bool
14047 661899826 : check_decltype_auto (location_t loc, tree type)
14048 : {
14049 661899826 : if (tree a = type_uses_auto (type))
14050 : {
14051 18492755 : if (AUTO_IS_DECLTYPE (a))
14052 : {
14053 638662 : if (a != type)
14054 : {
14055 33 : error_at (loc, "%qT as type rather than plain "
14056 : "%<decltype(auto)%>", type);
14057 33 : return true;
14058 : }
14059 638629 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
14060 : {
14061 37 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
14062 37 : return true;
14063 : }
14064 : }
14065 : }
14066 : return false;
14067 : }
14068 :
14069 : /* Issue an error about two mutually incompatible declspecs
14070 : with the given names and locations
14071 : e.g. "error: `signed' and `unsigned' specified together" */
14072 :
14073 : static void
14074 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
14075 : const char *name_b, location_t loc_b)
14076 : {
14077 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
14078 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
14079 : nullptr, highlight_colors::rhs);
14080 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
14081 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
14082 66 : error_at (&richloc, "%e and %e specified together",
14083 : &e_name_a, &e_name_b);
14084 66 : }
14085 :
14086 : /* Given declspecs and a declarator (abstract or otherwise), determine
14087 : the name and type of the object declared and construct a DECL node
14088 : for it.
14089 :
14090 : DECLSPECS points to the representation of declaration-specifier
14091 : sequence that precedes declarator.
14092 :
14093 : DECL_CONTEXT says which syntactic context this declaration is in:
14094 : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
14095 : FUNCDEF for a function definition. Like NORMAL but a few different
14096 : error messages in each case. Return value may be zero meaning
14097 : this definition is too screwy to try to parse.
14098 : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
14099 : handle member functions (which have FIELD context).
14100 : Return value may be zero meaning this definition is too screwy to
14101 : try to parse.
14102 : PARM for a parameter declaration (either within a function prototype
14103 : or before a function body). Make a PARM_DECL, or return void_type_node.
14104 : TPARM for a template parameter declaration.
14105 : CATCHPARM for a parameter declaration before a catch clause.
14106 : TYPENAME if for a typename (in a cast or sizeof).
14107 : Don't make a DECL node; just return the ..._TYPE node.
14108 : FIELD for a struct or union field; make a FIELD_DECL.
14109 : BITFIELD for a field with specified width.
14110 :
14111 : INITIALIZED is as for start_decl.
14112 :
14113 : ATTRLIST is a pointer to the list of attributes, which may be NULL
14114 : if there are none; *ATTRLIST may be modified if attributes from inside
14115 : the declarator should be applied to the declaration.
14116 :
14117 : When this function is called, scoping variables (such as
14118 : CURRENT_CLASS_TYPE) should reflect the scope in which the
14119 : declaration occurs, not the scope in which the new declaration will
14120 : be placed. For example, on:
14121 :
14122 : void S::f() { ... }
14123 :
14124 : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
14125 : should not be `S'.
14126 :
14127 : Returns a DECL (if a declarator is present), a TYPE (if there is no
14128 : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
14129 : error occurs. */
14130 :
14131 : tree
14132 1087571102 : grokdeclarator (const cp_declarator *declarator,
14133 : cp_decl_specifier_seq *declspecs,
14134 : enum decl_context decl_context,
14135 : int initialized,
14136 : tree* attrlist)
14137 : {
14138 1087571102 : tree type = NULL_TREE;
14139 1087571102 : int longlong = 0;
14140 1087571102 : int explicit_intN = 0;
14141 1087571102 : int int_n_alt = 0;
14142 1087571102 : int virtualp, explicitp, friendp, inlinep, staticp;
14143 1087571102 : int explicit_int = 0;
14144 1087571102 : int explicit_char = 0;
14145 1087571102 : int defaulted_int = 0;
14146 :
14147 1087571102 : tree typedef_decl = NULL_TREE;
14148 1087571102 : const char *name = NULL;
14149 1087571102 : tree typedef_type = NULL_TREE;
14150 : /* True if this declarator is a function definition. */
14151 1087571102 : bool funcdef_flag = false;
14152 1087571102 : cp_declarator_kind innermost_code = cdk_error;
14153 1087571102 : int bitfield = 0;
14154 : #if 0
14155 : /* See the code below that used this. */
14156 : tree decl_attr = NULL_TREE;
14157 : #endif
14158 :
14159 : /* Keep track of what sort of function is being processed
14160 : so that we can warn about default return values, or explicit
14161 : return values which do not match prescribed defaults. */
14162 1087571102 : special_function_kind sfk = sfk_none;
14163 :
14164 1087571102 : tree dname = NULL_TREE;
14165 1087571102 : tree ctor_return_type = NULL_TREE;
14166 1087571102 : enum overload_flags flags = NO_SPECIAL;
14167 : /* cv-qualifiers that apply to the declarator, for a declaration of
14168 : a member function. */
14169 1087571102 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
14170 : /* virt-specifiers that apply to the declarator, for a declaration of
14171 : a member function. */
14172 1087571102 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
14173 : /* ref-qualifier that applies to the declarator, for a declaration of
14174 : a member function. */
14175 1087571102 : cp_ref_qualifier rqual = REF_QUAL_NONE;
14176 : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
14177 1087571102 : int type_quals = get_type_quals (declspecs);
14178 1087571102 : tree raises = NULL_TREE;
14179 1087571102 : int template_count = 0;
14180 1087571102 : tree returned_attrs = NULL_TREE;
14181 1087571102 : tree contract_specifiers = NULL_TREE;
14182 1087571102 : tree parms = NULL_TREE;
14183 1087571102 : const cp_declarator *id_declarator;
14184 : /* The unqualified name of the declarator; either an
14185 : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
14186 1087571102 : tree unqualified_id;
14187 : /* The class type, if any, in which this entity is located,
14188 : or NULL_TREE if none. Note that this value may be different from
14189 : the current class type; for example if an attempt is made to declare
14190 : "A::f" inside "B", this value will be "A". */
14191 1087571102 : tree ctype = current_class_type;
14192 : /* The NAMESPACE_DECL for the namespace in which this entity is
14193 : located. If an unqualified name is used to declare the entity,
14194 : this value will be NULL_TREE, even if the entity is located at
14195 : namespace scope. */
14196 1087571102 : tree in_namespace = NULL_TREE;
14197 1087571102 : cp_storage_class storage_class;
14198 1087571102 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
14199 1087571102 : bool type_was_error_mark_node = false;
14200 1087571102 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
14201 1087571102 : bool template_type_arg = false;
14202 1087571102 : bool template_parm_flag = false;
14203 1087571102 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
14204 1087571102 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
14205 1087571102 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
14206 1087571102 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
14207 1087571102 : bool late_return_type_p = false;
14208 1087571102 : bool array_parameter_p = false;
14209 1087571102 : tree reqs = NULL_TREE;
14210 :
14211 1087571102 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
14212 1087571102 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
14213 1087571102 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
14214 1087571102 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
14215 1087571102 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
14216 1087571102 : explicit_intN = declspecs->explicit_intN_p;
14217 1087571102 : int_n_alt = declspecs->int_n_alt;
14218 1087571102 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
14219 :
14220 : // Was concept_p specified? Note that ds_concept
14221 : // implies ds_constexpr!
14222 1087571102 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
14223 1087571102 : if (concept_p)
14224 117 : constexpr_p = true;
14225 :
14226 1087571102 : if (decl_context == FUNCDEF)
14227 : funcdef_flag = true, decl_context = NORMAL;
14228 : else if (decl_context == MEMFUNCDEF)
14229 : funcdef_flag = true, decl_context = FIELD;
14230 : else if (decl_context == BITFIELD)
14231 : bitfield = 1, decl_context = FIELD;
14232 : else if (decl_context == TEMPLATE_TYPE_ARG)
14233 : template_type_arg = true, decl_context = TYPENAME;
14234 : else if (decl_context == TPARM)
14235 11497649 : template_parm_flag = true, decl_context = PARM;
14236 :
14237 1087571102 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
14238 11456459 : funcdef_flag = true;
14239 :
14240 1087571102 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
14241 : (type_quals,
14242 1087571102 : declspecs->locations));
14243 1087571102 : location_t id_loc;
14244 1087571102 : location_t init_loc;
14245 1087571102 : if (declarator)
14246 : {
14247 655968894 : id_loc = loc_or_input_loc (declarator->id_loc);
14248 655968894 : init_loc = loc_or_input_loc (declarator->init_loc);
14249 : }
14250 : else
14251 431602208 : init_loc = id_loc = input_location;
14252 :
14253 : /* Look inside a declarator for the name being declared
14254 : and get it as a string, for an error message. */
14255 1087571102 : for (id_declarator = declarator;
14256 1493549755 : id_declarator;
14257 405978653 : id_declarator = id_declarator->declarator)
14258 : {
14259 1018124038 : if (id_declarator->kind != cdk_id)
14260 405978678 : innermost_code = id_declarator->kind;
14261 :
14262 1018124038 : switch (id_declarator->kind)
14263 : {
14264 199093839 : case cdk_function:
14265 199093839 : if (id_declarator->declarator
14266 198701770 : && id_declarator->declarator->kind == cdk_id)
14267 : {
14268 196776418 : sfk = id_declarator->declarator->u.id.sfk;
14269 196776418 : if (sfk == sfk_destructor)
14270 3389686 : flags = DTOR_FLAG;
14271 : }
14272 : break;
14273 :
14274 612145360 : case cdk_id:
14275 612145360 : {
14276 612145360 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
14277 612145360 : tree decl = id_declarator->u.id.unqualified_name;
14278 612145360 : if (!decl)
14279 : break;
14280 612145360 : if (qualifying_scope)
14281 : {
14282 10135014 : if (check_for_bare_parameter_packs (qualifying_scope,
14283 10135014 : id_declarator->id_loc))
14284 6 : return error_mark_node;
14285 10135008 : if (at_function_scope_p ())
14286 : {
14287 : /* [dcl.meaning]
14288 :
14289 : A declarator-id shall not be qualified except
14290 : for ...
14291 :
14292 : None of the cases are permitted in block
14293 : scope. */
14294 0 : if (qualifying_scope == global_namespace)
14295 0 : error ("invalid use of qualified-name %<::%D%>",
14296 : decl);
14297 0 : else if (TYPE_P (qualifying_scope))
14298 0 : error ("invalid use of qualified-name %<%T::%D%>",
14299 : qualifying_scope, decl);
14300 : else
14301 0 : error ("invalid use of qualified-name %<%D::%D%>",
14302 : qualifying_scope, decl);
14303 0 : return error_mark_node;
14304 : }
14305 10135008 : else if (TYPE_P (qualifying_scope))
14306 : {
14307 9853570 : ctype = qualifying_scope;
14308 9853570 : if (!MAYBE_CLASS_TYPE_P (ctype))
14309 : {
14310 6 : error_at (id_declarator->id_loc,
14311 : "%q#T is not a class or namespace", ctype);
14312 6 : ctype = NULL_TREE;
14313 : }
14314 9853564 : else if (innermost_code != cdk_function
14315 521759 : && current_class_type
14316 9853579 : && !uniquely_derived_from_p (ctype,
14317 : current_class_type))
14318 : {
14319 9 : error_at (id_declarator->id_loc,
14320 : "invalid use of qualified-name %<%T::%D%>",
14321 : qualifying_scope, decl);
14322 9 : return error_mark_node;
14323 : }
14324 : }
14325 281438 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
14326 612145345 : in_namespace = qualifying_scope;
14327 : }
14328 612145345 : switch (TREE_CODE (decl))
14329 : {
14330 3389701 : case BIT_NOT_EXPR:
14331 3389701 : {
14332 3389701 : if (innermost_code != cdk_function)
14333 : {
14334 6 : error_at (EXPR_LOCATION (decl),
14335 : "declaration of %qE as non-function", decl);
14336 6 : return error_mark_node;
14337 : }
14338 3389695 : else if (!qualifying_scope
14339 3389695 : && !(current_class_type && at_class_scope_p ()))
14340 : {
14341 9 : error_at (EXPR_LOCATION (decl),
14342 : "declaration of %qE as non-member", decl);
14343 9 : return error_mark_node;
14344 : }
14345 :
14346 3389686 : tree type = TREE_OPERAND (decl, 0);
14347 3389686 : if (TYPE_P (type))
14348 3389683 : type = constructor_name (type);
14349 3389686 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
14350 3389686 : dname = decl;
14351 : }
14352 3389686 : break;
14353 :
14354 2987228 : case TEMPLATE_ID_EXPR:
14355 2987228 : {
14356 2987228 : tree fns = TREE_OPERAND (decl, 0);
14357 :
14358 2987228 : dname = fns;
14359 3975609 : if (!identifier_p (dname))
14360 2987201 : dname = OVL_NAME (dname);
14361 : }
14362 : /* Fall through. */
14363 :
14364 608755644 : case IDENTIFIER_NODE:
14365 608755644 : if (identifier_p (decl))
14366 605768416 : dname = decl;
14367 :
14368 608755644 : if (IDENTIFIER_KEYWORD_P (dname))
14369 : {
14370 0 : error ("declarator-id missing; using reserved word %qD",
14371 : dname);
14372 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14373 : }
14374 608755644 : else if (!IDENTIFIER_CONV_OP_P (dname))
14375 607689877 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14376 : else
14377 : {
14378 1065767 : gcc_assert (flags == NO_SPECIAL);
14379 1065767 : flags = TYPENAME_FLAG;
14380 1065767 : sfk = sfk_conversion;
14381 1065767 : tree glob = get_global_binding (dname);
14382 1065767 : if (glob && TREE_CODE (glob) == TYPE_DECL)
14383 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14384 : else
14385 : name = "<invalid operator>";
14386 : }
14387 : break;
14388 :
14389 0 : default:
14390 0 : gcc_unreachable ();
14391 : }
14392 : break;
14393 : }
14394 :
14395 : case cdk_array:
14396 : case cdk_pointer:
14397 : case cdk_reference:
14398 : case cdk_ptrmem:
14399 : break;
14400 :
14401 : case cdk_decomp:
14402 1018123983 : name = "structured binding";
14403 : break;
14404 :
14405 25 : case cdk_error:
14406 25 : return error_mark_node;
14407 :
14408 0 : default:
14409 0 : gcc_unreachable ();
14410 : }
14411 1018123983 : if (id_declarator->kind == cdk_id)
14412 : break;
14413 : }
14414 :
14415 : /* [dcl.fct.edf]
14416 :
14417 : The declarator in a function-definition shall have the form
14418 : D1 ( parameter-declaration-clause) ... */
14419 1087571047 : if (funcdef_flag && innermost_code != cdk_function)
14420 : {
14421 3 : error_at (id_loc, "function definition does not declare parameters");
14422 3 : return error_mark_node;
14423 : }
14424 :
14425 1087571044 : if (flags == TYPENAME_FLAG
14426 1087571044 : && innermost_code != cdk_function
14427 3 : && ! (ctype && !declspecs->any_specifiers_p))
14428 : {
14429 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
14430 3 : return error_mark_node;
14431 : }
14432 :
14433 1087571041 : if (dname && identifier_p (dname))
14434 : {
14435 608755638 : if (UDLIT_OPER_P (dname)
14436 608755638 : && innermost_code != cdk_function)
14437 : {
14438 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
14439 6 : return error_mark_node;
14440 : }
14441 :
14442 608755632 : if (IDENTIFIER_ANY_OP_P (dname))
14443 : {
14444 38735645 : if (typedef_p)
14445 : {
14446 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14447 3 : return error_mark_node;
14448 : }
14449 38735642 : else if (decl_context == PARM || decl_context == CATCHPARM)
14450 : {
14451 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
14452 15 : return error_mark_node;
14453 : }
14454 : }
14455 : }
14456 :
14457 : /* Anything declared one level down from the top level
14458 : must be one of the parameters of a function
14459 : (because the body is at least two levels down). */
14460 :
14461 : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14462 : by not allowing C++ class definitions to specify their parameters
14463 : with xdecls (must be spec.d in the parmlist).
14464 :
14465 : Since we now wait to push a class scope until we are sure that
14466 : we are in a legitimate method context, we must set oldcname
14467 : explicitly (since current_class_name is not yet alive).
14468 :
14469 : We also want to avoid calling this a PARM if it is in a namespace. */
14470 :
14471 1087571017 : if (decl_context == NORMAL && !toplevel_bindings_p ())
14472 : {
14473 75307091 : cp_binding_level *b = current_binding_level;
14474 75307091 : current_binding_level = b->level_chain;
14475 75307091 : if (current_binding_level != 0 && toplevel_bindings_p ())
14476 : decl_context = PARM;
14477 75307091 : current_binding_level = b;
14478 : }
14479 :
14480 1087571017 : if (name == NULL)
14481 475178327 : name = decl_context == PARM ? "parameter" : "type name";
14482 :
14483 1087571017 : if (consteval_p && constexpr_p)
14484 : {
14485 6 : error_at (declspecs->locations[ds_consteval],
14486 : "both %qs and %qs specified", "constexpr", "consteval");
14487 6 : return error_mark_node;
14488 : }
14489 :
14490 1087571011 : if (concept_p && typedef_p)
14491 : {
14492 9 : error_at (declspecs->locations[ds_concept],
14493 : "%qs cannot appear in a typedef declaration", "concept");
14494 9 : return error_mark_node;
14495 : }
14496 :
14497 1087571002 : if (constexpr_p && typedef_p)
14498 : {
14499 3 : error_at (declspecs->locations[ds_constexpr],
14500 : "%qs cannot appear in a typedef declaration", "constexpr");
14501 3 : return error_mark_node;
14502 : }
14503 :
14504 1087570999 : if (consteval_p && typedef_p)
14505 : {
14506 3 : error_at (declspecs->locations[ds_consteval],
14507 : "%qs cannot appear in a typedef declaration", "consteval");
14508 3 : return error_mark_node;
14509 : }
14510 :
14511 1087570996 : if (constinit_p && typedef_p)
14512 : {
14513 3 : error_at (declspecs->locations[ds_constinit],
14514 : "%qs cannot appear in a typedef declaration", "constinit");
14515 3 : return error_mark_node;
14516 : }
14517 :
14518 : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14519 : keywords shall appear in a decl-specifier-seq." */
14520 1087570993 : if (constinit_p && constexpr_p)
14521 : {
14522 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14523 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14524 6 : error_at (&richloc,
14525 : "can use at most one of the %<constinit%> and %<constexpr%> "
14526 : "specifiers");
14527 6 : return error_mark_node;
14528 6 : }
14529 :
14530 : /* If there were multiple types specified in the decl-specifier-seq,
14531 : issue an error message. */
14532 1087570987 : if (declspecs->multiple_types_p)
14533 : {
14534 1501 : error_at (typespec_loc,
14535 : "two or more data types in declaration of %qs", name);
14536 1501 : return error_mark_node;
14537 : }
14538 :
14539 1087569486 : if (declspecs->conflicting_specifiers_p)
14540 31 : return error_mark_node;
14541 :
14542 : /* Extract the basic type from the decl-specifier-seq. */
14543 1087569455 : type = declspecs->type;
14544 1087569455 : if (type == error_mark_node)
14545 : {
14546 1243 : type = NULL_TREE;
14547 1243 : type_was_error_mark_node = true;
14548 : }
14549 :
14550 : /* Ignore erroneous attributes. */
14551 1087569455 : if (attrlist && *attrlist == error_mark_node)
14552 12 : *attrlist = NULL_TREE;
14553 :
14554 : /* An object declared as __attribute__((unavailable)) suppresses
14555 : any reports of being declared with unavailable or deprecated
14556 : items. An object declared as __attribute__((deprecated))
14557 : suppresses warnings of uses of other deprecated items. */
14558 1087569455 : auto ds = make_temp_override (deprecated_state);
14559 1087569455 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
14560 171 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14561 1087569284 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
14562 976028 : deprecated_state = DEPRECATED_SUPPRESS;
14563 :
14564 1087569455 : cp_handle_deprecated_or_unavailable (type);
14565 1087569455 : if (type && TREE_CODE (type) == TYPE_DECL)
14566 : {
14567 757343630 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14568 757343630 : typedef_decl = type;
14569 757343630 : type = TREE_TYPE (typedef_decl);
14570 757343630 : if (DECL_ARTIFICIAL (typedef_decl))
14571 550409742 : cp_handle_deprecated_or_unavailable (type);
14572 : }
14573 : /* No type at all: default to `int', and set DEFAULTED_INT
14574 : because it was not a user-defined typedef. */
14575 1087569455 : if (type == NULL_TREE)
14576 : {
14577 40483798 : if (signed_p || unsigned_p || long_p || short_p)
14578 : {
14579 : /* These imply 'int'. */
14580 9564200 : type = integer_type_node;
14581 9564200 : defaulted_int = 1;
14582 : }
14583 : /* If we just have "complex", it is equivalent to "complex double". */
14584 30919598 : else if (!longlong && !explicit_intN
14585 30919598 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14586 : {
14587 81 : type = double_type_node;
14588 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14589 : "ISO C++ does not support plain %<complex%> meaning "
14590 : "%<double complex%>");
14591 : }
14592 : }
14593 : /* Gather flags. */
14594 1087569455 : explicit_int = declspecs->explicit_int_p;
14595 1087569455 : explicit_char = declspecs->explicit_char_p;
14596 :
14597 : #if 0
14598 : /* See the code below that used this. */
14599 : if (typedef_decl)
14600 : decl_attr = DECL_ATTRIBUTES (typedef_decl);
14601 : #endif
14602 1087569455 : typedef_type = type;
14603 :
14604 1087569455 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14605 2336609 : ctor_return_type = TREE_TYPE (dname);
14606 : else
14607 : ctor_return_type = ctype;
14608 :
14609 1087569455 : if (sfk != sfk_none)
14610 : {
14611 30917955 : type = check_special_function_return_type (sfk, type,
14612 : ctor_return_type,
14613 : type_quals,
14614 : &declarator,
14615 : declspecs->locations);
14616 30917955 : type_quals = TYPE_UNQUALIFIED;
14617 : }
14618 1056651500 : else if (type == NULL_TREE)
14619 : {
14620 1643 : int is_main;
14621 :
14622 1643 : explicit_int = -1;
14623 :
14624 : /* We handle `main' specially here, because 'main () { }' is so
14625 : common. With no options, it is allowed. With -Wreturn-type,
14626 : it is a warning. It is only an error with -pedantic-errors. */
14627 3286 : is_main = (funcdef_flag
14628 3520 : && dname && identifier_p (dname)
14629 234 : && MAIN_NAME_P (dname)
14630 68 : && ctype == NULL_TREE
14631 68 : && in_namespace == NULL_TREE
14632 1711 : && current_namespace == global_namespace);
14633 :
14634 1643 : if (type_was_error_mark_node)
14635 : /* We've already issued an error, don't complain more. */;
14636 400 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
14637 : /* Allow it, sigh. */;
14638 338 : else if (! is_main)
14639 276 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14640 : name);
14641 62 : else if (pedantic)
14642 4 : pedwarn (id_loc, OPT_Wpedantic,
14643 : "ISO C++ forbids declaration of %qs with no type", name);
14644 : else
14645 58 : warning_at (id_loc, OPT_Wreturn_type,
14646 : "ISO C++ forbids declaration of %qs with no type", name);
14647 :
14648 1643 : if (type_was_error_mark_node && template_parm_flag)
14649 : /* FIXME we should be able to propagate the error_mark_node as is
14650 : for other contexts too. */
14651 162 : type = error_mark_node;
14652 : else
14653 1481 : type = integer_type_node;
14654 : }
14655 :
14656 1087569455 : ctype = NULL_TREE;
14657 :
14658 1087569455 : if (explicit_intN)
14659 : {
14660 1164193 : if (! int_n_enabled_p[declspecs->int_n_idx])
14661 : {
14662 0 : error_at (declspecs->locations[ds_type_spec],
14663 : "%<__int%d%> is not supported by this target",
14664 0 : int_n_data[declspecs->int_n_idx].bitsize);
14665 0 : explicit_intN = false;
14666 : }
14667 : /* Don't pedwarn if the alternate "__intN__" form has been used instead
14668 : of "__intN". */
14669 1164193 : else if (!int_n_alt && pedantic)
14670 9911 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14671 : "ISO C++ does not support %<__int%d%> for %qs",
14672 9911 : int_n_data[declspecs->int_n_idx].bitsize, name);
14673 : }
14674 :
14675 : /* Now process the modifiers that were specified
14676 : and check for invalid combinations. */
14677 :
14678 : /* Long double is a special combination. */
14679 1087569455 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14680 : {
14681 9170657 : long_p = false;
14682 9170657 : type = cp_build_qualified_type (long_double_type_node,
14683 : cp_type_quals (type));
14684 : }
14685 :
14686 : /* Check all other uses of type modifiers. */
14687 :
14688 1087569455 : if (unsigned_p || signed_p || long_p || short_p)
14689 : {
14690 22087391 : location_t loc;
14691 22087391 : const char *key;
14692 22087391 : if (unsigned_p)
14693 : {
14694 15033183 : key = "unsigned";
14695 15033183 : loc = declspecs->locations[ds_unsigned];
14696 : }
14697 7054208 : else if (signed_p)
14698 : {
14699 1076361 : key = "signed";
14700 1076361 : loc = declspecs->locations[ds_signed];
14701 : }
14702 5977847 : else if (longlong)
14703 : {
14704 2327168 : key = "long long";
14705 2327168 : loc = declspecs->locations[ds_long_long];
14706 : }
14707 3650679 : else if (long_p)
14708 : {
14709 3067936 : key = "long";
14710 3067936 : loc = declspecs->locations[ds_long];
14711 : }
14712 : else /* if (short_p) */
14713 : {
14714 582743 : key = "short";
14715 582743 : loc = declspecs->locations[ds_short];
14716 : }
14717 :
14718 22087391 : int ok = 0;
14719 :
14720 22087391 : if (signed_p && unsigned_p)
14721 54 : complain_about_incompatible_declspecs
14722 54 : ("signed", declspecs->locations[ds_signed],
14723 : "unsigned", declspecs->locations[ds_unsigned]);
14724 22087337 : else if (long_p && short_p)
14725 12 : complain_about_incompatible_declspecs
14726 12 : ("long", declspecs->locations[ds_long],
14727 : "short", declspecs->locations[ds_short]);
14728 22087325 : else if (TREE_CODE (type) != INTEGER_TYPE
14729 22087312 : || type == char8_type_node
14730 22087288 : || type == char16_type_node
14731 22087264 : || type == char32_type_node
14732 22087240 : || ((long_p || short_p)
14733 11601783 : && (explicit_char || explicit_intN)))
14734 418 : error_at (loc, "%qs specified with %qT", key, type);
14735 22086907 : else if (!explicit_int && !defaulted_int
14736 22086907 : && !explicit_char && !explicit_intN)
14737 : {
14738 30 : if (typedef_decl)
14739 : {
14740 27 : pedwarn (loc, OPT_Wpedantic,
14741 : "%qs specified with typedef-name %qD",
14742 : key, typedef_decl);
14743 27 : ok = !flag_pedantic_errors;
14744 : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14745 : and is not built as a typedef. */
14746 27 : if (is_typedef_decl (typedef_decl))
14747 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
14748 : }
14749 3 : else if (declspecs->decltype_p)
14750 0 : error_at (loc, "%qs specified with %<decltype%>", key);
14751 : else
14752 3 : error_at (loc, "%qs specified with %<typeof%>", key);
14753 : }
14754 : else
14755 : ok = 1;
14756 :
14757 : /* Discard the type modifiers if they are invalid. */
14758 514 : if (! ok)
14759 : {
14760 : unsigned_p = false;
14761 : signed_p = false;
14762 : long_p = false;
14763 : short_p = false;
14764 : longlong = 0;
14765 : }
14766 : }
14767 :
14768 : /* Decide whether an integer type is signed or not.
14769 : Optionally treat bitfields as signed by default. */
14770 1087568965 : if (unsigned_p
14771 : /* [class.bit]
14772 :
14773 : It is implementation-defined whether a plain (neither
14774 : explicitly signed or unsigned) char, short, int, or long
14775 : bit-field is signed or unsigned.
14776 :
14777 : Naturally, we extend this to long long as well. Note that
14778 : this does not include wchar_t. */
14779 1087569455 : || (bitfield && !flag_signed_bitfields
14780 18 : && !signed_p
14781 : /* A typedef for plain `int' without `signed' can be
14782 : controlled just like plain `int', but a typedef for
14783 : `signed int' cannot be so controlled. */
14784 18 : && !(typedef_decl
14785 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14786 13 : && TREE_CODE (type) == INTEGER_TYPE
14787 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14788 : {
14789 15032968 : if (explicit_intN)
14790 549789 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14791 14483179 : else if (longlong)
14792 1487150 : type = long_long_unsigned_type_node;
14793 12996029 : else if (long_p)
14794 2441938 : type = long_unsigned_type_node;
14795 10554091 : else if (short_p)
14796 1478045 : type = short_unsigned_type_node;
14797 9076046 : else if (type == char_type_node)
14798 1979196 : type = unsigned_char_type_node;
14799 7096850 : else if (typedef_decl)
14800 25 : type = c_common_unsigned_type (type);
14801 : else
14802 7096825 : type = unsigned_type_node;
14803 : }
14804 1072536487 : else if (signed_p && type == char_type_node)
14805 709842 : type = signed_char_type_node;
14806 1071826645 : else if (explicit_intN)
14807 614404 : type = int_n_trees[declspecs->int_n_idx].signed_type;
14808 1071212241 : else if (longlong)
14809 2392702 : type = long_long_integer_type_node;
14810 1068819539 : else if (long_p)
14811 3137830 : type = long_integer_type_node;
14812 1065681709 : else if (short_p)
14813 663785 : type = short_integer_type_node;
14814 1065017924 : else if (signed_p && typedef_decl)
14815 9 : type = c_common_signed_type (type);
14816 :
14817 1087569455 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14818 : {
14819 1278927 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14820 3 : error_at (declspecs->locations[ds_complex],
14821 : "complex invalid for %qs", name);
14822 : /* If a modifier is specified, the resulting complex is the complex
14823 : form of TYPE. E.g, "complex short" is "complex short int". */
14824 1278924 : else if (type == integer_type_node)
14825 171 : type = complex_integer_type_node;
14826 1278753 : else if (type == float_type_node)
14827 349457 : type = complex_float_type_node;
14828 929296 : else if (type == double_type_node)
14829 349998 : type = complex_double_type_node;
14830 579298 : else if (type == long_double_type_node)
14831 349498 : type = complex_long_double_type_node;
14832 : else
14833 229800 : type = build_complex_type (type);
14834 : }
14835 :
14836 : /* If we're using the injected-class-name to form a compound type or a
14837 : declaration, replace it with the underlying class so we don't get
14838 : redundant typedefs in the debug output. But if we are returning the
14839 : type unchanged, leave it alone so that it's available to
14840 : maybe_get_template_decl_from_type_decl. */
14841 212961089 : if (CLASS_TYPE_P (type)
14842 212895913 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14843 36532383 : && type == TREE_TYPE (TYPE_NAME (type))
14844 1124101838 : && (declarator || type_quals))
14845 34884829 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14846 :
14847 1087569455 : type_quals |= cp_type_quals (type);
14848 2175138910 : type = cp_build_qualified_type
14849 2175138910 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14850 880635567 : || declspecs->decltype_p)
14851 : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14852 : /* We might have ignored or rejected some of the qualifiers. */
14853 1087569455 : type_quals = cp_type_quals (type);
14854 :
14855 1071620009 : if (cxx_dialect >= cxx17 && type && is_auto (type)
14856 23763657 : && innermost_code != cdk_function
14857 : /* Placeholder in parm gets a better error below. */
14858 14761046 : && !(decl_context == PARM || decl_context == CATCHPARM)
14859 1102291976 : && id_declarator && declarator != id_declarator)
14860 1202414 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14861 : {
14862 16 : auto_diagnostic_group g;
14863 16 : gcc_rich_location richloc (typespec_loc);
14864 16 : richloc.add_fixit_insert_after ("<>");
14865 16 : error_at (&richloc, "missing template argument list after %qE; "
14866 : "for deduction, template placeholder must be followed "
14867 : "by a simple declarator-id", tmpl);
14868 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14869 16 : type = error_mark_node;
14870 16 : }
14871 :
14872 1087569455 : staticp = 0;
14873 1087569455 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14874 1087569455 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14875 1087569455 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14876 :
14877 1087569455 : storage_class = declspecs->storage_class;
14878 1087569455 : if (storage_class == sc_static)
14879 25069333 : staticp = 1 + (decl_context == FIELD);
14880 1062500122 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14881 : /* Treat class-scope deduction guides as static member functions
14882 : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14883 24734822 : staticp = 2;
14884 :
14885 1087569455 : if (virtualp)
14886 : {
14887 4293625 : if (staticp == 2)
14888 : {
14889 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14890 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
14891 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14892 : "and %<static%>", dname);
14893 21 : storage_class = sc_none;
14894 21 : staticp = 0;
14895 21 : }
14896 4293625 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14897 : {
14898 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14899 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14900 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14901 : "declared both %<virtual%> and %<constexpr%> only in "
14902 : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14903 6 : }
14904 : }
14905 1087569455 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14906 :
14907 : /* Issue errors about use of storage classes for parameters. */
14908 1087569455 : if (decl_context == PARM)
14909 : {
14910 307419917 : if (typedef_p)
14911 : {
14912 24 : error_at (declspecs->locations[ds_typedef],
14913 : "typedef declaration invalid in parameter declaration");
14914 24 : return error_mark_node;
14915 : }
14916 307419893 : else if (template_parm_flag && storage_class != sc_none)
14917 : {
14918 16 : error_at (min_location (declspecs->locations[ds_thread],
14919 : declspecs->locations[ds_storage_class]),
14920 : "storage class specified for template parameter %qs",
14921 : name);
14922 16 : return error_mark_node;
14923 : }
14924 307419877 : else if (storage_class == sc_static
14925 307419877 : || storage_class == sc_extern
14926 307419874 : || thread_p)
14927 : {
14928 9 : error_at (min_location (declspecs->locations[ds_thread],
14929 : declspecs->locations[ds_storage_class]),
14930 : "storage class specified for parameter %qs", name);
14931 9 : return error_mark_node;
14932 : }
14933 :
14934 : /* Function parameters cannot be concept. */
14935 307419868 : if (concept_p)
14936 : {
14937 9 : error_at (declspecs->locations[ds_concept],
14938 : "a parameter cannot be declared %qs", "concept");
14939 9 : concept_p = 0;
14940 9 : constexpr_p = 0;
14941 : }
14942 : /* Function parameters cannot be constexpr. If we saw one, moan
14943 : and pretend it wasn't there. */
14944 307419859 : else if (constexpr_p)
14945 : {
14946 9 : error_at (declspecs->locations[ds_constexpr],
14947 : "a parameter cannot be declared %qs", "constexpr");
14948 9 : constexpr_p = 0;
14949 : }
14950 307419868 : if (constinit_p)
14951 : {
14952 6 : error_at (declspecs->locations[ds_constinit],
14953 : "a parameter cannot be declared %qs", "constinit");
14954 6 : constinit_p = 0;
14955 : }
14956 307419868 : if (consteval_p)
14957 : {
14958 3 : error_at (declspecs->locations[ds_consteval],
14959 : "a parameter cannot be declared %qs", "consteval");
14960 3 : consteval_p = 0;
14961 : }
14962 : }
14963 :
14964 : /* Give error if `virtual' is used outside of class declaration. */
14965 1087569406 : if (virtualp
14966 4293625 : && (current_class_name == NULL_TREE || decl_context != FIELD))
14967 : {
14968 68 : error_at (declspecs->locations[ds_virtual],
14969 : "%<virtual%> outside class declaration");
14970 68 : virtualp = 0;
14971 : }
14972 :
14973 1087569406 : if (innermost_code == cdk_decomp)
14974 : {
14975 237081 : location_t loc = (declarator->kind == cdk_reference
14976 247390 : ? declarator->declarator->id_loc : declarator->id_loc);
14977 247390 : if (inlinep)
14978 29 : error_at (declspecs->locations[ds_inline],
14979 : "structured binding declaration cannot be %qs", "inline");
14980 247390 : if (typedef_p)
14981 29 : error_at (declspecs->locations[ds_typedef],
14982 : "structured binding declaration cannot be %qs", "typedef");
14983 247390 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14984 101 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14985 : "structured binding declaration can be %qs only with "
14986 : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14987 247390 : if (consteval_p)
14988 13 : error_at (declspecs->locations[ds_consteval], "structured "
14989 : "binding declaration cannot be %qs", "consteval");
14990 247390 : if (thread_p && cxx_dialect < cxx20)
14991 75 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14992 : "structured binding declaration can be %qs only in "
14993 : "%<-std=c++20%> or %<-std=gnu++20%>",
14994 75 : declspecs->gnu_thread_keyword_p
14995 : ? "__thread" : "thread_local");
14996 247390 : if (concept_p)
14997 : {
14998 0 : error_at (declspecs->locations[ds_concept],
14999 : "structured binding declaration cannot be %qs", "concept");
15000 0 : constexpr_p = 0;
15001 : }
15002 : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
15003 247390 : if (type_quals & TYPE_QUAL_VOLATILE)
15004 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
15005 : "%<volatile%>-qualified structured binding is deprecated");
15006 247390 : switch (storage_class)
15007 : {
15008 : case sc_none:
15009 : break;
15010 28 : case sc_register:
15011 28 : error_at (loc, "structured binding declaration cannot be %qs",
15012 : "register");
15013 28 : break;
15014 252 : case sc_static:
15015 252 : if (cxx_dialect < cxx20)
15016 185 : pedwarn (loc, OPT_Wc__20_extensions,
15017 : "structured binding declaration can be %qs only in "
15018 : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
15019 : break;
15020 29 : case sc_extern:
15021 29 : error_at (loc, "structured binding declaration cannot be %qs",
15022 : "extern");
15023 29 : break;
15024 28 : case sc_mutable:
15025 28 : error_at (loc, "structured binding declaration cannot be %qs",
15026 : "mutable");
15027 28 : break;
15028 0 : case sc_auto:
15029 0 : error_at (loc, "structured binding declaration cannot be "
15030 : "C++98 %<auto%>");
15031 0 : break;
15032 0 : default:
15033 0 : gcc_unreachable ();
15034 : }
15035 247390 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
15036 247390 : || TYPE_IDENTIFIER (type) != auto_identifier)
15037 : {
15038 3 : if (type != error_mark_node)
15039 : {
15040 3 : auto_diagnostic_group d;
15041 3 : error_at (loc, "structured binding declaration cannot have "
15042 : "type %qT", type);
15043 3 : inform (loc,
15044 : "type must be cv-qualified %<auto%> or reference to "
15045 : "cv-qualified %<auto%>");
15046 3 : }
15047 3 : type = build_qualified_type (make_auto (), type_quals);
15048 3 : declspecs->type = type;
15049 : }
15050 247387 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
15051 51 : pedwarn (loc, OPT_Wpedantic,
15052 : "structured binding declaration cannot have constrained "
15053 : "%<auto%> type %qT", type);
15054 247390 : inlinep = 0;
15055 247390 : typedef_p = 0;
15056 247390 : consteval_p = 0;
15057 247390 : concept_p = 0;
15058 247390 : if (storage_class != sc_static)
15059 : {
15060 247138 : storage_class = sc_none;
15061 247138 : declspecs->storage_class = sc_none;
15062 : }
15063 : }
15064 :
15065 : /* Static anonymous unions are dealt with here. */
15066 1087569406 : if (staticp && decl_context == TYPENAME
15067 0 : && declspecs->type
15068 1087569406 : && ANON_AGGR_TYPE_P (declspecs->type))
15069 : decl_context = FIELD;
15070 :
15071 : /* Warn about storage classes that are invalid for certain
15072 : kinds of declarations (parameters, typenames, etc.). */
15073 1087569406 : if (thread_p
15074 20626 : && ((storage_class
15075 20626 : && storage_class != sc_extern
15076 528 : && storage_class != sc_static)
15077 20615 : || typedef_p))
15078 : {
15079 17 : location_t loc
15080 17 : = min_location (declspecs->locations[ds_thread],
15081 : declspecs->locations[ds_storage_class]);
15082 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
15083 17 : thread_p = false;
15084 : }
15085 1087569406 : if (decl_context != NORMAL
15086 911612670 : && ((storage_class != sc_none
15087 911612670 : && storage_class != sc_mutable)
15088 886877862 : || thread_p))
15089 : {
15090 24734811 : if ((decl_context == PARM || decl_context == CATCHPARM)
15091 36 : && (storage_class == sc_register
15092 36 : || storage_class == sc_auto))
15093 : ;
15094 24734775 : else if (typedef_p)
15095 : ;
15096 24734775 : else if (decl_context == FIELD
15097 : /* C++ allows static class elements. */
15098 24734775 : && storage_class == sc_static)
15099 : /* C++ also allows inlines and signed and unsigned elements,
15100 : but in those cases we don't come in here. */
15101 : ;
15102 : else
15103 : {
15104 15 : location_t loc
15105 15 : = min_location (declspecs->locations[ds_thread],
15106 : declspecs->locations[ds_storage_class]);
15107 15 : if (decl_context == FIELD)
15108 15 : error_at (loc, "storage class specified for %qs", name);
15109 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
15110 0 : error_at (loc, "storage class specified for parameter %qs", name);
15111 : else
15112 0 : error_at (loc, "storage class specified for typename");
15113 15 : if (storage_class == sc_register
15114 15 : || storage_class == sc_auto
15115 15 : || storage_class == sc_extern
15116 3 : || thread_p)
15117 15 : storage_class = sc_none;
15118 : }
15119 : }
15120 1062834595 : else if (storage_class == sc_extern && funcdef_flag
15121 1062834595 : && ! toplevel_bindings_p ())
15122 0 : error ("nested function %qs declared %<extern%>", name);
15123 1062834595 : else if (toplevel_bindings_p ())
15124 : {
15125 495456681 : if (storage_class == sc_auto)
15126 2 : error_at (declspecs->locations[ds_storage_class],
15127 : "top-level declaration of %qs specifies %<auto%>", name);
15128 : }
15129 567377914 : else if (thread_p
15130 567377914 : && storage_class != sc_extern
15131 243 : && storage_class != sc_static)
15132 : {
15133 210 : if (declspecs->gnu_thread_keyword_p)
15134 49 : pedwarn (declspecs->locations[ds_thread],
15135 49 : 0, "function-scope %qs implicitly auto and "
15136 : "declared %<__thread%>", name);
15137 :
15138 : /* When thread_local is applied to a variable of block scope the
15139 : storage-class-specifier static is implied if it does not appear
15140 : explicitly. */
15141 210 : storage_class = declspecs->storage_class = sc_static;
15142 210 : staticp = 1;
15143 : }
15144 :
15145 1087569406 : if (storage_class && friendp)
15146 : {
15147 12 : error_at (min_location (declspecs->locations[ds_thread],
15148 : declspecs->locations[ds_storage_class]),
15149 : "storage class specifiers invalid in friend function "
15150 : "declarations");
15151 12 : storage_class = sc_none;
15152 12 : staticp = 0;
15153 : }
15154 :
15155 1087569406 : if (!id_declarator)
15156 : unqualified_id = NULL_TREE;
15157 : else
15158 : {
15159 612143769 : unqualified_id = id_declarator->u.id.unqualified_name;
15160 612143769 : switch (TREE_CODE (unqualified_id))
15161 : {
15162 3389686 : case BIT_NOT_EXPR:
15163 3389686 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
15164 3389686 : if (TYPE_P (unqualified_id))
15165 3389683 : unqualified_id = constructor_name (unqualified_id);
15166 : break;
15167 :
15168 : case IDENTIFIER_NODE:
15169 : case TEMPLATE_ID_EXPR:
15170 : break;
15171 :
15172 0 : default:
15173 0 : gcc_unreachable ();
15174 : }
15175 : }
15176 :
15177 1087569406 : if (declspecs->std_attributes)
15178 : {
15179 795 : location_t attr_loc = declspecs->locations[ds_std_attribute];
15180 795 : auto_diagnostic_group d;
15181 795 : if (any_nonignored_attribute_p (declspecs->std_attributes)
15182 795 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
15183 792 : inform (attr_loc, "an attribute that appertains to a type-specifier "
15184 : "is ignored");
15185 795 : }
15186 :
15187 : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
15188 1087569406 : bool is_xobj_member_function = false;
15189 : /* Determine the type of the entity declared by recurring on the
15190 : declarator. */
15191 1493298655 : for (; declarator; declarator = declarator->declarator)
15192 : {
15193 1018120434 : const cp_declarator *inner_declarator;
15194 1018120434 : tree attrs;
15195 :
15196 1018120434 : if (type == error_mark_node)
15197 607 : return error_mark_node;
15198 :
15199 1018120106 : attrs = declarator->attributes;
15200 1018120106 : if (attrs)
15201 : {
15202 13756 : int attr_flags;
15203 :
15204 13756 : attr_flags = 0;
15205 13756 : if (declarator->kind == cdk_id)
15206 13048 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
15207 13756 : if (declarator->kind == cdk_function)
15208 691 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
15209 13756 : if (declarator->kind == cdk_array)
15210 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
15211 13756 : tree late_attrs = NULL_TREE;
15212 13756 : if (decl_context != PARM && decl_context != TYPENAME)
15213 : /* Assume that any attributes that get applied late to
15214 : templates will DTRT when applied to the declaration
15215 : as a whole. */
15216 13744 : late_attrs = splice_template_attributes (&attrs, type);
15217 13756 : returned_attrs = decl_attributes (&type,
15218 : attr_chainon (returned_attrs,
15219 : attrs),
15220 : attr_flags);
15221 13756 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
15222 : }
15223 :
15224 1018120106 : inner_declarator = declarator->declarator;
15225 :
15226 : /* We don't want to warn in parameter context because we don't
15227 : yet know if the parse will succeed, and this might turn out
15228 : to be a constructor call. */
15229 1018120106 : if (decl_context != PARM
15230 1018120106 : && decl_context != TYPENAME
15231 591222201 : && !typedef_p
15232 540674042 : && declarator->parenthesized != UNKNOWN_LOCATION
15233 : /* If the type is class-like and the inner name used a
15234 : global namespace qualifier, we need the parens.
15235 : Unfortunately all we can tell is whether a qualified name
15236 : was used or not. */
15237 1018120287 : && !(inner_declarator
15238 126 : && inner_declarator->kind == cdk_id
15239 108 : && inner_declarator->u.id.qualifying_scope
15240 18 : && (MAYBE_CLASS_TYPE_P (type)
15241 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
15242 : {
15243 169 : auto_diagnostic_group d;
15244 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
15245 : "unnecessary parentheses in declaration of %qs",
15246 : name))
15247 : {
15248 42 : gcc_rich_location iloc (declarator->parenthesized);
15249 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
15250 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
15251 42 : inform (&iloc, "remove parentheses");
15252 42 : }
15253 169 : }
15254 1018120106 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
15255 : break;
15256 :
15257 405729528 : switch (declarator->kind)
15258 : {
15259 3765748 : case cdk_array:
15260 7531496 : type = create_array_type_for_decl (dname, type,
15261 3765748 : declarator->u.array.bounds,
15262 3765748 : declarator->id_loc);
15263 3765748 : if (!valid_array_size_p (dname
15264 : ? declarator->id_loc : input_location,
15265 : type, dname))
15266 267 : type = error_mark_node;
15267 :
15268 3765748 : if (declarator->std_attributes)
15269 : /* [dcl.array]/1:
15270 :
15271 : The optional attribute-specifier-seq appertains to the
15272 : array type. */
15273 209 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15274 : break;
15275 :
15276 199093803 : case cdk_function:
15277 199093803 : {
15278 199093803 : tree arg_types;
15279 199093803 : int funcdecl_p;
15280 :
15281 : /* Declaring a function type. */
15282 :
15283 : /* Pick up type qualifiers which should be applied to `this'. */
15284 199093803 : memfn_quals = declarator->u.function.qualifiers;
15285 : /* Pick up virt-specifiers. */
15286 199093803 : virt_specifiers = declarator->u.function.virt_specifiers;
15287 : /* And ref-qualifier, too */
15288 199093803 : rqual = declarator->u.function.ref_qualifier;
15289 : /* And tx-qualifier. */
15290 199093803 : tree tx_qual = declarator->u.function.tx_qualifier;
15291 : /* Pick up the exception specifications. */
15292 199093803 : raises = declarator->u.function.exception_specification;
15293 : /* If the exception-specification is ill-formed, let's pretend
15294 : there wasn't one. */
15295 199093803 : if (raises == error_mark_node)
15296 9 : raises = NULL_TREE;
15297 :
15298 398187606 : auto find_xobj_parm = [](tree parm_list)
15299 : {
15300 : /* There is no need to iterate over the list,
15301 : only the first parm can be a valid xobj parm. */
15302 397948835 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
15303 : return NULL_TREE;
15304 : /* If we make it here, we are looking at an xobj parm.
15305 :
15306 : Non-null 'purpose' usually means the parm has a default
15307 : argument, we don't want to violate this assumption. */
15308 94353 : TREE_PURPOSE (parm_list) = NULL_TREE;
15309 94353 : return TREE_VALUE (parm_list);
15310 : };
15311 :
15312 199093803 : tree xobj_parm
15313 199093803 : = find_xobj_parm (declarator->u.function.parameters);
15314 199093803 : is_xobj_member_function = xobj_parm;
15315 :
15316 199093803 : if (xobj_parm && cxx_dialect < cxx23)
15317 42476 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
15318 : "explicit object member function only available "
15319 : "with %<-std=c++23%> or %<-std=gnu++23%>");
15320 :
15321 199093803 : if (xobj_parm && decl_context == TYPENAME)
15322 : {
15323 : /* We inform in every case, just differently depending on what
15324 : case it is. */
15325 9 : auto_diagnostic_group d;
15326 9 : bool ptr_type = true;
15327 : /* If declarator->kind is cdk_function and we are at the end of
15328 : the declarator chain, we are looking at a function type. */
15329 9 : if (!declarator->declarator)
15330 : {
15331 3 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15332 : "a function type cannot "
15333 : "have an explicit object parameter");
15334 3 : ptr_type = false;
15335 : }
15336 6 : else if (declarator->declarator->kind == cdk_pointer)
15337 3 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15338 : "a pointer to function type cannot "
15339 : "have an explicit object parameter");
15340 3 : else if (declarator->declarator->kind == cdk_ptrmem)
15341 3 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15342 : "a pointer to member function type "
15343 : "cannot have an explicit object parameter");
15344 : else
15345 0 : gcc_unreachable ();
15346 :
15347 : /* The locations being used here are probably not correct. */
15348 6 : if (ptr_type)
15349 6 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15350 : "the type of a pointer to explicit object member "
15351 : "function is a regular pointer to function type");
15352 : else
15353 3 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15354 : "the type of an explicit object "
15355 : "member function is a regular function type");
15356 : /* Ideally we should synthesize the correct syntax
15357 : for the user, perhaps this could be added later. */
15358 9 : }
15359 : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
15360 : the first parm node will never erroneously be detected here. */
15361 199093803 : {
15362 199093803 : auto_diagnostic_group d;
15363 199093803 : bool bad_xobj_parm_encountered = false;
15364 199093803 : for (tree parm = declarator->u.function.parameters;
15365 494014310 : parm && parm != void_list_node;
15366 294920507 : parm = TREE_CHAIN (parm))
15367 : {
15368 294920507 : if (TREE_PURPOSE (parm) != this_identifier)
15369 294920498 : continue;
15370 9 : bad_xobj_parm_encountered = true;
15371 9 : TREE_PURPOSE (parm) = NULL_TREE;
15372 9 : gcc_rich_location bad_xobj_parm
15373 9 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
15374 9 : error_at (&bad_xobj_parm,
15375 : "only the first parameter of a member function "
15376 : "can be declared as an explicit object parameter");
15377 9 : }
15378 199093803 : if (bad_xobj_parm_encountered && xobj_parm)
15379 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15380 : "valid explicit object parameter declared here");
15381 199093803 : }
15382 :
15383 199093803 : if (reqs)
15384 3 : error_at (location_of (reqs), "requires-clause on return type");
15385 199093803 : reqs = declarator->u.function.requires_clause;
15386 :
15387 : /* Say it's a definition only for the CALL_EXPR
15388 : closest to the identifier. */
15389 199093803 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15390 :
15391 : /* Handle a late-specified return type. */
15392 199093803 : tree late_return_type = declarator->u.function.late_return_type;
15393 199093803 : if (tree auto_node = type_uses_auto (type))
15394 : {
15395 9014495 : if (!late_return_type)
15396 : {
15397 5249576 : if (!funcdecl_p)
15398 : /* auto (*fp)() = f; is OK. */;
15399 5249542 : else if (current_class_type
15400 9652696 : && LAMBDA_TYPE_P (current_class_type))
15401 : /* OK for C++11 lambdas. */;
15402 3375104 : else if (cxx_dialect < cxx14)
15403 : {
15404 7 : auto_diagnostic_group d;
15405 7 : error_at (typespec_loc, "%qs function uses "
15406 : "%<auto%> type specifier without "
15407 : "trailing return type", name);
15408 7 : inform (typespec_loc,
15409 : "deduced return type only available "
15410 : "with %<-std=c++14%> or %<-std=gnu++14%>");
15411 7 : }
15412 3375097 : else if (virtualp)
15413 : {
15414 6 : error_at (typespec_loc, "virtual function "
15415 : "cannot have deduced return type");
15416 6 : virtualp = false;
15417 : }
15418 : }
15419 3764919 : else if (!is_auto (type) && sfk != sfk_conversion)
15420 : {
15421 15 : error_at (typespec_loc, "%qs function with trailing "
15422 : "return type has %qT as its type rather "
15423 : "than plain %<auto%>", name, type);
15424 276 : return error_mark_node;
15425 : }
15426 3764904 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15427 : {
15428 9 : if (funcdecl_p)
15429 3 : error_at (typespec_loc,
15430 : "%qs function with trailing return type "
15431 : "has %<decltype(auto)%> as its type "
15432 : "rather than plain %<auto%>", name);
15433 : else
15434 6 : error_at (typespec_loc,
15435 : "invalid use of %<decltype(auto)%>");
15436 9 : return error_mark_node;
15437 : }
15438 3764895 : else if (is_constrained_auto (type))
15439 : {
15440 6 : if (funcdecl_p)
15441 3 : error_at (typespec_loc,
15442 : "%qs function with trailing return type "
15443 : "has constrained %<auto%> type specifier "
15444 : "rather than plain %<auto%>",
15445 : name);
15446 : else
15447 3 : error_at (typespec_loc,
15448 : "invalid use of constrained %<auto%> type");
15449 6 : return error_mark_node;
15450 : }
15451 9014465 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15452 9014465 : if (!tmpl)
15453 7743616 : if (tree late_auto = type_uses_auto (late_return_type))
15454 40148 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15455 7743616 : if (tmpl)
15456 : {
15457 1270855 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
15458 : {
15459 13 : auto_diagnostic_group g;
15460 13 : error_at (typespec_loc, "deduced class "
15461 : "type %qD in function return type",
15462 13 : DECL_NAME (tmpl));
15463 13 : inform (DECL_SOURCE_LOCATION (tmpl),
15464 : "%qD declared here", tmpl);
15465 13 : return error_mark_node;
15466 13 : }
15467 1270842 : else if (!late_return_type)
15468 : {
15469 3 : auto_diagnostic_group d;
15470 3 : error_at (declarator->id_loc, "deduction guide "
15471 : "for %qT must have trailing return "
15472 3 : "type", TREE_TYPE (tmpl));
15473 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15474 : "%qD declared here", tmpl);
15475 3 : return error_mark_node;
15476 3 : }
15477 1270839 : else if (CLASS_TYPE_P (late_return_type)
15478 1270839 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15479 2541678 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15480 : == tmpl))
15481 : /* OK */;
15482 : else
15483 0 : error ("trailing return type %qT of deduction guide "
15484 : "is not a specialization of %qT",
15485 0 : late_return_type, TREE_TYPE (tmpl));
15486 : }
15487 : }
15488 190079308 : else if (late_return_type
15489 190079308 : && sfk != sfk_conversion)
15490 : {
15491 37 : if (late_return_type == error_mark_node)
15492 : return error_mark_node;
15493 37 : if (cxx_dialect < cxx11)
15494 : /* Not using maybe_warn_cpp0x because this should
15495 : always be an error. */
15496 1 : error_at (typespec_loc,
15497 : "trailing return type only available "
15498 : "with %<-std=c++11%> or %<-std=gnu++11%>");
15499 : else
15500 36 : error_at (typespec_loc, "%qs function with trailing "
15501 : "return type not declared with %<auto%> "
15502 : "type specifier", name);
15503 37 : return error_mark_node;
15504 : }
15505 199093720 : if (late_return_type && sfk == sfk_conversion)
15506 : {
15507 14 : error ("a conversion function cannot have a trailing return type");
15508 14 : return error_mark_node;
15509 : }
15510 199093706 : type = splice_late_return_type (type, late_return_type);
15511 199093706 : if (type == error_mark_node)
15512 : return error_mark_node;
15513 :
15514 199093628 : if (late_return_type)
15515 : {
15516 3764797 : late_return_type_p = true;
15517 3764797 : type_quals = cp_type_quals (type);
15518 : }
15519 :
15520 199093628 : if (type_quals != TYPE_UNQUALIFIED)
15521 : {
15522 : /* It's wrong, for instance, to issue a -Wignored-qualifiers
15523 : warning for
15524 : static_assert(!is_same_v<void(*)(), const void(*)()>);
15525 : because there the qualifier matters. */
15526 12202 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15527 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15528 : "qualifiers ignored on function return type");
15529 : /* [dcl.fct] "A volatile-qualified return type is
15530 : deprecated." */
15531 12202 : if (type_quals & TYPE_QUAL_VOLATILE)
15532 599 : warning_at (typespec_loc, OPT_Wvolatile,
15533 : "%<volatile%>-qualified return type is "
15534 : "deprecated");
15535 :
15536 : /* We now know that the TYPE_QUALS don't apply to the
15537 : decl, but to its return type. */
15538 : type_quals = TYPE_UNQUALIFIED;
15539 : }
15540 :
15541 : /* Error about some types functions can't return. */
15542 :
15543 199093628 : if (TREE_CODE (type) == FUNCTION_TYPE)
15544 : {
15545 30 : error_at (typespec_loc, "%qs declared as function returning "
15546 : "a function", name);
15547 30 : return error_mark_node;
15548 : }
15549 199093598 : if (TREE_CODE (type) == ARRAY_TYPE)
15550 : {
15551 6 : error_at (typespec_loc, "%qs declared as function returning "
15552 : "an array", name);
15553 6 : return error_mark_node;
15554 : }
15555 199093592 : if (constinit_p && funcdecl_p)
15556 : {
15557 15 : error_at (declspecs->locations[ds_constinit],
15558 : "%<constinit%> on function return type is not "
15559 : "allowed");
15560 15 : return error_mark_node;
15561 : }
15562 :
15563 199093577 : if (check_decltype_auto (typespec_loc, type))
15564 50 : return error_mark_node;
15565 :
15566 199093527 : if (ctype == NULL_TREE
15567 199093527 : && decl_context == FIELD
15568 : && funcdecl_p
15569 114990263 : && friendp == 0)
15570 107882653 : ctype = current_class_type;
15571 :
15572 199093527 : if (ctype && (sfk == sfk_constructor
15573 107882653 : || sfk == sfk_destructor))
15574 : {
15575 : /* We are within a class's scope. If our declarator name
15576 : is the same as the class name, and we are defining
15577 : a function, then it is a constructor/destructor, and
15578 : therefore returns a void type. */
15579 :
15580 : /* ISO C++ 12.4/2. A destructor may not be declared
15581 : const or volatile. A destructor may not be static.
15582 : A destructor may not be declared with ref-qualifier.
15583 :
15584 : ISO C++ 12.1. A constructor may not be declared
15585 : const or volatile. A constructor may not be
15586 : virtual. A constructor may not be static.
15587 : A constructor may not be declared with ref-qualifier. */
15588 27590054 : if (staticp == 2)
15589 9 : error_at (declspecs->locations[ds_storage_class],
15590 : (flags == DTOR_FLAG)
15591 : ? G_("destructor cannot be static member "
15592 : "function")
15593 : : G_("constructor cannot be static member "
15594 : "function"));
15595 27590054 : if (memfn_quals)
15596 : {
15597 9 : error ((flags == DTOR_FLAG)
15598 : ? G_("destructors may not be cv-qualified")
15599 : : G_("constructors may not be cv-qualified"));
15600 9 : memfn_quals = TYPE_UNQUALIFIED;
15601 : }
15602 :
15603 27590054 : if (rqual)
15604 : {
15605 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
15606 6 : error ((flags == DTOR_FLAG)
15607 : ? G_("destructors may not be ref-qualified")
15608 : : G_("constructors may not be ref-qualified"));
15609 6 : rqual = REF_QUAL_NONE;
15610 : }
15611 :
15612 27590054 : if (decl_context == FIELD
15613 27590054 : && !member_function_or_else (ctype,
15614 : current_class_type,
15615 : flags))
15616 0 : return error_mark_node;
15617 :
15618 27590054 : if (flags != DTOR_FLAG)
15619 : {
15620 : /* It's a constructor. */
15621 24373141 : if (explicitp == 1)
15622 5681500 : explicitp = 2;
15623 24373141 : if (virtualp)
15624 : {
15625 3 : permerror (declspecs->locations[ds_virtual],
15626 : "constructors cannot be declared %<virtual%>");
15627 3 : virtualp = 0;
15628 : }
15629 24373141 : if (decl_context == FIELD
15630 24373141 : && sfk != sfk_constructor)
15631 0 : return error_mark_node;
15632 : }
15633 27590054 : if (decl_context == FIELD)
15634 27590054 : staticp = 0;
15635 : }
15636 171503473 : else if (friendp)
15637 : {
15638 6778443 : if (virtualp)
15639 : {
15640 : /* Cannot be both friend and virtual. */
15641 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15642 12 : richloc.add_range (declspecs->locations[ds_friend]);
15643 12 : error_at (&richloc, "virtual functions cannot be friends");
15644 12 : friendp = 0;
15645 12 : }
15646 6778443 : if (decl_context == NORMAL)
15647 0 : error_at (declarator->id_loc,
15648 : "friend declaration not in class definition");
15649 6778443 : if (current_function_decl && funcdef_flag)
15650 : {
15651 9 : error_at (declarator->id_loc,
15652 : "cannot define friend function %qs in a local "
15653 : "class definition", name);
15654 9 : friendp = 0;
15655 : }
15656 : /* [class.friend]/6: A function can be defined in a friend
15657 : declaration if the function name is unqualified. */
15658 6778443 : if (funcdef_flag && in_namespace)
15659 : {
15660 6 : if (in_namespace == global_namespace)
15661 3 : error_at (declarator->id_loc,
15662 : "friend function definition %qs cannot have "
15663 : "a name qualified with %<::%>", name);
15664 : else
15665 3 : error_at (declarator->id_loc,
15666 : "friend function definition %qs cannot have "
15667 : "a name qualified with %<%D::%>", name,
15668 : in_namespace);
15669 : }
15670 : }
15671 164725030 : else if (ctype && sfk == sfk_conversion)
15672 : {
15673 1065291 : if (explicitp == 1)
15674 : {
15675 498484 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
15676 498484 : explicitp = 2;
15677 : }
15678 : }
15679 163659739 : else if (sfk == sfk_deduction_guide)
15680 : {
15681 1270839 : if (explicitp == 1)
15682 199093527 : explicitp = 2;
15683 : }
15684 :
15685 199093527 : if (xobj_parm)
15686 : {
15687 94353 : if (!ctype
15688 110 : && (decl_context == NORMAL || friendp)
15689 101 : && (in_namespace
15690 101 : || !declarator->declarator->u.id.qualifying_scope))
15691 : {
15692 24 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15693 : "a non-member function cannot have "
15694 : "an explicit object parameter");
15695 24 : is_xobj_member_function = false;
15696 : }
15697 : else
15698 : {
15699 94329 : if (virtualp)
15700 : {
15701 24 : auto_diagnostic_group d;
15702 24 : error_at (declspecs->locations[ds_virtual],
15703 : "an explicit object member function cannot "
15704 : "be %<virtual%>");
15705 24 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15706 : "explicit object parameter declared here");
15707 24 : virtualp = false;
15708 24 : }
15709 94329 : if (staticp >= 2)
15710 : {
15711 3 : auto_diagnostic_group d;
15712 3 : error_at (declspecs->locations[ds_storage_class],
15713 : "an explicit object member function cannot "
15714 : "be %<static%>");
15715 3 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15716 : "explicit object parameter declared here");
15717 3 : }
15718 94329 : if (unqualified_id
15719 199187847 : && identifier_p (unqualified_id)
15720 188649 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15721 12 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15722 : "%qD cannot be an explicit object member "
15723 : "function", unqualified_id);
15724 : }
15725 : }
15726 199093527 : tree pushed_scope = NULL_TREE;
15727 199093527 : if (funcdecl_p
15728 199093527 : && decl_context != FIELD
15729 82115057 : && inner_declarator->u.id.qualifying_scope
15730 208426401 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15731 9331446 : pushed_scope
15732 9331446 : = push_scope (inner_declarator->u.id.qualifying_scope);
15733 :
15734 199093527 : arg_types = grokparms (declarator->u.function.parameters, &parms);
15735 :
15736 199093527 : if (pushed_scope)
15737 9331446 : pop_scope (pushed_scope);
15738 :
15739 199093527 : if (inner_declarator
15740 198701475 : && inner_declarator->kind == cdk_id
15741 196776153 : && inner_declarator->u.id.sfk == sfk_destructor
15742 3389677 : && arg_types != void_list_node)
15743 : {
15744 21 : error_at (declarator->id_loc,
15745 : "destructors may not have parameters");
15746 21 : arg_types = void_list_node;
15747 21 : parms = NULL_TREE;
15748 21 : is_xobj_member_function = false;
15749 : }
15750 :
15751 199093527 : type = cp_build_function_type (type, arg_types);
15752 :
15753 199093527 : tree attrs = declarator->std_attributes;
15754 199093527 : if (tx_qual)
15755 : {
15756 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
15757 : /* transaction_safe applies to the type, but
15758 : transaction_safe_dynamic applies to the function. */
15759 242 : if (is_attribute_p ("transaction_safe", tx_qual))
15760 213 : attrs = attr_chainon (attrs, att);
15761 : else
15762 29 : returned_attrs = attr_chainon (returned_attrs, att);
15763 : }
15764 :
15765 : /* Actually apply the contract specifiers to the declaration. */
15766 199093527 : if (flag_contracts)
15767 22792644 : contract_specifiers
15768 : = contract_specifiers_concat
15769 22792644 : (contract_specifiers,
15770 22792644 : declarator->u.function.contract_specifiers);
15771 :
15772 199093527 : if (attrs)
15773 : /* [dcl.fct]/2:
15774 :
15775 : The optional attribute-specifier-seq appertains to
15776 : the function type. */
15777 513 : cplus_decl_attributes (&type, attrs, 0);
15778 :
15779 199093527 : if (raises)
15780 79313708 : type = build_exception_variant (type, raises);
15781 : }
15782 199093527 : break;
15783 :
15784 202869977 : case cdk_pointer:
15785 202869977 : case cdk_reference:
15786 202869977 : case cdk_ptrmem:
15787 : /* Filter out pointers-to-references and references-to-references.
15788 : We can get these if a TYPE_DECL is used. */
15789 :
15790 202869977 : if (TYPE_REF_P (type))
15791 : {
15792 240 : if (declarator->kind != cdk_reference)
15793 : {
15794 3 : error ("cannot declare pointer to %q#T", type);
15795 3 : type = TREE_TYPE (type);
15796 : }
15797 :
15798 : /* In C++0x, we allow reference to reference declarations
15799 : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15800 : and template type arguments [14.3.1/4 temp.arg.type]. The
15801 : check for direct reference to reference declarations, which
15802 : are still forbidden, occurs below. Reasoning behind the change
15803 : can be found in DR106, DR540, and the rvalue reference
15804 : proposals. */
15805 237 : else if (cxx_dialect == cxx98)
15806 : {
15807 0 : error ("cannot declare reference to %q#T", type);
15808 0 : type = TREE_TYPE (type);
15809 : }
15810 : }
15811 202869737 : else if (VOID_TYPE_P (type))
15812 : {
15813 5426056 : if (declarator->kind == cdk_reference)
15814 4 : error ("cannot declare reference to %q#T", type);
15815 5426052 : else if (declarator->kind == cdk_ptrmem)
15816 3 : error ("cannot declare pointer to %q#T member", type);
15817 : }
15818 :
15819 : /* We now know that the TYPE_QUALS don't apply to the decl,
15820 : but to the target of the pointer. */
15821 202869977 : type_quals = TYPE_UNQUALIFIED;
15822 :
15823 : /* This code used to handle METHOD_TYPE, but I don't think it's
15824 : possible to get it here anymore. */
15825 202869977 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15826 202869977 : if (declarator->kind == cdk_ptrmem
15827 1159503 : && TREE_CODE (type) == FUNCTION_TYPE)
15828 : {
15829 938169 : memfn_quals |= type_memfn_quals (type);
15830 938169 : if (rqual == REF_QUAL_NONE)
15831 598139 : rqual = type_memfn_rqual (type);
15832 1876338 : type = build_memfn_type (type,
15833 938169 : declarator->u.pointer.class_type,
15834 : memfn_quals,
15835 : rqual);
15836 938169 : if (type == error_mark_node)
15837 : return error_mark_node;
15838 :
15839 : rqual = REF_QUAL_NONE;
15840 : memfn_quals = TYPE_UNQUALIFIED;
15841 : }
15842 :
15843 202869974 : if (TREE_CODE (type) == FUNCTION_TYPE
15844 202869974 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15845 1063732 : || type_memfn_rqual (type) != REF_QUAL_NONE))
15846 15 : error (declarator->kind == cdk_reference
15847 : ? G_("cannot declare reference to qualified function type %qT")
15848 : : G_("cannot declare pointer to qualified function type %qT"),
15849 : type);
15850 :
15851 : /* When the pointed-to type involves components of variable size,
15852 : care must be taken to ensure that the size evaluation code is
15853 : emitted early enough to dominate all the possible later uses
15854 : and late enough for the variables on which it depends to have
15855 : been assigned.
15856 :
15857 : This is expected to happen automatically when the pointed-to
15858 : type has a name/declaration of it's own, but special attention
15859 : is required if the type is anonymous.
15860 :
15861 : We handle the NORMAL and FIELD contexts here by inserting a
15862 : dummy statement that just evaluates the size at a safe point
15863 : and ensures it is not deferred until e.g. within a deeper
15864 : conditional context (c++/43555).
15865 :
15866 : We expect nothing to be needed here for PARM or TYPENAME.
15867 : Evaluating the size at this point for TYPENAME would
15868 : actually be incorrect, as we might be in the middle of an
15869 : expression with side effects on the pointed-to type size
15870 : "arguments" prior to the pointer declaration point and the
15871 : size evaluation could end up prior to the side effects. */
15872 :
15873 202869974 : if (!TYPE_NAME (type)
15874 5740987 : && (decl_context == NORMAL || decl_context == FIELD)
15875 642942 : && at_function_scope_p ()
15876 202911704 : && variably_modified_type_p (type, NULL_TREE))
15877 : {
15878 119 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15879 : NULL_TREE, type);
15880 119 : add_decl_expr (TYPE_NAME (type));
15881 : }
15882 :
15883 202869974 : if (declarator->kind == cdk_reference)
15884 : {
15885 : /* In C++0x, the type we are creating a reference to might be
15886 : a typedef which is itself a reference type. In that case,
15887 : we follow the reference collapsing rules in
15888 : [7.1.3/8 dcl.typedef] to create the final reference type:
15889 :
15890 : "If a typedef TD names a type that is a reference to a type
15891 : T, an attempt to create the type 'lvalue reference to cv TD'
15892 : creates the type 'lvalue reference to T,' while an attempt
15893 : to create the type "rvalue reference to cv TD' creates the
15894 : type TD."
15895 : */
15896 135716946 : if (VOID_TYPE_P (type))
15897 : /* We already gave an error. */;
15898 135716942 : else if (TYPE_REF_P (type))
15899 : {
15900 237 : if (declarator->u.reference.rvalue_ref)
15901 : /* Leave type alone. */;
15902 : else
15903 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15904 : }
15905 : else
15906 135716705 : type = cp_build_reference_type
15907 135716705 : (type, declarator->u.reference.rvalue_ref);
15908 :
15909 : /* In C++0x, we need this check for direct reference to
15910 : reference declarations, which are forbidden by
15911 : [8.3.2/5 dcl.ref]. Reference to reference declarations
15912 : are only allowed indirectly through typedefs and template
15913 : type arguments. Example:
15914 :
15915 : void foo(int & &); // invalid ref-to-ref decl
15916 :
15917 : typedef int & int_ref;
15918 : void foo(int_ref &); // valid ref-to-ref decl
15919 : */
15920 135716946 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15921 2 : error ("cannot declare reference to %q#T, which is not "
15922 : "a typedef or a template type argument", type);
15923 : }
15924 67153028 : else if (TREE_CODE (type) == METHOD_TYPE)
15925 938166 : type = build_ptrmemfunc_type (build_pointer_type (type));
15926 66214862 : else if (declarator->kind == cdk_ptrmem)
15927 : {
15928 221334 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15929 : != NAMESPACE_DECL);
15930 221334 : if (declarator->u.pointer.class_type == error_mark_node)
15931 : /* We will already have complained. */
15932 3 : type = error_mark_node;
15933 : else
15934 221331 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15935 : type);
15936 : }
15937 : else
15938 65993528 : type = build_pointer_type (type);
15939 :
15940 : /* Process a list of type modifier keywords (such as
15941 : const or volatile) that were given inside the `*' or `&'. */
15942 :
15943 202869974 : if (declarator->u.pointer.qualifiers)
15944 : {
15945 8944945 : type
15946 8944945 : = cp_build_qualified_type (type,
15947 : declarator->u.pointer.qualifiers);
15948 8944945 : type_quals = cp_type_quals (type);
15949 : }
15950 :
15951 : /* Apply C++11 attributes to the pointer, and not to the
15952 : type pointed to. This is unlike what is done for GNU
15953 : attributes above. It is to comply with [dcl.ptr]/1:
15954 :
15955 : [the optional attribute-specifier-seq (7.6.1) appertains
15956 : to the pointer and not to the object pointed to]. */
15957 202869974 : if (declarator->std_attributes)
15958 189 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15959 :
15960 : ctype = NULL_TREE;
15961 : break;
15962 :
15963 : case cdk_error:
15964 : break;
15965 :
15966 0 : default:
15967 0 : gcc_unreachable ();
15968 : }
15969 : }
15970 :
15971 1087568799 : id_loc = declarator ? declarator->id_loc : input_location;
15972 :
15973 1087568799 : if (innermost_code != cdk_function
15974 : /* Don't check this if it can be the artificial decltype(auto)
15975 : we created when building a constraint in a compound-requirement:
15976 : that the type-constraint is plain is going to be checked in
15977 : cp_parser_compound_requirement. */
15978 1087568799 : && decl_context != TYPENAME
15979 1087568799 : && check_decltype_auto (id_loc, type))
15980 20 : return error_mark_node;
15981 :
15982 : /* A `constexpr' specifier used in an object declaration declares
15983 : the object as `const'. */
15984 1087568779 : if (constexpr_p && innermost_code != cdk_function)
15985 : {
15986 : /* DR1688 says that a `constexpr' specifier in combination with
15987 : `volatile' is valid. */
15988 :
15989 20833348 : if (!TYPE_REF_P (type))
15990 : {
15991 20821483 : type_quals |= TYPE_QUAL_CONST;
15992 20821483 : type = cp_build_qualified_type (type, type_quals);
15993 : }
15994 : }
15995 :
15996 612143171 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15997 2987226 : && !FUNC_OR_METHOD_TYPE_P (type)
15998 1089567597 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15999 : {
16000 0 : error ("template-id %qD used as a declarator",
16001 : unqualified_id);
16002 0 : unqualified_id = dname;
16003 : }
16004 :
16005 : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
16006 : qualified with a class-name, turn it into a METHOD_TYPE, unless
16007 : we know that the function is static. We take advantage of this
16008 : opportunity to do other processing that pertains to entities
16009 : explicitly declared to be class members. Note that if DECLARATOR
16010 : is non-NULL, we know it is a cdk_id declarator; otherwise, we
16011 : would not have exited the loop above. */
16012 1087568779 : if (declarator
16013 612390561 : && declarator->kind == cdk_id
16014 612143171 : && declarator->u.id.qualifying_scope
16015 1097703778 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
16016 : {
16017 9853555 : ctype = declarator->u.id.qualifying_scope;
16018 9853555 : ctype = TYPE_MAIN_VARIANT (ctype);
16019 9853555 : template_count = num_template_headers_for_class (ctype);
16020 :
16021 9853555 : if (ctype == current_class_type)
16022 : {
16023 54 : if (friendp)
16024 : {
16025 15 : permerror (declspecs->locations[ds_friend],
16026 : "member functions are implicitly "
16027 : "friends of their class");
16028 15 : friendp = 0;
16029 : }
16030 : else
16031 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
16032 : ctype, name);
16033 : }
16034 9853501 : else if (/* If the qualifying type is already complete, then we
16035 : can skip the following checks. */
16036 9853501 : !COMPLETE_TYPE_P (ctype)
16037 154 : && (/* If the function is being defined, then
16038 : qualifying type must certainly be complete. */
16039 : funcdef_flag
16040 : /* A friend declaration of "T::f" is OK, even if
16041 : "T" is a template parameter. But, if this
16042 : function is not a friend, the qualifying type
16043 : must be a class. */
16044 117 : || (!friendp && !CLASS_TYPE_P (ctype))
16045 : /* For a declaration, the type need not be
16046 : complete, if either it is dependent (since there
16047 : is no meaningful definition of complete in that
16048 : case) or the qualifying class is currently being
16049 : defined. */
16050 129 : || !(dependent_type_p (ctype)
16051 24 : || currently_open_class (ctype)))
16052 : /* Check that the qualifying type is complete. */
16053 9853565 : && !complete_type_or_else (ctype, NULL_TREE))
16054 64 : return error_mark_node;
16055 9853437 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16056 : {
16057 9331708 : if (current_class_type
16058 289 : && (!friendp || funcdef_flag || initialized))
16059 : {
16060 57 : error_at (id_loc, funcdef_flag || initialized
16061 : ? G_("cannot define member function %<%T::%s%> "
16062 : "within %qT")
16063 : : G_("cannot declare member function %<%T::%s%> "
16064 : "within %qT"),
16065 : ctype, name, current_class_type);
16066 48 : return error_mark_node;
16067 : }
16068 : }
16069 521729 : else if (typedef_p && current_class_type)
16070 : {
16071 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
16072 : ctype, name, current_class_type);
16073 0 : return error_mark_node;
16074 : }
16075 : }
16076 :
16077 1087568667 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
16078 61045764 : ctype = current_class_type;
16079 :
16080 : /* Now TYPE has the actual type. */
16081 :
16082 1087568667 : if (returned_attrs)
16083 : {
16084 2940 : if (attrlist)
16085 2940 : *attrlist = attr_chainon (returned_attrs, *attrlist);
16086 : else
16087 : attrlist = &returned_attrs;
16088 : }
16089 :
16090 1087568667 : if (declarator
16091 612390449 : && declarator->kind == cdk_id
16092 612143059 : && declarator->std_attributes
16093 601460 : && attrlist != NULL)
16094 : {
16095 : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
16096 : a declarator-id appertains to the entity that is declared. */
16097 601457 : if (declarator->std_attributes != error_mark_node)
16098 : {
16099 601439 : if (flag_reflection
16100 : && declarator->std_attributes != error_mark_node
16101 28568 : && lookup_annotation (declarator->std_attributes)
16102 40 : && *attrlist != error_mark_node
16103 601479 : && lookup_annotation (*attrlist))
16104 : /* If there are annotations in both lists, ensure
16105 : declarator->std_attributes go after *attrlist. See
16106 : PR124399. */
16107 14 : *attrlist = chainon (copy_list (*attrlist),
16108 14 : declarator->std_attributes);
16109 : else
16110 601425 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
16111 : }
16112 : else
16113 : /* We should have already diagnosed the issue (c++/78344). */
16114 18 : gcc_assert (seen_error ());
16115 : }
16116 :
16117 : /* Handle parameter packs. */
16118 1087568667 : if (parameter_pack_p)
16119 : {
16120 3224040 : if (decl_context == PARM)
16121 : /* Turn the type into a pack expansion.*/
16122 3224040 : type = make_pack_expansion (type);
16123 : else
16124 0 : error ("non-parameter %qs cannot be a parameter pack", name);
16125 : }
16126 :
16127 1087568667 : if ((decl_context == FIELD || decl_context == PARM)
16128 483126583 : && !processing_template_decl
16129 1241858429 : && variably_modified_type_p (type, NULL_TREE))
16130 : {
16131 12 : if (decl_context == FIELD)
16132 6 : error_at (id_loc,
16133 : "data member may not have variably modified type %qT", type);
16134 : else
16135 6 : error_at (id_loc,
16136 : "parameter may not have variably modified type %qT", type);
16137 12 : type = error_mark_node;
16138 : }
16139 :
16140 1087568667 : if (explicitp == 1 || (explicitp && friendp))
16141 : {
16142 : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
16143 : in the declaration of a constructor or conversion function within
16144 : a class definition. */
16145 74 : if (!current_class_type)
16146 59 : error_at (declspecs->locations[ds_explicit],
16147 : "%<explicit%> outside class declaration");
16148 15 : else if (friendp)
16149 12 : error_at (declspecs->locations[ds_explicit],
16150 : "%<explicit%> in friend declaration");
16151 : else
16152 3 : error_at (declspecs->locations[ds_explicit],
16153 : "only declarations of constructors and conversion operators "
16154 : "can be %<explicit%>");
16155 : explicitp = 0;
16156 : }
16157 :
16158 1087568667 : if (storage_class == sc_mutable)
16159 : {
16160 142529 : location_t sloc = declspecs->locations[ds_storage_class];
16161 142529 : if (decl_context != FIELD || friendp)
16162 : {
16163 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
16164 : name);
16165 30 : storage_class = sc_none;
16166 : }
16167 142499 : else if (decl_context == TYPENAME || typedef_p)
16168 : {
16169 0 : error_at (sloc,
16170 : "non-object member %qs cannot be declared %<mutable%>",
16171 : name);
16172 0 : storage_class = sc_none;
16173 : }
16174 142499 : else if (FUNC_OR_METHOD_TYPE_P (type))
16175 : {
16176 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
16177 : name);
16178 3 : storage_class = sc_none;
16179 : }
16180 142496 : else if (staticp)
16181 : {
16182 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
16183 : name);
16184 0 : storage_class = sc_none;
16185 : }
16186 142496 : else if (type_quals & TYPE_QUAL_CONST)
16187 : {
16188 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
16189 : name);
16190 9 : storage_class = sc_none;
16191 : }
16192 142487 : else if (TYPE_REF_P (type))
16193 : {
16194 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
16195 : name);
16196 6 : storage_class = sc_none;
16197 : }
16198 : }
16199 :
16200 : /* If this is declaring a typedef name, return a TYPE_DECL. */
16201 1087568667 : if (typedef_p && decl_context != TYPENAME)
16202 : {
16203 49397401 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
16204 49397401 : tree decl;
16205 :
16206 49397401 : if (funcdef_flag)
16207 : {
16208 6 : if (decl_context == NORMAL)
16209 3 : error_at (id_loc,
16210 : "typedef may not be a function definition");
16211 : else
16212 3 : error_at (id_loc,
16213 : "typedef may not be a member function definition");
16214 6 : return error_mark_node;
16215 : }
16216 :
16217 : /* This declaration:
16218 :
16219 : typedef void f(int) const;
16220 :
16221 : declares a function type which is not a member of any
16222 : particular class, but which is cv-qualified; for
16223 : example "f S::*" declares a pointer to a const-qualified
16224 : member function of S. We record the cv-qualification in the
16225 : function type. */
16226 49397395 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
16227 : {
16228 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
16229 :
16230 : /* We have now dealt with these qualifiers. */
16231 62 : memfn_quals = TYPE_UNQUALIFIED;
16232 62 : rqual = REF_QUAL_NONE;
16233 : }
16234 :
16235 49397395 : if (type_uses_auto (type))
16236 : {
16237 62 : if (alias_p)
16238 20 : error_at (declspecs->locations[ds_type_spec],
16239 : "%<auto%> not allowed in alias declaration");
16240 : else
16241 42 : error_at (declspecs->locations[ds_type_spec],
16242 : "typedef declared %<auto%>");
16243 62 : type = error_mark_node;
16244 : }
16245 :
16246 49397395 : if (reqs)
16247 3 : error_at (location_of (reqs), "requires-clause on typedef");
16248 :
16249 49397395 : if (id_declarator && declarator->u.id.qualifying_scope)
16250 : {
16251 21 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
16252 21 : type = error_mark_node;
16253 : }
16254 :
16255 49397395 : if (decl_context == FIELD)
16256 26511407 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
16257 : else
16258 22885988 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
16259 :
16260 49397395 : if (decl_context != FIELD)
16261 : {
16262 22885988 : if (!current_function_decl)
16263 10108559 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
16264 12777429 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
16265 : /* The TYPE_DECL is "abstract" because there will be
16266 : clones of this constructor/destructor, and there will
16267 : be copies of this TYPE_DECL generated in those
16268 : clones. The decloning optimization (for space) may
16269 : revert this subsequently if it determines that
16270 : the clones should share a common implementation. */
16271 163520 : DECL_ABSTRACT_P (decl) = true;
16272 :
16273 22885988 : set_originating_module (decl);
16274 : }
16275 26511407 : else if (current_class_type
16276 26511407 : && constructor_name_p (unqualified_id, current_class_type))
16277 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
16278 : "as enclosing class",
16279 : unqualified_id);
16280 :
16281 : /* If the user declares "typedef struct {...} foo" then the
16282 : struct will have an anonymous name. Fill that name in now.
16283 : Nothing can refer to it, so nothing needs know about the name
16284 : change. */
16285 49397395 : if (type != error_mark_node
16286 49397312 : && unqualified_id
16287 49397309 : && TYPE_NAME (type)
16288 45915824 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16289 91692512 : && (TYPE_UNNAMED_P (type)
16290 : /* An enum may have previously used an enumerator for linkage
16291 : purposes, but we want the typedef name to take priority. */
16292 45503774 : || enum_with_enumerator_for_linkage_p (type))
16293 412472 : && declspecs->type_definition_p
16294 412389 : && attributes_naming_typedef_ok (*attrlist)
16295 49809781 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
16296 412368 : name_unnamed_type (type, decl);
16297 :
16298 49397395 : if (signed_p
16299 49397395 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
16300 393320 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
16301 :
16302 49397395 : bad_specifiers (decl, BSP_TYPE, virtualp,
16303 : memfn_quals != TYPE_UNQUALIFIED,
16304 : inlinep, friendp, raises != NULL_TREE,
16305 : declspecs->locations);
16306 :
16307 49397395 : if (alias_p)
16308 : /* Acknowledge that this was written:
16309 : `using analias = atype;'. */
16310 22365299 : TYPE_DECL_ALIAS_P (decl) = 1;
16311 :
16312 : /* We use a flag so that dependent_opaque_alias_p doesn't have to
16313 : recompute the answer every single time. */
16314 49397395 : TYPE_DECL_OPAQUE_ALIAS_P (decl) = any_lambdas_p (type);
16315 :
16316 49397395 : return decl;
16317 : }
16318 :
16319 : /* Detect the case of an array type of unspecified size
16320 : which came, as such, direct from a typedef name.
16321 : We must copy the type, so that the array's domain can be
16322 : individually set by the object's initializer. */
16323 :
16324 1038171266 : if (type && typedef_type
16325 1007251949 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
16326 1038787477 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
16327 83 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16328 :
16329 : /* Detect where we're using a typedef of function type to declare a
16330 : function. PARMS will not be set, so we must create it now. */
16331 :
16332 1038171266 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
16333 : {
16334 16515 : tree decls = NULL_TREE;
16335 16515 : tree args;
16336 :
16337 16515 : for (args = TYPE_ARG_TYPES (type);
16338 35495 : args && args != void_list_node;
16339 18980 : args = TREE_CHAIN (args))
16340 : {
16341 18980 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
16342 18980 : TREE_VALUE (args));
16343 :
16344 18980 : DECL_CHAIN (decl) = decls;
16345 18980 : decls = decl;
16346 : }
16347 :
16348 16515 : parms = nreverse (decls);
16349 :
16350 16515 : if (decl_context != TYPENAME)
16351 : {
16352 : /* The qualifiers on the function type become the qualifiers on
16353 : the non-static member function. */
16354 15742 : memfn_quals |= type_memfn_quals (type);
16355 15742 : rqual = type_memfn_rqual (type);
16356 15742 : type_quals = TYPE_UNQUALIFIED;
16357 15742 : raises = TYPE_RAISES_EXCEPTIONS (type);
16358 : }
16359 : }
16360 :
16361 : /* If this is a type name (such as, in a cast or sizeof),
16362 : compute the type and return it now. */
16363 :
16364 1038170493 : if (decl_context == TYPENAME)
16365 : {
16366 : /* Note that here we don't care about type_quals. */
16367 :
16368 : /* Special case: "friend class foo" looks like a TYPENAME context. */
16369 427986055 : if (friendp)
16370 : {
16371 0 : if (inlinep)
16372 : {
16373 0 : error ("%<inline%> specified for friend class declaration");
16374 0 : inlinep = 0;
16375 : }
16376 :
16377 0 : if (!current_aggr)
16378 : {
16379 : /* Don't allow friend declaration without a class-key. */
16380 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16381 0 : permerror (input_location, "template parameters cannot be friends");
16382 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
16383 0 : permerror (input_location, "friend declaration requires class-key, "
16384 : "i.e. %<friend class %T::%D%>",
16385 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
16386 : else
16387 0 : permerror (input_location, "friend declaration requires class-key, "
16388 : "i.e. %<friend %#T%>",
16389 : type);
16390 : }
16391 :
16392 : /* Only try to do this stuff if we didn't already give up. */
16393 0 : if (type != integer_type_node)
16394 : {
16395 : /* A friendly class? */
16396 0 : if (current_class_type)
16397 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
16398 : /*complain=*/true);
16399 : else
16400 0 : error ("trying to make class %qT a friend of global scope",
16401 : type);
16402 :
16403 0 : type = void_type_node;
16404 : }
16405 : }
16406 427986055 : else if (memfn_quals || rqual)
16407 : {
16408 13882 : if (ctype == NULL_TREE
16409 13882 : && TREE_CODE (type) == METHOD_TYPE)
16410 0 : ctype = TYPE_METHOD_BASETYPE (type);
16411 :
16412 13882 : if (ctype)
16413 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
16414 : /* Core issue #547: need to allow this in template type args.
16415 : Allow it in general in C++11 for alias-declarations. */
16416 13882 : else if ((template_type_arg || cxx_dialect >= cxx11)
16417 13880 : && TREE_CODE (type) == FUNCTION_TYPE)
16418 13878 : type = apply_memfn_quals (type, memfn_quals, rqual);
16419 : else
16420 4 : error ("invalid qualifiers on non-member function type");
16421 : }
16422 :
16423 427986055 : if (reqs)
16424 3 : error_at (location_of (reqs), "requires-clause on type-id");
16425 :
16426 427986055 : return type;
16427 : }
16428 610185211 : else if (unqualified_id == NULL_TREE && decl_context != PARM
16429 872223 : && decl_context != CATCHPARM
16430 387176 : && TREE_CODE (type) != UNION_TYPE
16431 386793 : && ! bitfield
16432 386793 : && innermost_code != cdk_decomp)
16433 : {
16434 9 : error ("abstract declarator %qT used as declaration", type);
16435 9 : return error_mark_node;
16436 : }
16437 :
16438 610185202 : if (!FUNC_OR_METHOD_TYPE_P (type))
16439 : {
16440 : /* Only functions may be declared using an operator-function-id. */
16441 413450580 : if (dname && IDENTIFIER_ANY_OP_P (dname))
16442 : {
16443 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
16444 8 : return error_mark_node;
16445 : }
16446 :
16447 413450572 : if (reqs)
16448 15 : error_at (location_of (reqs),
16449 : "requires-clause on declaration of non-function type %qT",
16450 : type);
16451 : }
16452 :
16453 : /* We don't check parameter types here because we can emit a better
16454 : error message later. */
16455 610185194 : if (decl_context != PARM)
16456 : {
16457 302765429 : type = check_var_type (unqualified_id, type, id_loc);
16458 302765429 : if (type == error_mark_node)
16459 : return error_mark_node;
16460 : }
16461 :
16462 : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16463 : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16464 :
16465 610185110 : if (decl_context == PARM || decl_context == CATCHPARM)
16466 : {
16467 307919498 : if (ctype || in_namespace)
16468 0 : error ("cannot use %<::%> in parameter declaration");
16469 :
16470 307919498 : tree auto_node = type_uses_auto (type);
16471 307919498 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16472 : {
16473 126 : bool err_p = true;
16474 126 : if (cxx_dialect >= cxx14)
16475 : {
16476 224 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16477 3 : error_at (typespec_loc,
16478 : "cannot declare a parameter with %<decltype(auto)%>");
16479 115 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16480 : {
16481 7 : auto_diagnostic_group g;
16482 7 : gcc_rich_location richloc (typespec_loc);
16483 7 : richloc.add_fixit_insert_after ("<>");
16484 7 : error_at (&richloc,
16485 : "missing template argument list after %qE; template "
16486 : "placeholder not permitted in parameter", c);
16487 7 : if (decl_context == PARM && cxx_dialect >= cxx20)
16488 6 : inform (typespec_loc, "or use %<auto%> for an "
16489 : "abbreviated function template");
16490 7 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16491 7 : }
16492 108 : else if (decl_context == CATCHPARM || template_parm_flag)
16493 14 : error_at (typespec_loc,
16494 : "%<auto%> parameter not permitted in this context");
16495 : else
16496 : /* Do not issue an error while tentatively parsing a function
16497 : parameter: for T t(auto(a), 42);, when we just saw the 1st
16498 : parameter, we don't know yet that this construct won't be
16499 : a function declaration. Defer the checking to
16500 : cp_parser_parameter_declaration_clause. */
16501 : err_p = false;
16502 : }
16503 : else
16504 8 : error_at (typespec_loc, "parameter declared %<auto%>");
16505 32 : if (err_p)
16506 32 : type = error_mark_node;
16507 : }
16508 :
16509 : /* A parameter declared as an array of T is really a pointer to T.
16510 : One declared as a function is really a pointer to a function.
16511 : One declared as a member is really a pointer to member. */
16512 :
16513 307919498 : if (TREE_CODE (type) == ARRAY_TYPE)
16514 : {
16515 : /* Transfer const-ness of array into that of type pointed to. */
16516 801667 : type = build_pointer_type (TREE_TYPE (type));
16517 801667 : type_quals = TYPE_UNQUALIFIED;
16518 801667 : array_parameter_p = true;
16519 : }
16520 307117831 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16521 103737 : type = build_pointer_type (type);
16522 : }
16523 :
16524 152270357 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16525 : /* Don't convert xobj member functions to METHOD_TYPE. */
16526 117214147 : && !is_xobj_member_function
16527 716348018 : && !(unqualified_id
16528 212316907 : && identifier_p (unqualified_id)
16529 106162508 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16530 : {
16531 106154399 : cp_cv_quals real_quals = memfn_quals;
16532 106154399 : if (cxx_dialect < cxx14 && constexpr_p
16533 726899 : && sfk != sfk_constructor && sfk != sfk_destructor)
16534 3719 : real_quals |= TYPE_QUAL_CONST;
16535 106154399 : type = build_memfn_type (type, ctype, real_quals, rqual);
16536 : }
16537 :
16538 610185110 : {
16539 610185110 : tree decl = NULL_TREE;
16540 :
16541 610185110 : if (decl_context == PARM)
16542 : {
16543 307419765 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16544 307419765 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16545 :
16546 307419765 : bad_specifiers (decl, BSP_PARM, virtualp,
16547 : memfn_quals != TYPE_UNQUALIFIED,
16548 : inlinep, friendp, raises != NULL_TREE,
16549 : declspecs->locations);
16550 : }
16551 302765345 : else if (decl_context == FIELD)
16552 : {
16553 149195396 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16554 20756650 : if (tree auto_node = type_uses_auto (type))
16555 : {
16556 24 : location_t tloc = declspecs->locations[ds_type_spec];
16557 24 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16558 6 : error_at (tloc, "invalid use of template-name %qE without an "
16559 : "argument list",
16560 6 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16561 : else
16562 18 : error_at (tloc, "non-static data member declared with "
16563 : "placeholder %qT", auto_node);
16564 24 : type = error_mark_node;
16565 : }
16566 :
16567 : /* The C99 flexible array extension. */
16568 124460619 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16569 150340266 : && TYPE_DOMAIN (type) == NULL_TREE)
16570 : {
16571 1401 : if (ctype
16572 1401 : && (TREE_CODE (ctype) == UNION_TYPE
16573 1401 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16574 84 : pedwarn (id_loc, OPT_Wpedantic,
16575 : "flexible array member in union is a GCC extension");
16576 :
16577 : else
16578 : {
16579 : /* Array is a flexible member. */
16580 1317 : if (name)
16581 1317 : pedwarn (id_loc, OPT_Wpedantic,
16582 : "ISO C++ forbids flexible array member %qs", name);
16583 : else
16584 : pedwarn (input_location, OPT_Wpedantic,
16585 : "ISO C++ forbids flexible array members");
16586 :
16587 : /* Flexible array member has a null domain. */
16588 1317 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16589 : }
16590 : }
16591 :
16592 149195396 : if (type == error_mark_node)
16593 : {
16594 : /* Happens when declaring arrays of sizes which
16595 : are error_mark_node, for example. */
16596 24 : decl = NULL_TREE;
16597 : }
16598 149195372 : else if (in_namespace && !friendp)
16599 : {
16600 : /* Something like struct S { int N::j; }; */
16601 6 : error_at (id_loc, "invalid use of %<::%>");
16602 6 : return error_mark_node;
16603 : }
16604 149195366 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16605 : {
16606 114660873 : int publicp = 0;
16607 114660873 : tree function_context;
16608 :
16609 114660873 : if (friendp == 0)
16610 : {
16611 : /* This should never happen in pure C++ (the check
16612 : could be an assert). It could happen in
16613 : Objective-C++ if someone writes invalid code that
16614 : uses a function declaration for an instance
16615 : variable or property (instance variables and
16616 : properties are parsed as FIELD_DECLs, but they are
16617 : part of an Objective-C class, not a C++ class).
16618 : That code is invalid and is caught by this
16619 : check. */
16620 107882487 : if (!ctype)
16621 : {
16622 0 : error ("declaration of function %qD in invalid context",
16623 : unqualified_id);
16624 0 : return error_mark_node;
16625 : }
16626 :
16627 : /* ``A union may [ ... ] not [ have ] virtual functions.''
16628 : ARM 9.5 */
16629 107882487 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16630 : {
16631 3 : error_at (declspecs->locations[ds_virtual],
16632 : "function %qD declared %<virtual%> inside a union",
16633 : unqualified_id);
16634 3 : return error_mark_node;
16635 : }
16636 :
16637 4293503 : if (virtualp
16638 118954373 : && identifier_p (unqualified_id)
16639 4293503 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16640 : {
16641 9 : error_at (declspecs->locations[ds_virtual],
16642 : "%qD cannot be declared %<virtual%>, since it "
16643 : "is always static", unqualified_id);
16644 9 : virtualp = 0;
16645 : }
16646 : }
16647 :
16648 : /* Check that the name used for a destructor makes sense. */
16649 114660870 : if (sfk == sfk_destructor)
16650 : {
16651 3216949 : tree uqname = id_declarator->u.id.unqualified_name;
16652 :
16653 3216949 : if (!ctype)
16654 : {
16655 12 : gcc_assert (friendp);
16656 12 : error_at (id_loc, "expected qualified name in friend "
16657 : "declaration for destructor %qD", uqname);
16658 12 : return error_mark_node;
16659 : }
16660 :
16661 3216937 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16662 : {
16663 9 : error_at (id_loc, "declaration of %qD as member of %qT",
16664 : uqname, ctype);
16665 9 : return error_mark_node;
16666 : }
16667 3216928 : if (concept_p)
16668 : {
16669 9 : error_at (declspecs->locations[ds_concept],
16670 : "a destructor cannot be %qs", "concept");
16671 9 : return error_mark_node;
16672 : }
16673 3216919 : if (constexpr_p && cxx_dialect < cxx20)
16674 : {
16675 6 : error_at (declspecs->locations[ds_constexpr],
16676 : "%<constexpr%> destructors only available"
16677 : " with %<-std=c++20%> or %<-std=gnu++20%>");
16678 6 : return error_mark_node;
16679 : }
16680 3216913 : if (consteval_p)
16681 : {
16682 3 : error_at (declspecs->locations[ds_consteval],
16683 : "a destructor cannot be %qs", "consteval");
16684 3 : return error_mark_node;
16685 : }
16686 : }
16687 111443921 : else if (sfk == sfk_constructor && friendp && !ctype)
16688 : {
16689 0 : error ("expected qualified name in friend declaration "
16690 : "for constructor %qD",
16691 0 : id_declarator->u.id.unqualified_name);
16692 0 : return error_mark_node;
16693 : }
16694 114660831 : if (sfk == sfk_constructor)
16695 24373165 : if (concept_p)
16696 : {
16697 9 : error_at (declspecs->locations[ds_concept],
16698 : "a constructor cannot be %<concept%>");
16699 9 : return error_mark_node;
16700 : }
16701 90287666 : if (concept_p)
16702 : {
16703 9 : error_at (declspecs->locations[ds_concept],
16704 : "a concept cannot be a member function");
16705 9 : concept_p = false;
16706 : }
16707 114660813 : else if (consteval_p
16708 115466621 : && identifier_p (unqualified_id)
16709 115466612 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16710 : {
16711 9 : error_at (declspecs->locations[ds_consteval],
16712 : "%qD cannot be %qs", unqualified_id, "consteval");
16713 9 : consteval_p = false;
16714 : }
16715 :
16716 114660822 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16717 : {
16718 100212 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
16719 100212 : if (variable_template_p (tmpl))
16720 : {
16721 3 : auto_diagnostic_group d;
16722 3 : error_at (id_loc, "specialization of variable template "
16723 : "%qD declared as function", tmpl);
16724 3 : inform (DECL_SOURCE_LOCATION (tmpl),
16725 : "variable template declared here");
16726 3 : return error_mark_node;
16727 3 : }
16728 : }
16729 :
16730 : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16731 114660819 : function_context
16732 : = (ctype != NULL_TREE
16733 114660819 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16734 229321638 : publicp = ((! friendp || ! staticp)
16735 114660819 : && function_context == NULL_TREE);
16736 :
16737 114660819 : decl = grokfndecl (ctype, type,
16738 114660819 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16739 : ? unqualified_id : dname,
16740 : parms,
16741 : unqualified_id,
16742 : declspecs,
16743 : reqs,
16744 : virtualp, flags, memfn_quals, rqual, raises,
16745 : friendp ? -1 : 0, friendp, publicp,
16746 114660819 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16747 114660819 : | (8 * consteval_p),
16748 : initialized,
16749 : is_xobj_member_function, sfk,
16750 : funcdef_flag, late_return_type_p,
16751 : template_count, in_namespace,
16752 : attrlist, contract_specifiers, id_loc);
16753 114660819 : decl = set_virt_specifiers (decl, virt_specifiers);
16754 114660819 : if (decl == NULL_TREE)
16755 97 : return error_mark_node;
16756 : #if 0
16757 : /* This clobbers the attrs stored in `decl' from `attrlist'. */
16758 : /* The decl and setting of decl_attr is also turned off. */
16759 : decl = build_decl_attribute_variant (decl, decl_attr);
16760 : #endif
16761 :
16762 : /* [class.conv.ctor]
16763 :
16764 : A constructor declared without the function-specifier
16765 : explicit that can be called with a single parameter
16766 : specifies a conversion from the type of its first
16767 : parameter to the type of its class. Such a constructor
16768 : is called a converting constructor. */
16769 114660722 : if (explicitp == 2)
16770 6179984 : DECL_NONCONVERTING_P (decl) = 1;
16771 :
16772 114660722 : if (declspecs->explicit_specifier)
16773 454353 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16774 : }
16775 34534493 : else if (!staticp
16776 34534493 : && ((current_class_type
16777 20756641 : && same_type_p (TYPE_MAIN_VARIANT (type),
16778 : current_class_type))
16779 20756545 : || (!dependent_type_p (type)
16780 12533661 : && !COMPLETE_TYPE_P (complete_type (type))
16781 1449 : && (!complete_or_array_type_p (type)
16782 1374 : || initialized == SD_UNINITIALIZED))))
16783 : {
16784 1512 : if (TREE_CODE (type) != ARRAY_TYPE
16785 1512 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16786 : {
16787 171 : if (unqualified_id)
16788 : {
16789 126 : auto_diagnostic_group d;
16790 126 : error_at (id_loc, "field %qD has incomplete type %qT",
16791 : unqualified_id, type);
16792 126 : cxx_incomplete_type_inform (strip_array_types (type));
16793 126 : }
16794 : else
16795 45 : error ("name %qT has incomplete type", type);
16796 :
16797 171 : type = error_mark_node;
16798 171 : decl = NULL_TREE;
16799 : }
16800 : }
16801 55288098 : else if (!verify_type_context (input_location,
16802 : staticp
16803 : ? TCTX_STATIC_STORAGE
16804 : : TCTX_FIELD, type))
16805 : {
16806 0 : type = error_mark_node;
16807 0 : decl = NULL_TREE;
16808 : }
16809 : else
16810 : {
16811 34532975 : if (friendp)
16812 : {
16813 12 : if (unqualified_id)
16814 9 : error_at (id_loc,
16815 : "%qE is neither function nor member function; "
16816 : "cannot be declared friend", unqualified_id);
16817 : else
16818 3 : error ("unnamed field is neither function nor member "
16819 : "function; cannot be declared friend");
16820 12 : return error_mark_node;
16821 : }
16822 34532963 : decl = NULL_TREE;
16823 : }
16824 :
16825 149195221 : if (friendp)
16826 : {
16827 6778355 : if (flag_reflection && !funcdef_flag && decl)
16828 : {
16829 15934 : if (attrlist && lookup_annotation (*attrlist))
16830 : {
16831 : /* Remove the annotations to avoid spurious warning
16832 : below. */
16833 8 : *attrlist = remove_attribute ("internal ", "annotation ",
16834 : *attrlist);
16835 8 : error_at (id_loc, "annotation applied to non-defining "
16836 : "friend declaration %qD", decl);
16837 : }
16838 15934 : for (tree arg = DECL_ARGUMENTS (decl);
16839 36594 : arg; arg = DECL_CHAIN (arg))
16840 20660 : if (lookup_annotation (DECL_ATTRIBUTES (arg)))
16841 8 : error_at (DECL_SOURCE_LOCATION (arg),
16842 : "annotation applied to parameter %qD of "
16843 : "non-defining friend declaration", arg);
16844 : }
16845 : /* Packages tend to use GNU attributes on friends, so we only
16846 : warn for standard attributes. */
16847 6778355 : if (attrlist
16848 6778355 : && !funcdef_flag
16849 6778355 : && cxx11_attribute_p (*attrlist))
16850 : {
16851 27 : *attrlist = NULL_TREE;
16852 27 : auto_diagnostic_group d;
16853 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16854 27 : inform (id_loc, "an attribute that appertains to a friend "
16855 : "declaration that is not a definition is ignored");
16856 27 : }
16857 : /* Friends are treated specially. */
16858 6778355 : if (ctype == current_class_type)
16859 : ; /* We already issued a permerror. */
16860 6778355 : else if (decl && DECL_NAME (decl))
16861 : {
16862 6778352 : set_originating_module (decl, true);
16863 :
16864 6778352 : if (initialized)
16865 : /* Kludge: We need funcdef_flag to be true in do_friend for
16866 : in-class defaulted functions, but that breaks grokfndecl.
16867 : So set it here. */
16868 221188 : funcdef_flag = true;
16869 :
16870 6778352 : cplus_decl_attributes (&decl, *attrlist, 0);
16871 6778352 : *attrlist = NULL_TREE;
16872 :
16873 6778352 : tree scope = ctype ? ctype : in_namespace;
16874 6778352 : decl = do_friend (scope, unqualified_id, decl,
16875 : flags, funcdef_flag);
16876 6778352 : return decl;
16877 : }
16878 : else
16879 3 : return error_mark_node;
16880 : }
16881 :
16882 : /* Structure field. It may not be a function, except for C++. */
16883 :
16884 142416866 : if (decl == NULL_TREE)
16885 : {
16886 34534496 : if (staticp)
16887 : {
16888 : /* C++ allows static class members. All other work
16889 : for this is done by grokfield. */
16890 13777852 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
16891 : dname, type);
16892 13777852 : if (unqualified_id
16893 13777849 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16894 : {
16895 44703 : decl = check_explicit_specialization (unqualified_id, decl,
16896 : template_count,
16897 : concept_p * 8);
16898 44703 : if (decl == error_mark_node)
16899 : return error_mark_node;
16900 : }
16901 13777852 : set_linkage_for_static_data_member (decl);
16902 13777852 : if (concept_p)
16903 15 : error_at (declspecs->locations[ds_concept],
16904 : "static data member %qE declared %qs",
16905 : unqualified_id, "concept");
16906 13777837 : else if (constexpr_p && !initialized)
16907 : {
16908 9 : error_at (DECL_SOURCE_LOCATION (decl),
16909 : "%<constexpr%> static data member %qD must "
16910 : "have an initializer", decl);
16911 9 : constexpr_p = false;
16912 : }
16913 13777852 : if (consteval_p)
16914 3 : error_at (declspecs->locations[ds_consteval],
16915 : "static data member %qE declared %qs",
16916 : unqualified_id, "consteval");
16917 :
16918 13777852 : if (inlinep)
16919 24326 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16920 :
16921 13777852 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16922 13777852 : && !(cxx_dialect >= cxx17 && constexpr_p))
16923 : /* Even if there is an in-class initialization, DECL
16924 : is considered undefined until an out-of-class
16925 : definition is provided, unless this is an inline
16926 : variable. */
16927 1918759 : DECL_EXTERNAL (decl) = 1;
16928 :
16929 13777852 : if (thread_p)
16930 : {
16931 422 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16932 422 : if (!processing_template_decl)
16933 376 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16934 422 : if (declspecs->gnu_thread_keyword_p)
16935 34 : SET_DECL_GNU_TLS_P (decl);
16936 : }
16937 :
16938 : /* Set the constraints on the declaration. */
16939 13777852 : bool memtmpl = (current_template_depth
16940 13777852 : > template_class_depth (current_class_type));
16941 13777852 : if (memtmpl)
16942 : {
16943 400785 : tree ci = current_template_constraints ();
16944 400785 : set_constraints (decl, ci);
16945 : }
16946 : }
16947 : else
16948 : {
16949 20756644 : if (concept_p)
16950 : {
16951 12 : error_at (declspecs->locations[ds_concept],
16952 : "non-static data member %qE declared %qs",
16953 : unqualified_id, "concept");
16954 12 : concept_p = false;
16955 12 : constexpr_p = false;
16956 : }
16957 20756632 : else if (constexpr_p)
16958 : {
16959 9 : error_at (declspecs->locations[ds_constexpr],
16960 : "non-static data member %qE declared %qs",
16961 : unqualified_id, "constexpr");
16962 9 : constexpr_p = false;
16963 : }
16964 20756644 : if (constinit_p)
16965 : {
16966 6 : error_at (declspecs->locations[ds_constinit],
16967 : "non-static data member %qE declared %qs",
16968 : unqualified_id, "constinit");
16969 6 : constinit_p = false;
16970 : }
16971 20756644 : if (consteval_p)
16972 : {
16973 3 : error_at (declspecs->locations[ds_consteval],
16974 : "non-static data member %qE declared %qs",
16975 : unqualified_id, "consteval");
16976 3 : consteval_p = false;
16977 : }
16978 20756644 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16979 20756644 : DECL_NONADDRESSABLE_P (decl) = bitfield;
16980 20756644 : if (bitfield && !unqualified_id)
16981 139385 : DECL_PADDING_P (decl) = 1;
16982 :
16983 20756644 : if (storage_class == sc_mutable)
16984 : {
16985 142481 : DECL_MUTABLE_P (decl) = 1;
16986 142481 : storage_class = sc_none;
16987 : }
16988 :
16989 20756644 : if (initialized)
16990 : {
16991 : /* An attempt is being made to initialize a non-static
16992 : member. This is new in C++11. */
16993 2367784 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
16994 :
16995 : /* If this has been parsed with static storage class, but
16996 : errors forced staticp to be cleared, ensure NSDMI is
16997 : not present. */
16998 2367784 : if (declspecs->storage_class == sc_static)
16999 3 : DECL_INITIAL (decl) = error_mark_node;
17000 : }
17001 : }
17002 :
17003 55291140 : bad_specifiers (decl, BSP_FIELD, virtualp,
17004 : memfn_quals != TYPE_UNQUALIFIED,
17005 : staticp ? false : inlinep, friendp,
17006 : raises != NULL_TREE,
17007 : declspecs->locations);
17008 : }
17009 : }
17010 153569949 : else if (FUNC_OR_METHOD_TYPE_P (type))
17011 : {
17012 81970006 : tree original_name;
17013 81970006 : int publicp = 0;
17014 :
17015 81970006 : if (!unqualified_id)
17016 0 : return error_mark_node;
17017 :
17018 81970006 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
17019 : original_name = dname;
17020 : else
17021 81081816 : original_name = unqualified_id;
17022 : // FIXME:gcc_assert (original_name == dname);
17023 :
17024 81970006 : if (storage_class == sc_auto)
17025 1 : error_at (declspecs->locations[ds_storage_class],
17026 : "storage class %<auto%> invalid for function %qs", name);
17027 81970005 : else if (storage_class == sc_register)
17028 7 : error_at (declspecs->locations[ds_storage_class],
17029 : "storage class %<register%> invalid for function %qs",
17030 : name);
17031 81969998 : else if (thread_p)
17032 : {
17033 12 : if (declspecs->gnu_thread_keyword_p)
17034 12 : error_at (declspecs->locations[ds_thread],
17035 : "storage class %<__thread%> invalid for function %qs",
17036 : name);
17037 : else
17038 0 : error_at (declspecs->locations[ds_thread],
17039 : "storage class %<thread_local%> invalid for "
17040 : "function %qs", name);
17041 : }
17042 81969986 : else if (constinit_p)
17043 3 : error_at (declspecs->locations[ds_constinit],
17044 : "%<constinit%> specifier invalid for function %qs", name);
17045 :
17046 81970006 : if (virt_specifiers)
17047 6 : error ("virt-specifiers in %qs not allowed outside a class "
17048 : "definition", name);
17049 : /* Function declaration not at top level.
17050 : Storage classes other than `extern' are not allowed
17051 : and `extern' makes no difference. */
17052 81970006 : if (! toplevel_bindings_p ()
17053 62414 : && (storage_class == sc_static
17054 62405 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
17055 81970018 : && pedantic)
17056 : {
17057 9 : if (storage_class == sc_static)
17058 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
17059 : "%<static%> specifier invalid for function %qs "
17060 : "declared out of global scope", name);
17061 : else
17062 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
17063 : "%<inline%> specifier invalid for function %qs "
17064 : "declared out of global scope", name);
17065 : }
17066 :
17067 81970006 : if (ctype == NULL_TREE)
17068 : {
17069 72638593 : if (virtualp)
17070 : {
17071 0 : error ("virtual non-class function %qs", name);
17072 0 : virtualp = 0;
17073 : }
17074 72638593 : else if (sfk == sfk_constructor
17075 72638593 : || sfk == sfk_destructor)
17076 : {
17077 3 : error (funcdef_flag
17078 : ? G_("%qs defined in a non-class scope")
17079 : : G_("%qs declared in a non-class scope"), name);
17080 3 : sfk = sfk_none;
17081 : }
17082 : }
17083 81970006 : if (consteval_p
17084 82331518 : && identifier_p (unqualified_id)
17085 82331518 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
17086 : {
17087 9 : error_at (declspecs->locations[ds_consteval],
17088 : "%qD cannot be %qs", unqualified_id, "consteval");
17089 9 : consteval_p = false;
17090 : }
17091 :
17092 : /* Record whether the function is public. */
17093 81970006 : publicp = (ctype != NULL_TREE
17094 81970006 : || storage_class != sc_static);
17095 :
17096 163940012 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
17097 : declspecs, reqs, virtualp, flags, memfn_quals, rqual, raises,
17098 : 1, friendp,
17099 : publicp,
17100 81970006 : inlinep | (2 * constexpr_p) | (4 * concept_p)
17101 81970006 : | (8 * consteval_p),
17102 : initialized,
17103 : is_xobj_member_function, sfk,
17104 : funcdef_flag,
17105 : late_return_type_p,
17106 : template_count, in_namespace, attrlist,
17107 : contract_specifiers, id_loc);
17108 81970006 : if (decl == NULL_TREE)
17109 619 : return error_mark_node;
17110 :
17111 81969387 : if (explicitp == 2)
17112 11395 : DECL_NONCONVERTING_P (decl) = 1;
17113 81969387 : if (staticp == 1)
17114 : {
17115 117613 : int invalid_static = 0;
17116 :
17117 : /* Don't allow a static member function in a class, and forbid
17118 : declaring main to be static. */
17119 117613 : if (TREE_CODE (type) == METHOD_TYPE)
17120 : {
17121 0 : permerror (input_location, "cannot declare member function %qD to have "
17122 : "static linkage", decl);
17123 0 : invalid_static = 1;
17124 : }
17125 117613 : else if (current_function_decl)
17126 : {
17127 : /* 7.1.1: There can be no static function declarations within a
17128 : block. */
17129 9 : error_at (declspecs->locations[ds_storage_class],
17130 : "cannot declare static function inside another function");
17131 9 : invalid_static = 1;
17132 : }
17133 :
17134 9 : if (invalid_static)
17135 : {
17136 81969387 : staticp = 0;
17137 : storage_class = sc_none;
17138 : }
17139 : }
17140 81969387 : if (declspecs->explicit_specifier)
17141 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
17142 : }
17143 : else
17144 : {
17145 : /* It's a variable. */
17146 :
17147 : /* An uninitialized decl with `extern' is a reference. */
17148 142678178 : decl = grokvardecl (type, dname, unqualified_id,
17149 : declspecs,
17150 : initialized,
17151 : type_quals,
17152 : inlinep,
17153 : concept_p,
17154 : template_count,
17155 : ctype ? ctype : in_namespace,
17156 : id_loc);
17157 71599943 : if (decl == NULL_TREE)
17158 92 : return error_mark_node;
17159 :
17160 71599851 : bad_specifiers (decl, BSP_VAR, virtualp,
17161 : memfn_quals != TYPE_UNQUALIFIED,
17162 : inlinep, friendp, raises != NULL_TREE,
17163 : declspecs->locations);
17164 :
17165 71599851 : if (ctype)
17166 : {
17167 521702 : DECL_CONTEXT (decl) = ctype;
17168 521702 : if (staticp == 1)
17169 : {
17170 6 : permerror (declspecs->locations[ds_storage_class],
17171 : "%<static%> may not be used when defining "
17172 : "(as opposed to declaring) a static data member");
17173 6 : staticp = 0;
17174 6 : storage_class = sc_none;
17175 : }
17176 521702 : if (storage_class == sc_register && TREE_STATIC (decl))
17177 : {
17178 0 : error ("static member %qD declared %<register%>", decl);
17179 0 : storage_class = sc_none;
17180 : }
17181 521702 : if (storage_class == sc_extern && pedantic)
17182 : {
17183 0 : pedwarn (input_location, OPT_Wpedantic,
17184 : "cannot explicitly declare member %q#D to have "
17185 : "extern linkage", decl);
17186 0 : storage_class = sc_none;
17187 : }
17188 : }
17189 71078149 : else if (constexpr_p && DECL_EXTERNAL (decl))
17190 : {
17191 14 : error_at (DECL_SOURCE_LOCATION (decl),
17192 : "declaration of %<constexpr%> variable %qD "
17193 : "is not a definition", decl);
17194 14 : constexpr_p = false;
17195 : }
17196 71599851 : if (consteval_p)
17197 : {
17198 13 : error_at (DECL_SOURCE_LOCATION (decl),
17199 : "a variable cannot be declared %<consteval%>");
17200 13 : consteval_p = false;
17201 : }
17202 :
17203 71599851 : if (inlinep)
17204 6986932 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
17205 71599851 : if (innermost_code == cdk_decomp)
17206 : {
17207 247390 : gcc_assert (declarator && declarator->kind == cdk_decomp);
17208 247390 : DECL_SOURCE_LOCATION (decl) = id_loc;
17209 247390 : DECL_ARTIFICIAL (decl) = 1;
17210 247390 : fit_decomposition_lang_decl (decl, NULL_TREE);
17211 : }
17212 : }
17213 :
17214 603405869 : if (VAR_P (decl) && !initialized)
17215 7705733 : if (tree auto_node = type_uses_auto (type))
17216 104 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
17217 : {
17218 21 : location_t loc = declspecs->locations[ds_type_spec];
17219 21 : error_at (loc, "declaration of %q#D has no initializer", decl);
17220 21 : TREE_TYPE (decl) = error_mark_node;
17221 : }
17222 :
17223 603405869 : if (storage_class == sc_extern && initialized && !funcdef_flag)
17224 : {
17225 276 : if (toplevel_bindings_p ())
17226 : {
17227 : /* It's common practice (and completely valid) to have a const
17228 : be initialized and declared extern. */
17229 252 : if (!(type_quals & TYPE_QUAL_CONST))
17230 19 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
17231 : "%qs initialized and declared %<extern%>", name);
17232 : }
17233 : else
17234 : {
17235 24 : error_at (DECL_SOURCE_LOCATION (decl),
17236 : "%qs has both %<extern%> and initializer", name);
17237 24 : return error_mark_node;
17238 : }
17239 : }
17240 :
17241 : /* Record `register' declaration for warnings on &
17242 : and in case doing stupid register allocation. */
17243 :
17244 603405845 : if (storage_class == sc_register)
17245 : {
17246 2069 : DECL_REGISTER (decl) = 1;
17247 : /* Warn about register storage specifiers on PARM_DECLs. */
17248 2069 : if (TREE_CODE (decl) == PARM_DECL)
17249 : {
17250 35 : if (cxx_dialect >= cxx17)
17251 24 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
17252 : "ISO C++17 does not allow %<register%> storage "
17253 : "class specifier");
17254 : else
17255 11 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
17256 : "%<register%> storage class specifier used");
17257 : }
17258 : }
17259 603403776 : else if (storage_class == sc_extern)
17260 32080514 : DECL_THIS_EXTERN (decl) = 1;
17261 571323262 : else if (storage_class == sc_static)
17262 25069444 : DECL_THIS_STATIC (decl) = 1;
17263 :
17264 603405845 : if (VAR_P (decl))
17265 : {
17266 : /* Set constexpr flag on vars (functions got it in grokfndecl). */
17267 85377679 : if (constexpr_p)
17268 20833274 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
17269 : /* And the constinit flag (which only applies to variables). */
17270 64544405 : else if (constinit_p)
17271 453 : DECL_DECLARED_CONSTINIT_P (decl) = true;
17272 : }
17273 518028166 : else if (TREE_CODE (decl) == FUNCTION_DECL)
17274 : {
17275 : /* If we saw a return type, record its location. */
17276 189851757 : location_t loc = declspecs->locations[ds_type_spec];
17277 189851757 : if (loc == UNKNOWN_LOCATION)
17278 : /* Build DECL_RESULT in start_preparsed_function. */;
17279 155297021 : else if (!DECL_RESULT (decl))
17280 : {
17281 155257097 : tree restype = TREE_TYPE (TREE_TYPE (decl));
17282 155257097 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
17283 155257097 : DECL_ARTIFICIAL (resdecl) = 1;
17284 155257097 : DECL_IGNORED_P (resdecl) = 1;
17285 155257097 : DECL_RESULT (decl) = resdecl;
17286 : }
17287 39924 : else if (funcdef_flag)
17288 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
17289 : }
17290 :
17291 : /* Record constancy and volatility on the DECL itself . There's
17292 : no need to do this when processing a template; we'll do this
17293 : for the instantiated declaration based on the type of DECL. */
17294 603405845 : if (!processing_template_decl
17295 : /* Don't do it for instantiated variable templates either,
17296 : cp_apply_type_quals_to_decl should have been called on it
17297 : already and might have been overridden in cp_finish_decl
17298 : if initializer needs runtime initialization. */
17299 603405845 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
17300 201717004 : cp_apply_type_quals_to_decl (type_quals, decl);
17301 :
17302 603405845 : return decl;
17303 : }
17304 1087569449 : }
17305 :
17306 : /* Subroutine of start_function. Ensure that each of the parameter
17307 : types (as listed in PARMS) is complete, as is required for a
17308 : function definition. */
17309 :
17310 : static void
17311 168707840 : require_complete_types_for_parms (tree parms)
17312 : {
17313 499504988 : for (; parms; parms = DECL_CHAIN (parms))
17314 : {
17315 330797148 : if (dependent_type_p (TREE_TYPE (parms)))
17316 185466790 : continue;
17317 145330358 : if (!VOID_TYPE_P (TREE_TYPE (parms))
17318 145330358 : && complete_type_or_else (TREE_TYPE (parms), parms))
17319 : {
17320 145330277 : relayout_decl (parms);
17321 145330277 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
17322 :
17323 145330277 : abstract_virtuals_error (parms, TREE_TYPE (parms));
17324 145330277 : maybe_warn_parm_abi (TREE_TYPE (parms),
17325 145330277 : DECL_SOURCE_LOCATION (parms));
17326 : }
17327 : else
17328 : /* grokparms or complete_type_or_else will have already issued
17329 : an error. */
17330 81 : TREE_TYPE (parms) = error_mark_node;
17331 : }
17332 168707840 : }
17333 :
17334 : /* Returns nonzero if T is a local variable. */
17335 :
17336 : int
17337 181266983 : local_variable_p (const_tree t)
17338 : {
17339 181266983 : if ((VAR_P (t)
17340 70049752 : && (DECL_LOCAL_DECL_P (t)
17341 70049752 : || !DECL_CONTEXT (t)
17342 70006182 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
17343 187130770 : || (TREE_CODE (t) == PARM_DECL))
17344 161286876 : return 1;
17345 :
17346 : return 0;
17347 : }
17348 :
17349 : /* Like local_variable_p, but suitable for use as a tree-walking
17350 : function. */
17351 :
17352 : static tree
17353 5754613 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
17354 : void * /*data*/)
17355 : {
17356 5754613 : if (unevaluated_p (TREE_CODE (*tp)))
17357 : {
17358 : /* DR 2082 permits local variables in unevaluated contexts
17359 : within a default argument. */
17360 64 : *walk_subtrees = 0;
17361 64 : return NULL_TREE;
17362 : }
17363 :
17364 5754549 : if (local_variable_p (*tp)
17365 5754549 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
17366 : return *tp;
17367 5754549 : else if (TYPE_P (*tp))
17368 30 : *walk_subtrees = 0;
17369 :
17370 : return NULL_TREE;
17371 : }
17372 :
17373 : /* Check that ARG, which is a default-argument expression for a
17374 : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
17375 : something goes wrong. DECL may also be a _TYPE node, rather than a
17376 : DECL, if there is no DECL available. */
17377 :
17378 : tree
17379 2843115 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
17380 : {
17381 2843115 : tree var;
17382 2843115 : tree decl_type;
17383 :
17384 2843115 : if (TREE_CODE (arg) == DEFERRED_PARSE)
17385 : /* We get a DEFERRED_PARSE when looking at an in-class declaration
17386 : with a default argument. Ignore the argument for now; we'll
17387 : deal with it after the class is complete. */
17388 : return arg;
17389 :
17390 2014430 : if (TYPE_P (decl))
17391 : {
17392 : decl_type = decl;
17393 : decl = NULL_TREE;
17394 : }
17395 : else
17396 613466 : decl_type = TREE_TYPE (decl);
17397 :
17398 2014430 : if (arg == error_mark_node
17399 2014289 : || decl == error_mark_node
17400 2014280 : || TREE_TYPE (arg) == error_mark_node
17401 4028710 : || decl_type == error_mark_node)
17402 : /* Something already went wrong. There's no need to check
17403 : further. */
17404 : return error_mark_node;
17405 :
17406 : /* [dcl.fct.default]
17407 :
17408 : A default argument expression is implicitly converted to the
17409 : parameter type. */
17410 2014280 : ++cp_unevaluated_operand;
17411 : /* Avoid digest_init clobbering the initializer. */
17412 2014280 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
17413 2014280 : perform_implicit_conversion_flags (decl_type, carg, complain,
17414 : LOOKUP_IMPLICIT);
17415 2014280 : --cp_unevaluated_operand;
17416 :
17417 : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
17418 : the call sites. */
17419 1601906 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17420 412389 : && null_ptr_cst_p (arg)
17421 : /* Don't lose side-effects as in PR90473. */
17422 2391841 : && !TREE_SIDE_EFFECTS (arg))
17423 377558 : return nullptr_node;
17424 :
17425 : /* [dcl.fct.default]
17426 :
17427 : Local variables shall not be used in default argument
17428 : expressions.
17429 :
17430 : The keyword `this' shall not be used in a default argument of a
17431 : member function. */
17432 1636722 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17433 1636722 : if (var)
17434 : {
17435 0 : if (complain & tf_warning_or_error)
17436 : {
17437 0 : if (DECL_NAME (var) == this_identifier)
17438 0 : permerror (input_location, "default argument %qE uses %qD",
17439 : arg, var);
17440 : else
17441 0 : error ("default argument %qE uses local variable %qD", arg, var);
17442 : }
17443 0 : return error_mark_node;
17444 : }
17445 :
17446 : /* All is well. */
17447 1636722 : return arg;
17448 : }
17449 :
17450 : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17451 :
17452 : static tree
17453 294011078 : type_is_deprecated (tree type)
17454 : {
17455 433539804 : enum tree_code code;
17456 433539804 : if (TREE_DEPRECATED (type))
17457 : return type;
17458 433498171 : if (TYPE_NAME (type))
17459 : {
17460 289556756 : if (TREE_DEPRECATED (TYPE_NAME (type)))
17461 : return type;
17462 : else
17463 : {
17464 289556750 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17465 289556750 : return NULL_TREE;
17466 : }
17467 : }
17468 :
17469 : /* Do warn about using typedefs to a deprecated class. */
17470 143941415 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17471 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17472 :
17473 143941415 : code = TREE_CODE (type);
17474 :
17475 143941415 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17476 143941415 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17477 : || code == METHOD_TYPE || code == ARRAY_TYPE)
17478 139405410 : return type_is_deprecated (TREE_TYPE (type));
17479 :
17480 123316 : if (TYPE_PTRMEMFUNC_P (type))
17481 123316 : return type_is_deprecated
17482 123316 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17483 :
17484 : return NULL_TREE;
17485 : }
17486 :
17487 : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17488 :
17489 : static tree
17490 295268756 : type_is_unavailable (tree type)
17491 : {
17492 435549824 : enum tree_code code;
17493 435549824 : if (TREE_UNAVAILABLE (type))
17494 : return type;
17495 435549818 : if (TYPE_NAME (type))
17496 : {
17497 290856004 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17498 : return type;
17499 : else
17500 : {
17501 290855998 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17502 290855998 : return NULL_TREE;
17503 : }
17504 : }
17505 :
17506 : /* Do warn about using typedefs to a deprecated class. */
17507 144693814 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17508 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17509 :
17510 144693814 : code = TREE_CODE (type);
17511 :
17512 144693814 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17513 144693814 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17514 : || code == METHOD_TYPE || code == ARRAY_TYPE)
17515 140041736 : return type_is_unavailable (TREE_TYPE (type));
17516 :
17517 239332 : if (TYPE_PTRMEMFUNC_P (type))
17518 239332 : return type_is_unavailable
17519 239332 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17520 :
17521 : return NULL_TREE;
17522 : }
17523 :
17524 : /* Decode the list of parameter types for a function type.
17525 : Given the list of things declared inside the parens,
17526 : return a list of types.
17527 :
17528 : If this parameter does not end with an ellipsis, we append
17529 : void_list_node.
17530 :
17531 : *PARMS is set to the chain of PARM_DECLs created. */
17532 :
17533 : tree
17534 202194279 : grokparms (tree parmlist, tree *parms)
17535 : {
17536 202194279 : tree result = NULL_TREE;
17537 202194279 : tree decls = NULL_TREE;
17538 202194279 : tree parm;
17539 202194279 : int any_error = 0;
17540 :
17541 498115229 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17542 : {
17543 495525058 : tree type = NULL_TREE;
17544 495525058 : tree init = TREE_PURPOSE (parm);
17545 495525058 : tree decl = TREE_VALUE (parm);
17546 :
17547 495525058 : if (parm == void_list_node || parm == explicit_void_list_node)
17548 : break;
17549 :
17550 295920966 : if (! decl || TREE_TYPE (decl) == error_mark_node)
17551 : {
17552 198 : any_error = 1;
17553 198 : continue;
17554 : }
17555 :
17556 295920768 : type = TREE_TYPE (decl);
17557 295920768 : if (VOID_TYPE_P (type))
17558 : {
17559 82 : if (same_type_p (type, void_type_node)
17560 73 : && !init
17561 61 : && !DECL_NAME (decl) && !result
17562 110 : && TREE_CHAIN (parm) == void_list_node)
17563 : /* DR 577: A parameter list consisting of a single
17564 : unnamed parameter of non-dependent type 'void'. */
17565 : break;
17566 66 : else if (cv_qualified_p (type))
17567 9 : error_at (DECL_SOURCE_LOCATION (decl),
17568 : "invalid use of cv-qualified type %qT in "
17569 : "parameter declaration", type);
17570 : else
17571 57 : error_at (DECL_SOURCE_LOCATION (decl),
17572 : "invalid use of type %<void%> in parameter "
17573 : "declaration");
17574 : /* It's not a good idea to actually create parameters of
17575 : type `void'; other parts of the compiler assume that a
17576 : void type terminates the parameter list. */
17577 66 : type = error_mark_node;
17578 66 : TREE_TYPE (decl) = error_mark_node;
17579 : }
17580 :
17581 295920752 : if (type != error_mark_node)
17582 : {
17583 295920686 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17584 : {
17585 295268756 : tree unavailtype = type_is_unavailable (type);
17586 295268756 : if (unavailtype)
17587 12 : cp_handle_deprecated_or_unavailable (unavailtype);
17588 : }
17589 295920686 : if (deprecated_state != DEPRECATED_SUPPRESS
17590 295920686 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17591 : {
17592 294011078 : tree deptype = type_is_deprecated (type);
17593 294011078 : if (deptype)
17594 41639 : cp_handle_deprecated_or_unavailable (deptype);
17595 : }
17596 :
17597 : /* [dcl.fct] "A parameter with volatile-qualified type is
17598 : deprecated." */
17599 295920686 : if (CP_TYPE_VOLATILE_P (type))
17600 520 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17601 : "%<volatile%>-qualified parameter is "
17602 : "deprecated");
17603 :
17604 : /* Top-level qualifiers on the parameters are
17605 : ignored for function types. */
17606 295920686 : type = cp_build_qualified_type (type, 0);
17607 295920686 : if (TREE_CODE (type) == METHOD_TYPE)
17608 : {
17609 0 : error ("parameter %qD invalidly declared method type", decl);
17610 0 : type = build_pointer_type (type);
17611 0 : TREE_TYPE (decl) = type;
17612 : }
17613 295920686 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17614 : {
17615 : /* Before C++17 DR 393:
17616 : [dcl.fct]/6, parameter types cannot contain pointers
17617 : (references) to arrays of unknown bound. */
17618 1773210 : tree t = TREE_TYPE (type);
17619 1773210 : int ptr = TYPE_PTR_P (type);
17620 :
17621 1942736 : while (1)
17622 : {
17623 1857973 : if (TYPE_PTR_P (t))
17624 : ptr = 1;
17625 1776498 : else if (TREE_CODE (t) != ARRAY_TYPE)
17626 : break;
17627 3298 : else if (!TYPE_DOMAIN (t))
17628 : break;
17629 84763 : t = TREE_TYPE (t);
17630 : }
17631 1773210 : if (TREE_CODE (t) == ARRAY_TYPE)
17632 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17633 : ptr
17634 : ? G_("parameter %qD includes pointer to array of "
17635 : "unknown bound %qT")
17636 : : G_("parameter %qD includes reference to array of "
17637 : "unknown bound %qT"),
17638 : decl, t);
17639 : }
17640 :
17641 295920686 : if (init && !processing_template_decl)
17642 1442142 : init = check_default_argument (decl, init, tf_warning_or_error);
17643 : }
17644 :
17645 295920752 : DECL_CHAIN (decl) = decls;
17646 295920752 : decls = decl;
17647 295920752 : result = tree_cons (init, type, result);
17648 : }
17649 202194279 : decls = nreverse (decls);
17650 202194279 : result = nreverse (result);
17651 202194279 : if (parm)
17652 199604108 : result = chainon (result, void_list_node);
17653 202194279 : *parms = decls;
17654 202194279 : if (any_error)
17655 183 : result = NULL_TREE;
17656 :
17657 183 : if (any_error)
17658 : /* We had parm errors, recover by giving the function (...) type. */
17659 183 : result = NULL_TREE;
17660 :
17661 202194279 : return result;
17662 : }
17663 :
17664 :
17665 : /* D is a constructor or overloaded `operator='.
17666 :
17667 : Let T be the class in which D is declared. Then, this function
17668 : returns:
17669 :
17670 : -1 if D's is an ill-formed constructor or copy assignment operator
17671 : whose first parameter is of type `T'.
17672 : 0 if D is not a copy constructor or copy assignment
17673 : operator.
17674 : 1 if D is a copy constructor or copy assignment operator whose
17675 : first parameter is a reference to non-const qualified T.
17676 : 2 if D is a copy constructor or copy assignment operator whose
17677 : first parameter is a reference to const qualified T.
17678 :
17679 : This function can be used as a predicate. Positive values indicate
17680 : a copy constructor and nonzero values indicate a copy assignment
17681 : operator. */
17682 :
17683 : int
17684 356800641 : copy_fn_p (const_tree d)
17685 : {
17686 356800641 : tree args;
17687 356800641 : tree arg_type;
17688 356800641 : int result = 1;
17689 :
17690 356800641 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17691 :
17692 356800641 : if (TREE_CODE (d) == TEMPLATE_DECL
17693 356800641 : || (DECL_TEMPLATE_INFO (d)
17694 158161509 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17695 : /* Instantiations of template member functions are never copy
17696 : functions. Note that member functions of templated classes are
17697 : represented as template functions internally, and we must
17698 : accept those as copy functions. */
17699 : return 0;
17700 :
17701 301165224 : if (!DECL_CONSTRUCTOR_P (d)
17702 301165224 : && DECL_NAME (d) != assign_op_identifier)
17703 : return 0;
17704 :
17705 300333048 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17706 : {
17707 282 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17708 282 : if (!TYPE_REF_P (object_param)
17709 255 : || TYPE_REF_IS_RVALUE (object_param)
17710 : /* Reject unrelated object parameters. */
17711 237 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17712 519 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17713 : return 0;
17714 228 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17715 : }
17716 : else
17717 300332766 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17718 300332994 : if (!args)
17719 : return 0;
17720 :
17721 300325148 : arg_type = TREE_VALUE (args);
17722 300325148 : if (arg_type == error_mark_node)
17723 : return 0;
17724 :
17725 300325113 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17726 : {
17727 : /* Pass by value copy assignment operator. */
17728 : result = -1;
17729 : }
17730 300323571 : else if (TYPE_REF_P (arg_type)
17731 190336403 : && !TYPE_REF_IS_RVALUE (arg_type)
17732 420854650 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17733 : {
17734 97108542 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17735 96936973 : result = 2;
17736 : }
17737 : else
17738 : return 0;
17739 :
17740 97110084 : args = TREE_CHAIN (args);
17741 :
17742 100065039 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17743 : /* There are more non-optional args. */
17744 2954441 : return 0;
17745 :
17746 : return result;
17747 : }
17748 :
17749 : /* D is a constructor or overloaded `operator='.
17750 :
17751 : Let T be the class in which D is declared. Then, this function
17752 : returns true when D is a move constructor or move assignment
17753 : operator, false otherwise. */
17754 :
17755 : bool
17756 165718745 : move_fn_p (const_tree d)
17757 : {
17758 165718745 : if (cxx_dialect == cxx98)
17759 : /* There are no move constructors if we are in C++98 mode. */
17760 : return false;
17761 :
17762 165641657 : if (TREE_CODE (d) == TEMPLATE_DECL
17763 165641657 : || (DECL_TEMPLATE_INFO (d)
17764 66999149 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17765 : /* Instantiations of template member functions are never move
17766 : functions. Note that member functions of templated classes are
17767 : represented as template functions internally, and we must
17768 : accept those as move functions. */
17769 : return 0;
17770 :
17771 134456418 : return move_signature_fn_p (d);
17772 : }
17773 :
17774 : /* D is a constructor or overloaded `operator='.
17775 :
17776 : Then, this function returns true when D has the same signature as a move
17777 : constructor or move assignment operator (because either it is such a
17778 : ctor/op= or it is a template specialization with the same signature),
17779 : false otherwise. */
17780 :
17781 : bool
17782 134456418 : move_signature_fn_p (const_tree d)
17783 : {
17784 134456418 : tree args;
17785 134456418 : tree arg_type;
17786 134456418 : bool result = false;
17787 :
17788 134456418 : if (!DECL_CONSTRUCTOR_P (d)
17789 134456418 : && DECL_NAME (d) != assign_op_identifier)
17790 : return 0;
17791 :
17792 134456192 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17793 : {
17794 126 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17795 126 : if (!TYPE_REF_P (object_param)
17796 102 : || TYPE_REF_IS_RVALUE (object_param)
17797 : /* Reject unrelated object parameters. */
17798 90 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17799 216 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17800 : return 0;
17801 84 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17802 : }
17803 : else
17804 134456066 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17805 134456150 : if (!args)
17806 : return 0;
17807 :
17808 134453166 : arg_type = TREE_VALUE (args);
17809 134453166 : if (arg_type == error_mark_node)
17810 : return 0;
17811 :
17812 134453158 : if (TYPE_REF_P (arg_type)
17813 68430838 : && TYPE_REF_IS_RVALUE (arg_type)
17814 188060883 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17815 : DECL_CONTEXT (d)))
17816 : result = true;
17817 :
17818 134453158 : args = TREE_CHAIN (args);
17819 :
17820 150211586 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17821 : /* There are more non-optional args. */
17822 12516308 : return false;
17823 :
17824 : return result;
17825 : }
17826 :
17827 : /* Remember any special properties of member function DECL. */
17828 :
17829 : void
17830 366792800 : grok_special_member_properties (tree decl)
17831 : {
17832 366792800 : tree class_type;
17833 :
17834 366792800 : if (TREE_CODE (decl) == USING_DECL
17835 366792800 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17836 : return;
17837 :
17838 343779006 : class_type = DECL_CONTEXT (decl);
17839 343779006 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17840 : {
17841 155225587 : int ctor = copy_fn_p (decl);
17842 :
17843 155225587 : if (!DECL_ARTIFICIAL (decl))
17844 107683642 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17845 :
17846 155225587 : if (ctor > 0)
17847 : {
17848 : /* [class.copy]
17849 :
17850 : A non-template constructor for class X is a copy
17851 : constructor if its first parameter is of type X&, const
17852 : X&, volatile X& or const volatile X&, and either there
17853 : are no other parameters or else all other parameters have
17854 : default arguments. */
17855 31635134 : TYPE_HAS_COPY_CTOR (class_type) = 1;
17856 31635134 : if (ctor > 1)
17857 31555554 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17858 : }
17859 :
17860 155225587 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17861 33147320 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17862 :
17863 155225587 : if (is_list_ctor (decl))
17864 1580566 : TYPE_HAS_LIST_CTOR (class_type) = 1;
17865 :
17866 155225587 : if (maybe_constexpr_fn (decl)
17867 155225587 : && !ctor && !move_fn_p (decl))
17868 56639563 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17869 : }
17870 188553419 : else if (DECL_NAME (decl) == assign_op_identifier)
17871 : {
17872 : /* [class.copy]
17873 :
17874 : A non-template assignment operator for class X is a copy
17875 : assignment operator if its parameter is of type X, X&, const
17876 : X&, volatile X& or const volatile X&. */
17877 :
17878 15694255 : int assop = copy_fn_p (decl);
17879 :
17880 15694255 : if (assop)
17881 : {
17882 7978329 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17883 7978329 : if (assop != 1)
17884 7949402 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17885 : }
17886 : }
17887 172859164 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17888 2859688 : TYPE_HAS_CONVERSION (class_type) = true;
17889 :
17890 : /* Destructors are handled in check_methods. */
17891 : }
17892 :
17893 : /* Check a constructor DECL has the correct form. Complains
17894 : if the class has a constructor of the form X(X). */
17895 :
17896 : bool
17897 49837009 : grok_ctor_properties (const_tree ctype, const_tree decl)
17898 : {
17899 49837009 : int ctor_parm = copy_fn_p (decl);
17900 :
17901 49837009 : if (ctor_parm < 0)
17902 : {
17903 : /* [class.copy]
17904 :
17905 : A declaration of a constructor for a class X is ill-formed if
17906 : its first parameter is of type (optionally cv-qualified) X
17907 : and either there are no other parameters or else all other
17908 : parameters have default arguments.
17909 :
17910 : We *don't* complain about member template instantiations that
17911 : have this form, though; they can occur as we try to decide
17912 : what constructor to use during overload resolution. Since
17913 : overload resolution will never prefer such a constructor to
17914 : the non-template copy constructor (which is either explicitly
17915 : or implicitly defined), there's no need to worry about their
17916 : existence. Theoretically, they should never even be
17917 : instantiated, but that's hard to forestall. */
17918 9 : error_at (DECL_SOURCE_LOCATION (decl),
17919 : "invalid constructor; you probably meant %<%T (const %T&)%>",
17920 : ctype, ctype);
17921 9 : return false;
17922 : }
17923 :
17924 : return true;
17925 : }
17926 :
17927 : /* DECL is a declaration for an overloaded or conversion operator. If
17928 : COMPLAIN is true, errors are issued for invalid declarations. */
17929 :
17930 : bool
17931 62072696 : grok_op_properties (tree decl, bool complain)
17932 : {
17933 62072696 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17934 62072696 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17935 62072696 : tree name = DECL_NAME (decl);
17936 62072696 : location_t loc = DECL_SOURCE_LOCATION (decl);
17937 :
17938 62072696 : tree class_type = DECL_CONTEXT (decl);
17939 62072696 : if (class_type && !CLASS_TYPE_P (class_type))
17940 : class_type = NULL_TREE;
17941 :
17942 62072696 : tree_code operator_code;
17943 62072696 : unsigned op_flags;
17944 62072696 : if (IDENTIFIER_CONV_OP_P (name))
17945 : {
17946 : /* Conversion operators are TYPE_EXPR for the purposes of this
17947 : function. */
17948 : operator_code = TYPE_EXPR;
17949 : op_flags = OVL_OP_FLAG_UNARY;
17950 : }
17951 : else
17952 : {
17953 59609665 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17954 :
17955 59609665 : operator_code = ovl_op->tree_code;
17956 59609665 : op_flags = ovl_op->flags;
17957 59609665 : gcc_checking_assert (operator_code != ERROR_MARK);
17958 59609665 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17959 : }
17960 :
17961 59609665 : if (op_flags & OVL_OP_FLAG_ALLOC)
17962 : {
17963 : /* operator new and operator delete are quite special. */
17964 362307 : if (class_type)
17965 9757 : switch (op_flags)
17966 : {
17967 6686 : case OVL_OP_FLAG_ALLOC:
17968 6686 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17969 6686 : break;
17970 :
17971 2755 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17972 2755 : TYPE_GETS_DELETE (class_type) |= 1;
17973 2755 : break;
17974 :
17975 135 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17976 135 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17977 135 : break;
17978 :
17979 181 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17980 181 : TYPE_GETS_DELETE (class_type) |= 2;
17981 181 : break;
17982 :
17983 0 : default:
17984 0 : gcc_unreachable ();
17985 : }
17986 :
17987 : /* [basic.std.dynamic.allocation]/1:
17988 :
17989 : A program is ill-formed if an allocation function is declared
17990 : in a namespace scope other than global scope or declared
17991 : static in global scope.
17992 :
17993 : The same also holds true for deallocation functions. */
17994 362307 : if (DECL_NAMESPACE_SCOPE_P (decl))
17995 : {
17996 352550 : if (CP_DECL_CONTEXT (decl) != global_namespace)
17997 : {
17998 6 : error_at (loc, "%qD may not be declared within a namespace",
17999 : decl);
18000 6 : return false;
18001 : }
18002 :
18003 352544 : if (!TREE_PUBLIC (decl))
18004 : {
18005 6 : error_at (loc, "%qD may not be declared as static", decl);
18006 6 : return false;
18007 : }
18008 : }
18009 :
18010 : /* Check for replaceable global new/delete operators with
18011 : const std::nothrow_t & last argument, other replaceable global
18012 : new/delete operators are marked in cxx_init_decl_processing. */
18013 362295 : if (CP_DECL_CONTEXT (decl) == global_namespace)
18014 : {
18015 352538 : tree args = argtypes;
18016 352538 : if (args
18017 352526 : && args != void_list_node
18018 1057572 : && same_type_p (TREE_VALUE (args),
18019 : (op_flags & OVL_OP_FLAG_DELETE)
18020 : ? ptr_type_node : size_type_node))
18021 : {
18022 352496 : args = TREE_CHAIN (args);
18023 352496 : if (aligned_allocation_fn_p (decl))
18024 113700 : args = TREE_CHAIN (args);
18025 352496 : if (args
18026 352490 : && args != void_list_node
18027 587769 : && TREE_CHAIN (args) == void_list_node)
18028 : {
18029 206798 : tree t = TREE_VALUE (args);
18030 206798 : if (TYPE_REF_P (t)
18031 117327 : && !TYPE_REF_IS_RVALUE (t)
18032 117327 : && (t = TREE_TYPE (t))
18033 117327 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
18034 323665 : && is_std_class (t, "nothrow_t"))
18035 116867 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
18036 : }
18037 : }
18038 : }
18039 :
18040 362295 : if (op_flags & OVL_OP_FLAG_DELETE)
18041 : {
18042 207043 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
18043 207043 : coerce_delete_type (decl, loc);
18044 : }
18045 : else
18046 : {
18047 155252 : DECL_SET_IS_OPERATOR_NEW (decl, true);
18048 155252 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
18049 : }
18050 :
18051 : return true;
18052 : }
18053 :
18054 : /* An operator function must either be a non-static member function
18055 : or have at least one parameter of a class, a reference to a class,
18056 : an enumeration, or a reference to an enumeration. 13.4.0.6 */
18057 61710389 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
18058 : {
18059 15820099 : if (operator_code == TYPE_EXPR
18060 15820099 : || operator_code == COMPONENT_REF
18061 15820078 : || operator_code == NOP_EXPR)
18062 : {
18063 33 : error_at (loc, "%qD must be a non-static member function", decl);
18064 33 : return false;
18065 : }
18066 :
18067 15820066 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
18068 : {
18069 141685 : if (! DECL_STATIC_FUNCTION_P (decl))
18070 : {
18071 12 : error_at (loc, "%qD must be a member function", decl);
18072 12 : return false;
18073 : }
18074 141673 : if (cxx_dialect < cxx23
18075 : /* For lambdas we diagnose static lambda specifier elsewhere. */
18076 217512 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
18077 : /* For instantiations, we have diagnosed this already. */
18078 250415 : && ! DECL_USE_TEMPLATE (decl))
18079 108736 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
18080 : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
18081 : decl);
18082 141673 : if (operator_code == ARRAY_REF)
18083 : /* static operator[] should have exactly one argument
18084 : for C++20 and earlier, so that it isn't multidimensional. */
18085 : op_flags = OVL_OP_FLAG_UNARY;
18086 : }
18087 15678381 : else if (DECL_STATIC_FUNCTION_P (decl))
18088 : {
18089 9 : error_at (loc, "%qD must be either a non-static member "
18090 : "function or a non-member function", decl);
18091 9 : return false;
18092 : }
18093 : else
18094 423920 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
18095 : {
18096 16102292 : if (!arg || arg == void_list_node)
18097 : {
18098 18 : if (complain)
18099 15 : error_at (loc, "%qD must have an argument of class or "
18100 : "enumerated type", decl);
18101 : return false;
18102 : }
18103 :
18104 16102274 : tree type = non_reference (TREE_VALUE (arg));
18105 16102274 : if (type == error_mark_node)
18106 : return false;
18107 :
18108 : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
18109 : because these checks are performed even on template
18110 : functions. */
18111 16102271 : if (MAYBE_CLASS_TYPE_P (type)
18112 1454399 : || TREE_CODE (type) == ENUMERAL_TYPE)
18113 : break;
18114 423920 : }
18115 : }
18116 :
18117 61707013 : if (operator_code == CALL_EXPR)
18118 : /* There are no further restrictions on the arguments to an overloaded
18119 : "operator ()". */
18120 : return true;
18121 :
18122 : /* C++23 allows an arbitrary number of parameters and default arguments for
18123 : operator[], and none of the other checks below apply. */
18124 51336027 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
18125 : return true;
18126 :
18127 51020368 : if (operator_code == COND_EXPR)
18128 : {
18129 : /* 13.4.0.3 */
18130 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
18131 6 : return false;
18132 : }
18133 :
18134 : /* Count the number of arguments and check for ellipsis. */
18135 : int arity = 0;
18136 144988620 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
18137 : {
18138 93968273 : if (!arg)
18139 : {
18140 15 : error_at (loc, "%qD must not have variable number of arguments",
18141 : decl);
18142 15 : return false;
18143 : }
18144 93968258 : ++arity;
18145 : }
18146 : /* FIXME: We need tests for these errors with xobj member functions. */
18147 : /* Verify correct number of arguments. */
18148 51020347 : switch (op_flags)
18149 : {
18150 10514390 : case OVL_OP_FLAG_AMBIARY:
18151 10514390 : if (arity == 1)
18152 : {
18153 : /* We have a unary instance of an ambi-ary op. Remap to the
18154 : unary one. */
18155 4538053 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
18156 4538053 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
18157 4538053 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
18158 4538053 : operator_code = ovl_op->tree_code;
18159 4538053 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
18160 : }
18161 5976337 : else if (arity != 2)
18162 : {
18163 : /* This was an ambiguous operator but is invalid. */
18164 3 : error_at (loc,
18165 : methodp
18166 : ? G_("%qD must have either zero or one argument")
18167 : : G_("%qD must have either one or two arguments"), decl);
18168 3 : return false;
18169 : }
18170 5976334 : else if ((operator_code == POSTINCREMENT_EXPR
18171 5976334 : || operator_code == POSTDECREMENT_EXPR)
18172 2781037 : && ! processing_template_decl
18173 : /* x++ and x--'s second argument must be an int. */
18174 7794864 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
18175 : integer_type_node))
18176 : {
18177 15 : error_at (loc,
18178 : methodp
18179 : ? G_("postfix %qD must have %<int%> as its argument")
18180 : : G_("postfix %qD must have %<int%> as its second argument"),
18181 : decl);
18182 15 : return false;
18183 : }
18184 : break;
18185 :
18186 3534413 : case OVL_OP_FLAG_UNARY:
18187 3534413 : if (arity != 1)
18188 : {
18189 9 : error_at (loc,
18190 : methodp
18191 : ? G_("%qD must have no arguments")
18192 : : G_("%qD must have exactly one argument"), decl);
18193 9 : return false;
18194 : }
18195 : break;
18196 :
18197 36971544 : case OVL_OP_FLAG_BINARY:
18198 36971544 : if (arity != 2)
18199 : {
18200 3 : error_at (loc,
18201 : methodp
18202 : ? G_("%qD must have exactly one argument")
18203 : : G_("%qD must have exactly two arguments"), decl);
18204 3 : return false;
18205 : }
18206 : break;
18207 :
18208 0 : default:
18209 0 : gcc_unreachable ();
18210 : }
18211 :
18212 : /* There can be no default arguments. */
18213 144988479 : for (tree arg = argtypes; arg && arg != void_list_node;
18214 93968162 : arg = TREE_CHAIN (arg))
18215 93968177 : if (TREE_PURPOSE (arg))
18216 : {
18217 15 : TREE_PURPOSE (arg) = NULL_TREE;
18218 15 : error_at (loc, "%qD cannot have default arguments", decl);
18219 15 : return false;
18220 : }
18221 :
18222 : /* At this point the declaration is well-formed. It may not be
18223 : sensible though. */
18224 :
18225 : /* Check member function warnings only on the in-class declaration.
18226 : There's no point warning on an out-of-class definition. */
18227 51020302 : if (class_type && class_type != current_class_type)
18228 : return true;
18229 :
18230 : /* Warn about conversion operators that will never be used. */
18231 49105928 : if (IDENTIFIER_CONV_OP_P (name)
18232 2462563 : && ! DECL_TEMPLATE_INFO (decl)
18233 50171210 : && warn_class_conversion)
18234 : {
18235 1065249 : tree t = TREE_TYPE (name);
18236 1065249 : int ref = TYPE_REF_P (t);
18237 :
18238 1065249 : if (ref)
18239 21893 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
18240 :
18241 1065249 : if (VOID_TYPE_P (t))
18242 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
18243 : "will never use a type conversion operator", class_type);
18244 1065240 : else if (class_type)
18245 : {
18246 1065240 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
18247 24 : warning_at (loc, OPT_Wclass_conversion,
18248 : ref
18249 : ? G_("converting %qT to a reference to the same type "
18250 : "will never use a type conversion operator")
18251 : : G_("converting %qT to the same type "
18252 : "will never use a type conversion operator"),
18253 : class_type);
18254 : /* Don't force t to be complete here. */
18255 1065225 : else if (MAYBE_CLASS_TYPE_P (t)
18256 607600 : && COMPLETE_TYPE_P (t)
18257 161967 : && DERIVED_FROM_P (t, class_type))
18258 30 : warning_at (loc, OPT_Wclass_conversion,
18259 : ref
18260 : ? G_("converting %qT to a reference to a base class "
18261 : "%qT will never use a type conversion operator")
18262 : : G_("converting %qT to a base class %qT "
18263 : "will never use a type conversion operator"),
18264 : class_type, t);
18265 : }
18266 : }
18267 :
18268 49105928 : if (!warn_ecpp)
18269 : return true;
18270 :
18271 : /* Effective C++ rules below. */
18272 :
18273 : /* More Effective C++ rule 7. */
18274 54 : if (operator_code == TRUTH_ANDIF_EXPR
18275 54 : || operator_code == TRUTH_ORIF_EXPR
18276 54 : || operator_code == COMPOUND_EXPR)
18277 0 : warning_at (loc, OPT_Weffc__,
18278 : "user-defined %qD always evaluates both arguments", decl);
18279 :
18280 : /* More Effective C++ rule 6. */
18281 54 : if (operator_code == POSTINCREMENT_EXPR
18282 : || operator_code == POSTDECREMENT_EXPR
18283 : || operator_code == PREINCREMENT_EXPR
18284 54 : || operator_code == PREDECREMENT_EXPR)
18285 : {
18286 0 : tree arg = TREE_VALUE (argtypes);
18287 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
18288 0 : if (methodp || TYPE_REF_P (arg))
18289 0 : arg = TREE_TYPE (arg);
18290 0 : arg = TYPE_MAIN_VARIANT (arg);
18291 :
18292 0 : if (operator_code == PREINCREMENT_EXPR
18293 : || operator_code == PREDECREMENT_EXPR)
18294 : {
18295 0 : if (!TYPE_REF_P (ret)
18296 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
18297 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
18298 : build_reference_type (arg));
18299 : }
18300 : else
18301 : {
18302 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
18303 0 : warning_at (loc, OPT_Weffc__,
18304 : "postfix %qD should return %qT", decl, arg);
18305 : }
18306 : }
18307 :
18308 : /* Effective C++ rule 23. */
18309 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
18310 : && (operator_code == PLUS_EXPR
18311 9 : || operator_code == MINUS_EXPR
18312 9 : || operator_code == TRUNC_DIV_EXPR
18313 : || operator_code == MULT_EXPR
18314 : || operator_code == TRUNC_MOD_EXPR)
18315 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
18316 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
18317 :
18318 : return true;
18319 : }
18320 :
18321 : /* Return a string giving the keyword associate with CODE. */
18322 :
18323 : const char *
18324 3540 : tag_name (enum tag_types code)
18325 : {
18326 3540 : switch (code)
18327 : {
18328 : case record_type:
18329 : return "struct";
18330 112 : case class_type:
18331 112 : return "class";
18332 3 : case union_type:
18333 3 : return "union";
18334 9 : case enum_type:
18335 9 : return "enum";
18336 3327 : case typename_type:
18337 3327 : case none_type:
18338 3327 : return "typename";
18339 29 : case scope_type:
18340 29 : return nullptr;
18341 : }
18342 0 : gcc_unreachable ();
18343 : }
18344 :
18345 : /* Name lookup in an elaborated-type-specifier (after the keyword
18346 : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
18347 : elaborated-type-specifier is invalid, issue a diagnostic and return
18348 : error_mark_node; otherwise, return the *_TYPE to which it referred.
18349 : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
18350 :
18351 : tree
18352 12578859 : check_elaborated_type_specifier (enum tag_types tag_code,
18353 : tree decl,
18354 : bool allow_template_p)
18355 : {
18356 12578859 : tree type;
18357 :
18358 : /* In the case of:
18359 :
18360 : struct S { struct S *p; };
18361 :
18362 : name lookup will find the TYPE_DECL for the implicit "S::S"
18363 : typedef. Adjust for that here. */
18364 12578859 : if (DECL_SELF_REFERENCE_P (decl))
18365 437394 : decl = TYPE_NAME (TREE_TYPE (decl));
18366 :
18367 12578859 : type = TREE_TYPE (decl);
18368 :
18369 : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
18370 : is false for this case as well. */
18371 12578859 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18372 : {
18373 9 : error ("using template type parameter %qT after %qs",
18374 : type, tag_name (tag_code));
18375 9 : return error_mark_node;
18376 : }
18377 : /* Accept template template parameters. */
18378 12578850 : else if (allow_template_p
18379 7083093 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
18380 7083081 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
18381 : ;
18382 : /* [dcl.type.elab]
18383 :
18384 : If the identifier resolves to a typedef-name or the
18385 : simple-template-id resolves to an alias template
18386 : specialization, the elaborated-type-specifier is ill-formed.
18387 :
18388 : In other words, the only legitimate declaration to use in the
18389 : elaborated type specifier is the implicit typedef created when
18390 : the type is declared. */
18391 12578835 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
18392 437523 : && !DECL_SELF_REFERENCE_P (decl)
18393 12578964 : && tag_code != typename_type)
18394 : {
18395 78 : auto_diagnostic_group d;
18396 78 : if (alias_template_specialization_p (type, nt_opaque))
18397 21 : error ("using alias template specialization %qT after %qs",
18398 : type, tag_name (tag_code));
18399 : else
18400 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
18401 78 : inform (DECL_SOURCE_LOCATION (decl),
18402 : "%qD has a previous declaration here", decl);
18403 78 : return error_mark_node;
18404 78 : }
18405 12578757 : else if (TREE_CODE (type) != RECORD_TYPE
18406 12578757 : && TREE_CODE (type) != UNION_TYPE
18407 : && tag_code != enum_type
18408 73279 : && tag_code != typename_type)
18409 : {
18410 6 : auto_diagnostic_group d;
18411 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
18412 6 : inform (location_of (type), "%qT has a previous declaration here", type);
18413 6 : return error_mark_node;
18414 6 : }
18415 12578751 : else if (TREE_CODE (type) != ENUMERAL_TYPE
18416 12505517 : && tag_code == enum_type)
18417 : {
18418 3 : auto_diagnostic_group d;
18419 3 : error ("%qT referred to as enum", type);
18420 3 : inform (location_of (type), "%qT has a previous declaration here", type);
18421 3 : return error_mark_node;
18422 3 : }
18423 12578748 : else if (!allow_template_p
18424 5495697 : && TREE_CODE (type) == RECORD_TYPE
18425 17947112 : && CLASSTYPE_IS_TEMPLATE (type))
18426 : {
18427 : /* If a class template appears as elaborated type specifier
18428 : without a template header such as:
18429 :
18430 : template <class T> class C {};
18431 : void f(class C); // No template header here
18432 :
18433 : then the required template argument is missing. */
18434 18 : error ("template argument required for %<%s %T%>",
18435 : tag_name (tag_code),
18436 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18437 18 : return error_mark_node;
18438 : }
18439 :
18440 : return type;
18441 : }
18442 :
18443 : /* Lookup NAME of an elaborated type specifier according to SCOPE and
18444 : issue diagnostics if necessary. Return *_TYPE node upon success,
18445 : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18446 : error. */
18447 :
18448 : static tree
18449 27475742 : lookup_and_check_tag (enum tag_types tag_code, tree name,
18450 : TAG_how how, bool template_header_p)
18451 : {
18452 27475742 : tree decl;
18453 27475742 : if (how == TAG_how::GLOBAL)
18454 : {
18455 : /* First try ordinary name lookup, ignoring hidden class name
18456 : injected via friend declaration. */
18457 2211720 : decl = lookup_name (name, LOOK_want::TYPE);
18458 : /* If that fails, the name will be placed in the smallest
18459 : non-class, non-function-prototype scope according to 3.3.1/5.
18460 : We may already have a hidden name declared as friend in this
18461 : scope. So lookup again but not ignoring hidden names.
18462 : If we find one, that name will be made visible rather than
18463 : creating a new tag. */
18464 2211720 : if (!decl)
18465 19336 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18466 : }
18467 : else
18468 25264022 : decl = lookup_elaborated_type (name, how);
18469 :
18470 25283358 : if (!decl)
18471 : /* We found nothing. */
18472 : return NULL_TREE;
18473 :
18474 6200270 : if (TREE_CODE (decl) == TREE_LIST)
18475 : {
18476 3 : auto_diagnostic_group d;
18477 3 : error ("reference to %qD is ambiguous", name);
18478 3 : print_candidates (input_location, decl);
18479 3 : return error_mark_node;
18480 3 : }
18481 :
18482 2516260 : if (DECL_CLASS_TEMPLATE_P (decl)
18483 2516254 : && !template_header_p
18484 8716521 : && how == TAG_how::CURRENT_ONLY)
18485 : {
18486 12 : auto_diagnostic_group d;
18487 12 : error ("class template %qD redeclared as non-template", name);
18488 12 : inform (location_of (decl), "previous declaration here");
18489 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18490 12 : return error_mark_node;
18491 12 : }
18492 :
18493 2516248 : if (DECL_CLASS_TEMPLATE_P (decl)
18494 : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18495 : so ignore a template template parameter. */
18496 6200261 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18497 2516245 : decl = DECL_TEMPLATE_RESULT (decl);
18498 :
18499 6200255 : if (TREE_CODE (decl) != TYPE_DECL)
18500 : /* Found not-a-type. */
18501 : return NULL_TREE;
18502 :
18503 : /* Look for invalid nested type:
18504 : class C {
18505 : class C {};
18506 : }; */
18507 6200234 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18508 : {
18509 6 : error ("%qD has the same name as the class in which it is "
18510 : "declared", decl);
18511 6 : return error_mark_node;
18512 : }
18513 :
18514 : /* Two cases we need to consider when deciding if a class
18515 : template is allowed as an elaborated type specifier:
18516 : 1. It is a self reference to its own class.
18517 : 2. It comes with a template header.
18518 :
18519 : For example:
18520 :
18521 : template <class T> class C {
18522 : class C *c1; // DECL_SELF_REFERENCE_P is true
18523 : class D;
18524 : };
18525 : template <class U> class C; // template_header_p is true
18526 : template <class T> class C<T>::D {
18527 : class C *c2; // DECL_SELF_REFERENCE_P is true
18528 : }; */
18529 :
18530 18600684 : tree t = check_elaborated_type_specifier (tag_code, decl,
18531 : template_header_p
18532 11963077 : | DECL_SELF_REFERENCE_P (decl));
18533 2702395 : if (template_header_p && t && CLASS_TYPE_P (t)
18534 8902611 : && (!CLASSTYPE_TEMPLATE_INFO (t)
18535 2702365 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18536 : {
18537 21 : auto_diagnostic_group d;
18538 21 : error ("%qT is not a template", t);
18539 21 : inform (location_of (t), "previous declaration here");
18540 42 : if (TYPE_CLASS_SCOPE_P (t)
18541 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18542 3 : inform (input_location,
18543 : "perhaps you want to explicitly add %<%T::%>",
18544 3 : TYPE_CONTEXT (t));
18545 21 : return error_mark_node;
18546 21 : }
18547 :
18548 : return t;
18549 : }
18550 :
18551 : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18552 : Define the tag as a forward-reference if it is not defined.
18553 :
18554 : If a declaration is given, process it here, and report an error if
18555 : multiple declarations are not identical.
18556 :
18557 : SCOPE is TS_CURRENT when this is also a definition. Only look in
18558 : the current frame for the name (since C++ allows new names in any
18559 : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18560 : declaration. Only look beginning from the current scope outward up
18561 : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18562 :
18563 : TEMPLATE_HEADER_P is true when this declaration is preceded by
18564 : a set of template parameters. */
18565 :
18566 : tree
18567 27449409 : xref_tag (enum tag_types tag_code, tree name,
18568 : TAG_how how, bool template_header_p)
18569 : {
18570 27449409 : enum tree_code code;
18571 27449409 : tree context = NULL_TREE;
18572 :
18573 27449409 : auto_cond_timevar tv (TV_NAME_LOOKUP);
18574 :
18575 27449409 : gcc_assert (identifier_p (name));
18576 :
18577 27449409 : switch (tag_code)
18578 : {
18579 : case record_type:
18580 : case class_type:
18581 : code = RECORD_TYPE;
18582 : break;
18583 : case union_type:
18584 : code = UNION_TYPE;
18585 : break;
18586 : case enum_type:
18587 : code = ENUMERAL_TYPE;
18588 : break;
18589 0 : default:
18590 0 : gcc_unreachable ();
18591 : }
18592 :
18593 : /* In case of anonymous name, xref_tag is only called to
18594 : make type node and push name. Name lookup is not required. */
18595 27449409 : tree t = NULL_TREE;
18596 27449409 : if (!IDENTIFIER_ANON_P (name))
18597 25018119 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18598 :
18599 27449409 : if (t == error_mark_node)
18600 : return error_mark_node;
18601 :
18602 2952899 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18603 1404776 : && template_class_depth (current_class_type)
18604 28069851 : && template_header_p)
18605 : {
18606 361968 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18607 : return t;
18608 :
18609 : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18610 : a definition of this tag. Since, in addition, we are
18611 : currently processing a (member) template declaration of a
18612 : template class, we must be very careful; consider:
18613 :
18614 : template <class X> struct S1
18615 :
18616 : template <class U> struct S2
18617 : {
18618 : template <class V> friend struct S1;
18619 : };
18620 :
18621 : Here, the S2::S1 declaration should not be confused with the
18622 : outer declaration. In particular, the inner version should
18623 : have a template parameter of level 2, not level 1.
18624 :
18625 : On the other hand, when presented with:
18626 :
18627 : template <class T> struct S1
18628 : {
18629 : template <class U> struct S2 {};
18630 : template <class U> friend struct S2;
18631 : };
18632 :
18633 : the friend must find S1::S2 eventually. We accomplish this
18634 : by making sure that the new type we create to represent this
18635 : declaration has the right TYPE_CONTEXT. */
18636 361965 : context = TYPE_CONTEXT (t);
18637 361965 : t = NULL_TREE;
18638 : }
18639 :
18640 27449295 : if (! t)
18641 : {
18642 : /* If no such tag is yet defined, create a forward-reference node
18643 : and record it as the "definition".
18644 : When a real declaration of this type is found,
18645 : the forward-reference will be altered into a real type. */
18646 21648102 : if (code == ENUMERAL_TYPE)
18647 : {
18648 13 : error ("use of enum %q#D without previous declaration", name);
18649 13 : return error_mark_node;
18650 : }
18651 :
18652 21648089 : t = make_class_type (code);
18653 21648089 : TYPE_CONTEXT (t) = context;
18654 21648089 : if (IDENTIFIER_LAMBDA_P (name))
18655 : /* Mark it as a lambda type right now. Our caller will
18656 : correct the value. */
18657 1667896 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18658 21648089 : t = pushtag (name, t, how);
18659 : }
18660 : else
18661 : {
18662 5801193 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18663 : {
18664 : /* Check that we aren't trying to overload a class with different
18665 : constraints. */
18666 2340397 : if (!redeclare_class_template (t, current_template_parms,
18667 : current_template_constraints ()))
18668 60 : return error_mark_node;
18669 : }
18670 3460796 : else if (!processing_template_decl
18671 3114893 : && CLASS_TYPE_P (t)
18672 6549254 : && CLASSTYPE_IS_TEMPLATE (t))
18673 : {
18674 0 : auto_diagnostic_group d;
18675 0 : error ("redeclaration of %qT as a non-template", t);
18676 0 : inform (location_of (t), "previous declaration %qD", t);
18677 0 : return error_mark_node;
18678 0 : }
18679 :
18680 5801133 : if (modules_p ()
18681 5801133 : && how == TAG_how::CURRENT_ONLY)
18682 : {
18683 12462 : tree decl = TYPE_NAME (t);
18684 12462 : if (!module_may_redeclare (decl))
18685 15 : return error_mark_node;
18686 :
18687 12447 : tree not_tmpl = STRIP_TEMPLATE (decl);
18688 12447 : if (DECL_LANG_SPECIFIC (not_tmpl)
18689 4551 : && DECL_MODULE_ATTACH_P (not_tmpl)
18690 54 : && !DECL_MODULE_EXPORT_P (not_tmpl)
18691 12489 : && module_exporting_p ())
18692 : {
18693 6 : auto_diagnostic_group d;
18694 6 : error ("conflicting exporting for declaration %qD", decl);
18695 6 : inform (DECL_SOURCE_LOCATION (decl),
18696 : "previously declared here without exporting");
18697 6 : }
18698 :
18699 12447 : tree maybe_tmpl = decl;
18700 12447 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18701 9243 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18702 :
18703 : /* FIXME: we should do a more precise check for redefinitions
18704 : of a conflicting using-declaration here, as these diagnostics
18705 : are not ideal. */
18706 12447 : if (DECL_LANG_SPECIFIC (decl)
18707 4551 : && DECL_MODULE_IMPORT_P (decl)
18708 12504 : && CP_DECL_CONTEXT (decl) == current_namespace)
18709 : {
18710 : /* Push it into this TU's symbol slot. */
18711 54 : if (maybe_tmpl != decl)
18712 : /* We're in the template parm binding level.
18713 : Pushtag has logic to slide under that, but we're
18714 : not pushing a *new* type. */
18715 20 : push_nested_namespace (CP_DECL_CONTEXT (decl));
18716 :
18717 54 : pushdecl (maybe_tmpl);
18718 54 : if (maybe_tmpl != decl)
18719 20 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
18720 : }
18721 :
18722 12447 : set_instantiating_module (maybe_tmpl);
18723 : }
18724 : }
18725 :
18726 : return t;
18727 27449409 : }
18728 :
18729 : /* Create the binfo hierarchy for REF with (possibly NULL) base list
18730 : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18731 : access_* node, and the TREE_VALUE is the type of the base-class.
18732 : Non-NULL TREE_TYPE indicates virtual inheritance. */
18733 :
18734 : void
18735 73848769 : xref_basetypes (tree ref, tree base_list)
18736 : {
18737 73848769 : tree *basep;
18738 73848769 : tree binfo, base_binfo;
18739 73848769 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18740 73848769 : unsigned max_bases = 0; /* Maximum direct bases. */
18741 73848769 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18742 : /* Highest direct base index with annotations. */
18743 73848769 : unsigned max_annotated_base = 0;
18744 73848769 : int i;
18745 73848769 : tree default_access;
18746 73848769 : tree igo_prev; /* Track Inheritance Graph Order. */
18747 :
18748 73848769 : if (ref == error_mark_node)
18749 12 : return;
18750 :
18751 : /* The base of a derived class is private by default, all others are
18752 : public. */
18753 73848769 : default_access = (TREE_CODE (ref) == RECORD_TYPE
18754 73204818 : && CLASSTYPE_DECLARED_CLASS (ref)
18755 73848769 : ? access_private_node : access_public_node);
18756 :
18757 : /* First, make sure that any templates in base-classes are
18758 : instantiated. This ensures that if we call ourselves recursively
18759 : we do not get confused about which classes are marked and which
18760 : are not. */
18761 73848769 : basep = &base_list;
18762 108158279 : while (*basep)
18763 : {
18764 34309510 : tree basetype = TREE_VALUE (*basep);
18765 :
18766 : /* The dependent_type_p call below should really be dependent_scope_p
18767 : so that we give a hard error about using an incomplete type as a
18768 : base, but we allow it with a pedwarn for backward
18769 : compatibility. */
18770 34309510 : if (processing_template_decl
18771 34309510 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18772 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
18773 : diagnostics::kind::pedwarn);
18774 34309510 : if (!dependent_type_p (basetype)
18775 34309510 : && !complete_type_or_else (basetype, NULL))
18776 : /* An incomplete type. Remove it from the list. */
18777 15 : *basep = TREE_CHAIN (*basep);
18778 : else
18779 : {
18780 34309495 : max_bases++;
18781 34309495 : if (TREE_CODE (TREE_PURPOSE (*basep)) == TREE_LIST)
18782 28 : max_annotated_base = max_bases;
18783 34309495 : if (TREE_TYPE (*basep))
18784 70712 : max_dvbases++;
18785 34309495 : if (CLASS_TYPE_P (basetype))
18786 33482532 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18787 34309495 : basep = &TREE_CHAIN (*basep);
18788 : }
18789 : }
18790 73848769 : max_vbases += max_dvbases;
18791 :
18792 73848769 : TYPE_MARKED_P (ref) = 1;
18793 :
18794 : /* The binfo slot should be empty, unless this is an (ill-formed)
18795 : redefinition. */
18796 73848769 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18797 :
18798 73848769 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18799 :
18800 73848769 : binfo = make_tree_binfo (max_bases);
18801 :
18802 73848769 : TYPE_BINFO (ref) = binfo;
18803 73848769 : BINFO_OFFSET (binfo) = size_zero_node;
18804 73848769 : BINFO_TYPE (binfo) = ref;
18805 :
18806 : /* Apply base-class info set up to the variants of this type. */
18807 73848769 : fixup_type_variants (ref);
18808 :
18809 73848769 : if (max_bases)
18810 : {
18811 33372560 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases + max_annotated_base);
18812 33372560 : BINFO_BASE_ACCESSES (binfo)->quick_grow (max_bases + max_annotated_base);
18813 : /* A C++98 POD cannot have base classes. */
18814 33372560 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18815 :
18816 33372560 : if (TREE_CODE (ref) == UNION_TYPE)
18817 : {
18818 12 : error ("derived union %qT invalid", ref);
18819 12 : return;
18820 : }
18821 : }
18822 :
18823 33372548 : if (max_bases > 1)
18824 740325 : warning (OPT_Wmultiple_inheritance,
18825 : "%qT defined with multiple direct bases", ref);
18826 :
18827 73848757 : if (max_vbases)
18828 : {
18829 : /* An aggregate can't have virtual base classes. */
18830 210033 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18831 :
18832 210033 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
18833 :
18834 210033 : if (max_dvbases)
18835 69298 : warning (OPT_Wvirtual_inheritance,
18836 : "%qT defined with direct virtual base", ref);
18837 : }
18838 :
18839 108158240 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18840 : {
18841 34309483 : tree access = TREE_PURPOSE (base_list);
18842 34309483 : tree annotations = NULL_TREE;
18843 34309483 : if (TREE_CODE (access) == TREE_LIST)
18844 : {
18845 28 : annotations = TREE_VALUE (access);
18846 28 : access = TREE_PURPOSE (access);
18847 78 : for (tree *d = &annotations; *d; )
18848 : {
18849 50 : if (annotation_p (*d))
18850 : {
18851 50 : tree name = get_attribute_name (*d);
18852 50 : tree args = TREE_VALUE (*d);
18853 50 : const attribute_spec *as
18854 50 : = lookup_attribute_spec (TREE_PURPOSE (*d));
18855 50 : bool no_add_attrs = false;
18856 50 : as->handler (&binfo, name, args, 0, &no_add_attrs);
18857 50 : if (no_add_attrs)
18858 : {
18859 2 : *d = TREE_CHAIN (*d);
18860 2 : continue;
18861 : }
18862 : }
18863 48 : d = &TREE_CHAIN (*d);
18864 : }
18865 : }
18866 34309483 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18867 34309483 : tree basetype = TREE_VALUE (base_list);
18868 :
18869 34309483 : if (access == access_default_node)
18870 4648456 : access = default_access;
18871 :
18872 : /* Before C++17, an aggregate cannot have base classes. In C++17, an
18873 : aggregate can't have virtual, private, or protected base classes. */
18874 34309483 : if (cxx_dialect < cxx17
18875 34085076 : || access != access_public_node
18876 33111282 : || via_virtual)
18877 1265341 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18878 :
18879 34309483 : if (PACK_EXPANSION_P (basetype))
18880 29953 : basetype = PACK_EXPANSION_PATTERN (basetype);
18881 34309483 : if (TREE_CODE (basetype) == TYPE_DECL)
18882 0 : basetype = TREE_TYPE (basetype);
18883 34309483 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18884 : {
18885 24 : error ("base type %qT fails to be a struct or class type",
18886 : basetype);
18887 24 : goto dropped_base;
18888 : }
18889 34309459 : else if (ANON_AGGR_TYPE_P (basetype))
18890 : {
18891 2 : error ("base type %qT is anonymous struct type", basetype);
18892 2 : goto dropped_base;
18893 : }
18894 :
18895 34309457 : base_binfo = NULL_TREE;
18896 34309457 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18897 : {
18898 27102177 : base_binfo = TYPE_BINFO (basetype);
18899 : /* The original basetype could have been a typedef'd type. */
18900 27102177 : basetype = BINFO_TYPE (base_binfo);
18901 :
18902 : /* Inherit flags from the base. */
18903 81306531 : TYPE_HAS_NEW_OPERATOR (ref)
18904 27102177 : |= TYPE_HAS_NEW_OPERATOR (basetype);
18905 81306531 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18906 27102177 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18907 27102177 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18908 27102177 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18909 81306531 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
18910 27102177 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18911 81306531 : CLASSTYPE_REPEATED_BASE_P (ref)
18912 27102177 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
18913 : }
18914 :
18915 : /* We must do this test after we've seen through a typedef
18916 : type. */
18917 34309457 : if (TYPE_MARKED_P (basetype))
18918 : {
18919 18 : if (basetype == ref)
18920 0 : error ("recursive type %qT undefined", basetype);
18921 : else
18922 18 : error ("duplicate base type %qT invalid", basetype);
18923 18 : goto dropped_base;
18924 : }
18925 :
18926 34309439 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18927 : /* Regenerate the pack expansion for the bases. */
18928 29953 : basetype = make_pack_expansion (basetype);
18929 :
18930 34309439 : TYPE_MARKED_P (basetype) = 1;
18931 :
18932 34309439 : base_binfo = copy_binfo (base_binfo, basetype, ref,
18933 : &igo_prev, via_virtual);
18934 34309439 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
18935 34238742 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
18936 :
18937 34309439 : unsigned len;
18938 34309439 : len = BINFO_N_BASE_BINFOS (binfo);
18939 34309439 : BINFO_BASE_APPEND (binfo, base_binfo);
18940 34309439 : BINFO_BASE_ACCESS (binfo, len) = access;
18941 34309439 : if (len < max_annotated_base)
18942 34 : BINFO_BASE_ACCESS (binfo, max_bases + len) = annotations;
18943 34309439 : continue;
18944 :
18945 44 : dropped_base:
18946 : /* Update max_vbases to reflect the reality that we are dropping
18947 : this base: if it reaches zero we want to undo the vec_alloc
18948 : above to avoid inconsistencies during error-recovery: eg, in
18949 : build_special_member_call, CLASSTYPE_VBASECLASSES non null
18950 : and vtt null (c++/27952). */
18951 44 : if (via_virtual)
18952 12 : max_vbases--;
18953 44 : if (CLASS_TYPE_P (basetype))
18954 32 : max_vbases
18955 35 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18956 34309439 : }
18957 :
18958 73848757 : unsigned len = BINFO_N_BASE_BINFOS (binfo);
18959 73848757 : if (len < max_bases)
18960 : {
18961 44 : if (len && max_annotated_base)
18962 0 : memmove (&BINFO_BASE_ACCESS (binfo, len),
18963 0 : &BINFO_BASE_ACCESS (binfo, max_bases),
18964 0 : MIN (max_annotated_base, len) * sizeof (tree));
18965 44 : BINFO_BASE_ACCESSES (binfo)->truncate (len + MIN (max_annotated_base,
18966 : len));
18967 : }
18968 :
18969 73848757 : if (CLASSTYPE_VBASECLASSES (ref)
18970 73848757 : && max_vbases == 0)
18971 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
18972 :
18973 74058778 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18974 : /* If we didn't get max_vbases vbases, we must have shared at
18975 : least one of them, and are therefore diamond shaped. */
18976 22912 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18977 :
18978 : /* Unmark all the types. */
18979 108158196 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18980 34309439 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18981 73848757 : TYPE_MARKED_P (ref) = 0;
18982 :
18983 : /* Now see if we have a repeated base type. */
18984 73848757 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
18985 : {
18986 186288931 : for (base_binfo = binfo; base_binfo;
18987 112440610 : base_binfo = TREE_CHAIN (base_binfo))
18988 : {
18989 112441356 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18990 : {
18991 746 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18992 746 : break;
18993 : }
18994 112440610 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18995 : }
18996 186288931 : for (base_binfo = binfo; base_binfo;
18997 112440610 : base_binfo = TREE_CHAIN (base_binfo))
18998 112441356 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18999 112440610 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
19000 : else
19001 : break;
19002 : }
19003 : }
19004 :
19005 :
19006 : /* Copies the enum-related properties from type SRC to type DST.
19007 : Used with the underlying type of an enum and the enum itself. */
19008 : static void
19009 2082594 : copy_type_enum (tree dst, tree src)
19010 : {
19011 2082594 : tree t;
19012 4165212 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
19013 : {
19014 2082618 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
19015 2082618 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
19016 2082618 : TYPE_SIZE (t) = TYPE_SIZE (src);
19017 2082618 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
19018 2082618 : SET_TYPE_MODE (dst, TYPE_MODE (src));
19019 2082618 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
19020 2082618 : unsigned valign = TYPE_ALIGN (src);
19021 2082618 : if (TYPE_USER_ALIGN (t))
19022 12 : valign = MAX (valign, TYPE_ALIGN (t));
19023 : else
19024 2082606 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
19025 2082618 : SET_TYPE_ALIGN (t, valign);
19026 2082618 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
19027 : }
19028 2082594 : }
19029 :
19030 : /* Begin compiling the definition of an enumeration type.
19031 : NAME is its name,
19032 :
19033 : if ENUMTYPE is not NULL_TREE then the type has already been found.
19034 :
19035 : UNDERLYING_TYPE is the type that will be used as the storage for
19036 : the enumeration type. This should be NULL_TREE if no storage type
19037 : was specified.
19038 :
19039 : ATTRIBUTES are any attributes specified after the enum-key.
19040 :
19041 : SCOPED_ENUM_P is true if this is a scoped enumeration type.
19042 :
19043 : if IS_NEW is not NULL, gets TRUE iff a new type is created.
19044 :
19045 : Returns the type object, as yet incomplete.
19046 : Also records info about it so that build_enumerator
19047 : may be used to declare the individual values as they are read. */
19048 :
19049 : tree
19050 2457759 : start_enum (tree name, tree enumtype, tree underlying_type,
19051 : tree attributes, bool scoped_enum_p, bool *is_new)
19052 : {
19053 2457759 : tree prevtype = NULL_TREE;
19054 2457759 : gcc_assert (identifier_p (name));
19055 :
19056 2457759 : if (is_new)
19057 1983636 : *is_new = false;
19058 : /* [C++0x dcl.enum]p5:
19059 :
19060 : If not explicitly specified, the underlying type of a scoped
19061 : enumeration type is int. */
19062 2457759 : if (!underlying_type && scoped_enum_p)
19063 181545 : underlying_type = integer_type_node;
19064 :
19065 2457759 : if (underlying_type)
19066 614028 : underlying_type = cv_unqualified (underlying_type);
19067 :
19068 : /* If this is the real definition for a previous forward reference,
19069 : fill in the contents in the same object that used to be the
19070 : forward reference. */
19071 2457759 : if (!enumtype)
19072 2457623 : enumtype = lookup_and_check_tag (enum_type, name,
19073 : /*tag_scope=*/TAG_how::CURRENT_ONLY,
19074 : /*template_header_p=*/false);
19075 :
19076 : /* In case of a template_decl, the only check that should be deferred
19077 : to instantiation time is the comparison of underlying types. */
19078 2457759 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
19079 : {
19080 : /* Attempt to set the declaring module. */
19081 37110 : if (modules_p ())
19082 : {
19083 216 : tree decl = TYPE_NAME (enumtype);
19084 216 : if (!module_may_redeclare (decl))
19085 3 : enumtype = error_mark_node;
19086 : else
19087 213 : set_instantiating_module (decl);
19088 : }
19089 :
19090 37110 : if (enumtype == error_mark_node)
19091 : ;
19092 37326 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
19093 : {
19094 6 : auto_diagnostic_group d;
19095 6 : error_at (input_location, "scoped/unscoped mismatch "
19096 : "in enum %q#T", enumtype);
19097 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19098 : "previous definition here");
19099 6 : enumtype = error_mark_node;
19100 6 : }
19101 37101 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
19102 : {
19103 0 : auto_diagnostic_group d;
19104 0 : error_at (input_location, "underlying type mismatch "
19105 : "in enum %q#T", enumtype);
19106 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19107 : "previous definition here");
19108 0 : enumtype = error_mark_node;
19109 0 : }
19110 37086 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
19111 74187 : && !same_type_p (underlying_type,
19112 : ENUM_UNDERLYING_TYPE (enumtype)))
19113 : {
19114 29 : auto_diagnostic_group d;
19115 29 : error_at (input_location, "different underlying type "
19116 : "in enum %q#T", enumtype);
19117 29 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19118 : "previous definition here");
19119 29 : underlying_type = NULL_TREE;
19120 29 : }
19121 : }
19122 :
19123 2457759 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
19124 37101 : || processing_template_decl)
19125 : {
19126 : /* In case of error, make a dummy enum to allow parsing to
19127 : continue. */
19128 2420759 : if (enumtype == error_mark_node)
19129 : {
19130 9 : name = make_anon_name ();
19131 9 : enumtype = NULL_TREE;
19132 : }
19133 :
19134 : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
19135 : of an opaque enum, or an opaque enum of an already defined
19136 : enumeration (C++11).
19137 : In any other case, it'll be NULL_TREE. */
19138 2420759 : if (!enumtype)
19139 : {
19140 2420655 : if (is_new)
19141 1946535 : *is_new = true;
19142 : }
19143 2420759 : prevtype = enumtype;
19144 :
19145 : /* Do not push the decl more than once. */
19146 2420759 : if (!enumtype
19147 104 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
19148 : {
19149 2420658 : enumtype = cxx_make_type (ENUMERAL_TYPE);
19150 2420658 : enumtype = pushtag (name, enumtype);
19151 :
19152 2420658 : if (enumtype != error_mark_node)
19153 : {
19154 : /* The enum is considered opaque until the opening '{' of the
19155 : enumerator list. */
19156 2420640 : SET_OPAQUE_ENUM_P (enumtype, true);
19157 2420640 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
19158 :
19159 : /* std::byte aliases anything. */
19160 2420640 : if (TYPE_CONTEXT (enumtype) == std_node
19161 2789287 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
19162 13901 : TYPE_ALIAS_SET (enumtype) = 0;
19163 : }
19164 : }
19165 : else
19166 101 : enumtype = xref_tag (enum_type, name);
19167 :
19168 2420759 : if (enumtype == error_mark_node)
19169 : return error_mark_node;
19170 : }
19171 :
19172 2457741 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
19173 :
19174 2457741 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19175 :
19176 2457741 : if (underlying_type)
19177 : {
19178 613996 : if (ENUM_UNDERLYING_TYPE (enumtype))
19179 : /* We already checked that it matches, don't change it to a different
19180 : typedef variant. */;
19181 576939 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
19182 : {
19183 576865 : copy_type_enum (enumtype, underlying_type);
19184 576865 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
19185 : }
19186 74 : else if (dependent_type_p (underlying_type))
19187 61 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
19188 : else
19189 : {
19190 13 : error ("underlying type %qT of %qT must be an integral type",
19191 : underlying_type, enumtype);
19192 13 : copy_type_enum (enumtype, integer_type_node);
19193 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
19194 : }
19195 : }
19196 :
19197 : /* If into a template class, the returned enum is always the first
19198 : declaration (opaque or not) seen. This way all the references to
19199 : this type will be to the same declaration. The following ones are used
19200 : only to check for definition errors. */
19201 2457741 : if (prevtype && processing_template_decl)
19202 : return prevtype;
19203 : else
19204 2457640 : return enumtype;
19205 : }
19206 :
19207 : /* Returns true if TYPE is an enum that uses an enumerator name for
19208 : linkage purposes at namespace scope. The term is defined in [dcl.enum]/12
19209 : for all enums, not just those at namespace scope, but for backward ABI
19210 : compatibility we want to treat those not at namespace scope the old way
19211 : and e.g. mangle the class scope ones based on their position within the
19212 : class rather than the first enumerator. */
19213 :
19214 : bool
19215 421554986 : enum_with_enumerator_for_linkage_p (tree type)
19216 : {
19217 421554986 : return (cxx_dialect >= cxx20
19218 416860602 : && UNSCOPED_ENUM_P (type)
19219 20412452 : && TYPE_ANON_P (type)
19220 3158118 : && TYPE_VALUES (type)
19221 423494996 : && TYPE_NAMESPACE_SCOPE_P (type));
19222 : }
19223 :
19224 : /* After processing and defining all the values of an enumeration type,
19225 : install their decls in the enumeration type.
19226 : ENUMTYPE is the type object. */
19227 :
19228 : void
19229 2352348 : finish_enum_value_list (tree enumtype)
19230 : {
19231 2352348 : tree values;
19232 2352348 : tree underlying_type;
19233 2352348 : tree decl;
19234 2352348 : tree value;
19235 2352348 : tree minnode, maxnode;
19236 2352348 : tree t;
19237 :
19238 2352348 : ENUM_BEING_DEFINED_P (enumtype) = 0;
19239 :
19240 2352348 : bool fixed_underlying_type_p
19241 2352348 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
19242 :
19243 : /* We built up the VALUES in reverse order. */
19244 2352348 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
19245 :
19246 : /* For an enum defined in a template, just set the type of the values;
19247 : all further processing is postponed until the template is
19248 : instantiated. We need to set the type so that tsubst of a CONST_DECL
19249 : works. */
19250 2352348 : if (processing_template_decl)
19251 : {
19252 463961 : for (values = TYPE_VALUES (enumtype);
19253 830861 : values;
19254 463961 : values = TREE_CHAIN (values))
19255 463961 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
19256 : return;
19257 : }
19258 :
19259 : /* Determine the minimum and maximum values of the enumerators. */
19260 1985448 : if (TYPE_VALUES (enumtype))
19261 : {
19262 : minnode = maxnode = NULL_TREE;
19263 :
19264 11889038 : for (values = TYPE_VALUES (enumtype);
19265 13836731 : values;
19266 11889038 : values = TREE_CHAIN (values))
19267 : {
19268 11889038 : decl = TREE_VALUE (values);
19269 :
19270 : /* [dcl.enum]: Following the closing brace of an enum-specifier,
19271 : each enumerator has the type of its enumeration. Prior to the
19272 : closing brace, the type of each enumerator is the type of its
19273 : initializing value. */
19274 11889038 : TREE_TYPE (decl) = enumtype;
19275 :
19276 : /* Update the minimum and maximum values, if appropriate. */
19277 11889038 : value = DECL_INITIAL (decl);
19278 11889038 : if (!value || TREE_CODE (value) != INTEGER_CST)
19279 7 : value = integer_zero_node;
19280 : /* Figure out what the minimum and maximum values of the
19281 : enumerators are. */
19282 11889038 : if (!minnode)
19283 : minnode = maxnode = value;
19284 9941345 : else if (tree_int_cst_lt (maxnode, value))
19285 : maxnode = value;
19286 1545180 : else if (tree_int_cst_lt (value, minnode))
19287 175451 : minnode = value;
19288 : }
19289 : }
19290 : else
19291 : /* [dcl.enum]
19292 :
19293 : If the enumerator-list is empty, the underlying type is as if
19294 : the enumeration had a single enumerator with value 0. */
19295 37755 : minnode = maxnode = integer_zero_node;
19296 :
19297 1985448 : if (!fixed_underlying_type_p)
19298 : {
19299 : /* Compute the number of bits require to represent all values of the
19300 : enumeration. We must do this before the type of MINNODE and
19301 : MAXNODE are transformed, since tree_int_cst_min_precision relies
19302 : on the TREE_TYPE of the value it is passed. */
19303 1505716 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
19304 1505716 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
19305 1505716 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
19306 1505716 : int precision = MAX (lowprec, highprec);
19307 1505716 : unsigned int itk;
19308 1505716 : bool use_short_enum;
19309 :
19310 : /* Determine the underlying type of the enumeration.
19311 :
19312 : [dcl.enum]
19313 :
19314 : The underlying type of an enumeration is an integral type that
19315 : can represent all the enumerator values defined in the
19316 : enumeration. It is implementation-defined which integral type is
19317 : used as the underlying type for an enumeration except that the
19318 : underlying type shall not be larger than int unless the value of
19319 : an enumerator cannot fit in an int or unsigned int.
19320 :
19321 : We use "int" or an "unsigned int" as the underlying type, even if
19322 : a smaller integral type would work, unless the user has
19323 : explicitly requested that we use the smallest possible type. The
19324 : user can request that for all enumerations with a command line
19325 : flag, or for just one enumeration with an attribute. */
19326 :
19327 3011432 : use_short_enum = flag_short_enums
19328 1505716 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
19329 :
19330 : /* If the precision of the type was specified with an attribute and it
19331 : was too small, give an error. Otherwise, use it. */
19332 1505716 : if (TYPE_PRECISION (enumtype))
19333 : {
19334 11 : if (precision > TYPE_PRECISION (enumtype))
19335 0 : error ("specified mode too small for enumerated values");
19336 : else
19337 : {
19338 11 : use_short_enum = true;
19339 11 : precision = TYPE_PRECISION (enumtype);
19340 : }
19341 : }
19342 :
19343 2927797 : for (itk = (use_short_enum ? itk_char : itk_int);
19344 2927797 : itk != itk_none;
19345 : itk++)
19346 : {
19347 2927794 : underlying_type = integer_types[itk];
19348 2927794 : if (underlying_type != NULL_TREE
19349 2927776 : && TYPE_PRECISION (underlying_type) >= precision
19350 5853469 : && TYPE_SIGN (underlying_type) == sgn)
19351 : break;
19352 : }
19353 1505716 : if (itk == itk_none)
19354 : {
19355 : /* DR 377
19356 :
19357 : IF no integral type can represent all the enumerator values, the
19358 : enumeration is ill-formed. */
19359 3 : error ("no integral type can represent all of the enumerator values "
19360 : "for %qT", enumtype);
19361 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
19362 3 : underlying_type = integer_types[itk_unsigned_long_long];
19363 : }
19364 :
19365 : /* [dcl.enum]
19366 :
19367 : The value of sizeof() applied to an enumeration type, an object
19368 : of an enumeration type, or an enumerator, is the value of sizeof()
19369 : applied to the underlying type. */
19370 1505716 : copy_type_enum (enumtype, underlying_type);
19371 :
19372 : /* Compute the minimum and maximum values for the type.
19373 :
19374 : [dcl.enum]
19375 :
19376 : For an enumeration where emin is the smallest enumerator and emax
19377 : is the largest, the values of the enumeration are the values of the
19378 : underlying type in the range bmin to bmax, where bmin and bmax are,
19379 : respectively, the smallest and largest values of the smallest bit-
19380 : field that can store emin and emax. */
19381 :
19382 : /* The middle-end currently assumes that types with TYPE_PRECISION
19383 : narrower than their underlying type are suitably zero or sign
19384 : extended to fill their mode. Similarly, it assumes that the front
19385 : end assures that a value of a particular type must be within
19386 : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
19387 :
19388 : We used to set these fields based on bmin and bmax, but that led
19389 : to invalid assumptions like optimizing away bounds checking. So
19390 : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
19391 : TYPE_MAX_VALUE to the values for the mode above and only restrict
19392 : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
19393 1505716 : ENUM_UNDERLYING_TYPE (enumtype)
19394 1505716 : = build_distinct_type_copy (underlying_type);
19395 1505716 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
19396 1505716 : set_min_and_max_values_for_integral_type
19397 1505716 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
19398 :
19399 : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
19400 1505716 : if (flag_strict_enums)
19401 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
19402 :
19403 1505716 : if (use_short_enum)
19404 : {
19405 122 : TYPE_PACKED (enumtype) = use_short_enum;
19406 122 : fixup_attribute_variants (enumtype);
19407 : }
19408 : }
19409 : else
19410 479732 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
19411 :
19412 : /* If the enum is exported, mark the consts too. */
19413 1985448 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
19414 1583071 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
19415 1986036 : && at_namespace_scope_p ());
19416 :
19417 : /* Convert each of the enumerators to the type of the underlying
19418 : type of the enumeration. */
19419 13874486 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
19420 : {
19421 11889038 : decl = TREE_VALUE (values);
19422 11889038 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
19423 11889038 : if (fixed_underlying_type_p)
19424 : /* If the enumeration type has a fixed underlying type, we
19425 : already checked all of the enumerator values. */
19426 4169412 : value = DECL_INITIAL (decl);
19427 : else
19428 7719626 : value = perform_implicit_conversion (underlying_type,
19429 7719626 : DECL_INITIAL (decl),
19430 : tf_warning_or_error);
19431 11889038 : if (!value)
19432 3 : value = integer_zero_node;
19433 : /* Do not clobber shared ints. But do share identical enumerators. */
19434 11889038 : value = fold_convert (enumtype, value);
19435 :
19436 11889038 : DECL_INITIAL (decl) = value;
19437 11889038 : if (export_p)
19438 3594 : DECL_MODULE_EXPORT_P (decl) = true;
19439 11889038 : }
19440 :
19441 : /* Fix up all variant types of this enum type. */
19442 3970942 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
19443 1985494 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
19444 :
19445 1985448 : if (at_class_scope_p ()
19446 1067138 : && COMPLETE_TYPE_P (current_class_type)
19447 1985512 : && UNSCOPED_ENUM_P (enumtype))
19448 : {
19449 45 : insert_late_enum_def_bindings (current_class_type, enumtype);
19450 : /* TYPE_FIELDS needs fixup. */
19451 45 : fixup_type_variants (current_class_type);
19452 : }
19453 :
19454 : /* P2115: An unnamed enum uses the name of its first enumerator for
19455 : linkage purposes; reset the type linkage if that is the case. */
19456 1985448 : if (enum_with_enumerator_for_linkage_p (enumtype))
19457 355236 : reset_type_linkage (enumtype);
19458 :
19459 : /* Finish debugging output for this type. */
19460 1985448 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
19461 :
19462 : /* Each enumerator now has the type of its enumeration. Clear the cache
19463 : so that this change in types doesn't confuse us later on. */
19464 1985448 : clear_cv_and_fold_caches ();
19465 : }
19466 :
19467 : /* Finishes the enum type. This is called only the first time an
19468 : enumeration is seen, be it opaque or odinary.
19469 : ENUMTYPE is the type object. */
19470 :
19471 : void
19472 2340502 : finish_enum (tree enumtype)
19473 : {
19474 2340502 : if (processing_template_decl)
19475 : {
19476 366928 : if (at_function_scope_p ())
19477 9556 : add_stmt (build_min (TAG_DEFN, enumtype));
19478 : return;
19479 : }
19480 :
19481 : /* If this is a forward declaration, there should not be any variants,
19482 : though we can get a variant in the middle of an enum-specifier with
19483 : wacky code like 'enum E { e = sizeof(const E*) };' */
19484 3947148 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19485 : && (TYPE_VALUES (enumtype)
19486 : || !TYPE_NEXT_VARIANT (enumtype)));
19487 : }
19488 :
19489 : /* Build and install a CONST_DECL for an enumeration constant of the
19490 : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19491 : Apply ATTRIBUTES if available. LOC is the location of NAME.
19492 : Assignment of sequential values by default is handled here. */
19493 :
19494 : tree
19495 12352973 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19496 : location_t loc)
19497 : {
19498 12352973 : tree decl;
19499 12352973 : tree context;
19500 12352973 : tree type;
19501 :
19502 : /* scalar_constant_value will pull out this expression, so make sure
19503 : it's folded as appropriate.
19504 :
19505 : Creating a TARGET_EXPR in a template breaks when substituting, and
19506 : here we would create it for instance when using a class prvalue with
19507 : a user-defined conversion function. So don't use such a tree. We
19508 : instantiate VALUE here to get errors about bad enumerators even in
19509 : a template that does not get instantiated. */
19510 12352973 : if (processing_template_decl)
19511 463961 : value = maybe_fold_non_dependent_expr (value);
19512 :
19513 : /* If the VALUE was erroneous, pretend it wasn't there; that will
19514 : result in the enum being assigned the next value in sequence. */
19515 12352973 : if (value == error_mark_node)
19516 : value = NULL_TREE;
19517 :
19518 : /* Remove no-op casts from the value. */
19519 12352935 : if (value)
19520 9477946 : STRIP_TYPE_NOPS (value);
19521 :
19522 12352973 : if (! processing_template_decl)
19523 : {
19524 : /* Validate and default VALUE. */
19525 11889012 : if (value != NULL_TREE)
19526 : {
19527 5412315 : if (!ENUM_UNDERLYING_TYPE (enumtype))
19528 : {
19529 2758898 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19530 : value, true);
19531 2758898 : if (tmp_value)
19532 5412315 : value = tmp_value;
19533 : }
19534 2653417 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19535 : (TREE_TYPE (value)))
19536 39 : value = perform_implicit_conversion_flags
19537 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19538 : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19539 :
19540 5412315 : if (value == error_mark_node)
19541 : value = NULL_TREE;
19542 :
19543 5412294 : if (value != NULL_TREE)
19544 : {
19545 5412294 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19546 : (TREE_TYPE (value)))
19547 : {
19548 17 : error_at (cp_expr_loc_or_input_loc (value),
19549 : "enumerator value for %qD must have integral or "
19550 : "unscoped enumeration type", name);
19551 17 : value = NULL_TREE;
19552 : }
19553 : else
19554 : {
19555 5412277 : value = cxx_constant_value (value);
19556 :
19557 5412277 : if (TREE_CODE (value) != INTEGER_CST)
19558 : {
19559 48 : error ("enumerator value for %qD is not an integer "
19560 : "constant", name);
19561 48 : value = NULL_TREE;
19562 : }
19563 : }
19564 : }
19565 : }
19566 :
19567 : /* Default based on previous value. */
19568 65 : if (value == NULL_TREE)
19569 : {
19570 6476783 : if (TYPE_VALUES (enumtype))
19571 : {
19572 5926908 : tree prev_value;
19573 :
19574 : /* C++03 7.2/4: If no initializer is specified for the first
19575 : enumerator, the type is an unspecified integral
19576 : type. Otherwise the type is the same as the type of the
19577 : initializing value of the preceding enumerator unless the
19578 : incremented value is not representable in that type, in
19579 : which case the type is an unspecified integral type
19580 : sufficient to contain the incremented value. */
19581 5926908 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19582 5926908 : if (TREE_CODE (prev_value) != INTEGER_CST)
19583 2 : value = error_mark_node;
19584 : else
19585 : {
19586 5926906 : wi::overflow_type overflowed;
19587 5926906 : tree type = TREE_TYPE (prev_value);
19588 5926906 : signop sgn = TYPE_SIGN (type);
19589 5926906 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
19590 5926906 : &overflowed);
19591 5926906 : if (!overflowed)
19592 : {
19593 5926906 : bool pos = !wi::neg_p (wi, sgn);
19594 5926906 : if (!wi::fits_to_tree_p (wi, type))
19595 : {
19596 : unsigned int itk;
19597 53 : for (itk = itk_int; itk != itk_none; itk++)
19598 : {
19599 53 : type = integer_types[itk];
19600 53 : if (type != NULL_TREE
19601 53 : && (pos || !TYPE_UNSIGNED (type))
19602 106 : && wi::fits_to_tree_p (wi, type))
19603 : break;
19604 : }
19605 17 : if (type && cxx_dialect < cxx11
19606 6 : && itk > itk_unsigned_long)
19607 3 : pedwarn (input_location, OPT_Wlong_long,
19608 : pos ? G_("\
19609 : incremented enumerator value is too large for %<unsigned long%>") : G_("\
19610 : incremented enumerator value is too large for %<long%>"));
19611 : }
19612 5926906 : if (type == NULL_TREE)
19613 0 : overflowed = wi::OVF_UNKNOWN;
19614 : else
19615 5926906 : value = wide_int_to_tree (type, wi);
19616 : }
19617 :
19618 5926906 : if (overflowed)
19619 : {
19620 0 : error ("overflow in enumeration values at %qD", name);
19621 0 : value = error_mark_node;
19622 : }
19623 5926906 : }
19624 : }
19625 : else
19626 549875 : value = integer_zero_node;
19627 : }
19628 :
19629 : /* Remove no-op casts from the value. */
19630 11889012 : STRIP_TYPE_NOPS (value);
19631 :
19632 : /* If the underlying type of the enum is fixed, check whether
19633 : the enumerator values fits in the underlying type. If it
19634 : does not fit, the program is ill-formed [C++0x dcl.enum]. */
19635 11889012 : if (ENUM_UNDERLYING_TYPE (enumtype)
19636 : && value
19637 11889012 : && TREE_CODE (value) == INTEGER_CST)
19638 : {
19639 4169384 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19640 9 : error ("enumerator value %qE is outside the range of underlying "
19641 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19642 :
19643 : /* Convert the value to the appropriate type. */
19644 4169384 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19645 : }
19646 : }
19647 :
19648 : /* C++ associates enums with global, function, or class declarations. */
19649 12352973 : context = current_scope ();
19650 :
19651 : /* Build the actual enumeration constant. Note that the enumeration
19652 : constants have the underlying type of the enum (if it is fixed)
19653 : or the type of their initializer (if the underlying type of the
19654 : enum is not fixed):
19655 :
19656 : [ C++0x dcl.enum ]
19657 :
19658 : If the underlying type is fixed, the type of each enumerator
19659 : prior to the closing brace is the underlying type; if the
19660 : initializing value of an enumerator cannot be represented by
19661 : the underlying type, the program is ill-formed. If the
19662 : underlying type is not fixed, the type of each enumerator is
19663 : the type of its initializing value.
19664 :
19665 : If the underlying type is not fixed, it will be computed by
19666 : finish_enum and we will reset the type of this enumerator. Of
19667 : course, if we're processing a template, there may be no value. */
19668 12352973 : type = value ? TREE_TYPE (value) : NULL_TREE;
19669 :
19670 12352973 : decl = build_decl (loc, CONST_DECL, name, type);
19671 :
19672 12352973 : DECL_CONTEXT (decl) = enumtype;
19673 12352973 : TREE_CONSTANT (decl) = 1;
19674 12352973 : TREE_READONLY (decl) = 1;
19675 12352973 : DECL_INITIAL (decl) = value;
19676 :
19677 12352973 : if (attributes)
19678 146073 : cplus_decl_attributes (&decl, attributes, 0);
19679 :
19680 12352973 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19681 : {
19682 : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19683 : on the TYPE_FIELDS list for `S'. (That's so that you can say
19684 : things like `S::i' later.) */
19685 :
19686 : /* The enumerator may be getting declared outside of its enclosing
19687 : class, like so:
19688 :
19689 : class S { public: enum E : int; }; enum S::E : int { i = 7; };
19690 :
19691 : For which case we need to make sure that the access of `S::i'
19692 : matches the access of `S::E'. */
19693 2151399 : auto cas = make_temp_override (current_access_specifier);
19694 2151399 : set_current_access_from_decl (TYPE_NAME (enumtype));
19695 2151399 : finish_member_declaration (decl);
19696 2151399 : }
19697 : else
19698 10201574 : pushdecl (decl);
19699 :
19700 : /* Add this enumeration constant to the list for this type. */
19701 12352973 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19702 :
19703 12352973 : return decl;
19704 : }
19705 :
19706 : /* Look for an enumerator with the given NAME within the enumeration
19707 : type ENUMTYPE. This routine is used primarily for qualified name
19708 : lookup into an enumerator in C++0x, e.g.,
19709 :
19710 : enum class Color { Red, Green, Blue };
19711 :
19712 : Color color = Color::Red;
19713 :
19714 : Returns the value corresponding to the enumerator, or
19715 : NULL_TREE if no such enumerator was found. */
19716 : tree
19717 28878674 : lookup_enumerator (tree enumtype, tree name)
19718 : {
19719 28878674 : tree e;
19720 28878674 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19721 :
19722 28878674 : e = purpose_member (name, TYPE_VALUES (enumtype));
19723 57757250 : return e? TREE_VALUE (e) : NULL_TREE;
19724 : }
19725 :
19726 : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19727 :
19728 : tree
19729 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
19730 : vec<string_int_pair> *values)
19731 : {
19732 0 : location_t saved_loc = input_location;
19733 0 : input_location = loc;
19734 :
19735 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19736 : NULL_TREE, false, NULL);
19737 0 : if (!OPAQUE_ENUM_P (enumtype))
19738 : {
19739 0 : auto_diagnostic_group d;
19740 0 : error_at (loc, "multiple definition of %q#T", enumtype);
19741 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19742 : "previous definition here");
19743 0 : return enumtype;
19744 0 : }
19745 0 : SET_OPAQUE_ENUM_P (enumtype, false);
19746 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19747 :
19748 0 : for (const string_int_pair &value : values)
19749 0 : build_enumerator (get_identifier (value.first),
19750 0 : build_int_cst (integer_type_node, value.second),
19751 : enumtype, NULL_TREE, loc);
19752 :
19753 0 : finish_enum_value_list (enumtype);
19754 0 : finish_enum (enumtype);
19755 :
19756 0 : input_location = saved_loc;
19757 0 : return enumtype;
19758 : }
19759 :
19760 : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19761 :
19762 : tree
19763 0 : cxx_simulate_record_decl (location_t loc, const char *name,
19764 : array_slice<const tree> fields)
19765 : {
19766 0 : iloc_sentinel ils (loc);
19767 :
19768 0 : tree ident = get_identifier (name);
19769 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
19770 0 : if (type != error_mark_node
19771 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19772 : {
19773 0 : error ("redefinition of %q#T", type);
19774 0 : type = error_mark_node;
19775 : }
19776 0 : if (type == error_mark_node)
19777 0 : return lhd_simulate_record_decl (loc, name, fields);
19778 :
19779 0 : xref_basetypes (type, NULL_TREE);
19780 0 : type = begin_class_definition (type);
19781 0 : if (type == error_mark_node)
19782 0 : return lhd_simulate_record_decl (loc, name, fields);
19783 :
19784 0 : for (tree field : fields)
19785 0 : finish_member_declaration (field);
19786 :
19787 0 : type = finish_struct (type, NULL_TREE);
19788 :
19789 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
19790 0 : set_underlying_type (decl);
19791 0 : lang_hooks.decls.pushdecl (decl);
19792 :
19793 0 : return type;
19794 0 : }
19795 :
19796 : /* We're defining DECL. Make sure that its type is OK. */
19797 :
19798 : static void
19799 168707840 : check_function_type (tree decl, tree current_function_parms)
19800 : {
19801 168707840 : tree fntype = TREE_TYPE (decl);
19802 168707840 : tree return_type = complete_type (TREE_TYPE (fntype));
19803 :
19804 : /* In a function definition, arg types must be complete. */
19805 168707840 : require_complete_types_for_parms (current_function_parms);
19806 :
19807 168707840 : if (dependent_type_p (return_type)
19808 168707840 : || type_uses_auto (return_type))
19809 : return;
19810 115333104 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19811 : {
19812 12 : tree args = TYPE_ARG_TYPES (fntype);
19813 :
19814 12 : error ("return type %q#T is incomplete", return_type);
19815 :
19816 : /* Make it return void instead. */
19817 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
19818 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19819 : void_type_node,
19820 3 : TREE_CHAIN (args));
19821 : else
19822 18 : fntype = build_function_type (void_type_node, args,
19823 9 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19824 12 : fntype = (cp_build_type_attribute_variant
19825 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19826 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19827 12 : TREE_TYPE (decl) = fntype;
19828 : }
19829 : else
19830 : {
19831 115333092 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
19832 115333092 : maybe_warn_parm_abi (TREE_TYPE (fntype),
19833 115333092 : DECL_SOURCE_LOCATION (decl));
19834 : }
19835 : }
19836 :
19837 : /* True iff FN is an implicitly-defined default constructor. */
19838 :
19839 : static bool
19840 17620009 : implicit_default_ctor_p (tree fn)
19841 : {
19842 17620009 : return (DECL_CONSTRUCTOR_P (fn)
19843 17620009 : && !user_provided_p (fn)
19844 19603746 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19845 : }
19846 :
19847 : /* Clobber the contents of *this to let the back end know that the object
19848 : storage is dead when we enter the constructor or leave the destructor. */
19849 :
19850 : static tree
19851 1540075 : build_clobber_this (clobber_kind kind)
19852 : {
19853 : /* Clobbering an empty base is pointless, and harmful if its one byte
19854 : TYPE_SIZE overlays real data. */
19855 1540075 : if (is_empty_class (current_class_type))
19856 0 : return void_node;
19857 :
19858 : /* If we have virtual bases, clobber the whole object, but only if we're in
19859 : charge. If we don't have virtual bases, clobber the as-base type so we
19860 : don't mess with tail padding. */
19861 1540075 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19862 :
19863 1540075 : tree ctype = current_class_type;
19864 1540075 : if (!vbases)
19865 1532227 : ctype = CLASSTYPE_AS_BASE (ctype);
19866 :
19867 1540075 : tree clobber = build_clobber (ctype, kind);
19868 :
19869 1540075 : tree thisref = current_class_ref;
19870 1540075 : if (ctype != current_class_type)
19871 : {
19872 176145 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19873 176145 : thisref = convert_from_reference (thisref);
19874 : }
19875 :
19876 1540075 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19877 1540075 : if (kind == CLOBBER_OBJECT_BEGIN)
19878 0 : TREE_SET_CODE (exprstmt, INIT_EXPR);
19879 1540075 : if (vbases)
19880 7848 : exprstmt = build_if_in_charge (exprstmt);
19881 :
19882 : return exprstmt;
19883 : }
19884 :
19885 : /* Create the FUNCTION_DECL for a function definition.
19886 : DECLSPECS and DECLARATOR are the parts of the declaration;
19887 : they describe the function's name and the type it returns,
19888 : but twisted together in a fashion that parallels the syntax of C.
19889 :
19890 : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19891 : DECLARATOR is really the DECL for the function we are about to
19892 : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19893 : indicating that the function is an inline defined in-class.
19894 :
19895 : This function creates a binding context for the function body
19896 : as well as setting up the FUNCTION_DECL in current_function_decl.
19897 :
19898 : For C++, we must first check whether that datum makes any sense.
19899 : For example, "class A local_a(1,2);" means that variable local_a
19900 : is an aggregate of type A, which should have a constructor
19901 : applied to it with the argument list [1, 2].
19902 :
19903 : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19904 : or may be a BLOCK if the function has been defined previously
19905 : in this translation unit. On exit, DECL_INITIAL (decl1) will be
19906 : error_mark_node if the function has never been defined, or
19907 : a BLOCK if the function has been defined somewhere. */
19908 :
19909 : bool
19910 168707876 : start_preparsed_function (tree decl1, tree attrs, int flags)
19911 : {
19912 168707876 : tree ctype = NULL_TREE;
19913 168707876 : bool doing_friend = false;
19914 :
19915 : /* Sanity check. */
19916 168707876 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19917 168707876 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19918 :
19919 168707876 : tree fntype = TREE_TYPE (decl1);
19920 168707876 : if (DECL_CLASS_SCOPE_P (decl1))
19921 124461272 : ctype = DECL_CONTEXT (decl1);
19922 : else
19923 : {
19924 88493208 : ctype = DECL_FRIEND_CONTEXT (decl1);
19925 :
19926 44246603 : if (ctype)
19927 168707876 : doing_friend = true;
19928 : }
19929 :
19930 : /* Adjust for #pragma target/optimize if this is an artificial function that
19931 : (probably) didn't go through grokfndecl. We particularly don't want this
19932 : for deferred instantiations, which should match their template. */
19933 168707876 : if (DECL_ARTIFICIAL (decl1))
19934 3963581 : decl_attributes (&decl1, NULL_TREE, 0);
19935 :
19936 168707876 : if (DECL_DECLARED_INLINE_P (decl1)
19937 168707876 : && lookup_attribute ("noinline", attrs))
19938 : {
19939 0 : auto_urlify_attributes sentinel;
19940 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
19941 : "inline function %qD given attribute %qs", decl1, "noinline");
19942 0 : }
19943 :
19944 : /* Handle gnu_inline attribute. */
19945 168707876 : if (GNU_INLINE_P (decl1))
19946 : {
19947 2014025 : DECL_EXTERNAL (decl1) = 1;
19948 2014025 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
19949 2014025 : DECL_INTERFACE_KNOWN (decl1) = 1;
19950 2014025 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
19951 : }
19952 :
19953 168707876 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
19954 : /* This is a constructor, we must ensure that any default args
19955 : introduced by this definition are propagated to the clones
19956 : now. The clones are used directly in overload resolution. */
19957 21018495 : adjust_clone_args (decl1);
19958 :
19959 : /* Sometimes we don't notice that a function is a static member, and
19960 : build a METHOD_TYPE for it. Fix that up now. */
19961 168707876 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
19962 : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
19963 :
19964 : /* Set up current_class_type, and enter the scope of the class, if
19965 : appropriate. */
19966 168707876 : if (ctype)
19967 129218818 : push_nested_class (ctype);
19968 :
19969 : /* Now that we have entered the scope of the class, we must restore
19970 : the bindings for any template parameters surrounding DECL1, if it
19971 : is an inline member template. (Order is important; consider the
19972 : case where a template parameter has the same name as a field of
19973 : the class.) It is not until after this point that
19974 : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19975 168707876 : if (flags & SF_INCLASS_INLINE)
19976 86590790 : maybe_begin_member_template_processing (decl1);
19977 :
19978 : /* Effective C++ rule 15. */
19979 168707876 : if (warn_ecpp
19980 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19981 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19982 168707906 : && VOID_TYPE_P (TREE_TYPE (fntype)))
19983 0 : warning (OPT_Weffc__,
19984 : "%<operator=%> should return a reference to %<*this%>");
19985 :
19986 : /* Make the init_value nonzero so pushdecl knows this is not tentative.
19987 : error_mark_node is replaced below (in poplevel) with the BLOCK. */
19988 168707876 : if (!DECL_INITIAL (decl1))
19989 36693713 : DECL_INITIAL (decl1) = error_mark_node;
19990 :
19991 : /* This function exists in static storage.
19992 : (This does not mean `static' in the C sense!) */
19993 168707876 : TREE_STATIC (decl1) = 1;
19994 :
19995 : /* We must call push_template_decl after current_class_type is set
19996 : up. (If we are processing inline definitions after exiting a
19997 : class scope, current_class_type will be NULL_TREE until set above
19998 : by push_nested_class.) */
19999 168707876 : if (processing_template_decl)
20000 : {
20001 98930008 : tree newdecl1 = push_template_decl (decl1, doing_friend);
20002 98930008 : if (newdecl1 == error_mark_node)
20003 : {
20004 36 : if (ctype)
20005 9 : pop_nested_class ();
20006 : return false;
20007 : }
20008 98929972 : decl1 = newdecl1;
20009 : }
20010 :
20011 : /* Make sure the parameter and return types are reasonable. When
20012 : you declare a function, these types can be incomplete, but they
20013 : must be complete when you define the function. */
20014 168707840 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
20015 :
20016 : /* Build the return declaration for the function. */
20017 168707840 : tree restype = TREE_TYPE (fntype);
20018 :
20019 168707840 : if (DECL_RESULT (decl1) == NULL_TREE)
20020 : {
20021 : /* In a template instantiation, copy the return type location. When
20022 : parsing, the location will be set in grokdeclarator. */
20023 69626009 : location_t loc = input_location;
20024 69626009 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
20025 : {
20026 33041452 : tree tmpl = template_for_substitution (decl1);
20027 33041452 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
20028 32081526 : loc = DECL_SOURCE_LOCATION (res);
20029 : }
20030 :
20031 69626009 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
20032 69626009 : DECL_ARTIFICIAL (resdecl) = 1;
20033 69626009 : DECL_IGNORED_P (resdecl) = 1;
20034 69626009 : DECL_RESULT (decl1) = resdecl;
20035 :
20036 69626009 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
20037 : }
20038 :
20039 : /* Record the decl so that the function name is defined.
20040 : If we already have a decl for this name, and it is a FUNCTION_DECL,
20041 : use the old decl. */
20042 168707840 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
20043 : {
20044 : /* A specialization is not used to guide overload resolution. */
20045 20884477 : if (!DECL_FUNCTION_MEMBER_P (decl1)
20046 20864504 : && !(DECL_USE_TEMPLATE (decl1) &&
20047 13701 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
20048 : {
20049 10001003 : tree olddecl = pushdecl (decl1);
20050 :
20051 10001003 : if (olddecl == error_mark_node)
20052 : /* If something went wrong when registering the declaration,
20053 : use DECL1; we have to have a FUNCTION_DECL to use when
20054 : parsing the body of the function. */
20055 : ;
20056 : else
20057 : {
20058 : /* Otherwise, OLDDECL is either a previous declaration
20059 : of the same function or DECL1 itself. */
20060 :
20061 10000790 : if (warn_missing_declarations
20062 21 : && olddecl == decl1
20063 15 : && !DECL_MAIN_P (decl1)
20064 15 : && TREE_PUBLIC (decl1)
20065 10000802 : && !DECL_DECLARED_INLINE_P (decl1))
20066 : {
20067 9 : tree context;
20068 :
20069 : /* Check whether DECL1 is in an anonymous
20070 : namespace. */
20071 9 : for (context = DECL_CONTEXT (decl1);
20072 18 : context;
20073 9 : context = DECL_CONTEXT (context))
20074 : {
20075 12 : if (TREE_CODE (context) == NAMESPACE_DECL
20076 12 : && DECL_NAME (context) == NULL_TREE)
20077 : break;
20078 : }
20079 :
20080 9 : if (context == NULL)
20081 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
20082 6 : OPT_Wmissing_declarations,
20083 : "no previous declaration for %qD", decl1);
20084 : }
20085 :
20086 10000790 : decl1 = olddecl;
20087 : }
20088 : }
20089 : else
20090 : {
20091 : /* We need to set the DECL_CONTEXT. */
20092 848797 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
20093 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
20094 : }
20095 10849800 : fntype = TREE_TYPE (decl1);
20096 10849800 : restype = TREE_TYPE (fntype);
20097 :
20098 : /* If #pragma weak applies, mark the decl appropriately now.
20099 : The pragma only applies to global functions. Because
20100 : determining whether or not the #pragma applies involves
20101 : computing the mangled name for the declaration, we cannot
20102 : apply the pragma until after we have merged this declaration
20103 : with any previous declarations; if the original declaration
20104 : has a linkage specification, that specification applies to
20105 : the definition as well, and may affect the mangled name. */
20106 10849800 : if (DECL_FILE_SCOPE_P (decl1))
20107 2579337 : maybe_apply_pragma_weak (decl1);
20108 : }
20109 :
20110 : /* We are now in the scope of the function being defined. */
20111 168707840 : current_function_decl = decl1;
20112 :
20113 : /* Save the parm names or decls from this function's declarator
20114 : where store_parm_decls will find them. */
20115 168707840 : tree current_function_parms = DECL_ARGUMENTS (decl1);
20116 :
20117 : /* Let the user know we're compiling this function. */
20118 168707840 : announce_function (decl1);
20119 :
20120 168707840 : gcc_assert (DECL_INITIAL (decl1));
20121 :
20122 : /* This function may already have been parsed, in which case just
20123 : return; our caller will skip over the body without parsing. */
20124 168707840 : if (DECL_INITIAL (decl1) != error_mark_node)
20125 : return true;
20126 :
20127 : /* Initialize RTL machinery. We cannot do this until
20128 : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
20129 : even when processing a template; this is how we get
20130 : CFUN set up, and our per-function variables initialized.
20131 : FIXME factor out the non-RTL stuff. */
20132 168707840 : cp_binding_level *bl = current_binding_level;
20133 168707840 : allocate_struct_function (decl1, processing_template_decl);
20134 :
20135 : /* Initialize the language data structures. Whenever we start
20136 : a new function, we destroy temporaries in the usual way. */
20137 168707840 : cfun->language = ggc_cleared_alloc<language_function> ();
20138 168707840 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
20139 168707840 : current_binding_level = bl;
20140 :
20141 : /* If we are (erroneously) defining a function that we have already
20142 : defined before, wipe out what we knew before. */
20143 168707840 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
20144 168707840 : FNDECL_USED_AUTO (decl1) = false;
20145 168707840 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
20146 :
20147 168707840 : if (!processing_template_decl && type_uses_auto (restype))
20148 : {
20149 1063802 : FNDECL_USED_AUTO (decl1) = true;
20150 1063802 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
20151 : }
20152 :
20153 : /* Start the statement-tree, start the tree now. */
20154 168707840 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
20155 :
20156 168707840 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
20157 : {
20158 : /* We know that this was set up by `grokclassfn'. We do not
20159 : wait until `store_parm_decls', since evil parse errors may
20160 : never get us to that point. Here we keep the consistency
20161 : between `current_class_type' and `current_class_ptr'. */
20162 109453468 : tree t = DECL_ARGUMENTS (decl1);
20163 :
20164 109453468 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
20165 109453468 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
20166 :
20167 109453468 : cp_function_chain->x_current_class_ref
20168 109453468 : = cp_build_fold_indirect_ref (t);
20169 : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
20170 109453468 : cp_function_chain->x_current_class_ptr = t;
20171 :
20172 : /* Constructors and destructors need to know whether they're "in
20173 : charge" of initializing virtual base classes. */
20174 109453468 : t = DECL_CHAIN (t);
20175 109453468 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
20176 : {
20177 18444 : current_in_charge_parm = t;
20178 18444 : t = DECL_CHAIN (t);
20179 : }
20180 109453468 : if (DECL_HAS_VTT_PARM_P (decl1))
20181 : {
20182 36947 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
20183 36947 : current_vtt_parm = t;
20184 : }
20185 : }
20186 :
20187 304355800 : bool honor_interface = (!DECL_TEMPLOID_INSTANTIATION (decl1)
20188 : /* Implicitly-defined methods (like the
20189 : destructor for a class in which no destructor
20190 : is explicitly declared) must not be defined
20191 : until their definition is needed. So, we
20192 : ignore interface specifications for
20193 : compiler-generated functions. */
20194 205358094 : && !DECL_ARTIFICIAL (decl1));
20195 168707840 : struct c_fileinfo *finfo
20196 168707840 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
20197 :
20198 168707840 : if (processing_template_decl)
20199 : /* Don't mess with interface flags. */;
20200 69777868 : else if (DECL_INTERFACE_KNOWN (decl1))
20201 : {
20202 20375843 : tree ctx = decl_function_context (decl1);
20203 :
20204 20375843 : if (DECL_NOT_REALLY_EXTERN (decl1))
20205 16224422 : DECL_EXTERNAL (decl1) = 0;
20206 :
20207 20375843 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
20208 : /* This is a function in a local class in an extern inline
20209 : or template function. */
20210 3209171 : comdat_linkage (decl1);
20211 : }
20212 : /* If this function belongs to an interface, it is public.
20213 : If it belongs to someone else's interface, it is also external.
20214 : This only affects inlines and template instantiations. */
20215 49402025 : else if (!finfo->interface_unknown && honor_interface)
20216 : {
20217 106 : if (DECL_DECLARED_INLINE_P (decl1)
20218 106 : || DECL_TEMPLOID_INSTANTIATION (decl1))
20219 : {
20220 82 : DECL_EXTERNAL (decl1)
20221 164 : = (finfo->interface_only
20222 82 : || (DECL_DECLARED_INLINE_P (decl1)
20223 36 : && ! flag_implement_inlines
20224 0 : && !DECL_VINDEX (decl1)));
20225 :
20226 : /* For WIN32 we also want to put these in linkonce sections. */
20227 82 : maybe_make_one_only (decl1);
20228 : }
20229 : else
20230 24 : DECL_EXTERNAL (decl1) = 0;
20231 106 : DECL_INTERFACE_KNOWN (decl1) = 1;
20232 : /* If this function is in an interface implemented in this file,
20233 : make sure that the back end knows to emit this function
20234 : here. */
20235 106 : if (!DECL_EXTERNAL (decl1))
20236 60 : mark_needed (decl1);
20237 : }
20238 49401919 : else if (finfo->interface_unknown && finfo->interface_only
20239 0 : && honor_interface)
20240 : {
20241 : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
20242 : interface, we will have both finfo->interface_unknown and
20243 : finfo->interface_only set. In that case, we don't want to
20244 : use the normal heuristics because someone will supply a
20245 : #pragma implementation elsewhere, and deducing it here would
20246 : produce a conflict. */
20247 0 : comdat_linkage (decl1);
20248 0 : DECL_EXTERNAL (decl1) = 0;
20249 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
20250 0 : DECL_DEFER_OUTPUT (decl1) = 1;
20251 : }
20252 : else
20253 : {
20254 : /* This is a definition, not a reference.
20255 : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
20256 49401919 : if (!GNU_INLINE_P (decl1))
20257 49401916 : DECL_EXTERNAL (decl1) = 0;
20258 :
20259 49401919 : if ((DECL_DECLARED_INLINE_P (decl1)
20260 1923755 : || DECL_TEMPLOID_INSTANTIATION (decl1))
20261 51082149 : && ! DECL_INTERFACE_KNOWN (decl1))
20262 49158394 : DECL_DEFER_OUTPUT (decl1) = 1;
20263 : else
20264 243525 : DECL_INTERFACE_KNOWN (decl1) = 1;
20265 : }
20266 :
20267 : /* Determine the ELF visibility attribute for the function. We must not
20268 : do this before calling "pushdecl", as we must allow "duplicate_decls"
20269 : to merge any attributes appropriately. We also need to wait until
20270 : linkage is set. */
20271 168707840 : if (!DECL_CLONED_FUNCTION_P (decl1))
20272 151970303 : determine_visibility (decl1);
20273 :
20274 168707840 : if (!processing_template_decl)
20275 69777868 : maybe_instantiate_noexcept (decl1);
20276 :
20277 168707840 : begin_scope (sk_function_parms, decl1);
20278 :
20279 168707840 : ++function_depth;
20280 :
20281 168707840 : start_fname_decls ();
20282 :
20283 168707840 : store_parm_decls (current_function_parms);
20284 :
20285 168707840 : start_function_contracts (decl1);
20286 :
20287 168707840 : if (!processing_template_decl
20288 69777868 : && flag_lifetime_dse > 1
20289 139546366 : && DECL_CONSTRUCTOR_P (decl1)
20290 : /* Clobbering an empty base is harmful if it overlays real data. */
20291 19543245 : && !is_empty_class (current_class_type)
20292 : /* We can't clobber safely for an implicitly-defined default constructor
20293 : because part of the initialization might happen before we enter the
20294 : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
20295 17619364 : && !implicit_default_ctor_p (decl1)
20296 185107779 : && !lookup_attribute ("clobber *this",
20297 16399939 : DECL_ATTRIBUTES (current_class_ptr)))
20298 16390407 : DECL_ATTRIBUTES (current_class_ptr)
20299 32780814 : = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
20300 16390407 : DECL_ATTRIBUTES (current_class_ptr));
20301 :
20302 168707840 : if (!processing_template_decl
20303 139555736 : && DECL_CONSTRUCTOR_P (decl1)
20304 19544487 : && sanitize_flags_p (SANITIZE_VPTR)
20305 1935 : && !DECL_CLONED_FUNCTION_P (decl1)
20306 168708485 : && !implicit_default_ctor_p (decl1))
20307 485 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
20308 :
20309 168707840 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
20310 168707267 : start_lambda_scope (decl1);
20311 :
20312 : return true;
20313 : }
20314 :
20315 :
20316 : /* Like start_preparsed_function, except that instead of a
20317 : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
20318 :
20319 : Returns true on success. If the DECLARATOR is not suitable
20320 : for a function, we return false, which tells the parser to
20321 : skip the entire function. */
20322 :
20323 : bool
20324 40454578 : start_function (cp_decl_specifier_seq *declspecs,
20325 : const cp_declarator *declarator,
20326 : tree attrs)
20327 : {
20328 40454578 : tree decl1;
20329 :
20330 40454578 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
20331 40454578 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
20332 40454578 : if (decl1 == error_mark_node)
20333 : return false;
20334 :
20335 40454212 : if (DECL_MAIN_P (decl1))
20336 : /* main must return int. grokfndecl should have corrected it
20337 : (and issued a diagnostic) if the user got it wrong. */
20338 34203 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
20339 : integer_type_node));
20340 :
20341 40454212 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
20342 : }
20343 :
20344 : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
20345 : FN. */
20346 :
20347 : bool
20348 323072033 : use_eh_spec_block (tree fn)
20349 : {
20350 321093297 : return (flag_exceptions && flag_enforce_eh_specs
20351 321093297 : && !processing_template_decl
20352 : /* We insert the EH_SPEC_BLOCK only in the original
20353 : function; then, it is copied automatically to the
20354 : clones. */
20355 121896527 : && !DECL_CLONED_FUNCTION_P (fn)
20356 : /* Implicitly-generated constructors and destructors have
20357 : exception specifications. However, those specifications
20358 : are the union of the possible exceptions specified by the
20359 : constructors/destructors for bases and members, so no
20360 : unallowed exception will ever reach this function. By
20361 : not creating the EH_SPEC_BLOCK we save a little memory,
20362 : and we avoid spurious warnings about unreachable
20363 : code. */
20364 105200767 : && !DECL_DEFAULTED_FN (fn)
20365 425685052 : && !type_throw_all_p (TREE_TYPE (fn)));
20366 : }
20367 :
20368 : /* Helper function to push ARGS into the current lexical scope. DECL
20369 : is the function declaration. NONPARMS is used to handle enum
20370 : constants. */
20371 :
20372 : void
20373 163712635 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
20374 : {
20375 : /* If we're doing semantic analysis, then we'll call pushdecl
20376 : for each of these. We must do them in reverse order so that
20377 : they end in the correct forward order. */
20378 163712635 : args = nreverse (args);
20379 :
20380 163712635 : tree next;
20381 665964270 : for (tree parm = args; parm; parm = next)
20382 : {
20383 338539000 : next = DECL_CHAIN (parm);
20384 338539000 : if (TREE_CODE (parm) == PARM_DECL)
20385 338539000 : pushdecl (parm);
20386 0 : else if (nonparms)
20387 : {
20388 : /* If we find an enum constant or a type tag, put it aside for
20389 : the moment. */
20390 0 : TREE_CHAIN (parm) = NULL_TREE;
20391 0 : *nonparms = chainon (*nonparms, parm);
20392 : }
20393 : }
20394 :
20395 : /* Get the decls in their original chain order and record in the
20396 : function. This is all and only the PARM_DECLs that were
20397 : pushed into scope by the loop above. */
20398 163712635 : DECL_ARGUMENTS (decl) = get_local_decls ();
20399 163712635 : }
20400 :
20401 : /* Store the parameter declarations into the current function declaration.
20402 : This is called after parsing the parameter declarations, before
20403 : digesting the body of the function.
20404 :
20405 : Also install to binding contour return value identifier, if any. */
20406 :
20407 : static void
20408 168707840 : store_parm_decls (tree current_function_parms)
20409 : {
20410 168707840 : tree fndecl = current_function_decl;
20411 :
20412 : /* This is a chain of any other decls that came in among the parm
20413 : declarations. If a parm is declared with enum {foo, bar} x;
20414 : then CONST_DECLs for foo and bar are put here. */
20415 168707840 : tree nonparms = NULL_TREE;
20416 :
20417 168707840 : if (current_function_parms)
20418 : {
20419 : /* This case is when the function was defined with an ANSI prototype.
20420 : The parms already have decls, so we need not do anything here
20421 : except record them as in effect
20422 : and complain if any redundant old-style parm decls were written. */
20423 :
20424 160052149 : tree specparms = current_function_parms;
20425 :
20426 : /* Must clear this because it might contain TYPE_DECLs declared
20427 : at class level. */
20428 160052149 : current_binding_level->names = NULL;
20429 :
20430 160052149 : do_push_parm_decls (fndecl, specparms, &nonparms);
20431 : }
20432 : else
20433 8655691 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
20434 :
20435 : /* Now store the final chain of decls for the arguments
20436 : as the decl-chain of the current lexical scope.
20437 : Put the enumerators in as well, at the front so that
20438 : DECL_ARGUMENTS is not modified. */
20439 168707840 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
20440 :
20441 : /* Register cleanups for parameters with trivial_abi attribute, the cleanup
20442 : of which is the callee's responsibility. */
20443 168707840 : if (!processing_template_decl && !DECL_CLONED_FUNCTION_P (fndecl))
20444 137088149 : for (tree parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
20445 : {
20446 84047818 : if (TREE_CODE (parm) == PARM_DECL)
20447 : {
20448 84047818 : tree parm_type = TREE_TYPE (parm);
20449 84047818 : if (has_trivial_abi_attribute (parm_type))
20450 : {
20451 18 : tree cleanup
20452 18 : = cxx_maybe_build_cleanup (parm, tf_warning_or_error);
20453 18 : if (cleanup && cleanup != error_mark_node)
20454 18 : finish_decl_cleanup (parm, cleanup);
20455 : }
20456 : }
20457 : }
20458 :
20459 168707840 : if (use_eh_spec_block (current_function_decl))
20460 25770384 : current_eh_spec_block = begin_eh_spec_block ();
20461 168707840 : }
20462 :
20463 :
20464 : /* Mark CDTOR's implicit THIS argument for returning, if required by
20465 : the ABI.. Return the decl for THIS, if it is to be returned, and
20466 : NULL otherwise. */
20467 :
20468 : tree
20469 144861805 : maybe_prepare_return_this (tree cdtor)
20470 : {
20471 144861805 : if (targetm.cxx.cdtor_returns_this ())
20472 0 : if (tree val = DECL_ARGUMENTS (cdtor))
20473 : {
20474 0 : suppress_warning (val, OPT_Wuse_after_free_);
20475 0 : return val;
20476 : }
20477 :
20478 : return NULL_TREE;
20479 : }
20480 :
20481 : /* Set the return value of the [cd]tor if the ABI wants that. */
20482 :
20483 : void
20484 8522075 : maybe_return_this ()
20485 : {
20486 8522075 : if (tree val = maybe_prepare_return_this (current_function_decl))
20487 : {
20488 : /* Return the address of the object. */
20489 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
20490 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
20491 0 : DECL_RESULT (current_function_decl), val);
20492 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
20493 0 : add_stmt (exprstmt);
20494 : }
20495 8522075 : }
20496 :
20497 : /* Do all the processing for the beginning of a destructor; set up the
20498 : vtable pointers and cleanups for bases and members. */
20499 :
20500 : static void
20501 1687035 : begin_destructor_body (void)
20502 : {
20503 1687035 : tree compound_stmt;
20504 :
20505 : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20506 : issued an error message. We still want to try to process the
20507 : body of the function, but initialize_vtbl_ptrs will crash if
20508 : TYPE_BINFO is NULL. */
20509 1687035 : if (COMPLETE_TYPE_P (current_class_type))
20510 : {
20511 1687035 : compound_stmt = begin_compound_stmt (0);
20512 : /* Make all virtual function table pointers in non-virtual base
20513 : classes point to CURRENT_CLASS_TYPE's virtual function
20514 : tables. */
20515 1687035 : initialize_vtbl_ptrs (current_class_ptr);
20516 1687035 : finish_compound_stmt (compound_stmt);
20517 :
20518 1687035 : if (flag_lifetime_dse
20519 : /* Clobbering an empty base is harmful if it overlays real data. */
20520 1687035 : && !is_empty_class (current_class_type))
20521 : {
20522 1540138 : if (sanitize_flags_p (SANITIZE_VPTR)
20523 239 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20524 1540201 : && TYPE_CONTAINS_VPTR_P (current_class_type))
20525 : {
20526 63 : tree binfo = TYPE_BINFO (current_class_type);
20527 63 : tree ref
20528 63 : = cp_build_fold_indirect_ref (current_class_ptr);
20529 :
20530 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20531 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20532 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20533 : NOP_EXPR, vtbl,
20534 : tf_warning_or_error);
20535 : /* If the vptr is shared with some virtual nearly empty base,
20536 : don't clear it if not in charge, the dtor of the virtual
20537 : nearly empty base will do that later. */
20538 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
20539 : {
20540 : tree c = current_class_type;
20541 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
20542 : {
20543 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20544 : {
20545 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
20546 : tf_warning_or_error);
20547 36 : stmt = build_if_in_charge (stmt);
20548 36 : break;
20549 : }
20550 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20551 : }
20552 : }
20553 63 : finish_decl_cleanup (NULL_TREE, stmt);
20554 : }
20555 : else
20556 1540075 : finish_decl_cleanup (NULL_TREE,
20557 : build_clobber_this (CLOBBER_OBJECT_END));
20558 : }
20559 :
20560 : /* And insert cleanups for our bases and members so that they
20561 : will be properly destroyed if we throw. */
20562 1687035 : push_base_cleanups ();
20563 : }
20564 1687035 : }
20565 :
20566 : /* Do the necessary processing for the beginning of a function body, which
20567 : in this case includes member-initializers, but not the catch clauses of
20568 : a function-try-block. Currently, this means opening a binding level
20569 : for the member-initializers (in a ctor), member cleanups (in a dtor),
20570 : and capture proxies (in a lambda operator()). */
20571 :
20572 : tree
20573 132675021 : begin_function_body (void)
20574 : {
20575 273171323 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20576 : return NULL_TREE;
20577 :
20578 25933791 : if (processing_template_decl)
20579 : /* Do nothing now. */;
20580 : else
20581 : /* Always keep the BLOCK node associated with the outermost pair of
20582 : curly braces of a function. These are needed for correct
20583 : operation of dwarfout.c. */
20584 8694141 : keep_next_level (true);
20585 :
20586 25933791 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
20587 25933791 : current_binding_level->artificial = 1;
20588 :
20589 25933791 : if (processing_template_decl)
20590 : /* Do nothing now. */;
20591 17388282 : else if (DECL_DESTRUCTOR_P (current_function_decl))
20592 1687035 : begin_destructor_body ();
20593 :
20594 : return stmt;
20595 : }
20596 :
20597 : /* Do the processing for the end of a function body. Currently, this means
20598 : closing out the cleanups for fully-constructed bases and members, and in
20599 : the case of the destructor, deleting the object if desired. Again, this
20600 : is only meaningful for [cd]tors, since they are the only functions where
20601 : there is a significant distinction between the main body and any
20602 : function catch clauses. Handling, say, main() return semantics here
20603 : would be wrong, as flowing off the end of a function catch clause for
20604 : main() would also need to return 0. */
20605 :
20606 : void
20607 132674997 : finish_function_body (tree compstmt)
20608 : {
20609 132674997 : if (compstmt == NULL_TREE)
20610 : return;
20611 :
20612 : /* Close the block. */
20613 25933791 : finish_compound_stmt (compstmt);
20614 :
20615 25933791 : if (processing_template_decl)
20616 : /* Do nothing now. */;
20617 8694141 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
20618 8694141 : || DECL_DESTRUCTOR_P (current_function_decl))
20619 8197278 : maybe_return_this ();
20620 : }
20621 :
20622 : /* Given a function, returns the BLOCK corresponding to the outermost level
20623 : of curly braces, skipping the artificial block created for constructor
20624 : initializers. */
20625 :
20626 : tree
20627 365068 : outer_curly_brace_block (tree fndecl)
20628 : {
20629 365068 : tree block = DECL_INITIAL (fndecl);
20630 365068 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20631 : return block;
20632 1213 : block = BLOCK_SUBBLOCKS (block);
20633 1213 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20634 : return block;
20635 0 : block = BLOCK_SUBBLOCKS (block);
20636 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20637 : return block;
20638 : }
20639 :
20640 : /* If FNDECL is a class's key method, add the class to the list of
20641 : keyed classes that should be emitted. */
20642 :
20643 : static void
20644 168711789 : record_key_method_defined (tree fndecl)
20645 : {
20646 227970086 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20647 109548908 : && DECL_VIRTUAL_P (fndecl)
20648 171591273 : && !processing_template_decl)
20649 : {
20650 1495726 : tree fnclass = DECL_CONTEXT (fndecl);
20651 1495726 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20652 : {
20653 30697 : tree classdecl = TYPE_NAME (fnclass);
20654 : /* Classes attached to a named module are already handled. */
20655 30697 : if (!DECL_LANG_SPECIFIC (classdecl)
20656 31020 : || !DECL_MODULE_ATTACH_P (classdecl))
20657 30652 : vec_safe_push (keyed_classes, fnclass);
20658 : }
20659 : }
20660 168711789 : }
20661 :
20662 : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20663 : of "return *this;" immediately before its location, using FNDECL's
20664 : first statement (if any) to give the indentation, if appropriate. */
20665 :
20666 : static void
20667 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20668 : {
20669 18 : location_t indent = UNKNOWN_LOCATION;
20670 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20671 18 : if (stmts)
20672 9 : indent = EXPR_LOCATION (stmts);
20673 18 : richloc->add_fixit_insert_formatted ("return *this;",
20674 : richloc->get_loc (),
20675 : indent);
20676 18 : }
20677 :
20678 : /* Finish up a function declaration and compile that function
20679 : all the way to assembler language output. The free the storage
20680 : for the function definition. INLINE_P is TRUE if we just
20681 : finished processing the body of an in-class inline function
20682 : definition. (This processing will have taken place after the
20683 : class definition is complete.) */
20684 :
20685 : tree
20686 168707804 : finish_function (bool inline_p)
20687 : {
20688 168707804 : tree fndecl = current_function_decl;
20689 168707804 : tree fntype, ctype = NULL_TREE;
20690 168707804 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
20691 :
20692 : /* When we get some parse errors, we can end up without a
20693 : current_function_decl, so cope. */
20694 168707804 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
20695 0 : return error_mark_node;
20696 :
20697 168707804 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20698 168707231 : finish_lambda_scope ();
20699 :
20700 168707804 : if (c_dialect_objc ())
20701 0 : objc_finish_function ();
20702 :
20703 168707804 : record_key_method_defined (fndecl);
20704 :
20705 168707804 : fntype = TREE_TYPE (fndecl);
20706 :
20707 : /* TREE_READONLY (fndecl) = 1;
20708 : This caused &foo to be of type ptr-to-const-function
20709 : which then got a warning when stored in a ptr-to-function variable. */
20710 :
20711 168707804 : gcc_assert (building_stmt_list_p ());
20712 : /* The current function is being defined, so its DECL_INITIAL should
20713 : be set, and unless there's a multiple definition, it should be
20714 : error_mark_node. */
20715 168707804 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20716 :
20717 168707804 : cp_coroutine_transform *coroutine = nullptr;
20718 168707804 : if (flag_coroutines
20719 165853305 : && !processing_template_decl
20720 67956685 : && DECL_COROUTINE_P (fndecl)
20721 168712766 : && !DECL_RAMP_FN (fndecl))
20722 : {
20723 1710 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20724 : && !DECL_DEFAULTED_FN (fndecl));
20725 1710 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
20726 1710 : if (coroutine && coroutine->cp_valid_coroutine ())
20727 1710 : coroutine->apply_transforms ();
20728 :
20729 : /* We should handle coroutine IFNs in middle end lowering. */
20730 1710 : cfun->coroutine_component = true;
20731 :
20732 : /* Do not try to process the ramp's EH unless outlining succeeded. */
20733 1710 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
20734 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20735 : (TREE_TYPE (fndecl)),
20736 360 : current_eh_spec_block);
20737 :
20738 : /* If outlining succeeded, then add contracts handling if needed. */
20739 1710 : if (coroutine->cp_valid_coroutine ())
20740 1626 : maybe_apply_function_contracts (fndecl);
20741 : }
20742 : else
20743 : /* For a cloned function, we've already got all the code we need;
20744 : there's no need to add any extra bits. */
20745 168706094 : if (!DECL_CLONED_FUNCTION_P (fndecl))
20746 : {
20747 : /* Make it so that `main' always returns 0 by default. */
20748 151968557 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20749 152002745 : && !TREE_THIS_VOLATILE (current_function_decl))
20750 34188 : finish_return_stmt (integer_zero_node);
20751 :
20752 151968557 : if (use_eh_spec_block (current_function_decl))
20753 25770021 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20754 : (TREE_TYPE (current_function_decl)),
20755 25770021 : current_eh_spec_block);
20756 :
20757 151968557 : maybe_apply_function_contracts (current_function_decl);
20758 :
20759 : }
20760 :
20761 : /* If we're saving up tree structure, tie off the function now. */
20762 168707804 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20763 :
20764 168707804 : finish_fname_decls ();
20765 :
20766 : /* This must come after expand_function_end because cleanups might
20767 : have declarations (from inline functions) that need to go into
20768 : this function's blocks. */
20769 :
20770 : /* If the current binding level isn't the outermost binding level
20771 : for this function, either there is a bug, or we have experienced
20772 : syntax errors and the statement tree is malformed. */
20773 168707804 : if (current_binding_level->kind != sk_function_parms)
20774 : {
20775 : /* Make sure we have already experienced errors. */
20776 12 : gcc_assert (errorcount);
20777 :
20778 : /* Throw away the broken statement tree and extra binding
20779 : levels. */
20780 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20781 :
20782 27 : while (current_binding_level->kind != sk_function_parms)
20783 : {
20784 15 : if (current_binding_level->kind == sk_class)
20785 0 : pop_nested_class ();
20786 : else
20787 15 : poplevel (0, 0, 0);
20788 : }
20789 : }
20790 168707804 : poplevel (1, 0, 1);
20791 :
20792 : /* Statements should always be full-expressions at the outermost set
20793 : of curly braces for a function. */
20794 168707804 : gcc_assert (stmts_are_full_exprs_p ());
20795 :
20796 : /* If there are no return statements in a function with auto return type,
20797 : the return type is void. But if the declared type is something like
20798 : auto*, this is an error. */
20799 69777832 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20800 169771606 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20801 : {
20802 147807 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20803 147792 : && !current_function_returns_value
20804 295362 : && !current_function_returns_null)
20805 : {
20806 : /* We haven't applied return type deduction because we haven't
20807 : seen any return statements. Do that now. */
20808 147555 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20809 147555 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20810 : void_node, node, tf_warning_or_error,
20811 : adc_return_type);
20812 :
20813 147555 : apply_deduced_return_type (fndecl, void_type_node);
20814 147555 : fntype = TREE_TYPE (fndecl);
20815 : }
20816 252 : else if (!current_function_returns_value
20817 6 : && !current_function_returns_null)
20818 : {
20819 6 : auto_diagnostic_group d;
20820 12 : error ("no return statements in function returning %qT",
20821 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20822 6 : inform (input_location, "only plain %<auto%> return type can be "
20823 : "deduced to %<void%>");
20824 6 : }
20825 : }
20826 :
20827 168707804 : if (FNDECL_USED_AUTO (fndecl)
20828 168707804 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20829 1063550 : if (location_t fcloc = failed_completion_location (fndecl))
20830 : {
20831 6 : auto_diagnostic_group adg;
20832 6 : if (warning (OPT_Wsfinae_incomplete_,
20833 : "defining %qD, which previously failed to be deduced "
20834 : "in a SFINAE context", fndecl)
20835 6 : && warn_sfinae_incomplete == 1)
20836 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
20837 : "-Wsfinae-incomplete=2");
20838 6 : }
20839 :
20840 : /* Remember that we were in class scope. */
20841 168707804 : if (current_class_name)
20842 129218806 : ctype = current_class_type;
20843 :
20844 168707804 : if (DECL_DELETED_FN (fndecl))
20845 : {
20846 93 : DECL_INITIAL (fndecl) = error_mark_node;
20847 93 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
20848 93 : goto cleanup;
20849 : }
20850 :
20851 168707711 : if (flag_openmp)
20852 325477 : if (tree attr = lookup_attribute ("omp declare variant base",
20853 325477 : DECL_ATTRIBUTES (fndecl)))
20854 267 : omp_declare_variant_finalize (fndecl, attr);
20855 :
20856 : /* Complain if there's just no return statement. */
20857 168707711 : if ((warn_return_type
20858 47248 : || (cxx_dialect >= cxx14
20859 39212 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20860 168678358 : && !VOID_TYPE_P (TREE_TYPE (fntype))
20861 105997921 : && !dependent_type_p (TREE_TYPE (fntype))
20862 53691940 : && !current_function_returns_value && !current_function_returns_null
20863 : /* Don't complain if we abort or throw. */
20864 37770 : && !current_function_returns_abnormally
20865 : /* Don't complain if there's an infinite loop. */
20866 953 : && !current_function_infinite_loop
20867 : /* Don't complain if we are declared noreturn. */
20868 885 : && !TREE_THIS_VOLATILE (fndecl)
20869 879 : && !DECL_NAME (DECL_RESULT (fndecl))
20870 879 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20871 : /* Structor return values (if any) are set by the compiler. */
20872 1270 : && !DECL_CONSTRUCTOR_P (fndecl)
20873 635 : && !DECL_DESTRUCTOR_P (fndecl)
20874 168708346 : && targetm.warn_func_return (fndecl))
20875 : {
20876 629 : gcc_rich_location richloc (input_location);
20877 : /* Potentially add a "return *this;" fix-it hint for
20878 : assignment operators. */
20879 629 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20880 : {
20881 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20882 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
20883 32 : && current_class_ref
20884 26 : && same_type_ignoring_top_level_qualifiers_p
20885 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20886 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
20887 18 : add_return_star_this_fixit (&richloc, fndecl);
20888 : }
20889 629 : if (cxx_dialect >= cxx14
20890 629 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
20891 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
20892 : "returning non-void");
20893 627 : else if (warning_at (&richloc, OPT_Wreturn_type,
20894 : "no return statement in function returning "
20895 : "non-void"))
20896 169 : suppress_warning (fndecl, OPT_Wreturn_type);
20897 629 : }
20898 :
20899 : /* Lambda closure members are implicitly constexpr if possible. */
20900 168707711 : if (cxx_dialect >= cxx17
20901 291876344 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20902 2025855 : DECL_DECLARED_CONSTEXPR_P (fndecl)
20903 2025855 : = ((processing_template_decl
20904 658239 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
20905 4050830 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20906 :
20907 : /* Invoke the pre-genericize plugin before we start munging things. */
20908 168707711 : if (!processing_template_decl)
20909 69777739 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
20910 :
20911 : /* Fold away non-ODR usages of constants so that we don't need to
20912 : try and stream them in modules if they're internal. */
20913 168707711 : if (!processing_template_decl)
20914 69777739 : cp_fold_function_non_odr_use (fndecl);
20915 :
20916 : /* Save constexpr function body before it gets munged by
20917 : the NRV transformation. */
20918 168707711 : maybe_save_constexpr_fundef (fndecl);
20919 :
20920 : /* Perform delayed folding before NRV transformation. */
20921 168707711 : if (!processing_template_decl
20922 139555478 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20923 237372441 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20924 68664287 : cp_fold_function (fndecl);
20925 :
20926 : /* Set up the named return value optimization, if we can. Candidate
20927 : variables are selected in check_return_expr. */
20928 168707711 : if (tree r = current_function_return_value)
20929 : {
20930 44369996 : if (r != error_mark_node)
20931 234031 : finalize_nrv (fndecl, r);
20932 44369996 : current_function_return_value = NULL_TREE;
20933 : }
20934 :
20935 : /* Must mark the RESULT_DECL as being in this function. */
20936 168707711 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
20937 :
20938 : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
20939 : to the FUNCTION_DECL node itself. */
20940 168707711 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
20941 :
20942 : /* Store the end of the function, so that we get good line number
20943 : info for the epilogue. */
20944 168707711 : cfun->function_end_locus = input_location;
20945 :
20946 : /* Complain about parameters that are only set, but never otherwise used. */
20947 168707711 : if (warn_unused_but_set_parameter
20948 1000076 : && !processing_template_decl
20949 511002 : && errorcount == unused_but_set_errorcount
20950 169218710 : && !DECL_CLONED_FUNCTION_P (fndecl))
20951 : {
20952 399256 : tree decl;
20953 :
20954 399256 : for (decl = DECL_ARGUMENTS (fndecl);
20955 1064262 : decl;
20956 665006 : decl = DECL_CHAIN (decl))
20957 665006 : if (TREE_USED (decl)
20958 552057 : && TREE_CODE (decl) == PARM_DECL
20959 552057 : && !DECL_READ_P (decl)
20960 225 : && DECL_NAME (decl)
20961 225 : && !DECL_ARTIFICIAL (decl)
20962 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
20963 225 : && !DECL_IN_SYSTEM_HEADER (decl)
20964 225 : && TREE_TYPE (decl) != error_mark_node
20965 225 : && !TYPE_REF_P (TREE_TYPE (decl))
20966 665231 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
20967 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
20968 225 : warning_at (DECL_SOURCE_LOCATION (decl),
20969 225 : OPT_Wunused_but_set_parameter_,
20970 : "parameter %qD set but not used", decl);
20971 399256 : unused_but_set_errorcount = errorcount;
20972 : }
20973 :
20974 : /* Complain about locally defined typedefs that are not used in this
20975 : function. */
20976 168707711 : maybe_warn_unused_local_typedefs ();
20977 :
20978 : /* Possibly warn about unused parameters. */
20979 168707711 : if (warn_unused_parameter
20980 730690 : && !processing_template_decl
20981 169061313 : && !DECL_CLONED_FUNCTION_P (fndecl))
20982 271709 : do_warn_unused_parameter (fndecl);
20983 :
20984 : /* Genericize before inlining. */
20985 168707711 : if (!processing_template_decl
20986 139555478 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20987 237367950 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20988 68659796 : cp_genericize (fndecl);
20989 :
20990 : /* If this function can't throw any exceptions, remember that. */
20991 168707711 : if (!processing_template_decl
20992 69777739 : && !cp_function_chain->can_throw
20993 53202357 : && !flag_non_call_exceptions
20994 53193589 : && !decl_replaceable_p (fndecl,
20995 53193589 : opt_for_fn (fndecl, flag_semantic_interposition))
20996 221651721 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
20997 52941189 : TREE_NOTHROW (fndecl) = 1;
20998 :
20999 168707804 : cleanup:
21000 :
21001 : /* We're leaving the context of this function, so zap cfun. It's still in
21002 : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
21003 168707804 : set_cfun (NULL);
21004 168707804 : current_function_decl = NULL;
21005 :
21006 : /* If this is an in-class inline definition, we may have to pop the
21007 : bindings for the template parameters that we added in
21008 : maybe_begin_member_template_processing when start_function was
21009 : called. */
21010 168707804 : if (inline_p)
21011 86591228 : maybe_end_member_template_processing ();
21012 :
21013 : /* Leave the scope of the class. */
21014 168707804 : if (ctype)
21015 129218806 : pop_nested_class ();
21016 :
21017 168707804 : --function_depth;
21018 :
21019 : /* Clean up. */
21020 168707804 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
21021 :
21022 : /* Build outlined functions for coroutines and contracts. */
21023 :
21024 168707804 : if (coroutine)
21025 : {
21026 : /* Emit the resumer and destroyer functions now, providing that we have
21027 : not encountered some fatal error. */
21028 1710 : if (coroutine->cp_valid_coroutine ())
21029 : {
21030 1626 : coroutine->finish_transforms ();
21031 1626 : resumer = coroutine->get_resumer ();
21032 1626 : destroyer = coroutine->get_destroyer ();
21033 1626 : expand_or_defer_fn (resumer);
21034 1626 : expand_or_defer_fn (destroyer);
21035 : }
21036 1710 : delete coroutine;
21037 : }
21038 :
21039 : /* If we have used outlined contracts checking functions, build and emit
21040 : them here. */
21041 168707804 : finish_function_outlined_contracts (fndecl);
21042 :
21043 168707804 : return fndecl;
21044 : }
21045 :
21046 : /* Create the FUNCTION_DECL for a function definition.
21047 : DECLSPECS and DECLARATOR are the parts of the declaration;
21048 : they describe the return type and the name of the function,
21049 : but twisted together in a fashion that parallels the syntax of C.
21050 :
21051 : This function creates a binding context for the function body
21052 : as well as setting up the FUNCTION_DECL in current_function_decl.
21053 :
21054 : Returns a FUNCTION_DECL on success.
21055 :
21056 : If the DECLARATOR is not suitable for a function (it defines a datum
21057 : instead), we return 0, which tells yyparse to report a parse error.
21058 :
21059 : May return void_type_node indicating that this method is actually
21060 : a friend. See grokfield for more details.
21061 :
21062 : Came here with a `.pushlevel' .
21063 :
21064 : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
21065 : CHANGES TO CODE IN `grokfield'. */
21066 :
21067 : tree
21068 86816479 : grokmethod (cp_decl_specifier_seq *declspecs,
21069 : const cp_declarator *declarator, tree attrlist)
21070 : {
21071 86816479 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
21072 86816479 : &attrlist);
21073 :
21074 86816479 : if (fndecl == error_mark_node)
21075 : return error_mark_node;
21076 :
21077 86816374 : if (attrlist)
21078 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
21079 :
21080 : /* Pass friends other than inline friend functions back. */
21081 86816374 : if (fndecl == void_type_node)
21082 : return fndecl;
21083 :
21084 86816374 : if (DECL_IN_AGGR_P (fndecl))
21085 : {
21086 0 : if (DECL_CLASS_SCOPE_P (fndecl))
21087 0 : error ("%qD is already defined in class %qT", fndecl,
21088 0 : DECL_CONTEXT (fndecl));
21089 0 : return error_mark_node;
21090 : }
21091 :
21092 86816374 : check_template_shadow (fndecl);
21093 :
21094 : /* p1779 ABI-Isolation makes inline not a default for in-class
21095 : definitions attached to a named module. If the user explicitly
21096 : made it inline, grokdeclarator will already have done the right
21097 : things. */
21098 86816374 : if ((!named_module_attach_p ()
21099 1169 : || flag_module_implicit_inline
21100 : /* Lambda's operator function remains inline. */
21101 2302 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
21102 : /* If the user explicitly asked for this to be inline, we don't
21103 : need to do more, but more importantly we want to warn if we
21104 : can't inline it. */
21105 86815911 : && !DECL_DECLARED_INLINE_P (fndecl))
21106 : {
21107 48823185 : if (TREE_PUBLIC (fndecl))
21108 46571975 : DECL_COMDAT (fndecl) = 1;
21109 48823185 : DECL_DECLARED_INLINE_P (fndecl) = 1;
21110 : /* It's ok if we can't inline this. */
21111 48823185 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
21112 : }
21113 :
21114 : /* We process method specializations in finish_struct_1. */
21115 86816374 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
21116 : {
21117 : /* Avoid calling decl_spec_seq... until we have to. */
21118 69657584 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
21119 69657584 : fndecl = push_template_decl (fndecl, friendp);
21120 69657584 : if (fndecl == error_mark_node)
21121 : return fndecl;
21122 : }
21123 :
21124 86816366 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
21125 : {
21126 0 : fndecl = copy_node (fndecl);
21127 0 : TREE_CHAIN (fndecl) = NULL_TREE;
21128 : }
21129 :
21130 86816366 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
21131 :
21132 86816366 : DECL_IN_AGGR_P (fndecl) = 1;
21133 86816366 : return fndecl;
21134 : }
21135 :
21136 :
21137 : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
21138 : we can lay it out later, when and if its type becomes complete.
21139 :
21140 : Also handle constexpr variables where the initializer involves
21141 : an unlowered PTRMEM_CST because the class isn't complete yet. */
21142 :
21143 : void
21144 107884540 : maybe_register_incomplete_var (tree var)
21145 : {
21146 107884540 : gcc_assert (VAR_P (var));
21147 :
21148 : /* Keep track of variables with incomplete types. */
21149 47441022 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
21150 155323787 : && DECL_EXTERNAL (var))
21151 : {
21152 6807583 : tree inner_type = TREE_TYPE (var);
21153 :
21154 6967669 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
21155 160086 : inner_type = TREE_TYPE (inner_type);
21156 6807583 : inner_type = TYPE_MAIN_VARIANT (inner_type);
21157 :
21158 6965205 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
21159 : /* RTTI TD entries are created while defining the type_info. */
21160 6807583 : || (TYPE_LANG_SPECIFIC (inner_type)
21161 2414871 : && TYPE_BEING_DEFINED (inner_type)))
21162 : {
21163 157622 : incomplete_var iv = {var, inner_type};
21164 157622 : vec_safe_push (incomplete_vars, iv);
21165 : }
21166 12995785 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
21167 3616182 : && DECL_CLASS_SCOPE_P (var)
21168 1128793 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
21169 1128793 : && decl_constant_var_p (var)
21170 7373395 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
21171 : {
21172 : /* When the outermost open class is complete we can resolve any
21173 : pointers-to-members. */
21174 29 : tree context = outermost_open_class ();
21175 29 : incomplete_var iv = {var, context};
21176 29 : vec_safe_push (incomplete_vars, iv);
21177 : }
21178 : }
21179 107884540 : }
21180 :
21181 : /* Called when a class type (given by TYPE) is defined. If there are
21182 : any existing VAR_DECLs whose type has been completed by this
21183 : declaration, update them now. */
21184 :
21185 : void
21186 73707495 : complete_vars (tree type)
21187 : {
21188 73707495 : unsigned ix;
21189 73707495 : incomplete_var *iv;
21190 :
21191 74381777 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
21192 : {
21193 674282 : if (same_type_p (type, iv->incomplete_type))
21194 : {
21195 156420 : tree var = iv->decl;
21196 156420 : tree type = TREE_TYPE (var);
21197 :
21198 156420 : if (type != error_mark_node
21199 156420 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
21200 156417 : == iv->incomplete_type))
21201 : {
21202 : /* Complete the type of the variable. */
21203 156388 : complete_type (type);
21204 156388 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
21205 156388 : if (COMPLETE_TYPE_P (type))
21206 156373 : layout_var_decl (var);
21207 : }
21208 :
21209 : /* Remove this entry from the list. */
21210 156420 : incomplete_vars->unordered_remove (ix);
21211 : }
21212 : else
21213 517862 : ix++;
21214 : }
21215 73707495 : }
21216 :
21217 : /* If DECL is of a type which needs a cleanup, build and return an
21218 : expression to perform that cleanup here. Return NULL_TREE if no
21219 : cleanup need be done. DECL can also be a _REF when called from
21220 : split_nonconstant_init_1. */
21221 :
21222 : tree
21223 92744165 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
21224 : {
21225 92744165 : tree type;
21226 92744165 : tree attr;
21227 92744165 : tree cleanup;
21228 :
21229 : /* Assume no cleanup is required. */
21230 92744165 : cleanup = NULL_TREE;
21231 :
21232 92744165 : if (!decl || error_operand_p (decl))
21233 : return cleanup;
21234 :
21235 : /* Handle "__attribute__((cleanup))". We run the cleanup function
21236 : before the destructor since the destructor is what actually
21237 : terminates the lifetime of the object. */
21238 92744165 : if (DECL_P (decl))
21239 92385299 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
21240 : else
21241 : attr = NULL_TREE;
21242 92385299 : if (attr)
21243 : {
21244 159 : tree id;
21245 159 : tree fn;
21246 159 : tree arg;
21247 :
21248 : /* Get the name specified by the user for the cleanup function. */
21249 159 : id = TREE_VALUE (TREE_VALUE (attr));
21250 : /* Look up the name to find the cleanup function to call. It is
21251 : important to use lookup_name here because that is what is
21252 : used in c-common.cc:handle_cleanup_attribute when performing
21253 : initial checks on the attribute. Note that those checks
21254 : include ensuring that the function found is not an overloaded
21255 : function, or an object with an overloaded call operator,
21256 : etc.; we can rely on the fact that the function found is an
21257 : ordinary FUNCTION_DECL. */
21258 159 : fn = lookup_name (id);
21259 159 : arg = build_address (decl);
21260 159 : if (!mark_used (decl, complain) && !(complain & tf_error))
21261 0 : return error_mark_node;
21262 159 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
21263 159 : if (cleanup == error_mark_node)
21264 : return error_mark_node;
21265 : }
21266 : /* Handle ordinary C++ destructors. */
21267 92744153 : type = TREE_TYPE (decl);
21268 92744153 : if (type_build_dtor_call (type))
21269 : {
21270 6999620 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
21271 6999620 : tree addr;
21272 6999620 : tree call;
21273 :
21274 6999620 : if (TREE_CODE (type) == ARRAY_TYPE)
21275 : addr = decl;
21276 : else
21277 6994374 : addr = build_address (decl);
21278 :
21279 6999620 : call = build_delete (input_location, TREE_TYPE (addr), addr,
21280 : sfk_complete_destructor, flags, 0, complain);
21281 6999620 : if (call == error_mark_node)
21282 : cleanup = error_mark_node;
21283 6999557 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
21284 : /* Discard the call. */;
21285 4290324 : else if (decl_maybe_constant_destruction (decl, type)
21286 4290524 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
21287 200 : cxx_constant_dtor (call, decl);
21288 4290124 : else if (cleanup)
21289 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
21290 : else
21291 : cleanup = call;
21292 : }
21293 :
21294 : /* build_delete sets the location of the destructor call to the
21295 : current location, even though the destructor is going to be
21296 : called later, at the end of the current scope. This can lead to
21297 : a "jumpy" behavior for users of debuggers when they step around
21298 : the end of the block. So let's unset the location of the
21299 : destructor call instead. */
21300 92744153 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
21301 92744153 : if (cleanup && CONVERT_EXPR_P (cleanup))
21302 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
21303 :
21304 0 : if (cleanup
21305 4290331 : && DECL_P (decl)
21306 4289869 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
21307 : /* Treat objects with destructors as used; the destructor may do
21308 : something substantive. */
21309 4289863 : && !mark_used (decl, complain) && !(complain & tf_error))
21310 0 : return error_mark_node;
21311 :
21312 4290331 : if (cleanup && cfun && !processing_template_decl
21313 96591385 : && !expr_noexcept_p (cleanup, tf_none))
21314 5875 : cp_function_chain->throwing_cleanup = true;
21315 :
21316 : return cleanup;
21317 : }
21318 :
21319 :
21320 : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
21321 : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
21322 : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
21323 :
21324 : tree
21325 6166525 : static_fn_type (tree memfntype)
21326 : {
21327 6166525 : tree fntype;
21328 6166525 : tree args;
21329 :
21330 6166525 : if (TYPE_PTRMEMFUNC_P (memfntype))
21331 20927 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
21332 6166525 : if (INDIRECT_TYPE_P (memfntype)
21333 5407601 : || TREE_CODE (memfntype) == FUNCTION_DECL)
21334 5993601 : memfntype = TREE_TYPE (memfntype);
21335 6166525 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
21336 : return memfntype;
21337 626104 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
21338 626104 : args = TYPE_ARG_TYPES (memfntype);
21339 626104 : fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
21340 626104 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
21341 626104 : fntype = (cp_build_type_attribute_variant
21342 626104 : (fntype, TYPE_ATTRIBUTES (memfntype)));
21343 626104 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
21344 626104 : return fntype;
21345 : }
21346 :
21347 : /* DECL was originally constructed as a non-static member function,
21348 : but turned out to be static. Update it accordingly. */
21349 :
21350 : void
21351 183128 : revert_static_member_fn (tree decl)
21352 : {
21353 183128 : tree stype = static_fn_type (decl);
21354 183128 : cp_cv_quals quals = type_memfn_quals (stype);
21355 183128 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
21356 :
21357 183128 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
21358 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
21359 :
21360 183128 : TREE_TYPE (decl) = stype;
21361 :
21362 183128 : if (DECL_ARGUMENTS (decl))
21363 183128 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
21364 183128 : DECL_STATIC_FUNCTION_P (decl) = 1;
21365 183128 : }
21366 :
21367 : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
21368 : one of the language-independent trees. */
21369 :
21370 : enum cp_tree_node_structure_enum
21371 39396865606 : cp_tree_node_structure (union lang_tree_node * t)
21372 : {
21373 39396865606 : switch (TREE_CODE (&t->generic))
21374 : {
21375 : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
21376 161696265 : case BASELINK: return TS_CP_BASELINK;
21377 84020842 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
21378 11783125 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
21379 6264403 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
21380 2126595823 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
21381 3479168 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
21382 276467 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
21383 733607099 : case OVERLOAD: return TS_CP_OVERLOAD;
21384 717 : case PTRMEM_CST: return TS_CP_PTRMEM;
21385 16442477 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
21386 693068125 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
21387 1613327226 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
21388 702020150 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
21389 15817705 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
21390 6 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
21391 3665267 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
21392 14888992 : case REQUIRES_EXPR: return TS_CP_REQUIRES_EXPR;
21393 33209911749 : default: return TS_CP_GENERIC;
21394 : }
21395 : }
21396 :
21397 : bool
21398 9119 : cp_missing_noreturn_ok_p (tree decl)
21399 : {
21400 : /* A missing noreturn is ok for the `main' function. */
21401 9119 : return DECL_MAIN_P (decl);
21402 : }
21403 :
21404 : /* Return the decl used to identify the COMDAT group into which DECL should
21405 : be placed. */
21406 :
21407 : tree
21408 130282680 : cxx_comdat_group (tree decl)
21409 : {
21410 : /* Virtual tables, construction virtual tables, and virtual table
21411 : tables all go in a single COMDAT group, named after the primary
21412 : virtual table. */
21413 130282680 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
21414 540026 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
21415 : /* For all other DECLs, the COMDAT group is the mangled name of the
21416 : declaration itself. */
21417 : else
21418 : {
21419 129742654 : while (DECL_THUNK_P (decl))
21420 : {
21421 : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
21422 : into the same section as the target function. In that case
21423 : we must return target's name. */
21424 0 : tree target = THUNK_TARGET (decl);
21425 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
21426 0 : && DECL_SECTION_NAME (target) != NULL
21427 0 : && DECL_ONE_ONLY (target))
21428 : decl = target;
21429 : else
21430 : break;
21431 : }
21432 : /* If a ctor/dtor has already set the comdat group by
21433 : maybe_clone_body, don't override it. */
21434 129742654 : if (SUPPORTS_ONE_ONLY
21435 129742654 : && TREE_CODE (decl) == FUNCTION_DECL
21436 129742654 : && DECL_CLONED_FUNCTION_P (decl))
21437 18832993 : if (tree comdat = DECL_COMDAT_GROUP (decl))
21438 2754228 : return comdat;
21439 : }
21440 :
21441 : return decl;
21442 : }
21443 :
21444 : /* Returns the return type for FN as written by the user, which may include
21445 : a placeholder for a deduced return type. */
21446 :
21447 : tree
21448 567285193 : fndecl_declared_return_type (tree fn)
21449 : {
21450 567285193 : fn = STRIP_TEMPLATE (fn);
21451 567285193 : if (FNDECL_USED_AUTO (fn))
21452 403087 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
21453 :
21454 566882106 : return TREE_TYPE (TREE_TYPE (fn));
21455 : }
21456 :
21457 : /* Returns true iff DECL is a variable or function declared with an auto type
21458 : that has not yet been deduced to a real type. */
21459 :
21460 : bool
21461 1024684443 : undeduced_auto_decl (tree decl)
21462 : {
21463 1024684443 : if (cxx_dialect < cxx11)
21464 : return false;
21465 1022057633 : STRIP_ANY_LOCATION_WRAPPER (decl);
21466 468279877 : return ((VAR_OR_FUNCTION_DECL_P (decl)
21467 97764564 : || TREE_CODE (decl) == TEMPLATE_DECL)
21468 1392801439 : && type_uses_auto (TREE_TYPE (decl)));
21469 : }
21470 :
21471 : /* Complain if DECL has an undeduced return type. */
21472 :
21473 : bool
21474 338161635 : require_deduced_type (tree decl, tsubst_flags_t complain)
21475 : {
21476 338161635 : if (undeduced_auto_decl (decl))
21477 : {
21478 15137 : if (TREE_CODE (decl) == FUNCTION_DECL
21479 15137 : && fndecl_built_in_p (decl, CP_BUILT_IN_SOURCE_LOCATION,
21480 : BUILT_IN_FRONTEND))
21481 : {
21482 : /* Set the return type of __builtin_source_location. */
21483 10316 : tree type = get_source_location_impl_type ();
21484 10316 : if (type == error_mark_node)
21485 : {
21486 33 : inform (input_location, "using %qs", "__builtin_source_location");
21487 33 : return false;
21488 : }
21489 10283 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
21490 10283 : type = build_pointer_type (type);
21491 10283 : apply_deduced_return_type (decl, type);
21492 10283 : return true;
21493 : }
21494 :
21495 4821 : if (TREE_CODE (decl) == FUNCTION_DECL
21496 4821 : && fndecl_built_in_p (decl, CP_BUILT_IN_CURRENT_EXCEPTION,
21497 : BUILT_IN_FRONTEND))
21498 : {
21499 : /* Set the return type of __builtin_current_exception. */
21500 2073 : tree name = get_identifier ("exception_ptr");
21501 2073 : tree eptr = lookup_qualified_name (std_node, name);
21502 2073 : tree fld;
21503 2073 : if (TREE_CODE (eptr) != TYPE_DECL
21504 2073 : || !CLASS_TYPE_P (TREE_TYPE (eptr))
21505 2073 : || !COMPLETE_TYPE_P (TREE_TYPE (eptr))
21506 2073 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (eptr))))
21507 2073 : || DECL_ARTIFICIAL (fld)
21508 2073 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
21509 2073 : || next_aggregate_field (DECL_CHAIN (fld))
21510 4146 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (eptr)),
21511 2073 : TYPE_SIZE (TREE_TYPE (fld))))
21512 : {
21513 0 : error ("%qs used without %qs declaration",
21514 : "__builtin_current_exception", "std::exception_ptr");
21515 0 : return false;
21516 : }
21517 :
21518 2073 : apply_deduced_return_type (decl, TREE_TYPE (eptr));
21519 2073 : return true;
21520 : }
21521 :
21522 2748 : if (warning_suppressed_p (decl) && seen_error ())
21523 : /* We probably already complained about deduction failure. */;
21524 2565 : else if (complain & tf_error)
21525 74 : error ("use of %qD before deduction of %<auto%>", decl);
21526 2748 : note_failed_type_completion (decl, complain);
21527 2748 : return false;
21528 : }
21529 : return true;
21530 : }
21531 :
21532 : /* Create a representation of the explicit-specifier with
21533 : constant-expression of EXPR. COMPLAIN is as for tsubst. */
21534 :
21535 : tree
21536 8542244 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
21537 : {
21538 8542244 : if (check_for_bare_parameter_packs (expr))
21539 3 : return error_mark_node;
21540 :
21541 8542241 : if (instantiation_dependent_expression_p (expr))
21542 : /* Wait for instantiation, tsubst_function_decl will handle it. */
21543 : return expr;
21544 :
21545 6857308 : expr = build_converted_constant_bool_expr (expr, complain);
21546 6857308 : expr = instantiate_non_dependent_expr (expr, complain);
21547 6857308 : expr = cxx_constant_value (expr, complain);
21548 6857308 : return expr;
21549 : }
21550 :
21551 : #include "gt-cp-decl.h"
|