Branch data Line data Source code
1 : : /* Process declarations and variables for -*- C++ -*- compiler.
2 : : Copyright (C) 1988-2025 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 const char *tag_name (enum tag_types);
89 : : static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
90 : : static void maybe_deduce_size_from_array_init (tree, tree);
91 : : static void layout_var_decl (tree);
92 : : static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
93 : : static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
94 : : static void copy_type_enum (tree , tree);
95 : : static void check_function_type (tree, tree);
96 : : static void begin_destructor_body (void);
97 : : static void record_key_method_defined (tree);
98 : : static tree create_array_type_for_decl (tree, tree, tree, location_t);
99 : : static tree get_atexit_node (void);
100 : : static tree get_dso_handle_node (void);
101 : : static tree start_cleanup_fn (tree, bool, bool);
102 : : static void end_cleanup_fn (void);
103 : : static tree cp_make_fname_decl (location_t, tree, int);
104 : : static void initialize_predefined_identifiers (void);
105 : : static tree check_special_function_return_type
106 : : (special_function_kind, tree, tree, int, const cp_declarator**,
107 : : const location_t*);
108 : : static tree push_cp_library_fn (enum tree_code, tree, int);
109 : : static tree build_cp_library_fn (tree, enum tree_code, tree, int);
110 : : static void store_parm_decls (tree);
111 : : static void initialize_local_var (tree, tree, bool);
112 : : static void expand_static_init (tree, tree);
113 : : static location_t smallest_type_location (const cp_decl_specifier_seq*);
114 : : static bool identify_goto (tree, location_t, const location_t *,
115 : : enum diagnostics::kind, bool);
116 : :
117 : : /* The following symbols are subsumed in the cp_global_trees array, and
118 : : listed here individually for documentation purposes.
119 : :
120 : : C++ extensions
121 : : tree wchar_decl_node;
122 : :
123 : : tree vtable_entry_type;
124 : : tree delta_type_node;
125 : : tree __t_desc_type_node;
126 : :
127 : : tree class_type_node;
128 : : tree unknown_type_node;
129 : :
130 : : Array type `vtable_entry_type[]'
131 : :
132 : : tree vtbl_type_node;
133 : : tree vtbl_ptr_type_node;
134 : :
135 : : Namespaces,
136 : :
137 : : tree std_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 : 232577477 : current_tmpl_spec_kind (int n_class_scopes)
312 : : {
313 : 232577477 : int n_template_parm_scopes = 0;
314 : 232577477 : int seen_specialization_p = 0;
315 : 232577477 : int innermost_specialization_p = 0;
316 : 232577477 : cp_binding_level *b;
317 : :
318 : : /* Scan through the template parameter scopes. */
319 : 232577477 : for (b = current_binding_level;
320 : 291007715 : b->kind == sk_template_parms;
321 : 58430238 : 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 : 58430238 : if (b->explicit_spec_p)
333 : : {
334 : 1077019 : if (n_template_parm_scopes == 0)
335 : : innermost_specialization_p = 1;
336 : : else
337 : 1166 : seen_specialization_p = 1;
338 : : }
339 : 57353219 : else if (seen_specialization_p == 1)
340 : : return tsk_invalid_member_spec;
341 : :
342 : 58430238 : ++n_template_parm_scopes;
343 : : }
344 : :
345 : : /* Handle explicit instantiations. */
346 : 232577477 : if (processing_explicit_instantiation)
347 : : {
348 : 2058660 : 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 : 2058660 : return tsk_expl_inst;
358 : : }
359 : :
360 : 230518817 : 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 : 230518817 : 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 : 50020002 : 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 : 99464910 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394 : : }
395 : :
396 : : /* Exit the current scope. */
397 : :
398 : : void
399 : 89760458 : finish_scope (void)
400 : : {
401 : 89760458 : poplevel (0, 0, 0);
402 : 89760458 : }
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 : 20382 : check_label_used (tree label)
409 : : {
410 : 20382 : if (!processing_template_decl)
411 : : {
412 : 20073 : 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 : 20064 : warn_for_unused_label (label);
424 : : }
425 : 20382 : }
426 : :
427 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
428 : :
429 : : static int
430 : 229276 : sort_labels (const void *a, const void *b)
431 : : {
432 : 229276 : tree label1 = *(tree const *) a;
433 : 229276 : tree label2 = *(tree const *) b;
434 : :
435 : : /* DECL_UIDs can never be equal. */
436 : 229276 : 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 : 147823502 : pop_labels (tree block)
508 : : {
509 : 147823502 : if (!named_labels)
510 : 147818919 : 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 : 4583 : auto_vec<tree, 32> labels (named_labels->elements ());
518 : 4583 : hash_table<named_label_hash>::iterator end (named_labels->end ());
519 : :
520 : 4583 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
521 : 670 : && !processing_template_decl)
522 : : {
523 : 5970 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
524 : : {
525 : 2349 : named_label_entry *ent = *iter;
526 : 2349 : 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 : 45281 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
537 : : {
538 : 20349 : named_label_entry *ent = *iter;
539 : :
540 : 20349 : gcc_checking_assert (!ent->outer);
541 : 20349 : if (ent->label_decl)
542 : 20163 : labels.quick_push (ent->label_decl);
543 : 20349 : ggc_free (ent);
544 : : }
545 : 4583 : named_labels = NULL;
546 : 4583 : labels.qsort (sort_labels);
547 : :
548 : 24746 : while (labels.length ())
549 : : {
550 : 20163 : tree label = labels.pop ();
551 : :
552 : 20163 : DECL_CHAIN (label) = BLOCK_VARS (block);
553 : 20163 : BLOCK_VARS (block) = label;
554 : :
555 : 20163 : check_label_used (label);
556 : : }
557 : 4583 : }
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 : 2102906 : level_for_constexpr_if (cp_binding_level *b)
618 : : {
619 : 2098905 : return (b->kind == sk_cond && b->this_entity
620 : 292 : && TREE_CODE (b->this_entity) == IF_STMT
621 : 2103198 : && 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 : 2102882 : level_for_consteval_if (cp_binding_level *b)
628 : : {
629 : 2098881 : return (b->kind == sk_cond && b->this_entity
630 : 268 : && TREE_CODE (b->this_entity) == IF_STMT
631 : 2103150 : && 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 : 153160 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
652 : : {
653 : 153160 : named_label_entry *ent = *slot;
654 : 153160 : cp_binding_level *obl = bl->level_chain;
655 : :
656 : 153160 : if (ent->binding_level == bl)
657 : : {
658 : 10048 : tree decl;
659 : :
660 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
661 : : TREE_LISTs representing OVERLOADs, so be careful. */
662 : 14935 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
663 : 4960 : ? DECL_CHAIN (decl)
664 : 73 : : TREE_CHAIN (decl)))
665 : 4887 : if (decl_jump_unsafe (decl))
666 : : {
667 : 1862 : vec_safe_push (ent->bad_decls, decl);
668 : 1862 : ent->has_bad_decls = true;
669 : : }
670 : 3025 : else if (decl_instrument_init_bypass_p (decl))
671 : 432 : vec_safe_push (ent->bad_decls, decl);
672 : :
673 : 10048 : ent->binding_level = obl;
674 : 10048 : ent->names_in_scope = obl->names;
675 : 10048 : 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 : 4045 : case sk_block:
693 : 4045 : if (level_for_constexpr_if (obl))
694 : 15 : ent->in_constexpr_if = true;
695 : 4030 : else if (level_for_consteval_if (obl))
696 : 30 : ent->in_consteval_if = true;
697 : : break;
698 : : default:
699 : : break;
700 : : }
701 : : }
702 : 143112 : else if (ent->uses)
703 : : {
704 : : struct named_label_use_entry *use;
705 : :
706 : 22833 : for (use = ent->uses; use ; use = use->next)
707 : 15521 : if (use->binding_level == bl)
708 : : {
709 : 5981 : if (auto &cg = use->computed_goto)
710 : : {
711 : 57 : if (bl->kind == sk_catch)
712 : 3 : vec_safe_push (cg, get_identifier ("catch"));
713 : 66 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
714 : 9 : if (automatic_var_with_nontrivial_dtor_p (d))
715 : 6 : vec_safe_push (cg, d);
716 : : }
717 : :
718 : 5981 : use->binding_level = obl;
719 : 5981 : use->names_in_scope = obl->names;
720 : 5981 : if (bl->kind == sk_omp)
721 : 12 : use->in_omp_scope = true;
722 : : }
723 : : }
724 : :
725 : 153160 : 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 : 599800101 : poplevel (int keep, int reverse, int functionbody)
749 : : {
750 : 599800101 : tree link;
751 : : /* The chain of decls was accumulated in reverse order.
752 : : Put it into forward order, just for cleanliness. */
753 : 599800101 : tree decls;
754 : 599800101 : tree subblocks;
755 : 599800101 : tree block;
756 : 599800101 : tree decl;
757 : 599800101 : scope_kind kind;
758 : :
759 : 599800101 : auto_cond_timevar tv (TV_NAME_LOOKUP);
760 : 599800425 : restart:
761 : :
762 : 599800425 : block = NULL_TREE;
763 : :
764 : 599800425 : gcc_assert (current_binding_level->kind != sk_class
765 : : && current_binding_level->kind != sk_namespace);
766 : :
767 : 599800425 : if (current_binding_level->kind == sk_cleanup)
768 : : functionbody = 0;
769 : 599800425 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
770 : :
771 : 599800425 : 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 : 599800425 : gcc_assert (keep == 0 || keep == 1);
777 : :
778 : 599800425 : if (current_binding_level->keep)
779 : 7658812 : keep = 1;
780 : :
781 : : /* Any uses of undefined labels, and any defined labels, now operate
782 : : under constraints of next binding contour. */
783 : 599800425 : if (cfun && !functionbody && named_labels)
784 : 43153 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
785 : 196313 : (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 : 599800425 : decls = current_binding_level->names;
792 : 599800425 : if (reverse)
793 : : {
794 : 324089477 : decls = nreverse (decls);
795 : 324089477 : 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 : 599800425 : block = NULL_TREE;
802 : : /* Avoid function body block if possible. */
803 : 636648364 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
804 : : keep = 0;
805 : 562952747 : else if (keep == 1 || functionbody)
806 : 184615902 : block = make_node (BLOCK);
807 : 184615902 : if (block != NULL_TREE)
808 : : {
809 : 184615902 : BLOCK_VARS (block) = decls;
810 : 184615902 : BLOCK_SUBBLOCKS (block) = subblocks;
811 : : }
812 : :
813 : : /* In each subblock, record that this is its superior. */
814 : 599800425 : if (keep >= 0)
815 : 673439787 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
816 : 73639362 : BLOCK_SUPERCONTEXT (link) = block;
817 : :
818 : : /* Before we remove the declarations first check for unused variables. */
819 : 599800425 : if ((warn_unused_variable || warn_unused_but_set_variable)
820 : 6506010 : && current_binding_level->kind != sk_template_parms
821 : 5492666 : && !processing_template_decl)
822 : 6366019 : 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 : 3237674 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
828 : 3237674 : decl = strip_using_decl (udecl);
829 : :
830 : 3237674 : tree type = TREE_TYPE (decl);
831 : 3237674 : if (VAR_P (decl)
832 : 379124 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
833 : 3411 : && !DECL_IN_SYSTEM_HEADER (udecl)
834 : : /* For structured bindings, consider only real variables, not
835 : : subobjects. */
836 : 3399 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
837 : 3147 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
838 : : /* Don't warn about name-independent declarations. */
839 : 887 : && !name_independent_decl_p (decl)
840 : 731 : && type != error_mark_node
841 : 3241770 : && (!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 : 697 : if (!TREE_USED (decl))
847 : : {
848 : 302 : 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 : 299 : 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 : 296 : warning_at (DECL_SOURCE_LOCATION (decl),
858 : 296 : OPT_Wunused_variable, "unused variable %qD", decl);
859 : 302 : suppress_warning (decl, OPT_Wunused_variable);
860 : : }
861 : 395 : else if (DECL_CONTEXT (decl) == current_function_decl
862 : : // For -Wunused-but-set-variable leave references alone.
863 : 371 : && !TYPE_REF_P (TREE_TYPE (decl))
864 : 766 : && errorcount == unused_but_set_errorcount)
865 : : {
866 : 362 : 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 : 350 : warning_at (DECL_SOURCE_LOCATION (decl),
872 : 350 : OPT_Wunused_but_set_variable_,
873 : : "variable %qD set but not used", decl);
874 : 362 : unused_but_set_errorcount = errorcount;
875 : : }
876 : : }
877 : : }
878 : :
879 : : /* Remove declarations for all the DECLs in this level. */
880 : 1218617100 : for (link = decls; link; link = TREE_CHAIN (link))
881 : : {
882 : 618816675 : tree name;
883 : 618816675 : if (TREE_CODE (link) == TREE_LIST)
884 : : {
885 : 4340057 : decl = TREE_VALUE (link);
886 : 4340057 : name = TREE_PURPOSE (link);
887 : 4340057 : gcc_checking_assert (name);
888 : : }
889 : : else
890 : : {
891 : 614476618 : decl = link;
892 : 614476618 : name = DECL_NAME (decl);
893 : : }
894 : :
895 : : /* Remove the binding. */
896 : 618816675 : if (TREE_CODE (decl) == LABEL_DECL)
897 : 219 : pop_local_label (name, decl);
898 : : else
899 : 618816456 : pop_local_binding (name, decl);
900 : : }
901 : :
902 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
903 : 599800425 : for (link = current_binding_level->type_shadowed;
904 : 814847548 : link; link = TREE_CHAIN (link))
905 : 215047123 : 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 : 599800425 : if (block)
915 : : {
916 : 184615902 : tree* d;
917 : :
918 : 484147321 : for (d = &BLOCK_VARS (block); *d; )
919 : : {
920 : 299531419 : if (TREE_CODE (*d) == TREE_LIST
921 : 299531419 : || (!processing_template_decl
922 : 96611835 : && undeduced_auto_decl (*d)))
923 : 4340109 : *d = TREE_CHAIN (*d);
924 : : else
925 : 295191310 : 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 : 599800425 : if (functionbody)
932 : : {
933 : 147823502 : 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 : 110975824 : BLOCK_VARS (block) = 0;
939 : 110975824 : pop_labels (block);
940 : : }
941 : : else
942 : 36847678 : pop_labels (subblocks);
943 : : }
944 : :
945 : 599800425 : kind = current_binding_level->kind;
946 : 599800425 : if (kind == sk_cleanup)
947 : : {
948 : 324 : 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 : 324 : stmt = pop_stmt_list (current_binding_level->statement_list);
954 : 324 : stmt = c_build_bind_expr (input_location, block, stmt);
955 : 324 : add_stmt (stmt);
956 : : }
957 : :
958 : 599800425 : leave_scope ();
959 : 599800425 : if (functionbody)
960 : : {
961 : : /* The current function is being defined, so its DECL_INITIAL
962 : : should be error_mark_node. */
963 : 147823502 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
964 : 184671180 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
965 : 147823502 : if (subblocks)
966 : : {
967 : 69500396 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
968 : 45681781 : && BLOCK_SUBBLOCKS (subblocks))
969 : 1314896 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
970 : : else
971 : 35533043 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
972 : : }
973 : : }
974 : 451976923 : else if (block)
975 : 73640078 : current_binding_level->blocks
976 : 147280156 : = 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 : 378336845 : 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 : 599800425 : if (block)
991 : 184615902 : TREE_USED (block) = 1;
992 : :
993 : : /* All temporary bindings created for cleanups are popped silently. */
994 : 599800425 : if (kind == sk_cleanup)
995 : 324 : goto restart;
996 : :
997 : 1199600202 : return block;
998 : 599800101 : }
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 : 142435 : wrapup_namespace_globals ()
1006 : : {
1007 : 142435 : if (vec<tree, va_gc> *statics = static_decls)
1008 : : {
1009 : 15675325 : for (tree decl : *statics)
1010 : : {
1011 : 15558438 : if (warn_unused_function
1012 : 990193 : && TREE_CODE (decl) == FUNCTION_DECL
1013 : 742670 : && DECL_INITIAL (decl) == 0
1014 : 7244 : && DECL_EXTERNAL (decl)
1015 : 7244 : && !TREE_PUBLIC (decl)
1016 : 29 : && !DECL_ARTIFICIAL (decl)
1017 : 29 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1018 : 15558444 : && !warning_suppressed_p (decl, OPT_Wunused_function))
1019 : 6 : warning_at (DECL_SOURCE_LOCATION (decl),
1020 : 6 : OPT_Wunused_function,
1021 : : "%qF declared %<static%> but never defined", decl);
1022 : :
1023 : 15558438 : if (VAR_P (decl)
1024 : 4538824 : && DECL_EXTERNAL (decl)
1025 : 2016007 : && DECL_INLINE_VAR_P (decl)
1026 : 15558456 : && DECL_ODR_USED (decl))
1027 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
1028 : : "odr-used inline variable %qD is not defined", decl);
1029 : : }
1030 : :
1031 : : /* Clear out the list, so we don't rescan next time. */
1032 : 116887 : static_decls = NULL;
1033 : :
1034 : : /* Write out any globals that need to be output. */
1035 : 116887 : return wrapup_global_declarations (statics->address (),
1036 : 116887 : statics->length ());
1037 : : }
1038 : : return 0;
1039 : : }
1040 : :
1041 : : /* In C++, you don't have to write `struct S' to refer to `S'; you
1042 : : can just use `S'. We accomplish this by creating a TYPE_DECL as
1043 : : if the user had written `typedef struct S S'. Create and return
1044 : : the TYPE_DECL for TYPE. */
1045 : :
1046 : : tree
1047 : 160094930 : create_implicit_typedef (tree name, tree type)
1048 : : {
1049 : 160094930 : tree decl;
1050 : :
1051 : 160094930 : decl = build_decl (input_location, TYPE_DECL, name, type);
1052 : 160094930 : DECL_ARTIFICIAL (decl) = 1;
1053 : : /* There are other implicit type declarations, like the one *within*
1054 : : a class that allows you to write `S::S'. We must distinguish
1055 : : amongst these. */
1056 : 160094930 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1057 : 160094930 : TYPE_NAME (type) = decl;
1058 : 160094930 : TYPE_STUB_DECL (type) = decl;
1059 : :
1060 : 160094930 : return decl;
1061 : : }
1062 : :
1063 : : /* Function-scope local entities that need discriminators. Each entry
1064 : : is a {decl,name} pair. VAR_DECLs for anon unions get their name
1065 : : smashed, so we cannot rely on DECL_NAME. */
1066 : :
1067 : : static GTY((deletable)) vec<tree, va_gc> *local_entities;
1068 : :
1069 : : /* Determine the mangling discriminator of local DECL. There are
1070 : : generally very few of these in any particular function. */
1071 : :
1072 : : void
1073 : 731081 : determine_local_discriminator (tree decl, tree name)
1074 : : {
1075 : 731081 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1076 : 731081 : retrofit_lang_decl (decl);
1077 : 731081 : tree ctx = DECL_CONTEXT (decl);
1078 : 731081 : size_t nelts = vec_safe_length (local_entities);
1079 : 731081 : if (name == NULL_TREE)
1080 : 730822 : name = (TREE_CODE (decl) == TYPE_DECL
1081 : 1177368 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
1082 : 1315495 : ? NULL_TREE : DECL_NAME (decl));
1083 : 22024576 : for (size_t i = 0; i < nelts; i += 2)
1084 : : {
1085 : 21293960 : tree *pair = &(*local_entities)[i];
1086 : 21293960 : tree d = pair[0];
1087 : 21293960 : tree n = pair[1];
1088 : 21293960 : gcc_checking_assert (d != decl);
1089 : 21293960 : if (name == n
1090 : 3799608 : && TREE_CODE (decl) == TREE_CODE (d)
1091 : 25093559 : && ctx == DECL_CONTEXT (d))
1092 : : {
1093 : 465 : tree disc = integer_one_node;
1094 : 465 : if (DECL_DISCRIMINATOR (d))
1095 : 244 : disc = build_int_cst (TREE_TYPE (disc),
1096 : 244 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
1097 : 465 : DECL_DISCRIMINATOR (decl) = disc;
1098 : : /* Replace the saved decl. */
1099 : 465 : pair[0] = decl;
1100 : 465 : decl = NULL_TREE;
1101 : 465 : break;
1102 : : }
1103 : : }
1104 : :
1105 : 731081 : if (decl)
1106 : : {
1107 : 730616 : vec_safe_reserve (local_entities, 2);
1108 : 730616 : local_entities->quick_push (decl);
1109 : 730616 : local_entities->quick_push (name);
1110 : : }
1111 : 731081 : }
1112 : :
1113 : :
1114 : : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
1115 : :
1116 : : A non-template friend declaration with a requires-clause shall be a
1117 : : definition. A friend function template with a constraint that depends on a
1118 : : template parameter from an enclosing template shall be a definition. Such a
1119 : : constrained friend function or function template declaration does not
1120 : : declare the same function or function template as a declaration in any other
1121 : : scope.
1122 : :
1123 : : The ABI calls this a "member-like constrained friend" and mangles it like a
1124 : : member function to avoid collisions. */
1125 : :
1126 : : bool
1127 : 2072807507 : member_like_constrained_friend_p (tree decl)
1128 : : {
1129 : 2072807507 : return (TREE_CODE (decl) == FUNCTION_DECL
1130 : 627905280 : && DECL_UNIQUE_FRIEND_P (decl)
1131 : 91128598 : && DECL_FRIEND_CONTEXT (decl)
1132 : 40385333 : && get_constraints (decl)
1133 : 2073453182 : && (!DECL_TEMPLATE_INFO (decl)
1134 : 645675 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1135 : 5897 : || (uses_outer_template_parms_in_constraints
1136 : 5897 : (most_general_template (decl)))));
1137 : : }
1138 : :
1139 : : /* Returns true if functions FN1 and FN2 have equivalent trailing
1140 : : requires clauses. */
1141 : :
1142 : : static bool
1143 : 29450500 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1144 : : {
1145 : : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1146 : : same function as a declaration in any other scope." So no need to
1147 : : actually compare the requirements. */
1148 : 29450500 : if (member_like_constrained_friend_p (newfn)
1149 : 29450500 : || member_like_constrained_friend_p (oldfn))
1150 : 117 : return false;
1151 : :
1152 : : /* Compare only trailing requirements. */
1153 : 29450383 : tree reqs1 = get_trailing_function_requirements (newfn);
1154 : 29450383 : tree reqs2 = get_trailing_function_requirements (oldfn);
1155 : 29450383 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1156 : : return false;
1157 : :
1158 : : /* Substitution is needed when friends are involved. */
1159 : 29389525 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1160 : 29389525 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1161 : :
1162 : 29389525 : return cp_tree_equal (reqs1, reqs2);
1163 : : }
1164 : :
1165 : : /* Two functions of the same name correspond [basic.scope.scope] if
1166 : :
1167 : : + both declare functions with the same non-object-parameter-type-list,
1168 : : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1169 : : specified in [temp.friend]), and, if both are non-static members, they have
1170 : : corresponding object parameters, or
1171 : :
1172 : : + both declare function templates with equivalent
1173 : : non-object-parameter-type-lists, return types (if any), template-heads, and
1174 : : trailing requires-clauses (if any), and, if both are non-static members,
1175 : : they have corresponding object parameters.
1176 : :
1177 : : This is a subset of decls_match: it identifies declarations that cannot be
1178 : : overloaded with one another. This function does not consider DECL_NAME. */
1179 : :
1180 : : bool
1181 : 773609836 : fns_correspond (tree newdecl, tree olddecl)
1182 : : {
1183 : 773609836 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1184 : : return false;
1185 : :
1186 : 715754219 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1187 : : {
1188 : 715720844 : if (!template_heads_equivalent_p (newdecl, olddecl))
1189 : : return 0;
1190 : 166228914 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1191 : 166228914 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1192 : : }
1193 : :
1194 : 166262289 : tree f1 = TREE_TYPE (newdecl);
1195 : 166262289 : tree f2 = TREE_TYPE (olddecl);
1196 : :
1197 : 166262289 : int rq1 = type_memfn_rqual (f1);
1198 : 166262289 : int rq2 = type_memfn_rqual (f2);
1199 : :
1200 : : /* If only one is a non-static member function, ignore ref-quals. */
1201 : 166262289 : if (TREE_CODE (f1) != TREE_CODE (f2))
1202 : : rq1 = rq2;
1203 : : /* Two non-static member functions have corresponding object parameters if:
1204 : : + exactly one is an implicit object member function with no ref-qualifier
1205 : : and the types of their object parameters ([dcl.fct]), after removing
1206 : : top-level references, are the same, or
1207 : : + their object parameters have the same type. */
1208 : : /* ??? We treat member functions of different classes as corresponding even
1209 : : though that means the object parameters have different types. */
1210 : 166262289 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1211 : 0 : rq1 = rq2;
1212 : :
1213 : 166262289 : bool types_match = rq1 == rq2;
1214 : :
1215 : 166262289 : if (types_match)
1216 : : {
1217 : 166262289 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1218 : 166262289 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1219 : 166262289 : types_match = compparms (p1, p2);
1220 : : }
1221 : :
1222 : : /* Two function declarations match if either has a requires-clause
1223 : : then both have a requires-clause and their constraints-expressions
1224 : : are equivalent. */
1225 : 166262289 : if (types_match && flag_concepts)
1226 : 20489661 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1227 : :
1228 : : return types_match;
1229 : : }
1230 : :
1231 : : /* Subroutine of duplicate_decls: return truthvalue of whether
1232 : : or not types of these decls match.
1233 : :
1234 : : For C++, we must compare the parameter list so that `int' can match
1235 : : `int&' in a parameter position, but `int&' is not confused with
1236 : : `const int&'. */
1237 : :
1238 : : int
1239 : 957658825 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1240 : : {
1241 : 957658825 : int types_match;
1242 : :
1243 : 957658825 : if (newdecl == olddecl)
1244 : : return 1;
1245 : :
1246 : 957590017 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1247 : : /* If the two DECLs are not even the same kind of thing, we're not
1248 : : interested in their types. */
1249 : : return 0;
1250 : :
1251 : 475710333 : gcc_assert (DECL_P (newdecl));
1252 : :
1253 : 475710333 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1254 : : {
1255 : : /* Specializations of different templates are different functions
1256 : : even if they have the same type. */
1257 : 268091231 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1258 : 268091231 : ? DECL_TI_TEMPLATE (newdecl)
1259 : 612462 : : NULL_TREE);
1260 : 268091231 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1261 : 268091231 : ? DECL_TI_TEMPLATE (olddecl)
1262 : 3387260 : : NULL_TREE);
1263 : 268091231 : if (t1 != t2)
1264 : : return 0;
1265 : :
1266 : 265316367 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1267 : 268492765 : && ! (DECL_EXTERN_C_P (newdecl)
1268 : 3176398 : && DECL_EXTERN_C_P (olddecl)))
1269 : : return 0;
1270 : :
1271 : : /* A new declaration doesn't match a built-in one unless it
1272 : : is also extern "C". */
1273 : 245232493 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1274 : 245232493 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1275 : : return 0;
1276 : :
1277 : 244540395 : tree f1 = TREE_TYPE (newdecl);
1278 : 244540395 : tree f2 = TREE_TYPE (olddecl);
1279 : 244540395 : if (TREE_CODE (f1) != TREE_CODE (f2))
1280 : : return 0;
1281 : :
1282 : : /* A declaration with deduced return type should use its pre-deduction
1283 : : type for declaration matching. */
1284 : 244369716 : tree r2 = fndecl_declared_return_type (olddecl);
1285 : 244369716 : tree r1 = fndecl_declared_return_type (newdecl);
1286 : :
1287 : : /* For memchr et al, allow const void* return type (as specified by C++)
1288 : : when we expect void* (as in C). */
1289 : 244369716 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1290 : 6627483 : && DECL_EXTERN_C_P (olddecl)
1291 : 4205290 : && !same_type_p (r1, r2)
1292 : 70 : && TREE_CODE (r1) == POINTER_TYPE
1293 : 15 : && TREE_CODE (r2) == POINTER_TYPE
1294 : 244369728 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1295 : : r2 = r1;
1296 : :
1297 : 244369716 : tree p1 = TYPE_ARG_TYPES (f1);
1298 : 244369716 : tree p2 = TYPE_ARG_TYPES (f2);
1299 : :
1300 : 244369716 : if (same_type_p (r1, r2))
1301 : : {
1302 : 179331640 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1303 : 179331612 : && fndecl_built_in_p (olddecl))
1304 : : {
1305 : 50 : types_match = self_promoting_args_p (p1);
1306 : 50 : if (p1 == void_list_node)
1307 : 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1308 : : }
1309 : : else
1310 : 179331506 : types_match =
1311 : 179331506 : compparms (p1, p2)
1312 : 28959776 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1313 : 208291037 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1314 : 1246568 : || comp_type_attributes (TREE_TYPE (newdecl),
1315 : 1246568 : TREE_TYPE (olddecl)) != 0);
1316 : : }
1317 : : else
1318 : : types_match = 0;
1319 : :
1320 : : /* Two function declarations match if either has a requires-clause
1321 : : then both have a requires-clause and their constraints-expressions
1322 : : are equivalent. */
1323 : 28959560 : if (types_match && flag_concepts)
1324 : 8960813 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1325 : :
1326 : : /* The decls dont match if they correspond to two different versions
1327 : : of the same function. Disallow extern "C" functions to be
1328 : : versions for now. */
1329 : 28959560 : if (types_match
1330 : 28878577 : && !DECL_EXTERN_C_P (newdecl)
1331 : 20783387 : && !DECL_EXTERN_C_P (olddecl)
1332 : 49739123 : && disjoint_version_decls (newdecl, olddecl))
1333 : : {
1334 : 8814 : if (record_versions)
1335 : 8727 : maybe_version_functions (newdecl, olddecl);
1336 : 8814 : return 0;
1337 : : }
1338 : : }
1339 : 207619102 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1340 : : {
1341 : 206107195 : if (!template_heads_equivalent_p (newdecl, olddecl))
1342 : : return 0;
1343 : :
1344 : 66023434 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1345 : 66023434 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1346 : :
1347 : 66023434 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1348 : : return 0;
1349 : :
1350 : : /* Two template types match if they are the same. Otherwise, compare
1351 : : the underlying declarations. */
1352 : 66023428 : if (TREE_CODE (newres) == TYPE_DECL)
1353 : 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1354 : : else
1355 : 66023398 : types_match = decls_match (newres, oldres);
1356 : : }
1357 : : else
1358 : : {
1359 : : /* Need to check scope for variable declaration (VAR_DECL).
1360 : : For typedef (TYPE_DECL), scope is ignored. */
1361 : 1511907 : if (VAR_P (newdecl)
1362 : 1014849 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1363 : : /* [dcl.link]
1364 : : Two declarations for an object with C language linkage
1365 : : with the same name (ignoring the namespace that qualify
1366 : : it) that appear in different namespace scopes refer to
1367 : : the same object. */
1368 : 1691540 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1369 : : return 0;
1370 : :
1371 : 1332322 : if (TREE_TYPE (newdecl) == error_mark_node)
1372 : 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1373 : 1332322 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1374 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1375 : 1332322 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1376 : : types_match = 0;
1377 : : else
1378 : 1332322 : types_match = comptypes (TREE_TYPE (newdecl),
1379 : 1332322 : TREE_TYPE (olddecl),
1380 : : COMPARE_REDECLARATION);
1381 : : }
1382 : :
1383 : : return types_match;
1384 : : }
1385 : :
1386 : : /* Mark DECL as versioned if it isn't already. */
1387 : :
1388 : : void
1389 : 17544 : maybe_mark_function_versioned (tree decl)
1390 : : {
1391 : 17544 : if (!DECL_FUNCTION_VERSIONED (decl))
1392 : : {
1393 : : /* We need to insert function version now to make sure the correct
1394 : : pre-mangled assembler name is recorded. */
1395 : 1050 : cgraph_node *node = cgraph_node::get_create (decl);
1396 : :
1397 : 1050 : if (!node->function_version ())
1398 : 1050 : node->insert_new_function_version ();
1399 : :
1400 : 1050 : DECL_FUNCTION_VERSIONED (decl) = 1;
1401 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1402 : : to include the version marker. */
1403 : 1050 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1404 : 1044 : mangle_decl (decl);
1405 : : }
1406 : 17544 : }
1407 : :
1408 : : /* NEWDECL and OLDDECL have identical signatures. If they are
1409 : : different versions adjust them, record function versions, and return
1410 : : true. */
1411 : :
1412 : : bool
1413 : 168115 : maybe_version_functions (tree newdecl, tree olddecl)
1414 : : {
1415 : 168115 : if (!disjoint_version_decls (newdecl, olddecl))
1416 : : return false;
1417 : :
1418 : 8754 : maybe_mark_function_versioned (olddecl);
1419 : 8754 : if (DECL_LOCAL_DECL_P (olddecl))
1420 : : {
1421 : 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1422 : 18 : maybe_mark_function_versioned (olddecl);
1423 : : }
1424 : :
1425 : 8754 : maybe_mark_function_versioned (newdecl);
1426 : 8754 : if (DECL_LOCAL_DECL_P (newdecl))
1427 : : {
1428 : : /* Unfortunately, we can get here before pushdecl naturally calls
1429 : : push_local_extern_decl_alias, so we need to call it directly. */
1430 : 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1431 : 6 : push_local_extern_decl_alias (newdecl);
1432 : 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1433 : 18 : maybe_mark_function_versioned (newdecl);
1434 : : }
1435 : :
1436 : : /* Add the new version to the function version structure. */
1437 : 8754 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1438 : 8754 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1439 : 8754 : if (!fn_v)
1440 : 15 : fn_v = fn_node->insert_new_function_version ();
1441 : :
1442 : 8754 : cgraph_node::add_function_version (fn_v, newdecl);
1443 : :
1444 : 8754 : return true;
1445 : : }
1446 : :
1447 : : /* If NEWDECL is `static' and an `extern' was seen previously,
1448 : : warn about it. OLDDECL is the previous declaration.
1449 : :
1450 : : Note that this does not apply to the C++ case of declaring
1451 : : a variable `extern const' and then later `const'.
1452 : :
1453 : : Don't complain about built-in functions, since they are beyond
1454 : : the user's control. */
1455 : :
1456 : : void
1457 : 19365934 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1458 : : {
1459 : 19365934 : if (TREE_CODE (newdecl) == TYPE_DECL
1460 : 19338302 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1461 : 15688341 : || TREE_CODE (newdecl) == CONST_DECL
1462 : 15688341 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1463 : 19365922 : return;
1464 : :
1465 : : /* Don't get confused by static member functions; that's a different
1466 : : use of `static'. */
1467 : 15688341 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1468 : 15688341 : && DECL_STATIC_FUNCTION_P (newdecl))
1469 : : return;
1470 : :
1471 : : /* If the old declaration was `static', or the new one isn't, then
1472 : : everything is OK. */
1473 : 15521476 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1474 : : return;
1475 : :
1476 : : /* It's OK to declare a builtin function as `static'. */
1477 : 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1478 : 12 : && DECL_ARTIFICIAL (olddecl))
1479 : : return;
1480 : :
1481 : 12 : auto_diagnostic_group d;
1482 : 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1483 : : "%qD was declared %<extern%> and later %<static%>", newdecl))
1484 : 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1485 : : "previous declaration of %qD", olddecl);
1486 : 12 : }
1487 : :
1488 : : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1489 : : function templates. If their exception specifications do not
1490 : : match, issue a diagnostic. */
1491 : :
1492 : : static void
1493 : 18921215 : check_redeclaration_exception_specification (tree new_decl,
1494 : : tree old_decl)
1495 : : {
1496 : 18921215 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1497 : 18921215 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1498 : :
1499 : : /* Two default specs are equivalent, don't force evaluation. */
1500 : 7127482 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1501 : 19073597 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1502 : : return;
1503 : :
1504 : 18910793 : if (!type_dependent_expression_p (old_decl))
1505 : : {
1506 : 11214550 : maybe_instantiate_noexcept (new_decl);
1507 : 11214550 : maybe_instantiate_noexcept (old_decl);
1508 : : }
1509 : 18910793 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1510 : 18910793 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1511 : :
1512 : : /* [except.spec]
1513 : :
1514 : : If any declaration of a function has an exception-specification,
1515 : : all declarations, including the definition and an explicit
1516 : : specialization, of that function shall have an
1517 : : exception-specification with the same set of type-ids. */
1518 : 18910793 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1519 : 14836575 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1520 : 33747267 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1521 : : {
1522 : 326 : const char *const msg
1523 : : = G_("declaration of %qF has a different exception specifier");
1524 : 326 : bool complained = true;
1525 : 326 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1526 : 326 : auto_diagnostic_group d;
1527 : :
1528 : : /* Be permissive about C++98 vs C++11 operator new declarations. */
1529 : 652 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1530 : 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1531 : 328 : && (nothrow_spec_p (new_exceptions)
1532 : 1 : == nothrow_spec_p (old_exceptions)));
1533 : :
1534 : 326 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1535 : 326 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1536 : : /* Don't fuss about the C library; the C library functions are not
1537 : : specified to have exception specifications (just behave as if they
1538 : : have them), but some implementations include them. */
1539 : 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1540 : 93 : else if (!flag_exceptions)
1541 : : /* We used to silently permit mismatched eh specs with
1542 : : -fno-exceptions, so only complain if -pedantic. */
1543 : 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1544 : 90 : else if (!new_exceptions || global_new)
1545 : : /* Reduce to pedwarn for omitted exception specification. No warning
1546 : : flag for this; silence the warning by correcting the code. */
1547 : 21 : complained = pedwarn (new_loc, 0, msg, new_decl);
1548 : : else
1549 : 69 : error_at (new_loc, msg, new_decl);
1550 : :
1551 : 326 : if (complained)
1552 : 96 : inform (DECL_SOURCE_LOCATION (old_decl),
1553 : : "from previous declaration %qF", old_decl);
1554 : :
1555 : : /* Copy the old exception specification if new_decl has none. Unless the
1556 : : old decl is extern "C", as obscure code might depend on the type of
1557 : : the new declaration (e.g. noexcept-type19.C). */
1558 : 326 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1559 : 20 : TREE_TYPE (new_decl)
1560 : 40 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1561 : 326 : }
1562 : : }
1563 : :
1564 : : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1565 : : Otherwise issue diagnostics. */
1566 : :
1567 : : bool
1568 : 19366421 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1569 : : {
1570 : 19366421 : old_decl = STRIP_TEMPLATE (old_decl);
1571 : 19366421 : new_decl = STRIP_TEMPLATE (new_decl);
1572 : 19366421 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1573 : 19338765 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1574 : : return true;
1575 : 19338765 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1576 : 19338765 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1577 : : {
1578 : 19278352 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1579 : : return true;
1580 : 37843212 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1581 : 18921606 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1582 : : return true;
1583 : : }
1584 : 60419 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1585 : : {
1586 : : /* With -fimplicit-constexpr, ignore changes in the constexpr
1587 : : keyword. */
1588 : 62 : if (flag_implicit_constexpr
1589 : 62 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1590 : 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1591 : : return true;
1592 : 62 : if (fndecl_built_in_p (old_decl))
1593 : : {
1594 : : /* Hide a built-in declaration. */
1595 : 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1596 : 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1597 : 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1598 : 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1599 : 0 : return true;
1600 : : }
1601 : : /* 7.1.5 [dcl.constexpr]
1602 : : Note: An explicit specialization can differ from the template
1603 : : declaration with respect to the constexpr specifier. */
1604 : 62 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1605 : 62 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1606 : : return true;
1607 : :
1608 : 42 : const char *kind = "constexpr";
1609 : 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1610 : 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1611 : : kind = "consteval";
1612 : 42 : auto_diagnostic_group d;
1613 : 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1614 : : "redeclaration %qD differs in %qs "
1615 : : "from previous declaration", new_decl,
1616 : : kind);
1617 : 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1618 : : "previous declaration %qD", old_decl);
1619 : 42 : return false;
1620 : 42 : }
1621 : : return true;
1622 : : }
1623 : :
1624 : : /* DECL is a redeclaration of a function or function template. If
1625 : : it does have default arguments issue a diagnostic. Note: this
1626 : : function is used to enforce the requirements in C++11 8.3.6 about
1627 : : no default arguments in redeclarations. */
1628 : :
1629 : : static void
1630 : 11423763 : check_redeclaration_no_default_args (tree decl)
1631 : : {
1632 : 11423763 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1633 : :
1634 : 11423763 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1635 : 33634021 : t && t != void_list_node; t = TREE_CHAIN (t))
1636 : 22210315 : if (TREE_PURPOSE (t))
1637 : : {
1638 : 57 : permerror (DECL_SOURCE_LOCATION (decl),
1639 : : "redeclaration of %q#D may not have default "
1640 : : "arguments", decl);
1641 : 57 : return;
1642 : : }
1643 : : }
1644 : :
1645 : : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1646 : : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1647 : : the TEMPLATE_DECLs in case of function templates). This function is used
1648 : : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1649 : : "If a friend declaration specifies a default argument expression, that
1650 : : declaration shall be a definition and shall be the only declaration of
1651 : : the function or function template in the translation unit." */
1652 : :
1653 : : static void
1654 : 6414205 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1655 : : {
1656 : 12433306 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1657 : : return;
1658 : :
1659 : 2185966 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1660 : 1092983 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1661 : 2935922 : t1 && t1 != void_list_node;
1662 : 1842939 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1663 : 2587235 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1664 : 5062755 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1665 : : {
1666 : 36 : auto_diagnostic_group d;
1667 : 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1668 : : "friend declaration of %q#D specifies default "
1669 : : "arguments and isn%'t the only declaration", newdecl))
1670 : 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1671 : : "previous declaration of %q#D", olddecl);
1672 : 36 : return;
1673 : 36 : }
1674 : : }
1675 : :
1676 : : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1677 : : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1678 : :
1679 : : static void
1680 : 18341997 : merge_attribute_bits (tree newdecl, tree olddecl)
1681 : : {
1682 : 18341997 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1683 : 18341997 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1684 : 18341997 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1685 : 18341997 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1686 : 18341997 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1687 : 18341997 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1688 : 18341997 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1689 : 18341997 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1690 : 18341997 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1691 : 18341997 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1692 : 18341997 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1693 : 18341997 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1694 : 18341997 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1695 : 18341997 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1696 : 18341997 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1697 : 18341997 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1698 : 18341997 : }
1699 : :
1700 : : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1701 : : && lookup_attribute ("gnu_inline", \
1702 : : DECL_ATTRIBUTES (fn)))
1703 : :
1704 : : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1705 : : ambiguates olddecl. Returns true if an error occurs. */
1706 : :
1707 : : static bool
1708 : 202457078 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1709 : : {
1710 : 202457078 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1711 : 202457078 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1712 : : /* Function template declarations can be differentiated by parameter
1713 : : and return type. */
1714 : 202457078 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1715 : 202457078 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1716 : 202457078 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1717 : : TREE_TYPE (TREE_TYPE (olddecl))))
1718 : : {
1719 : : /* ... and also by their template-heads and requires-clauses. */
1720 : 4390 : if (template_heads_equivalent_p (newdecl, olddecl)
1721 : 4390 : && function_requirements_equivalent_p (newres, oldres))
1722 : : {
1723 : 0 : auto_diagnostic_group d;
1724 : 0 : error ("ambiguating new declaration %q+#D", newdecl);
1725 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1726 : : "old declaration %q#D", olddecl);
1727 : 0 : return true;
1728 : 0 : }
1729 : :
1730 : : /* FIXME: The types are the same but the are differences
1731 : : in either the template heads or function requirements.
1732 : : We should be able to diagnose a set of common errors
1733 : : stemming from these declarations. For example:
1734 : :
1735 : : template<typename T> requires C void f(...);
1736 : : template<typename T> void f(...) requires C;
1737 : :
1738 : : These are functionally equivalent but not equivalent. */
1739 : : }
1740 : :
1741 : : return false;
1742 : : }
1743 : :
1744 : : /* OLD_PARMS is the innermost set of template parameters for some template
1745 : : declaration, and NEW_PARMS is the corresponding set of template parameters
1746 : : for a redeclaration of that template. Merge the default arguments within
1747 : : these two sets of parameters. CLASS_P is true iff the template in
1748 : : question is a class template. */
1749 : :
1750 : : bool
1751 : 8242787 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1752 : : {
1753 : 8242787 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1754 : : == TREE_VEC_LENGTH (old_parms));
1755 : 23349657 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1756 : : {
1757 : 15106900 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1758 : 15106900 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1759 : 15106900 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1760 : 15106900 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1761 : 15106900 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1762 : : return false;
1763 : 15106897 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1764 : : {
1765 : 27 : auto_diagnostic_group d;
1766 : 27 : error ("redefinition of default argument for %q+#D", new_parm);
1767 : 27 : inform (DECL_SOURCE_LOCATION (old_parm),
1768 : : "original definition appeared here");
1769 : 27 : return false;
1770 : 27 : }
1771 : 15106870 : else if (new_default != NULL_TREE)
1772 : : /* Update the previous template parameters (which are the ones
1773 : : that will really count) with the new default value. */
1774 : 203782 : old_default = new_default;
1775 : 14903088 : else if (class_p && old_default != NULL_TREE)
1776 : : /* Update the new parameters, too; they'll be used as the
1777 : : parameters for any members. */
1778 : 1043756 : new_default = old_default;
1779 : : }
1780 : : return true;
1781 : : }
1782 : :
1783 : : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1784 : : If the redeclaration is invalid, a diagnostic is issued, and the
1785 : : error_mark_node is returned. Otherwise, OLDDECL is returned.
1786 : :
1787 : : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1788 : : returned.
1789 : :
1790 : : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1791 : : if the old decl was hidden.
1792 : :
1793 : : Hidden decls can be anticipated builtins, injected friends, or
1794 : : (coming soon) injected from a local-extern decl. */
1795 : :
1796 : : tree
1797 : 349971075 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1798 : : {
1799 : 349971075 : unsigned olddecl_uid = DECL_UID (olddecl);
1800 : 349971075 : int types_match = 0;
1801 : 349971075 : int new_defines_function = 0;
1802 : 349971075 : tree new_template_info;
1803 : 349971075 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1804 : 349971075 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1805 : :
1806 : 349971075 : if (newdecl == olddecl)
1807 : : return olddecl;
1808 : :
1809 : 349108117 : types_match = decls_match (newdecl, olddecl);
1810 : :
1811 : : /* If either the type of the new decl or the type of the old decl is an
1812 : : error_mark_node, then that implies that we have already issued an
1813 : : error (earlier) for some bogus type specification, and in that case,
1814 : : it is rather pointless to harass the user with yet more error message
1815 : : about the same declaration, so just pretend the types match here. */
1816 : 349108117 : if (TREE_TYPE (newdecl) == error_mark_node
1817 : 349108117 : || TREE_TYPE (olddecl) == error_mark_node)
1818 : : return error_mark_node;
1819 : :
1820 : : /* Check for redeclaration and other discrepancies. */
1821 : 349108092 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1822 : 349108092 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1823 : : {
1824 : 6465367 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1825 : : {
1826 : : /* Avoid warnings redeclaring built-ins which have not been
1827 : : explicitly declared. */
1828 : 192141 : if (was_hidden)
1829 : : {
1830 : 192113 : if (TREE_PUBLIC (newdecl)
1831 : 192113 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1832 : 12 : warning_at (newdecl_loc,
1833 : 12 : OPT_Wbuiltin_declaration_mismatch,
1834 : : "built-in function %qD declared as non-function",
1835 : : newdecl);
1836 : 192113 : return NULL_TREE;
1837 : : }
1838 : :
1839 : : /* If you declare a built-in or predefined function name as static,
1840 : : the old definition is overridden, but optionally warn this was a
1841 : : bad choice of name. */
1842 : 28 : if (! TREE_PUBLIC (newdecl))
1843 : : {
1844 : 28 : warning_at (newdecl_loc,
1845 : 28 : OPT_Wshadow,
1846 : 28 : fndecl_built_in_p (olddecl)
1847 : : ? G_("shadowing built-in function %q#D")
1848 : : : G_("shadowing library function %q#D"), olddecl);
1849 : : /* Discard the old built-in function. */
1850 : 28 : return NULL_TREE;
1851 : : }
1852 : : /* If the built-in is not ansi, then programs can override
1853 : : it even globally without an error. */
1854 : 0 : else if (! fndecl_built_in_p (olddecl))
1855 : 0 : warning_at (newdecl_loc, 0,
1856 : : "library function %q#D redeclared as non-function %q#D",
1857 : : olddecl, newdecl);
1858 : : else
1859 : 0 : error_at (newdecl_loc,
1860 : : "declaration of %q#D conflicts with built-in "
1861 : : "declaration %q#D", newdecl, olddecl);
1862 : 0 : return NULL_TREE;
1863 : : }
1864 : 6273226 : else if (!types_match)
1865 : : {
1866 : : /* Avoid warnings redeclaring built-ins which have not been
1867 : : explicitly declared. */
1868 : 2199008 : if (was_hidden)
1869 : : {
1870 : 837820 : tree t1, t2;
1871 : :
1872 : : /* A new declaration doesn't match a built-in one unless it
1873 : : is also extern "C". */
1874 : 837820 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1875 : 837820 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1876 : 837820 : if (!DECL_EXTERN_C_P (newdecl))
1877 : : return NULL_TREE;
1878 : :
1879 : 145722 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1880 : 145722 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1881 : 250340 : t1 || t2;
1882 : 104618 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1883 : : {
1884 : 250322 : if (!t1 || !t2)
1885 : : break;
1886 : : /* FILE, tm types are not known at the time
1887 : : we create the builtins. */
1888 : 777116 : for (unsigned i = 0;
1889 : 1027276 : i < sizeof (builtin_structptr_types)
1890 : : / sizeof (builtin_structptr_type);
1891 : : ++i)
1892 : 922652 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1893 : : {
1894 : 145536 : tree t = TREE_VALUE (t1);
1895 : :
1896 : 145536 : if (TYPE_PTR_P (t)
1897 : 291072 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1898 : 145536 : == get_identifier (builtin_structptr_types[i].str)
1899 : 291057 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1900 : : {
1901 : 145521 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1902 : :
1903 : 145521 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1904 : 145521 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1905 : 145521 : types_match = decls_match (newdecl, olddecl);
1906 : 145521 : if (types_match)
1907 : 145521 : return duplicate_decls (newdecl, olddecl,
1908 : 145521 : hiding, was_hidden);
1909 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1910 : : }
1911 : 15 : goto next_arg;
1912 : : }
1913 : :
1914 : 104624 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1915 : : break;
1916 : 104618 : next_arg:;
1917 : : }
1918 : :
1919 : 201 : warning_at (newdecl_loc,
1920 : 201 : OPT_Wbuiltin_declaration_mismatch,
1921 : : "declaration of %q#D conflicts with built-in "
1922 : : "declaration %q#D", newdecl, olddecl);
1923 : : }
1924 : 1361188 : else if ((DECL_EXTERN_C_P (newdecl)
1925 : 15 : && DECL_EXTERN_C_P (olddecl))
1926 : 2722361 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1927 : 1361173 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1928 : : {
1929 : : /* Don't really override olddecl for __* prefixed builtins
1930 : : except for __[^b]*_chk, the compiler might be using those
1931 : : explicitly. */
1932 : 15 : if (fndecl_built_in_p (olddecl))
1933 : : {
1934 : 15 : tree id = DECL_NAME (olddecl);
1935 : 15 : const char *name = IDENTIFIER_POINTER (id);
1936 : 15 : size_t len;
1937 : :
1938 : 15 : if (name[0] == '_'
1939 : 15 : && name[1] == '_'
1940 : 30 : && (startswith (name + 2, "builtin_")
1941 : 6 : || (len = strlen (name)) <= strlen ("___chk")
1942 : 6 : || memcmp (name + len - strlen ("_chk"),
1943 : : "_chk", strlen ("_chk") + 1) != 0))
1944 : : {
1945 : 15 : if (DECL_INITIAL (newdecl))
1946 : : {
1947 : 6 : error_at (newdecl_loc,
1948 : : "definition of %q#D ambiguates built-in "
1949 : : "declaration %q#D", newdecl, olddecl);
1950 : 6 : return error_mark_node;
1951 : : }
1952 : 9 : auto_diagnostic_group d;
1953 : 9 : if (permerror (newdecl_loc,
1954 : : "new declaration %q#D ambiguates built-in"
1955 : : " declaration %q#D", newdecl, olddecl)
1956 : 9 : && flag_permissive)
1957 : 3 : inform (newdecl_loc,
1958 : : "ignoring the %q#D declaration", newdecl);
1959 : 9 : return flag_permissive ? olddecl : error_mark_node;
1960 : 9 : }
1961 : : }
1962 : :
1963 : : /* A near match; override the builtin. */
1964 : :
1965 : 0 : if (TREE_PUBLIC (newdecl))
1966 : 0 : warning_at (newdecl_loc,
1967 : 0 : OPT_Wbuiltin_declaration_mismatch,
1968 : : "new declaration %q#D ambiguates built-in "
1969 : : "declaration %q#D", newdecl, olddecl);
1970 : : else
1971 : 0 : warning (OPT_Wshadow,
1972 : : fndecl_built_in_p (olddecl)
1973 : : ? G_("shadowing built-in function %q#D")
1974 : : : G_("shadowing library function %q#D"), olddecl);
1975 : : }
1976 : : else
1977 : : /* Discard the old built-in function. */
1978 : : return NULL_TREE;
1979 : :
1980 : : /* Replace the old RTL to avoid problems with inlining. */
1981 : 201 : COPY_DECL_RTL (newdecl, olddecl);
1982 : : }
1983 : : else
1984 : : {
1985 : : /* Even if the types match, prefer the new declarations type
1986 : : for built-ins which have not been explicitly declared,
1987 : : for exception lists, etc... */
1988 : 4074218 : tree type = TREE_TYPE (newdecl);
1989 : 4074218 : tree attribs = (*targetm.merge_type_attributes)
1990 : 4074218 : (TREE_TYPE (olddecl), type);
1991 : :
1992 : 4074218 : type = cp_build_type_attribute_variant (type, attribs);
1993 : 4074218 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1994 : : }
1995 : :
1996 : : /* If a function is explicitly declared "throw ()", propagate that to
1997 : : the corresponding builtin. */
1998 : 4074419 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1999 : 3913968 : && was_hidden
2000 : 3913925 : && TREE_NOTHROW (newdecl)
2001 : 7724661 : && !TREE_NOTHROW (olddecl))
2002 : : {
2003 : 29356 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
2004 : 29356 : tree tmpdecl = builtin_decl_explicit (fncode);
2005 : 29356 : if (tmpdecl && tmpdecl != olddecl && types_match)
2006 : 29356 : TREE_NOTHROW (tmpdecl) = 1;
2007 : : }
2008 : :
2009 : : /* Whether or not the builtin can throw exceptions has no
2010 : : bearing on this declarator. */
2011 : 4074419 : TREE_NOTHROW (olddecl) = 0;
2012 : :
2013 : 4074419 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2014 : : {
2015 : : /* If a builtin function is redeclared as `static', merge
2016 : : the declarations, but make the original one static. */
2017 : 3 : DECL_THIS_STATIC (olddecl) = 1;
2018 : 3 : TREE_PUBLIC (olddecl) = 0;
2019 : :
2020 : : /* Make the old declaration consistent with the new one so
2021 : : that all remnants of the builtin-ness of this function
2022 : : will be banished. */
2023 : 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2024 : 3 : COPY_DECL_RTL (newdecl, olddecl);
2025 : : }
2026 : : }
2027 : 342642725 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2028 : : {
2029 : : /* C++ Standard, 3.3, clause 4:
2030 : : "[Note: a namespace name or a class template name must be unique
2031 : : in its declarative region (7.3.2, clause 14). ]" */
2032 : 86177995 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
2033 : 86177968 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
2034 : : /* Namespace conflicts with not namespace. */;
2035 : 49272734 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
2036 : 135450690 : || DECL_TYPE_TEMPLATE_P (newdecl))
2037 : : /* Class template conflicts. */;
2038 : 86177950 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2039 : 49272734 : && DECL_TEMPLATE_RESULT (olddecl)
2040 : 49272734 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2041 : 135450678 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
2042 : 36861609 : && DECL_TEMPLATE_RESULT (newdecl)
2043 : 36861609 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2044 : : /* Variable template conflicts. */;
2045 : 86177941 : else if (concept_definition_p (olddecl)
2046 : 172355876 : || concept_definition_p (newdecl))
2047 : : /* Concept conflicts. */;
2048 : 86177929 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2049 : 49313324 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
2050 : 86218531 : || (TREE_CODE (olddecl) == FUNCTION_DECL
2051 : 36861616 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2052 : : {
2053 : : /* One is a function and the other is a template
2054 : : function. */
2055 : 86134307 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2056 : : return NULL_TREE;
2057 : :
2058 : : /* There can only be one! */
2059 : 60601 : auto_diagnostic_group d;
2060 : 60601 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
2061 : 60601 : && check_raw_literal_operator (olddecl))
2062 : 3 : error_at (newdecl_loc,
2063 : : "literal operator %q#D conflicts with"
2064 : : " raw literal operator", newdecl);
2065 : 60598 : else if (check_raw_literal_operator (newdecl))
2066 : 3 : error_at (newdecl_loc,
2067 : : "raw literal operator %q#D conflicts with"
2068 : : " literal operator template", newdecl);
2069 : : else
2070 : : return NULL_TREE;
2071 : :
2072 : 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2073 : 6 : return error_mark_node;
2074 : 60601 : }
2075 : 43622 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2076 : : /* A structured binding must be unique in its declarative region. */;
2077 : 40746 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2078 : 43619 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
2079 : : /* One is an implicit typedef, that's ok. */
2080 : : return NULL_TREE;
2081 : :
2082 : 153 : auto_diagnostic_group d;
2083 : 153 : error ("%q#D redeclared as different kind of entity", newdecl);
2084 : 153 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2085 : :
2086 : 153 : return error_mark_node;
2087 : 153 : }
2088 : 256464730 : else if (!types_match)
2089 : : {
2090 : 241140457 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2091 : : /* These are certainly not duplicate declarations; they're
2092 : : from different scopes. */
2093 : : return NULL_TREE;
2094 : :
2095 : 241140397 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2096 : : {
2097 : 202457096 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2098 : 202457096 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
2099 : :
2100 : : /* The name of a class template may not be declared to refer to
2101 : : any other template, class, function, object, namespace, value,
2102 : : or type in the same scope. */
2103 : 202457096 : if (TREE_CODE (oldres) == TYPE_DECL
2104 : 202457090 : || TREE_CODE (newres) == TYPE_DECL)
2105 : : {
2106 : 9 : auto_diagnostic_group d;
2107 : 9 : error_at (newdecl_loc,
2108 : : "conflicting declaration of template %q#D", newdecl);
2109 : 9 : inform (olddecl_loc,
2110 : : "previous declaration %q#D", olddecl);
2111 : 9 : return error_mark_node;
2112 : 9 : }
2113 : :
2114 : 202457087 : else if (TREE_CODE (oldres) == FUNCTION_DECL
2115 : 202457078 : && TREE_CODE (newres) == FUNCTION_DECL)
2116 : : {
2117 : 202457078 : if (duplicate_function_template_decls (newdecl, olddecl))
2118 : 0 : return error_mark_node;
2119 : : return NULL_TREE;
2120 : : }
2121 : : return NULL_TREE;
2122 : : }
2123 : 38683301 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2124 : : {
2125 : 38683153 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2126 : : {
2127 : 57 : auto_diagnostic_group d;
2128 : 57 : error_at (newdecl_loc,
2129 : : "conflicting declaration of C function %q#D",
2130 : : newdecl);
2131 : 57 : inform (olddecl_loc,
2132 : : "previous declaration %q#D", olddecl);
2133 : 57 : return error_mark_node;
2134 : 57 : }
2135 : : /* For function versions, params and types match, but they
2136 : : are not ambiguous. */
2137 : 38683096 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2138 : 38674828 : && !DECL_FUNCTION_VERSIONED (olddecl))
2139 : 8313 : || !same_type_p (fndecl_declared_return_type (newdecl),
2140 : : fndecl_declared_return_type (olddecl)))
2141 : : /* Let constrained hidden friends coexist for now, we'll
2142 : : check satisfaction later. */
2143 : 38674783 : && !member_like_constrained_friend_p (newdecl)
2144 : 38144183 : && !member_like_constrained_friend_p (olddecl)
2145 : : // The functions have the same parameter types.
2146 : 38108556 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2147 : 38108556 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2148 : : // And the same constraints.
2149 : 38683165 : && equivalently_constrained (newdecl, olddecl))
2150 : : {
2151 : 69 : auto_diagnostic_group d;
2152 : 69 : error_at (newdecl_loc,
2153 : : "ambiguating new declaration of %q#D", newdecl);
2154 : 69 : inform (olddecl_loc,
2155 : : "old declaration %q#D", olddecl);
2156 : 69 : return error_mark_node;
2157 : 69 : }
2158 : : else
2159 : 38683027 : return NULL_TREE;
2160 : : }
2161 : : else
2162 : : {
2163 : 148 : auto_diagnostic_group d;
2164 : 148 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2165 : 148 : inform (olddecl_loc,
2166 : : "previous declaration as %q#D", olddecl);
2167 : 148 : return error_mark_node;
2168 : 148 : }
2169 : : }
2170 : 15324273 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2171 : 15324273 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2172 : : {
2173 : : /* OMP UDRs are never duplicates. */
2174 : 33 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2175 : 33 : auto_diagnostic_group d;
2176 : 33 : error_at (newdecl_loc,
2177 : : "redeclaration of %<pragma omp declare reduction%>");
2178 : 33 : inform (olddecl_loc,
2179 : : "previous %<pragma omp declare reduction%> declaration");
2180 : 33 : return error_mark_node;
2181 : 33 : }
2182 : 15324240 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2183 : 15324240 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2184 : 220 : && (!DECL_TEMPLATE_INFO (newdecl)
2185 : 220 : || (DECL_TI_TEMPLATE (newdecl)
2186 : 220 : != DECL_TI_TEMPLATE (olddecl))))
2187 : 11197372 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2188 : 579480 : && (!DECL_TEMPLATE_INFO (olddecl)
2189 : 579480 : || (DECL_TI_TEMPLATE (olddecl)
2190 : 579480 : != DECL_TI_TEMPLATE (newdecl))))))
2191 : : /* It's OK to have a template specialization and a non-template
2192 : : with the same type, or to have specializations of two
2193 : : different templates with the same type. Note that if one is a
2194 : : specialization, and the other is an instantiation of the same
2195 : : template, that we do not exit at this point. That situation
2196 : : can occur if we instantiate a template class, and then
2197 : : specialize one of its methods. This situation is valid, but
2198 : : the declarations must be merged in the usual way. */
2199 : : return NULL_TREE;
2200 : 15324240 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2201 : 15324240 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2202 : 612176 : && !DECL_USE_TEMPLATE (newdecl))
2203 : 11197372 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2204 : 32916 : && !DECL_USE_TEMPLATE (olddecl))))
2205 : : /* One of the declarations is a template instantiation, and the
2206 : : other is not a template at all. That's OK. */
2207 : : return NULL_TREE;
2208 : 15324240 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2209 : : {
2210 : : /* In [namespace.alias] we have:
2211 : :
2212 : : In a declarative region, a namespace-alias-definition can be
2213 : : used to redefine a namespace-alias declared in that declarative
2214 : : region to refer only to the namespace to which it already
2215 : : refers.
2216 : :
2217 : : Therefore, if we encounter a second alias directive for the same
2218 : : alias, we can just ignore the second directive. */
2219 : 22 : if (DECL_NAMESPACE_ALIAS (newdecl)
2220 : 22 : && (DECL_NAMESPACE_ALIAS (newdecl)
2221 : 19 : == DECL_NAMESPACE_ALIAS (olddecl)))
2222 : : return olddecl;
2223 : :
2224 : : /* Leave it to update_binding to merge or report error. */
2225 : : return NULL_TREE;
2226 : : }
2227 : : /* Check if the two decls are non-mergeable versioned decls. */
2228 : 15324218 : else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2229 : : && diagnose_versioned_decls (olddecl, newdecl))
2230 : : return error_mark_node;
2231 : : else
2232 : : {
2233 : 15324218 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2234 : 15324218 : if (errmsg)
2235 : : {
2236 : 617 : auto_diagnostic_group d;
2237 : 617 : error_at (newdecl_loc, errmsg, newdecl);
2238 : 617 : if (DECL_NAME (olddecl) != NULL_TREE)
2239 : 617 : inform (olddecl_loc,
2240 : 617 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2241 : : ? G_("%q#D previously defined here")
2242 : : : G_("%q#D previously declared here"), olddecl);
2243 : 617 : if (cxx_dialect >= cxx26
2244 : 147 : && DECL_NAME (newdecl)
2245 : 147 : && id_equal (DECL_NAME (newdecl), "_")
2246 : 630 : && !name_independent_decl_p (newdecl))
2247 : : {
2248 : 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2249 : 2 : inform (newdecl_loc,
2250 : : "parameter declaration is not name-independent");
2251 : 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2252 : : {
2253 : 3 : if (at_namespace_scope_p ())
2254 : 2 : inform (newdecl_loc,
2255 : : "structured binding at namespace scope is not "
2256 : : "name-independent");
2257 : 1 : else if (TREE_STATIC (newdecl))
2258 : 1 : inform (newdecl_loc,
2259 : : "static structured binding is not "
2260 : : "name-independent");
2261 : 0 : else if (DECL_EXTERNAL (newdecl))
2262 : 0 : inform (newdecl_loc,
2263 : : "extern structured binding is not "
2264 : : "name-independent");
2265 : : }
2266 : 8 : else if (at_class_scope_p ()
2267 : 0 : && VAR_P (newdecl)
2268 : 8 : && TREE_STATIC (newdecl))
2269 : 0 : inform (newdecl_loc,
2270 : : "static data member is not name-independent");
2271 : 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2272 : 3 : inform (newdecl_loc,
2273 : : "variable at namespace scope is not name-independent");
2274 : 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2275 : 3 : inform (newdecl_loc,
2276 : : "static variable is not name-independent");
2277 : 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2278 : 0 : inform (newdecl_loc,
2279 : : "extern variable is not name-independent");
2280 : : }
2281 : 617 : return error_mark_node;
2282 : 617 : }
2283 : 15323601 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2284 : 11197135 : && DECL_INITIAL (olddecl) != NULL_TREE
2285 : 11879 : && !prototype_p (TREE_TYPE (olddecl))
2286 : 15323601 : && prototype_p (TREE_TYPE (newdecl)))
2287 : : {
2288 : : /* Prototype decl follows defn w/o prototype. */
2289 : 0 : auto_diagnostic_group d;
2290 : 0 : if (warning_at (newdecl_loc, 0,
2291 : : "prototype specified for %q#D", newdecl))
2292 : 0 : inform (olddecl_loc,
2293 : : "previous non-prototype definition here");
2294 : 0 : }
2295 : 14906769 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2296 : 26520736 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2297 : : {
2298 : : /* [dcl.link]
2299 : : If two declarations of the same function or object
2300 : : specify different linkage-specifications ..., the program
2301 : : is ill-formed.... Except for functions with C++ linkage,
2302 : : a function declaration without a linkage specification
2303 : : shall not precede the first linkage specification for
2304 : : that function. A function can be declared without a
2305 : : linkage specification after an explicit linkage
2306 : : specification has been seen; the linkage explicitly
2307 : : specified in the earlier declaration is not affected by
2308 : : such a function declaration.
2309 : :
2310 : : DR 563 raises the question why the restrictions on
2311 : : functions should not also apply to objects. Older
2312 : : versions of G++ silently ignore the linkage-specification
2313 : : for this example:
2314 : :
2315 : : namespace N {
2316 : : extern int i;
2317 : : extern "C" int i;
2318 : : }
2319 : :
2320 : : which is clearly wrong. Therefore, we now treat objects
2321 : : like functions. */
2322 : 1801 : if (current_lang_depth () == 0)
2323 : : {
2324 : : /* There is no explicit linkage-specification, so we use
2325 : : the linkage from the previous declaration. */
2326 : 1789 : retrofit_lang_decl (newdecl);
2327 : 1789 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2328 : : }
2329 : : else
2330 : : {
2331 : 12 : auto_diagnostic_group d;
2332 : 12 : error_at (newdecl_loc,
2333 : : "conflicting declaration of %q#D with %qL linkage",
2334 : 12 : newdecl, DECL_LANGUAGE (newdecl));
2335 : 12 : inform (olddecl_loc,
2336 : : "previous declaration with %qL linkage",
2337 : 12 : DECL_LANGUAGE (olddecl));
2338 : 12 : }
2339 : : }
2340 : :
2341 : 15323601 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2342 : : ;
2343 : 14689663 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2344 : : {
2345 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2346 : 12947524 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2347 : 10584748 : && (/* grokfndecl passes member function templates too
2348 : : as FUNCTION_DECLs. */
2349 : 8388709 : DECL_TEMPLATE_INFO (olddecl)
2350 : : /* C++11 8.3.6/6.
2351 : : Default arguments for a member function of a class
2352 : : template shall be specified on the initial declaration
2353 : : of the member function within the class template. */
2354 : 672787 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2355 : : {
2356 : 7797153 : check_redeclaration_no_default_args (newdecl);
2357 : :
2358 : 7797153 : if (DECL_TEMPLATE_INFO (olddecl)
2359 : 7797153 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2360 : : {
2361 : 2180966 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2362 : 2180966 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2363 : 2180966 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2364 : 2180966 : tree old_parms
2365 : 2180966 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2366 : 2180966 : merge_default_template_args (new_parms, old_parms,
2367 : : /*class_p=*/false);
2368 : : }
2369 : : }
2370 : : else
2371 : : {
2372 : 2787595 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2373 : 2787595 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2374 : 2787595 : int i = 1;
2375 : :
2376 : 7163162 : for (; t1 && t1 != void_list_node;
2377 : 4375567 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2378 : 4377397 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2379 : : {
2380 : 57 : if (simple_cst_equal (TREE_PURPOSE (t1),
2381 : 57 : TREE_PURPOSE (t2)) == 1)
2382 : : {
2383 : 24 : auto_diagnostic_group d;
2384 : 24 : if (permerror (newdecl_loc,
2385 : : "default argument given for parameter "
2386 : : "%d of %q#D", i, newdecl))
2387 : 24 : inform (olddecl_loc,
2388 : : "previous specification in %q#D here",
2389 : : olddecl);
2390 : 24 : }
2391 : : else
2392 : : {
2393 : 33 : auto_diagnostic_group d;
2394 : 33 : error_at (newdecl_loc,
2395 : : "default argument given for parameter %d "
2396 : : "of %q#D", i, newdecl);
2397 : 33 : inform (olddecl_loc,
2398 : : "previous specification in %q#D here",
2399 : : olddecl);
2400 : 33 : }
2401 : : }
2402 : :
2403 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2404 : : argument expression, that declaration... shall be the only
2405 : : declaration of the function or function template in the
2406 : : translation unit." */
2407 : 2787595 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2408 : : }
2409 : : }
2410 : : }
2411 : :
2412 : : /* Do not merge an implicit typedef with an explicit one. In:
2413 : :
2414 : : class A;
2415 : : ...
2416 : : typedef class A A __attribute__ ((foo));
2417 : :
2418 : : the attribute should apply only to the typedef. */
2419 : 19398020 : if (TREE_CODE (olddecl) == TYPE_DECL
2420 : 19398020 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2421 : 27635 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2422 : : return NULL_TREE;
2423 : :
2424 : 19366000 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2425 : : return NULL_TREE;
2426 : :
2427 : 19365988 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2428 : 30 : return error_mark_node;
2429 : :
2430 : 19365958 : if (modules_p ()
2431 : 45057 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2432 : 19393021 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2433 : : {
2434 : 27063 : if (!module_may_redeclare (olddecl, newdecl))
2435 : 24 : return error_mark_node;
2436 : :
2437 : 27039 : if (!hiding)
2438 : : {
2439 : : /* The old declaration should match the exportingness of the new
2440 : : declaration. But hidden friend declarations just keep the
2441 : : exportingness of the old declaration; see CWG2588. */
2442 : 23971 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2443 : 23971 : if (DECL_LANG_SPECIFIC (not_tmpl)
2444 : 23926 : && DECL_MODULE_ATTACH_P (not_tmpl)
2445 : : /* Typedefs are not entities and so are OK to be redeclared
2446 : : as exported: see [module.interface]/p6. */
2447 : 24186 : && TREE_CODE (olddecl) != TYPE_DECL)
2448 : : {
2449 : 203 : if (DECL_MODULE_EXPORT_P (newdecl)
2450 : 203 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2451 : : {
2452 : 15 : auto_diagnostic_group d;
2453 : 15 : error ("conflicting exporting for declaration %qD", newdecl);
2454 : 15 : inform (olddecl_loc,
2455 : : "previously declared here without exporting");
2456 : 15 : }
2457 : : }
2458 : 23768 : else if (DECL_MODULE_EXPORT_P (newdecl))
2459 : 10011 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2460 : : }
2461 : : }
2462 : :
2463 : : /* We have committed to returning OLDDECL at this point. */
2464 : :
2465 : : /* If new decl is `static' and an `extern' was seen previously,
2466 : : warn about it. */
2467 : 19365934 : warn_extern_redeclared_static (newdecl, olddecl);
2468 : :
2469 : : /* True to merge attributes between the declarations, false to
2470 : : set OLDDECL's attributes to those of NEWDECL (for template
2471 : : explicit specializations that specify their own attributes
2472 : : independent of those specified for the primary template). */
2473 : 19365934 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2474 : 15271509 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2475 : 19945399 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2476 : :
2477 : 19365934 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2478 : : {
2479 : 15271509 : if (merge_attr)
2480 : : {
2481 : 14692249 : {
2482 : 14692249 : auto_diagnostic_group d;
2483 : 14692249 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2484 : 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2485 : : ? G_("previous definition of %qD here")
2486 : : : G_("previous declaration of %qD here"), olddecl);
2487 : 14692249 : }
2488 : :
2489 : : /* [dcl.attr.noreturn]: The first declaration of a function shall
2490 : : specify the noreturn attribute if any declaration of that function
2491 : : specifies the noreturn attribute. */
2492 : 14692249 : tree a;
2493 : 14692249 : if (TREE_THIS_VOLATILE (newdecl)
2494 : 95193 : && !TREE_THIS_VOLATILE (olddecl)
2495 : : /* This applies to [[noreturn]] only, not its GNU variants. */
2496 : 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2497 : 54 : && cxx11_attribute_p (a)
2498 : 14692270 : && get_attribute_namespace (a) == NULL_TREE)
2499 : : {
2500 : 9 : auto_diagnostic_group d;
2501 : 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2502 : : "but its first declaration was not", newdecl);
2503 : 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2504 : 9 : }
2505 : : }
2506 : :
2507 : : /* Now that functions must hold information normally held
2508 : : by field decls, there is extra work to do so that
2509 : : declaration information does not get destroyed during
2510 : : definition. */
2511 : 15271509 : if (DECL_VINDEX (olddecl))
2512 : 165958 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2513 : 15271509 : if (DECL_CONTEXT (olddecl))
2514 : 15271509 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2515 : 15271509 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2516 : 15271509 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2517 : 15271509 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2518 : 15271509 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2519 : 15271509 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2520 : 15271509 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2521 : 15271509 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2522 : 15271509 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2523 : 15271509 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2524 : 15271509 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2525 : 15271509 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2526 : 2557158 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2527 : 5114316 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2528 : 15271509 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2529 : :
2530 : 15271509 : duplicate_contracts (newdecl, olddecl);
2531 : :
2532 : : /* Optionally warn about more than one declaration for the same
2533 : : name, but don't warn about a function declaration followed by a
2534 : : definition. */
2535 : 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2536 : 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2537 : : /* Don't warn about extern decl followed by definition. */
2538 : 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2539 : : /* Don't warn if at least one is/was hidden. */
2540 : 27 : && !(hiding || was_hidden)
2541 : : /* Don't warn about declaration followed by specialization. */
2542 : 15271521 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2543 : 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2544 : : {
2545 : 6 : auto_diagnostic_group d;
2546 : 6 : if (warning_at (newdecl_loc,
2547 : 6 : OPT_Wredundant_decls,
2548 : : "redundant redeclaration of %qD in same scope",
2549 : : newdecl))
2550 : 6 : inform (olddecl_loc,
2551 : : "previous declaration of %qD", olddecl);
2552 : 6 : }
2553 : :
2554 : : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2555 : : first declaration of the function or, for an explicit specialization
2556 : : of a function template, the first declaration of that
2557 : : specialization. */
2558 : 15271509 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2559 : 612176 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2560 : : {
2561 : 14692249 : if (DECL_DELETED_FN (newdecl))
2562 : : {
2563 : 21 : auto_diagnostic_group d;
2564 : 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2565 : : "is not first declaration", newdecl))
2566 : 21 : inform (olddecl_loc,
2567 : : "previous declaration of %qD", olddecl);
2568 : 21 : }
2569 : 14692249 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2570 : 14692249 : if (DECL_DELETED_FN (olddecl)
2571 : 6 : && DECL_INITIAL (olddecl)
2572 : 14692255 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2573 : 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2574 : : }
2575 : : }
2576 : :
2577 : : /* Deal with C++: must preserve virtual function table size. */
2578 : 19365934 : if (TREE_CODE (olddecl) == TYPE_DECL)
2579 : : {
2580 : 27632 : tree newtype = TREE_TYPE (newdecl);
2581 : 27632 : tree oldtype = TREE_TYPE (olddecl);
2582 : :
2583 : 27632 : if (newtype != error_mark_node && oldtype != error_mark_node
2584 : 55264 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2585 : 70857 : CLASSTYPE_FRIEND_CLASSES (newtype)
2586 : 23619 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2587 : :
2588 : 27632 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2589 : : }
2590 : :
2591 : : /* Copy all the DECL_... slots specified in the new decl except for
2592 : : any that we copy here from the old type. */
2593 : 19365934 : if (merge_attr)
2594 : 18786674 : DECL_ATTRIBUTES (newdecl)
2595 : 37573348 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2596 : : else
2597 : 579260 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2598 : :
2599 : : /* Transfer purviewness and importingness to the old decl. */
2600 : 19365934 : transfer_defining_module (olddecl, newdecl);
2601 : :
2602 : 19365934 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2603 : : {
2604 : 3649961 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2605 : 3649961 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2606 : 3649961 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2607 : :
2608 : : /* The new decl should not already have gathered any
2609 : : specializations. */
2610 : 3649961 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2611 : :
2612 : : /* Make sure the contracts are equivalent. */
2613 : 3649961 : duplicate_contracts (newdecl, olddecl);
2614 : :
2615 : : /* Remove contracts from old_result so they aren't appended to
2616 : : old_result by the merge function. */
2617 : 3649961 : remove_contract_attributes (old_result);
2618 : :
2619 : 3649961 : DECL_ATTRIBUTES (old_result)
2620 : 3649961 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2621 : :
2622 : 3649961 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2623 : : {
2624 : 3649931 : if (DECL_SOURCE_LOCATION (newdecl)
2625 : 3649931 : != DECL_SOURCE_LOCATION (olddecl))
2626 : : {
2627 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2628 : : later declarations of a function template. */
2629 : 3626610 : check_redeclaration_no_default_args (newdecl);
2630 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2631 : : argument expression, that declaration... shall be the only
2632 : : declaration of the function or function template in the
2633 : : translation unit." */
2634 : 3626610 : check_no_redeclaration_friend_default_args
2635 : 3626610 : (old_result, new_result);
2636 : :
2637 : 3626610 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2638 : 3626610 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2639 : 3626610 : merge_default_template_args (new_parms, old_parms,
2640 : : /*class_p=*/false);
2641 : : }
2642 : 3649931 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2643 : 2969445 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2644 : :
2645 : 3649931 : check_default_args (newdecl);
2646 : :
2647 : 7299850 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2648 : 3649931 : && DECL_INITIAL (new_result))
2649 : : {
2650 : 24 : if (DECL_INITIAL (old_result))
2651 : 12 : DECL_UNINLINABLE (old_result) = 1;
2652 : : else
2653 : 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2654 : 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2655 : 72 : DECL_NOT_REALLY_EXTERN (old_result)
2656 : 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2657 : 48 : DECL_INTERFACE_KNOWN (old_result)
2658 : 24 : = DECL_INTERFACE_KNOWN (new_result);
2659 : 24 : DECL_DECLARED_INLINE_P (old_result)
2660 : 24 : = DECL_DECLARED_INLINE_P (new_result);
2661 : 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2662 : 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2663 : : }
2664 : : else
2665 : : {
2666 : 3649907 : DECL_DECLARED_INLINE_P (old_result)
2667 : 3649907 : |= DECL_DECLARED_INLINE_P (new_result);
2668 : 3649907 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2669 : 3649907 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2670 : 3649907 : check_redeclaration_exception_specification (newdecl, olddecl);
2671 : :
2672 : 3649907 : merge_attribute_bits (new_result, old_result);
2673 : : }
2674 : : }
2675 : :
2676 : : /* If the new declaration is a definition, update the file and
2677 : : line information on the declaration, and also make
2678 : : the old declaration the same definition. */
2679 : 3649961 : if (DECL_INITIAL (new_result) != NULL_TREE)
2680 : : {
2681 : 5510916 : DECL_SOURCE_LOCATION (olddecl)
2682 : 5510916 : = DECL_SOURCE_LOCATION (old_result)
2683 : 2755458 : = DECL_SOURCE_LOCATION (newdecl);
2684 : 2755458 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2685 : 2755458 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2686 : : {
2687 : 2755458 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2688 : 9849582 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2689 : 7094124 : DECL_CONTEXT (p) = old_result;
2690 : :
2691 : 5510916 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2692 : 14 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2693 : : }
2694 : : }
2695 : :
2696 : 3649961 : return olddecl;
2697 : : }
2698 : :
2699 : 15715973 : if (types_match)
2700 : : {
2701 : 15715772 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2702 : 15271308 : check_redeclaration_exception_specification (newdecl, olddecl);
2703 : :
2704 : : /* Automatically handles default parameters. */
2705 : 15715772 : tree oldtype = TREE_TYPE (olddecl);
2706 : 15715772 : tree newtype;
2707 : :
2708 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2709 : : at newdecl, which will be ggc_freed. */
2710 : 15715772 : if (TREE_CODE (newdecl) == TYPE_DECL)
2711 : : {
2712 : : /* But NEWTYPE might have an attribute, honor that. */
2713 : 27632 : tree tem = TREE_TYPE (newdecl);
2714 : 27632 : newtype = oldtype;
2715 : :
2716 : 27632 : if (TYPE_USER_ALIGN (tem))
2717 : : {
2718 : 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2719 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2720 : 21 : TYPE_USER_ALIGN (newtype) = true;
2721 : : }
2722 : :
2723 : : /* And remove the new type from the variants list. */
2724 : 27632 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2725 : : {
2726 : 24 : tree remove = TREE_TYPE (newdecl);
2727 : 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2728 : : {
2729 : 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2730 : : /* If remove is the main variant, no need to remove that
2731 : : from the list. One of the DECL_ORIGINAL_TYPE
2732 : : variants, e.g. created for aligned attribute, might still
2733 : : refer to the newdecl TYPE_DECL though, so remove that one
2734 : : in that case. */
2735 : 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2736 : 6 : if (orig != remove)
2737 : 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2738 : 0 : t = TYPE_MAIN_VARIANT (t))
2739 : 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2740 : : {
2741 : 12 : TYPE_NEXT_VARIANT (t)
2742 : 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2743 : 6 : break;
2744 : : }
2745 : : }
2746 : : else
2747 : 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2748 : 0 : t = TYPE_NEXT_VARIANT (t))
2749 : 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2750 : : {
2751 : 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2752 : 15 : break;
2753 : : }
2754 : : }
2755 : : }
2756 : 15688140 : else if (merge_attr)
2757 : 15108880 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2758 : : else
2759 : 579260 : newtype = TREE_TYPE (newdecl);
2760 : :
2761 : 15715772 : if (VAR_P (newdecl))
2762 : : {
2763 : 416832 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2764 : : /* For already initialized vars, TREE_READONLY could have been
2765 : : cleared in cp_finish_decl, because the var needs runtime
2766 : : initialization or destruction. Make sure not to set
2767 : : TREE_READONLY on it again. */
2768 : 416832 : if (DECL_INITIALIZED_P (olddecl)
2769 : 1213 : && !DECL_EXTERNAL (olddecl)
2770 : 416917 : && !TREE_READONLY (olddecl))
2771 : 30 : TREE_READONLY (newdecl) = 0;
2772 : 416832 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2773 : 833664 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2774 : 416832 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2775 : 416832 : if (DECL_DEPENDENT_INIT_P (olddecl))
2776 : 106298 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2777 : 833664 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2778 : 416832 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2779 : 416832 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2780 : 416832 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2781 : 833664 : DECL_DECLARED_CONSTINIT_P (newdecl)
2782 : 416832 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2783 : :
2784 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2785 : 416832 : if (DECL_LANG_SPECIFIC (olddecl)
2786 : 830087 : && CP_DECL_THREADPRIVATE_P (olddecl))
2787 : : {
2788 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2789 : 41 : retrofit_lang_decl (newdecl);
2790 : 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2791 : : }
2792 : : }
2793 : :
2794 : : /* An explicit specialization of a function template or of a member
2795 : : function of a class template can be declared transaction_safe
2796 : : independently of whether the corresponding template entity is declared
2797 : : transaction_safe. */
2798 : 1204 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2799 : 1069 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2800 : 78 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2801 : 54 : && tx_safe_fn_type_p (newtype)
2802 : 15715772 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2803 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2804 : :
2805 : 15715772 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2806 : :
2807 : 15715772 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2808 : 15271308 : check_default_args (newdecl);
2809 : :
2810 : : /* Lay the type out, unless already done. */
2811 : 15715772 : if (! same_type_p (newtype, oldtype)
2812 : 366 : && TREE_TYPE (newdecl) != error_mark_node
2813 : 15716138 : && !(processing_template_decl && uses_template_parms (newdecl)))
2814 : 285 : layout_type (TREE_TYPE (newdecl));
2815 : :
2816 : 15715772 : if ((VAR_P (newdecl)
2817 : 15298940 : || TREE_CODE (newdecl) == PARM_DECL
2818 : 15298940 : || TREE_CODE (newdecl) == RESULT_DECL
2819 : 15298940 : || TREE_CODE (newdecl) == FIELD_DECL
2820 : 15298940 : || TREE_CODE (newdecl) == TYPE_DECL)
2821 : 15743404 : && !(processing_template_decl && uses_template_parms (newdecl)))
2822 : 280883 : layout_decl (newdecl, 0);
2823 : :
2824 : : /* Merge deprecatedness. */
2825 : 15715772 : if (TREE_DEPRECATED (newdecl))
2826 : 12058 : TREE_DEPRECATED (olddecl) = 1;
2827 : :
2828 : : /* Merge unavailability. */
2829 : 15715772 : if (TREE_UNAVAILABLE (newdecl))
2830 : 3 : TREE_UNAVAILABLE (olddecl) = 1;
2831 : :
2832 : : /* Preserve function specific target and optimization options */
2833 : 15715772 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2834 : : {
2835 : 15271308 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2836 : 15271308 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2837 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2838 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2839 : :
2840 : 15271308 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2841 : 15271308 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2842 : 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2843 : 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2844 : :
2845 : 15271308 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2846 : 15072622 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2847 : : }
2848 : : else
2849 : : {
2850 : : /* Merge the const type qualifier. */
2851 : 444464 : if (TREE_READONLY (newdecl))
2852 : 61693 : TREE_READONLY (olddecl) = 1;
2853 : : /* Merge the volatile type qualifier. */
2854 : 444464 : if (TREE_THIS_VOLATILE (newdecl))
2855 : 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2856 : : }
2857 : :
2858 : : /* Merge the initialization information. */
2859 : 15715772 : if (DECL_INITIAL (newdecl) == NULL_TREE
2860 : 15715772 : && DECL_INITIAL (olddecl) != NULL_TREE)
2861 : : {
2862 : 177700 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2863 : 177700 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2864 : 177700 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2865 : : {
2866 : 11795 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2867 : 11795 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2868 : : }
2869 : : }
2870 : :
2871 : 15715772 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2872 : : {
2873 : 15271308 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2874 : 15271308 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2875 : 15271308 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2876 : 15271308 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2877 : 54226 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2878 : 15271308 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2879 : 15271308 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2880 : 15271308 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2881 : 15271308 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2882 : :
2883 : 15271308 : if (merge_attr)
2884 : 14692048 : merge_attribute_bits (newdecl, olddecl);
2885 : : else
2886 : : {
2887 : : /* Merge the noreturn bit. */
2888 : 579260 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2889 : 579260 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2890 : 579260 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2891 : 579260 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2892 : 579260 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2893 : : }
2894 : : /* Keep the old RTL. */
2895 : 15271308 : COPY_DECL_RTL (olddecl, newdecl);
2896 : : }
2897 : 444464 : else if (VAR_P (newdecl)
2898 : 444464 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2899 : : {
2900 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2901 : : declaration was for an incomplete object and the new
2902 : : declaration is not since many attributes of the RTL will
2903 : : change. */
2904 : 416758 : COPY_DECL_RTL (olddecl, newdecl);
2905 : : }
2906 : : }
2907 : : /* If cannot merge, then use the new type and qualifiers,
2908 : : and don't preserve the old rtl. */
2909 : : else
2910 : : {
2911 : : /* Clean out any memory we had of the old declaration. */
2912 : 201 : tree oldstatic = value_member (olddecl, static_aggregates);
2913 : 201 : if (oldstatic)
2914 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2915 : :
2916 : 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2917 : 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2918 : 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2919 : 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2920 : 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2921 : : }
2922 : :
2923 : : /* Merge the storage class information. */
2924 : 15715973 : merge_weak (newdecl, olddecl);
2925 : :
2926 : 15715973 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2927 : 15715973 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2928 : 15715973 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2929 : 15715973 : if (! DECL_EXTERNAL (olddecl))
2930 : 32258 : DECL_EXTERNAL (newdecl) = 0;
2931 : 15715973 : if (! DECL_COMDAT (olddecl))
2932 : 6816208 : DECL_COMDAT (newdecl) = 0;
2933 : :
2934 : 15715973 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2935 : : {
2936 : 78 : if (!DECL_LOCAL_DECL_P (olddecl))
2937 : : /* This can happen if olddecl was brought in from the
2938 : : enclosing namespace via a using-decl. The new decl is
2939 : : then not a block-scope extern at all. */
2940 : 3 : DECL_LOCAL_DECL_P (newdecl) = false;
2941 : : else
2942 : : {
2943 : 75 : retrofit_lang_decl (newdecl);
2944 : 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2945 : 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2946 : 75 : if (alias != error_mark_node)
2947 : : {
2948 : 72 : DECL_ATTRIBUTES (alias)
2949 : 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2950 : 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2951 : 42 : merge_attribute_bits (newdecl, alias);
2952 : : }
2953 : : }
2954 : : }
2955 : :
2956 : 15715973 : new_template_info = NULL_TREE;
2957 : 15715973 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2958 : : {
2959 : 15684782 : bool new_redefines_gnu_inline = false;
2960 : :
2961 : 15684782 : if (new_defines_function
2962 : 15684782 : && ((DECL_INTERFACE_KNOWN (olddecl)
2963 : 1235 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2964 : 9189930 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2965 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2966 : : == FUNCTION_DECL))))
2967 : 2470 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2968 : :
2969 : : if (!new_redefines_gnu_inline)
2970 : : {
2971 : 15684704 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2972 : 15684704 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2973 : 15684704 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2974 : : }
2975 : :
2976 : 15684782 : if (TREE_CODE (newdecl) != TYPE_DECL)
2977 : : {
2978 : 15684718 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2979 : 15684718 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2980 : 15684718 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2981 : :
2982 : : /* If the OLDDECL is an instantiation and/or specialization,
2983 : : then the NEWDECL must be too. But, it may not yet be marked
2984 : : as such if the caller has created NEWDECL, but has not yet
2985 : : figured out that it is a redeclaration. */
2986 : 15684718 : if (!DECL_USE_TEMPLATE (newdecl))
2987 : 15072328 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2988 : :
2989 : 15684718 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2990 : 15105234 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2991 : 30210468 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2992 : : }
2993 : :
2994 : : /* Don't really know how much of the language-specific
2995 : : values we should copy from old to new. */
2996 : 15684782 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2997 : :
2998 : 15684782 : if (LANG_DECL_HAS_MIN (newdecl))
2999 : : {
3000 : 15684782 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3001 : 15684782 : if (DECL_TEMPLATE_INFO (newdecl))
3002 : : {
3003 : 624119 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
3004 : 624119 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3005 : 624119 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3006 : : /* Remember the presence of explicit specialization args. */
3007 : 1158526 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3008 : 1158526 : = TINFO_USED_TEMPLATE_ID (new_template_info);
3009 : : }
3010 : :
3011 : : /* We don't want to copy template info from a non-templated friend
3012 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3013 : 15684782 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3014 : : || !non_templated_friend_p (olddecl));
3015 : 15684782 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3016 : : }
3017 : :
3018 : 15684782 : if (DECL_DECLARES_FUNCTION_P (newdecl))
3019 : : {
3020 : : /* Only functions have these fields. */
3021 : 15271509 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3022 : 15271509 : DECL_BEFRIENDING_CLASSES (newdecl)
3023 : 15271509 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3024 : 15271509 : DECL_BEFRIENDING_CLASSES (olddecl));
3025 : : /* DECL_THUNKS is only valid for virtual functions,
3026 : : otherwise it is a DECL_FRIEND_CONTEXT. */
3027 : 15271509 : if (DECL_VIRTUAL_P (newdecl))
3028 : 660914 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3029 : 29221190 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3030 : 114728 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3031 : : }
3032 : 413273 : else if (VAR_P (newdecl))
3033 : : {
3034 : : /* Only variables have this field. */
3035 : 413209 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3036 : 194 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3037 : : }
3038 : : }
3039 : :
3040 : 15715973 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3041 : : {
3042 : 15271509 : tree parm;
3043 : :
3044 : : /* Merge parameter attributes. */
3045 : 15271509 : tree oldarg, newarg;
3046 : 15271509 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
3047 : 42692036 : oldarg && newarg;
3048 : 27420527 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
3049 : : {
3050 : 27420527 : DECL_ATTRIBUTES (newarg)
3051 : 27420527 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
3052 : 27420527 : if (lookup_attribute (NULL, "indeterminate",
3053 : 27420527 : DECL_ATTRIBUTES (newarg))
3054 : 27420578 : && !lookup_attribute (NULL, "indeterminate",
3055 : 51 : DECL_ATTRIBUTES (oldarg)))
3056 : : {
3057 : 12 : auto_diagnostic_group d;
3058 : 12 : error_at (DECL_SOURCE_LOCATION (newarg),
3059 : : "%<indeterminate%> attribute not specified "
3060 : : "for parameter %qD on the first declaration of "
3061 : : "its function", newarg);
3062 : 12 : inform (DECL_SOURCE_LOCATION (oldarg),
3063 : : "earlier declaration");
3064 : 12 : }
3065 : 27420527 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
3066 : : }
3067 : :
3068 : 15271509 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3069 : 15271509 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3070 : : {
3071 : : /* If newdecl is not a specialization, then it is not a
3072 : : template-related function at all. And that means that we
3073 : : should have exited above, returning 0. */
3074 : 579260 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3075 : :
3076 : 579260 : if (DECL_ODR_USED (olddecl))
3077 : : /* From [temp.expl.spec]:
3078 : :
3079 : : If a template, a member template or the member of a class
3080 : : template is explicitly specialized then that
3081 : : specialization shall be declared before the first use of
3082 : : that specialization that would cause an implicit
3083 : : instantiation to take place, in every translation unit in
3084 : : which such a use occurs. */
3085 : 0 : error ("explicit specialization of %qD after first use",
3086 : : olddecl);
3087 : :
3088 : 579260 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3089 : 579260 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3090 : 1158367 : && DECL_DECLARED_INLINE_P (newdecl));
3091 : :
3092 : : /* Don't propagate visibility from the template to the
3093 : : specialization here. We'll do that in determine_visibility if
3094 : : appropriate. */
3095 : 579260 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3096 : :
3097 : : /* [temp.expl.spec/14] We don't inline explicit specialization
3098 : : just because the primary template says so. */
3099 : 579260 : gcc_assert (!merge_attr);
3100 : :
3101 : 579260 : DECL_DECLARED_INLINE_P (olddecl)
3102 : 579260 : = DECL_DECLARED_INLINE_P (newdecl);
3103 : :
3104 : 579260 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
3105 : 579260 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3106 : :
3107 : 579260 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3108 : : }
3109 : 14692249 : else if (new_defines_function && DECL_INITIAL (olddecl))
3110 : : {
3111 : : /* Never inline re-defined extern inline functions.
3112 : : FIXME: this could be better handled by keeping both
3113 : : function as separate declarations. */
3114 : 78 : DECL_UNINLINABLE (newdecl) = 1;
3115 : : }
3116 : : else
3117 : : {
3118 : 14692171 : if (DECL_PENDING_INLINE_P (olddecl))
3119 : : {
3120 : 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
3121 : 6 : DECL_PENDING_INLINE_INFO (newdecl)
3122 : 12 : = DECL_PENDING_INLINE_INFO (olddecl);
3123 : : }
3124 : 14692165 : else if (DECL_PENDING_INLINE_P (newdecl))
3125 : : ;
3126 : 14692165 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3127 : 14692165 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3128 : 29384330 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3129 : :
3130 : 14692171 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3131 : :
3132 : 44076513 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3133 : 29384342 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3134 : :
3135 : 29384342 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3136 : 14692171 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3137 : 14692171 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3138 : 28965512 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3139 : : }
3140 : :
3141 : : /* Preserve abstractness on cloned [cd]tors. */
3142 : 15271509 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3143 : :
3144 : : /* Update newdecl's parms to point at olddecl. */
3145 : 48723272 : for (parm = DECL_ARGUMENTS (newdecl); parm;
3146 : 33451763 : parm = DECL_CHAIN (parm))
3147 : 33451763 : DECL_CONTEXT (parm) = olddecl;
3148 : :
3149 : 15271509 : if (! types_match)
3150 : : {
3151 : 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3152 : 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3153 : 201 : COPY_DECL_RTL (newdecl, olddecl);
3154 : : }
3155 : 15271509 : if (! types_match || new_defines_function)
3156 : : {
3157 : : /* These are the final DECL_ARGUMENTS that will be used within the
3158 : : body; update any references to old DECL_ARGUMENTS in the
3159 : : contracts, if present. */
3160 : 9191366 : if (tree contracts = DECL_CONTRACTS (newdecl))
3161 : 132 : remap_contracts (olddecl, newdecl, contracts, true);
3162 : :
3163 : : /* These need to be copied so that the names are available.
3164 : : Note that if the types do match, we'll preserve inline
3165 : : info and other bits, but if not, we won't. */
3166 : 9191366 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3167 : 9191366 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3168 : :
3169 : : /* In some cases, duplicate_contracts will remove contracts from
3170 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
3171 : : shared. If we removed them, re-add them. */
3172 : 9191366 : if (!DECL_CONTRACTS (olddecl))
3173 : 9191319 : copy_contract_attributes (olddecl, newdecl);
3174 : : }
3175 : : /* If redeclaring a builtin function, it stays built in
3176 : : if newdecl is a gnu_inline definition, or if newdecl is just
3177 : : a declaration. */
3178 : 15271509 : if (fndecl_built_in_p (olddecl)
3179 : 15330930 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3180 : : {
3181 : 4174035 : copy_decl_built_in_function (newdecl, olddecl);
3182 : : /* If we're keeping the built-in definition, keep the rtl,
3183 : : regardless of declaration matches. */
3184 : 4174035 : COPY_DECL_RTL (olddecl, newdecl);
3185 : 4174035 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3186 : : {
3187 : 4174029 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3188 : 4174029 : if (builtin_decl_explicit_p (fncode))
3189 : : {
3190 : : /* A compatible prototype of these builtin functions
3191 : : is seen, assume the runtime implements it with
3192 : : the expected semantics. */
3193 : 4174029 : switch (fncode)
3194 : : {
3195 : 10562 : case BUILT_IN_STPCPY:
3196 : 10562 : set_builtin_decl_implicit_p (fncode, true);
3197 : 10562 : break;
3198 : 4163467 : default:
3199 : 4163467 : set_builtin_decl_declared_p (fncode, true);
3200 : 4163467 : break;
3201 : : }
3202 : : }
3203 : :
3204 : 4174029 : copy_attributes_to_builtin (newdecl);
3205 : : }
3206 : : }
3207 : 15271509 : if (new_defines_function)
3208 : : /* If defining a function declared with other language
3209 : : linkage, use the previously declared language linkage. */
3210 : 9191165 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3211 : 6080344 : else if (types_match)
3212 : : {
3213 : 6080143 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3214 : : /* Don't clear out the arguments if we're just redeclaring a
3215 : : function. */
3216 : 6080143 : if (DECL_ARGUMENTS (olddecl))
3217 : : {
3218 : : /* If we removed contracts from previous definition, re-attach
3219 : : them. Otherwise, rewrite the contracts so they match the
3220 : : parameters of the new declaration. */
3221 : 1929894 : if (DECL_INITIAL (olddecl)
3222 : 11688 : && DECL_CONTRACTS (newdecl)
3223 : 1929905 : && !DECL_CONTRACTS (olddecl))
3224 : 10 : copy_contract_attributes (olddecl, newdecl);
3225 : : else
3226 : : {
3227 : : /* Temporarily undo the re-contexting of parameters so we can
3228 : : actually remap parameters. The inliner won't replace
3229 : : parameters if we don't do this. */
3230 : 1929884 : tree args = DECL_ARGUMENTS (newdecl);
3231 : 6155271 : for (tree p = args; p; p = DECL_CHAIN (p))
3232 : 4225387 : DECL_CONTEXT (p) = newdecl;
3233 : :
3234 : : /* Save new argument names for use in contracts parsing,
3235 : : unless we've already started parsing the body of olddecl
3236 : : (particular issues arise when newdecl is from a prior
3237 : : friend decl with no argument names, see
3238 : : modules/contracts-tpl-friend-1). */
3239 : 1929884 : if (tree contracts = DECL_CONTRACTS (olddecl))
3240 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3241 : :
3242 : : /* And reverse this operation again. */
3243 : 6155271 : for (tree p = args; p; p = DECL_CHAIN (p))
3244 : 4225387 : DECL_CONTEXT (p) = olddecl;
3245 : : }
3246 : :
3247 : 1929894 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3248 : : }
3249 : : }
3250 : : }
3251 : 444464 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3252 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3253 : :
3254 : : /* Now preserve various other info from the definition. */
3255 : 15715973 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3256 : 15715973 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3257 : 15715973 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3258 : 15715973 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3259 : :
3260 : : /* Warn about conflicting visibility specifications. */
3261 : 15715973 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3262 : 5549871 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3263 : 15716520 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3264 : : {
3265 : 12 : auto_diagnostic_group d;
3266 : 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3267 : : "%qD: visibility attribute ignored because it "
3268 : : "conflicts with previous declaration", newdecl))
3269 : 12 : inform (olddecl_loc,
3270 : : "previous declaration of %qD", olddecl);
3271 : 12 : }
3272 : : /* Choose the declaration which specified visibility. */
3273 : 15715973 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3274 : : {
3275 : 5549871 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3276 : 5549871 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3277 : : }
3278 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3279 : : so keep this behavior. */
3280 : 15715973 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3281 : : {
3282 : 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3283 : 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3284 : : }
3285 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3286 : 15715973 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3287 : : {
3288 : 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3289 : 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3290 : : }
3291 : 15715919 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3292 : 15715919 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3293 : 3 : DECL_USER_ALIGN (newdecl) = 1;
3294 : :
3295 : 15715973 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3296 : 31431946 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3297 : 15715973 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3298 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3299 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3300 : 15715973 : if (TREE_CODE (newdecl) == FIELD_DECL)
3301 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3302 : :
3303 : : /* Merge module entity mapping information. */
3304 : 15715973 : if (DECL_LANG_SPECIFIC (olddecl)
3305 : 31400801 : && (DECL_MODULE_ENTITY_P (olddecl)
3306 : 15684532 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3307 : : {
3308 : 296 : retrofit_lang_decl (newdecl);
3309 : 296 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3310 : 296 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3311 : : }
3312 : :
3313 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3314 : : with that from NEWDECL below. */
3315 : 15715973 : if (DECL_LANG_SPECIFIC (olddecl))
3316 : : {
3317 : 15684828 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3318 : : != DECL_LANG_SPECIFIC (newdecl));
3319 : 15684828 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3320 : : }
3321 : :
3322 : : /* Merge the USED information. */
3323 : 15715973 : if (TREE_USED (olddecl))
3324 : 501635 : TREE_USED (newdecl) = 1;
3325 : 15214338 : else if (TREE_USED (newdecl))
3326 : 54 : TREE_USED (olddecl) = 1;
3327 : :
3328 : 15715973 : if (VAR_P (newdecl))
3329 : : {
3330 : 416832 : if (DECL_READ_P (olddecl))
3331 : 36027 : DECL_READ_P (newdecl) = 1;
3332 : 380805 : else if (DECL_READ_P (newdecl))
3333 : 3 : DECL_READ_P (olddecl) = 1;
3334 : : }
3335 : :
3336 : 15715973 : if (DECL_PRESERVE_P (olddecl))
3337 : 48 : DECL_PRESERVE_P (newdecl) = 1;
3338 : 15715925 : else if (DECL_PRESERVE_P (newdecl))
3339 : 63 : DECL_PRESERVE_P (olddecl) = 1;
3340 : :
3341 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3342 : : to olddecl and deleted. */
3343 : 15715973 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3344 : 30987482 : && DECL_FUNCTION_VERSIONED (olddecl))
3345 : : {
3346 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3347 : 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3348 : : /* newdecl will be purged after copying to olddecl and is no longer
3349 : : a version. */
3350 : 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3351 : : }
3352 : :
3353 : 15715973 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3354 : : {
3355 : 15271509 : int function_size;
3356 : 15271509 : struct symtab_node *snode = symtab_node::get (olddecl);
3357 : :
3358 : 15271509 : function_size = sizeof (struct tree_decl_common);
3359 : :
3360 : 15271509 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3361 : : (char *) newdecl + sizeof (struct tree_common),
3362 : : function_size - sizeof (struct tree_common));
3363 : :
3364 : 15271509 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3365 : : (char *) newdecl + sizeof (struct tree_decl_common),
3366 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3367 : :
3368 : : /* Preserve symtab node mapping. */
3369 : 15271509 : olddecl->decl_with_vis.symtab_node = snode;
3370 : :
3371 : 15271509 : if (new_template_info)
3372 : : /* If newdecl is a template instantiation, it is possible that
3373 : : the following sequence of events has occurred:
3374 : :
3375 : : o A friend function was declared in a class template. The
3376 : : class template was instantiated.
3377 : :
3378 : : o The instantiation of the friend declaration was
3379 : : recorded on the instantiation list, and is newdecl.
3380 : :
3381 : : o Later, however, instantiate_class_template called pushdecl
3382 : : on the newdecl to perform name injection. But, pushdecl in
3383 : : turn called duplicate_decls when it discovered that another
3384 : : declaration of a global function with the same name already
3385 : : existed.
3386 : :
3387 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3388 : :
3389 : : If we're going to do that, we'd better make sure that
3390 : : olddecl, and not newdecl, is on the list of
3391 : : instantiations so that if we try to do the instantiation
3392 : : again we won't get the clobbered declaration. */
3393 : 624110 : reregister_specialization (newdecl,
3394 : : new_template_info,
3395 : : olddecl);
3396 : : }
3397 : : else
3398 : : {
3399 : 444464 : size_t size = tree_code_size (TREE_CODE (newdecl));
3400 : :
3401 : 444464 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3402 : : (char *) newdecl + sizeof (struct tree_common),
3403 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3404 : :
3405 : 444464 : switch (TREE_CODE (newdecl))
3406 : : {
3407 : 444464 : case LABEL_DECL:
3408 : 444464 : case VAR_DECL:
3409 : 444464 : case RESULT_DECL:
3410 : 444464 : case PARM_DECL:
3411 : 444464 : case FIELD_DECL:
3412 : 444464 : case TYPE_DECL:
3413 : 444464 : case CONST_DECL:
3414 : 444464 : {
3415 : 444464 : struct symtab_node *snode = NULL;
3416 : :
3417 : 444464 : if (VAR_P (newdecl)
3418 : 444464 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3419 : 3 : || DECL_EXTERNAL (olddecl)))
3420 : 416832 : snode = symtab_node::get (olddecl);
3421 : 444464 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3422 : : (char *) newdecl + sizeof (struct tree_decl_common),
3423 : : size - sizeof (struct tree_decl_common)
3424 : 444464 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3425 : 444464 : if (VAR_P (newdecl))
3426 : 416832 : olddecl->decl_with_vis.symtab_node = snode;
3427 : : }
3428 : : break;
3429 : 0 : default:
3430 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3431 : : (char *) newdecl + sizeof (struct tree_decl_common),
3432 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3433 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3434 : 0 : break;
3435 : : }
3436 : : }
3437 : :
3438 : 15715973 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3439 : : {
3440 : 15688341 : if (DECL_EXTERNAL (olddecl)
3441 : 418195 : || TREE_PUBLIC (olddecl)
3442 : 15688410 : || TREE_STATIC (olddecl))
3443 : : {
3444 : : /* Merge the section attribute.
3445 : : We want to issue an error if the sections conflict but that must be
3446 : : done later in decl_attributes since we are called before attributes
3447 : : are assigned. */
3448 : 15688341 : if (DECL_SECTION_NAME (newdecl) != NULL)
3449 : 7 : set_decl_section_name (olddecl, newdecl);
3450 : :
3451 : 15688341 : if (DECL_ONE_ONLY (newdecl))
3452 : : {
3453 : 0 : struct symtab_node *oldsym, *newsym;
3454 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3455 : 0 : oldsym = cgraph_node::get_create (olddecl);
3456 : : else
3457 : 0 : oldsym = varpool_node::get_create (olddecl);
3458 : 0 : newsym = symtab_node::get (newdecl);
3459 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3460 : : }
3461 : : }
3462 : :
3463 : 15688341 : if (VAR_P (newdecl)
3464 : 15688341 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3465 : : {
3466 : 404 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3467 : 404 : if (!processing_template_decl)
3468 : 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3469 : : }
3470 : : }
3471 : :
3472 : 15715973 : DECL_UID (olddecl) = olddecl_uid;
3473 : :
3474 : : /* NEWDECL contains the merged attribute lists.
3475 : : Update OLDDECL to be the same. */
3476 : 15715973 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3477 : :
3478 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3479 : : so that encode_section_info has a chance to look at the new decl
3480 : : flags and attributes. */
3481 : 15715973 : if (DECL_RTL_SET_P (olddecl)
3482 : 15715973 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3483 : 0 : || (VAR_P (olddecl)
3484 : 0 : && TREE_STATIC (olddecl))))
3485 : 0 : make_decl_rtl (olddecl);
3486 : :
3487 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3488 : : declaration of a member results in a call to duplicate_decls,
3489 : : freeing these nodes represents in a significant savings.
3490 : :
3491 : : Before releasing the node, be sore to remove function from symbol
3492 : : table that might have been inserted there to record comdat group.
3493 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3494 : : structure is shared in between newdecl and oldecl. */
3495 : 15715973 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3496 : 15271509 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3497 : 15715973 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3498 : : {
3499 : 15688341 : struct symtab_node *snode = symtab_node::get (newdecl);
3500 : 15688341 : if (snode)
3501 : 399 : snode->remove ();
3502 : : }
3503 : :
3504 : 15715973 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3505 : : {
3506 : 15271509 : tree clone;
3507 : 15797809 : FOR_EACH_CLONE (clone, olddecl)
3508 : : {
3509 : 526300 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3510 : 526300 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3511 : : }
3512 : : }
3513 : :
3514 : : /* Remove the associated constraints for newdecl, if any, before
3515 : : reclaiming memory. */
3516 : 15715973 : if (flag_concepts)
3517 : 4831308 : remove_constraints (newdecl);
3518 : :
3519 : : /* And similarly for any module tracking data. */
3520 : 15715973 : if (modules_p ())
3521 : 32439 : remove_defining_module (newdecl);
3522 : :
3523 : 15715973 : ggc_free (newdecl);
3524 : :
3525 : 15715973 : return olddecl;
3526 : : }
3527 : :
3528 : : /* Return zero if the declaration NEWDECL is valid
3529 : : when the declaration OLDDECL (assumed to be for the same name)
3530 : : has already been seen.
3531 : : Otherwise return an error message format string with a %s
3532 : : where the identifier should go. */
3533 : :
3534 : : static const char *
3535 : 15324218 : redeclaration_error_message (tree newdecl, tree olddecl)
3536 : : {
3537 : 15324254 : if (TREE_CODE (newdecl) == TYPE_DECL)
3538 : : {
3539 : : /* Because C++ can put things into name space for free,
3540 : : constructs like "typedef struct foo { ... } foo"
3541 : : would look like an erroneous redeclaration. */
3542 : 59688 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3543 : : return NULL;
3544 : : else
3545 : : return G_("redefinition of %q#D");
3546 : : }
3547 : 15264566 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3548 : : {
3549 : : /* If this is a pure function, its olddecl will actually be
3550 : : the original initialization to `0' (which we force to call
3551 : : abort()). Don't complain about redefinition in this case. */
3552 : 22394744 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3553 : 11197390 : && DECL_INITIAL (olddecl) == NULL_TREE)
3554 : : return NULL;
3555 : :
3556 : : /* If both functions come from different namespaces, this is not
3557 : : a redeclaration - this is a conflict with a used function. */
3558 : 22394714 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3559 : 2329167 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3560 : 11197360 : && ! decls_match (olddecl, newdecl))
3561 : : return G_("%qD conflicts with used function");
3562 : :
3563 : : /* We'll complain about linkage mismatches in
3564 : : warn_extern_redeclared_static. */
3565 : :
3566 : : /* Defining the same name twice is no good. */
3567 : 11197357 : if (decl_defined_p (olddecl)
3568 : 11197357 : && decl_defined_p (newdecl))
3569 : : {
3570 : 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3571 : : return G_("%q#D not declared in class");
3572 : 444 : else if (!GNU_INLINE_P (olddecl)
3573 : 390 : || GNU_INLINE_P (newdecl))
3574 : 219 : return G_("redefinition of %q#D");
3575 : : }
3576 : :
3577 : 12047183 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3578 : : {
3579 : 803095 : bool olda = GNU_INLINE_P (olddecl);
3580 : 803095 : bool newa = GNU_INLINE_P (newdecl);
3581 : :
3582 : 803095 : if (olda != newa)
3583 : : {
3584 : 15 : if (newa)
3585 : : return G_("%q+D redeclared inline with "
3586 : : "%<gnu_inline%> attribute");
3587 : : else
3588 : : return G_("%q+D redeclared inline without "
3589 : : "%<gnu_inline%> attribute");
3590 : : }
3591 : : }
3592 : :
3593 : : /* [class.compare.default]: A definition of a comparison operator as
3594 : : defaulted that appears in a class shall be the first declaration of
3595 : : that function. */
3596 : 11197123 : special_function_kind sfk = special_function_p (olddecl);
3597 : 11197123 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3598 : : return G_("comparison operator %q+D defaulted after "
3599 : : "its first declaration");
3600 : :
3601 : 11197120 : check_abi_tag_redeclaration
3602 : 11197120 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3603 : 11197120 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3604 : :
3605 : 11197120 : return NULL;
3606 : : }
3607 : 4067194 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3608 : : {
3609 : 3650000 : tree nt, ot;
3610 : :
3611 : 3650000 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3612 : : return G_("redefinition of %q#D");
3613 : :
3614 : 3649994 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3615 : 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3616 : 36 : DECL_TEMPLATE_RESULT (olddecl));
3617 : :
3618 : 3649958 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3619 : : return NULL;
3620 : :
3621 : 3649946 : nt = DECL_TEMPLATE_RESULT (newdecl);
3622 : 3649946 : if (DECL_TEMPLATE_INFO (nt))
3623 : 561324 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3624 : 3649946 : ot = DECL_TEMPLATE_RESULT (olddecl);
3625 : 3649946 : if (DECL_TEMPLATE_INFO (ot))
3626 : 3649946 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3627 : 6405458 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3628 : 3649985 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3629 : 15 : return G_("redefinition of %q#D");
3630 : :
3631 : 5056056 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3632 : : {
3633 : 916591 : bool olda = GNU_INLINE_P (ot);
3634 : 916591 : bool newa = GNU_INLINE_P (nt);
3635 : :
3636 : 916591 : if (olda != newa)
3637 : : {
3638 : 0 : if (newa)
3639 : : return G_("%q+D redeclared inline with "
3640 : : "%<gnu_inline%> attribute");
3641 : : else
3642 : : return G_("%q+D redeclared inline without "
3643 : : "%<gnu_inline%> attribute");
3644 : : }
3645 : : }
3646 : :
3647 : : /* Core issue #226 (C++11):
3648 : :
3649 : : If a friend function template declaration specifies a
3650 : : default template-argument, that declaration shall be a
3651 : : definition and shall be the only declaration of the
3652 : : function template in the translation unit. */
3653 : 3649931 : if ((cxx_dialect != cxx98)
3654 : 3633754 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3655 : 3867909 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3656 : : /*is_primary=*/true,
3657 : : /*is_partial=*/false,
3658 : : /*is_friend_decl=*/2))
3659 : : return G_("redeclaration of friend %q#D "
3660 : : "may not have default template arguments");
3661 : :
3662 : 3649928 : return NULL;
3663 : : }
3664 : 417194 : else if (VAR_P (newdecl)
3665 : 417110 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3666 : 417110 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3667 : 417241 : && (! DECL_LANG_SPECIFIC (olddecl)
3668 : 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3669 : 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3670 : : {
3671 : : /* Only variables can be thread-local, and all declarations must
3672 : : agree on this property. */
3673 : 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3674 : : return G_("thread-local declaration of %q#D follows "
3675 : : "non-thread-local declaration");
3676 : : else
3677 : 3 : return G_("non-thread-local declaration of %q#D follows "
3678 : : "thread-local declaration");
3679 : : }
3680 : 417188 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3681 : : {
3682 : : /* The objects have been declared at namespace scope. If either
3683 : : is a member of an anonymous union, then this is an invalid
3684 : : redeclaration. For example:
3685 : :
3686 : : int i;
3687 : : union { int i; };
3688 : :
3689 : : is invalid. */
3690 : 416959 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3691 : 833987 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3692 : : return G_("redeclaration of %q#D");
3693 : : /* If at least one declaration is a reference, there is no
3694 : : conflict. For example:
3695 : :
3696 : : int i = 3;
3697 : : extern int i;
3698 : :
3699 : : is valid. */
3700 : 417025 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3701 : : return NULL;
3702 : :
3703 : : /* Static data member declared outside a class definition
3704 : : if the variable is defined within the class with constexpr
3705 : : specifier is declaration rather than definition (and
3706 : : deprecated). */
3707 : 2824 : if (cxx_dialect >= cxx17
3708 : 2749 : && VAR_P (olddecl)
3709 : 2693 : && DECL_CLASS_SCOPE_P (olddecl)
3710 : 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3711 : 5461 : && !DECL_INITIAL (newdecl))
3712 : : {
3713 : 2637 : DECL_EXTERNAL (newdecl) = 1;
3714 : : /* For now, only warn with explicit -Wdeprecated. */
3715 : 2637 : if (OPTION_SET_P (warn_deprecated))
3716 : : {
3717 : 40 : auto_diagnostic_group d;
3718 : 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3719 : : "redundant redeclaration of %<constexpr%> "
3720 : : "static data member %qD", newdecl))
3721 : 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3722 : : "previous declaration of %qD", olddecl);
3723 : 40 : }
3724 : 2637 : return NULL;
3725 : : }
3726 : :
3727 : : /* Reject two definitions. */
3728 : : return G_("redefinition of %q#D");
3729 : : }
3730 : : else
3731 : : {
3732 : : /* Objects declared with block scope: */
3733 : : /* Reject two definitions, and reject a definition
3734 : : together with an external reference. */
3735 : 148 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3736 : : return G_("redeclaration of %q#D");
3737 : : return NULL;
3738 : : }
3739 : : }
3740 : :
3741 : :
3742 : : /* Hash and equality functions for the named_label table. */
3743 : :
3744 : : hashval_t
3745 : 77602 : named_label_hash::hash (const value_type entry)
3746 : : {
3747 : 77602 : return IDENTIFIER_HASH_VALUE (entry->name);
3748 : : }
3749 : :
3750 : : bool
3751 : 74664 : named_label_hash::equal (const value_type entry, compare_type name)
3752 : : {
3753 : 74664 : return name == entry->name;
3754 : : }
3755 : :
3756 : : /* Look for a label named ID in the current function. If one cannot
3757 : : be found, create one. Return the named_label_entry, or NULL on
3758 : : failure. */
3759 : :
3760 : : static named_label_entry *
3761 : 23827 : lookup_label_1 (tree id, bool making_local_p)
3762 : : {
3763 : 23827 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3764 : :
3765 : : /* You can't use labels at global scope. */
3766 : 23827 : if (current_function_decl == NULL_TREE)
3767 : : {
3768 : 3 : error ("label %qE referenced outside of any function", id);
3769 : 3 : return NULL;
3770 : : }
3771 : :
3772 : 23824 : if (!named_labels)
3773 : 4592 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3774 : :
3775 : 23824 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3776 : 23824 : named_label_entry **slot
3777 : 23824 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3778 : 23824 : named_label_entry *old = *slot;
3779 : :
3780 : 23824 : if (old && old->label_decl)
3781 : : {
3782 : 3454 : if (!making_local_p)
3783 : : return old;
3784 : :
3785 : 21 : if (old->binding_level == current_binding_level)
3786 : : {
3787 : 0 : auto_diagnostic_group d;
3788 : 0 : error ("local label %qE conflicts with existing label", id);
3789 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3790 : 0 : return NULL;
3791 : 0 : }
3792 : : }
3793 : :
3794 : : /* We are making a new decl, create or reuse the named_label_entry */
3795 : 20412 : named_label_entry *ent = NULL;
3796 : 33 : if (old && !old->label_decl)
3797 : : ent = old;
3798 : : else
3799 : : {
3800 : 20379 : ent = ggc_cleared_alloc<named_label_entry> ();
3801 : 20379 : ent->name = id;
3802 : 20379 : ent->outer = old;
3803 : 20379 : *slot = ent;
3804 : : }
3805 : :
3806 : : /* Now create the LABEL_DECL. */
3807 : 20391 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3808 : :
3809 : 20391 : DECL_CONTEXT (decl) = current_function_decl;
3810 : 20391 : SET_DECL_MODE (decl, VOIDmode);
3811 : 20391 : if (making_local_p)
3812 : : {
3813 : 219 : C_DECLARED_LABEL_FLAG (decl) = true;
3814 : 219 : DECL_CHAIN (decl) = current_binding_level->names;
3815 : 219 : current_binding_level->names = decl;
3816 : : }
3817 : :
3818 : 20391 : ent->label_decl = decl;
3819 : :
3820 : 20391 : return ent;
3821 : 23827 : }
3822 : :
3823 : : /* Wrapper for lookup_label_1. */
3824 : :
3825 : : tree
3826 : 2951 : lookup_label (tree id)
3827 : : {
3828 : 2951 : named_label_entry *ent = lookup_label_1 (id, false);
3829 : 2951 : return ent ? ent->label_decl : NULL_TREE;
3830 : : }
3831 : :
3832 : : /* Remember that we've seen &&ID. */
3833 : :
3834 : : void
3835 : 250 : mark_label_addressed (tree id)
3836 : : {
3837 : 250 : named_label_entry *ent = lookup_label_1 (id, false);
3838 : 250 : ent->addressed = true;
3839 : 250 : }
3840 : :
3841 : : tree
3842 : 219 : declare_local_label (tree id)
3843 : : {
3844 : 219 : named_label_entry *ent = lookup_label_1 (id, true);
3845 : 219 : return ent ? ent->label_decl : NULL_TREE;
3846 : : }
3847 : :
3848 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3849 : :
3850 : : static bool
3851 : 145104 : decl_jump_unsafe (tree decl)
3852 : : {
3853 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3854 : : with automatic storage duration is not in scope to a point where it is
3855 : : in scope is ill-formed unless the variable has scalar type, class type
3856 : : with a trivial default constructor and a trivial destructor, a
3857 : : cv-qualified version of one of these types, or an array of one of the
3858 : : preceding types and is declared without an initializer (8.5). */
3859 : 145104 : tree type = TREE_TYPE (decl);
3860 : :
3861 : 145104 : return (type != error_mark_node
3862 : 145086 : && VAR_P (decl)
3863 : 15020 : && !TREE_STATIC (decl)
3864 : 159988 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3865 : 12980 : || variably_modified_type_p (type, NULL_TREE)));
3866 : : }
3867 : :
3868 : : /* Returns true if decl is an automatic variable with vacuous initialization
3869 : : except when it is [[indeterminate]] or [[gnu::uninitialized]].
3870 : : Jumps across such initialization need to be instrumented for
3871 : : !!flag_auto_var_init. */
3872 : :
3873 : : static bool
3874 : 143191 : decl_instrument_init_bypass_p (tree decl)
3875 : : {
3876 : 143191 : tree type = TREE_TYPE (decl);
3877 : :
3878 : 143191 : return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
3879 : 66001 : && !processing_template_decl
3880 : 65755 : && type != error_mark_node
3881 : 65749 : && VAR_P (decl)
3882 : 1050 : && !TREE_STATIC (decl)
3883 : 1029 : && !DECL_EXTERNAL (decl)
3884 : 2030 : && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3885 : 1015 : || variably_modified_type_p (type, NULL_TREE))
3886 : 1015 : && !lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (decl))
3887 : 1015 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
3888 : 144206 : && !DECL_HAS_VALUE_EXPR_P (decl));
3889 : : }
3890 : :
3891 : : /* Build .DEFERRED_INIT call for DECL. */
3892 : :
3893 : : static tree
3894 : 133 : build_deferred_init_call (tree decl)
3895 : : {
3896 : 133 : tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
3897 : 266 : tree init_type_arg = build_int_cst (integer_type_node,
3898 : 133 : (int) flag_auto_var_init);
3899 : 133 : location_t loc = DECL_SOURCE_LOCATION (decl);
3900 : 133 : tree decl_name;
3901 : :
3902 : 133 : if (DECL_NAME (decl))
3903 : 133 : decl_name = build_string_literal (DECL_NAME (decl));
3904 : : else
3905 : : {
3906 : 0 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
3907 : 0 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
3908 : 0 : decl_name = build_string_literal (decl_name_anonymous);
3909 : : }
3910 : :
3911 : 133 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
3912 : 133 : TREE_TYPE (decl), 3,
3913 : : decl_size_arg, init_type_arg,
3914 : : decl_name);
3915 : 133 : tree ret = build2_loc (loc, MODIFY_EXPR, void_type_node, decl, call);
3916 : 133 : return build_stmt (loc, EXPR_STMT, ret);
3917 : : }
3918 : :
3919 : : /* Emit before ITER (and any labels/case labels before it) code like
3920 : : if (0)
3921 : : {
3922 : : l1:
3923 : : v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
3924 : : v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
3925 : : v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
3926 : : v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
3927 : : }
3928 : : and return l1 label, or if it already exists, assert it has the
3929 : : .DEFERRED_INIT calls for the right decls in the right order and
3930 : : amend it, either by adding extra labels in between or further
3931 : : ,DEFERRED_INIT calls before the first label and extra label before
3932 : : that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
3933 : : of adding a label. DECLS points to an array of NDECLS VAR_DECLs
3934 : : which should be initialized. */
3935 : :
3936 : : static tree
3937 : 121 : maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
3938 : : tree *decls, unsigned ndecls)
3939 : : {
3940 : 121 : tree lab = NULL_TREE;
3941 : 185 : for (; !tsi_end_p (iter); tsi_prev (&iter))
3942 : : {
3943 : 179 : switch (TREE_CODE (tsi_stmt (iter)))
3944 : : {
3945 : 64 : case LABEL_EXPR:
3946 : 64 : case CASE_LABEL_EXPR:
3947 : 64 : case DEBUG_BEGIN_STMT:
3948 : 64 : continue;
3949 : : default:
3950 : : break;
3951 : : }
3952 : : break;
3953 : : }
3954 : 121 : if (!tsi_end_p (iter)
3955 : 115 : && TREE_CODE (tsi_stmt (iter)) == IF_STMT
3956 : 197 : && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
3957 : : {
3958 : : /* Found IF_STMT added for this or some adjacent
3959 : : LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
3960 : : The decls are ordered so that we can always reuse it. Sometimes
3961 : : by no modifications at all and just returning the right label
3962 : : which was added already before, sometimes by adding a label in
3963 : : between two previously added .DEFERRED_INIT calls and sometimes
3964 : : by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
3965 : : before that) before the statements in IF_STMT body. */
3966 : 70 : tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
3967 : 70 : iter = tsi_last (then_clause);
3968 : 70 : bool add = false;
3969 : 378 : for (unsigned int i = 0; i < ndecls; ++i)
3970 : : {
3971 : 308 : tree decl = decls[i];
3972 : 308 : if (!add)
3973 : : {
3974 : : /* Skip over labels/case labels after .DEFERRED_INIT for the
3975 : : DECL we are looking for. */
3976 : 396 : while (!tsi_end_p (iter)
3977 : 396 : && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
3978 : 284 : || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
3979 : 25 : && !case_label)))
3980 : 118 : tsi_prev (&iter);
3981 : 278 : if (tsi_end_p (iter))
3982 : : {
3983 : : /* Reached the start, we'll need to prepend further
3984 : : statements. */
3985 : 19 : add = true;
3986 : 19 : iter = tsi_start (then_clause);
3987 : : }
3988 : : else
3989 : : {
3990 : : /* Found something, assert it is .DEFERRED_INIT for
3991 : : DECL. */
3992 : 259 : tree t = tsi_stmt (iter);
3993 : 259 : gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
3994 : 259 : t = EXPR_STMT_EXPR (t);
3995 : 259 : gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
3996 : : && TREE_OPERAND (t, 0) == decl
3997 : : && (TREE_CODE (TREE_OPERAND (t, 1))
3998 : : == CALL_EXPR));
3999 : 259 : t = TREE_OPERAND (t, 1);
4000 : 259 : gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
4001 : : && (CALL_EXPR_IFN (t)
4002 : : == IFN_DEFERRED_INIT));
4003 : 259 : tsi_prev (&iter);
4004 : : }
4005 : : }
4006 : 278 : if (add)
4007 : : {
4008 : : /* If reached the start in this or some earlier iteration,
4009 : : prepend .DEFERRED_INIT call for DECL. */
4010 : 49 : tree t = build_deferred_init_call (decl);
4011 : 49 : STMT_IS_FULL_EXPR_P (t) = 1;
4012 : 49 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4013 : : }
4014 : : }
4015 : 70 : if (!add)
4016 : : {
4017 : : /* If .DEFERRED_INIT calls for all the decls were already there,
4018 : : skip over case labels and if we find a LABEL_EXPR, return
4019 : : its label. */
4020 : 63 : while (!tsi_end_p (iter)
4021 : 63 : && !case_label
4022 : 117 : && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4023 : 12 : tsi_prev (&iter);
4024 : 51 : if (tsi_end_p (iter))
4025 : : {
4026 : : /* Only case labels were found and we are looking for normal
4027 : : label, we'll need to add it. */
4028 : 0 : add = true;
4029 : 0 : iter = tsi_start (then_clause);
4030 : : }
4031 : 51 : else if (!case_label
4032 : 51 : && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4033 : : /* Return existing label. */
4034 : 21 : lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4035 : : else
4036 : : {
4037 : : /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4038 : 30 : gcc_checking_assert (case_label
4039 : : || (TREE_CODE (tsi_stmt (iter))
4040 : : == EXPR_STMT));
4041 : 30 : add = true;
4042 : 30 : tsi_next (&iter);
4043 : 30 : gcc_checking_assert (!tsi_end_p (iter));
4044 : : }
4045 : : }
4046 : 21 : if (add)
4047 : : {
4048 : 49 : tree t;
4049 : 49 : if (case_label)
4050 : : t = case_label;
4051 : : else
4052 : : {
4053 : 40 : lab = create_artificial_label (UNKNOWN_LOCATION);
4054 : 40 : t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4055 : : }
4056 : 49 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4057 : : }
4058 : : }
4059 : : else
4060 : : {
4061 : : /* No IF_STMT created by this function found. Create it all
4062 : : from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4063 : : followed by .DEFERRED_INIT calls inside of a new if (0). */
4064 : 51 : tree new_then = push_stmt_list ();
4065 : 51 : if (!case_label)
4066 : : {
4067 : 22 : lab = create_artificial_label (UNKNOWN_LOCATION);
4068 : 22 : add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4069 : : }
4070 : : else
4071 : 29 : add_stmt (case_label);
4072 : 135 : for (unsigned int i = ndecls; i; --i)
4073 : 84 : add_stmt (build_deferred_init_call (decls[i - 1]));
4074 : 51 : new_then = pop_stmt_list (new_then);
4075 : 51 : tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4076 : : new_then, void_node, NULL_TREE);
4077 : 51 : IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4078 : 51 : if (tsi_end_p (iter))
4079 : : {
4080 : 6 : iter = tsi_start (iter.container);
4081 : 6 : tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4082 : : }
4083 : : else
4084 : 45 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4085 : : }
4086 : 121 : return lab;
4087 : : }
4088 : :
4089 : : /* Adjust backward gotos to named label ENT if they jump over vacuous
4090 : : initializers if !!flag_auto_var_init. ITER is the location of
4091 : : LABEL_EXPR for that named label. */
4092 : :
4093 : : static void
4094 : 19 : adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4095 : : {
4096 : 19 : auto_vec<tree, 4> decls;
4097 : 19 : unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4098 : 19 : tree decl;
4099 : 88 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4100 : 88 : if (!decl_jump_unsafe (decl))
4101 : : {
4102 : 88 : gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4103 : 88 : decls.safe_push (decl);
4104 : 176 : if (decls.length () == max_cnt)
4105 : : break;
4106 : : }
4107 : 19 : named_label_bck_direct_goto *dgoto;
4108 : 19 : unsigned last = 0;
4109 : 19 : tree lab = NULL_TREE;
4110 : 103 : FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4111 : : {
4112 : 46 : if (dgoto->n_bad_decls != last)
4113 : : {
4114 : 40 : last = dgoto->n_bad_decls;
4115 : 80 : lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4116 : : decls.address (), last);
4117 : : }
4118 : 46 : *dgoto->direct_goto = lab;
4119 : : }
4120 : 19 : }
4121 : :
4122 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4123 : : to the user. */
4124 : :
4125 : : static bool
4126 : 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
4127 : : enum diagnostics::kind diag_kind, bool computed)
4128 : : {
4129 : 286 : if (computed)
4130 : 43 : diag_kind = diagnostics::kind::warning;
4131 : 286 : bool complained
4132 : 373 : = emit_diagnostic (diag_kind, loc, 0,
4133 : : decl ? G_("jump to label %qD")
4134 : : : G_("jump to case label"), decl);
4135 : 286 : if (complained && locus)
4136 : : {
4137 : 199 : if (computed)
4138 : 43 : inform (*locus, " as a possible target of computed goto");
4139 : : else
4140 : 156 : inform (*locus, " from here");
4141 : : }
4142 : 286 : return complained;
4143 : : }
4144 : :
4145 : : /* Check that a single previously seen jump to a newly defined label
4146 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4147 : : the jump context; NAMES are the names in scope in LEVEL at the jump
4148 : : context; LOCUS is the source position of the jump or 0. COMPUTED
4149 : : is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4150 : : vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4151 : : initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4152 : : CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4153 : : Returns non-zero if all is well, 2 if any vacuous initializers were
4154 : : crossed. */
4155 : :
4156 : : static int
4157 : 2127739 : check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4158 : : bool exited_omp, const location_t *locus,
4159 : : vec<tree, va_gc> *computed,
4160 : : vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4161 : : tree case_label)
4162 : : {
4163 : 2127739 : auto_diagnostic_group d;
4164 : 2127739 : cp_binding_level *b;
4165 : 2127739 : bool complained = false;
4166 : 2127739 : int identified = 0;
4167 : 2127739 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4168 : 2127739 : bool saw_ceif = false, saw_se = false;
4169 : 2127739 : auto_vec<tree> vacuous_decls;
4170 : 2127739 : bool vacuous_inits = false;
4171 : :
4172 : 2127739 : if (exited_omp)
4173 : : {
4174 : 12 : complained = identify_goto (decl, input_location, locus,
4175 : : diagnostics::kind::error,
4176 : : computed);
4177 : 12 : if (complained)
4178 : 12 : inform (input_location, " exits OpenMP structured block");
4179 : : saw_omp = true;
4180 : : identified = 2;
4181 : : }
4182 : :
4183 : 6354612 : for (b = current_binding_level; b ; b = b->level_chain)
4184 : : {
4185 : 4226873 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4186 : :
4187 : 4366812 : for (new_decls = b->names; new_decls != old_decls;
4188 : 269202 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4189 : 129263 : : TREE_CHAIN (new_decls)))
4190 : : {
4191 : 139939 : bool problem = decl_jump_unsafe (new_decls);
4192 : 139939 : if (! problem)
4193 : : {
4194 : 139894 : if (decl_instrument_init_bypass_p (new_decls))
4195 : : {
4196 : 311 : if (direct_goto || case_label)
4197 : 246 : vacuous_decls.safe_push (new_decls);
4198 : : vacuous_inits = true;
4199 : : }
4200 : 139894 : continue;
4201 : : }
4202 : :
4203 : 45 : if (!identified)
4204 : : {
4205 : 45 : complained = identify_goto (decl, input_location, locus,
4206 : : diagnostics::kind::error,
4207 : : computed);
4208 : 45 : identified = 2;
4209 : : }
4210 : 45 : if (complained)
4211 : 45 : inform (DECL_SOURCE_LOCATION (new_decls),
4212 : : " crosses initialization of %q#D", new_decls);
4213 : : }
4214 : :
4215 : 4226873 : if (b == level)
4216 : : break;
4217 : :
4218 : 2099134 : const char *inf = NULL;
4219 : 2099134 : location_t loc = input_location;
4220 : 2099134 : switch (b->kind)
4221 : : {
4222 : 3 : case sk_try:
4223 : 3 : if (!saw_eh)
4224 : : inf = G_(" enters %<try%> block");
4225 : : saw_eh = true;
4226 : : break;
4227 : :
4228 : 9 : case sk_catch:
4229 : 9 : if (!saw_eh)
4230 : : inf = G_(" enters %<catch%> block");
4231 : : saw_eh = true;
4232 : : break;
4233 : :
4234 : 102 : case sk_omp:
4235 : 102 : if (!saw_omp)
4236 : : inf = G_(" enters OpenMP structured block");
4237 : : saw_omp = true;
4238 : : break;
4239 : :
4240 : 3 : case sk_transaction:
4241 : 3 : if (!saw_tm)
4242 : : inf = G_(" enters synchronized or atomic statement");
4243 : : saw_tm = true;
4244 : : break;
4245 : :
4246 : 18 : case sk_stmt_expr:
4247 : 18 : if (!saw_se)
4248 : : inf = G_(" enters statement expression");
4249 : : saw_se = true;
4250 : : break;
4251 : :
4252 : 2098864 : case sk_block:
4253 : 2098864 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
4254 : : {
4255 : 9 : inf = G_(" enters %<constexpr if%> statement");
4256 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4257 : : saw_cxif = true;
4258 : : }
4259 : 2098855 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
4260 : : {
4261 : 18 : inf = G_(" enters %<consteval if%> statement");
4262 : 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4263 : : saw_ceif = true;
4264 : : }
4265 : : break;
4266 : :
4267 : : default:
4268 : : break;
4269 : : }
4270 : :
4271 : : if (inf)
4272 : : {
4273 : 141 : if (identified < 2)
4274 : 141 : complained = identify_goto (decl, input_location, locus,
4275 : : diagnostics::kind::error,
4276 : : computed);
4277 : 141 : identified = 2;
4278 : 141 : if (complained)
4279 : 141 : inform (loc, inf);
4280 : : }
4281 : : }
4282 : :
4283 : 2127739 : if (!vec_safe_is_empty (computed))
4284 : : {
4285 : 9 : if (!identified)
4286 : 6 : complained = identify_goto (decl, input_location, locus,
4287 : : diagnostics::kind::error,
4288 : : computed);
4289 : 9 : identified = 2;
4290 : 9 : if (complained)
4291 : 18 : for (tree d : computed)
4292 : : {
4293 : 9 : if (DECL_P (d))
4294 : 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4295 : 3 : else if (d == get_identifier ("catch"))
4296 : 3 : inform (*locus, " does not clean up handled exception");
4297 : : }
4298 : : }
4299 : :
4300 : 2127822 : if (!vacuous_decls.is_empty () && !seen_error ())
4301 : : {
4302 : 81 : tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4303 : 81 : if (case_label)
4304 : : {
4305 : 38 : gcc_checking_assert (tsi_stmt (iter) == case_label);
4306 : 38 : tsi_delink (&iter);
4307 : 38 : iter = tsi_last (cur_stmt_list);
4308 : : }
4309 : 81 : tree lab = maybe_add_deferred_init_calls (iter, case_label,
4310 : : vacuous_decls.address (),
4311 : : vacuous_decls.length ());
4312 : 81 : if (lab)
4313 : : {
4314 : : unsigned int i;
4315 : : named_label_fwd_direct_goto *dgoto;
4316 : 167 : FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4317 : 43 : *dgoto->direct_goto = lab;
4318 : : }
4319 : : }
4320 : :
4321 : 2127739 : if (identified)
4322 : : return 0;
4323 : 2127535 : return vacuous_inits ? 2 : 1;
4324 : 2127739 : }
4325 : :
4326 : : static void
4327 : 2102 : check_previous_goto (tree decl, struct named_label_use_entry *use)
4328 : : {
4329 : 2102 : check_previous_goto_1 (decl, use->binding_level,
4330 : 2102 : use->names_in_scope, use->in_omp_scope,
4331 : 2102 : &use->o_goto_locus, use->computed_goto,
4332 : : use->direct_goto, NULL_TREE);
4333 : 2102 : vec_free (use->direct_goto);
4334 : 2102 : }
4335 : :
4336 : : static int
4337 : 2125637 : check_switch_goto (cp_binding_level *level, tree case_label)
4338 : : {
4339 : 2125637 : return check_previous_goto_1 (NULL_TREE, level, level->names,
4340 : 2125637 : false, NULL, nullptr, nullptr, case_label);
4341 : : }
4342 : :
4343 : : /* Check that a new jump to a label ENT is OK. DECLP is a pointer
4344 : : to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4345 : :
4346 : : void
4347 : 2829 : check_goto_1 (named_label_entry *ent, tree *declp)
4348 : : {
4349 : 2829 : auto_diagnostic_group d;
4350 : 2829 : tree decl = ent->label_decl;
4351 : 2829 : bool computed = declp == NULL;
4352 : :
4353 : : /* If the label hasn't been defined yet, defer checking. */
4354 : 2829 : if (! DECL_INITIAL (decl))
4355 : : {
4356 : : /* Don't bother creating another use if the last goto had the
4357 : : same data, and will therefore create the same set of errors. */
4358 : 2171 : if (ent->uses
4359 : 658 : && ent->uses->binding_level == current_binding_level
4360 : 60 : && ent->uses->names_in_scope == current_binding_level->names)
4361 : : {
4362 : 60 : if (declp
4363 : 54 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4364 : 11 : && !processing_template_decl)
4365 : 9 : vec_safe_push (ent->uses->direct_goto,
4366 : 9 : named_label_fwd_direct_goto { declp });
4367 : 60 : return;
4368 : : }
4369 : :
4370 : 2111 : named_label_use_entry *new_use
4371 : 2111 : = ggc_alloc<named_label_use_entry> ();
4372 : 2111 : new_use->binding_level = current_binding_level;
4373 : 2111 : new_use->names_in_scope = current_binding_level->names;
4374 : 2111 : new_use->o_goto_locus = input_location;
4375 : 2111 : new_use->in_omp_scope = false;
4376 : 2111 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4377 : 2111 : new_use->direct_goto = nullptr;
4378 : 2111 : if (declp
4379 : 2023 : && flag_auto_var_init > AUTO_INIT_UNINITIALIZED
4380 : 354 : && !processing_template_decl)
4381 : 297 : vec_safe_push (new_use->direct_goto,
4382 : 297 : named_label_fwd_direct_goto { declp });
4383 : :
4384 : 2111 : new_use->next = ent->uses;
4385 : 2111 : ent->uses = new_use;
4386 : 2111 : return;
4387 : : }
4388 : :
4389 : 658 : bool saw_catch = false, complained = false;
4390 : 658 : int identified = 0;
4391 : 658 : tree bad;
4392 : 658 : unsigned ix;
4393 : 658 : unsigned n_bad_decls = 0;
4394 : :
4395 : 658 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4396 : 643 : || ent->in_constexpr_if || ent->in_consteval_if
4397 : 637 : || ent->in_omp_scope || ent->in_stmt_expr
4398 : 619 : || ent->has_bad_decls)
4399 : : {
4400 : 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4401 : 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4402 : 30 : || ent->in_consteval_if || ent->in_transaction_scope
4403 : 24 : || ent->in_omp_scope || ent->in_stmt_expr)
4404 : 39 : diag_kind = diagnostics::kind::error;
4405 : 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4406 : : &input_location, diag_kind, computed);
4407 : 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
4408 : : }
4409 : :
4410 : 848 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4411 : : {
4412 : 190 : bool problem = decl_jump_unsafe (bad);
4413 : 190 : if (!problem)
4414 : : {
4415 : 184 : gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4416 : 184 : n_bad_decls++;
4417 : 184 : continue;
4418 : : }
4419 : :
4420 : 6 : if (DECL_ARTIFICIAL (bad))
4421 : : {
4422 : : /* Can't skip init of __exception_info. */
4423 : 0 : if (identified == 1)
4424 : : {
4425 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4426 : : &input_location,
4427 : : diagnostics::kind::error,
4428 : : computed);
4429 : 0 : identified = 2;
4430 : : }
4431 : 0 : if (complained)
4432 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4433 : : saw_catch = true;
4434 : : }
4435 : 6 : else if (complained)
4436 : 6 : inform (DECL_SOURCE_LOCATION (bad),
4437 : : " skips initialization of %q#D", bad);
4438 : : }
4439 : :
4440 : 658 : if (complained)
4441 : : {
4442 : 45 : if (ent->in_try_scope)
4443 : 9 : inform (input_location, " enters %<try%> block");
4444 : 36 : else if (ent->in_catch_scope && !saw_catch)
4445 : 3 : inform (input_location, " enters %<catch%> block");
4446 : 33 : else if (ent->in_transaction_scope)
4447 : 3 : inform (input_location, " enters synchronized or atomic statement");
4448 : 30 : else if (ent->in_constexpr_if)
4449 : 3 : inform (input_location, " enters %<constexpr if%> statement");
4450 : 27 : else if (ent->in_consteval_if)
4451 : 3 : inform (input_location, " enters %<consteval if%> statement");
4452 : 24 : else if (ent->in_stmt_expr)
4453 : 15 : inform (input_location, " enters statement expression");
4454 : : }
4455 : :
4456 : 658 : if (ent->in_omp_scope)
4457 : : {
4458 : 3 : if (complained)
4459 : 3 : inform (input_location, " enters OpenMP structured block");
4460 : : }
4461 : 655 : else if (flag_openmp)
4462 : 69 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4463 : : {
4464 : 42 : if (b == ent->binding_level)
4465 : : break;
4466 : 36 : if (b->kind == sk_omp)
4467 : : {
4468 : 21 : if (identified < 2)
4469 : : {
4470 : 42 : complained = identify_goto (decl,
4471 : 21 : DECL_SOURCE_LOCATION (decl),
4472 : : &input_location,
4473 : : diagnostics::kind::error,
4474 : : computed);
4475 : 21 : identified = 2;
4476 : : }
4477 : 21 : if (complained)
4478 : 21 : inform (input_location, " exits OpenMP structured block");
4479 : : break;
4480 : : }
4481 : : }
4482 : :
4483 : : /* Warn if a computed goto might involve a local variable going out of scope
4484 : : without being cleaned up. */
4485 : 658 : if (computed)
4486 : : {
4487 : 71 : auto level = ent->binding_level;
4488 : 71 : auto names = ent->names_in_scope;
4489 : 71 : for (auto b = current_binding_level; ; b = b->level_chain)
4490 : : {
4491 : 111 : if (b->kind == sk_catch)
4492 : : {
4493 : 6 : if (!identified)
4494 : : {
4495 : 6 : complained
4496 : 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4497 : : &input_location, diagnostics::kind::error,
4498 : : computed);
4499 : 6 : identified = 2;
4500 : : }
4501 : 6 : if (complained)
4502 : 6 : inform (input_location,
4503 : : " does not clean up handled exception");
4504 : : }
4505 : 111 : tree end = b == level ? names : NULL_TREE;
4506 : 127 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4507 : : {
4508 : 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4509 : : {
4510 : 10 : if (!identified)
4511 : : {
4512 : 10 : complained
4513 : 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4514 : : &input_location, diagnostics::kind::error,
4515 : : computed);
4516 : 10 : identified = 2;
4517 : : }
4518 : 10 : if (complained)
4519 : 10 : inform (DECL_SOURCE_LOCATION (d),
4520 : : " does not destroy %qD", d);
4521 : : }
4522 : : }
4523 : 111 : if (b == level)
4524 : : break;
4525 : 40 : }
4526 : : }
4527 : :
4528 : 658 : if (n_bad_decls && declp)
4529 : 46 : vec_safe_push (ent->direct_goto,
4530 : 46 : named_label_bck_direct_goto { declp, n_bad_decls });
4531 : 2829 : }
4532 : :
4533 : : /* Check that a new jump to a label *DECLP is OK. Called by
4534 : : finish_goto_stmt. */
4535 : :
4536 : : void
4537 : 2789 : check_goto (tree *declp)
4538 : : {
4539 : 2789 : if (!named_labels)
4540 : : return;
4541 : 2774 : tree decl = *declp;
4542 : 2774 : if (TREE_CODE (decl) != LABEL_DECL)
4543 : : {
4544 : : /* We don't know where a computed goto is jumping,
4545 : : so check all addressable labels. */
4546 : 306 : for (auto iter = named_labels->begin ();
4547 : 416 : iter != named_labels->end ();
4548 : 196 : ++iter)
4549 : : {
4550 : 196 : auto ent = *iter;
4551 : 196 : if (ent->addressed)
4552 : 165 : check_goto_1 (ent, NULL);
4553 : : }
4554 : : }
4555 : : else
4556 : : {
4557 : 2664 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4558 : 2664 : named_label_entry **slot
4559 : 2664 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4560 : 2664 : named_label_entry *ent = *slot;
4561 : 2664 : check_goto_1 (ent, declp);
4562 : : }
4563 : : }
4564 : :
4565 : : /* Check that a return is ok wrt OpenMP structured blocks.
4566 : : Called by finish_return_stmt. Returns true if all is well. */
4567 : :
4568 : : bool
4569 : 278392 : check_omp_return (void)
4570 : : {
4571 : 1099219 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4572 : 820827 : if (b->kind == sk_omp)
4573 : : {
4574 : 15 : error ("invalid exit from OpenMP structured block");
4575 : 15 : return false;
4576 : : }
4577 : 820812 : else if (b->kind == sk_function_parms)
4578 : : break;
4579 : : return true;
4580 : : }
4581 : :
4582 : : /* Define a label, specifying the location in the source file.
4583 : : Return the LABEL_DECL node for the label. */
4584 : :
4585 : : tree
4586 : 20388 : define_label (location_t location, tree name)
4587 : : {
4588 : 20388 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4589 : :
4590 : : /* After labels, make any new cleanups in the function go into their
4591 : : own new (temporary) binding contour. */
4592 : 20388 : for (cp_binding_level *p = current_binding_level;
4593 : 30862 : p->kind != sk_function_parms;
4594 : 10474 : p = p->level_chain)
4595 : 10474 : p->more_cleanups_ok = 0;
4596 : :
4597 : 20388 : named_label_entry *ent = lookup_label_1 (name, false);
4598 : 20388 : tree decl = ent->label_decl;
4599 : :
4600 : 20388 : if (DECL_INITIAL (decl) != NULL_TREE)
4601 : : {
4602 : 6 : error ("duplicate label %qD", decl);
4603 : 6 : return error_mark_node;
4604 : : }
4605 : : else
4606 : : {
4607 : : /* Mark label as having been defined. */
4608 : 20382 : DECL_INITIAL (decl) = error_mark_node;
4609 : : /* Say where in the source. */
4610 : 20382 : DECL_SOURCE_LOCATION (decl) = location;
4611 : :
4612 : 20382 : ent->binding_level = current_binding_level;
4613 : 20382 : ent->names_in_scope = current_binding_level->names;
4614 : :
4615 : 22484 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4616 : 2102 : check_previous_goto (decl, use);
4617 : 20382 : ent->uses = NULL;
4618 : : }
4619 : :
4620 : 20382 : return decl;
4621 : 20388 : }
4622 : :
4623 : : struct cp_switch
4624 : : {
4625 : : cp_binding_level *level;
4626 : : struct cp_switch *next;
4627 : : /* The SWITCH_STMT being built. */
4628 : : tree switch_stmt;
4629 : : /* A splay-tree mapping the low element of a case range to the high
4630 : : element, or NULL_TREE if there is no high element. Used to
4631 : : determine whether or not a new case label duplicates an old case
4632 : : label. We need a tree, rather than simply a hash table, because
4633 : : of the GNU case range extension. */
4634 : : splay_tree cases;
4635 : : /* Remember whether a default: case label has been seen. */
4636 : : bool has_default_p;
4637 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4638 : : bool break_stmt_seen_p;
4639 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4640 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4641 : : bool in_loop_body_p;
4642 : : };
4643 : :
4644 : : /* A stack of the currently active switch statements. The innermost
4645 : : switch statement is on the top of the stack. There is no need to
4646 : : mark the stack for garbage collection because it is only active
4647 : : during the processing of the body of a function, and we never
4648 : : collect at that point. */
4649 : :
4650 : : static struct cp_switch *switch_stack;
4651 : :
4652 : : /* Called right after a switch-statement condition is parsed.
4653 : : SWITCH_STMT is the switch statement being parsed. */
4654 : :
4655 : : void
4656 : 539108 : push_switch (tree switch_stmt)
4657 : : {
4658 : 539108 : struct cp_switch *p = XNEW (struct cp_switch);
4659 : 539108 : p->level = current_binding_level;
4660 : 539108 : p->next = switch_stack;
4661 : 539108 : p->switch_stmt = switch_stmt;
4662 : 539108 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4663 : 539108 : p->has_default_p = false;
4664 : 539108 : p->break_stmt_seen_p = false;
4665 : 539108 : p->in_loop_body_p = false;
4666 : 539108 : switch_stack = p;
4667 : 539108 : }
4668 : :
4669 : : void
4670 : 539108 : pop_switch (void)
4671 : : {
4672 : 539108 : struct cp_switch *cs = switch_stack;
4673 : :
4674 : : /* Emit warnings as needed. */
4675 : 539108 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4676 : 539108 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4677 : 539108 : const bool bool_cond_p
4678 : 539108 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4679 : 539108 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4680 : 539108 : if (!processing_template_decl)
4681 : 331760 : c_do_switch_warnings (cs->cases, switch_location,
4682 : 331760 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4683 : : bool_cond_p);
4684 : :
4685 : : /* For the benefit of block_may_fallthru remember if the switch body
4686 : : case labels cover all possible values and if there are break; stmts. */
4687 : 539108 : if (cs->has_default_p
4688 : 539108 : || (!processing_template_decl
4689 : 45402 : && c_switch_covers_all_cases_p (cs->cases,
4690 : 45402 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4691 : 421294 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4692 : 539108 : if (!cs->break_stmt_seen_p)
4693 : 284031 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4694 : : /* Now that we're done with the switch warnings, set the switch type
4695 : : to the type of the condition if the index type was of scoped enum type.
4696 : : (Such types don't participate in the integer promotions.) We do this
4697 : : because of bit-fields whose declared type is a scoped enum type:
4698 : : gimplification will use the lowered index type, but convert the
4699 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4700 : : and verify_gimple_switch doesn't accept that. */
4701 : 539108 : if (is_bitfield_expr_with_lowered_type (cond))
4702 : 61756 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4703 : 539108 : gcc_assert (!cs->in_loop_body_p);
4704 : 539108 : splay_tree_delete (cs->cases);
4705 : 539108 : switch_stack = switch_stack->next;
4706 : 539108 : free (cs);
4707 : 539108 : }
4708 : :
4709 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4710 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4711 : : in switch_stack we've seen a BREAK_STMT. */
4712 : :
4713 : : void
4714 : 3679265 : note_break_stmt (void)
4715 : : {
4716 : 3679265 : if (switch_stack && !switch_stack->in_loop_body_p)
4717 : 1640010 : switch_stack->break_stmt_seen_p = true;
4718 : 3679265 : }
4719 : :
4720 : : /* Note the start of processing of an iteration statement's body.
4721 : : The note_break_stmt function will do nothing while processing it.
4722 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4723 : :
4724 : : bool
4725 : 16089896 : note_iteration_stmt_body_start (void)
4726 : : {
4727 : 16089896 : if (!switch_stack)
4728 : : return false;
4729 : 134750 : bool ret = switch_stack->in_loop_body_p;
4730 : 134750 : switch_stack->in_loop_body_p = true;
4731 : 134750 : return ret;
4732 : : }
4733 : :
4734 : : /* Note the end of processing of an iteration statement's body. */
4735 : :
4736 : : void
4737 : 16089896 : note_iteration_stmt_body_end (bool prev)
4738 : : {
4739 : 16089896 : if (switch_stack)
4740 : 134750 : switch_stack->in_loop_body_p = prev;
4741 : 16089896 : }
4742 : :
4743 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4744 : : condition. Note that if TYPE and VALUE are already integral we don't
4745 : : really do the conversion because the language-independent
4746 : : warning/optimization code will work better that way. */
4747 : :
4748 : : static tree
4749 : 4250950 : case_conversion (tree type, tree value)
4750 : : {
4751 : 4250950 : if (value == NULL_TREE)
4752 : : return value;
4753 : :
4754 : 1839285 : value = mark_rvalue_use (value);
4755 : :
4756 : 1839285 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4757 : 1011893 : type = type_promotes_to (type);
4758 : :
4759 : 1839285 : tree ovalue = value;
4760 : : /* The constant-expression VALUE shall be a converted constant expression
4761 : : of the adjusted type of the switch condition, which doesn't allow
4762 : : narrowing conversions. */
4763 : 1839285 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4764 : :
4765 : 1839285 : if (cxx_dialect >= cxx11
4766 : 1839285 : && (SCOPED_ENUM_P (type)
4767 : 1008762 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4768 : : /* Use the converted value. */;
4769 : : else
4770 : : /* The already integral case. */
4771 : : value = ovalue;
4772 : :
4773 : 1839285 : return cxx_constant_value (value);
4774 : : }
4775 : :
4776 : : /* Note that we've seen a definition of a case label, and complain if this
4777 : : is a bad place for one. */
4778 : :
4779 : : tree
4780 : 4061559 : finish_case_label (location_t loc, tree low_value, tree high_value)
4781 : : {
4782 : 4061559 : tree cond, r;
4783 : 4061559 : cp_binding_level *p;
4784 : 4061559 : tree type;
4785 : :
4786 : 4061559 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4787 : 421258 : switch_stack->has_default_p = true;
4788 : :
4789 : 4061559 : if (processing_template_decl)
4790 : : {
4791 : 1935961 : tree label;
4792 : :
4793 : : /* For templates, just add the case label; we'll do semantic
4794 : : analysis at instantiation-time. But diagnose case labels
4795 : : in expansion statements with switch outside of it here. */
4796 : 1935961 : if (in_expansion_stmt)
4797 : 39 : for (cp_binding_level *b = current_binding_level;
4798 : 78 : b != switch_stack->level; b = b->level_chain)
4799 : 63 : if (b->kind == sk_template_for && b->this_entity)
4800 : : {
4801 : 24 : auto_diagnostic_group d;
4802 : 24 : error ("jump to case label");
4803 : 24 : inform (EXPR_LOCATION (b->this_entity),
4804 : : " enters %<template for%> statement");
4805 : 24 : return error_mark_node;
4806 : 24 : }
4807 : 1935937 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4808 : 1935937 : return add_stmt (build_case_label (low_value, high_value, label));
4809 : : }
4810 : :
4811 : : /* Find the condition on which this switch statement depends. */
4812 : 2125598 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4813 : 2125598 : if (cond && TREE_CODE (cond) == TREE_LIST)
4814 : 0 : cond = TREE_VALUE (cond);
4815 : :
4816 : 2125598 : int chk_switch_goto = check_switch_goto (switch_stack->level, NULL_TREE);
4817 : 2125598 : if (!chk_switch_goto)
4818 : 87 : return error_mark_node;
4819 : :
4820 : 2125511 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4821 : 2125511 : if (type == error_mark_node)
4822 : : return error_mark_node;
4823 : :
4824 : 2125475 : low_value = case_conversion (type, low_value);
4825 : 2125475 : high_value = case_conversion (type, high_value);
4826 : :
4827 : 2125475 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4828 : :
4829 : 2125475 : if (r != error_mark_node && chk_switch_goto == 2)
4830 : 39 : check_switch_goto (switch_stack->level, r);
4831 : :
4832 : : /* After labels, make any new cleanups in the function go into their
4833 : : own new (temporary) binding contour. */
4834 : 2125475 : for (p = current_binding_level;
4835 : 8936945 : p->kind != sk_function_parms;
4836 : 6811470 : p = p->level_chain)
4837 : 6811470 : p->more_cleanups_ok = 0;
4838 : :
4839 : : return r;
4840 : : }
4841 : :
4842 : : struct typename_info {
4843 : : tree scope;
4844 : : tree name;
4845 : : tree template_id;
4846 : : bool enum_p;
4847 : : bool class_p;
4848 : : bool union_p;
4849 : : };
4850 : :
4851 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4852 : : {
4853 : : typedef typename_info *compare_type;
4854 : :
4855 : : /* Hash a TYPENAME_TYPE. */
4856 : :
4857 : : static hashval_t
4858 : 405867409 : hash (tree context, tree fullname)
4859 : : {
4860 : 405867409 : hashval_t hash = 0;
4861 : 405867409 : hash = iterative_hash_object (context, hash);
4862 : 405867409 : hash = iterative_hash_object (fullname, hash);
4863 : 405867409 : return hash;
4864 : : }
4865 : :
4866 : : static hashval_t
4867 : 58706857 : hash (const typename_info *ti)
4868 : : {
4869 : 58706857 : return typename_hasher::hash (ti->scope, ti->template_id);
4870 : : }
4871 : :
4872 : : static hashval_t
4873 : 347160552 : hash (tree t)
4874 : : {
4875 : 347160552 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4876 : : }
4877 : :
4878 : : /* Compare two TYPENAME_TYPEs. */
4879 : :
4880 : : static bool
4881 : 403770604 : equal (tree t1, const typename_info *t2)
4882 : : {
4883 : 403770604 : return (TYPE_IDENTIFIER (t1) == t2->name
4884 : 118549678 : && TYPE_CONTEXT (t1) == t2->scope
4885 : 30161873 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4886 : 30149741 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4887 : 30149687 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p
4888 : 433433001 : && TYPENAME_IS_UNION_P (t1) == t2->union_p);
4889 : : }
4890 : : };
4891 : :
4892 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4893 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4894 : :
4895 : : Returns the new TYPENAME_TYPE. */
4896 : :
4897 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4898 : :
4899 : : tree
4900 : 58706857 : build_typename_type (tree context, tree name, tree fullname,
4901 : : enum tag_types tag_type)
4902 : : {
4903 : 58706857 : typename_info ti;
4904 : :
4905 : 58706857 : if (typename_htab == NULL)
4906 : 19345 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4907 : :
4908 : 58706857 : ti.scope = FROB_CONTEXT (context);
4909 : 58706857 : ti.name = name;
4910 : 58706857 : ti.template_id = fullname;
4911 : 58706857 : ti.enum_p = tag_type == enum_type;
4912 : 58706857 : ti.class_p = (tag_type == class_type || tag_type == record_type);
4913 : 58706857 : ti.union_p = tag_type == union_type;
4914 : 58706857 : hashval_t hash = typename_hasher::hash (&ti);
4915 : :
4916 : : /* See if we already have this type. */
4917 : 58706857 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4918 : 58706857 : tree t = *e;
4919 : 58706857 : if (*e)
4920 : : t = *e;
4921 : : else
4922 : : {
4923 : : /* Build the TYPENAME_TYPE. */
4924 : 29044460 : t = cxx_make_type (TYPENAME_TYPE);
4925 : 29044460 : TYPE_CONTEXT (t) = ti.scope;
4926 : 29044460 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4927 : 29044460 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4928 : 29044460 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4929 : 29044460 : TYPENAME_IS_UNION_P (t) = ti.union_p;
4930 : :
4931 : : /* Build the corresponding TYPE_DECL. */
4932 : 29044460 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4933 : 29044460 : TYPE_NAME (t) = d;
4934 : 29044460 : TYPE_STUB_DECL (t) = d;
4935 : 29044460 : DECL_CONTEXT (d) = ti.scope;
4936 : 29044460 : DECL_ARTIFICIAL (d) = 1;
4937 : :
4938 : : /* Store it in the hash table. */
4939 : 29044460 : *e = t;
4940 : :
4941 : : /* TYPENAME_TYPEs must always be compared structurally, because
4942 : : they may or may not resolve down to another type depending on
4943 : : the currently open classes. */
4944 : 29044460 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4945 : : }
4946 : :
4947 : 58706857 : return t;
4948 : : }
4949 : :
4950 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4951 : : provided to name the type. Returns an appropriate type, unless an
4952 : : error occurs, in which case error_mark_node is returned. If we
4953 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4954 : : return that, rather than the _TYPE it corresponds to, in other
4955 : : cases we look through the type decl. If TF_ERROR is set, complain
4956 : : about errors, otherwise be quiet. */
4957 : :
4958 : : tree
4959 : 217508209 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4960 : : tsubst_flags_t complain)
4961 : : {
4962 : 217508209 : tree fullname;
4963 : 217508209 : tree t;
4964 : 217508209 : bool want_template;
4965 : :
4966 : 217508209 : if (name == error_mark_node
4967 : 217508205 : || context == NULL_TREE
4968 : 217508205 : || context == error_mark_node)
4969 : : return error_mark_node;
4970 : :
4971 : 217508189 : if (TYPE_P (name))
4972 : : {
4973 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4974 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4975 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4976 : 0 : name = TYPE_IDENTIFIER (name);
4977 : : else
4978 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4979 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4980 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4981 : 0 : CLASSTYPE_TI_ARGS (name));
4982 : : }
4983 : 217508189 : else if (TREE_CODE (name) == TYPE_DECL)
4984 : 0 : name = DECL_NAME (name);
4985 : :
4986 : 217508189 : fullname = name;
4987 : :
4988 : 217508189 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4989 : : {
4990 : 6301906 : name = TREE_OPERAND (name, 0);
4991 : 6301906 : if (DECL_TYPE_TEMPLATE_P (name))
4992 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4993 : 6301906 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4994 : : {
4995 : 0 : if (complain & tf_error)
4996 : 0 : error ("%qD is not a type", name);
4997 : 0 : return error_mark_node;
4998 : : }
4999 : : }
5000 : 217508189 : if (TREE_CODE (name) == TEMPLATE_DECL)
5001 : : {
5002 : 0 : if (complain & tf_error)
5003 : 0 : error ("%qD used without template arguments", name);
5004 : 0 : return error_mark_node;
5005 : : }
5006 : 217508189 : else if (is_overloaded_fn (name))
5007 : : {
5008 : 3 : if (complain & tf_error)
5009 : 3 : error ("%qD is a function, not a type", name);
5010 : 3 : return error_mark_node;
5011 : : }
5012 : 217508186 : gcc_assert (identifier_p (name));
5013 : 217508186 : gcc_assert (TYPE_P (context));
5014 : :
5015 : 217508186 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5016 : : /* This can happen for C++17 variadic using (c++/88986). */;
5017 : 205235035 : else if (!MAYBE_CLASS_TYPE_P (context))
5018 : : {
5019 : 6 : if (complain & tf_error)
5020 : 6 : error ("%q#T is not a class", context);
5021 : 6 : return error_mark_node;
5022 : : }
5023 : :
5024 : : /* When the CONTEXT is a dependent type, NAME could refer to a
5025 : : dependent base class of CONTEXT. But look inside it anyway
5026 : : if CONTEXT is a currently open scope, in case it refers to a
5027 : : member of the current instantiation or a non-dependent base;
5028 : : lookup will stop when we hit a dependent base. */
5029 : 217508180 : if (!dependent_scope_p (context))
5030 : : {
5031 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5032 : : (as per [temp.res.general]/3), unless
5033 : : - the tag corresponds to a class-key or 'enum' so
5034 : : [basic.lookup.elab] applies, or
5035 : : - the tag corresponds to scope_type or tf_qualifying_scope is
5036 : : set so [basic.lookup.qual]/1 applies, or
5037 : : - we're inside a base-specifier so [class.derived.general]/2 applies;
5038 : : the tag will already be class_type in that case.
5039 : : TODO: If we'd set/track the scope_type tag thoroughly on all
5040 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5041 : : tf_qualifying_scope flag. */
5042 : 326409574 : bool want_type = (tag_type != none_type && tag_type != typename_type)
5043 : 163204787 : || (complain & tf_qualifying_scope);
5044 : 163204787 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5045 : : }
5046 : : else
5047 : : t = NULL_TREE;
5048 : :
5049 : 236269082 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5050 : 54304314 : return build_typename_type (context, name, fullname, tag_type);
5051 : :
5052 : 163203866 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5053 : :
5054 : 163203866 : if (!t)
5055 : : {
5056 : 18759981 : if (complain & tf_error)
5057 : : {
5058 : 260 : if (!COMPLETE_TYPE_P (context))
5059 : 18 : cxx_incomplete_type_error (NULL_TREE, context);
5060 : : else
5061 : 483 : error (want_template ? G_("no class template named %q#T in %q#T")
5062 : : : G_("no type named %q#T in %q#T"), name, context);
5063 : : }
5064 : 18759981 : return error_mark_node;
5065 : : }
5066 : :
5067 : : /* Pull out the template from an injected-class-name (or multiple). */
5068 : 144443885 : if (want_template)
5069 : 4760663 : t = maybe_get_template_decl_from_type_decl (t);
5070 : :
5071 : 144443885 : if (TREE_CODE (t) == TREE_LIST)
5072 : : {
5073 : 3 : if (complain & tf_error)
5074 : : {
5075 : 3 : auto_diagnostic_group d;
5076 : 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
5077 : 3 : print_candidates (t);
5078 : 3 : }
5079 : 3 : return error_mark_node;
5080 : : }
5081 : :
5082 : 144443882 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5083 : : {
5084 : 6 : if (complain & tf_error)
5085 : 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
5086 : : context, name, t);
5087 : 6 : return error_mark_node;
5088 : : }
5089 : 144443876 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
5090 : : {
5091 : 1469 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5092 : 2941 : && DECL_TYPE_TEMPLATE_P (t))
5093 : : /* The caller permits this typename-specifier to name a template
5094 : : (because it appears in a CTAD-enabled context). */;
5095 : : else
5096 : : {
5097 : 23 : if (complain & tf_error)
5098 : 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
5099 : : context, name, t);
5100 : 23 : return error_mark_node;
5101 : : }
5102 : : }
5103 : :
5104 : 144443853 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5105 : : context, complain))
5106 : 49 : return error_mark_node;
5107 : :
5108 : 144443804 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5109 : 1458 : return make_template_placeholder (t);
5110 : :
5111 : 144442346 : if (want_template)
5112 : : {
5113 : 4760657 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5114 : : NULL_TREE, context,
5115 : : complain | tf_user);
5116 : 4760657 : if (t == error_mark_node)
5117 : : return error_mark_node;
5118 : 4760652 : t = TYPE_NAME (t);
5119 : : }
5120 : :
5121 : 144442341 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5122 : 3308878 : t = TREE_TYPE (t);
5123 : :
5124 : 144442341 : maybe_record_typedef_use (t);
5125 : :
5126 : 144442341 : return t;
5127 : : }
5128 : :
5129 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5130 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5131 : : in which case error_mark_node is returned.
5132 : :
5133 : : If PARM_LIST is non-NULL, also make sure that the template parameter
5134 : : list of TEMPLATE_DECL matches.
5135 : :
5136 : : If COMPLAIN zero, don't complain about any errors that occur. */
5137 : :
5138 : : tree
5139 : 92398 : make_unbound_class_template (tree context, tree name, tree parm_list,
5140 : : tsubst_flags_t complain)
5141 : : {
5142 : 92398 : if (TYPE_P (name))
5143 : 0 : name = TYPE_IDENTIFIER (name);
5144 : 92398 : else if (DECL_P (name))
5145 : 0 : name = DECL_NAME (name);
5146 : 92398 : gcc_assert (identifier_p (name));
5147 : :
5148 : 92398 : if (!dependent_type_p (context)
5149 : 92398 : || currently_open_class (context))
5150 : : {
5151 : 29986 : tree tmpl = NULL_TREE;
5152 : :
5153 : 29986 : if (MAYBE_CLASS_TYPE_P (context))
5154 : 29980 : tmpl = lookup_field (context, name, 0, false);
5155 : :
5156 : 29980 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5157 : 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5158 : :
5159 : 29974 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5160 : : {
5161 : 12 : if (complain & tf_error)
5162 : 9 : error ("no class template named %q#T in %q#T", name, context);
5163 : 12 : return error_mark_node;
5164 : : }
5165 : :
5166 : 29974 : if (parm_list
5167 : 29974 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5168 : : {
5169 : 0 : if (complain & tf_error)
5170 : : {
5171 : 0 : auto_diagnostic_group d;
5172 : 0 : error ("template parameters do not match template %qD", tmpl);
5173 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
5174 : : "%qD declared here", tmpl);
5175 : 0 : }
5176 : 0 : return error_mark_node;
5177 : : }
5178 : :
5179 : 29974 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5180 : : complain))
5181 : 0 : return error_mark_node;
5182 : :
5183 : : return tmpl;
5184 : : }
5185 : :
5186 : 62412 : return make_unbound_class_template_raw (context, name, parm_list);
5187 : : }
5188 : :
5189 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
5190 : :
5191 : : tree
5192 : 62464 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5193 : : {
5194 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
5195 : 62464 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5196 : 62464 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5197 : 62464 : TREE_TYPE (t) = NULL_TREE;
5198 : 62464 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5199 : :
5200 : : /* Build the corresponding TEMPLATE_DECL. */
5201 : 62464 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5202 : 62464 : TYPE_NAME (t) = d;
5203 : 62464 : TYPE_STUB_DECL (t) = d;
5204 : 62464 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5205 : 62464 : DECL_ARTIFICIAL (d) = 1;
5206 : 62464 : DECL_TEMPLATE_PARMS (d) = parm_list;
5207 : :
5208 : 62464 : return t;
5209 : : }
5210 : :
5211 : :
5212 : :
5213 : : /* Push the declarations of builtin types into the global namespace.
5214 : : RID_INDEX is the index of the builtin type in the array
5215 : : RID_POINTERS. NAME is the name used when looking up the builtin
5216 : : type. TYPE is the _TYPE node for the builtin type.
5217 : :
5218 : : The calls to set_global_binding below should be
5219 : : eliminated. Built-in types should not be looked up name; their
5220 : : names are keywords that the parser can recognize. However, there
5221 : : is code in c-common.cc that uses identifier_global_value to look up
5222 : : built-in types by name. */
5223 : :
5224 : : void
5225 : 4021224 : record_builtin_type (enum rid rid_index,
5226 : : const char* name,
5227 : : tree type)
5228 : : {
5229 : 4021224 : tree decl = NULL_TREE;
5230 : :
5231 : 4021224 : if (name)
5232 : : {
5233 : 2368909 : tree tname = get_identifier (name);
5234 : 2368909 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5235 : 2368909 : DECL_ARTIFICIAL (tdecl) = 1;
5236 : 2368909 : set_global_binding (tdecl);
5237 : 2368909 : decl = tdecl;
5238 : : }
5239 : :
5240 : 4021224 : if ((int) rid_index < (int) RID_MAX)
5241 : 2466104 : if (tree rname = ridpointers[(int) rid_index])
5242 : 1882934 : if (!decl || DECL_NAME (decl) != rname)
5243 : : {
5244 : 1360730 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5245 : 1360730 : DECL_ARTIFICIAL (rdecl) = 1;
5246 : 1360730 : set_global_binding (rdecl);
5247 : 1360730 : if (!decl)
5248 : : decl = rdecl;
5249 : : }
5250 : :
5251 : 3729639 : if (decl)
5252 : : {
5253 : 3438054 : if (!TYPE_NAME (type))
5254 : 2854884 : TYPE_NAME (type) = decl;
5255 : 3438054 : debug_hooks->type_decl (decl, 0);
5256 : : }
5257 : 4021224 : }
5258 : :
5259 : : /* Push a type into the namespace so that the back ends ignore it. */
5260 : :
5261 : : static void
5262 : 291585 : record_unknown_type (tree type, const char* name)
5263 : : {
5264 : 291585 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5265 : : TYPE_DECL, get_identifier (name), type));
5266 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5267 : 291585 : DECL_IGNORED_P (decl) = 1;
5268 : 291585 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5269 : 291585 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5270 : 291585 : SET_TYPE_ALIGN (type, 1);
5271 : 291585 : TYPE_USER_ALIGN (type) = 0;
5272 : 291585 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5273 : 291585 : }
5274 : :
5275 : : /* Create all the predefined identifiers. */
5276 : :
5277 : : static void
5278 : 97195 : initialize_predefined_identifiers (void)
5279 : : {
5280 : 97195 : struct predefined_identifier
5281 : : {
5282 : : const char *name; /* Name. */
5283 : : tree *node; /* Node to store it in. */
5284 : : cp_identifier_kind kind; /* Kind of identifier. */
5285 : : };
5286 : :
5287 : : /* A table of identifiers to create at startup. */
5288 : 97195 : static const predefined_identifier predefined_identifiers[] = {
5289 : : {"C++", &lang_name_cplusplus, cik_normal},
5290 : : {"C", &lang_name_c, cik_normal},
5291 : : /* Some of these names have a trailing space so that it is
5292 : : impossible for them to conflict with names written by users. */
5293 : : {"__ct ", &ctor_identifier, cik_ctor},
5294 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
5295 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
5296 : : {"__dt ", &dtor_identifier, cik_dtor},
5297 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
5298 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
5299 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
5300 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
5301 : : {"__in_chrg", &in_charge_identifier, cik_normal},
5302 : : {"__as_base ", &as_base_identifier, cik_normal},
5303 : : {"this", &this_identifier, cik_normal},
5304 : : {"__delta", &delta_identifier, cik_normal},
5305 : : {"__pfn", &pfn_identifier, cik_normal},
5306 : : {"_vptr", &vptr_identifier, cik_normal},
5307 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
5308 : : {"::", &global_identifier, cik_normal},
5309 : : /* The demangler expects anonymous namespaces to be called
5310 : : something starting with '_GLOBAL__N_'. It no longer needs
5311 : : to be unique to the TU. */
5312 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
5313 : : {"auto", &auto_identifier, cik_normal},
5314 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
5315 : : {"initializer_list", &init_list_identifier, cik_normal},
5316 : : {"__for_range ", &for_range__identifier, cik_normal},
5317 : : {"__for_begin ", &for_begin__identifier, cik_normal},
5318 : : {"__for_end ", &for_end__identifier, cik_normal},
5319 : : {"__for_range", &for_range_identifier, cik_normal},
5320 : : {"__for_begin", &for_begin_identifier, cik_normal},
5321 : : {"__for_end", &for_end_identifier, cik_normal},
5322 : : {"abi_tag", &abi_tag_identifier, cik_normal},
5323 : : {"aligned", &aligned_identifier, cik_normal},
5324 : : {"begin", &begin_identifier, cik_normal},
5325 : : {"end", &end_identifier, cik_normal},
5326 : : {"get", &get__identifier, cik_normal},
5327 : : {"gnu", &gnu_identifier, cik_normal},
5328 : : {"tuple_element", &tuple_element_identifier, cik_normal},
5329 : : {"tuple_size", &tuple_size_identifier, cik_normal},
5330 : : {"type", &type_identifier, cik_normal},
5331 : : {"value", &value_identifier, cik_normal},
5332 : : {"_FUN", &fun_identifier, cik_normal},
5333 : : {"__closure", &closure_identifier, cik_normal},
5334 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
5335 : : {"heap ", &heap_identifier, cik_normal},
5336 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
5337 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
5338 : : {"heap []", &heap_vec_identifier, cik_normal},
5339 : : {"omp", &omp_identifier, cik_normal},
5340 : : {"internal ", &internal_identifier, cik_normal},
5341 : : {NULL, NULL, cik_normal}
5342 : : };
5343 : :
5344 : 4665360 : for (const predefined_identifier *pid = predefined_identifiers;
5345 : 4665360 : pid->name; ++pid)
5346 : : {
5347 : 4568165 : *pid->node = get_identifier (pid->name);
5348 : : /* Some of these identifiers already have a special kind. */
5349 : 4568165 : if (pid->kind != cik_normal)
5350 : 777560 : set_identifier_kind (*pid->node, pid->kind);
5351 : : }
5352 : 97195 : }
5353 : :
5354 : : /* Build a specific variant of operator new. */
5355 : :
5356 : : static void
5357 : 173441 : cxx_build_operator_new (tree newtype)
5358 : : {
5359 : 173441 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5360 : 173441 : DECL_IS_MALLOC (opnew) = 1;
5361 : 173441 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5362 : 173441 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5363 : 173441 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5364 : 173441 : DECL_IS_MALLOC (opnew) = 1;
5365 : 173441 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5366 : 173441 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5367 : 173441 : }
5368 : :
5369 : : /* Build a specific variant of operator delete. */
5370 : :
5371 : : static void
5372 : 327201 : cxx_build_operator_delete (tree deltype)
5373 : : {
5374 : 327201 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5375 : 327201 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5376 : 327201 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5377 : 327201 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5378 : 327201 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5379 : 327201 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5380 : 327201 : }
5381 : :
5382 : : /* Declare all variants of operator new and delete. */
5383 : :
5384 : : static void
5385 : 97195 : cxx_init_operator_new_delete_decls (void)
5386 : : {
5387 : 97195 : tree newattrs, extvisattr;
5388 : 97195 : tree newtype, deltype;
5389 : 97195 : tree ptr_ftype_sizetype;
5390 : 97195 : tree new_eh_spec;
5391 : 97195 : tree void_ftype_ptr = build_function_type_list (void_type_node,
5392 : : ptr_type_node, NULL_TREE);
5393 : 97195 : void_ftype_ptr
5394 : 97195 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
5395 : :
5396 : 97195 : ptr_ftype_sizetype
5397 : 97195 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5398 : 97195 : if (cxx_dialect == cxx98)
5399 : : {
5400 : 14067 : tree bad_alloc_id;
5401 : 14067 : tree bad_alloc_type_node;
5402 : 14067 : tree bad_alloc_decl;
5403 : :
5404 : 14067 : push_nested_namespace (std_node);
5405 : 14067 : bad_alloc_id = get_identifier ("bad_alloc");
5406 : 14067 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5407 : 14067 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5408 : 14067 : bad_alloc_decl
5409 : 14067 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5410 : 14067 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5411 : 14067 : pop_nested_namespace (std_node);
5412 : :
5413 : 14067 : new_eh_spec
5414 : 14067 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5415 : : }
5416 : : else
5417 : 83128 : new_eh_spec = noexcept_false_spec;
5418 : :
5419 : 97195 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5420 : : NULL_TREE);
5421 : 97195 : newattrs = tree_cons (get_identifier ("alloc_size"),
5422 : : build_tree_list (NULL_TREE, integer_one_node),
5423 : : extvisattr);
5424 : 97195 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5425 : 97195 : newtype = build_exception_variant (newtype, new_eh_spec);
5426 : 97195 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5427 : 97195 : deltype = build_exception_variant (deltype, empty_except_spec);
5428 : 97195 : cxx_build_operator_new (newtype);
5429 : 97195 : cxx_build_operator_delete (deltype);
5430 : 97195 : if (flag_sized_deallocation)
5431 : : {
5432 : : /* Also push the sized deallocation variants:
5433 : : void operator delete(void*, std::size_t) throw();
5434 : : void operator delete[](void*, std::size_t) throw(); */
5435 : 77519 : tree void_ftype_ptr_size
5436 : 77519 : = build_function_type_list (void_type_node, ptr_type_node,
5437 : : size_type_node, NULL_TREE);
5438 : 77519 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5439 : : extvisattr);
5440 : 77519 : deltype = build_exception_variant (deltype, empty_except_spec);
5441 : 77519 : cxx_build_operator_delete (deltype);
5442 : : }
5443 : :
5444 : 97195 : if (aligned_new_threshold)
5445 : : {
5446 : 76246 : push_nested_namespace (std_node);
5447 : 76246 : tree align_id = get_identifier ("align_val_t");
5448 : 76246 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5449 : : NULL_TREE, /*scoped*/true, NULL);
5450 : 76246 : pop_nested_namespace (std_node);
5451 : :
5452 : : /* operator new (size_t, align_val_t); */
5453 : 76246 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5454 : : align_type_node, NULL_TREE);
5455 : 76246 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5456 : 76246 : newtype = build_exception_variant (newtype, new_eh_spec);
5457 : 76246 : cxx_build_operator_new (newtype);
5458 : :
5459 : : /* operator delete (void *, align_val_t); */
5460 : 76246 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5461 : : align_type_node, NULL_TREE);
5462 : 76246 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5463 : 76246 : deltype = build_exception_variant (deltype, empty_except_spec);
5464 : 76246 : cxx_build_operator_delete (deltype);
5465 : :
5466 : 76246 : if (flag_sized_deallocation)
5467 : : {
5468 : : /* operator delete (void *, size_t, align_val_t); */
5469 : 76241 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5470 : : size_type_node, align_type_node,
5471 : : NULL_TREE);
5472 : 76241 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5473 : 76241 : deltype = build_exception_variant (deltype, empty_except_spec);
5474 : 76241 : cxx_build_operator_delete (deltype);
5475 : : }
5476 : : }
5477 : 97195 : }
5478 : :
5479 : : /* Create the predefined scalar types of C,
5480 : : and some nodes representing standard constants (0, 1, (void *)0).
5481 : : Initialize the global binding level.
5482 : : Make definitions for built-in primitive functions. */
5483 : :
5484 : : void
5485 : 97195 : cxx_init_decl_processing (void)
5486 : : {
5487 : 97195 : tree void_ftype;
5488 : :
5489 : : /* Create all the identifiers we need. */
5490 : 97195 : initialize_predefined_identifiers ();
5491 : :
5492 : : /* Create the global variables. */
5493 : 97195 : push_to_top_level ();
5494 : :
5495 : 97195 : current_function_decl = NULL_TREE;
5496 : 97195 : current_binding_level = NULL;
5497 : : /* Enter the global namespace. */
5498 : 97195 : gcc_assert (global_namespace == NULL_TREE);
5499 : 97195 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5500 : : void_type_node);
5501 : 97195 : TREE_PUBLIC (global_namespace) = true;
5502 : 97195 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5503 : 97195 : DECL_CONTEXT (global_namespace)
5504 : 97195 : = build_translation_unit_decl (get_identifier (main_input_filename));
5505 : : /* Remember whether we want the empty class passing ABI change warning
5506 : : in this TU. */
5507 : 97195 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5508 : 123912 : = warn_abi && abi_version_crosses (12);
5509 : 97195 : debug_hooks->register_main_translation_unit
5510 : 97195 : (DECL_CONTEXT (global_namespace));
5511 : 97195 : begin_scope (sk_namespace, global_namespace);
5512 : 97195 : current_namespace = global_namespace;
5513 : :
5514 : 97195 : if (flag_visibility_ms_compat)
5515 : 6 : default_visibility = VISIBILITY_HIDDEN;
5516 : :
5517 : : /* Initially, C. */
5518 : 97195 : current_lang_name = lang_name_c;
5519 : :
5520 : : /* Create the `std' namespace. */
5521 : 97195 : push_namespace (get_identifier ("std"));
5522 : 97195 : std_node = current_namespace;
5523 : 97195 : pop_namespace ();
5524 : :
5525 : 97195 : flag_noexcept_type = (cxx_dialect >= cxx17);
5526 : :
5527 : 97195 : c_common_nodes_and_builtins ();
5528 : :
5529 : 97195 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5530 : 97195 : tree decl
5531 : 97195 : = add_builtin_function ("__builtin_is_constant_evaluated",
5532 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5533 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5534 : 97195 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5535 : :
5536 : : /* The concrete return type of __builtin_source_location is
5537 : : const std::source_location::__impl*, but we can't form the type
5538 : : at this point. So we initially declare it with an auto return
5539 : : type which we then "deduce" from require_deduced_type upon first use. */
5540 : 97195 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5541 : 97195 : decl = add_builtin_function ("__builtin_source_location",
5542 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5543 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5544 : 97195 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5545 : :
5546 : 97195 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5547 : : NULL_TREE);
5548 : 97195 : decl
5549 : 97195 : = add_builtin_function ("__builtin_is_corresponding_member",
5550 : : bool_vaftype,
5551 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5552 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5553 : 97195 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5554 : :
5555 : 97195 : decl
5556 : 97195 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5557 : : bool_vaftype,
5558 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5559 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5560 : 97195 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5561 : :
5562 : 97195 : if (cxx_dialect >= cxx26)
5563 : : {
5564 : 23752 : tree void_ptrintftype
5565 : 23752 : = build_function_type_list (void_type_node, ptr_type_node,
5566 : : integer_type_node, NULL_TREE);
5567 : 23752 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5568 : : void_ptrintftype,
5569 : : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5570 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5571 : 23752 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5572 : : }
5573 : :
5574 : 97195 : integer_two_node = build_int_cst (NULL_TREE, 2);
5575 : :
5576 : : /* Guess at the initial static decls size. */
5577 : 97195 : vec_alloc (static_decls, 500);
5578 : :
5579 : : /* ... and keyed classes. */
5580 : 97195 : vec_alloc (keyed_classes, 100);
5581 : :
5582 : 97195 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5583 : 97195 : truthvalue_type_node = boolean_type_node;
5584 : 97195 : truthvalue_false_node = boolean_false_node;
5585 : 97195 : truthvalue_true_node = boolean_true_node;
5586 : :
5587 : 97195 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5588 : 97195 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5589 : 97195 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5590 : 97195 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5591 : : NULL_TREE);
5592 : :
5593 : : #if 0
5594 : : record_builtin_type (RID_MAX, NULL, string_type_node);
5595 : : #endif
5596 : :
5597 : 97195 : delta_type_node = ptrdiff_type_node;
5598 : 97195 : vtable_index_type = ptrdiff_type_node;
5599 : :
5600 : 97195 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5601 : 97195 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5602 : :
5603 : : /* Create the conversion operator marker. This operator's DECL_NAME
5604 : : is in the identifier table, so we can use identifier equality to
5605 : : find it. */
5606 : 97195 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5607 : : void_ftype);
5608 : :
5609 : : /* C++ extensions */
5610 : :
5611 : 97195 : unknown_type_node = make_node (LANG_TYPE);
5612 : 97195 : record_unknown_type (unknown_type_node, "unknown type");
5613 : :
5614 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5615 : 97195 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5616 : :
5617 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5618 : : result. */
5619 : 97195 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5620 : 97195 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5621 : :
5622 : 97195 : init_list_type_node = make_node (LANG_TYPE);
5623 : 97195 : record_unknown_type (init_list_type_node, "init list");
5624 : :
5625 : : /* Used when parsing to distinguish parameter-lists () and (void). */
5626 : 97195 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5627 : :
5628 : 97195 : {
5629 : : /* Make sure we get a unique function type, so we can give
5630 : : its pointer type a name. (This wins for gdb.) */
5631 : 97195 : tree vfunc_type = make_node (FUNCTION_TYPE);
5632 : 97195 : TREE_TYPE (vfunc_type) = integer_type_node;
5633 : 97195 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5634 : 97195 : layout_type (vfunc_type);
5635 : :
5636 : 97195 : vtable_entry_type = build_pointer_type (vfunc_type);
5637 : : }
5638 : 97195 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5639 : :
5640 : 97195 : vtbl_type_node
5641 : 97195 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5642 : 97195 : layout_type (vtbl_type_node);
5643 : 97195 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5644 : 97195 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5645 : 97195 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5646 : 97195 : layout_type (vtbl_ptr_type_node);
5647 : 97195 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5648 : :
5649 : 97195 : push_namespace (get_identifier ("__cxxabiv1"));
5650 : 97195 : abi_node = current_namespace;
5651 : 97195 : pop_namespace ();
5652 : :
5653 : 97195 : any_targ_node = make_node (LANG_TYPE);
5654 : 97195 : record_unknown_type (any_targ_node, "any type");
5655 : :
5656 : : /* Now, C++. */
5657 : 97195 : current_lang_name = lang_name_cplusplus;
5658 : :
5659 : 97195 : if (aligned_new_threshold > 1
5660 : 97195 : && !pow2p_hwi (aligned_new_threshold))
5661 : : {
5662 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
5663 : : aligned_new_threshold);
5664 : 0 : aligned_new_threshold = 1;
5665 : : }
5666 : 97195 : if (aligned_new_threshold == -1)
5667 : 118130 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5668 : 97195 : if (aligned_new_threshold == 1)
5669 : 76243 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5670 : :
5671 : : /* Ensure attribs.cc is initialized. */
5672 : 97195 : init_attributes ();
5673 : 97195 : cxx_init_operator_new_delete_decls ();
5674 : :
5675 : : /* C++-specific nullptr initialization. */
5676 : 97195 : if (abi_version_at_least (9))
5677 : 97024 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5678 : 97195 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5679 : :
5680 : 97195 : if (! supports_one_only ())
5681 : 0 : flag_weak = 0;
5682 : :
5683 : 97195 : abort_fndecl
5684 : 97195 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5685 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5686 : 97195 : if (flag_weak)
5687 : : /* If no definition is available, resolve references to NULL. */
5688 : 97165 : declare_weak (abort_fndecl);
5689 : :
5690 : : /* Perform other language dependent initializations. */
5691 : 97195 : init_class_processing ();
5692 : 97195 : init_rtti_processing ();
5693 : 97195 : init_template_processing ();
5694 : :
5695 : 97195 : if (flag_exceptions)
5696 : 95945 : init_exception_processing ();
5697 : :
5698 : 97195 : if (flag_contracts)
5699 : 155 : init_terminate_fn ();
5700 : :
5701 : 97195 : if (modules_p ())
5702 : 4650 : init_modules (parse_in);
5703 : :
5704 : 97195 : make_fname_decl = cp_make_fname_decl;
5705 : 97195 : start_fname_decls ();
5706 : :
5707 : : /* Show we use EH for cleanups. */
5708 : 97195 : if (flag_exceptions)
5709 : 95945 : using_eh_for_cleanups ();
5710 : :
5711 : : /* Check that the hardware interference sizes are at least
5712 : : alignof(max_align_t), as required by the standard. */
5713 : 97195 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5714 : 97195 : if (OPTION_SET_P (param_destruct_interfere_size))
5715 : : {
5716 : 0 : if (param_destruct_interfere_size < max_align)
5717 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5718 : : "%d", param_destruct_interfere_size, max_align);
5719 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5720 : 0 : warning (OPT_Winterference_size,
5721 : : "%<--param destructive-interference-size=%d%> "
5722 : : "is less than %<--param l1-cache-line-size=%d%>",
5723 : : param_destruct_interfere_size, param_l1_cache_line_size);
5724 : : }
5725 : 97195 : else if (param_destruct_interfere_size)
5726 : : /* Assume the internal value is OK. */;
5727 : 0 : else if (param_l1_cache_line_size >= max_align)
5728 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5729 : : /* else leave it unset. */
5730 : :
5731 : 97195 : if (OPTION_SET_P (param_construct_interfere_size))
5732 : : {
5733 : 0 : if (param_construct_interfere_size < max_align)
5734 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5735 : : "%d", param_construct_interfere_size, max_align);
5736 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5737 : 0 : && param_l1_cache_line_size >= max_align)
5738 : 0 : warning (OPT_Winterference_size,
5739 : : "%<--param constructive-interference-size=%d%> "
5740 : : "is greater than %<--param l1-cache-line-size=%d%>",
5741 : : param_construct_interfere_size, param_l1_cache_line_size);
5742 : : }
5743 : 97195 : else if (param_construct_interfere_size)
5744 : : /* Assume the internal value is OK. */;
5745 : 0 : else if (param_l1_cache_line_size >= max_align)
5746 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5747 : 97195 : }
5748 : :
5749 : : /* Enter an abi node in global-module context. returns a cookie to
5750 : : give to pop_abi_namespace. */
5751 : :
5752 : : unsigned
5753 : 61996 : push_abi_namespace (tree node)
5754 : : {
5755 : 61996 : push_nested_namespace (node);
5756 : 61996 : push_visibility ("default", 2);
5757 : 61996 : unsigned flags = module_kind;
5758 : 61996 : module_kind = 0;
5759 : 61996 : return flags;
5760 : : }
5761 : :
5762 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5763 : : you. */
5764 : :
5765 : : void
5766 : 61996 : pop_abi_namespace (unsigned flags, tree node)
5767 : : {
5768 : 61996 : module_kind = flags;
5769 : 61996 : pop_visibility (2);
5770 : 61996 : pop_nested_namespace (node);
5771 : 61996 : }
5772 : :
5773 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5774 : : the decl, LOC is the location to give the decl, NAME is the
5775 : : initialization string and TYPE_DEP indicates whether NAME depended
5776 : : on the type of the function. We make use of that to detect
5777 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5778 : : at the point of first use, so we mustn't push the decl now. */
5779 : :
5780 : : static tree
5781 : 132112 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5782 : : {
5783 : 132112 : tree domain = NULL_TREE;
5784 : 132112 : tree init = NULL_TREE;
5785 : :
5786 : 132112 : if (!(type_dep && current_function_decl && in_template_context))
5787 : : {
5788 : 41377 : const char *name = NULL;
5789 : 41377 : bool release_name = false;
5790 : :
5791 : 41377 : if (current_function_decl == NULL_TREE)
5792 : : name = "top level";
5793 : 41363 : else if (type_dep == 0)
5794 : : {
5795 : : /* __FUNCTION__ */
5796 : 620 : name = fname_as_string (type_dep);
5797 : 620 : release_name = true;
5798 : : }
5799 : : else
5800 : : {
5801 : : /* __PRETTY_FUNCTION__ */
5802 : 40743 : gcc_checking_assert (type_dep == 1);
5803 : 40743 : name = cxx_printable_name (current_function_decl, 2);
5804 : : }
5805 : :
5806 : 41363 : if (!release_name)
5807 : : {
5808 : 40757 : cpp_string cstr = { 0, 0 }, strname;
5809 : 40757 : size_t len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5810 : 40757 : char *namep = XNEWVEC (char, len);
5811 : 40757 : snprintf (namep, len, "\"%s\"", name);
5812 : 40757 : strname.text = (unsigned char *) namep;
5813 : 40757 : strname.len = len - 1;
5814 : 40757 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
5815 : : {
5816 : 40757 : name = (const char *) cstr.text;
5817 : 40757 : release_name = true;
5818 : : }
5819 : :
5820 : 40757 : XDELETEVEC (namep);
5821 : : }
5822 : :
5823 : 41377 : size_t length = strlen (name);
5824 : 41377 : domain = build_index_type (size_int (length));
5825 : 41377 : init = build_string (length + 1, name);
5826 : 41377 : if (release_name)
5827 : 41377 : free (const_cast<char *> (name));
5828 : : }
5829 : :
5830 : 132112 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5831 : 132112 : type = build_cplus_array_type (type, domain);
5832 : :
5833 : 132112 : if (init)
5834 : 41377 : TREE_TYPE (init) = type;
5835 : : else
5836 : 90735 : init = error_mark_node;
5837 : :
5838 : 132112 : tree decl = build_decl (loc, VAR_DECL, id, type);
5839 : :
5840 : 132112 : TREE_READONLY (decl) = 1;
5841 : 132112 : DECL_ARTIFICIAL (decl) = 1;
5842 : 132112 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5843 : 132112 : TREE_STATIC (decl) = 1;
5844 : :
5845 : 132112 : TREE_USED (decl) = 1;
5846 : :
5847 : 132112 : SET_DECL_VALUE_EXPR (decl, init);
5848 : 132112 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5849 : : /* For decl_constant_var_p. */
5850 : 132112 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5851 : :
5852 : 132112 : if (current_function_decl)
5853 : : {
5854 : 132098 : DECL_CONTEXT (decl) = current_function_decl;
5855 : 132098 : decl = pushdecl_outermost_localscope (decl);
5856 : 132098 : if (decl != error_mark_node)
5857 : 132095 : add_decl_expr (decl);
5858 : : else
5859 : 3 : gcc_assert (seen_error ());
5860 : : }
5861 : : else
5862 : : {
5863 : 14 : DECL_THIS_STATIC (decl) = true;
5864 : 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5865 : : }
5866 : :
5867 : 132112 : return decl;
5868 : : }
5869 : :
5870 : : /* Install DECL as a builtin function at current global scope. Return
5871 : : the new decl (if we found an existing version). Also installs it
5872 : : into ::std, if it's not '_*'. */
5873 : :
5874 : : tree
5875 : 224885478 : cxx_builtin_function (tree decl)
5876 : : {
5877 : 224885478 : retrofit_lang_decl (decl);
5878 : :
5879 : 224885478 : DECL_ARTIFICIAL (decl) = 1;
5880 : 224885478 : SET_DECL_LANGUAGE (decl, lang_c);
5881 : : /* Runtime library routines are, by definition, available in an
5882 : : external shared object. */
5883 : 224885478 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5884 : 224885478 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5885 : :
5886 : 224885478 : tree id = DECL_NAME (decl);
5887 : 224885478 : const char *name = IDENTIFIER_POINTER (id);
5888 : 224885478 : bool hiding = false;
5889 : 224885478 : if (name[0] != '_' || name[1] != '_')
5890 : : /* In the user's namespace, it must be declared before use. */
5891 : : hiding = true;
5892 : 188528426 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5893 : 188528426 : && !startswith (name + 2, "builtin_")
5894 : 211144261 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5895 : : "_chk", strlen ("_chk") + 1))
5896 : : /* Treat __*_chk fortification functions as anticipated as well,
5897 : : unless they are __builtin_*_chk. */
5898 : : hiding = true;
5899 : :
5900 : : /* All builtins that don't begin with an '_' should additionally
5901 : : go in the 'std' namespace. */
5902 : 224885478 : if (name[0] != '_')
5903 : : {
5904 : 36223884 : tree std_decl = copy_decl (decl);
5905 : :
5906 : 36223884 : push_nested_namespace (std_node);
5907 : 36223884 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5908 : 36223884 : pushdecl (std_decl, hiding);
5909 : 36223884 : pop_nested_namespace (std_node);
5910 : : }
5911 : :
5912 : 224885478 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5913 : 224885478 : decl = pushdecl (decl, hiding);
5914 : :
5915 : 224885478 : return decl;
5916 : : }
5917 : :
5918 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5919 : : scope. This is to allow function specific options to add new machine
5920 : : dependent builtins when the target ISA changes via attribute((target(...)))
5921 : : which saves space on program startup if the program does not use non-generic
5922 : : ISAs. */
5923 : :
5924 : : tree
5925 : 717764 : cxx_builtin_function_ext_scope (tree decl)
5926 : : {
5927 : 717764 : push_nested_namespace (global_namespace);
5928 : 717764 : decl = cxx_builtin_function (decl);
5929 : 717764 : pop_nested_namespace (global_namespace);
5930 : :
5931 : 717764 : return decl;
5932 : : }
5933 : :
5934 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5935 : :
5936 : : tree
5937 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5938 : : {
5939 : 0 : retrofit_lang_decl (decl);
5940 : :
5941 : 0 : DECL_ARTIFICIAL (decl) = 1;
5942 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5943 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5944 : 0 : return pushdecl (decl);
5945 : : }
5946 : :
5947 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5948 : : function. Not called directly. */
5949 : :
5950 : : static tree
5951 : 1569448 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5952 : : int ecf_flags)
5953 : : {
5954 : 1569448 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5955 : 1569448 : DECL_EXTERNAL (fn) = 1;
5956 : 1569448 : TREE_PUBLIC (fn) = 1;
5957 : 1569448 : DECL_ARTIFICIAL (fn) = 1;
5958 : 1569448 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5959 : 1569448 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5960 : 1569448 : SET_DECL_LANGUAGE (fn, lang_c);
5961 : : /* Runtime library routines are, by definition, available in an
5962 : : external shared object. */
5963 : 1569448 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5964 : 1569448 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5965 : 1569448 : set_call_expr_flags (fn, ecf_flags);
5966 : 1569448 : return fn;
5967 : : }
5968 : :
5969 : : /* Returns the _DECL for a library function with C++ linkage. */
5970 : :
5971 : : static tree
5972 : 1097320 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5973 : : int ecf_flags)
5974 : : {
5975 : 1097320 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5976 : 1097320 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5977 : 1097320 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5978 : 1097320 : return fn;
5979 : : }
5980 : :
5981 : : /* Like build_library_fn, but takes a C string instead of an
5982 : : IDENTIFIER_NODE. */
5983 : :
5984 : : tree
5985 : 103698 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5986 : : {
5987 : 103698 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5988 : : }
5989 : :
5990 : : /* Like build_cp_library_fn, but takes a C string instead of an
5991 : : IDENTIFIER_NODE. */
5992 : :
5993 : : tree
5994 : 96036 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5995 : : {
5996 : 96036 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5997 : 96036 : ecf_flags);
5998 : : }
5999 : :
6000 : : /* Like build_library_fn, but also pushes the function so that we will
6001 : : be able to find it via get_global_binding. Also, the function
6002 : : may throw exceptions listed in RAISES. */
6003 : :
6004 : : tree
6005 : 368430 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6006 : : {
6007 : 368430 : if (raises)
6008 : 41958 : type = build_exception_variant (type, raises);
6009 : :
6010 : 368430 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
6011 : 368430 : return pushdecl_top_level (fn);
6012 : : }
6013 : :
6014 : : /* Like build_cp_library_fn, but also pushes the function so that it
6015 : : will be found by normal lookup. */
6016 : :
6017 : : static tree
6018 : 1001284 : push_cp_library_fn (enum tree_code operator_code, tree type,
6019 : : int ecf_flags)
6020 : : {
6021 : 1001284 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
6022 : : operator_code, type, ecf_flags);
6023 : 1001284 : pushdecl (fn);
6024 : 1001284 : if (flag_tm)
6025 : 3028 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
6026 : 1001284 : return fn;
6027 : : }
6028 : :
6029 : : /* Like push_library_fn, but also note that this function throws
6030 : : and does not return. Used for __throw_foo and the like. */
6031 : :
6032 : : tree
6033 : 107284 : push_throw_library_fn (tree name, tree type)
6034 : : {
6035 : 107284 : tree fn = push_library_fn (name, type, NULL_TREE,
6036 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6037 : 107284 : return fn;
6038 : : }
6039 : :
6040 : : /* When we call finish_struct for an anonymous union, we create
6041 : : default copy constructors and such. But, an anonymous union
6042 : : shouldn't have such things; this function undoes the damage to the
6043 : : anonymous union type T.
6044 : :
6045 : : (The reason that we create the synthesized methods is that we don't
6046 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
6047 : : The first is an anonymous union; the second is just an ordinary
6048 : : union type.) */
6049 : :
6050 : : void
6051 : 110189 : fixup_anonymous_aggr (tree t)
6052 : : {
6053 : : /* Wipe out memory of synthesized methods. */
6054 : 110189 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6055 : 110189 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6056 : 110189 : TYPE_HAS_COPY_CTOR (t) = 0;
6057 : 110189 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6058 : 110189 : TYPE_HAS_COPY_ASSIGN (t) = 0;
6059 : 110189 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6060 : :
6061 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6062 : : invalid members. */
6063 : 581885 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6064 : : {
6065 : 471696 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6066 : 0 : *prev_p = DECL_CHAIN (probe);
6067 : : else
6068 : 471696 : prev_p = &DECL_CHAIN (probe);
6069 : :
6070 : 471696 : if (DECL_ARTIFICIAL (probe)
6071 : 471696 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6072 : 43750 : || TYPE_ANON_P (TREE_TYPE (probe))))
6073 : 172219 : continue;
6074 : :
6075 : 299477 : if (TREE_CODE (probe) != FIELD_DECL
6076 : 299423 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6077 : : {
6078 : : /* We already complained about static data members in
6079 : : finish_static_data_member_decl. */
6080 : 66 : if (!VAR_P (probe))
6081 : : {
6082 : 51 : auto_diagnostic_group d;
6083 : 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
6084 : 51 : TREE_CODE (t) == UNION_TYPE
6085 : : ? "%q#D invalid; an anonymous union may "
6086 : : "only have public non-static data members"
6087 : : : "%q#D invalid; an anonymous struct may "
6088 : : "only have public non-static data members", probe))
6089 : : {
6090 : 51 : static bool hint;
6091 : 51 : if (flag_permissive && !hint)
6092 : : {
6093 : 6 : hint = true;
6094 : 6 : inform (DECL_SOURCE_LOCATION (probe),
6095 : : "this flexibility is deprecated and will be "
6096 : : "removed");
6097 : : }
6098 : : }
6099 : 51 : }
6100 : : }
6101 : : }
6102 : :
6103 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6104 : 110189 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6105 : 110189 : unsigned store = 0;
6106 : 245223 : for (tree elt : vec)
6107 : 135034 : if (!is_overloaded_fn (elt))
6108 : 135013 : (*vec)[store++] = elt;
6109 : 110189 : vec_safe_truncate (vec, store);
6110 : :
6111 : : /* Wipe RTTI info. */
6112 : 110189 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
6113 : :
6114 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6115 : : assignment operators (because they cannot have these methods themselves).
6116 : : For anonymous unions this is already checked because they are not allowed
6117 : : in any union, otherwise we have to check it. */
6118 : 110189 : if (TREE_CODE (t) != UNION_TYPE)
6119 : : {
6120 : 20357 : tree field, type;
6121 : :
6122 : 20357 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6123 : : {
6124 : 9 : error_at (location_of (t), "anonymous struct with base classes");
6125 : : /* Avoid ICE after error on anon-struct9.C. */
6126 : 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
6127 : : }
6128 : :
6129 : 92042 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6130 : 71685 : if (TREE_CODE (field) == FIELD_DECL)
6131 : : {
6132 : 51227 : type = TREE_TYPE (field);
6133 : 51227 : if (CLASS_TYPE_P (type))
6134 : : {
6135 : 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
6136 : 6 : error ("member %q+#D with constructor not allowed "
6137 : : "in anonymous aggregate", field);
6138 : 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6139 : 0 : error ("member %q+#D with destructor not allowed "
6140 : : "in anonymous aggregate", field);
6141 : 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6142 : 0 : error ("member %q+#D with copy assignment operator "
6143 : : "not allowed in anonymous aggregate", field);
6144 : : }
6145 : : }
6146 : : }
6147 : 110189 : }
6148 : :
6149 : : /* Warn for an attribute located at LOCATION that appertains to the
6150 : : class type CLASS_TYPE that has not been properly placed after its
6151 : : class-key, in it class-specifier. */
6152 : :
6153 : : void
6154 : 108 : warn_misplaced_attr_for_class_type (location_t location,
6155 : : tree class_type)
6156 : : {
6157 : 108 : gcc_assert (OVERLOAD_TYPE_P (class_type));
6158 : :
6159 : 108 : auto_diagnostic_group d;
6160 : 108 : if (warning_at (location, OPT_Wattributes,
6161 : : "attribute ignored in declaration "
6162 : : "of %q#T", class_type))
6163 : 108 : inform (location,
6164 : : "attribute for %q#T must follow the %qs keyword",
6165 : : class_type, class_key_or_enum_as_string (class_type));
6166 : 108 : }
6167 : :
6168 : : /* Returns the cv-qualifiers that apply to the type specified
6169 : : by the DECLSPECS. */
6170 : :
6171 : : static int
6172 : 953323257 : get_type_quals (const cp_decl_specifier_seq *declspecs)
6173 : : {
6174 : 953323257 : int type_quals = TYPE_UNQUALIFIED;
6175 : :
6176 : 953323257 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6177 : 103530617 : type_quals |= TYPE_QUAL_CONST;
6178 : 953323257 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6179 : 1657842 : type_quals |= TYPE_QUAL_VOLATILE;
6180 : 953323257 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6181 : 34 : type_quals |= TYPE_QUAL_RESTRICT;
6182 : :
6183 : 953323257 : return type_quals;
6184 : : }
6185 : :
6186 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
6187 : : just declares a tagged type or anonymous union.
6188 : :
6189 : : Returns the type declared; or NULL_TREE if none. */
6190 : :
6191 : : tree
6192 : 33383822 : check_tag_decl (cp_decl_specifier_seq *declspecs,
6193 : : bool explicit_type_instantiation_p)
6194 : : {
6195 : 33383822 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6196 : 33383822 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6197 : : /* If a class, struct, or enum type is declared by the DECLSPECS
6198 : : (i.e, if a class-specifier, enum-specifier, or non-typename
6199 : : elaborated-type-specifier appears in the DECLSPECS),
6200 : : DECLARED_TYPE is set to the corresponding type. */
6201 : 33383822 : tree declared_type = NULL_TREE;
6202 : 33383822 : bool error_p = false;
6203 : :
6204 : 33383822 : if (declspecs->multiple_types_p)
6205 : 26 : error_at (smallest_type_location (declspecs),
6206 : : "multiple types in one declaration");
6207 : 33383796 : else if (declspecs->redefined_builtin_type)
6208 : : {
6209 : 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6210 : 21 : if (!in_system_header_at (loc))
6211 : 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
6212 : : declspecs->redefined_builtin_type);
6213 : 21 : return NULL_TREE;
6214 : : }
6215 : :
6216 : 33383801 : if (declspecs->type
6217 : 33383786 : && TYPE_P (declspecs->type)
6218 : 66561592 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6219 : 33176178 : && MAYBE_CLASS_TYPE_P (declspecs->type))
6220 : 1704564 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6221 : : declared_type = declspecs->type;
6222 : 207668 : else if (declspecs->type == error_mark_node)
6223 : 1459 : error_p = true;
6224 : :
6225 : 33383801 : if (type_uses_auto (declared_type))
6226 : : {
6227 : 8 : error_at (declspecs->locations[ds_type_spec],
6228 : : "%<auto%> can only be specified for variables "
6229 : : "or function declarations");
6230 : 8 : return error_mark_node;
6231 : : }
6232 : :
6233 : 33383793 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6234 : : declared_type = NULL_TREE;
6235 : :
6236 : 33383793 : if (!declared_type && !saw_friend && !error_p)
6237 : 91 : permerror (input_location, "declaration does not declare anything");
6238 : : /* Check for an anonymous union. */
6239 : 33176098 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6240 : 96440281 : && TYPE_UNNAMED_P (declared_type))
6241 : : {
6242 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
6243 : : can be omitted only when declaring a class (clause 9) or
6244 : : enumeration (7.2), that is, when the decl-specifier-seq contains
6245 : : either a class-specifier, an elaborated-type-specifier with
6246 : : a class-key (9.1), or an enum-specifier. In these cases and
6247 : : whenever a class-specifier or enum-specifier is present in the
6248 : : decl-specifier-seq, the identifiers in these specifiers are among
6249 : : the names being declared by the declaration (as class-name,
6250 : : enum-names, or enumerators, depending on the syntax). In such
6251 : : cases, and except for the declaration of an unnamed bit-field (9.6),
6252 : : the decl-specifier-seq shall introduce one or more names into the
6253 : : program, or shall redeclare a name introduced by a previous
6254 : : declaration. [Example:
6255 : : enum { }; // ill-formed
6256 : : typedef class { }; // ill-formed
6257 : : --end example] */
6258 : 110195 : if (saw_typedef)
6259 : : {
6260 : 6 : error_at (declspecs->locations[ds_typedef],
6261 : : "missing type-name in typedef-declaration");
6262 : 6 : return NULL_TREE;
6263 : : }
6264 : 110189 : /* Anonymous unions are objects, so they can have specifiers. */;
6265 : 110189 : SET_ANON_AGGR_TYPE_P (declared_type);
6266 : :
6267 : 110189 : if (TREE_CODE (declared_type) != UNION_TYPE)
6268 : 20357 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6269 : 20357 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6270 : : }
6271 : :
6272 : : else
6273 : : {
6274 : 33273507 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6275 : 9 : error_at (declspecs->locations[ds_inline],
6276 : : "%<inline%> can only be specified for functions");
6277 : 33273498 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6278 : 3 : error_at (declspecs->locations[ds_virtual],
6279 : : "%<virtual%> can only be specified for functions");
6280 : 33273495 : else if (saw_friend
6281 : 33273495 : && (!current_class_type
6282 : 1724909 : || current_scope () != current_class_type))
6283 : 0 : error_at (declspecs->locations[ds_friend],
6284 : : "%<friend%> can only be specified inside a class");
6285 : 33273495 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6286 : 3 : error_at (declspecs->locations[ds_explicit],
6287 : : "%<explicit%> can only be specified for constructors");
6288 : 33273492 : else if (declspecs->storage_class)
6289 : 3 : error_at (declspecs->locations[ds_storage_class],
6290 : : "a storage class can only be specified for objects "
6291 : : "and functions");
6292 : 33273489 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6293 : 9 : error_at (declspecs->locations[ds_const],
6294 : : "%<const%> can only be specified for objects and "
6295 : : "functions");
6296 : 33273480 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6297 : 3 : error_at (declspecs->locations[ds_volatile],
6298 : : "%<volatile%> can only be specified for objects and "
6299 : : "functions");
6300 : 33273477 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6301 : 3 : error_at (declspecs->locations[ds_restrict],
6302 : : "%<__restrict%> can only be specified for objects and "
6303 : : "functions");
6304 : 33273474 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6305 : 3 : error_at (declspecs->locations[ds_thread],
6306 : : "%<__thread%> can only be specified for objects "
6307 : : "and functions");
6308 : 33273471 : else if (saw_typedef)
6309 : 24 : warning_at (declspecs->locations[ds_typedef], 0,
6310 : : "%<typedef%> was ignored in this declaration");
6311 : 33273447 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6312 : 9 : error_at (declspecs->locations[ds_constexpr],
6313 : : "%qs cannot be used for type declarations", "constexpr");
6314 : 33273438 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6315 : 3 : error_at (declspecs->locations[ds_constinit],
6316 : : "%qs cannot be used for type declarations", "constinit");
6317 : 33273435 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6318 : 3 : error_at (declspecs->locations[ds_consteval],
6319 : : "%qs cannot be used for type declarations", "consteval");
6320 : : }
6321 : :
6322 : 33383787 : if (declspecs->attributes && warn_attributes && declared_type)
6323 : : {
6324 : 31 : location_t loc;
6325 : 25 : if (!CLASS_TYPE_P (declared_type)
6326 : 56 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6327 : : /* For a non-template class, use the name location. */
6328 : 22 : loc = location_of (declared_type);
6329 : : else
6330 : : /* For a template class (an explicit instantiation), use the
6331 : : current location. */
6332 : 9 : loc = input_location;
6333 : :
6334 : 31 : if (explicit_type_instantiation_p)
6335 : : /* [dcl.attr.grammar]/4:
6336 : :
6337 : : No attribute-specifier-seq shall appertain to an explicit
6338 : : instantiation. */
6339 : : {
6340 : 6 : auto_diagnostic_group d;
6341 : 6 : if (warning_at (loc, OPT_Wattributes,
6342 : : "attribute ignored in explicit instantiation %q#T",
6343 : : declared_type))
6344 : 6 : inform (loc,
6345 : : "no attribute can be applied to "
6346 : : "an explicit instantiation");
6347 : 6 : }
6348 : : else
6349 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
6350 : : }
6351 : :
6352 : 33383787 : if (declspecs->std_attributes
6353 : 83 : && declared_type
6354 : 33383867 : && any_nonignored_attribute_p (declspecs->std_attributes))
6355 : : {
6356 : 80 : auto_diagnostic_group d;
6357 : 80 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6358 : : "attribute ignored"))
6359 : 80 : inform (declspecs->locations[ds_std_attribute],
6360 : : "an attribute that appertains to a type-specifier is ignored");
6361 : 80 : }
6362 : :
6363 : : /* Diagnose invalid application of contracts, if any. */
6364 : 33383787 : if (find_contract (declspecs->attributes))
6365 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
6366 : : else
6367 : 33383786 : diagnose_misapplied_contracts (declspecs->std_attributes);
6368 : :
6369 : : return declared_type;
6370 : : }
6371 : :
6372 : : /* Called when a declaration is seen that contains no names to declare.
6373 : : If its type is a reference to a structure, union or enum inherited
6374 : : from a containing scope, shadow that tag name for the current scope
6375 : : with a forward reference.
6376 : : If its type defines a new named structure or union
6377 : : or defines an enum, it is valid but we need not do anything here.
6378 : : Otherwise, it is an error.
6379 : :
6380 : : C++: may have to grok the declspecs to learn about static,
6381 : : complain for anonymous unions.
6382 : :
6383 : : Returns the TYPE declared -- or NULL_TREE if none. */
6384 : :
6385 : : tree
6386 : 29253183 : shadow_tag (cp_decl_specifier_seq *declspecs)
6387 : : {
6388 : 29253183 : tree t = check_tag_decl (declspecs,
6389 : : /*explicit_type_instantiation_p=*/false);
6390 : :
6391 : 29253183 : if (!t)
6392 : : return NULL_TREE;
6393 : :
6394 : 29251711 : t = maybe_process_partial_specialization (t);
6395 : 29251711 : if (t == error_mark_node)
6396 : : return NULL_TREE;
6397 : :
6398 : : /* This is where the variables in an anonymous union are
6399 : : declared. An anonymous union declaration looks like:
6400 : : union { ... } ;
6401 : : because there is no declarator after the union, the parser
6402 : : sends that declaration here. */
6403 : 29250376 : if (ANON_AGGR_TYPE_P (t))
6404 : : {
6405 : 249 : fixup_anonymous_aggr (t);
6406 : :
6407 : 249 : if (TYPE_FIELDS (t))
6408 : : {
6409 : 249 : tree decl = grokdeclarator (/*declarator=*/NULL,
6410 : : declspecs, NORMAL, 0, NULL);
6411 : 249 : finish_anon_union (decl);
6412 : : }
6413 : : }
6414 : :
6415 : : return t;
6416 : : }
6417 : :
6418 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6419 : :
6420 : : tree
6421 : 355483896 : groktypename (cp_decl_specifier_seq *type_specifiers,
6422 : : const cp_declarator *declarator,
6423 : : bool is_template_arg)
6424 : : {
6425 : 355483896 : tree attrs;
6426 : 355483896 : tree type;
6427 : 56874024 : enum decl_context context
6428 : 355483896 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6429 : 355483896 : attrs = type_specifiers->attributes;
6430 : 355483896 : type_specifiers->attributes = NULL_TREE;
6431 : 355483896 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6432 : 355483896 : if (attrs && type != error_mark_node)
6433 : : {
6434 : 633 : if (CLASS_TYPE_P (type))
6435 : 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6436 : : "outside of definition", type);
6437 : 627 : else if (MAYBE_CLASS_TYPE_P (type))
6438 : : /* A template type parameter or other dependent type. */
6439 : 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6440 : : "type %qT without an associated declaration", type);
6441 : : else
6442 : 618 : cplus_decl_attributes (&type, attrs, 0);
6443 : : }
6444 : 355483896 : return type;
6445 : : }
6446 : :
6447 : : /* Process a DECLARATOR for a function-scope or namespace-scope
6448 : : variable or function declaration.
6449 : : (Function definitions go through start_function; class member
6450 : : declarations appearing in the body of the class go through
6451 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
6452 : : If an error occurs, the error_mark_node is returned instead.
6453 : :
6454 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6455 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6456 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
6457 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6458 : : implicitly initialized via a default constructor. It can also be
6459 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6460 : : mark the new decl as DECL_DECOMPOSITION_P.
6461 : :
6462 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6463 : : declaration.
6464 : :
6465 : : The scope represented by the context of the returned DECL is pushed
6466 : : (if it is not the global namespace) and is assigned to
6467 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
6468 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
6469 : :
6470 : : tree
6471 : 117958734 : start_decl (const cp_declarator *declarator,
6472 : : cp_decl_specifier_seq *declspecs,
6473 : : int initialized,
6474 : : tree attributes,
6475 : : tree prefix_attributes,
6476 : : tree *pushed_scope_p)
6477 : : {
6478 : 117958734 : tree decl;
6479 : 117958734 : tree context;
6480 : 117958734 : bool was_public;
6481 : 117958734 : int flags;
6482 : 117958734 : bool alias;
6483 : 117958734 : tree initial;
6484 : :
6485 : 117958734 : *pushed_scope_p = NULL_TREE;
6486 : :
6487 : 117958734 : if (prefix_attributes != error_mark_node)
6488 : 117958732 : attributes = attr_chainon (attributes, prefix_attributes);
6489 : :
6490 : 117958734 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6491 : : &attributes);
6492 : :
6493 : 117958734 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6494 : 117958734 : || decl == error_mark_node
6495 : 117956596 : || prefix_attributes == error_mark_node)
6496 : 2140 : return error_mark_node;
6497 : :
6498 : 117956594 : context = CP_DECL_CONTEXT (decl);
6499 : 117956594 : if (context != global_namespace)
6500 : 18619857 : *pushed_scope_p = push_scope (context);
6501 : :
6502 : 117956594 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6503 : : {
6504 : 27 : error_at (DECL_SOURCE_LOCATION (decl),
6505 : : "typedef %qD is initialized (use %qs instead)",
6506 : : decl, "decltype");
6507 : 27 : return error_mark_node;
6508 : : }
6509 : :
6510 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6511 : : with attribute validation. */
6512 : 117956567 : initial = DECL_INITIAL (decl);
6513 : :
6514 : 117956567 : if (initialized)
6515 : : {
6516 : 52369919 : if (! toplevel_bindings_p ()
6517 : 52369919 : && DECL_EXTERNAL (decl))
6518 : 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6519 : : decl);
6520 : 52369919 : DECL_EXTERNAL (decl) = 0;
6521 : 52369919 : if (toplevel_bindings_p ())
6522 : 4855563 : TREE_STATIC (decl) = 1;
6523 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6524 : : even though we might not yet have the initializer expression. */
6525 : 52369919 : if (!DECL_INITIAL (decl))
6526 : 51984291 : DECL_INITIAL (decl) = error_mark_node;
6527 : : }
6528 : 117956567 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6529 : :
6530 : 117956567 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6531 : 3982 : record_key_method_defined (decl);
6532 : :
6533 : : /* If this is a typedef that names the class for linkage purposes
6534 : : (7.1.3p8), apply any attributes directly to the type. */
6535 : 117956567 : if (TREE_CODE (decl) == TYPE_DECL
6536 : 20727815 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6537 : 126039205 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6538 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
6539 : : else
6540 : : flags = 0;
6541 : :
6542 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
6543 : 117956567 : cplus_decl_attributes (&decl, attributes, flags);
6544 : :
6545 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6546 : : assist with attribute validation. */
6547 : 117956567 : DECL_INITIAL (decl) = initial;
6548 : :
6549 : : /* Dllimported symbols cannot be defined. Static data members (which
6550 : : can be initialized in-class and dllimported) go through grokfield,
6551 : : not here, so we don't need to exclude those decls when checking for
6552 : : a definition. */
6553 : 117956567 : if (initialized && DECL_DLLIMPORT_P (decl))
6554 : : {
6555 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
6556 : : "definition of %q#D is marked %<dllimport%>", decl);
6557 : 0 : DECL_DLLIMPORT_P (decl) = 0;
6558 : : }
6559 : :
6560 : : /* If #pragma weak was used, mark the decl weak now. */
6561 : 117956567 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6562 : 45162780 : maybe_apply_pragma_weak (decl);
6563 : :
6564 : 117956567 : if (TREE_CODE (decl) == FUNCTION_DECL
6565 : 38877198 : && DECL_DECLARED_INLINE_P (decl)
6566 : 2126849 : && DECL_UNINLINABLE (decl)
6567 : 117956567 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6568 : : {
6569 : 0 : auto_urlify_attributes sentinel;
6570 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6571 : : "inline function %qD given attribute %qs", decl, "noinline");
6572 : 0 : }
6573 : :
6574 : 117956567 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6575 : : {
6576 : 787956 : bool this_tmpl = (current_template_depth
6577 : 787956 : > template_class_depth (context));
6578 : 787956 : if (VAR_P (decl))
6579 : : {
6580 : 413197 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6581 : 413197 : if (field == NULL_TREE
6582 : 413197 : || !(VAR_P (field) || variable_template_p (field)))
6583 : 7 : error ("%q+#D is not a static data member of %q#T", decl, context);
6584 : 413190 : else if (variable_template_p (field)
6585 : 413190 : && (DECL_LANG_SPECIFIC (decl)
6586 : 123 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6587 : : /* OK, specialization was already checked. */;
6588 : 413121 : else if (variable_template_p (field) && !this_tmpl)
6589 : : {
6590 : 3 : auto_diagnostic_group d;
6591 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6592 : : "non-member-template declaration of %qD", decl);
6593 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6594 : : "member template declaration here");
6595 : 3 : return error_mark_node;
6596 : 3 : }
6597 : : else
6598 : : {
6599 : 413118 : if (variable_template_p (field))
6600 : 51 : field = DECL_TEMPLATE_RESULT (field);
6601 : :
6602 : 413118 : if (DECL_CONTEXT (field) != context)
6603 : : {
6604 : 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6605 : 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6606 : : "to be defined as %<%T::%D%>",
6607 : 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6608 : 3 : context, DECL_NAME (decl));
6609 : 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6610 : : }
6611 : : /* Static data member are tricky; an in-class initialization
6612 : : still doesn't provide a definition, so the in-class
6613 : : declaration will have DECL_EXTERNAL set, but will have an
6614 : : initialization. Thus, duplicate_decls won't warn
6615 : : about this situation, and so we check here. */
6616 : 413118 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6617 : 9 : error ("duplicate initialization of %qD", decl);
6618 : 413118 : field = duplicate_decls (decl, field);
6619 : 413118 : if (field == error_mark_node)
6620 : : return error_mark_node;
6621 : 413062 : else if (field)
6622 : 413062 : decl = field;
6623 : : }
6624 : : }
6625 : : else
6626 : : {
6627 : 374772 : tree field = check_classfn (context, decl,
6628 : : this_tmpl
6629 : 13 : ? current_template_parms
6630 : : : NULL_TREE);
6631 : 374684 : if (field && field != error_mark_node
6632 : 749443 : && duplicate_decls (decl, field))
6633 : 374683 : decl = field;
6634 : : }
6635 : :
6636 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6637 : 787897 : DECL_IN_AGGR_P (decl) = 0;
6638 : : /* Do not mark DECL as an explicit specialization if it was not
6639 : : already marked as an instantiation; a declaration should
6640 : : never be marked as a specialization unless we know what
6641 : : template is being specialized. */
6642 : 787897 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6643 : : {
6644 : 373066 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6645 : 373066 : if (TREE_CODE (decl) == FUNCTION_DECL)
6646 : 351467 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6647 : 351467 : && DECL_DECLARED_INLINE_P (decl));
6648 : : else
6649 : 21599 : DECL_COMDAT (decl) = false;
6650 : :
6651 : : /* [temp.expl.spec] An explicit specialization of a static data
6652 : : member of a template is a definition if the declaration
6653 : : includes an initializer; otherwise, it is a declaration.
6654 : :
6655 : : We check for processing_specialization so this only applies
6656 : : to the new specialization syntax. */
6657 : 373066 : if (!initialized && processing_specialization)
6658 : 372864 : DECL_EXTERNAL (decl) = 1;
6659 : : }
6660 : :
6661 : 1160830 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6662 : : /* Aliases are definitions. */
6663 : 787954 : && !alias)
6664 : : {
6665 : 30 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6666 : 11 : permerror (declarator->id_loc,
6667 : : "declaration of %q#D outside of class is not definition",
6668 : : decl);
6669 : 19 : else if (flag_contract_strict_declarations)
6670 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6671 : : "declaration of %q#D outside of class is not definition",
6672 : : decl);
6673 : : }
6674 : : }
6675 : :
6676 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6677 : 117956508 : if (initialized == SD_DECOMPOSITION)
6678 : 199709 : fit_decomposition_lang_decl (decl, NULL_TREE);
6679 : :
6680 : 117956508 : was_public = TREE_PUBLIC (decl);
6681 : :
6682 : 196834706 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6683 : 118342154 : && current_function_decl)
6684 : : {
6685 : : /* A function-scope decl of some namespace-scope decl. */
6686 : 41371 : DECL_LOCAL_DECL_P (decl) = true;
6687 : 41371 : if (named_module_attach_p ())
6688 : 3 : error_at (declarator->id_loc,
6689 : : "block-scope extern declaration %q#D must not be"
6690 : : " attached to a named module", decl);
6691 : : }
6692 : :
6693 : : /* Enter this declaration into the symbol table. Don't push the plain
6694 : : VAR_DECL for a variable template. */
6695 : 117956508 : if (!template_parm_scope_p ()
6696 : 117956508 : || !VAR_P (decl))
6697 : 114863594 : decl = maybe_push_decl (decl);
6698 : :
6699 : 117956508 : if (processing_template_decl)
6700 : 72786714 : decl = push_template_decl (decl);
6701 : :
6702 : 117956508 : if (decl == error_mark_node)
6703 : : return error_mark_node;
6704 : :
6705 : 117956083 : if (VAR_P (decl)
6706 : 58351258 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6707 : 66180 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6708 : : /* But not templated variables. */
6709 : 117983563 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6710 : : {
6711 : : /* This is a const variable with implicit 'static'. Set
6712 : : DECL_THIS_STATIC so we can tell it from variables that are
6713 : : !TREE_PUBLIC because of the anonymous namespace. */
6714 : 27480 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6715 : 27480 : DECL_THIS_STATIC (decl) = 1;
6716 : : }
6717 : :
6718 : 64579133 : if (current_function_decl && VAR_P (decl)
6719 : 52562341 : && maybe_constexpr_fn (current_function_decl)
6720 : 125184372 : && cxx_dialect < cxx23)
6721 : : {
6722 : 3611366 : bool ok = false;
6723 : 3611366 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6724 : : {
6725 : 9 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6726 : 18 : error_at (DECL_SOURCE_LOCATION (decl),
6727 : : "%qD defined %<thread_local%> in %qs function only "
6728 : : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6729 : 9 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6730 : : ? "consteval" : "constexpr");
6731 : : }
6732 : 3611357 : else if (TREE_STATIC (decl))
6733 : : {
6734 : 50 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6735 : 72 : error_at (DECL_SOURCE_LOCATION (decl),
6736 : : "%qD defined %<static%> in %qs function only available "
6737 : : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6738 : 36 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6739 : : ? "consteval" : "constexpr");
6740 : : }
6741 : : else
6742 : : ok = true;
6743 : 45 : if (!ok)
6744 : 59 : cp_function_chain->invalid_constexpr = true;
6745 : : }
6746 : :
6747 : 117956083 : if (!processing_template_decl && VAR_P (decl))
6748 : 5906617 : start_decl_1 (decl, initialized);
6749 : :
6750 : 117956080 : return decl;
6751 : : }
6752 : :
6753 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6754 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6755 : : variable is initialized via an implicitly-called constructor.)
6756 : : This function must be called for ordinary variables (including, for
6757 : : example, implicit instantiations of templates), but must not be
6758 : : called for template declarations. */
6759 : :
6760 : : void
6761 : 5915585 : start_decl_1 (tree decl, bool initialized)
6762 : : {
6763 : 5915585 : gcc_checking_assert (!processing_template_decl);
6764 : :
6765 : 5915585 : if (error_operand_p (decl))
6766 : : return;
6767 : :
6768 : 5915570 : gcc_checking_assert (VAR_P (decl));
6769 : :
6770 : 5915570 : tree type = TREE_TYPE (decl);
6771 : 5915570 : bool complete_p = COMPLETE_TYPE_P (type);
6772 : 5915570 : bool aggregate_definition_p
6773 : 5915570 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6774 : :
6775 : : /* If an explicit initializer is present, or if this is a definition
6776 : : of an aggregate, then we need a complete type at this point.
6777 : : (Scalars are always complete types, so there is nothing to
6778 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6779 : : are issued below. */
6780 : 3652300 : if ((initialized || aggregate_definition_p)
6781 : 4873600 : && !complete_p
6782 : 4658184 : && COMPLETE_TYPE_P (complete_type (type)))
6783 : : {
6784 : 342069 : complete_p = true;
6785 : : /* We will not yet have set TREE_READONLY on DECL if the type
6786 : : was "const", but incomplete, before this point. But, now, we
6787 : : have a complete type, so we can try again. */
6788 : 342069 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6789 : : }
6790 : :
6791 : 5915567 : if (initialized)
6792 : : /* Is it valid for this decl to have an initializer at all? */
6793 : : {
6794 : : /* Don't allow initializations for incomplete types except for
6795 : : arrays which might be completed by the initialization. */
6796 : 4224346 : if (complete_p)
6797 : : ; /* A complete type is ok. */
6798 : 663582 : else if (type_uses_auto (type))
6799 : : ; /* An auto type is ok. */
6800 : 66498 : else if (TREE_CODE (type) != ARRAY_TYPE)
6801 : : {
6802 : 27 : auto_diagnostic_group d;
6803 : 27 : error ("variable %q#D has initializer but incomplete type", decl);
6804 : 48 : maybe_suggest_missing_header (input_location,
6805 : 27 : TYPE_IDENTIFIER (type),
6806 : 27 : CP_TYPE_CONTEXT (type));
6807 : 27 : type = TREE_TYPE (decl) = error_mark_node;
6808 : 27 : }
6809 : 66471 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6810 : : {
6811 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6812 : 0 : error ("elements of array %q#D have incomplete type", decl);
6813 : : /* else we already gave an error in start_decl. */
6814 : : }
6815 : : }
6816 : 1691221 : else if (aggregate_definition_p && !complete_p)
6817 : : {
6818 : 230 : if (type_uses_auto (type))
6819 : 68 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6820 : : else
6821 : : {
6822 : 162 : auto_diagnostic_group d;
6823 : 162 : error ("aggregate %q#D has incomplete type and cannot be defined",
6824 : : decl);
6825 : 279 : maybe_suggest_missing_header (input_location,
6826 : 162 : TYPE_IDENTIFIER (type),
6827 : 162 : CP_TYPE_CONTEXT (type));
6828 : : /* Change the type so that assemble_variable will give
6829 : : DECL an rtl we can live with: (mem (const_int 0)). */
6830 : 162 : type = TREE_TYPE (decl) = error_mark_node;
6831 : 162 : }
6832 : : }
6833 : :
6834 : : /* Create a new scope to hold this declaration if necessary.
6835 : : Whether or not a new scope is necessary cannot be determined
6836 : : until after the type has been completed; if the type is a
6837 : : specialization of a class template it is not until after
6838 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6839 : : will be set correctly. */
6840 : 5915567 : maybe_push_cleanup_level (type);
6841 : : }
6842 : :
6843 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6844 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6845 : :
6846 : : tree
6847 : 292 : do_aggregate_paren_init (tree init, tree type)
6848 : : {
6849 : 292 : tree val = TREE_VALUE (init);
6850 : :
6851 : 292 : if (TREE_CHAIN (init) == NULL_TREE)
6852 : : {
6853 : : /* If the list has a single element and it's a string literal,
6854 : : then it's the initializer for the array as a whole. */
6855 : 176 : if (TREE_CODE (type) == ARRAY_TYPE
6856 : 176 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6857 : 220 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6858 : : == STRING_CST)
6859 : : return val;
6860 : : /* Handle non-standard extensions like compound literals. This also
6861 : : prevents triggering aggregate parenthesized-initialization in
6862 : : compiler-generated code for =default. */
6863 : 137 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6864 : 137 : TREE_TYPE (val)))
6865 : : return val;
6866 : : }
6867 : :
6868 : 200 : init = build_constructor_from_list (init_list_type_node, init);
6869 : 200 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6870 : 200 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6871 : 200 : return init;
6872 : : }
6873 : :
6874 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6875 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6876 : : but will be set to a new CLEANUP_STMT if a temporary is created
6877 : : that must be destroyed subsequently.
6878 : :
6879 : : Returns an initializer expression to use to initialize DECL, or
6880 : : NULL if the initialization can be performed statically.
6881 : :
6882 : : Quotes on semantics can be found in ARM 8.4.3. */
6883 : :
6884 : : static tree
6885 : 333378 : grok_reference_init (tree decl, tree type, tree init, int flags)
6886 : : {
6887 : 333378 : if (init == NULL_TREE)
6888 : : {
6889 : 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6890 : 3 : || DECL_IN_AGGR_P (decl) == 0)
6891 : 18 : && ! DECL_THIS_EXTERN (decl))
6892 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
6893 : : "%qD declared as reference but not initialized", decl);
6894 : 15 : return NULL_TREE;
6895 : : }
6896 : :
6897 : 333363 : tree ttype = TREE_TYPE (type);
6898 : 333363 : if (TREE_CODE (init) == TREE_LIST)
6899 : : {
6900 : : /* This handles (C++20 only) code like
6901 : :
6902 : : const A& r(1, 2, 3);
6903 : :
6904 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6905 : 1301 : if (TREE_TYPE (init) == NULL_TREE
6906 : 1301 : && CP_AGGREGATE_TYPE_P (ttype)
6907 : 69 : && !DECL_DECOMPOSITION_P (decl)
6908 : 1367 : && (cxx_dialect >= cxx20))
6909 : : {
6910 : : /* We don't know yet if we should treat const A& r(1) as
6911 : : const A& r{1}. */
6912 : 43 : if (list_length (init) == 1)
6913 : : {
6914 : 22 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6915 : 22 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6916 : : tf_warning_or_error);
6917 : : }
6918 : : /* If the list had more than one element, the code is ill-formed
6919 : : pre-C++20, so we can build a constructor right away. */
6920 : : else
6921 : 21 : init = do_aggregate_paren_init (init, ttype);
6922 : : }
6923 : : else
6924 : 1258 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6925 : : tf_warning_or_error);
6926 : : }
6927 : :
6928 : 333363 : if (TREE_CODE (ttype) != ARRAY_TYPE
6929 : 333363 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6930 : : /* Note: default conversion is only called in very special cases. */
6931 : 12 : init = decay_conversion (init, tf_warning_or_error);
6932 : :
6933 : : /* check_initializer handles this for non-reference variables, but for
6934 : : references we need to do it here or the initializer will get the
6935 : : incomplete array type and confuse later calls to
6936 : : cp_complete_array_type. */
6937 : 333363 : if (TREE_CODE (ttype) == ARRAY_TYPE
6938 : 8996 : && TYPE_DOMAIN (ttype) == NULL_TREE
6939 : 333446 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6940 : 50 : || TREE_CODE (init) == STRING_CST))
6941 : : {
6942 : 33 : cp_complete_array_type (&ttype, init, false);
6943 : 33 : if (ttype != TREE_TYPE (type))
6944 : 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6945 : : }
6946 : :
6947 : : /* Convert INIT to the reference type TYPE. This may involve the
6948 : : creation of a temporary, whose lifetime must be the same as that
6949 : : of the reference. If so, a DECL_EXPR for the temporary will be
6950 : : added just after the DECL_EXPR for DECL. That's why we don't set
6951 : : DECL_INITIAL for local references (instead assigning to them
6952 : : explicitly); we need to allow the temporary to be initialized
6953 : : first. */
6954 : 333363 : return initialize_reference (type, init, flags,
6955 : 333363 : tf_warning_or_error);
6956 : : }
6957 : :
6958 : : /* Designated initializers in arrays are not supported in GNU C++.
6959 : : The parser cannot detect this error since it does not know whether
6960 : : a given brace-enclosed initializer is for a class type or for an
6961 : : array. This function checks that CE does not use a designated
6962 : : initializer. If it does, an error is issued. Returns true if CE
6963 : : is valid, i.e., does not have a designated initializer. */
6964 : :
6965 : : bool
6966 : 55178023 : check_array_designated_initializer (constructor_elt *ce,
6967 : : unsigned HOST_WIDE_INT index)
6968 : : {
6969 : : /* Designated initializers for array elements are not supported. */
6970 : 55178023 : if (ce->index)
6971 : : {
6972 : : /* The parser only allows identifiers as designated
6973 : : initializers. */
6974 : 36023357 : if (ce->index == error_mark_node)
6975 : : {
6976 : 0 : error ("name used in a GNU-style designated "
6977 : : "initializer for an array");
6978 : 0 : return false;
6979 : : }
6980 : 36023357 : else if (identifier_p (ce->index))
6981 : : {
6982 : 19 : error ("name %qD used in a GNU-style designated "
6983 : : "initializer for an array", ce->index);
6984 : 19 : ce->index = error_mark_node;
6985 : 19 : return false;
6986 : : }
6987 : :
6988 : 36023338 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6989 : : ce->index, true);
6990 : 36023338 : if (ce_index
6991 : 36023335 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6992 : 72046670 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6993 : : == INTEGER_CST))
6994 : : {
6995 : : /* A C99 designator is OK if it matches the current index. */
6996 : 36023332 : if (wi::to_wide (ce_index) == index)
6997 : : {
6998 : 36023326 : ce->index = ce_index;
6999 : 36023326 : return true;
7000 : : }
7001 : : else
7002 : 6 : sorry ("non-trivial designated initializers not supported");
7003 : : }
7004 : : else
7005 : 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
7006 : : "C99 designator %qE is not an integral constant-expression",
7007 : : ce->index);
7008 : :
7009 : 12 : return false;
7010 : : }
7011 : :
7012 : : return true;
7013 : : }
7014 : :
7015 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
7016 : : array until we finish parsing the initializer. If that's the
7017 : : situation we're in, update DECL accordingly. */
7018 : :
7019 : : static void
7020 : 61201568 : maybe_deduce_size_from_array_init (tree decl, tree init)
7021 : : {
7022 : 61201568 : tree type = TREE_TYPE (decl);
7023 : :
7024 : 61201568 : if (TREE_CODE (type) == ARRAY_TYPE
7025 : 847834 : && TYPE_DOMAIN (type) == NULL_TREE
7026 : 61758882 : && TREE_CODE (decl) != TYPE_DECL)
7027 : : {
7028 : : /* do_default is really a C-ism to deal with tentative definitions.
7029 : : But let's leave it here to ease the eventual merge. */
7030 : 557314 : int do_default = !DECL_EXTERNAL (decl);
7031 : 557314 : tree initializer = init ? init : DECL_INITIAL (decl);
7032 : 557314 : int failure = 0;
7033 : :
7034 : : /* Check that there are no designated initializers in INIT, as
7035 : : those are not supported in GNU C++, and as the middle-end
7036 : : will crash if presented with a non-numeric designated
7037 : : initializer. */
7038 : 557314 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7039 : : {
7040 : 97817 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7041 : 97817 : constructor_elt *ce;
7042 : 97817 : HOST_WIDE_INT i, j = 0;
7043 : 17420514 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7044 : : {
7045 : 17324289 : if (instantiation_dependent_expression_p (ce->index))
7046 : 61201568 : return;
7047 : 17324283 : if (!check_array_designated_initializer (ce, j))
7048 : 1 : failure = 1;
7049 : : /* If an un-designated initializer is type-dependent, we can't
7050 : : check brace elision yet. */
7051 : 17324283 : if (ce->index == NULL_TREE
7052 : 17324283 : && type_dependent_expression_p (ce->value))
7053 : : return;
7054 : 17322697 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7055 : 196 : j += RAW_DATA_LENGTH (ce->value);
7056 : : else
7057 : 17322501 : ++j;
7058 : : }
7059 : : }
7060 : :
7061 : 96225 : if (failure)
7062 : 1 : TREE_TYPE (decl) = error_mark_node;
7063 : : else
7064 : : {
7065 : 555721 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7066 : : do_default);
7067 : 555721 : if (failure == 1)
7068 : : {
7069 : 33 : error_at (cp_expr_loc_or_loc (initializer,
7070 : 30 : DECL_SOURCE_LOCATION (decl)),
7071 : : "initializer fails to determine size of %qD", decl);
7072 : : }
7073 : 555691 : else if (failure == 2)
7074 : : {
7075 : 30 : if (do_default)
7076 : : {
7077 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7078 : : "array size missing in %qD", decl);
7079 : : }
7080 : : /* If a `static' var's size isn't known, make it extern as
7081 : : well as static, so it does not get allocated. If it's not
7082 : : `static', then don't mark it extern; finish_incomplete_decl
7083 : : will give it a default size and it will get allocated. */
7084 : 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7085 : 0 : DECL_EXTERNAL (decl) = 1;
7086 : : }
7087 : 555661 : else if (failure == 3)
7088 : : {
7089 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7090 : : "zero-size array %qD", decl);
7091 : : }
7092 : : }
7093 : :
7094 : 555722 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7095 : :
7096 : 555722 : relayout_decl (decl);
7097 : :
7098 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
7099 : 555722 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7100 : : {
7101 : 320 : tree tmpl = template_for_substitution (decl);
7102 : 320 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
7103 : 52 : TREE_TYPE (tmpl) = TREE_TYPE (decl);
7104 : : }
7105 : : }
7106 : : }
7107 : :
7108 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7109 : : any appropriate error messages regarding the layout. */
7110 : :
7111 : : static void
7112 : 52279746 : layout_var_decl (tree decl)
7113 : : {
7114 : 52279746 : tree type;
7115 : :
7116 : 52279746 : type = TREE_TYPE (decl);
7117 : 52279746 : if (type == error_mark_node)
7118 : : return;
7119 : :
7120 : : /* If we haven't already laid out this declaration, do so now.
7121 : : Note that we must not call complete type for an external object
7122 : : because it's type might involve templates that we are not
7123 : : supposed to instantiate yet. (And it's perfectly valid to say
7124 : : `extern X x' for some incomplete type `X'.) */
7125 : 52279699 : if (!DECL_EXTERNAL (decl))
7126 : 43399340 : complete_type (type);
7127 : 52279699 : if (!DECL_SIZE (decl)
7128 : 541168 : && TREE_TYPE (decl) != error_mark_node
7129 : 52820867 : && complete_or_array_type_p (type))
7130 : 478860 : layout_decl (decl, 0);
7131 : :
7132 : 52279699 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7133 : : {
7134 : : /* An automatic variable with an incomplete type: that is an error.
7135 : : Don't talk about array types here, since we took care of that
7136 : : message in grokdeclarator. */
7137 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
7138 : : "storage size of %qD isn%'t known", decl);
7139 : 17 : TREE_TYPE (decl) = error_mark_node;
7140 : : }
7141 : : #if 0
7142 : : /* Keep this code around in case we later want to control debug info
7143 : : based on whether a type is "used". (jason 1999-11-11) */
7144 : :
7145 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7146 : : /* Let debugger know it should output info for this type. */
7147 : : note_debug_info_needed (ttype);
7148 : :
7149 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7150 : : note_debug_info_needed (DECL_CONTEXT (decl));
7151 : : #endif
7152 : :
7153 : 95679039 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7154 : 28210883 : && DECL_SIZE (decl) != NULL_TREE
7155 : 80291896 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
7156 : : {
7157 : 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7158 : 6 : && !DECL_LOCAL_DECL_P (decl))
7159 : 0 : constant_expression_warning (DECL_SIZE (decl));
7160 : : else
7161 : : {
7162 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7163 : : "storage size of %qD isn%'t constant", decl);
7164 : 6 : TREE_TYPE (decl) = error_mark_node;
7165 : 6 : type = error_mark_node;
7166 : : }
7167 : : }
7168 : :
7169 : : /* If the final element initializes a flexible array field, adjust
7170 : : the size of the DECL with the initializer based on whether the
7171 : : DECL is a union or a structure. */
7172 : 52279699 : if (type != error_mark_node
7173 : 52279693 : && DECL_INITIAL (decl)
7174 : 28131450 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7175 : 4044638 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7176 : 3272864 : && DECL_SIZE (decl) != NULL_TREE
7177 : 3272864 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7178 : 3272864 : && COMPLETE_TYPE_P (type)
7179 : 3272864 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7180 : 55552563 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7181 : : {
7182 : 3272864 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7183 : 3272864 : if (elt.index)
7184 : : {
7185 : 1088883 : tree itype = TREE_TYPE (elt.index);
7186 : 1088883 : tree vtype = TREE_TYPE (elt.value);
7187 : 1088883 : if (TREE_CODE (itype) == ARRAY_TYPE
7188 : 57260 : && TYPE_DOMAIN (itype) == NULL
7189 : 272 : && TREE_CODE (vtype) == ARRAY_TYPE
7190 : 1089155 : && COMPLETE_TYPE_P (vtype))
7191 : : {
7192 : : /* For a structure, add the size of the initializer to the DECL's
7193 : : size. */
7194 : 272 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7195 : : {
7196 : 257 : DECL_SIZE (decl)
7197 : 257 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7198 : : TYPE_SIZE (vtype));
7199 : 257 : DECL_SIZE_UNIT (decl)
7200 : 514 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7201 : : TYPE_SIZE_UNIT (vtype));
7202 : : }
7203 : : /* For a union, the DECL's size is the maximum of the current size
7204 : : and the size of the initializer. */
7205 : : else
7206 : : {
7207 : 15 : DECL_SIZE (decl)
7208 : 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
7209 : : TYPE_SIZE (vtype));
7210 : 15 : DECL_SIZE_UNIT (decl)
7211 : 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7212 : : TYPE_SIZE_UNIT (vtype));
7213 : : }
7214 : : }
7215 : : }
7216 : : }
7217 : : }
7218 : :
7219 : : /* If a local static variable is declared in an inline function, or if
7220 : : we have a weak definition, we must endeavor to create only one
7221 : : instance of the variable at link-time. */
7222 : :
7223 : : void
7224 : 60488367 : maybe_commonize_var (tree decl)
7225 : : {
7226 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
7227 : : bindings. */
7228 : 60488367 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7229 : : return;
7230 : :
7231 : : /* Static data in a function with comdat linkage also has comdat
7232 : : linkage. */
7233 : 55345322 : if ((TREE_STATIC (decl)
7234 : 30932338 : && DECL_FUNCTION_SCOPE_P (decl)
7235 : 143792 : && vague_linkage_p (DECL_CONTEXT (decl)))
7236 : 117385530 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7237 : : {
7238 : 26125939 : if (flag_weak)
7239 : : {
7240 : : /* With weak symbols, we simply make the variable COMDAT;
7241 : : that will cause copies in multiple translations units to
7242 : : be merged. */
7243 : 26125912 : comdat_linkage (decl);
7244 : : }
7245 : : else
7246 : : {
7247 : 27 : if (DECL_INITIAL (decl) == NULL_TREE
7248 : 27 : || DECL_INITIAL (decl) == error_mark_node)
7249 : : {
7250 : : /* Without weak symbols, we can use COMMON to merge
7251 : : uninitialized variables. */
7252 : 24 : TREE_PUBLIC (decl) = 1;
7253 : 24 : DECL_COMMON (decl) = 1;
7254 : : }
7255 : : else
7256 : : {
7257 : : /* While for initialized variables, we must use internal
7258 : : linkage -- which means that multiple copies will not
7259 : : be merged. */
7260 : 3 : TREE_PUBLIC (decl) = 0;
7261 : 3 : DECL_COMMON (decl) = 0;
7262 : 3 : DECL_INTERFACE_KNOWN (decl) = 1;
7263 : 3 : const char *msg;
7264 : 3 : if (DECL_INLINE_VAR_P (decl))
7265 : : msg = G_("sorry: semantics of inline variable "
7266 : : "%q#D are wrong (you%'ll wind up with "
7267 : : "multiple copies)");
7268 : : else
7269 : : msg = G_("sorry: semantics of inline function "
7270 : : "static data %q#D are wrong (you%'ll wind "
7271 : : "up with multiple copies)");
7272 : 3 : auto_diagnostic_group d;
7273 : 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7274 : : msg, decl))
7275 : 3 : inform (DECL_SOURCE_LOCATION (decl),
7276 : : "you can work around this by removing the initializer");
7277 : 3 : }
7278 : : }
7279 : : }
7280 : : }
7281 : :
7282 : : /* Issue an error message if DECL is an uninitialized const variable.
7283 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7284 : : context from potential_constant_expression. Returns true if all is well,
7285 : : false otherwise. */
7286 : :
7287 : : bool
7288 : 6959800 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7289 : : tsubst_flags_t complain)
7290 : : {
7291 : 6959800 : tree type = strip_array_types (TREE_TYPE (decl));
7292 : :
7293 : : /* ``Unless explicitly declared extern, a const object does not have
7294 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
7295 : : 7.1.6 */
7296 : 6959800 : if (VAR_P (decl)
7297 : 6959800 : && !TYPE_REF_P (type)
7298 : 6940240 : && (CP_TYPE_CONST_P (type)
7299 : : /* C++20 permits trivial default initialization in constexpr
7300 : : context (P1331R2). */
7301 : 5805031 : || (cxx_dialect < cxx20
7302 : 2528565 : && (constexpr_context_p
7303 : 2251741 : || var_in_constexpr_fn (decl))))
7304 : 8481603 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7305 : : {
7306 : 667389 : tree field = default_init_uninitialized_part (type);
7307 : 667389 : if (!field)
7308 : : return true;
7309 : :
7310 : 111482 : auto_diagnostic_group d;
7311 : 111482 : bool show_notes = true;
7312 : :
7313 : 111482 : if (!constexpr_context_p || cxx_dialect >= cxx20)
7314 : : {
7315 : 107753 : if (CP_TYPE_CONST_P (type))
7316 : : {
7317 : 90 : if (complain & tf_error)
7318 : 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7319 : : "uninitialized %<const %D%>", decl);
7320 : : }
7321 : : else
7322 : : {
7323 : 107663 : if (!is_instantiation_of_constexpr (current_function_decl)
7324 : 107663 : && (complain & tf_error))
7325 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
7326 : : "uninitialized variable %qD in %<constexpr%> "
7327 : : "function", decl);
7328 : : else
7329 : : show_notes = false;
7330 : 107663 : cp_function_chain->invalid_constexpr = true;
7331 : : }
7332 : : }
7333 : 3729 : else if (complain & tf_error)
7334 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
7335 : : "uninitialized variable %qD in %<constexpr%> context",
7336 : : decl);
7337 : :
7338 : 111482 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7339 : : {
7340 : 62 : tree defaulted_ctor;
7341 : :
7342 : 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7343 : : "%q#T has no user-provided default constructor", type);
7344 : 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
7345 : 62 : if (defaulted_ctor)
7346 : 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7347 : : "constructor is not user-provided because it is "
7348 : : "explicitly defaulted in the class body");
7349 : 62 : inform (DECL_SOURCE_LOCATION (field),
7350 : : "and the implicitly-defined constructor does not "
7351 : : "initialize %q#D", field);
7352 : : }
7353 : :
7354 : 111482 : return false;
7355 : 111482 : }
7356 : :
7357 : : return true;
7358 : : }
7359 : :
7360 : : /* Structure holding the current initializer being processed by reshape_init.
7361 : : CUR is a pointer to the current element being processed, END is a pointer
7362 : : after the last element present in the initializer and RAW_IDX is index into
7363 : : RAW_DATA_CST if that is CUR elt. */
7364 : : struct reshape_iter
7365 : : {
7366 : : constructor_elt *cur;
7367 : : constructor_elt *end;
7368 : : unsigned raw_idx;
7369 : : };
7370 : :
7371 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7372 : :
7373 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7374 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
7375 : : initialized as if for an aggregate class. If there are no more such fields,
7376 : : the return value will be NULL. */
7377 : :
7378 : : tree
7379 : 15129720 : next_aggregate_field (tree field)
7380 : : {
7381 : 15129720 : while (field
7382 : 52970825 : && (TREE_CODE (field) != FIELD_DECL
7383 : 9719719 : || DECL_UNNAMED_BIT_FIELD (field)
7384 : 9719597 : || (DECL_ARTIFICIAL (field)
7385 : : /* In C++17, aggregates can have bases. */
7386 : 81457 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7387 : 37841105 : field = DECL_CHAIN (field);
7388 : :
7389 : 15129720 : return field;
7390 : : }
7391 : :
7392 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7393 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7394 : : to a subobject. If there are no more such fields, the return value will be
7395 : : NULL. */
7396 : :
7397 : : tree
7398 : 58197244 : next_subobject_field (tree field)
7399 : : {
7400 : 58197244 : while (field
7401 : 713420396 : && (TREE_CODE (field) != FIELD_DECL
7402 : 26646203 : || DECL_UNNAMED_BIT_FIELD (field)
7403 : 26646112 : || (DECL_ARTIFICIAL (field)
7404 : 7212677 : && !DECL_FIELD_IS_BASE (field)
7405 : 1772043 : && !DECL_VIRTUAL_P (field))))
7406 : 655223152 : field = DECL_CHAIN (field);
7407 : :
7408 : 58197244 : return field;
7409 : : }
7410 : :
7411 : : /* Return true for [dcl.init.list] direct-list-initialization from
7412 : : single element of enumeration with a fixed underlying type. */
7413 : :
7414 : : bool
7415 : 135292026 : is_direct_enum_init (tree type, tree init)
7416 : : {
7417 : 135292026 : if (cxx_dialect >= cxx17
7418 : 133271991 : && TREE_CODE (type) == ENUMERAL_TYPE
7419 : 6491316 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7420 : 4129134 : && TREE_CODE (init) == CONSTRUCTOR
7421 : 427159 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
7422 : 218867 : && CONSTRUCTOR_NELTS (init) == 1
7423 : 218 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7424 : : /* DR 2374: The single element needs to be implicitly
7425 : : convertible to the underlying type of the enum. */
7426 : 218 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7427 : 135292242 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7428 : 216 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7429 : 216 : CONSTRUCTOR_ELT (init, 0)->value,
7430 : : LOOKUP_IMPLICIT, tf_none))
7431 : : return true;
7432 : : return false;
7433 : : }
7434 : :
7435 : : /* Helper function for reshape_init*. Split first element of
7436 : : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7437 : : to true if the whole d->cur has been consumed. */
7438 : :
7439 : : static tree
7440 : 25824364 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7441 : : {
7442 : 25824364 : *inc_cur = true;
7443 : 25824364 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7444 : : return NULL_TREE;
7445 : 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7446 : 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7447 : 30588 : *inc_cur = false;
7448 : : else
7449 : 132 : d->raw_idx = 0;
7450 : : return ret;
7451 : : }
7452 : :
7453 : : /* Wrapper around that which for RAW_DATA_CST in INIT
7454 : : (as well as in D->cur->value) peels off the first element
7455 : : of the raw data and returns it, otherwise increments
7456 : : D->cur and returns INIT. */
7457 : :
7458 : : static tree
7459 : 25824348 : consume_init (tree init, reshape_iter *d)
7460 : : {
7461 : 25824348 : bool inc_cur;
7462 : 25824348 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7463 : 30720 : init = raw_init;
7464 : 25824348 : if (inc_cur)
7465 : 25793760 : d->cur++;
7466 : 25824348 : return init;
7467 : : }
7468 : :
7469 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
7470 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7471 : : INTEGER_CST representing the size of the array minus one (the maximum index),
7472 : : or NULL_TREE if the array was declared without specifying the size. D is
7473 : : the iterator within the constructor. */
7474 : :
7475 : : static tree
7476 : 378769 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7477 : : tree first_initializer_p, bool vector_p,
7478 : : tsubst_flags_t complain)
7479 : : {
7480 : 378769 : tree new_init;
7481 : 378769 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7482 : 378769 : unsigned HOST_WIDE_INT max_index_cst = 0;
7483 : 378769 : unsigned HOST_WIDE_INT index;
7484 : :
7485 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
7486 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7487 : : to build a new one. But don't reuse if not complaining; if this is
7488 : : tentative, we might also reshape to another type (95319). */
7489 : 378769 : bool reuse = (first_initializer_p
7490 : 324667 : && (complain & tf_error)
7491 : 323412 : && !CP_AGGREGATE_TYPE_P (elt_type)
7492 : 649451 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7493 : 115998 : if (reuse)
7494 : : new_init = first_initializer_p;
7495 : : else
7496 : 115998 : new_init = build_constructor (init_list_type_node, NULL);
7497 : :
7498 : 378769 : if (sized_array_p)
7499 : : {
7500 : 263323 : poly_uint64 midx;
7501 : : /* Minus 1 is used for zero sized arrays. */
7502 : 263323 : if (integer_all_onesp (max_index))
7503 : 24 : return new_init;
7504 : :
7505 : 263299 : if (tree_fits_poly_uint64_p (max_index))
7506 : 263299 : midx = tree_to_poly_uint64 (max_index);
7507 : : /* sizetype is sign extended, not zero extended. */
7508 : : else
7509 : 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7510 : :
7511 : : /* For VLA vectors, we restict the number of elements in the constructor
7512 : : to lower bound of the VLA elements. */
7513 : 263299 : max_index_cst = constant_lower_bound (midx);
7514 : : }
7515 : :
7516 : 378745 : constructor_elt *first_cur = d->cur;
7517 : : /* Loop until there are no more initializers. */
7518 : 20378711 : for (index = 0;
7519 : 20378711 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7520 : : ++index)
7521 : : {
7522 : 20000148 : tree elt_init;
7523 : 20000148 : constructor_elt *old_cur = d->cur;
7524 : 20000148 : unsigned int old_raw_idx = d->raw_idx;
7525 : 20000148 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7526 : :
7527 : 20000148 : if (d->cur->index)
7528 : 2106832 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7529 : 20000148 : check_array_designated_initializer (d->cur, index);
7530 : 20000148 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7531 : 32707 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7532 : 10233 : || is_byte_access_type (elt_type))
7533 : 22474 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7534 : 1813 : && (!sized_array_p || index < max_index_cst)
7535 : 20001949 : && !vector_p)
7536 : : {
7537 : 481 : elt_init = d->cur->value;
7538 : 481 : unsigned int off = d->raw_idx;
7539 : 481 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7540 : 481 : if (!sized_array_p || len <= max_index_cst - index + 1)
7541 : : {
7542 : 472 : d->cur++;
7543 : 472 : d->raw_idx = 0;
7544 : : }
7545 : : else
7546 : : {
7547 : 9 : len = max_index_cst - index + 1;
7548 : 9 : d->raw_idx += len;
7549 : : }
7550 : 481 : if (!reuse || off || d->cur == old_cur)
7551 : : {
7552 : 27 : elt_init = copy_node (elt_init);
7553 : 27 : RAW_DATA_LENGTH (elt_init) = len;
7554 : 27 : RAW_DATA_POINTER (elt_init) += off;
7555 : : }
7556 : 481 : TREE_TYPE (elt_init) = elt_type;
7557 : : }
7558 : : else
7559 : 19999667 : elt_init = reshape_init_r (elt_type, d,
7560 : : /*first_initializer_p=*/NULL_TREE,
7561 : : complain);
7562 : 20000148 : if (elt_init == error_mark_node)
7563 : : return error_mark_node;
7564 : 19999969 : tree idx = size_int (index);
7565 : 19999969 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7566 : : {
7567 : : /* We need to stop reusing as some RAW_DATA_CST in the original
7568 : : ctor had to be split. */
7569 : 72 : new_init = build_constructor (init_list_type_node, NULL);
7570 : 72 : if (index)
7571 : : {
7572 : 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7573 : 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7574 : 72 : (d->cur - first_cur)
7575 : : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7576 : 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7577 : : {
7578 : : unsigned int j;
7579 : : tree nidx, nval;
7580 : 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7581 : : j, nidx, nval)
7582 : 4 : if (nidx)
7583 : : {
7584 : 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7585 : 4 : (void) nval;
7586 : 4 : break;
7587 : : }
7588 : : }
7589 : : }
7590 : : reuse = false;
7591 : : }
7592 : 19999897 : if (reuse)
7593 : : {
7594 : 18091652 : old_cur->index = idx;
7595 : 18091652 : old_cur->value = elt_init;
7596 : : }
7597 : : else
7598 : 1908317 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7599 : : idx, elt_init);
7600 : 19999969 : if (!TREE_CONSTANT (elt_init))
7601 : 192764 : TREE_CONSTANT (new_init) = false;
7602 : :
7603 : : /* This can happen with an invalid initializer (c++/54501). */
7604 : 19999969 : if (d->cur == old_cur
7605 : 32082 : && !sized_array_p
7606 : 14085 : && d->raw_idx == old_raw_idx)
7607 : : break;
7608 : :
7609 : 19999966 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7610 : 481 : index += RAW_DATA_LENGTH (elt_init) - 1;
7611 : : }
7612 : :
7613 : : return new_init;
7614 : : }
7615 : :
7616 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7617 : : Parameters are the same of reshape_init_r. */
7618 : :
7619 : : static tree
7620 : 332624 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7621 : : tsubst_flags_t complain)
7622 : : {
7623 : 332624 : tree max_index = NULL_TREE;
7624 : :
7625 : 332624 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7626 : :
7627 : 332624 : if (TYPE_DOMAIN (type))
7628 : 217226 : max_index = array_type_nelts_minus_one (type);
7629 : :
7630 : 332624 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7631 : 332624 : first_initializer_p, false, complain);
7632 : : }
7633 : :
7634 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7635 : : Parameters are the same of reshape_init_r. */
7636 : :
7637 : : static tree
7638 : 46145 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7639 : : {
7640 : 46145 : tree max_index = NULL_TREE;
7641 : :
7642 : 46145 : gcc_assert (VECTOR_TYPE_P (type));
7643 : :
7644 : 46145 : if (COMPOUND_LITERAL_P (d->cur->value))
7645 : : {
7646 : 0 : tree value = d->cur->value;
7647 : 0 : if (!same_type_p (TREE_TYPE (value), type))
7648 : : {
7649 : 0 : if (complain & tf_error)
7650 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7651 : 0 : TREE_TYPE (d->cur->value), type);
7652 : 0 : value = error_mark_node;
7653 : : }
7654 : 0 : ++d->cur;
7655 : 0 : return value;
7656 : : }
7657 : :
7658 : : /* For a vector, we initialize it as an array of the appropriate size. */
7659 : 46145 : if (VECTOR_TYPE_P (type))
7660 : 46145 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7661 : :
7662 : 46145 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7663 : 46145 : NULL_TREE, true, complain);
7664 : : }
7665 : :
7666 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7667 : : INIT, in isolation from any designator or other initializers. */
7668 : :
7669 : : static tree
7670 : 632804 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7671 : : {
7672 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7673 : : calling reshape_init, but this way can just live on the stack. */
7674 : 632804 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7675 : 632804 : reshape_iter iter = { &elt, &elt + 1, 0 };
7676 : 632804 : return reshape_init_r (type, &iter,
7677 : : /*first_initializer_p=*/NULL_TREE,
7678 : 632804 : complain);
7679 : : }
7680 : :
7681 : : /* Subroutine of reshape_init_r, processes the initializers for classes
7682 : : or union. Parameters are the same of reshape_init_r. */
7683 : :
7684 : : static tree
7685 : 3822113 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7686 : : tsubst_flags_t complain)
7687 : : {
7688 : 3822113 : tree field;
7689 : 3822113 : tree new_init;
7690 : :
7691 : 3822113 : gcc_assert (CLASS_TYPE_P (type));
7692 : :
7693 : : /* The initializer for a class is always a CONSTRUCTOR. */
7694 : 3822113 : new_init = build_constructor (init_list_type_node, NULL);
7695 : :
7696 : 3822113 : int binfo_idx = -1;
7697 : 3822113 : tree binfo = TYPE_BINFO (type);
7698 : 3822113 : tree base_binfo = NULL_TREE;
7699 : 3822113 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7700 : : {
7701 : : /* We get here from maybe_aggr_guide for C++20 class template argument
7702 : : deduction. In this case we need to look through the binfo because a
7703 : : template doesn't have base fields. */
7704 : 982 : binfo_idx = 0;
7705 : 982 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7706 : : }
7707 : 67 : if (base_binfo)
7708 : : field = base_binfo;
7709 : : else
7710 : 3822046 : field = next_aggregate_field (TYPE_FIELDS (type));
7711 : :
7712 : 3822113 : if (!field)
7713 : : {
7714 : : /* [dcl.init.aggr]
7715 : :
7716 : : An initializer for an aggregate member that is an
7717 : : empty class shall have the form of an empty
7718 : : initializer-list {}. */
7719 : 324 : if (!first_initializer_p)
7720 : : {
7721 : 270 : if (complain & tf_error)
7722 : 13 : error ("initializer for %qT must be brace-enclosed", type);
7723 : 270 : return error_mark_node;
7724 : : }
7725 : : return new_init;
7726 : : }
7727 : :
7728 : : /* For C++20 CTAD, handle pack expansions in the base list. */
7729 : : tree last_was_pack_expansion = NULL_TREE;
7730 : :
7731 : : /* Loop through the initializable fields, gathering initializers. */
7732 : 11667503 : while (d->cur != d->end)
7733 : : {
7734 : 7958652 : tree field_init;
7735 : 7958652 : constructor_elt *old_cur = d->cur;
7736 : 7958652 : unsigned old_raw_idx = d->raw_idx;
7737 : 7958652 : bool direct_desig = false;
7738 : :
7739 : : /* Handle C++20 designated initializers. */
7740 : 7958652 : if (d->cur->index)
7741 : : {
7742 : 632966 : if (d->cur->index == error_mark_node)
7743 : : return error_mark_node;
7744 : :
7745 : 632960 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7746 : : {
7747 : : /* We already reshaped this; we should have returned early from
7748 : : reshape_init. */
7749 : 0 : gcc_checking_assert (false);
7750 : : if (field != d->cur->index)
7751 : : {
7752 : : if (tree id = DECL_NAME (d->cur->index))
7753 : : gcc_checking_assert (d->cur->index
7754 : : == get_class_binding (type, id));
7755 : : field = d->cur->index;
7756 : : }
7757 : : }
7758 : 632960 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7759 : : {
7760 : 632954 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7761 : 632954 : field = get_class_binding (type, d->cur->index);
7762 : 632954 : direct_desig = true;
7763 : : }
7764 : : else
7765 : : {
7766 : 6 : if (complain & tf_error)
7767 : 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7768 : : " for class %qT", d->cur->index, type);
7769 : 6 : return error_mark_node;
7770 : : }
7771 : :
7772 : 632954 : if (!field && ANON_AGGR_TYPE_P (type))
7773 : : /* Apparently the designator isn't for a member of this anonymous
7774 : : struct, so head back to the enclosing class. */
7775 : : break;
7776 : :
7777 : 632951 : if (!field || TREE_CODE (field) != FIELD_DECL)
7778 : : {
7779 : 18 : if (complain & tf_error)
7780 : : {
7781 : 6 : if (field && TREE_CODE (field) == TREE_LIST)
7782 : : {
7783 : 3 : auto_diagnostic_group g;
7784 : 3 : error ("request for member %qD is ambiguous",
7785 : 3 : d->cur->index);
7786 : 3 : print_candidates (field);
7787 : 3 : }
7788 : : else
7789 : 3 : error ("%qT has no non-static data member named %qD", type,
7790 : 3 : d->cur->index);
7791 : : }
7792 : 18 : return error_mark_node;
7793 : : }
7794 : :
7795 : : /* If the element is an anonymous union object and the initializer
7796 : : list is a designated-initializer-list, the anonymous union object
7797 : : is initialized by the designated-initializer-list { D }, where D
7798 : : is the designated-initializer-clause naming a member of the
7799 : : anonymous union object. */
7800 : 632933 : tree ictx = DECL_CONTEXT (field);
7801 : 632933 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7802 : : {
7803 : : /* Find the anon aggr that is a direct member of TYPE. */
7804 : 141 : while (ANON_AGGR_TYPE_P (ictx))
7805 : : {
7806 : 138 : tree cctx = TYPE_CONTEXT (ictx);
7807 : 138 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7808 : 126 : goto found;
7809 : : ictx = cctx;
7810 : : }
7811 : :
7812 : : /* Not found, e.g. FIELD is a member of a base class. */
7813 : 3 : if (complain & tf_error)
7814 : 3 : error ("%qD is not a direct member of %qT", field, type);
7815 : 3 : return error_mark_node;
7816 : :
7817 : 126 : found:
7818 : : /* Now find the TYPE member with that anon aggr type. */
7819 : 126 : tree aafield = TYPE_FIELDS (type);
7820 : 1081 : for (; aafield; aafield = TREE_CHAIN (aafield))
7821 : 1081 : if (TREE_TYPE (aafield) == ictx)
7822 : : break;
7823 : 0 : gcc_assert (aafield);
7824 : : field = aafield;
7825 : : direct_desig = false;
7826 : : }
7827 : : }
7828 : :
7829 : : /* If we processed all the member of the class, we are done. */
7830 : 7958490 : if (!field)
7831 : : break;
7832 : :
7833 : 15916199 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7834 : 7958109 : ? field : NULL_TREE);
7835 : 7958109 : if (last_was_pack_expansion)
7836 : : /* Each non-trailing aggregate element that is a pack expansion is
7837 : : assumed to correspond to no elements of the initializer list. */
7838 : 19 : goto continue_;
7839 : :
7840 : 7958090 : if (direct_desig)
7841 : : {
7842 : : /* The designated field F is initialized from this one element.
7843 : :
7844 : : Note that we don't want to do this if we found the designator
7845 : : inside an anon aggr above; we use the normal code to implement:
7846 : :
7847 : : "If the element is an anonymous union member and the initializer
7848 : : list is a brace-enclosed designated- initializer-list, the element
7849 : : is initialized by the designated-initializer-list { D }, where D
7850 : : is the designated- initializer-clause naming a member of the
7851 : : anonymous union member." */
7852 : 632804 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7853 : 632804 : field_init = reshape_single_init (TREE_TYPE (field),
7854 : : d->cur->value, complain);
7855 : 632804 : d->cur++;
7856 : : }
7857 : : else
7858 : 7325286 : field_init = reshape_init_r (TREE_TYPE (field), d,
7859 : : /*first_initializer_p=*/NULL_TREE,
7860 : : complain);
7861 : :
7862 : 7958090 : if (field_init == error_mark_node)
7863 : : return error_mark_node;
7864 : :
7865 : 7957801 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7866 : : {
7867 : : /* This can happen with an invalid initializer for a flexible
7868 : : array member (c++/54441). */
7869 : 0 : if (complain & tf_error)
7870 : 0 : error ("invalid initializer for %q#D", field);
7871 : 0 : return error_mark_node;
7872 : : }
7873 : :
7874 : 7957801 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7875 : :
7876 : : /* [dcl.init.aggr]
7877 : :
7878 : : When a union is initialized with a brace-enclosed
7879 : : initializer, the braces shall only contain an
7880 : : initializer for the first member of the union. */
7881 : 7957801 : if (TREE_CODE (type) == UNION_TYPE)
7882 : : break;
7883 : :
7884 : 7845695 : continue_:
7885 : 7845714 : if (base_binfo)
7886 : : {
7887 : 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7888 : : field = base_binfo;
7889 : : else
7890 : 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7891 : : }
7892 : : else
7893 : 7845632 : field = next_aggregate_field (DECL_CHAIN (field));
7894 : : }
7895 : :
7896 : : /* A trailing aggregate element that is a pack expansion is assumed to
7897 : : correspond to all remaining elements of the initializer list (if any). */
7898 : 3821467 : if (last_was_pack_expansion)
7899 : : {
7900 : 16 : tree init = d->cur->value;
7901 : 16 : bool inc_cur;
7902 : 16 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7903 : 0 : init = raw_init;
7904 : 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7905 : : last_was_pack_expansion, init);
7906 : 46 : while (d->cur != d->end)
7907 : 30 : d->cur++;
7908 : : }
7909 : :
7910 : : return new_init;
7911 : : }
7912 : :
7913 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7914 : : designators are not valid; either complain or return true to indicate
7915 : : that reshape_init_r should return error_mark_node. */
7916 : :
7917 : : static bool
7918 : 72814 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7919 : : {
7920 : 72814 : if (d->cur->index)
7921 : : {
7922 : 30 : if (complain & tf_error)
7923 : 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7924 : : "C99 designator %qE outside aggregate initializer",
7925 : : d->cur->index);
7926 : : else
7927 : : return true;
7928 : : }
7929 : : return false;
7930 : : }
7931 : :
7932 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7933 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7934 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7935 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7936 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7937 : :
7938 : : static tree
7939 : 32222546 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7940 : : tsubst_flags_t complain)
7941 : : {
7942 : 32222546 : tree init = d->cur->value;
7943 : :
7944 : 32222546 : if (error_operand_p (init))
7945 : 124 : return error_mark_node;
7946 : :
7947 : 4264668 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7948 : 32295057 : && has_designator_problem (d, complain))
7949 : 0 : return error_mark_node;
7950 : :
7951 : 32222422 : tree stripped_init = tree_strip_any_location_wrapper (init);
7952 : :
7953 : 32222422 : if (TREE_CODE (type) == COMPLEX_TYPE)
7954 : : {
7955 : : /* A complex type can be initialized from one or two initializers,
7956 : : but braces are not elided. */
7957 : 678 : init = consume_init (init, d);
7958 : 678 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7959 : : {
7960 : 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7961 : : {
7962 : 0 : if (complain & tf_error)
7963 : 0 : error ("too many initializers for %qT", type);
7964 : : else
7965 : 0 : return error_mark_node;
7966 : : }
7967 : : }
7968 : 669 : else if (first_initializer_p && d->cur != d->end)
7969 : : {
7970 : 36 : if (error_operand_p (d->cur->value)
7971 : 36 : || has_designator_problem (d, complain))
7972 : 0 : return error_mark_node;
7973 : 36 : vec<constructor_elt, va_gc> *v = 0;
7974 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7975 : 36 : init = consume_init (d->cur->value, d);
7976 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7977 : 36 : init = build_constructor (init_list_type_node, v);
7978 : : }
7979 : 678 : return init;
7980 : : }
7981 : :
7982 : : /* A non-aggregate type is always initialized with a single
7983 : : initializer. */
7984 : 64388542 : if (!CP_AGGREGATE_TYPE_P (type)
7985 : : /* As is an array with dependent bound, which we can see
7986 : : during C++20 aggregate CTAD. */
7987 : 38569057 : || (cxx_dialect >= cxx20
7988 : 4346666 : && TREE_CODE (type) == ARRAY_TYPE
7989 : 191614 : && uses_template_parms (TYPE_DOMAIN (type))))
7990 : : {
7991 : : /* It is invalid to initialize a non-aggregate type with a
7992 : : brace-enclosed initializer before C++0x.
7993 : : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7994 : : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7995 : : a CONSTRUCTOR (with a record type). */
7996 : 25819496 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
7997 : : /* Don't complain about a capture-init. */
7998 : 310822 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7999 : 26119994 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
8000 : : {
8001 : 300459 : if (SCALAR_TYPE_P (type))
8002 : : {
8003 : 725 : if (cxx_dialect < cxx11)
8004 : : {
8005 : 16 : if (complain & tf_error)
8006 : 16 : error ("braces around scalar initializer for type %qT",
8007 : : type);
8008 : 16 : init = error_mark_node;
8009 : : }
8010 : 709 : else if (first_initializer_p
8011 : 709 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
8012 : 545 : && (BRACE_ENCLOSED_INITIALIZER_P
8013 : : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8014 : : {
8015 : 13 : if (complain & tf_error)
8016 : 13 : error ("too many braces around scalar initializer "
8017 : : "for type %qT", type);
8018 : 13 : init = error_mark_node;
8019 : : }
8020 : : }
8021 : : else
8022 : 299734 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8023 : : }
8024 : 25819496 : return consume_init (init, d);
8025 : : }
8026 : :
8027 : : /* "If T is a class type and the initializer list has a single element of
8028 : : type cv U, where U is T or a class derived from T, the object is
8029 : : initialized from that element." Even if T is an aggregate. */
8030 : 6378374 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8031 : 5934726 : && first_initializer_p
8032 : : /* But not if it's a designated init. */
8033 : 3852851 : && !d->cur->index
8034 : 3586963 : && d->end - d->cur == 1
8035 : 444640 : && TREE_CODE (init) != RAW_DATA_CST
8036 : 6846888 : && reference_related_p (type, TREE_TYPE (init)))
8037 : : {
8038 : 417 : d->cur++;
8039 : 417 : return init;
8040 : : }
8041 : :
8042 : : /* [dcl.init.aggr]
8043 : :
8044 : : All implicit type conversions (clause _conv_) are considered when
8045 : : initializing the aggregate member with an initializer from an
8046 : : initializer-list. If the initializer can initialize a member,
8047 : : the member is initialized. Otherwise, if the member is itself a
8048 : : non-empty subaggregate, brace elision is assumed and the
8049 : : initializer is considered for the initialization of the first
8050 : : member of the subaggregate. */
8051 : 2804613 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8052 : : /* But don't try this for the first initializer, since that would be
8053 : : looking through the outermost braces; A a2 = { a1 }; is not a
8054 : : valid aggregate initialization. */
8055 : 3597278 : && !first_initializer_p
8056 : 6416130 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8057 : 10197 : || can_convert_arg (type, TREE_TYPE (init),
8058 : 10197 : TREE_CODE (init) == RAW_DATA_CST
8059 : 6403437 : ? build_int_cst (integer_type_node,
8060 : 1606 : *(const unsigned char *)
8061 : 1606 : RAW_DATA_POINTER (init))
8062 : : : init,
8063 : : LOOKUP_NORMAL, complain)))
8064 : 4138 : return consume_init (init, d);
8065 : :
8066 : : /* [dcl.init.string]
8067 : :
8068 : : A char array (whether plain char, signed char, or unsigned char)
8069 : : can be initialized by a string-literal (optionally enclosed in
8070 : : braces); a wchar_t array can be initialized by a wide
8071 : : string-literal (optionally enclosed in braces). */
8072 : 6397693 : if (TREE_CODE (type) == ARRAY_TYPE
8073 : 6397693 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8074 : : {
8075 : 92505 : tree str_init = init;
8076 : 92505 : tree stripped_str_init = stripped_init;
8077 : 92505 : reshape_iter stripd = {};
8078 : :
8079 : : /* Strip one level of braces if and only if they enclose a single
8080 : : element (as allowed by [dcl.init.string]). */
8081 : 92505 : if (!first_initializer_p
8082 : 12613 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8083 : 103983 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8084 : : {
8085 : 398 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8086 : 398 : str_init = stripd.cur->value;
8087 : 398 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
8088 : : }
8089 : :
8090 : : /* If it's a string literal, then it's the initializer for the array
8091 : : as a whole. Otherwise, continue with normal initialization for
8092 : : array types (one value per array element). */
8093 : 92505 : if (TREE_CODE (stripped_str_init) == STRING_CST)
8094 : : {
8095 : 72 : if ((first_initializer_p && has_designator_problem (d, complain))
8096 : 1062 : || (stripd.cur && has_designator_problem (&stripd, complain)))
8097 : 990 : return error_mark_node;
8098 : 990 : d->cur++;
8099 : 990 : return str_init;
8100 : : }
8101 : : }
8102 : :
8103 : : /* The following cases are about aggregates. If we are not within a full
8104 : : initializer already, and there is not a CONSTRUCTOR, it means that there
8105 : : is a missing set of braces (that is, we are processing the case for
8106 : : which reshape_init exists). */
8107 : 6396703 : bool braces_elided_p = false;
8108 : 6396703 : if (!first_initializer_p)
8109 : : {
8110 : 2205159 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
8111 : : {
8112 : 2195851 : tree init_type = TREE_TYPE (init);
8113 : 2195851 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8114 : : /* There is no need to call reshape_init for pointer-to-member
8115 : : function initializers, as they are always constructed correctly
8116 : : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8117 : : which is missing outermost braces. We should warn below, and
8118 : : one of the routines below will wrap it in additional { }. */;
8119 : : /* For a nested compound literal, proceed to specialized routines,
8120 : : to handle initialization of arrays and similar. */
8121 : 2195842 : else if (COMPOUND_LITERAL_P (stripped_init))
8122 : 6 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8123 : : /* If we have an unresolved designator, we need to find the member it
8124 : : designates within TYPE, so proceed to the routines below. For
8125 : : FIELD_DECL or INTEGER_CST designators, we're already initializing
8126 : : the designated element. */
8127 : 2195836 : else if (d->cur->index
8128 : 30 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
8129 : : /* Brace elision with designators is only permitted for anonymous
8130 : : aggregates. */
8131 : 15 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
8132 : : /* A CONSTRUCTOR of the target's type is a previously
8133 : : digested initializer. */
8134 : 2195821 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8135 : : {
8136 : 0 : ++d->cur;
8137 : 0 : return init;
8138 : : }
8139 : : else
8140 : : {
8141 : : /* Something that hasn't been reshaped yet. */
8142 : 2195821 : ++d->cur;
8143 : 2195821 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8144 : 2195821 : return reshape_init (type, init, complain);
8145 : : }
8146 : : }
8147 : :
8148 : 9338 : if (complain & tf_warning)
8149 : 8189 : warning (OPT_Wmissing_braces,
8150 : : "missing braces around initializer for %qT",
8151 : : type);
8152 : : braces_elided_p = true;
8153 : : }
8154 : :
8155 : : /* Dispatch to specialized routines. */
8156 : 4200882 : tree new_init;
8157 : 4200882 : if (CLASS_TYPE_P (type))
8158 : 3822113 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
8159 : 378769 : else if (TREE_CODE (type) == ARRAY_TYPE)
8160 : 332624 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
8161 : 46145 : else if (VECTOR_TYPE_P (type))
8162 : 46145 : new_init = reshape_init_vector (type, d, complain);
8163 : : else
8164 : 0 : gcc_unreachable ();
8165 : :
8166 : 4200882 : if (braces_elided_p
8167 : 9338 : && TREE_CODE (new_init) == CONSTRUCTOR)
8168 : 9062 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8169 : :
8170 : : return new_init;
8171 : : }
8172 : :
8173 : : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
8174 : : brace-enclosed aggregate initializer.
8175 : :
8176 : : INIT is the CONSTRUCTOR containing the list of initializers describing
8177 : : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8178 : : It may not presently match the shape of the TYPE; for example:
8179 : :
8180 : : struct S { int a; int b; };
8181 : : struct S a[] = { 1, 2, 3, 4 };
8182 : :
8183 : : Here INIT will hold a vector of four elements, rather than a
8184 : : vector of two elements, each itself a vector of two elements. This
8185 : : routine transforms INIT from the former form into the latter. The
8186 : : revised CONSTRUCTOR node is returned. */
8187 : :
8188 : : tree
8189 : 10312131 : reshape_init (tree type, tree init, tsubst_flags_t complain)
8190 : : {
8191 : 10312131 : vec<constructor_elt, va_gc> *v;
8192 : 10312131 : reshape_iter d;
8193 : 10312131 : tree new_init;
8194 : :
8195 : 10312131 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8196 : :
8197 : 10312131 : v = CONSTRUCTOR_ELTS (init);
8198 : :
8199 : : /* An empty constructor does not need reshaping, and it is always a valid
8200 : : initializer. */
8201 : 10312847 : if (vec_safe_is_empty (v))
8202 : : return init;
8203 : :
8204 : 4265715 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8205 : : /* Already reshaped. */
8206 : : return init;
8207 : :
8208 : : /* Brace elision is not performed for a CONSTRUCTOR representing
8209 : : parenthesized aggregate initialization. */
8210 : 4265578 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
8211 : : {
8212 : 613 : tree elt = (*v)[0].value;
8213 : : /* If we're initializing a char array from a string-literal that is
8214 : : enclosed in braces, unwrap it here. */
8215 : 613 : if (TREE_CODE (type) == ARRAY_TYPE
8216 : 260 : && vec_safe_length (v) == 1
8217 : 144 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8218 : 652 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8219 : : return elt;
8220 : 579 : return init;
8221 : : }
8222 : :
8223 : : /* Handle [dcl.init.list] direct-list-initialization from
8224 : : single element of enumeration with a fixed underlying type. */
8225 : 4264965 : if (is_direct_enum_init (type, init))
8226 : : {
8227 : 176 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8228 : 176 : type = cv_unqualified (type);
8229 : 176 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8230 : : {
8231 : 161 : warning_sentinel w (warn_useless_cast);
8232 : 161 : warning_sentinel w2 (warn_ignored_qualifiers);
8233 : 161 : return cp_build_c_cast (input_location, type, elt,
8234 : : tf_warning_or_error);
8235 : 161 : }
8236 : : else
8237 : 15 : return error_mark_node;
8238 : : }
8239 : :
8240 : : /* Recurse on this CONSTRUCTOR. */
8241 : 4264789 : d.cur = &(*v)[0];
8242 : 4264789 : d.end = d.cur + v->length ();
8243 : 4264789 : d.raw_idx = 0;
8244 : :
8245 : 4264789 : new_init = reshape_init_r (type, &d, init, complain);
8246 : 4264789 : if (new_init == error_mark_node)
8247 : : return error_mark_node;
8248 : :
8249 : : /* Make sure all the element of the constructor were used. Otherwise,
8250 : : issue an error about exceeding initializers. */
8251 : 4264167 : if (d.cur != d.end)
8252 : : {
8253 : 251 : if (complain & tf_error)
8254 : 63 : error ("too many initializers for %qT", type);
8255 : 251 : return error_mark_node;
8256 : : }
8257 : :
8258 : 4263916 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8259 : 4263916 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8260 : 493329 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8261 : 4263916 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8262 : 4263916 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8263 : 32576 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8264 : : || seen_error ());
8265 : :
8266 : : return new_init;
8267 : : }
8268 : :
8269 : : /* Verify array initializer. Returns true if errors have been reported. */
8270 : :
8271 : : bool
8272 : 925373 : check_array_initializer (tree decl, tree type, tree init)
8273 : : {
8274 : 925373 : tree element_type = TREE_TYPE (type);
8275 : :
8276 : : /* Structured binding when initialized with an array type needs
8277 : : to have complete type. */
8278 : 925373 : if (decl
8279 : 919538 : && DECL_DECOMPOSITION_P (decl)
8280 : 366 : && DECL_DECOMP_IS_BASE (decl)
8281 : 925739 : && !COMPLETE_TYPE_P (type))
8282 : : {
8283 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
8284 : : "structured binding has incomplete type %qT", type);
8285 : 0 : TREE_TYPE (decl) = error_mark_node;
8286 : 0 : return true;
8287 : : }
8288 : :
8289 : : /* The array type itself need not be complete, because the
8290 : : initializer may tell us how many elements are in the array.
8291 : : But, the elements of the array must be complete. */
8292 : 925373 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
8293 : : {
8294 : 15 : if (decl)
8295 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
8296 : : "elements of array %q#D have incomplete type", decl);
8297 : : else
8298 : 6 : error ("elements of array %q#T have incomplete type", type);
8299 : 15 : return true;
8300 : : }
8301 : :
8302 : 925358 : location_t loc = (decl ? location_of (decl) : input_location);
8303 : 925358 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8304 : : return true;
8305 : :
8306 : : /* A compound literal can't have variable size. */
8307 : 925358 : if (init && !decl
8308 : 925358 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8309 : 5826 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8310 : : {
8311 : 3 : error ("variable-sized compound literal");
8312 : 3 : return true;
8313 : : }
8314 : : return false;
8315 : : }
8316 : :
8317 : : /* Subroutine of check_initializer; args are passed down from that function.
8318 : : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8319 : :
8320 : : static tree
8321 : 2553462 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
8322 : : {
8323 : 2553462 : gcc_assert (stmts_are_full_exprs_p ());
8324 : 2553462 : if (init)
8325 : 1885244 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8326 : 2553462 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
8327 : : }
8328 : :
8329 : : /* Verify INIT (the initializer for DECL), and record the
8330 : : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8331 : : grok_reference_init.
8332 : :
8333 : : If the return value is non-NULL, it is an expression that must be
8334 : : evaluated dynamically to initialize DECL. */
8335 : :
8336 : : static tree
8337 : 46334801 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8338 : : {
8339 : 46334801 : tree type;
8340 : 46334801 : tree init_code = NULL;
8341 : 46334801 : tree core_type;
8342 : :
8343 : : /* Things that are going to be initialized need to have complete
8344 : : type. */
8345 : 46334801 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8346 : :
8347 : 46334801 : if (DECL_HAS_VALUE_EXPR_P (decl))
8348 : : {
8349 : : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8350 : : it doesn't have storage to be initialized. */
8351 : 138 : gcc_assert (init == NULL_TREE);
8352 : : return NULL_TREE;
8353 : : }
8354 : :
8355 : 46334663 : if (type == error_mark_node)
8356 : : /* We will have already complained. */
8357 : : return NULL_TREE;
8358 : :
8359 : 46334663 : if (TREE_CODE (type) == ARRAY_TYPE)
8360 : : {
8361 : 919538 : if (check_array_initializer (decl, type, init))
8362 : : return NULL_TREE;
8363 : : }
8364 : 45415125 : else if (!COMPLETE_TYPE_P (type))
8365 : : {
8366 : 37 : error_at (DECL_SOURCE_LOCATION (decl),
8367 : : "%q#D has incomplete type", decl);
8368 : 37 : TREE_TYPE (decl) = error_mark_node;
8369 : 37 : return NULL_TREE;
8370 : : }
8371 : : else
8372 : : /* There is no way to make a variable-sized class type in GNU C++. */
8373 : 45415088 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8374 : :
8375 : 46334617 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8376 : : {
8377 : 1681686 : int init_len = CONSTRUCTOR_NELTS (init);
8378 : 1681686 : if (SCALAR_TYPE_P (type))
8379 : : {
8380 : 59944 : if (init_len == 0)
8381 : : {
8382 : 48552 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8383 : 48552 : init = build_zero_init (type, NULL_TREE, false);
8384 : : }
8385 : 11392 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8386 : : {
8387 : 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8388 : : "scalar object %qD requires one element in "
8389 : : "initializer", decl);
8390 : 6 : TREE_TYPE (decl) = error_mark_node;
8391 : 6 : return NULL_TREE;
8392 : : }
8393 : : }
8394 : : }
8395 : :
8396 : 46334611 : if (TREE_CODE (decl) == CONST_DECL)
8397 : : {
8398 : 0 : gcc_assert (!TYPE_REF_P (type));
8399 : :
8400 : 0 : DECL_INITIAL (decl) = init;
8401 : :
8402 : 0 : gcc_assert (init != NULL_TREE);
8403 : : init = NULL_TREE;
8404 : : }
8405 : 46334611 : else if (!init && DECL_REALLY_EXTERN (decl))
8406 : : ;
8407 : 46334611 : else if (flag_openmp
8408 : 184519 : && VAR_P (decl)
8409 : 184519 : && DECL_LANG_SPECIFIC (decl)
8410 : 106883 : && DECL_OMP_DECLARE_MAPPER_P (decl)
8411 : 46334611 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8412 : : return NULL_TREE;
8413 : 4405365 : else if (init || type_build_ctor_call (type)
8414 : 50071755 : || TYPE_REF_P (type))
8415 : : {
8416 : 42597482 : if (TYPE_REF_P (type))
8417 : : {
8418 : 333378 : init = grok_reference_init (decl, type, init, flags);
8419 : 333378 : flags |= LOOKUP_ALREADY_DIGESTED;
8420 : : }
8421 : 42264104 : else if (!init)
8422 : 668221 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8423 : : tf_warning_or_error);
8424 : : /* Do not reshape constructors of vectors (they don't need to be
8425 : : reshaped. */
8426 : 41595883 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8427 : : {
8428 : 1632804 : if (is_std_init_list (type))
8429 : : {
8430 : 331 : init = perform_implicit_conversion (type, init,
8431 : : tf_warning_or_error);
8432 : 331 : flags |= LOOKUP_ALREADY_DIGESTED;
8433 : : }
8434 : 1632473 : else if (TYPE_NON_AGGREGATE_CLASS (type))
8435 : : {
8436 : : /* Don't reshape if the class has constructors. */
8437 : 228937 : if (cxx_dialect == cxx98)
8438 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8439 : : "in C++98 %qD must be initialized by "
8440 : : "constructor, not by %<{...}%>",
8441 : : decl);
8442 : : }
8443 : 1403536 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8444 : : {
8445 : 0 : error ("opaque vector types cannot be initialized");
8446 : 0 : init = error_mark_node;
8447 : : }
8448 : : else
8449 : : {
8450 : 1403536 : init = reshape_init (type, init, tf_warning_or_error);
8451 : 1403536 : flags |= LOOKUP_NO_NARROWING;
8452 : : }
8453 : : }
8454 : : /* [dcl.init] "Otherwise, if the destination type is an array, the object
8455 : : is initialized as follows..." So handle things like
8456 : :
8457 : : int a[](1, 2, 3);
8458 : :
8459 : : which is permitted in C++20 by P0960. */
8460 : 39963079 : else if (TREE_CODE (init) == TREE_LIST
8461 : 665800 : && TREE_TYPE (init) == NULL_TREE
8462 : 665800 : && TREE_CODE (type) == ARRAY_TYPE
8463 : 165 : && !DECL_DECOMPOSITION_P (decl)
8464 : 39963223 : && (cxx_dialect >= cxx20))
8465 : 134 : init = do_aggregate_paren_init (init, type);
8466 : 39962945 : else if (TREE_CODE (init) == TREE_LIST
8467 : 665666 : && TREE_TYPE (init) != unknown_type_node
8468 : 40628611 : && !MAYBE_CLASS_TYPE_P (type))
8469 : : {
8470 : 136262 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8471 : :
8472 : : /* We get here with code like `int a (2);' */
8473 : 136262 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8474 : : tf_warning_or_error);
8475 : : }
8476 : :
8477 : : /* If DECL has an array type without a specific bound, deduce the
8478 : : array size from the initializer. */
8479 : 42597482 : maybe_deduce_size_from_array_init (decl, init);
8480 : 42597482 : type = TREE_TYPE (decl);
8481 : 42597482 : if (type == error_mark_node)
8482 : : return NULL_TREE;
8483 : :
8484 : 82193150 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8485 : 3788942 : && !(flags & LOOKUP_ALREADY_DIGESTED)
8486 : 3788611 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
8487 : 1465845 : && CP_AGGREGATE_TYPE_P (type)
8488 : 1236908 : && (CLASS_TYPE_P (type)
8489 : : /* The call to build_aggr_init below could end up
8490 : : calling build_vec_init, which may break when we
8491 : : are processing a template. */
8492 : 1593 : || processing_template_decl
8493 : 1524 : || !TYPE_NEEDS_CONSTRUCTING (type)
8494 : 1492 : || type_has_extended_temps (type))))
8495 : 82641821 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8496 : : {
8497 : 2553462 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8498 : :
8499 : : /* A constructor call is a non-trivial initializer even if
8500 : : it isn't explicitly written. */
8501 : 2553462 : if (TREE_SIDE_EFFECTS (init_code))
8502 : 2544385 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8503 : :
8504 : : /* If this is a constexpr initializer, expand_default_init will
8505 : : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8506 : : case, pull the initializer back out and pass it down into
8507 : : store_init_value. */
8508 : 6658110 : while (true)
8509 : : {
8510 : 6658110 : if (TREE_CODE (init_code) == EXPR_STMT
8511 : 4116347 : || TREE_CODE (init_code) == STMT_EXPR
8512 : 4105689 : || TREE_CODE (init_code) == CONVERT_EXPR)
8513 : 4094036 : init_code = TREE_OPERAND (init_code, 0);
8514 : 2564074 : else if (TREE_CODE (init_code) == BIND_EXPR)
8515 : 10612 : init_code = BIND_EXPR_BODY (init_code);
8516 : : else
8517 : : break;
8518 : : }
8519 : 2553462 : if (TREE_CODE (init_code) == INIT_EXPR)
8520 : : {
8521 : : /* In C++20, the call to build_aggr_init could have created
8522 : : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8523 : : A(1, 2). */
8524 : 1548143 : tree rhs = TREE_OPERAND (init_code, 1);
8525 : 1548143 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8526 : : /* Avoid leaking TARGET_EXPR into template trees. */
8527 : 5342 : rhs = build_implicit_conv_flags (type, init, flags);
8528 : 1548143 : init = rhs;
8529 : :
8530 : 1548143 : init_code = NULL_TREE;
8531 : : /* Don't call digest_init; it's unnecessary and will complain
8532 : : about aggregate initialization of non-aggregate classes. */
8533 : 1548128 : flags |= LOOKUP_ALREADY_DIGESTED;
8534 : : }
8535 : 1005319 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8536 : 2004971 : || DECL_DECLARED_CONSTINIT_P (decl))
8537 : : {
8538 : : /* Declared constexpr or constinit, but no suitable initializer;
8539 : : massage init appropriately so we can pass it into
8540 : : store_init_value for the error. */
8541 : 5685 : tree new_init = NULL_TREE;
8542 : 5685 : if (!processing_template_decl
8543 : 384 : && TREE_CODE (init_code) == CALL_EXPR)
8544 : 328 : new_init = build_cplus_new (type, init_code, tf_none);
8545 : 5333 : else if (CLASS_TYPE_P (type)
8546 : 10690 : && (!init || TREE_CODE (init) == TREE_LIST))
8547 : 5273 : new_init = build_functional_cast (input_location, type,
8548 : : init, tf_none);
8549 : 5601 : if (new_init)
8550 : : {
8551 : 5601 : init = new_init;
8552 : 5601 : if (TREE_CODE (init) == TARGET_EXPR
8553 : 349 : && !(flags & LOOKUP_ONLYCONVERTING))
8554 : 349 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8555 : : }
8556 : : init_code = NULL_TREE;
8557 : : }
8558 : : else
8559 : : init = NULL_TREE;
8560 : : }
8561 : :
8562 : 41597844 : if (init && TREE_CODE (init) != TREE_VEC)
8563 : : {
8564 : 41597829 : init_code = store_init_value (decl, init, cleanups, flags);
8565 : :
8566 : 41595132 : if (DECL_INITIAL (decl)
8567 : 27826402 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8568 : 43466511 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8569 : : {
8570 : 1099512 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8571 : 1099512 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8572 : 1099512 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8573 : 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8574 : : }
8575 : :
8576 : 1101350 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8577 : 52318 : && DECL_INITIAL (decl)
8578 : 51883 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8579 : 41644112 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8580 : 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8581 : 3 : DECL_SOURCE_LOCATION (decl)),
8582 : 3 : 0, "array %qD initialized by parenthesized "
8583 : : "string literal %qE",
8584 : 3 : decl, DECL_INITIAL (decl));
8585 : : init = NULL_TREE;
8586 : : }
8587 : : }
8588 : : else
8589 : : {
8590 : 7474258 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8591 : 4406872 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8592 : 669730 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8593 : 19 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8594 : : /*complain=*/true);
8595 : :
8596 : 3737129 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8597 : : tf_warning_or_error);
8598 : : }
8599 : :
8600 : 0 : if (init && init != error_mark_node)
8601 : 0 : init_code = cp_build_init_expr (decl, init);
8602 : :
8603 : 14894576 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8604 : 46353919 : && init_code != error_mark_node)
8605 : : init_code = NULL_TREE;
8606 : :
8607 : 46312131 : if (init_code)
8608 : : {
8609 : : /* We might have set these in cp_finish_decl. */
8610 : 14874797 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8611 : 14874797 : TREE_CONSTANT (decl) = false;
8612 : : }
8613 : :
8614 : 14874797 : if (init_code
8615 : 14874797 : && DECL_IN_AGGR_P (decl)
8616 : 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8617 : : {
8618 : 0 : static int explained = 0;
8619 : :
8620 : 0 : auto_diagnostic_group d;
8621 : 0 : if (cxx_dialect < cxx11)
8622 : 0 : error ("initializer invalid for static member with constructor");
8623 : 0 : else if (cxx_dialect < cxx17)
8624 : 0 : error ("non-constant in-class initialization invalid for static "
8625 : : "member %qD", decl);
8626 : : else
8627 : 0 : error ("non-constant in-class initialization invalid for non-inline "
8628 : : "static member %qD", decl);
8629 : 0 : if (!explained)
8630 : : {
8631 : 0 : inform (input_location,
8632 : : "(an out of class initialization is required)");
8633 : 0 : explained = 1;
8634 : : }
8635 : 0 : return NULL_TREE;
8636 : 0 : }
8637 : :
8638 : : return init_code;
8639 : : }
8640 : :
8641 : : /* If DECL is not a local variable, give it RTL. */
8642 : :
8643 : : static void
8644 : 107430409 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8645 : : {
8646 : 107430409 : int toplev = toplevel_bindings_p ();
8647 : 107430409 : int defer_p;
8648 : :
8649 : : /* Set the DECL_ASSEMBLER_NAME for the object. */
8650 : 107430409 : if (asmspec)
8651 : : {
8652 : : /* The `register' keyword, when used together with an
8653 : : asm-specification, indicates that the variable should be
8654 : : placed in a particular register. */
8655 : 997961 : if (VAR_P (decl) && DECL_REGISTER (decl))
8656 : : {
8657 : 87 : if (TREE_ADDRESSABLE (decl))
8658 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
8659 : : "address of explicit register variable %qD requested",
8660 : : decl);
8661 : : else
8662 : : {
8663 : 84 : set_user_assembler_name (decl, asmspec);
8664 : 84 : DECL_HARD_REGISTER (decl) = 1;
8665 : : }
8666 : : }
8667 : : else
8668 : : {
8669 : 997874 : if (TREE_CODE (decl) == FUNCTION_DECL
8670 : 997874 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8671 : 79350 : set_builtin_user_assembler_name (decl, asmspec);
8672 : 997874 : set_user_assembler_name (decl, asmspec);
8673 : 997874 : if (DECL_LOCAL_DECL_P (decl))
8674 : 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8675 : : /* We have to propagate the name to the ns-alias.
8676 : : This is horrible, as we're affecting a
8677 : : possibly-shared decl. Again, a one-true-decl
8678 : : model breaks down. */
8679 : 6 : if (ns_decl != error_mark_node)
8680 : 6 : set_user_assembler_name (ns_decl, asmspec);
8681 : : }
8682 : : }
8683 : :
8684 : : /* Handle non-variables up front. */
8685 : 107430409 : if (!VAR_P (decl))
8686 : : {
8687 : 55211411 : rest_of_decl_compilation (decl, toplev, at_eof);
8688 : 55211411 : return;
8689 : : }
8690 : :
8691 : : /* If we see a class member here, it should be a static data
8692 : : member. */
8693 : 52218998 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8694 : : {
8695 : 4174911 : gcc_assert (TREE_STATIC (decl));
8696 : : /* An in-class declaration of a static data member should be
8697 : : external; it is only a declaration, and not a definition. */
8698 : 4174911 : if (init == NULL_TREE)
8699 : 4174901 : gcc_assert (DECL_EXTERNAL (decl)
8700 : : || !TREE_PUBLIC (decl));
8701 : : }
8702 : :
8703 : : /* We don't create any RTL for local variables. */
8704 : 52218998 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8705 : : return;
8706 : :
8707 : : /* We defer emission of local statics until the corresponding
8708 : : DECL_EXPR is expanded. But with constexpr its function might never
8709 : : be expanded, so go ahead and tell cgraph about the variable now. */
8710 : 56300618 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8711 : 144364 : && !var_in_maybe_constexpr_fn (decl))
8712 : 56157716 : || DECL_VIRTUAL_P (decl));
8713 : :
8714 : : /* Defer template instantiations. */
8715 : 28150309 : if (DECL_LANG_SPECIFIC (decl)
8716 : 28150309 : && DECL_IMPLICIT_INSTANTIATION (decl))
8717 : : defer_p = 1;
8718 : :
8719 : : /* If we're not deferring, go ahead and assemble the variable. */
8720 : 16900187 : if (!defer_p)
8721 : 14531305 : rest_of_decl_compilation (decl, toplev, at_eof);
8722 : : }
8723 : :
8724 : : /* walk_tree helper for wrap_temporary_cleanups, below. */
8725 : :
8726 : : static tree
8727 : 10886944 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8728 : : {
8729 : : /* Stop at types or full-expression boundaries. */
8730 : 10886944 : if (TYPE_P (*stmt_p)
8731 : 10886733 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8732 : : {
8733 : 439 : *walk_subtrees = 0;
8734 : 439 : return NULL_TREE;
8735 : : }
8736 : :
8737 : 10886505 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8738 : : {
8739 : 196206 : tree guard = (tree)data;
8740 : 196206 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8741 : :
8742 : 104627 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8743 : 293707 : && !expr_noexcept_p (tcleanup, tf_none))
8744 : : {
8745 : 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8746 : : /* Tell honor_protect_cleanup_actions to handle this as a separate
8747 : : cleanup. */
8748 : 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8749 : 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8750 : : }
8751 : : }
8752 : :
8753 : : return NULL_TREE;
8754 : : }
8755 : :
8756 : : /* We're initializing a local variable which has a cleanup GUARD. If there
8757 : : are any temporaries used in the initializer INIT of this variable, we
8758 : : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8759 : : variable will be cleaned up properly if one of them throws.
8760 : :
8761 : : Unfortunately, there's no way to express this properly in terms of
8762 : : nesting, as the regions for the temporaries overlap the region for the
8763 : : variable itself; if there are two temporaries, the variable needs to be
8764 : : the first thing destroyed if either of the temporary destructors throws.
8765 : : However, we only want to run the variable's cleanup if it actually got
8766 : : constructed. So we need to guard the temporary cleanups with the
8767 : : variable's cleanup if they are run on the normal path, but not if they
8768 : : are run on the exceptional path. We implement this by telling
8769 : : honor_protect_cleanup_actions to strip the variable cleanup from the
8770 : : exceptional path.
8771 : :
8772 : : Another approach could be to make the variable cleanup region enclose
8773 : : initialization, but depend on a flag to indicate that the variable is
8774 : : initialized; that's effectively what we do for arrays. But the current
8775 : : approach works fine for non-arrays, and has no code overhead in the usual
8776 : : case where the temporary destructors are noexcept. */
8777 : :
8778 : : static void
8779 : 1001857 : wrap_temporary_cleanups (tree init, tree guard)
8780 : : {
8781 : 1001857 : if (TREE_CODE (guard) == BIND_EXPR)
8782 : : {
8783 : : /* An array cleanup region already encloses any temporary cleanups,
8784 : : don't wrap it around them again. */
8785 : 422 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8786 : : return;
8787 : : }
8788 : 1001435 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8789 : : }
8790 : :
8791 : : /* Generate code to initialize DECL (a local variable). */
8792 : :
8793 : : static void
8794 : 24068671 : initialize_local_var (tree decl, tree init, bool decomp)
8795 : : {
8796 : 24068671 : tree type = TREE_TYPE (decl);
8797 : 24068671 : tree cleanup;
8798 : 24068671 : int already_used;
8799 : :
8800 : 24068671 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8801 : 24068671 : gcc_assert (!TREE_STATIC (decl));
8802 : :
8803 : 24068671 : if (DECL_SIZE (decl) == NULL_TREE)
8804 : : {
8805 : : /* If we used it already as memory, it must stay in memory. */
8806 : 0 : DECL_INITIAL (decl) = NULL_TREE;
8807 : 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8808 : 0 : return;
8809 : : }
8810 : :
8811 : 24068671 : if (type == error_mark_node)
8812 : : return;
8813 : :
8814 : : /* Compute and store the initial value. */
8815 : 24068671 : already_used = TREE_USED (decl) || TREE_USED (type);
8816 : 24068671 : if (TREE_USED (type))
8817 : 32 : DECL_READ_P (decl) = 1;
8818 : :
8819 : : /* Generate a cleanup, if necessary. */
8820 : 24068671 : cleanup = (decomp ? NULL_TREE
8821 : 24047937 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8822 : :
8823 : : /* Perform the initialization. */
8824 : 24068671 : if (init)
8825 : : {
8826 : 14860531 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8827 : 14860531 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8828 : 13583432 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8829 : 21118802 : && TREE_OPERAND (init, 0) == decl)
8830 : : {
8831 : : /* Stick simple initializers in DECL_INITIAL so that
8832 : : -Wno-init-self works (c++/34772). */
8833 : 7535370 : DECL_INITIAL (decl) = rinit;
8834 : :
8835 : 7535370 : if (warn_init_self && TYPE_REF_P (type))
8836 : : {
8837 : 2536 : STRIP_NOPS (rinit);
8838 : 2536 : if (rinit == decl)
8839 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
8840 : 3 : OPT_Winit_self,
8841 : : "reference %qD is initialized with itself", decl);
8842 : : }
8843 : : }
8844 : : else
8845 : : {
8846 : 7325161 : int saved_stmts_are_full_exprs_p;
8847 : :
8848 : : /* If we're only initializing a single object, guard the
8849 : : destructors of any temporaries used in its initializer with
8850 : : its destructor. */
8851 : 7325161 : if (cleanup)
8852 : 995283 : wrap_temporary_cleanups (init, cleanup);
8853 : :
8854 : 7325161 : gcc_assert (building_stmt_list_p ());
8855 : 7325161 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8856 : : /* Avoid CLEANUP_POINT_EXPR for the structured binding
8857 : : bases, those will have CLEANUP_POINT_EXPR at the end of
8858 : : code emitted by cp_finish_decomp. */
8859 : 7325161 : if (decomp)
8860 : 20300 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8861 : : else
8862 : 7304861 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8863 : 7325161 : finish_expr_stmt (init);
8864 : 7325161 : current_stmt_tree ()->stmts_are_full_exprs_p
8865 : 7325161 : = saved_stmts_are_full_exprs_p;
8866 : : }
8867 : : }
8868 : :
8869 : : /* Set this to 0 so we can tell whether an aggregate which was
8870 : : initialized was ever used. Don't do this if it has a
8871 : : destructor, so we don't complain about the 'resource
8872 : : allocation is initialization' idiom. Now set
8873 : : attribute((unused)) on types so decls of that type will be
8874 : : marked used. (see TREE_USED, above.) */
8875 : 24068671 : if (TYPE_NEEDS_CONSTRUCTING (type)
8876 : 2041388 : && ! already_used
8877 : 10284 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8878 : 24072924 : && DECL_NAME (decl))
8879 : 4225 : TREE_USED (decl) = 0;
8880 : 24064446 : else if (already_used)
8881 : 21724408 : TREE_USED (decl) = 1;
8882 : :
8883 : 24068671 : if (cleanup)
8884 : 1008722 : finish_decl_cleanup (decl, cleanup);
8885 : : }
8886 : :
8887 : : /* DECL is a VAR_DECL for a compiler-generated variable with static
8888 : : storage duration (like a virtual table) whose initializer is a
8889 : : compile-time constant. Initialize the variable and provide it to the
8890 : : back end. */
8891 : :
8892 : : void
8893 : 2183981 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8894 : : {
8895 : 2183981 : tree init;
8896 : 2183981 : gcc_assert (DECL_ARTIFICIAL (decl));
8897 : 2183981 : init = build_constructor (TREE_TYPE (decl), v);
8898 : 2183981 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8899 : 2183981 : DECL_INITIAL (decl) = init;
8900 : 2183981 : DECL_INITIALIZED_P (decl) = 1;
8901 : : /* Mark the decl as constexpr so that we can access its content
8902 : : at compile time. */
8903 : 2183981 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8904 : 2183981 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
8905 : 2183981 : determine_visibility (decl);
8906 : 2183981 : layout_var_decl (decl);
8907 : 2183981 : maybe_commonize_var (decl);
8908 : 2183981 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8909 : 2183981 : }
8910 : :
8911 : : /* INIT is the initializer for a variable, as represented by the
8912 : : parser. Returns true iff INIT is value-dependent. */
8913 : :
8914 : : static bool
8915 : 8416527 : value_dependent_init_p (tree init)
8916 : : {
8917 : 8416527 : if (TREE_CODE (init) == TREE_LIST)
8918 : : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8919 : 9840 : return any_value_dependent_elements_p (init);
8920 : 8406687 : else if (TREE_CODE (init) == CONSTRUCTOR)
8921 : : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8922 : : {
8923 : 43865 : if (dependent_type_p (TREE_TYPE (init)))
8924 : : return true;
8925 : :
8926 : 43859 : vec<constructor_elt, va_gc> *elts;
8927 : 43859 : size_t nelts;
8928 : 43859 : size_t i;
8929 : :
8930 : 43859 : elts = CONSTRUCTOR_ELTS (init);
8931 : 43859 : nelts = vec_safe_length (elts);
8932 : 586628 : for (i = 0; i < nelts; ++i)
8933 : 558220 : if (value_dependent_init_p ((*elts)[i].value))
8934 : : return true;
8935 : : }
8936 : : else
8937 : : /* It must be a simple expression, e.g., int i = 3; */
8938 : 8362822 : return value_dependent_expression_p (init);
8939 : :
8940 : : return false;
8941 : : }
8942 : :
8943 : : /* A helper function to be called via walk_tree. If any label exists
8944 : : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8945 : :
8946 : : static tree
8947 : 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8948 : : {
8949 : 337 : if (TYPE_P (*tp))
8950 : 0 : *walk_subtrees = 0;
8951 : 337 : if (TREE_CODE (*tp) == LABEL_DECL)
8952 : 6 : cfun->has_forced_label_in_static = 1;
8953 : 337 : return NULL_TREE;
8954 : : }
8955 : :
8956 : : /* Return true if DECL has either a trivial destructor, or for C++20
8957 : : is constexpr and has a constexpr destructor. */
8958 : :
8959 : : static bool
8960 : 42726250 : decl_maybe_constant_destruction (tree decl, tree type)
8961 : : {
8962 : 42726250 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8963 : 42726250 : || (cxx_dialect >= cxx20
8964 : 1649822 : && VAR_P (decl)
8965 : 1649503 : && DECL_DECLARED_CONSTEXPR_P (decl)
8966 : 339 : && type_has_constexpr_destructor (strip_array_types (type))));
8967 : : }
8968 : :
8969 : : static tree declare_simd_adjust_this (tree *, int *, void *);
8970 : :
8971 : : /* Helper function of omp_declare_variant_finalize. Finalize one
8972 : : "omp declare variant base" attribute. Return true if it should be
8973 : : removed. */
8974 : :
8975 : : static bool
8976 : 1719 : omp_declare_variant_finalize_one (tree decl, tree attr)
8977 : : {
8978 : 1719 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8979 : : {
8980 : 164 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8981 : : DECL_ARGUMENTS (decl), NULL);
8982 : 164 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8983 : : DECL_ARGUMENTS (decl), NULL);
8984 : : }
8985 : :
8986 : 1719 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
8987 : 1719 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8988 : : OMP_TRAIT_CONSTRUCT_SIMD);
8989 : 1719 : if (simd)
8990 : : {
8991 : 51 : TREE_VALUE (simd)
8992 : 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8993 : 51 : OMP_TS_PROPERTIES (simd));
8994 : : /* FIXME, adjusting simd args unimplemented. */
8995 : 51 : return true;
8996 : : }
8997 : :
8998 : 1668 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
8999 : 1668 : location_t varid_loc
9000 : 1668 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
9001 : 1668 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
9002 : 1668 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
9003 : 1668 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
9004 : :
9005 : 1668 : location_t save_loc = input_location;
9006 : 1668 : input_location = varid_loc;
9007 : :
9008 : 1668 : releasing_vec args;
9009 : 1668 : tree parm = DECL_ARGUMENTS (decl);
9010 : 1668 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9011 : 164 : parm = DECL_CHAIN (parm);
9012 : 2990 : for (; parm; parm = DECL_CHAIN (parm))
9013 : 1322 : vec_safe_push (args, forward_parm (parm));
9014 : :
9015 : 1668 : unsigned nappend_args = 0;
9016 : 1668 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9017 : 1668 : if (append_args_list)
9018 : : {
9019 : 354 : append_args_list = TREE_VALUE (append_args_list);
9020 : 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9021 : 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
9022 : : : NULL_TREE);
9023 : 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9024 : 387 : nappend_args++;
9025 : 354 : if (nappend_args)
9026 : : {
9027 : 237 : tree type;
9028 : 237 : if ((type = lookup_qualified_name (global_namespace,
9029 : : "omp_interop_t",
9030 : : LOOK_want::NORMAL,
9031 : : /*complain*/false)) == NULL_TREE
9032 : 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
9033 : : {
9034 : 24 : location_t loc = input_location;
9035 : 24 : variant = tree_strip_any_location_wrapper (variant);
9036 : 24 : if (!identifier_p (variant))
9037 : : {
9038 : 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9039 : 21 : variant = OVL_FIRST (variant);
9040 : 21 : loc = EXPR_LOC_OR_LOC (variant,
9041 : : DECL_SOURCE_LOCATION (variant));
9042 : : }
9043 : 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9044 : 24 : args->length () + 1, variant);
9045 : 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9046 : : "%<append_args%> specified here");
9047 : 24 : return true;
9048 : : }
9049 : 564 : for (unsigned i = 0; i < nappend_args; i++)
9050 : 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
9051 : : }
9052 : : }
9053 : :
9054 : 1644 : bool koenig_p = false;
9055 : 1644 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9056 : : {
9057 : 1574 : if (identifier_p (variant)
9058 : : /* In C++20, we may need to perform ADL for a template
9059 : : name. */
9060 : 1550 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9061 : 18 : && identifier_p (TREE_OPERAND (variant, 0))))
9062 : : {
9063 : 24 : if (!args->is_empty ())
9064 : : {
9065 : 24 : koenig_p = true;
9066 : 24 : if (!any_type_dependent_arguments_p (args))
9067 : 21 : variant = perform_koenig_lookup (variant, args,
9068 : : tf_warning_or_error);
9069 : : }
9070 : : else
9071 : 0 : variant = unqualified_fn_lookup_error (variant);
9072 : : }
9073 : 1550 : else if (!args->is_empty () && is_overloaded_fn (variant))
9074 : : {
9075 : 830 : tree fn = get_first_fn (variant);
9076 : 830 : fn = STRIP_TEMPLATE (fn);
9077 : 830 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9078 : 830 : || DECL_FUNCTION_MEMBER_P (fn)
9079 : 717 : || DECL_LOCAL_DECL_P (fn)))
9080 : : {
9081 : 717 : koenig_p = true;
9082 : 717 : if (!any_type_dependent_arguments_p (args))
9083 : 666 : variant = perform_koenig_lookup (variant, args,
9084 : : tf_warning_or_error);
9085 : : }
9086 : : }
9087 : : }
9088 : :
9089 : 1644 : if (idk == CP_ID_KIND_QUALIFIED)
9090 : 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9091 : : koenig_p, tf_warning_or_error);
9092 : 1638 : else if (idk == CP_ID_KIND_NONE
9093 : 64 : && TREE_CODE (variant) == FUNCTION_DECL
9094 : 64 : && DECL_IOBJ_MEMBER_FUNCTION_P (variant)
9095 : 1686 : && CLASS_TYPE_P (DECL_CONTEXT (decl)))
9096 : : {
9097 : 48 : tree saved_ccp = current_class_ptr;
9098 : 48 : tree saved_ccr = current_class_ref;
9099 : 48 : current_class_ptr = NULL_TREE;
9100 : 48 : current_class_ref = NULL_TREE;
9101 : 48 : inject_this_parameter (DECL_CONTEXT (decl), TYPE_UNQUALIFIED);
9102 : 48 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9103 : : koenig_p, tf_warning_or_error);
9104 : 48 : current_class_ptr = saved_ccp;
9105 : 48 : current_class_ref = saved_ccr;
9106 : : }
9107 : : else
9108 : 1590 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9109 : : koenig_p, tf_warning_or_error);
9110 : 1644 : if (variant == error_mark_node && !processing_template_decl)
9111 : : return true;
9112 : :
9113 : 1578 : if (TREE_CODE (variant) == TARGET_EXPR)
9114 : 9 : variant = TARGET_EXPR_INITIAL (variant);
9115 : :
9116 : 3147 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9117 : 1578 : input_location = save_loc;
9118 : :
9119 : 1578 : if (variant == decl)
9120 : : {
9121 : 0 : error_at (varid_loc, "variant %qD is the same as base function",
9122 : : variant);
9123 : 0 : return true;
9124 : : }
9125 : :
9126 : 1578 : if (variant)
9127 : : {
9128 : 1494 : bool fail;
9129 : 1494 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9130 : 1494 : if (!nappend_args)
9131 : 1335 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9132 : : COMPARE_STRICT);
9133 : : else
9134 : : {
9135 : 159 : unsigned nbase_args = 0;
9136 : 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9137 : 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9138 : 240 : nbase_args++;
9139 : 159 : tree vargs, varg;
9140 : 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9141 : 399 : for (unsigned i = 0; i < nbase_args && varg;
9142 : 240 : i++, varg = TREE_CHAIN (varg))
9143 : 240 : vargs = varg;
9144 : 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
9145 : 258 : varg = TREE_CHAIN (varg);
9146 : 159 : tree saved_vargs;
9147 : 159 : int saved_no_named_args_stdarg = 0;
9148 : 159 : if (nbase_args)
9149 : : {
9150 : 111 : saved_vargs = TREE_CHAIN (vargs);
9151 : 111 : TREE_CHAIN (vargs) = varg;
9152 : : }
9153 : : else
9154 : : {
9155 : 48 : saved_vargs = vargs;
9156 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9157 : 48 : saved_no_named_args_stdarg
9158 : 48 : = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9159 : 48 : if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9160 : 48 : && varg == NULL_TREE)
9161 : 2 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9162 : : }
9163 : : /* Skip assert check that TYPE_CANONICAL is the same. */
9164 : 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9165 : : COMPARE_STRUCTURAL);
9166 : 159 : if (nbase_args)
9167 : 111 : TREE_CHAIN (vargs) = saved_vargs;
9168 : : else
9169 : : {
9170 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9171 : 48 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9172 : 96 : = saved_no_named_args_stdarg;
9173 : : }
9174 : 159 : varg = saved_vargs;
9175 : 159 : if (!fail && !processing_template_decl)
9176 : 402 : for (unsigned i = 0; i < nappend_args;
9177 : 246 : i++, varg = TREE_CHAIN (varg))
9178 : 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9179 : : {
9180 : 9 : error_at (DECL_SOURCE_LOCATION (variant),
9181 : : "argument %d of %qD must be of %<omp_interop_t%>",
9182 : 9 : nbase_args + i + 1, variant);
9183 : 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9184 : : "%<append_args%> specified here");
9185 : 9 : break;
9186 : : }
9187 : : }
9188 : 1494 : if (fail)
9189 : : {
9190 : 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
9191 : : "types", variant, decl);
9192 : 69 : return true;
9193 : : }
9194 : 1425 : if (fndecl_built_in_p (variant)
9195 : 1425 : && (startswith (varname, "__builtin_")
9196 : 0 : || startswith (varname, "__sync_")
9197 : 0 : || startswith (varname, "__atomic_")))
9198 : : {
9199 : 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
9200 : 6 : return true;
9201 : : }
9202 : : else
9203 : : {
9204 : 1419 : tree construct
9205 : 1419 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9206 : 1419 : omp_mark_declare_variant (match_loc, variant, construct);
9207 : 1419 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9208 : : return true;
9209 : 1039 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9210 : :
9211 : : // Prepend adjust_args list to variant attributes
9212 : 1039 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9213 : 1039 : if (adjust_args_list != NULL_TREE)
9214 : : {
9215 : 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
9216 : 285 : && TREE_VALUE (adjust_args_list))
9217 : : {
9218 : : /* Shift arg position for the added 'this' pointer. */
9219 : : /* Handle need_device_ptr */
9220 : 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9221 : 51 : t; t = TREE_CHAIN (t))
9222 : 30 : TREE_VALUE (t)
9223 : 30 : = build_int_cst (TREE_TYPE (t),
9224 : 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
9225 : : }
9226 : 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9227 : : {
9228 : : /* Shift likewise the number of args after which the
9229 : : interop object should be added. */
9230 : 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9231 : 6 : TREE_PURPOSE (nargs)
9232 : 6 : = build_int_cst (TREE_TYPE (nargs),
9233 : 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9234 : : }
9235 : 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9236 : 264 : TREE_VALUE (t)
9237 : 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9238 : 528 : DECL_ATTRIBUTES (variant) = tree_cons (
9239 : : get_identifier ("omp declare variant variant args"),
9240 : 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9241 : : }
9242 : : }
9243 : : }
9244 : 84 : else if (!processing_template_decl)
9245 : : {
9246 : 0 : error_at (varid_loc, "could not find variant declaration");
9247 : 0 : return true;
9248 : : }
9249 : :
9250 : : return false;
9251 : 1668 : }
9252 : :
9253 : : /* Helper function, finish up "omp declare variant base" attribute
9254 : : now that there is a DECL. ATTR is the first "omp declare variant base"
9255 : : attribute. */
9256 : :
9257 : : void
9258 : 1530 : omp_declare_variant_finalize (tree decl, tree attr)
9259 : : {
9260 : 1530 : size_t attr_len = strlen ("omp declare variant base");
9261 : 1530 : tree *list = &DECL_ATTRIBUTES (decl);
9262 : 1530 : bool remove_all = false;
9263 : 1530 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
9264 : 1530 : if (TREE_CHAIN (TREE_VALUE (attr))
9265 : 1530 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9266 : 3060 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9267 : 1530 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9268 : 3060 : if (DECL_CONSTRUCTOR_P (decl))
9269 : : {
9270 : 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9271 : 27 : remove_all = true;
9272 : : }
9273 : 1503 : else if (DECL_DESTRUCTOR_P (decl))
9274 : : {
9275 : 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9276 : 9 : remove_all = true;
9277 : : }
9278 : 1494 : else if (DECL_DEFAULTED_FN (decl))
9279 : : {
9280 : 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9281 : 2 : remove_all = true;
9282 : : }
9283 : 1492 : else if (DECL_DELETED_FN (decl))
9284 : : {
9285 : 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9286 : 2 : remove_all = true;
9287 : : }
9288 : 1490 : else if (DECL_VIRTUAL_P (decl))
9289 : : {
9290 : 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9291 : 0 : remove_all = true;
9292 : : }
9293 : : /* This loop is like private_lookup_attribute, except that it works
9294 : : with tree * rather than tree, as we might want to remove the
9295 : : attributes that are diagnosed as errorneous. */
9296 : 3301 : while (*list)
9297 : : {
9298 : 1771 : tree attr = get_attribute_name (*list);
9299 : 1771 : size_t ident_len = IDENTIFIER_LENGTH (attr);
9300 : 4665 : if (cmp_attribs ("omp declare variant base", attr_len,
9301 : 1771 : IDENTIFIER_POINTER (attr), ident_len))
9302 : : {
9303 : 1759 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
9304 : : {
9305 : 636 : *list = TREE_CHAIN (*list);
9306 : 636 : continue;
9307 : : }
9308 : : }
9309 : 1135 : list = &TREE_CHAIN (*list);
9310 : : }
9311 : 1530 : }
9312 : :
9313 : : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9314 : :
9315 : : /* Finish processing of a declaration;
9316 : : install its line number and initial value.
9317 : : If the length of an array type is not known before,
9318 : : it must be determined now, from the initial value, or it is an error.
9319 : :
9320 : : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9321 : : true, then INIT is an integral constant expression.
9322 : :
9323 : : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9324 : : if the (init) syntax was used.
9325 : :
9326 : : DECOMP is first identifier's DECL and identifier count in a structured
9327 : : bindings, nullptr if not a structured binding. */
9328 : :
9329 : : void
9330 : 291787836 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9331 : : tree asmspec_tree, int flags, cp_decomp *decomp)
9332 : : {
9333 : 291787836 : vec<tree, va_gc> *cleanups = NULL;
9334 : 291787836 : const char *asmspec = NULL;
9335 : 291787836 : int was_readonly = 0;
9336 : 291787836 : bool var_definition_p = false;
9337 : 291787836 : tree auto_node;
9338 : 291787836 : auto_vec<tree> extra_cleanups;
9339 : 291787836 : tree aggregates1 = NULL_TREE;
9340 : 291787836 : struct decomp_cleanup {
9341 : : tree decl;
9342 : : cp_decomp *&decomp;
9343 : 291785139 : ~decomp_cleanup ()
9344 : : {
9345 : 291785139 : if (decomp && DECL_DECOMPOSITION_P (decl))
9346 : 91999 : cp_finish_decomp (decl, decomp);
9347 : 291785139 : }
9348 : 291787836 : } decomp_cl = { decl, decomp };
9349 : :
9350 : 291787836 : if (decl == error_mark_node)
9351 : : return;
9352 : 291787796 : else if (! decl)
9353 : : {
9354 : 0 : if (init)
9355 : 0 : error ("assignment (not initialization) in declaration");
9356 : 0 : return;
9357 : : }
9358 : :
9359 : 291787796 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9360 : : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9361 : 291787796 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
9362 : :
9363 : 291787796 : tree type = TREE_TYPE (decl);
9364 : 291787796 : if (type == error_mark_node)
9365 : : return;
9366 : :
9367 : 291787253 : if (VAR_P (decl) && is_copy_initialization (init))
9368 : 82360246 : flags |= LOOKUP_ONLYCONVERTING;
9369 : :
9370 : : /* Warn about register storage specifiers except when in GNU global
9371 : : or local register variable extension. */
9372 : 291787253 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9373 : : {
9374 : 1931 : if (cxx_dialect >= cxx17)
9375 : 1282 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9376 : : "ISO C++17 does not allow %<register%> storage "
9377 : : "class specifier");
9378 : : else
9379 : 649 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9380 : : "%<register%> storage class specifier used");
9381 : : }
9382 : :
9383 : : /* If a name was specified, get the string. */
9384 : 291787253 : if (at_namespace_scope_p ())
9385 : 62327030 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9386 : 291787253 : if (asmspec_tree && asmspec_tree != error_mark_node)
9387 : 997970 : asmspec = TREE_STRING_POINTER (asmspec_tree);
9388 : :
9389 : 291787253 : bool in_class_decl
9390 : 291787253 : = (current_class_type
9391 : 180382119 : && CP_DECL_CONTEXT (decl) == current_class_type
9392 : 121026299 : && TYPE_BEING_DEFINED (current_class_type)
9393 : 411234728 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9394 : :
9395 : 115978843 : if (in_class_decl
9396 : 115978843 : && (DECL_INITIAL (decl) || init))
9397 : 83783881 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9398 : :
9399 : 291787253 : if (VAR_P (decl)
9400 : 291787253 : && (auto_node = type_uses_auto (type)))
9401 : : {
9402 : 9217443 : tree d_init;
9403 : 9217443 : if (init == NULL_TREE)
9404 : : {
9405 : 1796 : if (DECL_LANG_SPECIFIC (decl)
9406 : 1714 : && DECL_TEMPLATE_INSTANTIATION (decl)
9407 : 3507 : && !DECL_TEMPLATE_INSTANTIATED (decl))
9408 : : {
9409 : : /* init is null because we're deferring instantiating the
9410 : : initializer until we need it. Well, we need it now. */
9411 : 1705 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9412 : 1705 : return;
9413 : : }
9414 : :
9415 : 91 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9416 : : /* Class deduction with no initializer is OK. */;
9417 : : else
9418 : : {
9419 : : /* Ordinary auto deduction without an initializer, a situation
9420 : : which grokdeclarator already detects and rejects for the most
9421 : : part. But we can still get here if we're instantiating a
9422 : : variable template before we've fully parsed (and attached) its
9423 : : initializer, e.g. template<class> auto x = x<int>; */
9424 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
9425 : : "declaration of %q#D has no initializer", decl);
9426 : 3 : TREE_TYPE (decl) = error_mark_node;
9427 : 3 : return;
9428 : : }
9429 : : }
9430 : 9215735 : d_init = init;
9431 : 9215735 : if (d_init)
9432 : : {
9433 : 9215647 : if (TREE_CODE (d_init) == TREE_LIST
9434 : 9252737 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9435 : 22799 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9436 : : tf_warning_or_error);
9437 : 9215647 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9438 : : /* Force auto deduction now. Use tf_none to avoid redundant warnings
9439 : : on deprecated-14.C. */
9440 : 9215647 : mark_single_function (d_init, tf_none);
9441 : : }
9442 : 9215735 : enum auto_deduction_context adc = adc_variable_type;
9443 : 9215735 : if (DECL_DECOMPOSITION_P (decl))
9444 : : adc = adc_decomp_type;
9445 : 9215735 : tree outer_targs = NULL_TREE;
9446 : 9215735 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9447 : 338 : && DECL_LANG_SPECIFIC (decl)
9448 : 126 : && DECL_TEMPLATE_INFO (decl)
9449 : 9215792 : && !DECL_FUNCTION_SCOPE_P (decl))
9450 : : /* The outer template arguments might be needed for satisfaction.
9451 : : (For function scope variables, do_auto_deduction will obtain the
9452 : : outer template arguments from current_function_decl.) */
9453 : 57 : outer_targs = DECL_TI_ARGS (decl);
9454 : 9215735 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9455 : : tf_warning_or_error, adc,
9456 : : outer_targs, flags);
9457 : 9215735 : if (type == error_mark_node)
9458 : : return;
9459 : 9214873 : if (TREE_CODE (type) == FUNCTION_TYPE)
9460 : : {
9461 : 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
9462 : : "did you forget the %<()%>?", decl);
9463 : 3 : TREE_TYPE (decl) = error_mark_node;
9464 : 3 : return;
9465 : : }
9466 : : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9467 : 9214870 : if (!processing_template_decl
9468 : 2168644 : && !type_uses_auto (type)
9469 : 11383507 : && !COMPLETE_TYPE_P (complete_type (type)))
9470 : : {
9471 : 18 : auto_diagnostic_group d;
9472 : 18 : error_at (location_of (decl),
9473 : : "deduced type %qT for %qD is incomplete", type, decl);
9474 : 18 : cxx_incomplete_type_inform (type);
9475 : 18 : TREE_TYPE (decl) = error_mark_node;
9476 : 18 : return;
9477 : 18 : }
9478 : :
9479 : : /* Now that we have a type, try these again. */
9480 : 9214852 : layout_decl (decl, 0);
9481 : 9214852 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9482 : :
9483 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
9484 : 9214852 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9485 : : {
9486 : 17522 : tree tmpl = template_for_substitution (decl);
9487 : 17522 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9488 : 15255 : TREE_TYPE (tmpl) = type;
9489 : : }
9490 : : }
9491 : :
9492 : 291784662 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9493 : : {
9494 : 61 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9495 : 61 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9496 : : {
9497 : 6 : init = NULL_TREE;
9498 : 6 : DECL_EXTERNAL (decl) = 1;
9499 : : }
9500 : : }
9501 : :
9502 : 291784662 : if (VAR_P (decl)
9503 : 104597760 : && DECL_CLASS_SCOPE_P (decl)
9504 : 16220717 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
9505 : : TCTX_STATIC_STORAGE, type)
9506 : 308005379 : && DECL_INITIALIZED_IN_CLASS_P (decl))
9507 : 15028380 : check_static_variable_definition (decl, type);
9508 : :
9509 : 291784662 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
9510 : : {
9511 : 25968883 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9512 : 25968883 : ? TCTX_THREAD_STORAGE
9513 : : : TCTX_STATIC_STORAGE);
9514 : 25968883 : verify_type_context (input_location, context, TREE_TYPE (decl));
9515 : : }
9516 : :
9517 : 291784662 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
9518 : : {
9519 : 385637 : tree clone;
9520 : 385637 : if (init == ridpointers[(int)RID_DELETE]
9521 : 385637 : || (TREE_CODE (init) == STRING_CST
9522 : 24 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9523 : : {
9524 : : /* FIXME check this is 1st decl. */
9525 : 362354 : if (UNLIKELY (DECL_MAIN_P (decl)))
9526 : : {
9527 : : /* [basic.start.main]/3: A program that defines main as deleted
9528 : : is ill-formed. */
9529 : 3 : error ("%<::main%> cannot be deleted");
9530 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9531 : : }
9532 : : else
9533 : : {
9534 : 362351 : DECL_DELETED_FN (decl) = 1;
9535 : 362351 : DECL_DECLARED_INLINE_P (decl) = 1;
9536 : 362351 : DECL_INITIAL (decl)
9537 : 362351 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9538 : 362369 : FOR_EACH_CLONE (clone, decl)
9539 : : {
9540 : 18 : DECL_DELETED_FN (clone) = 1;
9541 : 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9542 : 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9543 : : }
9544 : : }
9545 : 362354 : init = NULL_TREE;
9546 : : }
9547 : 23283 : else if (init == ridpointers[(int)RID_DEFAULT])
9548 : : {
9549 : 23271 : if (defaultable_fn_check (decl))
9550 : 23247 : DECL_DEFAULTED_FN (decl) = 1;
9551 : : else
9552 : 24 : DECL_INITIAL (decl) = NULL_TREE;
9553 : : }
9554 : : }
9555 : :
9556 : 291784662 : if (init && VAR_P (decl))
9557 : : {
9558 : 89764027 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9559 : : /* If DECL is a reference, then we want to know whether init is a
9560 : : reference constant; init_const_expr_p as passed tells us whether
9561 : : it's an rvalue constant. */
9562 : 89764027 : if (TYPE_REF_P (type))
9563 : 2309307 : init_const_expr_p = potential_constant_expression (init);
9564 : 89764027 : if (init_const_expr_p)
9565 : : {
9566 : : /* Set these flags now for templates. We'll update the flags in
9567 : : store_init_value for instantiations. */
9568 : 77393094 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9569 : 77393094 : if (decl_maybe_constant_var_p (decl)
9570 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9571 : 77393094 : && !TYPE_REF_P (type))
9572 : 29610670 : TREE_CONSTANT (decl) = true;
9573 : : }
9574 : : /* This is handled mostly by gimplify.cc, but we have to deal with
9575 : : not warning about int x = x; as it is a GCC extension to turn off
9576 : : this warning but only if warn_init_self is zero. */
9577 : 89764027 : if (!DECL_EXTERNAL (decl)
9578 : 88263038 : && !TREE_STATIC (decl)
9579 : 64681055 : && decl == tree_strip_any_location_wrapper (init)
9580 : 89767829 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9581 : 3778 : suppress_warning (decl, OPT_Winit_self);
9582 : : }
9583 : 202020635 : else if (VAR_P (decl)
9584 : 14833733 : && COMPLETE_TYPE_P (type)
9585 : 11651004 : && !TYPE_REF_P (type)
9586 : 11650640 : && !dependent_type_p (type)
9587 : 213376845 : && is_really_empty_class (type, /*ignore_vptr*/false))
9588 : : /* We have no initializer but there's nothing to initialize anyway.
9589 : : Treat DECL as constant due to c++/109876. */
9590 : 500117 : TREE_CONSTANT (decl) = true;
9591 : :
9592 : 291784662 : if (flag_openmp
9593 : 834517 : && TREE_CODE (decl) == FUNCTION_DECL
9594 : : /* #pragma omp declare variant on methods handled in finish_struct
9595 : : instead. */
9596 : 292145658 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9597 : 139274 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9598 : 222185 : if (tree attr = lookup_attribute ("omp declare variant base",
9599 : 222185 : DECL_ATTRIBUTES (decl)))
9600 : 970 : omp_declare_variant_finalize (decl, attr);
9601 : :
9602 : 291784662 : if (processing_template_decl)
9603 : : {
9604 : 167754959 : bool type_dependent_p;
9605 : :
9606 : : /* Add this declaration to the statement-tree. */
9607 : 167754959 : if (at_function_scope_p ())
9608 : 60906320 : add_decl_expr (decl);
9609 : :
9610 : 167754959 : type_dependent_p = dependent_type_p (type);
9611 : :
9612 : 167754959 : if (check_for_bare_parameter_packs (init))
9613 : : {
9614 : 3 : init = NULL_TREE;
9615 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9616 : : }
9617 : :
9618 : : /* Generally, initializers in templates are expanded when the
9619 : : template is instantiated. But, if DECL is a variable constant
9620 : : then it can be used in future constant expressions, so its value
9621 : : must be available. */
9622 : :
9623 : 167754959 : bool dep_init = false;
9624 : :
9625 : 167754959 : if (!VAR_P (decl) || type_dependent_p)
9626 : : /* We can't do anything if the decl has dependent type. */;
9627 : 20484443 : else if (init
9628 : 18892407 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9629 : 15004913 : && !TYPE_REF_P (type)
9630 : 14977619 : && decl_maybe_constant_var_p (decl)
9631 : 28342750 : && !(dep_init = value_dependent_init_p (init)))
9632 : : {
9633 : : /* This variable seems to be a non-dependent constant, so process
9634 : : its initializer. If check_initializer returns non-null the
9635 : : initialization wasn't constant after all. */
9636 : 1880357 : tree init_code;
9637 : 1880357 : cleanups = make_tree_vector ();
9638 : 1880357 : init_code = check_initializer (decl, init, flags, &cleanups);
9639 : 1880357 : if (init_code == NULL_TREE)
9640 : 1880357 : init = NULL_TREE;
9641 : 1880357 : release_tree_vector (cleanups);
9642 : : }
9643 : : else
9644 : : {
9645 : 18604086 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9646 : : /* Try to deduce array size. */
9647 : 18604086 : maybe_deduce_size_from_array_init (decl, init);
9648 : : /* And complain about multiple initializers. */
9649 : 17012050 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9650 : 18665988 : && !MAYBE_CLASS_TYPE_P (type))
9651 : 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9652 : : tf_warning_or_error);
9653 : : }
9654 : :
9655 : 167754959 : if (init)
9656 : 48770379 : DECL_INITIAL (decl) = init;
9657 : :
9658 : 167754959 : if (dep_init)
9659 : : {
9660 : 5977950 : retrofit_lang_decl (decl);
9661 : 5977950 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9662 : : }
9663 : :
9664 : 167754959 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9665 : : {
9666 : 9 : if (TREE_ADDRESSABLE (decl))
9667 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
9668 : : "address of explicit register variable %qD requested",
9669 : : decl);
9670 : : else
9671 : : {
9672 : 6 : set_user_assembler_name (decl, asmspec);
9673 : 6 : DECL_HARD_REGISTER (decl) = 1;
9674 : : }
9675 : : }
9676 : 167754959 : return;
9677 : : }
9678 : :
9679 : : /* Just store non-static data member initializers for later. */
9680 : 124029703 : if (init && TREE_CODE (decl) == FIELD_DECL)
9681 : 407899 : DECL_INITIAL (decl) = init;
9682 : :
9683 : : /* Take care of TYPE_DECLs up front. */
9684 : 124029703 : if (TREE_CODE (decl) == TYPE_DECL)
9685 : : {
9686 : 10029630 : if (type != error_mark_node
9687 : 10029630 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9688 : : {
9689 : 4615195 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9690 : 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9691 : 4615195 : set_identifier_type_value (DECL_NAME (decl), decl);
9692 : : }
9693 : :
9694 : : /* If we have installed this as the canonical typedef for this
9695 : : type, and that type has not been defined yet, delay emitting
9696 : : the debug information for it, as we will emit it later. */
9697 : 10029630 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9698 : 10029630 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9699 : 579383 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9700 : :
9701 : 10029630 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9702 : : at_eof);
9703 : 10029630 : return;
9704 : : }
9705 : :
9706 : : /* A reference will be modified here, as it is initialized. */
9707 : 114000073 : if (! DECL_EXTERNAL (decl)
9708 : 52234686 : && TREE_READONLY (decl)
9709 : 141598924 : && TYPE_REF_P (type))
9710 : : {
9711 : 239324 : was_readonly = 1;
9712 : 239324 : TREE_READONLY (decl) = 0;
9713 : : }
9714 : :
9715 : : /* This needs to happen before extend_ref_init_temps. */
9716 : 114000073 : if (VAR_OR_FUNCTION_DECL_P (decl))
9717 : : {
9718 : 105249149 : if (VAR_P (decl))
9719 : 50037738 : maybe_commonize_var (decl);
9720 : 105249149 : determine_visibility (decl);
9721 : : }
9722 : :
9723 : 114000073 : if (VAR_P (decl))
9724 : : {
9725 : 50037738 : duration_kind dk = decl_storage_duration (decl);
9726 : : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9727 : : only to a declaration of a variable with static or thread storage
9728 : : duration." */
9729 : 50037738 : if (DECL_DECLARED_CONSTINIT_P (decl)
9730 : 50037738 : && !(dk == dk_thread || dk == dk_static))
9731 : : {
9732 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
9733 : : "%<constinit%> can only be applied to a variable with "
9734 : : "static or thread storage duration");
9735 : 24 : return;
9736 : : }
9737 : :
9738 : 50037714 : if (decomp)
9739 : : {
9740 : 41135 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9741 : 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9742 : : "%<constinit%> can be applied to structured binding "
9743 : : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
9744 : 41135 : cp_maybe_mangle_decomp (decl, decomp);
9745 : 41135 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9746 : : {
9747 : 346 : if (CP_DECL_THREAD_LOCAL_P (decl))
9748 : 37 : aggregates1 = tls_aggregates;
9749 : : else
9750 : 309 : aggregates1 = static_aggregates;
9751 : : }
9752 : : }
9753 : :
9754 : : /* If this is a local variable that will need a mangled name,
9755 : : register it now. We must do this before processing the
9756 : : initializer for the variable, since the initialization might
9757 : : require a guard variable, and since the mangled name of the
9758 : : guard variable will depend on the mangled name of this
9759 : : variable. */
9760 : 100075428 : if (DECL_FUNCTION_SCOPE_P (decl)
9761 : 24213053 : && TREE_STATIC (decl)
9762 : 50182078 : && !DECL_ARTIFICIAL (decl))
9763 : : {
9764 : : /* The variable holding an anonymous union will have had its
9765 : : discriminator set in finish_anon_union, after which it's
9766 : : NAME will have been cleared. */
9767 : 143475 : if (DECL_NAME (decl))
9768 : 143432 : determine_local_discriminator (decl);
9769 : : /* Normally has_forced_label_in_static is set during GIMPLE
9770 : : lowering, but [cd]tors are never actually compiled directly.
9771 : : We need to set this early so we can deal with the label
9772 : : address extension. */
9773 : 143475 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
9774 : 143446 : || DECL_DESTRUCTOR_P (current_function_decl))
9775 : 143485 : && init)
9776 : : {
9777 : 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
9778 : 29 : add_local_decl (cfun, decl);
9779 : : }
9780 : : /* And make sure it's in the symbol table for
9781 : : c_parse_final_cleanups to find. */
9782 : 143475 : varpool_node::get_create (decl);
9783 : : }
9784 : :
9785 : 50037714 : if (flag_openmp
9786 : 189210 : && VAR_P (decl)
9787 : 189210 : && DECL_LANG_SPECIFIC (decl)
9788 : 111860 : && DECL_OMP_DECLARE_MAPPER_P (decl)
9789 : 50037718 : && init)
9790 : : {
9791 : 3 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9792 : 3 : DECL_INITIAL (decl) = init;
9793 : : }
9794 : : /* Convert the initializer to the type of DECL, if we have not
9795 : : already initialized DECL. */
9796 : 50037711 : else if (!DECL_INITIALIZED_P (decl)
9797 : : /* If !DECL_EXTERNAL then DECL is being defined. In the
9798 : : case of a static data member initialized inside the
9799 : : class-specifier, there can be an initializer even if DECL
9800 : : is *not* defined. */
9801 : 50037711 : && (!DECL_EXTERNAL (decl) || init))
9802 : : {
9803 : 44454444 : cleanups = make_tree_vector ();
9804 : 44454444 : init = check_initializer (decl, init, flags, &cleanups);
9805 : :
9806 : : /* Handle:
9807 : :
9808 : : [dcl.init]
9809 : :
9810 : : The memory occupied by any object of static storage
9811 : : duration is zero-initialized at program startup before
9812 : : any other initialization takes place.
9813 : :
9814 : : We cannot create an appropriate initializer until after
9815 : : the type of DECL is finalized. If DECL_INITIAL is set,
9816 : : then the DECL is statically initialized, and any
9817 : : necessary zero-initialization has already been performed. */
9818 : 44451747 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9819 : 426965 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9820 : : /*nelts=*/NULL_TREE,
9821 : : /*static_storage_p=*/true);
9822 : : /* Remember that the initialization for this variable has
9823 : : taken place. */
9824 : 44451747 : DECL_INITIALIZED_P (decl) = 1;
9825 : : /* This declaration is the definition of this variable,
9826 : : unless we are initializing a static data member within
9827 : : the class specifier. */
9828 : 44451747 : if (!DECL_EXTERNAL (decl))
9829 : 50035017 : var_definition_p = true;
9830 : : }
9831 : : /* If the variable has an array type, lay out the type, even if
9832 : : there is no initializer. It is valid to index through the
9833 : : array, and we must get TYPE_ALIGN set correctly on the array
9834 : : type. */
9835 : 5583267 : else if (TREE_CODE (type) == ARRAY_TYPE)
9836 : 223246 : layout_type (type);
9837 : :
9838 : 50035017 : if (TREE_STATIC (decl)
9839 : 25398873 : && !at_function_scope_p ()
9840 : 75289526 : && current_function_decl == NULL)
9841 : : /* So decl is a global variable or a static member of a
9842 : : non local class. Record the types it uses
9843 : : so that we can decide later to emit debug info for them. */
9844 : 25254497 : record_types_used_by_current_var_decl (decl);
9845 : : }
9846 : :
9847 : : /* Add this declaration to the statement-tree. This needs to happen
9848 : : after the call to check_initializer so that the DECL_EXPR for a
9849 : : reference temp is added before the DECL_EXPR for the reference itself. */
9850 : 113997352 : if (DECL_FUNCTION_SCOPE_P (decl))
9851 : : {
9852 : : /* If we're building a variable sized type, and we might be
9853 : : reachable other than via the top of the current binding
9854 : : level, then create a new BIND_EXPR so that we deallocate
9855 : : the object at the right time. */
9856 : 24213053 : if (VAR_P (decl)
9857 : 24213053 : && DECL_SIZE (decl)
9858 : 24162389 : && !TREE_CONSTANT (DECL_SIZE (decl))
9859 : 24214058 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9860 : : {
9861 : 3 : tree bind;
9862 : 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9863 : 3 : TREE_SIDE_EFFECTS (bind) = 1;
9864 : 3 : add_stmt (bind);
9865 : 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
9866 : : }
9867 : 24213053 : add_decl_expr (decl);
9868 : : }
9869 : :
9870 : 113997352 : tree decomp_init = NULL_TREE;
9871 : : /* Let the middle end know about variables and functions -- but not
9872 : : static data members in uninstantiated class templates. */
9873 : 113997352 : if (VAR_OR_FUNCTION_DECL_P (decl))
9874 : : {
9875 : 105246428 : if (VAR_P (decl))
9876 : : {
9877 : 50035017 : layout_var_decl (decl);
9878 : 50035017 : if (!flag_weak)
9879 : : /* Check again now that we have an initializer. */
9880 : 57 : maybe_commonize_var (decl);
9881 : : /* A class-scope constexpr variable with an out-of-class declaration.
9882 : : C++17 makes them implicitly inline, but still force it out. */
9883 : 67638894 : if (DECL_INLINE_VAR_P (decl)
9884 : 18634353 : && !DECL_VAR_DECLARED_INLINE_P (decl)
9885 : 10037808 : && !DECL_TEMPLATE_INSTANTIATION (decl)
9886 : 34016683 : && !in_class_decl)
9887 : 103 : mark_needed (decl);
9888 : : }
9889 : :
9890 : 105246428 : if (var_definition_p
9891 : : /* With -fmerge-all-constants, gimplify_init_constructor
9892 : : might add TREE_STATIC to aggregate variables. */
9893 : 43398097 : && (TREE_STATIC (decl)
9894 : 24068831 : || (flag_merge_constants >= 2
9895 : 38 : && AGGREGATE_TYPE_P (type))))
9896 : : {
9897 : : /* If a TREE_READONLY variable needs initialization
9898 : : at runtime, it is no longer readonly and we need to
9899 : : avoid MEM_READONLY_P being set on RTL created for it. */
9900 : 19329269 : if (init)
9901 : : {
9902 : 14258 : if (TREE_READONLY (decl))
9903 : 524 : TREE_READONLY (decl) = 0;
9904 : : was_readonly = 0;
9905 : : }
9906 : 19315011 : else if (was_readonly)
9907 : 2032 : TREE_READONLY (decl) = 1;
9908 : :
9909 : : /* Likewise if it needs destruction. */
9910 : 19329269 : if (!decl_maybe_constant_destruction (decl, type))
9911 : 3431 : TREE_READONLY (decl) = 0;
9912 : : }
9913 : 85917159 : else if (VAR_P (decl)
9914 : 30705748 : && CP_DECL_THREAD_LOCAL_P (decl)
9915 : 20201 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9916 : 20201 : && (was_readonly || TREE_READONLY (decl))
9917 : 85917174 : && var_needs_tls_wrapper (decl))
9918 : : {
9919 : : /* TLS variables need dynamic initialization by the TLS wrapper
9920 : : function, we don't want to hoist accesses to it before the
9921 : : wrapper. */
9922 : 6 : was_readonly = 0;
9923 : 6 : TREE_READONLY (decl) = 0;
9924 : : }
9925 : :
9926 : 105246428 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
9927 : :
9928 : : /* Check for abstractness of the type. */
9929 : 105246428 : if (var_definition_p)
9930 : 43398097 : abstract_virtuals_error (decl, type);
9931 : :
9932 : 105246428 : if (decomp && !cp_finish_decomp (decl, decomp, true))
9933 : 20162 : decomp = NULL;
9934 : :
9935 : 105246428 : if (TREE_TYPE (decl) == error_mark_node)
9936 : : /* No initialization required. */
9937 : : ;
9938 : 105246358 : else if (TREE_CODE (decl) == FUNCTION_DECL)
9939 : : {
9940 : 55211411 : if (init)
9941 : : {
9942 : 579 : if (init == ridpointers[(int)RID_DEFAULT])
9943 : : {
9944 : : /* An out-of-class default definition is defined at
9945 : : the point where it is explicitly defaulted. */
9946 : 567 : if (DECL_DELETED_FN (decl))
9947 : 6 : maybe_explain_implicit_delete (decl);
9948 : 561 : else if (DECL_INITIAL (decl) == error_mark_node)
9949 : 537 : synthesize_method (decl);
9950 : : }
9951 : : else
9952 : 24 : error_at (cp_expr_loc_or_loc (init,
9953 : 12 : DECL_SOURCE_LOCATION (decl)),
9954 : : "function %q#D is initialized like a variable",
9955 : : decl);
9956 : : }
9957 : : /* else no initialization required. */
9958 : : }
9959 : 50034947 : else if (DECL_EXTERNAL (decl)
9960 : 50034947 : && ! (DECL_LANG_SPECIFIC (decl)
9961 : 6330146 : && DECL_NOT_REALLY_EXTERN (decl)))
9962 : : {
9963 : : /* check_initializer will have done any constant initialization. */
9964 : : }
9965 : : /* A variable definition. */
9966 : 45273908 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9967 : : {
9968 : : /* Initialize the local variable. */
9969 : 24068671 : if (!decomp)
9970 : 24047937 : initialize_local_var (decl, init, false);
9971 : : else
9972 : : {
9973 : 20734 : tree cleanup = NULL_TREE;
9974 : 20734 : if (DECL_SIZE (decl))
9975 : 20734 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9976 : : /* If cp_finish_decomp needs to emit any code, we need to emit that
9977 : : code after code emitted by initialize_local_var in a single
9978 : : CLEANUP_POINT_EXPR, so that temporaries are destructed only
9979 : : after the cp_finish_decomp emitted code.
9980 : : If there are any cleanups, either extend_ref_init_temps
9981 : : created ones or e.g. array destruction, push those first
9982 : : with the cleanups guarded on a bool temporary, initially
9983 : : set to false and set to true after initialize_local_var
9984 : : emitted code. */
9985 : 20734 : tree guard = NULL_TREE;
9986 : 20734 : if (cleanups || cleanup)
9987 : : {
9988 : 20734 : guard = get_internal_target_expr (boolean_false_node);
9989 : 20734 : add_stmt (guard);
9990 : 20734 : guard = TARGET_EXPR_SLOT (guard);
9991 : : }
9992 : 20734 : tree sl = push_stmt_list ();
9993 : 20734 : initialize_local_var (decl, init, true);
9994 : 20734 : if (guard)
9995 : : {
9996 : 20734 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9997 : : guard, boolean_true_node));
9998 : 20752 : for (tree &t : *cleanups)
9999 : 18 : t = build3 (COND_EXPR, void_type_node,
10000 : : guard, t, void_node);
10001 : 20734 : if (cleanup)
10002 : 25 : cleanup = build3 (COND_EXPR, void_type_node,
10003 : : guard, cleanup, void_node);
10004 : : }
10005 : 20734 : unsigned before = stmt_list_stack->length ();
10006 : 20734 : cp_finish_decomp (decl, decomp);
10007 : 20734 : decomp = NULL;
10008 : 20734 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
10009 : 20734 : sl = pop_stmt_list (sl);
10010 : 20734 : if (n_extra_cleanups)
10011 : : {
10012 : : /* If cp_finish_decomp needs any cleanups, such as for
10013 : : extend_ref_init_temps created vars, pop_stmt_list
10014 : : popped that all, so push those extra cleanups around
10015 : : the whole sequence with a guard variable. */
10016 : 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
10017 : 15 : guard = get_internal_target_expr (integer_zero_node);
10018 : 15 : add_stmt (guard);
10019 : 15 : guard = TARGET_EXPR_SLOT (guard);
10020 : 63 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
10021 : : {
10022 : 48 : tree_stmt_iterator tsi = tsi_last (sl);
10023 : 48 : gcc_assert (!tsi_end_p (tsi));
10024 : 48 : tree last = tsi_stmt (tsi);
10025 : 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
10026 : : && !CLEANUP_EH_ONLY (last));
10027 : 48 : tree cst = build_int_cst (integer_type_node, i + 1);
10028 : 96 : tree cl = build3 (COND_EXPR, void_type_node,
10029 : : build2 (GE_EXPR, boolean_type_node,
10030 : : guard, cst),
10031 : 48 : CLEANUP_EXPR (last), void_node);
10032 : 48 : extra_cleanups.safe_push (cl);
10033 : 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
10034 : : integer_type_node,
10035 : : guard, cst),
10036 : : TSI_SAME_STMT);
10037 : 48 : tree sl2 = CLEANUP_BODY (last);
10038 : 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10039 : 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10040 : 48 : tsi_delink (&tsi);
10041 : : }
10042 : : }
10043 : 20734 : decomp_init = maybe_cleanup_point_expr_void (sl);
10044 : 20734 : if (cleanup)
10045 : 25 : finish_decl_cleanup (decl, cleanup);
10046 : : }
10047 : : }
10048 : :
10049 : : /* If a variable is defined, and then a subsequent
10050 : : definition with external linkage is encountered, we will
10051 : : get here twice for the same variable. We want to avoid
10052 : : calling expand_static_init more than once. For variables
10053 : : that are not static data members, we can call
10054 : : expand_static_init only when we actually process the
10055 : : initializer. It is not legal to redeclare a static data
10056 : : member, so this issue does not arise in that case. */
10057 : 21205237 : else if (var_definition_p && TREE_STATIC (decl))
10058 : : {
10059 : 19329224 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10060 : : {
10061 : 98 : tree sl = push_stmt_list ();
10062 : 98 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10063 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10064 : 98 : expand_static_init (decl, init);
10065 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p
10066 : 98 : = saved_stmts_are_full_exprs_p;
10067 : 98 : cp_finish_decomp (decl, decomp);
10068 : 98 : decomp = NULL;
10069 : 98 : sl = pop_stmt_list (sl);
10070 : 98 : sl = maybe_cleanup_point_expr_void (sl);
10071 : 98 : add_stmt (sl);
10072 : : }
10073 : : else
10074 : 19329126 : expand_static_init (decl, init);
10075 : : }
10076 : : }
10077 : :
10078 : : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10079 : : reference, insert it in the statement-tree now. */
10080 : 113997352 : if (cleanups)
10081 : : {
10082 : 44458321 : for (tree t : *cleanups)
10083 : : {
10084 : 6574 : push_cleanup (NULL_TREE, t, false);
10085 : : /* As in initialize_local_var. */
10086 : 6574 : wrap_temporary_cleanups (init, t);
10087 : : }
10088 : 44451747 : release_tree_vector (cleanups);
10089 : : }
10090 : :
10091 : 113997430 : for (tree t : &extra_cleanups)
10092 : 48 : push_cleanup (NULL_TREE, t, false);
10093 : :
10094 : 113997352 : if (decomp_init)
10095 : 20734 : add_stmt (decomp_init);
10096 : :
10097 : 113997352 : if (decomp
10098 : 141 : && var_definition_p
10099 : 141 : && TREE_STATIC (decl)
10100 : 113997493 : && !DECL_FUNCTION_SCOPE_P (decl))
10101 : : {
10102 : 141 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10103 : 141 : ? tls_aggregates : static_aggregates);
10104 : 141 : tree aggregates2 = aggregates3;
10105 : 141 : if (aggregates2 != aggregates1)
10106 : : {
10107 : 66 : cp_finish_decomp (decl, decomp);
10108 : 66 : decomp = NULL;
10109 : 66 : if (aggregates3 != aggregates2)
10110 : : {
10111 : : /* If there are dynamic initializers for the structured
10112 : : binding base or associated extended ref temps and also
10113 : : dynamic initializers for the structured binding non-base
10114 : : vars, mark them. */
10115 : 315 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10116 : 252 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10117 : 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10118 : 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
10119 : : }
10120 : : }
10121 : : }
10122 : :
10123 : 113997352 : if (was_readonly)
10124 : 238742 : TREE_READONLY (decl) = 1;
10125 : :
10126 : 113997352 : if (flag_openmp
10127 : 429835 : && VAR_P (decl)
10128 : 114186562 : && lookup_attribute ("omp declare target implicit",
10129 : 189210 : DECL_ATTRIBUTES (decl)))
10130 : : {
10131 : 52 : DECL_ATTRIBUTES (decl)
10132 : 52 : = remove_attribute ("omp declare target implicit",
10133 : 52 : DECL_ATTRIBUTES (decl));
10134 : 52 : complete_type (TREE_TYPE (decl));
10135 : 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
10136 : : {
10137 : 30 : auto_diagnostic_group d;
10138 : 30 : error ("%q+D in declare target directive does not have mappable"
10139 : : " type", decl);
10140 : 30 : if (TREE_TYPE (decl) != error_mark_node
10141 : 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10142 : 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
10143 : 30 : }
10144 : 22 : else if (!lookup_attribute ("omp declare target",
10145 : 22 : DECL_ATTRIBUTES (decl))
10146 : 44 : && !lookup_attribute ("omp declare target link",
10147 : 22 : DECL_ATTRIBUTES (decl)))
10148 : : {
10149 : 22 : DECL_ATTRIBUTES (decl)
10150 : 22 : = tree_cons (get_identifier ("omp declare target"),
10151 : 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
10152 : 22 : symtab_node *node = symtab_node::get (decl);
10153 : 22 : if (node != NULL)
10154 : : {
10155 : 19 : node->offloadable = 1;
10156 : 19 : if (ENABLE_OFFLOADING)
10157 : : {
10158 : : g->have_offload = true;
10159 : : if (is_a <varpool_node *> (node))
10160 : : vec_safe_push (offload_vars, decl);
10161 : : }
10162 : : }
10163 : : }
10164 : : }
10165 : :
10166 : : /* This is the last point we can lower alignment so give the target the
10167 : : chance to do so. */
10168 : 113997352 : if (VAR_P (decl)
10169 : 50035017 : && !is_global_var (decl)
10170 : 138066183 : && !DECL_HARD_REGISTER (decl))
10171 : 24068778 : targetm.lower_local_decl_alignment (decl);
10172 : :
10173 : 113997352 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
10174 : 291785139 : }
10175 : :
10176 : : /* For class TYPE return itself or some its bases that contain
10177 : : any direct non-static data members. Return error_mark_node if an
10178 : : error has been diagnosed. */
10179 : :
10180 : : static tree
10181 : 25179 : find_decomp_class_base (location_t loc, tree type, tree ret,
10182 : : tsubst_flags_t complain)
10183 : : {
10184 : 50358 : if (LAMBDA_TYPE_P (type))
10185 : : {
10186 : 12 : if (complain & tf_error)
10187 : : {
10188 : 12 : auto_diagnostic_group d;
10189 : 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
10190 : 12 : inform (location_of (type), "lambda declared here");
10191 : 12 : }
10192 : 12 : return error_mark_node;
10193 : : }
10194 : :
10195 : 25167 : bool member_seen = false;
10196 : 352848 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10197 : 604434 : if (TREE_CODE (field) != FIELD_DECL
10198 : 51136 : || DECL_ARTIFICIAL (field)
10199 : 378762 : || DECL_UNNAMED_BIT_FIELD (field))
10200 : 276714 : continue;
10201 : 51006 : else if (ret)
10202 : : return type;
10203 : 50980 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10204 : : {
10205 : 8 : if ((complain & tf_error) == 0)
10206 : 0 : return error_mark_node;
10207 : 8 : auto_diagnostic_group d;
10208 : 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10209 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10210 : : "anonymous struct member", type);
10211 : : else
10212 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10213 : : "anonymous union member", type);
10214 : 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
10215 : 8 : return error_mark_node;
10216 : 8 : }
10217 : 50972 : else if (!accessible_p (type, field, true))
10218 : : {
10219 : 5 : if ((complain & tf_error) == 0)
10220 : 0 : return error_mark_node;
10221 : 5 : auto_diagnostic_group d;
10222 : 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10223 : : field, type);
10224 : 5 : inform (DECL_SOURCE_LOCATION (field),
10225 : 5 : TREE_PRIVATE (field)
10226 : : ? G_("declared private here")
10227 : : : G_("declared protected here"));
10228 : 5 : return error_mark_node;
10229 : 5 : }
10230 : : else
10231 : : member_seen = true;
10232 : :
10233 : 25128 : tree base_binfo, binfo;
10234 : 25128 : tree orig_ret = ret;
10235 : 25128 : int i;
10236 : 25128 : if (member_seen)
10237 : 25038 : ret = type;
10238 : 25186 : for (binfo = TYPE_BINFO (type), i = 0;
10239 : 25186 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10240 : : {
10241 : 78 : auto_diagnostic_group d;
10242 : 78 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10243 : : complain);
10244 : 78 : if (t == error_mark_node)
10245 : : {
10246 : 3 : if (complain & tf_error)
10247 : 3 : inform (location_of (type), "in base class of %qT", type);
10248 : 3 : return error_mark_node;
10249 : : }
10250 : 75 : if (t != NULL_TREE && t != ret)
10251 : : {
10252 : 48 : if (ret == type)
10253 : : {
10254 : 10 : if (complain & tf_error)
10255 : 10 : error_at (loc, "cannot decompose class type %qT: both it and "
10256 : : "its base class %qT have non-static data "
10257 : : "members", type, t);
10258 : 10 : return error_mark_node;
10259 : : }
10260 : 38 : else if (orig_ret != NULL_TREE)
10261 : : return t;
10262 : 38 : else if (ret != NULL_TREE)
10263 : : {
10264 : 7 : if (complain & tf_error)
10265 : 7 : error_at (loc, "cannot decompose class type %qT: its base "
10266 : : "classes %qT and %qT have non-static data "
10267 : : "members", type, ret, t);
10268 : 7 : return error_mark_node;
10269 : : }
10270 : : else
10271 : : ret = t;
10272 : : }
10273 : 78 : }
10274 : : return ret;
10275 : : }
10276 : :
10277 : : /* Return std::tuple_size<TYPE>::value. */
10278 : :
10279 : : static tree
10280 : 70139 : get_tuple_size (tree type)
10281 : : {
10282 : 70139 : tree args = make_tree_vec (1);
10283 : 70139 : TREE_VEC_ELT (args, 0) = type;
10284 : 70139 : tree inst = lookup_template_class (tuple_size_identifier, args,
10285 : : /*in_decl*/NULL_TREE,
10286 : : /*context*/std_node,
10287 : : tf_none);
10288 : 70139 : inst = complete_type (inst);
10289 : 70139 : if (inst == error_mark_node
10290 : 68720 : || !COMPLETE_TYPE_P (inst)
10291 : 115135 : || !CLASS_TYPE_P (type))
10292 : : return NULL_TREE;
10293 : 44996 : tree val = lookup_qualified_name (inst, value_identifier,
10294 : : LOOK_want::NORMAL, /*complain*/false);
10295 : 44996 : if (val == error_mark_node)
10296 : : return NULL_TREE;
10297 : 44990 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10298 : 44990 : val = maybe_constant_value (val, NULL_TREE, mce_true);
10299 : 44990 : if (TREE_CODE (val) == INTEGER_CST)
10300 : : return val;
10301 : : else
10302 : 6 : return error_mark_node;
10303 : : }
10304 : :
10305 : : /* Return std::tuple_element<I,TYPE>::type. */
10306 : :
10307 : : static tree
10308 : 48633 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10309 : : {
10310 : 48633 : tree args = make_tree_vec (2);
10311 : 48633 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10312 : 48633 : TREE_VEC_ELT (args, 1) = type;
10313 : 48633 : tree inst = lookup_template_class (tuple_element_identifier, args,
10314 : : /*in_decl*/NULL_TREE,
10315 : : /*context*/std_node,
10316 : : tf_warning_or_error);
10317 : 48633 : return make_typename_type (inst, type_identifier,
10318 : 48633 : none_type, tf_warning_or_error);
10319 : : }
10320 : :
10321 : : /* Return e.get<i>() or get<i>(e). */
10322 : :
10323 : : static tree
10324 : 48642 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10325 : : {
10326 : 48642 : tree targs = make_tree_vec (1);
10327 : 48642 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10328 : :
10329 : 48642 : tree etype = TREE_TYPE (decl);
10330 : 48642 : tree e = convert_from_reference (decl);
10331 : :
10332 : : /* [The id-expression] e is an lvalue if the type of the entity e is an
10333 : : lvalue reference and an xvalue otherwise. */
10334 : 48642 : if (!TYPE_REF_P (etype)
10335 : 48642 : || TYPE_REF_IS_RVALUE (etype))
10336 : 47352 : e = move (e);
10337 : :
10338 : 48642 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10339 : : LOOK_want::NORMAL, /*complain*/false);
10340 : 48642 : bool use_member_get = false;
10341 : :
10342 : : /* To use a member get, member lookup must find at least one
10343 : : declaration that is a function template
10344 : : whose first template parameter is a non-type parameter. */
10345 : 143489 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10346 : : {
10347 : 48642 : tree fn = *iter;
10348 : 48642 : if (TREE_CODE (fn) == TEMPLATE_DECL)
10349 : : {
10350 : 2440 : tree tparms = DECL_TEMPLATE_PARMS (fn);
10351 : 2440 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10352 : 2440 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10353 : : {
10354 : : use_member_get = true;
10355 : : break;
10356 : : }
10357 : : }
10358 : : }
10359 : :
10360 : 48642 : if (use_member_get)
10361 : : {
10362 : 2437 : fns = lookup_template_function (fns, targs);
10363 : 2437 : return build_new_method_call (e, fns, /*args*/NULL,
10364 : : /*path*/NULL_TREE, LOOKUP_NORMAL,
10365 : 2437 : /*fn_p*/NULL, tf_warning_or_error);
10366 : : }
10367 : : else
10368 : : {
10369 : 46205 : releasing_vec args (make_tree_vector_single (e));
10370 : 46205 : fns = lookup_template_function (get__identifier, targs);
10371 : 46205 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10372 : 46205 : return finish_call_expr (fns, &args, /*novirt*/false,
10373 : : /*koenig*/true, tf_warning_or_error);
10374 : 46205 : }
10375 : : }
10376 : :
10377 : : /* It's impossible to recover the decltype of a tuple decomposition variable
10378 : : based on the actual type of the variable, so store it in a hash table. */
10379 : :
10380 : : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10381 : :
10382 : : tree
10383 : 578 : lookup_decomp_type (tree v)
10384 : : {
10385 : 578 : if (decomp_type_table)
10386 : 451 : if (tree *slot = decomp_type_table->get (v))
10387 : 387 : return *slot;
10388 : : return NULL_TREE;
10389 : : }
10390 : :
10391 : : /* Mangle a decomposition declaration if needed. Arguments like
10392 : : in cp_finish_decomp. */
10393 : :
10394 : : static void
10395 : 41135 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10396 : : {
10397 : 41135 : if (!processing_template_decl
10398 : 41135 : && !error_operand_p (decl)
10399 : 82270 : && TREE_STATIC (decl))
10400 : : {
10401 : 605 : auto_vec<tree, 16> v;
10402 : 605 : v.safe_grow (decomp->count, true);
10403 : 605 : tree d = decomp->decl;
10404 : 2196 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10405 : 1591 : v[decomp->count - i - 1] = d;
10406 : 605 : if (DECL_FUNCTION_SCOPE_P (decl))
10407 : : {
10408 : : size_t sz = 3;
10409 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
10410 : 650 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10411 : 259 : char *name = XALLOCAVEC (char, sz);
10412 : 259 : name[0] = 'D';
10413 : 259 : name[1] = 'C';
10414 : 259 : char *p = name + 2;
10415 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
10416 : : {
10417 : 650 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10418 : 650 : *p++ = ' ';
10419 : 650 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
10420 : 650 : p += len;
10421 : : }
10422 : 259 : *p = '\0';
10423 : 259 : determine_local_discriminator (decl, get_identifier (name));
10424 : : }
10425 : 605 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10426 : 605 : maybe_apply_pragma_weak (decl);
10427 : 605 : }
10428 : 41135 : }
10429 : :
10430 : : /* Append #i to DECL_NAME (decl) or for name independent decls
10431 : : clear DECL_NAME (decl). */
10432 : :
10433 : : static void
10434 : 2031 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10435 : : {
10436 : 2031 : if (name_independent_decl_p (decl))
10437 : : /* Only "_" names are treated as name independent, "_#0" etc. is not and
10438 : : because we pushdecl the individual decl elements of structured binding
10439 : : pack, we could get redeclaration errors if there are 2 or more name
10440 : : independent structured binding packs in the same scope. */
10441 : 312 : DECL_NAME (decl) = NULL_TREE;
10442 : : else
10443 : : {
10444 : 1719 : tree name = DECL_NAME (decl);
10445 : 1719 : size_t len = IDENTIFIER_LENGTH (name) + 22;
10446 : 1719 : char *n = XALLOCAVEC (char, len);
10447 : 1719 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10448 : 1719 : IDENTIFIER_POINTER (name), i);
10449 : 1719 : DECL_NAME (decl) = get_identifier (n);
10450 : : }
10451 : 2031 : }
10452 : :
10453 : : /* Return structured binding size of TYPE or -1 if erroneous. */
10454 : :
10455 : : HOST_WIDE_INT
10456 : 460 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10457 : : {
10458 : 460 : if (TYPE_REF_P (type))
10459 : : {
10460 : 0 : type = complete_type (TREE_TYPE (type));
10461 : 0 : if (type == error_mark_node)
10462 : : return -1;
10463 : 0 : if (!COMPLETE_TYPE_P (type))
10464 : : {
10465 : 0 : if (complain & tf_error)
10466 : 0 : error_at (loc, "structured binding refers to incomplete type %qT",
10467 : : type);
10468 : 0 : return -1;
10469 : : }
10470 : : }
10471 : :
10472 : 460 : unsigned HOST_WIDE_INT eltscnt = 0;
10473 : 460 : if (TREE_CODE (type) == ARRAY_TYPE)
10474 : : {
10475 : 258 : if (TYPE_DOMAIN (type) == NULL_TREE)
10476 : : {
10477 : 9 : if (complain & tf_error)
10478 : 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
10479 : : type);
10480 : 9 : return -1;
10481 : : }
10482 : 249 : tree nelts = array_type_nelts_top (type);
10483 : 249 : if (nelts == error_mark_node)
10484 : : return -1;
10485 : 249 : if (!tree_fits_shwi_p (nelts))
10486 : : {
10487 : 3 : if (complain & tf_error)
10488 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10489 : 3 : return -1;
10490 : : }
10491 : 246 : return tree_to_shwi (nelts);
10492 : : }
10493 : : /* 2 GNU extensions. */
10494 : 202 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10495 : : return 2;
10496 : 196 : else if (TREE_CODE (type) == VECTOR_TYPE)
10497 : : {
10498 : 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10499 : : {
10500 : : if (complain & tf_error)
10501 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10502 : : return -1;
10503 : : }
10504 : 12 : return eltscnt;
10505 : : }
10506 : 184 : else if (tree tsize = get_tuple_size (type))
10507 : : {
10508 : 28 : if (tsize == error_mark_node
10509 : 22 : || !tree_fits_shwi_p (tsize)
10510 : 50 : || tree_int_cst_sgn (tsize) < 0)
10511 : : {
10512 : 9 : if (complain & tf_error)
10513 : 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10514 : : "constant expression", type);
10515 : 9 : return -1;
10516 : : }
10517 : 19 : return tree_to_shwi (tsize);
10518 : : }
10519 : 156 : else if (TREE_CODE (type) == UNION_TYPE)
10520 : : {
10521 : 3 : if (complain & tf_error)
10522 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10523 : 3 : return -1;
10524 : : }
10525 : 153 : else if (!CLASS_TYPE_P (type))
10526 : : {
10527 : 21 : if (complain & tf_error)
10528 : 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10529 : 21 : return -1;
10530 : : }
10531 : 132 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10532 : : return -1;
10533 : 132 : else if (!COMPLETE_TYPE_P (type))
10534 : : {
10535 : 3 : if (complain & tf_error)
10536 : 3 : error_at (loc, "structured binding refers to incomplete class type "
10537 : : "%qT", type);
10538 : 3 : return -1;
10539 : : }
10540 : : else
10541 : : {
10542 : 129 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10543 : 129 : if (btype == error_mark_node)
10544 : : return -1;
10545 : 108 : else if (btype == NULL_TREE)
10546 : : return 0;
10547 : 1011 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10548 : 1533 : if (TREE_CODE (field) != FIELD_DECL
10549 : 327 : || DECL_ARTIFICIAL (field)
10550 : 1251 : || DECL_UNNAMED_BIT_FIELD (field))
10551 : 609 : continue;
10552 : : else
10553 : 315 : eltscnt++;
10554 : 87 : return eltscnt;
10555 : : }
10556 : : }
10557 : :
10558 : : /* Finish a decomposition declaration. DECL is the underlying declaration
10559 : : "e", FIRST is the head of a chain of decls for the individual identifiers
10560 : : chained through DECL_CHAIN in reverse order and COUNT is the number of
10561 : : those decls. If TEST_P is true, return true if any code would need to be
10562 : : actually emitted but don't emit it. Return false otherwise. */
10563 : :
10564 : : bool
10565 : 154875 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10566 : : {
10567 : 154875 : tree first = decomp->decl;
10568 : 154875 : unsigned count = decomp->count;
10569 : 154875 : if (error_operand_p (decl))
10570 : : {
10571 : 57 : error_out:
10572 : 586 : while (count--)
10573 : : {
10574 : 402 : TREE_TYPE (first) = error_mark_node;
10575 : 402 : if (DECL_HAS_VALUE_EXPR_P (first))
10576 : : {
10577 : 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10578 : 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10579 : : }
10580 : 402 : first = DECL_CHAIN (first);
10581 : : }
10582 : 184 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
10583 : 51 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10584 : 184 : return false;
10585 : : }
10586 : :
10587 : 154818 : location_t loc = DECL_SOURCE_LOCATION (decl);
10588 : 154818 : if (type_dependent_expression_p (decl)
10589 : : /* This happens for range for when not in templates.
10590 : : Still add the DECL_VALUE_EXPRs for later processing. */
10591 : 154818 : || (!processing_template_decl
10592 : 62383 : && type_uses_auto (TREE_TYPE (decl))))
10593 : : {
10594 : 251862 : for (unsigned int i = 0; i < count; i++)
10595 : : {
10596 : 167940 : if (!DECL_HAS_VALUE_EXPR_P (first))
10597 : : {
10598 : 167940 : tree v = build_nt (ARRAY_REF, decl,
10599 : 167940 : size_int (count - i - 1),
10600 : : NULL_TREE, NULL_TREE);
10601 : 167940 : SET_DECL_VALUE_EXPR (first, v);
10602 : 167940 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10603 : : }
10604 : 167940 : if (processing_template_decl)
10605 : 167320 : fit_decomposition_lang_decl (first, decl);
10606 : 167940 : first = DECL_CHAIN (first);
10607 : : }
10608 : : return false;
10609 : : }
10610 : :
10611 : 70896 : auto_vec<tree, 16> v;
10612 : 70896 : v.safe_grow (count, true);
10613 : 70896 : tree d = first;
10614 : 70896 : int pack = -1;
10615 : 214551 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10616 : : {
10617 : 143655 : v[count - i - 1] = d;
10618 : 143655 : fit_decomposition_lang_decl (d, decl);
10619 : 143655 : if (DECL_PACK_P (d))
10620 : 1008 : pack = count - i - 1;
10621 : : }
10622 : :
10623 : 70896 : tree type = TREE_TYPE (decl);
10624 : 70896 : tree dexp = decl;
10625 : :
10626 : 70896 : if (TYPE_REF_P (type))
10627 : : {
10628 : 1761 : dexp = convert_from_reference (dexp);
10629 : 1761 : type = complete_type (TREE_TYPE (type));
10630 : 1761 : if (type == error_mark_node)
10631 : 127 : goto error_out;
10632 : 1761 : if (!COMPLETE_TYPE_P (type))
10633 : : {
10634 : 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10635 : : type);
10636 : 3 : goto error_out;
10637 : : }
10638 : : }
10639 : :
10640 : 70893 : tree eltype = NULL_TREE;
10641 : 70893 : unsigned HOST_WIDE_INT eltscnt = 0;
10642 : : /* Structured binding packs when initializer is non-dependent should
10643 : : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10644 : : of that TREE_VEC are the base and index, what is normally represented
10645 : : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10646 : : of the pack first element. The remaining elements of the TREE_VEC
10647 : : are VAR_DECLs for the pack elements. */
10648 : 70893 : tree packv = NULL_TREE;
10649 : :
10650 : 70893 : if (TREE_CODE (type) == ARRAY_TYPE)
10651 : : {
10652 : 800 : tree nelts;
10653 : 800 : nelts = array_type_nelts_top (type);
10654 : 800 : if (nelts == error_mark_node)
10655 : 0 : goto error_out;
10656 : 800 : if (DECL_DECOMP_BASE (decl))
10657 : : {
10658 : 9 : error_at (loc, "array initializer for structured binding "
10659 : : "declaration in condition");
10660 : 9 : goto error_out;
10661 : : }
10662 : 791 : if (!tree_fits_uhwi_p (nelts))
10663 : : {
10664 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10665 : 3 : goto error_out;
10666 : : }
10667 : 788 : eltscnt = tree_to_uhwi (nelts);
10668 : 788 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10669 : : {
10670 : 11 : cnt_mismatch:
10671 : 38 : auto_diagnostic_group d;
10672 : 38 : if (count > eltscnt)
10673 : 27 : error_n (loc, count,
10674 : : "%u name provided for structured binding",
10675 : : "%u names provided for structured binding", count);
10676 : : else
10677 : 11 : error_n (loc, count,
10678 : : "only %u name provided for structured binding",
10679 : : "only %u names provided for structured binding", count);
10680 : 38 : inform_n (loc, eltscnt,
10681 : : "while %qT decomposes into %wu element",
10682 : : "while %qT decomposes into %wu elements",
10683 : : type, eltscnt);
10684 : 38 : goto error_out;
10685 : : }
10686 : 777 : eltype = TREE_TYPE (type);
10687 : 2796 : for (unsigned int i = 0; i < count; i++)
10688 : : {
10689 : 2019 : if ((unsigned) pack == i)
10690 : : {
10691 : 155 : packv = make_tree_vec (eltscnt - count + 3);
10692 : 413 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10693 : : {
10694 : 258 : tree t;
10695 : 258 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10696 : 258 : set_sb_pack_name (t, j);
10697 : 258 : maybe_push_decl (t);
10698 : 258 : TREE_TYPE (t) = eltype;
10699 : 258 : layout_decl (t, 0);
10700 : 258 : if (!processing_template_decl)
10701 : : {
10702 : 182 : tree a = unshare_expr (dexp);
10703 : 182 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10704 : : NULL_TREE, NULL_TREE);
10705 : 182 : SET_DECL_VALUE_EXPR (t, a);
10706 : 182 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10707 : : }
10708 : : }
10709 : 2019 : continue;
10710 : 155 : }
10711 : 1864 : TREE_TYPE (v[i]) = eltype;
10712 : 1864 : layout_decl (v[i], 0);
10713 : 1864 : if (processing_template_decl)
10714 : 132 : continue;
10715 : 1732 : tree t = unshare_expr (dexp);
10716 : 1732 : unsigned HOST_WIDE_INT j = i;
10717 : 1732 : if (pack != -1 && (unsigned) pack < i)
10718 : 64 : j = i + eltscnt - count;
10719 : 1732 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10720 : 1732 : SET_DECL_VALUE_EXPR (v[i], t);
10721 : 1732 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10722 : : }
10723 : : }
10724 : : /* 2 GNU extensions. */
10725 : 70093 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10726 : : {
10727 : 78 : eltscnt = 2;
10728 : 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10729 : 3 : goto cnt_mismatch;
10730 : 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10731 : 213 : for (unsigned int i = 0; i < count; i++)
10732 : : {
10733 : 138 : if ((unsigned) pack == i)
10734 : : {
10735 : 60 : packv = make_tree_vec (eltscnt - count + 3);
10736 : 132 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10737 : : {
10738 : 72 : tree t;
10739 : 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10740 : 72 : set_sb_pack_name (t, j);
10741 : 72 : maybe_push_decl (t);
10742 : 72 : TREE_TYPE (t) = eltype;
10743 : 72 : layout_decl (t, 0);
10744 : 72 : if (!processing_template_decl)
10745 : : {
10746 : 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
10747 : : unshare_expr (dexp));
10748 : 48 : SET_DECL_VALUE_EXPR (t, a);
10749 : 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10750 : : }
10751 : : }
10752 : 60 : continue;
10753 : 60 : }
10754 : 78 : TREE_TYPE (v[i]) = eltype;
10755 : 78 : layout_decl (v[i], 0);
10756 : 78 : if (processing_template_decl)
10757 : 16 : continue;
10758 : 62 : tree t = unshare_expr (dexp);
10759 : 62 : unsigned HOST_WIDE_INT j = i;
10760 : 62 : if (pack != -1 && (unsigned) pack < i)
10761 : 16 : j = i + eltscnt - count;
10762 : 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
10763 : 62 : SET_DECL_VALUE_EXPR (v[i], t);
10764 : 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10765 : : }
10766 : : }
10767 : 70015 : else if (TREE_CODE (type) == VECTOR_TYPE)
10768 : : {
10769 : 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10770 : : {
10771 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10772 : : goto error_out;
10773 : : }
10774 : 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10775 : 3 : goto cnt_mismatch;
10776 : 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10777 : 249 : for (unsigned int i = 0; i < count; i++)
10778 : : {
10779 : 192 : if ((unsigned) pack == i)
10780 : : {
10781 : 48 : packv = make_tree_vec (eltscnt - count + 3);
10782 : 708 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10783 : : {
10784 : 660 : tree t;
10785 : 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10786 : 660 : set_sb_pack_name (t, j);
10787 : 660 : maybe_push_decl (t);
10788 : 660 : TREE_TYPE (t) = eltype;
10789 : 660 : layout_decl (t, 0);
10790 : 660 : if (!processing_template_decl)
10791 : : {
10792 : 440 : tree a = unshare_expr (dexp);
10793 : 440 : location_t loc = DECL_SOURCE_LOCATION (t);
10794 : 440 : tree s = size_int (j + pack);
10795 : 440 : convert_vector_to_array_for_subscript (loc, &a, s);
10796 : 440 : a = build4 (ARRAY_REF, eltype, a, s,
10797 : : NULL_TREE, NULL_TREE);
10798 : 440 : SET_DECL_VALUE_EXPR (t, a);
10799 : 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10800 : : }
10801 : : }
10802 : 84 : continue;
10803 : 48 : }
10804 : 144 : TREE_TYPE (v[i]) = eltype;
10805 : 144 : layout_decl (v[i], 0);
10806 : 144 : if (processing_template_decl)
10807 : 36 : continue;
10808 : 108 : tree t = unshare_expr (dexp);
10809 : 108 : unsigned HOST_WIDE_INT j = i;
10810 : 108 : if (pack != -1 && (unsigned) pack < i)
10811 : 24 : j = i + eltscnt - count;
10812 : 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10813 : 108 : &t, size_int (j));
10814 : 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10815 : 108 : SET_DECL_VALUE_EXPR (v[i], t);
10816 : 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10817 : : }
10818 : : }
10819 : 69955 : else if (tree tsize = get_tuple_size (type))
10820 : : {
10821 : 44962 : if (tsize == error_mark_node)
10822 : : {
10823 : 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10824 : : "constant expression", type);
10825 : 0 : goto error_out;
10826 : : }
10827 : 44962 : if (!tree_fits_uhwi_p (tsize))
10828 : : {
10829 : 12 : auto_diagnostic_group d;
10830 : 12 : error_n (loc, count,
10831 : : "%u name provided for structured binding",
10832 : : "%u names provided for structured binding", count);
10833 : 12 : inform (loc, "while %qT decomposes into %E elements",
10834 : : type, tsize);
10835 : 12 : goto error_out;
10836 : 12 : }
10837 : 44950 : eltscnt = tree_to_uhwi (tsize);
10838 : 44950 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10839 : 6 : goto cnt_mismatch;
10840 : 44944 : if (test_p && eltscnt)
10841 : 70769 : return true;
10842 : 23971 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
10843 : : {
10844 : : /* For structured bindings used in conditions we need to evaluate
10845 : : the conversion of decl (aka e in the standard) to bool or
10846 : : integral/enumeral type (the latter for switch conditions)
10847 : : before the get methods. */
10848 : 86 : tree cond = convert_from_reference (decl);
10849 : 86 : if (integer_onep (DECL_DECOMP_BASE (decl)))
10850 : : /* switch condition. */
10851 : 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10852 : : cond, true);
10853 : : else
10854 : : /* if/while/for condition. */
10855 : 68 : cond = contextual_conv_bool (cond, tf_warning_or_error);
10856 : 86 : if (cond && !error_operand_p (cond))
10857 : : {
10858 : : /* Wrap that value into a TARGET_EXPR, emit it right
10859 : : away and save for later uses in the cp_parse_condition
10860 : : or its instantiation. */
10861 : 86 : cond = get_internal_target_expr (cond);
10862 : 86 : add_stmt (cond);
10863 : 86 : DECL_DECOMP_BASE (decl) = cond;
10864 : : }
10865 : : }
10866 : 23971 : int save_read = DECL_READ_P (decl);
10867 : 72312 : for (unsigned i = 0; i < count; ++i)
10868 : : {
10869 : 48362 : location_t sloc = input_location;
10870 : 48362 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10871 : :
10872 : 48362 : if ((unsigned) pack == i)
10873 : : {
10874 : 276 : packv = make_tree_vec (eltscnt - count + 3);
10875 : 832 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10876 : : {
10877 : 556 : tree t;
10878 : 556 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10879 : 556 : set_sb_pack_name (t, j);
10880 : 556 : input_location = dloc;
10881 : 556 : tree init = get_tuple_decomp_init (decl, j + pack);
10882 : 556 : tree eltype = (init == error_mark_node ? error_mark_node
10883 : 556 : : get_tuple_element_type (type, j + pack));
10884 : 556 : input_location = sloc;
10885 : :
10886 : 556 : if (VOID_TYPE_P (eltype))
10887 : : {
10888 : 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
10889 : : "%<void%>", j + pack, type);
10890 : 0 : eltype = error_mark_node;
10891 : : }
10892 : 556 : if (init == error_mark_node || eltype == error_mark_node)
10893 : : {
10894 : 0 : inform (dloc, "in initialization of structured binding "
10895 : 0 : "pack %qD", v[pack]);
10896 : 0 : goto error_out;
10897 : : }
10898 : 556 : maybe_push_decl (t);
10899 : : /* Save the decltype away before reference collapse. */
10900 : 556 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
10901 : 556 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10902 : 556 : TREE_TYPE (t) = eltype;
10903 : 556 : layout_decl (t, 0);
10904 : 556 : DECL_HAS_VALUE_EXPR_P (t) = 0;
10905 : 556 : if (!processing_template_decl)
10906 : : {
10907 : 456 : copy_linkage (t, decl);
10908 : 456 : tree name = DECL_NAME (t);
10909 : 456 : if (TREE_STATIC (decl))
10910 : 36 : DECL_NAME (t) = DECL_NAME (v[pack]);
10911 : 456 : cp_finish_decl (t, init, /*constexpr*/false,
10912 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10913 : 456 : if (TREE_STATIC (decl))
10914 : : {
10915 : 36 : DECL_ASSEMBLER_NAME (t);
10916 : 36 : DECL_NAME (t) = name;
10917 : : }
10918 : : }
10919 : : }
10920 : 276 : continue;
10921 : 276 : }
10922 : :
10923 : 48086 : unsigned HOST_WIDE_INT j = i;
10924 : 48086 : if (pack != -1 && (unsigned) pack < i)
10925 : 238 : j = i + eltscnt - count;
10926 : 48086 : input_location = dloc;
10927 : 48086 : tree init = get_tuple_decomp_init (decl, j);
10928 : 48086 : tree eltype = (init == error_mark_node ? error_mark_node
10929 : 48077 : : get_tuple_element_type (type, j));
10930 : 48086 : input_location = sloc;
10931 : :
10932 : 48086 : if (VOID_TYPE_P (eltype))
10933 : : {
10934 : 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
10935 : : j, type);
10936 : 3 : eltype = error_mark_node;
10937 : : }
10938 : 48086 : if (init == error_mark_node || eltype == error_mark_node)
10939 : : {
10940 : 21 : inform (dloc, "in initialization of structured binding "
10941 : 21 : "variable %qD", v[i]);
10942 : 21 : goto error_out;
10943 : : }
10944 : : /* Save the decltype away before reference collapse. */
10945 : 48065 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
10946 : 48065 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10947 : 48065 : TREE_TYPE (v[i]) = eltype;
10948 : 48065 : layout_decl (v[i], 0);
10949 : 48065 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
10950 : : {
10951 : : /* In this case the names are variables, not just proxies. */
10952 : 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10953 : 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10954 : : }
10955 : 48065 : if (!processing_template_decl)
10956 : : {
10957 : 42045 : copy_linkage (v[i], decl);
10958 : 42045 : cp_finish_decl (v[i], init, /*constexpr*/false,
10959 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10960 : : }
10961 : : }
10962 : : /* Ignore reads from the underlying decl performed during initialization
10963 : : of the individual variables. If those will be read, we'll mark
10964 : : the underlying decl as read at that point. */
10965 : 23950 : DECL_READ_P (decl) = save_read;
10966 : : }
10967 : 24993 : else if (TREE_CODE (type) == UNION_TYPE)
10968 : : {
10969 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10970 : 3 : goto error_out;
10971 : : }
10972 : 24990 : else if (!CLASS_TYPE_P (type))
10973 : : {
10974 : 16 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10975 : 16 : goto error_out;
10976 : : }
10977 : 24974 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10978 : 0 : goto error_out;
10979 : 24974 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10980 : 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10981 : : type);
10982 : : else
10983 : : {
10984 : 24972 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
10985 : : tf_warning_or_error);
10986 : 24972 : if (btype == error_mark_node)
10987 : 21 : goto error_out;
10988 : 24951 : else if (btype == NULL_TREE)
10989 : : {
10990 : 17 : if (pack == 0 && count == 1)
10991 : : {
10992 : 16 : eltscnt = 0;
10993 : 16 : packv = make_tree_vec (2);
10994 : 16 : goto done;
10995 : : }
10996 : 1 : error_at (loc, "cannot decompose class type %qT without non-static "
10997 : : "data members", type);
10998 : 1 : goto error_out;
10999 : : }
11000 : 350300 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11001 : 600100 : if (TREE_CODE (field) != FIELD_DECL
11002 : 50648 : || DECL_ARTIFICIAL (field)
11003 : 376010 : || DECL_UNNAMED_BIT_FIELD (field))
11004 : 274734 : continue;
11005 : : else
11006 : 50632 : eltscnt++;
11007 : 24934 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
11008 : 15 : goto cnt_mismatch;
11009 : 24919 : tree t = dexp;
11010 : 24919 : if (type != btype)
11011 : : {
11012 : 18 : t = convert_to_base (t, btype, /*check_access*/true,
11013 : : /*nonnull*/false, tf_warning_or_error);
11014 : 18 : type = btype;
11015 : : }
11016 : 24919 : unsigned HOST_WIDE_INT j = 0;
11017 : 24919 : unsigned int i = 0;
11018 : 350089 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
11019 : 599745 : if (TREE_CODE (field) != FIELD_DECL
11020 : 50611 : || DECL_ARTIFICIAL (field)
11021 : 375777 : || DECL_UNNAMED_BIT_FIELD (field))
11022 : 274575 : continue;
11023 : : else
11024 : : {
11025 : 50595 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
11026 : : NULL_TREE);
11027 : 50595 : if (REFERENCE_REF_P (tt))
11028 : 30 : tt = TREE_OPERAND (tt, 0);
11029 : 50595 : if (pack != -1 && j >= (unsigned) pack)
11030 : : {
11031 : 666 : if (j == (unsigned) pack)
11032 : : {
11033 : 220 : packv = make_tree_vec (eltscnt - count + 3);
11034 : 220 : i++;
11035 : : }
11036 : 666 : if (j < (unsigned) pack + eltscnt - (count - 1))
11037 : : {
11038 : 485 : tree t;
11039 : 485 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11040 : 485 : set_sb_pack_name (t, j + 1 - i);
11041 : 485 : maybe_push_decl (t);
11042 : 485 : TREE_TYPE (t) = TREE_TYPE (tt);
11043 : 485 : layout_decl (t, 0);
11044 : 485 : if (!processing_template_decl)
11045 : : {
11046 : 364 : SET_DECL_VALUE_EXPR (t, tt);
11047 : 364 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11048 : : }
11049 : : else
11050 : 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11051 : 485 : j++;
11052 : 485 : continue;
11053 : 485 : }
11054 : : }
11055 : 50110 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
11056 : 50110 : layout_decl (v[i], 0);
11057 : 50110 : if (!processing_template_decl)
11058 : : {
11059 : 38849 : SET_DECL_VALUE_EXPR (v[i], tt);
11060 : 38849 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11061 : : }
11062 : 50110 : i++;
11063 : 50110 : j++;
11064 : : }
11065 : 24919 : if (pack != -1 && j == (unsigned) pack)
11066 : : {
11067 : 20 : gcc_checking_assert (eltscnt == count - 1);
11068 : 20 : packv = make_tree_vec (2);
11069 : : }
11070 : : }
11071 : 49796 : done:
11072 : 49796 : if (packv)
11073 : : {
11074 : 795 : gcc_checking_assert (pack != -1);
11075 : 795 : TREE_VEC_ELT (packv, 0) = decl;
11076 : 795 : TREE_VEC_ELT (packv, 1) = size_int (pack);
11077 : 795 : SET_DECL_VALUE_EXPR (v[pack], packv);
11078 : 795 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11079 : 795 : DECL_IGNORED_P (v[pack]) = 1;
11080 : 795 : if (!processing_template_decl)
11081 : 2047 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11082 : 1490 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
11083 : : }
11084 : 49796 : if (processing_template_decl)
11085 : : {
11086 : 26456 : for (unsigned int i = 0; i < count; i++)
11087 : 17705 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11088 : : {
11089 : 17311 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
11090 : : NULL_TREE, NULL_TREE);
11091 : 17311 : SET_DECL_VALUE_EXPR (v[i], a);
11092 : 17311 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11093 : : }
11094 : : }
11095 : : return false;
11096 : : }
11097 : :
11098 : : /* Returns a declaration for a VAR_DECL as if:
11099 : :
11100 : : extern "C" TYPE NAME;
11101 : :
11102 : : had been seen. Used to create compiler-generated global
11103 : : variables. */
11104 : :
11105 : : static tree
11106 : 2165 : declare_global_var (tree name, tree type)
11107 : : {
11108 : 2165 : auto cookie = push_abi_namespace (global_namespace);
11109 : 2165 : tree decl = build_decl (input_location, VAR_DECL, name, type);
11110 : 2165 : TREE_PUBLIC (decl) = 1;
11111 : 2165 : DECL_EXTERNAL (decl) = 1;
11112 : 2165 : DECL_ARTIFICIAL (decl) = 1;
11113 : 2165 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11114 : : /* If the user has explicitly declared this variable (perhaps
11115 : : because the code we are compiling is part of a low-level runtime
11116 : : library), then it is possible that our declaration will be merged
11117 : : with theirs by pushdecl. */
11118 : 2165 : decl = pushdecl (decl);
11119 : 2165 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
11120 : 2165 : pop_abi_namespace (cookie, global_namespace);
11121 : :
11122 : 2165 : return decl;
11123 : : }
11124 : :
11125 : : /* Returns the type for the argument to "atexit" corresponding to the function
11126 : : to be called when the program exits. */
11127 : :
11128 : : static tree
11129 : 24 : get_atexit_fn_ptr_type ()
11130 : : {
11131 : 24 : if (!atexit_fn_ptr_type_node)
11132 : : {
11133 : 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11134 : 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11135 : : }
11136 : :
11137 : 24 : return atexit_fn_ptr_type_node;
11138 : : }
11139 : :
11140 : : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11141 : : or "__cxa_throw" corresponding to the destructor to be called when the
11142 : : program exits. */
11143 : :
11144 : : tree
11145 : 18876 : get_cxa_atexit_fn_ptr_type ()
11146 : : {
11147 : 18876 : if (!cleanup_type)
11148 : : {
11149 : 14227 : tree fntype = build_function_type_list (void_type_node,
11150 : : ptr_type_node, NULL_TREE);
11151 : 14227 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11152 : 14227 : cleanup_type = build_pointer_type (fntype);
11153 : : }
11154 : :
11155 : 18876 : return cleanup_type;
11156 : : }
11157 : :
11158 : : /* Returns a pointer to the `atexit' function. Note that if
11159 : : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11160 : : `__cxa_atexit' function specified in the IA64 C++ ABI. */
11161 : :
11162 : : static tree
11163 : 3428 : get_atexit_node (void)
11164 : : {
11165 : 3428 : tree atexit_fndecl;
11166 : 3428 : tree fn_type;
11167 : 3428 : tree fn_ptr_type;
11168 : 3428 : const char *name;
11169 : 3428 : bool use_aeabi_atexit;
11170 : 3428 : tree ctx = global_namespace;
11171 : :
11172 : 3428 : if (atexit_node)
11173 : : return atexit_node;
11174 : :
11175 : 2131 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11176 : : {
11177 : : /* The declaration for `__cxa_atexit' is:
11178 : :
11179 : : int __cxa_atexit (void (*)(void *), void *, void *)
11180 : :
11181 : : We build up the argument types and then the function type
11182 : : itself. */
11183 : 2119 : tree argtype0, argtype1, argtype2;
11184 : :
11185 : 2119 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11186 : : /* First, build the pointer-to-function type for the first
11187 : : argument. */
11188 : 2119 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11189 : : /* Then, build the rest of the argument types. */
11190 : 2119 : argtype2 = ptr_type_node;
11191 : 2119 : if (use_aeabi_atexit)
11192 : : {
11193 : : argtype1 = fn_ptr_type;
11194 : : argtype0 = ptr_type_node;
11195 : : }
11196 : : else
11197 : : {
11198 : 2119 : argtype1 = ptr_type_node;
11199 : 2119 : argtype0 = fn_ptr_type;
11200 : : }
11201 : : /* And the final __cxa_atexit type. */
11202 : 2119 : fn_type = build_function_type_list (integer_type_node,
11203 : : argtype0, argtype1, argtype2,
11204 : : NULL_TREE);
11205 : : /* ... which needs noexcept. */
11206 : 2119 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11207 : 2119 : if (use_aeabi_atexit)
11208 : : {
11209 : 0 : name = "__aeabi_atexit";
11210 : 0 : push_to_top_level ();
11211 : 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
11212 : 0 : ctx = current_namespace;
11213 : 0 : while (n--)
11214 : 0 : pop_namespace ();
11215 : 0 : pop_from_top_level ();
11216 : : }
11217 : : else
11218 : : {
11219 : 2119 : name = "__cxa_atexit";
11220 : 2119 : ctx = abi_node;
11221 : : }
11222 : : }
11223 : : else
11224 : : {
11225 : : /* The declaration for `atexit' is:
11226 : :
11227 : : int atexit (void (*)());
11228 : :
11229 : : We build up the argument types and then the function type
11230 : : itself. */
11231 : 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
11232 : : /* Build the final atexit type. */
11233 : 12 : fn_type = build_function_type_list (integer_type_node,
11234 : : fn_ptr_type, NULL_TREE);
11235 : : /* ... which needs noexcept. */
11236 : 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11237 : 12 : name = "atexit";
11238 : : }
11239 : :
11240 : : /* Now, build the function declaration. */
11241 : 2131 : push_lang_context (lang_name_c);
11242 : 2131 : auto cookie = push_abi_namespace (ctx);
11243 : 2131 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
11244 : 2131 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11245 : : /* Install as hidden builtin so we're (a) more relaxed about
11246 : : exception spec matching and (b) will not give a confusing location
11247 : : in diagnostic and (c) won't magically appear in user-visible name
11248 : : lookups. */
11249 : 2131 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11250 : 2131 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11251 : 2131 : pop_abi_namespace (cookie, ctx);
11252 : 2131 : mark_used (atexit_fndecl);
11253 : 2131 : pop_lang_context ();
11254 : 2131 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11255 : :
11256 : 2131 : return atexit_node;
11257 : : }
11258 : :
11259 : : /* Like get_atexit_node, but for thread-local cleanups. */
11260 : :
11261 : : static tree
11262 : 86 : get_thread_atexit_node (void)
11263 : : {
11264 : 86 : if (thread_atexit_node)
11265 : : return thread_atexit_node;
11266 : :
11267 : : /* The declaration for `__cxa_thread_atexit' is:
11268 : :
11269 : : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11270 : 56 : tree fn_type = build_function_type_list (integer_type_node,
11271 : : get_cxa_atexit_fn_ptr_type (),
11272 : : ptr_type_node, ptr_type_node,
11273 : : NULL_TREE);
11274 : :
11275 : : /* Now, build the function declaration, as with __cxa_atexit. */
11276 : 56 : unsigned flags = push_abi_namespace ();
11277 : 56 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
11278 : : ECF_LEAF | ECF_NOTHROW);
11279 : 56 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11280 : 56 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11281 : 56 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11282 : 56 : pop_abi_namespace (flags);
11283 : 56 : mark_used (atexit_fndecl);
11284 : 56 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11285 : :
11286 : 56 : return thread_atexit_node;
11287 : : }
11288 : :
11289 : : /* Returns the __dso_handle VAR_DECL. */
11290 : :
11291 : : static tree
11292 : 3502 : get_dso_handle_node (void)
11293 : : {
11294 : 3502 : if (dso_handle_node)
11295 : : return dso_handle_node;
11296 : :
11297 : : /* Declare the variable. */
11298 : 2165 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11299 : : ptr_type_node);
11300 : :
11301 : : #ifdef HAVE_GAS_HIDDEN
11302 : 2165 : if (dso_handle_node != error_mark_node)
11303 : : {
11304 : 2162 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11305 : 2162 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11306 : : }
11307 : : #endif
11308 : :
11309 : 2165 : return dso_handle_node;
11310 : : }
11311 : :
11312 : : /* Begin a new function with internal linkage whose job will be simply
11313 : : to destroy some particular DECL. OB_PARM is true if object pointer
11314 : : is passed to the cleanup function, otherwise no argument is passed. */
11315 : :
11316 : : static tree
11317 : 367 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11318 : : {
11319 : 367 : push_to_top_level ();
11320 : :
11321 : : /* No need to mangle this. */
11322 : 367 : push_lang_context (lang_name_c);
11323 : :
11324 : : /* Build the name of the function. */
11325 : 367 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11326 : 367 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11327 : 367 : dname = targetm.strip_name_encoding (dname);
11328 : 731 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11329 : :
11330 : 367 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11331 : : : get_atexit_fn_ptr_type ());
11332 : : /* Build the function declaration. */
11333 : 367 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11334 : 367 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11335 : : /* It's a function with internal linkage, generated by the
11336 : : compiler. */
11337 : 367 : TREE_PUBLIC (fndecl) = 0;
11338 : 367 : DECL_ARTIFICIAL (fndecl) = 1;
11339 : : /* Make the function `inline' so that it is only emitted if it is
11340 : : actually needed. It is unlikely that it will be inlined, since
11341 : : it is only called via a function pointer, but we avoid unnecessary
11342 : : emissions this way. */
11343 : 367 : DECL_DECLARED_INLINE_P (fndecl) = 1;
11344 : 367 : DECL_INTERFACE_KNOWN (fndecl) = 1;
11345 : 367 : if (ob_parm)
11346 : : {
11347 : : /* Build the parameter. */
11348 : 355 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11349 : 355 : TREE_USED (parmdecl) = 1;
11350 : 355 : DECL_READ_P (parmdecl) = 1;
11351 : 355 : DECL_ARGUMENTS (fndecl) = parmdecl;
11352 : : }
11353 : :
11354 : 367 : fndecl = pushdecl (fndecl, /*hidden=*/true);
11355 : 367 : if (omp_target)
11356 : : {
11357 : 3 : DECL_ATTRIBUTES (fndecl)
11358 : 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11359 : 3 : DECL_ATTRIBUTES (fndecl));
11360 : 3 : DECL_ATTRIBUTES (fndecl)
11361 : 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11362 : 3 : DECL_ATTRIBUTES (fndecl));
11363 : : }
11364 : 367 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11365 : :
11366 : 367 : pop_lang_context ();
11367 : :
11368 : 367 : return current_function_decl;
11369 : : }
11370 : :
11371 : : /* Finish the cleanup function begun by start_cleanup_fn. */
11372 : :
11373 : : static void
11374 : 367 : end_cleanup_fn (void)
11375 : : {
11376 : 367 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
11377 : :
11378 : 367 : pop_from_top_level ();
11379 : 367 : }
11380 : :
11381 : : /* Generate code to handle the destruction of DECL, an object with
11382 : : static storage duration. */
11383 : :
11384 : : tree
11385 : 14986 : register_dtor_fn (tree decl, bool omp_target)
11386 : : {
11387 : 14986 : tree cleanup;
11388 : 14986 : tree addr;
11389 : 14986 : tree compound_stmt;
11390 : 14986 : tree fcall;
11391 : 14986 : tree type;
11392 : 14986 : bool ob_parm, dso_parm, use_dtor;
11393 : 14986 : tree arg0, arg1, arg2;
11394 : 14986 : tree atex_node;
11395 : :
11396 : 14986 : type = TREE_TYPE (decl);
11397 : 14986 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11398 : 11472 : return void_node;
11399 : :
11400 : 3514 : if (decl_maybe_constant_destruction (decl, type)
11401 : 3514 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11402 : : {
11403 : 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11404 : 0 : return void_node;
11405 : : }
11406 : :
11407 : : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11408 : : "__aeabi_atexit"), and DECL is a class object, we can just pass the
11409 : : destructor to "__cxa_atexit"; we don't have to build a temporary
11410 : : function to do the cleanup. */
11411 : 7028 : dso_parm = (flag_use_cxa_atexit
11412 : 3514 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
11413 : 3514 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11414 : 3502 : use_dtor = ob_parm && CLASS_TYPE_P (type);
11415 : 3514 : if (use_dtor)
11416 : : {
11417 : 3147 : cleanup = get_class_binding (type, complete_dtor_identifier);
11418 : :
11419 : : /* Make sure it is accessible. */
11420 : 3147 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11421 : : tf_warning_or_error);
11422 : : }
11423 : : else
11424 : : {
11425 : : /* Call build_cleanup before we enter the anonymous function so
11426 : : that any access checks will be done relative to the current
11427 : : scope, rather than the scope of the anonymous function. */
11428 : 367 : build_cleanup (decl);
11429 : :
11430 : : /* Now start the function. */
11431 : 367 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11432 : :
11433 : : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11434 : : to the original function, rather than the anonymous one. That
11435 : : will make the back end think that nested functions are in use,
11436 : : which causes confusion. */
11437 : 367 : push_deferring_access_checks (dk_no_check);
11438 : 367 : fcall = build_cleanup (decl);
11439 : 367 : pop_deferring_access_checks ();
11440 : :
11441 : : /* Create the body of the anonymous function. */
11442 : 367 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11443 : 367 : finish_expr_stmt (fcall);
11444 : 367 : finish_compound_stmt (compound_stmt);
11445 : 367 : end_cleanup_fn ();
11446 : : }
11447 : :
11448 : : /* Call atexit with the cleanup function. */
11449 : 3514 : mark_used (cleanup);
11450 : 3514 : cleanup = build_address (cleanup);
11451 : :
11452 : 3514 : if (CP_DECL_THREAD_LOCAL_P (decl))
11453 : 86 : atex_node = get_thread_atexit_node ();
11454 : : else
11455 : 3428 : atex_node = get_atexit_node ();
11456 : :
11457 : 3514 : if (use_dtor)
11458 : : {
11459 : : /* We must convert CLEANUP to the type that "__cxa_atexit"
11460 : : expects. */
11461 : 3147 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11462 : : /* "__cxa_atexit" will pass the address of DECL to the
11463 : : cleanup function. */
11464 : 3147 : mark_used (decl);
11465 : 3147 : addr = build_address (decl);
11466 : : /* The declared type of the parameter to "__cxa_atexit" is
11467 : : "void *". For plain "T*", we could just let the
11468 : : machinery in cp_build_function_call convert it -- but if the
11469 : : type is "cv-qualified T *", then we need to convert it
11470 : : before passing it in, to avoid spurious errors. */
11471 : 3147 : addr = build_nop (ptr_type_node, addr);
11472 : : }
11473 : : else
11474 : : /* Since the cleanup functions we build ignore the address
11475 : : they're given, there's no reason to pass the actual address
11476 : : in, and, in general, it's cheaper to pass NULL than any
11477 : : other value. */
11478 : 367 : addr = null_pointer_node;
11479 : :
11480 : 3514 : if (dso_parm)
11481 : 3502 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
11482 : : tf_warning_or_error);
11483 : 12 : else if (ob_parm)
11484 : : /* Just pass NULL to the dso handle parm if we don't actually
11485 : : have a DSO handle on this target. */
11486 : 0 : arg2 = null_pointer_node;
11487 : : else
11488 : : arg2 = NULL_TREE;
11489 : :
11490 : 3502 : if (ob_parm)
11491 : : {
11492 : 3502 : if (!CP_DECL_THREAD_LOCAL_P (decl)
11493 : 3502 : && targetm.cxx.use_aeabi_atexit ())
11494 : : {
11495 : : arg1 = cleanup;
11496 : : arg0 = addr;
11497 : : }
11498 : : else
11499 : : {
11500 : : arg1 = addr;
11501 : : arg0 = cleanup;
11502 : : }
11503 : : }
11504 : : else
11505 : : {
11506 : : arg0 = cleanup;
11507 : : arg1 = NULL_TREE;
11508 : : }
11509 : 3514 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11510 : 3514 : arg0, arg1, arg2, NULL_TREE);
11511 : : }
11512 : :
11513 : : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
11514 : : is its initializer. Generate code to handle the construction
11515 : : and destruction of DECL. */
11516 : :
11517 : : static void
11518 : 19329224 : expand_static_init (tree decl, tree init)
11519 : : {
11520 : 19329224 : gcc_assert (VAR_P (decl));
11521 : 19329224 : gcc_assert (TREE_STATIC (decl));
11522 : :
11523 : : /* Some variables require no dynamic initialization. */
11524 : 19329224 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11525 : : {
11526 : : /* Make sure the destructor is callable. */
11527 : 19325796 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11528 : 19325796 : if (!init)
11529 : : return;
11530 : : }
11531 : :
11532 : 15766 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11533 : 14846 : && !DECL_FUNCTION_SCOPE_P (decl))
11534 : : {
11535 : 15 : auto_diagnostic_group d;
11536 : 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11537 : 15 : if (init)
11538 : 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11539 : : "needs dynamic initialization", decl);
11540 : : else
11541 : 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11542 : : "has a non-trivial destructor", decl);
11543 : 15 : static bool informed;
11544 : 15 : if (!informed)
11545 : : {
11546 : 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11547 : : "initialization and destruction");
11548 : 6 : informed = true;
11549 : : }
11550 : 15 : return;
11551 : 15 : }
11552 : :
11553 : 14802 : if (DECL_FUNCTION_SCOPE_P (decl))
11554 : : {
11555 : : /* Emit code to perform this initialization but once. */
11556 : 3836 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11557 : 3836 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11558 : 3836 : tree guard, guard_addr;
11559 : 3836 : tree flag, begin;
11560 : : /* We don't need thread-safety code for thread-local vars. */
11561 : 3836 : bool thread_guard = (flag_threadsafe_statics
11562 : 3836 : && !CP_DECL_THREAD_LOCAL_P (decl));
11563 : :
11564 : : /* Emit code to perform this initialization but once. This code
11565 : : looks like:
11566 : :
11567 : : static <type> guard;
11568 : : if (!__atomic_load (guard.first_byte)) {
11569 : : if (__cxa_guard_acquire (&guard)) {
11570 : : bool flag = false;
11571 : : try {
11572 : : // Do initialization.
11573 : : flag = true; __cxa_guard_release (&guard);
11574 : : // Register variable for destruction at end of program.
11575 : : } catch {
11576 : : if (!flag) __cxa_guard_abort (&guard);
11577 : : }
11578 : : }
11579 : : }
11580 : :
11581 : : Note that the `flag' variable is only set to 1 *after* the
11582 : : initialization is complete. This ensures that an exception,
11583 : : thrown during the construction, will cause the variable to
11584 : : reinitialized when we pass through this code again, as per:
11585 : :
11586 : : [stmt.dcl]
11587 : :
11588 : : If the initialization exits by throwing an exception, the
11589 : : initialization is not complete, so it will be tried again
11590 : : the next time control enters the declaration.
11591 : :
11592 : : This process should be thread-safe, too; multiple threads
11593 : : should not be able to initialize the variable more than
11594 : : once. */
11595 : :
11596 : : /* Create the guard variable. */
11597 : 3836 : guard = get_guard (decl);
11598 : :
11599 : : /* Begin the conditional initialization. */
11600 : 3836 : if_stmt = begin_if_stmt ();
11601 : :
11602 : 3836 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11603 : 3836 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11604 : :
11605 : 3836 : if (thread_guard)
11606 : : {
11607 : 3694 : tree vfntype = NULL_TREE;
11608 : 3694 : tree acquire_name, release_name, abort_name;
11609 : 3694 : tree acquire_fn, release_fn, abort_fn;
11610 : 3694 : guard_addr = build_address (guard);
11611 : :
11612 : 3694 : acquire_name = get_identifier ("__cxa_guard_acquire");
11613 : 3694 : release_name = get_identifier ("__cxa_guard_release");
11614 : 3694 : abort_name = get_identifier ("__cxa_guard_abort");
11615 : 3694 : acquire_fn = get_global_binding (acquire_name);
11616 : 3694 : release_fn = get_global_binding (release_name);
11617 : 3694 : abort_fn = get_global_binding (abort_name);
11618 : 3694 : if (!acquire_fn)
11619 : 2078 : acquire_fn = push_library_fn
11620 : 2078 : (acquire_name, build_function_type_list (integer_type_node,
11621 : 2078 : TREE_TYPE (guard_addr),
11622 : : NULL_TREE),
11623 : : NULL_TREE, ECF_NOTHROW);
11624 : 3694 : if (!release_fn || !abort_fn)
11625 : 2081 : vfntype = build_function_type_list (void_type_node,
11626 : 2081 : TREE_TYPE (guard_addr),
11627 : : NULL_TREE);
11628 : 2081 : if (!release_fn)
11629 : 2081 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11630 : : ECF_NOTHROW);
11631 : 3694 : if (!abort_fn)
11632 : 2081 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11633 : : ECF_NOTHROW | ECF_LEAF);
11634 : :
11635 : 3694 : inner_if_stmt = begin_if_stmt ();
11636 : 3694 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11637 : : inner_if_stmt);
11638 : :
11639 : 3694 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11640 : 3694 : begin = get_internal_target_expr (boolean_false_node);
11641 : 3694 : flag = TARGET_EXPR_SLOT (begin);
11642 : :
11643 : 3694 : TARGET_EXPR_CLEANUP (begin)
11644 : 3694 : = build3 (COND_EXPR, void_type_node, flag,
11645 : : void_node,
11646 : : build_call_n (abort_fn, 1, guard_addr));
11647 : 3694 : CLEANUP_EH_ONLY (begin) = 1;
11648 : :
11649 : : /* Do the initialization itself. */
11650 : 3694 : init = add_stmt_to_compound (begin, init);
11651 : 3694 : init = add_stmt_to_compound (init,
11652 : : build2 (MODIFY_EXPR, void_type_node,
11653 : : flag, boolean_true_node));
11654 : :
11655 : : /* Use atexit to register a function for destroying this static
11656 : : variable. Do this before calling __cxa_guard_release. */
11657 : 3694 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11658 : :
11659 : 3694 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11660 : : guard_addr));
11661 : : }
11662 : : else
11663 : : {
11664 : 142 : init = add_stmt_to_compound (init, set_guard (guard));
11665 : :
11666 : : /* Use atexit to register a function for destroying this static
11667 : : variable. */
11668 : 142 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11669 : : }
11670 : :
11671 : 3836 : finish_expr_stmt (init);
11672 : :
11673 : 3836 : if (thread_guard)
11674 : : {
11675 : 3694 : finish_compound_stmt (inner_then_clause);
11676 : 3694 : finish_then_clause (inner_if_stmt);
11677 : 3694 : finish_if_stmt (inner_if_stmt);
11678 : : }
11679 : :
11680 : 3836 : finish_compound_stmt (then_clause);
11681 : 3836 : finish_then_clause (if_stmt);
11682 : 3836 : finish_if_stmt (if_stmt);
11683 : : }
11684 : 10966 : else if (CP_DECL_THREAD_LOCAL_P (decl))
11685 : 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
11686 : : else
11687 : 10171 : static_aggregates = tree_cons (init, decl, static_aggregates);
11688 : : }
11689 : :
11690 : :
11691 : : /* Make TYPE a complete type based on INITIAL_VALUE.
11692 : : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11693 : : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11694 : : 3 if the initializer list is empty (in pedantic mode). */
11695 : :
11696 : : int
11697 : 557763 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11698 : : {
11699 : 557763 : int failure;
11700 : 557763 : tree type, elt_type;
11701 : :
11702 : : /* Don't get confused by a CONSTRUCTOR for some other type. */
11703 : 557733 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11704 : 98209 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11705 : 558090 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11706 : : return 1;
11707 : :
11708 : 557760 : if (initial_value)
11709 : : {
11710 : : /* An array of character type can be initialized from a
11711 : : brace-enclosed string constant so call reshape_init to
11712 : : remove the optional braces from a braced string literal. */
11713 : 557730 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
11714 : 557730 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
11715 : 30291 : initial_value = reshape_init (*ptype, initial_value,
11716 : : tf_warning_or_error);
11717 : :
11718 : : /* If any of the elements are parameter packs, we can't actually
11719 : : complete this type now because the array size is dependent. */
11720 : 557730 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
11721 : 17624305 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
11722 : 17330082 : if (PACK_EXPANSION_P (e.value))
11723 : : return 0;
11724 : : }
11725 : :
11726 : 557760 : failure = complete_array_type (ptype, initial_value, do_default);
11727 : :
11728 : : /* We can create the array before the element type is complete, which
11729 : : means that we didn't have these two bits set in the original type
11730 : : either. In completing the type, we are expected to propagate these
11731 : : bits. See also complete_type which does the same thing for arrays
11732 : : of fixed size. */
11733 : 557760 : type = *ptype;
11734 : 1115517 : if (type != error_mark_node && TYPE_DOMAIN (type))
11735 : : {
11736 : 557733 : elt_type = TREE_TYPE (type);
11737 : 557733 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
11738 : 1115466 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11739 : 557733 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
11740 : : }
11741 : :
11742 : : return failure;
11743 : : }
11744 : :
11745 : : /* As above, but either give an error or reject zero-size arrays, depending
11746 : : on COMPLAIN. */
11747 : :
11748 : : int
11749 : 1519 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
11750 : : bool do_default, tsubst_flags_t complain)
11751 : : {
11752 : 1519 : int failure;
11753 : 1519 : bool sfinae = !(complain & tf_error);
11754 : : /* In SFINAE context we can't be lenient about zero-size arrays. */
11755 : 1519 : if (sfinae)
11756 : 3 : ++pedantic;
11757 : 1519 : failure = cp_complete_array_type (ptype, initial_value, do_default);
11758 : 1519 : if (sfinae)
11759 : 3 : --pedantic;
11760 : 1519 : if (failure)
11761 : : {
11762 : 3 : if (sfinae)
11763 : : /* Not an error. */;
11764 : 0 : else if (failure == 1)
11765 : 0 : error ("initializer fails to determine size of %qT", *ptype);
11766 : 0 : else if (failure == 2)
11767 : : {
11768 : 0 : if (do_default)
11769 : 0 : error ("array size missing in %qT", *ptype);
11770 : : }
11771 : 0 : else if (failure == 3)
11772 : 0 : error ("zero-size array %qT", *ptype);
11773 : 3 : *ptype = error_mark_node;
11774 : : }
11775 : 1519 : return failure;
11776 : : }
11777 : :
11778 : : /* Return zero if something is declared to be a member of type
11779 : : CTYPE when in the context of CUR_TYPE. STRING is the error
11780 : : message to print in that case. Otherwise, quietly return 1. */
11781 : :
11782 : : static int
11783 : 25070264 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
11784 : : {
11785 : 25070264 : if (ctype && ctype != cur_type)
11786 : : {
11787 : 0 : if (flags == DTOR_FLAG)
11788 : 0 : error ("destructor for alien class %qT cannot be a member", ctype);
11789 : : else
11790 : 0 : error ("constructor for alien class %qT cannot be a member", ctype);
11791 : 0 : return 0;
11792 : : }
11793 : : return 1;
11794 : : }
11795 : :
11796 : : /* Subroutine of `grokdeclarator'. */
11797 : :
11798 : : /* Generate errors possibly applicable for a given set of specifiers.
11799 : : This is for ARM $7.1.2. */
11800 : :
11801 : : static void
11802 : 417272993 : bad_specifiers (tree object,
11803 : : enum bad_spec_place type,
11804 : : int virtualp,
11805 : : int quals,
11806 : : int inlinep,
11807 : : int friendp,
11808 : : int raises,
11809 : : const location_t* locations)
11810 : : {
11811 : 417272993 : switch (type)
11812 : : {
11813 : 58858649 : case BSP_VAR:
11814 : 58858649 : if (virtualp)
11815 : 0 : error_at (locations[ds_virtual],
11816 : : "%qD declared as a %<virtual%> variable", object);
11817 : 58858649 : if (quals)
11818 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11819 : : "%qD invalid in variable declaration", object);
11820 : : break;
11821 : 284068738 : case BSP_PARM:
11822 : 284068738 : if (virtualp)
11823 : 0 : error_at (locations[ds_virtual],
11824 : : "%qD declared as a %<virtual%> parameter", object);
11825 : 284068738 : if (inlinep)
11826 : 6 : error_at (locations[ds_inline],
11827 : : "%qD declared as an %<inline%> parameter", object);
11828 : 284068738 : if (quals)
11829 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11830 : : "%qD invalid in parameter declaration", object);
11831 : : break;
11832 : 45241078 : case BSP_TYPE:
11833 : 45241078 : if (virtualp)
11834 : 0 : error_at (locations[ds_virtual],
11835 : : "%qD declared as a %<virtual%> type", object);
11836 : 45241078 : if (inlinep)
11837 : 6 : error_at (locations[ds_inline],
11838 : : "%qD declared as an %<inline%> type", object);
11839 : 45241078 : if (quals)
11840 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11841 : : "%qD invalid in type declaration", object);
11842 : : break;
11843 : 29104528 : case BSP_FIELD:
11844 : 29104528 : if (virtualp)
11845 : 6 : error_at (locations[ds_virtual],
11846 : : "%qD declared as a %<virtual%> field", object);
11847 : 29104528 : if (inlinep)
11848 : 6 : error_at (locations[ds_inline],
11849 : : "%qD declared as an %<inline%> field", object);
11850 : 29104528 : if (quals)
11851 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11852 : : "%qD invalid in field declaration", object);
11853 : : break;
11854 : 0 : default:
11855 : 0 : gcc_unreachable();
11856 : : }
11857 : 417272993 : if (friendp)
11858 : 0 : error ("%q+D declared as a friend", object);
11859 : 417272993 : if (raises
11860 : 109 : && !flag_noexcept_type
11861 : 417273014 : && (TREE_CODE (object) == TYPE_DECL
11862 : 15 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
11863 : 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
11864 : 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
11865 : 6 : error ("%q+D declared with an exception specification", object);
11866 : 417272993 : }
11867 : :
11868 : : /* DECL is a member function or static data member and is presently
11869 : : being defined. Check that the definition is taking place in a
11870 : : valid namespace. */
11871 : :
11872 : : static void
11873 : 87745033 : check_class_member_definition_namespace (tree decl)
11874 : : {
11875 : : /* These checks only apply to member functions and static data
11876 : : members. */
11877 : 87745033 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
11878 : : /* We check for problems with specializations in pt.cc in
11879 : : check_specialization_namespace, where we can issue better
11880 : : diagnostics. */
11881 : 87745033 : if (processing_specialization)
11882 : : return;
11883 : : /* We check this in check_explicit_instantiation_namespace. */
11884 : 87595438 : if (processing_explicit_instantiation)
11885 : : return;
11886 : : /* [class.mfct]
11887 : :
11888 : : A member function definition that appears outside of the
11889 : : class definition shall appear in a namespace scope enclosing
11890 : : the class definition.
11891 : :
11892 : : [class.static.data]
11893 : :
11894 : : The definition for a static data member shall appear in a
11895 : : namespace scope enclosing the member's class definition. */
11896 : 87595358 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
11897 : 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
11898 : 18 : decl, DECL_CONTEXT (decl));
11899 : : }
11900 : :
11901 : : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
11902 : : METHOD_TYPE for a non-static member function; QUALS are the
11903 : : cv-qualifiers that apply to the function. */
11904 : :
11905 : : tree
11906 : 212751531 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
11907 : : {
11908 : 212751531 : tree this_type;
11909 : 212751531 : tree qual_type;
11910 : 212751531 : tree parm;
11911 : 212751531 : cp_cv_quals this_quals;
11912 : :
11913 : 212751531 : if (CLASS_TYPE_P (type))
11914 : : {
11915 : 95158693 : this_type
11916 : 95158693 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
11917 : 95158693 : this_type = build_pointer_type (this_type);
11918 : : }
11919 : : else
11920 : 117592838 : this_type = type_of_this_parm (type);
11921 : : /* The `this' parameter is implicitly `const'; it cannot be
11922 : : assigned to. */
11923 : 212751531 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
11924 : 212751531 : qual_type = cp_build_qualified_type (this_type, this_quals);
11925 : 212751531 : parm = build_artificial_parm (fn, this_identifier, qual_type);
11926 : 212751531 : cp_apply_type_quals_to_decl (this_quals, parm);
11927 : 212751531 : return parm;
11928 : : }
11929 : :
11930 : : /* DECL is a static member function. Complain if it was declared
11931 : : with function-cv-quals. */
11932 : :
11933 : : static void
11934 : 9426353 : check_static_quals (tree decl, cp_cv_quals quals)
11935 : : {
11936 : 0 : if (quals != TYPE_UNQUALIFIED)
11937 : 6 : error ("static member function %q#D declared with type qualifiers",
11938 : : decl);
11939 : 0 : }
11940 : :
11941 : : /* Helper function. Replace the temporary this parameter injected
11942 : : during cp_finish_omp_declare_simd with the real this parameter. */
11943 : :
11944 : : static tree
11945 : 2692 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11946 : : {
11947 : 2692 : tree this_parm = (tree) data;
11948 : 2692 : if (TREE_CODE (*tp) == PARM_DECL
11949 : 450 : && DECL_NAME (*tp) == this_identifier
11950 : 2965 : && *tp != this_parm)
11951 : 273 : *tp = this_parm;
11952 : 2419 : else if (TYPE_P (*tp))
11953 : 33 : *walk_subtrees = 0;
11954 : 2692 : return NULL_TREE;
11955 : : }
11956 : :
11957 : : /* CTYPE is class type, or null if non-class.
11958 : : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11959 : : or METHOD_TYPE.
11960 : : DECLARATOR is the function's name.
11961 : : PARMS is a chain of PARM_DECLs for the function.
11962 : : VIRTUALP is truthvalue of whether the function is virtual or not.
11963 : : FLAGS are to be passed through to `grokclassfn'.
11964 : : QUALS are qualifiers indicating whether the function is `const'
11965 : : or `volatile'.
11966 : : RAISES is a list of exceptions that this function can raise.
11967 : : CHECK is 1 if we must find this method in CTYPE, 0 if we should
11968 : : not look, and -1 if we should not call `grokclassfn' at all.
11969 : :
11970 : : SFK is the kind of special function (if any) for the new function.
11971 : :
11972 : : Returns `NULL_TREE' if something goes wrong, after issuing
11973 : : applicable error messages. */
11974 : :
11975 : : static tree
11976 : 178815531 : grokfndecl (tree ctype,
11977 : : tree type,
11978 : : tree declarator,
11979 : : tree parms,
11980 : : tree orig_declarator,
11981 : : const cp_decl_specifier_seq *declspecs,
11982 : : tree decl_reqs,
11983 : : int virtualp,
11984 : : enum overload_flags flags,
11985 : : cp_cv_quals quals,
11986 : : cp_ref_qualifier rqual,
11987 : : tree raises,
11988 : : int check,
11989 : : int friendp,
11990 : : int publicp,
11991 : : int inlinep,
11992 : : bool deletedp,
11993 : : bool xobj_func_p,
11994 : : special_function_kind sfk,
11995 : : bool funcdef_flag,
11996 : : bool late_return_type_p,
11997 : : int template_count,
11998 : : tree in_namespace,
11999 : : tree* attrlist,
12000 : : location_t location)
12001 : : {
12002 : 178815531 : tree decl;
12003 : 178815531 : tree t;
12004 : :
12005 : 178815531 : if (location == UNKNOWN_LOCATION)
12006 : 0 : location = input_location;
12007 : :
12008 : : /* Was the concept specifier present? */
12009 : 178815531 : bool concept_p = inlinep & 4;
12010 : :
12011 : 178815531 : if (concept_p)
12012 : : {
12013 : 15 : error_at (location, "function concepts are no longer supported");
12014 : 15 : return NULL_TREE;
12015 : : }
12016 : :
12017 : 178815516 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
12018 : :
12019 : 178815516 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
12020 : :
12021 : : /* Set the constraints on the declaration. */
12022 : 178815516 : if (flag_concepts)
12023 : : {
12024 : 63032644 : tree tmpl_reqs = NULL_TREE;
12025 : 63032644 : tree ctx = friendp ? current_class_type : ctype;
12026 : 63032644 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
12027 : 63032644 : bool memtmpl = (!block_local
12028 : 63032644 : && (current_template_depth
12029 : 63011087 : > template_class_depth (ctx)));
12030 : 17444916 : if (memtmpl)
12031 : : {
12032 : 17444916 : if (!current_template_parms)
12033 : : /* If there are no template parameters, something must have
12034 : : gone wrong. */
12035 : 0 : gcc_assert (seen_error ());
12036 : : else
12037 : 17444916 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12038 : : }
12039 : 63032644 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
12040 : : /* C++20 CA378: Remove non-templated constrained functions. */
12041 : : /* [temp.friend]/9 A non-template friend declaration with a
12042 : : requires-clause shall be a definition. A friend function template with
12043 : : a constraint that depends on a template parameter from an enclosing
12044 : : template shall be a definition. */
12045 : 63032644 : if (ci
12046 : 4531349 : && (block_local
12047 : 4531346 : || !processing_template_decl
12048 : 4531289 : || (friendp && !memtmpl && !funcdef_flag)))
12049 : : {
12050 : 69 : if (!friendp || !processing_template_decl)
12051 : 60 : error_at (location, "constraints on a non-templated function");
12052 : : else
12053 : 9 : error_at (location, "constrained non-template friend declaration"
12054 : : " must be a definition");
12055 : : ci = NULL_TREE;
12056 : : }
12057 : 63032644 : set_constraints (decl, ci);
12058 : 642476 : if (ci && friendp && memtmpl && !funcdef_flag
12059 : 63043189 : && uses_outer_template_parms_in_constraints (decl, ctx))
12060 : 6 : error_at (location, "friend function template with constraints that "
12061 : : "depend on outer template parameters must be a definition");
12062 : : }
12063 : :
12064 : 178815516 : if (TREE_CODE (type) == METHOD_TYPE)
12065 : : {
12066 : 93564782 : tree parm = build_this_parm (decl, type, quals);
12067 : 93564782 : DECL_CHAIN (parm) = parms;
12068 : 93564782 : parms = parm;
12069 : :
12070 : : /* Allocate space to hold the vptr bit if needed. */
12071 : 93564782 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12072 : : }
12073 : :
12074 : 178815516 : DECL_ARGUMENTS (decl) = parms;
12075 : 540776262 : for (t = parms; t; t = DECL_CHAIN (t))
12076 : 361960746 : DECL_CONTEXT (t) = decl;
12077 : :
12078 : : /* Propagate volatile out from type to decl. */
12079 : 178815516 : if (TYPE_VOLATILE (type))
12080 : 0 : TREE_THIS_VOLATILE (decl) = 1;
12081 : :
12082 : : /* Setup decl according to sfk. */
12083 : 178815516 : switch (sfk)
12084 : : {
12085 : 22819810 : case sfk_constructor:
12086 : 22819810 : case sfk_copy_constructor:
12087 : 22819810 : case sfk_move_constructor:
12088 : 22819810 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12089 : 22819810 : DECL_NAME (decl) = ctor_identifier;
12090 : 22819810 : break;
12091 : 3236264 : case sfk_destructor:
12092 : 3236264 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
12093 : 3236264 : DECL_NAME (decl) = dtor_identifier;
12094 : 3236264 : break;
12095 : 1112206 : case sfk_deduction_guide:
12096 : : /* Give deduction guides a definition even though they don't really
12097 : : have one: the restriction that you can't repeat a deduction guide
12098 : : makes them more like a definition anyway. */
12099 : 1112206 : DECL_INITIAL (decl) = void_node;
12100 : : /* But to ensure that external-linkage deduction guides in header units
12101 : : don't fall afoul of [module.import] p6, mark them as inline. */
12102 : 1112206 : DECL_DECLARED_INLINE_P (decl) = true;
12103 : 1112206 : break;
12104 : : default:
12105 : : break;
12106 : : }
12107 : :
12108 : 178815516 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12109 : : {
12110 : 50294 : if (funcdef_flag)
12111 : 3 : error_at (location,
12112 : : "defining explicit specialization %qD in friend declaration",
12113 : : orig_declarator);
12114 : : else
12115 : : {
12116 : 50291 : tree fns = TREE_OPERAND (orig_declarator, 0);
12117 : 50291 : tree args = TREE_OPERAND (orig_declarator, 1);
12118 : :
12119 : 50291 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12120 : : {
12121 : : /* Something like `template <class T> friend void f<T>()'. */
12122 : 6 : error_at (location,
12123 : : "invalid use of template-id %qD in declaration "
12124 : : "of primary template",
12125 : : orig_declarator);
12126 : 6 : return NULL_TREE;
12127 : : }
12128 : :
12129 : : /* A friend declaration of the form friend void f<>(). Record
12130 : : the information in the TEMPLATE_ID_EXPR. */
12131 : 50285 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
12132 : :
12133 : 50285 : gcc_assert (identifier_p (fns) || OVL_P (fns));
12134 : 50285 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12135 : :
12136 : : /* Remember the befriending class like push_template_decl does for
12137 : : template friends. */
12138 : 50285 : gcc_checking_assert (!DECL_CHAIN (decl));
12139 : 50285 : DECL_CHAIN (decl) = current_scope ();
12140 : :
12141 : 172937 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12142 : 122657 : if (TREE_PURPOSE (t)
12143 : 122657 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12144 : : {
12145 : 5 : error_at (defparse_location (TREE_PURPOSE (t)),
12146 : : "default arguments are not allowed in declaration "
12147 : : "of friend template specialization %qD",
12148 : : decl);
12149 : 5 : return NULL_TREE;
12150 : : }
12151 : :
12152 : 50280 : if (inlinep & 1)
12153 : : {
12154 : 5 : error_at (declspecs->locations[ds_inline],
12155 : : "%<inline%> is not allowed in declaration of friend "
12156 : : "template specialization %qD",
12157 : : decl);
12158 : 5 : return NULL_TREE;
12159 : : }
12160 : : }
12161 : : }
12162 : :
12163 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12164 : : expression, that declaration shall be a definition..." */
12165 : 178815500 : if (friendp && !funcdef_flag)
12166 : : {
12167 : 1377246 : bool has_errored = false;
12168 : 1377246 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12169 : 3973848 : t && t != void_list_node; t = TREE_CHAIN (t))
12170 : 2596602 : if (TREE_PURPOSE (t))
12171 : : {
12172 : 48 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12173 : : /* For templates, mark the default argument as erroneous and give a
12174 : : hard error. */
12175 : 48 : if (processing_template_decl)
12176 : : {
12177 : 30 : diag_kind = diagnostics::kind::error;
12178 : 30 : TREE_PURPOSE (t) = error_mark_node;
12179 : : }
12180 : 48 : if (!has_errored)
12181 : : {
12182 : 45 : has_errored = true;
12183 : 45 : emit_diagnostic (diag_kind,
12184 : 45 : DECL_SOURCE_LOCATION (decl),
12185 : 45 : /*diagnostics::option_id=*/0,
12186 : : "friend declaration of %qD specifies default "
12187 : : "arguments and isn%'t a definition", decl);
12188 : : }
12189 : : }
12190 : : }
12191 : :
12192 : : /* If this decl has namespace scope, set that up. */
12193 : 178815500 : if (in_namespace)
12194 : 193194 : set_decl_namespace (decl, in_namespace, friendp);
12195 : 178622306 : else if (ctype)
12196 : 102913590 : DECL_CONTEXT (decl) = ctype;
12197 : : else
12198 : 75708716 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12199 : :
12200 : : /* `main' and builtins have implicit 'C' linkage. */
12201 : 178815500 : if (ctype == NULL_TREE
12202 : 75901910 : && DECL_FILE_SCOPE_P (decl)
12203 : 32895283 : && current_lang_name == lang_name_cplusplus
12204 : 181812154 : && (MAIN_NAME_P (declarator)
12205 : 2963294 : || (IDENTIFIER_LENGTH (declarator) > 10
12206 : 2109218 : && IDENTIFIER_POINTER (declarator)[0] == '_'
12207 : 1641938 : && IDENTIFIER_POINTER (declarator)[1] == '_'
12208 : 359313 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
12209 : : "builtin_"))
12210 : 2963276 : || (targetcm.cxx_implicit_extern_c
12211 : 0 : && (targetcm.cxx_implicit_extern_c
12212 : 0 : (IDENTIFIER_POINTER (declarator))))))
12213 : 33378 : SET_DECL_LANGUAGE (decl, lang_c);
12214 : :
12215 : 178815500 : DECL_STATIC_FUNCTION_P (decl)
12216 : 178815500 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12217 : 178815500 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12218 : :
12219 : 178815500 : if (deletedp)
12220 : 3613193 : DECL_DELETED_FN (decl) = 1;
12221 : :
12222 : 178815500 : if (ctype && funcdef_flag)
12223 : 87331747 : check_class_member_definition_namespace (decl);
12224 : :
12225 : 178815500 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12226 : : {
12227 : 33366 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
12228 : 9 : error_at (location, "cannot declare %<::main%> to be a template");
12229 : 33366 : if (inlinep & 1)
12230 : 3 : error_at (declspecs->locations[ds_inline],
12231 : : "cannot declare %<::main%> to be inline");
12232 : 33366 : if (inlinep & 2)
12233 : 6 : error_at (declspecs->locations[ds_constexpr],
12234 : : "cannot declare %<::main%> to be %qs", "constexpr");
12235 : 33366 : if (inlinep & 8)
12236 : 3 : error_at (declspecs->locations[ds_consteval],
12237 : : "cannot declare %<::main%> to be %qs", "consteval");
12238 : 33366 : if (!publicp)
12239 : 0 : error_at (location, "cannot declare %<::main%> to be static");
12240 : 33366 : if (current_lang_name != lang_name_cplusplus)
12241 : 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12242 : : " linkage specification other than %<extern \"C++\"%>");
12243 : 33366 : if (module_attach_p ())
12244 : : {
12245 : 3 : auto_diagnostic_group adg;
12246 : 3 : error_at (location, "cannot attach %<::main%> to a named module");
12247 : 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
12248 : : "global module instead");
12249 : 3 : }
12250 : : inlinep = 0;
12251 : : publicp = 1;
12252 : : }
12253 : :
12254 : : /* Members of anonymous types and local classes have no linkage; make
12255 : : them internal. If a typedef is made later, this will be changed. */
12256 : 178815500 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12257 : : publicp = 0;
12258 : 178802430 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12259 : : /* But members of local classes in a module CMI should have their
12260 : : definitions exported, in case they are (directly or indirectly)
12261 : : used by an importer. We don't just use module_has_cmi_p here
12262 : : because for entities in the GMF we don't yet know whether this
12263 : : module will have a CMI, so we'll conservatively assume it might. */
12264 : 1677673 : publicp = module_maybe_has_cmi_p ();
12265 : :
12266 : 178802430 : if (publicp && cxx_dialect == cxx98)
12267 : : {
12268 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12269 : : or enumeration declared in a local scope) shall not be used to
12270 : : declare an entity with linkage.
12271 : :
12272 : : DR 757 relaxes this restriction for C++0x. */
12273 : 1041533 : no_linkage_error (decl);
12274 : : }
12275 : :
12276 : 178815500 : TREE_PUBLIC (decl) = publicp;
12277 : 178815500 : if (! publicp)
12278 : : {
12279 : 1796750 : DECL_INTERFACE_KNOWN (decl) = 1;
12280 : 1796750 : DECL_NOT_REALLY_EXTERN (decl) = 1;
12281 : : }
12282 : :
12283 : : /* If the declaration was declared inline, mark it as such. */
12284 : 178815500 : if (inlinep)
12285 : : {
12286 : 55464800 : DECL_DECLARED_INLINE_P (decl) = 1;
12287 : 55464800 : if (publicp)
12288 : 55297988 : DECL_COMDAT (decl) = 1;
12289 : : }
12290 : 55464800 : if (inlinep & 2)
12291 : 34328419 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12292 : 144487081 : else if (inlinep & 8)
12293 : : {
12294 : 370522 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12295 : 370522 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12296 : : }
12297 : :
12298 : 178815500 : DECL_EXTERNAL (decl) = 1;
12299 : 178815500 : if (TREE_CODE (type) == FUNCTION_TYPE)
12300 : : {
12301 : 85250718 : if (quals || rqual)
12302 : 272 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12303 : : TYPE_UNQUALIFIED,
12304 : : REF_QUAL_NONE);
12305 : 85250718 : auto_diagnostic_group d;
12306 : 85250718 : if (quals)
12307 : 423 : error (!ctype
12308 : : ? G_("non-member function %qD cannot have cv-qualifier")
12309 : : : !xobj_func_p
12310 : 198 : ? G_("static member function %qD cannot have cv-qualifier")
12311 : : : G_("explicit object member function "
12312 : : "%qD cannot have cv-qualifier"),
12313 : : decl);
12314 : 85250718 : if (rqual)
12315 : 355 : error (!ctype
12316 : : ? G_("non-member function %qD cannot have ref-qualifier")
12317 : : : !xobj_func_p
12318 : 176 : ? G_("static member function %qD cannot have ref-qualifier")
12319 : : : G_("explicit object member function "
12320 : : "%qD cannot have ref-qualifier"),
12321 : : decl);
12322 : :
12323 : 85250718 : if (xobj_func_p && (quals || rqual))
12324 : 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12325 : : "explicit object parameter declared here");
12326 : 85250718 : quals = TYPE_UNQUALIFIED;
12327 : 85250718 : rqual = REF_QUAL_NONE;
12328 : :
12329 : 85250718 : }
12330 : :
12331 : 178815500 : if (deduction_guide_p (decl))
12332 : : {
12333 : 1112206 : tree type = TREE_TYPE (DECL_NAME (decl));
12334 : 1112206 : if (in_namespace == NULL_TREE
12335 : 1112206 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12336 : : {
12337 : 3 : auto_diagnostic_group d;
12338 : 3 : error_at (location, "deduction guide %qD must be declared in the "
12339 : : "same scope as %qT", decl, type);
12340 : 3 : inform (location_of (type), " declared here");
12341 : 3 : return NULL_TREE;
12342 : 3 : }
12343 : 2224406 : if (DECL_CLASS_SCOPE_P (decl)
12344 : 1112242 : && current_access_specifier != declared_access (TYPE_NAME (type)))
12345 : : {
12346 : 3 : auto_diagnostic_group d;
12347 : 3 : error_at (location, "deduction guide %qD must have the same access "
12348 : : "as %qT", decl, type);
12349 : 3 : inform (location_of (type), " declared here");
12350 : 3 : }
12351 : 1112203 : if (funcdef_flag)
12352 : 3 : error_at (location,
12353 : : "deduction guide %qD must not have a function body", decl);
12354 : : }
12355 : 333279669 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12356 : 188610453 : && !grok_op_properties (decl, /*complain=*/true))
12357 : : return NULL_TREE;
12358 : 177703147 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
12359 : : {
12360 : 290462 : bool long_long_unsigned_p;
12361 : 290462 : bool long_double_p;
12362 : 290462 : const char *suffix = NULL;
12363 : : /* [over.literal]/6: Literal operators shall not have C linkage. */
12364 : 290462 : if (DECL_LANGUAGE (decl) == lang_c)
12365 : : {
12366 : 6 : auto_diagnostic_group d;
12367 : 6 : error_at (location, "literal operator with C linkage");
12368 : 6 : maybe_show_extern_c_location ();
12369 : 6 : return NULL_TREE;
12370 : 6 : }
12371 : :
12372 : 290456 : if (DECL_NAMESPACE_SCOPE_P (decl))
12373 : : {
12374 : 290450 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12375 : : &long_double_p))
12376 : : {
12377 : 42 : error_at (location, "%qD has invalid argument list", decl);
12378 : 42 : return NULL_TREE;
12379 : : }
12380 : :
12381 : 290408 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12382 : 290408 : if (long_long_unsigned_p)
12383 : : {
12384 : 34943 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
12385 : 68 : warning_at (location, 0, "integer suffix %qs"
12386 : : " shadowed by implementation", suffix);
12387 : : }
12388 : 255465 : else if (long_double_p)
12389 : : {
12390 : 90312 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
12391 : 80 : warning_at (location, 0, "floating-point suffix %qs"
12392 : : " shadowed by implementation", suffix);
12393 : : }
12394 : : /* 17.6.3.3.5 */
12395 : 290408 : if (suffix[0] != '_'
12396 : 289858 : && !current_function_decl && !(friendp && !funcdef_flag))
12397 : 289852 : warning_at (location, OPT_Wliteral_suffix,
12398 : : "literal operator suffixes not preceded by %<_%>"
12399 : : " are reserved for future standardization");
12400 : : }
12401 : : else
12402 : : {
12403 : 6 : error_at (location, "%qD must be a non-member function", decl);
12404 : 6 : return NULL_TREE;
12405 : : }
12406 : : }
12407 : :
12408 : 177703096 : if (funcdef_flag)
12409 : : /* Make the init_value nonzero so pushdecl knows this is not
12410 : : tentative. error_mark_node is replaced later with the BLOCK. */
12411 : 122081133 : DECL_INITIAL (decl) = error_mark_node;
12412 : :
12413 : 178815296 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12414 : 67702880 : TREE_NOTHROW (decl) = 1;
12415 : :
12416 : 178815296 : if (flag_openmp || flag_openmp_simd)
12417 : : {
12418 : : /* Adjust "omp declare simd" attributes. */
12419 : 471212 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
12420 : 471212 : if (ods)
12421 : : {
12422 : : tree attr;
12423 : 2033 : for (attr = ods; attr;
12424 : 1078 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
12425 : : {
12426 : 1078 : if (TREE_CODE (type) == METHOD_TYPE)
12427 : 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12428 : : DECL_ARGUMENTS (decl), NULL);
12429 : 1078 : if (TREE_VALUE (attr) != NULL_TREE)
12430 : : {
12431 : 872 : tree cl = TREE_VALUE (TREE_VALUE (attr));
12432 : 872 : cl = c_omp_declare_simd_clauses_to_numbers
12433 : 872 : (DECL_ARGUMENTS (decl), cl);
12434 : 872 : if (cl)
12435 : 872 : TREE_VALUE (TREE_VALUE (attr)) = cl;
12436 : : else
12437 : 0 : TREE_VALUE (attr) = NULL_TREE;
12438 : : }
12439 : : }
12440 : : }
12441 : : }
12442 : :
12443 : : /* Caller will do the rest of this. */
12444 : 178815296 : if (check < 0)
12445 : 4581231 : return decl;
12446 : :
12447 : 174234065 : if (ctype != NULL_TREE)
12448 : 102913297 : grokclassfn (ctype, decl, flags);
12449 : :
12450 : : /* 12.4/3 */
12451 : 174234065 : if (cxx_dialect >= cxx11
12452 : 346401060 : && DECL_DESTRUCTOR_P (decl)
12453 : 3217295 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12454 : 174408676 : && !processing_template_decl)
12455 : 88398 : deduce_noexcept_on_destructor (decl);
12456 : :
12457 : 174234065 : set_originating_module (decl);
12458 : :
12459 : 174234065 : decl = check_explicit_specialization (orig_declarator, decl,
12460 : : template_count,
12461 : 174234065 : 2 * funcdef_flag +
12462 : 174234065 : 4 * (friendp != 0),
12463 : : *attrlist);
12464 : 174234065 : if (decl == error_mark_node)
12465 : : return NULL_TREE;
12466 : :
12467 : 174233816 : if (DECL_STATIC_FUNCTION_P (decl))
12468 : 9259711 : check_static_quals (decl, quals);
12469 : :
12470 : 174233816 : if (attrlist)
12471 : : {
12472 : 174233816 : cplus_decl_attributes (&decl, *attrlist, 0);
12473 : 174233816 : *attrlist = NULL_TREE;
12474 : : }
12475 : :
12476 : 174233816 : if (DECL_HAS_CONTRACTS_P (decl))
12477 : 467 : rebuild_postconditions (decl);
12478 : :
12479 : : /* Check main's type after attributes have been applied. */
12480 : 174233816 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12481 : : {
12482 : 33354 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12483 : : integer_type_node))
12484 : : {
12485 : 20 : tree dtype = TREE_TYPE (decl);
12486 : 20 : tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12487 : 20 : tree newtype;
12488 : 20 : error_at (declspecs->locations[ds_type_spec],
12489 : : "%<::main%> must return %<int%>");
12490 : 60 : newtype = build_function_type (integer_type_node, oldtypeargs,
12491 : 20 : TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12492 : 20 : TREE_TYPE (decl) = newtype;
12493 : : }
12494 : 33354 : if (warn_main)
12495 : 33332 : check_main_parameter_types (decl);
12496 : : }
12497 : :
12498 : 174233816 : if (ctype != NULL_TREE && check)
12499 : : {
12500 : 9292509 : tree old_decl = check_classfn (ctype, decl,
12501 : 9292509 : (current_template_depth
12502 : 9292509 : > template_class_depth (ctype))
12503 : 2182015 : ? current_template_parms
12504 : : : NULL_TREE);
12505 : :
12506 : 9292509 : if (old_decl == error_mark_node)
12507 : : return NULL_TREE;
12508 : :
12509 : 9292425 : if (old_decl)
12510 : : {
12511 : 8876964 : tree ok;
12512 : 8876964 : tree pushed_scope;
12513 : :
12514 : 8876964 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12515 : : /* Because grokfndecl is always supposed to return a
12516 : : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12517 : : here. We depend on our callers to figure out that its
12518 : : really a template that's being returned. */
12519 : 2182003 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
12520 : :
12521 : 8876964 : if (DECL_STATIC_FUNCTION_P (old_decl)
12522 : 8876964 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12523 : : {
12524 : : /* Remove the `this' parm added by grokclassfn. */
12525 : 166642 : revert_static_member_fn (decl);
12526 : 166642 : check_static_quals (decl, quals);
12527 : : }
12528 : 8876964 : if (DECL_ARTIFICIAL (old_decl))
12529 : : {
12530 : 9 : error ("definition of implicitly-declared %qD", old_decl);
12531 : 9 : return NULL_TREE;
12532 : : }
12533 : 8876955 : else if (DECL_DEFAULTED_FN (old_decl))
12534 : : {
12535 : 3 : auto_diagnostic_group d;
12536 : 3 : error ("definition of explicitly-defaulted %q+D", decl);
12537 : 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12538 : : "%q#D explicitly defaulted here", old_decl);
12539 : 3 : return NULL_TREE;
12540 : 3 : }
12541 : :
12542 : : /* Since we've smashed OLD_DECL to its
12543 : : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12544 : 8876952 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12545 : 1028 : decl = DECL_TEMPLATE_RESULT (decl);
12546 : :
12547 : : /* Attempt to merge the declarations. This can fail, in
12548 : : the case of some invalid specialization declarations. */
12549 : 8876952 : pushed_scope = push_scope (ctype);
12550 : 8876952 : ok = duplicate_decls (decl, old_decl);
12551 : 8876952 : if (pushed_scope)
12552 : 8876952 : pop_scope (pushed_scope);
12553 : 8876952 : if (!ok)
12554 : : {
12555 : 0 : error ("no %q#D member function declared in class %qT",
12556 : : decl, ctype);
12557 : 0 : return NULL_TREE;
12558 : : }
12559 : 8876952 : if (ok == error_mark_node)
12560 : : return NULL_TREE;
12561 : : return old_decl;
12562 : : }
12563 : : }
12564 : :
12565 : 330713536 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12566 : : return NULL_TREE;
12567 : :
12568 : 165356765 : if (ctype == NULL_TREE || check)
12569 : 71736037 : return decl;
12570 : :
12571 : 93620728 : if (virtualp)
12572 : 4141718 : DECL_VIRTUAL_P (decl) = 1;
12573 : :
12574 : 93620728 : return decl;
12575 : : }
12576 : :
12577 : : /* decl is a FUNCTION_DECL.
12578 : : specifiers are the parsed virt-specifiers.
12579 : :
12580 : : Set flags to reflect the virt-specifiers.
12581 : :
12582 : : Returns decl. */
12583 : :
12584 : : static tree
12585 : 98202056 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12586 : : {
12587 : 98202056 : if (decl == NULL_TREE)
12588 : : return decl;
12589 : 98201959 : if (specifiers & VIRT_SPEC_OVERRIDE)
12590 : 193668 : DECL_OVERRIDE_P (decl) = 1;
12591 : 98201959 : if (specifiers & VIRT_SPEC_FINAL)
12592 : 3190 : DECL_FINAL_P (decl) = 1;
12593 : : return decl;
12594 : : }
12595 : :
12596 : : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12597 : : the linkage that DECL will receive in the object file. */
12598 : :
12599 : : static void
12600 : 11921072 : set_linkage_for_static_data_member (tree decl)
12601 : : {
12602 : : /* A static data member always has static storage duration and
12603 : : external linkage. Note that static data members are forbidden in
12604 : : local classes -- the only situation in which a class has
12605 : : non-external linkage. */
12606 : 11921072 : TREE_PUBLIC (decl) = 1;
12607 : 11921072 : TREE_STATIC (decl) = 1;
12608 : : /* For non-template classes, static data members are always put
12609 : : out in exactly those files where they are defined, just as
12610 : : with ordinary namespace-scope variables. */
12611 : 11921072 : if (!processing_template_decl)
12612 : 9070737 : DECL_INTERFACE_KNOWN (decl) = 1;
12613 : 11921072 : }
12614 : :
12615 : : /* Create a VAR_DECL named NAME with the indicated TYPE.
12616 : :
12617 : : If SCOPE is non-NULL, it is the class type or namespace containing
12618 : : the variable. If SCOPE is NULL, the variable should is created in
12619 : : the innermost enclosing scope. */
12620 : :
12621 : : static tree
12622 : 58858701 : grokvardecl (tree type,
12623 : : tree name,
12624 : : tree orig_declarator,
12625 : : const cp_decl_specifier_seq *declspecs,
12626 : : int initialized,
12627 : : int type_quals,
12628 : : int inlinep,
12629 : : bool conceptp,
12630 : : int template_count,
12631 : : tree scope,
12632 : : location_t location)
12633 : : {
12634 : 58858701 : tree decl;
12635 : 58858701 : tree explicit_scope;
12636 : :
12637 : 58858701 : gcc_assert (!name || identifier_p (name));
12638 : :
12639 : 58858701 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12640 : 58858701 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12641 : :
12642 : : /* Compute the scope in which to place the variable, but remember
12643 : : whether or not that scope was explicitly specified by the user. */
12644 : 58858701 : explicit_scope = scope;
12645 : 58858701 : if (!scope)
12646 : : {
12647 : : /* An explicit "extern" specifier indicates a namespace-scope
12648 : : variable. */
12649 : 58437590 : if (declspecs->storage_class == sc_extern)
12650 : 565589 : scope = current_decl_namespace ();
12651 : 57872001 : else if (!at_function_scope_p ())
12652 : 4803853 : scope = current_scope ();
12653 : : }
12654 : :
12655 : 58437590 : if (scope
12656 : 5790553 : && (/* If the variable is a namespace-scope variable declared in a
12657 : : template, we need DECL_LANG_SPECIFIC. */
12658 : 5790553 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
12659 : : /* Similarly for namespace-scope variables with language linkage
12660 : : other than C++. */
12661 : 5377267 : || (TREE_CODE (scope) == NAMESPACE_DECL
12662 : 2758987 : && current_lang_name != lang_name_cplusplus)
12663 : : /* Similarly for static data members. */
12664 : 2931341 : || TYPE_P (scope)
12665 : : /* Similarly for explicit specializations. */
12666 : 2518055 : || (orig_declarator
12667 : 2517610 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
12668 : 3747347 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
12669 : : else
12670 : 55111354 : decl = build_decl (location, VAR_DECL, name, type);
12671 : :
12672 : 58858701 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
12673 : 7825 : set_decl_namespace (decl, explicit_scope, 0);
12674 : : else
12675 : 58850876 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
12676 : :
12677 : 58858701 : if (declspecs->storage_class == sc_extern)
12678 : : {
12679 : 565589 : DECL_THIS_EXTERN (decl) = 1;
12680 : 565589 : DECL_EXTERNAL (decl) = !initialized;
12681 : : }
12682 : :
12683 : 58858701 : if (DECL_CLASS_SCOPE_P (decl))
12684 : : {
12685 : 413286 : set_linkage_for_static_data_member (decl);
12686 : : /* This function is only called with out-of-class definitions. */
12687 : 413286 : DECL_EXTERNAL (decl) = 0;
12688 : 413286 : check_class_member_definition_namespace (decl);
12689 : : }
12690 : : /* At top level, either `static' or no s.c. makes a definition
12691 : : (perhaps tentative), and absence of `static' makes it public. */
12692 : 58445415 : else if (toplevel_bindings_p ())
12693 : : {
12694 : 10752210 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
12695 : 5376105 : && (DECL_THIS_EXTERN (decl)
12696 : 4772934 : || ! constp
12697 : 4772934 : || volatilep
12698 : 4698337 : || inlinep
12699 : 409005 : || in_template_context
12700 : 27732 : || processing_specialization
12701 : 27641 : || module_attach_p ()));
12702 : 5376105 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
12703 : : }
12704 : : /* Not at top level, only `static' makes a static definition. */
12705 : : else
12706 : : {
12707 : 53069310 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
12708 : 53069310 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
12709 : : }
12710 : :
12711 : 58858701 : set_originating_module (decl);
12712 : :
12713 : 58858701 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
12714 : : {
12715 : 20963 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
12716 : : {
12717 : 20955 : CP_DECL_THREAD_LOCAL_P (decl) = true;
12718 : : // NB: Set a tentative TLS model to avoid tls_model attribute
12719 : : // warnings due to lack of thread storage duration. It will
12720 : : // be updated by cplus_decl_attributes later.
12721 : 20955 : if (!processing_template_decl)
12722 : 20833 : set_decl_tls_model (decl, TLS_MODEL_REAL);
12723 : : }
12724 : 20963 : if (declspecs->gnu_thread_keyword_p)
12725 : 19986 : SET_DECL_GNU_TLS_P (decl);
12726 : : }
12727 : :
12728 : : /* If the type of the decl has no linkage, make sure that we'll
12729 : : notice that in mark_used. */
12730 : 58858701 : if (cxx_dialect > cxx98
12731 : 58586798 : && decl_linkage (decl) != lk_none
12732 : 58002953 : && DECL_LANG_SPECIFIC (decl) == NULL
12733 : 54176622 : && !DECL_EXTERN_C_P (decl)
12734 : 113035323 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
12735 : 5150 : retrofit_lang_decl (decl);
12736 : :
12737 : 58858701 : if (TREE_PUBLIC (decl))
12738 : : {
12739 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12740 : : or enumeration declared in a local scope) shall not be used to
12741 : : declare an entity with linkage.
12742 : :
12743 : : DR 757 relaxes this restriction for C++0x. */
12744 : 5724171 : if (cxx_dialect < cxx11)
12745 : 21424 : no_linkage_error (decl);
12746 : : }
12747 : : else
12748 : 53134530 : DECL_INTERFACE_KNOWN (decl) = 1;
12749 : :
12750 : 58858701 : if (DECL_NAME (decl)
12751 : 58858701 : && MAIN_NAME_P (DECL_NAME (decl)))
12752 : : {
12753 : 12 : if (scope == global_namespace)
12754 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12755 : : "cannot declare %<::main%> to be a global variable");
12756 : 6 : else if (DECL_EXTERN_C_P (decl))
12757 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
12758 : : "an entity named %<main%> cannot be declared with "
12759 : : "C language linkage");
12760 : : }
12761 : :
12762 : : /* Check if a variable is being declared as a concept. */
12763 : 58858701 : if (conceptp)
12764 : : {
12765 : 30 : if (!processing_template_decl)
12766 : 13 : error_at (declspecs->locations[ds_concept],
12767 : : "a non-template variable cannot be %<concept%>");
12768 : 17 : else if (!at_namespace_scope_p ())
12769 : 2 : error_at (declspecs->locations[ds_concept],
12770 : : "concept must be defined at namespace scope");
12771 : : else
12772 : 15 : error_at (declspecs->locations[ds_concept],
12773 : : "variable concepts are no longer supported");
12774 : 30 : return NULL_TREE;
12775 : : }
12776 : 58858671 : else if (flag_concepts
12777 : 58858671 : && current_template_depth > template_class_depth (scope))
12778 : : {
12779 : 19334669 : tree ci = current_template_constraints ();
12780 : 19334669 : set_constraints (decl, ci);
12781 : : }
12782 : :
12783 : : // Handle explicit specializations and instantiations of variable templates.
12784 : 58858671 : if (orig_declarator)
12785 : 58267002 : decl = check_explicit_specialization (orig_declarator, decl,
12786 : : template_count, 0);
12787 : :
12788 : 58858671 : return decl != error_mark_node ? decl : NULL_TREE;
12789 : : }
12790 : :
12791 : : /* Create and return a canonical pointer to member function type, for
12792 : : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
12793 : :
12794 : : tree
12795 : 3315931 : build_ptrmemfunc_type (tree type)
12796 : : {
12797 : 3315931 : tree field, fields;
12798 : 3315931 : tree t;
12799 : :
12800 : 3315931 : if (type == error_mark_node)
12801 : : return type;
12802 : :
12803 : : /* Make sure that we always have the unqualified pointer-to-member
12804 : : type first. */
12805 : 3315931 : if (cp_cv_quals quals = cp_type_quals (type))
12806 : : {
12807 : 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
12808 : 0 : return cp_build_qualified_type (unqual, quals);
12809 : : }
12810 : :
12811 : : /* If a canonical type already exists for this type, use it. We use
12812 : : this method instead of type_hash_canon, because it only does a
12813 : : simple equality check on the list of field members. */
12814 : :
12815 : 3315931 : t = TYPE_PTRMEMFUNC_TYPE (type);
12816 : 3315931 : if (t)
12817 : : return t;
12818 : :
12819 : 1081872 : t = make_node (RECORD_TYPE);
12820 : :
12821 : : /* Let the front end know this is a pointer to member function. */
12822 : 1081872 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
12823 : :
12824 : 1081872 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
12825 : 1081872 : DECL_NONADDRESSABLE_P (field) = 1;
12826 : 1081872 : fields = field;
12827 : :
12828 : 1081872 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
12829 : : delta_type_node);
12830 : 1081872 : DECL_NONADDRESSABLE_P (field) = 1;
12831 : 1081872 : DECL_CHAIN (field) = fields;
12832 : 1081872 : fields = field;
12833 : :
12834 : 1081872 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
12835 : :
12836 : : /* Zap out the name so that the back end will give us the debugging
12837 : : information for this anonymous RECORD_TYPE. */
12838 : 1081872 : TYPE_NAME (t) = NULL_TREE;
12839 : :
12840 : : /* Cache this pointer-to-member type so that we can find it again
12841 : : later. */
12842 : 1081872 : TYPE_PTRMEMFUNC_TYPE (type) = t;
12843 : :
12844 : 1081872 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
12845 : 540524 : SET_TYPE_STRUCTURAL_EQUALITY (t);
12846 : 541348 : else if (TYPE_CANONICAL (type) != type)
12847 : 351807 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
12848 : :
12849 : : return t;
12850 : : }
12851 : :
12852 : : /* Create and return a pointer to data member type. */
12853 : :
12854 : : tree
12855 : 322778 : build_ptrmem_type (tree class_type, tree member_type)
12856 : : {
12857 : 322778 : if (TREE_CODE (member_type) == METHOD_TYPE)
12858 : : {
12859 : 63226 : cp_cv_quals quals = type_memfn_quals (member_type);
12860 : 63226 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
12861 : 63226 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
12862 : 63226 : return build_ptrmemfunc_type (build_pointer_type (member_type));
12863 : : }
12864 : : else
12865 : : {
12866 : 259552 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
12867 : 259552 : return build_offset_type (class_type, member_type);
12868 : : }
12869 : : }
12870 : :
12871 : : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
12872 : : Check to see that the definition is valid. Issue appropriate error
12873 : : messages. */
12874 : :
12875 : : static void
12876 : 15028380 : check_static_variable_definition (tree decl, tree type)
12877 : : {
12878 : : /* Avoid redundant diagnostics on out-of-class definitions. */
12879 : 15028380 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
12880 : : ;
12881 : : /* Can't check yet if we don't know the type. */
12882 : 14072854 : else if (dependent_type_p (type))
12883 : : ;
12884 : : /* If DECL is declared constexpr, we'll do the appropriate checks
12885 : : in check_initializer. Similarly for inline static data members. */
12886 : 13768955 : else if (DECL_P (decl)
12887 : 13768955 : && (DECL_DECLARED_CONSTEXPR_P (decl)
12888 : 2426880 : || DECL_VAR_DECLARED_INLINE_P (decl)))
12889 : : ;
12890 : 2426312 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12891 : : {
12892 : 56 : if (!COMPLETE_TYPE_P (type))
12893 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12894 : : "in-class initialization of static data member %q#D of "
12895 : : "incomplete type", decl);
12896 : 45 : else if (literal_type_p (type))
12897 : 39 : permerror (DECL_SOURCE_LOCATION (decl),
12898 : : "%<constexpr%> needed for in-class initialization of "
12899 : : "static data member %q#D of non-integral type", decl);
12900 : : else
12901 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12902 : : "in-class initialization of static data member %q#D of "
12903 : : "non-literal type", decl);
12904 : : }
12905 : : /* Motion 10 at San Diego: If a static const integral data member is
12906 : : initialized with an integral constant expression, the initializer
12907 : : may appear either in the declaration (within the class), or in
12908 : : the definition, but not both. If it appears in the class, the
12909 : : member is a member constant. The file-scope definition is always
12910 : : required. */
12911 : 2426256 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
12912 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12913 : : "invalid in-class initialization of static data member "
12914 : : "of non-integral type %qT",
12915 : : type);
12916 : 2426245 : else if (!CP_TYPE_CONST_P (type))
12917 : 5 : error_at (DECL_SOURCE_LOCATION (decl),
12918 : : "ISO C++ forbids in-class initialization of non-const "
12919 : : "static member %qD",
12920 : : decl);
12921 : 2426240 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12922 : 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12923 : : "ISO C++ forbids initialization of member constant "
12924 : : "%qD of non-integral type %qT", decl, type);
12925 : 15028380 : }
12926 : :
12927 : : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
12928 : : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
12929 : : expressions out into temporary variables so that walk_tree doesn't
12930 : : step into them (c++/15764). */
12931 : :
12932 : : static tree
12933 : 3306 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
12934 : : {
12935 : 3306 : hash_set<tree> *pset = (hash_set<tree> *)data;
12936 : 3306 : tree expr = *expr_p;
12937 : 3306 : if (TREE_CODE (expr) == SAVE_EXPR)
12938 : : {
12939 : 1434 : tree op = TREE_OPERAND (expr, 0);
12940 : 1434 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
12941 : 1434 : if (TREE_SIDE_EFFECTS (op))
12942 : 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
12943 : 1434 : *walk_subtrees = 0;
12944 : : }
12945 : 1872 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
12946 : 1697 : *walk_subtrees = 0;
12947 : 3306 : return NULL;
12948 : : }
12949 : :
12950 : : /* Entry point for the above. */
12951 : :
12952 : : static void
12953 : 1493 : stabilize_vla_size (tree size)
12954 : : {
12955 : 1493 : hash_set<tree> pset;
12956 : : /* Break out any function calls into temporary variables. */
12957 : 1493 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12958 : 1493 : }
12959 : :
12960 : : /* Reduce a SIZEOF_EXPR to its value. */
12961 : :
12962 : : tree
12963 : 10701788 : fold_sizeof_expr (tree t)
12964 : : {
12965 : 10701788 : tree r;
12966 : 10701788 : if (SIZEOF_EXPR_TYPE_P (t))
12967 : 9951283 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12968 : 9951283 : TREE_TYPE (TREE_OPERAND (t, 0)),
12969 : : SIZEOF_EXPR, false, false);
12970 : 750505 : else if (TYPE_P (TREE_OPERAND (t, 0)))
12971 : 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12972 : 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12973 : : false, false);
12974 : : else
12975 : 750505 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12976 : 750505 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12977 : : false, false);
12978 : 10701788 : if (r == error_mark_node)
12979 : 78 : r = size_one_node;
12980 : 10701788 : r = cp_fold_convert (size_type_node, r);
12981 : 10701788 : return r;
12982 : : }
12983 : :
12984 : : /* Given the SIZE (i.e., number of elements) in an array, compute
12985 : : an appropriate index type for the array. If non-NULL, NAME is
12986 : : the name of the entity being declared. */
12987 : :
12988 : : static tree
12989 : 2859490 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12990 : : tsubst_flags_t complain)
12991 : : {
12992 : 2859490 : if (error_operand_p (size))
12993 : 37 : return error_mark_node;
12994 : :
12995 : : /* The type of the index being computed. */
12996 : 2859453 : tree itype;
12997 : :
12998 : : /* The original numeric size as seen in the source code before
12999 : : conversion to size_t. */
13000 : 2859453 : tree origsize = size;
13001 : :
13002 : 2859453 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
13003 : :
13004 : 2859453 : if (!type_dependent_expression_p (size))
13005 : : {
13006 : 2818877 : origsize = size = mark_rvalue_use (size);
13007 : :
13008 : 36225 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
13009 : 2819037 : && TREE_SIDE_EFFECTS (size))
13010 : : /* In C++98, we mark a non-constant array bound with a magic
13011 : : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
13012 : : else
13013 : : {
13014 : 2818876 : size = build_converted_constant_expr (size_type_node, size, complain);
13015 : : /* Pedantically a constant expression is required here and so
13016 : : __builtin_is_constant_evaluated () should fold to true if it
13017 : : is successfully folded into a constant. */
13018 : 2818876 : size = fold_non_dependent_expr (size, complain,
13019 : : /*manifestly_const_eval=*/true);
13020 : :
13021 : 2818876 : if (!TREE_CONSTANT (size))
13022 : 121067 : size = origsize;
13023 : : }
13024 : :
13025 : 2818877 : if (error_operand_p (size))
13026 : 3 : return error_mark_node;
13027 : :
13028 : : /* The array bound must be an integer type. */
13029 : 2818874 : tree type = TREE_TYPE (size);
13030 : 2818874 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
13031 : : {
13032 : 21 : if (!(complain & tf_error))
13033 : 0 : return error_mark_node;
13034 : 21 : if (name)
13035 : 18 : error_at (loc, "size of array %qD has non-integral type %qT",
13036 : : name, type);
13037 : : else
13038 : 3 : error_at (loc, "size of array has non-integral type %qT", type);
13039 : 21 : size = integer_one_node;
13040 : : }
13041 : : }
13042 : :
13043 : : /* A type is dependent if it is...an array type constructed from any
13044 : : dependent type or whose size is specified by a constant expression
13045 : : that is value-dependent. */
13046 : : /* We can only call value_dependent_expression_p on integral constant
13047 : : expressions. */
13048 : 2859450 : if (processing_template_decl
13049 : 1222560 : && potential_constant_expression (size)
13050 : 4081985 : && value_dependent_expression_p (size))
13051 : : {
13052 : : /* Just build the index type and mark that it requires
13053 : : structural equality checks. */
13054 : 720873 : in_template:
13055 : 720873 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13056 : : size, size_one_node));
13057 : 720873 : TYPE_DEPENDENT_P (itype) = 1;
13058 : 720873 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13059 : 720873 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
13060 : 720873 : return itype;
13061 : : }
13062 : :
13063 : 2153484 : if (TREE_CODE (size) != INTEGER_CST)
13064 : : {
13065 : 16800 : tree folded = cp_fully_fold (size);
13066 : 16800 : if (TREE_CODE (folded) == INTEGER_CST)
13067 : : {
13068 : 211 : if (name)
13069 : 137 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13070 : : "integral constant-expression", name);
13071 : : else
13072 : 74 : pedwarn (loc, OPT_Wpedantic,
13073 : : "size of array is not an integral constant-expression");
13074 : : }
13075 : 16800 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13076 : : /* We might have lost the TREE_CONSTANT flag e.g. when we are
13077 : : folding a conversion from a pointer to integral type. In that
13078 : : case issue an error below and don't treat this as a VLA. */;
13079 : : else
13080 : : /* Use the folded result for VLAs, too; it will have resolved
13081 : : SIZEOF_EXPR. */
13082 : : size = folded;
13083 : : }
13084 : :
13085 : : /* Normally, the array-bound will be a constant. */
13086 : 2153484 : if (TREE_CODE (size) == INTEGER_CST)
13087 : : {
13088 : : /* The size to use in diagnostics that reflects the constant
13089 : : size used in the source, rather than SIZE massaged above. */
13090 : 2136895 : tree diagsize = size;
13091 : :
13092 : : /* If the original size before conversion to size_t was signed
13093 : : and negative, convert it to ssizetype to restore the sign. */
13094 : 2136895 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13095 : : && TREE_CODE (size) == INTEGER_CST
13096 : 2136895 : && tree_int_cst_sign_bit (size))
13097 : : {
13098 : 230 : diagsize = fold_convert (ssizetype, size);
13099 : :
13100 : : /* Clear the overflow bit that may have been set as a result
13101 : : of the conversion from the sizetype of the new size to
13102 : : ssizetype. */
13103 : 230 : TREE_OVERFLOW (diagsize) = false;
13104 : : }
13105 : :
13106 : : /* Verify that the array has a positive number of elements
13107 : : and issue the appropriate diagnostic if it doesn't. */
13108 : 2136895 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13109 : : {
13110 : 461 : if (!(complain & tf_error))
13111 : 15 : return error_mark_node;
13112 : 446 : size = integer_one_node;
13113 : : }
13114 : : /* As an extension we allow zero-sized arrays. */
13115 : 2136434 : else if (integer_zerop (size))
13116 : : {
13117 : 3171 : if (!(complain & tf_error))
13118 : : /* We must fail if performing argument deduction (as
13119 : : indicated by the state of complain), so that
13120 : : another substitution can be found. */
13121 : 8 : return error_mark_node;
13122 : 3163 : else if (name)
13123 : 2893 : pedwarn (loc, OPT_Wpedantic,
13124 : : "ISO C++ forbids zero-size array %qD", name);
13125 : : else
13126 : 270 : pedwarn (loc, OPT_Wpedantic,
13127 : : "ISO C++ forbids zero-size array");
13128 : : }
13129 : : }
13130 : 16589 : else if (TREE_CONSTANT (size)
13131 : : /* We don't allow VLAs at non-function scopes, or during
13132 : : tentative template substitution. */
13133 : 16574 : || !at_function_scope_p ()
13134 : 32995 : || !(complain & tf_error))
13135 : : {
13136 : 189 : if (!(complain & tf_error))
13137 : 144 : return error_mark_node;
13138 : : /* `(int) &fn' is not a valid array bound. */
13139 : 45 : if (name)
13140 : 37 : error_at (loc,
13141 : : "size of array %qD is not an integral constant-expression",
13142 : : name);
13143 : : else
13144 : 8 : error_at (loc, "size of array is not an integral constant-expression");
13145 : 45 : size = integer_one_node;
13146 : : }
13147 : 16400 : else if (pedantic && warn_vla != 0)
13148 : : {
13149 : 30 : if (name)
13150 : 15 : pedwarn (name_loc, OPT_Wvla,
13151 : : "ISO C++ forbids variable length array %qD", name);
13152 : : else
13153 : 15 : pedwarn (input_location, OPT_Wvla,
13154 : : "ISO C++ forbids variable length array");
13155 : : }
13156 : 16370 : else if (warn_vla > 0)
13157 : : {
13158 : 3 : if (name)
13159 : 3 : warning_at (name_loc, OPT_Wvla,
13160 : : "variable length array %qD is used", name);
13161 : : else
13162 : 0 : warning (OPT_Wvla,
13163 : : "variable length array is used");
13164 : : }
13165 : :
13166 : 2153317 : if (processing_template_decl && !TREE_CONSTANT (size))
13167 : 14907 : goto in_template;
13168 : : else
13169 : : {
13170 : 2138410 : if (!TREE_CONSTANT (size))
13171 : : {
13172 : : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13173 : : of the MINUS_EXPR, which allows the -1 to get folded with the +1
13174 : : that happens when building TYPE_SIZE. */
13175 : 1493 : size = variable_size (size);
13176 : 1493 : stabilize_vla_size (size);
13177 : : }
13178 : :
13179 : : /* Compute the index of the largest element in the array. It is
13180 : : one less than the number of elements in the array. We save
13181 : : and restore PROCESSING_TEMPLATE_DECL so that computations in
13182 : : cp_build_binary_op will be appropriately folded. */
13183 : 2138410 : {
13184 : 2138410 : processing_template_decl_sentinel s;
13185 : 2138410 : itype = cp_build_binary_op (input_location,
13186 : : MINUS_EXPR,
13187 : : cp_convert (ssizetype, size, complain),
13188 : : cp_convert (ssizetype, integer_one_node,
13189 : : complain),
13190 : : complain);
13191 : 2138410 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13192 : 2138410 : }
13193 : :
13194 : 2138410 : if (!TREE_CONSTANT (itype))
13195 : : {
13196 : 1493 : if (sanitize_flags_p (SANITIZE_VLA)
13197 : 1493 : && current_function_decl != NULL_TREE)
13198 : : {
13199 : : /* We have to add 1 -- in the ubsan routine we generate
13200 : : LE_EXPR rather than LT_EXPR. */
13201 : 78 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13202 : : build_one_cst (TREE_TYPE (itype)));
13203 : 78 : t = ubsan_instrument_vla (input_location, t);
13204 : 78 : finish_expr_stmt (t);
13205 : : }
13206 : : }
13207 : : /* Make sure that there was no overflow when creating to a signed
13208 : : index type. (For example, on a 32-bit machine, an array with
13209 : : size 2^32 - 1 is too big.) */
13210 : 2136917 : else if (TREE_CODE (itype) == INTEGER_CST
13211 : 2136917 : && TREE_OVERFLOW (itype))
13212 : : {
13213 : 0 : if (!(complain & tf_error))
13214 : 0 : return error_mark_node;
13215 : 0 : error ("overflow in array dimension");
13216 : 0 : TREE_OVERFLOW (itype) = 0;
13217 : : }
13218 : : }
13219 : :
13220 : : /* Create and return the appropriate index type. */
13221 : 2138410 : itype = build_index_type (itype);
13222 : :
13223 : : /* If the index type were dependent, we would have returned early, so
13224 : : remember that it isn't. */
13225 : 2138410 : TYPE_DEPENDENT_P (itype) = 0;
13226 : 2138410 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13227 : 2138410 : return itype;
13228 : : }
13229 : :
13230 : : tree
13231 : 360104 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13232 : : {
13233 : 360104 : return compute_array_index_type_loc (input_location, name, size, complain);
13234 : : }
13235 : :
13236 : : /* Returns the scope (if any) in which the entity declared by
13237 : : DECLARATOR will be located. If the entity was declared with an
13238 : : unqualified name, NULL_TREE is returned. */
13239 : :
13240 : : tree
13241 : 706790669 : get_scope_of_declarator (const cp_declarator *declarator)
13242 : : {
13243 : 824378413 : while (declarator && declarator->kind != cdk_id)
13244 : 117587744 : declarator = declarator->declarator;
13245 : :
13246 : : /* If the declarator-id is a SCOPE_REF, the scope in which the
13247 : : declaration occurs is the first operand. */
13248 : 706790669 : if (declarator
13249 : 705549630 : && declarator->u.id.qualifying_scope)
13250 : : return declarator->u.id.qualifying_scope;
13251 : :
13252 : : /* Otherwise, the declarator is not a qualified name; the entity will
13253 : : be declared in the current scope. */
13254 : : return NULL_TREE;
13255 : : }
13256 : :
13257 : : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
13258 : : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13259 : : with this type. */
13260 : :
13261 : : static tree
13262 : 3246787 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13263 : : {
13264 : 3246787 : tree itype = NULL_TREE;
13265 : :
13266 : : /* If things have already gone awry, bail now. */
13267 : 3246787 : if (type == error_mark_node || size == error_mark_node)
13268 : : return error_mark_node;
13269 : :
13270 : : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13271 : : for a deduced class type. */
13272 : 3246724 : if (template_placeholder_p (type))
13273 : : {
13274 : 6 : if (name)
13275 : 0 : error_at (loc, "%qD declared as array of template placeholder "
13276 : : "type %qT", name, type);
13277 : : else
13278 : 6 : error ("creating array of template placeholder type %qT", type);
13279 : 6 : return error_mark_node;
13280 : : }
13281 : :
13282 : : /* If there are some types which cannot be array elements,
13283 : : issue an error-message and return. */
13284 : 3246718 : switch (TREE_CODE (type))
13285 : : {
13286 : 3 : case VOID_TYPE:
13287 : 3 : if (name)
13288 : 0 : error_at (loc, "declaration of %qD as array of void", name);
13289 : : else
13290 : 3 : error ("creating array of void");
13291 : 3 : return error_mark_node;
13292 : :
13293 : 6 : case FUNCTION_TYPE:
13294 : 6 : if (name)
13295 : 6 : error_at (loc, "declaration of %qD as array of functions", name);
13296 : : else
13297 : 0 : error ("creating array of functions");
13298 : 6 : return error_mark_node;
13299 : :
13300 : 21 : case REFERENCE_TYPE:
13301 : 21 : if (name)
13302 : 15 : error_at (loc, "declaration of %qD as array of references", name);
13303 : : else
13304 : 6 : error ("creating array of references");
13305 : 21 : return error_mark_node;
13306 : :
13307 : 0 : case METHOD_TYPE:
13308 : 0 : if (name)
13309 : 0 : error_at (loc, "declaration of %qD as array of function members",
13310 : : name);
13311 : : else
13312 : 0 : error ("creating array of function members");
13313 : 0 : return error_mark_node;
13314 : :
13315 : 3246688 : default:
13316 : 3246688 : break;
13317 : : }
13318 : :
13319 : 3246688 : if (!verify_type_context (name ? loc : input_location,
13320 : : TCTX_ARRAY_ELEMENT, type))
13321 : 0 : return error_mark_node;
13322 : :
13323 : : /* [dcl.array]
13324 : :
13325 : : The constant expressions that specify the bounds of the arrays
13326 : : can be omitted only for the first member of the sequence. */
13327 : 3246688 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13328 : : {
13329 : 30 : if (name)
13330 : 24 : error_at (loc, "declaration of %qD as multidimensional array must "
13331 : : "have bounds for all dimensions except the first",
13332 : : name);
13333 : : else
13334 : 6 : error ("multidimensional array must have bounds for all "
13335 : : "dimensions except the first");
13336 : :
13337 : 30 : return error_mark_node;
13338 : : }
13339 : :
13340 : : /* Figure out the index type for the array. */
13341 : 3246658 : if (size)
13342 : : {
13343 : 2499386 : itype = compute_array_index_type_loc (loc, name, size,
13344 : : tf_warning_or_error);
13345 : 2499386 : if (type_uses_auto (type)
13346 : 2499386 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13347 : : {
13348 : 9 : sorry_at (loc, "variable-length array of %<auto%>");
13349 : 9 : return error_mark_node;
13350 : : }
13351 : : }
13352 : :
13353 : 3246649 : return build_cplus_array_type (type, itype);
13354 : : }
13355 : :
13356 : : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
13357 : :
13358 : : static location_t
13359 : 954981322 : min_location (location_t loca, location_t locb)
13360 : : {
13361 : 954981322 : if (loca == UNKNOWN_LOCATION
13362 : 954981322 : || (locb != UNKNOWN_LOCATION
13363 : 104996230 : && linemap_location_before_p (line_table, locb, loca)))
13364 : 851337060 : return locb;
13365 : : return loca;
13366 : : }
13367 : :
13368 : : /* Returns the smallest location != UNKNOWN_LOCATION among the
13369 : : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13370 : : and LOCATIONS[ds_restrict]. */
13371 : :
13372 : : static location_t
13373 : 953323359 : smallest_type_quals_location (int type_quals, const location_t* locations)
13374 : : {
13375 : 953323359 : location_t loc = UNKNOWN_LOCATION;
13376 : :
13377 : 953323359 : if (type_quals & TYPE_QUAL_CONST)
13378 : 103530650 : loc = locations[ds_const];
13379 : :
13380 : 953323359 : if (type_quals & TYPE_QUAL_VOLATILE)
13381 : 1657872 : loc = min_location (loc, locations[ds_volatile]);
13382 : :
13383 : 953323359 : if (type_quals & TYPE_QUAL_RESTRICT)
13384 : 43 : loc = min_location (loc, locations[ds_restrict]);
13385 : :
13386 : 953323359 : return loc;
13387 : : }
13388 : :
13389 : : /* Returns the smallest among the latter and locations[ds_type_spec]. */
13390 : :
13391 : : static location_t
13392 : 953323338 : smallest_type_location (int type_quals, const location_t* locations)
13393 : : {
13394 : 953323338 : location_t loc = smallest_type_quals_location (type_quals, locations);
13395 : 953323338 : return min_location (loc, locations[ds_type_spec]);
13396 : : }
13397 : :
13398 : : static location_t
13399 : 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
13400 : : {
13401 : 26 : int type_quals = get_type_quals (declspecs);
13402 : 26 : return smallest_type_location (type_quals, declspecs->locations);
13403 : : }
13404 : :
13405 : : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
13406 : : strip out the pointer/reference declarator(s). */
13407 : :
13408 : : static bool
13409 : 27845205 : maybe_strip_indirect_ref (const cp_declarator** declarator)
13410 : : {
13411 : 27845205 : bool indirect_ref_p = false;
13412 : 55690557 : while (declarator && *declarator
13413 : 27845352 : && ((*declarator)->kind == cdk_pointer
13414 : 27845352 : || (*declarator)->kind == cdk_reference))
13415 : : {
13416 : 147 : indirect_ref_p = true;
13417 : 147 : *declarator = (*declarator)->declarator;
13418 : : }
13419 : 27845205 : return indirect_ref_p;
13420 : : }
13421 : :
13422 : : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13423 : : and DECLARATOR. SFK indicates the kind of special function (if any) that
13424 : : this function is. OPTYPE is the type given in a conversion operator
13425 : : declaration, or the class type for a constructor/destructor.
13426 : : Returns the actual return type of the function; that may be different
13427 : : than TYPE if an error occurs, or for certain special functions. */
13428 : :
13429 : : static tree
13430 : 27845205 : check_special_function_return_type (special_function_kind sfk,
13431 : : tree type,
13432 : : tree optype,
13433 : : int type_quals,
13434 : : const cp_declarator** declarator,
13435 : : const location_t* locations)
13436 : : {
13437 : 27845205 : gcc_assert (declarator);
13438 : 27845205 : location_t rettype_loc = (type
13439 : 27845205 : ? smallest_type_location (type_quals, locations)
13440 : 27845124 : : (*declarator)->id_loc);
13441 : 27845205 : switch (sfk)
13442 : : {
13443 : 22819852 : case sfk_constructor:
13444 : 22819852 : if (maybe_strip_indirect_ref (declarator) || type)
13445 : 81 : error_at (rettype_loc,
13446 : : "return type specification for constructor invalid");
13447 : 22819771 : else if (type_quals != TYPE_UNQUALIFIED)
13448 : 15 : error_at (smallest_type_quals_location (type_quals, locations),
13449 : : "qualifiers are not allowed on constructor declaration");
13450 : :
13451 : 22819852 : if (targetm.cxx.cdtor_returns_this ())
13452 : 0 : type = build_pointer_type (optype);
13453 : : else
13454 : 22819852 : type = void_type_node;
13455 : : break;
13456 : :
13457 : 3236315 : case sfk_destructor:
13458 : 3236315 : if (maybe_strip_indirect_ref (declarator) || type)
13459 : 45 : error_at (rettype_loc,
13460 : : "return type specification for destructor invalid");
13461 : 3236270 : else if (type_quals != TYPE_UNQUALIFIED)
13462 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
13463 : : "qualifiers are not allowed on destructor declaration");
13464 : :
13465 : : /* We can't use the proper return type here because we run into
13466 : : problems with ambiguous bases and covariant returns. */
13467 : 3236315 : if (targetm.cxx.cdtor_returns_this ())
13468 : 0 : type = build_pointer_type (void_type_node);
13469 : : else
13470 : 3236315 : type = void_type_node;
13471 : : break;
13472 : :
13473 : 676826 : case sfk_conversion:
13474 : 676826 : if (maybe_strip_indirect_ref (declarator) || type)
13475 : 42 : error_at (rettype_loc,
13476 : : "return type specified for %<operator %T%>", optype);
13477 : 676784 : else if (type_quals != TYPE_UNQUALIFIED)
13478 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
13479 : : "qualifiers are not allowed on declaration of "
13480 : : "%<operator %T%>", optype);
13481 : :
13482 : : type = optype;
13483 : : break;
13484 : :
13485 : 1112212 : case sfk_deduction_guide:
13486 : 1112212 : if (maybe_strip_indirect_ref (declarator) || type)
13487 : 0 : error_at (rettype_loc,
13488 : : "return type specified for deduction guide");
13489 : 1112212 : else if (type_quals != TYPE_UNQUALIFIED)
13490 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
13491 : : "qualifiers are not allowed on declaration of "
13492 : : "deduction guide");
13493 : 1112212 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13494 : : {
13495 : 3 : error ("template template parameter %qT in declaration of "
13496 : : "deduction guide", optype);
13497 : 3 : type = error_mark_node;
13498 : : }
13499 : : else
13500 : 1112209 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13501 : 30029724 : for (int i = 0; i < ds_last; ++i)
13502 : 28917512 : if (i != ds_explicit && locations[i])
13503 : 3 : error_at (locations[i],
13504 : : "%<decl-specifier%> in declaration of deduction guide");
13505 : : break;
13506 : :
13507 : 0 : default:
13508 : 0 : gcc_unreachable ();
13509 : : }
13510 : :
13511 : 27845205 : return type;
13512 : : }
13513 : :
13514 : : /* A variable or data member (whose unqualified name is IDENTIFIER)
13515 : : has been declared with the indicated TYPE. If the TYPE is not
13516 : : acceptable, issue an error message and return a type to use for
13517 : : error-recovery purposes. */
13518 : :
13519 : : tree
13520 : 297881431 : check_var_type (tree identifier, tree type, location_t loc)
13521 : : {
13522 : 297881431 : if (VOID_TYPE_P (type))
13523 : : {
13524 : 82 : if (!identifier)
13525 : 0 : error_at (loc, "unnamed variable or field declared void");
13526 : 82 : else if (identifier_p (identifier))
13527 : : {
13528 : 82 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13529 : 82 : error_at (loc, "variable or field %qE declared void",
13530 : : identifier);
13531 : : }
13532 : : else
13533 : 0 : error_at (loc, "variable or field declared void");
13534 : 82 : type = error_mark_node;
13535 : : }
13536 : :
13537 : 297881431 : return type;
13538 : : }
13539 : :
13540 : : /* Handle declaring DECL as an inline variable. */
13541 : :
13542 : : static void
13543 : 4366185 : mark_inline_variable (tree decl, location_t loc)
13544 : : {
13545 : 4366185 : bool inlinep = true;
13546 : 4366185 : if (! toplevel_bindings_p ())
13547 : : {
13548 : 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13549 : : "%qD declared at block scope", decl);
13550 : 24 : inlinep = false;
13551 : : }
13552 : 4366161 : else if (cxx_dialect < cxx17)
13553 : 648 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13554 : : "with %<-std=c++17%> or %<-std=gnu++17%>");
13555 : 672 : if (inlinep)
13556 : : {
13557 : 4366161 : retrofit_lang_decl (decl);
13558 : 4366161 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13559 : : }
13560 : 4366185 : }
13561 : :
13562 : :
13563 : : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13564 : : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13565 : : ORIG is the anonymous TYPE_NAME before that change. */
13566 : :
13567 : : static bool
13568 : 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13569 : : {
13570 : 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13571 : 42 : tree name = DECL_NAME (TYPE_NAME (type));
13572 : 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13573 : 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13574 : : "anonymous non-C-compatible type given name for linkage "
13575 : 42 : "purposes by %<typedef%> declaration");
13576 : 42 : }
13577 : :
13578 : : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13579 : : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13580 : : to TYPE but during recursion can be set to nested classes. */
13581 : :
13582 : : static bool
13583 : 526907 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13584 : : {
13585 : 526907 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13586 : : {
13587 : 6 : auto_diagnostic_group d;
13588 : 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13589 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13590 : : "type is not C-compatible because it has a base class");
13591 : 6 : return true;
13592 : 6 : }
13593 : 2494961 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13594 : 1968121 : switch (TREE_CODE (field))
13595 : : {
13596 : 9 : case VAR_DECL:
13597 : : /* static data members have been diagnosed already. */
13598 : 9 : continue;
13599 : 1311312 : case FIELD_DECL:
13600 : 1311312 : if (DECL_INITIAL (field))
13601 : : {
13602 : 4 : auto_diagnostic_group d;
13603 : 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13604 : 4 : inform (DECL_SOURCE_LOCATION (field),
13605 : : "type is not C-compatible because %qD has default "
13606 : : "member initializer", field);
13607 : 4 : return true;
13608 : 4 : }
13609 : 1311308 : continue;
13610 : 9 : case CONST_DECL:
13611 : 9 : continue;
13612 : 656714 : case TYPE_DECL:
13613 : 656714 : if (DECL_SELF_REFERENCE_P (field))
13614 : 526875 : continue;
13615 : 129839 : if (DECL_IMPLICIT_TYPEDEF_P (field))
13616 : : {
13617 : 129829 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13618 : 3 : continue;
13619 : 129826 : if (CLASS_TYPE_P (TREE_TYPE (field)))
13620 : : {
13621 : 129826 : tree tf = TREE_TYPE (field);
13622 : 129826 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13623 : : tf))
13624 : : return true;
13625 : 129801 : continue;
13626 : 129801 : }
13627 : : }
13628 : : /* FALLTHRU */
13629 : 32 : case FUNCTION_DECL:
13630 : 32 : case TEMPLATE_DECL:
13631 : 32 : {
13632 : 32 : auto_diagnostic_group d;
13633 : 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13634 : 32 : inform (DECL_SOURCE_LOCATION (field),
13635 : : "type is not C-compatible because it contains %qD "
13636 : : "declaration", field);
13637 : 32 : return true;
13638 : 32 : }
13639 : : default:
13640 : : break;
13641 : 1311326 : }
13642 : : return false;
13643 : : }
13644 : :
13645 : : /* Assign a typedef-given name to a class or enumeration type declared
13646 : : as anonymous at first. This was split out of grokdeclarator
13647 : : because it is also used in libcc1. */
13648 : :
13649 : : void
13650 : 398861 : name_unnamed_type (tree type, tree decl)
13651 : : {
13652 : 1196583 : gcc_assert (TYPE_UNNAMED_P (type)
13653 : : || enum_with_enumerator_for_linkage_p (type));
13654 : :
13655 : : /* Replace the anonymous decl with the real decl. Be careful not to
13656 : : rename other typedefs (such as the self-reference) of type. */
13657 : 398861 : tree orig = TYPE_NAME (type);
13658 : 1194862 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13659 : 796001 : if (TYPE_NAME (t) == orig)
13660 : 398861 : TYPE_NAME (t) = decl;
13661 : :
13662 : : /* If this is a typedef within a template class, the nested
13663 : : type is a (non-primary) template. The name for the
13664 : : template needs updating as well. */
13665 : 398861 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
13666 : 64 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
13667 : :
13668 : : /* Adjust linkage now that we aren't unnamed anymore. */
13669 : 398861 : reset_type_linkage (type);
13670 : :
13671 : 398861 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
13672 : 397081 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
13673 : :
13674 : : /* FIXME remangle member functions; member functions of a
13675 : : type with external linkage have external linkage. */
13676 : :
13677 : : /* Check that our job is done, and that it would fail if we
13678 : : attempted to do it again. */
13679 : 797722 : gcc_assert (!TYPE_UNNAMED_P (type)
13680 : : && !enum_with_enumerator_for_linkage_p (type));
13681 : 398861 : }
13682 : :
13683 : : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
13684 : : is allowed. TYPE might contain a decltype(auto). Returns true if
13685 : : there was a problem, false otherwise. */
13686 : :
13687 : : static bool
13688 : 598465747 : check_decltype_auto (location_t loc, tree type)
13689 : : {
13690 : 598465747 : if (tree a = type_uses_auto (type))
13691 : : {
13692 : 11109002 : if (AUTO_IS_DECLTYPE (a))
13693 : : {
13694 : 389116 : if (a != type)
13695 : : {
13696 : 33 : error_at (loc, "%qT as type rather than plain "
13697 : : "%<decltype(auto)%>", type);
13698 : 33 : return true;
13699 : : }
13700 : 389083 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
13701 : : {
13702 : 37 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
13703 : 37 : return true;
13704 : : }
13705 : : }
13706 : : }
13707 : : return false;
13708 : : }
13709 : :
13710 : : /* Issue an error about two mutually incompatible declspecs
13711 : : with the given names and locations
13712 : : e.g. "error: `signed' and `unsigned' specified together" */
13713 : :
13714 : : static void
13715 : 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
13716 : : const char *name_b, location_t loc_b)
13717 : : {
13718 : 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
13719 : 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
13720 : : nullptr, highlight_colors::rhs);
13721 : 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
13722 : 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
13723 : 66 : error_at (&richloc, "%e and %e specified together",
13724 : : &e_name_a, &e_name_b);
13725 : 66 : }
13726 : :
13727 : : /* Given declspecs and a declarator (abstract or otherwise), determine
13728 : : the name and type of the object declared and construct a DECL node
13729 : : for it.
13730 : :
13731 : : DECLSPECS points to the representation of declaration-specifier
13732 : : sequence that precedes declarator.
13733 : :
13734 : : DECL_CONTEXT says which syntactic context this declaration is in:
13735 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
13736 : : FUNCDEF for a function definition. Like NORMAL but a few different
13737 : : error messages in each case. Return value may be zero meaning
13738 : : this definition is too screwy to try to parse.
13739 : : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
13740 : : handle member functions (which have FIELD context).
13741 : : Return value may be zero meaning this definition is too screwy to
13742 : : try to parse.
13743 : : PARM for a parameter declaration (either within a function prototype
13744 : : or before a function body). Make a PARM_DECL, or return void_type_node.
13745 : : TPARM for a template parameter declaration.
13746 : : CATCHPARM for a parameter declaration before a catch clause.
13747 : : TYPENAME if for a typename (in a cast or sizeof).
13748 : : Don't make a DECL node; just return the ..._TYPE node.
13749 : : FIELD for a struct or union field; make a FIELD_DECL.
13750 : : BITFIELD for a field with specified width.
13751 : :
13752 : : INITIALIZED is as for start_decl.
13753 : :
13754 : : ATTRLIST is a pointer to the list of attributes, which may be NULL
13755 : : if there are none; *ATTRLIST may be modified if attributes from inside
13756 : : the declarator should be applied to the declaration.
13757 : :
13758 : : When this function is called, scoping variables (such as
13759 : : CURRENT_CLASS_TYPE) should reflect the scope in which the
13760 : : declaration occurs, not the scope in which the new declaration will
13761 : : be placed. For example, on:
13762 : :
13763 : : void S::f() { ... }
13764 : :
13765 : : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
13766 : : should not be `S'.
13767 : :
13768 : : Returns a DECL (if a declarator is present), a TYPE (if there is no
13769 : : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
13770 : : error occurs. */
13771 : :
13772 : : tree
13773 : 953323231 : grokdeclarator (const cp_declarator *declarator,
13774 : : cp_decl_specifier_seq *declspecs,
13775 : : enum decl_context decl_context,
13776 : : int initialized,
13777 : : tree* attrlist)
13778 : : {
13779 : 953323231 : tree type = NULL_TREE;
13780 : 953323231 : int longlong = 0;
13781 : 953323231 : int explicit_intN = 0;
13782 : 953323231 : int int_n_alt = 0;
13783 : 953323231 : int virtualp, explicitp, friendp, inlinep, staticp;
13784 : 953323231 : int explicit_int = 0;
13785 : 953323231 : int explicit_char = 0;
13786 : 953323231 : int defaulted_int = 0;
13787 : :
13788 : 953323231 : tree typedef_decl = NULL_TREE;
13789 : 953323231 : const char *name = NULL;
13790 : 953323231 : tree typedef_type = NULL_TREE;
13791 : : /* True if this declarator is a function definition. */
13792 : 953323231 : bool funcdef_flag = false;
13793 : 953323231 : cp_declarator_kind innermost_code = cdk_error;
13794 : 953323231 : int bitfield = 0;
13795 : : #if 0
13796 : : /* See the code below that used this. */
13797 : : tree decl_attr = NULL_TREE;
13798 : : #endif
13799 : :
13800 : : /* Keep track of what sort of function is being processed
13801 : : so that we can warn about default return values, or explicit
13802 : : return values which do not match prescribed defaults. */
13803 : 953323231 : special_function_kind sfk = sfk_none;
13804 : :
13805 : 953323231 : tree dname = NULL_TREE;
13806 : 953323231 : tree ctor_return_type = NULL_TREE;
13807 : 953323231 : enum overload_flags flags = NO_SPECIAL;
13808 : : /* cv-qualifiers that apply to the declarator, for a declaration of
13809 : : a member function. */
13810 : 953323231 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
13811 : : /* virt-specifiers that apply to the declarator, for a declaration of
13812 : : a member function. */
13813 : 953323231 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
13814 : : /* ref-qualifier that applies to the declarator, for a declaration of
13815 : : a member function. */
13816 : 953323231 : cp_ref_qualifier rqual = REF_QUAL_NONE;
13817 : : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
13818 : 953323231 : int type_quals = get_type_quals (declspecs);
13819 : 953323231 : tree raises = NULL_TREE;
13820 : 953323231 : int template_count = 0;
13821 : 953323231 : tree returned_attrs = NULL_TREE;
13822 : 953323231 : tree parms = NULL_TREE;
13823 : 953323231 : const cp_declarator *id_declarator;
13824 : : /* The unqualified name of the declarator; either an
13825 : : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
13826 : 953323231 : tree unqualified_id;
13827 : : /* The class type, if any, in which this entity is located,
13828 : : or NULL_TREE if none. Note that this value may be different from
13829 : : the current class type; for example if an attempt is made to declare
13830 : : "A::f" inside "B", this value will be "A". */
13831 : 953323231 : tree ctype = current_class_type;
13832 : : /* The NAMESPACE_DECL for the namespace in which this entity is
13833 : : located. If an unqualified name is used to declare the entity,
13834 : : this value will be NULL_TREE, even if the entity is located at
13835 : : namespace scope. */
13836 : 953323231 : tree in_namespace = NULL_TREE;
13837 : 953323231 : cp_storage_class storage_class;
13838 : 953323231 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
13839 : 953323231 : bool type_was_error_mark_node = false;
13840 : 953323231 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
13841 : 953323231 : bool template_type_arg = false;
13842 : 953323231 : bool template_parm_flag = false;
13843 : 953323231 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
13844 : 953323231 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
13845 : 953323231 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
13846 : 953323231 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
13847 : 953323231 : bool late_return_type_p = false;
13848 : 953323231 : bool array_parameter_p = false;
13849 : 953323231 : tree reqs = NULL_TREE;
13850 : :
13851 : 953323231 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
13852 : 953323231 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
13853 : 953323231 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
13854 : 953323231 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
13855 : 953323231 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
13856 : 953323231 : explicit_intN = declspecs->explicit_intN_p;
13857 : 953323231 : int_n_alt = declspecs->int_n_alt;
13858 : 953323231 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
13859 : :
13860 : : // Was concept_p specified? Note that ds_concept
13861 : : // implies ds_constexpr!
13862 : 953323231 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
13863 : 953323231 : if (concept_p)
13864 : 117 : constexpr_p = true;
13865 : :
13866 : 953323231 : if (decl_context == FUNCDEF)
13867 : : funcdef_flag = true, decl_context = NORMAL;
13868 : : else if (decl_context == MEMFUNCDEF)
13869 : : funcdef_flag = true, decl_context = FIELD;
13870 : : else if (decl_context == BITFIELD)
13871 : : bitfield = 1, decl_context = FIELD;
13872 : : else if (decl_context == TEMPLATE_TYPE_ARG)
13873 : : template_type_arg = true, decl_context = TYPENAME;
13874 : : else if (decl_context == TPARM)
13875 : 12134485 : template_parm_flag = true, decl_context = PARM;
13876 : :
13877 : 953323231 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
13878 : 9622976 : funcdef_flag = true;
13879 : :
13880 : 953323231 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
13881 : : (type_quals,
13882 : 953323231 : declspecs->locations));
13883 : 953323231 : location_t id_loc;
13884 : 953323231 : location_t init_loc;
13885 : 953323231 : if (declarator)
13886 : : {
13887 : 588906522 : id_loc = loc_or_input_loc (declarator->id_loc);
13888 : 588906522 : init_loc = loc_or_input_loc (declarator->init_loc);
13889 : : }
13890 : : else
13891 : 364416709 : init_loc = id_loc = input_location;
13892 : :
13893 : : /* Look inside a declarator for the name being declared
13894 : : and get it as a string, for an error message. */
13895 : 953323231 : for (id_declarator = declarator;
13896 : 1322768919 : id_declarator;
13897 : 369445688 : id_declarator = id_declarator->declarator)
13898 : : {
13899 : 920386578 : if (id_declarator->kind != cdk_id)
13900 : 369445713 : innermost_code = id_declarator->kind;
13901 : :
13902 : 920386578 : switch (id_declarator->kind)
13903 : : {
13904 : 181340700 : case cdk_function:
13905 : 181340700 : if (id_declarator->declarator
13906 : 180864749 : && id_declarator->declarator->kind == cdk_id)
13907 : : {
13908 : 178963521 : sfk = id_declarator->declarator->u.id.sfk;
13909 : 178963521 : if (sfk == sfk_destructor)
13910 : 3236318 : flags = DTOR_FLAG;
13911 : : }
13912 : : break;
13913 : :
13914 : 550940865 : case cdk_id:
13915 : 550940865 : {
13916 : 550940865 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
13917 : 550940865 : tree decl = id_declarator->u.id.unqualified_name;
13918 : 550940865 : if (!decl)
13919 : : break;
13920 : 550940865 : if (qualifying_scope)
13921 : : {
13922 : 9907347 : if (check_for_bare_parameter_packs (qualifying_scope,
13923 : 9907347 : id_declarator->id_loc))
13924 : 6 : return error_mark_node;
13925 : 9907341 : if (at_function_scope_p ())
13926 : : {
13927 : : /* [dcl.meaning]
13928 : :
13929 : : A declarator-id shall not be qualified except
13930 : : for ...
13931 : :
13932 : : None of the cases are permitted in block
13933 : : scope. */
13934 : 0 : if (qualifying_scope == global_namespace)
13935 : 0 : error ("invalid use of qualified-name %<::%D%>",
13936 : : decl);
13937 : 0 : else if (TYPE_P (qualifying_scope))
13938 : 0 : error ("invalid use of qualified-name %<%T::%D%>",
13939 : : qualifying_scope, decl);
13940 : : else
13941 : 0 : error ("invalid use of qualified-name %<%D::%D%>",
13942 : : qualifying_scope, decl);
13943 : 0 : return error_mark_node;
13944 : : }
13945 : 9907341 : else if (TYPE_P (qualifying_scope))
13946 : : {
13947 : 9706280 : ctype = qualifying_scope;
13948 : 9706280 : if (!MAYBE_CLASS_TYPE_P (ctype))
13949 : : {
13950 : 6 : error_at (id_declarator->id_loc,
13951 : : "%q#T is not a class or namespace", ctype);
13952 : 6 : ctype = NULL_TREE;
13953 : : }
13954 : 9706274 : else if (innermost_code != cdk_function
13955 : 413338 : && current_class_type
13956 : 9706289 : && !uniquely_derived_from_p (ctype,
13957 : : current_class_type))
13958 : : {
13959 : 9 : error_at (id_declarator->id_loc,
13960 : : "invalid use of qualified-name %<%T::%D%>",
13961 : : qualifying_scope, decl);
13962 : 9 : return error_mark_node;
13963 : : }
13964 : : }
13965 : 201061 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
13966 : 550940850 : in_namespace = qualifying_scope;
13967 : : }
13968 : 550940850 : switch (TREE_CODE (decl))
13969 : : {
13970 : 3236333 : case BIT_NOT_EXPR:
13971 : 3236333 : {
13972 : 3236333 : if (innermost_code != cdk_function)
13973 : : {
13974 : 6 : error_at (EXPR_LOCATION (decl),
13975 : : "declaration of %qE as non-function", decl);
13976 : 6 : return error_mark_node;
13977 : : }
13978 : 3236327 : else if (!qualifying_scope
13979 : 3236327 : && !(current_class_type && at_class_scope_p ()))
13980 : : {
13981 : 9 : error_at (EXPR_LOCATION (decl),
13982 : : "declaration of %qE as non-member", decl);
13983 : 9 : return error_mark_node;
13984 : : }
13985 : :
13986 : 3236318 : tree type = TREE_OPERAND (decl, 0);
13987 : 3236318 : if (TYPE_P (type))
13988 : 3236315 : type = constructor_name (type);
13989 : 3236318 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
13990 : 3236318 : dname = decl;
13991 : : }
13992 : 3236318 : break;
13993 : :
13994 : 2169397 : case TEMPLATE_ID_EXPR:
13995 : 2169397 : {
13996 : 2169397 : tree fns = TREE_OPERAND (decl, 0);
13997 : :
13998 : 2169397 : dname = fns;
13999 : 3103165 : if (!identifier_p (dname))
14000 : 2169373 : dname = OVL_NAME (dname);
14001 : : }
14002 : : /* Fall through. */
14003 : :
14004 : 547704517 : case IDENTIFIER_NODE:
14005 : 547704517 : if (identifier_p (decl))
14006 : 545535120 : dname = decl;
14007 : :
14008 : 547704517 : if (IDENTIFIER_KEYWORD_P (dname))
14009 : : {
14010 : 0 : error ("declarator-id missing; using reserved word %qD",
14011 : : dname);
14012 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14013 : : }
14014 : 547704517 : else if (!IDENTIFIER_CONV_OP_P (dname))
14015 : 547027688 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14016 : : else
14017 : : {
14018 : 676829 : gcc_assert (flags == NO_SPECIAL);
14019 : 676829 : flags = TYPENAME_FLAG;
14020 : 676829 : sfk = sfk_conversion;
14021 : 676829 : tree glob = get_global_binding (dname);
14022 : 676829 : if (glob && TREE_CODE (glob) == TYPE_DECL)
14023 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
14024 : : else
14025 : : name = "<invalid operator>";
14026 : : }
14027 : : break;
14028 : :
14029 : 0 : default:
14030 : 0 : gcc_unreachable ();
14031 : : }
14032 : : break;
14033 : : }
14034 : :
14035 : : case cdk_array:
14036 : : case cdk_pointer:
14037 : : case cdk_reference:
14038 : : case cdk_ptrmem:
14039 : : break;
14040 : :
14041 : : case cdk_decomp:
14042 : 920386523 : name = "structured binding";
14043 : : break;
14044 : :
14045 : 25 : case cdk_error:
14046 : 25 : return error_mark_node;
14047 : :
14048 : 0 : default:
14049 : 0 : gcc_unreachable ();
14050 : : }
14051 : 920386523 : if (id_declarator->kind == cdk_id)
14052 : : break;
14053 : : }
14054 : :
14055 : : /* [dcl.fct.edf]
14056 : :
14057 : : The declarator in a function-definition shall have the form
14058 : : D1 ( parameter-declaration-clause) ... */
14059 : 953323176 : if (funcdef_flag && innermost_code != cdk_function)
14060 : : {
14061 : 3 : error_at (id_loc, "function definition does not declare parameters");
14062 : 3 : return error_mark_node;
14063 : : }
14064 : :
14065 : 953323173 : if (flags == TYPENAME_FLAG
14066 : 953323173 : && innermost_code != cdk_function
14067 : 3 : && ! (ctype && !declspecs->any_specifiers_p))
14068 : : {
14069 : 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
14070 : 3 : return error_mark_node;
14071 : : }
14072 : :
14073 : 953323170 : if (dname && identifier_p (dname))
14074 : : {
14075 : 547704511 : if (UDLIT_OPER_P (dname)
14076 : 547704511 : && innermost_code != cdk_function)
14077 : : {
14078 : 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
14079 : 6 : return error_mark_node;
14080 : : }
14081 : :
14082 : 547704505 : if (IDENTIFIER_ANY_OP_P (dname))
14083 : : {
14084 : 33034136 : if (typedef_p)
14085 : : {
14086 : 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14087 : 3 : return error_mark_node;
14088 : : }
14089 : 33034133 : else if (decl_context == PARM || decl_context == CATCHPARM)
14090 : : {
14091 : 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
14092 : 15 : return error_mark_node;
14093 : : }
14094 : : }
14095 : : }
14096 : :
14097 : : /* Anything declared one level down from the top level
14098 : : must be one of the parameters of a function
14099 : : (because the body is at least two levels down). */
14100 : :
14101 : : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14102 : : by not allowing C++ class definitions to specify their parameters
14103 : : with xdecls (must be spec.d in the parmlist).
14104 : :
14105 : : Since we now wait to push a class scope until we are sure that
14106 : : we are in a legitimate method context, we must set oldcname
14107 : : explicitly (since current_class_name is not yet alive).
14108 : :
14109 : : We also want to avoid calling this a PARM if it is in a namespace. */
14110 : :
14111 : 953323146 : if (decl_context == NORMAL && !toplevel_bindings_p ())
14112 : : {
14113 : 64579603 : cp_binding_level *b = current_binding_level;
14114 : 64579603 : current_binding_level = b->level_chain;
14115 : 64579603 : if (current_binding_level != 0 && toplevel_bindings_p ())
14116 : : decl_context = PARM;
14117 : 64579603 : current_binding_level = b;
14118 : : }
14119 : :
14120 : 953323146 : if (name == NULL)
14121 : 402282974 : name = decl_context == PARM ? "parameter" : "type name";
14122 : :
14123 : 953323146 : if (consteval_p && constexpr_p)
14124 : : {
14125 : 6 : error_at (declspecs->locations[ds_consteval],
14126 : : "both %qs and %qs specified", "constexpr", "consteval");
14127 : 6 : return error_mark_node;
14128 : : }
14129 : :
14130 : 953323140 : if (concept_p && typedef_p)
14131 : : {
14132 : 9 : error_at (declspecs->locations[ds_concept],
14133 : : "%qs cannot appear in a typedef declaration", "concept");
14134 : 9 : return error_mark_node;
14135 : : }
14136 : :
14137 : 953323131 : if (constexpr_p && typedef_p)
14138 : : {
14139 : 3 : error_at (declspecs->locations[ds_constexpr],
14140 : : "%qs cannot appear in a typedef declaration", "constexpr");
14141 : 3 : return error_mark_node;
14142 : : }
14143 : :
14144 : 953323128 : if (consteval_p && typedef_p)
14145 : : {
14146 : 3 : error_at (declspecs->locations[ds_consteval],
14147 : : "%qs cannot appear in a typedef declaration", "consteval");
14148 : 3 : return error_mark_node;
14149 : : }
14150 : :
14151 : 953323125 : if (constinit_p && typedef_p)
14152 : : {
14153 : 3 : error_at (declspecs->locations[ds_constinit],
14154 : : "%qs cannot appear in a typedef declaration", "constinit");
14155 : 3 : return error_mark_node;
14156 : : }
14157 : :
14158 : : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14159 : : keywords shall appear in a decl-specifier-seq." */
14160 : 953323122 : if (constinit_p && constexpr_p)
14161 : : {
14162 : 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14163 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14164 : 6 : error_at (&richloc,
14165 : : "can use at most one of the %<constinit%> and %<constexpr%> "
14166 : : "specifiers");
14167 : 6 : return error_mark_node;
14168 : 6 : }
14169 : :
14170 : : /* If there were multiple types specified in the decl-specifier-seq,
14171 : : issue an error message. */
14172 : 953323116 : if (declspecs->multiple_types_p)
14173 : : {
14174 : 1501 : error_at (typespec_loc,
14175 : : "two or more data types in declaration of %qs", name);
14176 : 1501 : return error_mark_node;
14177 : : }
14178 : :
14179 : 953321615 : if (declspecs->conflicting_specifiers_p)
14180 : 31 : return error_mark_node;
14181 : :
14182 : : /* Extract the basic type from the decl-specifier-seq. */
14183 : 953321584 : type = declspecs->type;
14184 : 953321584 : if (type == error_mark_node)
14185 : : {
14186 : 1168 : type = NULL_TREE;
14187 : 1168 : type_was_error_mark_node = true;
14188 : : }
14189 : :
14190 : : /* Ignore erroneous attributes. */
14191 : 953321584 : if (attrlist && *attrlist == error_mark_node)
14192 : 12 : *attrlist = NULL_TREE;
14193 : :
14194 : : /* An object declared as __attribute__((unavailable)) suppresses
14195 : : any reports of being declared with unavailable or deprecated
14196 : : items. An object declared as __attribute__((deprecated))
14197 : : suppresses warnings of uses of other deprecated items. */
14198 : 953321584 : auto ds = make_temp_override (deprecated_state);
14199 : 953321584 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
14200 : 159 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14201 : 953321425 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
14202 : 719950 : deprecated_state = DEPRECATED_SUPPRESS;
14203 : :
14204 : 953321584 : cp_handle_deprecated_or_unavailable (type);
14205 : 953321584 : if (type && TREE_CODE (type) == TYPE_DECL)
14206 : : {
14207 : 654342802 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14208 : 654342802 : typedef_decl = type;
14209 : 654342802 : type = TREE_TYPE (typedef_decl);
14210 : 654342802 : if (DECL_ARTIFICIAL (typedef_decl))
14211 : 472453895 : cp_handle_deprecated_or_unavailable (type);
14212 : : }
14213 : : /* No type at all: default to `int', and set DEFAULTED_INT
14214 : : because it was not a user-defined typedef. */
14215 : 953321584 : if (type == NULL_TREE)
14216 : : {
14217 : 36331285 : if (signed_p || unsigned_p || long_p || short_p)
14218 : : {
14219 : : /* These imply 'int'. */
14220 : 8484512 : type = integer_type_node;
14221 : 8484512 : defaulted_int = 1;
14222 : : }
14223 : : /* If we just have "complex", it is equivalent to "complex double". */
14224 : 27846773 : else if (!longlong && !explicit_intN
14225 : 27846773 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14226 : : {
14227 : 81 : type = double_type_node;
14228 : 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14229 : : "ISO C++ does not support plain %<complex%> meaning "
14230 : : "%<double complex%>");
14231 : : }
14232 : : }
14233 : : /* Gather flags. */
14234 : 953321584 : explicit_int = declspecs->explicit_int_p;
14235 : 953321584 : explicit_char = declspecs->explicit_char_p;
14236 : :
14237 : : #if 0
14238 : : /* See the code below that used this. */
14239 : : if (typedef_decl)
14240 : : decl_attr = DECL_ATTRIBUTES (typedef_decl);
14241 : : #endif
14242 : 953321584 : typedef_type = type;
14243 : :
14244 : 953321584 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14245 : 1789038 : ctor_return_type = TREE_TYPE (dname);
14246 : : else
14247 : : ctor_return_type = ctype;
14248 : :
14249 : 953321584 : if (sfk != sfk_none)
14250 : : {
14251 : 27845205 : type = check_special_function_return_type (sfk, type,
14252 : : ctor_return_type,
14253 : : type_quals,
14254 : : &declarator,
14255 : : declspecs->locations);
14256 : 27845205 : type_quals = TYPE_UNQUALIFIED;
14257 : : }
14258 : 925476379 : else if (type == NULL_TREE)
14259 : : {
14260 : 1568 : int is_main;
14261 : :
14262 : 1568 : explicit_int = -1;
14263 : :
14264 : : /* We handle `main' specially here, because 'main () { }' is so
14265 : : common. With no options, it is allowed. With -Wreturn-type,
14266 : : it is a warning. It is only an error with -pedantic-errors. */
14267 : 3136 : is_main = (funcdef_flag
14268 : 3370 : && dname && identifier_p (dname)
14269 : 234 : && MAIN_NAME_P (dname)
14270 : 68 : && ctype == NULL_TREE
14271 : 68 : && in_namespace == NULL_TREE
14272 : 1636 : && current_namespace == global_namespace);
14273 : :
14274 : 1568 : if (type_was_error_mark_node)
14275 : : /* We've already issued an error, don't complain more. */;
14276 : 400 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
14277 : : /* Allow it, sigh. */;
14278 : 338 : else if (! is_main)
14279 : 276 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14280 : : name);
14281 : 62 : else if (pedantic)
14282 : 4 : pedwarn (id_loc, OPT_Wpedantic,
14283 : : "ISO C++ forbids declaration of %qs with no type", name);
14284 : : else
14285 : 58 : warning_at (id_loc, OPT_Wreturn_type,
14286 : : "ISO C++ forbids declaration of %qs with no type", name);
14287 : :
14288 : 1568 : if (type_was_error_mark_node && template_parm_flag)
14289 : : /* FIXME we should be able to propagate the error_mark_node as is
14290 : : for other contexts too. */
14291 : 148 : type = error_mark_node;
14292 : : else
14293 : 1420 : type = integer_type_node;
14294 : : }
14295 : :
14296 : 953321584 : ctype = NULL_TREE;
14297 : :
14298 : 953321584 : if (explicit_intN)
14299 : : {
14300 : 1033107 : if (! int_n_enabled_p[declspecs->int_n_idx])
14301 : : {
14302 : 0 : error_at (declspecs->locations[ds_type_spec],
14303 : : "%<__int%d%> is not supported by this target",
14304 : 0 : int_n_data[declspecs->int_n_idx].bitsize);
14305 : 0 : explicit_intN = false;
14306 : : }
14307 : : /* Don't pedwarn if the alternate "__intN__" form has been used instead
14308 : : of "__intN". */
14309 : 1033107 : else if (!int_n_alt && pedantic)
14310 : 4364 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14311 : : "ISO C++ does not support %<__int%d%> for %qs",
14312 : 4364 : int_n_data[declspecs->int_n_idx].bitsize, name);
14313 : : }
14314 : :
14315 : : /* Now process the modifiers that were specified
14316 : : and check for invalid combinations. */
14317 : :
14318 : : /* Long double is a special combination. */
14319 : 953321584 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14320 : : {
14321 : 9252574 : long_p = false;
14322 : 9252574 : type = cp_build_qualified_type (long_double_type_node,
14323 : : cp_type_quals (type));
14324 : : }
14325 : :
14326 : : /* Check all other uses of type modifiers. */
14327 : :
14328 : 953321584 : if (unsigned_p || signed_p || long_p || short_p)
14329 : : {
14330 : 20357644 : location_t loc;
14331 : 20357644 : const char *key;
14332 : 20357644 : if (unsigned_p)
14333 : : {
14334 : 13462147 : key = "unsigned";
14335 : 13462147 : loc = declspecs->locations[ds_unsigned];
14336 : : }
14337 : 6895497 : else if (signed_p)
14338 : : {
14339 : 1127455 : key = "signed";
14340 : 1127455 : loc = declspecs->locations[ds_signed];
14341 : : }
14342 : 5768042 : else if (longlong)
14343 : : {
14344 : 2237235 : key = "long long";
14345 : 2237235 : loc = declspecs->locations[ds_long_long];
14346 : : }
14347 : 3530807 : else if (long_p)
14348 : : {
14349 : 2989172 : key = "long";
14350 : 2989172 : loc = declspecs->locations[ds_long];
14351 : : }
14352 : : else /* if (short_p) */
14353 : : {
14354 : 541635 : key = "short";
14355 : 541635 : loc = declspecs->locations[ds_short];
14356 : : }
14357 : :
14358 : 20357644 : int ok = 0;
14359 : :
14360 : 20357644 : if (signed_p && unsigned_p)
14361 : 54 : complain_about_incompatible_declspecs
14362 : 54 : ("signed", declspecs->locations[ds_signed],
14363 : : "unsigned", declspecs->locations[ds_unsigned]);
14364 : 20357590 : else if (long_p && short_p)
14365 : 12 : complain_about_incompatible_declspecs
14366 : 12 : ("long", declspecs->locations[ds_long],
14367 : : "short", declspecs->locations[ds_short]);
14368 : 20357578 : else if (TREE_CODE (type) != INTEGER_TYPE
14369 : 20357565 : || type == char8_type_node
14370 : 20357541 : || type == char16_type_node
14371 : 20357517 : || type == char32_type_node
14372 : 20357493 : || ((long_p || short_p)
14373 : 11039696 : && (explicit_char || explicit_intN)))
14374 : 418 : error_at (loc, "%qs specified with %qT", key, type);
14375 : 20357160 : else if (!explicit_int && !defaulted_int
14376 : 20357160 : && !explicit_char && !explicit_intN)
14377 : : {
14378 : 30 : if (typedef_decl)
14379 : : {
14380 : 27 : pedwarn (loc, OPT_Wpedantic,
14381 : : "%qs specified with typedef-name %qD",
14382 : : key, typedef_decl);
14383 : 27 : ok = !flag_pedantic_errors;
14384 : : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14385 : : and is not built as a typedef. */
14386 : 27 : if (is_typedef_decl (typedef_decl))
14387 : 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
14388 : : }
14389 : 3 : else if (declspecs->decltype_p)
14390 : 0 : error_at (loc, "%qs specified with %<decltype%>", key);
14391 : : else
14392 : 3 : error_at (loc, "%qs specified with %<typeof%>", key);
14393 : : }
14394 : : else
14395 : : ok = 1;
14396 : :
14397 : : /* Discard the type modifiers if they are invalid. */
14398 : 514 : if (! ok)
14399 : : {
14400 : : unsigned_p = false;
14401 : : signed_p = false;
14402 : : long_p = false;
14403 : : short_p = false;
14404 : : longlong = 0;
14405 : : }
14406 : : }
14407 : :
14408 : : /* Decide whether an integer type is signed or not.
14409 : : Optionally treat bitfields as signed by default. */
14410 : 953321094 : if (unsigned_p
14411 : : /* [class.bit]
14412 : :
14413 : : It is implementation-defined whether a plain (neither
14414 : : explicitly signed or unsigned) char, short, int, or long
14415 : : bit-field is signed or unsigned.
14416 : :
14417 : : Naturally, we extend this to long long as well. Note that
14418 : : this does not include wchar_t. */
14419 : 953321584 : || (bitfield && !flag_signed_bitfields
14420 : 18 : && !signed_p
14421 : : /* A typedef for plain `int' without `signed' can be
14422 : : controlled just like plain `int', but a typedef for
14423 : : `signed int' cannot be so controlled. */
14424 : 18 : && !(typedef_decl
14425 : 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14426 : 13 : && TREE_CODE (type) == INTEGER_TYPE
14427 : 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14428 : : {
14429 : 13461932 : if (explicit_intN)
14430 : 487939 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14431 : 12973993 : else if (longlong)
14432 : 1416851 : type = long_long_unsigned_type_node;
14433 : 11557142 : else if (long_p)
14434 : 2262749 : type = long_unsigned_type_node;
14435 : 9294393 : else if (short_p)
14436 : 1338066 : type = short_unsigned_type_node;
14437 : 7956327 : else if (type == char_type_node)
14438 : 1626050 : type = unsigned_char_type_node;
14439 : 6330277 : else if (typedef_decl)
14440 : 25 : type = c_common_unsigned_type (type);
14441 : : else
14442 : 6330252 : type = unsigned_type_node;
14443 : : }
14444 : 939859652 : else if (signed_p && type == char_type_node)
14445 : 704493 : type = signed_char_type_node;
14446 : 939155159 : else if (explicit_intN)
14447 : 545168 : type = int_n_trees[declspecs->int_n_idx].signed_type;
14448 : 938609991 : else if (longlong)
14449 : 2315713 : type = long_long_integer_type_node;
14450 : 936294278 : else if (long_p)
14451 : 3071220 : type = long_integer_type_node;
14452 : 933223058 : else if (short_p)
14453 : 634764 : type = short_integer_type_node;
14454 : 932588294 : else if (signed_p && typedef_decl)
14455 : 9 : type = c_common_signed_type (type);
14456 : :
14457 : 953321584 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14458 : : {
14459 : 1449675 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14460 : 3 : error_at (declspecs->locations[ds_complex],
14461 : : "complex invalid for %qs", name);
14462 : : /* If a modifier is specified, the resulting complex is the complex
14463 : : form of TYPE. E.g, "complex short" is "complex short int". */
14464 : 1449672 : else if (type == integer_type_node)
14465 : 164 : type = complex_integer_type_node;
14466 : 1449508 : else if (type == float_type_node)
14467 : 364193 : type = complex_float_type_node;
14468 : 1085315 : else if (type == double_type_node)
14469 : 364729 : type = complex_double_type_node;
14470 : 720586 : else if (type == long_double_type_node)
14471 : 364220 : type = complex_long_double_type_node;
14472 : : else
14473 : 356366 : type = build_complex_type (type);
14474 : : }
14475 : :
14476 : : /* If we're using the injected-class-name to form a compound type or a
14477 : : declaration, replace it with the underlying class so we don't get
14478 : : redundant typedefs in the debug output. But if we are returning the
14479 : : type unchanged, leave it alone so that it's available to
14480 : : maybe_get_template_decl_from_type_decl. */
14481 : 188103672 : if (CLASS_TYPE_P (type)
14482 : 188051828 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14483 : 30685059 : && type == TREE_TYPE (TYPE_NAME (type))
14484 : 984006643 : && (declarator || type_quals))
14485 : 29376677 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14486 : :
14487 : 953321584 : type_quals |= cp_type_quals (type);
14488 : 1906643168 : type = cp_build_qualified_type
14489 : 1906643168 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14490 : 771432677 : || declspecs->decltype_p)
14491 : : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14492 : : /* We might have ignored or rejected some of the qualifiers. */
14493 : 953321584 : type_quals = cp_type_quals (type);
14494 : :
14495 : 938289823 : if (cxx_dialect >= cxx17 && type && is_auto (type)
14496 : 14814639 : && innermost_code != cdk_function
14497 : : /* Placeholder in parm gets a better error below. */
14498 : 8676018 : && !(decl_context == PARM || decl_context == CATCHPARM)
14499 : 961949805 : && id_declarator && declarator != id_declarator)
14500 : 1037912 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14501 : : {
14502 : 16 : auto_diagnostic_group g;
14503 : 16 : gcc_rich_location richloc (typespec_loc);
14504 : 16 : richloc.add_fixit_insert_after ("<>");
14505 : 16 : error_at (&richloc, "missing template argument list after %qE; "
14506 : : "for deduction, template placeholder must be followed "
14507 : : "by a simple declarator-id", tmpl);
14508 : 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14509 : 16 : type = error_mark_node;
14510 : 16 : }
14511 : :
14512 : 953321584 : staticp = 0;
14513 : 953321584 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14514 : 953321584 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14515 : 953321584 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14516 : :
14517 : 953321584 : storage_class = declspecs->storage_class;
14518 : 953321584 : if (storage_class == sc_static)
14519 : 21084173 : staticp = 1 + (decl_context == FIELD);
14520 : 932237411 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14521 : : /* Treat class-scope deduction guides as static member functions
14522 : : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14523 : 20753362 : staticp = 2;
14524 : :
14525 : 953321584 : if (virtualp)
14526 : : {
14527 : 4141841 : if (staticp == 2)
14528 : : {
14529 : 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14530 : 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
14531 : 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14532 : : "and %<static%>", dname);
14533 : 21 : storage_class = sc_none;
14534 : 21 : staticp = 0;
14535 : 21 : }
14536 : 4141841 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14537 : : {
14538 : 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14539 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14540 : 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14541 : : "declared both %<virtual%> and %<constexpr%> only in "
14542 : : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14543 : 6 : }
14544 : : }
14545 : 953321584 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14546 : :
14547 : : /* Issue errors about use of storage classes for parameters. */
14548 : 953321584 : if (decl_context == PARM)
14549 : : {
14550 : 284068888 : if (typedef_p)
14551 : : {
14552 : 24 : error_at (declspecs->locations[ds_typedef],
14553 : : "typedef declaration invalid in parameter declaration");
14554 : 24 : return error_mark_node;
14555 : : }
14556 : 284068864 : else if (template_parm_flag && storage_class != sc_none)
14557 : : {
14558 : 16 : error_at (min_location (declspecs->locations[ds_thread],
14559 : : declspecs->locations[ds_storage_class]),
14560 : : "storage class specified for template parameter %qs",
14561 : : name);
14562 : 16 : return error_mark_node;
14563 : : }
14564 : 284068848 : else if (storage_class == sc_static
14565 : 284068848 : || storage_class == sc_extern
14566 : 284068845 : || thread_p)
14567 : : {
14568 : 9 : error_at (min_location (declspecs->locations[ds_thread],
14569 : : declspecs->locations[ds_storage_class]),
14570 : : "storage class specified for parameter %qs", name);
14571 : 9 : return error_mark_node;
14572 : : }
14573 : :
14574 : : /* Function parameters cannot be concept. */
14575 : 284068839 : if (concept_p)
14576 : : {
14577 : 9 : error_at (declspecs->locations[ds_concept],
14578 : : "a parameter cannot be declared %qs", "concept");
14579 : 9 : concept_p = 0;
14580 : 9 : constexpr_p = 0;
14581 : : }
14582 : : /* Function parameters cannot be constexpr. If we saw one, moan
14583 : : and pretend it wasn't there. */
14584 : 284068830 : else if (constexpr_p)
14585 : : {
14586 : 9 : error_at (declspecs->locations[ds_constexpr],
14587 : : "a parameter cannot be declared %qs", "constexpr");
14588 : 9 : constexpr_p = 0;
14589 : : }
14590 : 284068839 : if (constinit_p)
14591 : : {
14592 : 6 : error_at (declspecs->locations[ds_constinit],
14593 : : "a parameter cannot be declared %qs", "constinit");
14594 : 6 : constinit_p = 0;
14595 : : }
14596 : 284068839 : if (consteval_p)
14597 : : {
14598 : 3 : error_at (declspecs->locations[ds_consteval],
14599 : : "a parameter cannot be declared %qs", "consteval");
14600 : 3 : consteval_p = 0;
14601 : : }
14602 : : }
14603 : :
14604 : : /* Give error if `virtual' is used outside of class declaration. */
14605 : 953321535 : if (virtualp
14606 : 4141841 : && (current_class_name == NULL_TREE || decl_context != FIELD))
14607 : : {
14608 : 68 : error_at (declspecs->locations[ds_virtual],
14609 : : "%<virtual%> outside class declaration");
14610 : 68 : virtualp = 0;
14611 : : }
14612 : :
14613 : 953321535 : if (innermost_code == cdk_decomp)
14614 : : {
14615 : 88921 : location_t loc = (declarator->kind == cdk_reference
14616 : 99367 : ? declarator->declarator->id_loc : declarator->id_loc);
14617 : 99367 : if (inlinep)
14618 : 29 : error_at (declspecs->locations[ds_inline],
14619 : : "structured binding declaration cannot be %qs", "inline");
14620 : 99367 : if (typedef_p)
14621 : 29 : error_at (declspecs->locations[ds_typedef],
14622 : : "structured binding declaration cannot be %qs", "typedef");
14623 : 99367 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14624 : 96 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14625 : : "structured binding declaration can be %qs only with "
14626 : : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14627 : 99367 : if (consteval_p)
14628 : 13 : error_at (declspecs->locations[ds_consteval], "structured "
14629 : : "binding declaration cannot be %qs", "consteval");
14630 : 99367 : if (thread_p && cxx_dialect < cxx20)
14631 : 75 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14632 : : "structured binding declaration can be %qs only in "
14633 : : "%<-std=c++20%> or %<-std=gnu++20%>",
14634 : 75 : declspecs->gnu_thread_keyword_p
14635 : : ? "__thread" : "thread_local");
14636 : 99367 : if (concept_p)
14637 : : {
14638 : 0 : error_at (declspecs->locations[ds_concept],
14639 : : "structured binding declaration cannot be %qs", "concept");
14640 : 0 : constexpr_p = 0;
14641 : : }
14642 : : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
14643 : 99367 : if (type_quals & TYPE_QUAL_VOLATILE)
14644 : 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
14645 : : "%<volatile%>-qualified structured binding is deprecated");
14646 : 99367 : switch (storage_class)
14647 : : {
14648 : : case sc_none:
14649 : : break;
14650 : 28 : case sc_register:
14651 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14652 : : "register");
14653 : 28 : break;
14654 : 235 : case sc_static:
14655 : 235 : if (cxx_dialect < cxx20)
14656 : 185 : pedwarn (loc, OPT_Wc__20_extensions,
14657 : : "structured binding declaration can be %qs only in "
14658 : : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
14659 : : break;
14660 : 29 : case sc_extern:
14661 : 29 : error_at (loc, "structured binding declaration cannot be %qs",
14662 : : "extern");
14663 : 29 : break;
14664 : 28 : case sc_mutable:
14665 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14666 : : "mutable");
14667 : 28 : break;
14668 : 0 : case sc_auto:
14669 : 0 : error_at (loc, "structured binding declaration cannot be "
14670 : : "C++98 %<auto%>");
14671 : 0 : break;
14672 : 0 : default:
14673 : 0 : gcc_unreachable ();
14674 : : }
14675 : 99367 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
14676 : 99367 : || TYPE_IDENTIFIER (type) != auto_identifier)
14677 : : {
14678 : 3 : if (type != error_mark_node)
14679 : : {
14680 : 3 : auto_diagnostic_group d;
14681 : 3 : error_at (loc, "structured binding declaration cannot have "
14682 : : "type %qT", type);
14683 : 3 : inform (loc,
14684 : : "type must be cv-qualified %<auto%> or reference to "
14685 : : "cv-qualified %<auto%>");
14686 : 3 : }
14687 : 3 : type = build_qualified_type (make_auto (), type_quals);
14688 : 3 : declspecs->type = type;
14689 : : }
14690 : 99364 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
14691 : 51 : pedwarn (loc, OPT_Wpedantic,
14692 : : "structured binding declaration cannot have constrained "
14693 : : "%<auto%> type %qT", type);
14694 : 99367 : inlinep = 0;
14695 : 99367 : typedef_p = 0;
14696 : 99367 : consteval_p = 0;
14697 : 99367 : concept_p = 0;
14698 : 99367 : if (storage_class != sc_static)
14699 : : {
14700 : 99132 : storage_class = sc_none;
14701 : 99132 : declspecs->storage_class = sc_none;
14702 : : }
14703 : : }
14704 : :
14705 : : /* Static anonymous unions are dealt with here. */
14706 : 953321535 : if (staticp && decl_context == TYPENAME
14707 : 0 : && declspecs->type
14708 : 953321535 : && ANON_AGGR_TYPE_P (declspecs->type))
14709 : : decl_context = FIELD;
14710 : :
14711 : : /* Warn about storage classes that are invalid for certain
14712 : : kinds of declarations (parameters, typenames, etc.). */
14713 : 953321535 : if (thread_p
14714 : 21407 : && ((storage_class
14715 : 21407 : && storage_class != sc_extern
14716 : 524 : && storage_class != sc_static)
14717 : 21396 : || typedef_p))
14718 : : {
14719 : 17 : location_t loc
14720 : 17 : = min_location (declspecs->locations[ds_thread],
14721 : : declspecs->locations[ds_storage_class]);
14722 : 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
14723 : 17 : thread_p = false;
14724 : : }
14725 : 953321535 : if (decl_context != NORMAL
14726 : 793627637 : && ((storage_class != sc_none
14727 : 793627637 : && storage_class != sc_mutable)
14728 : 772874287 : || thread_p))
14729 : : {
14730 : 20753353 : if ((decl_context == PARM || decl_context == CATCHPARM)
14731 : 36 : && (storage_class == sc_register
14732 : 36 : || storage_class == sc_auto))
14733 : : ;
14734 : 20753317 : else if (typedef_p)
14735 : : ;
14736 : 20753317 : else if (decl_context == FIELD
14737 : : /* C++ allows static class elements. */
14738 : 20753317 : && storage_class == sc_static)
14739 : : /* C++ also allows inlines and signed and unsigned elements,
14740 : : but in those cases we don't come in here. */
14741 : : ;
14742 : : else
14743 : : {
14744 : 15 : location_t loc
14745 : 15 : = min_location (declspecs->locations[ds_thread],
14746 : : declspecs->locations[ds_storage_class]);
14747 : 15 : if (decl_context == FIELD)
14748 : 15 : error_at (loc, "storage class specified for %qs", name);
14749 : 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
14750 : 0 : error_at (loc, "storage class specified for parameter %qs", name);
14751 : : else
14752 : 0 : error_at (loc, "storage class specified for typename");
14753 : 15 : if (storage_class == sc_register
14754 : 15 : || storage_class == sc_auto
14755 : 15 : || storage_class == sc_extern
14756 : 3 : || thread_p)
14757 : 15 : storage_class = sc_none;
14758 : : }
14759 : : }
14760 : 932568182 : else if (storage_class == sc_extern && funcdef_flag
14761 : 932568182 : && ! toplevel_bindings_p ())
14762 : 0 : error ("nested function %qs declared %<extern%>", name);
14763 : 932568182 : else if (toplevel_bindings_p ())
14764 : : {
14765 : 430655134 : if (storage_class == sc_auto)
14766 : 2 : error_at (declspecs->locations[ds_storage_class],
14767 : : "top-level declaration of %qs specifies %<auto%>", name);
14768 : : }
14769 : 501913048 : else if (thread_p
14770 : 501913048 : && storage_class != sc_extern
14771 : 223 : && storage_class != sc_static)
14772 : : {
14773 : 190 : if (declspecs->gnu_thread_keyword_p)
14774 : 49 : pedwarn (declspecs->locations[ds_thread],
14775 : 49 : 0, "function-scope %qs implicitly auto and "
14776 : : "declared %<__thread%>", name);
14777 : :
14778 : : /* When thread_local is applied to a variable of block scope the
14779 : : storage-class-specifier static is implied if it does not appear
14780 : : explicitly. */
14781 : 190 : storage_class = declspecs->storage_class = sc_static;
14782 : 190 : staticp = 1;
14783 : : }
14784 : :
14785 : 953321535 : if (storage_class && friendp)
14786 : : {
14787 : 12 : error_at (min_location (declspecs->locations[ds_thread],
14788 : : declspecs->locations[ds_storage_class]),
14789 : : "storage class specifiers invalid in friend function "
14790 : : "declarations");
14791 : 12 : storage_class = sc_none;
14792 : 12 : staticp = 0;
14793 : : }
14794 : :
14795 : 953321535 : if (!id_declarator)
14796 : : unqualified_id = NULL_TREE;
14797 : : else
14798 : : {
14799 : 550939274 : unqualified_id = id_declarator->u.id.unqualified_name;
14800 : 550939274 : switch (TREE_CODE (unqualified_id))
14801 : : {
14802 : 3236318 : case BIT_NOT_EXPR:
14803 : 3236318 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
14804 : 3236318 : if (TYPE_P (unqualified_id))
14805 : 3236315 : unqualified_id = constructor_name (unqualified_id);
14806 : : break;
14807 : :
14808 : : case IDENTIFIER_NODE:
14809 : : case TEMPLATE_ID_EXPR:
14810 : : break;
14811 : :
14812 : 0 : default:
14813 : 0 : gcc_unreachable ();
14814 : : }
14815 : : }
14816 : :
14817 : 953321535 : if (declspecs->std_attributes
14818 : 953321535 : && !diagnose_misapplied_contracts (declspecs->std_attributes))
14819 : : {
14820 : 705 : location_t attr_loc = declspecs->locations[ds_std_attribute];
14821 : 705 : auto_diagnostic_group d;
14822 : 705 : if (any_nonignored_attribute_p (declspecs->std_attributes)
14823 : 705 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
14824 : 702 : inform (attr_loc, "an attribute that appertains to a type-specifier "
14825 : : "is ignored");
14826 : 705 : }
14827 : :
14828 : 953321535 : if (attrlist)
14829 : 940937350 : diagnose_misapplied_contracts (*attrlist);
14830 : :
14831 : : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
14832 : : bool is_xobj_member_function = false;
14833 : : /* Determine the type of the entity declared by recurring on the
14834 : : declarator. */
14835 : 1322665846 : for (; declarator; declarator = declarator->declarator)
14836 : : {
14837 : 920382978 : const cp_declarator *inner_declarator;
14838 : 920382978 : tree attrs;
14839 : :
14840 : 920382978 : if (type == error_mark_node)
14841 : 601 : return error_mark_node;
14842 : :
14843 : 920382652 : attrs = declarator->attributes;
14844 : 920382652 : if (attrs)
14845 : : {
14846 : 13259 : int attr_flags;
14847 : :
14848 : 13259 : attr_flags = 0;
14849 : 13259 : if (declarator->kind == cdk_id)
14850 : 12555 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
14851 : 13259 : if (declarator->kind == cdk_function)
14852 : 687 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
14853 : 13259 : if (declarator->kind == cdk_array)
14854 : 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
14855 : 13259 : tree late_attrs = NULL_TREE;
14856 : 13259 : if (decl_context != PARM && decl_context != TYPENAME)
14857 : : /* Assume that any attributes that get applied late to
14858 : : templates will DTRT when applied to the declaration
14859 : : as a whole. */
14860 : 13247 : late_attrs = splice_template_attributes (&attrs, type);
14861 : 13259 : returned_attrs = decl_attributes (&type,
14862 : : attr_chainon (returned_attrs,
14863 : : attrs),
14864 : : attr_flags);
14865 : 13259 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
14866 : : }
14867 : :
14868 : 920382652 : inner_declarator = declarator->declarator;
14869 : :
14870 : : /* Check that contracts aren't misapplied. */
14871 : 920382652 : if (tree contract_attr = find_contract (declarator->std_attributes))
14872 : 484 : if (declarator->kind != cdk_function
14873 : 479 : || innermost_code != cdk_function)
14874 : 7 : diagnose_misapplied_contracts (contract_attr);
14875 : :
14876 : : /* We don't want to warn in parameter context because we don't
14877 : : yet know if the parse will succeed, and this might turn out
14878 : : to be a constructor call. */
14879 : 920382652 : if (decl_context != PARM
14880 : 920382652 : && decl_context != TYPENAME
14881 : 530016800 : && !typedef_p
14882 : 483507934 : && declarator->parenthesized != UNKNOWN_LOCATION
14883 : : /* If the type is class-like and the inner name used a
14884 : : global namespace qualifier, we need the parens.
14885 : : Unfortunately all we can tell is whether a qualified name
14886 : : was used or not. */
14887 : 920382833 : && !(inner_declarator
14888 : 126 : && inner_declarator->kind == cdk_id
14889 : 108 : && inner_declarator->u.id.qualifying_scope
14890 : 18 : && (MAYBE_CLASS_TYPE_P (type)
14891 : 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
14892 : : {
14893 : 169 : auto_diagnostic_group d;
14894 : 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
14895 : : "unnecessary parentheses in declaration of %qs",
14896 : : name))
14897 : : {
14898 : 42 : gcc_rich_location iloc (declarator->parenthesized);
14899 : 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
14900 : 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
14901 : 42 : inform (&iloc, "remove parentheses");
14902 : 42 : }
14903 : 169 : }
14904 : 920382652 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
14905 : : break;
14906 : :
14907 : 369344586 : switch (declarator->kind)
14908 : : {
14909 : 3246787 : case cdk_array:
14910 : 6493574 : type = create_array_type_for_decl (dname, type,
14911 : 3246787 : declarator->u.array.bounds,
14912 : 3246787 : declarator->id_loc);
14913 : 3246787 : if (!valid_array_size_p (dname
14914 : : ? declarator->id_loc : input_location,
14915 : : type, dname))
14916 : 267 : type = error_mark_node;
14917 : :
14918 : 3246787 : if (declarator->std_attributes)
14919 : : /* [dcl.array]/1:
14920 : :
14921 : : The optional attribute-specifier-seq appertains to the
14922 : : array type. */
14923 : 189 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
14924 : : break;
14925 : :
14926 : 181340664 : case cdk_function:
14927 : 181340664 : {
14928 : 181340664 : tree arg_types;
14929 : 181340664 : int funcdecl_p;
14930 : :
14931 : : /* Declaring a function type. */
14932 : :
14933 : : /* Pick up type qualifiers which should be applied to `this'. */
14934 : 181340664 : memfn_quals = declarator->u.function.qualifiers;
14935 : : /* Pick up virt-specifiers. */
14936 : 181340664 : virt_specifiers = declarator->u.function.virt_specifiers;
14937 : : /* And ref-qualifier, too */
14938 : 181340664 : rqual = declarator->u.function.ref_qualifier;
14939 : : /* And tx-qualifier. */
14940 : 181340664 : tree tx_qual = declarator->u.function.tx_qualifier;
14941 : : /* Pick up the exception specifications. */
14942 : 181340664 : raises = declarator->u.function.exception_specification;
14943 : : /* If the exception-specification is ill-formed, let's pretend
14944 : : there wasn't one. */
14945 : 181340664 : if (raises == error_mark_node)
14946 : 9 : raises = NULL_TREE;
14947 : :
14948 : 362681328 : auto find_xobj_parm = [](tree parm_list)
14949 : : {
14950 : : /* There is no need to iterate over the list,
14951 : : only the first parm can be a valid xobj parm. */
14952 : 362448794 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
14953 : : return NULL_TREE;
14954 : : /* If we make it here, we are looking at an xobj parm.
14955 : :
14956 : : Non-null 'purpose' usually means the parm has a default
14957 : : argument, we don't want to violate this assumption. */
14958 : 90265 : TREE_PURPOSE (parm_list) = NULL_TREE;
14959 : 90265 : return TREE_VALUE (parm_list);
14960 : : };
14961 : :
14962 : 181340664 : tree xobj_parm
14963 : 181340664 : = find_xobj_parm (declarator->u.function.parameters);
14964 : 181340664 : is_xobj_member_function = xobj_parm;
14965 : :
14966 : 181340664 : if (xobj_parm && cxx_dialect < cxx23)
14967 : 27361 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
14968 : : "explicit object member function only available "
14969 : : "with %<-std=c++23%> or %<-std=gnu++23%>");
14970 : :
14971 : 181340664 : if (xobj_parm && decl_context == TYPENAME)
14972 : : {
14973 : : /* We inform in every case, just differently depending on what
14974 : : case it is. */
14975 : 6 : auto_diagnostic_group d;
14976 : 6 : bool ptr_type = true;
14977 : : /* If declarator->kind is cdk_function and we are at the end of
14978 : : the declarator chain, we are looking at a function type. */
14979 : 6 : if (!declarator->declarator)
14980 : : {
14981 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14982 : : "a function type cannot "
14983 : : "have an explicit object parameter");
14984 : 2 : ptr_type = false;
14985 : : }
14986 : 4 : else if (declarator->declarator->kind == cdk_pointer)
14987 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14988 : : "a pointer to function type cannot "
14989 : : "have an explicit object parameter");
14990 : 2 : else if (declarator->declarator->kind == cdk_ptrmem)
14991 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14992 : : "a pointer to member function type "
14993 : : "cannot have an explicit object parameter");
14994 : : else
14995 : 0 : gcc_unreachable ();
14996 : :
14997 : : /* The locations being used here are probably not correct. */
14998 : 4 : if (ptr_type)
14999 : 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15000 : : "the type of a pointer to explicit object member "
15001 : : "function is a regular pointer to function type");
15002 : : else
15003 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15004 : : "the type of an explicit object "
15005 : : "member function is a regular function type");
15006 : : /* Ideally we should synthesize the correct syntax
15007 : : for the user, perhaps this could be added later. */
15008 : 6 : }
15009 : : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
15010 : : the first parm node will never erroneously be detected here. */
15011 : 181340664 : {
15012 : 181340664 : auto_diagnostic_group d;
15013 : 181340664 : bool bad_xobj_parm_encountered = false;
15014 : 181340664 : for (tree parm = declarator->u.function.parameters;
15015 : 453640102 : parm && parm != void_list_node;
15016 : 272299438 : parm = TREE_CHAIN (parm))
15017 : : {
15018 : 272299438 : if (TREE_PURPOSE (parm) != this_identifier)
15019 : 272299432 : continue;
15020 : 6 : bad_xobj_parm_encountered = true;
15021 : 6 : TREE_PURPOSE (parm) = NULL_TREE;
15022 : 6 : gcc_rich_location bad_xobj_parm
15023 : 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
15024 : 6 : error_at (&bad_xobj_parm,
15025 : : "only the first parameter of a member function "
15026 : : "can be declared as an explicit object parameter");
15027 : 6 : }
15028 : 181340664 : if (bad_xobj_parm_encountered && xobj_parm)
15029 : 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15030 : : "valid explicit object parameter declared here");
15031 : 181340664 : }
15032 : :
15033 : 181340664 : if (reqs)
15034 : 3 : error_at (location_of (reqs), "requires-clause on return type");
15035 : 181340664 : reqs = declarator->u.function.requires_clause;
15036 : :
15037 : : /* Say it's a definition only for the CALL_EXPR
15038 : : closest to the identifier. */
15039 : 181340664 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15040 : :
15041 : : /* Handle a late-specified return type. */
15042 : 181340664 : tree late_return_type = declarator->u.function.late_return_type;
15043 : 181340664 : if (tree auto_node = type_uses_auto (type))
15044 : : {
15045 : 6150147 : if (!late_return_type)
15046 : : {
15047 : 2983461 : if (!funcdecl_p)
15048 : : /* auto (*fp)() = f; is OK. */;
15049 : 2983427 : else if (current_class_type
15050 : 5546873 : && LAMBDA_TYPE_P (current_class_type))
15051 : : /* OK for C++11 lambdas. */;
15052 : 1836527 : else if (cxx_dialect < cxx14)
15053 : : {
15054 : 7 : auto_diagnostic_group d;
15055 : 7 : error_at (typespec_loc, "%qs function uses "
15056 : : "%<auto%> type specifier without "
15057 : : "trailing return type", name);
15058 : 7 : inform (typespec_loc,
15059 : : "deduced return type only available "
15060 : : "with %<-std=c++14%> or %<-std=gnu++14%>");
15061 : 7 : }
15062 : 1836520 : else if (virtualp)
15063 : : {
15064 : 6 : error_at (typespec_loc, "virtual function "
15065 : : "cannot have deduced return type");
15066 : 6 : virtualp = false;
15067 : : }
15068 : : }
15069 : 3166686 : else if (!is_auto (type) && sfk != sfk_conversion)
15070 : : {
15071 : 15 : error_at (typespec_loc, "%qs function with trailing "
15072 : : "return type has %qT as its type rather "
15073 : : "than plain %<auto%>", name, type);
15074 : 272 : return error_mark_node;
15075 : : }
15076 : 3166671 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15077 : : {
15078 : 9 : if (funcdecl_p)
15079 : 3 : error_at (typespec_loc,
15080 : : "%qs function with trailing return type "
15081 : : "has %<decltype(auto)%> as its type "
15082 : : "rather than plain %<auto%>", name);
15083 : : else
15084 : 6 : error_at (typespec_loc,
15085 : : "invalid use of %<decltype(auto)%>");
15086 : 9 : return error_mark_node;
15087 : : }
15088 : 3166662 : else if (is_constrained_auto (type))
15089 : : {
15090 : 6 : if (funcdecl_p)
15091 : 3 : error_at (typespec_loc,
15092 : : "%qs function with trailing return type "
15093 : : "has constrained %<auto%> type specifier "
15094 : : "rather than plain %<auto%>",
15095 : : name);
15096 : : else
15097 : 3 : error_at (typespec_loc,
15098 : : "invalid use of constrained %<auto%> type");
15099 : 6 : return error_mark_node;
15100 : : }
15101 : 6150117 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15102 : 6150117 : if (!tmpl)
15103 : 5037901 : if (tree late_auto = type_uses_auto (late_return_type))
15104 : 25086 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15105 : 5037901 : if (tmpl)
15106 : : {
15107 : 1112220 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
15108 : : {
15109 : 11 : auto_diagnostic_group g;
15110 : 11 : error_at (typespec_loc, "deduced class "
15111 : : "type %qD in function return type",
15112 : 11 : DECL_NAME (tmpl));
15113 : 11 : inform (DECL_SOURCE_LOCATION (tmpl),
15114 : : "%qD declared here", tmpl);
15115 : 11 : return error_mark_node;
15116 : 11 : }
15117 : 1112209 : else if (!late_return_type)
15118 : : {
15119 : 3 : auto_diagnostic_group d;
15120 : 3 : error_at (declarator->id_loc, "deduction guide "
15121 : : "for %qT must have trailing return "
15122 : 3 : "type", TREE_TYPE (tmpl));
15123 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15124 : : "%qD declared here", tmpl);
15125 : 3 : return error_mark_node;
15126 : 3 : }
15127 : 1112206 : else if (CLASS_TYPE_P (late_return_type)
15128 : 1112206 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15129 : 2224412 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15130 : : == tmpl))
15131 : : /* OK */;
15132 : : else
15133 : 0 : error ("trailing return type %qT of deduction guide "
15134 : : "is not a specialization of %qT",
15135 : 0 : late_return_type, TREE_TYPE (tmpl));
15136 : : }
15137 : : }
15138 : 175190517 : else if (late_return_type
15139 : 175190517 : && sfk != sfk_conversion)
15140 : : {
15141 : 37 : if (late_return_type == error_mark_node)
15142 : : return error_mark_node;
15143 : 37 : if (cxx_dialect < cxx11)
15144 : : /* Not using maybe_warn_cpp0x because this should
15145 : : always be an error. */
15146 : 1 : error_at (typespec_loc,
15147 : : "trailing return type only available "
15148 : : "with %<-std=c++11%> or %<-std=gnu++11%>");
15149 : : else
15150 : 36 : error_at (typespec_loc, "%qs function with trailing "
15151 : : "return type not declared with %<auto%> "
15152 : : "type specifier", name);
15153 : 37 : return error_mark_node;
15154 : : }
15155 : 181340583 : if (late_return_type && sfk == sfk_conversion)
15156 : : {
15157 : 14 : error ("a conversion function cannot have a trailing return type");
15158 : 14 : return error_mark_node;
15159 : : }
15160 : 181340569 : type = splice_late_return_type (type, late_return_type);
15161 : 181340569 : if (type == error_mark_node)
15162 : : return error_mark_node;
15163 : :
15164 : 181340493 : if (late_return_type)
15165 : : {
15166 : 3166568 : late_return_type_p = true;
15167 : 3166568 : type_quals = cp_type_quals (type);
15168 : : }
15169 : :
15170 : 181340493 : if (type_quals != TYPE_UNQUALIFIED)
15171 : : {
15172 : : /* It's wrong, for instance, to issue a -Wignored-qualifiers
15173 : : warning for
15174 : : static_assert(!is_same_v<void(*)(), const void(*)()>);
15175 : : because there the qualifier matters. */
15176 : 12466 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15177 : 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15178 : : "qualifiers ignored on function return type");
15179 : : /* [dcl.fct] "A volatile-qualified return type is
15180 : : deprecated." */
15181 : 12466 : if (type_quals & TYPE_QUAL_VOLATILE)
15182 : 599 : warning_at (typespec_loc, OPT_Wvolatile,
15183 : : "%<volatile%>-qualified return type is "
15184 : : "deprecated");
15185 : :
15186 : : /* We now know that the TYPE_QUALS don't apply to the
15187 : : decl, but to its return type. */
15188 : : type_quals = TYPE_UNQUALIFIED;
15189 : : }
15190 : :
15191 : : /* Error about some types functions can't return. */
15192 : :
15193 : 181340493 : if (TREE_CODE (type) == FUNCTION_TYPE)
15194 : : {
15195 : 30 : error_at (typespec_loc, "%qs declared as function returning "
15196 : : "a function", name);
15197 : 30 : return error_mark_node;
15198 : : }
15199 : 181340463 : if (TREE_CODE (type) == ARRAY_TYPE)
15200 : : {
15201 : 6 : error_at (typespec_loc, "%qs declared as function returning "
15202 : : "an array", name);
15203 : 6 : return error_mark_node;
15204 : : }
15205 : 181340457 : if (constinit_p && funcdecl_p)
15206 : : {
15207 : 15 : error_at (declspecs->locations[ds_constinit],
15208 : : "%<constinit%> on function return type is not "
15209 : : "allowed");
15210 : 15 : return error_mark_node;
15211 : : }
15212 : :
15213 : 181340442 : if (check_decltype_auto (typespec_loc, type))
15214 : 50 : return error_mark_node;
15215 : :
15216 : 181340392 : if (ctype == NULL_TREE
15217 : 181340392 : && decl_context == FIELD
15218 : : && funcdecl_p
15219 : 98518475 : && friendp == 0)
15220 : 93621012 : ctype = current_class_type;
15221 : :
15222 : 181340392 : if (ctype && (sfk == sfk_constructor
15223 : 93621012 : || sfk == sfk_destructor))
15224 : : {
15225 : : /* We are within a class's scope. If our declarator name
15226 : : is the same as the class name, and we are defining
15227 : : a function, then it is a constructor/destructor, and
15228 : : therefore returns a void type. */
15229 : :
15230 : : /* ISO C++ 12.4/2. A destructor may not be declared
15231 : : const or volatile. A destructor may not be static.
15232 : : A destructor may not be declared with ref-qualifier.
15233 : :
15234 : : ISO C++ 12.1. A constructor may not be declared
15235 : : const or volatile. A constructor may not be
15236 : : virtual. A constructor may not be static.
15237 : : A constructor may not be declared with ref-qualifier. */
15238 : 25070264 : if (staticp == 2)
15239 : 9 : error_at (declspecs->locations[ds_storage_class],
15240 : : (flags == DTOR_FLAG)
15241 : : ? G_("destructor cannot be static member "
15242 : : "function")
15243 : : : G_("constructor cannot be static member "
15244 : : "function"));
15245 : 25070264 : if (memfn_quals)
15246 : : {
15247 : 9 : error ((flags == DTOR_FLAG)
15248 : : ? G_("destructors may not be cv-qualified")
15249 : : : G_("constructors may not be cv-qualified"));
15250 : 9 : memfn_quals = TYPE_UNQUALIFIED;
15251 : : }
15252 : :
15253 : 25070264 : if (rqual)
15254 : : {
15255 : 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
15256 : 6 : error ((flags == DTOR_FLAG)
15257 : : ? G_("destructors may not be ref-qualified")
15258 : : : G_("constructors may not be ref-qualified"));
15259 : 6 : rqual = REF_QUAL_NONE;
15260 : : }
15261 : :
15262 : 25070264 : if (decl_context == FIELD
15263 : 25070264 : && !member_function_or_else (ctype,
15264 : : current_class_type,
15265 : : flags))
15266 : 0 : return error_mark_node;
15267 : :
15268 : 25070264 : if (flags != DTOR_FLAG)
15269 : : {
15270 : : /* It's a constructor. */
15271 : 22009945 : if (explicitp == 1)
15272 : 5202447 : explicitp = 2;
15273 : 22009945 : if (virtualp)
15274 : : {
15275 : 3 : permerror (declspecs->locations[ds_virtual],
15276 : : "constructors cannot be declared %<virtual%>");
15277 : 3 : virtualp = 0;
15278 : : }
15279 : 22009945 : if (decl_context == FIELD
15280 : 22009945 : && sfk != sfk_constructor)
15281 : 0 : return error_mark_node;
15282 : : }
15283 : 25070264 : if (decl_context == FIELD)
15284 : 25070264 : staticp = 0;
15285 : : }
15286 : 156270128 : else if (friendp)
15287 : : {
15288 : 4581322 : if (virtualp)
15289 : : {
15290 : : /* Cannot be both friend and virtual. */
15291 : 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15292 : 12 : richloc.add_range (declspecs->locations[ds_friend]);
15293 : 12 : error_at (&richloc, "virtual functions cannot be friends");
15294 : 12 : friendp = 0;
15295 : 12 : }
15296 : 4581322 : if (decl_context == NORMAL)
15297 : 0 : error_at (declarator->id_loc,
15298 : : "friend declaration not in class definition");
15299 : 4581322 : if (current_function_decl && funcdef_flag)
15300 : : {
15301 : 9 : error_at (declarator->id_loc,
15302 : : "cannot define friend function %qs in a local "
15303 : : "class definition", name);
15304 : 9 : friendp = 0;
15305 : : }
15306 : : /* [class.friend]/6: A function can be defined in a friend
15307 : : declaration if the function name is unqualified. */
15308 : 4581322 : if (funcdef_flag && in_namespace)
15309 : : {
15310 : 6 : if (in_namespace == global_namespace)
15311 : 3 : error_at (declarator->id_loc,
15312 : : "friend function definition %qs cannot have "
15313 : : "a name qualified with %<::%>", name);
15314 : : else
15315 : 3 : error_at (declarator->id_loc,
15316 : : "friend function definition %qs cannot have "
15317 : : "a name qualified with %<%D::%>", name,
15318 : : in_namespace);
15319 : : }
15320 : : }
15321 : 151688806 : else if (ctype && sfk == sfk_conversion)
15322 : : {
15323 : 676397 : if (explicitp == 1)
15324 : : {
15325 : 323606 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
15326 : 323606 : explicitp = 2;
15327 : : }
15328 : : }
15329 : 151012409 : else if (sfk == sfk_deduction_guide)
15330 : : {
15331 : 1112206 : if (explicitp == 1)
15332 : 181340392 : explicitp = 2;
15333 : : }
15334 : :
15335 : 181340392 : if (xobj_parm)
15336 : : {
15337 : 90265 : if (!ctype
15338 : 90265 : && decl_context == NORMAL
15339 : 49 : && (in_namespace
15340 : 49 : || !declarator->declarator->u.id.qualifying_scope))
15341 : 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15342 : : "a non-member function cannot have "
15343 : : "an explicit object parameter");
15344 : : else
15345 : : {
15346 : 90261 : if (virtualp)
15347 : : {
15348 : 16 : auto_diagnostic_group d;
15349 : 16 : error_at (declspecs->locations[ds_virtual],
15350 : : "an explicit object member function cannot "
15351 : : "be %<virtual%>");
15352 : 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15353 : : "explicit object parameter declared here");
15354 : 16 : virtualp = false;
15355 : 16 : }
15356 : 90261 : if (staticp >= 2)
15357 : : {
15358 : 2 : auto_diagnostic_group d;
15359 : 2 : error_at (declspecs->locations[ds_storage_class],
15360 : : "an explicit object member function cannot "
15361 : : "be %<static%>");
15362 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15363 : : "explicit object parameter declared here");
15364 : 2 : }
15365 : 90261 : if (unqualified_id
15366 : 181430647 : && identifier_p (unqualified_id)
15367 : 180516 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15368 : 8 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15369 : : "%qD cannot be an explicit object member "
15370 : : "function", unqualified_id);
15371 : : }
15372 : : }
15373 : 181340392 : tree pushed_scope = NULL_TREE;
15374 : 181340392 : if (funcdecl_p
15375 : 181340392 : && decl_context != FIELD
15376 : 80760926 : && inner_declarator->u.id.qualifying_scope
15377 : 190634326 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15378 : 9292602 : pushed_scope
15379 : 9292602 : = push_scope (inner_declarator->u.id.qualifying_scope);
15380 : :
15381 : 181340392 : arg_types = grokparms (declarator->u.function.parameters, &parms);
15382 : :
15383 : 181340392 : if (pushed_scope)
15384 : 9292602 : pop_scope (pushed_scope);
15385 : :
15386 : 181340392 : if (inner_declarator
15387 : 180864458 : && inner_declarator->kind == cdk_id
15388 : 178963260 : && inner_declarator->u.id.sfk == sfk_destructor
15389 : 3236309 : && arg_types != void_list_node)
15390 : : {
15391 : 20 : error_at (declarator->id_loc,
15392 : : "destructors may not have parameters");
15393 : 20 : arg_types = void_list_node;
15394 : 20 : parms = NULL_TREE;
15395 : 20 : is_xobj_member_function = false;
15396 : : }
15397 : :
15398 : 181340392 : type = cp_build_function_type (type, arg_types);
15399 : :
15400 : 181340392 : tree attrs = declarator->std_attributes;
15401 : 181340392 : if (tx_qual)
15402 : : {
15403 : 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
15404 : : /* transaction_safe applies to the type, but
15405 : : transaction_safe_dynamic applies to the function. */
15406 : 242 : if (is_attribute_p ("transaction_safe", tx_qual))
15407 : 213 : attrs = attr_chainon (attrs, att);
15408 : : else
15409 : 29 : returned_attrs = attr_chainon (returned_attrs, att);
15410 : : }
15411 : :
15412 : : /* Actually apply the contract attributes to the declaration. */
15413 : 181341558 : for (tree *p = &attrs; *p;)
15414 : : {
15415 : 1166 : tree l = *p;
15416 : 1166 : if (cxx_contract_attribute_p (l))
15417 : : {
15418 : 667 : *p = TREE_CHAIN (l);
15419 : : /* Intentionally reverse order of contracts so they're
15420 : : reversed back into their lexical order. */
15421 : 667 : TREE_CHAIN (l) = NULL_TREE;
15422 : 667 : returned_attrs = chainon (l, returned_attrs);
15423 : : }
15424 : : else
15425 : 499 : p = &TREE_CHAIN (l);
15426 : : }
15427 : :
15428 : 181340392 : if (attrs)
15429 : : /* [dcl.fct]/2:
15430 : :
15431 : : The optional attribute-specifier-seq appertains to
15432 : : the function type. */
15433 : 487 : cplus_decl_attributes (&type, attrs, 0);
15434 : :
15435 : 181340392 : if (raises)
15436 : 71758786 : type = build_exception_variant (type, raises);
15437 : : }
15438 : 181340392 : break;
15439 : :
15440 : 184757135 : case cdk_pointer:
15441 : 184757135 : case cdk_reference:
15442 : 184757135 : case cdk_ptrmem:
15443 : : /* Filter out pointers-to-references and references-to-references.
15444 : : We can get these if a TYPE_DECL is used. */
15445 : :
15446 : 184757135 : if (TYPE_REF_P (type))
15447 : : {
15448 : 238 : if (declarator->kind != cdk_reference)
15449 : : {
15450 : 3 : error ("cannot declare pointer to %q#T", type);
15451 : 3 : type = TREE_TYPE (type);
15452 : : }
15453 : :
15454 : : /* In C++0x, we allow reference to reference declarations
15455 : : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15456 : : and template type arguments [14.3.1/4 temp.arg.type]. The
15457 : : check for direct reference to reference declarations, which
15458 : : are still forbidden, occurs below. Reasoning behind the change
15459 : : can be found in DR106, DR540, and the rvalue reference
15460 : : proposals. */
15461 : 235 : else if (cxx_dialect == cxx98)
15462 : : {
15463 : 0 : error ("cannot declare reference to %q#T", type);
15464 : 0 : type = TREE_TYPE (type);
15465 : : }
15466 : : }
15467 : 184756897 : else if (VOID_TYPE_P (type))
15468 : : {
15469 : 4793649 : if (declarator->kind == cdk_reference)
15470 : 4 : error ("cannot declare reference to %q#T", type);
15471 : 4793645 : else if (declarator->kind == cdk_ptrmem)
15472 : 3 : error ("cannot declare pointer to %q#T member", type);
15473 : : }
15474 : :
15475 : : /* We now know that the TYPE_QUALS don't apply to the decl,
15476 : : but to the target of the pointer. */
15477 : 184757135 : type_quals = TYPE_UNQUALIFIED;
15478 : :
15479 : : /* This code used to handle METHOD_TYPE, but I don't think it's
15480 : : possible to get it here anymore. */
15481 : 184757135 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15482 : 184757135 : if (declarator->kind == cdk_ptrmem
15483 : 1134133 : && TREE_CODE (type) == FUNCTION_TYPE)
15484 : : {
15485 : 931386 : memfn_quals |= type_memfn_quals (type);
15486 : 1862772 : type = build_memfn_type (type,
15487 : 931386 : declarator->u.pointer.class_type,
15488 : : memfn_quals,
15489 : : rqual);
15490 : 931386 : if (type == error_mark_node)
15491 : : return error_mark_node;
15492 : :
15493 : : rqual = REF_QUAL_NONE;
15494 : : memfn_quals = TYPE_UNQUALIFIED;
15495 : : }
15496 : :
15497 : 184757132 : if (TREE_CODE (type) == FUNCTION_TYPE
15498 : 184757132 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15499 : 1029903 : || type_memfn_rqual (type) != REF_QUAL_NONE))
15500 : 15 : error (declarator->kind == cdk_reference
15501 : : ? G_("cannot declare reference to qualified function type %qT")
15502 : : : G_("cannot declare pointer to qualified function type %qT"),
15503 : : type);
15504 : :
15505 : : /* When the pointed-to type involves components of variable size,
15506 : : care must be taken to ensure that the size evaluation code is
15507 : : emitted early enough to dominate all the possible later uses
15508 : : and late enough for the variables on which it depends to have
15509 : : been assigned.
15510 : :
15511 : : This is expected to happen automatically when the pointed-to
15512 : : type has a name/declaration of it's own, but special attention
15513 : : is required if the type is anonymous.
15514 : :
15515 : : We handle the NORMAL and FIELD contexts here by inserting a
15516 : : dummy statement that just evaluates the size at a safe point
15517 : : and ensures it is not deferred until e.g. within a deeper
15518 : : conditional context (c++/43555).
15519 : :
15520 : : We expect nothing to be needed here for PARM or TYPENAME.
15521 : : Evaluating the size at this point for TYPENAME would
15522 : : actually be incorrect, as we might be in the middle of an
15523 : : expression with side effects on the pointed-to type size
15524 : : "arguments" prior to the pointer declaration point and the
15525 : : size evaluation could end up prior to the side effects. */
15526 : :
15527 : 184757132 : if (!TYPE_NAME (type)
15528 : 5452635 : && (decl_context == NORMAL || decl_context == FIELD)
15529 : 625019 : && at_function_scope_p ()
15530 : 184799040 : && variably_modified_type_p (type, NULL_TREE))
15531 : : {
15532 : 96 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15533 : : NULL_TREE, type);
15534 : 96 : add_decl_expr (TYPE_NAME (type));
15535 : : }
15536 : :
15537 : 184757132 : if (declarator->kind == cdk_reference)
15538 : : {
15539 : : /* In C++0x, the type we are creating a reference to might be
15540 : : a typedef which is itself a reference type. In that case,
15541 : : we follow the reference collapsing rules in
15542 : : [7.1.3/8 dcl.typedef] to create the final reference type:
15543 : :
15544 : : "If a typedef TD names a type that is a reference to a type
15545 : : T, an attempt to create the type 'lvalue reference to cv TD'
15546 : : creates the type 'lvalue reference to T,' while an attempt
15547 : : to create the type "rvalue reference to cv TD' creates the
15548 : : type TD."
15549 : : */
15550 : 121765952 : if (VOID_TYPE_P (type))
15551 : : /* We already gave an error. */;
15552 : 121765948 : else if (TYPE_REF_P (type))
15553 : : {
15554 : 235 : if (declarator->u.reference.rvalue_ref)
15555 : : /* Leave type alone. */;
15556 : : else
15557 : 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15558 : : }
15559 : : else
15560 : 121765713 : type = cp_build_reference_type
15561 : 121765713 : (type, declarator->u.reference.rvalue_ref);
15562 : :
15563 : : /* In C++0x, we need this check for direct reference to
15564 : : reference declarations, which are forbidden by
15565 : : [8.3.2/5 dcl.ref]. Reference to reference declarations
15566 : : are only allowed indirectly through typedefs and template
15567 : : type arguments. Example:
15568 : :
15569 : : void foo(int & &); // invalid ref-to-ref decl
15570 : :
15571 : : typedef int & int_ref;
15572 : : void foo(int_ref &); // valid ref-to-ref decl
15573 : : */
15574 : 121765952 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15575 : 0 : error ("cannot declare reference to %q#T, which is not "
15576 : : "a typedef or a template type argument", type);
15577 : : }
15578 : 62991180 : else if (TREE_CODE (type) == METHOD_TYPE)
15579 : 931383 : type = build_ptrmemfunc_type (build_pointer_type (type));
15580 : 62059797 : else if (declarator->kind == cdk_ptrmem)
15581 : : {
15582 : 202747 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15583 : : != NAMESPACE_DECL);
15584 : 202747 : if (declarator->u.pointer.class_type == error_mark_node)
15585 : : /* We will already have complained. */
15586 : 3 : type = error_mark_node;
15587 : : else
15588 : 202744 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15589 : : type);
15590 : : }
15591 : : else
15592 : 61857050 : type = build_pointer_type (type);
15593 : :
15594 : : /* Process a list of type modifier keywords (such as
15595 : : const or volatile) that were given inside the `*' or `&'. */
15596 : :
15597 : 184757132 : if (declarator->u.pointer.qualifiers)
15598 : : {
15599 : 8583375 : type
15600 : 8583375 : = cp_build_qualified_type (type,
15601 : : declarator->u.pointer.qualifiers);
15602 : 8583375 : type_quals = cp_type_quals (type);
15603 : : }
15604 : :
15605 : : /* Apply C++11 attributes to the pointer, and not to the
15606 : : type pointed to. This is unlike what is done for GNU
15607 : : attributes above. It is to comply with [dcl.ptr]/1:
15608 : :
15609 : : [the optional attribute-specifier-seq (7.6.1) appertains
15610 : : to the pointer and not to the object pointed to]. */
15611 : 184757132 : if (declarator->std_attributes)
15612 : 169 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15613 : :
15614 : : ctype = NULL_TREE;
15615 : : break;
15616 : :
15617 : : case cdk_error:
15618 : : break;
15619 : :
15620 : 0 : default:
15621 : 0 : gcc_unreachable ();
15622 : : }
15623 : : }
15624 : :
15625 : 953320934 : id_loc = declarator ? declarator->id_loc : input_location;
15626 : :
15627 : 953320934 : if (innermost_code != cdk_function
15628 : : /* Don't check this if it can be the artifical decltype(auto)
15629 : : we created when building a constraint in a compound-requirement:
15630 : : that the type-constraint is plain is going to be checked in
15631 : : cp_parser_compound_requirement. */
15632 : 953320934 : && decl_context != TYPENAME
15633 : 953320934 : && check_decltype_auto (id_loc, type))
15634 : 20 : return error_mark_node;
15635 : :
15636 : : /* A `constexpr' specifier used in an object declaration declares
15637 : : the object as `const'. */
15638 : 953320914 : if (constexpr_p && innermost_code != cdk_function)
15639 : : {
15640 : : /* DR1688 says that a `constexpr' specifier in combination with
15641 : : `volatile' is valid. */
15642 : :
15643 : 15301136 : if (!TYPE_REF_P (type))
15644 : : {
15645 : 15285918 : type_quals |= TYPE_QUAL_CONST;
15646 : 15285918 : type = cp_build_qualified_type (type, type_quals);
15647 : : }
15648 : : }
15649 : :
15650 : 550938682 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15651 : 2169395 : && !FUNC_OR_METHOD_TYPE_P (type)
15652 : 954556517 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15653 : : {
15654 : 0 : error ("template-id %qD used as a declarator",
15655 : : unqualified_id);
15656 : 0 : unqualified_id = dname;
15657 : : }
15658 : :
15659 : : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
15660 : : qualified with a class-name, turn it into a METHOD_TYPE, unless
15661 : : we know that the function is static. We take advantage of this
15662 : : opportunity to do other processing that pertains to entities
15663 : : explicitly declared to be class members. Note that if DECLARATOR
15664 : : is non-NULL, we know it is a cdk_id declarator; otherwise, we
15665 : : would not have exited the loop above. */
15666 : 953320914 : if (declarator
15667 : 551038049 : && declarator->kind == cdk_id
15668 : 550938682 : && declarator->u.id.qualifying_scope
15669 : 963228246 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
15670 : : {
15671 : 9706265 : ctype = declarator->u.id.qualifying_scope;
15672 : 9706265 : ctype = TYPE_MAIN_VARIANT (ctype);
15673 : 9706265 : template_count = num_template_headers_for_class (ctype);
15674 : :
15675 : 9706265 : if (ctype == current_class_type)
15676 : : {
15677 : 54 : if (friendp)
15678 : : {
15679 : 15 : permerror (declspecs->locations[ds_friend],
15680 : : "member functions are implicitly "
15681 : : "friends of their class");
15682 : 15 : friendp = 0;
15683 : : }
15684 : : else
15685 : 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
15686 : : ctype, name);
15687 : : }
15688 : 9706211 : else if (/* If the qualifying type is already complete, then we
15689 : : can skip the following checks. */
15690 : 9706211 : !COMPLETE_TYPE_P (ctype)
15691 : 148 : && (/* If the function is being defined, then
15692 : : qualifying type must certainly be complete. */
15693 : : funcdef_flag
15694 : : /* A friend declaration of "T::f" is OK, even if
15695 : : "T" is a template parameter. But, if this
15696 : : function is not a friend, the qualifying type
15697 : : must be a class. */
15698 : 111 : || (!friendp && !CLASS_TYPE_P (ctype))
15699 : : /* For a declaration, the type need not be
15700 : : complete, if either it is dependent (since there
15701 : : is no meaningful definition of complete in that
15702 : : case) or the qualifying class is currently being
15703 : : defined. */
15704 : 123 : || !(dependent_type_p (ctype)
15705 : 24 : || currently_open_class (ctype)))
15706 : : /* Check that the qualifying type is complete. */
15707 : 9706275 : && !complete_type_or_else (ctype, NULL_TREE))
15708 : 64 : return error_mark_node;
15709 : 9706147 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15710 : : {
15711 : 9292839 : if (current_class_type
15712 : 264 : && (!friendp || funcdef_flag || initialized))
15713 : : {
15714 : 59 : error_at (id_loc, funcdef_flag || initialized
15715 : : ? G_("cannot define member function %<%T::%s%> "
15716 : : "within %qT")
15717 : : : G_("cannot declare member function %<%T::%s%> "
15718 : : "within %qT"),
15719 : : ctype, name, current_class_type);
15720 : 49 : return error_mark_node;
15721 : : }
15722 : : }
15723 : 413308 : else if (typedef_p && current_class_type)
15724 : : {
15725 : 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
15726 : : ctype, name, current_class_type);
15727 : 0 : return error_mark_node;
15728 : : }
15729 : : }
15730 : :
15731 : 953320801 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
15732 : 53617625 : ctype = current_class_type;
15733 : :
15734 : : /* Now TYPE has the actual type. */
15735 : :
15736 : 953320801 : if (returned_attrs)
15737 : : {
15738 : 3396 : if (attrlist)
15739 : 3396 : *attrlist = attr_chainon (returned_attrs, *attrlist);
15740 : : else
15741 : : attrlist = &returned_attrs;
15742 : : }
15743 : :
15744 : 953320801 : if (declarator
15745 : 551037936 : && declarator->kind == cdk_id
15746 : 550938569 : && declarator->std_attributes
15747 : 367068 : && attrlist != NULL)
15748 : : {
15749 : : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
15750 : : a declarator-id appertains to the entity that is declared. */
15751 : 367065 : if (declarator->std_attributes != error_mark_node)
15752 : 367047 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
15753 : : else
15754 : : /* We should have already diagnosed the issue (c++/78344). */
15755 : 18 : gcc_assert (seen_error ());
15756 : : }
15757 : :
15758 : : /* Handle parameter packs. */
15759 : 953320801 : if (parameter_pack_p)
15760 : : {
15761 : 3004635 : if (decl_context == PARM)
15762 : : /* Turn the type into a pack expansion.*/
15763 : 3004635 : type = make_pack_expansion (type);
15764 : : else
15765 : 0 : error ("non-parameter %qs cannot be a parameter pack", name);
15766 : : }
15767 : :
15768 : 953320801 : if ((decl_context == FIELD || decl_context == PARM)
15769 : 435888654 : && !processing_template_decl
15770 : 1093894153 : && variably_modified_type_p (type, NULL_TREE))
15771 : : {
15772 : 12 : if (decl_context == FIELD)
15773 : 6 : error_at (id_loc,
15774 : : "data member may not have variably modified type %qT", type);
15775 : : else
15776 : 6 : error_at (id_loc,
15777 : : "parameter may not have variably modified type %qT", type);
15778 : 12 : type = error_mark_node;
15779 : : }
15780 : :
15781 : 953320801 : if (explicitp == 1 || (explicitp && friendp))
15782 : : {
15783 : : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
15784 : : in the declaration of a constructor or conversion function within
15785 : : a class definition. */
15786 : 74 : if (!current_class_type)
15787 : 59 : error_at (declspecs->locations[ds_explicit],
15788 : : "%<explicit%> outside class declaration");
15789 : 15 : else if (friendp)
15790 : 12 : error_at (declspecs->locations[ds_explicit],
15791 : : "%<explicit%> in friend declaration");
15792 : : else
15793 : 3 : error_at (declspecs->locations[ds_explicit],
15794 : : "only declarations of constructors and conversion operators "
15795 : : "can be %<explicit%>");
15796 : : explicitp = 0;
15797 : : }
15798 : :
15799 : 953320801 : if (storage_class == sc_mutable)
15800 : : {
15801 : 148991 : location_t sloc = declspecs->locations[ds_storage_class];
15802 : 148991 : if (decl_context != FIELD || friendp)
15803 : : {
15804 : 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
15805 : : name);
15806 : 30 : storage_class = sc_none;
15807 : : }
15808 : 148961 : else if (decl_context == TYPENAME || typedef_p)
15809 : : {
15810 : 0 : error_at (sloc,
15811 : : "non-object member %qs cannot be declared %<mutable%>",
15812 : : name);
15813 : 0 : storage_class = sc_none;
15814 : : }
15815 : 148961 : else if (FUNC_OR_METHOD_TYPE_P (type))
15816 : : {
15817 : 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
15818 : : name);
15819 : 3 : storage_class = sc_none;
15820 : : }
15821 : 148958 : else if (staticp)
15822 : : {
15823 : 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
15824 : : name);
15825 : 0 : storage_class = sc_none;
15826 : : }
15827 : 148958 : else if (type_quals & TYPE_QUAL_CONST)
15828 : : {
15829 : 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
15830 : : name);
15831 : 9 : storage_class = sc_none;
15832 : : }
15833 : 148949 : else if (TYPE_REF_P (type))
15834 : : {
15835 : 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
15836 : : name);
15837 : 6 : storage_class = sc_none;
15838 : : }
15839 : : }
15840 : :
15841 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
15842 : 953320801 : if (typedef_p && decl_context != TYPENAME)
15843 : : {
15844 : 45241084 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
15845 : 45241084 : tree decl;
15846 : :
15847 : 45241084 : if (funcdef_flag)
15848 : : {
15849 : 6 : if (decl_context == NORMAL)
15850 : 3 : error_at (id_loc,
15851 : : "typedef may not be a function definition");
15852 : : else
15853 : 3 : error_at (id_loc,
15854 : : "typedef may not be a member function definition");
15855 : 6 : return error_mark_node;
15856 : : }
15857 : :
15858 : : /* This declaration:
15859 : :
15860 : : typedef void f(int) const;
15861 : :
15862 : : declares a function type which is not a member of any
15863 : : particular class, but which is cv-qualified; for
15864 : : example "f S::*" declares a pointer to a const-qualified
15865 : : member function of S. We record the cv-qualification in the
15866 : : function type. */
15867 : 45241078 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
15868 : : {
15869 : 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
15870 : :
15871 : : /* We have now dealt with these qualifiers. */
15872 : 62 : memfn_quals = TYPE_UNQUALIFIED;
15873 : 62 : rqual = REF_QUAL_NONE;
15874 : : }
15875 : :
15876 : 45241078 : if (type_uses_auto (type))
15877 : : {
15878 : 60 : if (alias_p)
15879 : 20 : error_at (declspecs->locations[ds_type_spec],
15880 : : "%<auto%> not allowed in alias declaration");
15881 : : else
15882 : 40 : error_at (declspecs->locations[ds_type_spec],
15883 : : "typedef declared %<auto%>");
15884 : 60 : type = error_mark_node;
15885 : : }
15886 : :
15887 : 45241078 : if (reqs)
15888 : 3 : error_at (location_of (reqs), "requires-clause on typedef");
15889 : :
15890 : 45241078 : if (id_declarator && declarator->u.id.qualifying_scope)
15891 : : {
15892 : 22 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
15893 : 22 : type = error_mark_node;
15894 : : }
15895 : :
15896 : 45241078 : if (decl_context == FIELD)
15897 : 24513236 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
15898 : : else
15899 : 20727842 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
15900 : :
15901 : 45241078 : if (decl_context != FIELD)
15902 : : {
15903 : 20727842 : if (!current_function_decl)
15904 : 8751220 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
15905 : 11976622 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
15906 : : /* The TYPE_DECL is "abstract" because there will be
15907 : : clones of this constructor/destructor, and there will
15908 : : be copies of this TYPE_DECL generated in those
15909 : : clones. The decloning optimization (for space) may
15910 : : revert this subsequently if it determines that
15911 : : the clones should share a common implementation. */
15912 : 157634 : DECL_ABSTRACT_P (decl) = true;
15913 : :
15914 : 20727842 : set_originating_module (decl);
15915 : : }
15916 : 24513236 : else if (current_class_type
15917 : 24513236 : && constructor_name_p (unqualified_id, current_class_type))
15918 : 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
15919 : : "as enclosing class",
15920 : : unqualified_id);
15921 : :
15922 : : /* If the user declares "typedef struct {...} foo" then the
15923 : : struct will have an anonymous name. Fill that name in now.
15924 : : Nothing can refer to it, so nothing needs know about the name
15925 : : change. */
15926 : 45241078 : if (type != error_mark_node
15927 : 45240996 : && unqualified_id
15928 : 45240993 : && TYPE_NAME (type)
15929 : 42254124 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15930 : 84564016 : && (TYPE_UNNAMED_P (type)
15931 : : /* An enum may have previously used an enumerator for linkage
15932 : : purposes, but we want the typedef name to take priority. */
15933 : 41855571 : || enum_with_enumerator_for_linkage_p (type))
15934 : 398952 : && declspecs->type_definition_p
15935 : 398882 : && attributes_naming_typedef_ok (*attrlist)
15936 : 45639957 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
15937 : 398861 : name_unnamed_type (type, decl);
15938 : :
15939 : 45241078 : if (signed_p
15940 : 45241078 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
15941 : 383597 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
15942 : :
15943 : 45241078 : bad_specifiers (decl, BSP_TYPE, virtualp,
15944 : : memfn_quals != TYPE_UNQUALIFIED,
15945 : : inlinep, friendp, raises != NULL_TREE,
15946 : : declspecs->locations);
15947 : :
15948 : 45241078 : if (alias_p)
15949 : : /* Acknowledge that this was written:
15950 : : `using analias = atype;'. */
15951 : 17784749 : TYPE_DECL_ALIAS_P (decl) = 1;
15952 : :
15953 : 45241078 : return decl;
15954 : : }
15955 : :
15956 : : /* Detect the case of an array type of unspecified size
15957 : : which came, as such, direct from a typedef name.
15958 : : We must copy the type, so that the array's domain can be
15959 : : individually set by the object's initializer. */
15960 : :
15961 : 908079717 : if (type && typedef_type
15962 : 880233223 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
15963 : 908614857 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
15964 : 79 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15965 : :
15966 : : /* Detect where we're using a typedef of function type to declare a
15967 : : function. PARMS will not be set, so we must create it now. */
15968 : :
15969 : 908079717 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
15970 : : {
15971 : 15611 : tree decls = NULL_TREE;
15972 : 15611 : tree args;
15973 : :
15974 : 15611 : for (args = TYPE_ARG_TYPES (type);
15975 : 33685 : args && args != void_list_node;
15976 : 18074 : args = TREE_CHAIN (args))
15977 : : {
15978 : 18074 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
15979 : 18074 : TREE_VALUE (args));
15980 : :
15981 : 18074 : DECL_CHAIN (decl) = decls;
15982 : 18074 : decls = decl;
15983 : : }
15984 : :
15985 : 15611 : parms = nreverse (decls);
15986 : :
15987 : 15611 : if (decl_context != TYPENAME)
15988 : : {
15989 : : /* The qualifiers on the function type become the qualifiers on
15990 : : the non-static member function. */
15991 : 14921 : memfn_quals |= type_memfn_quals (type);
15992 : 14921 : rqual = type_memfn_rqual (type);
15993 : 14921 : type_quals = TYPE_UNQUALIFIED;
15994 : 14921 : raises = TYPE_RAISES_EXCEPTIONS (type);
15995 : : }
15996 : : }
15997 : :
15998 : : /* If this is a type name (such as, in a cast or sizeof),
15999 : : compute the type and return it now. */
16000 : :
16001 : 908079027 : if (decl_context == TYPENAME)
16002 : : {
16003 : : /* Note that here we don't care about type_quals. */
16004 : :
16005 : : /* Special case: "friend class foo" looks like a TYPENAME context. */
16006 : 357232045 : if (friendp)
16007 : : {
16008 : 0 : if (inlinep)
16009 : : {
16010 : 0 : error ("%<inline%> specified for friend class declaration");
16011 : 0 : inlinep = 0;
16012 : : }
16013 : :
16014 : 0 : if (!current_aggr)
16015 : : {
16016 : : /* Don't allow friend declaration without a class-key. */
16017 : 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16018 : 0 : permerror (input_location, "template parameters cannot be friends");
16019 : 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
16020 : 0 : permerror (input_location, "friend declaration requires class-key, "
16021 : : "i.e. %<friend class %T::%D%>",
16022 : 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
16023 : : else
16024 : 0 : permerror (input_location, "friend declaration requires class-key, "
16025 : : "i.e. %<friend %#T%>",
16026 : : type);
16027 : : }
16028 : :
16029 : : /* Only try to do this stuff if we didn't already give up. */
16030 : 0 : if (type != integer_type_node)
16031 : : {
16032 : : /* A friendly class? */
16033 : 0 : if (current_class_type)
16034 : 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
16035 : : /*complain=*/true);
16036 : : else
16037 : 0 : error ("trying to make class %qT a friend of global scope",
16038 : : type);
16039 : :
16040 : 0 : type = void_type_node;
16041 : : }
16042 : : }
16043 : 357232045 : else if (memfn_quals || rqual)
16044 : : {
16045 : 63866 : if (ctype == NULL_TREE
16046 : 63866 : && TREE_CODE (type) == METHOD_TYPE)
16047 : 0 : ctype = TYPE_METHOD_BASETYPE (type);
16048 : :
16049 : 63866 : if (ctype)
16050 : 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
16051 : : /* Core issue #547: need to allow this in template type args.
16052 : : Allow it in general in C++11 for alias-declarations. */
16053 : 63866 : else if ((template_type_arg || cxx_dialect >= cxx11)
16054 : 63864 : && TREE_CODE (type) == FUNCTION_TYPE)
16055 : 63862 : type = apply_memfn_quals (type, memfn_quals, rqual);
16056 : : else
16057 : 4 : error ("invalid qualifiers on non-member function type");
16058 : : }
16059 : :
16060 : 357232045 : if (reqs)
16061 : 3 : error_at (location_of (reqs), "requires-clause on type-id");
16062 : :
16063 : 357232045 : return type;
16064 : : }
16065 : 550847672 : else if (unqualified_id == NULL_TREE && decl_context != PARM
16066 : 730493 : && decl_context != CATCHPARM
16067 : 238431 : && TREE_CODE (type) != UNION_TYPE
16068 : 238191 : && ! bitfield
16069 : 238191 : && innermost_code != cdk_decomp)
16070 : : {
16071 : 9 : error ("abstract declarator %qT used as declaration", type);
16072 : 9 : return error_mark_node;
16073 : : }
16074 : :
16075 : 550847663 : if (!FUNC_OR_METHOD_TYPE_P (type))
16076 : : {
16077 : : /* Only functions may be declared using an operator-function-id. */
16078 : 371923842 : if (dname && IDENTIFIER_ANY_OP_P (dname))
16079 : : {
16080 : 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
16081 : 8 : return error_mark_node;
16082 : : }
16083 : :
16084 : 371923834 : if (reqs)
16085 : 15 : error_at (location_of (reqs),
16086 : : "requires-clause on declaration of non-function type %qT",
16087 : : type);
16088 : : }
16089 : :
16090 : : /* We don't check parameter types here because we can emit a better
16091 : : error message later. */
16092 : 550847655 : if (decl_context != PARM)
16093 : : {
16094 : 266778917 : type = check_var_type (unqualified_id, type, id_loc);
16095 : 266778917 : if (type == error_mark_node)
16096 : : return error_mark_node;
16097 : : }
16098 : :
16099 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16100 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16101 : :
16102 : 550847579 : if (decl_context == PARM || decl_context == CATCHPARM)
16103 : : {
16104 : 284575380 : if (ctype || in_namespace)
16105 : 0 : error ("cannot use %<::%> in parameter declaration");
16106 : :
16107 : 284575380 : tree auto_node = type_uses_auto (type);
16108 : 284575380 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16109 : : {
16110 : 120 : bool err_p = true;
16111 : 120 : if (cxx_dialect >= cxx14)
16112 : : {
16113 : 212 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16114 : 3 : error_at (typespec_loc,
16115 : : "cannot declare a parameter with %<decltype(auto)%>");
16116 : 109 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16117 : : {
16118 : 5 : auto_diagnostic_group g;
16119 : 5 : gcc_rich_location richloc (typespec_loc);
16120 : 5 : richloc.add_fixit_insert_after ("<>");
16121 : 5 : error_at (&richloc,
16122 : : "missing template argument list after %qE; template "
16123 : : "placeholder not permitted in parameter", c);
16124 : 5 : if (decl_context == PARM && cxx_dialect >= cxx20)
16125 : 4 : inform (typespec_loc, "or use %<auto%> for an "
16126 : : "abbreviated function template");
16127 : 5 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16128 : 5 : }
16129 : 104 : else if (decl_context == CATCHPARM || template_parm_flag)
16130 : 14 : error_at (typespec_loc,
16131 : : "%<auto%> parameter not permitted in this context");
16132 : : else
16133 : : /* Do not issue an error while tentatively parsing a function
16134 : : parameter: for T t(auto(a), 42);, when we just saw the 1st
16135 : : parameter, we don't know yet that this construct won't be
16136 : : a function declaration. Defer the checking to
16137 : : cp_parser_parameter_declaration_clause. */
16138 : : err_p = false;
16139 : : }
16140 : : else
16141 : 8 : error_at (typespec_loc, "parameter declared %<auto%>");
16142 : 30 : if (err_p)
16143 : 30 : type = error_mark_node;
16144 : : }
16145 : :
16146 : : /* A parameter declared as an array of T is really a pointer to T.
16147 : : One declared as a function is really a pointer to a function.
16148 : : One declared as a member is really a pointer to member. */
16149 : :
16150 : 284575380 : if (TREE_CODE (type) == ARRAY_TYPE)
16151 : : {
16152 : : /* Transfer const-ness of array into that of type pointed to. */
16153 : 751553 : type = build_pointer_type (TREE_TYPE (type));
16154 : 751553 : type_quals = TYPE_UNQUALIFIED;
16155 : 751553 : array_parameter_p = true;
16156 : : }
16157 : 283823827 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16158 : 108230 : type = build_pointer_type (type);
16159 : : }
16160 : :
16161 : 132431455 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16162 : : /* Don't convert xobj member functions to METHOD_TYPE. */
16163 : 102913635 : && !is_xobj_member_function
16164 : 644425432 : && !(unqualified_id
16165 : 187142317 : && identifier_p (unqualified_id)
16166 : 93577490 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16167 : : {
16168 : 93564827 : cp_cv_quals real_quals = memfn_quals;
16169 : 93564827 : if (cxx_dialect < cxx14 && constexpr_p
16170 : 654810 : && sfk != sfk_constructor && sfk != sfk_destructor)
16171 : 3807 : real_quals |= TYPE_QUAL_CONST;
16172 : 93564827 : type = build_memfn_type (type, ctype, real_quals, rqual);
16173 : : }
16174 : :
16175 : 550847579 : {
16176 : 550847579 : tree decl = NULL_TREE;
16177 : :
16178 : 550847579 : if (decl_context == PARM)
16179 : : {
16180 : 284068738 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16181 : 284068738 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16182 : :
16183 : 284068738 : bad_specifiers (decl, BSP_PARM, virtualp,
16184 : : memfn_quals != TYPE_UNQUALIFIED,
16185 : : inlinep, friendp, raises != NULL_TREE,
16186 : : declspecs->locations);
16187 : : }
16188 : 266778841 : else if (decl_context == FIELD)
16189 : : {
16190 : 127306665 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16191 : 17596748 : if (tree auto_node = type_uses_auto (type))
16192 : : {
16193 : 24 : location_t tloc = declspecs->locations[ds_type_spec];
16194 : 24 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16195 : 6 : error_at (tloc, "invalid use of template-name %qE without an "
16196 : : "argument list",
16197 : 6 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16198 : : else
16199 : 18 : error_at (tloc, "non-static data member declared with "
16200 : : "placeholder %qT", auto_node);
16201 : 24 : type = error_mark_node;
16202 : : }
16203 : :
16204 : : /* The C99 flexible array extension. */
16205 : 106553348 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16206 : 128436274 : && TYPE_DOMAIN (type) == NULL_TREE)
16207 : : {
16208 : 1396 : if (ctype
16209 : 1396 : && (TREE_CODE (ctype) == UNION_TYPE
16210 : 1396 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16211 : 84 : pedwarn (id_loc, OPT_Wpedantic,
16212 : : "flexible array member in union is a GCC extension");
16213 : :
16214 : : else
16215 : : {
16216 : : /* Array is a flexible member. */
16217 : 1312 : if (name)
16218 : 1312 : pedwarn (id_loc, OPT_Wpedantic,
16219 : : "ISO C++ forbids flexible array member %qs", name);
16220 : : else
16221 : : pedwarn (input_location, OPT_Wpedantic,
16222 : : "ISO C++ forbids flexible array members");
16223 : :
16224 : : /* Flexible array member has a null domain. */
16225 : 1312 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16226 : : }
16227 : : }
16228 : :
16229 : 127306665 : if (type == error_mark_node)
16230 : : {
16231 : : /* Happens when declaring arrays of sizes which
16232 : : are error_mark_node, for example. */
16233 : 24 : decl = NULL_TREE;
16234 : : }
16235 : 127306641 : else if (in_namespace && !friendp)
16236 : : {
16237 : : /* Something like struct S { int N::j; }; */
16238 : 6 : error_at (id_loc, "invalid use of %<::%>");
16239 : 6 : return error_mark_node;
16240 : : }
16241 : 127306635 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16242 : : {
16243 : 98202110 : int publicp = 0;
16244 : 98202110 : tree function_context;
16245 : :
16246 : 98202110 : if (friendp == 0)
16247 : : {
16248 : : /* This should never happen in pure C++ (the check
16249 : : could be an assert). It could happen in
16250 : : Objective-C++ if someone writes invalid code that
16251 : : uses a function declaration for an instance
16252 : : variable or property (instance variables and
16253 : : properties are parsed as FIELD_DECLs, but they are
16254 : : part of an Objective-C class, not a C++ class).
16255 : : That code is invalid and is caught by this
16256 : : check. */
16257 : 93620845 : if (!ctype)
16258 : : {
16259 : 0 : error ("declaration of function %qD in invalid context",
16260 : : unqualified_id);
16261 : 0 : return error_mark_node;
16262 : : }
16263 : :
16264 : : /* ``A union may [ ... ] not [ have ] virtual functions.''
16265 : : ARM 9.5 */
16266 : 93620845 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16267 : : {
16268 : 3 : error_at (declspecs->locations[ds_virtual],
16269 : : "function %qD declared %<virtual%> inside a union",
16270 : : unqualified_id);
16271 : 3 : return error_mark_node;
16272 : : }
16273 : :
16274 : 4141727 : if (virtualp
16275 : 102343834 : && identifier_p (unqualified_id)
16276 : 4141727 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16277 : : {
16278 : 9 : error_at (declspecs->locations[ds_virtual],
16279 : : "%qD cannot be declared %<virtual%>, since it "
16280 : : "is always static", unqualified_id);
16281 : 9 : virtualp = 0;
16282 : : }
16283 : : }
16284 : :
16285 : : /* Check that the name used for a destructor makes sense. */
16286 : 98202107 : if (sfk == sfk_destructor)
16287 : : {
16288 : 3060355 : tree uqname = id_declarator->u.id.unqualified_name;
16289 : :
16290 : 3060355 : if (!ctype)
16291 : : {
16292 : 12 : gcc_assert (friendp);
16293 : 12 : error_at (id_loc, "expected qualified name in friend "
16294 : : "declaration for destructor %qD", uqname);
16295 : 12 : return error_mark_node;
16296 : : }
16297 : :
16298 : 3060343 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16299 : : {
16300 : 9 : error_at (id_loc, "declaration of %qD as member of %qT",
16301 : : uqname, ctype);
16302 : 9 : return error_mark_node;
16303 : : }
16304 : 3060334 : if (concept_p)
16305 : : {
16306 : 9 : error_at (declspecs->locations[ds_concept],
16307 : : "a destructor cannot be %qs", "concept");
16308 : 9 : return error_mark_node;
16309 : : }
16310 : 3060325 : if (constexpr_p && cxx_dialect < cxx20)
16311 : : {
16312 : 6 : error_at (declspecs->locations[ds_constexpr],
16313 : : "%<constexpr%> destructors only available"
16314 : : " with %<-std=c++20%> or %<-std=gnu++20%>");
16315 : 6 : return error_mark_node;
16316 : : }
16317 : 3060319 : if (consteval_p)
16318 : : {
16319 : 3 : error_at (declspecs->locations[ds_consteval],
16320 : : "a destructor cannot be %qs", "consteval");
16321 : 3 : return error_mark_node;
16322 : : }
16323 : : }
16324 : 95141752 : else if (sfk == sfk_constructor && friendp && !ctype)
16325 : : {
16326 : 0 : error ("expected qualified name in friend declaration "
16327 : : "for constructor %qD",
16328 : 0 : id_declarator->u.id.unqualified_name);
16329 : 0 : return error_mark_node;
16330 : : }
16331 : 98202068 : if (sfk == sfk_constructor)
16332 : 22009969 : if (concept_p)
16333 : : {
16334 : 9 : error_at (declspecs->locations[ds_concept],
16335 : : "a constructor cannot be %<concept%>");
16336 : 9 : return error_mark_node;
16337 : : }
16338 : 76192099 : if (concept_p)
16339 : : {
16340 : 9 : error_at (declspecs->locations[ds_concept],
16341 : : "a concept cannot be a member function");
16342 : 9 : concept_p = false;
16343 : : }
16344 : 98202050 : else if (consteval_p
16345 : 98502155 : && identifier_p (unqualified_id)
16346 : 98502146 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16347 : : {
16348 : 9 : error_at (declspecs->locations[ds_consteval],
16349 : : "%qD cannot be %qs", unqualified_id, "consteval");
16350 : 9 : consteval_p = false;
16351 : : }
16352 : :
16353 : 98202059 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16354 : : {
16355 : 50315 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
16356 : 50315 : if (variable_template_p (tmpl))
16357 : : {
16358 : 3 : auto_diagnostic_group d;
16359 : 3 : error_at (id_loc, "specialization of variable template "
16360 : : "%qD declared as function", tmpl);
16361 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
16362 : : "variable template declared here");
16363 : 3 : return error_mark_node;
16364 : 3 : }
16365 : : }
16366 : :
16367 : : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16368 : 98202056 : function_context
16369 : : = (ctype != NULL_TREE
16370 : 98202056 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16371 : 196404112 : publicp = ((! friendp || ! staticp)
16372 : 98202056 : && function_context == NULL_TREE);
16373 : :
16374 : 98202056 : decl = grokfndecl (ctype, type,
16375 : 98202056 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16376 : : ? unqualified_id : dname,
16377 : : parms,
16378 : : unqualified_id,
16379 : : declspecs,
16380 : : reqs,
16381 : : virtualp, flags, memfn_quals, rqual, raises,
16382 : : friendp ? -1 : 0, friendp, publicp,
16383 : 98202056 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16384 : 98202056 : | (8 * consteval_p),
16385 : : initialized == SD_DELETED,
16386 : : is_xobj_member_function, sfk,
16387 : : funcdef_flag, late_return_type_p,
16388 : : template_count, in_namespace,
16389 : : attrlist, id_loc);
16390 : 98202056 : decl = set_virt_specifiers (decl, virt_specifiers);
16391 : 98202056 : if (decl == NULL_TREE)
16392 : 97 : return error_mark_node;
16393 : : #if 0
16394 : : /* This clobbers the attrs stored in `decl' from `attrlist'. */
16395 : : /* The decl and setting of decl_attr is also turned off. */
16396 : : decl = build_decl_attribute_variant (decl, decl_attr);
16397 : : #endif
16398 : :
16399 : : /* [class.conv.ctor]
16400 : :
16401 : : A constructor declared without the function-specifier
16402 : : explicit that can be called with a single parameter
16403 : : specifies a conversion from the type of its first
16404 : : parameter to the type of its class. Such a constructor
16405 : : is called a converting constructor. */
16406 : 98201959 : if (explicitp == 2)
16407 : 5526053 : DECL_NONCONVERTING_P (decl) = 1;
16408 : :
16409 : 98201959 : if (declspecs->explicit_specifier)
16410 : 229704 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16411 : : }
16412 : 29104525 : else if (!staticp
16413 : 29104525 : && ((current_class_type
16414 : 17596739 : && same_type_p (TYPE_MAIN_VARIANT (type),
16415 : : current_class_type))
16416 : 17596643 : || (!dependent_type_p (type)
16417 : 10898097 : && !COMPLETE_TYPE_P (complete_type (type))
16418 : 1444 : && (!complete_or_array_type_p (type)
16419 : 1369 : || initialized == SD_UNINITIALIZED))))
16420 : : {
16421 : 1507 : if (TREE_CODE (type) != ARRAY_TYPE
16422 : 1507 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16423 : : {
16424 : 171 : if (unqualified_id)
16425 : : {
16426 : 126 : auto_diagnostic_group d;
16427 : 126 : error_at (id_loc, "field %qD has incomplete type %qT",
16428 : : unqualified_id, type);
16429 : 126 : cxx_incomplete_type_inform (strip_array_types (type));
16430 : 126 : }
16431 : : else
16432 : 45 : error ("name %qT has incomplete type", type);
16433 : :
16434 : 171 : type = error_mark_node;
16435 : 171 : decl = NULL_TREE;
16436 : : }
16437 : : }
16438 : 46698238 : else if (!verify_type_context (input_location,
16439 : : staticp
16440 : : ? TCTX_STATIC_STORAGE
16441 : : : TCTX_FIELD, type))
16442 : : {
16443 : 0 : type = error_mark_node;
16444 : 0 : decl = NULL_TREE;
16445 : : }
16446 : : else
16447 : : {
16448 : 29103012 : if (friendp)
16449 : : {
16450 : 12 : if (unqualified_id)
16451 : 9 : error_at (id_loc,
16452 : : "%qE is neither function nor member function; "
16453 : : "cannot be declared friend", unqualified_id);
16454 : : else
16455 : 3 : error ("unnamed field is neither function nor member "
16456 : : "function; cannot be declared friend");
16457 : 12 : return error_mark_node;
16458 : : }
16459 : 29103000 : decl = NULL_TREE;
16460 : : }
16461 : :
16462 : 127306490 : if (friendp)
16463 : : {
16464 : : /* Packages tend to use GNU attributes on friends, so we only
16465 : : warn for standard attributes. */
16466 : 4581234 : if (attrlist
16467 : 4581234 : && !funcdef_flag
16468 : 1377246 : && cxx11_attribute_p (*attrlist)
16469 : 4581263 : && !all_attributes_are_contracts_p (*attrlist))
16470 : : {
16471 : 27 : *attrlist = NULL_TREE;
16472 : 27 : auto_diagnostic_group d;
16473 : 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16474 : 27 : inform (id_loc, "an attribute that appertains to a friend "
16475 : : "declaration that is not a definition is ignored");
16476 : 27 : }
16477 : : /* Friends are treated specially. */
16478 : 4581234 : if (ctype == current_class_type)
16479 : : ; /* We already issued a permerror. */
16480 : 4581234 : else if (decl && DECL_NAME (decl))
16481 : : {
16482 : 4581231 : set_originating_module (decl, true);
16483 : :
16484 : 4581231 : if (initialized)
16485 : : /* Kludge: We need funcdef_flag to be true in do_friend for
16486 : : in-class defaulted functions, but that breaks grokfndecl.
16487 : : So set it here. */
16488 : 70324 : funcdef_flag = true;
16489 : :
16490 : 4581231 : cplus_decl_attributes (&decl, *attrlist, 0);
16491 : 4581231 : *attrlist = NULL_TREE;
16492 : :
16493 : 4581231 : tree scope = ctype ? ctype : in_namespace;
16494 : 4581231 : decl = do_friend (scope, unqualified_id, decl,
16495 : : flags, funcdef_flag);
16496 : 4581231 : return decl;
16497 : : }
16498 : : else
16499 : 3 : return error_mark_node;
16500 : : }
16501 : :
16502 : : /* Structure field. It may not be a function, except for C++. */
16503 : :
16504 : 122725256 : if (decl == NULL_TREE)
16505 : : {
16506 : 29104528 : if (staticp)
16507 : : {
16508 : : /* C++ allows static class members. All other work
16509 : : for this is done by grokfield. */
16510 : 11507786 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
16511 : : dname, type);
16512 : 11507786 : if (unqualified_id
16513 : 11507783 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16514 : : {
16515 : 76410 : decl = check_explicit_specialization (unqualified_id, decl,
16516 : : template_count,
16517 : : concept_p * 8);
16518 : 76410 : if (decl == error_mark_node)
16519 : : return error_mark_node;
16520 : : }
16521 : 11507786 : set_linkage_for_static_data_member (decl);
16522 : 11507786 : if (concept_p)
16523 : 15 : error_at (declspecs->locations[ds_concept],
16524 : : "static data member %qE declared %qs",
16525 : : unqualified_id, "concept");
16526 : 11507771 : else if (constexpr_p && !initialized)
16527 : : {
16528 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
16529 : : "%<constexpr%> static data member %qD must "
16530 : : "have an initializer", decl);
16531 : 9 : constexpr_p = false;
16532 : : }
16533 : 11507786 : if (consteval_p)
16534 : 3 : error_at (declspecs->locations[ds_consteval],
16535 : : "static data member %qE declared %qs",
16536 : : unqualified_id, "consteval");
16537 : :
16538 : 11507786 : if (inlinep)
16539 : 11941 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16540 : :
16541 : 11507786 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16542 : 11507786 : && !(cxx_dialect >= cxx17 && constexpr_p))
16543 : : /* Even if there is an in-class initialization, DECL
16544 : : is considered undefined until an out-of-class
16545 : : definition is provided, unless this is an inline
16546 : : variable. */
16547 : 1774987 : DECL_EXTERNAL (decl) = 1;
16548 : :
16549 : 11507786 : if (thread_p)
16550 : : {
16551 : 420 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16552 : 420 : if (!processing_template_decl)
16553 : 374 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16554 : 420 : if (declspecs->gnu_thread_keyword_p)
16555 : 34 : SET_DECL_GNU_TLS_P (decl);
16556 : : }
16557 : :
16558 : : /* Set the constraints on the declaration. */
16559 : 11507786 : bool memtmpl = (current_template_depth
16560 : 11507786 : > template_class_depth (current_class_type));
16561 : 11507786 : if (memtmpl)
16562 : : {
16563 : 316091 : tree ci = current_template_constraints ();
16564 : 316091 : set_constraints (decl, ci);
16565 : : }
16566 : : }
16567 : : else
16568 : : {
16569 : 17596742 : if (concept_p)
16570 : : {
16571 : 12 : error_at (declspecs->locations[ds_concept],
16572 : : "non-static data member %qE declared %qs",
16573 : : unqualified_id, "concept");
16574 : 12 : concept_p = false;
16575 : 12 : constexpr_p = false;
16576 : : }
16577 : 17596730 : else if (constexpr_p)
16578 : : {
16579 : 9 : error_at (declspecs->locations[ds_constexpr],
16580 : : "non-static data member %qE declared %qs",
16581 : : unqualified_id, "constexpr");
16582 : 9 : constexpr_p = false;
16583 : : }
16584 : 17596742 : if (constinit_p)
16585 : : {
16586 : 6 : error_at (declspecs->locations[ds_constinit],
16587 : : "non-static data member %qE declared %qs",
16588 : : unqualified_id, "constinit");
16589 : 6 : constinit_p = false;
16590 : : }
16591 : 17596742 : if (consteval_p)
16592 : : {
16593 : 3 : error_at (declspecs->locations[ds_consteval],
16594 : : "non-static data member %qE declared %qs",
16595 : : unqualified_id, "consteval");
16596 : 3 : consteval_p = false;
16597 : : }
16598 : 17596742 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16599 : 17596742 : DECL_NONADDRESSABLE_P (decl) = bitfield;
16600 : 17596742 : if (bitfield && !unqualified_id)
16601 : 138806 : DECL_PADDING_P (decl) = 1;
16602 : :
16603 : 17596742 : if (storage_class == sc_mutable)
16604 : : {
16605 : 148943 : DECL_MUTABLE_P (decl) = 1;
16606 : 148943 : storage_class = sc_none;
16607 : : }
16608 : :
16609 : 17596742 : if (initialized)
16610 : : {
16611 : : /* An attempt is being made to initialize a non-static
16612 : : member. This is new in C++11. */
16613 : 1320900 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
16614 : :
16615 : : /* If this has been parsed with static storage class, but
16616 : : errors forced staticp to be cleared, ensure NSDMI is
16617 : : not present. */
16618 : 1320900 : if (declspecs->storage_class == sc_static)
16619 : 3 : DECL_INITIAL (decl) = error_mark_node;
16620 : : }
16621 : : }
16622 : :
16623 : 46701270 : bad_specifiers (decl, BSP_FIELD, virtualp,
16624 : : memfn_quals != TYPE_UNQUALIFIED,
16625 : : staticp ? false : inlinep, friendp,
16626 : : raises != NULL_TREE,
16627 : : declspecs->locations);
16628 : : }
16629 : : }
16630 : 139472176 : else if (FUNC_OR_METHOD_TYPE_P (type))
16631 : : {
16632 : 80613475 : tree original_name;
16633 : 80613475 : int publicp = 0;
16634 : :
16635 : 80613475 : if (!unqualified_id)
16636 : 0 : return error_mark_node;
16637 : :
16638 : 80613475 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16639 : : original_name = dname;
16640 : : else
16641 : 79730004 : original_name = unqualified_id;
16642 : : // FIXME:gcc_assert (original_name == dname);
16643 : :
16644 : 80613475 : if (storage_class == sc_auto)
16645 : 1 : error_at (declspecs->locations[ds_storage_class],
16646 : : "storage class %<auto%> invalid for function %qs", name);
16647 : 80613474 : else if (storage_class == sc_register)
16648 : 7 : error_at (declspecs->locations[ds_storage_class],
16649 : : "storage class %<register%> invalid for function %qs",
16650 : : name);
16651 : 80613467 : else if (thread_p)
16652 : : {
16653 : 12 : if (declspecs->gnu_thread_keyword_p)
16654 : 12 : error_at (declspecs->locations[ds_thread],
16655 : : "storage class %<__thread%> invalid for function %qs",
16656 : : name);
16657 : : else
16658 : 0 : error_at (declspecs->locations[ds_thread],
16659 : : "storage class %<thread_local%> invalid for "
16660 : : "function %qs", name);
16661 : : }
16662 : 80613455 : else if (constinit_p)
16663 : 3 : error_at (declspecs->locations[ds_constinit],
16664 : : "%<constinit%> specifier invalid for function %qs", name);
16665 : :
16666 : 80613475 : if (virt_specifiers)
16667 : 6 : error ("virt-specifiers in %qs not allowed outside a class "
16668 : : "definition", name);
16669 : : /* Function declaration not at top level.
16670 : : Storage classes other than `extern' are not allowed
16671 : : and `extern' makes no difference. */
16672 : 80613475 : if (! toplevel_bindings_p ()
16673 : 40233 : && (storage_class == sc_static
16674 : 40224 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
16675 : 80613487 : && pedantic)
16676 : : {
16677 : 9 : if (storage_class == sc_static)
16678 : 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
16679 : : "%<static%> specifier invalid for function %qs "
16680 : : "declared out of global scope", name);
16681 : : else
16682 : 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
16683 : : "%<inline%> specifier invalid for function %qs "
16684 : : "declared out of global scope", name);
16685 : : }
16686 : :
16687 : 80613475 : if (ctype == NULL_TREE)
16688 : : {
16689 : 71320906 : if (virtualp)
16690 : : {
16691 : 0 : error ("virtual non-class function %qs", name);
16692 : 0 : virtualp = 0;
16693 : : }
16694 : 71320906 : else if (sfk == sfk_constructor
16695 : 71320906 : || sfk == sfk_destructor)
16696 : : {
16697 : 3 : error (funcdef_flag
16698 : : ? G_("%qs defined in a non-class scope")
16699 : : : G_("%qs declared in a non-class scope"), name);
16700 : 3 : sfk = sfk_none;
16701 : : }
16702 : : }
16703 : 80613475 : if (consteval_p
16704 : 80681330 : && identifier_p (unqualified_id)
16705 : 80681330 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16706 : : {
16707 : 9 : error_at (declspecs->locations[ds_consteval],
16708 : : "%qD cannot be %qs", unqualified_id, "consteval");
16709 : 9 : consteval_p = false;
16710 : : }
16711 : :
16712 : : /* Record whether the function is public. */
16713 : 80613475 : publicp = (ctype != NULL_TREE
16714 : 80613475 : || storage_class != sc_static);
16715 : :
16716 : 161226950 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
16717 : : declspecs,
16718 : : reqs, virtualp, flags, memfn_quals, rqual, raises,
16719 : : 1, friendp,
16720 : : publicp,
16721 : 80613475 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16722 : 80613475 : | (8 * consteval_p),
16723 : : initialized == SD_DELETED,
16724 : : is_xobj_member_function, sfk,
16725 : : funcdef_flag,
16726 : : late_return_type_p,
16727 : : template_count, in_namespace, attrlist,
16728 : : id_loc);
16729 : 80613475 : if (decl == NULL_TREE)
16730 : 510 : return error_mark_node;
16731 : :
16732 : 80612965 : if (explicitp == 2)
16733 : 5680 : DECL_NONCONVERTING_P (decl) = 1;
16734 : 80612965 : if (staticp == 1)
16735 : : {
16736 : 113210 : int invalid_static = 0;
16737 : :
16738 : : /* Don't allow a static member function in a class, and forbid
16739 : : declaring main to be static. */
16740 : 113210 : if (TREE_CODE (type) == METHOD_TYPE)
16741 : : {
16742 : 0 : permerror (input_location, "cannot declare member function %qD to have "
16743 : : "static linkage", decl);
16744 : 0 : invalid_static = 1;
16745 : : }
16746 : 113210 : else if (current_function_decl)
16747 : : {
16748 : : /* 7.1.1: There can be no static function declarations within a
16749 : : block. */
16750 : 9 : error_at (declspecs->locations[ds_storage_class],
16751 : : "cannot declare static function inside another function");
16752 : 9 : invalid_static = 1;
16753 : : }
16754 : :
16755 : 9 : if (invalid_static)
16756 : : {
16757 : 80612965 : staticp = 0;
16758 : : storage_class = sc_none;
16759 : : }
16760 : : }
16761 : 80612965 : if (declspecs->explicit_specifier)
16762 : 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16763 : : }
16764 : : else
16765 : : {
16766 : : /* It's a variable. */
16767 : :
16768 : : /* An uninitialized decl with `extern' is a reference. */
16769 : 117304116 : decl = grokvardecl (type, dname, unqualified_id,
16770 : : declspecs,
16771 : : initialized,
16772 : : type_quals,
16773 : : inlinep,
16774 : : concept_p,
16775 : : template_count,
16776 : : ctype ? ctype : in_namespace,
16777 : : id_loc);
16778 : 58858701 : if (decl == NULL_TREE)
16779 : 52 : return error_mark_node;
16780 : :
16781 : 58858649 : bad_specifiers (decl, BSP_VAR, virtualp,
16782 : : memfn_quals != TYPE_UNQUALIFIED,
16783 : : inlinep, friendp, raises != NULL_TREE,
16784 : : declspecs->locations);
16785 : :
16786 : 58858649 : if (ctype)
16787 : : {
16788 : 413280 : DECL_CONTEXT (decl) = ctype;
16789 : 413280 : if (staticp == 1)
16790 : : {
16791 : 6 : permerror (declspecs->locations[ds_storage_class],
16792 : : "%<static%> may not be used when defining "
16793 : : "(as opposed to declaring) a static data member");
16794 : 6 : staticp = 0;
16795 : 6 : storage_class = sc_none;
16796 : : }
16797 : 413280 : if (storage_class == sc_register && TREE_STATIC (decl))
16798 : : {
16799 : 0 : error ("static member %qD declared %<register%>", decl);
16800 : 0 : storage_class = sc_none;
16801 : : }
16802 : 413280 : if (storage_class == sc_extern && pedantic)
16803 : : {
16804 : 0 : pedwarn (input_location, OPT_Wpedantic,
16805 : : "cannot explicitly declare member %q#D to have "
16806 : : "extern linkage", decl);
16807 : 0 : storage_class = sc_none;
16808 : : }
16809 : : }
16810 : 58445369 : else if (constexpr_p && DECL_EXTERNAL (decl))
16811 : : {
16812 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
16813 : : "declaration of %<constexpr%> variable %qD "
16814 : : "is not a definition", decl);
16815 : 12 : constexpr_p = false;
16816 : : }
16817 : 58858649 : if (consteval_p)
16818 : : {
16819 : 13 : error_at (DECL_SOURCE_LOCATION (decl),
16820 : : "a variable cannot be declared %<consteval%>");
16821 : 13 : consteval_p = false;
16822 : : }
16823 : :
16824 : 58858649 : if (inlinep)
16825 : 4354244 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16826 : 58858649 : if (innermost_code == cdk_decomp)
16827 : : {
16828 : 99367 : gcc_assert (declarator && declarator->kind == cdk_decomp);
16829 : 99367 : DECL_SOURCE_LOCATION (decl) = id_loc;
16830 : 99367 : DECL_ARTIFICIAL (decl) = 1;
16831 : 99367 : fit_decomposition_lang_decl (decl, NULL_TREE);
16832 : : }
16833 : : }
16834 : :
16835 : 546265608 : if (VAR_P (decl) && !initialized)
16836 : 6982047 : if (tree auto_node = type_uses_auto (type))
16837 : 98 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16838 : : {
16839 : 18 : location_t loc = declspecs->locations[ds_type_spec];
16840 : 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
16841 : 18 : TREE_TYPE (decl) = error_mark_node;
16842 : : }
16843 : :
16844 : 546265608 : if (storage_class == sc_extern && initialized && !funcdef_flag)
16845 : : {
16846 : 261 : if (toplevel_bindings_p ())
16847 : : {
16848 : : /* It's common practice (and completely valid) to have a const
16849 : : be initialized and declared extern. */
16850 : 237 : if (!(type_quals & TYPE_QUAL_CONST))
16851 : 20 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
16852 : : "%qs initialized and declared %<extern%>", name);
16853 : : }
16854 : : else
16855 : : {
16856 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
16857 : : "%qs has both %<extern%> and initializer", name);
16858 : 24 : return error_mark_node;
16859 : : }
16860 : : }
16861 : :
16862 : : /* Record `register' declaration for warnings on &
16863 : : and in case doing stupid register allocation. */
16864 : :
16865 : 546265584 : if (storage_class == sc_register)
16866 : : {
16867 : 2069 : DECL_REGISTER (decl) = 1;
16868 : : /* Warn about register storage specifiers on PARM_DECLs. */
16869 : 2069 : if (TREE_CODE (decl) == PARM_DECL)
16870 : : {
16871 : 35 : if (cxx_dialect >= cxx17)
16872 : 24 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16873 : : "ISO C++17 does not allow %<register%> storage "
16874 : : "class specifier");
16875 : : else
16876 : 11 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16877 : : "%<register%> storage class specifier used");
16878 : : }
16879 : : }
16880 : 546263515 : else if (storage_class == sc_extern)
16881 : 32324916 : DECL_THIS_EXTERN (decl) = 1;
16882 : 513938599 : else if (storage_class == sc_static)
16883 : 21084264 : DECL_THIS_STATIC (decl) = 1;
16884 : :
16885 : 546265584 : if (VAR_P (decl))
16886 : : {
16887 : : /* Set constexpr flag on vars (functions got it in grokfndecl). */
16888 : 70366411 : if (constexpr_p)
16889 : 15301064 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
16890 : : /* And the constinit flag (which only applies to variables). */
16891 : 55065347 : else if (constinit_p)
16892 : 436 : DECL_DECLARED_CONSTINIT_P (decl) = true;
16893 : : }
16894 : 475899173 : else if (TREE_CODE (decl) == FUNCTION_DECL)
16895 : : {
16896 : : /* If we saw a return type, record its location. */
16897 : 174233693 : location_t loc = declspecs->locations[ds_type_spec];
16898 : 174233693 : if (loc == UNKNOWN_LOCATION)
16899 : : /* Build DECL_RESULT in start_preparsed_function. */;
16900 : 143660044 : else if (!DECL_RESULT (decl))
16901 : : {
16902 : 143622606 : tree restype = TREE_TYPE (TREE_TYPE (decl));
16903 : 143622606 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
16904 : 143622606 : DECL_ARTIFICIAL (resdecl) = 1;
16905 : 143622606 : DECL_IGNORED_P (resdecl) = 1;
16906 : 143622606 : DECL_RESULT (decl) = resdecl;
16907 : : }
16908 : 37438 : else if (funcdef_flag)
16909 : 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
16910 : : }
16911 : :
16912 : : /* Record constancy and volatility on the DECL itself . There's
16913 : : no need to do this when processing a template; we'll do this
16914 : : for the instantiated declaration based on the type of DECL. */
16915 : 546265584 : if (!processing_template_decl
16916 : : /* Don't do it for instantiated variable templates either,
16917 : : cp_apply_type_quals_to_decl should have been called on it
16918 : : already and might have been overridden in cp_finish_decl
16919 : : if initializer needs runtime initialization. */
16920 : 546265584 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
16921 : 186935793 : cp_apply_type_quals_to_decl (type_quals, decl);
16922 : :
16923 : 546265584 : return decl;
16924 : : }
16925 : 953321578 : }
16926 : :
16927 : : /* Subroutine of start_function. Ensure that each of the parameter
16928 : : types (as listed in PARMS) is complete, as is required for a
16929 : : function definition. */
16930 : :
16931 : : static void
16932 : 147823538 : require_complete_types_for_parms (tree parms)
16933 : : {
16934 : 437536669 : for (; parms; parms = DECL_CHAIN (parms))
16935 : : {
16936 : 289713131 : if (dependent_type_p (TREE_TYPE (parms)))
16937 : 166355909 : continue;
16938 : 123357222 : if (!VOID_TYPE_P (TREE_TYPE (parms))
16939 : 123357222 : && complete_type_or_else (TREE_TYPE (parms), parms))
16940 : : {
16941 : 123357141 : relayout_decl (parms);
16942 : 123357141 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
16943 : :
16944 : 123357141 : abstract_virtuals_error (parms, TREE_TYPE (parms));
16945 : 123357141 : maybe_warn_parm_abi (TREE_TYPE (parms),
16946 : 123357141 : DECL_SOURCE_LOCATION (parms));
16947 : : }
16948 : : else
16949 : : /* grokparms or complete_type_or_else will have already issued
16950 : : an error. */
16951 : 81 : TREE_TYPE (parms) = error_mark_node;
16952 : : }
16953 : 147823538 : }
16954 : :
16955 : : /* Returns nonzero if T is a local variable. */
16956 : :
16957 : : int
16958 : 160554241 : local_variable_p (const_tree t)
16959 : : {
16960 : 160554241 : if ((VAR_P (t)
16961 : 70094375 : && (DECL_LOCAL_DECL_P (t)
16962 : 70094375 : || !DECL_CONTEXT (t)
16963 : 70051300 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
16964 : 164414284 : || (TREE_CODE (t) == PARM_DECL))
16965 : 143994169 : return 1;
16966 : :
16967 : : return 0;
16968 : : }
16969 : :
16970 : : /* Like local_variable_p, but suitable for use as a tree-walking
16971 : : function. */
16972 : :
16973 : : static tree
16974 : 5162819 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
16975 : : void * /*data*/)
16976 : : {
16977 : 5162819 : if (unevaluated_p (TREE_CODE (*tp)))
16978 : : {
16979 : : /* DR 2082 permits local variables in unevaluated contexts
16980 : : within a default argument. */
16981 : 15 : *walk_subtrees = 0;
16982 : 15 : return NULL_TREE;
16983 : : }
16984 : :
16985 : 5162804 : if (local_variable_p (*tp)
16986 : 5162804 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
16987 : : return *tp;
16988 : 5162804 : else if (TYPE_P (*tp))
16989 : 30 : *walk_subtrees = 0;
16990 : :
16991 : : return NULL_TREE;
16992 : : }
16993 : :
16994 : : /* Check that ARG, which is a default-argument expression for a
16995 : : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
16996 : : something goes wrong. DECL may also be a _TYPE node, rather than a
16997 : : DECL, if there is no DECL available. */
16998 : :
16999 : : tree
17000 : 2648145 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
17001 : : {
17002 : 2648145 : tree var;
17003 : 2648145 : tree decl_type;
17004 : :
17005 : 2648145 : if (TREE_CODE (arg) == DEFERRED_PARSE)
17006 : : /* We get a DEFERRED_PARSE when looking at an in-class declaration
17007 : : with a default argument. Ignore the argument for now; we'll
17008 : : deal with it after the class is complete. */
17009 : : return arg;
17010 : :
17011 : 1914151 : if (TYPE_P (decl))
17012 : : {
17013 : : decl_type = decl;
17014 : : decl = NULL_TREE;
17015 : : }
17016 : : else
17017 : 577436 : decl_type = TREE_TYPE (decl);
17018 : :
17019 : 1914151 : if (arg == error_mark_node
17020 : 1914033 : || decl == error_mark_node
17021 : 1914024 : || TREE_TYPE (arg) == error_mark_node
17022 : 3828175 : || decl_type == error_mark_node)
17023 : : /* Something already went wrong. There's no need to check
17024 : : further. */
17025 : : return error_mark_node;
17026 : :
17027 : : /* [dcl.fct.default]
17028 : :
17029 : : A default argument expression is implicitly converted to the
17030 : : parameter type. */
17031 : 1914024 : ++cp_unevaluated_operand;
17032 : : /* Avoid digest_init clobbering the initializer. */
17033 : 1914024 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
17034 : 1914024 : perform_implicit_conversion_flags (decl_type, carg, complain,
17035 : : LOOKUP_IMPLICIT);
17036 : 1914024 : --cp_unevaluated_operand;
17037 : :
17038 : : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
17039 : : the call sites. */
17040 : 1419515 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17041 : 494524 : && null_ptr_cst_p (arg)
17042 : : /* Don't lose side-effects as in PR90473. */
17043 : 2298837 : && !TREE_SIDE_EFFECTS (arg))
17044 : 384810 : return nullptr_node;
17045 : :
17046 : : /* [dcl.fct.default]
17047 : :
17048 : : Local variables shall not be used in default argument
17049 : : expressions.
17050 : :
17051 : : The keyword `this' shall not be used in a default argument of a
17052 : : member function. */
17053 : 1529214 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17054 : 1529214 : if (var)
17055 : : {
17056 : 0 : if (complain & tf_warning_or_error)
17057 : : {
17058 : 0 : if (DECL_NAME (var) == this_identifier)
17059 : 0 : permerror (input_location, "default argument %qE uses %qD",
17060 : : arg, var);
17061 : : else
17062 : 0 : error ("default argument %qE uses local variable %qD", arg, var);
17063 : : }
17064 : 0 : return error_mark_node;
17065 : : }
17066 : :
17067 : : /* All is well. */
17068 : 1529214 : return arg;
17069 : : }
17070 : :
17071 : : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17072 : :
17073 : : static tree
17074 : 270389803 : type_is_deprecated (tree type)
17075 : : {
17076 : 398892095 : enum tree_code code;
17077 : 398892095 : if (TREE_DEPRECATED (type))
17078 : : return type;
17079 : 398849502 : if (TYPE_NAME (type))
17080 : : {
17081 : 266034059 : if (TREE_DEPRECATED (TYPE_NAME (type)))
17082 : : return type;
17083 : : else
17084 : : {
17085 : 266034053 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17086 : 266034053 : return NULL_TREE;
17087 : : }
17088 : : }
17089 : :
17090 : : /* Do warn about using typedefs to a deprecated class. */
17091 : 132815443 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17092 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17093 : :
17094 : 132815443 : code = TREE_CODE (type);
17095 : :
17096 : 132815443 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17097 : 132815443 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17098 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
17099 : 128385814 : return type_is_deprecated (TREE_TYPE (type));
17100 : :
17101 : 116478 : if (TYPE_PTRMEMFUNC_P (type))
17102 : 116478 : return type_is_deprecated
17103 : 116478 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17104 : :
17105 : : return NULL_TREE;
17106 : : }
17107 : :
17108 : : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17109 : :
17110 : : static tree
17111 : 271301475 : type_is_unavailable (tree type)
17112 : : {
17113 : 400372310 : enum tree_code code;
17114 : 400372310 : if (TREE_UNAVAILABLE (type))
17115 : : return type;
17116 : 400372304 : if (TYPE_NAME (type))
17117 : : {
17118 : 266966601 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17119 : : return type;
17120 : : else
17121 : : {
17122 : 266966595 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17123 : 266966595 : return NULL_TREE;
17124 : : }
17125 : : }
17126 : :
17127 : : /* Do warn about using typedefs to a deprecated class. */
17128 : 133405703 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17129 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17130 : :
17131 : 133405703 : code = TREE_CODE (type);
17132 : :
17133 : 133405703 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17134 : 133405703 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17135 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
17136 : 128844405 : return type_is_unavailable (TREE_TYPE (type));
17137 : :
17138 : 226430 : if (TYPE_PTRMEMFUNC_P (type))
17139 : 226430 : return type_is_unavailable
17140 : 226430 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17141 : :
17142 : : return NULL_TREE;
17143 : : }
17144 : :
17145 : : /* Decode the list of parameter types for a function type.
17146 : : Given the list of things declared inside the parens,
17147 : : return a list of types.
17148 : :
17149 : : If this parameter does not end with an ellipsis, we append
17150 : : void_list_node.
17151 : :
17152 : : *PARMS is set to the chain of PARM_DECLs created. */
17153 : :
17154 : : tree
17155 : 182448588 : grokparms (tree parmlist, tree *parms)
17156 : : {
17157 : 182448588 : tree result = NULL_TREE;
17158 : 182448588 : tree decls = NULL_TREE;
17159 : 182448588 : tree parm;
17160 : 182448588 : int any_error = 0;
17161 : :
17162 : 454381105 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17163 : : {
17164 : 452855708 : tree type = NULL_TREE;
17165 : 452855708 : tree init = TREE_PURPOSE (parm);
17166 : 452855708 : tree decl = TREE_VALUE (parm);
17167 : :
17168 : 452855708 : if (parm == void_list_node || parm == explicit_void_list_node)
17169 : : break;
17170 : :
17171 : 271932529 : if (! decl || TREE_TYPE (decl) == error_mark_node)
17172 : : {
17173 : 186 : any_error = 1;
17174 : 186 : continue;
17175 : : }
17176 : :
17177 : 271932343 : type = TREE_TYPE (decl);
17178 : 271932343 : if (VOID_TYPE_P (type))
17179 : : {
17180 : 78 : if (same_type_p (type, void_type_node)
17181 : 69 : && !init
17182 : 57 : && !DECL_NAME (decl) && !result
17183 : 102 : && TREE_CHAIN (parm) == void_list_node)
17184 : : /* DR 577: A parameter list consisting of a single
17185 : : unnamed parameter of non-dependent type 'void'. */
17186 : : break;
17187 : 66 : else if (cv_qualified_p (type))
17188 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17189 : : "invalid use of cv-qualified type %qT in "
17190 : : "parameter declaration", type);
17191 : : else
17192 : 57 : error_at (DECL_SOURCE_LOCATION (decl),
17193 : : "invalid use of type %<void%> in parameter "
17194 : : "declaration");
17195 : : /* It's not a good idea to actually create parameters of
17196 : : type `void'; other parts of the compiler assume that a
17197 : : void type terminates the parameter list. */
17198 : 66 : type = error_mark_node;
17199 : 66 : TREE_TYPE (decl) = error_mark_node;
17200 : : }
17201 : :
17202 : 271932331 : if (type != error_mark_node)
17203 : : {
17204 : 271932265 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17205 : : {
17206 : 271301475 : tree unavailtype = type_is_unavailable (type);
17207 : 271301475 : if (unavailtype)
17208 : 12 : cp_handle_deprecated_or_unavailable (unavailtype);
17209 : : }
17210 : 271932265 : if (deprecated_state != DEPRECATED_SUPPRESS
17211 : 271932265 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17212 : : {
17213 : 270389803 : tree deptype = type_is_deprecated (type);
17214 : 270389803 : if (deptype)
17215 : 42599 : cp_handle_deprecated_or_unavailable (deptype);
17216 : : }
17217 : :
17218 : : /* [dcl.fct] "A parameter with volatile-qualified type is
17219 : : deprecated." */
17220 : 271932265 : if (CP_TYPE_VOLATILE_P (type))
17221 : 504 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17222 : : "%<volatile%>-qualified parameter is "
17223 : : "deprecated");
17224 : :
17225 : : /* Top-level qualifiers on the parameters are
17226 : : ignored for function types. */
17227 : 271932265 : type = cp_build_qualified_type (type, 0);
17228 : 271932265 : if (TREE_CODE (type) == METHOD_TYPE)
17229 : : {
17230 : 0 : error ("parameter %qD invalidly declared method type", decl);
17231 : 0 : type = build_pointer_type (type);
17232 : 0 : TREE_TYPE (decl) = type;
17233 : : }
17234 : 271932265 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17235 : : {
17236 : : /* Before C++17 DR 393:
17237 : : [dcl.fct]/6, parameter types cannot contain pointers
17238 : : (references) to arrays of unknown bound. */
17239 : 1627688 : tree t = TREE_TYPE (type);
17240 : 1627688 : int ptr = TYPE_PTR_P (type);
17241 : :
17242 : 1785710 : while (1)
17243 : : {
17244 : 1706699 : if (TYPE_PTR_P (t))
17245 : : ptr = 1;
17246 : 1630818 : else if (TREE_CODE (t) != ARRAY_TYPE)
17247 : : break;
17248 : 3140 : else if (!TYPE_DOMAIN (t))
17249 : : break;
17250 : 79011 : t = TREE_TYPE (t);
17251 : : }
17252 : 1627688 : if (TREE_CODE (t) == ARRAY_TYPE)
17253 : 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17254 : : ptr
17255 : : ? G_("parameter %qD includes pointer to array of "
17256 : : "unknown bound %qT")
17257 : : : G_("parameter %qD includes reference to array of "
17258 : : "unknown bound %qT"),
17259 : : decl, t);
17260 : : }
17261 : :
17262 : 271932265 : if (init && !processing_template_decl)
17263 : 1311421 : init = check_default_argument (decl, init, tf_warning_or_error);
17264 : : }
17265 : :
17266 : 271932331 : DECL_CHAIN (decl) = decls;
17267 : 271932331 : decls = decl;
17268 : 271932331 : result = tree_cons (init, type, result);
17269 : : }
17270 : 182448588 : decls = nreverse (decls);
17271 : 182448588 : result = nreverse (result);
17272 : 182448588 : if (parm)
17273 : 180923191 : result = chainon (result, void_list_node);
17274 : 182448588 : *parms = decls;
17275 : 182448588 : if (any_error)
17276 : 172 : result = NULL_TREE;
17277 : :
17278 : 172 : if (any_error)
17279 : : /* We had parm errors, recover by giving the function (...) type. */
17280 : 172 : result = NULL_TREE;
17281 : :
17282 : 182448588 : return result;
17283 : : }
17284 : :
17285 : :
17286 : : /* D is a constructor or overloaded `operator='.
17287 : :
17288 : : Let T be the class in which D is declared. Then, this function
17289 : : returns:
17290 : :
17291 : : -1 if D's is an ill-formed constructor or copy assignment operator
17292 : : whose first parameter is of type `T'.
17293 : : 0 if D is not a copy constructor or copy assignment
17294 : : operator.
17295 : : 1 if D is a copy constructor or copy assignment operator whose
17296 : : first parameter is a reference to non-const qualified T.
17297 : : 2 if D is a copy constructor or copy assignment operator whose
17298 : : first parameter is a reference to const qualified T.
17299 : :
17300 : : This function can be used as a predicate. Positive values indicate
17301 : : a copy constructor and nonzero values indicate a copy assignment
17302 : : operator. */
17303 : :
17304 : : int
17305 : 302424282 : copy_fn_p (const_tree d)
17306 : : {
17307 : 302424282 : tree args;
17308 : 302424282 : tree arg_type;
17309 : 302424282 : int result = 1;
17310 : :
17311 : 302424282 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17312 : :
17313 : 302424282 : if (TREE_CODE (d) == TEMPLATE_DECL
17314 : 302424282 : || (DECL_TEMPLATE_INFO (d)
17315 : 144522482 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17316 : : /* Instantiations of template member functions are never copy
17317 : : functions. Note that member functions of templated classes are
17318 : : represented as template functions internally, and we must
17319 : : accept those as copy functions. */
17320 : : return 0;
17321 : :
17322 : 254717292 : if (!DECL_CONSTRUCTOR_P (d)
17323 : 254717292 : && DECL_NAME (d) != assign_op_identifier)
17324 : : return 0;
17325 : :
17326 : 253938249 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17327 : : {
17328 : 154 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17329 : 154 : if (!TYPE_REF_P (object_param)
17330 : 148 : || TYPE_REF_IS_RVALUE (object_param)
17331 : : /* Reject unrelated object parameters. */
17332 : 136 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17333 : 290 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17334 : 24 : return 0;
17335 : 130 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17336 : : }
17337 : : else
17338 : 253938095 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17339 : 253938225 : if (!args)
17340 : : return 0;
17341 : :
17342 : 253930700 : arg_type = TREE_VALUE (args);
17343 : 253930700 : if (arg_type == error_mark_node)
17344 : : return 0;
17345 : :
17346 : 253930665 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17347 : : {
17348 : : /* Pass by value copy assignment operator. */
17349 : : result = -1;
17350 : : }
17351 : 253928277 : else if (TYPE_REF_P (arg_type)
17352 : 156741543 : && !TYPE_REF_IS_RVALUE (arg_type)
17353 : 355343272 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17354 : : {
17355 : 80390741 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17356 : 80298670 : result = 2;
17357 : : }
17358 : : else
17359 : : return 0;
17360 : :
17361 : 80393129 : args = TREE_CHAIN (args);
17362 : :
17363 : 83072139 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17364 : : /* There are more non-optional args. */
17365 : : return 0;
17366 : :
17367 : : return result;
17368 : : }
17369 : :
17370 : : /* D is a constructor or overloaded `operator='.
17371 : :
17372 : : Let T be the class in which D is declared. Then, this function
17373 : : returns true when D is a move constructor or move assignment
17374 : : operator, false otherwise. */
17375 : :
17376 : : bool
17377 : 125670532 : move_fn_p (const_tree d)
17378 : : {
17379 : 125670532 : if (cxx_dialect == cxx98)
17380 : : /* There are no move constructors if we are in C++98 mode. */
17381 : : return false;
17382 : :
17383 : 125604431 : if (TREE_CODE (d) == TEMPLATE_DECL
17384 : 125604431 : || (DECL_TEMPLATE_INFO (d)
17385 : 56785778 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17386 : : /* Instantiations of template member functions are never move
17387 : : functions. Note that member functions of templated classes are
17388 : : represented as template functions internally, and we must
17389 : : accept those as move functions. */
17390 : : return 0;
17391 : :
17392 : 103516879 : return move_signature_fn_p (d);
17393 : : }
17394 : :
17395 : : /* D is a constructor or overloaded `operator='.
17396 : :
17397 : : Then, this function returns true when D has the same signature as a move
17398 : : constructor or move assignment operator (because either it is such a
17399 : : ctor/op= or it is a template specialization with the same signature),
17400 : : false otherwise. */
17401 : :
17402 : : bool
17403 : 103516879 : move_signature_fn_p (const_tree d)
17404 : : {
17405 : 103516879 : tree args;
17406 : 103516879 : tree arg_type;
17407 : 103516879 : bool result = false;
17408 : :
17409 : 103516879 : if (!DECL_CONSTRUCTOR_P (d)
17410 : 103516879 : && DECL_NAME (d) != assign_op_identifier)
17411 : : return 0;
17412 : :
17413 : 103516815 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17414 : : {
17415 : 88 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17416 : 88 : if (!TYPE_REF_P (object_param)
17417 : 84 : || TYPE_REF_IS_RVALUE (object_param)
17418 : : /* Reject unrelated object parameters. */
17419 : 76 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17420 : 164 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17421 : 16 : return 0;
17422 : 72 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17423 : : }
17424 : : else
17425 : 103516727 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17426 : 103516799 : if (!args)
17427 : : return 0;
17428 : :
17429 : 103514022 : arg_type = TREE_VALUE (args);
17430 : 103514022 : if (arg_type == error_mark_node)
17431 : : return 0;
17432 : :
17433 : 103514014 : if (TYPE_REF_P (arg_type)
17434 : 50237701 : && TYPE_REF_IS_RVALUE (arg_type)
17435 : 142632274 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17436 : : DECL_CONTEXT (d)))
17437 : : result = true;
17438 : :
17439 : 103514014 : args = TREE_CHAIN (args);
17440 : :
17441 : 115183348 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17442 : : /* There are more non-optional args. */
17443 : : return false;
17444 : :
17445 : : return result;
17446 : : }
17447 : :
17448 : : /* Remember any special properties of member function DECL. */
17449 : :
17450 : : void
17451 : 303633866 : grok_special_member_properties (tree decl)
17452 : : {
17453 : 303633866 : tree class_type;
17454 : :
17455 : 303633866 : if (TREE_CODE (decl) == USING_DECL
17456 : 303633866 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17457 : : return;
17458 : :
17459 : 285000333 : class_type = DECL_CONTEXT (decl);
17460 : 285000333 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17461 : : {
17462 : 125595003 : int ctor = copy_fn_p (decl);
17463 : :
17464 : 125595003 : if (!DECL_ARTIFICIAL (decl))
17465 : 93783225 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17466 : :
17467 : 125595003 : if (ctor > 0)
17468 : : {
17469 : : /* [class.copy]
17470 : :
17471 : : A non-template constructor for class X is a copy
17472 : : constructor if its first parameter is of type X&, const
17473 : : X&, volatile X& or const volatile X&, and either there
17474 : : are no other parameters or else all other parameters have
17475 : : default arguments. */
17476 : 24612227 : TYPE_HAS_COPY_CTOR (class_type) = 1;
17477 : 24612227 : if (ctor > 1)
17478 : 24579695 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17479 : : }
17480 : :
17481 : 125595003 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17482 : 26064790 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17483 : :
17484 : 125595003 : if (is_list_ctor (decl))
17485 : 1480415 : TYPE_HAS_LIST_CTOR (class_type) = 1;
17486 : :
17487 : 125595003 : if (maybe_constexpr_fn (decl)
17488 : 125595003 : && !ctor && !move_fn_p (decl))
17489 : 32906130 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17490 : : }
17491 : 159405330 : else if (DECL_NAME (decl) == assign_op_identifier)
17492 : : {
17493 : : /* [class.copy]
17494 : :
17495 : : A non-template assignment operator for class X is a copy
17496 : : assignment operator if its parameter is of type X, X&, const
17497 : : X&, volatile X& or const volatile X&. */
17498 : :
17499 : 13744883 : int assop = copy_fn_p (decl);
17500 : :
17501 : 13744883 : if (assop)
17502 : : {
17503 : 6873441 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17504 : 6873441 : if (assop != 1)
17505 : 6843342 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17506 : : }
17507 : : }
17508 : 145660447 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17509 : 2022916 : TYPE_HAS_CONVERSION (class_type) = true;
17510 : :
17511 : : /* Destructors are handled in check_methods. */
17512 : : }
17513 : :
17514 : : /* Check a constructor DECL has the correct form. Complains
17515 : : if the class has a constructor of the form X(X). */
17516 : :
17517 : : bool
17518 : 43817425 : grok_ctor_properties (const_tree ctype, const_tree decl)
17519 : : {
17520 : 43817425 : int ctor_parm = copy_fn_p (decl);
17521 : :
17522 : 43817425 : if (ctor_parm < 0)
17523 : : {
17524 : : /* [class.copy]
17525 : :
17526 : : A declaration of a constructor for a class X is ill-formed if
17527 : : its first parameter is of type (optionally cv-qualified) X
17528 : : and either there are no other parameters or else all other
17529 : : parameters have default arguments.
17530 : :
17531 : : We *don't* complain about member template instantiations that
17532 : : have this form, though; they can occur as we try to decide
17533 : : what constructor to use during overload resolution. Since
17534 : : overload resolution will never prefer such a constructor to
17535 : : the non-template copy constructor (which is either explicitly
17536 : : or implicitly defined), there's no need to worry about their
17537 : : existence. Theoretically, they should never even be
17538 : : instantiated, but that's hard to forestall. */
17539 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17540 : : "invalid constructor; you probably meant %<%T (const %T&)%>",
17541 : : ctype, ctype);
17542 : 9 : return false;
17543 : : }
17544 : :
17545 : : return true;
17546 : : }
17547 : :
17548 : : /* DECL is a declaration for an overloaded or conversion operator. If
17549 : : COMPLAIN is true, errors are issued for invalid declarations. */
17550 : :
17551 : : bool
17552 : 53293857 : grok_op_properties (tree decl, bool complain)
17553 : : {
17554 : 53293857 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17555 : 53293857 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17556 : 53293857 : tree name = DECL_NAME (decl);
17557 : 53293857 : location_t loc = DECL_SOURCE_LOCATION (decl);
17558 : :
17559 : 53293857 : tree class_type = DECL_CONTEXT (decl);
17560 : 53293857 : if (class_type && !CLASS_TYPE_P (class_type))
17561 : : class_type = NULL_TREE;
17562 : :
17563 : 53293857 : tree_code operator_code;
17564 : 53293857 : unsigned op_flags;
17565 : 53293857 : if (IDENTIFIER_CONV_OP_P (name))
17566 : : {
17567 : : /* Conversion operators are TYPE_EXPR for the purposes of this
17568 : : function. */
17569 : : operator_code = TYPE_EXPR;
17570 : : op_flags = OVL_OP_FLAG_UNARY;
17571 : : }
17572 : : else
17573 : : {
17574 : 51621168 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17575 : :
17576 : 51621168 : operator_code = ovl_op->tree_code;
17577 : 51621168 : op_flags = ovl_op->flags;
17578 : 51621168 : gcc_checking_assert (operator_code != ERROR_MARK);
17579 : 51621168 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17580 : : }
17581 : :
17582 : 51621168 : if (op_flags & OVL_OP_FLAG_ALLOC)
17583 : : {
17584 : : /* operator new and operator delete are quite special. */
17585 : 339527 : if (class_type)
17586 : 14172 : switch (op_flags)
17587 : : {
17588 : 10075 : case OVL_OP_FLAG_ALLOC:
17589 : 10075 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17590 : 10075 : break;
17591 : :
17592 : 3830 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17593 : 3830 : TYPE_GETS_DELETE (class_type) |= 1;
17594 : 3830 : break;
17595 : :
17596 : 130 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17597 : 130 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17598 : 130 : break;
17599 : :
17600 : 137 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17601 : 137 : TYPE_GETS_DELETE (class_type) |= 2;
17602 : 137 : break;
17603 : :
17604 : 0 : default:
17605 : 0 : gcc_unreachable ();
17606 : : }
17607 : :
17608 : : /* [basic.std.dynamic.allocation]/1:
17609 : :
17610 : : A program is ill-formed if an allocation function is declared
17611 : : in a namespace scope other than global scope or declared
17612 : : static in global scope.
17613 : :
17614 : : The same also holds true for deallocation functions. */
17615 : 339527 : if (DECL_NAMESPACE_SCOPE_P (decl))
17616 : : {
17617 : 325355 : if (CP_DECL_CONTEXT (decl) != global_namespace)
17618 : : {
17619 : 6 : error_at (loc, "%qD may not be declared within a namespace",
17620 : : decl);
17621 : 6 : return false;
17622 : : }
17623 : :
17624 : 325349 : if (!TREE_PUBLIC (decl))
17625 : : {
17626 : 6 : error_at (loc, "%qD may not be declared as static", decl);
17627 : 6 : return false;
17628 : : }
17629 : : }
17630 : :
17631 : : /* Check for replaceable global new/delete operators with
17632 : : const std::nothrow_t & last argument, other replaceable global
17633 : : new/delete operators are marked in cxx_init_decl_processing. */
17634 : 339515 : if (CP_DECL_CONTEXT (decl) == global_namespace)
17635 : : {
17636 : 325343 : tree args = argtypes;
17637 : 325343 : if (args
17638 : 325331 : && args != void_list_node
17639 : 975987 : && same_type_p (TREE_VALUE (args),
17640 : : (op_flags & OVL_OP_FLAG_DELETE)
17641 : : ? ptr_type_node : size_type_node))
17642 : : {
17643 : 325301 : args = TREE_CHAIN (args);
17644 : 325301 : if (aligned_allocation_fn_p (decl))
17645 : 104876 : args = TREE_CHAIN (args);
17646 : 325301 : if (args
17647 : 325295 : && args != void_list_node
17648 : 542415 : && TREE_CHAIN (args) == void_list_node)
17649 : : {
17650 : 190851 : tree t = TREE_VALUE (args);
17651 : 190851 : if (TYPE_REF_P (t)
17652 : 108282 : && !TYPE_REF_IS_RVALUE (t)
17653 : 108282 : && (t = TREE_TYPE (t))
17654 : 108282 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
17655 : 298685 : && is_std_class (t, "nothrow_t"))
17656 : 107834 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
17657 : : }
17658 : : }
17659 : : }
17660 : :
17661 : 339515 : if (op_flags & OVL_OP_FLAG_DELETE)
17662 : : {
17663 : 192269 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
17664 : 192269 : coerce_delete_type (decl, loc);
17665 : : }
17666 : : else
17667 : : {
17668 : 147246 : DECL_SET_IS_OPERATOR_NEW (decl, true);
17669 : 147246 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
17670 : : }
17671 : :
17672 : 339515 : return true;
17673 : : }
17674 : :
17675 : : /* An operator function must either be a non-static member function
17676 : : or have at least one parameter of a class, a reference to a class,
17677 : : an enumeration, or a reference to an enumeration. 13.4.0.6 */
17678 : 52954330 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17679 : : {
17680 : 15258464 : if (operator_code == TYPE_EXPR
17681 : 15258464 : || operator_code == COMPONENT_REF
17682 : 15258443 : || operator_code == NOP_EXPR)
17683 : : {
17684 : 33 : error_at (loc, "%qD must be a non-static member function", decl);
17685 : 33 : return false;
17686 : : }
17687 : :
17688 : 15258431 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
17689 : : {
17690 : 52576 : if (! DECL_STATIC_FUNCTION_P (decl))
17691 : : {
17692 : 12 : error_at (loc, "%qD must be a member function", decl);
17693 : 12 : return false;
17694 : : }
17695 : 52564 : if (cxx_dialect < cxx23
17696 : : /* For lambdas we diagnose static lambda specifier elsewhere. */
17697 : 52376 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
17698 : : /* For instantiations, we have diagnosed this already. */
17699 : 78738 : && ! DECL_USE_TEMPLATE (decl))
17700 : 26168 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
17701 : : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
17702 : : decl);
17703 : 52564 : if (operator_code == ARRAY_REF)
17704 : : /* static operator[] should have exactly one argument
17705 : : for C++20 and earlier, so that it isn't multidimensional. */
17706 : : op_flags = OVL_OP_FLAG_UNARY;
17707 : : }
17708 : 15205855 : else if (DECL_STATIC_FUNCTION_P (decl))
17709 : : {
17710 : 9 : error_at (loc, "%qD must be either a non-static member "
17711 : : "function or a non-member function", decl);
17712 : 9 : return false;
17713 : : }
17714 : : else
17715 : 502327 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
17716 : : {
17717 : 15708173 : if (!arg || arg == void_list_node)
17718 : : {
17719 : 18 : if (complain)
17720 : 15 : error_at (loc, "%qD must have an argument of class or "
17721 : : "enumerated type", decl);
17722 : 18 : return false;
17723 : : }
17724 : :
17725 : 15708155 : tree type = non_reference (TREE_VALUE (arg));
17726 : 15708155 : if (type == error_mark_node)
17727 : : return false;
17728 : :
17729 : : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
17730 : : because these checks are performed even on template
17731 : : functions. */
17732 : 15708152 : if (MAYBE_CLASS_TYPE_P (type)
17733 : 1480446 : || TREE_CODE (type) == ENUMERAL_TYPE)
17734 : : break;
17735 : 502327 : }
17736 : : }
17737 : :
17738 : 52954217 : if (operator_code == CALL_EXPR)
17739 : : /* There are no further restrictions on the arguments to an overloaded
17740 : : "operator ()". */
17741 : : return true;
17742 : :
17743 : : /* C++23 allows an arbitrary number of parameters and default arguments for
17744 : : operator[], and none of the other checks below apply. */
17745 : 45958765 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
17746 : : return true;
17747 : :
17748 : 45561307 : if (operator_code == COND_EXPR)
17749 : : {
17750 : : /* 13.4.0.3 */
17751 : 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
17752 : 6 : return false;
17753 : : }
17754 : :
17755 : : /* Count the number of arguments and check for ellipsis. */
17756 : : int arity = 0;
17757 : 130473639 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
17758 : : {
17759 : 84912353 : if (!arg)
17760 : : {
17761 : 15 : error_at (loc, "%qD must not have variable number of arguments",
17762 : : decl);
17763 : 15 : return false;
17764 : : }
17765 : 84912338 : ++arity;
17766 : : }
17767 : : /* FIXME: We need tests for these errors with xobj member functions. */
17768 : : /* Verify correct number of arguments. */
17769 : 45561286 : switch (op_flags)
17770 : : {
17771 : 8069544 : case OVL_OP_FLAG_AMBIARY:
17772 : 8069544 : if (arity == 1)
17773 : : {
17774 : : /* We have a unary instance of an ambi-ary op. Remap to the
17775 : : unary one. */
17776 : 3573363 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
17777 : 3573363 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
17778 : 3573363 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
17779 : 3573363 : operator_code = ovl_op->tree_code;
17780 : 3573363 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17781 : : }
17782 : 4496181 : else if (arity != 2)
17783 : : {
17784 : : /* This was an ambiguous operator but is invalid. */
17785 : 3 : error_at (loc,
17786 : : methodp
17787 : : ? G_("%qD must have either zero or one argument")
17788 : : : G_("%qD must have either one or two arguments"), decl);
17789 : 3 : return false;
17790 : : }
17791 : 4496178 : else if ((operator_code == POSTINCREMENT_EXPR
17792 : 4496178 : || operator_code == POSTDECREMENT_EXPR)
17793 : 2161107 : && ! processing_template_decl
17794 : : /* x++ and x--'s second argument must be an int. */
17795 : 5923752 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
17796 : : integer_type_node))
17797 : : {
17798 : 15 : error_at (loc,
17799 : : methodp
17800 : : ? G_("postfix %qD must have %<int%> as its argument")
17801 : : : G_("postfix %qD must have %<int%> as its second argument"),
17802 : : decl);
17803 : 15 : return false;
17804 : : }
17805 : : break;
17806 : :
17807 : 2636901 : case OVL_OP_FLAG_UNARY:
17808 : 2636901 : if (arity != 1)
17809 : : {
17810 : 9 : error_at (loc,
17811 : : methodp
17812 : : ? G_("%qD must have no arguments")
17813 : : : G_("%qD must have exactly one argument"), decl);
17814 : 9 : return false;
17815 : : }
17816 : : break;
17817 : :
17818 : 34854841 : case OVL_OP_FLAG_BINARY:
17819 : 34854841 : if (arity != 2)
17820 : : {
17821 : 3 : error_at (loc,
17822 : : methodp
17823 : : ? G_("%qD must have exactly one argument")
17824 : : : G_("%qD must have exactly two arguments"), decl);
17825 : 3 : return false;
17826 : : }
17827 : : break;
17828 : :
17829 : 0 : default:
17830 : 0 : gcc_unreachable ();
17831 : : }
17832 : :
17833 : : /* There can be no default arguments. */
17834 : 130473498 : for (tree arg = argtypes; arg && arg != void_list_node;
17835 : 84912242 : arg = TREE_CHAIN (arg))
17836 : 84912257 : if (TREE_PURPOSE (arg))
17837 : : {
17838 : 15 : TREE_PURPOSE (arg) = NULL_TREE;
17839 : 15 : error_at (loc, "%qD cannot have default arguments", decl);
17840 : 15 : return false;
17841 : : }
17842 : :
17843 : : /* At this point the declaration is well-formed. It may not be
17844 : : sensible though. */
17845 : :
17846 : : /* Check member function warnings only on the in-class declaration.
17847 : : There's no point warning on an out-of-class definition. */
17848 : 45561241 : if (class_type && class_type != current_class_type)
17849 : : return true;
17850 : :
17851 : : /* Warn about conversion operators that will never be used. */
17852 : 43593938 : if (IDENTIFIER_CONV_OP_P (name)
17853 : 1672265 : && ! DECL_TEMPLATE_INFO (decl)
17854 : 44270326 : && warn_class_conversion)
17855 : : {
17856 : 676355 : tree t = TREE_TYPE (name);
17857 : 676355 : int ref = TYPE_REF_P (t);
17858 : :
17859 : 676355 : if (ref)
17860 : 21569 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
17861 : :
17862 : 676355 : if (VOID_TYPE_P (t))
17863 : 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
17864 : : "will never use a type conversion operator", class_type);
17865 : 676346 : else if (class_type)
17866 : : {
17867 : 676346 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
17868 : 24 : warning_at (loc, OPT_Wclass_conversion,
17869 : : ref
17870 : : ? G_("converting %qT to a reference to the same type "
17871 : : "will never use a type conversion operator")
17872 : : : G_("converting %qT to the same type "
17873 : : "will never use a type conversion operator"),
17874 : : class_type);
17875 : : /* Don't force t to be complete here. */
17876 : 676331 : else if (MAYBE_CLASS_TYPE_P (t)
17877 : 306382 : && COMPLETE_TYPE_P (t)
17878 : 68755 : && DERIVED_FROM_P (t, class_type))
17879 : 30 : warning_at (loc, OPT_Wclass_conversion,
17880 : : ref
17881 : : ? G_("converting %qT to a reference to a base class "
17882 : : "%qT will never use a type conversion operator")
17883 : : : G_("converting %qT to a base class %qT "
17884 : : "will never use a type conversion operator"),
17885 : : class_type, t);
17886 : : }
17887 : : }
17888 : :
17889 : 43593938 : if (!warn_ecpp)
17890 : : return true;
17891 : :
17892 : : /* Effective C++ rules below. */
17893 : :
17894 : : /* More Effective C++ rule 7. */
17895 : 54 : if (operator_code == TRUTH_ANDIF_EXPR
17896 : 54 : || operator_code == TRUTH_ORIF_EXPR
17897 : 54 : || operator_code == COMPOUND_EXPR)
17898 : 0 : warning_at (loc, OPT_Weffc__,
17899 : : "user-defined %qD always evaluates both arguments", decl);
17900 : :
17901 : : /* More Effective C++ rule 6. */
17902 : 54 : if (operator_code == POSTINCREMENT_EXPR
17903 : : || operator_code == POSTDECREMENT_EXPR
17904 : : || operator_code == PREINCREMENT_EXPR
17905 : 54 : || operator_code == PREDECREMENT_EXPR)
17906 : : {
17907 : 0 : tree arg = TREE_VALUE (argtypes);
17908 : 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
17909 : 0 : if (methodp || TYPE_REF_P (arg))
17910 : 0 : arg = TREE_TYPE (arg);
17911 : 0 : arg = TYPE_MAIN_VARIANT (arg);
17912 : :
17913 : 0 : if (operator_code == PREINCREMENT_EXPR
17914 : : || operator_code == PREDECREMENT_EXPR)
17915 : : {
17916 : 0 : if (!TYPE_REF_P (ret)
17917 : 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
17918 : 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
17919 : : build_reference_type (arg));
17920 : : }
17921 : : else
17922 : : {
17923 : 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
17924 : 0 : warning_at (loc, OPT_Weffc__,
17925 : : "postfix %qD should return %qT", decl, arg);
17926 : : }
17927 : : }
17928 : :
17929 : : /* Effective C++ rule 23. */
17930 : 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
17931 : : && (operator_code == PLUS_EXPR
17932 : 9 : || operator_code == MINUS_EXPR
17933 : 9 : || operator_code == TRUNC_DIV_EXPR
17934 : : || operator_code == MULT_EXPR
17935 : : || operator_code == TRUNC_MOD_EXPR)
17936 : 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
17937 : 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
17938 : :
17939 : : return true;
17940 : : }
17941 : :
17942 : : /* Return a string giving the keyword associate with CODE. */
17943 : :
17944 : : static const char *
17945 : 111 : tag_name (enum tag_types code)
17946 : : {
17947 : 111 : switch (code)
17948 : : {
17949 : : case record_type:
17950 : : return "struct";
17951 : 60 : case class_type:
17952 : 60 : return "class";
17953 : 0 : case union_type:
17954 : 0 : return "union";
17955 : 3 : case enum_type:
17956 : 3 : return "enum";
17957 : 0 : case typename_type:
17958 : 0 : return "typename";
17959 : 0 : default:
17960 : 0 : gcc_unreachable ();
17961 : : }
17962 : : }
17963 : :
17964 : : /* Name lookup in an elaborated-type-specifier (after the keyword
17965 : : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
17966 : : elaborated-type-specifier is invalid, issue a diagnostic and return
17967 : : error_mark_node; otherwise, return the *_TYPE to which it referred.
17968 : : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
17969 : :
17970 : : tree
17971 : 11608194 : check_elaborated_type_specifier (enum tag_types tag_code,
17972 : : tree decl,
17973 : : bool allow_template_p)
17974 : : {
17975 : 11608194 : tree type;
17976 : :
17977 : : /* In the case of:
17978 : :
17979 : : struct S { struct S *p; };
17980 : :
17981 : : name lookup will find the TYPE_DECL for the implicit "S::S"
17982 : : typedef. Adjust for that here. */
17983 : 11608194 : if (DECL_SELF_REFERENCE_P (decl))
17984 : 355173 : decl = TYPE_NAME (TREE_TYPE (decl));
17985 : :
17986 : 11608194 : type = TREE_TYPE (decl);
17987 : :
17988 : : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
17989 : : is false for this case as well. */
17990 : 11608194 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17991 : : {
17992 : 9 : error ("using template type parameter %qT after %qs",
17993 : : type, tag_name (tag_code));
17994 : 9 : return error_mark_node;
17995 : : }
17996 : : /* Accept template template parameters. */
17997 : 11608185 : else if (allow_template_p
17998 : 6631458 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
17999 : 6631446 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
18000 : : ;
18001 : : /* [dcl.type.elab]
18002 : :
18003 : : If the identifier resolves to a typedef-name or the
18004 : : simple-template-id resolves to an alias template
18005 : : specialization, the elaborated-type-specifier is ill-formed.
18006 : :
18007 : : In other words, the only legitimate declaration to use in the
18008 : : elaborated type specifier is the implicit typedef created when
18009 : : the type is declared. */
18010 : 11608170 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
18011 : 355289 : && !DECL_SELF_REFERENCE_P (decl)
18012 : 11608286 : && tag_code != typename_type)
18013 : : {
18014 : 78 : auto_diagnostic_group d;
18015 : 78 : if (alias_template_specialization_p (type, nt_opaque))
18016 : 21 : error ("using alias template specialization %qT after %qs",
18017 : : type, tag_name (tag_code));
18018 : : else
18019 : 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
18020 : 78 : inform (DECL_SOURCE_LOCATION (decl),
18021 : : "%qD has a previous declaration here", decl);
18022 : 78 : return error_mark_node;
18023 : 78 : }
18024 : 11608092 : else if (TREE_CODE (type) != RECORD_TYPE
18025 : 11608092 : && TREE_CODE (type) != UNION_TYPE
18026 : : && tag_code != enum_type
18027 : 64768 : && tag_code != typename_type)
18028 : : {
18029 : 6 : auto_diagnostic_group d;
18030 : 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
18031 : 6 : inform (location_of (type), "%qT has a previous declaration here", type);
18032 : 6 : return error_mark_node;
18033 : 6 : }
18034 : 11608086 : else if (TREE_CODE (type) != ENUMERAL_TYPE
18035 : 11543354 : && tag_code == enum_type)
18036 : : {
18037 : 3 : auto_diagnostic_group d;
18038 : 3 : error ("%qT referred to as enum", type);
18039 : 3 : inform (location_of (type), "%qT has a previous declaration here", type);
18040 : 3 : return error_mark_node;
18041 : 3 : }
18042 : 11608083 : else if (!allow_template_p
18043 : 4976667 : && TREE_CODE (type) == RECORD_TYPE
18044 : 16465689 : && CLASSTYPE_IS_TEMPLATE (type))
18045 : : {
18046 : : /* If a class template appears as elaborated type specifier
18047 : : without a template header such as:
18048 : :
18049 : : template <class T> class C {};
18050 : : void f(class C); // No template header here
18051 : :
18052 : : then the required template argument is missing. */
18053 : 18 : error ("template argument required for %<%s %T%>",
18054 : : tag_name (tag_code),
18055 : 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18056 : 18 : return error_mark_node;
18057 : : }
18058 : :
18059 : : return type;
18060 : : }
18061 : :
18062 : : /* Lookup NAME of an elaborated type specifier according to SCOPE and
18063 : : issue diagnostics if necessary. Return *_TYPE node upon success,
18064 : : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18065 : : error. */
18066 : :
18067 : : static tree
18068 : 23004633 : lookup_and_check_tag (enum tag_types tag_code, tree name,
18069 : : TAG_how how, bool template_header_p)
18070 : : {
18071 : 23004633 : tree decl;
18072 : 23004633 : if (how == TAG_how::GLOBAL)
18073 : : {
18074 : : /* First try ordinary name lookup, ignoring hidden class name
18075 : : injected via friend declaration. */
18076 : 1980936 : decl = lookup_name (name, LOOK_want::TYPE);
18077 : : /* If that fails, the name will be placed in the smallest
18078 : : non-class, non-function-prototype scope according to 3.3.1/5.
18079 : : We may already have a hidden name declared as friend in this
18080 : : scope. So lookup again but not ignoring hidden names.
18081 : : If we find one, that name will be made visible rather than
18082 : : creating a new tag. */
18083 : 1980936 : if (!decl)
18084 : 18622 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18085 : : }
18086 : : else
18087 : 21023697 : decl = lookup_elaborated_type (name, how);
18088 : :
18089 : 21042319 : if (!decl)
18090 : : /* We found nothing. */
18091 : : return NULL_TREE;
18092 : :
18093 : 5172511 : if (TREE_CODE (decl) == TREE_LIST)
18094 : : {
18095 : 3 : auto_diagnostic_group d;
18096 : 3 : error ("reference to %qD is ambiguous", name);
18097 : 3 : print_candidates (decl);
18098 : 3 : return error_mark_node;
18099 : 3 : }
18100 : :
18101 : 2138898 : if (DECL_CLASS_TEMPLATE_P (decl)
18102 : 2138892 : && !template_header_p
18103 : 7311400 : && how == TAG_how::CURRENT_ONLY)
18104 : : {
18105 : 12 : auto_diagnostic_group d;
18106 : 12 : error ("class template %qD redeclared as non-template", name);
18107 : 12 : inform (location_of (decl), "previous declaration here");
18108 : 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18109 : 12 : return error_mark_node;
18110 : 12 : }
18111 : :
18112 : 2138886 : if (DECL_CLASS_TEMPLATE_P (decl)
18113 : : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18114 : : so ignore a template template parameter. */
18115 : 5172502 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18116 : 2138883 : decl = DECL_TEMPLATE_RESULT (decl);
18117 : :
18118 : 5172496 : if (TREE_CODE (decl) != TYPE_DECL)
18119 : : /* Found not-a-type. */
18120 : : return NULL_TREE;
18121 : :
18122 : : /* Look for invalid nested type:
18123 : : class C {
18124 : : class C {};
18125 : : }; */
18126 : 5172475 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18127 : : {
18128 : 6 : error ("%qD has the same name as the class in which it is "
18129 : : "declared", decl);
18130 : 6 : return error_mark_node;
18131 : : }
18132 : :
18133 : : /* Two cases we need to consider when deciding if a class
18134 : : template is allowed as an elaborated type specifier:
18135 : : 1. It is a self reference to its own class.
18136 : : 2. It comes with a template header.
18137 : :
18138 : : For example:
18139 : :
18140 : : template <class T> class C {
18141 : : class C *c1; // DECL_SELF_REFERENCE_P is true
18142 : : class D;
18143 : : };
18144 : : template <class U> class C; // template_header_p is true
18145 : : template <class T> class C<T>::D {
18146 : : class C *c2; // DECL_SELF_REFERENCE_P is true
18147 : : }; */
18148 : :
18149 : 15517407 : tree t = check_elaborated_type_specifier (tag_code, decl,
18150 : : template_header_p
18151 : 9989780 : | DECL_SELF_REFERENCE_P (decl));
18152 : 2269823 : if (template_header_p && t && CLASS_TYPE_P (t)
18153 : 7442274 : && (!CLASSTYPE_TEMPLATE_INFO (t)
18154 : 2269787 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18155 : : {
18156 : 21 : auto_diagnostic_group d;
18157 : 21 : error ("%qT is not a template", t);
18158 : 21 : inform (location_of (t), "previous declaration here");
18159 : 42 : if (TYPE_CLASS_SCOPE_P (t)
18160 : 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18161 : 3 : inform (input_location,
18162 : : "perhaps you want to explicitly add %<%T::%>",
18163 : 3 : TYPE_CONTEXT (t));
18164 : 21 : return error_mark_node;
18165 : 21 : }
18166 : :
18167 : : return t;
18168 : : }
18169 : :
18170 : : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18171 : : Define the tag as a forward-reference if it is not defined.
18172 : :
18173 : : If a declaration is given, process it here, and report an error if
18174 : : multiple declarations are not identical.
18175 : :
18176 : : SCOPE is TS_CURRENT when this is also a definition. Only look in
18177 : : the current frame for the name (since C++ allows new names in any
18178 : : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18179 : : declaration. Only look beginning from the current scope outward up
18180 : : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18181 : :
18182 : : TEMPLATE_HEADER_P is true when this declaration is preceded by
18183 : : a set of template parameters. */
18184 : :
18185 : : tree
18186 : 22596815 : xref_tag (enum tag_types tag_code, tree name,
18187 : : TAG_how how, bool template_header_p)
18188 : : {
18189 : 22596815 : enum tree_code code;
18190 : 22596815 : tree context = NULL_TREE;
18191 : :
18192 : 22596815 : auto_cond_timevar tv (TV_NAME_LOOKUP);
18193 : :
18194 : 22596815 : gcc_assert (identifier_p (name));
18195 : :
18196 : 22596815 : switch (tag_code)
18197 : : {
18198 : : case record_type:
18199 : : case class_type:
18200 : : code = RECORD_TYPE;
18201 : : break;
18202 : : case union_type:
18203 : : code = UNION_TYPE;
18204 : : break;
18205 : : case enum_type:
18206 : : code = ENUMERAL_TYPE;
18207 : : break;
18208 : 0 : default:
18209 : 0 : gcc_unreachable ();
18210 : : }
18211 : :
18212 : : /* In case of anonymous name, xref_tag is only called to
18213 : : make type node and push name. Name lookup is not required. */
18214 : 22596815 : tree t = NULL_TREE;
18215 : 22596815 : if (!IDENTIFIER_ANON_P (name))
18216 : 20847373 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18217 : :
18218 : 22596815 : if (t == error_mark_node)
18219 : : return error_mark_node;
18220 : :
18221 : 2590077 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18222 : 1139463 : && template_class_depth (current_class_type)
18223 : 23013696 : && template_header_p)
18224 : : {
18225 : 289781 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18226 : : return t;
18227 : :
18228 : : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18229 : : a definition of this tag. Since, in addition, we are
18230 : : currently processing a (member) template declaration of a
18231 : : template class, we must be very careful; consider:
18232 : :
18233 : : template <class X> struct S1
18234 : :
18235 : : template <class U> struct S2
18236 : : {
18237 : : template <class V> friend struct S1;
18238 : : };
18239 : :
18240 : : Here, the S2::S1 declaration should not be confused with the
18241 : : outer declaration. In particular, the inner version should
18242 : : have a template parameter of level 2, not level 1.
18243 : :
18244 : : On the other hand, when presented with:
18245 : :
18246 : : template <class T> struct S1
18247 : : {
18248 : : template <class U> struct S2 {};
18249 : : template <class U> friend struct S2;
18250 : : };
18251 : :
18252 : : the friend must find S1::S2 eventually. We accomplish this
18253 : : by making sure that the new type we create to represent this
18254 : : declaration has the right TYPE_CONTEXT. */
18255 : 289778 : context = TYPE_CONTEXT (t);
18256 : 289778 : t = NULL_TREE;
18257 : : }
18258 : :
18259 : 22596701 : if (! t)
18260 : : {
18261 : : /* If no such tag is yet defined, create a forward-reference node
18262 : : and record it as the "definition".
18263 : : When a real declaration of this type is found,
18264 : : the forward-reference will be altered into a real type. */
18265 : 17749586 : if (code == ENUMERAL_TYPE)
18266 : : {
18267 : 14 : error ("use of enum %q#D without previous declaration", name);
18268 : 14 : return error_mark_node;
18269 : : }
18270 : :
18271 : 17749572 : t = make_class_type (code);
18272 : 17749572 : TYPE_CONTEXT (t) = context;
18273 : 17749572 : if (IDENTIFIER_LAMBDA_P (name))
18274 : : /* Mark it as a lambda type right now. Our caller will
18275 : : correct the value. */
18276 : 1066519 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18277 : 17749572 : t = pushtag (name, t, how);
18278 : : }
18279 : : else
18280 : : {
18281 : 4847115 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18282 : : {
18283 : : /* Check that we aren't trying to overload a class with different
18284 : : constraints. */
18285 : 1980006 : if (!redeclare_class_template (t, current_template_parms,
18286 : : current_template_constraints ()))
18287 : 60 : return error_mark_node;
18288 : : }
18289 : 2867109 : else if (!processing_template_decl
18290 : 2667678 : && CLASS_TYPE_P (t)
18291 : 5508543 : && CLASSTYPE_IS_TEMPLATE (t))
18292 : : {
18293 : 0 : auto_diagnostic_group d;
18294 : 0 : error ("redeclaration of %qT as a non-template", t);
18295 : 0 : inform (location_of (t), "previous declaration %qD", t);
18296 : 0 : return error_mark_node;
18297 : 0 : }
18298 : :
18299 : 4847055 : if (modules_p ()
18300 : 4847055 : && how == TAG_how::CURRENT_ONLY)
18301 : : {
18302 : 9395 : tree decl = TYPE_NAME (t);
18303 : 9395 : if (!module_may_redeclare (decl))
18304 : 15 : return error_mark_node;
18305 : :
18306 : 9380 : tree not_tmpl = STRIP_TEMPLATE (decl);
18307 : 9380 : if (DECL_LANG_SPECIFIC (not_tmpl)
18308 : 4312 : && DECL_MODULE_ATTACH_P (not_tmpl)
18309 : 45 : && !DECL_MODULE_EXPORT_P (not_tmpl)
18310 : 9413 : && module_exporting_p ())
18311 : : {
18312 : 6 : auto_diagnostic_group d;
18313 : 6 : error ("conflicting exporting for declaration %qD", decl);
18314 : 6 : inform (DECL_SOURCE_LOCATION (decl),
18315 : : "previously declared here without exporting");
18316 : 6 : }
18317 : :
18318 : 9380 : tree maybe_tmpl = decl;
18319 : 9380 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18320 : 6988 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18321 : :
18322 : : /* FIXME: we should do a more precise check for redefinitions
18323 : : of a conflicting using-declaration here, as these diagnostics
18324 : : are not ideal. */
18325 : 9380 : if (DECL_LANG_SPECIFIC (decl)
18326 : 4312 : && DECL_MODULE_IMPORT_P (decl)
18327 : 9437 : && CP_DECL_CONTEXT (decl) == current_namespace)
18328 : : {
18329 : : /* Push it into this TU's symbol slot. */
18330 : 54 : if (maybe_tmpl != decl)
18331 : : /* We're in the template parm binding level.
18332 : : Pushtag has logic to slide under that, but we're
18333 : : not pushing a *new* type. */
18334 : 20 : push_nested_namespace (CP_DECL_CONTEXT (decl));
18335 : :
18336 : 54 : pushdecl (maybe_tmpl);
18337 : 54 : if (maybe_tmpl != decl)
18338 : 20 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
18339 : : }
18340 : :
18341 : 9380 : set_instantiating_module (maybe_tmpl);
18342 : : }
18343 : : }
18344 : :
18345 : : return t;
18346 : 22596815 : }
18347 : :
18348 : : /* Create the binfo hierarchy for REF with (possibly NULL) base list
18349 : : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18350 : : access_* node, and the TREE_VALUE is the type of the base-class.
18351 : : Non-NULL TREE_TYPE indicates virtual inheritance. */
18352 : :
18353 : : void
18354 : 59797661 : xref_basetypes (tree ref, tree base_list)
18355 : : {
18356 : 59797661 : tree *basep;
18357 : 59797661 : tree binfo, base_binfo;
18358 : 59797661 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18359 : 59797661 : unsigned max_bases = 0; /* Maximum direct bases. */
18360 : 59797661 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18361 : 59797661 : int i;
18362 : 59797661 : tree default_access;
18363 : 59797661 : tree igo_prev; /* Track Inheritance Graph Order. */
18364 : :
18365 : 59797661 : if (ref == error_mark_node)
18366 : 12 : return;
18367 : :
18368 : : /* The base of a derived class is private by default, all others are
18369 : : public. */
18370 : 59797661 : default_access = (TREE_CODE (ref) == RECORD_TYPE
18371 : 59243455 : && CLASSTYPE_DECLARED_CLASS (ref)
18372 : 59797661 : ? access_private_node : access_public_node);
18373 : :
18374 : : /* First, make sure that any templates in base-classes are
18375 : : instantiated. This ensures that if we call ourselves recursively
18376 : : we do not get confused about which classes are marked and which
18377 : : are not. */
18378 : 59797661 : basep = &base_list;
18379 : 90398384 : while (*basep)
18380 : : {
18381 : 30600723 : tree basetype = TREE_VALUE (*basep);
18382 : :
18383 : : /* The dependent_type_p call below should really be dependent_scope_p
18384 : : so that we give a hard error about using an incomplete type as a
18385 : : base, but we allow it with a pedwarn for backward
18386 : : compatibility. */
18387 : 30600723 : if (processing_template_decl
18388 : 30600723 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18389 : 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
18390 : : diagnostics::kind::pedwarn);
18391 : 30600723 : if (!dependent_type_p (basetype)
18392 : 30600723 : && !complete_type_or_else (basetype, NULL))
18393 : : /* An incomplete type. Remove it from the list. */
18394 : 15 : *basep = TREE_CHAIN (*basep);
18395 : : else
18396 : : {
18397 : 30600708 : max_bases++;
18398 : 30600708 : if (TREE_TYPE (*basep))
18399 : 71277 : max_dvbases++;
18400 : 30600708 : if (CLASS_TYPE_P (basetype))
18401 : 29762141 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18402 : 30600708 : basep = &TREE_CHAIN (*basep);
18403 : : }
18404 : : }
18405 : 59797661 : max_vbases += max_dvbases;
18406 : :
18407 : 59797661 : TYPE_MARKED_P (ref) = 1;
18408 : :
18409 : : /* The binfo slot should be empty, unless this is an (ill-formed)
18410 : : redefinition. */
18411 : 59797661 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18412 : :
18413 : 59797661 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18414 : :
18415 : 59797661 : binfo = make_tree_binfo (max_bases);
18416 : :
18417 : 59797661 : TYPE_BINFO (ref) = binfo;
18418 : 59797661 : BINFO_OFFSET (binfo) = size_zero_node;
18419 : 59797661 : BINFO_TYPE (binfo) = ref;
18420 : :
18421 : : /* Apply base-class info set up to the variants of this type. */
18422 : 59797661 : fixup_type_variants (ref);
18423 : :
18424 : 59797661 : if (max_bases)
18425 : : {
18426 : 29696872 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
18427 : : /* A C++98 POD cannot have base classes. */
18428 : 29696872 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18429 : :
18430 : 29696872 : if (TREE_CODE (ref) == UNION_TYPE)
18431 : : {
18432 : 12 : error ("derived union %qT invalid", ref);
18433 : 12 : return;
18434 : : }
18435 : : }
18436 : :
18437 : 29696860 : if (max_bases > 1)
18438 : 703230 : warning (OPT_Wmultiple_inheritance,
18439 : : "%qT defined with multiple direct bases", ref);
18440 : :
18441 : 59797649 : if (max_vbases)
18442 : : {
18443 : : /* An aggregate can't have virtual base classes. */
18444 : 215136 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18445 : :
18446 : 215136 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
18447 : :
18448 : 215136 : if (max_dvbases)
18449 : 70082 : warning (OPT_Wvirtual_inheritance,
18450 : : "%qT defined with direct virtual base", ref);
18451 : : }
18452 : :
18453 : 90398345 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18454 : : {
18455 : 30600696 : tree access = TREE_PURPOSE (base_list);
18456 : 30600696 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18457 : 30600696 : tree basetype = TREE_VALUE (base_list);
18458 : :
18459 : 30600696 : if (access == access_default_node)
18460 : 4492643 : access = default_access;
18461 : :
18462 : : /* Before C++17, an aggregate cannot have base classes. In C++17, an
18463 : : aggregate can't have virtual, private, or protected base classes. */
18464 : 30600696 : if (cxx_dialect < cxx17
18465 : 30389711 : || access != access_public_node
18466 : 29447667 : || via_virtual)
18467 : 1221244 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18468 : :
18469 : 30600696 : if (PACK_EXPANSION_P (basetype))
18470 : 24203 : basetype = PACK_EXPANSION_PATTERN (basetype);
18471 : 30600696 : if (TREE_CODE (basetype) == TYPE_DECL)
18472 : 0 : basetype = TREE_TYPE (basetype);
18473 : 30600696 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18474 : : {
18475 : 24 : error ("base type %qT fails to be a struct or class type",
18476 : : basetype);
18477 : 24 : goto dropped_base;
18478 : : }
18479 : :
18480 : 30600672 : base_binfo = NULL_TREE;
18481 : 30600672 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18482 : : {
18483 : 23901909 : base_binfo = TYPE_BINFO (basetype);
18484 : : /* The original basetype could have been a typedef'd type. */
18485 : 23901909 : basetype = BINFO_TYPE (base_binfo);
18486 : :
18487 : : /* Inherit flags from the base. */
18488 : 71705727 : TYPE_HAS_NEW_OPERATOR (ref)
18489 : 23901909 : |= TYPE_HAS_NEW_OPERATOR (basetype);
18490 : 71705727 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18491 : 23901909 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18492 : 23901909 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18493 : 23901909 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18494 : 71705727 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
18495 : 23901909 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18496 : 71705727 : CLASSTYPE_REPEATED_BASE_P (ref)
18497 : 23901909 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
18498 : : }
18499 : :
18500 : : /* We must do this test after we've seen through a typedef
18501 : : type. */
18502 : 30600672 : if (TYPE_MARKED_P (basetype))
18503 : : {
18504 : 18 : if (basetype == ref)
18505 : 0 : error ("recursive type %qT undefined", basetype);
18506 : : else
18507 : 18 : error ("duplicate base type %qT invalid", basetype);
18508 : 18 : goto dropped_base;
18509 : : }
18510 : :
18511 : 30600654 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18512 : : /* Regenerate the pack expansion for the bases. */
18513 : 24203 : basetype = make_pack_expansion (basetype);
18514 : :
18515 : 30600654 : TYPE_MARKED_P (basetype) = 1;
18516 : :
18517 : 30600654 : base_binfo = copy_binfo (base_binfo, basetype, ref,
18518 : : &igo_prev, via_virtual);
18519 : 30600654 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
18520 : 30529392 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
18521 : :
18522 : 30600654 : BINFO_BASE_APPEND (binfo, base_binfo);
18523 : 30600654 : BINFO_BASE_ACCESS_APPEND (binfo, access);
18524 : 30600654 : continue;
18525 : :
18526 : 42 : dropped_base:
18527 : : /* Update max_vbases to reflect the reality that we are dropping
18528 : : this base: if it reaches zero we want to undo the vec_alloc
18529 : : above to avoid inconsistencies during error-recovery: eg, in
18530 : : build_special_member_call, CLASSTYPE_VBASECLASSES non null
18531 : : and vtt null (c++/27952). */
18532 : 42 : if (via_virtual)
18533 : 12 : max_vbases--;
18534 : 42 : if (CLASS_TYPE_P (basetype))
18535 : 30 : max_vbases
18536 : 33 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18537 : 30600654 : }
18538 : :
18539 : 59797649 : if (CLASSTYPE_VBASECLASSES (ref)
18540 : 59797649 : && max_vbases == 0)
18541 : 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
18542 : :
18543 : 60012773 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18544 : : /* If we didn't get max_vbases vbases, we must have shared at
18545 : : least one of them, and are therefore diamond shaped. */
18546 : 23122 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18547 : :
18548 : : /* Unmark all the types. */
18549 : 90398303 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18550 : 30600654 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18551 : 59797649 : TYPE_MARKED_P (ref) = 0;
18552 : :
18553 : : /* Now see if we have a repeated base type. */
18554 : 59797649 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
18555 : : {
18556 : 153896037 : for (base_binfo = binfo; base_binfo;
18557 : 94098820 : base_binfo = TREE_CHAIN (base_binfo))
18558 : : {
18559 : 94099534 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18560 : : {
18561 : 714 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18562 : 714 : break;
18563 : : }
18564 : 94098820 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18565 : : }
18566 : 153896037 : for (base_binfo = binfo; base_binfo;
18567 : 94098820 : base_binfo = TREE_CHAIN (base_binfo))
18568 : 94099534 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18569 : 94098820 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18570 : : else
18571 : : break;
18572 : : }
18573 : : }
18574 : :
18575 : :
18576 : : /* Copies the enum-related properties from type SRC to type DST.
18577 : : Used with the underlying type of an enum and the enum itself. */
18578 : : static void
18579 : 1826040 : copy_type_enum (tree dst, tree src)
18580 : : {
18581 : 1826040 : tree t;
18582 : 3652098 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
18583 : : {
18584 : 1826058 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
18585 : 1826058 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
18586 : 1826058 : TYPE_SIZE (t) = TYPE_SIZE (src);
18587 : 1826058 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
18588 : 1826058 : SET_TYPE_MODE (dst, TYPE_MODE (src));
18589 : 1826058 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
18590 : 1826058 : unsigned valign = TYPE_ALIGN (src);
18591 : 1826058 : if (TYPE_USER_ALIGN (t))
18592 : 12 : valign = MAX (valign, TYPE_ALIGN (t));
18593 : : else
18594 : 1826046 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
18595 : 1826058 : SET_TYPE_ALIGN (t, valign);
18596 : 1826058 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
18597 : : }
18598 : 1826040 : }
18599 : :
18600 : : /* Begin compiling the definition of an enumeration type.
18601 : : NAME is its name,
18602 : :
18603 : : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
18604 : :
18605 : : UNDERLYING_TYPE is the type that will be used as the storage for
18606 : : the enumeration type. This should be NULL_TREE if no storage type
18607 : : was specified.
18608 : :
18609 : : ATTRIBUTES are any attributes specified after the enum-key.
18610 : :
18611 : : SCOPED_ENUM_P is true if this is a scoped enumeration type.
18612 : :
18613 : : if IS_NEW is not NULL, gets TRUE iff a new type is created.
18614 : :
18615 : : Returns the type object, as yet incomplete.
18616 : : Also records info about it so that build_enumerator
18617 : : may be used to declare the individual values as they are read. */
18618 : :
18619 : : tree
18620 : 2157391 : start_enum (tree name, tree enumtype, tree underlying_type,
18621 : : tree attributes, bool scoped_enum_p, bool *is_new)
18622 : : {
18623 : 2157391 : tree prevtype = NULL_TREE;
18624 : 2157391 : gcc_assert (identifier_p (name));
18625 : :
18626 : 2157391 : if (is_new)
18627 : 1719988 : *is_new = false;
18628 : : /* [C++0x dcl.enum]p5:
18629 : :
18630 : : If not explicitly specified, the underlying type of a scoped
18631 : : enumeration type is int. */
18632 : 2157391 : if (!underlying_type && scoped_enum_p)
18633 : 128947 : underlying_type = integer_type_node;
18634 : :
18635 : 2157391 : if (underlying_type)
18636 : 455184 : underlying_type = cv_unqualified (underlying_type);
18637 : :
18638 : : /* If this is the real definition for a previous forward reference,
18639 : : fill in the contents in the same object that used to be the
18640 : : forward reference. */
18641 : 2157391 : if (!enumtype)
18642 : 2157260 : enumtype = lookup_and_check_tag (enum_type, name,
18643 : : /*tag_scope=*/TAG_how::CURRENT_ONLY,
18644 : : /*template_header_p=*/false);
18645 : :
18646 : : /* In case of a template_decl, the only check that should be deferred
18647 : : to instantiation time is the comparison of underlying types. */
18648 : 2157391 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
18649 : : {
18650 : : /* Attempt to set the declaring module. */
18651 : 35611 : if (modules_p ())
18652 : : {
18653 : 170 : tree decl = TYPE_NAME (enumtype);
18654 : 170 : if (!module_may_redeclare (decl))
18655 : 3 : enumtype = error_mark_node;
18656 : : else
18657 : 167 : set_instantiating_module (decl);
18658 : : }
18659 : :
18660 : 35611 : if (enumtype == error_mark_node)
18661 : : ;
18662 : 35798 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
18663 : : {
18664 : 6 : auto_diagnostic_group d;
18665 : 6 : error_at (input_location, "scoped/unscoped mismatch "
18666 : : "in enum %q#T", enumtype);
18667 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18668 : : "previous definition here");
18669 : 6 : enumtype = error_mark_node;
18670 : 6 : }
18671 : 35602 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
18672 : : {
18673 : 0 : auto_diagnostic_group d;
18674 : 0 : error_at (input_location, "underlying type mismatch "
18675 : : "in enum %q#T", enumtype);
18676 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18677 : : "previous definition here");
18678 : 0 : enumtype = error_mark_node;
18679 : 0 : }
18680 : 35586 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
18681 : 71188 : && !same_type_p (underlying_type,
18682 : : ENUM_UNDERLYING_TYPE (enumtype)))
18683 : : {
18684 : 26 : auto_diagnostic_group d;
18685 : 26 : error_at (input_location, "different underlying type "
18686 : : "in enum %q#T", enumtype);
18687 : 26 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18688 : : "previous definition here");
18689 : 26 : underlying_type = NULL_TREE;
18690 : 26 : }
18691 : : }
18692 : :
18693 : 2157391 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
18694 : 35602 : || processing_template_decl)
18695 : : {
18696 : : /* In case of error, make a dummy enum to allow parsing to
18697 : : continue. */
18698 : 2121877 : if (enumtype == error_mark_node)
18699 : : {
18700 : 9 : name = make_anon_name ();
18701 : 9 : enumtype = NULL_TREE;
18702 : : }
18703 : :
18704 : : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
18705 : : of an opaque enum, or an opaque enum of an already defined
18706 : : enumeration (C++11).
18707 : : In any other case, it'll be NULL_TREE. */
18708 : 2121877 : if (!enumtype)
18709 : : {
18710 : 2121786 : if (is_new)
18711 : 1684386 : *is_new = true;
18712 : : }
18713 : 2121877 : prevtype = enumtype;
18714 : :
18715 : : /* Do not push the decl more than once. */
18716 : 2121877 : if (!enumtype
18717 : 91 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
18718 : : {
18719 : 2121789 : enumtype = cxx_make_type (ENUMERAL_TYPE);
18720 : 2121789 : enumtype = pushtag (name, enumtype);
18721 : :
18722 : 2121789 : if (enumtype != error_mark_node)
18723 : : {
18724 : : /* The enum is considered opaque until the opening '{' of the
18725 : : enumerator list. */
18726 : 2121771 : SET_OPAQUE_ENUM_P (enumtype, true);
18727 : 2121771 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
18728 : :
18729 : : /* std::byte aliases anything. */
18730 : 2121771 : if (TYPE_CONTEXT (enumtype) == std_node
18731 : 2454234 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
18732 : 13128 : TYPE_ALIAS_SET (enumtype) = 0;
18733 : : }
18734 : : }
18735 : : else
18736 : 88 : enumtype = xref_tag (enum_type, name);
18737 : :
18738 : 2121877 : if (enumtype == error_mark_node)
18739 : : return error_mark_node;
18740 : : }
18741 : :
18742 : 2157373 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
18743 : :
18744 : 2157373 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18745 : :
18746 : 2157373 : if (underlying_type)
18747 : : {
18748 : 455155 : if (ENUM_UNDERLYING_TYPE (enumtype))
18749 : : /* We already checked that it matches, don't change it to a different
18750 : : typedef variant. */;
18751 : 419595 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
18752 : : {
18753 : 419531 : copy_type_enum (enumtype, underlying_type);
18754 : 419531 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18755 : : }
18756 : 64 : else if (dependent_type_p (underlying_type))
18757 : 51 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18758 : : else
18759 : : {
18760 : 13 : error ("underlying type %qT of %qT must be an integral type",
18761 : : underlying_type, enumtype);
18762 : 13 : copy_type_enum (enumtype, integer_type_node);
18763 : 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
18764 : : }
18765 : : }
18766 : :
18767 : : /* If into a template class, the returned enum is always the first
18768 : : declaration (opaque or not) seen. This way all the references to
18769 : : this type will be to the same declaration. The following ones are used
18770 : : only to check for definition errors. */
18771 : 2157373 : if (prevtype && processing_template_decl)
18772 : : return prevtype;
18773 : : else
18774 : 2157285 : return enumtype;
18775 : : }
18776 : :
18777 : : /* Returns true if TYPE is an enum that uses an enumerator name for
18778 : : linkage purposes at namespace scope. The term is defined in [dcl.enum]/12
18779 : : for all enums, not just those at namespace scope, but for backward ABI
18780 : : compatibility we want to treat those not at namespace scope the old way
18781 : : and e.g. mangle the class scope ones based on their position within the
18782 : : class rather than the first enumerator. */
18783 : :
18784 : : bool
18785 : 362137339 : enum_with_enumerator_for_linkage_p (tree type)
18786 : : {
18787 : 362137339 : return (cxx_dialect >= cxx20
18788 : 143606150 : && UNSCOPED_ENUM_P (type)
18789 : 6341660 : && TYPE_ANON_P (type)
18790 : 1159364 : && TYPE_VALUES (type)
18791 : 362847573 : && TYPE_NAMESPACE_SCOPE_P (type));
18792 : : }
18793 : :
18794 : : /* After processing and defining all the values of an enumeration type,
18795 : : install their decls in the enumeration type.
18796 : : ENUMTYPE is the type object. */
18797 : :
18798 : : void
18799 : 2056269 : finish_enum_value_list (tree enumtype)
18800 : : {
18801 : 2056269 : tree values;
18802 : 2056269 : tree underlying_type;
18803 : 2056269 : tree decl;
18804 : 2056269 : tree value;
18805 : 2056269 : tree minnode, maxnode;
18806 : 2056269 : tree t;
18807 : :
18808 : 2056269 : bool fixed_underlying_type_p
18809 : 2056269 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
18810 : :
18811 : : /* We built up the VALUES in reverse order. */
18812 : 2056269 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
18813 : :
18814 : : /* For an enum defined in a template, just set the type of the values;
18815 : : all further processing is postponed until the template is
18816 : : instantiated. We need to set the type so that tsubst of a CONST_DECL
18817 : : works. */
18818 : 2056269 : if (processing_template_decl)
18819 : : {
18820 : 373191 : for (values = TYPE_VALUES (enumtype);
18821 : 689825 : values;
18822 : 373191 : values = TREE_CHAIN (values))
18823 : 373191 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
18824 : : return;
18825 : : }
18826 : :
18827 : : /* Determine the minimum and maximum values of the enumerators. */
18828 : 1739635 : if (TYPE_VALUES (enumtype))
18829 : : {
18830 : : minnode = maxnode = NULL_TREE;
18831 : :
18832 : 10913175 : for (values = TYPE_VALUES (enumtype);
18833 : 12623696 : values;
18834 : 10913175 : values = TREE_CHAIN (values))
18835 : : {
18836 : 10913175 : decl = TREE_VALUE (values);
18837 : :
18838 : : /* [dcl.enum]: Following the closing brace of an enum-specifier,
18839 : : each enumerator has the type of its enumeration. Prior to the
18840 : : closing brace, the type of each enumerator is the type of its
18841 : : initializing value. */
18842 : 10913175 : TREE_TYPE (decl) = enumtype;
18843 : :
18844 : : /* Update the minimum and maximum values, if appropriate. */
18845 : 10913175 : value = DECL_INITIAL (decl);
18846 : 10913175 : if (TREE_CODE (value) != INTEGER_CST)
18847 : 4 : value = integer_zero_node;
18848 : : /* Figure out what the minimum and maximum values of the
18849 : : enumerators are. */
18850 : 10913175 : if (!minnode)
18851 : : minnode = maxnode = value;
18852 : 9202654 : else if (tree_int_cst_lt (maxnode, value))
18853 : : maxnode = value;
18854 : 1404517 : else if (tree_int_cst_lt (value, minnode))
18855 : 154985 : minnode = value;
18856 : : }
18857 : : }
18858 : : else
18859 : : /* [dcl.enum]
18860 : :
18861 : : If the enumerator-list is empty, the underlying type is as if
18862 : : the enumeration had a single enumerator with value 0. */
18863 : 29114 : minnode = maxnode = integer_zero_node;
18864 : :
18865 : 1739635 : if (!fixed_underlying_type_p)
18866 : : {
18867 : : /* Compute the number of bits require to represent all values of the
18868 : : enumeration. We must do this before the type of MINNODE and
18869 : : MAXNODE are transformed, since tree_int_cst_min_precision relies
18870 : : on the TREE_TYPE of the value it is passed. */
18871 : 1406496 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
18872 : 1406496 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
18873 : 1406496 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
18874 : 1406496 : int precision = MAX (lowprec, highprec);
18875 : 1406496 : unsigned int itk;
18876 : 1406496 : bool use_short_enum;
18877 : :
18878 : : /* Determine the underlying type of the enumeration.
18879 : :
18880 : : [dcl.enum]
18881 : :
18882 : : The underlying type of an enumeration is an integral type that
18883 : : can represent all the enumerator values defined in the
18884 : : enumeration. It is implementation-defined which integral type is
18885 : : used as the underlying type for an enumeration except that the
18886 : : underlying type shall not be larger than int unless the value of
18887 : : an enumerator cannot fit in an int or unsigned int.
18888 : :
18889 : : We use "int" or an "unsigned int" as the underlying type, even if
18890 : : a smaller integral type would work, unless the user has
18891 : : explicitly requested that we use the smallest possible type. The
18892 : : user can request that for all enumerations with a command line
18893 : : flag, or for just one enumeration with an attribute. */
18894 : :
18895 : 2812992 : use_short_enum = flag_short_enums
18896 : 1406496 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
18897 : :
18898 : : /* If the precision of the type was specified with an attribute and it
18899 : : was too small, give an error. Otherwise, use it. */
18900 : 1406496 : if (TYPE_PRECISION (enumtype))
18901 : : {
18902 : 11 : if (precision > TYPE_PRECISION (enumtype))
18903 : 0 : error ("specified mode too small for enumerated values");
18904 : : else
18905 : : {
18906 : 11 : use_short_enum = true;
18907 : 11 : precision = TYPE_PRECISION (enumtype);
18908 : : }
18909 : : }
18910 : :
18911 : 2731400 : for (itk = (use_short_enum ? itk_char : itk_int);
18912 : 2731400 : itk != itk_none;
18913 : : itk++)
18914 : : {
18915 : 2731397 : underlying_type = integer_types[itk];
18916 : 2731397 : if (underlying_type != NULL_TREE
18917 : 2731379 : && TYPE_PRECISION (underlying_type) >= precision
18918 : 5460775 : && TYPE_SIGN (underlying_type) == sgn)
18919 : : break;
18920 : : }
18921 : 1406496 : if (itk == itk_none)
18922 : : {
18923 : : /* DR 377
18924 : :
18925 : : IF no integral type can represent all the enumerator values, the
18926 : : enumeration is ill-formed. */
18927 : 3 : error ("no integral type can represent all of the enumerator values "
18928 : : "for %qT", enumtype);
18929 : 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
18930 : 3 : underlying_type = integer_types[itk_unsigned_long_long];
18931 : : }
18932 : :
18933 : : /* [dcl.enum]
18934 : :
18935 : : The value of sizeof() applied to an enumeration type, an object
18936 : : of an enumeration type, or an enumerator, is the value of sizeof()
18937 : : applied to the underlying type. */
18938 : 1406496 : copy_type_enum (enumtype, underlying_type);
18939 : :
18940 : : /* Compute the minimum and maximum values for the type.
18941 : :
18942 : : [dcl.enum]
18943 : :
18944 : : For an enumeration where emin is the smallest enumerator and emax
18945 : : is the largest, the values of the enumeration are the values of the
18946 : : underlying type in the range bmin to bmax, where bmin and bmax are,
18947 : : respectively, the smallest and largest values of the smallest bit-
18948 : : field that can store emin and emax. */
18949 : :
18950 : : /* The middle-end currently assumes that types with TYPE_PRECISION
18951 : : narrower than their underlying type are suitably zero or sign
18952 : : extended to fill their mode. Similarly, it assumes that the front
18953 : : end assures that a value of a particular type must be within
18954 : : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
18955 : :
18956 : : We used to set these fields based on bmin and bmax, but that led
18957 : : to invalid assumptions like optimizing away bounds checking. So
18958 : : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
18959 : : TYPE_MAX_VALUE to the values for the mode above and only restrict
18960 : : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
18961 : 1406496 : ENUM_UNDERLYING_TYPE (enumtype)
18962 : 1406496 : = build_distinct_type_copy (underlying_type);
18963 : 1406496 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
18964 : 1406496 : set_min_and_max_values_for_integral_type
18965 : 1406496 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
18966 : :
18967 : : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
18968 : 1406496 : if (flag_strict_enums)
18969 : 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
18970 : :
18971 : 1406496 : if (use_short_enum)
18972 : : {
18973 : 79 : TYPE_PACKED (enumtype) = use_short_enum;
18974 : 79 : fixup_attribute_variants (enumtype);
18975 : : }
18976 : : }
18977 : : else
18978 : 333139 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
18979 : :
18980 : : /* If the enum is exported, mark the consts too. */
18981 : 1739635 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
18982 : 1477169 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
18983 : 1740207 : && at_namespace_scope_p ());
18984 : :
18985 : : /* Convert each of the enumerators to the type of the underlying
18986 : : type of the enumeration. */
18987 : 12652810 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
18988 : : {
18989 : 10913175 : decl = TREE_VALUE (values);
18990 : 10913175 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
18991 : 10913175 : if (fixed_underlying_type_p)
18992 : : /* If the enumeration type has a fixed underlying type, we
18993 : : already checked all of the enumerator values. */
18994 : 3239539 : value = DECL_INITIAL (decl);
18995 : : else
18996 : 7673636 : value = perform_implicit_conversion (underlying_type,
18997 : 7673636 : DECL_INITIAL (decl),
18998 : : tf_warning_or_error);
18999 : : /* Do not clobber shared ints. But do share identical enumerators. */
19000 : 10913175 : value = fold_convert (enumtype, value);
19001 : :
19002 : 10913175 : DECL_INITIAL (decl) = value;
19003 : 10913175 : if (export_p)
19004 : 3510 : DECL_MODULE_EXPORT_P (decl) = true;
19005 : 10913175 : }
19006 : :
19007 : : /* Fix up all variant types of this enum type. */
19008 : 3479288 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
19009 : 1739653 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
19010 : :
19011 : 1739635 : if (at_class_scope_p ()
19012 : 966429 : && COMPLETE_TYPE_P (current_class_type)
19013 : 1739696 : && UNSCOPED_ENUM_P (enumtype))
19014 : : {
19015 : 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
19016 : : /* TYPE_FIELDS needs fixup. */
19017 : 42 : fixup_type_variants (current_class_type);
19018 : : }
19019 : :
19020 : : /* P2115: An unnamed enum uses the name of its first enumerator for
19021 : : linkage purposes; reset the type linkage if that is the case. */
19022 : 1739635 : if (enum_with_enumerator_for_linkage_p (enumtype))
19023 : 109763 : reset_type_linkage (enumtype);
19024 : :
19025 : : /* Finish debugging output for this type. */
19026 : 1739635 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
19027 : :
19028 : : /* Each enumerator now has the type of its enumeration. Clear the cache
19029 : : so that this change in types doesn't confuse us later on. */
19030 : 1739635 : clear_cv_and_fold_caches ();
19031 : : }
19032 : :
19033 : : /* Finishes the enum type. This is called only the first time an
19034 : : enumeration is seen, be it opaque or odinary.
19035 : : ENUMTYPE is the type object. */
19036 : :
19037 : : void
19038 : 2045483 : finish_enum (tree enumtype)
19039 : : {
19040 : 2045483 : if (processing_template_decl)
19041 : : {
19042 : 316662 : if (at_function_scope_p ())
19043 : 2779 : add_stmt (build_min (TAG_DEFN, enumtype));
19044 : 316662 : return;
19045 : : }
19046 : :
19047 : : /* If this is a forward declaration, there should not be any variants,
19048 : : though we can get a variant in the middle of an enum-specifier with
19049 : : wacky code like 'enum E { e = sizeof(const E*) };' */
19050 : 3457642 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19051 : : && (TYPE_VALUES (enumtype)
19052 : : || !TYPE_NEXT_VARIANT (enumtype)));
19053 : : }
19054 : :
19055 : : /* Build and install a CONST_DECL for an enumeration constant of the
19056 : : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19057 : : Apply ATTRIBUTES if available. LOC is the location of NAME.
19058 : : Assignment of sequential values by default is handled here. */
19059 : :
19060 : : tree
19061 : 11286343 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19062 : : location_t loc)
19063 : : {
19064 : 11286343 : tree decl;
19065 : 11286343 : tree context;
19066 : 11286343 : tree type;
19067 : :
19068 : : /* scalar_constant_value will pull out this expression, so make sure
19069 : : it's folded as appropriate.
19070 : :
19071 : : Creating a TARGET_EXPR in a template breaks when substituting, and
19072 : : here we would create it for instance when using a class prvalue with
19073 : : a user-defined conversion function. So don't use such a tree. We
19074 : : instantiate VALUE here to get errors about bad enumerators even in
19075 : : a template that does not get instantiated. */
19076 : 11286343 : if (processing_template_decl)
19077 : 373191 : value = maybe_fold_non_dependent_expr (value);
19078 : :
19079 : : /* If the VALUE was erroneous, pretend it wasn't there; that will
19080 : : result in the enum being assigned the next value in sequence. */
19081 : 11286343 : if (value == error_mark_node)
19082 : : value = NULL_TREE;
19083 : :
19084 : : /* Remove no-op casts from the value. */
19085 : 11286305 : if (value)
19086 : 8658564 : STRIP_TYPE_NOPS (value);
19087 : :
19088 : 11286343 : if (! processing_template_decl)
19089 : : {
19090 : : /* Validate and default VALUE. */
19091 : 10913152 : if (value != NULL_TREE)
19092 : : {
19093 : 4930235 : if (!ENUM_UNDERLYING_TYPE (enumtype))
19094 : : {
19095 : 2644206 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19096 : : value, true);
19097 : 2644206 : if (tmp_value)
19098 : 4930235 : value = tmp_value;
19099 : : }
19100 : 2286029 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19101 : : (TREE_TYPE (value)))
19102 : 39 : value = perform_implicit_conversion_flags
19103 : 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19104 : : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19105 : :
19106 : 4930235 : if (value == error_mark_node)
19107 : : value = NULL_TREE;
19108 : :
19109 : 4930214 : if (value != NULL_TREE)
19110 : : {
19111 : 4930214 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19112 : : (TREE_TYPE (value)))
19113 : : {
19114 : 17 : error_at (cp_expr_loc_or_input_loc (value),
19115 : : "enumerator value for %qD must have integral or "
19116 : : "unscoped enumeration type", name);
19117 : 17 : value = NULL_TREE;
19118 : : }
19119 : : else
19120 : : {
19121 : 4930197 : value = cxx_constant_value (value);
19122 : :
19123 : 4930197 : if (TREE_CODE (value) != INTEGER_CST)
19124 : : {
19125 : 46 : error ("enumerator value for %qD is not an integer "
19126 : : "constant", name);
19127 : 46 : value = NULL_TREE;
19128 : : }
19129 : : }
19130 : : }
19131 : : }
19132 : :
19133 : : /* Default based on previous value. */
19134 : 63 : if (value == NULL_TREE)
19135 : : {
19136 : 5983001 : if (TYPE_VALUES (enumtype))
19137 : : {
19138 : 5519916 : tree prev_value;
19139 : :
19140 : : /* C++03 7.2/4: If no initializer is specified for the first
19141 : : enumerator, the type is an unspecified integral
19142 : : type. Otherwise the type is the same as the type of the
19143 : : initializing value of the preceding enumerator unless the
19144 : : incremented value is not representable in that type, in
19145 : : which case the type is an unspecified integral type
19146 : : sufficient to contain the incremented value. */
19147 : 5519916 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19148 : 5519916 : if (TREE_CODE (prev_value) != INTEGER_CST)
19149 : 2 : value = error_mark_node;
19150 : : else
19151 : : {
19152 : 5519914 : wi::overflow_type overflowed;
19153 : 5519914 : tree type = TREE_TYPE (prev_value);
19154 : 5519914 : signop sgn = TYPE_SIGN (type);
19155 : 5519914 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
19156 : 5519914 : &overflowed);
19157 : 5519914 : if (!overflowed)
19158 : : {
19159 : 5519914 : bool pos = !wi::neg_p (wi, sgn);
19160 : 5519914 : if (!wi::fits_to_tree_p (wi, type))
19161 : : {
19162 : : unsigned int itk;
19163 : 53 : for (itk = itk_int; itk != itk_none; itk++)
19164 : : {
19165 : 53 : type = integer_types[itk];
19166 : 53 : if (type != NULL_TREE
19167 : 53 : && (pos || !TYPE_UNSIGNED (type))
19168 : 106 : && wi::fits_to_tree_p (wi, type))
19169 : : break;
19170 : : }
19171 : 17 : if (type && cxx_dialect < cxx11
19172 : 6 : && itk > itk_unsigned_long)
19173 : 3 : pedwarn (input_location, OPT_Wlong_long,
19174 : : pos ? G_("\
19175 : : incremented enumerator value is too large for %<unsigned long%>") : G_("\
19176 : : incremented enumerator value is too large for %<long%>"));
19177 : : }
19178 : 5519914 : if (type == NULL_TREE)
19179 : 0 : overflowed = wi::OVF_UNKNOWN;
19180 : : else
19181 : 5519914 : value = wide_int_to_tree (type, wi);
19182 : : }
19183 : :
19184 : 5519914 : if (overflowed)
19185 : : {
19186 : 0 : error ("overflow in enumeration values at %qD", name);
19187 : 0 : value = error_mark_node;
19188 : : }
19189 : 5519914 : }
19190 : : }
19191 : : else
19192 : 463085 : value = integer_zero_node;
19193 : : }
19194 : :
19195 : : /* Remove no-op casts from the value. */
19196 : 10913152 : STRIP_TYPE_NOPS (value);
19197 : :
19198 : : /* If the underlying type of the enum is fixed, check whether
19199 : : the enumerator values fits in the underlying type. If it
19200 : : does not fit, the program is ill-formed [C++0x dcl.enum]. */
19201 : 10913152 : if (ENUM_UNDERLYING_TYPE (enumtype)
19202 : : && value
19203 : 10913152 : && TREE_CODE (value) == INTEGER_CST)
19204 : : {
19205 : 3239514 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19206 : 9 : error ("enumerator value %qE is outside the range of underlying "
19207 : 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19208 : :
19209 : : /* Convert the value to the appropriate type. */
19210 : 3239514 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19211 : : }
19212 : : }
19213 : :
19214 : : /* C++ associates enums with global, function, or class declarations. */
19215 : 11286343 : context = current_scope ();
19216 : :
19217 : : /* Build the actual enumeration constant. Note that the enumeration
19218 : : constants have the underlying type of the enum (if it is fixed)
19219 : : or the type of their initializer (if the underlying type of the
19220 : : enum is not fixed):
19221 : :
19222 : : [ C++0x dcl.enum ]
19223 : :
19224 : : If the underlying type is fixed, the type of each enumerator
19225 : : prior to the closing brace is the underlying type; if the
19226 : : initializing value of an enumerator cannot be represented by
19227 : : the underlying type, the program is ill-formed. If the
19228 : : underlying type is not fixed, the type of each enumerator is
19229 : : the type of its initializing value.
19230 : :
19231 : : If the underlying type is not fixed, it will be computed by
19232 : : finish_enum and we will reset the type of this enumerator. Of
19233 : : course, if we're processing a template, there may be no value. */
19234 : 11286343 : type = value ? TREE_TYPE (value) : NULL_TREE;
19235 : :
19236 : 11286343 : decl = build_decl (loc, CONST_DECL, name, type);
19237 : :
19238 : 11286343 : DECL_CONTEXT (decl) = enumtype;
19239 : 11286343 : TREE_CONSTANT (decl) = 1;
19240 : 11286343 : TREE_READONLY (decl) = 1;
19241 : 11286343 : DECL_INITIAL (decl) = value;
19242 : :
19243 : 11286343 : if (attributes)
19244 : 127809 : cplus_decl_attributes (&decl, attributes, 0);
19245 : :
19246 : 11286343 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19247 : : {
19248 : : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19249 : : on the TYPE_FIELDS list for `S'. (That's so that you can say
19250 : : things like `S::i' later.) */
19251 : :
19252 : : /* The enumerator may be getting declared outside of its enclosing
19253 : : class, like so:
19254 : :
19255 : : class S { public: enum E : int; }; enum S::E : int { i = 7; };
19256 : :
19257 : : For which case we need to make sure that the access of `S::i'
19258 : : matches the access of `S::E'. */
19259 : 2006433 : auto cas = make_temp_override (current_access_specifier);
19260 : 2006433 : set_current_access_from_decl (TYPE_NAME (enumtype));
19261 : 2006433 : finish_member_declaration (decl);
19262 : 2006433 : }
19263 : : else
19264 : 9279910 : pushdecl (decl);
19265 : :
19266 : : /* Add this enumeration constant to the list for this type. */
19267 : 11286343 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19268 : :
19269 : 11286343 : return decl;
19270 : : }
19271 : :
19272 : : /* Look for an enumerator with the given NAME within the enumeration
19273 : : type ENUMTYPE. This routine is used primarily for qualified name
19274 : : lookup into an enumerator in C++0x, e.g.,
19275 : :
19276 : : enum class Color { Red, Green, Blue };
19277 : :
19278 : : Color color = Color::Red;
19279 : :
19280 : : Returns the value corresponding to the enumerator, or
19281 : : NULL_TREE if no such enumerator was found. */
19282 : : tree
19283 : 12240250 : lookup_enumerator (tree enumtype, tree name)
19284 : : {
19285 : 12240250 : tree e;
19286 : 12240250 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19287 : :
19288 : 12240250 : e = purpose_member (name, TYPE_VALUES (enumtype));
19289 : 24480403 : return e? TREE_VALUE (e) : NULL_TREE;
19290 : : }
19291 : :
19292 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19293 : :
19294 : : tree
19295 : 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
19296 : : vec<string_int_pair> *values)
19297 : : {
19298 : 0 : location_t saved_loc = input_location;
19299 : 0 : input_location = loc;
19300 : :
19301 : 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19302 : : NULL_TREE, false, NULL);
19303 : 0 : if (!OPAQUE_ENUM_P (enumtype))
19304 : : {
19305 : 0 : auto_diagnostic_group d;
19306 : 0 : error_at (loc, "multiple definition of %q#T", enumtype);
19307 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19308 : : "previous definition here");
19309 : 0 : return enumtype;
19310 : 0 : }
19311 : 0 : SET_OPAQUE_ENUM_P (enumtype, false);
19312 : 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19313 : :
19314 : 0 : for (const string_int_pair &value : values)
19315 : 0 : build_enumerator (get_identifier (value.first),
19316 : 0 : build_int_cst (integer_type_node, value.second),
19317 : : enumtype, NULL_TREE, loc);
19318 : :
19319 : 0 : finish_enum_value_list (enumtype);
19320 : 0 : finish_enum (enumtype);
19321 : :
19322 : 0 : input_location = saved_loc;
19323 : 0 : return enumtype;
19324 : : }
19325 : :
19326 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19327 : :
19328 : : tree
19329 : 0 : cxx_simulate_record_decl (location_t loc, const char *name,
19330 : : array_slice<const tree> fields)
19331 : : {
19332 : 0 : iloc_sentinel ils (loc);
19333 : :
19334 : 0 : tree ident = get_identifier (name);
19335 : 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
19336 : 0 : if (type != error_mark_node
19337 : 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19338 : : {
19339 : 0 : error ("redefinition of %q#T", type);
19340 : 0 : type = error_mark_node;
19341 : : }
19342 : 0 : if (type == error_mark_node)
19343 : 0 : return lhd_simulate_record_decl (loc, name, fields);
19344 : :
19345 : 0 : xref_basetypes (type, NULL_TREE);
19346 : 0 : type = begin_class_definition (type);
19347 : 0 : if (type == error_mark_node)
19348 : 0 : return lhd_simulate_record_decl (loc, name, fields);
19349 : :
19350 : 0 : for (tree field : fields)
19351 : 0 : finish_member_declaration (field);
19352 : :
19353 : 0 : type = finish_struct (type, NULL_TREE);
19354 : :
19355 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
19356 : 0 : set_underlying_type (decl);
19357 : 0 : lang_hooks.decls.pushdecl (decl);
19358 : :
19359 : 0 : return type;
19360 : 0 : }
19361 : :
19362 : : /* We're defining DECL. Make sure that its type is OK. */
19363 : :
19364 : : static void
19365 : 147823538 : check_function_type (tree decl, tree current_function_parms)
19366 : : {
19367 : 147823538 : tree fntype = TREE_TYPE (decl);
19368 : 147823538 : tree return_type = complete_type (TREE_TYPE (fntype));
19369 : :
19370 : : /* In a function definition, arg types must be complete. */
19371 : 147823538 : require_complete_types_for_parms (current_function_parms);
19372 : :
19373 : 147823538 : if (dependent_type_p (return_type)
19374 : 147823538 : || type_uses_auto (return_type))
19375 : 44687950 : return;
19376 : 103135588 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19377 : : {
19378 : 12 : tree args = TYPE_ARG_TYPES (fntype);
19379 : :
19380 : 12 : error ("return type %q#T is incomplete", return_type);
19381 : :
19382 : : /* Make it return void instead. */
19383 : 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
19384 : 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19385 : : void_type_node,
19386 : 3 : TREE_CHAIN (args));
19387 : : else
19388 : 18 : fntype = build_function_type (void_type_node, args,
19389 : 9 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19390 : 12 : fntype = (cp_build_type_attribute_variant
19391 : 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19392 : 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19393 : 12 : TREE_TYPE (decl) = fntype;
19394 : : }
19395 : : else
19396 : : {
19397 : 103135576 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
19398 : 103135576 : maybe_warn_parm_abi (TREE_TYPE (fntype),
19399 : 103135576 : DECL_SOURCE_LOCATION (decl));
19400 : : }
19401 : : }
19402 : :
19403 : : /* True iff FN is an implicitly-defined default constructor. */
19404 : :
19405 : : static bool
19406 : 14931806 : implicit_default_ctor_p (tree fn)
19407 : : {
19408 : 14931806 : return (DECL_CONSTRUCTOR_P (fn)
19409 : 14931806 : && !user_provided_p (fn)
19410 : 16993338 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19411 : : }
19412 : :
19413 : : /* Clobber the contents of *this to let the back end know that the object
19414 : : storage is dead when we enter the constructor or leave the destructor. */
19415 : :
19416 : : static tree
19417 : 1370605 : build_clobber_this (clobber_kind kind)
19418 : : {
19419 : : /* Clobbering an empty base is pointless, and harmful if its one byte
19420 : : TYPE_SIZE overlays real data. */
19421 : 1370605 : if (is_empty_class (current_class_type))
19422 : 0 : return void_node;
19423 : :
19424 : : /* If we have virtual bases, clobber the whole object, but only if we're in
19425 : : charge. If we don't have virtual bases, clobber the as-base type so we
19426 : : don't mess with tail padding. */
19427 : 1370605 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19428 : :
19429 : 1370605 : tree ctype = current_class_type;
19430 : 1370605 : if (!vbases)
19431 : 1361876 : ctype = CLASSTYPE_AS_BASE (ctype);
19432 : :
19433 : 1370605 : tree clobber = build_clobber (ctype, kind);
19434 : :
19435 : 1370605 : tree thisref = current_class_ref;
19436 : 1370605 : if (ctype != current_class_type)
19437 : : {
19438 : 135878 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19439 : 135878 : thisref = convert_from_reference (thisref);
19440 : : }
19441 : :
19442 : 1370605 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19443 : 1370605 : if (kind == CLOBBER_OBJECT_BEGIN)
19444 : 0 : TREE_SET_CODE (exprstmt, INIT_EXPR);
19445 : 1370605 : if (vbases)
19446 : 8729 : exprstmt = build_if_in_charge (exprstmt);
19447 : :
19448 : : return exprstmt;
19449 : : }
19450 : :
19451 : : /* Create the FUNCTION_DECL for a function definition.
19452 : : DECLSPECS and DECLARATOR are the parts of the declaration;
19453 : : they describe the function's name and the type it returns,
19454 : : but twisted together in a fashion that parallels the syntax of C.
19455 : :
19456 : : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19457 : : DECLARATOR is really the DECL for the function we are about to
19458 : : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19459 : : indicating that the function is an inline defined in-class.
19460 : :
19461 : : This function creates a binding context for the function body
19462 : : as well as setting up the FUNCTION_DECL in current_function_decl.
19463 : :
19464 : : For C++, we must first check whether that datum makes any sense.
19465 : : For example, "class A local_a(1,2);" means that variable local_a
19466 : : is an aggregate of type A, which should have a constructor
19467 : : applied to it with the argument list [1, 2].
19468 : :
19469 : : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19470 : : or may be a BLOCK if the function has been defined previously
19471 : : in this translation unit. On exit, DECL_INITIAL (decl1) will be
19472 : : error_mark_node if the function has never been defined, or
19473 : : a BLOCK if the function has been defined somewhere. */
19474 : :
19475 : : bool
19476 : 147823574 : start_preparsed_function (tree decl1, tree attrs, int flags)
19477 : : {
19478 : 147823574 : tree ctype = NULL_TREE;
19479 : 147823574 : bool doing_friend = false;
19480 : :
19481 : : /* Sanity check. */
19482 : 147823574 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19483 : 147823574 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19484 : :
19485 : 147823574 : tree fntype = TREE_TYPE (decl1);
19486 : 147823574 : if (DECL_CLASS_SCOPE_P (decl1))
19487 : 107103295 : ctype = DECL_CONTEXT (decl1);
19488 : : else
19489 : : {
19490 : 81440558 : ctype = DECL_FRIEND_CONTEXT (decl1);
19491 : :
19492 : 40720278 : if (ctype)
19493 : 147823574 : doing_friend = true;
19494 : : }
19495 : :
19496 : : /* Adjust for #pragma target/optimize if this is an artificial function that
19497 : : (probably) didn't go through grokfndecl. We particularly don't want this
19498 : : for deferred instantiations, which should match their template. */
19499 : 147823574 : if (DECL_ARTIFICIAL (decl1))
19500 : 3194536 : decl_attributes (&decl1, NULL_TREE, 0);
19501 : :
19502 : 147823574 : if (DECL_DECLARED_INLINE_P (decl1)
19503 : 147823574 : && lookup_attribute ("noinline", attrs))
19504 : : {
19505 : 0 : auto_urlify_attributes sentinel;
19506 : 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
19507 : : "inline function %qD given attribute %qs", decl1, "noinline");
19508 : 0 : }
19509 : :
19510 : : /* Handle gnu_inline attribute. */
19511 : 147823574 : if (GNU_INLINE_P (decl1))
19512 : : {
19513 : 1752135 : DECL_EXTERNAL (decl1) = 1;
19514 : 1752135 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
19515 : 1752135 : DECL_INTERFACE_KNOWN (decl1) = 1;
19516 : 1752135 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
19517 : : }
19518 : :
19519 : 147823574 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
19520 : : /* This is a constructor, we must ensure that any default args
19521 : : introduced by this definition are propagated to the clones
19522 : : now. The clones are used directly in overload resolution. */
19523 : 19159313 : adjust_clone_args (decl1);
19524 : :
19525 : : /* Sometimes we don't notice that a function is a static member, and
19526 : : build a METHOD_TYPE for it. Fix that up now. */
19527 : 147823574 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
19528 : : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
19529 : :
19530 : : /* Set up current_class_type, and enter the scope of the class, if
19531 : : appropriate. */
19532 : 147823574 : if (ctype)
19533 : 110323246 : push_nested_class (ctype);
19534 : :
19535 : : /* Now that we have entered the scope of the class, we must restore
19536 : : the bindings for any template parameters surrounding DECL1, if it
19537 : : is an inline member template. (Order is important; consider the
19538 : : case where a template parameter has the same name as a field of
19539 : : the class.) It is not until after this point that
19540 : : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19541 : 147823574 : if (flags & SF_INCLASS_INLINE)
19542 : 72672773 : maybe_begin_member_template_processing (decl1);
19543 : :
19544 : : /* Effective C++ rule 15. */
19545 : 147823574 : if (warn_ecpp
19546 : 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19547 : 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19548 : 147823604 : && VOID_TYPE_P (TREE_TYPE (fntype)))
19549 : 0 : warning (OPT_Weffc__,
19550 : : "%<operator=%> should return a reference to %<*this%>");
19551 : :
19552 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
19553 : : error_mark_node is replaced below (in poplevel) with the BLOCK. */
19554 : 147823574 : if (!DECL_INITIAL (decl1))
19555 : 31300130 : DECL_INITIAL (decl1) = error_mark_node;
19556 : :
19557 : : /* This function exists in static storage.
19558 : : (This does not mean `static' in the C sense!) */
19559 : 147823574 : TREE_STATIC (decl1) = 1;
19560 : :
19561 : : /* We must call push_template_decl after current_class_type is set
19562 : : up. (If we are processing inline definitions after exiting a
19563 : : class scope, current_class_type will be NULL_TREE until set above
19564 : : by push_nested_class.) */
19565 : 147823574 : if (processing_template_decl)
19566 : : {
19567 : 88244877 : tree newdecl1 = push_template_decl (decl1, doing_friend);
19568 : 88244877 : if (newdecl1 == error_mark_node)
19569 : : {
19570 : 36 : if (ctype)
19571 : 9 : pop_nested_class ();
19572 : 36 : return false;
19573 : : }
19574 : 88244841 : decl1 = newdecl1;
19575 : : }
19576 : :
19577 : : /* Make sure the parameter and return types are reasonable. When
19578 : : you declare a function, these types can be incomplete, but they
19579 : : must be complete when you define the function. */
19580 : 147823538 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
19581 : :
19582 : : /* Build the return declaration for the function. */
19583 : 147823538 : tree restype = TREE_TYPE (fntype);
19584 : :
19585 : 147823538 : if (DECL_RESULT (decl1) == NULL_TREE)
19586 : : {
19587 : : /* In a template instantiation, copy the return type location. When
19588 : : parsing, the location will be set in grokdeclarator. */
19589 : 59360930 : location_t loc = input_location;
19590 : 59360930 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
19591 : : {
19592 : 28097868 : tree tmpl = template_for_substitution (decl1);
19593 : 28097868 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
19594 : 26968879 : loc = DECL_SOURCE_LOCATION (res);
19595 : : }
19596 : :
19597 : 59360930 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
19598 : 59360930 : DECL_ARTIFICIAL (resdecl) = 1;
19599 : 59360930 : DECL_IGNORED_P (resdecl) = 1;
19600 : 59360930 : DECL_RESULT (decl1) = resdecl;
19601 : :
19602 : 59360930 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
19603 : : }
19604 : :
19605 : : /* Record the decl so that the function name is defined.
19606 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
19607 : : use the old decl. */
19608 : 147823538 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
19609 : : {
19610 : : /* A specialization is not used to guide overload resolution. */
19611 : 20690570 : if (!DECL_FUNCTION_MEMBER_P (decl1)
19612 : 20676963 : && !(DECL_USE_TEMPLATE (decl1) &&
19613 : 5246 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
19614 : : {
19615 : 9933132 : tree olddecl = pushdecl (decl1);
19616 : :
19617 : 9933132 : if (olddecl == error_mark_node)
19618 : : /* If something went wrong when registering the declaration,
19619 : : use DECL1; we have to have a FUNCTION_DECL to use when
19620 : : parsing the body of the function. */
19621 : : ;
19622 : : else
19623 : : {
19624 : : /* Otherwise, OLDDECL is either a previous declaration
19625 : : of the same function or DECL1 itself. */
19626 : :
19627 : 9932922 : if (warn_missing_declarations
19628 : 21 : && olddecl == decl1
19629 : 15 : && !DECL_MAIN_P (decl1)
19630 : 15 : && TREE_PUBLIC (decl1)
19631 : 9932934 : && !DECL_DECLARED_INLINE_P (decl1))
19632 : : {
19633 : 9 : tree context;
19634 : :
19635 : : /* Check whether DECL1 is in an anonymous
19636 : : namespace. */
19637 : 9 : for (context = DECL_CONTEXT (decl1);
19638 : 18 : context;
19639 : 9 : context = DECL_CONTEXT (context))
19640 : : {
19641 : 12 : if (TREE_CODE (context) == NAMESPACE_DECL
19642 : 12 : && DECL_NAME (context) == NULL_TREE)
19643 : : break;
19644 : : }
19645 : :
19646 : 9 : if (context == NULL)
19647 : 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
19648 : 6 : OPT_Wmissing_declarations,
19649 : : "no previous declaration for %qD", decl1);
19650 : : }
19651 : :
19652 : 9932922 : decl1 = olddecl;
19653 : : }
19654 : : }
19655 : : else
19656 : : {
19657 : : /* We need to set the DECL_CONTEXT. */
19658 : 805453 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
19659 : 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
19660 : : }
19661 : 10738585 : fntype = TREE_TYPE (decl1);
19662 : 10738585 : restype = TREE_TYPE (fntype);
19663 : :
19664 : : /* If #pragma weak applies, mark the decl appropriately now.
19665 : : The pragma only applies to global functions. Because
19666 : : determining whether or not the #pragma applies involves
19667 : : computing the mangled name for the declaration, we cannot
19668 : : apply the pragma until after we have merged this declaration
19669 : : with any previous declarations; if the original declaration
19670 : : has a linkage specification, that specification applies to
19671 : : the definition as well, and may affect the mangled name. */
19672 : 10738585 : if (DECL_FILE_SCOPE_P (decl1))
19673 : 2541351 : maybe_apply_pragma_weak (decl1);
19674 : : }
19675 : :
19676 : : /* We are now in the scope of the function being defined. */
19677 : 147823538 : current_function_decl = decl1;
19678 : :
19679 : : /* Save the parm names or decls from this function's declarator
19680 : : where store_parm_decls will find them. */
19681 : 147823538 : tree current_function_parms = DECL_ARGUMENTS (decl1);
19682 : :
19683 : : /* Let the user know we're compiling this function. */
19684 : 147823538 : announce_function (decl1);
19685 : :
19686 : 147823538 : gcc_assert (DECL_INITIAL (decl1));
19687 : :
19688 : : /* This function may already have been parsed, in which case just
19689 : : return; our caller will skip over the body without parsing. */
19690 : 147823538 : if (DECL_INITIAL (decl1) != error_mark_node)
19691 : : return true;
19692 : :
19693 : : /* Initialize RTL machinery. We cannot do this until
19694 : : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
19695 : : even when processing a template; this is how we get
19696 : : CFUN set up, and our per-function variables initialized.
19697 : : FIXME factor out the non-RTL stuff. */
19698 : 147823538 : cp_binding_level *bl = current_binding_level;
19699 : 147823538 : allocate_struct_function (decl1, processing_template_decl);
19700 : :
19701 : : /* Initialize the language data structures. Whenever we start
19702 : : a new function, we destroy temporaries in the usual way. */
19703 : 147823538 : cfun->language = ggc_cleared_alloc<language_function> ();
19704 : 147823538 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
19705 : 147823538 : current_binding_level = bl;
19706 : :
19707 : : /* If we are (erroneously) defining a function that we have already
19708 : : defined before, wipe out what we knew before. */
19709 : 147823538 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
19710 : 147823538 : FNDECL_USED_AUTO (decl1) = false;
19711 : 147823538 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
19712 : :
19713 : 147823538 : if (!processing_template_decl && type_uses_auto (restype))
19714 : : {
19715 : 934084 : FNDECL_USED_AUTO (decl1) = true;
19716 : 934084 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
19717 : : }
19718 : :
19719 : : /* Start the statement-tree, start the tree now. */
19720 : 147823538 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
19721 : :
19722 : 147823538 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
19723 : : {
19724 : : /* We know that this was set up by `grokclassfn'. We do not
19725 : : wait until `store_parm_decls', since evil parse errors may
19726 : : never get us to that point. Here we keep the consistency
19727 : : between `current_class_type' and `current_class_ptr'. */
19728 : 95029232 : tree t = DECL_ARGUMENTS (decl1);
19729 : :
19730 : 95029232 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
19731 : 95029232 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
19732 : :
19733 : 95029232 : cp_function_chain->x_current_class_ref
19734 : 95029232 : = cp_build_fold_indirect_ref (t);
19735 : : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
19736 : 95029232 : cp_function_chain->x_current_class_ptr = t;
19737 : :
19738 : : /* Constructors and destructors need to know whether they're "in
19739 : : charge" of initializing virtual base classes. */
19740 : 95029232 : t = DECL_CHAIN (t);
19741 : 95029232 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
19742 : : {
19743 : 20037 : current_in_charge_parm = t;
19744 : 20037 : t = DECL_CHAIN (t);
19745 : : }
19746 : 95029232 : if (DECL_HAS_VTT_PARM_P (decl1))
19747 : : {
19748 : 40117 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
19749 : 40117 : current_vtt_parm = t;
19750 : : }
19751 : : }
19752 : :
19753 : 147823538 : bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
19754 : : /* Implicitly-defined methods (like the
19755 : : destructor for a class in which no destructor
19756 : : is explicitly declared) must not be defined
19757 : : until their definition is needed. So, we
19758 : : ignore interface specifications for
19759 : : compiler-generated functions. */
19760 : 147823538 : && !DECL_ARTIFICIAL (decl1));
19761 : 147823538 : struct c_fileinfo *finfo
19762 : 147823538 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
19763 : :
19764 : 147823538 : if (processing_template_decl)
19765 : : /* Don't mess with interface flags. */;
19766 : 59578697 : else if (DECL_INTERFACE_KNOWN (decl1))
19767 : : {
19768 : 18256916 : tree ctx = decl_function_context (decl1);
19769 : :
19770 : 18256916 : if (DECL_NOT_REALLY_EXTERN (decl1))
19771 : 14603659 : DECL_EXTERNAL (decl1) = 0;
19772 : :
19773 : 18256916 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
19774 : : /* This is a function in a local class in an extern inline
19775 : : or template function. */
19776 : 2963807 : comdat_linkage (decl1);
19777 : : }
19778 : : /* If this function belongs to an interface, it is public.
19779 : : If it belongs to someone else's interface, it is also external.
19780 : : This only affects inlines and template instantiations. */
19781 : 41321781 : else if (!finfo->interface_unknown && honor_interface)
19782 : : {
19783 : 112 : if (DECL_DECLARED_INLINE_P (decl1)
19784 : 112 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19785 : : {
19786 : 88 : DECL_EXTERNAL (decl1)
19787 : 176 : = (finfo->interface_only
19788 : 88 : || (DECL_DECLARED_INLINE_P (decl1)
19789 : 42 : && ! flag_implement_inlines
19790 : 0 : && !DECL_VINDEX (decl1)));
19791 : :
19792 : : /* For WIN32 we also want to put these in linkonce sections. */
19793 : 88 : maybe_make_one_only (decl1);
19794 : : }
19795 : : else
19796 : 24 : DECL_EXTERNAL (decl1) = 0;
19797 : 112 : DECL_INTERFACE_KNOWN (decl1) = 1;
19798 : : /* If this function is in an interface implemented in this file,
19799 : : make sure that the back end knows to emit this function
19800 : : here. */
19801 : 112 : if (!DECL_EXTERNAL (decl1))
19802 : 66 : mark_needed (decl1);
19803 : : }
19804 : 41321669 : else if (finfo->interface_unknown && finfo->interface_only
19805 : 0 : && honor_interface)
19806 : : {
19807 : : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
19808 : : interface, we will have both finfo->interface_unknown and
19809 : : finfo->interface_only set. In that case, we don't want to
19810 : : use the normal heuristics because someone will supply a
19811 : : #pragma implementation elsewhere, and deducing it here would
19812 : : produce a conflict. */
19813 : 0 : comdat_linkage (decl1);
19814 : 0 : DECL_EXTERNAL (decl1) = 0;
19815 : 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
19816 : 0 : DECL_DEFER_OUTPUT (decl1) = 1;
19817 : : }
19818 : : else
19819 : : {
19820 : : /* This is a definition, not a reference.
19821 : : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
19822 : 41321669 : if (!GNU_INLINE_P (decl1))
19823 : 41321666 : DECL_EXTERNAL (decl1) = 0;
19824 : :
19825 : 41321669 : if ((DECL_DECLARED_INLINE_P (decl1)
19826 : 2105312 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19827 : 43191327 : && ! DECL_INTERFACE_KNOWN (decl1))
19828 : 41086015 : DECL_DEFER_OUTPUT (decl1) = 1;
19829 : : else
19830 : 235654 : DECL_INTERFACE_KNOWN (decl1) = 1;
19831 : : }
19832 : :
19833 : : /* Determine the ELF visibility attribute for the function. We must not
19834 : : do this before calling "pushdecl", as we must allow "duplicate_decls"
19835 : : to merge any attributes appropriately. We also need to wait until
19836 : : linkage is set. */
19837 : 147823538 : if (!DECL_CLONED_FUNCTION_P (decl1))
19838 : 133309609 : determine_visibility (decl1);
19839 : :
19840 : 147823538 : if (!processing_template_decl)
19841 : 59578697 : maybe_instantiate_noexcept (decl1);
19842 : :
19843 : 147823538 : begin_scope (sk_function_parms, decl1);
19844 : :
19845 : 147823538 : ++function_depth;
19846 : :
19847 : 147823538 : start_fname_decls ();
19848 : :
19849 : 147823538 : store_parm_decls (current_function_parms);
19850 : :
19851 : 147823538 : start_function_contracts (decl1);
19852 : :
19853 : 147823538 : if (!processing_template_decl
19854 : 59578697 : && flag_lifetime_dse > 1
19855 : 119137262 : && DECL_CONSTRUCTOR_P (decl1)
19856 : : /* Clobbering an empty base is harmful if it overlays real data. */
19857 : 16617500 : && !is_empty_class (current_class_type)
19858 : : /* We can't clobber safely for an implicitly-defined default constructor
19859 : : because part of the initialization might happen before we enter the
19860 : : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
19861 : 14931170 : && !implicit_default_ctor_p (decl1)
19862 : 161410875 : && !lookup_attribute ("clobber *this",
19863 : 13587337 : DECL_ATTRIBUTES (current_class_ptr)))
19864 : 13578881 : DECL_ATTRIBUTES (current_class_ptr)
19865 : 27157762 : = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
19866 : 13578881 : DECL_ATTRIBUTES (current_class_ptr));
19867 : :
19868 : 147823538 : if (!processing_template_decl
19869 : 119157394 : && DECL_CONSTRUCTOR_P (decl1)
19870 : 16620152 : && sanitize_flags_p (SANITIZE_VPTR)
19871 : 1908 : && !DECL_CLONED_FUNCTION_P (decl1)
19872 : 147824174 : && !implicit_default_ctor_p (decl1))
19873 : 480 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
19874 : :
19875 : 147823538 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
19876 : 147822970 : start_lambda_scope (decl1);
19877 : :
19878 : : return true;
19879 : : }
19880 : :
19881 : :
19882 : : /* Like start_preparsed_function, except that instead of a
19883 : : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
19884 : :
19885 : : Returns true on success. If the DECLARATOR is not suitable
19886 : : for a function, we return false, which tells the parser to
19887 : : skip the entire function. */
19888 : :
19889 : : bool
19890 : 39677741 : start_function (cp_decl_specifier_seq *declspecs,
19891 : : const cp_declarator *declarator,
19892 : : tree attrs)
19893 : : {
19894 : 39677741 : tree decl1;
19895 : :
19896 : 39677741 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
19897 : 39677741 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
19898 : 39677741 : if (decl1 == error_mark_node)
19899 : : return false;
19900 : :
19901 : 39677450 : if (DECL_MAIN_P (decl1))
19902 : : /* main must return int. grokfndecl should have corrected it
19903 : : (and issued a diagnostic) if the user got it wrong. */
19904 : 33309 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
19905 : : integer_type_node));
19906 : :
19907 : 39677450 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
19908 : : }
19909 : :
19910 : : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
19911 : : FN. */
19912 : :
19913 : : bool
19914 : 282884811 : use_eh_spec_block (tree fn)
19915 : : {
19916 : 280975598 : return (flag_exceptions && flag_enforce_eh_specs
19917 : 280975598 : && !processing_template_decl
19918 : : /* We insert the EH_SPEC_BLOCK only in the original
19919 : : function; then, it is copied automatically to the
19920 : : clones. */
19921 : 103900239 : && !DECL_CLONED_FUNCTION_P (fn)
19922 : : /* Implicitly-generated constructors and destructors have
19923 : : exception specifications. However, those specifications
19924 : : are the union of the possible exceptions specified by the
19925 : : constructors/destructors for bases and members, so no
19926 : : unallowed exception will ever reach this function. By
19927 : : not creating the EH_SPEC_BLOCK we save a little memory,
19928 : : and we avoid spurious warnings about unreachable
19929 : : code. */
19930 : 89426238 : && !DECL_DEFAULTED_FN (fn)
19931 : 369887489 : && !type_throw_all_p (TREE_TYPE (fn)));
19932 : : }
19933 : :
19934 : : /* Helper function to push ARGS into the current lexical scope. DECL
19935 : : is the function declaration. NONPARMS is used to handle enum
19936 : : constants. */
19937 : :
19938 : : void
19939 : 143981240 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
19940 : : {
19941 : : /* If we're doing semantic analysis, then we'll call pushdecl
19942 : : for each of these. We must do them in reverse order so that
19943 : : they end in the correct forward order. */
19944 : 143981240 : args = nreverse (args);
19945 : :
19946 : 143981240 : tree next;
19947 : 439757554 : for (tree parm = args; parm; parm = next)
19948 : : {
19949 : 295776314 : next = DECL_CHAIN (parm);
19950 : 295776314 : if (TREE_CODE (parm) == PARM_DECL)
19951 : 295776314 : pushdecl (parm);
19952 : 0 : else if (nonparms)
19953 : : {
19954 : : /* If we find an enum constant or a type tag, put it aside for
19955 : : the moment. */
19956 : 0 : TREE_CHAIN (parm) = NULL_TREE;
19957 : 0 : *nonparms = chainon (*nonparms, parm);
19958 : : }
19959 : : }
19960 : :
19961 : : /* Get the decls in their original chain order and record in the
19962 : : function. This is all and only the PARM_DECLs that were
19963 : : pushed into scope by the loop above. */
19964 : 143981240 : DECL_ARGUMENTS (decl) = get_local_decls ();
19965 : 143981240 : }
19966 : :
19967 : : /* Store the parameter declarations into the current function declaration.
19968 : : This is called after parsing the parameter declarations, before
19969 : : digesting the body of the function.
19970 : :
19971 : : Also install to binding contour return value identifier, if any. */
19972 : :
19973 : : static void
19974 : 147823538 : store_parm_decls (tree current_function_parms)
19975 : : {
19976 : 147823538 : tree fndecl = current_function_decl;
19977 : :
19978 : : /* This is a chain of any other decls that came in among the parm
19979 : : declarations. If a parm is declared with enum {foo, bar} x;
19980 : : then CONST_DECLs for foo and bar are put here. */
19981 : 147823538 : tree nonparms = NULL_TREE;
19982 : :
19983 : 147823538 : if (current_function_parms)
19984 : : {
19985 : : /* This case is when the function was defined with an ANSI prototype.
19986 : : The parms already have decls, so we need not do anything here
19987 : : except record them as in effect
19988 : : and complain if any redundant old-style parm decls were written. */
19989 : :
19990 : 141233520 : tree specparms = current_function_parms;
19991 : :
19992 : : /* Must clear this because it might contain TYPE_DECLs declared
19993 : : at class level. */
19994 : 141233520 : current_binding_level->names = NULL;
19995 : :
19996 : 141233520 : do_push_parm_decls (fndecl, specparms, &nonparms);
19997 : : }
19998 : : else
19999 : 6590018 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
20000 : :
20001 : : /* Now store the final chain of decls for the arguments
20002 : : as the decl-chain of the current lexical scope.
20003 : : Put the enumerators in as well, at the front so that
20004 : : DECL_ARGUMENTS is not modified. */
20005 : 147823538 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
20006 : :
20007 : 147823538 : if (use_eh_spec_block (current_function_decl))
20008 : 20038788 : current_eh_spec_block = begin_eh_spec_block ();
20009 : 147823538 : }
20010 : :
20011 : :
20012 : : /* Mark CDTOR's implicit THIS argument for returning, if required by
20013 : : the ABI.. Return the decl for THIS, if it is to be returned, and
20014 : : NULL otherwise. */
20015 : :
20016 : : tree
20017 : 117461586 : maybe_prepare_return_this (tree cdtor)
20018 : : {
20019 : 117461586 : if (targetm.cxx.cdtor_returns_this ())
20020 : 0 : if (tree val = DECL_ARGUMENTS (cdtor))
20021 : : {
20022 : 0 : suppress_warning (val, OPT_Wuse_after_free);
20023 : 0 : return val;
20024 : : }
20025 : :
20026 : : return NULL_TREE;
20027 : : }
20028 : :
20029 : : /* Set the return value of the [cd]tor if the ABI wants that. */
20030 : :
20031 : : void
20032 : 7397519 : maybe_return_this ()
20033 : : {
20034 : 7397519 : if (tree val = maybe_prepare_return_this (current_function_decl))
20035 : : {
20036 : : /* Return the address of the object. */
20037 : 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
20038 : 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
20039 : 0 : DECL_RESULT (current_function_decl), val);
20040 : 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
20041 : 0 : add_stmt (exprstmt);
20042 : : }
20043 : 7397519 : }
20044 : :
20045 : : /* Do all the processing for the beginning of a destructor; set up the
20046 : : vtable pointers and cleanups for bases and members. */
20047 : :
20048 : : static void
20049 : 1564182 : begin_destructor_body (void)
20050 : : {
20051 : 1564182 : tree compound_stmt;
20052 : :
20053 : : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20054 : : issued an error message. We still want to try to process the
20055 : : body of the function, but initialize_vtbl_ptrs will crash if
20056 : : TYPE_BINFO is NULL. */
20057 : 1564182 : if (COMPLETE_TYPE_P (current_class_type))
20058 : : {
20059 : 1564182 : compound_stmt = begin_compound_stmt (0);
20060 : : /* Make all virtual function table pointers in non-virtual base
20061 : : classes point to CURRENT_CLASS_TYPE's virtual function
20062 : : tables. */
20063 : 1564182 : initialize_vtbl_ptrs (current_class_ptr);
20064 : 1564182 : finish_compound_stmt (compound_stmt);
20065 : :
20066 : 1564182 : if (flag_lifetime_dse
20067 : : /* Clobbering an empty base is harmful if it overlays real data. */
20068 : 1564182 : && !is_empty_class (current_class_type))
20069 : : {
20070 : 1370668 : if (sanitize_flags_p (SANITIZE_VPTR)
20071 : 236 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20072 : 1370731 : && TYPE_CONTAINS_VPTR_P (current_class_type))
20073 : : {
20074 : 63 : tree binfo = TYPE_BINFO (current_class_type);
20075 : 63 : tree ref
20076 : 63 : = cp_build_fold_indirect_ref (current_class_ptr);
20077 : :
20078 : 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20079 : 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20080 : 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20081 : : NOP_EXPR, vtbl,
20082 : : tf_warning_or_error);
20083 : : /* If the vptr is shared with some virtual nearly empty base,
20084 : : don't clear it if not in charge, the dtor of the virtual
20085 : : nearly empty base will do that later. */
20086 : 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
20087 : : {
20088 : : tree c = current_class_type;
20089 : 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
20090 : : {
20091 : 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20092 : : {
20093 : 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
20094 : : tf_warning_or_error);
20095 : 36 : stmt = build_if_in_charge (stmt);
20096 : 36 : break;
20097 : : }
20098 : 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20099 : : }
20100 : : }
20101 : 63 : finish_decl_cleanup (NULL_TREE, stmt);
20102 : : }
20103 : : else
20104 : 1370605 : finish_decl_cleanup (NULL_TREE,
20105 : : build_clobber_this (CLOBBER_OBJECT_END));
20106 : : }
20107 : :
20108 : : /* And insert cleanups for our bases and members so that they
20109 : : will be properly destroyed if we throw. */
20110 : 1564182 : push_base_cleanups ();
20111 : : }
20112 : 1564182 : }
20113 : :
20114 : : /* Do the necessary processing for the beginning of a function body, which
20115 : : in this case includes member-initializers, but not the catch clauses of
20116 : : a function-try-block. Currently, this means opening a binding level
20117 : : for the member-initializers (in a ctor), member cleanups (in a dtor),
20118 : : and capture proxies (in a lambda operator()). */
20119 : :
20120 : : tree
20121 : 117362911 : begin_function_body (void)
20122 : : {
20123 : 240000053 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20124 : : return NULL_TREE;
20125 : :
20126 : 23379939 : if (processing_template_decl)
20127 : : /* Do nothing now. */;
20128 : : else
20129 : : /* Always keep the BLOCK node associated with the outermost pair of
20130 : : curly braces of a function. These are needed for correct
20131 : : operation of dwarfout.c. */
20132 : 7602120 : keep_next_level (true);
20133 : :
20134 : 23379939 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
20135 : 23379939 : current_binding_level->artificial = 1;
20136 : :
20137 : 23379939 : if (processing_template_decl)
20138 : : /* Do nothing now. */;
20139 : 15204240 : else if (DECL_DESTRUCTOR_P (current_function_decl))
20140 : 1564182 : begin_destructor_body ();
20141 : :
20142 : : return stmt;
20143 : : }
20144 : :
20145 : : /* Do the processing for the end of a function body. Currently, this means
20146 : : closing out the cleanups for fully-constructed bases and members, and in
20147 : : the case of the destructor, deleting the object if desired. Again, this
20148 : : is only meaningful for [cd]tors, since they are the only functions where
20149 : : there is a significant distinction between the main body and any
20150 : : function catch clauses. Handling, say, main() return semantics here
20151 : : would be wrong, as flowing off the end of a function catch clause for
20152 : : main() would also need to return 0. */
20153 : :
20154 : : void
20155 : 117362887 : finish_function_body (tree compstmt)
20156 : : {
20157 : 117362887 : if (compstmt == NULL_TREE)
20158 : : return;
20159 : :
20160 : : /* Close the block. */
20161 : 23379939 : finish_compound_stmt (compstmt);
20162 : :
20163 : 23379939 : if (processing_template_decl)
20164 : : /* Do nothing now. */;
20165 : 7602120 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
20166 : 7602120 : || DECL_DESTRUCTOR_P (current_function_decl))
20167 : 7100114 : maybe_return_this ();
20168 : : }
20169 : :
20170 : : /* Given a function, returns the BLOCK corresponding to the outermost level
20171 : : of curly braces, skipping the artificial block created for constructor
20172 : : initializers. */
20173 : :
20174 : : tree
20175 : 324546 : outer_curly_brace_block (tree fndecl)
20176 : : {
20177 : 324546 : tree block = DECL_INITIAL (fndecl);
20178 : 324546 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20179 : : return block;
20180 : 1183 : block = BLOCK_SUBBLOCKS (block);
20181 : 1183 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20182 : : return block;
20183 : 0 : block = BLOCK_SUBBLOCKS (block);
20184 : 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20185 : : return block;
20186 : : }
20187 : :
20188 : : /* If FNDECL is a class's key method, add the class to the list of
20189 : : keyed classes that should be emitted. */
20190 : :
20191 : : static void
20192 : 147827484 : record_key_method_defined (tree fndecl)
20193 : : {
20194 : 200625712 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20195 : 95120396 : && DECL_VIRTUAL_P (fndecl)
20196 : 150571717 : && !processing_template_decl)
20197 : : {
20198 : 1392498 : tree fnclass = DECL_CONTEXT (fndecl);
20199 : 1392498 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20200 : : {
20201 : 1612 : tree classdecl = TYPE_NAME (fnclass);
20202 : : /* Classes attached to a named module are already handled. */
20203 : 1612 : if (!DECL_LANG_SPECIFIC (classdecl)
20204 : 1660 : || !DECL_MODULE_ATTACH_P (classdecl))
20205 : 1567 : vec_safe_push (keyed_classes, fnclass);
20206 : : }
20207 : : }
20208 : 147827484 : }
20209 : :
20210 : : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20211 : : of "return *this;" immediately before its location, using FNDECL's
20212 : : first statement (if any) to give the indentation, if appropriate. */
20213 : :
20214 : : static void
20215 : 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20216 : : {
20217 : 18 : location_t indent = UNKNOWN_LOCATION;
20218 : 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20219 : 18 : if (stmts)
20220 : 9 : indent = EXPR_LOCATION (stmts);
20221 : 18 : richloc->add_fixit_insert_formatted ("return *this;",
20222 : : richloc->get_loc (),
20223 : : indent);
20224 : 18 : }
20225 : :
20226 : : /* Finish up a function declaration and compile that function
20227 : : all the way to assembler language output. The free the storage
20228 : : for the function definition. INLINE_P is TRUE if we just
20229 : : finished processing the body of an in-class inline function
20230 : : definition. (This processing will have taken place after the
20231 : : class definition is complete.) */
20232 : :
20233 : : tree
20234 : 147823502 : finish_function (bool inline_p)
20235 : : {
20236 : 147823502 : tree fndecl = current_function_decl;
20237 : 147823502 : tree fntype, ctype = NULL_TREE;
20238 : 147823502 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
20239 : :
20240 : : /* When we get some parse errors, we can end up without a
20241 : : current_function_decl, so cope. */
20242 : 147823502 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
20243 : 0 : return error_mark_node;
20244 : :
20245 : 147823502 : bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
20246 : 147823502 : && !processing_template_decl);
20247 : :
20248 : 147823502 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20249 : 147822934 : finish_lambda_scope ();
20250 : :
20251 : 147823502 : if (c_dialect_objc ())
20252 : 0 : objc_finish_function ();
20253 : :
20254 : 147823502 : record_key_method_defined (fndecl);
20255 : :
20256 : 147823502 : fntype = TREE_TYPE (fndecl);
20257 : :
20258 : : /* TREE_READONLY (fndecl) = 1;
20259 : : This caused &foo to be of type ptr-to-const-function
20260 : : which then got a warning when stored in a ptr-to-function variable. */
20261 : :
20262 : 147823502 : gcc_assert (building_stmt_list_p ());
20263 : : /* The current function is being defined, so its DECL_INITIAL should
20264 : : be set, and unless there's a multiple definition, it should be
20265 : : error_mark_node. */
20266 : 147823502 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20267 : :
20268 : 147823502 : cp_coroutine_transform *coroutine = nullptr;
20269 : 147823502 : if (flag_coroutines
20270 : 58245317 : && !processing_template_decl
20271 : 25903220 : && DECL_COROUTINE_P (fndecl)
20272 : 147825167 : && !DECL_RAMP_FN (fndecl))
20273 : : {
20274 : 1665 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20275 : : && !DECL_DEFAULTED_FN (fndecl));
20276 : 1665 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
20277 : 1665 : if (coroutine && coroutine->cp_valid_coroutine ())
20278 : 1665 : coroutine->apply_transforms ();
20279 : :
20280 : : /* We should handle coroutine IFNs in middle end lowering. */
20281 : 1665 : cfun->coroutine_component = true;
20282 : :
20283 : : /* Do not try to process the ramp's EH unless outlining succeeded. */
20284 : 1665 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
20285 : 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20286 : : (TREE_TYPE (fndecl)),
20287 : 360 : current_eh_spec_block);
20288 : :
20289 : : /* If outlining succeeded, then add contracts handling if needed. */
20290 : 1665 : if (coroutine->cp_valid_coroutine () && do_contracts)
20291 : 6 : maybe_apply_function_contracts (fndecl);
20292 : : }
20293 : : else
20294 : : /* For a cloned function, we've already got all the code we need;
20295 : : there's no need to add any extra bits. */
20296 : 147821837 : if (!DECL_CLONED_FUNCTION_P (fndecl))
20297 : : {
20298 : : /* Make it so that `main' always returns 0 by default. */
20299 : 133307908 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20300 : 133341202 : && !TREE_THIS_VOLATILE (current_function_decl))
20301 : 33294 : finish_return_stmt (integer_zero_node);
20302 : :
20303 : 133307908 : if (use_eh_spec_block (current_function_decl))
20304 : 20038425 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20305 : : (TREE_TYPE (current_function_decl)),
20306 : 20038425 : current_eh_spec_block);
20307 : :
20308 : 133307908 : if (do_contracts)
20309 : 420 : maybe_apply_function_contracts (current_function_decl);
20310 : :
20311 : : }
20312 : :
20313 : : /* If we're saving up tree structure, tie off the function now. */
20314 : 147823502 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20315 : :
20316 : 147823502 : finish_fname_decls ();
20317 : :
20318 : : /* This must come after expand_function_end because cleanups might
20319 : : have declarations (from inline functions) that need to go into
20320 : : this function's blocks. */
20321 : :
20322 : : /* If the current binding level isn't the outermost binding level
20323 : : for this function, either there is a bug, or we have experienced
20324 : : syntax errors and the statement tree is malformed. */
20325 : 147823502 : if (current_binding_level->kind != sk_function_parms)
20326 : : {
20327 : : /* Make sure we have already experienced errors. */
20328 : 12 : gcc_assert (errorcount);
20329 : :
20330 : : /* Throw away the broken statement tree and extra binding
20331 : : levels. */
20332 : 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20333 : :
20334 : 27 : while (current_binding_level->kind != sk_function_parms)
20335 : : {
20336 : 15 : if (current_binding_level->kind == sk_class)
20337 : 0 : pop_nested_class ();
20338 : : else
20339 : 15 : poplevel (0, 0, 0);
20340 : : }
20341 : : }
20342 : 147823502 : poplevel (1, 0, 1);
20343 : :
20344 : : /* Statements should always be full-expressions at the outermost set
20345 : : of curly braces for a function. */
20346 : 147823502 : gcc_assert (stmts_are_full_exprs_p ());
20347 : :
20348 : : /* If there are no return statements in a function with auto return type,
20349 : : the return type is void. But if the declared type is something like
20350 : : auto*, this is an error. */
20351 : 59578661 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20352 : 148757586 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20353 : : {
20354 : 247423 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20355 : 247408 : && !current_function_returns_value
20356 : 494633 : && !current_function_returns_null)
20357 : : {
20358 : : /* We haven't applied return type deduction because we haven't
20359 : : seen any return statements. Do that now. */
20360 : 247210 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20361 : 247210 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20362 : : void_node, node, tf_warning_or_error,
20363 : : adc_return_type);
20364 : :
20365 : 247210 : apply_deduced_return_type (fndecl, void_type_node);
20366 : 247210 : fntype = TREE_TYPE (fndecl);
20367 : : }
20368 : 213 : else if (!current_function_returns_value
20369 : 6 : && !current_function_returns_null)
20370 : : {
20371 : 6 : auto_diagnostic_group d;
20372 : 12 : error ("no return statements in function returning %qT",
20373 : 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20374 : 6 : inform (input_location, "only plain %<auto%> return type can be "
20375 : : "deduced to %<void%>");
20376 : 6 : }
20377 : : }
20378 : :
20379 : 147823502 : if (FNDECL_USED_AUTO (fndecl)
20380 : 147823502 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20381 : 933871 : if (location_t fcloc = failed_completion_location (fndecl))
20382 : : {
20383 : 6 : auto_diagnostic_group adg;
20384 : 6 : if (warning (OPT_Wsfinae_incomplete_,
20385 : : "defining %qD, which previously failed to be deduced "
20386 : : "in a SFINAE context", fndecl)
20387 : 6 : && warn_sfinae_incomplete == 1)
20388 : 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
20389 : : "-Wsfinae-incomplete=2");
20390 : 6 : }
20391 : :
20392 : : /* Remember that we were in class scope. */
20393 : 147823502 : if (current_class_name)
20394 : 110323234 : ctype = current_class_type;
20395 : :
20396 : 147823502 : if (DECL_DELETED_FN (fndecl))
20397 : : {
20398 : 87 : DECL_INITIAL (fndecl) = error_mark_node;
20399 : 87 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
20400 : 87 : goto cleanup;
20401 : : }
20402 : :
20403 : 147823415 : if (flag_openmp)
20404 : 323288 : if (tree attr = lookup_attribute ("omp declare variant base",
20405 : 323288 : DECL_ATTRIBUTES (fndecl)))
20406 : 249 : omp_declare_variant_finalize (fndecl, attr);
20407 : :
20408 : : /* Complain if there's just no return statement. */
20409 : 147823415 : if ((warn_return_type
20410 : 46877 : || (cxx_dialect >= cxx14
20411 : 38884 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20412 : 147793693 : && !VOID_TYPE_P (TREE_TYPE (fntype))
20413 : 91247641 : && !dependent_type_p (TREE_TYPE (fntype))
20414 : 47498813 : && !current_function_returns_value && !current_function_returns_null
20415 : : /* Don't complain if we abort or throw. */
20416 : 69378 : && !current_function_returns_abnormally
20417 : : /* Don't complain if there's an infinite loop. */
20418 : 924 : && !current_function_infinite_loop
20419 : : /* Don't complain if we are declared noreturn. */
20420 : 856 : && !TREE_THIS_VOLATILE (fndecl)
20421 : 850 : && !DECL_NAME (DECL_RESULT (fndecl))
20422 : 850 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20423 : : /* Structor return values (if any) are set by the compiler. */
20424 : 1230 : && !DECL_CONSTRUCTOR_P (fndecl)
20425 : 615 : && !DECL_DESTRUCTOR_P (fndecl)
20426 : 147824030 : && targetm.warn_func_return (fndecl))
20427 : : {
20428 : 609 : gcc_rich_location richloc (input_location);
20429 : : /* Potentially add a "return *this;" fix-it hint for
20430 : : assignment operators. */
20431 : 609 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20432 : : {
20433 : 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20434 : 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
20435 : 32 : && current_class_ref
20436 : 26 : && same_type_ignoring_top_level_qualifiers_p
20437 : 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20438 : 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
20439 : 18 : add_return_star_this_fixit (&richloc, fndecl);
20440 : : }
20441 : 609 : if (cxx_dialect >= cxx14
20442 : 609 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
20443 : 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
20444 : : "returning non-void");
20445 : 607 : else if (warning_at (&richloc, OPT_Wreturn_type,
20446 : : "no return statement in function returning "
20447 : : "non-void"))
20448 : 160 : suppress_warning (fndecl, OPT_Wreturn_type);
20449 : 609 : }
20450 : :
20451 : : /* Lambda closure members are implicitly constexpr if possible. */
20452 : 147823415 : if (cxx_dialect >= cxx17
20453 : 253699359 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20454 : 1399632 : DECL_DECLARED_CONSTEXPR_P (fndecl)
20455 : 1399632 : = ((processing_template_decl
20456 : 607428 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
20457 : 2798278 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20458 : :
20459 : : /* Save constexpr function body before it gets munged by
20460 : : the NRV transformation. */
20461 : 147823415 : maybe_save_constexpr_fundef (fndecl);
20462 : :
20463 : : /* Invoke the pre-genericize plugin before we start munging things. */
20464 : 147823415 : if (!processing_template_decl)
20465 : 59578574 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
20466 : :
20467 : : /* Perform delayed folding before NRV transformation. */
20468 : 147823415 : if (!processing_template_decl
20469 : 119157148 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20470 : 206804387 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20471 : 58980534 : cp_fold_function (fndecl);
20472 : :
20473 : : /* Set up the named return value optimization, if we can. Candidate
20474 : : variables are selected in check_return_expr. */
20475 : 147823415 : if (tree r = current_function_return_value)
20476 : : {
20477 : 37374138 : if (r != error_mark_node)
20478 : 192738 : finalize_nrv (fndecl, r);
20479 : 37374138 : current_function_return_value = NULL_TREE;
20480 : : }
20481 : :
20482 : : /* Must mark the RESULT_DECL as being in this function. */
20483 : 147823415 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
20484 : :
20485 : : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
20486 : : to the FUNCTION_DECL node itself. */
20487 : 147823415 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
20488 : :
20489 : : /* Store the end of the function, so that we get good line number
20490 : : info for the epilogue. */
20491 : 147823415 : cfun->function_end_locus = input_location;
20492 : :
20493 : : /* Complain about parameters that are only set, but never otherwise used. */
20494 : 147823415 : if (warn_unused_but_set_parameter
20495 : 930158 : && !processing_template_decl
20496 : 478954 : && errorcount == unused_but_set_errorcount
20497 : 148302366 : && !DECL_CLONED_FUNCTION_P (fndecl))
20498 : : {
20499 : 374927 : tree decl;
20500 : :
20501 : 374927 : for (decl = DECL_ARGUMENTS (fndecl);
20502 : 1002627 : decl;
20503 : 627700 : decl = DECL_CHAIN (decl))
20504 : 627700 : if (TREE_USED (decl)
20505 : 520473 : && TREE_CODE (decl) == PARM_DECL
20506 : 520473 : && !DECL_READ_P (decl)
20507 : 225 : && DECL_NAME (decl)
20508 : 225 : && !DECL_ARTIFICIAL (decl)
20509 : 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
20510 : 225 : && !DECL_IN_SYSTEM_HEADER (decl)
20511 : 225 : && TREE_TYPE (decl) != error_mark_node
20512 : 225 : && !TYPE_REF_P (TREE_TYPE (decl))
20513 : 627925 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
20514 : 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
20515 : 225 : warning_at (DECL_SOURCE_LOCATION (decl),
20516 : 225 : OPT_Wunused_but_set_parameter_,
20517 : : "parameter %qD set but not used", decl);
20518 : 374927 : unused_but_set_errorcount = errorcount;
20519 : : }
20520 : :
20521 : : /* Complain about locally defined typedefs that are not used in this
20522 : : function. */
20523 : 147823415 : maybe_warn_unused_local_typedefs ();
20524 : :
20525 : : /* Possibly warn about unused parameters. */
20526 : 147823415 : if (warn_unused_parameter
20527 : 660768 : && !processing_template_decl
20528 : 148144968 : && !DECL_CLONED_FUNCTION_P (fndecl))
20529 : 247379 : do_warn_unused_parameter (fndecl);
20530 : :
20531 : : /* Genericize before inlining. */
20532 : 147823415 : if (!processing_template_decl
20533 : 119157148 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20534 : 206804237 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20535 : 58980384 : cp_genericize (fndecl);
20536 : :
20537 : : /* If this function can't throw any exceptions, remember that. */
20538 : 147823415 : if (!processing_template_decl
20539 : 59578574 : && !cp_function_chain->can_throw
20540 : 44884701 : && !flag_non_call_exceptions
20541 : 44876806 : && !decl_replaceable_p (fndecl,
20542 : 44876806 : opt_for_fn (fndecl, flag_semantic_interposition))
20543 : 192444155 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
20544 : 44618371 : TREE_NOTHROW (fndecl) = 1;
20545 : :
20546 : 147823502 : cleanup:
20547 : :
20548 : : /* We're leaving the context of this function, so zap cfun. It's still in
20549 : : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
20550 : 147823502 : set_cfun (NULL);
20551 : 147823502 : current_function_decl = NULL;
20552 : :
20553 : : /* If this is an in-class inline definition, we may have to pop the
20554 : : bindings for the template parameters that we added in
20555 : : maybe_begin_member_template_processing when start_function was
20556 : : called. */
20557 : 147823502 : if (inline_p)
20558 : 72673201 : maybe_end_member_template_processing ();
20559 : :
20560 : : /* Leave the scope of the class. */
20561 : 147823502 : if (ctype)
20562 : 110323234 : pop_nested_class ();
20563 : :
20564 : 147823502 : --function_depth;
20565 : :
20566 : : /* Clean up. */
20567 : 147823502 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
20568 : :
20569 : : /* Build outlined functions for coroutines and contracts. */
20570 : :
20571 : 147823502 : if (coroutine)
20572 : : {
20573 : : /* Emit the resumer and destroyer functions now, providing that we have
20574 : : not encountered some fatal error. */
20575 : 1665 : if (coroutine->cp_valid_coroutine ())
20576 : : {
20577 : 1581 : coroutine->finish_transforms ();
20578 : 1581 : resumer = coroutine->get_resumer ();
20579 : 1581 : destroyer = coroutine->get_destroyer ();
20580 : 1581 : expand_or_defer_fn (resumer);
20581 : 1581 : expand_or_defer_fn (destroyer);
20582 : : }
20583 : 1665 : delete coroutine;
20584 : : }
20585 : :
20586 : : /* If we have used outlined contracts checking functions, build and emit
20587 : : them here. */
20588 : 147823502 : finish_function_contracts (fndecl);
20589 : :
20590 : 147823502 : return fndecl;
20591 : : }
20592 : :
20593 : : /* Create the FUNCTION_DECL for a function definition.
20594 : : DECLSPECS and DECLARATOR are the parts of the declaration;
20595 : : they describe the return type and the name of the function,
20596 : : but twisted together in a fashion that parallels the syntax of C.
20597 : :
20598 : : This function creates a binding context for the function body
20599 : : as well as setting up the FUNCTION_DECL in current_function_decl.
20600 : :
20601 : : Returns a FUNCTION_DECL on success.
20602 : :
20603 : : If the DECLARATOR is not suitable for a function (it defines a datum
20604 : : instead), we return 0, which tells yyparse to report a parse error.
20605 : :
20606 : : May return void_type_node indicating that this method is actually
20607 : : a friend. See grokfield for more details.
20608 : :
20609 : : Came here with a `.pushlevel' .
20610 : :
20611 : : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
20612 : : CHANGES TO CODE IN `grokfield'. */
20613 : :
20614 : : tree
20615 : 72780679 : grokmethod (cp_decl_specifier_seq *declspecs,
20616 : : const cp_declarator *declarator, tree attrlist)
20617 : : {
20618 : 72780679 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
20619 : 72780679 : &attrlist);
20620 : :
20621 : 72780679 : if (fndecl == error_mark_node)
20622 : : return error_mark_node;
20623 : :
20624 : 72780576 : if (attrlist)
20625 : 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
20626 : :
20627 : : /* Pass friends other than inline friend functions back. */
20628 : 72780576 : if (fndecl == void_type_node)
20629 : : return fndecl;
20630 : :
20631 : 72780576 : if (DECL_IN_AGGR_P (fndecl))
20632 : : {
20633 : 0 : if (DECL_CLASS_SCOPE_P (fndecl))
20634 : 0 : error ("%qD is already defined in class %qT", fndecl,
20635 : 0 : DECL_CONTEXT (fndecl));
20636 : 0 : return error_mark_node;
20637 : : }
20638 : :
20639 : 72780576 : check_template_shadow (fndecl);
20640 : :
20641 : : /* p1779 ABI-Isolation makes inline not a default for in-class
20642 : : definitions attached to a named module. If the user explicitly
20643 : : made it inline, grokdeclarator will already have done the right
20644 : : things. */
20645 : 72780576 : if ((!named_module_attach_p ()
20646 : 724 : || flag_module_implicit_inline
20647 : : /* Lambda's operator function remains inline. */
20648 : 1421 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
20649 : : /* If the user explicitly asked for this to be inline, we don't
20650 : : need to do more, but more importantly we want to warn if we
20651 : : can't inline it. */
20652 : 72780191 : && !DECL_DECLARED_INLINE_P (fndecl))
20653 : : {
20654 : 50859038 : if (TREE_PUBLIC (fndecl))
20655 : 49275393 : DECL_COMDAT (fndecl) = 1;
20656 : 50859038 : DECL_DECLARED_INLINE_P (fndecl) = 1;
20657 : : /* It's ok if we can't inline this. */
20658 : 50859038 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
20659 : : }
20660 : :
20661 : : /* We process method specializations in finish_struct_1. */
20662 : 72780576 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
20663 : : {
20664 : : /* Avoid calling decl_spec_seq... until we have to. */
20665 : 59482538 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
20666 : 59482538 : fndecl = push_template_decl (fndecl, friendp);
20667 : 59482538 : if (fndecl == error_mark_node)
20668 : : return fndecl;
20669 : : }
20670 : :
20671 : 72780568 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
20672 : : {
20673 : 0 : fndecl = copy_node (fndecl);
20674 : 0 : TREE_CHAIN (fndecl) = NULL_TREE;
20675 : : }
20676 : :
20677 : 72780568 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
20678 : :
20679 : 72780568 : DECL_IN_AGGR_P (fndecl) = 1;
20680 : 72780568 : return fndecl;
20681 : : }
20682 : :
20683 : :
20684 : : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
20685 : : we can lay it out later, when and if its type becomes complete.
20686 : :
20687 : : Also handle constexpr variables where the initializer involves
20688 : : an unlowered PTRMEM_CST because the class isn't complete yet. */
20689 : :
20690 : : void
20691 : 92376368 : maybe_register_incomplete_var (tree var)
20692 : : {
20693 : 92376368 : gcc_assert (VAR_P (var));
20694 : :
20695 : : /* Keep track of variables with incomplete types. */
20696 : 41741472 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
20697 : 134116178 : && DECL_EXTERNAL (var))
20698 : : {
20699 : 5728798 : tree inner_type = TREE_TYPE (var);
20700 : :
20701 : 5888825 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
20702 : 160027 : inner_type = TREE_TYPE (inner_type);
20703 : 5728798 : inner_type = TYPE_MAIN_VARIANT (inner_type);
20704 : :
20705 : 5790720 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
20706 : : /* RTTI TD entries are created while defining the type_info. */
20707 : 5728798 : || (TYPE_LANG_SPECIFIC (inner_type)
20708 : 2301304 : && TYPE_BEING_DEFINED (inner_type)))
20709 : : {
20710 : 61922 : incomplete_var iv = {var, inner_type};
20711 : 61922 : vec_safe_push (incomplete_vars, iv);
20712 : : }
20713 : 11029274 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
20714 : 3496598 : && DECL_CLASS_SCOPE_P (var)
20715 : 1101247 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
20716 : 1101247 : && decl_constant_var_p (var)
20717 : 6382910 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
20718 : : {
20719 : : /* When the outermost open class is complete we can resolve any
20720 : : pointers-to-members. */
20721 : 13 : tree context = outermost_open_class ();
20722 : 13 : incomplete_var iv = {var, context};
20723 : 13 : vec_safe_push (incomplete_vars, iv);
20724 : : }
20725 : : }
20726 : 92376368 : }
20727 : :
20728 : : /* Called when a class type (given by TYPE) is defined. If there are
20729 : : any existing VAR_DECLs whose type has been completed by this
20730 : : declaration, update them now. */
20731 : :
20732 : : void
20733 : 59684958 : complete_vars (tree type)
20734 : : {
20735 : 59684958 : unsigned ix;
20736 : 59684958 : incomplete_var *iv;
20737 : :
20738 : 60133131 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
20739 : : {
20740 : 448173 : if (same_type_p (type, iv->incomplete_type))
20741 : : {
20742 : 60779 : tree var = iv->decl;
20743 : 60779 : tree type = TREE_TYPE (var);
20744 : :
20745 : 60779 : if (type != error_mark_node
20746 : 60779 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
20747 : 60776 : == iv->incomplete_type))
20748 : : {
20749 : : /* Complete the type of the variable. */
20750 : 60763 : complete_type (type);
20751 : 60763 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
20752 : 60763 : if (COMPLETE_TYPE_P (type))
20753 : 60748 : layout_var_decl (var);
20754 : : }
20755 : :
20756 : : /* Remove this entry from the list. */
20757 : 60779 : incomplete_vars->unordered_remove (ix);
20758 : : }
20759 : : else
20760 : 387394 : ix++;
20761 : : }
20762 : 59684958 : }
20763 : :
20764 : : /* If DECL is of a type which needs a cleanup, build and return an
20765 : : expression to perform that cleanup here. Return NULL_TREE if no
20766 : : cleanup need be done. DECL can also be a _REF when called from
20767 : : split_nonconstant_init_1. */
20768 : :
20769 : : tree
20770 : 71822011 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
20771 : : {
20772 : 71822011 : tree type;
20773 : 71822011 : tree attr;
20774 : 71822011 : tree cleanup;
20775 : :
20776 : : /* Assume no cleanup is required. */
20777 : 71822011 : cleanup = NULL_TREE;
20778 : :
20779 : 71822011 : if (!decl || error_operand_p (decl))
20780 : : return cleanup;
20781 : :
20782 : : /* Handle "__attribute__((cleanup))". We run the cleanup function
20783 : : before the destructor since the destructor is what actually
20784 : : terminates the lifetime of the object. */
20785 : 71822011 : if (DECL_P (decl))
20786 : 71418171 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
20787 : : else
20788 : : attr = NULL_TREE;
20789 : 71418171 : if (attr)
20790 : : {
20791 : 159 : tree id;
20792 : 159 : tree fn;
20793 : 159 : tree arg;
20794 : :
20795 : : /* Get the name specified by the user for the cleanup function. */
20796 : 159 : id = TREE_VALUE (TREE_VALUE (attr));
20797 : : /* Look up the name to find the cleanup function to call. It is
20798 : : important to use lookup_name here because that is what is
20799 : : used in c-common.cc:handle_cleanup_attribute when performing
20800 : : initial checks on the attribute. Note that those checks
20801 : : include ensuring that the function found is not an overloaded
20802 : : function, or an object with an overloaded call operator,
20803 : : etc.; we can rely on the fact that the function found is an
20804 : : ordinary FUNCTION_DECL. */
20805 : 159 : fn = lookup_name (id);
20806 : 159 : arg = build_address (decl);
20807 : 159 : if (!mark_used (decl, complain) && !(complain & tf_error))
20808 : 0 : return error_mark_node;
20809 : 159 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
20810 : 159 : if (cleanup == error_mark_node)
20811 : : return error_mark_node;
20812 : : }
20813 : : /* Handle ordinary C++ destructors. */
20814 : 71821999 : type = TREE_TYPE (decl);
20815 : 71821999 : if (type_build_dtor_call (type))
20816 : : {
20817 : 5492204 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
20818 : 5492204 : tree addr;
20819 : 5492204 : tree call;
20820 : :
20821 : 5492204 : if (TREE_CODE (type) == ARRAY_TYPE)
20822 : : addr = decl;
20823 : : else
20824 : 5490211 : addr = build_address (decl);
20825 : :
20826 : 5492204 : call = build_delete (input_location, TREE_TYPE (addr), addr,
20827 : : sfk_complete_destructor, flags, 0, complain);
20828 : 5492204 : if (call == error_mark_node)
20829 : : cleanup = error_mark_node;
20830 : 5492165 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
20831 : : /* Discard the call. */;
20832 : 4064243 : else if (decl_maybe_constant_destruction (decl, type)
20833 : 4064390 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
20834 : 147 : cxx_constant_dtor (call, decl);
20835 : 4064096 : else if (cleanup)
20836 : 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
20837 : : else
20838 : : cleanup = call;
20839 : : }
20840 : :
20841 : : /* build_delete sets the location of the destructor call to the
20842 : : current location, even though the destructor is going to be
20843 : : called later, at the end of the current scope. This can lead to
20844 : : a "jumpy" behavior for users of debuggers when they step around
20845 : : the end of the block. So let's unset the location of the
20846 : : destructor call instead. */
20847 : 71821999 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
20848 : 71821999 : if (cleanup && CONVERT_EXPR_P (cleanup))
20849 : 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
20850 : :
20851 : 0 : if (cleanup
20852 : 4064279 : && DECL_P (decl)
20853 : 4063806 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
20854 : : /* Treat objects with destructors as used; the destructor may do
20855 : : something substantive. */
20856 : 4063800 : && !mark_used (decl, complain) && !(complain & tf_error))
20857 : 0 : return error_mark_node;
20858 : :
20859 : 4064279 : if (cleanup && cfun && !processing_template_decl
20860 : 75436853 : && !expr_noexcept_p (cleanup, tf_none))
20861 : 5998 : cp_function_chain->throwing_cleanup = true;
20862 : :
20863 : : return cleanup;
20864 : : }
20865 : :
20866 : :
20867 : : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
20868 : : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
20869 : : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
20870 : :
20871 : : tree
20872 : 3189592 : static_fn_type (tree memfntype)
20873 : : {
20874 : 3189592 : tree fntype;
20875 : 3189592 : tree args;
20876 : :
20877 : 3189592 : if (TYPE_PTRMEMFUNC_P (memfntype))
20878 : 21646 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
20879 : 3189592 : if (INDIRECT_TYPE_P (memfntype)
20880 : 2803764 : || TREE_CODE (memfntype) == FUNCTION_DECL)
20881 : 3009543 : memfntype = TREE_TYPE (memfntype);
20882 : 3189592 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
20883 : : return memfntype;
20884 : 590719 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
20885 : 590719 : args = TYPE_ARG_TYPES (memfntype);
20886 : 590719 : fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
20887 : 590719 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
20888 : 590719 : fntype = (cp_build_type_attribute_variant
20889 : 590719 : (fntype, TYPE_ATTRIBUTES (memfntype)));
20890 : 590719 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
20891 : 590719 : return fntype;
20892 : : }
20893 : :
20894 : : /* DECL was originally constructed as a non-static member function,
20895 : : but turned out to be static. Update it accordingly. */
20896 : :
20897 : : void
20898 : 167755 : revert_static_member_fn (tree decl)
20899 : : {
20900 : 167755 : tree stype = static_fn_type (decl);
20901 : 167755 : cp_cv_quals quals = type_memfn_quals (stype);
20902 : 167755 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
20903 : :
20904 : 167755 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
20905 : 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
20906 : :
20907 : 167755 : TREE_TYPE (decl) = stype;
20908 : :
20909 : 167755 : if (DECL_ARGUMENTS (decl))
20910 : 167755 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
20911 : 167755 : DECL_STATIC_FUNCTION_P (decl) = 1;
20912 : 167755 : }
20913 : :
20914 : : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
20915 : : one of the language-independent trees. */
20916 : :
20917 : : enum cp_tree_node_structure_enum
20918 : 33877855144 : cp_tree_node_structure (union lang_tree_node * t)
20919 : : {
20920 : 33877855144 : switch (TREE_CODE (&t->generic))
20921 : : {
20922 : : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
20923 : 146552622 : case BASELINK: return TS_CP_BASELINK;
20924 : 40283211 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
20925 : 11435113 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
20926 : 3724015 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
20927 : 2006597961 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
20928 : 2477158 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
20929 : 240531 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
20930 : 642625274 : case OVERLOAD: return TS_CP_OVERLOAD;
20931 : 799 : case PTRMEM_CST: return TS_CP_PTRMEM;
20932 : 14660234 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
20933 : 621588529 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
20934 : 1440780373 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
20935 : 614963883 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
20936 : 14836465 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
20937 : 6 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
20938 : 2568956 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
20939 : 28314520014 : default: return TS_CP_GENERIC;
20940 : : }
20941 : : }
20942 : :
20943 : : bool
20944 : 10083 : cp_missing_noreturn_ok_p (tree decl)
20945 : : {
20946 : : /* A missing noreturn is ok for the `main' function. */
20947 : 10083 : return DECL_MAIN_P (decl);
20948 : : }
20949 : :
20950 : : /* Return the decl used to identify the COMDAT group into which DECL should
20951 : : be placed. */
20952 : :
20953 : : tree
20954 : 94187776 : cxx_comdat_group (tree decl)
20955 : : {
20956 : : /* Virtual tables, construction virtual tables, and virtual table
20957 : : tables all go in a single COMDAT group, named after the primary
20958 : : virtual table. */
20959 : 94187776 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
20960 : 481599 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
20961 : : /* For all other DECLs, the COMDAT group is the mangled name of the
20962 : : declaration itself. */
20963 : : else
20964 : : {
20965 : 93706177 : while (DECL_THUNK_P (decl))
20966 : : {
20967 : : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
20968 : : into the same section as the target function. In that case
20969 : : we must return target's name. */
20970 : 0 : tree target = THUNK_TARGET (decl);
20971 : 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
20972 : 0 : && DECL_SECTION_NAME (target) != NULL
20973 : 0 : && DECL_ONE_ONLY (target))
20974 : : decl = target;
20975 : : else
20976 : : break;
20977 : : }
20978 : : /* If a ctor/dtor has already set the comdat group by
20979 : : maybe_clone_body, don't override it. */
20980 : 93706177 : if (SUPPORTS_ONE_ONLY
20981 : 93706177 : && TREE_CODE (decl) == FUNCTION_DECL
20982 : 93706177 : && DECL_CLONED_FUNCTION_P (decl))
20983 : 16007435 : if (tree comdat = DECL_COMDAT_GROUP (decl))
20984 : : return comdat;
20985 : : }
20986 : :
20987 : : return decl;
20988 : : }
20989 : :
20990 : : /* Returns the return type for FN as written by the user, which may include
20991 : : a placeholder for a deduced return type. */
20992 : :
20993 : : tree
20994 : 496937713 : fndecl_declared_return_type (tree fn)
20995 : : {
20996 : 496937713 : fn = STRIP_TEMPLATE (fn);
20997 : 496937713 : if (FNDECL_USED_AUTO (fn))
20998 : 5581 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
20999 : :
21000 : 496932132 : return TREE_TYPE (TREE_TYPE (fn));
21001 : : }
21002 : :
21003 : : /* Returns true iff DECL is a variable or function declared with an auto type
21004 : : that has not yet been deduced to a real type. */
21005 : :
21006 : : bool
21007 : 821494155 : undeduced_auto_decl (tree decl)
21008 : : {
21009 : 821494155 : if (cxx_dialect < cxx11)
21010 : : return false;
21011 : 819125573 : STRIP_ANY_LOCATION_WRAPPER (decl);
21012 : 352521904 : return ((VAR_OR_FUNCTION_DECL_P (decl)
21013 : 79740008 : || TREE_CODE (decl) == TEMPLATE_DECL)
21014 : 1092045990 : && type_uses_auto (TREE_TYPE (decl)));
21015 : : }
21016 : :
21017 : : /* Complain if DECL has an undeduced return type. */
21018 : :
21019 : : bool
21020 : 266062703 : require_deduced_type (tree decl, tsubst_flags_t complain)
21021 : : {
21022 : 266062703 : if (undeduced_auto_decl (decl))
21023 : : {
21024 : 3296 : if (TREE_CODE (decl) == FUNCTION_DECL
21025 : 3269 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
21026 : 5970 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
21027 : : {
21028 : : /* Set the return type of __builtin_source_location. */
21029 : 2674 : tree type = get_source_location_impl_type ();
21030 : 2674 : if (type == error_mark_node)
21031 : : {
21032 : 33 : inform (input_location, "using %qs", "__builtin_source_location");
21033 : 33 : return false;
21034 : : }
21035 : 2641 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
21036 : 2641 : type = build_pointer_type (type);
21037 : 2641 : apply_deduced_return_type (decl, type);
21038 : 2641 : return true;
21039 : : }
21040 : :
21041 : 622 : if (warning_suppressed_p (decl) && seen_error ())
21042 : : /* We probably already complained about deduction failure. */;
21043 : 477 : else if (complain & tf_error)
21044 : 72 : error ("use of %qD before deduction of %<auto%>", decl);
21045 : 622 : note_failed_type_completion (decl, complain);
21046 : 622 : return false;
21047 : : }
21048 : : return true;
21049 : : }
21050 : :
21051 : : /* Create a representation of the explicit-specifier with
21052 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
21053 : :
21054 : : tree
21055 : 6528736 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
21056 : : {
21057 : 6528736 : if (check_for_bare_parameter_packs (expr))
21058 : 3 : return error_mark_node;
21059 : :
21060 : 6528733 : if (instantiation_dependent_expression_p (expr))
21061 : : /* Wait for instantiation, tsubst_function_decl will handle it. */
21062 : : return expr;
21063 : :
21064 : 5782726 : expr = build_converted_constant_bool_expr (expr, complain);
21065 : 5782726 : expr = instantiate_non_dependent_expr (expr, complain);
21066 : 5782726 : expr = cxx_constant_value (expr, complain);
21067 : 5782726 : return expr;
21068 : : }
21069 : :
21070 : : #include "gt-cp-decl.h"
|