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 : 227964439 : current_tmpl_spec_kind (int n_class_scopes)
312 : : {
313 : 227964439 : int n_template_parm_scopes = 0;
314 : 227964439 : int seen_specialization_p = 0;
315 : 227964439 : int innermost_specialization_p = 0;
316 : 227964439 : cp_binding_level *b;
317 : :
318 : : /* Scan through the template parameter scopes. */
319 : 227964439 : for (b = current_binding_level;
320 : 285325510 : b->kind == sk_template_parms;
321 : 57361071 : 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 : 57361071 : if (b->explicit_spec_p)
333 : : {
334 : 984824 : if (n_template_parm_scopes == 0)
335 : : innermost_specialization_p = 1;
336 : : else
337 : 1141 : seen_specialization_p = 1;
338 : : }
339 : 56376247 : else if (seen_specialization_p == 1)
340 : : return tsk_invalid_member_spec;
341 : :
342 : 57361071 : ++n_template_parm_scopes;
343 : : }
344 : :
345 : : /* Handle explicit instantiations. */
346 : 227964439 : if (processing_explicit_instantiation)
347 : : {
348 : 2036901 : 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 : 2036901 : return tsk_expl_inst;
358 : : }
359 : :
360 : 225927538 : 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 : 225927538 : 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 : 49026136 : 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 : 97565110 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394 : : }
395 : :
396 : : /* Exit the current scope. */
397 : :
398 : : void
399 : 87577724 : finish_scope (void)
400 : : {
401 : 87577724 : poplevel (0, 0, 0);
402 : 87577724 : }
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 : 20310 : check_label_used (tree label)
409 : : {
410 : 20310 : if (!processing_template_decl)
411 : : {
412 : 20037 : 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 : 20028 : warn_for_unused_label (label);
424 : : }
425 : 20310 : }
426 : :
427 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
428 : :
429 : : static int
430 : 228736 : sort_labels (const void *a, const void *b)
431 : : {
432 : 228736 : tree label1 = *(tree const *) a;
433 : 228736 : tree label2 = *(tree const *) b;
434 : :
435 : : /* DECL_UIDs can never be equal. */
436 : 228736 : 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 : 969 : adjust_backward_gotos (tree *tp, int *walk_subtrees, void *data)
449 : : {
450 : 969 : tree t = *tp;
451 : 969 : switch (TREE_CODE (t))
452 : : {
453 : 1 : 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 : 1 : if (DECL_NAME (LABEL_EXPR_LABEL (t)))
461 : : {
462 : 1 : named_label_entry *ent
463 : 1 : = lookup_label_1 (DECL_NAME (LABEL_EXPR_LABEL (t)), false);
464 : 1 : if (ent->direct_goto)
465 : : {
466 : 1 : *tp = alloc_stmt_list ();
467 : 1 : append_to_statement_list_force (t, tp);
468 : 1 : adjust_backward_goto (ent, tsi_last (*tp));
469 : : }
470 : : }
471 : 1 : *walk_subtrees = 0;
472 : 1 : break;
473 : 49 : case STATEMENT_LIST:
474 : 49 : {
475 : 49 : tree_stmt_iterator i;
476 : 49 : *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 : 293 : for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
482 : 244 : if (TREE_CODE (tsi_stmt (i)) != LABEL_EXPR)
483 : 231 : cp_walk_tree (tsi_stmt_ptr (i), adjust_backward_gotos,
484 : : data, (hash_set<tree> *) data);
485 : 13 : else if (DECL_NAME (LABEL_EXPR_LABEL (tsi_stmt (i))))
486 : : {
487 : 6 : named_label_entry *ent
488 : 6 : = lookup_label_1 (DECL_NAME (LABEL_EXPR_LABEL (tsi_stmt (i))),
489 : : false);
490 : 6 : if (ent->direct_goto)
491 : 6 : adjust_backward_goto (ent, i);
492 : : }
493 : 49 : break;
494 : : }
495 : 919 : default:
496 : 919 : if (TYPE_P (t))
497 : 2 : *walk_subtrees = 0;
498 : : }
499 : 969 : 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 : 143882238 : pop_labels (tree block)
508 : : {
509 : 143882238 : if (!named_labels)
510 : 143877667 : 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 : 4571 : auto_vec<tree, 32> labels (named_labels->elements ());
518 : 4571 : hash_table<named_label_hash>::iterator end (named_labels->end ());
519 : :
520 : 4571 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
521 : : {
522 : 6066 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
523 : : {
524 : 2380 : named_label_entry *ent = *iter;
525 : 2380 : if (ent->direct_goto)
526 : : {
527 : 2 : hash_set<tree> pset;
528 : 2 : cp_walk_tree (&DECL_SAVED_TREE (current_function_decl),
529 : : adjust_backward_gotos, &pset, &pset);
530 : 2 : break;
531 : 2 : }
532 : : }
533 : : }
534 : :
535 : 45125 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
536 : : {
537 : 20277 : named_label_entry *ent = *iter;
538 : :
539 : 20277 : gcc_checking_assert (!ent->outer);
540 : 20277 : if (ent->label_decl)
541 : 20091 : labels.quick_push (ent->label_decl);
542 : 20277 : ggc_free (ent);
543 : : }
544 : 4571 : named_labels = NULL;
545 : 4571 : labels.qsort (sort_labels);
546 : :
547 : 24662 : while (labels.length ())
548 : : {
549 : 20091 : tree label = labels.pop ();
550 : :
551 : 20091 : DECL_CHAIN (label) = BLOCK_VARS (block);
552 : 20091 : BLOCK_VARS (block) = label;
553 : :
554 : 20091 : check_label_used (label);
555 : : }
556 : 4571 : }
557 : :
558 : : /* At the end of a block with local labels, restore the outer definition. */
559 : :
560 : : static void
561 : 219 : pop_local_label (tree id, tree label)
562 : : {
563 : 219 : check_label_used (label);
564 : 219 : named_label_entry **slot = named_labels->find_slot_with_hash
565 : 219 : (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
566 : 219 : named_label_entry *ent = *slot;
567 : :
568 : 219 : if (ent->outer)
569 : : ent = ent->outer;
570 : : else
571 : : {
572 : 198 : ent = ggc_cleared_alloc<named_label_entry> ();
573 : 198 : ent->name = id;
574 : : }
575 : 219 : *slot = ent;
576 : 219 : }
577 : :
578 : : /* The following two routines are used to interface to Objective-C++.
579 : : The binding level is purposely treated as an opaque type. */
580 : :
581 : : void *
582 : 0 : objc_get_current_scope (void)
583 : : {
584 : 0 : return current_binding_level;
585 : : }
586 : :
587 : : /* The following routine is used by the NeXT-style SJLJ exceptions;
588 : : variables get marked 'volatile' so as to not be clobbered by
589 : : _setjmp()/_longjmp() calls. All variables in the current scope,
590 : : as well as parent scopes up to (but not including) ENCLOSING_BLK
591 : : shall be thusly marked. */
592 : :
593 : : void
594 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
595 : : {
596 : 0 : cp_binding_level *scope;
597 : :
598 : 0 : for (scope = current_binding_level;
599 : 0 : scope && scope != enclosing_blk;
600 : 0 : scope = scope->level_chain)
601 : : {
602 : 0 : tree decl;
603 : :
604 : 0 : for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
605 : 0 : objc_volatilize_decl (decl);
606 : :
607 : : /* Do not climb up past the current function. */
608 : 0 : if (scope->kind == sk_function_parms)
609 : : break;
610 : : }
611 : 0 : }
612 : :
613 : : /* True if B is the level for the condition of a constexpr if. */
614 : :
615 : : static bool
616 : 1996181 : level_for_constexpr_if (cp_binding_level *b)
617 : : {
618 : 1992552 : return (b->kind == sk_cond && b->this_entity
619 : 268 : && TREE_CODE (b->this_entity) == IF_STMT
620 : 1996449 : && IF_STMT_CONSTEXPR_P (b->this_entity));
621 : : }
622 : :
623 : : /* True if B is the level for the condition of a consteval if. */
624 : :
625 : : static bool
626 : 1996157 : level_for_consteval_if (cp_binding_level *b)
627 : : {
628 : 1992528 : return (b->kind == sk_cond && b->this_entity
629 : 244 : && TREE_CODE (b->this_entity) == IF_STMT
630 : 1996401 : && IF_STMT_CONSTEVAL_P (b->this_entity));
631 : : }
632 : :
633 : : /* True if T is a non-static VAR_DECL that has a non-trivial destructor.
634 : : See [stmt.dcl]/2. */
635 : :
636 : : static bool
637 : 25 : automatic_var_with_nontrivial_dtor_p (const_tree t)
638 : : {
639 : 25 : if (error_operand_p (t))
640 : : return false;
641 : :
642 : 19 : return (VAR_P (t)
643 : 19 : && decl_storage_duration (CONST_CAST_TREE (t)) == dk_auto
644 : 38 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)));
645 : : }
646 : :
647 : : /* Update data for defined and undefined labels when leaving a scope. */
648 : :
649 : : int
650 : 149980 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
651 : : {
652 : 149980 : named_label_entry *ent = *slot;
653 : 149980 : cp_binding_level *obl = bl->level_chain;
654 : :
655 : 149980 : if (ent->binding_level == bl)
656 : : {
657 : 9760 : tree decl;
658 : :
659 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
660 : : TREE_LISTs representing OVERLOADs, so be careful. */
661 : 14047 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
662 : 4360 : ? DECL_CHAIN (decl)
663 : 73 : : TREE_CHAIN (decl)))
664 : 4287 : if (decl_jump_unsafe (decl))
665 : : {
666 : 1790 : vec_safe_push (ent->bad_decls, decl);
667 : 1790 : ent->has_bad_decls = true;
668 : : }
669 : 2497 : else if (decl_instrument_init_bypass_p (decl))
670 : 351 : vec_safe_push (ent->bad_decls, decl);
671 : :
672 : 9760 : ent->binding_level = obl;
673 : 9760 : ent->names_in_scope = obl->names;
674 : 9760 : switch (bl->kind)
675 : : {
676 : 42 : case sk_try:
677 : 42 : ent->in_try_scope = true;
678 : 42 : break;
679 : 27 : case sk_catch:
680 : 27 : ent->in_catch_scope = true;
681 : 27 : break;
682 : 324 : case sk_omp:
683 : 324 : ent->in_omp_scope = true;
684 : 324 : break;
685 : 3 : case sk_transaction:
686 : 3 : ent->in_transaction_scope = true;
687 : 3 : break;
688 : 45 : case sk_stmt_expr:
689 : 45 : ent->in_stmt_expr = true;
690 : 45 : break;
691 : 3805 : case sk_block:
692 : 3805 : if (level_for_constexpr_if (obl))
693 : 15 : ent->in_constexpr_if = true;
694 : 3790 : else if (level_for_consteval_if (obl))
695 : 30 : ent->in_consteval_if = true;
696 : : break;
697 : : default:
698 : : break;
699 : : }
700 : : }
701 : 140220 : else if (ent->uses)
702 : : {
703 : : struct named_label_use_entry *use;
704 : :
705 : 19881 : for (use = ent->uses; use ; use = use->next)
706 : 13157 : if (use->binding_level == bl)
707 : : {
708 : 5609 : if (auto &cg = use->computed_goto)
709 : : {
710 : 57 : if (bl->kind == sk_catch)
711 : 3 : vec_safe_push (cg, get_identifier ("catch"));
712 : 66 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
713 : 9 : if (automatic_var_with_nontrivial_dtor_p (d))
714 : 6 : vec_safe_push (cg, d);
715 : : }
716 : :
717 : 5609 : use->binding_level = obl;
718 : 5609 : use->names_in_scope = obl->names;
719 : 5609 : if (bl->kind == sk_omp)
720 : 12 : use->in_omp_scope = true;
721 : : }
722 : : }
723 : :
724 : 149980 : return 1;
725 : : }
726 : :
727 : : /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
728 : : when errors were reported, except for -Werror-unused-but-set-*. */
729 : : static int unused_but_set_errorcount;
730 : :
731 : : /* Exit a binding level.
732 : : Pop the level off, and restore the state of the identifier-decl mappings
733 : : that were in effect when this level was entered.
734 : :
735 : : If KEEP == 1, this level had explicit declarations, so
736 : : and create a "block" (a BLOCK node) for the level
737 : : to record its declarations and subblocks for symbol table output.
738 : :
739 : : If FUNCTIONBODY is nonzero, this level is the body of a function,
740 : : so create a block as if KEEP were set and also clear out all
741 : : label names.
742 : :
743 : : If REVERSE is nonzero, reverse the order of decls before putting
744 : : them into the BLOCK. */
745 : :
746 : : tree
747 : 581488089 : poplevel (int keep, int reverse, int functionbody)
748 : : {
749 : 581488089 : tree link;
750 : : /* The chain of decls was accumulated in reverse order.
751 : : Put it into forward order, just for cleanliness. */
752 : 581488089 : tree decls;
753 : 581488089 : tree subblocks;
754 : 581488089 : tree block;
755 : 581488089 : tree decl;
756 : 581488089 : scope_kind kind;
757 : :
758 : 581488089 : auto_cond_timevar tv (TV_NAME_LOOKUP);
759 : 581488395 : restart:
760 : :
761 : 581488395 : block = NULL_TREE;
762 : :
763 : 581488395 : gcc_assert (current_binding_level->kind != sk_class
764 : : && current_binding_level->kind != sk_namespace);
765 : :
766 : 581488395 : if (current_binding_level->kind == sk_cleanup)
767 : : functionbody = 0;
768 : 581488395 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
769 : :
770 : 581488395 : gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
771 : :
772 : : /* We used to use KEEP == 2 to indicate that the new block should go
773 : : at the beginning of the list of blocks at this binding level,
774 : : rather than the end. This hack is no longer used. */
775 : 581488395 : gcc_assert (keep == 0 || keep == 1);
776 : :
777 : 581488395 : if (current_binding_level->keep)
778 : 7382250 : keep = 1;
779 : :
780 : : /* Any uses of undefined labels, and any defined labels, now operate
781 : : under constraints of next binding contour. */
782 : 581488395 : if (cfun && !functionbody && named_labels)
783 : 42229 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
784 : 192209 : (current_binding_level);
785 : :
786 : : /* Get the decls in the order they were written.
787 : : Usually current_binding_level->names is in reverse order.
788 : : But parameter decls were previously put in forward order. */
789 : :
790 : 581488395 : decls = current_binding_level->names;
791 : 581488395 : if (reverse)
792 : : {
793 : 313058576 : decls = nreverse (decls);
794 : 313058576 : current_binding_level->names = decls;
795 : : }
796 : :
797 : : /* If there were any declarations or structure tags in that level,
798 : : or if this level is a function body,
799 : : create a BLOCK to record them for the life of this function. */
800 : 581488395 : block = NULL_TREE;
801 : : /* Avoid function body block if possible. */
802 : 617260110 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
803 : : keep = 0;
804 : 545716941 : else if (keep == 1 || functionbody)
805 : 179462316 : block = make_node (BLOCK);
806 : 179462316 : if (block != NULL_TREE)
807 : : {
808 : 179462316 : BLOCK_VARS (block) = decls;
809 : 179462316 : BLOCK_SUBBLOCKS (block) = subblocks;
810 : : }
811 : :
812 : : /* In each subblock, record that this is its superior. */
813 : 581488395 : if (keep >= 0)
814 : 652839211 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
815 : 71350816 : BLOCK_SUPERCONTEXT (link) = block;
816 : :
817 : : /* Before we remove the declarations first check for unused variables. */
818 : 581488395 : if ((warn_unused_variable || warn_unused_but_set_variable)
819 : 6308255 : && current_binding_level->kind != sk_template_parms
820 : 5338031 : && !processing_template_decl)
821 : 6244247 : for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
822 : : {
823 : : /* There are cases where D itself is a TREE_LIST. See in
824 : : push_local_binding where the list of decls returned by
825 : : getdecls is built. */
826 : 3193298 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
827 : 3193298 : decl = strip_using_decl (udecl);
828 : :
829 : 3193298 : tree type = TREE_TYPE (decl);
830 : 3193298 : if (VAR_P (decl)
831 : 371762 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
832 : 3369 : && !DECL_IN_SYSTEM_HEADER (udecl)
833 : : /* For structured bindings, consider only real variables, not
834 : : subobjects. */
835 : 3357 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
836 : 3105 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
837 : : /* Don't warn about name-independent declarations. */
838 : 885 : && !name_independent_decl_p (decl)
839 : 729 : && type != error_mark_node
840 : 3197352 : && (!CLASS_TYPE_P (type)
841 : 51 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
842 : 3 : || lookup_attribute ("warn_unused",
843 : 3 : TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
844 : : {
845 : 697 : if (!TREE_USED (decl))
846 : : {
847 : 302 : if (TREE_CODE (udecl) == USING_DECL)
848 : 3 : warning_at (DECL_SOURCE_LOCATION (udecl),
849 : 3 : OPT_Wunused_variable,
850 : : "unused using-declaration %qD", udecl);
851 : 299 : else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
852 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
853 : 3 : OPT_Wunused_variable,
854 : : "unused structured binding declaration");
855 : : else
856 : 296 : warning_at (DECL_SOURCE_LOCATION (decl),
857 : 296 : OPT_Wunused_variable, "unused variable %qD", decl);
858 : 302 : suppress_warning (decl, OPT_Wunused_variable);
859 : : }
860 : 395 : else if (DECL_CONTEXT (decl) == current_function_decl
861 : : // For -Wunused-but-set-variable leave references alone.
862 : 371 : && !TYPE_REF_P (TREE_TYPE (decl))
863 : 766 : && errorcount == unused_but_set_errorcount)
864 : : {
865 : 362 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
866 : 12 : warning_at (DECL_SOURCE_LOCATION (decl),
867 : 12 : OPT_Wunused_but_set_variable_, "structured "
868 : : "binding declaration set but not used");
869 : : else
870 : 350 : warning_at (DECL_SOURCE_LOCATION (decl),
871 : 350 : OPT_Wunused_but_set_variable_,
872 : : "variable %qD set but not used", decl);
873 : 362 : unused_but_set_errorcount = errorcount;
874 : : }
875 : : }
876 : : }
877 : :
878 : : /* Remove declarations for all the DECLs in this level. */
879 : 1185192045 : for (link = decls; link; link = TREE_CHAIN (link))
880 : : {
881 : 603703650 : tree name;
882 : 603703650 : if (TREE_CODE (link) == TREE_LIST)
883 : : {
884 : 3984317 : decl = TREE_VALUE (link);
885 : 3984317 : name = TREE_PURPOSE (link);
886 : 3984317 : gcc_checking_assert (name);
887 : : }
888 : : else
889 : : {
890 : 599719333 : decl = link;
891 : 599719333 : name = DECL_NAME (decl);
892 : : }
893 : :
894 : : /* Remove the binding. */
895 : 603703650 : if (TREE_CODE (decl) == LABEL_DECL)
896 : 219 : pop_local_label (name, decl);
897 : : else
898 : 603703431 : pop_local_binding (name, decl);
899 : : }
900 : :
901 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
902 : 581488395 : for (link = current_binding_level->type_shadowed;
903 : 790531936 : link; link = TREE_CHAIN (link))
904 : 209043541 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
905 : :
906 : : /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
907 : : list if a `using' declaration put them there. The debugging
908 : : back ends won't understand OVERLOAD, so we remove them here.
909 : : Because the BLOCK_VARS are (temporarily) shared with
910 : : CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
911 : : popped all the bindings. Also remove undeduced 'auto' decls,
912 : : which LTO doesn't understand, and can't have been used by anything. */
913 : 581488395 : if (block)
914 : : {
915 : 179462316 : tree* d;
916 : :
917 : 471901733 : for (d = &BLOCK_VARS (block); *d; )
918 : : {
919 : 292439417 : if (TREE_CODE (*d) == TREE_LIST
920 : 292439417 : || (!processing_template_decl
921 : 93519094 : && undeduced_auto_decl (*d)))
922 : 3984369 : *d = TREE_CHAIN (*d);
923 : : else
924 : 288455048 : d = &DECL_CHAIN (*d);
925 : : }
926 : : }
927 : :
928 : : /* If the level being exited is the top level of a function,
929 : : check over all the labels. */
930 : 581488395 : if (functionbody)
931 : : {
932 : 143882238 : if (block)
933 : : {
934 : : /* Since this is the top level block of a function, the vars are
935 : : the function's parameters. Don't leave them in the BLOCK
936 : : because they are found in the FUNCTION_DECL instead. */
937 : 108110784 : BLOCK_VARS (block) = 0;
938 : 108110784 : pop_labels (block);
939 : : }
940 : : else
941 : 35771454 : pop_labels (subblocks);
942 : : }
943 : :
944 : 581488395 : kind = current_binding_level->kind;
945 : 581488395 : if (kind == sk_cleanup)
946 : : {
947 : 306 : tree stmt;
948 : :
949 : : /* If this is a temporary binding created for a cleanup, then we'll
950 : : have pushed a statement list level. Pop that, create a new
951 : : BIND_EXPR for the block, and insert it into the stream. */
952 : 306 : stmt = pop_stmt_list (current_binding_level->statement_list);
953 : 306 : stmt = c_build_bind_expr (input_location, block, stmt);
954 : 306 : add_stmt (stmt);
955 : : }
956 : :
957 : 581488395 : leave_scope ();
958 : 581488395 : if (functionbody)
959 : : {
960 : : /* The current function is being defined, so its DECL_INITIAL
961 : : should be error_mark_node. */
962 : 143882238 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
963 : 179653692 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
964 : 143882238 : if (subblocks)
965 : : {
966 : 67379127 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
967 : 44263387 : && BLOCK_SUBBLOCKS (subblocks))
968 : 1253759 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
969 : : else
970 : 34517956 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
971 : : }
972 : : }
973 : 437606157 : else if (block)
974 : 71351532 : current_binding_level->blocks
975 : 142703064 : = block_chainon (current_binding_level->blocks, block);
976 : :
977 : : /* If we did not make a block for the level just exited,
978 : : any blocks made for inner levels
979 : : (since they cannot be recorded as subblocks in that level)
980 : : must be carried forward so they will later become subblocks
981 : : of something else. */
982 : 366254625 : else if (subblocks)
983 : 18 : current_binding_level->blocks
984 : 36 : = block_chainon (current_binding_level->blocks, subblocks);
985 : :
986 : : /* Each and every BLOCK node created here in `poplevel' is important
987 : : (e.g. for proper debugging information) so if we created one
988 : : earlier, mark it as "used". */
989 : 581488395 : if (block)
990 : 179462316 : TREE_USED (block) = 1;
991 : :
992 : : /* All temporary bindings created for cleanups are popped silently. */
993 : 581488395 : if (kind == sk_cleanup)
994 : 306 : goto restart;
995 : :
996 : 1162976178 : return block;
997 : 581488089 : }
998 : :
999 : : /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
1000 : : /* Diagnose odr-used extern inline variables without definitions
1001 : : in the current TU. */
1002 : :
1003 : : int
1004 : 141551 : wrapup_namespace_globals ()
1005 : : {
1006 : 141551 : if (vec<tree, va_gc> *statics = static_decls)
1007 : : {
1008 : 15209143 : for (tree decl : *statics)
1009 : : {
1010 : 15092978 : if (warn_unused_function
1011 : 984697 : && TREE_CODE (decl) == FUNCTION_DECL
1012 : 739533 : && DECL_INITIAL (decl) == 0
1013 : 7621 : && DECL_EXTERNAL (decl)
1014 : 7621 : && !TREE_PUBLIC (decl)
1015 : 29 : && !DECL_ARTIFICIAL (decl)
1016 : 29 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1017 : 15092984 : && !warning_suppressed_p (decl, OPT_Wunused_function))
1018 : 6 : warning_at (DECL_SOURCE_LOCATION (decl),
1019 : 6 : OPT_Wunused_function,
1020 : : "%qF declared %<static%> but never defined", decl);
1021 : :
1022 : 15092978 : if (VAR_P (decl)
1023 : 4340416 : && DECL_EXTERNAL (decl)
1024 : 2010310 : && DECL_INLINE_VAR_P (decl)
1025 : 15092996 : && DECL_ODR_USED (decl))
1026 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
1027 : : "odr-used inline variable %qD is not defined", decl);
1028 : : }
1029 : :
1030 : : /* Clear out the list, so we don't rescan next time. */
1031 : 116165 : static_decls = NULL;
1032 : :
1033 : : /* Write out any globals that need to be output. */
1034 : 116165 : return wrapup_global_declarations (statics->address (),
1035 : 116165 : statics->length ());
1036 : : }
1037 : : return 0;
1038 : : }
1039 : :
1040 : : /* In C++, you don't have to write `struct S' to refer to `S'; you
1041 : : can just use `S'. We accomplish this by creating a TYPE_DECL as
1042 : : if the user had written `typedef struct S S'. Create and return
1043 : : the TYPE_DECL for TYPE. */
1044 : :
1045 : : tree
1046 : 154540588 : create_implicit_typedef (tree name, tree type)
1047 : : {
1048 : 154540588 : tree decl;
1049 : :
1050 : 154540588 : decl = build_decl (input_location, TYPE_DECL, name, type);
1051 : 154540588 : DECL_ARTIFICIAL (decl) = 1;
1052 : : /* There are other implicit type declarations, like the one *within*
1053 : : a class that allows you to write `S::S'. We must distinguish
1054 : : amongst these. */
1055 : 154540588 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1056 : 154540588 : TYPE_NAME (type) = decl;
1057 : 154540588 : TYPE_STUB_DECL (type) = decl;
1058 : :
1059 : 154540588 : return decl;
1060 : : }
1061 : :
1062 : : /* Function-scope local entities that need discriminators. Each entry
1063 : : is a {decl,name} pair. VAR_DECLs for anon unions get their name
1064 : : smashed, so we cannot rely on DECL_NAME. */
1065 : :
1066 : : static GTY((deletable)) vec<tree, va_gc> *local_entities;
1067 : :
1068 : : /* Determine the mangling discriminator of local DECL. There are
1069 : : generally very few of these in any particular function. */
1070 : :
1071 : : void
1072 : 711402 : determine_local_discriminator (tree decl, tree name)
1073 : : {
1074 : 711402 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1075 : 711402 : retrofit_lang_decl (decl);
1076 : 711402 : tree ctx = DECL_CONTEXT (decl);
1077 : 711402 : size_t nelts = vec_safe_length (local_entities);
1078 : 711402 : if (name == NULL_TREE)
1079 : 711143 : name = (TREE_CODE (decl) == TYPE_DECL
1080 : 1141566 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
1081 : 1277915 : ? NULL_TREE : DECL_NAME (decl));
1082 : 21346478 : for (size_t i = 0; i < nelts; i += 2)
1083 : : {
1084 : 20635541 : tree *pair = &(*local_entities)[i];
1085 : 20635541 : tree d = pair[0];
1086 : 20635541 : tree n = pair[1];
1087 : 20635541 : gcc_checking_assert (d != decl);
1088 : 20635541 : if (name == n
1089 : 3675105 : && TREE_CODE (decl) == TREE_CODE (d)
1090 : 24310637 : && ctx == DECL_CONTEXT (d))
1091 : : {
1092 : 465 : tree disc = integer_one_node;
1093 : 465 : if (DECL_DISCRIMINATOR (d))
1094 : 244 : disc = build_int_cst (TREE_TYPE (disc),
1095 : 244 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
1096 : 465 : DECL_DISCRIMINATOR (decl) = disc;
1097 : : /* Replace the saved decl. */
1098 : 465 : pair[0] = decl;
1099 : 465 : decl = NULL_TREE;
1100 : 465 : break;
1101 : : }
1102 : : }
1103 : :
1104 : 711402 : if (decl)
1105 : : {
1106 : 710937 : vec_safe_reserve (local_entities, 2);
1107 : 710937 : local_entities->quick_push (decl);
1108 : 710937 : local_entities->quick_push (name);
1109 : : }
1110 : 711402 : }
1111 : :
1112 : :
1113 : : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
1114 : :
1115 : : A non-template friend declaration with a requires-clause shall be a
1116 : : definition. A friend function template with a constraint that depends on a
1117 : : template parameter from an enclosing template shall be a definition. Such a
1118 : : constrained friend function or function template declaration does not
1119 : : declare the same function or function template as a declaration in any other
1120 : : scope.
1121 : :
1122 : : The ABI calls this a "member-like constrained friend" and mangles it like a
1123 : : member function to avoid collisions. */
1124 : :
1125 : : bool
1126 : 1997240688 : member_like_constrained_friend_p (tree decl)
1127 : : {
1128 : 1997240688 : return (TREE_CODE (decl) == FUNCTION_DECL
1129 : 602512902 : && DECL_UNIQUE_FRIEND_P (decl)
1130 : 86349266 : && DECL_FRIEND_CONTEXT (decl)
1131 : 38462676 : && get_constraints (decl)
1132 : 1997793105 : && (!DECL_TEMPLATE_INFO (decl)
1133 : 552417 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1134 : 4281 : || (uses_outer_template_parms_in_constraints
1135 : 4281 : (most_general_template (decl)))));
1136 : : }
1137 : :
1138 : : /* Returns true if functions FN1 and FN2 have equivalent trailing
1139 : : requires clauses. */
1140 : :
1141 : : static bool
1142 : 25483572 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1143 : : {
1144 : : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1145 : : same function as a declaration in any other scope." So no need to
1146 : : actually compare the requirements. */
1147 : 25483572 : if (member_like_constrained_friend_p (newfn)
1148 : 25483572 : || member_like_constrained_friend_p (oldfn))
1149 : 113 : return false;
1150 : :
1151 : : /* Compare only trailing requirements. */
1152 : 25483459 : tree reqs1 = get_trailing_function_requirements (newfn);
1153 : 25483459 : tree reqs2 = get_trailing_function_requirements (oldfn);
1154 : 25483459 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1155 : : return false;
1156 : :
1157 : : /* Substitution is needed when friends are involved. */
1158 : 25432897 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1159 : 25432897 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1160 : :
1161 : 25432897 : return cp_tree_equal (reqs1, reqs2);
1162 : : }
1163 : :
1164 : : /* Two functions of the same name correspond [basic.scope.scope] if
1165 : :
1166 : : + both declare functions with the same non-object-parameter-type-list,
1167 : : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1168 : : specified in [temp.friend]), and, if both are non-static members, they have
1169 : : corresponding object parameters, or
1170 : :
1171 : : + both declare function templates with equivalent
1172 : : non-object-parameter-type-lists, return types (if any), template-heads, and
1173 : : trailing requires-clauses (if any), and, if both are non-static members,
1174 : : they have corresponding object parameters.
1175 : :
1176 : : This is a subset of decls_match: it identifies declarations that cannot be
1177 : : overloaded with one another. This function does not consider DECL_NAME. */
1178 : :
1179 : : bool
1180 : 690801821 : fns_correspond (tree newdecl, tree olddecl)
1181 : : {
1182 : 690801821 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1183 : : return false;
1184 : :
1185 : 639102366 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1186 : : {
1187 : 639075375 : if (!template_heads_equivalent_p (newdecl, olddecl))
1188 : : return 0;
1189 : 147844812 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1190 : 147844812 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1191 : : }
1192 : :
1193 : 147871803 : tree f1 = TREE_TYPE (newdecl);
1194 : 147871803 : tree f2 = TREE_TYPE (olddecl);
1195 : :
1196 : 147871803 : int rq1 = type_memfn_rqual (f1);
1197 : 147871803 : int rq2 = type_memfn_rqual (f2);
1198 : :
1199 : : /* If only one is a non-static member function, ignore ref-quals. */
1200 : 147871803 : if (TREE_CODE (f1) != TREE_CODE (f2))
1201 : : rq1 = rq2;
1202 : : /* Two non-static member functions have corresponding object parameters if:
1203 : : + exactly one is an implicit object member function with no ref-qualifier
1204 : : and the types of their object parameters ([dcl.fct]), after removing
1205 : : top-level references, are the same, or
1206 : : + their object parameters have the same type. */
1207 : : /* ??? We treat member functions of different classes as corresponding even
1208 : : though that means the object parameters have different types. */
1209 : 147871803 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1210 : 0 : rq1 = rq2;
1211 : :
1212 : 147871803 : bool types_match = rq1 == rq2;
1213 : :
1214 : 147871803 : if (types_match)
1215 : : {
1216 : 147871803 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1217 : 147871803 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1218 : 147871803 : types_match = compparms (p1, p2);
1219 : : }
1220 : :
1221 : : /* Two function declarations match if either has a requires-clause
1222 : : then both have a requires-clause and their constraints-expressions
1223 : : are equivalent. */
1224 : 147871803 : if (types_match && flag_concepts)
1225 : 18230523 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1226 : :
1227 : : return types_match;
1228 : : }
1229 : :
1230 : : /* Subroutine of duplicate_decls: return truthvalue of whether
1231 : : or not types of these decls match.
1232 : :
1233 : : For C++, we must compare the parameter list so that `int' can match
1234 : : `int&' in a parameter position, but `int&' is not confused with
1235 : : `const int&'. */
1236 : :
1237 : : int
1238 : 950509877 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1239 : : {
1240 : 950509877 : int types_match;
1241 : :
1242 : 950509877 : if (newdecl == olddecl)
1243 : : return 1;
1244 : :
1245 : 950443771 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1246 : : /* If the two DECLs are not even the same kind of thing, we're not
1247 : : interested in their types. */
1248 : : return 0;
1249 : :
1250 : 470760801 : gcc_assert (DECL_P (newdecl));
1251 : :
1252 : 470760801 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1253 : : {
1254 : : /* Specializations of different templates are different functions
1255 : : even if they have the same type. */
1256 : 264023709 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1257 : 264023709 : ? DECL_TI_TEMPLATE (newdecl)
1258 : 595321 : : NULL_TREE);
1259 : 264023709 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1260 : 264023709 : ? DECL_TI_TEMPLATE (olddecl)
1261 : 3214298 : : NULL_TREE);
1262 : 264023709 : if (t1 != t2)
1263 : : return 0;
1264 : :
1265 : 261404666 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1266 : 264557884 : && ! (DECL_EXTERN_C_P (newdecl)
1267 : 3153218 : && DECL_EXTERN_C_P (olddecl)))
1268 : : return 0;
1269 : :
1270 : : /* A new declaration doesn't match a built-in one unless it
1271 : : is also extern "C". */
1272 : 241642403 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1273 : 241642403 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1274 : : return 0;
1275 : :
1276 : 240955078 : tree f1 = TREE_TYPE (newdecl);
1277 : 240955078 : tree f2 = TREE_TYPE (olddecl);
1278 : 240955078 : if (TREE_CODE (f1) != TREE_CODE (f2))
1279 : : return 0;
1280 : :
1281 : : /* A declaration with deduced return type should use its pre-deduction
1282 : : type for declaration matching. */
1283 : 240787218 : tree r2 = fndecl_declared_return_type (olddecl);
1284 : 240787218 : tree r1 = fndecl_declared_return_type (newdecl);
1285 : :
1286 : : /* For memchr et al, allow const void* return type (as specified by C++)
1287 : : when we expect void* (as in C). */
1288 : 240787218 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1289 : 6560752 : && DECL_EXTERN_C_P (olddecl)
1290 : 4165019 : && !same_type_p (r1, r2)
1291 : 70 : && TREE_CODE (r1) == POINTER_TYPE
1292 : 15 : && TREE_CODE (r2) == POINTER_TYPE
1293 : 240787230 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1294 : : r2 = r1;
1295 : :
1296 : 240787218 : tree p1 = TYPE_ARG_TYPES (f1);
1297 : 240787218 : tree p2 = TYPE_ARG_TYPES (f2);
1298 : :
1299 : 240787218 : if (same_type_p (r1, r2))
1300 : : {
1301 : 176972013 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1302 : 176971985 : && fndecl_built_in_p (olddecl))
1303 : : {
1304 : 50 : types_match = self_promoting_args_p (p1);
1305 : 50 : if (p1 == void_list_node)
1306 : 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1307 : : }
1308 : : else
1309 : 176971879 : types_match =
1310 : 176971879 : compparms (p1, p2)
1311 : 28597663 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1312 : 205569300 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1313 : 1229187 : || comp_type_attributes (TREE_TYPE (newdecl),
1314 : 1229187 : TREE_TYPE (olddecl)) != 0);
1315 : : }
1316 : : else
1317 : : types_match = 0;
1318 : :
1319 : : /* Two function declarations match if either has a requires-clause
1320 : : then both have a requires-clause and their constraints-expressions
1321 : : are equivalent. */
1322 : 28597450 : if (types_match && flag_concepts)
1323 : 7253023 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1324 : :
1325 : : /* The decls dont match if they correspond to two different versions
1326 : : of the same function. Disallow extern "C" functions to be
1327 : : versions for now. */
1328 : 28597450 : if (types_match
1329 : 28530908 : && !DECL_EXTERN_C_P (newdecl)
1330 : 20523920 : && !DECL_EXTERN_C_P (olddecl)
1331 : 49117590 : && disjoint_version_decls (newdecl, olddecl))
1332 : : {
1333 : 8814 : if (record_versions)
1334 : 8727 : maybe_version_functions (newdecl, olddecl);
1335 : 8814 : return 0;
1336 : : }
1337 : : }
1338 : 206737092 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1339 : : {
1340 : 205284562 : if (!template_heads_equivalent_p (newdecl, olddecl))
1341 : : return 0;
1342 : :
1343 : 65862607 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1344 : 65862607 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1345 : :
1346 : 65862607 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1347 : : return 0;
1348 : :
1349 : : /* Two template types match if they are the same. Otherwise, compare
1350 : : the underlying declarations. */
1351 : 65862601 : if (TREE_CODE (newres) == TYPE_DECL)
1352 : 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1353 : : else
1354 : 65862571 : types_match = decls_match (newres, oldres);
1355 : : }
1356 : : else
1357 : : {
1358 : : /* Need to check scope for variable declaration (VAR_DECL).
1359 : : For typedef (TYPE_DECL), scope is ignored. */
1360 : 1452530 : if (VAR_P (newdecl)
1361 : 968960 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1362 : : /* [dcl.link]
1363 : : Two declarations for an object with C language linkage
1364 : : with the same name (ignoring the namespace that qualify
1365 : : it) that appear in different namespace scopes refer to
1366 : : the same object. */
1367 : 1630494 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1368 : : return 0;
1369 : :
1370 : 1274614 : if (TREE_TYPE (newdecl) == error_mark_node)
1371 : 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1372 : 1274614 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1373 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1374 : 1274614 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1375 : : types_match = 0;
1376 : : else
1377 : 1274614 : types_match = comptypes (TREE_TYPE (newdecl),
1378 : 1274614 : TREE_TYPE (olddecl),
1379 : : COMPARE_REDECLARATION);
1380 : : }
1381 : :
1382 : : return types_match;
1383 : : }
1384 : :
1385 : : /* Mark DECL as versioned if it isn't already. */
1386 : :
1387 : : void
1388 : 17544 : maybe_mark_function_versioned (tree decl)
1389 : : {
1390 : 17544 : if (!DECL_FUNCTION_VERSIONED (decl))
1391 : : {
1392 : : /* We need to insert function version now to make sure the correct
1393 : : pre-mangled assembler name is recorded. */
1394 : 1050 : cgraph_node *node = cgraph_node::get_create (decl);
1395 : :
1396 : 1050 : if (!node->function_version ())
1397 : 1050 : node->insert_new_function_version ();
1398 : :
1399 : 1050 : DECL_FUNCTION_VERSIONED (decl) = 1;
1400 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1401 : : to include the version marker. */
1402 : 1050 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1403 : 1044 : mangle_decl (decl);
1404 : : }
1405 : 17544 : }
1406 : :
1407 : : /* NEWDECL and OLDDECL have identical signatures. If they are
1408 : : different versions adjust them, record function versions, and return
1409 : : true. */
1410 : :
1411 : : bool
1412 : 165782 : maybe_version_functions (tree newdecl, tree olddecl)
1413 : : {
1414 : 165782 : if (!disjoint_version_decls (newdecl, olddecl))
1415 : : return false;
1416 : :
1417 : 8754 : maybe_mark_function_versioned (olddecl);
1418 : 8754 : if (DECL_LOCAL_DECL_P (olddecl))
1419 : : {
1420 : 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1421 : 18 : maybe_mark_function_versioned (olddecl);
1422 : : }
1423 : :
1424 : 8754 : maybe_mark_function_versioned (newdecl);
1425 : 8754 : if (DECL_LOCAL_DECL_P (newdecl))
1426 : : {
1427 : : /* Unfortunately, we can get here before pushdecl naturally calls
1428 : : push_local_extern_decl_alias, so we need to call it directly. */
1429 : 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1430 : 6 : push_local_extern_decl_alias (newdecl);
1431 : 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1432 : 18 : maybe_mark_function_versioned (newdecl);
1433 : : }
1434 : :
1435 : : /* Add the new version to the function version structure. */
1436 : 8754 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1437 : 8754 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1438 : 8754 : if (!fn_v)
1439 : 15 : fn_v = fn_node->insert_new_function_version ();
1440 : :
1441 : 8754 : cgraph_node::add_function_version (fn_v, newdecl);
1442 : :
1443 : 8754 : return true;
1444 : : }
1445 : :
1446 : : /* If NEWDECL is `static' and an `extern' was seen previously,
1447 : : warn about it. OLDDECL is the previous declaration.
1448 : :
1449 : : Note that this does not apply to the C++ case of declaring
1450 : : a variable `extern const' and then later `const'.
1451 : :
1452 : : Don't complain about built-in functions, since they are beyond
1453 : : the user's control. */
1454 : :
1455 : : void
1456 : 19107829 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1457 : : {
1458 : 19107829 : if (TREE_CODE (newdecl) == TYPE_DECL
1459 : 19081503 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1460 : 15486377 : || TREE_CODE (newdecl) == CONST_DECL
1461 : 15486377 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1462 : 19107817 : return;
1463 : :
1464 : : /* Don't get confused by static member functions; that's a different
1465 : : use of `static'. */
1466 : 15486377 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1467 : 15486377 : && DECL_STATIC_FUNCTION_P (newdecl))
1468 : : return;
1469 : :
1470 : : /* If the old declaration was `static', or the new one isn't, then
1471 : : everything is OK. */
1472 : 15322322 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1473 : : return;
1474 : :
1475 : : /* It's OK to declare a builtin function as `static'. */
1476 : 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1477 : 12 : && DECL_ARTIFICIAL (olddecl))
1478 : : return;
1479 : :
1480 : 12 : auto_diagnostic_group d;
1481 : 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1482 : : "%qD was declared %<extern%> and later %<static%>", newdecl))
1483 : 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1484 : : "previous declaration of %qD", olddecl);
1485 : 12 : }
1486 : :
1487 : : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1488 : : function templates. If their exception specifications do not
1489 : : match, issue a diagnostic. */
1490 : :
1491 : : static void
1492 : 18686526 : check_redeclaration_exception_specification (tree new_decl,
1493 : : tree old_decl)
1494 : : {
1495 : 18686526 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1496 : 18686526 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1497 : :
1498 : : /* Two default specs are equivalent, don't force evaluation. */
1499 : 7026035 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1500 : 18837556 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1501 : : return;
1502 : :
1503 : 18676176 : if (!type_dependent_expression_p (old_decl))
1504 : : {
1505 : 11049207 : maybe_instantiate_noexcept (new_decl);
1506 : 11049207 : maybe_instantiate_noexcept (old_decl);
1507 : : }
1508 : 18676176 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1509 : 18676176 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1510 : :
1511 : : /* [except.spec]
1512 : :
1513 : : If any declaration of a function has an exception-specification,
1514 : : all declarations, including the definition and an explicit
1515 : : specialization, of that function shall have an
1516 : : exception-specification with the same set of type-ids. */
1517 : 18676176 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1518 : 14640329 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1519 : 33316410 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1520 : : {
1521 : 326 : const char *const msg
1522 : : = G_("declaration of %qF has a different exception specifier");
1523 : 326 : bool complained = true;
1524 : 326 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1525 : 326 : auto_diagnostic_group d;
1526 : :
1527 : : /* Be permissive about C++98 vs C++11 operator new declarations. */
1528 : 652 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1529 : 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1530 : 328 : && (nothrow_spec_p (new_exceptions)
1531 : 1 : == nothrow_spec_p (old_exceptions)));
1532 : :
1533 : 326 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1534 : 326 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1535 : : /* Don't fuss about the C library; the C library functions are not
1536 : : specified to have exception specifications (just behave as if they
1537 : : have them), but some implementations include them. */
1538 : 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1539 : 93 : else if (!flag_exceptions)
1540 : : /* We used to silently permit mismatched eh specs with
1541 : : -fno-exceptions, so only complain if -pedantic. */
1542 : 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1543 : 90 : else if (!new_exceptions || global_new)
1544 : : /* Reduce to pedwarn for omitted exception specification. No warning
1545 : : flag for this; silence the warning by correcting the code. */
1546 : 21 : complained = pedwarn (new_loc, 0, msg, new_decl);
1547 : : else
1548 : 69 : error_at (new_loc, msg, new_decl);
1549 : :
1550 : 326 : if (complained)
1551 : 96 : inform (DECL_SOURCE_LOCATION (old_decl),
1552 : : "from previous declaration %qF", old_decl);
1553 : :
1554 : : /* Copy the old exception specification if new_decl has none. Unless the
1555 : : old decl is extern "C", as obscure code might depend on the type of
1556 : : the new declaration (e.g. noexcept-type19.C). */
1557 : 326 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1558 : 20 : TREE_TYPE (new_decl)
1559 : 40 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1560 : 326 : }
1561 : : }
1562 : :
1563 : : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1564 : : Otherwise issue diagnostics. */
1565 : :
1566 : : bool
1567 : 19108316 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1568 : : {
1569 : 19108316 : old_decl = STRIP_TEMPLATE (old_decl);
1570 : 19108316 : new_decl = STRIP_TEMPLATE (new_decl);
1571 : 19108316 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1572 : 19081966 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1573 : : return true;
1574 : 19081966 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1575 : 19081966 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1576 : : {
1577 : 19039858 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1578 : : return true;
1579 : 37373836 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1580 : 18686918 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1581 : : return true;
1582 : : }
1583 : 42114 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1584 : : {
1585 : : /* With -fimplicit-constexpr, ignore changes in the constexpr
1586 : : keyword. */
1587 : 61 : if (flag_implicit_constexpr
1588 : 61 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1589 : 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1590 : : return true;
1591 : 61 : if (fndecl_built_in_p (old_decl))
1592 : : {
1593 : : /* Hide a built-in declaration. */
1594 : 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1595 : 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1596 : 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1597 : 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1598 : 0 : return true;
1599 : : }
1600 : : /* 7.1.5 [dcl.constexpr]
1601 : : Note: An explicit specialization can differ from the template
1602 : : declaration with respect to the constexpr specifier. */
1603 : 61 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1604 : 61 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1605 : : return true;
1606 : :
1607 : 42 : const char *kind = "constexpr";
1608 : 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1609 : 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1610 : : kind = "consteval";
1611 : 42 : auto_diagnostic_group d;
1612 : 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1613 : : "redeclaration %qD differs in %qs "
1614 : : "from previous declaration", new_decl,
1615 : : kind);
1616 : 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1617 : : "previous declaration %qD", old_decl);
1618 : 42 : return false;
1619 : 42 : }
1620 : : return true;
1621 : : }
1622 : :
1623 : : /* DECL is a redeclaration of a function or function template. If
1624 : : it does have default arguments issue a diagnostic. Note: this
1625 : : function is used to enforce the requirements in C++11 8.3.6 about
1626 : : no default arguments in redeclarations. */
1627 : :
1628 : : static void
1629 : 11299141 : check_redeclaration_no_default_args (tree decl)
1630 : : {
1631 : 11299141 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1632 : :
1633 : 11299141 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1634 : 33255992 : t && t != void_list_node; t = TREE_CHAIN (t))
1635 : 21956908 : if (TREE_PURPOSE (t))
1636 : : {
1637 : 57 : permerror (DECL_SOURCE_LOCATION (decl),
1638 : : "redeclaration of %q#D may not have default "
1639 : : "arguments", decl);
1640 : 57 : return;
1641 : : }
1642 : : }
1643 : :
1644 : : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1645 : : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1646 : : the TEMPLATE_DECLs in case of function templates). This function is used
1647 : : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1648 : : "If a friend declaration specifies a default argument expression, that
1649 : : declaration shall be a definition and shall be the only declaration of
1650 : : the function or function template in the translation unit." */
1651 : :
1652 : : static void
1653 : 6305319 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1654 : : {
1655 : 12229358 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1656 : : return;
1657 : :
1658 : 2115854 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1659 : 1057927 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1660 : 2844586 : t1 && t1 != void_list_node;
1661 : 1786659 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1662 : 2507001 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1663 : 4898003 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1664 : : {
1665 : 36 : auto_diagnostic_group d;
1666 : 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1667 : : "friend declaration of %q#D specifies default "
1668 : : "arguments and isn%'t the only declaration", newdecl))
1669 : 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1670 : : "previous declaration of %q#D", olddecl);
1671 : 36 : return;
1672 : 36 : }
1673 : : }
1674 : :
1675 : : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1676 : : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1677 : :
1678 : : static void
1679 : 18114465 : merge_attribute_bits (tree newdecl, tree olddecl)
1680 : : {
1681 : 18114465 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1682 : 18114465 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1683 : 18114465 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1684 : 18114465 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1685 : 18114465 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1686 : 18114465 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1687 : 18114465 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1688 : 18114465 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1689 : 18114465 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1690 : 18114465 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1691 : 18114465 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1692 : 18114465 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1693 : 18114465 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1694 : 18114465 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1695 : 18114465 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1696 : 18114465 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1697 : 18114465 : }
1698 : :
1699 : : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1700 : : && lookup_attribute ("gnu_inline", \
1701 : : DECL_ATTRIBUTES (fn)))
1702 : :
1703 : : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1704 : : ambiguates olddecl. Returns true if an error occurs. */
1705 : :
1706 : : static bool
1707 : 201689370 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1708 : : {
1709 : 201689370 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1710 : 201689370 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1711 : : /* Function template declarations can be differentiated by parameter
1712 : : and return type. */
1713 : 201689370 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1714 : 201689370 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1715 : 201689370 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1716 : : TREE_TYPE (TREE_TYPE (olddecl))))
1717 : : {
1718 : : /* ... and also by their template-heads and requires-clauses. */
1719 : 4225 : if (template_heads_equivalent_p (newdecl, olddecl)
1720 : 4225 : && function_requirements_equivalent_p (newres, oldres))
1721 : : {
1722 : 0 : auto_diagnostic_group d;
1723 : 0 : error ("ambiguating new declaration %q+#D", newdecl);
1724 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1725 : : "old declaration %q#D", olddecl);
1726 : 0 : return true;
1727 : 0 : }
1728 : :
1729 : : /* FIXME: The types are the same but the are differences
1730 : : in either the template heads or function requirements.
1731 : : We should be able to diagnose a set of common errors
1732 : : stemming from these declarations. For example:
1733 : :
1734 : : template<typename T> requires C void f(...);
1735 : : template<typename T> void f(...) requires C;
1736 : :
1737 : : These are functionally equivalent but not equivalent. */
1738 : : }
1739 : :
1740 : : return false;
1741 : : }
1742 : :
1743 : : /* OLD_PARMS is the innermost set of template parameters for some template
1744 : : declaration, and NEW_PARMS is the corresponding set of template parameters
1745 : : for a redeclaration of that template. Merge the default arguments within
1746 : : these two sets of parameters. CLASS_P is true iff the template in
1747 : : question is a class template. */
1748 : :
1749 : : bool
1750 : 8120621 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1751 : : {
1752 : 8120621 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1753 : : == TREE_VEC_LENGTH (old_parms));
1754 : 22990599 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1755 : : {
1756 : 14870008 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1757 : 14870008 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1758 : 14870008 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1759 : 14870008 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1760 : 14870008 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1761 : : return false;
1762 : 14870005 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1763 : : {
1764 : 27 : auto_diagnostic_group d;
1765 : 27 : error ("redefinition of default argument for %q+#D", new_parm);
1766 : 27 : inform (DECL_SOURCE_LOCATION (old_parm),
1767 : : "original definition appeared here");
1768 : 27 : return false;
1769 : 27 : }
1770 : 14869978 : else if (new_default != NULL_TREE)
1771 : : /* Update the previous template parameters (which are the ones
1772 : : that will really count) with the new default value. */
1773 : 199481 : old_default = new_default;
1774 : 14670497 : else if (class_p && old_default != NULL_TREE)
1775 : : /* Update the new parameters, too; they'll be used as the
1776 : : parameters for any members. */
1777 : 1023821 : new_default = old_default;
1778 : : }
1779 : : return true;
1780 : : }
1781 : :
1782 : : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1783 : : If the redeclaration is invalid, a diagnostic is issued, and the
1784 : : error_mark_node is returned. Otherwise, OLDDECL is returned.
1785 : :
1786 : : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1787 : : returned.
1788 : :
1789 : : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1790 : : if the old decl was hidden.
1791 : :
1792 : : Hidden decls can be anticipated builtins, injected friends, or
1793 : : (coming soon) injected from a local-extern decl. */
1794 : :
1795 : : tree
1796 : 346907743 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1797 : : {
1798 : 346907743 : unsigned olddecl_uid = DECL_UID (olddecl);
1799 : 346907743 : int types_match = 0;
1800 : 346907743 : int new_defines_function = 0;
1801 : 346907743 : tree new_template_info;
1802 : 346907743 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1803 : 346907743 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1804 : :
1805 : 346907743 : if (newdecl == olddecl)
1806 : : return olddecl;
1807 : :
1808 : 346054687 : types_match = decls_match (newdecl, olddecl);
1809 : :
1810 : : /* If either the type of the new decl or the type of the old decl is an
1811 : : error_mark_node, then that implies that we have already issued an
1812 : : error (earlier) for some bogus type specification, and in that case,
1813 : : it is rather pointless to harass the user with yet more error message
1814 : : about the same declaration, so just pretend the types match here. */
1815 : 346054687 : if (TREE_TYPE (newdecl) == error_mark_node
1816 : 346054687 : || TREE_TYPE (olddecl) == error_mark_node)
1817 : : return error_mark_node;
1818 : :
1819 : : /* Check for redeclaration and other discrepancies. */
1820 : 346054662 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1821 : 346054662 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1822 : : {
1823 : 6404624 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1824 : : {
1825 : : /* Avoid warnings redeclaring built-ins which have not been
1826 : : explicitly declared. */
1827 : 187902 : if (was_hidden)
1828 : : {
1829 : 187874 : if (TREE_PUBLIC (newdecl)
1830 : 187874 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1831 : 12 : warning_at (newdecl_loc,
1832 : 12 : OPT_Wbuiltin_declaration_mismatch,
1833 : : "built-in function %qD declared as non-function",
1834 : : newdecl);
1835 : 187874 : return NULL_TREE;
1836 : : }
1837 : :
1838 : : /* If you declare a built-in or predefined function name as static,
1839 : : the old definition is overridden, but optionally warn this was a
1840 : : bad choice of name. */
1841 : 28 : if (! TREE_PUBLIC (newdecl))
1842 : : {
1843 : 28 : warning_at (newdecl_loc,
1844 : 28 : OPT_Wshadow,
1845 : 28 : fndecl_built_in_p (olddecl)
1846 : : ? G_("shadowing built-in function %q#D")
1847 : : : G_("shadowing library function %q#D"), olddecl);
1848 : : /* Discard the old built-in function. */
1849 : 28 : return NULL_TREE;
1850 : : }
1851 : : /* If the built-in is not ansi, then programs can override
1852 : : it even globally without an error. */
1853 : 0 : else if (! fndecl_built_in_p (olddecl))
1854 : 0 : warning_at (newdecl_loc, 0,
1855 : : "library function %q#D redeclared as non-function %q#D",
1856 : : olddecl, newdecl);
1857 : : else
1858 : 0 : error_at (newdecl_loc,
1859 : : "declaration of %q#D conflicts with built-in "
1860 : : "declaration %q#D", newdecl, olddecl);
1861 : 0 : return NULL_TREE;
1862 : : }
1863 : 6216722 : else if (!types_match)
1864 : : {
1865 : : /* Avoid warnings redeclaring built-ins which have not been
1866 : : explicitly declared. */
1867 : 2180875 : if (was_hidden)
1868 : : {
1869 : 830951 : tree t1, t2;
1870 : :
1871 : : /* A new declaration doesn't match a built-in one unless it
1872 : : is also extern "C". */
1873 : 830951 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1874 : 830951 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1875 : 830951 : if (!DECL_EXTERN_C_P (newdecl))
1876 : : return NULL_TREE;
1877 : :
1878 : 143626 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1879 : 143626 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1880 : 246664 : t1 || t2;
1881 : 103038 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1882 : : {
1883 : 246646 : if (!t1 || !t2)
1884 : : break;
1885 : : /* FILE, tm types are not known at the time
1886 : : we create the builtins. */
1887 : 766578 : for (unsigned i = 0;
1888 : 1013062 : i < sizeof (builtin_structptr_types)
1889 : : / sizeof (builtin_structptr_type);
1890 : : ++i)
1891 : 910018 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1892 : : {
1893 : 143440 : tree t = TREE_VALUE (t1);
1894 : :
1895 : 143440 : if (TYPE_PTR_P (t)
1896 : 286880 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1897 : 143440 : == get_identifier (builtin_structptr_types[i].str)
1898 : 286865 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1899 : : {
1900 : 143425 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1901 : :
1902 : 143425 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1903 : 143425 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1904 : 143425 : types_match = decls_match (newdecl, olddecl);
1905 : 143425 : if (types_match)
1906 : 143425 : return duplicate_decls (newdecl, olddecl,
1907 : 143425 : hiding, was_hidden);
1908 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1909 : : }
1910 : 15 : goto next_arg;
1911 : : }
1912 : :
1913 : 103044 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1914 : : break;
1915 : 103038 : next_arg:;
1916 : : }
1917 : :
1918 : 201 : warning_at (newdecl_loc,
1919 : 201 : OPT_Wbuiltin_declaration_mismatch,
1920 : : "declaration of %q#D conflicts with built-in "
1921 : : "declaration %q#D", newdecl, olddecl);
1922 : : }
1923 : 1349924 : else if ((DECL_EXTERN_C_P (newdecl)
1924 : 15 : && DECL_EXTERN_C_P (olddecl))
1925 : 2699833 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1926 : 1349909 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1927 : : {
1928 : : /* Don't really override olddecl for __* prefixed builtins
1929 : : except for __[^b]*_chk, the compiler might be using those
1930 : : explicitly. */
1931 : 15 : if (fndecl_built_in_p (olddecl))
1932 : : {
1933 : 15 : tree id = DECL_NAME (olddecl);
1934 : 15 : const char *name = IDENTIFIER_POINTER (id);
1935 : 15 : size_t len;
1936 : :
1937 : 15 : if (name[0] == '_'
1938 : 15 : && name[1] == '_'
1939 : 30 : && (startswith (name + 2, "builtin_")
1940 : 6 : || (len = strlen (name)) <= strlen ("___chk")
1941 : 6 : || memcmp (name + len - strlen ("_chk"),
1942 : : "_chk", strlen ("_chk") + 1) != 0))
1943 : : {
1944 : 15 : if (DECL_INITIAL (newdecl))
1945 : : {
1946 : 6 : error_at (newdecl_loc,
1947 : : "definition of %q#D ambiguates built-in "
1948 : : "declaration %q#D", newdecl, olddecl);
1949 : 6 : return error_mark_node;
1950 : : }
1951 : 9 : auto_diagnostic_group d;
1952 : 9 : if (permerror (newdecl_loc,
1953 : : "new declaration %q#D ambiguates built-in"
1954 : : " declaration %q#D", newdecl, olddecl)
1955 : 9 : && flag_permissive)
1956 : 3 : inform (newdecl_loc,
1957 : : "ignoring the %q#D declaration", newdecl);
1958 : 9 : return flag_permissive ? olddecl : error_mark_node;
1959 : 9 : }
1960 : : }
1961 : :
1962 : : /* A near match; override the builtin. */
1963 : :
1964 : 0 : if (TREE_PUBLIC (newdecl))
1965 : 0 : warning_at (newdecl_loc,
1966 : 0 : OPT_Wbuiltin_declaration_mismatch,
1967 : : "new declaration %q#D ambiguates built-in "
1968 : : "declaration %q#D", newdecl, olddecl);
1969 : : else
1970 : 0 : warning (OPT_Wshadow,
1971 : : fndecl_built_in_p (olddecl)
1972 : : ? G_("shadowing built-in function %q#D")
1973 : : : G_("shadowing library function %q#D"), olddecl);
1974 : : }
1975 : : else
1976 : : /* Discard the old built-in function. */
1977 : : return NULL_TREE;
1978 : :
1979 : : /* Replace the old RTL to avoid problems with inlining. */
1980 : 201 : COPY_DECL_RTL (newdecl, olddecl);
1981 : : }
1982 : : else
1983 : : {
1984 : : /* Even if the types match, prefer the new declarations type
1985 : : for built-ins which have not been explicitly declared,
1986 : : for exception lists, etc... */
1987 : 4035847 : tree type = TREE_TYPE (newdecl);
1988 : 4035847 : tree attribs = (*targetm.merge_type_attributes)
1989 : 4035847 : (TREE_TYPE (olddecl), type);
1990 : :
1991 : 4035847 : type = cp_build_type_attribute_variant (type, attribs);
1992 : 4035847 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1993 : : }
1994 : :
1995 : : /* If a function is explicitly declared "throw ()", propagate that to
1996 : : the corresponding builtin. */
1997 : 4036048 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1998 : 3877889 : && was_hidden
1999 : 3877846 : && TREE_NOTHROW (newdecl)
2000 : 7654617 : && !TREE_NOTHROW (olddecl))
2001 : : {
2002 : 29155 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
2003 : 29155 : tree tmpdecl = builtin_decl_explicit (fncode);
2004 : 29155 : if (tmpdecl && tmpdecl != olddecl && types_match)
2005 : 29155 : TREE_NOTHROW (tmpdecl) = 1;
2006 : : }
2007 : :
2008 : : /* Whether or not the builtin can throw exceptions has no
2009 : : bearing on this declarator. */
2010 : 4036048 : TREE_NOTHROW (olddecl) = 0;
2011 : :
2012 : 4036048 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2013 : : {
2014 : : /* If a builtin function is redeclared as `static', merge
2015 : : the declarations, but make the original one static. */
2016 : 3 : DECL_THIS_STATIC (olddecl) = 1;
2017 : 3 : TREE_PUBLIC (olddecl) = 0;
2018 : :
2019 : : /* Make the old declaration consistent with the new one so
2020 : : that all remnants of the builtin-ness of this function
2021 : : will be banished. */
2022 : 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2023 : 3 : COPY_DECL_RTL (newdecl, olddecl);
2024 : : }
2025 : : }
2026 : 339650038 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2027 : : {
2028 : : /* C++ Standard, 3.3, clause 4:
2029 : : "[Note: a namespace name or a class template name must be unique
2030 : : in its declarative region (7.3.2, clause 14). ]" */
2031 : 85546115 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
2032 : 85546088 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
2033 : : /* Namespace conflicts with not namespace. */;
2034 : 49319519 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
2035 : 134865595 : || DECL_TYPE_TEMPLATE_P (newdecl))
2036 : : /* Class template conflicts. */;
2037 : 85546070 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2038 : 49319519 : && DECL_TEMPLATE_RESULT (olddecl)
2039 : 49319519 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2040 : 134865583 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
2041 : 36183481 : && DECL_TEMPLATE_RESULT (newdecl)
2042 : 36183481 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2043 : : /* Variable template conflicts. */;
2044 : 85546061 : else if (concept_definition_p (olddecl)
2045 : 171092116 : || concept_definition_p (newdecl))
2046 : : /* Concept conflicts. */;
2047 : 85546049 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2048 : 49359810 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
2049 : 85586352 : || (TREE_CODE (olddecl) == FUNCTION_DECL
2050 : 36183488 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2051 : : {
2052 : : /* One is a function and the other is a template
2053 : : function. */
2054 : 85502964 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2055 : : return NULL_TREE;
2056 : :
2057 : : /* There can only be one! */
2058 : 60163 : auto_diagnostic_group d;
2059 : 60163 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
2060 : 60163 : && check_raw_literal_operator (olddecl))
2061 : 3 : error_at (newdecl_loc,
2062 : : "literal operator %q#D conflicts with"
2063 : : " raw literal operator", newdecl);
2064 : 60160 : else if (check_raw_literal_operator (newdecl))
2065 : 3 : error_at (newdecl_loc,
2066 : : "raw literal operator %q#D conflicts with"
2067 : : " literal operator template", newdecl);
2068 : : else
2069 : : return NULL_TREE;
2070 : :
2071 : 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2072 : 6 : return error_mark_node;
2073 : 60163 : }
2074 : 43085 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2075 : : /* A structured binding must be unique in its declarative region. */;
2076 : 40444 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2077 : 43082 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
2078 : : /* One is an implicit typedef, that's ok. */
2079 : : return NULL_TREE;
2080 : :
2081 : 153 : auto_diagnostic_group d;
2082 : 153 : error ("%q#D redeclared as different kind of entity", newdecl);
2083 : 153 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
2084 : :
2085 : 153 : return error_mark_node;
2086 : 153 : }
2087 : 254103923 : else if (!types_match)
2088 : : {
2089 : 239000001 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
2090 : : /* These are certainly not duplicate declarations; they're
2091 : : from different scopes. */
2092 : : return NULL_TREE;
2093 : :
2094 : 238999941 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2095 : : {
2096 : 201689388 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2097 : 201689388 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
2098 : :
2099 : : /* The name of a class template may not be declared to refer to
2100 : : any other template, class, function, object, namespace, value,
2101 : : or type in the same scope. */
2102 : 201689388 : if (TREE_CODE (oldres) == TYPE_DECL
2103 : 201689382 : || TREE_CODE (newres) == TYPE_DECL)
2104 : : {
2105 : 9 : auto_diagnostic_group d;
2106 : 9 : error_at (newdecl_loc,
2107 : : "conflicting declaration of template %q#D", newdecl);
2108 : 9 : inform (olddecl_loc,
2109 : : "previous declaration %q#D", olddecl);
2110 : 9 : return error_mark_node;
2111 : 9 : }
2112 : :
2113 : 201689379 : else if (TREE_CODE (oldres) == FUNCTION_DECL
2114 : 201689370 : && TREE_CODE (newres) == FUNCTION_DECL)
2115 : : {
2116 : 201689370 : if (duplicate_function_template_decls (newdecl, olddecl))
2117 : 0 : return error_mark_node;
2118 : : return NULL_TREE;
2119 : : }
2120 : : return NULL_TREE;
2121 : : }
2122 : 37310553 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2123 : : {
2124 : 37310405 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2125 : : {
2126 : 57 : auto_diagnostic_group d;
2127 : 57 : error_at (newdecl_loc,
2128 : : "conflicting declaration of C function %q#D",
2129 : : newdecl);
2130 : 57 : inform (olddecl_loc,
2131 : : "previous declaration %q#D", olddecl);
2132 : 57 : return error_mark_node;
2133 : 57 : }
2134 : : /* For function versions, params and types match, but they
2135 : : are not ambiguous. */
2136 : 37310348 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2137 : 37302080 : && !DECL_FUNCTION_VERSIONED (olddecl))
2138 : 8313 : || !same_type_p (fndecl_declared_return_type (newdecl),
2139 : : fndecl_declared_return_type (olddecl)))
2140 : : /* Let constrained hidden friends coexist for now, we'll
2141 : : check satisfaction later. */
2142 : 37302035 : && !member_like_constrained_friend_p (newdecl)
2143 : 36854414 : && !member_like_constrained_friend_p (olddecl)
2144 : : // The functions have the same parameter types.
2145 : 36821110 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2146 : 36821110 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2147 : : // And the same constraints.
2148 : 37310417 : && equivalently_constrained (newdecl, olddecl))
2149 : : {
2150 : 69 : auto_diagnostic_group d;
2151 : 69 : error_at (newdecl_loc,
2152 : : "ambiguating new declaration of %q#D", newdecl);
2153 : 69 : inform (olddecl_loc,
2154 : : "old declaration %q#D", olddecl);
2155 : 69 : return error_mark_node;
2156 : 69 : }
2157 : : else
2158 : 37310279 : return NULL_TREE;
2159 : : }
2160 : : else
2161 : : {
2162 : 148 : auto_diagnostic_group d;
2163 : 148 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2164 : 148 : inform (olddecl_loc,
2165 : : "previous declaration as %q#D", olddecl);
2166 : 148 : return error_mark_node;
2167 : 148 : }
2168 : : }
2169 : 15103922 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2170 : 15103922 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2171 : : {
2172 : : /* OMP UDRs are never duplicates. */
2173 : 27 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2174 : 27 : auto_diagnostic_group d;
2175 : 27 : error_at (newdecl_loc,
2176 : : "redeclaration of %<pragma omp declare reduction%>");
2177 : 27 : inform (olddecl_loc,
2178 : : "previous %<pragma omp declare reduction%> declaration");
2179 : 27 : return error_mark_node;
2180 : 27 : }
2181 : 15103895 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2182 : 15103895 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2183 : 220 : && (!DECL_TEMPLATE_INFO (newdecl)
2184 : 220 : || (DECL_TI_TEMPLATE (newdecl)
2185 : 220 : != DECL_TI_TEMPLATE (olddecl))))
2186 : 11055889 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2187 : 572323 : && (!DECL_TEMPLATE_INFO (olddecl)
2188 : 572323 : || (DECL_TI_TEMPLATE (olddecl)
2189 : 572323 : != DECL_TI_TEMPLATE (newdecl))))))
2190 : : /* It's OK to have a template specialization and a non-template
2191 : : with the same type, or to have specializations of two
2192 : : different templates with the same type. Note that if one is a
2193 : : specialization, and the other is an instantiation of the same
2194 : : template, that we do not exit at this point. That situation
2195 : : can occur if we instantiate a template class, and then
2196 : : specialize one of its methods. This situation is valid, but
2197 : : the declarations must be merged in the usual way. */
2198 : : return NULL_TREE;
2199 : 15103895 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2200 : 15103895 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2201 : 595035 : && !DECL_USE_TEMPLATE (newdecl))
2202 : 11055889 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2203 : 22932 : && !DECL_USE_TEMPLATE (olddecl))))
2204 : : /* One of the declarations is a template instantiation, and the
2205 : : other is not a template at all. That's OK. */
2206 : : return NULL_TREE;
2207 : 15103895 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2208 : : {
2209 : : /* In [namespace.alias] we have:
2210 : :
2211 : : In a declarative region, a namespace-alias-definition can be
2212 : : used to redefine a namespace-alias declared in that declarative
2213 : : region to refer only to the namespace to which it already
2214 : : refers.
2215 : :
2216 : : Therefore, if we encounter a second alias directive for the same
2217 : : alias, we can just ignore the second directive. */
2218 : 21 : if (DECL_NAMESPACE_ALIAS (newdecl)
2219 : 21 : && (DECL_NAMESPACE_ALIAS (newdecl)
2220 : 18 : == DECL_NAMESPACE_ALIAS (olddecl)))
2221 : : return olddecl;
2222 : :
2223 : : /* Leave it to update_binding to merge or report error. */
2224 : : return NULL_TREE;
2225 : : }
2226 : : /* Check if the two decls are non-mergeable versioned decls. */
2227 : 15103874 : else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2228 : : && diagnose_versioned_decls (olddecl, newdecl))
2229 : : return error_mark_node;
2230 : : else
2231 : : {
2232 : 15103874 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2233 : 15103874 : if (errmsg)
2234 : : {
2235 : 617 : auto_diagnostic_group d;
2236 : 617 : error_at (newdecl_loc, errmsg, newdecl);
2237 : 617 : if (DECL_NAME (olddecl) != NULL_TREE)
2238 : 617 : inform (olddecl_loc,
2239 : 617 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2240 : : ? G_("%q#D previously defined here")
2241 : : : G_("%q#D previously declared here"), olddecl);
2242 : 617 : if (cxx_dialect >= cxx26
2243 : 147 : && DECL_NAME (newdecl)
2244 : 147 : && id_equal (DECL_NAME (newdecl), "_")
2245 : 630 : && !name_independent_decl_p (newdecl))
2246 : : {
2247 : 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2248 : 2 : inform (newdecl_loc,
2249 : : "parameter declaration is not name-independent");
2250 : 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2251 : : {
2252 : 3 : if (at_namespace_scope_p ())
2253 : 2 : inform (newdecl_loc,
2254 : : "structured binding at namespace scope is not "
2255 : : "name-independent");
2256 : 1 : else if (TREE_STATIC (newdecl))
2257 : 1 : inform (newdecl_loc,
2258 : : "static structured binding is not "
2259 : : "name-independent");
2260 : 0 : else if (DECL_EXTERNAL (newdecl))
2261 : 0 : inform (newdecl_loc,
2262 : : "extern structured binding is not "
2263 : : "name-independent");
2264 : : }
2265 : 8 : else if (at_class_scope_p ()
2266 : 0 : && VAR_P (newdecl)
2267 : 8 : && TREE_STATIC (newdecl))
2268 : 0 : inform (newdecl_loc,
2269 : : "static data member is not name-independent");
2270 : 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2271 : 3 : inform (newdecl_loc,
2272 : : "variable at namespace scope is not name-independent");
2273 : 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2274 : 3 : inform (newdecl_loc,
2275 : : "static variable is not name-independent");
2276 : 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2277 : 0 : inform (newdecl_loc,
2278 : : "extern variable is not name-independent");
2279 : : }
2280 : 617 : return error_mark_node;
2281 : 617 : }
2282 : 15103257 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2283 : 11055652 : && DECL_INITIAL (olddecl) != NULL_TREE
2284 : 11812 : && !prototype_p (TREE_TYPE (olddecl))
2285 : 15103257 : && prototype_p (TREE_TYPE (newdecl)))
2286 : : {
2287 : : /* Prototype decl follows defn w/o prototype. */
2288 : 0 : auto_diagnostic_group d;
2289 : 0 : if (warning_at (newdecl_loc, 0,
2290 : : "prototype specified for %q#D", newdecl))
2291 : 0 : inform (olddecl_loc,
2292 : : "previous non-prototype definition here");
2293 : 0 : }
2294 : 14708535 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2295 : 26158909 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2296 : : {
2297 : : /* [dcl.link]
2298 : : If two declarations of the same function or object
2299 : : specify different linkage-specifications ..., the program
2300 : : is ill-formed.... Except for functions with C++ linkage,
2301 : : a function declaration without a linkage specification
2302 : : shall not precede the first linkage specification for
2303 : : that function. A function can be declared without a
2304 : : linkage specification after an explicit linkage
2305 : : specification has been seen; the linkage explicitly
2306 : : specified in the earlier declaration is not affected by
2307 : : such a function declaration.
2308 : :
2309 : : DR 563 raises the question why the restrictions on
2310 : : functions should not also apply to objects. Older
2311 : : versions of G++ silently ignore the linkage-specification
2312 : : for this example:
2313 : :
2314 : : namespace N {
2315 : : extern int i;
2316 : : extern "C" int i;
2317 : : }
2318 : :
2319 : : which is clearly wrong. Therefore, we now treat objects
2320 : : like functions. */
2321 : 1781 : if (current_lang_depth () == 0)
2322 : : {
2323 : : /* There is no explicit linkage-specification, so we use
2324 : : the linkage from the previous declaration. */
2325 : 1769 : retrofit_lang_decl (newdecl);
2326 : 1769 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2327 : : }
2328 : : else
2329 : : {
2330 : 12 : auto_diagnostic_group d;
2331 : 12 : error_at (newdecl_loc,
2332 : : "conflicting declaration of %q#D with %qL linkage",
2333 : 12 : newdecl, DECL_LANGUAGE (newdecl));
2334 : 12 : inform (olddecl_loc,
2335 : : "previous declaration with %qL linkage",
2336 : 12 : DECL_LANGUAGE (olddecl));
2337 : 12 : }
2338 : : }
2339 : :
2340 : 15103257 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2341 : : ;
2342 : 14486636 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2343 : : {
2344 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2345 : 12774509 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2346 : 10460406 : && (/* grokfndecl passes member function templates too
2347 : : as FUNCTION_DECLs. */
2348 : 8310233 : DECL_TEMPLATE_INFO (olddecl)
2349 : : /* C++11 8.3.6/6.
2350 : : Default arguments for a member function of a class
2351 : : template shall be specified on the initial declaration
2352 : : of the member function within the class template. */
2353 : 663719 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2354 : : {
2355 : 7727114 : check_redeclaration_no_default_args (newdecl);
2356 : :
2357 : 7727114 : if (DECL_TEMPLATE_INFO (olddecl)
2358 : 7727114 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2359 : : {
2360 : 2160470 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2361 : 2160470 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2362 : 2160470 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2363 : 2160470 : tree old_parms
2364 : 2160470 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2365 : 2160470 : merge_default_template_args (new_parms, old_parms,
2366 : : /*class_p=*/false);
2367 : : }
2368 : : }
2369 : : else
2370 : : {
2371 : 2733292 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2372 : 2733292 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2373 : 2733292 : int i = 1;
2374 : :
2375 : 7025386 : for (; t1 && t1 != void_list_node;
2376 : 4292094 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2377 : 4293900 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2378 : : {
2379 : 57 : if (simple_cst_equal (TREE_PURPOSE (t1),
2380 : 57 : TREE_PURPOSE (t2)) == 1)
2381 : : {
2382 : 24 : auto_diagnostic_group d;
2383 : 24 : if (permerror (newdecl_loc,
2384 : : "default argument given for parameter "
2385 : : "%d of %q#D", i, newdecl))
2386 : 24 : inform (olddecl_loc,
2387 : : "previous specification in %q#D here",
2388 : : olddecl);
2389 : 24 : }
2390 : : else
2391 : : {
2392 : 33 : auto_diagnostic_group d;
2393 : 33 : error_at (newdecl_loc,
2394 : : "default argument given for parameter %d "
2395 : : "of %q#D", i, newdecl);
2396 : 33 : inform (olddecl_loc,
2397 : : "previous specification in %q#D here",
2398 : : olddecl);
2399 : 33 : }
2400 : : }
2401 : :
2402 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2403 : : argument expression, that declaration... shall be the only
2404 : : declaration of the function or function template in the
2405 : : translation unit." */
2406 : 2733292 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2407 : : }
2408 : : }
2409 : : }
2410 : :
2411 : : /* Do not merge an implicit typedef with an explicit one. In:
2412 : :
2413 : : class A;
2414 : : ...
2415 : : typedef class A A __attribute__ ((foo));
2416 : :
2417 : : the attribute should apply only to the typedef. */
2418 : 19139305 : if (TREE_CODE (olddecl) == TYPE_DECL
2419 : 19139305 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2420 : 26329 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2421 : : return NULL_TREE;
2422 : :
2423 : 19107895 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2424 : : return NULL_TREE;
2425 : :
2426 : 19107883 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2427 : 30 : return error_mark_node;
2428 : :
2429 : 19107853 : if (modules_p ()
2430 : 41416 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2431 : 19132624 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2432 : : {
2433 : 24771 : if (!module_may_redeclare (olddecl, newdecl))
2434 : 24 : return error_mark_node;
2435 : :
2436 : 24747 : if (!hiding)
2437 : : {
2438 : : /* The old declaration should match the exportingness of the new
2439 : : declaration. But hidden friend declarations just keep the
2440 : : exportingness of the old declaration; see CWG2588. */
2441 : 21928 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2442 : 21928 : if (DECL_LANG_SPECIFIC (not_tmpl)
2443 : 21883 : && DECL_MODULE_ATTACH_P (not_tmpl)
2444 : : /* Typedefs are not entities and so are OK to be redeclared
2445 : : as exported: see [module.interface]/p6. */
2446 : 22141 : && TREE_CODE (olddecl) != TYPE_DECL)
2447 : : {
2448 : 201 : if (DECL_MODULE_EXPORT_P (newdecl)
2449 : 201 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2450 : : {
2451 : 15 : auto_diagnostic_group d;
2452 : 15 : error ("conflicting exporting for declaration %qD", newdecl);
2453 : 15 : inform (olddecl_loc,
2454 : : "previously declared here without exporting");
2455 : 15 : }
2456 : : }
2457 : 21727 : else if (DECL_MODULE_EXPORT_P (newdecl))
2458 : 9048 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2459 : : }
2460 : : }
2461 : :
2462 : : /* We have committed to returning OLDDECL at this point. */
2463 : :
2464 : : /* If new decl is `static' and an `extern' was seen previously,
2465 : : warn about it. */
2466 : 19107829 : warn_extern_redeclared_static (newdecl, olddecl);
2467 : :
2468 : : /* True to merge attributes between the declarations, false to
2469 : : set OLDDECL's attributes to those of NEWDECL (for template
2470 : : explicit specializations that specify their own attributes
2471 : : independent of those specified for the primary template). */
2472 : 19107829 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2473 : 15091655 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2474 : 19680137 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2475 : :
2476 : 19107829 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2477 : : {
2478 : 15091655 : if (merge_attr)
2479 : : {
2480 : 14519552 : {
2481 : 14519552 : auto_diagnostic_group d;
2482 : 14519552 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2483 : 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2484 : : ? G_("previous definition of %qD here")
2485 : : : G_("previous declaration of %qD here"), olddecl);
2486 : 14519552 : }
2487 : :
2488 : : /* [dcl.attr.noreturn]: The first declaration of a function shall
2489 : : specify the noreturn attribute if any declaration of that function
2490 : : specifies the noreturn attribute. */
2491 : 14519552 : tree a;
2492 : 14519552 : if (TREE_THIS_VOLATILE (newdecl)
2493 : 82361 : && !TREE_THIS_VOLATILE (olddecl)
2494 : : /* This applies to [[noreturn]] only, not its GNU variants. */
2495 : 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2496 : 54 : && cxx11_attribute_p (a)
2497 : 14519573 : && get_attribute_namespace (a) == NULL_TREE)
2498 : : {
2499 : 9 : auto_diagnostic_group d;
2500 : 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2501 : : "but its first declaration was not", newdecl);
2502 : 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2503 : 9 : }
2504 : : }
2505 : :
2506 : : /* Now that functions must hold information normally held
2507 : : by field decls, there is extra work to do so that
2508 : : declaration information does not get destroyed during
2509 : : definition. */
2510 : 15091655 : if (DECL_VINDEX (olddecl))
2511 : 164523 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2512 : 15091655 : if (DECL_CONTEXT (olddecl))
2513 : 15091655 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2514 : 15091655 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2515 : 15091655 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2516 : 15091655 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2517 : 15091655 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2518 : 15091655 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2519 : 15091655 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2520 : 15091655 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2521 : 15091655 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2522 : 15091655 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2523 : 15091655 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2524 : 15091655 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2525 : 2527870 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2526 : 5055740 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2527 : 15091655 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2528 : :
2529 : 15091655 : duplicate_contracts (newdecl, olddecl);
2530 : :
2531 : : /* Optionally warn about more than one declaration for the same
2532 : : name, but don't warn about a function declaration followed by a
2533 : : definition. */
2534 : 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2535 : 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2536 : : /* Don't warn about extern decl followed by definition. */
2537 : 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2538 : : /* Don't warn if at least one is/was hidden. */
2539 : 27 : && !(hiding || was_hidden)
2540 : : /* Don't warn about declaration followed by specialization. */
2541 : 15091667 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2542 : 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2543 : : {
2544 : 6 : auto_diagnostic_group d;
2545 : 6 : if (warning_at (newdecl_loc,
2546 : 6 : OPT_Wredundant_decls,
2547 : : "redundant redeclaration of %qD in same scope",
2548 : : newdecl))
2549 : 6 : inform (olddecl_loc,
2550 : : "previous declaration of %qD", olddecl);
2551 : 6 : }
2552 : :
2553 : : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2554 : : first declaration of the function or, for an explicit specialization
2555 : : of a function template, the first declaration of that
2556 : : specialization. */
2557 : 15091655 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2558 : 595035 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2559 : : {
2560 : 14519552 : if (DECL_DELETED_FN (newdecl))
2561 : : {
2562 : 21 : auto_diagnostic_group d;
2563 : 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2564 : : "is not first declaration", newdecl))
2565 : 21 : inform (olddecl_loc,
2566 : : "previous declaration of %qD", olddecl);
2567 : 21 : }
2568 : 14519552 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2569 : 14519552 : if (DECL_DELETED_FN (olddecl)
2570 : 6 : && DECL_INITIAL (olddecl)
2571 : 14519558 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2572 : 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2573 : : }
2574 : : }
2575 : :
2576 : : /* Deal with C++: must preserve virtual function table size. */
2577 : 19107829 : if (TREE_CODE (olddecl) == TYPE_DECL)
2578 : : {
2579 : 26326 : tree newtype = TREE_TYPE (newdecl);
2580 : 26326 : tree oldtype = TREE_TYPE (olddecl);
2581 : :
2582 : 26326 : if (newtype != error_mark_node && oldtype != error_mark_node
2583 : 52652 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2584 : 66969 : CLASSTYPE_FRIEND_CLASSES (newtype)
2585 : 22323 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2586 : :
2587 : 26326 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2588 : : }
2589 : :
2590 : : /* Copy all the DECL_... slots specified in the new decl except for
2591 : : any that we copy here from the old type. */
2592 : 19107829 : if (merge_attr)
2593 : 18535726 : DECL_ATTRIBUTES (newdecl)
2594 : 37071452 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2595 : : else
2596 : 572103 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2597 : :
2598 : : /* Transfer purviewness and importingness to the old decl. */
2599 : 19107829 : transfer_defining_module (olddecl, newdecl);
2600 : :
2601 : 19107829 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2602 : : {
2603 : 3595126 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2604 : 3595126 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2605 : 3595126 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2606 : :
2607 : : /* The new decl should not already have gathered any
2608 : : specializations. */
2609 : 3595126 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2610 : :
2611 : : /* Make sure the contracts are equivalent. */
2612 : 3595126 : duplicate_contracts (newdecl, olddecl);
2613 : :
2614 : : /* Remove contracts from old_result so they aren't appended to
2615 : : old_result by the merge function. */
2616 : 3595126 : remove_contract_attributes (old_result);
2617 : :
2618 : 3595126 : DECL_ATTRIBUTES (old_result)
2619 : 3595126 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2620 : :
2621 : 3595126 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2622 : : {
2623 : 3595096 : if (DECL_SOURCE_LOCATION (newdecl)
2624 : 3595096 : != DECL_SOURCE_LOCATION (olddecl))
2625 : : {
2626 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2627 : : later declarations of a function template. */
2628 : 3572027 : check_redeclaration_no_default_args (newdecl);
2629 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2630 : : argument expression, that declaration... shall be the only
2631 : : declaration of the function or function template in the
2632 : : translation unit." */
2633 : 3572027 : check_no_redeclaration_friend_default_args
2634 : 3572027 : (old_result, new_result);
2635 : :
2636 : 3572027 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2637 : 3572027 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2638 : 3572027 : merge_default_template_args (new_parms, old_parms,
2639 : : /*class_p=*/false);
2640 : : }
2641 : 3595096 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2642 : 2935438 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2643 : :
2644 : 3595096 : check_default_args (newdecl);
2645 : :
2646 : 7190180 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2647 : 3595096 : && DECL_INITIAL (new_result))
2648 : : {
2649 : 24 : if (DECL_INITIAL (old_result))
2650 : 12 : DECL_UNINLINABLE (old_result) = 1;
2651 : : else
2652 : 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2653 : 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2654 : 72 : DECL_NOT_REALLY_EXTERN (old_result)
2655 : 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2656 : 48 : DECL_INTERFACE_KNOWN (old_result)
2657 : 24 : = DECL_INTERFACE_KNOWN (new_result);
2658 : 24 : DECL_DECLARED_INLINE_P (old_result)
2659 : 24 : = DECL_DECLARED_INLINE_P (new_result);
2660 : 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2661 : 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2662 : : }
2663 : : else
2664 : : {
2665 : 3595072 : DECL_DECLARED_INLINE_P (old_result)
2666 : 3595072 : |= DECL_DECLARED_INLINE_P (new_result);
2667 : 3595072 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2668 : 3595072 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2669 : 3595072 : check_redeclaration_exception_specification (newdecl, olddecl);
2670 : :
2671 : 3595072 : merge_attribute_bits (new_result, old_result);
2672 : : }
2673 : : }
2674 : :
2675 : : /* If the new declaration is a definition, update the file and
2676 : : line information on the declaration, and also make
2677 : : the old declaration the same definition. */
2678 : 3595126 : if (DECL_INITIAL (new_result) != NULL_TREE)
2679 : : {
2680 : 5446546 : DECL_SOURCE_LOCATION (olddecl)
2681 : 5446546 : = DECL_SOURCE_LOCATION (old_result)
2682 : 2723273 : = DECL_SOURCE_LOCATION (newdecl);
2683 : 2723273 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2684 : 2723273 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2685 : : {
2686 : 2723273 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2687 : 9732313 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2688 : 7009040 : DECL_CONTEXT (p) = old_result;
2689 : :
2690 : 5446546 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2691 : 14 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2692 : : }
2693 : : }
2694 : :
2695 : 3595126 : return olddecl;
2696 : : }
2697 : :
2698 : 15512703 : if (types_match)
2699 : : {
2700 : 15512502 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2701 : 15091454 : check_redeclaration_exception_specification (newdecl, olddecl);
2702 : :
2703 : : /* Automatically handles default parameters. */
2704 : 15512502 : tree oldtype = TREE_TYPE (olddecl);
2705 : 15512502 : tree newtype;
2706 : :
2707 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2708 : : at newdecl, which will be ggc_freed. */
2709 : 15512502 : if (TREE_CODE (newdecl) == TYPE_DECL)
2710 : : {
2711 : : /* But NEWTYPE might have an attribute, honor that. */
2712 : 26326 : tree tem = TREE_TYPE (newdecl);
2713 : 26326 : newtype = oldtype;
2714 : :
2715 : 26326 : if (TYPE_USER_ALIGN (tem))
2716 : : {
2717 : 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2718 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2719 : 21 : TYPE_USER_ALIGN (newtype) = true;
2720 : : }
2721 : :
2722 : : /* And remove the new type from the variants list. */
2723 : 26326 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2724 : : {
2725 : 24 : tree remove = TREE_TYPE (newdecl);
2726 : 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2727 : : {
2728 : 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2729 : : /* If remove is the main variant, no need to remove that
2730 : : from the list. One of the DECL_ORIGINAL_TYPE
2731 : : variants, e.g. created for aligned attribute, might still
2732 : : refer to the newdecl TYPE_DECL though, so remove that one
2733 : : in that case. */
2734 : 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2735 : 6 : if (orig != remove)
2736 : 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2737 : 0 : t = TYPE_MAIN_VARIANT (t))
2738 : 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2739 : : {
2740 : 12 : TYPE_NEXT_VARIANT (t)
2741 : 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2742 : 6 : break;
2743 : : }
2744 : : }
2745 : : else
2746 : 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2747 : 0 : t = TYPE_NEXT_VARIANT (t))
2748 : 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2749 : : {
2750 : 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2751 : 15 : break;
2752 : : }
2753 : : }
2754 : : }
2755 : 15486176 : else if (merge_attr)
2756 : 14914073 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2757 : : else
2758 : 572103 : newtype = TREE_TYPE (newdecl);
2759 : :
2760 : 15512502 : if (VAR_P (newdecl))
2761 : : {
2762 : 394722 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2763 : : /* For already initialized vars, TREE_READONLY could have been
2764 : : cleared in cp_finish_decl, because the var needs runtime
2765 : : initialization or destruction. Make sure not to set
2766 : : TREE_READONLY on it again. */
2767 : 394722 : if (DECL_INITIALIZED_P (olddecl)
2768 : 1213 : && !DECL_EXTERNAL (olddecl)
2769 : 394807 : && !TREE_READONLY (olddecl))
2770 : 30 : TREE_READONLY (newdecl) = 0;
2771 : 394722 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2772 : 789444 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2773 : 394722 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2774 : 394722 : if (DECL_DEPENDENT_INIT_P (olddecl))
2775 : 105197 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2776 : 789444 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2777 : 394722 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2778 : 394722 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2779 : 394722 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2780 : 789444 : DECL_DECLARED_CONSTINIT_P (newdecl)
2781 : 394722 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2782 : :
2783 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2784 : 394722 : if (DECL_LANG_SPECIFIC (olddecl)
2785 : 785865 : && CP_DECL_THREADPRIVATE_P (olddecl))
2786 : : {
2787 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2788 : 41 : retrofit_lang_decl (newdecl);
2789 : 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2790 : : }
2791 : : }
2792 : :
2793 : : /* An explicit specialization of a function template or of a member
2794 : : function of a class template can be declared transaction_safe
2795 : : independently of whether the corresponding template entity is declared
2796 : : transaction_safe. */
2797 : 1147 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2798 : 1045 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2799 : 57 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2800 : 51 : && tx_safe_fn_type_p (newtype)
2801 : 15512502 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2802 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2803 : :
2804 : 15512502 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2805 : :
2806 : 15512502 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2807 : 15091454 : check_default_args (newdecl);
2808 : :
2809 : : /* Lay the type out, unless already done. */
2810 : 15512502 : if (! same_type_p (newtype, oldtype)
2811 : 366 : && TREE_TYPE (newdecl) != error_mark_node
2812 : 15512868 : && !(processing_template_decl && uses_template_parms (newdecl)))
2813 : 285 : layout_type (TREE_TYPE (newdecl));
2814 : :
2815 : 15512502 : if ((VAR_P (newdecl)
2816 : 15117780 : || TREE_CODE (newdecl) == PARM_DECL
2817 : 15117780 : || TREE_CODE (newdecl) == RESULT_DECL
2818 : 15117780 : || TREE_CODE (newdecl) == FIELD_DECL
2819 : 15117780 : || TREE_CODE (newdecl) == TYPE_DECL)
2820 : 15538828 : && !(processing_template_decl && uses_template_parms (newdecl)))
2821 : 259283 : layout_decl (newdecl, 0);
2822 : :
2823 : : /* Merge deprecatedness. */
2824 : 15512502 : if (TREE_DEPRECATED (newdecl))
2825 : 11929 : TREE_DEPRECATED (olddecl) = 1;
2826 : :
2827 : : /* Merge unavailability. */
2828 : 15512502 : if (TREE_UNAVAILABLE (newdecl))
2829 : 3 : TREE_UNAVAILABLE (olddecl) = 1;
2830 : :
2831 : : /* Preserve function specific target and optimization options */
2832 : 15512502 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2833 : : {
2834 : 15091454 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2835 : 15091454 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2836 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2837 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2838 : :
2839 : 15091454 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2840 : 15091454 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2841 : 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2842 : 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2843 : :
2844 : 15091454 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2845 : 14905000 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2846 : : }
2847 : : else
2848 : : {
2849 : : /* Merge the const type qualifier. */
2850 : 421048 : if (TREE_READONLY (newdecl))
2851 : 43389 : TREE_READONLY (olddecl) = 1;
2852 : : /* Merge the volatile type qualifier. */
2853 : 421048 : if (TREE_THIS_VOLATILE (newdecl))
2854 : 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2855 : : }
2856 : :
2857 : : /* Merge the initialization information. */
2858 : 15512502 : if (DECL_INITIAL (newdecl) == NULL_TREE
2859 : 15512502 : && DECL_INITIAL (olddecl) != NULL_TREE)
2860 : : {
2861 : 175748 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2862 : 175748 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2863 : 175748 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2864 : : {
2865 : 11728 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2866 : 11728 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2867 : : }
2868 : : }
2869 : :
2870 : 15512502 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2871 : : {
2872 : 15091454 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2873 : 15091454 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2874 : 15091454 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2875 : 15091454 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2876 : 53458 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2877 : 15091454 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2878 : 15091454 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2879 : 15091454 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2880 : 15091454 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2881 : :
2882 : 15091454 : if (merge_attr)
2883 : 14519351 : merge_attribute_bits (newdecl, olddecl);
2884 : : else
2885 : : {
2886 : : /* Merge the noreturn bit. */
2887 : 572103 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2888 : 572103 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2889 : 572103 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2890 : 572103 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2891 : 572103 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2892 : : }
2893 : : /* Keep the old RTL. */
2894 : 15091454 : COPY_DECL_RTL (olddecl, newdecl);
2895 : : }
2896 : 421048 : else if (VAR_P (newdecl)
2897 : 421048 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2898 : : {
2899 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2900 : : declaration was for an incomplete object and the new
2901 : : declaration is not since many attributes of the RTL will
2902 : : change. */
2903 : 394648 : COPY_DECL_RTL (olddecl, newdecl);
2904 : : }
2905 : : }
2906 : : /* If cannot merge, then use the new type and qualifiers,
2907 : : and don't preserve the old rtl. */
2908 : : else
2909 : : {
2910 : : /* Clean out any memory we had of the old declaration. */
2911 : 201 : tree oldstatic = value_member (olddecl, static_aggregates);
2912 : 201 : if (oldstatic)
2913 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2914 : :
2915 : 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2916 : 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2917 : 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2918 : 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2919 : 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2920 : : }
2921 : :
2922 : : /* Merge the storage class information. */
2923 : 15512703 : merge_weak (newdecl, olddecl);
2924 : :
2925 : 15512703 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2926 : 15512703 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2927 : 15512703 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2928 : 15512703 : if (! DECL_EXTERNAL (olddecl))
2929 : 30952 : DECL_EXTERNAL (newdecl) = 0;
2930 : 15512703 : if (! DECL_COMDAT (olddecl))
2931 : 6732226 : DECL_COMDAT (newdecl) = 0;
2932 : :
2933 : 15512703 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2934 : : {
2935 : 78 : if (!DECL_LOCAL_DECL_P (olddecl))
2936 : : /* This can happen if olddecl was brought in from the
2937 : : enclosing namespace via a using-decl. The new decl is
2938 : : then not a block-scope extern at all. */
2939 : 3 : DECL_LOCAL_DECL_P (newdecl) = false;
2940 : : else
2941 : : {
2942 : 75 : retrofit_lang_decl (newdecl);
2943 : 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2944 : 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2945 : 75 : if (alias != error_mark_node)
2946 : : {
2947 : 72 : DECL_ATTRIBUTES (alias)
2948 : 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2949 : 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2950 : 42 : merge_attribute_bits (newdecl, alias);
2951 : : }
2952 : : }
2953 : : }
2954 : :
2955 : 15512703 : new_template_info = NULL_TREE;
2956 : 15512703 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2957 : : {
2958 : 15482804 : bool new_redefines_gnu_inline = false;
2959 : :
2960 : 15482804 : if (new_defines_function
2961 : 15482804 : && ((DECL_INTERFACE_KNOWN (olddecl)
2962 : 1234 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2963 : 9081832 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2964 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2965 : : == FUNCTION_DECL))))
2966 : 2468 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2967 : :
2968 : : if (!new_redefines_gnu_inline)
2969 : : {
2970 : 15482726 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2971 : 15482726 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2972 : 15482726 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2973 : : }
2974 : :
2975 : 15482804 : if (TREE_CODE (newdecl) != TYPE_DECL)
2976 : : {
2977 : 15482752 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2978 : 15482752 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2979 : 15482752 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2980 : :
2981 : : /* If the OLDDECL is an instantiation and/or specialization,
2982 : : then the NEWDECL must be too. But, it may not yet be marked
2983 : : as such if the caller has created NEWDECL, but has not yet
2984 : : figured out that it is a redeclaration. */
2985 : 15482752 : if (!DECL_USE_TEMPLATE (newdecl))
2986 : 14887503 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2987 : :
2988 : 15482752 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2989 : 14910425 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2990 : 29820850 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2991 : : }
2992 : :
2993 : : /* Don't really know how much of the language-specific
2994 : : values we should copy from old to new. */
2995 : 15482804 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2996 : :
2997 : 15482804 : if (LANG_DECL_HAS_MIN (newdecl))
2998 : : {
2999 : 15482804 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3000 : 15482804 : if (DECL_TEMPLATE_INFO (newdecl))
3001 : : {
3002 : 606852 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
3003 : 606852 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3004 : 606852 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3005 : : /* Remember the presence of explicit specialization args. */
3006 : 1144212 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3007 : 1144212 : = TINFO_USED_TEMPLATE_ID (new_template_info);
3008 : : }
3009 : :
3010 : : /* We don't want to copy template info from a non-templated friend
3011 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3012 : 15482804 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3013 : : || !non_templated_friend_p (olddecl));
3014 : 15482804 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3015 : : }
3016 : :
3017 : 15482804 : if (DECL_DECLARES_FUNCTION_P (newdecl))
3018 : : {
3019 : : /* Only functions have these fields. */
3020 : 15091655 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3021 : 15091655 : DECL_BEFRIENDING_CLASSES (newdecl)
3022 : 15091655 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3023 : 15091655 : DECL_BEFRIENDING_CLASSES (olddecl));
3024 : : /* DECL_THUNKS is only valid for virtual functions,
3025 : : otherwise it is a DECL_FRIEND_CONTEXT. */
3026 : 15091655 : if (DECL_VIRTUAL_P (newdecl))
3027 : 655099 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3028 : 28873112 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3029 : 105500 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3030 : : }
3031 : 391149 : else if (VAR_P (newdecl))
3032 : : {
3033 : : /* Only variables have this field. */
3034 : 391097 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3035 : 194 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3036 : : }
3037 : : }
3038 : :
3039 : 15512703 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3040 : : {
3041 : 15091655 : tree parm;
3042 : :
3043 : : /* Merge parameter attributes. */
3044 : 15091655 : tree oldarg, newarg;
3045 : 15091655 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
3046 : 42196845 : oldarg && newarg;
3047 : 27105190 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
3048 : : {
3049 : 27105190 : DECL_ATTRIBUTES (newarg)
3050 : 27105190 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
3051 : 27105190 : if (lookup_attribute (NULL, "indeterminate",
3052 : 27105190 : DECL_ATTRIBUTES (newarg))
3053 : 27105241 : && !lookup_attribute (NULL, "indeterminate",
3054 : 51 : DECL_ATTRIBUTES (oldarg)))
3055 : : {
3056 : 12 : auto_diagnostic_group d;
3057 : 12 : error_at (DECL_SOURCE_LOCATION (newarg),
3058 : : "%<indeterminate%> attribute not specified "
3059 : : "for parameter %qD on the first declaration of "
3060 : : "its function", newarg);
3061 : 12 : inform (DECL_SOURCE_LOCATION (oldarg),
3062 : : "earlier declaration");
3063 : 12 : }
3064 : 27105190 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
3065 : : }
3066 : :
3067 : 15091655 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3068 : 15091655 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3069 : : {
3070 : : /* If newdecl is not a specialization, then it is not a
3071 : : template-related function at all. And that means that we
3072 : : should have exited above, returning 0. */
3073 : 572103 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3074 : :
3075 : 572103 : if (DECL_ODR_USED (olddecl))
3076 : : /* From [temp.expl.spec]:
3077 : :
3078 : : If a template, a member template or the member of a class
3079 : : template is explicitly specialized then that
3080 : : specialization shall be declared before the first use of
3081 : : that specialization that would cause an implicit
3082 : : instantiation to take place, in every translation unit in
3083 : : which such a use occurs. */
3084 : 0 : error ("explicit specialization of %qD after first use",
3085 : : olddecl);
3086 : :
3087 : 572103 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3088 : 572103 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3089 : 1144053 : && DECL_DECLARED_INLINE_P (newdecl));
3090 : :
3091 : : /* Don't propagate visibility from the template to the
3092 : : specialization here. We'll do that in determine_visibility if
3093 : : appropriate. */
3094 : 572103 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3095 : :
3096 : : /* [temp.expl.spec/14] We don't inline explicit specialization
3097 : : just because the primary template says so. */
3098 : 572103 : gcc_assert (!merge_attr);
3099 : :
3100 : 572103 : DECL_DECLARED_INLINE_P (olddecl)
3101 : 572103 : = DECL_DECLARED_INLINE_P (newdecl);
3102 : :
3103 : 572103 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
3104 : 572103 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3105 : :
3106 : 572103 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3107 : : }
3108 : 14519552 : else if (new_defines_function && DECL_INITIAL (olddecl))
3109 : : {
3110 : : /* Never inline re-defined extern inline functions.
3111 : : FIXME: this could be better handled by keeping both
3112 : : function as separate declarations. */
3113 : 78 : DECL_UNINLINABLE (newdecl) = 1;
3114 : : }
3115 : : else
3116 : : {
3117 : 14519474 : if (DECL_PENDING_INLINE_P (olddecl))
3118 : : {
3119 : 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
3120 : 6 : DECL_PENDING_INLINE_INFO (newdecl)
3121 : 12 : = DECL_PENDING_INLINE_INFO (olddecl);
3122 : : }
3123 : 14519468 : else if (DECL_PENDING_INLINE_P (newdecl))
3124 : : ;
3125 : 14519468 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3126 : 14519468 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3127 : 29038936 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3128 : :
3129 : 14519474 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3130 : :
3131 : 43558422 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3132 : 29038948 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3133 : :
3134 : 29038948 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3135 : 14519474 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3136 : 14519474 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3137 : 28639332 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3138 : : }
3139 : :
3140 : : /* Preserve abstractness on cloned [cd]tors. */
3141 : 15091655 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3142 : :
3143 : : /* Update newdecl's parms to point at olddecl. */
3144 : 48170451 : for (parm = DECL_ARGUMENTS (newdecl); parm;
3145 : 33078796 : parm = DECL_CHAIN (parm))
3146 : 33078796 : DECL_CONTEXT (parm) = olddecl;
3147 : :
3148 : 15091655 : if (! types_match)
3149 : : {
3150 : 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3151 : 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3152 : 201 : COPY_DECL_RTL (newdecl, olddecl);
3153 : : }
3154 : 15091655 : if (! types_match || new_defines_function)
3155 : : {
3156 : : /* These are the final DECL_ARGUMENTS that will be used within the
3157 : : body; update any references to old DECL_ARGUMENTS in the
3158 : : contracts, if present. */
3159 : 9083267 : if (tree contracts = DECL_CONTRACTS (newdecl))
3160 : 132 : remap_contracts (olddecl, newdecl, contracts, true);
3161 : :
3162 : : /* These need to be copied so that the names are available.
3163 : : Note that if the types do match, we'll preserve inline
3164 : : info and other bits, but if not, we won't. */
3165 : 9083267 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3166 : 9083267 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3167 : :
3168 : : /* In some cases, duplicate_contracts will remove contracts from
3169 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
3170 : : shared. If we removed them, re-add them. */
3171 : 9083267 : if (!DECL_CONTRACTS (olddecl))
3172 : 9083220 : copy_contract_attributes (olddecl, newdecl);
3173 : : }
3174 : : /* If redeclaring a builtin function, it stays built in
3175 : : if newdecl is a gnu_inline definition, or if newdecl is just
3176 : : a declaration. */
3177 : 15091655 : if (fndecl_built_in_p (olddecl)
3178 : 15150220 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3179 : : {
3180 : 4134832 : copy_decl_built_in_function (newdecl, olddecl);
3181 : : /* If we're keeping the built-in definition, keep the rtl,
3182 : : regardless of declaration matches. */
3183 : 4134832 : COPY_DECL_RTL (olddecl, newdecl);
3184 : 4134832 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3185 : : {
3186 : 4134826 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3187 : 4134826 : if (builtin_decl_explicit_p (fncode))
3188 : : {
3189 : : /* A compatible prototype of these builtin functions
3190 : : is seen, assume the runtime implements it with
3191 : : the expected semantics. */
3192 : 4134826 : switch (fncode)
3193 : : {
3194 : 10491 : case BUILT_IN_STPCPY:
3195 : 10491 : set_builtin_decl_implicit_p (fncode, true);
3196 : 10491 : break;
3197 : 4124335 : default:
3198 : 4124335 : set_builtin_decl_declared_p (fncode, true);
3199 : 4124335 : break;
3200 : : }
3201 : : }
3202 : :
3203 : 4134826 : copy_attributes_to_builtin (newdecl);
3204 : : }
3205 : : }
3206 : 15091655 : if (new_defines_function)
3207 : : /* If defining a function declared with other language
3208 : : linkage, use the previously declared language linkage. */
3209 : 9083066 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3210 : 6008589 : else if (types_match)
3211 : : {
3212 : 6008388 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3213 : : /* Don't clear out the arguments if we're just redeclaring a
3214 : : function. */
3215 : 6008388 : if (DECL_ARGUMENTS (olddecl))
3216 : : {
3217 : : /* If we removed contracts from previous definition, re-attach
3218 : : them. Otherwise, rewrite the contracts so they match the
3219 : : parameters of the new declaration. */
3220 : 1899027 : if (DECL_INITIAL (olddecl)
3221 : 11621 : && DECL_CONTRACTS (newdecl)
3222 : 1899038 : && !DECL_CONTRACTS (olddecl))
3223 : 10 : copy_contract_attributes (olddecl, newdecl);
3224 : : else
3225 : : {
3226 : : /* Temporarily undo the re-contexting of parameters so we can
3227 : : actually remap parameters. The inliner won't replace
3228 : : parameters if we don't do this. */
3229 : 1899017 : tree args = DECL_ARGUMENTS (newdecl);
3230 : 6063468 : for (tree p = args; p; p = DECL_CHAIN (p))
3231 : 4164451 : DECL_CONTEXT (p) = newdecl;
3232 : :
3233 : : /* Save new argument names for use in contracts parsing,
3234 : : unless we've already started parsing the body of olddecl
3235 : : (particular issues arise when newdecl is from a prior
3236 : : friend decl with no argument names, see
3237 : : modules/contracts-tpl-friend-1). */
3238 : 1899017 : if (tree contracts = DECL_CONTRACTS (olddecl))
3239 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3240 : :
3241 : : /* And reverse this operation again. */
3242 : 6063468 : for (tree p = args; p; p = DECL_CHAIN (p))
3243 : 4164451 : DECL_CONTEXT (p) = olddecl;
3244 : : }
3245 : :
3246 : 1899027 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3247 : : }
3248 : : }
3249 : : }
3250 : 421048 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3251 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3252 : :
3253 : : /* Now preserve various other info from the definition. */
3254 : 15512703 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3255 : 15512703 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3256 : 15512703 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3257 : 15512703 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3258 : :
3259 : : /* Warn about conflicting visibility specifications. */
3260 : 15512703 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3261 : 5455494 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3262 : 15513240 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3263 : : {
3264 : 12 : auto_diagnostic_group d;
3265 : 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3266 : : "%qD: visibility attribute ignored because it "
3267 : : "conflicts with previous declaration", newdecl))
3268 : 12 : inform (olddecl_loc,
3269 : : "previous declaration of %qD", olddecl);
3270 : 12 : }
3271 : : /* Choose the declaration which specified visibility. */
3272 : 15512703 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3273 : : {
3274 : 5455494 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3275 : 5455494 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3276 : : }
3277 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3278 : : so keep this behavior. */
3279 : 15512703 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3280 : : {
3281 : 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3282 : 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3283 : : }
3284 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3285 : 15512703 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3286 : : {
3287 : 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3288 : 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3289 : : }
3290 : 15512649 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3291 : 15512649 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3292 : 3 : DECL_USER_ALIGN (newdecl) = 1;
3293 : :
3294 : 15512703 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3295 : 31025406 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3296 : 15512703 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3297 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3298 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3299 : 15512703 : if (TREE_CODE (newdecl) == FIELD_DECL)
3300 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3301 : :
3302 : : /* Merge module entity mapping information. */
3303 : 15512703 : if (DECL_LANG_SPECIFIC (olddecl)
3304 : 30995553 : && (DECL_MODULE_ENTITY_P (olddecl)
3305 : 15482559 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3306 : : {
3307 : 291 : retrofit_lang_decl (newdecl);
3308 : 291 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3309 : 291 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3310 : : }
3311 : :
3312 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3313 : : with that from NEWDECL below. */
3314 : 15512703 : if (DECL_LANG_SPECIFIC (olddecl))
3315 : : {
3316 : 15482850 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3317 : : != DECL_LANG_SPECIFIC (newdecl));
3318 : 15482850 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3319 : : }
3320 : :
3321 : : /* Merge the USED information. */
3322 : 15512703 : if (TREE_USED (olddecl))
3323 : 490606 : TREE_USED (newdecl) = 1;
3324 : 15022097 : else if (TREE_USED (newdecl))
3325 : 54 : TREE_USED (olddecl) = 1;
3326 : :
3327 : 15512703 : if (VAR_P (newdecl))
3328 : : {
3329 : 394722 : if (DECL_READ_P (olddecl))
3330 : 35451 : DECL_READ_P (newdecl) = 1;
3331 : 359271 : else if (DECL_READ_P (newdecl))
3332 : 3 : DECL_READ_P (olddecl) = 1;
3333 : : }
3334 : :
3335 : 15512703 : if (DECL_PRESERVE_P (olddecl))
3336 : 48 : DECL_PRESERVE_P (newdecl) = 1;
3337 : 15512655 : else if (DECL_PRESERVE_P (newdecl))
3338 : 63 : DECL_PRESERVE_P (olddecl) = 1;
3339 : :
3340 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3341 : : to olddecl and deleted. */
3342 : 15512703 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3343 : 30604358 : && DECL_FUNCTION_VERSIONED (olddecl))
3344 : : {
3345 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3346 : 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3347 : : /* newdecl will be purged after copying to olddecl and is no longer
3348 : : a version. */
3349 : 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3350 : : }
3351 : :
3352 : 15512703 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3353 : : {
3354 : 15091655 : int function_size;
3355 : 15091655 : struct symtab_node *snode = symtab_node::get (olddecl);
3356 : :
3357 : 15091655 : function_size = sizeof (struct tree_decl_common);
3358 : :
3359 : 15091655 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3360 : : (char *) newdecl + sizeof (struct tree_common),
3361 : : function_size - sizeof (struct tree_common));
3362 : :
3363 : 15091655 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3364 : : (char *) newdecl + sizeof (struct tree_decl_common),
3365 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3366 : :
3367 : : /* Preserve symtab node mapping. */
3368 : 15091655 : olddecl->decl_with_vis.symtab_node = snode;
3369 : :
3370 : 15091655 : if (new_template_info)
3371 : : /* If newdecl is a template instantiation, it is possible that
3372 : : the following sequence of events has occurred:
3373 : :
3374 : : o A friend function was declared in a class template. The
3375 : : class template was instantiated.
3376 : :
3377 : : o The instantiation of the friend declaration was
3378 : : recorded on the instantiation list, and is newdecl.
3379 : :
3380 : : o Later, however, instantiate_class_template called pushdecl
3381 : : on the newdecl to perform name injection. But, pushdecl in
3382 : : turn called duplicate_decls when it discovered that another
3383 : : declaration of a global function with the same name already
3384 : : existed.
3385 : :
3386 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3387 : :
3388 : : If we're going to do that, we'd better make sure that
3389 : : olddecl, and not newdecl, is on the list of
3390 : : instantiations so that if we try to do the instantiation
3391 : : again we won't get the clobbered declaration. */
3392 : 606843 : reregister_specialization (newdecl,
3393 : : new_template_info,
3394 : : olddecl);
3395 : : }
3396 : : else
3397 : : {
3398 : 421048 : size_t size = tree_code_size (TREE_CODE (newdecl));
3399 : :
3400 : 421048 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3401 : : (char *) newdecl + sizeof (struct tree_common),
3402 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3403 : :
3404 : 421048 : switch (TREE_CODE (newdecl))
3405 : : {
3406 : 421048 : case LABEL_DECL:
3407 : 421048 : case VAR_DECL:
3408 : 421048 : case RESULT_DECL:
3409 : 421048 : case PARM_DECL:
3410 : 421048 : case FIELD_DECL:
3411 : 421048 : case TYPE_DECL:
3412 : 421048 : case CONST_DECL:
3413 : 421048 : {
3414 : 421048 : struct symtab_node *snode = NULL;
3415 : :
3416 : 421048 : if (VAR_P (newdecl)
3417 : 421048 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3418 : 3 : || DECL_EXTERNAL (olddecl)))
3419 : 394722 : snode = symtab_node::get (olddecl);
3420 : 421048 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3421 : : (char *) newdecl + sizeof (struct tree_decl_common),
3422 : : size - sizeof (struct tree_decl_common)
3423 : 421048 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3424 : 421048 : if (VAR_P (newdecl))
3425 : 394722 : olddecl->decl_with_vis.symtab_node = snode;
3426 : : }
3427 : : break;
3428 : 0 : default:
3429 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3430 : : (char *) newdecl + sizeof (struct tree_decl_common),
3431 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3432 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3433 : 0 : break;
3434 : : }
3435 : : }
3436 : :
3437 : 15512703 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3438 : : {
3439 : 15486377 : if (DECL_EXTERNAL (olddecl)
3440 : 396085 : || TREE_PUBLIC (olddecl)
3441 : 15486446 : || TREE_STATIC (olddecl))
3442 : : {
3443 : : /* Merge the section attribute.
3444 : : We want to issue an error if the sections conflict but that must be
3445 : : done later in decl_attributes since we are called before attributes
3446 : : are assigned. */
3447 : 15486377 : if (DECL_SECTION_NAME (newdecl) != NULL)
3448 : 7 : set_decl_section_name (olddecl, newdecl);
3449 : :
3450 : 15486377 : if (DECL_ONE_ONLY (newdecl))
3451 : : {
3452 : 0 : struct symtab_node *oldsym, *newsym;
3453 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3454 : 0 : oldsym = cgraph_node::get_create (olddecl);
3455 : : else
3456 : 0 : oldsym = varpool_node::get_create (olddecl);
3457 : 0 : newsym = symtab_node::get (newdecl);
3458 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3459 : : }
3460 : : }
3461 : :
3462 : 15486377 : if (VAR_P (newdecl)
3463 : 15486377 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3464 : : {
3465 : 404 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3466 : 404 : if (!processing_template_decl)
3467 : 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3468 : : }
3469 : : }
3470 : :
3471 : 15512703 : DECL_UID (olddecl) = olddecl_uid;
3472 : :
3473 : : /* NEWDECL contains the merged attribute lists.
3474 : : Update OLDDECL to be the same. */
3475 : 15512703 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3476 : :
3477 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3478 : : so that encode_section_info has a chance to look at the new decl
3479 : : flags and attributes. */
3480 : 15512703 : if (DECL_RTL_SET_P (olddecl)
3481 : 15512703 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3482 : 0 : || (VAR_P (olddecl)
3483 : 0 : && TREE_STATIC (olddecl))))
3484 : 0 : make_decl_rtl (olddecl);
3485 : :
3486 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3487 : : declaration of a member results in a call to duplicate_decls,
3488 : : freeing these nodes represents in a significant savings.
3489 : :
3490 : : Before releasing the node, be sore to remove function from symbol
3491 : : table that might have been inserted there to record comdat group.
3492 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3493 : : structure is shared in between newdecl and oldecl. */
3494 : 15512703 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3495 : 15091655 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3496 : 15512703 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3497 : : {
3498 : 15486377 : struct symtab_node *snode = symtab_node::get (newdecl);
3499 : 15486377 : if (snode)
3500 : 399 : snode->remove ();
3501 : : }
3502 : :
3503 : 15512703 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3504 : : {
3505 : 15091655 : tree clone;
3506 : 15609087 : FOR_EACH_CLONE (clone, olddecl)
3507 : : {
3508 : 517432 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3509 : 517432 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3510 : : }
3511 : : }
3512 : :
3513 : : /* Remove the associated constraints for newdecl, if any, before
3514 : : reclaiming memory. */
3515 : 15512703 : if (flag_concepts)
3516 : 3901243 : remove_constraints (newdecl);
3517 : :
3518 : : /* And similarly for any module tracking data. */
3519 : 15512703 : if (modules_p ())
3520 : 29812 : remove_defining_module (newdecl);
3521 : :
3522 : 15512703 : ggc_free (newdecl);
3523 : :
3524 : 15512703 : return olddecl;
3525 : : }
3526 : :
3527 : : /* Return zero if the declaration NEWDECL is valid
3528 : : when the declaration OLDDECL (assumed to be for the same name)
3529 : : has already been seen.
3530 : : Otherwise return an error message format string with a %s
3531 : : where the identifier should go. */
3532 : :
3533 : : static const char *
3534 : 15103874 : redeclaration_error_message (tree newdecl, tree olddecl)
3535 : : {
3536 : 15103910 : if (TREE_CODE (newdecl) == TYPE_DECL)
3537 : : {
3538 : : /* Because C++ can put things into name space for free,
3539 : : constructs like "typedef struct foo { ... } foo"
3540 : : would look like an erroneous redeclaration. */
3541 : 57772 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3542 : : return NULL;
3543 : : else
3544 : : return G_("redefinition of %q#D");
3545 : : }
3546 : 15046138 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3547 : : {
3548 : : /* If this is a pure function, its olddecl will actually be
3549 : : the original initialization to `0' (which we force to call
3550 : : abort()). Don't complain about redefinition in this case. */
3551 : 22111778 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3552 : 11055907 : && DECL_INITIAL (olddecl) == NULL_TREE)
3553 : : return NULL;
3554 : :
3555 : : /* If both functions come from different namespaces, this is not
3556 : : a redeclaration - this is a conflict with a used function. */
3557 : 22111748 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3558 : 2270225 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3559 : 11055877 : && ! decls_match (olddecl, newdecl))
3560 : : return G_("%qD conflicts with used function");
3561 : :
3562 : : /* We'll complain about linkage mismatches in
3563 : : warn_extern_redeclared_static. */
3564 : :
3565 : : /* Defining the same name twice is no good. */
3566 : 11055874 : if (decl_defined_p (olddecl)
3567 : 11055874 : && decl_defined_p (newdecl))
3568 : : {
3569 : 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3570 : : return G_("%q#D not declared in class");
3571 : 444 : else if (!GNU_INLINE_P (olddecl)
3572 : 390 : || GNU_INLINE_P (newdecl))
3573 : 219 : return G_("redefinition of %q#D");
3574 : : }
3575 : :
3576 : 11790517 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3577 : : {
3578 : 688416 : bool olda = GNU_INLINE_P (olddecl);
3579 : 688416 : bool newa = GNU_INLINE_P (newdecl);
3580 : :
3581 : 688416 : if (olda != newa)
3582 : : {
3583 : 15 : if (newa)
3584 : : return G_("%q+D redeclared inline with "
3585 : : "%<gnu_inline%> attribute");
3586 : : else
3587 : : return G_("%q+D redeclared inline without "
3588 : : "%<gnu_inline%> attribute");
3589 : : }
3590 : : }
3591 : :
3592 : : /* [class.compare.default]: A definition of a comparison operator as
3593 : : defaulted that appears in a class shall be the first declaration of
3594 : : that function. */
3595 : 11055640 : special_function_kind sfk = special_function_p (olddecl);
3596 : 11055640 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3597 : : return G_("comparison operator %q+D defaulted after "
3598 : : "its first declaration");
3599 : :
3600 : 11055637 : check_abi_tag_redeclaration
3601 : 11055637 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3602 : 11055637 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3603 : :
3604 : 11055637 : return NULL;
3605 : : }
3606 : 3990249 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3607 : : {
3608 : 3595165 : tree nt, ot;
3609 : :
3610 : 3595165 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3611 : : return G_("redefinition of %q#D");
3612 : :
3613 : 3595159 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3614 : 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3615 : 36 : DECL_TEMPLATE_RESULT (olddecl));
3616 : :
3617 : 3595123 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3618 : : return NULL;
3619 : :
3620 : 3595111 : nt = DECL_TEMPLATE_RESULT (newdecl);
3621 : 3595111 : if (DECL_TEMPLATE_INFO (nt))
3622 : 542374 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3623 : 3595111 : ot = DECL_TEMPLATE_RESULT (olddecl);
3624 : 3595111 : if (DECL_TEMPLATE_INFO (ot))
3625 : 3595111 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3626 : 6318438 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3627 : 3595150 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3628 : 15 : return G_("redefinition of %q#D");
3629 : :
3630 : 4923565 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3631 : : {
3632 : 847464 : bool olda = GNU_INLINE_P (ot);
3633 : 847464 : bool newa = GNU_INLINE_P (nt);
3634 : :
3635 : 847464 : if (olda != newa)
3636 : : {
3637 : 0 : if (newa)
3638 : : return G_("%q+D redeclared inline with "
3639 : : "%<gnu_inline%> attribute");
3640 : : else
3641 : : return G_("%q+D redeclared inline without "
3642 : : "%<gnu_inline%> attribute");
3643 : : }
3644 : : }
3645 : :
3646 : : /* Core issue #226 (C++11):
3647 : :
3648 : : If a friend function template declaration specifies a
3649 : : default template-argument, that declaration shall be a
3650 : : definition and shall be the only declaration of the
3651 : : function template in the translation unit. */
3652 : 3595096 : if ((cxx_dialect != cxx98)
3653 : 3579251 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3654 : 3811272 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3655 : : /*is_primary=*/true,
3656 : : /*is_partial=*/false,
3657 : : /*is_friend_decl=*/2))
3658 : : return G_("redeclaration of friend %q#D "
3659 : : "may not have default template arguments");
3660 : :
3661 : 3595093 : return NULL;
3662 : : }
3663 : 395084 : else if (VAR_P (newdecl)
3664 : 395000 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3665 : 395000 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3666 : 395131 : && (! DECL_LANG_SPECIFIC (olddecl)
3667 : 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3668 : 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3669 : : {
3670 : : /* Only variables can be thread-local, and all declarations must
3671 : : agree on this property. */
3672 : 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3673 : : return G_("thread-local declaration of %q#D follows "
3674 : : "non-thread-local declaration");
3675 : : else
3676 : 3 : return G_("non-thread-local declaration of %q#D follows "
3677 : : "thread-local declaration");
3678 : : }
3679 : 395078 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3680 : : {
3681 : : /* The objects have been declared at namespace scope. If either
3682 : : is a member of an anonymous union, then this is an invalid
3683 : : redeclaration. For example:
3684 : :
3685 : : int i;
3686 : : union { int i; };
3687 : :
3688 : : is invalid. */
3689 : 394849 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3690 : 789767 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3691 : : return G_("redeclaration of %q#D");
3692 : : /* If at least one declaration is a reference, there is no
3693 : : conflict. For example:
3694 : :
3695 : : int i = 3;
3696 : : extern int i;
3697 : :
3698 : : is valid. */
3699 : 394915 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3700 : : return NULL;
3701 : :
3702 : : /* Static data member declared outside a class definition
3703 : : if the variable is defined within the class with constexpr
3704 : : specifier is declaration rather than definition (and
3705 : : deprecated). */
3706 : 2824 : if (cxx_dialect >= cxx17
3707 : 2749 : && VAR_P (olddecl)
3708 : 2693 : && DECL_CLASS_SCOPE_P (olddecl)
3709 : 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3710 : 5461 : && !DECL_INITIAL (newdecl))
3711 : : {
3712 : 2637 : DECL_EXTERNAL (newdecl) = 1;
3713 : : /* For now, only warn with explicit -Wdeprecated. */
3714 : 2637 : if (OPTION_SET_P (warn_deprecated))
3715 : : {
3716 : 40 : auto_diagnostic_group d;
3717 : 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3718 : : "redundant redeclaration of %<constexpr%> "
3719 : : "static data member %qD", newdecl))
3720 : 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3721 : : "previous declaration of %qD", olddecl);
3722 : 40 : }
3723 : 2637 : return NULL;
3724 : : }
3725 : :
3726 : : /* Reject two definitions. */
3727 : : return G_("redefinition of %q#D");
3728 : : }
3729 : : else
3730 : : {
3731 : : /* Objects declared with block scope: */
3732 : : /* Reject two definitions, and reject a definition
3733 : : together with an external reference. */
3734 : 148 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3735 : : return G_("redeclaration of %q#D");
3736 : : return NULL;
3737 : : }
3738 : : }
3739 : :
3740 : :
3741 : : /* Hash and equality functions for the named_label table. */
3742 : :
3743 : : hashval_t
3744 : 75010 : named_label_hash::hash (const value_type entry)
3745 : : {
3746 : 75010 : return IDENTIFIER_HASH_VALUE (entry->name);
3747 : : }
3748 : :
3749 : : bool
3750 : 72072 : named_label_hash::equal (const value_type entry, compare_type name)
3751 : : {
3752 : 72072 : return name == entry->name;
3753 : : }
3754 : :
3755 : : /* Look for a label named ID in the current function. If one cannot
3756 : : be found, create one. Return the named_label_entry, or NULL on
3757 : : failure. */
3758 : :
3759 : : static named_label_entry *
3760 : 23383 : lookup_label_1 (tree id, bool making_local_p)
3761 : : {
3762 : 23383 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3763 : :
3764 : : /* You can't use labels at global scope. */
3765 : 23383 : if (current_function_decl == NULL_TREE)
3766 : : {
3767 : 3 : error ("label %qE referenced outside of any function", id);
3768 : 3 : return NULL;
3769 : : }
3770 : :
3771 : 23380 : if (!named_labels)
3772 : 4580 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3773 : :
3774 : 23380 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3775 : 23380 : named_label_entry **slot
3776 : 23380 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3777 : 23380 : named_label_entry *old = *slot;
3778 : :
3779 : 23380 : if (old && old->label_decl)
3780 : : {
3781 : 3082 : if (!making_local_p)
3782 : : return old;
3783 : :
3784 : 21 : if (old->binding_level == current_binding_level)
3785 : : {
3786 : 0 : auto_diagnostic_group d;
3787 : 0 : error ("local label %qE conflicts with existing label", id);
3788 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3789 : 0 : return NULL;
3790 : 0 : }
3791 : : }
3792 : :
3793 : : /* We are making a new decl, create or reuse the named_label_entry */
3794 : 20340 : named_label_entry *ent = NULL;
3795 : 33 : if (old && !old->label_decl)
3796 : : ent = old;
3797 : : else
3798 : : {
3799 : 20307 : ent = ggc_cleared_alloc<named_label_entry> ();
3800 : 20307 : ent->name = id;
3801 : 20307 : ent->outer = old;
3802 : 20307 : *slot = ent;
3803 : : }
3804 : :
3805 : : /* Now create the LABEL_DECL. */
3806 : 20319 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3807 : :
3808 : 20319 : DECL_CONTEXT (decl) = current_function_decl;
3809 : 20319 : SET_DECL_MODE (decl, VOIDmode);
3810 : 20319 : if (making_local_p)
3811 : : {
3812 : 219 : C_DECLARED_LABEL_FLAG (decl) = true;
3813 : 219 : DECL_CHAIN (decl) = current_binding_level->names;
3814 : 219 : current_binding_level->names = decl;
3815 : : }
3816 : :
3817 : 20319 : ent->label_decl = decl;
3818 : :
3819 : 20319 : return ent;
3820 : 23383 : }
3821 : :
3822 : : /* Wrapper for lookup_label_1. */
3823 : :
3824 : : tree
3825 : 2591 : lookup_label (tree id)
3826 : : {
3827 : 2591 : named_label_entry *ent = lookup_label_1 (id, false);
3828 : 2591 : return ent ? ent->label_decl : NULL_TREE;
3829 : : }
3830 : :
3831 : : /* Remember that we've seen &&ID. */
3832 : :
3833 : : void
3834 : 250 : mark_label_addressed (tree id)
3835 : : {
3836 : 250 : named_label_entry *ent = lookup_label_1 (id, false);
3837 : 250 : ent->addressed = true;
3838 : 250 : }
3839 : :
3840 : : tree
3841 : 219 : declare_local_label (tree id)
3842 : : {
3843 : 219 : named_label_entry *ent = lookup_label_1 (id, true);
3844 : 219 : return ent ? ent->label_decl : NULL_TREE;
3845 : : }
3846 : :
3847 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3848 : :
3849 : : static bool
3850 : 143520 : decl_jump_unsafe (tree decl)
3851 : : {
3852 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3853 : : with automatic storage duration is not in scope to a point where it is
3854 : : in scope is ill-formed unless the variable has scalar type, class type
3855 : : with a trivial default constructor and a trivial destructor, a
3856 : : cv-qualified version of one of these types, or an array of one of the
3857 : : preceding types and is declared without an initializer (8.5). */
3858 : 143520 : tree type = TREE_TYPE (decl);
3859 : :
3860 : 143520 : return (type != error_mark_node
3861 : 143502 : && VAR_P (decl)
3862 : 13440 : && !TREE_STATIC (decl)
3863 : 156824 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3864 : 11472 : || variably_modified_type_p (type, NULL_TREE)));
3865 : : }
3866 : :
3867 : : /* Returns true if decl is an automatic variable with vacuous initialization
3868 : : except when it is [[indeterminate]] or [[gnu::uninitialized]].
3869 : : Jumps across such initialization need to be instrumented for
3870 : : !!flag_auto_var_init. */
3871 : :
3872 : : static bool
3873 : 141679 : decl_instrument_init_bypass_p (tree decl)
3874 : : {
3875 : 141679 : tree type = TREE_TYPE (decl);
3876 : :
3877 : 141679 : return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
3878 : 65353 : && type != error_mark_node
3879 : 65347 : && VAR_P (decl)
3880 : 613 : && !TREE_STATIC (decl)
3881 : 592 : && !DECL_EXTERNAL (decl)
3882 : 1156 : && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3883 : 578 : || variably_modified_type_p (type, NULL_TREE))
3884 : 578 : && !lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (decl))
3885 : 578 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
3886 : 142257 : && !DECL_HAS_VALUE_EXPR_P (decl));
3887 : : }
3888 : :
3889 : : /* Build .DEFERRED_INIT call for DECL. */
3890 : :
3891 : : static tree
3892 : 63 : build_deferred_init_call (tree decl)
3893 : : {
3894 : 63 : tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
3895 : 126 : tree init_type_arg = build_int_cst (integer_type_node,
3896 : 63 : (int) flag_auto_var_init);
3897 : 63 : location_t loc = DECL_SOURCE_LOCATION (decl);
3898 : 63 : tree decl_name;
3899 : :
3900 : 63 : if (DECL_NAME (decl))
3901 : 63 : decl_name = build_string_literal (DECL_NAME (decl));
3902 : : else
3903 : : {
3904 : 0 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
3905 : 0 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
3906 : 0 : decl_name = build_string_literal (decl_name_anonymous);
3907 : : }
3908 : :
3909 : 63 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
3910 : 63 : TREE_TYPE (decl), 3,
3911 : : decl_size_arg, init_type_arg,
3912 : : decl_name);
3913 : 63 : tree ret = build2_loc (loc, MODIFY_EXPR, void_type_node, decl, call);
3914 : 63 : return build_stmt (loc, EXPR_STMT, ret);
3915 : : }
3916 : :
3917 : : /* Emit before ITER (and any labels/case labels before it) code like
3918 : : if (0)
3919 : : {
3920 : : l1:
3921 : : v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
3922 : : v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
3923 : : v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
3924 : : v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
3925 : : }
3926 : : and return l1 label, or if it already exists, assert it has the
3927 : : .DEFERRED_INIT calls for the right decls in the right order and
3928 : : amend it, either by adding extra labels in between or further
3929 : : ,DEFERRED_INIT calls before the first label and extra label before
3930 : : that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
3931 : : of adding a label. DECLS points to an array of NDECLS VAR_DECLs
3932 : : which should be initialized. */
3933 : :
3934 : : static tree
3935 : 57 : maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
3936 : : tree *decls, unsigned ndecls)
3937 : : {
3938 : 57 : tree lab = NULL_TREE;
3939 : 79 : for (; !tsi_end_p (iter); tsi_prev (&iter))
3940 : : {
3941 : 75 : switch (TREE_CODE (tsi_stmt (iter)))
3942 : : {
3943 : 22 : case LABEL_EXPR:
3944 : 22 : case CASE_LABEL_EXPR:
3945 : 22 : case DEBUG_BEGIN_STMT:
3946 : 22 : continue;
3947 : : default:
3948 : : break;
3949 : : }
3950 : : break;
3951 : : }
3952 : 57 : if (!tsi_end_p (iter)
3953 : 53 : && TREE_CODE (tsi_stmt (iter)) == IF_STMT
3954 : 83 : && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
3955 : : {
3956 : : /* Found IF_STMT added for this or some adjacent
3957 : : LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
3958 : : The decls are ordered so that we can always reuse it. Sometimes
3959 : : by no modifications at all and just returning the right label
3960 : : which was added already before, sometimes by adding a label in
3961 : : between two previously added .DEFERRED_INIT calls and sometimes
3962 : : by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
3963 : : before that) before the statements in IF_STMT body. */
3964 : 24 : tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
3965 : 24 : iter = tsi_last (then_clause);
3966 : 24 : bool add = false;
3967 : 128 : for (unsigned int i = 0; i < ndecls; ++i)
3968 : : {
3969 : 104 : tree decl = decls[i];
3970 : 104 : if (!add)
3971 : : {
3972 : : /* Skip over labels/case labels after .DEFERRED_INIT for the
3973 : : DECL we are looking for. */
3974 : 134 : while (!tsi_end_p (iter)
3975 : 134 : && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
3976 : 96 : || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
3977 : 9 : && !case_label)))
3978 : 40 : tsi_prev (&iter);
3979 : 94 : if (tsi_end_p (iter))
3980 : : {
3981 : : /* Reached the start, we'll need to prepend further
3982 : : statements. */
3983 : 7 : add = true;
3984 : 7 : iter = tsi_start (then_clause);
3985 : : }
3986 : : else
3987 : : {
3988 : : /* Found something, assert it is .DEFERRED_INIT for
3989 : : DECL. */
3990 : 87 : tree t = tsi_stmt (iter);
3991 : 87 : gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
3992 : 87 : t = EXPR_STMT_EXPR (t);
3993 : 87 : gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
3994 : : && TREE_OPERAND (t, 0) == decl
3995 : : && (TREE_CODE (TREE_OPERAND (t, 1))
3996 : : == CALL_EXPR));
3997 : 87 : t = TREE_OPERAND (t, 1);
3998 : 87 : gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
3999 : : && (CALL_EXPR_IFN (t)
4000 : : == IFN_DEFERRED_INIT));
4001 : 87 : tsi_prev (&iter);
4002 : : }
4003 : : }
4004 : 94 : if (add)
4005 : : {
4006 : : /* If reached the start in this or some earlier iteration,
4007 : : prepend .DEFERRED_INIT call for DECL. */
4008 : 17 : tree t = build_deferred_init_call (decl);
4009 : 17 : STMT_IS_FULL_EXPR_P (t) = 1;
4010 : 17 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4011 : : }
4012 : : }
4013 : 24 : if (!add)
4014 : : {
4015 : : /* If .DEFERRED_INIT calls for all the decls were already there,
4016 : : skip over case labels and if we find a LABEL_EXPR, return
4017 : : its label. */
4018 : 21 : while (!tsi_end_p (iter)
4019 : 21 : && !case_label
4020 : 39 : && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4021 : 4 : tsi_prev (&iter);
4022 : 17 : if (tsi_end_p (iter))
4023 : : {
4024 : : /* Only case labels were found and we are looking for normal
4025 : : label, we'll need to add it. */
4026 : 0 : add = true;
4027 : 0 : iter = tsi_start (then_clause);
4028 : : }
4029 : 17 : else if (!case_label
4030 : 17 : && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4031 : : /* Return existing label. */
4032 : 7 : lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4033 : : else
4034 : : {
4035 : : /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4036 : 10 : gcc_checking_assert (case_label
4037 : : || (TREE_CODE (tsi_stmt (iter))
4038 : : == EXPR_STMT));
4039 : 10 : add = true;
4040 : 10 : tsi_next (&iter);
4041 : 10 : gcc_checking_assert (!tsi_end_p (iter));
4042 : : }
4043 : : }
4044 : 7 : if (add)
4045 : : {
4046 : 17 : tree t;
4047 : 17 : if (case_label)
4048 : : t = case_label;
4049 : : else
4050 : : {
4051 : 14 : lab = create_artificial_label (UNKNOWN_LOCATION);
4052 : 14 : t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4053 : : }
4054 : 17 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4055 : : }
4056 : : }
4057 : : else
4058 : : {
4059 : : /* No IF_STMT created by this function found. Create it all
4060 : : from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4061 : : followed by .DEFERRED_INIT calls inside of a new if (0). */
4062 : 33 : tree new_then = push_stmt_list ();
4063 : 33 : if (!case_label)
4064 : : {
4065 : 14 : lab = create_artificial_label (UNKNOWN_LOCATION);
4066 : 14 : add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4067 : : }
4068 : : else
4069 : 19 : add_stmt (case_label);
4070 : 79 : for (unsigned int i = ndecls; i; --i)
4071 : 46 : add_stmt (build_deferred_init_call (decls[i - 1]));
4072 : 33 : new_then = pop_stmt_list (new_then);
4073 : 33 : tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4074 : : new_then, void_node, NULL_TREE);
4075 : 33 : IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4076 : 33 : if (tsi_end_p (iter))
4077 : : {
4078 : 4 : iter = tsi_start (iter.container);
4079 : 4 : tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4080 : : }
4081 : : else
4082 : 29 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4083 : : }
4084 : 57 : return lab;
4085 : : }
4086 : :
4087 : : /* Adjust backward gotos to named label ENT if they jump over vacuous
4088 : : initializers if !!flag_auto_var_init. ITER is the location of
4089 : : LABEL_EXPR for that named label. */
4090 : :
4091 : : static void
4092 : 7 : adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4093 : : {
4094 : 7 : auto_vec<tree, 4> decls;
4095 : 7 : unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4096 : 7 : tree decl;
4097 : 30 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4098 : 30 : if (!decl_jump_unsafe (decl))
4099 : : {
4100 : 30 : gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4101 : 30 : decls.safe_push (decl);
4102 : 60 : if (decls.length () == max_cnt)
4103 : : break;
4104 : : }
4105 : 7 : named_label_bck_direct_goto *dgoto;
4106 : 7 : unsigned last = 0;
4107 : 7 : tree lab = NULL_TREE;
4108 : 37 : FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4109 : : {
4110 : 16 : if (dgoto->n_bad_decls != last)
4111 : : {
4112 : 14 : last = dgoto->n_bad_decls;
4113 : 28 : lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4114 : : decls.address (), last);
4115 : : }
4116 : 16 : *dgoto->direct_goto = lab;
4117 : : }
4118 : 7 : }
4119 : :
4120 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4121 : : to the user. */
4122 : :
4123 : : static bool
4124 : 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
4125 : : enum diagnostics::kind diag_kind, bool computed)
4126 : : {
4127 : 286 : if (computed)
4128 : 43 : diag_kind = diagnostics::kind::warning;
4129 : 286 : bool complained
4130 : 373 : = emit_diagnostic (diag_kind, loc, 0,
4131 : : decl ? G_("jump to label %qD")
4132 : : : G_("jump to case label"), decl);
4133 : 286 : if (complained && locus)
4134 : : {
4135 : 199 : if (computed)
4136 : 43 : inform (*locus, " as a possible target of computed goto");
4137 : : else
4138 : 156 : inform (*locus, " from here");
4139 : : }
4140 : 286 : return complained;
4141 : : }
4142 : :
4143 : : /* Check that a single previously seen jump to a newly defined label
4144 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4145 : : the jump context; NAMES are the names in scope in LEVEL at the jump
4146 : : context; LOCUS is the source position of the jump or 0. COMPUTED
4147 : : is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4148 : : vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4149 : : initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4150 : : CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4151 : : Returns non-zero if all is well, 2 if any vacuous initializers were
4152 : : crossed. */
4153 : :
4154 : : static int
4155 : 2021278 : check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4156 : : bool exited_omp, const location_t *locus,
4157 : : vec<tree, va_gc> *computed,
4158 : : vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4159 : : tree case_label)
4160 : : {
4161 : 2021278 : auto_diagnostic_group d;
4162 : 2021278 : cp_binding_level *b;
4163 : 2021278 : bool complained = false;
4164 : 2021278 : int identified = 0;
4165 : 2021278 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4166 : 2021278 : bool saw_ceif = false, saw_se = false;
4167 : 2021278 : auto_vec<tree> vacuous_decls;
4168 : 2021278 : bool vacuous_inits = false;
4169 : :
4170 : 2021278 : if (exited_omp)
4171 : : {
4172 : 12 : complained = identify_goto (decl, input_location, locus,
4173 : : diagnostics::kind::error,
4174 : : computed);
4175 : 12 : if (complained)
4176 : 12 : inform (input_location, " exits OpenMP structured block");
4177 : : saw_omp = true;
4178 : : identified = 2;
4179 : : }
4180 : :
4181 : 6035193 : for (b = current_binding_level; b ; b = b->level_chain)
4182 : : {
4183 : 4013915 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4184 : :
4185 : 4153050 : for (new_decls = b->names; new_decls != old_decls;
4186 : 268394 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4187 : 129259 : : TREE_CHAIN (new_decls)))
4188 : : {
4189 : 139135 : bool problem = decl_jump_unsafe (new_decls);
4190 : 139135 : if (! problem)
4191 : : {
4192 : 139090 : if (decl_instrument_init_bypass_p (new_decls))
4193 : : {
4194 : 135 : if (direct_goto || case_label)
4195 : 102 : vacuous_decls.safe_push (new_decls);
4196 : : vacuous_inits = true;
4197 : : }
4198 : 139090 : continue;
4199 : : }
4200 : :
4201 : 45 : if (!identified)
4202 : : {
4203 : 45 : complained = identify_goto (decl, input_location, locus,
4204 : : diagnostics::kind::error,
4205 : : computed);
4206 : 45 : identified = 2;
4207 : : }
4208 : 45 : if (complained)
4209 : 45 : inform (DECL_SOURCE_LOCATION (new_decls),
4210 : : " crosses initialization of %q#D", new_decls);
4211 : : }
4212 : :
4213 : 4013915 : if (b == level)
4214 : : break;
4215 : :
4216 : 1992637 : const char *inf = NULL;
4217 : 1992637 : location_t loc = input_location;
4218 : 1992637 : switch (b->kind)
4219 : : {
4220 : 3 : case sk_try:
4221 : 3 : if (!saw_eh)
4222 : : inf = G_(" enters %<try%> block");
4223 : : saw_eh = true;
4224 : : break;
4225 : :
4226 : 9 : case sk_catch:
4227 : 9 : if (!saw_eh)
4228 : : inf = G_(" enters %<catch%> block");
4229 : : saw_eh = true;
4230 : : break;
4231 : :
4232 : 102 : case sk_omp:
4233 : 102 : if (!saw_omp)
4234 : : inf = G_(" enters OpenMP structured block");
4235 : : saw_omp = true;
4236 : : break;
4237 : :
4238 : 3 : case sk_transaction:
4239 : 3 : if (!saw_tm)
4240 : : inf = G_(" enters synchronized or atomic statement");
4241 : : saw_tm = true;
4242 : : break;
4243 : :
4244 : 18 : case sk_stmt_expr:
4245 : 18 : if (!saw_se)
4246 : : inf = G_(" enters statement expression");
4247 : : saw_se = true;
4248 : : break;
4249 : :
4250 : 1992379 : case sk_block:
4251 : 1992379 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
4252 : : {
4253 : 9 : inf = G_(" enters %<constexpr if%> statement");
4254 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4255 : : saw_cxif = true;
4256 : : }
4257 : 1992370 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
4258 : : {
4259 : 18 : inf = G_(" enters %<consteval if%> statement");
4260 : 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4261 : : saw_ceif = true;
4262 : : }
4263 : : break;
4264 : :
4265 : : default:
4266 : : break;
4267 : : }
4268 : :
4269 : : if (inf)
4270 : : {
4271 : 141 : if (identified < 2)
4272 : 141 : complained = identify_goto (decl, input_location, locus,
4273 : : diagnostics::kind::error,
4274 : : computed);
4275 : 141 : identified = 2;
4276 : 141 : if (complained)
4277 : 141 : inform (loc, inf);
4278 : : }
4279 : : }
4280 : :
4281 : 2021278 : if (!vec_safe_is_empty (computed))
4282 : : {
4283 : 9 : if (!identified)
4284 : 6 : complained = identify_goto (decl, input_location, locus,
4285 : : diagnostics::kind::error,
4286 : : computed);
4287 : 9 : identified = 2;
4288 : 9 : if (complained)
4289 : 18 : for (tree d : computed)
4290 : : {
4291 : 9 : if (DECL_P (d))
4292 : 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4293 : 3 : else if (d == get_identifier ("catch"))
4294 : 3 : inform (*locus, " does not clean up handled exception");
4295 : : }
4296 : : }
4297 : :
4298 : 2021323 : if (!vacuous_decls.is_empty () && !seen_error ())
4299 : : {
4300 : 43 : tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4301 : 43 : if (case_label)
4302 : : {
4303 : 22 : gcc_checking_assert (tsi_stmt (iter) == case_label);
4304 : 22 : tsi_delink (&iter);
4305 : 22 : iter = tsi_last (cur_stmt_list);
4306 : : }
4307 : 43 : tree lab = maybe_add_deferred_init_calls (iter, case_label,
4308 : : vacuous_decls.address (),
4309 : : vacuous_decls.length ());
4310 : 43 : if (lab)
4311 : : {
4312 : : unsigned int i;
4313 : : named_label_fwd_direct_goto *dgoto;
4314 : 85 : FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4315 : 21 : *dgoto->direct_goto = lab;
4316 : : }
4317 : : }
4318 : :
4319 : 2021278 : if (identified)
4320 : : return 0;
4321 : 2021074 : return vacuous_inits ? 2 : 1;
4322 : 2021278 : }
4323 : :
4324 : : static void
4325 : 1934 : check_previous_goto (tree decl, struct named_label_use_entry *use)
4326 : : {
4327 : 1934 : check_previous_goto_1 (decl, use->binding_level,
4328 : 1934 : use->names_in_scope, use->in_omp_scope,
4329 : 1934 : &use->o_goto_locus, use->computed_goto,
4330 : : use->direct_goto, NULL_TREE);
4331 : 1934 : vec_free (use->direct_goto);
4332 : 1934 : }
4333 : :
4334 : : static int
4335 : 2019344 : check_switch_goto (cp_binding_level *level, tree case_label)
4336 : : {
4337 : 2019344 : return check_previous_goto_1 (NULL_TREE, level, level->names,
4338 : 2019344 : false, NULL, nullptr, nullptr, case_label);
4339 : : }
4340 : :
4341 : : /* Check that a new jump to a label ENT is OK. DECLP is a pointer
4342 : : to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4343 : :
4344 : : void
4345 : 2469 : check_goto_1 (named_label_entry *ent, tree *declp)
4346 : : {
4347 : 2469 : auto_diagnostic_group d;
4348 : 2469 : tree decl = ent->label_decl;
4349 : 2469 : bool computed = declp == NULL;
4350 : :
4351 : : /* If the label hasn't been defined yet, defer checking. */
4352 : 2469 : if (! DECL_INITIAL (decl))
4353 : : {
4354 : : /* Don't bother creating another use if the last goto had the
4355 : : same data, and will therefore create the same set of errors. */
4356 : 2003 : if (ent->uses
4357 : 526 : && ent->uses->binding_level == current_binding_level
4358 : 60 : && ent->uses->names_in_scope == current_binding_level->names)
4359 : : {
4360 : 60 : if (declp && flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
4361 : 11 : vec_safe_push (ent->uses->direct_goto,
4362 : 11 : named_label_fwd_direct_goto { declp });
4363 : 60 : return;
4364 : : }
4365 : :
4366 : 1943 : named_label_use_entry *new_use
4367 : 1943 : = ggc_alloc<named_label_use_entry> ();
4368 : 1943 : new_use->binding_level = current_binding_level;
4369 : 1943 : new_use->names_in_scope = current_binding_level->names;
4370 : 1943 : new_use->o_goto_locus = input_location;
4371 : 1943 : new_use->in_omp_scope = false;
4372 : 1943 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4373 : 1943 : new_use->direct_goto = nullptr;
4374 : 1943 : if (declp && flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
4375 : 297 : vec_safe_push (new_use->direct_goto,
4376 : 297 : named_label_fwd_direct_goto { declp });
4377 : :
4378 : 1943 : new_use->next = ent->uses;
4379 : 1943 : ent->uses = new_use;
4380 : 1943 : return;
4381 : : }
4382 : :
4383 : 466 : bool saw_catch = false, complained = false;
4384 : 466 : int identified = 0;
4385 : 466 : tree bad;
4386 : 466 : unsigned ix;
4387 : 466 : unsigned n_bad_decls = 0;
4388 : :
4389 : 466 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4390 : 451 : || ent->in_constexpr_if || ent->in_consteval_if
4391 : 445 : || ent->in_omp_scope || ent->in_stmt_expr
4392 : 427 : || ent->has_bad_decls)
4393 : : {
4394 : 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4395 : 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4396 : 30 : || ent->in_consteval_if || ent->in_transaction_scope
4397 : 24 : || ent->in_omp_scope || ent->in_stmt_expr)
4398 : 39 : diag_kind = diagnostics::kind::error;
4399 : 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4400 : : &input_location, diag_kind, computed);
4401 : 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
4402 : : }
4403 : :
4404 : 534 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4405 : : {
4406 : 68 : bool problem = decl_jump_unsafe (bad);
4407 : 68 : if (!problem)
4408 : : {
4409 : 62 : gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4410 : 62 : n_bad_decls++;
4411 : 62 : continue;
4412 : : }
4413 : :
4414 : 6 : if (DECL_ARTIFICIAL (bad))
4415 : : {
4416 : : /* Can't skip init of __exception_info. */
4417 : 0 : if (identified == 1)
4418 : : {
4419 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4420 : : &input_location,
4421 : : diagnostics::kind::error,
4422 : : computed);
4423 : 0 : identified = 2;
4424 : : }
4425 : 0 : if (complained)
4426 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4427 : : saw_catch = true;
4428 : : }
4429 : 6 : else if (complained)
4430 : 6 : inform (DECL_SOURCE_LOCATION (bad),
4431 : : " skips initialization of %q#D", bad);
4432 : : }
4433 : :
4434 : 466 : if (complained)
4435 : : {
4436 : 45 : if (ent->in_try_scope)
4437 : 9 : inform (input_location, " enters %<try%> block");
4438 : 36 : else if (ent->in_catch_scope && !saw_catch)
4439 : 3 : inform (input_location, " enters %<catch%> block");
4440 : 33 : else if (ent->in_transaction_scope)
4441 : 3 : inform (input_location, " enters synchronized or atomic statement");
4442 : 30 : else if (ent->in_constexpr_if)
4443 : 3 : inform (input_location, " enters %<constexpr if%> statement");
4444 : 27 : else if (ent->in_consteval_if)
4445 : 3 : inform (input_location, " enters %<consteval if%> statement");
4446 : 24 : else if (ent->in_stmt_expr)
4447 : 15 : inform (input_location, " enters statement expression");
4448 : : }
4449 : :
4450 : 466 : if (ent->in_omp_scope)
4451 : : {
4452 : 3 : if (complained)
4453 : 3 : inform (input_location, " enters OpenMP structured block");
4454 : : }
4455 : 463 : else if (flag_openmp)
4456 : 69 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4457 : : {
4458 : 42 : if (b == ent->binding_level)
4459 : : break;
4460 : 36 : if (b->kind == sk_omp)
4461 : : {
4462 : 21 : if (identified < 2)
4463 : : {
4464 : 42 : complained = identify_goto (decl,
4465 : 21 : DECL_SOURCE_LOCATION (decl),
4466 : : &input_location,
4467 : : diagnostics::kind::error,
4468 : : computed);
4469 : 21 : identified = 2;
4470 : : }
4471 : 21 : if (complained)
4472 : 21 : inform (input_location, " exits OpenMP structured block");
4473 : : break;
4474 : : }
4475 : : }
4476 : :
4477 : : /* Warn if a computed goto might involve a local variable going out of scope
4478 : : without being cleaned up. */
4479 : 466 : if (computed)
4480 : : {
4481 : 71 : auto level = ent->binding_level;
4482 : 71 : auto names = ent->names_in_scope;
4483 : 71 : for (auto b = current_binding_level; ; b = b->level_chain)
4484 : : {
4485 : 111 : if (b->kind == sk_catch)
4486 : : {
4487 : 6 : if (!identified)
4488 : : {
4489 : 6 : complained
4490 : 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4491 : : &input_location, diagnostics::kind::error,
4492 : : computed);
4493 : 6 : identified = 2;
4494 : : }
4495 : 6 : if (complained)
4496 : 6 : inform (input_location,
4497 : : " does not clean up handled exception");
4498 : : }
4499 : 111 : tree end = b == level ? names : NULL_TREE;
4500 : 127 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4501 : : {
4502 : 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4503 : : {
4504 : 10 : if (!identified)
4505 : : {
4506 : 10 : complained
4507 : 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4508 : : &input_location, diagnostics::kind::error,
4509 : : computed);
4510 : 10 : identified = 2;
4511 : : }
4512 : 10 : if (complained)
4513 : 10 : inform (DECL_SOURCE_LOCATION (d),
4514 : : " does not destroy %qD", d);
4515 : : }
4516 : : }
4517 : 111 : if (b == level)
4518 : : break;
4519 : 40 : }
4520 : : }
4521 : :
4522 : 466 : if (n_bad_decls && declp)
4523 : 16 : vec_safe_push (ent->direct_goto,
4524 : 16 : named_label_bck_direct_goto { declp, n_bad_decls });
4525 : 2469 : }
4526 : :
4527 : : /* Check that a new jump to a label *DECLP is OK. Called by
4528 : : finish_goto_stmt. */
4529 : :
4530 : : void
4531 : 2429 : check_goto (tree *declp)
4532 : : {
4533 : 2429 : if (!named_labels)
4534 : : return;
4535 : 2414 : tree decl = *declp;
4536 : 2414 : if (TREE_CODE (decl) != LABEL_DECL)
4537 : : {
4538 : : /* We don't know where a computed goto is jumping,
4539 : : so check all addressable labels. */
4540 : 306 : for (auto iter = named_labels->begin ();
4541 : 416 : iter != named_labels->end ();
4542 : 196 : ++iter)
4543 : : {
4544 : 196 : auto ent = *iter;
4545 : 196 : if (ent->addressed)
4546 : 165 : check_goto_1 (ent, NULL);
4547 : : }
4548 : : }
4549 : : else
4550 : : {
4551 : 2304 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4552 : 2304 : named_label_entry **slot
4553 : 2304 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4554 : 2304 : named_label_entry *ent = *slot;
4555 : 2304 : check_goto_1 (ent, declp);
4556 : : }
4557 : : }
4558 : :
4559 : : /* Check that a return is ok wrt OpenMP structured blocks.
4560 : : Called by finish_return_stmt. Returns true if all is well. */
4561 : :
4562 : : bool
4563 : 232510 : check_omp_return (void)
4564 : : {
4565 : 891523 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4566 : 659013 : if (b->kind == sk_omp)
4567 : : {
4568 : 15 : error ("invalid exit from OpenMP structured block");
4569 : 15 : return false;
4570 : : }
4571 : 658998 : else if (b->kind == sk_function_parms)
4572 : : break;
4573 : : return true;
4574 : : }
4575 : :
4576 : : /* Define a label, specifying the location in the source file.
4577 : : Return the LABEL_DECL node for the label. */
4578 : :
4579 : : tree
4580 : 20316 : define_label (location_t location, tree name)
4581 : : {
4582 : 20316 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4583 : :
4584 : : /* After labels, make any new cleanups in the function go into their
4585 : : own new (temporary) binding contour. */
4586 : 20316 : for (cp_binding_level *p = current_binding_level;
4587 : 30502 : p->kind != sk_function_parms;
4588 : 10186 : p = p->level_chain)
4589 : 10186 : p->more_cleanups_ok = 0;
4590 : :
4591 : 20316 : named_label_entry *ent = lookup_label_1 (name, false);
4592 : 20316 : tree decl = ent->label_decl;
4593 : :
4594 : 20316 : if (DECL_INITIAL (decl) != NULL_TREE)
4595 : : {
4596 : 6 : error ("duplicate label %qD", decl);
4597 : 6 : return error_mark_node;
4598 : : }
4599 : : else
4600 : : {
4601 : : /* Mark label as having been defined. */
4602 : 20310 : DECL_INITIAL (decl) = error_mark_node;
4603 : : /* Say where in the source. */
4604 : 20310 : DECL_SOURCE_LOCATION (decl) = location;
4605 : :
4606 : 20310 : ent->binding_level = current_binding_level;
4607 : 20310 : ent->names_in_scope = current_binding_level->names;
4608 : :
4609 : 22244 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4610 : 1934 : check_previous_goto (decl, use);
4611 : 20310 : ent->uses = NULL;
4612 : : }
4613 : :
4614 : 20310 : return decl;
4615 : 20316 : }
4616 : :
4617 : : struct cp_switch
4618 : : {
4619 : : cp_binding_level *level;
4620 : : struct cp_switch *next;
4621 : : /* The SWITCH_STMT being built. */
4622 : : tree switch_stmt;
4623 : : /* A splay-tree mapping the low element of a case range to the high
4624 : : element, or NULL_TREE if there is no high element. Used to
4625 : : determine whether or not a new case label duplicates an old case
4626 : : label. We need a tree, rather than simply a hash table, because
4627 : : of the GNU case range extension. */
4628 : : splay_tree cases;
4629 : : /* Remember whether a default: case label has been seen. */
4630 : : bool has_default_p;
4631 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4632 : : bool break_stmt_seen_p;
4633 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4634 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4635 : : bool in_loop_body_p;
4636 : : };
4637 : :
4638 : : /* A stack of the currently active switch statements. The innermost
4639 : : switch statement is on the top of the stack. There is no need to
4640 : : mark the stack for garbage collection because it is only active
4641 : : during the processing of the body of a function, and we never
4642 : : collect at that point. */
4643 : :
4644 : : static struct cp_switch *switch_stack;
4645 : :
4646 : : /* Called right after a switch-statement condition is parsed.
4647 : : SWITCH_STMT is the switch statement being parsed. */
4648 : :
4649 : : void
4650 : 513704 : push_switch (tree switch_stmt)
4651 : : {
4652 : 513704 : struct cp_switch *p = XNEW (struct cp_switch);
4653 : 513704 : p->level = current_binding_level;
4654 : 513704 : p->next = switch_stack;
4655 : 513704 : p->switch_stmt = switch_stmt;
4656 : 513704 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4657 : 513704 : p->has_default_p = false;
4658 : 513704 : p->break_stmt_seen_p = false;
4659 : 513704 : p->in_loop_body_p = false;
4660 : 513704 : switch_stack = p;
4661 : 513704 : }
4662 : :
4663 : : void
4664 : 513704 : pop_switch (void)
4665 : : {
4666 : 513704 : struct cp_switch *cs = switch_stack;
4667 : :
4668 : : /* Emit warnings as needed. */
4669 : 513704 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4670 : 513704 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4671 : 513704 : const bool bool_cond_p
4672 : 513704 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4673 : 513704 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4674 : 513704 : if (!processing_template_decl)
4675 : 317993 : c_do_switch_warnings (cs->cases, switch_location,
4676 : 317993 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4677 : : bool_cond_p);
4678 : :
4679 : : /* For the benefit of block_may_fallthru remember if the switch body
4680 : : case labels cover all possible values and if there are break; stmts. */
4681 : 513704 : if (cs->has_default_p
4682 : 513704 : || (!processing_template_decl
4683 : 43830 : && c_switch_covers_all_cases_p (cs->cases,
4684 : 43830 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4685 : 399370 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4686 : 513704 : if (!cs->break_stmt_seen_p)
4687 : 268888 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4688 : : /* Now that we're done with the switch warnings, set the switch type
4689 : : to the type of the condition if the index type was of scoped enum type.
4690 : : (Such types don't participate in the integer promotions.) We do this
4691 : : because of bit-fields whose declared type is a scoped enum type:
4692 : : gimplification will use the lowered index type, but convert the
4693 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4694 : : and verify_gimple_switch doesn't accept that. */
4695 : 513704 : if (is_bitfield_expr_with_lowered_type (cond))
4696 : 56828 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4697 : 513704 : gcc_assert (!cs->in_loop_body_p);
4698 : 513704 : splay_tree_delete (cs->cases);
4699 : 513704 : switch_stack = switch_stack->next;
4700 : 513704 : free (cs);
4701 : 513704 : }
4702 : :
4703 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4704 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4705 : : in switch_stack we've seen a BREAK_STMT. */
4706 : :
4707 : : void
4708 : 3580676 : note_break_stmt (void)
4709 : : {
4710 : 3580676 : if (switch_stack && !switch_stack->in_loop_body_p)
4711 : 1569318 : switch_stack->break_stmt_seen_p = true;
4712 : 3580676 : }
4713 : :
4714 : : /* Note the start of processing of an iteration statement's body.
4715 : : The note_break_stmt function will do nothing while processing it.
4716 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4717 : :
4718 : : bool
4719 : 15655852 : note_iteration_stmt_body_start (void)
4720 : : {
4721 : 15655852 : if (!switch_stack)
4722 : : return false;
4723 : 120239 : bool ret = switch_stack->in_loop_body_p;
4724 : 120239 : switch_stack->in_loop_body_p = true;
4725 : 120239 : return ret;
4726 : : }
4727 : :
4728 : : /* Note the end of processing of an iteration statement's body. */
4729 : :
4730 : : void
4731 : 15655852 : note_iteration_stmt_body_end (bool prev)
4732 : : {
4733 : 15655852 : if (switch_stack)
4734 : 120239 : switch_stack->in_loop_body_p = prev;
4735 : 15655852 : }
4736 : :
4737 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4738 : : condition. Note that if TYPE and VALUE are already integral we don't
4739 : : really do the conversion because the language-independent
4740 : : warning/optimization code will work better that way. */
4741 : :
4742 : : static tree
4743 : 4038396 : case_conversion (tree type, tree value)
4744 : : {
4745 : 4038396 : if (value == NULL_TREE)
4746 : : return value;
4747 : :
4748 : 1745203 : value = mark_rvalue_use (value);
4749 : :
4750 : 1745203 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4751 : 982025 : type = type_promotes_to (type);
4752 : :
4753 : 1745203 : tree ovalue = value;
4754 : : /* The constant-expression VALUE shall be a converted constant expression
4755 : : of the adjusted type of the switch condition, which doesn't allow
4756 : : narrowing conversions. */
4757 : 1745203 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4758 : :
4759 : 1745203 : if (cxx_dialect >= cxx11
4760 : 1745203 : && (SCOPED_ENUM_P (type)
4761 : 978900 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4762 : : /* Use the converted value. */;
4763 : : else
4764 : : /* The already integral case. */
4765 : : value = ovalue;
4766 : :
4767 : 1745203 : return cxx_constant_value (value);
4768 : : }
4769 : :
4770 : : /* Note that we've seen a definition of a case label, and complain if this
4771 : : is a bad place for one. */
4772 : :
4773 : : tree
4774 : 3859949 : finish_case_label (location_t loc, tree low_value, tree high_value)
4775 : : {
4776 : 3859949 : tree cond, r;
4777 : 3859949 : cp_binding_level *p;
4778 : 3859949 : tree type;
4779 : :
4780 : 3859949 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4781 : 399334 : switch_stack->has_default_p = true;
4782 : :
4783 : 3859949 : if (processing_template_decl)
4784 : : {
4785 : 1840628 : tree label;
4786 : :
4787 : : /* For templates, just add the case label; we'll do semantic
4788 : : analysis at instantiation-time. But diagnose case labels
4789 : : in expansion statements with switch outside of it here. */
4790 : 1840628 : if (in_expansion_stmt)
4791 : 39 : for (cp_binding_level *b = current_binding_level;
4792 : 78 : b != switch_stack->level; b = b->level_chain)
4793 : 63 : if (b->kind == sk_template_for && b->this_entity)
4794 : : {
4795 : 24 : auto_diagnostic_group d;
4796 : 24 : error ("jump to case label");
4797 : 24 : inform (EXPR_LOCATION (b->this_entity),
4798 : : " enters %<template for%> statement");
4799 : 24 : return error_mark_node;
4800 : 24 : }
4801 : 1840604 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4802 : 1840604 : return add_stmt (build_case_label (low_value, high_value, label));
4803 : : }
4804 : :
4805 : : /* Find the condition on which this switch statement depends. */
4806 : 2019321 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4807 : 2019321 : if (cond && TREE_CODE (cond) == TREE_LIST)
4808 : 0 : cond = TREE_VALUE (cond);
4809 : :
4810 : 2019321 : int chk_switch_goto = check_switch_goto (switch_stack->level, NULL_TREE);
4811 : 2019321 : if (!chk_switch_goto)
4812 : 87 : return error_mark_node;
4813 : :
4814 : 2019234 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4815 : 2019234 : if (type == error_mark_node)
4816 : : return error_mark_node;
4817 : :
4818 : 2019198 : low_value = case_conversion (type, low_value);
4819 : 2019198 : high_value = case_conversion (type, high_value);
4820 : :
4821 : 2019198 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4822 : :
4823 : 2019198 : if (r != error_mark_node && chk_switch_goto == 2)
4824 : 23 : check_switch_goto (switch_stack->level, r);
4825 : :
4826 : : /* After labels, make any new cleanups in the function go into their
4827 : : own new (temporary) binding contour. */
4828 : 2019198 : for (p = current_binding_level;
4829 : 8501915 : p->kind != sk_function_parms;
4830 : 6482717 : p = p->level_chain)
4831 : 6482717 : p->more_cleanups_ok = 0;
4832 : :
4833 : : return r;
4834 : : }
4835 : :
4836 : : struct typename_info {
4837 : : tree scope;
4838 : : tree name;
4839 : : tree template_id;
4840 : : bool enum_p;
4841 : : bool class_p;
4842 : : bool union_p;
4843 : : };
4844 : :
4845 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4846 : : {
4847 : : typedef typename_info *compare_type;
4848 : :
4849 : : /* Hash a TYPENAME_TYPE. */
4850 : :
4851 : : static hashval_t
4852 : 396995099 : hash (tree context, tree fullname)
4853 : : {
4854 : 396995099 : hashval_t hash = 0;
4855 : 396995099 : hash = iterative_hash_object (context, hash);
4856 : 396995099 : hash = iterative_hash_object (fullname, hash);
4857 : 396995099 : return hash;
4858 : : }
4859 : :
4860 : : static hashval_t
4861 : 57602146 : hash (const typename_info *ti)
4862 : : {
4863 : 57602146 : return typename_hasher::hash (ti->scope, ti->template_id);
4864 : : }
4865 : :
4866 : : static hashval_t
4867 : 339392953 : hash (tree t)
4868 : : {
4869 : 339392953 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4870 : : }
4871 : :
4872 : : /* Compare two TYPENAME_TYPEs. */
4873 : :
4874 : : static bool
4875 : 395106136 : equal (tree t1, const typename_info *t2)
4876 : : {
4877 : 395106136 : return (TYPE_IDENTIFIER (t1) == t2->name
4878 : 115824880 : && TYPE_CONTEXT (t1) == t2->scope
4879 : 29579934 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4880 : 29568015 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4881 : 29567961 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p
4882 : 424197101 : && TYPENAME_IS_UNION_P (t1) == t2->union_p);
4883 : : }
4884 : : };
4885 : :
4886 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4887 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4888 : :
4889 : : Returns the new TYPENAME_TYPE. */
4890 : :
4891 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4892 : :
4893 : : tree
4894 : 57602146 : build_typename_type (tree context, tree name, tree fullname,
4895 : : enum tag_types tag_type)
4896 : : {
4897 : 57602146 : typename_info ti;
4898 : :
4899 : 57602146 : if (typename_htab == NULL)
4900 : 19204 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4901 : :
4902 : 57602146 : ti.scope = FROB_CONTEXT (context);
4903 : 57602146 : ti.name = name;
4904 : 57602146 : ti.template_id = fullname;
4905 : 57602146 : ti.enum_p = tag_type == enum_type;
4906 : 57602146 : ti.class_p = (tag_type == class_type || tag_type == record_type);
4907 : 57602146 : ti.union_p = tag_type == union_type;
4908 : 57602146 : hashval_t hash = typename_hasher::hash (&ti);
4909 : :
4910 : : /* See if we already have this type. */
4911 : 57602146 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4912 : 57602146 : tree t = *e;
4913 : 57602146 : if (*e)
4914 : : t = *e;
4915 : : else
4916 : : {
4917 : : /* Build the TYPENAME_TYPE. */
4918 : 28511181 : t = cxx_make_type (TYPENAME_TYPE);
4919 : 28511181 : TYPE_CONTEXT (t) = ti.scope;
4920 : 28511181 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4921 : 28511181 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4922 : 28511181 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4923 : 28511181 : TYPENAME_IS_UNION_P (t) = ti.union_p;
4924 : :
4925 : : /* Build the corresponding TYPE_DECL. */
4926 : 28511181 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4927 : 28511181 : TYPE_NAME (t) = d;
4928 : 28511181 : TYPE_STUB_DECL (t) = d;
4929 : 28511181 : DECL_CONTEXT (d) = ti.scope;
4930 : 28511181 : DECL_ARTIFICIAL (d) = 1;
4931 : :
4932 : : /* Store it in the hash table. */
4933 : 28511181 : *e = t;
4934 : :
4935 : : /* TYPENAME_TYPEs must always be compared structurally, because
4936 : : they may or may not resolve down to another type depending on
4937 : : the currently open classes. */
4938 : 28511181 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4939 : : }
4940 : :
4941 : 57602146 : return t;
4942 : : }
4943 : :
4944 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4945 : : provided to name the type. Returns an appropriate type, unless an
4946 : : error occurs, in which case error_mark_node is returned. If we
4947 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4948 : : return that, rather than the _TYPE it corresponds to, in other
4949 : : cases we look through the type decl. If TF_ERROR is set, complain
4950 : : about errors, otherwise be quiet. */
4951 : :
4952 : : tree
4953 : 203474776 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4954 : : tsubst_flags_t complain)
4955 : : {
4956 : 203474776 : tree fullname;
4957 : 203474776 : tree t;
4958 : 203474776 : bool want_template;
4959 : :
4960 : 203474776 : if (name == error_mark_node
4961 : 203474774 : || context == NULL_TREE
4962 : 203474774 : || context == error_mark_node)
4963 : : return error_mark_node;
4964 : :
4965 : 203474758 : if (TYPE_P (name))
4966 : : {
4967 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4968 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4969 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4970 : 0 : name = TYPE_IDENTIFIER (name);
4971 : : else
4972 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4973 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4974 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4975 : 0 : CLASSTYPE_TI_ARGS (name));
4976 : : }
4977 : 203474758 : else if (TREE_CODE (name) == TYPE_DECL)
4978 : 0 : name = DECL_NAME (name);
4979 : :
4980 : 203474758 : fullname = name;
4981 : :
4982 : 203474758 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4983 : : {
4984 : 6376941 : name = TREE_OPERAND (name, 0);
4985 : 6376941 : if (DECL_TYPE_TEMPLATE_P (name))
4986 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4987 : 6376941 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4988 : : {
4989 : 0 : if (complain & tf_error)
4990 : 0 : error ("%qD is not a type", name);
4991 : 0 : return error_mark_node;
4992 : : }
4993 : : }
4994 : 203474758 : if (TREE_CODE (name) == TEMPLATE_DECL)
4995 : : {
4996 : 0 : if (complain & tf_error)
4997 : 0 : error ("%qD used without template arguments", name);
4998 : 0 : return error_mark_node;
4999 : : }
5000 : 203474758 : else if (is_overloaded_fn (name))
5001 : : {
5002 : 3 : if (complain & tf_error)
5003 : 3 : error ("%qD is a function, not a type", name);
5004 : 3 : return error_mark_node;
5005 : : }
5006 : 203474755 : gcc_assert (identifier_p (name));
5007 : 203474755 : gcc_assert (TYPE_P (context));
5008 : :
5009 : 203474755 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5010 : : /* This can happen for C++17 variadic using (c++/88986). */;
5011 : 191507110 : else if (!MAYBE_CLASS_TYPE_P (context))
5012 : : {
5013 : 6 : if (complain & tf_error)
5014 : 6 : error ("%q#T is not a class", context);
5015 : 6 : return error_mark_node;
5016 : : }
5017 : :
5018 : : /* When the CONTEXT is a dependent type, NAME could refer to a
5019 : : dependent base class of CONTEXT. But look inside it anyway
5020 : : if CONTEXT is a currently open scope, in case it refers to a
5021 : : member of the current instantiation or a non-dependent base;
5022 : : lookup will stop when we hit a dependent base. */
5023 : 203474749 : if (!dependent_scope_p (context))
5024 : : {
5025 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5026 : : (as per [temp.res.general]/3), unless
5027 : : - the tag corresponds to a class-key or 'enum' so
5028 : : [basic.lookup.elab] applies, or
5029 : : - the tag corresponds to scope_type or tf_qualifying_scope is
5030 : : set so [basic.lookup.qual]/1 applies, or
5031 : : - we're inside a base-specifier so [class.derived.general]/2 applies;
5032 : : the tag will already be class_type in that case.
5033 : : TODO: If we'd set/track the scope_type tag thoroughly on all
5034 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5035 : : tf_qualifying_scope flag. */
5036 : 300291092 : bool want_type = (tag_type != none_type && tag_type != typename_type)
5037 : 150145546 : || (complain & tf_qualifying_scope);
5038 : 150145546 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5039 : : }
5040 : : else
5041 : : t = NULL_TREE;
5042 : :
5043 : 221544936 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5044 : 53330121 : return build_typename_type (context, name, fullname, tag_type);
5045 : :
5046 : 150144628 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5047 : :
5048 : 150144628 : if (!t)
5049 : : {
5050 : 18069269 : if (complain & tf_error)
5051 : : {
5052 : 250 : if (!COMPLETE_TYPE_P (context))
5053 : 18 : cxx_incomplete_type_error (NULL_TREE, context);
5054 : : else
5055 : 463 : error (want_template ? G_("no class template named %q#T in %q#T")
5056 : : : G_("no type named %q#T in %q#T"), name, context);
5057 : : }
5058 : 18069269 : return error_mark_node;
5059 : : }
5060 : :
5061 : : /* Pull out the template from an injected-class-name (or multiple). */
5062 : 132075359 : if (want_template)
5063 : 4868058 : t = maybe_get_template_decl_from_type_decl (t);
5064 : :
5065 : 132075359 : if (TREE_CODE (t) == TREE_LIST)
5066 : : {
5067 : 3 : if (complain & tf_error)
5068 : : {
5069 : 3 : auto_diagnostic_group d;
5070 : 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
5071 : 3 : print_candidates (t);
5072 : 3 : }
5073 : 3 : return error_mark_node;
5074 : : }
5075 : :
5076 : 132075356 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5077 : : {
5078 : 6 : if (complain & tf_error)
5079 : 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
5080 : : context, name, t);
5081 : 6 : return error_mark_node;
5082 : : }
5083 : 132075350 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
5084 : : {
5085 : 987 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5086 : 1977 : && DECL_TYPE_TEMPLATE_P (t))
5087 : : /* The caller permits this typename-specifier to name a template
5088 : : (because it appears in a CTAD-enabled context). */;
5089 : : else
5090 : : {
5091 : 23 : if (complain & tf_error)
5092 : 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
5093 : : context, name, t);
5094 : 23 : return error_mark_node;
5095 : : }
5096 : : }
5097 : :
5098 : 132075327 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5099 : : context, complain))
5100 : 49 : return error_mark_node;
5101 : :
5102 : 132075278 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5103 : 976 : return make_template_placeholder (t);
5104 : :
5105 : 132074302 : if (want_template)
5106 : : {
5107 : 4868052 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5108 : : NULL_TREE, context,
5109 : : complain | tf_user);
5110 : 4868052 : if (t == error_mark_node)
5111 : : return error_mark_node;
5112 : 4868047 : t = TYPE_NAME (t);
5113 : : }
5114 : :
5115 : 132074297 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5116 : 3424536 : t = TREE_TYPE (t);
5117 : :
5118 : 132074297 : maybe_record_typedef_use (t);
5119 : :
5120 : 132074297 : return t;
5121 : : }
5122 : :
5123 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5124 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5125 : : in which case error_mark_node is returned.
5126 : :
5127 : : If PARM_LIST is non-NULL, also make sure that the template parameter
5128 : : list of TEMPLATE_DECL matches.
5129 : :
5130 : : If COMPLAIN zero, don't complain about any errors that occur. */
5131 : :
5132 : : tree
5133 : 82808 : make_unbound_class_template (tree context, tree name, tree parm_list,
5134 : : tsubst_flags_t complain)
5135 : : {
5136 : 82808 : if (TYPE_P (name))
5137 : 0 : name = TYPE_IDENTIFIER (name);
5138 : 82808 : else if (DECL_P (name))
5139 : 0 : name = DECL_NAME (name);
5140 : 82808 : gcc_assert (identifier_p (name));
5141 : :
5142 : 82808 : if (!dependent_type_p (context)
5143 : 82808 : || currently_open_class (context))
5144 : : {
5145 : 27614 : tree tmpl = NULL_TREE;
5146 : :
5147 : 27614 : if (MAYBE_CLASS_TYPE_P (context))
5148 : 27608 : tmpl = lookup_field (context, name, 0, false);
5149 : :
5150 : 27608 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5151 : 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5152 : :
5153 : 27602 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5154 : : {
5155 : 12 : if (complain & tf_error)
5156 : 9 : error ("no class template named %q#T in %q#T", name, context);
5157 : 12 : return error_mark_node;
5158 : : }
5159 : :
5160 : 27602 : if (parm_list
5161 : 27602 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5162 : : {
5163 : 0 : if (complain & tf_error)
5164 : : {
5165 : 0 : auto_diagnostic_group d;
5166 : 0 : error ("template parameters do not match template %qD", tmpl);
5167 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
5168 : : "%qD declared here", tmpl);
5169 : 0 : }
5170 : 0 : return error_mark_node;
5171 : : }
5172 : :
5173 : 27602 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5174 : : complain))
5175 : 0 : return error_mark_node;
5176 : :
5177 : : return tmpl;
5178 : : }
5179 : :
5180 : 55194 : return make_unbound_class_template_raw (context, name, parm_list);
5181 : : }
5182 : :
5183 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
5184 : :
5185 : : tree
5186 : 55238 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5187 : : {
5188 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
5189 : 55238 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5190 : 55238 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5191 : 55238 : TREE_TYPE (t) = NULL_TREE;
5192 : 55238 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5193 : :
5194 : : /* Build the corresponding TEMPLATE_DECL. */
5195 : 55238 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5196 : 55238 : TYPE_NAME (t) = d;
5197 : 55238 : TYPE_STUB_DECL (t) = d;
5198 : 55238 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5199 : 55238 : DECL_ARTIFICIAL (d) = 1;
5200 : 55238 : DECL_TEMPLATE_PARMS (d) = parm_list;
5201 : :
5202 : 55238 : return t;
5203 : : }
5204 : :
5205 : :
5206 : :
5207 : : /* Push the declarations of builtin types into the global namespace.
5208 : : RID_INDEX is the index of the builtin type in the array
5209 : : RID_POINTERS. NAME is the name used when looking up the builtin
5210 : : type. TYPE is the _TYPE node for the builtin type.
5211 : :
5212 : : The calls to set_global_binding below should be
5213 : : eliminated. Built-in types should not be looked up name; their
5214 : : names are keywords that the parser can recognize. However, there
5215 : : is code in c-common.cc that uses identifier_global_value to look up
5216 : : built-in types by name. */
5217 : :
5218 : : void
5219 : 3964690 : record_builtin_type (enum rid rid_index,
5220 : : const char* name,
5221 : : tree type)
5222 : : {
5223 : 3964690 : tree decl = NULL_TREE;
5224 : :
5225 : 3964690 : if (name)
5226 : : {
5227 : 2321912 : tree tname = get_identifier (name);
5228 : 2321912 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5229 : 2321912 : DECL_ARTIFICIAL (tdecl) = 1;
5230 : 2321912 : set_global_binding (tdecl);
5231 : 2321912 : decl = tdecl;
5232 : : }
5233 : :
5234 : 3964690 : if ((int) rid_index < (int) RID_MAX)
5235 : 2418546 : if (tree rname = ridpointers[(int) rid_index])
5236 : 1838742 : if (!decl || DECL_NAME (decl) != rname)
5237 : : {
5238 : 1352876 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5239 : 1352876 : DECL_ARTIFICIAL (rdecl) = 1;
5240 : 1352876 : set_global_binding (rdecl);
5241 : 1352876 : if (!decl)
5242 : : decl = rdecl;
5243 : : }
5244 : :
5245 : 3674788 : if (decl)
5246 : : {
5247 : 3384886 : if (!TYPE_NAME (type))
5248 : 2805082 : TYPE_NAME (type) = decl;
5249 : 3384886 : debug_hooks->type_decl (decl, 0);
5250 : : }
5251 : 3964690 : }
5252 : :
5253 : : /* Push a type into the namespace so that the back ends ignore it. */
5254 : :
5255 : : static void
5256 : 289902 : record_unknown_type (tree type, const char* name)
5257 : : {
5258 : 289902 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5259 : : TYPE_DECL, get_identifier (name), type));
5260 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5261 : 289902 : DECL_IGNORED_P (decl) = 1;
5262 : 289902 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5263 : 289902 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5264 : 289902 : SET_TYPE_ALIGN (type, 1);
5265 : 289902 : TYPE_USER_ALIGN (type) = 0;
5266 : 289902 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5267 : 289902 : }
5268 : :
5269 : : /* Create all the predefined identifiers. */
5270 : :
5271 : : static void
5272 : 96634 : initialize_predefined_identifiers (void)
5273 : : {
5274 : 96634 : struct predefined_identifier
5275 : : {
5276 : : const char *name; /* Name. */
5277 : : tree *node; /* Node to store it in. */
5278 : : cp_identifier_kind kind; /* Kind of identifier. */
5279 : : };
5280 : :
5281 : : /* A table of identifiers to create at startup. */
5282 : 96634 : static const predefined_identifier predefined_identifiers[] = {
5283 : : {"C++", &lang_name_cplusplus, cik_normal},
5284 : : {"C", &lang_name_c, cik_normal},
5285 : : /* Some of these names have a trailing space so that it is
5286 : : impossible for them to conflict with names written by users. */
5287 : : {"__ct ", &ctor_identifier, cik_ctor},
5288 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
5289 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
5290 : : {"__dt ", &dtor_identifier, cik_dtor},
5291 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
5292 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
5293 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
5294 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
5295 : : {"__in_chrg", &in_charge_identifier, cik_normal},
5296 : : {"__as_base ", &as_base_identifier, cik_normal},
5297 : : {"this", &this_identifier, cik_normal},
5298 : : {"__delta", &delta_identifier, cik_normal},
5299 : : {"__pfn", &pfn_identifier, cik_normal},
5300 : : {"_vptr", &vptr_identifier, cik_normal},
5301 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
5302 : : {"::", &global_identifier, cik_normal},
5303 : : /* The demangler expects anonymous namespaces to be called
5304 : : something starting with '_GLOBAL__N_'. It no longer needs
5305 : : to be unique to the TU. */
5306 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
5307 : : {"auto", &auto_identifier, cik_normal},
5308 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
5309 : : {"initializer_list", &init_list_identifier, cik_normal},
5310 : : {"__for_range ", &for_range__identifier, cik_normal},
5311 : : {"__for_begin ", &for_begin__identifier, cik_normal},
5312 : : {"__for_end ", &for_end__identifier, cik_normal},
5313 : : {"__for_range", &for_range_identifier, cik_normal},
5314 : : {"__for_begin", &for_begin_identifier, cik_normal},
5315 : : {"__for_end", &for_end_identifier, cik_normal},
5316 : : {"abi_tag", &abi_tag_identifier, cik_normal},
5317 : : {"aligned", &aligned_identifier, cik_normal},
5318 : : {"begin", &begin_identifier, cik_normal},
5319 : : {"end", &end_identifier, cik_normal},
5320 : : {"get", &get__identifier, cik_normal},
5321 : : {"gnu", &gnu_identifier, cik_normal},
5322 : : {"tuple_element", &tuple_element_identifier, cik_normal},
5323 : : {"tuple_size", &tuple_size_identifier, cik_normal},
5324 : : {"type", &type_identifier, cik_normal},
5325 : : {"value", &value_identifier, cik_normal},
5326 : : {"_FUN", &fun_identifier, cik_normal},
5327 : : {"__closure", &closure_identifier, cik_normal},
5328 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
5329 : : {"heap ", &heap_identifier, cik_normal},
5330 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
5331 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
5332 : : {"heap []", &heap_vec_identifier, cik_normal},
5333 : : {"omp", &omp_identifier, cik_normal},
5334 : : {"internal ", &internal_identifier, cik_normal},
5335 : : {NULL, NULL, cik_normal}
5336 : : };
5337 : :
5338 : 4638432 : for (const predefined_identifier *pid = predefined_identifiers;
5339 : 4638432 : pid->name; ++pid)
5340 : : {
5341 : 4541798 : *pid->node = get_identifier (pid->name);
5342 : : /* Some of these identifiers already have a special kind. */
5343 : 4541798 : if (pid->kind != cik_normal)
5344 : 773072 : set_identifier_kind (*pid->node, pid->kind);
5345 : : }
5346 : 96634 : }
5347 : :
5348 : : /* Build a specific variant of operator new. */
5349 : :
5350 : : static void
5351 : 172373 : cxx_build_operator_new (tree newtype)
5352 : : {
5353 : 172373 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5354 : 172373 : DECL_IS_MALLOC (opnew) = 1;
5355 : 172373 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5356 : 172373 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5357 : 172373 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5358 : 172373 : DECL_IS_MALLOC (opnew) = 1;
5359 : 172373 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5360 : 172373 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5361 : 172373 : }
5362 : :
5363 : : /* Build a specific variant of operator delete. */
5364 : :
5365 : : static void
5366 : 325117 : cxx_build_operator_delete (tree deltype)
5367 : : {
5368 : 325117 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5369 : 325117 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5370 : 325117 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5371 : 325117 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5372 : 325117 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5373 : 325117 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5374 : 325117 : }
5375 : :
5376 : : /* Declare all variants of operator new and delete. */
5377 : :
5378 : : static void
5379 : 96634 : cxx_init_operator_new_delete_decls (void)
5380 : : {
5381 : 96634 : tree newattrs, extvisattr;
5382 : 96634 : tree newtype, deltype;
5383 : 96634 : tree ptr_ftype_sizetype;
5384 : 96634 : tree new_eh_spec;
5385 : 96634 : tree void_ftype_ptr = build_function_type_list (void_type_node,
5386 : : ptr_type_node, NULL_TREE);
5387 : 96634 : void_ftype_ptr
5388 : 96634 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
5389 : :
5390 : 96634 : ptr_ftype_sizetype
5391 : 96634 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5392 : 96634 : if (cxx_dialect == cxx98)
5393 : : {
5394 : 14023 : tree bad_alloc_id;
5395 : 14023 : tree bad_alloc_type_node;
5396 : 14023 : tree bad_alloc_decl;
5397 : :
5398 : 14023 : push_nested_namespace (std_node);
5399 : 14023 : bad_alloc_id = get_identifier ("bad_alloc");
5400 : 14023 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5401 : 14023 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5402 : 14023 : bad_alloc_decl
5403 : 14023 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5404 : 14023 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5405 : 14023 : pop_nested_namespace (std_node);
5406 : :
5407 : 14023 : new_eh_spec
5408 : 14023 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5409 : : }
5410 : : else
5411 : 82611 : new_eh_spec = noexcept_false_spec;
5412 : :
5413 : 96634 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5414 : : NULL_TREE);
5415 : 96634 : newattrs = tree_cons (get_identifier ("alloc_size"),
5416 : : build_tree_list (NULL_TREE, integer_one_node),
5417 : : extvisattr);
5418 : 96634 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5419 : 96634 : newtype = build_exception_variant (newtype, new_eh_spec);
5420 : 96634 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5421 : 96634 : deltype = build_exception_variant (deltype, empty_except_spec);
5422 : 96634 : cxx_build_operator_new (newtype);
5423 : 96634 : cxx_build_operator_delete (deltype);
5424 : 96634 : if (flag_sized_deallocation)
5425 : : {
5426 : : /* Also push the sized deallocation variants:
5427 : : void operator delete(void*, std::size_t) throw();
5428 : : void operator delete[](void*, std::size_t) throw(); */
5429 : 77010 : tree void_ftype_ptr_size
5430 : 77010 : = build_function_type_list (void_type_node, ptr_type_node,
5431 : : size_type_node, NULL_TREE);
5432 : 77010 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5433 : : extvisattr);
5434 : 77010 : deltype = build_exception_variant (deltype, empty_except_spec);
5435 : 77010 : cxx_build_operator_delete (deltype);
5436 : : }
5437 : :
5438 : 96634 : if (aligned_new_threshold)
5439 : : {
5440 : 75739 : push_nested_namespace (std_node);
5441 : 75739 : tree align_id = get_identifier ("align_val_t");
5442 : 75739 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5443 : : NULL_TREE, /*scoped*/true, NULL);
5444 : 75739 : pop_nested_namespace (std_node);
5445 : :
5446 : : /* operator new (size_t, align_val_t); */
5447 : 75739 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5448 : : align_type_node, NULL_TREE);
5449 : 75739 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5450 : 75739 : newtype = build_exception_variant (newtype, new_eh_spec);
5451 : 75739 : cxx_build_operator_new (newtype);
5452 : :
5453 : : /* operator delete (void *, align_val_t); */
5454 : 75739 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5455 : : align_type_node, NULL_TREE);
5456 : 75739 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5457 : 75739 : deltype = build_exception_variant (deltype, empty_except_spec);
5458 : 75739 : cxx_build_operator_delete (deltype);
5459 : :
5460 : 75739 : if (flag_sized_deallocation)
5461 : : {
5462 : : /* operator delete (void *, size_t, align_val_t); */
5463 : 75734 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5464 : : size_type_node, align_type_node,
5465 : : NULL_TREE);
5466 : 75734 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5467 : 75734 : deltype = build_exception_variant (deltype, empty_except_spec);
5468 : 75734 : cxx_build_operator_delete (deltype);
5469 : : }
5470 : : }
5471 : 96634 : }
5472 : :
5473 : : /* Create the predefined scalar types of C,
5474 : : and some nodes representing standard constants (0, 1, (void *)0).
5475 : : Initialize the global binding level.
5476 : : Make definitions for built-in primitive functions. */
5477 : :
5478 : : void
5479 : 96634 : cxx_init_decl_processing (void)
5480 : : {
5481 : 96634 : tree void_ftype;
5482 : :
5483 : : /* Create all the identifiers we need. */
5484 : 96634 : initialize_predefined_identifiers ();
5485 : :
5486 : : /* Create the global variables. */
5487 : 96634 : push_to_top_level ();
5488 : :
5489 : 96634 : current_function_decl = NULL_TREE;
5490 : 96634 : current_binding_level = NULL;
5491 : : /* Enter the global namespace. */
5492 : 96634 : gcc_assert (global_namespace == NULL_TREE);
5493 : 96634 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5494 : : void_type_node);
5495 : 96634 : TREE_PUBLIC (global_namespace) = true;
5496 : 96634 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5497 : 96634 : DECL_CONTEXT (global_namespace)
5498 : 96634 : = build_translation_unit_decl (get_identifier (main_input_filename));
5499 : : /* Remember whether we want the empty class passing ABI change warning
5500 : : in this TU. */
5501 : 96634 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5502 : 123063 : = warn_abi && abi_version_crosses (12);
5503 : 96634 : debug_hooks->register_main_translation_unit
5504 : 96634 : (DECL_CONTEXT (global_namespace));
5505 : 96634 : begin_scope (sk_namespace, global_namespace);
5506 : 96634 : current_namespace = global_namespace;
5507 : :
5508 : 96634 : if (flag_visibility_ms_compat)
5509 : 6 : default_visibility = VISIBILITY_HIDDEN;
5510 : :
5511 : : /* Initially, C. */
5512 : 96634 : current_lang_name = lang_name_c;
5513 : :
5514 : : /* Create the `std' namespace. */
5515 : 96634 : push_namespace (get_identifier ("std"));
5516 : 96634 : std_node = current_namespace;
5517 : 96634 : pop_namespace ();
5518 : :
5519 : 96634 : flag_noexcept_type = (cxx_dialect >= cxx17);
5520 : :
5521 : 96634 : c_common_nodes_and_builtins ();
5522 : :
5523 : 96634 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5524 : 96634 : tree decl
5525 : 96634 : = add_builtin_function ("__builtin_is_constant_evaluated",
5526 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5527 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5528 : 96634 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5529 : :
5530 : : /* The concrete return type of __builtin_source_location is
5531 : : const std::source_location::__impl*, but we can't form the type
5532 : : at this point. So we initially declare it with an auto return
5533 : : type which we then "deduce" from require_deduced_type upon first use. */
5534 : 96634 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5535 : 96634 : decl = add_builtin_function ("__builtin_source_location",
5536 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5537 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5538 : 96634 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5539 : :
5540 : 96634 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5541 : : NULL_TREE);
5542 : 96634 : decl
5543 : 96634 : = add_builtin_function ("__builtin_is_corresponding_member",
5544 : : bool_vaftype,
5545 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5546 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5547 : 96634 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5548 : :
5549 : 96634 : decl
5550 : 96634 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5551 : : bool_vaftype,
5552 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5553 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5554 : 96634 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5555 : :
5556 : 96634 : if (cxx_dialect >= cxx26)
5557 : : {
5558 : 23674 : tree void_ptrintftype
5559 : 23674 : = build_function_type_list (void_type_node, ptr_type_node,
5560 : : integer_type_node, NULL_TREE);
5561 : 23674 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5562 : : void_ptrintftype,
5563 : : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5564 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5565 : 23674 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5566 : : }
5567 : :
5568 : 96634 : integer_two_node = build_int_cst (NULL_TREE, 2);
5569 : :
5570 : : /* Guess at the initial static decls size. */
5571 : 96634 : vec_alloc (static_decls, 500);
5572 : :
5573 : : /* ... and keyed classes. */
5574 : 96634 : vec_alloc (keyed_classes, 100);
5575 : :
5576 : 96634 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5577 : 96634 : truthvalue_type_node = boolean_type_node;
5578 : 96634 : truthvalue_false_node = boolean_false_node;
5579 : 96634 : truthvalue_true_node = boolean_true_node;
5580 : :
5581 : 96634 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5582 : 96634 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5583 : 96634 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5584 : 96634 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5585 : : NULL_TREE);
5586 : :
5587 : : #if 0
5588 : : record_builtin_type (RID_MAX, NULL, string_type_node);
5589 : : #endif
5590 : :
5591 : 96634 : delta_type_node = ptrdiff_type_node;
5592 : 96634 : vtable_index_type = ptrdiff_type_node;
5593 : :
5594 : 96634 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5595 : 96634 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5596 : :
5597 : : /* Create the conversion operator marker. This operator's DECL_NAME
5598 : : is in the identifier table, so we can use identifier equality to
5599 : : find it. */
5600 : 96634 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5601 : : void_ftype);
5602 : :
5603 : : /* C++ extensions */
5604 : :
5605 : 96634 : unknown_type_node = make_node (LANG_TYPE);
5606 : 96634 : record_unknown_type (unknown_type_node, "unknown type");
5607 : :
5608 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5609 : 96634 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5610 : :
5611 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5612 : : result. */
5613 : 96634 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5614 : 96634 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5615 : :
5616 : 96634 : init_list_type_node = make_node (LANG_TYPE);
5617 : 96634 : record_unknown_type (init_list_type_node, "init list");
5618 : :
5619 : : /* Used when parsing to distinguish parameter-lists () and (void). */
5620 : 96634 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5621 : :
5622 : 96634 : {
5623 : : /* Make sure we get a unique function type, so we can give
5624 : : its pointer type a name. (This wins for gdb.) */
5625 : 96634 : tree vfunc_type = make_node (FUNCTION_TYPE);
5626 : 96634 : TREE_TYPE (vfunc_type) = integer_type_node;
5627 : 96634 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5628 : 96634 : layout_type (vfunc_type);
5629 : :
5630 : 96634 : vtable_entry_type = build_pointer_type (vfunc_type);
5631 : : }
5632 : 96634 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5633 : :
5634 : 96634 : vtbl_type_node
5635 : 96634 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5636 : 96634 : layout_type (vtbl_type_node);
5637 : 96634 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5638 : 96634 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5639 : 96634 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5640 : 96634 : layout_type (vtbl_ptr_type_node);
5641 : 96634 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5642 : :
5643 : 96634 : push_namespace (get_identifier ("__cxxabiv1"));
5644 : 96634 : abi_node = current_namespace;
5645 : 96634 : pop_namespace ();
5646 : :
5647 : 96634 : any_targ_node = make_node (LANG_TYPE);
5648 : 96634 : record_unknown_type (any_targ_node, "any type");
5649 : :
5650 : : /* Now, C++. */
5651 : 96634 : current_lang_name = lang_name_cplusplus;
5652 : :
5653 : 96634 : if (aligned_new_threshold > 1
5654 : 96634 : && !pow2p_hwi (aligned_new_threshold))
5655 : : {
5656 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
5657 : : aligned_new_threshold);
5658 : 0 : aligned_new_threshold = 1;
5659 : : }
5660 : 96634 : if (aligned_new_threshold == -1)
5661 : 117515 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5662 : 96634 : if (aligned_new_threshold == 1)
5663 : 75736 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5664 : :
5665 : : /* Ensure attribs.cc is initialized. */
5666 : 96634 : init_attributes ();
5667 : 96634 : cxx_init_operator_new_delete_decls ();
5668 : :
5669 : : /* C++-specific nullptr initialization. */
5670 : 96634 : if (abi_version_at_least (9))
5671 : 96463 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5672 : 96634 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5673 : :
5674 : 96634 : if (! supports_one_only ())
5675 : 0 : flag_weak = 0;
5676 : :
5677 : 96634 : abort_fndecl
5678 : 96634 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5679 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5680 : 96634 : if (flag_weak)
5681 : : /* If no definition is available, resolve references to NULL. */
5682 : 96604 : declare_weak (abort_fndecl);
5683 : :
5684 : : /* Perform other language dependent initializations. */
5685 : 96634 : init_class_processing ();
5686 : 96634 : init_rtti_processing ();
5687 : 96634 : init_template_processing ();
5688 : :
5689 : 96634 : if (flag_exceptions)
5690 : 95393 : init_exception_processing ();
5691 : :
5692 : 96634 : if (flag_contracts)
5693 : 155 : init_terminate_fn ();
5694 : :
5695 : 96634 : if (modules_p ())
5696 : 4594 : init_modules (parse_in);
5697 : :
5698 : 96634 : make_fname_decl = cp_make_fname_decl;
5699 : 96634 : start_fname_decls ();
5700 : :
5701 : : /* Show we use EH for cleanups. */
5702 : 96634 : if (flag_exceptions)
5703 : 95393 : using_eh_for_cleanups ();
5704 : :
5705 : : /* Check that the hardware interference sizes are at least
5706 : : alignof(max_align_t), as required by the standard. */
5707 : 96634 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5708 : 96634 : if (OPTION_SET_P (param_destruct_interfere_size))
5709 : : {
5710 : 0 : if (param_destruct_interfere_size < max_align)
5711 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5712 : : "%d", param_destruct_interfere_size, max_align);
5713 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5714 : 0 : warning (OPT_Winterference_size,
5715 : : "%<--param destructive-interference-size=%d%> "
5716 : : "is less than %<--param l1-cache-line-size=%d%>",
5717 : : param_destruct_interfere_size, param_l1_cache_line_size);
5718 : : }
5719 : 96634 : else if (param_destruct_interfere_size)
5720 : : /* Assume the internal value is OK. */;
5721 : 0 : else if (param_l1_cache_line_size >= max_align)
5722 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5723 : : /* else leave it unset. */
5724 : :
5725 : 96634 : if (OPTION_SET_P (param_construct_interfere_size))
5726 : : {
5727 : 0 : if (param_construct_interfere_size < max_align)
5728 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5729 : : "%d", param_construct_interfere_size, max_align);
5730 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5731 : 0 : && param_l1_cache_line_size >= max_align)
5732 : 0 : warning (OPT_Winterference_size,
5733 : : "%<--param constructive-interference-size=%d%> "
5734 : : "is greater than %<--param l1-cache-line-size=%d%>",
5735 : : param_construct_interfere_size, param_l1_cache_line_size);
5736 : : }
5737 : 96634 : else if (param_construct_interfere_size)
5738 : : /* Assume the internal value is OK. */;
5739 : 0 : else if (param_l1_cache_line_size >= max_align)
5740 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5741 : 96634 : }
5742 : :
5743 : : /* Enter an abi node in global-module context. returns a cookie to
5744 : : give to pop_abi_namespace. */
5745 : :
5746 : : unsigned
5747 : 61352 : push_abi_namespace (tree node)
5748 : : {
5749 : 61352 : push_nested_namespace (node);
5750 : 61352 : push_visibility ("default", 2);
5751 : 61352 : unsigned flags = module_kind;
5752 : 61352 : module_kind = 0;
5753 : 61352 : return flags;
5754 : : }
5755 : :
5756 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5757 : : you. */
5758 : :
5759 : : void
5760 : 61352 : pop_abi_namespace (unsigned flags, tree node)
5761 : : {
5762 : 61352 : module_kind = flags;
5763 : 61352 : pop_visibility (2);
5764 : 61352 : pop_nested_namespace (node);
5765 : 61352 : }
5766 : :
5767 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5768 : : the decl, LOC is the location to give the decl, NAME is the
5769 : : initialization string and TYPE_DEP indicates whether NAME depended
5770 : : on the type of the function. We make use of that to detect
5771 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5772 : : at the point of first use, so we mustn't push the decl now. */
5773 : :
5774 : : static tree
5775 : 117207 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5776 : : {
5777 : 117207 : tree domain = NULL_TREE;
5778 : 117207 : tree init = NULL_TREE;
5779 : :
5780 : 117207 : if (!(type_dep && current_function_decl && in_template_context))
5781 : : {
5782 : 38063 : const char *name = NULL;
5783 : 38063 : bool release_name = false;
5784 : :
5785 : 38063 : if (current_function_decl == NULL_TREE)
5786 : : name = "top level";
5787 : 38049 : else if (type_dep == 0)
5788 : : {
5789 : : /* __FUNCTION__ */
5790 : 620 : name = fname_as_string (type_dep);
5791 : 620 : release_name = true;
5792 : : }
5793 : : else
5794 : : {
5795 : : /* __PRETTY_FUNCTION__ */
5796 : 37429 : gcc_checking_assert (type_dep == 1);
5797 : 37429 : name = cxx_printable_name (current_function_decl, 2);
5798 : : }
5799 : :
5800 : 38049 : if (!release_name)
5801 : : {
5802 : 37443 : cpp_string cstr = { 0, 0 }, strname;
5803 : 37443 : size_t len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5804 : 37443 : char *namep = XNEWVEC (char, len);
5805 : 37443 : snprintf (namep, len, "\"%s\"", name);
5806 : 37443 : strname.text = (unsigned char *) namep;
5807 : 37443 : strname.len = len - 1;
5808 : 37443 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
5809 : : {
5810 : 37443 : name = (const char *) cstr.text;
5811 : 37443 : release_name = true;
5812 : : }
5813 : :
5814 : 37443 : XDELETEVEC (namep);
5815 : : }
5816 : :
5817 : 38063 : size_t length = strlen (name);
5818 : 38063 : domain = build_index_type (size_int (length));
5819 : 38063 : init = build_string (length + 1, name);
5820 : 38063 : if (release_name)
5821 : 38063 : free (const_cast<char *> (name));
5822 : : }
5823 : :
5824 : 117207 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5825 : 117207 : type = build_cplus_array_type (type, domain);
5826 : :
5827 : 117207 : if (init)
5828 : 38063 : TREE_TYPE (init) = type;
5829 : : else
5830 : 79144 : init = error_mark_node;
5831 : :
5832 : 117207 : tree decl = build_decl (loc, VAR_DECL, id, type);
5833 : :
5834 : 117207 : TREE_READONLY (decl) = 1;
5835 : 117207 : DECL_ARTIFICIAL (decl) = 1;
5836 : 117207 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5837 : 117207 : TREE_STATIC (decl) = 1;
5838 : :
5839 : 117207 : TREE_USED (decl) = 1;
5840 : :
5841 : 117207 : SET_DECL_VALUE_EXPR (decl, init);
5842 : 117207 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5843 : : /* For decl_constant_var_p. */
5844 : 117207 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5845 : :
5846 : 117207 : if (current_function_decl)
5847 : : {
5848 : 117193 : DECL_CONTEXT (decl) = current_function_decl;
5849 : 117193 : decl = pushdecl_outermost_localscope (decl);
5850 : 117193 : if (decl != error_mark_node)
5851 : 117190 : add_decl_expr (decl);
5852 : : else
5853 : 3 : gcc_assert (seen_error ());
5854 : : }
5855 : : else
5856 : : {
5857 : 14 : DECL_THIS_STATIC (decl) = true;
5858 : 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5859 : : }
5860 : :
5861 : 117207 : return decl;
5862 : : }
5863 : :
5864 : : /* Install DECL as a builtin function at current global scope. Return
5865 : : the new decl (if we found an existing version). Also installs it
5866 : : into ::std, if it's not '_*'. */
5867 : :
5868 : : tree
5869 : 223345457 : cxx_builtin_function (tree decl)
5870 : : {
5871 : 223345457 : retrofit_lang_decl (decl);
5872 : :
5873 : 223345457 : DECL_ARTIFICIAL (decl) = 1;
5874 : 223345457 : SET_DECL_LANGUAGE (decl, lang_c);
5875 : : /* Runtime library routines are, by definition, available in an
5876 : : external shared object. */
5877 : 223345457 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5878 : 223345457 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5879 : :
5880 : 223345457 : tree id = DECL_NAME (decl);
5881 : 223345457 : const char *name = IDENTIFIER_POINTER (id);
5882 : 223345457 : bool hiding = false;
5883 : 223345457 : if (name[0] != '_' || name[1] != '_')
5884 : : /* In the user's namespace, it must be declared before use. */
5885 : : hiding = true;
5886 : 187292176 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5887 : 187292176 : && !startswith (name + 2, "builtin_")
5888 : 209777850 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5889 : : "_chk", strlen ("_chk") + 1))
5890 : : /* Treat __*_chk fortification functions as anticipated as well,
5891 : : unless they are __builtin_*_chk. */
5892 : : hiding = true;
5893 : :
5894 : : /* All builtins that don't begin with an '_' should additionally
5895 : : go in the 'std' namespace. */
5896 : 223345457 : if (name[0] != '_')
5897 : : {
5898 : 35920939 : tree std_decl = copy_decl (decl);
5899 : :
5900 : 35920939 : push_nested_namespace (std_node);
5901 : 35920939 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5902 : 35920939 : pushdecl (std_decl, hiding);
5903 : 35920939 : pop_nested_namespace (std_node);
5904 : : }
5905 : :
5906 : 223345457 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5907 : 223345457 : decl = pushdecl (decl, hiding);
5908 : :
5909 : 223345457 : return decl;
5910 : : }
5911 : :
5912 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5913 : : scope. This is to allow function specific options to add new machine
5914 : : dependent builtins when the target ISA changes via attribute((target(...)))
5915 : : which saves space on program startup if the program does not use non-generic
5916 : : ISAs. */
5917 : :
5918 : : tree
5919 : 717765 : cxx_builtin_function_ext_scope (tree decl)
5920 : : {
5921 : 717765 : push_nested_namespace (global_namespace);
5922 : 717765 : decl = cxx_builtin_function (decl);
5923 : 717765 : pop_nested_namespace (global_namespace);
5924 : :
5925 : 717765 : return decl;
5926 : : }
5927 : :
5928 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5929 : :
5930 : : tree
5931 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5932 : : {
5933 : 0 : retrofit_lang_decl (decl);
5934 : :
5935 : 0 : DECL_ARTIFICIAL (decl) = 1;
5936 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5937 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5938 : 0 : return pushdecl (decl);
5939 : : }
5940 : :
5941 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5942 : : function. Not called directly. */
5943 : :
5944 : : static tree
5945 : 1558587 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5946 : : int ecf_flags)
5947 : : {
5948 : 1558587 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5949 : 1558587 : DECL_EXTERNAL (fn) = 1;
5950 : 1558587 : TREE_PUBLIC (fn) = 1;
5951 : 1558587 : DECL_ARTIFICIAL (fn) = 1;
5952 : 1558587 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5953 : 1558587 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5954 : 1558587 : SET_DECL_LANGUAGE (fn, lang_c);
5955 : : /* Runtime library routines are, by definition, available in an
5956 : : external shared object. */
5957 : 1558587 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5958 : 1558587 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5959 : 1558587 : set_call_expr_flags (fn, ecf_flags);
5960 : 1558587 : return fn;
5961 : : }
5962 : :
5963 : : /* Returns the _DECL for a library function with C++ linkage. */
5964 : :
5965 : : static tree
5966 : 1090464 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5967 : : int ecf_flags)
5968 : : {
5969 : 1090464 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5970 : 1090464 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5971 : 1090464 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5972 : 1090464 : return fn;
5973 : : }
5974 : :
5975 : : /* Like build_library_fn, but takes a C string instead of an
5976 : : IDENTIFIER_NODE. */
5977 : :
5978 : : tree
5979 : 102896 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5980 : : {
5981 : 102896 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5982 : : }
5983 : :
5984 : : /* Like build_cp_library_fn, but takes a C string instead of an
5985 : : IDENTIFIER_NODE. */
5986 : :
5987 : : tree
5988 : 95484 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5989 : : {
5990 : 95484 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5991 : 95484 : ecf_flags);
5992 : : }
5993 : :
5994 : : /* Like build_library_fn, but also pushes the function so that we will
5995 : : be able to find it via get_global_binding. Also, the function
5996 : : may throw exceptions listed in RAISES. */
5997 : :
5998 : : tree
5999 : 365227 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6000 : : {
6001 : 365227 : if (raises)
6002 : 41123 : type = build_exception_variant (type, raises);
6003 : :
6004 : 365227 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
6005 : 365227 : return pushdecl_top_level (fn);
6006 : : }
6007 : :
6008 : : /* Like build_cp_library_fn, but also pushes the function so that it
6009 : : will be found by normal lookup. */
6010 : :
6011 : : static tree
6012 : 994980 : push_cp_library_fn (enum tree_code operator_code, tree type,
6013 : : int ecf_flags)
6014 : : {
6015 : 994980 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
6016 : : operator_code, type, ecf_flags);
6017 : 994980 : pushdecl (fn);
6018 : 994980 : if (flag_tm)
6019 : 3016 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
6020 : 994980 : return fn;
6021 : : }
6022 : :
6023 : : /* Like push_library_fn, but also note that this function throws
6024 : : and does not return. Used for __throw_foo and the like. */
6025 : :
6026 : : tree
6027 : 106624 : push_throw_library_fn (tree name, tree type)
6028 : : {
6029 : 106624 : tree fn = push_library_fn (name, type, NULL_TREE,
6030 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6031 : 106624 : return fn;
6032 : : }
6033 : :
6034 : : /* When we call finish_struct for an anonymous union, we create
6035 : : default copy constructors and such. But, an anonymous union
6036 : : shouldn't have such things; this function undoes the damage to the
6037 : : anonymous union type T.
6038 : :
6039 : : (The reason that we create the synthesized methods is that we don't
6040 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
6041 : : The first is an anonymous union; the second is just an ordinary
6042 : : union type.) */
6043 : :
6044 : : void
6045 : 106288 : fixup_anonymous_aggr (tree t)
6046 : : {
6047 : : /* Wipe out memory of synthesized methods. */
6048 : 106288 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6049 : 106288 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6050 : 106288 : TYPE_HAS_COPY_CTOR (t) = 0;
6051 : 106288 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6052 : 106288 : TYPE_HAS_COPY_ASSIGN (t) = 0;
6053 : 106288 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6054 : :
6055 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6056 : : invalid members. */
6057 : 555439 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6058 : : {
6059 : 449151 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6060 : 0 : *prev_p = DECL_CHAIN (probe);
6061 : : else
6062 : 449151 : prev_p = &DECL_CHAIN (probe);
6063 : :
6064 : 449151 : if (DECL_ARTIFICIAL (probe)
6065 : 449151 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6066 : 43388 : || TYPE_ANON_P (TREE_TYPE (probe))))
6067 : 165286 : continue;
6068 : :
6069 : 283865 : if (TREE_CODE (probe) != FIELD_DECL
6070 : 283811 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6071 : : {
6072 : : /* We already complained about static data members in
6073 : : finish_static_data_member_decl. */
6074 : 66 : if (!VAR_P (probe))
6075 : : {
6076 : 51 : auto_diagnostic_group d;
6077 : 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
6078 : 51 : TREE_CODE (t) == UNION_TYPE
6079 : : ? "%q#D invalid; an anonymous union may "
6080 : : "only have public non-static data members"
6081 : : : "%q#D invalid; an anonymous struct may "
6082 : : "only have public non-static data members", probe))
6083 : : {
6084 : 51 : static bool hint;
6085 : 51 : if (flag_permissive && !hint)
6086 : : {
6087 : 6 : hint = true;
6088 : 6 : inform (DECL_SOURCE_LOCATION (probe),
6089 : : "this flexibility is deprecated and will be "
6090 : : "removed");
6091 : : }
6092 : : }
6093 : 51 : }
6094 : : }
6095 : : }
6096 : :
6097 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6098 : 106288 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6099 : 106288 : unsigned store = 0;
6100 : 235615 : for (tree elt : vec)
6101 : 129327 : if (!is_overloaded_fn (elt))
6102 : 129306 : (*vec)[store++] = elt;
6103 : 106288 : vec_safe_truncate (vec, store);
6104 : :
6105 : : /* Wipe RTTI info. */
6106 : 106288 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
6107 : :
6108 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6109 : : assignment operators (because they cannot have these methods themselves).
6110 : : For anonymous unions this is already checked because they are not allowed
6111 : : in any union, otherwise we have to check it. */
6112 : 106288 : if (TREE_CODE (t) != UNION_TYPE)
6113 : : {
6114 : 20223 : tree field, type;
6115 : :
6116 : 20223 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6117 : : {
6118 : 9 : error_at (location_of (t), "anonymous struct with base classes");
6119 : : /* Avoid ICE after error on anon-struct9.C. */
6120 : 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
6121 : : }
6122 : :
6123 : 91439 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6124 : 71216 : if (TREE_CODE (field) == FIELD_DECL)
6125 : : {
6126 : 50892 : type = TREE_TYPE (field);
6127 : 50892 : if (CLASS_TYPE_P (type))
6128 : : {
6129 : 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
6130 : 6 : error ("member %q+#D with constructor not allowed "
6131 : : "in anonymous aggregate", field);
6132 : 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6133 : 0 : error ("member %q+#D with destructor not allowed "
6134 : : "in anonymous aggregate", field);
6135 : 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6136 : 0 : error ("member %q+#D with copy assignment operator "
6137 : : "not allowed in anonymous aggregate", field);
6138 : : }
6139 : : }
6140 : : }
6141 : 106288 : }
6142 : :
6143 : : /* Warn for an attribute located at LOCATION that appertains to the
6144 : : class type CLASS_TYPE that has not been properly placed after its
6145 : : class-key, in it class-specifier. */
6146 : :
6147 : : void
6148 : 108 : warn_misplaced_attr_for_class_type (location_t location,
6149 : : tree class_type)
6150 : : {
6151 : 108 : gcc_assert (OVERLOAD_TYPE_P (class_type));
6152 : :
6153 : 108 : auto_diagnostic_group d;
6154 : 108 : if (warning_at (location, OPT_Wattributes,
6155 : : "attribute ignored in declaration "
6156 : : "of %q#T", class_type))
6157 : 108 : inform (location,
6158 : : "attribute for %q#T must follow the %qs keyword",
6159 : : class_type, class_key_or_enum_as_string (class_type));
6160 : 108 : }
6161 : :
6162 : : /* Returns the cv-qualifiers that apply to the type specified
6163 : : by the DECLSPECS. */
6164 : :
6165 : : static int
6166 : 931250716 : get_type_quals (const cp_decl_specifier_seq *declspecs)
6167 : : {
6168 : 931250716 : int type_quals = TYPE_UNQUALIFIED;
6169 : :
6170 : 931250716 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6171 : 101736000 : type_quals |= TYPE_QUAL_CONST;
6172 : 931250716 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6173 : 1582771 : type_quals |= TYPE_QUAL_VOLATILE;
6174 : 931250716 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6175 : 34 : type_quals |= TYPE_QUAL_RESTRICT;
6176 : :
6177 : 931250716 : return type_quals;
6178 : : }
6179 : :
6180 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
6181 : : just declares a tagged type or anonymous union.
6182 : :
6183 : : Returns the type declared; or NULL_TREE if none. */
6184 : :
6185 : : tree
6186 : 32460096 : check_tag_decl (cp_decl_specifier_seq *declspecs,
6187 : : bool explicit_type_instantiation_p)
6188 : : {
6189 : 32460096 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6190 : 32460096 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6191 : : /* If a class, struct, or enum type is declared by the DECLSPECS
6192 : : (i.e, if a class-specifier, enum-specifier, or non-typename
6193 : : elaborated-type-specifier appears in the DECLSPECS),
6194 : : DECLARED_TYPE is set to the corresponding type. */
6195 : 32460096 : tree declared_type = NULL_TREE;
6196 : 32460096 : bool error_p = false;
6197 : :
6198 : 32460096 : if (declspecs->multiple_types_p)
6199 : 26 : error_at (smallest_type_location (declspecs),
6200 : : "multiple types in one declaration");
6201 : 32460070 : else if (declspecs->redefined_builtin_type)
6202 : : {
6203 : 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6204 : 21 : if (!in_system_header_at (loc))
6205 : 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
6206 : : declspecs->redefined_builtin_type);
6207 : 21 : return NULL_TREE;
6208 : : }
6209 : :
6210 : 32460075 : if (declspecs->type
6211 : 32460060 : && TYPE_P (declspecs->type)
6212 : 64725768 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6213 : 32264126 : && MAYBE_CLASS_TYPE_P (declspecs->type))
6214 : 1669699 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6215 : : declared_type = declspecs->type;
6216 : 195994 : else if (declspecs->type == error_mark_node)
6217 : 1459 : error_p = true;
6218 : :
6219 : 32460075 : if (type_uses_auto (declared_type))
6220 : : {
6221 : 8 : error_at (declspecs->locations[ds_type_spec],
6222 : : "%<auto%> can only be specified for variables "
6223 : : "or function declarations");
6224 : 8 : return error_mark_node;
6225 : : }
6226 : :
6227 : 32460067 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6228 : : declared_type = NULL_TREE;
6229 : :
6230 : 32460067 : if (!declared_type && !saw_friend && !error_p)
6231 : 91 : permerror (input_location, "declaration does not declare anything");
6232 : : /* Check for an anonymous union. */
6233 : 32264046 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6234 : 93758188 : && TYPE_UNNAMED_P (declared_type))
6235 : : {
6236 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
6237 : : can be omitted only when declaring a class (clause 9) or
6238 : : enumeration (7.2), that is, when the decl-specifier-seq contains
6239 : : either a class-specifier, an elaborated-type-specifier with
6240 : : a class-key (9.1), or an enum-specifier. In these cases and
6241 : : whenever a class-specifier or enum-specifier is present in the
6242 : : decl-specifier-seq, the identifiers in these specifiers are among
6243 : : the names being declared by the declaration (as class-name,
6244 : : enum-names, or enumerators, depending on the syntax). In such
6245 : : cases, and except for the declaration of an unnamed bit-field (9.6),
6246 : : the decl-specifier-seq shall introduce one or more names into the
6247 : : program, or shall redeclare a name introduced by a previous
6248 : : declaration. [Example:
6249 : : enum { }; // ill-formed
6250 : : typedef class { }; // ill-formed
6251 : : --end example] */
6252 : 106294 : if (saw_typedef)
6253 : : {
6254 : 6 : error_at (declspecs->locations[ds_typedef],
6255 : : "missing type-name in typedef-declaration");
6256 : 6 : return NULL_TREE;
6257 : : }
6258 : 106288 : /* Anonymous unions are objects, so they can have specifiers. */;
6259 : 106288 : SET_ANON_AGGR_TYPE_P (declared_type);
6260 : :
6261 : 106288 : if (TREE_CODE (declared_type) != UNION_TYPE)
6262 : 20223 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6263 : 20223 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6264 : : }
6265 : :
6266 : : else
6267 : : {
6268 : 32353682 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6269 : 9 : error_at (declspecs->locations[ds_inline],
6270 : : "%<inline%> can only be specified for functions");
6271 : 32353673 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6272 : 3 : error_at (declspecs->locations[ds_virtual],
6273 : : "%<virtual%> can only be specified for functions");
6274 : 32353670 : else if (saw_friend
6275 : 32353670 : && (!current_class_type
6276 : 1674398 : || current_scope () != current_class_type))
6277 : 0 : error_at (declspecs->locations[ds_friend],
6278 : : "%<friend%> can only be specified inside a class");
6279 : 32353670 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6280 : 3 : error_at (declspecs->locations[ds_explicit],
6281 : : "%<explicit%> can only be specified for constructors");
6282 : 32353667 : else if (declspecs->storage_class)
6283 : 3 : error_at (declspecs->locations[ds_storage_class],
6284 : : "a storage class can only be specified for objects "
6285 : : "and functions");
6286 : 32353664 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6287 : 9 : error_at (declspecs->locations[ds_const],
6288 : : "%<const%> can only be specified for objects and "
6289 : : "functions");
6290 : 32353655 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6291 : 3 : error_at (declspecs->locations[ds_volatile],
6292 : : "%<volatile%> can only be specified for objects and "
6293 : : "functions");
6294 : 32353652 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6295 : 3 : error_at (declspecs->locations[ds_restrict],
6296 : : "%<__restrict%> can only be specified for objects and "
6297 : : "functions");
6298 : 32353649 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6299 : 3 : error_at (declspecs->locations[ds_thread],
6300 : : "%<__thread%> can only be specified for objects "
6301 : : "and functions");
6302 : 32353646 : else if (saw_typedef)
6303 : 24 : warning_at (declspecs->locations[ds_typedef], 0,
6304 : : "%<typedef%> was ignored in this declaration");
6305 : 32353622 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6306 : 9 : error_at (declspecs->locations[ds_constexpr],
6307 : : "%qs cannot be used for type declarations", "constexpr");
6308 : 32353613 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6309 : 3 : error_at (declspecs->locations[ds_constinit],
6310 : : "%qs cannot be used for type declarations", "constinit");
6311 : 32353610 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6312 : 3 : error_at (declspecs->locations[ds_consteval],
6313 : : "%qs cannot be used for type declarations", "consteval");
6314 : : }
6315 : :
6316 : 32460061 : if (declspecs->attributes && warn_attributes && declared_type)
6317 : : {
6318 : 31 : location_t loc;
6319 : 25 : if (!CLASS_TYPE_P (declared_type)
6320 : 56 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6321 : : /* For a non-template class, use the name location. */
6322 : 22 : loc = location_of (declared_type);
6323 : : else
6324 : : /* For a template class (an explicit instantiation), use the
6325 : : current location. */
6326 : 9 : loc = input_location;
6327 : :
6328 : 31 : if (explicit_type_instantiation_p)
6329 : : /* [dcl.attr.grammar]/4:
6330 : :
6331 : : No attribute-specifier-seq shall appertain to an explicit
6332 : : instantiation. */
6333 : : {
6334 : 6 : auto_diagnostic_group d;
6335 : 6 : if (warning_at (loc, OPT_Wattributes,
6336 : : "attribute ignored in explicit instantiation %q#T",
6337 : : declared_type))
6338 : 6 : inform (loc,
6339 : : "no attribute can be applied to "
6340 : : "an explicit instantiation");
6341 : 6 : }
6342 : : else
6343 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
6344 : : }
6345 : :
6346 : 32460061 : if (declspecs->std_attributes
6347 : 83 : && declared_type
6348 : 32460141 : && any_nonignored_attribute_p (declspecs->std_attributes))
6349 : : {
6350 : 80 : auto_diagnostic_group d;
6351 : 80 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6352 : : "attribute ignored"))
6353 : 80 : inform (declspecs->locations[ds_std_attribute],
6354 : : "an attribute that appertains to a type-specifier is ignored");
6355 : 80 : }
6356 : :
6357 : : /* Diagnose invalid application of contracts, if any. */
6358 : 32460061 : if (find_contract (declspecs->attributes))
6359 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
6360 : : else
6361 : 32460060 : diagnose_misapplied_contracts (declspecs->std_attributes);
6362 : :
6363 : : return declared_type;
6364 : : }
6365 : :
6366 : : /* Called when a declaration is seen that contains no names to declare.
6367 : : If its type is a reference to a structure, union or enum inherited
6368 : : from a containing scope, shadow that tag name for the current scope
6369 : : with a forward reference.
6370 : : If its type defines a new named structure or union
6371 : : or defines an enum, it is valid but we need not do anything here.
6372 : : Otherwise, it is an error.
6373 : :
6374 : : C++: may have to grok the declspecs to learn about static,
6375 : : complain for anonymous unions.
6376 : :
6377 : : Returns the TYPE declared -- or NULL_TREE if none. */
6378 : :
6379 : : tree
6380 : 28415183 : shadow_tag (cp_decl_specifier_seq *declspecs)
6381 : : {
6382 : 28415183 : tree t = check_tag_decl (declspecs,
6383 : : /*explicit_type_instantiation_p=*/false);
6384 : :
6385 : 28415183 : if (!t)
6386 : : return NULL_TREE;
6387 : :
6388 : 28413711 : t = maybe_process_partial_specialization (t);
6389 : 28413711 : if (t == error_mark_node)
6390 : : return NULL_TREE;
6391 : :
6392 : : /* This is where the variables in an anonymous union are
6393 : : declared. An anonymous union declaration looks like:
6394 : : union { ... } ;
6395 : : because there is no declarator after the union, the parser
6396 : : sends that declaration here. */
6397 : 28412481 : if (ANON_AGGR_TYPE_P (t))
6398 : : {
6399 : 249 : fixup_anonymous_aggr (t);
6400 : :
6401 : 249 : if (TYPE_FIELDS (t))
6402 : : {
6403 : 249 : tree decl = grokdeclarator (/*declarator=*/NULL,
6404 : : declspecs, NORMAL, 0, NULL);
6405 : 249 : finish_anon_union (decl);
6406 : : }
6407 : : }
6408 : :
6409 : : return t;
6410 : : }
6411 : :
6412 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6413 : :
6414 : : tree
6415 : 345300868 : groktypename (cp_decl_specifier_seq *type_specifiers,
6416 : : const cp_declarator *declarator,
6417 : : bool is_template_arg)
6418 : : {
6419 : 345300868 : tree attrs;
6420 : 345300868 : tree type;
6421 : 54904071 : enum decl_context context
6422 : 345300868 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6423 : 345300868 : attrs = type_specifiers->attributes;
6424 : 345300868 : type_specifiers->attributes = NULL_TREE;
6425 : 345300868 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6426 : 345300868 : if (attrs && type != error_mark_node)
6427 : : {
6428 : 633 : if (CLASS_TYPE_P (type))
6429 : 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6430 : : "outside of definition", type);
6431 : 627 : else if (MAYBE_CLASS_TYPE_P (type))
6432 : : /* A template type parameter or other dependent type. */
6433 : 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6434 : : "type %qT without an associated declaration", type);
6435 : : else
6436 : 618 : cplus_decl_attributes (&type, attrs, 0);
6437 : : }
6438 : 345300868 : return type;
6439 : : }
6440 : :
6441 : : /* Process a DECLARATOR for a function-scope or namespace-scope
6442 : : variable or function declaration.
6443 : : (Function definitions go through start_function; class member
6444 : : declarations appearing in the body of the class go through
6445 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
6446 : : If an error occurs, the error_mark_node is returned instead.
6447 : :
6448 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6449 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6450 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
6451 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6452 : : implicitly initialized via a default constructor. It can also be
6453 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6454 : : mark the new decl as DECL_DECOMPOSITION_P.
6455 : :
6456 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6457 : : declaration.
6458 : :
6459 : : The scope represented by the context of the returned DECL is pushed
6460 : : (if it is not the global namespace) and is assigned to
6461 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
6462 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
6463 : :
6464 : : tree
6465 : 115565378 : start_decl (const cp_declarator *declarator,
6466 : : cp_decl_specifier_seq *declspecs,
6467 : : int initialized,
6468 : : tree attributes,
6469 : : tree prefix_attributes,
6470 : : tree *pushed_scope_p)
6471 : : {
6472 : 115565378 : tree decl;
6473 : 115565378 : tree context;
6474 : 115565378 : bool was_public;
6475 : 115565378 : int flags;
6476 : 115565378 : bool alias;
6477 : 115565378 : tree initial;
6478 : :
6479 : 115565378 : *pushed_scope_p = NULL_TREE;
6480 : :
6481 : 115565378 : if (prefix_attributes != error_mark_node)
6482 : 115565376 : attributes = attr_chainon (attributes, prefix_attributes);
6483 : :
6484 : 115565378 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6485 : : &attributes);
6486 : :
6487 : 115565378 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6488 : 115565378 : || decl == error_mark_node
6489 : 115563243 : || prefix_attributes == error_mark_node)
6490 : 2137 : return error_mark_node;
6491 : :
6492 : 115563241 : context = CP_DECL_CONTEXT (decl);
6493 : 115563241 : if (context != global_namespace)
6494 : 17919962 : *pushed_scope_p = push_scope (context);
6495 : :
6496 : 115563241 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6497 : : {
6498 : 27 : error_at (DECL_SOURCE_LOCATION (decl),
6499 : : "typedef %qD is initialized (use %qs instead)",
6500 : : decl, "decltype");
6501 : 27 : return error_mark_node;
6502 : : }
6503 : :
6504 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6505 : : with attribute validation. */
6506 : 115563214 : initial = DECL_INITIAL (decl);
6507 : :
6508 : 115563214 : if (initialized)
6509 : : {
6510 : 50919657 : if (! toplevel_bindings_p ()
6511 : 50919657 : && DECL_EXTERNAL (decl))
6512 : 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6513 : : decl);
6514 : 50919657 : DECL_EXTERNAL (decl) = 0;
6515 : 50919657 : if (toplevel_bindings_p ())
6516 : 4463056 : TREE_STATIC (decl) = 1;
6517 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6518 : : even though we might not yet have the initializer expression. */
6519 : 50919657 : if (!DECL_INITIAL (decl))
6520 : 50566903 : DECL_INITIAL (decl) = error_mark_node;
6521 : : }
6522 : 115563214 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6523 : :
6524 : 115563214 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6525 : 3863 : record_key_method_defined (decl);
6526 : :
6527 : : /* If this is a typedef that names the class for linkage purposes
6528 : : (7.1.3p8), apply any attributes directly to the type. */
6529 : 115563214 : if (TREE_CODE (decl) == TYPE_DECL
6530 : 20278959 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6531 : 123508945 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6532 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
6533 : : else
6534 : : flags = 0;
6535 : :
6536 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
6537 : 115563214 : cplus_decl_attributes (&decl, attributes, flags);
6538 : :
6539 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6540 : : assist with attribute validation. */
6541 : 115563214 : DECL_INITIAL (decl) = initial;
6542 : :
6543 : : /* Dllimported symbols cannot be defined. Static data members (which
6544 : : can be initialized in-class and dllimported) go through grokfield,
6545 : : not here, so we don't need to exclude those decls when checking for
6546 : : a definition. */
6547 : 115563214 : if (initialized && DECL_DLLIMPORT_P (decl))
6548 : : {
6549 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
6550 : : "definition of %q#D is marked %<dllimport%>", decl);
6551 : 0 : DECL_DLLIMPORT_P (decl) = 0;
6552 : : }
6553 : :
6554 : : /* If #pragma weak was used, mark the decl weak now. */
6555 : 115563214 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6556 : 44365212 : maybe_apply_pragma_weak (decl);
6557 : :
6558 : 115563214 : if (TREE_CODE (decl) == FUNCTION_DECL
6559 : 38432550 : && DECL_DECLARED_INLINE_P (decl)
6560 : 1989830 : && DECL_UNINLINABLE (decl)
6561 : 115563214 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6562 : : {
6563 : 0 : auto_urlify_attributes sentinel;
6564 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6565 : : "inline function %qD given attribute %qs", decl, "noinline");
6566 : 0 : }
6567 : :
6568 : 115563214 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6569 : : {
6570 : 762454 : bool this_tmpl = (current_template_depth
6571 : 762454 : > template_class_depth (context));
6572 : 762454 : if (VAR_P (decl))
6573 : : {
6574 : 391085 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6575 : 391085 : if (field == NULL_TREE
6576 : 391085 : || !(VAR_P (field) || variable_template_p (field)))
6577 : 7 : error ("%q+#D is not a static data member of %q#T", decl, context);
6578 : 391078 : else if (variable_template_p (field)
6579 : 391078 : && (DECL_LANG_SPECIFIC (decl)
6580 : 123 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6581 : : /* OK, specialization was already checked. */;
6582 : 391009 : else if (variable_template_p (field) && !this_tmpl)
6583 : : {
6584 : 3 : auto_diagnostic_group d;
6585 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6586 : : "non-member-template declaration of %qD", decl);
6587 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6588 : : "member template declaration here");
6589 : 3 : return error_mark_node;
6590 : 3 : }
6591 : : else
6592 : : {
6593 : 391006 : if (variable_template_p (field))
6594 : 51 : field = DECL_TEMPLATE_RESULT (field);
6595 : :
6596 : 391006 : if (DECL_CONTEXT (field) != context)
6597 : : {
6598 : 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6599 : 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6600 : : "to be defined as %<%T::%D%>",
6601 : 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6602 : 3 : context, DECL_NAME (decl));
6603 : 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6604 : : }
6605 : : /* Static data member are tricky; an in-class initialization
6606 : : still doesn't provide a definition, so the in-class
6607 : : declaration will have DECL_EXTERNAL set, but will have an
6608 : : initialization. Thus, duplicate_decls won't warn
6609 : : about this situation, and so we check here. */
6610 : 391006 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6611 : 9 : error ("duplicate initialization of %qD", decl);
6612 : 391006 : field = duplicate_decls (decl, field);
6613 : 391006 : if (field == error_mark_node)
6614 : : return error_mark_node;
6615 : 390950 : else if (field)
6616 : 390950 : decl = field;
6617 : : }
6618 : : }
6619 : : else
6620 : : {
6621 : 371382 : tree field = check_classfn (context, decl,
6622 : : this_tmpl
6623 : 13 : ? current_template_parms
6624 : : : NULL_TREE);
6625 : 371297 : if (field && field != error_mark_node
6626 : 742666 : && duplicate_decls (decl, field))
6627 : 371296 : decl = field;
6628 : : }
6629 : :
6630 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6631 : 762395 : DECL_IN_AGGR_P (decl) = 0;
6632 : : /* Do not mark DECL as an explicit specialization if it was not
6633 : : already marked as an instantiation; a declaration should
6634 : : never be marked as a specialization unless we know what
6635 : : template is being specialized. */
6636 : 762395 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6637 : : {
6638 : 369743 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6639 : 369743 : if (TREE_CODE (decl) == FUNCTION_DECL)
6640 : 348320 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6641 : 348320 : && DECL_DECLARED_INLINE_P (decl));
6642 : : else
6643 : 21423 : DECL_COMDAT (decl) = false;
6644 : :
6645 : : /* [temp.expl.spec] An explicit specialization of a static data
6646 : : member of a template is a definition if the declaration
6647 : : includes an initializer; otherwise, it is a declaration.
6648 : :
6649 : : We check for processing_specialization so this only applies
6650 : : to the new specialization syntax. */
6651 : 369743 : if (!initialized && processing_specialization)
6652 : 369544 : DECL_EXTERNAL (decl) = 1;
6653 : : }
6654 : :
6655 : 1132008 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6656 : : /* Aliases are definitions. */
6657 : 762452 : && !alias)
6658 : : {
6659 : 30 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6660 : 11 : permerror (declarator->id_loc,
6661 : : "declaration of %q#D outside of class is not definition",
6662 : : decl);
6663 : 19 : else if (flag_contract_strict_declarations)
6664 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6665 : : "declaration of %q#D outside of class is not definition",
6666 : : decl);
6667 : : }
6668 : : }
6669 : :
6670 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6671 : 115563155 : if (initialized == SD_DECOMPOSITION)
6672 : 174871 : fit_decomposition_lang_decl (decl, NULL_TREE);
6673 : :
6674 : 115563155 : was_public = TREE_PUBLIC (decl);
6675 : :
6676 : 192463075 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6677 : 115915927 : && current_function_decl)
6678 : : {
6679 : : /* A function-scope decl of some namespace-scope decl. */
6680 : 40353 : DECL_LOCAL_DECL_P (decl) = true;
6681 : 40353 : if (named_module_attach_p ())
6682 : 3 : error_at (declarator->id_loc,
6683 : : "block-scope extern declaration %q#D must not be"
6684 : : " attached to a named module", decl);
6685 : : }
6686 : :
6687 : : /* Enter this declaration into the symbol table. Don't push the plain
6688 : : VAR_DECL for a variable template. */
6689 : 115563155 : if (!template_parm_scope_p ()
6690 : 115563155 : || !VAR_P (decl))
6691 : 112676415 : decl = maybe_push_decl (decl);
6692 : :
6693 : 115563155 : if (processing_template_decl)
6694 : 71191574 : decl = push_template_decl (decl);
6695 : :
6696 : 115563155 : if (decl == error_mark_node)
6697 : : return error_mark_node;
6698 : :
6699 : 115562730 : if (VAR_P (decl)
6700 : 56851409 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6701 : 66089 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6702 : : /* But not templated variables. */
6703 : 115590160 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6704 : : {
6705 : : /* This is a const variable with implicit 'static'. Set
6706 : : DECL_THIS_STATIC so we can tell it from variables that are
6707 : : !TREE_PUBLIC because of the anonymous namespace. */
6708 : 27430 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6709 : 27430 : DECL_THIS_STATIC (decl) = 1;
6710 : : }
6711 : :
6712 : 63234038 : if (current_function_decl && VAR_P (decl)
6713 : 51435849 : && maybe_constexpr_fn (current_function_decl)
6714 : 121775293 : && cxx_dialect < cxx23)
6715 : : {
6716 : 2895581 : bool ok = false;
6717 : 2895581 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6718 : : {
6719 : 10 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6720 : 20 : error_at (DECL_SOURCE_LOCATION (decl),
6721 : : "%qD defined %<thread_local%> in %qs function only "
6722 : : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6723 : 10 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6724 : : ? "consteval" : "constexpr");
6725 : : }
6726 : 2895571 : else if (TREE_STATIC (decl))
6727 : : {
6728 : 52 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6729 : 76 : error_at (DECL_SOURCE_LOCATION (decl),
6730 : : "%qD defined %<static%> in %qs function only available "
6731 : : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6732 : 38 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6733 : : ? "consteval" : "constexpr");
6734 : : }
6735 : : else
6736 : : ok = true;
6737 : 48 : if (!ok)
6738 : 62 : cp_function_chain->invalid_constexpr = true;
6739 : : }
6740 : :
6741 : 115562730 : if (!processing_template_decl && VAR_P (decl))
6742 : 5560528 : start_decl_1 (decl, initialized);
6743 : :
6744 : 115562727 : return decl;
6745 : : }
6746 : :
6747 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6748 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6749 : : variable is initialized via an implicitly-called constructor.)
6750 : : This function must be called for ordinary variables (including, for
6751 : : example, implicit instantiations of templates), but must not be
6752 : : called for template declarations. */
6753 : :
6754 : : void
6755 : 5569399 : start_decl_1 (tree decl, bool initialized)
6756 : : {
6757 : 5569399 : gcc_checking_assert (!processing_template_decl);
6758 : :
6759 : 5569399 : if (error_operand_p (decl))
6760 : : return;
6761 : :
6762 : 5569384 : gcc_checking_assert (VAR_P (decl));
6763 : :
6764 : 5569384 : tree type = TREE_TYPE (decl);
6765 : 5569384 : bool complete_p = COMPLETE_TYPE_P (type);
6766 : 5569384 : bool aggregate_definition_p
6767 : 5569384 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6768 : :
6769 : : /* If an explicit initializer is present, or if this is a definition
6770 : : of an aggregate, then we need a complete type at this point.
6771 : : (Scalars are always complete types, so there is nothing to
6772 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6773 : : are issued below. */
6774 : 3493128 : if ((initialized || aggregate_definition_p)
6775 : 4535928 : && !complete_p
6776 : 4451315 : && COMPLETE_TYPE_P (complete_type (type)))
6777 : : {
6778 : 339327 : complete_p = true;
6779 : : /* We will not yet have set TREE_READONLY on DECL if the type
6780 : : was "const", but incomplete, before this point. But, now, we
6781 : : have a complete type, so we can try again. */
6782 : 339327 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6783 : : }
6784 : :
6785 : 5569381 : if (initialized)
6786 : : /* Is it valid for this decl to have an initializer at all? */
6787 : : {
6788 : : /* Don't allow initializations for incomplete types except for
6789 : : arrays which might be completed by the initialization. */
6790 : 3898592 : if (complete_p)
6791 : : ; /* A complete type is ok. */
6792 : 618628 : else if (type_uses_auto (type))
6793 : : ; /* An auto type is ok. */
6794 : 64814 : else if (TREE_CODE (type) != ARRAY_TYPE)
6795 : : {
6796 : 27 : auto_diagnostic_group d;
6797 : 27 : error ("variable %q#D has initializer but incomplete type", decl);
6798 : 48 : maybe_suggest_missing_header (input_location,
6799 : 27 : TYPE_IDENTIFIER (type),
6800 : 27 : CP_TYPE_CONTEXT (type));
6801 : 27 : type = TREE_TYPE (decl) = error_mark_node;
6802 : 27 : }
6803 : 64787 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6804 : : {
6805 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6806 : 0 : error ("elements of array %q#D have incomplete type", decl);
6807 : : /* else we already gave an error in start_decl. */
6808 : : }
6809 : : }
6810 : 1670789 : else if (aggregate_definition_p && !complete_p)
6811 : : {
6812 : 229 : if (type_uses_auto (type))
6813 : 67 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6814 : : else
6815 : : {
6816 : 162 : auto_diagnostic_group d;
6817 : 162 : error ("aggregate %q#D has incomplete type and cannot be defined",
6818 : : decl);
6819 : 279 : maybe_suggest_missing_header (input_location,
6820 : 162 : TYPE_IDENTIFIER (type),
6821 : 162 : CP_TYPE_CONTEXT (type));
6822 : : /* Change the type so that assemble_variable will give
6823 : : DECL an rtl we can live with: (mem (const_int 0)). */
6824 : 162 : type = TREE_TYPE (decl) = error_mark_node;
6825 : 162 : }
6826 : : }
6827 : :
6828 : : /* Create a new scope to hold this declaration if necessary.
6829 : : Whether or not a new scope is necessary cannot be determined
6830 : : until after the type has been completed; if the type is a
6831 : : specialization of a class template it is not until after
6832 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6833 : : will be set correctly. */
6834 : 5569381 : maybe_push_cleanup_level (type);
6835 : : }
6836 : :
6837 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6838 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6839 : :
6840 : : tree
6841 : 239 : do_aggregate_paren_init (tree init, tree type)
6842 : : {
6843 : 239 : tree val = TREE_VALUE (init);
6844 : :
6845 : 239 : if (TREE_CHAIN (init) == NULL_TREE)
6846 : : {
6847 : : /* If the list has a single element and it's a string literal,
6848 : : then it's the initializer for the array as a whole. */
6849 : 124 : if (TREE_CODE (type) == ARRAY_TYPE
6850 : 124 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6851 : 155 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6852 : : == STRING_CST)
6853 : : return val;
6854 : : /* Handle non-standard extensions like compound literals. This also
6855 : : prevents triggering aggregate parenthesized-initialization in
6856 : : compiler-generated code for =default. */
6857 : 97 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6858 : 97 : TREE_TYPE (val)))
6859 : : return val;
6860 : : }
6861 : :
6862 : 187 : init = build_constructor_from_list (init_list_type_node, init);
6863 : 187 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6864 : 187 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6865 : 187 : return init;
6866 : : }
6867 : :
6868 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6869 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6870 : : but will be set to a new CLEANUP_STMT if a temporary is created
6871 : : that must be destroyed subsequently.
6872 : :
6873 : : Returns an initializer expression to use to initialize DECL, or
6874 : : NULL if the initialization can be performed statically.
6875 : :
6876 : : Quotes on semantics can be found in ARM 8.4.3. */
6877 : :
6878 : : static tree
6879 : 322629 : grok_reference_init (tree decl, tree type, tree init, int flags)
6880 : : {
6881 : 322629 : if (init == NULL_TREE)
6882 : : {
6883 : 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6884 : 3 : || DECL_IN_AGGR_P (decl) == 0)
6885 : 18 : && ! DECL_THIS_EXTERN (decl))
6886 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
6887 : : "%qD declared as reference but not initialized", decl);
6888 : 15 : return NULL_TREE;
6889 : : }
6890 : :
6891 : 322614 : tree ttype = TREE_TYPE (type);
6892 : 322614 : if (TREE_CODE (init) == TREE_LIST)
6893 : : {
6894 : : /* This handles (C++20 only) code like
6895 : :
6896 : : const A& r(1, 2, 3);
6897 : :
6898 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6899 : 1301 : if (TREE_TYPE (init) == NULL_TREE
6900 : 1301 : && CP_AGGREGATE_TYPE_P (ttype)
6901 : 69 : && !DECL_DECOMPOSITION_P (decl)
6902 : 1367 : && (cxx_dialect >= cxx20))
6903 : : {
6904 : : /* We don't know yet if we should treat const A& r(1) as
6905 : : const A& r{1}. */
6906 : 40 : if (list_length (init) == 1)
6907 : : {
6908 : 19 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6909 : 19 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6910 : : tf_warning_or_error);
6911 : : }
6912 : : /* If the list had more than one element, the code is ill-formed
6913 : : pre-C++20, so we can build a constructor right away. */
6914 : : else
6915 : 21 : init = do_aggregate_paren_init (init, ttype);
6916 : : }
6917 : : else
6918 : 1261 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6919 : : tf_warning_or_error);
6920 : : }
6921 : :
6922 : 322614 : if (TREE_CODE (ttype) != ARRAY_TYPE
6923 : 322614 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6924 : : /* Note: default conversion is only called in very special cases. */
6925 : 12 : init = decay_conversion (init, tf_warning_or_error);
6926 : :
6927 : : /* check_initializer handles this for non-reference variables, but for
6928 : : references we need to do it here or the initializer will get the
6929 : : incomplete array type and confuse later calls to
6930 : : cp_complete_array_type. */
6931 : 322614 : if (TREE_CODE (ttype) == ARRAY_TYPE
6932 : 8678 : && TYPE_DOMAIN (ttype) == NULL_TREE
6933 : 322697 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6934 : 50 : || TREE_CODE (init) == STRING_CST))
6935 : : {
6936 : 33 : cp_complete_array_type (&ttype, init, false);
6937 : 33 : if (ttype != TREE_TYPE (type))
6938 : 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6939 : : }
6940 : :
6941 : : /* Convert INIT to the reference type TYPE. This may involve the
6942 : : creation of a temporary, whose lifetime must be the same as that
6943 : : of the reference. If so, a DECL_EXPR for the temporary will be
6944 : : added just after the DECL_EXPR for DECL. That's why we don't set
6945 : : DECL_INITIAL for local references (instead assigning to them
6946 : : explicitly); we need to allow the temporary to be initialized
6947 : : first. */
6948 : 322614 : return initialize_reference (type, init, flags,
6949 : 322614 : tf_warning_or_error);
6950 : : }
6951 : :
6952 : : /* Designated initializers in arrays are not supported in GNU C++.
6953 : : The parser cannot detect this error since it does not know whether
6954 : : a given brace-enclosed initializer is for a class type or for an
6955 : : array. This function checks that CE does not use a designated
6956 : : initializer. If it does, an error is issued. Returns true if CE
6957 : : is valid, i.e., does not have a designated initializer. */
6958 : :
6959 : : bool
6960 : 51632625 : check_array_designated_initializer (constructor_elt *ce,
6961 : : unsigned HOST_WIDE_INT index)
6962 : : {
6963 : : /* Designated initializers for array elements are not supported. */
6964 : 51632625 : if (ce->index)
6965 : : {
6966 : : /* The parser only allows identifiers as designated
6967 : : initializers. */
6968 : 33627375 : if (ce->index == error_mark_node)
6969 : : {
6970 : 0 : error ("name used in a GNU-style designated "
6971 : : "initializer for an array");
6972 : 0 : return false;
6973 : : }
6974 : 33627375 : else if (identifier_p (ce->index))
6975 : : {
6976 : 17 : error ("name %qD used in a GNU-style designated "
6977 : : "initializer for an array", ce->index);
6978 : 17 : return false;
6979 : : }
6980 : :
6981 : 33627358 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6982 : : ce->index, true);
6983 : 33627358 : if (ce_index
6984 : 33627355 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6985 : 67254710 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6986 : : == INTEGER_CST))
6987 : : {
6988 : : /* A C99 designator is OK if it matches the current index. */
6989 : 33627352 : if (wi::to_wide (ce_index) == index)
6990 : : {
6991 : 33627346 : ce->index = ce_index;
6992 : 33627346 : return true;
6993 : : }
6994 : : else
6995 : 6 : sorry ("non-trivial designated initializers not supported");
6996 : : }
6997 : : else
6998 : 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
6999 : : "C99 designator %qE is not an integral constant-expression",
7000 : : ce->index);
7001 : :
7002 : 12 : return false;
7003 : : }
7004 : :
7005 : : return true;
7006 : : }
7007 : :
7008 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
7009 : : array until we finish parsing the initializer. If that's the
7010 : : situation we're in, update DECL accordingly. */
7011 : :
7012 : : static void
7013 : 57765059 : maybe_deduce_size_from_array_init (tree decl, tree init)
7014 : : {
7015 : 57765059 : tree type = TREE_TYPE (decl);
7016 : :
7017 : 57765059 : if (TREE_CODE (type) == ARRAY_TYPE
7018 : 812060 : && TYPE_DOMAIN (type) == NULL_TREE
7019 : 58293675 : && TREE_CODE (decl) != TYPE_DECL)
7020 : : {
7021 : : /* do_default is really a C-ism to deal with tentative definitions.
7022 : : But let's leave it here to ease the eventual merge. */
7023 : 528616 : int do_default = !DECL_EXTERNAL (decl);
7024 : 528616 : tree initializer = init ? init : DECL_INITIAL (decl);
7025 : 528616 : int failure = 0;
7026 : :
7027 : : /* Check that there are no designated initializers in INIT, as
7028 : : those are not supported in GNU C++, and as the middle-end
7029 : : will crash if presented with a non-numeric designated
7030 : : initializer. */
7031 : 528616 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7032 : : {
7033 : 95041 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7034 : 95041 : constructor_elt *ce;
7035 : 95041 : HOST_WIDE_INT i, j = 0;
7036 : 16288275 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7037 : : {
7038 : 16194781 : if (instantiation_dependent_expression_p (ce->index))
7039 : 57765059 : return;
7040 : 16194775 : if (!check_array_designated_initializer (ce, j))
7041 : 2 : failure = 1;
7042 : : /* If an un-designated initializer is type-dependent, we can't
7043 : : check brace elision yet. */
7044 : 16194775 : if (ce->index == NULL_TREE
7045 : 16194775 : && type_dependent_expression_p (ce->value))
7046 : : return;
7047 : 16193234 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7048 : 196 : j += RAW_DATA_LENGTH (ce->value);
7049 : : else
7050 : 16193038 : ++j;
7051 : : }
7052 : : }
7053 : :
7054 : 93494 : if (failure)
7055 : 2 : TREE_TYPE (decl) = error_mark_node;
7056 : : else
7057 : : {
7058 : 527067 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7059 : : do_default);
7060 : 527067 : if (failure == 1)
7061 : : {
7062 : 33 : error_at (cp_expr_loc_or_loc (initializer,
7063 : 30 : DECL_SOURCE_LOCATION (decl)),
7064 : : "initializer fails to determine size of %qD", decl);
7065 : : }
7066 : 527037 : else if (failure == 2)
7067 : : {
7068 : 30 : if (do_default)
7069 : : {
7070 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7071 : : "array size missing in %qD", decl);
7072 : : }
7073 : : /* If a `static' var's size isn't known, make it extern as
7074 : : well as static, so it does not get allocated. If it's not
7075 : : `static', then don't mark it extern; finish_incomplete_decl
7076 : : will give it a default size and it will get allocated. */
7077 : 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7078 : 0 : DECL_EXTERNAL (decl) = 1;
7079 : : }
7080 : 527007 : else if (failure == 3)
7081 : : {
7082 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7083 : : "zero-size array %qD", decl);
7084 : : }
7085 : : }
7086 : :
7087 : 527069 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7088 : :
7089 : 527069 : relayout_decl (decl);
7090 : :
7091 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
7092 : 527069 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7093 : : {
7094 : 320 : tree tmpl = template_for_substitution (decl);
7095 : 320 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
7096 : 52 : TREE_TYPE (tmpl) = TREE_TYPE (decl);
7097 : : }
7098 : : }
7099 : : }
7100 : :
7101 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7102 : : any appropriate error messages regarding the layout. */
7103 : :
7104 : : static void
7105 : 49035006 : layout_var_decl (tree decl)
7106 : : {
7107 : 49035006 : tree type;
7108 : :
7109 : 49035006 : type = TREE_TYPE (decl);
7110 : 49035006 : if (type == error_mark_node)
7111 : : return;
7112 : :
7113 : : /* If we haven't already laid out this declaration, do so now.
7114 : : Note that we must not call complete type for an external object
7115 : : because it's type might involve templates that we are not
7116 : : supposed to instantiate yet. (And it's perfectly valid to say
7117 : : `extern X x' for some incomplete type `X'.) */
7118 : 49034958 : if (!DECL_EXTERNAL (decl))
7119 : 40348480 : complete_type (type);
7120 : 49034958 : if (!DECL_SIZE (decl)
7121 : 518826 : && TREE_TYPE (decl) != error_mark_node
7122 : 49553784 : && complete_or_array_type_p (type))
7123 : 474822 : layout_decl (decl, 0);
7124 : :
7125 : 49034958 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7126 : : {
7127 : : /* An automatic variable with an incomplete type: that is an error.
7128 : : Don't talk about array types here, since we took care of that
7129 : : message in grokdeclarator. */
7130 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
7131 : : "storage size of %qD isn%'t known", decl);
7132 : 17 : TREE_TYPE (decl) = error_mark_node;
7133 : : }
7134 : : #if 0
7135 : : /* Keep this code around in case we later want to control debug info
7136 : : based on whether a type is "used". (jason 1999-11-11) */
7137 : :
7138 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7139 : : /* Let debugger know it should output info for this type. */
7140 : : note_debug_info_needed (ttype);
7141 : :
7142 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7143 : : note_debug_info_needed (DECL_CONTEXT (decl));
7144 : : #endif
7145 : :
7146 : 89383438 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7147 : 25613658 : && DECL_SIZE (decl) != NULL_TREE
7148 : 74469530 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
7149 : : {
7150 : 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7151 : 6 : && !DECL_LOCAL_DECL_P (decl))
7152 : 0 : constant_expression_warning (DECL_SIZE (decl));
7153 : : else
7154 : : {
7155 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7156 : : "storage size of %qD isn%'t constant", decl);
7157 : 6 : TREE_TYPE (decl) = error_mark_node;
7158 : 6 : type = error_mark_node;
7159 : : }
7160 : : }
7161 : :
7162 : : /* If the final element initializes a flexible array field, adjust
7163 : : the size of the DECL with the initializer based on whether the
7164 : : DECL is a union or a structure. */
7165 : 49034958 : if (type != error_mark_node
7166 : 49034952 : && DECL_INITIAL (decl)
7167 : 25521307 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7168 : 3822856 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7169 : 3162985 : && DECL_SIZE (decl) != NULL_TREE
7170 : 3162985 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7171 : 3162985 : && COMPLETE_TYPE_P (type)
7172 : 3162985 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7173 : 52197943 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7174 : : {
7175 : 3162985 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7176 : 3162985 : if (elt.index)
7177 : : {
7178 : 1009467 : tree itype = TREE_TYPE (elt.index);
7179 : 1009467 : tree vtype = TREE_TYPE (elt.value);
7180 : 1009467 : if (TREE_CODE (itype) == ARRAY_TYPE
7181 : 53473 : && TYPE_DOMAIN (itype) == NULL
7182 : 272 : && TREE_CODE (vtype) == ARRAY_TYPE
7183 : 1009739 : && COMPLETE_TYPE_P (vtype))
7184 : : {
7185 : : /* For a structure, add the size of the initializer to the DECL's
7186 : : size. */
7187 : 272 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7188 : : {
7189 : 257 : DECL_SIZE (decl)
7190 : 257 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7191 : : TYPE_SIZE (vtype));
7192 : 257 : DECL_SIZE_UNIT (decl)
7193 : 514 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7194 : : TYPE_SIZE_UNIT (vtype));
7195 : : }
7196 : : /* For a union, the DECL's size is the maximum of the current size
7197 : : and the size of the initializer. */
7198 : : else
7199 : : {
7200 : 15 : DECL_SIZE (decl)
7201 : 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
7202 : : TYPE_SIZE (vtype));
7203 : 15 : DECL_SIZE_UNIT (decl)
7204 : 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7205 : : TYPE_SIZE_UNIT (vtype));
7206 : : }
7207 : : }
7208 : : }
7209 : : }
7210 : : }
7211 : :
7212 : : /* If a local static variable is declared in an inline function, or if
7213 : : we have a weak definition, we must endeavor to create only one
7214 : : instance of the variable at link-time. */
7215 : :
7216 : : void
7217 : 56001230 : maybe_commonize_var (tree decl)
7218 : : {
7219 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
7220 : : bindings. */
7221 : 56001230 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7222 : : return;
7223 : :
7224 : : /* Static data in a function with comdat linkage also has comdat
7225 : : linkage. */
7226 : 50977185 : if ((TREE_STATIC (decl)
7227 : 27207661 : && DECL_FUNCTION_SCOPE_P (decl)
7228 : 142014 : && vague_linkage_p (DECL_CONTEXT (decl)))
7229 : 105570979 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7230 : : {
7231 : 22620255 : if (flag_weak)
7232 : : {
7233 : : /* With weak symbols, we simply make the variable COMDAT;
7234 : : that will cause copies in multiple translations units to
7235 : : be merged. */
7236 : 22620228 : comdat_linkage (decl);
7237 : : }
7238 : : else
7239 : : {
7240 : 27 : if (DECL_INITIAL (decl) == NULL_TREE
7241 : 27 : || DECL_INITIAL (decl) == error_mark_node)
7242 : : {
7243 : : /* Without weak symbols, we can use COMMON to merge
7244 : : uninitialized variables. */
7245 : 24 : TREE_PUBLIC (decl) = 1;
7246 : 24 : DECL_COMMON (decl) = 1;
7247 : : }
7248 : : else
7249 : : {
7250 : : /* While for initialized variables, we must use internal
7251 : : linkage -- which means that multiple copies will not
7252 : : be merged. */
7253 : 3 : TREE_PUBLIC (decl) = 0;
7254 : 3 : DECL_COMMON (decl) = 0;
7255 : 3 : DECL_INTERFACE_KNOWN (decl) = 1;
7256 : 3 : const char *msg;
7257 : 3 : if (DECL_INLINE_VAR_P (decl))
7258 : : msg = G_("sorry: semantics of inline variable "
7259 : : "%q#D are wrong (you%'ll wind up with "
7260 : : "multiple copies)");
7261 : : else
7262 : : msg = G_("sorry: semantics of inline function "
7263 : : "static data %q#D are wrong (you%'ll wind "
7264 : : "up with multiple copies)");
7265 : 3 : auto_diagnostic_group d;
7266 : 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7267 : : msg, decl))
7268 : 3 : inform (DECL_SOURCE_LOCATION (decl),
7269 : : "you can work around this by removing the initializer");
7270 : 3 : }
7271 : : }
7272 : : }
7273 : : }
7274 : :
7275 : : /* Issue an error message if DECL is an uninitialized const variable.
7276 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7277 : : context from potential_constant_expression. Returns true if all is well,
7278 : : false otherwise. */
7279 : :
7280 : : bool
7281 : 6563611 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7282 : : tsubst_flags_t complain)
7283 : : {
7284 : 6563611 : tree type = strip_array_types (TREE_TYPE (decl));
7285 : :
7286 : : /* ``Unless explicitly declared extern, a const object does not have
7287 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
7288 : : 7.1.6 */
7289 : 6563611 : if (VAR_P (decl)
7290 : 6563611 : && !TYPE_REF_P (type)
7291 : 6546209 : && (CP_TYPE_CONST_P (type)
7292 : : /* C++20 permits trivial default initialization in constexpr
7293 : : context (P1331R2). */
7294 : 5491150 : || (cxx_dialect < cxx20
7295 : 2797193 : && (constexpr_context_p
7296 : 2497647 : || var_in_constexpr_fn (decl))))
7297 : 8030326 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7298 : : {
7299 : 658359 : tree field = default_init_uninitialized_part (type);
7300 : 658359 : if (!field)
7301 : : return true;
7302 : :
7303 : 113838 : auto_diagnostic_group d;
7304 : 113838 : bool show_notes = true;
7305 : :
7306 : 113838 : if (!constexpr_context_p || cxx_dialect >= cxx20)
7307 : : {
7308 : 110066 : if (CP_TYPE_CONST_P (type))
7309 : : {
7310 : 90 : if (complain & tf_error)
7311 : 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7312 : : "uninitialized %<const %D%>", decl);
7313 : : }
7314 : : else
7315 : : {
7316 : 109976 : if (!is_instantiation_of_constexpr (current_function_decl)
7317 : 109976 : && (complain & tf_error))
7318 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
7319 : : "uninitialized variable %qD in %<constexpr%> "
7320 : : "function", decl);
7321 : : else
7322 : : show_notes = false;
7323 : 109976 : cp_function_chain->invalid_constexpr = true;
7324 : : }
7325 : : }
7326 : 3772 : else if (complain & tf_error)
7327 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
7328 : : "uninitialized variable %qD in %<constexpr%> context",
7329 : : decl);
7330 : :
7331 : 113838 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7332 : : {
7333 : 62 : tree defaulted_ctor;
7334 : :
7335 : 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7336 : : "%q#T has no user-provided default constructor", type);
7337 : 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
7338 : 62 : if (defaulted_ctor)
7339 : 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7340 : : "constructor is not user-provided because it is "
7341 : : "explicitly defaulted in the class body");
7342 : 62 : inform (DECL_SOURCE_LOCATION (field),
7343 : : "and the implicitly-defined constructor does not "
7344 : : "initialize %q#D", field);
7345 : : }
7346 : :
7347 : 113838 : return false;
7348 : 113838 : }
7349 : :
7350 : : return true;
7351 : : }
7352 : :
7353 : : /* Structure holding the current initializer being processed by reshape_init.
7354 : : CUR is a pointer to the current element being processed, END is a pointer
7355 : : after the last element present in the initializer and RAW_IDX is index into
7356 : : RAW_DATA_CST if that is CUR elt. */
7357 : : struct reshape_iter
7358 : : {
7359 : : constructor_elt *cur;
7360 : : constructor_elt *end;
7361 : : unsigned raw_idx;
7362 : : };
7363 : :
7364 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7365 : :
7366 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7367 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
7368 : : initialized as if for an aggregate class. If there are no more such fields,
7369 : : the return value will be NULL. */
7370 : :
7371 : : tree
7372 : 14554390 : next_aggregate_field (tree field)
7373 : : {
7374 : 14554390 : while (field
7375 : 50936465 : && (TREE_CODE (field) != FIELD_DECL
7376 : 9339711 : || DECL_UNNAMED_BIT_FIELD (field)
7377 : 9339589 : || (DECL_ARTIFICIAL (field)
7378 : : /* In C++17, aggregates can have bases. */
7379 : 76440 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7380 : 36382075 : field = DECL_CHAIN (field);
7381 : :
7382 : 14554390 : return field;
7383 : : }
7384 : :
7385 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7386 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7387 : : to a subobject. If there are no more such fields, the return value will be
7388 : : NULL. */
7389 : :
7390 : : tree
7391 : 56019093 : next_subobject_field (tree field)
7392 : : {
7393 : 56019093 : while (field
7394 : 684712967 : && (TREE_CODE (field) != FIELD_DECL
7395 : 25679969 : || DECL_UNNAMED_BIT_FIELD (field)
7396 : 25679889 : || (DECL_ARTIFICIAL (field)
7397 : 7040588 : && !DECL_FIELD_IS_BASE (field)
7398 : 1741576 : && !DECL_VIRTUAL_P (field))))
7399 : 628693874 : field = DECL_CHAIN (field);
7400 : :
7401 : 56019093 : return field;
7402 : : }
7403 : :
7404 : : /* Return true for [dcl.init.list] direct-list-initialization from
7405 : : single element of enumeration with a fixed underlying type. */
7406 : :
7407 : : bool
7408 : 128382097 : is_direct_enum_init (tree type, tree init)
7409 : : {
7410 : 128382097 : if (cxx_dialect >= cxx17
7411 : 126367053 : && TREE_CODE (type) == ENUMERAL_TYPE
7412 : 6258140 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7413 : 3987296 : && TREE_CODE (init) == CONSTRUCTOR
7414 : 412351 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
7415 : 208976 : && CONSTRUCTOR_NELTS (init) == 1
7416 : 218 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7417 : : /* DR 2374: The single element needs to be implicitly
7418 : : convertible to the underlying type of the enum. */
7419 : 218 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7420 : 128382313 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7421 : 216 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7422 : 216 : CONSTRUCTOR_ELT (init, 0)->value,
7423 : : LOOKUP_IMPLICIT, tf_none))
7424 : : return true;
7425 : : return false;
7426 : : }
7427 : :
7428 : : /* Helper function for reshape_init*. Split first element of
7429 : : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7430 : : to true if the whole d->cur has been consumed. */
7431 : :
7432 : : static tree
7433 : 24323333 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7434 : : {
7435 : 24323333 : *inc_cur = true;
7436 : 24323333 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7437 : : return NULL_TREE;
7438 : 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7439 : 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7440 : 30588 : *inc_cur = false;
7441 : : else
7442 : 132 : d->raw_idx = 0;
7443 : : return ret;
7444 : : }
7445 : :
7446 : : /* Wrapper around that which for RAW_DATA_CST in INIT
7447 : : (as well as in D->cur->value) peels off the first element
7448 : : of the raw data and returns it, otherwise increments
7449 : : D->cur and returns INIT. */
7450 : :
7451 : : static tree
7452 : 24323317 : consume_init (tree init, reshape_iter *d)
7453 : : {
7454 : 24323317 : bool inc_cur;
7455 : 24323317 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7456 : 30720 : init = raw_init;
7457 : 24323317 : if (inc_cur)
7458 : 24292729 : d->cur++;
7459 : 24323317 : return init;
7460 : : }
7461 : :
7462 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
7463 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7464 : : INTEGER_CST representing the size of the array minus one (the maximum index),
7465 : : or NULL_TREE if the array was declared without specifying the size. D is
7466 : : the iterator within the constructor. */
7467 : :
7468 : : static tree
7469 : 369957 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7470 : : tree first_initializer_p, bool vector_p,
7471 : : tsubst_flags_t complain)
7472 : : {
7473 : 369957 : tree new_init;
7474 : 369957 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7475 : 369957 : unsigned HOST_WIDE_INT max_index_cst = 0;
7476 : 369957 : unsigned HOST_WIDE_INT index;
7477 : :
7478 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
7479 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7480 : : to build a new one. But don't reuse if not complaining; if this is
7481 : : tentative, we might also reshape to another type (95319). */
7482 : 369957 : bool reuse = (first_initializer_p
7483 : 316962 : && (complain & tf_error)
7484 : 315709 : && !CP_AGGREGATE_TYPE_P (elt_type)
7485 : 634814 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7486 : 112918 : if (reuse)
7487 : : new_init = first_initializer_p;
7488 : : else
7489 : 112918 : new_init = build_constructor (init_list_type_node, NULL);
7490 : :
7491 : 369957 : if (sized_array_p)
7492 : : {
7493 : 258192 : poly_uint64 midx;
7494 : : /* Minus 1 is used for zero sized arrays. */
7495 : 258192 : if (integer_all_onesp (max_index))
7496 : 24 : return new_init;
7497 : :
7498 : 258168 : if (tree_fits_poly_uint64_p (max_index))
7499 : 258168 : midx = tree_to_poly_uint64 (max_index);
7500 : : /* sizetype is sign extended, not zero extended. */
7501 : : else
7502 : 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7503 : :
7504 : : /* For VLA vectors, we restict the number of elements in the constructor
7505 : : to lower bound of the VLA elements. */
7506 : 258168 : max_index_cst = constant_lower_bound (midx);
7507 : : }
7508 : :
7509 : 369933 : constructor_elt *first_cur = d->cur;
7510 : : /* Loop until there are no more initializers. */
7511 : 19094283 : for (index = 0;
7512 : 19094283 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7513 : : ++index)
7514 : : {
7515 : 18724532 : tree elt_init;
7516 : 18724532 : constructor_elt *old_cur = d->cur;
7517 : 18724532 : unsigned int old_raw_idx = d->raw_idx;
7518 : 18724532 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7519 : :
7520 : 18724532 : if (d->cur->index)
7521 : 1971489 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7522 : 18724532 : check_array_designated_initializer (d->cur, index);
7523 : 18724532 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7524 : 32707 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7525 : 10233 : || is_byte_access_type (elt_type))
7526 : 22474 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7527 : 1813 : && (!sized_array_p || index < max_index_cst)
7528 : 18726333 : && !vector_p)
7529 : : {
7530 : 481 : elt_init = d->cur->value;
7531 : 481 : unsigned int off = d->raw_idx;
7532 : 481 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7533 : 481 : if (!sized_array_p || len <= max_index_cst - index + 1)
7534 : : {
7535 : 472 : d->cur++;
7536 : 472 : d->raw_idx = 0;
7537 : : }
7538 : : else
7539 : : {
7540 : 9 : len = max_index_cst - index + 1;
7541 : 9 : d->raw_idx += len;
7542 : : }
7543 : 481 : if (!reuse || off || d->cur == old_cur)
7544 : : {
7545 : 27 : elt_init = copy_node (elt_init);
7546 : 27 : RAW_DATA_LENGTH (elt_init) = len;
7547 : 27 : RAW_DATA_POINTER (elt_init) += off;
7548 : : }
7549 : 481 : TREE_TYPE (elt_init) = elt_type;
7550 : : }
7551 : : else
7552 : 18724051 : elt_init = reshape_init_r (elt_type, d,
7553 : : /*first_initializer_p=*/NULL_TREE,
7554 : : complain);
7555 : 18724532 : if (elt_init == error_mark_node)
7556 : : return error_mark_node;
7557 : 18724353 : tree idx = size_int (index);
7558 : 18724353 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7559 : : {
7560 : : /* We need to stop reusing as some RAW_DATA_CST in the original
7561 : : ctor had to be split. */
7562 : 72 : new_init = build_constructor (init_list_type_node, NULL);
7563 : 72 : if (index)
7564 : : {
7565 : 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7566 : 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7567 : 72 : (d->cur - first_cur)
7568 : : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7569 : 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7570 : : {
7571 : : unsigned int j;
7572 : : tree nidx, nval;
7573 : 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7574 : : j, nidx, nval)
7575 : 4 : if (nidx)
7576 : : {
7577 : 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7578 : 4 : (void) nval;
7579 : 4 : break;
7580 : : }
7581 : : }
7582 : : }
7583 : : reuse = false;
7584 : : }
7585 : 18724281 : if (reuse)
7586 : : {
7587 : 16863420 : old_cur->index = idx;
7588 : 16863420 : old_cur->value = elt_init;
7589 : : }
7590 : : else
7591 : 1860933 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7592 : : idx, elt_init);
7593 : 18724353 : if (!TREE_CONSTANT (elt_init))
7594 : 188790 : TREE_CONSTANT (new_init) = false;
7595 : :
7596 : : /* This can happen with an invalid initializer (c++/54501). */
7597 : 18724353 : if (d->cur == old_cur
7598 : 32082 : && !sized_array_p
7599 : 14085 : && d->raw_idx == old_raw_idx)
7600 : : break;
7601 : :
7602 : 18724350 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7603 : 481 : index += RAW_DATA_LENGTH (elt_init) - 1;
7604 : : }
7605 : :
7606 : : return new_init;
7607 : : }
7608 : :
7609 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7610 : : Parameters are the same of reshape_init_r. */
7611 : :
7612 : : static tree
7613 : 323815 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7614 : : tsubst_flags_t complain)
7615 : : {
7616 : 323815 : tree max_index = NULL_TREE;
7617 : :
7618 : 323815 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7619 : :
7620 : 323815 : if (TYPE_DOMAIN (type))
7621 : 212095 : max_index = array_type_nelts_minus_one (type);
7622 : :
7623 : 323815 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7624 : 323815 : first_initializer_p, false, complain);
7625 : : }
7626 : :
7627 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7628 : : Parameters are the same of reshape_init_r. */
7629 : :
7630 : : static tree
7631 : 46142 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7632 : : {
7633 : 46142 : tree max_index = NULL_TREE;
7634 : :
7635 : 46142 : gcc_assert (VECTOR_TYPE_P (type));
7636 : :
7637 : 46142 : if (COMPOUND_LITERAL_P (d->cur->value))
7638 : : {
7639 : 0 : tree value = d->cur->value;
7640 : 0 : if (!same_type_p (TREE_TYPE (value), type))
7641 : : {
7642 : 0 : if (complain & tf_error)
7643 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7644 : 0 : TREE_TYPE (d->cur->value), type);
7645 : 0 : value = error_mark_node;
7646 : : }
7647 : 0 : ++d->cur;
7648 : 0 : return value;
7649 : : }
7650 : :
7651 : : /* For a vector, we initialize it as an array of the appropriate size. */
7652 : 46142 : if (VECTOR_TYPE_P (type))
7653 : 46142 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7654 : :
7655 : 46142 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7656 : 46142 : NULL_TREE, true, complain);
7657 : : }
7658 : :
7659 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7660 : : INIT, in isolation from any designator or other initializers. */
7661 : :
7662 : : static tree
7663 : 587567 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7664 : : {
7665 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7666 : : calling reshape_init, but this way can just live on the stack. */
7667 : 587567 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7668 : 587567 : reshape_iter iter = { &elt, &elt + 1, 0 };
7669 : 587567 : return reshape_init_r (type, &iter,
7670 : : /*first_initializer_p=*/NULL_TREE,
7671 : 587567 : complain);
7672 : : }
7673 : :
7674 : : /* Subroutine of reshape_init_r, processes the initializers for classes
7675 : : or union. Parameters are the same of reshape_init_r. */
7676 : :
7677 : : static tree
7678 : 3678678 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7679 : : tsubst_flags_t complain)
7680 : : {
7681 : 3678678 : tree field;
7682 : 3678678 : tree new_init;
7683 : :
7684 : 3678678 : gcc_assert (CLASS_TYPE_P (type));
7685 : :
7686 : : /* The initializer for a class is always a CONSTRUCTOR. */
7687 : 3678678 : new_init = build_constructor (init_list_type_node, NULL);
7688 : :
7689 : 3678678 : int binfo_idx = -1;
7690 : 3678678 : tree binfo = TYPE_BINFO (type);
7691 : 3678678 : tree base_binfo = NULL_TREE;
7692 : 3678678 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7693 : : {
7694 : : /* We get here from maybe_aggr_guide for C++20 class template argument
7695 : : deduction. In this case we need to look through the binfo because a
7696 : : template doesn't have base fields. */
7697 : 342 : binfo_idx = 0;
7698 : 342 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7699 : : }
7700 : 67 : if (base_binfo)
7701 : : field = base_binfo;
7702 : : else
7703 : 3678611 : field = next_aggregate_field (TYPE_FIELDS (type));
7704 : :
7705 : 3678678 : if (!field)
7706 : : {
7707 : : /* [dcl.init.aggr]
7708 : :
7709 : : An initializer for an aggregate member that is an
7710 : : empty class shall have the form of an empty
7711 : : initializer-list {}. */
7712 : 324 : if (!first_initializer_p)
7713 : : {
7714 : 270 : if (complain & tf_error)
7715 : 13 : error ("initializer for %qT must be brace-enclosed", type);
7716 : 270 : return error_mark_node;
7717 : : }
7718 : : return new_init;
7719 : : }
7720 : :
7721 : : /* For C++20 CTAD, handle pack expansions in the base list. */
7722 : : tree last_was_pack_expansion = NULL_TREE;
7723 : :
7724 : : /* Loop through the initializable fields, gathering initializers. */
7725 : 11232677 : while (d->cur != d->end)
7726 : : {
7727 : 7666385 : tree field_init;
7728 : 7666385 : constructor_elt *old_cur = d->cur;
7729 : 7666385 : unsigned old_raw_idx = d->raw_idx;
7730 : 7666385 : bool direct_desig = false;
7731 : :
7732 : : /* Handle C++20 designated initializers. */
7733 : 7666385 : if (d->cur->index)
7734 : : {
7735 : 587727 : if (d->cur->index == error_mark_node)
7736 : : return error_mark_node;
7737 : :
7738 : 587721 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7739 : : {
7740 : : /* We already reshaped this; we should have returned early from
7741 : : reshape_init. */
7742 : 0 : gcc_checking_assert (false);
7743 : : if (field != d->cur->index)
7744 : : {
7745 : : if (tree id = DECL_NAME (d->cur->index))
7746 : : gcc_checking_assert (d->cur->index
7747 : : == get_class_binding (type, id));
7748 : : field = d->cur->index;
7749 : : }
7750 : : }
7751 : 587721 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7752 : : {
7753 : 587715 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7754 : 587715 : field = get_class_binding (type, d->cur->index);
7755 : 587715 : direct_desig = true;
7756 : : }
7757 : : else
7758 : : {
7759 : 6 : if (complain & tf_error)
7760 : 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7761 : : " for class %qT", d->cur->index, type);
7762 : 6 : return error_mark_node;
7763 : : }
7764 : :
7765 : 587715 : if (!field && ANON_AGGR_TYPE_P (type))
7766 : : /* Apparently the designator isn't for a member of this anonymous
7767 : : struct, so head back to the enclosing class. */
7768 : : break;
7769 : :
7770 : 587712 : if (!field || TREE_CODE (field) != FIELD_DECL)
7771 : : {
7772 : 18 : if (complain & tf_error)
7773 : : {
7774 : 6 : if (field && TREE_CODE (field) == TREE_LIST)
7775 : : {
7776 : 3 : auto_diagnostic_group g;
7777 : 3 : error ("request for member %qD is ambiguous",
7778 : 3 : d->cur->index);
7779 : 3 : print_candidates (field);
7780 : 3 : }
7781 : : else
7782 : 3 : error ("%qT has no non-static data member named %qD", type,
7783 : 3 : d->cur->index);
7784 : : }
7785 : 18 : return error_mark_node;
7786 : : }
7787 : :
7788 : : /* If the element is an anonymous union object and the initializer
7789 : : list is a designated-initializer-list, the anonymous union object
7790 : : is initialized by the designated-initializer-list { D }, where D
7791 : : is the designated-initializer-clause naming a member of the
7792 : : anonymous union object. */
7793 : 587694 : tree ictx = DECL_CONTEXT (field);
7794 : 587694 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7795 : : {
7796 : : /* Find the anon aggr that is a direct member of TYPE. */
7797 : 139 : while (ANON_AGGR_TYPE_P (ictx))
7798 : : {
7799 : 136 : tree cctx = TYPE_CONTEXT (ictx);
7800 : 136 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7801 : 124 : goto found;
7802 : : ictx = cctx;
7803 : : }
7804 : :
7805 : : /* Not found, e.g. FIELD is a member of a base class. */
7806 : 3 : if (complain & tf_error)
7807 : 3 : error ("%qD is not a direct member of %qT", field, type);
7808 : 3 : return error_mark_node;
7809 : :
7810 : 124 : found:
7811 : : /* Now find the TYPE member with that anon aggr type. */
7812 : 124 : tree aafield = TYPE_FIELDS (type);
7813 : 1067 : for (; aafield; aafield = TREE_CHAIN (aafield))
7814 : 1067 : if (TREE_TYPE (aafield) == ictx)
7815 : : break;
7816 : 0 : gcc_assert (aafield);
7817 : : field = aafield;
7818 : : direct_desig = false;
7819 : : }
7820 : : }
7821 : :
7822 : : /* If we processed all the member of the class, we are done. */
7823 : 7666225 : if (!field)
7824 : : break;
7825 : :
7826 : 15331849 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7827 : 7665934 : ? field : NULL_TREE);
7828 : 7665934 : if (last_was_pack_expansion)
7829 : : /* Each non-trailing aggregate element that is a pack expansion is
7830 : : assumed to correspond to no elements of the initializer list. */
7831 : 19 : goto continue_;
7832 : :
7833 : 7665915 : if (direct_desig)
7834 : : {
7835 : : /* The designated field F is initialized from this one element.
7836 : :
7837 : : Note that we don't want to do this if we found the designator
7838 : : inside an anon aggr above; we use the normal code to implement:
7839 : :
7840 : : "If the element is an anonymous union member and the initializer
7841 : : list is a brace-enclosed designated- initializer-list, the element
7842 : : is initialized by the designated-initializer-list { D }, where D
7843 : : is the designated- initializer-clause naming a member of the
7844 : : anonymous union member." */
7845 : 587567 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7846 : 587567 : field_init = reshape_single_init (TREE_TYPE (field),
7847 : : d->cur->value, complain);
7848 : 587567 : d->cur++;
7849 : : }
7850 : : else
7851 : 7078348 : field_init = reshape_init_r (TREE_TYPE (field), d,
7852 : : /*first_initializer_p=*/NULL_TREE,
7853 : : complain);
7854 : :
7855 : 7665915 : if (field_init == error_mark_node)
7856 : : return error_mark_node;
7857 : :
7858 : 7665626 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7859 : : {
7860 : : /* This can happen with an invalid initializer for a flexible
7861 : : array member (c++/54441). */
7862 : 0 : if (complain & tf_error)
7863 : 0 : error ("invalid initializer for %q#D", field);
7864 : 0 : return error_mark_node;
7865 : : }
7866 : :
7867 : 7665626 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7868 : :
7869 : : /* [dcl.init.aggr]
7870 : :
7871 : : When a union is initialized with a brace-enclosed
7872 : : initializer, the braces shall only contain an
7873 : : initializer for the first member of the union. */
7874 : 7665626 : if (TREE_CODE (type) == UNION_TYPE)
7875 : : break;
7876 : :
7877 : 7554304 : continue_:
7878 : 7554323 : if (base_binfo)
7879 : : {
7880 : 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7881 : : field = base_binfo;
7882 : : else
7883 : 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7884 : : }
7885 : : else
7886 : 7554241 : field = next_aggregate_field (DECL_CHAIN (field));
7887 : : }
7888 : :
7889 : : /* A trailing aggregate element that is a pack expansion is assumed to
7890 : : correspond to all remaining elements of the initializer list (if any). */
7891 : 3678032 : if (last_was_pack_expansion)
7892 : : {
7893 : 16 : tree init = d->cur->value;
7894 : 16 : bool inc_cur;
7895 : 16 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7896 : 0 : init = raw_init;
7897 : 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7898 : : last_was_pack_expansion, init);
7899 : 46 : while (d->cur != d->end)
7900 : 30 : d->cur++;
7901 : : }
7902 : :
7903 : : return new_init;
7904 : : }
7905 : :
7906 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7907 : : designators are not valid; either complain or return true to indicate
7908 : : that reshape_init_r should return error_mark_node. */
7909 : :
7910 : : static bool
7911 : 65301 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7912 : : {
7913 : 65301 : if (d->cur->index)
7914 : : {
7915 : 30 : if (complain & tf_error)
7916 : 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7917 : : "C99 designator %qE outside aggregate initializer",
7918 : : d->cur->index);
7919 : : else
7920 : : return true;
7921 : : }
7922 : : return false;
7923 : : }
7924 : :
7925 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7926 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7927 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7928 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7929 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7930 : :
7931 : : static tree
7932 : 30496112 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7933 : : tsubst_flags_t complain)
7934 : : {
7935 : 30496112 : tree init = d->cur->value;
7936 : :
7937 : 30496112 : if (error_operand_p (init))
7938 : 123 : return error_mark_node;
7939 : :
7940 : 4106026 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7941 : 30561124 : && has_designator_problem (d, complain))
7942 : 0 : return error_mark_node;
7943 : :
7944 : 30495989 : tree stripped_init = tree_strip_any_location_wrapper (init);
7945 : :
7946 : 30495989 : if (TREE_CODE (type) == COMPLEX_TYPE)
7947 : : {
7948 : : /* A complex type can be initialized from one or two initializers,
7949 : : but braces are not elided. */
7950 : 678 : init = consume_init (init, d);
7951 : 678 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7952 : : {
7953 : 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7954 : : {
7955 : 0 : if (complain & tf_error)
7956 : 0 : error ("too many initializers for %qT", type);
7957 : : else
7958 : 0 : return error_mark_node;
7959 : : }
7960 : : }
7961 : 669 : else if (first_initializer_p && d->cur != d->end)
7962 : : {
7963 : 36 : if (error_operand_p (d->cur->value)
7964 : 36 : || has_designator_problem (d, complain))
7965 : 0 : return error_mark_node;
7966 : 36 : vec<constructor_elt, va_gc> *v = 0;
7967 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7968 : 36 : init = consume_init (d->cur->value, d);
7969 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7970 : 36 : init = build_constructor (init_list_type_node, v);
7971 : : }
7972 : 678 : return init;
7973 : : }
7974 : :
7975 : : /* A non-aggregate type is always initialized with a single
7976 : : initializer. */
7977 : 60935682 : if (!CP_AGGREGATE_TYPE_P (type)
7978 : : /* As is an array with dependent bound, which we can see
7979 : : during C++20 aggregate CTAD. */
7980 : 36616688 : || (cxx_dialect >= cxx20
7981 : 3983514 : && TREE_CODE (type) == ARRAY_TYPE
7982 : 161953 : && uses_template_parms (TYPE_DOMAIN (type))))
7983 : : {
7984 : : /* It is invalid to initialize a non-aggregate type with a
7985 : : brace-enclosed initializer before C++0x.
7986 : : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7987 : : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7988 : : a CONSTRUCTOR (with a record type). */
7989 : 24319005 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
7990 : : /* Don't complain about a capture-init. */
7991 : 308447 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7992 : 24617363 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7993 : : {
7994 : 298319 : if (SCALAR_TYPE_P (type))
7995 : : {
7996 : 726 : if (cxx_dialect < cxx11)
7997 : : {
7998 : 16 : if (complain & tf_error)
7999 : 16 : error ("braces around scalar initializer for type %qT",
8000 : : type);
8001 : 16 : init = error_mark_node;
8002 : : }
8003 : 710 : else if (first_initializer_p
8004 : 710 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
8005 : 546 : && (BRACE_ENCLOSED_INITIALIZER_P
8006 : : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8007 : : {
8008 : 13 : if (complain & tf_error)
8009 : 13 : error ("too many braces around scalar initializer "
8010 : : "for type %qT", type);
8011 : 13 : init = error_mark_node;
8012 : : }
8013 : : }
8014 : : else
8015 : 297593 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8016 : : }
8017 : 24319005 : return consume_init (init, d);
8018 : : }
8019 : :
8020 : : /* "If T is a class type and the initializer list has a single element of
8021 : : type cv U, where U is T or a class derived from T, the object is
8022 : : initialized from that element." Even if T is an aggregate. */
8023 : 6152471 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8024 : 5719920 : && first_initializer_p
8025 : : /* But not if it's a designated init. */
8026 : 3709432 : && !d->cur->index
8027 : 3460690 : && d->end - d->cur == 1
8028 : 435396 : && TREE_CODE (init) != RAW_DATA_CST
8029 : 6611702 : && reference_related_p (type, TREE_TYPE (init)))
8030 : : {
8031 : 416 : d->cur++;
8032 : 416 : return init;
8033 : : }
8034 : :
8035 : : /* [dcl.init.aggr]
8036 : :
8037 : : All implicit type conversions (clause _conv_) are considered when
8038 : : initializing the aggregate member with an initializer from an
8039 : : initializer-list. If the initializer can initialize a member,
8040 : : the member is initialized. Otherwise, if the member is itself a
8041 : : non-empty subaggregate, brace elision is assumed and the
8042 : : initializer is considered for the initialization of the first
8043 : : member of the subaggregate. */
8044 : 2702486 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8045 : : /* But don't try this for the first initializer, since that would be
8046 : : looking through the outermost braces; A a2 = { a1 }; is not a
8047 : : valid aggregate initialization. */
8048 : 3473464 : && !first_initializer_p
8049 : 6188519 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8050 : 9069 : || can_convert_arg (type, TREE_TYPE (init),
8051 : 9069 : TREE_CODE (init) == RAW_DATA_CST
8052 : 6177496 : ? build_int_cst (integer_type_node,
8053 : 1606 : *(const unsigned char *)
8054 : 1606 : RAW_DATA_POINTER (init))
8055 : : : init,
8056 : : LOOKUP_NORMAL, complain)))
8057 : 3598 : return consume_init (init, d);
8058 : :
8059 : : /* [dcl.init.string]
8060 : :
8061 : : A char array (whether plain char, signed char, or unsigned char)
8062 : : can be initialized by a string-literal (optionally enclosed in
8063 : : braces); a wchar_t array can be initialized by a wide
8064 : : string-literal (optionally enclosed in braces). */
8065 : 6172292 : if (TREE_CODE (type) == ARRAY_TYPE
8066 : 6172292 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8067 : : {
8068 : 89405 : tree str_init = init;
8069 : 89405 : tree stripped_str_init = stripped_init;
8070 : 89405 : reshape_iter stripd = {};
8071 : :
8072 : : /* Strip one level of braces if and only if they enclose a single
8073 : : element (as allowed by [dcl.init.string]). */
8074 : 89405 : if (!first_initializer_p
8075 : 12444 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8076 : 100740 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8077 : : {
8078 : 375 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8079 : 375 : str_init = stripd.cur->value;
8080 : 375 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
8081 : : }
8082 : :
8083 : : /* If it's a string literal, then it's the initializer for the array
8084 : : as a whole. Otherwise, continue with normal initialization for
8085 : : array types (one value per array element). */
8086 : 89405 : if (TREE_CODE (stripped_str_init) == STRING_CST)
8087 : : {
8088 : 72 : if ((first_initializer_p && has_designator_problem (d, complain))
8089 : 1025 : || (stripd.cur && has_designator_problem (&stripd, complain)))
8090 : 953 : return error_mark_node;
8091 : 953 : d->cur++;
8092 : 953 : return str_init;
8093 : : }
8094 : : }
8095 : :
8096 : : /* The following cases are about aggregates. If we are not within a full
8097 : : initializer already, and there is not a CONSTRUCTOR, it means that there
8098 : : is a missing set of braces (that is, we are processing the case for
8099 : : which reshape_init exists). */
8100 : 6171339 : bool braces_elided_p = false;
8101 : 6171339 : if (!first_initializer_p)
8102 : : {
8103 : 2130936 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
8104 : : {
8105 : 2122734 : tree init_type = TREE_TYPE (init);
8106 : 2122734 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8107 : : /* There is no need to call reshape_init for pointer-to-member
8108 : : function initializers, as they are always constructed correctly
8109 : : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8110 : : which is missing outermost braces. We should warn below, and
8111 : : one of the routines below will wrap it in additional { }. */;
8112 : : /* For a nested compound literal, proceed to specialized routines,
8113 : : to handle initialization of arrays and similar. */
8114 : 2122725 : else if (COMPOUND_LITERAL_P (stripped_init))
8115 : 6 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8116 : : /* If we have an unresolved designator, we need to find the member it
8117 : : designates within TYPE, so proceed to the routines below. For
8118 : : FIELD_DECL or INTEGER_CST designators, we're already initializing
8119 : : the designated element. */
8120 : 2122719 : else if (d->cur->index
8121 : 27 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
8122 : : /* Brace elision with designators is only permitted for anonymous
8123 : : aggregates. */
8124 : 15 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
8125 : : /* A CONSTRUCTOR of the target's type is a previously
8126 : : digested initializer. */
8127 : 2122704 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8128 : : {
8129 : 0 : ++d->cur;
8130 : 0 : return init;
8131 : : }
8132 : : else
8133 : : {
8134 : : /* Something that hasn't been reshaped yet. */
8135 : 2122704 : ++d->cur;
8136 : 2122704 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8137 : 2122704 : return reshape_init (type, init, complain);
8138 : : }
8139 : : }
8140 : :
8141 : 8232 : if (complain & tf_warning)
8142 : 7083 : warning (OPT_Wmissing_braces,
8143 : : "missing braces around initializer for %qT",
8144 : : type);
8145 : : braces_elided_p = true;
8146 : : }
8147 : :
8148 : : /* Dispatch to specialized routines. */
8149 : 4048635 : tree new_init;
8150 : 4048635 : if (CLASS_TYPE_P (type))
8151 : 3678678 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
8152 : 369957 : else if (TREE_CODE (type) == ARRAY_TYPE)
8153 : 323815 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
8154 : 46142 : else if (VECTOR_TYPE_P (type))
8155 : 46142 : new_init = reshape_init_vector (type, d, complain);
8156 : : else
8157 : 0 : gcc_unreachable ();
8158 : :
8159 : 4048635 : if (braces_elided_p
8160 : 8232 : && TREE_CODE (new_init) == CONSTRUCTOR)
8161 : 7956 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8162 : :
8163 : : return new_init;
8164 : : }
8165 : :
8166 : : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
8167 : : brace-enclosed aggregate initializer.
8168 : :
8169 : : INIT is the CONSTRUCTOR containing the list of initializers describing
8170 : : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8171 : : It may not presently match the shape of the TYPE; for example:
8172 : :
8173 : : struct S { int a; int b; };
8174 : : struct S a[] = { 1, 2, 3, 4 };
8175 : :
8176 : : Here INIT will hold a vector of four elements, rather than a
8177 : : vector of two elements, each itself a vector of two elements. This
8178 : : routine transforms INIT from the former form into the latter. The
8179 : : revised CONSTRUCTOR node is returned. */
8180 : :
8181 : : tree
8182 : 9784371 : reshape_init (tree type, tree init, tsubst_flags_t complain)
8183 : : {
8184 : 9784371 : vec<constructor_elt, va_gc> *v;
8185 : 9784371 : reshape_iter d;
8186 : 9784371 : tree new_init;
8187 : :
8188 : 9784371 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8189 : :
8190 : 9784371 : v = CONSTRUCTOR_ELTS (init);
8191 : :
8192 : : /* An empty constructor does not need reshaping, and it is always a valid
8193 : : initializer. */
8194 : 9784975 : if (vec_safe_is_empty (v))
8195 : : return init;
8196 : :
8197 : 4106955 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8198 : : /* Already reshaped. */
8199 : : return init;
8200 : :
8201 : : /* Brace elision is not performed for a CONSTRUCTOR representing
8202 : : parenthesized aggregate initialization. */
8203 : 4106818 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
8204 : : {
8205 : 496 : tree elt = (*v)[0].value;
8206 : : /* If we're initializing a char array from a string-literal that is
8207 : : enclosed in braces, unwrap it here. */
8208 : 496 : if (TREE_CODE (type) == ARRAY_TYPE
8209 : 221 : && vec_safe_length (v) == 1
8210 : 121 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8211 : 529 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8212 : : return elt;
8213 : 467 : return init;
8214 : : }
8215 : :
8216 : : /* Handle [dcl.init.list] direct-list-initialization from
8217 : : single element of enumeration with a fixed underlying type. */
8218 : 4106322 : if (is_direct_enum_init (type, init))
8219 : : {
8220 : 176 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8221 : 176 : type = cv_unqualified (type);
8222 : 176 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8223 : : {
8224 : 161 : warning_sentinel w (warn_useless_cast);
8225 : 161 : warning_sentinel w2 (warn_ignored_qualifiers);
8226 : 161 : return cp_build_c_cast (input_location, type, elt,
8227 : : tf_warning_or_error);
8228 : 161 : }
8229 : : else
8230 : 15 : return error_mark_node;
8231 : : }
8232 : :
8233 : : /* Recurse on this CONSTRUCTOR. */
8234 : 4106146 : d.cur = &(*v)[0];
8235 : 4106146 : d.end = d.cur + v->length ();
8236 : 4106146 : d.raw_idx = 0;
8237 : :
8238 : 4106146 : new_init = reshape_init_r (type, &d, init, complain);
8239 : 4106146 : if (new_init == error_mark_node)
8240 : : return error_mark_node;
8241 : :
8242 : : /* Make sure all the element of the constructor were used. Otherwise,
8243 : : issue an error about exceeding initializers. */
8244 : 4105525 : if (d.cur != d.end)
8245 : : {
8246 : 159 : if (complain & tf_error)
8247 : 63 : error ("too many initializers for %qT", type);
8248 : 159 : return error_mark_node;
8249 : : }
8250 : :
8251 : 4105366 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8252 : 4105366 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8253 : 467763 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8254 : 4105366 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8255 : 4105366 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8256 : 31126 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8257 : : || seen_error ());
8258 : :
8259 : : return new_init;
8260 : : }
8261 : :
8262 : : /* Verify array initializer. Returns true if errors have been reported. */
8263 : :
8264 : : bool
8265 : 883106 : check_array_initializer (tree decl, tree type, tree init)
8266 : : {
8267 : 883106 : tree element_type = TREE_TYPE (type);
8268 : :
8269 : : /* Structured binding when initialized with an array type needs
8270 : : to have complete type. */
8271 : 883106 : if (decl
8272 : 877649 : && DECL_DECOMPOSITION_P (decl)
8273 : 366 : && DECL_DECOMP_IS_BASE (decl)
8274 : 883472 : && !COMPLETE_TYPE_P (type))
8275 : : {
8276 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
8277 : : "structured binding has incomplete type %qT", type);
8278 : 0 : TREE_TYPE (decl) = error_mark_node;
8279 : 0 : return true;
8280 : : }
8281 : :
8282 : : /* The array type itself need not be complete, because the
8283 : : initializer may tell us how many elements are in the array.
8284 : : But, the elements of the array must be complete. */
8285 : 883106 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
8286 : : {
8287 : 15 : if (decl)
8288 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
8289 : : "elements of array %q#D have incomplete type", decl);
8290 : : else
8291 : 6 : error ("elements of array %q#T have incomplete type", type);
8292 : 15 : return true;
8293 : : }
8294 : :
8295 : 883091 : location_t loc = (decl ? location_of (decl) : input_location);
8296 : 883091 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8297 : : return true;
8298 : :
8299 : : /* A compound literal can't have variable size. */
8300 : 883091 : if (init && !decl
8301 : 883091 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8302 : 5448 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8303 : : {
8304 : 3 : error ("variable-sized compound literal");
8305 : 3 : return true;
8306 : : }
8307 : : return false;
8308 : : }
8309 : :
8310 : : /* Subroutine of check_initializer; args are passed down from that function.
8311 : : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8312 : :
8313 : : static tree
8314 : 2404955 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
8315 : : {
8316 : 2404955 : gcc_assert (stmts_are_full_exprs_p ());
8317 : 2404955 : if (init)
8318 : 1766459 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8319 : 2404955 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
8320 : : }
8321 : :
8322 : : /* Verify INIT (the initializer for DECL), and record the
8323 : : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8324 : : grok_reference_init.
8325 : :
8326 : : If the return value is non-NULL, it is an expression that must be
8327 : : evaluated dynamically to initialize DECL. */
8328 : :
8329 : : static tree
8330 : 43202939 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8331 : : {
8332 : 43202939 : tree type;
8333 : 43202939 : tree init_code = NULL;
8334 : 43202939 : tree core_type;
8335 : :
8336 : : /* Things that are going to be initialized need to have complete
8337 : : type. */
8338 : 43202939 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8339 : :
8340 : 43202939 : if (DECL_HAS_VALUE_EXPR_P (decl))
8341 : : {
8342 : : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8343 : : it doesn't have storage to be initialized. */
8344 : 138 : gcc_assert (init == NULL_TREE);
8345 : : return NULL_TREE;
8346 : : }
8347 : :
8348 : 43202801 : if (type == error_mark_node)
8349 : : /* We will have already complained. */
8350 : : return NULL_TREE;
8351 : :
8352 : 43202801 : if (TREE_CODE (type) == ARRAY_TYPE)
8353 : : {
8354 : 877649 : if (check_array_initializer (decl, type, init))
8355 : : return NULL_TREE;
8356 : : }
8357 : 42325152 : else if (!COMPLETE_TYPE_P (type))
8358 : : {
8359 : 37 : error_at (DECL_SOURCE_LOCATION (decl),
8360 : : "%q#D has incomplete type", decl);
8361 : 37 : TREE_TYPE (decl) = error_mark_node;
8362 : 37 : return NULL_TREE;
8363 : : }
8364 : : else
8365 : : /* There is no way to make a variable-sized class type in GNU C++. */
8366 : 42325115 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8367 : :
8368 : 43202755 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8369 : : {
8370 : 1520331 : int init_len = CONSTRUCTOR_NELTS (init);
8371 : 1520331 : if (SCALAR_TYPE_P (type))
8372 : : {
8373 : 54008 : if (init_len == 0)
8374 : : {
8375 : 44738 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8376 : 44738 : init = build_zero_init (type, NULL_TREE, false);
8377 : : }
8378 : 9270 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8379 : : {
8380 : 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8381 : : "scalar object %qD requires one element in "
8382 : : "initializer", decl);
8383 : 6 : TREE_TYPE (decl) = error_mark_node;
8384 : 6 : return NULL_TREE;
8385 : : }
8386 : : }
8387 : : }
8388 : :
8389 : 43202749 : if (TREE_CODE (decl) == CONST_DECL)
8390 : : {
8391 : 0 : gcc_assert (!TYPE_REF_P (type));
8392 : :
8393 : 0 : DECL_INITIAL (decl) = init;
8394 : :
8395 : 0 : gcc_assert (init != NULL_TREE);
8396 : : init = NULL_TREE;
8397 : : }
8398 : 43202749 : else if (!init && DECL_REALLY_EXTERN (decl))
8399 : : ;
8400 : 43202749 : else if (flag_openmp
8401 : 131812 : && VAR_P (decl)
8402 : 131812 : && DECL_LANG_SPECIFIC (decl)
8403 : 56904 : && DECL_OMP_DECLARE_MAPPER_P (decl)
8404 : 43202749 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8405 : : return NULL_TREE;
8406 : 4314479 : else if (init || type_build_ctor_call (type)
8407 : 46878729 : || TYPE_REF_P (type))
8408 : : {
8409 : 39526784 : if (TYPE_REF_P (type))
8410 : : {
8411 : 322629 : init = grok_reference_init (decl, type, init, flags);
8412 : 322629 : flags |= LOOKUP_ALREADY_DIGESTED;
8413 : : }
8414 : 39204155 : else if (!init)
8415 : 638499 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8416 : : tf_warning_or_error);
8417 : : /* Do not reshape constructors of vectors (they don't need to be
8418 : : reshaped. */
8419 : 38565656 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8420 : : {
8421 : 1475263 : if (is_std_init_list (type))
8422 : : {
8423 : 331 : init = perform_implicit_conversion (type, init,
8424 : : tf_warning_or_error);
8425 : 331 : flags |= LOOKUP_ALREADY_DIGESTED;
8426 : : }
8427 : 1474932 : else if (TYPE_NON_AGGREGATE_CLASS (type))
8428 : : {
8429 : : /* Don't reshape if the class has constructors. */
8430 : 217712 : if (cxx_dialect == cxx98)
8431 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8432 : : "in C++98 %qD must be initialized by "
8433 : : "constructor, not by %<{...}%>",
8434 : : decl);
8435 : : }
8436 : 1257220 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8437 : : {
8438 : 0 : error ("opaque vector types cannot be initialized");
8439 : 0 : init = error_mark_node;
8440 : : }
8441 : : else
8442 : : {
8443 : 1257220 : init = reshape_init (type, init, tf_warning_or_error);
8444 : 1257220 : flags |= LOOKUP_NO_NARROWING;
8445 : : }
8446 : : }
8447 : : /* [dcl.init] "Otherwise, if the destination type is an array, the object
8448 : : is initialized as follows..." So handle things like
8449 : :
8450 : : int a[](1, 2, 3);
8451 : :
8452 : : which is permitted in C++20 by P0960. */
8453 : 37090393 : else if (TREE_CODE (init) == TREE_LIST
8454 : 629994 : && TREE_TYPE (init) == NULL_TREE
8455 : 629994 : && TREE_CODE (type) == ARRAY_TYPE
8456 : 165 : && !DECL_DECOMPOSITION_P (decl)
8457 : 37090537 : && (cxx_dialect >= cxx20))
8458 : 124 : init = do_aggregate_paren_init (init, type);
8459 : 37090269 : else if (TREE_CODE (init) == TREE_LIST
8460 : 629870 : && TREE_TYPE (init) != unknown_type_node
8461 : 37720139 : && !MAYBE_CLASS_TYPE_P (type))
8462 : : {
8463 : 135280 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8464 : :
8465 : : /* We get here with code like `int a (2);' */
8466 : 135280 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8467 : : tf_warning_or_error);
8468 : : }
8469 : :
8470 : : /* If DECL has an array type without a specific bound, deduce the
8471 : : array size from the initializer. */
8472 : 39526784 : maybe_deduce_size_from_array_init (decl, init);
8473 : 39526784 : type = TREE_TYPE (decl);
8474 : 39526784 : if (type == error_mark_node)
8475 : : return NULL_TREE;
8476 : :
8477 : 76221829 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8478 : 3500237 : && !(flags & LOOKUP_ALREADY_DIGESTED)
8479 : 3499906 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
8480 : 1314421 : && CP_AGGREGATE_TYPE_P (type)
8481 : 1096709 : && (CLASS_TYPE_P (type)
8482 : : /* The call to build_aggr_init below could end up
8483 : : calling build_vec_init, which may break when we
8484 : : are processing a template. */
8485 : 1589 : || processing_template_decl
8486 : 1523 : || !TYPE_NEEDS_CONSTRUCTING (type)
8487 : 1491 : || type_has_extended_temps (type))))
8488 : 76648930 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8489 : : {
8490 : 2404955 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8491 : :
8492 : : /* A constructor call is a non-trivial initializer even if
8493 : : it isn't explicitly written. */
8494 : 2404955 : if (TREE_SIDE_EFFECTS (init_code))
8495 : 2396326 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8496 : :
8497 : : /* If this is a constexpr initializer, expand_default_init will
8498 : : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8499 : : case, pull the initializer back out and pass it down into
8500 : : store_init_value. */
8501 : 6245512 : while (true)
8502 : : {
8503 : 6245512 : if (TREE_CODE (init_code) == EXPR_STMT
8504 : 3851805 : || TREE_CODE (init_code) == STMT_EXPR
8505 : 3842096 : || TREE_CODE (init_code) == CONVERT_EXPR)
8506 : 3830894 : init_code = TREE_OPERAND (init_code, 0);
8507 : 2414618 : else if (TREE_CODE (init_code) == BIND_EXPR)
8508 : 9663 : init_code = BIND_EXPR_BODY (init_code);
8509 : : else
8510 : : break;
8511 : : }
8512 : 2404955 : if (TREE_CODE (init_code) == INIT_EXPR)
8513 : : {
8514 : : /* In C++20, the call to build_aggr_init could have created
8515 : : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8516 : : A(1, 2). */
8517 : 1433512 : tree rhs = TREE_OPERAND (init_code, 1);
8518 : 1433512 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8519 : : /* Avoid leaking TARGET_EXPR into template trees. */
8520 : 4851 : rhs = build_implicit_conv_flags (type, init, flags);
8521 : 1433512 : init = rhs;
8522 : :
8523 : 1433512 : init_code = NULL_TREE;
8524 : : /* Don't call digest_init; it's unnecessary and will complain
8525 : : about aggregate initialization of non-aggregate classes. */
8526 : 1433497 : flags |= LOOKUP_ALREADY_DIGESTED;
8527 : : }
8528 : 971443 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8529 : 1937677 : || DECL_DECLARED_CONSTINIT_P (decl))
8530 : : {
8531 : : /* Declared constexpr or constinit, but no suitable initializer;
8532 : : massage init appropriately so we can pass it into
8533 : : store_init_value for the error. */
8534 : 5227 : tree new_init = NULL_TREE;
8535 : 5227 : if (!processing_template_decl
8536 : 384 : && TREE_CODE (init_code) == CALL_EXPR)
8537 : 328 : new_init = build_cplus_new (type, init_code, tf_none);
8538 : 4875 : else if (CLASS_TYPE_P (type)
8539 : 9774 : && (!init || TREE_CODE (init) == TREE_LIST))
8540 : 4815 : new_init = build_functional_cast (input_location, type,
8541 : : init, tf_none);
8542 : 5143 : if (new_init)
8543 : : {
8544 : 5143 : init = new_init;
8545 : 5143 : if (TREE_CODE (init) == TARGET_EXPR
8546 : 349 : && !(flags & LOOKUP_ONLYCONVERTING))
8547 : 349 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8548 : : }
8549 : : init_code = NULL_TREE;
8550 : : }
8551 : : else
8552 : : init = NULL_TREE;
8553 : : }
8554 : :
8555 : 38560563 : if (init && TREE_CODE (init) != TREE_VEC)
8556 : : {
8557 : 38560548 : init_code = store_init_value (decl, init, cleanups, flags);
8558 : :
8559 : 38557851 : if (DECL_INITIAL (decl)
8560 : 25181145 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8561 : 40236959 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8562 : : {
8563 : 1019149 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8564 : 1019149 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8565 : 1019149 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8566 : 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8567 : : }
8568 : :
8569 : 925620 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8570 : 49299 : && DECL_INITIAL (decl)
8571 : 48872 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8572 : 38603853 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8573 : 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8574 : 3 : DECL_SOURCE_LOCATION (decl)),
8575 : 3 : 0, "array %qD initialized by parenthesized "
8576 : : "string literal %qE",
8577 : 3 : decl, DECL_INITIAL (decl));
8578 : : init = NULL_TREE;
8579 : : }
8580 : : }
8581 : : else
8582 : : {
8583 : 7351930 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8584 : 4334091 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8585 : 658113 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8586 : 19 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8587 : : /*complain=*/true);
8588 : :
8589 : 3675965 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8590 : : tf_warning_or_error);
8591 : : }
8592 : :
8593 : 0 : if (init && init != error_mark_node)
8594 : 0 : init_code = cp_build_init_expr (decl, init);
8595 : :
8596 : 14461574 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8597 : 43219246 : && init_code != error_mark_node)
8598 : : init_code = NULL_TREE;
8599 : :
8600 : 43183071 : if (init_code)
8601 : : {
8602 : : /* We might have set these in cp_finish_decl. */
8603 : 14444598 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8604 : 14444598 : TREE_CONSTANT (decl) = false;
8605 : : }
8606 : :
8607 : 14444598 : if (init_code
8608 : 14444598 : && DECL_IN_AGGR_P (decl)
8609 : 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8610 : : {
8611 : 0 : static int explained = 0;
8612 : :
8613 : 0 : auto_diagnostic_group d;
8614 : 0 : if (cxx_dialect < cxx11)
8615 : 0 : error ("initializer invalid for static member with constructor");
8616 : 0 : else if (cxx_dialect < cxx17)
8617 : 0 : error ("non-constant in-class initialization invalid for static "
8618 : : "member %qD", decl);
8619 : : else
8620 : 0 : error ("non-constant in-class initialization invalid for non-inline "
8621 : : "static member %qD", decl);
8622 : 0 : if (!explained)
8623 : : {
8624 : 0 : inform (input_location,
8625 : : "(an out of class initialization is required)");
8626 : 0 : explained = 1;
8627 : : }
8628 : 0 : return NULL_TREE;
8629 : 0 : }
8630 : :
8631 : : return init_code;
8632 : : }
8633 : :
8634 : : /* If DECL is not a local variable, give it RTL. */
8635 : :
8636 : : static void
8637 : 102997931 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8638 : : {
8639 : 102997931 : int toplev = toplevel_bindings_p ();
8640 : 102997931 : int defer_p;
8641 : :
8642 : : /* Set the DECL_ASSEMBLER_NAME for the object. */
8643 : 102997931 : if (asmspec)
8644 : : {
8645 : : /* The `register' keyword, when used together with an
8646 : : asm-specification, indicates that the variable should be
8647 : : placed in a particular register. */
8648 : 982345 : if (VAR_P (decl) && DECL_REGISTER (decl))
8649 : : {
8650 : 81 : set_user_assembler_name (decl, asmspec);
8651 : 81 : DECL_HARD_REGISTER (decl) = 1;
8652 : : }
8653 : : else
8654 : : {
8655 : 982264 : if (TREE_CODE (decl) == FUNCTION_DECL
8656 : 982264 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8657 : 77886 : set_builtin_user_assembler_name (decl, asmspec);
8658 : 982264 : set_user_assembler_name (decl, asmspec);
8659 : 982264 : if (DECL_LOCAL_DECL_P (decl))
8660 : 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8661 : : /* We have to propagate the name to the ns-alias.
8662 : : This is horrible, as we're affecting a
8663 : : possibly-shared decl. Again, a one-true-decl
8664 : : model breaks down. */
8665 : 6 : if (ns_decl != error_mark_node)
8666 : 6 : set_user_assembler_name (ns_decl, asmspec);
8667 : : }
8668 : : }
8669 : :
8670 : : /* Handle non-variables up front. */
8671 : 102997931 : if (!VAR_P (decl))
8672 : : {
8673 : 54005369 : rest_of_decl_compilation (decl, toplev, at_eof);
8674 : 54005369 : return;
8675 : : }
8676 : :
8677 : : /* If we see a class member here, it should be a static data
8678 : : member. */
8679 : 48992562 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8680 : : {
8681 : 4061828 : gcc_assert (TREE_STATIC (decl));
8682 : : /* An in-class declaration of a static data member should be
8683 : : external; it is only a declaration, and not a definition. */
8684 : 4061828 : if (init == NULL_TREE)
8685 : 4061818 : gcc_assert (DECL_EXTERNAL (decl)
8686 : : || !TREE_PUBLIC (decl));
8687 : : }
8688 : :
8689 : : /* We don't create any RTL for local variables. */
8690 : 48992562 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8691 : : return;
8692 : :
8693 : : /* We defer emission of local statics until the corresponding
8694 : : DECL_EXPR is expanded. But with constexpr its function might never
8695 : : be expanded, so go ahead and tell cgraph about the variable now. */
8696 : 51142778 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8697 : 142586 : && !var_in_maybe_constexpr_fn (decl))
8698 : 51000322 : || DECL_VIRTUAL_P (decl));
8699 : :
8700 : : /* Defer template instantiations. */
8701 : 25571389 : if (DECL_LANG_SPECIFIC (decl)
8702 : 25571389 : && DECL_IMPLICIT_INSTANTIATION (decl))
8703 : : defer_p = 1;
8704 : :
8705 : : /* If we're not deferring, go ahead and assemble the variable. */
8706 : 15657878 : if (!defer_p)
8707 : 13320190 : rest_of_decl_compilation (decl, toplev, at_eof);
8708 : : }
8709 : :
8710 : : /* walk_tree helper for wrap_temporary_cleanups, below. */
8711 : :
8712 : : static tree
8713 : 10537524 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8714 : : {
8715 : : /* Stop at types or full-expression boundaries. */
8716 : 10537524 : if (TYPE_P (*stmt_p)
8717 : 10537313 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8718 : : {
8719 : 439 : *walk_subtrees = 0;
8720 : 439 : return NULL_TREE;
8721 : : }
8722 : :
8723 : 10537085 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8724 : : {
8725 : 185656 : tree guard = (tree)data;
8726 : 185656 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8727 : :
8728 : 99559 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8729 : 278477 : && !expr_noexcept_p (tcleanup, tf_none))
8730 : : {
8731 : 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8732 : : /* Tell honor_protect_cleanup_actions to handle this as a separate
8733 : : cleanup. */
8734 : 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8735 : 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8736 : : }
8737 : : }
8738 : :
8739 : : return NULL_TREE;
8740 : : }
8741 : :
8742 : : /* We're initializing a local variable which has a cleanup GUARD. If there
8743 : : are any temporaries used in the initializer INIT of this variable, we
8744 : : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8745 : : variable will be cleaned up properly if one of them throws.
8746 : :
8747 : : Unfortunately, there's no way to express this properly in terms of
8748 : : nesting, as the regions for the temporaries overlap the region for the
8749 : : variable itself; if there are two temporaries, the variable needs to be
8750 : : the first thing destroyed if either of the temporary destructors throws.
8751 : : However, we only want to run the variable's cleanup if it actually got
8752 : : constructed. So we need to guard the temporary cleanups with the
8753 : : variable's cleanup if they are run on the normal path, but not if they
8754 : : are run on the exceptional path. We implement this by telling
8755 : : honor_protect_cleanup_actions to strip the variable cleanup from the
8756 : : exceptional path.
8757 : :
8758 : : Another approach could be to make the variable cleanup region enclose
8759 : : initialization, but depend on a flag to indicate that the variable is
8760 : : initialized; that's effectively what we do for arrays. But the current
8761 : : approach works fine for non-arrays, and has no code overhead in the usual
8762 : : case where the temporary destructors are noexcept. */
8763 : :
8764 : : static void
8765 : 971045 : wrap_temporary_cleanups (tree init, tree guard)
8766 : : {
8767 : 971045 : if (TREE_CODE (guard) == BIND_EXPR)
8768 : : {
8769 : : /* An array cleanup region already encloses any temporary cleanups,
8770 : : don't wrap it around them again. */
8771 : 422 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8772 : : return;
8773 : : }
8774 : 970623 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8775 : : }
8776 : :
8777 : : /* Generate code to initialize DECL (a local variable). */
8778 : :
8779 : : static void
8780 : 23421155 : initialize_local_var (tree decl, tree init, bool decomp)
8781 : : {
8782 : 23421155 : tree type = TREE_TYPE (decl);
8783 : 23421155 : tree cleanup;
8784 : 23421155 : int already_used;
8785 : :
8786 : 23421155 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8787 : 23421155 : gcc_assert (!TREE_STATIC (decl));
8788 : :
8789 : 23421155 : if (DECL_SIZE (decl) == NULL_TREE)
8790 : : {
8791 : : /* If we used it already as memory, it must stay in memory. */
8792 : 0 : DECL_INITIAL (decl) = NULL_TREE;
8793 : 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8794 : 0 : return;
8795 : : }
8796 : :
8797 : 23421155 : if (type == error_mark_node)
8798 : : return;
8799 : :
8800 : : /* Compute and store the initial value. */
8801 : 23421155 : already_used = TREE_USED (decl) || TREE_USED (type);
8802 : 23421155 : if (TREE_USED (type))
8803 : 32 : DECL_READ_P (decl) = 1;
8804 : :
8805 : : /* Generate a cleanup, if necessary. */
8806 : 23421155 : cleanup = (decomp ? NULL_TREE
8807 : 23402156 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8808 : :
8809 : : /* Perform the initialization. */
8810 : 23421155 : if (init)
8811 : : {
8812 : 14430321 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8813 : 14430321 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8814 : 13206174 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8815 : 20560655 : && TREE_OPERAND (init, 0) == decl)
8816 : : {
8817 : : /* Stick simple initializers in DECL_INITIAL so that
8818 : : -Wno-init-self works (c++/34772). */
8819 : 7354481 : DECL_INITIAL (decl) = rinit;
8820 : :
8821 : 7354481 : if (warn_init_self && TYPE_REF_P (type))
8822 : : {
8823 : 2536 : STRIP_NOPS (rinit);
8824 : 2536 : if (rinit == decl)
8825 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
8826 : 3 : OPT_Winit_self,
8827 : : "reference %qD is initialized with itself", decl);
8828 : : }
8829 : : }
8830 : : else
8831 : : {
8832 : 7075840 : int saved_stmts_are_full_exprs_p;
8833 : :
8834 : : /* If we're only initializing a single object, guard the
8835 : : destructors of any temporaries used in its initializer with
8836 : : its destructor. */
8837 : 7075840 : if (cleanup)
8838 : 964895 : wrap_temporary_cleanups (init, cleanup);
8839 : :
8840 : 7075840 : gcc_assert (building_stmt_list_p ());
8841 : 7075840 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8842 : : /* Avoid CLEANUP_POINT_EXPR for the structured binding
8843 : : bases, those will have CLEANUP_POINT_EXPR at the end of
8844 : : code emitted by cp_finish_decomp. */
8845 : 7075840 : if (decomp)
8846 : 18575 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8847 : : else
8848 : 7057265 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8849 : 7075840 : finish_expr_stmt (init);
8850 : 7075840 : current_stmt_tree ()->stmts_are_full_exprs_p
8851 : 7075840 : = saved_stmts_are_full_exprs_p;
8852 : : }
8853 : : }
8854 : :
8855 : : /* Set this to 0 so we can tell whether an aggregate which was
8856 : : initialized was ever used. Don't do this if it has a
8857 : : destructor, so we don't complain about the 'resource
8858 : : allocation is initialization' idiom. Now set
8859 : : attribute((unused)) on types so decls of that type will be
8860 : : marked used. (see TREE_USED, above.) */
8861 : 23421155 : if (TYPE_NEEDS_CONSTRUCTING (type)
8862 : 1943609 : && ! already_used
8863 : 9789 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8864 : 23425355 : && DECL_NAME (decl))
8865 : 4172 : TREE_USED (decl) = 0;
8866 : 23416983 : else if (already_used)
8867 : 21145223 : TREE_USED (decl) = 1;
8868 : :
8869 : 23421155 : if (cleanup)
8870 : 977688 : finish_decl_cleanup (decl, cleanup);
8871 : : }
8872 : :
8873 : : /* DECL is a VAR_DECL for a compiler-generated variable with static
8874 : : storage duration (like a virtual table) whose initializer is a
8875 : : compile-time constant. Initialize the variable and provide it to the
8876 : : back end. */
8877 : :
8878 : : void
8879 : 2153518 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8880 : : {
8881 : 2153518 : tree init;
8882 : 2153518 : gcc_assert (DECL_ARTIFICIAL (decl));
8883 : 2153518 : init = build_constructor (TREE_TYPE (decl), v);
8884 : 2153518 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8885 : 2153518 : DECL_INITIAL (decl) = init;
8886 : 2153518 : DECL_INITIALIZED_P (decl) = 1;
8887 : : /* Mark the decl as constexpr so that we can access its content
8888 : : at compile time. */
8889 : 2153518 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8890 : 2153518 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
8891 : 2153518 : determine_visibility (decl);
8892 : 2153518 : layout_var_decl (decl);
8893 : 2153518 : maybe_commonize_var (decl);
8894 : 2153518 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8895 : 2153518 : }
8896 : :
8897 : : /* INIT is the initializer for a variable, as represented by the
8898 : : parser. Returns true iff INIT is value-dependent. */
8899 : :
8900 : : static bool
8901 : 8203550 : value_dependent_init_p (tree init)
8902 : : {
8903 : 8203550 : if (TREE_CODE (init) == TREE_LIST)
8904 : : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8905 : 9238 : return any_value_dependent_elements_p (init);
8906 : 8194312 : else if (TREE_CODE (init) == CONSTRUCTOR)
8907 : : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8908 : : {
8909 : 40978 : if (dependent_type_p (TREE_TYPE (init)))
8910 : : return true;
8911 : :
8912 : 40972 : vec<constructor_elt, va_gc> *elts;
8913 : 40972 : size_t nelts;
8914 : 40972 : size_t i;
8915 : :
8916 : 40972 : elts = CONSTRUCTOR_ELTS (init);
8917 : 40972 : nelts = vec_safe_length (elts);
8918 : 569627 : for (i = 0; i < nelts; ++i)
8919 : 543583 : if (value_dependent_init_p ((*elts)[i].value))
8920 : : return true;
8921 : : }
8922 : : else
8923 : : /* It must be a simple expression, e.g., int i = 3; */
8924 : 8153334 : return value_dependent_expression_p (init);
8925 : :
8926 : : return false;
8927 : : }
8928 : :
8929 : : /* A helper function to be called via walk_tree. If any label exists
8930 : : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8931 : :
8932 : : static tree
8933 : 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8934 : : {
8935 : 337 : if (TYPE_P (*tp))
8936 : 0 : *walk_subtrees = 0;
8937 : 337 : if (TREE_CODE (*tp) == LABEL_DECL)
8938 : 6 : cfun->has_forced_label_in_static = 1;
8939 : 337 : return NULL_TREE;
8940 : : }
8941 : :
8942 : : /* Return true if DECL has either a trivial destructor, or for C++20
8943 : : is constexpr and has a constexpr destructor. */
8944 : :
8945 : : static bool
8946 : 37798333 : decl_maybe_constant_destruction (tree decl, tree type)
8947 : : {
8948 : 37798333 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8949 : 37798333 : || (cxx_dialect >= cxx20
8950 : 1339619 : && VAR_P (decl)
8951 : 1339364 : && DECL_DECLARED_CONSTEXPR_P (decl)
8952 : 339 : && type_has_constexpr_destructor (strip_array_types (type))));
8953 : : }
8954 : :
8955 : : static tree declare_simd_adjust_this (tree *, int *, void *);
8956 : :
8957 : : /* Helper function of omp_declare_variant_finalize. Finalize one
8958 : : "omp declare variant base" attribute. Return true if it should be
8959 : : removed. */
8960 : :
8961 : : static bool
8962 : 1490 : omp_declare_variant_finalize_one (tree decl, tree attr)
8963 : : {
8964 : 1490 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8965 : : {
8966 : 116 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8967 : : DECL_ARGUMENTS (decl), NULL);
8968 : 116 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8969 : : DECL_ARGUMENTS (decl), NULL);
8970 : : }
8971 : :
8972 : 1490 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
8973 : 1490 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8974 : : OMP_TRAIT_CONSTRUCT_SIMD);
8975 : 1490 : if (simd)
8976 : : {
8977 : 51 : TREE_VALUE (simd)
8978 : 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8979 : 51 : OMP_TS_PROPERTIES (simd));
8980 : : /* FIXME, adjusting simd args unimplemented. */
8981 : 51 : return true;
8982 : : }
8983 : :
8984 : 1439 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
8985 : 1439 : location_t varid_loc
8986 : 1439 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8987 : 1439 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8988 : 1439 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8989 : 1439 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8990 : :
8991 : 1439 : location_t save_loc = input_location;
8992 : 1439 : input_location = varid_loc;
8993 : :
8994 : 1439 : releasing_vec args;
8995 : 1439 : tree parm = DECL_ARGUMENTS (decl);
8996 : 1439 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8997 : 116 : parm = DECL_CHAIN (parm);
8998 : 2647 : for (; parm; parm = DECL_CHAIN (parm))
8999 : 1208 : vec_safe_push (args, forward_parm (parm));
9000 : :
9001 : 1439 : unsigned nappend_args = 0;
9002 : 1439 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9003 : 1439 : if (append_args_list)
9004 : : {
9005 : 354 : append_args_list = TREE_VALUE (append_args_list);
9006 : 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9007 : 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
9008 : : : NULL_TREE);
9009 : 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9010 : 387 : nappend_args++;
9011 : 354 : if (nappend_args)
9012 : : {
9013 : 237 : tree type;
9014 : 237 : if ((type = lookup_qualified_name (global_namespace,
9015 : : "omp_interop_t",
9016 : : LOOK_want::NORMAL,
9017 : : /*complain*/false)) == NULL_TREE
9018 : 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
9019 : : {
9020 : 24 : location_t loc = input_location;
9021 : 24 : variant = tree_strip_any_location_wrapper (variant);
9022 : 24 : if (!identifier_p (variant))
9023 : : {
9024 : 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9025 : 21 : variant = OVL_FIRST (variant);
9026 : 21 : loc = EXPR_LOC_OR_LOC (variant,
9027 : : DECL_SOURCE_LOCATION (variant));
9028 : : }
9029 : 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9030 : 24 : args->length () + 1, variant);
9031 : 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9032 : : "%<append_args%> specified here");
9033 : 24 : return true;
9034 : : }
9035 : 564 : for (unsigned i = 0; i < nappend_args; i++)
9036 : 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
9037 : : }
9038 : : }
9039 : :
9040 : 1415 : bool koenig_p = false;
9041 : 1415 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9042 : : {
9043 : 1409 : if (identifier_p (variant)
9044 : : /* In C++20, we may need to perform ADL for a template
9045 : : name. */
9046 : 1385 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9047 : 18 : && identifier_p (TREE_OPERAND (variant, 0))))
9048 : : {
9049 : 24 : if (!args->is_empty ())
9050 : : {
9051 : 24 : koenig_p = true;
9052 : 24 : if (!any_type_dependent_arguments_p (args))
9053 : 21 : variant = perform_koenig_lookup (variant, args,
9054 : : tf_warning_or_error);
9055 : : }
9056 : : else
9057 : 0 : variant = unqualified_fn_lookup_error (variant);
9058 : : }
9059 : 1385 : else if (!args->is_empty () && is_overloaded_fn (variant))
9060 : : {
9061 : 716 : tree fn = get_first_fn (variant);
9062 : 716 : fn = STRIP_TEMPLATE (fn);
9063 : 716 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9064 : 716 : || DECL_FUNCTION_MEMBER_P (fn)
9065 : 603 : || DECL_LOCAL_DECL_P (fn)))
9066 : : {
9067 : 603 : koenig_p = true;
9068 : 603 : if (!any_type_dependent_arguments_p (args))
9069 : 552 : variant = perform_koenig_lookup (variant, args,
9070 : : tf_warning_or_error);
9071 : : }
9072 : : }
9073 : : }
9074 : :
9075 : 1415 : if (idk == CP_ID_KIND_QUALIFIED)
9076 : 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9077 : : koenig_p, tf_warning_or_error);
9078 : : else
9079 : 1409 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9080 : : koenig_p, tf_warning_or_error);
9081 : 1415 : if (variant == error_mark_node && !processing_template_decl)
9082 : : return true;
9083 : :
9084 : 1370 : if (TREE_CODE (variant) == TARGET_EXPR)
9085 : 9 : variant = TARGET_EXPR_INITIAL (variant);
9086 : :
9087 : 2731 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9088 : 1370 : input_location = save_loc;
9089 : :
9090 : 1370 : if (variant == decl)
9091 : : {
9092 : 0 : error_at (varid_loc, "variant %qD is the same as base function",
9093 : : variant);
9094 : 0 : return true;
9095 : : }
9096 : :
9097 : 1370 : if (variant)
9098 : : {
9099 : 1286 : bool fail;
9100 : 1286 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9101 : 1286 : if (!nappend_args)
9102 : 1127 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9103 : : COMPARE_STRICT);
9104 : : else
9105 : : {
9106 : 159 : unsigned nbase_args = 0;
9107 : 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9108 : 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9109 : 240 : nbase_args++;
9110 : 159 : tree vargs, varg;
9111 : 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9112 : 399 : for (unsigned i = 0; i < nbase_args && varg;
9113 : 240 : i++, varg = TREE_CHAIN (varg))
9114 : 240 : vargs = varg;
9115 : 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
9116 : 258 : varg = TREE_CHAIN (varg);
9117 : 159 : tree saved_vargs;
9118 : 159 : int saved_no_named_args_stdarg = 0;
9119 : 159 : if (nbase_args)
9120 : : {
9121 : 111 : saved_vargs = TREE_CHAIN (vargs);
9122 : 111 : TREE_CHAIN (vargs) = varg;
9123 : : }
9124 : : else
9125 : : {
9126 : 48 : saved_vargs = vargs;
9127 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9128 : 48 : saved_no_named_args_stdarg
9129 : 48 : = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9130 : 48 : if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9131 : 48 : && varg == NULL_TREE)
9132 : 2 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9133 : : }
9134 : : /* Skip assert check that TYPE_CANONICAL is the same. */
9135 : 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9136 : : COMPARE_STRUCTURAL);
9137 : 159 : if (nbase_args)
9138 : 111 : TREE_CHAIN (vargs) = saved_vargs;
9139 : : else
9140 : : {
9141 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9142 : 48 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9143 : 96 : = saved_no_named_args_stdarg;
9144 : : }
9145 : 159 : varg = saved_vargs;
9146 : 159 : if (!fail && !processing_template_decl)
9147 : 402 : for (unsigned i = 0; i < nappend_args;
9148 : 246 : i++, varg = TREE_CHAIN (varg))
9149 : 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9150 : : {
9151 : 9 : error_at (DECL_SOURCE_LOCATION (variant),
9152 : : "argument %d of %qD must be of %<omp_interop_t%>",
9153 : 9 : nbase_args + i + 1, variant);
9154 : 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9155 : : "%<append_args%> specified here");
9156 : 9 : break;
9157 : : }
9158 : : }
9159 : 1286 : if (fail)
9160 : : {
9161 : 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
9162 : : "types", variant, decl);
9163 : 69 : return true;
9164 : : }
9165 : 1217 : if (fndecl_built_in_p (variant)
9166 : 1217 : && (startswith (varname, "__builtin_")
9167 : 0 : || startswith (varname, "__sync_")
9168 : 0 : || startswith (varname, "__atomic_")))
9169 : : {
9170 : 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
9171 : 6 : return true;
9172 : : }
9173 : : else
9174 : : {
9175 : 1211 : tree construct
9176 : 1211 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9177 : 1211 : omp_mark_declare_variant (match_loc, variant, construct);
9178 : 1211 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9179 : : return true;
9180 : 861 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9181 : :
9182 : : // Prepend adjust_args list to variant attributes
9183 : 861 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9184 : 861 : if (adjust_args_list != NULL_TREE)
9185 : : {
9186 : 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
9187 : 285 : && TREE_VALUE (adjust_args_list))
9188 : : {
9189 : : /* Shift arg position for the added 'this' pointer. */
9190 : : /* Handle need_device_ptr */
9191 : 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9192 : 51 : t; t = TREE_CHAIN (t))
9193 : 30 : TREE_VALUE (t)
9194 : 30 : = build_int_cst (TREE_TYPE (t),
9195 : 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
9196 : : }
9197 : 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9198 : : {
9199 : : /* Shift likewise the number of args after which the
9200 : : interop object should be added. */
9201 : 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9202 : 6 : TREE_PURPOSE (nargs)
9203 : 6 : = build_int_cst (TREE_TYPE (nargs),
9204 : 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9205 : : }
9206 : 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9207 : 264 : TREE_VALUE (t)
9208 : 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9209 : 528 : DECL_ATTRIBUTES (variant) = tree_cons (
9210 : : get_identifier ("omp declare variant variant args"),
9211 : 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9212 : : }
9213 : : }
9214 : : }
9215 : 84 : else if (!processing_template_decl)
9216 : : {
9217 : 0 : error_at (varid_loc, "could not find variant declaration");
9218 : 0 : return true;
9219 : : }
9220 : :
9221 : : return false;
9222 : 1439 : }
9223 : :
9224 : : /* Helper function, finish up "omp declare variant base" attribute
9225 : : now that there is a DECL. ATTR is the first "omp declare variant base"
9226 : : attribute. */
9227 : :
9228 : : void
9229 : 1352 : omp_declare_variant_finalize (tree decl, tree attr)
9230 : : {
9231 : 1352 : size_t attr_len = strlen ("omp declare variant base");
9232 : 1352 : tree *list = &DECL_ATTRIBUTES (decl);
9233 : 1352 : bool remove_all = false;
9234 : 1352 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
9235 : 1352 : if (TREE_CHAIN (TREE_VALUE (attr))
9236 : 1352 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9237 : 2704 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9238 : 1352 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9239 : 2704 : if (DECL_CONSTRUCTOR_P (decl))
9240 : : {
9241 : 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9242 : 27 : remove_all = true;
9243 : : }
9244 : 1325 : else if (DECL_DESTRUCTOR_P (decl))
9245 : : {
9246 : 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9247 : 9 : remove_all = true;
9248 : : }
9249 : 1316 : else if (DECL_DEFAULTED_FN (decl))
9250 : : {
9251 : 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9252 : 2 : remove_all = true;
9253 : : }
9254 : 1314 : else if (DECL_DELETED_FN (decl))
9255 : : {
9256 : 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9257 : 2 : remove_all = true;
9258 : : }
9259 : 1312 : else if (DECL_VIRTUAL_P (decl))
9260 : : {
9261 : 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9262 : 0 : remove_all = true;
9263 : : }
9264 : : /* This loop is like private_lookup_attribute, except that it works
9265 : : with tree * rather than tree, as we might want to remove the
9266 : : attributes that are diagnosed as errorneous. */
9267 : 2894 : while (*list)
9268 : : {
9269 : 1542 : tree attr = get_attribute_name (*list);
9270 : 1542 : size_t ident_len = IDENTIFIER_LENGTH (attr);
9271 : 4029 : if (cmp_attribs ("omp declare variant base", attr_len,
9272 : 1542 : IDENTIFIER_POINTER (attr), ident_len))
9273 : : {
9274 : 1530 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
9275 : : {
9276 : 585 : *list = TREE_CHAIN (*list);
9277 : 585 : continue;
9278 : : }
9279 : : }
9280 : 957 : list = &TREE_CHAIN (*list);
9281 : : }
9282 : 1352 : }
9283 : :
9284 : : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9285 : :
9286 : : /* Finish processing of a declaration;
9287 : : install its line number and initial value.
9288 : : If the length of an array type is not known before,
9289 : : it must be determined now, from the initial value, or it is an error.
9290 : :
9291 : : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9292 : : true, then INIT is an integral constant expression.
9293 : :
9294 : : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9295 : : if the (init) syntax was used.
9296 : :
9297 : : DECOMP is first identifier's DECL and identifier count in a structured
9298 : : bindings, nullptr if not a structured binding. */
9299 : :
9300 : : void
9301 : 283328202 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9302 : : tree asmspec_tree, int flags, cp_decomp *decomp)
9303 : : {
9304 : 283328202 : vec<tree, va_gc> *cleanups = NULL;
9305 : 283328202 : const char *asmspec = NULL;
9306 : 283328202 : int was_readonly = 0;
9307 : 283328202 : bool var_definition_p = false;
9308 : 283328202 : tree auto_node;
9309 : 283328202 : auto_vec<tree> extra_cleanups;
9310 : 283328202 : tree aggregates1 = NULL_TREE;
9311 : 283328202 : struct decomp_cleanup {
9312 : : tree decl;
9313 : : cp_decomp *&decomp;
9314 : 283325505 : ~decomp_cleanup ()
9315 : : {
9316 : 283325505 : if (decomp && DECL_DECOMPOSITION_P (decl))
9317 : 80218 : cp_finish_decomp (decl, decomp);
9318 : 283325505 : }
9319 : 283328202 : } decomp_cl = { decl, decomp };
9320 : :
9321 : 283328202 : if (decl == error_mark_node)
9322 : : return;
9323 : 283328165 : else if (! decl)
9324 : : {
9325 : 0 : if (init)
9326 : 0 : error ("assignment (not initialization) in declaration");
9327 : 0 : return;
9328 : : }
9329 : :
9330 : 283328165 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9331 : : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9332 : 283328165 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
9333 : :
9334 : 283328165 : tree type = TREE_TYPE (decl);
9335 : 283328165 : if (type == error_mark_node)
9336 : : return;
9337 : :
9338 : 283327629 : if (VAR_P (decl) && is_copy_initialization (init))
9339 : 78549038 : flags |= LOOKUP_ONLYCONVERTING;
9340 : :
9341 : : /* Warn about register storage specifiers except when in GNU global
9342 : : or local register variable extension. */
9343 : 283327629 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9344 : : {
9345 : 1928 : if (cxx_dialect >= cxx17)
9346 : 1280 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9347 : : "ISO C++17 does not allow %<register%> storage "
9348 : : "class specifier");
9349 : : else
9350 : 648 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9351 : : "%<register%> storage class specifier used");
9352 : : }
9353 : :
9354 : : /* If a name was specified, get the string. */
9355 : 283327629 : if (at_namespace_scope_p ())
9356 : 60018112 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9357 : 283327629 : if (asmspec_tree && asmspec_tree != error_mark_node)
9358 : 982348 : asmspec = TREE_STRING_POINTER (asmspec_tree);
9359 : :
9360 : 283327629 : bool in_class_decl
9361 : 283327629 : = (current_class_type
9362 : 175043119 : && CP_DECL_CONTEXT (decl) == current_class_type
9363 : 117401467 : && TYPE_BEING_DEFINED (current_class_type)
9364 : 399205960 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9365 : :
9366 : 112510589 : if (in_class_decl
9367 : 112510589 : && (DECL_INITIAL (decl) || init))
9368 : 80952149 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9369 : :
9370 : 283327629 : if (VAR_P (decl)
9371 : 283327629 : && (auto_node = type_uses_auto (type)))
9372 : : {
9373 : 8671990 : tree d_init;
9374 : 8671990 : if (init == NULL_TREE)
9375 : : {
9376 : 1396 : if (DECL_LANG_SPECIFIC (decl)
9377 : 1315 : && DECL_TEMPLATE_INSTANTIATION (decl)
9378 : 2708 : && !DECL_TEMPLATE_INSTANTIATED (decl))
9379 : : {
9380 : : /* init is null because we're deferring instantiating the
9381 : : initializer until we need it. Well, we need it now. */
9382 : 1306 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9383 : 1306 : return;
9384 : : }
9385 : :
9386 : 90 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9387 : : /* Class deduction with no initializer is OK. */;
9388 : : else
9389 : : {
9390 : : /* Ordinary auto deduction without an initializer, a situation
9391 : : which grokdeclarator already detects and rejects for the most
9392 : : part. But we can still get here if we're instantiating a
9393 : : variable template before we've fully parsed (and attached) its
9394 : : initializer, e.g. template<class> auto x = x<int>; */
9395 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
9396 : : "declaration of %q#D has no initializer", decl);
9397 : 3 : TREE_TYPE (decl) = error_mark_node;
9398 : 3 : return;
9399 : : }
9400 : : }
9401 : 8670681 : d_init = init;
9402 : 8670681 : if (d_init)
9403 : : {
9404 : 8670594 : if (TREE_CODE (d_init) == TREE_LIST
9405 : 8706569 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9406 : 22383 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9407 : : tf_warning_or_error);
9408 : 8670594 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9409 : : /* Force auto deduction now. Use tf_none to avoid redundant warnings
9410 : : on deprecated-14.C. */
9411 : 8670594 : mark_single_function (d_init, tf_none);
9412 : : }
9413 : 8670681 : enum auto_deduction_context adc = adc_variable_type;
9414 : 8670681 : if (DECL_DECOMPOSITION_P (decl))
9415 : : adc = adc_decomp_type;
9416 : 8670681 : tree outer_targs = NULL_TREE;
9417 : 8670681 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9418 : 335 : && DECL_LANG_SPECIFIC (decl)
9419 : 126 : && DECL_TEMPLATE_INFO (decl)
9420 : 8670738 : && !DECL_FUNCTION_SCOPE_P (decl))
9421 : : /* The outer template arguments might be needed for satisfaction.
9422 : : (For function scope variables, do_auto_deduction will obtain the
9423 : : outer template arguments from current_function_decl.) */
9424 : 57 : outer_targs = DECL_TI_ARGS (decl);
9425 : 8670681 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9426 : : tf_warning_or_error, adc,
9427 : : outer_targs, flags);
9428 : 8670681 : if (type == error_mark_node)
9429 : : return;
9430 : 8669826 : if (TREE_CODE (type) == FUNCTION_TYPE)
9431 : : {
9432 : 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
9433 : : "did you forget the %<()%>?", decl);
9434 : 3 : TREE_TYPE (decl) = error_mark_node;
9435 : 3 : return;
9436 : : }
9437 : : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9438 : 8669823 : if (!processing_template_decl
9439 : 2013705 : && !type_uses_auto (type)
9440 : 10683521 : && !COMPLETE_TYPE_P (complete_type (type)))
9441 : : {
9442 : 18 : auto_diagnostic_group d;
9443 : 18 : error_at (location_of (decl),
9444 : : "deduced type %qT for %qD is incomplete", type, decl);
9445 : 18 : cxx_incomplete_type_inform (type);
9446 : 18 : TREE_TYPE (decl) = error_mark_node;
9447 : 18 : return;
9448 : 18 : }
9449 : :
9450 : : /* Now that we have a type, try these again. */
9451 : 8669805 : layout_decl (decl, 0);
9452 : 8669805 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9453 : :
9454 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
9455 : 8669805 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9456 : : {
9457 : 16465 : tree tmpl = template_for_substitution (decl);
9458 : 16465 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9459 : 14696 : TREE_TYPE (tmpl) = type;
9460 : : }
9461 : : }
9462 : :
9463 : 283325444 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9464 : : {
9465 : 61 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9466 : 61 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9467 : : {
9468 : 6 : init = NULL_TREE;
9469 : 6 : DECL_EXTERNAL (decl) = 1;
9470 : : }
9471 : : }
9472 : :
9473 : 283325444 : if (VAR_P (decl)
9474 : 100237090 : && DECL_CLASS_SCOPE_P (decl)
9475 : 15150747 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
9476 : : TCTX_STATIC_STORAGE, type)
9477 : 298476191 : && DECL_INITIALIZED_IN_CLASS_P (decl))
9478 : 14006686 : check_static_variable_definition (decl, type);
9479 : :
9480 : 283325444 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
9481 : : {
9482 : 23420426 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9483 : 23420426 : ? TCTX_THREAD_STORAGE
9484 : : : TCTX_STATIC_STORAGE);
9485 : 23420426 : verify_type_context (input_location, context, TREE_TYPE (decl));
9486 : : }
9487 : :
9488 : 283325444 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
9489 : : {
9490 : 352763 : tree clone;
9491 : 352763 : if (init == ridpointers[(int)RID_DELETE]
9492 : 352763 : || (TREE_CODE (init) == STRING_CST
9493 : 24 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9494 : : {
9495 : : /* FIXME check this is 1st decl. */
9496 : 329723 : if (UNLIKELY (DECL_MAIN_P (decl)))
9497 : : {
9498 : : /* [basic.start.main]/3: A program that defines main as deleted
9499 : : is ill-formed. */
9500 : 3 : error ("%<::main%> cannot be deleted");
9501 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9502 : : }
9503 : : else
9504 : : {
9505 : 329720 : DECL_DELETED_FN (decl) = 1;
9506 : 329720 : DECL_DECLARED_INLINE_P (decl) = 1;
9507 : 329720 : DECL_INITIAL (decl)
9508 : 329720 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9509 : 329738 : FOR_EACH_CLONE (clone, decl)
9510 : : {
9511 : 18 : DECL_DELETED_FN (clone) = 1;
9512 : 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9513 : 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9514 : : }
9515 : : }
9516 : 329723 : init = NULL_TREE;
9517 : : }
9518 : 23040 : else if (init == ridpointers[(int)RID_DEFAULT])
9519 : : {
9520 : 23028 : if (defaultable_fn_check (decl))
9521 : 23004 : DECL_DEFAULTED_FN (decl) = 1;
9522 : : else
9523 : 24 : DECL_INITIAL (decl) = NULL_TREE;
9524 : : }
9525 : : }
9526 : :
9527 : 283325444 : if (init && VAR_P (decl))
9528 : : {
9529 : 85687460 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9530 : : /* If DECL is a reference, then we want to know whether init is a
9531 : : reference constant; init_const_expr_p as passed tells us whether
9532 : : it's an rvalue constant. */
9533 : 85687460 : if (TYPE_REF_P (type))
9534 : 2272909 : init_const_expr_p = potential_constant_expression (init);
9535 : 85687460 : if (init_const_expr_p)
9536 : : {
9537 : : /* Set these flags now for templates. We'll update the flags in
9538 : : store_init_value for instantiations. */
9539 : 73501791 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9540 : 73501791 : if (decl_maybe_constant_var_p (decl)
9541 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9542 : 73501791 : && !TYPE_REF_P (type))
9543 : 26899024 : TREE_CONSTANT (decl) = true;
9544 : : }
9545 : : /* This is handled mostly by gimplify.cc, but we have to deal with
9546 : : not warning about int x = x; as it is a GCC extension to turn off
9547 : : this warning but only if warn_init_self is zero. */
9548 : 85687460 : if (!DECL_EXTERNAL (decl)
9549 : 84205931 : && !TREE_STATIC (decl)
9550 : 63172557 : && decl == tree_strip_any_location_wrapper (init)
9551 : 85691262 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9552 : 3778 : suppress_warning (decl, OPT_Winit_self);
9553 : : }
9554 : 197637984 : else if (VAR_P (decl)
9555 : 14549630 : && COMPLETE_TYPE_P (type)
9556 : 11425718 : && !TYPE_REF_P (type)
9557 : 11425354 : && !dependent_type_p (type)
9558 : 208773014 : && is_really_empty_class (type, /*ignore_vptr*/false))
9559 : : /* We have no initializer but there's nothing to initialize anyway.
9560 : : Treat DECL as constant due to c++/109876. */
9561 : 491981 : TREE_CONSTANT (decl) = true;
9562 : :
9563 : 283325444 : if (flag_openmp
9564 : 744054 : && TREE_CODE (decl) == FUNCTION_DECL
9565 : : /* #pragma omp declare variant on methods handled in finish_struct
9566 : : instead. */
9567 : 283664428 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9568 : 129411 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9569 : 210026 : if (tree attr = lookup_attribute ("omp declare variant base",
9570 : 210026 : DECL_ATTRIBUTES (decl)))
9571 : 967 : omp_declare_variant_finalize (decl, attr);
9572 : :
9573 : 283325444 : if (processing_template_decl)
9574 : : {
9575 : 164110493 : bool type_dependent_p;
9576 : :
9577 : : /* Add this declaration to the statement-tree. */
9578 : 164110493 : if (at_function_scope_p ())
9579 : 59683679 : add_decl_expr (decl);
9580 : :
9581 : 164110493 : type_dependent_p = dependent_type_p (type);
9582 : :
9583 : 164110493 : if (check_for_bare_parameter_packs (init))
9584 : : {
9585 : 3 : init = NULL_TREE;
9586 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9587 : : }
9588 : :
9589 : : /* Generally, initializers in templates are expanded when the
9590 : : template is instantiated. But, if DECL is a variable constant
9591 : : then it can be used in future constant expressions, so its value
9592 : : must be available. */
9593 : :
9594 : 164110493 : bool dep_init = false;
9595 : :
9596 : 164110493 : if (!VAR_P (decl) || type_dependent_p)
9597 : : /* We can't do anything if the decl has dependent type. */;
9598 : 20053055 : else if (init
9599 : 18481815 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9600 : 14625853 : && !TYPE_REF_P (type)
9601 : 14599620 : && decl_maybe_constant_var_p (decl)
9602 : 27713022 : && !(dep_init = value_dependent_init_p (init)))
9603 : : {
9604 : : /* This variable seems to be a non-dependent constant, so process
9605 : : its initializer. If check_initializer returns non-null the
9606 : : initialization wasn't constant after all. */
9607 : 1814780 : tree init_code;
9608 : 1814780 : cleanups = make_tree_vector ();
9609 : 1814780 : init_code = check_initializer (decl, init, flags, &cleanups);
9610 : 1814780 : if (init_code == NULL_TREE)
9611 : 1814780 : init = NULL_TREE;
9612 : 1814780 : release_tree_vector (cleanups);
9613 : : }
9614 : : else
9615 : : {
9616 : 18238275 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9617 : : /* Try to deduce array size. */
9618 : 18238275 : maybe_deduce_size_from_array_init (decl, init);
9619 : : /* And complain about multiple initializers. */
9620 : 16667035 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9621 : 18298411 : && !MAYBE_CLASS_TYPE_P (type))
9622 : 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9623 : : tf_warning_or_error);
9624 : : }
9625 : :
9626 : 164110493 : if (init)
9627 : 47685790 : DECL_INITIAL (decl) = init;
9628 : :
9629 : 164110493 : if (dep_init)
9630 : : {
9631 : 5845187 : retrofit_lang_decl (decl);
9632 : 5845187 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9633 : : }
9634 : :
9635 : 164110493 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9636 : : {
9637 : 3 : set_user_assembler_name (decl, asmspec);
9638 : 3 : DECL_HARD_REGISTER (decl) = 1;
9639 : : }
9640 : 164110493 : return;
9641 : : }
9642 : :
9643 : : /* Just store non-static data member initializers for later. */
9644 : 119214951 : if (init && TREE_CODE (decl) == FIELD_DECL)
9645 : 389170 : DECL_INITIAL (decl) = init;
9646 : :
9647 : : /* Take care of TYPE_DECLs up front. */
9648 : 119214951 : if (TREE_CODE (decl) == TYPE_DECL)
9649 : : {
9650 : 9753414 : if (type != error_mark_node
9651 : 9753414 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9652 : : {
9653 : 4454686 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9654 : 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9655 : 4454686 : set_identifier_type_value (DECL_NAME (decl), decl);
9656 : : }
9657 : :
9658 : : /* If we have installed this as the canonical typedef for this
9659 : : type, and that type has not been defined yet, delay emitting
9660 : : the debug information for it, as we will emit it later. */
9661 : 9753414 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9662 : 9753414 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9663 : 561547 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9664 : :
9665 : 9753414 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9666 : : at_eof);
9667 : 9753414 : return;
9668 : : }
9669 : :
9670 : : /* A reference will be modified here, as it is initialized. */
9671 : 109461537 : if (! DECL_EXTERNAL (decl)
9672 : 49030714 : && TREE_READONLY (decl)
9673 : 134479682 : && TYPE_REF_P (type))
9674 : : {
9675 : 233709 : was_readonly = 1;
9676 : 233709 : TREE_READONLY (decl) = 0;
9677 : : }
9678 : :
9679 : : /* This needs to happen before extend_ref_init_temps. */
9680 : 109461537 : if (VAR_OR_FUNCTION_DECL_P (decl))
9681 : : {
9682 : 100847134 : if (VAR_P (decl))
9683 : 46841765 : maybe_commonize_var (decl);
9684 : 100847134 : determine_visibility (decl);
9685 : : }
9686 : :
9687 : 109461537 : if (VAR_P (decl))
9688 : : {
9689 : 46841765 : duration_kind dk = decl_storage_duration (decl);
9690 : : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9691 : : only to a declaration of a variable with static or thread storage
9692 : : duration." */
9693 : 46841765 : if (DECL_DECLARED_CONSTINIT_P (decl)
9694 : 46841765 : && !(dk == dk_thread || dk == dk_static))
9695 : : {
9696 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
9697 : : "%<constinit%> can only be applied to a variable with "
9698 : : "static or thread storage duration");
9699 : 24 : return;
9700 : : }
9701 : :
9702 : 46841741 : if (decomp)
9703 : : {
9704 : 37607 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9705 : 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9706 : : "%<constinit%> can be applied to structured binding "
9707 : : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
9708 : 37607 : cp_maybe_mangle_decomp (decl, decomp);
9709 : 37607 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9710 : : {
9711 : 346 : if (CP_DECL_THREAD_LOCAL_P (decl))
9712 : 37 : aggregates1 = tls_aggregates;
9713 : : else
9714 : 309 : aggregates1 = static_aggregates;
9715 : : }
9716 : : }
9717 : :
9718 : : /* If this is a local variable that will need a mangled name,
9719 : : register it now. We must do this before processing the
9720 : : initializer for the variable, since the initialization might
9721 : : require a guard variable, and since the mangled name of the
9722 : : guard variable will depend on the mangled name of this
9723 : : variable. */
9724 : 93683482 : if (DECL_FUNCTION_SCOPE_P (decl)
9725 : 23563759 : && TREE_STATIC (decl)
9726 : 46984327 : && !DECL_ARTIFICIAL (decl))
9727 : : {
9728 : : /* The variable holding an anonymous union will have had its
9729 : : discriminator set in finish_anon_union, after which it's
9730 : : NAME will have been cleared. */
9731 : 141697 : if (DECL_NAME (decl))
9732 : 141654 : determine_local_discriminator (decl);
9733 : : /* Normally has_forced_label_in_static is set during GIMPLE
9734 : : lowering, but [cd]tors are never actually compiled directly.
9735 : : We need to set this early so we can deal with the label
9736 : : address extension. */
9737 : 141697 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
9738 : 141668 : || DECL_DESTRUCTOR_P (current_function_decl))
9739 : 141707 : && init)
9740 : : {
9741 : 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
9742 : 29 : add_local_decl (cfun, decl);
9743 : : }
9744 : : /* And make sure it's in the symbol table for
9745 : : c_parse_final_cleanups to find. */
9746 : 141697 : varpool_node::get_create (decl);
9747 : : }
9748 : :
9749 : 46841741 : if (flag_openmp
9750 : 135788 : && VAR_P (decl)
9751 : 135788 : && DECL_LANG_SPECIFIC (decl)
9752 : 61069 : && DECL_OMP_DECLARE_MAPPER_P (decl)
9753 : 46841745 : && init)
9754 : : {
9755 : 3 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9756 : 3 : DECL_INITIAL (decl) = init;
9757 : : }
9758 : : /* Convert the initializer to the type of DECL, if we have not
9759 : : already initialized DECL. */
9760 : 46841738 : else if (!DECL_INITIALIZED_P (decl)
9761 : : /* If !DECL_EXTERNAL then DECL is being defined. In the
9762 : : case of a static data member initialized inside the
9763 : : class-specifier, there can be an initializer even if DECL
9764 : : is *not* defined. */
9765 : 46841738 : && (!DECL_EXTERNAL (decl) || init))
9766 : : {
9767 : 41388159 : cleanups = make_tree_vector ();
9768 : 41388159 : init = check_initializer (decl, init, flags, &cleanups);
9769 : :
9770 : : /* Handle:
9771 : :
9772 : : [dcl.init]
9773 : :
9774 : : The memory occupied by any object of static storage
9775 : : duration is zero-initialized at program startup before
9776 : : any other initialization takes place.
9777 : :
9778 : : We cannot create an appropriate initializer until after
9779 : : the type of DECL is finalized. If DECL_INITIAL is set,
9780 : : then the DECL is statically initialized, and any
9781 : : necessary zero-initialization has already been performed. */
9782 : 41385462 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9783 : 419849 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9784 : : /*nelts=*/NULL_TREE,
9785 : : /*static_storage_p=*/true);
9786 : : /* Remember that the initialization for this variable has
9787 : : taken place. */
9788 : 41385462 : DECL_INITIALIZED_P (decl) = 1;
9789 : : /* This declaration is the definition of this variable,
9790 : : unless we are initializing a static data member within
9791 : : the class specifier. */
9792 : 41385462 : if (!DECL_EXTERNAL (decl))
9793 : 46839044 : var_definition_p = true;
9794 : : }
9795 : : /* If the variable has an array type, lay out the type, even if
9796 : : there is no initializer. It is valid to index through the
9797 : : array, and we must get TYPE_ALIGN set correctly on the array
9798 : : type. */
9799 : 5453579 : else if (TREE_CODE (type) == ARRAY_TYPE)
9800 : 221229 : layout_type (type);
9801 : :
9802 : 46839044 : if (TREE_STATIC (decl)
9803 : 22853552 : && !at_function_scope_p ()
9804 : 69550010 : && current_function_decl == NULL)
9805 : : /* So decl is a global variable or a static member of a
9806 : : non local class. Record the types it uses
9807 : : so that we can decide later to emit debug info for them. */
9808 : 22710954 : record_types_used_by_current_var_decl (decl);
9809 : : }
9810 : :
9811 : : /* Add this declaration to the statement-tree. This needs to happen
9812 : : after the call to check_initializer so that the DECL_EXPR for a
9813 : : reference temp is added before the DECL_EXPR for the reference itself. */
9814 : 109458816 : if (DECL_FUNCTION_SCOPE_P (decl))
9815 : : {
9816 : : /* If we're building a variable sized type, and we might be
9817 : : reachable other than via the top of the current binding
9818 : : level, then create a new BIND_EXPR so that we deallocate
9819 : : the object at the right time. */
9820 : 23563759 : if (VAR_P (decl)
9821 : 23563759 : && DECL_SIZE (decl)
9822 : 23513874 : && !TREE_CONSTANT (DECL_SIZE (decl))
9823 : 23564752 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9824 : : {
9825 : 3 : tree bind;
9826 : 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9827 : 3 : TREE_SIDE_EFFECTS (bind) = 1;
9828 : 3 : add_stmt (bind);
9829 : 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
9830 : : }
9831 : 23563759 : add_decl_expr (decl);
9832 : : }
9833 : :
9834 : 109458816 : tree decomp_init = NULL_TREE;
9835 : : /* Let the middle end know about variables and functions -- but not
9836 : : static data members in uninstantiated class templates. */
9837 : 109458816 : if (VAR_OR_FUNCTION_DECL_P (decl))
9838 : : {
9839 : 100844413 : if (VAR_P (decl))
9840 : : {
9841 : 46839044 : layout_var_decl (decl);
9842 : 46839044 : if (!flag_weak)
9843 : : /* Check again now that we have an initializer. */
9844 : 57 : maybe_commonize_var (decl);
9845 : : /* A class-scope constexpr variable with an out-of-class declaration.
9846 : : C++17 makes them implicitly inline, but still force it out. */
9847 : 63241032 : if (DECL_INLINE_VAR_P (decl)
9848 : 16313889 : && !DECL_VAR_DECLARED_INLINE_P (decl)
9849 : 9104677 : && !DECL_TEMPLATE_INSTANTIATION (decl)
9850 : 30578809 : && !in_class_decl)
9851 : 103 : mark_needed (decl);
9852 : : }
9853 : :
9854 : 100844413 : if (var_definition_p
9855 : : /* With -fmerge-all-constants, gimplify_init_constructor
9856 : : might add TREE_STATIC to aggregate variables. */
9857 : 40347238 : && (TREE_STATIC (decl)
9858 : 23421315 : || (flag_merge_constants >= 2
9859 : 38 : && AGGREGATE_TYPE_P (type))))
9860 : : {
9861 : : /* If a TREE_READONLY variable needs initialization
9862 : : at runtime, it is no longer readonly and we need to
9863 : : avoid MEM_READONLY_P being set on RTL created for it. */
9864 : 16925926 : if (init)
9865 : : {
9866 : 14269 : if (TREE_READONLY (decl))
9867 : 524 : TREE_READONLY (decl) = 0;
9868 : : was_readonly = 0;
9869 : : }
9870 : 16911657 : else if (was_readonly)
9871 : 1913 : TREE_READONLY (decl) = 1;
9872 : :
9873 : : /* Likewise if it needs destruction. */
9874 : 16925926 : if (!decl_maybe_constant_destruction (decl, type))
9875 : 3421 : TREE_READONLY (decl) = 0;
9876 : : }
9877 : 83918487 : else if (VAR_P (decl)
9878 : 29913118 : && CP_DECL_THREAD_LOCAL_P (decl)
9879 : 20067 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9880 : 20067 : && (was_readonly || TREE_READONLY (decl))
9881 : 83918502 : && var_needs_tls_wrapper (decl))
9882 : : {
9883 : : /* TLS variables need dynamic initialization by the TLS wrapper
9884 : : function, we don't want to hoist accesses to it before the
9885 : : wrapper. */
9886 : 6 : was_readonly = 0;
9887 : 6 : TREE_READONLY (decl) = 0;
9888 : : }
9889 : :
9890 : 100844413 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
9891 : :
9892 : : /* Check for abstractness of the type. */
9893 : 100844413 : if (var_definition_p)
9894 : 40347238 : abstract_virtuals_error (decl, type);
9895 : :
9896 : 100844413 : if (decomp && !cp_finish_decomp (decl, decomp, true))
9897 : 18369 : decomp = NULL;
9898 : :
9899 : 100844413 : if (TREE_TYPE (decl) == error_mark_node)
9900 : : /* No initialization required. */
9901 : : ;
9902 : 100844342 : else if (TREE_CODE (decl) == FUNCTION_DECL)
9903 : : {
9904 : 54005369 : if (init)
9905 : : {
9906 : 576 : if (init == ridpointers[(int)RID_DEFAULT])
9907 : : {
9908 : : /* An out-of-class default definition is defined at
9909 : : the point where it is explicitly defaulted. */
9910 : 564 : if (DECL_DELETED_FN (decl))
9911 : 6 : maybe_explain_implicit_delete (decl);
9912 : 558 : else if (DECL_INITIAL (decl) == error_mark_node)
9913 : 534 : synthesize_method (decl);
9914 : : }
9915 : : else
9916 : 24 : error_at (cp_expr_loc_or_loc (init,
9917 : 12 : DECL_SOURCE_LOCATION (decl)),
9918 : : "function %q#D is initialized like a variable",
9919 : : decl);
9920 : : }
9921 : : /* else no initialization required. */
9922 : : }
9923 : 46838973 : else if (DECL_EXTERNAL (decl)
9924 : 46838973 : && ! (DECL_LANG_SPECIFIC (decl)
9925 : 6185191 : && DECL_NOT_REALLY_EXTERN (decl)))
9926 : : {
9927 : : /* check_initializer will have done any constant initialization. */
9928 : : }
9929 : : /* A variable definition. */
9930 : 42194314 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9931 : : {
9932 : : /* Initialize the local variable. */
9933 : 23421155 : if (!decomp)
9934 : 23402156 : initialize_local_var (decl, init, false);
9935 : : else
9936 : : {
9937 : 18999 : tree cleanup = NULL_TREE;
9938 : 18999 : if (DECL_SIZE (decl))
9939 : 18999 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9940 : : /* If cp_finish_decomp needs to emit any code, we need to emit that
9941 : : code after code emitted by initialize_local_var in a single
9942 : : CLEANUP_POINT_EXPR, so that temporaries are destructed only
9943 : : after the cp_finish_decomp emitted code.
9944 : : If there are any cleanups, either extend_ref_init_temps
9945 : : created ones or e.g. array destruction, push those first
9946 : : with the cleanups guarded on a bool temporary, initially
9947 : : set to false and set to true after initialize_local_var
9948 : : emitted code. */
9949 : 18999 : tree guard = NULL_TREE;
9950 : 18999 : if (cleanups || cleanup)
9951 : : {
9952 : 18999 : guard = get_internal_target_expr (boolean_false_node);
9953 : 18999 : add_stmt (guard);
9954 : 18999 : guard = TARGET_EXPR_SLOT (guard);
9955 : : }
9956 : 18999 : tree sl = push_stmt_list ();
9957 : 18999 : initialize_local_var (decl, init, true);
9958 : 18999 : if (guard)
9959 : : {
9960 : 18999 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9961 : : guard, boolean_true_node));
9962 : 19017 : for (tree &t : *cleanups)
9963 : 18 : t = build3 (COND_EXPR, void_type_node,
9964 : : guard, t, void_node);
9965 : 18999 : if (cleanup)
9966 : 25 : cleanup = build3 (COND_EXPR, void_type_node,
9967 : : guard, cleanup, void_node);
9968 : : }
9969 : 18999 : unsigned before = stmt_list_stack->length ();
9970 : 18999 : cp_finish_decomp (decl, decomp);
9971 : 18999 : decomp = NULL;
9972 : 18999 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
9973 : 18999 : sl = pop_stmt_list (sl);
9974 : 18999 : if (n_extra_cleanups)
9975 : : {
9976 : : /* If cp_finish_decomp needs any cleanups, such as for
9977 : : extend_ref_init_temps created vars, pop_stmt_list
9978 : : popped that all, so push those extra cleanups around
9979 : : the whole sequence with a guard variable. */
9980 : 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
9981 : 15 : guard = get_internal_target_expr (integer_zero_node);
9982 : 15 : add_stmt (guard);
9983 : 15 : guard = TARGET_EXPR_SLOT (guard);
9984 : 63 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
9985 : : {
9986 : 48 : tree_stmt_iterator tsi = tsi_last (sl);
9987 : 48 : gcc_assert (!tsi_end_p (tsi));
9988 : 48 : tree last = tsi_stmt (tsi);
9989 : 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
9990 : : && !CLEANUP_EH_ONLY (last));
9991 : 48 : tree cst = build_int_cst (integer_type_node, i + 1);
9992 : 96 : tree cl = build3 (COND_EXPR, void_type_node,
9993 : : build2 (GE_EXPR, boolean_type_node,
9994 : : guard, cst),
9995 : 48 : CLEANUP_EXPR (last), void_node);
9996 : 48 : extra_cleanups.safe_push (cl);
9997 : 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
9998 : : integer_type_node,
9999 : : guard, cst),
10000 : : TSI_SAME_STMT);
10001 : 48 : tree sl2 = CLEANUP_BODY (last);
10002 : 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10003 : 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10004 : 48 : tsi_delink (&tsi);
10005 : : }
10006 : : }
10007 : 18999 : decomp_init = maybe_cleanup_point_expr_void (sl);
10008 : 18999 : if (cleanup)
10009 : 25 : finish_decl_cleanup (decl, cleanup);
10010 : : }
10011 : : }
10012 : :
10013 : : /* If a variable is defined, and then a subsequent
10014 : : definition with external linkage is encountered, we will
10015 : : get here twice for the same variable. We want to avoid
10016 : : calling expand_static_init more than once. For variables
10017 : : that are not static data members, we can call
10018 : : expand_static_init only when we actually process the
10019 : : initializer. It is not legal to redeclare a static data
10020 : : member, so this issue does not arise in that case. */
10021 : 18773159 : else if (var_definition_p && TREE_STATIC (decl))
10022 : : {
10023 : 16925880 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10024 : : {
10025 : 98 : tree sl = push_stmt_list ();
10026 : 98 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10027 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10028 : 98 : expand_static_init (decl, init);
10029 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p
10030 : 98 : = saved_stmts_are_full_exprs_p;
10031 : 98 : cp_finish_decomp (decl, decomp);
10032 : 98 : decomp = NULL;
10033 : 98 : sl = pop_stmt_list (sl);
10034 : 98 : sl = maybe_cleanup_point_expr_void (sl);
10035 : 98 : add_stmt (sl);
10036 : : }
10037 : : else
10038 : 16925782 : expand_static_init (decl, init);
10039 : : }
10040 : : }
10041 : :
10042 : : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10043 : : reference, insert it in the statement-tree now. */
10044 : 109458816 : if (cleanups)
10045 : : {
10046 : 41391612 : for (tree t : *cleanups)
10047 : : {
10048 : 6150 : push_cleanup (NULL_TREE, t, false);
10049 : : /* As in initialize_local_var. */
10050 : 6150 : wrap_temporary_cleanups (init, t);
10051 : : }
10052 : 41385462 : release_tree_vector (cleanups);
10053 : : }
10054 : :
10055 : 109458894 : for (tree t : &extra_cleanups)
10056 : 48 : push_cleanup (NULL_TREE, t, false);
10057 : :
10058 : 109458816 : if (decomp_init)
10059 : 18999 : add_stmt (decomp_init);
10060 : :
10061 : 109458816 : if (decomp
10062 : 141 : && var_definition_p
10063 : 141 : && TREE_STATIC (decl)
10064 : 109458957 : && !DECL_FUNCTION_SCOPE_P (decl))
10065 : : {
10066 : 141 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10067 : 141 : ? tls_aggregates : static_aggregates);
10068 : 141 : tree aggregates2 = aggregates3;
10069 : 141 : if (aggregates2 != aggregates1)
10070 : : {
10071 : 66 : cp_finish_decomp (decl, decomp);
10072 : 66 : decomp = NULL;
10073 : 66 : if (aggregates3 != aggregates2)
10074 : : {
10075 : : /* If there are dynamic initializers for the structured
10076 : : binding base or associated extended ref temps and also
10077 : : dynamic initializers for the structured binding non-base
10078 : : vars, mark them. */
10079 : 315 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10080 : 252 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10081 : 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10082 : 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
10083 : : }
10084 : : }
10085 : : }
10086 : :
10087 : 109458816 : if (was_readonly)
10088 : 233127 : TREE_READONLY (decl) = 1;
10089 : :
10090 : 109458816 : if (flag_openmp
10091 : 360804 : && VAR_P (decl)
10092 : 109594604 : && lookup_attribute ("omp declare target implicit",
10093 : 135788 : DECL_ATTRIBUTES (decl)))
10094 : : {
10095 : 52 : DECL_ATTRIBUTES (decl)
10096 : 52 : = remove_attribute ("omp declare target implicit",
10097 : 52 : DECL_ATTRIBUTES (decl));
10098 : 52 : complete_type (TREE_TYPE (decl));
10099 : 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
10100 : : {
10101 : 30 : auto_diagnostic_group d;
10102 : 30 : error ("%q+D in declare target directive does not have mappable"
10103 : : " type", decl);
10104 : 30 : if (TREE_TYPE (decl) != error_mark_node
10105 : 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10106 : 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
10107 : 30 : }
10108 : 22 : else if (!lookup_attribute ("omp declare target",
10109 : 22 : DECL_ATTRIBUTES (decl))
10110 : 44 : && !lookup_attribute ("omp declare target link",
10111 : 22 : DECL_ATTRIBUTES (decl)))
10112 : : {
10113 : 22 : DECL_ATTRIBUTES (decl)
10114 : 22 : = tree_cons (get_identifier ("omp declare target"),
10115 : 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
10116 : 22 : symtab_node *node = symtab_node::get (decl);
10117 : 22 : if (node != NULL)
10118 : : {
10119 : 19 : node->offloadable = 1;
10120 : 19 : if (ENABLE_OFFLOADING)
10121 : : {
10122 : : g->have_offload = true;
10123 : : if (is_a <varpool_node *> (node))
10124 : : vec_safe_push (offload_vars, decl);
10125 : : }
10126 : : }
10127 : : }
10128 : : }
10129 : :
10130 : : /* This is the last point we can lower alignment so give the target the
10131 : : chance to do so. */
10132 : 109458816 : if (VAR_P (decl)
10133 : 46839044 : && !is_global_var (decl)
10134 : 132880131 : && !DECL_HARD_REGISTER (decl))
10135 : 23421265 : targetm.lower_local_decl_alignment (decl);
10136 : :
10137 : 109458816 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
10138 : 283325505 : }
10139 : :
10140 : : /* For class TYPE return itself or some its bases that contain
10141 : : any direct non-static data members. Return error_mark_node if an
10142 : : error has been diagnosed. */
10143 : :
10144 : : static tree
10145 : 22929 : find_decomp_class_base (location_t loc, tree type, tree ret,
10146 : : tsubst_flags_t complain)
10147 : : {
10148 : 45858 : if (LAMBDA_TYPE_P (type))
10149 : : {
10150 : 12 : if (complain & tf_error)
10151 : : {
10152 : 12 : auto_diagnostic_group d;
10153 : 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
10154 : 12 : inform (location_of (type), "lambda declared here");
10155 : 12 : }
10156 : 12 : return error_mark_node;
10157 : : }
10158 : :
10159 : 22917 : bool member_seen = false;
10160 : 324289 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10161 : 556314 : if (TREE_CODE (field) != FIELD_DECL
10162 : 46638 : || DECL_ARTIFICIAL (field)
10163 : 347955 : || DECL_UNNAMED_BIT_FIELD (field))
10164 : 254903 : continue;
10165 : 46508 : else if (ret)
10166 : : return type;
10167 : 46482 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10168 : : {
10169 : 8 : if ((complain & tf_error) == 0)
10170 : 0 : return error_mark_node;
10171 : 8 : auto_diagnostic_group d;
10172 : 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10173 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10174 : : "anonymous struct member", type);
10175 : : else
10176 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10177 : : "anonymous union member", type);
10178 : 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
10179 : 8 : return error_mark_node;
10180 : 8 : }
10181 : 46474 : else if (!accessible_p (type, field, true))
10182 : : {
10183 : 5 : if ((complain & tf_error) == 0)
10184 : 0 : return error_mark_node;
10185 : 5 : auto_diagnostic_group d;
10186 : 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10187 : : field, type);
10188 : 5 : inform (DECL_SOURCE_LOCATION (field),
10189 : 5 : TREE_PRIVATE (field)
10190 : : ? G_("declared private here")
10191 : : : G_("declared protected here"));
10192 : 5 : return error_mark_node;
10193 : 5 : }
10194 : : else
10195 : : member_seen = true;
10196 : :
10197 : 22878 : tree base_binfo, binfo;
10198 : 22878 : tree orig_ret = ret;
10199 : 22878 : int i;
10200 : 22878 : if (member_seen)
10201 : 22789 : ret = type;
10202 : 22936 : for (binfo = TYPE_BINFO (type), i = 0;
10203 : 22936 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10204 : : {
10205 : 78 : auto_diagnostic_group d;
10206 : 78 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10207 : : complain);
10208 : 78 : if (t == error_mark_node)
10209 : : {
10210 : 3 : if (complain & tf_error)
10211 : 3 : inform (location_of (type), "in base class of %qT", type);
10212 : 3 : return error_mark_node;
10213 : : }
10214 : 75 : if (t != NULL_TREE && t != ret)
10215 : : {
10216 : 48 : if (ret == type)
10217 : : {
10218 : 10 : if (complain & tf_error)
10219 : 10 : error_at (loc, "cannot decompose class type %qT: both it and "
10220 : : "its base class %qT have non-static data "
10221 : : "members", type, t);
10222 : 10 : return error_mark_node;
10223 : : }
10224 : 38 : else if (orig_ret != NULL_TREE)
10225 : : return t;
10226 : 38 : else if (ret != NULL_TREE)
10227 : : {
10228 : 7 : if (complain & tf_error)
10229 : 7 : error_at (loc, "cannot decompose class type %qT: its base "
10230 : : "classes %qT and %qT have non-static data "
10231 : : "members", type, ret, t);
10232 : 7 : return error_mark_node;
10233 : : }
10234 : : else
10235 : : ret = t;
10236 : : }
10237 : 78 : }
10238 : : return ret;
10239 : : }
10240 : :
10241 : : /* Return std::tuple_size<TYPE>::value. */
10242 : :
10243 : : static tree
10244 : 64191 : get_tuple_size (tree type)
10245 : : {
10246 : 64191 : tree args = make_tree_vec (1);
10247 : 64191 : TREE_VEC_ELT (args, 0) = type;
10248 : 64191 : tree inst = lookup_template_class (tuple_size_identifier, args,
10249 : : /*in_decl*/NULL_TREE,
10250 : : /*context*/std_node,
10251 : : tf_none);
10252 : 64191 : inst = complete_type (inst);
10253 : 64191 : if (inst == error_mark_node
10254 : 62772 : || !COMPLETE_TYPE_P (inst)
10255 : 105489 : || !CLASS_TYPE_P (type))
10256 : : return NULL_TREE;
10257 : 41298 : tree val = lookup_qualified_name (inst, value_identifier,
10258 : : LOOK_want::NORMAL, /*complain*/false);
10259 : 41298 : if (val == error_mark_node)
10260 : : return NULL_TREE;
10261 : 41292 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10262 : 41292 : val = maybe_constant_value (val, NULL_TREE, mce_true);
10263 : 41292 : if (TREE_CODE (val) == INTEGER_CST)
10264 : : return val;
10265 : : else
10266 : 6 : return error_mark_node;
10267 : : }
10268 : :
10269 : : /* Return std::tuple_element<I,TYPE>::type. */
10270 : :
10271 : : static tree
10272 : 44835 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10273 : : {
10274 : 44835 : tree args = make_tree_vec (2);
10275 : 44835 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10276 : 44835 : TREE_VEC_ELT (args, 1) = type;
10277 : 44835 : tree inst = lookup_template_class (tuple_element_identifier, args,
10278 : : /*in_decl*/NULL_TREE,
10279 : : /*context*/std_node,
10280 : : tf_warning_or_error);
10281 : 44835 : return make_typename_type (inst, type_identifier,
10282 : 44835 : none_type, tf_warning_or_error);
10283 : : }
10284 : :
10285 : : /* Return e.get<i>() or get<i>(e). */
10286 : :
10287 : : static tree
10288 : 44844 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10289 : : {
10290 : 44844 : tree targs = make_tree_vec (1);
10291 : 44844 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10292 : :
10293 : 44844 : tree etype = TREE_TYPE (decl);
10294 : 44844 : tree e = convert_from_reference (decl);
10295 : :
10296 : : /* [The id-expression] e is an lvalue if the type of the entity e is an
10297 : : lvalue reference and an xvalue otherwise. */
10298 : 44844 : if (!TYPE_REF_P (etype)
10299 : 44844 : || TYPE_REF_IS_RVALUE (etype))
10300 : 43554 : e = move (e);
10301 : :
10302 : 44844 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10303 : : LOOK_want::NORMAL, /*complain*/false);
10304 : 44844 : bool use_member_get = false;
10305 : :
10306 : : /* To use a member get, member lookup must find at least one
10307 : : declaration that is a function template
10308 : : whose first template parameter is a non-type parameter. */
10309 : 132095 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10310 : : {
10311 : 44844 : tree fn = *iter;
10312 : 44844 : if (TREE_CODE (fn) == TEMPLATE_DECL)
10313 : : {
10314 : 2440 : tree tparms = DECL_TEMPLATE_PARMS (fn);
10315 : 2440 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10316 : 2440 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10317 : : {
10318 : : use_member_get = true;
10319 : : break;
10320 : : }
10321 : : }
10322 : : }
10323 : :
10324 : 44844 : if (use_member_get)
10325 : : {
10326 : 2437 : fns = lookup_template_function (fns, targs);
10327 : 2437 : return build_new_method_call (e, fns, /*args*/NULL,
10328 : : /*path*/NULL_TREE, LOOKUP_NORMAL,
10329 : 2437 : /*fn_p*/NULL, tf_warning_or_error);
10330 : : }
10331 : : else
10332 : : {
10333 : 42407 : releasing_vec args (make_tree_vector_single (e));
10334 : 42407 : fns = lookup_template_function (get__identifier, targs);
10335 : 42407 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10336 : 42407 : return finish_call_expr (fns, &args, /*novirt*/false,
10337 : : /*koenig*/true, tf_warning_or_error);
10338 : 42407 : }
10339 : : }
10340 : :
10341 : : /* It's impossible to recover the decltype of a tuple decomposition variable
10342 : : based on the actual type of the variable, so store it in a hash table. */
10343 : :
10344 : : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10345 : :
10346 : : tree
10347 : 546 : lookup_decomp_type (tree v)
10348 : : {
10349 : 546 : if (decomp_type_table)
10350 : 419 : if (tree *slot = decomp_type_table->get (v))
10351 : 355 : return *slot;
10352 : : return NULL_TREE;
10353 : : }
10354 : :
10355 : : /* Mangle a decomposition declaration if needed. Arguments like
10356 : : in cp_finish_decomp. */
10357 : :
10358 : : static void
10359 : 37607 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10360 : : {
10361 : 37607 : if (!processing_template_decl
10362 : 37607 : && !error_operand_p (decl)
10363 : 75214 : && TREE_STATIC (decl))
10364 : : {
10365 : 605 : auto_vec<tree, 16> v;
10366 : 605 : v.safe_grow (decomp->count, true);
10367 : 605 : tree d = decomp->decl;
10368 : 2196 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10369 : 1591 : v[decomp->count - i - 1] = d;
10370 : 605 : if (DECL_FUNCTION_SCOPE_P (decl))
10371 : : {
10372 : : size_t sz = 3;
10373 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
10374 : 650 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10375 : 259 : char *name = XALLOCAVEC (char, sz);
10376 : 259 : name[0] = 'D';
10377 : 259 : name[1] = 'C';
10378 : 259 : char *p = name + 2;
10379 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
10380 : : {
10381 : 650 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10382 : 650 : *p++ = ' ';
10383 : 650 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
10384 : 650 : p += len;
10385 : : }
10386 : 259 : *p = '\0';
10387 : 259 : determine_local_discriminator (decl, get_identifier (name));
10388 : : }
10389 : 605 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10390 : 605 : maybe_apply_pragma_weak (decl);
10391 : 605 : }
10392 : 37607 : }
10393 : :
10394 : : /* Append #i to DECL_NAME (decl) or for name independent decls
10395 : : clear DECL_NAME (decl). */
10396 : :
10397 : : static void
10398 : 2031 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10399 : : {
10400 : 2031 : if (name_independent_decl_p (decl))
10401 : : /* Only "_" names are treated as name independent, "_#0" etc. is not and
10402 : : because we pushdecl the individual decl elements of structured binding
10403 : : pack, we could get redeclaration errors if there are 2 or more name
10404 : : independent structured binding packs in the same scope. */
10405 : 312 : DECL_NAME (decl) = NULL_TREE;
10406 : : else
10407 : : {
10408 : 1719 : tree name = DECL_NAME (decl);
10409 : 1719 : size_t len = IDENTIFIER_LENGTH (name) + 22;
10410 : 1719 : char *n = XALLOCAVEC (char, len);
10411 : 1719 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10412 : 1719 : IDENTIFIER_POINTER (name), i);
10413 : 1719 : DECL_NAME (decl) = get_identifier (n);
10414 : : }
10415 : 2031 : }
10416 : :
10417 : : /* Return structured binding size of TYPE or -1 if erroneous. */
10418 : :
10419 : : HOST_WIDE_INT
10420 : 460 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10421 : : {
10422 : 460 : if (TYPE_REF_P (type))
10423 : : {
10424 : 0 : type = complete_type (TREE_TYPE (type));
10425 : 0 : if (type == error_mark_node)
10426 : : return -1;
10427 : 0 : if (!COMPLETE_TYPE_P (type))
10428 : : {
10429 : 0 : if (complain & tf_error)
10430 : 0 : error_at (loc, "structured binding refers to incomplete type %qT",
10431 : : type);
10432 : 0 : return -1;
10433 : : }
10434 : : }
10435 : :
10436 : 460 : unsigned HOST_WIDE_INT eltscnt = 0;
10437 : 460 : if (TREE_CODE (type) == ARRAY_TYPE)
10438 : : {
10439 : 258 : if (TYPE_DOMAIN (type) == NULL_TREE)
10440 : : {
10441 : 9 : if (complain & tf_error)
10442 : 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
10443 : : type);
10444 : 9 : return -1;
10445 : : }
10446 : 249 : tree nelts = array_type_nelts_top (type);
10447 : 249 : if (nelts == error_mark_node)
10448 : : return -1;
10449 : 249 : if (!tree_fits_shwi_p (nelts))
10450 : : {
10451 : 3 : if (complain & tf_error)
10452 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10453 : 3 : return -1;
10454 : : }
10455 : 246 : return tree_to_shwi (nelts);
10456 : : }
10457 : : /* 2 GNU extensions. */
10458 : 202 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10459 : : return 2;
10460 : 196 : else if (TREE_CODE (type) == VECTOR_TYPE)
10461 : : {
10462 : 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10463 : : {
10464 : : if (complain & tf_error)
10465 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10466 : : return -1;
10467 : : }
10468 : 12 : return eltscnt;
10469 : : }
10470 : 184 : else if (tree tsize = get_tuple_size (type))
10471 : : {
10472 : 28 : if (tsize == error_mark_node
10473 : 22 : || !tree_fits_shwi_p (tsize)
10474 : 50 : || tree_int_cst_sgn (tsize) < 0)
10475 : : {
10476 : 9 : if (complain & tf_error)
10477 : 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10478 : : "constant expression", type);
10479 : 9 : return -1;
10480 : : }
10481 : 19 : return tree_to_shwi (tsize);
10482 : : }
10483 : 156 : else if (TREE_CODE (type) == UNION_TYPE)
10484 : : {
10485 : 3 : if (complain & tf_error)
10486 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10487 : 3 : return -1;
10488 : : }
10489 : 153 : else if (!CLASS_TYPE_P (type))
10490 : : {
10491 : 21 : if (complain & tf_error)
10492 : 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10493 : 21 : return -1;
10494 : : }
10495 : 132 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10496 : : return -1;
10497 : 132 : else if (!COMPLETE_TYPE_P (type))
10498 : : {
10499 : 3 : if (complain & tf_error)
10500 : 3 : error_at (loc, "structured binding refers to incomplete class type "
10501 : : "%qT", type);
10502 : 3 : return -1;
10503 : : }
10504 : : else
10505 : : {
10506 : 129 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10507 : 129 : if (btype == error_mark_node)
10508 : : return -1;
10509 : 108 : else if (btype == NULL_TREE)
10510 : : return 0;
10511 : 1011 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10512 : 1533 : if (TREE_CODE (field) != FIELD_DECL
10513 : 327 : || DECL_ARTIFICIAL (field)
10514 : 1251 : || DECL_UNNAMED_BIT_FIELD (field))
10515 : 609 : continue;
10516 : : else
10517 : 315 : eltscnt++;
10518 : 87 : return eltscnt;
10519 : : }
10520 : : }
10521 : :
10522 : : /* Finish a decomposition declaration. DECL is the underlying declaration
10523 : : "e", FIRST is the head of a chain of decls for the individual identifiers
10524 : : chained through DECL_CHAIN in reverse order and COUNT is the number of
10525 : : those decls. If TEST_P is true, return true if any code would need to be
10526 : : actually emitted but don't emit it. Return false otherwise. */
10527 : :
10528 : : bool
10529 : 137831 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10530 : : {
10531 : 137831 : tree first = decomp->decl;
10532 : 137831 : unsigned count = decomp->count;
10533 : 137831 : if (error_operand_p (decl))
10534 : : {
10535 : 57 : error_out:
10536 : 583 : while (count--)
10537 : : {
10538 : 400 : TREE_TYPE (first) = error_mark_node;
10539 : 400 : if (DECL_HAS_VALUE_EXPR_P (first))
10540 : : {
10541 : 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10542 : 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10543 : : }
10544 : 400 : first = DECL_CHAIN (first);
10545 : : }
10546 : 183 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
10547 : 51 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10548 : 183 : return false;
10549 : : }
10550 : :
10551 : 137774 : location_t loc = DECL_SOURCE_LOCATION (decl);
10552 : 137774 : if (type_dependent_expression_p (decl)
10553 : : /* This happens for range for when not in templates.
10554 : : Still add the DECL_VALUE_EXPRs for later processing. */
10555 : 137774 : || (!processing_template_decl
10556 : 57120 : && type_uses_auto (TREE_TYPE (decl))))
10557 : : {
10558 : 218588 : for (unsigned int i = 0; i < count; i++)
10559 : : {
10560 : 145762 : if (!DECL_HAS_VALUE_EXPR_P (first))
10561 : : {
10562 : 145762 : tree v = build_nt (ARRAY_REF, decl,
10563 : 145762 : size_int (count - i - 1),
10564 : : NULL_TREE, NULL_TREE);
10565 : 145762 : SET_DECL_VALUE_EXPR (first, v);
10566 : 145762 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10567 : : }
10568 : 145762 : if (processing_template_decl)
10569 : 145142 : fit_decomposition_lang_decl (first, decl);
10570 : 145762 : first = DECL_CHAIN (first);
10571 : : }
10572 : : return false;
10573 : : }
10574 : :
10575 : 64948 : auto_vec<tree, 16> v;
10576 : 64948 : v.safe_grow (count, true);
10577 : 64948 : tree d = first;
10578 : 64948 : int pack = -1;
10579 : 196963 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10580 : : {
10581 : 132015 : v[count - i - 1] = d;
10582 : 132015 : fit_decomposition_lang_decl (d, decl);
10583 : 132015 : if (DECL_PACK_P (d))
10584 : 1008 : pack = count - i - 1;
10585 : : }
10586 : :
10587 : 64948 : tree type = TREE_TYPE (decl);
10588 : 64948 : tree dexp = decl;
10589 : :
10590 : 64948 : if (TYPE_REF_P (type))
10591 : : {
10592 : 1761 : dexp = convert_from_reference (dexp);
10593 : 1761 : type = complete_type (TREE_TYPE (type));
10594 : 1761 : if (type == error_mark_node)
10595 : 126 : goto error_out;
10596 : 1761 : if (!COMPLETE_TYPE_P (type))
10597 : : {
10598 : 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10599 : : type);
10600 : 3 : goto error_out;
10601 : : }
10602 : : }
10603 : :
10604 : 64945 : tree eltype = NULL_TREE;
10605 : 64945 : unsigned HOST_WIDE_INT eltscnt = 0;
10606 : : /* Structured binding packs when initializer is non-dependent should
10607 : : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10608 : : of that TREE_VEC are the base and index, what is normally represented
10609 : : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10610 : : of the pack first element. The remaining elements of the TREE_VEC
10611 : : are VAR_DECLs for the pack elements. */
10612 : 64945 : tree packv = NULL_TREE;
10613 : :
10614 : 64945 : if (TREE_CODE (type) == ARRAY_TYPE)
10615 : : {
10616 : 800 : tree nelts;
10617 : 800 : nelts = array_type_nelts_top (type);
10618 : 800 : if (nelts == error_mark_node)
10619 : 0 : goto error_out;
10620 : 800 : if (DECL_DECOMP_BASE (decl))
10621 : : {
10622 : 9 : error_at (loc, "array initializer for structured binding "
10623 : : "declaration in condition");
10624 : 9 : goto error_out;
10625 : : }
10626 : 791 : if (!tree_fits_uhwi_p (nelts))
10627 : : {
10628 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10629 : 3 : goto error_out;
10630 : : }
10631 : 788 : eltscnt = tree_to_uhwi (nelts);
10632 : 788 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10633 : : {
10634 : 11 : cnt_mismatch:
10635 : 38 : auto_diagnostic_group d;
10636 : 38 : if (count > eltscnt)
10637 : 27 : error_n (loc, count,
10638 : : "%u name provided for structured binding",
10639 : : "%u names provided for structured binding", count);
10640 : : else
10641 : 11 : error_n (loc, count,
10642 : : "only %u name provided for structured binding",
10643 : : "only %u names provided for structured binding", count);
10644 : 38 : inform_n (loc, eltscnt,
10645 : : "while %qT decomposes into %wu element",
10646 : : "while %qT decomposes into %wu elements",
10647 : : type, eltscnt);
10648 : 38 : goto error_out;
10649 : : }
10650 : 777 : eltype = TREE_TYPE (type);
10651 : 2796 : for (unsigned int i = 0; i < count; i++)
10652 : : {
10653 : 2019 : if ((unsigned) pack == i)
10654 : : {
10655 : 155 : packv = make_tree_vec (eltscnt - count + 3);
10656 : 413 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10657 : : {
10658 : 258 : tree t;
10659 : 258 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10660 : 258 : set_sb_pack_name (t, j);
10661 : 258 : maybe_push_decl (t);
10662 : 258 : TREE_TYPE (t) = eltype;
10663 : 258 : layout_decl (t, 0);
10664 : 258 : if (!processing_template_decl)
10665 : : {
10666 : 182 : tree a = unshare_expr (dexp);
10667 : 182 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10668 : : NULL_TREE, NULL_TREE);
10669 : 182 : SET_DECL_VALUE_EXPR (t, a);
10670 : 182 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10671 : : }
10672 : : }
10673 : 2019 : continue;
10674 : 155 : }
10675 : 1864 : TREE_TYPE (v[i]) = eltype;
10676 : 1864 : layout_decl (v[i], 0);
10677 : 1864 : if (processing_template_decl)
10678 : 132 : continue;
10679 : 1732 : tree t = unshare_expr (dexp);
10680 : 1732 : unsigned HOST_WIDE_INT j = i;
10681 : 1732 : if (pack != -1 && (unsigned) pack < i)
10682 : 64 : j = i + eltscnt - count;
10683 : 1732 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10684 : 1732 : SET_DECL_VALUE_EXPR (v[i], t);
10685 : 1732 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10686 : : }
10687 : : }
10688 : : /* 2 GNU extensions. */
10689 : 64145 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10690 : : {
10691 : 78 : eltscnt = 2;
10692 : 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10693 : 3 : goto cnt_mismatch;
10694 : 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10695 : 213 : for (unsigned int i = 0; i < count; i++)
10696 : : {
10697 : 138 : if ((unsigned) pack == i)
10698 : : {
10699 : 60 : packv = make_tree_vec (eltscnt - count + 3);
10700 : 132 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10701 : : {
10702 : 72 : tree t;
10703 : 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10704 : 72 : set_sb_pack_name (t, j);
10705 : 72 : maybe_push_decl (t);
10706 : 72 : TREE_TYPE (t) = eltype;
10707 : 72 : layout_decl (t, 0);
10708 : 72 : if (!processing_template_decl)
10709 : : {
10710 : 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
10711 : : unshare_expr (dexp));
10712 : 48 : SET_DECL_VALUE_EXPR (t, a);
10713 : 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10714 : : }
10715 : : }
10716 : 60 : continue;
10717 : 60 : }
10718 : 78 : TREE_TYPE (v[i]) = eltype;
10719 : 78 : layout_decl (v[i], 0);
10720 : 78 : if (processing_template_decl)
10721 : 16 : continue;
10722 : 62 : tree t = unshare_expr (dexp);
10723 : 62 : unsigned HOST_WIDE_INT j = i;
10724 : 62 : if (pack != -1 && (unsigned) pack < i)
10725 : 16 : j = i + eltscnt - count;
10726 : 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
10727 : 62 : SET_DECL_VALUE_EXPR (v[i], t);
10728 : 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10729 : : }
10730 : : }
10731 : 64067 : else if (TREE_CODE (type) == VECTOR_TYPE)
10732 : : {
10733 : 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10734 : : {
10735 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10736 : : goto error_out;
10737 : : }
10738 : 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10739 : 3 : goto cnt_mismatch;
10740 : 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10741 : 249 : for (unsigned int i = 0; i < count; i++)
10742 : : {
10743 : 192 : if ((unsigned) pack == i)
10744 : : {
10745 : 48 : packv = make_tree_vec (eltscnt - count + 3);
10746 : 708 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10747 : : {
10748 : 660 : tree t;
10749 : 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10750 : 660 : set_sb_pack_name (t, j);
10751 : 660 : maybe_push_decl (t);
10752 : 660 : TREE_TYPE (t) = eltype;
10753 : 660 : layout_decl (t, 0);
10754 : 660 : if (!processing_template_decl)
10755 : : {
10756 : 440 : tree a = unshare_expr (dexp);
10757 : 440 : location_t loc = DECL_SOURCE_LOCATION (t);
10758 : 440 : tree s = size_int (j + pack);
10759 : 440 : convert_vector_to_array_for_subscript (loc, &a, s);
10760 : 440 : a = build4 (ARRAY_REF, eltype, a, s,
10761 : : NULL_TREE, NULL_TREE);
10762 : 440 : SET_DECL_VALUE_EXPR (t, a);
10763 : 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10764 : : }
10765 : : }
10766 : 84 : continue;
10767 : 48 : }
10768 : 144 : TREE_TYPE (v[i]) = eltype;
10769 : 144 : layout_decl (v[i], 0);
10770 : 144 : if (processing_template_decl)
10771 : 36 : continue;
10772 : 108 : tree t = unshare_expr (dexp);
10773 : 108 : unsigned HOST_WIDE_INT j = i;
10774 : 108 : if (pack != -1 && (unsigned) pack < i)
10775 : 24 : j = i + eltscnt - count;
10776 : 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10777 : 108 : &t, size_int (j));
10778 : 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10779 : 108 : SET_DECL_VALUE_EXPR (v[i], t);
10780 : 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10781 : : }
10782 : : }
10783 : 64007 : else if (tree tsize = get_tuple_size (type))
10784 : : {
10785 : 41264 : if (tsize == error_mark_node)
10786 : : {
10787 : 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10788 : : "constant expression", type);
10789 : 0 : goto error_out;
10790 : : }
10791 : 41264 : if (!tree_fits_uhwi_p (tsize))
10792 : : {
10793 : 12 : auto_diagnostic_group d;
10794 : 12 : error_n (loc, count,
10795 : : "%u name provided for structured binding",
10796 : : "%u names provided for structured binding", count);
10797 : 12 : inform (loc, "while %qT decomposes into %E elements",
10798 : : type, tsize);
10799 : 12 : goto error_out;
10800 : 12 : }
10801 : 41252 : eltscnt = tree_to_uhwi (tsize);
10802 : 41252 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10803 : 6 : goto cnt_mismatch;
10804 : 41246 : if (test_p && eltscnt)
10805 : 64822 : return true;
10806 : 22008 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
10807 : : {
10808 : : /* For structured bindings used in conditions we need to evaluate
10809 : : the conversion of decl (aka e in the standard) to bool or
10810 : : integral/enumeral type (the latter for switch conditions)
10811 : : before the get methods. */
10812 : 86 : tree cond = convert_from_reference (decl);
10813 : 86 : if (integer_onep (DECL_DECOMP_BASE (decl)))
10814 : : /* switch condition. */
10815 : 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10816 : : cond, true);
10817 : : else
10818 : : /* if/while/for condition. */
10819 : 68 : cond = contextual_conv_bool (cond, tf_warning_or_error);
10820 : 86 : if (cond && !error_operand_p (cond))
10821 : : {
10822 : : /* Wrap that value into a TARGET_EXPR, emit it right
10823 : : away and save for later uses in the cp_parse_condition
10824 : : or its instantiation. */
10825 : 86 : cond = get_internal_target_expr (cond);
10826 : 86 : add_stmt (cond);
10827 : 86 : DECL_DECOMP_BASE (decl) = cond;
10828 : : }
10829 : : }
10830 : 22008 : int save_read = DECL_READ_P (decl);
10831 : 66551 : for (unsigned i = 0; i < count; ++i)
10832 : : {
10833 : 44564 : location_t sloc = input_location;
10834 : 44564 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10835 : :
10836 : 44564 : if ((unsigned) pack == i)
10837 : : {
10838 : 276 : packv = make_tree_vec (eltscnt - count + 3);
10839 : 832 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10840 : : {
10841 : 556 : tree t;
10842 : 556 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10843 : 556 : set_sb_pack_name (t, j);
10844 : 556 : input_location = dloc;
10845 : 556 : tree init = get_tuple_decomp_init (decl, j + pack);
10846 : 556 : tree eltype = (init == error_mark_node ? error_mark_node
10847 : 556 : : get_tuple_element_type (type, j + pack));
10848 : 556 : input_location = sloc;
10849 : :
10850 : 556 : if (VOID_TYPE_P (eltype))
10851 : : {
10852 : 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
10853 : : "%<void%>", j + pack, type);
10854 : 0 : eltype = error_mark_node;
10855 : : }
10856 : 556 : if (init == error_mark_node || eltype == error_mark_node)
10857 : : {
10858 : 0 : inform (dloc, "in initialization of structured binding "
10859 : 0 : "pack %qD", v[pack]);
10860 : 0 : goto error_out;
10861 : : }
10862 : 556 : maybe_push_decl (t);
10863 : : /* Save the decltype away before reference collapse. */
10864 : 556 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
10865 : 556 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10866 : 556 : TREE_TYPE (t) = eltype;
10867 : 556 : layout_decl (t, 0);
10868 : 556 : DECL_HAS_VALUE_EXPR_P (t) = 0;
10869 : 556 : if (!processing_template_decl)
10870 : : {
10871 : 456 : copy_linkage (t, decl);
10872 : 456 : tree name = DECL_NAME (t);
10873 : 456 : if (TREE_STATIC (decl))
10874 : 36 : DECL_NAME (t) = DECL_NAME (v[pack]);
10875 : 456 : cp_finish_decl (t, init, /*constexpr*/false,
10876 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10877 : 456 : if (TREE_STATIC (decl))
10878 : : {
10879 : 36 : DECL_ASSEMBLER_NAME (t);
10880 : 36 : DECL_NAME (t) = name;
10881 : : }
10882 : : }
10883 : : }
10884 : 276 : continue;
10885 : 276 : }
10886 : :
10887 : 44288 : unsigned HOST_WIDE_INT j = i;
10888 : 44288 : if (pack != -1 && (unsigned) pack < i)
10889 : 238 : j = i + eltscnt - count;
10890 : 44288 : input_location = dloc;
10891 : 44288 : tree init = get_tuple_decomp_init (decl, j);
10892 : 44288 : tree eltype = (init == error_mark_node ? error_mark_node
10893 : 44279 : : get_tuple_element_type (type, j));
10894 : 44288 : input_location = sloc;
10895 : :
10896 : 44288 : if (VOID_TYPE_P (eltype))
10897 : : {
10898 : 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
10899 : : j, type);
10900 : 3 : eltype = error_mark_node;
10901 : : }
10902 : 44288 : if (init == error_mark_node || eltype == error_mark_node)
10903 : : {
10904 : 21 : inform (dloc, "in initialization of structured binding "
10905 : 21 : "variable %qD", v[i]);
10906 : 21 : goto error_out;
10907 : : }
10908 : : /* Save the decltype away before reference collapse. */
10909 : 44267 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
10910 : 44267 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10911 : 44267 : TREE_TYPE (v[i]) = eltype;
10912 : 44267 : layout_decl (v[i], 0);
10913 : 44267 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
10914 : : {
10915 : : /* In this case the names are variables, not just proxies. */
10916 : 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10917 : 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10918 : : }
10919 : 44267 : if (!processing_template_decl)
10920 : : {
10921 : 38703 : copy_linkage (v[i], decl);
10922 : 38703 : cp_finish_decl (v[i], init, /*constexpr*/false,
10923 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10924 : : }
10925 : : }
10926 : : /* Ignore reads from the underlying decl performed during initialization
10927 : : of the individual variables. If those will be read, we'll mark
10928 : : the underlying decl as read at that point. */
10929 : 21987 : DECL_READ_P (decl) = save_read;
10930 : : }
10931 : 22743 : else if (TREE_CODE (type) == UNION_TYPE)
10932 : : {
10933 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10934 : 3 : goto error_out;
10935 : : }
10936 : 22740 : else if (!CLASS_TYPE_P (type))
10937 : : {
10938 : 16 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10939 : 16 : goto error_out;
10940 : : }
10941 : 22724 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10942 : 0 : goto error_out;
10943 : 22724 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10944 : 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10945 : : type);
10946 : : else
10947 : : {
10948 : 22722 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
10949 : : tf_warning_or_error);
10950 : 22722 : if (btype == error_mark_node)
10951 : 21 : goto error_out;
10952 : 22701 : else if (btype == NULL_TREE)
10953 : : {
10954 : 16 : if (pack == 0 && count == 1)
10955 : : {
10956 : 16 : eltscnt = 0;
10957 : 16 : packv = make_tree_vec (2);
10958 : 16 : goto done;
10959 : : }
10960 : 0 : error_at (loc, "cannot decompose class type %qT without non-static "
10961 : : "data members", type);
10962 : 0 : goto error_out;
10963 : : }
10964 : 321755 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10965 : 552006 : if (TREE_CODE (field) != FIELD_DECL
10966 : 46150 : || DECL_ARTIFICIAL (field)
10967 : 345216 : || DECL_UNNAMED_BIT_FIELD (field))
10968 : 252936 : continue;
10969 : : else
10970 : 46134 : eltscnt++;
10971 : 22685 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10972 : 15 : goto cnt_mismatch;
10973 : 22670 : tree t = dexp;
10974 : 22670 : if (type != btype)
10975 : : {
10976 : 18 : t = convert_to_base (t, btype, /*check_access*/true,
10977 : : /*nonnull*/false, tf_warning_or_error);
10978 : 18 : type = btype;
10979 : : }
10980 : 22670 : unsigned HOST_WIDE_INT j = 0;
10981 : 22670 : unsigned int i = 0;
10982 : 321544 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10983 : 551651 : if (TREE_CODE (field) != FIELD_DECL
10984 : 46113 : || DECL_ARTIFICIAL (field)
10985 : 344983 : || DECL_UNNAMED_BIT_FIELD (field))
10986 : 252777 : continue;
10987 : : else
10988 : : {
10989 : 46097 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
10990 : : NULL_TREE);
10991 : 46097 : if (REFERENCE_REF_P (tt))
10992 : 30 : tt = TREE_OPERAND (tt, 0);
10993 : 46097 : if (pack != -1 && j >= (unsigned) pack)
10994 : : {
10995 : 666 : if (j == (unsigned) pack)
10996 : : {
10997 : 220 : packv = make_tree_vec (eltscnt - count + 3);
10998 : 220 : i++;
10999 : : }
11000 : 666 : if (j < (unsigned) pack + eltscnt - (count - 1))
11001 : : {
11002 : 485 : tree t;
11003 : 485 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11004 : 485 : set_sb_pack_name (t, j + 1 - i);
11005 : 485 : maybe_push_decl (t);
11006 : 485 : TREE_TYPE (t) = TREE_TYPE (tt);
11007 : 485 : layout_decl (t, 0);
11008 : 485 : if (!processing_template_decl)
11009 : : {
11010 : 364 : SET_DECL_VALUE_EXPR (t, tt);
11011 : 364 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11012 : : }
11013 : : else
11014 : 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11015 : 485 : j++;
11016 : 485 : continue;
11017 : 485 : }
11018 : : }
11019 : 45612 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
11020 : 45612 : layout_decl (v[i], 0);
11021 : 45612 : if (!processing_template_decl)
11022 : : {
11023 : 35265 : SET_DECL_VALUE_EXPR (v[i], tt);
11024 : 35265 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11025 : : }
11026 : 45612 : i++;
11027 : 45612 : j++;
11028 : : }
11029 : 22670 : if (pack != -1 && j == (unsigned) pack)
11030 : : {
11031 : 20 : gcc_checking_assert (eltscnt == count - 1);
11032 : 20 : packv = make_tree_vec (2);
11033 : : }
11034 : : }
11035 : 45584 : done:
11036 : 45584 : if (packv)
11037 : : {
11038 : 795 : gcc_checking_assert (pack != -1);
11039 : 795 : TREE_VEC_ELT (packv, 0) = decl;
11040 : 795 : TREE_VEC_ELT (packv, 1) = size_int (pack);
11041 : 795 : SET_DECL_VALUE_EXPR (v[pack], packv);
11042 : 795 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11043 : 795 : DECL_IGNORED_P (v[pack]) = 1;
11044 : 795 : if (!processing_template_decl)
11045 : 2047 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11046 : 1490 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
11047 : : }
11048 : 45584 : if (processing_template_decl)
11049 : : {
11050 : 24401 : for (unsigned int i = 0; i < count; i++)
11051 : 16335 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11052 : : {
11053 : 15941 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
11054 : : NULL_TREE, NULL_TREE);
11055 : 15941 : SET_DECL_VALUE_EXPR (v[i], a);
11056 : 15941 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11057 : : }
11058 : : }
11059 : : return false;
11060 : : }
11061 : :
11062 : : /* Returns a declaration for a VAR_DECL as if:
11063 : :
11064 : : extern "C" TYPE NAME;
11065 : :
11066 : : had been seen. Used to create compiler-generated global
11067 : : variables. */
11068 : :
11069 : : static tree
11070 : 2154 : declare_global_var (tree name, tree type)
11071 : : {
11072 : 2154 : auto cookie = push_abi_namespace (global_namespace);
11073 : 2154 : tree decl = build_decl (input_location, VAR_DECL, name, type);
11074 : 2154 : TREE_PUBLIC (decl) = 1;
11075 : 2154 : DECL_EXTERNAL (decl) = 1;
11076 : 2154 : DECL_ARTIFICIAL (decl) = 1;
11077 : 2154 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11078 : : /* If the user has explicitly declared this variable (perhaps
11079 : : because the code we are compiling is part of a low-level runtime
11080 : : library), then it is possible that our declaration will be merged
11081 : : with theirs by pushdecl. */
11082 : 2154 : decl = pushdecl (decl);
11083 : 2154 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
11084 : 2154 : pop_abi_namespace (cookie, global_namespace);
11085 : :
11086 : 2154 : return decl;
11087 : : }
11088 : :
11089 : : /* Returns the type for the argument to "atexit" corresponding to the function
11090 : : to be called when the program exits. */
11091 : :
11092 : : static tree
11093 : 24 : get_atexit_fn_ptr_type ()
11094 : : {
11095 : 24 : if (!atexit_fn_ptr_type_node)
11096 : : {
11097 : 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11098 : 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11099 : : }
11100 : :
11101 : 24 : return atexit_fn_ptr_type_node;
11102 : : }
11103 : :
11104 : : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11105 : : or "__cxa_throw" corresponding to the destructor to be called when the
11106 : : program exits. */
11107 : :
11108 : : tree
11109 : 18505 : get_cxa_atexit_fn_ptr_type ()
11110 : : {
11111 : 18505 : if (!cleanup_type)
11112 : : {
11113 : 13883 : tree fntype = build_function_type_list (void_type_node,
11114 : : ptr_type_node, NULL_TREE);
11115 : 13883 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11116 : 13883 : cleanup_type = build_pointer_type (fntype);
11117 : : }
11118 : :
11119 : 18505 : return cleanup_type;
11120 : : }
11121 : :
11122 : : /* Returns a pointer to the `atexit' function. Note that if
11123 : : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11124 : : `__cxa_atexit' function specified in the IA64 C++ ABI. */
11125 : :
11126 : : static tree
11127 : 3418 : get_atexit_node (void)
11128 : : {
11129 : 3418 : tree atexit_fndecl;
11130 : 3418 : tree fn_type;
11131 : 3418 : tree fn_ptr_type;
11132 : 3418 : const char *name;
11133 : 3418 : bool use_aeabi_atexit;
11134 : 3418 : tree ctx = global_namespace;
11135 : :
11136 : 3418 : if (atexit_node)
11137 : : return atexit_node;
11138 : :
11139 : 2120 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11140 : : {
11141 : : /* The declaration for `__cxa_atexit' is:
11142 : :
11143 : : int __cxa_atexit (void (*)(void *), void *, void *)
11144 : :
11145 : : We build up the argument types and then the function type
11146 : : itself. */
11147 : 2108 : tree argtype0, argtype1, argtype2;
11148 : :
11149 : 2108 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11150 : : /* First, build the pointer-to-function type for the first
11151 : : argument. */
11152 : 2108 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11153 : : /* Then, build the rest of the argument types. */
11154 : 2108 : argtype2 = ptr_type_node;
11155 : 2108 : if (use_aeabi_atexit)
11156 : : {
11157 : : argtype1 = fn_ptr_type;
11158 : : argtype0 = ptr_type_node;
11159 : : }
11160 : : else
11161 : : {
11162 : 2108 : argtype1 = ptr_type_node;
11163 : 2108 : argtype0 = fn_ptr_type;
11164 : : }
11165 : : /* And the final __cxa_atexit type. */
11166 : 2108 : fn_type = build_function_type_list (integer_type_node,
11167 : : argtype0, argtype1, argtype2,
11168 : : NULL_TREE);
11169 : : /* ... which needs noexcept. */
11170 : 2108 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11171 : 2108 : if (use_aeabi_atexit)
11172 : : {
11173 : 0 : name = "__aeabi_atexit";
11174 : 0 : push_to_top_level ();
11175 : 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
11176 : 0 : ctx = current_namespace;
11177 : 0 : while (n--)
11178 : 0 : pop_namespace ();
11179 : 0 : pop_from_top_level ();
11180 : : }
11181 : : else
11182 : : {
11183 : 2108 : name = "__cxa_atexit";
11184 : 2108 : ctx = abi_node;
11185 : : }
11186 : : }
11187 : : else
11188 : : {
11189 : : /* The declaration for `atexit' is:
11190 : :
11191 : : int atexit (void (*)());
11192 : :
11193 : : We build up the argument types and then the function type
11194 : : itself. */
11195 : 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
11196 : : /* Build the final atexit type. */
11197 : 12 : fn_type = build_function_type_list (integer_type_node,
11198 : : fn_ptr_type, NULL_TREE);
11199 : : /* ... which needs noexcept. */
11200 : 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11201 : 12 : name = "atexit";
11202 : : }
11203 : :
11204 : : /* Now, build the function declaration. */
11205 : 2120 : push_lang_context (lang_name_c);
11206 : 2120 : auto cookie = push_abi_namespace (ctx);
11207 : 2120 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
11208 : 2120 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11209 : : /* Install as hidden builtin so we're (a) more relaxed about
11210 : : exception spec matching and (b) will not give a confusing location
11211 : : in diagnostic and (c) won't magically appear in user-visible name
11212 : : lookups. */
11213 : 2120 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11214 : 2120 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11215 : 2120 : pop_abi_namespace (cookie, ctx);
11216 : 2120 : mark_used (atexit_fndecl);
11217 : 2120 : pop_lang_context ();
11218 : 2120 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11219 : :
11220 : 2120 : return atexit_node;
11221 : : }
11222 : :
11223 : : /* Like get_atexit_node, but for thread-local cleanups. */
11224 : :
11225 : : static tree
11226 : 86 : get_thread_atexit_node (void)
11227 : : {
11228 : 86 : if (thread_atexit_node)
11229 : : return thread_atexit_node;
11230 : :
11231 : : /* The declaration for `__cxa_thread_atexit' is:
11232 : :
11233 : : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11234 : 56 : tree fn_type = build_function_type_list (integer_type_node,
11235 : : get_cxa_atexit_fn_ptr_type (),
11236 : : ptr_type_node, ptr_type_node,
11237 : : NULL_TREE);
11238 : :
11239 : : /* Now, build the function declaration, as with __cxa_atexit. */
11240 : 56 : unsigned flags = push_abi_namespace ();
11241 : 56 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
11242 : : ECF_LEAF | ECF_NOTHROW);
11243 : 56 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11244 : 56 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11245 : 56 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11246 : 56 : pop_abi_namespace (flags);
11247 : 56 : mark_used (atexit_fndecl);
11248 : 56 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11249 : :
11250 : 56 : return thread_atexit_node;
11251 : : }
11252 : :
11253 : : /* Returns the __dso_handle VAR_DECL. */
11254 : :
11255 : : static tree
11256 : 3492 : get_dso_handle_node (void)
11257 : : {
11258 : 3492 : if (dso_handle_node)
11259 : : return dso_handle_node;
11260 : :
11261 : : /* Declare the variable. */
11262 : 2154 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11263 : : ptr_type_node);
11264 : :
11265 : : #ifdef HAVE_GAS_HIDDEN
11266 : 2154 : if (dso_handle_node != error_mark_node)
11267 : : {
11268 : 2151 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11269 : 2151 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11270 : : }
11271 : : #endif
11272 : :
11273 : 2154 : return dso_handle_node;
11274 : : }
11275 : :
11276 : : /* Begin a new function with internal linkage whose job will be simply
11277 : : to destroy some particular DECL. OB_PARM is true if object pointer
11278 : : is passed to the cleanup function, otherwise no argument is passed. */
11279 : :
11280 : : static tree
11281 : 367 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11282 : : {
11283 : 367 : push_to_top_level ();
11284 : :
11285 : : /* No need to mangle this. */
11286 : 367 : push_lang_context (lang_name_c);
11287 : :
11288 : : /* Build the name of the function. */
11289 : 367 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11290 : 367 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11291 : 367 : dname = targetm.strip_name_encoding (dname);
11292 : 731 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11293 : :
11294 : 367 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11295 : : : get_atexit_fn_ptr_type ());
11296 : : /* Build the function declaration. */
11297 : 367 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11298 : 367 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11299 : : /* It's a function with internal linkage, generated by the
11300 : : compiler. */
11301 : 367 : TREE_PUBLIC (fndecl) = 0;
11302 : 367 : DECL_ARTIFICIAL (fndecl) = 1;
11303 : : /* Make the function `inline' so that it is only emitted if it is
11304 : : actually needed. It is unlikely that it will be inlined, since
11305 : : it is only called via a function pointer, but we avoid unnecessary
11306 : : emissions this way. */
11307 : 367 : DECL_DECLARED_INLINE_P (fndecl) = 1;
11308 : 367 : DECL_INTERFACE_KNOWN (fndecl) = 1;
11309 : 367 : if (ob_parm)
11310 : : {
11311 : : /* Build the parameter. */
11312 : 355 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11313 : 355 : TREE_USED (parmdecl) = 1;
11314 : 355 : DECL_READ_P (parmdecl) = 1;
11315 : 355 : DECL_ARGUMENTS (fndecl) = parmdecl;
11316 : : }
11317 : :
11318 : 367 : fndecl = pushdecl (fndecl, /*hidden=*/true);
11319 : 367 : if (omp_target)
11320 : : {
11321 : 3 : DECL_ATTRIBUTES (fndecl)
11322 : 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11323 : 3 : DECL_ATTRIBUTES (fndecl));
11324 : 3 : DECL_ATTRIBUTES (fndecl)
11325 : 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11326 : 3 : DECL_ATTRIBUTES (fndecl));
11327 : : }
11328 : 367 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11329 : :
11330 : 367 : pop_lang_context ();
11331 : :
11332 : 367 : return current_function_decl;
11333 : : }
11334 : :
11335 : : /* Finish the cleanup function begun by start_cleanup_fn. */
11336 : :
11337 : : static void
11338 : 367 : end_cleanup_fn (void)
11339 : : {
11340 : 367 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
11341 : :
11342 : 367 : pop_from_top_level ();
11343 : 367 : }
11344 : :
11345 : : /* Generate code to handle the destruction of DECL, an object with
11346 : : static storage duration. */
11347 : :
11348 : : tree
11349 : 14902 : register_dtor_fn (tree decl, bool omp_target)
11350 : : {
11351 : 14902 : tree cleanup;
11352 : 14902 : tree addr;
11353 : 14902 : tree compound_stmt;
11354 : 14902 : tree fcall;
11355 : 14902 : tree type;
11356 : 14902 : bool ob_parm, dso_parm, use_dtor;
11357 : 14902 : tree arg0, arg1, arg2;
11358 : 14902 : tree atex_node;
11359 : :
11360 : 14902 : type = TREE_TYPE (decl);
11361 : 14902 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11362 : 11398 : return void_node;
11363 : :
11364 : 3504 : if (decl_maybe_constant_destruction (decl, type)
11365 : 3504 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11366 : : {
11367 : 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11368 : 0 : return void_node;
11369 : : }
11370 : :
11371 : : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11372 : : "__aeabi_atexit"), and DECL is a class object, we can just pass the
11373 : : destructor to "__cxa_atexit"; we don't have to build a temporary
11374 : : function to do the cleanup. */
11375 : 7008 : dso_parm = (flag_use_cxa_atexit
11376 : 3504 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
11377 : 3504 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11378 : 3492 : use_dtor = ob_parm && CLASS_TYPE_P (type);
11379 : 3504 : if (use_dtor)
11380 : : {
11381 : 3137 : cleanup = get_class_binding (type, complete_dtor_identifier);
11382 : :
11383 : : /* Make sure it is accessible. */
11384 : 3137 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11385 : : tf_warning_or_error);
11386 : : }
11387 : : else
11388 : : {
11389 : : /* Call build_cleanup before we enter the anonymous function so
11390 : : that any access checks will be done relative to the current
11391 : : scope, rather than the scope of the anonymous function. */
11392 : 367 : build_cleanup (decl);
11393 : :
11394 : : /* Now start the function. */
11395 : 367 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11396 : :
11397 : : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11398 : : to the original function, rather than the anonymous one. That
11399 : : will make the back end think that nested functions are in use,
11400 : : which causes confusion. */
11401 : 367 : push_deferring_access_checks (dk_no_check);
11402 : 367 : fcall = build_cleanup (decl);
11403 : 367 : pop_deferring_access_checks ();
11404 : :
11405 : : /* Create the body of the anonymous function. */
11406 : 367 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11407 : 367 : finish_expr_stmt (fcall);
11408 : 367 : finish_compound_stmt (compound_stmt);
11409 : 367 : end_cleanup_fn ();
11410 : : }
11411 : :
11412 : : /* Call atexit with the cleanup function. */
11413 : 3504 : mark_used (cleanup);
11414 : 3504 : cleanup = build_address (cleanup);
11415 : :
11416 : 3504 : if (CP_DECL_THREAD_LOCAL_P (decl))
11417 : 86 : atex_node = get_thread_atexit_node ();
11418 : : else
11419 : 3418 : atex_node = get_atexit_node ();
11420 : :
11421 : 3504 : if (use_dtor)
11422 : : {
11423 : : /* We must convert CLEANUP to the type that "__cxa_atexit"
11424 : : expects. */
11425 : 3137 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11426 : : /* "__cxa_atexit" will pass the address of DECL to the
11427 : : cleanup function. */
11428 : 3137 : mark_used (decl);
11429 : 3137 : addr = build_address (decl);
11430 : : /* The declared type of the parameter to "__cxa_atexit" is
11431 : : "void *". For plain "T*", we could just let the
11432 : : machinery in cp_build_function_call convert it -- but if the
11433 : : type is "cv-qualified T *", then we need to convert it
11434 : : before passing it in, to avoid spurious errors. */
11435 : 3137 : addr = build_nop (ptr_type_node, addr);
11436 : : }
11437 : : else
11438 : : /* Since the cleanup functions we build ignore the address
11439 : : they're given, there's no reason to pass the actual address
11440 : : in, and, in general, it's cheaper to pass NULL than any
11441 : : other value. */
11442 : 367 : addr = null_pointer_node;
11443 : :
11444 : 3504 : if (dso_parm)
11445 : 3492 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
11446 : : tf_warning_or_error);
11447 : 12 : else if (ob_parm)
11448 : : /* Just pass NULL to the dso handle parm if we don't actually
11449 : : have a DSO handle on this target. */
11450 : 0 : arg2 = null_pointer_node;
11451 : : else
11452 : : arg2 = NULL_TREE;
11453 : :
11454 : 3492 : if (ob_parm)
11455 : : {
11456 : 3492 : if (!CP_DECL_THREAD_LOCAL_P (decl)
11457 : 3492 : && targetm.cxx.use_aeabi_atexit ())
11458 : : {
11459 : : arg1 = cleanup;
11460 : : arg0 = addr;
11461 : : }
11462 : : else
11463 : : {
11464 : : arg1 = addr;
11465 : : arg0 = cleanup;
11466 : : }
11467 : : }
11468 : : else
11469 : : {
11470 : : arg0 = cleanup;
11471 : : arg1 = NULL_TREE;
11472 : : }
11473 : 3504 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11474 : 3504 : arg0, arg1, arg2, NULL_TREE);
11475 : : }
11476 : :
11477 : : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
11478 : : is its initializer. Generate code to handle the construction
11479 : : and destruction of DECL. */
11480 : :
11481 : : static void
11482 : 16925880 : expand_static_init (tree decl, tree init)
11483 : : {
11484 : 16925880 : gcc_assert (VAR_P (decl));
11485 : 16925880 : gcc_assert (TREE_STATIC (decl));
11486 : :
11487 : : /* Some variables require no dynamic initialization. */
11488 : 16925880 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11489 : : {
11490 : : /* Make sure the destructor is callable. */
11491 : 16922462 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11492 : 16922462 : if (!init)
11493 : : return;
11494 : : }
11495 : :
11496 : 15682 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11497 : 14762 : && !DECL_FUNCTION_SCOPE_P (decl))
11498 : : {
11499 : 15 : auto_diagnostic_group d;
11500 : 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11501 : 15 : if (init)
11502 : 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11503 : : "needs dynamic initialization", decl);
11504 : : else
11505 : 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11506 : : "has a non-trivial destructor", decl);
11507 : 15 : static bool informed;
11508 : 15 : if (!informed)
11509 : : {
11510 : 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11511 : : "initialization and destruction");
11512 : 6 : informed = true;
11513 : : }
11514 : 15 : return;
11515 : 15 : }
11516 : :
11517 : 14718 : if (DECL_FUNCTION_SCOPE_P (decl))
11518 : : {
11519 : : /* Emit code to perform this initialization but once. */
11520 : 3798 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11521 : 3798 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11522 : 3798 : tree guard, guard_addr;
11523 : 3798 : tree flag, begin;
11524 : : /* We don't need thread-safety code for thread-local vars. */
11525 : 3798 : bool thread_guard = (flag_threadsafe_statics
11526 : 3798 : && !CP_DECL_THREAD_LOCAL_P (decl));
11527 : :
11528 : : /* Emit code to perform this initialization but once. This code
11529 : : looks like:
11530 : :
11531 : : static <type> guard;
11532 : : if (!__atomic_load (guard.first_byte)) {
11533 : : if (__cxa_guard_acquire (&guard)) {
11534 : : bool flag = false;
11535 : : try {
11536 : : // Do initialization.
11537 : : flag = true; __cxa_guard_release (&guard);
11538 : : // Register variable for destruction at end of program.
11539 : : } catch {
11540 : : if (!flag) __cxa_guard_abort (&guard);
11541 : : }
11542 : : }
11543 : : }
11544 : :
11545 : : Note that the `flag' variable is only set to 1 *after* the
11546 : : initialization is complete. This ensures that an exception,
11547 : : thrown during the construction, will cause the variable to
11548 : : reinitialized when we pass through this code again, as per:
11549 : :
11550 : : [stmt.dcl]
11551 : :
11552 : : If the initialization exits by throwing an exception, the
11553 : : initialization is not complete, so it will be tried again
11554 : : the next time control enters the declaration.
11555 : :
11556 : : This process should be thread-safe, too; multiple threads
11557 : : should not be able to initialize the variable more than
11558 : : once. */
11559 : :
11560 : : /* Create the guard variable. */
11561 : 3798 : guard = get_guard (decl);
11562 : :
11563 : : /* Begin the conditional initialization. */
11564 : 3798 : if_stmt = begin_if_stmt ();
11565 : :
11566 : 3798 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11567 : 3798 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11568 : :
11569 : 3798 : if (thread_guard)
11570 : : {
11571 : 3656 : tree vfntype = NULL_TREE;
11572 : 3656 : tree acquire_name, release_name, abort_name;
11573 : 3656 : tree acquire_fn, release_fn, abort_fn;
11574 : 3656 : guard_addr = build_address (guard);
11575 : :
11576 : 3656 : acquire_name = get_identifier ("__cxa_guard_acquire");
11577 : 3656 : release_name = get_identifier ("__cxa_guard_release");
11578 : 3656 : abort_name = get_identifier ("__cxa_guard_abort");
11579 : 3656 : acquire_fn = get_global_binding (acquire_name);
11580 : 3656 : release_fn = get_global_binding (release_name);
11581 : 3656 : abort_fn = get_global_binding (abort_name);
11582 : 3656 : if (!acquire_fn)
11583 : 2040 : acquire_fn = push_library_fn
11584 : 2040 : (acquire_name, build_function_type_list (integer_type_node,
11585 : 2040 : TREE_TYPE (guard_addr),
11586 : : NULL_TREE),
11587 : : NULL_TREE, ECF_NOTHROW);
11588 : 3656 : if (!release_fn || !abort_fn)
11589 : 2043 : vfntype = build_function_type_list (void_type_node,
11590 : 2043 : TREE_TYPE (guard_addr),
11591 : : NULL_TREE);
11592 : 2043 : if (!release_fn)
11593 : 2043 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11594 : : ECF_NOTHROW);
11595 : 3656 : if (!abort_fn)
11596 : 2043 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11597 : : ECF_NOTHROW | ECF_LEAF);
11598 : :
11599 : 3656 : inner_if_stmt = begin_if_stmt ();
11600 : 3656 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11601 : : inner_if_stmt);
11602 : :
11603 : 3656 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11604 : 3656 : begin = get_internal_target_expr (boolean_false_node);
11605 : 3656 : flag = TARGET_EXPR_SLOT (begin);
11606 : :
11607 : 3656 : TARGET_EXPR_CLEANUP (begin)
11608 : 3656 : = build3 (COND_EXPR, void_type_node, flag,
11609 : : void_node,
11610 : : build_call_n (abort_fn, 1, guard_addr));
11611 : 3656 : CLEANUP_EH_ONLY (begin) = 1;
11612 : :
11613 : : /* Do the initialization itself. */
11614 : 3656 : init = add_stmt_to_compound (begin, init);
11615 : 3656 : init = add_stmt_to_compound (init,
11616 : : build2 (MODIFY_EXPR, void_type_node,
11617 : : flag, boolean_true_node));
11618 : :
11619 : : /* Use atexit to register a function for destroying this static
11620 : : variable. Do this before calling __cxa_guard_release. */
11621 : 3656 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11622 : :
11623 : 3656 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11624 : : guard_addr));
11625 : : }
11626 : : else
11627 : : {
11628 : 142 : init = add_stmt_to_compound (init, set_guard (guard));
11629 : :
11630 : : /* Use atexit to register a function for destroying this static
11631 : : variable. */
11632 : 142 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11633 : : }
11634 : :
11635 : 3798 : finish_expr_stmt (init);
11636 : :
11637 : 3798 : if (thread_guard)
11638 : : {
11639 : 3656 : finish_compound_stmt (inner_then_clause);
11640 : 3656 : finish_then_clause (inner_if_stmt);
11641 : 3656 : finish_if_stmt (inner_if_stmt);
11642 : : }
11643 : :
11644 : 3798 : finish_compound_stmt (then_clause);
11645 : 3798 : finish_then_clause (if_stmt);
11646 : 3798 : finish_if_stmt (if_stmt);
11647 : : }
11648 : 10920 : else if (CP_DECL_THREAD_LOCAL_P (decl))
11649 : 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
11650 : : else
11651 : 10125 : static_aggregates = tree_cons (init, decl, static_aggregates);
11652 : : }
11653 : :
11654 : :
11655 : : /* Make TYPE a complete type based on INITIAL_VALUE.
11656 : : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11657 : : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11658 : : 3 if the initializer list is empty (in pedantic mode). */
11659 : :
11660 : : int
11661 : 528986 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11662 : : {
11663 : 528986 : int failure;
11664 : 528986 : tree type, elt_type;
11665 : :
11666 : : /* Don't get confused by a CONSTRUCTOR for some other type. */
11667 : 528956 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11668 : 95353 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11669 : 529311 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11670 : : return 1;
11671 : :
11672 : 528983 : if (initial_value)
11673 : : {
11674 : : /* An array of character type can be initialized from a
11675 : : brace-enclosed string constant so call reshape_init to
11676 : : remove the optional braces from a braced string literal. */
11677 : 528953 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
11678 : 528953 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
11679 : 29135 : initial_value = reshape_init (*ptype, initial_value,
11680 : : tf_warning_or_error);
11681 : :
11682 : : /* If any of the elements are parameter packs, we can't actually
11683 : : complete this type now because the array size is dependent. */
11684 : 528953 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
11685 : 16485733 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
11686 : 16200048 : if (PACK_EXPANSION_P (e.value))
11687 : : return 0;
11688 : : }
11689 : :
11690 : 528983 : failure = complete_array_type (ptype, initial_value, do_default);
11691 : :
11692 : : /* We can create the array before the element type is complete, which
11693 : : means that we didn't have these two bits set in the original type
11694 : : either. In completing the type, we are expected to propagate these
11695 : : bits. See also complete_type which does the same thing for arrays
11696 : : of fixed size. */
11697 : 528983 : type = *ptype;
11698 : 1057963 : if (type != error_mark_node && TYPE_DOMAIN (type))
11699 : : {
11700 : 528956 : elt_type = TREE_TYPE (type);
11701 : 528956 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
11702 : 1057912 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11703 : 528956 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
11704 : : }
11705 : :
11706 : : return failure;
11707 : : }
11708 : :
11709 : : /* As above, but either give an error or reject zero-size arrays, depending
11710 : : on COMPLAIN. */
11711 : :
11712 : : int
11713 : 1409 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
11714 : : bool do_default, tsubst_flags_t complain)
11715 : : {
11716 : 1409 : int failure;
11717 : 1409 : bool sfinae = !(complain & tf_error);
11718 : : /* In SFINAE context we can't be lenient about zero-size arrays. */
11719 : 1409 : if (sfinae)
11720 : 3 : ++pedantic;
11721 : 1409 : failure = cp_complete_array_type (ptype, initial_value, do_default);
11722 : 1409 : if (sfinae)
11723 : 3 : --pedantic;
11724 : 1409 : if (failure)
11725 : : {
11726 : 3 : if (sfinae)
11727 : : /* Not an error. */;
11728 : 0 : else if (failure == 1)
11729 : 0 : error ("initializer fails to determine size of %qT", *ptype);
11730 : 0 : else if (failure == 2)
11731 : : {
11732 : 0 : if (do_default)
11733 : 0 : error ("array size missing in %qT", *ptype);
11734 : : }
11735 : 0 : else if (failure == 3)
11736 : 0 : error ("zero-size array %qT", *ptype);
11737 : 3 : *ptype = error_mark_node;
11738 : : }
11739 : 1409 : return failure;
11740 : : }
11741 : :
11742 : : /* Return zero if something is declared to be a member of type
11743 : : CTYPE when in the context of CUR_TYPE. STRING is the error
11744 : : message to print in that case. Otherwise, quietly return 1. */
11745 : :
11746 : : static int
11747 : 24631687 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
11748 : : {
11749 : 24631687 : if (ctype && ctype != cur_type)
11750 : : {
11751 : 0 : if (flags == DTOR_FLAG)
11752 : 0 : error ("destructor for alien class %qT cannot be a member", ctype);
11753 : : else
11754 : 0 : error ("constructor for alien class %qT cannot be a member", ctype);
11755 : 0 : return 0;
11756 : : }
11757 : : return 1;
11758 : : }
11759 : :
11760 : : /* Subroutine of `grokdeclarator'. */
11761 : :
11762 : : /* Generate errors possibly applicable for a given set of specifiers.
11763 : : This is for ARM $7.1.2. */
11764 : :
11765 : : static void
11766 : 408851670 : bad_specifiers (tree object,
11767 : : enum bad_spec_place type,
11768 : : int virtualp,
11769 : : int quals,
11770 : : int inlinep,
11771 : : int friendp,
11772 : : int raises,
11773 : : const location_t* locations)
11774 : : {
11775 : 408851670 : switch (type)
11776 : : {
11777 : 57353729 : case BSP_VAR:
11778 : 57353729 : if (virtualp)
11779 : 0 : error_at (locations[ds_virtual],
11780 : : "%qD declared as a %<virtual%> variable", object);
11781 : 57353729 : if (quals)
11782 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11783 : : "%qD invalid in variable declaration", object);
11784 : : break;
11785 : 279314284 : case BSP_PARM:
11786 : 279314284 : if (virtualp)
11787 : 0 : error_at (locations[ds_virtual],
11788 : : "%qD declared as a %<virtual%> parameter", object);
11789 : 279314284 : if (inlinep)
11790 : 6 : error_at (locations[ds_inline],
11791 : : "%qD declared as an %<inline%> parameter", object);
11792 : 279314284 : if (quals)
11793 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11794 : : "%qD invalid in parameter declaration", object);
11795 : : break;
11796 : 44346060 : case BSP_TYPE:
11797 : 44346060 : if (virtualp)
11798 : 0 : error_at (locations[ds_virtual],
11799 : : "%qD declared as a %<virtual%> type", object);
11800 : 44346060 : if (inlinep)
11801 : 6 : error_at (locations[ds_inline],
11802 : : "%qD declared as an %<inline%> type", object);
11803 : 44346060 : if (quals)
11804 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11805 : : "%qD invalid in type declaration", object);
11806 : : break;
11807 : 27837597 : case BSP_FIELD:
11808 : 27837597 : if (virtualp)
11809 : 6 : error_at (locations[ds_virtual],
11810 : : "%qD declared as a %<virtual%> field", object);
11811 : 27837597 : if (inlinep)
11812 : 6 : error_at (locations[ds_inline],
11813 : : "%qD declared as an %<inline%> field", object);
11814 : 27837597 : if (quals)
11815 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11816 : : "%qD invalid in field declaration", object);
11817 : : break;
11818 : 0 : default:
11819 : 0 : gcc_unreachable();
11820 : : }
11821 : 408851670 : if (friendp)
11822 : 0 : error ("%q+D declared as a friend", object);
11823 : 408851670 : if (raises
11824 : 109 : && !flag_noexcept_type
11825 : 408851691 : && (TREE_CODE (object) == TYPE_DECL
11826 : 15 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
11827 : 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
11828 : 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
11829 : 6 : error ("%q+D declared with an exception specification", object);
11830 : 408851670 : }
11831 : :
11832 : : /* DECL is a member function or static data member and is presently
11833 : : being defined. Check that the definition is taking place in a
11834 : : valid namespace. */
11835 : :
11836 : : static void
11837 : 85549196 : check_class_member_definition_namespace (tree decl)
11838 : : {
11839 : : /* These checks only apply to member functions and static data
11840 : : members. */
11841 : 85549196 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
11842 : : /* We check for problems with specializations in pt.cc in
11843 : : check_specialization_namespace, where we can issue better
11844 : : diagnostics. */
11845 : 85549196 : if (processing_specialization)
11846 : : return;
11847 : : /* We check this in check_explicit_instantiation_namespace. */
11848 : 85400698 : if (processing_explicit_instantiation)
11849 : : return;
11850 : : /* [class.mfct]
11851 : :
11852 : : A member function definition that appears outside of the
11853 : : class definition shall appear in a namespace scope enclosing
11854 : : the class definition.
11855 : :
11856 : : [class.static.data]
11857 : :
11858 : : The definition for a static data member shall appear in a
11859 : : namespace scope enclosing the member's class definition. */
11860 : 85400617 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
11861 : 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
11862 : 18 : decl, DECL_CONTEXT (decl));
11863 : : }
11864 : :
11865 : : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
11866 : : METHOD_TYPE for a non-static member function; QUALS are the
11867 : : cv-qualifiers that apply to the function. */
11868 : :
11869 : : tree
11870 : 208241959 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
11871 : : {
11872 : 208241959 : tree this_type;
11873 : 208241959 : tree qual_type;
11874 : 208241959 : tree parm;
11875 : 208241959 : cp_cv_quals this_quals;
11876 : :
11877 : 208241959 : if (CLASS_TYPE_P (type))
11878 : : {
11879 : 93417519 : this_type
11880 : 93417519 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
11881 : 93417519 : this_type = build_pointer_type (this_type);
11882 : : }
11883 : : else
11884 : 114824440 : this_type = type_of_this_parm (type);
11885 : : /* The `this' parameter is implicitly `const'; it cannot be
11886 : : assigned to. */
11887 : 208241959 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
11888 : 208241959 : qual_type = cp_build_qualified_type (this_type, this_quals);
11889 : 208241959 : parm = build_artificial_parm (fn, this_identifier, qual_type);
11890 : 208241959 : cp_apply_type_quals_to_decl (this_quals, parm);
11891 : 208241959 : return parm;
11892 : : }
11893 : :
11894 : : /* DECL is a static member function. Complain if it was declared
11895 : : with function-cv-quals. */
11896 : :
11897 : : static void
11898 : 8883986 : check_static_quals (tree decl, cp_cv_quals quals)
11899 : : {
11900 : 0 : if (quals != TYPE_UNQUALIFIED)
11901 : 6 : error ("static member function %q#D declared with type qualifiers",
11902 : : decl);
11903 : 0 : }
11904 : :
11905 : : /* Helper function. Replace the temporary this parameter injected
11906 : : during cp_finish_omp_declare_simd with the real this parameter. */
11907 : :
11908 : : static tree
11909 : 2500 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11910 : : {
11911 : 2500 : tree this_parm = (tree) data;
11912 : 2500 : if (TREE_CODE (*tp) == PARM_DECL
11913 : 450 : && DECL_NAME (*tp) == this_identifier
11914 : 2773 : && *tp != this_parm)
11915 : 273 : *tp = this_parm;
11916 : 2227 : else if (TYPE_P (*tp))
11917 : 33 : *walk_subtrees = 0;
11918 : 2500 : return NULL_TREE;
11919 : : }
11920 : :
11921 : : /* CTYPE is class type, or null if non-class.
11922 : : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11923 : : or METHOD_TYPE.
11924 : : DECLARATOR is the function's name.
11925 : : PARMS is a chain of PARM_DECLs for the function.
11926 : : VIRTUALP is truthvalue of whether the function is virtual or not.
11927 : : FLAGS are to be passed through to `grokclassfn'.
11928 : : QUALS are qualifiers indicating whether the function is `const'
11929 : : or `volatile'.
11930 : : RAISES is a list of exceptions that this function can raise.
11931 : : CHECK is 1 if we must find this method in CTYPE, 0 if we should
11932 : : not look, and -1 if we should not call `grokclassfn' at all.
11933 : :
11934 : : SFK is the kind of special function (if any) for the new function.
11935 : :
11936 : : Returns `NULL_TREE' if something goes wrong, after issuing
11937 : : applicable error messages. */
11938 : :
11939 : : static tree
11940 : 175432929 : grokfndecl (tree ctype,
11941 : : tree type,
11942 : : tree declarator,
11943 : : tree parms,
11944 : : tree orig_declarator,
11945 : : const cp_decl_specifier_seq *declspecs,
11946 : : tree decl_reqs,
11947 : : int virtualp,
11948 : : enum overload_flags flags,
11949 : : cp_cv_quals quals,
11950 : : cp_ref_qualifier rqual,
11951 : : tree raises,
11952 : : int check,
11953 : : int friendp,
11954 : : int publicp,
11955 : : int inlinep,
11956 : : bool deletedp,
11957 : : bool xobj_func_p,
11958 : : special_function_kind sfk,
11959 : : bool funcdef_flag,
11960 : : bool late_return_type_p,
11961 : : int template_count,
11962 : : tree in_namespace,
11963 : : tree* attrlist,
11964 : : location_t location)
11965 : : {
11966 : 175432929 : tree decl;
11967 : 175432929 : tree t;
11968 : :
11969 : 175432929 : if (location == UNKNOWN_LOCATION)
11970 : 0 : location = input_location;
11971 : :
11972 : : /* Was the concept specifier present? */
11973 : 175432929 : bool concept_p = inlinep & 4;
11974 : :
11975 : 175432929 : if (concept_p)
11976 : : {
11977 : 15 : error_at (location, "function concepts are no longer supported");
11978 : 15 : return NULL_TREE;
11979 : : }
11980 : :
11981 : 175432914 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
11982 : :
11983 : 175432914 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
11984 : :
11985 : : /* Set the constraints on the declaration. */
11986 : 175432914 : if (flag_concepts)
11987 : : {
11988 : 51708873 : tree tmpl_reqs = NULL_TREE;
11989 : 51708873 : tree ctx = friendp ? current_class_type : ctype;
11990 : 51708873 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
11991 : 51708873 : bool memtmpl = (!block_local
11992 : 51708873 : && (current_template_depth
11993 : 51693466 : > template_class_depth (ctx)));
11994 : 14566527 : if (memtmpl)
11995 : : {
11996 : 14566527 : if (!current_template_parms)
11997 : : /* If there are no template parameters, something must have
11998 : : gone wrong. */
11999 : 0 : gcc_assert (seen_error ());
12000 : : else
12001 : 14566527 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12002 : : }
12003 : 51708873 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
12004 : : /* C++20 CA378: Remove non-templated constrained functions. */
12005 : : /* [temp.friend]/9 A non-template friend declaration with a
12006 : : requires-clause shall be a definition. A friend function template with
12007 : : a constraint that depends on a template parameter from an enclosing
12008 : : template shall be a definition. */
12009 : 51708873 : if (ci
12010 : 3863138 : && (block_local
12011 : 3863135 : || !processing_template_decl
12012 : 3863080 : || (friendp && !memtmpl && !funcdef_flag)))
12013 : : {
12014 : 67 : if (!friendp || !processing_template_decl)
12015 : 58 : error_at (location, "constraints on a non-templated function");
12016 : : else
12017 : 9 : error_at (location, "constrained non-template friend declaration"
12018 : : " must be a definition");
12019 : : ci = NULL_TREE;
12020 : : }
12021 : 51708873 : set_constraints (decl, ci);
12022 : 550254 : if (ci && friendp && memtmpl && !funcdef_flag
12023 : 51718498 : && uses_outer_template_parms_in_constraints (decl, ctx))
12024 : 6 : error_at (location, "friend function template with constraints that "
12025 : : "depend on outer template parameters must be a definition");
12026 : : }
12027 : :
12028 : 175432914 : if (TREE_CODE (type) == METHOD_TYPE)
12029 : : {
12030 : 91869234 : tree parm = build_this_parm (decl, type, quals);
12031 : 91869234 : DECL_CHAIN (parm) = parms;
12032 : 91869234 : parms = parm;
12033 : :
12034 : : /* Allocate space to hold the vptr bit if needed. */
12035 : 91869234 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12036 : : }
12037 : :
12038 : 175432914 : DECL_ARGUMENTS (decl) = parms;
12039 : 531253582 : for (t = parms; t; t = DECL_CHAIN (t))
12040 : 355820668 : DECL_CONTEXT (t) = decl;
12041 : :
12042 : : /* Propagate volatile out from type to decl. */
12043 : 175432914 : if (TYPE_VOLATILE (type))
12044 : 0 : TREE_THIS_VOLATILE (decl) = 1;
12045 : :
12046 : : /* Setup decl according to sfk. */
12047 : 175432914 : switch (sfk)
12048 : : {
12049 : 22418556 : case sfk_constructor:
12050 : 22418556 : case sfk_copy_constructor:
12051 : 22418556 : case sfk_move_constructor:
12052 : 22418556 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12053 : 22418556 : DECL_NAME (decl) = ctor_identifier;
12054 : 22418556 : break;
12055 : 3188739 : case sfk_destructor:
12056 : 3188739 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
12057 : 3188739 : DECL_NAME (decl) = dtor_identifier;
12058 : 3188739 : break;
12059 : 1084759 : case sfk_deduction_guide:
12060 : : /* Give deduction guides a definition even though they don't really
12061 : : have one: the restriction that you can't repeat a deduction guide
12062 : : makes them more like a definition anyway. */
12063 : 1084759 : DECL_INITIAL (decl) = void_node;
12064 : : /* But to ensure that external-linkage deduction guides in header units
12065 : : don't fall afoul of [module.import] p6, mark them as inline. */
12066 : 1084759 : DECL_DECLARED_INLINE_P (decl) = true;
12067 : 1084759 : break;
12068 : : default:
12069 : : break;
12070 : : }
12071 : :
12072 : 175432914 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12073 : : {
12074 : 39989 : if (funcdef_flag)
12075 : 3 : error_at (location,
12076 : : "defining explicit specialization %qD in friend declaration",
12077 : : orig_declarator);
12078 : : else
12079 : : {
12080 : 39986 : tree fns = TREE_OPERAND (orig_declarator, 0);
12081 : 39986 : tree args = TREE_OPERAND (orig_declarator, 1);
12082 : :
12083 : 39986 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12084 : : {
12085 : : /* Something like `template <class T> friend void f<T>()'. */
12086 : 6 : error_at (location,
12087 : : "invalid use of template-id %qD in declaration "
12088 : : "of primary template",
12089 : : orig_declarator);
12090 : 6 : return NULL_TREE;
12091 : : }
12092 : :
12093 : : /* A friend declaration of the form friend void f<>(). Record
12094 : : the information in the TEMPLATE_ID_EXPR. */
12095 : 39980 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
12096 : :
12097 : 39980 : gcc_assert (identifier_p (fns) || OVL_P (fns));
12098 : 39980 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12099 : :
12100 : : /* Remember the befriending class like push_template_decl does for
12101 : : template friends. */
12102 : 39980 : gcc_checking_assert (!DECL_CHAIN (decl));
12103 : 39980 : DECL_CHAIN (decl) = current_scope ();
12104 : :
12105 : 141817 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12106 : 101841 : if (TREE_PURPOSE (t)
12107 : 101841 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12108 : : {
12109 : 4 : error_at (defparse_location (TREE_PURPOSE (t)),
12110 : : "default arguments are not allowed in declaration "
12111 : : "of friend template specialization %qD",
12112 : : decl);
12113 : 4 : return NULL_TREE;
12114 : : }
12115 : :
12116 : 39976 : if (inlinep & 1)
12117 : : {
12118 : 4 : error_at (declspecs->locations[ds_inline],
12119 : : "%<inline%> is not allowed in declaration of friend "
12120 : : "template specialization %qD",
12121 : : decl);
12122 : 4 : return NULL_TREE;
12123 : : }
12124 : : }
12125 : : }
12126 : :
12127 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12128 : : expression, that declaration shall be a definition..." */
12129 : 175432900 : if (friendp && !funcdef_flag)
12130 : : {
12131 : 1338293 : bool has_errored = false;
12132 : 1338293 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12133 : 3877029 : t && t != void_list_node; t = TREE_CHAIN (t))
12134 : 2538736 : if (TREE_PURPOSE (t))
12135 : : {
12136 : 48 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12137 : : /* For templates, mark the default argument as erroneous and give a
12138 : : hard error. */
12139 : 48 : if (processing_template_decl)
12140 : : {
12141 : 30 : diag_kind = diagnostics::kind::error;
12142 : 30 : TREE_PURPOSE (t) = error_mark_node;
12143 : : }
12144 : 48 : if (!has_errored)
12145 : : {
12146 : 45 : has_errored = true;
12147 : 45 : emit_diagnostic (diag_kind,
12148 : 45 : DECL_SOURCE_LOCATION (decl),
12149 : 45 : /*diagnostics::option_id=*/0,
12150 : : "friend declaration of %qD specifies default "
12151 : : "arguments and isn%'t a definition", decl);
12152 : : }
12153 : : }
12154 : : }
12155 : :
12156 : : /* If this decl has namespace scope, set that up. */
12157 : 175432900 : if (in_namespace)
12158 : 178790 : set_decl_namespace (decl, in_namespace, friendp);
12159 : 175254110 : else if (ctype)
12160 : 100646626 : DECL_CONTEXT (decl) = ctype;
12161 : : else
12162 : 74607484 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12163 : :
12164 : : /* `main' and builtins have implicit 'C' linkage. */
12165 : 175432900 : if (ctype == NULL_TREE
12166 : 74786274 : && DECL_FILE_SCOPE_P (decl)
12167 : 32588305 : && current_lang_name == lang_name_cplusplus
12168 : 178412761 : && (MAIN_NAME_P (declarator)
12169 : 2946677 : || (IDENTIFIER_LENGTH (declarator) > 10
12170 : 2099533 : && IDENTIFIER_POINTER (declarator)[0] == '_'
12171 : 1638297 : && IDENTIFIER_POINTER (declarator)[1] == '_'
12172 : 355672 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
12173 : : "builtin_"))
12174 : 2946659 : || (targetcm.cxx_implicit_extern_c
12175 : 0 : && (targetcm.cxx_implicit_extern_c
12176 : 0 : (IDENTIFIER_POINTER (declarator))))))
12177 : 33202 : SET_DECL_LANGUAGE (decl, lang_c);
12178 : :
12179 : 175432900 : DECL_STATIC_FUNCTION_P (decl)
12180 : 175432900 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12181 : 175432900 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12182 : :
12183 : 175432900 : if (deletedp)
12184 : 3556797 : DECL_DELETED_FN (decl) = 1;
12185 : :
12186 : 175432900 : if (ctype && funcdef_flag)
12187 : 85158021 : check_class_member_definition_namespace (decl);
12188 : :
12189 : 175432900 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12190 : : {
12191 : 33190 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
12192 : 9 : error_at (location, "cannot declare %<::main%> to be a template");
12193 : 33190 : if (inlinep & 1)
12194 : 3 : error_at (declspecs->locations[ds_inline],
12195 : : "cannot declare %<::main%> to be inline");
12196 : 33190 : if (inlinep & 2)
12197 : 6 : error_at (declspecs->locations[ds_constexpr],
12198 : : "cannot declare %<::main%> to be %qs", "constexpr");
12199 : 33190 : if (inlinep & 8)
12200 : 3 : error_at (declspecs->locations[ds_consteval],
12201 : : "cannot declare %<::main%> to be %qs", "consteval");
12202 : 33190 : if (!publicp)
12203 : 0 : error_at (location, "cannot declare %<::main%> to be static");
12204 : 33190 : if (current_lang_name != lang_name_cplusplus)
12205 : 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12206 : : " linkage specification other than %<extern \"C++\"%>");
12207 : 33190 : if (module_attach_p ())
12208 : : {
12209 : 3 : auto_diagnostic_group adg;
12210 : 3 : error_at (location, "cannot attach %<::main%> to a named module");
12211 : 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
12212 : : "global module instead");
12213 : 3 : }
12214 : : inlinep = 0;
12215 : : publicp = 1;
12216 : : }
12217 : :
12218 : : /* Members of anonymous types and local classes have no linkage; make
12219 : : them internal. If a typedef is made later, this will be changed. */
12220 : 175432900 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12221 : : publicp = 0;
12222 : 175419910 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12223 : : /* But members of local classes in a module CMI should have their
12224 : : definitions exported, in case they are (directly or indirectly)
12225 : : used by an importer. We don't just use module_has_cmi_p here
12226 : : because for entities in the GMF we don't yet know whether this
12227 : : module will have a CMI, so we'll conservatively assume it might. */
12228 : 1578197 : publicp = module_maybe_has_cmi_p ();
12229 : :
12230 : 175419910 : if (publicp && cxx_dialect == cxx98)
12231 : : {
12232 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12233 : : or enumeration declared in a local scope) shall not be used to
12234 : : declare an entity with linkage.
12235 : :
12236 : : DR 757 relaxes this restriction for C++0x. */
12237 : 1025999 : no_linkage_error (decl);
12238 : : }
12239 : :
12240 : 175432900 : TREE_PUBLIC (decl) = publicp;
12241 : 175432900 : if (! publicp)
12242 : : {
12243 : 1694107 : DECL_INTERFACE_KNOWN (decl) = 1;
12244 : 1694107 : DECL_NOT_REALLY_EXTERN (decl) = 1;
12245 : : }
12246 : :
12247 : : /* If the declaration was declared inline, mark it as such. */
12248 : 175432900 : if (inlinep)
12249 : : {
12250 : 52370840 : DECL_DECLARED_INLINE_P (decl) = 1;
12251 : 52370840 : if (publicp)
12252 : 52223632 : DECL_COMDAT (decl) = 1;
12253 : : }
12254 : 52370840 : if (inlinep & 2)
12255 : 31260992 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12256 : 144171908 : else if (inlinep & 8)
12257 : : {
12258 : 289444 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12259 : 289444 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12260 : : }
12261 : :
12262 : 175432900 : DECL_EXTERNAL (decl) = 1;
12263 : 175432900 : if (TREE_CODE (type) == FUNCTION_TYPE)
12264 : : {
12265 : 83563666 : if (quals || rqual)
12266 : 272 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12267 : : TYPE_UNQUALIFIED,
12268 : : REF_QUAL_NONE);
12269 : 83563666 : auto_diagnostic_group d;
12270 : 83563666 : if (quals)
12271 : 423 : error (!ctype
12272 : : ? G_("non-member function %qD cannot have cv-qualifier")
12273 : : : !xobj_func_p
12274 : 198 : ? G_("static member function %qD cannot have cv-qualifier")
12275 : : : G_("explicit object member function "
12276 : : "%qD cannot have cv-qualifier"),
12277 : : decl);
12278 : 83563666 : if (rqual)
12279 : 355 : error (!ctype
12280 : : ? G_("non-member function %qD cannot have ref-qualifier")
12281 : : : !xobj_func_p
12282 : 176 : ? G_("static member function %qD cannot have ref-qualifier")
12283 : : : G_("explicit object member function "
12284 : : "%qD cannot have ref-qualifier"),
12285 : : decl);
12286 : :
12287 : 83563666 : if (xobj_func_p && (quals || rqual))
12288 : 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12289 : : "explicit object parameter declared here");
12290 : 83563666 : quals = TYPE_UNQUALIFIED;
12291 : 83563666 : rqual = REF_QUAL_NONE;
12292 : :
12293 : 83563666 : }
12294 : :
12295 : 175432900 : if (deduction_guide_p (decl))
12296 : : {
12297 : 1084759 : tree type = TREE_TYPE (DECL_NAME (decl));
12298 : 1084759 : if (in_namespace == NULL_TREE
12299 : 1084759 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12300 : : {
12301 : 3 : auto_diagnostic_group d;
12302 : 3 : error_at (location, "deduction guide %qD must be declared in the "
12303 : : "same scope as %qT", decl, type);
12304 : 3 : inform (location_of (type), " declared here");
12305 : 3 : return NULL_TREE;
12306 : 3 : }
12307 : 2169512 : if (DECL_CLASS_SCOPE_P (decl)
12308 : 1084795 : && current_access_specifier != declared_access (TYPE_NAME (type)))
12309 : : {
12310 : 3 : auto_diagnostic_group d;
12311 : 3 : error_at (location, "deduction guide %qD must have the same access "
12312 : : "as %qT", decl, type);
12313 : 3 : inform (location_of (type), " declared here");
12314 : 3 : }
12315 : 1084756 : if (funcdef_flag)
12316 : 3 : error_at (location,
12317 : : "deduction guide %qD must not have a function body", decl);
12318 : : }
12319 : 327097599 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12320 : 185006735 : && !grok_op_properties (decl, /*complain=*/true))
12321 : : return NULL_TREE;
12322 : 174347991 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
12323 : : {
12324 : 283539 : bool long_long_unsigned_p;
12325 : 283539 : bool long_double_p;
12326 : 283539 : const char *suffix = NULL;
12327 : : /* [over.literal]/6: Literal operators shall not have C linkage. */
12328 : 283539 : if (DECL_LANGUAGE (decl) == lang_c)
12329 : : {
12330 : 6 : auto_diagnostic_group d;
12331 : 6 : error_at (location, "literal operator with C linkage");
12332 : 6 : maybe_show_extern_c_location ();
12333 : 6 : return NULL_TREE;
12334 : 6 : }
12335 : :
12336 : 283533 : if (DECL_NAMESPACE_SCOPE_P (decl))
12337 : : {
12338 : 283527 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12339 : : &long_double_p))
12340 : : {
12341 : 42 : error_at (location, "%qD has invalid argument list", decl);
12342 : 42 : return NULL_TREE;
12343 : : }
12344 : :
12345 : 283485 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12346 : 283485 : if (long_long_unsigned_p)
12347 : : {
12348 : 34284 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
12349 : 68 : warning_at (location, 0, "integer suffix %qs"
12350 : : " shadowed by implementation", suffix);
12351 : : }
12352 : 249201 : else if (long_double_p)
12353 : : {
12354 : 89673 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
12355 : 80 : warning_at (location, 0, "floating-point suffix %qs"
12356 : : " shadowed by implementation", suffix);
12357 : : }
12358 : : /* 17.6.3.3.5 */
12359 : 283485 : if (suffix[0] != '_'
12360 : 282936 : && !current_function_decl && !(friendp && !funcdef_flag))
12361 : 282930 : warning_at (location, OPT_Wliteral_suffix,
12362 : : "literal operator suffixes not preceded by %<_%>"
12363 : : " are reserved for future standardization");
12364 : : }
12365 : : else
12366 : : {
12367 : 6 : error_at (location, "%qD must be a non-member function", decl);
12368 : 6 : return NULL_TREE;
12369 : : }
12370 : : }
12371 : :
12372 : 174347940 : if (funcdef_flag)
12373 : : /* Make the init_value nonzero so pushdecl knows this is not
12374 : : tentative. error_mark_node is replaced later with the BLOCK. */
12375 : 119255203 : DECL_INITIAL (decl) = error_mark_node;
12376 : :
12377 : 175432693 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12378 : 66206970 : TREE_NOTHROW (decl) = 1;
12379 : :
12380 : 175432693 : if (flag_openmp || flag_openmp_simd)
12381 : : {
12382 : : /* Adjust "omp declare simd" attributes. */
12383 : 450481 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
12384 : 450481 : if (ods)
12385 : : {
12386 : : tree attr;
12387 : 2006 : for (attr = ods; attr;
12388 : 1060 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
12389 : : {
12390 : 1060 : if (TREE_CODE (type) == METHOD_TYPE)
12391 : 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12392 : : DECL_ARGUMENTS (decl), NULL);
12393 : 1060 : if (TREE_VALUE (attr) != NULL_TREE)
12394 : : {
12395 : 854 : tree cl = TREE_VALUE (TREE_VALUE (attr));
12396 : 854 : cl = c_omp_declare_simd_clauses_to_numbers
12397 : 854 : (DECL_ARGUMENTS (decl), cl);
12398 : 854 : if (cl)
12399 : 854 : TREE_VALUE (TREE_VALUE (attr)) = cl;
12400 : : else
12401 : 0 : TREE_VALUE (attr) = NULL_TREE;
12402 : : }
12403 : : }
12404 : : }
12405 : : }
12406 : :
12407 : : /* Caller will do the rest of this. */
12408 : 175432693 : if (check < 0)
12409 : 4331591 : return decl;
12410 : :
12411 : 171101102 : if (ctype != NULL_TREE)
12412 : 100646330 : grokclassfn (ctype, decl, flags);
12413 : :
12414 : : /* 12.4/3 */
12415 : 171101102 : if (cxx_dialect >= cxx11
12416 : 340165624 : && DECL_DESTRUCTOR_P (decl)
12417 : 3170162 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12418 : 171273988 : && !processing_template_decl)
12419 : 87404 : deduce_noexcept_on_destructor (decl);
12420 : :
12421 : 171101102 : set_originating_module (decl);
12422 : :
12423 : 171101102 : decl = check_explicit_specialization (orig_declarator, decl,
12424 : : template_count,
12425 : 171101102 : 2 * funcdef_flag +
12426 : 171101102 : 4 * (friendp != 0),
12427 : : *attrlist);
12428 : 171101102 : if (decl == error_mark_node)
12429 : : return NULL_TREE;
12430 : :
12431 : 171100854 : if (DECL_STATIC_FUNCTION_P (decl))
12432 : 8720151 : check_static_quals (decl, quals);
12433 : :
12434 : 171100854 : if (attrlist)
12435 : : {
12436 : 171100854 : cplus_decl_attributes (&decl, *attrlist, 0);
12437 : 171100854 : *attrlist = NULL_TREE;
12438 : : }
12439 : :
12440 : 171100854 : if (DECL_HAS_CONTRACTS_P (decl))
12441 : 467 : rebuild_postconditions (decl);
12442 : :
12443 : : /* Check main's type after attributes have been applied. */
12444 : 171100854 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12445 : : {
12446 : 33178 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12447 : : integer_type_node))
12448 : : {
12449 : 19 : tree dtype = TREE_TYPE (decl);
12450 : 19 : tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12451 : 19 : tree newtype;
12452 : 19 : error_at (declspecs->locations[ds_type_spec],
12453 : : "%<::main%> must return %<int%>");
12454 : 57 : newtype = build_function_type (integer_type_node, oldtypeargs,
12455 : 19 : TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12456 : 19 : TREE_TYPE (decl) = newtype;
12457 : : }
12458 : 33178 : if (warn_main)
12459 : 33156 : check_main_parameter_types (decl);
12460 : : }
12461 : :
12462 : 171100854 : if (ctype != NULL_TREE && check)
12463 : : {
12464 : 9204028 : tree old_decl = check_classfn (ctype, decl,
12465 : 9204028 : (current_template_depth
12466 : 9204028 : > template_class_depth (ctype))
12467 : 2161494 : ? current_template_parms
12468 : : : NULL_TREE);
12469 : :
12470 : 9204028 : if (old_decl == error_mark_node)
12471 : : return NULL_TREE;
12472 : :
12473 : 9203947 : if (old_decl)
12474 : : {
12475 : 8791985 : tree ok;
12476 : 8791985 : tree pushed_scope;
12477 : :
12478 : 8791985 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12479 : : /* Because grokfndecl is always supposed to return a
12480 : : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12481 : : here. We depend on our callers to figure out that its
12482 : : really a template that's being returned. */
12483 : 2161482 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
12484 : :
12485 : 8791985 : if (DECL_STATIC_FUNCTION_P (old_decl)
12486 : 8791985 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12487 : : {
12488 : : /* Remove the `this' parm added by grokclassfn. */
12489 : 163835 : revert_static_member_fn (decl);
12490 : 163835 : check_static_quals (decl, quals);
12491 : : }
12492 : 8791985 : if (DECL_ARTIFICIAL (old_decl))
12493 : : {
12494 : 9 : error ("definition of implicitly-declared %qD", old_decl);
12495 : 9 : return NULL_TREE;
12496 : : }
12497 : 8791976 : else if (DECL_DEFAULTED_FN (old_decl))
12498 : : {
12499 : 3 : auto_diagnostic_group d;
12500 : 3 : error ("definition of explicitly-defaulted %q+D", decl);
12501 : 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12502 : : "%q#D explicitly defaulted here", old_decl);
12503 : 3 : return NULL_TREE;
12504 : 3 : }
12505 : :
12506 : : /* Since we've smashed OLD_DECL to its
12507 : : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12508 : 8791973 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12509 : 1003 : decl = DECL_TEMPLATE_RESULT (decl);
12510 : :
12511 : : /* Attempt to merge the declarations. This can fail, in
12512 : : the case of some invalid specialization declarations. */
12513 : 8791973 : pushed_scope = push_scope (ctype);
12514 : 8791973 : ok = duplicate_decls (decl, old_decl);
12515 : 8791973 : if (pushed_scope)
12516 : 8791973 : pop_scope (pushed_scope);
12517 : 8791973 : if (!ok)
12518 : : {
12519 : 0 : error ("no %q#D member function declared in class %qT",
12520 : : decl, ctype);
12521 : 0 : return NULL_TREE;
12522 : : }
12523 : 8791973 : if (ok == error_mark_node)
12524 : : return NULL_TREE;
12525 : : return old_decl;
12526 : : }
12527 : : }
12528 : :
12529 : 324617576 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12530 : : return NULL_TREE;
12531 : :
12532 : 162308785 : if (ctype == NULL_TREE || check)
12533 : 70866543 : return decl;
12534 : :
12535 : 91442242 : if (virtualp)
12536 : 4085211 : DECL_VIRTUAL_P (decl) = 1;
12537 : :
12538 : 91442242 : return decl;
12539 : : }
12540 : :
12541 : : /* decl is a FUNCTION_DECL.
12542 : : specifiers are the parsed virt-specifiers.
12543 : :
12544 : : Set flags to reflect the virt-specifiers.
12545 : :
12546 : : Returns decl. */
12547 : :
12548 : : static tree
12549 : 95773931 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12550 : : {
12551 : 95773931 : if (decl == NULL_TREE)
12552 : : return decl;
12553 : 95773833 : if (specifiers & VIRT_SPEC_OVERRIDE)
12554 : 187420 : DECL_OVERRIDE_P (decl) = 1;
12555 : 95773833 : if (specifiers & VIRT_SPEC_FINAL)
12556 : 2962 : DECL_FINAL_P (decl) = 1;
12557 : : return decl;
12558 : : }
12559 : :
12560 : : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12561 : : the linkage that DECL will receive in the object file. */
12562 : :
12563 : : static void
12564 : 10982065 : set_linkage_for_static_data_member (tree decl)
12565 : : {
12566 : : /* A static data member always has static storage duration and
12567 : : external linkage. Note that static data members are forbidden in
12568 : : local classes -- the only situation in which a class has
12569 : : non-external linkage. */
12570 : 10982065 : TREE_PUBLIC (decl) = 1;
12571 : 10982065 : TREE_STATIC (decl) = 1;
12572 : : /* For non-template classes, static data members are always put
12573 : : out in exactly those files where they are defined, just as
12574 : : with ordinary namespace-scope variables. */
12575 : 10982065 : if (!processing_template_decl)
12576 : 8170811 : DECL_INTERFACE_KNOWN (decl) = 1;
12577 : 10982065 : }
12578 : :
12579 : : /* Create a VAR_DECL named NAME with the indicated TYPE.
12580 : :
12581 : : If SCOPE is non-NULL, it is the class type or namespace containing
12582 : : the variable. If SCOPE is NULL, the variable should is created in
12583 : : the innermost enclosing scope. */
12584 : :
12585 : : static tree
12586 : 57353778 : grokvardecl (tree type,
12587 : : tree name,
12588 : : tree orig_declarator,
12589 : : const cp_decl_specifier_seq *declspecs,
12590 : : int initialized,
12591 : : int type_quals,
12592 : : int inlinep,
12593 : : bool conceptp,
12594 : : int template_count,
12595 : : tree scope,
12596 : : location_t location)
12597 : : {
12598 : 57353778 : tree decl;
12599 : 57353778 : tree explicit_scope;
12600 : :
12601 : 57353778 : gcc_assert (!name || identifier_p (name));
12602 : :
12603 : 57353778 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12604 : 57353778 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12605 : :
12606 : : /* Compute the scope in which to place the variable, but remember
12607 : : whether or not that scope was explicitly specified by the user. */
12608 : 57353778 : explicit_scope = scope;
12609 : 57353778 : if (!scope)
12610 : : {
12611 : : /* An explicit "extern" specifier indicates a namespace-scope
12612 : : variable. */
12613 : 56956481 : if (declspecs->storage_class == sc_extern)
12614 : 562463 : scope = current_decl_namespace ();
12615 : 56394018 : else if (!at_function_scope_p ())
12616 : 4457430 : scope = current_scope ();
12617 : : }
12618 : :
12619 : 56956481 : if (scope
12620 : 5417190 : && (/* If the variable is a namespace-scope variable declared in a
12621 : : template, we need DECL_LANG_SPECIFIC. */
12622 : 5417190 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
12623 : : /* Similarly for namespace-scope variables with language linkage
12624 : : other than C++. */
12625 : 5026015 : || (TREE_CODE (scope) == NAMESPACE_DECL
12626 : 2529073 : && current_lang_name != lang_name_cplusplus)
12627 : : /* Similarly for static data members. */
12628 : 2682145 : || TYPE_P (scope)
12629 : : /* Similarly for explicit specializations. */
12630 : 2290970 : || (orig_declarator
12631 : 2290525 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
12632 : 3516233 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
12633 : : else
12634 : 53837545 : decl = build_decl (location, VAR_DECL, name, type);
12635 : :
12636 : 57353778 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
12637 : 6122 : set_decl_namespace (decl, explicit_scope, 0);
12638 : : else
12639 : 57347656 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
12640 : :
12641 : 57353778 : if (declspecs->storage_class == sc_extern)
12642 : : {
12643 : 562463 : DECL_THIS_EXTERN (decl) = 1;
12644 : 562463 : DECL_EXTERNAL (decl) = !initialized;
12645 : : }
12646 : :
12647 : 57353778 : if (DECL_CLASS_SCOPE_P (decl))
12648 : : {
12649 : 391175 : set_linkage_for_static_data_member (decl);
12650 : : /* This function is only called with out-of-class definitions. */
12651 : 391175 : DECL_EXTERNAL (decl) = 0;
12652 : 391175 : check_class_member_definition_namespace (decl);
12653 : : }
12654 : : /* At top level, either `static' or no s.c. makes a definition
12655 : : (perhaps tentative), and absence of `static' makes it public. */
12656 : 56962603 : else if (toplevel_bindings_p ())
12657 : : {
12658 : 10049718 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
12659 : 5024859 : && (DECL_THIS_EXTERN (decl)
12660 : 4424849 : || ! constp
12661 : 4424849 : || volatilep
12662 : 4350483 : || inlinep
12663 : 364657 : || in_template_context
12664 : 27681 : || processing_specialization
12665 : 27590 : || module_attach_p ()));
12666 : 5024859 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
12667 : : }
12668 : : /* Not at top level, only `static' makes a static definition. */
12669 : : else
12670 : : {
12671 : 51937744 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
12672 : 51937744 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
12673 : : }
12674 : :
12675 : 57353778 : set_originating_module (decl);
12676 : :
12677 : 57353778 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
12678 : : {
12679 : 20825 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
12680 : : {
12681 : 20817 : CP_DECL_THREAD_LOCAL_P (decl) = true;
12682 : : // NB: Set a tentative TLS model to avoid tls_model attribute
12683 : : // warnings due to lack of thread storage duration. It will
12684 : : // be updated by cplus_decl_attributes later.
12685 : 20817 : if (!processing_template_decl)
12686 : 20695 : set_decl_tls_model (decl, TLS_MODEL_REAL);
12687 : : }
12688 : 20825 : if (declspecs->gnu_thread_keyword_p)
12689 : 19848 : SET_DECL_GNU_TLS_P (decl);
12690 : : }
12691 : :
12692 : : /* If the type of the decl has no linkage, make sure that we'll
12693 : : notice that in mark_used. */
12694 : 57353778 : if (cxx_dialect > cxx98
12695 : 57086754 : && decl_linkage (decl) != lk_none
12696 : 56519852 : && DECL_LANG_SPECIFIC (decl) == NULL
12697 : 52932327 : && !DECL_EXTERN_C_P (decl)
12698 : 110286105 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
12699 : 5106 : retrofit_lang_decl (decl);
12700 : :
12701 : 57353778 : if (TREE_PUBLIC (decl))
12702 : : {
12703 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12704 : : or enumeration declared in a local scope) shall not be used to
12705 : : declare an entity with linkage.
12706 : :
12707 : : DR 757 relaxes this restriction for C++0x. */
12708 : 5350900 : if (cxx_dialect < cxx11)
12709 : 21222 : no_linkage_error (decl);
12710 : : }
12711 : : else
12712 : 52002878 : DECL_INTERFACE_KNOWN (decl) = 1;
12713 : :
12714 : 57353778 : if (DECL_NAME (decl)
12715 : 57353778 : && MAIN_NAME_P (DECL_NAME (decl)))
12716 : : {
12717 : 12 : if (scope == global_namespace)
12718 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12719 : : "cannot declare %<::main%> to be a global variable");
12720 : 6 : else if (DECL_EXTERN_C_P (decl))
12721 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
12722 : : "an entity named %<main%> cannot be declared with "
12723 : : "C language linkage");
12724 : : }
12725 : :
12726 : : /* Check if a variable is being declared as a concept. */
12727 : 57353778 : if (conceptp)
12728 : : {
12729 : 27 : if (!processing_template_decl)
12730 : 11 : error_at (declspecs->locations[ds_concept],
12731 : : "a non-template variable cannot be %<concept%>");
12732 : 16 : else if (!at_namespace_scope_p ())
12733 : 1 : error_at (declspecs->locations[ds_concept],
12734 : : "concept must be defined at namespace scope");
12735 : : else
12736 : 15 : error_at (declspecs->locations[ds_concept],
12737 : : "variable concepts are no longer supported");
12738 : 27 : return NULL_TREE;
12739 : : }
12740 : 57353751 : else if (flag_concepts
12741 : 57353751 : && current_template_depth > template_class_depth (scope))
12742 : : {
12743 : 16347143 : tree ci = current_template_constraints ();
12744 : 16347143 : set_constraints (decl, ci);
12745 : : }
12746 : :
12747 : : // Handle explicit specializations and instantiations of variable templates.
12748 : 57353751 : if (orig_declarator)
12749 : 56779235 : decl = check_explicit_specialization (orig_declarator, decl,
12750 : : template_count, 0);
12751 : :
12752 : 57353751 : return decl != error_mark_node ? decl : NULL_TREE;
12753 : : }
12754 : :
12755 : : /* Create and return a canonical pointer to member function type, for
12756 : : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
12757 : :
12758 : : tree
12759 : 3290581 : build_ptrmemfunc_type (tree type)
12760 : : {
12761 : 3290581 : tree field, fields;
12762 : 3290581 : tree t;
12763 : :
12764 : 3290581 : if (type == error_mark_node)
12765 : : return type;
12766 : :
12767 : : /* Make sure that we always have the unqualified pointer-to-member
12768 : : type first. */
12769 : 3290581 : if (cp_cv_quals quals = cp_type_quals (type))
12770 : : {
12771 : 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
12772 : 0 : return cp_build_qualified_type (unqual, quals);
12773 : : }
12774 : :
12775 : : /* If a canonical type already exists for this type, use it. We use
12776 : : this method instead of type_hash_canon, because it only does a
12777 : : simple equality check on the list of field members. */
12778 : :
12779 : 3290581 : t = TYPE_PTRMEMFUNC_TYPE (type);
12780 : 3290581 : if (t)
12781 : : return t;
12782 : :
12783 : 1072400 : t = make_node (RECORD_TYPE);
12784 : :
12785 : : /* Let the front end know this is a pointer to member function. */
12786 : 1072400 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
12787 : :
12788 : 1072400 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
12789 : 1072400 : DECL_NONADDRESSABLE_P (field) = 1;
12790 : 1072400 : fields = field;
12791 : :
12792 : 1072400 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
12793 : : delta_type_node);
12794 : 1072400 : DECL_NONADDRESSABLE_P (field) = 1;
12795 : 1072400 : DECL_CHAIN (field) = fields;
12796 : 1072400 : fields = field;
12797 : :
12798 : 1072400 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
12799 : :
12800 : : /* Zap out the name so that the back end will give us the debugging
12801 : : information for this anonymous RECORD_TYPE. */
12802 : 1072400 : TYPE_NAME (t) = NULL_TREE;
12803 : :
12804 : : /* Cache this pointer-to-member type so that we can find it again
12805 : : later. */
12806 : 1072400 : TYPE_PTRMEMFUNC_TYPE (type) = t;
12807 : :
12808 : 1072400 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
12809 : 536760 : SET_TYPE_STRUCTURAL_EQUALITY (t);
12810 : 535640 : else if (TYPE_CANONICAL (type) != type)
12811 : 348235 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
12812 : :
12813 : : return t;
12814 : : }
12815 : :
12816 : : /* Create and return a pointer to data member type. */
12817 : :
12818 : : tree
12819 : 317214 : build_ptrmem_type (tree class_type, tree member_type)
12820 : : {
12821 : 317214 : if (TREE_CODE (member_type) == METHOD_TYPE)
12822 : : {
12823 : 62814 : cp_cv_quals quals = type_memfn_quals (member_type);
12824 : 62814 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
12825 : 62814 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
12826 : 62814 : return build_ptrmemfunc_type (build_pointer_type (member_type));
12827 : : }
12828 : : else
12829 : : {
12830 : 254400 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
12831 : 254400 : return build_offset_type (class_type, member_type);
12832 : : }
12833 : : }
12834 : :
12835 : : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
12836 : : Check to see that the definition is valid. Issue appropriate error
12837 : : messages. */
12838 : :
12839 : : static void
12840 : 14006686 : check_static_variable_definition (tree decl, tree type)
12841 : : {
12842 : : /* Avoid redundant diagnostics on out-of-class definitions. */
12843 : 14006686 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
12844 : : ;
12845 : : /* Can't check yet if we don't know the type. */
12846 : 13083228 : else if (dependent_type_p (type))
12847 : : ;
12848 : : /* If DECL is declared constexpr, we'll do the appropriate checks
12849 : : in check_initializer. Similarly for inline static data members. */
12850 : 12789914 : else if (DECL_P (decl)
12851 : 12789914 : && (DECL_DECLARED_CONSTEXPR_P (decl)
12852 : 2382561 : || DECL_VAR_DECLARED_INLINE_P (decl)))
12853 : : ;
12854 : 2382000 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12855 : : {
12856 : 56 : if (!COMPLETE_TYPE_P (type))
12857 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12858 : : "in-class initialization of static data member %q#D of "
12859 : : "incomplete type", decl);
12860 : 45 : else if (literal_type_p (type))
12861 : 39 : permerror (DECL_SOURCE_LOCATION (decl),
12862 : : "%<constexpr%> needed for in-class initialization of "
12863 : : "static data member %q#D of non-integral type", decl);
12864 : : else
12865 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12866 : : "in-class initialization of static data member %q#D of "
12867 : : "non-literal type", decl);
12868 : : }
12869 : : /* Motion 10 at San Diego: If a static const integral data member is
12870 : : initialized with an integral constant expression, the initializer
12871 : : may appear either in the declaration (within the class), or in
12872 : : the definition, but not both. If it appears in the class, the
12873 : : member is a member constant. The file-scope definition is always
12874 : : required. */
12875 : 2381944 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
12876 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12877 : : "invalid in-class initialization of static data member "
12878 : : "of non-integral type %qT",
12879 : : type);
12880 : 2381933 : else if (!CP_TYPE_CONST_P (type))
12881 : 5 : error_at (DECL_SOURCE_LOCATION (decl),
12882 : : "ISO C++ forbids in-class initialization of non-const "
12883 : : "static member %qD",
12884 : : decl);
12885 : 2381928 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12886 : 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12887 : : "ISO C++ forbids initialization of member constant "
12888 : : "%qD of non-integral type %qT", decl, type);
12889 : 14006686 : }
12890 : :
12891 : : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
12892 : : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
12893 : : expressions out into temporary variables so that walk_tree doesn't
12894 : : step into them (c++/15764). */
12895 : :
12896 : : static tree
12897 : 3244 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
12898 : : {
12899 : 3244 : hash_set<tree> *pset = (hash_set<tree> *)data;
12900 : 3244 : tree expr = *expr_p;
12901 : 3244 : if (TREE_CODE (expr) == SAVE_EXPR)
12902 : : {
12903 : 1403 : tree op = TREE_OPERAND (expr, 0);
12904 : 1403 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
12905 : 1403 : if (TREE_SIDE_EFFECTS (op))
12906 : 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
12907 : 1403 : *walk_subtrees = 0;
12908 : : }
12909 : 1841 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
12910 : 1666 : *walk_subtrees = 0;
12911 : 3244 : return NULL;
12912 : : }
12913 : :
12914 : : /* Entry point for the above. */
12915 : :
12916 : : static void
12917 : 1462 : stabilize_vla_size (tree size)
12918 : : {
12919 : 1462 : hash_set<tree> pset;
12920 : : /* Break out any function calls into temporary variables. */
12921 : 1462 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12922 : 1462 : }
12923 : :
12924 : : /* Reduce a SIZEOF_EXPR to its value. */
12925 : :
12926 : : tree
12927 : 9765439 : fold_sizeof_expr (tree t)
12928 : : {
12929 : 9765439 : tree r;
12930 : 9765439 : if (SIZEOF_EXPR_TYPE_P (t))
12931 : 9048361 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12932 : 9048361 : TREE_TYPE (TREE_OPERAND (t, 0)),
12933 : : SIZEOF_EXPR, false, false);
12934 : 717078 : else if (TYPE_P (TREE_OPERAND (t, 0)))
12935 : 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12936 : 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12937 : : false, false);
12938 : : else
12939 : 717078 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12940 : 717078 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12941 : : false, false);
12942 : 9765439 : if (r == error_mark_node)
12943 : 63 : r = size_one_node;
12944 : 9765439 : r = cp_fold_convert (size_type_node, r);
12945 : 9765439 : return r;
12946 : : }
12947 : :
12948 : : /* Given the SIZE (i.e., number of elements) in an array, compute
12949 : : an appropriate index type for the array. If non-NULL, NAME is
12950 : : the name of the entity being declared. */
12951 : :
12952 : : static tree
12953 : 2789876 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12954 : : tsubst_flags_t complain)
12955 : : {
12956 : 2789876 : if (error_operand_p (size))
12957 : 37 : return error_mark_node;
12958 : :
12959 : : /* The type of the index being computed. */
12960 : 2789839 : tree itype;
12961 : :
12962 : : /* The original numeric size as seen in the source code before
12963 : : conversion to size_t. */
12964 : 2789839 : tree origsize = size;
12965 : :
12966 : 2789839 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
12967 : :
12968 : 2789839 : if (!type_dependent_expression_p (size))
12969 : : {
12970 : 2750201 : origsize = size = mark_rvalue_use (size);
12971 : :
12972 : 35852 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
12973 : 2750357 : && TREE_SIDE_EFFECTS (size))
12974 : : /* In C++98, we mark a non-constant array bound with a magic
12975 : : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
12976 : : else
12977 : : {
12978 : 2750200 : size = build_converted_constant_expr (size_type_node, size, complain);
12979 : : /* Pedantically a constant expression is required here and so
12980 : : __builtin_is_constant_evaluated () should fold to true if it
12981 : : is successfully folded into a constant. */
12982 : 2750200 : size = fold_non_dependent_expr (size, complain,
12983 : : /*manifestly_const_eval=*/true);
12984 : :
12985 : 2750200 : if (!TREE_CONSTANT (size))
12986 : 118863 : size = origsize;
12987 : : }
12988 : :
12989 : 2750201 : if (error_operand_p (size))
12990 : 3 : return error_mark_node;
12991 : :
12992 : : /* The array bound must be an integer type. */
12993 : 2750198 : tree type = TREE_TYPE (size);
12994 : 2750198 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
12995 : : {
12996 : 21 : if (!(complain & tf_error))
12997 : 0 : return error_mark_node;
12998 : 21 : if (name)
12999 : 18 : error_at (loc, "size of array %qD has non-integral type %qT",
13000 : : name, type);
13001 : : else
13002 : 3 : error_at (loc, "size of array has non-integral type %qT", type);
13003 : 21 : size = integer_one_node;
13004 : : }
13005 : : }
13006 : :
13007 : : /* A type is dependent if it is...an array type constructed from any
13008 : : dependent type or whose size is specified by a constant expression
13009 : : that is value-dependent. */
13010 : : /* We can only call value_dependent_expression_p on integral constant
13011 : : expressions. */
13012 : 2789836 : if (processing_template_decl
13013 : 1188550 : && potential_constant_expression (size)
13014 : 3978361 : && value_dependent_expression_p (size))
13015 : : {
13016 : : /* Just build the index type and mark that it requires
13017 : : structural equality checks. */
13018 : 696363 : in_template:
13019 : 696363 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13020 : : size, size_one_node));
13021 : 696363 : TYPE_DEPENDENT_P (itype) = 1;
13022 : 696363 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13023 : 696363 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
13024 : 696363 : return itype;
13025 : : }
13026 : :
13027 : 2107925 : if (TREE_CODE (size) != INTEGER_CST)
13028 : : {
13029 : 16310 : tree folded = cp_fully_fold (size);
13030 : 16310 : if (TREE_CODE (folded) == INTEGER_CST)
13031 : : {
13032 : 207 : if (name)
13033 : 133 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13034 : : "integral constant-expression", name);
13035 : : else
13036 : 74 : pedwarn (loc, OPT_Wpedantic,
13037 : : "size of array is not an integral constant-expression");
13038 : : }
13039 : 16310 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13040 : : /* We might have lost the TREE_CONSTANT flag e.g. when we are
13041 : : folding a conversion from a pointer to integral type. In that
13042 : : case issue an error below and don't treat this as a VLA. */;
13043 : : else
13044 : : /* Use the folded result for VLAs, too; it will have resolved
13045 : : SIZEOF_EXPR. */
13046 : : size = folded;
13047 : : }
13048 : :
13049 : : /* Normally, the array-bound will be a constant. */
13050 : 2107925 : if (TREE_CODE (size) == INTEGER_CST)
13051 : : {
13052 : : /* The size to use in diagnostics that reflects the constant
13053 : : size used in the source, rather than SIZE massaged above. */
13054 : 2091822 : tree diagsize = size;
13055 : :
13056 : : /* If the original size before conversion to size_t was signed
13057 : : and negative, convert it to ssizetype to restore the sign. */
13058 : 2091822 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13059 : : && TREE_CODE (size) == INTEGER_CST
13060 : 2091822 : && tree_int_cst_sign_bit (size))
13061 : : {
13062 : 226 : diagsize = fold_convert (ssizetype, size);
13063 : :
13064 : : /* Clear the overflow bit that may have been set as a result
13065 : : of the conversion from the sizetype of the new size to
13066 : : ssizetype. */
13067 : 226 : TREE_OVERFLOW (diagsize) = false;
13068 : : }
13069 : :
13070 : : /* Verify that the array has a positive number of elements
13071 : : and issue the appropriate diagnostic if it doesn't. */
13072 : 2091822 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13073 : : {
13074 : 457 : if (!(complain & tf_error))
13075 : 15 : return error_mark_node;
13076 : 442 : size = integer_one_node;
13077 : : }
13078 : : /* As an extension we allow zero-sized arrays. */
13079 : 2091365 : else if (integer_zerop (size))
13080 : : {
13081 : 3168 : if (!(complain & tf_error))
13082 : : /* We must fail if performing argument deduction (as
13083 : : indicated by the state of complain), so that
13084 : : another substitution can be found. */
13085 : 8 : return error_mark_node;
13086 : 3160 : else if (name)
13087 : 2890 : pedwarn (loc, OPT_Wpedantic,
13088 : : "ISO C++ forbids zero-size array %qD", name);
13089 : : else
13090 : 270 : pedwarn (loc, OPT_Wpedantic,
13091 : : "ISO C++ forbids zero-size array");
13092 : : }
13093 : : }
13094 : 16103 : else if (TREE_CONSTANT (size)
13095 : : /* We don't allow VLAs at non-function scopes, or during
13096 : : tentative template substitution. */
13097 : 16088 : || !at_function_scope_p ()
13098 : 32023 : || !(complain & tf_error))
13099 : : {
13100 : 189 : if (!(complain & tf_error))
13101 : 144 : return error_mark_node;
13102 : : /* `(int) &fn' is not a valid array bound. */
13103 : 45 : if (name)
13104 : 37 : error_at (loc,
13105 : : "size of array %qD is not an integral constant-expression",
13106 : : name);
13107 : : else
13108 : 8 : error_at (loc, "size of array is not an integral constant-expression");
13109 : 45 : size = integer_one_node;
13110 : : }
13111 : 15914 : else if (pedantic && warn_vla != 0)
13112 : : {
13113 : 30 : if (name)
13114 : 15 : pedwarn (name_loc, OPT_Wvla,
13115 : : "ISO C++ forbids variable length array %qD", name);
13116 : : else
13117 : 15 : pedwarn (input_location, OPT_Wvla,
13118 : : "ISO C++ forbids variable length array");
13119 : : }
13120 : 15884 : else if (warn_vla > 0)
13121 : : {
13122 : 3 : if (name)
13123 : 3 : warning_at (name_loc, OPT_Wvla,
13124 : : "variable length array %qD is used", name);
13125 : : else
13126 : 0 : warning (OPT_Wvla,
13127 : : "variable length array is used");
13128 : : }
13129 : :
13130 : 2107758 : if (processing_template_decl && !TREE_CONSTANT (size))
13131 : 14452 : goto in_template;
13132 : : else
13133 : : {
13134 : 2093306 : if (!TREE_CONSTANT (size))
13135 : : {
13136 : : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13137 : : of the MINUS_EXPR, which allows the -1 to get folded with the +1
13138 : : that happens when building TYPE_SIZE. */
13139 : 1462 : size = variable_size (size);
13140 : 1462 : stabilize_vla_size (size);
13141 : : }
13142 : :
13143 : : /* Compute the index of the largest element in the array. It is
13144 : : one less than the number of elements in the array. We save
13145 : : and restore PROCESSING_TEMPLATE_DECL so that computations in
13146 : : cp_build_binary_op will be appropriately folded. */
13147 : 2093306 : {
13148 : 2093306 : processing_template_decl_sentinel s;
13149 : 2093306 : itype = cp_build_binary_op (input_location,
13150 : : MINUS_EXPR,
13151 : : cp_convert (ssizetype, size, complain),
13152 : : cp_convert (ssizetype, integer_one_node,
13153 : : complain),
13154 : : complain);
13155 : 2093306 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13156 : 2093306 : }
13157 : :
13158 : 2093306 : if (!TREE_CONSTANT (itype))
13159 : : {
13160 : 1462 : if (sanitize_flags_p (SANITIZE_VLA)
13161 : 1462 : && current_function_decl != NULL_TREE)
13162 : : {
13163 : : /* We have to add 1 -- in the ubsan routine we generate
13164 : : LE_EXPR rather than LT_EXPR. */
13165 : 69 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13166 : : build_one_cst (TREE_TYPE (itype)));
13167 : 69 : t = ubsan_instrument_vla (input_location, t);
13168 : 69 : finish_expr_stmt (t);
13169 : : }
13170 : : }
13171 : : /* Make sure that there was no overflow when creating to a signed
13172 : : index type. (For example, on a 32-bit machine, an array with
13173 : : size 2^32 - 1 is too big.) */
13174 : 2091844 : else if (TREE_CODE (itype) == INTEGER_CST
13175 : 2091844 : && TREE_OVERFLOW (itype))
13176 : : {
13177 : 0 : if (!(complain & tf_error))
13178 : 0 : return error_mark_node;
13179 : 0 : error ("overflow in array dimension");
13180 : 0 : TREE_OVERFLOW (itype) = 0;
13181 : : }
13182 : : }
13183 : :
13184 : : /* Create and return the appropriate index type. */
13185 : 2093306 : itype = build_index_type (itype);
13186 : :
13187 : : /* If the index type were dependent, we would have returned early, so
13188 : : remember that it isn't. */
13189 : 2093306 : TYPE_DEPENDENT_P (itype) = 0;
13190 : 2093306 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13191 : 2093306 : return itype;
13192 : : }
13193 : :
13194 : : tree
13195 : 335383 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13196 : : {
13197 : 335383 : return compute_array_index_type_loc (input_location, name, size, complain);
13198 : : }
13199 : :
13200 : : /* Returns the scope (if any) in which the entity declared by
13201 : : DECLARATOR will be located. If the entity was declared with an
13202 : : unqualified name, NULL_TREE is returned. */
13203 : :
13204 : : tree
13205 : 692968506 : get_scope_of_declarator (const cp_declarator *declarator)
13206 : : {
13207 : 808820655 : while (declarator && declarator->kind != cdk_id)
13208 : 115852149 : declarator = declarator->declarator;
13209 : :
13210 : : /* If the declarator-id is a SCOPE_REF, the scope in which the
13211 : : declaration occurs is the first operand. */
13212 : 692968506 : if (declarator
13213 : 691747659 : && declarator->u.id.qualifying_scope)
13214 : : return declarator->u.id.qualifying_scope;
13215 : :
13216 : : /* Otherwise, the declarator is not a qualified name; the entity will
13217 : : be declared in the current scope. */
13218 : : return NULL_TREE;
13219 : : }
13220 : :
13221 : : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
13222 : : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13223 : : with this type. */
13224 : :
13225 : : static tree
13226 : 3189775 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13227 : : {
13228 : 3189775 : tree itype = NULL_TREE;
13229 : :
13230 : : /* If things have already gone awry, bail now. */
13231 : 3189775 : if (type == error_mark_node || size == error_mark_node)
13232 : : return error_mark_node;
13233 : :
13234 : : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13235 : : for a deduced class type. */
13236 : 3189712 : if (template_placeholder_p (type))
13237 : : {
13238 : 6 : if (name)
13239 : 0 : error_at (loc, "%qD declared as array of template placeholder "
13240 : : "type %qT", name, type);
13241 : : else
13242 : 6 : error ("creating array of template placeholder type %qT", type);
13243 : 6 : return error_mark_node;
13244 : : }
13245 : :
13246 : : /* If there are some types which cannot be array elements,
13247 : : issue an error-message and return. */
13248 : 3189706 : switch (TREE_CODE (type))
13249 : : {
13250 : 3 : case VOID_TYPE:
13251 : 3 : if (name)
13252 : 0 : error_at (loc, "declaration of %qD as array of void", name);
13253 : : else
13254 : 3 : error ("creating array of void");
13255 : 3 : return error_mark_node;
13256 : :
13257 : 6 : case FUNCTION_TYPE:
13258 : 6 : if (name)
13259 : 6 : error_at (loc, "declaration of %qD as array of functions", name);
13260 : : else
13261 : 0 : error ("creating array of functions");
13262 : 6 : return error_mark_node;
13263 : :
13264 : 21 : case REFERENCE_TYPE:
13265 : 21 : if (name)
13266 : 15 : error_at (loc, "declaration of %qD as array of references", name);
13267 : : else
13268 : 6 : error ("creating array of references");
13269 : 21 : return error_mark_node;
13270 : :
13271 : 0 : case METHOD_TYPE:
13272 : 0 : if (name)
13273 : 0 : error_at (loc, "declaration of %qD as array of function members",
13274 : : name);
13275 : : else
13276 : 0 : error ("creating array of function members");
13277 : 0 : return error_mark_node;
13278 : :
13279 : 3189676 : default:
13280 : 3189676 : break;
13281 : : }
13282 : :
13283 : 3189676 : if (!verify_type_context (name ? loc : input_location,
13284 : : TCTX_ARRAY_ELEMENT, type))
13285 : 0 : return error_mark_node;
13286 : :
13287 : : /* [dcl.array]
13288 : :
13289 : : The constant expressions that specify the bounds of the arrays
13290 : : can be omitted only for the first member of the sequence. */
13291 : 3189676 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13292 : : {
13293 : 30 : if (name)
13294 : 24 : error_at (loc, "declaration of %qD as multidimensional array must "
13295 : : "have bounds for all dimensions except the first",
13296 : : name);
13297 : : else
13298 : 6 : error ("multidimensional array must have bounds for all "
13299 : : "dimensions except the first");
13300 : :
13301 : 30 : return error_mark_node;
13302 : : }
13303 : :
13304 : : /* Figure out the index type for the array. */
13305 : 3189646 : if (size)
13306 : : {
13307 : 2454493 : itype = compute_array_index_type_loc (loc, name, size,
13308 : : tf_warning_or_error);
13309 : 2454493 : if (type_uses_auto (type)
13310 : 2454493 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13311 : : {
13312 : 9 : sorry_at (loc, "variable-length array of %<auto%>");
13313 : 9 : return error_mark_node;
13314 : : }
13315 : : }
13316 : :
13317 : 3189637 : return build_cplus_array_type (type, itype);
13318 : : }
13319 : :
13320 : : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
13321 : :
13322 : : static location_t
13323 : 932833710 : min_location (location_t loca, location_t locb)
13324 : : {
13325 : 932833710 : if (loca == UNKNOWN_LOCATION
13326 : 932833710 : || (locb != UNKNOWN_LOCATION
13327 : 103129937 : && linemap_location_before_p (line_table, locb, loca)))
13328 : 831163116 : return locb;
13329 : : return loca;
13330 : : }
13331 : :
13332 : : /* Returns the smallest location != UNKNOWN_LOCATION among the
13333 : : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13334 : : and LOCATIONS[ds_restrict]. */
13335 : :
13336 : : static location_t
13337 : 931250818 : smallest_type_quals_location (int type_quals, const location_t* locations)
13338 : : {
13339 : 931250818 : location_t loc = UNKNOWN_LOCATION;
13340 : :
13341 : 931250818 : if (type_quals & TYPE_QUAL_CONST)
13342 : 101736033 : loc = locations[ds_const];
13343 : :
13344 : 931250818 : if (type_quals & TYPE_QUAL_VOLATILE)
13345 : 1582801 : loc = min_location (loc, locations[ds_volatile]);
13346 : :
13347 : 931250818 : if (type_quals & TYPE_QUAL_RESTRICT)
13348 : 43 : loc = min_location (loc, locations[ds_restrict]);
13349 : :
13350 : 931250818 : return loc;
13351 : : }
13352 : :
13353 : : /* Returns the smallest among the latter and locations[ds_type_spec]. */
13354 : :
13355 : : static location_t
13356 : 931250797 : smallest_type_location (int type_quals, const location_t* locations)
13357 : : {
13358 : 931250797 : location_t loc = smallest_type_quals_location (type_quals, locations);
13359 : 931250797 : return min_location (loc, locations[ds_type_spec]);
13360 : : }
13361 : :
13362 : : static location_t
13363 : 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
13364 : : {
13365 : 26 : int type_quals = get_type_quals (declspecs);
13366 : 26 : return smallest_type_location (type_quals, declspecs->locations);
13367 : : }
13368 : :
13369 : : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
13370 : : strip out the pointer/reference declarator(s). */
13371 : :
13372 : : static bool
13373 : 27330569 : maybe_strip_indirect_ref (const cp_declarator** declarator)
13374 : : {
13375 : 27330569 : bool indirect_ref_p = false;
13376 : 54661285 : while (declarator && *declarator
13377 : 27330716 : && ((*declarator)->kind == cdk_pointer
13378 : 27330716 : || (*declarator)->kind == cdk_reference))
13379 : : {
13380 : 147 : indirect_ref_p = true;
13381 : 147 : *declarator = (*declarator)->declarator;
13382 : : }
13383 : 27330569 : return indirect_ref_p;
13384 : : }
13385 : :
13386 : : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13387 : : and DECLARATOR. SFK indicates the kind of special function (if any) that
13388 : : this function is. OPTYPE is the type given in a conversion operator
13389 : : declaration, or the class type for a constructor/destructor.
13390 : : Returns the actual return type of the function; that may be different
13391 : : than TYPE if an error occurs, or for certain special functions. */
13392 : :
13393 : : static tree
13394 : 27330569 : check_special_function_return_type (special_function_kind sfk,
13395 : : tree type,
13396 : : tree optype,
13397 : : int type_quals,
13398 : : const cp_declarator** declarator,
13399 : : const location_t* locations)
13400 : : {
13401 : 27330569 : gcc_assert (declarator);
13402 : 27330569 : location_t rettype_loc = (type
13403 : 27330569 : ? smallest_type_location (type_quals, locations)
13404 : 27330488 : : (*declarator)->id_loc);
13405 : 27330569 : switch (sfk)
13406 : : {
13407 : 22418598 : case sfk_constructor:
13408 : 22418598 : if (maybe_strip_indirect_ref (declarator) || type)
13409 : 81 : error_at (rettype_loc,
13410 : : "return type specification for constructor invalid");
13411 : 22418517 : else if (type_quals != TYPE_UNQUALIFIED)
13412 : 15 : error_at (smallest_type_quals_location (type_quals, locations),
13413 : : "qualifiers are not allowed on constructor declaration");
13414 : :
13415 : 22418598 : if (targetm.cxx.cdtor_returns_this ())
13416 : 0 : type = build_pointer_type (optype);
13417 : : else
13418 : 22418598 : type = void_type_node;
13419 : : break;
13420 : :
13421 : 3188790 : case sfk_destructor:
13422 : 3188790 : if (maybe_strip_indirect_ref (declarator) || type)
13423 : 45 : error_at (rettype_loc,
13424 : : "return type specification for destructor invalid");
13425 : 3188745 : else if (type_quals != TYPE_UNQUALIFIED)
13426 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
13427 : : "qualifiers are not allowed on destructor declaration");
13428 : :
13429 : : /* We can't use the proper return type here because we run into
13430 : : problems with ambiguous bases and covariant returns. */
13431 : 3188790 : if (targetm.cxx.cdtor_returns_this ())
13432 : 0 : type = build_pointer_type (void_type_node);
13433 : : else
13434 : 3188790 : type = void_type_node;
13435 : : break;
13436 : :
13437 : 638416 : case sfk_conversion:
13438 : 638416 : if (maybe_strip_indirect_ref (declarator) || type)
13439 : 42 : error_at (rettype_loc,
13440 : : "return type specified for %<operator %T%>", optype);
13441 : 638374 : else if (type_quals != TYPE_UNQUALIFIED)
13442 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
13443 : : "qualifiers are not allowed on declaration of "
13444 : : "%<operator %T%>", optype);
13445 : :
13446 : : type = optype;
13447 : : break;
13448 : :
13449 : 1084765 : case sfk_deduction_guide:
13450 : 1084765 : if (maybe_strip_indirect_ref (declarator) || type)
13451 : 0 : error_at (rettype_loc,
13452 : : "return type specified for deduction guide");
13453 : 1084765 : else if (type_quals != TYPE_UNQUALIFIED)
13454 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
13455 : : "qualifiers are not allowed on declaration of "
13456 : : "deduction guide");
13457 : 1084765 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13458 : : {
13459 : 3 : error ("template template parameter %qT in declaration of "
13460 : : "deduction guide", optype);
13461 : 3 : type = error_mark_node;
13462 : : }
13463 : : else
13464 : 1084762 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13465 : 29288655 : for (int i = 0; i < ds_last; ++i)
13466 : 28203890 : if (i != ds_explicit && locations[i])
13467 : 3 : error_at (locations[i],
13468 : : "%<decl-specifier%> in declaration of deduction guide");
13469 : : break;
13470 : :
13471 : 0 : default:
13472 : 0 : gcc_unreachable ();
13473 : : }
13474 : :
13475 : 27330569 : return type;
13476 : : }
13477 : :
13478 : : /* A variable or data member (whose unqualified name is IDENTIFIER)
13479 : : has been declared with the indicated TYPE. If the TYPE is not
13480 : : acceptable, issue an error message and return a type to use for
13481 : : error-recovery purposes. */
13482 : :
13483 : : tree
13484 : 289857073 : check_var_type (tree identifier, tree type, location_t loc)
13485 : : {
13486 : 289857073 : if (VOID_TYPE_P (type))
13487 : : {
13488 : 88 : if (!identifier)
13489 : 0 : error_at (loc, "unnamed variable or field declared void");
13490 : 88 : else if (identifier_p (identifier))
13491 : : {
13492 : 88 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13493 : 88 : error_at (loc, "variable or field %qE declared void",
13494 : : identifier);
13495 : : }
13496 : : else
13497 : 0 : error_at (loc, "variable or field declared void");
13498 : 88 : type = error_mark_node;
13499 : : }
13500 : :
13501 : 289857073 : return type;
13502 : : }
13503 : :
13504 : : /* Handle declaring DECL as an inline variable. */
13505 : :
13506 : : static void
13507 : 4043499 : mark_inline_variable (tree decl, location_t loc)
13508 : : {
13509 : 4043499 : bool inlinep = true;
13510 : 4043499 : if (! toplevel_bindings_p ())
13511 : : {
13512 : 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13513 : : "%qD declared at block scope", decl);
13514 : 24 : inlinep = false;
13515 : : }
13516 : 4043475 : else if (cxx_dialect < cxx17)
13517 : 648 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13518 : : "with %<-std=c++17%> or %<-std=gnu++17%>");
13519 : 672 : if (inlinep)
13520 : : {
13521 : 4043475 : retrofit_lang_decl (decl);
13522 : 4043475 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13523 : : }
13524 : 4043499 : }
13525 : :
13526 : :
13527 : : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13528 : : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13529 : : ORIG is the anonymous TYPE_NAME before that change. */
13530 : :
13531 : : static bool
13532 : 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13533 : : {
13534 : 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13535 : 42 : tree name = DECL_NAME (TYPE_NAME (type));
13536 : 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13537 : 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13538 : : "anonymous non-C-compatible type given name for linkage "
13539 : 42 : "purposes by %<typedef%> declaration");
13540 : 42 : }
13541 : :
13542 : : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13543 : : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13544 : : to TYPE but during recursion can be set to nested classes. */
13545 : :
13546 : : static bool
13547 : 520156 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13548 : : {
13549 : 520156 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13550 : : {
13551 : 6 : auto_diagnostic_group d;
13552 : 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13553 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13554 : : "type is not C-compatible because it has a base class");
13555 : 6 : return true;
13556 : 6 : }
13557 : 2465333 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13558 : 1945244 : switch (TREE_CODE (field))
13559 : : {
13560 : 9 : case VAR_DECL:
13561 : : /* static data members have been diagnosed already. */
13562 : 9 : continue;
13563 : 1296363 : case FIELD_DECL:
13564 : 1296363 : if (DECL_INITIAL (field))
13565 : : {
13566 : 4 : auto_diagnostic_group d;
13567 : 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13568 : 4 : inform (DECL_SOURCE_LOCATION (field),
13569 : : "type is not C-compatible because %qD has default "
13570 : : "member initializer", field);
13571 : 4 : return true;
13572 : 4 : }
13573 : 1296359 : continue;
13574 : 9 : case CONST_DECL:
13575 : 9 : continue;
13576 : 648786 : case TYPE_DECL:
13577 : 648786 : if (DECL_SELF_REFERENCE_P (field))
13578 : 520124 : continue;
13579 : 128662 : if (DECL_IMPLICIT_TYPEDEF_P (field))
13580 : : {
13581 : 128652 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13582 : 3 : continue;
13583 : 128649 : if (CLASS_TYPE_P (TREE_TYPE (field)))
13584 : : {
13585 : 128649 : tree tf = TREE_TYPE (field);
13586 : 128649 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13587 : : tf))
13588 : : return true;
13589 : 128624 : continue;
13590 : 128624 : }
13591 : : }
13592 : : /* FALLTHRU */
13593 : 32 : case FUNCTION_DECL:
13594 : 32 : case TEMPLATE_DECL:
13595 : 32 : {
13596 : 32 : auto_diagnostic_group d;
13597 : 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13598 : 32 : inform (DECL_SOURCE_LOCATION (field),
13599 : : "type is not C-compatible because it contains %qD "
13600 : : "declaration", field);
13601 : 32 : return true;
13602 : 32 : }
13603 : : default:
13604 : : break;
13605 : 1296377 : }
13606 : : return false;
13607 : : }
13608 : :
13609 : : /* Assign a typedef-given name to a class or enumeration type declared
13610 : : as anonymous at first. This was split out of grokdeclarator
13611 : : because it is also used in libcc1. */
13612 : :
13613 : : void
13614 : 393287 : name_unnamed_type (tree type, tree decl)
13615 : : {
13616 : 1179861 : gcc_assert (TYPE_UNNAMED_P (type)
13617 : : || enum_with_enumerator_for_linkage_p (type));
13618 : :
13619 : : /* Replace the anonymous decl with the real decl. Be careful not to
13620 : : rename other typedefs (such as the self-reference) of type. */
13621 : 393287 : tree orig = TYPE_NAME (type);
13622 : 1178140 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13623 : 784853 : if (TYPE_NAME (t) == orig)
13624 : 393287 : TYPE_NAME (t) = decl;
13625 : :
13626 : : /* If this is a typedef within a template class, the nested
13627 : : type is a (non-primary) template. The name for the
13628 : : template needs updating as well. */
13629 : 393287 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
13630 : 64 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
13631 : :
13632 : : /* Adjust linkage now that we aren't unnamed anymore. */
13633 : 393287 : reset_type_linkage (type);
13634 : :
13635 : 393287 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
13636 : 391507 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
13637 : :
13638 : : /* FIXME remangle member functions; member functions of a
13639 : : type with external linkage have external linkage. */
13640 : :
13641 : : /* Check that our job is done, and that it would fail if we
13642 : : attempted to do it again. */
13643 : 786574 : gcc_assert (!TYPE_UNNAMED_P (type)
13644 : : && !enum_with_enumerator_for_linkage_p (type));
13645 : 393287 : }
13646 : :
13647 : : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
13648 : : is allowed. TYPE might contain a decltype(auto). Returns true if
13649 : : there was a problem, false otherwise. */
13650 : :
13651 : : static bool
13652 : 586636720 : check_decltype_auto (location_t loc, tree type)
13653 : : {
13654 : 586636720 : if (tree a = type_uses_auto (type))
13655 : : {
13656 : 10348035 : if (AUTO_IS_DECLTYPE (a))
13657 : : {
13658 : 337566 : if (a != type)
13659 : : {
13660 : 33 : error_at (loc, "%qT as type rather than plain "
13661 : : "%<decltype(auto)%>", type);
13662 : 33 : return true;
13663 : : }
13664 : 337533 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
13665 : : {
13666 : 35 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
13667 : 35 : return true;
13668 : : }
13669 : : }
13670 : : }
13671 : : return false;
13672 : : }
13673 : :
13674 : : /* Issue an error about two mutually incompatible declspecs
13675 : : with the given names and locations
13676 : : e.g. "error: `signed' and `unsigned' specified together" */
13677 : :
13678 : : static void
13679 : 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
13680 : : const char *name_b, location_t loc_b)
13681 : : {
13682 : 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
13683 : 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
13684 : : nullptr, highlight_colors::rhs);
13685 : 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
13686 : 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
13687 : 66 : error_at (&richloc, "%e and %e specified together",
13688 : : &e_name_a, &e_name_b);
13689 : 66 : }
13690 : :
13691 : : /* Given declspecs and a declarator (abstract or otherwise), determine
13692 : : the name and type of the object declared and construct a DECL node
13693 : : for it.
13694 : :
13695 : : DECLSPECS points to the representation of declaration-specifier
13696 : : sequence that precedes declarator.
13697 : :
13698 : : DECL_CONTEXT says which syntactic context this declaration is in:
13699 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
13700 : : FUNCDEF for a function definition. Like NORMAL but a few different
13701 : : error messages in each case. Return value may be zero meaning
13702 : : this definition is too screwy to try to parse.
13703 : : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
13704 : : handle member functions (which have FIELD context).
13705 : : Return value may be zero meaning this definition is too screwy to
13706 : : try to parse.
13707 : : PARM for a parameter declaration (either within a function prototype
13708 : : or before a function body). Make a PARM_DECL, or return void_type_node.
13709 : : TPARM for a template parameter declaration.
13710 : : CATCHPARM for a parameter declaration before a catch clause.
13711 : : TYPENAME if for a typename (in a cast or sizeof).
13712 : : Don't make a DECL node; just return the ..._TYPE node.
13713 : : FIELD for a struct or union field; make a FIELD_DECL.
13714 : : BITFIELD for a field with specified width.
13715 : :
13716 : : INITIALIZED is as for start_decl.
13717 : :
13718 : : ATTRLIST is a pointer to the list of attributes, which may be NULL
13719 : : if there are none; *ATTRLIST may be modified if attributes from inside
13720 : : the declarator should be applied to the declaration.
13721 : :
13722 : : When this function is called, scoping variables (such as
13723 : : CURRENT_CLASS_TYPE) should reflect the scope in which the
13724 : : declaration occurs, not the scope in which the new declaration will
13725 : : be placed. For example, on:
13726 : :
13727 : : void S::f() { ... }
13728 : :
13729 : : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
13730 : : should not be `S'.
13731 : :
13732 : : Returns a DECL (if a declarator is present), a TYPE (if there is no
13733 : : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
13734 : : error occurs. */
13735 : :
13736 : : tree
13737 : 931250690 : grokdeclarator (const cp_declarator *declarator,
13738 : : cp_decl_specifier_seq *declspecs,
13739 : : enum decl_context decl_context,
13740 : : int initialized,
13741 : : tree* attrlist)
13742 : : {
13743 : 931250690 : tree type = NULL_TREE;
13744 : 931250690 : int longlong = 0;
13745 : 931250690 : int explicit_intN = 0;
13746 : 931250690 : int int_n_alt = 0;
13747 : 931250690 : int virtualp, explicitp, friendp, inlinep, staticp;
13748 : 931250690 : int explicit_int = 0;
13749 : 931250690 : int explicit_char = 0;
13750 : 931250690 : int defaulted_int = 0;
13751 : :
13752 : 931250690 : tree typedef_decl = NULL_TREE;
13753 : 931250690 : const char *name = NULL;
13754 : 931250690 : tree typedef_type = NULL_TREE;
13755 : : /* True if this declarator is a function definition. */
13756 : 931250690 : bool funcdef_flag = false;
13757 : 931250690 : cp_declarator_kind innermost_code = cdk_error;
13758 : 931250690 : int bitfield = 0;
13759 : : #if 0
13760 : : /* See the code below that used this. */
13761 : : tree decl_attr = NULL_TREE;
13762 : : #endif
13763 : :
13764 : : /* Keep track of what sort of function is being processed
13765 : : so that we can warn about default return values, or explicit
13766 : : return values which do not match prescribed defaults. */
13767 : 931250690 : special_function_kind sfk = sfk_none;
13768 : :
13769 : 931250690 : tree dname = NULL_TREE;
13770 : 931250690 : tree ctor_return_type = NULL_TREE;
13771 : 931250690 : enum overload_flags flags = NO_SPECIAL;
13772 : : /* cv-qualifiers that apply to the declarator, for a declaration of
13773 : : a member function. */
13774 : 931250690 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
13775 : : /* virt-specifiers that apply to the declarator, for a declaration of
13776 : : a member function. */
13777 : 931250690 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
13778 : : /* ref-qualifier that applies to the declarator, for a declaration of
13779 : : a member function. */
13780 : 931250690 : cp_ref_qualifier rqual = REF_QUAL_NONE;
13781 : : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
13782 : 931250690 : int type_quals = get_type_quals (declspecs);
13783 : 931250690 : tree raises = NULL_TREE;
13784 : 931250690 : int template_count = 0;
13785 : 931250690 : tree returned_attrs = NULL_TREE;
13786 : 931250690 : tree parms = NULL_TREE;
13787 : 931250690 : const cp_declarator *id_declarator;
13788 : : /* The unqualified name of the declarator; either an
13789 : : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
13790 : 931250690 : tree unqualified_id;
13791 : : /* The class type, if any, in which this entity is located,
13792 : : or NULL_TREE if none. Note that this value may be different from
13793 : : the current class type; for example if an attempt is made to declare
13794 : : "A::f" inside "B", this value will be "A". */
13795 : 931250690 : tree ctype = current_class_type;
13796 : : /* The NAMESPACE_DECL for the namespace in which this entity is
13797 : : located. If an unqualified name is used to declare the entity,
13798 : : this value will be NULL_TREE, even if the entity is located at
13799 : : namespace scope. */
13800 : 931250690 : tree in_namespace = NULL_TREE;
13801 : 931250690 : cp_storage_class storage_class;
13802 : 931250690 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
13803 : 931250690 : bool type_was_error_mark_node = false;
13804 : 931250690 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
13805 : 931250690 : bool template_type_arg = false;
13806 : 931250690 : bool template_parm_flag = false;
13807 : 931250690 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
13808 : 931250690 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
13809 : 931250690 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
13810 : 931250690 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
13811 : 931250690 : bool late_return_type_p = false;
13812 : 931250690 : bool array_parameter_p = false;
13813 : 931250690 : tree reqs = NULL_TREE;
13814 : :
13815 : 931250690 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
13816 : 931250690 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
13817 : 931250690 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
13818 : 931250690 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
13819 : 931250690 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
13820 : 931250690 : explicit_intN = declspecs->explicit_intN_p;
13821 : 931250690 : int_n_alt = declspecs->int_n_alt;
13822 : 931250690 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
13823 : :
13824 : : // Was concept_p specified? Note that ds_concept
13825 : : // implies ds_constexpr!
13826 : 931250690 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
13827 : 931250690 : if (concept_p)
13828 : 114 : constexpr_p = true;
13829 : :
13830 : 931250690 : if (decl_context == FUNCDEF)
13831 : : funcdef_flag = true, decl_context = NORMAL;
13832 : : else if (decl_context == MEMFUNCDEF)
13833 : : funcdef_flag = true, decl_context = FIELD;
13834 : : else if (decl_context == BITFIELD)
13835 : : bitfield = 1, decl_context = FIELD;
13836 : : else if (decl_context == TEMPLATE_TYPE_ARG)
13837 : : template_type_arg = true, decl_context = TYPENAME;
13838 : : else if (decl_context == TPARM)
13839 : 12070269 : template_parm_flag = true, decl_context = PARM;
13840 : :
13841 : 931250690 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
13842 : 9401302 : funcdef_flag = true;
13843 : :
13844 : 931250690 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
13845 : : (type_quals,
13846 : 931250690 : declspecs->locations));
13847 : 931250690 : location_t id_loc;
13848 : 931250690 : location_t init_loc;
13849 : 931250690 : if (declarator)
13850 : : {
13851 : 577006929 : id_loc = loc_or_input_loc (declarator->id_loc);
13852 : 577006929 : init_loc = loc_or_input_loc (declarator->init_loc);
13853 : : }
13854 : : else
13855 : 354243761 : init_loc = id_loc = input_location;
13856 : :
13857 : : /* Look inside a declarator for the name being declared
13858 : : and get it as a string, for an error message. */
13859 : 931250690 : for (id_declarator = declarator;
13860 : 1293910874 : id_declarator;
13861 : 362660184 : id_declarator = id_declarator->declarator)
13862 : : {
13863 : 902502086 : if (id_declarator->kind != cdk_id)
13864 : 362660209 : innermost_code = id_declarator->kind;
13865 : :
13866 : 902502086 : switch (id_declarator->kind)
13867 : : {
13868 : 177931578 : case cdk_function:
13869 : 177931578 : if (id_declarator->declarator
13870 : 177456966 : && id_declarator->declarator->kind == cdk_id)
13871 : : {
13872 : 175579507 : sfk = id_declarator->declarator->u.id.sfk;
13873 : 175579507 : if (sfk == sfk_destructor)
13874 : 3188793 : flags = DTOR_FLAG;
13875 : : }
13876 : : break;
13877 : :
13878 : 539841877 : case cdk_id:
13879 : 539841877 : {
13880 : 539841877 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
13881 : 539841877 : tree decl = id_declarator->u.id.unqualified_name;
13882 : 539841877 : if (!decl)
13883 : : break;
13884 : 539841877 : if (qualifying_scope)
13885 : : {
13886 : 9780650 : if (check_for_bare_parameter_packs (qualifying_scope,
13887 : 9780650 : id_declarator->id_loc))
13888 : 6 : return error_mark_node;
13889 : 9780644 : if (at_function_scope_p ())
13890 : : {
13891 : : /* [dcl.meaning]
13892 : :
13893 : : A declarator-id shall not be qualified except
13894 : : for ...
13895 : :
13896 : : None of the cases are permitted in block
13897 : : scope. */
13898 : 0 : if (qualifying_scope == global_namespace)
13899 : 0 : error ("invalid use of qualified-name %<::%D%>",
13900 : : decl);
13901 : 0 : else if (TYPE_P (qualifying_scope))
13902 : 0 : error ("invalid use of qualified-name %<%T::%D%>",
13903 : : qualifying_scope, decl);
13904 : : else
13905 : 0 : error ("invalid use of qualified-name %<%D::%D%>",
13906 : : qualifying_scope, decl);
13907 : 0 : return error_mark_node;
13908 : : }
13909 : 9780644 : else if (TYPE_P (qualifying_scope))
13910 : : {
13911 : 9595690 : ctype = qualifying_scope;
13912 : 9595690 : if (!MAYBE_CLASS_TYPE_P (ctype))
13913 : : {
13914 : 6 : error_at (id_declarator->id_loc,
13915 : : "%q#T is not a class or namespace", ctype);
13916 : 6 : ctype = NULL_TREE;
13917 : : }
13918 : 9595684 : else if (innermost_code != cdk_function
13919 : 391227 : && current_class_type
13920 : 9595699 : && !uniquely_derived_from_p (ctype,
13921 : : current_class_type))
13922 : : {
13923 : 9 : error_at (id_declarator->id_loc,
13924 : : "invalid use of qualified-name %<%T::%D%>",
13925 : : qualifying_scope, decl);
13926 : 9 : return error_mark_node;
13927 : : }
13928 : : }
13929 : 184954 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
13930 : 539841862 : in_namespace = qualifying_scope;
13931 : : }
13932 : 539841862 : switch (TREE_CODE (decl))
13933 : : {
13934 : 3188808 : case BIT_NOT_EXPR:
13935 : 3188808 : {
13936 : 3188808 : if (innermost_code != cdk_function)
13937 : : {
13938 : 6 : error_at (EXPR_LOCATION (decl),
13939 : : "declaration of %qE as non-function", decl);
13940 : 6 : return error_mark_node;
13941 : : }
13942 : 3188802 : else if (!qualifying_scope
13943 : 3188802 : && !(current_class_type && at_class_scope_p ()))
13944 : : {
13945 : 9 : error_at (EXPR_LOCATION (decl),
13946 : : "declaration of %qE as non-member", decl);
13947 : 9 : return error_mark_node;
13948 : : }
13949 : :
13950 : 3188793 : tree type = TREE_OPERAND (decl, 0);
13951 : 3188793 : if (TYPE_P (type))
13952 : 3188790 : type = constructor_name (type);
13953 : 3188793 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
13954 : 3188793 : dname = decl;
13955 : : }
13956 : 3188793 : break;
13957 : :
13958 : 2032094 : case TEMPLATE_ID_EXPR:
13959 : 2032094 : {
13960 : 2032094 : tree fns = TREE_OPERAND (decl, 0);
13961 : :
13962 : 2032094 : dname = fns;
13963 : 2947379 : if (!identifier_p (dname))
13964 : 2032079 : dname = OVL_NAME (dname);
13965 : : }
13966 : : /* Fall through. */
13967 : :
13968 : 536653054 : case IDENTIFIER_NODE:
13969 : 536653054 : if (identifier_p (decl))
13970 : 534620960 : dname = decl;
13971 : :
13972 : 536653054 : if (IDENTIFIER_KEYWORD_P (dname))
13973 : : {
13974 : 0 : error ("declarator-id missing; using reserved word %qD",
13975 : : dname);
13976 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13977 : : }
13978 : 536653054 : else if (!IDENTIFIER_CONV_OP_P (dname))
13979 : 536014635 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13980 : : else
13981 : : {
13982 : 638419 : gcc_assert (flags == NO_SPECIAL);
13983 : 638419 : flags = TYPENAME_FLAG;
13984 : 638419 : sfk = sfk_conversion;
13985 : 638419 : tree glob = get_global_binding (dname);
13986 : 638419 : if (glob && TREE_CODE (glob) == TYPE_DECL)
13987 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13988 : : else
13989 : : name = "<invalid operator>";
13990 : : }
13991 : : break;
13992 : :
13993 : 0 : default:
13994 : 0 : gcc_unreachable ();
13995 : : }
13996 : : break;
13997 : : }
13998 : :
13999 : : case cdk_array:
14000 : : case cdk_pointer:
14001 : : case cdk_reference:
14002 : : case cdk_ptrmem:
14003 : : break;
14004 : :
14005 : : case cdk_decomp:
14006 : 902502031 : name = "structured binding";
14007 : : break;
14008 : :
14009 : 25 : case cdk_error:
14010 : 25 : return error_mark_node;
14011 : :
14012 : 0 : default:
14013 : 0 : gcc_unreachable ();
14014 : : }
14015 : 902502031 : if (id_declarator->kind == cdk_id)
14016 : : break;
14017 : : }
14018 : :
14019 : : /* [dcl.fct.edf]
14020 : :
14021 : : The declarator in a function-definition shall have the form
14022 : : D1 ( parameter-declaration-clause) ... */
14023 : 931250635 : if (funcdef_flag && innermost_code != cdk_function)
14024 : : {
14025 : 3 : error_at (id_loc, "function definition does not declare parameters");
14026 : 3 : return error_mark_node;
14027 : : }
14028 : :
14029 : 931250632 : if (flags == TYPENAME_FLAG
14030 : 931250632 : && innermost_code != cdk_function
14031 : 3 : && ! (ctype && !declspecs->any_specifiers_p))
14032 : : {
14033 : 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
14034 : 3 : return error_mark_node;
14035 : : }
14036 : :
14037 : 931250629 : if (dname && identifier_p (dname))
14038 : : {
14039 : 536653048 : if (UDLIT_OPER_P (dname)
14040 : 536653048 : && innermost_code != cdk_function)
14041 : : {
14042 : 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
14043 : 6 : return error_mark_node;
14044 : : }
14045 : :
14046 : 536653042 : if (IDENTIFIER_ANY_OP_P (dname))
14047 : : {
14048 : 32257335 : if (typedef_p)
14049 : : {
14050 : 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14051 : 3 : return error_mark_node;
14052 : : }
14053 : 32257332 : else if (decl_context == PARM || decl_context == CATCHPARM)
14054 : : {
14055 : 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
14056 : 15 : return error_mark_node;
14057 : : }
14058 : : }
14059 : : }
14060 : :
14061 : : /* Anything declared one level down from the top level
14062 : : must be one of the parameters of a function
14063 : : (because the body is at least two levels down). */
14064 : :
14065 : : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14066 : : by not allowing C++ class definitions to specify their parameters
14067 : : with xdecls (must be spec.d in the parmlist).
14068 : :
14069 : : Since we now wait to push a class scope until we are sure that
14070 : : we are in a legitimate method context, we must set oldcname
14071 : : explicitly (since current_class_name is not yet alive).
14072 : :
14073 : : We also want to avoid calling this a PARM if it is in a namespace. */
14074 : :
14075 : 931250605 : if (decl_context == NORMAL && !toplevel_bindings_p ())
14076 : : {
14077 : 63234506 : cp_binding_level *b = current_binding_level;
14078 : 63234506 : current_binding_level = b->level_chain;
14079 : 63234506 : if (current_binding_level != 0 && toplevel_bindings_p ())
14080 : : decl_context = PARM;
14081 : 63234506 : current_binding_level = b;
14082 : : }
14083 : :
14084 : 931250605 : if (name == NULL)
14085 : 391321847 : name = decl_context == PARM ? "parameter" : "type name";
14086 : :
14087 : 931250605 : if (consteval_p && constexpr_p)
14088 : : {
14089 : 6 : error_at (declspecs->locations[ds_consteval],
14090 : : "both %qs and %qs specified", "constexpr", "consteval");
14091 : 6 : return error_mark_node;
14092 : : }
14093 : :
14094 : 931250599 : if (concept_p && typedef_p)
14095 : : {
14096 : 9 : error_at (declspecs->locations[ds_concept],
14097 : : "%qs cannot appear in a typedef declaration", "concept");
14098 : 9 : return error_mark_node;
14099 : : }
14100 : :
14101 : 931250590 : if (constexpr_p && typedef_p)
14102 : : {
14103 : 3 : error_at (declspecs->locations[ds_constexpr],
14104 : : "%qs cannot appear in a typedef declaration", "constexpr");
14105 : 3 : return error_mark_node;
14106 : : }
14107 : :
14108 : 931250587 : if (consteval_p && typedef_p)
14109 : : {
14110 : 3 : error_at (declspecs->locations[ds_consteval],
14111 : : "%qs cannot appear in a typedef declaration", "consteval");
14112 : 3 : return error_mark_node;
14113 : : }
14114 : :
14115 : 931250584 : if (constinit_p && typedef_p)
14116 : : {
14117 : 3 : error_at (declspecs->locations[ds_constinit],
14118 : : "%qs cannot appear in a typedef declaration", "constinit");
14119 : 3 : return error_mark_node;
14120 : : }
14121 : :
14122 : : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14123 : : keywords shall appear in a decl-specifier-seq." */
14124 : 931250581 : if (constinit_p && constexpr_p)
14125 : : {
14126 : 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14127 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14128 : 6 : error_at (&richloc,
14129 : : "can use at most one of the %<constinit%> and %<constexpr%> "
14130 : : "specifiers");
14131 : 6 : return error_mark_node;
14132 : 6 : }
14133 : :
14134 : : /* If there were multiple types specified in the decl-specifier-seq,
14135 : : issue an error message. */
14136 : 931250575 : if (declspecs->multiple_types_p)
14137 : : {
14138 : 1500 : error_at (typespec_loc,
14139 : : "two or more data types in declaration of %qs", name);
14140 : 1500 : return error_mark_node;
14141 : : }
14142 : :
14143 : 931249075 : if (declspecs->conflicting_specifiers_p)
14144 : 31 : return error_mark_node;
14145 : :
14146 : : /* Extract the basic type from the decl-specifier-seq. */
14147 : 931249044 : type = declspecs->type;
14148 : 931249044 : if (type == error_mark_node)
14149 : : {
14150 : 1175 : type = NULL_TREE;
14151 : 1175 : type_was_error_mark_node = true;
14152 : : }
14153 : :
14154 : : /* Ignore erroneous attributes. */
14155 : 931249044 : if (attrlist && *attrlist == error_mark_node)
14156 : 12 : *attrlist = NULL_TREE;
14157 : :
14158 : : /* An object declared as __attribute__((unavailable)) suppresses
14159 : : any reports of being declared with unavailable or deprecated
14160 : : items. An object declared as __attribute__((deprecated))
14161 : : suppresses warnings of uses of other deprecated items. */
14162 : 931249044 : auto ds = make_temp_override (deprecated_state);
14163 : 931249044 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
14164 : 159 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14165 : 931248885 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
14166 : 717166 : deprecated_state = DEPRECATED_SUPPRESS;
14167 : :
14168 : 931249044 : cp_handle_deprecated_or_unavailable (type);
14169 : 931249044 : if (type && TREE_CODE (type) == TYPE_DECL)
14170 : : {
14171 : 638855402 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14172 : 638855402 : typedef_decl = type;
14173 : 638855402 : type = TREE_TYPE (typedef_decl);
14174 : 638855402 : if (DECL_ARTIFICIAL (typedef_decl))
14175 : 460645754 : cp_handle_deprecated_or_unavailable (type);
14176 : : }
14177 : : /* No type at all: default to `int', and set DEFAULTED_INT
14178 : : because it was not a user-defined typedef. */
14179 : 931249044 : if (type == NULL_TREE)
14180 : : {
14181 : 35571467 : if (signed_p || unsigned_p || long_p || short_p)
14182 : : {
14183 : : /* These imply 'int'. */
14184 : 8239322 : type = integer_type_node;
14185 : 8239322 : defaulted_int = 1;
14186 : : }
14187 : : /* If we just have "complex", it is equivalent to "complex double". */
14188 : 27332145 : else if (!longlong && !explicit_intN
14189 : 27332145 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14190 : : {
14191 : 81 : type = double_type_node;
14192 : 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14193 : : "ISO C++ does not support plain %<complex%> meaning "
14194 : : "%<double complex%>");
14195 : : }
14196 : : }
14197 : : /* Gather flags. */
14198 : 931249044 : explicit_int = declspecs->explicit_int_p;
14199 : 931249044 : explicit_char = declspecs->explicit_char_p;
14200 : :
14201 : : #if 0
14202 : : /* See the code below that used this. */
14203 : : if (typedef_decl)
14204 : : decl_attr = DECL_ATTRIBUTES (typedef_decl);
14205 : : #endif
14206 : 931249044 : typedef_type = type;
14207 : :
14208 : 931249044 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14209 : 1723181 : ctor_return_type = TREE_TYPE (dname);
14210 : : else
14211 : : ctor_return_type = ctype;
14212 : :
14213 : 931249044 : if (sfk != sfk_none)
14214 : : {
14215 : 27330569 : type = check_special_function_return_type (sfk, type,
14216 : : ctor_return_type,
14217 : : type_quals,
14218 : : &declarator,
14219 : : declspecs->locations);
14220 : 27330569 : type_quals = TYPE_UNQUALIFIED;
14221 : : }
14222 : 903918475 : else if (type == NULL_TREE)
14223 : : {
14224 : 1576 : int is_main;
14225 : :
14226 : 1576 : explicit_int = -1;
14227 : :
14228 : : /* We handle `main' specially here, because 'main () { }' is so
14229 : : common. With no options, it is allowed. With -Wreturn-type,
14230 : : it is a warning. It is only an error with -pedantic-errors. */
14231 : 3152 : is_main = (funcdef_flag
14232 : 3387 : && dname && identifier_p (dname)
14233 : 235 : && MAIN_NAME_P (dname)
14234 : 68 : && ctype == NULL_TREE
14235 : 68 : && in_namespace == NULL_TREE
14236 : 1644 : && current_namespace == global_namespace);
14237 : :
14238 : 1576 : if (type_was_error_mark_node)
14239 : : /* We've already issued an error, don't complain more. */;
14240 : 401 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
14241 : : /* Allow it, sigh. */;
14242 : 339 : else if (! is_main)
14243 : 277 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14244 : : name);
14245 : 62 : else if (pedantic)
14246 : 4 : pedwarn (id_loc, OPT_Wpedantic,
14247 : : "ISO C++ forbids declaration of %qs with no type", name);
14248 : : else
14249 : 58 : warning_at (id_loc, OPT_Wreturn_type,
14250 : : "ISO C++ forbids declaration of %qs with no type", name);
14251 : :
14252 : 1576 : if (type_was_error_mark_node && template_parm_flag)
14253 : : /* FIXME we should be able to propagate the error_mark_node as is
14254 : : for other contexts too. */
14255 : 151 : type = error_mark_node;
14256 : : else
14257 : 1425 : type = integer_type_node;
14258 : : }
14259 : :
14260 : 931249044 : ctype = NULL_TREE;
14261 : :
14262 : 931249044 : if (explicit_intN)
14263 : : {
14264 : 964561 : if (! int_n_enabled_p[declspecs->int_n_idx])
14265 : : {
14266 : 0 : error_at (declspecs->locations[ds_type_spec],
14267 : : "%<__int%d%> is not supported by this target",
14268 : 0 : int_n_data[declspecs->int_n_idx].bitsize);
14269 : 0 : explicit_intN = false;
14270 : : }
14271 : : /* Don't pedwarn if the alternate "__intN__" form has been used instead
14272 : : of "__intN". */
14273 : 964561 : else if (!int_n_alt && pedantic)
14274 : 3928 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14275 : : "ISO C++ does not support %<__int%d%> for %qs",
14276 : 3928 : int_n_data[declspecs->int_n_idx].bitsize, name);
14277 : : }
14278 : :
14279 : : /* Now process the modifiers that were specified
14280 : : and check for invalid combinations. */
14281 : :
14282 : : /* Long double is a special combination. */
14283 : 931249044 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14284 : : {
14285 : 9168457 : long_p = false;
14286 : 9168457 : type = cp_build_qualified_type (long_double_type_node,
14287 : : cp_type_quals (type));
14288 : : }
14289 : :
14290 : : /* Check all other uses of type modifiers. */
14291 : :
14292 : 931249044 : if (unsigned_p || signed_p || long_p || short_p)
14293 : : {
14294 : 19804414 : location_t loc;
14295 : 19804414 : const char *key;
14296 : 19804414 : if (unsigned_p)
14297 : : {
14298 : 13070693 : key = "unsigned";
14299 : 13070693 : loc = declspecs->locations[ds_unsigned];
14300 : : }
14301 : 6733721 : else if (signed_p)
14302 : : {
14303 : 1088600 : key = "signed";
14304 : 1088600 : loc = declspecs->locations[ds_signed];
14305 : : }
14306 : 5645121 : else if (longlong)
14307 : : {
14308 : 2192143 : key = "long long";
14309 : 2192143 : loc = declspecs->locations[ds_long_long];
14310 : : }
14311 : 3452978 : else if (long_p)
14312 : : {
14313 : 2934331 : key = "long";
14314 : 2934331 : loc = declspecs->locations[ds_long];
14315 : : }
14316 : : else /* if (short_p) */
14317 : : {
14318 : 518647 : key = "short";
14319 : 518647 : loc = declspecs->locations[ds_short];
14320 : : }
14321 : :
14322 : 19804414 : int ok = 0;
14323 : :
14324 : 19804414 : if (signed_p && unsigned_p)
14325 : 54 : complain_about_incompatible_declspecs
14326 : 54 : ("signed", declspecs->locations[ds_signed],
14327 : : "unsigned", declspecs->locations[ds_unsigned]);
14328 : 19804360 : else if (long_p && short_p)
14329 : 12 : complain_about_incompatible_declspecs
14330 : 12 : ("long", declspecs->locations[ds_long],
14331 : : "short", declspecs->locations[ds_short]);
14332 : 19804348 : else if (TREE_CODE (type) != INTEGER_TYPE
14333 : 19804335 : || type == char8_type_node
14334 : 19804311 : || type == char16_type_node
14335 : 19804287 : || type == char32_type_node
14336 : 19804263 : || ((long_p || short_p)
14337 : 10747866 : && (explicit_char || explicit_intN)))
14338 : 418 : error_at (loc, "%qs specified with %qT", key, type);
14339 : 19803930 : else if (!explicit_int && !defaulted_int
14340 : 19803930 : && !explicit_char && !explicit_intN)
14341 : : {
14342 : 30 : if (typedef_decl)
14343 : : {
14344 : 27 : pedwarn (loc, OPT_Wpedantic,
14345 : : "%qs specified with typedef-name %qD",
14346 : : key, typedef_decl);
14347 : 27 : ok = !flag_pedantic_errors;
14348 : : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14349 : : and is not built as a typedef. */
14350 : 27 : if (is_typedef_decl (typedef_decl))
14351 : 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
14352 : : }
14353 : 3 : else if (declspecs->decltype_p)
14354 : 0 : error_at (loc, "%qs specified with %<decltype%>", key);
14355 : : else
14356 : 3 : error_at (loc, "%qs specified with %<typeof%>", key);
14357 : : }
14358 : : else
14359 : : ok = 1;
14360 : :
14361 : : /* Discard the type modifiers if they are invalid. */
14362 : 514 : if (! ok)
14363 : : {
14364 : : unsigned_p = false;
14365 : : signed_p = false;
14366 : : long_p = false;
14367 : : short_p = false;
14368 : : longlong = 0;
14369 : : }
14370 : : }
14371 : :
14372 : : /* Decide whether an integer type is signed or not.
14373 : : Optionally treat bitfields as signed by default. */
14374 : 931248554 : if (unsigned_p
14375 : : /* [class.bit]
14376 : :
14377 : : It is implementation-defined whether a plain (neither
14378 : : explicitly signed or unsigned) char, short, int, or long
14379 : : bit-field is signed or unsigned.
14380 : :
14381 : : Naturally, we extend this to long long as well. Note that
14382 : : this does not include wchar_t. */
14383 : 931249044 : || (bitfield && !flag_signed_bitfields
14384 : 18 : && !signed_p
14385 : : /* A typedef for plain `int' without `signed' can be
14386 : : controlled just like plain `int', but a typedef for
14387 : : `signed int' cannot be so controlled. */
14388 : 18 : && !(typedef_decl
14389 : 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14390 : 13 : && TREE_CODE (type) == INTEGER_TYPE
14391 : 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14392 : : {
14393 : 13070478 : if (explicit_intN)
14394 : 457491 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14395 : 12612987 : else if (longlong)
14396 : 1374953 : type = long_long_unsigned_type_node;
14397 : 11238034 : else if (long_p)
14398 : 2186441 : type = long_unsigned_type_node;
14399 : 9051593 : else if (short_p)
14400 : 1290097 : type = short_unsigned_type_node;
14401 : 7761496 : else if (type == char_type_node)
14402 : 1560517 : type = unsigned_char_type_node;
14403 : 6200979 : else if (typedef_decl)
14404 : 25 : type = c_common_unsigned_type (type);
14405 : : else
14406 : 6200954 : type = unsigned_type_node;
14407 : : }
14408 : 918178566 : else if (signed_p && type == char_type_node)
14409 : 675226 : type = signed_char_type_node;
14410 : 917503340 : else if (explicit_intN)
14411 : 507070 : type = int_n_trees[declspecs->int_n_idx].signed_type;
14412 : 916996270 : else if (longlong)
14413 : 2269808 : type = long_long_integer_type_node;
14414 : 914726462 : else if (long_p)
14415 : 3015485 : type = long_integer_type_node;
14416 : 911710977 : else if (short_p)
14417 : 610749 : type = short_integer_type_node;
14418 : 911100228 : else if (signed_p && typedef_decl)
14419 : 9 : type = c_common_signed_type (type);
14420 : :
14421 : 931249044 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14422 : : {
14423 : 1431911 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14424 : 3 : error_at (declspecs->locations[ds_complex],
14425 : : "complex invalid for %qs", name);
14426 : : /* If a modifier is specified, the resulting complex is the complex
14427 : : form of TYPE. E.g, "complex short" is "complex short int". */
14428 : 1431908 : else if (type == integer_type_node)
14429 : 164 : type = complex_integer_type_node;
14430 : 1431744 : else if (type == float_type_node)
14431 : 361781 : type = complex_float_type_node;
14432 : 1069963 : else if (type == double_type_node)
14433 : 362317 : type = complex_double_type_node;
14434 : 707646 : else if (type == long_double_type_node)
14435 : 361808 : type = complex_long_double_type_node;
14436 : : else
14437 : 345838 : type = build_complex_type (type);
14438 : : }
14439 : :
14440 : : /* If we're using the injected-class-name to form a compound type or a
14441 : : declaration, replace it with the underlying class so we don't get
14442 : : redundant typedefs in the debug output. But if we are returning the
14443 : : type unchanged, leave it alone so that it's available to
14444 : : maybe_get_template_decl_from_type_decl. */
14445 : 184409565 : if (CLASS_TYPE_P (type)
14446 : 184359501 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14447 : 29777585 : && type == TREE_TYPE (TYPE_NAME (type))
14448 : 961026629 : && (declarator || type_quals))
14449 : 28545066 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14450 : :
14451 : 931249044 : type_quals |= cp_type_quals (type);
14452 : 1862498088 : type = cp_build_qualified_type
14453 : 1862498088 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14454 : 753039396 : || declspecs->decltype_p)
14455 : : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14456 : : /* We might have ignored or rejected some of the qualifiers. */
14457 : 931249044 : type_quals = cp_type_quals (type);
14458 : :
14459 : 916294227 : if (cxx_dialect >= cxx17 && type && is_auto (type)
14460 : 13820280 : && innermost_code != cdk_function
14461 : : /* Placeholder in parm gets a better error below. */
14462 : 8048108 : && !(decl_context == PARM || decl_context == CATCHPARM)
14463 : 939252215 : && id_declarator && declarator != id_declarator)
14464 : 1015916 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14465 : : {
14466 : 16 : auto_diagnostic_group g;
14467 : 16 : gcc_rich_location richloc (typespec_loc);
14468 : 16 : richloc.add_fixit_insert_after ("<>");
14469 : 16 : error_at (&richloc, "missing template argument list after %qE; "
14470 : : "for deduction, template placeholder must be followed "
14471 : : "by a simple declarator-id", tmpl);
14472 : 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14473 : 16 : type = error_mark_node;
14474 : 16 : }
14475 : :
14476 : 931249044 : staticp = 0;
14477 : 931249044 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14478 : 931249044 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14479 : 931249044 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14480 : :
14481 : 931249044 : storage_class = declspecs->storage_class;
14482 : 931249044 : if (storage_class == sc_static)
14483 : 19622231 : staticp = 1 + (decl_context == FIELD);
14484 : 911626813 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14485 : : /* Treat class-scope deduction guides as static member functions
14486 : : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14487 : 19297312 : staticp = 2;
14488 : :
14489 : 931249044 : if (virtualp)
14490 : : {
14491 : 4085334 : if (staticp == 2)
14492 : : {
14493 : 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14494 : 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
14495 : 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14496 : : "and %<static%>", dname);
14497 : 21 : storage_class = sc_none;
14498 : 21 : staticp = 0;
14499 : 21 : }
14500 : 4085334 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14501 : : {
14502 : 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14503 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14504 : 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14505 : : "declared both %<virtual%> and %<constexpr%> only in "
14506 : : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14507 : 6 : }
14508 : : }
14509 : 931249044 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14510 : :
14511 : : /* Issue errors about use of storage classes for parameters. */
14512 : 931249044 : if (decl_context == PARM)
14513 : : {
14514 : 279314433 : if (typedef_p)
14515 : : {
14516 : 24 : error_at (declspecs->locations[ds_typedef],
14517 : : "typedef declaration invalid in parameter declaration");
14518 : 24 : return error_mark_node;
14519 : : }
14520 : 279314409 : else if (template_parm_flag && storage_class != sc_none)
14521 : : {
14522 : 16 : error_at (min_location (declspecs->locations[ds_thread],
14523 : : declspecs->locations[ds_storage_class]),
14524 : : "storage class specified for template parameter %qs",
14525 : : name);
14526 : 16 : return error_mark_node;
14527 : : }
14528 : 279314393 : else if (storage_class == sc_static
14529 : 279314393 : || storage_class == sc_extern
14530 : 279314390 : || thread_p)
14531 : : {
14532 : 9 : error_at (min_location (declspecs->locations[ds_thread],
14533 : : declspecs->locations[ds_storage_class]),
14534 : : "storage class specified for parameter %qs", name);
14535 : 9 : return error_mark_node;
14536 : : }
14537 : :
14538 : : /* Function parameters cannot be concept. */
14539 : 279314384 : if (concept_p)
14540 : : {
14541 : 9 : error_at (declspecs->locations[ds_concept],
14542 : : "a parameter cannot be declared %qs", "concept");
14543 : 9 : concept_p = 0;
14544 : 9 : constexpr_p = 0;
14545 : : }
14546 : : /* Function parameters cannot be constexpr. If we saw one, moan
14547 : : and pretend it wasn't there. */
14548 : 279314375 : else if (constexpr_p)
14549 : : {
14550 : 9 : error_at (declspecs->locations[ds_constexpr],
14551 : : "a parameter cannot be declared %qs", "constexpr");
14552 : 9 : constexpr_p = 0;
14553 : : }
14554 : 279314384 : if (constinit_p)
14555 : : {
14556 : 6 : error_at (declspecs->locations[ds_constinit],
14557 : : "a parameter cannot be declared %qs", "constinit");
14558 : 6 : constinit_p = 0;
14559 : : }
14560 : 279314384 : if (consteval_p)
14561 : : {
14562 : 3 : error_at (declspecs->locations[ds_consteval],
14563 : : "a parameter cannot be declared %qs", "consteval");
14564 : 3 : consteval_p = 0;
14565 : : }
14566 : : }
14567 : :
14568 : : /* Give error if `virtual' is used outside of class declaration. */
14569 : 931248995 : if (virtualp
14570 : 4085334 : && (current_class_name == NULL_TREE || decl_context != FIELD))
14571 : : {
14572 : 68 : error_at (declspecs->locations[ds_virtual],
14573 : : "%<virtual%> outside class declaration");
14574 : 68 : virtualp = 0;
14575 : : }
14576 : :
14577 : 931248995 : if (innermost_code == cdk_decomp)
14578 : : {
14579 : 76560 : location_t loc = (declarator->kind == cdk_reference
14580 : 86941 : ? declarator->declarator->id_loc : declarator->id_loc);
14581 : 86941 : if (inlinep)
14582 : 29 : error_at (declspecs->locations[ds_inline],
14583 : : "structured binding declaration cannot be %qs", "inline");
14584 : 86941 : if (typedef_p)
14585 : 29 : error_at (declspecs->locations[ds_typedef],
14586 : : "structured binding declaration cannot be %qs", "typedef");
14587 : 86941 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14588 : 96 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14589 : : "structured binding declaration can be %qs only with "
14590 : : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14591 : 86941 : if (consteval_p)
14592 : 13 : error_at (declspecs->locations[ds_consteval], "structured "
14593 : : "binding declaration cannot be %qs", "consteval");
14594 : 86941 : if (thread_p && cxx_dialect < cxx20)
14595 : 77 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14596 : : "structured binding declaration can be %qs only in "
14597 : : "%<-std=c++20%> or %<-std=gnu++20%>",
14598 : 77 : declspecs->gnu_thread_keyword_p
14599 : : ? "__thread" : "thread_local");
14600 : 86941 : if (concept_p)
14601 : : {
14602 : 0 : error_at (declspecs->locations[ds_concept],
14603 : : "structured binding declaration cannot be %qs", "concept");
14604 : 0 : constexpr_p = 0;
14605 : : }
14606 : : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
14607 : 86941 : if (type_quals & TYPE_QUAL_VOLATILE)
14608 : 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
14609 : : "%<volatile%>-qualified structured binding is deprecated");
14610 : 86941 : switch (storage_class)
14611 : : {
14612 : : case sc_none:
14613 : : break;
14614 : 28 : case sc_register:
14615 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14616 : : "register");
14617 : 28 : break;
14618 : 235 : case sc_static:
14619 : 235 : if (cxx_dialect < cxx20)
14620 : 186 : pedwarn (loc, OPT_Wc__20_extensions,
14621 : : "structured binding declaration can be %qs only in "
14622 : : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
14623 : : break;
14624 : 29 : case sc_extern:
14625 : 29 : error_at (loc, "structured binding declaration cannot be %qs",
14626 : : "extern");
14627 : 29 : break;
14628 : 28 : case sc_mutable:
14629 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14630 : : "mutable");
14631 : 28 : break;
14632 : 0 : case sc_auto:
14633 : 0 : error_at (loc, "structured binding declaration cannot be "
14634 : : "C++98 %<auto%>");
14635 : 0 : break;
14636 : 0 : default:
14637 : 0 : gcc_unreachable ();
14638 : : }
14639 : 86941 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
14640 : 86941 : || TYPE_IDENTIFIER (type) != auto_identifier)
14641 : : {
14642 : 3 : if (type != error_mark_node)
14643 : : {
14644 : 3 : auto_diagnostic_group d;
14645 : 3 : error_at (loc, "structured binding declaration cannot have "
14646 : : "type %qT", type);
14647 : 3 : inform (loc,
14648 : : "type must be cv-qualified %<auto%> or reference to "
14649 : : "cv-qualified %<auto%>");
14650 : 3 : }
14651 : 3 : type = build_qualified_type (make_auto (), type_quals);
14652 : 3 : declspecs->type = type;
14653 : : }
14654 : 86938 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
14655 : 51 : pedwarn (loc, OPT_Wpedantic,
14656 : : "structured binding declaration cannot have constrained "
14657 : : "%<auto%> type %qT", type);
14658 : 86941 : inlinep = 0;
14659 : 86941 : typedef_p = 0;
14660 : 86941 : consteval_p = 0;
14661 : 86941 : concept_p = 0;
14662 : 86941 : if (storage_class != sc_static)
14663 : : {
14664 : 86706 : storage_class = sc_none;
14665 : 86706 : declspecs->storage_class = sc_none;
14666 : : }
14667 : : }
14668 : :
14669 : : /* Static anonymous unions are dealt with here. */
14670 : 931248995 : if (staticp && decl_context == TYPENAME
14671 : 0 : && declspecs->type
14672 : 931248995 : && ANON_AGGR_TYPE_P (declspecs->type))
14673 : : decl_context = FIELD;
14674 : :
14675 : : /* Warn about storage classes that are invalid for certain
14676 : : kinds of declarations (parameters, typenames, etc.). */
14677 : 931248995 : if (thread_p
14678 : 21269 : && ((storage_class
14679 : 21269 : && storage_class != sc_extern
14680 : 524 : && storage_class != sc_static)
14681 : 21258 : || typedef_p))
14682 : : {
14683 : 17 : location_t loc
14684 : 17 : = min_location (declspecs->locations[ds_thread],
14685 : : declspecs->locations[ds_storage_class]);
14686 : 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
14687 : 17 : thread_p = false;
14688 : : }
14689 : 931248995 : if (decl_context != NORMAL
14690 : 774458279 : && ((storage_class != sc_none
14691 : 774458279 : && storage_class != sc_mutable)
14692 : 755160979 : || thread_p))
14693 : : {
14694 : 19297303 : if ((decl_context == PARM || decl_context == CATCHPARM)
14695 : 36 : && (storage_class == sc_register
14696 : 36 : || storage_class == sc_auto))
14697 : : ;
14698 : 19297267 : else if (typedef_p)
14699 : : ;
14700 : 19297267 : else if (decl_context == FIELD
14701 : : /* C++ allows static class elements. */
14702 : 19297267 : && storage_class == sc_static)
14703 : : /* C++ also allows inlines and signed and unsigned elements,
14704 : : but in those cases we don't come in here. */
14705 : : ;
14706 : : else
14707 : : {
14708 : 15 : location_t loc
14709 : 15 : = min_location (declspecs->locations[ds_thread],
14710 : : declspecs->locations[ds_storage_class]);
14711 : 15 : if (decl_context == FIELD)
14712 : 15 : error_at (loc, "storage class specified for %qs", name);
14713 : 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
14714 : 0 : error_at (loc, "storage class specified for parameter %qs", name);
14715 : : else
14716 : 0 : error_at (loc, "storage class specified for typename");
14717 : 15 : if (storage_class == sc_register
14718 : 15 : || storage_class == sc_auto
14719 : 15 : || storage_class == sc_extern
14720 : 3 : || thread_p)
14721 : 15 : storage_class = sc_none;
14722 : : }
14723 : : }
14724 : 911951692 : else if (storage_class == sc_extern && funcdef_flag
14725 : 911951692 : && ! toplevel_bindings_p ())
14726 : 0 : error ("nested function %qs declared %<extern%>", name);
14727 : 911951692 : else if (toplevel_bindings_p ())
14728 : : {
14729 : 420529179 : if (storage_class == sc_auto)
14730 : 2 : error_at (declspecs->locations[ds_storage_class],
14731 : : "top-level declaration of %qs specifies %<auto%>", name);
14732 : : }
14733 : 491422513 : else if (thread_p
14734 : 491422513 : && storage_class != sc_extern
14735 : 223 : && storage_class != sc_static)
14736 : : {
14737 : 190 : if (declspecs->gnu_thread_keyword_p)
14738 : 49 : pedwarn (declspecs->locations[ds_thread],
14739 : 49 : 0, "function-scope %qs implicitly auto and "
14740 : : "declared %<__thread%>", name);
14741 : :
14742 : : /* When thread_local is applied to a variable of block scope the
14743 : : storage-class-specifier static is implied if it does not appear
14744 : : explicitly. */
14745 : 190 : storage_class = declspecs->storage_class = sc_static;
14746 : 190 : staticp = 1;
14747 : : }
14748 : :
14749 : 931248995 : if (storage_class && friendp)
14750 : : {
14751 : 12 : error_at (min_location (declspecs->locations[ds_thread],
14752 : : declspecs->locations[ds_storage_class]),
14753 : : "storage class specifiers invalid in friend function "
14754 : : "declarations");
14755 : 12 : storage_class = sc_none;
14756 : 12 : staticp = 0;
14757 : : }
14758 : :
14759 : 931248995 : if (!id_declarator)
14760 : : unqualified_id = NULL_TREE;
14761 : : else
14762 : : {
14763 : 539840286 : unqualified_id = id_declarator->u.id.unqualified_name;
14764 : 539840286 : switch (TREE_CODE (unqualified_id))
14765 : : {
14766 : 3188793 : case BIT_NOT_EXPR:
14767 : 3188793 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
14768 : 3188793 : if (TYPE_P (unqualified_id))
14769 : 3188790 : unqualified_id = constructor_name (unqualified_id);
14770 : : break;
14771 : :
14772 : : case IDENTIFIER_NODE:
14773 : : case TEMPLATE_ID_EXPR:
14774 : : break;
14775 : :
14776 : 0 : default:
14777 : 0 : gcc_unreachable ();
14778 : : }
14779 : : }
14780 : :
14781 : 931248995 : if (declspecs->std_attributes
14782 : 931248995 : && !diagnose_misapplied_contracts (declspecs->std_attributes))
14783 : : {
14784 : 705 : location_t attr_loc = declspecs->locations[ds_std_attribute];
14785 : 705 : auto_diagnostic_group d;
14786 : 705 : if (any_nonignored_attribute_p (declspecs->std_attributes)
14787 : 705 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
14788 : 702 : inform (attr_loc, "an attribute that appertains to a type-specifier "
14789 : : "is ignored");
14790 : 705 : }
14791 : :
14792 : 931248995 : if (attrlist)
14793 : 918936407 : diagnose_misapplied_contracts (*attrlist);
14794 : :
14795 : : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
14796 : : bool is_xobj_member_function = false;
14797 : : /* Determine the type of the entity declared by recurring on the
14798 : : declarator. */
14799 : 1293820231 : for (; declarator; declarator = declarator->declarator)
14800 : : {
14801 : 902498488 : const cp_declarator *inner_declarator;
14802 : 902498488 : tree attrs;
14803 : :
14804 : 902498488 : if (type == error_mark_node)
14805 : 599 : return error_mark_node;
14806 : :
14807 : 902498162 : attrs = declarator->attributes;
14808 : 902498162 : if (attrs)
14809 : : {
14810 : 13171 : int attr_flags;
14811 : :
14812 : 13171 : attr_flags = 0;
14813 : 13171 : if (declarator->kind == cdk_id)
14814 : 12467 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
14815 : 13171 : if (declarator->kind == cdk_function)
14816 : 687 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
14817 : 13171 : if (declarator->kind == cdk_array)
14818 : 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
14819 : 13171 : tree late_attrs = NULL_TREE;
14820 : 13171 : if (decl_context != PARM && decl_context != TYPENAME)
14821 : : /* Assume that any attributes that get applied late to
14822 : : templates will DTRT when applied to the declaration
14823 : : as a whole. */
14824 : 13159 : late_attrs = splice_template_attributes (&attrs, type);
14825 : 13171 : returned_attrs = decl_attributes (&type,
14826 : : attr_chainon (returned_attrs,
14827 : : attrs),
14828 : : attr_flags);
14829 : 13171 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
14830 : : }
14831 : :
14832 : 902498162 : inner_declarator = declarator->declarator;
14833 : :
14834 : : /* Check that contracts aren't misapplied. */
14835 : 902498162 : if (tree contract_attr = find_contract (declarator->std_attributes))
14836 : 484 : if (declarator->kind != cdk_function
14837 : 479 : || innermost_code != cdk_function)
14838 : 7 : diagnose_misapplied_contracts (contract_attr);
14839 : :
14840 : : /* We don't want to warn in parameter context because we don't
14841 : : yet know if the parse will succeed, and this might turn out
14842 : : to be a constructor call. */
14843 : 902498162 : if (decl_context != PARM
14844 : 902498162 : && decl_context != TYPENAME
14845 : 518931869 : && !typedef_p
14846 : 473308527 : && declarator->parenthesized != UNKNOWN_LOCATION
14847 : : /* If the type is class-like and the inner name used a
14848 : : global namespace qualifier, we need the parens.
14849 : : Unfortunately all we can tell is whether a qualified name
14850 : : was used or not. */
14851 : 902498343 : && !(inner_declarator
14852 : 126 : && inner_declarator->kind == cdk_id
14853 : 108 : && inner_declarator->u.id.qualifying_scope
14854 : 18 : && (MAYBE_CLASS_TYPE_P (type)
14855 : 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
14856 : : {
14857 : 169 : auto_diagnostic_group d;
14858 : 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
14859 : : "unnecessary parentheses in declaration of %qs",
14860 : : name))
14861 : : {
14862 : 42 : gcc_rich_location iloc (declarator->parenthesized);
14863 : 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
14864 : 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
14865 : 42 : inform (&iloc, "remove parentheses");
14866 : 42 : }
14867 : 169 : }
14868 : 902498162 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
14869 : : break;
14870 : :
14871 : 362571509 : switch (declarator->kind)
14872 : : {
14873 : 3189775 : case cdk_array:
14874 : 6379550 : type = create_array_type_for_decl (dname, type,
14875 : 3189775 : declarator->u.array.bounds,
14876 : 3189775 : declarator->id_loc);
14877 : 3189775 : if (!valid_array_size_p (dname
14878 : : ? declarator->id_loc : input_location,
14879 : : type, dname))
14880 : 267 : type = error_mark_node;
14881 : :
14882 : 3189775 : if (declarator->std_attributes)
14883 : : /* [dcl.array]/1:
14884 : :
14885 : : The optional attribute-specifier-seq appertains to the
14886 : : array type. */
14887 : 189 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
14888 : : break;
14889 : :
14890 : 177931542 : case cdk_function:
14891 : 177931542 : {
14892 : 177931542 : tree arg_types;
14893 : 177931542 : int funcdecl_p;
14894 : :
14895 : : /* Declaring a function type. */
14896 : :
14897 : : /* Pick up type qualifiers which should be applied to `this'. */
14898 : 177931542 : memfn_quals = declarator->u.function.qualifiers;
14899 : : /* Pick up virt-specifiers. */
14900 : 177931542 : virt_specifiers = declarator->u.function.virt_specifiers;
14901 : : /* And ref-qualifier, too */
14902 : 177931542 : rqual = declarator->u.function.ref_qualifier;
14903 : : /* And tx-qualifier. */
14904 : 177931542 : tree tx_qual = declarator->u.function.tx_qualifier;
14905 : : /* Pick up the exception specifications. */
14906 : 177931542 : raises = declarator->u.function.exception_specification;
14907 : : /* If the exception-specification is ill-formed, let's pretend
14908 : : there wasn't one. */
14909 : 177931542 : if (raises == error_mark_node)
14910 : 9 : raises = NULL_TREE;
14911 : :
14912 : 355863084 : auto find_xobj_parm = [](tree parm_list)
14913 : : {
14914 : : /* There is no need to iterate over the list,
14915 : : only the first parm can be a valid xobj parm. */
14916 : 355630480 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
14917 : : return NULL_TREE;
14918 : : /* If we make it here, we are looking at an xobj parm.
14919 : :
14920 : : Non-null 'purpose' usually means the parm has a default
14921 : : argument, we don't want to violate this assumption. */
14922 : 58379 : TREE_PURPOSE (parm_list) = NULL_TREE;
14923 : 58379 : return TREE_VALUE (parm_list);
14924 : : };
14925 : :
14926 : 177931542 : tree xobj_parm
14927 : 177931542 : = find_xobj_parm (declarator->u.function.parameters);
14928 : 177931542 : is_xobj_member_function = xobj_parm;
14929 : :
14930 : 177931542 : if (xobj_parm && cxx_dialect < cxx23)
14931 : 24 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
14932 : : "explicit object member function only available "
14933 : : "with %<-std=c++23%> or %<-std=gnu++23%>");
14934 : :
14935 : 177931542 : if (xobj_parm && decl_context == TYPENAME)
14936 : : {
14937 : : /* We inform in every case, just differently depending on what
14938 : : case it is. */
14939 : 6 : auto_diagnostic_group d;
14940 : 6 : bool ptr_type = true;
14941 : : /* If declarator->kind is cdk_function and we are at the end of
14942 : : the declarator chain, we are looking at a function type. */
14943 : 6 : if (!declarator->declarator)
14944 : : {
14945 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14946 : : "a function type cannot "
14947 : : "have an explicit object parameter");
14948 : 2 : ptr_type = false;
14949 : : }
14950 : 4 : else if (declarator->declarator->kind == cdk_pointer)
14951 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14952 : : "a pointer to function type cannot "
14953 : : "have an explicit object parameter");
14954 : 2 : else if (declarator->declarator->kind == cdk_ptrmem)
14955 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14956 : : "a pointer to member function type "
14957 : : "cannot have an explicit object parameter");
14958 : : else
14959 : 0 : gcc_unreachable ();
14960 : :
14961 : : /* The locations being used here are probably not correct. */
14962 : 4 : if (ptr_type)
14963 : 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14964 : : "the type of a pointer to explicit object member "
14965 : : "function is a regular pointer to function type");
14966 : : else
14967 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14968 : : "the type of an explicit object "
14969 : : "member function is a regular function type");
14970 : : /* Ideally we should synthesize the correct syntax
14971 : : for the user, perhaps this could be added later. */
14972 : 6 : }
14973 : : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
14974 : : the first parm node will never erroneously be detected here. */
14975 : 177931542 : {
14976 : 177931542 : auto_diagnostic_group d;
14977 : 177931542 : bool bad_xobj_parm_encountered = false;
14978 : 177931542 : for (tree parm = declarator->u.function.parameters;
14979 : 445749900 : parm && parm != void_list_node;
14980 : 267818358 : parm = TREE_CHAIN (parm))
14981 : : {
14982 : 267818358 : if (TREE_PURPOSE (parm) != this_identifier)
14983 : 267818352 : continue;
14984 : 6 : bad_xobj_parm_encountered = true;
14985 : 6 : TREE_PURPOSE (parm) = NULL_TREE;
14986 : 6 : gcc_rich_location bad_xobj_parm
14987 : 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
14988 : 6 : error_at (&bad_xobj_parm,
14989 : : "only the first parameter of a member function "
14990 : : "can be declared as an explicit object parameter");
14991 : 6 : }
14992 : 177931542 : if (bad_xobj_parm_encountered && xobj_parm)
14993 : 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14994 : : "valid explicit object parameter declared here");
14995 : 177931542 : }
14996 : :
14997 : 177931542 : if (reqs)
14998 : 3 : error_at (location_of (reqs), "requires-clause on return type");
14999 : 177931542 : reqs = declarator->u.function.requires_clause;
15000 : :
15001 : : /* Say it's a definition only for the CALL_EXPR
15002 : : closest to the identifier. */
15003 : 177931542 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15004 : :
15005 : : /* Handle a late-specified return type. */
15006 : 177931542 : tree late_return_type = declarator->u.function.late_return_type;
15007 : 177931542 : if (tree auto_node = type_uses_auto (type))
15008 : : {
15009 : 5783698 : if (!late_return_type)
15010 : : {
15011 : 2687325 : if (!funcdecl_p)
15012 : : /* auto (*fp)() = f; is OK. */;
15013 : 2687293 : else if (current_class_type
15014 : 5026229 : && LAMBDA_TYPE_P (current_class_type))
15015 : : /* OK for C++11 lambdas. */;
15016 : 1629357 : else if (cxx_dialect < cxx14)
15017 : : {
15018 : 7 : auto_diagnostic_group d;
15019 : 7 : error_at (typespec_loc, "%qs function uses "
15020 : : "%<auto%> type specifier without "
15021 : : "trailing return type", name);
15022 : 7 : inform (typespec_loc,
15023 : : "deduced return type only available "
15024 : : "with %<-std=c++14%> or %<-std=gnu++14%>");
15025 : 7 : }
15026 : 1629350 : else if (virtualp)
15027 : : {
15028 : 6 : error_at (typespec_loc, "virtual function "
15029 : : "cannot have deduced return type");
15030 : 6 : virtualp = false;
15031 : : }
15032 : : }
15033 : 3096373 : else if (!is_auto (type) && sfk != sfk_conversion)
15034 : : {
15035 : 15 : error_at (typespec_loc, "%qs function with trailing "
15036 : : "return type has %qT as its type rather "
15037 : : "than plain %<auto%>", name, type);
15038 : 270 : return error_mark_node;
15039 : : }
15040 : 3096358 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15041 : : {
15042 : 9 : if (funcdecl_p)
15043 : 3 : error_at (typespec_loc,
15044 : : "%qs function with trailing return type "
15045 : : "has %<decltype(auto)%> as its type "
15046 : : "rather than plain %<auto%>", name);
15047 : : else
15048 : 6 : error_at (typespec_loc,
15049 : : "invalid use of %<decltype(auto)%>");
15050 : 9 : return error_mark_node;
15051 : : }
15052 : 3096349 : else if (is_constrained_auto (type))
15053 : : {
15054 : 6 : if (funcdecl_p)
15055 : 3 : error_at (typespec_loc,
15056 : : "%qs function with trailing return type "
15057 : : "has constrained %<auto%> type specifier "
15058 : : "rather than plain %<auto%>",
15059 : : name);
15060 : : else
15061 : 3 : error_at (typespec_loc,
15062 : : "invalid use of constrained %<auto%> type");
15063 : 6 : return error_mark_node;
15064 : : }
15065 : 5783668 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15066 : 5783668 : if (!tmpl)
15067 : 4698900 : if (tree late_auto = type_uses_auto (late_return_type))
15068 : 23887 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15069 : 4698900 : if (tmpl)
15070 : : {
15071 : 1084772 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
15072 : : {
15073 : 10 : auto_diagnostic_group g;
15074 : 10 : error_at (typespec_loc, "deduced class "
15075 : : "type %qD in function return type",
15076 : 10 : DECL_NAME (tmpl));
15077 : 10 : inform (DECL_SOURCE_LOCATION (tmpl),
15078 : : "%qD declared here", tmpl);
15079 : 10 : return error_mark_node;
15080 : 10 : }
15081 : 1084762 : else if (!late_return_type)
15082 : : {
15083 : 3 : auto_diagnostic_group d;
15084 : 3 : error_at (declarator->id_loc, "deduction guide "
15085 : : "for %qT must have trailing return "
15086 : 3 : "type", TREE_TYPE (tmpl));
15087 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15088 : : "%qD declared here", tmpl);
15089 : 3 : return error_mark_node;
15090 : 3 : }
15091 : 1084759 : else if (CLASS_TYPE_P (late_return_type)
15092 : 1084759 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15093 : 2169518 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15094 : : == tmpl))
15095 : : /* OK */;
15096 : : else
15097 : 0 : error ("trailing return type %qT of deduction guide "
15098 : : "is not a specialization of %qT",
15099 : 0 : late_return_type, TREE_TYPE (tmpl));
15100 : : }
15101 : : }
15102 : 172147844 : else if (late_return_type
15103 : 172147844 : && sfk != sfk_conversion)
15104 : : {
15105 : 37 : if (late_return_type == error_mark_node)
15106 : : return error_mark_node;
15107 : 37 : if (cxx_dialect < cxx11)
15108 : : /* Not using maybe_warn_cpp0x because this should
15109 : : always be an error. */
15110 : 1 : error_at (typespec_loc,
15111 : : "trailing return type only available "
15112 : : "with %<-std=c++11%> or %<-std=gnu++11%>");
15113 : : else
15114 : 36 : error_at (typespec_loc, "%qs function with trailing "
15115 : : "return type not declared with %<auto%> "
15116 : : "type specifier", name);
15117 : 37 : return error_mark_node;
15118 : : }
15119 : 177931462 : if (late_return_type && sfk == sfk_conversion)
15120 : : {
15121 : 14 : error ("a conversion function cannot have a trailing return type");
15122 : 14 : return error_mark_node;
15123 : : }
15124 : 177931448 : type = splice_late_return_type (type, late_return_type);
15125 : 177931448 : if (type == error_mark_node)
15126 : : return error_mark_node;
15127 : :
15128 : 177931372 : if (late_return_type)
15129 : : {
15130 : 3096255 : late_return_type_p = true;
15131 : 3096255 : type_quals = cp_type_quals (type);
15132 : : }
15133 : :
15134 : 177931372 : if (type_quals != TYPE_UNQUALIFIED)
15135 : : {
15136 : : /* It's wrong, for instance, to issue a -Wignored-qualifiers
15137 : : warning for
15138 : : static_assert(!is_same_v<void(*)(), const void(*)()>);
15139 : : because there the qualifier matters. */
15140 : 12391 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15141 : 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15142 : : "qualifiers ignored on function return type");
15143 : : /* [dcl.fct] "A volatile-qualified return type is
15144 : : deprecated." */
15145 : 12391 : if (type_quals & TYPE_QUAL_VOLATILE)
15146 : 599 : warning_at (typespec_loc, OPT_Wvolatile,
15147 : : "%<volatile%>-qualified return type is "
15148 : : "deprecated");
15149 : :
15150 : : /* We now know that the TYPE_QUALS don't apply to the
15151 : : decl, but to its return type. */
15152 : : type_quals = TYPE_UNQUALIFIED;
15153 : : }
15154 : :
15155 : : /* Error about some types functions can't return. */
15156 : :
15157 : 177931372 : if (TREE_CODE (type) == FUNCTION_TYPE)
15158 : : {
15159 : 30 : error_at (typespec_loc, "%qs declared as function returning "
15160 : : "a function", name);
15161 : 30 : return error_mark_node;
15162 : : }
15163 : 177931342 : if (TREE_CODE (type) == ARRAY_TYPE)
15164 : : {
15165 : 6 : error_at (typespec_loc, "%qs declared as function returning "
15166 : : "an array", name);
15167 : 6 : return error_mark_node;
15168 : : }
15169 : 177931336 : if (constinit_p && funcdecl_p)
15170 : : {
15171 : 15 : error_at (declspecs->locations[ds_constinit],
15172 : : "%<constinit%> on function return type is not "
15173 : : "allowed");
15174 : 15 : return error_mark_node;
15175 : : }
15176 : :
15177 : 177931321 : if (check_decltype_auto (typespec_loc, type))
15178 : 49 : return error_mark_node;
15179 : :
15180 : 177931272 : if (ctype == NULL_TREE
15181 : 177931272 : && decl_context == FIELD
15182 : : && funcdecl_p
15183 : 96087041 : && friendp == 0)
15184 : 91442529 : ctype = current_class_type;
15185 : :
15186 : 177931272 : if (ctype && (sfk == sfk_constructor
15187 : 91442529 : || sfk == sfk_destructor))
15188 : : {
15189 : : /* We are within a class's scope. If our declarator name
15190 : : is the same as the class name, and we are defining
15191 : : a function, then it is a constructor/destructor, and
15192 : : therefore returns a void type. */
15193 : :
15194 : : /* ISO C++ 12.4/2. A destructor may not be declared
15195 : : const or volatile. A destructor may not be static.
15196 : : A destructor may not be declared with ref-qualifier.
15197 : :
15198 : : ISO C++ 12.1. A constructor may not be declared
15199 : : const or volatile. A constructor may not be
15200 : : virtual. A constructor may not be static.
15201 : : A constructor may not be declared with ref-qualifier. */
15202 : 24631687 : if (staticp == 2)
15203 : 9 : error_at (declspecs->locations[ds_storage_class],
15204 : : (flags == DTOR_FLAG)
15205 : : ? G_("destructor cannot be static member "
15206 : : "function")
15207 : : : G_("constructor cannot be static member "
15208 : : "function"));
15209 : 24631687 : if (memfn_quals)
15210 : : {
15211 : 9 : error ((flags == DTOR_FLAG)
15212 : : ? G_("destructors may not be cv-qualified")
15213 : : : G_("constructors may not be cv-qualified"));
15214 : 9 : memfn_quals = TYPE_UNQUALIFIED;
15215 : : }
15216 : :
15217 : 24631687 : if (rqual)
15218 : : {
15219 : 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
15220 : 6 : error ((flags == DTOR_FLAG)
15221 : : ? G_("destructors may not be ref-qualified")
15222 : : : G_("constructors may not be ref-qualified"));
15223 : 6 : rqual = REF_QUAL_NONE;
15224 : : }
15225 : :
15226 : 24631687 : if (decl_context == FIELD
15227 : 24631687 : && !member_function_or_else (ctype,
15228 : : current_class_type,
15229 : : flags))
15230 : 0 : return error_mark_node;
15231 : :
15232 : 24631687 : if (flags != DTOR_FLAG)
15233 : : {
15234 : : /* It's a constructor. */
15235 : 21617150 : if (explicitp == 1)
15236 : 5141998 : explicitp = 2;
15237 : 21617150 : if (virtualp)
15238 : : {
15239 : 3 : permerror (declspecs->locations[ds_virtual],
15240 : : "constructors cannot be declared %<virtual%>");
15241 : 3 : virtualp = 0;
15242 : : }
15243 : 21617150 : if (decl_context == FIELD
15244 : 21617150 : && sfk != sfk_constructor)
15245 : 0 : return error_mark_node;
15246 : : }
15247 : 24631687 : if (decl_context == FIELD)
15248 : 24631687 : staticp = 0;
15249 : : }
15250 : 153299585 : else if (friendp)
15251 : : {
15252 : 4331680 : if (virtualp)
15253 : : {
15254 : : /* Cannot be both friend and virtual. */
15255 : 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15256 : 12 : richloc.add_range (declspecs->locations[ds_friend]);
15257 : 12 : error_at (&richloc, "virtual functions cannot be friends");
15258 : 12 : friendp = 0;
15259 : 12 : }
15260 : 4331680 : if (decl_context == NORMAL)
15261 : 0 : error_at (declarator->id_loc,
15262 : : "friend declaration not in class definition");
15263 : 4331680 : if (current_function_decl && funcdef_flag)
15264 : : {
15265 : 9 : error_at (declarator->id_loc,
15266 : : "cannot define friend function %qs in a local "
15267 : : "class definition", name);
15268 : 9 : friendp = 0;
15269 : : }
15270 : : /* [class.friend]/6: A function can be defined in a friend
15271 : : declaration if the function name is unqualified. */
15272 : 4331680 : if (funcdef_flag && in_namespace)
15273 : : {
15274 : 6 : if (in_namespace == global_namespace)
15275 : 3 : error_at (declarator->id_loc,
15276 : : "friend function definition %qs cannot have "
15277 : : "a name qualified with %<::%>", name);
15278 : : else
15279 : 3 : error_at (declarator->id_loc,
15280 : : "friend function definition %qs cannot have "
15281 : : "a name qualified with %<%D::%>", name,
15282 : : in_namespace);
15283 : : }
15284 : : }
15285 : 148967905 : else if (ctype && sfk == sfk_conversion)
15286 : : {
15287 : 637993 : if (explicitp == 1)
15288 : : {
15289 : 306434 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
15290 : 306434 : explicitp = 2;
15291 : : }
15292 : : }
15293 : 148329912 : else if (sfk == sfk_deduction_guide)
15294 : : {
15295 : 1084759 : if (explicitp == 1)
15296 : 177931272 : explicitp = 2;
15297 : : }
15298 : :
15299 : 177931272 : if (xobj_parm)
15300 : : {
15301 : 58379 : if (!ctype
15302 : 58379 : && decl_context == NORMAL
15303 : 49 : && (in_namespace
15304 : 49 : || !declarator->declarator->u.id.qualifying_scope))
15305 : 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15306 : : "a non-member function cannot have "
15307 : : "an explicit object parameter");
15308 : : else
15309 : : {
15310 : 58375 : if (virtualp)
15311 : : {
15312 : 16 : auto_diagnostic_group d;
15313 : 16 : error_at (declspecs->locations[ds_virtual],
15314 : : "an explicit object member function cannot "
15315 : : "be %<virtual%>");
15316 : 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15317 : : "explicit object parameter declared here");
15318 : 16 : virtualp = false;
15319 : 16 : }
15320 : 58375 : if (staticp >= 2)
15321 : : {
15322 : 2 : auto_diagnostic_group d;
15323 : 2 : error_at (declspecs->locations[ds_storage_class],
15324 : : "an explicit object member function cannot "
15325 : : "be %<static%>");
15326 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15327 : : "explicit object parameter declared here");
15328 : 2 : }
15329 : 58375 : if (unqualified_id
15330 : 177989641 : && identifier_p (unqualified_id)
15331 : 116744 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15332 : 8 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15333 : : "%qD cannot be an explicit object member "
15334 : : "function", unqualified_id);
15335 : : }
15336 : : }
15337 : 177931272 : tree pushed_scope = NULL_TREE;
15338 : 177931272 : if (funcdecl_p
15339 : 177931272 : && decl_context != FIELD
15340 : 79805038 : && inner_declarator->u.id.qualifying_scope
15341 : 187136719 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15342 : 9204121 : pushed_scope
15343 : 9204121 : = push_scope (inner_declarator->u.id.qualifying_scope);
15344 : :
15345 : 177931272 : arg_types = grokparms (declarator->u.function.parameters, &parms);
15346 : :
15347 : 177931272 : if (pushed_scope)
15348 : 9204121 : pop_scope (pushed_scope);
15349 : :
15350 : 177931272 : if (inner_declarator
15351 : 177456676 : && inner_declarator->kind == cdk_id
15352 : 175579247 : && inner_declarator->u.id.sfk == sfk_destructor
15353 : 3188784 : && arg_types != void_list_node)
15354 : : {
15355 : 20 : error_at (declarator->id_loc,
15356 : : "destructors may not have parameters");
15357 : 20 : arg_types = void_list_node;
15358 : 20 : parms = NULL_TREE;
15359 : 20 : is_xobj_member_function = false;
15360 : : }
15361 : :
15362 : 177931272 : type = cp_build_function_type (type, arg_types);
15363 : :
15364 : 177931272 : tree attrs = declarator->std_attributes;
15365 : 177931272 : if (tx_qual)
15366 : : {
15367 : 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
15368 : : /* transaction_safe applies to the type, but
15369 : : transaction_safe_dynamic applies to the function. */
15370 : 242 : if (is_attribute_p ("transaction_safe", tx_qual))
15371 : 213 : attrs = attr_chainon (attrs, att);
15372 : : else
15373 : 29 : returned_attrs = attr_chainon (returned_attrs, att);
15374 : : }
15375 : :
15376 : : /* Actually apply the contract attributes to the declaration. */
15377 : 177932438 : for (tree *p = &attrs; *p;)
15378 : : {
15379 : 1166 : tree l = *p;
15380 : 1166 : if (cxx_contract_attribute_p (l))
15381 : : {
15382 : 667 : *p = TREE_CHAIN (l);
15383 : : /* Intentionally reverse order of contracts so they're
15384 : : reversed back into their lexical order. */
15385 : 667 : TREE_CHAIN (l) = NULL_TREE;
15386 : 667 : returned_attrs = chainon (l, returned_attrs);
15387 : : }
15388 : : else
15389 : 499 : p = &TREE_CHAIN (l);
15390 : : }
15391 : :
15392 : 177931272 : if (attrs)
15393 : : /* [dcl.fct]/2:
15394 : :
15395 : : The optional attribute-specifier-seq appertains to
15396 : : the function type. */
15397 : 487 : cplus_decl_attributes (&type, attrs, 0);
15398 : :
15399 : 177931272 : if (raises)
15400 : 70107786 : type = build_exception_variant (type, raises);
15401 : : }
15402 : 177931272 : break;
15403 : :
15404 : 181450192 : case cdk_pointer:
15405 : 181450192 : case cdk_reference:
15406 : 181450192 : case cdk_ptrmem:
15407 : : /* Filter out pointers-to-references and references-to-references.
15408 : : We can get these if a TYPE_DECL is used. */
15409 : :
15410 : 181450192 : if (TYPE_REF_P (type))
15411 : : {
15412 : 238 : if (declarator->kind != cdk_reference)
15413 : : {
15414 : 3 : error ("cannot declare pointer to %q#T", type);
15415 : 3 : type = TREE_TYPE (type);
15416 : : }
15417 : :
15418 : : /* In C++0x, we allow reference to reference declarations
15419 : : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15420 : : and template type arguments [14.3.1/4 temp.arg.type]. The
15421 : : check for direct reference to reference declarations, which
15422 : : are still forbidden, occurs below. Reasoning behind the change
15423 : : can be found in DR106, DR540, and the rvalue reference
15424 : : proposals. */
15425 : 235 : else if (cxx_dialect == cxx98)
15426 : : {
15427 : 0 : error ("cannot declare reference to %q#T", type);
15428 : 0 : type = TREE_TYPE (type);
15429 : : }
15430 : : }
15431 : 181449954 : else if (VOID_TYPE_P (type))
15432 : : {
15433 : 4700201 : if (declarator->kind == cdk_reference)
15434 : 4 : error ("cannot declare reference to %q#T", type);
15435 : 4700197 : else if (declarator->kind == cdk_ptrmem)
15436 : 3 : error ("cannot declare pointer to %q#T member", type);
15437 : : }
15438 : :
15439 : : /* We now know that the TYPE_QUALS don't apply to the decl,
15440 : : but to the target of the pointer. */
15441 : 181450192 : type_quals = TYPE_UNQUALIFIED;
15442 : :
15443 : : /* This code used to handle METHOD_TYPE, but I don't think it's
15444 : : possible to get it here anymore. */
15445 : 181450192 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15446 : 181450192 : if (declarator->kind == cdk_ptrmem
15447 : 1121461 : && TREE_CODE (type) == FUNCTION_TYPE)
15448 : : {
15449 : 923557 : memfn_quals |= type_memfn_quals (type);
15450 : 1847114 : type = build_memfn_type (type,
15451 : 923557 : declarator->u.pointer.class_type,
15452 : : memfn_quals,
15453 : : rqual);
15454 : 923557 : if (type == error_mark_node)
15455 : : return error_mark_node;
15456 : :
15457 : : rqual = REF_QUAL_NONE;
15458 : : memfn_quals = TYPE_UNQUALIFIED;
15459 : : }
15460 : :
15461 : 181450189 : if (TREE_CODE (type) == FUNCTION_TYPE
15462 : 181450189 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15463 : 1012527 : || type_memfn_rqual (type) != REF_QUAL_NONE))
15464 : 15 : error (declarator->kind == cdk_reference
15465 : : ? G_("cannot declare reference to qualified function type %qT")
15466 : : : G_("cannot declare pointer to qualified function type %qT"),
15467 : : type);
15468 : :
15469 : : /* When the pointed-to type involves components of variable size,
15470 : : care must be taken to ensure that the size evaluation code is
15471 : : emitted early enough to dominate all the possible later uses
15472 : : and late enough for the variables on which it depends to have
15473 : : been assigned.
15474 : :
15475 : : This is expected to happen automatically when the pointed-to
15476 : : type has a name/declaration of it's own, but special attention
15477 : : is required if the type is anonymous.
15478 : :
15479 : : We handle the NORMAL and FIELD contexts here by inserting a
15480 : : dummy statement that just evaluates the size at a safe point
15481 : : and ensures it is not deferred until e.g. within a deeper
15482 : : conditional context (c++/43555).
15483 : :
15484 : : We expect nothing to be needed here for PARM or TYPENAME.
15485 : : Evaluating the size at this point for TYPENAME would
15486 : : actually be incorrect, as we might be in the middle of an
15487 : : expression with side effects on the pointed-to type size
15488 : : "arguments" prior to the pointer declaration point and the
15489 : : size evaluation could end up prior to the side effects. */
15490 : :
15491 : 181450189 : if (!TYPE_NAME (type)
15492 : 5361595 : && (decl_context == NORMAL || decl_context == FIELD)
15493 : 617133 : && at_function_scope_p ()
15494 : 181491745 : && variably_modified_type_p (type, NULL_TREE))
15495 : : {
15496 : 96 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15497 : : NULL_TREE, type);
15498 : 96 : add_decl_expr (TYPE_NAME (type));
15499 : : }
15500 : :
15501 : 181450189 : if (declarator->kind == cdk_reference)
15502 : : {
15503 : : /* In C++0x, the type we are creating a reference to might be
15504 : : a typedef which is itself a reference type. In that case,
15505 : : we follow the reference collapsing rules in
15506 : : [7.1.3/8 dcl.typedef] to create the final reference type:
15507 : :
15508 : : "If a typedef TD names a type that is a reference to a type
15509 : : T, an attempt to create the type 'lvalue reference to cv TD'
15510 : : creates the type 'lvalue reference to T,' while an attempt
15511 : : to create the type "rvalue reference to cv TD' creates the
15512 : : type TD."
15513 : : */
15514 : 119478268 : if (VOID_TYPE_P (type))
15515 : : /* We already gave an error. */;
15516 : 119478264 : else if (TYPE_REF_P (type))
15517 : : {
15518 : 235 : if (declarator->u.reference.rvalue_ref)
15519 : : /* Leave type alone. */;
15520 : : else
15521 : 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15522 : : }
15523 : : else
15524 : 119478029 : type = cp_build_reference_type
15525 : 119478029 : (type, declarator->u.reference.rvalue_ref);
15526 : :
15527 : : /* In C++0x, we need this check for direct reference to
15528 : : reference declarations, which are forbidden by
15529 : : [8.3.2/5 dcl.ref]. Reference to reference declarations
15530 : : are only allowed indirectly through typedefs and template
15531 : : type arguments. Example:
15532 : :
15533 : : void foo(int & &); // invalid ref-to-ref decl
15534 : :
15535 : : typedef int & int_ref;
15536 : : void foo(int_ref &); // valid ref-to-ref decl
15537 : : */
15538 : 119478268 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15539 : 0 : error ("cannot declare reference to %q#T, which is not "
15540 : : "a typedef or a template type argument", type);
15541 : : }
15542 : 61971921 : else if (TREE_CODE (type) == METHOD_TYPE)
15543 : 923554 : type = build_ptrmemfunc_type (build_pointer_type (type));
15544 : 61048367 : else if (declarator->kind == cdk_ptrmem)
15545 : : {
15546 : 197904 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15547 : : != NAMESPACE_DECL);
15548 : 197904 : if (declarator->u.pointer.class_type == error_mark_node)
15549 : : /* We will already have complained. */
15550 : 3 : type = error_mark_node;
15551 : : else
15552 : 197901 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15553 : : type);
15554 : : }
15555 : : else
15556 : 60850463 : type = build_pointer_type (type);
15557 : :
15558 : : /* Process a list of type modifier keywords (such as
15559 : : const or volatile) that were given inside the `*' or `&'. */
15560 : :
15561 : 181450189 : if (declarator->u.pointer.qualifiers)
15562 : : {
15563 : 8450949 : type
15564 : 8450949 : = cp_build_qualified_type (type,
15565 : : declarator->u.pointer.qualifiers);
15566 : 8450949 : type_quals = cp_type_quals (type);
15567 : : }
15568 : :
15569 : : /* Apply C++11 attributes to the pointer, and not to the
15570 : : type pointed to. This is unlike what is done for GNU
15571 : : attributes above. It is to comply with [dcl.ptr]/1:
15572 : :
15573 : : [the optional attribute-specifier-seq (7.6.1) appertains
15574 : : to the pointer and not to the object pointed to]. */
15575 : 181450189 : if (declarator->std_attributes)
15576 : 169 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15577 : :
15578 : : ctype = NULL_TREE;
15579 : : break;
15580 : :
15581 : : case cdk_error:
15582 : : break;
15583 : :
15584 : 0 : default:
15585 : 0 : gcc_unreachable ();
15586 : : }
15587 : : }
15588 : :
15589 : 931248396 : id_loc = declarator ? declarator->id_loc : input_location;
15590 : :
15591 : 931248396 : if (innermost_code != cdk_function
15592 : : /* Don't check this if it can be the artifical decltype(auto)
15593 : : we created when building a constraint in a compound-requirement:
15594 : : that the type-constraint is plain is going to be checked in
15595 : : cp_parser_compound_requirement. */
15596 : 931248396 : && decl_context != TYPENAME
15597 : 931248396 : && check_decltype_auto (id_loc, type))
15598 : 19 : return error_mark_node;
15599 : :
15600 : : /* A `constexpr' specifier used in an object declaration declares
15601 : : the object as `const'. */
15602 : 931248377 : if (constexpr_p && innermost_code != cdk_function)
15603 : : {
15604 : : /* DR1688 says that a `constexpr' specifier in combination with
15605 : : `volatile' is valid. */
15606 : :
15607 : 13989091 : if (!TYPE_REF_P (type))
15608 : : {
15609 : 13974082 : type_quals |= TYPE_QUAL_CONST;
15610 : 13974082 : type = cp_build_qualified_type (type, type_quals);
15611 : : }
15612 : : }
15613 : :
15614 : 539839696 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15615 : 2032092 : && !FUNC_OR_METHOD_TYPE_P (type)
15616 : 932365169 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15617 : : {
15618 : 0 : error ("template-id %qD used as a declarator",
15619 : : unqualified_id);
15620 : 0 : unqualified_id = dname;
15621 : : }
15622 : :
15623 : : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
15624 : : qualified with a class-name, turn it into a METHOD_TYPE, unless
15625 : : we know that the function is static. We take advantage of this
15626 : : opportunity to do other processing that pertains to entities
15627 : : explicitly declared to be class members. Note that if DECLARATOR
15628 : : is non-NULL, we know it is a cdk_id declarator; otherwise, we
15629 : : would not have exited the loop above. */
15630 : 931248377 : if (declarator
15631 : 539926637 : && declarator->kind == cdk_id
15632 : 539839696 : && declarator->u.id.qualifying_scope
15633 : 941029012 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
15634 : : {
15635 : 9595675 : ctype = declarator->u.id.qualifying_scope;
15636 : 9595675 : ctype = TYPE_MAIN_VARIANT (ctype);
15637 : 9595675 : template_count = num_template_headers_for_class (ctype);
15638 : :
15639 : 9595675 : if (ctype == current_class_type)
15640 : : {
15641 : 54 : if (friendp)
15642 : : {
15643 : 15 : permerror (declspecs->locations[ds_friend],
15644 : : "member functions are implicitly "
15645 : : "friends of their class");
15646 : 15 : friendp = 0;
15647 : : }
15648 : : else
15649 : 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
15650 : : ctype, name);
15651 : : }
15652 : 9595621 : else if (/* If the qualifying type is already complete, then we
15653 : : can skip the following checks. */
15654 : 9595621 : !COMPLETE_TYPE_P (ctype)
15655 : 150 : && (/* If the function is being defined, then
15656 : : qualifying type must certainly be complete. */
15657 : : funcdef_flag
15658 : : /* A friend declaration of "T::f" is OK, even if
15659 : : "T" is a template parameter. But, if this
15660 : : function is not a friend, the qualifying type
15661 : : must be a class. */
15662 : 111 : || (!friendp && !CLASS_TYPE_P (ctype))
15663 : : /* For a declaration, the type need not be
15664 : : complete, if either it is dependent (since there
15665 : : is no meaningful definition of complete in that
15666 : : case) or the qualifying class is currently being
15667 : : defined. */
15668 : 123 : || !(dependent_type_p (ctype)
15669 : 24 : || currently_open_class (ctype)))
15670 : : /* Check that the qualifying type is complete. */
15671 : 9595687 : && !complete_type_or_else (ctype, NULL_TREE))
15672 : 66 : return error_mark_node;
15673 : 9595555 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15674 : : {
15675 : 9204358 : if (current_class_type
15676 : 264 : && (!friendp || funcdef_flag || initialized))
15677 : : {
15678 : 59 : error_at (id_loc, funcdef_flag || initialized
15679 : : ? G_("cannot define member function %<%T::%s%> "
15680 : : "within %qT")
15681 : : : G_("cannot declare member function %<%T::%s%> "
15682 : : "within %qT"),
15683 : : ctype, name, current_class_type);
15684 : 49 : return error_mark_node;
15685 : : }
15686 : : }
15687 : 391197 : else if (typedef_p && current_class_type)
15688 : : {
15689 : 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
15690 : : ctype, name, current_class_type);
15691 : 0 : return error_mark_node;
15692 : : }
15693 : : }
15694 : :
15695 : 931248262 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
15696 : 51904532 : ctype = current_class_type;
15697 : :
15698 : : /* Now TYPE has the actual type. */
15699 : :
15700 : 931248262 : if (returned_attrs)
15701 : : {
15702 : 3369 : if (attrlist)
15703 : 3369 : *attrlist = attr_chainon (returned_attrs, *attrlist);
15704 : : else
15705 : : attrlist = &returned_attrs;
15706 : : }
15707 : :
15708 : 931248262 : if (declarator
15709 : 539926522 : && declarator->kind == cdk_id
15710 : 539839581 : && declarator->std_attributes
15711 : 340753 : && attrlist != NULL)
15712 : : {
15713 : : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
15714 : : a declarator-id appertains to the entity that is declared. */
15715 : 340750 : if (declarator->std_attributes != error_mark_node)
15716 : 340732 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
15717 : : else
15718 : : /* We should have already diagnosed the issue (c++/78344). */
15719 : 18 : gcc_assert (seen_error ());
15720 : : }
15721 : :
15722 : : /* Handle parameter packs. */
15723 : 931248262 : if (parameter_pack_p)
15724 : : {
15725 : 2973523 : if (decl_context == PARM)
15726 : : /* Turn the type into a pack expansion.*/
15727 : 2973523 : type = make_pack_expansion (type);
15728 : : else
15729 : 0 : error ("non-parameter %qs cannot be a parameter pack", name);
15730 : : }
15731 : :
15732 : 931248262 : if ((decl_context == FIELD || decl_context == PARM)
15733 : 426992988 : && !processing_template_decl
15734 : 1068249488 : && variably_modified_type_p (type, NULL_TREE))
15735 : : {
15736 : 12 : if (decl_context == FIELD)
15737 : 6 : error_at (id_loc,
15738 : : "data member may not have variably modified type %qT", type);
15739 : : else
15740 : 6 : error_at (id_loc,
15741 : : "parameter may not have variably modified type %qT", type);
15742 : 12 : type = error_mark_node;
15743 : : }
15744 : :
15745 : 931248262 : if (explicitp == 1 || (explicitp && friendp))
15746 : : {
15747 : : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
15748 : : in the declaration of a constructor or conversion function within
15749 : : a class definition. */
15750 : 74 : if (!current_class_type)
15751 : 59 : error_at (declspecs->locations[ds_explicit],
15752 : : "%<explicit%> outside class declaration");
15753 : 15 : else if (friendp)
15754 : 12 : error_at (declspecs->locations[ds_explicit],
15755 : : "%<explicit%> in friend declaration");
15756 : : else
15757 : 3 : error_at (declspecs->locations[ds_explicit],
15758 : : "only declarations of constructors and conversion operators "
15759 : : "can be %<explicit%>");
15760 : : explicitp = 0;
15761 : : }
15762 : :
15763 : 931248262 : if (storage_class == sc_mutable)
15764 : : {
15765 : 147273 : location_t sloc = declspecs->locations[ds_storage_class];
15766 : 147273 : if (decl_context != FIELD || friendp)
15767 : : {
15768 : 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
15769 : : name);
15770 : 30 : storage_class = sc_none;
15771 : : }
15772 : 147243 : else if (decl_context == TYPENAME || typedef_p)
15773 : : {
15774 : 0 : error_at (sloc,
15775 : : "non-object member %qs cannot be declared %<mutable%>",
15776 : : name);
15777 : 0 : storage_class = sc_none;
15778 : : }
15779 : 147243 : else if (FUNC_OR_METHOD_TYPE_P (type))
15780 : : {
15781 : 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
15782 : : name);
15783 : 3 : storage_class = sc_none;
15784 : : }
15785 : 147240 : else if (staticp)
15786 : : {
15787 : 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
15788 : : name);
15789 : 0 : storage_class = sc_none;
15790 : : }
15791 : 147240 : else if (type_quals & TYPE_QUAL_CONST)
15792 : : {
15793 : 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
15794 : : name);
15795 : 9 : storage_class = sc_none;
15796 : : }
15797 : 147231 : else if (TYPE_REF_P (type))
15798 : : {
15799 : 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
15800 : : name);
15801 : 6 : storage_class = sc_none;
15802 : : }
15803 : : }
15804 : :
15805 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
15806 : 931248262 : if (typedef_p && decl_context != TYPENAME)
15807 : : {
15808 : 44346066 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
15809 : 44346066 : tree decl;
15810 : :
15811 : 44346066 : if (funcdef_flag)
15812 : : {
15813 : 6 : if (decl_context == NORMAL)
15814 : 3 : error_at (id_loc,
15815 : : "typedef may not be a function definition");
15816 : : else
15817 : 3 : error_at (id_loc,
15818 : : "typedef may not be a member function definition");
15819 : 6 : return error_mark_node;
15820 : : }
15821 : :
15822 : : /* This declaration:
15823 : :
15824 : : typedef void f(int) const;
15825 : :
15826 : : declares a function type which is not a member of any
15827 : : particular class, but which is cv-qualified; for
15828 : : example "f S::*" declares a pointer to a const-qualified
15829 : : member function of S. We record the cv-qualification in the
15830 : : function type. */
15831 : 44346060 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
15832 : : {
15833 : 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
15834 : :
15835 : : /* We have now dealt with these qualifiers. */
15836 : 62 : memfn_quals = TYPE_UNQUALIFIED;
15837 : 62 : rqual = REF_QUAL_NONE;
15838 : : }
15839 : :
15840 : 44346060 : if (type_uses_auto (type))
15841 : : {
15842 : 59 : if (alias_p)
15843 : 19 : error_at (declspecs->locations[ds_type_spec],
15844 : : "%<auto%> not allowed in alias declaration");
15845 : : else
15846 : 40 : error_at (declspecs->locations[ds_type_spec],
15847 : : "typedef declared %<auto%>");
15848 : 59 : type = error_mark_node;
15849 : : }
15850 : :
15851 : 44346060 : if (reqs)
15852 : 3 : error_at (location_of (reqs), "requires-clause on typedef");
15853 : :
15854 : 44346060 : if (id_declarator && declarator->u.id.qualifying_scope)
15855 : : {
15856 : 22 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
15857 : 22 : type = error_mark_node;
15858 : : }
15859 : :
15860 : 44346060 : if (decl_context == FIELD)
15861 : 24067074 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
15862 : : else
15863 : 20278986 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
15864 : :
15865 : 44346060 : if (decl_context != FIELD)
15866 : : {
15867 : 20278986 : if (!current_function_decl)
15868 : 8519955 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
15869 : 11759031 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
15870 : : /* The TYPE_DECL is "abstract" because there will be
15871 : : clones of this constructor/destructor, and there will
15872 : : be copies of this TYPE_DECL generated in those
15873 : : clones. The decloning optimization (for space) may
15874 : : revert this subsequently if it determines that
15875 : : the clones should share a common implementation. */
15876 : 155396 : DECL_ABSTRACT_P (decl) = true;
15877 : :
15878 : 20278986 : set_originating_module (decl);
15879 : : }
15880 : 24067074 : else if (current_class_type
15881 : 24067074 : && constructor_name_p (unqualified_id, current_class_type))
15882 : 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
15883 : : "as enclosing class",
15884 : : unqualified_id);
15885 : :
15886 : : /* If the user declares "typedef struct {...} foo" then the
15887 : : struct will have an anonymous name. Fill that name in now.
15888 : : Nothing can refer to it, so nothing needs know about the name
15889 : : change. */
15890 : 44346060 : if (type != error_mark_node
15891 : 44345979 : && unqualified_id
15892 : 44345976 : && TYPE_NAME (type)
15893 : 41441389 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15894 : 82964732 : && (TYPE_UNNAMED_P (type)
15895 : : /* An enum may have previously used an enumerator for linkage
15896 : : purposes, but we want the typedef name to take priority. */
15897 : 41048177 : || enum_with_enumerator_for_linkage_p (type))
15898 : 393378 : && declspecs->type_definition_p
15899 : 393308 : && attributes_naming_typedef_ok (*attrlist)
15900 : 44739365 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
15901 : 393287 : name_unnamed_type (type, decl);
15902 : :
15903 : 44346060 : if (signed_p
15904 : 44346060 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
15905 : 379315 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
15906 : :
15907 : 44346060 : bad_specifiers (decl, BSP_TYPE, virtualp,
15908 : : memfn_quals != TYPE_UNQUALIFIED,
15909 : : inlinep, friendp, raises != NULL_TREE,
15910 : : declspecs->locations);
15911 : :
15912 : 44346060 : if (alias_p)
15913 : : /* Acknowledge that this was written:
15914 : : `using analias = atype;'. */
15915 : 17102851 : TYPE_DECL_ALIAS_P (decl) = 1;
15916 : :
15917 : 44346060 : return decl;
15918 : : }
15919 : :
15920 : : /* Detect the case of an array type of unspecified size
15921 : : which came, as such, direct from a typedef name.
15922 : : We must copy the type, so that the array's domain can be
15923 : : individually set by the object's initializer. */
15924 : :
15925 : 886902196 : if (type && typedef_type
15926 : 859570332 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
15927 : 887430129 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
15928 : 79 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15929 : :
15930 : : /* Detect where we're using a typedef of function type to declare a
15931 : : function. PARMS will not be set, so we must create it now. */
15932 : :
15933 : 886902196 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
15934 : : {
15935 : 15319 : tree decls = NULL_TREE;
15936 : 15319 : tree args;
15937 : :
15938 : 15319 : for (args = TYPE_ARG_TYPES (type);
15939 : 33101 : args && args != void_list_node;
15940 : 17782 : args = TREE_CHAIN (args))
15941 : : {
15942 : 17782 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
15943 : 17782 : TREE_VALUE (args));
15944 : :
15945 : 17782 : DECL_CHAIN (decl) = decls;
15946 : 17782 : decls = decl;
15947 : : }
15948 : :
15949 : 15319 : parms = nreverse (decls);
15950 : :
15951 : 15319 : if (decl_context != TYPENAME)
15952 : : {
15953 : : /* The qualifiers on the function type become the qualifiers on
15954 : : the non-static member function. */
15955 : 14629 : memfn_quals |= type_memfn_quals (type);
15956 : 14629 : rqual = type_memfn_rqual (type);
15957 : 14629 : type_quals = TYPE_UNQUALIFIED;
15958 : 14629 : raises = TYPE_RAISES_EXCEPTIONS (type);
15959 : : }
15960 : : }
15961 : :
15962 : : /* If this is a type name (such as, in a cast or sizeof),
15963 : : compute the type and return it now. */
15964 : :
15965 : 886901506 : if (decl_context == TYPENAME)
15966 : : {
15967 : : /* Note that here we don't care about type_quals. */
15968 : :
15969 : : /* Special case: "friend class foo" looks like a TYPENAME context. */
15970 : 346963426 : if (friendp)
15971 : : {
15972 : 0 : if (inlinep)
15973 : : {
15974 : 0 : error ("%<inline%> specified for friend class declaration");
15975 : 0 : inlinep = 0;
15976 : : }
15977 : :
15978 : 0 : if (!current_aggr)
15979 : : {
15980 : : /* Don't allow friend declaration without a class-key. */
15981 : 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15982 : 0 : permerror (input_location, "template parameters cannot be friends");
15983 : 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
15984 : 0 : permerror (input_location, "friend declaration requires class-key, "
15985 : : "i.e. %<friend class %T::%D%>",
15986 : 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
15987 : : else
15988 : 0 : permerror (input_location, "friend declaration requires class-key, "
15989 : : "i.e. %<friend %#T%>",
15990 : : type);
15991 : : }
15992 : :
15993 : : /* Only try to do this stuff if we didn't already give up. */
15994 : 0 : if (type != integer_type_node)
15995 : : {
15996 : : /* A friendly class? */
15997 : 0 : if (current_class_type)
15998 : 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
15999 : : /*complain=*/true);
16000 : : else
16001 : 0 : error ("trying to make class %qT a friend of global scope",
16002 : : type);
16003 : :
16004 : 0 : type = void_type_node;
16005 : : }
16006 : : }
16007 : 346963426 : else if (memfn_quals || rqual)
16008 : : {
16009 : 64732 : if (ctype == NULL_TREE
16010 : 64732 : && TREE_CODE (type) == METHOD_TYPE)
16011 : 0 : ctype = TYPE_METHOD_BASETYPE (type);
16012 : :
16013 : 64732 : if (ctype)
16014 : 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
16015 : : /* Core issue #547: need to allow this in template type args.
16016 : : Allow it in general in C++11 for alias-declarations. */
16017 : 64732 : else if ((template_type_arg || cxx_dialect >= cxx11)
16018 : 64730 : && TREE_CODE (type) == FUNCTION_TYPE)
16019 : 64728 : type = apply_memfn_quals (type, memfn_quals, rqual);
16020 : : else
16021 : 4 : error ("invalid qualifiers on non-member function type");
16022 : : }
16023 : :
16024 : 346963426 : if (reqs)
16025 : 3 : error_at (location_of (reqs), "requires-clause on type-id");
16026 : :
16027 : 346963426 : return type;
16028 : : }
16029 : 539938770 : else if (unqualified_id == NULL_TREE && decl_context != PARM
16030 : 712003 : && decl_context != CATCHPARM
16031 : 224668 : && TREE_CODE (type) != UNION_TYPE
16032 : 224428 : && ! bitfield
16033 : 224428 : && innermost_code != cdk_decomp)
16034 : : {
16035 : 9 : error ("abstract declarator %qT used as declaration", type);
16036 : 9 : return error_mark_node;
16037 : : }
16038 : :
16039 : 539938761 : if (!FUNC_OR_METHOD_TYPE_P (type))
16040 : : {
16041 : : /* Only functions may be declared using an operator-function-id. */
16042 : 364398269 : if (dname && IDENTIFIER_ANY_OP_P (dname))
16043 : : {
16044 : 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
16045 : 8 : return error_mark_node;
16046 : : }
16047 : :
16048 : 364398261 : if (reqs)
16049 : 15 : error_at (location_of (reqs),
16050 : : "requires-clause on declaration of non-function type %qT",
16051 : : type);
16052 : : }
16053 : :
16054 : : /* We don't check parameter types here because we can emit a better
16055 : : error message later. */
16056 : 539938753 : if (decl_context != PARM)
16057 : : {
16058 : 260624469 : type = check_var_type (unqualified_id, type, id_loc);
16059 : 260624469 : if (type == error_mark_node)
16060 : : return error_mark_node;
16061 : : }
16062 : :
16063 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16064 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16065 : :
16066 : 539938671 : if (decl_context == PARM || decl_context == CATCHPARM)
16067 : : {
16068 : 279815854 : if (ctype || in_namespace)
16069 : 0 : error ("cannot use %<::%> in parameter declaration");
16070 : :
16071 : 279815854 : tree auto_node = type_uses_auto (type);
16072 : 279815854 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16073 : : {
16074 : 119 : bool err_p = true;
16075 : 119 : if (cxx_dialect >= cxx14)
16076 : : {
16077 : 210 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16078 : 3 : error_at (typespec_loc,
16079 : : "cannot declare a parameter with %<decltype(auto)%>");
16080 : 108 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16081 : : {
16082 : 4 : auto_diagnostic_group g;
16083 : 4 : gcc_rich_location richloc (typespec_loc);
16084 : 4 : richloc.add_fixit_insert_after ("<>");
16085 : 4 : error_at (&richloc,
16086 : : "missing template argument list after %qE; template "
16087 : : "placeholder not permitted in parameter", c);
16088 : 4 : if (decl_context == PARM && cxx_dialect >= cxx20)
16089 : 3 : inform (typespec_loc, "or use %<auto%> for an "
16090 : : "abbreviated function template");
16091 : 4 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16092 : 4 : }
16093 : 104 : else if (decl_context == CATCHPARM || template_parm_flag)
16094 : 14 : error_at (typespec_loc,
16095 : : "%<auto%> parameter not permitted in this context");
16096 : : else
16097 : : /* Do not issue an error while tentatively parsing a function
16098 : : parameter: for T t(auto(a), 42);, when we just saw the 1st
16099 : : parameter, we don't know yet that this construct won't be
16100 : : a function declaration. Defer the checking to
16101 : : cp_parser_parameter_declaration_clause. */
16102 : : err_p = false;
16103 : : }
16104 : : else
16105 : 8 : error_at (typespec_loc, "parameter declared %<auto%>");
16106 : 29 : if (err_p)
16107 : 29 : type = error_mark_node;
16108 : : }
16109 : :
16110 : : /* A parameter declared as an array of T is really a pointer to T.
16111 : : One declared as a function is really a pointer to a function.
16112 : : One declared as a member is really a pointer to member. */
16113 : :
16114 : 279815854 : if (TREE_CODE (type) == ARRAY_TYPE)
16115 : : {
16116 : : /* Transfer const-ness of array into that of type pointed to. */
16117 : 739421 : type = build_pointer_type (TREE_TYPE (type));
16118 : 739421 : type_quals = TYPE_UNQUALIFIED;
16119 : 739421 : array_parameter_p = true;
16120 : : }
16121 : 279076433 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16122 : 107503 : type = build_pointer_type (type);
16123 : : }
16124 : :
16125 : 128875449 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16126 : : /* Don't convert xobj member functions to METHOD_TYPE. */
16127 : 100646671 : && !is_xobj_member_function
16128 : 631820600 : && !(unqualified_id
16129 : 183750848 : && identifier_p (unqualified_id)
16130 : 91881569 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16131 : : {
16132 : 91869279 : cp_cv_quals real_quals = memfn_quals;
16133 : 91869279 : if (cxx_dialect < cxx14 && constexpr_p
16134 : 645936 : && sfk != sfk_constructor && sfk != sfk_destructor)
16135 : 3805 : real_quals |= TYPE_QUAL_CONST;
16136 : 91869279 : type = build_memfn_type (type, ctype, real_quals, rqual);
16137 : : }
16138 : :
16139 : 539938671 : {
16140 : 539938671 : tree decl = NULL_TREE;
16141 : :
16142 : 539938671 : if (decl_context == PARM)
16143 : : {
16144 : 279314284 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16145 : 279314284 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16146 : :
16147 : 279314284 : bad_specifiers (decl, BSP_PARM, virtualp,
16148 : : memfn_quals != TYPE_UNQUALIFIED,
16149 : : inlinep, friendp, raises != NULL_TREE,
16150 : : declspecs->locations);
16151 : : }
16152 : 260624387 : else if (decl_context == FIELD)
16153 : : {
16154 : 123611611 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16155 : 17246713 : if (tree auto_node = type_uses_auto (type))
16156 : : {
16157 : 22 : location_t tloc = declspecs->locations[ds_type_spec];
16158 : 22 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16159 : 4 : error_at (tloc, "invalid use of template-name %qE without an "
16160 : : "argument list",
16161 : 4 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16162 : : else
16163 : 18 : error_at (tloc, "non-static data member declared with "
16164 : : "placeholder %qT", auto_node);
16165 : 22 : type = error_mark_node;
16166 : : }
16167 : :
16168 : : /* The C99 flexible array extension. */
16169 : 104314344 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16170 : 124727839 : && TYPE_DOMAIN (type) == NULL_TREE)
16171 : : {
16172 : 1395 : if (ctype
16173 : 1395 : && (TREE_CODE (ctype) == UNION_TYPE
16174 : 1395 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16175 : 84 : pedwarn (id_loc, OPT_Wpedantic,
16176 : : "flexible array member in union is a GCC extension");
16177 : :
16178 : : else
16179 : : {
16180 : : /* Array is a flexible member. */
16181 : 1311 : if (name)
16182 : 1311 : pedwarn (id_loc, OPT_Wpedantic,
16183 : : "ISO C++ forbids flexible array member %qs", name);
16184 : : else
16185 : : pedwarn (input_location, OPT_Wpedantic,
16186 : : "ISO C++ forbids flexible array members");
16187 : :
16188 : : /* Flexible array member has a null domain. */
16189 : 1311 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16190 : : }
16191 : : }
16192 : :
16193 : 123611611 : if (type == error_mark_node)
16194 : : {
16195 : : /* Happens when declaring arrays of sizes which
16196 : : are error_mark_node, for example. */
16197 : 22 : decl = NULL_TREE;
16198 : : }
16199 : 123611589 : else if (in_namespace && !friendp)
16200 : : {
16201 : : /* Something like struct S { int N::j; }; */
16202 : 6 : error_at (id_loc, "invalid use of %<::%>");
16203 : 6 : return error_mark_node;
16204 : : }
16205 : 123611583 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16206 : : {
16207 : 95773985 : int publicp = 0;
16208 : 95773985 : tree function_context;
16209 : :
16210 : 95773985 : if (friendp == 0)
16211 : : {
16212 : : /* This should never happen in pure C++ (the check
16213 : : could be an assert). It could happen in
16214 : : Objective-C++ if someone writes invalid code that
16215 : : uses a function declaration for an instance
16216 : : variable or property (instance variables and
16217 : : properties are parsed as FIELD_DECLs, but they are
16218 : : part of an Objective-C class, not a C++ class).
16219 : : That code is invalid and is caught by this
16220 : : check. */
16221 : 91442362 : if (!ctype)
16222 : : {
16223 : 0 : error ("declaration of function %qD in invalid context",
16224 : : unqualified_id);
16225 : 0 : return error_mark_node;
16226 : : }
16227 : :
16228 : : /* ``A union may [ ... ] not [ have ] virtual functions.''
16229 : : ARM 9.5 */
16230 : 91442362 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16231 : : {
16232 : 3 : error_at (declspecs->locations[ds_virtual],
16233 : : "function %qD declared %<virtual%> inside a union",
16234 : : unqualified_id);
16235 : 3 : return error_mark_node;
16236 : : }
16237 : :
16238 : 4085220 : if (virtualp
16239 : 99859202 : && identifier_p (unqualified_id)
16240 : 4085220 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16241 : : {
16242 : 9 : error_at (declspecs->locations[ds_virtual],
16243 : : "%qD cannot be declared %<virtual%>, since it "
16244 : : "is always static", unqualified_id);
16245 : 9 : virtualp = 0;
16246 : : }
16247 : : }
16248 : :
16249 : : /* Check that the name used for a destructor makes sense. */
16250 : 95773982 : if (sfk == sfk_destructor)
16251 : : {
16252 : 3014573 : tree uqname = id_declarator->u.id.unqualified_name;
16253 : :
16254 : 3014573 : if (!ctype)
16255 : : {
16256 : 12 : gcc_assert (friendp);
16257 : 12 : error_at (id_loc, "expected qualified name in friend "
16258 : : "declaration for destructor %qD", uqname);
16259 : 12 : return error_mark_node;
16260 : : }
16261 : :
16262 : 3014561 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16263 : : {
16264 : 9 : error_at (id_loc, "declaration of %qD as member of %qT",
16265 : : uqname, ctype);
16266 : 9 : return error_mark_node;
16267 : : }
16268 : 3014552 : if (concept_p)
16269 : : {
16270 : 9 : error_at (declspecs->locations[ds_concept],
16271 : : "a destructor cannot be %qs", "concept");
16272 : 9 : return error_mark_node;
16273 : : }
16274 : 3014543 : if (constexpr_p && cxx_dialect < cxx20)
16275 : : {
16276 : 6 : error_at (declspecs->locations[ds_constexpr],
16277 : : "%<constexpr%> destructors only available"
16278 : : " with %<-std=c++20%> or %<-std=gnu++20%>");
16279 : 6 : return error_mark_node;
16280 : : }
16281 : 3014537 : if (consteval_p)
16282 : : {
16283 : 3 : error_at (declspecs->locations[ds_consteval],
16284 : : "a destructor cannot be %qs", "consteval");
16285 : 3 : return error_mark_node;
16286 : : }
16287 : : }
16288 : 92759409 : else if (sfk == sfk_constructor && friendp && !ctype)
16289 : : {
16290 : 0 : error ("expected qualified name in friend declaration "
16291 : : "for constructor %qD",
16292 : 0 : id_declarator->u.id.unqualified_name);
16293 : 0 : return error_mark_node;
16294 : : }
16295 : 95773943 : if (sfk == sfk_constructor)
16296 : 21617174 : if (concept_p)
16297 : : {
16298 : 9 : error_at (declspecs->locations[ds_concept],
16299 : : "a constructor cannot be %<concept%>");
16300 : 9 : return error_mark_node;
16301 : : }
16302 : 74156769 : if (concept_p)
16303 : : {
16304 : 9 : error_at (declspecs->locations[ds_concept],
16305 : : "a concept cannot be a member function");
16306 : 9 : concept_p = false;
16307 : : }
16308 : 95773925 : else if (consteval_p
16309 : 96013153 : && identifier_p (unqualified_id)
16310 : 96013144 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16311 : : {
16312 : 9 : error_at (declspecs->locations[ds_consteval],
16313 : : "%qD cannot be %qs", unqualified_id, "consteval");
16314 : 9 : consteval_p = false;
16315 : : }
16316 : :
16317 : 95773934 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16318 : : {
16319 : 40010 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
16320 : 40010 : if (variable_template_p (tmpl))
16321 : : {
16322 : 3 : auto_diagnostic_group d;
16323 : 3 : error_at (id_loc, "specialization of variable template "
16324 : : "%qD declared as function", tmpl);
16325 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
16326 : : "variable template declared here");
16327 : 3 : return error_mark_node;
16328 : 3 : }
16329 : : }
16330 : :
16331 : : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16332 : 95773931 : function_context
16333 : : = (ctype != NULL_TREE
16334 : 95773931 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16335 : 191547862 : publicp = ((! friendp || ! staticp)
16336 : 95773931 : && function_context == NULL_TREE);
16337 : :
16338 : 95773931 : decl = grokfndecl (ctype, type,
16339 : 95773931 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16340 : : ? unqualified_id : dname,
16341 : : parms,
16342 : : unqualified_id,
16343 : : declspecs,
16344 : : reqs,
16345 : : virtualp, flags, memfn_quals, rqual, raises,
16346 : : friendp ? -1 : 0, friendp, publicp,
16347 : 95773931 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16348 : 95773931 : | (8 * consteval_p),
16349 : : initialized == SD_DELETED,
16350 : : is_xobj_member_function, sfk,
16351 : : funcdef_flag, late_return_type_p,
16352 : : template_count, in_namespace,
16353 : : attrlist, id_loc);
16354 : 95773931 : decl = set_virt_specifiers (decl, virt_specifiers);
16355 : 95773931 : if (decl == NULL_TREE)
16356 : 98 : return error_mark_node;
16357 : : #if 0
16358 : : /* This clobbers the attrs stored in `decl' from `attrlist'. */
16359 : : /* The decl and setting of decl_attr is also turned off. */
16360 : : decl = build_decl_attribute_variant (decl, decl_attr);
16361 : : #endif
16362 : :
16363 : : /* [class.conv.ctor]
16364 : :
16365 : : A constructor declared without the function-specifier
16366 : : explicit that can be called with a single parameter
16367 : : specifies a conversion from the type of its first
16368 : : parameter to the type of its class. Such a constructor
16369 : : is called a converting constructor. */
16370 : 95773833 : if (explicitp == 2)
16371 : 5448432 : DECL_NONCONVERTING_P (decl) = 1;
16372 : :
16373 : 95773833 : if (declspecs->explicit_specifier)
16374 : 188696 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16375 : : }
16376 : 27837598 : else if (!staticp
16377 : 27837598 : && ((current_class_type
16378 : 17246708 : && same_type_p (TYPE_MAIN_VARIANT (type),
16379 : : current_class_type))
16380 : 17246612 : || (!dependent_type_p (type)
16381 : 10710728 : && !COMPLETE_TYPE_P (complete_type (type))
16382 : 1443 : && (!complete_or_array_type_p (type)
16383 : 1368 : || initialized == SD_UNINITIALIZED))))
16384 : : {
16385 : 1506 : if (TREE_CODE (type) != ARRAY_TYPE
16386 : 1506 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16387 : : {
16388 : 171 : if (unqualified_id)
16389 : : {
16390 : 126 : auto_diagnostic_group d;
16391 : 126 : error_at (id_loc, "field %qD has incomplete type %qT",
16392 : : unqualified_id, type);
16393 : 126 : cxx_incomplete_type_inform (strip_array_types (type));
16394 : 126 : }
16395 : : else
16396 : 45 : error ("name %qT has incomplete type", type);
16397 : :
16398 : 171 : type = error_mark_node;
16399 : 171 : decl = NULL_TREE;
16400 : : }
16401 : : }
16402 : 45081282 : else if (!verify_type_context (input_location,
16403 : : staticp
16404 : : ? TCTX_STATIC_STORAGE
16405 : : : TCTX_FIELD, type))
16406 : : {
16407 : 0 : type = error_mark_node;
16408 : 0 : decl = NULL_TREE;
16409 : : }
16410 : : else
16411 : : {
16412 : 27836086 : if (friendp)
16413 : : {
16414 : 14 : if (unqualified_id)
16415 : 11 : error_at (id_loc,
16416 : : "%qE is neither function nor member function; "
16417 : : "cannot be declared friend", unqualified_id);
16418 : : else
16419 : 3 : error ("unnamed field is neither function nor member "
16420 : : "function; cannot be declared friend");
16421 : 14 : return error_mark_node;
16422 : : }
16423 : 27836072 : decl = NULL_TREE;
16424 : : }
16425 : :
16426 : 123611433 : if (friendp)
16427 : : {
16428 : : /* Packages tend to use GNU attributes on friends, so we only
16429 : : warn for standard attributes. */
16430 : 4331594 : if (attrlist
16431 : 4331594 : && !funcdef_flag
16432 : 1338293 : && cxx11_attribute_p (*attrlist)
16433 : 4331623 : && !all_attributes_are_contracts_p (*attrlist))
16434 : : {
16435 : 27 : *attrlist = NULL_TREE;
16436 : 27 : auto_diagnostic_group d;
16437 : 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16438 : 27 : inform (id_loc, "an attribute that appertains to a friend "
16439 : : "declaration that is not a definition is ignored");
16440 : 27 : }
16441 : : /* Friends are treated specially. */
16442 : 4331594 : if (ctype == current_class_type)
16443 : : ; /* We already issued a permerror. */
16444 : 4331594 : else if (decl && DECL_NAME (decl))
16445 : : {
16446 : 4331591 : set_originating_module (decl, true);
16447 : :
16448 : 4331591 : if (initialized)
16449 : : /* Kludge: We need funcdef_flag to be true in do_friend for
16450 : : in-class defaulted functions, but that breaks grokfndecl.
16451 : : So set it here. */
16452 : 59097 : funcdef_flag = true;
16453 : :
16454 : 4331591 : cplus_decl_attributes (&decl, *attrlist, 0);
16455 : 4331591 : *attrlist = NULL_TREE;
16456 : :
16457 : 4331591 : tree scope = ctype ? ctype : in_namespace;
16458 : 4331591 : decl = do_friend (scope, unqualified_id, decl,
16459 : : flags, funcdef_flag);
16460 : 4331591 : return decl;
16461 : : }
16462 : : else
16463 : 3 : return error_mark_node;
16464 : : }
16465 : :
16466 : : /* Structure field. It may not be a function, except for C++. */
16467 : :
16468 : 119279839 : if (decl == NULL_TREE)
16469 : : {
16470 : 27837597 : if (staticp)
16471 : : {
16472 : : /* C++ allows static class members. All other work
16473 : : for this is done by grokfield. */
16474 : 10590890 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
16475 : : dname, type);
16476 : 10590890 : if (unqualified_id
16477 : 10590887 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16478 : : {
16479 : 84102 : decl = check_explicit_specialization (unqualified_id, decl,
16480 : : template_count,
16481 : : concept_p * 8);
16482 : 84102 : if (decl == error_mark_node)
16483 : : return error_mark_node;
16484 : : }
16485 : 10590890 : set_linkage_for_static_data_member (decl);
16486 : 10590890 : if (concept_p)
16487 : 15 : error_at (declspecs->locations[ds_concept],
16488 : : "static data member %qE declared %qs",
16489 : : unqualified_id, "concept");
16490 : 10590875 : else if (constexpr_p && !initialized)
16491 : : {
16492 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
16493 : : "%<constexpr%> static data member %qD must "
16494 : : "have an initializer", decl);
16495 : 9 : constexpr_p = false;
16496 : : }
16497 : 10590890 : if (consteval_p)
16498 : 3 : error_at (declspecs->locations[ds_consteval],
16499 : : "static data member %qE declared %qs",
16500 : : unqualified_id, "consteval");
16501 : :
16502 : 10590890 : if (inlinep)
16503 : 11292 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16504 : :
16505 : 10590890 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16506 : 10590890 : && !(cxx_dialect >= cxx17 && constexpr_p))
16507 : : /* Even if there is an in-class initialization, DECL
16508 : : is considered undefined until an out-of-class
16509 : : definition is provided, unless this is an inline
16510 : : variable. */
16511 : 1737032 : DECL_EXTERNAL (decl) = 1;
16512 : :
16513 : 10590890 : if (thread_p)
16514 : : {
16515 : 420 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16516 : 420 : if (!processing_template_decl)
16517 : 374 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16518 : 420 : if (declspecs->gnu_thread_keyword_p)
16519 : 34 : SET_DECL_GNU_TLS_P (decl);
16520 : : }
16521 : :
16522 : : /* Set the constraints on the declaration. */
16523 : 10590890 : bool memtmpl = (current_template_depth
16524 : 10590890 : > template_class_depth (current_class_type));
16525 : 10590890 : if (memtmpl)
16526 : : {
16527 : 316482 : tree ci = current_template_constraints ();
16528 : 316482 : set_constraints (decl, ci);
16529 : : }
16530 : : }
16531 : : else
16532 : : {
16533 : 17246707 : if (concept_p)
16534 : : {
16535 : 12 : error_at (declspecs->locations[ds_concept],
16536 : : "non-static data member %qE declared %qs",
16537 : : unqualified_id, "concept");
16538 : 12 : concept_p = false;
16539 : 12 : constexpr_p = false;
16540 : : }
16541 : 17246695 : else if (constexpr_p)
16542 : : {
16543 : 9 : error_at (declspecs->locations[ds_constexpr],
16544 : : "non-static data member %qE declared %qs",
16545 : : unqualified_id, "constexpr");
16546 : 9 : constexpr_p = false;
16547 : : }
16548 : 17246707 : if (constinit_p)
16549 : : {
16550 : 6 : error_at (declspecs->locations[ds_constinit],
16551 : : "non-static data member %qE declared %qs",
16552 : : unqualified_id, "constinit");
16553 : 6 : constinit_p = false;
16554 : : }
16555 : 17246707 : if (consteval_p)
16556 : : {
16557 : 3 : error_at (declspecs->locations[ds_consteval],
16558 : : "non-static data member %qE declared %qs",
16559 : : unqualified_id, "consteval");
16560 : 3 : consteval_p = false;
16561 : : }
16562 : 17246707 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16563 : 17246707 : DECL_NONADDRESSABLE_P (decl) = bitfield;
16564 : 17246707 : if (bitfield && !unqualified_id)
16565 : 137469 : DECL_PADDING_P (decl) = 1;
16566 : :
16567 : 17246707 : if (storage_class == sc_mutable)
16568 : : {
16569 : 147225 : DECL_MUTABLE_P (decl) = 1;
16570 : 147225 : storage_class = sc_none;
16571 : : }
16572 : :
16573 : 17246707 : if (initialized)
16574 : : {
16575 : : /* An attempt is being made to initialize a non-static
16576 : : member. This is new in C++11. */
16577 : 1253413 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
16578 : :
16579 : : /* If this has been parsed with static storage class, but
16580 : : errors forced staticp to be cleared, ensure NSDMI is
16581 : : not present. */
16582 : 1253413 : if (declspecs->storage_class == sc_static)
16583 : 3 : DECL_INITIAL (decl) = error_mark_node;
16584 : : }
16585 : : }
16586 : :
16587 : 45084304 : bad_specifiers (decl, BSP_FIELD, virtualp,
16588 : : memfn_quals != TYPE_UNQUALIFIED,
16589 : : staticp ? false : inlinep, friendp,
16590 : : raises != NULL_TREE,
16591 : : declspecs->locations);
16592 : : }
16593 : : }
16594 : 137012776 : else if (FUNC_OR_METHOD_TYPE_P (type))
16595 : : {
16596 : 79658998 : tree original_name;
16597 : 79658998 : int publicp = 0;
16598 : :
16599 : 79658998 : if (!unqualified_id)
16600 : 0 : return error_mark_node;
16601 : :
16602 : 79658998 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16603 : : original_name = dname;
16604 : : else
16605 : 78783714 : original_name = unqualified_id;
16606 : : // FIXME:gcc_assert (original_name == dname);
16607 : :
16608 : 79658998 : if (storage_class == sc_auto)
16609 : 1 : error_at (declspecs->locations[ds_storage_class],
16610 : : "storage class %<auto%> invalid for function %qs", name);
16611 : 79658997 : else if (storage_class == sc_register)
16612 : 7 : error_at (declspecs->locations[ds_storage_class],
16613 : : "storage class %<register%> invalid for function %qs",
16614 : : name);
16615 : 79658990 : else if (thread_p)
16616 : : {
16617 : 12 : if (declspecs->gnu_thread_keyword_p)
16618 : 12 : error_at (declspecs->locations[ds_thread],
16619 : : "storage class %<__thread%> invalid for function %qs",
16620 : : name);
16621 : : else
16622 : 0 : error_at (declspecs->locations[ds_thread],
16623 : : "storage class %<thread_local%> invalid for "
16624 : : "function %qs", name);
16625 : : }
16626 : 79658978 : else if (constinit_p)
16627 : 3 : error_at (declspecs->locations[ds_constinit],
16628 : : "%<constinit%> specifier invalid for function %qs", name);
16629 : :
16630 : 79658998 : if (virt_specifiers)
16631 : 6 : error ("virt-specifiers in %qs not allowed outside a class "
16632 : : "definition", name);
16633 : : /* Function declaration not at top level.
16634 : : Storage classes other than `extern' are not allowed
16635 : : and `extern' makes no difference. */
16636 : 79658998 : if (! toplevel_bindings_p ()
16637 : 39221 : && (storage_class == sc_static
16638 : 39212 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
16639 : 79659010 : && pedantic)
16640 : : {
16641 : 9 : if (storage_class == sc_static)
16642 : 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
16643 : : "%<static%> specifier invalid for function %qs "
16644 : : "declared out of global scope", name);
16645 : : else
16646 : 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
16647 : : "%<inline%> specifier invalid for function %qs "
16648 : : "declared out of global scope", name);
16649 : : }
16650 : :
16651 : 79658998 : if (ctype == NULL_TREE)
16652 : : {
16653 : 70454910 : if (virtualp)
16654 : : {
16655 : 0 : error ("virtual non-class function %qs", name);
16656 : 0 : virtualp = 0;
16657 : : }
16658 : 70454910 : else if (sfk == sfk_constructor
16659 : 70454910 : || sfk == sfk_destructor)
16660 : : {
16661 : 3 : error (funcdef_flag
16662 : : ? G_("%qs defined in a non-class scope")
16663 : : : G_("%qs declared in a non-class scope"), name);
16664 : 3 : sfk = sfk_none;
16665 : : }
16666 : : }
16667 : 79658998 : if (consteval_p
16668 : 79706744 : && identifier_p (unqualified_id)
16669 : 79706744 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16670 : : {
16671 : 9 : error_at (declspecs->locations[ds_consteval],
16672 : : "%qD cannot be %qs", unqualified_id, "consteval");
16673 : 9 : consteval_p = false;
16674 : : }
16675 : :
16676 : : /* Record whether the function is public. */
16677 : 79658998 : publicp = (ctype != NULL_TREE
16678 : 79658998 : || storage_class != sc_static);
16679 : :
16680 : 159317996 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
16681 : : declspecs,
16682 : : reqs, virtualp, flags, memfn_quals, rqual, raises,
16683 : : 1, friendp,
16684 : : publicp,
16685 : 79658998 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16686 : 79658998 : | (8 * consteval_p),
16687 : : initialized == SD_DELETED,
16688 : : is_xobj_member_function, sfk,
16689 : : funcdef_flag,
16690 : : late_return_type_p,
16691 : : template_count, in_namespace, attrlist,
16692 : : id_loc);
16693 : 79658998 : if (decl == NULL_TREE)
16694 : 506 : return error_mark_node;
16695 : :
16696 : 79658492 : if (explicitp == 2)
16697 : 5359 : DECL_NONCONVERTING_P (decl) = 1;
16698 : 79658492 : if (staticp == 1)
16699 : : {
16700 : 109017 : int invalid_static = 0;
16701 : :
16702 : : /* Don't allow a static member function in a class, and forbid
16703 : : declaring main to be static. */
16704 : 109017 : if (TREE_CODE (type) == METHOD_TYPE)
16705 : : {
16706 : 0 : permerror (input_location, "cannot declare member function %qD to have "
16707 : : "static linkage", decl);
16708 : 0 : invalid_static = 1;
16709 : : }
16710 : 109017 : else if (current_function_decl)
16711 : : {
16712 : : /* 7.1.1: There can be no static function declarations within a
16713 : : block. */
16714 : 9 : error_at (declspecs->locations[ds_storage_class],
16715 : : "cannot declare static function inside another function");
16716 : 9 : invalid_static = 1;
16717 : : }
16718 : :
16719 : 9 : if (invalid_static)
16720 : : {
16721 : 79658492 : staticp = 0;
16722 : : storage_class = sc_none;
16723 : : }
16724 : : }
16725 : 79658492 : if (declspecs->explicit_specifier)
16726 : 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16727 : : }
16728 : : else
16729 : : {
16730 : : /* It's a variable. */
16731 : :
16732 : : /* An uninitialized decl with `extern' is a reference. */
16733 : 114316381 : decl = grokvardecl (type, dname, unqualified_id,
16734 : : declspecs,
16735 : : initialized,
16736 : : type_quals,
16737 : : inlinep,
16738 : : concept_p,
16739 : : template_count,
16740 : : ctype ? ctype : in_namespace,
16741 : : id_loc);
16742 : 57353778 : if (decl == NULL_TREE)
16743 : 49 : return error_mark_node;
16744 : :
16745 : 57353729 : bad_specifiers (decl, BSP_VAR, virtualp,
16746 : : memfn_quals != TYPE_UNQUALIFIED,
16747 : : inlinep, friendp, raises != NULL_TREE,
16748 : : declspecs->locations);
16749 : :
16750 : 57353729 : if (ctype)
16751 : : {
16752 : 391169 : DECL_CONTEXT (decl) = ctype;
16753 : 391169 : if (staticp == 1)
16754 : : {
16755 : 6 : permerror (declspecs->locations[ds_storage_class],
16756 : : "%<static%> may not be used when defining "
16757 : : "(as opposed to declaring) a static data member");
16758 : 6 : staticp = 0;
16759 : 6 : storage_class = sc_none;
16760 : : }
16761 : 391169 : if (storage_class == sc_register && TREE_STATIC (decl))
16762 : : {
16763 : 0 : error ("static member %qD declared %<register%>", decl);
16764 : 0 : storage_class = sc_none;
16765 : : }
16766 : 391169 : if (storage_class == sc_extern && pedantic)
16767 : : {
16768 : 0 : pedwarn (input_location, OPT_Wpedantic,
16769 : : "cannot explicitly declare member %q#D to have "
16770 : : "extern linkage", decl);
16771 : 0 : storage_class = sc_none;
16772 : : }
16773 : : }
16774 : 56962560 : else if (constexpr_p && DECL_EXTERNAL (decl))
16775 : : {
16776 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
16777 : : "declaration of %<constexpr%> variable %qD "
16778 : : "is not a definition", decl);
16779 : 12 : constexpr_p = false;
16780 : : }
16781 : 57353729 : if (consteval_p)
16782 : : {
16783 : 13 : error_at (DECL_SOURCE_LOCATION (decl),
16784 : : "a variable cannot be declared %<consteval%>");
16785 : 13 : consteval_p = false;
16786 : : }
16787 : :
16788 : 57353729 : if (inlinep)
16789 : 4032207 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16790 : 57353729 : if (innermost_code == cdk_decomp)
16791 : : {
16792 : 86941 : gcc_assert (declarator && declarator->kind == cdk_decomp);
16793 : 86941 : DECL_SOURCE_LOCATION (decl) = id_loc;
16794 : 86941 : DECL_ARTIFICIAL (decl) = 1;
16795 : 86941 : fit_decomposition_lang_decl (decl, NULL_TREE);
16796 : : }
16797 : : }
16798 : :
16799 : 535606344 : if (VAR_P (decl) && !initialized)
16800 : 6874434 : if (tree auto_node = type_uses_auto (type))
16801 : 97 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16802 : : {
16803 : 18 : location_t loc = declspecs->locations[ds_type_spec];
16804 : 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
16805 : 18 : TREE_TYPE (decl) = error_mark_node;
16806 : : }
16807 : :
16808 : 535606344 : if (storage_class == sc_extern && initialized && !funcdef_flag)
16809 : : {
16810 : 260 : if (toplevel_bindings_p ())
16811 : : {
16812 : : /* It's common practice (and completely valid) to have a const
16813 : : be initialized and declared extern. */
16814 : 236 : if (!(type_quals & TYPE_QUAL_CONST))
16815 : 19 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
16816 : : "%qs initialized and declared %<extern%>", name);
16817 : : }
16818 : : else
16819 : : {
16820 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
16821 : : "%qs has both %<extern%> and initializer", name);
16822 : 24 : return error_mark_node;
16823 : : }
16824 : : }
16825 : :
16826 : : /* Record `register' declaration for warnings on &
16827 : : and in case doing stupid register allocation. */
16828 : :
16829 : 535606320 : if (storage_class == sc_register)
16830 : : {
16831 : 2060 : DECL_REGISTER (decl) = 1;
16832 : : /* Warn about register storage specifiers on PARM_DECLs. */
16833 : 2060 : if (TREE_CODE (decl) == PARM_DECL)
16834 : : {
16835 : 35 : if (cxx_dialect >= cxx17)
16836 : 22 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16837 : : "ISO C++17 does not allow %<register%> storage "
16838 : : "class specifier");
16839 : : else
16840 : 13 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16841 : : "%<register%> storage class specifier used");
16842 : : }
16843 : : }
16844 : 535604260 : else if (storage_class == sc_extern)
16845 : 32026246 : DECL_THIS_EXTERN (decl) = 1;
16846 : 503578014 : else if (storage_class == sc_static)
16847 : 19622319 : DECL_THIS_STATIC (decl) = 1;
16848 : :
16849 : 535606320 : if (VAR_P (decl))
16850 : : {
16851 : : /* Set constexpr flag on vars (functions got it in grokfndecl). */
16852 : 67944595 : if (constexpr_p)
16853 : 13989022 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
16854 : : /* And the constinit flag (which only applies to variables). */
16855 : 53955573 : else if (constinit_p)
16856 : 436 : DECL_DECLARED_CONSTINIT_P (decl) = true;
16857 : : }
16858 : 467661725 : else if (TREE_CODE (decl) == FUNCTION_DECL)
16859 : : {
16860 : : /* If we saw a return type, record its location. */
16861 : 171100734 : location_t loc = declspecs->locations[ds_type_spec];
16862 : 171100734 : if (loc == UNKNOWN_LOCATION)
16863 : : /* Build DECL_RESULT in start_preparsed_function. */;
16864 : 141216549 : else if (!DECL_RESULT (decl))
16865 : : {
16866 : 141179987 : tree restype = TREE_TYPE (TREE_TYPE (decl));
16867 : 141179987 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
16868 : 141179987 : DECL_ARTIFICIAL (resdecl) = 1;
16869 : 141179987 : DECL_IGNORED_P (resdecl) = 1;
16870 : 141179987 : DECL_RESULT (decl) = resdecl;
16871 : : }
16872 : 36562 : else if (funcdef_flag)
16873 : 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
16874 : : }
16875 : :
16876 : : /* Record constancy and volatility on the DECL itself . There's
16877 : : no need to do this when processing a template; we'll do this
16878 : : for the instantiated declaration based on the type of DECL. */
16879 : 535606320 : if (!processing_template_decl
16880 : : /* Don't do it for instantiated variable templates either,
16881 : : cp_apply_type_quals_to_decl should have been called on it
16882 : : already and might have been overridden in cp_finish_decl
16883 : : if initializer needs runtime initialization. */
16884 : 535606320 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
16885 : 182742920 : cp_apply_type_quals_to_decl (type_quals, decl);
16886 : :
16887 : 535606320 : return decl;
16888 : : }
16889 : 931249038 : }
16890 : :
16891 : : /* Subroutine of start_function. Ensure that each of the parameter
16892 : : types (as listed in PARMS) is complete, as is required for a
16893 : : function definition. */
16894 : :
16895 : : static void
16896 : 143882274 : require_complete_types_for_parms (tree parms)
16897 : : {
16898 : 426716518 : for (; parms; parms = DECL_CHAIN (parms))
16899 : : {
16900 : 282834244 : if (dependent_type_p (TREE_TYPE (parms)))
16901 : 163433893 : continue;
16902 : 119400351 : if (!VOID_TYPE_P (TREE_TYPE (parms))
16903 : 119400351 : && complete_type_or_else (TREE_TYPE (parms), parms))
16904 : : {
16905 : 119400270 : relayout_decl (parms);
16906 : 119400270 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
16907 : :
16908 : 119400270 : abstract_virtuals_error (parms, TREE_TYPE (parms));
16909 : 119400270 : maybe_warn_parm_abi (TREE_TYPE (parms),
16910 : 119400270 : DECL_SOURCE_LOCATION (parms));
16911 : : }
16912 : : else
16913 : : /* grokparms or complete_type_or_else will have already issued
16914 : : an error. */
16915 : 81 : TREE_TYPE (parms) = error_mark_node;
16916 : : }
16917 : 143882274 : }
16918 : :
16919 : : /* Returns nonzero if T is a local variable. */
16920 : :
16921 : : int
16922 : 153591788 : local_variable_p (const_tree t)
16923 : : {
16924 : 153591788 : if ((VAR_P (t)
16925 : 68037408 : && (DECL_LOCAL_DECL_P (t)
16926 : 68037408 : || !DECL_CONTEXT (t)
16927 : 67994654 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
16928 : 157262130 : || (TREE_CODE (t) == PARM_DECL))
16929 : 137677033 : return 1;
16930 : :
16931 : : return 0;
16932 : : }
16933 : :
16934 : : /* Like local_variable_p, but suitable for use as a tree-walking
16935 : : function. */
16936 : :
16937 : : static tree
16938 : 5037480 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
16939 : : void * /*data*/)
16940 : : {
16941 : 5037480 : if (unevaluated_p (TREE_CODE (*tp)))
16942 : : {
16943 : : /* DR 2082 permits local variables in unevaluated contexts
16944 : : within a default argument. */
16945 : 15 : *walk_subtrees = 0;
16946 : 15 : return NULL_TREE;
16947 : : }
16948 : :
16949 : 5037465 : if (local_variable_p (*tp)
16950 : 5037465 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
16951 : : return *tp;
16952 : 5037465 : else if (TYPE_P (*tp))
16953 : 30 : *walk_subtrees = 0;
16954 : :
16955 : : return NULL_TREE;
16956 : : }
16957 : :
16958 : : /* Check that ARG, which is a default-argument expression for a
16959 : : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
16960 : : something goes wrong. DECL may also be a _TYPE node, rather than a
16961 : : DECL, if there is no DECL available. */
16962 : :
16963 : : tree
16964 : 2603430 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
16965 : : {
16966 : 2603430 : tree var;
16967 : 2603430 : tree decl_type;
16968 : :
16969 : 2603430 : if (TREE_CODE (arg) == DEFERRED_PARSE)
16970 : : /* We get a DEFERRED_PARSE when looking at an in-class declaration
16971 : : with a default argument. Ignore the argument for now; we'll
16972 : : deal with it after the class is complete. */
16973 : : return arg;
16974 : :
16975 : 1882100 : if (TYPE_P (decl))
16976 : : {
16977 : : decl_type = decl;
16978 : : decl = NULL_TREE;
16979 : : }
16980 : : else
16981 : 563745 : decl_type = TREE_TYPE (decl);
16982 : :
16983 : 1882100 : if (arg == error_mark_node
16984 : 1881982 : || decl == error_mark_node
16985 : 1881973 : || TREE_TYPE (arg) == error_mark_node
16986 : 3764073 : || decl_type == error_mark_node)
16987 : : /* Something already went wrong. There's no need to check
16988 : : further. */
16989 : : return error_mark_node;
16990 : :
16991 : : /* [dcl.fct.default]
16992 : :
16993 : : A default argument expression is implicitly converted to the
16994 : : parameter type. */
16995 : 1881973 : ++cp_unevaluated_operand;
16996 : : /* Avoid digest_init clobbering the initializer. */
16997 : 1881973 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
16998 : 1881973 : perform_implicit_conversion_flags (decl_type, carg, complain,
16999 : : LOOKUP_IMPLICIT);
17000 : 1881973 : --cp_unevaluated_operand;
17001 : :
17002 : : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
17003 : : the call sites. */
17004 : 1383508 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17005 : 498480 : && null_ptr_cst_p (arg)
17006 : : /* Don't lose side-effects as in PR90473. */
17007 : 2257136 : && !TREE_SIDE_EFFECTS (arg))
17008 : 375160 : return nullptr_node;
17009 : :
17010 : : /* [dcl.fct.default]
17011 : :
17012 : : Local variables shall not be used in default argument
17013 : : expressions.
17014 : :
17015 : : The keyword `this' shall not be used in a default argument of a
17016 : : member function. */
17017 : 1506813 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17018 : 1506813 : if (var)
17019 : : {
17020 : 0 : if (complain & tf_warning_or_error)
17021 : : {
17022 : 0 : if (DECL_NAME (var) == this_identifier)
17023 : 0 : permerror (input_location, "default argument %qE uses %qD",
17024 : : arg, var);
17025 : : else
17026 : 0 : error ("default argument %qE uses local variable %qD", arg, var);
17027 : : }
17028 : 0 : return error_mark_node;
17029 : : }
17030 : :
17031 : : /* All is well. */
17032 : 1506813 : return arg;
17033 : : }
17034 : :
17035 : : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17036 : :
17037 : : static tree
17038 : 265717788 : type_is_deprecated (tree type)
17039 : : {
17040 : 391948727 : enum tree_code code;
17041 : 391948727 : if (TREE_DEPRECATED (type))
17042 : : return type;
17043 : 391906486 : if (TYPE_NAME (type))
17044 : : {
17045 : 261413266 : if (TREE_DEPRECATED (TYPE_NAME (type)))
17046 : : return type;
17047 : : else
17048 : : {
17049 : 261413260 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17050 : 261413260 : return NULL_TREE;
17051 : : }
17052 : : }
17053 : :
17054 : : /* Do warn about using typedefs to a deprecated class. */
17055 : 130493220 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17056 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17057 : :
17058 : 130493220 : code = TREE_CODE (type);
17059 : :
17060 : 130493220 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17061 : 130493220 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17062 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
17063 : 126115525 : return type_is_deprecated (TREE_TYPE (type));
17064 : :
17065 : 115414 : if (TYPE_PTRMEMFUNC_P (type))
17066 : 115414 : return type_is_deprecated
17067 : 115414 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17068 : :
17069 : : return NULL_TREE;
17070 : : }
17071 : :
17072 : : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17073 : :
17074 : : static tree
17075 : 266620491 : type_is_unavailable (tree type)
17076 : : {
17077 : 393406794 : enum tree_code code;
17078 : 393406794 : if (TREE_UNAVAILABLE (type))
17079 : : return type;
17080 : 393406788 : if (TYPE_NAME (type))
17081 : : {
17082 : 262328468 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17083 : : return type;
17084 : : else
17085 : : {
17086 : 262328462 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17087 : 262328462 : return NULL_TREE;
17088 : : }
17089 : : }
17090 : :
17091 : : /* Do warn about using typedefs to a deprecated class. */
17092 : 131078320 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17093 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17094 : :
17095 : 131078320 : code = TREE_CODE (type);
17096 : :
17097 : 131078320 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17098 : 131078320 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17099 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
17100 : 126561873 : return type_is_unavailable (TREE_TYPE (type));
17101 : :
17102 : 224430 : if (TYPE_PTRMEMFUNC_P (type))
17103 : 224430 : return type_is_unavailable
17104 : 224430 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17105 : :
17106 : : return NULL_TREE;
17107 : : }
17108 : :
17109 : : /* Decode the list of parameter types for a function type.
17110 : : Given the list of things declared inside the parens,
17111 : : return a list of types.
17112 : :
17113 : : If this parameter does not end with an ellipsis, we append
17114 : : void_list_node.
17115 : :
17116 : : *PARMS is set to the chain of PARM_DECLs created. */
17117 : :
17118 : : tree
17119 : 178743743 : grokparms (tree parmlist, tree *parms)
17120 : : {
17121 : 178743743 : tree result = NULL_TREE;
17122 : 178743743 : tree decls = NULL_TREE;
17123 : 178743743 : tree parm;
17124 : 178743743 : int any_error = 0;
17125 : :
17126 : 445986102 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17127 : : {
17128 : 444613992 : tree type = NULL_TREE;
17129 : 444613992 : tree init = TREE_PURPOSE (parm);
17130 : 444613992 : tree decl = TREE_VALUE (parm);
17131 : :
17132 : 444613992 : if (parm == void_list_node || parm == explicit_void_list_node)
17133 : : break;
17134 : :
17135 : 267242371 : if (! decl || TREE_TYPE (decl) == error_mark_node)
17136 : : {
17137 : 185 : any_error = 1;
17138 : 185 : continue;
17139 : : }
17140 : :
17141 : 267242186 : type = TREE_TYPE (decl);
17142 : 267242186 : if (VOID_TYPE_P (type))
17143 : : {
17144 : 78 : if (same_type_p (type, void_type_node)
17145 : 69 : && !init
17146 : 57 : && !DECL_NAME (decl) && !result
17147 : 102 : && TREE_CHAIN (parm) == void_list_node)
17148 : : /* DR 577: A parameter list consisting of a single
17149 : : unnamed parameter of non-dependent type 'void'. */
17150 : : break;
17151 : 66 : else if (cv_qualified_p (type))
17152 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17153 : : "invalid use of cv-qualified type %qT in "
17154 : : "parameter declaration", type);
17155 : : else
17156 : 57 : error_at (DECL_SOURCE_LOCATION (decl),
17157 : : "invalid use of type %<void%> in parameter "
17158 : : "declaration");
17159 : : /* It's not a good idea to actually create parameters of
17160 : : type `void'; other parts of the compiler assume that a
17161 : : void type terminates the parameter list. */
17162 : 66 : type = error_mark_node;
17163 : 66 : TREE_TYPE (decl) = error_mark_node;
17164 : : }
17165 : :
17166 : 267242174 : if (type != error_mark_node)
17167 : : {
17168 : 267242108 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17169 : : {
17170 : 266620491 : tree unavailtype = type_is_unavailable (type);
17171 : 266620491 : if (unavailtype)
17172 : 12 : cp_handle_deprecated_or_unavailable (unavailtype);
17173 : : }
17174 : 267242108 : if (deprecated_state != DEPRECATED_SUPPRESS
17175 : 267242108 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17176 : : {
17177 : 265717788 : tree deptype = type_is_deprecated (type);
17178 : 265717788 : if (deptype)
17179 : 42247 : cp_handle_deprecated_or_unavailable (deptype);
17180 : : }
17181 : :
17182 : : /* [dcl.fct] "A parameter with volatile-qualified type is
17183 : : deprecated." */
17184 : 267242108 : if (CP_TYPE_VOLATILE_P (type))
17185 : 504 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17186 : : "%<volatile%>-qualified parameter is "
17187 : : "deprecated");
17188 : :
17189 : : /* Top-level qualifiers on the parameters are
17190 : : ignored for function types. */
17191 : 267242108 : type = cp_build_qualified_type (type, 0);
17192 : 267242108 : if (TREE_CODE (type) == METHOD_TYPE)
17193 : : {
17194 : 0 : error ("parameter %qD invalidly declared method type", decl);
17195 : 0 : type = build_pointer_type (type);
17196 : 0 : TREE_TYPE (decl) = type;
17197 : : }
17198 : 267242108 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17199 : : {
17200 : : /* Before C++17 DR 393:
17201 : : [dcl.fct]/6, parameter types cannot contain pointers
17202 : : (references) to arrays of unknown bound. */
17203 : 1612835 : tree t = TREE_TYPE (type);
17204 : 1612835 : int ptr = TYPE_PTR_P (type);
17205 : :
17206 : 1769589 : while (1)
17207 : : {
17208 : 1691212 : if (TYPE_PTR_P (t))
17209 : : ptr = 1;
17210 : 1615947 : else if (TREE_CODE (t) != ARRAY_TYPE)
17211 : : break;
17212 : 3122 : else if (!TYPE_DOMAIN (t))
17213 : : break;
17214 : 78377 : t = TREE_TYPE (t);
17215 : : }
17216 : 1612835 : if (TREE_CODE (t) == ARRAY_TYPE)
17217 : 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17218 : : ptr
17219 : : ? G_("parameter %qD includes pointer to array of "
17220 : : "unknown bound %qT")
17221 : : : G_("parameter %qD includes reference to array of "
17222 : : "unknown bound %qT"),
17223 : : decl, t);
17224 : : }
17225 : :
17226 : 267242108 : if (init && !processing_template_decl)
17227 : 1285066 : init = check_default_argument (decl, init, tf_warning_or_error);
17228 : : }
17229 : :
17230 : 267242174 : DECL_CHAIN (decl) = decls;
17231 : 267242174 : decls = decl;
17232 : 267242174 : result = tree_cons (init, type, result);
17233 : : }
17234 : 178743743 : decls = nreverse (decls);
17235 : 178743743 : result = nreverse (result);
17236 : 178743743 : if (parm)
17237 : 177371633 : result = chainon (result, void_list_node);
17238 : 178743743 : *parms = decls;
17239 : 178743743 : if (any_error)
17240 : 171 : result = NULL_TREE;
17241 : :
17242 : 171 : if (any_error)
17243 : : /* We had parm errors, recover by giving the function (...) type. */
17244 : 171 : result = NULL_TREE;
17245 : :
17246 : 178743743 : return result;
17247 : : }
17248 : :
17249 : :
17250 : : /* D is a constructor or overloaded `operator='.
17251 : :
17252 : : Let T be the class in which D is declared. Then, this function
17253 : : returns:
17254 : :
17255 : : -1 if D's is an ill-formed constructor or copy assignment operator
17256 : : whose first parameter is of type `T'.
17257 : : 0 if D is not a copy constructor or copy assignment
17258 : : operator.
17259 : : 1 if D is a copy constructor or copy assignment operator whose
17260 : : first parameter is a reference to non-const qualified T.
17261 : : 2 if D is a copy constructor or copy assignment operator whose
17262 : : first parameter is a reference to const qualified T.
17263 : :
17264 : : This function can be used as a predicate. Positive values indicate
17265 : : a copy constructor and nonzero values indicate a copy assignment
17266 : : operator. */
17267 : :
17268 : : int
17269 : 291083618 : copy_fn_p (const_tree d)
17270 : : {
17271 : 291083618 : tree args;
17272 : 291083618 : tree arg_type;
17273 : 291083618 : int result = 1;
17274 : :
17275 : 291083618 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17276 : :
17277 : 291083618 : if (TREE_CODE (d) == TEMPLATE_DECL
17278 : 291083618 : || (DECL_TEMPLATE_INFO (d)
17279 : 139724678 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17280 : : /* Instantiations of template member functions are never copy
17281 : : functions. Note that member functions of templated classes are
17282 : : represented as template functions internally, and we must
17283 : : accept those as copy functions. */
17284 : : return 0;
17285 : :
17286 : 244840835 : if (!DECL_CONSTRUCTOR_P (d)
17287 : 244840835 : && DECL_NAME (d) != assign_op_identifier)
17288 : : return 0;
17289 : :
17290 : 244070222 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17291 : : {
17292 : 154 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17293 : 154 : if (!TYPE_REF_P (object_param)
17294 : 148 : || TYPE_REF_IS_RVALUE (object_param)
17295 : : /* Reject unrelated object parameters. */
17296 : 136 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17297 : 290 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17298 : 24 : return 0;
17299 : 130 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17300 : : }
17301 : : else
17302 : 244070068 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17303 : 244070198 : if (!args)
17304 : : return 0;
17305 : :
17306 : 244062698 : arg_type = TREE_VALUE (args);
17307 : 244062698 : if (arg_type == error_mark_node)
17308 : : return 0;
17309 : :
17310 : 244062663 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17311 : : {
17312 : : /* Pass by value copy assignment operator. */
17313 : : result = -1;
17314 : : }
17315 : 244060574 : else if (TYPE_REF_P (arg_type)
17316 : 150712990 : && !TYPE_REF_IS_RVALUE (arg_type)
17317 : 341587663 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17318 : : {
17319 : 77084462 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17320 : 76995612 : result = 2;
17321 : : }
17322 : : else
17323 : : return 0;
17324 : :
17325 : 77086551 : args = TREE_CHAIN (args);
17326 : :
17327 : 79689225 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17328 : : /* There are more non-optional args. */
17329 : : return 0;
17330 : :
17331 : : return result;
17332 : : }
17333 : :
17334 : : /* D is a constructor or overloaded `operator='.
17335 : :
17336 : : Let T be the class in which D is declared. Then, this function
17337 : : returns true when D is a move constructor or move assignment
17338 : : operator, false otherwise. */
17339 : :
17340 : : bool
17341 : 117994242 : move_fn_p (const_tree d)
17342 : : {
17343 : 117994242 : if (cxx_dialect == cxx98)
17344 : : /* There are no move constructors if we are in C++98 mode. */
17345 : : return false;
17346 : :
17347 : 117929585 : if (TREE_CODE (d) == TEMPLATE_DECL
17348 : 117929585 : || (DECL_TEMPLATE_INFO (d)
17349 : 53445894 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17350 : : /* Instantiations of template member functions are never move
17351 : : functions. Note that member functions of templated classes are
17352 : : represented as template functions internally, and we must
17353 : : accept those as move functions. */
17354 : : return 0;
17355 : :
17356 : 97216966 : return move_signature_fn_p (d);
17357 : : }
17358 : :
17359 : : /* D is a constructor or overloaded `operator='.
17360 : :
17361 : : Then, this function returns true when D has the same signature as a move
17362 : : constructor or move assignment operator (because either it is such a
17363 : : ctor/op= or it is a template specialization with the same signature),
17364 : : false otherwise. */
17365 : :
17366 : : bool
17367 : 97216966 : move_signature_fn_p (const_tree d)
17368 : : {
17369 : 97216966 : tree args;
17370 : 97216966 : tree arg_type;
17371 : 97216966 : bool result = false;
17372 : :
17373 : 97216966 : if (!DECL_CONSTRUCTOR_P (d)
17374 : 97216966 : && DECL_NAME (d) != assign_op_identifier)
17375 : : return 0;
17376 : :
17377 : 97216902 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17378 : : {
17379 : 88 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17380 : 88 : if (!TYPE_REF_P (object_param)
17381 : 84 : || TYPE_REF_IS_RVALUE (object_param)
17382 : : /* Reject unrelated object parameters. */
17383 : 76 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17384 : 164 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17385 : 16 : return 0;
17386 : 72 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17387 : : }
17388 : : else
17389 : 97216814 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17390 : 97216886 : if (!args)
17391 : : return 0;
17392 : :
17393 : 97214134 : arg_type = TREE_VALUE (args);
17394 : 97214134 : if (arg_type == error_mark_node)
17395 : : return 0;
17396 : :
17397 : 97214126 : if (TYPE_REF_P (arg_type)
17398 : 47513544 : && TYPE_REF_IS_RVALUE (arg_type)
17399 : 134292461 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17400 : : DECL_CONTEXT (d)))
17401 : : result = true;
17402 : :
17403 : 97214126 : args = TREE_CHAIN (args);
17404 : :
17405 : 107943281 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17406 : : /* There are more non-optional args. */
17407 : : return false;
17408 : :
17409 : : return result;
17410 : : }
17411 : :
17412 : : /* Remember any special properties of member function DECL. */
17413 : :
17414 : : void
17415 : 295043813 : grok_special_member_properties (tree decl)
17416 : : {
17417 : 295043813 : tree class_type;
17418 : :
17419 : 295043813 : if (TREE_CODE (decl) == USING_DECL
17420 : 295043813 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17421 : : return;
17422 : :
17423 : 277255434 : class_type = DECL_CONTEXT (decl);
17424 : 277255434 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17425 : : {
17426 : 121852510 : int ctor = copy_fn_p (decl);
17427 : :
17428 : 121852510 : if (!DECL_ARTIFICIAL (decl))
17429 : 91849561 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17430 : :
17431 : 121852510 : if (ctor > 0)
17432 : : {
17433 : : /* [class.copy]
17434 : :
17435 : : A non-template constructor for class X is a copy
17436 : : constructor if its first parameter is of type X&, const
17437 : : X&, volatile X& or const volatile X&, and either there
17438 : : are no other parameters or else all other parameters have
17439 : : default arguments. */
17440 : 23719161 : TYPE_HAS_COPY_CTOR (class_type) = 1;
17441 : 23719161 : if (ctor > 1)
17442 : 23688302 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17443 : : }
17444 : :
17445 : 121852510 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17446 : 25209502 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17447 : :
17448 : 121852510 : if (is_list_ctor (decl))
17449 : 1442090 : TYPE_HAS_LIST_CTOR (class_type) = 1;
17450 : :
17451 : 121852510 : if (maybe_constexpr_fn (decl)
17452 : 121852510 : && !ctor && !move_fn_p (decl))
17453 : 30404611 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17454 : : }
17455 : 155402924 : else if (DECL_NAME (decl) == assign_op_identifier)
17456 : : {
17457 : : /* [class.copy]
17458 : :
17459 : : A non-template assignment operator for class X is a copy
17460 : : assignment operator if its parameter is of type X, X&, const
17461 : : X&, volatile X& or const volatile X&. */
17462 : :
17463 : 13477681 : int assop = copy_fn_p (decl);
17464 : :
17465 : 13477681 : if (assop)
17466 : : {
17467 : 6734614 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17468 : 6734614 : if (assop != 1)
17469 : 6704716 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17470 : : }
17471 : : }
17472 : 141925243 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17473 : 1943175 : TYPE_HAS_CONVERSION (class_type) = true;
17474 : :
17475 : : /* Destructors are handled in check_methods. */
17476 : : }
17477 : :
17478 : : /* Check a constructor DECL has the correct form. Complains
17479 : : if the class has a constructor of the form X(X). */
17480 : :
17481 : : bool
17482 : 42938898 : grok_ctor_properties (const_tree ctype, const_tree decl)
17483 : : {
17484 : 42938898 : int ctor_parm = copy_fn_p (decl);
17485 : :
17486 : 42938898 : if (ctor_parm < 0)
17487 : : {
17488 : : /* [class.copy]
17489 : :
17490 : : A declaration of a constructor for a class X is ill-formed if
17491 : : its first parameter is of type (optionally cv-qualified) X
17492 : : and either there are no other parameters or else all other
17493 : : parameters have default arguments.
17494 : :
17495 : : We *don't* complain about member template instantiations that
17496 : : have this form, though; they can occur as we try to decide
17497 : : what constructor to use during overload resolution. Since
17498 : : overload resolution will never prefer such a constructor to
17499 : : the non-template copy constructor (which is either explicitly
17500 : : or implicitly defined), there's no need to worry about their
17501 : : existence. Theoretically, they should never even be
17502 : : instantiated, but that's hard to forestall. */
17503 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17504 : : "invalid constructor; you probably meant %<%T (const %T&)%>",
17505 : : ctype, ctype);
17506 : 9 : return false;
17507 : : }
17508 : :
17509 : : return true;
17510 : : }
17511 : :
17512 : : /* DECL is a declaration for an overloaded or conversion operator. If
17513 : : COMPLAIN is true, errors are issued for invalid declarations. */
17514 : :
17515 : : bool
17516 : 52062626 : grok_op_properties (tree decl, bool complain)
17517 : : {
17518 : 52062626 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17519 : 52062626 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17520 : 52062626 : tree name = DECL_NAME (decl);
17521 : 52062626 : location_t loc = DECL_SOURCE_LOCATION (decl);
17522 : :
17523 : 52062626 : tree class_type = DECL_CONTEXT (decl);
17524 : 52062626 : if (class_type && !CLASS_TYPE_P (class_type))
17525 : : class_type = NULL_TREE;
17526 : :
17527 : 52062626 : tree_code operator_code;
17528 : 52062626 : unsigned op_flags;
17529 : 52062626 : if (IDENTIFIER_CONV_OP_P (name))
17530 : : {
17531 : : /* Conversion operators are TYPE_EXPR for the purposes of this
17532 : : function. */
17533 : : operator_code = TYPE_EXPR;
17534 : : op_flags = OVL_OP_FLAG_UNARY;
17535 : : }
17536 : : else
17537 : : {
17538 : 50457515 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17539 : :
17540 : 50457515 : operator_code = ovl_op->tree_code;
17541 : 50457515 : op_flags = ovl_op->flags;
17542 : 50457515 : gcc_checking_assert (operator_code != ERROR_MARK);
17543 : 50457515 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17544 : : }
17545 : :
17546 : 50457515 : if (op_flags & OVL_OP_FLAG_ALLOC)
17547 : : {
17548 : : /* operator new and operator delete are quite special. */
17549 : 334541 : if (class_type)
17550 : 13796 : switch (op_flags)
17551 : : {
17552 : 9793 : case OVL_OP_FLAG_ALLOC:
17553 : 9793 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17554 : 9793 : break;
17555 : :
17556 : 3736 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17557 : 3736 : TYPE_GETS_DELETE (class_type) |= 1;
17558 : 3736 : break;
17559 : :
17560 : 130 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17561 : 130 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17562 : 130 : break;
17563 : :
17564 : 137 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17565 : 137 : TYPE_GETS_DELETE (class_type) |= 2;
17566 : 137 : break;
17567 : :
17568 : 0 : default:
17569 : 0 : gcc_unreachable ();
17570 : : }
17571 : :
17572 : : /* [basic.std.dynamic.allocation]/1:
17573 : :
17574 : : A program is ill-formed if an allocation function is declared
17575 : : in a namespace scope other than global scope or declared
17576 : : static in global scope.
17577 : :
17578 : : The same also holds true for deallocation functions. */
17579 : 334541 : if (DECL_NAMESPACE_SCOPE_P (decl))
17580 : : {
17581 : 320745 : if (CP_DECL_CONTEXT (decl) != global_namespace)
17582 : : {
17583 : 6 : error_at (loc, "%qD may not be declared within a namespace",
17584 : : decl);
17585 : 6 : return false;
17586 : : }
17587 : :
17588 : 320739 : if (!TREE_PUBLIC (decl))
17589 : : {
17590 : 6 : error_at (loc, "%qD may not be declared as static", decl);
17591 : 6 : return false;
17592 : : }
17593 : : }
17594 : :
17595 : : /* Check for replaceable global new/delete operators with
17596 : : const std::nothrow_t & last argument, other replaceable global
17597 : : new/delete operators are marked in cxx_init_decl_processing. */
17598 : 334529 : if (CP_DECL_CONTEXT (decl) == global_namespace)
17599 : : {
17600 : 320733 : tree args = argtypes;
17601 : 320733 : if (args
17602 : 320721 : && args != void_list_node
17603 : 962157 : && same_type_p (TREE_VALUE (args),
17604 : : (op_flags & OVL_OP_FLAG_DELETE)
17605 : : ? ptr_type_node : size_type_node))
17606 : : {
17607 : 320691 : args = TREE_CHAIN (args);
17608 : 320691 : if (aligned_allocation_fn_p (decl))
17609 : 103364 : args = TREE_CHAIN (args);
17610 : 320691 : if (args
17611 : 320685 : && args != void_list_node
17612 : 534731 : && TREE_CHAIN (args) == void_list_node)
17613 : : {
17614 : 188155 : tree t = TREE_VALUE (args);
17615 : 188155 : if (TYPE_REF_P (t)
17616 : 106746 : && !TYPE_REF_IS_RVALUE (t)
17617 : 106746 : && (t = TREE_TYPE (t))
17618 : 106746 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
17619 : 294453 : && is_std_class (t, "nothrow_t"))
17620 : 106298 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
17621 : : }
17622 : : }
17623 : : }
17624 : :
17625 : 334529 : if (op_flags & OVL_OP_FLAG_DELETE)
17626 : : {
17627 : 189493 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
17628 : 189493 : coerce_delete_type (decl, loc);
17629 : : }
17630 : : else
17631 : : {
17632 : 145036 : DECL_SET_IS_OPERATOR_NEW (decl, true);
17633 : 145036 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
17634 : : }
17635 : :
17636 : 334529 : return true;
17637 : : }
17638 : :
17639 : : /* An operator function must either be a non-static member function
17640 : : or have at least one parameter of a class, a reference to a class,
17641 : : an enumeration, or a reference to an enumeration. 13.4.0.6 */
17642 : 51728085 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17643 : : {
17644 : 15006211 : if (operator_code == TYPE_EXPR
17645 : 15006211 : || operator_code == COMPONENT_REF
17646 : 15006190 : || operator_code == NOP_EXPR)
17647 : : {
17648 : 33 : error_at (loc, "%qD must be a non-static member function", decl);
17649 : 33 : return false;
17650 : : }
17651 : :
17652 : 15006178 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
17653 : : {
17654 : 3027 : if (! DECL_STATIC_FUNCTION_P (decl))
17655 : : {
17656 : 12 : error_at (loc, "%qD must be a member function", decl);
17657 : 12 : return false;
17658 : : }
17659 : 3015 : if (cxx_dialect < cxx23
17660 : : /* For lambdas we diagnose static lambda specifier elsewhere. */
17661 : 95 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
17662 : : /* For instantiations, we have diagnosed this already. */
17663 : 3047 : && ! DECL_USE_TEMPLATE (decl))
17664 : 26 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
17665 : : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
17666 : : decl);
17667 : 3015 : if (operator_code == ARRAY_REF)
17668 : : /* static operator[] should have exactly one argument
17669 : : for C++20 and earlier, so that it isn't multidimensional. */
17670 : : op_flags = OVL_OP_FLAG_UNARY;
17671 : : }
17672 : 15003151 : else if (DECL_STATIC_FUNCTION_P (decl))
17673 : : {
17674 : 9 : error_at (loc, "%qD must be either a non-static member "
17675 : : "function or a non-member function", decl);
17676 : 9 : return false;
17677 : : }
17678 : : else
17679 : 504840 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
17680 : : {
17681 : 15507982 : if (!arg || arg == void_list_node)
17682 : : {
17683 : 18 : if (complain)
17684 : 15 : error_at (loc, "%qD must have an argument of class or "
17685 : : "enumerated type", decl);
17686 : 18 : return false;
17687 : : }
17688 : :
17689 : 15507964 : tree type = non_reference (TREE_VALUE (arg));
17690 : 15507964 : if (type == error_mark_node)
17691 : : return false;
17692 : :
17693 : : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
17694 : : because these checks are performed even on template
17695 : : functions. */
17696 : 15507961 : if (MAYBE_CLASS_TYPE_P (type)
17697 : 1471233 : || TREE_CODE (type) == ENUMERAL_TYPE)
17698 : : break;
17699 : 504840 : }
17700 : : }
17701 : :
17702 : 51727974 : if (operator_code == CALL_EXPR)
17703 : : /* There are no further restrictions on the arguments to an overloaded
17704 : : "operator ()". */
17705 : : return true;
17706 : :
17707 : : /* C++23 allows an arbitrary number of parameters and default arguments for
17708 : : operator[], and none of the other checks below apply. */
17709 : 45072871 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
17710 : : return true;
17711 : :
17712 : 44695262 : if (operator_code == COND_EXPR)
17713 : : {
17714 : : /* 13.4.0.3 */
17715 : 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
17716 : 6 : return false;
17717 : : }
17718 : :
17719 : : /* Count the number of arguments and check for ellipsis. */
17720 : : int arity = 0;
17721 : 128072787 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
17722 : : {
17723 : 83377546 : if (!arg)
17724 : : {
17725 : 15 : error_at (loc, "%qD must not have variable number of arguments",
17726 : : decl);
17727 : 15 : return false;
17728 : : }
17729 : 83377531 : ++arity;
17730 : : }
17731 : : /* FIXME: We need tests for these errors with xobj member functions. */
17732 : : /* Verify correct number of arguments. */
17733 : 44695241 : switch (op_flags)
17734 : : {
17735 : 7811438 : case OVL_OP_FLAG_AMBIARY:
17736 : 7811438 : if (arity == 1)
17737 : : {
17738 : : /* We have a unary instance of an ambi-ary op. Remap to the
17739 : : unary one. */
17740 : 3466448 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
17741 : 3466448 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
17742 : 3466448 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
17743 : 3466448 : operator_code = ovl_op->tree_code;
17744 : 3466448 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17745 : : }
17746 : 4344990 : else if (arity != 2)
17747 : : {
17748 : : /* This was an ambiguous operator but is invalid. */
17749 : 3 : error_at (loc,
17750 : : methodp
17751 : : ? G_("%qD must have either zero or one argument")
17752 : : : G_("%qD must have either one or two arguments"), decl);
17753 : 3 : return false;
17754 : : }
17755 : 4344987 : else if ((operator_code == POSTINCREMENT_EXPR
17756 : 4344987 : || operator_code == POSTDECREMENT_EXPR)
17757 : 2099634 : && ! processing_template_decl
17758 : : /* x++ and x--'s second argument must be an int. */
17759 : 5733392 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
17760 : : integer_type_node))
17761 : : {
17762 : 15 : error_at (loc,
17763 : : methodp
17764 : : ? G_("postfix %qD must have %<int%> as its argument")
17765 : : : G_("postfix %qD must have %<int%> as its second argument"),
17766 : : decl);
17767 : 15 : return false;
17768 : : }
17769 : : break;
17770 : :
17771 : 2546533 : case OVL_OP_FLAG_UNARY:
17772 : 2546533 : if (arity != 1)
17773 : : {
17774 : 12 : error_at (loc,
17775 : : methodp
17776 : : ? G_("%qD must have no arguments")
17777 : : : G_("%qD must have exactly one argument"), decl);
17778 : 12 : return false;
17779 : : }
17780 : : break;
17781 : :
17782 : 34337270 : case OVL_OP_FLAG_BINARY:
17783 : 34337270 : if (arity != 2)
17784 : : {
17785 : 3 : error_at (loc,
17786 : : methodp
17787 : : ? G_("%qD must have exactly one argument")
17788 : : : G_("%qD must have exactly two arguments"), decl);
17789 : 3 : return false;
17790 : : }
17791 : : break;
17792 : :
17793 : 0 : default:
17794 : 0 : gcc_unreachable ();
17795 : : }
17796 : :
17797 : : /* There can be no default arguments. */
17798 : 128072640 : for (tree arg = argtypes; arg && arg != void_list_node;
17799 : 83377432 : arg = TREE_CHAIN (arg))
17800 : 83377447 : if (TREE_PURPOSE (arg))
17801 : : {
17802 : 15 : TREE_PURPOSE (arg) = NULL_TREE;
17803 : 15 : error_at (loc, "%qD cannot have default arguments", decl);
17804 : 15 : return false;
17805 : : }
17806 : :
17807 : : /* At this point the declaration is well-formed. It may not be
17808 : : sensible though. */
17809 : :
17810 : : /* Check member function warnings only on the in-class declaration.
17811 : : There's no point warning on an out-of-class definition. */
17812 : 44695193 : if (class_type && class_type != current_class_type)
17813 : : return true;
17814 : :
17815 : : /* Warn about conversion operators that will never be used. */
17816 : 42741528 : if (IDENTIFIER_CONV_OP_P (name)
17817 : 1604693 : && ! DECL_TEMPLATE_INFO (decl)
17818 : 43379512 : && warn_class_conversion)
17819 : : {
17820 : 637951 : tree t = TREE_TYPE (name);
17821 : 637951 : int ref = TYPE_REF_P (t);
17822 : :
17823 : 637951 : if (ref)
17824 : 21429 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
17825 : :
17826 : 637951 : if (VOID_TYPE_P (t))
17827 : 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
17828 : : "will never use a type conversion operator", class_type);
17829 : 637942 : else if (class_type)
17830 : : {
17831 : 637942 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
17832 : 24 : warning_at (loc, OPT_Wclass_conversion,
17833 : : ref
17834 : : ? G_("converting %qT to a reference to the same type "
17835 : : "will never use a type conversion operator")
17836 : : : G_("converting %qT to the same type "
17837 : : "will never use a type conversion operator"),
17838 : : class_type);
17839 : : /* Don't force t to be complete here. */
17840 : 637927 : else if (MAYBE_CLASS_TYPE_P (t)
17841 : 280599 : && COMPLETE_TYPE_P (t)
17842 : 61284 : && DERIVED_FROM_P (t, class_type))
17843 : 30 : warning_at (loc, OPT_Wclass_conversion,
17844 : : ref
17845 : : ? G_("converting %qT to a reference to a base class "
17846 : : "%qT will never use a type conversion operator")
17847 : : : G_("converting %qT to a base class %qT "
17848 : : "will never use a type conversion operator"),
17849 : : class_type, t);
17850 : : }
17851 : : }
17852 : :
17853 : 42741528 : if (!warn_ecpp)
17854 : : return true;
17855 : :
17856 : : /* Effective C++ rules below. */
17857 : :
17858 : : /* More Effective C++ rule 7. */
17859 : 54 : if (operator_code == TRUTH_ANDIF_EXPR
17860 : 54 : || operator_code == TRUTH_ORIF_EXPR
17861 : 54 : || operator_code == COMPOUND_EXPR)
17862 : 0 : warning_at (loc, OPT_Weffc__,
17863 : : "user-defined %qD always evaluates both arguments", decl);
17864 : :
17865 : : /* More Effective C++ rule 6. */
17866 : 54 : if (operator_code == POSTINCREMENT_EXPR
17867 : : || operator_code == POSTDECREMENT_EXPR
17868 : : || operator_code == PREINCREMENT_EXPR
17869 : 54 : || operator_code == PREDECREMENT_EXPR)
17870 : : {
17871 : 0 : tree arg = TREE_VALUE (argtypes);
17872 : 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
17873 : 0 : if (methodp || TYPE_REF_P (arg))
17874 : 0 : arg = TREE_TYPE (arg);
17875 : 0 : arg = TYPE_MAIN_VARIANT (arg);
17876 : :
17877 : 0 : if (operator_code == PREINCREMENT_EXPR
17878 : : || operator_code == PREDECREMENT_EXPR)
17879 : : {
17880 : 0 : if (!TYPE_REF_P (ret)
17881 : 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
17882 : 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
17883 : : build_reference_type (arg));
17884 : : }
17885 : : else
17886 : : {
17887 : 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
17888 : 0 : warning_at (loc, OPT_Weffc__,
17889 : : "postfix %qD should return %qT", decl, arg);
17890 : : }
17891 : : }
17892 : :
17893 : : /* Effective C++ rule 23. */
17894 : 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
17895 : : && (operator_code == PLUS_EXPR
17896 : 9 : || operator_code == MINUS_EXPR
17897 : 9 : || operator_code == TRUNC_DIV_EXPR
17898 : : || operator_code == MULT_EXPR
17899 : : || operator_code == TRUNC_MOD_EXPR)
17900 : 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
17901 : 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
17902 : :
17903 : : return true;
17904 : : }
17905 : :
17906 : : /* Return a string giving the keyword associate with CODE. */
17907 : :
17908 : : static const char *
17909 : 111 : tag_name (enum tag_types code)
17910 : : {
17911 : 111 : switch (code)
17912 : : {
17913 : : case record_type:
17914 : : return "struct";
17915 : 60 : case class_type:
17916 : 60 : return "class";
17917 : 0 : case union_type:
17918 : 0 : return "union";
17919 : 3 : case enum_type:
17920 : 3 : return "enum";
17921 : 0 : case typename_type:
17922 : 0 : return "typename";
17923 : 0 : default:
17924 : 0 : gcc_unreachable ();
17925 : : }
17926 : : }
17927 : :
17928 : : /* Name lookup in an elaborated-type-specifier (after the keyword
17929 : : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
17930 : : elaborated-type-specifier is invalid, issue a diagnostic and return
17931 : : error_mark_node; otherwise, return the *_TYPE to which it referred.
17932 : : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
17933 : :
17934 : : tree
17935 : 11431681 : check_elaborated_type_specifier (enum tag_types tag_code,
17936 : : tree decl,
17937 : : bool allow_template_p)
17938 : : {
17939 : 11431681 : tree type;
17940 : :
17941 : : /* In the case of:
17942 : :
17943 : : struct S { struct S *p; };
17944 : :
17945 : : name lookup will find the TYPE_DECL for the implicit "S::S"
17946 : : typedef. Adjust for that here. */
17947 : 11431681 : if (DECL_SELF_REFERENCE_P (decl))
17948 : 340536 : decl = TYPE_NAME (TREE_TYPE (decl));
17949 : :
17950 : 11431681 : type = TREE_TYPE (decl);
17951 : :
17952 : : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
17953 : : is false for this case as well. */
17954 : 11431681 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17955 : : {
17956 : 9 : error ("using template type parameter %qT after %qs",
17957 : : type, tag_name (tag_code));
17958 : 9 : return error_mark_node;
17959 : : }
17960 : : /* Accept template template parameters. */
17961 : 11431672 : else if (allow_template_p
17962 : 6541007 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
17963 : 6540995 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
17964 : : ;
17965 : : /* [dcl.type.elab]
17966 : :
17967 : : If the identifier resolves to a typedef-name or the
17968 : : simple-template-id resolves to an alias template
17969 : : specialization, the elaborated-type-specifier is ill-formed.
17970 : :
17971 : : In other words, the only legitimate declaration to use in the
17972 : : elaborated type specifier is the implicit typedef created when
17973 : : the type is declared. */
17974 : 11431657 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
17975 : 340652 : && !DECL_SELF_REFERENCE_P (decl)
17976 : 11431773 : && tag_code != typename_type)
17977 : : {
17978 : 78 : auto_diagnostic_group d;
17979 : 78 : if (alias_template_specialization_p (type, nt_opaque))
17980 : 21 : error ("using alias template specialization %qT after %qs",
17981 : : type, tag_name (tag_code));
17982 : : else
17983 : 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
17984 : 78 : inform (DECL_SOURCE_LOCATION (decl),
17985 : : "%qD has a previous declaration here", decl);
17986 : 78 : return error_mark_node;
17987 : 78 : }
17988 : 11431579 : else if (TREE_CODE (type) != RECORD_TYPE
17989 : 11431579 : && TREE_CODE (type) != UNION_TYPE
17990 : : && tag_code != enum_type
17991 : 64099 : && tag_code != typename_type)
17992 : : {
17993 : 6 : auto_diagnostic_group d;
17994 : 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
17995 : 6 : inform (location_of (type), "%qT has a previous declaration here", type);
17996 : 6 : return error_mark_node;
17997 : 6 : }
17998 : 11431573 : else if (TREE_CODE (type) != ENUMERAL_TYPE
17999 : 11367510 : && tag_code == enum_type)
18000 : : {
18001 : 3 : auto_diagnostic_group d;
18002 : 3 : error ("%qT referred to as enum", type);
18003 : 3 : inform (location_of (type), "%qT has a previous declaration here", type);
18004 : 3 : return error_mark_node;
18005 : 3 : }
18006 : 11431570 : else if (!allow_template_p
18007 : 4890605 : && TREE_CODE (type) == RECORD_TYPE
18008 : 16204266 : && CLASSTYPE_IS_TEMPLATE (type))
18009 : : {
18010 : : /* If a class template appears as elaborated type specifier
18011 : : without a template header such as:
18012 : :
18013 : : template <class T> class C {};
18014 : : void f(class C); // No template header here
18015 : :
18016 : : then the required template argument is missing. */
18017 : 18 : error ("template argument required for %<%s %T%>",
18018 : : tag_name (tag_code),
18019 : 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18020 : 18 : return error_mark_node;
18021 : : }
18022 : :
18023 : : return type;
18024 : : }
18025 : :
18026 : : /* Lookup NAME of an elaborated type specifier according to SCOPE and
18027 : : issue diagnostics if necessary. Return *_TYPE node upon success,
18028 : : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18029 : : error. */
18030 : :
18031 : : static tree
18032 : 22420965 : lookup_and_check_tag (enum tag_types tag_code, tree name,
18033 : : TAG_how how, bool template_header_p)
18034 : : {
18035 : 22420965 : tree decl;
18036 : 22420965 : if (how == TAG_how::GLOBAL)
18037 : : {
18038 : : /* First try ordinary name lookup, ignoring hidden class name
18039 : : injected via friend declaration. */
18040 : 1952506 : decl = lookup_name (name, LOOK_want::TYPE);
18041 : : /* If that fails, the name will be placed in the smallest
18042 : : non-class, non-function-prototype scope according to 3.3.1/5.
18043 : : We may already have a hidden name declared as friend in this
18044 : : scope. So lookup again but not ignoring hidden names.
18045 : : If we find one, that name will be made visible rather than
18046 : : creating a new tag. */
18047 : 1952506 : if (!decl)
18048 : 18378 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18049 : : }
18050 : : else
18051 : 20468459 : decl = lookup_elaborated_type (name, how);
18052 : :
18053 : 20486837 : if (!decl)
18054 : : /* We found nothing. */
18055 : : return NULL_TREE;
18056 : :
18057 : 5054391 : if (TREE_CODE (decl) == TREE_LIST)
18058 : : {
18059 : 3 : auto_diagnostic_group d;
18060 : 3 : error ("reference to %qD is ambiguous", name);
18061 : 3 : print_candidates (decl);
18062 : 3 : return error_mark_node;
18063 : 3 : }
18064 : :
18065 : 2102969 : if (DECL_CLASS_TEMPLATE_P (decl)
18066 : 2102963 : && !template_header_p
18067 : 7157351 : && how == TAG_how::CURRENT_ONLY)
18068 : : {
18069 : 12 : auto_diagnostic_group d;
18070 : 12 : error ("class template %qD redeclared as non-template", name);
18071 : 12 : inform (location_of (decl), "previous declaration here");
18072 : 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18073 : 12 : return error_mark_node;
18074 : 12 : }
18075 : :
18076 : 2102957 : if (DECL_CLASS_TEMPLATE_P (decl)
18077 : : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18078 : : so ignore a template template parameter. */
18079 : 5054382 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18080 : 2102954 : decl = DECL_TEMPLATE_RESULT (decl);
18081 : :
18082 : 5054376 : if (TREE_CODE (decl) != TYPE_DECL)
18083 : : /* Found not-a-type. */
18084 : : return NULL_TREE;
18085 : :
18086 : : /* Look for invalid nested type:
18087 : : class C {
18088 : : class C {};
18089 : : }; */
18090 : 5054355 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18091 : : {
18092 : 6 : error ("%qD has the same name as the class in which it is "
18093 : : "declared", decl);
18094 : 6 : return error_mark_node;
18095 : : }
18096 : :
18097 : : /* Two cases we need to consider when deciding if a class
18098 : : template is allowed as an elaborated type specifier:
18099 : : 1. It is a self reference to its own class.
18100 : : 2. It comes with a template header.
18101 : :
18102 : : For example:
18103 : :
18104 : : template <class T> class C {
18105 : : class C *c1; // DECL_SELF_REFERENCE_P is true
18106 : : class D;
18107 : : };
18108 : : template <class U> class C; // template_header_p is true
18109 : : template <class T> class C<T>::D {
18110 : : class C *c2; // DECL_SELF_REFERENCE_P is true
18111 : : }; */
18112 : :
18113 : 15163047 : tree t = check_elaborated_type_specifier (tag_code, decl,
18114 : : template_header_p
18115 : 9768177 : | DECL_SELF_REFERENCE_P (decl));
18116 : 2226063 : if (template_header_p && t && CLASS_TYPE_P (t)
18117 : 7280394 : && (!CLASSTYPE_TEMPLATE_INFO (t)
18118 : 2226027 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18119 : : {
18120 : 21 : auto_diagnostic_group d;
18121 : 21 : error ("%qT is not a template", t);
18122 : 21 : inform (location_of (t), "previous declaration here");
18123 : 42 : if (TYPE_CLASS_SCOPE_P (t)
18124 : 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18125 : 3 : inform (input_location,
18126 : : "perhaps you want to explicitly add %<%T::%>",
18127 : 3 : TYPE_CONTEXT (t));
18128 : 21 : return error_mark_node;
18129 : 21 : }
18130 : :
18131 : : return t;
18132 : : }
18133 : :
18134 : : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18135 : : Define the tag as a forward-reference if it is not defined.
18136 : :
18137 : : If a declaration is given, process it here, and report an error if
18138 : : multiple declarations are not identical.
18139 : :
18140 : : SCOPE is TS_CURRENT when this is also a definition. Only look in
18141 : : the current frame for the name (since C++ allows new names in any
18142 : : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18143 : : declaration. Only look beginning from the current scope outward up
18144 : : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18145 : :
18146 : : TEMPLATE_HEADER_P is true when this declaration is preceded by
18147 : : a set of template parameters. */
18148 : :
18149 : : tree
18150 : 21961491 : xref_tag (enum tag_types tag_code, tree name,
18151 : : TAG_how how, bool template_header_p)
18152 : : {
18153 : 21961491 : enum tree_code code;
18154 : 21961491 : tree context = NULL_TREE;
18155 : :
18156 : 21961491 : auto_cond_timevar tv (TV_NAME_LOOKUP);
18157 : :
18158 : 21961491 : gcc_assert (identifier_p (name));
18159 : :
18160 : 21961491 : switch (tag_code)
18161 : : {
18162 : : case record_type:
18163 : : case class_type:
18164 : : code = RECORD_TYPE;
18165 : : break;
18166 : : case union_type:
18167 : : code = UNION_TYPE;
18168 : : break;
18169 : : case enum_type:
18170 : : code = ENUMERAL_TYPE;
18171 : : break;
18172 : 0 : default:
18173 : 0 : gcc_unreachable ();
18174 : : }
18175 : :
18176 : : /* In case of anonymous name, xref_tag is only called to
18177 : : make type node and push name. Name lookup is not required. */
18178 : 21961491 : tree t = NULL_TREE;
18179 : 21961491 : if (!IDENTIFIER_ANON_P (name))
18180 : 20309974 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18181 : :
18182 : 21961491 : if (t == error_mark_node)
18183 : : return error_mark_node;
18184 : :
18185 : 2543485 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18186 : 1110744 : && template_class_depth (current_class_type)
18187 : 22361104 : && template_header_p)
18188 : : {
18189 : 280859 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18190 : : return t;
18191 : :
18192 : : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18193 : : a definition of this tag. Since, in addition, we are
18194 : : currently processing a (member) template declaration of a
18195 : : template class, we must be very careful; consider:
18196 : :
18197 : : template <class X> struct S1
18198 : :
18199 : : template <class U> struct S2
18200 : : {
18201 : : template <class V> friend struct S1;
18202 : : };
18203 : :
18204 : : Here, the S2::S1 declaration should not be confused with the
18205 : : outer declaration. In particular, the inner version should
18206 : : have a template parameter of level 2, not level 1.
18207 : :
18208 : : On the other hand, when presented with:
18209 : :
18210 : : template <class T> struct S1
18211 : : {
18212 : : template <class U> struct S2 {};
18213 : : template <class U> friend struct S2;
18214 : : };
18215 : :
18216 : : the friend must find S1::S2 eventually. We accomplish this
18217 : : by making sure that the new type we create to represent this
18218 : : declaration has the right TYPE_CONTEXT. */
18219 : 280856 : context = TYPE_CONTEXT (t);
18220 : 280856 : t = NULL_TREE;
18221 : : }
18222 : :
18223 : 21961377 : if (! t)
18224 : : {
18225 : : /* If no such tag is yet defined, create a forward-reference node
18226 : : and record it as the "definition".
18227 : : When a real declaration of this type is found,
18228 : : the forward-reference will be altered into a real type. */
18229 : 17223035 : if (code == ENUMERAL_TYPE)
18230 : : {
18231 : 14 : error ("use of enum %q#D without previous declaration", name);
18232 : 14 : return error_mark_node;
18233 : : }
18234 : :
18235 : 17223021 : t = make_class_type (code);
18236 : 17223021 : TYPE_CONTEXT (t) = context;
18237 : 17223021 : if (IDENTIFIER_LAMBDA_P (name))
18238 : : /* Mark it as a lambda type right now. Our caller will
18239 : : correct the value. */
18240 : 979576 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18241 : 17223021 : t = pushtag (name, t, how);
18242 : : }
18243 : : else
18244 : : {
18245 : 4738342 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18246 : : {
18247 : : /* Check that we aren't trying to overload a class with different
18248 : : constraints. */
18249 : 1945168 : if (!redeclare_class_template (t, current_template_parms,
18250 : : current_template_constraints ()))
18251 : 60 : return error_mark_node;
18252 : : }
18253 : 2793174 : else if (!processing_template_decl
18254 : 2603480 : && CLASS_TYPE_P (t)
18255 : 5370426 : && CLASSTYPE_IS_TEMPLATE (t))
18256 : : {
18257 : 0 : auto_diagnostic_group d;
18258 : 0 : error ("redeclaration of %qT as a non-template", t);
18259 : 0 : inform (location_of (t), "previous declaration %qD", t);
18260 : 0 : return error_mark_node;
18261 : 0 : }
18262 : :
18263 : 4738282 : if (modules_p ()
18264 : 4738282 : && how == TAG_how::CURRENT_ONLY)
18265 : : {
18266 : 8868 : tree decl = TYPE_NAME (t);
18267 : 8868 : if (!module_may_redeclare (decl))
18268 : 15 : return error_mark_node;
18269 : :
18270 : 8853 : tree not_tmpl = STRIP_TEMPLATE (decl);
18271 : 8853 : if (DECL_LANG_SPECIFIC (not_tmpl)
18272 : 3939 : && DECL_MODULE_ATTACH_P (not_tmpl)
18273 : 45 : && !DECL_MODULE_EXPORT_P (not_tmpl)
18274 : 8886 : && module_exporting_p ())
18275 : : {
18276 : 6 : auto_diagnostic_group d;
18277 : 6 : error ("conflicting exporting for declaration %qD", decl);
18278 : 6 : inform (DECL_SOURCE_LOCATION (decl),
18279 : : "previously declared here without exporting");
18280 : 6 : }
18281 : :
18282 : 8853 : tree maybe_tmpl = decl;
18283 : 8853 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18284 : 6658 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18285 : :
18286 : : /* FIXME: we should do a more precise check for redefinitions
18287 : : of a conflicting using-declaration here, as these diagnostics
18288 : : are not ideal. */
18289 : 8853 : if (DECL_LANG_SPECIFIC (decl)
18290 : 3939 : && DECL_MODULE_IMPORT_P (decl)
18291 : 8900 : && CP_DECL_CONTEXT (decl) == current_namespace)
18292 : : {
18293 : : /* Push it into this TU's symbol slot. */
18294 : 44 : if (maybe_tmpl != decl)
18295 : : /* We're in the template parm binding level.
18296 : : Pushtag has logic to slide under that, but we're
18297 : : not pushing a *new* type. */
18298 : 18 : push_nested_namespace (CP_DECL_CONTEXT (decl));
18299 : :
18300 : 44 : pushdecl (maybe_tmpl);
18301 : 44 : if (maybe_tmpl != decl)
18302 : 18 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
18303 : : }
18304 : :
18305 : 8853 : set_instantiating_module (maybe_tmpl);
18306 : : }
18307 : : }
18308 : :
18309 : : return t;
18310 : 21961491 : }
18311 : :
18312 : : /* Create the binfo hierarchy for REF with (possibly NULL) base list
18313 : : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18314 : : access_* node, and the TREE_VALUE is the type of the base-class.
18315 : : Non-NULL TREE_TYPE indicates virtual inheritance. */
18316 : :
18317 : : void
18318 : 57123150 : xref_basetypes (tree ref, tree base_list)
18319 : : {
18320 : 57123150 : tree *basep;
18321 : 57123150 : tree binfo, base_binfo;
18322 : 57123150 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18323 : 57123150 : unsigned max_bases = 0; /* Maximum direct bases. */
18324 : 57123150 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18325 : 57123150 : int i;
18326 : 57123150 : tree default_access;
18327 : 57123150 : tree igo_prev; /* Track Inheritance Graph Order. */
18328 : :
18329 : 57123150 : if (ref == error_mark_node)
18330 : 12 : return;
18331 : :
18332 : : /* The base of a derived class is private by default, all others are
18333 : : public. */
18334 : 57123150 : default_access = (TREE_CODE (ref) == RECORD_TYPE
18335 : 56590427 : && CLASSTYPE_DECLARED_CLASS (ref)
18336 : 57123150 : ? access_private_node : access_public_node);
18337 : :
18338 : : /* First, make sure that any templates in base-classes are
18339 : : instantiated. This ensures that if we call ourselves recursively
18340 : : we do not get confused about which classes are marked and which
18341 : : are not. */
18342 : 57123150 : basep = &base_list;
18343 : 86827632 : while (*basep)
18344 : : {
18345 : 29704482 : tree basetype = TREE_VALUE (*basep);
18346 : :
18347 : : /* The dependent_type_p call below should really be dependent_scope_p
18348 : : so that we give a hard error about using an incomplete type as a
18349 : : base, but we allow it with a pedwarn for backward
18350 : : compatibility. */
18351 : 29704482 : if (processing_template_decl
18352 : 29704482 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18353 : 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
18354 : : diagnostics::kind::pedwarn);
18355 : 29704482 : if (!dependent_type_p (basetype)
18356 : 29704482 : && !complete_type_or_else (basetype, NULL))
18357 : : /* An incomplete type. Remove it from the list. */
18358 : 15 : *basep = TREE_CHAIN (*basep);
18359 : : else
18360 : : {
18361 : 29704467 : max_bases++;
18362 : 29704467 : if (TREE_TYPE (*basep))
18363 : 70704 : max_dvbases++;
18364 : 29704467 : if (CLASS_TYPE_P (basetype))
18365 : 28868350 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18366 : 29704467 : basep = &TREE_CHAIN (*basep);
18367 : : }
18368 : : }
18369 : 57123150 : max_vbases += max_dvbases;
18370 : :
18371 : 57123150 : TYPE_MARKED_P (ref) = 1;
18372 : :
18373 : : /* The binfo slot should be empty, unless this is an (ill-formed)
18374 : : redefinition. */
18375 : 57123150 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18376 : :
18377 : 57123150 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18378 : :
18379 : 57123150 : binfo = make_tree_binfo (max_bases);
18380 : :
18381 : 57123150 : TYPE_BINFO (ref) = binfo;
18382 : 57123150 : BINFO_OFFSET (binfo) = size_zero_node;
18383 : 57123150 : BINFO_TYPE (binfo) = ref;
18384 : :
18385 : : /* Apply base-class info set up to the variants of this type. */
18386 : 57123150 : fixup_type_variants (ref);
18387 : :
18388 : 57123150 : if (max_bases)
18389 : : {
18390 : 28813479 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
18391 : : /* A C++98 POD cannot have base classes. */
18392 : 28813479 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18393 : :
18394 : 28813479 : if (TREE_CODE (ref) == UNION_TYPE)
18395 : : {
18396 : 12 : error ("derived union %qT invalid", ref);
18397 : 12 : return;
18398 : : }
18399 : : }
18400 : :
18401 : 28813467 : if (max_bases > 1)
18402 : 693246 : warning (OPT_Wmultiple_inheritance,
18403 : : "%qT defined with multiple direct bases", ref);
18404 : :
18405 : 57123138 : if (max_vbases)
18406 : : {
18407 : : /* An aggregate can't have virtual base classes. */
18408 : 213452 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18409 : :
18410 : 213452 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
18411 : :
18412 : 213452 : if (max_dvbases)
18413 : 69509 : warning (OPT_Wvirtual_inheritance,
18414 : : "%qT defined with direct virtual base", ref);
18415 : : }
18416 : :
18417 : 86827593 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18418 : : {
18419 : 29704455 : tree access = TREE_PURPOSE (base_list);
18420 : 29704455 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18421 : 29704455 : tree basetype = TREE_VALUE (base_list);
18422 : :
18423 : 29704455 : if (access == access_default_node)
18424 : 4415192 : access = default_access;
18425 : :
18426 : : /* Before C++17, an aggregate cannot have base classes. In C++17, an
18427 : : aggregate can't have virtual, private, or protected base classes. */
18428 : 29704455 : if (cxx_dialect < cxx17
18429 : 29494742 : || access != access_public_node
18430 : 28565665 : || via_virtual)
18431 : 1206468 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18432 : :
18433 : 29704455 : if (PACK_EXPANSION_P (basetype))
18434 : 23758 : basetype = PACK_EXPANSION_PATTERN (basetype);
18435 : 29704455 : if (TREE_CODE (basetype) == TYPE_DECL)
18436 : 0 : basetype = TREE_TYPE (basetype);
18437 : 29704455 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18438 : : {
18439 : 24 : error ("base type %qT fails to be a struct or class type",
18440 : : basetype);
18441 : 24 : goto dropped_base;
18442 : : }
18443 : :
18444 : 29704431 : base_binfo = NULL_TREE;
18445 : 29704431 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18446 : : {
18447 : 23115588 : base_binfo = TYPE_BINFO (basetype);
18448 : : /* The original basetype could have been a typedef'd type. */
18449 : 23115588 : basetype = BINFO_TYPE (base_binfo);
18450 : :
18451 : : /* Inherit flags from the base. */
18452 : 69346764 : TYPE_HAS_NEW_OPERATOR (ref)
18453 : 23115588 : |= TYPE_HAS_NEW_OPERATOR (basetype);
18454 : 69346764 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18455 : 23115588 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18456 : 23115588 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18457 : 23115588 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18458 : 69346764 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
18459 : 23115588 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18460 : 69346764 : CLASSTYPE_REPEATED_BASE_P (ref)
18461 : 23115588 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
18462 : : }
18463 : :
18464 : : /* We must do this test after we've seen through a typedef
18465 : : type. */
18466 : 29704431 : if (TYPE_MARKED_P (basetype))
18467 : : {
18468 : 18 : if (basetype == ref)
18469 : 0 : error ("recursive type %qT undefined", basetype);
18470 : : else
18471 : 18 : error ("duplicate base type %qT invalid", basetype);
18472 : 18 : goto dropped_base;
18473 : : }
18474 : :
18475 : 29704413 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18476 : : /* Regenerate the pack expansion for the bases. */
18477 : 23758 : basetype = make_pack_expansion (basetype);
18478 : :
18479 : 29704413 : TYPE_MARKED_P (basetype) = 1;
18480 : :
18481 : 29704413 : base_binfo = copy_binfo (base_binfo, basetype, ref,
18482 : : &igo_prev, via_virtual);
18483 : 29704413 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
18484 : 29633724 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
18485 : :
18486 : 29704413 : BINFO_BASE_APPEND (binfo, base_binfo);
18487 : 29704413 : BINFO_BASE_ACCESS_APPEND (binfo, access);
18488 : 29704413 : continue;
18489 : :
18490 : 42 : dropped_base:
18491 : : /* Update max_vbases to reflect the reality that we are dropping
18492 : : this base: if it reaches zero we want to undo the vec_alloc
18493 : : above to avoid inconsistencies during error-recovery: eg, in
18494 : : build_special_member_call, CLASSTYPE_VBASECLASSES non null
18495 : : and vtt null (c++/27952). */
18496 : 42 : if (via_virtual)
18497 : 12 : max_vbases--;
18498 : 42 : if (CLASS_TYPE_P (basetype))
18499 : 30 : max_vbases
18500 : 33 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18501 : 29704413 : }
18502 : :
18503 : 57123138 : if (CLASSTYPE_VBASECLASSES (ref)
18504 : 57123138 : && max_vbases == 0)
18505 : 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
18506 : :
18507 : 57336578 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18508 : : /* If we didn't get max_vbases vbases, we must have shared at
18509 : : least one of them, and are therefore diamond shaped. */
18510 : 22938 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18511 : :
18512 : : /* Unmark all the types. */
18513 : 86827551 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18514 : 29704413 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18515 : 57123138 : TYPE_MARKED_P (ref) = 0;
18516 : :
18517 : : /* Now see if we have a repeated base type. */
18518 : 57123138 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
18519 : : {
18520 : 147582862 : for (base_binfo = binfo; base_binfo;
18521 : 90460156 : base_binfo = TREE_CHAIN (base_binfo))
18522 : : {
18523 : 90460867 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18524 : : {
18525 : 711 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18526 : 711 : break;
18527 : : }
18528 : 90460156 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18529 : : }
18530 : 147582862 : for (base_binfo = binfo; base_binfo;
18531 : 90460156 : base_binfo = TREE_CHAIN (base_binfo))
18532 : 90460867 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18533 : 90460156 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18534 : : else
18535 : : break;
18536 : : }
18537 : : }
18538 : :
18539 : :
18540 : : /* Copies the enum-related properties from type SRC to type DST.
18541 : : Used with the underlying type of an enum and the enum itself. */
18542 : : static void
18543 : 1783983 : copy_type_enum (tree dst, tree src)
18544 : : {
18545 : 1783983 : tree t;
18546 : 3567984 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
18547 : : {
18548 : 1784001 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
18549 : 1784001 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
18550 : 1784001 : TYPE_SIZE (t) = TYPE_SIZE (src);
18551 : 1784001 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
18552 : 1784001 : SET_TYPE_MODE (dst, TYPE_MODE (src));
18553 : 1784001 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
18554 : 1784001 : unsigned valign = TYPE_ALIGN (src);
18555 : 1784001 : if (TYPE_USER_ALIGN (t))
18556 : 12 : valign = MAX (valign, TYPE_ALIGN (t));
18557 : : else
18558 : 1783989 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
18559 : 1784001 : SET_TYPE_ALIGN (t, valign);
18560 : 1784001 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
18561 : : }
18562 : 1783983 : }
18563 : :
18564 : : /* Begin compiling the definition of an enumeration type.
18565 : : NAME is its name,
18566 : :
18567 : : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
18568 : :
18569 : : UNDERLYING_TYPE is the type that will be used as the storage for
18570 : : the enumeration type. This should be NULL_TREE if no storage type
18571 : : was specified.
18572 : :
18573 : : ATTRIBUTES are any attributes specified after the enum-key.
18574 : :
18575 : : SCOPED_ENUM_P is true if this is a scoped enumeration type.
18576 : :
18577 : : if IS_NEW is not NULL, gets TRUE iff a new type is created.
18578 : :
18579 : : Returns the type object, as yet incomplete.
18580 : : Also records info about it so that build_enumerator
18581 : : may be used to declare the individual values as they are read. */
18582 : :
18583 : : tree
18584 : 2111122 : start_enum (tree name, tree enumtype, tree underlying_type,
18585 : : tree attributes, bool scoped_enum_p, bool *is_new)
18586 : : {
18587 : 2111122 : tree prevtype = NULL_TREE;
18588 : 2111122 : gcc_assert (identifier_p (name));
18589 : :
18590 : 2111122 : if (is_new)
18591 : 1684855 : *is_new = false;
18592 : : /* [C++0x dcl.enum]p5:
18593 : :
18594 : : If not explicitly specified, the underlying type of a scoped
18595 : : enumeration type is int. */
18596 : 2111122 : if (!underlying_type && scoped_enum_p)
18597 : 121160 : underlying_type = integer_type_node;
18598 : :
18599 : 2111122 : if (underlying_type)
18600 : 439509 : underlying_type = cv_unqualified (underlying_type);
18601 : :
18602 : : /* If this is the real definition for a previous forward reference,
18603 : : fill in the contents in the same object that used to be the
18604 : : forward reference. */
18605 : 2111122 : if (!enumtype)
18606 : 2110991 : enumtype = lookup_and_check_tag (enum_type, name,
18607 : : /*tag_scope=*/TAG_how::CURRENT_ONLY,
18608 : : /*template_header_p=*/false);
18609 : :
18610 : : /* In case of a template_decl, the only check that should be deferred
18611 : : to instantiation time is the comparison of underlying types. */
18612 : 2111122 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
18613 : : {
18614 : : /* Attempt to set the declaring module. */
18615 : 35186 : if (modules_p ())
18616 : : {
18617 : 160 : tree decl = TYPE_NAME (enumtype);
18618 : 160 : if (!module_may_redeclare (decl))
18619 : 3 : enumtype = error_mark_node;
18620 : : else
18621 : 157 : set_instantiating_module (decl);
18622 : : }
18623 : :
18624 : 35186 : if (enumtype == error_mark_node)
18625 : : ;
18626 : 35373 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
18627 : : {
18628 : 6 : auto_diagnostic_group d;
18629 : 6 : error_at (input_location, "scoped/unscoped mismatch "
18630 : : "in enum %q#T", enumtype);
18631 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18632 : : "previous definition here");
18633 : 6 : enumtype = error_mark_node;
18634 : 6 : }
18635 : 35177 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
18636 : : {
18637 : 0 : auto_diagnostic_group d;
18638 : 0 : error_at (input_location, "underlying type mismatch "
18639 : : "in enum %q#T", enumtype);
18640 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18641 : : "previous definition here");
18642 : 0 : enumtype = error_mark_node;
18643 : 0 : }
18644 : 35161 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
18645 : 70338 : && !same_type_p (underlying_type,
18646 : : ENUM_UNDERLYING_TYPE (enumtype)))
18647 : : {
18648 : 26 : auto_diagnostic_group d;
18649 : 26 : error_at (input_location, "different underlying type "
18650 : : "in enum %q#T", enumtype);
18651 : 26 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18652 : : "previous definition here");
18653 : 26 : underlying_type = NULL_TREE;
18654 : 26 : }
18655 : : }
18656 : :
18657 : 2111122 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
18658 : 35177 : || processing_template_decl)
18659 : : {
18660 : : /* In case of error, make a dummy enum to allow parsing to
18661 : : continue. */
18662 : 2076033 : if (enumtype == error_mark_node)
18663 : : {
18664 : 9 : name = make_anon_name ();
18665 : 9 : enumtype = NULL_TREE;
18666 : : }
18667 : :
18668 : : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
18669 : : of an opaque enum, or an opaque enum of an already defined
18670 : : enumeration (C++11).
18671 : : In any other case, it'll be NULL_TREE. */
18672 : 2076033 : if (!enumtype)
18673 : : {
18674 : 2075942 : if (is_new)
18675 : 1649678 : *is_new = true;
18676 : : }
18677 : 2076033 : prevtype = enumtype;
18678 : :
18679 : : /* Do not push the decl more than once. */
18680 : 2076033 : if (!enumtype
18681 : 91 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
18682 : : {
18683 : 2075945 : enumtype = cxx_make_type (ENUMERAL_TYPE);
18684 : 2075945 : enumtype = pushtag (name, enumtype);
18685 : :
18686 : 2075945 : if (enumtype != error_mark_node)
18687 : : {
18688 : : /* The enum is considered opaque until the opening '{' of the
18689 : : enumerator list. */
18690 : 2075927 : SET_OPAQUE_ENUM_P (enumtype, true);
18691 : 2075927 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
18692 : :
18693 : : /* std::byte aliases anything. */
18694 : 2075927 : if (TYPE_CONTEXT (enumtype) == std_node
18695 : 2401304 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
18696 : 12977 : TYPE_ALIAS_SET (enumtype) = 0;
18697 : : }
18698 : : }
18699 : : else
18700 : 88 : enumtype = xref_tag (enum_type, name);
18701 : :
18702 : 2076033 : if (enumtype == error_mark_node)
18703 : : return error_mark_node;
18704 : : }
18705 : :
18706 : 2111104 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
18707 : :
18708 : 2111104 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18709 : :
18710 : 2111104 : if (underlying_type)
18711 : : {
18712 : 439480 : if (ENUM_UNDERLYING_TYPE (enumtype))
18713 : : /* We already checked that it matches, don't change it to a different
18714 : : typedef variant. */;
18715 : 404345 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
18716 : : {
18717 : 404281 : copy_type_enum (enumtype, underlying_type);
18718 : 404281 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18719 : : }
18720 : 64 : else if (dependent_type_p (underlying_type))
18721 : 51 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18722 : : else
18723 : : {
18724 : 13 : error ("underlying type %qT of %qT must be an integral type",
18725 : : underlying_type, enumtype);
18726 : 13 : copy_type_enum (enumtype, integer_type_node);
18727 : 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
18728 : : }
18729 : : }
18730 : :
18731 : : /* If into a template class, the returned enum is always the first
18732 : : declaration (opaque or not) seen. This way all the references to
18733 : : this type will be to the same declaration. The following ones are used
18734 : : only to check for definition errors. */
18735 : 2111104 : if (prevtype && processing_template_decl)
18736 : : return prevtype;
18737 : : else
18738 : 2111016 : return enumtype;
18739 : : }
18740 : :
18741 : : /* Returns true if TYPE is an enum that uses an enumerator name for
18742 : : linkage purposes. */
18743 : :
18744 : : bool
18745 : 351508641 : enum_with_enumerator_for_linkage_p (tree type)
18746 : : {
18747 : 351508641 : return (cxx_dialect >= cxx20
18748 : 118432160 : && UNSCOPED_ENUM_P (type)
18749 : 5742528 : && TYPE_ANON_P (type)
18750 : 352617495 : && TYPE_VALUES (type));
18751 : : }
18752 : :
18753 : : /* After processing and defining all the values of an enumeration type,
18754 : : install their decls in the enumeration type.
18755 : : ENUMTYPE is the type object. */
18756 : :
18757 : : void
18758 : 2010725 : finish_enum_value_list (tree enumtype)
18759 : : {
18760 : 2010725 : tree values;
18761 : 2010725 : tree underlying_type;
18762 : 2010725 : tree decl;
18763 : 2010725 : tree value;
18764 : 2010725 : tree minnode, maxnode;
18765 : 2010725 : tree t;
18766 : :
18767 : 2010725 : bool fixed_underlying_type_p
18768 : 2010725 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
18769 : :
18770 : : /* We built up the VALUES in reverse order. */
18771 : 2010725 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
18772 : :
18773 : : /* For an enum defined in a template, just set the type of the values;
18774 : : all further processing is postponed until the template is
18775 : : instantiated. We need to set the type so that tsubst of a CONST_DECL
18776 : : works. */
18777 : 2010725 : if (processing_template_decl)
18778 : : {
18779 : 367716 : for (values = TYPE_VALUES (enumtype);
18780 : 680383 : values;
18781 : 367716 : values = TREE_CHAIN (values))
18782 : 367716 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
18783 : : return;
18784 : : }
18785 : :
18786 : : /* Determine the minimum and maximum values of the enumerators. */
18787 : 1698058 : if (TYPE_VALUES (enumtype))
18788 : : {
18789 : : minnode = maxnode = NULL_TREE;
18790 : :
18791 : 10718087 : for (values = TYPE_VALUES (enumtype);
18792 : 12387619 : values;
18793 : 10718087 : values = TREE_CHAIN (values))
18794 : : {
18795 : 10718087 : decl = TREE_VALUE (values);
18796 : :
18797 : : /* [dcl.enum]: Following the closing brace of an enum-specifier,
18798 : : each enumerator has the type of its enumeration. Prior to the
18799 : : closing brace, the type of each enumerator is the type of its
18800 : : initializing value. */
18801 : 10718087 : TREE_TYPE (decl) = enumtype;
18802 : :
18803 : : /* Update the minimum and maximum values, if appropriate. */
18804 : 10718087 : value = DECL_INITIAL (decl);
18805 : 10718087 : if (TREE_CODE (value) != INTEGER_CST)
18806 : 4 : value = integer_zero_node;
18807 : : /* Figure out what the minimum and maximum values of the
18808 : : enumerators are. */
18809 : 10718087 : if (!minnode)
18810 : : minnode = maxnode = value;
18811 : 9048555 : else if (tree_int_cst_lt (maxnode, value))
18812 : : maxnode = value;
18813 : 1369857 : else if (tree_int_cst_lt (value, minnode))
18814 : 149250 : minnode = value;
18815 : : }
18816 : : }
18817 : : else
18818 : : /* [dcl.enum]
18819 : :
18820 : : If the enumerator-list is empty, the underlying type is as if
18821 : : the enumeration had a single enumerator with value 0. */
18822 : 28526 : minnode = maxnode = integer_zero_node;
18823 : :
18824 : 1698058 : if (!fixed_underlying_type_p)
18825 : : {
18826 : : /* Compute the number of bits require to represent all values of the
18827 : : enumeration. We must do this before the type of MINNODE and
18828 : : MAXNODE are transformed, since tree_int_cst_min_precision relies
18829 : : on the TREE_TYPE of the value it is passed. */
18830 : 1379689 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
18831 : 1379689 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
18832 : 1379689 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
18833 : 1379689 : int precision = MAX (lowprec, highprec);
18834 : 1379689 : unsigned int itk;
18835 : 1379689 : bool use_short_enum;
18836 : :
18837 : : /* Determine the underlying type of the enumeration.
18838 : :
18839 : : [dcl.enum]
18840 : :
18841 : : The underlying type of an enumeration is an integral type that
18842 : : can represent all the enumerator values defined in the
18843 : : enumeration. It is implementation-defined which integral type is
18844 : : used as the underlying type for an enumeration except that the
18845 : : underlying type shall not be larger than int unless the value of
18846 : : an enumerator cannot fit in an int or unsigned int.
18847 : :
18848 : : We use "int" or an "unsigned int" as the underlying type, even if
18849 : : a smaller integral type would work, unless the user has
18850 : : explicitly requested that we use the smallest possible type. The
18851 : : user can request that for all enumerations with a command line
18852 : : flag, or for just one enumeration with an attribute. */
18853 : :
18854 : 2759378 : use_short_enum = flag_short_enums
18855 : 1379689 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
18856 : :
18857 : : /* If the precision of the type was specified with an attribute and it
18858 : : was too small, give an error. Otherwise, use it. */
18859 : 1379689 : if (TYPE_PRECISION (enumtype))
18860 : : {
18861 : 11 : if (precision > TYPE_PRECISION (enumtype))
18862 : 0 : error ("specified mode too small for enumerated values");
18863 : : else
18864 : : {
18865 : 11 : use_short_enum = true;
18866 : 11 : precision = TYPE_PRECISION (enumtype);
18867 : : }
18868 : : }
18869 : :
18870 : 2680887 : for (itk = (use_short_enum ? itk_char : itk_int);
18871 : 2680887 : itk != itk_none;
18872 : : itk++)
18873 : : {
18874 : 2680884 : underlying_type = integer_types[itk];
18875 : 2680884 : if (underlying_type != NULL_TREE
18876 : 2680866 : && TYPE_PRECISION (underlying_type) >= precision
18877 : 5359801 : && TYPE_SIGN (underlying_type) == sgn)
18878 : : break;
18879 : : }
18880 : 1379689 : if (itk == itk_none)
18881 : : {
18882 : : /* DR 377
18883 : :
18884 : : IF no integral type can represent all the enumerator values, the
18885 : : enumeration is ill-formed. */
18886 : 3 : error ("no integral type can represent all of the enumerator values "
18887 : : "for %qT", enumtype);
18888 : 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
18889 : 3 : underlying_type = integer_types[itk_unsigned_long_long];
18890 : : }
18891 : :
18892 : : /* [dcl.enum]
18893 : :
18894 : : The value of sizeof() applied to an enumeration type, an object
18895 : : of an enumeration type, or an enumerator, is the value of sizeof()
18896 : : applied to the underlying type. */
18897 : 1379689 : copy_type_enum (enumtype, underlying_type);
18898 : :
18899 : : /* Compute the minimum and maximum values for the type.
18900 : :
18901 : : [dcl.enum]
18902 : :
18903 : : For an enumeration where emin is the smallest enumerator and emax
18904 : : is the largest, the values of the enumeration are the values of the
18905 : : underlying type in the range bmin to bmax, where bmin and bmax are,
18906 : : respectively, the smallest and largest values of the smallest bit-
18907 : : field that can store emin and emax. */
18908 : :
18909 : : /* The middle-end currently assumes that types with TYPE_PRECISION
18910 : : narrower than their underlying type are suitably zero or sign
18911 : : extended to fill their mode. Similarly, it assumes that the front
18912 : : end assures that a value of a particular type must be within
18913 : : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
18914 : :
18915 : : We used to set these fields based on bmin and bmax, but that led
18916 : : to invalid assumptions like optimizing away bounds checking. So
18917 : : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
18918 : : TYPE_MAX_VALUE to the values for the mode above and only restrict
18919 : : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
18920 : 1379689 : ENUM_UNDERLYING_TYPE (enumtype)
18921 : 1379689 : = build_distinct_type_copy (underlying_type);
18922 : 1379689 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
18923 : 1379689 : set_min_and_max_values_for_integral_type
18924 : 1379689 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
18925 : :
18926 : : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
18927 : 1379689 : if (flag_strict_enums)
18928 : 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
18929 : :
18930 : 1379689 : if (use_short_enum)
18931 : : {
18932 : 79 : TYPE_PACKED (enumtype) = use_short_enum;
18933 : 79 : fixup_attribute_variants (enumtype);
18934 : : }
18935 : : }
18936 : : else
18937 : 318369 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
18938 : :
18939 : : /* If the enum is exported, mark the consts too. */
18940 : 1698058 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
18941 : 1449863 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
18942 : 1698579 : && at_namespace_scope_p ());
18943 : :
18944 : : /* Convert each of the enumerators to the type of the underlying
18945 : : type of the enumeration. */
18946 : 12416145 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
18947 : : {
18948 : 10718087 : decl = TREE_VALUE (values);
18949 : 10718087 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
18950 : 10718087 : if (fixed_underlying_type_p)
18951 : : /* If the enumeration type has a fixed underlying type, we
18952 : : already checked all of the enumerator values. */
18953 : 3134232 : value = DECL_INITIAL (decl);
18954 : : else
18955 : 7583855 : value = perform_implicit_conversion (underlying_type,
18956 : 7583855 : DECL_INITIAL (decl),
18957 : : tf_warning_or_error);
18958 : : /* Do not clobber shared ints. But do share identical enumerators. */
18959 : 10718087 : value = fold_convert (enumtype, value);
18960 : :
18961 : 10718087 : DECL_INITIAL (decl) = value;
18962 : 10718087 : if (export_p)
18963 : 2890 : DECL_MODULE_EXPORT_P (decl) = true;
18964 : 10718087 : }
18965 : :
18966 : : /* Fix up all variant types of this enum type. */
18967 : 3396134 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
18968 : 1698076 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
18969 : :
18970 : 1698058 : if (at_class_scope_p ()
18971 : 943952 : && COMPLETE_TYPE_P (current_class_type)
18972 : 1698119 : && UNSCOPED_ENUM_P (enumtype))
18973 : : {
18974 : 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
18975 : : /* TYPE_FIELDS needs fixup. */
18976 : 42 : fixup_type_variants (current_class_type);
18977 : : }
18978 : :
18979 : : /* P2115: An unnamed enum uses the name of its first enumerator for
18980 : : linkage purposes; reset the type linkage if that is the case. */
18981 : 1698058 : if (enum_with_enumerator_for_linkage_p (enumtype))
18982 : 332168 : reset_type_linkage (enumtype);
18983 : :
18984 : : /* Finish debugging output for this type. */
18985 : 1698058 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
18986 : :
18987 : : /* Each enumerator now has the type of its enumeration. Clear the cache
18988 : : so that this change in types doesn't confuse us later on. */
18989 : 1698058 : clear_cv_and_fold_caches ();
18990 : : }
18991 : :
18992 : : /* Finishes the enum type. This is called only the first time an
18993 : : enumeration is seen, be it opaque or odinary.
18994 : : ENUMTYPE is the type object. */
18995 : :
18996 : : void
18997 : 2000146 : finish_enum (tree enumtype)
18998 : : {
18999 : 2000146 : if (processing_template_decl)
19000 : : {
19001 : 312695 : if (at_function_scope_p ())
19002 : 2549 : add_stmt (build_min (TAG_DEFN, enumtype));
19003 : 312695 : return;
19004 : : }
19005 : :
19006 : : /* If this is a forward declaration, there should not be any variants,
19007 : : though we can get a variant in the middle of an enum-specifier with
19008 : : wacky code like 'enum E { e = sizeof(const E*) };' */
19009 : 3374902 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19010 : : && (TYPE_VALUES (enumtype)
19011 : : || !TYPE_NEXT_VARIANT (enumtype)));
19012 : : }
19013 : :
19014 : : /* Build and install a CONST_DECL for an enumeration constant of the
19015 : : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19016 : : Apply ATTRIBUTES if available. LOC is the location of NAME.
19017 : : Assignment of sequential values by default is handled here. */
19018 : :
19019 : : tree
19020 : 11085780 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19021 : : location_t loc)
19022 : : {
19023 : 11085780 : tree decl;
19024 : 11085780 : tree context;
19025 : 11085780 : tree type;
19026 : :
19027 : : /* scalar_constant_value will pull out this expression, so make sure
19028 : : it's folded as appropriate.
19029 : :
19030 : : Creating a TARGET_EXPR in a template breaks when substituting, and
19031 : : here we would create it for instance when using a class prvalue with
19032 : : a user-defined conversion function. So don't use such a tree. We
19033 : : instantiate VALUE here to get errors about bad enumerators even in
19034 : : a template that does not get instantiated. */
19035 : 11085780 : if (processing_template_decl)
19036 : 367716 : value = maybe_fold_non_dependent_expr (value);
19037 : :
19038 : : /* If the VALUE was erroneous, pretend it wasn't there; that will
19039 : : result in the enum being assigned the next value in sequence. */
19040 : 11085780 : if (value == error_mark_node)
19041 : : value = NULL_TREE;
19042 : :
19043 : : /* Remove no-op casts from the value. */
19044 : 11085742 : if (value)
19045 : 8466280 : STRIP_TYPE_NOPS (value);
19046 : :
19047 : 11085780 : if (! processing_template_decl)
19048 : : {
19049 : : /* Validate and default VALUE. */
19050 : 10718064 : if (value != NULL_TREE)
19051 : : {
19052 : 4820461 : if (!ENUM_UNDERLYING_TYPE (enumtype))
19053 : : {
19054 : 2596293 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19055 : : value, true);
19056 : 2596293 : if (tmp_value)
19057 : 4820461 : value = tmp_value;
19058 : : }
19059 : 2224168 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19060 : : (TREE_TYPE (value)))
19061 : 39 : value = perform_implicit_conversion_flags
19062 : 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19063 : : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19064 : :
19065 : 4820461 : if (value == error_mark_node)
19066 : : value = NULL_TREE;
19067 : :
19068 : 4820440 : if (value != NULL_TREE)
19069 : : {
19070 : 4820440 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19071 : : (TREE_TYPE (value)))
19072 : : {
19073 : 17 : error_at (cp_expr_loc_or_input_loc (value),
19074 : : "enumerator value for %qD must have integral or "
19075 : : "unscoped enumeration type", name);
19076 : 17 : value = NULL_TREE;
19077 : : }
19078 : : else
19079 : : {
19080 : 4820423 : value = cxx_constant_value (value);
19081 : :
19082 : 4820423 : if (TREE_CODE (value) != INTEGER_CST)
19083 : : {
19084 : 50 : error ("enumerator value for %qD is not an integer "
19085 : : "constant", name);
19086 : 50 : value = NULL_TREE;
19087 : : }
19088 : : }
19089 : : }
19090 : : }
19091 : :
19092 : : /* Default based on previous value. */
19093 : 67 : if (value == NULL_TREE)
19094 : : {
19095 : 5897691 : if (TYPE_VALUES (enumtype))
19096 : : {
19097 : 5446208 : tree prev_value;
19098 : :
19099 : : /* C++03 7.2/4: If no initializer is specified for the first
19100 : : enumerator, the type is an unspecified integral
19101 : : type. Otherwise the type is the same as the type of the
19102 : : initializing value of the preceding enumerator unless the
19103 : : incremented value is not representable in that type, in
19104 : : which case the type is an unspecified integral type
19105 : : sufficient to contain the incremented value. */
19106 : 5446208 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19107 : 5446208 : if (TREE_CODE (prev_value) != INTEGER_CST)
19108 : 2 : value = error_mark_node;
19109 : : else
19110 : : {
19111 : 5446206 : wi::overflow_type overflowed;
19112 : 5446206 : tree type = TREE_TYPE (prev_value);
19113 : 5446206 : signop sgn = TYPE_SIGN (type);
19114 : 5446206 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
19115 : 5446206 : &overflowed);
19116 : 5446206 : if (!overflowed)
19117 : : {
19118 : 5446206 : bool pos = !wi::neg_p (wi, sgn);
19119 : 5446206 : if (!wi::fits_to_tree_p (wi, type))
19120 : : {
19121 : : unsigned int itk;
19122 : 53 : for (itk = itk_int; itk != itk_none; itk++)
19123 : : {
19124 : 53 : type = integer_types[itk];
19125 : 53 : if (type != NULL_TREE
19126 : 53 : && (pos || !TYPE_UNSIGNED (type))
19127 : 106 : && wi::fits_to_tree_p (wi, type))
19128 : : break;
19129 : : }
19130 : 17 : if (type && cxx_dialect < cxx11
19131 : 6 : && itk > itk_unsigned_long)
19132 : 3 : pedwarn (input_location, OPT_Wlong_long,
19133 : : pos ? G_("\
19134 : : incremented enumerator value is too large for %<unsigned long%>") : G_("\
19135 : : incremented enumerator value is too large for %<long%>"));
19136 : : }
19137 : 5446206 : if (type == NULL_TREE)
19138 : 0 : overflowed = wi::OVF_UNKNOWN;
19139 : : else
19140 : 5446206 : value = wide_int_to_tree (type, wi);
19141 : : }
19142 : :
19143 : 5446206 : if (overflowed)
19144 : : {
19145 : 0 : error ("overflow in enumeration values at %qD", name);
19146 : 0 : value = error_mark_node;
19147 : : }
19148 : 5446206 : }
19149 : : }
19150 : : else
19151 : 451483 : value = integer_zero_node;
19152 : : }
19153 : :
19154 : : /* Remove no-op casts from the value. */
19155 : 10718064 : STRIP_TYPE_NOPS (value);
19156 : :
19157 : : /* If the underlying type of the enum is fixed, check whether
19158 : : the enumerator values fits in the underlying type. If it
19159 : : does not fit, the program is ill-formed [C++0x dcl.enum]. */
19160 : 10718064 : if (ENUM_UNDERLYING_TYPE (enumtype)
19161 : : && value
19162 : 10718064 : && TREE_CODE (value) == INTEGER_CST)
19163 : : {
19164 : 3134207 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19165 : 9 : error ("enumerator value %qE is outside the range of underlying "
19166 : 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19167 : :
19168 : : /* Convert the value to the appropriate type. */
19169 : 3134207 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19170 : : }
19171 : : }
19172 : :
19173 : : /* C++ associates enums with global, function, or class declarations. */
19174 : 11085780 : context = current_scope ();
19175 : :
19176 : : /* Build the actual enumeration constant. Note that the enumeration
19177 : : constants have the underlying type of the enum (if it is fixed)
19178 : : or the type of their initializer (if the underlying type of the
19179 : : enum is not fixed):
19180 : :
19181 : : [ C++0x dcl.enum ]
19182 : :
19183 : : If the underlying type is fixed, the type of each enumerator
19184 : : prior to the closing brace is the underlying type; if the
19185 : : initializing value of an enumerator cannot be represented by
19186 : : the underlying type, the program is ill-formed. If the
19187 : : underlying type is not fixed, the type of each enumerator is
19188 : : the type of its initializing value.
19189 : :
19190 : : If the underlying type is not fixed, it will be computed by
19191 : : finish_enum and we will reset the type of this enumerator. Of
19192 : : course, if we're processing a template, there may be no value. */
19193 : 11085780 : type = value ? TREE_TYPE (value) : NULL_TREE;
19194 : :
19195 : 11085780 : decl = build_decl (loc, CONST_DECL, name, type);
19196 : :
19197 : 11085780 : DECL_CONTEXT (decl) = enumtype;
19198 : 11085780 : TREE_CONSTANT (decl) = 1;
19199 : 11085780 : TREE_READONLY (decl) = 1;
19200 : 11085780 : DECL_INITIAL (decl) = value;
19201 : :
19202 : 11085780 : if (attributes)
19203 : 124586 : cplus_decl_attributes (&decl, attributes, 0);
19204 : :
19205 : 11085780 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19206 : : {
19207 : : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19208 : : on the TYPE_FIELDS list for `S'. (That's so that you can say
19209 : : things like `S::i' later.) */
19210 : :
19211 : : /* The enumerator may be getting declared outside of its enclosing
19212 : : class, like so:
19213 : :
19214 : : class S { public: enum E : int; }; enum S::E : int { i = 7; };
19215 : :
19216 : : For which case we need to make sure that the access of `S::i'
19217 : : matches the access of `S::E'. */
19218 : 1974325 : auto cas = make_temp_override (current_access_specifier);
19219 : 1974325 : set_current_access_from_decl (TYPE_NAME (enumtype));
19220 : 1974325 : finish_member_declaration (decl);
19221 : 1974325 : }
19222 : : else
19223 : 9111455 : pushdecl (decl);
19224 : :
19225 : : /* Add this enumeration constant to the list for this type. */
19226 : 11085780 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19227 : :
19228 : 11085780 : return decl;
19229 : : }
19230 : :
19231 : : /* Look for an enumerator with the given NAME within the enumeration
19232 : : type ENUMTYPE. This routine is used primarily for qualified name
19233 : : lookup into an enumerator in C++0x, e.g.,
19234 : :
19235 : : enum class Color { Red, Green, Blue };
19236 : :
19237 : : Color color = Color::Red;
19238 : :
19239 : : Returns the value corresponding to the enumerator, or
19240 : : NULL_TREE if no such enumerator was found. */
19241 : : tree
19242 : 11291912 : lookup_enumerator (tree enumtype, tree name)
19243 : : {
19244 : 11291912 : tree e;
19245 : 11291912 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19246 : :
19247 : 11291912 : e = purpose_member (name, TYPE_VALUES (enumtype));
19248 : 22583727 : return e? TREE_VALUE (e) : NULL_TREE;
19249 : : }
19250 : :
19251 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19252 : :
19253 : : tree
19254 : 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
19255 : : vec<string_int_pair> *values)
19256 : : {
19257 : 0 : location_t saved_loc = input_location;
19258 : 0 : input_location = loc;
19259 : :
19260 : 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19261 : : NULL_TREE, false, NULL);
19262 : 0 : if (!OPAQUE_ENUM_P (enumtype))
19263 : : {
19264 : 0 : auto_diagnostic_group d;
19265 : 0 : error_at (loc, "multiple definition of %q#T", enumtype);
19266 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19267 : : "previous definition here");
19268 : 0 : return enumtype;
19269 : 0 : }
19270 : 0 : SET_OPAQUE_ENUM_P (enumtype, false);
19271 : 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19272 : :
19273 : 0 : for (const string_int_pair &value : values)
19274 : 0 : build_enumerator (get_identifier (value.first),
19275 : 0 : build_int_cst (integer_type_node, value.second),
19276 : : enumtype, NULL_TREE, loc);
19277 : :
19278 : 0 : finish_enum_value_list (enumtype);
19279 : 0 : finish_enum (enumtype);
19280 : :
19281 : 0 : input_location = saved_loc;
19282 : 0 : return enumtype;
19283 : : }
19284 : :
19285 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19286 : :
19287 : : tree
19288 : 0 : cxx_simulate_record_decl (location_t loc, const char *name,
19289 : : array_slice<const tree> fields)
19290 : : {
19291 : 0 : iloc_sentinel ils (loc);
19292 : :
19293 : 0 : tree ident = get_identifier (name);
19294 : 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
19295 : 0 : if (type != error_mark_node
19296 : 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19297 : : {
19298 : 0 : error ("redefinition of %q#T", type);
19299 : 0 : type = error_mark_node;
19300 : : }
19301 : 0 : if (type == error_mark_node)
19302 : 0 : return lhd_simulate_record_decl (loc, name, fields);
19303 : :
19304 : 0 : xref_basetypes (type, NULL_TREE);
19305 : 0 : type = begin_class_definition (type);
19306 : 0 : if (type == error_mark_node)
19307 : 0 : return lhd_simulate_record_decl (loc, name, fields);
19308 : :
19309 : 0 : for (tree field : fields)
19310 : 0 : finish_member_declaration (field);
19311 : :
19312 : 0 : type = finish_struct (type, NULL_TREE);
19313 : :
19314 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
19315 : 0 : set_underlying_type (decl);
19316 : 0 : lang_hooks.decls.pushdecl (decl);
19317 : :
19318 : 0 : return type;
19319 : 0 : }
19320 : :
19321 : : /* We're defining DECL. Make sure that its type is OK. */
19322 : :
19323 : : static void
19324 : 143882274 : check_function_type (tree decl, tree current_function_parms)
19325 : : {
19326 : 143882274 : tree fntype = TREE_TYPE (decl);
19327 : 143882274 : tree return_type = complete_type (TREE_TYPE (fntype));
19328 : :
19329 : : /* In a function definition, arg types must be complete. */
19330 : 143882274 : require_complete_types_for_parms (current_function_parms);
19331 : :
19332 : 143882274 : if (dependent_type_p (return_type)
19333 : 143882274 : || type_uses_auto (return_type))
19334 : 43570751 : return;
19335 : 100311523 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19336 : : {
19337 : 12 : tree args = TYPE_ARG_TYPES (fntype);
19338 : :
19339 : 12 : error ("return type %q#T is incomplete", return_type);
19340 : :
19341 : : /* Make it return void instead. */
19342 : 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
19343 : 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19344 : : void_type_node,
19345 : 3 : TREE_CHAIN (args));
19346 : : else
19347 : 18 : fntype = build_function_type (void_type_node, args,
19348 : 9 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19349 : 12 : fntype = (cp_build_type_attribute_variant
19350 : 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19351 : 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19352 : 12 : TREE_TYPE (decl) = fntype;
19353 : : }
19354 : : else
19355 : : {
19356 : 100311511 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
19357 : 100311511 : maybe_warn_parm_abi (TREE_TYPE (fntype),
19358 : 100311511 : DECL_SOURCE_LOCATION (decl));
19359 : : }
19360 : : }
19361 : :
19362 : : /* True iff FN is an implicitly-defined default constructor. */
19363 : :
19364 : : static bool
19365 : 14415689 : implicit_default_ctor_p (tree fn)
19366 : : {
19367 : 14415689 : return (DECL_CONSTRUCTOR_P (fn)
19368 : 14415689 : && !user_provided_p (fn)
19369 : 16418151 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19370 : : }
19371 : :
19372 : : /* Clobber the contents of *this to let the back end know that the object
19373 : : storage is dead when we enter the constructor or leave the destructor. */
19374 : :
19375 : : static tree
19376 : 1319628 : build_clobber_this (clobber_kind kind)
19377 : : {
19378 : : /* Clobbering an empty base is pointless, and harmful if its one byte
19379 : : TYPE_SIZE overlays real data. */
19380 : 1319628 : if (is_empty_class (current_class_type))
19381 : 0 : return void_node;
19382 : :
19383 : : /* If we have virtual bases, clobber the whole object, but only if we're in
19384 : : charge. If we don't have virtual bases, clobber the as-base type so we
19385 : : don't mess with tail padding. */
19386 : 1319628 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19387 : :
19388 : 1319628 : tree ctype = current_class_type;
19389 : 1319628 : if (!vbases)
19390 : 1311037 : ctype = CLASSTYPE_AS_BASE (ctype);
19391 : :
19392 : 1319628 : tree clobber = build_clobber (ctype, kind);
19393 : :
19394 : 1319628 : tree thisref = current_class_ref;
19395 : 1319628 : if (ctype != current_class_type)
19396 : : {
19397 : 131794 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19398 : 131794 : thisref = convert_from_reference (thisref);
19399 : : }
19400 : :
19401 : 1319628 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19402 : 1319628 : if (kind == CLOBBER_OBJECT_BEGIN)
19403 : 0 : TREE_SET_CODE (exprstmt, INIT_EXPR);
19404 : 1319628 : if (vbases)
19405 : 8591 : exprstmt = build_if_in_charge (exprstmt);
19406 : :
19407 : : return exprstmt;
19408 : : }
19409 : :
19410 : : /* Create the FUNCTION_DECL for a function definition.
19411 : : DECLSPECS and DECLARATOR are the parts of the declaration;
19412 : : they describe the function's name and the type it returns,
19413 : : but twisted together in a fashion that parallels the syntax of C.
19414 : :
19415 : : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19416 : : DECLARATOR is really the DECL for the function we are about to
19417 : : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19418 : : indicating that the function is an inline defined in-class.
19419 : :
19420 : : This function creates a binding context for the function body
19421 : : as well as setting up the FUNCTION_DECL in current_function_decl.
19422 : :
19423 : : For C++, we must first check whether that datum makes any sense.
19424 : : For example, "class A local_a(1,2);" means that variable local_a
19425 : : is an aggregate of type A, which should have a constructor
19426 : : applied to it with the argument list [1, 2].
19427 : :
19428 : : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19429 : : or may be a BLOCK if the function has been defined previously
19430 : : in this translation unit. On exit, DECL_INITIAL (decl1) will be
19431 : : error_mark_node if the function has never been defined, or
19432 : : a BLOCK if the function has been defined somewhere. */
19433 : :
19434 : : bool
19435 : 143882310 : start_preparsed_function (tree decl1, tree attrs, int flags)
19436 : : {
19437 : 143882310 : tree ctype = NULL_TREE;
19438 : 143882310 : bool doing_friend = false;
19439 : :
19440 : : /* Sanity check. */
19441 : 143882310 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19442 : 143882310 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19443 : :
19444 : 143882310 : tree fntype = TREE_TYPE (decl1);
19445 : 143882310 : if (DECL_CLASS_SCOPE_P (decl1))
19446 : 104005622 : ctype = DECL_CONTEXT (decl1);
19447 : : else
19448 : : {
19449 : 79753376 : ctype = DECL_FRIEND_CONTEXT (decl1);
19450 : :
19451 : 39876687 : if (ctype)
19452 : 143882310 : doing_friend = true;
19453 : : }
19454 : :
19455 : : /* Adjust for #pragma target/optimize if this is an artificial function that
19456 : : (probably) didn't go through grokfndecl. We particularly don't want this
19457 : : for deferred instantiations, which should match their template. */
19458 : 143882310 : if (DECL_ARTIFICIAL (decl1))
19459 : 3040423 : decl_attributes (&decl1, NULL_TREE, 0);
19460 : :
19461 : 143882310 : if (DECL_DECLARED_INLINE_P (decl1)
19462 : 143882310 : && lookup_attribute ("noinline", attrs))
19463 : : {
19464 : 0 : auto_urlify_attributes sentinel;
19465 : 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
19466 : : "inline function %qD given attribute %qs", decl1, "noinline");
19467 : 0 : }
19468 : :
19469 : : /* Handle gnu_inline attribute. */
19470 : 143882310 : if (GNU_INLINE_P (decl1))
19471 : : {
19472 : 1747305 : DECL_EXTERNAL (decl1) = 1;
19473 : 1747305 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
19474 : 1747305 : DECL_INTERFACE_KNOWN (decl1) = 1;
19475 : 1747305 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
19476 : : }
19477 : :
19478 : 143882310 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
19479 : : /* This is a constructor, we must ensure that any default args
19480 : : introduced by this definition are propagated to the clones
19481 : : now. The clones are used directly in overload resolution. */
19482 : 18768167 : adjust_clone_args (decl1);
19483 : :
19484 : : /* Sometimes we don't notice that a function is a static member, and
19485 : : build a METHOD_TYPE for it. Fix that up now. */
19486 : 143882310 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
19487 : : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
19488 : :
19489 : : /* Set up current_class_type, and enter the scope of the class, if
19490 : : appropriate. */
19491 : 143882310 : if (ctype)
19492 : 107020071 : push_nested_class (ctype);
19493 : :
19494 : : /* Now that we have entered the scope of the class, we must restore
19495 : : the bindings for any template parameters surrounding DECL1, if it
19496 : : is an inline member template. (Order is important; consider the
19497 : : case where a template parameter has the same name as a field of
19498 : : the class.) It is not until after this point that
19499 : : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19500 : 143882310 : if (flags & SF_INCLASS_INLINE)
19501 : 70546498 : maybe_begin_member_template_processing (decl1);
19502 : :
19503 : : /* Effective C++ rule 15. */
19504 : 143882310 : if (warn_ecpp
19505 : 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19506 : 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19507 : 143882340 : && VOID_TYPE_P (TREE_TYPE (fntype)))
19508 : 0 : warning (OPT_Weffc__,
19509 : : "%<operator=%> should return a reference to %<*this%>");
19510 : :
19511 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
19512 : : error_mark_node is replaced below (in poplevel) with the BLOCK. */
19513 : 143882310 : if (!DECL_INITIAL (decl1))
19514 : 30153250 : DECL_INITIAL (decl1) = error_mark_node;
19515 : :
19516 : : /* This function exists in static storage.
19517 : : (This does not mean `static' in the C sense!) */
19518 : 143882310 : TREE_STATIC (decl1) = 1;
19519 : :
19520 : : /* We must call push_template_decl after current_class_type is set
19521 : : up. (If we are processing inline definitions after exiting a
19522 : : class scope, current_class_type will be NULL_TREE until set above
19523 : : by push_nested_class.) */
19524 : 143882310 : if (processing_template_decl)
19525 : : {
19526 : 86590284 : tree newdecl1 = push_template_decl (decl1, doing_friend);
19527 : 86590284 : if (newdecl1 == error_mark_node)
19528 : : {
19529 : 36 : if (ctype)
19530 : 9 : pop_nested_class ();
19531 : 36 : return false;
19532 : : }
19533 : 86590248 : decl1 = newdecl1;
19534 : : }
19535 : :
19536 : : /* Make sure the parameter and return types are reasonable. When
19537 : : you declare a function, these types can be incomplete, but they
19538 : : must be complete when you define the function. */
19539 : 143882274 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
19540 : :
19541 : : /* Build the return declaration for the function. */
19542 : 143882274 : tree restype = TREE_TYPE (fntype);
19543 : :
19544 : 143882274 : if (DECL_RESULT (decl1) == NULL_TREE)
19545 : : {
19546 : : /* In a template instantiation, copy the return type location. When
19547 : : parsing, the location will be set in grokdeclarator. */
19548 : 57280861 : location_t loc = input_location;
19549 : 57280861 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
19550 : : {
19551 : 27056784 : tree tmpl = template_for_substitution (decl1);
19552 : 27056784 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
19553 : 25965949 : loc = DECL_SOURCE_LOCATION (res);
19554 : : }
19555 : :
19556 : 57280861 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
19557 : 57280861 : DECL_ARTIFICIAL (resdecl) = 1;
19558 : 57280861 : DECL_IGNORED_P (resdecl) = 1;
19559 : 57280861 : DECL_RESULT (decl1) = resdecl;
19560 : :
19561 : 57280861 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
19562 : : }
19563 : :
19564 : : /* Record the decl so that the function name is defined.
19565 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
19566 : : use the old decl. */
19567 : 143882274 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
19568 : : {
19569 : : /* A specialization is not used to guide overload resolution. */
19570 : 20385533 : if (!DECL_FUNCTION_MEMBER_P (decl1)
19571 : 20372222 : && !(DECL_USE_TEMPLATE (decl1) &&
19572 : 3660 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
19573 : : {
19574 : 9787339 : tree olddecl = pushdecl (decl1);
19575 : :
19576 : 9787339 : if (olddecl == error_mark_node)
19577 : : /* If something went wrong when registering the declaration,
19578 : : use DECL1; we have to have a FUNCTION_DECL to use when
19579 : : parsing the body of the function. */
19580 : : ;
19581 : : else
19582 : : {
19583 : : /* Otherwise, OLDDECL is either a previous declaration
19584 : : of the same function or DECL1 itself. */
19585 : :
19586 : 9787129 : if (warn_missing_declarations
19587 : 21 : && olddecl == decl1
19588 : 15 : && !DECL_MAIN_P (decl1)
19589 : 15 : && TREE_PUBLIC (decl1)
19590 : 9787141 : && !DECL_DECLARED_INLINE_P (decl1))
19591 : : {
19592 : 9 : tree context;
19593 : :
19594 : : /* Check whether DECL1 is in an anonymous
19595 : : namespace. */
19596 : 9 : for (context = DECL_CONTEXT (decl1);
19597 : 18 : context;
19598 : 9 : context = DECL_CONTEXT (context))
19599 : : {
19600 : 12 : if (TREE_CODE (context) == NAMESPACE_DECL
19601 : 12 : && DECL_NAME (context) == NULL_TREE)
19602 : : break;
19603 : : }
19604 : :
19605 : 9 : if (context == NULL)
19606 : 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
19607 : 6 : OPT_Wmissing_declarations,
19608 : : "no previous declaration for %qD", decl1);
19609 : : }
19610 : :
19611 : 9787129 : decl1 = olddecl;
19612 : : }
19613 : : }
19614 : : else
19615 : : {
19616 : : /* We need to set the DECL_CONTEXT. */
19617 : 793884 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
19618 : 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
19619 : : }
19620 : 10581223 : fntype = TREE_TYPE (decl1);
19621 : 10581223 : restype = TREE_TYPE (fntype);
19622 : :
19623 : : /* If #pragma weak applies, mark the decl appropriately now.
19624 : : The pragma only applies to global functions. Because
19625 : : determining whether or not the #pragma applies involves
19626 : : computing the mangled name for the declaration, we cannot
19627 : : apply the pragma until after we have merged this declaration
19628 : : with any previous declarations; if the original declaration
19629 : : has a linkage specification, that specification applies to
19630 : : the definition as well, and may affect the mangled name. */
19631 : 10581223 : if (DECL_FILE_SCOPE_P (decl1))
19632 : 2528832 : maybe_apply_pragma_weak (decl1);
19633 : : }
19634 : :
19635 : : /* We are now in the scope of the function being defined. */
19636 : 143882274 : current_function_decl = decl1;
19637 : :
19638 : : /* Save the parm names or decls from this function's declarator
19639 : : where store_parm_decls will find them. */
19640 : 143882274 : tree current_function_parms = DECL_ARGUMENTS (decl1);
19641 : :
19642 : : /* Let the user know we're compiling this function. */
19643 : 143882274 : announce_function (decl1);
19644 : :
19645 : 143882274 : gcc_assert (DECL_INITIAL (decl1));
19646 : :
19647 : : /* This function may already have been parsed, in which case just
19648 : : return; our caller will skip over the body without parsing. */
19649 : 143882274 : if (DECL_INITIAL (decl1) != error_mark_node)
19650 : : return true;
19651 : :
19652 : : /* Initialize RTL machinery. We cannot do this until
19653 : : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
19654 : : even when processing a template; this is how we get
19655 : : CFUN set up, and our per-function variables initialized.
19656 : : FIXME factor out the non-RTL stuff. */
19657 : 143882274 : cp_binding_level *bl = current_binding_level;
19658 : 143882274 : allocate_struct_function (decl1, processing_template_decl);
19659 : :
19660 : : /* Initialize the language data structures. Whenever we start
19661 : : a new function, we destroy temporaries in the usual way. */
19662 : 143882274 : cfun->language = ggc_cleared_alloc<language_function> ();
19663 : 143882274 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
19664 : 143882274 : current_binding_level = bl;
19665 : :
19666 : : /* If we are (erroneously) defining a function that we have already
19667 : : defined before, wipe out what we knew before. */
19668 : 143882274 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
19669 : 143882274 : FNDECL_USED_AUTO (decl1) = false;
19670 : 143882274 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
19671 : :
19672 : 143882274 : if (!processing_template_decl && type_uses_auto (restype))
19673 : : {
19674 : 845846 : FNDECL_USED_AUTO (decl1) = true;
19675 : 845846 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
19676 : : }
19677 : :
19678 : : /* Start the statement-tree, start the tree now. */
19679 : 143882274 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
19680 : :
19681 : 143882274 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
19682 : : {
19683 : : /* We know that this was set up by `grokclassfn'. We do not
19684 : : wait until `store_parm_decls', since evil parse errors may
19685 : : never get us to that point. Here we keep the consistency
19686 : : between `current_class_type' and `current_class_ptr'. */
19687 : 92686126 : tree t = DECL_ARGUMENTS (decl1);
19688 : :
19689 : 92686126 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
19690 : 92686126 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
19691 : :
19692 : 92686126 : cp_function_chain->x_current_class_ref
19693 : 92686126 : = cp_build_fold_indirect_ref (t);
19694 : : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
19695 : 92686126 : cp_function_chain->x_current_class_ptr = t;
19696 : :
19697 : : /* Constructors and destructors need to know whether they're "in
19698 : : charge" of initializing virtual base classes. */
19699 : 92686126 : t = DECL_CHAIN (t);
19700 : 92686126 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
19701 : : {
19702 : 19756 : current_in_charge_parm = t;
19703 : 19756 : t = DECL_CHAIN (t);
19704 : : }
19705 : 92686126 : if (DECL_HAS_VTT_PARM_P (decl1))
19706 : : {
19707 : 39525 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
19708 : 39525 : current_vtt_parm = t;
19709 : : }
19710 : : }
19711 : :
19712 : 143882274 : bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
19713 : : /* Implicitly-defined methods (like the
19714 : : destructor for a class in which no destructor
19715 : : is explicitly declared) must not be defined
19716 : : until their definition is needed. So, we
19717 : : ignore interface specifications for
19718 : : compiler-generated functions. */
19719 : 143882274 : && !DECL_ARTIFICIAL (decl1));
19720 : 143882274 : struct c_fileinfo *finfo
19721 : 143882274 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
19722 : :
19723 : 143882274 : if (processing_template_decl)
19724 : : /* Don't mess with interface flags. */;
19725 : 57292026 : else if (DECL_INTERFACE_KNOWN (decl1))
19726 : : {
19727 : 17783465 : tree ctx = decl_function_context (decl1);
19728 : :
19729 : 17783465 : if (DECL_NOT_REALLY_EXTERN (decl1))
19730 : 14108191 : DECL_EXTERNAL (decl1) = 0;
19731 : :
19732 : 17783465 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
19733 : : /* This is a function in a local class in an extern inline
19734 : : or template function. */
19735 : 2835095 : comdat_linkage (decl1);
19736 : : }
19737 : : /* If this function belongs to an interface, it is public.
19738 : : If it belongs to someone else's interface, it is also external.
19739 : : This only affects inlines and template instantiations. */
19740 : 39508561 : else if (!finfo->interface_unknown && honor_interface)
19741 : : {
19742 : 112 : if (DECL_DECLARED_INLINE_P (decl1)
19743 : 112 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19744 : : {
19745 : 88 : DECL_EXTERNAL (decl1)
19746 : 176 : = (finfo->interface_only
19747 : 88 : || (DECL_DECLARED_INLINE_P (decl1)
19748 : 42 : && ! flag_implement_inlines
19749 : 0 : && !DECL_VINDEX (decl1)));
19750 : :
19751 : : /* For WIN32 we also want to put these in linkonce sections. */
19752 : 88 : maybe_make_one_only (decl1);
19753 : : }
19754 : : else
19755 : 24 : DECL_EXTERNAL (decl1) = 0;
19756 : 112 : DECL_INTERFACE_KNOWN (decl1) = 1;
19757 : : /* If this function is in an interface implemented in this file,
19758 : : make sure that the back end knows to emit this function
19759 : : here. */
19760 : 112 : if (!DECL_EXTERNAL (decl1))
19761 : 66 : mark_needed (decl1);
19762 : : }
19763 : 39508449 : else if (finfo->interface_unknown && finfo->interface_only
19764 : 0 : && honor_interface)
19765 : : {
19766 : : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
19767 : : interface, we will have both finfo->interface_unknown and
19768 : : finfo->interface_only set. In that case, we don't want to
19769 : : use the normal heuristics because someone will supply a
19770 : : #pragma implementation elsewhere, and deducing it here would
19771 : : produce a conflict. */
19772 : 0 : comdat_linkage (decl1);
19773 : 0 : DECL_EXTERNAL (decl1) = 0;
19774 : 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
19775 : 0 : DECL_DEFER_OUTPUT (decl1) = 1;
19776 : : }
19777 : : else
19778 : : {
19779 : : /* This is a definition, not a reference.
19780 : : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
19781 : 39508449 : if (!GNU_INLINE_P (decl1))
19782 : 39508446 : DECL_EXTERNAL (decl1) = 0;
19783 : :
19784 : 39508449 : if ((DECL_DECLARED_INLINE_P (decl1)
19785 : 2093007 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19786 : 41367331 : && ! DECL_INTERFACE_KNOWN (decl1))
19787 : 39274324 : DECL_DEFER_OUTPUT (decl1) = 1;
19788 : : else
19789 : 234125 : DECL_INTERFACE_KNOWN (decl1) = 1;
19790 : : }
19791 : :
19792 : : /* Determine the ELF visibility attribute for the function. We must not
19793 : : do this before calling "pushdecl", as we must allow "duplicate_decls"
19794 : : to merge any attributes appropriately. We also need to wait until
19795 : : linkage is set. */
19796 : 143882274 : if (!DECL_CLONED_FUNCTION_P (decl1))
19797 : 129868583 : determine_visibility (decl1);
19798 : :
19799 : 143882274 : if (!processing_template_decl)
19800 : 57292026 : maybe_instantiate_noexcept (decl1);
19801 : :
19802 : 143882274 : begin_scope (sk_function_parms, decl1);
19803 : :
19804 : 143882274 : ++function_depth;
19805 : :
19806 : 143882274 : start_fname_decls ();
19807 : :
19808 : 143882274 : store_parm_decls (current_function_parms);
19809 : :
19810 : 143882274 : start_function_contracts (decl1);
19811 : :
19812 : 143882274 : if (!processing_template_decl
19813 : 57292026 : && flag_lifetime_dse > 1
19814 : 114564488 : && DECL_CONSTRUCTOR_P (decl1)
19815 : : /* Clobbering an empty base is harmful if it overlays real data. */
19816 : 16048506 : && !is_empty_class (current_class_type)
19817 : : /* We can't clobber safely for an implicitly-defined default constructor
19818 : : because part of the initialization might happen before we enter the
19819 : : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
19820 : 14415097 : && !implicit_default_ctor_p (decl1)
19821 : 156995870 : && !lookup_attribute ("clobber *this",
19822 : 13113596 : DECL_ATTRIBUTES (current_class_ptr)))
19823 : 13108690 : DECL_ATTRIBUTES (current_class_ptr)
19824 : 26217380 : = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
19825 : 13108690 : DECL_ATTRIBUTES (current_class_ptr));
19826 : :
19827 : 143882274 : if (!processing_template_decl
19828 : 114584052 : && DECL_CONSTRUCTOR_P (decl1)
19829 : 16051002 : && sanitize_flags_p (SANITIZE_VPTR)
19830 : 1776 : && !DECL_CLONED_FUNCTION_P (decl1)
19831 : 143882866 : && !implicit_default_ctor_p (decl1))
19832 : 436 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
19833 : :
19834 : 143882274 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
19835 : 143881706 : start_lambda_scope (decl1);
19836 : :
19837 : : return true;
19838 : : }
19839 : :
19840 : :
19841 : : /* Like start_preparsed_function, except that instead of a
19842 : : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
19843 : :
19844 : : Returns true on success. If the DECLARATOR is not suitable
19845 : : for a function, we return false, which tells the parser to
19846 : : skip the entire function. */
19847 : :
19848 : : bool
19849 : 39189674 : start_function (cp_decl_specifier_seq *declspecs,
19850 : : const cp_declarator *declarator,
19851 : : tree attrs)
19852 : : {
19853 : 39189674 : tree decl1;
19854 : :
19855 : 39189674 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
19856 : 39189674 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
19857 : 39189674 : if (decl1 == error_mark_node)
19858 : : return false;
19859 : :
19860 : 39189385 : if (DECL_MAIN_P (decl1))
19861 : : /* main must return int. grokfndecl should have corrected it
19862 : : (and issued a diagnostic) if the user got it wrong. */
19863 : 33133 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
19864 : : integer_type_node));
19865 : :
19866 : 39189385 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
19867 : : }
19868 : :
19869 : : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
19870 : : FN. */
19871 : :
19872 : : bool
19873 : 275354395 : use_eh_spec_block (tree fn)
19874 : : {
19875 : 273446451 : return (flag_exceptions && flag_enforce_eh_specs
19876 : 273446451 : && !processing_template_decl
19877 : : /* We insert the EH_SPEC_BLOCK only in the original
19878 : : function; then, it is copied automatically to the
19879 : : clones. */
19880 : 99766612 : && !DECL_CLONED_FUNCTION_P (fn)
19881 : : /* Implicitly-generated constructors and destructors have
19882 : : exception specifications. However, those specifications
19883 : : are the union of the possible exceptions specified by the
19884 : : constructors/destructors for bases and members, so no
19885 : : unallowed exception will ever reach this function. By
19886 : : not creating the EH_SPEC_BLOCK we save a little memory,
19887 : : and we avoid spurious warnings about unreachable
19888 : : code. */
19889 : 85792723 : && !DECL_DEFAULTED_FN (fn)
19890 : 358791990 : && !type_throw_all_p (TREE_TYPE (fn)));
19891 : : }
19892 : :
19893 : : /* Helper function to push ARGS into the current lexical scope. DECL
19894 : : is the function declaration. NONPARMS is used to handle enum
19895 : : constants. */
19896 : :
19897 : : void
19898 : 140433369 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
19899 : : {
19900 : : /* If we're doing semantic analysis, then we'll call pushdecl
19901 : : for each of these. We must do them in reverse order so that
19902 : : they end in the correct forward order. */
19903 : 140433369 : args = nreverse (args);
19904 : :
19905 : 140433369 : tree next;
19906 : 429105203 : for (tree parm = args; parm; parm = next)
19907 : : {
19908 : 288671834 : next = DECL_CHAIN (parm);
19909 : 288671834 : if (TREE_CODE (parm) == PARM_DECL)
19910 : 288671834 : pushdecl (parm);
19911 : 0 : else if (nonparms)
19912 : : {
19913 : : /* If we find an enum constant or a type tag, put it aside for
19914 : : the moment. */
19915 : 0 : TREE_CHAIN (parm) = NULL_TREE;
19916 : 0 : *nonparms = chainon (*nonparms, parm);
19917 : : }
19918 : : }
19919 : :
19920 : : /* Get the decls in their original chain order and record in the
19921 : : function. This is all and only the PARM_DECLs that were
19922 : : pushed into scope by the loop above. */
19923 : 140433369 : DECL_ARGUMENTS (decl) = get_local_decls ();
19924 : 140433369 : }
19925 : :
19926 : : /* Store the parameter declarations into the current function declaration.
19927 : : This is called after parsing the parameter declarations, before
19928 : : digesting the body of the function.
19929 : :
19930 : : Also install to binding contour return value identifier, if any. */
19931 : :
19932 : : static void
19933 : 143882274 : store_parm_decls (tree current_function_parms)
19934 : : {
19935 : 143882274 : tree fndecl = current_function_decl;
19936 : :
19937 : : /* This is a chain of any other decls that came in among the parm
19938 : : declarations. If a parm is declared with enum {foo, bar} x;
19939 : : then CONST_DECLs for foo and bar are put here. */
19940 : 143882274 : tree nonparms = NULL_TREE;
19941 : :
19942 : 143882274 : if (current_function_parms)
19943 : : {
19944 : : /* This case is when the function was defined with an ANSI prototype.
19945 : : The parms already have decls, so we need not do anything here
19946 : : except record them as in effect
19947 : : and complain if any redundant old-style parm decls were written. */
19948 : :
19949 : 137802486 : tree specparms = current_function_parms;
19950 : :
19951 : : /* Must clear this because it might contain TYPE_DECLs declared
19952 : : at class level. */
19953 : 137802486 : current_binding_level->names = NULL;
19954 : :
19955 : 137802486 : do_push_parm_decls (fndecl, specparms, &nonparms);
19956 : : }
19957 : : else
19958 : 6079788 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
19959 : :
19960 : : /* Now store the final chain of decls for the arguments
19961 : : as the decl-chain of the current lexical scope.
19962 : : Put the enumerators in as well, at the front so that
19963 : : DECL_ARGUMENTS is not modified. */
19964 : 143882274 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
19965 : :
19966 : 143882274 : if (use_eh_spec_block (current_function_decl))
19967 : 19028732 : current_eh_spec_block = begin_eh_spec_block ();
19968 : 143882274 : }
19969 : :
19970 : :
19971 : : /* Mark CDTOR's implicit THIS argument for returning, if required by
19972 : : the ABI.. Return the decl for THIS, if it is to be returned, and
19973 : : NULL otherwise. */
19974 : :
19975 : : tree
19976 : 113807237 : maybe_prepare_return_this (tree cdtor)
19977 : : {
19978 : 113807237 : if (targetm.cxx.cdtor_returns_this ())
19979 : 0 : if (tree val = DECL_ARGUMENTS (cdtor))
19980 : : {
19981 : 0 : suppress_warning (val, OPT_Wuse_after_free);
19982 : 0 : return val;
19983 : : }
19984 : :
19985 : : return NULL_TREE;
19986 : : }
19987 : :
19988 : : /* Set the return value of the [cd]tor if the ABI wants that. */
19989 : :
19990 : : void
19991 : 7139119 : maybe_return_this ()
19992 : : {
19993 : 7139119 : if (tree val = maybe_prepare_return_this (current_function_decl))
19994 : : {
19995 : : /* Return the address of the object. */
19996 : 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
19997 : 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
19998 : 0 : DECL_RESULT (current_function_decl), val);
19999 : 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
20000 : 0 : add_stmt (exprstmt);
20001 : : }
20002 : 7139119 : }
20003 : :
20004 : : /* Do all the processing for the beginning of a destructor; set up the
20005 : : vtable pointers and cleanups for bases and members. */
20006 : :
20007 : : static void
20008 : 1516182 : begin_destructor_body (void)
20009 : : {
20010 : 1516182 : tree compound_stmt;
20011 : :
20012 : : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20013 : : issued an error message. We still want to try to process the
20014 : : body of the function, but initialize_vtbl_ptrs will crash if
20015 : : TYPE_BINFO is NULL. */
20016 : 1516182 : if (COMPLETE_TYPE_P (current_class_type))
20017 : : {
20018 : 1516182 : compound_stmt = begin_compound_stmt (0);
20019 : : /* Make all virtual function table pointers in non-virtual base
20020 : : classes point to CURRENT_CLASS_TYPE's virtual function
20021 : : tables. */
20022 : 1516182 : initialize_vtbl_ptrs (current_class_ptr);
20023 : 1516182 : finish_compound_stmt (compound_stmt);
20024 : :
20025 : 1516182 : if (flag_lifetime_dse
20026 : : /* Clobbering an empty base is harmful if it overlays real data. */
20027 : 1516182 : && !is_empty_class (current_class_type))
20028 : : {
20029 : 1319691 : if (sanitize_flags_p (SANITIZE_VPTR)
20030 : 223 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20031 : 1319754 : && TYPE_CONTAINS_VPTR_P (current_class_type))
20032 : : {
20033 : 63 : tree binfo = TYPE_BINFO (current_class_type);
20034 : 63 : tree ref
20035 : 63 : = cp_build_fold_indirect_ref (current_class_ptr);
20036 : :
20037 : 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20038 : 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20039 : 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20040 : : NOP_EXPR, vtbl,
20041 : : tf_warning_or_error);
20042 : : /* If the vptr is shared with some virtual nearly empty base,
20043 : : don't clear it if not in charge, the dtor of the virtual
20044 : : nearly empty base will do that later. */
20045 : 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
20046 : : {
20047 : : tree c = current_class_type;
20048 : 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
20049 : : {
20050 : 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20051 : : {
20052 : 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
20053 : : tf_warning_or_error);
20054 : 36 : stmt = build_if_in_charge (stmt);
20055 : 36 : break;
20056 : : }
20057 : 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20058 : : }
20059 : : }
20060 : 63 : finish_decl_cleanup (NULL_TREE, stmt);
20061 : : }
20062 : : else
20063 : 1319628 : finish_decl_cleanup (NULL_TREE,
20064 : : build_clobber_this (CLOBBER_OBJECT_END));
20065 : : }
20066 : :
20067 : : /* And insert cleanups for our bases and members so that they
20068 : : will be properly destroyed if we throw. */
20069 : 1516182 : push_base_cleanups ();
20070 : : }
20071 : 1516182 : }
20072 : :
20073 : : /* Do the necessary processing for the beginning of a function body, which
20074 : : in this case includes member-initializers, but not the catch clauses of
20075 : : a function-try-block. Currently, this means opening a binding level
20076 : : for the member-initializers (in a ctor), member cleanups (in a dtor),
20077 : : and capture proxies (in a lambda operator()). */
20078 : :
20079 : : tree
20080 : 114593414 : begin_function_body (void)
20081 : : {
20082 : 234166850 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20083 : : return NULL_TREE;
20084 : :
20085 : 22813304 : if (processing_template_decl)
20086 : : /* Do nothing now. */;
20087 : : else
20088 : : /* Always keep the BLOCK node associated with the outermost pair of
20089 : : curly braces of a function. These are needed for correct
20090 : : operation of dwarfout.c. */
20091 : 7325915 : keep_next_level (true);
20092 : :
20093 : 22813304 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
20094 : 22813304 : current_binding_level->artificial = 1;
20095 : :
20096 : 22813304 : if (processing_template_decl)
20097 : : /* Do nothing now. */;
20098 : 14651830 : else if (DECL_DESTRUCTOR_P (current_function_decl))
20099 : 1516182 : begin_destructor_body ();
20100 : :
20101 : : return stmt;
20102 : : }
20103 : :
20104 : : /* Do the processing for the end of a function body. Currently, this means
20105 : : closing out the cleanups for fully-constructed bases and members, and in
20106 : : the case of the destructor, deleting the object if desired. Again, this
20107 : : is only meaningful for [cd]tors, since they are the only functions where
20108 : : there is a significant distinction between the main body and any
20109 : : function catch clauses. Handling, say, main() return semantics here
20110 : : would be wrong, as flowing off the end of a function catch clause for
20111 : : main() would also need to return 0. */
20112 : :
20113 : : void
20114 : 114593390 : finish_function_body (tree compstmt)
20115 : : {
20116 : 114593390 : if (compstmt == NULL_TREE)
20117 : : return;
20118 : :
20119 : : /* Close the block. */
20120 : 22813304 : finish_compound_stmt (compstmt);
20121 : :
20122 : 22813304 : if (processing_template_decl)
20123 : : /* Do nothing now. */;
20124 : 7325915 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
20125 : 7325915 : || DECL_DESTRUCTOR_P (current_function_decl))
20126 : 6863824 : maybe_return_this ();
20127 : : }
20128 : :
20129 : : /* Given a function, returns the BLOCK corresponding to the outermost level
20130 : : of curly braces, skipping the artificial block created for constructor
20131 : : initializers. */
20132 : :
20133 : : tree
20134 : 310056 : outer_curly_brace_block (tree fndecl)
20135 : : {
20136 : 310056 : tree block = DECL_INITIAL (fndecl);
20137 : 310056 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20138 : : return block;
20139 : 1076 : block = BLOCK_SUBBLOCKS (block);
20140 : 1076 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20141 : : return block;
20142 : 0 : block = BLOCK_SUBBLOCKS (block);
20143 : 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20144 : : return block;
20145 : : }
20146 : :
20147 : : /* If FNDECL is a class's key method, add the class to the list of
20148 : : keyed classes that should be emitted. */
20149 : :
20150 : : static void
20151 : 143886101 : record_key_method_defined (tree fndecl)
20152 : : {
20153 : 195086052 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20154 : 92745082 : && DECL_VIRTUAL_P (fndecl)
20155 : 146533399 : && !processing_template_decl)
20156 : : {
20157 : 1309618 : tree fnclass = DECL_CONTEXT (fndecl);
20158 : 1309618 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20159 : : {
20160 : 1612 : tree classdecl = TYPE_NAME (fnclass);
20161 : : /* Classes attached to a named module are already handled. */
20162 : 1612 : if (!DECL_LANG_SPECIFIC (classdecl)
20163 : 1660 : || !DECL_MODULE_ATTACH_P (classdecl))
20164 : 1567 : vec_safe_push (keyed_classes, fnclass);
20165 : : }
20166 : : }
20167 : 143886101 : }
20168 : :
20169 : : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20170 : : of "return *this;" immediately before its location, using FNDECL's
20171 : : first statement (if any) to give the indentation, if appropriate. */
20172 : :
20173 : : static void
20174 : 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20175 : : {
20176 : 18 : location_t indent = UNKNOWN_LOCATION;
20177 : 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20178 : 18 : if (stmts)
20179 : 9 : indent = EXPR_LOCATION (stmts);
20180 : 18 : richloc->add_fixit_insert_formatted ("return *this;",
20181 : : richloc->get_loc (),
20182 : : indent);
20183 : 18 : }
20184 : :
20185 : : /* Finish up a function declaration and compile that function
20186 : : all the way to assembler language output. The free the storage
20187 : : for the function definition. INLINE_P is TRUE if we just
20188 : : finished processing the body of an in-class inline function
20189 : : definition. (This processing will have taken place after the
20190 : : class definition is complete.) */
20191 : :
20192 : : tree
20193 : 143882238 : finish_function (bool inline_p)
20194 : : {
20195 : 143882238 : tree fndecl = current_function_decl;
20196 : 143882238 : tree fntype, ctype = NULL_TREE;
20197 : 143882238 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
20198 : :
20199 : : /* When we get some parse errors, we can end up without a
20200 : : current_function_decl, so cope. */
20201 : 143882238 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
20202 : 0 : return error_mark_node;
20203 : :
20204 : 143882238 : bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
20205 : 143882238 : && !processing_template_decl);
20206 : :
20207 : 143882238 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20208 : 143881670 : finish_lambda_scope ();
20209 : :
20210 : 143882238 : if (c_dialect_objc ())
20211 : 0 : objc_finish_function ();
20212 : :
20213 : 143882238 : record_key_method_defined (fndecl);
20214 : :
20215 : 143882238 : fntype = TREE_TYPE (fndecl);
20216 : :
20217 : : /* TREE_READONLY (fndecl) = 1;
20218 : : This caused &foo to be of type ptr-to-const-function
20219 : : which then got a warning when stored in a ptr-to-function variable. */
20220 : :
20221 : 143882238 : gcc_assert (building_stmt_list_p ());
20222 : : /* The current function is being defined, so its DECL_INITIAL should
20223 : : be set, and unless there's a multiple definition, it should be
20224 : : error_mark_node. */
20225 : 143882238 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20226 : :
20227 : 143882238 : cp_coroutine_transform *coroutine = nullptr;
20228 : 143882238 : if (flag_coroutines
20229 : 48561330 : && !processing_template_decl
20230 : 21207330 : && DECL_COROUTINE_P (fndecl)
20231 : 143883903 : && !DECL_RAMP_FN (fndecl))
20232 : : {
20233 : 1665 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20234 : : && !DECL_DEFAULTED_FN (fndecl));
20235 : 1665 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
20236 : 1665 : if (coroutine && coroutine->cp_valid_coroutine ())
20237 : 1665 : coroutine->apply_transforms ();
20238 : :
20239 : : /* We should handle coroutine IFNs in middle end lowering. */
20240 : 1665 : cfun->coroutine_component = true;
20241 : :
20242 : : /* Do not try to process the ramp's EH unless outlining succeeded. */
20243 : 1665 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
20244 : 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20245 : : (TREE_TYPE (fndecl)),
20246 : 360 : current_eh_spec_block);
20247 : :
20248 : : /* If outlining succeeded, then add contracts handling if needed. */
20249 : 1665 : if (coroutine->cp_valid_coroutine () && do_contracts)
20250 : 6 : maybe_apply_function_contracts (fndecl);
20251 : : }
20252 : : else
20253 : : /* For a cloned function, we've already got all the code we need;
20254 : : there's no need to add any extra bits. */
20255 : 143880573 : if (!DECL_CLONED_FUNCTION_P (fndecl))
20256 : : {
20257 : : /* Make it so that `main' always returns 0 by default. */
20258 : 129866882 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20259 : 129900000 : && !TREE_THIS_VOLATILE (current_function_decl))
20260 : 33118 : finish_return_stmt (integer_zero_node);
20261 : :
20262 : 129866882 : if (use_eh_spec_block (current_function_decl))
20263 : 19028369 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20264 : : (TREE_TYPE (current_function_decl)),
20265 : 19028369 : current_eh_spec_block);
20266 : :
20267 : 129866882 : if (do_contracts)
20268 : 420 : maybe_apply_function_contracts (current_function_decl);
20269 : :
20270 : : }
20271 : :
20272 : : /* If we're saving up tree structure, tie off the function now. */
20273 : 143882238 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20274 : :
20275 : 143882238 : finish_fname_decls ();
20276 : :
20277 : : /* This must come after expand_function_end because cleanups might
20278 : : have declarations (from inline functions) that need to go into
20279 : : this function's blocks. */
20280 : :
20281 : : /* If the current binding level isn't the outermost binding level
20282 : : for this function, either there is a bug, or we have experienced
20283 : : syntax errors and the statement tree is malformed. */
20284 : 143882238 : if (current_binding_level->kind != sk_function_parms)
20285 : : {
20286 : : /* Make sure we have already experienced errors. */
20287 : 12 : gcc_assert (errorcount);
20288 : :
20289 : : /* Throw away the broken statement tree and extra binding
20290 : : levels. */
20291 : 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20292 : :
20293 : 27 : while (current_binding_level->kind != sk_function_parms)
20294 : : {
20295 : 15 : if (current_binding_level->kind == sk_class)
20296 : 0 : pop_nested_class ();
20297 : : else
20298 : 15 : poplevel (0, 0, 0);
20299 : : }
20300 : : }
20301 : 143882238 : poplevel (1, 0, 1);
20302 : :
20303 : : /* Statements should always be full-expressions at the outermost set
20304 : : of curly braces for a function. */
20305 : 143882238 : gcc_assert (stmts_are_full_exprs_p ());
20306 : :
20307 : : /* If there are no return statements in a function with auto return type,
20308 : : the return type is void. But if the declared type is something like
20309 : : auto*, this is an error. */
20310 : 57291990 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20311 : 144728084 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20312 : : {
20313 : 231157 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20314 : 231142 : && !current_function_returns_value
20315 : 462108 : && !current_function_returns_null)
20316 : : {
20317 : : /* We haven't applied return type deduction because we haven't
20318 : : seen any return statements. Do that now. */
20319 : 230951 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20320 : 230951 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20321 : : void_node, node, tf_warning_or_error,
20322 : : adc_return_type);
20323 : :
20324 : 230951 : apply_deduced_return_type (fndecl, void_type_node);
20325 : 230951 : fntype = TREE_TYPE (fndecl);
20326 : : }
20327 : 206 : else if (!current_function_returns_value
20328 : 6 : && !current_function_returns_null)
20329 : : {
20330 : 6 : auto_diagnostic_group d;
20331 : 12 : error ("no return statements in function returning %qT",
20332 : 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20333 : 6 : inform (input_location, "only plain %<auto%> return type can be "
20334 : : "deduced to %<void%>");
20335 : 6 : }
20336 : : }
20337 : :
20338 : 143882238 : if (FNDECL_USED_AUTO (fndecl)
20339 : 143882238 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20340 : 845640 : if (location_t fcloc = failed_completion_location (fndecl))
20341 : : {
20342 : 6 : auto_diagnostic_group adg;
20343 : 6 : if (warning (OPT_Wsfinae_incomplete_,
20344 : : "defining %qD, which previously failed to be deduced "
20345 : : "in a SFINAE context", fndecl)
20346 : 6 : && warn_sfinae_incomplete == 1)
20347 : 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
20348 : : "-Wsfinae-incomplete=2");
20349 : 6 : }
20350 : :
20351 : : /* Remember that we were in class scope. */
20352 : 143882238 : if (current_class_name)
20353 : 107020059 : ctype = current_class_type;
20354 : :
20355 : 143882238 : if (DECL_DELETED_FN (fndecl))
20356 : : {
20357 : 87 : DECL_INITIAL (fndecl) = error_mark_node;
20358 : 87 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
20359 : 87 : goto cleanup;
20360 : : }
20361 : :
20362 : 143882151 : if (flag_openmp)
20363 : 289257 : if (tree attr = lookup_attribute ("omp declare variant base",
20364 : 289257 : DECL_ATTRIBUTES (fndecl)))
20365 : 159 : omp_declare_variant_finalize (fndecl, attr);
20366 : :
20367 : : /* Complain if there's just no return statement. */
20368 : 143882151 : if ((warn_return_type
20369 : 41977 : || (cxx_dialect >= cxx14
20370 : 33984 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20371 : 143850877 : && !VOID_TYPE_P (TREE_TYPE (fntype))
20372 : 88636867 : && !dependent_type_p (TREE_TYPE (fntype))
20373 : 45917523 : && !current_function_returns_value && !current_function_returns_null
20374 : : /* Don't complain if we abort or throw. */
20375 : 64916 : && !current_function_returns_abnormally
20376 : : /* Don't complain if there's an infinite loop. */
20377 : 922 : && !current_function_infinite_loop
20378 : : /* Don't complain if we are declared noreturn. */
20379 : 854 : && !TREE_THIS_VOLATILE (fndecl)
20380 : 848 : && !DECL_NAME (DECL_RESULT (fndecl))
20381 : 848 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20382 : : /* Structor return values (if any) are set by the compiler. */
20383 : 1226 : && !DECL_CONSTRUCTOR_P (fndecl)
20384 : 613 : && !DECL_DESTRUCTOR_P (fndecl)
20385 : 143882764 : && targetm.warn_func_return (fndecl))
20386 : : {
20387 : 607 : gcc_rich_location richloc (input_location);
20388 : : /* Potentially add a "return *this;" fix-it hint for
20389 : : assignment operators. */
20390 : 607 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20391 : : {
20392 : 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20393 : 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
20394 : 32 : && current_class_ref
20395 : 26 : && same_type_ignoring_top_level_qualifiers_p
20396 : 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20397 : 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
20398 : 18 : add_return_star_this_fixit (&richloc, fndecl);
20399 : : }
20400 : 607 : if (cxx_dialect >= cxx14
20401 : 607 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
20402 : 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
20403 : : "returning non-void");
20404 : 605 : else if (warning_at (&richloc, OPT_Wreturn_type,
20405 : : "no return statement in function returning "
20406 : : "non-void"))
20407 : 159 : suppress_warning (fndecl, OPT_Wreturn_type);
20408 : 607 : }
20409 : :
20410 : : /* Lambda closure members are implicitly constexpr if possible. */
20411 : 143882151 : if (cxx_dialect >= cxx17
20412 : 246709338 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20413 : 1275080 : DECL_DECLARED_CONSTEXPR_P (fndecl)
20414 : 1275080 : = ((processing_template_decl
20415 : 550319 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
20416 : 2549135 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20417 : :
20418 : : /* Save constexpr function body before it gets munged by
20419 : : the NRV transformation. */
20420 : 143882151 : maybe_save_constexpr_fundef (fndecl);
20421 : :
20422 : : /* Invoke the pre-genericize plugin before we start munging things. */
20423 : 143882151 : if (!processing_template_decl)
20424 : 57291903 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
20425 : :
20426 : : /* Perform delayed folding before NRV transformation. */
20427 : 143882151 : if (!processing_template_decl
20428 : 114583806 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20429 : 200639087 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20430 : 56756498 : cp_fold_function (fndecl);
20431 : :
20432 : : /* Set up the named return value optimization, if we can. Candidate
20433 : : variables are selected in check_return_expr. */
20434 : 143882151 : if (tree r = current_function_return_value)
20435 : : {
20436 : 35837744 : if (r != error_mark_node)
20437 : 181545 : finalize_nrv (fndecl, r);
20438 : 35837744 : current_function_return_value = NULL_TREE;
20439 : : }
20440 : :
20441 : : /* Must mark the RESULT_DECL as being in this function. */
20442 : 143882151 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
20443 : :
20444 : : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
20445 : : to the FUNCTION_DECL node itself. */
20446 : 143882151 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
20447 : :
20448 : : /* Store the end of the function, so that we get good line number
20449 : : info for the epilogue. */
20450 : 143882151 : cfun->function_end_locus = input_location;
20451 : :
20452 : : /* Complain about parameters that are only set, but never otherwise used. */
20453 : 143882151 : if (warn_unused_but_set_parameter
20454 : 927744 : && !processing_template_decl
20455 : 477172 : && errorcount == unused_but_set_errorcount
20456 : 144359320 : && !DECL_CLONED_FUNCTION_P (fndecl))
20457 : : {
20458 : 373885 : tree decl;
20459 : :
20460 : 373885 : for (decl = DECL_ARGUMENTS (fndecl);
20461 : 997527 : decl;
20462 : 623642 : decl = DECL_CHAIN (decl))
20463 : 623642 : if (TREE_USED (decl)
20464 : 516555 : && TREE_CODE (decl) == PARM_DECL
20465 : 516555 : && !DECL_READ_P (decl)
20466 : 225 : && DECL_NAME (decl)
20467 : 225 : && !DECL_ARTIFICIAL (decl)
20468 : 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
20469 : 225 : && !DECL_IN_SYSTEM_HEADER (decl)
20470 : 225 : && TREE_TYPE (decl) != error_mark_node
20471 : 225 : && !TYPE_REF_P (TREE_TYPE (decl))
20472 : 623867 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
20473 : 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
20474 : 225 : warning_at (DECL_SOURCE_LOCATION (decl),
20475 : 225 : OPT_Wunused_but_set_parameter_,
20476 : : "parameter %qD set but not used", decl);
20477 : 373885 : unused_but_set_errorcount = errorcount;
20478 : : }
20479 : :
20480 : : /* Complain about locally defined typedefs that are not used in this
20481 : : function. */
20482 : 143882151 : maybe_warn_unused_local_typedefs ();
20483 : :
20484 : : /* Possibly warn about unused parameters. */
20485 : 143882151 : if (warn_unused_parameter
20486 : 658428 : && !processing_template_decl
20487 : 144201814 : && !DECL_CLONED_FUNCTION_P (fndecl))
20488 : 246133 : do_warn_unused_parameter (fndecl);
20489 : :
20490 : : /* Genericize before inlining. */
20491 : 143882151 : if (!processing_template_decl
20492 : 114583806 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20493 : 200638937 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20494 : 56756348 : cp_genericize (fndecl);
20495 : :
20496 : : /* If this function can't throw any exceptions, remember that. */
20497 : 143882151 : if (!processing_template_decl
20498 : 57291903 : && !cp_function_chain->can_throw
20499 : 43250926 : && !flag_non_call_exceptions
20500 : 43246238 : && !decl_replaceable_p (fndecl,
20501 : 43246238 : opt_for_fn (fndecl, flag_semantic_interposition))
20502 : 186874809 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
20503 : 42990289 : TREE_NOTHROW (fndecl) = 1;
20504 : :
20505 : 143882238 : cleanup:
20506 : :
20507 : : /* We're leaving the context of this function, so zap cfun. It's still in
20508 : : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
20509 : 143882238 : set_cfun (NULL);
20510 : 143882238 : current_function_decl = NULL;
20511 : :
20512 : : /* If this is an in-class inline definition, we may have to pop the
20513 : : bindings for the template parameters that we added in
20514 : : maybe_begin_member_template_processing when start_function was
20515 : : called. */
20516 : 143882238 : if (inline_p)
20517 : 70546926 : maybe_end_member_template_processing ();
20518 : :
20519 : : /* Leave the scope of the class. */
20520 : 143882238 : if (ctype)
20521 : 107020059 : pop_nested_class ();
20522 : :
20523 : 143882238 : --function_depth;
20524 : :
20525 : : /* Clean up. */
20526 : 143882238 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
20527 : :
20528 : : /* Build outlined functions for coroutines and contracts. */
20529 : :
20530 : 143882238 : if (coroutine)
20531 : : {
20532 : : /* Emit the resumer and destroyer functions now, providing that we have
20533 : : not encountered some fatal error. */
20534 : 1665 : if (coroutine->cp_valid_coroutine ())
20535 : : {
20536 : 1581 : coroutine->finish_transforms ();
20537 : 1581 : resumer = coroutine->get_resumer ();
20538 : 1581 : destroyer = coroutine->get_destroyer ();
20539 : 1581 : expand_or_defer_fn (resumer);
20540 : 1581 : expand_or_defer_fn (destroyer);
20541 : : }
20542 : 1665 : delete coroutine;
20543 : : }
20544 : :
20545 : : /* If we have used outlined contracts checking functions, build and emit
20546 : : them here. */
20547 : 143882238 : finish_function_contracts (fndecl);
20548 : :
20549 : 143882238 : return fndecl;
20550 : : }
20551 : :
20552 : : /* Create the FUNCTION_DECL for a function definition.
20553 : : DECLSPECS and DECLARATOR are the parts of the declaration;
20554 : : they describe the return type and the name of the function,
20555 : : but twisted together in a fashion that parallels the syntax of C.
20556 : :
20557 : : This function creates a binding context for the function body
20558 : : as well as setting up the FUNCTION_DECL in current_function_decl.
20559 : :
20560 : : Returns a FUNCTION_DECL on success.
20561 : :
20562 : : If the DECLARATOR is not suitable for a function (it defines a datum
20563 : : instead), we return 0, which tells yyparse to report a parse error.
20564 : :
20565 : : May return void_type_node indicating that this method is actually
20566 : : a friend. See grokfield for more details.
20567 : :
20568 : : Came here with a `.pushlevel' .
20569 : :
20570 : : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
20571 : : CHANGES TO CODE IN `grokfield'. */
20572 : :
20573 : : tree
20574 : 70664491 : grokmethod (cp_decl_specifier_seq *declspecs,
20575 : : const cp_declarator *declarator, tree attrlist)
20576 : : {
20577 : 70664491 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
20578 : 70664491 : &attrlist);
20579 : :
20580 : 70664491 : if (fndecl == error_mark_node)
20581 : : return error_mark_node;
20582 : :
20583 : 70664388 : if (attrlist)
20584 : 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
20585 : :
20586 : : /* Pass friends other than inline friend functions back. */
20587 : 70664388 : if (fndecl == void_type_node)
20588 : : return fndecl;
20589 : :
20590 : 70664388 : if (DECL_IN_AGGR_P (fndecl))
20591 : : {
20592 : 0 : if (DECL_CLASS_SCOPE_P (fndecl))
20593 : 0 : error ("%qD is already defined in class %qT", fndecl,
20594 : 0 : DECL_CONTEXT (fndecl));
20595 : 0 : return error_mark_node;
20596 : : }
20597 : :
20598 : 70664388 : check_template_shadow (fndecl);
20599 : :
20600 : : /* p1779 ABI-Isolation makes inline not a default for in-class
20601 : : definitions attached to a named module. If the user explicitly
20602 : : made it inline, grokdeclarator will already have done the right
20603 : : things. */
20604 : 70664388 : if ((!named_module_attach_p ()
20605 : 630 : || flag_module_implicit_inline
20606 : : /* Lambda's operator function remains inline. */
20607 : 1233 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
20608 : : /* If the user explicitly asked for this to be inline, we don't
20609 : : need to do more, but more importantly we want to warn if we
20610 : : can't inline it. */
20611 : 70664012 : && !DECL_DECLARED_INLINE_P (fndecl))
20612 : : {
20613 : 51012547 : if (TREE_PUBLIC (fndecl))
20614 : 49511407 : DECL_COMDAT (fndecl) = 1;
20615 : 51012547 : DECL_DECLARED_INLINE_P (fndecl) = 1;
20616 : : /* It's ok if we can't inline this. */
20617 : 51012547 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
20618 : : }
20619 : :
20620 : : /* We process method specializations in finish_struct_1. */
20621 : 70664388 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
20622 : : {
20623 : : /* Avoid calling decl_spec_seq... until we have to. */
20624 : 58149611 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
20625 : 58149611 : fndecl = push_template_decl (fndecl, friendp);
20626 : 58149611 : if (fndecl == error_mark_node)
20627 : : return fndecl;
20628 : : }
20629 : :
20630 : 70664381 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
20631 : : {
20632 : 0 : fndecl = copy_node (fndecl);
20633 : 0 : TREE_CHAIN (fndecl) = NULL_TREE;
20634 : : }
20635 : :
20636 : 70664381 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
20637 : :
20638 : 70664381 : DECL_IN_AGGR_P (fndecl) = 1;
20639 : 70664381 : return fndecl;
20640 : : }
20641 : :
20642 : :
20643 : : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
20644 : : we can lay it out later, when and if its type becomes complete.
20645 : :
20646 : : Also handle constexpr variables where the initializer involves
20647 : : an unlowered PTRMEM_CST because the class isn't complete yet. */
20648 : :
20649 : : void
20650 : 89336548 : maybe_register_incomplete_var (tree var)
20651 : : {
20652 : 89336548 : gcc_assert (VAR_P (var));
20653 : :
20654 : : /* Keep track of variables with incomplete types. */
20655 : 39838591 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
20656 : 129173504 : && DECL_EXTERNAL (var))
20657 : : {
20658 : 5610482 : tree inner_type = TREE_TYPE (var);
20659 : :
20660 : 5768953 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
20661 : 158471 : inner_type = TREE_TYPE (inner_type);
20662 : 5610482 : inner_type = TYPE_MAIN_VARIANT (inner_type);
20663 : :
20664 : 5654100 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
20665 : : /* RTTI TD entries are created while defining the type_info. */
20666 : 5610482 : || (TYPE_LANG_SPECIFIC (inner_type)
20667 : 2267001 : && TYPE_BEING_DEFINED (inner_type)))
20668 : : {
20669 : 43618 : incomplete_var iv = {var, inner_type};
20670 : 43618 : vec_safe_push (incomplete_vars, iv);
20671 : : }
20672 : 10829415 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
20673 : 3450958 : && DECL_CLASS_SCOPE_P (var)
20674 : 1087127 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
20675 : 1087127 : && decl_constant_var_p (var)
20676 : 6274130 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
20677 : : {
20678 : : /* When the outermost open class is complete we can resolve any
20679 : : pointers-to-members. */
20680 : 13 : tree context = outermost_open_class ();
20681 : 13 : incomplete_var iv = {var, context};
20682 : 13 : vec_safe_push (incomplete_vars, iv);
20683 : : }
20684 : : }
20685 : 89336548 : }
20686 : :
20687 : : /* Called when a class type (given by TYPE) is defined. If there are
20688 : : any existing VAR_DECLs whose type has been completed by this
20689 : : declaration, update them now. */
20690 : :
20691 : : void
20692 : 57011548 : complete_vars (tree type)
20693 : : {
20694 : 57011548 : unsigned ix;
20695 : 57011548 : incomplete_var *iv;
20696 : :
20697 : 57438940 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
20698 : : {
20699 : 427392 : if (same_type_p (type, iv->incomplete_type))
20700 : : {
20701 : 42475 : tree var = iv->decl;
20702 : 42475 : tree type = TREE_TYPE (var);
20703 : :
20704 : 42475 : if (type != error_mark_node
20705 : 42475 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
20706 : 42472 : == iv->incomplete_type))
20707 : : {
20708 : : /* Complete the type of the variable. */
20709 : 42459 : complete_type (type);
20710 : 42459 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
20711 : 42459 : if (COMPLETE_TYPE_P (type))
20712 : 42444 : layout_var_decl (var);
20713 : : }
20714 : :
20715 : : /* Remove this entry from the list. */
20716 : 42475 : incomplete_vars->unordered_remove (ix);
20717 : : }
20718 : : else
20719 : 384917 : ix++;
20720 : : }
20721 : 57011548 : }
20722 : :
20723 : : /* If DECL is of a type which needs a cleanup, build and return an
20724 : : expression to perform that cleanup here. Return NULL_TREE if no
20725 : : cleanup need be done. DECL can also be a _REF when called from
20726 : : split_nonconstant_init_1. */
20727 : :
20728 : : tree
20729 : 67017077 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
20730 : : {
20731 : 67017077 : tree type;
20732 : 67017077 : tree attr;
20733 : 67017077 : tree cleanup;
20734 : :
20735 : : /* Assume no cleanup is required. */
20736 : 67017077 : cleanup = NULL_TREE;
20737 : :
20738 : 67017077 : if (!decl || error_operand_p (decl))
20739 : : return cleanup;
20740 : :
20741 : : /* Handle "__attribute__((cleanup))". We run the cleanup function
20742 : : before the destructor since the destructor is what actually
20743 : : terminates the lifetime of the object. */
20744 : 67017077 : if (DECL_P (decl))
20745 : 66644054 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
20746 : : else
20747 : : attr = NULL_TREE;
20748 : 66644054 : if (attr)
20749 : : {
20750 : 157 : tree id;
20751 : 157 : tree fn;
20752 : 157 : tree arg;
20753 : :
20754 : : /* Get the name specified by the user for the cleanup function. */
20755 : 157 : id = TREE_VALUE (TREE_VALUE (attr));
20756 : : /* Look up the name to find the cleanup function to call. It is
20757 : : important to use lookup_name here because that is what is
20758 : : used in c-common.cc:handle_cleanup_attribute when performing
20759 : : initial checks on the attribute. Note that those checks
20760 : : include ensuring that the function found is not an overloaded
20761 : : function, or an object with an overloaded call operator,
20762 : : etc.; we can rely on the fact that the function found is an
20763 : : ordinary FUNCTION_DECL. */
20764 : 157 : fn = lookup_name (id);
20765 : 157 : arg = build_address (decl);
20766 : 157 : if (!mark_used (decl, complain) && !(complain & tf_error))
20767 : 0 : return error_mark_node;
20768 : 157 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
20769 : 157 : if (cleanup == error_mark_node)
20770 : : return error_mark_node;
20771 : : }
20772 : : /* Handle ordinary C++ destructors. */
20773 : 67017065 : type = TREE_TYPE (decl);
20774 : 67017065 : if (type_build_dtor_call (type))
20775 : : {
20776 : 5313363 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
20777 : 5313363 : tree addr;
20778 : 5313363 : tree call;
20779 : :
20780 : 5313363 : if (TREE_CODE (type) == ARRAY_TYPE)
20781 : : addr = decl;
20782 : : else
20783 : 5311613 : addr = build_address (decl);
20784 : :
20785 : 5313363 : call = build_delete (input_location, TREE_TYPE (addr), addr,
20786 : : sfk_complete_destructor, flags, 0, complain);
20787 : 5313363 : if (call == error_mark_node)
20788 : : cleanup = error_mark_node;
20789 : 5313324 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
20790 : : /* Discard the call. */;
20791 : 3943023 : else if (decl_maybe_constant_destruction (decl, type)
20792 : 3943170 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
20793 : 147 : cxx_constant_dtor (call, decl);
20794 : 3942876 : else if (cleanup)
20795 : 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
20796 : : else
20797 : : cleanup = call;
20798 : : }
20799 : :
20800 : : /* build_delete sets the location of the destructor call to the
20801 : : current location, even though the destructor is going to be
20802 : : called later, at the end of the current scope. This can lead to
20803 : : a "jumpy" behavior for users of debuggers when they step around
20804 : : the end of the block. So let's unset the location of the
20805 : : destructor call instead. */
20806 : 67017065 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
20807 : 67017065 : if (cleanup && CONVERT_EXPR_P (cleanup))
20808 : 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
20809 : :
20810 : 0 : if (cleanup
20811 : 3943057 : && DECL_P (decl)
20812 : 3942586 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
20813 : : /* Treat objects with destructors as used; the destructor may do
20814 : : something substantive. */
20815 : 3942580 : && !mark_used (decl, complain) && !(complain & tf_error))
20816 : 0 : return error_mark_node;
20817 : :
20818 : 3943057 : if (cleanup && cfun && !processing_template_decl
20819 : 70519511 : && !expr_noexcept_p (cleanup, tf_none))
20820 : 5893 : cp_function_chain->throwing_cleanup = true;
20821 : :
20822 : : return cleanup;
20823 : : }
20824 : :
20825 : :
20826 : : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
20827 : : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
20828 : : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
20829 : :
20830 : : tree
20831 : 2764012 : static_fn_type (tree memfntype)
20832 : : {
20833 : 2764012 : tree fntype;
20834 : 2764012 : tree args;
20835 : :
20836 : 2764012 : if (TYPE_PTRMEMFUNC_P (memfntype))
20837 : 21522 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
20838 : 2764012 : if (INDIRECT_TYPE_P (memfntype)
20839 : 2428982 : || TREE_CODE (memfntype) == FUNCTION_DECL)
20840 : 2585169 : memfntype = TREE_TYPE (memfntype);
20841 : 2764012 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
20842 : : return memfntype;
20843 : 563261 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
20844 : 563261 : args = TYPE_ARG_TYPES (memfntype);
20845 : 563261 : fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
20846 : 563261 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
20847 : 563261 : fntype = (cp_build_type_attribute_variant
20848 : 563261 : (fntype, TYPE_ATTRIBUTES (memfntype)));
20849 : 563261 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
20850 : 563261 : return fntype;
20851 : : }
20852 : :
20853 : : /* DECL was originally constructed as a non-static member function,
20854 : : but turned out to be static. Update it accordingly. */
20855 : :
20856 : : void
20857 : 164921 : revert_static_member_fn (tree decl)
20858 : : {
20859 : 164921 : tree stype = static_fn_type (decl);
20860 : 164921 : cp_cv_quals quals = type_memfn_quals (stype);
20861 : 164921 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
20862 : :
20863 : 164921 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
20864 : 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
20865 : :
20866 : 164921 : TREE_TYPE (decl) = stype;
20867 : :
20868 : 164921 : if (DECL_ARGUMENTS (decl))
20869 : 164921 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
20870 : 164921 : DECL_STATIC_FUNCTION_P (decl) = 1;
20871 : 164921 : }
20872 : :
20873 : : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
20874 : : one of the language-independent trees. */
20875 : :
20876 : : enum cp_tree_node_structure_enum
20877 : 32850452982 : cp_tree_node_structure (union lang_tree_node * t)
20878 : : {
20879 : 32850452982 : switch (TREE_CODE (&t->generic))
20880 : : {
20881 : : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
20882 : 143527750 : case BASELINK: return TS_CP_BASELINK;
20883 : 35586915 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
20884 : 11131928 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
20885 : 3354369 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
20886 : 1965508455 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
20887 : 2336511 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
20888 : 171268 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
20889 : 626116798 : case OVERLOAD: return TS_CP_OVERLOAD;
20890 : 795 : case PTRMEM_CST: return TS_CP_PTRMEM;
20891 : 14280932 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
20892 : 604939047 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
20893 : 1401559439 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
20894 : 597965271 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
20895 : 14509722 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
20896 : 6 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
20897 : 2481617 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
20898 : 27426982159 : default: return TS_CP_GENERIC;
20899 : : }
20900 : : }
20901 : :
20902 : : bool
20903 : 10036 : cp_missing_noreturn_ok_p (tree decl)
20904 : : {
20905 : : /* A missing noreturn is ok for the `main' function. */
20906 : 10036 : return DECL_MAIN_P (decl);
20907 : : }
20908 : :
20909 : : /* Return the decl used to identify the COMDAT group into which DECL should
20910 : : be placed. */
20911 : :
20912 : : tree
20913 : 86844116 : cxx_comdat_group (tree decl)
20914 : : {
20915 : : /* Virtual tables, construction virtual tables, and virtual table
20916 : : tables all go in a single COMDAT group, named after the primary
20917 : : virtual table. */
20918 : 86844116 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
20919 : 453842 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
20920 : : /* For all other DECLs, the COMDAT group is the mangled name of the
20921 : : declaration itself. */
20922 : : else
20923 : : {
20924 : 86390274 : while (DECL_THUNK_P (decl))
20925 : : {
20926 : : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
20927 : : into the same section as the target function. In that case
20928 : : we must return target's name. */
20929 : 0 : tree target = THUNK_TARGET (decl);
20930 : 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
20931 : 0 : && DECL_SECTION_NAME (target) != NULL
20932 : 0 : && DECL_ONE_ONLY (target))
20933 : : decl = target;
20934 : : else
20935 : : break;
20936 : : }
20937 : : /* If a ctor/dtor has already set the comdat group by
20938 : : maybe_clone_body, don't override it. */
20939 : 86390274 : if (SUPPORTS_ONE_ONLY
20940 : 86390274 : && TREE_CODE (decl) == FUNCTION_DECL
20941 : 86390274 : && DECL_CLONED_FUNCTION_P (decl))
20942 : 15369510 : if (tree comdat = DECL_COMDAT_GROUP (decl))
20943 : : return comdat;
20944 : : }
20945 : :
20946 : : return decl;
20947 : : }
20948 : :
20949 : : /* Returns the return type for FN as written by the user, which may include
20950 : : a placeholder for a deduced return type. */
20951 : :
20952 : : tree
20953 : 489576492 : fndecl_declared_return_type (tree fn)
20954 : : {
20955 : 489576492 : fn = STRIP_TEMPLATE (fn);
20956 : 489576492 : if (FNDECL_USED_AUTO (fn))
20957 : 4759 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
20958 : :
20959 : 489571733 : return TREE_TYPE (TREE_TYPE (fn));
20960 : : }
20961 : :
20962 : : /* Returns true iff DECL is a variable or function declared with an auto type
20963 : : that has not yet been deduced to a real type. */
20964 : :
20965 : : bool
20966 : 782728365 : undeduced_auto_decl (tree decl)
20967 : : {
20968 : 782728365 : if (cxx_dialect < cxx11)
20969 : : return false;
20970 : 780396448 : STRIP_ANY_LOCATION_WRAPPER (decl);
20971 : 334031567 : return ((VAR_OR_FUNCTION_DECL_P (decl)
20972 : 76330304 : || TREE_CODE (decl) == TEMPLATE_DECL)
20973 : 1038232750 : && type_uses_auto (TREE_TYPE (decl)));
20974 : : }
20975 : :
20976 : : /* Complain if DECL has an undeduced return type. */
20977 : :
20978 : : bool
20979 : 250878283 : require_deduced_type (tree decl, tsubst_flags_t complain)
20980 : : {
20981 : 250878283 : if (undeduced_auto_decl (decl))
20982 : : {
20983 : 3062 : if (TREE_CODE (decl) == FUNCTION_DECL
20984 : 3035 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
20985 : 5509 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
20986 : : {
20987 : : /* Set the return type of __builtin_source_location. */
20988 : 2447 : tree type = get_source_location_impl_type ();
20989 : 2447 : if (type == error_mark_node)
20990 : : {
20991 : 33 : inform (input_location, "using %qs", "__builtin_source_location");
20992 : 33 : return false;
20993 : : }
20994 : 2414 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
20995 : 2414 : type = build_pointer_type (type);
20996 : 2414 : apply_deduced_return_type (decl, type);
20997 : 2414 : return true;
20998 : : }
20999 : :
21000 : 615 : if (warning_suppressed_p (decl) && seen_error ())
21001 : : /* We probably already complained about deduction failure. */;
21002 : 477 : else if (complain & tf_error)
21003 : 72 : error ("use of %qD before deduction of %<auto%>", decl);
21004 : 615 : note_failed_type_completion (decl, complain);
21005 : 615 : return false;
21006 : : }
21007 : : return true;
21008 : : }
21009 : :
21010 : : /* Create a representation of the explicit-specifier with
21011 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
21012 : :
21013 : : tree
21014 : 6338924 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
21015 : : {
21016 : 6338924 : if (check_for_bare_parameter_packs (expr))
21017 : 3 : return error_mark_node;
21018 : :
21019 : 6338921 : if (instantiation_dependent_expression_p (expr))
21020 : : /* Wait for instantiation, tsubst_function_decl will handle it. */
21021 : : return expr;
21022 : :
21023 : 5676732 : expr = build_converted_constant_bool_expr (expr, complain);
21024 : 5676732 : expr = instantiate_non_dependent_expr (expr, complain);
21025 : 5676732 : expr = cxx_constant_value (expr, complain);
21026 : 5676732 : return expr;
21027 : : }
21028 : :
21029 : : #include "gt-cp-decl.h"
|