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 : 226085373 : current_tmpl_spec_kind (int n_class_scopes)
312 : : {
313 : 226085373 : int n_template_parm_scopes = 0;
314 : 226085373 : int seen_specialization_p = 0;
315 : 226085373 : int innermost_specialization_p = 0;
316 : 226085373 : cp_binding_level *b;
317 : :
318 : : /* Scan through the template parameter scopes. */
319 : 226085373 : for (b = current_binding_level;
320 : 282985546 : b->kind == sk_template_parms;
321 : 56900173 : 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 : 56900173 : if (b->explicit_spec_p)
333 : : {
334 : 976975 : if (n_template_parm_scopes == 0)
335 : : innermost_specialization_p = 1;
336 : : else
337 : 1093 : seen_specialization_p = 1;
338 : : }
339 : 55923198 : else if (seen_specialization_p == 1)
340 : : return tsk_invalid_member_spec;
341 : :
342 : 56900173 : ++n_template_parm_scopes;
343 : : }
344 : :
345 : : /* Handle explicit instantiations. */
346 : 226085373 : if (processing_explicit_instantiation)
347 : : {
348 : 2020063 : 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 : 2020063 : return tsk_expl_inst;
358 : : }
359 : :
360 : 224065310 : 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 : 224065310 : 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 : 48620388 : 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 : 96757413 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
394 : : }
395 : :
396 : : /* Exit the current scope. */
397 : :
398 : : void
399 : 86828944 : finish_scope (void)
400 : : {
401 : 86828944 : poplevel (0, 0, 0);
402 : 86828944 : }
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 : 20304 : check_label_used (tree label)
409 : : {
410 : 20304 : if (!processing_template_decl)
411 : : {
412 : 20031 : 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 : 20022 : warn_for_unused_label (label);
424 : : }
425 : 20304 : }
426 : :
427 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
428 : :
429 : : static int
430 : 228832 : sort_labels (const void *a, const void *b)
431 : : {
432 : 228832 : tree label1 = *(tree const *) a;
433 : 228832 : tree label2 = *(tree const *) b;
434 : :
435 : : /* DECL_UIDs can never be equal. */
436 : 228832 : 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 : 142532394 : pop_labels (tree block)
508 : : {
509 : 142532394 : if (!named_labels)
510 : 142527829 : 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 : 4565 : auto_vec<tree, 32> labels (named_labels->elements ());
518 : 4565 : hash_table<named_label_hash>::iterator end (named_labels->end ());
519 : :
520 : 4565 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
521 : : {
522 : 6058 : 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 : 45131 : for (decltype (end) iter (named_labels->begin ()); iter != end; ++iter)
536 : : {
537 : 20283 : named_label_entry *ent = *iter;
538 : :
539 : 20283 : gcc_checking_assert (!ent->outer);
540 : 20283 : if (ent->label_decl)
541 : 20097 : labels.quick_push (ent->label_decl);
542 : 20283 : ggc_free (ent);
543 : : }
544 : 4565 : named_labels = NULL;
545 : 4565 : labels.qsort (sort_labels);
546 : :
547 : 24662 : while (labels.length ())
548 : : {
549 : 20097 : tree label = labels.pop ();
550 : :
551 : 20097 : DECL_CHAIN (label) = BLOCK_VARS (block);
552 : 20097 : BLOCK_VARS (block) = label;
553 : :
554 : 20097 : check_label_used (label);
555 : : }
556 : 4565 : }
557 : :
558 : : /* At the end of a block with local labels, restore the outer definition. */
559 : :
560 : : static void
561 : 207 : pop_local_label (tree id, tree label)
562 : : {
563 : 207 : check_label_used (label);
564 : 207 : named_label_entry **slot = named_labels->find_slot_with_hash
565 : 207 : (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
566 : 207 : named_label_entry *ent = *slot;
567 : :
568 : 207 : 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 : 207 : *slot = ent;
576 : 207 : }
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 : 1982337 : level_for_constexpr_if (cp_binding_level *b)
617 : : {
618 : 1978678 : return (b->kind == sk_cond && b->this_entity
619 : 268 : && TREE_CODE (b->this_entity) == IF_STMT
620 : 1982605 : && 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 : 1982313 : level_for_consteval_if (cp_binding_level *b)
627 : : {
628 : 1978654 : return (b->kind == sk_cond && b->this_entity
629 : 244 : && TREE_CODE (b->this_entity) == IF_STMT
630 : 1982557 : && 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 : 150100 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
651 : : {
652 : 150100 : named_label_entry *ent = *slot;
653 : 150100 : cp_binding_level *obl = bl->level_chain;
654 : :
655 : 150100 : if (ent->binding_level == bl)
656 : : {
657 : 9814 : tree decl;
658 : :
659 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
660 : : TREE_LISTs representing OVERLOADs, so be careful. */
661 : 14095 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
662 : 4354 : ? DECL_CHAIN (decl)
663 : 73 : : TREE_CHAIN (decl)))
664 : 4281 : if (decl_jump_unsafe (decl))
665 : : {
666 : 1814 : vec_safe_push (ent->bad_decls, decl);
667 : 1814 : ent->has_bad_decls = true;
668 : : }
669 : 2467 : else if (decl_instrument_init_bypass_p (decl))
670 : 347 : vec_safe_push (ent->bad_decls, decl);
671 : :
672 : 9814 : ent->binding_level = obl;
673 : 9814 : ent->names_in_scope = obl->names;
674 : 9814 : 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 : 348 : case sk_omp:
683 : 348 : ent->in_omp_scope = true;
684 : 348 : 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 : 3835 : case sk_block:
692 : 3835 : if (level_for_constexpr_if (obl))
693 : 15 : ent->in_constexpr_if = true;
694 : 3820 : else if (level_for_consteval_if (obl))
695 : 30 : ent->in_consteval_if = true;
696 : : break;
697 : : default:
698 : : break;
699 : : }
700 : : }
701 : 140286 : 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 : 150100 : 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 : 576179749 : poplevel (int keep, int reverse, int functionbody)
748 : : {
749 : 576179749 : tree link;
750 : : /* The chain of decls was accumulated in reverse order.
751 : : Put it into forward order, just for cleanliness. */
752 : 576179749 : tree decls;
753 : 576179749 : tree subblocks;
754 : 576179749 : tree block;
755 : 576179749 : tree decl;
756 : 576179749 : scope_kind kind;
757 : :
758 : 576179749 : auto_cond_timevar tv (TV_NAME_LOOKUP);
759 : 576180019 : restart:
760 : :
761 : 576180019 : block = NULL_TREE;
762 : :
763 : 576180019 : gcc_assert (current_binding_level->kind != sk_class
764 : : && current_binding_level->kind != sk_namespace);
765 : :
766 : 576180019 : if (current_binding_level->kind == sk_cleanup)
767 : : functionbody = 0;
768 : 576180019 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
769 : :
770 : 576180019 : 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 : 576180019 : gcc_assert (keep == 0 || keep == 1);
776 : :
777 : 576180019 : if (current_binding_level->keep)
778 : 7324717 : keep = 1;
779 : :
780 : : /* Any uses of undefined labels, and any defined labels, now operate
781 : : under constraints of next binding contour. */
782 : 576180019 : if (cfun && !functionbody && named_labels)
783 : 42247 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
784 : 192347 : (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 : 576180019 : decls = current_binding_level->names;
791 : 576180019 : if (reverse)
792 : : {
793 : 310229799 : decls = nreverse (decls);
794 : 310229799 : 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 : 576180019 : block = NULL_TREE;
801 : : /* Avoid function body block if possible. */
802 : 611664207 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
803 : : keep = 0;
804 : 540696092 : else if (keep == 1 || functionbody)
805 : 177813681 : block = make_node (BLOCK);
806 : 177813681 : if (block != NULL_TREE)
807 : : {
808 : 177813681 : BLOCK_VARS (block) = decls;
809 : 177813681 : BLOCK_SUBBLOCKS (block) = subblocks;
810 : : }
811 : :
812 : : /* In each subblock, record that this is its superior. */
813 : 576180019 : if (keep >= 0)
814 : 646944517 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
815 : 70764498 : BLOCK_SUPERCONTEXT (link) = block;
816 : :
817 : : /* Before we remove the declarations first check for unused variables. */
818 : 576180019 : if ((warn_unused_variable || warn_unused_but_set_variable)
819 : 6315154 : && current_binding_level->kind != sk_template_parms
820 : 5346230 : && !processing_template_decl)
821 : 6253681 : 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 : 3196979 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
827 : 3196979 : decl = strip_using_decl (udecl);
828 : :
829 : 3196979 : tree type = TREE_TYPE (decl);
830 : 3196979 : if (VAR_P (decl)
831 : 372939 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
832 : 3397 : && !DECL_IN_SYSTEM_HEADER (udecl)
833 : : /* For structured bindings, consider only real variables, not
834 : : subobjects. */
835 : 3385 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
836 : 3131 : : (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 : 3201061 : && (!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 : 1174490343 : for (link = decls; link; link = TREE_CHAIN (link))
880 : : {
881 : 598310324 : tree name;
882 : 598310324 : if (TREE_CODE (link) == TREE_LIST)
883 : : {
884 : 3956479 : decl = TREE_VALUE (link);
885 : 3956479 : name = TREE_PURPOSE (link);
886 : 3956479 : gcc_checking_assert (name);
887 : : }
888 : : else
889 : : {
890 : 594353845 : decl = link;
891 : 594353845 : name = DECL_NAME (decl);
892 : : }
893 : :
894 : : /* Remove the binding. */
895 : 598310324 : if (TREE_CODE (decl) == LABEL_DECL)
896 : 207 : pop_local_label (name, decl);
897 : : else
898 : 598310117 : pop_local_binding (name, decl);
899 : : }
900 : :
901 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
902 : 576180019 : for (link = current_binding_level->type_shadowed;
903 : 783337510 : link; link = TREE_CHAIN (link))
904 : 207157491 : 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 : 576180019 : if (block)
914 : : {
915 : 177813681 : tree* d;
916 : :
917 : 467537448 : for (d = &BLOCK_VARS (block); *d; )
918 : : {
919 : 289723767 : if (TREE_CODE (*d) == TREE_LIST
920 : 289723767 : || (!processing_template_decl
921 : 92340529 : && undeduced_auto_decl (*d)))
922 : 3956531 : *d = TREE_CHAIN (*d);
923 : : else
924 : 285767236 : 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 : 576180019 : if (functionbody)
931 : : {
932 : 142532394 : 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 : 107048467 : BLOCK_VARS (block) = 0;
938 : 107048467 : pop_labels (block);
939 : : }
940 : : else
941 : 35483927 : pop_labels (subblocks);
942 : : }
943 : :
944 : 576180019 : kind = current_binding_level->kind;
945 : 576180019 : if (kind == sk_cleanup)
946 : : {
947 : 270 : 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 : 270 : stmt = pop_stmt_list (current_binding_level->statement_list);
953 : 270 : stmt = c_build_bind_expr (input_location, block, stmt);
954 : 270 : add_stmt (stmt);
955 : : }
956 : :
957 : 576180019 : leave_scope ();
958 : 576180019 : if (functionbody)
959 : : {
960 : : /* The current function is being defined, so its DECL_INITIAL
961 : : should be error_mark_node. */
962 : 142532394 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
963 : 178016321 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
964 : 142532394 : if (subblocks)
965 : : {
966 : 66843886 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
967 : 43910834 : && BLOCK_SUBBLOCKS (subblocks))
968 : 1243953 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
969 : : else
970 : 34240235 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
971 : : }
972 : : }
973 : 433647625 : else if (block)
974 : 70765214 : current_binding_level->blocks
975 : 141530428 : = 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 : 362882411 : 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 : 576180019 : if (block)
990 : 177813681 : TREE_USED (block) = 1;
991 : :
992 : : /* All temporary bindings created for cleanups are popped silently. */
993 : 576180019 : if (kind == sk_cleanup)
994 : 270 : goto restart;
995 : :
996 : 1152359498 : return block;
997 : 576179749 : }
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 : 141126 : wrapup_namespace_globals ()
1005 : : {
1006 : 141126 : if (vec<tree, va_gc> *statics = static_decls)
1007 : : {
1008 : 14951983 : for (tree decl : *statics)
1009 : : {
1010 : 14836091 : if (warn_unused_function
1011 : 984984 : && TREE_CODE (decl) == FUNCTION_DECL
1012 : 739742 : && DECL_INITIAL (decl) == 0
1013 : 7598 : && DECL_EXTERNAL (decl)
1014 : 7598 : && !TREE_PUBLIC (decl)
1015 : 29 : && !DECL_ARTIFICIAL (decl)
1016 : 29 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
1017 : 14836097 : && !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 : 14836091 : if (VAR_P (decl)
1023 : 4310309 : && DECL_EXTERNAL (decl)
1024 : 1994939 : && DECL_INLINE_VAR_P (decl)
1025 : 14836109 : && 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 : 115892 : static_decls = NULL;
1032 : :
1033 : : /* Write out any globals that need to be output. */
1034 : 115892 : return wrapup_global_declarations (statics->address (),
1035 : 115892 : 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 : 152558293 : create_implicit_typedef (tree name, tree type)
1047 : : {
1048 : 152558293 : tree decl;
1049 : :
1050 : 152558293 : decl = build_decl (input_location, TYPE_DECL, name, type);
1051 : 152558293 : 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 : 152558293 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
1056 : 152558293 : TYPE_NAME (type) = decl;
1057 : 152558293 : TYPE_STUB_DECL (type) = decl;
1058 : :
1059 : 152558293 : 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 : 704285 : determine_local_discriminator (tree decl, tree name)
1073 : : {
1074 : 704285 : auto_cond_timevar tv (TV_NAME_LOOKUP);
1075 : 704285 : retrofit_lang_decl (decl);
1076 : 704285 : tree ctx = DECL_CONTEXT (decl);
1077 : 704285 : size_t nelts = vec_safe_length (local_entities);
1078 : 704285 : if (name == NULL_TREE)
1079 : 704026 : name = (TREE_CODE (decl) == TYPE_DECL
1080 : 1128564 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
1081 : 1264702 : ? NULL_TREE : DECL_NAME (decl));
1082 : 21181778 : for (size_t i = 0; i < nelts; i += 2)
1083 : : {
1084 : 20477958 : tree *pair = &(*local_entities)[i];
1085 : 20477958 : tree d = pair[0];
1086 : 20477958 : tree n = pair[1];
1087 : 20477958 : gcc_checking_assert (d != decl);
1088 : 20477958 : if (name == n
1089 : 3637522 : && TREE_CODE (decl) == TREE_CODE (d)
1090 : 24115471 : && 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 : 704285 : if (decl)
1105 : : {
1106 : 703820 : vec_safe_reserve (local_entities, 2);
1107 : 703820 : local_entities->quick_push (decl);
1108 : 703820 : local_entities->quick_push (name);
1109 : : }
1110 : 704285 : }
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 : 1971313776 : member_like_constrained_friend_p (tree decl)
1127 : : {
1128 : 1971313776 : return (TREE_CODE (decl) == FUNCTION_DECL
1129 : 595395129 : && DECL_UNIQUE_FRIEND_P (decl)
1130 : 85439788 : && DECL_FRIEND_CONTEXT (decl)
1131 : 38043773 : && get_constraints (decl)
1132 : 1971823363 : && (!DECL_TEMPLATE_INFO (decl)
1133 : 509587 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1134 : 4249 : || (uses_outer_template_parms_in_constraints
1135 : 4249 : (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 : 24527867 : 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 : 24527867 : if (member_like_constrained_friend_p (newfn)
1148 : 24527867 : || member_like_constrained_friend_p (oldfn))
1149 : 111 : return false;
1150 : :
1151 : : /* Compare only trailing requirements. */
1152 : 24527756 : tree reqs1 = get_trailing_function_requirements (newfn);
1153 : 24527756 : tree reqs2 = get_trailing_function_requirements (oldfn);
1154 : 24527756 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1155 : : return false;
1156 : :
1157 : : /* Substitution is needed when friends are involved. */
1158 : 24477591 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1159 : 24477591 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1160 : :
1161 : 24477591 : 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 : 656466881 : fns_correspond (tree newdecl, tree olddecl)
1181 : : {
1182 : 656466881 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1183 : : return false;
1184 : :
1185 : 608794841 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1186 : : {
1187 : 608768639 : if (!template_heads_equivalent_p (newdecl, olddecl))
1188 : : return 0;
1189 : 140486143 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1190 : 140486143 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1191 : : }
1192 : :
1193 : 140512345 : tree f1 = TREE_TYPE (newdecl);
1194 : 140512345 : tree f2 = TREE_TYPE (olddecl);
1195 : :
1196 : 140512345 : int rq1 = type_memfn_rqual (f1);
1197 : 140512345 : int rq2 = type_memfn_rqual (f2);
1198 : :
1199 : : /* If only one is a non-static member function, ignore ref-quals. */
1200 : 140512345 : 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 : 140512345 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1210 : 0 : rq1 = rq2;
1211 : :
1212 : 140512345 : bool types_match = rq1 == rq2;
1213 : :
1214 : 140512345 : if (types_match)
1215 : : {
1216 : 140512345 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1217 : 140512345 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1218 : 140512345 : 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 : 140512345 : if (types_match && flag_concepts)
1225 : 17307160 : 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 : 942769137 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1239 : : {
1240 : 942769137 : int types_match;
1241 : :
1242 : 942769137 : if (newdecl == olddecl)
1243 : : return 1;
1244 : :
1245 : 942703583 : 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 : 467169395 : gcc_assert (DECL_P (newdecl));
1251 : :
1252 : 467169395 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1253 : : {
1254 : : /* Specializations of different templates are different functions
1255 : : even if they have the same type. */
1256 : 261986489 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1257 : 261986489 : ? DECL_TI_TEMPLATE (newdecl)
1258 : 590302 : : NULL_TREE);
1259 : 261986489 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1260 : 261986489 : ? DECL_TI_TEMPLATE (olddecl)
1261 : 3186117 : : NULL_TREE);
1262 : 261986489 : if (t1 != t2)
1263 : : return 0;
1264 : :
1265 : 259390608 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1266 : 262527624 : && ! (DECL_EXTERN_C_P (newdecl)
1267 : 3137016 : && 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 : 239767464 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1273 : 239767464 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1274 : : return 0;
1275 : :
1276 : 239083638 : tree f1 = TREE_TYPE (newdecl);
1277 : 239083638 : tree f2 = TREE_TYPE (olddecl);
1278 : 239083638 : 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 : 238917247 : tree r2 = fndecl_declared_return_type (olddecl);
1284 : 238917247 : 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 : 238917247 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1289 : 6518608 : && DECL_EXTERN_C_P (olddecl)
1290 : 4138127 : && !same_type_p (r1, r2)
1291 : 70 : && TREE_CODE (r1) == POINTER_TYPE
1292 : 15 : && TREE_CODE (r2) == POINTER_TYPE
1293 : 238917259 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1294 : : r2 = r1;
1295 : :
1296 : 238917247 : tree p1 = TYPE_ARG_TYPES (f1);
1297 : 238917247 : tree p2 = TYPE_ARG_TYPES (f2);
1298 : :
1299 : 238917247 : if (same_type_p (r1, r2))
1300 : : {
1301 : 175688021 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1302 : 175687993 : && 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 : 175687887 : types_match =
1310 : 175687887 : compparms (p1, p2)
1311 : 28481108 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1312 : 204168754 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1313 : 1218826 : || comp_type_attributes (TREE_TYPE (newdecl),
1314 : 1218826 : 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 : 28480908 : if (types_match && flag_concepts)
1323 : 7220681 : 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 : 28480908 : if (types_match
1329 : 28414916 : && !DECL_EXTERN_C_P (newdecl)
1330 : 20375235 : && !DECL_EXTERN_C_P (olddecl)
1331 : 48852409 : && disjoint_version_decls (newdecl, olddecl))
1332 : : {
1333 : 8733 : if (record_versions)
1334 : 8646 : maybe_version_functions (newdecl, olddecl);
1335 : 8733 : return 0;
1336 : : }
1337 : : }
1338 : 205182906 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1339 : : {
1340 : 203742343 : if (!template_heads_equivalent_p (newdecl, olddecl))
1341 : : return 0;
1342 : :
1343 : 65382793 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1344 : 65382793 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1345 : :
1346 : 65382793 : 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 : 65382787 : if (TREE_CODE (newres) == TYPE_DECL)
1352 : 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1353 : : else
1354 : 65382757 : 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 : 1440563 : if (VAR_P (newdecl)
1361 : 960776 : && 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 : 1617524 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1368 : : return 0;
1369 : :
1370 : 1263650 : if (TREE_TYPE (newdecl) == error_mark_node)
1371 : 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1372 : 1263650 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1373 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1374 : 1263650 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1375 : : types_match = 0;
1376 : : else
1377 : 1263650 : types_match = comptypes (TREE_TYPE (newdecl),
1378 : 1263650 : 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 : 17382 : maybe_mark_function_versioned (tree decl)
1389 : : {
1390 : 17382 : 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 : 1047 : cgraph_node *node = cgraph_node::get_create (decl);
1395 : :
1396 : 1047 : if (!node->function_version ())
1397 : 1047 : node->insert_new_function_version ();
1398 : :
1399 : 1047 : DECL_FUNCTION_VERSIONED (decl) = 1;
1400 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1401 : : to include the version marker. */
1402 : 1047 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1403 : 1041 : mangle_decl (decl);
1404 : : }
1405 : 17382 : }
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 : 164798 : maybe_version_functions (tree newdecl, tree olddecl)
1413 : : {
1414 : 164798 : if (!disjoint_version_decls (newdecl, olddecl))
1415 : : return false;
1416 : :
1417 : 8673 : maybe_mark_function_versioned (olddecl);
1418 : 8673 : 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 : 8673 : maybe_mark_function_versioned (newdecl);
1425 : 8673 : 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 : 8673 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1437 : 8673 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1438 : 8673 : if (!fn_v)
1439 : 15 : fn_v = fn_node->insert_new_function_version ();
1440 : :
1441 : 8673 : cgraph_node::add_function_version (fn_v, newdecl);
1442 : :
1443 : 8673 : 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 : 18996174 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1457 : : {
1458 : 18996174 : if (TREE_CODE (newdecl) == TYPE_DECL
1459 : 18970306 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1460 : 15400853 : || TREE_CODE (newdecl) == CONST_DECL
1461 : 15400853 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1462 : 18996162 : return;
1463 : :
1464 : : /* Don't get confused by static member functions; that's a different
1465 : : use of `static'. */
1466 : 15400853 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1467 : 15400853 : && 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 : 15238170 : 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 : 18578934 : check_redeclaration_exception_specification (tree new_decl,
1493 : : tree old_decl)
1494 : : {
1495 : 18578934 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1496 : 18578934 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1497 : :
1498 : : /* Two default specs are equivalent, don't force evaluation. */
1499 : 7008161 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1500 : 18728676 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1501 : : return;
1502 : :
1503 : 18568642 : if (!type_dependent_expression_p (old_decl))
1504 : : {
1505 : 10990544 : maybe_instantiate_noexcept (new_decl);
1506 : 10990544 : maybe_instantiate_noexcept (old_decl);
1507 : : }
1508 : 18568642 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1509 : 18568642 : 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 : 18568642 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1518 : 14558963 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1519 : 33127522 : && !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 : 18996661 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1568 : : {
1569 : 18996661 : old_decl = STRIP_TEMPLATE (old_decl);
1570 : 18996661 : new_decl = STRIP_TEMPLATE (new_decl);
1571 : 18996661 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1572 : 18970769 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1573 : : return true;
1574 : 18970769 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1575 : 18970769 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1576 : : {
1577 : 18928914 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1578 : : return true;
1579 : 37158652 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1580 : 18579326 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1581 : : return true;
1582 : : }
1583 : 41861 : 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 : 11224104 : check_redeclaration_no_default_args (tree decl)
1630 : : {
1631 : 11224104 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1632 : :
1633 : 11224104 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1634 : 33024909 : t && t != void_list_node; t = TREE_CHAIN (t))
1635 : 21800862 : 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 : 6278774 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1654 : : {
1655 : 12190667 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1656 : : return;
1657 : :
1658 : 2077156 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1659 : 1038578 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1660 : 2788880 : t1 && t1 != void_list_node;
1661 : 1750302 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1662 : 2442151 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1663 : 4815377 : || (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 : 18011754 : merge_attribute_bits (tree newdecl, tree olddecl)
1680 : : {
1681 : 18011754 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1682 : 18011754 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1683 : 18011754 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1684 : 18011754 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1685 : 18011754 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1686 : 18011754 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1687 : 18011754 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1688 : 18011754 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1689 : 18011754 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1690 : 18011754 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1691 : 18011754 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1692 : 18011754 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1693 : 18011754 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1694 : 18011754 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1695 : 18011754 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1696 : 18011754 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1697 : 18011754 : }
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 : 200172824 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1708 : : {
1709 : 200172824 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1710 : 200172824 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1711 : : /* Function template declarations can be differentiated by parameter
1712 : : and return type. */
1713 : 200172824 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1714 : 200172824 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1715 : 200172824 : && 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 : 4195 : if (template_heads_equivalent_p (newdecl, olddecl)
1720 : 4195 : && 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 : 8060543 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1751 : : {
1752 : 8060543 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1753 : : == TREE_VEC_LENGTH (old_parms));
1754 : 22822924 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1755 : : {
1756 : 14762411 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1757 : 14762411 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1758 : 14762411 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1759 : 14762411 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1760 : 14762411 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1761 : : return false;
1762 : 14762408 : 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 : 14762381 : 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 : 198221 : old_default = new_default;
1774 : 14564160 : 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 : 1012839 : 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 : 344167011 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1797 : : {
1798 : 344167011 : unsigned olddecl_uid = DECL_UID (olddecl);
1799 : 344167011 : int types_match = 0;
1800 : 344167011 : int new_defines_function = 0;
1801 : 344167011 : tree new_template_info;
1802 : 344167011 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1803 : 344167011 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1804 : :
1805 : 344167011 : if (newdecl == olddecl)
1806 : : return olddecl;
1807 : :
1808 : 343321141 : 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 : 343321141 : if (TREE_TYPE (newdecl) == error_mark_node
1816 : 343321141 : || TREE_TYPE (olddecl) == error_mark_node)
1817 : : return error_mark_node;
1818 : :
1819 : : /* Check for redeclaration and other discrepancies. */
1820 : 343321106 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1821 : 343321106 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1822 : : {
1823 : 6366999 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1824 : : {
1825 : : /* Avoid warnings redeclaring built-ins which have not been
1826 : : explicitly declared. */
1827 : 185803 : if (was_hidden)
1828 : : {
1829 : 185775 : if (TREE_PUBLIC (newdecl)
1830 : 185775 : && 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 : 185775 : 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 : 6181196 : else if (!types_match)
1864 : : {
1865 : : /* Avoid warnings redeclaring built-ins which have not been
1866 : : explicitly declared. */
1867 : 2171517 : if (was_hidden)
1868 : : {
1869 : 826284 : tree t1, t2;
1870 : :
1871 : : /* A new declaration doesn't match a built-in one unless it
1872 : : is also extern "C". */
1873 : 826284 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1874 : 826284 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1875 : 826284 : if (!DECL_EXTERN_C_P (newdecl))
1876 : : return NULL_TREE;
1877 : :
1878 : 142458 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1879 : 142458 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1880 : 244484 : t1 || t2;
1881 : 102026 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1882 : : {
1883 : 244466 : if (!t1 || !t2)
1884 : : break;
1885 : : /* FILE, tm types are not known at the time
1886 : : we create the builtins. */
1887 : 759681 : for (unsigned i = 0;
1888 : 1003985 : i < sizeof (builtin_structptr_types)
1889 : : / sizeof (builtin_structptr_type);
1890 : : ++i)
1891 : 901953 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1892 : : {
1893 : 142272 : tree t = TREE_VALUE (t1);
1894 : :
1895 : 142272 : if (TYPE_PTR_P (t)
1896 : 284544 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1897 : 142272 : == get_identifier (builtin_structptr_types[i].str)
1898 : 284529 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1899 : : {
1900 : 142257 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1901 : :
1902 : 142257 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1903 : 142257 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1904 : 142257 : types_match = decls_match (newdecl, olddecl);
1905 : 142257 : if (types_match)
1906 : 142257 : return duplicate_decls (newdecl, olddecl,
1907 : 142257 : hiding, was_hidden);
1908 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1909 : : }
1910 : 15 : goto next_arg;
1911 : : }
1912 : :
1913 : 102032 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1914 : : break;
1915 : 102026 : 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 : 1345233 : else if ((DECL_EXTERN_C_P (newdecl)
1924 : 15 : && DECL_EXTERN_C_P (olddecl))
1925 : 2690451 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1926 : 1345218 : 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 : 4009679 : tree type = TREE_TYPE (newdecl);
1988 : 4009679 : tree attribs = (*targetm.merge_type_attributes)
1989 : 4009679 : (TREE_TYPE (olddecl), type);
1990 : :
1991 : 4009679 : type = cp_build_type_attribute_variant (type, attribs);
1992 : 4009679 : 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 : 4009880 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1998 : 3853333 : && was_hidden
1999 : 3853290 : && TREE_NOTHROW (newdecl)
2000 : 7606213 : && !TREE_NOTHROW (olddecl))
2001 : : {
2002 : 29008 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
2003 : 29008 : tree tmpdecl = builtin_decl_explicit (fncode);
2004 : 29008 : if (tmpdecl && tmpdecl != olddecl && types_match)
2005 : 29008 : TREE_NOTHROW (tmpdecl) = 1;
2006 : : }
2007 : :
2008 : : /* Whether or not the builtin can throw exceptions has no
2009 : : bearing on this declarator. */
2010 : 4009880 : TREE_NOTHROW (olddecl) = 0;
2011 : :
2012 : 4009880 : 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 : 336954107 : 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 : 84805043 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
2032 : 84805016 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
2033 : : /* Namespace conflicts with not namespace. */;
2034 : 48880639 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
2035 : 133685643 : || DECL_TYPE_TEMPLATE_P (newdecl))
2036 : : /* Class template conflicts. */;
2037 : 84804998 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
2038 : 48880639 : && DECL_TEMPLATE_RESULT (olddecl)
2039 : 48880639 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
2040 : 133685631 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
2041 : 35881559 : && DECL_TEMPLATE_RESULT (newdecl)
2042 : 35881559 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
2043 : : /* Variable template conflicts. */;
2044 : 84804989 : else if (concept_definition_p (olddecl)
2045 : 169609972 : || concept_definition_p (newdecl))
2046 : : /* Concept conflicts. */;
2047 : 84804977 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
2048 : 48920689 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
2049 : 84845039 : || (TREE_CODE (olddecl) == FUNCTION_DECL
2050 : 35881566 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2051 : : {
2052 : : /* One is a function and the other is a template
2053 : : function. */
2054 : 84762162 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
2055 : : return NULL_TREE;
2056 : :
2057 : : /* There can only be one! */
2058 : 59797 : auto_diagnostic_group d;
2059 : 59797 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
2060 : 59797 : && check_raw_literal_operator (olddecl))
2061 : 3 : error_at (newdecl_loc,
2062 : : "literal operator %q#D conflicts with"
2063 : : " raw literal operator", newdecl);
2064 : 59794 : 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 : 59797 : }
2074 : 42815 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
2075 : : /* A structured binding must be unique in its declarative region. */;
2076 : 40203 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2077 : 42812 : || 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 : 252149064 : else if (!types_match)
2088 : : {
2089 : 237130903 : 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 : 237130843 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2095 : : {
2096 : 200172842 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
2097 : 200172842 : 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 : 200172842 : if (TREE_CODE (oldres) == TYPE_DECL
2103 : 200172836 : || 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 : 200172833 : else if (TREE_CODE (oldres) == FUNCTION_DECL
2114 : 200172824 : && TREE_CODE (newres) == FUNCTION_DECL)
2115 : : {
2116 : 200172824 : if (duplicate_function_template_decls (newdecl, olddecl))
2117 : 0 : return error_mark_node;
2118 : : return NULL_TREE;
2119 : : }
2120 : : return NULL_TREE;
2121 : : }
2122 : 36958001 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2123 : : {
2124 : 36957853 : 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 : 36957796 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2137 : 36949609 : && !DECL_FUNCTION_VERSIONED (olddecl))
2138 : 8232 : || !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 : 36949564 : && !member_like_constrained_friend_p (newdecl)
2143 : 36535918 : && !member_like_constrained_friend_p (olddecl)
2144 : : // The functions have the same parameter types.
2145 : 36502897 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2146 : 36502897 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2147 : : // And the same constraints.
2148 : 36957859 : && equivalently_constrained (newdecl, olddecl))
2149 : : {
2150 : 63 : auto_diagnostic_group d;
2151 : 63 : error_at (newdecl_loc,
2152 : : "ambiguating new declaration of %q#D", newdecl);
2153 : 63 : inform (olddecl_loc,
2154 : : "old declaration %q#D", olddecl);
2155 : 63 : return error_mark_node;
2156 : 63 : }
2157 : : else
2158 : 36957733 : 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 : 15018161 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2170 : 15018161 : && 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 : 15018134 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2182 : 15018134 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2183 : 220 : && (!DECL_TEMPLATE_INFO (newdecl)
2184 : 220 : || (DECL_TI_TEMPLATE (newdecl)
2185 : 220 : != DECL_TI_TEMPLATE (olddecl))))
2186 : 11000138 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2187 : 567442 : && (!DECL_TEMPLATE_INFO (olddecl)
2188 : 567442 : || (DECL_TI_TEMPLATE (olddecl)
2189 : 567442 : != 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 : 15018134 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2200 : 15018134 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2201 : 590016 : && !DECL_USE_TEMPLATE (newdecl))
2202 : 11000138 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2203 : 22794 : && !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 : 15018134 : 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 : 15018113 : else if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
2228 : : && diagnose_versioned_decls (olddecl, newdecl))
2229 : : return error_mark_node;
2230 : : else
2231 : : {
2232 : 15018113 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2233 : 15018113 : if (errmsg)
2234 : : {
2235 : 608 : auto_diagnostic_group d;
2236 : 608 : error_at (newdecl_loc, errmsg, newdecl);
2237 : 608 : if (DECL_NAME (olddecl) != NULL_TREE)
2238 : 608 : inform (olddecl_loc,
2239 : 608 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2240 : : ? G_("%q#D previously defined here")
2241 : : : G_("%q#D previously declared here"), olddecl);
2242 : 608 : if (cxx_dialect >= cxx26
2243 : 147 : && DECL_NAME (newdecl)
2244 : 147 : && id_equal (DECL_NAME (newdecl), "_")
2245 : 621 : && !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 : 608 : return error_mark_node;
2281 : 608 : }
2282 : 15017505 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2283 : 10999901 : && DECL_INITIAL (olddecl) != NULL_TREE
2284 : 11763 : && !prototype_p (TREE_TYPE (olddecl))
2285 : 15017505 : && 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 : 14626388 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2295 : 26017406 : && 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 : 1758 : if (current_lang_depth () == 0)
2322 : : {
2323 : : /* There is no explicit linkage-specification, so we use
2324 : : the linkage from the previous declaration. */
2325 : 1746 : retrofit_lang_decl (newdecl);
2326 : 1746 : 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 : 15017505 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2341 : : ;
2342 : 14406073 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2343 : : {
2344 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2345 : 12724795 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2346 : 10409674 : && (/* grokfndecl passes member function templates too
2347 : : as FUNCTION_DECLs. */
2348 : 8257111 : 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 : 659566 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2354 : : {
2355 : 7677502 : check_redeclaration_no_default_args (newdecl);
2356 : :
2357 : 7677502 : if (DECL_TEMPLATE_INFO (olddecl)
2358 : 7677502 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2359 : : {
2360 : 2147946 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2361 : 2147946 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2362 : 2147946 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2363 : 2147946 : tree old_parms
2364 : 2147946 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2365 : 2147946 : merge_default_template_args (new_parms, old_parms,
2366 : : /*class_p=*/false);
2367 : : }
2368 : : }
2369 : : else
2370 : : {
2371 : 2732172 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2372 : 2732172 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2373 : 2732172 : int i = 1;
2374 : :
2375 : 7076810 : for (; t1 && t1 != void_list_node;
2376 : 4344638 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2377 : 4346396 : 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 : 2732172 : 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 : 19027385 : if (TREE_CODE (olddecl) == TYPE_DECL
2419 : 19027385 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2420 : 25871 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2421 : : return NULL_TREE;
2422 : :
2423 : 18996240 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2424 : : return NULL_TREE;
2425 : :
2426 : 18996228 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2427 : 30 : return error_mark_node;
2428 : :
2429 : 18996198 : if (modules_p ()
2430 : 41409 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2431 : 19020879 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2432 : : {
2433 : 24681 : if (!module_may_redeclare (olddecl, newdecl))
2434 : 24 : return error_mark_node;
2435 : :
2436 : 24657 : 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 : 21853 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2442 : 21853 : if (DECL_LANG_SPECIFIC (not_tmpl)
2443 : 21808 : && 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 : 22066 : && 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 : 21652 : else if (DECL_MODULE_EXPORT_P (newdecl))
2458 : 9032 : 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 : 18996174 : 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 : 18996174 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2473 : 15009736 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2474 : 19563601 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2475 : :
2476 : 18996174 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2477 : : {
2478 : 15009736 : if (merge_attr)
2479 : : {
2480 : 14442514 : {
2481 : 14442514 : auto_diagnostic_group d;
2482 : 14442514 : 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 : 14442514 : }
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 : 14442514 : tree a;
2492 : 14442514 : if (TREE_THIS_VOLATILE (newdecl)
2493 : 111416 : && !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 : 14442535 : && 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 : 15009736 : if (DECL_VINDEX (olddecl))
2511 : 163085 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2512 : 15009736 : if (DECL_CONTEXT (olddecl))
2513 : 15009736 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2514 : 15009736 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2515 : 15009736 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2516 : 15009736 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2517 : 15009736 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2518 : 15009736 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2519 : 15009736 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2520 : 15009736 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2521 : 15009736 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2522 : 15009736 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2523 : 15009736 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2524 : 15009736 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2525 : 2513486 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2526 : 5026972 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2527 : 15009736 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2528 : :
2529 : 15009736 : 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 : 15009748 : && (! 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 : 15009736 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2558 : 590016 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2559 : : {
2560 : 14442514 : 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 : 14442514 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2569 : 14442514 : if (DECL_DELETED_FN (olddecl)
2570 : 6 : && DECL_INITIAL (olddecl)
2571 : 14442520 : && 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 : 18996174 : if (TREE_CODE (olddecl) == TYPE_DECL)
2578 : : {
2579 : 25868 : tree newtype = TREE_TYPE (newdecl);
2580 : 25868 : tree oldtype = TREE_TYPE (olddecl);
2581 : :
2582 : 25868 : if (newtype != error_mark_node && oldtype != error_mark_node
2583 : 51736 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2584 : 66405 : CLASSTYPE_FRIEND_CLASSES (newtype)
2585 : 22135 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2586 : :
2587 : 25868 : 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 : 18996174 : if (merge_attr)
2593 : 18428952 : DECL_ATTRIBUTES (newdecl)
2594 : 36857904 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2595 : : else
2596 : 567222 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2597 : :
2598 : 18996174 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2599 : : {
2600 : 3569453 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2601 : 3569453 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2602 : 3569453 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2603 : :
2604 : : /* The new decl should not already have gathered any
2605 : : specializations. */
2606 : 3569453 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2607 : :
2608 : : /* Make sure the contracts are equivalent. */
2609 : 3569453 : duplicate_contracts (newdecl, olddecl);
2610 : :
2611 : : /* Remove contracts from old_result so they aren't appended to
2612 : : old_result by the merge function. */
2613 : 3569453 : remove_contract_attributes (old_result);
2614 : :
2615 : 3569453 : DECL_ATTRIBUTES (old_result)
2616 : 3569453 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2617 : :
2618 : 3569453 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2619 : : {
2620 : 3569423 : if (DECL_SOURCE_LOCATION (newdecl)
2621 : 3569423 : != DECL_SOURCE_LOCATION (olddecl))
2622 : : {
2623 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2624 : : later declarations of a function template. */
2625 : 3546602 : check_redeclaration_no_default_args (newdecl);
2626 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2627 : : argument expression, that declaration... shall be the only
2628 : : declaration of the function or function template in the
2629 : : translation unit." */
2630 : 3546602 : check_no_redeclaration_friend_default_args
2631 : 3546602 : (old_result, new_result);
2632 : :
2633 : 3546602 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2634 : 3546602 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2635 : 3546602 : merge_default_template_args (new_parms, old_parms,
2636 : : /*class_p=*/false);
2637 : : }
2638 : 3569423 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2639 : 2914748 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2640 : :
2641 : 3569423 : check_default_args (newdecl);
2642 : :
2643 : 7138834 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2644 : 3569423 : && DECL_INITIAL (new_result))
2645 : : {
2646 : 24 : if (DECL_INITIAL (old_result))
2647 : 12 : DECL_UNINLINABLE (old_result) = 1;
2648 : : else
2649 : 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2650 : 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2651 : 72 : DECL_NOT_REALLY_EXTERN (old_result)
2652 : 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2653 : 48 : DECL_INTERFACE_KNOWN (old_result)
2654 : 24 : = DECL_INTERFACE_KNOWN (new_result);
2655 : 24 : DECL_DECLARED_INLINE_P (old_result)
2656 : 24 : = DECL_DECLARED_INLINE_P (new_result);
2657 : 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2658 : 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2659 : : }
2660 : : else
2661 : : {
2662 : 3569399 : DECL_DECLARED_INLINE_P (old_result)
2663 : 3569399 : |= DECL_DECLARED_INLINE_P (new_result);
2664 : 3569399 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2665 : 3569399 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2666 : 3569399 : check_redeclaration_exception_specification (newdecl, olddecl);
2667 : :
2668 : 3569399 : merge_attribute_bits (new_result, old_result);
2669 : : }
2670 : : }
2671 : :
2672 : : /* Propagate purviewness and importingness as with
2673 : : set_instantiating_module, unless newdecl is a friend injection. */
2674 : 3569453 : if (modules_p () && DECL_LANG_SPECIFIC (new_result)
2675 : 3581035 : && !(TREE_CODE (new_result) == FUNCTION_DECL
2676 : 11576 : && DECL_UNIQUE_FRIEND_P (new_result)))
2677 : : {
2678 : 9756 : if (DECL_MODULE_PURVIEW_P (new_result))
2679 : 4755 : DECL_MODULE_PURVIEW_P (old_result) = true;
2680 : 9756 : if (!DECL_MODULE_IMPORT_P (new_result))
2681 : 9756 : DECL_MODULE_IMPORT_P (old_result) = false;
2682 : : }
2683 : :
2684 : : /* If the new declaration is a definition, update the file and
2685 : : line information on the declaration, and also make
2686 : : the old declaration the same definition. */
2687 : 3569453 : if (DECL_INITIAL (new_result) != NULL_TREE)
2688 : : {
2689 : 5408122 : DECL_SOURCE_LOCATION (olddecl)
2690 : 5408122 : = DECL_SOURCE_LOCATION (old_result)
2691 : 2704061 : = DECL_SOURCE_LOCATION (newdecl);
2692 : 2704061 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2693 : 2704061 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2694 : : {
2695 : 2704061 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2696 : 9663421 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2697 : 6959360 : DECL_CONTEXT (p) = old_result;
2698 : :
2699 : 5408122 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2700 : 14 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2701 : : }
2702 : : }
2703 : :
2704 : 3569453 : return olddecl;
2705 : : }
2706 : :
2707 : 15426721 : if (types_match)
2708 : : {
2709 : 15426520 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2710 : 15009535 : check_redeclaration_exception_specification (newdecl, olddecl);
2711 : :
2712 : : /* Automatically handles default parameters. */
2713 : 15426520 : tree oldtype = TREE_TYPE (olddecl);
2714 : 15426520 : tree newtype;
2715 : :
2716 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2717 : : at newdecl, which will be ggc_freed. */
2718 : 15426520 : if (TREE_CODE (newdecl) == TYPE_DECL)
2719 : : {
2720 : : /* But NEWTYPE might have an attribute, honor that. */
2721 : 25868 : tree tem = TREE_TYPE (newdecl);
2722 : 25868 : newtype = oldtype;
2723 : :
2724 : 25868 : if (TYPE_USER_ALIGN (tem))
2725 : : {
2726 : 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2727 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2728 : 21 : TYPE_USER_ALIGN (newtype) = true;
2729 : : }
2730 : :
2731 : : /* And remove the new type from the variants list. */
2732 : 25868 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2733 : : {
2734 : 24 : tree remove = TREE_TYPE (newdecl);
2735 : 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2736 : : {
2737 : 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2738 : : /* If remove is the main variant, no need to remove that
2739 : : from the list. One of the DECL_ORIGINAL_TYPE
2740 : : variants, e.g. created for aligned attribute, might still
2741 : : refer to the newdecl TYPE_DECL though, so remove that one
2742 : : in that case. */
2743 : 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2744 : 6 : if (orig != remove)
2745 : 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2746 : 0 : t = TYPE_MAIN_VARIANT (t))
2747 : 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2748 : : {
2749 : 12 : TYPE_NEXT_VARIANT (t)
2750 : 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2751 : 6 : break;
2752 : : }
2753 : : }
2754 : : else
2755 : 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2756 : 0 : t = TYPE_NEXT_VARIANT (t))
2757 : 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2758 : : {
2759 : 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2760 : 15 : break;
2761 : : }
2762 : : }
2763 : : }
2764 : 15400652 : else if (merge_attr)
2765 : 14833430 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2766 : : else
2767 : 567222 : newtype = TREE_TYPE (newdecl);
2768 : :
2769 : 15426520 : if (VAR_P (newdecl))
2770 : : {
2771 : 391117 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2772 : : /* For already initialized vars, TREE_READONLY could have been
2773 : : cleared in cp_finish_decl, because the var needs runtime
2774 : : initialization or destruction. Make sure not to set
2775 : : TREE_READONLY on it again. */
2776 : 391117 : if (DECL_INITIALIZED_P (olddecl)
2777 : 1213 : && !DECL_EXTERNAL (olddecl)
2778 : 391202 : && !TREE_READONLY (olddecl))
2779 : 30 : TREE_READONLY (newdecl) = 0;
2780 : 391117 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2781 : 782234 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2782 : 391117 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2783 : 391117 : if (DECL_DEPENDENT_INIT_P (olddecl))
2784 : 104177 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2785 : 782234 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2786 : 391117 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2787 : 391117 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2788 : 391117 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2789 : 782234 : DECL_DECLARED_CONSTINIT_P (newdecl)
2790 : 391117 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2791 : :
2792 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2793 : 391117 : if (DECL_LANG_SPECIFIC (olddecl)
2794 : 778655 : && CP_DECL_THREADPRIVATE_P (olddecl))
2795 : : {
2796 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2797 : 41 : retrofit_lang_decl (newdecl);
2798 : 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2799 : : }
2800 : : }
2801 : :
2802 : : /* An explicit specialization of a function template or of a member
2803 : : function of a class template can be declared transaction_safe
2804 : : independently of whether the corresponding template entity is declared
2805 : : transaction_safe. */
2806 : 1145 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2807 : 1043 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2808 : 57 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2809 : 51 : && tx_safe_fn_type_p (newtype)
2810 : 15426520 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2811 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2812 : :
2813 : 15426520 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2814 : :
2815 : 15426520 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2816 : 15009535 : check_default_args (newdecl);
2817 : :
2818 : : /* Lay the type out, unless already done. */
2819 : 15426520 : if (! same_type_p (newtype, oldtype)
2820 : 366 : && TREE_TYPE (newdecl) != error_mark_node
2821 : 15426886 : && !(processing_template_decl && uses_template_parms (newdecl)))
2822 : 285 : layout_type (TREE_TYPE (newdecl));
2823 : :
2824 : 15426520 : if ((VAR_P (newdecl)
2825 : 15035403 : || TREE_CODE (newdecl) == PARM_DECL
2826 : 15035403 : || TREE_CODE (newdecl) == RESULT_DECL
2827 : 15035403 : || TREE_CODE (newdecl) == FIELD_DECL
2828 : 15035403 : || TREE_CODE (newdecl) == TYPE_DECL)
2829 : 15452388 : && !(processing_template_decl && uses_template_parms (newdecl)))
2830 : 256756 : layout_decl (newdecl, 0);
2831 : :
2832 : : /* Merge deprecatedness. */
2833 : 15426520 : if (TREE_DEPRECATED (newdecl))
2834 : 11809 : TREE_DEPRECATED (olddecl) = 1;
2835 : :
2836 : : /* Merge unavailability. */
2837 : 15426520 : if (TREE_UNAVAILABLE (newdecl))
2838 : 3 : TREE_UNAVAILABLE (olddecl) = 1;
2839 : :
2840 : : /* Preserve function specific target and optimization options */
2841 : 15426520 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2842 : : {
2843 : 15009535 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2844 : 15009535 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2845 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2846 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2847 : :
2848 : 15009535 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2849 : 15009535 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2850 : 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2851 : 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2852 : :
2853 : 15009535 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2854 : 14836124 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2855 : : }
2856 : : else
2857 : : {
2858 : : /* Merge the const type qualifier. */
2859 : 416985 : if (TREE_READONLY (newdecl))
2860 : 43136 : TREE_READONLY (olddecl) = 1;
2861 : : /* Merge the volatile type qualifier. */
2862 : 416985 : if (TREE_THIS_VOLATILE (newdecl))
2863 : 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2864 : : }
2865 : :
2866 : : /* Merge the initialization information. */
2867 : 15426520 : if (DECL_INITIAL (newdecl) == NULL_TREE
2868 : 15426520 : && DECL_INITIAL (olddecl) != NULL_TREE)
2869 : : {
2870 : 174111 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2871 : 174111 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2872 : 174111 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2873 : : {
2874 : 11679 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2875 : 11679 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2876 : : }
2877 : : }
2878 : :
2879 : 15426520 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2880 : : {
2881 : 15009535 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2882 : 15009535 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2883 : 15009535 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2884 : 15009535 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2885 : 52912 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2886 : 15009535 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2887 : 15009535 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2888 : 15009535 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2889 : 15009535 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2890 : :
2891 : 15009535 : if (merge_attr)
2892 : 14442313 : merge_attribute_bits (newdecl, olddecl);
2893 : : else
2894 : : {
2895 : : /* Merge the noreturn bit. */
2896 : 567222 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2897 : 567222 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2898 : 567222 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2899 : 567222 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2900 : 567222 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2901 : : }
2902 : : /* Keep the old RTL. */
2903 : 15009535 : COPY_DECL_RTL (olddecl, newdecl);
2904 : : }
2905 : 416985 : else if (VAR_P (newdecl)
2906 : 416985 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2907 : : {
2908 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2909 : : declaration was for an incomplete object and the new
2910 : : declaration is not since many attributes of the RTL will
2911 : : change. */
2912 : 391043 : COPY_DECL_RTL (olddecl, newdecl);
2913 : : }
2914 : : }
2915 : : /* If cannot merge, then use the new type and qualifiers,
2916 : : and don't preserve the old rtl. */
2917 : : else
2918 : : {
2919 : : /* Clean out any memory we had of the old declaration. */
2920 : 201 : tree oldstatic = value_member (olddecl, static_aggregates);
2921 : 201 : if (oldstatic)
2922 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2923 : :
2924 : 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2925 : 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2926 : 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2927 : 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2928 : 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2929 : : }
2930 : :
2931 : : /* Merge the storage class information. */
2932 : 15426721 : merge_weak (newdecl, olddecl);
2933 : :
2934 : 15426721 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2935 : 15426721 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2936 : 15426721 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2937 : 15426721 : if (! DECL_EXTERNAL (olddecl))
2938 : 30494 : DECL_EXTERNAL (newdecl) = 0;
2939 : 15426721 : if (! DECL_COMDAT (olddecl))
2940 : 6706599 : DECL_COMDAT (newdecl) = 0;
2941 : :
2942 : 15426721 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2943 : : {
2944 : 78 : if (!DECL_LOCAL_DECL_P (olddecl))
2945 : : /* This can happen if olddecl was brought in from the
2946 : : enclosing namespace via a using-decl. The new decl is
2947 : : then not a block-scope extern at all. */
2948 : 3 : DECL_LOCAL_DECL_P (newdecl) = false;
2949 : : else
2950 : : {
2951 : 75 : retrofit_lang_decl (newdecl);
2952 : 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2953 : 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2954 : 75 : if (alias != error_mark_node)
2955 : : {
2956 : 72 : DECL_ATTRIBUTES (alias)
2957 : 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2958 : 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2959 : 42 : merge_attribute_bits (newdecl, alias);
2960 : : }
2961 : : }
2962 : : }
2963 : :
2964 : 15426721 : new_template_info = NULL_TREE;
2965 : 15426721 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2966 : : {
2967 : 15397283 : bool new_redefines_gnu_inline = false;
2968 : :
2969 : 15397283 : if (new_defines_function
2970 : 15397283 : && ((DECL_INTERFACE_KNOWN (olddecl)
2971 : 1242 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2972 : 9011117 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2973 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2974 : : == FUNCTION_DECL))))
2975 : 2484 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2976 : :
2977 : : if (!new_redefines_gnu_inline)
2978 : : {
2979 : 15397205 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2980 : 15397205 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2981 : 15397205 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2982 : : }
2983 : :
2984 : 15397283 : if (TREE_CODE (newdecl) != TYPE_DECL)
2985 : : {
2986 : 15397231 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2987 : 15397231 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2988 : 15397231 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2989 : :
2990 : : /* If the OLDDECL is an instantiation and/or specialization,
2991 : : then the NEWDECL must be too. But, it may not yet be marked
2992 : : as such if the caller has created NEWDECL, but has not yet
2993 : : figured out that it is a redeclaration. */
2994 : 15397231 : if (!DECL_USE_TEMPLATE (newdecl))
2995 : 14807001 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2996 : :
2997 : 15397231 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2998 : 14829785 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2999 : 29659570 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3000 : : }
3001 : :
3002 : : /* Don't really know how much of the language-specific
3003 : : values we should copy from old to new. */
3004 : 15397283 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3005 : :
3006 : 15397283 : if (LANG_DECL_HAS_MIN (newdecl))
3007 : : {
3008 : 15397283 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3009 : 15397283 : if (DECL_TEMPLATE_INFO (newdecl))
3010 : : {
3011 : 601707 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
3012 : 601707 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3013 : 601707 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
3014 : : /* Remember the presence of explicit specialization args. */
3015 : 1134450 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
3016 : 1134450 : = TINFO_USED_TEMPLATE_ID (new_template_info);
3017 : : }
3018 : :
3019 : : /* We don't want to copy template info from a non-templated friend
3020 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
3021 : 15397283 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
3022 : : || !non_templated_friend_p (olddecl));
3023 : 15397283 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3024 : : }
3025 : :
3026 : 15397283 : if (DECL_DECLARES_FUNCTION_P (newdecl))
3027 : : {
3028 : : /* Only functions have these fields. */
3029 : 15009736 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3030 : 15009736 : DECL_BEFRIENDING_CLASSES (newdecl)
3031 : 15009736 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3032 : 15009736 : DECL_BEFRIENDING_CLASSES (olddecl));
3033 : : /* DECL_THUNKS is only valid for virtual functions,
3034 : : otherwise it is a DECL_FRIEND_CONTEXT. */
3035 : 15009736 : if (DECL_VIRTUAL_P (newdecl))
3036 : 649598 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
3037 : 28720276 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
3038 : 104768 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
3039 : : }
3040 : 387547 : else if (VAR_P (newdecl))
3041 : : {
3042 : : /* Only variables have this field. */
3043 : 387495 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
3044 : 194 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
3045 : : }
3046 : : }
3047 : :
3048 : 15426721 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3049 : : {
3050 : 15009736 : tree parm;
3051 : :
3052 : : /* Merge parameter attributes. */
3053 : 15009736 : tree oldarg, newarg;
3054 : 15009736 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
3055 : 42008867 : oldarg && newarg;
3056 : 26999131 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
3057 : : {
3058 : 26999131 : DECL_ATTRIBUTES (newarg)
3059 : 26999131 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
3060 : 26999131 : if (lookup_attribute (NULL, "indeterminate",
3061 : 26999131 : DECL_ATTRIBUTES (newarg))
3062 : 26999182 : && !lookup_attribute (NULL, "indeterminate",
3063 : 51 : DECL_ATTRIBUTES (oldarg)))
3064 : : {
3065 : 12 : auto_diagnostic_group d;
3066 : 12 : error_at (DECL_SOURCE_LOCATION (newarg),
3067 : : "%<indeterminate%> attribute not specified "
3068 : : "for parameter %qD on the first declaration of "
3069 : : "its function", newarg);
3070 : 12 : inform (DECL_SOURCE_LOCATION (oldarg),
3071 : : "earlier declaration");
3072 : 12 : }
3073 : 26999131 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
3074 : : }
3075 : :
3076 : 15009736 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3077 : 15009736 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3078 : : {
3079 : : /* If newdecl is not a specialization, then it is not a
3080 : : template-related function at all. And that means that we
3081 : : should have exited above, returning 0. */
3082 : 567222 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
3083 : :
3084 : 567222 : if (DECL_ODR_USED (olddecl))
3085 : : /* From [temp.expl.spec]:
3086 : :
3087 : : If a template, a member template or the member of a class
3088 : : template is explicitly specialized then that
3089 : : specialization shall be declared before the first use of
3090 : : that specialization that would cause an implicit
3091 : : instantiation to take place, in every translation unit in
3092 : : which such a use occurs. */
3093 : 0 : error ("explicit specialization of %qD after first use",
3094 : : olddecl);
3095 : :
3096 : 567222 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3097 : 567222 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
3098 : 1134291 : && DECL_DECLARED_INLINE_P (newdecl));
3099 : :
3100 : : /* Don't propagate visibility from the template to the
3101 : : specialization here. We'll do that in determine_visibility if
3102 : : appropriate. */
3103 : 567222 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
3104 : :
3105 : : /* [temp.expl.spec/14] We don't inline explicit specialization
3106 : : just because the primary template says so. */
3107 : 567222 : gcc_assert (!merge_attr);
3108 : :
3109 : 567222 : DECL_DECLARED_INLINE_P (olddecl)
3110 : 567222 : = DECL_DECLARED_INLINE_P (newdecl);
3111 : :
3112 : 567222 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
3113 : 567222 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
3114 : :
3115 : 567222 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
3116 : : }
3117 : 14442514 : else if (new_defines_function && DECL_INITIAL (olddecl))
3118 : : {
3119 : : /* Never inline re-defined extern inline functions.
3120 : : FIXME: this could be better handled by keeping both
3121 : : function as separate declarations. */
3122 : 78 : DECL_UNINLINABLE (newdecl) = 1;
3123 : : }
3124 : : else
3125 : : {
3126 : 14442436 : if (DECL_PENDING_INLINE_P (olddecl))
3127 : : {
3128 : 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
3129 : 6 : DECL_PENDING_INLINE_INFO (newdecl)
3130 : 12 : = DECL_PENDING_INLINE_INFO (olddecl);
3131 : : }
3132 : 14442430 : else if (DECL_PENDING_INLINE_P (newdecl))
3133 : : ;
3134 : 14442430 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
3135 : 14442430 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
3136 : 28884860 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
3137 : :
3138 : 14442436 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3139 : :
3140 : 43327308 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3141 : 28884872 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3142 : :
3143 : 28884872 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3144 : 14442436 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3145 : 14442436 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3146 : 28488730 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3147 : : }
3148 : :
3149 : : /* Preserve abstractness on cloned [cd]tors. */
3150 : 15009736 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3151 : :
3152 : : /* Update newdecl's parms to point at olddecl. */
3153 : 47943688 : for (parm = DECL_ARGUMENTS (newdecl); parm;
3154 : 32933952 : parm = DECL_CHAIN (parm))
3155 : 32933952 : DECL_CONTEXT (parm) = olddecl;
3156 : :
3157 : 15009736 : if (! types_match)
3158 : : {
3159 : 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3160 : 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3161 : 201 : COPY_DECL_RTL (newdecl, olddecl);
3162 : : }
3163 : 15009736 : if (! types_match || new_defines_function)
3164 : : {
3165 : : /* These are the final DECL_ARGUMENTS that will be used within the
3166 : : body; update any references to old DECL_ARGUMENTS in the
3167 : : contracts, if present. */
3168 : 9012560 : if (tree contracts = DECL_CONTRACTS (newdecl))
3169 : 132 : remap_contracts (olddecl, newdecl, contracts, true);
3170 : :
3171 : : /* These need to be copied so that the names are available.
3172 : : Note that if the types do match, we'll preserve inline
3173 : : info and other bits, but if not, we won't. */
3174 : 9012560 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3175 : 9012560 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3176 : :
3177 : : /* In some cases, duplicate_contracts will remove contracts from
3178 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
3179 : : shared. If we removed them, re-add them. */
3180 : 9012560 : if (!DECL_CONTRACTS (olddecl))
3181 : 9012513 : copy_contract_attributes (olddecl, newdecl);
3182 : : }
3183 : : /* If redeclaring a builtin function, it stays built in
3184 : : if newdecl is a gnu_inline definition, or if newdecl is just
3185 : : a declaration. */
3186 : 15009736 : if (fndecl_built_in_p (olddecl)
3187 : 15067667 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3188 : : {
3189 : 4108286 : copy_decl_built_in_function (newdecl, olddecl);
3190 : : /* If we're keeping the built-in definition, keep the rtl,
3191 : : regardless of declaration matches. */
3192 : 4108286 : COPY_DECL_RTL (olddecl, newdecl);
3193 : 4108286 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3194 : : {
3195 : 4108280 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3196 : 4108280 : if (builtin_decl_explicit_p (fncode))
3197 : : {
3198 : : /* A compatible prototype of these builtin functions
3199 : : is seen, assume the runtime implements it with
3200 : : the expected semantics. */
3201 : 4108280 : switch (fncode)
3202 : : {
3203 : 10446 : case BUILT_IN_STPCPY:
3204 : 10446 : set_builtin_decl_implicit_p (fncode, true);
3205 : 10446 : break;
3206 : 4097834 : default:
3207 : 4097834 : set_builtin_decl_declared_p (fncode, true);
3208 : 4097834 : break;
3209 : : }
3210 : : }
3211 : :
3212 : 4108280 : copy_attributes_to_builtin (newdecl);
3213 : : }
3214 : : }
3215 : 15009736 : if (new_defines_function)
3216 : : /* If defining a function declared with other language
3217 : : linkage, use the previously declared language linkage. */
3218 : 9012359 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3219 : 5997377 : else if (types_match)
3220 : : {
3221 : 5997176 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3222 : : /* Don't clear out the arguments if we're just redeclaring a
3223 : : function. */
3224 : 5997176 : if (DECL_ARGUMENTS (olddecl))
3225 : : {
3226 : : /* If we removed contracts from previous definition, re-attach
3227 : : them. Otherwise, rewrite the contracts so they match the
3228 : : parameters of the new declaration. */
3229 : 1914606 : if (DECL_INITIAL (olddecl)
3230 : 11572 : && DECL_CONTRACTS (newdecl)
3231 : 1914617 : && !DECL_CONTRACTS (olddecl))
3232 : 10 : copy_contract_attributes (olddecl, newdecl);
3233 : : else
3234 : : {
3235 : : /* Temporarily undo the re-contexting of parameters so we can
3236 : : actually remap parameters. The inliner won't replace
3237 : : parameters if we don't do this. */
3238 : 1914596 : tree args = DECL_ARGUMENTS (newdecl);
3239 : 6153885 : for (tree p = args; p; p = DECL_CHAIN (p))
3240 : 4239289 : DECL_CONTEXT (p) = newdecl;
3241 : :
3242 : : /* Save new argument names for use in contracts parsing,
3243 : : unless we've already started parsing the body of olddecl
3244 : : (particular issues arise when newdecl is from a prior
3245 : : friend decl with no argument names, see
3246 : : modules/contracts-tpl-friend-1). */
3247 : 1914596 : if (tree contracts = DECL_CONTRACTS (olddecl))
3248 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3249 : :
3250 : : /* And reverse this operation again. */
3251 : 6153885 : for (tree p = args; p; p = DECL_CHAIN (p))
3252 : 4239289 : DECL_CONTEXT (p) = olddecl;
3253 : : }
3254 : :
3255 : 1914606 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3256 : : }
3257 : : }
3258 : : }
3259 : 416985 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3260 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3261 : :
3262 : : /* Now preserve various other info from the definition. */
3263 : 15426721 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3264 : 15426721 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3265 : 15426721 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3266 : 15426721 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3267 : :
3268 : : /* Warn about conflicting visibility specifications. */
3269 : 15426721 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3270 : 5420229 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3271 : 15427246 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3272 : : {
3273 : 12 : auto_diagnostic_group d;
3274 : 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3275 : : "%qD: visibility attribute ignored because it "
3276 : : "conflicts with previous declaration", newdecl))
3277 : 12 : inform (olddecl_loc,
3278 : : "previous declaration of %qD", olddecl);
3279 : 12 : }
3280 : : /* Choose the declaration which specified visibility. */
3281 : 15426721 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3282 : : {
3283 : 5420229 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3284 : 5420229 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3285 : : }
3286 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3287 : : so keep this behavior. */
3288 : 15426721 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3289 : : {
3290 : 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3291 : 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3292 : : }
3293 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3294 : 15426721 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3295 : : {
3296 : 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3297 : 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3298 : : }
3299 : 15426667 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3300 : 15426667 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3301 : 3 : DECL_USER_ALIGN (newdecl) = 1;
3302 : :
3303 : 15426721 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3304 : 30853442 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3305 : 15426721 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3306 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3307 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3308 : 15426721 : if (TREE_CODE (newdecl) == FIELD_DECL)
3309 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3310 : :
3311 : : /* Merge module entity mapping information. */
3312 : 15426721 : if (DECL_LANG_SPECIFIC (olddecl)
3313 : 30824047 : && (DECL_MODULE_ENTITY_P (olddecl)
3314 : 15397044 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3315 : : {
3316 : 282 : retrofit_lang_decl (newdecl);
3317 : 282 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3318 : 282 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3319 : : }
3320 : :
3321 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3322 : : with that from NEWDECL below. */
3323 : 15426721 : if (DECL_LANG_SPECIFIC (olddecl))
3324 : : {
3325 : 15397326 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3326 : : != DECL_LANG_SPECIFIC (newdecl));
3327 : 15397326 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3328 : : }
3329 : :
3330 : : /* Merge the USED information. */
3331 : 15426721 : if (TREE_USED (olddecl))
3332 : 487112 : TREE_USED (newdecl) = 1;
3333 : 14939609 : else if (TREE_USED (newdecl))
3334 : 54 : TREE_USED (olddecl) = 1;
3335 : :
3336 : 15426721 : if (VAR_P (newdecl))
3337 : : {
3338 : 391117 : if (DECL_READ_P (olddecl))
3339 : 35021 : DECL_READ_P (newdecl) = 1;
3340 : 356096 : else if (DECL_READ_P (newdecl))
3341 : 3 : DECL_READ_P (olddecl) = 1;
3342 : : }
3343 : :
3344 : 15426721 : if (DECL_PRESERVE_P (olddecl))
3345 : 48 : DECL_PRESERVE_P (newdecl) = 1;
3346 : 15426673 : else if (DECL_PRESERVE_P (newdecl))
3347 : 63 : DECL_PRESERVE_P (olddecl) = 1;
3348 : :
3349 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3350 : : to olddecl and deleted. */
3351 : 15426721 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3352 : 30436457 : && DECL_FUNCTION_VERSIONED (olddecl))
3353 : : {
3354 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3355 : 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3356 : : /* newdecl will be purged after copying to olddecl and is no longer
3357 : : a version. */
3358 : 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3359 : : }
3360 : :
3361 : 15426721 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3362 : : {
3363 : 15009736 : int function_size;
3364 : 15009736 : struct symtab_node *snode = symtab_node::get (olddecl);
3365 : :
3366 : 15009736 : function_size = sizeof (struct tree_decl_common);
3367 : :
3368 : 15009736 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3369 : : (char *) newdecl + sizeof (struct tree_common),
3370 : : function_size - sizeof (struct tree_common));
3371 : :
3372 : 15009736 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3373 : : (char *) newdecl + sizeof (struct tree_decl_common),
3374 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3375 : :
3376 : : /* Preserve symtab node mapping. */
3377 : 15009736 : olddecl->decl_with_vis.symtab_node = snode;
3378 : :
3379 : 15009736 : if (new_template_info)
3380 : : /* If newdecl is a template instantiation, it is possible that
3381 : : the following sequence of events has occurred:
3382 : :
3383 : : o A friend function was declared in a class template. The
3384 : : class template was instantiated.
3385 : :
3386 : : o The instantiation of the friend declaration was
3387 : : recorded on the instantiation list, and is newdecl.
3388 : :
3389 : : o Later, however, instantiate_class_template called pushdecl
3390 : : on the newdecl to perform name injection. But, pushdecl in
3391 : : turn called duplicate_decls when it discovered that another
3392 : : declaration of a global function with the same name already
3393 : : existed.
3394 : :
3395 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3396 : :
3397 : : If we're going to do that, we'd better make sure that
3398 : : olddecl, and not newdecl, is on the list of
3399 : : instantiations so that if we try to do the instantiation
3400 : : again we won't get the clobbered declaration. */
3401 : 601698 : reregister_specialization (newdecl,
3402 : : new_template_info,
3403 : : olddecl);
3404 : : }
3405 : : else
3406 : : {
3407 : 416985 : size_t size = tree_code_size (TREE_CODE (newdecl));
3408 : :
3409 : 416985 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3410 : : (char *) newdecl + sizeof (struct tree_common),
3411 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3412 : :
3413 : 416985 : switch (TREE_CODE (newdecl))
3414 : : {
3415 : 416985 : case LABEL_DECL:
3416 : 416985 : case VAR_DECL:
3417 : 416985 : case RESULT_DECL:
3418 : 416985 : case PARM_DECL:
3419 : 416985 : case FIELD_DECL:
3420 : 416985 : case TYPE_DECL:
3421 : 416985 : case CONST_DECL:
3422 : 416985 : {
3423 : 416985 : struct symtab_node *snode = NULL;
3424 : :
3425 : 416985 : if (VAR_P (newdecl)
3426 : 416985 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3427 : 3 : || DECL_EXTERNAL (olddecl)))
3428 : 391117 : snode = symtab_node::get (olddecl);
3429 : 416985 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3430 : : (char *) newdecl + sizeof (struct tree_decl_common),
3431 : : size - sizeof (struct tree_decl_common)
3432 : 416985 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3433 : 416985 : if (VAR_P (newdecl))
3434 : 391117 : olddecl->decl_with_vis.symtab_node = snode;
3435 : : }
3436 : : break;
3437 : 0 : default:
3438 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3439 : : (char *) newdecl + sizeof (struct tree_decl_common),
3440 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3441 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3442 : 0 : break;
3443 : : }
3444 : : }
3445 : :
3446 : 15426721 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3447 : : {
3448 : 15400853 : if (DECL_EXTERNAL (olddecl)
3449 : 392483 : || TREE_PUBLIC (olddecl)
3450 : 15400922 : || TREE_STATIC (olddecl))
3451 : : {
3452 : : /* Merge the section attribute.
3453 : : We want to issue an error if the sections conflict but that must be
3454 : : done later in decl_attributes since we are called before attributes
3455 : : are assigned. */
3456 : 15400853 : if (DECL_SECTION_NAME (newdecl) != NULL)
3457 : 7 : set_decl_section_name (olddecl, newdecl);
3458 : :
3459 : 15400853 : if (DECL_ONE_ONLY (newdecl))
3460 : : {
3461 : 0 : struct symtab_node *oldsym, *newsym;
3462 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3463 : 0 : oldsym = cgraph_node::get_create (olddecl);
3464 : : else
3465 : 0 : oldsym = varpool_node::get_create (olddecl);
3466 : 0 : newsym = symtab_node::get (newdecl);
3467 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3468 : : }
3469 : : }
3470 : :
3471 : 15400853 : if (VAR_P (newdecl)
3472 : 15400853 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3473 : : {
3474 : 404 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3475 : 404 : if (!processing_template_decl)
3476 : 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3477 : : }
3478 : : }
3479 : :
3480 : 15426721 : DECL_UID (olddecl) = olddecl_uid;
3481 : :
3482 : : /* NEWDECL contains the merged attribute lists.
3483 : : Update OLDDECL to be the same. */
3484 : 15426721 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3485 : :
3486 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3487 : : so that encode_section_info has a chance to look at the new decl
3488 : : flags and attributes. */
3489 : 15426721 : if (DECL_RTL_SET_P (olddecl)
3490 : 15426721 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3491 : 0 : || (VAR_P (olddecl)
3492 : 0 : && TREE_STATIC (olddecl))))
3493 : 0 : make_decl_rtl (olddecl);
3494 : :
3495 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3496 : : declaration of a member results in a call to duplicate_decls,
3497 : : freeing these nodes represents in a significant savings.
3498 : :
3499 : : Before releasing the node, be sore to remove function from symbol
3500 : : table that might have been inserted there to record comdat group.
3501 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3502 : : structure is shared in between newdecl and oldecl. */
3503 : 15426721 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3504 : 15009736 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3505 : 15426721 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3506 : : {
3507 : 15400853 : struct symtab_node *snode = symtab_node::get (newdecl);
3508 : 15400853 : if (snode)
3509 : 399 : snode->remove ();
3510 : : }
3511 : :
3512 : 15426721 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3513 : : {
3514 : 15009736 : tree clone;
3515 : 15523176 : FOR_EACH_CLONE (clone, olddecl)
3516 : : {
3517 : 513440 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3518 : 513440 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3519 : : }
3520 : : }
3521 : :
3522 : : /* Remove the associated constraints for newdecl, if any, before
3523 : : reclaiming memory. */
3524 : 15426721 : if (flag_concepts)
3525 : 3878121 : remove_constraints (newdecl);
3526 : :
3527 : : /* And similarly for any module tracking data. */
3528 : 15426721 : if (modules_p ())
3529 : 29803 : remove_defining_module (newdecl);
3530 : :
3531 : 15426721 : ggc_free (newdecl);
3532 : :
3533 : 15426721 : return olddecl;
3534 : : }
3535 : :
3536 : : /* Return zero if the declaration NEWDECL is valid
3537 : : when the declaration OLDDECL (assumed to be for the same name)
3538 : : has already been seen.
3539 : : Otherwise return an error message format string with a %s
3540 : : where the identifier should go. */
3541 : :
3542 : : static const char *
3543 : 15018113 : redeclaration_error_message (tree newdecl, tree olddecl)
3544 : : {
3545 : 15018149 : if (TREE_CODE (newdecl) == TYPE_DECL)
3546 : : {
3547 : : /* Because C++ can put things into name space for free,
3548 : : constructs like "typedef struct foo { ... } foo"
3549 : : would look like an erroneous redeclaration. */
3550 : 57049 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3551 : : return NULL;
3552 : : else
3553 : : return G_("redefinition of %q#D");
3554 : : }
3555 : 14961100 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3556 : : {
3557 : : /* If this is a pure function, its olddecl will actually be
3558 : : the original initialization to `0' (which we force to call
3559 : : abort()). Don't complain about redefinition in this case. */
3560 : 22000276 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3561 : 11000156 : && DECL_INITIAL (olddecl) == NULL_TREE)
3562 : : return NULL;
3563 : :
3564 : : /* If both functions come from different namespaces, this is not
3565 : : a redeclaration - this is a conflict with a used function. */
3566 : 22000246 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3567 : 2271428 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3568 : 11000126 : && ! decls_match (olddecl, newdecl))
3569 : : return G_("%qD conflicts with used function");
3570 : :
3571 : : /* We'll complain about linkage mismatches in
3572 : : warn_extern_redeclared_static. */
3573 : :
3574 : : /* Defining the same name twice is no good. */
3575 : 11000123 : if (decl_defined_p (olddecl)
3576 : 11000123 : && decl_defined_p (newdecl))
3577 : : {
3578 : 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3579 : : return G_("%q#D not declared in class");
3580 : 444 : else if (!GNU_INLINE_P (olddecl)
3581 : 390 : || GNU_INLINE_P (newdecl))
3582 : 219 : return G_("redefinition of %q#D");
3583 : : }
3584 : :
3585 : 11727804 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3586 : : {
3587 : 681946 : bool olda = GNU_INLINE_P (olddecl);
3588 : 681946 : bool newa = GNU_INLINE_P (newdecl);
3589 : :
3590 : 681946 : if (olda != newa)
3591 : : {
3592 : 15 : if (newa)
3593 : : return G_("%q+D redeclared inline with "
3594 : : "%<gnu_inline%> attribute");
3595 : : else
3596 : : return G_("%q+D redeclared inline without "
3597 : : "%<gnu_inline%> attribute");
3598 : : }
3599 : : }
3600 : :
3601 : : /* [class.compare.default]: A definition of a comparison operator as
3602 : : defaulted that appears in a class shall be the first declaration of
3603 : : that function. */
3604 : 10999889 : special_function_kind sfk = special_function_p (olddecl);
3605 : 10999889 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3606 : : return G_("comparison operator %q+D defaulted after "
3607 : : "its first declaration");
3608 : :
3609 : 10999886 : check_abi_tag_redeclaration
3610 : 10999886 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3611 : 10999886 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3612 : :
3613 : 10999886 : return NULL;
3614 : : }
3615 : 3960962 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3616 : : {
3617 : 3569492 : tree nt, ot;
3618 : :
3619 : 3569492 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3620 : : return G_("redefinition of %q#D");
3621 : :
3622 : 3569486 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3623 : 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3624 : 36 : DECL_TEMPLATE_RESULT (olddecl));
3625 : :
3626 : 3569450 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3627 : : return NULL;
3628 : :
3629 : 3569438 : nt = DECL_TEMPLATE_RESULT (newdecl);
3630 : 3569438 : if (DECL_TEMPLATE_INFO (nt))
3631 : 538592 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3632 : 3569438 : ot = DECL_TEMPLATE_RESULT (olddecl);
3633 : 3569438 : if (DECL_TEMPLATE_INFO (ot))
3634 : 3569438 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3635 : 6273550 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3636 : 3569477 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3637 : 15 : return G_("redefinition of %q#D");
3638 : :
3639 : 4888718 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3640 : : {
3641 : 841325 : bool olda = GNU_INLINE_P (ot);
3642 : 841325 : bool newa = GNU_INLINE_P (nt);
3643 : :
3644 : 841325 : if (olda != newa)
3645 : : {
3646 : 0 : if (newa)
3647 : : return G_("%q+D redeclared inline with "
3648 : : "%<gnu_inline%> attribute");
3649 : : else
3650 : : return G_("%q+D redeclared inline without "
3651 : : "%<gnu_inline%> attribute");
3652 : : }
3653 : : }
3654 : :
3655 : : /* Core issue #226 (C++11):
3656 : :
3657 : : If a friend function template declaration specifies a
3658 : : default template-argument, that declaration shall be a
3659 : : definition and shall be the only declaration of the
3660 : : function template in the translation unit. */
3661 : 3569423 : if ((cxx_dialect != cxx98)
3662 : 3554241 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3663 : 3784079 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3664 : : /*is_primary=*/true,
3665 : : /*is_partial=*/false,
3666 : : /*is_friend_decl=*/2))
3667 : : return G_("redeclaration of friend %q#D "
3668 : : "may not have default template arguments");
3669 : :
3670 : 3569420 : return NULL;
3671 : : }
3672 : 391470 : else if (VAR_P (newdecl)
3673 : 391386 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3674 : 391386 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3675 : 391517 : && (! DECL_LANG_SPECIFIC (olddecl)
3676 : 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3677 : 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3678 : : {
3679 : : /* Only variables can be thread-local, and all declarations must
3680 : : agree on this property. */
3681 : 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3682 : : return G_("thread-local declaration of %q#D follows "
3683 : : "non-thread-local declaration");
3684 : : else
3685 : 3 : return G_("non-thread-local declaration of %q#D follows "
3686 : : "thread-local declaration");
3687 : : }
3688 : 391464 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3689 : : {
3690 : : /* The objects have been declared at namespace scope. If either
3691 : : is a member of an anonymous union, then this is an invalid
3692 : : redeclaration. For example:
3693 : :
3694 : : int i;
3695 : : union { int i; };
3696 : :
3697 : : is invalid. */
3698 : 391244 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3699 : 782557 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3700 : : return G_("redeclaration of %q#D");
3701 : : /* If at least one declaration is a reference, there is no
3702 : : conflict. For example:
3703 : :
3704 : : int i = 3;
3705 : : extern int i;
3706 : :
3707 : : is valid. */
3708 : 391310 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3709 : : return NULL;
3710 : :
3711 : : /* Static data member declared outside a class definition
3712 : : if the variable is defined within the class with constexpr
3713 : : specifier is declaration rather than definition (and
3714 : : deprecated). */
3715 : 2824 : if (cxx_dialect >= cxx17
3716 : 2749 : && VAR_P (olddecl)
3717 : 2693 : && DECL_CLASS_SCOPE_P (olddecl)
3718 : 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3719 : 5461 : && !DECL_INITIAL (newdecl))
3720 : : {
3721 : 2637 : DECL_EXTERNAL (newdecl) = 1;
3722 : : /* For now, only warn with explicit -Wdeprecated. */
3723 : 2637 : if (OPTION_SET_P (warn_deprecated))
3724 : : {
3725 : 40 : auto_diagnostic_group d;
3726 : 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3727 : : "redundant redeclaration of %<constexpr%> "
3728 : : "static data member %qD", newdecl))
3729 : 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3730 : : "previous declaration of %qD", olddecl);
3731 : 40 : }
3732 : 2637 : return NULL;
3733 : : }
3734 : :
3735 : : /* Reject two definitions. */
3736 : : return G_("redefinition of %q#D");
3737 : : }
3738 : : else
3739 : : {
3740 : : /* Objects declared with block scope: */
3741 : : /* Reject two definitions, and reject a definition
3742 : : together with an external reference. */
3743 : 139 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3744 : : return G_("redeclaration of %q#D");
3745 : : return NULL;
3746 : : }
3747 : : }
3748 : :
3749 : :
3750 : : /* Hash and equality functions for the named_label table. */
3751 : :
3752 : : hashval_t
3753 : 74998 : named_label_hash::hash (const value_type entry)
3754 : : {
3755 : 74998 : return IDENTIFIER_HASH_VALUE (entry->name);
3756 : : }
3757 : :
3758 : : bool
3759 : 72060 : named_label_hash::equal (const value_type entry, compare_type name)
3760 : : {
3761 : 72060 : return name == entry->name;
3762 : : }
3763 : :
3764 : : /* Look for a label named ID in the current function. If one cannot
3765 : : be found, create one. Return the named_label_entry, or NULL on
3766 : : failure. */
3767 : :
3768 : : static named_label_entry *
3769 : 23359 : lookup_label_1 (tree id, bool making_local_p)
3770 : : {
3771 : 23359 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3772 : :
3773 : : /* You can't use labels at global scope. */
3774 : 23359 : if (current_function_decl == NULL_TREE)
3775 : : {
3776 : 3 : error ("label %qE referenced outside of any function", id);
3777 : 3 : return NULL;
3778 : : }
3779 : :
3780 : 23356 : if (!named_labels)
3781 : 4574 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3782 : :
3783 : 23356 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3784 : 23356 : named_label_entry **slot
3785 : 23356 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3786 : 23356 : named_label_entry *old = *slot;
3787 : :
3788 : 23356 : if (old && old->label_decl)
3789 : : {
3790 : 3052 : if (!making_local_p)
3791 : : return old;
3792 : :
3793 : 9 : if (old->binding_level == current_binding_level)
3794 : : {
3795 : 0 : auto_diagnostic_group d;
3796 : 0 : error ("local label %qE conflicts with existing label", id);
3797 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3798 : 0 : return NULL;
3799 : 0 : }
3800 : : }
3801 : :
3802 : : /* We are making a new decl, create or reuse the named_label_entry */
3803 : 20322 : named_label_entry *ent = NULL;
3804 : 21 : if (old && !old->label_decl)
3805 : : ent = old;
3806 : : else
3807 : : {
3808 : 20301 : ent = ggc_cleared_alloc<named_label_entry> ();
3809 : 20301 : ent->name = id;
3810 : 20301 : ent->outer = old;
3811 : 20301 : *slot = ent;
3812 : : }
3813 : :
3814 : : /* Now create the LABEL_DECL. */
3815 : 20313 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3816 : :
3817 : 20313 : DECL_CONTEXT (decl) = current_function_decl;
3818 : 20313 : SET_DECL_MODE (decl, VOIDmode);
3819 : 20313 : if (making_local_p)
3820 : : {
3821 : 207 : C_DECLARED_LABEL_FLAG (decl) = true;
3822 : 207 : DECL_CHAIN (decl) = current_binding_level->names;
3823 : 207 : current_binding_level->names = decl;
3824 : : }
3825 : :
3826 : 20313 : ent->label_decl = decl;
3827 : :
3828 : 20313 : return ent;
3829 : 23359 : }
3830 : :
3831 : : /* Wrapper for lookup_label_1. */
3832 : :
3833 : : tree
3834 : 2585 : lookup_label (tree id)
3835 : : {
3836 : 2585 : named_label_entry *ent = lookup_label_1 (id, false);
3837 : 2585 : return ent ? ent->label_decl : NULL_TREE;
3838 : : }
3839 : :
3840 : : /* Remember that we've seen &&ID. */
3841 : :
3842 : : void
3843 : 250 : mark_label_addressed (tree id)
3844 : : {
3845 : 250 : named_label_entry *ent = lookup_label_1 (id, false);
3846 : 250 : ent->addressed = true;
3847 : 250 : }
3848 : :
3849 : : tree
3850 : 207 : declare_local_label (tree id)
3851 : : {
3852 : 207 : named_label_entry *ent = lookup_label_1 (id, true);
3853 : 207 : return ent ? ent->label_decl : NULL_TREE;
3854 : : }
3855 : :
3856 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3857 : :
3858 : : static bool
3859 : 143508 : decl_jump_unsafe (tree decl)
3860 : : {
3861 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3862 : : with automatic storage duration is not in scope to a point where it is
3863 : : in scope is ill-formed unless the variable has scalar type, class type
3864 : : with a trivial default constructor and a trivial destructor, a
3865 : : cv-qualified version of one of these types, or an array of one of the
3866 : : preceding types and is declared without an initializer (8.5). */
3867 : 143508 : tree type = TREE_TYPE (decl);
3868 : :
3869 : 143508 : return (type != error_mark_node
3870 : 143490 : && VAR_P (decl)
3871 : 13452 : && !TREE_STATIC (decl)
3872 : 156824 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3873 : 11460 : || variably_modified_type_p (type, NULL_TREE)));
3874 : : }
3875 : :
3876 : : /* Returns true if decl is an automatic variable with vacuous initialization
3877 : : except when it is [[indeterminate]] or [[gnu::uninitialized]].
3878 : : Jumps across such initialization need to be instrumented for
3879 : : !!flag_auto_var_init. */
3880 : :
3881 : : static bool
3882 : 141643 : decl_instrument_init_bypass_p (tree decl)
3883 : : {
3884 : 141643 : tree type = TREE_TYPE (decl);
3885 : :
3886 : 141643 : return (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
3887 : 65337 : && type != error_mark_node
3888 : 65331 : && VAR_P (decl)
3889 : 605 : && !TREE_STATIC (decl)
3890 : 584 : && !DECL_EXTERNAL (decl)
3891 : 1140 : && !(DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3892 : 570 : || variably_modified_type_p (type, NULL_TREE))
3893 : 570 : && !lookup_attribute (NULL, "indeterminate", DECL_ATTRIBUTES (decl))
3894 : 570 : && !lookup_attribute ("uninitialized", DECL_ATTRIBUTES (decl))
3895 : 142213 : && !DECL_HAS_VALUE_EXPR_P (decl));
3896 : : }
3897 : :
3898 : : /* Build .DEFERRED_INIT call for DECL. */
3899 : :
3900 : : static tree
3901 : 59 : build_deferred_init_call (tree decl)
3902 : : {
3903 : 59 : tree decl_size_arg = TYPE_SIZE_UNIT (TREE_TYPE (decl));
3904 : 118 : tree init_type_arg = build_int_cst (integer_type_node,
3905 : 59 : (int) flag_auto_var_init);
3906 : 59 : location_t loc = DECL_SOURCE_LOCATION (decl);
3907 : 59 : tree decl_name;
3908 : :
3909 : 59 : if (DECL_NAME (decl))
3910 : 59 : decl_name = build_string_literal (DECL_NAME (decl));
3911 : : else
3912 : : {
3913 : 0 : char decl_name_anonymous[3 + (HOST_BITS_PER_INT + 2) / 3];
3914 : 0 : sprintf (decl_name_anonymous, "D.%u", DECL_UID (decl));
3915 : 0 : decl_name = build_string_literal (decl_name_anonymous);
3916 : : }
3917 : :
3918 : 59 : tree call = build_call_expr_internal_loc (loc, IFN_DEFERRED_INIT,
3919 : 59 : TREE_TYPE (decl), 3,
3920 : : decl_size_arg, init_type_arg,
3921 : : decl_name);
3922 : 59 : tree ret = build2_loc (loc, MODIFY_EXPR, void_type_node, decl, call);
3923 : 59 : return build_stmt (loc, EXPR_STMT, ret);
3924 : : }
3925 : :
3926 : : /* Emit before ITER (and any labels/case labels before it) code like
3927 : : if (0)
3928 : : {
3929 : : l1:
3930 : : v4 = .DEFERRED_INIT (sizeof (v4), ?, "v4");
3931 : : v3 = .DEFERRED_INIT (sizeof (v3), ?, "v3");
3932 : : v2 = .DEFERRED_INIT (sizeof (v2), ?, "v2");
3933 : : v1 = .DEFERRED_INIT (sizeof (v1), ?, "v1");
3934 : : }
3935 : : and return l1 label, or if it already exists, assert it has the
3936 : : .DEFERRED_INIT calls for the right decls in the right order and
3937 : : amend it, either by adding extra labels in between or further
3938 : : ,DEFERRED_INIT calls before the first label and extra label before
3939 : : that. If CASE_LABEL is non-NULL, emit that CASE_LABEL_EXPR instead
3940 : : of adding a label. DECLS points to an array of NDECLS VAR_DECLs
3941 : : which should be initialized. */
3942 : :
3943 : : static tree
3944 : 53 : maybe_add_deferred_init_calls (tree_stmt_iterator iter, tree case_label,
3945 : : tree *decls, unsigned ndecls)
3946 : : {
3947 : 53 : tree lab = NULL_TREE;
3948 : 75 : for (; !tsi_end_p (iter); tsi_prev (&iter))
3949 : : {
3950 : 71 : switch (TREE_CODE (tsi_stmt (iter)))
3951 : : {
3952 : 22 : case LABEL_EXPR:
3953 : 22 : case CASE_LABEL_EXPR:
3954 : 22 : case DEBUG_BEGIN_STMT:
3955 : 22 : continue;
3956 : : default:
3957 : : break;
3958 : : }
3959 : : break;
3960 : : }
3961 : 53 : if (!tsi_end_p (iter)
3962 : 49 : && TREE_CODE (tsi_stmt (iter)) == IF_STMT
3963 : 79 : && IF_STMT_VACUOUS_INIT_P (tsi_stmt (iter)))
3964 : : {
3965 : : /* Found IF_STMT added for this or some adjacent
3966 : : LABEL_EXPR/CASE_LABEL_EXPR by an earlier call to this function.
3967 : : The decls are ordered so that we can always reuse it. Sometimes
3968 : : by no modifications at all and just returning the right label
3969 : : which was added already before, sometimes by adding a label in
3970 : : between two previously added .DEFERRED_INIT calls and sometimes
3971 : : by adding extra statements (.DEFERRED_INIT calls and LABEL_EXPR
3972 : : before that) before the statements in IF_STMT body. */
3973 : 24 : tree then_clause = THEN_CLAUSE (tsi_stmt (iter));
3974 : 24 : iter = tsi_last (then_clause);
3975 : 24 : bool add = false;
3976 : 128 : for (unsigned int i = 0; i < ndecls; ++i)
3977 : : {
3978 : 104 : tree decl = decls[i];
3979 : 104 : if (!add)
3980 : : {
3981 : : /* Skip over labels/case labels after .DEFERRED_INIT for the
3982 : : DECL we are looking for. */
3983 : 134 : while (!tsi_end_p (iter)
3984 : 134 : && (TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR
3985 : 96 : || (TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR
3986 : 9 : && !case_label)))
3987 : 40 : tsi_prev (&iter);
3988 : 94 : if (tsi_end_p (iter))
3989 : : {
3990 : : /* Reached the start, we'll need to prepend further
3991 : : statements. */
3992 : 7 : add = true;
3993 : 7 : iter = tsi_start (then_clause);
3994 : : }
3995 : : else
3996 : : {
3997 : : /* Found something, assert it is .DEFERRED_INIT for
3998 : : DECL. */
3999 : 87 : tree t = tsi_stmt (iter);
4000 : 87 : gcc_checking_assert (TREE_CODE (t) == EXPR_STMT);
4001 : 87 : t = EXPR_STMT_EXPR (t);
4002 : 87 : gcc_checking_assert (TREE_CODE (t) == MODIFY_EXPR
4003 : : && TREE_OPERAND (t, 0) == decl
4004 : : && (TREE_CODE (TREE_OPERAND (t, 1))
4005 : : == CALL_EXPR));
4006 : 87 : t = TREE_OPERAND (t, 1);
4007 : 87 : gcc_checking_assert (CALL_EXPR_FN (t) == NULL_TREE
4008 : : && (CALL_EXPR_IFN (t)
4009 : : == IFN_DEFERRED_INIT));
4010 : 87 : tsi_prev (&iter);
4011 : : }
4012 : : }
4013 : 94 : if (add)
4014 : : {
4015 : : /* If reached the start in this or some earlier iteration,
4016 : : prepend .DEFERRED_INIT call for DECL. */
4017 : 17 : tree t = build_deferred_init_call (decl);
4018 : 17 : STMT_IS_FULL_EXPR_P (t) = 1;
4019 : 17 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4020 : : }
4021 : : }
4022 : 24 : if (!add)
4023 : : {
4024 : : /* If .DEFERRED_INIT calls for all the decls were already there,
4025 : : skip over case labels and if we find a LABEL_EXPR, return
4026 : : its label. */
4027 : 21 : while (!tsi_end_p (iter)
4028 : 21 : && !case_label
4029 : 39 : && TREE_CODE (tsi_stmt (iter)) == CASE_LABEL_EXPR)
4030 : 4 : tsi_prev (&iter);
4031 : 17 : if (tsi_end_p (iter))
4032 : : {
4033 : : /* Only case labels were found and we are looking for normal
4034 : : label, we'll need to add it. */
4035 : 0 : add = true;
4036 : 0 : iter = tsi_start (then_clause);
4037 : : }
4038 : 17 : else if (!case_label
4039 : 17 : && TREE_CODE (tsi_stmt (iter)) == LABEL_EXPR)
4040 : : /* Return existing label. */
4041 : 7 : lab = LABEL_EXPR_LABEL (tsi_stmt (iter));
4042 : : else
4043 : : {
4044 : : /* We'll need to add a LABEL_EXPR or move CASE_LABEL_EXPR. */
4045 : 10 : gcc_checking_assert (case_label
4046 : : || (TREE_CODE (tsi_stmt (iter))
4047 : : == EXPR_STMT));
4048 : 10 : add = true;
4049 : 10 : tsi_next (&iter);
4050 : 10 : gcc_checking_assert (!tsi_end_p (iter));
4051 : : }
4052 : : }
4053 : 7 : if (add)
4054 : : {
4055 : 17 : tree t;
4056 : 17 : if (case_label)
4057 : : t = case_label;
4058 : : else
4059 : : {
4060 : 14 : lab = create_artificial_label (UNKNOWN_LOCATION);
4061 : 14 : t = build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab);
4062 : : }
4063 : 17 : tsi_link_before (&iter, t, TSI_CONTINUE_LINKING);
4064 : : }
4065 : : }
4066 : : else
4067 : : {
4068 : : /* No IF_STMT created by this function found. Create it all
4069 : : from scratch, so a LABEL_EXPR (or moved CASE_LABEL_EXPR)
4070 : : followed by .DEFERRED_INIT calls inside of a new if (0). */
4071 : 29 : tree new_then = push_stmt_list ();
4072 : 29 : if (!case_label)
4073 : : {
4074 : 10 : lab = create_artificial_label (UNKNOWN_LOCATION);
4075 : 10 : add_stmt (build_stmt (UNKNOWN_LOCATION, LABEL_EXPR, lab));
4076 : : }
4077 : : else
4078 : 19 : add_stmt (case_label);
4079 : 71 : for (unsigned int i = ndecls; i; --i)
4080 : 42 : add_stmt (build_deferred_init_call (decls[i - 1]));
4081 : 29 : new_then = pop_stmt_list (new_then);
4082 : 29 : tree stmt = build4 (IF_STMT, void_type_node, boolean_false_node,
4083 : : new_then, void_node, NULL_TREE);
4084 : 29 : IF_STMT_VACUOUS_INIT_P (stmt) = 1;
4085 : 29 : if (tsi_end_p (iter))
4086 : : {
4087 : 4 : iter = tsi_start (iter.container);
4088 : 4 : tsi_link_before (&iter, stmt, TSI_SAME_STMT);
4089 : : }
4090 : : else
4091 : 25 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
4092 : : }
4093 : 53 : return lab;
4094 : : }
4095 : :
4096 : : /* Adjust backward gotos to named label ENT if they jump over vacuous
4097 : : initializers if !!flag_auto_var_init. ITER is the location of
4098 : : LABEL_EXPR for that named label. */
4099 : :
4100 : : static void
4101 : 7 : adjust_backward_goto (named_label_entry *ent, tree_stmt_iterator iter)
4102 : : {
4103 : 7 : auto_vec<tree, 4> decls;
4104 : 7 : unsigned int i, max_cnt = ent->direct_goto->last ().n_bad_decls;
4105 : 7 : tree decl;
4106 : 30 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, i, decl)
4107 : 30 : if (!decl_jump_unsafe (decl))
4108 : : {
4109 : 30 : gcc_checking_assert (decl_instrument_init_bypass_p (decl));
4110 : 30 : decls.safe_push (decl);
4111 : 60 : if (decls.length () == max_cnt)
4112 : : break;
4113 : : }
4114 : 7 : named_label_bck_direct_goto *dgoto;
4115 : 7 : unsigned last = 0;
4116 : 7 : tree lab = NULL_TREE;
4117 : 37 : FOR_EACH_VEC_SAFE_ELT_REVERSE (ent->direct_goto, i, dgoto)
4118 : : {
4119 : 16 : if (dgoto->n_bad_decls != last)
4120 : : {
4121 : 14 : last = dgoto->n_bad_decls;
4122 : 28 : lab = maybe_add_deferred_init_calls (iter, NULL_TREE,
4123 : : decls.address (), last);
4124 : : }
4125 : 16 : *dgoto->direct_goto = lab;
4126 : : }
4127 : 7 : }
4128 : :
4129 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
4130 : : to the user. */
4131 : :
4132 : : static bool
4133 : 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
4134 : : enum diagnostics::kind diag_kind, bool computed)
4135 : : {
4136 : 286 : if (computed)
4137 : 43 : diag_kind = diagnostics::kind::warning;
4138 : 286 : bool complained
4139 : 373 : = emit_diagnostic (diag_kind, loc, 0,
4140 : : decl ? G_("jump to label %qD")
4141 : : : G_("jump to case label"), decl);
4142 : 286 : if (complained && locus)
4143 : : {
4144 : 199 : if (computed)
4145 : 43 : inform (*locus, " as a possible target of computed goto");
4146 : : else
4147 : 156 : inform (*locus, " from here");
4148 : : }
4149 : 286 : return complained;
4150 : : }
4151 : :
4152 : : /* Check that a single previously seen jump to a newly defined label
4153 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4154 : : the jump context; NAMES are the names in scope in LEVEL at the jump
4155 : : context; LOCUS is the source position of the jump or 0. COMPUTED
4156 : : is a vec of decls if the jump is a computed goto. DIRECT_GOTO is a
4157 : : vec of pointers to LABEL_DECLs that might need adjusting if vacuous
4158 : : initializations are crossed for !!flag_auto_var_init. CASE_LABEL is
4159 : : CASE_LABEL_EXPR to be moved if needed for the check_switch_goto case.
4160 : : Returns non-zero if all is well, 2 if any vacuous initializers were
4161 : : crossed. */
4162 : :
4163 : : static int
4164 : 2007398 : check_previous_goto_1 (tree decl, cp_binding_level *level, tree names,
4165 : : bool exited_omp, const location_t *locus,
4166 : : vec<tree, va_gc> *computed,
4167 : : vec<named_label_fwd_direct_goto, va_gc> *direct_goto,
4168 : : tree case_label)
4169 : : {
4170 : 2007398 : auto_diagnostic_group d;
4171 : 2007398 : cp_binding_level *b;
4172 : 2007398 : bool complained = false;
4173 : 2007398 : int identified = 0;
4174 : 2007398 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
4175 : 2007398 : bool saw_ceif = false, saw_se = false;
4176 : 2007398 : auto_vec<tree> vacuous_decls;
4177 : 2007398 : bool vacuous_inits = false;
4178 : :
4179 : 2007398 : if (exited_omp)
4180 : : {
4181 : 12 : complained = identify_goto (decl, input_location, locus,
4182 : : diagnostics::kind::error,
4183 : : computed);
4184 : 12 : if (complained)
4185 : 12 : inform (input_location, " exits OpenMP structured block");
4186 : : saw_omp = true;
4187 : : identified = 2;
4188 : : }
4189 : :
4190 : 5993559 : for (b = current_binding_level; b ; b = b->level_chain)
4191 : : {
4192 : 3986161 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
4193 : :
4194 : 4125290 : for (new_decls = b->names; new_decls != old_decls;
4195 : 268388 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
4196 : 129259 : : TREE_CHAIN (new_decls)))
4197 : : {
4198 : 139129 : bool problem = decl_jump_unsafe (new_decls);
4199 : 139129 : if (! problem)
4200 : : {
4201 : 139084 : if (decl_instrument_init_bypass_p (new_decls))
4202 : : {
4203 : 131 : if (direct_goto || case_label)
4204 : 98 : vacuous_decls.safe_push (new_decls);
4205 : : vacuous_inits = true;
4206 : : }
4207 : 139084 : continue;
4208 : : }
4209 : :
4210 : 45 : if (!identified)
4211 : : {
4212 : 45 : complained = identify_goto (decl, input_location, locus,
4213 : : diagnostics::kind::error,
4214 : : computed);
4215 : 45 : identified = 2;
4216 : : }
4217 : 45 : if (complained)
4218 : 45 : inform (DECL_SOURCE_LOCATION (new_decls),
4219 : : " crosses initialization of %q#D", new_decls);
4220 : : }
4221 : :
4222 : 3986161 : if (b == level)
4223 : : break;
4224 : :
4225 : 1978763 : const char *inf = NULL;
4226 : 1978763 : location_t loc = input_location;
4227 : 1978763 : switch (b->kind)
4228 : : {
4229 : 3 : case sk_try:
4230 : 3 : if (!saw_eh)
4231 : : inf = G_(" enters %<try%> block");
4232 : : saw_eh = true;
4233 : : break;
4234 : :
4235 : 9 : case sk_catch:
4236 : 9 : if (!saw_eh)
4237 : : inf = G_(" enters %<catch%> block");
4238 : : saw_eh = true;
4239 : : break;
4240 : :
4241 : 102 : case sk_omp:
4242 : 102 : if (!saw_omp)
4243 : : inf = G_(" enters OpenMP structured block");
4244 : : saw_omp = true;
4245 : : break;
4246 : :
4247 : 3 : case sk_transaction:
4248 : 3 : if (!saw_tm)
4249 : : inf = G_(" enters synchronized or atomic statement");
4250 : : saw_tm = true;
4251 : : break;
4252 : :
4253 : 18 : case sk_stmt_expr:
4254 : 18 : if (!saw_se)
4255 : : inf = G_(" enters statement expression");
4256 : : saw_se = true;
4257 : : break;
4258 : :
4259 : 1978505 : case sk_block:
4260 : 1978505 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
4261 : : {
4262 : 9 : inf = G_(" enters %<constexpr if%> statement");
4263 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4264 : : saw_cxif = true;
4265 : : }
4266 : 1978496 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
4267 : : {
4268 : 18 : inf = G_(" enters %<consteval if%> statement");
4269 : 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
4270 : : saw_ceif = true;
4271 : : }
4272 : : break;
4273 : :
4274 : : default:
4275 : : break;
4276 : : }
4277 : :
4278 : : if (inf)
4279 : : {
4280 : 141 : if (identified < 2)
4281 : 141 : complained = identify_goto (decl, input_location, locus,
4282 : : diagnostics::kind::error,
4283 : : computed);
4284 : 141 : identified = 2;
4285 : 141 : if (complained)
4286 : 141 : inform (loc, inf);
4287 : : }
4288 : : }
4289 : :
4290 : 2007398 : if (!vec_safe_is_empty (computed))
4291 : : {
4292 : 9 : if (!identified)
4293 : 6 : complained = identify_goto (decl, input_location, locus,
4294 : : diagnostics::kind::error,
4295 : : computed);
4296 : 9 : identified = 2;
4297 : 9 : if (complained)
4298 : 18 : for (tree d : computed)
4299 : : {
4300 : 9 : if (DECL_P (d))
4301 : 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
4302 : 3 : else if (d == get_identifier ("catch"))
4303 : 3 : inform (*locus, " does not clean up handled exception");
4304 : : }
4305 : : }
4306 : :
4307 : 2007439 : if (!vacuous_decls.is_empty () && !seen_error ())
4308 : : {
4309 : 39 : tree_stmt_iterator iter = tsi_last (cur_stmt_list);
4310 : 39 : if (case_label)
4311 : : {
4312 : 22 : gcc_checking_assert (tsi_stmt (iter) == case_label);
4313 : 22 : tsi_delink (&iter);
4314 : 22 : iter = tsi_last (cur_stmt_list);
4315 : : }
4316 : 39 : tree lab = maybe_add_deferred_init_calls (iter, case_label,
4317 : : vacuous_decls.address (),
4318 : : vacuous_decls.length ());
4319 : 39 : if (lab)
4320 : : {
4321 : : unsigned int i;
4322 : : named_label_fwd_direct_goto *dgoto;
4323 : 73 : FOR_EACH_VEC_SAFE_ELT (direct_goto, i, dgoto)
4324 : 17 : *dgoto->direct_goto = lab;
4325 : : }
4326 : : }
4327 : :
4328 : 2007398 : if (identified)
4329 : : return 0;
4330 : 2007194 : return vacuous_inits ? 2 : 1;
4331 : 2007398 : }
4332 : :
4333 : : static void
4334 : 1928 : check_previous_goto (tree decl, struct named_label_use_entry *use)
4335 : : {
4336 : 1928 : check_previous_goto_1 (decl, use->binding_level,
4337 : 1928 : use->names_in_scope, use->in_omp_scope,
4338 : 1928 : &use->o_goto_locus, use->computed_goto,
4339 : : use->direct_goto, NULL_TREE);
4340 : 1928 : vec_free (use->direct_goto);
4341 : 1928 : }
4342 : :
4343 : : static int
4344 : 2005470 : check_switch_goto (cp_binding_level *level, tree case_label)
4345 : : {
4346 : 2005470 : return check_previous_goto_1 (NULL_TREE, level, level->names,
4347 : 2005470 : false, NULL, nullptr, nullptr, case_label);
4348 : : }
4349 : :
4350 : : /* Check that a new jump to a label ENT is OK. DECLP is a pointer
4351 : : to a LABEL_DECL for direct gotos and NULL for computed gotos. */
4352 : :
4353 : : void
4354 : 2463 : check_goto_1 (named_label_entry *ent, tree *declp)
4355 : : {
4356 : 2463 : auto_diagnostic_group d;
4357 : 2463 : tree decl = ent->label_decl;
4358 : 2463 : bool computed = declp == NULL;
4359 : :
4360 : : /* If the label hasn't been defined yet, defer checking. */
4361 : 2463 : if (! DECL_INITIAL (decl))
4362 : : {
4363 : : /* Don't bother creating another use if the last goto had the
4364 : : same data, and will therefore create the same set of errors. */
4365 : 1997 : if (ent->uses
4366 : 526 : && ent->uses->binding_level == current_binding_level
4367 : 60 : && ent->uses->names_in_scope == current_binding_level->names)
4368 : : {
4369 : 60 : if (declp && flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
4370 : 11 : vec_safe_push (ent->uses->direct_goto,
4371 : 11 : named_label_fwd_direct_goto { declp });
4372 : 60 : return;
4373 : : }
4374 : :
4375 : 1937 : named_label_use_entry *new_use
4376 : 1937 : = ggc_alloc<named_label_use_entry> ();
4377 : 1937 : new_use->binding_level = current_binding_level;
4378 : 1937 : new_use->names_in_scope = current_binding_level->names;
4379 : 1937 : new_use->o_goto_locus = input_location;
4380 : 1937 : new_use->in_omp_scope = false;
4381 : 1937 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
4382 : 1937 : new_use->direct_goto = nullptr;
4383 : 1937 : if (declp && flag_auto_var_init > AUTO_INIT_UNINITIALIZED)
4384 : 293 : vec_safe_push (new_use->direct_goto,
4385 : 293 : named_label_fwd_direct_goto { declp });
4386 : :
4387 : 1937 : new_use->next = ent->uses;
4388 : 1937 : ent->uses = new_use;
4389 : 1937 : return;
4390 : : }
4391 : :
4392 : 466 : bool saw_catch = false, complained = false;
4393 : 466 : int identified = 0;
4394 : 466 : tree bad;
4395 : 466 : unsigned ix;
4396 : 466 : unsigned n_bad_decls = 0;
4397 : :
4398 : 466 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
4399 : 451 : || ent->in_constexpr_if || ent->in_consteval_if
4400 : 445 : || ent->in_omp_scope || ent->in_stmt_expr
4401 : 427 : || ent->has_bad_decls)
4402 : : {
4403 : 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
4404 : 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
4405 : 30 : || ent->in_consteval_if || ent->in_transaction_scope
4406 : 24 : || ent->in_omp_scope || ent->in_stmt_expr)
4407 : 39 : diag_kind = diagnostics::kind::error;
4408 : 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4409 : : &input_location, diag_kind, computed);
4410 : 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
4411 : : }
4412 : :
4413 : 534 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
4414 : : {
4415 : 68 : bool problem = decl_jump_unsafe (bad);
4416 : 68 : if (!problem)
4417 : : {
4418 : 62 : gcc_checking_assert (decl_instrument_init_bypass_p (bad));
4419 : 62 : n_bad_decls++;
4420 : 62 : continue;
4421 : : }
4422 : :
4423 : 6 : if (DECL_ARTIFICIAL (bad))
4424 : : {
4425 : : /* Can't skip init of __exception_info. */
4426 : 0 : if (identified == 1)
4427 : : {
4428 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4429 : : &input_location,
4430 : : diagnostics::kind::error,
4431 : : computed);
4432 : 0 : identified = 2;
4433 : : }
4434 : 0 : if (complained)
4435 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
4436 : : saw_catch = true;
4437 : : }
4438 : 6 : else if (complained)
4439 : 6 : inform (DECL_SOURCE_LOCATION (bad),
4440 : : " skips initialization of %q#D", bad);
4441 : : }
4442 : :
4443 : 466 : if (complained)
4444 : : {
4445 : 45 : if (ent->in_try_scope)
4446 : 9 : inform (input_location, " enters %<try%> block");
4447 : 36 : else if (ent->in_catch_scope && !saw_catch)
4448 : 3 : inform (input_location, " enters %<catch%> block");
4449 : 33 : else if (ent->in_transaction_scope)
4450 : 3 : inform (input_location, " enters synchronized or atomic statement");
4451 : 30 : else if (ent->in_constexpr_if)
4452 : 3 : inform (input_location, " enters %<constexpr if%> statement");
4453 : 27 : else if (ent->in_consteval_if)
4454 : 3 : inform (input_location, " enters %<consteval if%> statement");
4455 : 24 : else if (ent->in_stmt_expr)
4456 : 15 : inform (input_location, " enters statement expression");
4457 : : }
4458 : :
4459 : 466 : if (ent->in_omp_scope)
4460 : : {
4461 : 3 : if (complained)
4462 : 3 : inform (input_location, " enters OpenMP structured block");
4463 : : }
4464 : 463 : else if (flag_openmp)
4465 : 69 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4466 : : {
4467 : 42 : if (b == ent->binding_level)
4468 : : break;
4469 : 36 : if (b->kind == sk_omp)
4470 : : {
4471 : 21 : if (identified < 2)
4472 : : {
4473 : 42 : complained = identify_goto (decl,
4474 : 21 : DECL_SOURCE_LOCATION (decl),
4475 : : &input_location,
4476 : : diagnostics::kind::error,
4477 : : computed);
4478 : 21 : identified = 2;
4479 : : }
4480 : 21 : if (complained)
4481 : 21 : inform (input_location, " exits OpenMP structured block");
4482 : : break;
4483 : : }
4484 : : }
4485 : :
4486 : : /* Warn if a computed goto might involve a local variable going out of scope
4487 : : without being cleaned up. */
4488 : 466 : if (computed)
4489 : : {
4490 : 71 : auto level = ent->binding_level;
4491 : 71 : auto names = ent->names_in_scope;
4492 : 71 : for (auto b = current_binding_level; ; b = b->level_chain)
4493 : : {
4494 : 111 : if (b->kind == sk_catch)
4495 : : {
4496 : 6 : if (!identified)
4497 : : {
4498 : 6 : complained
4499 : 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4500 : : &input_location, diagnostics::kind::error,
4501 : : computed);
4502 : 6 : identified = 2;
4503 : : }
4504 : 6 : if (complained)
4505 : 6 : inform (input_location,
4506 : : " does not clean up handled exception");
4507 : : }
4508 : 111 : tree end = b == level ? names : NULL_TREE;
4509 : 127 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4510 : : {
4511 : 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4512 : : {
4513 : 10 : if (!identified)
4514 : : {
4515 : 10 : complained
4516 : 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4517 : : &input_location, diagnostics::kind::error,
4518 : : computed);
4519 : 10 : identified = 2;
4520 : : }
4521 : 10 : if (complained)
4522 : 10 : inform (DECL_SOURCE_LOCATION (d),
4523 : : " does not destroy %qD", d);
4524 : : }
4525 : : }
4526 : 111 : if (b == level)
4527 : : break;
4528 : 40 : }
4529 : : }
4530 : :
4531 : 466 : if (n_bad_decls && declp)
4532 : 16 : vec_safe_push (ent->direct_goto,
4533 : 16 : named_label_bck_direct_goto { declp, n_bad_decls });
4534 : 2463 : }
4535 : :
4536 : : /* Check that a new jump to a label *DECLP is OK. Called by
4537 : : finish_goto_stmt. */
4538 : :
4539 : : void
4540 : 2423 : check_goto (tree *declp)
4541 : : {
4542 : 2423 : if (!named_labels)
4543 : : return;
4544 : 2408 : tree decl = *declp;
4545 : 2408 : if (TREE_CODE (decl) != LABEL_DECL)
4546 : : {
4547 : : /* We don't know where a computed goto is jumping,
4548 : : so check all addressable labels. */
4549 : 306 : for (auto iter = named_labels->begin ();
4550 : 416 : iter != named_labels->end ();
4551 : 196 : ++iter)
4552 : : {
4553 : 196 : auto ent = *iter;
4554 : 196 : if (ent->addressed)
4555 : 165 : check_goto_1 (ent, NULL);
4556 : : }
4557 : : }
4558 : : else
4559 : : {
4560 : 2298 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4561 : 2298 : named_label_entry **slot
4562 : 2298 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4563 : 2298 : named_label_entry *ent = *slot;
4564 : 2298 : check_goto_1 (ent, declp);
4565 : : }
4566 : : }
4567 : :
4568 : : /* Check that a return is ok wrt OpenMP structured blocks.
4569 : : Called by finish_return_stmt. Returns true if all is well. */
4570 : :
4571 : : bool
4572 : 232193 : check_omp_return (void)
4573 : : {
4574 : 890492 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4575 : 658299 : if (b->kind == sk_omp)
4576 : : {
4577 : 15 : error ("invalid exit from OpenMP structured block");
4578 : 15 : return false;
4579 : : }
4580 : 658284 : else if (b->kind == sk_function_parms)
4581 : : break;
4582 : : return true;
4583 : : }
4584 : :
4585 : : /* Define a label, specifying the location in the source file.
4586 : : Return the LABEL_DECL node for the label. */
4587 : :
4588 : : tree
4589 : 20310 : define_label (location_t location, tree name)
4590 : : {
4591 : 20310 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4592 : :
4593 : : /* After labels, make any new cleanups in the function go into their
4594 : : own new (temporary) binding contour. */
4595 : 20310 : for (cp_binding_level *p = current_binding_level;
4596 : 30538 : p->kind != sk_function_parms;
4597 : 10228 : p = p->level_chain)
4598 : 10228 : p->more_cleanups_ok = 0;
4599 : :
4600 : 20310 : named_label_entry *ent = lookup_label_1 (name, false);
4601 : 20310 : tree decl = ent->label_decl;
4602 : :
4603 : 20310 : if (DECL_INITIAL (decl) != NULL_TREE)
4604 : : {
4605 : 6 : error ("duplicate label %qD", decl);
4606 : 6 : return error_mark_node;
4607 : : }
4608 : : else
4609 : : {
4610 : : /* Mark label as having been defined. */
4611 : 20304 : DECL_INITIAL (decl) = error_mark_node;
4612 : : /* Say where in the source. */
4613 : 20304 : DECL_SOURCE_LOCATION (decl) = location;
4614 : :
4615 : 20304 : ent->binding_level = current_binding_level;
4616 : 20304 : ent->names_in_scope = current_binding_level->names;
4617 : :
4618 : 22232 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4619 : 1928 : check_previous_goto (decl, use);
4620 : 20304 : ent->uses = NULL;
4621 : : }
4622 : :
4623 : 20304 : return decl;
4624 : 20310 : }
4625 : :
4626 : : struct cp_switch
4627 : : {
4628 : : cp_binding_level *level;
4629 : : struct cp_switch *next;
4630 : : /* The SWITCH_STMT being built. */
4631 : : tree switch_stmt;
4632 : : /* A splay-tree mapping the low element of a case range to the high
4633 : : element, or NULL_TREE if there is no high element. Used to
4634 : : determine whether or not a new case label duplicates an old case
4635 : : label. We need a tree, rather than simply a hash table, because
4636 : : of the GNU case range extension. */
4637 : : splay_tree cases;
4638 : : /* Remember whether a default: case label has been seen. */
4639 : : bool has_default_p;
4640 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4641 : : bool break_stmt_seen_p;
4642 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4643 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4644 : : bool in_loop_body_p;
4645 : : };
4646 : :
4647 : : /* A stack of the currently active switch statements. The innermost
4648 : : switch statement is on the top of the stack. There is no need to
4649 : : mark the stack for garbage collection because it is only active
4650 : : during the processing of the body of a function, and we never
4651 : : collect at that point. */
4652 : :
4653 : : static struct cp_switch *switch_stack;
4654 : :
4655 : : /* Called right after a switch-statement condition is parsed.
4656 : : SWITCH_STMT is the switch statement being parsed. */
4657 : :
4658 : : void
4659 : 500873 : push_switch (tree switch_stmt)
4660 : : {
4661 : 500873 : struct cp_switch *p = XNEW (struct cp_switch);
4662 : 500873 : p->level = current_binding_level;
4663 : 500873 : p->next = switch_stack;
4664 : 500873 : p->switch_stmt = switch_stmt;
4665 : 500873 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4666 : 500873 : p->has_default_p = false;
4667 : 500873 : p->break_stmt_seen_p = false;
4668 : 500873 : p->in_loop_body_p = false;
4669 : 500873 : switch_stack = p;
4670 : 500873 : }
4671 : :
4672 : : void
4673 : 500873 : pop_switch (void)
4674 : : {
4675 : 500873 : struct cp_switch *cs = switch_stack;
4676 : :
4677 : : /* Emit warnings as needed. */
4678 : 500873 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4679 : 500873 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4680 : 500873 : const bool bool_cond_p
4681 : 500873 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4682 : 500873 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4683 : 500873 : if (!processing_template_decl)
4684 : 316033 : c_do_switch_warnings (cs->cases, switch_location,
4685 : 316033 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4686 : : bool_cond_p);
4687 : :
4688 : : /* For the benefit of block_may_fallthru remember if the switch body
4689 : : case labels cover all possible values and if there are break; stmts. */
4690 : 500873 : if (cs->has_default_p
4691 : 500873 : || (!processing_template_decl
4692 : 43600 : && c_switch_covers_all_cases_p (cs->cases,
4693 : 43600 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4694 : 387231 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4695 : 500873 : if (!cs->break_stmt_seen_p)
4696 : 257628 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4697 : : /* Now that we're done with the switch warnings, set the switch type
4698 : : to the type of the condition if the index type was of scoped enum type.
4699 : : (Such types don't participate in the integer promotions.) We do this
4700 : : because of bit-fields whose declared type is a scoped enum type:
4701 : : gimplification will use the lowered index type, but convert the
4702 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4703 : : and verify_gimple_switch doesn't accept that. */
4704 : 500873 : if (is_bitfield_expr_with_lowered_type (cond))
4705 : 56476 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4706 : 500873 : gcc_assert (!cs->in_loop_body_p);
4707 : 500873 : splay_tree_delete (cs->cases);
4708 : 500873 : switch_stack = switch_stack->next;
4709 : 500873 : free (cs);
4710 : 500873 : }
4711 : :
4712 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4713 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4714 : : in switch_stack we've seen a BREAK_STMT. */
4715 : :
4716 : : void
4717 : 3556845 : note_break_stmt (void)
4718 : : {
4719 : 3556845 : if (switch_stack && !switch_stack->in_loop_body_p)
4720 : 1558836 : switch_stack->break_stmt_seen_p = true;
4721 : 3556845 : }
4722 : :
4723 : : /* Note the start of processing of an iteration statement's body.
4724 : : The note_break_stmt function will do nothing while processing it.
4725 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4726 : :
4727 : : bool
4728 : 15545135 : note_iteration_stmt_body_start (void)
4729 : : {
4730 : 15545135 : if (!switch_stack)
4731 : : return false;
4732 : 119422 : bool ret = switch_stack->in_loop_body_p;
4733 : 119422 : switch_stack->in_loop_body_p = true;
4734 : 119422 : return ret;
4735 : : }
4736 : :
4737 : : /* Note the end of processing of an iteration statement's body. */
4738 : :
4739 : : void
4740 : 15545135 : note_iteration_stmt_body_end (bool prev)
4741 : : {
4742 : 15545135 : if (switch_stack)
4743 : 119422 : switch_stack->in_loop_body_p = prev;
4744 : 15545135 : }
4745 : :
4746 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4747 : : condition. Note that if TYPE and VALUE are already integral we don't
4748 : : really do the conversion because the language-independent
4749 : : warning/optimization code will work better that way. */
4750 : :
4751 : : static tree
4752 : 4010648 : case_conversion (tree type, tree value)
4753 : : {
4754 : 4010648 : if (value == NULL_TREE)
4755 : : return value;
4756 : :
4757 : 1733059 : value = mark_rvalue_use (value);
4758 : :
4759 : 1733059 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4760 : 974669 : type = type_promotes_to (type);
4761 : :
4762 : 1733059 : tree ovalue = value;
4763 : : /* The constant-expression VALUE shall be a converted constant expression
4764 : : of the adjusted type of the switch condition, which doesn't allow
4765 : : narrowing conversions. */
4766 : 1733059 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4767 : :
4768 : 1733059 : if (cxx_dialect >= cxx11
4769 : 1733059 : && (SCOPED_ENUM_P (type)
4770 : 971944 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4771 : : /* Use the converted value. */;
4772 : : else
4773 : : /* The already integral case. */
4774 : : value = ovalue;
4775 : :
4776 : 1733059 : return cxx_constant_value (value);
4777 : : }
4778 : :
4779 : : /* Note that we've seen a definition of a case label, and complain if this
4780 : : is a bad place for one. */
4781 : :
4782 : : tree
4783 : 3708539 : finish_case_label (location_t loc, tree low_value, tree high_value)
4784 : : {
4785 : 3708539 : tree cond, r;
4786 : 3708539 : cp_binding_level *p;
4787 : 3708539 : tree type;
4788 : :
4789 : 3708539 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4790 : 387195 : switch_stack->has_default_p = true;
4791 : :
4792 : 3708539 : if (processing_template_decl)
4793 : : {
4794 : 1703092 : tree label;
4795 : :
4796 : : /* For templates, just add the case label; we'll do semantic
4797 : : analysis at instantiation-time. But diagnose case labels
4798 : : in expansion statements with switch outside of it here. */
4799 : 1703092 : if (in_expansion_stmt)
4800 : 39 : for (cp_binding_level *b = current_binding_level;
4801 : 78 : b != switch_stack->level; b = b->level_chain)
4802 : 63 : if (b->kind == sk_template_for && b->this_entity)
4803 : : {
4804 : 24 : auto_diagnostic_group d;
4805 : 24 : error ("jump to case label");
4806 : 24 : inform (EXPR_LOCATION (b->this_entity),
4807 : : " enters %<template for%> statement");
4808 : 24 : return error_mark_node;
4809 : 24 : }
4810 : 1703068 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4811 : 1703068 : return add_stmt (build_case_label (low_value, high_value, label));
4812 : : }
4813 : :
4814 : : /* Find the condition on which this switch statement depends. */
4815 : 2005447 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4816 : 2005447 : if (cond && TREE_CODE (cond) == TREE_LIST)
4817 : 0 : cond = TREE_VALUE (cond);
4818 : :
4819 : 2005447 : int chk_switch_goto = check_switch_goto (switch_stack->level, NULL_TREE);
4820 : 2005447 : if (!chk_switch_goto)
4821 : 87 : return error_mark_node;
4822 : :
4823 : 2005360 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4824 : 2005360 : if (type == error_mark_node)
4825 : : return error_mark_node;
4826 : :
4827 : 2005324 : low_value = case_conversion (type, low_value);
4828 : 2005324 : high_value = case_conversion (type, high_value);
4829 : :
4830 : 2005324 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4831 : :
4832 : 2005324 : if (r != error_mark_node && chk_switch_goto == 2)
4833 : 23 : check_switch_goto (switch_stack->level, r);
4834 : :
4835 : : /* After labels, make any new cleanups in the function go into their
4836 : : own new (temporary) binding contour. */
4837 : 2005324 : for (p = current_binding_level;
4838 : 8439261 : p->kind != sk_function_parms;
4839 : 6433937 : p = p->level_chain)
4840 : 6433937 : p->more_cleanups_ok = 0;
4841 : :
4842 : : return r;
4843 : : }
4844 : :
4845 : : struct typename_info {
4846 : : tree scope;
4847 : : tree name;
4848 : : tree template_id;
4849 : : bool enum_p;
4850 : : bool class_p;
4851 : : bool union_p;
4852 : : };
4853 : :
4854 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4855 : : {
4856 : : typedef typename_info *compare_type;
4857 : :
4858 : : /* Hash a TYPENAME_TYPE. */
4859 : :
4860 : : static hashval_t
4861 : 394934558 : hash (tree context, tree fullname)
4862 : : {
4863 : 394934558 : hashval_t hash = 0;
4864 : 394934558 : hash = iterative_hash_object (context, hash);
4865 : 394934558 : hash = iterative_hash_object (fullname, hash);
4866 : 394934558 : return hash;
4867 : : }
4868 : :
4869 : : static hashval_t
4870 : 57197213 : hash (const typename_info *ti)
4871 : : {
4872 : 57197213 : return typename_hasher::hash (ti->scope, ti->template_id);
4873 : : }
4874 : :
4875 : : static hashval_t
4876 : 337737345 : hash (tree t)
4877 : : {
4878 : 337737345 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4879 : : }
4880 : :
4881 : : /* Compare two TYPENAME_TYPEs. */
4882 : :
4883 : : static bool
4884 : 393137458 : equal (tree t1, const typename_info *t2)
4885 : : {
4886 : 393137458 : return (TYPE_IDENTIFIER (t1) == t2->name
4887 : 115054336 : && TYPE_CONTEXT (t1) == t2->scope
4888 : 29371521 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4889 : 29359898 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4890 : 29359844 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p
4891 : 422022295 : && TYPENAME_IS_UNION_P (t1) == t2->union_p);
4892 : : }
4893 : : };
4894 : :
4895 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4896 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4897 : :
4898 : : Returns the new TYPENAME_TYPE. */
4899 : :
4900 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4901 : :
4902 : : tree
4903 : 57197213 : build_typename_type (tree context, tree name, tree fullname,
4904 : : enum tag_types tag_type)
4905 : : {
4906 : 57197213 : typename_info ti;
4907 : :
4908 : 57197213 : if (typename_htab == NULL)
4909 : 19047 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4910 : :
4911 : 57197213 : ti.scope = FROB_CONTEXT (context);
4912 : 57197213 : ti.name = name;
4913 : 57197213 : ti.template_id = fullname;
4914 : 57197213 : ti.enum_p = tag_type == enum_type;
4915 : 57197213 : ti.class_p = (tag_type == class_type || tag_type == record_type);
4916 : 57197213 : ti.union_p = tag_type == union_type;
4917 : 57197213 : hashval_t hash = typename_hasher::hash (&ti);
4918 : :
4919 : : /* See if we already have this type. */
4920 : 57197213 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4921 : 57197213 : tree t = *e;
4922 : 57197213 : if (*e)
4923 : : t = *e;
4924 : : else
4925 : : {
4926 : : /* Build the TYPENAME_TYPE. */
4927 : 28312376 : t = cxx_make_type (TYPENAME_TYPE);
4928 : 28312376 : TYPE_CONTEXT (t) = ti.scope;
4929 : 28312376 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4930 : 28312376 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4931 : 28312376 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4932 : 28312376 : TYPENAME_IS_UNION_P (t) = ti.union_p;
4933 : :
4934 : : /* Build the corresponding TYPE_DECL. */
4935 : 28312376 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4936 : 28312376 : TYPE_NAME (t) = d;
4937 : 28312376 : TYPE_STUB_DECL (t) = d;
4938 : 28312376 : DECL_CONTEXT (d) = ti.scope;
4939 : 28312376 : DECL_ARTIFICIAL (d) = 1;
4940 : :
4941 : : /* Store it in the hash table. */
4942 : 28312376 : *e = t;
4943 : :
4944 : : /* TYPENAME_TYPEs must always be compared structurally, because
4945 : : they may or may not resolve down to another type depending on
4946 : : the currently open classes. */
4947 : 28312376 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4948 : : }
4949 : :
4950 : 57197213 : return t;
4951 : : }
4952 : :
4953 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4954 : : provided to name the type. Returns an appropriate type, unless an
4955 : : error occurs, in which case error_mark_node is returned. If we
4956 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4957 : : return that, rather than the _TYPE it corresponds to, in other
4958 : : cases we look through the type decl. If TF_ERROR is set, complain
4959 : : about errors, otherwise be quiet. */
4960 : :
4961 : : tree
4962 : 200027646 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4963 : : tsubst_flags_t complain)
4964 : : {
4965 : 200027646 : tree fullname;
4966 : 200027646 : tree t;
4967 : 200027646 : bool want_template;
4968 : :
4969 : 200027646 : if (name == error_mark_node
4970 : 200027644 : || context == NULL_TREE
4971 : 200027644 : || context == error_mark_node)
4972 : : return error_mark_node;
4973 : :
4974 : 200027628 : if (TYPE_P (name))
4975 : : {
4976 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4977 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4978 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4979 : 0 : name = TYPE_IDENTIFIER (name);
4980 : : else
4981 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4982 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4983 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4984 : 0 : CLASSTYPE_TI_ARGS (name));
4985 : : }
4986 : 200027628 : else if (TREE_CODE (name) == TYPE_DECL)
4987 : 0 : name = DECL_NAME (name);
4988 : :
4989 : 200027628 : fullname = name;
4990 : :
4991 : 200027628 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4992 : : {
4993 : 6325654 : name = TREE_OPERAND (name, 0);
4994 : 6325654 : if (DECL_TYPE_TEMPLATE_P (name))
4995 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4996 : 6325654 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4997 : : {
4998 : 0 : if (complain & tf_error)
4999 : 0 : error ("%qD is not a type", name);
5000 : 0 : return error_mark_node;
5001 : : }
5002 : : }
5003 : 200027628 : if (TREE_CODE (name) == TEMPLATE_DECL)
5004 : : {
5005 : 0 : if (complain & tf_error)
5006 : 0 : error ("%qD used without template arguments", name);
5007 : 0 : return error_mark_node;
5008 : : }
5009 : 200027628 : else if (is_overloaded_fn (name))
5010 : : {
5011 : 3 : if (complain & tf_error)
5012 : 3 : error ("%qD is a function, not a type", name);
5013 : 3 : return error_mark_node;
5014 : : }
5015 : 200027625 : gcc_assert (identifier_p (name));
5016 : 200027625 : gcc_assert (TYPE_P (context));
5017 : :
5018 : 200027625 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
5019 : : /* This can happen for C++17 variadic using (c++/88986). */;
5020 : 188137763 : else if (!MAYBE_CLASS_TYPE_P (context))
5021 : : {
5022 : 6 : if (complain & tf_error)
5023 : 6 : error ("%q#T is not a class", context);
5024 : 6 : return error_mark_node;
5025 : : }
5026 : :
5027 : : /* When the CONTEXT is a dependent type, NAME could refer to a
5028 : : dependent base class of CONTEXT. But look inside it anyway
5029 : : if CONTEXT is a currently open scope, in case it refers to a
5030 : : member of the current instantiation or a non-dependent base;
5031 : : lookup will stop when we hit a dependent base. */
5032 : 200027619 : if (!dependent_scope_p (context))
5033 : : {
5034 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
5035 : : (as per [temp.res.general]/3), unless
5036 : : - the tag corresponds to a class-key or 'enum' so
5037 : : [basic.lookup.elab] applies, or
5038 : : - the tag corresponds to scope_type or tf_qualifying_scope is
5039 : : set so [basic.lookup.qual]/1 applies, or
5040 : : - we're inside a base-specifier so [class.derived.general]/2 applies;
5041 : : the tag will already be class_type in that case.
5042 : : TODO: If we'd set/track the scope_type tag thoroughly on all
5043 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
5044 : : tf_qualifying_scope flag. */
5045 : 294148798 : bool want_type = (tag_type != none_type && tag_type != typename_type)
5046 : 147074399 : || (complain & tf_qualifying_scope);
5047 : 147074399 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
5048 : : }
5049 : : else
5050 : : t = NULL_TREE;
5051 : :
5052 : 217682872 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
5053 : 52954138 : return build_typename_type (context, name, fullname, tag_type);
5054 : :
5055 : 147073481 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
5056 : :
5057 : 147073481 : if (!t)
5058 : : {
5059 : 17654335 : if (complain & tf_error)
5060 : : {
5061 : 242 : if (!COMPLETE_TYPE_P (context))
5062 : 18 : cxx_incomplete_type_error (NULL_TREE, context);
5063 : : else
5064 : 447 : error (want_template ? G_("no class template named %q#T in %q#T")
5065 : : : G_("no type named %q#T in %q#T"), name, context);
5066 : : }
5067 : 17654335 : return error_mark_node;
5068 : : }
5069 : :
5070 : : /* Pull out the template from an injected-class-name (or multiple). */
5071 : 129419146 : if (want_template)
5072 : 4828048 : t = maybe_get_template_decl_from_type_decl (t);
5073 : :
5074 : 129419146 : if (TREE_CODE (t) == TREE_LIST)
5075 : : {
5076 : 3 : if (complain & tf_error)
5077 : : {
5078 : 3 : auto_diagnostic_group d;
5079 : 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
5080 : 3 : print_candidates (t);
5081 : 3 : }
5082 : 3 : return error_mark_node;
5083 : : }
5084 : :
5085 : 129419143 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
5086 : : {
5087 : 6 : if (complain & tf_error)
5088 : 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
5089 : : context, name, t);
5090 : 6 : return error_mark_node;
5091 : : }
5092 : 129419137 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
5093 : : {
5094 : 987 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
5095 : 1977 : && DECL_TYPE_TEMPLATE_P (t))
5096 : : /* The caller permits this typename-specifier to name a template
5097 : : (because it appears in a CTAD-enabled context). */;
5098 : : else
5099 : : {
5100 : 23 : if (complain & tf_error)
5101 : 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
5102 : : context, name, t);
5103 : 23 : return error_mark_node;
5104 : : }
5105 : : }
5106 : :
5107 : 129419114 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
5108 : : context, complain))
5109 : 49 : return error_mark_node;
5110 : :
5111 : 129419065 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
5112 : 976 : return make_template_placeholder (t);
5113 : :
5114 : 129418089 : if (want_template)
5115 : : {
5116 : 4828042 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
5117 : : NULL_TREE, context,
5118 : : complain | tf_user);
5119 : 4828042 : if (t == error_mark_node)
5120 : : return error_mark_node;
5121 : 4828037 : t = TYPE_NAME (t);
5122 : : }
5123 : :
5124 : 129418084 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5125 : 3396209 : t = TREE_TYPE (t);
5126 : :
5127 : 129418084 : maybe_record_typedef_use (t);
5128 : :
5129 : 129418084 : return t;
5130 : : }
5131 : :
5132 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
5133 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
5134 : : in which case error_mark_node is returned.
5135 : :
5136 : : If PARM_LIST is non-NULL, also make sure that the template parameter
5137 : : list of TEMPLATE_DECL matches.
5138 : :
5139 : : If COMPLAIN zero, don't complain about any errors that occur. */
5140 : :
5141 : : tree
5142 : 82392 : make_unbound_class_template (tree context, tree name, tree parm_list,
5143 : : tsubst_flags_t complain)
5144 : : {
5145 : 82392 : if (TYPE_P (name))
5146 : 0 : name = TYPE_IDENTIFIER (name);
5147 : 82392 : else if (DECL_P (name))
5148 : 0 : name = DECL_NAME (name);
5149 : 82392 : gcc_assert (identifier_p (name));
5150 : :
5151 : 82392 : if (!dependent_type_p (context)
5152 : 82392 : || currently_open_class (context))
5153 : : {
5154 : 27494 : tree tmpl = NULL_TREE;
5155 : :
5156 : 27494 : if (MAYBE_CLASS_TYPE_P (context))
5157 : 27488 : tmpl = lookup_field (context, name, 0, false);
5158 : :
5159 : 27488 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
5160 : 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5161 : :
5162 : 27482 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
5163 : : {
5164 : 12 : if (complain & tf_error)
5165 : 9 : error ("no class template named %q#T in %q#T", name, context);
5166 : 12 : return error_mark_node;
5167 : : }
5168 : :
5169 : 27482 : if (parm_list
5170 : 27482 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
5171 : : {
5172 : 0 : if (complain & tf_error)
5173 : : {
5174 : 0 : auto_diagnostic_group d;
5175 : 0 : error ("template parameters do not match template %qD", tmpl);
5176 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
5177 : : "%qD declared here", tmpl);
5178 : 0 : }
5179 : 0 : return error_mark_node;
5180 : : }
5181 : :
5182 : 27482 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
5183 : : complain))
5184 : 0 : return error_mark_node;
5185 : :
5186 : : return tmpl;
5187 : : }
5188 : :
5189 : 54898 : return make_unbound_class_template_raw (context, name, parm_list);
5190 : : }
5191 : :
5192 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
5193 : :
5194 : : tree
5195 : 54942 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
5196 : : {
5197 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
5198 : 54942 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
5199 : 54942 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5200 : 54942 : TREE_TYPE (t) = NULL_TREE;
5201 : 54942 : SET_TYPE_STRUCTURAL_EQUALITY (t);
5202 : :
5203 : : /* Build the corresponding TEMPLATE_DECL. */
5204 : 54942 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
5205 : 54942 : TYPE_NAME (t) = d;
5206 : 54942 : TYPE_STUB_DECL (t) = d;
5207 : 54942 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
5208 : 54942 : DECL_ARTIFICIAL (d) = 1;
5209 : 54942 : DECL_TEMPLATE_PARMS (d) = parm_list;
5210 : :
5211 : 54942 : return t;
5212 : : }
5213 : :
5214 : :
5215 : :
5216 : : /* Push the declarations of builtin types into the global namespace.
5217 : : RID_INDEX is the index of the builtin type in the array
5218 : : RID_POINTERS. NAME is the name used when looking up the builtin
5219 : : type. TYPE is the _TYPE node for the builtin type.
5220 : :
5221 : : The calls to set_global_binding below should be
5222 : : eliminated. Built-in types should not be looked up name; their
5223 : : names are keywords that the parser can recognize. However, there
5224 : : is code in c-common.cc that uses identifier_global_value to look up
5225 : : built-in types by name. */
5226 : :
5227 : : void
5228 : 3957937 : record_builtin_type (enum rid rid_index,
5229 : : const char* name,
5230 : : tree type)
5231 : : {
5232 : 3957937 : tree decl = NULL_TREE;
5233 : :
5234 : 3957937 : if (name)
5235 : : {
5236 : 2317930 : tree tname = get_identifier (name);
5237 : 2317930 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
5238 : 2317930 : DECL_ARTIFICIAL (tdecl) = 1;
5239 : 2317930 : set_global_binding (tdecl);
5240 : 2317930 : decl = tdecl;
5241 : : }
5242 : :
5243 : 3957937 : if ((int) rid_index < (int) RID_MAX)
5244 : 2414401 : if (tree rname = ridpointers[(int) rid_index])
5245 : 1835575 : if (!decl || DECL_NAME (decl) != rname)
5246 : : {
5247 : 1350594 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
5248 : 1350594 : DECL_ARTIFICIAL (rdecl) = 1;
5249 : 1350594 : set_global_binding (rdecl);
5250 : 1350594 : if (!decl)
5251 : : decl = rdecl;
5252 : : }
5253 : :
5254 : 3668524 : if (decl)
5255 : : {
5256 : 3379111 : if (!TYPE_NAME (type))
5257 : 2800285 : TYPE_NAME (type) = decl;
5258 : 3379111 : debug_hooks->type_decl (decl, 0);
5259 : : }
5260 : 3957937 : }
5261 : :
5262 : : /* Push a type into the namespace so that the back ends ignore it. */
5263 : :
5264 : : static void
5265 : 289413 : record_unknown_type (tree type, const char* name)
5266 : : {
5267 : 289413 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
5268 : : TYPE_DECL, get_identifier (name), type));
5269 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
5270 : 289413 : DECL_IGNORED_P (decl) = 1;
5271 : 289413 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5272 : 289413 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
5273 : 289413 : SET_TYPE_ALIGN (type, 1);
5274 : 289413 : TYPE_USER_ALIGN (type) = 0;
5275 : 289413 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
5276 : 289413 : }
5277 : :
5278 : : /* Create all the predefined identifiers. */
5279 : :
5280 : : static void
5281 : 96471 : initialize_predefined_identifiers (void)
5282 : : {
5283 : 96471 : struct predefined_identifier
5284 : : {
5285 : : const char *name; /* Name. */
5286 : : tree *node; /* Node to store it in. */
5287 : : cp_identifier_kind kind; /* Kind of identifier. */
5288 : : };
5289 : :
5290 : : /* A table of identifiers to create at startup. */
5291 : 96471 : static const predefined_identifier predefined_identifiers[] = {
5292 : : {"C++", &lang_name_cplusplus, cik_normal},
5293 : : {"C", &lang_name_c, cik_normal},
5294 : : /* Some of these names have a trailing space so that it is
5295 : : impossible for them to conflict with names written by users. */
5296 : : {"__ct ", &ctor_identifier, cik_ctor},
5297 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
5298 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
5299 : : {"__dt ", &dtor_identifier, cik_dtor},
5300 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
5301 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
5302 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
5303 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
5304 : : {"__in_chrg", &in_charge_identifier, cik_normal},
5305 : : {"__as_base ", &as_base_identifier, cik_normal},
5306 : : {"this", &this_identifier, cik_normal},
5307 : : {"__delta", &delta_identifier, cik_normal},
5308 : : {"__pfn", &pfn_identifier, cik_normal},
5309 : : {"_vptr", &vptr_identifier, cik_normal},
5310 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
5311 : : {"::", &global_identifier, cik_normal},
5312 : : /* The demangler expects anonymous namespaces to be called
5313 : : something starting with '_GLOBAL__N_'. It no longer needs
5314 : : to be unique to the TU. */
5315 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
5316 : : {"auto", &auto_identifier, cik_normal},
5317 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
5318 : : {"initializer_list", &init_list_identifier, cik_normal},
5319 : : {"__for_range ", &for_range__identifier, cik_normal},
5320 : : {"__for_begin ", &for_begin__identifier, cik_normal},
5321 : : {"__for_end ", &for_end__identifier, cik_normal},
5322 : : {"__for_range", &for_range_identifier, cik_normal},
5323 : : {"__for_begin", &for_begin_identifier, cik_normal},
5324 : : {"__for_end", &for_end_identifier, cik_normal},
5325 : : {"abi_tag", &abi_tag_identifier, cik_normal},
5326 : : {"aligned", &aligned_identifier, cik_normal},
5327 : : {"begin", &begin_identifier, cik_normal},
5328 : : {"end", &end_identifier, cik_normal},
5329 : : {"get", &get__identifier, cik_normal},
5330 : : {"gnu", &gnu_identifier, cik_normal},
5331 : : {"tuple_element", &tuple_element_identifier, cik_normal},
5332 : : {"tuple_size", &tuple_size_identifier, cik_normal},
5333 : : {"type", &type_identifier, cik_normal},
5334 : : {"value", &value_identifier, cik_normal},
5335 : : {"_FUN", &fun_identifier, cik_normal},
5336 : : {"__closure", &closure_identifier, cik_normal},
5337 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
5338 : : {"heap ", &heap_identifier, cik_normal},
5339 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
5340 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
5341 : : {"heap []", &heap_vec_identifier, cik_normal},
5342 : : {"omp", &omp_identifier, cik_normal},
5343 : : {"internal ", &internal_identifier, cik_normal},
5344 : : {NULL, NULL, cik_normal}
5345 : : };
5346 : :
5347 : 4630608 : for (const predefined_identifier *pid = predefined_identifiers;
5348 : 4630608 : pid->name; ++pid)
5349 : : {
5350 : 4534137 : *pid->node = get_identifier (pid->name);
5351 : : /* Some of these identifiers already have a special kind. */
5352 : 4534137 : if (pid->kind != cik_normal)
5353 : 771768 : set_identifier_kind (*pid->node, pid->kind);
5354 : : }
5355 : 96471 : }
5356 : :
5357 : : /* Build a specific variant of operator new. */
5358 : :
5359 : : static void
5360 : 172079 : cxx_build_operator_new (tree newtype)
5361 : : {
5362 : 172079 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5363 : 172079 : DECL_IS_MALLOC (opnew) = 1;
5364 : 172079 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5365 : 172079 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5366 : 172079 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5367 : 172079 : DECL_IS_MALLOC (opnew) = 1;
5368 : 172079 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5369 : 172079 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5370 : 172079 : }
5371 : :
5372 : : /* Build a specific variant of operator delete. */
5373 : :
5374 : : static void
5375 : 324565 : cxx_build_operator_delete (tree deltype)
5376 : : {
5377 : 324565 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5378 : 324565 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5379 : 324565 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5380 : 324565 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5381 : 324565 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5382 : 324565 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5383 : 324565 : }
5384 : :
5385 : : /* Declare all variants of operator new and delete. */
5386 : :
5387 : : static void
5388 : 96471 : cxx_init_operator_new_delete_decls (void)
5389 : : {
5390 : 96471 : tree newattrs, extvisattr;
5391 : 96471 : tree newtype, deltype;
5392 : 96471 : tree ptr_ftype_sizetype;
5393 : 96471 : tree new_eh_spec;
5394 : 96471 : tree void_ftype_ptr = build_function_type_list (void_type_node,
5395 : : ptr_type_node, NULL_TREE);
5396 : 96471 : void_ftype_ptr
5397 : 96471 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
5398 : :
5399 : 96471 : ptr_ftype_sizetype
5400 : 96471 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5401 : 96471 : if (cxx_dialect == cxx98)
5402 : : {
5403 : 13989 : tree bad_alloc_id;
5404 : 13989 : tree bad_alloc_type_node;
5405 : 13989 : tree bad_alloc_decl;
5406 : :
5407 : 13989 : push_nested_namespace (std_node);
5408 : 13989 : bad_alloc_id = get_identifier ("bad_alloc");
5409 : 13989 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5410 : 13989 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5411 : 13989 : bad_alloc_decl
5412 : 13989 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5413 : 13989 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5414 : 13989 : pop_nested_namespace (std_node);
5415 : :
5416 : 13989 : new_eh_spec
5417 : 13989 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5418 : : }
5419 : : else
5420 : 82482 : new_eh_spec = noexcept_false_spec;
5421 : :
5422 : 96471 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5423 : : NULL_TREE);
5424 : 96471 : newattrs = tree_cons (get_identifier ("alloc_size"),
5425 : : build_tree_list (NULL_TREE, integer_one_node),
5426 : : extvisattr);
5427 : 96471 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5428 : 96471 : newtype = build_exception_variant (newtype, new_eh_spec);
5429 : 96471 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5430 : 96471 : deltype = build_exception_variant (deltype, empty_except_spec);
5431 : 96471 : cxx_build_operator_new (newtype);
5432 : 96471 : cxx_build_operator_delete (deltype);
5433 : 96471 : if (flag_sized_deallocation)
5434 : : {
5435 : : /* Also push the sized deallocation variants:
5436 : : void operator delete(void*, std::size_t) throw();
5437 : : void operator delete[](void*, std::size_t) throw(); */
5438 : 76883 : tree void_ftype_ptr_size
5439 : 76883 : = build_function_type_list (void_type_node, ptr_type_node,
5440 : : size_type_node, NULL_TREE);
5441 : 76883 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5442 : : extvisattr);
5443 : 76883 : deltype = build_exception_variant (deltype, empty_except_spec);
5444 : 76883 : cxx_build_operator_delete (deltype);
5445 : : }
5446 : :
5447 : 96471 : if (aligned_new_threshold)
5448 : : {
5449 : 75608 : push_nested_namespace (std_node);
5450 : 75608 : tree align_id = get_identifier ("align_val_t");
5451 : 75608 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5452 : : NULL_TREE, /*scoped*/true, NULL);
5453 : 75608 : pop_nested_namespace (std_node);
5454 : :
5455 : : /* operator new (size_t, align_val_t); */
5456 : 75608 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5457 : : align_type_node, NULL_TREE);
5458 : 75608 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5459 : 75608 : newtype = build_exception_variant (newtype, new_eh_spec);
5460 : 75608 : cxx_build_operator_new (newtype);
5461 : :
5462 : : /* operator delete (void *, align_val_t); */
5463 : 75608 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5464 : : align_type_node, NULL_TREE);
5465 : 75608 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5466 : 75608 : deltype = build_exception_variant (deltype, empty_except_spec);
5467 : 75608 : cxx_build_operator_delete (deltype);
5468 : :
5469 : 75608 : if (flag_sized_deallocation)
5470 : : {
5471 : : /* operator delete (void *, size_t, align_val_t); */
5472 : 75603 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5473 : : size_type_node, align_type_node,
5474 : : NULL_TREE);
5475 : 75603 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5476 : 75603 : deltype = build_exception_variant (deltype, empty_except_spec);
5477 : 75603 : cxx_build_operator_delete (deltype);
5478 : : }
5479 : : }
5480 : 96471 : }
5481 : :
5482 : : /* Create the predefined scalar types of C,
5483 : : and some nodes representing standard constants (0, 1, (void *)0).
5484 : : Initialize the global binding level.
5485 : : Make definitions for built-in primitive functions. */
5486 : :
5487 : : void
5488 : 96471 : cxx_init_decl_processing (void)
5489 : : {
5490 : 96471 : tree void_ftype;
5491 : :
5492 : : /* Create all the identifiers we need. */
5493 : 96471 : initialize_predefined_identifiers ();
5494 : :
5495 : : /* Create the global variables. */
5496 : 96471 : push_to_top_level ();
5497 : :
5498 : 96471 : current_function_decl = NULL_TREE;
5499 : 96471 : current_binding_level = NULL;
5500 : : /* Enter the global namespace. */
5501 : 96471 : gcc_assert (global_namespace == NULL_TREE);
5502 : 96471 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5503 : : void_type_node);
5504 : 96471 : TREE_PUBLIC (global_namespace) = true;
5505 : 96471 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5506 : 96471 : DECL_CONTEXT (global_namespace)
5507 : 96471 : = build_translation_unit_decl (get_identifier (main_input_filename));
5508 : : /* Remember whether we want the empty class passing ABI change warning
5509 : : in this TU. */
5510 : 96471 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5511 : 122496 : = warn_abi && abi_version_crosses (12);
5512 : 96471 : debug_hooks->register_main_translation_unit
5513 : 96471 : (DECL_CONTEXT (global_namespace));
5514 : 96471 : begin_scope (sk_namespace, global_namespace);
5515 : 96471 : current_namespace = global_namespace;
5516 : :
5517 : 96471 : if (flag_visibility_ms_compat)
5518 : 6 : default_visibility = VISIBILITY_HIDDEN;
5519 : :
5520 : : /* Initially, C. */
5521 : 96471 : current_lang_name = lang_name_c;
5522 : :
5523 : : /* Create the `std' namespace. */
5524 : 96471 : push_namespace (get_identifier ("std"));
5525 : 96471 : std_node = current_namespace;
5526 : 96471 : pop_namespace ();
5527 : :
5528 : 96471 : flag_noexcept_type = (cxx_dialect >= cxx17);
5529 : :
5530 : 96471 : c_common_nodes_and_builtins ();
5531 : :
5532 : 96471 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5533 : 96471 : tree decl
5534 : 96471 : = add_builtin_function ("__builtin_is_constant_evaluated",
5535 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5536 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5537 : 96471 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5538 : :
5539 : : /* The concrete return type of __builtin_source_location is
5540 : : const std::source_location::__impl*, but we can't form the type
5541 : : at this point. So we initially declare it with an auto return
5542 : : type which we then "deduce" from require_deduced_type upon first use. */
5543 : 96471 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5544 : 96471 : decl = add_builtin_function ("__builtin_source_location",
5545 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5546 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5547 : 96471 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5548 : :
5549 : 96471 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5550 : : NULL_TREE);
5551 : 96471 : decl
5552 : 96471 : = add_builtin_function ("__builtin_is_corresponding_member",
5553 : : bool_vaftype,
5554 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5555 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5556 : 96471 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5557 : :
5558 : 96471 : decl
5559 : 96471 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5560 : : bool_vaftype,
5561 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5562 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5563 : 96471 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5564 : :
5565 : 96471 : if (cxx_dialect >= cxx26)
5566 : : {
5567 : 23587 : tree void_ptrintftype
5568 : 23587 : = build_function_type_list (void_type_node, ptr_type_node,
5569 : : integer_type_node, NULL_TREE);
5570 : 23587 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5571 : : void_ptrintftype,
5572 : : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5573 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5574 : 23587 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5575 : : }
5576 : :
5577 : 96471 : integer_two_node = build_int_cst (NULL_TREE, 2);
5578 : :
5579 : : /* Guess at the initial static decls size. */
5580 : 96471 : vec_alloc (static_decls, 500);
5581 : :
5582 : : /* ... and keyed classes. */
5583 : 96471 : vec_alloc (keyed_classes, 100);
5584 : :
5585 : 96471 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5586 : 96471 : truthvalue_type_node = boolean_type_node;
5587 : 96471 : truthvalue_false_node = boolean_false_node;
5588 : 96471 : truthvalue_true_node = boolean_true_node;
5589 : :
5590 : 96471 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5591 : 96471 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5592 : 96471 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5593 : 96471 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5594 : : NULL_TREE);
5595 : :
5596 : : #if 0
5597 : : record_builtin_type (RID_MAX, NULL, string_type_node);
5598 : : #endif
5599 : :
5600 : 96471 : delta_type_node = ptrdiff_type_node;
5601 : 96471 : vtable_index_type = ptrdiff_type_node;
5602 : :
5603 : 96471 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5604 : 96471 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5605 : :
5606 : : /* Create the conversion operator marker. This operator's DECL_NAME
5607 : : is in the identifier table, so we can use identifier equality to
5608 : : find it. */
5609 : 96471 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5610 : : void_ftype);
5611 : :
5612 : : /* C++ extensions */
5613 : :
5614 : 96471 : unknown_type_node = make_node (LANG_TYPE);
5615 : 96471 : record_unknown_type (unknown_type_node, "unknown type");
5616 : :
5617 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5618 : 96471 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5619 : :
5620 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5621 : : result. */
5622 : 96471 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5623 : 96471 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5624 : :
5625 : 96471 : init_list_type_node = make_node (LANG_TYPE);
5626 : 96471 : record_unknown_type (init_list_type_node, "init list");
5627 : :
5628 : : /* Used when parsing to distinguish parameter-lists () and (void). */
5629 : 96471 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5630 : :
5631 : 96471 : {
5632 : : /* Make sure we get a unique function type, so we can give
5633 : : its pointer type a name. (This wins for gdb.) */
5634 : 96471 : tree vfunc_type = make_node (FUNCTION_TYPE);
5635 : 96471 : TREE_TYPE (vfunc_type) = integer_type_node;
5636 : 96471 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5637 : 96471 : layout_type (vfunc_type);
5638 : :
5639 : 96471 : vtable_entry_type = build_pointer_type (vfunc_type);
5640 : : }
5641 : 96471 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5642 : :
5643 : 96471 : vtbl_type_node
5644 : 96471 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5645 : 96471 : layout_type (vtbl_type_node);
5646 : 96471 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5647 : 96471 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5648 : 96471 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5649 : 96471 : layout_type (vtbl_ptr_type_node);
5650 : 96471 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5651 : :
5652 : 96471 : push_namespace (get_identifier ("__cxxabiv1"));
5653 : 96471 : abi_node = current_namespace;
5654 : 96471 : pop_namespace ();
5655 : :
5656 : 96471 : any_targ_node = make_node (LANG_TYPE);
5657 : 96471 : record_unknown_type (any_targ_node, "any type");
5658 : :
5659 : : /* Now, C++. */
5660 : 96471 : current_lang_name = lang_name_cplusplus;
5661 : :
5662 : 96471 : if (aligned_new_threshold > 1
5663 : 96471 : && !pow2p_hwi (aligned_new_threshold))
5664 : : {
5665 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
5666 : : aligned_new_threshold);
5667 : 0 : aligned_new_threshold = 1;
5668 : : }
5669 : 96471 : if (aligned_new_threshold == -1)
5670 : 117320 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5671 : 96471 : if (aligned_new_threshold == 1)
5672 : 75605 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5673 : :
5674 : : /* Ensure attribs.cc is initialized. */
5675 : 96471 : init_attributes ();
5676 : 96471 : cxx_init_operator_new_delete_decls ();
5677 : :
5678 : : /* C++-specific nullptr initialization. */
5679 : 96471 : if (abi_version_at_least (9))
5680 : 96300 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5681 : 96471 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5682 : :
5683 : 96471 : if (! supports_one_only ())
5684 : 0 : flag_weak = 0;
5685 : :
5686 : 96471 : abort_fndecl
5687 : 96471 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5688 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5689 : 96471 : if (flag_weak)
5690 : : /* If no definition is available, resolve references to NULL. */
5691 : 96441 : declare_weak (abort_fndecl);
5692 : :
5693 : : /* Perform other language dependent initializations. */
5694 : 96471 : init_class_processing ();
5695 : 96471 : init_rtti_processing ();
5696 : 96471 : init_template_processing ();
5697 : :
5698 : 96471 : if (flag_exceptions)
5699 : 95231 : init_exception_processing ();
5700 : :
5701 : 96471 : if (flag_contracts)
5702 : 155 : init_terminate_fn ();
5703 : :
5704 : 96471 : if (modules_p ())
5705 : 4489 : init_modules (parse_in);
5706 : :
5707 : 96471 : make_fname_decl = cp_make_fname_decl;
5708 : 96471 : start_fname_decls ();
5709 : :
5710 : : /* Show we use EH for cleanups. */
5711 : 96471 : if (flag_exceptions)
5712 : 95231 : using_eh_for_cleanups ();
5713 : :
5714 : : /* Check that the hardware interference sizes are at least
5715 : : alignof(max_align_t), as required by the standard. */
5716 : 96471 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5717 : 96471 : if (OPTION_SET_P (param_destruct_interfere_size))
5718 : : {
5719 : 0 : if (param_destruct_interfere_size < max_align)
5720 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5721 : : "%d", param_destruct_interfere_size, max_align);
5722 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5723 : 0 : warning (OPT_Winterference_size,
5724 : : "%<--param destructive-interference-size=%d%> "
5725 : : "is less than %<--param l1-cache-line-size=%d%>",
5726 : : param_destruct_interfere_size, param_l1_cache_line_size);
5727 : : }
5728 : 96471 : else if (param_destruct_interfere_size)
5729 : : /* Assume the internal value is OK. */;
5730 : 0 : else if (param_l1_cache_line_size >= max_align)
5731 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5732 : : /* else leave it unset. */
5733 : :
5734 : 96471 : if (OPTION_SET_P (param_construct_interfere_size))
5735 : : {
5736 : 0 : if (param_construct_interfere_size < max_align)
5737 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5738 : : "%d", param_construct_interfere_size, max_align);
5739 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5740 : 0 : && param_l1_cache_line_size >= max_align)
5741 : 0 : warning (OPT_Winterference_size,
5742 : : "%<--param constructive-interference-size=%d%> "
5743 : : "is greater than %<--param l1-cache-line-size=%d%>",
5744 : : param_construct_interfere_size, param_l1_cache_line_size);
5745 : : }
5746 : 96471 : else if (param_construct_interfere_size)
5747 : : /* Assume the internal value is OK. */;
5748 : 0 : else if (param_l1_cache_line_size >= max_align)
5749 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5750 : 96471 : }
5751 : :
5752 : : /* Enter an abi node in global-module context. returns a cookie to
5753 : : give to pop_abi_namespace. */
5754 : :
5755 : : unsigned
5756 : 60927 : push_abi_namespace (tree node)
5757 : : {
5758 : 60927 : push_nested_namespace (node);
5759 : 60927 : push_visibility ("default", 2);
5760 : 60927 : unsigned flags = module_kind;
5761 : 60927 : module_kind = 0;
5762 : 60927 : return flags;
5763 : : }
5764 : :
5765 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5766 : : you. */
5767 : :
5768 : : void
5769 : 60927 : pop_abi_namespace (unsigned flags, tree node)
5770 : : {
5771 : 60927 : module_kind = flags;
5772 : 60927 : pop_visibility (2);
5773 : 60927 : pop_nested_namespace (node);
5774 : 60927 : }
5775 : :
5776 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5777 : : the decl, LOC is the location to give the decl, NAME is the
5778 : : initialization string and TYPE_DEP indicates whether NAME depended
5779 : : on the type of the function. We make use of that to detect
5780 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5781 : : at the point of first use, so we mustn't push the decl now. */
5782 : :
5783 : : static tree
5784 : 116437 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5785 : : {
5786 : 116437 : tree domain = NULL_TREE;
5787 : 116437 : tree init = NULL_TREE;
5788 : :
5789 : 116437 : if (!(type_dep && current_function_decl && in_template_context))
5790 : : {
5791 : 37840 : const char *name = NULL;
5792 : 37840 : bool release_name = false;
5793 : :
5794 : 37840 : if (current_function_decl == NULL_TREE)
5795 : : name = "top level";
5796 : 37826 : else if (type_dep == 0)
5797 : : {
5798 : : /* __FUNCTION__ */
5799 : 620 : name = fname_as_string (type_dep);
5800 : 620 : release_name = true;
5801 : : }
5802 : : else
5803 : : {
5804 : : /* __PRETTY_FUNCTION__ */
5805 : 37206 : gcc_checking_assert (type_dep == 1);
5806 : 37206 : name = cxx_printable_name (current_function_decl, 2);
5807 : : }
5808 : :
5809 : 37826 : if (!release_name)
5810 : : {
5811 : 37220 : cpp_string cstr = { 0, 0 }, strname;
5812 : 37220 : size_t len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5813 : 37220 : char *namep = XNEWVEC (char, len);
5814 : 37220 : snprintf (namep, len, "\"%s\"", name);
5815 : 37220 : strname.text = (unsigned char *) namep;
5816 : 37220 : strname.len = len - 1;
5817 : 37220 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
5818 : : {
5819 : 37220 : name = (const char *) cstr.text;
5820 : 37220 : release_name = true;
5821 : : }
5822 : :
5823 : 37220 : XDELETEVEC (namep);
5824 : : }
5825 : :
5826 : 37840 : size_t length = strlen (name);
5827 : 37840 : domain = build_index_type (size_int (length));
5828 : 37840 : init = build_string (length + 1, name);
5829 : 37840 : if (release_name)
5830 : 37840 : free (const_cast<char *> (name));
5831 : : }
5832 : :
5833 : 116437 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5834 : 116437 : type = build_cplus_array_type (type, domain);
5835 : :
5836 : 116437 : if (init)
5837 : 37840 : TREE_TYPE (init) = type;
5838 : : else
5839 : 78597 : init = error_mark_node;
5840 : :
5841 : 116437 : tree decl = build_decl (loc, VAR_DECL, id, type);
5842 : :
5843 : 116437 : TREE_READONLY (decl) = 1;
5844 : 116437 : DECL_ARTIFICIAL (decl) = 1;
5845 : 116437 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5846 : 116437 : TREE_STATIC (decl) = 1;
5847 : :
5848 : 116437 : TREE_USED (decl) = 1;
5849 : :
5850 : 116437 : SET_DECL_VALUE_EXPR (decl, init);
5851 : 116437 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5852 : : /* For decl_constant_var_p. */
5853 : 116437 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5854 : :
5855 : 116437 : if (current_function_decl)
5856 : : {
5857 : 116423 : DECL_CONTEXT (decl) = current_function_decl;
5858 : 116423 : decl = pushdecl_outermost_localscope (decl);
5859 : 116423 : if (decl != error_mark_node)
5860 : 116420 : add_decl_expr (decl);
5861 : : else
5862 : 3 : gcc_assert (seen_error ());
5863 : : }
5864 : : else
5865 : : {
5866 : 14 : DECL_THIS_STATIC (decl) = true;
5867 : 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5868 : : }
5869 : :
5870 : 116437 : return decl;
5871 : : }
5872 : :
5873 : : /* Install DECL as a builtin function at current global scope. Return
5874 : : the new decl (if we found an existing version). Also installs it
5875 : : into ::std, if it's not '_*'. */
5876 : :
5877 : : tree
5878 : 222813112 : cxx_builtin_function (tree decl)
5879 : : {
5880 : 222813112 : retrofit_lang_decl (decl);
5881 : :
5882 : 222813112 : DECL_ARTIFICIAL (decl) = 1;
5883 : 222813112 : SET_DECL_LANGUAGE (decl, lang_c);
5884 : : /* Runtime library routines are, by definition, available in an
5885 : : external shared object. */
5886 : 222813112 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5887 : 222813112 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5888 : :
5889 : 222813112 : tree id = DECL_NAME (decl);
5890 : 222813112 : const char *name = IDENTIFIER_POINTER (id);
5891 : 222813112 : bool hiding = false;
5892 : 222813112 : if (name[0] != '_' || name[1] != '_')
5893 : : /* In the user's namespace, it must be declared before use. */
5894 : : hiding = true;
5895 : 186816738 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5896 : 186816738 : && !startswith (name + 2, "builtin_")
5897 : 209278782 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5898 : : "_chk", strlen ("_chk") + 1))
5899 : : /* Treat __*_chk fortification functions as anticipated as well,
5900 : : unless they are __builtin_*_chk. */
5901 : : hiding = true;
5902 : :
5903 : : /* All builtins that don't begin with an '_' should additionally
5904 : : go in the 'std' namespace. */
5905 : 222813112 : if (name[0] != '_')
5906 : : {
5907 : 35864224 : tree std_decl = copy_decl (decl);
5908 : :
5909 : 35864224 : push_nested_namespace (std_node);
5910 : 35864224 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5911 : 35864224 : pushdecl (std_decl, hiding);
5912 : 35864224 : pop_nested_namespace (std_node);
5913 : : }
5914 : :
5915 : 222813112 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5916 : 222813112 : decl = pushdecl (decl, hiding);
5917 : :
5918 : 222813112 : return decl;
5919 : : }
5920 : :
5921 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5922 : : scope. This is to allow function specific options to add new machine
5923 : : dependent builtins when the target ISA changes via attribute((target(...)))
5924 : : which saves space on program startup if the program does not use non-generic
5925 : : ISAs. */
5926 : :
5927 : : tree
5928 : 629402 : cxx_builtin_function_ext_scope (tree decl)
5929 : : {
5930 : 629402 : push_nested_namespace (global_namespace);
5931 : 629402 : decl = cxx_builtin_function (decl);
5932 : 629402 : pop_nested_namespace (global_namespace);
5933 : :
5934 : 629402 : return decl;
5935 : : }
5936 : :
5937 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5938 : :
5939 : : tree
5940 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5941 : : {
5942 : 0 : retrofit_lang_decl (decl);
5943 : :
5944 : 0 : DECL_ARTIFICIAL (decl) = 1;
5945 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5946 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5947 : 0 : return pushdecl (decl);
5948 : : }
5949 : :
5950 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5951 : : function. Not called directly. */
5952 : :
5953 : : static tree
5954 : 1555291 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5955 : : int ecf_flags)
5956 : : {
5957 : 1555291 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5958 : 1555291 : DECL_EXTERNAL (fn) = 1;
5959 : 1555291 : TREE_PUBLIC (fn) = 1;
5960 : 1555291 : DECL_ARTIFICIAL (fn) = 1;
5961 : 1555291 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5962 : 1555291 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5963 : 1555291 : SET_DECL_LANGUAGE (fn, lang_c);
5964 : : /* Runtime library routines are, by definition, available in an
5965 : : external shared object. */
5966 : 1555291 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5967 : 1555291 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5968 : 1555291 : set_call_expr_flags (fn, ecf_flags);
5969 : 1555291 : return fn;
5970 : : }
5971 : :
5972 : : /* Returns the _DECL for a library function with C++ linkage. */
5973 : :
5974 : : static tree
5975 : 1088610 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5976 : : int ecf_flags)
5977 : : {
5978 : 1088610 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5979 : 1088610 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5980 : 1088610 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5981 : 1088610 : return fn;
5982 : : }
5983 : :
5984 : : /* Like build_library_fn, but takes a C string instead of an
5985 : : IDENTIFIER_NODE. */
5986 : :
5987 : : tree
5988 : 102687 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5989 : : {
5990 : 102687 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5991 : : }
5992 : :
5993 : : /* Like build_cp_library_fn, but takes a C string instead of an
5994 : : IDENTIFIER_NODE. */
5995 : :
5996 : : tree
5997 : 95322 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5998 : : {
5999 : 95322 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
6000 : 95322 : ecf_flags);
6001 : : }
6002 : :
6003 : : /* Like build_library_fn, but also pushes the function so that we will
6004 : : be able to find it via get_global_binding. Also, the function
6005 : : may throw exceptions listed in RAISES. */
6006 : :
6007 : : tree
6008 : 363994 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
6009 : : {
6010 : 363994 : if (raises)
6011 : 40788 : type = build_exception_variant (type, raises);
6012 : :
6013 : 363994 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
6014 : 363994 : return pushdecl_top_level (fn);
6015 : : }
6016 : :
6017 : : /* Like build_cp_library_fn, but also pushes the function so that it
6018 : : will be found by normal lookup. */
6019 : :
6020 : : static tree
6021 : 993288 : push_cp_library_fn (enum tree_code operator_code, tree type,
6022 : : int ecf_flags)
6023 : : {
6024 : 993288 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
6025 : : operator_code, type, ecf_flags);
6026 : 993288 : pushdecl (fn);
6027 : 993288 : if (flag_tm)
6028 : 3112 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
6029 : 993288 : return fn;
6030 : : }
6031 : :
6032 : : /* Like push_library_fn, but also note that this function throws
6033 : : and does not return. Used for __throw_foo and the like. */
6034 : :
6035 : : tree
6036 : 106380 : push_throw_library_fn (tree name, tree type)
6037 : : {
6038 : 106380 : tree fn = push_library_fn (name, type, NULL_TREE,
6039 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
6040 : 106380 : return fn;
6041 : : }
6042 : :
6043 : : /* When we call finish_struct for an anonymous union, we create
6044 : : default copy constructors and such. But, an anonymous union
6045 : : shouldn't have such things; this function undoes the damage to the
6046 : : anonymous union type T.
6047 : :
6048 : : (The reason that we create the synthesized methods is that we don't
6049 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
6050 : : The first is an anonymous union; the second is just an ordinary
6051 : : union type.) */
6052 : :
6053 : : void
6054 : 105438 : fixup_anonymous_aggr (tree t)
6055 : : {
6056 : : /* Wipe out memory of synthesized methods. */
6057 : 105438 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
6058 : 105438 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6059 : 105438 : TYPE_HAS_COPY_CTOR (t) = 0;
6060 : 105438 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
6061 : 105438 : TYPE_HAS_COPY_ASSIGN (t) = 0;
6062 : 105438 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
6063 : :
6064 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
6065 : : invalid members. */
6066 : 551239 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
6067 : : {
6068 : 445801 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
6069 : 0 : *prev_p = DECL_CHAIN (probe);
6070 : : else
6071 : 445801 : prev_p = &DECL_CHAIN (probe);
6072 : :
6073 : 445801 : if (DECL_ARTIFICIAL (probe)
6074 : 445801 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
6075 : 43156 : || TYPE_ANON_P (TREE_TYPE (probe))))
6076 : 164015 : continue;
6077 : :
6078 : 281786 : if (TREE_CODE (probe) != FIELD_DECL
6079 : 281732 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
6080 : : {
6081 : : /* We already complained about static data members in
6082 : : finish_static_data_member_decl. */
6083 : 66 : if (!VAR_P (probe))
6084 : : {
6085 : 51 : auto_diagnostic_group d;
6086 : 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
6087 : 51 : TREE_CODE (t) == UNION_TYPE
6088 : : ? "%q#D invalid; an anonymous union may "
6089 : : "only have public non-static data members"
6090 : : : "%q#D invalid; an anonymous struct may "
6091 : : "only have public non-static data members", probe))
6092 : : {
6093 : 51 : static bool hint;
6094 : 51 : if (flag_permissive && !hint)
6095 : : {
6096 : 6 : hint = true;
6097 : 6 : inform (DECL_SOURCE_LOCATION (probe),
6098 : : "this flexibility is deprecated and will be "
6099 : : "removed");
6100 : : }
6101 : : }
6102 : 51 : }
6103 : : }
6104 : : }
6105 : :
6106 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
6107 : 105438 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
6108 : 105438 : unsigned store = 0;
6109 : 234054 : for (tree elt : vec)
6110 : 128616 : if (!is_overloaded_fn (elt))
6111 : 128595 : (*vec)[store++] = elt;
6112 : 105438 : vec_safe_truncate (vec, store);
6113 : :
6114 : : /* Wipe RTTI info. */
6115 : 105438 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
6116 : :
6117 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6118 : : assignment operators (because they cannot have these methods themselves).
6119 : : For anonymous unions this is already checked because they are not allowed
6120 : : in any union, otherwise we have to check it. */
6121 : 105438 : if (TREE_CODE (t) != UNION_TYPE)
6122 : : {
6123 : 20125 : tree field, type;
6124 : :
6125 : 20125 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
6126 : : {
6127 : 9 : error_at (location_of (t), "anonymous struct with base classes");
6128 : : /* Avoid ICE after error on anon-struct9.C. */
6129 : 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
6130 : : }
6131 : :
6132 : 90998 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6133 : 70873 : if (TREE_CODE (field) == FIELD_DECL)
6134 : : {
6135 : 50647 : type = TREE_TYPE (field);
6136 : 50647 : if (CLASS_TYPE_P (type))
6137 : : {
6138 : 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
6139 : 6 : error ("member %q+#D with constructor not allowed "
6140 : : "in anonymous aggregate", field);
6141 : 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6142 : 0 : error ("member %q+#D with destructor not allowed "
6143 : : "in anonymous aggregate", field);
6144 : 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
6145 : 0 : error ("member %q+#D with copy assignment operator "
6146 : : "not allowed in anonymous aggregate", field);
6147 : : }
6148 : : }
6149 : : }
6150 : 105438 : }
6151 : :
6152 : : /* Warn for an attribute located at LOCATION that appertains to the
6153 : : class type CLASS_TYPE that has not been properly placed after its
6154 : : class-key, in it class-specifier. */
6155 : :
6156 : : void
6157 : 108 : warn_misplaced_attr_for_class_type (location_t location,
6158 : : tree class_type)
6159 : : {
6160 : 108 : gcc_assert (OVERLOAD_TYPE_P (class_type));
6161 : :
6162 : 108 : auto_diagnostic_group d;
6163 : 108 : if (warning_at (location, OPT_Wattributes,
6164 : : "attribute ignored in declaration "
6165 : : "of %q#T", class_type))
6166 : 108 : inform (location,
6167 : : "attribute for %q#T must follow the %qs keyword",
6168 : : class_type, class_key_or_enum_as_string (class_type));
6169 : 108 : }
6170 : :
6171 : : /* Returns the cv-qualifiers that apply to the type specified
6172 : : by the DECLSPECS. */
6173 : :
6174 : : static int
6175 : 922612844 : get_type_quals (const cp_decl_specifier_seq *declspecs)
6176 : : {
6177 : 922612844 : int type_quals = TYPE_UNQUALIFIED;
6178 : :
6179 : 922612844 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6180 : 100915783 : type_quals |= TYPE_QUAL_CONST;
6181 : 922612844 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6182 : 1571519 : type_quals |= TYPE_QUAL_VOLATILE;
6183 : 922612844 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6184 : 34 : type_quals |= TYPE_QUAL_RESTRICT;
6185 : :
6186 : 922612844 : return type_quals;
6187 : : }
6188 : :
6189 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
6190 : : just declares a tagged type or anonymous union.
6191 : :
6192 : : Returns the type declared; or NULL_TREE if none. */
6193 : :
6194 : : tree
6195 : 32196968 : check_tag_decl (cp_decl_specifier_seq *declspecs,
6196 : : bool explicit_type_instantiation_p)
6197 : : {
6198 : 32196968 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
6199 : 32196968 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
6200 : : /* If a class, struct, or enum type is declared by the DECLSPECS
6201 : : (i.e, if a class-specifier, enum-specifier, or non-typename
6202 : : elaborated-type-specifier appears in the DECLSPECS),
6203 : : DECLARED_TYPE is set to the corresponding type. */
6204 : 32196968 : tree declared_type = NULL_TREE;
6205 : 32196968 : bool error_p = false;
6206 : :
6207 : 32196968 : if (declspecs->multiple_types_p)
6208 : 26 : error_at (smallest_type_location (declspecs),
6209 : : "multiple types in one declaration");
6210 : 32196942 : else if (declspecs->redefined_builtin_type)
6211 : : {
6212 : 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
6213 : 21 : if (!in_system_header_at (loc))
6214 : 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
6215 : : declspecs->redefined_builtin_type);
6216 : 21 : return NULL_TREE;
6217 : : }
6218 : :
6219 : 32196947 : if (declspecs->type
6220 : 32196932 : && TYPE_P (declspecs->type)
6221 : 64200860 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
6222 : 32002361 : && MAYBE_CLASS_TYPE_P (declspecs->type))
6223 : 1655323 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
6224 : : declared_type = declspecs->type;
6225 : 194628 : else if (declspecs->type == error_mark_node)
6226 : 1468 : error_p = true;
6227 : :
6228 : 32196947 : if (type_uses_auto (declared_type))
6229 : : {
6230 : 8 : error_at (declspecs->locations[ds_type_spec],
6231 : : "%<auto%> can only be specified for variables "
6232 : : "or function declarations");
6233 : 8 : return error_mark_node;
6234 : : }
6235 : :
6236 : 32196939 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
6237 : : declared_type = NULL_TREE;
6238 : :
6239 : 32196939 : if (!declared_type && !saw_friend && !error_p)
6240 : 88 : permerror (input_location, "declaration does not declare anything");
6241 : : /* Check for an anonymous union. */
6242 : 32002284 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
6243 : 92999405 : && TYPE_UNNAMED_P (declared_type))
6244 : : {
6245 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
6246 : : can be omitted only when declaring a class (clause 9) or
6247 : : enumeration (7.2), that is, when the decl-specifier-seq contains
6248 : : either a class-specifier, an elaborated-type-specifier with
6249 : : a class-key (9.1), or an enum-specifier. In these cases and
6250 : : whenever a class-specifier or enum-specifier is present in the
6251 : : decl-specifier-seq, the identifiers in these specifiers are among
6252 : : the names being declared by the declaration (as class-name,
6253 : : enum-names, or enumerators, depending on the syntax). In such
6254 : : cases, and except for the declaration of an unnamed bit-field (9.6),
6255 : : the decl-specifier-seq shall introduce one or more names into the
6256 : : program, or shall redeclare a name introduced by a previous
6257 : : declaration. [Example:
6258 : : enum { }; // ill-formed
6259 : : typedef class { }; // ill-formed
6260 : : --end example] */
6261 : 105444 : if (saw_typedef)
6262 : : {
6263 : 6 : error_at (declspecs->locations[ds_typedef],
6264 : : "missing type-name in typedef-declaration");
6265 : 6 : return NULL_TREE;
6266 : : }
6267 : 105438 : /* Anonymous unions are objects, so they can have specifiers. */;
6268 : 105438 : SET_ANON_AGGR_TYPE_P (declared_type);
6269 : :
6270 : 105438 : if (TREE_CODE (declared_type) != UNION_TYPE)
6271 : 20125 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
6272 : 20125 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
6273 : : }
6274 : :
6275 : : else
6276 : : {
6277 : 32091407 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
6278 : 9 : error_at (declspecs->locations[ds_inline],
6279 : : "%<inline%> can only be specified for functions");
6280 : 32091398 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
6281 : 3 : error_at (declspecs->locations[ds_virtual],
6282 : : "%<virtual%> can only be specified for functions");
6283 : 32091395 : else if (saw_friend
6284 : 32091395 : && (!current_class_type
6285 : 1661665 : || current_scope () != current_class_type))
6286 : 0 : error_at (declspecs->locations[ds_friend],
6287 : : "%<friend%> can only be specified inside a class");
6288 : 32091395 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
6289 : 3 : error_at (declspecs->locations[ds_explicit],
6290 : : "%<explicit%> can only be specified for constructors");
6291 : 32091392 : else if (declspecs->storage_class)
6292 : 3 : error_at (declspecs->locations[ds_storage_class],
6293 : : "a storage class can only be specified for objects "
6294 : : "and functions");
6295 : 32091389 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
6296 : 9 : error_at (declspecs->locations[ds_const],
6297 : : "%<const%> can only be specified for objects and "
6298 : : "functions");
6299 : 32091380 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
6300 : 3 : error_at (declspecs->locations[ds_volatile],
6301 : : "%<volatile%> can only be specified for objects and "
6302 : : "functions");
6303 : 32091377 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
6304 : 3 : error_at (declspecs->locations[ds_restrict],
6305 : : "%<__restrict%> can only be specified for objects and "
6306 : : "functions");
6307 : 32091374 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
6308 : 3 : error_at (declspecs->locations[ds_thread],
6309 : : "%<__thread%> can only be specified for objects "
6310 : : "and functions");
6311 : 32091371 : else if (saw_typedef)
6312 : 24 : warning_at (declspecs->locations[ds_typedef], 0,
6313 : : "%<typedef%> was ignored in this declaration");
6314 : 32091347 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
6315 : 9 : error_at (declspecs->locations[ds_constexpr],
6316 : : "%qs cannot be used for type declarations", "constexpr");
6317 : 32091338 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
6318 : 3 : error_at (declspecs->locations[ds_constinit],
6319 : : "%qs cannot be used for type declarations", "constinit");
6320 : 32091335 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
6321 : 3 : error_at (declspecs->locations[ds_consteval],
6322 : : "%qs cannot be used for type declarations", "consteval");
6323 : : }
6324 : :
6325 : 32196933 : if (declspecs->attributes && warn_attributes && declared_type)
6326 : : {
6327 : 31 : location_t loc;
6328 : 25 : if (!CLASS_TYPE_P (declared_type)
6329 : 56 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
6330 : : /* For a non-template class, use the name location. */
6331 : 22 : loc = location_of (declared_type);
6332 : : else
6333 : : /* For a template class (an explicit instantiation), use the
6334 : : current location. */
6335 : 9 : loc = input_location;
6336 : :
6337 : 31 : if (explicit_type_instantiation_p)
6338 : : /* [dcl.attr.grammar]/4:
6339 : :
6340 : : No attribute-specifier-seq shall appertain to an explicit
6341 : : instantiation. */
6342 : : {
6343 : 6 : auto_diagnostic_group d;
6344 : 6 : if (warning_at (loc, OPT_Wattributes,
6345 : : "attribute ignored in explicit instantiation %q#T",
6346 : : declared_type))
6347 : 6 : inform (loc,
6348 : : "no attribute can be applied to "
6349 : : "an explicit instantiation");
6350 : 6 : }
6351 : : else
6352 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
6353 : : }
6354 : :
6355 : 32196933 : if (declspecs->std_attributes
6356 : 83 : && declared_type
6357 : 32197013 : && any_nonignored_attribute_p (declspecs->std_attributes))
6358 : : {
6359 : 80 : auto_diagnostic_group d;
6360 : 80 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
6361 : : "attribute ignored"))
6362 : 80 : inform (declspecs->locations[ds_std_attribute],
6363 : : "an attribute that appertains to a type-specifier is ignored");
6364 : 80 : }
6365 : :
6366 : : /* Diagnose invalid application of contracts, if any. */
6367 : 32196933 : if (find_contract (declspecs->attributes))
6368 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
6369 : : else
6370 : 32196932 : diagnose_misapplied_contracts (declspecs->std_attributes);
6371 : :
6372 : : return declared_type;
6373 : : }
6374 : :
6375 : : /* Called when a declaration is seen that contains no names to declare.
6376 : : If its type is a reference to a structure, union or enum inherited
6377 : : from a containing scope, shadow that tag name for the current scope
6378 : : with a forward reference.
6379 : : If its type defines a new named structure or union
6380 : : or defines an enum, it is valid but we need not do anything here.
6381 : : Otherwise, it is an error.
6382 : :
6383 : : C++: may have to grok the declspecs to learn about static,
6384 : : complain for anonymous unions.
6385 : :
6386 : : Returns the TYPE declared -- or NULL_TREE if none. */
6387 : :
6388 : : tree
6389 : 28183431 : shadow_tag (cp_decl_specifier_seq *declspecs)
6390 : : {
6391 : 28183431 : tree t = check_tag_decl (declspecs,
6392 : : /*explicit_type_instantiation_p=*/false);
6393 : :
6394 : 28183431 : if (!t)
6395 : : return NULL_TREE;
6396 : :
6397 : 28181953 : t = maybe_process_partial_specialization (t);
6398 : 28181953 : if (t == error_mark_node)
6399 : : return NULL_TREE;
6400 : :
6401 : : /* This is where the variables in an anonymous union are
6402 : : declared. An anonymous union declaration looks like:
6403 : : union { ... } ;
6404 : : because there is no declarator after the union, the parser
6405 : : sends that declaration here. */
6406 : 28180723 : if (ANON_AGGR_TYPE_P (t))
6407 : : {
6408 : 249 : fixup_anonymous_aggr (t);
6409 : :
6410 : 249 : if (TYPE_FIELDS (t))
6411 : : {
6412 : 249 : tree decl = grokdeclarator (/*declarator=*/NULL,
6413 : : declspecs, NORMAL, 0, NULL);
6414 : 249 : finish_anon_union (decl);
6415 : : }
6416 : : }
6417 : :
6418 : : return t;
6419 : : }
6420 : :
6421 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
6422 : :
6423 : : tree
6424 : 341553815 : groktypename (cp_decl_specifier_seq *type_specifiers,
6425 : : const cp_declarator *declarator,
6426 : : bool is_template_arg)
6427 : : {
6428 : 341553815 : tree attrs;
6429 : 341553815 : tree type;
6430 : 53918263 : enum decl_context context
6431 : 341553815 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
6432 : 341553815 : attrs = type_specifiers->attributes;
6433 : 341553815 : type_specifiers->attributes = NULL_TREE;
6434 : 341553815 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
6435 : 341553815 : if (attrs && type != error_mark_node)
6436 : : {
6437 : 600 : if (CLASS_TYPE_P (type))
6438 : 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
6439 : : "outside of definition", type);
6440 : 594 : else if (MAYBE_CLASS_TYPE_P (type))
6441 : : /* A template type parameter or other dependent type. */
6442 : 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
6443 : : "type %qT without an associated declaration", type);
6444 : : else
6445 : 585 : cplus_decl_attributes (&type, attrs, 0);
6446 : : }
6447 : 341553815 : return type;
6448 : : }
6449 : :
6450 : : /* Process a DECLARATOR for a function-scope or namespace-scope
6451 : : variable or function declaration.
6452 : : (Function definitions go through start_function; class member
6453 : : declarations appearing in the body of the class go through
6454 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
6455 : : If an error occurs, the error_mark_node is returned instead.
6456 : :
6457 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
6458 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
6459 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
6460 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
6461 : : implicitly initialized via a default constructor. It can also be
6462 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
6463 : : mark the new decl as DECL_DECOMPOSITION_P.
6464 : :
6465 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
6466 : : declaration.
6467 : :
6468 : : The scope represented by the context of the returned DECL is pushed
6469 : : (if it is not the global namespace) and is assigned to
6470 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
6471 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
6472 : :
6473 : : tree
6474 : 114707382 : start_decl (const cp_declarator *declarator,
6475 : : cp_decl_specifier_seq *declspecs,
6476 : : int initialized,
6477 : : tree attributes,
6478 : : tree prefix_attributes,
6479 : : tree *pushed_scope_p)
6480 : : {
6481 : 114707382 : tree decl;
6482 : 114707382 : tree context;
6483 : 114707382 : bool was_public;
6484 : 114707382 : int flags;
6485 : 114707382 : bool alias;
6486 : 114707382 : tree initial;
6487 : :
6488 : 114707382 : *pushed_scope_p = NULL_TREE;
6489 : :
6490 : 114707382 : if (prefix_attributes != error_mark_node)
6491 : 114707380 : attributes = attr_chainon (attributes, prefix_attributes);
6492 : :
6493 : 114707382 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6494 : : &attributes);
6495 : :
6496 : 114707382 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6497 : 114707382 : || decl == error_mark_node
6498 : 114705247 : || prefix_attributes == error_mark_node)
6499 : 2137 : return error_mark_node;
6500 : :
6501 : 114705245 : context = CP_DECL_CONTEXT (decl);
6502 : 114705245 : if (context != global_namespace)
6503 : 17765209 : *pushed_scope_p = push_scope (context);
6504 : :
6505 : 114705245 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6506 : : {
6507 : 27 : error_at (DECL_SOURCE_LOCATION (decl),
6508 : : "typedef %qD is initialized (use %qs instead)",
6509 : : decl, "decltype");
6510 : 27 : return error_mark_node;
6511 : : }
6512 : :
6513 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6514 : : with attribute validation. */
6515 : 114705218 : initial = DECL_INITIAL (decl);
6516 : :
6517 : 114705218 : if (initialized)
6518 : : {
6519 : 50469820 : if (! toplevel_bindings_p ()
6520 : 50469820 : && DECL_EXTERNAL (decl))
6521 : 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6522 : : decl);
6523 : 50469820 : DECL_EXTERNAL (decl) = 0;
6524 : 50469820 : if (toplevel_bindings_p ())
6525 : 4409044 : TREE_STATIC (decl) = 1;
6526 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6527 : : even though we might not yet have the initializer expression. */
6528 : 50469820 : if (!DECL_INITIAL (decl))
6529 : 50119711 : DECL_INITIAL (decl) = error_mark_node;
6530 : : }
6531 : 114705218 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6532 : :
6533 : 114705218 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6534 : 3865 : record_key_method_defined (decl);
6535 : :
6536 : : /* If this is a typedef that names the class for linkage purposes
6537 : : (7.1.3p8), apply any attributes directly to the type. */
6538 : 114705218 : if (TREE_CODE (decl) == TYPE_DECL
6539 : 20104176 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6540 : 122595387 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6541 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
6542 : : else
6543 : : flags = 0;
6544 : :
6545 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
6546 : 114705218 : cplus_decl_attributes (&decl, attributes, flags);
6547 : :
6548 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6549 : : assist with attribute validation. */
6550 : 114705218 : DECL_INITIAL (decl) = initial;
6551 : :
6552 : : /* Dllimported symbols cannot be defined. Static data members (which
6553 : : can be initialized in-class and dllimported) go through grokfield,
6554 : : not here, so we don't need to exclude those decls when checking for
6555 : : a definition. */
6556 : 114705218 : if (initialized && DECL_DLLIMPORT_P (decl))
6557 : : {
6558 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
6559 : : "definition of %q#D is marked %<dllimport%>", decl);
6560 : 0 : DECL_DLLIMPORT_P (decl) = 0;
6561 : : }
6562 : :
6563 : : /* If #pragma weak was used, mark the decl weak now. */
6564 : 114705218 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6565 : 44075897 : maybe_apply_pragma_weak (decl);
6566 : :
6567 : 114705218 : if (TREE_CODE (decl) == FUNCTION_DECL
6568 : 38226518 : && DECL_DECLARED_INLINE_P (decl)
6569 : 1975726 : && DECL_UNINLINABLE (decl)
6570 : 114705218 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6571 : : {
6572 : 0 : auto_urlify_attributes sentinel;
6573 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6574 : : "inline function %qD given attribute %qs", decl, "noinline");
6575 : 0 : }
6576 : :
6577 : 114705218 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6578 : : {
6579 : 755576 : bool this_tmpl = (current_template_depth
6580 : 755576 : > template_class_depth (context));
6581 : 755576 : if (VAR_P (decl))
6582 : : {
6583 : 387477 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6584 : 387477 : if (field == NULL_TREE
6585 : 387477 : || !(VAR_P (field) || variable_template_p (field)))
6586 : 7 : error ("%q+#D is not a static data member of %q#T", decl, context);
6587 : 387470 : else if (variable_template_p (field)
6588 : 387470 : && (DECL_LANG_SPECIFIC (decl)
6589 : 111 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6590 : : /* OK, specialization was already checked. */;
6591 : 387407 : else if (variable_template_p (field) && !this_tmpl)
6592 : : {
6593 : 3 : auto_diagnostic_group d;
6594 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6595 : : "non-member-template declaration of %qD", decl);
6596 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6597 : : "member template declaration here");
6598 : 3 : return error_mark_node;
6599 : 3 : }
6600 : : else
6601 : : {
6602 : 387404 : if (variable_template_p (field))
6603 : 45 : field = DECL_TEMPLATE_RESULT (field);
6604 : :
6605 : 387404 : if (DECL_CONTEXT (field) != context)
6606 : : {
6607 : 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6608 : 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6609 : : "to be defined as %<%T::%D%>",
6610 : 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6611 : 3 : context, DECL_NAME (decl));
6612 : 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6613 : : }
6614 : : /* Static data member are tricky; an in-class initialization
6615 : : still doesn't provide a definition, so the in-class
6616 : : declaration will have DECL_EXTERNAL set, but will have an
6617 : : initialization. Thus, duplicate_decls won't warn
6618 : : about this situation, and so we check here. */
6619 : 387404 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6620 : 9 : error ("duplicate initialization of %qD", decl);
6621 : 387404 : field = duplicate_decls (decl, field);
6622 : 387404 : if (field == error_mark_node)
6623 : : return error_mark_node;
6624 : 387348 : else if (field)
6625 : 387348 : decl = field;
6626 : : }
6627 : : }
6628 : : else
6629 : : {
6630 : 368112 : tree field = check_classfn (context, decl,
6631 : : this_tmpl
6632 : 13 : ? current_template_parms
6633 : : : NULL_TREE);
6634 : 368027 : if (field && field != error_mark_node
6635 : 736126 : && duplicate_decls (decl, field))
6636 : 368026 : decl = field;
6637 : : }
6638 : :
6639 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6640 : 755517 : DECL_IN_AGGR_P (decl) = 0;
6641 : : /* Do not mark DECL as an explicit specialization if it was not
6642 : : already marked as an instantiation; a declaration should
6643 : : never be marked as a specialization unless we know what
6644 : : template is being specialized. */
6645 : 755517 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6646 : : {
6647 : 366515 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6648 : 366515 : if (TREE_CODE (decl) == FUNCTION_DECL)
6649 : 345268 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6650 : 345268 : && DECL_DECLARED_INLINE_P (decl));
6651 : : else
6652 : 21247 : DECL_COMDAT (decl) = false;
6653 : :
6654 : : /* [temp.expl.spec] An explicit specialization of a static data
6655 : : member of a template is a definition if the declaration
6656 : : includes an initializer; otherwise, it is a declaration.
6657 : :
6658 : : We check for processing_specialization so this only applies
6659 : : to the new specialization syntax. */
6660 : 366515 : if (!initialized && processing_specialization)
6661 : 366322 : DECL_EXTERNAL (decl) = 1;
6662 : : }
6663 : :
6664 : 1121908 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6665 : : /* Aliases are definitions. */
6666 : 755574 : && !alias)
6667 : : {
6668 : 30 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6669 : 11 : permerror (declarator->id_loc,
6670 : : "declaration of %q#D outside of class is not definition",
6671 : : decl);
6672 : 19 : else if (flag_contract_strict_declarations)
6673 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6674 : : "declaration of %q#D outside of class is not definition",
6675 : : decl);
6676 : : }
6677 : : }
6678 : :
6679 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6680 : 114705159 : if (initialized == SD_DECOMPOSITION)
6681 : 173653 : fit_decomposition_lang_decl (decl, NULL_TREE);
6682 : :
6683 : 114705159 : was_public = TREE_PUBLIC (decl);
6684 : :
6685 : 190953890 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6686 : 115055286 : && current_function_decl)
6687 : : {
6688 : : /* A function-scope decl of some namespace-scope decl. */
6689 : 39989 : DECL_LOCAL_DECL_P (decl) = true;
6690 : 39989 : if (named_module_attach_p ())
6691 : 3 : error_at (declarator->id_loc,
6692 : : "block-scope extern declaration %q#D must not be"
6693 : : " attached to a named module", decl);
6694 : : }
6695 : :
6696 : : /* Enter this declaration into the symbol table. Don't push the plain
6697 : : VAR_DECL for a variable template. */
6698 : 114705159 : if (!template_parm_scope_p ()
6699 : 114705159 : || !VAR_P (decl))
6700 : 111861094 : decl = maybe_push_decl (decl);
6701 : :
6702 : 114705159 : if (processing_template_decl)
6703 : 70622938 : decl = push_template_decl (decl);
6704 : :
6705 : 114705159 : if (decl == error_mark_node)
6706 : : return error_mark_node;
6707 : :
6708 : 114704730 : if (VAR_P (decl)
6709 : 56374218 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6710 : 66106 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6711 : : /* But not templated variables. */
6712 : 114732155 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6713 : : {
6714 : : /* This is a const variable with implicit 'static'. Set
6715 : : DECL_THIS_STATIC so we can tell it from variables that are
6716 : : !TREE_PUBLIC because of the anonymous namespace. */
6717 : 27425 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6718 : 27425 : DECL_THIS_STATIC (decl) = 1;
6719 : : }
6720 : :
6721 : 62717773 : if (current_function_decl && VAR_P (decl)
6722 : 51018390 : && maybe_constexpr_fn (current_function_decl)
6723 : 120866581 : && cxx_dialect < cxx23)
6724 : : {
6725 : 2882121 : bool ok = false;
6726 : 2882121 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6727 : : {
6728 : 10 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6729 : 20 : error_at (DECL_SOURCE_LOCATION (decl),
6730 : : "%qD defined %<thread_local%> in %qs function only "
6731 : : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6732 : 10 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6733 : : ? "consteval" : "constexpr");
6734 : : }
6735 : 2882111 : else if (TREE_STATIC (decl))
6736 : : {
6737 : 52 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6738 : 76 : error_at (DECL_SOURCE_LOCATION (decl),
6739 : : "%qD defined %<static%> in %qs function only available "
6740 : : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6741 : 38 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6742 : : ? "consteval" : "constexpr");
6743 : : }
6744 : : else
6745 : : ok = true;
6746 : 48 : if (!ok)
6747 : 62 : cp_function_chain->invalid_constexpr = true;
6748 : : }
6749 : :
6750 : 114704730 : if (!processing_template_decl && VAR_P (decl))
6751 : 5491550 : start_decl_1 (decl, initialized);
6752 : :
6753 : 114704727 : return decl;
6754 : : }
6755 : :
6756 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6757 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6758 : : variable is initialized via an implicitly-called constructor.)
6759 : : This function must be called for ordinary variables (including, for
6760 : : example, implicit instantiations of templates), but must not be
6761 : : called for template declarations. */
6762 : :
6763 : : void
6764 : 5500370 : start_decl_1 (tree decl, bool initialized)
6765 : : {
6766 : 5500370 : gcc_checking_assert (!processing_template_decl);
6767 : :
6768 : 5500370 : if (error_operand_p (decl))
6769 : : return;
6770 : :
6771 : 5500355 : gcc_checking_assert (VAR_P (decl));
6772 : :
6773 : 5500355 : tree type = TREE_TYPE (decl);
6774 : 5500355 : bool complete_p = COMPLETE_TYPE_P (type);
6775 : 5500355 : bool aggregate_definition_p
6776 : 5500355 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6777 : :
6778 : : /* If an explicit initializer is present, or if this is a definition
6779 : : of an aggregate, then we need a complete type at this point.
6780 : : (Scalars are always complete types, so there is nothing to
6781 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6782 : : are issued below. */
6783 : 3439998 : if ((initialized || aggregate_definition_p)
6784 : 4472730 : && !complete_p
6785 : 4392209 : && COMPLETE_TYPE_P (complete_type (type)))
6786 : : {
6787 : 337571 : complete_p = true;
6788 : : /* We will not yet have set TREE_READONLY on DECL if the type
6789 : : was "const", but incomplete, before this point. But, now, we
6790 : : have a complete type, so we can try again. */
6791 : 337571 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6792 : : }
6793 : :
6794 : 5500352 : if (initialized)
6795 : : /* Is it valid for this decl to have an initializer at all? */
6796 : : {
6797 : : /* Don't allow initializations for incomplete types except for
6798 : : arrays which might be completed by the initialization. */
6799 : 3839449 : if (complete_p)
6800 : : ; /* A complete type is ok. */
6801 : 614411 : else if (type_uses_auto (type))
6802 : : ; /* An auto type is ok. */
6803 : 64640 : else if (TREE_CODE (type) != ARRAY_TYPE)
6804 : : {
6805 : 33 : auto_diagnostic_group d;
6806 : 33 : error ("variable %q#D has initializer but incomplete type", decl);
6807 : 60 : maybe_suggest_missing_header (input_location,
6808 : 33 : TYPE_IDENTIFIER (type),
6809 : 33 : CP_TYPE_CONTEXT (type));
6810 : 33 : type = TREE_TYPE (decl) = error_mark_node;
6811 : 33 : }
6812 : 64607 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6813 : : {
6814 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6815 : 0 : error ("elements of array %q#D have incomplete type", decl);
6816 : : /* else we already gave an error in start_decl. */
6817 : : }
6818 : : }
6819 : 1660903 : else if (aggregate_definition_p && !complete_p)
6820 : : {
6821 : 226 : if (type_uses_auto (type))
6822 : 64 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6823 : : else
6824 : : {
6825 : 162 : auto_diagnostic_group d;
6826 : 162 : error ("aggregate %q#D has incomplete type and cannot be defined",
6827 : : decl);
6828 : 279 : maybe_suggest_missing_header (input_location,
6829 : 162 : TYPE_IDENTIFIER (type),
6830 : 162 : CP_TYPE_CONTEXT (type));
6831 : : /* Change the type so that assemble_variable will give
6832 : : DECL an rtl we can live with: (mem (const_int 0)). */
6833 : 162 : type = TREE_TYPE (decl) = error_mark_node;
6834 : 162 : }
6835 : : }
6836 : :
6837 : : /* Create a new scope to hold this declaration if necessary.
6838 : : Whether or not a new scope is necessary cannot be determined
6839 : : until after the type has been completed; if the type is a
6840 : : specialization of a class template it is not until after
6841 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6842 : : will be set correctly. */
6843 : 5500352 : maybe_push_cleanup_level (type);
6844 : : }
6845 : :
6846 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6847 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6848 : :
6849 : : tree
6850 : 239 : do_aggregate_paren_init (tree init, tree type)
6851 : : {
6852 : 239 : tree val = TREE_VALUE (init);
6853 : :
6854 : 239 : if (TREE_CHAIN (init) == NULL_TREE)
6855 : : {
6856 : : /* If the list has a single element and it's a string literal,
6857 : : then it's the initializer for the array as a whole. */
6858 : 124 : if (TREE_CODE (type) == ARRAY_TYPE
6859 : 124 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6860 : 155 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6861 : : == STRING_CST)
6862 : : return val;
6863 : : /* Handle non-standard extensions like compound literals. This also
6864 : : prevents triggering aggregate parenthesized-initialization in
6865 : : compiler-generated code for =default. */
6866 : 97 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6867 : 97 : TREE_TYPE (val)))
6868 : : return val;
6869 : : }
6870 : :
6871 : 187 : init = build_constructor_from_list (init_list_type_node, init);
6872 : 187 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6873 : 187 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6874 : 187 : return init;
6875 : : }
6876 : :
6877 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6878 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6879 : : but will be set to a new CLEANUP_STMT if a temporary is created
6880 : : that must be destroyed subsequently.
6881 : :
6882 : : Returns an initializer expression to use to initialize DECL, or
6883 : : NULL if the initialization can be performed statically.
6884 : :
6885 : : Quotes on semantics can be found in ARM 8.4.3. */
6886 : :
6887 : : static tree
6888 : 320569 : grok_reference_init (tree decl, tree type, tree init, int flags)
6889 : : {
6890 : 320569 : if (init == NULL_TREE)
6891 : : {
6892 : 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6893 : 3 : || DECL_IN_AGGR_P (decl) == 0)
6894 : 18 : && ! DECL_THIS_EXTERN (decl))
6895 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
6896 : : "%qD declared as reference but not initialized", decl);
6897 : 15 : return NULL_TREE;
6898 : : }
6899 : :
6900 : 320554 : tree ttype = TREE_TYPE (type);
6901 : 320554 : if (TREE_CODE (init) == TREE_LIST)
6902 : : {
6903 : : /* This handles (C++20 only) code like
6904 : :
6905 : : const A& r(1, 2, 3);
6906 : :
6907 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6908 : 1247 : if (TREE_TYPE (init) == NULL_TREE
6909 : 1247 : && CP_AGGREGATE_TYPE_P (ttype)
6910 : 62 : && !DECL_DECOMPOSITION_P (decl)
6911 : 1306 : && (cxx_dialect >= cxx20))
6912 : : {
6913 : : /* We don't know yet if we should treat const A& r(1) as
6914 : : const A& r{1}. */
6915 : 33 : if (list_length (init) == 1)
6916 : : {
6917 : 12 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6918 : 12 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6919 : : tf_warning_or_error);
6920 : : }
6921 : : /* If the list had more than one element, the code is ill-formed
6922 : : pre-C++20, so we can build a constructor right away. */
6923 : : else
6924 : 21 : init = do_aggregate_paren_init (init, ttype);
6925 : : }
6926 : : else
6927 : 1214 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6928 : : tf_warning_or_error);
6929 : : }
6930 : :
6931 : 320554 : if (TREE_CODE (ttype) != ARRAY_TYPE
6932 : 320554 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6933 : : /* Note: default conversion is only called in very special cases. */
6934 : 12 : init = decay_conversion (init, tf_warning_or_error);
6935 : :
6936 : : /* check_initializer handles this for non-reference variables, but for
6937 : : references we need to do it here or the initializer will get the
6938 : : incomplete array type and confuse later calls to
6939 : : cp_complete_array_type. */
6940 : 320554 : if (TREE_CODE (ttype) == ARRAY_TYPE
6941 : 8621 : && TYPE_DOMAIN (ttype) == NULL_TREE
6942 : 320637 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6943 : 50 : || TREE_CODE (init) == STRING_CST))
6944 : : {
6945 : 33 : cp_complete_array_type (&ttype, init, false);
6946 : 33 : if (ttype != TREE_TYPE (type))
6947 : 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6948 : : }
6949 : :
6950 : : /* Convert INIT to the reference type TYPE. This may involve the
6951 : : creation of a temporary, whose lifetime must be the same as that
6952 : : of the reference. If so, a DECL_EXPR for the temporary will be
6953 : : added just after the DECL_EXPR for DECL. That's why we don't set
6954 : : DECL_INITIAL for local references (instead assigning to them
6955 : : explicitly); we need to allow the temporary to be initialized
6956 : : first. */
6957 : 320554 : return initialize_reference (type, init, flags,
6958 : 320554 : tf_warning_or_error);
6959 : : }
6960 : :
6961 : : /* Designated initializers in arrays are not supported in GNU C++.
6962 : : The parser cannot detect this error since it does not know whether
6963 : : a given brace-enclosed initializer is for a class type or for an
6964 : : array. This function checks that CE does not use a designated
6965 : : initializer. If it does, an error is issued. Returns true if CE
6966 : : is valid, i.e., does not have a designated initializer. */
6967 : :
6968 : : bool
6969 : 51216428 : check_array_designated_initializer (constructor_elt *ce,
6970 : : unsigned HOST_WIDE_INT index)
6971 : : {
6972 : : /* Designated initializers for array elements are not supported. */
6973 : 51216428 : if (ce->index)
6974 : : {
6975 : : /* The parser only allows identifiers as designated
6976 : : initializers. */
6977 : 33365047 : if (ce->index == error_mark_node)
6978 : : {
6979 : 0 : error ("name used in a GNU-style designated "
6980 : : "initializer for an array");
6981 : 0 : return false;
6982 : : }
6983 : 33365047 : else if (identifier_p (ce->index))
6984 : : {
6985 : 17 : error ("name %qD used in a GNU-style designated "
6986 : : "initializer for an array", ce->index);
6987 : 17 : return false;
6988 : : }
6989 : :
6990 : 33365030 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6991 : : ce->index, true);
6992 : 33365030 : if (ce_index
6993 : 33365027 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6994 : 66730054 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6995 : : == INTEGER_CST))
6996 : : {
6997 : : /* A C99 designator is OK if it matches the current index. */
6998 : 33365024 : if (wi::to_wide (ce_index) == index)
6999 : : {
7000 : 33365018 : ce->index = ce_index;
7001 : 33365018 : return true;
7002 : : }
7003 : : else
7004 : 6 : sorry ("non-trivial designated initializers not supported");
7005 : : }
7006 : : else
7007 : 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
7008 : : "C99 designator %qE is not an integral constant-expression",
7009 : : ce->index);
7010 : :
7011 : 12 : return false;
7012 : : }
7013 : :
7014 : : return true;
7015 : : }
7016 : :
7017 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
7018 : : array until we finish parsing the initializer. If that's the
7019 : : situation we're in, update DECL accordingly. */
7020 : :
7021 : : static void
7022 : 57093126 : maybe_deduce_size_from_array_init (tree decl, tree init)
7023 : : {
7024 : 57093126 : tree type = TREE_TYPE (decl);
7025 : :
7026 : 57093126 : if (TREE_CODE (type) == ARRAY_TYPE
7027 : 807207 : && TYPE_DOMAIN (type) == NULL_TREE
7028 : 57618830 : && TREE_CODE (decl) != TYPE_DECL)
7029 : : {
7030 : : /* do_default is really a C-ism to deal with tentative definitions.
7031 : : But let's leave it here to ease the eventual merge. */
7032 : 525704 : int do_default = !DECL_EXTERNAL (decl);
7033 : 525704 : tree initializer = init ? init : DECL_INITIAL (decl);
7034 : 525704 : int failure = 0;
7035 : :
7036 : : /* Check that there are no designated initializers in INIT, as
7037 : : those are not supported in GNU C++, and as the middle-end
7038 : : will crash if presented with a non-numeric designated
7039 : : initializer. */
7040 : 525704 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
7041 : : {
7042 : 94533 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
7043 : 94533 : constructor_elt *ce;
7044 : 94533 : HOST_WIDE_INT i, j = 0;
7045 : 16179051 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
7046 : : {
7047 : 16086053 : if (instantiation_dependent_expression_p (ce->index))
7048 : 57093126 : return;
7049 : 16086047 : if (!check_array_designated_initializer (ce, j))
7050 : 2 : failure = 1;
7051 : : /* If an un-designated initializer is type-dependent, we can't
7052 : : check brace elision yet. */
7053 : 16086047 : if (ce->index == NULL_TREE
7054 : 16086047 : && type_dependent_expression_p (ce->value))
7055 : : return;
7056 : 16084518 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7057 : 193 : j += RAW_DATA_LENGTH (ce->value);
7058 : : else
7059 : 16084325 : ++j;
7060 : : }
7061 : : }
7062 : :
7063 : 92998 : if (failure)
7064 : 2 : TREE_TYPE (decl) = error_mark_node;
7065 : : else
7066 : : {
7067 : 524167 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
7068 : : do_default);
7069 : 524167 : if (failure == 1)
7070 : : {
7071 : 33 : error_at (cp_expr_loc_or_loc (initializer,
7072 : 30 : DECL_SOURCE_LOCATION (decl)),
7073 : : "initializer fails to determine size of %qD", decl);
7074 : : }
7075 : 524137 : else if (failure == 2)
7076 : : {
7077 : 30 : if (do_default)
7078 : : {
7079 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7080 : : "array size missing in %qD", decl);
7081 : : }
7082 : : /* If a `static' var's size isn't known, make it extern as
7083 : : well as static, so it does not get allocated. If it's not
7084 : : `static', then don't mark it extern; finish_incomplete_decl
7085 : : will give it a default size and it will get allocated. */
7086 : 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7087 : 0 : DECL_EXTERNAL (decl) = 1;
7088 : : }
7089 : 524107 : else if (failure == 3)
7090 : : {
7091 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7092 : : "zero-size array %qD", decl);
7093 : : }
7094 : : }
7095 : :
7096 : 524169 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
7097 : :
7098 : 524169 : relayout_decl (decl);
7099 : : }
7100 : : }
7101 : :
7102 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7103 : : any appropriate error messages regarding the layout. */
7104 : :
7105 : : static void
7106 : 48473879 : layout_var_decl (tree decl)
7107 : : {
7108 : 48473879 : tree type;
7109 : :
7110 : 48473879 : type = TREE_TYPE (decl);
7111 : 48473879 : if (type == error_mark_node)
7112 : : return;
7113 : :
7114 : : /* If we haven't already laid out this declaration, do so now.
7115 : : Note that we must not call complete type for an external object
7116 : : because it's type might involve templates that we are not
7117 : : supposed to instantiate yet. (And it's perfectly valid to say
7118 : : `extern X x' for some incomplete type `X'.) */
7119 : 48473831 : if (!DECL_EXTERNAL (decl))
7120 : 39859091 : complete_type (type);
7121 : 48473831 : if (!DECL_SIZE (decl)
7122 : 515581 : && TREE_TYPE (decl) != error_mark_node
7123 : 48989412 : && complete_or_array_type_p (type))
7124 : 471834 : layout_decl (decl, 0);
7125 : :
7126 : 48473831 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7127 : : {
7128 : : /* An automatic variable with an incomplete type: that is an error.
7129 : : Don't talk about array types here, since we took care of that
7130 : : message in grokdeclarator. */
7131 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
7132 : : "storage size of %qD isn%'t known", decl);
7133 : 17 : TREE_TYPE (decl) = error_mark_node;
7134 : : }
7135 : : #if 0
7136 : : /* Keep this code around in case we later want to control debug info
7137 : : based on whether a type is "used". (jason 1999-11-11) */
7138 : :
7139 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
7140 : : /* Let debugger know it should output info for this type. */
7141 : : note_debug_info_needed (ttype);
7142 : :
7143 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7144 : : note_debug_info_needed (DECL_CONTEXT (decl));
7145 : : #endif
7146 : :
7147 : 88332922 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7148 : 25231968 : && DECL_SIZE (decl) != NULL_TREE
7149 : 73528202 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
7150 : : {
7151 : 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7152 : 6 : && !DECL_LOCAL_DECL_P (decl))
7153 : 0 : constant_expression_warning (DECL_SIZE (decl));
7154 : : else
7155 : : {
7156 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
7157 : : "storage size of %qD isn%'t constant", decl);
7158 : 6 : TREE_TYPE (decl) = error_mark_node;
7159 : 6 : type = error_mark_node;
7160 : : }
7161 : : }
7162 : :
7163 : : /* If the final element initializes a flexible array field, adjust
7164 : : the size of the DECL with the initializer based on whether the
7165 : : DECL is a union or a structure. */
7166 : 48473831 : if (type != error_mark_node
7167 : 48473825 : && DECL_INITIAL (decl)
7168 : 25145544 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7169 : 3791494 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
7170 : 3140225 : && DECL_SIZE (decl) != NULL_TREE
7171 : 3140225 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
7172 : 3140225 : && COMPLETE_TYPE_P (type)
7173 : 3140225 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
7174 : 51614056 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
7175 : : {
7176 : 3140225 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
7177 : 3140225 : if (elt.index)
7178 : : {
7179 : 1003098 : tree itype = TREE_TYPE (elt.index);
7180 : 1003098 : tree vtype = TREE_TYPE (elt.value);
7181 : 1003098 : if (TREE_CODE (itype) == ARRAY_TYPE
7182 : 53105 : && TYPE_DOMAIN (itype) == NULL
7183 : 272 : && TREE_CODE (vtype) == ARRAY_TYPE
7184 : 1003370 : && COMPLETE_TYPE_P (vtype))
7185 : : {
7186 : : /* For a structure, add the size of the initializer to the DECL's
7187 : : size. */
7188 : 272 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
7189 : : {
7190 : 257 : DECL_SIZE (decl)
7191 : 257 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
7192 : : TYPE_SIZE (vtype));
7193 : 257 : DECL_SIZE_UNIT (decl)
7194 : 514 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
7195 : : TYPE_SIZE_UNIT (vtype));
7196 : : }
7197 : : /* For a union, the DECL's size is the maximum of the current size
7198 : : and the size of the initializer. */
7199 : : else
7200 : : {
7201 : 15 : DECL_SIZE (decl)
7202 : 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
7203 : : TYPE_SIZE (vtype));
7204 : 15 : DECL_SIZE_UNIT (decl)
7205 : 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
7206 : : TYPE_SIZE_UNIT (vtype));
7207 : : }
7208 : : }
7209 : : }
7210 : : }
7211 : : }
7212 : :
7213 : : /* If a local static variable is declared in an inline function, or if
7214 : : we have a weak definition, we must endeavor to create only one
7215 : : instance of the variable at link-time. */
7216 : :
7217 : : void
7218 : 55191612 : maybe_commonize_var (tree decl)
7219 : : {
7220 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
7221 : : bindings. */
7222 : 55191612 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
7223 : : return;
7224 : :
7225 : : /* Static data in a function with comdat linkage also has comdat
7226 : : linkage. */
7227 : 50204391 : if ((TREE_STATIC (decl)
7228 : 26616374 : && DECL_FUNCTION_SCOPE_P (decl)
7229 : 141263 : && vague_linkage_p (DECL_CONTEXT (decl)))
7230 : 103613956 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
7231 : : {
7232 : 22113650 : if (flag_weak)
7233 : : {
7234 : : /* With weak symbols, we simply make the variable COMDAT;
7235 : : that will cause copies in multiple translations units to
7236 : : be merged. */
7237 : 22113623 : comdat_linkage (decl);
7238 : : }
7239 : : else
7240 : : {
7241 : 27 : if (DECL_INITIAL (decl) == NULL_TREE
7242 : 27 : || DECL_INITIAL (decl) == error_mark_node)
7243 : : {
7244 : : /* Without weak symbols, we can use COMMON to merge
7245 : : uninitialized variables. */
7246 : 24 : TREE_PUBLIC (decl) = 1;
7247 : 24 : DECL_COMMON (decl) = 1;
7248 : : }
7249 : : else
7250 : : {
7251 : : /* While for initialized variables, we must use internal
7252 : : linkage -- which means that multiple copies will not
7253 : : be merged. */
7254 : 3 : TREE_PUBLIC (decl) = 0;
7255 : 3 : DECL_COMMON (decl) = 0;
7256 : 3 : DECL_INTERFACE_KNOWN (decl) = 1;
7257 : 3 : const char *msg;
7258 : 3 : if (DECL_INLINE_VAR_P (decl))
7259 : : msg = G_("sorry: semantics of inline variable "
7260 : : "%q#D are wrong (you%'ll wind up with "
7261 : : "multiple copies)");
7262 : : else
7263 : : msg = G_("sorry: semantics of inline function "
7264 : : "static data %q#D are wrong (you%'ll wind "
7265 : : "up with multiple copies)");
7266 : 3 : auto_diagnostic_group d;
7267 : 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
7268 : : msg, decl))
7269 : 3 : inform (DECL_SOURCE_LOCATION (decl),
7270 : : "you can work around this by removing the initializer");
7271 : 3 : }
7272 : : }
7273 : : }
7274 : : }
7275 : :
7276 : : /* Issue an error message if DECL is an uninitialized const variable.
7277 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
7278 : : context from potential_constant_expression. Returns true if all is well,
7279 : : false otherwise. */
7280 : :
7281 : : bool
7282 : 6521273 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
7283 : : tsubst_flags_t complain)
7284 : : {
7285 : 6521273 : tree type = strip_array_types (TREE_TYPE (decl));
7286 : :
7287 : : /* ``Unless explicitly declared extern, a const object does not have
7288 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
7289 : : 7.1.6 */
7290 : 6521273 : if (VAR_P (decl)
7291 : 6521273 : && !TYPE_REF_P (type)
7292 : 6504033 : && (CP_TYPE_CONST_P (type)
7293 : : /* C++20 permits trivial default initialization in constexpr
7294 : : context (P1331R2). */
7295 : 5456795 : || (cxx_dialect < cxx20
7296 : 2781060 : && (constexpr_context_p
7297 : 2483828 : || var_in_constexpr_fn (decl))))
7298 : 7977373 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
7299 : : {
7300 : 653834 : tree field = default_init_uninitialized_part (type);
7301 : 653834 : if (!field)
7302 : : return true;
7303 : :
7304 : 113346 : auto_diagnostic_group d;
7305 : 113346 : bool show_notes = true;
7306 : :
7307 : 113346 : if (!constexpr_context_p || cxx_dialect >= cxx20)
7308 : : {
7309 : 109586 : if (CP_TYPE_CONST_P (type))
7310 : : {
7311 : 90 : if (complain & tf_error)
7312 : 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
7313 : : "uninitialized %<const %D%>", decl);
7314 : : }
7315 : : else
7316 : : {
7317 : 109496 : if (!is_instantiation_of_constexpr (current_function_decl)
7318 : 109496 : && (complain & tf_error))
7319 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
7320 : : "uninitialized variable %qD in %<constexpr%> "
7321 : : "function", decl);
7322 : : else
7323 : : show_notes = false;
7324 : 109496 : cp_function_chain->invalid_constexpr = true;
7325 : : }
7326 : : }
7327 : 3760 : else if (complain & tf_error)
7328 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
7329 : : "uninitialized variable %qD in %<constexpr%> context",
7330 : : decl);
7331 : :
7332 : 113346 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
7333 : : {
7334 : 62 : tree defaulted_ctor;
7335 : :
7336 : 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
7337 : : "%q#T has no user-provided default constructor", type);
7338 : 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
7339 : 62 : if (defaulted_ctor)
7340 : 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
7341 : : "constructor is not user-provided because it is "
7342 : : "explicitly defaulted in the class body");
7343 : 62 : inform (DECL_SOURCE_LOCATION (field),
7344 : : "and the implicitly-defined constructor does not "
7345 : : "initialize %q#D", field);
7346 : : }
7347 : :
7348 : 113346 : return false;
7349 : 113346 : }
7350 : :
7351 : : return true;
7352 : : }
7353 : :
7354 : : /* Structure holding the current initializer being processed by reshape_init.
7355 : : CUR is a pointer to the current element being processed, END is a pointer
7356 : : after the last element present in the initializer and RAW_IDX is index into
7357 : : RAW_DATA_CST if that is CUR elt. */
7358 : : struct reshape_iter
7359 : : {
7360 : : constructor_elt *cur;
7361 : : constructor_elt *end;
7362 : : unsigned raw_idx;
7363 : : };
7364 : :
7365 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
7366 : :
7367 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7368 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
7369 : : initialized as if for an aggregate class. If there are no more such fields,
7370 : : the return value will be NULL. */
7371 : :
7372 : : tree
7373 : 14444367 : next_aggregate_field (tree field)
7374 : : {
7375 : 14444367 : while (field
7376 : 50479117 : && (TREE_CODE (field) != FIELD_DECL
7377 : 9276509 : || DECL_UNNAMED_BIT_FIELD (field)
7378 : 9276387 : || (DECL_ARTIFICIAL (field)
7379 : : /* In C++17, aggregates can have bases. */
7380 : 75990 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
7381 : 36034750 : field = DECL_CHAIN (field);
7382 : :
7383 : 14444367 : return field;
7384 : : }
7385 : :
7386 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
7387 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
7388 : : to a subobject. If there are no more such fields, the return value will be
7389 : : NULL. */
7390 : :
7391 : : tree
7392 : 55280069 : next_subobject_field (tree field)
7393 : : {
7394 : 55280069 : while (field
7395 : 672486341 : && (TREE_CODE (field) != FIELD_DECL
7396 : 25448327 : || DECL_UNNAMED_BIT_FIELD (field)
7397 : 25448247 : || (DECL_ARTIFICIAL (field)
7398 : 6993099 : && !DECL_FIELD_IS_BASE (field)
7399 : 1728168 : && !DECL_VIRTUAL_P (field))))
7400 : 617206272 : field = DECL_CHAIN (field);
7401 : :
7402 : 55280069 : return field;
7403 : : }
7404 : :
7405 : : /* Return true for [dcl.init.list] direct-list-initialization from
7406 : : single element of enumeration with a fixed underlying type. */
7407 : :
7408 : : bool
7409 : 126913505 : is_direct_enum_init (tree type, tree init)
7410 : : {
7411 : 126913505 : if (cxx_dialect >= cxx17
7412 : 124987247 : && TREE_CODE (type) == ENUMERAL_TYPE
7413 : 6209803 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
7414 : 3955593 : && TREE_CODE (init) == CONSTRUCTOR
7415 : 410120 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
7416 : 207782 : && CONSTRUCTOR_NELTS (init) == 1
7417 : 218 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
7418 : : /* DR 2374: The single element needs to be implicitly
7419 : : convertible to the underlying type of the enum. */
7420 : 218 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
7421 : 126913721 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
7422 : 216 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
7423 : 216 : CONSTRUCTOR_ELT (init, 0)->value,
7424 : : LOOKUP_IMPLICIT, tf_none))
7425 : : return true;
7426 : : return false;
7427 : : }
7428 : :
7429 : : /* Helper function for reshape_init*. Split first element of
7430 : : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
7431 : : to true if the whole d->cur has been consumed. */
7432 : :
7433 : : static tree
7434 : 24125084 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
7435 : : {
7436 : 24125084 : *inc_cur = true;
7437 : 24125084 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
7438 : : return NULL_TREE;
7439 : 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
7440 : 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
7441 : 30588 : *inc_cur = false;
7442 : : else
7443 : 132 : d->raw_idx = 0;
7444 : : return ret;
7445 : : }
7446 : :
7447 : : /* Wrapper around that which for RAW_DATA_CST in INIT
7448 : : (as well as in D->cur->value) peels off the first element
7449 : : of the raw data and returns it, otherwise increments
7450 : : D->cur and returns INIT. */
7451 : :
7452 : : static tree
7453 : 24125068 : consume_init (tree init, reshape_iter *d)
7454 : : {
7455 : 24125068 : bool inc_cur;
7456 : 24125068 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7457 : 30720 : init = raw_init;
7458 : 24125068 : if (inc_cur)
7459 : 24094480 : d->cur++;
7460 : 24125068 : return init;
7461 : : }
7462 : :
7463 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
7464 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
7465 : : INTEGER_CST representing the size of the array minus one (the maximum index),
7466 : : or NULL_TREE if the array was declared without specifying the size. D is
7467 : : the iterator within the constructor. */
7468 : :
7469 : : static tree
7470 : 363989 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
7471 : : tree first_initializer_p, bool vector_p,
7472 : : tsubst_flags_t complain)
7473 : : {
7474 : 363989 : tree new_init;
7475 : 363989 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
7476 : 363989 : unsigned HOST_WIDE_INT max_index_cst = 0;
7477 : 363989 : unsigned HOST_WIDE_INT index;
7478 : :
7479 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
7480 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7481 : : to build a new one. But don't reuse if not complaining; if this is
7482 : : tentative, we might also reshape to another type (95319). */
7483 : 363989 : bool reuse = (first_initializer_p
7484 : 315304 : && (complain & tf_error)
7485 : 314051 : && !CP_AGGREGATE_TYPE_P (elt_type)
7486 : 627546 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7487 : 108226 : if (reuse)
7488 : : new_init = first_initializer_p;
7489 : : else
7490 : 108226 : new_init = build_constructor (init_list_type_node, NULL);
7491 : :
7492 : 363989 : if (sized_array_p)
7493 : : {
7494 : 252811 : poly_uint64 midx;
7495 : : /* Minus 1 is used for zero sized arrays. */
7496 : 252811 : if (integer_all_onesp (max_index))
7497 : 24 : return new_init;
7498 : :
7499 : 252787 : if (tree_fits_poly_uint64_p (max_index))
7500 : 252787 : midx = tree_to_poly_uint64 (max_index);
7501 : : /* sizetype is sign extended, not zero extended. */
7502 : : else
7503 : 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7504 : :
7505 : : /* For VLA vectors, we restict the number of elements in the constructor
7506 : : to lower bound of the VLA elements. */
7507 : 252787 : max_index_cst = constant_lower_bound (midx);
7508 : : }
7509 : :
7510 : 363965 : constructor_elt *first_cur = d->cur;
7511 : : /* Loop until there are no more initializers. */
7512 : 18928390 : for (index = 0;
7513 : 18928390 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7514 : : ++index)
7515 : : {
7516 : 18564607 : tree elt_init;
7517 : 18564607 : constructor_elt *old_cur = d->cur;
7518 : 18564607 : unsigned int old_raw_idx = d->raw_idx;
7519 : 18564607 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7520 : :
7521 : 18564607 : if (d->cur->index)
7522 : 1959108 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7523 : 18564607 : check_array_designated_initializer (d->cur, index);
7524 : 18564607 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7525 : 32695 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7526 : 10233 : || is_byte_access_type (elt_type))
7527 : 22462 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7528 : 1801 : && (!sized_array_p || index < max_index_cst)
7529 : 18566396 : && !vector_p)
7530 : : {
7531 : 469 : elt_init = d->cur->value;
7532 : 469 : unsigned int off = d->raw_idx;
7533 : 469 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7534 : 469 : if (!sized_array_p || len <= max_index_cst - index + 1)
7535 : : {
7536 : 460 : d->cur++;
7537 : 460 : d->raw_idx = 0;
7538 : : }
7539 : : else
7540 : : {
7541 : 9 : len = max_index_cst - index + 1;
7542 : 9 : d->raw_idx += len;
7543 : : }
7544 : 469 : if (!reuse || off || d->cur == old_cur)
7545 : : {
7546 : 27 : elt_init = copy_node (elt_init);
7547 : 27 : RAW_DATA_LENGTH (elt_init) = len;
7548 : 27 : RAW_DATA_POINTER (elt_init) += off;
7549 : : }
7550 : 469 : TREE_TYPE (elt_init) = elt_type;
7551 : : }
7552 : : else
7553 : 18564138 : elt_init = reshape_init_r (elt_type, d,
7554 : : /*first_initializer_p=*/NULL_TREE,
7555 : : complain);
7556 : 18564607 : if (elt_init == error_mark_node)
7557 : : return error_mark_node;
7558 : 18564428 : tree idx = size_int (index);
7559 : 18564428 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7560 : : {
7561 : : /* We need to stop reusing as some RAW_DATA_CST in the original
7562 : : ctor had to be split. */
7563 : 72 : new_init = build_constructor (init_list_type_node, NULL);
7564 : 72 : if (index)
7565 : : {
7566 : 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7567 : 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7568 : 72 : (d->cur - first_cur)
7569 : : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7570 : 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7571 : : {
7572 : : unsigned int j;
7573 : : tree nidx, nval;
7574 : 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7575 : : j, nidx, nval)
7576 : 4 : if (nidx)
7577 : : {
7578 : 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7579 : 4 : (void) nval;
7580 : 4 : break;
7581 : : }
7582 : : }
7583 : : }
7584 : : reuse = false;
7585 : : }
7586 : 18564356 : if (reuse)
7587 : : {
7588 : 16754958 : old_cur->index = idx;
7589 : 16754958 : old_cur->value = elt_init;
7590 : : }
7591 : : else
7592 : 1809470 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7593 : : idx, elt_init);
7594 : 18564428 : if (!TREE_CONSTANT (elt_init))
7595 : 169342 : TREE_CONSTANT (new_init) = false;
7596 : :
7597 : : /* This can happen with an invalid initializer (c++/54501). */
7598 : 18564428 : if (d->cur == old_cur
7599 : 32082 : && !sized_array_p
7600 : 14085 : && d->raw_idx == old_raw_idx)
7601 : : break;
7602 : :
7603 : 18564425 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7604 : 469 : index += RAW_DATA_LENGTH (elt_init) - 1;
7605 : : }
7606 : :
7607 : : return new_init;
7608 : : }
7609 : :
7610 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7611 : : Parameters are the same of reshape_init_r. */
7612 : :
7613 : : static tree
7614 : 322797 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7615 : : tsubst_flags_t complain)
7616 : : {
7617 : 322797 : tree max_index = NULL_TREE;
7618 : :
7619 : 322797 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7620 : :
7621 : 322797 : if (TYPE_DOMAIN (type))
7622 : 211664 : max_index = array_type_nelts_minus_one (type);
7623 : :
7624 : 322797 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7625 : 322797 : first_initializer_p, false, complain);
7626 : : }
7627 : :
7628 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7629 : : Parameters are the same of reshape_init_r. */
7630 : :
7631 : : static tree
7632 : 41192 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7633 : : {
7634 : 41192 : tree max_index = NULL_TREE;
7635 : :
7636 : 41192 : gcc_assert (VECTOR_TYPE_P (type));
7637 : :
7638 : 41192 : if (COMPOUND_LITERAL_P (d->cur->value))
7639 : : {
7640 : 0 : tree value = d->cur->value;
7641 : 0 : if (!same_type_p (TREE_TYPE (value), type))
7642 : : {
7643 : 0 : if (complain & tf_error)
7644 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7645 : 0 : TREE_TYPE (d->cur->value), type);
7646 : 0 : value = error_mark_node;
7647 : : }
7648 : 0 : ++d->cur;
7649 : 0 : return value;
7650 : : }
7651 : :
7652 : : /* For a vector, we initialize it as an array of the appropriate size. */
7653 : 41192 : if (VECTOR_TYPE_P (type))
7654 : 41192 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7655 : :
7656 : 41192 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7657 : 41192 : NULL_TREE, true, complain);
7658 : : }
7659 : :
7660 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7661 : : INIT, in isolation from any designator or other initializers. */
7662 : :
7663 : : static tree
7664 : 583112 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7665 : : {
7666 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7667 : : calling reshape_init, but this way can just live on the stack. */
7668 : 583112 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7669 : 583112 : reshape_iter iter = { &elt, &elt + 1, 0 };
7670 : 583112 : return reshape_init_r (type, &iter,
7671 : : /*first_initializer_p=*/NULL_TREE,
7672 : 583112 : complain);
7673 : : }
7674 : :
7675 : : /* Subroutine of reshape_init_r, processes the initializers for classes
7676 : : or union. Parameters are the same of reshape_init_r. */
7677 : :
7678 : : static tree
7679 : 3652420 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7680 : : tsubst_flags_t complain)
7681 : : {
7682 : 3652420 : tree field;
7683 : 3652420 : tree new_init;
7684 : :
7685 : 3652420 : gcc_assert (CLASS_TYPE_P (type));
7686 : :
7687 : : /* The initializer for a class is always a CONSTRUCTOR. */
7688 : 3652420 : new_init = build_constructor (init_list_type_node, NULL);
7689 : :
7690 : 3652420 : int binfo_idx = -1;
7691 : 3652420 : tree binfo = TYPE_BINFO (type);
7692 : 3652420 : tree base_binfo = NULL_TREE;
7693 : 3652420 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7694 : : {
7695 : : /* We get here from maybe_aggr_guide for C++20 class template argument
7696 : : deduction. In this case we need to look through the binfo because a
7697 : : template doesn't have base fields. */
7698 : 342 : binfo_idx = 0;
7699 : 342 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7700 : : }
7701 : 67 : if (base_binfo)
7702 : : field = base_binfo;
7703 : : else
7704 : 3652353 : field = next_aggregate_field (TYPE_FIELDS (type));
7705 : :
7706 : 3652420 : if (!field)
7707 : : {
7708 : : /* [dcl.init.aggr]
7709 : :
7710 : : An initializer for an aggregate member that is an
7711 : : empty class shall have the form of an empty
7712 : : initializer-list {}. */
7713 : 324 : if (!first_initializer_p)
7714 : : {
7715 : 270 : if (complain & tf_error)
7716 : 13 : error ("initializer for %qT must be brace-enclosed", type);
7717 : 270 : return error_mark_node;
7718 : : }
7719 : : return new_init;
7720 : : }
7721 : :
7722 : : /* For C++20 CTAD, handle pack expansions in the base list. */
7723 : : tree last_was_pack_expansion = NULL_TREE;
7724 : :
7725 : : /* Loop through the initializable fields, gathering initializers. */
7726 : 11152876 : while (d->cur != d->end)
7727 : : {
7728 : 7612055 : tree field_init;
7729 : 7612055 : constructor_elt *old_cur = d->cur;
7730 : 7612055 : unsigned old_raw_idx = d->raw_idx;
7731 : 7612055 : bool direct_desig = false;
7732 : :
7733 : : /* Handle C++20 designated initializers. */
7734 : 7612055 : if (d->cur->index)
7735 : : {
7736 : 583272 : if (d->cur->index == error_mark_node)
7737 : : return error_mark_node;
7738 : :
7739 : 583266 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7740 : : {
7741 : : /* We already reshaped this; we should have returned early from
7742 : : reshape_init. */
7743 : 0 : gcc_checking_assert (false);
7744 : : if (field != d->cur->index)
7745 : : {
7746 : : if (tree id = DECL_NAME (d->cur->index))
7747 : : gcc_checking_assert (d->cur->index
7748 : : == get_class_binding (type, id));
7749 : : field = d->cur->index;
7750 : : }
7751 : : }
7752 : 583266 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7753 : : {
7754 : 583260 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7755 : 583260 : field = get_class_binding (type, d->cur->index);
7756 : 583260 : direct_desig = true;
7757 : : }
7758 : : else
7759 : : {
7760 : 6 : if (complain & tf_error)
7761 : 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7762 : : " for class %qT", d->cur->index, type);
7763 : 6 : return error_mark_node;
7764 : : }
7765 : :
7766 : 583260 : if (!field && ANON_AGGR_TYPE_P (type))
7767 : : /* Apparently the designator isn't for a member of this anonymous
7768 : : struct, so head back to the enclosing class. */
7769 : : break;
7770 : :
7771 : 583257 : if (!field || TREE_CODE (field) != FIELD_DECL)
7772 : : {
7773 : 18 : if (complain & tf_error)
7774 : : {
7775 : 6 : if (field && TREE_CODE (field) == TREE_LIST)
7776 : : {
7777 : 3 : auto_diagnostic_group g;
7778 : 3 : error ("request for member %qD is ambiguous",
7779 : 3 : d->cur->index);
7780 : 3 : print_candidates (field);
7781 : 3 : }
7782 : : else
7783 : 3 : error ("%qT has no non-static data member named %qD", type,
7784 : 3 : d->cur->index);
7785 : : }
7786 : 18 : return error_mark_node;
7787 : : }
7788 : :
7789 : : /* If the element is an anonymous union object and the initializer
7790 : : list is a designated-initializer-list, the anonymous union object
7791 : : is initialized by the designated-initializer-list { D }, where D
7792 : : is the designated-initializer-clause naming a member of the
7793 : : anonymous union object. */
7794 : 583239 : tree ictx = DECL_CONTEXT (field);
7795 : 583239 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7796 : : {
7797 : : /* Find the anon aggr that is a direct member of TYPE. */
7798 : 139 : while (ANON_AGGR_TYPE_P (ictx))
7799 : : {
7800 : 136 : tree cctx = TYPE_CONTEXT (ictx);
7801 : 136 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7802 : 124 : goto found;
7803 : : ictx = cctx;
7804 : : }
7805 : :
7806 : : /* Not found, e.g. FIELD is a member of a base class. */
7807 : 3 : if (complain & tf_error)
7808 : 3 : error ("%qD is not a direct member of %qT", field, type);
7809 : 3 : return error_mark_node;
7810 : :
7811 : 124 : found:
7812 : : /* Now find the TYPE member with that anon aggr type. */
7813 : 124 : tree aafield = TYPE_FIELDS (type);
7814 : 1067 : for (; aafield; aafield = TREE_CHAIN (aafield))
7815 : 1067 : if (TREE_TYPE (aafield) == ictx)
7816 : : break;
7817 : 0 : gcc_assert (aafield);
7818 : : field = aafield;
7819 : : direct_desig = false;
7820 : : }
7821 : : }
7822 : :
7823 : : /* If we processed all the member of the class, we are done. */
7824 : 7611895 : if (!field)
7825 : : break;
7826 : :
7827 : 15223189 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7828 : 7611604 : ? field : NULL_TREE);
7829 : 7611604 : if (last_was_pack_expansion)
7830 : : /* Each non-trailing aggregate element that is a pack expansion is
7831 : : assumed to correspond to no elements of the initializer list. */
7832 : 19 : goto continue_;
7833 : :
7834 : 7611585 : if (direct_desig)
7835 : : {
7836 : : /* The designated field F is initialized from this one element.
7837 : :
7838 : : Note that we don't want to do this if we found the designator
7839 : : inside an anon aggr above; we use the normal code to implement:
7840 : :
7841 : : "If the element is an anonymous union member and the initializer
7842 : : list is a brace-enclosed designated- initializer-list, the element
7843 : : is initialized by the designated-initializer-list { D }, where D
7844 : : is the designated- initializer-clause naming a member of the
7845 : : anonymous union member." */
7846 : 583112 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7847 : 583112 : field_init = reshape_single_init (TREE_TYPE (field),
7848 : : d->cur->value, complain);
7849 : 583112 : d->cur++;
7850 : : }
7851 : : else
7852 : 7028473 : field_init = reshape_init_r (TREE_TYPE (field), d,
7853 : : /*first_initializer_p=*/NULL_TREE,
7854 : : complain);
7855 : :
7856 : 7611585 : if (field_init == error_mark_node)
7857 : : return error_mark_node;
7858 : :
7859 : 7611296 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7860 : : {
7861 : : /* This can happen with an invalid initializer for a flexible
7862 : : array member (c++/54441). */
7863 : 0 : if (complain & tf_error)
7864 : 0 : error ("invalid initializer for %q#D", field);
7865 : 0 : return error_mark_node;
7866 : : }
7867 : :
7868 : 7611296 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7869 : :
7870 : : /* [dcl.init.aggr]
7871 : :
7872 : : When a union is initialized with a brace-enclosed
7873 : : initializer, the braces shall only contain an
7874 : : initializer for the first member of the union. */
7875 : 7611296 : if (TREE_CODE (type) == UNION_TYPE)
7876 : : break;
7877 : :
7878 : 7500761 : continue_:
7879 : 7500780 : if (base_binfo)
7880 : : {
7881 : 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7882 : : field = base_binfo;
7883 : : else
7884 : 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7885 : : }
7886 : : else
7887 : 7500698 : field = next_aggregate_field (DECL_CHAIN (field));
7888 : : }
7889 : :
7890 : : /* A trailing aggregate element that is a pack expansion is assumed to
7891 : : correspond to all remaining elements of the initializer list (if any). */
7892 : 3651774 : if (last_was_pack_expansion)
7893 : : {
7894 : 16 : tree init = d->cur->value;
7895 : 16 : bool inc_cur;
7896 : 16 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7897 : 0 : init = raw_init;
7898 : 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7899 : : last_was_pack_expansion, init);
7900 : 46 : while (d->cur != d->end)
7901 : 30 : d->cur++;
7902 : : }
7903 : :
7904 : : return new_init;
7905 : : }
7906 : :
7907 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7908 : : designators are not valid; either complain or return true to indicate
7909 : : that reshape_init_r should return error_mark_node. */
7910 : :
7911 : : static bool
7912 : 64855 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7913 : : {
7914 : 64855 : if (d->cur->index)
7915 : : {
7916 : 30 : if (complain & tf_error)
7917 : 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7918 : : "C99 designator %qE outside aggregate initializer",
7919 : : d->cur->index);
7920 : : else
7921 : : return true;
7922 : : }
7923 : : return false;
7924 : : }
7925 : :
7926 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7927 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7928 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7929 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7930 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7931 : :
7932 : : static tree
7933 : 30248559 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7934 : : tsubst_flags_t complain)
7935 : : {
7936 : 30248559 : tree init = d->cur->value;
7937 : :
7938 : 30248559 : if (error_operand_p (init))
7939 : 125 : return error_mark_node;
7940 : :
7941 : 4072714 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7942 : 30313123 : && has_designator_problem (d, complain))
7943 : 0 : return error_mark_node;
7944 : :
7945 : 30248434 : tree stripped_init = tree_strip_any_location_wrapper (init);
7946 : :
7947 : 30248434 : if (TREE_CODE (type) == COMPLEX_TYPE)
7948 : : {
7949 : : /* A complex type can be initialized from one or two initializers,
7950 : : but braces are not elided. */
7951 : 588 : init = consume_init (init, d);
7952 : 588 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7953 : : {
7954 : 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7955 : : {
7956 : 0 : if (complain & tf_error)
7957 : 0 : error ("too many initializers for %qT", type);
7958 : : else
7959 : 0 : return error_mark_node;
7960 : : }
7961 : : }
7962 : 579 : else if (first_initializer_p && d->cur != d->end)
7963 : : {
7964 : 36 : if (error_operand_p (d->cur->value)
7965 : 36 : || has_designator_problem (d, complain))
7966 : 0 : return error_mark_node;
7967 : 36 : vec<constructor_elt, va_gc> *v = 0;
7968 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7969 : 36 : init = consume_init (d->cur->value, d);
7970 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7971 : 36 : init = build_constructor (init_list_type_node, v);
7972 : : }
7973 : 588 : return init;
7974 : : }
7975 : :
7976 : : /* A non-aggregate type is always initialized with a single
7977 : : initializer. */
7978 : 60445792 : if (!CP_AGGREGATE_TYPE_P (type)
7979 : : /* As is an array with dependent bound, which we can see
7980 : : during C++20 aggregate CTAD. */
7981 : 36324865 : || (cxx_dialect >= cxx20
7982 : 3949113 : && TREE_CODE (type) == ARRAY_TYPE
7983 : 161659 : && uses_template_parms (TYPE_DOMAIN (type))))
7984 : : {
7985 : : /* It is invalid to initialize a non-aggregate type with a
7986 : : brace-enclosed initializer before C++0x.
7987 : : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7988 : : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7989 : : a CONSTRUCTOR (with a record type). */
7990 : 24120938 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
7991 : : /* Don't complain about a capture-init. */
7992 : 307006 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7993 : 24417906 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7994 : : {
7995 : 296929 : if (SCALAR_TYPE_P (type))
7996 : : {
7997 : 726 : if (cxx_dialect < cxx11)
7998 : : {
7999 : 16 : if (complain & tf_error)
8000 : 16 : error ("braces around scalar initializer for type %qT",
8001 : : type);
8002 : 16 : init = error_mark_node;
8003 : : }
8004 : 710 : else if (first_initializer_p
8005 : 710 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
8006 : 546 : && (BRACE_ENCLOSED_INITIALIZER_P
8007 : : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
8008 : : {
8009 : 13 : if (complain & tf_error)
8010 : 13 : error ("too many braces around scalar initializer "
8011 : : "for type %qT", type);
8012 : 13 : init = error_mark_node;
8013 : : }
8014 : : }
8015 : : else
8016 : 296203 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8017 : : }
8018 : 24120938 : return consume_init (init, d);
8019 : : }
8020 : :
8021 : : /* "If T is a class type and the initializer list has a single element of
8022 : : type cv U, where U is T or a class derived from T, the object is
8023 : : initialized from that element." Even if T is an aggregate. */
8024 : 6104849 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
8025 : 5673919 : && first_initializer_p
8026 : : /* But not if it's a designated init. */
8027 : 3679958 : && !d->cur->index
8028 : 3433195 : && d->end - d->cur == 1
8029 : 433079 : && TREE_CODE (init) != RAW_DATA_CST
8030 : 6559987 : && reference_related_p (type, TREE_TYPE (init)))
8031 : : {
8032 : 416 : d->cur++;
8033 : 416 : return init;
8034 : : }
8035 : :
8036 : : /* [dcl.init.aggr]
8037 : :
8038 : : All implicit type conversions (clause _conv_) are considered when
8039 : : initializing the aggregate member with an initializer from an
8040 : : initializer-list. If the initializer can initialize a member,
8041 : : the member is initialized. Otherwise, if the member is itself a
8042 : : non-empty subaggregate, brace elision is assumed and the
8043 : : initializer is considered for the initialization of the first
8044 : : member of the subaggregate. */
8045 : 2681949 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
8046 : : /* But don't try this for the first initializer, since that would be
8047 : : looking through the outermost braces; A a2 = { a1 }; is not a
8048 : : valid aggregate initialization. */
8049 : 3444603 : && !first_initializer_p
8050 : 6139669 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
8051 : 9709 : || can_convert_arg (type, TREE_TYPE (init),
8052 : 9709 : TREE_CODE (init) == RAW_DATA_CST
8053 : 6128098 : ? build_int_cst (integer_type_node,
8054 : 1606 : *(const unsigned char *)
8055 : 1606 : RAW_DATA_POINTER (init))
8056 : : : init,
8057 : : LOOKUP_NORMAL, complain)))
8058 : 3506 : return consume_init (init, d);
8059 : :
8060 : : /* [dcl.init.string]
8061 : :
8062 : : A char array (whether plain char, signed char, or unsigned char)
8063 : : can be initialized by a string-literal (optionally enclosed in
8064 : : braces); a wchar_t array can be initialized by a wide
8065 : : string-literal (optionally enclosed in braces). */
8066 : 6122986 : if (TREE_CODE (type) == ARRAY_TYPE
8067 : 6122986 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8068 : : {
8069 : 88913 : tree str_init = init;
8070 : 88913 : tree stripped_str_init = stripped_init;
8071 : 88913 : reshape_iter stripd = {};
8072 : :
8073 : : /* Strip one level of braces if and only if they enclose a single
8074 : : element (as allowed by [dcl.init.string]). */
8075 : 88913 : if (!first_initializer_p
8076 : 12383 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
8077 : 100187 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
8078 : : {
8079 : 375 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
8080 : 375 : str_init = stripd.cur->value;
8081 : 375 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
8082 : : }
8083 : :
8084 : : /* If it's a string literal, then it's the initializer for the array
8085 : : as a whole. Otherwise, continue with normal initialization for
8086 : : array types (one value per array element). */
8087 : 88913 : if (TREE_CODE (stripped_str_init) == STRING_CST)
8088 : : {
8089 : 72 : if ((first_initializer_p && has_designator_problem (d, complain))
8090 : 1025 : || (stripd.cur && has_designator_problem (&stripd, complain)))
8091 : 953 : return error_mark_node;
8092 : 953 : d->cur++;
8093 : 953 : return str_init;
8094 : : }
8095 : : }
8096 : :
8097 : : /* The following cases are about aggregates. If we are not within a full
8098 : : initializer already, and there is not a CONSTRUCTOR, it means that there
8099 : : is a missing set of braces (that is, we are processing the case for
8100 : : which reshape_init exists). */
8101 : 6122033 : bool braces_elided_p = false;
8102 : 6122033 : if (!first_initializer_p)
8103 : : {
8104 : 2114496 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
8105 : : {
8106 : 2105654 : tree init_type = TREE_TYPE (init);
8107 : 2105654 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
8108 : : /* There is no need to call reshape_init for pointer-to-member
8109 : : function initializers, as they are always constructed correctly
8110 : : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
8111 : : which is missing outermost braces. We should warn below, and
8112 : : one of the routines below will wrap it in additional { }. */;
8113 : : /* For a nested compound literal, proceed to specialized routines,
8114 : : to handle initialization of arrays and similar. */
8115 : 2105645 : else if (COMPOUND_LITERAL_P (stripped_init))
8116 : 6 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8117 : : /* If we have an unresolved designator, we need to find the member it
8118 : : designates within TYPE, so proceed to the routines below. For
8119 : : FIELD_DECL or INTEGER_CST designators, we're already initializing
8120 : : the designated element. */
8121 : 2105639 : else if (d->cur->index
8122 : 27 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
8123 : : /* Brace elision with designators is only permitted for anonymous
8124 : : aggregates. */
8125 : 15 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
8126 : : /* A CONSTRUCTOR of the target's type is a previously
8127 : : digested initializer. */
8128 : 2105624 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
8129 : : {
8130 : 0 : ++d->cur;
8131 : 0 : return init;
8132 : : }
8133 : : else
8134 : : {
8135 : : /* Something that hasn't been reshaped yet. */
8136 : 2105624 : ++d->cur;
8137 : 2105624 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
8138 : 2105624 : return reshape_init (type, init, complain);
8139 : : }
8140 : : }
8141 : :
8142 : 8872 : if (complain & tf_warning)
8143 : 7723 : warning (OPT_Wmissing_braces,
8144 : : "missing braces around initializer for %qT",
8145 : : type);
8146 : : braces_elided_p = true;
8147 : : }
8148 : :
8149 : : /* Dispatch to specialized routines. */
8150 : 4016409 : tree new_init;
8151 : 4016409 : if (CLASS_TYPE_P (type))
8152 : 3652420 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
8153 : 363989 : else if (TREE_CODE (type) == ARRAY_TYPE)
8154 : 322797 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
8155 : 41192 : else if (VECTOR_TYPE_P (type))
8156 : 41192 : new_init = reshape_init_vector (type, d, complain);
8157 : : else
8158 : 0 : gcc_unreachable ();
8159 : :
8160 : 4016409 : if (braces_elided_p
8161 : 8872 : && TREE_CODE (new_init) == CONSTRUCTOR)
8162 : 8596 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
8163 : :
8164 : : return new_init;
8165 : : }
8166 : :
8167 : : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
8168 : : brace-enclosed aggregate initializer.
8169 : :
8170 : : INIT is the CONSTRUCTOR containing the list of initializers describing
8171 : : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
8172 : : It may not presently match the shape of the TYPE; for example:
8173 : :
8174 : : struct S { int a; int b; };
8175 : : struct S a[] = { 1, 2, 3, 4 };
8176 : :
8177 : : Here INIT will hold a vector of four elements, rather than a
8178 : : vector of two elements, each itself a vector of two elements. This
8179 : : routine transforms INIT from the former form into the latter. The
8180 : : revised CONSTRUCTOR node is returned. */
8181 : :
8182 : : tree
8183 : 9637900 : reshape_init (tree type, tree init, tsubst_flags_t complain)
8184 : : {
8185 : 9637900 : vec<constructor_elt, va_gc> *v;
8186 : 9637900 : reshape_iter d;
8187 : 9637900 : tree new_init;
8188 : :
8189 : 9637900 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
8190 : :
8191 : 9637900 : v = CONSTRUCTOR_ELTS (init);
8192 : :
8193 : : /* An empty constructor does not need reshaping, and it is always a valid
8194 : : initializer. */
8195 : 9638503 : if (vec_safe_is_empty (v))
8196 : : return init;
8197 : :
8198 : 4073644 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
8199 : : /* Already reshaped. */
8200 : : return init;
8201 : :
8202 : : /* Brace elision is not performed for a CONSTRUCTOR representing
8203 : : parenthesized aggregate initialization. */
8204 : 4073507 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
8205 : : {
8206 : 495 : tree elt = (*v)[0].value;
8207 : : /* If we're initializing a char array from a string-literal that is
8208 : : enclosed in braces, unwrap it here. */
8209 : 495 : if (TREE_CODE (type) == ARRAY_TYPE
8210 : 221 : && vec_safe_length (v) == 1
8211 : 121 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8212 : 528 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
8213 : : return elt;
8214 : 466 : return init;
8215 : : }
8216 : :
8217 : : /* Handle [dcl.init.list] direct-list-initialization from
8218 : : single element of enumeration with a fixed underlying type. */
8219 : 4073012 : if (is_direct_enum_init (type, init))
8220 : : {
8221 : 176 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
8222 : 176 : type = cv_unqualified (type);
8223 : 176 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8224 : : {
8225 : 161 : warning_sentinel w (warn_useless_cast);
8226 : 161 : warning_sentinel w2 (warn_ignored_qualifiers);
8227 : 161 : return cp_build_c_cast (input_location, type, elt,
8228 : : tf_warning_or_error);
8229 : 161 : }
8230 : : else
8231 : 15 : return error_mark_node;
8232 : : }
8233 : :
8234 : : /* Recurse on this CONSTRUCTOR. */
8235 : 4072836 : d.cur = &(*v)[0];
8236 : 4072836 : d.end = d.cur + v->length ();
8237 : 4072836 : d.raw_idx = 0;
8238 : :
8239 : 4072836 : new_init = reshape_init_r (type, &d, init, complain);
8240 : 4072836 : if (new_init == error_mark_node)
8241 : : return error_mark_node;
8242 : :
8243 : : /* Make sure all the element of the constructor were used. Otherwise,
8244 : : issue an error about exceeding initializers. */
8245 : 4072213 : if (d.cur != d.end)
8246 : : {
8247 : 159 : if (complain & tf_error)
8248 : 63 : error ("too many initializers for %qT", type);
8249 : 159 : return error_mark_node;
8250 : : }
8251 : :
8252 : 4072054 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
8253 : 4072054 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8254 : 464901 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
8255 : 4072054 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
8256 : 4072054 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
8257 : 30791 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
8258 : : || seen_error ());
8259 : :
8260 : : return new_init;
8261 : : }
8262 : :
8263 : : /* Verify array initializer. Returns true if errors have been reported. */
8264 : :
8265 : : bool
8266 : 878014 : check_array_initializer (tree decl, tree type, tree init)
8267 : : {
8268 : 878014 : tree element_type = TREE_TYPE (type);
8269 : :
8270 : : /* Structured binding when initialized with an array type needs
8271 : : to have complete type. */
8272 : 878014 : if (decl
8273 : 872560 : && DECL_DECOMPOSITION_P (decl)
8274 : 363 : && DECL_DECOMP_IS_BASE (decl)
8275 : 878377 : && !COMPLETE_TYPE_P (type))
8276 : : {
8277 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
8278 : : "structured binding has incomplete type %qT", type);
8279 : 0 : TREE_TYPE (decl) = error_mark_node;
8280 : 0 : return true;
8281 : : }
8282 : :
8283 : : /* The array type itself need not be complete, because the
8284 : : initializer may tell us how many elements are in the array.
8285 : : But, the elements of the array must be complete. */
8286 : 878014 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
8287 : : {
8288 : 15 : if (decl)
8289 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
8290 : : "elements of array %q#D have incomplete type", decl);
8291 : : else
8292 : 6 : error ("elements of array %q#T have incomplete type", type);
8293 : 15 : return true;
8294 : : }
8295 : :
8296 : 877999 : location_t loc = (decl ? location_of (decl) : input_location);
8297 : 877999 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
8298 : : return true;
8299 : :
8300 : : /* A compound literal can't have variable size. */
8301 : 877999 : if (init && !decl
8302 : 877999 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
8303 : 5445 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
8304 : : {
8305 : 3 : error ("variable-sized compound literal");
8306 : 3 : return true;
8307 : : }
8308 : : return false;
8309 : : }
8310 : :
8311 : : /* Subroutine of check_initializer; args are passed down from that function.
8312 : : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
8313 : :
8314 : : static tree
8315 : 2388325 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
8316 : : {
8317 : 2388325 : gcc_assert (stmts_are_full_exprs_p ());
8318 : 2388325 : if (init)
8319 : 1755140 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
8320 : 2388325 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
8321 : : }
8322 : :
8323 : : /* Verify INIT (the initializer for DECL), and record the
8324 : : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
8325 : : grok_reference_init.
8326 : :
8327 : : If the return value is non-NULL, it is an expression that must be
8328 : : evaluated dynamically to initialize DECL. */
8329 : :
8330 : : static tree
8331 : 42677479 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
8332 : : {
8333 : 42677479 : tree type;
8334 : 42677479 : tree init_code = NULL;
8335 : 42677479 : tree core_type;
8336 : :
8337 : : /* Things that are going to be initialized need to have complete
8338 : : type. */
8339 : 42677479 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8340 : :
8341 : 42677479 : if (DECL_HAS_VALUE_EXPR_P (decl))
8342 : : {
8343 : : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
8344 : : it doesn't have storage to be initialized. */
8345 : 138 : gcc_assert (init == NULL_TREE);
8346 : : return NULL_TREE;
8347 : : }
8348 : :
8349 : 42677341 : if (type == error_mark_node)
8350 : : /* We will have already complained. */
8351 : : return NULL_TREE;
8352 : :
8353 : 42677341 : if (TREE_CODE (type) == ARRAY_TYPE)
8354 : : {
8355 : 872560 : if (check_array_initializer (decl, type, init))
8356 : : return NULL_TREE;
8357 : : }
8358 : 41804781 : else if (!COMPLETE_TYPE_P (type))
8359 : : {
8360 : 37 : error_at (DECL_SOURCE_LOCATION (decl),
8361 : : "%q#D has incomplete type", decl);
8362 : 37 : TREE_TYPE (decl) = error_mark_node;
8363 : 37 : return NULL_TREE;
8364 : : }
8365 : : else
8366 : : /* There is no way to make a variable-sized class type in GNU C++. */
8367 : 41804744 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
8368 : :
8369 : 42677295 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
8370 : : {
8371 : 1506352 : int init_len = CONSTRUCTOR_NELTS (init);
8372 : 1506352 : if (SCALAR_TYPE_P (type))
8373 : : {
8374 : 53663 : if (init_len == 0)
8375 : : {
8376 : 44448 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8377 : 44448 : init = build_zero_init (type, NULL_TREE, false);
8378 : : }
8379 : 9215 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
8380 : : {
8381 : 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8382 : : "scalar object %qD requires one element in "
8383 : : "initializer", decl);
8384 : 6 : TREE_TYPE (decl) = error_mark_node;
8385 : 6 : return NULL_TREE;
8386 : : }
8387 : : }
8388 : : }
8389 : :
8390 : 42677289 : if (TREE_CODE (decl) == CONST_DECL)
8391 : : {
8392 : 0 : gcc_assert (!TYPE_REF_P (type));
8393 : :
8394 : 0 : DECL_INITIAL (decl) = init;
8395 : :
8396 : 0 : gcc_assert (init != NULL_TREE);
8397 : : init = NULL_TREE;
8398 : : }
8399 : 42677289 : else if (!init && DECL_REALLY_EXTERN (decl))
8400 : : ;
8401 : 42677289 : else if (flag_openmp
8402 : 131583 : && VAR_P (decl)
8403 : 131583 : && DECL_LANG_SPECIFIC (decl)
8404 : 56715 : && DECL_OMP_DECLARE_MAPPER_P (decl)
8405 : 42677289 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
8406 : : return NULL_TREE;
8407 : 4287876 : else if (init || type_build_ctor_call (type)
8408 : 46331977 : || TYPE_REF_P (type))
8409 : : {
8410 : 39022616 : if (TYPE_REF_P (type))
8411 : : {
8412 : 320569 : init = grok_reference_init (decl, type, init, flags);
8413 : 320569 : flags |= LOOKUP_ALREADY_DIGESTED;
8414 : : }
8415 : 38702047 : else if (!init)
8416 : 633188 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8417 : : tf_warning_or_error);
8418 : : /* Do not reshape constructors of vectors (they don't need to be
8419 : : reshaped. */
8420 : 38068859 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8421 : : {
8422 : 1461577 : if (is_std_init_list (type))
8423 : : {
8424 : 331 : init = perform_implicit_conversion (type, init,
8425 : : tf_warning_or_error);
8426 : 331 : flags |= LOOKUP_ALREADY_DIGESTED;
8427 : : }
8428 : 1461246 : else if (TYPE_NON_AGGREGATE_CLASS (type))
8429 : : {
8430 : : /* Don't reshape if the class has constructors. */
8431 : 216299 : if (cxx_dialect == cxx98)
8432 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
8433 : : "in C++98 %qD must be initialized by "
8434 : : "constructor, not by %<{...}%>",
8435 : : decl);
8436 : : }
8437 : 1244947 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
8438 : : {
8439 : 0 : error ("opaque vector types cannot be initialized");
8440 : 0 : init = error_mark_node;
8441 : : }
8442 : : else
8443 : : {
8444 : 1244947 : init = reshape_init (type, init, tf_warning_or_error);
8445 : 1244947 : flags |= LOOKUP_NO_NARROWING;
8446 : : }
8447 : : }
8448 : : /* [dcl.init] "Otherwise, if the destination type is an array, the object
8449 : : is initialized as follows..." So handle things like
8450 : :
8451 : : int a[](1, 2, 3);
8452 : :
8453 : : which is permitted in C++20 by P0960. */
8454 : 36607282 : else if (TREE_CODE (init) == TREE_LIST
8455 : 625992 : && TREE_TYPE (init) == NULL_TREE
8456 : 625992 : && TREE_CODE (type) == ARRAY_TYPE
8457 : 165 : && !DECL_DECOMPOSITION_P (decl)
8458 : 36607426 : && (cxx_dialect >= cxx20))
8459 : 124 : init = do_aggregate_paren_init (init, type);
8460 : 36607158 : else if (TREE_CODE (init) == TREE_LIST
8461 : 625868 : && TREE_TYPE (init) != unknown_type_node
8462 : 37233026 : && !MAYBE_CLASS_TYPE_P (type))
8463 : : {
8464 : 134484 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8465 : :
8466 : : /* We get here with code like `int a (2);' */
8467 : 134484 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8468 : : tf_warning_or_error);
8469 : : }
8470 : :
8471 : : /* If DECL has an array type without a specific bound, deduce the
8472 : : array size from the initializer. */
8473 : 39022616 : maybe_deduce_size_from_array_init (decl, init);
8474 : 39022616 : type = TREE_TYPE (decl);
8475 : 39022616 : if (type == error_mark_node)
8476 : : return NULL_TREE;
8477 : :
8478 : 75233013 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
8479 : 3472222 : && !(flags & LOOKUP_ALREADY_DIGESTED)
8480 : 3471891 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
8481 : 1301620 : && CP_AGGREGATE_TYPE_P (type)
8482 : 1085321 : && (CLASS_TYPE_P (type)
8483 : : /* The call to build_aggr_init below could end up
8484 : : calling build_vec_init, which may break when we
8485 : : are processing a template. */
8486 : 1589 : || processing_template_decl
8487 : 1523 : || !TYPE_NEEDS_CONSTRUCTING (type)
8488 : 1491 : || type_has_extended_temps (type))))
8489 : 75657221 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8490 : : {
8491 : 2388325 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8492 : :
8493 : : /* A constructor call is a non-trivial initializer even if
8494 : : it isn't explicitly written. */
8495 : 2388325 : if (TREE_SIDE_EFFECTS (init_code))
8496 : 2379742 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8497 : :
8498 : : /* If this is a constexpr initializer, expand_default_init will
8499 : : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8500 : : case, pull the initializer back out and pass it down into
8501 : : store_init_value. */
8502 : 6202935 : while (true)
8503 : : {
8504 : 6202935 : if (TREE_CODE (init_code) == EXPR_STMT
8505 : 3825809 : || TREE_CODE (init_code) == STMT_EXPR
8506 : 3816171 : || TREE_CODE (init_code) == CONVERT_EXPR)
8507 : 3805018 : init_code = TREE_OPERAND (init_code, 0);
8508 : 2397917 : else if (TREE_CODE (init_code) == BIND_EXPR)
8509 : 9592 : init_code = BIND_EXPR_BODY (init_code);
8510 : : else
8511 : : break;
8512 : : }
8513 : 2388325 : if (TREE_CODE (init_code) == INIT_EXPR)
8514 : : {
8515 : : /* In C++20, the call to build_aggr_init could have created
8516 : : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8517 : : A(1, 2). */
8518 : 1424248 : tree rhs = TREE_OPERAND (init_code, 1);
8519 : 1424248 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8520 : : /* Avoid leaking TARGET_EXPR into template trees. */
8521 : 4814 : rhs = build_implicit_conv_flags (type, init, flags);
8522 : 1424248 : init = rhs;
8523 : :
8524 : 1424248 : init_code = NULL_TREE;
8525 : : /* Don't call digest_init; it's unnecessary and will complain
8526 : : about aggregate initialization of non-aggregate classes. */
8527 : 1424233 : flags |= LOOKUP_ALREADY_DIGESTED;
8528 : : }
8529 : 964077 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8530 : 1922979 : || DECL_DECLARED_CONSTINIT_P (decl))
8531 : : {
8532 : : /* Declared constexpr or constinit, but no suitable initializer;
8533 : : massage init appropriately so we can pass it into
8534 : : store_init_value for the error. */
8535 : 5193 : tree new_init = NULL_TREE;
8536 : 5193 : if (!processing_template_decl
8537 : 384 : && TREE_CODE (init_code) == CALL_EXPR)
8538 : 328 : new_init = build_cplus_new (type, init_code, tf_none);
8539 : 4841 : else if (CLASS_TYPE_P (type)
8540 : 9706 : && (!init || TREE_CODE (init) == TREE_LIST))
8541 : 4781 : new_init = build_functional_cast (input_location, type,
8542 : : init, tf_none);
8543 : 5109 : if (new_init)
8544 : : {
8545 : 5109 : init = new_init;
8546 : 5109 : if (TREE_CODE (init) == TARGET_EXPR
8547 : 349 : && !(flags & LOOKUP_ONLYCONVERTING))
8548 : 349 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8549 : : }
8550 : : init_code = NULL_TREE;
8551 : : }
8552 : : else
8553 : : init = NULL_TREE;
8554 : : }
8555 : :
8556 : 38063727 : if (init && TREE_CODE (init) != TREE_VEC)
8557 : : {
8558 : 38063712 : init_code = store_init_value (decl, init, cleanups, flags);
8559 : :
8560 : 38061015 : if (DECL_INITIAL (decl)
8561 : 24794442 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8562 : 39725066 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8563 : : {
8564 : 1012706 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8565 : 1012706 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8566 : 1012706 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8567 : 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8568 : : }
8569 : :
8570 : 913090 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8571 : 49215 : && DECL_INITIAL (decl)
8572 : 48788 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8573 : 38106958 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8574 : 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8575 : 3 : DECL_SOURCE_LOCATION (decl)),
8576 : 3 : 0, "array %qD initialized by parenthesized "
8577 : : "string literal %qE",
8578 : 3 : decl, DECL_INITIAL (decl));
8579 : : init = NULL_TREE;
8580 : : }
8581 : : }
8582 : : else
8583 : : {
8584 : 7309346 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8585 : 4309304 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8586 : 654618 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8587 : 19 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8588 : : /*complain=*/true);
8589 : :
8590 : 3654673 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8591 : : tf_warning_or_error);
8592 : : }
8593 : :
8594 : 0 : if (init && init != error_mark_node)
8595 : 0 : init_code = cp_build_init_expr (decl, init);
8596 : :
8597 : 14342938 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8598 : 42693688 : && init_code != error_mark_node)
8599 : : init_code = NULL_TREE;
8600 : :
8601 : 42657709 : if (init_code)
8602 : : {
8603 : : /* We might have set these in cp_finish_decl. */
8604 : 14326060 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8605 : 14326060 : TREE_CONSTANT (decl) = false;
8606 : : }
8607 : :
8608 : 14326060 : if (init_code
8609 : 14326060 : && DECL_IN_AGGR_P (decl)
8610 : 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8611 : : {
8612 : 0 : static int explained = 0;
8613 : :
8614 : 0 : auto_diagnostic_group d;
8615 : 0 : if (cxx_dialect < cxx11)
8616 : 0 : error ("initializer invalid for static member with constructor");
8617 : 0 : else if (cxx_dialect < cxx17)
8618 : 0 : error ("non-constant in-class initialization invalid for static "
8619 : : "member %qD", decl);
8620 : : else
8621 : 0 : error ("non-constant in-class initialization invalid for non-inline "
8622 : : "static member %qD", decl);
8623 : 0 : if (!explained)
8624 : : {
8625 : 0 : inform (input_location,
8626 : : "(an out of class initialization is required)");
8627 : 0 : explained = 1;
8628 : : }
8629 : 0 : return NULL_TREE;
8630 : 0 : }
8631 : :
8632 : : return init_code;
8633 : : }
8634 : :
8635 : : /* If DECL is not a local variable, give it RTL. */
8636 : :
8637 : : static void
8638 : 102101636 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8639 : : {
8640 : 102101636 : int toplev = toplevel_bindings_p ();
8641 : 102101636 : int defer_p;
8642 : :
8643 : : /* Set the DECL_ASSEMBLER_NAME for the object. */
8644 : 102101636 : if (asmspec)
8645 : : {
8646 : : /* The `register' keyword, when used together with an
8647 : : asm-specification, indicates that the variable should be
8648 : : placed in a particular register. */
8649 : 973533 : if (VAR_P (decl) && DECL_REGISTER (decl))
8650 : : {
8651 : 81 : set_user_assembler_name (decl, asmspec);
8652 : 81 : DECL_HARD_REGISTER (decl) = 1;
8653 : : }
8654 : : else
8655 : : {
8656 : 973452 : if (TREE_CODE (decl) == FUNCTION_DECL
8657 : 973452 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8658 : 77172 : set_builtin_user_assembler_name (decl, asmspec);
8659 : 973452 : set_user_assembler_name (decl, asmspec);
8660 : 973452 : if (DECL_LOCAL_DECL_P (decl))
8661 : 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8662 : : /* We have to propagate the name to the ns-alias.
8663 : : This is horrible, as we're affecting a
8664 : : possibly-shared decl. Again, a one-true-decl
8665 : : model breaks down. */
8666 : 6 : if (ns_decl != error_mark_node)
8667 : 6 : set_user_assembler_name (ns_decl, asmspec);
8668 : : }
8669 : : }
8670 : :
8671 : : /* Handle non-variables up front. */
8672 : 102101636 : if (!VAR_P (decl))
8673 : : {
8674 : 53669948 : rest_of_decl_compilation (decl, toplev, at_eof);
8675 : 53669948 : return;
8676 : : }
8677 : :
8678 : : /* If we see a class member here, it should be a static data
8679 : : member. */
8680 : 48431688 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8681 : : {
8682 : 4024588 : gcc_assert (TREE_STATIC (decl));
8683 : : /* An in-class declaration of a static data member should be
8684 : : external; it is only a declaration, and not a definition. */
8685 : 4024588 : if (init == NULL_TREE)
8686 : 4024578 : gcc_assert (DECL_EXTERNAL (decl)
8687 : : || !TREE_PUBLIC (decl));
8688 : : }
8689 : :
8690 : : /* We don't create any RTL for local variables. */
8691 : 48431688 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8692 : : return;
8693 : :
8694 : : /* We defer emission of local statics until the corresponding
8695 : : DECL_EXPR is expanded. But with constexpr its function might never
8696 : : be expanded, so go ahead and tell cgraph about the variable now. */
8697 : 50379904 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8698 : 141832 : && !var_in_maybe_constexpr_fn (decl))
8699 : 50238202 : || DECL_VIRTUAL_P (decl));
8700 : :
8701 : : /* Defer template instantiations. */
8702 : 25189952 : if (DECL_LANG_SPECIFIC (decl)
8703 : 25189952 : && DECL_IMPLICIT_INSTANTIATION (decl))
8704 : : defer_p = 1;
8705 : :
8706 : : /* If we're not deferring, go ahead and assemble the variable. */
8707 : 15551540 : if (!defer_p)
8708 : 13231237 : rest_of_decl_compilation (decl, toplev, at_eof);
8709 : : }
8710 : :
8711 : : /* walk_tree helper for wrap_temporary_cleanups, below. */
8712 : :
8713 : : static tree
8714 : 10450248 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8715 : : {
8716 : : /* Stop at types or full-expression boundaries. */
8717 : 10450248 : if (TYPE_P (*stmt_p)
8718 : 10450037 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8719 : : {
8720 : 439 : *walk_subtrees = 0;
8721 : 439 : return NULL_TREE;
8722 : : }
8723 : :
8724 : 10449809 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8725 : : {
8726 : 179698 : tree guard = (tree)data;
8727 : 179698 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8728 : :
8729 : 99230 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8730 : 272219 : && !expr_noexcept_p (tcleanup, tf_none))
8731 : : {
8732 : 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8733 : : /* Tell honor_protect_cleanup_actions to handle this as a separate
8734 : : cleanup. */
8735 : 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8736 : 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8737 : : }
8738 : : }
8739 : :
8740 : : return NULL_TREE;
8741 : : }
8742 : :
8743 : : /* We're initializing a local variable which has a cleanup GUARD. If there
8744 : : are any temporaries used in the initializer INIT of this variable, we
8745 : : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8746 : : variable will be cleaned up properly if one of them throws.
8747 : :
8748 : : Unfortunately, there's no way to express this properly in terms of
8749 : : nesting, as the regions for the temporaries overlap the region for the
8750 : : variable itself; if there are two temporaries, the variable needs to be
8751 : : the first thing destroyed if either of the temporary destructors throws.
8752 : : However, we only want to run the variable's cleanup if it actually got
8753 : : constructed. So we need to guard the temporary cleanups with the
8754 : : variable's cleanup if they are run on the normal path, but not if they
8755 : : are run on the exceptional path. We implement this by telling
8756 : : honor_protect_cleanup_actions to strip the variable cleanup from the
8757 : : exceptional path.
8758 : :
8759 : : Another approach could be to make the variable cleanup region enclose
8760 : : initialization, but depend on a flag to indicate that the variable is
8761 : : initialized; that's effectively what we do for arrays. But the current
8762 : : approach works fine for non-arrays, and has no code overhead in the usual
8763 : : case where the temporary destructors are noexcept. */
8764 : :
8765 : : static void
8766 : 963257 : wrap_temporary_cleanups (tree init, tree guard)
8767 : : {
8768 : 963257 : if (TREE_CODE (guard) == BIND_EXPR)
8769 : : {
8770 : : /* An array cleanup region already encloses any temporary cleanups,
8771 : : don't wrap it around them again. */
8772 : 422 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8773 : : return;
8774 : : }
8775 : 962835 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8776 : : }
8777 : :
8778 : : /* Generate code to initialize DECL (a local variable). */
8779 : :
8780 : : static void
8781 : 23241718 : initialize_local_var (tree decl, tree init, bool decomp)
8782 : : {
8783 : 23241718 : tree type = TREE_TYPE (decl);
8784 : 23241718 : tree cleanup;
8785 : 23241718 : int already_used;
8786 : :
8787 : 23241718 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8788 : 23241718 : gcc_assert (!TREE_STATIC (decl));
8789 : :
8790 : 23241718 : if (DECL_SIZE (decl) == NULL_TREE)
8791 : : {
8792 : : /* If we used it already as memory, it must stay in memory. */
8793 : 0 : DECL_INITIAL (decl) = NULL_TREE;
8794 : 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8795 : 0 : return;
8796 : : }
8797 : :
8798 : 23241718 : if (type == error_mark_node)
8799 : : return;
8800 : :
8801 : : /* Compute and store the initial value. */
8802 : 23241718 : already_used = TREE_USED (decl) || TREE_USED (type);
8803 : 23241718 : if (TREE_USED (type))
8804 : 32 : DECL_READ_P (decl) = 1;
8805 : :
8806 : : /* Generate a cleanup, if necessary. */
8807 : 23241718 : cleanup = (decomp ? NULL_TREE
8808 : 23222831 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8809 : :
8810 : : /* Perform the initialization. */
8811 : 23241718 : if (init)
8812 : : {
8813 : 14311872 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8814 : 14311872 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8815 : 13097287 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8816 : 20391352 : && TREE_OPERAND (init, 0) == decl)
8817 : : {
8818 : : /* Stick simple initializers in DECL_INITIAL so that
8819 : : -Wno-init-self works (c++/34772). */
8820 : 7294065 : DECL_INITIAL (decl) = rinit;
8821 : :
8822 : 7294065 : if (warn_init_self && TYPE_REF_P (type))
8823 : : {
8824 : 2548 : STRIP_NOPS (rinit);
8825 : 2548 : if (rinit == decl)
8826 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
8827 : 3 : OPT_Winit_self,
8828 : : "reference %qD is initialized with itself", decl);
8829 : : }
8830 : : }
8831 : : else
8832 : : {
8833 : 7017807 : int saved_stmts_are_full_exprs_p;
8834 : :
8835 : : /* If we're only initializing a single object, guard the
8836 : : destructors of any temporaries used in its initializer with
8837 : : its destructor. */
8838 : 7017807 : if (cleanup)
8839 : 957136 : wrap_temporary_cleanups (init, cleanup);
8840 : :
8841 : 7017807 : gcc_assert (building_stmt_list_p ());
8842 : 7017807 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8843 : : /* Avoid CLEANUP_POINT_EXPR for the structured binding
8844 : : bases, those will have CLEANUP_POINT_EXPR at the end of
8845 : : code emitted by cp_finish_decomp. */
8846 : 7017807 : if (decomp)
8847 : 18463 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8848 : : else
8849 : 6999344 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8850 : 7017807 : finish_expr_stmt (init);
8851 : 7017807 : current_stmt_tree ()->stmts_are_full_exprs_p
8852 : 7017807 : = saved_stmts_are_full_exprs_p;
8853 : : }
8854 : : }
8855 : :
8856 : : /* Set this to 0 so we can tell whether an aggregate which was
8857 : : initialized was ever used. Don't do this if it has a
8858 : : destructor, so we don't complain about the 'resource
8859 : : allocation is initialization' idiom. Now set
8860 : : attribute((unused)) on types so decls of that type will be
8861 : : marked used. (see TREE_USED, above.) */
8862 : 23241718 : if (TYPE_NEEDS_CONSTRUCTING (type)
8863 : 1924909 : && ! already_used
8864 : 4595 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8865 : 23245888 : && DECL_NAME (decl))
8866 : 4142 : TREE_USED (decl) = 0;
8867 : 23237576 : else if (already_used)
8868 : 21012079 : TREE_USED (decl) = 1;
8869 : :
8870 : 23241718 : if (cleanup)
8871 : 969856 : finish_decl_cleanup (decl, cleanup);
8872 : : }
8873 : :
8874 : : /* DECL is a VAR_DECL for a compiler-generated variable with static
8875 : : storage duration (like a virtual table) whose initializer is a
8876 : : compile-time constant. Initialize the variable and provide it to the
8877 : : back end. */
8878 : :
8879 : : void
8880 : 2137127 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8881 : : {
8882 : 2137127 : tree init;
8883 : 2137127 : gcc_assert (DECL_ARTIFICIAL (decl));
8884 : 2137127 : init = build_constructor (TREE_TYPE (decl), v);
8885 : 2137127 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8886 : 2137127 : DECL_INITIAL (decl) = init;
8887 : 2137127 : DECL_INITIALIZED_P (decl) = 1;
8888 : : /* Mark the decl as constexpr so that we can access its content
8889 : : at compile time. */
8890 : 2137127 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8891 : 2137127 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
8892 : 2137127 : determine_visibility (decl);
8893 : 2137127 : layout_var_decl (decl);
8894 : 2137127 : maybe_commonize_var (decl);
8895 : 2137127 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8896 : 2137127 : }
8897 : :
8898 : : /* INIT is the initializer for a variable, as represented by the
8899 : : parser. Returns true iff INIT is value-dependent. */
8900 : :
8901 : : static bool
8902 : 8098862 : value_dependent_init_p (tree init)
8903 : : {
8904 : 8098862 : if (TREE_CODE (init) == TREE_LIST)
8905 : : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8906 : 9159 : return any_value_dependent_elements_p (init);
8907 : 8089703 : else if (TREE_CODE (init) == CONSTRUCTOR)
8908 : : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8909 : : {
8910 : 40740 : if (dependent_type_p (TREE_TYPE (init)))
8911 : : return true;
8912 : :
8913 : 40734 : vec<constructor_elt, va_gc> *elts;
8914 : 40734 : size_t nelts;
8915 : 40734 : size_t i;
8916 : :
8917 : 40734 : elts = CONSTRUCTOR_ELTS (init);
8918 : 40734 : nelts = vec_safe_length (elts);
8919 : 566681 : for (i = 0; i < nelts; ++i)
8920 : 540792 : if (value_dependent_init_p ((*elts)[i].value))
8921 : : return true;
8922 : : }
8923 : : else
8924 : : /* It must be a simple expression, e.g., int i = 3; */
8925 : 8048963 : return value_dependent_expression_p (init);
8926 : :
8927 : : return false;
8928 : : }
8929 : :
8930 : : /* A helper function to be called via walk_tree. If any label exists
8931 : : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8932 : :
8933 : : static tree
8934 : 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8935 : : {
8936 : 337 : if (TYPE_P (*tp))
8937 : 0 : *walk_subtrees = 0;
8938 : 337 : if (TREE_CODE (*tp) == LABEL_DECL)
8939 : 6 : cfun->has_forced_label_in_static = 1;
8940 : 337 : return NULL_TREE;
8941 : : }
8942 : :
8943 : : /* Return true if DECL has either a trivial destructor, or for C++20
8944 : : is constexpr and has a constexpr destructor. */
8945 : :
8946 : : static bool
8947 : 37134566 : decl_maybe_constant_destruction (tree decl, tree type)
8948 : : {
8949 : 37134566 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8950 : 37134566 : || (cxx_dialect >= cxx20
8951 : 1330961 : && VAR_P (decl)
8952 : 1330706 : && DECL_DECLARED_CONSTEXPR_P (decl)
8953 : 339 : && type_has_constexpr_destructor (strip_array_types (type))));
8954 : : }
8955 : :
8956 : : static tree declare_simd_adjust_this (tree *, int *, void *);
8957 : :
8958 : : /* Helper function of omp_declare_variant_finalize. Finalize one
8959 : : "omp declare variant base" attribute. Return true if it should be
8960 : : removed. */
8961 : :
8962 : : static bool
8963 : 1488 : omp_declare_variant_finalize_one (tree decl, tree attr)
8964 : : {
8965 : 1488 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8966 : : {
8967 : 116 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8968 : : DECL_ARGUMENTS (decl), NULL);
8969 : 116 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8970 : : DECL_ARGUMENTS (decl), NULL);
8971 : : }
8972 : :
8973 : 1488 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
8974 : 1488 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8975 : : OMP_TRAIT_CONSTRUCT_SIMD);
8976 : 1488 : if (simd)
8977 : : {
8978 : 51 : TREE_VALUE (simd)
8979 : 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8980 : 51 : OMP_TS_PROPERTIES (simd));
8981 : : /* FIXME, adjusting simd args unimplemented. */
8982 : 51 : return true;
8983 : : }
8984 : :
8985 : 1437 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
8986 : 1437 : location_t varid_loc
8987 : 1437 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8988 : 1437 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8989 : 1437 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8990 : 1437 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8991 : :
8992 : 1437 : location_t save_loc = input_location;
8993 : 1437 : input_location = varid_loc;
8994 : :
8995 : 1437 : releasing_vec args;
8996 : 1437 : tree parm = DECL_ARGUMENTS (decl);
8997 : 1437 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8998 : 116 : parm = DECL_CHAIN (parm);
8999 : 2645 : for (; parm; parm = DECL_CHAIN (parm))
9000 : 1208 : vec_safe_push (args, forward_parm (parm));
9001 : :
9002 : 1437 : unsigned nappend_args = 0;
9003 : 1437 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9004 : 1437 : if (append_args_list)
9005 : : {
9006 : 354 : append_args_list = TREE_VALUE (append_args_list);
9007 : 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
9008 : 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
9009 : : : NULL_TREE);
9010 : 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9011 : 387 : nappend_args++;
9012 : 354 : if (nappend_args)
9013 : : {
9014 : 237 : tree type;
9015 : 237 : if ((type = lookup_qualified_name (global_namespace,
9016 : : "omp_interop_t",
9017 : : LOOK_want::NORMAL,
9018 : : /*complain*/false)) == NULL_TREE
9019 : 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
9020 : : {
9021 : 24 : location_t loc = input_location;
9022 : 24 : variant = tree_strip_any_location_wrapper (variant);
9023 : 24 : if (!identifier_p (variant))
9024 : : {
9025 : 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
9026 : 21 : variant = OVL_FIRST (variant);
9027 : 21 : loc = EXPR_LOC_OR_LOC (variant,
9028 : : DECL_SOURCE_LOCATION (variant));
9029 : : }
9030 : 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
9031 : 24 : args->length () + 1, variant);
9032 : 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9033 : : "%<append_args%> specified here");
9034 : 24 : return true;
9035 : : }
9036 : 564 : for (unsigned i = 0; i < nappend_args; i++)
9037 : 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
9038 : : }
9039 : : }
9040 : :
9041 : 1413 : bool koenig_p = false;
9042 : 1413 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
9043 : : {
9044 : 1407 : if (identifier_p (variant)
9045 : : /* In C++20, we may need to perform ADL for a template
9046 : : name. */
9047 : 1383 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
9048 : 18 : && identifier_p (TREE_OPERAND (variant, 0))))
9049 : : {
9050 : 24 : if (!args->is_empty ())
9051 : : {
9052 : 24 : koenig_p = true;
9053 : 24 : if (!any_type_dependent_arguments_p (args))
9054 : 21 : variant = perform_koenig_lookup (variant, args,
9055 : : tf_warning_or_error);
9056 : : }
9057 : : else
9058 : 0 : variant = unqualified_fn_lookup_error (variant);
9059 : : }
9060 : 1383 : else if (!args->is_empty () && is_overloaded_fn (variant))
9061 : : {
9062 : 716 : tree fn = get_first_fn (variant);
9063 : 716 : fn = STRIP_TEMPLATE (fn);
9064 : 716 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
9065 : 716 : || DECL_FUNCTION_MEMBER_P (fn)
9066 : 603 : || DECL_LOCAL_DECL_P (fn)))
9067 : : {
9068 : 603 : koenig_p = true;
9069 : 603 : if (!any_type_dependent_arguments_p (args))
9070 : 552 : variant = perform_koenig_lookup (variant, args,
9071 : : tf_warning_or_error);
9072 : : }
9073 : : }
9074 : : }
9075 : :
9076 : 1413 : if (idk == CP_ID_KIND_QUALIFIED)
9077 : 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
9078 : : koenig_p, tf_warning_or_error);
9079 : : else
9080 : 1407 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
9081 : : koenig_p, tf_warning_or_error);
9082 : 1413 : if (variant == error_mark_node && !processing_template_decl)
9083 : : return true;
9084 : :
9085 : 1368 : if (TREE_CODE (variant) == TARGET_EXPR)
9086 : 9 : variant = TARGET_EXPR_INITIAL (variant);
9087 : :
9088 : 2727 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
9089 : 1368 : input_location = save_loc;
9090 : :
9091 : 1368 : if (variant == decl)
9092 : : {
9093 : 0 : error_at (varid_loc, "variant %qD is the same as base function",
9094 : : variant);
9095 : 0 : return true;
9096 : : }
9097 : :
9098 : 1368 : if (variant)
9099 : : {
9100 : 1284 : bool fail;
9101 : 1284 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
9102 : 1284 : if (!nappend_args)
9103 : 1125 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9104 : : COMPARE_STRICT);
9105 : : else
9106 : : {
9107 : 159 : unsigned nbase_args = 0;
9108 : 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
9109 : 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
9110 : 240 : nbase_args++;
9111 : 159 : tree vargs, varg;
9112 : 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
9113 : 399 : for (unsigned i = 0; i < nbase_args && varg;
9114 : 240 : i++, varg = TREE_CHAIN (varg))
9115 : 240 : vargs = varg;
9116 : 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
9117 : 258 : varg = TREE_CHAIN (varg);
9118 : 159 : tree saved_vargs;
9119 : 159 : int saved_no_named_args_stdarg = 0;
9120 : 159 : if (nbase_args)
9121 : : {
9122 : 111 : saved_vargs = TREE_CHAIN (vargs);
9123 : 111 : TREE_CHAIN (vargs) = varg;
9124 : : }
9125 : : else
9126 : : {
9127 : 48 : saved_vargs = vargs;
9128 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
9129 : 48 : saved_no_named_args_stdarg
9130 : 48 : = TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant));
9131 : 48 : if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl))
9132 : 48 : && varg == NULL_TREE)
9133 : 2 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant)) = 1;
9134 : : }
9135 : : /* Skip assert check that TYPE_CANONICAL is the same. */
9136 : 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
9137 : : COMPARE_STRUCTURAL);
9138 : 159 : if (nbase_args)
9139 : 111 : TREE_CHAIN (vargs) = saved_vargs;
9140 : : else
9141 : : {
9142 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
9143 : 48 : TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (variant))
9144 : 96 : = saved_no_named_args_stdarg;
9145 : : }
9146 : 159 : varg = saved_vargs;
9147 : 159 : if (!fail && !processing_template_decl)
9148 : 402 : for (unsigned i = 0; i < nappend_args;
9149 : 246 : i++, varg = TREE_CHAIN (varg))
9150 : 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
9151 : : {
9152 : 9 : error_at (DECL_SOURCE_LOCATION (variant),
9153 : : "argument %d of %qD must be of %<omp_interop_t%>",
9154 : 9 : nbase_args + i + 1, variant);
9155 : 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
9156 : : "%<append_args%> specified here");
9157 : 9 : break;
9158 : : }
9159 : : }
9160 : 1284 : if (fail)
9161 : : {
9162 : 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
9163 : : "types", variant, decl);
9164 : 69 : return true;
9165 : : }
9166 : 1215 : if (fndecl_built_in_p (variant)
9167 : 1215 : && (startswith (varname, "__builtin_")
9168 : 0 : || startswith (varname, "__sync_")
9169 : 0 : || startswith (varname, "__atomic_")))
9170 : : {
9171 : 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
9172 : 6 : return true;
9173 : : }
9174 : : else
9175 : : {
9176 : 1209 : tree construct
9177 : 1209 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
9178 : 1209 : omp_mark_declare_variant (match_loc, variant, construct);
9179 : 1209 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
9180 : : return true;
9181 : 861 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
9182 : :
9183 : : // Prepend adjust_args list to variant attributes
9184 : 861 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
9185 : 861 : if (adjust_args_list != NULL_TREE)
9186 : : {
9187 : 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
9188 : 285 : && TREE_VALUE (adjust_args_list))
9189 : : {
9190 : : /* Shift arg position for the added 'this' pointer. */
9191 : : /* Handle need_device_ptr */
9192 : 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
9193 : 51 : t; t = TREE_CHAIN (t))
9194 : 30 : TREE_VALUE (t)
9195 : 30 : = build_int_cst (TREE_TYPE (t),
9196 : 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
9197 : : }
9198 : 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
9199 : : {
9200 : : /* Shift likewise the number of args after which the
9201 : : interop object should be added. */
9202 : 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
9203 : 6 : TREE_PURPOSE (nargs)
9204 : 6 : = build_int_cst (TREE_TYPE (nargs),
9205 : 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
9206 : : }
9207 : 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
9208 : 264 : TREE_VALUE (t)
9209 : 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
9210 : 528 : DECL_ATTRIBUTES (variant) = tree_cons (
9211 : : get_identifier ("omp declare variant variant args"),
9212 : 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
9213 : : }
9214 : : }
9215 : : }
9216 : 84 : else if (!processing_template_decl)
9217 : : {
9218 : 0 : error_at (varid_loc, "could not find variant declaration");
9219 : 0 : return true;
9220 : : }
9221 : :
9222 : : return false;
9223 : 1437 : }
9224 : :
9225 : : /* Helper function, finish up "omp declare variant base" attribute
9226 : : now that there is a DECL. ATTR is the first "omp declare variant base"
9227 : : attribute. */
9228 : :
9229 : : void
9230 : 1351 : omp_declare_variant_finalize (tree decl, tree attr)
9231 : : {
9232 : 1351 : size_t attr_len = strlen ("omp declare variant base");
9233 : 1351 : tree *list = &DECL_ATTRIBUTES (decl);
9234 : 1351 : bool remove_all = false;
9235 : 1351 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
9236 : 1351 : if (TREE_CHAIN (TREE_VALUE (attr))
9237 : 1351 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
9238 : 2702 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
9239 : 1351 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
9240 : 2702 : if (DECL_CONSTRUCTOR_P (decl))
9241 : : {
9242 : 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
9243 : 27 : remove_all = true;
9244 : : }
9245 : 1324 : else if (DECL_DESTRUCTOR_P (decl))
9246 : : {
9247 : 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
9248 : 9 : remove_all = true;
9249 : : }
9250 : 1315 : else if (DECL_DEFAULTED_FN (decl))
9251 : : {
9252 : 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
9253 : 2 : remove_all = true;
9254 : : }
9255 : 1313 : else if (DECL_DELETED_FN (decl))
9256 : : {
9257 : 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
9258 : 2 : remove_all = true;
9259 : : }
9260 : 1311 : else if (DECL_VIRTUAL_P (decl))
9261 : : {
9262 : 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
9263 : 0 : remove_all = true;
9264 : : }
9265 : : /* This loop is like private_lookup_attribute, except that it works
9266 : : with tree * rather than tree, as we might want to remove the
9267 : : attributes that are diagnosed as errorneous. */
9268 : 2891 : while (*list)
9269 : : {
9270 : 1540 : tree attr = get_attribute_name (*list);
9271 : 1540 : size_t ident_len = IDENTIFIER_LENGTH (attr);
9272 : 4025 : if (cmp_attribs ("omp declare variant base", attr_len,
9273 : 1540 : IDENTIFIER_POINTER (attr), ident_len))
9274 : : {
9275 : 1528 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
9276 : : {
9277 : 583 : *list = TREE_CHAIN (*list);
9278 : 583 : continue;
9279 : : }
9280 : : }
9281 : 957 : list = &TREE_CHAIN (*list);
9282 : : }
9283 : 1351 : }
9284 : :
9285 : : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
9286 : :
9287 : : /* Finish processing of a declaration;
9288 : : install its line number and initial value.
9289 : : If the length of an array type is not known before,
9290 : : it must be determined now, from the initial value, or it is an error.
9291 : :
9292 : : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
9293 : : true, then INIT is an integral constant expression.
9294 : :
9295 : : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
9296 : : if the (init) syntax was used.
9297 : :
9298 : : DECOMP is first identifier's DECL and identifier count in a structured
9299 : : bindings, nullptr if not a structured binding. */
9300 : :
9301 : : void
9302 : 280980415 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
9303 : : tree asmspec_tree, int flags, cp_decomp *decomp)
9304 : : {
9305 : 280980415 : vec<tree, va_gc> *cleanups = NULL;
9306 : 280980415 : const char *asmspec = NULL;
9307 : 280980415 : int was_readonly = 0;
9308 : 280980415 : bool var_definition_p = false;
9309 : 280980415 : tree auto_node;
9310 : 280980415 : auto_vec<tree> extra_cleanups;
9311 : 280980415 : tree aggregates1 = NULL_TREE;
9312 : 280980415 : struct decomp_cleanup {
9313 : : tree decl;
9314 : : cp_decomp *&decomp;
9315 : 280977718 : ~decomp_cleanup ()
9316 : : {
9317 : 280977718 : if (decomp && DECL_DECOMPOSITION_P (decl))
9318 : 79654 : cp_finish_decomp (decl, decomp);
9319 : 280977718 : }
9320 : 280980415 : } decomp_cl = { decl, decomp };
9321 : :
9322 : 280980415 : if (decl == error_mark_node)
9323 : : return;
9324 : 280980378 : else if (! decl)
9325 : : {
9326 : 0 : if (init)
9327 : 0 : error ("assignment (not initialization) in declaration");
9328 : 0 : return;
9329 : : }
9330 : :
9331 : 280980378 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
9332 : : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
9333 : 280980378 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
9334 : :
9335 : 280980378 : tree type = TREE_TYPE (decl);
9336 : 280980378 : if (type == error_mark_node)
9337 : : return;
9338 : :
9339 : 280979845 : if (VAR_P (decl) && is_copy_initialization (init))
9340 : 77716916 : flags |= LOOKUP_ONLYCONVERTING;
9341 : :
9342 : : /* Warn about register storage specifiers except when in GNU global
9343 : : or local register variable extension. */
9344 : 280979845 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
9345 : : {
9346 : 1928 : if (cxx_dialect >= cxx17)
9347 : 1280 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9348 : : "ISO C++17 does not allow %<register%> storage "
9349 : : "class specifier");
9350 : : else
9351 : 648 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
9352 : : "%<register%> storage class specifier used");
9353 : : }
9354 : :
9355 : : /* If a name was specified, get the string. */
9356 : 280979845 : if (at_namespace_scope_p ())
9357 : 59430457 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
9358 : 280979845 : if (asmspec_tree && asmspec_tree != error_mark_node)
9359 : 973536 : asmspec = TREE_STRING_POINTER (asmspec_tree);
9360 : :
9361 : 280979845 : bool in_class_decl
9362 : 280979845 : = (current_class_type
9363 : 173597477 : && CP_DECL_CONTEXT (decl) == current_class_type
9364 : 116450659 : && TYPE_BEING_DEFINED (current_class_type)
9365 : 395924268 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
9366 : :
9367 : 111608331 : if (in_class_decl
9368 : 111608331 : && (DECL_INITIAL (decl) || init))
9369 : 80307510 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
9370 : :
9371 : 280979845 : if (VAR_P (decl)
9372 : 280979845 : && (auto_node = type_uses_auto (type)))
9373 : : {
9374 : 8604812 : tree d_init;
9375 : 8604812 : if (init == NULL_TREE)
9376 : : {
9377 : 1387 : if (DECL_LANG_SPECIFIC (decl)
9378 : 1309 : && DECL_TEMPLATE_INSTANTIATION (decl)
9379 : 2693 : && !DECL_TEMPLATE_INSTANTIATED (decl))
9380 : : {
9381 : : /* init is null because we're deferring instantiating the
9382 : : initializer until we need it. Well, we need it now. */
9383 : 1300 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
9384 : 1300 : return;
9385 : : }
9386 : :
9387 : 87 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9388 : : /* Class deduction with no initializer is OK. */;
9389 : : else
9390 : : {
9391 : : /* Ordinary auto deduction without an initializer, a situation
9392 : : which grokdeclarator already detects and rejects for the most
9393 : : part. But we can still get here if we're instantiating a
9394 : : variable template before we've fully parsed (and attached) its
9395 : : initializer, e.g. template<class> auto x = x<int>; */
9396 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
9397 : : "declaration of %q#D has no initializer", decl);
9398 : 3 : TREE_TYPE (decl) = error_mark_node;
9399 : 3 : return;
9400 : : }
9401 : : }
9402 : 8603509 : d_init = init;
9403 : 8603509 : if (d_init)
9404 : : {
9405 : 8603425 : if (TREE_CODE (d_init) == TREE_LIST
9406 : 8639178 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
9407 : 22267 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
9408 : : tf_warning_or_error);
9409 : 8603425 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
9410 : : /* Force auto deduction now. Use tf_none to avoid redundant warnings
9411 : : on deprecated-14.C. */
9412 : 8603425 : mark_single_function (d_init, tf_none);
9413 : : }
9414 : 8603509 : enum auto_deduction_context adc = adc_variable_type;
9415 : 8603509 : if (DECL_DECOMPOSITION_P (decl))
9416 : : adc = adc_decomp_type;
9417 : 8603509 : tree outer_targs = NULL_TREE;
9418 : 8603509 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
9419 : 335 : && DECL_LANG_SPECIFIC (decl)
9420 : 126 : && DECL_TEMPLATE_INFO (decl)
9421 : 8603566 : && !DECL_FUNCTION_SCOPE_P (decl))
9422 : : /* The outer template arguments might be needed for satisfaction.
9423 : : (For function scope variables, do_auto_deduction will obtain the
9424 : : outer template arguments from current_function_decl.) */
9425 : 57 : outer_targs = DECL_TI_ARGS (decl);
9426 : 8603509 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
9427 : : tf_warning_or_error, adc,
9428 : : outer_targs, flags);
9429 : 8603509 : if (type == error_mark_node)
9430 : : return;
9431 : 8602668 : if (TREE_CODE (type) == FUNCTION_TYPE)
9432 : : {
9433 : 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
9434 : : "did you forget the %<()%>?", decl);
9435 : 3 : TREE_TYPE (decl) = error_mark_node;
9436 : 3 : return;
9437 : : }
9438 : : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
9439 : 8602665 : if (!processing_template_decl
9440 : 2000208 : && !type_uses_auto (type)
9441 : 10602866 : && !COMPLETE_TYPE_P (complete_type (type)))
9442 : : {
9443 : 18 : auto_diagnostic_group d;
9444 : 18 : error_at (location_of (decl),
9445 : : "deduced type %qT for %qD is incomplete", type, decl);
9446 : 18 : cxx_incomplete_type_inform (type);
9447 : 18 : TREE_TYPE (decl) = error_mark_node;
9448 : 18 : return;
9449 : 18 : }
9450 : :
9451 : : /* Now that we have a type, try these again. */
9452 : 8602647 : layout_decl (decl, 0);
9453 : 8602647 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
9454 : :
9455 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
9456 : 8602647 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
9457 : : {
9458 : 16321 : tree tmpl = template_for_substitution (decl);
9459 : 16321 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
9460 : 14560 : TREE_TYPE (tmpl) = type;
9461 : : }
9462 : : }
9463 : :
9464 : 280977680 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
9465 : : {
9466 : 61 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
9467 : 61 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
9468 : : {
9469 : 6 : init = NULL_TREE;
9470 : 6 : DECL_EXTERNAL (decl) = 1;
9471 : : }
9472 : : }
9473 : :
9474 : 280977680 : if (VAR_P (decl)
9475 : 99269473 : && DECL_CLASS_SCOPE_P (decl)
9476 : 15034814 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
9477 : : TCTX_STATIC_STORAGE, type)
9478 : 296012494 : && DECL_INITIALIZED_IN_CLASS_P (decl))
9479 : 13900959 : check_static_variable_definition (decl, type);
9480 : :
9481 : 280977680 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
9482 : : {
9483 : 23055380 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
9484 : 23055380 : ? TCTX_THREAD_STORAGE
9485 : : : TCTX_STATIC_STORAGE);
9486 : 23055380 : verify_type_context (input_location, context, TREE_TYPE (decl));
9487 : : }
9488 : :
9489 : 280977680 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
9490 : : {
9491 : 350118 : tree clone;
9492 : 350118 : if (init == ridpointers[(int)RID_DELETE]
9493 : 350118 : || (TREE_CODE (init) == STRING_CST
9494 : 24 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9495 : : {
9496 : : /* FIXME check this is 1st decl. */
9497 : 327296 : if (UNLIKELY (DECL_MAIN_P (decl)))
9498 : : {
9499 : : /* [basic.start.main]/3: A program that defines main as deleted
9500 : : is ill-formed. */
9501 : 3 : error ("%<::main%> cannot be deleted");
9502 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9503 : : }
9504 : : else
9505 : : {
9506 : 327293 : DECL_DELETED_FN (decl) = 1;
9507 : 327293 : DECL_DECLARED_INLINE_P (decl) = 1;
9508 : 327293 : DECL_INITIAL (decl)
9509 : 327293 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9510 : 327311 : FOR_EACH_CLONE (clone, decl)
9511 : : {
9512 : 18 : DECL_DELETED_FN (clone) = 1;
9513 : 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9514 : 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9515 : : }
9516 : : }
9517 : 327296 : init = NULL_TREE;
9518 : : }
9519 : 22822 : else if (init == ridpointers[(int)RID_DEFAULT])
9520 : : {
9521 : 22810 : if (defaultable_fn_check (decl))
9522 : 22786 : DECL_DEFAULTED_FN (decl) = 1;
9523 : : else
9524 : 24 : DECL_INITIAL (decl) = NULL_TREE;
9525 : : }
9526 : : }
9527 : :
9528 : 280977680 : if (init && VAR_P (decl))
9529 : : {
9530 : 84814348 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9531 : : /* If DECL is a reference, then we want to know whether init is a
9532 : : reference constant; init_const_expr_p as passed tells us whether
9533 : : it's an rvalue constant. */
9534 : 84814348 : if (TYPE_REF_P (type))
9535 : 2247235 : init_const_expr_p = potential_constant_expression (init);
9536 : 84814348 : if (init_const_expr_p)
9537 : : {
9538 : : /* Set these flags now for templates. We'll update the flags in
9539 : : store_init_value for instantiations. */
9540 : 72768970 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9541 : 72768970 : if (decl_maybe_constant_var_p (decl)
9542 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9543 : 72768970 : && !TYPE_REF_P (type))
9544 : 26466336 : TREE_CONSTANT (decl) = true;
9545 : : }
9546 : : /* This is handled mostly by gimplify.cc, but we have to deal with
9547 : : not warning about int x = x; as it is a GCC extension to turn off
9548 : : this warning but only if warn_init_self is zero. */
9549 : 84814348 : if (!DECL_EXTERNAL (decl)
9550 : 83345240 : && !TREE_STATIC (decl)
9551 : 62672700 : && decl == tree_strip_any_location_wrapper (init)
9552 : 84817607 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9553 : 3235 : suppress_warning (decl, OPT_Winit_self);
9554 : : }
9555 : 196163332 : else if (VAR_P (decl)
9556 : 14455125 : && COMPLETE_TYPE_P (type)
9557 : 11350110 : && !TYPE_REF_P (type)
9558 : 11349746 : && !dependent_type_p (type)
9559 : 207224970 : && is_really_empty_class (type, /*ignore_vptr*/false))
9560 : : /* We have no initializer but there's nothing to initialize anyway.
9561 : : Treat DECL as constant due to c++/109876. */
9562 : 489163 : TREE_CONSTANT (decl) = true;
9563 : :
9564 : 280977680 : if (flag_openmp
9565 : 743189 : && TREE_CODE (decl) == FUNCTION_DECL
9566 : : /* #pragma omp declare variant on methods handled in finish_struct
9567 : : instead. */
9568 : 281315971 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9569 : 129022 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9570 : 209722 : if (tree attr = lookup_attribute ("omp declare variant base",
9571 : 209722 : DECL_ATTRIBUTES (decl)))
9572 : 967 : omp_declare_variant_finalize (decl, attr);
9573 : :
9574 : 280977680 : if (processing_template_decl)
9575 : : {
9576 : 162794889 : bool type_dependent_p;
9577 : :
9578 : : /* Add this declaration to the statement-tree. */
9579 : 162794889 : if (at_function_scope_p ())
9580 : 59223331 : add_decl_expr (decl);
9581 : :
9582 : 162794889 : type_dependent_p = dependent_type_p (type);
9583 : :
9584 : 162794889 : if (check_for_bare_parameter_packs (init))
9585 : : {
9586 : 3 : init = NULL_TREE;
9587 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9588 : : }
9589 : :
9590 : : /* Generally, initializers in templates are expanded when the
9591 : : template is instantiated. But, if DECL is a variable constant
9592 : : then it can be used in future constant expressions, so its value
9593 : : must be available. */
9594 : :
9595 : 162794889 : bool dep_init = false;
9596 : :
9597 : 162794889 : if (!VAR_P (decl) || type_dependent_p)
9598 : : /* We can't do anything if the decl has dependent type. */;
9599 : 19857959 : else if (init
9600 : 18288773 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9601 : 14478258 : && !TYPE_REF_P (type)
9602 : 14452265 : && decl_maybe_constant_var_p (decl)
9603 : 27416029 : && !(dep_init = value_dependent_init_p (init)))
9604 : : {
9605 : : /* This variable seems to be a non-dependent constant, so process
9606 : : its initializer. If check_initializer returns non-null the
9607 : : initialization wasn't constant after all. */
9608 : 1787449 : tree init_code;
9609 : 1787449 : cleanups = make_tree_vector ();
9610 : 1787449 : init_code = check_initializer (decl, init, flags, &cleanups);
9611 : 1787449 : if (init_code == NULL_TREE)
9612 : 1787449 : init = NULL_TREE;
9613 : 1787449 : release_tree_vector (cleanups);
9614 : : }
9615 : : else
9616 : : {
9617 : 18070510 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9618 : : /* Try to deduce array size. */
9619 : 18070510 : maybe_deduce_size_from_array_init (decl, init);
9620 : : /* And complain about multiple initializers. */
9621 : 16501324 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9622 : 18130229 : && !MAYBE_CLASS_TYPE_P (type))
9623 : 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9624 : : tf_warning_or_error);
9625 : : }
9626 : :
9627 : 162794889 : if (init)
9628 : 47303650 : DECL_INITIAL (decl) = init;
9629 : :
9630 : 162794889 : if (dep_init)
9631 : : {
9632 : 5770621 : retrofit_lang_decl (decl);
9633 : 5770621 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9634 : : }
9635 : :
9636 : 162794889 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9637 : : {
9638 : 3 : set_user_assembler_name (decl, asmspec);
9639 : 3 : DECL_HARD_REGISTER (decl) = 1;
9640 : : }
9641 : 162794889 : return;
9642 : : }
9643 : :
9644 : : /* Just store non-static data member initializers for later. */
9645 : 118182791 : if (init && TREE_CODE (decl) == FIELD_DECL)
9646 : 386899 : DECL_INITIAL (decl) = init;
9647 : :
9648 : : /* Take care of TYPE_DECLs up front. */
9649 : 118182791 : if (TREE_CODE (decl) == TYPE_DECL)
9650 : : {
9651 : 9667878 : if (type != error_mark_node
9652 : 9667878 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9653 : : {
9654 : 4419255 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9655 : 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9656 : 4419255 : set_identifier_type_value (DECL_NAME (decl), decl);
9657 : : }
9658 : :
9659 : : /* If we have installed this as the canonical typedef for this
9660 : : type, and that type has not been defined yet, delay emitting
9661 : : the debug information for it, as we will emit it later. */
9662 : 9667878 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9663 : 9667878 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9664 : 555596 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9665 : :
9666 : 9667878 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9667 : : at_eof);
9668 : 9667878 : return;
9669 : : }
9670 : :
9671 : : /* A reference will be modified here, as it is initialized. */
9672 : 108514913 : if (! DECL_EXTERNAL (decl)
9673 : 48474128 : && TREE_READONLY (decl)
9674 : 133173703 : && TYPE_REF_P (type))
9675 : : {
9676 : 232157 : was_readonly = 1;
9677 : 232157 : TREE_READONLY (decl) = 0;
9678 : : }
9679 : :
9680 : : /* This needs to happen before extend_ref_init_temps. */
9681 : 108514913 : if (VAR_OR_FUNCTION_DECL_P (decl))
9682 : : {
9683 : 99967230 : if (VAR_P (decl))
9684 : 46297282 : maybe_commonize_var (decl);
9685 : 99967230 : determine_visibility (decl);
9686 : : }
9687 : :
9688 : 108514913 : if (VAR_P (decl))
9689 : : {
9690 : 46297282 : duration_kind dk = decl_storage_duration (decl);
9691 : : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9692 : : only to a declaration of a variable with static or thread storage
9693 : : duration." */
9694 : 46297282 : if (DECL_DECLARED_CONSTINIT_P (decl)
9695 : 46297282 : && !(dk == dk_thread || dk == dk_static))
9696 : : {
9697 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
9698 : : "%<constinit%> can only be applied to a variable with "
9699 : : "static or thread storage duration");
9700 : 24 : return;
9701 : : }
9702 : :
9703 : 46297258 : if (decomp)
9704 : : {
9705 : 37380 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9706 : 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9707 : : "%<constinit%> can be applied to structured binding "
9708 : : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
9709 : 37380 : cp_maybe_mangle_decomp (decl, decomp);
9710 : 37380 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9711 : : {
9712 : 346 : if (CP_DECL_THREAD_LOCAL_P (decl))
9713 : 37 : aggregates1 = tls_aggregates;
9714 : : else
9715 : 309 : aggregates1 = static_aggregates;
9716 : : }
9717 : : }
9718 : :
9719 : : /* If this is a local variable that will need a mangled name,
9720 : : register it now. We must do this before processing the
9721 : : initializer for the variable, since the initialization might
9722 : : require a guard variable, and since the mangled name of the
9723 : : guard variable will depend on the mangled name of this
9724 : : variable. */
9725 : 92594516 : if (DECL_FUNCTION_SCOPE_P (decl)
9726 : 23383568 : && TREE_STATIC (decl)
9727 : 46439090 : && !DECL_ARTIFICIAL (decl))
9728 : : {
9729 : : /* The variable holding an anonymous union will have had its
9730 : : discriminator set in finish_anon_union, after which it's
9731 : : NAME will have been cleared. */
9732 : 140946 : if (DECL_NAME (decl))
9733 : 140903 : determine_local_discriminator (decl);
9734 : : /* Normally has_forced_label_in_static is set during GIMPLE
9735 : : lowering, but [cd]tors are never actually compiled directly.
9736 : : We need to set this early so we can deal with the label
9737 : : address extension. */
9738 : 140946 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
9739 : 140917 : || DECL_DESTRUCTOR_P (current_function_decl))
9740 : 140956 : && init)
9741 : : {
9742 : 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
9743 : 29 : add_local_decl (cfun, decl);
9744 : : }
9745 : : /* And make sure it's in the symbol table for
9746 : : c_parse_final_cleanups to find. */
9747 : 140946 : varpool_node::get_create (decl);
9748 : : }
9749 : :
9750 : 46297258 : if (flag_openmp
9751 : 135572 : && VAR_P (decl)
9752 : 135572 : && DECL_LANG_SPECIFIC (decl)
9753 : 60882 : && DECL_OMP_DECLARE_MAPPER_P (decl)
9754 : 46297262 : && init)
9755 : : {
9756 : 3 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9757 : 3 : DECL_INITIAL (decl) = init;
9758 : : }
9759 : : /* Convert the initializer to the type of DECL, if we have not
9760 : : already initialized DECL. */
9761 : 46297255 : else if (!DECL_INITIALIZED_P (decl)
9762 : : /* If !DECL_EXTERNAL then DECL is being defined. In the
9763 : : case of a static data member initialized inside the
9764 : : class-specifier, there can be an initializer even if DECL
9765 : : is *not* defined. */
9766 : 46297255 : && (!DECL_EXTERNAL (decl) || init))
9767 : : {
9768 : 40890030 : cleanups = make_tree_vector ();
9769 : 40890030 : init = check_initializer (decl, init, flags, &cleanups);
9770 : :
9771 : : /* Handle:
9772 : :
9773 : : [dcl.init]
9774 : :
9775 : : The memory occupied by any object of static storage
9776 : : duration is zero-initialized at program startup before
9777 : : any other initialization takes place.
9778 : :
9779 : : We cannot create an appropriate initializer until after
9780 : : the type of DECL is finalized. If DECL_INITIAL is set,
9781 : : then the DECL is statically initialized, and any
9782 : : necessary zero-initialization has already been performed. */
9783 : 40887333 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9784 : 417791 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9785 : : /*nelts=*/NULL_TREE,
9786 : : /*static_storage_p=*/true);
9787 : : /* Remember that the initialization for this variable has
9788 : : taken place. */
9789 : 40887333 : DECL_INITIALIZED_P (decl) = 1;
9790 : : /* This declaration is the definition of this variable,
9791 : : unless we are initializing a static data member within
9792 : : the class specifier. */
9793 : 40887333 : if (!DECL_EXTERNAL (decl))
9794 : 46294561 : var_definition_p = true;
9795 : : }
9796 : : /* If the variable has an array type, lay out the type, even if
9797 : : there is no initializer. It is valid to index through the
9798 : : array, and we must get TYPE_ALIGN set correctly on the array
9799 : : type. */
9800 : 5407225 : else if (TREE_CODE (type) == ARRAY_TYPE)
9801 : 219250 : layout_type (type);
9802 : :
9803 : 46294561 : if (TREE_STATIC (decl)
9804 : 22491772 : && !at_function_scope_p ()
9805 : 68644501 : && current_function_decl == NULL)
9806 : : /* So decl is a global variable or a static member of a
9807 : : non local class. Record the types it uses
9808 : : so that we can decide later to emit debug info for them. */
9809 : 22349928 : record_types_used_by_current_var_decl (decl);
9810 : : }
9811 : :
9812 : : /* Add this declaration to the statement-tree. This needs to happen
9813 : : after the call to check_initializer so that the DECL_EXPR for a
9814 : : reference temp is added before the DECL_EXPR for the reference itself. */
9815 : 108512192 : if (DECL_FUNCTION_SCOPE_P (decl))
9816 : : {
9817 : : /* If we're building a variable sized type, and we might be
9818 : : reachable other than via the top of the current binding
9819 : : level, then create a new BIND_EXPR so that we deallocate
9820 : : the object at the right time. */
9821 : 23383568 : if (VAR_P (decl)
9822 : 23383568 : && DECL_SIZE (decl)
9823 : 23334002 : && !TREE_CONSTANT (DECL_SIZE (decl))
9824 : 23384561 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9825 : : {
9826 : 3 : tree bind;
9827 : 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9828 : 3 : TREE_SIDE_EFFECTS (bind) = 1;
9829 : 3 : add_stmt (bind);
9830 : 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
9831 : : }
9832 : 23383568 : add_decl_expr (decl);
9833 : : }
9834 : :
9835 : 108512192 : tree decomp_init = NULL_TREE;
9836 : : /* Let the middle end know about variables and functions -- but not
9837 : : static data members in uninstantiated class templates. */
9838 : 108512192 : if (VAR_OR_FUNCTION_DECL_P (decl))
9839 : : {
9840 : 99964509 : if (VAR_P (decl))
9841 : : {
9842 : 46294561 : layout_var_decl (decl);
9843 : 46294561 : if (!flag_weak)
9844 : : /* Check again now that we have an initializer. */
9845 : 57 : maybe_commonize_var (decl);
9846 : : /* A class-scope constexpr variable with an out-of-class declaration.
9847 : : C++17 makes them implicitly inline, but still force it out. */
9848 : 62550628 : if (DECL_INLINE_VAR_P (decl)
9849 : 16026529 : && !DECL_VAR_DECLARED_INLINE_P (decl)
9850 : 9039680 : && !DECL_TEMPLATE_INSTANTIATION (decl)
9851 : 30168287 : && !in_class_decl)
9852 : 103 : mark_needed (decl);
9853 : : }
9854 : :
9855 : 99964509 : if (var_definition_p
9856 : : /* With -fmerge-all-constants, gimplify_init_constructor
9857 : : might add TREE_STATIC to aggregate variables. */
9858 : 39857849 : && (TREE_STATIC (decl)
9859 : 23241878 : || (flag_merge_constants >= 2
9860 : 38 : && AGGREGATE_TYPE_P (type))))
9861 : : {
9862 : : /* If a TREE_READONLY variable needs initialization
9863 : : at runtime, it is no longer readonly and we need to
9864 : : avoid MEM_READONLY_P being set on RTL created for it. */
9865 : 16615974 : if (init)
9866 : : {
9867 : 14180 : if (TREE_READONLY (decl))
9868 : 521 : TREE_READONLY (decl) = 0;
9869 : : was_readonly = 0;
9870 : : }
9871 : 16601794 : else if (was_readonly)
9872 : 1901 : TREE_READONLY (decl) = 1;
9873 : :
9874 : : /* Likewise if it needs destruction. */
9875 : 16615974 : if (!decl_maybe_constant_destruction (decl, type))
9876 : 3407 : TREE_READONLY (decl) = 0;
9877 : : }
9878 : 83348535 : else if (VAR_P (decl)
9879 : 29678587 : && CP_DECL_THREAD_LOCAL_P (decl)
9880 : 19965 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9881 : 19965 : && (was_readonly || TREE_READONLY (decl))
9882 : 83348550 : && var_needs_tls_wrapper (decl))
9883 : : {
9884 : : /* TLS variables need dynamic initialization by the TLS wrapper
9885 : : function, we don't want to hoist accesses to it before the
9886 : : wrapper. */
9887 : 6 : was_readonly = 0;
9888 : 6 : TREE_READONLY (decl) = 0;
9889 : : }
9890 : :
9891 : 99964509 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
9892 : :
9893 : : /* Check for abstractness of the type. */
9894 : 99964509 : if (var_definition_p)
9895 : 39857849 : abstract_virtuals_error (decl, type);
9896 : :
9897 : 99964509 : if (decomp && !cp_finish_decomp (decl, decomp, true))
9898 : 18254 : decomp = NULL;
9899 : :
9900 : 99964509 : if (TREE_TYPE (decl) == error_mark_node)
9901 : : /* No initialization required. */
9902 : : ;
9903 : 99964438 : else if (TREE_CODE (decl) == FUNCTION_DECL)
9904 : : {
9905 : 53669948 : if (init)
9906 : : {
9907 : 582 : if (init == ridpointers[(int)RID_DEFAULT])
9908 : : {
9909 : : /* An out-of-class default definition is defined at
9910 : : the point where it is explicitly defaulted. */
9911 : 570 : if (DECL_DELETED_FN (decl))
9912 : 6 : maybe_explain_implicit_delete (decl);
9913 : 564 : else if (DECL_INITIAL (decl) == error_mark_node)
9914 : 540 : synthesize_method (decl);
9915 : : }
9916 : : else
9917 : 24 : error_at (cp_expr_loc_or_loc (init,
9918 : 12 : DECL_SOURCE_LOCATION (decl)),
9919 : : "function %q#D is initialized like a variable",
9920 : : decl);
9921 : : }
9922 : : /* else no initialization required. */
9923 : : }
9924 : 46294490 : else if (DECL_EXTERNAL (decl)
9925 : 46294490 : && ! (DECL_LANG_SPECIFIC (decl)
9926 : 6131019 : && DECL_NOT_REALLY_EXTERN (decl)))
9927 : : {
9928 : : /* check_initializer will have done any constant initialization. */
9929 : : }
9930 : : /* A variable definition. */
9931 : 41690507 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9932 : : {
9933 : : /* Initialize the local variable. */
9934 : 23241718 : if (!decomp)
9935 : 23222831 : initialize_local_var (decl, init, false);
9936 : : else
9937 : : {
9938 : 18887 : tree cleanup = NULL_TREE;
9939 : 18887 : if (DECL_SIZE (decl))
9940 : 18887 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9941 : : /* If cp_finish_decomp needs to emit any code, we need to emit that
9942 : : code after code emitted by initialize_local_var in a single
9943 : : CLEANUP_POINT_EXPR, so that temporaries are destructed only
9944 : : after the cp_finish_decomp emitted code.
9945 : : If there are any cleanups, either extend_ref_init_temps
9946 : : created ones or e.g. array destruction, push those first
9947 : : with the cleanups guarded on a bool temporary, initially
9948 : : set to false and set to true after initialize_local_var
9949 : : emitted code. */
9950 : 18887 : tree guard = NULL_TREE;
9951 : 18887 : if (cleanups || cleanup)
9952 : : {
9953 : 18887 : guard = get_internal_target_expr (boolean_false_node);
9954 : 18887 : add_stmt (guard);
9955 : 18887 : guard = TARGET_EXPR_SLOT (guard);
9956 : : }
9957 : 18887 : tree sl = push_stmt_list ();
9958 : 18887 : initialize_local_var (decl, init, true);
9959 : 18887 : if (guard)
9960 : : {
9961 : 18887 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9962 : : guard, boolean_true_node));
9963 : 18905 : for (tree &t : *cleanups)
9964 : 18 : t = build3 (COND_EXPR, void_type_node,
9965 : : guard, t, void_node);
9966 : 18887 : if (cleanup)
9967 : 25 : cleanup = build3 (COND_EXPR, void_type_node,
9968 : : guard, cleanup, void_node);
9969 : : }
9970 : 18887 : unsigned before = stmt_list_stack->length ();
9971 : 18887 : cp_finish_decomp (decl, decomp);
9972 : 18887 : decomp = NULL;
9973 : 18887 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
9974 : 18887 : sl = pop_stmt_list (sl);
9975 : 18887 : if (n_extra_cleanups)
9976 : : {
9977 : : /* If cp_finish_decomp needs any cleanups, such as for
9978 : : extend_ref_init_temps created vars, pop_stmt_list
9979 : : popped that all, so push those extra cleanups around
9980 : : the whole sequence with a guard variable. */
9981 : 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
9982 : 15 : guard = get_internal_target_expr (integer_zero_node);
9983 : 15 : add_stmt (guard);
9984 : 15 : guard = TARGET_EXPR_SLOT (guard);
9985 : 63 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
9986 : : {
9987 : 48 : tree_stmt_iterator tsi = tsi_last (sl);
9988 : 48 : gcc_assert (!tsi_end_p (tsi));
9989 : 48 : tree last = tsi_stmt (tsi);
9990 : 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
9991 : : && !CLEANUP_EH_ONLY (last));
9992 : 48 : tree cst = build_int_cst (integer_type_node, i + 1);
9993 : 96 : tree cl = build3 (COND_EXPR, void_type_node,
9994 : : build2 (GE_EXPR, boolean_type_node,
9995 : : guard, cst),
9996 : 48 : CLEANUP_EXPR (last), void_node);
9997 : 48 : extra_cleanups.safe_push (cl);
9998 : 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
9999 : : integer_type_node,
10000 : : guard, cst),
10001 : : TSI_SAME_STMT);
10002 : 48 : tree sl2 = CLEANUP_BODY (last);
10003 : 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
10004 : 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
10005 : 48 : tsi_delink (&tsi);
10006 : : }
10007 : : }
10008 : 18887 : decomp_init = maybe_cleanup_point_expr_void (sl);
10009 : 18887 : if (cleanup)
10010 : 25 : finish_decl_cleanup (decl, cleanup);
10011 : : }
10012 : : }
10013 : :
10014 : : /* If a variable is defined, and then a subsequent
10015 : : definition with external linkage is encountered, we will
10016 : : get here twice for the same variable. We want to avoid
10017 : : calling expand_static_init more than once. For variables
10018 : : that are not static data members, we can call
10019 : : expand_static_init only when we actually process the
10020 : : initializer. It is not legal to redeclare a static data
10021 : : member, so this issue does not arise in that case. */
10022 : 18448789 : else if (var_definition_p && TREE_STATIC (decl))
10023 : : {
10024 : 16615928 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
10025 : : {
10026 : 98 : tree sl = push_stmt_list ();
10027 : 98 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
10028 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
10029 : 98 : expand_static_init (decl, init);
10030 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p
10031 : 98 : = saved_stmts_are_full_exprs_p;
10032 : 98 : cp_finish_decomp (decl, decomp);
10033 : 98 : decomp = NULL;
10034 : 98 : sl = pop_stmt_list (sl);
10035 : 98 : sl = maybe_cleanup_point_expr_void (sl);
10036 : 98 : add_stmt (sl);
10037 : : }
10038 : : else
10039 : 16615830 : expand_static_init (decl, init);
10040 : : }
10041 : : }
10042 : :
10043 : : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
10044 : : reference, insert it in the statement-tree now. */
10045 : 108512192 : if (cleanups)
10046 : : {
10047 : 40893454 : for (tree t : *cleanups)
10048 : : {
10049 : 6121 : push_cleanup (NULL_TREE, t, false);
10050 : : /* As in initialize_local_var. */
10051 : 6121 : wrap_temporary_cleanups (init, t);
10052 : : }
10053 : 40887333 : release_tree_vector (cleanups);
10054 : : }
10055 : :
10056 : 108512270 : for (tree t : &extra_cleanups)
10057 : 48 : push_cleanup (NULL_TREE, t, false);
10058 : :
10059 : 108512192 : if (decomp_init)
10060 : 18887 : add_stmt (decomp_init);
10061 : :
10062 : 108512192 : if (decomp
10063 : 141 : && var_definition_p
10064 : 141 : && TREE_STATIC (decl)
10065 : 108512333 : && !DECL_FUNCTION_SCOPE_P (decl))
10066 : : {
10067 : 141 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
10068 : 141 : ? tls_aggregates : static_aggregates);
10069 : 141 : tree aggregates2 = aggregates3;
10070 : 141 : if (aggregates2 != aggregates1)
10071 : : {
10072 : 66 : cp_finish_decomp (decl, decomp);
10073 : 66 : decomp = NULL;
10074 : 66 : if (aggregates3 != aggregates2)
10075 : : {
10076 : : /* If there are dynamic initializers for the structured
10077 : : binding base or associated extended ref temps and also
10078 : : dynamic initializers for the structured binding non-base
10079 : : vars, mark them. */
10080 : 315 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
10081 : 252 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
10082 : 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
10083 : 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
10084 : : }
10085 : : }
10086 : : }
10087 : :
10088 : 108512192 : if (was_readonly)
10089 : 231575 : TREE_READONLY (decl) = 1;
10090 : :
10091 : 108512192 : if (flag_openmp
10092 : 360233 : && VAR_P (decl)
10093 : 108647764 : && lookup_attribute ("omp declare target implicit",
10094 : 135572 : DECL_ATTRIBUTES (decl)))
10095 : : {
10096 : 52 : DECL_ATTRIBUTES (decl)
10097 : 52 : = remove_attribute ("omp declare target implicit",
10098 : 52 : DECL_ATTRIBUTES (decl));
10099 : 52 : complete_type (TREE_TYPE (decl));
10100 : 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
10101 : : {
10102 : 30 : auto_diagnostic_group d;
10103 : 30 : error ("%q+D in declare target directive does not have mappable"
10104 : : " type", decl);
10105 : 30 : if (TREE_TYPE (decl) != error_mark_node
10106 : 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
10107 : 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
10108 : 30 : }
10109 : 22 : else if (!lookup_attribute ("omp declare target",
10110 : 22 : DECL_ATTRIBUTES (decl))
10111 : 44 : && !lookup_attribute ("omp declare target link",
10112 : 22 : DECL_ATTRIBUTES (decl)))
10113 : : {
10114 : 22 : DECL_ATTRIBUTES (decl)
10115 : 22 : = tree_cons (get_identifier ("omp declare target"),
10116 : 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
10117 : 22 : symtab_node *node = symtab_node::get (decl);
10118 : 22 : if (node != NULL)
10119 : : {
10120 : 19 : node->offloadable = 1;
10121 : 19 : if (ENABLE_OFFLOADING)
10122 : : {
10123 : : g->have_offload = true;
10124 : : if (is_a <varpool_node *> (node))
10125 : : vec_safe_push (offload_vars, decl);
10126 : : }
10127 : : }
10128 : : }
10129 : : }
10130 : :
10131 : : /* This is the last point we can lower alignment so give the target the
10132 : : chance to do so. */
10133 : 108512192 : if (VAR_P (decl)
10134 : 46294561 : && !is_global_var (decl)
10135 : 131754070 : && !DECL_HARD_REGISTER (decl))
10136 : 23241828 : targetm.lower_local_decl_alignment (decl);
10137 : :
10138 : 108512192 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
10139 : 280977718 : }
10140 : :
10141 : : /* For class TYPE return itself or some its bases that contain
10142 : : any direct non-static data members. Return error_mark_node if an
10143 : : error has been diagnosed. */
10144 : :
10145 : : static tree
10146 : 22787 : find_decomp_class_base (location_t loc, tree type, tree ret,
10147 : : tsubst_flags_t complain)
10148 : : {
10149 : 45574 : if (LAMBDA_TYPE_P (type))
10150 : : {
10151 : 12 : if (complain & tf_error)
10152 : : {
10153 : 12 : auto_diagnostic_group d;
10154 : 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
10155 : 12 : inform (location_of (type), "lambda declared here");
10156 : 12 : }
10157 : 12 : return error_mark_node;
10158 : : }
10159 : :
10160 : 22775 : bool member_seen = false;
10161 : 322143 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10162 : 552590 : if (TREE_CODE (field) != FIELD_DECL
10163 : 46354 : || DECL_ARTIFICIAL (field)
10164 : 345667 : || DECL_UNNAMED_BIT_FIELD (field))
10165 : 253183 : continue;
10166 : 46224 : else if (ret)
10167 : : return type;
10168 : 46198 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10169 : : {
10170 : 8 : if ((complain & tf_error) == 0)
10171 : 0 : return error_mark_node;
10172 : 8 : auto_diagnostic_group d;
10173 : 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
10174 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10175 : : "anonymous struct member", type);
10176 : : else
10177 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
10178 : : "anonymous union member", type);
10179 : 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
10180 : 8 : return error_mark_node;
10181 : 8 : }
10182 : 46190 : else if (!accessible_p (type, field, true))
10183 : : {
10184 : 5 : if ((complain & tf_error) == 0)
10185 : 0 : return error_mark_node;
10186 : 5 : auto_diagnostic_group d;
10187 : 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
10188 : : field, type);
10189 : 5 : inform (DECL_SOURCE_LOCATION (field),
10190 : 5 : TREE_PRIVATE (field)
10191 : : ? G_("declared private here")
10192 : : : G_("declared protected here"));
10193 : 5 : return error_mark_node;
10194 : 5 : }
10195 : : else
10196 : : member_seen = true;
10197 : :
10198 : 22736 : tree base_binfo, binfo;
10199 : 22736 : tree orig_ret = ret;
10200 : 22736 : int i;
10201 : 22736 : if (member_seen)
10202 : 22647 : ret = type;
10203 : 22794 : for (binfo = TYPE_BINFO (type), i = 0;
10204 : 22794 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10205 : : {
10206 : 78 : auto_diagnostic_group d;
10207 : 78 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
10208 : : complain);
10209 : 78 : if (t == error_mark_node)
10210 : : {
10211 : 3 : if (complain & tf_error)
10212 : 3 : inform (location_of (type), "in base class of %qT", type);
10213 : 3 : return error_mark_node;
10214 : : }
10215 : 75 : if (t != NULL_TREE && t != ret)
10216 : : {
10217 : 48 : if (ret == type)
10218 : : {
10219 : 10 : if (complain & tf_error)
10220 : 10 : error_at (loc, "cannot decompose class type %qT: both it and "
10221 : : "its base class %qT have non-static data "
10222 : : "members", type, t);
10223 : 10 : return error_mark_node;
10224 : : }
10225 : 38 : else if (orig_ret != NULL_TREE)
10226 : : return t;
10227 : 38 : else if (ret != NULL_TREE)
10228 : : {
10229 : 7 : if (complain & tf_error)
10230 : 7 : error_at (loc, "cannot decompose class type %qT: its base "
10231 : : "classes %qT and %qT have non-static data "
10232 : : "members", type, ret, t);
10233 : 7 : return error_mark_node;
10234 : : }
10235 : : else
10236 : : ret = t;
10237 : : }
10238 : 78 : }
10239 : : return ret;
10240 : : }
10241 : :
10242 : : /* Return std::tuple_size<TYPE>::value. */
10243 : :
10244 : : static tree
10245 : 63809 : get_tuple_size (tree type)
10246 : : {
10247 : 63809 : tree args = make_tree_vec (1);
10248 : 63809 : TREE_VEC_ELT (args, 0) = type;
10249 : 63809 : tree inst = lookup_template_class (tuple_size_identifier, args,
10250 : : /*in_decl*/NULL_TREE,
10251 : : /*context*/std_node,
10252 : : tf_none);
10253 : 63809 : inst = complete_type (inst);
10254 : 63809 : if (inst == error_mark_node
10255 : 62390 : || !COMPLETE_TYPE_P (inst)
10256 : 104867 : || !CLASS_TYPE_P (type))
10257 : : return NULL_TREE;
10258 : 41058 : tree val = lookup_qualified_name (inst, value_identifier,
10259 : : LOOK_want::NORMAL, /*complain*/false);
10260 : 41058 : if (val == error_mark_node)
10261 : : return NULL_TREE;
10262 : 41052 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
10263 : 41052 : val = maybe_constant_value (val, NULL_TREE, mce_true);
10264 : 41052 : if (TREE_CODE (val) == INTEGER_CST)
10265 : : return val;
10266 : : else
10267 : 6 : return error_mark_node;
10268 : : }
10269 : :
10270 : : /* Return std::tuple_element<I,TYPE>::type. */
10271 : :
10272 : : static tree
10273 : 44579 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
10274 : : {
10275 : 44579 : tree args = make_tree_vec (2);
10276 : 44579 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
10277 : 44579 : TREE_VEC_ELT (args, 1) = type;
10278 : 44579 : tree inst = lookup_template_class (tuple_element_identifier, args,
10279 : : /*in_decl*/NULL_TREE,
10280 : : /*context*/std_node,
10281 : : tf_warning_or_error);
10282 : 44579 : return make_typename_type (inst, type_identifier,
10283 : 44579 : none_type, tf_warning_or_error);
10284 : : }
10285 : :
10286 : : /* Return e.get<i>() or get<i>(e). */
10287 : :
10288 : : static tree
10289 : 44588 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
10290 : : {
10291 : 44588 : tree targs = make_tree_vec (1);
10292 : 44588 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
10293 : :
10294 : 44588 : tree etype = TREE_TYPE (decl);
10295 : 44588 : tree e = convert_from_reference (decl);
10296 : :
10297 : : /* [The id-expression] e is an lvalue if the type of the entity e is an
10298 : : lvalue reference and an xvalue otherwise. */
10299 : 44588 : if (!TYPE_REF_P (etype)
10300 : 44588 : || TYPE_REF_IS_RVALUE (etype))
10301 : 43298 : e = move (e);
10302 : :
10303 : 44588 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
10304 : : LOOK_want::NORMAL, /*complain*/false);
10305 : 44588 : bool use_member_get = false;
10306 : :
10307 : : /* To use a member get, member lookup must find at least one
10308 : : declaration that is a function template
10309 : : whose first template parameter is a non-type parameter. */
10310 : 131327 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
10311 : : {
10312 : 44588 : tree fn = *iter;
10313 : 44588 : if (TREE_CODE (fn) == TEMPLATE_DECL)
10314 : : {
10315 : 2440 : tree tparms = DECL_TEMPLATE_PARMS (fn);
10316 : 2440 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
10317 : 2440 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
10318 : : {
10319 : : use_member_get = true;
10320 : : break;
10321 : : }
10322 : : }
10323 : : }
10324 : :
10325 : 44588 : if (use_member_get)
10326 : : {
10327 : 2437 : fns = lookup_template_function (fns, targs);
10328 : 2437 : return build_new_method_call (e, fns, /*args*/NULL,
10329 : : /*path*/NULL_TREE, LOOKUP_NORMAL,
10330 : 2437 : /*fn_p*/NULL, tf_warning_or_error);
10331 : : }
10332 : : else
10333 : : {
10334 : 42151 : releasing_vec args (make_tree_vector_single (e));
10335 : 42151 : fns = lookup_template_function (get__identifier, targs);
10336 : 42151 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
10337 : 42151 : return finish_call_expr (fns, &args, /*novirt*/false,
10338 : : /*koenig*/true, tf_warning_or_error);
10339 : 42151 : }
10340 : : }
10341 : :
10342 : : /* It's impossible to recover the decltype of a tuple decomposition variable
10343 : : based on the actual type of the variable, so store it in a hash table. */
10344 : :
10345 : : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
10346 : :
10347 : : tree
10348 : 546 : lookup_decomp_type (tree v)
10349 : : {
10350 : 546 : if (decomp_type_table)
10351 : 419 : if (tree *slot = decomp_type_table->get (v))
10352 : 355 : return *slot;
10353 : : return NULL_TREE;
10354 : : }
10355 : :
10356 : : /* Mangle a decomposition declaration if needed. Arguments like
10357 : : in cp_finish_decomp. */
10358 : :
10359 : : static void
10360 : 37380 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
10361 : : {
10362 : 37380 : if (!processing_template_decl
10363 : 37380 : && !error_operand_p (decl)
10364 : 74760 : && TREE_STATIC (decl))
10365 : : {
10366 : 605 : auto_vec<tree, 16> v;
10367 : 605 : v.safe_grow (decomp->count, true);
10368 : 605 : tree d = decomp->decl;
10369 : 2196 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
10370 : 1591 : v[decomp->count - i - 1] = d;
10371 : 605 : if (DECL_FUNCTION_SCOPE_P (decl))
10372 : : {
10373 : : size_t sz = 3;
10374 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
10375 : 650 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
10376 : 259 : char *name = XALLOCAVEC (char, sz);
10377 : 259 : name[0] = 'D';
10378 : 259 : name[1] = 'C';
10379 : 259 : char *p = name + 2;
10380 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
10381 : : {
10382 : 650 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
10383 : 650 : *p++ = ' ';
10384 : 650 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
10385 : 650 : p += len;
10386 : : }
10387 : 259 : *p = '\0';
10388 : 259 : determine_local_discriminator (decl, get_identifier (name));
10389 : : }
10390 : 605 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
10391 : 605 : maybe_apply_pragma_weak (decl);
10392 : 605 : }
10393 : 37380 : }
10394 : :
10395 : : /* Append #i to DECL_NAME (decl) or for name independent decls
10396 : : clear DECL_NAME (decl). */
10397 : :
10398 : : static void
10399 : 2021 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
10400 : : {
10401 : 2021 : if (name_independent_decl_p (decl))
10402 : : /* Only "_" names are treated as name independent, "_#0" etc. is not and
10403 : : because we pushdecl the individual decl elements of structured binding
10404 : : pack, we could get redeclaration errors if there are 2 or more name
10405 : : independent structured binding packs in the same scope. */
10406 : 312 : DECL_NAME (decl) = NULL_TREE;
10407 : : else
10408 : : {
10409 : 1709 : tree name = DECL_NAME (decl);
10410 : 1709 : size_t len = IDENTIFIER_LENGTH (name) + 22;
10411 : 1709 : char *n = XALLOCAVEC (char, len);
10412 : 1709 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
10413 : 1709 : IDENTIFIER_POINTER (name), i);
10414 : 1709 : DECL_NAME (decl) = get_identifier (n);
10415 : : }
10416 : 2021 : }
10417 : :
10418 : : /* Return structured binding size of TYPE or -1 if erroneous. */
10419 : :
10420 : : HOST_WIDE_INT
10421 : 457 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
10422 : : {
10423 : 457 : if (TYPE_REF_P (type))
10424 : : {
10425 : 0 : type = complete_type (TREE_TYPE (type));
10426 : 0 : if (type == error_mark_node)
10427 : : return -1;
10428 : 0 : if (!COMPLETE_TYPE_P (type))
10429 : : {
10430 : 0 : if (complain & tf_error)
10431 : 0 : error_at (loc, "structured binding refers to incomplete type %qT",
10432 : : type);
10433 : 0 : return -1;
10434 : : }
10435 : : }
10436 : :
10437 : 457 : unsigned HOST_WIDE_INT eltscnt = 0;
10438 : 457 : if (TREE_CODE (type) == ARRAY_TYPE)
10439 : : {
10440 : 255 : if (TYPE_DOMAIN (type) == NULL_TREE)
10441 : : {
10442 : 9 : if (complain & tf_error)
10443 : 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
10444 : : type);
10445 : 9 : return -1;
10446 : : }
10447 : 246 : tree nelts = array_type_nelts_top (type);
10448 : 246 : if (nelts == error_mark_node)
10449 : : return -1;
10450 : 246 : if (!tree_fits_shwi_p (nelts))
10451 : : {
10452 : 3 : if (complain & tf_error)
10453 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10454 : 3 : return -1;
10455 : : }
10456 : 243 : return tree_to_shwi (nelts);
10457 : : }
10458 : : /* 2 GNU extensions. */
10459 : 202 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10460 : : return 2;
10461 : 196 : else if (TREE_CODE (type) == VECTOR_TYPE)
10462 : : {
10463 : 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10464 : : {
10465 : : if (complain & tf_error)
10466 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10467 : : return -1;
10468 : : }
10469 : 12 : return eltscnt;
10470 : : }
10471 : 184 : else if (tree tsize = get_tuple_size (type))
10472 : : {
10473 : 28 : if (tsize == error_mark_node
10474 : 22 : || !tree_fits_shwi_p (tsize)
10475 : 50 : || tree_int_cst_sgn (tsize) < 0)
10476 : : {
10477 : 9 : if (complain & tf_error)
10478 : 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10479 : : "constant expression", type);
10480 : 9 : return -1;
10481 : : }
10482 : 19 : return tree_to_shwi (tsize);
10483 : : }
10484 : 156 : else if (TREE_CODE (type) == UNION_TYPE)
10485 : : {
10486 : 3 : if (complain & tf_error)
10487 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10488 : 3 : return -1;
10489 : : }
10490 : 153 : else if (!CLASS_TYPE_P (type))
10491 : : {
10492 : 21 : if (complain & tf_error)
10493 : 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10494 : 21 : return -1;
10495 : : }
10496 : 132 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10497 : : return -1;
10498 : 132 : else if (!COMPLETE_TYPE_P (type))
10499 : : {
10500 : 3 : if (complain & tf_error)
10501 : 3 : error_at (loc, "structured binding refers to incomplete class type "
10502 : : "%qT", type);
10503 : 3 : return -1;
10504 : : }
10505 : : else
10506 : : {
10507 : 129 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10508 : 129 : if (btype == error_mark_node)
10509 : : return -1;
10510 : 108 : else if (btype == NULL_TREE)
10511 : : return 0;
10512 : 1011 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10513 : 1533 : if (TREE_CODE (field) != FIELD_DECL
10514 : 327 : || DECL_ARTIFICIAL (field)
10515 : 1251 : || DECL_UNNAMED_BIT_FIELD (field))
10516 : 609 : continue;
10517 : : else
10518 : 315 : eltscnt++;
10519 : 87 : return eltscnt;
10520 : : }
10521 : : }
10522 : :
10523 : : /* Finish a decomposition declaration. DECL is the underlying declaration
10524 : : "e", FIRST is the head of a chain of decls for the individual identifiers
10525 : : chained through DECL_CHAIN in reverse order and COUNT is the number of
10526 : : those decls. If TEST_P is true, return true if any code would need to be
10527 : : actually emitted but don't emit it. Return false otherwise. */
10528 : :
10529 : : bool
10530 : 136928 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10531 : : {
10532 : 136928 : tree first = decomp->decl;
10533 : 136928 : unsigned count = decomp->count;
10534 : 136928 : if (error_operand_p (decl))
10535 : : {
10536 : 57 : error_out:
10537 : 583 : while (count--)
10538 : : {
10539 : 400 : TREE_TYPE (first) = error_mark_node;
10540 : 400 : if (DECL_HAS_VALUE_EXPR_P (first))
10541 : : {
10542 : 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10543 : 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10544 : : }
10545 : 400 : first = DECL_CHAIN (first);
10546 : : }
10547 : 183 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
10548 : 51 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10549 : 183 : return false;
10550 : : }
10551 : :
10552 : 136871 : location_t loc = DECL_SOURCE_LOCATION (decl);
10553 : 136871 : if (type_dependent_expression_p (decl)
10554 : : /* This happens for range for when not in templates.
10555 : : Still add the DECL_VALUE_EXPRs for later processing. */
10556 : 136871 : || (!processing_template_decl
10557 : 56781 : && type_uses_auto (TREE_TYPE (decl))))
10558 : : {
10559 : 217043 : for (unsigned int i = 0; i < count; i++)
10560 : : {
10561 : 144732 : if (!DECL_HAS_VALUE_EXPR_P (first))
10562 : : {
10563 : 144732 : tree v = build_nt (ARRAY_REF, decl,
10564 : 144732 : size_int (count - i - 1),
10565 : : NULL_TREE, NULL_TREE);
10566 : 144732 : SET_DECL_VALUE_EXPR (first, v);
10567 : 144732 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10568 : : }
10569 : 144732 : if (processing_template_decl)
10570 : 144112 : fit_decomposition_lang_decl (first, decl);
10571 : 144732 : first = DECL_CHAIN (first);
10572 : : }
10573 : : return false;
10574 : : }
10575 : :
10576 : 64560 : auto_vec<tree, 16> v;
10577 : 64560 : v.safe_grow (count, true);
10578 : 64560 : tree d = first;
10579 : 64560 : int pack = -1;
10580 : 195796 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10581 : : {
10582 : 131236 : v[count - i - 1] = d;
10583 : 131236 : fit_decomposition_lang_decl (d, decl);
10584 : 131236 : if (DECL_PACK_P (d))
10585 : 1005 : pack = count - i - 1;
10586 : : }
10587 : :
10588 : 64560 : tree type = TREE_TYPE (decl);
10589 : 64560 : tree dexp = decl;
10590 : :
10591 : 64560 : if (TYPE_REF_P (type))
10592 : : {
10593 : 1758 : dexp = convert_from_reference (dexp);
10594 : 1758 : type = complete_type (TREE_TYPE (type));
10595 : 1758 : if (type == error_mark_node)
10596 : 126 : goto error_out;
10597 : 1758 : if (!COMPLETE_TYPE_P (type))
10598 : : {
10599 : 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10600 : : type);
10601 : 3 : goto error_out;
10602 : : }
10603 : : }
10604 : :
10605 : 64557 : tree eltype = NULL_TREE;
10606 : 64557 : unsigned HOST_WIDE_INT eltscnt = 0;
10607 : : /* Structured binding packs when initializer is non-dependent should
10608 : : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10609 : : of that TREE_VEC are the base and index, what is normally represented
10610 : : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10611 : : of the pack first element. The remaining elements of the TREE_VEC
10612 : : are VAR_DECLs for the pack elements. */
10613 : 64557 : tree packv = NULL_TREE;
10614 : :
10615 : 64557 : if (TREE_CODE (type) == ARRAY_TYPE)
10616 : : {
10617 : 794 : tree nelts;
10618 : 794 : nelts = array_type_nelts_top (type);
10619 : 794 : if (nelts == error_mark_node)
10620 : 0 : goto error_out;
10621 : 794 : if (DECL_DECOMP_BASE (decl))
10622 : : {
10623 : 9 : error_at (loc, "array initializer for structured binding "
10624 : : "declaration in condition");
10625 : 9 : goto error_out;
10626 : : }
10627 : 785 : if (!tree_fits_uhwi_p (nelts))
10628 : : {
10629 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10630 : 3 : goto error_out;
10631 : : }
10632 : 782 : eltscnt = tree_to_uhwi (nelts);
10633 : 782 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10634 : : {
10635 : 11 : cnt_mismatch:
10636 : 38 : auto_diagnostic_group d;
10637 : 38 : if (count > eltscnt)
10638 : 27 : error_n (loc, count,
10639 : : "%u name provided for structured binding",
10640 : : "%u names provided for structured binding", count);
10641 : : else
10642 : 11 : error_n (loc, count,
10643 : : "only %u name provided for structured binding",
10644 : : "only %u names provided for structured binding", count);
10645 : 38 : inform_n (loc, eltscnt,
10646 : : "while %qT decomposes into %wu element",
10647 : : "while %qT decomposes into %wu elements",
10648 : : type, eltscnt);
10649 : 38 : goto error_out;
10650 : : }
10651 : 771 : eltype = TREE_TYPE (type);
10652 : 2775 : for (unsigned int i = 0; i < count; i++)
10653 : : {
10654 : 2004 : if ((unsigned) pack == i)
10655 : : {
10656 : 152 : packv = make_tree_vec (eltscnt - count + 3);
10657 : 400 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10658 : : {
10659 : 248 : tree t;
10660 : 248 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10661 : 248 : set_sb_pack_name (t, j);
10662 : 248 : maybe_push_decl (t);
10663 : 248 : TREE_TYPE (t) = eltype;
10664 : 248 : layout_decl (t, 0);
10665 : 248 : if (!processing_template_decl)
10666 : : {
10667 : 172 : tree a = unshare_expr (dexp);
10668 : 172 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10669 : : NULL_TREE, NULL_TREE);
10670 : 172 : SET_DECL_VALUE_EXPR (t, a);
10671 : 172 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10672 : : }
10673 : : }
10674 : 2004 : continue;
10675 : 152 : }
10676 : 1852 : TREE_TYPE (v[i]) = eltype;
10677 : 1852 : layout_decl (v[i], 0);
10678 : 1852 : if (processing_template_decl)
10679 : 132 : continue;
10680 : 1720 : tree t = unshare_expr (dexp);
10681 : 1720 : unsigned HOST_WIDE_INT j = i;
10682 : 1720 : if (pack != -1 && (unsigned) pack < i)
10683 : 64 : j = i + eltscnt - count;
10684 : 1720 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10685 : 1720 : SET_DECL_VALUE_EXPR (v[i], t);
10686 : 1720 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10687 : : }
10688 : : }
10689 : : /* 2 GNU extensions. */
10690 : 63763 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10691 : : {
10692 : 78 : eltscnt = 2;
10693 : 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10694 : 3 : goto cnt_mismatch;
10695 : 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10696 : 213 : for (unsigned int i = 0; i < count; i++)
10697 : : {
10698 : 138 : if ((unsigned) pack == i)
10699 : : {
10700 : 60 : packv = make_tree_vec (eltscnt - count + 3);
10701 : 132 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10702 : : {
10703 : 72 : tree t;
10704 : 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10705 : 72 : set_sb_pack_name (t, j);
10706 : 72 : maybe_push_decl (t);
10707 : 72 : TREE_TYPE (t) = eltype;
10708 : 72 : layout_decl (t, 0);
10709 : 72 : if (!processing_template_decl)
10710 : : {
10711 : 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
10712 : : unshare_expr (dexp));
10713 : 48 : SET_DECL_VALUE_EXPR (t, a);
10714 : 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10715 : : }
10716 : : }
10717 : 60 : continue;
10718 : 60 : }
10719 : 78 : TREE_TYPE (v[i]) = eltype;
10720 : 78 : layout_decl (v[i], 0);
10721 : 78 : if (processing_template_decl)
10722 : 16 : continue;
10723 : 62 : tree t = unshare_expr (dexp);
10724 : 62 : unsigned HOST_WIDE_INT j = i;
10725 : 62 : if (pack != -1 && (unsigned) pack < i)
10726 : 16 : j = i + eltscnt - count;
10727 : 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
10728 : 62 : SET_DECL_VALUE_EXPR (v[i], t);
10729 : 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10730 : : }
10731 : : }
10732 : 63685 : else if (TREE_CODE (type) == VECTOR_TYPE)
10733 : : {
10734 : 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10735 : : {
10736 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10737 : : goto error_out;
10738 : : }
10739 : 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10740 : 3 : goto cnt_mismatch;
10741 : 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10742 : 249 : for (unsigned int i = 0; i < count; i++)
10743 : : {
10744 : 192 : if ((unsigned) pack == i)
10745 : : {
10746 : 48 : packv = make_tree_vec (eltscnt - count + 3);
10747 : 708 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10748 : : {
10749 : 660 : tree t;
10750 : 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10751 : 660 : set_sb_pack_name (t, j);
10752 : 660 : maybe_push_decl (t);
10753 : 660 : TREE_TYPE (t) = eltype;
10754 : 660 : layout_decl (t, 0);
10755 : 660 : if (!processing_template_decl)
10756 : : {
10757 : 440 : tree a = unshare_expr (dexp);
10758 : 440 : location_t loc = DECL_SOURCE_LOCATION (t);
10759 : 440 : tree s = size_int (j + pack);
10760 : 440 : convert_vector_to_array_for_subscript (loc, &a, s);
10761 : 440 : a = build4 (ARRAY_REF, eltype, a, s,
10762 : : NULL_TREE, NULL_TREE);
10763 : 440 : SET_DECL_VALUE_EXPR (t, a);
10764 : 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10765 : : }
10766 : : }
10767 : 84 : continue;
10768 : 48 : }
10769 : 144 : TREE_TYPE (v[i]) = eltype;
10770 : 144 : layout_decl (v[i], 0);
10771 : 144 : if (processing_template_decl)
10772 : 36 : continue;
10773 : 108 : tree t = unshare_expr (dexp);
10774 : 108 : unsigned HOST_WIDE_INT j = i;
10775 : 108 : if (pack != -1 && (unsigned) pack < i)
10776 : 24 : j = i + eltscnt - count;
10777 : 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10778 : 108 : &t, size_int (j));
10779 : 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10780 : 108 : SET_DECL_VALUE_EXPR (v[i], t);
10781 : 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10782 : : }
10783 : : }
10784 : 63625 : else if (tree tsize = get_tuple_size (type))
10785 : : {
10786 : 41024 : if (tsize == error_mark_node)
10787 : : {
10788 : 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10789 : : "constant expression", type);
10790 : 0 : goto error_out;
10791 : : }
10792 : 41024 : if (!tree_fits_uhwi_p (tsize))
10793 : : {
10794 : 12 : auto_diagnostic_group d;
10795 : 12 : error_n (loc, count,
10796 : : "%u name provided for structured binding",
10797 : : "%u names provided for structured binding", count);
10798 : 12 : inform (loc, "while %qT decomposes into %E elements",
10799 : : type, tsize);
10800 : 12 : goto error_out;
10801 : 12 : }
10802 : 41012 : eltscnt = tree_to_uhwi (tsize);
10803 : 41012 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10804 : 6 : goto cnt_mismatch;
10805 : 41006 : if (test_p && eltscnt)
10806 : 64434 : return true;
10807 : 21880 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
10808 : : {
10809 : : /* For structured bindings used in conditions we need to evaluate
10810 : : the conversion of decl (aka e in the standard) to bool or
10811 : : integral/enumeral type (the latter for switch conditions)
10812 : : before the get methods. */
10813 : 86 : tree cond = convert_from_reference (decl);
10814 : 86 : if (integer_onep (DECL_DECOMP_BASE (decl)))
10815 : : /* switch condition. */
10816 : 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10817 : : cond, true);
10818 : : else
10819 : : /* if/while/for condition. */
10820 : 68 : cond = contextual_conv_bool (cond, tf_warning_or_error);
10821 : 86 : if (cond && !error_operand_p (cond))
10822 : : {
10823 : : /* Wrap that value into a TARGET_EXPR, emit it right
10824 : : away and save for later uses in the cp_parse_condition
10825 : : or its instantiation. */
10826 : 86 : cond = get_internal_target_expr (cond);
10827 : 86 : add_stmt (cond);
10828 : 86 : DECL_DECOMP_BASE (decl) = cond;
10829 : : }
10830 : : }
10831 : 21880 : int save_read = DECL_READ_P (decl);
10832 : 66167 : for (unsigned i = 0; i < count; ++i)
10833 : : {
10834 : 44308 : location_t sloc = input_location;
10835 : 44308 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10836 : :
10837 : 44308 : if ((unsigned) pack == i)
10838 : : {
10839 : 276 : packv = make_tree_vec (eltscnt - count + 3);
10840 : 832 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10841 : : {
10842 : 556 : tree t;
10843 : 556 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10844 : 556 : set_sb_pack_name (t, j);
10845 : 556 : input_location = dloc;
10846 : 556 : tree init = get_tuple_decomp_init (decl, j + pack);
10847 : 556 : tree eltype = (init == error_mark_node ? error_mark_node
10848 : 556 : : get_tuple_element_type (type, j + pack));
10849 : 556 : input_location = sloc;
10850 : :
10851 : 556 : if (VOID_TYPE_P (eltype))
10852 : : {
10853 : 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
10854 : : "%<void%>", j + pack, type);
10855 : 0 : eltype = error_mark_node;
10856 : : }
10857 : 556 : if (init == error_mark_node || eltype == error_mark_node)
10858 : : {
10859 : 0 : inform (dloc, "in initialization of structured binding "
10860 : 0 : "pack %qD", v[pack]);
10861 : 0 : goto error_out;
10862 : : }
10863 : 556 : maybe_push_decl (t);
10864 : : /* Save the decltype away before reference collapse. */
10865 : 556 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
10866 : 556 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10867 : 556 : TREE_TYPE (t) = eltype;
10868 : 556 : layout_decl (t, 0);
10869 : 556 : DECL_HAS_VALUE_EXPR_P (t) = 0;
10870 : 556 : if (!processing_template_decl)
10871 : : {
10872 : 456 : copy_linkage (t, decl);
10873 : 456 : tree name = DECL_NAME (t);
10874 : 456 : if (TREE_STATIC (decl))
10875 : 36 : DECL_NAME (t) = DECL_NAME (v[pack]);
10876 : 456 : cp_finish_decl (t, init, /*constexpr*/false,
10877 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10878 : 456 : if (TREE_STATIC (decl))
10879 : : {
10880 : 36 : DECL_ASSEMBLER_NAME (t);
10881 : 36 : DECL_NAME (t) = name;
10882 : : }
10883 : : }
10884 : : }
10885 : 276 : continue;
10886 : 276 : }
10887 : :
10888 : 44032 : unsigned HOST_WIDE_INT j = i;
10889 : 44032 : if (pack != -1 && (unsigned) pack < i)
10890 : 238 : j = i + eltscnt - count;
10891 : 44032 : input_location = dloc;
10892 : 44032 : tree init = get_tuple_decomp_init (decl, j);
10893 : 44032 : tree eltype = (init == error_mark_node ? error_mark_node
10894 : 44023 : : get_tuple_element_type (type, j));
10895 : 44032 : input_location = sloc;
10896 : :
10897 : 44032 : if (VOID_TYPE_P (eltype))
10898 : : {
10899 : 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
10900 : : j, type);
10901 : 3 : eltype = error_mark_node;
10902 : : }
10903 : 44032 : if (init == error_mark_node || eltype == error_mark_node)
10904 : : {
10905 : 21 : inform (dloc, "in initialization of structured binding "
10906 : 21 : "variable %qD", v[i]);
10907 : 21 : goto error_out;
10908 : : }
10909 : : /* Save the decltype away before reference collapse. */
10910 : 44011 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
10911 : 44011 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10912 : 44011 : TREE_TYPE (v[i]) = eltype;
10913 : 44011 : layout_decl (v[i], 0);
10914 : 44011 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
10915 : : {
10916 : : /* In this case the names are variables, not just proxies. */
10917 : 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10918 : 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10919 : : }
10920 : 44011 : if (!processing_template_decl)
10921 : : {
10922 : 38479 : copy_linkage (v[i], decl);
10923 : 38479 : cp_finish_decl (v[i], init, /*constexpr*/false,
10924 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10925 : : }
10926 : : }
10927 : : /* Ignore reads from the underlying decl performed during initialization
10928 : : of the individual variables. If those will be read, we'll mark
10929 : : the underlying decl as read at that point. */
10930 : 21859 : DECL_READ_P (decl) = save_read;
10931 : : }
10932 : 22601 : else if (TREE_CODE (type) == UNION_TYPE)
10933 : : {
10934 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10935 : 3 : goto error_out;
10936 : : }
10937 : 22598 : else if (!CLASS_TYPE_P (type))
10938 : : {
10939 : 16 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10940 : 16 : goto error_out;
10941 : : }
10942 : 22582 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10943 : 0 : goto error_out;
10944 : 22582 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10945 : 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10946 : : type);
10947 : : else
10948 : : {
10949 : 22580 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
10950 : : tf_warning_or_error);
10951 : 22580 : if (btype == error_mark_node)
10952 : 21 : goto error_out;
10953 : 22559 : else if (btype == NULL_TREE)
10954 : : {
10955 : 16 : if (pack == 0 && count == 1)
10956 : : {
10957 : 16 : eltscnt = 0;
10958 : 16 : packv = make_tree_vec (2);
10959 : 16 : goto done;
10960 : : }
10961 : 0 : error_at (loc, "cannot decompose class type %qT without non-static "
10962 : : "data members", type);
10963 : 0 : goto error_out;
10964 : : }
10965 : 319609 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10966 : 548282 : if (TREE_CODE (field) != FIELD_DECL
10967 : 45866 : || DECL_ARTIFICIAL (field)
10968 : 342928 : || DECL_UNNAMED_BIT_FIELD (field))
10969 : 251216 : continue;
10970 : : else
10971 : 45850 : eltscnt++;
10972 : 22543 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10973 : 15 : goto cnt_mismatch;
10974 : 22528 : tree t = dexp;
10975 : 22528 : if (type != btype)
10976 : : {
10977 : 18 : t = convert_to_base (t, btype, /*check_access*/true,
10978 : : /*nonnull*/false, tf_warning_or_error);
10979 : 18 : type = btype;
10980 : : }
10981 : 22528 : unsigned HOST_WIDE_INT j = 0;
10982 : 22528 : unsigned int i = 0;
10983 : 319398 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10984 : 547927 : if (TREE_CODE (field) != FIELD_DECL
10985 : 45829 : || DECL_ARTIFICIAL (field)
10986 : 342695 : || DECL_UNNAMED_BIT_FIELD (field))
10987 : 251057 : continue;
10988 : : else
10989 : : {
10990 : 45813 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
10991 : : NULL_TREE);
10992 : 45813 : if (REFERENCE_REF_P (tt))
10993 : 30 : tt = TREE_OPERAND (tt, 0);
10994 : 45813 : if (pack != -1 && j >= (unsigned) pack)
10995 : : {
10996 : 666 : if (j == (unsigned) pack)
10997 : : {
10998 : 220 : packv = make_tree_vec (eltscnt - count + 3);
10999 : 220 : i++;
11000 : : }
11001 : 666 : if (j < (unsigned) pack + eltscnt - (count - 1))
11002 : : {
11003 : 485 : tree t;
11004 : 485 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
11005 : 485 : set_sb_pack_name (t, j + 1 - i);
11006 : 485 : maybe_push_decl (t);
11007 : 485 : TREE_TYPE (t) = TREE_TYPE (tt);
11008 : 485 : layout_decl (t, 0);
11009 : 485 : if (!processing_template_decl)
11010 : : {
11011 : 364 : SET_DECL_VALUE_EXPR (t, tt);
11012 : 364 : DECL_HAS_VALUE_EXPR_P (t) = 1;
11013 : : }
11014 : : else
11015 : 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
11016 : 485 : j++;
11017 : 485 : continue;
11018 : 485 : }
11019 : : }
11020 : 45328 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
11021 : 45328 : layout_decl (v[i], 0);
11022 : 45328 : if (!processing_template_decl)
11023 : : {
11024 : 35047 : SET_DECL_VALUE_EXPR (v[i], tt);
11025 : 35047 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11026 : : }
11027 : 45328 : i++;
11028 : 45328 : j++;
11029 : : }
11030 : 22528 : if (pack != -1 && j == (unsigned) pack)
11031 : : {
11032 : 20 : gcc_checking_assert (eltscnt == count - 1);
11033 : 20 : packv = make_tree_vec (2);
11034 : : }
11035 : : }
11036 : 45308 : done:
11037 : 45308 : if (packv)
11038 : : {
11039 : 792 : gcc_checking_assert (pack != -1);
11040 : 792 : TREE_VEC_ELT (packv, 0) = decl;
11041 : 792 : TREE_VEC_ELT (packv, 1) = size_int (pack);
11042 : 792 : SET_DECL_VALUE_EXPR (v[pack], packv);
11043 : 792 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
11044 : 792 : DECL_IGNORED_P (v[pack]) = 1;
11045 : 792 : if (!processing_template_decl)
11046 : 2034 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
11047 : 1480 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
11048 : : }
11049 : 45308 : if (processing_template_decl)
11050 : : {
11051 : 24254 : for (unsigned int i = 0; i < count; i++)
11052 : 16237 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
11053 : : {
11054 : 15843 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
11055 : : NULL_TREE, NULL_TREE);
11056 : 15843 : SET_DECL_VALUE_EXPR (v[i], a);
11057 : 15843 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
11058 : : }
11059 : : }
11060 : : return false;
11061 : : }
11062 : :
11063 : : /* Returns a declaration for a VAR_DECL as if:
11064 : :
11065 : : extern "C" TYPE NAME;
11066 : :
11067 : : had been seen. Used to create compiler-generated global
11068 : : variables. */
11069 : :
11070 : : static tree
11071 : 2138 : declare_global_var (tree name, tree type)
11072 : : {
11073 : 2138 : auto cookie = push_abi_namespace (global_namespace);
11074 : 2138 : tree decl = build_decl (input_location, VAR_DECL, name, type);
11075 : 2138 : TREE_PUBLIC (decl) = 1;
11076 : 2138 : DECL_EXTERNAL (decl) = 1;
11077 : 2138 : DECL_ARTIFICIAL (decl) = 1;
11078 : 2138 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11079 : : /* If the user has explicitly declared this variable (perhaps
11080 : : because the code we are compiling is part of a low-level runtime
11081 : : library), then it is possible that our declaration will be merged
11082 : : with theirs by pushdecl. */
11083 : 2138 : decl = pushdecl (decl);
11084 : 2138 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
11085 : 2138 : pop_abi_namespace (cookie, global_namespace);
11086 : :
11087 : 2138 : return decl;
11088 : : }
11089 : :
11090 : : /* Returns the type for the argument to "atexit" corresponding to the function
11091 : : to be called when the program exits. */
11092 : :
11093 : : static tree
11094 : 24 : get_atexit_fn_ptr_type ()
11095 : : {
11096 : 24 : if (!atexit_fn_ptr_type_node)
11097 : : {
11098 : 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
11099 : 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
11100 : : }
11101 : :
11102 : 24 : return atexit_fn_ptr_type_node;
11103 : : }
11104 : :
11105 : : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
11106 : : or "__cxa_throw" corresponding to the destructor to be called when the
11107 : : program exits. */
11108 : :
11109 : : tree
11110 : 18382 : get_cxa_atexit_fn_ptr_type ()
11111 : : {
11112 : 18382 : if (!cleanup_type)
11113 : : {
11114 : 13774 : tree fntype = build_function_type_list (void_type_node,
11115 : : ptr_type_node, NULL_TREE);
11116 : 13774 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
11117 : 13774 : cleanup_type = build_pointer_type (fntype);
11118 : : }
11119 : :
11120 : 18382 : return cleanup_type;
11121 : : }
11122 : :
11123 : : /* Returns a pointer to the `atexit' function. Note that if
11124 : : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
11125 : : `__cxa_atexit' function specified in the IA64 C++ ABI. */
11126 : :
11127 : : static tree
11128 : 3404 : get_atexit_node (void)
11129 : : {
11130 : 3404 : tree atexit_fndecl;
11131 : 3404 : tree fn_type;
11132 : 3404 : tree fn_ptr_type;
11133 : 3404 : const char *name;
11134 : 3404 : bool use_aeabi_atexit;
11135 : 3404 : tree ctx = global_namespace;
11136 : :
11137 : 3404 : if (atexit_node)
11138 : : return atexit_node;
11139 : :
11140 : 2104 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
11141 : : {
11142 : : /* The declaration for `__cxa_atexit' is:
11143 : :
11144 : : int __cxa_atexit (void (*)(void *), void *, void *)
11145 : :
11146 : : We build up the argument types and then the function type
11147 : : itself. */
11148 : 2092 : tree argtype0, argtype1, argtype2;
11149 : :
11150 : 2092 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
11151 : : /* First, build the pointer-to-function type for the first
11152 : : argument. */
11153 : 2092 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
11154 : : /* Then, build the rest of the argument types. */
11155 : 2092 : argtype2 = ptr_type_node;
11156 : 2092 : if (use_aeabi_atexit)
11157 : : {
11158 : : argtype1 = fn_ptr_type;
11159 : : argtype0 = ptr_type_node;
11160 : : }
11161 : : else
11162 : : {
11163 : 2092 : argtype1 = ptr_type_node;
11164 : 2092 : argtype0 = fn_ptr_type;
11165 : : }
11166 : : /* And the final __cxa_atexit type. */
11167 : 2092 : fn_type = build_function_type_list (integer_type_node,
11168 : : argtype0, argtype1, argtype2,
11169 : : NULL_TREE);
11170 : : /* ... which needs noexcept. */
11171 : 2092 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11172 : 2092 : if (use_aeabi_atexit)
11173 : : {
11174 : 0 : name = "__aeabi_atexit";
11175 : 0 : push_to_top_level ();
11176 : 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
11177 : 0 : ctx = current_namespace;
11178 : 0 : while (n--)
11179 : 0 : pop_namespace ();
11180 : 0 : pop_from_top_level ();
11181 : : }
11182 : : else
11183 : : {
11184 : 2092 : name = "__cxa_atexit";
11185 : 2092 : ctx = abi_node;
11186 : : }
11187 : : }
11188 : : else
11189 : : {
11190 : : /* The declaration for `atexit' is:
11191 : :
11192 : : int atexit (void (*)());
11193 : :
11194 : : We build up the argument types and then the function type
11195 : : itself. */
11196 : 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
11197 : : /* Build the final atexit type. */
11198 : 12 : fn_type = build_function_type_list (integer_type_node,
11199 : : fn_ptr_type, NULL_TREE);
11200 : : /* ... which needs noexcept. */
11201 : 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
11202 : 12 : name = "atexit";
11203 : : }
11204 : :
11205 : : /* Now, build the function declaration. */
11206 : 2104 : push_lang_context (lang_name_c);
11207 : 2104 : auto cookie = push_abi_namespace (ctx);
11208 : 2104 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
11209 : 2104 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11210 : : /* Install as hidden builtin so we're (a) more relaxed about
11211 : : exception spec matching and (b) will not give a confusing location
11212 : : in diagnostic and (c) won't magically appear in user-visible name
11213 : : lookups. */
11214 : 2104 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11215 : 2104 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11216 : 2104 : pop_abi_namespace (cookie, ctx);
11217 : 2104 : mark_used (atexit_fndecl);
11218 : 2104 : pop_lang_context ();
11219 : 2104 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11220 : :
11221 : 2104 : return atexit_node;
11222 : : }
11223 : :
11224 : : /* Like get_atexit_node, but for thread-local cleanups. */
11225 : :
11226 : : static tree
11227 : 86 : get_thread_atexit_node (void)
11228 : : {
11229 : 86 : if (thread_atexit_node)
11230 : : return thread_atexit_node;
11231 : :
11232 : : /* The declaration for `__cxa_thread_atexit' is:
11233 : :
11234 : : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
11235 : 56 : tree fn_type = build_function_type_list (integer_type_node,
11236 : : get_cxa_atexit_fn_ptr_type (),
11237 : : ptr_type_node, ptr_type_node,
11238 : : NULL_TREE);
11239 : :
11240 : : /* Now, build the function declaration, as with __cxa_atexit. */
11241 : 56 : unsigned flags = push_abi_namespace ();
11242 : 56 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
11243 : : ECF_LEAF | ECF_NOTHROW);
11244 : 56 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
11245 : 56 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
11246 : 56 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
11247 : 56 : pop_abi_namespace (flags);
11248 : 56 : mark_used (atexit_fndecl);
11249 : 56 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
11250 : :
11251 : 56 : return thread_atexit_node;
11252 : : }
11253 : :
11254 : : /* Returns the __dso_handle VAR_DECL. */
11255 : :
11256 : : static tree
11257 : 3478 : get_dso_handle_node (void)
11258 : : {
11259 : 3478 : if (dso_handle_node)
11260 : : return dso_handle_node;
11261 : :
11262 : : /* Declare the variable. */
11263 : 2138 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
11264 : : ptr_type_node);
11265 : :
11266 : : #ifdef HAVE_GAS_HIDDEN
11267 : 2138 : if (dso_handle_node != error_mark_node)
11268 : : {
11269 : 2135 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
11270 : 2135 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
11271 : : }
11272 : : #endif
11273 : :
11274 : 2138 : return dso_handle_node;
11275 : : }
11276 : :
11277 : : /* Begin a new function with internal linkage whose job will be simply
11278 : : to destroy some particular DECL. OB_PARM is true if object pointer
11279 : : is passed to the cleanup function, otherwise no argument is passed. */
11280 : :
11281 : : static tree
11282 : 367 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
11283 : : {
11284 : 367 : push_to_top_level ();
11285 : :
11286 : : /* No need to mangle this. */
11287 : 367 : push_lang_context (lang_name_c);
11288 : :
11289 : : /* Build the name of the function. */
11290 : 367 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
11291 : 367 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11292 : 367 : dname = targetm.strip_name_encoding (dname);
11293 : 731 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
11294 : :
11295 : 367 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
11296 : : : get_atexit_fn_ptr_type ());
11297 : : /* Build the function declaration. */
11298 : 367 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
11299 : 367 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
11300 : : /* It's a function with internal linkage, generated by the
11301 : : compiler. */
11302 : 367 : TREE_PUBLIC (fndecl) = 0;
11303 : 367 : DECL_ARTIFICIAL (fndecl) = 1;
11304 : : /* Make the function `inline' so that it is only emitted if it is
11305 : : actually needed. It is unlikely that it will be inlined, since
11306 : : it is only called via a function pointer, but we avoid unnecessary
11307 : : emissions this way. */
11308 : 367 : DECL_DECLARED_INLINE_P (fndecl) = 1;
11309 : 367 : DECL_INTERFACE_KNOWN (fndecl) = 1;
11310 : 367 : if (ob_parm)
11311 : : {
11312 : : /* Build the parameter. */
11313 : 355 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
11314 : 355 : TREE_USED (parmdecl) = 1;
11315 : 355 : DECL_READ_P (parmdecl) = 1;
11316 : 355 : DECL_ARGUMENTS (fndecl) = parmdecl;
11317 : : }
11318 : :
11319 : 367 : fndecl = pushdecl (fndecl, /*hidden=*/true);
11320 : 367 : if (omp_target)
11321 : : {
11322 : 3 : DECL_ATTRIBUTES (fndecl)
11323 : 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
11324 : 3 : DECL_ATTRIBUTES (fndecl));
11325 : 3 : DECL_ATTRIBUTES (fndecl)
11326 : 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
11327 : 3 : DECL_ATTRIBUTES (fndecl));
11328 : : }
11329 : 367 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
11330 : :
11331 : 367 : pop_lang_context ();
11332 : :
11333 : 367 : return current_function_decl;
11334 : : }
11335 : :
11336 : : /* Finish the cleanup function begun by start_cleanup_fn. */
11337 : :
11338 : : static void
11339 : 367 : end_cleanup_fn (void)
11340 : : {
11341 : 367 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
11342 : :
11343 : 367 : pop_from_top_level ();
11344 : 367 : }
11345 : :
11346 : : /* Generate code to handle the destruction of DECL, an object with
11347 : : static storage duration. */
11348 : :
11349 : : tree
11350 : 14819 : register_dtor_fn (tree decl, bool omp_target)
11351 : : {
11352 : 14819 : tree cleanup;
11353 : 14819 : tree addr;
11354 : 14819 : tree compound_stmt;
11355 : 14819 : tree fcall;
11356 : 14819 : tree type;
11357 : 14819 : bool ob_parm, dso_parm, use_dtor;
11358 : 14819 : tree arg0, arg1, arg2;
11359 : 14819 : tree atex_node;
11360 : :
11361 : 14819 : type = TREE_TYPE (decl);
11362 : 14819 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
11363 : 11329 : return void_node;
11364 : :
11365 : 3490 : if (decl_maybe_constant_destruction (decl, type)
11366 : 3490 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
11367 : : {
11368 : 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11369 : 0 : return void_node;
11370 : : }
11371 : :
11372 : : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
11373 : : "__aeabi_atexit"), and DECL is a class object, we can just pass the
11374 : : destructor to "__cxa_atexit"; we don't have to build a temporary
11375 : : function to do the cleanup. */
11376 : 6980 : dso_parm = (flag_use_cxa_atexit
11377 : 3490 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
11378 : 3490 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
11379 : 3478 : use_dtor = ob_parm && CLASS_TYPE_P (type);
11380 : 3490 : if (use_dtor)
11381 : : {
11382 : 3123 : cleanup = get_class_binding (type, complete_dtor_identifier);
11383 : :
11384 : : /* Make sure it is accessible. */
11385 : 3123 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
11386 : : tf_warning_or_error);
11387 : : }
11388 : : else
11389 : : {
11390 : : /* Call build_cleanup before we enter the anonymous function so
11391 : : that any access checks will be done relative to the current
11392 : : scope, rather than the scope of the anonymous function. */
11393 : 367 : build_cleanup (decl);
11394 : :
11395 : : /* Now start the function. */
11396 : 367 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
11397 : :
11398 : : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
11399 : : to the original function, rather than the anonymous one. That
11400 : : will make the back end think that nested functions are in use,
11401 : : which causes confusion. */
11402 : 367 : push_deferring_access_checks (dk_no_check);
11403 : 367 : fcall = build_cleanup (decl);
11404 : 367 : pop_deferring_access_checks ();
11405 : :
11406 : : /* Create the body of the anonymous function. */
11407 : 367 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
11408 : 367 : finish_expr_stmt (fcall);
11409 : 367 : finish_compound_stmt (compound_stmt);
11410 : 367 : end_cleanup_fn ();
11411 : : }
11412 : :
11413 : : /* Call atexit with the cleanup function. */
11414 : 3490 : mark_used (cleanup);
11415 : 3490 : cleanup = build_address (cleanup);
11416 : :
11417 : 3490 : if (CP_DECL_THREAD_LOCAL_P (decl))
11418 : 86 : atex_node = get_thread_atexit_node ();
11419 : : else
11420 : 3404 : atex_node = get_atexit_node ();
11421 : :
11422 : 3490 : if (use_dtor)
11423 : : {
11424 : : /* We must convert CLEANUP to the type that "__cxa_atexit"
11425 : : expects. */
11426 : 3123 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
11427 : : /* "__cxa_atexit" will pass the address of DECL to the
11428 : : cleanup function. */
11429 : 3123 : mark_used (decl);
11430 : 3123 : addr = build_address (decl);
11431 : : /* The declared type of the parameter to "__cxa_atexit" is
11432 : : "void *". For plain "T*", we could just let the
11433 : : machinery in cp_build_function_call convert it -- but if the
11434 : : type is "cv-qualified T *", then we need to convert it
11435 : : before passing it in, to avoid spurious errors. */
11436 : 3123 : addr = build_nop (ptr_type_node, addr);
11437 : : }
11438 : : else
11439 : : /* Since the cleanup functions we build ignore the address
11440 : : they're given, there's no reason to pass the actual address
11441 : : in, and, in general, it's cheaper to pass NULL than any
11442 : : other value. */
11443 : 367 : addr = null_pointer_node;
11444 : :
11445 : 3490 : if (dso_parm)
11446 : 3478 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
11447 : : tf_warning_or_error);
11448 : 12 : else if (ob_parm)
11449 : : /* Just pass NULL to the dso handle parm if we don't actually
11450 : : have a DSO handle on this target. */
11451 : 0 : arg2 = null_pointer_node;
11452 : : else
11453 : : arg2 = NULL_TREE;
11454 : :
11455 : 3478 : if (ob_parm)
11456 : : {
11457 : 3478 : if (!CP_DECL_THREAD_LOCAL_P (decl)
11458 : 3478 : && targetm.cxx.use_aeabi_atexit ())
11459 : : {
11460 : : arg1 = cleanup;
11461 : : arg0 = addr;
11462 : : }
11463 : : else
11464 : : {
11465 : : arg1 = addr;
11466 : : arg0 = cleanup;
11467 : : }
11468 : : }
11469 : : else
11470 : : {
11471 : : arg0 = cleanup;
11472 : : arg1 = NULL_TREE;
11473 : : }
11474 : 3490 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
11475 : 3490 : arg0, arg1, arg2, NULL_TREE);
11476 : : }
11477 : :
11478 : : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
11479 : : is its initializer. Generate code to handle the construction
11480 : : and destruction of DECL. */
11481 : :
11482 : : static void
11483 : 16615928 : expand_static_init (tree decl, tree init)
11484 : : {
11485 : 16615928 : gcc_assert (VAR_P (decl));
11486 : 16615928 : gcc_assert (TREE_STATIC (decl));
11487 : :
11488 : : /* Some variables require no dynamic initialization. */
11489 : 16615928 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
11490 : : {
11491 : : /* Make sure the destructor is callable. */
11492 : 16612524 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11493 : 16612524 : if (!init)
11494 : : return;
11495 : : }
11496 : :
11497 : 15599 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11498 : 14679 : && !DECL_FUNCTION_SCOPE_P (decl))
11499 : : {
11500 : 15 : auto_diagnostic_group d;
11501 : 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11502 : 15 : if (init)
11503 : 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11504 : : "needs dynamic initialization", decl);
11505 : : else
11506 : 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11507 : : "has a non-trivial destructor", decl);
11508 : 15 : static bool informed;
11509 : 15 : if (!informed)
11510 : : {
11511 : 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11512 : : "initialization and destruction");
11513 : 6 : informed = true;
11514 : : }
11515 : 15 : return;
11516 : 15 : }
11517 : :
11518 : 14635 : if (DECL_FUNCTION_SCOPE_P (decl))
11519 : : {
11520 : : /* Emit code to perform this initialization but once. */
11521 : 3762 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11522 : 3762 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11523 : 3762 : tree guard, guard_addr;
11524 : 3762 : tree flag, begin;
11525 : : /* We don't need thread-safety code for thread-local vars. */
11526 : 3762 : bool thread_guard = (flag_threadsafe_statics
11527 : 3762 : && !CP_DECL_THREAD_LOCAL_P (decl));
11528 : :
11529 : : /* Emit code to perform this initialization but once. This code
11530 : : looks like:
11531 : :
11532 : : static <type> guard;
11533 : : if (!__atomic_load (guard.first_byte)) {
11534 : : if (__cxa_guard_acquire (&guard)) {
11535 : : bool flag = false;
11536 : : try {
11537 : : // Do initialization.
11538 : : flag = true; __cxa_guard_release (&guard);
11539 : : // Register variable for destruction at end of program.
11540 : : } catch {
11541 : : if (!flag) __cxa_guard_abort (&guard);
11542 : : }
11543 : : }
11544 : : }
11545 : :
11546 : : Note that the `flag' variable is only set to 1 *after* the
11547 : : initialization is complete. This ensures that an exception,
11548 : : thrown during the construction, will cause the variable to
11549 : : reinitialized when we pass through this code again, as per:
11550 : :
11551 : : [stmt.dcl]
11552 : :
11553 : : If the initialization exits by throwing an exception, the
11554 : : initialization is not complete, so it will be tried again
11555 : : the next time control enters the declaration.
11556 : :
11557 : : This process should be thread-safe, too; multiple threads
11558 : : should not be able to initialize the variable more than
11559 : : once. */
11560 : :
11561 : : /* Create the guard variable. */
11562 : 3762 : guard = get_guard (decl);
11563 : :
11564 : : /* Begin the conditional initialization. */
11565 : 3762 : if_stmt = begin_if_stmt ();
11566 : :
11567 : 3762 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11568 : 3762 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11569 : :
11570 : 3762 : if (thread_guard)
11571 : : {
11572 : 3620 : tree vfntype = NULL_TREE;
11573 : 3620 : tree acquire_name, release_name, abort_name;
11574 : 3620 : tree acquire_fn, release_fn, abort_fn;
11575 : 3620 : guard_addr = build_address (guard);
11576 : :
11577 : 3620 : acquire_name = get_identifier ("__cxa_guard_acquire");
11578 : 3620 : release_name = get_identifier ("__cxa_guard_release");
11579 : 3620 : abort_name = get_identifier ("__cxa_guard_abort");
11580 : 3620 : acquire_fn = get_global_binding (acquire_name);
11581 : 3620 : release_fn = get_global_binding (release_name);
11582 : 3620 : abort_fn = get_global_binding (abort_name);
11583 : 3620 : if (!acquire_fn)
11584 : 2004 : acquire_fn = push_library_fn
11585 : 2004 : (acquire_name, build_function_type_list (integer_type_node,
11586 : 2004 : TREE_TYPE (guard_addr),
11587 : : NULL_TREE),
11588 : : NULL_TREE, ECF_NOTHROW);
11589 : 3620 : if (!release_fn || !abort_fn)
11590 : 2007 : vfntype = build_function_type_list (void_type_node,
11591 : 2007 : TREE_TYPE (guard_addr),
11592 : : NULL_TREE);
11593 : 2007 : if (!release_fn)
11594 : 2007 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11595 : : ECF_NOTHROW);
11596 : 3620 : if (!abort_fn)
11597 : 2007 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11598 : : ECF_NOTHROW | ECF_LEAF);
11599 : :
11600 : 3620 : inner_if_stmt = begin_if_stmt ();
11601 : 3620 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11602 : : inner_if_stmt);
11603 : :
11604 : 3620 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11605 : 3620 : begin = get_internal_target_expr (boolean_false_node);
11606 : 3620 : flag = TARGET_EXPR_SLOT (begin);
11607 : :
11608 : 3620 : TARGET_EXPR_CLEANUP (begin)
11609 : 3620 : = build3 (COND_EXPR, void_type_node, flag,
11610 : : void_node,
11611 : : build_call_n (abort_fn, 1, guard_addr));
11612 : 3620 : CLEANUP_EH_ONLY (begin) = 1;
11613 : :
11614 : : /* Do the initialization itself. */
11615 : 3620 : init = add_stmt_to_compound (begin, init);
11616 : 3620 : init = add_stmt_to_compound (init,
11617 : : build2 (MODIFY_EXPR, void_type_node,
11618 : : flag, boolean_true_node));
11619 : :
11620 : : /* Use atexit to register a function for destroying this static
11621 : : variable. Do this before calling __cxa_guard_release. */
11622 : 3620 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11623 : :
11624 : 3620 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11625 : : guard_addr));
11626 : : }
11627 : : else
11628 : : {
11629 : 142 : init = add_stmt_to_compound (init, set_guard (guard));
11630 : :
11631 : : /* Use atexit to register a function for destroying this static
11632 : : variable. */
11633 : 142 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11634 : : }
11635 : :
11636 : 3762 : finish_expr_stmt (init);
11637 : :
11638 : 3762 : if (thread_guard)
11639 : : {
11640 : 3620 : finish_compound_stmt (inner_then_clause);
11641 : 3620 : finish_then_clause (inner_if_stmt);
11642 : 3620 : finish_if_stmt (inner_if_stmt);
11643 : : }
11644 : :
11645 : 3762 : finish_compound_stmt (then_clause);
11646 : 3762 : finish_then_clause (if_stmt);
11647 : 3762 : finish_if_stmt (if_stmt);
11648 : : }
11649 : 10873 : else if (CP_DECL_THREAD_LOCAL_P (decl))
11650 : 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
11651 : : else
11652 : 10078 : static_aggregates = tree_cons (init, decl, static_aggregates);
11653 : : }
11654 : :
11655 : :
11656 : : /* Make TYPE a complete type based on INITIAL_VALUE.
11657 : : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11658 : : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11659 : : 3 if the initializer list is empty (in pedantic mode). */
11660 : :
11661 : : int
11662 : 526086 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11663 : : {
11664 : 526086 : int failure;
11665 : 526086 : tree type, elt_type;
11666 : :
11667 : : /* Don't get confused by a CONSTRUCTOR for some other type. */
11668 : 526056 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11669 : 94857 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11670 : 526411 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11671 : : return 1;
11672 : :
11673 : 526083 : if (initial_value)
11674 : : {
11675 : : /* An array of character type can be initialized from a
11676 : : brace-enclosed string constant so call reshape_init to
11677 : : remove the optional braces from a braced string literal. */
11678 : 526053 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
11679 : 526053 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
11680 : 28976 : initial_value = reshape_init (*ptype, initial_value,
11681 : : tf_warning_or_error);
11682 : :
11683 : : /* If any of the elements are parameter packs, we can't actually
11684 : : complete this type now because the array size is dependent. */
11685 : 526053 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
11686 : 16375529 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
11687 : 16091332 : if (PACK_EXPANSION_P (e.value))
11688 : : return 0;
11689 : : }
11690 : :
11691 : 526083 : failure = complete_array_type (ptype, initial_value, do_default);
11692 : :
11693 : : /* We can create the array before the element type is complete, which
11694 : : means that we didn't have these two bits set in the original type
11695 : : either. In completing the type, we are expected to propagate these
11696 : : bits. See also complete_type which does the same thing for arrays
11697 : : of fixed size. */
11698 : 526083 : type = *ptype;
11699 : 1052163 : if (type != error_mark_node && TYPE_DOMAIN (type))
11700 : : {
11701 : 526056 : elt_type = TREE_TYPE (type);
11702 : 526056 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
11703 : 1052112 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11704 : 526056 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
11705 : : }
11706 : :
11707 : : return failure;
11708 : : }
11709 : :
11710 : : /* As above, but either give an error or reject zero-size arrays, depending
11711 : : on COMPLAIN. */
11712 : :
11713 : : int
11714 : 1409 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
11715 : : bool do_default, tsubst_flags_t complain)
11716 : : {
11717 : 1409 : int failure;
11718 : 1409 : bool sfinae = !(complain & tf_error);
11719 : : /* In SFINAE context we can't be lenient about zero-size arrays. */
11720 : 1409 : if (sfinae)
11721 : 3 : ++pedantic;
11722 : 1409 : failure = cp_complete_array_type (ptype, initial_value, do_default);
11723 : 1409 : if (sfinae)
11724 : 3 : --pedantic;
11725 : 1409 : if (failure)
11726 : : {
11727 : 3 : if (sfinae)
11728 : : /* Not an error. */;
11729 : 0 : else if (failure == 1)
11730 : 0 : error ("initializer fails to determine size of %qT", *ptype);
11731 : 0 : else if (failure == 2)
11732 : : {
11733 : 0 : if (do_default)
11734 : 0 : error ("array size missing in %qT", *ptype);
11735 : : }
11736 : 0 : else if (failure == 3)
11737 : 0 : error ("zero-size array %qT", *ptype);
11738 : 3 : *ptype = error_mark_node;
11739 : : }
11740 : 1409 : return failure;
11741 : : }
11742 : :
11743 : : /* Return zero if something is declared to be a member of type
11744 : : CTYPE when in the context of CUR_TYPE. STRING is the error
11745 : : message to print in that case. Otherwise, quietly return 1. */
11746 : :
11747 : : static int
11748 : 24422167 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
11749 : : {
11750 : 24422167 : if (ctype && ctype != cur_type)
11751 : : {
11752 : 0 : if (flags == DTOR_FLAG)
11753 : 0 : error ("destructor for alien class %qT cannot be a member", ctype);
11754 : : else
11755 : 0 : error ("constructor for alien class %qT cannot be a member", ctype);
11756 : 0 : return 0;
11757 : : }
11758 : : return 1;
11759 : : }
11760 : :
11761 : : /* Subroutine of `grokdeclarator'. */
11762 : :
11763 : : /* Generate errors possibly applicable for a given set of specifiers.
11764 : : This is for ARM $7.1.2. */
11765 : :
11766 : : static void
11767 : 405461356 : bad_specifiers (tree object,
11768 : : enum bad_spec_place type,
11769 : : int virtualp,
11770 : : int quals,
11771 : : int inlinep,
11772 : : int friendp,
11773 : : int raises,
11774 : : const location_t* locations)
11775 : : {
11776 : 405461356 : switch (type)
11777 : : {
11778 : 56872624 : case BSP_VAR:
11779 : 56872624 : if (virtualp)
11780 : 0 : error_at (locations[ds_virtual],
11781 : : "%qD declared as a %<virtual%> variable", object);
11782 : 56872624 : if (quals)
11783 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11784 : : "%qD invalid in variable declaration", object);
11785 : : break;
11786 : 276953989 : case BSP_PARM:
11787 : 276953989 : if (virtualp)
11788 : 0 : error_at (locations[ds_virtual],
11789 : : "%qD declared as a %<virtual%> parameter", object);
11790 : 276953989 : if (inlinep)
11791 : 6 : error_at (locations[ds_inline],
11792 : : "%qD declared as an %<inline%> parameter", object);
11793 : 276953989 : if (quals)
11794 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11795 : : "%qD invalid in parameter declaration", object);
11796 : : break;
11797 : 43993280 : case BSP_TYPE:
11798 : 43993280 : if (virtualp)
11799 : 0 : error_at (locations[ds_virtual],
11800 : : "%qD declared as a %<virtual%> type", object);
11801 : 43993280 : if (inlinep)
11802 : 6 : error_at (locations[ds_inline],
11803 : : "%qD declared as an %<inline%> type", object);
11804 : 43993280 : if (quals)
11805 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11806 : : "%qD invalid in type declaration", object);
11807 : : break;
11808 : 27641463 : case BSP_FIELD:
11809 : 27641463 : if (virtualp)
11810 : 6 : error_at (locations[ds_virtual],
11811 : : "%qD declared as a %<virtual%> field", object);
11812 : 27641463 : if (inlinep)
11813 : 6 : error_at (locations[ds_inline],
11814 : : "%qD declared as an %<inline%> field", object);
11815 : 27641463 : if (quals)
11816 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11817 : : "%qD invalid in field declaration", object);
11818 : : break;
11819 : 0 : default:
11820 : 0 : gcc_unreachable();
11821 : : }
11822 : 405461356 : if (friendp)
11823 : 0 : error ("%q+D declared as a friend", object);
11824 : 405461356 : if (raises
11825 : 109 : && !flag_noexcept_type
11826 : 405461377 : && (TREE_CODE (object) == TYPE_DECL
11827 : 15 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
11828 : 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
11829 : 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
11830 : 6 : error ("%q+D declared with an exception specification", object);
11831 : 405461356 : }
11832 : :
11833 : : /* DECL is a member function or static data member and is presently
11834 : : being defined. Check that the definition is taking place in a
11835 : : valid namespace. */
11836 : :
11837 : : static void
11838 : 84843652 : check_class_member_definition_namespace (tree decl)
11839 : : {
11840 : : /* These checks only apply to member functions and static data
11841 : : members. */
11842 : 84843652 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
11843 : : /* We check for problems with specializations in pt.cc in
11844 : : check_specialization_namespace, where we can issue better
11845 : : diagnostics. */
11846 : 84843652 : if (processing_specialization)
11847 : : return;
11848 : : /* We check this in check_explicit_instantiation_namespace. */
11849 : 84696104 : if (processing_explicit_instantiation)
11850 : : return;
11851 : : /* [class.mfct]
11852 : :
11853 : : A member function definition that appears outside of the
11854 : : class definition shall appear in a namespace scope enclosing
11855 : : the class definition.
11856 : :
11857 : : [class.static.data]
11858 : :
11859 : : The definition for a static data member shall appear in a
11860 : : namespace scope enclosing the member's class definition. */
11861 : 84696026 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
11862 : 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
11863 : 18 : decl, DECL_CONTEXT (decl));
11864 : : }
11865 : :
11866 : : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
11867 : : METHOD_TYPE for a non-static member function; QUALS are the
11868 : : cv-qualifiers that apply to the function. */
11869 : :
11870 : : tree
11871 : 206462063 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
11872 : : {
11873 : 206462063 : tree this_type;
11874 : 206462063 : tree qual_type;
11875 : 206462063 : tree parm;
11876 : 206462063 : cp_cv_quals this_quals;
11877 : :
11878 : 206462063 : if (CLASS_TYPE_P (type))
11879 : : {
11880 : 92653050 : this_type
11881 : 92653050 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
11882 : 92653050 : this_type = build_pointer_type (this_type);
11883 : : }
11884 : : else
11885 : 113809013 : this_type = type_of_this_parm (type);
11886 : : /* The `this' parameter is implicitly `const'; it cannot be
11887 : : assigned to. */
11888 : 206462063 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
11889 : 206462063 : qual_type = cp_build_qualified_type (this_type, this_quals);
11890 : 206462063 : parm = build_artificial_parm (fn, this_identifier, qual_type);
11891 : 206462063 : cp_apply_type_quals_to_decl (this_quals, parm);
11892 : 206462063 : return parm;
11893 : : }
11894 : :
11895 : : /* DECL is a static member function. Complain if it was declared
11896 : : with function-cv-quals. */
11897 : :
11898 : : static void
11899 : 8815931 : check_static_quals (tree decl, cp_cv_quals quals)
11900 : : {
11901 : 0 : if (quals != TYPE_UNQUALIFIED)
11902 : 6 : error ("static member function %q#D declared with type qualifiers",
11903 : : decl);
11904 : 0 : }
11905 : :
11906 : : /* Helper function. Replace the temporary this parameter injected
11907 : : during cp_finish_omp_declare_simd with the real this parameter. */
11908 : :
11909 : : static tree
11910 : 2500 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11911 : : {
11912 : 2500 : tree this_parm = (tree) data;
11913 : 2500 : if (TREE_CODE (*tp) == PARM_DECL
11914 : 450 : && DECL_NAME (*tp) == this_identifier
11915 : 2773 : && *tp != this_parm)
11916 : 273 : *tp = this_parm;
11917 : 2227 : else if (TYPE_P (*tp))
11918 : 33 : *walk_subtrees = 0;
11919 : 2500 : return NULL_TREE;
11920 : : }
11921 : :
11922 : : /* CTYPE is class type, or null if non-class.
11923 : : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11924 : : or METHOD_TYPE.
11925 : : DECLARATOR is the function's name.
11926 : : PARMS is a chain of PARM_DECLs for the function.
11927 : : VIRTUALP is truthvalue of whether the function is virtual or not.
11928 : : FLAGS are to be passed through to `grokclassfn'.
11929 : : QUALS are qualifiers indicating whether the function is `const'
11930 : : or `volatile'.
11931 : : RAISES is a list of exceptions that this function can raise.
11932 : : CHECK is 1 if we must find this method in CTYPE, 0 if we should
11933 : : not look, and -1 if we should not call `grokclassfn' at all.
11934 : :
11935 : : SFK is the kind of special function (if any) for the new function.
11936 : :
11937 : : Returns `NULL_TREE' if something goes wrong, after issuing
11938 : : applicable error messages. */
11939 : :
11940 : : static tree
11941 : 173946295 : grokfndecl (tree ctype,
11942 : : tree type,
11943 : : tree declarator,
11944 : : tree parms,
11945 : : tree orig_declarator,
11946 : : const cp_decl_specifier_seq *declspecs,
11947 : : tree decl_reqs,
11948 : : int virtualp,
11949 : : enum overload_flags flags,
11950 : : cp_cv_quals quals,
11951 : : cp_ref_qualifier rqual,
11952 : : tree raises,
11953 : : int check,
11954 : : int friendp,
11955 : : int publicp,
11956 : : int inlinep,
11957 : : bool deletedp,
11958 : : bool xobj_func_p,
11959 : : special_function_kind sfk,
11960 : : bool funcdef_flag,
11961 : : bool late_return_type_p,
11962 : : int template_count,
11963 : : tree in_namespace,
11964 : : tree* attrlist,
11965 : : location_t location)
11966 : : {
11967 : 173946295 : tree decl;
11968 : 173946295 : tree t;
11969 : :
11970 : 173946295 : if (location == UNKNOWN_LOCATION)
11971 : 0 : location = input_location;
11972 : :
11973 : : /* Was the concept specifier present? */
11974 : 173946295 : bool concept_p = inlinep & 4;
11975 : :
11976 : 173946295 : if (concept_p)
11977 : : {
11978 : 15 : error_at (location, "function concepts are no longer supported");
11979 : 15 : return NULL_TREE;
11980 : : }
11981 : :
11982 : 173946280 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
11983 : :
11984 : 173946280 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
11985 : :
11986 : : /* Set the constraints on the declaration. */
11987 : 173946280 : if (flag_concepts)
11988 : : {
11989 : 51202319 : tree tmpl_reqs = NULL_TREE;
11990 : 51202319 : tree ctx = friendp ? current_class_type : ctype;
11991 : 51202319 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
11992 : 51202319 : bool memtmpl = (!block_local
11993 : 51202319 : && (current_template_depth
11994 : 51187020 : > template_class_depth (ctx)));
11995 : 14409304 : if (memtmpl)
11996 : : {
11997 : 14409304 : if (!current_template_parms)
11998 : : /* If there are no template parameters, something must have
11999 : : gone wrong. */
12000 : 0 : gcc_assert (seen_error ());
12001 : : else
12002 : 14409304 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12003 : : }
12004 : 51202319 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
12005 : : /* C++20 CA378: Remove non-templated constrained functions. */
12006 : : /* [temp.friend]/9 A non-template friend declaration with a
12007 : : requires-clause shall be a definition. A friend function template with
12008 : : a constraint that depends on a template parameter from an enclosing
12009 : : template shall be a definition. */
12010 : 51202319 : if (ci
12011 : 3794829 : && (block_local
12012 : 3794826 : || !processing_template_decl
12013 : 3794771 : || (friendp && !memtmpl && !funcdef_flag)))
12014 : : {
12015 : 67 : if (!friendp || !processing_template_decl)
12016 : 58 : error_at (location, "constraints on a non-templated function");
12017 : : else
12018 : 9 : error_at (location, "constrained non-template friend declaration"
12019 : : " must be a definition");
12020 : : ci = NULL_TREE;
12021 : : }
12022 : 51202319 : set_constraints (decl, ci);
12023 : 541823 : if (ci && friendp && memtmpl && !funcdef_flag
12024 : 51211876 : && uses_outer_template_parms_in_constraints (decl, ctx))
12025 : 6 : error_at (location, "friend function template with constraints that "
12026 : : "depend on outer template parameters must be a definition");
12027 : : }
12028 : :
12029 : 173946280 : if (TREE_CODE (type) == METHOD_TYPE)
12030 : : {
12031 : 91121340 : tree parm = build_this_parm (decl, type, quals);
12032 : 91121340 : DECL_CHAIN (parm) = parms;
12033 : 91121340 : parms = parm;
12034 : :
12035 : : /* Allocate space to hold the vptr bit if needed. */
12036 : 91121340 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
12037 : : }
12038 : :
12039 : 173946280 : DECL_ARGUMENTS (decl) = parms;
12040 : 526767517 : for (t = parms; t; t = DECL_CHAIN (t))
12041 : 352821237 : DECL_CONTEXT (t) = decl;
12042 : :
12043 : : /* Propagate volatile out from type to decl. */
12044 : 173946280 : if (TYPE_VOLATILE (type))
12045 : 0 : TREE_THIS_VOLATILE (decl) = 1;
12046 : :
12047 : : /* Setup decl according to sfk. */
12048 : 173946280 : switch (sfk)
12049 : : {
12050 : 22227397 : case sfk_constructor:
12051 : 22227397 : case sfk_copy_constructor:
12052 : 22227397 : case sfk_move_constructor:
12053 : 22227397 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
12054 : 22227397 : DECL_NAME (decl) = ctor_identifier;
12055 : 22227397 : break;
12056 : 3164081 : case sfk_destructor:
12057 : 3164081 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
12058 : 3164081 : DECL_NAME (decl) = dtor_identifier;
12059 : 3164081 : break;
12060 : 1077245 : case sfk_deduction_guide:
12061 : : /* Give deduction guides a definition even though they don't really
12062 : : have one: the restriction that you can't repeat a deduction guide
12063 : : makes them more like a definition anyway. */
12064 : 1077245 : DECL_INITIAL (decl) = void_node;
12065 : : /* But to ensure that external-linkage deduction guides in header units
12066 : : don't fall afoul of [module.import] p6, mark them as inline. */
12067 : 1077245 : DECL_DECLARED_INLINE_P (decl) = true;
12068 : 1077245 : break;
12069 : : default:
12070 : : break;
12071 : : }
12072 : :
12073 : 173946280 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
12074 : : {
12075 : 39665 : if (funcdef_flag)
12076 : 3 : error_at (location,
12077 : : "defining explicit specialization %qD in friend declaration",
12078 : : orig_declarator);
12079 : : else
12080 : : {
12081 : 39662 : tree fns = TREE_OPERAND (orig_declarator, 0);
12082 : 39662 : tree args = TREE_OPERAND (orig_declarator, 1);
12083 : :
12084 : 39662 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12085 : : {
12086 : : /* Something like `template <class T> friend void f<T>()'. */
12087 : 6 : error_at (location,
12088 : : "invalid use of template-id %qD in declaration "
12089 : : "of primary template",
12090 : : orig_declarator);
12091 : 6 : return NULL_TREE;
12092 : : }
12093 : :
12094 : : /* A friend declaration of the form friend void f<>(). Record
12095 : : the information in the TEMPLATE_ID_EXPR. */
12096 : 39656 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
12097 : :
12098 : 39656 : gcc_assert (identifier_p (fns) || OVL_P (fns));
12099 : 39656 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
12100 : :
12101 : 140614 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
12102 : 100962 : if (TREE_PURPOSE (t)
12103 : 100962 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
12104 : : {
12105 : 4 : error_at (defparse_location (TREE_PURPOSE (t)),
12106 : : "default arguments are not allowed in declaration "
12107 : : "of friend template specialization %qD",
12108 : : decl);
12109 : 4 : return NULL_TREE;
12110 : : }
12111 : :
12112 : 39652 : if (inlinep & 1)
12113 : : {
12114 : 4 : error_at (declspecs->locations[ds_inline],
12115 : : "%<inline%> is not allowed in declaration of friend "
12116 : : "template specialization %qD",
12117 : : decl);
12118 : 4 : return NULL_TREE;
12119 : : }
12120 : : }
12121 : : }
12122 : :
12123 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
12124 : : expression, that declaration shall be a definition..." */
12125 : 173946266 : if (friendp && !funcdef_flag)
12126 : : {
12127 : 1278136 : bool has_errored = false;
12128 : 1278136 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
12129 : 3697926 : t && t != void_list_node; t = TREE_CHAIN (t))
12130 : 2419790 : if (TREE_PURPOSE (t))
12131 : : {
12132 : 48 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
12133 : : /* For templates, mark the default argument as erroneous and give a
12134 : : hard error. */
12135 : 48 : if (processing_template_decl)
12136 : : {
12137 : 30 : diag_kind = diagnostics::kind::error;
12138 : 30 : TREE_PURPOSE (t) = error_mark_node;
12139 : : }
12140 : 48 : if (!has_errored)
12141 : : {
12142 : 45 : has_errored = true;
12143 : 45 : emit_diagnostic (diag_kind,
12144 : 45 : DECL_SOURCE_LOCATION (decl),
12145 : 45 : /*diagnostics::option_id=*/0,
12146 : : "friend declaration of %qD specifies default "
12147 : : "arguments and isn%'t a definition", decl);
12148 : : }
12149 : : }
12150 : : }
12151 : :
12152 : : /* If this decl has namespace scope, set that up. */
12153 : 173946266 : if (in_namespace)
12154 : 177348 : set_decl_namespace (decl, in_namespace, friendp);
12155 : 173768918 : else if (ctype)
12156 : 99834136 : DECL_CONTEXT (decl) = ctype;
12157 : : else
12158 : 73934782 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
12159 : :
12160 : : /* `main' and builtins have implicit 'C' linkage. */
12161 : 173946266 : if (ctype == NULL_TREE
12162 : 74112130 : && DECL_FILE_SCOPE_P (decl)
12163 : 32267563 : && current_lang_name == lang_name_cplusplus
12164 : 176746513 : && (MAIN_NAME_P (declarator)
12165 : 2766977 : || (IDENTIFIER_LENGTH (declarator) > 10
12166 : 1931412 : && IDENTIFIER_POINTER (declarator)[0] == '_'
12167 : 1474166 : && IDENTIFIER_POINTER (declarator)[1] == '_'
12168 : 351386 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
12169 : : "builtin_"))
12170 : 2766959 : || (targetcm.cxx_implicit_extern_c
12171 : 0 : && (targetcm.cxx_implicit_extern_c
12172 : 0 : (IDENTIFIER_POINTER (declarator))))))
12173 : 33288 : SET_DECL_LANGUAGE (decl, lang_c);
12174 : :
12175 : 173946266 : DECL_STATIC_FUNCTION_P (decl)
12176 : 173946266 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
12177 : 173946266 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
12178 : :
12179 : 173946266 : if (deletedp)
12180 : 3527001 : DECL_DELETED_FN (decl) = 1;
12181 : :
12182 : 173946266 : if (ctype && funcdef_flag)
12183 : 84456088 : check_class_member_definition_namespace (decl);
12184 : :
12185 : 173946266 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12186 : : {
12187 : 33276 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
12188 : 9 : error_at (location, "cannot declare %<::main%> to be a template");
12189 : 33276 : if (inlinep & 1)
12190 : 3 : error_at (declspecs->locations[ds_inline],
12191 : : "cannot declare %<::main%> to be inline");
12192 : 33276 : if (inlinep & 2)
12193 : 6 : error_at (declspecs->locations[ds_constexpr],
12194 : : "cannot declare %<::main%> to be %qs", "constexpr");
12195 : 33276 : if (inlinep & 8)
12196 : 3 : error_at (declspecs->locations[ds_consteval],
12197 : : "cannot declare %<::main%> to be %qs", "consteval");
12198 : 33276 : if (!publicp)
12199 : 0 : error_at (location, "cannot declare %<::main%> to be static");
12200 : 33276 : if (current_lang_name != lang_name_cplusplus)
12201 : 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
12202 : : " linkage specification other than %<extern \"C++\"%>");
12203 : 33276 : if (module_attach_p ())
12204 : : {
12205 : 3 : auto_diagnostic_group adg;
12206 : 3 : error_at (location, "cannot attach %<::main%> to a named module");
12207 : 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
12208 : : "global module instead");
12209 : 3 : }
12210 : : inlinep = 0;
12211 : : publicp = 1;
12212 : : }
12213 : :
12214 : : /* Members of anonymous types and local classes have no linkage; make
12215 : : them internal. If a typedef is made later, this will be changed. */
12216 : 173946266 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
12217 : : publicp = 0;
12218 : 173905513 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
12219 : : /* But members of local classes in a module CMI should have their
12220 : : definitions exported, in case they are (directly or indirectly)
12221 : : used by an importer. We don't just use module_has_cmi_p here
12222 : : because for entities in the GMF we don't yet know whether this
12223 : : module will have a CMI, so we'll conservatively assume it might. */
12224 : 1543775 : publicp = module_maybe_has_cmi_p ();
12225 : :
12226 : 173905513 : if (publicp && cxx_dialect == cxx98)
12227 : : {
12228 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12229 : : or enumeration declared in a local scope) shall not be used to
12230 : : declare an entity with linkage.
12231 : :
12232 : : DR 757 relaxes this restriction for C++0x. */
12233 : 936176 : no_linkage_error (decl);
12234 : : }
12235 : :
12236 : 173946266 : TREE_PUBLIC (decl) = publicp;
12237 : 173946266 : if (! publicp)
12238 : : {
12239 : 1686406 : DECL_INTERFACE_KNOWN (decl) = 1;
12240 : 1686406 : DECL_NOT_REALLY_EXTERN (decl) = 1;
12241 : : }
12242 : :
12243 : : /* If the declaration was declared inline, mark it as such. */
12244 : 173946266 : if (inlinep)
12245 : : {
12246 : 51727133 : DECL_DECLARED_INLINE_P (decl) = 1;
12247 : 51727133 : if (publicp)
12248 : 51581261 : DECL_COMDAT (decl) = 1;
12249 : : }
12250 : 51727133 : if (inlinep & 2)
12251 : 30912107 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12252 : 143034159 : else if (inlinep & 8)
12253 : : {
12254 : 287275 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
12255 : 287275 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
12256 : : }
12257 : :
12258 : 173946266 : DECL_EXTERNAL (decl) = 1;
12259 : 173946266 : if (TREE_CODE (type) == FUNCTION_TYPE)
12260 : : {
12261 : 82824926 : if (quals || rqual)
12262 : 272 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
12263 : : TYPE_UNQUALIFIED,
12264 : : REF_QUAL_NONE);
12265 : 82824926 : auto_diagnostic_group d;
12266 : 82824926 : if (quals)
12267 : 423 : error (!ctype
12268 : : ? G_("non-member function %qD cannot have cv-qualifier")
12269 : : : !xobj_func_p
12270 : 198 : ? G_("static member function %qD cannot have cv-qualifier")
12271 : : : G_("explicit object member function "
12272 : : "%qD cannot have cv-qualifier"),
12273 : : decl);
12274 : 82824926 : if (rqual)
12275 : 355 : error (!ctype
12276 : : ? G_("non-member function %qD cannot have ref-qualifier")
12277 : : : !xobj_func_p
12278 : 176 : ? G_("static member function %qD cannot have ref-qualifier")
12279 : : : G_("explicit object member function "
12280 : : "%qD cannot have ref-qualifier"),
12281 : : decl);
12282 : :
12283 : 82824926 : if (xobj_func_p && (quals || rqual))
12284 : 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
12285 : : "explicit object parameter declared here");
12286 : 82824926 : quals = TYPE_UNQUALIFIED;
12287 : 82824926 : rqual = REF_QUAL_NONE;
12288 : :
12289 : 82824926 : }
12290 : :
12291 : 173946266 : if (deduction_guide_p (decl))
12292 : : {
12293 : 1077245 : tree type = TREE_TYPE (DECL_NAME (decl));
12294 : 1077245 : if (in_namespace == NULL_TREE
12295 : 1077245 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
12296 : : {
12297 : 3 : auto_diagnostic_group d;
12298 : 3 : error_at (location, "deduction guide %qD must be declared in the "
12299 : : "same scope as %qT", decl, type);
12300 : 3 : inform (location_of (type), " declared here");
12301 : 3 : return NULL_TREE;
12302 : 3 : }
12303 : 2154484 : if (DECL_CLASS_SCOPE_P (decl)
12304 : 1077281 : && current_access_specifier != declared_access (TYPE_NAME (type)))
12305 : : {
12306 : 3 : auto_diagnostic_group d;
12307 : 3 : error_at (location, "deduction guide %qD must have the same access "
12308 : : "as %qT", decl, type);
12309 : 3 : inform (location_of (type), " declared here");
12310 : 3 : }
12311 : 1077242 : if (funcdef_flag)
12312 : 3 : error_at (location,
12313 : : "deduction guide %qD must not have a function body", decl);
12314 : : }
12315 : 324329839 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
12316 : 183438742 : && !grok_op_properties (decl, /*complain=*/true))
12317 : : return NULL_TREE;
12318 : 172868871 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
12319 : : {
12320 : 281425 : bool long_long_unsigned_p;
12321 : 281425 : bool long_double_p;
12322 : 281425 : const char *suffix = NULL;
12323 : : /* [over.literal]/6: Literal operators shall not have C linkage. */
12324 : 281425 : if (DECL_LANGUAGE (decl) == lang_c)
12325 : : {
12326 : 6 : auto_diagnostic_group d;
12327 : 6 : error_at (location, "literal operator with C linkage");
12328 : 6 : maybe_show_extern_c_location ();
12329 : 6 : return NULL_TREE;
12330 : 6 : }
12331 : :
12332 : 281419 : if (DECL_NAMESPACE_SCOPE_P (decl))
12333 : : {
12334 : 281413 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
12335 : : &long_double_p))
12336 : : {
12337 : 42 : error_at (location, "%qD has invalid argument list", decl);
12338 : 42 : return NULL_TREE;
12339 : : }
12340 : :
12341 : 281371 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
12342 : 281371 : if (long_long_unsigned_p)
12343 : : {
12344 : 34103 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
12345 : 68 : warning_at (location, 0, "integer suffix %qs"
12346 : : " shadowed by implementation", suffix);
12347 : : }
12348 : 247268 : else if (long_double_p)
12349 : : {
12350 : 89160 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
12351 : 80 : warning_at (location, 0, "floating-point suffix %qs"
12352 : : " shadowed by implementation", suffix);
12353 : : }
12354 : : /* 17.6.3.3.5 */
12355 : 281371 : if (suffix[0] != '_'
12356 : 280822 : && !current_function_decl && !(friendp && !funcdef_flag))
12357 : 280816 : warning_at (location, OPT_Wliteral_suffix,
12358 : : "literal operator suffixes not preceded by %<_%>"
12359 : : " are reserved for future standardization");
12360 : : }
12361 : : else
12362 : : {
12363 : 6 : error_at (location, "%qD must be a non-member function", decl);
12364 : 6 : return NULL_TREE;
12365 : : }
12366 : : }
12367 : :
12368 : 172868820 : if (funcdef_flag)
12369 : : /* Make the init_value nonzero so pushdecl knows this is not
12370 : : tentative. error_mark_node is replaced later with the BLOCK. */
12371 : 118153035 : DECL_INITIAL (decl) = error_mark_node;
12372 : :
12373 : 173946059 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
12374 : 65737657 : TREE_NOTHROW (decl) = 1;
12375 : :
12376 : 173946059 : if (flag_openmp || flag_openmp_simd)
12377 : : {
12378 : : /* Adjust "omp declare simd" attributes. */
12379 : 449720 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
12380 : 449720 : if (ods)
12381 : : {
12382 : : tree attr;
12383 : 2006 : for (attr = ods; attr;
12384 : 1060 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
12385 : : {
12386 : 1060 : if (TREE_CODE (type) == METHOD_TYPE)
12387 : 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
12388 : : DECL_ARGUMENTS (decl), NULL);
12389 : 1060 : if (TREE_VALUE (attr) != NULL_TREE)
12390 : : {
12391 : 854 : tree cl = TREE_VALUE (TREE_VALUE (attr));
12392 : 854 : cl = c_omp_declare_simd_clauses_to_numbers
12393 : 854 : (DECL_ARGUMENTS (decl), cl);
12394 : 854 : if (cl)
12395 : 854 : TREE_VALUE (TREE_VALUE (attr)) = cl;
12396 : : else
12397 : 0 : TREE_VALUE (attr) = NULL_TREE;
12398 : : }
12399 : : }
12400 : : }
12401 : : }
12402 : :
12403 : : /* Caller will do the rest of this. */
12404 : 173946059 : if (check < 0)
12405 : 4246501 : return decl;
12406 : :
12407 : 169699558 : if (ctype != NULL_TREE)
12408 : 99833840 : grokclassfn (ctype, decl, flags);
12409 : :
12410 : : /* 12.4/3 */
12411 : 169699558 : if (cxx_dialect >= cxx11
12412 : 337541362 : && DECL_DESTRUCTOR_P (decl)
12413 : 3146247 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
12414 : 169871034 : && !processing_template_decl)
12415 : 86632 : deduce_noexcept_on_destructor (decl);
12416 : :
12417 : 169699558 : set_originating_module (decl);
12418 : :
12419 : 169699558 : decl = check_explicit_specialization (orig_declarator, decl,
12420 : : template_count,
12421 : 169699558 : 2 * funcdef_flag +
12422 : 169699558 : 4 * (friendp != 0),
12423 : : *attrlist);
12424 : 169699558 : if (decl == error_mark_node)
12425 : : return NULL_TREE;
12426 : :
12427 : 169699310 : if (DECL_STATIC_FUNCTION_P (decl))
12428 : 8653468 : check_static_quals (decl, quals);
12429 : :
12430 : 169699310 : if (attrlist)
12431 : : {
12432 : 169699310 : cplus_decl_attributes (&decl, *attrlist, 0);
12433 : 169699310 : *attrlist = NULL_TREE;
12434 : : }
12435 : :
12436 : 169699310 : if (DECL_HAS_CONTRACTS_P (decl))
12437 : 467 : rebuild_postconditions (decl);
12438 : :
12439 : : /* Check main's type after attributes have been applied. */
12440 : 169699310 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
12441 : : {
12442 : 33264 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
12443 : : integer_type_node))
12444 : : {
12445 : 19 : tree dtype = TREE_TYPE (decl);
12446 : 19 : tree oldtypeargs = TYPE_ARG_TYPES (dtype);
12447 : 19 : tree newtype;
12448 : 19 : error_at (declspecs->locations[ds_type_spec],
12449 : : "%<::main%> must return %<int%>");
12450 : 57 : newtype = build_function_type (integer_type_node, oldtypeargs,
12451 : 19 : TYPE_NO_NAMED_ARGS_STDARG_P (dtype));
12452 : 19 : TREE_TYPE (decl) = newtype;
12453 : : }
12454 : 33264 : if (warn_main)
12455 : 33242 : check_main_parameter_types (decl);
12456 : : }
12457 : :
12458 : 169699310 : if (ctype != NULL_TREE && check)
12459 : : {
12460 : 9143608 : tree old_decl = check_classfn (ctype, decl,
12461 : 9143608 : (current_template_depth
12462 : 9143608 : > template_class_depth (ctype))
12463 : 2148922 : ? current_template_parms
12464 : : : NULL_TREE);
12465 : :
12466 : 9143608 : if (old_decl == error_mark_node)
12467 : : return NULL_TREE;
12468 : :
12469 : 9143527 : if (old_decl)
12470 : : {
12471 : 8734947 : tree ok;
12472 : 8734947 : tree pushed_scope;
12473 : :
12474 : 8734947 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
12475 : : /* Because grokfndecl is always supposed to return a
12476 : : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
12477 : : here. We depend on our callers to figure out that its
12478 : : really a template that's being returned. */
12479 : 2148910 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
12480 : :
12481 : 8734947 : if (DECL_STATIC_FUNCTION_P (old_decl)
12482 : 8734947 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
12483 : : {
12484 : : /* Remove the `this' parm added by grokclassfn. */
12485 : 162463 : revert_static_member_fn (decl);
12486 : 162463 : check_static_quals (decl, quals);
12487 : : }
12488 : 8734947 : if (DECL_ARTIFICIAL (old_decl))
12489 : : {
12490 : 9 : error ("definition of implicitly-declared %qD", old_decl);
12491 : 9 : return NULL_TREE;
12492 : : }
12493 : 8734938 : else if (DECL_DEFAULTED_FN (old_decl))
12494 : : {
12495 : 3 : auto_diagnostic_group d;
12496 : 3 : error ("definition of explicitly-defaulted %q+D", decl);
12497 : 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12498 : : "%q#D explicitly defaulted here", old_decl);
12499 : 3 : return NULL_TREE;
12500 : 3 : }
12501 : :
12502 : : /* Since we've smashed OLD_DECL to its
12503 : : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12504 : 8734935 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12505 : 955 : decl = DECL_TEMPLATE_RESULT (decl);
12506 : :
12507 : : /* Attempt to merge the declarations. This can fail, in
12508 : : the case of some invalid specialization declarations. */
12509 : 8734935 : pushed_scope = push_scope (ctype);
12510 : 8734935 : ok = duplicate_decls (decl, old_decl);
12511 : 8734935 : if (pushed_scope)
12512 : 8734935 : pop_scope (pushed_scope);
12513 : 8734935 : if (!ok)
12514 : : {
12515 : 0 : error ("no %q#D member function declared in class %qT",
12516 : : decl, ctype);
12517 : 0 : return NULL_TREE;
12518 : : }
12519 : 8734935 : if (ok == error_mark_node)
12520 : : return NULL_TREE;
12521 : : return old_decl;
12522 : : }
12523 : : }
12524 : :
12525 : 321928564 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12526 : : return NULL_TREE;
12527 : :
12528 : 160964279 : if (ctype == NULL_TREE || check)
12529 : 70274107 : return decl;
12530 : :
12531 : 90690172 : if (virtualp)
12532 : 4051050 : DECL_VIRTUAL_P (decl) = 1;
12533 : :
12534 : 90690172 : return decl;
12535 : : }
12536 : :
12537 : : /* decl is a FUNCTION_DECL.
12538 : : specifiers are the parsed virt-specifiers.
12539 : :
12540 : : Set flags to reflect the virt-specifiers.
12541 : :
12542 : : Returns decl. */
12543 : :
12544 : : static tree
12545 : 94936771 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12546 : : {
12547 : 94936771 : if (decl == NULL_TREE)
12548 : : return decl;
12549 : 94936673 : if (specifiers & VIRT_SPEC_OVERRIDE)
12550 : 186236 : DECL_OVERRIDE_P (decl) = 1;
12551 : 94936673 : if (specifiers & VIRT_SPEC_FINAL)
12552 : 2946 : DECL_FINAL_P (decl) = 1;
12553 : : return decl;
12554 : : }
12555 : :
12556 : : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12557 : : the linkage that DECL will receive in the object file. */
12558 : :
12559 : : static void
12560 : 10908035 : set_linkage_for_static_data_member (tree decl)
12561 : : {
12562 : : /* A static data member always has static storage duration and
12563 : : external linkage. Note that static data members are forbidden in
12564 : : local classes -- the only situation in which a class has
12565 : : non-external linkage. */
12566 : 10908035 : TREE_PUBLIC (decl) = 1;
12567 : 10908035 : TREE_STATIC (decl) = 1;
12568 : : /* For non-template classes, static data members are always put
12569 : : out in exactly those files where they are defined, just as
12570 : : with ordinary namespace-scope variables. */
12571 : 10908035 : if (!processing_template_decl)
12572 : 8117165 : DECL_INTERFACE_KNOWN (decl) = 1;
12573 : 10908035 : }
12574 : :
12575 : : /* Create a VAR_DECL named NAME with the indicated TYPE.
12576 : :
12577 : : If SCOPE is non-NULL, it is the class type or namespace containing
12578 : : the variable. If SCOPE is NULL, the variable should is created in
12579 : : the innermost enclosing scope. */
12580 : :
12581 : : static tree
12582 : 56872673 : grokvardecl (tree type,
12583 : : tree name,
12584 : : tree orig_declarator,
12585 : : const cp_decl_specifier_seq *declspecs,
12586 : : int initialized,
12587 : : int type_quals,
12588 : : int inlinep,
12589 : : bool conceptp,
12590 : : int template_count,
12591 : : tree scope,
12592 : : location_t location)
12593 : : {
12594 : 56872673 : tree decl;
12595 : 56872673 : tree explicit_scope;
12596 : :
12597 : 56872673 : gcc_assert (!name || identifier_p (name));
12598 : :
12599 : 56872673 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12600 : 56872673 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12601 : :
12602 : : /* Compute the scope in which to place the variable, but remember
12603 : : whether or not that scope was explicitly specified by the user. */
12604 : 56872673 : explicit_scope = scope;
12605 : 56872673 : if (!scope)
12606 : : {
12607 : : /* An explicit "extern" specifier indicates a namespace-scope
12608 : : variable. */
12609 : 56480505 : if (declspecs->storage_class == sc_extern)
12610 : 559213 : scope = current_decl_namespace ();
12611 : 55921292 : else if (!at_function_scope_p ())
12612 : 4406084 : scope = current_scope ();
12613 : : }
12614 : :
12615 : 56480505 : if (scope
12616 : 5357465 : && (/* If the variable is a namespace-scope variable declared in a
12617 : : template, we need DECL_LANG_SPECIFIC. */
12618 : 5357465 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
12619 : : /* Similarly for namespace-scope variables with language linkage
12620 : : other than C++. */
12621 : 4969901 : || (TREE_CODE (scope) == NAMESPACE_DECL
12622 : 2512884 : && current_lang_name != lang_name_cplusplus)
12623 : : /* Similarly for static data members. */
12624 : 2664584 : || TYPE_P (scope)
12625 : : /* Similarly for explicit specializations. */
12626 : 2277020 : || (orig_declarator
12627 : 2276575 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
12628 : 3467708 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
12629 : : else
12630 : 53404965 : decl = build_decl (location, VAR_DECL, name, type);
12631 : :
12632 : 56872673 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
12633 : 4604 : set_decl_namespace (decl, explicit_scope, 0);
12634 : : else
12635 : 56868069 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
12636 : :
12637 : 56872673 : if (declspecs->storage_class == sc_extern)
12638 : : {
12639 : 559213 : DECL_THIS_EXTERN (decl) = 1;
12640 : 559213 : DECL_EXTERNAL (decl) = !initialized;
12641 : : }
12642 : :
12643 : 56872673 : if (DECL_CLASS_SCOPE_P (decl))
12644 : : {
12645 : 387564 : set_linkage_for_static_data_member (decl);
12646 : : /* This function is only called with out-of-class definitions. */
12647 : 387564 : DECL_EXTERNAL (decl) = 0;
12648 : 387564 : check_class_member_definition_namespace (decl);
12649 : : }
12650 : : /* At top level, either `static' or no s.c. makes a definition
12651 : : (perhaps tentative), and absence of `static' makes it public. */
12652 : 56485109 : else if (toplevel_bindings_p ())
12653 : : {
12654 : 9937490 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
12655 : 4968745 : && (DECL_THIS_EXTERN (decl)
12656 : 4371963 : || ! constp
12657 : 4371963 : || volatilep
12658 : 4297591 : || inlinep
12659 : 357562 : || in_template_context
12660 : 27676 : || processing_specialization
12661 : 27585 : || module_attach_p ()));
12662 : 4968745 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
12663 : : }
12664 : : /* Not at top level, only `static' makes a static definition. */
12665 : : else
12666 : : {
12667 : 51516364 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
12668 : 51516364 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
12669 : : }
12670 : :
12671 : 56872673 : set_originating_module (decl);
12672 : :
12673 : 56872673 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
12674 : : {
12675 : 20729 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
12676 : : {
12677 : 20721 : CP_DECL_THREAD_LOCAL_P (decl) = true;
12678 : : // NB: Set a tentative TLS model to avoid tls_model attribute
12679 : : // warnings due to lack of thread storage duration. It will
12680 : : // be updated by cplus_decl_attributes later.
12681 : 20721 : if (!processing_template_decl)
12682 : 20599 : set_decl_tls_model (decl, TLS_MODEL_REAL);
12683 : : }
12684 : 20729 : if (declspecs->gnu_thread_keyword_p)
12685 : 19752 : SET_DECL_GNU_TLS_P (decl);
12686 : : }
12687 : :
12688 : : /* If the type of the decl has no linkage, make sure that we'll
12689 : : notice that in mark_used. */
12690 : 56872673 : if (cxx_dialect > cxx98
12691 : 56625547 : && decl_linkage (decl) != lk_none
12692 : 56062664 : && DECL_LANG_SPECIFIC (decl) == NULL
12693 : 52523360 : && !DECL_EXTERN_C_P (decl)
12694 : 109396033 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
12695 : 4880 : retrofit_lang_decl (decl);
12696 : :
12697 : 56872673 : if (TREE_PUBLIC (decl))
12698 : : {
12699 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12700 : : or enumeration declared in a local scope) shall not be used to
12701 : : declare an entity with linkage.
12702 : :
12703 : : DR 757 relaxes this restriction for C++0x. */
12704 : 5291158 : if (cxx_dialect < cxx11)
12705 : 20834 : no_linkage_error (decl);
12706 : : }
12707 : : else
12708 : 51581515 : DECL_INTERFACE_KNOWN (decl) = 1;
12709 : :
12710 : 56872673 : if (DECL_NAME (decl)
12711 : 56872673 : && MAIN_NAME_P (DECL_NAME (decl)))
12712 : : {
12713 : 12 : if (scope == global_namespace)
12714 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12715 : : "cannot declare %<::main%> to be a global variable");
12716 : 6 : else if (DECL_EXTERN_C_P (decl))
12717 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
12718 : : "an entity named %<main%> cannot be declared with "
12719 : : "C language linkage");
12720 : : }
12721 : :
12722 : : /* Check if a variable is being declared as a concept. */
12723 : 56872673 : if (conceptp)
12724 : : {
12725 : 27 : if (!processing_template_decl)
12726 : 11 : error_at (declspecs->locations[ds_concept],
12727 : : "a non-template variable cannot be %<concept%>");
12728 : 16 : else if (!at_namespace_scope_p ())
12729 : 1 : error_at (declspecs->locations[ds_concept],
12730 : : "concept must be defined at namespace scope");
12731 : : else
12732 : 15 : error_at (declspecs->locations[ds_concept],
12733 : : "variable concepts are no longer supported");
12734 : 27 : return NULL_TREE;
12735 : : }
12736 : 56872646 : else if (flag_concepts
12737 : 56872646 : && current_template_depth > template_class_depth (scope))
12738 : : {
12739 : 16207313 : tree ci = current_template_constraints ();
12740 : 16207313 : set_constraints (decl, ci);
12741 : : }
12742 : :
12743 : : // Handle explicit specializations and instantiations of variable templates.
12744 : 56872646 : if (orig_declarator)
12745 : 56302573 : decl = check_explicit_specialization (orig_declarator, decl,
12746 : : template_count, 0);
12747 : :
12748 : 56872646 : return decl != error_mark_node ? decl : NULL_TREE;
12749 : : }
12750 : :
12751 : : /* Create and return a canonical pointer to member function type, for
12752 : : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
12753 : :
12754 : : tree
12755 : 3271198 : build_ptrmemfunc_type (tree type)
12756 : : {
12757 : 3271198 : tree field, fields;
12758 : 3271198 : tree t;
12759 : :
12760 : 3271198 : if (type == error_mark_node)
12761 : : return type;
12762 : :
12763 : : /* Make sure that we always have the unqualified pointer-to-member
12764 : : type first. */
12765 : 3271198 : if (cp_cv_quals quals = cp_type_quals (type))
12766 : : {
12767 : 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
12768 : 0 : return cp_build_qualified_type (unqual, quals);
12769 : : }
12770 : :
12771 : : /* If a canonical type already exists for this type, use it. We use
12772 : : this method instead of type_hash_canon, because it only does a
12773 : : simple equality check on the list of field members. */
12774 : :
12775 : 3271198 : t = TYPE_PTRMEMFUNC_TYPE (type);
12776 : 3271198 : if (t)
12777 : : return t;
12778 : :
12779 : 1065530 : t = make_node (RECORD_TYPE);
12780 : :
12781 : : /* Let the front end know this is a pointer to member function. */
12782 : 1065530 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
12783 : :
12784 : 1065530 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
12785 : 1065530 : DECL_NONADDRESSABLE_P (field) = 1;
12786 : 1065530 : fields = field;
12787 : :
12788 : 1065530 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
12789 : : delta_type_node);
12790 : 1065530 : DECL_NONADDRESSABLE_P (field) = 1;
12791 : 1065530 : DECL_CHAIN (field) = fields;
12792 : 1065530 : fields = field;
12793 : :
12794 : 1065530 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
12795 : :
12796 : : /* Zap out the name so that the back end will give us the debugging
12797 : : information for this anonymous RECORD_TYPE. */
12798 : 1065530 : TYPE_NAME (t) = NULL_TREE;
12799 : :
12800 : : /* Cache this pointer-to-member type so that we can find it again
12801 : : later. */
12802 : 1065530 : TYPE_PTRMEMFUNC_TYPE (type) = t;
12803 : :
12804 : 1065530 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
12805 : 534112 : SET_TYPE_STRUCTURAL_EQUALITY (t);
12806 : 531418 : else if (TYPE_CANONICAL (type) != type)
12807 : 345286 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
12808 : :
12809 : : return t;
12810 : : }
12811 : :
12812 : : /* Create and return a pointer to data member type. */
12813 : :
12814 : : tree
12815 : 315313 : build_ptrmem_type (tree class_type, tree member_type)
12816 : : {
12817 : 315313 : if (TREE_CODE (member_type) == METHOD_TYPE)
12818 : : {
12819 : 62509 : cp_cv_quals quals = type_memfn_quals (member_type);
12820 : 62509 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
12821 : 62509 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
12822 : 62509 : return build_ptrmemfunc_type (build_pointer_type (member_type));
12823 : : }
12824 : : else
12825 : : {
12826 : 252804 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
12827 : 252804 : return build_offset_type (class_type, member_type);
12828 : : }
12829 : : }
12830 : :
12831 : : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
12832 : : Check to see that the definition is valid. Issue appropriate error
12833 : : messages. */
12834 : :
12835 : : static void
12836 : 13900959 : check_static_variable_definition (tree decl, tree type)
12837 : : {
12838 : : /* Avoid redundant diagnostics on out-of-class definitions. */
12839 : 13900959 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
12840 : : ;
12841 : : /* Can't check yet if we don't know the type. */
12842 : 12989099 : else if (dependent_type_p (type))
12843 : : ;
12844 : : /* If DECL is declared constexpr, we'll do the appropriate checks
12845 : : in check_initializer. Similarly for inline static data members. */
12846 : 12697732 : else if (DECL_P (decl)
12847 : 12697732 : && (DECL_DECLARED_CONSTEXPR_P (decl)
12848 : 2362065 : || DECL_VAR_DECLARED_INLINE_P (decl)))
12849 : : ;
12850 : 2361516 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12851 : : {
12852 : 56 : if (!COMPLETE_TYPE_P (type))
12853 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12854 : : "in-class initialization of static data member %q#D of "
12855 : : "incomplete type", decl);
12856 : 45 : else if (literal_type_p (type))
12857 : 39 : permerror (DECL_SOURCE_LOCATION (decl),
12858 : : "%<constexpr%> needed for in-class initialization of "
12859 : : "static data member %q#D of non-integral type", decl);
12860 : : else
12861 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12862 : : "in-class initialization of static data member %q#D of "
12863 : : "non-literal type", decl);
12864 : : }
12865 : : /* Motion 10 at San Diego: If a static const integral data member is
12866 : : initialized with an integral constant expression, the initializer
12867 : : may appear either in the declaration (within the class), or in
12868 : : the definition, but not both. If it appears in the class, the
12869 : : member is a member constant. The file-scope definition is always
12870 : : required. */
12871 : 2361460 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
12872 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12873 : : "invalid in-class initialization of static data member "
12874 : : "of non-integral type %qT",
12875 : : type);
12876 : 2361449 : else if (!CP_TYPE_CONST_P (type))
12877 : 5 : error_at (DECL_SOURCE_LOCATION (decl),
12878 : : "ISO C++ forbids in-class initialization of non-const "
12879 : : "static member %qD",
12880 : : decl);
12881 : 2361444 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12882 : 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12883 : : "ISO C++ forbids initialization of member constant "
12884 : : "%qD of non-integral type %qT", decl, type);
12885 : 13900959 : }
12886 : :
12887 : : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
12888 : : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
12889 : : expressions out into temporary variables so that walk_tree doesn't
12890 : : step into them (c++/15764). */
12891 : :
12892 : : static tree
12893 : 3238 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
12894 : : {
12895 : 3238 : hash_set<tree> *pset = (hash_set<tree> *)data;
12896 : 3238 : tree expr = *expr_p;
12897 : 3238 : if (TREE_CODE (expr) == SAVE_EXPR)
12898 : : {
12899 : 1400 : tree op = TREE_OPERAND (expr, 0);
12900 : 1400 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
12901 : 1400 : if (TREE_SIDE_EFFECTS (op))
12902 : 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
12903 : 1400 : *walk_subtrees = 0;
12904 : : }
12905 : 1838 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
12906 : 1663 : *walk_subtrees = 0;
12907 : 3238 : return NULL;
12908 : : }
12909 : :
12910 : : /* Entry point for the above. */
12911 : :
12912 : : static void
12913 : 1459 : stabilize_vla_size (tree size)
12914 : : {
12915 : 1459 : hash_set<tree> pset;
12916 : : /* Break out any function calls into temporary variables. */
12917 : 1459 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12918 : 1459 : }
12919 : :
12920 : : /* Reduce a SIZEOF_EXPR to its value. */
12921 : :
12922 : : tree
12923 : 9546839 : fold_sizeof_expr (tree t)
12924 : : {
12925 : 9546839 : tree r;
12926 : 9546839 : if (SIZEOF_EXPR_TYPE_P (t))
12927 : 8832942 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12928 : 8832942 : TREE_TYPE (TREE_OPERAND (t, 0)),
12929 : : SIZEOF_EXPR, false, false);
12930 : 713897 : else if (TYPE_P (TREE_OPERAND (t, 0)))
12931 : 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12932 : 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12933 : : false, false);
12934 : : else
12935 : 713897 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12936 : 713897 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12937 : : false, false);
12938 : 9546839 : if (r == error_mark_node)
12939 : 63 : r = size_one_node;
12940 : 9546839 : r = cp_fold_convert (size_type_node, r);
12941 : 9546839 : return r;
12942 : : }
12943 : :
12944 : : /* Given the SIZE (i.e., number of elements) in an array, compute
12945 : : an appropriate index type for the array. If non-NULL, NAME is
12946 : : the name of the entity being declared. */
12947 : :
12948 : : static tree
12949 : 2763871 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12950 : : tsubst_flags_t complain)
12951 : : {
12952 : 2763871 : if (error_operand_p (size))
12953 : 37 : return error_mark_node;
12954 : :
12955 : : /* The type of the index being computed. */
12956 : 2763834 : tree itype;
12957 : :
12958 : : /* The original numeric size as seen in the source code before
12959 : : conversion to size_t. */
12960 : 2763834 : tree origsize = size;
12961 : :
12962 : 2763834 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
12963 : :
12964 : 2763834 : if (!type_dependent_expression_p (size))
12965 : : {
12966 : 2726396 : origsize = size = mark_rvalue_use (size);
12967 : :
12968 : 34743 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
12969 : 2726552 : && TREE_SIDE_EFFECTS (size))
12970 : : /* In C++98, we mark a non-constant array bound with a magic
12971 : : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
12972 : : else
12973 : : {
12974 : 2726395 : size = build_converted_constant_expr (size_type_node, size, complain);
12975 : : /* Pedantically a constant expression is required here and so
12976 : : __builtin_is_constant_evaluated () should fold to true if it
12977 : : is successfully folded into a constant. */
12978 : 2726395 : size = fold_non_dependent_expr (size, complain,
12979 : : /*manifestly_const_eval=*/true);
12980 : :
12981 : 2726395 : if (!TREE_CONSTANT (size))
12982 : 118123 : size = origsize;
12983 : : }
12984 : :
12985 : 2726396 : if (error_operand_p (size))
12986 : 3 : return error_mark_node;
12987 : :
12988 : : /* The array bound must be an integer type. */
12989 : 2726393 : tree type = TREE_TYPE (size);
12990 : 2726393 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
12991 : : {
12992 : 21 : if (!(complain & tf_error))
12993 : 0 : return error_mark_node;
12994 : 21 : if (name)
12995 : 18 : error_at (loc, "size of array %qD has non-integral type %qT",
12996 : : name, type);
12997 : : else
12998 : 3 : error_at (loc, "size of array has non-integral type %qT", type);
12999 : 21 : size = integer_one_node;
13000 : : }
13001 : : }
13002 : :
13003 : : /* A type is dependent if it is...an array type constructed from any
13004 : : dependent type or whose size is specified by a constant expression
13005 : : that is value-dependent. */
13006 : : /* We can only call value_dependent_expression_p on integral constant
13007 : : expressions. */
13008 : 2763831 : if (processing_template_decl
13009 : 1177921 : && potential_constant_expression (size)
13010 : 3941727 : && value_dependent_expression_p (size))
13011 : : {
13012 : : /* Just build the index type and mark that it requires
13013 : : structural equality checks. */
13014 : 686851 : in_template:
13015 : 686851 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
13016 : : size, size_one_node));
13017 : 686851 : TYPE_DEPENDENT_P (itype) = 1;
13018 : 686851 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13019 : 686851 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
13020 : 686851 : return itype;
13021 : : }
13022 : :
13023 : 2091306 : if (TREE_CODE (size) != INTEGER_CST)
13024 : : {
13025 : 16192 : tree folded = cp_fully_fold (size);
13026 : 16192 : if (TREE_CODE (folded) == INTEGER_CST)
13027 : : {
13028 : 218 : if (name)
13029 : 144 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
13030 : : "integral constant-expression", name);
13031 : : else
13032 : 74 : pedwarn (loc, OPT_Wpedantic,
13033 : : "size of array is not an integral constant-expression");
13034 : : }
13035 : 16192 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
13036 : : /* We might have lost the TREE_CONSTANT flag e.g. when we are
13037 : : folding a conversion from a pointer to integral type. In that
13038 : : case issue an error below and don't treat this as a VLA. */;
13039 : : else
13040 : : /* Use the folded result for VLAs, too; it will have resolved
13041 : : SIZEOF_EXPR. */
13042 : : size = folded;
13043 : : }
13044 : :
13045 : : /* Normally, the array-bound will be a constant. */
13046 : 2091306 : if (TREE_CODE (size) == INTEGER_CST)
13047 : : {
13048 : : /* The size to use in diagnostics that reflects the constant
13049 : : size used in the source, rather than SIZE massaged above. */
13050 : 2075332 : tree diagsize = size;
13051 : :
13052 : : /* If the original size before conversion to size_t was signed
13053 : : and negative, convert it to ssizetype to restore the sign. */
13054 : 2075332 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
13055 : : && TREE_CODE (size) == INTEGER_CST
13056 : 2075332 : && tree_int_cst_sign_bit (size))
13057 : : {
13058 : 237 : diagsize = fold_convert (ssizetype, size);
13059 : :
13060 : : /* Clear the overflow bit that may have been set as a result
13061 : : of the conversion from the sizetype of the new size to
13062 : : ssizetype. */
13063 : 237 : TREE_OVERFLOW (diagsize) = false;
13064 : : }
13065 : :
13066 : : /* Verify that the array has a positive number of elements
13067 : : and issue the appropriate diagnostic if it doesn't. */
13068 : 2075332 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
13069 : : {
13070 : 468 : if (!(complain & tf_error))
13071 : 15 : return error_mark_node;
13072 : 453 : size = integer_one_node;
13073 : : }
13074 : : /* As an extension we allow zero-sized arrays. */
13075 : 2074864 : else if (integer_zerop (size))
13076 : : {
13077 : 3159 : if (!(complain & tf_error))
13078 : : /* We must fail if performing argument deduction (as
13079 : : indicated by the state of complain), so that
13080 : : another substitution can be found. */
13081 : 8 : return error_mark_node;
13082 : 3151 : else if (name)
13083 : 2884 : pedwarn (loc, OPT_Wpedantic,
13084 : : "ISO C++ forbids zero-size array %qD", name);
13085 : : else
13086 : 267 : pedwarn (loc, OPT_Wpedantic,
13087 : : "ISO C++ forbids zero-size array");
13088 : : }
13089 : : }
13090 : 15974 : else if (TREE_CONSTANT (size)
13091 : : /* We don't allow VLAs at non-function scopes, or during
13092 : : tentative template substitution. */
13093 : 15959 : || !at_function_scope_p ()
13094 : 31765 : || !(complain & tf_error))
13095 : : {
13096 : 189 : if (!(complain & tf_error))
13097 : 144 : return error_mark_node;
13098 : : /* `(int) &fn' is not a valid array bound. */
13099 : 45 : if (name)
13100 : 37 : error_at (loc,
13101 : : "size of array %qD is not an integral constant-expression",
13102 : : name);
13103 : : else
13104 : 8 : error_at (loc, "size of array is not an integral constant-expression");
13105 : 45 : size = integer_one_node;
13106 : : }
13107 : 15785 : else if (pedantic && warn_vla != 0)
13108 : : {
13109 : 30 : if (name)
13110 : 15 : pedwarn (name_loc, OPT_Wvla,
13111 : : "ISO C++ forbids variable length array %qD", name);
13112 : : else
13113 : 15 : pedwarn (input_location, OPT_Wvla,
13114 : : "ISO C++ forbids variable length array");
13115 : : }
13116 : 15755 : else if (warn_vla > 0)
13117 : : {
13118 : 3 : if (name)
13119 : 3 : warning_at (name_loc, OPT_Wvla,
13120 : : "variable length array %qD is used", name);
13121 : : else
13122 : 0 : warning (OPT_Wvla,
13123 : : "variable length array is used");
13124 : : }
13125 : :
13126 : 2091139 : if (processing_template_decl && !TREE_CONSTANT (size))
13127 : 14326 : goto in_template;
13128 : : else
13129 : : {
13130 : 2076813 : if (!TREE_CONSTANT (size))
13131 : : {
13132 : : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
13133 : : of the MINUS_EXPR, which allows the -1 to get folded with the +1
13134 : : that happens when building TYPE_SIZE. */
13135 : 1459 : size = variable_size (size);
13136 : 1459 : stabilize_vla_size (size);
13137 : : }
13138 : :
13139 : : /* Compute the index of the largest element in the array. It is
13140 : : one less than the number of elements in the array. We save
13141 : : and restore PROCESSING_TEMPLATE_DECL so that computations in
13142 : : cp_build_binary_op will be appropriately folded. */
13143 : 2076813 : {
13144 : 2076813 : processing_template_decl_sentinel s;
13145 : 2076813 : itype = cp_build_binary_op (input_location,
13146 : : MINUS_EXPR,
13147 : : cp_convert (ssizetype, size, complain),
13148 : : cp_convert (ssizetype, integer_one_node,
13149 : : complain),
13150 : : complain);
13151 : 2076813 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
13152 : 2076813 : }
13153 : :
13154 : 2076813 : if (!TREE_CONSTANT (itype))
13155 : : {
13156 : 1459 : if (sanitize_flags_p (SANITIZE_VLA)
13157 : 1459 : && current_function_decl != NULL_TREE)
13158 : : {
13159 : : /* We have to add 1 -- in the ubsan routine we generate
13160 : : LE_EXPR rather than LT_EXPR. */
13161 : 69 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
13162 : : build_one_cst (TREE_TYPE (itype)));
13163 : 69 : t = ubsan_instrument_vla (input_location, t);
13164 : 69 : finish_expr_stmt (t);
13165 : : }
13166 : : }
13167 : : /* Make sure that there was no overflow when creating to a signed
13168 : : index type. (For example, on a 32-bit machine, an array with
13169 : : size 2^32 - 1 is too big.) */
13170 : 2075354 : else if (TREE_CODE (itype) == INTEGER_CST
13171 : 2075354 : && TREE_OVERFLOW (itype))
13172 : : {
13173 : 0 : if (!(complain & tf_error))
13174 : 0 : return error_mark_node;
13175 : 0 : error ("overflow in array dimension");
13176 : 0 : TREE_OVERFLOW (itype) = 0;
13177 : : }
13178 : : }
13179 : :
13180 : : /* Create and return the appropriate index type. */
13181 : 2076813 : itype = build_index_type (itype);
13182 : :
13183 : : /* If the index type were dependent, we would have returned early, so
13184 : : remember that it isn't. */
13185 : 2076813 : TYPE_DEPENDENT_P (itype) = 0;
13186 : 2076813 : TYPE_DEPENDENT_P_VALID (itype) = 1;
13187 : 2076813 : return itype;
13188 : : }
13189 : :
13190 : : tree
13191 : 327952 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
13192 : : {
13193 : 327952 : return compute_array_index_type_loc (input_location, name, size, complain);
13194 : : }
13195 : :
13196 : : /* Returns the scope (if any) in which the entity declared by
13197 : : DECLARATOR will be located. If the entity was declared with an
13198 : : unqualified name, NULL_TREE is returned. */
13199 : :
13200 : : tree
13201 : 687154054 : get_scope_of_declarator (const cp_declarator *declarator)
13202 : : {
13203 : 801966872 : while (declarator && declarator->kind != cdk_id)
13204 : 114812818 : declarator = declarator->declarator;
13205 : :
13206 : : /* If the declarator-id is a SCOPE_REF, the scope in which the
13207 : : declaration occurs is the first operand. */
13208 : 687154054 : if (declarator
13209 : 685937338 : && declarator->u.id.qualifying_scope)
13210 : : return declarator->u.id.qualifying_scope;
13211 : :
13212 : : /* Otherwise, the declarator is not a qualified name; the entity will
13213 : : be declared in the current scope. */
13214 : : return NULL_TREE;
13215 : : }
13216 : :
13217 : : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
13218 : : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
13219 : : with this type. */
13220 : :
13221 : : static tree
13222 : 3164709 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
13223 : : {
13224 : 3164709 : tree itype = NULL_TREE;
13225 : :
13226 : : /* If things have already gone awry, bail now. */
13227 : 3164709 : if (type == error_mark_node || size == error_mark_node)
13228 : : return error_mark_node;
13229 : :
13230 : : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
13231 : : for a deduced class type. */
13232 : 3164646 : if (template_placeholder_p (type))
13233 : : {
13234 : 6 : if (name)
13235 : 0 : error_at (loc, "%qD declared as array of template placeholder "
13236 : : "type %qT", name, type);
13237 : : else
13238 : 6 : error ("creating array of template placeholder type %qT", type);
13239 : 6 : return error_mark_node;
13240 : : }
13241 : :
13242 : : /* If there are some types which cannot be array elements,
13243 : : issue an error-message and return. */
13244 : 3164640 : switch (TREE_CODE (type))
13245 : : {
13246 : 3 : case VOID_TYPE:
13247 : 3 : if (name)
13248 : 0 : error_at (loc, "declaration of %qD as array of void", name);
13249 : : else
13250 : 3 : error ("creating array of void");
13251 : 3 : return error_mark_node;
13252 : :
13253 : 6 : case FUNCTION_TYPE:
13254 : 6 : if (name)
13255 : 6 : error_at (loc, "declaration of %qD as array of functions", name);
13256 : : else
13257 : 0 : error ("creating array of functions");
13258 : 6 : return error_mark_node;
13259 : :
13260 : 21 : case REFERENCE_TYPE:
13261 : 21 : if (name)
13262 : 15 : error_at (loc, "declaration of %qD as array of references", name);
13263 : : else
13264 : 6 : error ("creating array of references");
13265 : 21 : return error_mark_node;
13266 : :
13267 : 0 : case METHOD_TYPE:
13268 : 0 : if (name)
13269 : 0 : error_at (loc, "declaration of %qD as array of function members",
13270 : : name);
13271 : : else
13272 : 0 : error ("creating array of function members");
13273 : 0 : return error_mark_node;
13274 : :
13275 : 3164610 : default:
13276 : 3164610 : break;
13277 : : }
13278 : :
13279 : 3164610 : if (!verify_type_context (name ? loc : input_location,
13280 : : TCTX_ARRAY_ELEMENT, type))
13281 : 0 : return error_mark_node;
13282 : :
13283 : : /* [dcl.array]
13284 : :
13285 : : The constant expressions that specify the bounds of the arrays
13286 : : can be omitted only for the first member of the sequence. */
13287 : 3164610 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13288 : : {
13289 : 30 : if (name)
13290 : 24 : error_at (loc, "declaration of %qD as multidimensional array must "
13291 : : "have bounds for all dimensions except the first",
13292 : : name);
13293 : : else
13294 : 6 : error ("multidimensional array must have bounds for all "
13295 : : "dimensions except the first");
13296 : :
13297 : 30 : return error_mark_node;
13298 : : }
13299 : :
13300 : : /* Figure out the index type for the array. */
13301 : 3164580 : if (size)
13302 : : {
13303 : 2435919 : itype = compute_array_index_type_loc (loc, name, size,
13304 : : tf_warning_or_error);
13305 : 2435919 : if (type_uses_auto (type)
13306 : 2435919 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
13307 : : {
13308 : 9 : sorry_at (loc, "variable-length array of %<auto%>");
13309 : 9 : return error_mark_node;
13310 : : }
13311 : : }
13312 : :
13313 : 3164571 : return build_cplus_array_type (type, itype);
13314 : : }
13315 : :
13316 : : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
13317 : :
13318 : : static location_t
13319 : 924184586 : min_location (location_t loca, location_t locb)
13320 : : {
13321 : 924184586 : if (loca == UNKNOWN_LOCATION
13322 : 924184586 : || (locb != UNKNOWN_LOCATION
13323 : 102300345 : && linemap_location_before_p (line_table, locb, loca)))
13324 : 823328224 : return locb;
13325 : : return loca;
13326 : : }
13327 : :
13328 : : /* Returns the smallest location != UNKNOWN_LOCATION among the
13329 : : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
13330 : : and LOCATIONS[ds_restrict]. */
13331 : :
13332 : : static location_t
13333 : 922612946 : smallest_type_quals_location (int type_quals, const location_t* locations)
13334 : : {
13335 : 922612946 : location_t loc = UNKNOWN_LOCATION;
13336 : :
13337 : 922612946 : if (type_quals & TYPE_QUAL_CONST)
13338 : 100915816 : loc = locations[ds_const];
13339 : :
13340 : 922612946 : if (type_quals & TYPE_QUAL_VOLATILE)
13341 : 1571549 : loc = min_location (loc, locations[ds_volatile]);
13342 : :
13343 : 922612946 : if (type_quals & TYPE_QUAL_RESTRICT)
13344 : 43 : loc = min_location (loc, locations[ds_restrict]);
13345 : :
13346 : 922612946 : return loc;
13347 : : }
13348 : :
13349 : : /* Returns the smallest among the latter and locations[ds_type_spec]. */
13350 : :
13351 : : static location_t
13352 : 922612925 : smallest_type_location (int type_quals, const location_t* locations)
13353 : : {
13354 : 922612925 : location_t loc = smallest_type_quals_location (type_quals, locations);
13355 : 922612925 : return min_location (loc, locations[ds_type_spec]);
13356 : : }
13357 : :
13358 : : static location_t
13359 : 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
13360 : : {
13361 : 26 : int type_quals = get_type_quals (declspecs);
13362 : 26 : return smallest_type_location (type_quals, declspecs->locations);
13363 : : }
13364 : :
13365 : : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
13366 : : strip out the pointer/reference declarator(s). */
13367 : :
13368 : : static bool
13369 : 27101410 : maybe_strip_indirect_ref (const cp_declarator** declarator)
13370 : : {
13371 : 27101410 : bool indirect_ref_p = false;
13372 : 54202967 : while (declarator && *declarator
13373 : 27101557 : && ((*declarator)->kind == cdk_pointer
13374 : 27101557 : || (*declarator)->kind == cdk_reference))
13375 : : {
13376 : 147 : indirect_ref_p = true;
13377 : 147 : *declarator = (*declarator)->declarator;
13378 : : }
13379 : 27101410 : return indirect_ref_p;
13380 : : }
13381 : :
13382 : : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
13383 : : and DECLARATOR. SFK indicates the kind of special function (if any) that
13384 : : this function is. OPTYPE is the type given in a conversion operator
13385 : : declaration, or the class type for a constructor/destructor.
13386 : : Returns the actual return type of the function; that may be different
13387 : : than TYPE if an error occurs, or for certain special functions. */
13388 : :
13389 : : static tree
13390 : 27101410 : check_special_function_return_type (special_function_kind sfk,
13391 : : tree type,
13392 : : tree optype,
13393 : : int type_quals,
13394 : : const cp_declarator** declarator,
13395 : : const location_t* locations)
13396 : : {
13397 : 27101410 : gcc_assert (declarator);
13398 : 27101410 : location_t rettype_loc = (type
13399 : 27101410 : ? smallest_type_location (type_quals, locations)
13400 : 27101329 : : (*declarator)->id_loc);
13401 : 27101410 : switch (sfk)
13402 : : {
13403 : 22227439 : case sfk_constructor:
13404 : 22227439 : if (maybe_strip_indirect_ref (declarator) || type)
13405 : 81 : error_at (rettype_loc,
13406 : : "return type specification for constructor invalid");
13407 : 22227358 : else if (type_quals != TYPE_UNQUALIFIED)
13408 : 15 : error_at (smallest_type_quals_location (type_quals, locations),
13409 : : "qualifiers are not allowed on constructor declaration");
13410 : :
13411 : 22227439 : if (targetm.cxx.cdtor_returns_this ())
13412 : 0 : type = build_pointer_type (optype);
13413 : : else
13414 : 22227439 : type = void_type_node;
13415 : : break;
13416 : :
13417 : 3164132 : case sfk_destructor:
13418 : 3164132 : if (maybe_strip_indirect_ref (declarator) || type)
13419 : 45 : error_at (rettype_loc,
13420 : : "return type specification for destructor invalid");
13421 : 3164087 : else if (type_quals != TYPE_UNQUALIFIED)
13422 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
13423 : : "qualifiers are not allowed on destructor declaration");
13424 : :
13425 : : /* We can't use the proper return type here because we run into
13426 : : problems with ambiguous bases and covariant returns. */
13427 : 3164132 : if (targetm.cxx.cdtor_returns_this ())
13428 : 0 : type = build_pointer_type (void_type_node);
13429 : : else
13430 : 3164132 : type = void_type_node;
13431 : : break;
13432 : :
13433 : 632588 : case sfk_conversion:
13434 : 632588 : if (maybe_strip_indirect_ref (declarator) || type)
13435 : 42 : error_at (rettype_loc,
13436 : : "return type specified for %<operator %T%>", optype);
13437 : 632546 : else if (type_quals != TYPE_UNQUALIFIED)
13438 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
13439 : : "qualifiers are not allowed on declaration of "
13440 : : "%<operator %T%>", optype);
13441 : :
13442 : : type = optype;
13443 : : break;
13444 : :
13445 : 1077251 : case sfk_deduction_guide:
13446 : 1077251 : if (maybe_strip_indirect_ref (declarator) || type)
13447 : 0 : error_at (rettype_loc,
13448 : : "return type specified for deduction guide");
13449 : 1077251 : else if (type_quals != TYPE_UNQUALIFIED)
13450 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
13451 : : "qualifiers are not allowed on declaration of "
13452 : : "deduction guide");
13453 : 1077251 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
13454 : : {
13455 : 3 : error ("template template parameter %qT in declaration of "
13456 : : "deduction guide", optype);
13457 : 3 : type = error_mark_node;
13458 : : }
13459 : : else
13460 : 1077248 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
13461 : 29085777 : for (int i = 0; i < ds_last; ++i)
13462 : 28008526 : if (i != ds_explicit && locations[i])
13463 : 3 : error_at (locations[i],
13464 : : "%<decl-specifier%> in declaration of deduction guide");
13465 : : break;
13466 : :
13467 : 0 : default:
13468 : 0 : gcc_unreachable ();
13469 : : }
13470 : :
13471 : 27101410 : return type;
13472 : : }
13473 : :
13474 : : /* A variable or data member (whose unqualified name is IDENTIFIER)
13475 : : has been declared with the indicated TYPE. If the TYPE is not
13476 : : acceptable, issue an error message and return a type to use for
13477 : : error-recovery purposes. */
13478 : :
13479 : : tree
13480 : 287297180 : check_var_type (tree identifier, tree type, location_t loc)
13481 : : {
13482 : 287297180 : if (VOID_TYPE_P (type))
13483 : : {
13484 : 88 : if (!identifier)
13485 : 0 : error_at (loc, "unnamed variable or field declared void");
13486 : 88 : else if (identifier_p (identifier))
13487 : : {
13488 : 88 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
13489 : 88 : error_at (loc, "variable or field %qE declared void",
13490 : : identifier);
13491 : : }
13492 : : else
13493 : 0 : error_at (loc, "variable or field declared void");
13494 : 88 : type = error_mark_node;
13495 : : }
13496 : :
13497 : 287297180 : return type;
13498 : : }
13499 : :
13500 : : /* Handle declaring DECL as an inline variable. */
13501 : :
13502 : : static void
13503 : 3997302 : mark_inline_variable (tree decl, location_t loc)
13504 : : {
13505 : 3997302 : bool inlinep = true;
13506 : 3997302 : if (! toplevel_bindings_p ())
13507 : : {
13508 : 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13509 : : "%qD declared at block scope", decl);
13510 : 24 : inlinep = false;
13511 : : }
13512 : 3997278 : else if (cxx_dialect < cxx17)
13513 : 690 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13514 : : "with %<-std=c++17%> or %<-std=gnu++17%>");
13515 : 714 : if (inlinep)
13516 : : {
13517 : 3997278 : retrofit_lang_decl (decl);
13518 : 3997278 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13519 : : }
13520 : 3997302 : }
13521 : :
13522 : :
13523 : : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13524 : : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13525 : : ORIG is the anonymous TYPE_NAME before that change. */
13526 : :
13527 : : static bool
13528 : 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13529 : : {
13530 : 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13531 : 42 : tree name = DECL_NAME (TYPE_NAME (type));
13532 : 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13533 : 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13534 : : "anonymous non-C-compatible type given name for linkage "
13535 : 42 : "purposes by %<typedef%> declaration");
13536 : 42 : }
13537 : :
13538 : : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13539 : : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13540 : : to TYPE but during recursion can be set to nested classes. */
13541 : :
13542 : : static bool
13543 : 515730 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13544 : : {
13545 : 515730 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13546 : : {
13547 : 6 : auto_diagnostic_group d;
13548 : 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13549 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13550 : : "type is not C-compatible because it has a base class");
13551 : 6 : return true;
13552 : 6 : }
13553 : 2445872 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13554 : 1930209 : switch (TREE_CODE (field))
13555 : : {
13556 : 9 : case VAR_DECL:
13557 : : /* static data members have been diagnosed already. */
13558 : 9 : continue;
13559 : 1286544 : case FIELD_DECL:
13560 : 1286544 : if (DECL_INITIAL (field))
13561 : : {
13562 : 4 : auto_diagnostic_group d;
13563 : 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13564 : 4 : inform (DECL_SOURCE_LOCATION (field),
13565 : : "type is not C-compatible because %qD has default "
13566 : : "member initializer", field);
13567 : 4 : return true;
13568 : 4 : }
13569 : 1286540 : continue;
13570 : 9 : case CONST_DECL:
13571 : 9 : continue;
13572 : 643570 : case TYPE_DECL:
13573 : 643570 : if (DECL_SELF_REFERENCE_P (field))
13574 : 515698 : continue;
13575 : 127872 : if (DECL_IMPLICIT_TYPEDEF_P (field))
13576 : : {
13577 : 127862 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13578 : 3 : continue;
13579 : 127859 : if (CLASS_TYPE_P (TREE_TYPE (field)))
13580 : : {
13581 : 127859 : tree tf = TREE_TYPE (field);
13582 : 127859 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13583 : : tf))
13584 : : return true;
13585 : 127834 : continue;
13586 : 127834 : }
13587 : : }
13588 : : /* FALLTHRU */
13589 : 32 : case FUNCTION_DECL:
13590 : 32 : case TEMPLATE_DECL:
13591 : 32 : {
13592 : 32 : auto_diagnostic_group d;
13593 : 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13594 : 32 : inform (DECL_SOURCE_LOCATION (field),
13595 : : "type is not C-compatible because it contains %qD "
13596 : : "declaration", field);
13597 : 32 : return true;
13598 : 32 : }
13599 : : default:
13600 : : break;
13601 : 1286558 : }
13602 : : return false;
13603 : : }
13604 : :
13605 : : /* Assign a typedef-given name to a class or enumeration type declared
13606 : : as anonymous at first. This was split out of grokdeclarator
13607 : : because it is also used in libcc1. */
13608 : :
13609 : : void
13610 : 389501 : name_unnamed_type (tree type, tree decl)
13611 : : {
13612 : 1168503 : gcc_assert (TYPE_UNNAMED_P (type)
13613 : : || enum_with_enumerator_for_linkage_p (type));
13614 : :
13615 : : /* Replace the anonymous decl with the real decl. Be careful not to
13616 : : rename other typedefs (such as the self-reference) of type. */
13617 : 389501 : tree orig = TYPE_NAME (type);
13618 : 1166932 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13619 : 777431 : if (TYPE_NAME (t) == orig)
13620 : 389501 : TYPE_NAME (t) = decl;
13621 : :
13622 : : /* If this is a typedef within a template class, the nested
13623 : : type is a (non-primary) template. The name for the
13624 : : template needs updating as well. */
13625 : 389501 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
13626 : 64 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
13627 : :
13628 : : /* Adjust linkage now that we aren't unnamed anymore. */
13629 : 389501 : reset_type_linkage (type);
13630 : :
13631 : 389501 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
13632 : 387871 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
13633 : :
13634 : : /* FIXME remangle member functions; member functions of a
13635 : : type with external linkage have external linkage. */
13636 : :
13637 : : /* Check that our job is done, and that it would fail if we
13638 : : attempted to do it again. */
13639 : 779002 : gcc_assert (!TYPE_UNNAMED_P (type)
13640 : : && !enum_with_enumerator_for_linkage_p (type));
13641 : 389501 : }
13642 : :
13643 : : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
13644 : : is allowed. TYPE might contain a decltype(auto). Returns true if
13645 : : there was a problem, false otherwise. */
13646 : :
13647 : : static bool
13648 : 581743380 : check_decltype_auto (location_t loc, tree type)
13649 : : {
13650 : 581743380 : if (tree a = type_uses_auto (type))
13651 : : {
13652 : 10267145 : if (AUTO_IS_DECLTYPE (a))
13653 : : {
13654 : 339759 : if (a != type)
13655 : : {
13656 : 33 : error_at (loc, "%qT as type rather than plain "
13657 : : "%<decltype(auto)%>", type);
13658 : 33 : return true;
13659 : : }
13660 : 339726 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
13661 : : {
13662 : 35 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
13663 : 35 : return true;
13664 : : }
13665 : : }
13666 : : }
13667 : : return false;
13668 : : }
13669 : :
13670 : : /* Issue an error about two mutually incompatible declspecs
13671 : : with the given names and locations
13672 : : e.g. "error: `signed' and `unsigned' specified together" */
13673 : :
13674 : : static void
13675 : 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
13676 : : const char *name_b, location_t loc_b)
13677 : : {
13678 : 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
13679 : 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
13680 : : nullptr, highlight_colors::rhs);
13681 : 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
13682 : 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
13683 : 66 : error_at (&richloc, "%e and %e specified together",
13684 : : &e_name_a, &e_name_b);
13685 : 66 : }
13686 : :
13687 : : /* Given declspecs and a declarator (abstract or otherwise), determine
13688 : : the name and type of the object declared and construct a DECL node
13689 : : for it.
13690 : :
13691 : : DECLSPECS points to the representation of declaration-specifier
13692 : : sequence that precedes declarator.
13693 : :
13694 : : DECL_CONTEXT says which syntactic context this declaration is in:
13695 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
13696 : : FUNCDEF for a function definition. Like NORMAL but a few different
13697 : : error messages in each case. Return value may be zero meaning
13698 : : this definition is too screwy to try to parse.
13699 : : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
13700 : : handle member functions (which have FIELD context).
13701 : : Return value may be zero meaning this definition is too screwy to
13702 : : try to parse.
13703 : : PARM for a parameter declaration (either within a function prototype
13704 : : or before a function body). Make a PARM_DECL, or return void_type_node.
13705 : : TPARM for a template parameter declaration.
13706 : : CATCHPARM for a parameter declaration before a catch clause.
13707 : : TYPENAME if for a typename (in a cast or sizeof).
13708 : : Don't make a DECL node; just return the ..._TYPE node.
13709 : : FIELD for a struct or union field; make a FIELD_DECL.
13710 : : BITFIELD for a field with specified width.
13711 : :
13712 : : INITIALIZED is as for start_decl.
13713 : :
13714 : : ATTRLIST is a pointer to the list of attributes, which may be NULL
13715 : : if there are none; *ATTRLIST may be modified if attributes from inside
13716 : : the declarator should be applied to the declaration.
13717 : :
13718 : : When this function is called, scoping variables (such as
13719 : : CURRENT_CLASS_TYPE) should reflect the scope in which the
13720 : : declaration occurs, not the scope in which the new declaration will
13721 : : be placed. For example, on:
13722 : :
13723 : : void S::f() { ... }
13724 : :
13725 : : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
13726 : : should not be `S'.
13727 : :
13728 : : Returns a DECL (if a declarator is present), a TYPE (if there is no
13729 : : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
13730 : : error occurs. */
13731 : :
13732 : : tree
13733 : 922612818 : grokdeclarator (const cp_declarator *declarator,
13734 : : cp_decl_specifier_seq *declspecs,
13735 : : enum decl_context decl_context,
13736 : : int initialized,
13737 : : tree* attrlist)
13738 : : {
13739 : 922612818 : tree type = NULL_TREE;
13740 : 922612818 : int longlong = 0;
13741 : 922612818 : int explicit_intN = 0;
13742 : 922612818 : int int_n_alt = 0;
13743 : 922612818 : int virtualp, explicitp, friendp, inlinep, staticp;
13744 : 922612818 : int explicit_int = 0;
13745 : 922612818 : int explicit_char = 0;
13746 : 922612818 : int defaulted_int = 0;
13747 : :
13748 : 922612818 : tree typedef_decl = NULL_TREE;
13749 : 922612818 : const char *name = NULL;
13750 : 922612818 : tree typedef_type = NULL_TREE;
13751 : : /* True if this declarator is a function definition. */
13752 : 922612818 : bool funcdef_flag = false;
13753 : 922612818 : cp_declarator_kind innermost_code = cdk_error;
13754 : 922612818 : int bitfield = 0;
13755 : : #if 0
13756 : : /* See the code below that used this. */
13757 : : tree decl_attr = NULL_TREE;
13758 : : #endif
13759 : :
13760 : : /* Keep track of what sort of function is being processed
13761 : : so that we can warn about default return values, or explicit
13762 : : return values which do not match prescribed defaults. */
13763 : 922612818 : special_function_kind sfk = sfk_none;
13764 : :
13765 : 922612818 : tree dname = NULL_TREE;
13766 : 922612818 : tree ctor_return_type = NULL_TREE;
13767 : 922612818 : enum overload_flags flags = NO_SPECIAL;
13768 : : /* cv-qualifiers that apply to the declarator, for a declaration of
13769 : : a member function. */
13770 : 922612818 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
13771 : : /* virt-specifiers that apply to the declarator, for a declaration of
13772 : : a member function. */
13773 : 922612818 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
13774 : : /* ref-qualifier that applies to the declarator, for a declaration of
13775 : : a member function. */
13776 : 922612818 : cp_ref_qualifier rqual = REF_QUAL_NONE;
13777 : : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
13778 : 922612818 : int type_quals = get_type_quals (declspecs);
13779 : 922612818 : tree raises = NULL_TREE;
13780 : 922612818 : int template_count = 0;
13781 : 922612818 : tree returned_attrs = NULL_TREE;
13782 : 922612818 : tree parms = NULL_TREE;
13783 : 922612818 : const cp_declarator *id_declarator;
13784 : : /* The unqualified name of the declarator; either an
13785 : : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
13786 : 922612818 : tree unqualified_id;
13787 : : /* The class type, if any, in which this entity is located,
13788 : : or NULL_TREE if none. Note that this value may be different from
13789 : : the current class type; for example if an attempt is made to declare
13790 : : "A::f" inside "B", this value will be "A". */
13791 : 922612818 : tree ctype = current_class_type;
13792 : : /* The NAMESPACE_DECL for the namespace in which this entity is
13793 : : located. If an unqualified name is used to declare the entity,
13794 : : this value will be NULL_TREE, even if the entity is located at
13795 : : namespace scope. */
13796 : 922612818 : tree in_namespace = NULL_TREE;
13797 : 922612818 : cp_storage_class storage_class;
13798 : 922612818 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
13799 : 922612818 : bool type_was_error_mark_node = false;
13800 : 922612818 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
13801 : 922612818 : bool template_type_arg = false;
13802 : 922612818 : bool template_parm_flag = false;
13803 : 922612818 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
13804 : 922612818 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
13805 : 922612818 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
13806 : 922612818 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
13807 : 922612818 : bool late_return_type_p = false;
13808 : 922612818 : bool array_parameter_p = false;
13809 : 922612818 : tree reqs = NULL_TREE;
13810 : :
13811 : 922612818 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
13812 : 922612818 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
13813 : 922612818 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
13814 : 922612818 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
13815 : 922612818 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
13816 : 922612818 : explicit_intN = declspecs->explicit_intN_p;
13817 : 922612818 : int_n_alt = declspecs->int_n_alt;
13818 : 922612818 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
13819 : :
13820 : : // Was concept_p specified? Note that ds_concept
13821 : : // implies ds_constexpr!
13822 : 922612818 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
13823 : 922612818 : if (concept_p)
13824 : 114 : constexpr_p = true;
13825 : :
13826 : 922612818 : if (decl_context == FUNCDEF)
13827 : : funcdef_flag = true, decl_context = NORMAL;
13828 : : else if (decl_context == MEMFUNCDEF)
13829 : : funcdef_flag = true, decl_context = FIELD;
13830 : : else if (decl_context == BITFIELD)
13831 : : bitfield = 1, decl_context = FIELD;
13832 : : else if (decl_context == TEMPLATE_TYPE_ARG)
13833 : : template_type_arg = true, decl_context = TYPENAME;
13834 : : else if (decl_context == TPARM)
13835 : 11983997 : template_parm_flag = true, decl_context = PARM;
13836 : :
13837 : 922612818 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
13838 : 9302544 : funcdef_flag = true;
13839 : :
13840 : 922612818 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
13841 : : (type_quals,
13842 : 922612818 : declspecs->locations));
13843 : 922612818 : location_t id_loc;
13844 : 922612818 : location_t init_loc;
13845 : 922612818 : if (declarator)
13846 : : {
13847 : 572057505 : id_loc = loc_or_input_loc (declarator->id_loc);
13848 : 572057505 : init_loc = loc_or_input_loc (declarator->init_loc);
13849 : : }
13850 : : else
13851 : 350555313 : init_loc = id_loc = input_location;
13852 : :
13853 : : /* Look inside a declarator for the name being declared
13854 : : and get it as a string, for an error message. */
13855 : 922612818 : for (id_declarator = declarator;
13856 : 1282103802 : id_declarator;
13857 : 359490984 : id_declarator = id_declarator->declarator)
13858 : : {
13859 : 894835134 : if (id_declarator->kind != cdk_id)
13860 : 359491009 : innermost_code = id_declarator->kind;
13861 : :
13862 : 894835134 : switch (id_declarator->kind)
13863 : : {
13864 : 176427679 : case cdk_function:
13865 : 176427679 : if (id_declarator->declarator
13866 : 175955569 : && id_declarator->declarator->kind == cdk_id)
13867 : : {
13868 : 174092000 : sfk = id_declarator->declarator->u.id.sfk;
13869 : 174092000 : if (sfk == sfk_destructor)
13870 : 3164135 : flags = DTOR_FLAG;
13871 : : }
13872 : : break;
13873 : :
13874 : 535344125 : case cdk_id:
13875 : 535344125 : {
13876 : 535344125 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
13877 : 535344125 : tree decl = id_declarator->u.id.unqualified_name;
13878 : 535344125 : if (!decl)
13879 : : break;
13880 : 535344125 : if (qualifying_scope)
13881 : : {
13882 : 9713659 : if (check_for_bare_parameter_packs (qualifying_scope,
13883 : 9713659 : id_declarator->id_loc))
13884 : 6 : return error_mark_node;
13885 : 9713653 : if (at_function_scope_p ())
13886 : : {
13887 : : /* [dcl.meaning]
13888 : :
13889 : : A declarator-id shall not be qualified except
13890 : : for ...
13891 : :
13892 : : None of the cases are permitted in block
13893 : : scope. */
13894 : 0 : if (qualifying_scope == global_namespace)
13895 : 0 : error ("invalid use of qualified-name %<::%D%>",
13896 : : decl);
13897 : 0 : else if (TYPE_P (qualifying_scope))
13898 : 0 : error ("invalid use of qualified-name %<%T::%D%>",
13899 : : qualifying_scope, decl);
13900 : : else
13901 : 0 : error ("invalid use of qualified-name %<%D::%D%>",
13902 : : qualifying_scope, decl);
13903 : 0 : return error_mark_node;
13904 : : }
13905 : 9713653 : else if (TYPE_P (qualifying_scope))
13906 : : {
13907 : 9531659 : ctype = qualifying_scope;
13908 : 9531659 : if (!MAYBE_CLASS_TYPE_P (ctype))
13909 : : {
13910 : 6 : error_at (id_declarator->id_loc,
13911 : : "%q#T is not a class or namespace", ctype);
13912 : 6 : ctype = NULL_TREE;
13913 : : }
13914 : 9531653 : else if (innermost_code != cdk_function
13915 : 387616 : && current_class_type
13916 : 9531668 : && !uniquely_derived_from_p (ctype,
13917 : : current_class_type))
13918 : : {
13919 : 9 : error_at (id_declarator->id_loc,
13920 : : "invalid use of qualified-name %<%T::%D%>",
13921 : : qualifying_scope, decl);
13922 : 9 : return error_mark_node;
13923 : : }
13924 : : }
13925 : 181994 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
13926 : 535344110 : in_namespace = qualifying_scope;
13927 : : }
13928 : 535344110 : switch (TREE_CODE (decl))
13929 : : {
13930 : 3164150 : case BIT_NOT_EXPR:
13931 : 3164150 : {
13932 : 3164150 : if (innermost_code != cdk_function)
13933 : : {
13934 : 6 : error_at (EXPR_LOCATION (decl),
13935 : : "declaration of %qE as non-function", decl);
13936 : 6 : return error_mark_node;
13937 : : }
13938 : 3164144 : else if (!qualifying_scope
13939 : 3164144 : && !(current_class_type && at_class_scope_p ()))
13940 : : {
13941 : 9 : error_at (EXPR_LOCATION (decl),
13942 : : "declaration of %qE as non-member", decl);
13943 : 9 : return error_mark_node;
13944 : : }
13945 : :
13946 : 3164135 : tree type = TREE_OPERAND (decl, 0);
13947 : 3164135 : if (TYPE_P (type))
13948 : 3164132 : type = constructor_name (type);
13949 : 3164135 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
13950 : 3164135 : dname = decl;
13951 : : }
13952 : 3164135 : break;
13953 : :
13954 : 2012159 : case TEMPLATE_ID_EXPR:
13955 : 2012159 : {
13956 : 2012159 : tree fns = TREE_OPERAND (decl, 0);
13957 : :
13958 : 2012159 : dname = fns;
13959 : 2919173 : if (!identifier_p (dname))
13960 : 2012144 : dname = OVL_NAME (dname);
13961 : : }
13962 : : /* Fall through. */
13963 : :
13964 : 532179960 : case IDENTIFIER_NODE:
13965 : 532179960 : if (identifier_p (decl))
13966 : 530167801 : dname = decl;
13967 : :
13968 : 532179960 : if (IDENTIFIER_KEYWORD_P (dname))
13969 : : {
13970 : 0 : error ("declarator-id missing; using reserved word %qD",
13971 : : dname);
13972 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13973 : : }
13974 : 532179960 : else if (!IDENTIFIER_CONV_OP_P (dname))
13975 : 531547369 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13976 : : else
13977 : : {
13978 : 632591 : gcc_assert (flags == NO_SPECIAL);
13979 : 632591 : flags = TYPENAME_FLAG;
13980 : 632591 : sfk = sfk_conversion;
13981 : 632591 : tree glob = get_global_binding (dname);
13982 : 632591 : if (glob && TREE_CODE (glob) == TYPE_DECL)
13983 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13984 : : else
13985 : : name = "<invalid operator>";
13986 : : }
13987 : : break;
13988 : :
13989 : 0 : default:
13990 : 0 : gcc_unreachable ();
13991 : : }
13992 : : break;
13993 : : }
13994 : :
13995 : : case cdk_array:
13996 : : case cdk_pointer:
13997 : : case cdk_reference:
13998 : : case cdk_ptrmem:
13999 : : break;
14000 : :
14001 : : case cdk_decomp:
14002 : 894835079 : name = "structured binding";
14003 : : break;
14004 : :
14005 : 25 : case cdk_error:
14006 : 25 : return error_mark_node;
14007 : :
14008 : 0 : default:
14009 : 0 : gcc_unreachable ();
14010 : : }
14011 : 894835079 : if (id_declarator->kind == cdk_id)
14012 : : break;
14013 : : }
14014 : :
14015 : : /* [dcl.fct.edf]
14016 : :
14017 : : The declarator in a function-definition shall have the form
14018 : : D1 ( parameter-declaration-clause) ... */
14019 : 922612763 : if (funcdef_flag && innermost_code != cdk_function)
14020 : : {
14021 : 3 : error_at (id_loc, "function definition does not declare parameters");
14022 : 3 : return error_mark_node;
14023 : : }
14024 : :
14025 : 922612760 : if (flags == TYPENAME_FLAG
14026 : 922612760 : && innermost_code != cdk_function
14027 : 3 : && ! (ctype && !declspecs->any_specifiers_p))
14028 : : {
14029 : 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
14030 : 3 : return error_mark_node;
14031 : : }
14032 : :
14033 : 922612757 : if (dname && identifier_p (dname))
14034 : : {
14035 : 532179954 : if (UDLIT_OPER_P (dname)
14036 : 532179954 : && innermost_code != cdk_function)
14037 : : {
14038 : 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
14039 : 6 : return error_mark_node;
14040 : : }
14041 : :
14042 : 532179948 : if (IDENTIFIER_ANY_OP_P (dname))
14043 : : {
14044 : 31977982 : if (typedef_p)
14045 : : {
14046 : 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
14047 : 3 : return error_mark_node;
14048 : : }
14049 : 31977979 : else if (decl_context == PARM || decl_context == CATCHPARM)
14050 : : {
14051 : 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
14052 : 15 : return error_mark_node;
14053 : : }
14054 : : }
14055 : : }
14056 : :
14057 : : /* Anything declared one level down from the top level
14058 : : must be one of the parameters of a function
14059 : : (because the body is at least two levels down). */
14060 : :
14061 : : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
14062 : : by not allowing C++ class definitions to specify their parameters
14063 : : with xdecls (must be spec.d in the parmlist).
14064 : :
14065 : : Since we now wait to push a class scope until we are sure that
14066 : : we are in a legitimate method context, we must set oldcname
14067 : : explicitly (since current_class_name is not yet alive).
14068 : :
14069 : : We also want to avoid calling this a PARM if it is in a namespace. */
14070 : :
14071 : 922612733 : if (decl_context == NORMAL && !toplevel_bindings_p ())
14072 : : {
14073 : 62718241 : cp_binding_level *b = current_binding_level;
14074 : 62718241 : current_binding_level = b->level_chain;
14075 : 62718241 : if (current_binding_level != 0 && toplevel_bindings_p ())
14076 : : decl_context = PARM;
14077 : 62718241 : current_binding_level = b;
14078 : : }
14079 : :
14080 : 922612733 : if (name == NULL)
14081 : 387182336 : name = decl_context == PARM ? "parameter" : "type name";
14082 : :
14083 : 922612733 : if (consteval_p && constexpr_p)
14084 : : {
14085 : 6 : error_at (declspecs->locations[ds_consteval],
14086 : : "both %qs and %qs specified", "constexpr", "consteval");
14087 : 6 : return error_mark_node;
14088 : : }
14089 : :
14090 : 922612727 : if (concept_p && typedef_p)
14091 : : {
14092 : 9 : error_at (declspecs->locations[ds_concept],
14093 : : "%qs cannot appear in a typedef declaration", "concept");
14094 : 9 : return error_mark_node;
14095 : : }
14096 : :
14097 : 922612718 : if (constexpr_p && typedef_p)
14098 : : {
14099 : 3 : error_at (declspecs->locations[ds_constexpr],
14100 : : "%qs cannot appear in a typedef declaration", "constexpr");
14101 : 3 : return error_mark_node;
14102 : : }
14103 : :
14104 : 922612715 : if (consteval_p && typedef_p)
14105 : : {
14106 : 3 : error_at (declspecs->locations[ds_consteval],
14107 : : "%qs cannot appear in a typedef declaration", "consteval");
14108 : 3 : return error_mark_node;
14109 : : }
14110 : :
14111 : 922612712 : if (constinit_p && typedef_p)
14112 : : {
14113 : 3 : error_at (declspecs->locations[ds_constinit],
14114 : : "%qs cannot appear in a typedef declaration", "constinit");
14115 : 3 : return error_mark_node;
14116 : : }
14117 : :
14118 : : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
14119 : : keywords shall appear in a decl-specifier-seq." */
14120 : 922612709 : if (constinit_p && constexpr_p)
14121 : : {
14122 : 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
14123 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14124 : 6 : error_at (&richloc,
14125 : : "can use at most one of the %<constinit%> and %<constexpr%> "
14126 : : "specifiers");
14127 : 6 : return error_mark_node;
14128 : 6 : }
14129 : :
14130 : : /* If there were multiple types specified in the decl-specifier-seq,
14131 : : issue an error message. */
14132 : 922612703 : if (declspecs->multiple_types_p)
14133 : : {
14134 : 1500 : error_at (typespec_loc,
14135 : : "two or more data types in declaration of %qs", name);
14136 : 1500 : return error_mark_node;
14137 : : }
14138 : :
14139 : 922611203 : if (declspecs->conflicting_specifiers_p)
14140 : 31 : return error_mark_node;
14141 : :
14142 : : /* Extract the basic type from the decl-specifier-seq. */
14143 : 922611172 : type = declspecs->type;
14144 : 922611172 : if (type == error_mark_node)
14145 : : {
14146 : 1175 : type = NULL_TREE;
14147 : 1175 : type_was_error_mark_node = true;
14148 : : }
14149 : :
14150 : : /* Ignore erroneous attributes. */
14151 : 922611172 : if (attrlist && *attrlist == error_mark_node)
14152 : 12 : *attrlist = NULL_TREE;
14153 : :
14154 : : /* An object declared as __attribute__((unavailable)) suppresses
14155 : : any reports of being declared with unavailable or deprecated
14156 : : items. An object declared as __attribute__((deprecated))
14157 : : suppresses warnings of uses of other deprecated items. */
14158 : 922611172 : auto ds = make_temp_override (deprecated_state);
14159 : 922611172 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
14160 : 159 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
14161 : 922611013 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
14162 : 711108 : deprecated_state = DEPRECATED_SUPPRESS;
14163 : :
14164 : 922611172 : cp_handle_deprecated_or_unavailable (type);
14165 : 922611172 : if (type && TREE_CODE (type) == TYPE_DECL)
14166 : : {
14167 : 632199754 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
14168 : 632199754 : typedef_decl = type;
14169 : 632199754 : type = TREE_TYPE (typedef_decl);
14170 : 632199754 : if (DECL_ARTIFICIAL (typedef_decl))
14171 : 456544958 : cp_handle_deprecated_or_unavailable (type);
14172 : : }
14173 : : /* No type at all: default to `int', and set DEFAULTED_INT
14174 : : because it was not a user-defined typedef. */
14175 : 922611172 : if (type == NULL_TREE)
14176 : : {
14177 : 35277812 : if (signed_p || unsigned_p || long_p || short_p)
14178 : : {
14179 : : /* These imply 'int'. */
14180 : 8174826 : type = integer_type_node;
14181 : 8174826 : defaulted_int = 1;
14182 : : }
14183 : : /* If we just have "complex", it is equivalent to "complex double". */
14184 : 27102986 : else if (!longlong && !explicit_intN
14185 : 27102986 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
14186 : : {
14187 : 81 : type = double_type_node;
14188 : 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
14189 : : "ISO C++ does not support plain %<complex%> meaning "
14190 : : "%<double complex%>");
14191 : : }
14192 : : }
14193 : : /* Gather flags. */
14194 : 922611172 : explicit_int = declspecs->explicit_int_p;
14195 : 922611172 : explicit_char = declspecs->explicit_char_p;
14196 : :
14197 : : #if 0
14198 : : /* See the code below that used this. */
14199 : : if (typedef_decl)
14200 : : decl_attr = DECL_ATTRIBUTES (typedef_decl);
14201 : : #endif
14202 : 922611172 : typedef_type = type;
14203 : :
14204 : 922611172 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
14205 : 1709839 : ctor_return_type = TREE_TYPE (dname);
14206 : : else
14207 : : ctor_return_type = ctype;
14208 : :
14209 : 922611172 : if (sfk != sfk_none)
14210 : : {
14211 : 27101410 : type = check_special_function_return_type (sfk, type,
14212 : : ctor_return_type,
14213 : : type_quals,
14214 : : &declarator,
14215 : : declspecs->locations);
14216 : 27101410 : type_quals = TYPE_UNQUALIFIED;
14217 : : }
14218 : 895509762 : else if (type == NULL_TREE)
14219 : : {
14220 : 1576 : int is_main;
14221 : :
14222 : 1576 : explicit_int = -1;
14223 : :
14224 : : /* We handle `main' specially here, because 'main () { }' is so
14225 : : common. With no options, it is allowed. With -Wreturn-type,
14226 : : it is a warning. It is only an error with -pedantic-errors. */
14227 : 3152 : is_main = (funcdef_flag
14228 : 3387 : && dname && identifier_p (dname)
14229 : 235 : && MAIN_NAME_P (dname)
14230 : 68 : && ctype == NULL_TREE
14231 : 68 : && in_namespace == NULL_TREE
14232 : 1644 : && current_namespace == global_namespace);
14233 : :
14234 : 1576 : if (type_was_error_mark_node)
14235 : : /* We've already issued an error, don't complain more. */;
14236 : 401 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
14237 : : /* Allow it, sigh. */;
14238 : 339 : else if (! is_main)
14239 : 277 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
14240 : : name);
14241 : 62 : else if (pedantic)
14242 : 4 : pedwarn (id_loc, OPT_Wpedantic,
14243 : : "ISO C++ forbids declaration of %qs with no type", name);
14244 : : else
14245 : 58 : warning_at (id_loc, OPT_Wreturn_type,
14246 : : "ISO C++ forbids declaration of %qs with no type", name);
14247 : :
14248 : 1576 : if (type_was_error_mark_node && template_parm_flag)
14249 : : /* FIXME we should be able to propagate the error_mark_node as is
14250 : : for other contexts too. */
14251 : 151 : type = error_mark_node;
14252 : : else
14253 : 1425 : type = integer_type_node;
14254 : : }
14255 : :
14256 : 922611172 : ctype = NULL_TREE;
14257 : :
14258 : 922611172 : if (explicit_intN)
14259 : : {
14260 : 957454 : if (! int_n_enabled_p[declspecs->int_n_idx])
14261 : : {
14262 : 0 : error_at (declspecs->locations[ds_type_spec],
14263 : : "%<__int%d%> is not supported by this target",
14264 : 0 : int_n_data[declspecs->int_n_idx].bitsize);
14265 : 0 : explicit_intN = false;
14266 : : }
14267 : : /* Don't pedwarn if the alternate "__intN__" form has been used instead
14268 : : of "__intN". */
14269 : 957454 : else if (!int_n_alt && pedantic)
14270 : 3914 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
14271 : : "ISO C++ does not support %<__int%d%> for %qs",
14272 : 3914 : int_n_data[declspecs->int_n_idx].bitsize, name);
14273 : : }
14274 : :
14275 : : /* Now process the modifiers that were specified
14276 : : and check for invalid combinations. */
14277 : :
14278 : : /* Long double is a special combination. */
14279 : 922611172 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
14280 : : {
14281 : 9119151 : long_p = false;
14282 : 9119151 : type = cp_build_qualified_type (long_double_type_node,
14283 : : cp_type_quals (type));
14284 : : }
14285 : :
14286 : : /* Check all other uses of type modifiers. */
14287 : :
14288 : 922611172 : if (unsigned_p || signed_p || long_p || short_p)
14289 : : {
14290 : 19682918 : location_t loc;
14291 : 19682918 : const char *key;
14292 : 19682918 : if (unsigned_p)
14293 : : {
14294 : 13000318 : key = "unsigned";
14295 : 13000318 : loc = declspecs->locations[ds_unsigned];
14296 : : }
14297 : 6682600 : else if (signed_p)
14298 : : {
14299 : 1080376 : key = "signed";
14300 : 1080376 : loc = declspecs->locations[ds_signed];
14301 : : }
14302 : 5602224 : else if (longlong)
14303 : : {
14304 : 2171736 : key = "long long";
14305 : 2171736 : loc = declspecs->locations[ds_long_long];
14306 : : }
14307 : 3430488 : else if (long_p)
14308 : : {
14309 : 2920697 : key = "long";
14310 : 2920697 : loc = declspecs->locations[ds_long];
14311 : : }
14312 : : else /* if (short_p) */
14313 : : {
14314 : 509791 : key = "short";
14315 : 509791 : loc = declspecs->locations[ds_short];
14316 : : }
14317 : :
14318 : 19682918 : int ok = 0;
14319 : :
14320 : 19682918 : if (signed_p && unsigned_p)
14321 : 54 : complain_about_incompatible_declspecs
14322 : 54 : ("signed", declspecs->locations[ds_signed],
14323 : : "unsigned", declspecs->locations[ds_unsigned]);
14324 : 19682864 : else if (long_p && short_p)
14325 : 12 : complain_about_incompatible_declspecs
14326 : 12 : ("long", declspecs->locations[ds_long],
14327 : : "short", declspecs->locations[ds_short]);
14328 : 19682852 : else if (TREE_CODE (type) != INTEGER_TYPE
14329 : 19682839 : || type == char8_type_node
14330 : 19682815 : || type == char16_type_node
14331 : 19682791 : || type == char32_type_node
14332 : 19682767 : || ((long_p || short_p)
14333 : 10659141 : && (explicit_char || explicit_intN)))
14334 : 418 : error_at (loc, "%qs specified with %qT", key, type);
14335 : 19682434 : else if (!explicit_int && !defaulted_int
14336 : 19682434 : && !explicit_char && !explicit_intN)
14337 : : {
14338 : 30 : if (typedef_decl)
14339 : : {
14340 : 27 : pedwarn (loc, OPT_Wpedantic,
14341 : : "%qs specified with typedef-name %qD",
14342 : : key, typedef_decl);
14343 : 27 : ok = !flag_pedantic_errors;
14344 : : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
14345 : : and is not built as a typedef. */
14346 : 27 : if (is_typedef_decl (typedef_decl))
14347 : 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
14348 : : }
14349 : 3 : else if (declspecs->decltype_p)
14350 : 0 : error_at (loc, "%qs specified with %<decltype%>", key);
14351 : : else
14352 : 3 : error_at (loc, "%qs specified with %<typeof%>", key);
14353 : : }
14354 : : else
14355 : : ok = 1;
14356 : :
14357 : : /* Discard the type modifiers if they are invalid. */
14358 : 514 : if (! ok)
14359 : : {
14360 : : unsigned_p = false;
14361 : : signed_p = false;
14362 : : long_p = false;
14363 : : short_p = false;
14364 : : longlong = 0;
14365 : : }
14366 : : }
14367 : :
14368 : : /* Decide whether an integer type is signed or not.
14369 : : Optionally treat bitfields as signed by default. */
14370 : 922610682 : if (unsigned_p
14371 : : /* [class.bit]
14372 : :
14373 : : It is implementation-defined whether a plain (neither
14374 : : explicitly signed or unsigned) char, short, int, or long
14375 : : bit-field is signed or unsigned.
14376 : :
14377 : : Naturally, we extend this to long long as well. Note that
14378 : : this does not include wchar_t. */
14379 : 922611172 : || (bitfield && !flag_signed_bitfields
14380 : 18 : && !signed_p
14381 : : /* A typedef for plain `int' without `signed' can be
14382 : : controlled just like plain `int', but a typedef for
14383 : : `signed int' cannot be so controlled. */
14384 : 18 : && !(typedef_decl
14385 : 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
14386 : 13 : && TREE_CODE (type) == INTEGER_TYPE
14387 : 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
14388 : : {
14389 : 13000103 : if (explicit_intN)
14390 : 454112 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
14391 : 12545991 : else if (longlong)
14392 : 1359697 : type = long_long_unsigned_type_node;
14393 : 11186294 : else if (long_p)
14394 : 2168570 : type = long_unsigned_type_node;
14395 : 9017724 : else if (short_p)
14396 : 1279112 : type = short_unsigned_type_node;
14397 : 7738612 : else if (type == char_type_node)
14398 : 1545047 : type = unsigned_char_type_node;
14399 : 6193565 : else if (typedef_decl)
14400 : 25 : type = c_common_unsigned_type (type);
14401 : : else
14402 : 6193540 : type = unsigned_type_node;
14403 : : }
14404 : 909611069 : else if (signed_p && type == char_type_node)
14405 : 669786 : type = signed_char_type_node;
14406 : 908941283 : else if (explicit_intN)
14407 : 503342 : type = int_n_trees[declspecs->int_n_idx].signed_type;
14408 : 908437941 : else if (longlong)
14409 : 2248909 : type = long_long_integer_type_node;
14410 : 906189032 : else if (long_p)
14411 : 3001299 : type = long_integer_type_node;
14412 : 903187733 : else if (short_p)
14413 : 601221 : type = short_integer_type_node;
14414 : 902586512 : else if (signed_p && typedef_decl)
14415 : 9 : type = c_common_signed_type (type);
14416 : :
14417 : 922611172 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
14418 : : {
14419 : 1423076 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
14420 : 3 : error_at (declspecs->locations[ds_complex],
14421 : : "complex invalid for %qs", name);
14422 : : /* If a modifier is specified, the resulting complex is the complex
14423 : : form of TYPE. E.g, "complex short" is "complex short int". */
14424 : 1423073 : else if (type == integer_type_node)
14425 : 164 : type = complex_integer_type_node;
14426 : 1422909 : else if (type == float_type_node)
14427 : 360017 : type = complex_float_type_node;
14428 : 1062892 : else if (type == double_type_node)
14429 : 360550 : type = complex_double_type_node;
14430 : 702342 : else if (type == long_double_type_node)
14431 : 360044 : type = complex_long_double_type_node;
14432 : : else
14433 : 342298 : type = build_complex_type (type);
14434 : : }
14435 : :
14436 : : /* If we're using the injected-class-name to form a compound type or a
14437 : : declaration, replace it with the underlying class so we don't get
14438 : : redundant typedefs in the debug output. But if we are returning the
14439 : : type unchanged, leave it alone so that it's available to
14440 : : maybe_get_template_decl_from_type_decl. */
14441 : 182864981 : if (CLASS_TYPE_P (type)
14442 : 182815291 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
14443 : 29473558 : && type == TREE_TYPE (TYPE_NAME (type))
14444 : 952084730 : && (declarator || type_quals))
14445 : 28267113 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
14446 : :
14447 : 922611172 : type_quals |= cp_type_quals (type);
14448 : 1845222344 : type = cp_build_qualified_type
14449 : 1845222344 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
14450 : 746956376 : || declspecs->decltype_p)
14451 : : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
14452 : : /* We might have ignored or rejected some of the qualifiers. */
14453 : 922611172 : type_quals = cp_type_quals (type);
14454 : :
14455 : 908222933 : if (cxx_dialect >= cxx17 && type && is_auto (type)
14456 : 13721283 : && innermost_code != cdk_function
14457 : : /* Placeholder in parm gets a better error below. */
14458 : 7989389 : && !(decl_context == PARM || decl_context == CATCHPARM)
14459 : 930557884 : && id_declarator && declarator != id_declarator)
14460 : 1000194 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
14461 : : {
14462 : 16 : auto_diagnostic_group g;
14463 : 16 : gcc_rich_location richloc (typespec_loc);
14464 : 16 : richloc.add_fixit_insert_after ("<>");
14465 : 16 : error_at (&richloc, "missing template argument list after %qE; "
14466 : : "for deduction, template placeholder must be followed "
14467 : : "by a simple declarator-id", tmpl);
14468 : 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
14469 : 16 : type = error_mark_node;
14470 : 16 : }
14471 : :
14472 : 922611172 : staticp = 0;
14473 : 922611172 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
14474 : 922611172 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
14475 : 922611172 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
14476 : :
14477 : 922611172 : storage_class = declspecs->storage_class;
14478 : 922611172 : if (storage_class == sc_static)
14479 : 19483297 : staticp = 1 + (decl_context == FIELD);
14480 : 903127875 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
14481 : : /* Treat class-scope deduction guides as static member functions
14482 : : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
14483 : 19160378 : staticp = 2;
14484 : :
14485 : 922611172 : if (virtualp)
14486 : : {
14487 : 4051173 : if (staticp == 2)
14488 : : {
14489 : 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14490 : 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
14491 : 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
14492 : : "and %<static%>", dname);
14493 : 21 : storage_class = sc_none;
14494 : 21 : staticp = 0;
14495 : 21 : }
14496 : 4051173 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14497 : : {
14498 : 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14499 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14500 : 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14501 : : "declared both %<virtual%> and %<constexpr%> only in "
14502 : : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14503 : 6 : }
14504 : : }
14505 : 922611172 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14506 : :
14507 : : /* Issue errors about use of storage classes for parameters. */
14508 : 922611172 : if (decl_context == PARM)
14509 : : {
14510 : 276954138 : if (typedef_p)
14511 : : {
14512 : 24 : error_at (declspecs->locations[ds_typedef],
14513 : : "typedef declaration invalid in parameter declaration");
14514 : 24 : return error_mark_node;
14515 : : }
14516 : 276954114 : else if (template_parm_flag && storage_class != sc_none)
14517 : : {
14518 : 16 : error_at (min_location (declspecs->locations[ds_thread],
14519 : : declspecs->locations[ds_storage_class]),
14520 : : "storage class specified for template parameter %qs",
14521 : : name);
14522 : 16 : return error_mark_node;
14523 : : }
14524 : 276954098 : else if (storage_class == sc_static
14525 : 276954098 : || storage_class == sc_extern
14526 : 276954095 : || thread_p)
14527 : : {
14528 : 9 : error_at (min_location (declspecs->locations[ds_thread],
14529 : : declspecs->locations[ds_storage_class]),
14530 : : "storage class specified for parameter %qs", name);
14531 : 9 : return error_mark_node;
14532 : : }
14533 : :
14534 : : /* Function parameters cannot be concept. */
14535 : 276954089 : if (concept_p)
14536 : : {
14537 : 9 : error_at (declspecs->locations[ds_concept],
14538 : : "a parameter cannot be declared %qs", "concept");
14539 : 9 : concept_p = 0;
14540 : 9 : constexpr_p = 0;
14541 : : }
14542 : : /* Function parameters cannot be constexpr. If we saw one, moan
14543 : : and pretend it wasn't there. */
14544 : 276954080 : else if (constexpr_p)
14545 : : {
14546 : 9 : error_at (declspecs->locations[ds_constexpr],
14547 : : "a parameter cannot be declared %qs", "constexpr");
14548 : 9 : constexpr_p = 0;
14549 : : }
14550 : 276954089 : if (constinit_p)
14551 : : {
14552 : 6 : error_at (declspecs->locations[ds_constinit],
14553 : : "a parameter cannot be declared %qs", "constinit");
14554 : 6 : constinit_p = 0;
14555 : : }
14556 : 276954089 : if (consteval_p)
14557 : : {
14558 : 3 : error_at (declspecs->locations[ds_consteval],
14559 : : "a parameter cannot be declared %qs", "consteval");
14560 : 3 : consteval_p = 0;
14561 : : }
14562 : : }
14563 : :
14564 : : /* Give error if `virtual' is used outside of class declaration. */
14565 : 922611123 : if (virtualp
14566 : 4051173 : && (current_class_name == NULL_TREE || decl_context != FIELD))
14567 : : {
14568 : 68 : error_at (declspecs->locations[ds_virtual],
14569 : : "%<virtual%> outside class declaration");
14570 : 68 : virtualp = 0;
14571 : : }
14572 : :
14573 : 922611123 : if (innermost_code == cdk_decomp)
14574 : : {
14575 : 76000 : location_t loc = (declarator->kind == cdk_reference
14576 : 86332 : ? declarator->declarator->id_loc : declarator->id_loc);
14577 : 86332 : if (inlinep)
14578 : 29 : error_at (declspecs->locations[ds_inline],
14579 : : "structured binding declaration cannot be %qs", "inline");
14580 : 86332 : if (typedef_p)
14581 : 29 : error_at (declspecs->locations[ds_typedef],
14582 : : "structured binding declaration cannot be %qs", "typedef");
14583 : 86332 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14584 : 96 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14585 : : "structured binding declaration can be %qs only with "
14586 : : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14587 : 86332 : if (consteval_p)
14588 : 13 : error_at (declspecs->locations[ds_consteval], "structured "
14589 : : "binding declaration cannot be %qs", "consteval");
14590 : 86332 : if (thread_p && cxx_dialect < cxx20)
14591 : 77 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14592 : : "structured binding declaration can be %qs only in "
14593 : : "%<-std=c++20%> or %<-std=gnu++20%>",
14594 : 77 : declspecs->gnu_thread_keyword_p
14595 : : ? "__thread" : "thread_local");
14596 : 86332 : if (concept_p)
14597 : : {
14598 : 0 : error_at (declspecs->locations[ds_concept],
14599 : : "structured binding declaration cannot be %qs", "concept");
14600 : 0 : constexpr_p = 0;
14601 : : }
14602 : : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
14603 : 86332 : if (type_quals & TYPE_QUAL_VOLATILE)
14604 : 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
14605 : : "%<volatile%>-qualified structured binding is deprecated");
14606 : 86332 : switch (storage_class)
14607 : : {
14608 : : case sc_none:
14609 : : break;
14610 : 28 : case sc_register:
14611 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14612 : : "register");
14613 : 28 : break;
14614 : 235 : case sc_static:
14615 : 235 : if (cxx_dialect < cxx20)
14616 : 186 : pedwarn (loc, OPT_Wc__20_extensions,
14617 : : "structured binding declaration can be %qs only in "
14618 : : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
14619 : : break;
14620 : 29 : case sc_extern:
14621 : 29 : error_at (loc, "structured binding declaration cannot be %qs",
14622 : : "extern");
14623 : 29 : break;
14624 : 28 : case sc_mutable:
14625 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14626 : : "mutable");
14627 : 28 : break;
14628 : 0 : case sc_auto:
14629 : 0 : error_at (loc, "structured binding declaration cannot be "
14630 : : "C++98 %<auto%>");
14631 : 0 : break;
14632 : 0 : default:
14633 : 0 : gcc_unreachable ();
14634 : : }
14635 : 86332 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
14636 : 86332 : || TYPE_IDENTIFIER (type) != auto_identifier)
14637 : : {
14638 : 3 : if (type != error_mark_node)
14639 : : {
14640 : 3 : auto_diagnostic_group d;
14641 : 3 : error_at (loc, "structured binding declaration cannot have "
14642 : : "type %qT", type);
14643 : 3 : inform (loc,
14644 : : "type must be cv-qualified %<auto%> or reference to "
14645 : : "cv-qualified %<auto%>");
14646 : 3 : }
14647 : 3 : type = build_qualified_type (make_auto (), type_quals);
14648 : 3 : declspecs->type = type;
14649 : : }
14650 : 86329 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
14651 : 51 : pedwarn (loc, OPT_Wpedantic,
14652 : : "structured binding declaration cannot have constrained "
14653 : : "%<auto%> type %qT", type);
14654 : 86332 : inlinep = 0;
14655 : 86332 : typedef_p = 0;
14656 : 86332 : consteval_p = 0;
14657 : 86332 : concept_p = 0;
14658 : 86332 : if (storage_class != sc_static)
14659 : : {
14660 : 86097 : storage_class = sc_none;
14661 : 86097 : declspecs->storage_class = sc_none;
14662 : : }
14663 : : }
14664 : :
14665 : : /* Static anonymous unions are dealt with here. */
14666 : 922611123 : if (staticp && decl_context == TYPENAME
14667 : 0 : && declspecs->type
14668 : 922611123 : && ANON_AGGR_TYPE_P (declspecs->type))
14669 : : decl_context = FIELD;
14670 : :
14671 : : /* Warn about storage classes that are invalid for certain
14672 : : kinds of declarations (parameters, typenames, etc.). */
14673 : 922611123 : if (thread_p
14674 : 21173 : && ((storage_class
14675 : 21173 : && storage_class != sc_extern
14676 : 524 : && storage_class != sc_static)
14677 : 21162 : || typedef_p))
14678 : : {
14679 : 17 : location_t loc
14680 : 17 : = min_location (declspecs->locations[ds_thread],
14681 : : declspecs->locations[ds_storage_class]);
14682 : 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
14683 : 17 : thread_p = false;
14684 : : }
14685 : 922611123 : if (decl_context != NORMAL
14686 : 767121848 : && ((storage_class != sc_none
14687 : 767121848 : && storage_class != sc_mutable)
14688 : 747961482 : || thread_p))
14689 : : {
14690 : 19160369 : if ((decl_context == PARM || decl_context == CATCHPARM)
14691 : 36 : && (storage_class == sc_register
14692 : 36 : || storage_class == sc_auto))
14693 : : ;
14694 : 19160333 : else if (typedef_p)
14695 : : ;
14696 : 19160333 : else if (decl_context == FIELD
14697 : : /* C++ allows static class elements. */
14698 : 19160333 : && storage_class == sc_static)
14699 : : /* C++ also allows inlines and signed and unsigned elements,
14700 : : but in those cases we don't come in here. */
14701 : : ;
14702 : : else
14703 : : {
14704 : 15 : location_t loc
14705 : 15 : = min_location (declspecs->locations[ds_thread],
14706 : : declspecs->locations[ds_storage_class]);
14707 : 15 : if (decl_context == FIELD)
14708 : 15 : error_at (loc, "storage class specified for %qs", name);
14709 : 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
14710 : 0 : error_at (loc, "storage class specified for parameter %qs", name);
14711 : : else
14712 : 0 : error_at (loc, "storage class specified for typename");
14713 : 15 : if (storage_class == sc_register
14714 : 15 : || storage_class == sc_auto
14715 : 15 : || storage_class == sc_extern
14716 : 3 : || thread_p)
14717 : 15 : storage_class = sc_none;
14718 : : }
14719 : : }
14720 : 903450754 : else if (storage_class == sc_extern && funcdef_flag
14721 : 903450754 : && ! toplevel_bindings_p ())
14722 : 0 : error ("nested function %qs declared %<extern%>", name);
14723 : 903450754 : else if (toplevel_bindings_p ())
14724 : : {
14725 : 416804544 : if (storage_class == sc_auto)
14726 : 2 : error_at (declspecs->locations[ds_storage_class],
14727 : : "top-level declaration of %qs specifies %<auto%>", name);
14728 : : }
14729 : 486646210 : else if (thread_p
14730 : 486646210 : && storage_class != sc_extern
14731 : 223 : && storage_class != sc_static)
14732 : : {
14733 : 190 : if (declspecs->gnu_thread_keyword_p)
14734 : 49 : pedwarn (declspecs->locations[ds_thread],
14735 : 49 : 0, "function-scope %qs implicitly auto and "
14736 : : "declared %<__thread%>", name);
14737 : :
14738 : : /* When thread_local is applied to a variable of block scope the
14739 : : storage-class-specifier static is implied if it does not appear
14740 : : explicitly. */
14741 : 190 : storage_class = declspecs->storage_class = sc_static;
14742 : 190 : staticp = 1;
14743 : : }
14744 : :
14745 : 922611123 : if (storage_class && friendp)
14746 : : {
14747 : 12 : error_at (min_location (declspecs->locations[ds_thread],
14748 : : declspecs->locations[ds_storage_class]),
14749 : : "storage class specifiers invalid in friend function "
14750 : : "declarations");
14751 : 12 : storage_class = sc_none;
14752 : 12 : staticp = 0;
14753 : : }
14754 : :
14755 : 922611123 : if (!id_declarator)
14756 : : unqualified_id = NULL_TREE;
14757 : : else
14758 : : {
14759 : 535342534 : unqualified_id = id_declarator->u.id.unqualified_name;
14760 : 535342534 : switch (TREE_CODE (unqualified_id))
14761 : : {
14762 : 3164135 : case BIT_NOT_EXPR:
14763 : 3164135 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
14764 : 3164135 : if (TYPE_P (unqualified_id))
14765 : 3164132 : unqualified_id = constructor_name (unqualified_id);
14766 : : break;
14767 : :
14768 : : case IDENTIFIER_NODE:
14769 : : case TEMPLATE_ID_EXPR:
14770 : : break;
14771 : :
14772 : 0 : default:
14773 : 0 : gcc_unreachable ();
14774 : : }
14775 : : }
14776 : :
14777 : 922611123 : if (declspecs->std_attributes
14778 : 922611123 : && !diagnose_misapplied_contracts (declspecs->std_attributes))
14779 : : {
14780 : 705 : location_t attr_loc = declspecs->locations[ds_std_attribute];
14781 : 705 : auto_diagnostic_group d;
14782 : 705 : if (any_nonignored_attribute_p (declspecs->std_attributes)
14783 : 705 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
14784 : 702 : inform (attr_loc, "an attribute that appertains to a type-specifier "
14785 : : "is ignored");
14786 : 705 : }
14787 : :
14788 : 922611123 : if (attrlist)
14789 : 910386287 : diagnose_misapplied_contracts (*attrlist);
14790 : :
14791 : : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
14792 : : bool is_xobj_member_function = false;
14793 : : /* Determine the type of the entity declared by recurring on the
14794 : : declarator. */
14795 : 1282013768 : for (; declarator; declarator = declarator->declarator)
14796 : : {
14797 : 894831536 : const cp_declarator *inner_declarator;
14798 : 894831536 : tree attrs;
14799 : :
14800 : 894831536 : if (type == error_mark_node)
14801 : 599 : return error_mark_node;
14802 : :
14803 : 894831210 : attrs = declarator->attributes;
14804 : 894831210 : if (attrs)
14805 : : {
14806 : 13015 : int attr_flags;
14807 : :
14808 : 13015 : attr_flags = 0;
14809 : 13015 : if (declarator->kind == cdk_id)
14810 : 12341 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
14811 : 13015 : if (declarator->kind == cdk_function)
14812 : 657 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
14813 : 13015 : if (declarator->kind == cdk_array)
14814 : 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
14815 : 13015 : tree late_attrs = NULL_TREE;
14816 : 13015 : if (decl_context != PARM && decl_context != TYPENAME)
14817 : : /* Assume that any attributes that get applied late to
14818 : : templates will DTRT when applied to the declaration
14819 : : as a whole. */
14820 : 13003 : late_attrs = splice_template_attributes (&attrs, type);
14821 : 13015 : returned_attrs = decl_attributes (&type,
14822 : : attr_chainon (returned_attrs,
14823 : : attrs),
14824 : : attr_flags);
14825 : 13015 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
14826 : : }
14827 : :
14828 : 894831210 : inner_declarator = declarator->declarator;
14829 : :
14830 : : /* Check that contracts aren't misapplied. */
14831 : 894831210 : if (tree contract_attr = find_contract (declarator->std_attributes))
14832 : 484 : if (declarator->kind != cdk_function
14833 : 479 : || innermost_code != cdk_function)
14834 : 7 : diagnose_misapplied_contracts (contract_attr);
14835 : :
14836 : : /* We don't want to warn in parameter context because we don't
14837 : : yet know if the parse will succeed, and this might turn out
14838 : : to be a constructor call. */
14839 : 894831210 : if (decl_context != PARM
14840 : 894831210 : && decl_context != TYPENAME
14841 : 514523965 : && !typedef_p
14842 : 469263524 : && declarator->parenthesized != UNKNOWN_LOCATION
14843 : : /* If the type is class-like and the inner name used a
14844 : : global namespace qualifier, we need the parens.
14845 : : Unfortunately all we can tell is whether a qualified name
14846 : : was used or not. */
14847 : 894831391 : && !(inner_declarator
14848 : 126 : && inner_declarator->kind == cdk_id
14849 : 108 : && inner_declarator->u.id.qualifying_scope
14850 : 18 : && (MAYBE_CLASS_TYPE_P (type)
14851 : 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
14852 : : {
14853 : 169 : auto_diagnostic_group d;
14854 : 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
14855 : : "unnecessary parentheses in declaration of %qs",
14856 : : name))
14857 : : {
14858 : 42 : gcc_rich_location iloc (declarator->parenthesized);
14859 : 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
14860 : 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
14861 : 42 : inform (&iloc, "remove parentheses");
14862 : 42 : }
14863 : 169 : }
14864 : 894831210 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
14865 : : break;
14866 : :
14867 : 359402918 : switch (declarator->kind)
14868 : : {
14869 : 3164709 : case cdk_array:
14870 : 6329418 : type = create_array_type_for_decl (dname, type,
14871 : 3164709 : declarator->u.array.bounds,
14872 : 3164709 : declarator->id_loc);
14873 : 3164709 : if (!valid_array_size_p (dname
14874 : : ? declarator->id_loc : input_location,
14875 : : type, dname))
14876 : 267 : type = error_mark_node;
14877 : :
14878 : 3164709 : if (declarator->std_attributes)
14879 : : /* [dcl.array]/1:
14880 : :
14881 : : The optional attribute-specifier-seq appertains to the
14882 : : array type. */
14883 : 189 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
14884 : : break;
14885 : :
14886 : 176427643 : case cdk_function:
14887 : 176427643 : {
14888 : 176427643 : tree arg_types;
14889 : 176427643 : int funcdecl_p;
14890 : :
14891 : : /* Declaring a function type. */
14892 : :
14893 : : /* Pick up type qualifiers which should be applied to `this'. */
14894 : 176427643 : memfn_quals = declarator->u.function.qualifiers;
14895 : : /* Pick up virt-specifiers. */
14896 : 176427643 : virt_specifiers = declarator->u.function.virt_specifiers;
14897 : : /* And ref-qualifier, too */
14898 : 176427643 : rqual = declarator->u.function.ref_qualifier;
14899 : : /* And tx-qualifier. */
14900 : 176427643 : tree tx_qual = declarator->u.function.tx_qualifier;
14901 : : /* Pick up the exception specifications. */
14902 : 176427643 : raises = declarator->u.function.exception_specification;
14903 : : /* If the exception-specification is ill-formed, let's pretend
14904 : : there wasn't one. */
14905 : 176427643 : if (raises == error_mark_node)
14906 : 9 : raises = NULL_TREE;
14907 : :
14908 : 352855286 : auto find_xobj_parm = [](tree parm_list)
14909 : : {
14910 : : /* There is no need to iterate over the list,
14911 : : only the first parm can be a valid xobj parm. */
14912 : 352624779 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
14913 : : return NULL_TREE;
14914 : : /* If we make it here, we are looking at an xobj parm.
14915 : :
14916 : : Non-null 'purpose' usually means the parm has a default
14917 : : argument, we don't want to violate this assumption. */
14918 : 60418 : TREE_PURPOSE (parm_list) = NULL_TREE;
14919 : 60418 : return TREE_VALUE (parm_list);
14920 : : };
14921 : :
14922 : 176427643 : tree xobj_parm
14923 : 176427643 : = find_xobj_parm (declarator->u.function.parameters);
14924 : 176427643 : is_xobj_member_function = xobj_parm;
14925 : :
14926 : 176427643 : if (xobj_parm && cxx_dialect < cxx23)
14927 : 24 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
14928 : : "explicit object member function only available "
14929 : : "with %<-std=c++23%> or %<-std=gnu++23%>");
14930 : :
14931 : 176427643 : if (xobj_parm && decl_context == TYPENAME)
14932 : : {
14933 : : /* We inform in every case, just differently depending on what
14934 : : case it is. */
14935 : 6 : auto_diagnostic_group d;
14936 : 6 : bool ptr_type = true;
14937 : : /* If declarator->kind is cdk_function and we are at the end of
14938 : : the declarator chain, we are looking at a function type. */
14939 : 6 : if (!declarator->declarator)
14940 : : {
14941 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14942 : : "a function type cannot "
14943 : : "have an explicit object parameter");
14944 : 2 : ptr_type = false;
14945 : : }
14946 : 4 : else if (declarator->declarator->kind == cdk_pointer)
14947 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14948 : : "a pointer to function type cannot "
14949 : : "have an explicit object parameter");
14950 : 2 : else if (declarator->declarator->kind == cdk_ptrmem)
14951 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14952 : : "a pointer to member function type "
14953 : : "cannot have an explicit object parameter");
14954 : : else
14955 : 0 : gcc_unreachable ();
14956 : :
14957 : : /* The locations being used here are probably not correct. */
14958 : 4 : if (ptr_type)
14959 : 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14960 : : "the type of a pointer to explicit object member "
14961 : : "function is a regular pointer to function type");
14962 : : else
14963 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14964 : : "the type of an explicit object "
14965 : : "member function is a regular function type");
14966 : : /* Ideally we should synthesize the correct syntax
14967 : : for the user, perhaps this could be added later. */
14968 : 6 : }
14969 : : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
14970 : : the first parm node will never erroneously be detected here. */
14971 : 176427643 : {
14972 : 176427643 : auto_diagnostic_group d;
14973 : 176427643 : bool bad_xobj_parm_encountered = false;
14974 : 176427643 : for (tree parm = declarator->u.function.parameters;
14975 : 441964031 : parm && parm != void_list_node;
14976 : 265536388 : parm = TREE_CHAIN (parm))
14977 : : {
14978 : 265536388 : if (TREE_PURPOSE (parm) != this_identifier)
14979 : 265536382 : continue;
14980 : 6 : bad_xobj_parm_encountered = true;
14981 : 6 : TREE_PURPOSE (parm) = NULL_TREE;
14982 : 6 : gcc_rich_location bad_xobj_parm
14983 : 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
14984 : 6 : error_at (&bad_xobj_parm,
14985 : : "only the first parameter of a member function "
14986 : : "can be declared as an explicit object parameter");
14987 : 6 : }
14988 : 176427643 : if (bad_xobj_parm_encountered && xobj_parm)
14989 : 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14990 : : "valid explicit object parameter declared here");
14991 : 176427643 : }
14992 : :
14993 : 176427643 : if (reqs)
14994 : 3 : error_at (location_of (reqs), "requires-clause on return type");
14995 : 176427643 : reqs = declarator->u.function.requires_clause;
14996 : :
14997 : : /* Say it's a definition only for the CALL_EXPR
14998 : : closest to the identifier. */
14999 : 176427643 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
15000 : :
15001 : : /* Handle a late-specified return type. */
15002 : 176427643 : tree late_return_type = declarator->u.function.late_return_type;
15003 : 176427643 : if (tree auto_node = type_uses_auto (type))
15004 : : {
15005 : 5743420 : if (!late_return_type)
15006 : : {
15007 : 2666553 : if (!funcdecl_p)
15008 : : /* auto (*fp)() = f; is OK. */;
15009 : 2666521 : else if (current_class_type
15010 : 4986979 : && LAMBDA_TYPE_P (current_class_type))
15011 : : /* OK for C++11 lambdas. */;
15012 : 1613618 : else if (cxx_dialect < cxx14)
15013 : : {
15014 : 7 : auto_diagnostic_group d;
15015 : 7 : error_at (typespec_loc, "%qs function uses "
15016 : : "%<auto%> type specifier without "
15017 : : "trailing return type", name);
15018 : 7 : inform (typespec_loc,
15019 : : "deduced return type only available "
15020 : : "with %<-std=c++14%> or %<-std=gnu++14%>");
15021 : 7 : }
15022 : 1613611 : else if (virtualp)
15023 : : {
15024 : 6 : error_at (typespec_loc, "virtual function "
15025 : : "cannot have deduced return type");
15026 : 6 : virtualp = false;
15027 : : }
15028 : : }
15029 : 3076867 : else if (!is_auto (type) && sfk != sfk_conversion)
15030 : : {
15031 : 15 : error_at (typespec_loc, "%qs function with trailing "
15032 : : "return type has %qT as its type rather "
15033 : : "than plain %<auto%>", name, type);
15034 : 270 : return error_mark_node;
15035 : : }
15036 : 3076852 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
15037 : : {
15038 : 9 : if (funcdecl_p)
15039 : 3 : error_at (typespec_loc,
15040 : : "%qs function with trailing return type "
15041 : : "has %<decltype(auto)%> as its type "
15042 : : "rather than plain %<auto%>", name);
15043 : : else
15044 : 6 : error_at (typespec_loc,
15045 : : "invalid use of %<decltype(auto)%>");
15046 : 9 : return error_mark_node;
15047 : : }
15048 : 3076843 : else if (is_constrained_auto (type))
15049 : : {
15050 : 6 : if (funcdecl_p)
15051 : 3 : error_at (typespec_loc,
15052 : : "%qs function with trailing return type "
15053 : : "has constrained %<auto%> type specifier "
15054 : : "rather than plain %<auto%>",
15055 : : name);
15056 : : else
15057 : 3 : error_at (typespec_loc,
15058 : : "invalid use of constrained %<auto%> type");
15059 : 6 : return error_mark_node;
15060 : : }
15061 : 5743390 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
15062 : 5743390 : if (!tmpl)
15063 : 4666136 : if (tree late_auto = type_uses_auto (late_return_type))
15064 : 26214 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
15065 : 4666136 : if (tmpl)
15066 : : {
15067 : 1077258 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
15068 : : {
15069 : 10 : auto_diagnostic_group g;
15070 : 10 : error_at (typespec_loc, "deduced class "
15071 : : "type %qD in function return type",
15072 : 10 : DECL_NAME (tmpl));
15073 : 10 : inform (DECL_SOURCE_LOCATION (tmpl),
15074 : : "%qD declared here", tmpl);
15075 : 10 : return error_mark_node;
15076 : 10 : }
15077 : 1077248 : else if (!late_return_type)
15078 : : {
15079 : 3 : auto_diagnostic_group d;
15080 : 3 : error_at (declarator->id_loc, "deduction guide "
15081 : : "for %qT must have trailing return "
15082 : 3 : "type", TREE_TYPE (tmpl));
15083 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15084 : : "%qD declared here", tmpl);
15085 : 3 : return error_mark_node;
15086 : 3 : }
15087 : 1077245 : else if (CLASS_TYPE_P (late_return_type)
15088 : 1077245 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
15089 : 2154490 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
15090 : : == tmpl))
15091 : : /* OK */;
15092 : : else
15093 : 0 : error ("trailing return type %qT of deduction guide "
15094 : : "is not a specialization of %qT",
15095 : 0 : late_return_type, TREE_TYPE (tmpl));
15096 : : }
15097 : : }
15098 : 170684223 : else if (late_return_type
15099 : 170684223 : && sfk != sfk_conversion)
15100 : : {
15101 : 37 : if (late_return_type == error_mark_node)
15102 : : return error_mark_node;
15103 : 37 : if (cxx_dialect < cxx11)
15104 : : /* Not using maybe_warn_cpp0x because this should
15105 : : always be an error. */
15106 : 1 : error_at (typespec_loc,
15107 : : "trailing return type only available "
15108 : : "with %<-std=c++11%> or %<-std=gnu++11%>");
15109 : : else
15110 : 36 : error_at (typespec_loc, "%qs function with trailing "
15111 : : "return type not declared with %<auto%> "
15112 : : "type specifier", name);
15113 : 37 : return error_mark_node;
15114 : : }
15115 : 176427563 : if (late_return_type && sfk == sfk_conversion)
15116 : : {
15117 : 14 : error ("a conversion function cannot have a trailing return type");
15118 : 14 : return error_mark_node;
15119 : : }
15120 : 176427549 : type = splice_late_return_type (type, late_return_type);
15121 : 176427549 : if (type == error_mark_node)
15122 : : return error_mark_node;
15123 : :
15124 : 176427473 : if (late_return_type)
15125 : : {
15126 : 3076749 : late_return_type_p = true;
15127 : 3076749 : type_quals = cp_type_quals (type);
15128 : : }
15129 : :
15130 : 176427473 : if (type_quals != TYPE_UNQUALIFIED)
15131 : : {
15132 : : /* It's wrong, for instance, to issue a -Wignored-qualifiers
15133 : : warning for
15134 : : static_assert(!is_same_v<void(*)(), const void(*)()>);
15135 : : because there the qualifier matters. */
15136 : 12330 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
15137 : 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
15138 : : "qualifiers ignored on function return type");
15139 : : /* [dcl.fct] "A volatile-qualified return type is
15140 : : deprecated." */
15141 : 12330 : if (type_quals & TYPE_QUAL_VOLATILE)
15142 : 599 : warning_at (typespec_loc, OPT_Wvolatile,
15143 : : "%<volatile%>-qualified return type is "
15144 : : "deprecated");
15145 : :
15146 : : /* We now know that the TYPE_QUALS don't apply to the
15147 : : decl, but to its return type. */
15148 : : type_quals = TYPE_UNQUALIFIED;
15149 : : }
15150 : :
15151 : : /* Error about some types functions can't return. */
15152 : :
15153 : 176427473 : if (TREE_CODE (type) == FUNCTION_TYPE)
15154 : : {
15155 : 30 : error_at (typespec_loc, "%qs declared as function returning "
15156 : : "a function", name);
15157 : 30 : return error_mark_node;
15158 : : }
15159 : 176427443 : if (TREE_CODE (type) == ARRAY_TYPE)
15160 : : {
15161 : 6 : error_at (typespec_loc, "%qs declared as function returning "
15162 : : "an array", name);
15163 : 6 : return error_mark_node;
15164 : : }
15165 : 176427437 : if (constinit_p && funcdecl_p)
15166 : : {
15167 : 15 : error_at (declspecs->locations[ds_constinit],
15168 : : "%<constinit%> on function return type is not "
15169 : : "allowed");
15170 : 15 : return error_mark_node;
15171 : : }
15172 : :
15173 : 176427422 : if (check_decltype_auto (typespec_loc, type))
15174 : 49 : return error_mark_node;
15175 : :
15176 : 176427373 : if (ctype == NULL_TREE
15177 : 176427373 : && decl_context == FIELD
15178 : : && funcdecl_p
15179 : 95247350 : && friendp == 0)
15180 : 90690459 : ctype = current_class_type;
15181 : :
15182 : 176427373 : if (ctype && (sfk == sfk_constructor
15183 : 90690459 : || sfk == sfk_destructor))
15184 : : {
15185 : : /* We are within a class's scope. If our declarator name
15186 : : is the same as the class name, and we are defining
15187 : : a function, then it is a constructor/destructor, and
15188 : : therefore returns a void type. */
15189 : :
15190 : : /* ISO C++ 12.4/2. A destructor may not be declared
15191 : : const or volatile. A destructor may not be static.
15192 : : A destructor may not be declared with ref-qualifier.
15193 : :
15194 : : ISO C++ 12.1. A constructor may not be declared
15195 : : const or volatile. A constructor may not be
15196 : : virtual. A constructor may not be static.
15197 : : A constructor may not be declared with ref-qualifier. */
15198 : 24422167 : if (staticp == 2)
15199 : 9 : error_at (declspecs->locations[ds_storage_class],
15200 : : (flags == DTOR_FLAG)
15201 : : ? G_("destructor cannot be static member "
15202 : : "function")
15203 : : : G_("constructor cannot be static member "
15204 : : "function"));
15205 : 24422167 : if (memfn_quals)
15206 : : {
15207 : 9 : error ((flags == DTOR_FLAG)
15208 : : ? G_("destructors may not be cv-qualified")
15209 : : : G_("constructors may not be cv-qualified"));
15210 : 9 : memfn_quals = TYPE_UNQUALIFIED;
15211 : : }
15212 : :
15213 : 24422167 : if (rqual)
15214 : : {
15215 : 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
15216 : 6 : error ((flags == DTOR_FLAG)
15217 : : ? G_("destructors may not be ref-qualified")
15218 : : : G_("constructors may not be ref-qualified"));
15219 : 6 : rqual = REF_QUAL_NONE;
15220 : : }
15221 : :
15222 : 24422167 : if (decl_context == FIELD
15223 : 24422167 : && !member_function_or_else (ctype,
15224 : : current_class_type,
15225 : : flags))
15226 : 0 : return error_mark_node;
15227 : :
15228 : 24422167 : if (flags != DTOR_FLAG)
15229 : : {
15230 : : /* It's a constructor. */
15231 : 21430842 : if (explicitp == 1)
15232 : 5101148 : explicitp = 2;
15233 : 21430842 : if (virtualp)
15234 : : {
15235 : 3 : permerror (declspecs->locations[ds_virtual],
15236 : : "constructors cannot be declared %<virtual%>");
15237 : 3 : virtualp = 0;
15238 : : }
15239 : 21430842 : if (decl_context == FIELD
15240 : 21430842 : && sfk != sfk_constructor)
15241 : 0 : return error_mark_node;
15242 : : }
15243 : 24422167 : if (decl_context == FIELD)
15244 : 24422167 : staticp = 0;
15245 : : }
15246 : 152005206 : else if (friendp)
15247 : : {
15248 : 4246590 : if (virtualp)
15249 : : {
15250 : : /* Cannot be both friend and virtual. */
15251 : 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
15252 : 12 : richloc.add_range (declspecs->locations[ds_friend]);
15253 : 12 : error_at (&richloc, "virtual functions cannot be friends");
15254 : 12 : friendp = 0;
15255 : 12 : }
15256 : 4246590 : if (decl_context == NORMAL)
15257 : 0 : error_at (declarator->id_loc,
15258 : : "friend declaration not in class definition");
15259 : 4246590 : if (current_function_decl && funcdef_flag)
15260 : : {
15261 : 9 : error_at (declarator->id_loc,
15262 : : "cannot define friend function %qs in a local "
15263 : : "class definition", name);
15264 : 9 : friendp = 0;
15265 : : }
15266 : : /* [class.friend]/6: A function can be defined in a friend
15267 : : declaration if the function name is unqualified. */
15268 : 4246590 : if (funcdef_flag && in_namespace)
15269 : : {
15270 : 6 : if (in_namespace == global_namespace)
15271 : 3 : error_at (declarator->id_loc,
15272 : : "friend function definition %qs cannot have "
15273 : : "a name qualified with %<::%>", name);
15274 : : else
15275 : 3 : error_at (declarator->id_loc,
15276 : : "friend function definition %qs cannot have "
15277 : : "a name qualified with %<%D::%>", name,
15278 : : in_namespace);
15279 : : }
15280 : : }
15281 : 147758616 : else if (ctype && sfk == sfk_conversion)
15282 : : {
15283 : 632177 : if (explicitp == 1)
15284 : : {
15285 : 302800 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
15286 : 302800 : explicitp = 2;
15287 : : }
15288 : : }
15289 : 147126439 : else if (sfk == sfk_deduction_guide)
15290 : : {
15291 : 1077245 : if (explicitp == 1)
15292 : 176427373 : explicitp = 2;
15293 : : }
15294 : :
15295 : 176427373 : if (xobj_parm)
15296 : : {
15297 : 60418 : if (!ctype
15298 : 60418 : && decl_context == NORMAL
15299 : 49 : && (in_namespace
15300 : 49 : || !declarator->declarator->u.id.qualifying_scope))
15301 : 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15302 : : "a non-member function cannot have "
15303 : : "an explicit object parameter");
15304 : : else
15305 : : {
15306 : 60414 : if (virtualp)
15307 : : {
15308 : 16 : auto_diagnostic_group d;
15309 : 16 : error_at (declspecs->locations[ds_virtual],
15310 : : "an explicit object member function cannot "
15311 : : "be %<virtual%>");
15312 : 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15313 : : "explicit object parameter declared here");
15314 : 16 : virtualp = false;
15315 : 16 : }
15316 : 60414 : if (staticp >= 2)
15317 : : {
15318 : 2 : auto_diagnostic_group d;
15319 : 2 : error_at (declspecs->locations[ds_storage_class],
15320 : : "an explicit object member function cannot "
15321 : : "be %<static%>");
15322 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
15323 : : "explicit object parameter declared here");
15324 : 2 : }
15325 : 60414 : if (unqualified_id
15326 : 176487781 : && identifier_p (unqualified_id)
15327 : 120822 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15328 : 8 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
15329 : : "%qD cannot be an explicit object member "
15330 : : "function", unqualified_id);
15331 : : }
15332 : : }
15333 : 176427373 : tree pushed_scope = NULL_TREE;
15334 : 176427373 : if (funcdecl_p
15335 : 176427373 : && decl_context != FIELD
15336 : 79154691 : && inner_declarator->u.id.qualifying_scope
15337 : 185572400 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
15338 : 9143701 : pushed_scope
15339 : 9143701 : = push_scope (inner_declarator->u.id.qualifying_scope);
15340 : :
15341 : 176427373 : arg_types = grokparms (declarator->u.function.parameters, &parms);
15342 : :
15343 : 176427373 : if (pushed_scope)
15344 : 9143701 : pop_scope (pushed_scope);
15345 : :
15346 : 176427373 : if (inner_declarator
15347 : 175955279 : && inner_declarator->kind == cdk_id
15348 : 174091740 : && inner_declarator->u.id.sfk == sfk_destructor
15349 : 3164126 : && arg_types != void_list_node)
15350 : : {
15351 : 20 : error_at (declarator->id_loc,
15352 : : "destructors may not have parameters");
15353 : 20 : arg_types = void_list_node;
15354 : 20 : parms = NULL_TREE;
15355 : 20 : is_xobj_member_function = false;
15356 : : }
15357 : :
15358 : 176427373 : type = cp_build_function_type (type, arg_types);
15359 : :
15360 : 176427373 : tree attrs = declarator->std_attributes;
15361 : 176427373 : if (tx_qual)
15362 : : {
15363 : 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
15364 : : /* transaction_safe applies to the type, but
15365 : : transaction_safe_dynamic applies to the function. */
15366 : 242 : if (is_attribute_p ("transaction_safe", tx_qual))
15367 : 213 : attrs = attr_chainon (attrs, att);
15368 : : else
15369 : 29 : returned_attrs = attr_chainon (returned_attrs, att);
15370 : : }
15371 : :
15372 : : /* Actually apply the contract attributes to the declaration. */
15373 : 176428539 : for (tree *p = &attrs; *p;)
15374 : : {
15375 : 1166 : tree l = *p;
15376 : 1166 : if (cxx_contract_attribute_p (l))
15377 : : {
15378 : 667 : *p = TREE_CHAIN (l);
15379 : : /* Intentionally reverse order of contracts so they're
15380 : : reversed back into their lexical order. */
15381 : 667 : TREE_CHAIN (l) = NULL_TREE;
15382 : 667 : returned_attrs = chainon (l, returned_attrs);
15383 : : }
15384 : : else
15385 : 499 : p = &TREE_CHAIN (l);
15386 : : }
15387 : :
15388 : 176427373 : if (attrs)
15389 : : /* [dcl.fct]/2:
15390 : :
15391 : : The optional attribute-specifier-seq appertains to
15392 : : the function type. */
15393 : 487 : cplus_decl_attributes (&type, attrs, 0);
15394 : :
15395 : 176427373 : if (raises)
15396 : 69600791 : type = build_exception_variant (type, raises);
15397 : : }
15398 : 176427373 : break;
15399 : :
15400 : 179810566 : case cdk_pointer:
15401 : 179810566 : case cdk_reference:
15402 : 179810566 : case cdk_ptrmem:
15403 : : /* Filter out pointers-to-references and references-to-references.
15404 : : We can get these if a TYPE_DECL is used. */
15405 : :
15406 : 179810566 : if (TYPE_REF_P (type))
15407 : : {
15408 : 238 : if (declarator->kind != cdk_reference)
15409 : : {
15410 : 3 : error ("cannot declare pointer to %q#T", type);
15411 : 3 : type = TREE_TYPE (type);
15412 : : }
15413 : :
15414 : : /* In C++0x, we allow reference to reference declarations
15415 : : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
15416 : : and template type arguments [14.3.1/4 temp.arg.type]. The
15417 : : check for direct reference to reference declarations, which
15418 : : are still forbidden, occurs below. Reasoning behind the change
15419 : : can be found in DR106, DR540, and the rvalue reference
15420 : : proposals. */
15421 : 235 : else if (cxx_dialect == cxx98)
15422 : : {
15423 : 0 : error ("cannot declare reference to %q#T", type);
15424 : 0 : type = TREE_TYPE (type);
15425 : : }
15426 : : }
15427 : 179810328 : else if (VOID_TYPE_P (type))
15428 : : {
15429 : 4660441 : if (declarator->kind == cdk_reference)
15430 : 4 : error ("cannot declare reference to %q#T", type);
15431 : 4660437 : else if (declarator->kind == cdk_ptrmem)
15432 : 3 : error ("cannot declare pointer to %q#T member", type);
15433 : : }
15434 : :
15435 : : /* We now know that the TYPE_QUALS don't apply to the decl,
15436 : : but to the target of the pointer. */
15437 : 179810566 : type_quals = TYPE_UNQUALIFIED;
15438 : :
15439 : : /* This code used to handle METHOD_TYPE, but I don't think it's
15440 : : possible to get it here anymore. */
15441 : 179810566 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15442 : 179810566 : if (declarator->kind == cdk_ptrmem
15443 : 1113830 : && TREE_CODE (type) == FUNCTION_TYPE)
15444 : : {
15445 : 917196 : memfn_quals |= type_memfn_quals (type);
15446 : 1834392 : type = build_memfn_type (type,
15447 : 917196 : declarator->u.pointer.class_type,
15448 : : memfn_quals,
15449 : : rqual);
15450 : 917196 : if (type == error_mark_node)
15451 : : return error_mark_node;
15452 : :
15453 : : rqual = REF_QUAL_NONE;
15454 : : memfn_quals = TYPE_UNQUALIFIED;
15455 : : }
15456 : :
15457 : 179810563 : if (TREE_CODE (type) == FUNCTION_TYPE
15458 : 179810563 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15459 : 1004492 : || type_memfn_rqual (type) != REF_QUAL_NONE))
15460 : 15 : error (declarator->kind == cdk_reference
15461 : : ? G_("cannot declare reference to qualified function type %qT")
15462 : : : G_("cannot declare pointer to qualified function type %qT"),
15463 : : type);
15464 : :
15465 : : /* When the pointed-to type involves components of variable size,
15466 : : care must be taken to ensure that the size evaluation code is
15467 : : emitted early enough to dominate all the possible later uses
15468 : : and late enough for the variables on which it depends to have
15469 : : been assigned.
15470 : :
15471 : : This is expected to happen automatically when the pointed-to
15472 : : type has a name/declaration of it's own, but special attention
15473 : : is required if the type is anonymous.
15474 : :
15475 : : We handle the NORMAL and FIELD contexts here by inserting a
15476 : : dummy statement that just evaluates the size at a safe point
15477 : : and ensures it is not deferred until e.g. within a deeper
15478 : : conditional context (c++/43555).
15479 : :
15480 : : We expect nothing to be needed here for PARM or TYPENAME.
15481 : : Evaluating the size at this point for TYPENAME would
15482 : : actually be incorrect, as we might be in the middle of an
15483 : : expression with side effects on the pointed-to type size
15484 : : "arguments" prior to the pointer declaration point and the
15485 : : size evaluation could end up prior to the side effects. */
15486 : :
15487 : 179810563 : if (!TYPE_NAME (type)
15488 : 5320091 : && (decl_context == NORMAL || decl_context == FIELD)
15489 : 611777 : && at_function_scope_p ()
15490 : 179851775 : && variably_modified_type_p (type, NULL_TREE))
15491 : : {
15492 : 96 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
15493 : : NULL_TREE, type);
15494 : 96 : add_decl_expr (TYPE_NAME (type));
15495 : : }
15496 : :
15497 : 179810563 : if (declarator->kind == cdk_reference)
15498 : : {
15499 : : /* In C++0x, the type we are creating a reference to might be
15500 : : a typedef which is itself a reference type. In that case,
15501 : : we follow the reference collapsing rules in
15502 : : [7.1.3/8 dcl.typedef] to create the final reference type:
15503 : :
15504 : : "If a typedef TD names a type that is a reference to a type
15505 : : T, an attempt to create the type 'lvalue reference to cv TD'
15506 : : creates the type 'lvalue reference to T,' while an attempt
15507 : : to create the type "rvalue reference to cv TD' creates the
15508 : : type TD."
15509 : : */
15510 : 118243838 : if (VOID_TYPE_P (type))
15511 : : /* We already gave an error. */;
15512 : 118243834 : else if (TYPE_REF_P (type))
15513 : : {
15514 : 235 : if (declarator->u.reference.rvalue_ref)
15515 : : /* Leave type alone. */;
15516 : : else
15517 : 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15518 : : }
15519 : : else
15520 : 118243599 : type = cp_build_reference_type
15521 : 118243599 : (type, declarator->u.reference.rvalue_ref);
15522 : :
15523 : : /* In C++0x, we need this check for direct reference to
15524 : : reference declarations, which are forbidden by
15525 : : [8.3.2/5 dcl.ref]. Reference to reference declarations
15526 : : are only allowed indirectly through typedefs and template
15527 : : type arguments. Example:
15528 : :
15529 : : void foo(int & &); // invalid ref-to-ref decl
15530 : :
15531 : : typedef int & int_ref;
15532 : : void foo(int_ref &); // valid ref-to-ref decl
15533 : : */
15534 : 118243838 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15535 : 0 : error ("cannot declare reference to %q#T, which is not "
15536 : : "a typedef or a template type argument", type);
15537 : : }
15538 : 61566725 : else if (TREE_CODE (type) == METHOD_TYPE)
15539 : 917193 : type = build_ptrmemfunc_type (build_pointer_type (type));
15540 : 60649532 : else if (declarator->kind == cdk_ptrmem)
15541 : : {
15542 : 196634 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15543 : : != NAMESPACE_DECL);
15544 : 196634 : if (declarator->u.pointer.class_type == error_mark_node)
15545 : : /* We will already have complained. */
15546 : 3 : type = error_mark_node;
15547 : : else
15548 : 196631 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15549 : : type);
15550 : : }
15551 : : else
15552 : 60452898 : type = build_pointer_type (type);
15553 : :
15554 : : /* Process a list of type modifier keywords (such as
15555 : : const or volatile) that were given inside the `*' or `&'. */
15556 : :
15557 : 179810563 : if (declarator->u.pointer.qualifiers)
15558 : : {
15559 : 8370801 : type
15560 : 8370801 : = cp_build_qualified_type (type,
15561 : : declarator->u.pointer.qualifiers);
15562 : 8370801 : type_quals = cp_type_quals (type);
15563 : : }
15564 : :
15565 : : /* Apply C++11 attributes to the pointer, and not to the
15566 : : type pointed to. This is unlike what is done for GNU
15567 : : attributes above. It is to comply with [dcl.ptr]/1:
15568 : :
15569 : : [the optional attribute-specifier-seq (7.6.1) appertains
15570 : : to the pointer and not to the object pointed to]. */
15571 : 179810563 : if (declarator->std_attributes)
15572 : 169 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15573 : :
15574 : : ctype = NULL_TREE;
15575 : : break;
15576 : :
15577 : : case cdk_error:
15578 : : break;
15579 : :
15580 : 0 : default:
15581 : 0 : gcc_unreachable ();
15582 : : }
15583 : : }
15584 : :
15585 : 922610524 : id_loc = declarator ? declarator->id_loc : input_location;
15586 : :
15587 : 922610524 : if (innermost_code != cdk_function
15588 : : /* Don't check this if it can be the artifical decltype(auto)
15589 : : we created when building a constraint in a compound-requirement:
15590 : : that the type-constraint is plain is going to be checked in
15591 : : cp_parser_compound_requirement. */
15592 : 922610524 : && decl_context != TYPENAME
15593 : 922610524 : && check_decltype_auto (id_loc, type))
15594 : 19 : return error_mark_node;
15595 : :
15596 : : /* A `constexpr' specifier used in an object declaration declares
15597 : : the object as `const'. */
15598 : 922610505 : if (constexpr_p && innermost_code != cdk_function)
15599 : : {
15600 : : /* DR1688 says that a `constexpr' specifier in combination with
15601 : : `volatile' is valid. */
15602 : :
15603 : 13875871 : if (!TYPE_REF_P (type))
15604 : : {
15605 : 13860959 : type_quals |= TYPE_QUAL_CONST;
15606 : 13860959 : type = cp_build_qualified_type (type, type_quals);
15607 : : }
15608 : : }
15609 : :
15610 : 535341944 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15611 : 2012157 : && !FUNC_OR_METHOD_TYPE_P (type)
15612 : 923715633 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15613 : : {
15614 : 0 : error ("template-id %qD used as a declarator",
15615 : : unqualified_id);
15616 : 0 : unqualified_id = dname;
15617 : : }
15618 : :
15619 : : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
15620 : : qualified with a class-name, turn it into a METHOD_TYPE, unless
15621 : : we know that the function is static. We take advantage of this
15622 : : opportunity to do other processing that pertains to entities
15623 : : explicitly declared to be class members. Note that if DECLARATOR
15624 : : is non-NULL, we know it is a cdk_id declarator; otherwise, we
15625 : : would not have exited the loop above. */
15626 : 922610505 : if (declarator
15627 : 535428276 : && declarator->kind == cdk_id
15628 : 535341944 : && declarator->u.id.qualifying_scope
15629 : 932324149 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
15630 : : {
15631 : 9531644 : ctype = declarator->u.id.qualifying_scope;
15632 : 9531644 : ctype = TYPE_MAIN_VARIANT (ctype);
15633 : 9531644 : template_count = num_template_headers_for_class (ctype);
15634 : :
15635 : 9531644 : if (ctype == current_class_type)
15636 : : {
15637 : 54 : if (friendp)
15638 : : {
15639 : 15 : permerror (declspecs->locations[ds_friend],
15640 : : "member functions are implicitly "
15641 : : "friends of their class");
15642 : 15 : friendp = 0;
15643 : : }
15644 : : else
15645 : 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
15646 : : ctype, name);
15647 : : }
15648 : 9531590 : else if (/* If the qualifying type is already complete, then we
15649 : : can skip the following checks. */
15650 : 9531590 : !COMPLETE_TYPE_P (ctype)
15651 : 150 : && (/* If the function is being defined, then
15652 : : qualifying type must certainly be complete. */
15653 : : funcdef_flag
15654 : : /* A friend declaration of "T::f" is OK, even if
15655 : : "T" is a template parameter. But, if this
15656 : : function is not a friend, the qualifying type
15657 : : must be a class. */
15658 : 111 : || (!friendp && !CLASS_TYPE_P (ctype))
15659 : : /* For a declaration, the type need not be
15660 : : complete, if either it is dependent (since there
15661 : : is no meaningful definition of complete in that
15662 : : case) or the qualifying class is currently being
15663 : : defined. */
15664 : 123 : || !(dependent_type_p (ctype)
15665 : 24 : || currently_open_class (ctype)))
15666 : : /* Check that the qualifying type is complete. */
15667 : 9531656 : && !complete_type_or_else (ctype, NULL_TREE))
15668 : 66 : return error_mark_node;
15669 : 9531524 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15670 : : {
15671 : 9143938 : if (current_class_type
15672 : 264 : && (!friendp || funcdef_flag || initialized))
15673 : : {
15674 : 59 : error_at (id_loc, funcdef_flag || initialized
15675 : : ? G_("cannot define member function %<%T::%s%> "
15676 : : "within %qT")
15677 : : : G_("cannot declare member function %<%T::%s%> "
15678 : : "within %qT"),
15679 : : ctype, name, current_class_type);
15680 : 49 : return error_mark_node;
15681 : : }
15682 : : }
15683 : 387586 : else if (typedef_p && current_class_type)
15684 : : {
15685 : 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
15686 : : ctype, name, current_class_type);
15687 : 0 : return error_mark_node;
15688 : : }
15689 : : }
15690 : :
15691 : 922610390 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
15692 : 51530401 : ctype = current_class_type;
15693 : :
15694 : : /* Now TYPE has the actual type. */
15695 : :
15696 : 922610390 : if (returned_attrs)
15697 : : {
15698 : 3338 : if (attrlist)
15699 : 3338 : *attrlist = attr_chainon (returned_attrs, *attrlist);
15700 : : else
15701 : : attrlist = &returned_attrs;
15702 : : }
15703 : :
15704 : 922610390 : if (declarator
15705 : 535428161 : && declarator->kind == cdk_id
15706 : 535341829 : && declarator->std_attributes
15707 : 338552 : && attrlist != NULL)
15708 : : {
15709 : : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
15710 : : a declarator-id appertains to the entity that is declared. */
15711 : 338549 : if (declarator->std_attributes != error_mark_node)
15712 : 338531 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
15713 : : else
15714 : : /* We should have already diagnosed the issue (c++/78344). */
15715 : 18 : gcc_assert (seen_error ());
15716 : : }
15717 : :
15718 : : /* Handle parameter packs. */
15719 : 922610390 : if (parameter_pack_p)
15720 : : {
15721 : 2956661 : if (decl_context == PARM)
15722 : : /* Turn the type into a pack expansion.*/
15723 : 2956661 : type = make_pack_expansion (type);
15724 : : else
15725 : 0 : error ("non-parameter %qs cannot be a parameter pack", name);
15726 : : }
15727 : :
15728 : 922610390 : if ((decl_context == FIELD || decl_context == PARM)
15729 : 423421402 : && !processing_template_decl
15730 : 1058371375 : && variably_modified_type_p (type, NULL_TREE))
15731 : : {
15732 : 12 : if (decl_context == FIELD)
15733 : 6 : error_at (id_loc,
15734 : : "data member may not have variably modified type %qT", type);
15735 : : else
15736 : 6 : error_at (id_loc,
15737 : : "parameter may not have variably modified type %qT", type);
15738 : 12 : type = error_mark_node;
15739 : : }
15740 : :
15741 : 922610390 : if (explicitp == 1 || (explicitp && friendp))
15742 : : {
15743 : : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
15744 : : in the declaration of a constructor or conversion function within
15745 : : a class definition. */
15746 : 74 : if (!current_class_type)
15747 : 59 : error_at (declspecs->locations[ds_explicit],
15748 : : "%<explicit%> outside class declaration");
15749 : 15 : else if (friendp)
15750 : 12 : error_at (declspecs->locations[ds_explicit],
15751 : : "%<explicit%> in friend declaration");
15752 : : else
15753 : 3 : error_at (declspecs->locations[ds_explicit],
15754 : : "only declarations of constructors and conversion operators "
15755 : : "can be %<explicit%>");
15756 : : explicitp = 0;
15757 : : }
15758 : :
15759 : 922610390 : if (storage_class == sc_mutable)
15760 : : {
15761 : 145963 : location_t sloc = declspecs->locations[ds_storage_class];
15762 : 145963 : if (decl_context != FIELD || friendp)
15763 : : {
15764 : 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
15765 : : name);
15766 : 30 : storage_class = sc_none;
15767 : : }
15768 : 145933 : else if (decl_context == TYPENAME || typedef_p)
15769 : : {
15770 : 0 : error_at (sloc,
15771 : : "non-object member %qs cannot be declared %<mutable%>",
15772 : : name);
15773 : 0 : storage_class = sc_none;
15774 : : }
15775 : 145933 : else if (FUNC_OR_METHOD_TYPE_P (type))
15776 : : {
15777 : 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
15778 : : name);
15779 : 3 : storage_class = sc_none;
15780 : : }
15781 : 145930 : else if (staticp)
15782 : : {
15783 : 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
15784 : : name);
15785 : 0 : storage_class = sc_none;
15786 : : }
15787 : 145930 : else if (type_quals & TYPE_QUAL_CONST)
15788 : : {
15789 : 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
15790 : : name);
15791 : 9 : storage_class = sc_none;
15792 : : }
15793 : 145921 : else if (TYPE_REF_P (type))
15794 : : {
15795 : 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
15796 : : name);
15797 : 6 : storage_class = sc_none;
15798 : : }
15799 : : }
15800 : :
15801 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
15802 : 922610390 : if (typedef_p && decl_context != TYPENAME)
15803 : : {
15804 : 43993286 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
15805 : 43993286 : tree decl;
15806 : :
15807 : 43993286 : if (funcdef_flag)
15808 : : {
15809 : 6 : if (decl_context == NORMAL)
15810 : 3 : error_at (id_loc,
15811 : : "typedef may not be a function definition");
15812 : : else
15813 : 3 : error_at (id_loc,
15814 : : "typedef may not be a member function definition");
15815 : 6 : return error_mark_node;
15816 : : }
15817 : :
15818 : : /* This declaration:
15819 : :
15820 : : typedef void f(int) const;
15821 : :
15822 : : declares a function type which is not a member of any
15823 : : particular class, but which is cv-qualified; for
15824 : : example "f S::*" declares a pointer to a const-qualified
15825 : : member function of S. We record the cv-qualification in the
15826 : : function type. */
15827 : 43993280 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
15828 : : {
15829 : 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
15830 : :
15831 : : /* We have now dealt with these qualifiers. */
15832 : 62 : memfn_quals = TYPE_UNQUALIFIED;
15833 : 62 : rqual = REF_QUAL_NONE;
15834 : : }
15835 : :
15836 : 43993280 : if (type_uses_auto (type))
15837 : : {
15838 : 59 : if (alias_p)
15839 : 19 : error_at (declspecs->locations[ds_type_spec],
15840 : : "%<auto%> not allowed in alias declaration");
15841 : : else
15842 : 40 : error_at (declspecs->locations[ds_type_spec],
15843 : : "typedef declared %<auto%>");
15844 : 59 : type = error_mark_node;
15845 : : }
15846 : :
15847 : 43993280 : if (reqs)
15848 : 3 : error_at (location_of (reqs), "requires-clause on typedef");
15849 : :
15850 : 43993280 : if (id_declarator && declarator->u.id.qualifying_scope)
15851 : : {
15852 : 22 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
15853 : 22 : type = error_mark_node;
15854 : : }
15855 : :
15856 : 43993280 : if (decl_context == FIELD)
15857 : 23889077 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
15858 : : else
15859 : 20104203 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
15860 : :
15861 : 43993280 : if (decl_context != FIELD)
15862 : : {
15863 : 20104203 : if (!current_function_decl)
15864 : 8443614 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
15865 : 11660589 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
15866 : : /* The TYPE_DECL is "abstract" because there will be
15867 : : clones of this constructor/destructor, and there will
15868 : : be copies of this TYPE_DECL generated in those
15869 : : clones. The decloning optimization (for space) may
15870 : : revert this subsequently if it determines that
15871 : : the clones should share a common implementation. */
15872 : 157008 : DECL_ABSTRACT_P (decl) = true;
15873 : :
15874 : 20104203 : set_originating_module (decl);
15875 : : }
15876 : 23889077 : else if (current_class_type
15877 : 23889077 : && constructor_name_p (unqualified_id, current_class_type))
15878 : 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
15879 : : "as enclosing class",
15880 : : unqualified_id);
15881 : :
15882 : : /* If the user declares "typedef struct {...} foo" then the
15883 : : struct will have an anonymous name. Fill that name in now.
15884 : : Nothing can refer to it, so nothing needs know about the name
15885 : : change. */
15886 : 43993280 : if (type != error_mark_node
15887 : 43993199 : && unqualified_id
15888 : 43993196 : && TYPE_NAME (type)
15889 : 41109644 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15890 : 82300147 : && (TYPE_UNNAMED_P (type)
15891 : : /* An enum may have previously used an enumerator for linkage
15892 : : purposes, but we want the typedef name to take priority. */
15893 : 40720168 : || enum_with_enumerator_for_linkage_p (type))
15894 : 389592 : && declspecs->type_definition_p
15895 : 389522 : && attributes_naming_typedef_ok (*attrlist)
15896 : 44382799 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
15897 : 389501 : name_unnamed_type (type, decl);
15898 : :
15899 : 43993280 : if (signed_p
15900 : 43993280 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
15901 : 375704 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
15902 : :
15903 : 43993280 : bad_specifiers (decl, BSP_TYPE, virtualp,
15904 : : memfn_quals != TYPE_UNQUALIFIED,
15905 : : inlinep, friendp, raises != NULL_TREE,
15906 : : declspecs->locations);
15907 : :
15908 : 43993280 : if (alias_p)
15909 : : /* Acknowledge that this was written:
15910 : : `using analias = atype;'. */
15911 : 16951508 : TYPE_DECL_ALIAS_P (decl) = 1;
15912 : :
15913 : 43993280 : return decl;
15914 : : }
15915 : :
15916 : : /* Detect the case of an array type of unspecified size
15917 : : which came, as such, direct from a typedef name.
15918 : : We must copy the type, so that the array's domain can be
15919 : : individually set by the object's initializer. */
15920 : :
15921 : 878617104 : if (type && typedef_type
15922 : 851514399 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
15923 : 879139763 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
15924 : 79 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15925 : :
15926 : : /* Detect where we're using a typedef of function type to declare a
15927 : : function. PARMS will not be set, so we must create it now. */
15928 : :
15929 : 878617104 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
15930 : : {
15931 : 15179 : tree decls = NULL_TREE;
15932 : 15179 : tree args;
15933 : :
15934 : 15179 : for (args = TYPE_ARG_TYPES (type);
15935 : 32821 : args && args != void_list_node;
15936 : 17642 : args = TREE_CHAIN (args))
15937 : : {
15938 : 17642 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
15939 : 17642 : TREE_VALUE (args));
15940 : :
15941 : 17642 : DECL_CHAIN (decl) = decls;
15942 : 17642 : decls = decl;
15943 : : }
15944 : :
15945 : 15179 : parms = nreverse (decls);
15946 : :
15947 : 15179 : if (decl_context != TYPENAME)
15948 : : {
15949 : : /* The qualifiers on the function type become the qualifiers on
15950 : : the non-static member function. */
15951 : 14489 : memfn_quals |= type_memfn_quals (type);
15952 : 14489 : rqual = type_memfn_rqual (type);
15953 : 14489 : type_quals = TYPE_UNQUALIFIED;
15954 : 14489 : raises = TYPE_RAISES_EXCEPTIONS (type);
15955 : : }
15956 : : }
15957 : :
15958 : : /* If this is a type name (such as, in a cast or sizeof),
15959 : : compute the type and return it now. */
15960 : :
15961 : 878616414 : if (decl_context == TYPENAME)
15962 : : {
15963 : : /* Note that here we don't care about type_quals. */
15964 : :
15965 : : /* Special case: "friend class foo" looks like a TYPENAME context. */
15966 : 343202502 : if (friendp)
15967 : : {
15968 : 0 : if (inlinep)
15969 : : {
15970 : 0 : error ("%<inline%> specified for friend class declaration");
15971 : 0 : inlinep = 0;
15972 : : }
15973 : :
15974 : 0 : if (!current_aggr)
15975 : : {
15976 : : /* Don't allow friend declaration without a class-key. */
15977 : 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15978 : 0 : permerror (input_location, "template parameters cannot be friends");
15979 : 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
15980 : 0 : permerror (input_location, "friend declaration requires class-key, "
15981 : : "i.e. %<friend class %T::%D%>",
15982 : 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
15983 : : else
15984 : 0 : permerror (input_location, "friend declaration requires class-key, "
15985 : : "i.e. %<friend %#T%>",
15986 : : type);
15987 : : }
15988 : :
15989 : : /* Only try to do this stuff if we didn't already give up. */
15990 : 0 : if (type != integer_type_node)
15991 : : {
15992 : : /* A friendly class? */
15993 : 0 : if (current_class_type)
15994 : 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
15995 : : /*complain=*/true);
15996 : : else
15997 : 0 : error ("trying to make class %qT a friend of global scope",
15998 : : type);
15999 : :
16000 : 0 : type = void_type_node;
16001 : : }
16002 : : }
16003 : 343202502 : else if (memfn_quals || rqual)
16004 : : {
16005 : 64372 : if (ctype == NULL_TREE
16006 : 64372 : && TREE_CODE (type) == METHOD_TYPE)
16007 : 0 : ctype = TYPE_METHOD_BASETYPE (type);
16008 : :
16009 : 64372 : if (ctype)
16010 : 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
16011 : : /* Core issue #547: need to allow this in template type args.
16012 : : Allow it in general in C++11 for alias-declarations. */
16013 : 64372 : else if ((template_type_arg || cxx_dialect >= cxx11)
16014 : 64370 : && TREE_CODE (type) == FUNCTION_TYPE)
16015 : 64368 : type = apply_memfn_quals (type, memfn_quals, rqual);
16016 : : else
16017 : 4 : error ("invalid qualifiers on non-member function type");
16018 : : }
16019 : :
16020 : 343202502 : if (reqs)
16021 : 3 : error_at (location_of (reqs), "requires-clause on type-id");
16022 : :
16023 : 343202502 : return type;
16024 : : }
16025 : 535414602 : else if (unqualified_id == NULL_TREE && decl_context != PARM
16026 : 706033 : && decl_context != CATCHPARM
16027 : 222532 : && TREE_CODE (type) != UNION_TYPE
16028 : 222292 : && ! bitfield
16029 : 222292 : && innermost_code != cdk_decomp)
16030 : : {
16031 : 9 : error ("abstract declarator %qT used as declaration", type);
16032 : 9 : return error_mark_node;
16033 : : }
16034 : :
16035 : 535414593 : if (!FUNC_OR_METHOD_TYPE_P (type))
16036 : : {
16037 : : /* Only functions may be declared using an operator-function-id. */
16038 : 361361274 : if (dname && IDENTIFIER_ANY_OP_P (dname))
16039 : : {
16040 : 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
16041 : 8 : return error_mark_node;
16042 : : }
16043 : :
16044 : 361361266 : if (reqs)
16045 : 15 : error_at (location_of (reqs),
16046 : : "requires-clause on declaration of non-function type %qT",
16047 : : type);
16048 : : }
16049 : :
16050 : : /* We don't check parameter types here because we can emit a better
16051 : : error message later. */
16052 : 535414585 : if (decl_context != PARM)
16053 : : {
16054 : 258460596 : type = check_var_type (unqualified_id, type, id_loc);
16055 : 258460596 : if (type == error_mark_node)
16056 : : return error_mark_node;
16057 : : }
16058 : :
16059 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
16060 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
16061 : :
16062 : 535414503 : if (decl_context == PARM || decl_context == CATCHPARM)
16063 : : {
16064 : 277451638 : if (ctype || in_namespace)
16065 : 0 : error ("cannot use %<::%> in parameter declaration");
16066 : :
16067 : 277451638 : tree auto_node = type_uses_auto (type);
16068 : 277451638 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
16069 : : {
16070 : 119 : bool err_p = true;
16071 : 119 : if (cxx_dialect >= cxx14)
16072 : : {
16073 : 210 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
16074 : 3 : error_at (typespec_loc,
16075 : : "cannot declare a parameter with %<decltype(auto)%>");
16076 : 108 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16077 : : {
16078 : 4 : auto_diagnostic_group g;
16079 : 4 : gcc_rich_location richloc (typespec_loc);
16080 : 4 : richloc.add_fixit_insert_after ("<>");
16081 : 4 : error_at (&richloc,
16082 : : "missing template argument list after %qE; template "
16083 : : "placeholder not permitted in parameter", c);
16084 : 4 : if (decl_context == PARM && cxx_dialect >= cxx20)
16085 : 3 : inform (typespec_loc, "or use %<auto%> for an "
16086 : : "abbreviated function template");
16087 : 4 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
16088 : 4 : }
16089 : 104 : else if (decl_context == CATCHPARM || template_parm_flag)
16090 : 14 : error_at (typespec_loc,
16091 : : "%<auto%> parameter not permitted in this context");
16092 : : else
16093 : : /* Do not issue an error while tentatively parsing a function
16094 : : parameter: for T t(auto(a), 42);, when we just saw the 1st
16095 : : parameter, we don't know yet that this construct won't be
16096 : : a function declaration. Defer the checking to
16097 : : cp_parser_parameter_declaration_clause. */
16098 : : err_p = false;
16099 : : }
16100 : : else
16101 : 8 : error_at (typespec_loc, "parameter declared %<auto%>");
16102 : 29 : if (err_p)
16103 : 29 : type = error_mark_node;
16104 : : }
16105 : :
16106 : : /* A parameter declared as an array of T is really a pointer to T.
16107 : : One declared as a function is really a pointer to a function.
16108 : : One declared as a member is really a pointer to member. */
16109 : :
16110 : 277451638 : if (TREE_CODE (type) == ARRAY_TYPE)
16111 : : {
16112 : : /* Transfer const-ness of array into that of type pointed to. */
16113 : 732365 : type = build_pointer_type (TREE_TYPE (type));
16114 : 732365 : type_quals = TYPE_UNQUALIFIED;
16115 : 732365 : array_parameter_p = true;
16116 : : }
16117 : 276719273 : else if (TREE_CODE (type) == FUNCTION_TYPE)
16118 : 106964 : type = build_pointer_type (type);
16119 : : }
16120 : :
16121 : 127863214 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
16122 : : /* Don't convert xobj member functions to METHOD_TYPE. */
16123 : 99834181 : && !is_xobj_member_function
16124 : 626548418 : && !(unqualified_id
16125 : 182254940 : && identifier_p (unqualified_id)
16126 : 91133555 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
16127 : : {
16128 : 91121385 : cp_cv_quals real_quals = memfn_quals;
16129 : 91121385 : if (cxx_dialect < cxx14 && constexpr_p
16130 : 630009 : && sfk != sfk_constructor && sfk != sfk_destructor)
16131 : 3801 : real_quals |= TYPE_QUAL_CONST;
16132 : 91121385 : type = build_memfn_type (type, ctype, real_quals, rqual);
16133 : : }
16134 : :
16135 : 535414503 : {
16136 : 535414503 : tree decl = NULL_TREE;
16137 : :
16138 : 535414503 : if (decl_context == PARM)
16139 : : {
16140 : 276953989 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
16141 : 276953989 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
16142 : :
16143 : 276953989 : bad_specifiers (decl, BSP_PARM, virtualp,
16144 : : memfn_quals != TYPE_UNQUALIFIED,
16145 : : inlinep, friendp, raises != NULL_TREE,
16146 : : declspecs->locations);
16147 : : }
16148 : 258460514 : else if (decl_context == FIELD)
16149 : : {
16150 : 122578317 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
16151 : 17120998 : if (tree auto_node = type_uses_auto (type))
16152 : : {
16153 : 22 : location_t tloc = declspecs->locations[ds_type_spec];
16154 : 22 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16155 : 4 : error_at (tloc, "invalid use of template-name %qE without an "
16156 : : "argument list",
16157 : 4 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
16158 : : else
16159 : 18 : error_at (tloc, "non-static data member declared with "
16160 : : "placeholder %qT", auto_node);
16161 : 22 : type = error_mark_node;
16162 : : }
16163 : :
16164 : : /* The C99 flexible array extension. */
16165 : 103417984 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
16166 : 123685577 : && TYPE_DOMAIN (type) == NULL_TREE)
16167 : : {
16168 : 1395 : if (ctype
16169 : 1395 : && (TREE_CODE (ctype) == UNION_TYPE
16170 : 1395 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
16171 : 84 : pedwarn (id_loc, OPT_Wpedantic,
16172 : : "flexible array member in union is a GCC extension");
16173 : :
16174 : : else
16175 : : {
16176 : : /* Array is a flexible member. */
16177 : 1311 : if (name)
16178 : 1311 : pedwarn (id_loc, OPT_Wpedantic,
16179 : : "ISO C++ forbids flexible array member %qs", name);
16180 : : else
16181 : : pedwarn (input_location, OPT_Wpedantic,
16182 : : "ISO C++ forbids flexible array members");
16183 : :
16184 : : /* Flexible array member has a null domain. */
16185 : 1311 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
16186 : : }
16187 : : }
16188 : :
16189 : 122578317 : if (type == error_mark_node)
16190 : : {
16191 : : /* Happens when declaring arrays of sizes which
16192 : : are error_mark_node, for example. */
16193 : 22 : decl = NULL_TREE;
16194 : : }
16195 : 122578295 : else if (in_namespace && !friendp)
16196 : : {
16197 : : /* Something like struct S { int N::j; }; */
16198 : 6 : error_at (id_loc, "invalid use of %<::%>");
16199 : 6 : return error_mark_node;
16200 : : }
16201 : 122578289 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
16202 : : {
16203 : 94936825 : int publicp = 0;
16204 : 94936825 : tree function_context;
16205 : :
16206 : 94936825 : if (friendp == 0)
16207 : : {
16208 : : /* This should never happen in pure C++ (the check
16209 : : could be an assert). It could happen in
16210 : : Objective-C++ if someone writes invalid code that
16211 : : uses a function declaration for an instance
16212 : : variable or property (instance variables and
16213 : : properties are parsed as FIELD_DECLs, but they are
16214 : : part of an Objective-C class, not a C++ class).
16215 : : That code is invalid and is caught by this
16216 : : check. */
16217 : 90690292 : if (!ctype)
16218 : : {
16219 : 0 : error ("declaration of function %qD in invalid context",
16220 : : unqualified_id);
16221 : 0 : return error_mark_node;
16222 : : }
16223 : :
16224 : : /* ``A union may [ ... ] not [ have ] virtual functions.''
16225 : : ARM 9.5 */
16226 : 90690292 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
16227 : : {
16228 : 3 : error_at (declspecs->locations[ds_virtual],
16229 : : "function %qD declared %<virtual%> inside a union",
16230 : : unqualified_id);
16231 : 3 : return error_mark_node;
16232 : : }
16233 : :
16234 : 4051059 : if (virtualp
16235 : 98987881 : && identifier_p (unqualified_id)
16236 : 4051059 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16237 : : {
16238 : 9 : error_at (declspecs->locations[ds_virtual],
16239 : : "%qD cannot be declared %<virtual%>, since it "
16240 : : "is always static", unqualified_id);
16241 : 9 : virtualp = 0;
16242 : : }
16243 : : }
16244 : :
16245 : : /* Check that the name used for a destructor makes sense. */
16246 : 94936822 : if (sfk == sfk_destructor)
16247 : : {
16248 : 2991361 : tree uqname = id_declarator->u.id.unqualified_name;
16249 : :
16250 : 2991361 : if (!ctype)
16251 : : {
16252 : 12 : gcc_assert (friendp);
16253 : 12 : error_at (id_loc, "expected qualified name in friend "
16254 : : "declaration for destructor %qD", uqname);
16255 : 12 : return error_mark_node;
16256 : : }
16257 : :
16258 : 2991349 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
16259 : : {
16260 : 9 : error_at (id_loc, "declaration of %qD as member of %qT",
16261 : : uqname, ctype);
16262 : 9 : return error_mark_node;
16263 : : }
16264 : 2991340 : if (concept_p)
16265 : : {
16266 : 9 : error_at (declspecs->locations[ds_concept],
16267 : : "a destructor cannot be %qs", "concept");
16268 : 9 : return error_mark_node;
16269 : : }
16270 : 2991331 : if (constexpr_p && cxx_dialect < cxx20)
16271 : : {
16272 : 6 : error_at (declspecs->locations[ds_constexpr],
16273 : : "%<constexpr%> destructors only available"
16274 : : " with %<-std=c++20%> or %<-std=gnu++20%>");
16275 : 6 : return error_mark_node;
16276 : : }
16277 : 2991325 : if (consteval_p)
16278 : : {
16279 : 3 : error_at (declspecs->locations[ds_consteval],
16280 : : "a destructor cannot be %qs", "consteval");
16281 : 3 : return error_mark_node;
16282 : : }
16283 : : }
16284 : 91945461 : else if (sfk == sfk_constructor && friendp && !ctype)
16285 : : {
16286 : 0 : error ("expected qualified name in friend declaration "
16287 : : "for constructor %qD",
16288 : 0 : id_declarator->u.id.unqualified_name);
16289 : 0 : return error_mark_node;
16290 : : }
16291 : 94936783 : if (sfk == sfk_constructor)
16292 : 21430866 : if (concept_p)
16293 : : {
16294 : 9 : error_at (declspecs->locations[ds_concept],
16295 : : "a constructor cannot be %<concept%>");
16296 : 9 : return error_mark_node;
16297 : : }
16298 : 73505917 : if (concept_p)
16299 : : {
16300 : 9 : error_at (declspecs->locations[ds_concept],
16301 : : "a concept cannot be a member function");
16302 : 9 : concept_p = false;
16303 : : }
16304 : 94936765 : else if (consteval_p
16305 : 95174240 : && identifier_p (unqualified_id)
16306 : 95174231 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16307 : : {
16308 : 9 : error_at (declspecs->locations[ds_consteval],
16309 : : "%qD cannot be %qs", unqualified_id, "consteval");
16310 : 9 : consteval_p = false;
16311 : : }
16312 : :
16313 : 94936774 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16314 : : {
16315 : 39686 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
16316 : 39686 : if (variable_template_p (tmpl))
16317 : : {
16318 : 3 : auto_diagnostic_group d;
16319 : 3 : error_at (id_loc, "specialization of variable template "
16320 : : "%qD declared as function", tmpl);
16321 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
16322 : : "variable template declared here");
16323 : 3 : return error_mark_node;
16324 : 3 : }
16325 : : }
16326 : :
16327 : : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
16328 : 94936771 : function_context
16329 : : = (ctype != NULL_TREE
16330 : 94936771 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
16331 : 189873542 : publicp = ((! friendp || ! staticp)
16332 : 94936771 : && function_context == NULL_TREE);
16333 : :
16334 : 94936771 : decl = grokfndecl (ctype, type,
16335 : 94936771 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
16336 : : ? unqualified_id : dname,
16337 : : parms,
16338 : : unqualified_id,
16339 : : declspecs,
16340 : : reqs,
16341 : : virtualp, flags, memfn_quals, rqual, raises,
16342 : : friendp ? -1 : 0, friendp, publicp,
16343 : 94936771 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16344 : 94936771 : | (8 * consteval_p),
16345 : : initialized == SD_DELETED,
16346 : : is_xobj_member_function, sfk,
16347 : : funcdef_flag, late_return_type_p,
16348 : : template_count, in_namespace,
16349 : : attrlist, id_loc);
16350 : 94936771 : decl = set_virt_specifiers (decl, virt_specifiers);
16351 : 94936771 : if (decl == NULL_TREE)
16352 : 98 : return error_mark_node;
16353 : : #if 0
16354 : : /* This clobbers the attrs stored in `decl' from `attrlist'. */
16355 : : /* The decl and setting of decl_attr is also turned off. */
16356 : : decl = build_decl_attribute_variant (decl, decl_attr);
16357 : : #endif
16358 : :
16359 : : /* [class.conv.ctor]
16360 : :
16361 : : A constructor declared without the function-specifier
16362 : : explicit that can be called with a single parameter
16363 : : specifies a conversion from the type of its first
16364 : : parameter to the type of its class. Such a constructor
16365 : : is called a converting constructor. */
16366 : 94936673 : if (explicitp == 2)
16367 : 5403948 : DECL_NONCONVERTING_P (decl) = 1;
16368 : :
16369 : 94936673 : if (declspecs->explicit_specifier)
16370 : 172483 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16371 : : }
16372 : 27641464 : else if (!staticp
16373 : 27641464 : && ((current_class_type
16374 : 17120993 : && same_type_p (TYPE_MAIN_VARIANT (type),
16375 : : current_class_type))
16376 : 17120897 : || (!dependent_type_p (type)
16377 : 10630543 : && !COMPLETE_TYPE_P (complete_type (type))
16378 : 1443 : && (!complete_or_array_type_p (type)
16379 : 1368 : || initialized == SD_UNINITIALIZED))))
16380 : : {
16381 : 1506 : if (TREE_CODE (type) != ARRAY_TYPE
16382 : 1506 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
16383 : : {
16384 : 171 : if (unqualified_id)
16385 : : {
16386 : 126 : auto_diagnostic_group d;
16387 : 126 : error_at (id_loc, "field %qD has incomplete type %qT",
16388 : : unqualified_id, type);
16389 : 126 : cxx_incomplete_type_inform (strip_array_types (type));
16390 : 126 : }
16391 : : else
16392 : 45 : error ("name %qT has incomplete type", type);
16393 : :
16394 : 171 : type = error_mark_node;
16395 : 171 : decl = NULL_TREE;
16396 : : }
16397 : : }
16398 : 44759433 : else if (!verify_type_context (input_location,
16399 : : staticp
16400 : : ? TCTX_STATIC_STORAGE
16401 : : : TCTX_FIELD, type))
16402 : : {
16403 : 0 : type = error_mark_node;
16404 : 0 : decl = NULL_TREE;
16405 : : }
16406 : : else
16407 : : {
16408 : 27639952 : if (friendp)
16409 : : {
16410 : 14 : if (unqualified_id)
16411 : 11 : error_at (id_loc,
16412 : : "%qE is neither function nor member function; "
16413 : : "cannot be declared friend", unqualified_id);
16414 : : else
16415 : 3 : error ("unnamed field is neither function nor member "
16416 : : "function; cannot be declared friend");
16417 : 14 : return error_mark_node;
16418 : : }
16419 : 27639938 : decl = NULL_TREE;
16420 : : }
16421 : :
16422 : 122578139 : if (friendp)
16423 : : {
16424 : : /* Packages tend to use GNU attributes on friends, so we only
16425 : : warn for standard attributes. */
16426 : 4246504 : if (attrlist
16427 : 4246504 : && !funcdef_flag
16428 : 1278136 : && cxx11_attribute_p (*attrlist)
16429 : 4246533 : && !all_attributes_are_contracts_p (*attrlist))
16430 : : {
16431 : 27 : *attrlist = NULL_TREE;
16432 : 27 : auto_diagnostic_group d;
16433 : 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
16434 : 27 : inform (id_loc, "an attribute that appertains to a friend "
16435 : : "declaration that is not a definition is ignored");
16436 : 27 : }
16437 : : /* Friends are treated specially. */
16438 : 4246504 : if (ctype == current_class_type)
16439 : : ; /* We already issued a permerror. */
16440 : 4246504 : else if (decl && DECL_NAME (decl))
16441 : : {
16442 : 4246501 : set_originating_module (decl, true);
16443 : :
16444 : 4246501 : if (initialized)
16445 : : /* Kludge: We need funcdef_flag to be true in do_friend for
16446 : : in-class defaulted functions, but that breaks grokfndecl.
16447 : : So set it here. */
16448 : 58677 : funcdef_flag = true;
16449 : :
16450 : 4246501 : cplus_decl_attributes (&decl, *attrlist, 0);
16451 : 4246501 : *attrlist = NULL_TREE;
16452 : :
16453 : 4246501 : tree scope = ctype ? ctype : in_namespace;
16454 : 4246501 : decl = do_friend (scope, unqualified_id, decl,
16455 : : flags, funcdef_flag);
16456 : 4246501 : return decl;
16457 : : }
16458 : : else
16459 : 3 : return error_mark_node;
16460 : : }
16461 : :
16462 : : /* Structure field. It may not be a function, except for C++. */
16463 : :
16464 : 118331635 : if (decl == NULL_TREE)
16465 : : {
16466 : 27641463 : if (staticp)
16467 : : {
16468 : : /* C++ allows static class members. All other work
16469 : : for this is done by grokfield. */
16470 : 10520471 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
16471 : : dname, type);
16472 : 10520471 : if (unqualified_id
16473 : 10520468 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16474 : : {
16475 : 83242 : decl = check_explicit_specialization (unqualified_id, decl,
16476 : : template_count,
16477 : : concept_p * 8);
16478 : 83242 : if (decl == error_mark_node)
16479 : : return error_mark_node;
16480 : : }
16481 : 10520471 : set_linkage_for_static_data_member (decl);
16482 : 10520471 : if (concept_p)
16483 : 15 : error_at (declspecs->locations[ds_concept],
16484 : : "static data member %qE declared %qs",
16485 : : unqualified_id, "concept");
16486 : 10520456 : else if (constexpr_p && !initialized)
16487 : : {
16488 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
16489 : : "%<constexpr%> static data member %qD must "
16490 : : "have an initializer", decl);
16491 : 9 : constexpr_p = false;
16492 : : }
16493 : 10520471 : if (consteval_p)
16494 : 3 : error_at (declspecs->locations[ds_consteval],
16495 : : "static data member %qE declared %qs",
16496 : : unqualified_id, "consteval");
16497 : :
16498 : 10520471 : if (inlinep)
16499 : 11193 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16500 : :
16501 : 10520471 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16502 : 10520471 : && !(cxx_dialect >= cxx17 && constexpr_p))
16503 : : /* Even if there is an in-class initialization, DECL
16504 : : is considered undefined until an out-of-class
16505 : : definition is provided, unless this is an inline
16506 : : variable. */
16507 : 1721145 : DECL_EXTERNAL (decl) = 1;
16508 : :
16509 : 10520471 : if (thread_p)
16510 : : {
16511 : 420 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16512 : 420 : if (!processing_template_decl)
16513 : 374 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16514 : 420 : if (declspecs->gnu_thread_keyword_p)
16515 : 34 : SET_DECL_GNU_TLS_P (decl);
16516 : : }
16517 : :
16518 : : /* Set the constraints on the declaration. */
16519 : 10520471 : bool memtmpl = (current_template_depth
16520 : 10520471 : > template_class_depth (current_class_type));
16521 : 10520471 : if (memtmpl)
16522 : : {
16523 : 313666 : tree ci = current_template_constraints ();
16524 : 313666 : set_constraints (decl, ci);
16525 : : }
16526 : : }
16527 : : else
16528 : : {
16529 : 17120992 : if (concept_p)
16530 : : {
16531 : 12 : error_at (declspecs->locations[ds_concept],
16532 : : "non-static data member %qE declared %qs",
16533 : : unqualified_id, "concept");
16534 : 12 : concept_p = false;
16535 : 12 : constexpr_p = false;
16536 : : }
16537 : 17120980 : else if (constexpr_p)
16538 : : {
16539 : 9 : error_at (declspecs->locations[ds_constexpr],
16540 : : "non-static data member %qE declared %qs",
16541 : : unqualified_id, "constexpr");
16542 : 9 : constexpr_p = false;
16543 : : }
16544 : 17120992 : if (constinit_p)
16545 : : {
16546 : 6 : error_at (declspecs->locations[ds_constinit],
16547 : : "non-static data member %qE declared %qs",
16548 : : unqualified_id, "constinit");
16549 : 6 : constinit_p = false;
16550 : : }
16551 : 17120992 : if (consteval_p)
16552 : : {
16553 : 3 : error_at (declspecs->locations[ds_consteval],
16554 : : "non-static data member %qE declared %qs",
16555 : : unqualified_id, "consteval");
16556 : 3 : consteval_p = false;
16557 : : }
16558 : 17120992 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16559 : 17120992 : DECL_NONADDRESSABLE_P (decl) = bitfield;
16560 : 17120992 : if (bitfield && !unqualified_id)
16561 : 135942 : DECL_PADDING_P (decl) = 1;
16562 : :
16563 : 17120992 : if (storage_class == sc_mutable)
16564 : : {
16565 : 145915 : DECL_MUTABLE_P (decl) = 1;
16566 : 145915 : storage_class = sc_none;
16567 : : }
16568 : :
16569 : 17120992 : if (initialized)
16570 : : {
16571 : : /* An attempt is being made to initialize a non-static
16572 : : member. This is new in C++11. */
16573 : 1243481 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
16574 : :
16575 : : /* If this has been parsed with static storage class, but
16576 : : errors forced staticp to be cleared, ensure NSDMI is
16577 : : not present. */
16578 : 1243481 : if (declspecs->storage_class == sc_static)
16579 : 3 : DECL_INITIAL (decl) = error_mark_node;
16580 : : }
16581 : : }
16582 : :
16583 : 44762455 : bad_specifiers (decl, BSP_FIELD, virtualp,
16584 : : memfn_quals != TYPE_UNQUALIFIED,
16585 : : staticp ? false : inlinep, friendp,
16586 : : raises != NULL_TREE,
16587 : : declspecs->locations);
16588 : : }
16589 : : }
16590 : 135882197 : else if (FUNC_OR_METHOD_TYPE_P (type))
16591 : : {
16592 : 79009524 : tree original_name;
16593 : 79009524 : int publicp = 0;
16594 : :
16595 : 79009524 : if (!unqualified_id)
16596 : 0 : return error_mark_node;
16597 : :
16598 : 79009524 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16599 : : original_name = dname;
16600 : : else
16601 : 78142187 : original_name = unqualified_id;
16602 : : // FIXME:gcc_assert (original_name == dname);
16603 : :
16604 : 79009524 : if (storage_class == sc_auto)
16605 : 1 : error_at (declspecs->locations[ds_storage_class],
16606 : : "storage class %<auto%> invalid for function %qs", name);
16607 : 79009523 : else if (storage_class == sc_register)
16608 : 7 : error_at (declspecs->locations[ds_storage_class],
16609 : : "storage class %<register%> invalid for function %qs",
16610 : : name);
16611 : 79009516 : else if (thread_p)
16612 : : {
16613 : 12 : if (declspecs->gnu_thread_keyword_p)
16614 : 12 : error_at (declspecs->locations[ds_thread],
16615 : : "storage class %<__thread%> invalid for function %qs",
16616 : : name);
16617 : : else
16618 : 0 : error_at (declspecs->locations[ds_thread],
16619 : : "storage class %<thread_local%> invalid for "
16620 : : "function %qs", name);
16621 : : }
16622 : 79009504 : else if (constinit_p)
16623 : 3 : error_at (declspecs->locations[ds_constinit],
16624 : : "%<constinit%> specifier invalid for function %qs", name);
16625 : :
16626 : 79009524 : if (virt_specifiers)
16627 : 6 : error ("virt-specifiers in %qs not allowed outside a class "
16628 : : "definition", name);
16629 : : /* Function declaration not at top level.
16630 : : Storage classes other than `extern' are not allowed
16631 : : and `extern' makes no difference. */
16632 : 79009524 : if (! toplevel_bindings_p ()
16633 : 38857 : && (storage_class == sc_static
16634 : 38848 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
16635 : 79009536 : && pedantic)
16636 : : {
16637 : 9 : if (storage_class == sc_static)
16638 : 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
16639 : : "%<static%> specifier invalid for function %qs "
16640 : : "declared out of global scope", name);
16641 : : else
16642 : 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
16643 : : "%<inline%> specifier invalid for function %qs "
16644 : : "declared out of global scope", name);
16645 : : }
16646 : :
16647 : 79009524 : if (ctype == NULL_TREE)
16648 : : {
16649 : 69865856 : if (virtualp)
16650 : : {
16651 : 0 : error ("virtual non-class function %qs", name);
16652 : 0 : virtualp = 0;
16653 : : }
16654 : 69865856 : else if (sfk == sfk_constructor
16655 : 69865856 : || sfk == sfk_destructor)
16656 : : {
16657 : 3 : error (funcdef_flag
16658 : : ? G_("%qs defined in a non-class scope")
16659 : : : G_("%qs declared in a non-class scope"), name);
16660 : 3 : sfk = sfk_none;
16661 : : }
16662 : : }
16663 : 79009524 : if (consteval_p
16664 : 79056880 : && identifier_p (unqualified_id)
16665 : 79056880 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16666 : : {
16667 : 9 : error_at (declspecs->locations[ds_consteval],
16668 : : "%qD cannot be %qs", unqualified_id, "consteval");
16669 : 9 : consteval_p = false;
16670 : : }
16671 : :
16672 : : /* Record whether the function is public. */
16673 : 79009524 : publicp = (ctype != NULL_TREE
16674 : 79009524 : || storage_class != sc_static);
16675 : :
16676 : 158019048 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
16677 : : declspecs,
16678 : : reqs, virtualp, flags, memfn_quals, rqual, raises,
16679 : : 1, friendp,
16680 : : publicp,
16681 : 79009524 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16682 : 79009524 : | (8 * consteval_p),
16683 : : initialized == SD_DELETED,
16684 : : is_xobj_member_function, sfk,
16685 : : funcdef_flag,
16686 : : late_return_type_p,
16687 : : template_count, in_namespace, attrlist,
16688 : : id_loc);
16689 : 79009524 : if (decl == NULL_TREE)
16690 : 506 : return error_mark_node;
16691 : :
16692 : 79009018 : if (explicitp == 2)
16693 : 5312 : DECL_NONCONVERTING_P (decl) = 1;
16694 : 79009018 : if (staticp == 1)
16695 : : {
16696 : 107988 : int invalid_static = 0;
16697 : :
16698 : : /* Don't allow a static member function in a class, and forbid
16699 : : declaring main to be static. */
16700 : 107988 : if (TREE_CODE (type) == METHOD_TYPE)
16701 : : {
16702 : 0 : permerror (input_location, "cannot declare member function %qD to have "
16703 : : "static linkage", decl);
16704 : 0 : invalid_static = 1;
16705 : : }
16706 : 107988 : else if (current_function_decl)
16707 : : {
16708 : : /* 7.1.1: There can be no static function declarations within a
16709 : : block. */
16710 : 9 : error_at (declspecs->locations[ds_storage_class],
16711 : : "cannot declare static function inside another function");
16712 : 9 : invalid_static = 1;
16713 : : }
16714 : :
16715 : 9 : if (invalid_static)
16716 : : {
16717 : 79009018 : staticp = 0;
16718 : : storage_class = sc_none;
16719 : : }
16720 : : }
16721 : 79009018 : if (declspecs->explicit_specifier)
16722 : 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16723 : : }
16724 : : else
16725 : : {
16726 : : /* It's a variable. */
16727 : :
16728 : : /* An uninitialized decl with `extern' is a reference. */
16729 : 113357782 : decl = grokvardecl (type, dname, unqualified_id,
16730 : : declspecs,
16731 : : initialized,
16732 : : type_quals,
16733 : : inlinep,
16734 : : concept_p,
16735 : : template_count,
16736 : : ctype ? ctype : in_namespace,
16737 : : id_loc);
16738 : 56872673 : if (decl == NULL_TREE)
16739 : 49 : return error_mark_node;
16740 : :
16741 : 56872624 : bad_specifiers (decl, BSP_VAR, virtualp,
16742 : : memfn_quals != TYPE_UNQUALIFIED,
16743 : : inlinep, friendp, raises != NULL_TREE,
16744 : : declspecs->locations);
16745 : :
16746 : 56872624 : if (ctype)
16747 : : {
16748 : 387558 : DECL_CONTEXT (decl) = ctype;
16749 : 387558 : if (staticp == 1)
16750 : : {
16751 : 6 : permerror (declspecs->locations[ds_storage_class],
16752 : : "%<static%> may not be used when defining "
16753 : : "(as opposed to declaring) a static data member");
16754 : 6 : staticp = 0;
16755 : 6 : storage_class = sc_none;
16756 : : }
16757 : 387558 : if (storage_class == sc_register && TREE_STATIC (decl))
16758 : : {
16759 : 0 : error ("static member %qD declared %<register%>", decl);
16760 : 0 : storage_class = sc_none;
16761 : : }
16762 : 387558 : if (storage_class == sc_extern && pedantic)
16763 : : {
16764 : 0 : pedwarn (input_location, OPT_Wpedantic,
16765 : : "cannot explicitly declare member %q#D to have "
16766 : : "extern linkage", decl);
16767 : 0 : storage_class = sc_none;
16768 : : }
16769 : : }
16770 : 56485066 : else if (constexpr_p && DECL_EXTERNAL (decl))
16771 : : {
16772 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
16773 : : "declaration of %<constexpr%> variable %qD "
16774 : : "is not a definition", decl);
16775 : 12 : constexpr_p = false;
16776 : : }
16777 : 56872624 : if (consteval_p)
16778 : : {
16779 : 13 : error_at (DECL_SOURCE_LOCATION (decl),
16780 : : "a variable cannot be declared %<consteval%>");
16781 : 13 : consteval_p = false;
16782 : : }
16783 : :
16784 : 56872624 : if (inlinep)
16785 : 3986109 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16786 : 56872624 : if (innermost_code == cdk_decomp)
16787 : : {
16788 : 86332 : gcc_assert (declarator && declarator->kind == cdk_decomp);
16789 : 86332 : DECL_SOURCE_LOCATION (decl) = id_loc;
16790 : 86332 : DECL_ARTIFICIAL (decl) = 1;
16791 : 86332 : fit_decomposition_lang_decl (decl, NULL_TREE);
16792 : : }
16793 : : }
16794 : :
16795 : 531167266 : if (VAR_P (decl) && !initialized)
16796 : 6839055 : if (tree auto_node = type_uses_auto (type))
16797 : 94 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16798 : : {
16799 : 18 : location_t loc = declspecs->locations[ds_type_spec];
16800 : 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
16801 : 18 : TREE_TYPE (decl) = error_mark_node;
16802 : : }
16803 : :
16804 : 531167266 : if (storage_class == sc_extern && initialized && !funcdef_flag)
16805 : : {
16806 : 260 : if (toplevel_bindings_p ())
16807 : : {
16808 : : /* It's common practice (and completely valid) to have a const
16809 : : be initialized and declared extern. */
16810 : 236 : if (!(type_quals & TYPE_QUAL_CONST))
16811 : 19 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
16812 : : "%qs initialized and declared %<extern%>", name);
16813 : : }
16814 : : else
16815 : : {
16816 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
16817 : : "%qs has both %<extern%> and initializer", name);
16818 : 24 : return error_mark_node;
16819 : : }
16820 : : }
16821 : :
16822 : : /* Record `register' declaration for warnings on &
16823 : : and in case doing stupid register allocation. */
16824 : :
16825 : 531167242 : if (storage_class == sc_register)
16826 : : {
16827 : 2060 : DECL_REGISTER (decl) = 1;
16828 : : /* Warn about register storage specifiers on PARM_DECLs. */
16829 : 2060 : if (TREE_CODE (decl) == PARM_DECL)
16830 : : {
16831 : 35 : if (cxx_dialect >= cxx17)
16832 : 22 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16833 : : "ISO C++17 does not allow %<register%> storage "
16834 : : "class specifier");
16835 : : else
16836 : 13 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16837 : : "%<register%> storage class specifier used");
16838 : : }
16839 : : }
16840 : 531165182 : else if (storage_class == sc_extern)
16841 : 31710746 : DECL_THIS_EXTERN (decl) = 1;
16842 : 499454436 : else if (storage_class == sc_static)
16843 : 19483385 : DECL_THIS_STATIC (decl) = 1;
16844 : :
16845 : 531167242 : if (VAR_P (decl))
16846 : : {
16847 : : /* Set constexpr flag on vars (functions got it in grokfndecl). */
16848 : 67393071 : if (constexpr_p)
16849 : 13875802 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
16850 : : /* And the constinit flag (which only applies to variables). */
16851 : 53517269 : else if (constinit_p)
16852 : 436 : DECL_DECLARED_CONSTINIT_P (decl) = true;
16853 : : }
16854 : 463774171 : else if (TREE_CODE (decl) == FUNCTION_DECL)
16855 : : {
16856 : : /* If we saw a return type, record its location. */
16857 : 169699190 : location_t loc = declspecs->locations[ds_type_spec];
16858 : 169699190 : if (loc == UNKNOWN_LOCATION)
16859 : : /* Build DECL_RESULT in start_preparsed_function. */;
16860 : 140060704 : else if (!DECL_RESULT (decl))
16861 : : {
16862 : 140024562 : tree restype = TREE_TYPE (TREE_TYPE (decl));
16863 : 140024562 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
16864 : 140024562 : DECL_ARTIFICIAL (resdecl) = 1;
16865 : 140024562 : DECL_IGNORED_P (resdecl) = 1;
16866 : 140024562 : DECL_RESULT (decl) = resdecl;
16867 : : }
16868 : 36142 : else if (funcdef_flag)
16869 : 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
16870 : : }
16871 : :
16872 : : /* Record constancy and volatility on the DECL itself . There's
16873 : : no need to do this when processing a template; we'll do this
16874 : : for the instantiated declaration based on the type of DECL. */
16875 : 531167242 : if (!processing_template_decl
16876 : : /* Don't do it for instantiated variable templates either,
16877 : : cp_apply_type_quals_to_decl should have been called on it
16878 : : already and might have been overridden in cp_finish_decl
16879 : : if initializer needs runtime initialization. */
16880 : 531167242 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
16881 : 181080088 : cp_apply_type_quals_to_decl (type_quals, decl);
16882 : :
16883 : 531167242 : return decl;
16884 : : }
16885 : 922611166 : }
16886 : :
16887 : : /* Subroutine of start_function. Ensure that each of the parameter
16888 : : types (as listed in PARMS) is complete, as is required for a
16889 : : function definition. */
16890 : :
16891 : : static void
16892 : 142532430 : require_complete_types_for_parms (tree parms)
16893 : : {
16894 : 422689488 : for (; parms; parms = DECL_CHAIN (parms))
16895 : : {
16896 : 280157058 : if (dependent_type_p (TREE_TYPE (parms)))
16897 : 162175782 : continue;
16898 : 117981276 : if (!VOID_TYPE_P (TREE_TYPE (parms))
16899 : 117981276 : && complete_type_or_else (TREE_TYPE (parms), parms))
16900 : : {
16901 : 117981195 : relayout_decl (parms);
16902 : 117981195 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
16903 : :
16904 : 117981195 : abstract_virtuals_error (parms, TREE_TYPE (parms));
16905 : 117981195 : maybe_warn_parm_abi (TREE_TYPE (parms),
16906 : 117981195 : DECL_SOURCE_LOCATION (parms));
16907 : : }
16908 : : else
16909 : : /* grokparms or complete_type_or_else will have already issued
16910 : : an error. */
16911 : 81 : TREE_TYPE (parms) = error_mark_node;
16912 : : }
16913 : 142532430 : }
16914 : :
16915 : : /* Returns nonzero if T is a local variable. */
16916 : :
16917 : : int
16918 : 152056617 : local_variable_p (const_tree t)
16919 : : {
16920 : 152056617 : if ((VAR_P (t)
16921 : 67582649 : && (DECL_LOCAL_DECL_P (t)
16922 : 67582649 : || !DECL_CONTEXT (t)
16923 : 67540154 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
16924 : 155646805 : || (TREE_CODE (t) == PARM_DECL))
16925 : 136352880 : return 1;
16926 : :
16927 : : return 0;
16928 : : }
16929 : :
16930 : : /* Like local_variable_p, but suitable for use as a tree-walking
16931 : : function. */
16932 : :
16933 : : static tree
16934 : 4990133 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
16935 : : void * /*data*/)
16936 : : {
16937 : 4990133 : if (unevaluated_p (TREE_CODE (*tp)))
16938 : : {
16939 : : /* DR 2082 permits local variables in unevaluated contexts
16940 : : within a default argument. */
16941 : 15 : *walk_subtrees = 0;
16942 : 15 : return NULL_TREE;
16943 : : }
16944 : :
16945 : 4990118 : if (local_variable_p (*tp)
16946 : 4990118 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
16947 : : return *tp;
16948 : 4990118 : else if (TYPE_P (*tp))
16949 : 30 : *walk_subtrees = 0;
16950 : :
16951 : : return NULL_TREE;
16952 : : }
16953 : :
16954 : : /* Check that ARG, which is a default-argument expression for a
16955 : : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
16956 : : something goes wrong. DECL may also be a _TYPE node, rather than a
16957 : : DECL, if there is no DECL available. */
16958 : :
16959 : : tree
16960 : 2568518 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
16961 : : {
16962 : 2568518 : tree var;
16963 : 2568518 : tree decl_type;
16964 : :
16965 : 2568518 : if (TREE_CODE (arg) == DEFERRED_PARSE)
16966 : : /* We get a DEFERRED_PARSE when looking at an in-class declaration
16967 : : with a default argument. Ignore the argument for now; we'll
16968 : : deal with it after the class is complete. */
16969 : : return arg;
16970 : :
16971 : 1852707 : if (TYPE_P (decl))
16972 : : {
16973 : : decl_type = decl;
16974 : : decl = NULL_TREE;
16975 : : }
16976 : : else
16977 : 559106 : decl_type = TREE_TYPE (decl);
16978 : :
16979 : 1852707 : if (arg == error_mark_node
16980 : 1852589 : || decl == error_mark_node
16981 : 1852580 : || TREE_TYPE (arg) == error_mark_node
16982 : 3705287 : || decl_type == error_mark_node)
16983 : : /* Something already went wrong. There's no need to check
16984 : : further. */
16985 : : return error_mark_node;
16986 : :
16987 : : /* [dcl.fct.default]
16988 : :
16989 : : A default argument expression is implicitly converted to the
16990 : : parameter type. */
16991 : 1852580 : ++cp_unevaluated_operand;
16992 : : /* Avoid digest_init clobbering the initializer. */
16993 : 1852580 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
16994 : 1852580 : perform_implicit_conversion_flags (decl_type, carg, complain,
16995 : : LOOKUP_IMPLICIT);
16996 : 1852580 : --cp_unevaluated_operand;
16997 : :
16998 : : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
16999 : : the call sites. */
17000 : 1373404 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
17001 : 479191 : && null_ptr_cst_p (arg)
17002 : : /* Don't lose side-effects as in PR90473. */
17003 : 2219373 : && !TREE_SIDE_EFFECTS (arg))
17004 : 366790 : return nullptr_node;
17005 : :
17006 : : /* [dcl.fct.default]
17007 : :
17008 : : Local variables shall not be used in default argument
17009 : : expressions.
17010 : :
17011 : : The keyword `this' shall not be used in a default argument of a
17012 : : member function. */
17013 : 1485790 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
17014 : 1485790 : if (var)
17015 : : {
17016 : 0 : if (complain & tf_warning_or_error)
17017 : : {
17018 : 0 : if (DECL_NAME (var) == this_identifier)
17019 : 0 : permerror (input_location, "default argument %qE uses %qD",
17020 : : arg, var);
17021 : : else
17022 : 0 : error ("default argument %qE uses local variable %qD", arg, var);
17023 : : }
17024 : 0 : return error_mark_node;
17025 : : }
17026 : :
17027 : : /* All is well. */
17028 : 1485790 : return arg;
17029 : : }
17030 : :
17031 : : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
17032 : :
17033 : : static tree
17034 : 263457100 : type_is_deprecated (tree type)
17035 : : {
17036 : 388702761 : enum tree_code code;
17037 : 388702761 : if (TREE_DEPRECATED (type))
17038 : : return type;
17039 : 388660872 : if (TYPE_NAME (type))
17040 : : {
17041 : 259177512 : if (TREE_DEPRECATED (TYPE_NAME (type)))
17042 : : return type;
17043 : : else
17044 : : {
17045 : 259177506 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17046 : 259177506 : return NULL_TREE;
17047 : : }
17048 : : }
17049 : :
17050 : : /* Do warn about using typedefs to a deprecated class. */
17051 : 129483360 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17052 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17053 : :
17054 : 129483360 : code = TREE_CODE (type);
17055 : :
17056 : 129483360 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17057 : 129483360 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17058 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
17059 : 125131383 : return type_is_deprecated (TREE_TYPE (type));
17060 : :
17061 : 114278 : if (TYPE_PTRMEMFUNC_P (type))
17062 : 114278 : return type_is_deprecated
17063 : 114278 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17064 : :
17065 : : return NULL_TREE;
17066 : : }
17067 : :
17068 : : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
17069 : :
17070 : : static tree
17071 : 264352198 : type_is_unavailable (tree type)
17072 : : {
17073 : 390147996 : enum tree_code code;
17074 : 390147996 : if (TREE_UNAVAILABLE (type))
17075 : : return type;
17076 : 390147990 : if (TYPE_NAME (type))
17077 : : {
17078 : 260084885 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
17079 : : return type;
17080 : : else
17081 : : {
17082 : 260084879 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
17083 : 260084879 : return NULL_TREE;
17084 : : }
17085 : : }
17086 : :
17087 : : /* Do warn about using typedefs to a deprecated class. */
17088 : 130063105 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
17089 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
17090 : :
17091 : 130063105 : code = TREE_CODE (type);
17092 : :
17093 : 130063105 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
17094 : 130063105 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
17095 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
17096 : 125573640 : return type_is_unavailable (TREE_TYPE (type));
17097 : :
17098 : 222158 : if (TYPE_PTRMEMFUNC_P (type))
17099 : 222158 : return type_is_unavailable
17100 : 222158 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17101 : :
17102 : : return NULL_TREE;
17103 : : }
17104 : :
17105 : : /* Decode the list of parameter types for a function type.
17106 : : Given the list of things declared inside the parens,
17107 : : return a list of types.
17108 : :
17109 : : If this parameter does not end with an ellipsis, we append
17110 : : void_list_node.
17111 : :
17112 : : *PARMS is set to the chain of PARM_DECLs created. */
17113 : :
17114 : : tree
17115 : 177234037 : grokparms (tree parmlist, tree *parms)
17116 : : {
17117 : 177234037 : tree result = NULL_TREE;
17118 : 177234037 : tree decls = NULL_TREE;
17119 : 177234037 : tree parm;
17120 : 177234037 : int any_error = 0;
17121 : :
17122 : 442202386 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
17123 : : {
17124 : 440841024 : tree type = NULL_TREE;
17125 : 440841024 : tree init = TREE_PURPOSE (parm);
17126 : 440841024 : tree decl = TREE_VALUE (parm);
17127 : :
17128 : 440841024 : if (parm == void_list_node || parm == explicit_void_list_node)
17129 : : break;
17130 : :
17131 : 264968361 : if (! decl || TREE_TYPE (decl) == error_mark_node)
17132 : : {
17133 : 185 : any_error = 1;
17134 : 185 : continue;
17135 : : }
17136 : :
17137 : 264968176 : type = TREE_TYPE (decl);
17138 : 264968176 : if (VOID_TYPE_P (type))
17139 : : {
17140 : 78 : if (same_type_p (type, void_type_node)
17141 : 69 : && !init
17142 : 57 : && !DECL_NAME (decl) && !result
17143 : 102 : && TREE_CHAIN (parm) == void_list_node)
17144 : : /* DR 577: A parameter list consisting of a single
17145 : : unnamed parameter of non-dependent type 'void'. */
17146 : : break;
17147 : 66 : else if (cv_qualified_p (type))
17148 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17149 : : "invalid use of cv-qualified type %qT in "
17150 : : "parameter declaration", type);
17151 : : else
17152 : 57 : error_at (DECL_SOURCE_LOCATION (decl),
17153 : : "invalid use of type %<void%> in parameter "
17154 : : "declaration");
17155 : : /* It's not a good idea to actually create parameters of
17156 : : type `void'; other parts of the compiler assume that a
17157 : : void type terminates the parameter list. */
17158 : 66 : type = error_mark_node;
17159 : 66 : TREE_TYPE (decl) = error_mark_node;
17160 : : }
17161 : :
17162 : 264968164 : if (type != error_mark_node)
17163 : : {
17164 : 264968098 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17165 : : {
17166 : 264352198 : tree unavailtype = type_is_unavailable (type);
17167 : 264352198 : if (unavailtype)
17168 : 12 : cp_handle_deprecated_or_unavailable (unavailtype);
17169 : : }
17170 : 264968098 : if (deprecated_state != DEPRECATED_SUPPRESS
17171 : 264968098 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
17172 : : {
17173 : 263457100 : tree deptype = type_is_deprecated (type);
17174 : 263457100 : if (deptype)
17175 : 41895 : cp_handle_deprecated_or_unavailable (deptype);
17176 : : }
17177 : :
17178 : : /* [dcl.fct] "A parameter with volatile-qualified type is
17179 : : deprecated." */
17180 : 264968098 : if (CP_TYPE_VOLATILE_P (type))
17181 : 504 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
17182 : : "%<volatile%>-qualified parameter is "
17183 : : "deprecated");
17184 : :
17185 : : /* Top-level qualifiers on the parameters are
17186 : : ignored for function types. */
17187 : 264968098 : type = cp_build_qualified_type (type, 0);
17188 : 264968098 : if (TREE_CODE (type) == METHOD_TYPE)
17189 : : {
17190 : 0 : error ("parameter %qD invalidly declared method type", decl);
17191 : 0 : type = build_pointer_type (type);
17192 : 0 : TREE_TYPE (decl) = type;
17193 : : }
17194 : 264968098 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
17195 : : {
17196 : : /* Before C++17 DR 393:
17197 : : [dcl.fct]/6, parameter types cannot contain pointers
17198 : : (references) to arrays of unknown bound. */
17199 : 1573917 : tree t = TREE_TYPE (type);
17200 : 1573917 : int ptr = TYPE_PTR_P (type);
17201 : :
17202 : 1727231 : while (1)
17203 : : {
17204 : 1650574 : if (TYPE_PTR_P (t))
17205 : : ptr = 1;
17206 : 1577021 : else if (TREE_CODE (t) != ARRAY_TYPE)
17207 : : break;
17208 : 3114 : else if (!TYPE_DOMAIN (t))
17209 : : break;
17210 : 76657 : t = TREE_TYPE (t);
17211 : : }
17212 : 1573917 : if (TREE_CODE (t) == ARRAY_TYPE)
17213 : 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
17214 : : ptr
17215 : : ? G_("parameter %qD includes pointer to array of "
17216 : : "unknown bound %qT")
17217 : : : G_("parameter %qD includes reference to array of "
17218 : : "unknown bound %qT"),
17219 : : decl, t);
17220 : : }
17221 : :
17222 : 264968098 : if (init && !processing_template_decl)
17223 : 1274908 : init = check_default_argument (decl, init, tf_warning_or_error);
17224 : : }
17225 : :
17226 : 264968164 : DECL_CHAIN (decl) = decls;
17227 : 264968164 : decls = decl;
17228 : 264968164 : result = tree_cons (init, type, result);
17229 : : }
17230 : 177234037 : decls = nreverse (decls);
17231 : 177234037 : result = nreverse (result);
17232 : 177234037 : if (parm)
17233 : 175872675 : result = chainon (result, void_list_node);
17234 : 177234037 : *parms = decls;
17235 : 177234037 : if (any_error)
17236 : 171 : result = NULL_TREE;
17237 : :
17238 : 171 : if (any_error)
17239 : : /* We had parm errors, recover by giving the function (...) type. */
17240 : 171 : result = NULL_TREE;
17241 : :
17242 : 177234037 : return result;
17243 : : }
17244 : :
17245 : :
17246 : : /* D is a constructor or overloaded `operator='.
17247 : :
17248 : : Let T be the class in which D is declared. Then, this function
17249 : : returns:
17250 : :
17251 : : -1 if D's is an ill-formed constructor or copy assignment operator
17252 : : whose first parameter is of type `T'.
17253 : : 0 if D is not a copy constructor or copy assignment
17254 : : operator.
17255 : : 1 if D is a copy constructor or copy assignment operator whose
17256 : : first parameter is a reference to non-const qualified T.
17257 : : 2 if D is a copy constructor or copy assignment operator whose
17258 : : first parameter is a reference to const qualified T.
17259 : :
17260 : : This function can be used as a predicate. Positive values indicate
17261 : : a copy constructor and nonzero values indicate a copy assignment
17262 : : operator. */
17263 : :
17264 : : int
17265 : 287018764 : copy_fn_p (const_tree d)
17266 : : {
17267 : 287018764 : tree args;
17268 : 287018764 : tree arg_type;
17269 : 287018764 : int result = 1;
17270 : :
17271 : 287018764 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
17272 : :
17273 : 287018764 : if (TREE_CODE (d) == TEMPLATE_DECL
17274 : 287018764 : || (DECL_TEMPLATE_INFO (d)
17275 : 138102616 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17276 : : /* Instantiations of template member functions are never copy
17277 : : functions. Note that member functions of templated classes are
17278 : : represented as template functions internally, and we must
17279 : : accept those as copy functions. */
17280 : : return 0;
17281 : :
17282 : 241458754 : if (!DECL_CONSTRUCTOR_P (d)
17283 : 241458754 : && DECL_NAME (d) != assign_op_identifier)
17284 : : return 0;
17285 : :
17286 : 240692460 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17287 : : {
17288 : 154 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17289 : 154 : if (!TYPE_REF_P (object_param)
17290 : 148 : || TYPE_REF_IS_RVALUE (object_param)
17291 : : /* Reject unrelated object parameters. */
17292 : 136 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17293 : 290 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17294 : 24 : return 0;
17295 : 130 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17296 : : }
17297 : : else
17298 : 240692306 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17299 : 240692436 : if (!args)
17300 : : return 0;
17301 : :
17302 : 240684990 : arg_type = TREE_VALUE (args);
17303 : 240684990 : if (arg_type == error_mark_node)
17304 : : return 0;
17305 : :
17306 : 240684955 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
17307 : : {
17308 : : /* Pass by value copy assignment operator. */
17309 : : result = -1;
17310 : : }
17311 : 240682869 : else if (TYPE_REF_P (arg_type)
17312 : 148414670 : && !TYPE_REF_IS_RVALUE (arg_type)
17313 : 336498921 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
17314 : : {
17315 : 75508332 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
17316 : 75419964 : result = 2;
17317 : : }
17318 : : else
17319 : : return 0;
17320 : :
17321 : 75510418 : args = TREE_CHAIN (args);
17322 : :
17323 : 78089813 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17324 : : /* There are more non-optional args. */
17325 : : return 0;
17326 : :
17327 : : return result;
17328 : : }
17329 : :
17330 : : /* D is a constructor or overloaded `operator='.
17331 : :
17332 : : Let T be the class in which D is declared. Then, this function
17333 : : returns true when D is a move constructor or move assignment
17334 : : operator, false otherwise. */
17335 : :
17336 : : bool
17337 : 116318154 : move_fn_p (const_tree d)
17338 : : {
17339 : 116318154 : if (cxx_dialect == cxx98)
17340 : : /* There are no move constructors if we are in C++98 mode. */
17341 : : return false;
17342 : :
17343 : 116256334 : if (TREE_CODE (d) == TEMPLATE_DECL
17344 : 116256334 : || (DECL_TEMPLATE_INFO (d)
17345 : 52744592 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
17346 : : /* Instantiations of template member functions are never move
17347 : : functions. Note that member functions of templated classes are
17348 : : represented as template functions internally, and we must
17349 : : accept those as move functions. */
17350 : : return 0;
17351 : :
17352 : 96004977 : return move_signature_fn_p (d);
17353 : : }
17354 : :
17355 : : /* D is a constructor or overloaded `operator='.
17356 : :
17357 : : Then, this function returns true when D has the same signature as a move
17358 : : constructor or move assignment operator (because either it is such a
17359 : : ctor/op= or it is a template specialization with the same signature),
17360 : : false otherwise. */
17361 : :
17362 : : bool
17363 : 96004977 : move_signature_fn_p (const_tree d)
17364 : : {
17365 : 96004977 : tree args;
17366 : 96004977 : tree arg_type;
17367 : 96004977 : bool result = false;
17368 : :
17369 : 96004977 : if (!DECL_CONSTRUCTOR_P (d)
17370 : 96004977 : && DECL_NAME (d) != assign_op_identifier)
17371 : : return 0;
17372 : :
17373 : 96004913 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
17374 : : {
17375 : 88 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
17376 : 88 : if (!TYPE_REF_P (object_param)
17377 : 84 : || TYPE_REF_IS_RVALUE (object_param)
17378 : : /* Reject unrelated object parameters. */
17379 : 76 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
17380 : 164 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
17381 : 16 : return 0;
17382 : 72 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
17383 : : }
17384 : : else
17385 : 96004825 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
17386 : 96004897 : if (!args)
17387 : : return 0;
17388 : :
17389 : 96002163 : arg_type = TREE_VALUE (args);
17390 : 96002163 : if (arg_type == error_mark_node)
17391 : : return 0;
17392 : :
17393 : 96002155 : if (TYPE_REF_P (arg_type)
17394 : 46980818 : && TYPE_REF_IS_RVALUE (arg_type)
17395 : 132612399 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
17396 : : DECL_CONTEXT (d)))
17397 : : result = true;
17398 : :
17399 : 96002155 : args = TREE_CHAIN (args);
17400 : :
17401 : 106656137 : if (args && args != void_list_node && !TREE_PURPOSE (args))
17402 : : /* There are more non-optional args. */
17403 : : return false;
17404 : :
17405 : : return result;
17406 : : }
17407 : :
17408 : : /* Remember any special properties of member function DECL. */
17409 : :
17410 : : void
17411 : 292262320 : grok_special_member_properties (tree decl)
17412 : : {
17413 : 292262320 : tree class_type;
17414 : :
17415 : 292262320 : if (TREE_CODE (decl) == USING_DECL
17416 : 292262320 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17417 : : return;
17418 : :
17419 : 274624556 : class_type = DECL_CONTEXT (decl);
17420 : 274624556 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
17421 : : {
17422 : 120740989 : int ctor = copy_fn_p (decl);
17423 : :
17424 : 120740989 : if (!DECL_ARTIFICIAL (decl))
17425 : 90987101 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
17426 : :
17427 : 120740989 : if (ctor > 0)
17428 : : {
17429 : : /* [class.copy]
17430 : :
17431 : : A non-template constructor for class X is a copy
17432 : : constructor if its first parameter is of type X&, const
17433 : : X&, volatile X& or const volatile X&, and either there
17434 : : are no other parameters or else all other parameters have
17435 : : default arguments. */
17436 : 23493021 : TYPE_HAS_COPY_CTOR (class_type) = 1;
17437 : 23493021 : if (ctor > 1)
17438 : 23462349 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
17439 : : }
17440 : :
17441 : 120740989 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
17442 : 24982288 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
17443 : :
17444 : 120740989 : if (is_list_ctor (decl))
17445 : 1427168 : TYPE_HAS_LIST_CTOR (class_type) = 1;
17446 : :
17447 : 120740989 : if (maybe_constexpr_fn (decl)
17448 : 120740989 : && !ctor && !move_fn_p (decl))
17449 : 30043023 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
17450 : : }
17451 : 153883567 : else if (DECL_NAME (decl) == assign_op_identifier)
17452 : : {
17453 : : /* [class.copy]
17454 : :
17455 : : A non-template assignment operator for class X is a copy
17456 : : assignment operator if its parameter is of type X, X&, const
17457 : : X&, volatile X& or const volatile X&. */
17458 : :
17459 : 13323001 : int assop = copy_fn_p (decl);
17460 : :
17461 : 13323001 : if (assop)
17462 : : {
17463 : 6652865 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
17464 : 6652865 : if (assop != 1)
17465 : 6623115 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
17466 : : }
17467 : : }
17468 : 140560566 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
17469 : 1926841 : TYPE_HAS_CONVERSION (class_type) = true;
17470 : :
17471 : : /* Destructors are handled in check_methods. */
17472 : : }
17473 : :
17474 : : /* Check a constructor DECL has the correct form. Complains
17475 : : if the class has a constructor of the form X(X). */
17476 : :
17477 : : bool
17478 : 42544855 : grok_ctor_properties (const_tree ctype, const_tree decl)
17479 : : {
17480 : 42544855 : int ctor_parm = copy_fn_p (decl);
17481 : :
17482 : 42544855 : if (ctor_parm < 0)
17483 : : {
17484 : : /* [class.copy]
17485 : :
17486 : : A declaration of a constructor for a class X is ill-formed if
17487 : : its first parameter is of type (optionally cv-qualified) X
17488 : : and either there are no other parameters or else all other
17489 : : parameters have default arguments.
17490 : :
17491 : : We *don't* complain about member template instantiations that
17492 : : have this form, though; they can occur as we try to decide
17493 : : what constructor to use during overload resolution. Since
17494 : : overload resolution will never prefer such a constructor to
17495 : : the non-template copy constructor (which is either explicitly
17496 : : or implicitly defined), there's no need to worry about their
17497 : : existence. Theoretically, they should never even be
17498 : : instantiated, but that's hard to forestall. */
17499 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17500 : : "invalid constructor; you probably meant %<%T (const %T&)%>",
17501 : : ctype, ctype);
17502 : 9 : return false;
17503 : : }
17504 : :
17505 : : return true;
17506 : : }
17507 : :
17508 : : /* DECL is a declaration for an overloaded or conversion operator. If
17509 : : COMPLAIN is true, errors are issued for invalid declarations. */
17510 : :
17511 : : bool
17512 : 51555311 : grok_op_properties (tree decl, bool complain)
17513 : : {
17514 : 51555311 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17515 : 51555311 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17516 : 51555311 : tree name = DECL_NAME (decl);
17517 : 51555311 : location_t loc = DECL_SOURCE_LOCATION (decl);
17518 : :
17519 : 51555311 : tree class_type = DECL_CONTEXT (decl);
17520 : 51555311 : if (class_type && !CLASS_TYPE_P (class_type))
17521 : : class_type = NULL_TREE;
17522 : :
17523 : 51555311 : tree_code operator_code;
17524 : 51555311 : unsigned op_flags;
17525 : 51555311 : if (IDENTIFIER_CONV_OP_P (name))
17526 : : {
17527 : : /* Conversion operators are TYPE_EXPR for the purposes of this
17528 : : function. */
17529 : : operator_code = TYPE_EXPR;
17530 : : op_flags = OVL_OP_FLAG_UNARY;
17531 : : }
17532 : : else
17533 : : {
17534 : 49964822 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17535 : :
17536 : 49964822 : operator_code = ovl_op->tree_code;
17537 : 49964822 : op_flags = ovl_op->flags;
17538 : 49964822 : gcc_checking_assert (operator_code != ERROR_MARK);
17539 : 49964822 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17540 : : }
17541 : :
17542 : 49964822 : if (op_flags & OVL_OP_FLAG_ALLOC)
17543 : : {
17544 : : /* operator new and operator delete are quite special. */
17545 : 331142 : if (class_type)
17546 : 13676 : switch (op_flags)
17547 : : {
17548 : 9703 : case OVL_OP_FLAG_ALLOC:
17549 : 9703 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17550 : 9703 : break;
17551 : :
17552 : 3706 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17553 : 3706 : TYPE_GETS_DELETE (class_type) |= 1;
17554 : 3706 : break;
17555 : :
17556 : 130 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17557 : 130 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17558 : 130 : break;
17559 : :
17560 : 137 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17561 : 137 : TYPE_GETS_DELETE (class_type) |= 2;
17562 : 137 : break;
17563 : :
17564 : 0 : default:
17565 : 0 : gcc_unreachable ();
17566 : : }
17567 : :
17568 : : /* [basic.std.dynamic.allocation]/1:
17569 : :
17570 : : A program is ill-formed if an allocation function is declared
17571 : : in a namespace scope other than global scope or declared
17572 : : static in global scope.
17573 : :
17574 : : The same also holds true for deallocation functions. */
17575 : 331142 : if (DECL_NAMESPACE_SCOPE_P (decl))
17576 : : {
17577 : 317466 : if (CP_DECL_CONTEXT (decl) != global_namespace)
17578 : : {
17579 : 6 : error_at (loc, "%qD may not be declared within a namespace",
17580 : : decl);
17581 : 6 : return false;
17582 : : }
17583 : :
17584 : 317460 : if (!TREE_PUBLIC (decl))
17585 : : {
17586 : 6 : error_at (loc, "%qD may not be declared as static", decl);
17587 : 6 : return false;
17588 : : }
17589 : : }
17590 : :
17591 : : /* Check for replaceable global new/delete operators with
17592 : : const std::nothrow_t & last argument, other replaceable global
17593 : : new/delete operators are marked in cxx_init_decl_processing. */
17594 : 331130 : if (CP_DECL_CONTEXT (decl) == global_namespace)
17595 : : {
17596 : 317454 : tree args = argtypes;
17597 : 317454 : if (args
17598 : 317442 : && args != void_list_node
17599 : 952320 : && same_type_p (TREE_VALUE (args),
17600 : : (op_flags & OVL_OP_FLAG_DELETE)
17601 : : ? ptr_type_node : size_type_node))
17602 : : {
17603 : 317412 : args = TREE_CHAIN (args);
17604 : 317412 : if (aligned_allocation_fn_p (decl))
17605 : 102324 : args = TREE_CHAIN (args);
17606 : 317412 : if (args
17607 : 317406 : && args != void_list_node
17608 : 529265 : && TREE_CHAIN (args) == void_list_node)
17609 : : {
17610 : 186228 : tree t = TREE_VALUE (args);
17611 : 186228 : if (TYPE_REF_P (t)
17612 : 105654 : && !TYPE_REF_IS_RVALUE (t)
17613 : 105654 : && (t = TREE_TYPE (t))
17614 : 105654 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
17615 : 291434 : && is_std_class (t, "nothrow_t"))
17616 : 105206 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
17617 : : }
17618 : : }
17619 : : }
17620 : :
17621 : 331130 : if (op_flags & OVL_OP_FLAG_DELETE)
17622 : : {
17623 : 187565 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
17624 : 187565 : coerce_delete_type (decl, loc);
17625 : : }
17626 : : else
17627 : : {
17628 : 143565 : DECL_SET_IS_OPERATOR_NEW (decl, true);
17629 : 143565 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
17630 : : }
17631 : :
17632 : 331130 : return true;
17633 : : }
17634 : :
17635 : : /* An operator function must either be a non-static member function
17636 : : or have at least one parameter of a class, a reference to a class,
17637 : : an enumeration, or a reference to an enumeration. 13.4.0.6 */
17638 : 51224169 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17639 : : {
17640 : 14841533 : if (operator_code == TYPE_EXPR
17641 : 14841533 : || operator_code == COMPONENT_REF
17642 : 14841512 : || operator_code == NOP_EXPR)
17643 : : {
17644 : 33 : error_at (loc, "%qD must be a non-static member function", decl);
17645 : 33 : return false;
17646 : : }
17647 : :
17648 : 14841500 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
17649 : : {
17650 : 2768 : if (! DECL_STATIC_FUNCTION_P (decl))
17651 : : {
17652 : 12 : error_at (loc, "%qD must be a member function", decl);
17653 : 12 : return false;
17654 : : }
17655 : 2756 : if (cxx_dialect < cxx23
17656 : : /* For lambdas we diagnose static lambda specifier elsewhere. */
17657 : 95 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
17658 : : /* For instantiations, we have diagnosed this already. */
17659 : 2788 : && ! DECL_USE_TEMPLATE (decl))
17660 : 26 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
17661 : : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
17662 : : decl);
17663 : 2756 : if (operator_code == ARRAY_REF)
17664 : : /* static operator[] should have exactly one argument
17665 : : for C++20 and earlier, so that it isn't multidimensional. */
17666 : : op_flags = OVL_OP_FLAG_UNARY;
17667 : : }
17668 : 14838732 : else if (DECL_STATIC_FUNCTION_P (decl))
17669 : : {
17670 : 9 : error_at (loc, "%qD must be either a non-static member "
17671 : : "function or a non-member function", decl);
17672 : 9 : return false;
17673 : : }
17674 : : else
17675 : 498671 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
17676 : : {
17677 : 15337394 : if (!arg || arg == void_list_node)
17678 : : {
17679 : 18 : if (complain)
17680 : 15 : error_at (loc, "%qD must have an argument of class or "
17681 : : "enumerated type", decl);
17682 : 18 : return false;
17683 : : }
17684 : :
17685 : 15337376 : tree type = non_reference (TREE_VALUE (arg));
17686 : 15337376 : if (type == error_mark_node)
17687 : : return false;
17688 : :
17689 : : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
17690 : : because these checks are performed even on template
17691 : : functions. */
17692 : 15337373 : if (MAYBE_CLASS_TYPE_P (type)
17693 : 1458050 : || TREE_CODE (type) == ENUMERAL_TYPE)
17694 : : break;
17695 : 498671 : }
17696 : : }
17697 : :
17698 : 51224058 : if (operator_code == CALL_EXPR)
17699 : : /* There are no further restrictions on the arguments to an overloaded
17700 : : "operator ()". */
17701 : : return true;
17702 : :
17703 : : /* C++23 allows an arbitrary number of parameters and default arguments for
17704 : : operator[], and none of the other checks below apply. */
17705 : 44631684 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
17706 : : return true;
17707 : :
17708 : 44262670 : if (operator_code == COND_EXPR)
17709 : : {
17710 : : /* 13.4.0.3 */
17711 : 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
17712 : 6 : return false;
17713 : : }
17714 : :
17715 : : /* Count the number of arguments and check for ellipsis. */
17716 : : int arity = 0;
17717 : 126837968 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
17718 : : {
17719 : 82575319 : if (!arg)
17720 : : {
17721 : 15 : error_at (loc, "%qD must not have variable number of arguments",
17722 : : decl);
17723 : 15 : return false;
17724 : : }
17725 : 82575304 : ++arity;
17726 : : }
17727 : : /* FIXME: We need tests for these errors with xobj member functions. */
17728 : : /* Verify correct number of arguments. */
17729 : 44262649 : switch (op_flags)
17730 : : {
17731 : 7720167 : case OVL_OP_FLAG_AMBIARY:
17732 : 7720167 : if (arity == 1)
17733 : : {
17734 : : /* We have a unary instance of an ambi-ary op. Remap to the
17735 : : unary one. */
17736 : 3429986 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
17737 : 3429986 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
17738 : 3429986 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
17739 : 3429986 : operator_code = ovl_op->tree_code;
17740 : 3429986 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17741 : : }
17742 : 4290181 : else if (arity != 2)
17743 : : {
17744 : : /* This was an ambiguous operator but is invalid. */
17745 : 3 : error_at (loc,
17746 : : methodp
17747 : : ? G_("%qD must have either zero or one argument")
17748 : : : G_("%qD must have either one or two arguments"), decl);
17749 : 3 : return false;
17750 : : }
17751 : 4290178 : else if ((operator_code == POSTINCREMENT_EXPR
17752 : 4290178 : || operator_code == POSTDECREMENT_EXPR)
17753 : 2077884 : && ! processing_template_decl
17754 : : /* x++ and x--'s second argument must be an int. */
17755 : 5661946 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
17756 : : integer_type_node))
17757 : : {
17758 : 15 : error_at (loc,
17759 : : methodp
17760 : : ? G_("postfix %qD must have %<int%> as its argument")
17761 : : : G_("postfix %qD must have %<int%> as its second argument"),
17762 : : decl);
17763 : 15 : return false;
17764 : : }
17765 : : break;
17766 : :
17767 : 2520038 : case OVL_OP_FLAG_UNARY:
17768 : 2520038 : if (arity != 1)
17769 : : {
17770 : 12 : error_at (loc,
17771 : : methodp
17772 : : ? G_("%qD must have no arguments")
17773 : : : G_("%qD must have exactly one argument"), decl);
17774 : 12 : return false;
17775 : : }
17776 : : break;
17777 : :
17778 : 34022444 : case OVL_OP_FLAG_BINARY:
17779 : 34022444 : if (arity != 2)
17780 : : {
17781 : 3 : error_at (loc,
17782 : : methodp
17783 : : ? G_("%qD must have exactly one argument")
17784 : : : G_("%qD must have exactly two arguments"), decl);
17785 : 3 : return false;
17786 : : }
17787 : : break;
17788 : :
17789 : 0 : default:
17790 : 0 : gcc_unreachable ();
17791 : : }
17792 : :
17793 : : /* There can be no default arguments. */
17794 : 126837821 : for (tree arg = argtypes; arg && arg != void_list_node;
17795 : 82575205 : arg = TREE_CHAIN (arg))
17796 : 82575220 : if (TREE_PURPOSE (arg))
17797 : : {
17798 : 15 : TREE_PURPOSE (arg) = NULL_TREE;
17799 : 15 : error_at (loc, "%qD cannot have default arguments", decl);
17800 : 15 : return false;
17801 : : }
17802 : :
17803 : : /* At this point the declaration is well-formed. It may not be
17804 : : sensible though. */
17805 : :
17806 : : /* Check member function warnings only on the in-class declaration.
17807 : : There's no point warning on an out-of-class definition. */
17808 : 44262601 : if (class_type && class_type != current_class_type)
17809 : : return true;
17810 : :
17811 : : /* Warn about conversion operators that will never be used. */
17812 : 42319151 : if (IDENTIFIER_CONV_OP_P (name)
17813 : 1590083 : && ! DECL_TEMPLATE_INFO (decl)
17814 : 42951319 : && warn_class_conversion)
17815 : : {
17816 : 632135 : tree t = TREE_TYPE (name);
17817 : 632135 : int ref = TYPE_REF_P (t);
17818 : :
17819 : 632135 : if (ref)
17820 : 21329 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
17821 : :
17822 : 632135 : if (VOID_TYPE_P (t))
17823 : 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
17824 : : "will never use a type conversion operator", class_type);
17825 : 632126 : else if (class_type)
17826 : : {
17827 : 632126 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
17828 : 24 : warning_at (loc, OPT_Wclass_conversion,
17829 : : ref
17830 : : ? G_("converting %qT to a reference to the same type "
17831 : : "will never use a type conversion operator")
17832 : : : G_("converting %qT to the same type "
17833 : : "will never use a type conversion operator"),
17834 : : class_type);
17835 : : /* Don't force t to be complete here. */
17836 : 632111 : else if (MAYBE_CLASS_TYPE_P (t)
17837 : 278747 : && COMPLETE_TYPE_P (t)
17838 : 60921 : && DERIVED_FROM_P (t, class_type))
17839 : 30 : warning_at (loc, OPT_Wclass_conversion,
17840 : : ref
17841 : : ? G_("converting %qT to a reference to a base class "
17842 : : "%qT will never use a type conversion operator")
17843 : : : G_("converting %qT to a base class %qT "
17844 : : "will never use a type conversion operator"),
17845 : : class_type, t);
17846 : : }
17847 : : }
17848 : :
17849 : 42319151 : if (!warn_ecpp)
17850 : : return true;
17851 : :
17852 : : /* Effective C++ rules below. */
17853 : :
17854 : : /* More Effective C++ rule 7. */
17855 : 54 : if (operator_code == TRUTH_ANDIF_EXPR
17856 : 54 : || operator_code == TRUTH_ORIF_EXPR
17857 : 54 : || operator_code == COMPOUND_EXPR)
17858 : 0 : warning_at (loc, OPT_Weffc__,
17859 : : "user-defined %qD always evaluates both arguments", decl);
17860 : :
17861 : : /* More Effective C++ rule 6. */
17862 : 54 : if (operator_code == POSTINCREMENT_EXPR
17863 : : || operator_code == POSTDECREMENT_EXPR
17864 : : || operator_code == PREINCREMENT_EXPR
17865 : 54 : || operator_code == PREDECREMENT_EXPR)
17866 : : {
17867 : 0 : tree arg = TREE_VALUE (argtypes);
17868 : 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
17869 : 0 : if (methodp || TYPE_REF_P (arg))
17870 : 0 : arg = TREE_TYPE (arg);
17871 : 0 : arg = TYPE_MAIN_VARIANT (arg);
17872 : :
17873 : 0 : if (operator_code == PREINCREMENT_EXPR
17874 : : || operator_code == PREDECREMENT_EXPR)
17875 : : {
17876 : 0 : if (!TYPE_REF_P (ret)
17877 : 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
17878 : 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
17879 : : build_reference_type (arg));
17880 : : }
17881 : : else
17882 : : {
17883 : 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
17884 : 0 : warning_at (loc, OPT_Weffc__,
17885 : : "postfix %qD should return %qT", decl, arg);
17886 : : }
17887 : : }
17888 : :
17889 : : /* Effective C++ rule 23. */
17890 : 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
17891 : : && (operator_code == PLUS_EXPR
17892 : 9 : || operator_code == MINUS_EXPR
17893 : 9 : || operator_code == TRUNC_DIV_EXPR
17894 : : || operator_code == MULT_EXPR
17895 : : || operator_code == TRUNC_MOD_EXPR)
17896 : 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
17897 : 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
17898 : :
17899 : : return true;
17900 : : }
17901 : :
17902 : : /* Return a string giving the keyword associate with CODE. */
17903 : :
17904 : : static const char *
17905 : 111 : tag_name (enum tag_types code)
17906 : : {
17907 : 111 : switch (code)
17908 : : {
17909 : : case record_type:
17910 : : return "struct";
17911 : 60 : case class_type:
17912 : 60 : return "class";
17913 : 0 : case union_type:
17914 : 0 : return "union";
17915 : 3 : case enum_type:
17916 : 3 : return "enum";
17917 : 0 : case typename_type:
17918 : 0 : return "typename";
17919 : 0 : default:
17920 : 0 : gcc_unreachable ();
17921 : : }
17922 : : }
17923 : :
17924 : : /* Name lookup in an elaborated-type-specifier (after the keyword
17925 : : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
17926 : : elaborated-type-specifier is invalid, issue a diagnostic and return
17927 : : error_mark_node; otherwise, return the *_TYPE to which it referred.
17928 : : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
17929 : :
17930 : : tree
17931 : 11351034 : check_elaborated_type_specifier (enum tag_types tag_code,
17932 : : tree decl,
17933 : : bool allow_template_p)
17934 : : {
17935 : 11351034 : tree type;
17936 : :
17937 : : /* In the case of:
17938 : :
17939 : : struct S { struct S *p; };
17940 : :
17941 : : name lookup will find the TYPE_DECL for the implicit "S::S"
17942 : : typedef. Adjust for that here. */
17943 : 11351034 : if (DECL_SELF_REFERENCE_P (decl))
17944 : 336181 : decl = TYPE_NAME (TREE_TYPE (decl));
17945 : :
17946 : 11351034 : type = TREE_TYPE (decl);
17947 : :
17948 : : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
17949 : : is false for this case as well. */
17950 : 11351034 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17951 : : {
17952 : 9 : error ("using template type parameter %qT after %qs",
17953 : : type, tag_name (tag_code));
17954 : 9 : return error_mark_node;
17955 : : }
17956 : : /* Accept template template parameters. */
17957 : 11351025 : else if (allow_template_p
17958 : 6493563 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
17959 : 6493551 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
17960 : : ;
17961 : : /* [dcl.type.elab]
17962 : :
17963 : : If the identifier resolves to a typedef-name or the
17964 : : simple-template-id resolves to an alias template
17965 : : specialization, the elaborated-type-specifier is ill-formed.
17966 : :
17967 : : In other words, the only legitimate declaration to use in the
17968 : : elaborated type specifier is the implicit typedef created when
17969 : : the type is declared. */
17970 : 11351010 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
17971 : 336297 : && !DECL_SELF_REFERENCE_P (decl)
17972 : 11351126 : && tag_code != typename_type)
17973 : : {
17974 : 78 : auto_diagnostic_group d;
17975 : 78 : if (alias_template_specialization_p (type, nt_opaque))
17976 : 21 : error ("using alias template specialization %qT after %qs",
17977 : : type, tag_name (tag_code));
17978 : : else
17979 : 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
17980 : 78 : inform (DECL_SOURCE_LOCATION (decl),
17981 : : "%qD has a previous declaration here", decl);
17982 : 78 : return error_mark_node;
17983 : 78 : }
17984 : 11350932 : else if (TREE_CODE (type) != RECORD_TYPE
17985 : 11350932 : && TREE_CODE (type) != UNION_TYPE
17986 : : && tag_code != enum_type
17987 : 63635 : && tag_code != typename_type)
17988 : : {
17989 : 6 : auto_diagnostic_group d;
17990 : 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
17991 : 6 : inform (location_of (type), "%qT has a previous declaration here", type);
17992 : 6 : return error_mark_node;
17993 : 6 : }
17994 : 11350926 : else if (TREE_CODE (type) != ENUMERAL_TYPE
17995 : 11287327 : && tag_code == enum_type)
17996 : : {
17997 : 3 : auto_diagnostic_group d;
17998 : 3 : error ("%qT referred to as enum", type);
17999 : 3 : inform (location_of (type), "%qT has a previous declaration here", type);
18000 : 3 : return error_mark_node;
18001 : 3 : }
18002 : 11350923 : else if (!allow_template_p
18003 : 4857402 : && TREE_CODE (type) == RECORD_TYPE
18004 : 16091210 : && CLASSTYPE_IS_TEMPLATE (type))
18005 : : {
18006 : : /* If a class template appears as elaborated type specifier
18007 : : without a template header such as:
18008 : :
18009 : : template <class T> class C {};
18010 : : void f(class C); // No template header here
18011 : :
18012 : : then the required template argument is missing. */
18013 : 18 : error ("template argument required for %<%s %T%>",
18014 : : tag_name (tag_code),
18015 : 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
18016 : 18 : return error_mark_node;
18017 : : }
18018 : :
18019 : : return type;
18020 : : }
18021 : :
18022 : : /* Lookup NAME of an elaborated type specifier according to SCOPE and
18023 : : issue diagnostics if necessary. Return *_TYPE node upon success,
18024 : : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
18025 : : error. */
18026 : :
18027 : : static tree
18028 : 22238404 : lookup_and_check_tag (enum tag_types tag_code, tree name,
18029 : : TAG_how how, bool template_header_p)
18030 : : {
18031 : 22238404 : tree decl;
18032 : 22238404 : if (how == TAG_how::GLOBAL)
18033 : : {
18034 : : /* First try ordinary name lookup, ignoring hidden class name
18035 : : injected via friend declaration. */
18036 : 1935704 : decl = lookup_name (name, LOOK_want::TYPE);
18037 : : /* If that fails, the name will be placed in the smallest
18038 : : non-class, non-function-prototype scope according to 3.3.1/5.
18039 : : We may already have a hidden name declared as friend in this
18040 : : scope. So lookup again but not ignoring hidden names.
18041 : : If we find one, that name will be made visible rather than
18042 : : creating a new tag. */
18043 : 1935704 : if (!decl)
18044 : 18207 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
18045 : : }
18046 : : else
18047 : 20302700 : decl = lookup_elaborated_type (name, how);
18048 : :
18049 : 20320907 : if (!decl)
18050 : : /* We found nothing. */
18051 : : return NULL_TREE;
18052 : :
18053 : 5012838 : if (TREE_CODE (decl) == TREE_LIST)
18054 : : {
18055 : 3 : auto_diagnostic_group d;
18056 : 3 : error ("reference to %qD is ambiguous", name);
18057 : 3 : print_candidates (decl);
18058 : 3 : return error_mark_node;
18059 : 3 : }
18060 : :
18061 : 2087187 : if (DECL_CLASS_TEMPLATE_P (decl)
18062 : 2087181 : && !template_header_p
18063 : 7100016 : && how == TAG_how::CURRENT_ONLY)
18064 : : {
18065 : 12 : auto_diagnostic_group d;
18066 : 12 : error ("class template %qD redeclared as non-template", name);
18067 : 12 : inform (location_of (decl), "previous declaration here");
18068 : 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
18069 : 12 : return error_mark_node;
18070 : 12 : }
18071 : :
18072 : 2087175 : if (DECL_CLASS_TEMPLATE_P (decl)
18073 : : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
18074 : : so ignore a template template parameter. */
18075 : 5012829 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
18076 : 2087172 : decl = DECL_TEMPLATE_RESULT (decl);
18077 : :
18078 : 5012823 : if (TREE_CODE (decl) != TYPE_DECL)
18079 : : /* Found not-a-type. */
18080 : : return NULL_TREE;
18081 : :
18082 : : /* Look for invalid nested type:
18083 : : class C {
18084 : : class C {};
18085 : : }; */
18086 : 5012802 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
18087 : : {
18088 : 6 : error ("%qD has the same name as the class in which it is "
18089 : : "declared", decl);
18090 : 6 : return error_mark_node;
18091 : : }
18092 : :
18093 : : /* Two cases we need to consider when deciding if a class
18094 : : template is allowed as an elaborated type specifier:
18095 : : 1. It is a self reference to its own class.
18096 : : 2. It comes with a template header.
18097 : :
18098 : : For example:
18099 : :
18100 : : template <class T> class C {
18101 : : class C *c1; // DECL_SELF_REFERENCE_P is true
18102 : : class D;
18103 : : };
18104 : : template <class U> class C; // template_header_p is true
18105 : : template <class T> class C<T>::D {
18106 : : class C *c2; // DECL_SELF_REFERENCE_P is true
18107 : : }; */
18108 : :
18109 : 15038388 : tree t = check_elaborated_type_specifier (tag_code, decl,
18110 : : template_header_p
18111 : 9689426 : | DECL_SELF_REFERENCE_P (decl));
18112 : 2207850 : if (template_header_p && t && CLASS_TYPE_P (t)
18113 : 7220628 : && (!CLASSTYPE_TEMPLATE_INFO (t)
18114 : 2207814 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
18115 : : {
18116 : 21 : auto_diagnostic_group d;
18117 : 21 : error ("%qT is not a template", t);
18118 : 21 : inform (location_of (t), "previous declaration here");
18119 : 42 : if (TYPE_CLASS_SCOPE_P (t)
18120 : 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
18121 : 3 : inform (input_location,
18122 : : "perhaps you want to explicitly add %<%T::%>",
18123 : 3 : TYPE_CONTEXT (t));
18124 : 21 : return error_mark_node;
18125 : 21 : }
18126 : :
18127 : : return t;
18128 : : }
18129 : :
18130 : : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
18131 : : Define the tag as a forward-reference if it is not defined.
18132 : :
18133 : : If a declaration is given, process it here, and report an error if
18134 : : multiple declarations are not identical.
18135 : :
18136 : : SCOPE is TS_CURRENT when this is also a definition. Only look in
18137 : : the current frame for the name (since C++ allows new names in any
18138 : : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
18139 : : declaration. Only look beginning from the current scope outward up
18140 : : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
18141 : :
18142 : : TEMPLATE_HEADER_P is true when this declaration is preceded by
18143 : : a set of template parameters. */
18144 : :
18145 : : tree
18146 : 21786990 : xref_tag (enum tag_types tag_code, tree name,
18147 : : TAG_how how, bool template_header_p)
18148 : : {
18149 : 21786990 : enum tree_code code;
18150 : 21786990 : tree context = NULL_TREE;
18151 : :
18152 : 21786990 : auto_cond_timevar tv (TV_NAME_LOOKUP);
18153 : :
18154 : 21786990 : gcc_assert (identifier_p (name));
18155 : :
18156 : 21786990 : switch (tag_code)
18157 : : {
18158 : : case record_type:
18159 : : case class_type:
18160 : : code = RECORD_TYPE;
18161 : : break;
18162 : : case union_type:
18163 : : code = UNION_TYPE;
18164 : : break;
18165 : : case enum_type:
18166 : : code = ENUMERAL_TYPE;
18167 : : break;
18168 : 0 : default:
18169 : 0 : gcc_unreachable ();
18170 : : }
18171 : :
18172 : : /* In case of anonymous name, xref_tag is only called to
18173 : : make type node and push name. Name lookup is not required. */
18174 : 21786990 : tree t = NULL_TREE;
18175 : 21786990 : if (!IDENTIFIER_ANON_P (name))
18176 : 20145465 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
18177 : :
18178 : 21786990 : if (t == error_mark_node)
18179 : : return error_mark_node;
18180 : :
18181 : 2520976 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
18182 : 1100880 : && template_class_depth (current_class_type)
18183 : 22182277 : && template_header_p)
18184 : : {
18185 : 277396 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
18186 : : return t;
18187 : :
18188 : : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
18189 : : a definition of this tag. Since, in addition, we are
18190 : : currently processing a (member) template declaration of a
18191 : : template class, we must be very careful; consider:
18192 : :
18193 : : template <class X> struct S1
18194 : :
18195 : : template <class U> struct S2
18196 : : {
18197 : : template <class V> friend struct S1;
18198 : : };
18199 : :
18200 : : Here, the S2::S1 declaration should not be confused with the
18201 : : outer declaration. In particular, the inner version should
18202 : : have a template parameter of level 2, not level 1.
18203 : :
18204 : : On the other hand, when presented with:
18205 : :
18206 : : template <class T> struct S1
18207 : : {
18208 : : template <class U> struct S2 {};
18209 : : template <class U> friend struct S2;
18210 : : };
18211 : :
18212 : : the friend must find S1::S2 eventually. We accomplish this
18213 : : by making sure that the new type we create to represent this
18214 : : declaration has the right TYPE_CONTEXT. */
18215 : 277393 : context = TYPE_CONTEXT (t);
18216 : 277393 : t = NULL_TREE;
18217 : : }
18218 : :
18219 : 21786876 : if (! t)
18220 : : {
18221 : : /* If no such tag is yet defined, create a forward-reference node
18222 : : and record it as the "definition".
18223 : : When a real declaration of this type is found,
18224 : : the forward-reference will be altered into a real type. */
18225 : 17086318 : if (code == ENUMERAL_TYPE)
18226 : : {
18227 : 14 : error ("use of enum %q#D without previous declaration", name);
18228 : 14 : return error_mark_node;
18229 : : }
18230 : :
18231 : 17086304 : t = make_class_type (code);
18232 : 17086304 : TYPE_CONTEXT (t) = context;
18233 : 17086304 : if (IDENTIFIER_LAMBDA_P (name))
18234 : : /* Mark it as a lambda type right now. Our caller will
18235 : : correct the value. */
18236 : 975306 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
18237 : 17086304 : t = pushtag (name, t, how);
18238 : : }
18239 : : else
18240 : : {
18241 : 4700558 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
18242 : : {
18243 : : /* Check that we aren't trying to overload a class with different
18244 : : constraints. */
18245 : 1930418 : if (!redeclare_class_template (t, current_template_parms,
18246 : : current_template_constraints ()))
18247 : 60 : return error_mark_node;
18248 : : }
18249 : 2770140 : else if (!processing_template_decl
18250 : 2581734 : && CLASS_TYPE_P (t)
18251 : 5325788 : && CLASSTYPE_IS_TEMPLATE (t))
18252 : : {
18253 : 0 : auto_diagnostic_group d;
18254 : 0 : error ("redeclaration of %qT as a non-template", t);
18255 : 0 : inform (location_of (t), "previous declaration %qD", t);
18256 : 0 : return error_mark_node;
18257 : 0 : }
18258 : :
18259 : 4700498 : if (modules_p ()
18260 : 4700498 : && how == TAG_how::CURRENT_ONLY)
18261 : : {
18262 : 8879 : tree decl = TYPE_NAME (t);
18263 : 8879 : if (!module_may_redeclare (decl))
18264 : 15 : return error_mark_node;
18265 : :
18266 : 8864 : tree not_tmpl = STRIP_TEMPLATE (decl);
18267 : 8864 : if (DECL_LANG_SPECIFIC (not_tmpl)
18268 : 3943 : && DECL_MODULE_ATTACH_P (not_tmpl)
18269 : 42 : && !DECL_MODULE_EXPORT_P (not_tmpl)
18270 : 8897 : && module_exporting_p ())
18271 : : {
18272 : 6 : auto_diagnostic_group d;
18273 : 6 : error ("conflicting exporting for declaration %qD", decl);
18274 : 6 : inform (DECL_SOURCE_LOCATION (decl),
18275 : : "previously declared here without exporting");
18276 : 6 : }
18277 : :
18278 : 8864 : tree maybe_tmpl = decl;
18279 : 8864 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
18280 : 6670 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
18281 : :
18282 : : /* FIXME: we should do a more precise check for redefinitions
18283 : : of a conflicting using-declaration here, as these diagnostics
18284 : : are not ideal. */
18285 : 8864 : if (DECL_LANG_SPECIFIC (decl)
18286 : 3943 : && DECL_MODULE_IMPORT_P (decl)
18287 : 8911 : && CP_DECL_CONTEXT (decl) == current_namespace)
18288 : : {
18289 : : /* Push it into this TU's symbol slot. */
18290 : 44 : if (maybe_tmpl != decl)
18291 : : /* We're in the template parm binding level.
18292 : : Pushtag has logic to slide under that, but we're
18293 : : not pushing a *new* type. */
18294 : 18 : push_nested_namespace (CP_DECL_CONTEXT (decl));
18295 : :
18296 : 44 : pushdecl (maybe_tmpl);
18297 : 44 : if (maybe_tmpl != decl)
18298 : 18 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
18299 : : }
18300 : :
18301 : 8864 : set_instantiating_module (maybe_tmpl);
18302 : : }
18303 : : }
18304 : :
18305 : : return t;
18306 : 21786990 : }
18307 : :
18308 : : /* Create the binfo hierarchy for REF with (possibly NULL) base list
18309 : : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
18310 : : access_* node, and the TREE_VALUE is the type of the base-class.
18311 : : Non-NULL TREE_TYPE indicates virtual inheritance. */
18312 : :
18313 : : void
18314 : 56292838 : xref_basetypes (tree ref, tree base_list)
18315 : : {
18316 : 56292838 : tree *basep;
18317 : 56292838 : tree binfo, base_binfo;
18318 : 56292838 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
18319 : 56292838 : unsigned max_bases = 0; /* Maximum direct bases. */
18320 : 56292838 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
18321 : 56292838 : int i;
18322 : 56292838 : tree default_access;
18323 : 56292838 : tree igo_prev; /* Track Inheritance Graph Order. */
18324 : :
18325 : 56292838 : if (ref == error_mark_node)
18326 : 12 : return;
18327 : :
18328 : : /* The base of a derived class is private by default, all others are
18329 : : public. */
18330 : 56292838 : default_access = (TREE_CODE (ref) == RECORD_TYPE
18331 : 55764674 : && CLASSTYPE_DECLARED_CLASS (ref)
18332 : 56292838 : ? access_private_node : access_public_node);
18333 : :
18334 : : /* First, make sure that any templates in base-classes are
18335 : : instantiated. This ensures that if we call ourselves recursively
18336 : : we do not get confused about which classes are marked and which
18337 : : are not. */
18338 : 56292838 : basep = &base_list;
18339 : 85537649 : while (*basep)
18340 : : {
18341 : 29244811 : tree basetype = TREE_VALUE (*basep);
18342 : :
18343 : : /* The dependent_type_p call below should really be dependent_scope_p
18344 : : so that we give a hard error about using an incomplete type as a
18345 : : base, but we allow it with a pedwarn for backward
18346 : : compatibility. */
18347 : 29244811 : if (processing_template_decl
18348 : 29244811 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
18349 : 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
18350 : : diagnostics::kind::pedwarn);
18351 : 29244811 : if (!dependent_type_p (basetype)
18352 : 29244811 : && !complete_type_or_else (basetype, NULL))
18353 : : /* An incomplete type. Remove it from the list. */
18354 : 15 : *basep = TREE_CHAIN (*basep);
18355 : : else
18356 : : {
18357 : 29244796 : max_bases++;
18358 : 29244796 : if (TREE_TYPE (*basep))
18359 : 70205 : max_dvbases++;
18360 : 29244796 : if (CLASS_TYPE_P (basetype))
18361 : 28415306 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18362 : 29244796 : basep = &TREE_CHAIN (*basep);
18363 : : }
18364 : : }
18365 : 56292838 : max_vbases += max_dvbases;
18366 : :
18367 : 56292838 : TYPE_MARKED_P (ref) = 1;
18368 : :
18369 : : /* The binfo slot should be empty, unless this is an (ill-formed)
18370 : : redefinition. */
18371 : 56292838 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
18372 : :
18373 : 56292838 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
18374 : :
18375 : 56292838 : binfo = make_tree_binfo (max_bases);
18376 : :
18377 : 56292838 : TYPE_BINFO (ref) = binfo;
18378 : 56292838 : BINFO_OFFSET (binfo) = size_zero_node;
18379 : 56292838 : BINFO_TYPE (binfo) = ref;
18380 : :
18381 : : /* Apply base-class info set up to the variants of this type. */
18382 : 56292838 : fixup_type_variants (ref);
18383 : :
18384 : 56292838 : if (max_bases)
18385 : : {
18386 : 28360003 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
18387 : : /* A C++98 POD cannot have base classes. */
18388 : 28360003 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
18389 : :
18390 : 28360003 : if (TREE_CODE (ref) == UNION_TYPE)
18391 : : {
18392 : 12 : error ("derived union %qT invalid", ref);
18393 : 12 : return;
18394 : : }
18395 : : }
18396 : :
18397 : 28359991 : if (max_bases > 1)
18398 : 688354 : warning (OPT_Wmultiple_inheritance,
18399 : : "%qT defined with multiple direct bases", ref);
18400 : :
18401 : 56292826 : if (max_vbases)
18402 : : {
18403 : : /* An aggregate can't have virtual base classes. */
18404 : 212044 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18405 : :
18406 : 212044 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
18407 : :
18408 : 212044 : if (max_dvbases)
18409 : 69010 : warning (OPT_Wvirtual_inheritance,
18410 : : "%qT defined with direct virtual base", ref);
18411 : : }
18412 : :
18413 : 85537610 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
18414 : : {
18415 : 29244784 : tree access = TREE_PURPOSE (base_list);
18416 : 29244784 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
18417 : 29244784 : tree basetype = TREE_VALUE (base_list);
18418 : :
18419 : 29244784 : if (access == access_default_node)
18420 : 4382289 : access = default_access;
18421 : :
18422 : : /* Before C++17, an aggregate cannot have base classes. In C++17, an
18423 : : aggregate can't have virtual, private, or protected base classes. */
18424 : 29244784 : if (cxx_dialect < cxx17
18425 : 29037007 : || access != access_public_node
18426 : 28114454 : || via_virtual)
18427 : 1197540 : CLASSTYPE_NON_AGGREGATE (ref) = true;
18428 : :
18429 : 29244784 : if (PACK_EXPANSION_P (basetype))
18430 : 23629 : basetype = PACK_EXPANSION_PATTERN (basetype);
18431 : 29244784 : if (TREE_CODE (basetype) == TYPE_DECL)
18432 : 0 : basetype = TREE_TYPE (basetype);
18433 : 29244784 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
18434 : : {
18435 : 24 : error ("base type %qT fails to be a struct or class type",
18436 : : basetype);
18437 : 24 : goto dropped_base;
18438 : : }
18439 : :
18440 : 29244760 : base_binfo = NULL_TREE;
18441 : 29244760 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
18442 : : {
18443 : 22709569 : base_binfo = TYPE_BINFO (basetype);
18444 : : /* The original basetype could have been a typedef'd type. */
18445 : 22709569 : basetype = BINFO_TYPE (base_binfo);
18446 : :
18447 : : /* Inherit flags from the base. */
18448 : 68128707 : TYPE_HAS_NEW_OPERATOR (ref)
18449 : 22709569 : |= TYPE_HAS_NEW_OPERATOR (basetype);
18450 : 68128707 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
18451 : 22709569 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
18452 : 22709569 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
18453 : 22709569 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
18454 : 68128707 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
18455 : 22709569 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
18456 : 68128707 : CLASSTYPE_REPEATED_BASE_P (ref)
18457 : 22709569 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
18458 : : }
18459 : :
18460 : : /* We must do this test after we've seen through a typedef
18461 : : type. */
18462 : 29244760 : if (TYPE_MARKED_P (basetype))
18463 : : {
18464 : 18 : if (basetype == ref)
18465 : 0 : error ("recursive type %qT undefined", basetype);
18466 : : else
18467 : 18 : error ("duplicate base type %qT invalid", basetype);
18468 : 18 : goto dropped_base;
18469 : : }
18470 : :
18471 : 29244742 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
18472 : : /* Regenerate the pack expansion for the bases. */
18473 : 23629 : basetype = make_pack_expansion (basetype);
18474 : :
18475 : 29244742 : TYPE_MARKED_P (basetype) = 1;
18476 : :
18477 : 29244742 : base_binfo = copy_binfo (base_binfo, basetype, ref,
18478 : : &igo_prev, via_virtual);
18479 : 29244742 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
18480 : 29174552 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
18481 : :
18482 : 29244742 : BINFO_BASE_APPEND (binfo, base_binfo);
18483 : 29244742 : BINFO_BASE_ACCESS_APPEND (binfo, access);
18484 : 29244742 : continue;
18485 : :
18486 : 42 : dropped_base:
18487 : : /* Update max_vbases to reflect the reality that we are dropping
18488 : : this base: if it reaches zero we want to undo the vec_alloc
18489 : : above to avoid inconsistencies during error-recovery: eg, in
18490 : : build_special_member_call, CLASSTYPE_VBASECLASSES non null
18491 : : and vtt null (c++/27952). */
18492 : 42 : if (via_virtual)
18493 : 12 : max_vbases--;
18494 : 42 : if (CLASS_TYPE_P (basetype))
18495 : 30 : max_vbases
18496 : 33 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18497 : 29244742 : }
18498 : :
18499 : 56292826 : if (CLASSTYPE_VBASECLASSES (ref)
18500 : 56292826 : && max_vbases == 0)
18501 : 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
18502 : :
18503 : 56504858 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18504 : : /* If we didn't get max_vbases vbases, we must have shared at
18505 : : least one of them, and are therefore diamond shaped. */
18506 : 22760 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18507 : :
18508 : : /* Unmark all the types. */
18509 : 85537568 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18510 : 29244742 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18511 : 56292826 : TYPE_MARKED_P (ref) = 0;
18512 : :
18513 : : /* Now see if we have a repeated base type. */
18514 : 56292826 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
18515 : : {
18516 : 145438935 : for (base_binfo = binfo; base_binfo;
18517 : 89146541 : base_binfo = TREE_CHAIN (base_binfo))
18518 : : {
18519 : 89147252 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18520 : : {
18521 : 711 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18522 : 711 : break;
18523 : : }
18524 : 89146541 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18525 : : }
18526 : 145438935 : for (base_binfo = binfo; base_binfo;
18527 : 89146541 : base_binfo = TREE_CHAIN (base_binfo))
18528 : 89147252 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18529 : 89146541 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18530 : : else
18531 : : break;
18532 : : }
18533 : : }
18534 : :
18535 : :
18536 : : /* Copies the enum-related properties from type SRC to type DST.
18537 : : Used with the underlying type of an enum and the enum itself. */
18538 : : static void
18539 : 1769152 : copy_type_enum (tree dst, tree src)
18540 : : {
18541 : 1769152 : tree t;
18542 : 3538322 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
18543 : : {
18544 : 1769170 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
18545 : 1769170 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
18546 : 1769170 : TYPE_SIZE (t) = TYPE_SIZE (src);
18547 : 1769170 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
18548 : 1769170 : SET_TYPE_MODE (dst, TYPE_MODE (src));
18549 : 1769170 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
18550 : 1769170 : unsigned valign = TYPE_ALIGN (src);
18551 : 1769170 : if (TYPE_USER_ALIGN (t))
18552 : 12 : valign = MAX (valign, TYPE_ALIGN (t));
18553 : : else
18554 : 1769158 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
18555 : 1769170 : SET_TYPE_ALIGN (t, valign);
18556 : 1769170 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
18557 : : }
18558 : 1769152 : }
18559 : :
18560 : : /* Begin compiling the definition of an enumeration type.
18561 : : NAME is its name,
18562 : :
18563 : : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
18564 : :
18565 : : UNDERLYING_TYPE is the type that will be used as the storage for
18566 : : the enumeration type. This should be NULL_TREE if no storage type
18567 : : was specified.
18568 : :
18569 : : ATTRIBUTES are any attributes specified after the enum-key.
18570 : :
18571 : : SCOPED_ENUM_P is true if this is a scoped enumeration type.
18572 : :
18573 : : if IS_NEW is not NULL, gets TRUE iff a new type is created.
18574 : :
18575 : : Returns the type object, as yet incomplete.
18576 : : Also records info about it so that build_enumerator
18577 : : may be used to declare the individual values as they are read. */
18578 : :
18579 : : tree
18580 : 2093070 : start_enum (tree name, tree enumtype, tree underlying_type,
18581 : : tree attributes, bool scoped_enum_p, bool *is_new)
18582 : : {
18583 : 2093070 : tree prevtype = NULL_TREE;
18584 : 2093070 : gcc_assert (identifier_p (name));
18585 : :
18586 : 2093070 : if (is_new)
18587 : 1670247 : *is_new = false;
18588 : : /* [C++0x dcl.enum]p5:
18589 : :
18590 : : If not explicitly specified, the underlying type of a scoped
18591 : : enumeration type is int. */
18592 : 2093070 : if (!underlying_type && scoped_enum_p)
18593 : 120400 : underlying_type = integer_type_node;
18594 : :
18595 : 2093070 : if (underlying_type)
18596 : 437138 : underlying_type = cv_unqualified (underlying_type);
18597 : :
18598 : : /* If this is the real definition for a previous forward reference,
18599 : : fill in the contents in the same object that used to be the
18600 : : forward reference. */
18601 : 2093070 : if (!enumtype)
18602 : 2092939 : enumtype = lookup_and_check_tag (enum_type, name,
18603 : : /*tag_scope=*/TAG_how::CURRENT_ONLY,
18604 : : /*template_header_p=*/false);
18605 : :
18606 : : /* In case of a template_decl, the only check that should be deferred
18607 : : to instantiation time is the comparison of underlying types. */
18608 : 2093070 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
18609 : : {
18610 : : /* Attempt to set the declaring module. */
18611 : 34880 : if (modules_p ())
18612 : : {
18613 : 160 : tree decl = TYPE_NAME (enumtype);
18614 : 160 : if (!module_may_redeclare (decl))
18615 : 3 : enumtype = error_mark_node;
18616 : : else
18617 : 157 : set_instantiating_module (decl);
18618 : : }
18619 : :
18620 : 34880 : if (enumtype == error_mark_node)
18621 : : ;
18622 : 35067 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
18623 : : {
18624 : 6 : auto_diagnostic_group d;
18625 : 6 : error_at (input_location, "scoped/unscoped mismatch "
18626 : : "in enum %q#T", enumtype);
18627 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18628 : : "previous definition here");
18629 : 6 : enumtype = error_mark_node;
18630 : 6 : }
18631 : 34871 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
18632 : : {
18633 : 0 : auto_diagnostic_group d;
18634 : 0 : error_at (input_location, "underlying type mismatch "
18635 : : "in enum %q#T", enumtype);
18636 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18637 : : "previous definition here");
18638 : 0 : enumtype = error_mark_node;
18639 : 0 : }
18640 : 34855 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
18641 : 69726 : && !same_type_p (underlying_type,
18642 : : ENUM_UNDERLYING_TYPE (enumtype)))
18643 : : {
18644 : 26 : auto_diagnostic_group d;
18645 : 26 : error_at (input_location, "different underlying type "
18646 : : "in enum %q#T", enumtype);
18647 : 26 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18648 : : "previous definition here");
18649 : 26 : underlying_type = NULL_TREE;
18650 : 26 : }
18651 : : }
18652 : :
18653 : 2093070 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
18654 : 34871 : || processing_template_decl)
18655 : : {
18656 : : /* In case of error, make a dummy enum to allow parsing to
18657 : : continue. */
18658 : 2058287 : if (enumtype == error_mark_node)
18659 : : {
18660 : 9 : name = make_anon_name ();
18661 : 9 : enumtype = NULL_TREE;
18662 : : }
18663 : :
18664 : : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
18665 : : of an opaque enum, or an opaque enum of an already defined
18666 : : enumeration (C++11).
18667 : : In any other case, it'll be NULL_TREE. */
18668 : 2058287 : if (!enumtype)
18669 : : {
18670 : 2058196 : if (is_new)
18671 : 1635376 : *is_new = true;
18672 : : }
18673 : 2058287 : prevtype = enumtype;
18674 : :
18675 : : /* Do not push the decl more than once. */
18676 : 2058287 : if (!enumtype
18677 : 91 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
18678 : : {
18679 : 2058199 : enumtype = cxx_make_type (ENUMERAL_TYPE);
18680 : 2058199 : enumtype = pushtag (name, enumtype);
18681 : :
18682 : 2058199 : if (enumtype != error_mark_node)
18683 : : {
18684 : : /* The enum is considered opaque until the opening '{' of the
18685 : : enumerator list. */
18686 : 2058181 : SET_OPAQUE_ENUM_P (enumtype, true);
18687 : 2058181 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
18688 : :
18689 : : /* std::byte aliases anything. */
18690 : 2058181 : if (TYPE_CONTEXT (enumtype) == std_node
18691 : 2381626 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
18692 : 12845 : TYPE_ALIAS_SET (enumtype) = 0;
18693 : : }
18694 : : }
18695 : : else
18696 : 88 : enumtype = xref_tag (enum_type, name);
18697 : :
18698 : 2058287 : if (enumtype == error_mark_node)
18699 : : return error_mark_node;
18700 : : }
18701 : :
18702 : 2093052 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
18703 : :
18704 : 2093052 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18705 : :
18706 : 2093052 : if (underlying_type)
18707 : : {
18708 : 437109 : if (ENUM_UNDERLYING_TYPE (enumtype))
18709 : : /* We already checked that it matches, don't change it to a different
18710 : : typedef variant. */;
18711 : 402280 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
18712 : : {
18713 : 402216 : copy_type_enum (enumtype, underlying_type);
18714 : 402216 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18715 : : }
18716 : 64 : else if (dependent_type_p (underlying_type))
18717 : 51 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18718 : : else
18719 : : {
18720 : 13 : error ("underlying type %qT of %qT must be an integral type",
18721 : : underlying_type, enumtype);
18722 : 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
18723 : : }
18724 : : }
18725 : :
18726 : : /* If into a template class, the returned enum is always the first
18727 : : declaration (opaque or not) seen. This way all the references to
18728 : : this type will be to the same declaration. The following ones are used
18729 : : only to check for definition errors. */
18730 : 2093052 : if (prevtype && processing_template_decl)
18731 : : return prevtype;
18732 : : else
18733 : 2092964 : return enumtype;
18734 : : }
18735 : :
18736 : : /* Returns true if TYPE is an enum that uses an enumerator name for
18737 : : linkage purposes. */
18738 : :
18739 : : bool
18740 : 346682682 : enum_with_enumerator_for_linkage_p (tree type)
18741 : : {
18742 : 346682682 : return (cxx_dialect >= cxx20
18743 : 115355785 : && UNSCOPED_ENUM_P (type)
18744 : 5701754 : && TYPE_ANON_P (type)
18745 : 347783755 : && TYPE_VALUES (type));
18746 : : }
18747 : :
18748 : : /* After processing and defining all the values of an enumeration type,
18749 : : install their decls in the enumeration type.
18750 : : ENUMTYPE is the type object. */
18751 : :
18752 : : void
18753 : 1992997 : finish_enum_value_list (tree enumtype)
18754 : : {
18755 : 1992997 : tree values;
18756 : 1992997 : tree underlying_type;
18757 : 1992997 : tree decl;
18758 : 1992997 : tree value;
18759 : 1992997 : tree minnode, maxnode;
18760 : 1992997 : tree t;
18761 : :
18762 : 1992997 : bool fixed_underlying_type_p
18763 : 1992997 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
18764 : :
18765 : : /* We built up the VALUES in reverse order. */
18766 : 1992997 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
18767 : :
18768 : : /* For an enum defined in a template, just set the type of the values;
18769 : : all further processing is postponed until the template is
18770 : : instantiated. We need to set the type so that tsubst of a CONST_DECL
18771 : : works. */
18772 : 1992997 : if (processing_template_decl)
18773 : : {
18774 : 364357 : for (values = TYPE_VALUES (enumtype);
18775 : 673984 : values;
18776 : 364357 : values = TREE_CHAIN (values))
18777 : 364357 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
18778 : : return;
18779 : : }
18780 : :
18781 : : /* Determine the minimum and maximum values of the enumerators. */
18782 : 1683370 : if (TYPE_VALUES (enumtype))
18783 : : {
18784 : : minnode = maxnode = NULL_TREE;
18785 : :
18786 : 10627014 : for (values = TYPE_VALUES (enumtype);
18787 : 12282132 : values;
18788 : 10627014 : values = TREE_CHAIN (values))
18789 : : {
18790 : 10627014 : decl = TREE_VALUE (values);
18791 : :
18792 : : /* [dcl.enum]: Following the closing brace of an enum-specifier,
18793 : : each enumerator has the type of its enumeration. Prior to the
18794 : : closing brace, the type of each enumerator is the type of its
18795 : : initializing value. */
18796 : 10627014 : TREE_TYPE (decl) = enumtype;
18797 : :
18798 : : /* Update the minimum and maximum values, if appropriate. */
18799 : 10627014 : value = DECL_INITIAL (decl);
18800 : 10627014 : if (TREE_CODE (value) != INTEGER_CST)
18801 : 4 : value = integer_zero_node;
18802 : : /* Figure out what the minimum and maximum values of the
18803 : : enumerators are. */
18804 : 10627014 : if (!minnode)
18805 : : minnode = maxnode = value;
18806 : 8971896 : else if (tree_int_cst_lt (maxnode, value))
18807 : : maxnode = value;
18808 : 1356435 : else if (tree_int_cst_lt (value, minnode))
18809 : 147363 : minnode = value;
18810 : : }
18811 : : }
18812 : : else
18813 : : /* [dcl.enum]
18814 : :
18815 : : If the enumerator-list is empty, the underlying type is as if
18816 : : the enumeration had a single enumerator with value 0. */
18817 : 28252 : minnode = maxnode = integer_zero_node;
18818 : :
18819 : 1683370 : if (!fixed_underlying_type_p)
18820 : : {
18821 : : /* Compute the number of bits require to represent all values of the
18822 : : enumeration. We must do this before the type of MINNODE and
18823 : : MAXNODE are transformed, since tree_int_cst_min_precision relies
18824 : : on the TREE_TYPE of the value it is passed. */
18825 : 1366936 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
18826 : 1366936 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
18827 : 1366936 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
18828 : 1366936 : int precision = MAX (lowprec, highprec);
18829 : 1366936 : unsigned int itk;
18830 : 1366936 : bool use_short_enum;
18831 : :
18832 : : /* Determine the underlying type of the enumeration.
18833 : :
18834 : : [dcl.enum]
18835 : :
18836 : : The underlying type of an enumeration is an integral type that
18837 : : can represent all the enumerator values defined in the
18838 : : enumeration. It is implementation-defined which integral type is
18839 : : used as the underlying type for an enumeration except that the
18840 : : underlying type shall not be larger than int unless the value of
18841 : : an enumerator cannot fit in an int or unsigned int.
18842 : :
18843 : : We use "int" or an "unsigned int" as the underlying type, even if
18844 : : a smaller integral type would work, unless the user has
18845 : : explicitly requested that we use the smallest possible type. The
18846 : : user can request that for all enumerations with a command line
18847 : : flag, or for just one enumeration with an attribute. */
18848 : :
18849 : 2733872 : use_short_enum = flag_short_enums
18850 : 1366936 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
18851 : :
18852 : : /* If the precision of the type was specified with an attribute and it
18853 : : was too small, give an error. Otherwise, use it. */
18854 : 1366936 : if (TYPE_PRECISION (enumtype))
18855 : : {
18856 : 11 : if (precision > TYPE_PRECISION (enumtype))
18857 : 0 : error ("specified mode too small for enumerated values");
18858 : : else
18859 : : {
18860 : 11 : use_short_enum = true;
18861 : 11 : precision = TYPE_PRECISION (enumtype);
18862 : : }
18863 : : }
18864 : :
18865 : 2656071 : for (itk = (use_short_enum ? itk_char : itk_int);
18866 : 2656071 : itk != itk_none;
18867 : : itk++)
18868 : : {
18869 : 2656068 : underlying_type = integer_types[itk];
18870 : 2656068 : if (underlying_type != NULL_TREE
18871 : 2656050 : && TYPE_PRECISION (underlying_type) >= precision
18872 : 5310177 : && TYPE_SIGN (underlying_type) == sgn)
18873 : : break;
18874 : : }
18875 : 1366936 : if (itk == itk_none)
18876 : : {
18877 : : /* DR 377
18878 : :
18879 : : IF no integral type can represent all the enumerator values, the
18880 : : enumeration is ill-formed. */
18881 : 3 : error ("no integral type can represent all of the enumerator values "
18882 : : "for %qT", enumtype);
18883 : 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
18884 : 3 : underlying_type = integer_types[itk_unsigned_long_long];
18885 : : }
18886 : :
18887 : : /* [dcl.enum]
18888 : :
18889 : : The value of sizeof() applied to an enumeration type, an object
18890 : : of an enumeration type, or an enumerator, is the value of sizeof()
18891 : : applied to the underlying type. */
18892 : 1366936 : copy_type_enum (enumtype, underlying_type);
18893 : :
18894 : : /* Compute the minimum and maximum values for the type.
18895 : :
18896 : : [dcl.enum]
18897 : :
18898 : : For an enumeration where emin is the smallest enumerator and emax
18899 : : is the largest, the values of the enumeration are the values of the
18900 : : underlying type in the range bmin to bmax, where bmin and bmax are,
18901 : : respectively, the smallest and largest values of the smallest bit-
18902 : : field that can store emin and emax. */
18903 : :
18904 : : /* The middle-end currently assumes that types with TYPE_PRECISION
18905 : : narrower than their underlying type are suitably zero or sign
18906 : : extended to fill their mode. Similarly, it assumes that the front
18907 : : end assures that a value of a particular type must be within
18908 : : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
18909 : :
18910 : : We used to set these fields based on bmin and bmax, but that led
18911 : : to invalid assumptions like optimizing away bounds checking. So
18912 : : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
18913 : : TYPE_MAX_VALUE to the values for the mode above and only restrict
18914 : : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
18915 : 1366936 : ENUM_UNDERLYING_TYPE (enumtype)
18916 : 1366936 : = build_distinct_type_copy (underlying_type);
18917 : 1366936 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
18918 : 1366936 : set_min_and_max_values_for_integral_type
18919 : 1366936 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
18920 : :
18921 : : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
18922 : 1366936 : if (flag_strict_enums)
18923 : 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
18924 : :
18925 : 1366936 : if (use_short_enum)
18926 : : {
18927 : 79 : TYPE_PACKED (enumtype) = use_short_enum;
18928 : 79 : fixup_attribute_variants (enumtype);
18929 : : }
18930 : : }
18931 : : else
18932 : 316434 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
18933 : :
18934 : : /* If the enum is exported, mark the consts too. */
18935 : 1683370 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
18936 : 1436752 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
18937 : 1683891 : && at_namespace_scope_p ());
18938 : :
18939 : : /* Convert each of the enumerators to the type of the underlying
18940 : : type of the enumeration. */
18941 : 12310384 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
18942 : : {
18943 : 10627014 : decl = TREE_VALUE (values);
18944 : 10627014 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
18945 : 10627014 : if (fixed_underlying_type_p)
18946 : : /* If the enumeration type has a fixed underlying type, we
18947 : : already checked all of the enumerator values. */
18948 : 3111295 : value = DECL_INITIAL (decl);
18949 : : else
18950 : 7515719 : value = perform_implicit_conversion (underlying_type,
18951 : 7515719 : DECL_INITIAL (decl),
18952 : : tf_warning_or_error);
18953 : : /* Do not clobber shared ints. */
18954 : 10627014 : if (value != error_mark_node)
18955 : : {
18956 : 10627012 : value = copy_node (value);
18957 : :
18958 : 10627012 : TREE_TYPE (value) = enumtype;
18959 : : }
18960 : 10627014 : DECL_INITIAL (decl) = value;
18961 : 10627014 : if (export_p)
18962 : 2890 : DECL_MODULE_EXPORT_P (decl) = true;
18963 : 10627014 : }
18964 : :
18965 : : /* Fix up all variant types of this enum type. */
18966 : 3366758 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
18967 : 1683388 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
18968 : :
18969 : 1683370 : if (at_class_scope_p ()
18970 : 934830 : && COMPLETE_TYPE_P (current_class_type)
18971 : 1683431 : && UNSCOPED_ENUM_P (enumtype))
18972 : : {
18973 : 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
18974 : : /* TYPE_FIELDS needs fixup. */
18975 : 42 : fixup_type_variants (current_class_type);
18976 : : }
18977 : :
18978 : : /* P2115: An unnamed enum uses the name of its first enumerator for
18979 : : linkage purposes; reset the type linkage if that is the case. */
18980 : 1683370 : if (enum_with_enumerator_for_linkage_p (enumtype))
18981 : 329887 : reset_type_linkage (enumtype);
18982 : :
18983 : : /* Finish debugging output for this type. */
18984 : 1683370 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
18985 : :
18986 : : /* Each enumerator now has the type of its enumeration. Clear the cache
18987 : : so that this change in types doesn't confuse us later on. */
18988 : 1683370 : clear_cv_and_fold_caches ();
18989 : : }
18990 : :
18991 : : /* Finishes the enum type. This is called only the first time an
18992 : : enumeration is seen, be it opaque or odinary.
18993 : : ENUMTYPE is the type object. */
18994 : :
18995 : : void
18996 : 1982531 : finish_enum (tree enumtype)
18997 : : {
18998 : 1982531 : if (processing_template_decl)
18999 : : {
19000 : 309655 : if (at_function_scope_p ())
19001 : 2532 : add_stmt (build_min (TAG_DEFN, enumtype));
19002 : 309655 : return;
19003 : : }
19004 : :
19005 : : /* If this is a forward declaration, there should not be any variants,
19006 : : though we can get a variant in the middle of an enum-specifier with
19007 : : wacky code like 'enum E { e = sizeof(const E*) };' */
19008 : 3345752 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
19009 : : && (TYPE_VALUES (enumtype)
19010 : : || !TYPE_NEXT_VARIANT (enumtype)));
19011 : : }
19012 : :
19013 : : /* Build and install a CONST_DECL for an enumeration constant of the
19014 : : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
19015 : : Apply ATTRIBUTES if available. LOC is the location of NAME.
19016 : : Assignment of sequential values by default is handled here. */
19017 : :
19018 : : tree
19019 : 10991348 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
19020 : : location_t loc)
19021 : : {
19022 : 10991348 : tree decl;
19023 : 10991348 : tree context;
19024 : 10991348 : tree type;
19025 : :
19026 : : /* scalar_constant_value will pull out this expression, so make sure
19027 : : it's folded as appropriate.
19028 : :
19029 : : Creating a TARGET_EXPR in a template breaks when substituting, and
19030 : : here we would create it for instance when using a class prvalue with
19031 : : a user-defined conversion function. So don't use such a tree. We
19032 : : instantiate VALUE here to get errors about bad enumerators even in
19033 : : a template that does not get instantiated. */
19034 : 10991348 : if (processing_template_decl)
19035 : 364357 : value = maybe_fold_non_dependent_expr (value);
19036 : :
19037 : : /* If the VALUE was erroneous, pretend it wasn't there; that will
19038 : : result in the enum being assigned the next value in sequence. */
19039 : 10991348 : if (value == error_mark_node)
19040 : : value = NULL_TREE;
19041 : :
19042 : : /* Remove no-op casts from the value. */
19043 : 10991310 : if (value)
19044 : 8373995 : STRIP_TYPE_NOPS (value);
19045 : :
19046 : 10991348 : if (! processing_template_decl)
19047 : : {
19048 : : /* Validate and default VALUE. */
19049 : 10626991 : if (value != NULL_TREE)
19050 : : {
19051 : 4768918 : if (!ENUM_UNDERLYING_TYPE (enumtype))
19052 : : {
19053 : 2562845 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
19054 : : value, true);
19055 : 2562845 : if (tmp_value)
19056 : 4768918 : value = tmp_value;
19057 : : }
19058 : 2206073 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19059 : : (TREE_TYPE (value)))
19060 : 39 : value = perform_implicit_conversion_flags
19061 : 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
19062 : : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
19063 : :
19064 : 4768918 : if (value == error_mark_node)
19065 : : value = NULL_TREE;
19066 : :
19067 : 4768897 : if (value != NULL_TREE)
19068 : : {
19069 : 4768897 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
19070 : : (TREE_TYPE (value)))
19071 : : {
19072 : 17 : error_at (cp_expr_loc_or_input_loc (value),
19073 : : "enumerator value for %qD must have integral or "
19074 : : "unscoped enumeration type", name);
19075 : 17 : value = NULL_TREE;
19076 : : }
19077 : : else
19078 : : {
19079 : 4768880 : value = cxx_constant_value (value);
19080 : :
19081 : 4768880 : if (TREE_CODE (value) != INTEGER_CST)
19082 : : {
19083 : 50 : error ("enumerator value for %qD is not an integer "
19084 : : "constant", name);
19085 : 50 : value = NULL_TREE;
19086 : : }
19087 : : }
19088 : : }
19089 : : }
19090 : :
19091 : : /* Default based on previous value. */
19092 : 67 : if (value == NULL_TREE)
19093 : : {
19094 : 5858161 : if (TYPE_VALUES (enumtype))
19095 : : {
19096 : 5410082 : tree prev_value;
19097 : :
19098 : : /* C++03 7.2/4: If no initializer is specified for the first
19099 : : enumerator, the type is an unspecified integral
19100 : : type. Otherwise the type is the same as the type of the
19101 : : initializing value of the preceding enumerator unless the
19102 : : incremented value is not representable in that type, in
19103 : : which case the type is an unspecified integral type
19104 : : sufficient to contain the incremented value. */
19105 : 5410082 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
19106 : 5410082 : if (TREE_CODE (prev_value) != INTEGER_CST)
19107 : 2 : value = error_mark_node;
19108 : : else
19109 : : {
19110 : 5410080 : wi::overflow_type overflowed;
19111 : 5410080 : tree type = TREE_TYPE (prev_value);
19112 : 5410080 : signop sgn = TYPE_SIGN (type);
19113 : 5410080 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
19114 : 5410080 : &overflowed);
19115 : 5410080 : if (!overflowed)
19116 : : {
19117 : 5410080 : bool pos = !wi::neg_p (wi, sgn);
19118 : 5410080 : if (!wi::fits_to_tree_p (wi, type))
19119 : : {
19120 : : unsigned int itk;
19121 : 53 : for (itk = itk_int; itk != itk_none; itk++)
19122 : : {
19123 : 53 : type = integer_types[itk];
19124 : 53 : if (type != NULL_TREE
19125 : 53 : && (pos || !TYPE_UNSIGNED (type))
19126 : 106 : && wi::fits_to_tree_p (wi, type))
19127 : : break;
19128 : : }
19129 : 17 : if (type && cxx_dialect < cxx11
19130 : 6 : && itk > itk_unsigned_long)
19131 : 3 : pedwarn (input_location, OPT_Wlong_long,
19132 : : pos ? G_("\
19133 : : incremented enumerator value is too large for %<unsigned long%>") : G_("\
19134 : : incremented enumerator value is too large for %<long%>"));
19135 : : }
19136 : 5410080 : if (type == NULL_TREE)
19137 : 0 : overflowed = wi::OVF_UNKNOWN;
19138 : : else
19139 : 5410080 : value = wide_int_to_tree (type, wi);
19140 : : }
19141 : :
19142 : 5410080 : if (overflowed)
19143 : : {
19144 : 0 : error ("overflow in enumeration values at %qD", name);
19145 : 0 : value = error_mark_node;
19146 : : }
19147 : 5410080 : }
19148 : : }
19149 : : else
19150 : 448079 : value = integer_zero_node;
19151 : : }
19152 : :
19153 : : /* Remove no-op casts from the value. */
19154 : 10626991 : STRIP_TYPE_NOPS (value);
19155 : :
19156 : : /* If the underlying type of the enum is fixed, check whether
19157 : : the enumerator values fits in the underlying type. If it
19158 : : does not fit, the program is ill-formed [C++0x dcl.enum]. */
19159 : 10626991 : if (ENUM_UNDERLYING_TYPE (enumtype)
19160 : : && value
19161 : 10626991 : && TREE_CODE (value) == INTEGER_CST)
19162 : : {
19163 : 3111270 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
19164 : 9 : error ("enumerator value %qE is outside the range of underlying "
19165 : 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
19166 : :
19167 : : /* Convert the value to the appropriate type. */
19168 : 3111270 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
19169 : : }
19170 : : }
19171 : :
19172 : : /* C++ associates enums with global, function, or class declarations. */
19173 : 10991348 : context = current_scope ();
19174 : :
19175 : : /* Build the actual enumeration constant. Note that the enumeration
19176 : : constants have the underlying type of the enum (if it is fixed)
19177 : : or the type of their initializer (if the underlying type of the
19178 : : enum is not fixed):
19179 : :
19180 : : [ C++0x dcl.enum ]
19181 : :
19182 : : If the underlying type is fixed, the type of each enumerator
19183 : : prior to the closing brace is the underlying type; if the
19184 : : initializing value of an enumerator cannot be represented by
19185 : : the underlying type, the program is ill-formed. If the
19186 : : underlying type is not fixed, the type of each enumerator is
19187 : : the type of its initializing value.
19188 : :
19189 : : If the underlying type is not fixed, it will be computed by
19190 : : finish_enum and we will reset the type of this enumerator. Of
19191 : : course, if we're processing a template, there may be no value. */
19192 : 10991348 : type = value ? TREE_TYPE (value) : NULL_TREE;
19193 : :
19194 : 10991348 : decl = build_decl (loc, CONST_DECL, name, type);
19195 : :
19196 : 10991348 : DECL_CONTEXT (decl) = enumtype;
19197 : 10991348 : TREE_CONSTANT (decl) = 1;
19198 : 10991348 : TREE_READONLY (decl) = 1;
19199 : 10991348 : DECL_INITIAL (decl) = value;
19200 : :
19201 : 10991348 : if (attributes)
19202 : 43837 : cplus_decl_attributes (&decl, attributes, 0);
19203 : :
19204 : 10991348 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
19205 : : {
19206 : : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
19207 : : on the TYPE_FIELDS list for `S'. (That's so that you can say
19208 : : things like `S::i' later.) */
19209 : :
19210 : : /* The enumerator may be getting declared outside of its enclosing
19211 : : class, like so:
19212 : :
19213 : : class S { public: enum E : int; }; enum S::E : int { i = 7; };
19214 : :
19215 : : For which case we need to make sure that the access of `S::i'
19216 : : matches the access of `S::E'. */
19217 : 1956362 : auto cas = make_temp_override (current_access_specifier);
19218 : 1956362 : set_current_access_from_decl (TYPE_NAME (enumtype));
19219 : 1956362 : finish_member_declaration (decl);
19220 : 1956362 : }
19221 : : else
19222 : 9034986 : pushdecl (decl);
19223 : :
19224 : : /* Add this enumeration constant to the list for this type. */
19225 : 10991348 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
19226 : :
19227 : 10991348 : return decl;
19228 : : }
19229 : :
19230 : : /* Look for an enumerator with the given NAME within the enumeration
19231 : : type ENUMTYPE. This routine is used primarily for qualified name
19232 : : lookup into an enumerator in C++0x, e.g.,
19233 : :
19234 : : enum class Color { Red, Green, Blue };
19235 : :
19236 : : Color color = Color::Red;
19237 : :
19238 : : Returns the value corresponding to the enumerator, or
19239 : : NULL_TREE if no such enumerator was found. */
19240 : : tree
19241 : 11221541 : lookup_enumerator (tree enumtype, tree name)
19242 : : {
19243 : 11221541 : tree e;
19244 : 11221541 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
19245 : :
19246 : 11221541 : e = purpose_member (name, TYPE_VALUES (enumtype));
19247 : 22442973 : return e? TREE_VALUE (e) : NULL_TREE;
19248 : : }
19249 : :
19250 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
19251 : :
19252 : : tree
19253 : 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
19254 : : vec<string_int_pair> *values)
19255 : : {
19256 : 0 : location_t saved_loc = input_location;
19257 : 0 : input_location = loc;
19258 : :
19259 : 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
19260 : : NULL_TREE, false, NULL);
19261 : 0 : if (!OPAQUE_ENUM_P (enumtype))
19262 : : {
19263 : 0 : auto_diagnostic_group d;
19264 : 0 : error_at (loc, "multiple definition of %q#T", enumtype);
19265 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
19266 : : "previous definition here");
19267 : 0 : return enumtype;
19268 : 0 : }
19269 : 0 : SET_OPAQUE_ENUM_P (enumtype, false);
19270 : 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
19271 : :
19272 : 0 : for (const string_int_pair &value : values)
19273 : 0 : build_enumerator (get_identifier (value.first),
19274 : 0 : build_int_cst (integer_type_node, value.second),
19275 : : enumtype, NULL_TREE, loc);
19276 : :
19277 : 0 : finish_enum_value_list (enumtype);
19278 : 0 : finish_enum (enumtype);
19279 : :
19280 : 0 : input_location = saved_loc;
19281 : 0 : return enumtype;
19282 : : }
19283 : :
19284 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
19285 : :
19286 : : tree
19287 : 0 : cxx_simulate_record_decl (location_t loc, const char *name,
19288 : : array_slice<const tree> fields)
19289 : : {
19290 : 0 : iloc_sentinel ils (loc);
19291 : :
19292 : 0 : tree ident = get_identifier (name);
19293 : 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
19294 : 0 : if (type != error_mark_node
19295 : 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
19296 : : {
19297 : 0 : error ("redefinition of %q#T", type);
19298 : 0 : type = error_mark_node;
19299 : : }
19300 : 0 : if (type == error_mark_node)
19301 : 0 : return lhd_simulate_record_decl (loc, name, fields);
19302 : :
19303 : 0 : xref_basetypes (type, NULL_TREE);
19304 : 0 : type = begin_class_definition (type);
19305 : 0 : if (type == error_mark_node)
19306 : 0 : return lhd_simulate_record_decl (loc, name, fields);
19307 : :
19308 : 0 : for (tree field : fields)
19309 : 0 : finish_member_declaration (field);
19310 : :
19311 : 0 : type = finish_struct (type, NULL_TREE);
19312 : :
19313 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
19314 : 0 : set_underlying_type (decl);
19315 : 0 : lang_hooks.decls.pushdecl (decl);
19316 : :
19317 : 0 : return type;
19318 : 0 : }
19319 : :
19320 : : /* We're defining DECL. Make sure that its type is OK. */
19321 : :
19322 : : static void
19323 : 142532430 : check_function_type (tree decl, tree current_function_parms)
19324 : : {
19325 : 142532430 : tree fntype = TREE_TYPE (decl);
19326 : 142532430 : tree return_type = complete_type (TREE_TYPE (fntype));
19327 : :
19328 : : /* In a function definition, arg types must be complete. */
19329 : 142532430 : require_complete_types_for_parms (current_function_parms);
19330 : :
19331 : 142532430 : if (dependent_type_p (return_type)
19332 : 142532430 : || type_uses_auto (return_type))
19333 : 43209132 : return;
19334 : 99323298 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
19335 : : {
19336 : 12 : tree args = TYPE_ARG_TYPES (fntype);
19337 : :
19338 : 12 : error ("return type %q#T is incomplete", return_type);
19339 : :
19340 : : /* Make it return void instead. */
19341 : 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
19342 : 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
19343 : : void_type_node,
19344 : 3 : TREE_CHAIN (args));
19345 : : else
19346 : 18 : fntype = build_function_type (void_type_node, args,
19347 : 9 : TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
19348 : 12 : fntype = (cp_build_type_attribute_variant
19349 : 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
19350 : 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
19351 : 12 : TREE_TYPE (decl) = fntype;
19352 : : }
19353 : : else
19354 : : {
19355 : 99323286 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
19356 : 99323286 : maybe_warn_parm_abi (TREE_TYPE (fntype),
19357 : 99323286 : DECL_SOURCE_LOCATION (decl));
19358 : : }
19359 : : }
19360 : :
19361 : : /* True iff FN is an implicitly-defined default constructor. */
19362 : :
19363 : : static bool
19364 : 14306477 : implicit_default_ctor_p (tree fn)
19365 : : {
19366 : 14306477 : return (DECL_CONSTRUCTOR_P (fn)
19367 : 14306477 : && !user_provided_p (fn)
19368 : 16297659 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
19369 : : }
19370 : :
19371 : : /* Clobber the contents of *this to let the back end know that the object
19372 : : storage is dead when we enter the constructor or leave the destructor. */
19373 : :
19374 : : static tree
19375 : 1310041 : build_clobber_this (clobber_kind kind)
19376 : : {
19377 : : /* Clobbering an empty base is pointless, and harmful if its one byte
19378 : : TYPE_SIZE overlays real data. */
19379 : 1310041 : if (is_empty_class (current_class_type))
19380 : 0 : return void_node;
19381 : :
19382 : : /* If we have virtual bases, clobber the whole object, but only if we're in
19383 : : charge. If we don't have virtual bases, clobber the as-base type so we
19384 : : don't mess with tail padding. */
19385 : 1310041 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
19386 : :
19387 : 1310041 : tree ctype = current_class_type;
19388 : 1310041 : if (!vbases)
19389 : 1301300 : ctype = CLASSTYPE_AS_BASE (ctype);
19390 : :
19391 : 1310041 : tree clobber = build_clobber (ctype, kind);
19392 : :
19393 : 1310041 : tree thisref = current_class_ref;
19394 : 1310041 : if (ctype != current_class_type)
19395 : : {
19396 : 130867 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
19397 : 130867 : thisref = convert_from_reference (thisref);
19398 : : }
19399 : :
19400 : 1310041 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
19401 : 1310041 : if (kind == CLOBBER_OBJECT_BEGIN)
19402 : 0 : TREE_SET_CODE (exprstmt, INIT_EXPR);
19403 : 1310041 : if (vbases)
19404 : 8741 : exprstmt = build_if_in_charge (exprstmt);
19405 : :
19406 : : return exprstmt;
19407 : : }
19408 : :
19409 : : /* Create the FUNCTION_DECL for a function definition.
19410 : : DECLSPECS and DECLARATOR are the parts of the declaration;
19411 : : they describe the function's name and the type it returns,
19412 : : but twisted together in a fashion that parallels the syntax of C.
19413 : :
19414 : : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
19415 : : DECLARATOR is really the DECL for the function we are about to
19416 : : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
19417 : : indicating that the function is an inline defined in-class.
19418 : :
19419 : : This function creates a binding context for the function body
19420 : : as well as setting up the FUNCTION_DECL in current_function_decl.
19421 : :
19422 : : For C++, we must first check whether that datum makes any sense.
19423 : : For example, "class A local_a(1,2);" means that variable local_a
19424 : : is an aggregate of type A, which should have a constructor
19425 : : applied to it with the argument list [1, 2].
19426 : :
19427 : : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
19428 : : or may be a BLOCK if the function has been defined previously
19429 : : in this translation unit. On exit, DECL_INITIAL (decl1) will be
19430 : : error_mark_node if the function has never been defined, or
19431 : : a BLOCK if the function has been defined somewhere. */
19432 : :
19433 : : bool
19434 : 142532466 : start_preparsed_function (tree decl1, tree attrs, int flags)
19435 : : {
19436 : 142532466 : tree ctype = NULL_TREE;
19437 : 142532466 : bool doing_friend = false;
19438 : :
19439 : : /* Sanity check. */
19440 : 142532466 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
19441 : 142532466 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
19442 : :
19443 : 142532466 : tree fntype = TREE_TYPE (decl1);
19444 : 142532466 : if (DECL_CLASS_SCOPE_P (decl1))
19445 : 103116626 : ctype = DECL_CONTEXT (decl1);
19446 : : else
19447 : : {
19448 : 78831680 : ctype = DECL_FRIEND_CONTEXT (decl1);
19449 : :
19450 : 39415839 : if (ctype)
19451 : 142532466 : doing_friend = true;
19452 : : }
19453 : :
19454 : : /* Adjust for #pragma target/optimize if this is an artificial function that
19455 : : (probably) didn't go through grokfndecl. We particularly don't want this
19456 : : for deferred instantiations, which should match their template. */
19457 : 142532466 : if (DECL_ARTIFICIAL (decl1))
19458 : 3024705 : decl_attributes (&decl1, NULL_TREE, 0);
19459 : :
19460 : 142532466 : if (DECL_DECLARED_INLINE_P (decl1)
19461 : 142532466 : && lookup_attribute ("noinline", attrs))
19462 : : {
19463 : 0 : auto_urlify_attributes sentinel;
19464 : 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
19465 : : "inline function %qD given attribute %qs", decl1, "noinline");
19466 : 0 : }
19467 : :
19468 : : /* Handle gnu_inline attribute. */
19469 : 142532466 : if (GNU_INLINE_P (decl1))
19470 : : {
19471 : 1575938 : DECL_EXTERNAL (decl1) = 1;
19472 : 1575938 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
19473 : 1575938 : DECL_INTERFACE_KNOWN (decl1) = 1;
19474 : 1575938 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
19475 : : }
19476 : :
19477 : 142532466 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
19478 : : /* This is a constructor, we must ensure that any default args
19479 : : introduced by this definition are propagated to the clones
19480 : : now. The clones are used directly in overload resolution. */
19481 : 18616988 : adjust_clone_args (decl1);
19482 : :
19483 : : /* Sometimes we don't notice that a function is a static member, and
19484 : : build a METHOD_TYPE for it. Fix that up now. */
19485 : 142532466 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
19486 : : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
19487 : :
19488 : : /* Set up current_class_type, and enter the scope of the class, if
19489 : : appropriate. */
19490 : 142532466 : if (ctype)
19491 : 106103323 : push_nested_class (ctype);
19492 : :
19493 : : /* Now that we have entered the scope of the class, we must restore
19494 : : the bindings for any template parameters surrounding DECL1, if it
19495 : : is an inline member template. (Order is important; consider the
19496 : : case where a template parameter has the same name as a field of
19497 : : the class.) It is not until after this point that
19498 : : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19499 : 142532466 : if (flags & SF_INCLASS_INLINE)
19500 : 69968242 : maybe_begin_member_template_processing (decl1);
19501 : :
19502 : : /* Effective C++ rule 15. */
19503 : 142532466 : if (warn_ecpp
19504 : 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19505 : 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19506 : 142532496 : && VOID_TYPE_P (TREE_TYPE (fntype)))
19507 : 0 : warning (OPT_Weffc__,
19508 : : "%<operator=%> should return a reference to %<*this%>");
19509 : :
19510 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
19511 : : error_mark_node is replaced below (in poplevel) with the BLOCK. */
19512 : 142532466 : if (!DECL_INITIAL (decl1))
19513 : 29834920 : DECL_INITIAL (decl1) = error_mark_node;
19514 : :
19515 : : /* This function exists in static storage.
19516 : : (This does not mean `static' in the C sense!) */
19517 : 142532466 : TREE_STATIC (decl1) = 1;
19518 : :
19519 : : /* We must call push_template_decl after current_class_type is set
19520 : : up. (If we are processing inline definitions after exiting a
19521 : : class scope, current_class_type will be NULL_TREE until set above
19522 : : by push_nested_class.) */
19523 : 142532466 : if (processing_template_decl)
19524 : : {
19525 : 85895134 : tree newdecl1 = push_template_decl (decl1, doing_friend);
19526 : 85895134 : if (newdecl1 == error_mark_node)
19527 : : {
19528 : 36 : if (ctype)
19529 : 9 : pop_nested_class ();
19530 : 36 : return false;
19531 : : }
19532 : 85895098 : decl1 = newdecl1;
19533 : : }
19534 : :
19535 : : /* Make sure the parameter and return types are reasonable. When
19536 : : you declare a function, these types can be incomplete, but they
19537 : : must be complete when you define the function. */
19538 : 142532430 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
19539 : :
19540 : : /* Build the return declaration for the function. */
19541 : 142532430 : tree restype = TREE_TYPE (fntype);
19542 : :
19543 : 142532430 : if (DECL_RESULT (decl1) == NULL_TREE)
19544 : : {
19545 : : /* In a template instantiation, copy the return type location. When
19546 : : parsing, the location will be set in grokdeclarator. */
19547 : 56754656 : location_t loc = input_location;
19548 : 56754656 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
19549 : : {
19550 : 26758779 : tree tmpl = template_for_substitution (decl1);
19551 : 26758779 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
19552 : 25674796 : loc = DECL_SOURCE_LOCATION (res);
19553 : : }
19554 : :
19555 : 56754656 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
19556 : 56754656 : DECL_ARTIFICIAL (resdecl) = 1;
19557 : 56754656 : DECL_IGNORED_P (resdecl) = 1;
19558 : 56754656 : DECL_RESULT (decl1) = resdecl;
19559 : :
19560 : 56754656 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
19561 : : }
19562 : :
19563 : : /* Record the decl so that the function name is defined.
19564 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
19565 : : use the old decl. */
19566 : 142532430 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
19567 : : {
19568 : : /* A specialization is not used to guide overload resolution. */
19569 : 19940184 : if (!DECL_FUNCTION_MEMBER_P (decl1)
19570 : 19926939 : && !(DECL_USE_TEMPLATE (decl1) &&
19571 : 3648 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
19572 : : {
19573 : 9567179 : tree olddecl = pushdecl (decl1);
19574 : :
19575 : 9567179 : if (olddecl == error_mark_node)
19576 : : /* If something went wrong when registering the declaration,
19577 : : use DECL1; we have to have a FUNCTION_DECL to use when
19578 : : parsing the body of the function. */
19579 : : ;
19580 : : else
19581 : : {
19582 : : /* Otherwise, OLDDECL is either a previous declaration
19583 : : of the same function or DECL1 itself. */
19584 : :
19585 : 9566969 : if (warn_missing_declarations
19586 : 21 : && olddecl == decl1
19587 : 15 : && !DECL_MAIN_P (decl1)
19588 : 15 : && TREE_PUBLIC (decl1)
19589 : 9566981 : && !DECL_DECLARED_INLINE_P (decl1))
19590 : : {
19591 : 9 : tree context;
19592 : :
19593 : : /* Check whether DECL1 is in an anonymous
19594 : : namespace. */
19595 : 9 : for (context = DECL_CONTEXT (decl1);
19596 : 18 : context;
19597 : 9 : context = DECL_CONTEXT (context))
19598 : : {
19599 : 12 : if (TREE_CODE (context) == NAMESPACE_DECL
19600 : 12 : && DECL_NAME (context) == NULL_TREE)
19601 : : break;
19602 : : }
19603 : :
19604 : 9 : if (context == NULL)
19605 : 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
19606 : 6 : OPT_Wmissing_declarations,
19607 : : "no previous declaration for %qD", decl1);
19608 : : }
19609 : :
19610 : 9566969 : decl1 = olddecl;
19611 : : }
19612 : : }
19613 : : else
19614 : : {
19615 : : /* We need to set the DECL_CONTEXT. */
19616 : 788933 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
19617 : 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
19618 : : }
19619 : 10356112 : fntype = TREE_TYPE (decl1);
19620 : 10356112 : restype = TREE_TYPE (fntype);
19621 : :
19622 : : /* If #pragma weak applies, mark the decl appropriately now.
19623 : : The pragma only applies to global functions. Because
19624 : : determining whether or not the #pragma applies involves
19625 : : computing the mangled name for the declaration, we cannot
19626 : : apply the pragma until after we have merged this declaration
19627 : : with any previous declarations; if the original declaration
19628 : : has a linkage specification, that specification applies to
19629 : : the definition as well, and may affect the mangled name. */
19630 : 10356112 : if (DECL_FILE_SCOPE_P (decl1))
19631 : 2351658 : maybe_apply_pragma_weak (decl1);
19632 : : }
19633 : :
19634 : : /* We are now in the scope of the function being defined. */
19635 : 142532430 : current_function_decl = decl1;
19636 : :
19637 : : /* Save the parm names or decls from this function's declarator
19638 : : where store_parm_decls will find them. */
19639 : 142532430 : tree current_function_parms = DECL_ARGUMENTS (decl1);
19640 : :
19641 : : /* Let the user know we're compiling this function. */
19642 : 142532430 : announce_function (decl1);
19643 : :
19644 : 142532430 : gcc_assert (DECL_INITIAL (decl1));
19645 : :
19646 : : /* This function may already have been parsed, in which case just
19647 : : return; our caller will skip over the body without parsing. */
19648 : 142532430 : if (DECL_INITIAL (decl1) != error_mark_node)
19649 : : return true;
19650 : :
19651 : : /* Initialize RTL machinery. We cannot do this until
19652 : : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
19653 : : even when processing a template; this is how we get
19654 : : CFUN set up, and our per-function variables initialized.
19655 : : FIXME factor out the non-RTL stuff. */
19656 : 142532430 : cp_binding_level *bl = current_binding_level;
19657 : 142532430 : allocate_struct_function (decl1, processing_template_decl);
19658 : :
19659 : : /* Initialize the language data structures. Whenever we start
19660 : : a new function, we destroy temporaries in the usual way. */
19661 : 142532430 : cfun->language = ggc_cleared_alloc<language_function> ();
19662 : 142532430 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
19663 : 142532430 : current_binding_level = bl;
19664 : :
19665 : : /* If we are (erroneously) defining a function that we have already
19666 : : defined before, wipe out what we knew before. */
19667 : 142532430 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
19668 : 142532430 : FNDECL_USED_AUTO (decl1) = false;
19669 : 142532430 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
19670 : :
19671 : 142532430 : if (!processing_template_decl && type_uses_auto (restype))
19672 : : {
19673 : 837349 : FNDECL_USED_AUTO (decl1) = true;
19674 : 837349 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
19675 : : }
19676 : :
19677 : : /* Start the statement-tree, start the tree now. */
19678 : 142532430 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
19679 : :
19680 : 142532430 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
19681 : : {
19682 : : /* We know that this was set up by `grokclassfn'. We do not
19683 : : wait until `store_parm_decls', since evil parse errors may
19684 : : never get us to that point. Here we keep the consistency
19685 : : between `current_class_type' and `current_class_ptr'. */
19686 : 91897984 : tree t = DECL_ARGUMENTS (decl1);
19687 : :
19688 : 91897984 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
19689 : 91897984 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
19690 : :
19691 : 91897984 : cp_function_chain->x_current_class_ref
19692 : 91897984 : = cp_build_fold_indirect_ref (t);
19693 : : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
19694 : 91897984 : cp_function_chain->x_current_class_ptr = t;
19695 : :
19696 : : /* Constructors and destructors need to know whether they're "in
19697 : : charge" of initializing virtual base classes. */
19698 : 91897984 : t = DECL_CHAIN (t);
19699 : 91897984 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
19700 : : {
19701 : 20055 : current_in_charge_parm = t;
19702 : 20055 : t = DECL_CHAIN (t);
19703 : : }
19704 : 91897984 : if (DECL_HAS_VTT_PARM_P (decl1))
19705 : : {
19706 : 40123 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
19707 : 40123 : current_vtt_parm = t;
19708 : : }
19709 : : }
19710 : :
19711 : 142532430 : bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
19712 : : /* Implicitly-defined methods (like the
19713 : : destructor for a class in which no destructor
19714 : : is explicitly declared) must not be defined
19715 : : until their definition is needed. So, we
19716 : : ignore interface specifications for
19717 : : compiler-generated functions. */
19718 : 142532430 : && !DECL_ARTIFICIAL (decl1));
19719 : 142532430 : struct c_fileinfo *finfo
19720 : 142532430 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
19721 : :
19722 : 142532430 : if (processing_template_decl)
19723 : : /* Don't mess with interface flags. */;
19724 : 56637332 : else if (DECL_INTERFACE_KNOWN (decl1))
19725 : : {
19726 : 17487166 : tree ctx = decl_function_context (decl1);
19727 : :
19728 : 17487166 : if (DECL_NOT_REALLY_EXTERN (decl1))
19729 : 13996617 : DECL_EXTERNAL (decl1) = 0;
19730 : :
19731 : 17487166 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
19732 : : /* This is a function in a local class in an extern inline
19733 : : or template function. */
19734 : 2806934 : comdat_linkage (decl1);
19735 : : }
19736 : : /* If this function belongs to an interface, it is public.
19737 : : If it belongs to someone else's interface, it is also external.
19738 : : This only affects inlines and template instantiations. */
19739 : 39150166 : else if (!finfo->interface_unknown && honor_interface)
19740 : : {
19741 : 112 : if (DECL_DECLARED_INLINE_P (decl1)
19742 : 112 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19743 : : {
19744 : 88 : DECL_EXTERNAL (decl1)
19745 : 176 : = (finfo->interface_only
19746 : 88 : || (DECL_DECLARED_INLINE_P (decl1)
19747 : 42 : && ! flag_implement_inlines
19748 : 0 : && !DECL_VINDEX (decl1)));
19749 : :
19750 : : /* For WIN32 we also want to put these in linkonce sections. */
19751 : 88 : maybe_make_one_only (decl1);
19752 : : }
19753 : : else
19754 : 24 : DECL_EXTERNAL (decl1) = 0;
19755 : 112 : DECL_INTERFACE_KNOWN (decl1) = 1;
19756 : : /* If this function is in an interface implemented in this file,
19757 : : make sure that the back end knows to emit this function
19758 : : here. */
19759 : 112 : if (!DECL_EXTERNAL (decl1))
19760 : 66 : mark_needed (decl1);
19761 : : }
19762 : 39150054 : else if (finfo->interface_unknown && finfo->interface_only
19763 : 0 : && honor_interface)
19764 : : {
19765 : : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
19766 : : interface, we will have both finfo->interface_unknown and
19767 : : finfo->interface_only set. In that case, we don't want to
19768 : : use the normal heuristics because someone will supply a
19769 : : #pragma implementation elsewhere, and deducing it here would
19770 : : produce a conflict. */
19771 : 0 : comdat_linkage (decl1);
19772 : 0 : DECL_EXTERNAL (decl1) = 0;
19773 : 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
19774 : 0 : DECL_DEFER_OUTPUT (decl1) = 1;
19775 : : }
19776 : : else
19777 : : {
19778 : : /* This is a definition, not a reference.
19779 : : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
19780 : 39150054 : if (!GNU_INLINE_P (decl1))
19781 : 39150051 : DECL_EXTERNAL (decl1) = 0;
19782 : :
19783 : 39150054 : if ((DECL_DECLARED_INLINE_P (decl1)
19784 : 2080258 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19785 : 40996984 : && ! DECL_INTERFACE_KNOWN (decl1))
19786 : 38916726 : DECL_DEFER_OUTPUT (decl1) = 1;
19787 : : else
19788 : 233328 : DECL_INTERFACE_KNOWN (decl1) = 1;
19789 : : }
19790 : :
19791 : : /* Determine the ELF visibility attribute for the function. We must not
19792 : : do this before calling "pushdecl", as we must allow "duplicate_decls"
19793 : : to merge any attributes appropriately. We also need to wait until
19794 : : linkage is set. */
19795 : 142532430 : if (!DECL_CLONED_FUNCTION_P (decl1))
19796 : 128629542 : determine_visibility (decl1);
19797 : :
19798 : 142532430 : if (!processing_template_decl)
19799 : 56637332 : maybe_instantiate_noexcept (decl1);
19800 : :
19801 : 142532430 : begin_scope (sk_function_parms, decl1);
19802 : :
19803 : 142532430 : ++function_depth;
19804 : :
19805 : 142532430 : start_fname_decls ();
19806 : :
19807 : 142532430 : store_parm_decls (current_function_parms);
19808 : :
19809 : 142532430 : start_function_contracts (decl1);
19810 : :
19811 : 142532430 : if (!processing_template_decl
19812 : 56637332 : && flag_lifetime_dse > 1
19813 : 113255100 : && DECL_CONSTRUCTOR_P (decl1)
19814 : : /* Clobbering an empty base is harmful if it overlays real data. */
19815 : 15918417 : && !is_empty_class (current_class_type)
19816 : : /* We can't clobber safely for an implicitly-defined default constructor
19817 : : because part of the initialization might happen before we enter the
19818 : : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
19819 : 14305888 : && !implicit_default_ctor_p (decl1)
19820 : 155544467 : && !lookup_attribute ("clobber *this",
19821 : 13012037 : DECL_ATTRIBUTES (current_class_ptr)))
19822 : 13007123 : DECL_ATTRIBUTES (current_class_ptr)
19823 : 26014246 : = tree_cons (get_identifier ("clobber *this"), NULL_TREE,
19824 : 13007123 : DECL_ATTRIBUTES (current_class_ptr));
19825 : :
19826 : 142532430 : if (!processing_template_decl
19827 : 113274664 : && DECL_CONSTRUCTOR_P (decl1)
19828 : 15920913 : && sanitize_flags_p (SANITIZE_VPTR)
19829 : 1767 : && !DECL_CLONED_FUNCTION_P (decl1)
19830 : 142533019 : && !implicit_default_ctor_p (decl1))
19831 : 433 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
19832 : :
19833 : 142532430 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
19834 : 142531862 : start_lambda_scope (decl1);
19835 : :
19836 : : return true;
19837 : : }
19838 : :
19839 : :
19840 : : /* Like start_preparsed_function, except that instead of a
19841 : : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
19842 : :
19843 : : Returns true on success. If the DECLARATOR is not suitable
19844 : : for a function, we return false, which tells the parser to
19845 : : skip the entire function. */
19846 : :
19847 : : bool
19848 : 38763067 : start_function (cp_decl_specifier_seq *declspecs,
19849 : : const cp_declarator *declarator,
19850 : : tree attrs)
19851 : : {
19852 : 38763067 : tree decl1;
19853 : :
19854 : 38763067 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
19855 : 38763067 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
19856 : 38763067 : if (decl1 == error_mark_node)
19857 : : return false;
19858 : :
19859 : 38762778 : if (DECL_MAIN_P (decl1))
19860 : : /* main must return int. grokfndecl should have corrected it
19861 : : (and issued a diagnostic) if the user got it wrong. */
19862 : 33219 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
19863 : : integer_type_node));
19864 : :
19865 : 38762778 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
19866 : : }
19867 : :
19868 : : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
19869 : : FN. */
19870 : :
19871 : : bool
19872 : 272756973 : use_eh_spec_block (tree fn)
19873 : : {
19874 : 270849091 : return (flag_exceptions && flag_enforce_eh_specs
19875 : 270849091 : && !processing_template_decl
19876 : : /* We insert the EH_SPEC_BLOCK only in the original
19877 : : function; then, it is copied automatically to the
19878 : : clones. */
19879 : 98562528 : && !DECL_CLONED_FUNCTION_P (fn)
19880 : : /* Implicitly-generated constructors and destructors have
19881 : : exception specifications. However, those specifications
19882 : : are the union of the possible exceptions specified by the
19883 : : constructors/destructors for bases and members, so no
19884 : : unallowed exception will ever reach this function. By
19885 : : not creating the EH_SPEC_BLOCK we save a little memory,
19886 : : and we avoid spurious warnings about unreachable
19887 : : code. */
19888 : 84699442 : && !DECL_DEFAULTED_FN (fn)
19889 : 355114875 : && !type_throw_all_p (TREE_TYPE (fn)));
19890 : : }
19891 : :
19892 : : /* Helper function to push ARGS into the current lexical scope. DECL
19893 : : is the function declaration. NONPARMS is used to handle enum
19894 : : constants. */
19895 : :
19896 : : void
19897 : 139112701 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
19898 : : {
19899 : : /* If we're doing semantic analysis, then we'll call pushdecl
19900 : : for each of these. We must do them in reverse order so that
19901 : : they end in the correct forward order. */
19902 : 139112701 : args = nreverse (args);
19903 : :
19904 : 139112701 : tree next;
19905 : 425042454 : for (tree parm = args; parm; parm = next)
19906 : : {
19907 : 285929753 : next = DECL_CHAIN (parm);
19908 : 285929753 : if (TREE_CODE (parm) == PARM_DECL)
19909 : 285929753 : pushdecl (parm);
19910 : 0 : else if (nonparms)
19911 : : {
19912 : : /* If we find an enum constant or a type tag, put it aside for
19913 : : the moment. */
19914 : 0 : TREE_CHAIN (parm) = NULL_TREE;
19915 : 0 : *nonparms = chainon (*nonparms, parm);
19916 : : }
19917 : : }
19918 : :
19919 : : /* Get the decls in their original chain order and record in the
19920 : : function. This is all and only the PARM_DECLs that were
19921 : : pushed into scope by the loop above. */
19922 : 139112701 : DECL_ARGUMENTS (decl) = get_local_decls ();
19923 : 139112701 : }
19924 : :
19925 : : /* Store the parameter declarations into the current function declaration.
19926 : : This is called after parsing the parameter declarations, before
19927 : : digesting the body of the function.
19928 : :
19929 : : Also install to binding contour return value identifier, if any. */
19930 : :
19931 : : static void
19932 : 142532430 : store_parm_decls (tree current_function_parms)
19933 : : {
19934 : 142532430 : tree fndecl = current_function_decl;
19935 : :
19936 : : /* This is a chain of any other decls that came in among the parm
19937 : : declarations. If a parm is declared with enum {foo, bar} x;
19938 : : then CONST_DECLs for foo and bar are put here. */
19939 : 142532430 : tree nonparms = NULL_TREE;
19940 : :
19941 : 142532430 : if (current_function_parms)
19942 : : {
19943 : : /* This case is when the function was defined with an ANSI prototype.
19944 : : The parms already have decls, so we need not do anything here
19945 : : except record them as in effect
19946 : : and complain if any redundant old-style parm decls were written. */
19947 : :
19948 : 136511583 : tree specparms = current_function_parms;
19949 : :
19950 : : /* Must clear this because it might contain TYPE_DECLs declared
19951 : : at class level. */
19952 : 136511583 : current_binding_level->names = NULL;
19953 : :
19954 : 136511583 : do_push_parm_decls (fndecl, specparms, &nonparms);
19955 : : }
19956 : : else
19957 : 6020847 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
19958 : :
19959 : : /* Now store the final chain of decls for the arguments
19960 : : as the decl-chain of the current lexical scope.
19961 : : Put the enumerators in as well, at the front so that
19962 : : DECL_ARGUMENTS is not modified. */
19963 : 142532430 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
19964 : :
19965 : 142532430 : if (use_eh_spec_block (current_function_decl))
19966 : 18855618 : current_eh_spec_block = begin_eh_spec_block ();
19967 : 142532430 : }
19968 : :
19969 : :
19970 : : /* Mark CDTOR's implicit THIS argument for returning, if required by
19971 : : the ABI.. Return the decl for THIS, if it is to be returned, and
19972 : : NULL otherwise. */
19973 : :
19974 : : tree
19975 : 112735273 : maybe_prepare_return_this (tree cdtor)
19976 : : {
19977 : 112735273 : if (targetm.cxx.cdtor_returns_this ())
19978 : 0 : if (tree val = DECL_ARGUMENTS (cdtor))
19979 : : {
19980 : 0 : suppress_warning (val, OPT_Wuse_after_free);
19981 : 0 : return val;
19982 : : }
19983 : :
19984 : : return NULL_TREE;
19985 : : }
19986 : :
19987 : : /* Set the return value of the [cd]tor if the ABI wants that. */
19988 : :
19989 : : void
19990 : 7083110 : maybe_return_this ()
19991 : : {
19992 : 7083110 : if (tree val = maybe_prepare_return_this (current_function_decl))
19993 : : {
19994 : : /* Return the address of the object. */
19995 : 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
19996 : 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
19997 : 0 : DECL_RESULT (current_function_decl), val);
19998 : 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
19999 : 0 : add_stmt (exprstmt);
20000 : : }
20001 : 7083110 : }
20002 : :
20003 : : /* Do all the processing for the beginning of a destructor; set up the
20004 : : vtable pointers and cleanups for bases and members. */
20005 : :
20006 : : static void
20007 : 1504739 : begin_destructor_body (void)
20008 : : {
20009 : 1504739 : tree compound_stmt;
20010 : :
20011 : : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
20012 : : issued an error message. We still want to try to process the
20013 : : body of the function, but initialize_vtbl_ptrs will crash if
20014 : : TYPE_BINFO is NULL. */
20015 : 1504739 : if (COMPLETE_TYPE_P (current_class_type))
20016 : : {
20017 : 1504739 : compound_stmt = begin_compound_stmt (0);
20018 : : /* Make all virtual function table pointers in non-virtual base
20019 : : classes point to CURRENT_CLASS_TYPE's virtual function
20020 : : tables. */
20021 : 1504739 : initialize_vtbl_ptrs (current_class_ptr);
20022 : 1504739 : finish_compound_stmt (compound_stmt);
20023 : :
20024 : 1504739 : if (flag_lifetime_dse
20025 : : /* Clobbering an empty base is harmful if it overlays real data. */
20026 : 1504739 : && !is_empty_class (current_class_type))
20027 : : {
20028 : 1310104 : if (sanitize_flags_p (SANITIZE_VPTR)
20029 : 223 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
20030 : 1310167 : && TYPE_CONTAINS_VPTR_P (current_class_type))
20031 : : {
20032 : 63 : tree binfo = TYPE_BINFO (current_class_type);
20033 : 63 : tree ref
20034 : 63 : = cp_build_fold_indirect_ref (current_class_ptr);
20035 : :
20036 : 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
20037 : 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
20038 : 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
20039 : : NOP_EXPR, vtbl,
20040 : : tf_warning_or_error);
20041 : : /* If the vptr is shared with some virtual nearly empty base,
20042 : : don't clear it if not in charge, the dtor of the virtual
20043 : : nearly empty base will do that later. */
20044 : 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
20045 : : {
20046 : : tree c = current_class_type;
20047 : 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
20048 : : {
20049 : 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
20050 : : {
20051 : 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
20052 : : tf_warning_or_error);
20053 : 36 : stmt = build_if_in_charge (stmt);
20054 : 36 : break;
20055 : : }
20056 : 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
20057 : : }
20058 : : }
20059 : 63 : finish_decl_cleanup (NULL_TREE, stmt);
20060 : : }
20061 : : else
20062 : 1310041 : finish_decl_cleanup (NULL_TREE,
20063 : : build_clobber_this (CLOBBER_OBJECT_END));
20064 : : }
20065 : :
20066 : : /* And insert cleanups for our bases and members so that they
20067 : : will be properly destroyed if we throw. */
20068 : 1504739 : push_base_cleanups ();
20069 : : }
20070 : 1504739 : }
20071 : :
20072 : : /* Do the necessary processing for the beginning of a function body, which
20073 : : in this case includes member-initializers, but not the catch clauses of
20074 : : a function-try-block. Currently, this means opening a binding level
20075 : : for the member-initializers (in a ctor), member cleanups (in a dtor),
20076 : : and capture proxies (in a lambda operator()). */
20077 : :
20078 : : tree
20079 : 113547259 : begin_function_body (void)
20080 : : {
20081 : 232039400 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
20082 : : return NULL_TREE;
20083 : :
20084 : 22635593 : if (processing_template_decl)
20085 : : /* Do nothing now. */;
20086 : : else
20087 : : /* Always keep the BLOCK node associated with the outermost pair of
20088 : : curly braces of a function. These are needed for correct
20089 : : operation of dwarfout.c. */
20090 : 7268014 : keep_next_level (true);
20091 : :
20092 : 22635593 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
20093 : 22635593 : current_binding_level->artificial = 1;
20094 : :
20095 : 22635593 : if (processing_template_decl)
20096 : : /* Do nothing now. */;
20097 : 14536028 : else if (DECL_DESTRUCTOR_P (current_function_decl))
20098 : 1504739 : begin_destructor_body ();
20099 : :
20100 : : return stmt;
20101 : : }
20102 : :
20103 : : /* Do the processing for the end of a function body. Currently, this means
20104 : : closing out the cleanups for fully-constructed bases and members, and in
20105 : : the case of the destructor, deleting the object if desired. Again, this
20106 : : is only meaningful for [cd]tors, since they are the only functions where
20107 : : there is a significant distinction between the main body and any
20108 : : function catch clauses. Handling, say, main() return semantics here
20109 : : would be wrong, as flowing off the end of a function catch clause for
20110 : : main() would also need to return 0. */
20111 : :
20112 : : void
20113 : 113547235 : finish_function_body (tree compstmt)
20114 : : {
20115 : 113547235 : if (compstmt == NULL_TREE)
20116 : : return;
20117 : :
20118 : : /* Close the block. */
20119 : 22635593 : finish_compound_stmt (compstmt);
20120 : :
20121 : 22635593 : if (processing_template_decl)
20122 : : /* Do nothing now. */;
20123 : 7268014 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
20124 : 7268014 : || DECL_DESTRUCTOR_P (current_function_decl))
20125 : 6809016 : maybe_return_this ();
20126 : : }
20127 : :
20128 : : /* Given a function, returns the BLOCK corresponding to the outermost level
20129 : : of curly braces, skipping the artificial block created for constructor
20130 : : initializers. */
20131 : :
20132 : : tree
20133 : 307504 : outer_curly_brace_block (tree fndecl)
20134 : : {
20135 : 307504 : tree block = DECL_INITIAL (fndecl);
20136 : 307504 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20137 : : return block;
20138 : 1076 : block = BLOCK_SUBBLOCKS (block);
20139 : 1076 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
20140 : : return block;
20141 : 0 : block = BLOCK_SUBBLOCKS (block);
20142 : 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
20143 : : return block;
20144 : : }
20145 : :
20146 : : /* If FNDECL is a class's key method, add the class to the list of
20147 : : keyed classes that should be emitted. */
20148 : :
20149 : : static void
20150 : 142536259 : record_key_method_defined (tree fndecl)
20151 : : {
20152 : 193174510 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
20153 : 91958955 : && DECL_VIRTUAL_P (fndecl)
20154 : 145165252 : && !processing_template_decl)
20155 : : {
20156 : 1302641 : tree fnclass = DECL_CONTEXT (fndecl);
20157 : 1302641 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
20158 : : {
20159 : 1612 : tree classdecl = TYPE_NAME (fnclass);
20160 : : /* Classes attached to a named module are already handled. */
20161 : 1612 : if (!DECL_LANG_SPECIFIC (classdecl)
20162 : 1660 : || !DECL_MODULE_ATTACH_P (classdecl))
20163 : 1567 : vec_safe_push (keyed_classes, fnclass);
20164 : : }
20165 : : }
20166 : 142536259 : }
20167 : :
20168 : : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
20169 : : of "return *this;" immediately before its location, using FNDECL's
20170 : : first statement (if any) to give the indentation, if appropriate. */
20171 : :
20172 : : static void
20173 : 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
20174 : : {
20175 : 18 : location_t indent = UNKNOWN_LOCATION;
20176 : 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
20177 : 18 : if (stmts)
20178 : 9 : indent = EXPR_LOCATION (stmts);
20179 : 18 : richloc->add_fixit_insert_formatted ("return *this;",
20180 : : richloc->get_loc (),
20181 : : indent);
20182 : 18 : }
20183 : :
20184 : : /* Finish up a function declaration and compile that function
20185 : : all the way to assembler language output. The free the storage
20186 : : for the function definition. INLINE_P is TRUE if we just
20187 : : finished processing the body of an in-class inline function
20188 : : definition. (This processing will have taken place after the
20189 : : class definition is complete.) */
20190 : :
20191 : : tree
20192 : 142532394 : finish_function (bool inline_p)
20193 : : {
20194 : 142532394 : tree fndecl = current_function_decl;
20195 : 142532394 : tree fntype, ctype = NULL_TREE;
20196 : 142532394 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
20197 : :
20198 : : /* When we get some parse errors, we can end up without a
20199 : : current_function_decl, so cope. */
20200 : 142532394 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
20201 : 0 : return error_mark_node;
20202 : :
20203 : 142532394 : bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
20204 : 142532394 : && !processing_template_decl);
20205 : :
20206 : 142532394 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20207 : 142531826 : finish_lambda_scope ();
20208 : :
20209 : 142532394 : if (c_dialect_objc ())
20210 : 0 : objc_finish_function ();
20211 : :
20212 : 142532394 : record_key_method_defined (fndecl);
20213 : :
20214 : 142532394 : fntype = TREE_TYPE (fndecl);
20215 : :
20216 : : /* TREE_READONLY (fndecl) = 1;
20217 : : This caused &foo to be of type ptr-to-const-function
20218 : : which then got a warning when stored in a ptr-to-function variable. */
20219 : :
20220 : 142532394 : gcc_assert (building_stmt_list_p ());
20221 : : /* The current function is being defined, so its DECL_INITIAL should
20222 : : be set, and unless there's a multiple definition, it should be
20223 : : error_mark_node. */
20224 : 142532394 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
20225 : :
20226 : 142532394 : cp_coroutine_transform *coroutine = nullptr;
20227 : 142532394 : if (flag_coroutines
20228 : 48010503 : && !processing_template_decl
20229 : 20918771 : && DECL_COROUTINE_P (fndecl)
20230 : 142534059 : && !DECL_RAMP_FN (fndecl))
20231 : : {
20232 : 1665 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
20233 : : && !DECL_DEFAULTED_FN (fndecl));
20234 : 1665 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
20235 : 1665 : if (coroutine && coroutine->cp_valid_coroutine ())
20236 : 1665 : coroutine->apply_transforms ();
20237 : :
20238 : : /* We should handle coroutine IFNs in middle end lowering. */
20239 : 1665 : cfun->coroutine_component = true;
20240 : :
20241 : : /* Do not try to process the ramp's EH unless outlining succeeded. */
20242 : 1665 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
20243 : 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20244 : : (TREE_TYPE (fndecl)),
20245 : 360 : current_eh_spec_block);
20246 : :
20247 : : /* If outlining succeeded, then add contracts handling if needed. */
20248 : 1665 : if (coroutine->cp_valid_coroutine () && do_contracts)
20249 : 6 : maybe_apply_function_contracts (fndecl);
20250 : : }
20251 : : else
20252 : : /* For a cloned function, we've already got all the code we need;
20253 : : there's no need to add any extra bits. */
20254 : 142530729 : if (!DECL_CLONED_FUNCTION_P (fndecl))
20255 : : {
20256 : : /* Make it so that `main' always returns 0 by default. */
20257 : 128627841 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
20258 : 128661045 : && !TREE_THIS_VOLATILE (current_function_decl))
20259 : 33204 : finish_return_stmt (integer_zero_node);
20260 : :
20261 : 128627841 : if (use_eh_spec_block (current_function_decl))
20262 : 18855255 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
20263 : : (TREE_TYPE (current_function_decl)),
20264 : 18855255 : current_eh_spec_block);
20265 : :
20266 : 128627841 : if (do_contracts)
20267 : 420 : maybe_apply_function_contracts (current_function_decl);
20268 : :
20269 : : }
20270 : :
20271 : : /* If we're saving up tree structure, tie off the function now. */
20272 : 142532394 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
20273 : :
20274 : 142532394 : finish_fname_decls ();
20275 : :
20276 : : /* This must come after expand_function_end because cleanups might
20277 : : have declarations (from inline functions) that need to go into
20278 : : this function's blocks. */
20279 : :
20280 : : /* If the current binding level isn't the outermost binding level
20281 : : for this function, either there is a bug, or we have experienced
20282 : : syntax errors and the statement tree is malformed. */
20283 : 142532394 : if (current_binding_level->kind != sk_function_parms)
20284 : : {
20285 : : /* Make sure we have already experienced errors. */
20286 : 12 : gcc_assert (errorcount);
20287 : :
20288 : : /* Throw away the broken statement tree and extra binding
20289 : : levels. */
20290 : 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
20291 : :
20292 : 27 : while (current_binding_level->kind != sk_function_parms)
20293 : : {
20294 : 15 : if (current_binding_level->kind == sk_class)
20295 : 0 : pop_nested_class ();
20296 : : else
20297 : 15 : poplevel (0, 0, 0);
20298 : : }
20299 : : }
20300 : 142532394 : poplevel (1, 0, 1);
20301 : :
20302 : : /* Statements should always be full-expressions at the outermost set
20303 : : of curly braces for a function. */
20304 : 142532394 : gcc_assert (stmts_are_full_exprs_p ());
20305 : :
20306 : : /* If there are no return statements in a function with auto return type,
20307 : : the return type is void. But if the declared type is something like
20308 : : auto*, this is an error. */
20309 : 56637296 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
20310 : 143369743 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20311 : : {
20312 : 229764 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20313 : 229749 : && !current_function_returns_value
20314 : 459322 : && !current_function_returns_null)
20315 : : {
20316 : : /* We haven't applied return type deduction because we haven't
20317 : : seen any return statements. Do that now. */
20318 : 229558 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20319 : 229558 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
20320 : : void_node, node, tf_warning_or_error,
20321 : : adc_return_type);
20322 : :
20323 : 229558 : apply_deduced_return_type (fndecl, void_type_node);
20324 : 229558 : fntype = TREE_TYPE (fndecl);
20325 : : }
20326 : 206 : else if (!current_function_returns_value
20327 : 6 : && !current_function_returns_null)
20328 : : {
20329 : 6 : auto_diagnostic_group d;
20330 : 12 : error ("no return statements in function returning %qT",
20331 : 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
20332 : 6 : inform (input_location, "only plain %<auto%> return type can be "
20333 : : "deduced to %<void%>");
20334 : 6 : }
20335 : : }
20336 : :
20337 : 142532394 : if (FNDECL_USED_AUTO (fndecl)
20338 : 142532394 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
20339 : 837143 : if (location_t fcloc = failed_completion_location (fndecl))
20340 : : {
20341 : 6 : auto_diagnostic_group adg;
20342 : 6 : if (warning (OPT_Wsfinae_incomplete_,
20343 : : "defining %qD, which previously failed to be deduced "
20344 : : "in a SFINAE context", fndecl)
20345 : 6 : && warn_sfinae_incomplete == 1)
20346 : 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
20347 : : "-Wsfinae-incomplete=2");
20348 : 6 : }
20349 : :
20350 : : /* Remember that we were in class scope. */
20351 : 142532394 : if (current_class_name)
20352 : 106103311 : ctype = current_class_type;
20353 : :
20354 : 142532394 : if (DECL_DELETED_FN (fndecl))
20355 : : {
20356 : 87 : DECL_INITIAL (fndecl) = error_mark_node;
20357 : 87 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
20358 : 87 : goto cleanup;
20359 : : }
20360 : :
20361 : 142532307 : if (flag_openmp)
20362 : 288868 : if (tree attr = lookup_attribute ("omp declare variant base",
20363 : 288868 : DECL_ATTRIBUTES (fndecl)))
20364 : 158 : omp_declare_variant_finalize (fndecl, attr);
20365 : :
20366 : : /* Complain if there's just no return statement. */
20367 : 142532307 : if ((warn_return_type
20368 : 41887 : || (cxx_dialect >= cxx14
20369 : 33896 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
20370 : 142501034 : && !VOID_TYPE_P (TREE_TYPE (fntype))
20371 : 87724454 : && !dependent_type_p (TREE_TYPE (fntype))
20372 : 45358231 : && !current_function_returns_value && !current_function_returns_null
20373 : : /* Don't complain if we abort or throw. */
20374 : 64531 : && !current_function_returns_abnormally
20375 : : /* Don't complain if there's an infinite loop. */
20376 : 922 : && !current_function_infinite_loop
20377 : : /* Don't complain if we are declared noreturn. */
20378 : 854 : && !TREE_THIS_VOLATILE (fndecl)
20379 : 848 : && !DECL_NAME (DECL_RESULT (fndecl))
20380 : 848 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
20381 : : /* Structor return values (if any) are set by the compiler. */
20382 : 1226 : && !DECL_CONSTRUCTOR_P (fndecl)
20383 : 613 : && !DECL_DESTRUCTOR_P (fndecl)
20384 : 142532920 : && targetm.warn_func_return (fndecl))
20385 : : {
20386 : 607 : gcc_rich_location richloc (input_location);
20387 : : /* Potentially add a "return *this;" fix-it hint for
20388 : : assignment operators. */
20389 : 607 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
20390 : : {
20391 : 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
20392 : 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
20393 : 32 : && current_class_ref
20394 : 26 : && same_type_ignoring_top_level_qualifiers_p
20395 : 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
20396 : 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
20397 : 18 : add_return_star_this_fixit (&richloc, fndecl);
20398 : : }
20399 : 607 : if (cxx_dialect >= cxx14
20400 : 607 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
20401 : 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
20402 : : "returning non-void");
20403 : 605 : else if (warning_at (&richloc, OPT_Wreturn_type,
20404 : : "no return statement in function returning "
20405 : : "non-void"))
20406 : 159 : suppress_warning (fndecl, OPT_Wreturn_type);
20407 : 607 : }
20408 : :
20409 : : /* Lambda closure members are implicitly constexpr if possible. */
20410 : 142532307 : if (cxx_dialect >= cxx17
20411 : 244490224 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
20412 : 1269118 : DECL_DECLARED_CONSTEXPR_P (fndecl)
20413 : 1269118 : = ((processing_template_decl
20414 : 546730 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
20415 : 2537211 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
20416 : :
20417 : : /* Save constexpr function body before it gets munged by
20418 : : the NRV transformation. */
20419 : 142532307 : maybe_save_constexpr_fundef (fndecl);
20420 : :
20421 : : /* Invoke the pre-genericize plugin before we start munging things. */
20422 : 142532307 : if (!processing_template_decl)
20423 : 56637209 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
20424 : :
20425 : : /* Perform delayed folding before NRV transformation. */
20426 : 142532307 : if (!processing_template_decl
20427 : 113274418 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20428 : 198659394 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20429 : 56126649 : cp_fold_function (fndecl);
20430 : :
20431 : : /* Set up the named return value optimization, if we can. Candidate
20432 : : variables are selected in check_return_expr. */
20433 : 142532307 : if (tree r = current_function_return_value)
20434 : : {
20435 : 35343465 : if (r != error_mark_node)
20436 : 180133 : finalize_nrv (fndecl, r);
20437 : 35343465 : current_function_return_value = NULL_TREE;
20438 : : }
20439 : :
20440 : : /* Must mark the RESULT_DECL as being in this function. */
20441 : 142532307 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
20442 : :
20443 : : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
20444 : : to the FUNCTION_DECL node itself. */
20445 : 142532307 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
20446 : :
20447 : : /* Store the end of the function, so that we get good line number
20448 : : info for the epilogue. */
20449 : 142532307 : cfun->function_end_locus = input_location;
20450 : :
20451 : : /* Complain about parameters that are only set, but never otherwise used. */
20452 : 142532307 : if (warn_unused_but_set_parameter
20453 : 926899 : && !processing_template_decl
20454 : 476735 : && errorcount == unused_but_set_errorcount
20455 : 143009039 : && !DECL_CLONED_FUNCTION_P (fndecl))
20456 : : {
20457 : 373536 : tree decl;
20458 : :
20459 : 373536 : for (decl = DECL_ARGUMENTS (fndecl);
20460 : 996750 : decl;
20461 : 623214 : decl = DECL_CHAIN (decl))
20462 : 623214 : if (TREE_USED (decl)
20463 : 516283 : && TREE_CODE (decl) == PARM_DECL
20464 : 516283 : && !DECL_READ_P (decl)
20465 : 225 : && DECL_NAME (decl)
20466 : 225 : && !DECL_ARTIFICIAL (decl)
20467 : 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
20468 : 225 : && !DECL_IN_SYSTEM_HEADER (decl)
20469 : 225 : && TREE_TYPE (decl) != error_mark_node
20470 : 225 : && !TYPE_REF_P (TREE_TYPE (decl))
20471 : 623439 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
20472 : 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
20473 : 225 : warning_at (DECL_SOURCE_LOCATION (decl),
20474 : 225 : OPT_Wunused_but_set_parameter_,
20475 : : "parameter %qD set but not used", decl);
20476 : 373536 : unused_but_set_errorcount = errorcount;
20477 : : }
20478 : :
20479 : : /* Complain about locally defined typedefs that are not used in this
20480 : : function. */
20481 : 142532307 : maybe_warn_unused_local_typedefs ();
20482 : :
20483 : : /* Possibly warn about unused parameters. */
20484 : 142532307 : if (warn_unused_parameter
20485 : 657583 : && !processing_template_decl
20486 : 142851533 : && !DECL_CLONED_FUNCTION_P (fndecl))
20487 : 245784 : do_warn_unused_parameter (fndecl);
20488 : :
20489 : : /* Genericize before inlining. */
20490 : 142532307 : if (!processing_template_decl
20491 : 113274418 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
20492 : 198659244 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
20493 : 56126499 : cp_genericize (fndecl);
20494 : :
20495 : : /* If this function can't throw any exceptions, remember that. */
20496 : 142532307 : if (!processing_template_decl
20497 : 56637209 : && !cp_function_chain->can_throw
20498 : 42696433 : && !flag_non_call_exceptions
20499 : 42691745 : && !decl_replaceable_p (fndecl,
20500 : 42691745 : opt_for_fn (fndecl, flag_semantic_interposition))
20501 : 184972458 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
20502 : 42437782 : TREE_NOTHROW (fndecl) = 1;
20503 : :
20504 : 142532394 : cleanup:
20505 : :
20506 : : /* We're leaving the context of this function, so zap cfun. It's still in
20507 : : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
20508 : 142532394 : set_cfun (NULL);
20509 : 142532394 : current_function_decl = NULL;
20510 : :
20511 : : /* If this is an in-class inline definition, we may have to pop the
20512 : : bindings for the template parameters that we added in
20513 : : maybe_begin_member_template_processing when start_function was
20514 : : called. */
20515 : 142532394 : if (inline_p)
20516 : 69968670 : maybe_end_member_template_processing ();
20517 : :
20518 : : /* Leave the scope of the class. */
20519 : 142532394 : if (ctype)
20520 : 106103311 : pop_nested_class ();
20521 : :
20522 : 142532394 : --function_depth;
20523 : :
20524 : : /* Clean up. */
20525 : 142532394 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
20526 : :
20527 : : /* Build outlined functions for coroutines and contracts. */
20528 : :
20529 : 142532394 : if (coroutine)
20530 : : {
20531 : : /* Emit the resumer and destroyer functions now, providing that we have
20532 : : not encountered some fatal error. */
20533 : 1665 : if (coroutine->cp_valid_coroutine ())
20534 : : {
20535 : 1581 : coroutine->finish_transforms ();
20536 : 1581 : resumer = coroutine->get_resumer ();
20537 : 1581 : destroyer = coroutine->get_destroyer ();
20538 : 1581 : expand_or_defer_fn (resumer);
20539 : 1581 : expand_or_defer_fn (destroyer);
20540 : : }
20541 : 1665 : delete coroutine;
20542 : : }
20543 : :
20544 : : /* If we have used outlined contracts checking functions, build and emit
20545 : : them here. */
20546 : 142532394 : finish_function_contracts (fndecl);
20547 : :
20548 : 142532394 : return fndecl;
20549 : : }
20550 : :
20551 : : /* Create the FUNCTION_DECL for a function definition.
20552 : : DECLSPECS and DECLARATOR are the parts of the declaration;
20553 : : they describe the return type and the name of the function,
20554 : : but twisted together in a fashion that parallels the syntax of C.
20555 : :
20556 : : This function creates a binding context for the function body
20557 : : as well as setting up the FUNCTION_DECL in current_function_decl.
20558 : :
20559 : : Returns a FUNCTION_DECL on success.
20560 : :
20561 : : If the DECLARATOR is not suitable for a function (it defines a datum
20562 : : instead), we return 0, which tells yyparse to report a parse error.
20563 : :
20564 : : May return void_type_node indicating that this method is actually
20565 : : a friend. See grokfield for more details.
20566 : :
20567 : : Came here with a `.pushlevel' .
20568 : :
20569 : : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
20570 : : CHANGES TO CODE IN `grokfield'. */
20571 : :
20572 : : tree
20573 : 70087688 : grokmethod (cp_decl_specifier_seq *declspecs,
20574 : : const cp_declarator *declarator, tree attrlist)
20575 : : {
20576 : 70087688 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
20577 : 70087688 : &attrlist);
20578 : :
20579 : 70087688 : if (fndecl == error_mark_node)
20580 : : return error_mark_node;
20581 : :
20582 : 70087585 : if (attrlist)
20583 : 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
20584 : :
20585 : : /* Pass friends other than inline friend functions back. */
20586 : 70087585 : if (fndecl == void_type_node)
20587 : : return fndecl;
20588 : :
20589 : 70087585 : if (DECL_IN_AGGR_P (fndecl))
20590 : : {
20591 : 0 : if (DECL_CLASS_SCOPE_P (fndecl))
20592 : 0 : error ("%qD is already defined in class %qT", fndecl,
20593 : 0 : DECL_CONTEXT (fndecl));
20594 : 0 : return error_mark_node;
20595 : : }
20596 : :
20597 : 70087585 : check_template_shadow (fndecl);
20598 : :
20599 : : /* p1779 ABI-Isolation makes inline not a default for in-class
20600 : : definitions attached to a named module. If the user explicitly
20601 : : made it inline, grokdeclarator will already have done the right
20602 : : things. */
20603 : 70087585 : if ((!named_module_attach_p ()
20604 : 585 : || flag_module_implicit_inline
20605 : : /* Lambda's operator function remains inline. */
20606 : 1152 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
20607 : : /* If the user explicitly asked for this to be inline, we don't
20608 : : need to do more, but more importantly we want to warn if we
20609 : : can't inline it. */
20610 : 70087239 : && !DECL_DECLARED_INLINE_P (fndecl))
20611 : : {
20612 : 50674152 : if (TREE_PUBLIC (fndecl))
20613 : 49179131 : DECL_COMDAT (fndecl) = 1;
20614 : 50674152 : DECL_DECLARED_INLINE_P (fndecl) = 1;
20615 : : /* It's ok if we can't inline this. */
20616 : 50674152 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
20617 : : }
20618 : :
20619 : : /* We process method specializations in finish_struct_1. */
20620 : 70087585 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
20621 : : {
20622 : : /* Avoid calling decl_spec_seq... until we have to. */
20623 : 57657399 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
20624 : 57657399 : fndecl = push_template_decl (fndecl, friendp);
20625 : 57657399 : if (fndecl == error_mark_node)
20626 : : return fndecl;
20627 : : }
20628 : :
20629 : 70087578 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
20630 : : {
20631 : 0 : fndecl = copy_node (fndecl);
20632 : 0 : TREE_CHAIN (fndecl) = NULL_TREE;
20633 : : }
20634 : :
20635 : 70087578 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
20636 : :
20637 : 70087578 : DECL_IN_AGGR_P (fndecl) = 1;
20638 : 70087578 : return fndecl;
20639 : : }
20640 : :
20641 : :
20642 : : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
20643 : : we can lay it out later, when and if its type becomes complete.
20644 : :
20645 : : Also handle constexpr variables where the initializer involves
20646 : : an unlowered PTRMEM_CST because the class isn't complete yet. */
20647 : :
20648 : : void
20649 : 88673334 : maybe_register_incomplete_var (tree var)
20650 : : {
20651 : 88673334 : gcc_assert (VAR_P (var));
20652 : :
20653 : : /* Keep track of variables with incomplete types. */
20654 : 39542493 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
20655 : 128214204 : && DECL_EXTERNAL (var))
20656 : : {
20657 : 5563175 : tree inner_type = TREE_TYPE (var);
20658 : :
20659 : 5720083 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
20660 : 156908 : inner_type = TREE_TYPE (inner_type);
20661 : 5563175 : inner_type = TYPE_MAIN_VARIANT (inner_type);
20662 : :
20663 : 5606536 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
20664 : : /* RTTI TD entries are created while defining the type_info. */
20665 : 5563175 : || (TYPE_LANG_SPECIFIC (inner_type)
20666 : 2248746 : && TYPE_BEING_DEFINED (inner_type)))
20667 : : {
20668 : 43361 : incomplete_var iv = {var, inner_type};
20669 : 43361 : vec_safe_push (incomplete_vars, iv);
20670 : : }
20671 : 10736236 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
20672 : 3423014 : && DECL_CLASS_SCOPE_P (var)
20673 : 1076619 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
20674 : 1076619 : && decl_constant_var_p (var)
20675 : 6220261 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
20676 : : {
20677 : : /* When the outermost open class is complete we can resolve any
20678 : : pointers-to-members. */
20679 : 13 : tree context = outermost_open_class ();
20680 : 13 : incomplete_var iv = {var, context};
20681 : 13 : vec_safe_push (incomplete_vars, iv);
20682 : : }
20683 : : }
20684 : 88673334 : }
20685 : :
20686 : : /* Called when a class type (given by TYPE) is defined. If there are
20687 : : any existing VAR_DECLs whose type has been completed by this
20688 : : declaration, update them now. */
20689 : :
20690 : : void
20691 : 56182071 : complete_vars (tree type)
20692 : : {
20693 : 56182071 : unsigned ix;
20694 : 56182071 : incomplete_var *iv;
20695 : :
20696 : 56608357 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
20697 : : {
20698 : 426286 : if (same_type_p (type, iv->incomplete_type))
20699 : : {
20700 : 42222 : tree var = iv->decl;
20701 : 42222 : tree type = TREE_TYPE (var);
20702 : :
20703 : 42222 : if (type != error_mark_node
20704 : 42222 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
20705 : 42219 : == iv->incomplete_type))
20706 : : {
20707 : : /* Complete the type of the variable. */
20708 : 42206 : complete_type (type);
20709 : 42206 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
20710 : 42206 : if (COMPLETE_TYPE_P (type))
20711 : 42191 : layout_var_decl (var);
20712 : : }
20713 : :
20714 : : /* Remove this entry from the list. */
20715 : 42222 : incomplete_vars->unordered_remove (ix);
20716 : : }
20717 : : else
20718 : 384064 : ix++;
20719 : : }
20720 : 56182071 : }
20721 : :
20722 : : /* If DECL is of a type which needs a cleanup, build and return an
20723 : : expression to perform that cleanup here. Return NULL_TREE if no
20724 : : cleanup need be done. DECL can also be a _REF when called from
20725 : : split_nonconstant_init_1. */
20726 : :
20727 : : tree
20728 : 65938212 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
20729 : : {
20730 : 65938212 : tree type;
20731 : 65938212 : tree attr;
20732 : 65938212 : tree cleanup;
20733 : :
20734 : : /* Assume no cleanup is required. */
20735 : 65938212 : cleanup = NULL_TREE;
20736 : :
20737 : 65938212 : if (!decl || error_operand_p (decl))
20738 : : return cleanup;
20739 : :
20740 : : /* Handle "__attribute__((cleanup))". We run the cleanup function
20741 : : before the destructor since the destructor is what actually
20742 : : terminates the lifetime of the object. */
20743 : 65938212 : if (DECL_P (decl))
20744 : 65567826 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
20745 : : else
20746 : : attr = NULL_TREE;
20747 : 65567826 : if (attr)
20748 : : {
20749 : 157 : tree id;
20750 : 157 : tree fn;
20751 : 157 : tree arg;
20752 : :
20753 : : /* Get the name specified by the user for the cleanup function. */
20754 : 157 : id = TREE_VALUE (TREE_VALUE (attr));
20755 : : /* Look up the name to find the cleanup function to call. It is
20756 : : important to use lookup_name here because that is what is
20757 : : used in c-common.cc:handle_cleanup_attribute when performing
20758 : : initial checks on the attribute. Note that those checks
20759 : : include ensuring that the function found is not an overloaded
20760 : : function, or an object with an overloaded call operator,
20761 : : etc.; we can rely on the fact that the function found is an
20762 : : ordinary FUNCTION_DECL. */
20763 : 157 : fn = lookup_name (id);
20764 : 157 : arg = build_address (decl);
20765 : 157 : if (!mark_used (decl, complain) && !(complain & tf_error))
20766 : 0 : return error_mark_node;
20767 : 157 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
20768 : 157 : if (cleanup == error_mark_node)
20769 : : return error_mark_node;
20770 : : }
20771 : : /* Handle ordinary C++ destructors. */
20772 : 65938200 : type = TREE_TYPE (decl);
20773 : 65938200 : if (type_build_dtor_call (type))
20774 : : {
20775 : 5260980 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
20776 : 5260980 : tree addr;
20777 : 5260980 : tree call;
20778 : :
20779 : 5260980 : if (TREE_CODE (type) == ARRAY_TYPE)
20780 : : addr = decl;
20781 : : else
20782 : 5259230 : addr = build_address (decl);
20783 : :
20784 : 5260980 : call = build_delete (input_location, TREE_TYPE (addr), addr,
20785 : : sfk_complete_destructor, flags, 0, complain);
20786 : 5260980 : if (call == error_mark_node)
20787 : : cleanup = error_mark_node;
20788 : 5260941 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
20789 : : /* Discard the call. */;
20790 : 3899174 : else if (decl_maybe_constant_destruction (decl, type)
20791 : 3899321 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
20792 : 147 : cxx_constant_dtor (call, decl);
20793 : 3899027 : else if (cleanup)
20794 : 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
20795 : : else
20796 : : cleanup = call;
20797 : : }
20798 : :
20799 : : /* build_delete sets the location of the destructor call to the
20800 : : current location, even though the destructor is going to be
20801 : : called later, at the end of the current scope. This can lead to
20802 : : a "jumpy" behavior for users of debuggers when they step around
20803 : : the end of the block. So let's unset the location of the
20804 : : destructor call instead. */
20805 : 65938200 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
20806 : 65938200 : if (cleanup && CONVERT_EXPR_P (cleanup))
20807 : 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
20808 : :
20809 : 0 : if (cleanup
20810 : 3899208 : && DECL_P (decl)
20811 : 3898737 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
20812 : : /* Treat objects with destructors as used; the destructor may do
20813 : : something substantive. */
20814 : 3898731 : && !mark_used (decl, complain) && !(complain & tf_error))
20815 : 0 : return error_mark_node;
20816 : :
20817 : 3899208 : if (cleanup && cfun && !processing_template_decl
20818 : 69399610 : && !expr_noexcept_p (cleanup, tf_none))
20819 : 5779 : cp_function_chain->throwing_cleanup = true;
20820 : :
20821 : : return cleanup;
20822 : : }
20823 : :
20824 : :
20825 : : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
20826 : : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
20827 : : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
20828 : :
20829 : : tree
20830 : 2744042 : static_fn_type (tree memfntype)
20831 : : {
20832 : 2744042 : tree fntype;
20833 : 2744042 : tree args;
20834 : :
20835 : 2744042 : if (TYPE_PTRMEMFUNC_P (memfntype))
20836 : 21429 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
20837 : 2744042 : if (INDIRECT_TYPE_P (memfntype)
20838 : 2412894 : || TREE_CODE (memfntype) == FUNCTION_DECL)
20839 : 2566038 : memfntype = TREE_TYPE (memfntype);
20840 : 2744042 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
20841 : : return memfntype;
20842 : 559772 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
20843 : 559772 : args = TYPE_ARG_TYPES (memfntype);
20844 : 559772 : fntype = cp_build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
20845 : 559772 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
20846 : 559772 : fntype = (cp_build_type_attribute_variant
20847 : 559772 : (fntype, TYPE_ATTRIBUTES (memfntype)));
20848 : 559772 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
20849 : 559772 : return fntype;
20850 : : }
20851 : :
20852 : : /* DECL was originally constructed as a non-static member function,
20853 : : but turned out to be static. Update it accordingly. */
20854 : :
20855 : : void
20856 : 163501 : revert_static_member_fn (tree decl)
20857 : : {
20858 : 163501 : tree stype = static_fn_type (decl);
20859 : 163501 : cp_cv_quals quals = type_memfn_quals (stype);
20860 : 163501 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
20861 : :
20862 : 163501 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
20863 : 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
20864 : :
20865 : 163501 : TREE_TYPE (decl) = stype;
20866 : :
20867 : 163501 : if (DECL_ARGUMENTS (decl))
20868 : 163501 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
20869 : 163501 : DECL_STATIC_FUNCTION_P (decl) = 1;
20870 : 163501 : }
20871 : :
20872 : : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
20873 : : one of the language-independent trees. */
20874 : :
20875 : : enum cp_tree_node_structure_enum
20876 : 32325961895 : cp_tree_node_structure (union lang_tree_node * t)
20877 : : {
20878 : 32325961895 : switch (TREE_CODE (&t->generic))
20879 : : {
20880 : : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
20881 : 141300233 : case BASELINK: return TS_CP_BASELINK;
20882 : 34589968 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
20883 : 10888403 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
20884 : 3300542 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
20885 : 1939731099 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
20886 : 2298793 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
20887 : 171131 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
20888 : 616039661 : case OVERLOAD: return TS_CP_OVERLOAD;
20889 : 783 : case PTRMEM_CST: return TS_CP_PTRMEM;
20890 : 14063140 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
20891 : 595683656 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
20892 : 1377826634 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
20893 : 589569565 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
20894 : 14298042 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
20895 : 6 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
20896 : 2463866 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
20897 : 26983736373 : default: return TS_CP_GENERIC;
20898 : : }
20899 : : }
20900 : :
20901 : : bool
20902 : 10002 : cp_missing_noreturn_ok_p (tree decl)
20903 : : {
20904 : : /* A missing noreturn is ok for the `main' function. */
20905 : 10002 : return DECL_MAIN_P (decl);
20906 : : }
20907 : :
20908 : : /* Return the decl used to identify the COMDAT group into which DECL should
20909 : : be placed. */
20910 : :
20911 : : tree
20912 : 85601969 : cxx_comdat_group (tree decl)
20913 : : {
20914 : : /* Virtual tables, construction virtual tables, and virtual table
20915 : : tables all go in a single COMDAT group, named after the primary
20916 : : virtual table. */
20917 : 85601969 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
20918 : 451216 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
20919 : : /* For all other DECLs, the COMDAT group is the mangled name of the
20920 : : declaration itself. */
20921 : : else
20922 : : {
20923 : 85150753 : while (DECL_THUNK_P (decl))
20924 : : {
20925 : : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
20926 : : into the same section as the target function. In that case
20927 : : we must return target's name. */
20928 : 0 : tree target = THUNK_TARGET (decl);
20929 : 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
20930 : 0 : && DECL_SECTION_NAME (target) != NULL
20931 : 0 : && DECL_ONE_ONLY (target))
20932 : : decl = target;
20933 : : else
20934 : : break;
20935 : : }
20936 : : /* If a ctor/dtor has already set the comdat group by
20937 : : maybe_clone_body, don't override it. */
20938 : 85150753 : if (SUPPORTS_ONE_ONLY
20939 : 85150753 : && TREE_CODE (decl) == FUNCTION_DECL
20940 : 85150753 : && DECL_CLONED_FUNCTION_P (decl))
20941 : 15238659 : if (tree comdat = DECL_COMDAT_GROUP (decl))
20942 : : return comdat;
20943 : : }
20944 : :
20945 : : return decl;
20946 : : }
20947 : :
20948 : : /* Returns the return type for FN as written by the user, which may include
20949 : : a placeholder for a deduced return type. */
20950 : :
20951 : : tree
20952 : 485783947 : fndecl_declared_return_type (tree fn)
20953 : : {
20954 : 485783947 : fn = STRIP_TEMPLATE (fn);
20955 : 485783947 : if (FNDECL_USED_AUTO (fn))
20956 : 4739 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
20957 : :
20958 : 485779208 : return TREE_TYPE (TREE_TYPE (fn));
20959 : : }
20960 : :
20961 : : /* Returns true iff DECL is a variable or function declared with an auto type
20962 : : that has not yet been deduced to a real type. */
20963 : :
20964 : : bool
20965 : 772680661 : undeduced_auto_decl (tree decl)
20966 : : {
20967 : 772680661 : if (cxx_dialect < cxx11)
20968 : : return false;
20969 : 770664420 : STRIP_ANY_LOCATION_WRAPPER (decl);
20970 : 328236769 : return ((VAR_OR_FUNCTION_DECL_P (decl)
20971 : 75305096 : || TREE_CODE (decl) == TEMPLATE_DECL)
20972 : 1023728897 : && type_uses_auto (TREE_TYPE (decl)));
20973 : : }
20974 : :
20975 : : /* Complain if DECL has an undeduced return type. */
20976 : :
20977 : : bool
20978 : 247235054 : require_deduced_type (tree decl, tsubst_flags_t complain)
20979 : : {
20980 : 247235054 : if (undeduced_auto_decl (decl))
20981 : : {
20982 : 3045 : if (TREE_CODE (decl) == FUNCTION_DECL
20983 : 3018 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
20984 : 5475 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
20985 : : {
20986 : : /* Set the return type of __builtin_source_location. */
20987 : 2430 : tree type = get_source_location_impl_type ();
20988 : 2430 : if (type == error_mark_node)
20989 : : {
20990 : 33 : inform (input_location, "using %qs", "__builtin_source_location");
20991 : 33 : return false;
20992 : : }
20993 : 2397 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
20994 : 2397 : type = build_pointer_type (type);
20995 : 2397 : apply_deduced_return_type (decl, type);
20996 : 2397 : return true;
20997 : : }
20998 : :
20999 : 615 : if (warning_suppressed_p (decl) && seen_error ())
21000 : : /* We probably already complained about deduction failure. */;
21001 : 477 : else if (complain & tf_error)
21002 : 72 : error ("use of %qD before deduction of %<auto%>", decl);
21003 : 615 : note_failed_type_completion (decl, complain);
21004 : 615 : return false;
21005 : : }
21006 : : return true;
21007 : : }
21008 : :
21009 : : /* Create a representation of the explicit-specifier with
21010 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
21011 : :
21012 : : tree
21013 : 6258146 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
21014 : : {
21015 : 6258146 : if (check_for_bare_parameter_packs (expr))
21016 : 3 : return error_mark_node;
21017 : :
21018 : 6258143 : if (instantiation_dependent_expression_p (expr))
21019 : : /* Wait for instantiation, tsubst_function_decl will handle it. */
21020 : : return expr;
21021 : :
21022 : 5619032 : expr = build_converted_constant_bool_expr (expr, complain);
21023 : 5619032 : expr = instantiate_non_dependent_expr (expr, complain);
21024 : 5619032 : expr = cxx_constant_value (expr, complain);
21025 : 5619032 : return expr;
21026 : : }
21027 : :
21028 : : #include "gt-cp-decl.h"
|