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 "gcc-urlifier.h"
64 : : #include "diagnostic-highlight-colors.h"
65 : : #include "pretty-print-markup.h"
66 : :
67 : : /* Possible cases of bad specifiers type used by bad_specifiers. */
68 : : enum bad_spec_place {
69 : : BSP_VAR, /* variable */
70 : : BSP_PARM, /* parameter */
71 : : BSP_TYPE, /* type */
72 : : BSP_FIELD /* field */
73 : : };
74 : :
75 : : static const char *redeclaration_error_message (tree, tree);
76 : :
77 : : static bool decl_jump_unsafe (tree);
78 : : static void require_complete_types_for_parms (tree);
79 : : static tree grok_reference_init (tree, tree, tree, int);
80 : : static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
81 : : int, int, int, bool, int, tree, location_t);
82 : : static void check_static_variable_definition (tree, tree);
83 : : static void record_unknown_type (tree, const char *);
84 : : static int member_function_or_else (tree, tree, enum overload_flags);
85 : : static tree local_variable_p_walkfn (tree *, int *, void *);
86 : : static const char *tag_name (enum tag_types);
87 : : static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
88 : : static void maybe_deduce_size_from_array_init (tree, tree);
89 : : static void layout_var_decl (tree);
90 : : static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
91 : : static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
92 : : static void copy_type_enum (tree , tree);
93 : : static void check_function_type (tree, tree);
94 : : static void begin_destructor_body (void);
95 : : static void record_key_method_defined (tree);
96 : : static tree create_array_type_for_decl (tree, tree, tree, location_t);
97 : : static tree get_atexit_node (void);
98 : : static tree get_dso_handle_node (void);
99 : : static tree start_cleanup_fn (tree, bool, bool);
100 : : static void end_cleanup_fn (void);
101 : : static tree cp_make_fname_decl (location_t, tree, int);
102 : : static void initialize_predefined_identifiers (void);
103 : : static tree check_special_function_return_type
104 : : (special_function_kind, tree, tree, int, const cp_declarator**,
105 : : const location_t*);
106 : : static tree push_cp_library_fn (enum tree_code, tree, int);
107 : : static tree build_cp_library_fn (tree, enum tree_code, tree, int);
108 : : static void store_parm_decls (tree);
109 : : static void initialize_local_var (tree, tree, bool);
110 : : static void expand_static_init (tree, tree);
111 : : static location_t smallest_type_location (const cp_decl_specifier_seq*);
112 : : static bool identify_goto (tree, location_t, const location_t *,
113 : : diagnostic_t, bool);
114 : :
115 : : /* The following symbols are subsumed in the cp_global_trees array, and
116 : : listed here individually for documentation purposes.
117 : :
118 : : C++ extensions
119 : : tree wchar_decl_node;
120 : :
121 : : tree vtable_entry_type;
122 : : tree delta_type_node;
123 : : tree __t_desc_type_node;
124 : :
125 : : tree class_type_node;
126 : : tree unknown_type_node;
127 : :
128 : : Array type `vtable_entry_type[]'
129 : :
130 : : tree vtbl_type_node;
131 : : tree vtbl_ptr_type_node;
132 : :
133 : : Namespaces,
134 : :
135 : : tree std_node;
136 : : tree abi_node;
137 : :
138 : : A FUNCTION_DECL which can call `abort'. Not necessarily the
139 : : one that the user will declare, but sufficient to be called
140 : : by routines that want to abort the program.
141 : :
142 : : tree abort_fndecl;
143 : :
144 : : Used by RTTI
145 : : tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
146 : : tree tinfo_var_id; */
147 : :
148 : : tree cp_global_trees[CPTI_MAX];
149 : :
150 : : /* A list of objects which have constructors or destructors
151 : : which reside in namespace scope. The decl is stored in
152 : : the TREE_VALUE slot and the initializer is stored
153 : : in the TREE_PURPOSE slot. */
154 : : tree static_aggregates;
155 : :
156 : : /* Like static_aggregates, but for thread_local variables. */
157 : : tree tls_aggregates;
158 : :
159 : : /* A hash-map mapping from variable decls to the dynamic initializer for
160 : : the decl. This is currently only used by OpenMP. */
161 : : decl_tree_map *dynamic_initializers;
162 : :
163 : : /* -- end of C++ */
164 : :
165 : : /* A node for the integer constant 2. */
166 : :
167 : : tree integer_two_node;
168 : :
169 : : /* vector of static decls. */
170 : : vec<tree, va_gc> *static_decls;
171 : :
172 : : /* vector of keyed classes. */
173 : : vec<tree, va_gc> *keyed_classes;
174 : :
175 : : /* Used only for jumps to as-yet undefined labels, since jumps to
176 : : defined labels can have their validity checked immediately. */
177 : :
178 : : struct GTY((chain_next ("%h.next"))) named_label_use_entry {
179 : : struct named_label_use_entry *next;
180 : : /* The binding level to which this entry is *currently* attached.
181 : : This is initially the binding level in which the goto appeared,
182 : : but is modified as scopes are closed. */
183 : : cp_binding_level *binding_level;
184 : : /* The head of the names list that was current when the goto appeared,
185 : : or the inner scope popped. These are the decls that will *not* be
186 : : skipped when jumping to the label. */
187 : : tree names_in_scope;
188 : : /* If the use is a possible destination of a computed goto, a vec of decls
189 : : that aren't destroyed, filled in by poplevel_named_label_1. */
190 : : vec<tree,va_gc> *computed_goto;
191 : : /* The location of the goto, for error reporting. */
192 : : location_t o_goto_locus;
193 : : /* True if an OpenMP structured block scope has been closed since
194 : : the goto appeared. This means that the branch from the label will
195 : : illegally exit an OpenMP scope. */
196 : : bool in_omp_scope;
197 : : };
198 : :
199 : : /* A list of all LABEL_DECLs in the function that have names. Here so
200 : : we can clear out their names' definitions at the end of the
201 : : function, and so we can check the validity of jumps to these labels. */
202 : :
203 : : struct GTY((for_user)) named_label_entry {
204 : :
205 : : tree name; /* Name of decl. */
206 : :
207 : : tree label_decl; /* LABEL_DECL, unless deleted local label. */
208 : :
209 : : named_label_entry *outer; /* Outer shadowed chain. */
210 : :
211 : : /* The binding level to which the label is *currently* attached.
212 : : This is initially set to the binding level in which the label
213 : : is defined, but is modified as scopes are closed. */
214 : : cp_binding_level *binding_level;
215 : :
216 : : /* The head of the names list that was current when the label was
217 : : defined, or the inner scope popped. These are the decls that will
218 : : be skipped when jumping to the label. */
219 : : tree names_in_scope;
220 : :
221 : : /* A vector of all decls from all binding levels that would be
222 : : crossed by a backward branch to the label. */
223 : : vec<tree, va_gc> *bad_decls;
224 : :
225 : : /* A list of uses of the label, before the label is defined. */
226 : : named_label_use_entry *uses;
227 : :
228 : : /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for
229 : : this, it has a more specific meaning for LABEL_DECL. */
230 : : bool addressed;
231 : :
232 : : /* The following bits are set after the label is defined, and are
233 : : updated as scopes are popped. They indicate that a jump to the
234 : : label will illegally enter a scope of the given flavor. */
235 : : bool in_try_scope;
236 : : bool in_catch_scope;
237 : : bool in_omp_scope;
238 : : bool in_transaction_scope;
239 : : bool in_constexpr_if;
240 : : bool in_consteval_if;
241 : : bool in_stmt_expr;
242 : : };
243 : :
244 : : #define named_labels cp_function_chain->x_named_labels
245 : :
246 : : /* The number of function bodies which we are currently processing.
247 : : (Zero if we are at namespace scope, one inside the body of a
248 : : function, two inside the body of a function in a local class, etc.) */
249 : : int function_depth;
250 : :
251 : : /* Whether the exception-specifier is part of a function type (i.e. C++17). */
252 : : bool flag_noexcept_type;
253 : :
254 : : /* States indicating how grokdeclarator() should handle declspecs marked
255 : : with __attribute__((deprecated)). An object declared as
256 : : __attribute__((deprecated)) suppresses warnings of uses of other
257 : : deprecated items. */
258 : : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
259 : :
260 : :
261 : : /* A list of VAR_DECLs whose type was incomplete at the time the
262 : : variable was declared. */
263 : :
264 : : struct GTY(()) incomplete_var {
265 : : tree decl;
266 : : tree incomplete_type;
267 : : };
268 : :
269 : :
270 : : static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
271 : :
272 : : /* Returns the kind of template specialization we are currently
273 : : processing, given that it's declaration contained N_CLASS_SCOPES
274 : : explicit scope qualifications. */
275 : :
276 : : tmpl_spec_kind
277 : 219412770 : current_tmpl_spec_kind (int n_class_scopes)
278 : : {
279 : 219412770 : int n_template_parm_scopes = 0;
280 : 219412770 : int seen_specialization_p = 0;
281 : 219412770 : int innermost_specialization_p = 0;
282 : 219412770 : cp_binding_level *b;
283 : :
284 : : /* Scan through the template parameter scopes. */
285 : 219412770 : for (b = current_binding_level;
286 : 275320548 : b->kind == sk_template_parms;
287 : 55907778 : b = b->level_chain)
288 : : {
289 : : /* If we see a specialization scope inside a parameter scope,
290 : : then something is wrong. That corresponds to a declaration
291 : : like:
292 : :
293 : : template <class T> template <> ...
294 : :
295 : : which is always invalid since [temp.expl.spec] forbids the
296 : : specialization of a class member template if the enclosing
297 : : class templates are not explicitly specialized as well. */
298 : 55907778 : if (b->explicit_spec_p)
299 : : {
300 : 928981 : if (n_template_parm_scopes == 0)
301 : : innermost_specialization_p = 1;
302 : : else
303 : 1085 : seen_specialization_p = 1;
304 : : }
305 : 54978797 : else if (seen_specialization_p == 1)
306 : : return tsk_invalid_member_spec;
307 : :
308 : 55907778 : ++n_template_parm_scopes;
309 : : }
310 : :
311 : : /* Handle explicit instantiations. */
312 : 219412770 : if (processing_explicit_instantiation)
313 : : {
314 : 1991001 : if (n_template_parm_scopes != 0)
315 : : /* We've seen a template parameter list during an explicit
316 : : instantiation. For example:
317 : :
318 : : template <class T> template void f(int);
319 : :
320 : : This is erroneous. */
321 : : return tsk_invalid_expl_inst;
322 : : else
323 : 1991001 : return tsk_expl_inst;
324 : : }
325 : :
326 : 217421769 : if (n_template_parm_scopes < n_class_scopes)
327 : : /* We've not seen enough template headers to match all the
328 : : specialized classes present. For example:
329 : :
330 : : template <class T> void R<T>::S<T>::f(int);
331 : :
332 : : This is invalid; there needs to be one set of template
333 : : parameters for each class. */
334 : : return tsk_insufficient_parms;
335 : 217421769 : else if (n_template_parm_scopes == n_class_scopes)
336 : : /* We're processing a non-template declaration (even though it may
337 : : be a member of a template class.) For example:
338 : :
339 : : template <class T> void S<T>::f(int);
340 : :
341 : : The `class T' matches the `S<T>', leaving no template headers
342 : : corresponding to the `f'. */
343 : : return tsk_none;
344 : 47755122 : else if (n_template_parm_scopes > n_class_scopes + 1)
345 : : /* We've got too many template headers. For example:
346 : :
347 : : template <> template <class T> void f (T);
348 : :
349 : : There need to be more enclosing classes. */
350 : : return tsk_excessive_parms;
351 : : else
352 : : /* This must be a template. It's of the form:
353 : :
354 : : template <class T> template <class U> void S<T>::f(U);
355 : :
356 : : This is a specialization if the innermost level was a
357 : : specialization; otherwise it's just a definition of the
358 : : template. */
359 : 95077586 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
360 : : }
361 : :
362 : : /* Exit the current scope. */
363 : :
364 : : void
365 : 85140410 : finish_scope (void)
366 : : {
367 : 85140410 : poplevel (0, 0, 0);
368 : 85140410 : }
369 : :
370 : : /* When a label goes out of scope, check to see if that label was used
371 : : in a valid manner, and issue any appropriate warnings or errors. */
372 : :
373 : : static void
374 : 19726 : check_label_used (tree label)
375 : : {
376 : 19726 : if (!processing_template_decl)
377 : : {
378 : 19453 : if (DECL_INITIAL (label) == NULL_TREE)
379 : : {
380 : 9 : location_t location;
381 : :
382 : 9 : error ("label %q+D used but not defined", label);
383 : 9 : location = input_location;
384 : : /* FIXME want (LOCATION_FILE (input_location), (line)0) */
385 : : /* Avoid crashing later. */
386 : 9 : define_label (location, DECL_NAME (label));
387 : : }
388 : : else
389 : 19444 : warn_for_unused_label (label);
390 : : }
391 : 19726 : }
392 : :
393 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
394 : :
395 : : static int
396 : 217776 : sort_labels (const void *a, const void *b)
397 : : {
398 : 217776 : tree label1 = *(tree const *) a;
399 : 217776 : tree label2 = *(tree const *) b;
400 : :
401 : : /* DECL_UIDs can never be equal. */
402 : 217776 : return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
403 : : }
404 : :
405 : : /* At the end of a function, all labels declared within the function
406 : : go out of scope. BLOCK is the top-level block for the
407 : : function. */
408 : :
409 : : static void
410 : 139343303 : pop_labels (tree block)
411 : : {
412 : 139343303 : if (!named_labels)
413 : 139338809 : return;
414 : :
415 : : /* We need to add the labels to the block chain, so debug
416 : : information is emitted. But, we want the order to be stable so
417 : : need to sort them first. Otherwise the debug output could be
418 : : randomly ordered. I guess it's mostly stable, unless the hash
419 : : table implementation changes. */
420 : 4494 : auto_vec<tree, 32> labels (named_labels->elements ());
421 : 4494 : hash_table<named_label_hash>::iterator end (named_labels->end ());
422 : 24199 : for (hash_table<named_label_hash>::iterator iter
423 : 48398 : (named_labels->begin ()); iter != end; ++iter)
424 : : {
425 : 19705 : named_label_entry *ent = *iter;
426 : :
427 : 19705 : gcc_checking_assert (!ent->outer);
428 : 19705 : if (ent->label_decl)
429 : 19519 : labels.quick_push (ent->label_decl);
430 : 19705 : ggc_free (ent);
431 : : }
432 : 4494 : named_labels = NULL;
433 : 4494 : labels.qsort (sort_labels);
434 : :
435 : 24013 : while (labels.length ())
436 : : {
437 : 19519 : tree label = labels.pop ();
438 : :
439 : 19519 : DECL_CHAIN (label) = BLOCK_VARS (block);
440 : 19519 : BLOCK_VARS (block) = label;
441 : :
442 : 19519 : check_label_used (label);
443 : : }
444 : 4494 : }
445 : :
446 : : /* At the end of a block with local labels, restore the outer definition. */
447 : :
448 : : static void
449 : 207 : pop_local_label (tree id, tree label)
450 : : {
451 : 207 : check_label_used (label);
452 : 207 : named_label_entry **slot = named_labels->find_slot_with_hash
453 : 207 : (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
454 : 207 : named_label_entry *ent = *slot;
455 : :
456 : 207 : if (ent->outer)
457 : : ent = ent->outer;
458 : : else
459 : : {
460 : 198 : ent = ggc_cleared_alloc<named_label_entry> ();
461 : 198 : ent->name = id;
462 : : }
463 : 207 : *slot = ent;
464 : 207 : }
465 : :
466 : : /* The following two routines are used to interface to Objective-C++.
467 : : The binding level is purposely treated as an opaque type. */
468 : :
469 : : void *
470 : 0 : objc_get_current_scope (void)
471 : : {
472 : 0 : return current_binding_level;
473 : : }
474 : :
475 : : /* The following routine is used by the NeXT-style SJLJ exceptions;
476 : : variables get marked 'volatile' so as to not be clobbered by
477 : : _setjmp()/_longjmp() calls. All variables in the current scope,
478 : : as well as parent scopes up to (but not including) ENCLOSING_BLK
479 : : shall be thusly marked. */
480 : :
481 : : void
482 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
483 : : {
484 : 0 : cp_binding_level *scope;
485 : :
486 : 0 : for (scope = current_binding_level;
487 : 0 : scope && scope != enclosing_blk;
488 : 0 : scope = scope->level_chain)
489 : : {
490 : 0 : tree decl;
491 : :
492 : 0 : for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
493 : 0 : objc_volatilize_decl (decl);
494 : :
495 : : /* Do not climb up past the current function. */
496 : 0 : if (scope->kind == sk_function_parms)
497 : : break;
498 : : }
499 : 0 : }
500 : :
501 : : /* True if B is the level for the condition of a constexpr if. */
502 : :
503 : : static bool
504 : 1938551 : level_for_constexpr_if (cp_binding_level *b)
505 : : {
506 : 1935646 : return (b->kind == sk_cond && b->this_entity
507 : 262 : && TREE_CODE (b->this_entity) == IF_STMT
508 : 1938813 : && IF_STMT_CONSTEXPR_P (b->this_entity));
509 : : }
510 : :
511 : : /* True if B is the level for the condition of a consteval if. */
512 : :
513 : : static bool
514 : 1938527 : level_for_consteval_if (cp_binding_level *b)
515 : : {
516 : 1935622 : return (b->kind == sk_cond && b->this_entity
517 : 238 : && TREE_CODE (b->this_entity) == IF_STMT
518 : 1938765 : && IF_STMT_CONSTEVAL_P (b->this_entity));
519 : : }
520 : :
521 : : /* True if T is a non-static VAR_DECL that has a non-trivial destructor.
522 : : See [stmt.dcl]/2. */
523 : :
524 : : static bool
525 : 25 : automatic_var_with_nontrivial_dtor_p (const_tree t)
526 : : {
527 : 25 : if (error_operand_p (t))
528 : : return false;
529 : :
530 : 19 : return (VAR_P (t)
531 : 19 : && decl_storage_duration (CONST_CAST_TREE (t)) == dk_auto
532 : 38 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)));
533 : : }
534 : :
535 : : /* Update data for defined and undefined labels when leaving a scope. */
536 : :
537 : : int
538 : 144735 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
539 : : {
540 : 144735 : named_label_entry *ent = *slot;
541 : 144735 : cp_binding_level *obl = bl->level_chain;
542 : :
543 : 144735 : if (ent->binding_level == bl)
544 : : {
545 : 8662 : tree decl;
546 : :
547 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
548 : : TREE_LISTs representing OVERLOADs, so be careful. */
549 : 12401 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
550 : 3812 : ? DECL_CHAIN (decl)
551 : 73 : : TREE_CHAIN (decl)))
552 : 3739 : if (decl_jump_unsafe (decl))
553 : 1454 : vec_safe_push (ent->bad_decls, decl);
554 : :
555 : 8662 : ent->binding_level = obl;
556 : 8662 : ent->names_in_scope = obl->names;
557 : 8662 : switch (bl->kind)
558 : : {
559 : 42 : case sk_try:
560 : 42 : ent->in_try_scope = true;
561 : 42 : break;
562 : 27 : case sk_catch:
563 : 27 : ent->in_catch_scope = true;
564 : 27 : break;
565 : 348 : case sk_omp:
566 : 348 : ent->in_omp_scope = true;
567 : 348 : break;
568 : 3 : case sk_transaction:
569 : 3 : ent->in_transaction_scope = true;
570 : 3 : break;
571 : 45 : case sk_stmt_expr:
572 : 45 : ent->in_stmt_expr = true;
573 : 45 : break;
574 : 3098 : case sk_block:
575 : 3098 : if (level_for_constexpr_if (bl->level_chain))
576 : 15 : ent->in_constexpr_if = true;
577 : 3083 : else if (level_for_consteval_if (bl->level_chain))
578 : 30 : ent->in_consteval_if = true;
579 : : break;
580 : : default:
581 : : break;
582 : : }
583 : : }
584 : 136073 : else if (ent->uses)
585 : : {
586 : : struct named_label_use_entry *use;
587 : :
588 : 13229 : for (use = ent->uses; use ; use = use->next)
589 : 8830 : if (use->binding_level == bl)
590 : : {
591 : 2944 : if (auto &cg = use->computed_goto)
592 : : {
593 : 57 : if (bl->kind == sk_catch)
594 : 3 : vec_safe_push (cg, get_identifier ("catch"));
595 : 66 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
596 : 9 : if (automatic_var_with_nontrivial_dtor_p (d))
597 : 6 : vec_safe_push (cg, d);
598 : : }
599 : :
600 : 2944 : use->binding_level = obl;
601 : 2944 : use->names_in_scope = obl->names;
602 : 2944 : if (bl->kind == sk_omp)
603 : 12 : use->in_omp_scope = true;
604 : : }
605 : : }
606 : :
607 : 144735 : return 1;
608 : : }
609 : :
610 : : /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
611 : : when errors were reported, except for -Werror-unused-but-set-*. */
612 : : static int unused_but_set_errorcount;
613 : :
614 : : /* Exit a binding level.
615 : : Pop the level off, and restore the state of the identifier-decl mappings
616 : : that were in effect when this level was entered.
617 : :
618 : : If KEEP == 1, this level had explicit declarations, so
619 : : and create a "block" (a BLOCK node) for the level
620 : : to record its declarations and subblocks for symbol table output.
621 : :
622 : : If FUNCTIONBODY is nonzero, this level is the body of a function,
623 : : so create a block as if KEEP were set and also clear out all
624 : : label names.
625 : :
626 : : If REVERSE is nonzero, reverse the order of decls before putting
627 : : them into the BLOCK. */
628 : :
629 : : tree
630 : 561667936 : poplevel (int keep, int reverse, int functionbody)
631 : : {
632 : 561667936 : tree link;
633 : : /* The chain of decls was accumulated in reverse order.
634 : : Put it into forward order, just for cleanliness. */
635 : 561667936 : tree decls;
636 : 561667936 : tree subblocks;
637 : 561667936 : tree block;
638 : 561667936 : tree decl;
639 : 561667936 : scope_kind kind;
640 : :
641 : 561667936 : auto_cond_timevar tv (TV_NAME_LOOKUP);
642 : 561668200 : restart:
643 : :
644 : 561668200 : block = NULL_TREE;
645 : :
646 : 561668200 : gcc_assert (current_binding_level->kind != sk_class
647 : : && current_binding_level->kind != sk_namespace);
648 : :
649 : 561668200 : if (current_binding_level->kind == sk_cleanup)
650 : : functionbody = 0;
651 : 561668200 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
652 : :
653 : 561668200 : gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
654 : :
655 : : /* We used to use KEEP == 2 to indicate that the new block should go
656 : : at the beginning of the list of blocks at this binding level,
657 : : rather than the end. This hack is no longer used. */
658 : 561668200 : gcc_assert (keep == 0 || keep == 1);
659 : :
660 : 561668200 : if (current_binding_level->keep)
661 : 7172940 : keep = 1;
662 : :
663 : : /* Any uses of undefined labels, and any defined labels, now operate
664 : : under constraints of next binding contour. */
665 : 561668200 : if (cfun && !functionbody && named_labels)
666 : 41386 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
667 : 186121 : (current_binding_level);
668 : :
669 : : /* Get the decls in the order they were written.
670 : : Usually current_binding_level->names is in reverse order.
671 : : But parameter decls were previously put in forward order. */
672 : :
673 : 561668200 : decls = current_binding_level->names;
674 : 561668200 : if (reverse)
675 : : {
676 : 301656954 : decls = nreverse (decls);
677 : 301656954 : current_binding_level->names = decls;
678 : : }
679 : :
680 : : /* If there were any declarations or structure tags in that level,
681 : : or if this level is a function body,
682 : : create a BLOCK to record them for the life of this function. */
683 : 561668200 : block = NULL_TREE;
684 : : /* Avoid function body block if possible. */
685 : 596026333 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
686 : : keep = 0;
687 : 527310322 : else if (keep == 1 || functionbody)
688 : 173547867 : block = make_node (BLOCK);
689 : 173547867 : if (block != NULL_TREE)
690 : : {
691 : 173547867 : BLOCK_VARS (block) = decls;
692 : 173547867 : BLOCK_SUBBLOCKS (block) = subblocks;
693 : : }
694 : :
695 : : /* In each subblock, record that this is its superior. */
696 : 561668200 : if (keep >= 0)
697 : 630229904 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
698 : 68561704 : BLOCK_SUPERCONTEXT (link) = block;
699 : :
700 : : /* Before we remove the declarations first check for unused variables. */
701 : 561668200 : if ((warn_unused_variable || warn_unused_but_set_variable)
702 : 6154870 : && current_binding_level->kind != sk_template_parms
703 : 5210350 : && !processing_template_decl)
704 : 6144601 : for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
705 : : {
706 : : /* There are cases where D itself is a TREE_LIST. See in
707 : : push_local_binding where the list of decls returned by
708 : : getdecls is built. */
709 : 3156625 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
710 : 3156625 : decl = strip_using_decl (udecl);
711 : :
712 : 3156625 : tree type = TREE_TYPE (decl);
713 : 3156625 : if (VAR_P (decl)
714 : 363511 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
715 : 2974 : && !DECL_IN_SYSTEM_HEADER (udecl)
716 : : /* For structured bindings, consider only real variables, not
717 : : subobjects. */
718 : 2962 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
719 : 2749 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
720 : : /* Don't warn about name-independent declarations. */
721 : 643 : && !name_independent_decl_p (decl)
722 : 499 : && type != error_mark_node
723 : 3160054 : && (!CLASS_TYPE_P (type)
724 : 51 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
725 : 3 : || lookup_attribute ("warn_unused",
726 : 3 : TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
727 : : {
728 : 467 : if (!TREE_USED (decl))
729 : : {
730 : 302 : if (TREE_CODE (udecl) == USING_DECL)
731 : 3 : warning_at (DECL_SOURCE_LOCATION (udecl),
732 : 3 : OPT_Wunused_variable,
733 : : "unused using-declaration %qD", udecl);
734 : 299 : else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
735 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
736 : 3 : OPT_Wunused_variable,
737 : : "unused structured binding declaration");
738 : : else
739 : 296 : warning_at (DECL_SOURCE_LOCATION (decl),
740 : 296 : OPT_Wunused_variable, "unused variable %qD", decl);
741 : 302 : suppress_warning (decl, OPT_Wunused_variable);
742 : : }
743 : 165 : else if (DECL_CONTEXT (decl) == current_function_decl
744 : : // For -Wunused-but-set-variable leave references alone.
745 : 141 : && !TYPE_REF_P (TREE_TYPE (decl))
746 : 306 : && errorcount == unused_but_set_errorcount)
747 : : {
748 : 132 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
749 : 12 : warning_at (DECL_SOURCE_LOCATION (decl),
750 : 12 : OPT_Wunused_but_set_variable, "structured "
751 : : "binding declaration set but not used");
752 : : else
753 : 120 : warning_at (DECL_SOURCE_LOCATION (decl),
754 : 120 : OPT_Wunused_but_set_variable,
755 : : "variable %qD set but not used", decl);
756 : 132 : unused_but_set_errorcount = errorcount;
757 : : }
758 : : }
759 : : }
760 : :
761 : : /* Remove declarations for all the DECLs in this level. */
762 : 1144149562 : for (link = decls; link; link = TREE_CHAIN (link))
763 : : {
764 : 582481362 : tree name;
765 : 582481362 : if (TREE_CODE (link) == TREE_LIST)
766 : : {
767 : 839740 : decl = TREE_VALUE (link);
768 : 839740 : name = TREE_PURPOSE (link);
769 : 839740 : gcc_checking_assert (name);
770 : : }
771 : : else
772 : : {
773 : 581641622 : decl = link;
774 : 581641622 : name = DECL_NAME (decl);
775 : : }
776 : :
777 : : /* Remove the binding. */
778 : 582481362 : if (TREE_CODE (decl) == LABEL_DECL)
779 : 207 : pop_local_label (name, decl);
780 : : else
781 : 582481155 : pop_local_binding (name, decl);
782 : : }
783 : :
784 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
785 : 561668200 : for (link = current_binding_level->type_shadowed;
786 : 764683886 : link; link = TREE_CHAIN (link))
787 : 203015686 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
788 : :
789 : : /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
790 : : list if a `using' declaration put them there. The debugging
791 : : back ends won't understand OVERLOAD, so we remove them here.
792 : : Because the BLOCK_VARS are (temporarily) shared with
793 : : CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
794 : : popped all the bindings. Also remove undeduced 'auto' decls,
795 : : which LTO doesn't understand, and can't have been used by anything. */
796 : 561668200 : if (block)
797 : : {
798 : 173547867 : tree* d;
799 : :
800 : 454314781 : for (d = &BLOCK_VARS (block); *d; )
801 : : {
802 : 280766914 : if (TREE_CODE (*d) == TREE_LIST
803 : 280766914 : || (!processing_template_decl
804 : 90401495 : && undeduced_auto_decl (*d)))
805 : 839774 : *d = TREE_CHAIN (*d);
806 : : else
807 : 279927140 : d = &DECL_CHAIN (*d);
808 : : }
809 : : }
810 : :
811 : : /* If the level being exited is the top level of a function,
812 : : check over all the labels. */
813 : 561668200 : if (functionbody)
814 : : {
815 : 139343303 : if (block)
816 : : {
817 : : /* Since this is the top level block of a function, the vars are
818 : : the function's parameters. Don't leave them in the BLOCK
819 : : because they are found in the FUNCTION_DECL instead. */
820 : 104985425 : BLOCK_VARS (block) = 0;
821 : 104985425 : pop_labels (block);
822 : : }
823 : : else
824 : 34357878 : pop_labels (subblocks);
825 : : }
826 : :
827 : 561668200 : kind = current_binding_level->kind;
828 : 561668200 : if (kind == sk_cleanup)
829 : : {
830 : 264 : tree stmt;
831 : :
832 : : /* If this is a temporary binding created for a cleanup, then we'll
833 : : have pushed a statement list level. Pop that, create a new
834 : : BIND_EXPR for the block, and insert it into the stream. */
835 : 264 : stmt = pop_stmt_list (current_binding_level->statement_list);
836 : 264 : stmt = c_build_bind_expr (input_location, block, stmt);
837 : 264 : add_stmt (stmt);
838 : : }
839 : :
840 : 561668200 : leave_scope ();
841 : 561668200 : if (functionbody)
842 : : {
843 : : /* The current function is being defined, so its DECL_INITIAL
844 : : should be error_mark_node. */
845 : 139343303 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
846 : 173701181 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
847 : 139343303 : if (subblocks)
848 : : {
849 : 64618321 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
850 : 42559345 : && BLOCK_SUBBLOCKS (subblocks))
851 : 1174167 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
852 : : else
853 : 33183966 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
854 : : }
855 : : }
856 : 422324897 : else if (block)
857 : 68562442 : current_binding_level->blocks
858 : 137124884 : = block_chainon (current_binding_level->blocks, block);
859 : :
860 : : /* If we did not make a block for the level just exited,
861 : : any blocks made for inner levels
862 : : (since they cannot be recorded as subblocks in that level)
863 : : must be carried forward so they will later become subblocks
864 : : of something else. */
865 : 353762455 : else if (subblocks)
866 : 9 : current_binding_level->blocks
867 : 18 : = block_chainon (current_binding_level->blocks, subblocks);
868 : :
869 : : /* Each and every BLOCK node created here in `poplevel' is important
870 : : (e.g. for proper debugging information) so if we created one
871 : : earlier, mark it as "used". */
872 : 561668200 : if (block)
873 : 173547867 : TREE_USED (block) = 1;
874 : :
875 : : /* All temporary bindings created for cleanups are popped silently. */
876 : 561668200 : if (kind == sk_cleanup)
877 : 264 : goto restart;
878 : :
879 : 1123335872 : return block;
880 : 561667936 : }
881 : :
882 : : /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
883 : : /* Diagnose odr-used extern inline variables without definitions
884 : : in the current TU. */
885 : :
886 : : int
887 : 139067 : wrapup_namespace_globals ()
888 : : {
889 : 139067 : if (vec<tree, va_gc> *statics = static_decls)
890 : : {
891 : 14241330 : for (tree decl : *statics)
892 : : {
893 : 14127015 : if (warn_unused_function
894 : 979337 : && TREE_CODE (decl) == FUNCTION_DECL
895 : 734436 : && DECL_INITIAL (decl) == 0
896 : 2635 : && DECL_EXTERNAL (decl)
897 : 2635 : && !TREE_PUBLIC (decl)
898 : 19 : && !DECL_ARTIFICIAL (decl)
899 : 19 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
900 : 14127021 : && !warning_suppressed_p (decl, OPT_Wunused_function))
901 : 6 : warning_at (DECL_SOURCE_LOCATION (decl),
902 : 6 : OPT_Wunused_function,
903 : : "%qF declared %<static%> but never defined", decl);
904 : :
905 : 14127015 : if (VAR_P (decl)
906 : 4208857 : && DECL_EXTERNAL (decl)
907 : 1981128 : && DECL_INLINE_VAR_P (decl)
908 : 14127033 : && DECL_ODR_USED (decl))
909 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
910 : : "odr-used inline variable %qD is not defined", decl);
911 : : }
912 : :
913 : : /* Clear out the list, so we don't rescan next time. */
914 : 114315 : static_decls = NULL;
915 : :
916 : : /* Write out any globals that need to be output. */
917 : 114315 : return wrapup_global_declarations (statics->address (),
918 : 114315 : statics->length ());
919 : : }
920 : : return 0;
921 : : }
922 : :
923 : : /* In C++, you don't have to write `struct S' to refer to `S'; you
924 : : can just use `S'. We accomplish this by creating a TYPE_DECL as
925 : : if the user had written `typedef struct S S'. Create and return
926 : : the TYPE_DECL for TYPE. */
927 : :
928 : : tree
929 : 146337605 : create_implicit_typedef (tree name, tree type)
930 : : {
931 : 146337605 : tree decl;
932 : :
933 : 146337605 : decl = build_decl (input_location, TYPE_DECL, name, type);
934 : 146337605 : DECL_ARTIFICIAL (decl) = 1;
935 : : /* There are other implicit type declarations, like the one *within*
936 : : a class that allows you to write `S::S'. We must distinguish
937 : : amongst these. */
938 : 146337605 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
939 : 146337605 : TYPE_NAME (type) = decl;
940 : 146337605 : TYPE_STUB_DECL (type) = decl;
941 : :
942 : 146337605 : return decl;
943 : : }
944 : :
945 : : /* Function-scope local entities that need discriminators. Each entry
946 : : is a {decl,name} pair. VAR_DECLs for anon unions get their name
947 : : smashed, so we cannot rely on DECL_NAME. */
948 : :
949 : : static GTY((deletable)) vec<tree, va_gc> *local_entities;
950 : :
951 : : /* Determine the mangling discriminator of local DECL. There are
952 : : generally very few of these in any particular function. */
953 : :
954 : : void
955 : 690545 : determine_local_discriminator (tree decl, tree name)
956 : : {
957 : 690545 : auto_cond_timevar tv (TV_NAME_LOOKUP);
958 : 690545 : retrofit_lang_decl (decl);
959 : 690545 : tree ctx = DECL_CONTEXT (decl);
960 : 690545 : size_t nelts = vec_safe_length (local_entities);
961 : 690545 : if (name == NULL_TREE)
962 : 690422 : name = (TREE_CODE (decl) == TYPE_DECL
963 : 1106270 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
964 : 1239975 : ? NULL_TREE : DECL_NAME (decl));
965 : 20522988 : for (size_t i = 0; i < nelts; i += 2)
966 : : {
967 : 19832798 : tree *pair = &(*local_entities)[i];
968 : 19832798 : tree d = pair[0];
969 : 19832798 : tree n = pair[1];
970 : 19832798 : gcc_checking_assert (d != decl);
971 : 19832798 : if (name == n
972 : 3546804 : && TREE_CODE (decl) == TREE_CODE (d)
973 : 23379593 : && ctx == DECL_CONTEXT (d))
974 : : {
975 : 355 : tree disc = integer_one_node;
976 : 355 : if (DECL_DISCRIMINATOR (d))
977 : 179 : disc = build_int_cst (TREE_TYPE (disc),
978 : 179 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
979 : 355 : DECL_DISCRIMINATOR (decl) = disc;
980 : : /* Replace the saved decl. */
981 : 355 : pair[0] = decl;
982 : 355 : decl = NULL_TREE;
983 : 355 : break;
984 : : }
985 : : }
986 : :
987 : 690545 : if (decl)
988 : : {
989 : 690190 : vec_safe_reserve (local_entities, 2);
990 : 690190 : local_entities->quick_push (decl);
991 : 690190 : local_entities->quick_push (name);
992 : : }
993 : 690545 : }
994 : :
995 : :
996 : : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
997 : :
998 : : A non-template friend declaration with a requires-clause shall be a
999 : : definition. A friend function template with a constraint that depends on a
1000 : : template parameter from an enclosing template shall be a definition. Such a
1001 : : constrained friend function or function template declaration does not
1002 : : declare the same function or function template as a declaration in any other
1003 : : scope.
1004 : :
1005 : : The ABI calls this a "member-like constrained friend" and mangles it like a
1006 : : member function to avoid collisions. */
1007 : :
1008 : : bool
1009 : 1925280231 : member_like_constrained_friend_p (tree decl)
1010 : : {
1011 : 1925280231 : return (TREE_CODE (decl) == FUNCTION_DECL
1012 : 582082123 : && DECL_UNIQUE_FRIEND_P (decl)
1013 : 76130362 : && DECL_FRIEND_CONTEXT (decl)
1014 : 33628585 : && get_constraints (decl)
1015 : 1925560419 : && (!DECL_TEMPLATE_INFO (decl)
1016 : 280188 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1017 : 2685 : || (uses_outer_template_parms_in_constraints
1018 : 2685 : (most_general_template (decl)))));
1019 : : }
1020 : :
1021 : : /* Returns true if functions FN1 and FN2 have equivalent trailing
1022 : : requires clauses. */
1023 : :
1024 : : static bool
1025 : 25182208 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1026 : : {
1027 : : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1028 : : same function as a declaration in any other scope." So no need to
1029 : : actually compare the requirements. */
1030 : 25182208 : if (member_like_constrained_friend_p (newfn)
1031 : 25182208 : || member_like_constrained_friend_p (oldfn))
1032 : 107 : return false;
1033 : :
1034 : : /* Compare only trailing requirements. */
1035 : 25182101 : tree reqs1 = get_trailing_function_requirements (newfn);
1036 : 25182101 : tree reqs2 = get_trailing_function_requirements (oldfn);
1037 : 25182101 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1038 : : return false;
1039 : :
1040 : : /* Substitution is needed when friends are involved. */
1041 : 25140233 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1042 : 25140233 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1043 : :
1044 : 25140233 : return cp_tree_equal (reqs1, reqs2);
1045 : : }
1046 : :
1047 : : /* Two functions of the same name correspond [basic.scope.scope] if
1048 : :
1049 : : + both declare functions with the same non-object-parameter-type-list,
1050 : : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1051 : : specified in [temp.friend]), and, if both are non-static members, they have
1052 : : corresponding object parameters, or
1053 : :
1054 : : + both declare function templates with equivalent
1055 : : non-object-parameter-type-lists, return types (if any), template-heads, and
1056 : : trailing requires-clauses (if any), and, if both are non-static members,
1057 : : they have corresponding object parameters.
1058 : :
1059 : : This is a subset of decls_match: it identifies declarations that cannot be
1060 : : overloaded with one another. This function does not consider DECL_NAME. */
1061 : :
1062 : : bool
1063 : 715347822 : fns_correspond (tree newdecl, tree olddecl)
1064 : : {
1065 : 715347822 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1066 : : return false;
1067 : :
1068 : 666078762 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1069 : : {
1070 : 666053910 : if (!template_heads_equivalent_p (newdecl, olddecl))
1071 : : return 0;
1072 : 154231895 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1073 : 154231895 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1074 : : }
1075 : :
1076 : 154256747 : tree f1 = TREE_TYPE (newdecl);
1077 : 154256747 : tree f2 = TREE_TYPE (olddecl);
1078 : :
1079 : 154256747 : int rq1 = type_memfn_rqual (f1);
1080 : 154256747 : int rq2 = type_memfn_rqual (f2);
1081 : :
1082 : : /* If only one is a non-static member function, ignore ref-quals. */
1083 : 154256747 : if (TREE_CODE (f1) != TREE_CODE (f2))
1084 : : rq1 = rq2;
1085 : : /* Two non-static member functions have corresponding object parameters if:
1086 : : + exactly one is an implicit object member function with no ref-qualifier
1087 : : and the types of their object parameters ([dcl.fct]), after removing
1088 : : top-level references, are the same, or
1089 : : + their object parameters have the same type. */
1090 : : /* ??? We treat member functions of different classes as corresponding even
1091 : : though that means the object parameters have different types. */
1092 : 154256747 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1093 : 0 : rq1 = rq2;
1094 : :
1095 : 154256747 : bool types_match = rq1 == rq2;
1096 : :
1097 : 154256747 : if (types_match)
1098 : : {
1099 : 154256747 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1100 : 154256747 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1101 : 154256747 : types_match = compparms (p1, p2);
1102 : : }
1103 : :
1104 : : /* Two function declarations match if either has a requires-clause
1105 : : then both have a requires-clause and their constraints-expressions
1106 : : are equivalent. */
1107 : 154256747 : if (types_match && flag_concepts)
1108 : 18781505 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1109 : :
1110 : : return types_match;
1111 : : }
1112 : :
1113 : : /* Subroutine of duplicate_decls: return truthvalue of whether
1114 : : or not types of these decls match.
1115 : :
1116 : : For C++, we must compare the parameter list so that `int' can match
1117 : : `int&' in a parameter position, but `int&' is not confused with
1118 : : `const int&'. */
1119 : :
1120 : : int
1121 : 913947005 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1122 : : {
1123 : 913947005 : int types_match;
1124 : :
1125 : 913947005 : if (newdecl == olddecl)
1126 : : return 1;
1127 : :
1128 : 913882596 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1129 : : /* If the two DECLs are not even the same kind of thing, we're not
1130 : : interested in their types. */
1131 : : return 0;
1132 : :
1133 : 450868795 : gcc_assert (DECL_P (newdecl));
1134 : :
1135 : 450868795 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1136 : : {
1137 : : /* Specializations of different templates are different functions
1138 : : even if they have the same type. */
1139 : 251590965 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1140 : 251590965 : ? DECL_TI_TEMPLATE (newdecl)
1141 : 566149 : : NULL_TREE);
1142 : 251590965 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1143 : 251590965 : ? DECL_TI_TEMPLATE (olddecl)
1144 : 3110091 : : NULL_TREE);
1145 : 251590965 : if (t1 != t2)
1146 : : return 0;
1147 : :
1148 : 249046957 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1149 : 252139155 : && ! (DECL_EXTERN_C_P (newdecl)
1150 : 3092198 : && DECL_EXTERN_C_P (olddecl)))
1151 : : return 0;
1152 : :
1153 : : /* A new declaration doesn't match a built-in one unless it
1154 : : is also extern "C". */
1155 : 230032337 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1156 : 230032337 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1157 : : return 0;
1158 : :
1159 : 229358017 : tree f1 = TREE_TYPE (newdecl);
1160 : 229358017 : tree f2 = TREE_TYPE (olddecl);
1161 : 229358017 : if (TREE_CODE (f1) != TREE_CODE (f2))
1162 : : return 0;
1163 : :
1164 : : /* A declaration with deduced return type should use its pre-deduction
1165 : : type for declaration matching. */
1166 : 229196475 : tree r2 = fndecl_declared_return_type (olddecl);
1167 : 229196475 : tree r1 = fndecl_declared_return_type (newdecl);
1168 : :
1169 : : /* For memchr et al, allow const void* return type (as specified by C++)
1170 : : when we expect void* (as in C). */
1171 : 229196475 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1172 : 6386576 : && DECL_EXTERN_C_P (olddecl)
1173 : 4042537 : && !same_type_p (r1, r2)
1174 : 70 : && TREE_CODE (r1) == POINTER_TYPE
1175 : 15 : && TREE_CODE (r2) == POINTER_TYPE
1176 : 229196487 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1177 : : r2 = r1;
1178 : :
1179 : 229196475 : tree p1 = TYPE_ARG_TYPES (f1);
1180 : 229196475 : tree p2 = TYPE_ARG_TYPES (f2);
1181 : :
1182 : 229196475 : if (same_type_p (r1, r2))
1183 : : {
1184 : 168245861 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1185 : 168245831 : && fndecl_built_in_p (olddecl))
1186 : : {
1187 : 50 : types_match = self_promoting_args_p (p1);
1188 : 50 : if (p1 == void_list_node)
1189 : 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1190 : : }
1191 : : else
1192 : 168245722 : types_match =
1193 : 168245722 : compparms (p1, p2)
1194 : 26037710 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1195 : 194283204 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1196 : 1201964 : || comp_type_attributes (TREE_TYPE (newdecl),
1197 : 1201964 : TREE_TYPE (olddecl)) != 0);
1198 : : }
1199 : : else
1200 : : types_match = 0;
1201 : :
1202 : : /* Two function declarations match if either has a requires-clause
1203 : : then both have a requires-clause and their constraints-expressions
1204 : : are equivalent. */
1205 : 26037523 : if (types_match && flag_concepts)
1206 : 6400677 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1207 : :
1208 : : /* The decls dont match if they correspond to two different versions
1209 : : of the same function. Disallow extern "C" functions to be
1210 : : versions for now. */
1211 : 26037523 : if (types_match
1212 : 25980529 : && !DECL_EXTERN_C_P (newdecl)
1213 : 20018983 : && !DECL_EXTERN_C_P (olddecl)
1214 : 46052780 : && targetm.target_option.function_versions (newdecl, olddecl))
1215 : : {
1216 : 8574 : if (record_versions)
1217 : 16974 : maybe_version_functions (newdecl, olddecl,
1218 : 8487 : (!DECL_FUNCTION_VERSIONED (newdecl)
1219 : 16104 : || !DECL_FUNCTION_VERSIONED (olddecl)));
1220 : 8574 : return 0;
1221 : : }
1222 : : }
1223 : 199277830 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1224 : : {
1225 : 197862873 : if (!template_heads_equivalent_p (newdecl, olddecl))
1226 : : return 0;
1227 : :
1228 : 63014084 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1229 : 63014084 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1230 : :
1231 : 63014084 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1232 : : return 0;
1233 : :
1234 : : /* Two template types match if they are the same. Otherwise, compare
1235 : : the underlying declarations. */
1236 : 63014078 : if (TREE_CODE (newres) == TYPE_DECL)
1237 : 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1238 : : else
1239 : 63014048 : types_match = decls_match (newres, oldres);
1240 : : }
1241 : : else
1242 : : {
1243 : : /* Need to check scope for variable declaration (VAR_DECL).
1244 : : For typedef (TYPE_DECL), scope is ignored. */
1245 : 1414957 : if (VAR_P (newdecl)
1246 : 945016 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1247 : : /* [dcl.link]
1248 : : Two declarations for an object with C language linkage
1249 : : with the same name (ignoring the namespace that qualify
1250 : : it) that appear in different namespace scopes refer to
1251 : : the same object. */
1252 : 1589037 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1253 : : return 0;
1254 : :
1255 : 1240925 : if (TREE_TYPE (newdecl) == error_mark_node)
1256 : 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1257 : 1240925 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1258 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1259 : 1240925 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1260 : : types_match = 0;
1261 : : else
1262 : 1240925 : types_match = comptypes (TREE_TYPE (newdecl),
1263 : 1240925 : TREE_TYPE (olddecl),
1264 : : COMPARE_REDECLARATION);
1265 : : }
1266 : :
1267 : : return types_match;
1268 : : }
1269 : :
1270 : : /* Mark DECL as versioned if it isn't already. */
1271 : :
1272 : : static void
1273 : 17064 : maybe_mark_function_versioned (tree decl)
1274 : : {
1275 : 17064 : if (!DECL_FUNCTION_VERSIONED (decl))
1276 : : {
1277 : 1044 : DECL_FUNCTION_VERSIONED (decl) = 1;
1278 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1279 : : to include the version marker. */
1280 : 1044 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1281 : 42 : mangle_decl (decl);
1282 : : }
1283 : 17064 : }
1284 : :
1285 : : /* NEWDECL and OLDDECL have identical signatures. If they are
1286 : : different versions adjust them and return true.
1287 : : If RECORD is set to true, record function versions. */
1288 : :
1289 : : bool
1290 : 161396 : maybe_version_functions (tree newdecl, tree olddecl, bool record)
1291 : : {
1292 : 161396 : if (!targetm.target_option.function_versions (newdecl, olddecl))
1293 : : return false;
1294 : :
1295 : 8514 : maybe_mark_function_versioned (olddecl);
1296 : 8514 : if (DECL_LOCAL_DECL_P (olddecl))
1297 : : {
1298 : 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1299 : 18 : maybe_mark_function_versioned (olddecl);
1300 : : }
1301 : :
1302 : 8514 : maybe_mark_function_versioned (newdecl);
1303 : 8514 : if (DECL_LOCAL_DECL_P (newdecl))
1304 : : {
1305 : : /* Unfortunately, we can get here before pushdecl naturally calls
1306 : : push_local_extern_decl_alias, so we need to call it directly. */
1307 : 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1308 : 6 : push_local_extern_decl_alias (newdecl);
1309 : 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1310 : 18 : maybe_mark_function_versioned (newdecl);
1311 : : }
1312 : :
1313 : 8514 : if (record)
1314 : : {
1315 : : /* Add the new version to the function version structure. */
1316 : 897 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1317 : 897 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1318 : 897 : if (!fn_v)
1319 : 171 : fn_v = fn_node->insert_new_function_version ();
1320 : :
1321 : 897 : cgraph_node::add_function_version (fn_v, newdecl);
1322 : : }
1323 : :
1324 : : return true;
1325 : : }
1326 : :
1327 : : /* If NEWDECL is `static' and an `extern' was seen previously,
1328 : : warn about it. OLDDECL is the previous declaration.
1329 : :
1330 : : Note that this does not apply to the C++ case of declaring
1331 : : a variable `extern const' and then later `const'.
1332 : :
1333 : : Don't complain about built-in functions, since they are beyond
1334 : : the user's control. */
1335 : :
1336 : : void
1337 : 17993153 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1338 : : {
1339 : 17993153 : if (TREE_CODE (newdecl) == TYPE_DECL
1340 : 17968549 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1341 : 14462449 : || TREE_CODE (newdecl) == CONST_DECL
1342 : 14462449 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1343 : 17993141 : return;
1344 : :
1345 : : /* Don't get confused by static member functions; that's a different
1346 : : use of `static'. */
1347 : 14462449 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1348 : 14462449 : && DECL_STATIC_FUNCTION_P (newdecl))
1349 : : return;
1350 : :
1351 : : /* If the old declaration was `static', or the new one isn't, then
1352 : : everything is OK. */
1353 : 14304678 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1354 : : return;
1355 : :
1356 : : /* It's OK to declare a builtin function as `static'. */
1357 : 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1358 : 12 : && DECL_ARTIFICIAL (olddecl))
1359 : : return;
1360 : :
1361 : 12 : auto_diagnostic_group d;
1362 : 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1363 : : "%qD was declared %<extern%> and later %<static%>", newdecl))
1364 : 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1365 : : "previous declaration of %qD", olddecl);
1366 : 12 : }
1367 : :
1368 : : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1369 : : function templates. If their exception specifications do not
1370 : : match, issue a diagnostic. */
1371 : :
1372 : : static void
1373 : 17583487 : check_redeclaration_exception_specification (tree new_decl,
1374 : : tree old_decl)
1375 : : {
1376 : 17583487 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1377 : 17583487 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1378 : :
1379 : : /* Two default specs are equivalent, don't force evaluation. */
1380 : 6210444 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1381 : 17731033 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1382 : : return;
1383 : :
1384 : 17573340 : if (!type_dependent_expression_p (old_decl))
1385 : : {
1386 : 10122383 : maybe_instantiate_noexcept (new_decl);
1387 : 10122383 : maybe_instantiate_noexcept (old_decl);
1388 : : }
1389 : 17573340 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1390 : 17573340 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1391 : :
1392 : : /* [except.spec]
1393 : :
1394 : : If any declaration of a function has an exception-specification,
1395 : : all declarations, including the definition and an explicit
1396 : : specialization, of that function shall have an
1397 : : exception-specification with the same set of type-ids. */
1398 : 17573340 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1399 : 13657419 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1400 : 31230681 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1401 : : {
1402 : 326 : const char *const msg
1403 : : = G_("declaration of %qF has a different exception specifier");
1404 : 326 : bool complained = true;
1405 : 326 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1406 : 326 : auto_diagnostic_group d;
1407 : :
1408 : : /* Be permissive about C++98 vs C++11 operator new declarations. */
1409 : 652 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1410 : 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1411 : 328 : && (nothrow_spec_p (new_exceptions)
1412 : 1 : == nothrow_spec_p (old_exceptions)));
1413 : :
1414 : 326 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1415 : 326 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1416 : : /* Don't fuss about the C library; the C library functions are not
1417 : : specified to have exception specifications (just behave as if they
1418 : : have them), but some implementations include them. */
1419 : 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1420 : 93 : else if (!flag_exceptions)
1421 : : /* We used to silently permit mismatched eh specs with
1422 : : -fno-exceptions, so only complain if -pedantic. */
1423 : 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1424 : 90 : else if (!new_exceptions || global_new)
1425 : : /* Reduce to pedwarn for omitted exception specification. No warning
1426 : : flag for this; silence the warning by correcting the code. */
1427 : 21 : complained = pedwarn (new_loc, 0, msg, new_decl);
1428 : : else
1429 : 69 : error_at (new_loc, msg, new_decl);
1430 : :
1431 : 326 : if (complained)
1432 : 96 : inform (DECL_SOURCE_LOCATION (old_decl),
1433 : : "from previous declaration %qF", old_decl);
1434 : :
1435 : : /* Copy the old exception specification if new_decl has none. Unless the
1436 : : old decl is extern "C", as obscure code might depend on the type of
1437 : : the new declaration (e.g. noexcept-type19.C). */
1438 : 326 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1439 : 20 : TREE_TYPE (new_decl)
1440 : 40 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1441 : 326 : }
1442 : : }
1443 : :
1444 : : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1445 : : Otherwise issue diagnostics. */
1446 : :
1447 : : bool
1448 : 17993634 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1449 : : {
1450 : 17993634 : old_decl = STRIP_TEMPLATE (old_decl);
1451 : 17993634 : new_decl = STRIP_TEMPLATE (new_decl);
1452 : 17993634 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1453 : 17969006 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1454 : : return true;
1455 : 17969006 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1456 : 17969006 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1457 : : {
1458 : 17928551 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1459 : : return true;
1460 : 35167746 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1461 : 17583873 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1462 : : return true;
1463 : : }
1464 : 40461 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1465 : : {
1466 : : /* With -fimplicit-constexpr, ignore changes in the constexpr
1467 : : keyword. */
1468 : 61 : if (flag_implicit_constexpr
1469 : 61 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1470 : 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1471 : : return true;
1472 : 61 : if (fndecl_built_in_p (old_decl))
1473 : : {
1474 : : /* Hide a built-in declaration. */
1475 : 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1476 : 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1477 : 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1478 : 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1479 : 0 : return true;
1480 : : }
1481 : : /* 7.1.5 [dcl.constexpr]
1482 : : Note: An explicit specialization can differ from the template
1483 : : declaration with respect to the constexpr specifier. */
1484 : 61 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1485 : 61 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1486 : : return true;
1487 : :
1488 : 42 : const char *kind = "constexpr";
1489 : 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1490 : 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1491 : : kind = "consteval";
1492 : 42 : auto_diagnostic_group d;
1493 : 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1494 : : "redeclaration %qD differs in %qs "
1495 : : "from previous declaration", new_decl,
1496 : : kind);
1497 : 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1498 : : "previous declaration %qD", old_decl);
1499 : 42 : return false;
1500 : 42 : }
1501 : : return true;
1502 : : }
1503 : :
1504 : : /* DECL is a redeclaration of a function or function template. If
1505 : : it does have default arguments issue a diagnostic. Note: this
1506 : : function is used to enforce the requirements in C++11 8.3.6 about
1507 : : no default arguments in redeclarations. */
1508 : :
1509 : : static void
1510 : 11032750 : check_redeclaration_no_default_args (tree decl)
1511 : : {
1512 : 11032750 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1513 : :
1514 : 11032750 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1515 : 32437040 : t && t != void_list_node; t = TREE_CHAIN (t))
1516 : 21404344 : if (TREE_PURPOSE (t))
1517 : : {
1518 : 54 : permerror (DECL_SOURCE_LOCATION (decl),
1519 : : "redeclaration of %q#D may not have default "
1520 : : "arguments", decl);
1521 : 54 : return;
1522 : : }
1523 : : }
1524 : :
1525 : : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1526 : : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1527 : : the TEMPLATE_DECLs in case of function templates). This function is used
1528 : : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1529 : : "If a friend declaration specifies a default argument expression, that
1530 : : declaration shall be a definition and shall be the only declaration of
1531 : : the function or function template in the translation unit." */
1532 : :
1533 : : static void
1534 : 5529895 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1535 : : {
1536 : 10702235 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1537 : : return;
1538 : :
1539 : 2021918 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1540 : 1010959 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1541 : 2716549 : t1 && t1 != void_list_node;
1542 : 1705590 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1543 : 2380665 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1544 : 4687689 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1545 : : {
1546 : 36 : auto_diagnostic_group d;
1547 : 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1548 : : "friend declaration of %q#D specifies default "
1549 : : "arguments and isn%'t the only declaration", newdecl))
1550 : 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1551 : : "previous declaration of %q#D", olddecl);
1552 : 36 : return;
1553 : 36 : }
1554 : : }
1555 : :
1556 : : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1557 : : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1558 : :
1559 : : static void
1560 : 17017723 : merge_attribute_bits (tree newdecl, tree olddecl)
1561 : : {
1562 : 17017723 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1563 : 17017723 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1564 : 17017723 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1565 : 17017723 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1566 : 17017723 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1567 : 17017723 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1568 : 17017723 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1569 : 17017723 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1570 : 17017723 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1571 : 17017723 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1572 : 17017723 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1573 : 17017723 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1574 : 17017723 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1575 : 17017723 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1576 : 17017723 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1577 : 17017723 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1578 : 17017723 : }
1579 : :
1580 : : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1581 : : && lookup_attribute ("gnu_inline", \
1582 : : DECL_ATTRIBUTES (fn)))
1583 : :
1584 : : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1585 : : ambiguates olddecl. Returns true if an error occurs. */
1586 : :
1587 : : static bool
1588 : 194356707 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1589 : : {
1590 : 194356707 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1591 : 194356707 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1592 : : /* Function template declarations can be differentiated by parameter
1593 : : and return type. */
1594 : 194356707 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1595 : 194356707 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1596 : 194356707 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1597 : : TREE_TYPE (TREE_TYPE (olddecl))))
1598 : : {
1599 : : /* ... and also by their template-heads and requires-clauses. */
1600 : 3993 : if (template_heads_equivalent_p (newdecl, olddecl)
1601 : 3993 : && function_requirements_equivalent_p (newres, oldres))
1602 : : {
1603 : 0 : auto_diagnostic_group d;
1604 : 0 : error ("ambiguating new declaration %q+#D", newdecl);
1605 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1606 : : "old declaration %q#D", olddecl);
1607 : 0 : return true;
1608 : 0 : }
1609 : :
1610 : : /* FIXME: The types are the same but the are differences
1611 : : in either the template heads or function requirements.
1612 : : We should be able to diagnose a set of common errors
1613 : : stemming from these declarations. For example:
1614 : :
1615 : : template<typename T> requires C void f(...);
1616 : : template<typename T> void f(...) requires C;
1617 : :
1618 : : These are functionally equivalent but not equivalent. */
1619 : : }
1620 : :
1621 : : return false;
1622 : : }
1623 : :
1624 : : /* OLD_PARMS is the innermost set of template parameters for some template
1625 : : declaration, and NEW_PARMS is the corresponding set of template parameters
1626 : : for a redeclaration of that template. Merge the default arguments within
1627 : : these two sets of parameters. CLASS_P is true iff the template in
1628 : : question is a class template. */
1629 : :
1630 : : bool
1631 : 7931711 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1632 : : {
1633 : 7931711 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1634 : : == TREE_VEC_LENGTH (old_parms));
1635 : 22433474 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1636 : : {
1637 : 14501775 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1638 : 14501775 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1639 : 14501775 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1640 : 14501775 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1641 : 14501775 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1642 : : return false;
1643 : 14501772 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1644 : : {
1645 : 9 : auto_diagnostic_group d;
1646 : 9 : error ("redefinition of default argument for %q+#D", new_parm);
1647 : 9 : inform (DECL_SOURCE_LOCATION (old_parm),
1648 : : "original definition appeared here");
1649 : 9 : return false;
1650 : 9 : }
1651 : 14501763 : else if (new_default != NULL_TREE)
1652 : : /* Update the previous template parameters (which are the ones
1653 : : that will really count) with the new default value. */
1654 : 194052 : old_default = new_default;
1655 : 14307711 : else if (class_p && old_default != NULL_TREE)
1656 : : /* Update the new parameters, too; they'll be used as the
1657 : : parameters for any members. */
1658 : 984978 : new_default = old_default;
1659 : : }
1660 : : return true;
1661 : : }
1662 : :
1663 : : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1664 : : If the redeclaration is invalid, a diagnostic is issued, and the
1665 : : error_mark_node is returned. Otherwise, OLDDECL is returned.
1666 : :
1667 : : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1668 : : returned.
1669 : :
1670 : : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1671 : : if the old decl was hidden.
1672 : :
1673 : : Hidden decls can be anticipated builtins, injected friends, or
1674 : : (coming soon) injected from a local-extern decl. */
1675 : :
1676 : : tree
1677 : 329794258 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1678 : : {
1679 : 329794258 : unsigned olddecl_uid = DECL_UID (olddecl);
1680 : 329794258 : int types_match = 0;
1681 : 329794258 : int new_defines_function = 0;
1682 : 329794258 : tree new_template_info;
1683 : 329794258 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1684 : 329794258 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1685 : :
1686 : 329794258 : if (newdecl == olddecl)
1687 : : return olddecl;
1688 : :
1689 : 328951000 : types_match = decls_match (newdecl, olddecl);
1690 : :
1691 : : /* If either the type of the new decl or the type of the old decl is an
1692 : : error_mark_node, then that implies that we have already issued an
1693 : : error (earlier) for some bogus type specification, and in that case,
1694 : : it is rather pointless to harass the user with yet more error message
1695 : : about the same declaration, so just pretend the types match here. */
1696 : 328951000 : if (TREE_TYPE (newdecl) == error_mark_node
1697 : 328951000 : || TREE_TYPE (olddecl) == error_mark_node)
1698 : : return error_mark_node;
1699 : :
1700 : : /* Check for redeclaration and other discrepancies. */
1701 : 328950977 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1702 : 328950977 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1703 : : {
1704 : 6237725 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1705 : : {
1706 : : /* Avoid warnings redeclaring built-ins which have not been
1707 : : explicitly declared. */
1708 : 183075 : if (was_hidden)
1709 : : {
1710 : 183047 : if (TREE_PUBLIC (newdecl)
1711 : 183047 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1712 : 12 : warning_at (newdecl_loc,
1713 : 12 : OPT_Wbuiltin_declaration_mismatch,
1714 : : "built-in function %qD declared as non-function",
1715 : : newdecl);
1716 : 183047 : return NULL_TREE;
1717 : : }
1718 : :
1719 : : /* If you declare a built-in or predefined function name as static,
1720 : : the old definition is overridden, but optionally warn this was a
1721 : : bad choice of name. */
1722 : 28 : if (! TREE_PUBLIC (newdecl))
1723 : : {
1724 : 28 : warning_at (newdecl_loc,
1725 : 28 : OPT_Wshadow,
1726 : 28 : fndecl_built_in_p (olddecl)
1727 : : ? G_("shadowing built-in function %q#D")
1728 : : : G_("shadowing library function %q#D"), olddecl);
1729 : : /* Discard the old built-in function. */
1730 : 28 : return NULL_TREE;
1731 : : }
1732 : : /* If the built-in is not ansi, then programs can override
1733 : : it even globally without an error. */
1734 : 0 : else if (! fndecl_built_in_p (olddecl))
1735 : 0 : warning_at (newdecl_loc, 0,
1736 : : "library function %q#D redeclared as non-function %q#D",
1737 : : olddecl, newdecl);
1738 : : else
1739 : 0 : error_at (newdecl_loc,
1740 : : "declaration of %q#D conflicts with built-in "
1741 : : "declaration %q#D", newdecl, olddecl);
1742 : 0 : return NULL_TREE;
1743 : : }
1744 : 6054650 : else if (!types_match)
1745 : : {
1746 : : /* Avoid warnings redeclaring built-ins which have not been
1747 : : explicitly declared. */
1748 : 2138729 : if (was_hidden)
1749 : : {
1750 : 814714 : tree t1, t2;
1751 : :
1752 : : /* A new declaration doesn't match a built-in one unless it
1753 : : is also extern "C". */
1754 : 814714 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1755 : 814714 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1756 : 814714 : if (!DECL_EXTERN_C_P (newdecl))
1757 : : return NULL_TREE;
1758 : :
1759 : 140394 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1760 : 140394 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1761 : 240932 : t1 || t2;
1762 : 100538 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1763 : : {
1764 : 240914 : if (!t1 || !t2)
1765 : : break;
1766 : : /* FILE, tm types are not known at the time
1767 : : we create the builtins. */
1768 : 748612 : for (unsigned i = 0;
1769 : 989364 : i < sizeof (builtin_structptr_types)
1770 : : / sizeof (builtin_structptr_type);
1771 : : ++i)
1772 : 888820 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1773 : : {
1774 : 140208 : tree t = TREE_VALUE (t1);
1775 : :
1776 : 140208 : if (TYPE_PTR_P (t)
1777 : 280416 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1778 : 140208 : == get_identifier (builtin_structptr_types[i].str)
1779 : 280401 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1780 : : {
1781 : 140193 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1782 : :
1783 : 140193 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1784 : 140193 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1785 : 140193 : types_match = decls_match (newdecl, olddecl);
1786 : 140193 : if (types_match)
1787 : 140193 : return duplicate_decls (newdecl, olddecl,
1788 : 140193 : hiding, was_hidden);
1789 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1790 : : }
1791 : 15 : goto next_arg;
1792 : : }
1793 : :
1794 : 100544 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1795 : : break;
1796 : 100538 : next_arg:;
1797 : : }
1798 : :
1799 : 201 : warning_at (newdecl_loc,
1800 : 201 : OPT_Wbuiltin_declaration_mismatch,
1801 : : "declaration of %q#D conflicts with built-in "
1802 : : "declaration %q#D", newdecl, olddecl);
1803 : : }
1804 : 1324015 : else if ((DECL_EXTERN_C_P (newdecl)
1805 : 15 : && DECL_EXTERN_C_P (olddecl))
1806 : 2648015 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1807 : 1324000 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1808 : : {
1809 : : /* Don't really override olddecl for __* prefixed builtins
1810 : : except for __[^b]*_chk, the compiler might be using those
1811 : : explicitly. */
1812 : 15 : if (fndecl_built_in_p (olddecl))
1813 : : {
1814 : 15 : tree id = DECL_NAME (olddecl);
1815 : 15 : const char *name = IDENTIFIER_POINTER (id);
1816 : 15 : size_t len;
1817 : :
1818 : 15 : if (name[0] == '_'
1819 : 15 : && name[1] == '_'
1820 : 30 : && (startswith (name + 2, "builtin_")
1821 : 6 : || (len = strlen (name)) <= strlen ("___chk")
1822 : 6 : || memcmp (name + len - strlen ("_chk"),
1823 : : "_chk", strlen ("_chk") + 1) != 0))
1824 : : {
1825 : 15 : if (DECL_INITIAL (newdecl))
1826 : : {
1827 : 6 : error_at (newdecl_loc,
1828 : : "definition of %q#D ambiguates built-in "
1829 : : "declaration %q#D", newdecl, olddecl);
1830 : 6 : return error_mark_node;
1831 : : }
1832 : 9 : auto_diagnostic_group d;
1833 : 9 : if (permerror (newdecl_loc,
1834 : : "new declaration %q#D ambiguates built-in"
1835 : : " declaration %q#D", newdecl, olddecl)
1836 : 9 : && flag_permissive)
1837 : 3 : inform (newdecl_loc,
1838 : : "ignoring the %q#D declaration", newdecl);
1839 : 9 : return flag_permissive ? olddecl : error_mark_node;
1840 : 9 : }
1841 : : }
1842 : :
1843 : : /* A near match; override the builtin. */
1844 : :
1845 : 0 : if (TREE_PUBLIC (newdecl))
1846 : 0 : warning_at (newdecl_loc,
1847 : 0 : OPT_Wbuiltin_declaration_mismatch,
1848 : : "new declaration %q#D ambiguates built-in "
1849 : : "declaration %q#D", newdecl, olddecl);
1850 : : else
1851 : 0 : warning (OPT_Wshadow,
1852 : : fndecl_built_in_p (olddecl)
1853 : : ? G_("shadowing built-in function %q#D")
1854 : : : G_("shadowing library function %q#D"), olddecl);
1855 : : }
1856 : : else
1857 : : /* Discard the old built-in function. */
1858 : : return NULL_TREE;
1859 : :
1860 : : /* Replace the old RTL to avoid problems with inlining. */
1861 : 201 : COPY_DECL_RTL (newdecl, olddecl);
1862 : : }
1863 : : else
1864 : : {
1865 : : /* Even if the types match, prefer the new declarations type
1866 : : for built-ins which have not been explicitly declared,
1867 : : for exception lists, etc... */
1868 : 3915921 : tree type = TREE_TYPE (newdecl);
1869 : 3915921 : tree attribs = (*targetm.merge_type_attributes)
1870 : 3915921 : (TREE_TYPE (olddecl), type);
1871 : :
1872 : 3915921 : type = cp_build_type_attribute_variant (type, attribs);
1873 : 3915921 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1874 : : }
1875 : :
1876 : : /* If a function is explicitly declared "throw ()", propagate that to
1877 : : the corresponding builtin. */
1878 : 3916122 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1879 : 3761871 : && was_hidden
1880 : 3761828 : && TREE_NOTHROW (newdecl)
1881 : 7424686 : && !TREE_NOTHROW (olddecl))
1882 : : {
1883 : 28588 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1884 : 28588 : tree tmpdecl = builtin_decl_explicit (fncode);
1885 : 28588 : if (tmpdecl && tmpdecl != olddecl && types_match)
1886 : 28588 : TREE_NOTHROW (tmpdecl) = 1;
1887 : : }
1888 : :
1889 : : /* Whether or not the builtin can throw exceptions has no
1890 : : bearing on this declarator. */
1891 : 3916122 : TREE_NOTHROW (olddecl) = 0;
1892 : :
1893 : 3916122 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1894 : : {
1895 : : /* If a builtin function is redeclared as `static', merge
1896 : : the declarations, but make the original one static. */
1897 : 3 : DECL_THIS_STATIC (olddecl) = 1;
1898 : 3 : TREE_PUBLIC (olddecl) = 0;
1899 : :
1900 : : /* Make the old declaration consistent with the new one so
1901 : : that all remnants of the builtin-ness of this function
1902 : : will be banished. */
1903 : 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1904 : 3 : COPY_DECL_RTL (newdecl, olddecl);
1905 : : }
1906 : : }
1907 : 322713252 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1908 : : {
1909 : : /* C++ Standard, 3.3, clause 4:
1910 : : "[Note: a namespace name or a class template name must be unique
1911 : : in its declarative region (7.3.2, clause 14). ]" */
1912 : 80073729 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
1913 : 80073708 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1914 : : /* Namespace conflicts with not namespace. */;
1915 : 46159628 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
1916 : 126233324 : || DECL_TYPE_TEMPLATE_P (newdecl))
1917 : : /* Class template conflicts. */;
1918 : 80073690 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1919 : 46159628 : && DECL_TEMPLATE_RESULT (olddecl)
1920 : 46159628 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1921 : 126233312 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
1922 : 33871950 : && DECL_TEMPLATE_RESULT (newdecl)
1923 : 33871950 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1924 : : /* Variable template conflicts. */;
1925 : 80073681 : else if (concept_definition_p (olddecl)
1926 : 160147356 : || concept_definition_p (newdecl))
1927 : : /* Concept conflicts. */;
1928 : 80073669 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1929 : 46199106 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
1930 : 80113159 : || (TREE_CODE (olddecl) == FUNCTION_DECL
1931 : 33871957 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1932 : : {
1933 : : /* One is a function and the other is a template
1934 : : function. */
1935 : 80031542 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1936 : : return NULL_TREE;
1937 : :
1938 : : /* There can only be one! */
1939 : 58891 : auto_diagnostic_group d;
1940 : 58891 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
1941 : 58891 : && check_raw_literal_operator (olddecl))
1942 : 3 : error_at (newdecl_loc,
1943 : : "literal operator %q#D conflicts with"
1944 : : " raw literal operator", newdecl);
1945 : 58888 : else if (check_raw_literal_operator (newdecl))
1946 : 3 : error_at (newdecl_loc,
1947 : : "raw literal operator %q#D conflicts with"
1948 : : " literal operator template", newdecl);
1949 : : else
1950 : : return NULL_TREE;
1951 : :
1952 : 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1953 : 6 : return error_mark_node;
1954 : 58891 : }
1955 : 42127 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
1956 : : /* A structured binding must be unique in its declarative region. */;
1957 : 39628 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1958 : 42124 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1959 : : /* One is an implicit typedef, that's ok. */
1960 : : return NULL_TREE;
1961 : :
1962 : 147 : auto_diagnostic_group d;
1963 : 147 : error ("%q#D redeclared as different kind of entity", newdecl);
1964 : 147 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1965 : :
1966 : 147 : return error_mark_node;
1967 : 147 : }
1968 : 242639523 : else if (!types_match)
1969 : : {
1970 : 228531144 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1971 : : /* These are certainly not duplicate declarations; they're
1972 : : from different scopes. */
1973 : : return NULL_TREE;
1974 : :
1975 : 228531084 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1976 : : {
1977 : 194356725 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1978 : 194356725 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1979 : :
1980 : : /* The name of a class template may not be declared to refer to
1981 : : any other template, class, function, object, namespace, value,
1982 : : or type in the same scope. */
1983 : 194356725 : if (TREE_CODE (oldres) == TYPE_DECL
1984 : 194356719 : || TREE_CODE (newres) == TYPE_DECL)
1985 : : {
1986 : 9 : auto_diagnostic_group d;
1987 : 9 : error_at (newdecl_loc,
1988 : : "conflicting declaration of template %q#D", newdecl);
1989 : 9 : inform (olddecl_loc,
1990 : : "previous declaration %q#D", olddecl);
1991 : 9 : return error_mark_node;
1992 : 9 : }
1993 : :
1994 : 194356716 : else if (TREE_CODE (oldres) == FUNCTION_DECL
1995 : 194356707 : && TREE_CODE (newres) == FUNCTION_DECL)
1996 : : {
1997 : 194356707 : if (duplicate_function_template_decls (newdecl, olddecl))
1998 : 0 : return error_mark_node;
1999 : : return NULL_TREE;
2000 : : }
2001 : : return NULL_TREE;
2002 : : }
2003 : 34174359 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2004 : : {
2005 : 34174248 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2006 : : {
2007 : 57 : auto_diagnostic_group d;
2008 : 57 : error_at (newdecl_loc,
2009 : : "conflicting declaration of C function %q#D",
2010 : : newdecl);
2011 : 57 : inform (olddecl_loc,
2012 : : "previous declaration %q#D", olddecl);
2013 : 57 : return error_mark_node;
2014 : 57 : }
2015 : : /* For function versions, params and types match, but they
2016 : : are not ambiguous. */
2017 : 34174191 : else if ((!DECL_FUNCTION_VERSIONED (newdecl)
2018 : 34166163 : && !DECL_FUNCTION_VERSIONED (olddecl))
2019 : : /* Let constrained hidden friends coexist for now, we'll
2020 : : check satisfaction later. */
2021 : 34166118 : && !member_like_constrained_friend_p (newdecl)
2022 : 33961470 : && !member_like_constrained_friend_p (olddecl)
2023 : : // The functions have the same parameter types.
2024 : 33929528 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2025 : 33929528 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2026 : : // And the same constraints.
2027 : 34174254 : && equivalently_constrained (newdecl, olddecl))
2028 : : {
2029 : 63 : auto_diagnostic_group d;
2030 : 63 : error_at (newdecl_loc,
2031 : : "ambiguating new declaration of %q#D", newdecl);
2032 : 63 : inform (olddecl_loc,
2033 : : "old declaration %q#D", olddecl);
2034 : 63 : return error_mark_node;
2035 : 63 : }
2036 : : else
2037 : 34174128 : return NULL_TREE;
2038 : : }
2039 : : else
2040 : : {
2041 : 111 : auto_diagnostic_group d;
2042 : 111 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2043 : 111 : inform (olddecl_loc,
2044 : : "previous declaration as %q#D", olddecl);
2045 : 111 : return error_mark_node;
2046 : 111 : }
2047 : : }
2048 : 14108379 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2049 : 14108379 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2050 : : {
2051 : : /* OMP UDRs are never duplicates. */
2052 : 27 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2053 : 27 : auto_diagnostic_group d;
2054 : 27 : error_at (newdecl_loc,
2055 : : "redeclaration of %<pragma omp declare reduction%>");
2056 : 27 : inform (olddecl_loc,
2057 : : "previous %<pragma omp declare reduction%> declaration");
2058 : 27 : return error_mark_node;
2059 : 27 : }
2060 : 14108352 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2061 : 14108352 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2062 : 220 : && (!DECL_TEMPLATE_INFO (newdecl)
2063 : 220 : || (DECL_TI_TEMPLATE (newdecl)
2064 : 220 : != DECL_TI_TEMPLATE (olddecl))))
2065 : 10161805 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2066 : 566023 : && (!DECL_TEMPLATE_INFO (olddecl)
2067 : 566023 : || (DECL_TI_TEMPLATE (olddecl)
2068 : 566023 : != DECL_TI_TEMPLATE (newdecl))))))
2069 : : /* It's OK to have a template specialization and a non-template
2070 : : with the same type, or to have specializations of two
2071 : : different templates with the same type. Note that if one is a
2072 : : specialization, and the other is an instantiation of the same
2073 : : template, that we do not exit at this point. That situation
2074 : : can occur if we instantiate a template class, and then
2075 : : specialize one of its methods. This situation is valid, but
2076 : : the declarations must be merged in the usual way. */
2077 : : return NULL_TREE;
2078 : 14108352 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2079 : 14108352 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2080 : 565863 : && !DECL_USE_TEMPLATE (newdecl))
2081 : 10161805 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2082 : 60 : && !DECL_USE_TEMPLATE (olddecl))))
2083 : : /* One of the declarations is a template instantiation, and the
2084 : : other is not a template at all. That's OK. */
2085 : : return NULL_TREE;
2086 : 14108352 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2087 : : {
2088 : : /* In [namespace.alias] we have:
2089 : :
2090 : : In a declarative region, a namespace-alias-definition can be
2091 : : used to redefine a namespace-alias declared in that declarative
2092 : : region to refer only to the namespace to which it already
2093 : : refers.
2094 : :
2095 : : Therefore, if we encounter a second alias directive for the same
2096 : : alias, we can just ignore the second directive. */
2097 : 21 : if (DECL_NAMESPACE_ALIAS (newdecl)
2098 : 21 : && (DECL_NAMESPACE_ALIAS (newdecl)
2099 : 18 : == DECL_NAMESPACE_ALIAS (olddecl)))
2100 : : return olddecl;
2101 : :
2102 : : /* Leave it to update_binding to merge or report error. */
2103 : : return NULL_TREE;
2104 : : }
2105 : : else
2106 : : {
2107 : 14108331 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2108 : 14108331 : if (errmsg)
2109 : : {
2110 : 550 : auto_diagnostic_group d;
2111 : 550 : error_at (newdecl_loc, errmsg, newdecl);
2112 : 550 : if (DECL_NAME (olddecl) != NULL_TREE)
2113 : 550 : inform (olddecl_loc,
2114 : 550 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2115 : : ? G_("%q#D previously defined here")
2116 : : : G_("%q#D previously declared here"), olddecl);
2117 : 550 : if (cxx_dialect >= cxx26
2118 : 147 : && DECL_NAME (newdecl)
2119 : 147 : && id_equal (DECL_NAME (newdecl), "_")
2120 : 563 : && !name_independent_decl_p (newdecl))
2121 : : {
2122 : 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2123 : 2 : inform (newdecl_loc,
2124 : : "parameter declaration is not name-independent");
2125 : 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2126 : : {
2127 : 3 : if (at_namespace_scope_p ())
2128 : 2 : inform (newdecl_loc,
2129 : : "structured binding at namespace scope is not "
2130 : : "name-independent");
2131 : 1 : else if (TREE_STATIC (newdecl))
2132 : 1 : inform (newdecl_loc,
2133 : : "static structured binding is not "
2134 : : "name-independent");
2135 : 0 : else if (DECL_EXTERNAL (newdecl))
2136 : 0 : inform (newdecl_loc,
2137 : : "extern structured binding is not "
2138 : : "name-independent");
2139 : : }
2140 : 8 : else if (at_class_scope_p ()
2141 : 0 : && VAR_P (newdecl)
2142 : 8 : && TREE_STATIC (newdecl))
2143 : 0 : inform (newdecl_loc,
2144 : : "static data member is not name-independent");
2145 : 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2146 : 3 : inform (newdecl_loc,
2147 : : "variable at namespace scope is not name-independent");
2148 : 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2149 : 3 : inform (newdecl_loc,
2150 : : "static variable is not name-independent");
2151 : 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2152 : 0 : inform (newdecl_loc,
2153 : : "extern variable is not name-independent");
2154 : : }
2155 : 550 : return error_mark_node;
2156 : 550 : }
2157 : 14107781 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2158 : 10161565 : && DECL_INITIAL (olddecl) != NULL_TREE
2159 : 11614 : && !prototype_p (TREE_TYPE (olddecl))
2160 : 14107781 : && prototype_p (TREE_TYPE (newdecl)))
2161 : : {
2162 : : /* Prototype decl follows defn w/o prototype. */
2163 : 0 : auto_diagnostic_group d;
2164 : 0 : if (warning_at (newdecl_loc, 0,
2165 : : "prototype specified for %q#D", newdecl))
2166 : 0 : inform (olddecl_loc,
2167 : : "previous non-prototype definition here");
2168 : 0 : }
2169 : 13722974 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2170 : 24269346 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2171 : : {
2172 : : /* [dcl.link]
2173 : : If two declarations of the same function or object
2174 : : specify different linkage-specifications ..., the program
2175 : : is ill-formed.... Except for functions with C++ linkage,
2176 : : a function declaration without a linkage specification
2177 : : shall not precede the first linkage specification for
2178 : : that function. A function can be declared without a
2179 : : linkage specification after an explicit linkage
2180 : : specification has been seen; the linkage explicitly
2181 : : specified in the earlier declaration is not affected by
2182 : : such a function declaration.
2183 : :
2184 : : DR 563 raises the question why the restrictions on
2185 : : functions should not also apply to objects. Older
2186 : : versions of G++ silently ignore the linkage-specification
2187 : : for this example:
2188 : :
2189 : : namespace N {
2190 : : extern int i;
2191 : : extern "C" int i;
2192 : : }
2193 : :
2194 : : which is clearly wrong. Therefore, we now treat objects
2195 : : like functions. */
2196 : 1748 : if (current_lang_depth () == 0)
2197 : : {
2198 : : /* There is no explicit linkage-specification, so we use
2199 : : the linkage from the previous declaration. */
2200 : 1742 : retrofit_lang_decl (newdecl);
2201 : 1742 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2202 : : }
2203 : : else
2204 : : {
2205 : 6 : auto_diagnostic_group d;
2206 : 6 : error_at (newdecl_loc,
2207 : : "conflicting declaration of %q#D with %qL linkage",
2208 : 6 : newdecl, DECL_LANGUAGE (newdecl));
2209 : 6 : inform (olddecl_loc,
2210 : : "previous declaration with %qL linkage",
2211 : 6 : DECL_LANGUAGE (olddecl));
2212 : 6 : }
2213 : : }
2214 : :
2215 : 14107781 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2216 : : ;
2217 : 13520814 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2218 : : {
2219 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2220 : 11228731 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2221 : 9595491 : && (/* grokfndecl passes member function templates too
2222 : : as FUNCTION_DECLs. */
2223 : 8119897 : DECL_TEMPLATE_INFO (olddecl)
2224 : : /* C++11 8.3.6/6.
2225 : : Default arguments for a member function of a class
2226 : : template shall be specified on the initial declaration
2227 : : of the member function within the class template. */
2228 : 649765 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2229 : : {
2230 : 7549173 : check_redeclaration_no_default_args (newdecl);
2231 : :
2232 : 7549173 : if (DECL_TEMPLATE_INFO (olddecl)
2233 : 7549173 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2234 : : {
2235 : 2114431 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2236 : 2114431 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2237 : 2114431 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2238 : 2114431 : tree old_parms
2239 : 2114431 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2240 : 2114431 : merge_default_template_args (new_parms, old_parms,
2241 : : /*class_p=*/false);
2242 : : }
2243 : : }
2244 : : else
2245 : : {
2246 : 2046318 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2247 : 2046318 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2248 : 2046318 : int i = 1;
2249 : :
2250 : 5519732 : for (; t1 && t1 != void_list_node;
2251 : 3473414 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2252 : 3475113 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2253 : : {
2254 : 39 : if (simple_cst_equal (TREE_PURPOSE (t1),
2255 : 39 : TREE_PURPOSE (t2)) == 1)
2256 : : {
2257 : 12 : auto_diagnostic_group d;
2258 : 12 : if (permerror (newdecl_loc,
2259 : : "default argument given for parameter "
2260 : : "%d of %q#D", i, newdecl))
2261 : 12 : inform (olddecl_loc,
2262 : : "previous specification in %q#D here",
2263 : : olddecl);
2264 : 12 : }
2265 : : else
2266 : : {
2267 : 27 : auto_diagnostic_group d;
2268 : 27 : error_at (newdecl_loc,
2269 : : "default argument given for parameter %d "
2270 : : "of %q#D", i, newdecl);
2271 : 27 : inform (olddecl_loc,
2272 : : "previous specification in %q#D here",
2273 : : olddecl);
2274 : 27 : }
2275 : : }
2276 : :
2277 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2278 : : argument expression, that declaration... shall be the only
2279 : : declaration of the function or function template in the
2280 : : translation unit." */
2281 : 2046318 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2282 : : }
2283 : : }
2284 : : }
2285 : :
2286 : : /* Do not merge an implicit typedef with an explicit one. In:
2287 : :
2288 : : class A;
2289 : : ...
2290 : : typedef class A A __attribute__ ((foo));
2291 : :
2292 : : the attribute should apply only to the typedef. */
2293 : 18023903 : if (TREE_CODE (olddecl) == TYPE_DECL
2294 : 18023903 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2295 : 24607 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2296 : : return NULL_TREE;
2297 : :
2298 : 17993219 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2299 : : return NULL_TREE;
2300 : :
2301 : 17993207 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2302 : 30 : return error_mark_node;
2303 : :
2304 : 17993177 : if (modules_p ()
2305 : 40065 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2306 : 18016554 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2307 : : {
2308 : 23377 : if (!module_may_redeclare (olddecl, newdecl))
2309 : 24 : return error_mark_node;
2310 : :
2311 : 23353 : if (!hiding)
2312 : : {
2313 : : /* The old declaration should match the exportingness of the new
2314 : : declaration. But hidden friend declarations just keep the
2315 : : exportingness of the old declaration; see CWG2588. */
2316 : 20947 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2317 : 20947 : if (DECL_LANG_SPECIFIC (not_tmpl)
2318 : 20905 : && DECL_MODULE_ATTACH_P (not_tmpl)
2319 : : /* Typedefs are not entities and so are OK to be redeclared
2320 : : as exported: see [module.interface]/p6. */
2321 : 21160 : && TREE_CODE (olddecl) != TYPE_DECL)
2322 : : {
2323 : 201 : if (DECL_MODULE_EXPORT_P (newdecl)
2324 : 201 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2325 : : {
2326 : 15 : auto_diagnostic_group d;
2327 : 15 : error ("conflicting exporting for declaration %qD", newdecl);
2328 : 15 : inform (olddecl_loc,
2329 : : "previously declared here without exporting");
2330 : 15 : }
2331 : : }
2332 : 20746 : else if (DECL_MODULE_EXPORT_P (newdecl))
2333 : 8650 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2334 : : }
2335 : : }
2336 : :
2337 : : /* We have committed to returning OLDDECL at this point. */
2338 : :
2339 : : /* If new decl is `static' and an `extern' was seen previously,
2340 : : warn about it. */
2341 : 17993153 : warn_extern_redeclared_static (newdecl, olddecl);
2342 : :
2343 : : /* True to merge attributes between the declarations, false to
2344 : : set OLDDECL's attributes to those of NEWDECL (for template
2345 : : explicit specializations that specify their own attributes
2346 : : independent of those specified for the primary template). */
2347 : 17993153 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2348 : 14077642 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2349 : 18559161 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2350 : :
2351 : 17993153 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2352 : : {
2353 : 14077642 : if (merge_attr)
2354 : : {
2355 : 13511839 : {
2356 : 13511839 : auto_diagnostic_group d;
2357 : 13511839 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2358 : 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2359 : : ? G_("previous definition of %qD here")
2360 : : : G_("previous declaration of %qD here"), olddecl);
2361 : 13511839 : }
2362 : :
2363 : : /* [dcl.attr.noreturn]: The first declaration of a function shall
2364 : : specify the noreturn attribute if any declaration of that function
2365 : : specifies the noreturn attribute. */
2366 : 13511839 : tree a;
2367 : 13511839 : if (TREE_THIS_VOLATILE (newdecl)
2368 : 109854 : && !TREE_THIS_VOLATILE (olddecl)
2369 : : /* This applies to [[noreturn]] only, not its GNU variants. */
2370 : 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2371 : 54 : && cxx11_attribute_p (a)
2372 : 13511860 : && get_attribute_namespace (a) == NULL_TREE)
2373 : : {
2374 : 9 : auto_diagnostic_group d;
2375 : 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2376 : : "but its first declaration was not", newdecl);
2377 : 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2378 : 9 : }
2379 : : }
2380 : :
2381 : : /* Now that functions must hold information normally held
2382 : : by field decls, there is extra work to do so that
2383 : : declaration information does not get destroyed during
2384 : : definition. */
2385 : 14077642 : if (DECL_VINDEX (olddecl))
2386 : 160729 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2387 : 14077642 : if (DECL_CONTEXT (olddecl))
2388 : 14077642 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2389 : 14077642 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2390 : 14077642 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2391 : 14077642 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2392 : 14077642 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2393 : 14077642 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2394 : 14077642 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2395 : 14077642 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2396 : 14077642 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2397 : 14077642 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2398 : 14077642 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2399 : 14077642 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2400 : 2473663 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2401 : 4947326 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2402 : 14077642 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2403 : :
2404 : 14077642 : duplicate_contracts (newdecl, olddecl);
2405 : :
2406 : : /* Optionally warn about more than one declaration for the same
2407 : : name, but don't warn about a function declaration followed by a
2408 : : definition. */
2409 : 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2410 : 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2411 : : /* Don't warn about extern decl followed by definition. */
2412 : 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2413 : : /* Don't warn if at least one is/was hidden. */
2414 : 27 : && !(hiding || was_hidden)
2415 : : /* Don't warn about declaration followed by specialization. */
2416 : 14077654 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2417 : 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2418 : : {
2419 : 6 : auto_diagnostic_group d;
2420 : 6 : if (warning_at (newdecl_loc,
2421 : 6 : OPT_Wredundant_decls,
2422 : : "redundant redeclaration of %qD in same scope",
2423 : : newdecl))
2424 : 6 : inform (olddecl_loc,
2425 : : "previous declaration of %qD", olddecl);
2426 : 6 : }
2427 : :
2428 : : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2429 : : first declaration of the function or, for an explicit specialization
2430 : : of a function template, the first declaration of that
2431 : : specialization. */
2432 : 14077642 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2433 : 565863 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2434 : : {
2435 : 13511839 : if (DECL_DELETED_FN (newdecl))
2436 : : {
2437 : 21 : auto_diagnostic_group d;
2438 : 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2439 : : "is not first declaration", newdecl))
2440 : 21 : inform (olddecl_loc,
2441 : : "previous declaration of %qD", olddecl);
2442 : 21 : }
2443 : 13511839 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2444 : 13511839 : if (DECL_DELETED_FN (olddecl)
2445 : 6 : && DECL_INITIAL (olddecl)
2446 : 13511845 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2447 : 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2448 : : }
2449 : : }
2450 : :
2451 : : /* Deal with C++: must preserve virtual function table size. */
2452 : 17993153 : if (TREE_CODE (olddecl) == TYPE_DECL)
2453 : : {
2454 : 24604 : tree newtype = TREE_TYPE (newdecl);
2455 : 24604 : tree oldtype = TREE_TYPE (olddecl);
2456 : :
2457 : 24604 : if (newtype != error_mark_node && oldtype != error_mark_node
2458 : 49208 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2459 : 62640 : CLASSTYPE_FRIEND_CLASSES (newtype)
2460 : 20880 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2461 : :
2462 : 24604 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2463 : : }
2464 : :
2465 : : /* Copy all the DECL_... slots specified in the new decl except for
2466 : : any that we copy here from the old type. */
2467 : 17993153 : if (merge_attr)
2468 : 17427350 : DECL_ATTRIBUTES (newdecl)
2469 : 34854700 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2470 : : else
2471 : 565803 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2472 : :
2473 : 17993153 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2474 : : {
2475 : 3506100 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2476 : 3506100 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2477 : 3506100 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2478 : :
2479 : : /* The new decl should not already have gathered any
2480 : : specializations. */
2481 : 3506100 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2482 : :
2483 : : /* Make sure the contracts are equivalent. */
2484 : 3506100 : duplicate_contracts (newdecl, olddecl);
2485 : :
2486 : : /* Remove contracts from old_result so they aren't appended to
2487 : : old_result by the merge function. */
2488 : 3506100 : remove_contract_attributes (old_result);
2489 : :
2490 : 3506100 : DECL_ATTRIBUTES (old_result)
2491 : 3506100 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2492 : :
2493 : 3506100 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2494 : : {
2495 : 3506070 : if (DECL_SOURCE_LOCATION (newdecl)
2496 : 3506070 : != DECL_SOURCE_LOCATION (olddecl))
2497 : : {
2498 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2499 : : later declarations of a function template. */
2500 : 3483577 : check_redeclaration_no_default_args (newdecl);
2501 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2502 : : argument expression, that declaration... shall be the only
2503 : : declaration of the function or function template in the
2504 : : translation unit." */
2505 : 3483577 : check_no_redeclaration_friend_default_args
2506 : 3483577 : (old_result, new_result);
2507 : :
2508 : 3483577 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2509 : 3483577 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2510 : 3483577 : merge_default_template_args (new_parms, old_parms,
2511 : : /*class_p=*/false);
2512 : : }
2513 : 3506070 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2514 : 2869442 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2515 : :
2516 : 3506070 : check_default_args (newdecl);
2517 : :
2518 : 7012128 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2519 : 3506070 : && DECL_INITIAL (new_result))
2520 : : {
2521 : 24 : if (DECL_INITIAL (old_result))
2522 : 12 : DECL_UNINLINABLE (old_result) = 1;
2523 : : else
2524 : 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2525 : 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2526 : 72 : DECL_NOT_REALLY_EXTERN (old_result)
2527 : 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2528 : 48 : DECL_INTERFACE_KNOWN (old_result)
2529 : 24 : = DECL_INTERFACE_KNOWN (new_result);
2530 : 24 : DECL_DECLARED_INLINE_P (old_result)
2531 : 24 : = DECL_DECLARED_INLINE_P (new_result);
2532 : 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2533 : 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2534 : : }
2535 : : else
2536 : : {
2537 : 3506046 : DECL_DECLARED_INLINE_P (old_result)
2538 : 3506046 : |= DECL_DECLARED_INLINE_P (new_result);
2539 : 3506046 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2540 : 3506046 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2541 : 3506046 : check_redeclaration_exception_specification (newdecl, olddecl);
2542 : :
2543 : 3506046 : merge_attribute_bits (new_result, old_result);
2544 : : }
2545 : : }
2546 : :
2547 : : /* Propagate purviewness and importingness as with
2548 : : set_instantiating_module, unless newdecl is a friend injection. */
2549 : 3506100 : if (modules_p () && DECL_LANG_SPECIFIC (new_result)
2550 : 3517718 : && !(TREE_CODE (new_result) == FUNCTION_DECL
2551 : 11612 : && DECL_UNIQUE_FRIEND_P (new_result)))
2552 : : {
2553 : 9818 : if (DECL_MODULE_PURVIEW_P (new_result))
2554 : 4779 : DECL_MODULE_PURVIEW_P (old_result) = true;
2555 : 9818 : if (!DECL_MODULE_IMPORT_P (new_result))
2556 : 9818 : DECL_MODULE_IMPORT_P (old_result) = false;
2557 : : }
2558 : :
2559 : : /* If the new declaration is a definition, update the file and
2560 : : line information on the declaration, and also make
2561 : : the old declaration the same definition. */
2562 : 3506100 : if (DECL_INITIAL (new_result) != NULL_TREE)
2563 : : {
2564 : 5322832 : DECL_SOURCE_LOCATION (olddecl)
2565 : 5322832 : = DECL_SOURCE_LOCATION (old_result)
2566 : 2661416 : = DECL_SOURCE_LOCATION (newdecl);
2567 : 2661416 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2568 : 2661416 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2569 : : {
2570 : 2661416 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2571 : 9478967 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2572 : 6817551 : DECL_CONTEXT (p) = old_result;
2573 : :
2574 : 5322832 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2575 : 14 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2576 : : }
2577 : : }
2578 : :
2579 : 3506100 : return olddecl;
2580 : : }
2581 : :
2582 : 14487053 : if (types_match)
2583 : : {
2584 : 14486852 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2585 : 14077441 : check_redeclaration_exception_specification (newdecl, olddecl);
2586 : :
2587 : : /* Automatically handles default parameters. */
2588 : 14486852 : tree oldtype = TREE_TYPE (olddecl);
2589 : 14486852 : tree newtype;
2590 : :
2591 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2592 : : at newdecl, which will be ggc_freed. */
2593 : 14486852 : if (TREE_CODE (newdecl) == TYPE_DECL)
2594 : : {
2595 : : /* But NEWTYPE might have an attribute, honor that. */
2596 : 24604 : tree tem = TREE_TYPE (newdecl);
2597 : 24604 : newtype = oldtype;
2598 : :
2599 : 24604 : if (TYPE_USER_ALIGN (tem))
2600 : : {
2601 : 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2602 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2603 : 21 : TYPE_USER_ALIGN (newtype) = true;
2604 : : }
2605 : :
2606 : : /* And remove the new type from the variants list. */
2607 : 24604 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2608 : : {
2609 : 24 : tree remove = TREE_TYPE (newdecl);
2610 : 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2611 : : {
2612 : 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2613 : : /* If remove is the main variant, no need to remove that
2614 : : from the list. One of the DECL_ORIGINAL_TYPE
2615 : : variants, e.g. created for aligned attribute, might still
2616 : : refer to the newdecl TYPE_DECL though, so remove that one
2617 : : in that case. */
2618 : 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2619 : 6 : if (orig != remove)
2620 : 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2621 : 0 : t = TYPE_MAIN_VARIANT (t))
2622 : 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2623 : : {
2624 : 12 : TYPE_NEXT_VARIANT (t)
2625 : 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2626 : 6 : break;
2627 : : }
2628 : : }
2629 : : else
2630 : 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2631 : 0 : t = TYPE_NEXT_VARIANT (t))
2632 : 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2633 : : {
2634 : 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2635 : 15 : break;
2636 : : }
2637 : : }
2638 : : }
2639 : 14462248 : else if (merge_attr)
2640 : 13896445 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2641 : : else
2642 : 565803 : newtype = TREE_TYPE (newdecl);
2643 : :
2644 : 14486852 : if (VAR_P (newdecl))
2645 : : {
2646 : 384807 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2647 : : /* For already initialized vars, TREE_READONLY could have been
2648 : : cleared in cp_finish_decl, because the var needs runtime
2649 : : initialization or destruction. Make sure not to set
2650 : : TREE_READONLY on it again. */
2651 : 384807 : if (DECL_INITIALIZED_P (olddecl)
2652 : 1213 : && !DECL_EXTERNAL (olddecl)
2653 : 384892 : && !TREE_READONLY (olddecl))
2654 : 30 : TREE_READONLY (newdecl) = 0;
2655 : 384807 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2656 : 769614 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2657 : 384807 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2658 : 384807 : if (DECL_DEPENDENT_INIT_P (olddecl))
2659 : 102718 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2660 : 769614 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2661 : 384807 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2662 : 384807 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2663 : 384807 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2664 : 769614 : DECL_DECLARED_CONSTINIT_P (newdecl)
2665 : 384807 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2666 : :
2667 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2668 : 384807 : if (DECL_LANG_SPECIFIC (olddecl)
2669 : 766021 : && CP_DECL_THREADPRIVATE_P (olddecl))
2670 : : {
2671 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2672 : 41 : retrofit_lang_decl (newdecl);
2673 : 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2674 : : }
2675 : : }
2676 : :
2677 : : /* An explicit specialization of a function template or of a member
2678 : : function of a class template can be declared transaction_safe
2679 : : independently of whether the corresponding template entity is declared
2680 : : transaction_safe. */
2681 : 1123 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2682 : 1021 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2683 : 50 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2684 : 50 : && tx_safe_fn_type_p (newtype)
2685 : 14486852 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2686 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2687 : :
2688 : 14486852 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2689 : :
2690 : 14486852 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2691 : 14077441 : check_default_args (newdecl);
2692 : :
2693 : : /* Lay the type out, unless already done. */
2694 : 14486852 : if (! same_type_p (newtype, oldtype)
2695 : 366 : && TREE_TYPE (newdecl) != error_mark_node
2696 : 14487218 : && !(processing_template_decl && uses_template_parms (newdecl)))
2697 : 285 : layout_type (TREE_TYPE (newdecl));
2698 : :
2699 : 14486852 : if ((VAR_P (newdecl)
2700 : 14102045 : || TREE_CODE (newdecl) == PARM_DECL
2701 : 14102045 : || TREE_CODE (newdecl) == RESULT_DECL
2702 : 14102045 : || TREE_CODE (newdecl) == FIELD_DECL
2703 : 14102045 : || TREE_CODE (newdecl) == TYPE_DECL)
2704 : 14511456 : && !(processing_template_decl && uses_template_parms (newdecl)))
2705 : 251349 : layout_decl (newdecl, 0);
2706 : :
2707 : : /* Merge deprecatedness. */
2708 : 14486852 : if (TREE_DEPRECATED (newdecl))
2709 : 11633 : TREE_DEPRECATED (olddecl) = 1;
2710 : :
2711 : : /* Merge unavailability. */
2712 : 14486852 : if (TREE_UNAVAILABLE (newdecl))
2713 : 3 : TREE_UNAVAILABLE (olddecl) = 1;
2714 : :
2715 : : /* Preserve function specific target and optimization options */
2716 : 14486852 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2717 : : {
2718 : 14077441 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2719 : 14077441 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2720 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2721 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2722 : :
2723 : 14077441 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2724 : 14077441 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2725 : 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2726 : 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2727 : :
2728 : 14077441 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2729 : 13910538 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2730 : : }
2731 : : else
2732 : : {
2733 : : /* Merge the const type qualifier. */
2734 : 409411 : if (TREE_READONLY (newdecl))
2735 : 41735 : TREE_READONLY (olddecl) = 1;
2736 : : /* Merge the volatile type qualifier. */
2737 : 409411 : if (TREE_THIS_VOLATILE (newdecl))
2738 : 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2739 : : }
2740 : :
2741 : : /* Merge the initialization information. */
2742 : 14486852 : if (DECL_INITIAL (newdecl) == NULL_TREE
2743 : 14486852 : && DECL_INITIAL (olddecl) != NULL_TREE)
2744 : : {
2745 : 171763 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2746 : 171763 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2747 : 171763 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2748 : : {
2749 : 11530 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2750 : 11530 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2751 : : }
2752 : : }
2753 : :
2754 : 14486852 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2755 : : {
2756 : 14077441 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2757 : 14077441 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2758 : 14077441 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2759 : 14077441 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2760 : 52142 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2761 : 14077441 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2762 : 14077441 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2763 : 14077441 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2764 : 14077441 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2765 : :
2766 : 14077441 : if (merge_attr)
2767 : 13511638 : merge_attribute_bits (newdecl, olddecl);
2768 : : else
2769 : : {
2770 : : /* Merge the noreturn bit. */
2771 : 565803 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2772 : 565803 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2773 : 565803 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2774 : 565803 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2775 : 565803 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2776 : : }
2777 : : /* Keep the old RTL. */
2778 : 14077441 : COPY_DECL_RTL (olddecl, newdecl);
2779 : : }
2780 : 409411 : else if (VAR_P (newdecl)
2781 : 409411 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2782 : : {
2783 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2784 : : declaration was for an incomplete object and the new
2785 : : declaration is not since many attributes of the RTL will
2786 : : change. */
2787 : 384733 : COPY_DECL_RTL (olddecl, newdecl);
2788 : : }
2789 : : }
2790 : : /* If cannot merge, then use the new type and qualifiers,
2791 : : and don't preserve the old rtl. */
2792 : : else
2793 : : {
2794 : : /* Clean out any memory we had of the old declaration. */
2795 : 201 : tree oldstatic = value_member (olddecl, static_aggregates);
2796 : 201 : if (oldstatic)
2797 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2798 : :
2799 : 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2800 : 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2801 : 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2802 : 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2803 : 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2804 : : }
2805 : :
2806 : : /* Merge the storage class information. */
2807 : 14487053 : merge_weak (newdecl, olddecl);
2808 : :
2809 : 14487053 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2810 : 14487053 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2811 : 14487053 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2812 : 14487053 : if (! DECL_EXTERNAL (olddecl))
2813 : 29221 : DECL_EXTERNAL (newdecl) = 0;
2814 : 14487053 : if (! DECL_COMDAT (olddecl))
2815 : 5937920 : DECL_COMDAT (newdecl) = 0;
2816 : :
2817 : 14487053 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2818 : : {
2819 : 75 : if (!DECL_LOCAL_DECL_P (olddecl))
2820 : : /* This can happen if olddecl was brought in from the
2821 : : enclosing namespace via a using-decl. The new decl is
2822 : : then not a block-scope extern at all. */
2823 : 3 : DECL_LOCAL_DECL_P (newdecl) = false;
2824 : : else
2825 : : {
2826 : 72 : retrofit_lang_decl (newdecl);
2827 : 72 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2828 : 72 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2829 : 72 : if (alias != error_mark_node)
2830 : : {
2831 : 69 : DECL_ATTRIBUTES (alias)
2832 : 69 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2833 : 69 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2834 : 39 : merge_attribute_bits (newdecl, alias);
2835 : : }
2836 : : }
2837 : : }
2838 : :
2839 : 14487053 : new_template_info = NULL_TREE;
2840 : 14487053 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2841 : : {
2842 : 14458865 : bool new_redefines_gnu_inline = false;
2843 : :
2844 : 14458865 : if (new_defines_function
2845 : 14458865 : && ((DECL_INTERFACE_KNOWN (olddecl)
2846 : 1239 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2847 : 8866658 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2848 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2849 : : == FUNCTION_DECL))))
2850 : 2478 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2851 : :
2852 : : if (!new_redefines_gnu_inline)
2853 : : {
2854 : 14458787 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2855 : 14458787 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2856 : 14458787 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2857 : : }
2858 : :
2859 : 14458865 : if (TREE_CODE (newdecl) != TYPE_DECL)
2860 : : {
2861 : 14458813 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2862 : 14458813 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2863 : 14458813 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2864 : :
2865 : : /* If the OLDDECL is an instantiation and/or specialization,
2866 : : then the NEWDECL must be too. But, it may not yet be marked
2867 : : as such if the caller has created NEWDECL, but has not yet
2868 : : figured out that it is a redeclaration. */
2869 : 14458813 : if (!DECL_USE_TEMPLATE (newdecl))
2870 : 13892736 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2871 : :
2872 : 14458813 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2873 : 13892786 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2874 : 27785572 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2875 : : }
2876 : :
2877 : : /* Don't really know how much of the language-specific
2878 : : values we should copy from old to new. */
2879 : 14458865 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2880 : :
2881 : 14458865 : if (LANG_DECL_HAS_MIN (newdecl))
2882 : : {
2883 : 14458865 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2884 : 14458865 : if (DECL_TEMPLATE_INFO (newdecl))
2885 : : {
2886 : 577390 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
2887 : 577390 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2888 : 577390 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2889 : : /* Remember the presence of explicit specialization args. */
2890 : 1131612 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2891 : 1131612 : = TINFO_USED_TEMPLATE_ID (new_template_info);
2892 : : }
2893 : :
2894 : : /* We don't want to copy template info from a non-templated friend
2895 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2896 : 14458865 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2897 : : || !non_templated_friend_p (olddecl));
2898 : 14458865 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2899 : : }
2900 : :
2901 : 14458865 : if (DECL_DECLARES_FUNCTION_P (newdecl))
2902 : : {
2903 : : /* Only functions have these fields. */
2904 : 14077642 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2905 : 14077642 : DECL_BEFRIENDING_CLASSES (newdecl)
2906 : 14077642 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2907 : 14077642 : DECL_BEFRIENDING_CLASSES (olddecl));
2908 : : /* DECL_THUNKS is only valid for virtual functions,
2909 : : otherwise it is a DECL_FRIEND_CONTEXT. */
2910 : 14077642 : if (DECL_VIRTUAL_P (newdecl))
2911 : 640105 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2912 : 26875074 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2913 : 100108 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2914 : : }
2915 : 381223 : else if (VAR_P (newdecl))
2916 : : {
2917 : : /* Only variables have this field. */
2918 : 381171 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2919 : 194 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2920 : : }
2921 : : }
2922 : :
2923 : 14487053 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2924 : : {
2925 : 14077642 : tree parm;
2926 : :
2927 : : /* Merge parameter attributes. */
2928 : 14077642 : tree oldarg, newarg;
2929 : 14077642 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2930 : 39818158 : oldarg && newarg;
2931 : 25740516 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2932 : : {
2933 : 25740516 : DECL_ATTRIBUTES (newarg)
2934 : 25740516 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
2935 : 25740516 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2936 : : }
2937 : :
2938 : 14077642 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2939 : 14077642 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2940 : : {
2941 : : /* If newdecl is not a specialization, then it is not a
2942 : : template-related function at all. And that means that we
2943 : : should have exited above, returning 0. */
2944 : 565803 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2945 : :
2946 : 565803 : if (DECL_ODR_USED (olddecl))
2947 : : /* From [temp.expl.spec]:
2948 : :
2949 : : If a template, a member template or the member of a class
2950 : : template is explicitly specialized then that
2951 : : specialization shall be declared before the first use of
2952 : : that specialization that would cause an implicit
2953 : : instantiation to take place, in every translation unit in
2954 : : which such a use occurs. */
2955 : 0 : error ("explicit specialization of %qD after first use",
2956 : : olddecl);
2957 : :
2958 : 565803 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2959 : 565803 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2960 : 1131453 : && DECL_DECLARED_INLINE_P (newdecl));
2961 : :
2962 : : /* Don't propagate visibility from the template to the
2963 : : specialization here. We'll do that in determine_visibility if
2964 : : appropriate. */
2965 : 565803 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2966 : :
2967 : : /* [temp.expl.spec/14] We don't inline explicit specialization
2968 : : just because the primary template says so. */
2969 : 565803 : gcc_assert (!merge_attr);
2970 : :
2971 : 565803 : DECL_DECLARED_INLINE_P (olddecl)
2972 : 565803 : = DECL_DECLARED_INLINE_P (newdecl);
2973 : :
2974 : 565803 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
2975 : 565803 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2976 : :
2977 : 565803 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2978 : : }
2979 : 13511839 : else if (new_defines_function && DECL_INITIAL (olddecl))
2980 : : {
2981 : : /* Never inline re-defined extern inline functions.
2982 : : FIXME: this could be better handled by keeping both
2983 : : function as separate declarations. */
2984 : 78 : DECL_UNINLINABLE (newdecl) = 1;
2985 : : }
2986 : : else
2987 : : {
2988 : 13511761 : if (DECL_PENDING_INLINE_P (olddecl))
2989 : : {
2990 : 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
2991 : 6 : DECL_PENDING_INLINE_INFO (newdecl)
2992 : 12 : = DECL_PENDING_INLINE_INFO (olddecl);
2993 : : }
2994 : 13511755 : else if (DECL_PENDING_INLINE_P (newdecl))
2995 : : ;
2996 : 13511755 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2997 : 13511755 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2998 : 27023510 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2999 : :
3000 : 13511761 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3001 : :
3002 : 40535283 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3003 : 27023522 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3004 : :
3005 : 27023522 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3006 : 13511761 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3007 : 13511761 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3008 : 26633328 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3009 : : }
3010 : :
3011 : : /* Preserve abstractness on cloned [cd]tors. */
3012 : 14077642 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3013 : :
3014 : : /* Update newdecl's parms to point at olddecl. */
3015 : 45631869 : for (parm = DECL_ARGUMENTS (newdecl); parm;
3016 : 31554227 : parm = DECL_CHAIN (parm))
3017 : 31554227 : DECL_CONTEXT (parm) = olddecl;
3018 : :
3019 : 14077642 : if (! types_match)
3020 : : {
3021 : 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3022 : 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3023 : 201 : COPY_DECL_RTL (newdecl, olddecl);
3024 : : }
3025 : 14077642 : if (! types_match || new_defines_function)
3026 : : {
3027 : : /* These are the final DECL_ARGUMENTS that will be used within the
3028 : : body; update any references to old DECL_ARGUMENTS in the
3029 : : contracts, if present. */
3030 : 8868098 : if (tree contracts = DECL_CONTRACTS (newdecl))
3031 : 132 : remap_contracts (olddecl, newdecl, contracts, true);
3032 : :
3033 : : /* These need to be copied so that the names are available.
3034 : : Note that if the types do match, we'll preserve inline
3035 : : info and other bits, but if not, we won't. */
3036 : 8868098 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3037 : 8868098 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3038 : :
3039 : : /* In some cases, duplicate_contracts will remove contracts from
3040 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
3041 : : shared. If we removed them, re-add them. */
3042 : 8868098 : if (!DECL_CONTRACTS (olddecl))
3043 : 8868051 : copy_contract_attributes (olddecl, newdecl);
3044 : : }
3045 : : /* If redeclaring a builtin function, it stays built in
3046 : : if newdecl is a gnu_inline definition, or if newdecl is just
3047 : : a declaration. */
3048 : 14077642 : if (fndecl_built_in_p (olddecl)
3049 : 14134682 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3050 : : {
3051 : 3895176 : copy_decl_built_in_function (newdecl, olddecl);
3052 : : /* If we're keeping the built-in definition, keep the rtl,
3053 : : regardless of declaration matches. */
3054 : 3895176 : COPY_DECL_RTL (olddecl, newdecl);
3055 : 3895176 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3056 : : {
3057 : 3895170 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3058 : 3895170 : if (builtin_decl_explicit_p (fncode))
3059 : : {
3060 : : /* A compatible prototype of these builtin functions
3061 : : is seen, assume the runtime implements it with
3062 : : the expected semantics. */
3063 : 3895170 : switch (fncode)
3064 : : {
3065 : 10295 : case BUILT_IN_STPCPY:
3066 : 10295 : set_builtin_decl_implicit_p (fncode, true);
3067 : 10295 : break;
3068 : 3884875 : default:
3069 : 3884875 : set_builtin_decl_declared_p (fncode, true);
3070 : 3884875 : break;
3071 : : }
3072 : : }
3073 : :
3074 : 3895170 : copy_attributes_to_builtin (newdecl);
3075 : : }
3076 : : }
3077 : 14077642 : if (new_defines_function)
3078 : : /* If defining a function declared with other language
3079 : : linkage, use the previously declared language linkage. */
3080 : 8867897 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3081 : 5209745 : else if (types_match)
3082 : : {
3083 : 5209544 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3084 : : /* Don't clear out the arguments if we're just redeclaring a
3085 : : function. */
3086 : 5209544 : if (DECL_ARGUMENTS (olddecl))
3087 : : {
3088 : : /* If we removed contracts from previous definition, re-attach
3089 : : them. Otherwise, rewrite the contracts so they match the
3090 : : parameters of the new declaration. */
3091 : 1222339 : if (DECL_INITIAL (olddecl)
3092 : 11423 : && DECL_CONTRACTS (newdecl)
3093 : 1222350 : && !DECL_CONTRACTS (olddecl))
3094 : 10 : copy_contract_attributes (olddecl, newdecl);
3095 : : else
3096 : : {
3097 : : /* Temporarily undo the re-contexting of parameters so we can
3098 : : actually remap parameters. The inliner won't replace
3099 : : parameters if we don't do this. */
3100 : 1222329 : tree args = DECL_ARGUMENTS (newdecl);
3101 : 4575729 : for (tree p = args; p; p = DECL_CHAIN (p))
3102 : 3353400 : DECL_CONTEXT (p) = newdecl;
3103 : :
3104 : : /* Save new argument names for use in contracts parsing,
3105 : : unless we've already started parsing the body of olddecl
3106 : : (particular issues arise when newdecl is from a prior
3107 : : friend decl with no argument names, see
3108 : : modules/contracts-tpl-friend-1). */
3109 : 1222329 : if (tree contracts = DECL_CONTRACTS (olddecl))
3110 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3111 : :
3112 : : /* And reverse this operation again. */
3113 : 4575729 : for (tree p = args; p; p = DECL_CHAIN (p))
3114 : 3353400 : DECL_CONTEXT (p) = olddecl;
3115 : : }
3116 : :
3117 : 1222339 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3118 : : }
3119 : : }
3120 : : }
3121 : 409411 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3122 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3123 : :
3124 : : /* Now preserve various other info from the definition. */
3125 : 14487053 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3126 : 14487053 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3127 : 14487053 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3128 : 14487053 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3129 : :
3130 : : /* Warn about conflicting visibility specifications. */
3131 : 14487053 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3132 : 5164617 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3133 : 14487573 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3134 : : {
3135 : 12 : auto_diagnostic_group d;
3136 : 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3137 : : "%qD: visibility attribute ignored because it "
3138 : : "conflicts with previous declaration", newdecl))
3139 : 12 : inform (olddecl_loc,
3140 : : "previous declaration of %qD", olddecl);
3141 : 12 : }
3142 : : /* Choose the declaration which specified visibility. */
3143 : 14487053 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3144 : : {
3145 : 5164617 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3146 : 5164617 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3147 : : }
3148 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3149 : : so keep this behavior. */
3150 : 14487053 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3151 : : {
3152 : 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3153 : 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3154 : : }
3155 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3156 : 14487053 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3157 : : {
3158 : 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3159 : 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3160 : : }
3161 : 14486999 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3162 : 14486999 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3163 : 3 : DECL_USER_ALIGN (newdecl) = 1;
3164 : :
3165 : 14487053 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3166 : 28974106 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3167 : 14487053 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3168 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3169 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3170 : 14487053 : if (TREE_CODE (newdecl) == FIELD_DECL)
3171 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3172 : :
3173 : : /* Merge module entity mapping information. */
3174 : 14487053 : if (DECL_LANG_SPECIFIC (olddecl)
3175 : 28945961 : && (DECL_MODULE_ENTITY_P (olddecl)
3176 : 14458635 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3177 : : {
3178 : 273 : retrofit_lang_decl (newdecl);
3179 : 273 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3180 : 273 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3181 : : }
3182 : :
3183 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3184 : : with that from NEWDECL below. */
3185 : 14487053 : if (DECL_LANG_SPECIFIC (olddecl))
3186 : : {
3187 : 14458908 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3188 : : != DECL_LANG_SPECIFIC (newdecl));
3189 : 14458908 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3190 : : }
3191 : :
3192 : : /* Merge the USED information. */
3193 : 14487053 : if (TREE_USED (olddecl))
3194 : 478279 : TREE_USED (newdecl) = 1;
3195 : 14008774 : else if (TREE_USED (newdecl))
3196 : 54 : TREE_USED (olddecl) = 1;
3197 : :
3198 : 14487053 : if (VAR_P (newdecl))
3199 : : {
3200 : 384807 : if (DECL_READ_P (olddecl))
3201 : 34465 : DECL_READ_P (newdecl) = 1;
3202 : 350342 : else if (DECL_READ_P (newdecl))
3203 : 3 : DECL_READ_P (olddecl) = 1;
3204 : : }
3205 : :
3206 : 14487053 : if (DECL_PRESERVE_P (olddecl))
3207 : 48 : DECL_PRESERVE_P (newdecl) = 1;
3208 : 14487005 : else if (DECL_PRESERVE_P (newdecl))
3209 : 63 : DECL_PRESERVE_P (olddecl) = 1;
3210 : :
3211 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3212 : : to olddecl and deleted. */
3213 : 14487053 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3214 : 28564695 : && DECL_FUNCTION_VERSIONED (olddecl))
3215 : : {
3216 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3217 : 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3218 : : /* newdecl will be purged after copying to olddecl and is no longer
3219 : : a version. */
3220 : 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3221 : : }
3222 : :
3223 : 14487053 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3224 : : {
3225 : 14077642 : int function_size;
3226 : 14077642 : struct symtab_node *snode = symtab_node::get (olddecl);
3227 : :
3228 : 14077642 : function_size = sizeof (struct tree_decl_common);
3229 : :
3230 : 14077642 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3231 : : (char *) newdecl + sizeof (struct tree_common),
3232 : : function_size - sizeof (struct tree_common));
3233 : :
3234 : 14077642 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3235 : : (char *) newdecl + sizeof (struct tree_decl_common),
3236 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3237 : :
3238 : : /* Preserve symtab node mapping. */
3239 : 14077642 : olddecl->decl_with_vis.symtab_node = snode;
3240 : :
3241 : 14077642 : if (new_template_info)
3242 : : /* If newdecl is a template instantiation, it is possible that
3243 : : the following sequence of events has occurred:
3244 : :
3245 : : o A friend function was declared in a class template. The
3246 : : class template was instantiated.
3247 : :
3248 : : o The instantiation of the friend declaration was
3249 : : recorded on the instantiation list, and is newdecl.
3250 : :
3251 : : o Later, however, instantiate_class_template called pushdecl
3252 : : on the newdecl to perform name injection. But, pushdecl in
3253 : : turn called duplicate_decls when it discovered that another
3254 : : declaration of a global function with the same name already
3255 : : existed.
3256 : :
3257 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3258 : :
3259 : : If we're going to do that, we'd better make sure that
3260 : : olddecl, and not newdecl, is on the list of
3261 : : instantiations so that if we try to do the instantiation
3262 : : again we won't get the clobbered declaration. */
3263 : 577381 : reregister_specialization (newdecl,
3264 : : new_template_info,
3265 : : olddecl);
3266 : : }
3267 : : else
3268 : : {
3269 : 409411 : size_t size = tree_code_size (TREE_CODE (newdecl));
3270 : :
3271 : 409411 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3272 : : (char *) newdecl + sizeof (struct tree_common),
3273 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3274 : :
3275 : 409411 : switch (TREE_CODE (newdecl))
3276 : : {
3277 : 409411 : case LABEL_DECL:
3278 : 409411 : case VAR_DECL:
3279 : 409411 : case RESULT_DECL:
3280 : 409411 : case PARM_DECL:
3281 : 409411 : case FIELD_DECL:
3282 : 409411 : case TYPE_DECL:
3283 : 409411 : case CONST_DECL:
3284 : 409411 : {
3285 : 409411 : struct symtab_node *snode = NULL;
3286 : :
3287 : 409411 : if (VAR_P (newdecl)
3288 : 409411 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3289 : 3 : || DECL_EXTERNAL (olddecl)))
3290 : 384807 : snode = symtab_node::get (olddecl);
3291 : 409411 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3292 : : (char *) newdecl + sizeof (struct tree_decl_common),
3293 : : size - sizeof (struct tree_decl_common)
3294 : 409411 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3295 : 409411 : if (VAR_P (newdecl))
3296 : 384807 : olddecl->decl_with_vis.symtab_node = snode;
3297 : : }
3298 : : break;
3299 : 0 : default:
3300 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3301 : : (char *) newdecl + sizeof (struct tree_decl_common),
3302 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3303 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3304 : 0 : break;
3305 : : }
3306 : : }
3307 : :
3308 : 14487053 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3309 : : {
3310 : 14462449 : if (DECL_EXTERNAL (olddecl)
3311 : 386170 : || TREE_PUBLIC (olddecl)
3312 : 14462518 : || TREE_STATIC (olddecl))
3313 : : {
3314 : : /* Merge the section attribute.
3315 : : We want to issue an error if the sections conflict but that must be
3316 : : done later in decl_attributes since we are called before attributes
3317 : : are assigned. */
3318 : 14462449 : if (DECL_SECTION_NAME (newdecl) != NULL)
3319 : 7 : set_decl_section_name (olddecl, newdecl);
3320 : :
3321 : 14462449 : if (DECL_ONE_ONLY (newdecl))
3322 : : {
3323 : 0 : struct symtab_node *oldsym, *newsym;
3324 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3325 : 0 : oldsym = cgraph_node::get_create (olddecl);
3326 : : else
3327 : 0 : oldsym = varpool_node::get_create (olddecl);
3328 : 0 : newsym = symtab_node::get (newdecl);
3329 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3330 : : }
3331 : : }
3332 : :
3333 : 14462449 : if (VAR_P (newdecl)
3334 : 14462449 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3335 : : {
3336 : 398 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3337 : 398 : if (!processing_template_decl)
3338 : 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3339 : : }
3340 : : }
3341 : :
3342 : 14487053 : DECL_UID (olddecl) = olddecl_uid;
3343 : :
3344 : : /* NEWDECL contains the merged attribute lists.
3345 : : Update OLDDECL to be the same. */
3346 : 14487053 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3347 : :
3348 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3349 : : so that encode_section_info has a chance to look at the new decl
3350 : : flags and attributes. */
3351 : 14487053 : if (DECL_RTL_SET_P (olddecl)
3352 : 14487053 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3353 : 0 : || (VAR_P (olddecl)
3354 : 0 : && TREE_STATIC (olddecl))))
3355 : 0 : make_decl_rtl (olddecl);
3356 : :
3357 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3358 : : declaration of a member results in a call to duplicate_decls,
3359 : : freeing these nodes represents in a significant savings.
3360 : :
3361 : : Before releasing the node, be sore to remove function from symbol
3362 : : table that might have been inserted there to record comdat group.
3363 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3364 : : structure is shared in between newdecl and oldecl. */
3365 : 14487053 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3366 : 14077642 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3367 : 14487053 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3368 : : {
3369 : 14462449 : struct symtab_node *snode = symtab_node::get (newdecl);
3370 : 14462449 : if (snode)
3371 : 399 : snode->remove ();
3372 : : }
3373 : :
3374 : 14487053 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3375 : : {
3376 : 14077642 : tree clone;
3377 : 14583807 : FOR_EACH_CLONE (clone, olddecl)
3378 : : {
3379 : 506165 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3380 : 506165 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3381 : : }
3382 : : }
3383 : :
3384 : : /* Remove the associated constraints for newdecl, if any, before
3385 : : reclaiming memory. */
3386 : 14487053 : if (flag_concepts)
3387 : 3517023 : remove_constraints (newdecl);
3388 : :
3389 : : /* And similarly for any module tracking data. */
3390 : 14487053 : if (modules_p ())
3391 : 28423 : remove_defining_module (newdecl);
3392 : :
3393 : 14487053 : ggc_free (newdecl);
3394 : :
3395 : 14487053 : return olddecl;
3396 : : }
3397 : :
3398 : : /* Return zero if the declaration NEWDECL is valid
3399 : : when the declaration OLDDECL (assumed to be for the same name)
3400 : : has already been seen.
3401 : : Otherwise return an error message format string with a %s
3402 : : where the identifier should go. */
3403 : :
3404 : : static const char *
3405 : 14108331 : redeclaration_error_message (tree newdecl, tree olddecl)
3406 : : {
3407 : 14108367 : if (TREE_CODE (newdecl) == TYPE_DECL)
3408 : : {
3409 : : /* Because C++ can put things into name space for free,
3410 : : constructs like "typedef struct foo { ... } foo"
3411 : : would look like an erroneous redeclaration. */
3412 : 55324 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3413 : : return NULL;
3414 : : else
3415 : : return G_("redefinition of %q#D");
3416 : : }
3417 : 14053043 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3418 : : {
3419 : : /* If this is a pure function, its olddecl will actually be
3420 : : the original initialization to `0' (which we force to call
3421 : : abort()). Don't complain about redefinition in this case. */
3422 : 20323610 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3423 : 10161823 : && DECL_INITIAL (olddecl) == NULL_TREE)
3424 : : return NULL;
3425 : :
3426 : : /* If both functions come from different namespaces, this is not
3427 : : a redeclaration - this is a conflict with a used function. */
3428 : 20323580 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3429 : 1567326 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3430 : 10161793 : && ! decls_match (olddecl, newdecl))
3431 : : return G_("%qD conflicts with used function");
3432 : :
3433 : : /* We'll complain about linkage mismatches in
3434 : : warn_extern_redeclared_static. */
3435 : :
3436 : : /* Defining the same name twice is no good. */
3437 : 10161790 : if (decl_defined_p (olddecl)
3438 : 10161790 : && decl_defined_p (newdecl))
3439 : : {
3440 : 306 : if (DECL_NAME (olddecl) == NULL_TREE)
3441 : : return G_("%q#D not declared in class");
3442 : 447 : else if (!GNU_INLINE_P (olddecl)
3443 : 393 : || GNU_INLINE_P (newdecl))
3444 : 222 : return G_("redefinition of %q#D");
3445 : : }
3446 : :
3447 : 10843633 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3448 : : {
3449 : 636767 : bool olda = GNU_INLINE_P (olddecl);
3450 : 636767 : bool newa = GNU_INLINE_P (newdecl);
3451 : :
3452 : 636767 : if (olda != newa)
3453 : : {
3454 : 15 : if (newa)
3455 : : return G_("%q+D redeclared inline with "
3456 : : "%<gnu_inline%> attribute");
3457 : : else
3458 : : return G_("%q+D redeclared inline without "
3459 : : "%<gnu_inline%> attribute");
3460 : : }
3461 : : }
3462 : :
3463 : : /* [class.compare.default]: A definition of a comparison operator as
3464 : : defaulted that appears in a class shall be the first declaration of
3465 : : that function. */
3466 : 10161553 : special_function_kind sfk = special_function_p (olddecl);
3467 : 10161553 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3468 : : return G_("comparison operator %q+D defaulted after "
3469 : : "its first declaration");
3470 : :
3471 : 10161550 : check_abi_tag_redeclaration
3472 : 10161550 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3473 : 10161550 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3474 : :
3475 : 10161550 : return NULL;
3476 : : }
3477 : 3891238 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3478 : : {
3479 : 3506139 : tree nt, ot;
3480 : :
3481 : 3506139 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3482 : : return G_("redefinition of %q#D");
3483 : :
3484 : 3506133 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3485 : 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3486 : 36 : DECL_TEMPLATE_RESULT (olddecl));
3487 : :
3488 : 3506097 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3489 : : return NULL;
3490 : :
3491 : 3506085 : nt = DECL_TEMPLATE_RESULT (newdecl);
3492 : 3506085 : if (DECL_TEMPLATE_INFO (nt))
3493 : 523889 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3494 : 3506085 : ot = DECL_TEMPLATE_RESULT (olddecl);
3495 : 3506085 : if (DECL_TEMPLATE_INFO (ot))
3496 : 3506085 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3497 : 6167552 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3498 : 3506124 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3499 : 15 : return G_("redefinition of %q#D");
3500 : :
3501 : 4788714 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3502 : : {
3503 : 815464 : bool olda = GNU_INLINE_P (ot);
3504 : 815464 : bool newa = GNU_INLINE_P (nt);
3505 : :
3506 : 815464 : if (olda != newa)
3507 : : {
3508 : 0 : if (newa)
3509 : : return G_("%q+D redeclared inline with "
3510 : : "%<gnu_inline%> attribute");
3511 : : else
3512 : : return G_("%q+D redeclared inline without "
3513 : : "%<gnu_inline%> attribute");
3514 : : }
3515 : : }
3516 : :
3517 : : /* Core issue #226 (C++11):
3518 : :
3519 : : If a friend function template declaration specifies a
3520 : : default template-argument, that declaration shall be a
3521 : : definition and shall be the only declaration of the
3522 : : function template in the translation unit. */
3523 : 3506070 : if ((cxx_dialect != cxx98)
3524 : 3491001 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3525 : 3717594 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3526 : : /*is_primary=*/true,
3527 : : /*is_partial=*/false,
3528 : : /*is_friend_decl=*/2))
3529 : : return G_("redeclaration of friend %q#D "
3530 : : "may not have default template arguments");
3531 : :
3532 : 3506067 : return NULL;
3533 : : }
3534 : 385099 : else if (VAR_P (newdecl)
3535 : 385015 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3536 : 385015 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3537 : 385146 : && (! DECL_LANG_SPECIFIC (olddecl)
3538 : 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3539 : 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3540 : : {
3541 : : /* Only variables can be thread-local, and all declarations must
3542 : : agree on this property. */
3543 : 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3544 : : return G_("thread-local declaration of %q#D follows "
3545 : : "non-thread-local declaration");
3546 : : else
3547 : 3 : return G_("non-thread-local declaration of %q#D follows "
3548 : : "thread-local declaration");
3549 : : }
3550 : 385093 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3551 : : {
3552 : : /* The objects have been declared at namespace scope. If either
3553 : : is a member of an anonymous union, then this is an invalid
3554 : : redeclaration. For example:
3555 : :
3556 : : int i;
3557 : : union { int i; };
3558 : :
3559 : : is invalid. */
3560 : 384934 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3561 : 769937 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3562 : : return G_("redeclaration of %q#D");
3563 : : /* If at least one declaration is a reference, there is no
3564 : : conflict. For example:
3565 : :
3566 : : int i = 3;
3567 : : extern int i;
3568 : :
3569 : : is valid. */
3570 : 385000 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3571 : : return NULL;
3572 : :
3573 : : /* Static data member declared outside a class definition
3574 : : if the variable is defined within the class with constexpr
3575 : : specifier is declaration rather than definition (and
3576 : : deprecated). */
3577 : 2824 : if (cxx_dialect >= cxx17
3578 : 2749 : && VAR_P (olddecl)
3579 : 2693 : && DECL_CLASS_SCOPE_P (olddecl)
3580 : 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3581 : 5461 : && !DECL_INITIAL (newdecl))
3582 : : {
3583 : 2637 : DECL_EXTERNAL (newdecl) = 1;
3584 : : /* For now, only warn with explicit -Wdeprecated. */
3585 : 2637 : if (OPTION_SET_P (warn_deprecated))
3586 : : {
3587 : 40 : auto_diagnostic_group d;
3588 : 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3589 : : "redundant redeclaration of %<constexpr%> "
3590 : : "static data member %qD", newdecl))
3591 : 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3592 : : "previous declaration of %qD", olddecl);
3593 : 40 : }
3594 : 2637 : return NULL;
3595 : : }
3596 : :
3597 : : /* Reject two definitions. */
3598 : : return G_("redefinition of %q#D");
3599 : : }
3600 : : else
3601 : : {
3602 : : /* Objects declared with block scope: */
3603 : : /* Reject two definitions, and reject a definition
3604 : : together with an external reference. */
3605 : 78 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3606 : : return G_("redeclaration of %q#D");
3607 : : return NULL;
3608 : : }
3609 : : }
3610 : :
3611 : :
3612 : : /* Hash and equality functions for the named_label table. */
3613 : :
3614 : : hashval_t
3615 : 67488 : named_label_hash::hash (const value_type entry)
3616 : : {
3617 : 67488 : return IDENTIFIER_HASH_VALUE (entry->name);
3618 : : }
3619 : :
3620 : : bool
3621 : 63937 : named_label_hash::equal (const value_type entry, compare_type name)
3622 : : {
3623 : 63937 : return name == entry->name;
3624 : : }
3625 : :
3626 : : /* Look for a label named ID in the current function. If one cannot
3627 : : be found, create one. Return the named_label_entry, or NULL on
3628 : : failure. */
3629 : :
3630 : : static named_label_entry *
3631 : 22344 : lookup_label_1 (tree id, bool making_local_p)
3632 : : {
3633 : 22344 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3634 : :
3635 : : /* You can't use labels at global scope. */
3636 : 22344 : if (current_function_decl == NULL_TREE)
3637 : : {
3638 : 3 : error ("label %qE referenced outside of any function", id);
3639 : 3 : return NULL;
3640 : : }
3641 : :
3642 : 22341 : if (!named_labels)
3643 : 4503 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3644 : :
3645 : 22341 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3646 : 22341 : named_label_entry **slot
3647 : 22341 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3648 : 22341 : named_label_entry *old = *slot;
3649 : :
3650 : 22341 : if (old && old->label_decl)
3651 : : {
3652 : 2615 : if (!making_local_p)
3653 : : return old;
3654 : :
3655 : 9 : if (old->binding_level == current_binding_level)
3656 : : {
3657 : 0 : auto_diagnostic_group d;
3658 : 0 : error ("local label %qE conflicts with existing label", id);
3659 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3660 : 0 : return NULL;
3661 : 0 : }
3662 : : }
3663 : :
3664 : : /* We are making a new decl, create or reuse the named_label_entry */
3665 : 19744 : named_label_entry *ent = NULL;
3666 : 21 : if (old && !old->label_decl)
3667 : : ent = old;
3668 : : else
3669 : : {
3670 : 19723 : ent = ggc_cleared_alloc<named_label_entry> ();
3671 : 19723 : ent->name = id;
3672 : 19723 : ent->outer = old;
3673 : 19723 : *slot = ent;
3674 : : }
3675 : :
3676 : : /* Now create the LABEL_DECL. */
3677 : 19735 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3678 : :
3679 : 19735 : DECL_CONTEXT (decl) = current_function_decl;
3680 : 19735 : SET_DECL_MODE (decl, VOIDmode);
3681 : 19735 : if (making_local_p)
3682 : : {
3683 : 207 : C_DECLARED_LABEL_FLAG (decl) = true;
3684 : 207 : DECL_CHAIN (decl) = current_binding_level->names;
3685 : 207 : current_binding_level->names = decl;
3686 : : }
3687 : :
3688 : 19735 : ent->label_decl = decl;
3689 : :
3690 : 19735 : return ent;
3691 : 22344 : }
3692 : :
3693 : : /* Wrapper for lookup_label_1. */
3694 : :
3695 : : tree
3696 : 2155 : lookup_label (tree id)
3697 : : {
3698 : 2155 : named_label_entry *ent = lookup_label_1 (id, false);
3699 : 2155 : return ent ? ent->label_decl : NULL_TREE;
3700 : : }
3701 : :
3702 : : /* Remember that we've seen &&ID. */
3703 : :
3704 : : void
3705 : 250 : mark_label_addressed (tree id)
3706 : : {
3707 : 250 : named_label_entry *ent = lookup_label_1 (id, false);
3708 : 250 : ent->addressed = true;
3709 : 250 : }
3710 : :
3711 : : tree
3712 : 207 : declare_local_label (tree id)
3713 : : {
3714 : 207 : named_label_entry *ent = lookup_label_1 (id, true);
3715 : 207 : return ent ? ent->label_decl : NULL_TREE;
3716 : : }
3717 : :
3718 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3719 : :
3720 : : static bool
3721 : 13727 : decl_jump_unsafe (tree decl)
3722 : : {
3723 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3724 : : with automatic storage duration is not in scope to a point where it is
3725 : : in scope is ill-formed unless the variable has scalar type, class type
3726 : : with a trivial default constructor and a trivial destructor, a
3727 : : cv-qualified version of one of these types, or an array of one of the
3728 : : preceding types and is declared without an initializer (8.5). */
3729 : 13727 : tree type = TREE_TYPE (decl);
3730 : :
3731 : 13727 : return (type != error_mark_node
3732 : 13709 : && VAR_P (decl)
3733 : 12541 : && !TREE_STATIC (decl)
3734 : 26132 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3735 : 10909 : || variably_modified_type_p (type, NULL_TREE)));
3736 : : }
3737 : :
3738 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3739 : : to the user. */
3740 : :
3741 : : static bool
3742 : 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
3743 : : diagnostic_t diag_kind, bool computed)
3744 : : {
3745 : 286 : if (computed)
3746 : 43 : diag_kind = DK_WARNING;
3747 : 286 : bool complained
3748 : 373 : = emit_diagnostic (diag_kind, loc, 0,
3749 : : decl ? G_("jump to label %qD")
3750 : : : G_("jump to case label"), decl);
3751 : 286 : if (complained && locus)
3752 : : {
3753 : 199 : if (computed)
3754 : 43 : inform (*locus, " as a possible target of computed goto");
3755 : : else
3756 : 156 : inform (*locus, " from here");
3757 : : }
3758 : 286 : return complained;
3759 : : }
3760 : :
3761 : : /* Check that a single previously seen jump to a newly defined label
3762 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3763 : : the jump context; NAMES are the names in scope in LEVEL at the jump
3764 : : context; LOCUS is the source position of the jump or 0. COMPUTED
3765 : : is a vec of decls if the jump is a computed goto. Returns
3766 : : true if all is well. */
3767 : :
3768 : : static bool
3769 : 1963592 : check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3770 : : bool exited_omp, const location_t *locus,
3771 : : vec<tree,va_gc> *computed)
3772 : : {
3773 : 1963592 : auto_diagnostic_group d;
3774 : 1963592 : cp_binding_level *b;
3775 : 1963592 : bool complained = false;
3776 : 1963592 : int identified = 0;
3777 : 1963592 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3778 : 1963592 : bool saw_ceif = false, saw_se = false;
3779 : :
3780 : 1963592 : if (exited_omp)
3781 : : {
3782 : 12 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3783 : : computed);
3784 : 12 : if (complained)
3785 : 12 : inform (input_location, " exits OpenMP structured block");
3786 : : saw_omp = true;
3787 : : identified = 2;
3788 : : }
3789 : :
3790 : 5862879 : for (b = current_binding_level; b ; b = b->level_chain)
3791 : : {
3792 : 3899287 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3793 : :
3794 : 3919251 : for (new_decls = b->names; new_decls != old_decls;
3795 : 9982 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3796 : 401 : : TREE_CHAIN (new_decls)))
3797 : : {
3798 : 9982 : bool problem = decl_jump_unsafe (new_decls);
3799 : 9982 : if (! problem)
3800 : 9937 : continue;
3801 : :
3802 : 45 : if (!identified)
3803 : : {
3804 : 45 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3805 : : computed);
3806 : 45 : identified = 2;
3807 : : }
3808 : 45 : if (complained)
3809 : 45 : inform (DECL_SOURCE_LOCATION (new_decls),
3810 : : " crosses initialization of %q#D", new_decls);
3811 : : }
3812 : :
3813 : 3899287 : if (b == level)
3814 : : break;
3815 : :
3816 : 1935695 : const char *inf = NULL;
3817 : 1935695 : location_t loc = input_location;
3818 : 1935695 : switch (b->kind)
3819 : : {
3820 : 3 : case sk_try:
3821 : 3 : if (!saw_eh)
3822 : : inf = G_(" enters %<try%> block");
3823 : : saw_eh = true;
3824 : : break;
3825 : :
3826 : 9 : case sk_catch:
3827 : 9 : if (!saw_eh)
3828 : : inf = G_(" enters %<catch%> block");
3829 : : saw_eh = true;
3830 : : break;
3831 : :
3832 : 102 : case sk_omp:
3833 : 102 : if (!saw_omp)
3834 : : inf = G_(" enters OpenMP structured block");
3835 : : saw_omp = true;
3836 : : break;
3837 : :
3838 : 3 : case sk_transaction:
3839 : 3 : if (!saw_tm)
3840 : : inf = G_(" enters synchronized or atomic statement");
3841 : : saw_tm = true;
3842 : : break;
3843 : :
3844 : 18 : case sk_stmt_expr:
3845 : 18 : if (!saw_se)
3846 : : inf = G_(" enters statement expression");
3847 : : saw_se = true;
3848 : : break;
3849 : :
3850 : 1935456 : case sk_block:
3851 : 1935456 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3852 : : {
3853 : 9 : inf = G_(" enters %<constexpr if%> statement");
3854 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3855 : : saw_cxif = true;
3856 : : }
3857 : 1935447 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3858 : : {
3859 : 18 : inf = G_(" enters %<consteval if%> statement");
3860 : 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3861 : : saw_ceif = true;
3862 : : }
3863 : : break;
3864 : :
3865 : : default:
3866 : : break;
3867 : : }
3868 : :
3869 : : if (inf)
3870 : : {
3871 : 141 : if (identified < 2)
3872 : 141 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3873 : : computed);
3874 : 141 : identified = 2;
3875 : 141 : if (complained)
3876 : 141 : inform (loc, inf);
3877 : : }
3878 : : }
3879 : :
3880 : 1963592 : if (!vec_safe_is_empty (computed))
3881 : : {
3882 : 9 : if (!identified)
3883 : 6 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3884 : : computed);
3885 : 9 : identified = 2;
3886 : 9 : if (complained)
3887 : 18 : for (tree d : computed)
3888 : : {
3889 : 9 : if (DECL_P (d))
3890 : 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3891 : 3 : else if (d == get_identifier ("catch"))
3892 : 3 : inform (*locus, " does not clean up handled exception");
3893 : : }
3894 : : }
3895 : :
3896 : 3927184 : return !identified;
3897 : 1963592 : }
3898 : :
3899 : : static void
3900 : 1415 : check_previous_goto (tree decl, struct named_label_use_entry *use)
3901 : : {
3902 : 1415 : check_previous_goto_1 (decl, use->binding_level,
3903 : 1415 : use->names_in_scope, use->in_omp_scope,
3904 : 1415 : &use->o_goto_locus, use->computed_goto);
3905 : 1415 : }
3906 : :
3907 : : static bool
3908 : 1962177 : check_switch_goto (cp_binding_level* level)
3909 : : {
3910 : 1962177 : return check_previous_goto_1 (NULL_TREE, level, level->names,
3911 : 1962177 : false, NULL, nullptr);
3912 : : }
3913 : :
3914 : : /* Check that a new jump to a label ENT is OK. COMPUTED is true
3915 : : if this is a possible target of a computed goto. */
3916 : :
3917 : : void
3918 : 2033 : check_goto_1 (named_label_entry *ent, bool computed)
3919 : : {
3920 : 2033 : auto_diagnostic_group d;
3921 : 2033 : tree decl = ent->label_decl;
3922 : :
3923 : : /* If the label hasn't been defined yet, defer checking. */
3924 : 2033 : if (! DECL_INITIAL (decl))
3925 : : {
3926 : : /* Don't bother creating another use if the last goto had the
3927 : : same data, and will therefore create the same set of errors. */
3928 : 1615 : if (ent->uses
3929 : 493 : && ent->uses->names_in_scope == current_binding_level->names)
3930 : : return;
3931 : :
3932 : 1424 : named_label_use_entry *new_use
3933 : 1424 : = ggc_alloc<named_label_use_entry> ();
3934 : 1424 : new_use->binding_level = current_binding_level;
3935 : 1424 : new_use->names_in_scope = current_binding_level->names;
3936 : 1424 : new_use->o_goto_locus = input_location;
3937 : 1424 : new_use->in_omp_scope = false;
3938 : 1424 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3939 : :
3940 : 1424 : new_use->next = ent->uses;
3941 : 1424 : ent->uses = new_use;
3942 : 1424 : return;
3943 : : }
3944 : :
3945 : 418 : bool saw_catch = false, complained = false;
3946 : 418 : int identified = 0;
3947 : 418 : tree bad;
3948 : 418 : unsigned ix;
3949 : :
3950 : 409 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3951 : 403 : || ent->in_constexpr_if || ent->in_consteval_if
3952 : 397 : || ent->in_omp_scope || ent->in_stmt_expr
3953 : 797 : || !vec_safe_is_empty (ent->bad_decls))
3954 : : {
3955 : 45 : diagnostic_t diag_kind = DK_PERMERROR;
3956 : 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3957 : 30 : || ent->in_consteval_if || ent->in_transaction_scope
3958 : 24 : || ent->in_omp_scope || ent->in_stmt_expr)
3959 : 39 : diag_kind = DK_ERROR;
3960 : 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3961 : : &input_location, diag_kind, computed);
3962 : 45 : identified = 1 + (diag_kind == DK_ERROR);
3963 : : }
3964 : :
3965 : 424 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3966 : : {
3967 : 6 : bool problem = decl_jump_unsafe (bad);
3968 : :
3969 : 6 : if (problem && DECL_ARTIFICIAL (bad))
3970 : : {
3971 : : /* Can't skip init of __exception_info. */
3972 : 0 : if (identified == 1)
3973 : : {
3974 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3975 : : &input_location, DK_ERROR, computed);
3976 : 0 : identified = 2;
3977 : : }
3978 : 0 : if (complained)
3979 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3980 : : saw_catch = true;
3981 : : }
3982 : 6 : else if (complained)
3983 : 6 : inform (DECL_SOURCE_LOCATION (bad),
3984 : : " skips initialization of %q#D", bad);
3985 : : }
3986 : :
3987 : 418 : if (complained)
3988 : : {
3989 : 45 : if (ent->in_try_scope)
3990 : 9 : inform (input_location, " enters %<try%> block");
3991 : 36 : else if (ent->in_catch_scope && !saw_catch)
3992 : 3 : inform (input_location, " enters %<catch%> block");
3993 : 33 : else if (ent->in_transaction_scope)
3994 : 3 : inform (input_location, " enters synchronized or atomic statement");
3995 : 30 : else if (ent->in_constexpr_if)
3996 : 3 : inform (input_location, " enters %<constexpr if%> statement");
3997 : 27 : else if (ent->in_consteval_if)
3998 : 3 : inform (input_location, " enters %<consteval if%> statement");
3999 : 24 : else if (ent->in_stmt_expr)
4000 : 15 : inform (input_location, " enters statement expression");
4001 : : }
4002 : :
4003 : 418 : if (ent->in_omp_scope)
4004 : : {
4005 : 3 : if (complained)
4006 : 3 : inform (input_location, " enters OpenMP structured block");
4007 : : }
4008 : 415 : else if (flag_openmp)
4009 : 69 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4010 : : {
4011 : 42 : if (b == ent->binding_level)
4012 : : break;
4013 : 36 : if (b->kind == sk_omp)
4014 : : {
4015 : 21 : if (identified < 2)
4016 : : {
4017 : 42 : complained = identify_goto (decl,
4018 : 21 : DECL_SOURCE_LOCATION (decl),
4019 : : &input_location, DK_ERROR,
4020 : : computed);
4021 : 21 : identified = 2;
4022 : : }
4023 : 21 : if (complained)
4024 : 21 : inform (input_location, " exits OpenMP structured block");
4025 : : break;
4026 : : }
4027 : : }
4028 : :
4029 : : /* Warn if a computed goto might involve a local variable going out of scope
4030 : : without being cleaned up. */
4031 : 418 : if (computed)
4032 : : {
4033 : 71 : auto level = ent->binding_level;
4034 : 71 : auto names = ent->names_in_scope;
4035 : 71 : for (auto b = current_binding_level; ; b = b->level_chain)
4036 : : {
4037 : 111 : if (b->kind == sk_catch)
4038 : : {
4039 : 6 : if (!identified)
4040 : : {
4041 : 6 : complained
4042 : 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4043 : : &input_location, DK_ERROR, computed);
4044 : 6 : identified = 2;
4045 : : }
4046 : 6 : if (complained)
4047 : 6 : inform (input_location,
4048 : : " does not clean up handled exception");
4049 : : }
4050 : 111 : tree end = b == level ? names : NULL_TREE;
4051 : 127 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4052 : : {
4053 : 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4054 : : {
4055 : 10 : if (!identified)
4056 : : {
4057 : 10 : complained
4058 : 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4059 : : &input_location, DK_ERROR, computed);
4060 : 10 : identified = 2;
4061 : : }
4062 : 10 : if (complained)
4063 : 10 : inform (DECL_SOURCE_LOCATION (d),
4064 : : " does not destroy %qD", d);
4065 : : }
4066 : : }
4067 : 111 : if (b == level)
4068 : : break;
4069 : 40 : }
4070 : : }
4071 : 2033 : }
4072 : :
4073 : : /* Check that a new jump to a label DECL is OK. Called by
4074 : : finish_goto_stmt. */
4075 : :
4076 : : void
4077 : 1993 : check_goto (tree decl)
4078 : : {
4079 : 1993 : if (!named_labels)
4080 : : return;
4081 : 1978 : if (TREE_CODE (decl) != LABEL_DECL)
4082 : : {
4083 : : /* We don't know where a computed goto is jumping,
4084 : : so check all addressable labels. */
4085 : 306 : for (auto iter = named_labels->begin ();
4086 : 416 : iter != named_labels->end ();
4087 : 196 : ++iter)
4088 : : {
4089 : 196 : auto ent = *iter;
4090 : 196 : if (ent->addressed)
4091 : 165 : check_goto_1 (ent, true);
4092 : : }
4093 : : }
4094 : : else
4095 : : {
4096 : 1868 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4097 : 1868 : named_label_entry **slot
4098 : 1868 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4099 : 1868 : named_label_entry *ent = *slot;
4100 : 1868 : check_goto_1 (ent, false);
4101 : : }
4102 : : }
4103 : :
4104 : : /* Check that a return is ok wrt OpenMP structured blocks.
4105 : : Called by finish_return_stmt. Returns true if all is well. */
4106 : :
4107 : : bool
4108 : 226490 : check_omp_return (void)
4109 : : {
4110 : 871091 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4111 : 644601 : if (b->kind == sk_omp)
4112 : : {
4113 : 15 : error ("invalid exit from OpenMP structured block");
4114 : 15 : return false;
4115 : : }
4116 : 644586 : else if (b->kind == sk_function_parms)
4117 : : break;
4118 : : return true;
4119 : : }
4120 : :
4121 : : /* Define a label, specifying the location in the source file.
4122 : : Return the LABEL_DECL node for the label. */
4123 : :
4124 : : tree
4125 : 19732 : define_label (location_t location, tree name)
4126 : : {
4127 : 19732 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4128 : :
4129 : : /* After labels, make any new cleanups in the function go into their
4130 : : own new (temporary) binding contour. */
4131 : 19732 : for (cp_binding_level *p = current_binding_level;
4132 : 28808 : p->kind != sk_function_parms;
4133 : 9076 : p = p->level_chain)
4134 : 9076 : p->more_cleanups_ok = 0;
4135 : :
4136 : 19732 : named_label_entry *ent = lookup_label_1 (name, false);
4137 : 19732 : tree decl = ent->label_decl;
4138 : :
4139 : 19732 : if (DECL_INITIAL (decl) != NULL_TREE)
4140 : : {
4141 : 6 : error ("duplicate label %qD", decl);
4142 : 6 : return error_mark_node;
4143 : : }
4144 : : else
4145 : : {
4146 : : /* Mark label as having been defined. */
4147 : 19726 : DECL_INITIAL (decl) = error_mark_node;
4148 : : /* Say where in the source. */
4149 : 19726 : DECL_SOURCE_LOCATION (decl) = location;
4150 : :
4151 : 19726 : ent->binding_level = current_binding_level;
4152 : 19726 : ent->names_in_scope = current_binding_level->names;
4153 : :
4154 : 21141 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4155 : 1415 : check_previous_goto (decl, use);
4156 : 19726 : ent->uses = NULL;
4157 : : }
4158 : :
4159 : 19726 : return decl;
4160 : 19732 : }
4161 : :
4162 : : struct cp_switch
4163 : : {
4164 : : cp_binding_level *level;
4165 : : struct cp_switch *next;
4166 : : /* The SWITCH_STMT being built. */
4167 : : tree switch_stmt;
4168 : : /* A splay-tree mapping the low element of a case range to the high
4169 : : element, or NULL_TREE if there is no high element. Used to
4170 : : determine whether or not a new case label duplicates an old case
4171 : : label. We need a tree, rather than simply a hash table, because
4172 : : of the GNU case range extension. */
4173 : : splay_tree cases;
4174 : : /* Remember whether a default: case label has been seen. */
4175 : : bool has_default_p;
4176 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4177 : : bool break_stmt_seen_p;
4178 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4179 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4180 : : bool in_loop_body_p;
4181 : : };
4182 : :
4183 : : /* A stack of the currently active switch statements. The innermost
4184 : : switch statement is on the top of the stack. There is no need to
4185 : : mark the stack for garbage collection because it is only active
4186 : : during the processing of the body of a function, and we never
4187 : : collect at that point. */
4188 : :
4189 : : static struct cp_switch *switch_stack;
4190 : :
4191 : : /* Called right after a switch-statement condition is parsed.
4192 : : SWITCH_STMT is the switch statement being parsed. */
4193 : :
4194 : : void
4195 : 478415 : push_switch (tree switch_stmt)
4196 : : {
4197 : 478415 : struct cp_switch *p = XNEW (struct cp_switch);
4198 : 478415 : p->level = current_binding_level;
4199 : 478415 : p->next = switch_stack;
4200 : 478415 : p->switch_stmt = switch_stmt;
4201 : 478415 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4202 : 478415 : p->has_default_p = false;
4203 : 478415 : p->break_stmt_seen_p = false;
4204 : 478415 : p->in_loop_body_p = false;
4205 : 478415 : switch_stack = p;
4206 : 478415 : }
4207 : :
4208 : : void
4209 : 478415 : pop_switch (void)
4210 : : {
4211 : 478415 : struct cp_switch *cs = switch_stack;
4212 : :
4213 : : /* Emit warnings as needed. */
4214 : 478415 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4215 : 478415 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4216 : 478415 : const bool bool_cond_p
4217 : 478415 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4218 : 478415 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4219 : 478415 : if (!processing_template_decl)
4220 : 307621 : c_do_switch_warnings (cs->cases, switch_location,
4221 : 307621 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4222 : : bool_cond_p);
4223 : :
4224 : : /* For the benefit of block_may_fallthru remember if the switch body
4225 : : case labels cover all possible values and if there are break; stmts. */
4226 : 478415 : if (cs->has_default_p
4227 : 478415 : || (!processing_template_decl
4228 : 42936 : && c_switch_covers_all_cases_p (cs->cases,
4229 : 42936 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4230 : 367149 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4231 : 478415 : if (!cs->break_stmt_seen_p)
4232 : 248465 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4233 : : /* Now that we're done with the switch warnings, set the switch type
4234 : : to the type of the condition if the index type was of scoped enum type.
4235 : : (Such types don't participate in the integer promotions.) We do this
4236 : : because of bit-fields whose declared type is a scoped enum type:
4237 : : gimplification will use the lowered index type, but convert the
4238 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4239 : : and verify_gimple_switch doesn't accept that. */
4240 : 478415 : if (is_bitfield_expr_with_lowered_type (cond))
4241 : 53967 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4242 : 478415 : gcc_assert (!cs->in_loop_body_p);
4243 : 478415 : splay_tree_delete (cs->cases);
4244 : 478415 : switch_stack = switch_stack->next;
4245 : 478415 : free (cs);
4246 : 478415 : }
4247 : :
4248 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4249 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4250 : : in switch_stack we've seen a BREAK_STMT. */
4251 : :
4252 : : void
4253 : 3440398 : note_break_stmt (void)
4254 : : {
4255 : 3440398 : if (switch_stack && !switch_stack->in_loop_body_p)
4256 : 1482020 : switch_stack->break_stmt_seen_p = true;
4257 : 3440398 : }
4258 : :
4259 : : /* Note the start of processing of an iteration statement's body.
4260 : : The note_break_stmt function will do nothing while processing it.
4261 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4262 : :
4263 : : bool
4264 : 15241672 : note_iteration_stmt_body_start (void)
4265 : : {
4266 : 15241672 : if (!switch_stack)
4267 : : return false;
4268 : 115747 : bool ret = switch_stack->in_loop_body_p;
4269 : 115747 : switch_stack->in_loop_body_p = true;
4270 : 115747 : return ret;
4271 : : }
4272 : :
4273 : : /* Note the end of processing of an iteration statement's body. */
4274 : :
4275 : : void
4276 : 15241672 : note_iteration_stmt_body_end (bool prev)
4277 : : {
4278 : 15241672 : if (switch_stack)
4279 : 115747 : switch_stack->in_loop_body_p = prev;
4280 : 15241672 : }
4281 : :
4282 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4283 : : condition. Note that if TYPE and VALUE are already integral we don't
4284 : : really do the conversion because the language-independent
4285 : : warning/optimization code will work better that way. */
4286 : :
4287 : : static tree
4288 : 3924108 : case_conversion (tree type, tree value)
4289 : : {
4290 : 3924108 : if (value == NULL_TREE)
4291 : : return value;
4292 : :
4293 : 1697531 : value = mark_rvalue_use (value);
4294 : :
4295 : 1697531 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4296 : 974757 : type = type_promotes_to (type);
4297 : :
4298 : 1697531 : tree ovalue = value;
4299 : : /* The constant-expression VALUE shall be a converted constant expression
4300 : : of the adjusted type of the switch condition, which doesn't allow
4301 : : narrowing conversions. */
4302 : 1697531 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4303 : :
4304 : 1697531 : if (cxx_dialect >= cxx11
4305 : 1697531 : && (SCOPED_ENUM_P (type)
4306 : 972039 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4307 : : /* Use the converted value. */;
4308 : : else
4309 : : /* The already integral case. */
4310 : : value = ovalue;
4311 : :
4312 : 1697531 : return cxx_constant_value (value);
4313 : : }
4314 : :
4315 : : /* Note that we've seen a definition of a case label, and complain if this
4316 : : is a bad place for one. */
4317 : :
4318 : : tree
4319 : 3538335 : finish_case_label (location_t loc, tree low_value, tree high_value)
4320 : : {
4321 : 3538335 : tree cond, r;
4322 : 3538335 : cp_binding_level *p;
4323 : 3538335 : tree type;
4324 : :
4325 : 3538335 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4326 : 367113 : switch_stack->has_default_p = true;
4327 : :
4328 : 3538335 : if (processing_template_decl)
4329 : : {
4330 : 1576158 : tree label;
4331 : :
4332 : : /* For templates, just add the case label; we'll do semantic
4333 : : analysis at instantiation-time. */
4334 : 1576158 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4335 : 1576158 : return add_stmt (build_case_label (low_value, high_value, label));
4336 : : }
4337 : :
4338 : : /* Find the condition on which this switch statement depends. */
4339 : 1962177 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4340 : 1962177 : if (cond && TREE_CODE (cond) == TREE_LIST)
4341 : 0 : cond = TREE_VALUE (cond);
4342 : :
4343 : 1962177 : if (!check_switch_goto (switch_stack->level))
4344 : 87 : return error_mark_node;
4345 : :
4346 : 1962090 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4347 : 1962090 : if (type == error_mark_node)
4348 : : return error_mark_node;
4349 : :
4350 : 1962054 : low_value = case_conversion (type, low_value);
4351 : 1962054 : high_value = case_conversion (type, high_value);
4352 : :
4353 : 1962054 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4354 : :
4355 : : /* After labels, make any new cleanups in the function go into their
4356 : : own new (temporary) binding contour. */
4357 : 1962054 : for (p = current_binding_level;
4358 : 8270557 : p->kind != sk_function_parms;
4359 : 6308503 : p = p->level_chain)
4360 : 6308503 : p->more_cleanups_ok = 0;
4361 : :
4362 : : return r;
4363 : : }
4364 : :
4365 : : struct typename_info {
4366 : : tree scope;
4367 : : tree name;
4368 : : tree template_id;
4369 : : bool enum_p;
4370 : : bool class_p;
4371 : : };
4372 : :
4373 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4374 : : {
4375 : : typedef typename_info *compare_type;
4376 : :
4377 : : /* Hash a TYPENAME_TYPE. */
4378 : :
4379 : : static hashval_t
4380 : 382088931 : hash (tree context, tree fullname)
4381 : : {
4382 : 382088931 : hashval_t hash = 0;
4383 : 382088931 : hash = iterative_hash_object (context, hash);
4384 : 382088931 : hash = iterative_hash_object (fullname, hash);
4385 : 382088931 : return hash;
4386 : : }
4387 : :
4388 : : static hashval_t
4389 : 55613510 : hash (const typename_info *ti)
4390 : : {
4391 : 55613510 : return typename_hasher::hash (ti->scope, ti->template_id);
4392 : : }
4393 : :
4394 : : static hashval_t
4395 : 326475421 : hash (tree t)
4396 : : {
4397 : 326475421 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4398 : : }
4399 : :
4400 : : /* Compare two TYPENAME_TYPEs. */
4401 : :
4402 : : static bool
4403 : 379531518 : equal (tree t1, const typename_info *t2)
4404 : : {
4405 : 379531518 : return (TYPE_IDENTIFIER (t1) == t2->name
4406 : 112386951 : && TYPE_CONTEXT (t1) == t2->scope
4407 : 28428438 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4408 : 28418060 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4409 : 407949524 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4410 : : }
4411 : : };
4412 : :
4413 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4414 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4415 : :
4416 : : Returns the new TYPENAME_TYPE. */
4417 : :
4418 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4419 : :
4420 : : tree
4421 : 55613510 : build_typename_type (tree context, tree name, tree fullname,
4422 : : enum tag_types tag_type)
4423 : : {
4424 : 55613510 : typename_info ti;
4425 : :
4426 : 55613510 : if (typename_htab == NULL)
4427 : 18806 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4428 : :
4429 : 55613510 : ti.scope = FROB_CONTEXT (context);
4430 : 55613510 : ti.name = name;
4431 : 55613510 : ti.template_id = fullname;
4432 : 55613510 : ti.enum_p = tag_type == enum_type;
4433 : 55613510 : ti.class_p = (tag_type == class_type
4434 : : || tag_type == record_type
4435 : 55613510 : || tag_type == union_type);
4436 : 55613510 : hashval_t hash = typename_hasher::hash (&ti);
4437 : :
4438 : : /* See if we already have this type. */
4439 : 55613510 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4440 : 55613510 : tree t = *e;
4441 : 55613510 : if (*e)
4442 : : t = *e;
4443 : : else
4444 : : {
4445 : : /* Build the TYPENAME_TYPE. */
4446 : 27195528 : t = cxx_make_type (TYPENAME_TYPE);
4447 : 27195528 : TYPE_CONTEXT (t) = ti.scope;
4448 : 27195528 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4449 : 27195528 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4450 : 27195528 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4451 : :
4452 : : /* Build the corresponding TYPE_DECL. */
4453 : 27195528 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4454 : 27195528 : TYPE_NAME (t) = d;
4455 : 27195528 : TYPE_STUB_DECL (t) = d;
4456 : 27195528 : DECL_CONTEXT (d) = ti.scope;
4457 : 27195528 : DECL_ARTIFICIAL (d) = 1;
4458 : :
4459 : : /* Store it in the hash table. */
4460 : 27195528 : *e = t;
4461 : :
4462 : : /* TYPENAME_TYPEs must always be compared structurally, because
4463 : : they may or may not resolve down to another type depending on
4464 : : the currently open classes. */
4465 : 27195528 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4466 : : }
4467 : :
4468 : 55613510 : return t;
4469 : : }
4470 : :
4471 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4472 : : provided to name the type. Returns an appropriate type, unless an
4473 : : error occurs, in which case error_mark_node is returned. If we
4474 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4475 : : return that, rather than the _TYPE it corresponds to, in other
4476 : : cases we look through the type decl. If TF_ERROR is set, complain
4477 : : about errors, otherwise be quiet. */
4478 : :
4479 : : tree
4480 : 187688486 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4481 : : tsubst_flags_t complain)
4482 : : {
4483 : 187688486 : tree fullname;
4484 : 187688486 : tree t;
4485 : 187688486 : bool want_template;
4486 : :
4487 : 187688486 : if (name == error_mark_node
4488 : 187688484 : || context == NULL_TREE
4489 : 187688484 : || context == error_mark_node)
4490 : : return error_mark_node;
4491 : :
4492 : 187688471 : if (TYPE_P (name))
4493 : : {
4494 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4495 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4496 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4497 : 0 : name = TYPE_IDENTIFIER (name);
4498 : : else
4499 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4500 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4501 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4502 : 0 : CLASSTYPE_TI_ARGS (name));
4503 : : }
4504 : 187688471 : else if (TREE_CODE (name) == TYPE_DECL)
4505 : 0 : name = DECL_NAME (name);
4506 : :
4507 : 187688471 : fullname = name;
4508 : :
4509 : 187688471 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4510 : : {
4511 : 6113097 : name = TREE_OPERAND (name, 0);
4512 : 6113097 : if (DECL_TYPE_TEMPLATE_P (name))
4513 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4514 : 6113097 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4515 : : {
4516 : 0 : if (complain & tf_error)
4517 : 0 : error ("%qD is not a type", name);
4518 : 0 : return error_mark_node;
4519 : : }
4520 : : }
4521 : 187688471 : if (TREE_CODE (name) == TEMPLATE_DECL)
4522 : : {
4523 : 0 : if (complain & tf_error)
4524 : 0 : error ("%qD used without template arguments", name);
4525 : 0 : return error_mark_node;
4526 : : }
4527 : 187688471 : else if (is_overloaded_fn (name))
4528 : : {
4529 : 3 : if (complain & tf_error)
4530 : 3 : error ("%qD is a function, not a type", name);
4531 : 3 : return error_mark_node;
4532 : : }
4533 : 187688468 : gcc_assert (identifier_p (name));
4534 : 187688468 : gcc_assert (TYPE_P (context));
4535 : :
4536 : 187688468 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4537 : : /* This can happen for C++17 variadic using (c++/88986). */;
4538 : 176143006 : else if (!MAYBE_CLASS_TYPE_P (context))
4539 : : {
4540 : 6 : if (complain & tf_error)
4541 : 6 : error ("%q#T is not a class", context);
4542 : 6 : return error_mark_node;
4543 : : }
4544 : :
4545 : : /* When the CONTEXT is a dependent type, NAME could refer to a
4546 : : dependent base class of CONTEXT. But look inside it anyway
4547 : : if CONTEXT is a currently open scope, in case it refers to a
4548 : : member of the current instantiation or a non-dependent base;
4549 : : lookup will stop when we hit a dependent base. */
4550 : 187688462 : if (!dependent_scope_p (context))
4551 : : {
4552 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4553 : : (as per [temp.res.general]/3), unless
4554 : : - the tag corresponds to a class-key or 'enum' so
4555 : : [basic.lookup.elab] applies, or
4556 : : - the tag corresponds to scope_type or tf_qualifying_scope is
4557 : : set so [basic.lookup.qual]/1 applies.
4558 : : TODO: If we'd set/track the scope_type tag thoroughly on all
4559 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4560 : : tf_qualifying_scope flag. */
4561 : 272283516 : bool want_type = (tag_type != none_type && tag_type != typename_type)
4562 : 136141758 : || (complain & tf_qualifying_scope);
4563 : 136141758 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4564 : : }
4565 : : else
4566 : : t = NULL_TREE;
4567 : :
4568 : 205052095 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
4569 : 51547622 : return build_typename_type (context, name, fullname, tag_type);
4570 : :
4571 : 136140840 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4572 : :
4573 : 136140840 : if (!t)
4574 : : {
4575 : 17362715 : if (complain & tf_error)
4576 : : {
4577 : 231 : if (!COMPLETE_TYPE_P (context))
4578 : 18 : cxx_incomplete_type_error (NULL_TREE, context);
4579 : : else
4580 : 425 : error (want_template ? G_("no class template named %q#T in %q#T")
4581 : : : G_("no type named %q#T in %q#T"), name, context);
4582 : : }
4583 : 17362715 : return error_mark_node;
4584 : : }
4585 : :
4586 : : /* Pull out the template from an injected-class-name (or multiple). */
4587 : 118778125 : if (want_template)
4588 : 4682196 : t = maybe_get_template_decl_from_type_decl (t);
4589 : :
4590 : 118778125 : if (TREE_CODE (t) == TREE_LIST)
4591 : : {
4592 : 3 : if (complain & tf_error)
4593 : : {
4594 : 3 : auto_diagnostic_group d;
4595 : 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
4596 : 3 : print_candidates (t);
4597 : 3 : }
4598 : 3 : return error_mark_node;
4599 : : }
4600 : :
4601 : 118778122 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4602 : : {
4603 : 6 : if (complain & tf_error)
4604 : 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
4605 : : context, name, t);
4606 : 6 : return error_mark_node;
4607 : : }
4608 : 118778116 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
4609 : : {
4610 : 327 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4611 : 657 : && DECL_TYPE_TEMPLATE_P (t))
4612 : : /* The caller permits this typename-specifier to name a template
4613 : : (because it appears in a CTAD-enabled context). */;
4614 : : else
4615 : : {
4616 : 23 : if (complain & tf_error)
4617 : 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
4618 : : context, name, t);
4619 : 23 : return error_mark_node;
4620 : : }
4621 : : }
4622 : :
4623 : 118778093 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4624 : : context, complain))
4625 : 49 : return error_mark_node;
4626 : :
4627 : 118778044 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4628 : 316 : return make_template_placeholder (t);
4629 : :
4630 : 118777728 : if (want_template)
4631 : : {
4632 : 4682190 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4633 : : NULL_TREE, context,
4634 : : complain | tf_user);
4635 : 4682190 : if (t == error_mark_node)
4636 : : return error_mark_node;
4637 : 4682185 : t = TYPE_NAME (t);
4638 : : }
4639 : :
4640 : 118777723 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4641 : 3337141 : t = TREE_TYPE (t);
4642 : :
4643 : 118777723 : maybe_record_typedef_use (t);
4644 : :
4645 : 118777723 : return t;
4646 : : }
4647 : :
4648 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4649 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4650 : : in which case error_mark_node is returned.
4651 : :
4652 : : If PARM_LIST is non-NULL, also make sure that the template parameter
4653 : : list of TEMPLATE_DECL matches.
4654 : :
4655 : : If COMPLAIN zero, don't complain about any errors that occur. */
4656 : :
4657 : : tree
4658 : 80272 : make_unbound_class_template (tree context, tree name, tree parm_list,
4659 : : tsubst_flags_t complain)
4660 : : {
4661 : 80272 : if (TYPE_P (name))
4662 : 0 : name = TYPE_IDENTIFIER (name);
4663 : 80272 : else if (DECL_P (name))
4664 : 0 : name = DECL_NAME (name);
4665 : 80272 : gcc_assert (identifier_p (name));
4666 : :
4667 : 80272 : if (!dependent_type_p (context)
4668 : 80272 : || currently_open_class (context))
4669 : : {
4670 : 26470 : tree tmpl = NULL_TREE;
4671 : :
4672 : 26470 : if (MAYBE_CLASS_TYPE_P (context))
4673 : 26464 : tmpl = lookup_field (context, name, 0, false);
4674 : :
4675 : 26464 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4676 : 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4677 : :
4678 : 26458 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4679 : : {
4680 : 12 : if (complain & tf_error)
4681 : 9 : error ("no class template named %q#T in %q#T", name, context);
4682 : 12 : return error_mark_node;
4683 : : }
4684 : :
4685 : 26458 : if (parm_list
4686 : 26458 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4687 : : {
4688 : 0 : if (complain & tf_error)
4689 : : {
4690 : 0 : auto_diagnostic_group d;
4691 : 0 : error ("template parameters do not match template %qD", tmpl);
4692 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
4693 : : "%qD declared here", tmpl);
4694 : 0 : }
4695 : 0 : return error_mark_node;
4696 : : }
4697 : :
4698 : 26458 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4699 : : complain))
4700 : 0 : return error_mark_node;
4701 : :
4702 : : return tmpl;
4703 : : }
4704 : :
4705 : 53802 : return make_unbound_class_template_raw (context, name, parm_list);
4706 : : }
4707 : :
4708 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
4709 : :
4710 : : tree
4711 : 53846 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4712 : : {
4713 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
4714 : 53846 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4715 : 53846 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4716 : 53846 : TREE_TYPE (t) = NULL_TREE;
4717 : 53846 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4718 : :
4719 : : /* Build the corresponding TEMPLATE_DECL. */
4720 : 53846 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4721 : 53846 : TYPE_NAME (t) = d;
4722 : 53846 : TYPE_STUB_DECL (t) = d;
4723 : 53846 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4724 : 53846 : DECL_ARTIFICIAL (d) = 1;
4725 : 53846 : DECL_TEMPLATE_PARMS (d) = parm_list;
4726 : :
4727 : 53846 : return t;
4728 : : }
4729 : :
4730 : :
4731 : :
4732 : : /* Push the declarations of builtin types into the global namespace.
4733 : : RID_INDEX is the index of the builtin type in the array
4734 : : RID_POINTERS. NAME is the name used when looking up the builtin
4735 : : type. TYPE is the _TYPE node for the builtin type.
4736 : :
4737 : : The calls to set_global_binding below should be
4738 : : eliminated. Built-in types should not be looked up name; their
4739 : : names are keywords that the parser can recognize. However, there
4740 : : is code in c-common.cc that uses identifier_global_value to look up
4741 : : built-in types by name. */
4742 : :
4743 : : void
4744 : 3898627 : record_builtin_type (enum rid rid_index,
4745 : : const char* name,
4746 : : tree type)
4747 : : {
4748 : 3898627 : tree decl = NULL_TREE;
4749 : :
4750 : 3898627 : if (name)
4751 : : {
4752 : 2283015 : tree tname = get_identifier (name);
4753 : 2283015 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4754 : 2283015 : DECL_ARTIFICIAL (tdecl) = 1;
4755 : 2283015 : set_global_binding (tdecl);
4756 : 2283015 : decl = tdecl;
4757 : : }
4758 : :
4759 : 3898627 : if ((int) rid_index < (int) RID_MAX)
4760 : 2378051 : if (tree rname = ridpointers[(int) rid_index])
4761 : 1807835 : if (!decl || DECL_NAME (decl) != rname)
4762 : : {
4763 : 1330504 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4764 : 1330504 : DECL_ARTIFICIAL (rdecl) = 1;
4765 : 1330504 : set_global_binding (rdecl);
4766 : 1330504 : if (!decl)
4767 : : decl = rdecl;
4768 : : }
4769 : :
4770 : 3613519 : if (decl)
4771 : : {
4772 : 3328411 : if (!TYPE_NAME (type))
4773 : 2758195 : TYPE_NAME (type) = decl;
4774 : 3328411 : debug_hooks->type_decl (decl, 0);
4775 : : }
4776 : 3898627 : }
4777 : :
4778 : : /* Push a type into the namespace so that the back ends ignore it. */
4779 : :
4780 : : static void
4781 : 285108 : record_unknown_type (tree type, const char* name)
4782 : : {
4783 : 285108 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4784 : : TYPE_DECL, get_identifier (name), type));
4785 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4786 : 285108 : DECL_IGNORED_P (decl) = 1;
4787 : 285108 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4788 : 285108 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4789 : 285108 : SET_TYPE_ALIGN (type, 1);
4790 : 285108 : TYPE_USER_ALIGN (type) = 0;
4791 : 285108 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4792 : 285108 : }
4793 : :
4794 : : /* Create all the predefined identifiers. */
4795 : :
4796 : : static void
4797 : 95036 : initialize_predefined_identifiers (void)
4798 : : {
4799 : 95036 : struct predefined_identifier
4800 : : {
4801 : : const char *name; /* Name. */
4802 : : tree *node; /* Node to store it in. */
4803 : : cp_identifier_kind kind; /* Kind of identifier. */
4804 : : };
4805 : :
4806 : : /* A table of identifiers to create at startup. */
4807 : 95036 : static const predefined_identifier predefined_identifiers[] = {
4808 : : {"C++", &lang_name_cplusplus, cik_normal},
4809 : : {"C", &lang_name_c, cik_normal},
4810 : : /* Some of these names have a trailing space so that it is
4811 : : impossible for them to conflict with names written by users. */
4812 : : {"__ct ", &ctor_identifier, cik_ctor},
4813 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
4814 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4815 : : {"__dt ", &dtor_identifier, cik_dtor},
4816 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
4817 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4818 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4819 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
4820 : : {"__in_chrg", &in_charge_identifier, cik_normal},
4821 : : {"__as_base ", &as_base_identifier, cik_normal},
4822 : : {"this", &this_identifier, cik_normal},
4823 : : {"__delta", &delta_identifier, cik_normal},
4824 : : {"__pfn", &pfn_identifier, cik_normal},
4825 : : {"_vptr", &vptr_identifier, cik_normal},
4826 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4827 : : {"::", &global_identifier, cik_normal},
4828 : : /* The demangler expects anonymous namespaces to be called
4829 : : something starting with '_GLOBAL__N_'. It no longer needs
4830 : : to be unique to the TU. */
4831 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4832 : : {"auto", &auto_identifier, cik_normal},
4833 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4834 : : {"initializer_list", &init_list_identifier, cik_normal},
4835 : : {"__for_range ", &for_range__identifier, cik_normal},
4836 : : {"__for_begin ", &for_begin__identifier, cik_normal},
4837 : : {"__for_end ", &for_end__identifier, cik_normal},
4838 : : {"__for_range", &for_range_identifier, cik_normal},
4839 : : {"__for_begin", &for_begin_identifier, cik_normal},
4840 : : {"__for_end", &for_end_identifier, cik_normal},
4841 : : {"abi_tag", &abi_tag_identifier, cik_normal},
4842 : : {"aligned", &aligned_identifier, cik_normal},
4843 : : {"begin", &begin_identifier, cik_normal},
4844 : : {"end", &end_identifier, cik_normal},
4845 : : {"get", &get__identifier, cik_normal},
4846 : : {"gnu", &gnu_identifier, cik_normal},
4847 : : {"tuple_element", &tuple_element_identifier, cik_normal},
4848 : : {"tuple_size", &tuple_size_identifier, cik_normal},
4849 : : {"type", &type_identifier, cik_normal},
4850 : : {"value", &value_identifier, cik_normal},
4851 : : {"_FUN", &fun_identifier, cik_normal},
4852 : : {"__closure", &closure_identifier, cik_normal},
4853 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
4854 : : {"heap ", &heap_identifier, cik_normal},
4855 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
4856 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4857 : : {"heap []", &heap_vec_identifier, cik_normal},
4858 : : {"omp", &omp_identifier, cik_normal},
4859 : : {"internal ", &internal_identifier, cik_normal},
4860 : : {NULL, NULL, cik_normal}
4861 : : };
4862 : :
4863 : 4561728 : for (const predefined_identifier *pid = predefined_identifiers;
4864 : 4561728 : pid->name; ++pid)
4865 : : {
4866 : 4466692 : *pid->node = get_identifier (pid->name);
4867 : : /* Some of these identifiers already have a special kind. */
4868 : 4466692 : if (pid->kind != cik_normal)
4869 : 760288 : set_identifier_kind (*pid->node, pid->kind);
4870 : : }
4871 : 95036 : }
4872 : :
4873 : : /* Build a specific variant of operator new. */
4874 : :
4875 : : static void
4876 : 169435 : cxx_build_operator_new (tree newtype)
4877 : : {
4878 : 169435 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4879 : 169435 : DECL_IS_MALLOC (opnew) = 1;
4880 : 169435 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
4881 : 169435 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4882 : 169435 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4883 : 169435 : DECL_IS_MALLOC (opnew) = 1;
4884 : 169435 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
4885 : 169435 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4886 : 169435 : }
4887 : :
4888 : : /* Build a specific variant of operator delete. */
4889 : :
4890 : : static void
4891 : 319446 : cxx_build_operator_delete (tree deltype)
4892 : : {
4893 : 319446 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4894 : 319446 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4895 : 319446 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4896 : 319446 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4897 : 319446 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4898 : 319446 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4899 : 319446 : }
4900 : :
4901 : : /* Declare all variants of operator new and delete. */
4902 : :
4903 : : static void
4904 : 95036 : cxx_init_operator_new_delete_decls (void)
4905 : : {
4906 : 95036 : tree newattrs, extvisattr;
4907 : 95036 : tree newtype, deltype;
4908 : 95036 : tree ptr_ftype_sizetype;
4909 : 95036 : tree new_eh_spec;
4910 : 95036 : tree void_ftype_ptr = build_function_type_list (void_type_node,
4911 : : ptr_type_node, NULL_TREE);
4912 : 95036 : void_ftype_ptr
4913 : 95036 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
4914 : :
4915 : 95036 : ptr_ftype_sizetype
4916 : 95036 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4917 : 95036 : if (cxx_dialect == cxx98)
4918 : : {
4919 : 13914 : tree bad_alloc_id;
4920 : 13914 : tree bad_alloc_type_node;
4921 : 13914 : tree bad_alloc_decl;
4922 : :
4923 : 13914 : push_nested_namespace (std_node);
4924 : 13914 : bad_alloc_id = get_identifier ("bad_alloc");
4925 : 13914 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
4926 : 13914 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4927 : 13914 : bad_alloc_decl
4928 : 13914 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4929 : 13914 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4930 : 13914 : pop_nested_namespace (std_node);
4931 : :
4932 : 13914 : new_eh_spec
4933 : 13914 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4934 : : }
4935 : : else
4936 : 81122 : new_eh_spec = noexcept_false_spec;
4937 : :
4938 : 95036 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
4939 : : NULL_TREE);
4940 : 95036 : newattrs = tree_cons (get_identifier ("alloc_size"),
4941 : : build_tree_list (NULL_TREE, integer_one_node),
4942 : : extvisattr);
4943 : 95036 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4944 : 95036 : newtype = build_exception_variant (newtype, new_eh_spec);
4945 : 95036 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4946 : 95036 : deltype = build_exception_variant (deltype, empty_except_spec);
4947 : 95036 : cxx_build_operator_new (newtype);
4948 : 95036 : cxx_build_operator_delete (deltype);
4949 : 95036 : if (flag_sized_deallocation)
4950 : : {
4951 : : /* Also push the sized deallocation variants:
4952 : : void operator delete(void*, std::size_t) throw();
4953 : : void operator delete[](void*, std::size_t) throw(); */
4954 : 75617 : tree void_ftype_ptr_size
4955 : 75617 : = build_function_type_list (void_type_node, ptr_type_node,
4956 : : size_type_node, NULL_TREE);
4957 : 75617 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4958 : : extvisattr);
4959 : 75617 : deltype = build_exception_variant (deltype, empty_except_spec);
4960 : 75617 : cxx_build_operator_delete (deltype);
4961 : : }
4962 : :
4963 : 95036 : if (aligned_new_threshold)
4964 : : {
4965 : 74399 : push_nested_namespace (std_node);
4966 : 74399 : tree align_id = get_identifier ("align_val_t");
4967 : 74399 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4968 : : NULL_TREE, /*scoped*/true, NULL);
4969 : 74399 : pop_nested_namespace (std_node);
4970 : :
4971 : : /* operator new (size_t, align_val_t); */
4972 : 74399 : newtype = build_function_type_list (ptr_type_node, size_type_node,
4973 : : align_type_node, NULL_TREE);
4974 : 74399 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
4975 : 74399 : newtype = build_exception_variant (newtype, new_eh_spec);
4976 : 74399 : cxx_build_operator_new (newtype);
4977 : :
4978 : : /* operator delete (void *, align_val_t); */
4979 : 74399 : deltype = build_function_type_list (void_type_node, ptr_type_node,
4980 : : align_type_node, NULL_TREE);
4981 : 74399 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4982 : 74399 : deltype = build_exception_variant (deltype, empty_except_spec);
4983 : 74399 : cxx_build_operator_delete (deltype);
4984 : :
4985 : 74399 : if (flag_sized_deallocation)
4986 : : {
4987 : : /* operator delete (void *, size_t, align_val_t); */
4988 : 74394 : deltype = build_function_type_list (void_type_node, ptr_type_node,
4989 : : size_type_node, align_type_node,
4990 : : NULL_TREE);
4991 : 74394 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4992 : 74394 : deltype = build_exception_variant (deltype, empty_except_spec);
4993 : 74394 : cxx_build_operator_delete (deltype);
4994 : : }
4995 : : }
4996 : 95036 : }
4997 : :
4998 : : /* Create the predefined scalar types of C,
4999 : : and some nodes representing standard constants (0, 1, (void *)0).
5000 : : Initialize the global binding level.
5001 : : Make definitions for built-in primitive functions. */
5002 : :
5003 : : void
5004 : 95036 : cxx_init_decl_processing (void)
5005 : : {
5006 : 95036 : tree void_ftype;
5007 : :
5008 : : /* Create all the identifiers we need. */
5009 : 95036 : initialize_predefined_identifiers ();
5010 : :
5011 : : /* Create the global variables. */
5012 : 95036 : push_to_top_level ();
5013 : :
5014 : 95036 : current_function_decl = NULL_TREE;
5015 : 95036 : current_binding_level = NULL;
5016 : : /* Enter the global namespace. */
5017 : 95036 : gcc_assert (global_namespace == NULL_TREE);
5018 : 95036 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5019 : : void_type_node);
5020 : 95036 : TREE_PUBLIC (global_namespace) = true;
5021 : 95036 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5022 : 95036 : DECL_CONTEXT (global_namespace)
5023 : 95036 : = build_translation_unit_decl (get_identifier (main_input_filename));
5024 : : /* Remember whether we want the empty class passing ABI change warning
5025 : : in this TU. */
5026 : 95036 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5027 : 120563 : = warn_abi && abi_version_crosses (12);
5028 : 95036 : debug_hooks->register_main_translation_unit
5029 : 95036 : (DECL_CONTEXT (global_namespace));
5030 : 95036 : begin_scope (sk_namespace, global_namespace);
5031 : 95036 : current_namespace = global_namespace;
5032 : :
5033 : 95036 : if (flag_visibility_ms_compat)
5034 : 6 : default_visibility = VISIBILITY_HIDDEN;
5035 : :
5036 : : /* Initially, C. */
5037 : 95036 : current_lang_name = lang_name_c;
5038 : :
5039 : : /* Create the `std' namespace. */
5040 : 95036 : push_namespace (get_identifier ("std"));
5041 : 95036 : std_node = current_namespace;
5042 : 95036 : pop_namespace ();
5043 : :
5044 : 95036 : flag_noexcept_type = (cxx_dialect >= cxx17);
5045 : :
5046 : 95036 : c_common_nodes_and_builtins ();
5047 : :
5048 : 95036 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5049 : 95036 : tree decl
5050 : 95036 : = add_builtin_function ("__builtin_is_constant_evaluated",
5051 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5052 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5053 : 95036 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5054 : :
5055 : : /* The concrete return type of __builtin_source_location is
5056 : : const std::source_location::__impl*, but we can't form the type
5057 : : at this point. So we initially declare it with an auto return
5058 : : type which we then "deduce" from require_deduced_type upon first use. */
5059 : 95036 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5060 : 95036 : decl = add_builtin_function ("__builtin_source_location",
5061 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5062 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5063 : 95036 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5064 : :
5065 : 95036 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5066 : : NULL_TREE);
5067 : 95036 : decl
5068 : 95036 : = add_builtin_function ("__builtin_is_corresponding_member",
5069 : : bool_vaftype,
5070 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5071 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5072 : 95036 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5073 : :
5074 : 95036 : decl
5075 : 95036 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5076 : : bool_vaftype,
5077 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5078 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5079 : 95036 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5080 : :
5081 : 95036 : integer_two_node = build_int_cst (NULL_TREE, 2);
5082 : :
5083 : : /* Guess at the initial static decls size. */
5084 : 95036 : vec_alloc (static_decls, 500);
5085 : :
5086 : : /* ... and keyed classes. */
5087 : 95036 : vec_alloc (keyed_classes, 100);
5088 : :
5089 : 95036 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5090 : 95036 : truthvalue_type_node = boolean_type_node;
5091 : 95036 : truthvalue_false_node = boolean_false_node;
5092 : 95036 : truthvalue_true_node = boolean_true_node;
5093 : :
5094 : 95036 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5095 : 95036 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5096 : 95036 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5097 : 95036 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5098 : : NULL_TREE);
5099 : :
5100 : : #if 0
5101 : : record_builtin_type (RID_MAX, NULL, string_type_node);
5102 : : #endif
5103 : :
5104 : 95036 : delta_type_node = ptrdiff_type_node;
5105 : 95036 : vtable_index_type = ptrdiff_type_node;
5106 : :
5107 : 95036 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5108 : 95036 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5109 : :
5110 : : /* Create the conversion operator marker. This operator's DECL_NAME
5111 : : is in the identifier table, so we can use identifier equality to
5112 : : find it. */
5113 : 95036 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5114 : : void_ftype);
5115 : :
5116 : : /* C++ extensions */
5117 : :
5118 : 95036 : unknown_type_node = make_node (LANG_TYPE);
5119 : 95036 : record_unknown_type (unknown_type_node, "unknown type");
5120 : :
5121 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5122 : 95036 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5123 : :
5124 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5125 : : result. */
5126 : 95036 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5127 : 95036 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5128 : :
5129 : 95036 : init_list_type_node = make_node (LANG_TYPE);
5130 : 95036 : record_unknown_type (init_list_type_node, "init list");
5131 : :
5132 : : /* Used when parsing to distinguish parameter-lists () and (void). */
5133 : 95036 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5134 : :
5135 : 95036 : {
5136 : : /* Make sure we get a unique function type, so we can give
5137 : : its pointer type a name. (This wins for gdb.) */
5138 : 95036 : tree vfunc_type = make_node (FUNCTION_TYPE);
5139 : 95036 : TREE_TYPE (vfunc_type) = integer_type_node;
5140 : 95036 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5141 : 95036 : layout_type (vfunc_type);
5142 : :
5143 : 95036 : vtable_entry_type = build_pointer_type (vfunc_type);
5144 : : }
5145 : 95036 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5146 : :
5147 : 95036 : vtbl_type_node
5148 : 95036 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5149 : 95036 : layout_type (vtbl_type_node);
5150 : 95036 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5151 : 95036 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5152 : 95036 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5153 : 95036 : layout_type (vtbl_ptr_type_node);
5154 : 95036 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5155 : :
5156 : 95036 : push_namespace (get_identifier ("__cxxabiv1"));
5157 : 95036 : abi_node = current_namespace;
5158 : 95036 : pop_namespace ();
5159 : :
5160 : 95036 : any_targ_node = make_node (LANG_TYPE);
5161 : 95036 : record_unknown_type (any_targ_node, "any type");
5162 : :
5163 : : /* Now, C++. */
5164 : 95036 : current_lang_name = lang_name_cplusplus;
5165 : :
5166 : 95036 : if (aligned_new_threshold > 1
5167 : 95036 : && !pow2p_hwi (aligned_new_threshold))
5168 : : {
5169 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
5170 : : aligned_new_threshold);
5171 : 0 : aligned_new_threshold = 1;
5172 : : }
5173 : 95036 : if (aligned_new_threshold == -1)
5174 : 115659 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5175 : 95036 : if (aligned_new_threshold == 1)
5176 : 74396 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5177 : :
5178 : : /* Ensure attribs.cc is initialized. */
5179 : 95036 : init_attributes ();
5180 : 95036 : cxx_init_operator_new_delete_decls ();
5181 : :
5182 : : /* C++-specific nullptr initialization. */
5183 : 95036 : if (abi_version_at_least (9))
5184 : 94865 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5185 : 95036 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5186 : :
5187 : 95036 : if (! supports_one_only ())
5188 : 0 : flag_weak = 0;
5189 : :
5190 : 95036 : abort_fndecl
5191 : 95036 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5192 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5193 : 95036 : if (flag_weak)
5194 : : /* If no definition is available, resolve references to NULL. */
5195 : 95006 : declare_weak (abort_fndecl);
5196 : :
5197 : : /* Perform other language dependent initializations. */
5198 : 95036 : init_class_processing ();
5199 : 95036 : init_rtti_processing ();
5200 : 95036 : init_template_processing ();
5201 : :
5202 : 95036 : if (flag_exceptions)
5203 : 93810 : init_exception_processing ();
5204 : :
5205 : 95036 : if (flag_contracts)
5206 : 153 : init_terminate_fn ();
5207 : :
5208 : 95036 : if (modules_p ())
5209 : 4289 : init_modules (parse_in);
5210 : :
5211 : 95036 : make_fname_decl = cp_make_fname_decl;
5212 : 95036 : start_fname_decls ();
5213 : :
5214 : : /* Show we use EH for cleanups. */
5215 : 95036 : if (flag_exceptions)
5216 : 93810 : using_eh_for_cleanups ();
5217 : :
5218 : : /* Check that the hardware interference sizes are at least
5219 : : alignof(max_align_t), as required by the standard. */
5220 : 95036 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5221 : 95036 : if (OPTION_SET_P (param_destruct_interfere_size))
5222 : : {
5223 : 0 : if (param_destruct_interfere_size < max_align)
5224 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5225 : : "%d", param_destruct_interfere_size, max_align);
5226 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5227 : 0 : warning (OPT_Winterference_size,
5228 : : "%<--param destructive-interference-size=%d%> "
5229 : : "is less than %<--param l1-cache-line-size=%d%>",
5230 : : param_destruct_interfere_size, param_l1_cache_line_size);
5231 : : }
5232 : 95036 : else if (param_destruct_interfere_size)
5233 : : /* Assume the internal value is OK. */;
5234 : 0 : else if (param_l1_cache_line_size >= max_align)
5235 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5236 : : /* else leave it unset. */
5237 : :
5238 : 95036 : if (OPTION_SET_P (param_construct_interfere_size))
5239 : : {
5240 : 0 : if (param_construct_interfere_size < max_align)
5241 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5242 : : "%d", param_construct_interfere_size, max_align);
5243 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5244 : 0 : && param_l1_cache_line_size >= max_align)
5245 : 0 : warning (OPT_Winterference_size,
5246 : : "%<--param constructive-interference-size=%d%> "
5247 : : "is greater than %<--param l1-cache-line-size=%d%>",
5248 : : param_construct_interfere_size, param_l1_cache_line_size);
5249 : : }
5250 : 95036 : else if (param_construct_interfere_size)
5251 : : /* Assume the internal value is OK. */;
5252 : 0 : else if (param_l1_cache_line_size >= max_align)
5253 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5254 : 95036 : }
5255 : :
5256 : : /* Enter an abi node in global-module context. returns a cookie to
5257 : : give to pop_abi_namespace. */
5258 : :
5259 : : unsigned
5260 : 59162 : push_abi_namespace (tree node)
5261 : : {
5262 : 59162 : push_nested_namespace (node);
5263 : 59162 : push_visibility ("default", 2);
5264 : 59162 : unsigned flags = module_kind;
5265 : 59162 : module_kind = 0;
5266 : 59162 : return flags;
5267 : : }
5268 : :
5269 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5270 : : you. */
5271 : :
5272 : : void
5273 : 59162 : pop_abi_namespace (unsigned flags, tree node)
5274 : : {
5275 : 59162 : module_kind = flags;
5276 : 59162 : pop_visibility (2);
5277 : 59162 : pop_nested_namespace (node);
5278 : 59162 : }
5279 : :
5280 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5281 : : the decl, LOC is the location to give the decl, NAME is the
5282 : : initialization string and TYPE_DEP indicates whether NAME depended
5283 : : on the type of the function. We make use of that to detect
5284 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5285 : : at the point of first use, so we mustn't push the decl now. */
5286 : :
5287 : : static tree
5288 : 112791 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5289 : : {
5290 : 112791 : tree domain = NULL_TREE;
5291 : 112791 : tree init = NULL_TREE;
5292 : :
5293 : 112791 : if (!(type_dep && current_function_decl && in_template_context))
5294 : : {
5295 : 34948 : const char *name = NULL;
5296 : 34948 : bool release_name = false;
5297 : :
5298 : 34948 : if (current_function_decl == NULL_TREE)
5299 : : name = "top level";
5300 : 34934 : else if (type_dep == 0)
5301 : : {
5302 : : /* __FUNCTION__ */
5303 : 605 : name = fname_as_string (type_dep);
5304 : 605 : release_name = true;
5305 : : }
5306 : : else
5307 : : {
5308 : : /* __PRETTY_FUNCTION__ */
5309 : 34329 : gcc_checking_assert (type_dep == 1);
5310 : 34329 : name = cxx_printable_name (current_function_decl, 2);
5311 : : }
5312 : :
5313 : 34948 : size_t length = strlen (name);
5314 : 34948 : domain = build_index_type (size_int (length));
5315 : 34948 : init = build_string (length + 1, name);
5316 : 34948 : if (release_name)
5317 : 605 : free (const_cast<char *> (name));
5318 : : }
5319 : :
5320 : 112791 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5321 : 112791 : type = build_cplus_array_type (type, domain);
5322 : :
5323 : 112791 : if (init)
5324 : 34948 : TREE_TYPE (init) = type;
5325 : : else
5326 : 77843 : init = error_mark_node;
5327 : :
5328 : 112791 : tree decl = build_decl (loc, VAR_DECL, id, type);
5329 : :
5330 : 112791 : TREE_READONLY (decl) = 1;
5331 : 112791 : DECL_ARTIFICIAL (decl) = 1;
5332 : 112791 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5333 : 112791 : TREE_STATIC (decl) = 1;
5334 : :
5335 : 112791 : TREE_USED (decl) = 1;
5336 : :
5337 : 112791 : SET_DECL_VALUE_EXPR (decl, init);
5338 : 112791 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5339 : : /* For decl_constant_var_p. */
5340 : 112791 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5341 : :
5342 : 112791 : if (current_function_decl)
5343 : : {
5344 : 112777 : DECL_CONTEXT (decl) = current_function_decl;
5345 : 112777 : decl = pushdecl_outermost_localscope (decl);
5346 : 112777 : if (decl != error_mark_node)
5347 : 112774 : add_decl_expr (decl);
5348 : : else
5349 : 3 : gcc_assert (seen_error ());
5350 : : }
5351 : : else
5352 : : {
5353 : 14 : DECL_THIS_STATIC (decl) = true;
5354 : 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5355 : : }
5356 : :
5357 : 112791 : return decl;
5358 : : }
5359 : :
5360 : : /* Install DECL as a builtin function at current global scope. Return
5361 : : the new decl (if we found an existing version). Also installs it
5362 : : into ::std, if it's not '_*'. */
5363 : :
5364 : : tree
5365 : 219393652 : cxx_builtin_function (tree decl)
5366 : : {
5367 : 219393652 : retrofit_lang_decl (decl);
5368 : :
5369 : 219393652 : DECL_ARTIFICIAL (decl) = 1;
5370 : 219393652 : SET_DECL_LANGUAGE (decl, lang_c);
5371 : : /* Runtime library routines are, by definition, available in an
5372 : : external shared object. */
5373 : 219393652 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5374 : 219393652 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5375 : :
5376 : 219393652 : tree id = DECL_NAME (decl);
5377 : 219393652 : const char *name = IDENTIFIER_POINTER (id);
5378 : 219393652 : bool hiding = false;
5379 : 219393652 : if (name[0] != '_' || name[1] != '_')
5380 : : /* In the user's namespace, it must be declared before use. */
5381 : : hiding = true;
5382 : 183992762 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5383 : 183992762 : && !startswith (name + 2, "builtin_")
5384 : 206101532 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5385 : : "_chk", strlen ("_chk") + 1))
5386 : : /* Treat __*_chk fortification functions as anticipated as well,
5387 : : unless they are __builtin_*_chk. */
5388 : : hiding = true;
5389 : :
5390 : : /* All builtins that don't begin with an '_' should additionally
5391 : : go in the 'std' namespace. */
5392 : 219393652 : if (name[0] != '_')
5393 : : {
5394 : 35271020 : tree std_decl = copy_decl (decl);
5395 : :
5396 : 35271020 : push_nested_namespace (std_node);
5397 : 35271020 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5398 : 35271020 : pushdecl (std_decl, hiding);
5399 : 35271020 : pop_nested_namespace (std_node);
5400 : : }
5401 : :
5402 : 219393652 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5403 : 219393652 : decl = pushdecl (decl, hiding);
5404 : :
5405 : 219393652 : return decl;
5406 : : }
5407 : :
5408 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5409 : : scope. This is to allow function specific options to add new machine
5410 : : dependent builtins when the target ISA changes via attribute((target(...)))
5411 : : which saves space on program startup if the program does not use non-generic
5412 : : ISAs. */
5413 : :
5414 : : tree
5415 : 626302 : cxx_builtin_function_ext_scope (tree decl)
5416 : : {
5417 : 626302 : push_nested_namespace (global_namespace);
5418 : 626302 : decl = cxx_builtin_function (decl);
5419 : 626302 : pop_nested_namespace (global_namespace);
5420 : :
5421 : 626302 : return decl;
5422 : : }
5423 : :
5424 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5425 : :
5426 : : tree
5427 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5428 : : {
5429 : 0 : retrofit_lang_decl (decl);
5430 : :
5431 : 0 : DECL_ARTIFICIAL (decl) = 1;
5432 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5433 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5434 : 0 : return pushdecl (decl);
5435 : : }
5436 : :
5437 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5438 : : function. Not called directly. */
5439 : :
5440 : : static tree
5441 : 1531060 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5442 : : int ecf_flags)
5443 : : {
5444 : 1531060 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5445 : 1531060 : DECL_EXTERNAL (fn) = 1;
5446 : 1531060 : TREE_PUBLIC (fn) = 1;
5447 : 1531060 : DECL_ARTIFICIAL (fn) = 1;
5448 : 1531060 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5449 : 1531060 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5450 : 1531060 : SET_DECL_LANGUAGE (fn, lang_c);
5451 : : /* Runtime library routines are, by definition, available in an
5452 : : external shared object. */
5453 : 1531060 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5454 : 1531060 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5455 : 1531060 : set_call_expr_flags (fn, ecf_flags);
5456 : 1531060 : return fn;
5457 : : }
5458 : :
5459 : : /* Returns the _DECL for a library function with C++ linkage. */
5460 : :
5461 : : static tree
5462 : 1071663 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5463 : : int ecf_flags)
5464 : : {
5465 : 1071663 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5466 : 1071663 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5467 : 1071663 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5468 : 1071663 : return fn;
5469 : : }
5470 : :
5471 : : /* Like build_library_fn, but takes a C string instead of an
5472 : : IDENTIFIER_NODE. */
5473 : :
5474 : : tree
5475 : 101097 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5476 : : {
5477 : 101097 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5478 : : }
5479 : :
5480 : : /* Like build_cp_library_fn, but takes a C string instead of an
5481 : : IDENTIFIER_NODE. */
5482 : :
5483 : : tree
5484 : 93901 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5485 : : {
5486 : 93901 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5487 : 93901 : ecf_flags);
5488 : : }
5489 : :
5490 : : /* Like build_library_fn, but also pushes the function so that we will
5491 : : be able to find it via get_global_binding. Also, the function
5492 : : may throw exceptions listed in RAISES. */
5493 : :
5494 : : tree
5495 : 358300 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5496 : : {
5497 : 358300 : if (raises)
5498 : 40160 : type = build_exception_variant (type, raises);
5499 : :
5500 : 358300 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5501 : 358300 : return pushdecl_top_level (fn);
5502 : : }
5503 : :
5504 : : /* Like build_cp_library_fn, but also pushes the function so that it
5505 : : will be found by normal lookup. */
5506 : :
5507 : : static tree
5508 : 977762 : push_cp_library_fn (enum tree_code operator_code, tree type,
5509 : : int ecf_flags)
5510 : : {
5511 : 977762 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5512 : : operator_code, type, ecf_flags);
5513 : 977762 : pushdecl (fn);
5514 : 977762 : if (flag_tm)
5515 : 3100 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
5516 : 977762 : return fn;
5517 : : }
5518 : :
5519 : : /* Like push_library_fn, but also note that this function throws
5520 : : and does not return. Used for __throw_foo and the like. */
5521 : :
5522 : : tree
5523 : 104786 : push_throw_library_fn (tree name, tree type)
5524 : : {
5525 : 104786 : tree fn = push_library_fn (name, type, NULL_TREE,
5526 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5527 : 104786 : return fn;
5528 : : }
5529 : :
5530 : : /* When we call finish_struct for an anonymous union, we create
5531 : : default copy constructors and such. But, an anonymous union
5532 : : shouldn't have such things; this function undoes the damage to the
5533 : : anonymous union type T.
5534 : :
5535 : : (The reason that we create the synthesized methods is that we don't
5536 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
5537 : : The first is an anonymous union; the second is just an ordinary
5538 : : union type.) */
5539 : :
5540 : : void
5541 : 92439 : fixup_anonymous_aggr (tree t)
5542 : : {
5543 : : /* Wipe out memory of synthesized methods. */
5544 : 92439 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5545 : 92439 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5546 : 92439 : TYPE_HAS_COPY_CTOR (t) = 0;
5547 : 92439 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5548 : 92439 : TYPE_HAS_COPY_ASSIGN (t) = 0;
5549 : 92439 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5550 : :
5551 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5552 : : invalid members. */
5553 : 495253 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5554 : : {
5555 : 402814 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5556 : 0 : *prev_p = DECL_CHAIN (probe);
5557 : : else
5558 : 402814 : prev_p = &DECL_CHAIN (probe);
5559 : :
5560 : 402814 : if (DECL_ARTIFICIAL (probe)
5561 : 402814 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5562 : 42352 : || TYPE_ANON_P (TREE_TYPE (probe))))
5563 : 138807 : continue;
5564 : :
5565 : 264007 : if (TREE_CODE (probe) != FIELD_DECL
5566 : 263953 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5567 : : {
5568 : : /* We already complained about static data members in
5569 : : finish_static_data_member_decl. */
5570 : 66 : if (!VAR_P (probe))
5571 : : {
5572 : 51 : auto_diagnostic_group d;
5573 : 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
5574 : 51 : TREE_CODE (t) == UNION_TYPE
5575 : : ? "%q#D invalid; an anonymous union may "
5576 : : "only have public non-static data members"
5577 : : : "%q#D invalid; an anonymous struct may "
5578 : : "only have public non-static data members", probe))
5579 : : {
5580 : 51 : static bool hint;
5581 : 51 : if (flag_permissive && !hint)
5582 : : {
5583 : 6 : hint = true;
5584 : 6 : inform (DECL_SOURCE_LOCATION (probe),
5585 : : "this flexibility is deprecated and will be "
5586 : : "removed");
5587 : : }
5588 : : }
5589 : 51 : }
5590 : : }
5591 : : }
5592 : :
5593 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5594 : 92439 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5595 : 92439 : unsigned store = 0;
5596 : 217434 : for (tree elt : vec)
5597 : 124995 : if (!is_overloaded_fn (elt))
5598 : 124974 : (*vec)[store++] = elt;
5599 : 92439 : vec_safe_truncate (vec, store);
5600 : :
5601 : : /* Wipe RTTI info. */
5602 : 92439 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5603 : :
5604 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5605 : : assignment operators (because they cannot have these methods themselves).
5606 : : For anonymous unions this is already checked because they are not allowed
5607 : : in any union, otherwise we have to check it. */
5608 : 92439 : if (TREE_CODE (t) != UNION_TYPE)
5609 : : {
5610 : 19842 : tree field, type;
5611 : :
5612 : 19842 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5613 : : {
5614 : 9 : error_at (location_of (t), "anonymous struct with base classes");
5615 : : /* Avoid ICE after error on anon-struct9.C. */
5616 : 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
5617 : : }
5618 : :
5619 : 89729 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5620 : 69887 : if (TREE_CODE (field) == FIELD_DECL)
5621 : : {
5622 : 49944 : type = TREE_TYPE (field);
5623 : 49944 : if (CLASS_TYPE_P (type))
5624 : : {
5625 : 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
5626 : 6 : error ("member %q+#D with constructor not allowed "
5627 : : "in anonymous aggregate", field);
5628 : 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5629 : 0 : error ("member %q+#D with destructor not allowed "
5630 : : "in anonymous aggregate", field);
5631 : 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5632 : 0 : error ("member %q+#D with copy assignment operator "
5633 : : "not allowed in anonymous aggregate", field);
5634 : : }
5635 : : }
5636 : : }
5637 : 92439 : }
5638 : :
5639 : : /* Warn for an attribute located at LOCATION that appertains to the
5640 : : class type CLASS_TYPE that has not been properly placed after its
5641 : : class-key, in it class-specifier. */
5642 : :
5643 : : void
5644 : 102 : warn_misplaced_attr_for_class_type (location_t location,
5645 : : tree class_type)
5646 : : {
5647 : 102 : gcc_assert (OVERLOAD_TYPE_P (class_type));
5648 : :
5649 : 102 : auto_diagnostic_group d;
5650 : 102 : if (warning_at (location, OPT_Wattributes,
5651 : : "attribute ignored in declaration "
5652 : : "of %q#T", class_type))
5653 : 102 : inform (location,
5654 : : "attribute for %q#T must follow the %qs keyword",
5655 : : class_type, class_key_or_enum_as_string (class_type));
5656 : 102 : }
5657 : :
5658 : : /* Returns the cv-qualifiers that apply to the type specified
5659 : : by the DECLSPECS. */
5660 : :
5661 : : static int
5662 : 898066072 : get_type_quals (const cp_decl_specifier_seq *declspecs)
5663 : : {
5664 : 898066072 : int type_quals = TYPE_UNQUALIFIED;
5665 : :
5666 : 898066072 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5667 : 98766780 : type_quals |= TYPE_QUAL_CONST;
5668 : 898066072 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5669 : 1553792 : type_quals |= TYPE_QUAL_VOLATILE;
5670 : 898066072 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5671 : 34 : type_quals |= TYPE_QUAL_RESTRICT;
5672 : :
5673 : 898066072 : return type_quals;
5674 : : }
5675 : :
5676 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
5677 : : just declares a tagged type or anonymous union.
5678 : :
5679 : : Returns the type declared; or NULL_TREE if none. */
5680 : :
5681 : : tree
5682 : 31744666 : check_tag_decl (cp_decl_specifier_seq *declspecs,
5683 : : bool explicit_type_instantiation_p)
5684 : : {
5685 : 31744666 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5686 : 31744666 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5687 : : /* If a class, struct, or enum type is declared by the DECLSPECS
5688 : : (i.e, if a class-specifier, enum-specifier, or non-typename
5689 : : elaborated-type-specifier appears in the DECLSPECS),
5690 : : DECLARED_TYPE is set to the corresponding type. */
5691 : 31744666 : tree declared_type = NULL_TREE;
5692 : 31744666 : bool error_p = false;
5693 : :
5694 : 31744666 : if (declspecs->multiple_types_p)
5695 : 26 : error_at (smallest_type_location (declspecs),
5696 : : "multiple types in one declaration");
5697 : 31744640 : else if (declspecs->redefined_builtin_type)
5698 : : {
5699 : 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5700 : 21 : if (!in_system_header_at (loc))
5701 : 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
5702 : : declspecs->redefined_builtin_type);
5703 : 21 : return NULL_TREE;
5704 : : }
5705 : :
5706 : 31744645 : if (declspecs->type
5707 : 31744630 : && TYPE_P (declspecs->type)
5708 : 63300915 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5709 : 31554818 : && MAYBE_CLASS_TYPE_P (declspecs->type))
5710 : 1622766 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5711 : : declared_type = declspecs->type;
5712 : 189869 : else if (declspecs->type == error_mark_node)
5713 : 1462 : error_p = true;
5714 : :
5715 : 31744645 : if (type_uses_auto (declared_type))
5716 : : {
5717 : 8 : error_at (declspecs->locations[ds_type_spec],
5718 : : "%<auto%> can only be specified for variables "
5719 : : "or function declarations");
5720 : 8 : return error_mark_node;
5721 : : }
5722 : :
5723 : 31744637 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5724 : : declared_type = NULL_TREE;
5725 : :
5726 : 31744637 : if (!declared_type && !saw_friend && !error_p)
5727 : 88 : permerror (input_location, "declaration does not declare anything");
5728 : : /* Check for an anonymous union. */
5729 : 31554741 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5730 : 91703932 : && TYPE_UNNAMED_P (declared_type))
5731 : : {
5732 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
5733 : : can be omitted only when declaring a class (clause 9) or
5734 : : enumeration (7.2), that is, when the decl-specifier-seq contains
5735 : : either a class-specifier, an elaborated-type-specifier with
5736 : : a class-key (9.1), or an enum-specifier. In these cases and
5737 : : whenever a class-specifier or enum-specifier is present in the
5738 : : decl-specifier-seq, the identifiers in these specifiers are among
5739 : : the names being declared by the declaration (as class-name,
5740 : : enum-names, or enumerators, depending on the syntax). In such
5741 : : cases, and except for the declaration of an unnamed bit-field (9.6),
5742 : : the decl-specifier-seq shall introduce one or more names into the
5743 : : program, or shall redeclare a name introduced by a previous
5744 : : declaration. [Example:
5745 : : enum { }; // ill-formed
5746 : : typedef class { }; // ill-formed
5747 : : --end example] */
5748 : 92445 : if (saw_typedef)
5749 : : {
5750 : 6 : error_at (declspecs->locations[ds_typedef],
5751 : : "missing type-name in typedef-declaration");
5752 : 6 : return NULL_TREE;
5753 : : }
5754 : 92439 : /* Anonymous unions are objects, so they can have specifiers. */;
5755 : 92439 : SET_ANON_AGGR_TYPE_P (declared_type);
5756 : :
5757 : 92439 : if (TREE_CODE (declared_type) != UNION_TYPE)
5758 : 19842 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5759 : 19842 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5760 : : }
5761 : :
5762 : : else
5763 : : {
5764 : 31652104 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5765 : 9 : error_at (declspecs->locations[ds_inline],
5766 : : "%<inline%> can only be specified for functions");
5767 : 31652095 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5768 : 3 : error_at (declspecs->locations[ds_virtual],
5769 : : "%<virtual%> can only be specified for functions");
5770 : 31652092 : else if (saw_friend
5771 : 31652092 : && (!current_class_type
5772 : 1643890 : || current_scope () != current_class_type))
5773 : 0 : error_at (declspecs->locations[ds_friend],
5774 : : "%<friend%> can only be specified inside a class");
5775 : 31652092 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5776 : 3 : error_at (declspecs->locations[ds_explicit],
5777 : : "%<explicit%> can only be specified for constructors");
5778 : 31652089 : else if (declspecs->storage_class)
5779 : 3 : error_at (declspecs->locations[ds_storage_class],
5780 : : "a storage class can only be specified for objects "
5781 : : "and functions");
5782 : 31652086 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5783 : 9 : error_at (declspecs->locations[ds_const],
5784 : : "%<const%> can only be specified for objects and "
5785 : : "functions");
5786 : 31652077 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5787 : 3 : error_at (declspecs->locations[ds_volatile],
5788 : : "%<volatile%> can only be specified for objects and "
5789 : : "functions");
5790 : 31652074 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5791 : 3 : error_at (declspecs->locations[ds_restrict],
5792 : : "%<__restrict%> can only be specified for objects and "
5793 : : "functions");
5794 : 31652071 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5795 : 3 : error_at (declspecs->locations[ds_thread],
5796 : : "%<__thread%> can only be specified for objects "
5797 : : "and functions");
5798 : 31652068 : else if (saw_typedef)
5799 : 24 : warning_at (declspecs->locations[ds_typedef], 0,
5800 : : "%<typedef%> was ignored in this declaration");
5801 : 31652044 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5802 : 9 : error_at (declspecs->locations[ds_constexpr],
5803 : : "%qs cannot be used for type declarations", "constexpr");
5804 : 31652035 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5805 : 3 : error_at (declspecs->locations[ds_constinit],
5806 : : "%qs cannot be used for type declarations", "constinit");
5807 : 31652032 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5808 : 3 : error_at (declspecs->locations[ds_consteval],
5809 : : "%qs cannot be used for type declarations", "consteval");
5810 : : }
5811 : :
5812 : 31744631 : if (declspecs->attributes && warn_attributes && declared_type)
5813 : : {
5814 : 31 : location_t loc;
5815 : 25 : if (!CLASS_TYPE_P (declared_type)
5816 : 56 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5817 : : /* For a non-template class, use the name location. */
5818 : 22 : loc = location_of (declared_type);
5819 : : else
5820 : : /* For a template class (an explicit instantiation), use the
5821 : : current location. */
5822 : 9 : loc = input_location;
5823 : :
5824 : 31 : if (explicit_type_instantiation_p)
5825 : : /* [dcl.attr.grammar]/4:
5826 : :
5827 : : No attribute-specifier-seq shall appertain to an explicit
5828 : : instantiation. */
5829 : : {
5830 : 6 : auto_diagnostic_group d;
5831 : 6 : if (warning_at (loc, OPT_Wattributes,
5832 : : "attribute ignored in explicit instantiation %q#T",
5833 : : declared_type))
5834 : 6 : inform (loc,
5835 : : "no attribute can be applied to "
5836 : : "an explicit instantiation");
5837 : 6 : }
5838 : : else
5839 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
5840 : : }
5841 : :
5842 : 31744631 : if (declspecs->std_attributes
5843 : 77 : && declared_type
5844 : 31744705 : && any_nonignored_attribute_p (declspecs->std_attributes))
5845 : : {
5846 : 74 : auto_diagnostic_group d;
5847 : 74 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
5848 : : "attribute ignored"))
5849 : 74 : inform (declspecs->locations[ds_std_attribute],
5850 : : "an attribute that appertains to a type-specifier is ignored");
5851 : 74 : }
5852 : :
5853 : : /* Diagnose invalid application of contracts, if any. */
5854 : 31744631 : if (find_contract (declspecs->attributes))
5855 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
5856 : : else
5857 : 31744630 : diagnose_misapplied_contracts (declspecs->std_attributes);
5858 : :
5859 : : return declared_type;
5860 : : }
5861 : :
5862 : : /* Called when a declaration is seen that contains no names to declare.
5863 : : If its type is a reference to a structure, union or enum inherited
5864 : : from a containing scope, shadow that tag name for the current scope
5865 : : with a forward reference.
5866 : : If its type defines a new named structure or union
5867 : : or defines an enum, it is valid but we need not do anything here.
5868 : : Otherwise, it is an error.
5869 : :
5870 : : C++: may have to grok the declspecs to learn about static,
5871 : : complain for anonymous unions.
5872 : :
5873 : : Returns the TYPE declared -- or NULL_TREE if none. */
5874 : :
5875 : : tree
5876 : 27818663 : shadow_tag (cp_decl_specifier_seq *declspecs)
5877 : : {
5878 : 27818663 : tree t = check_tag_decl (declspecs,
5879 : : /*explicit_type_instantiation_p=*/false);
5880 : :
5881 : 27818663 : if (!t)
5882 : : return NULL_TREE;
5883 : :
5884 : 27817191 : t = maybe_process_partial_specialization (t);
5885 : 27817191 : if (t == error_mark_node)
5886 : : return NULL_TREE;
5887 : :
5888 : : /* This is where the variables in an anonymous union are
5889 : : declared. An anonymous union declaration looks like:
5890 : : union { ... } ;
5891 : : because there is no declarator after the union, the parser
5892 : : sends that declaration here. */
5893 : 27816039 : if (ANON_AGGR_TYPE_P (t))
5894 : : {
5895 : 245 : fixup_anonymous_aggr (t);
5896 : :
5897 : 245 : if (TYPE_FIELDS (t))
5898 : : {
5899 : 245 : tree decl = grokdeclarator (/*declarator=*/NULL,
5900 : : declspecs, NORMAL, 0, NULL);
5901 : 245 : finish_anon_union (decl);
5902 : : }
5903 : : }
5904 : :
5905 : : return t;
5906 : : }
5907 : :
5908 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5909 : :
5910 : : tree
5911 : 333479937 : groktypename (cp_decl_specifier_seq *type_specifiers,
5912 : : const cp_declarator *declarator,
5913 : : bool is_template_arg)
5914 : : {
5915 : 333479937 : tree attrs;
5916 : 333479937 : tree type;
5917 : 52115009 : enum decl_context context
5918 : 333479937 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5919 : 333479937 : attrs = type_specifiers->attributes;
5920 : 333479937 : type_specifiers->attributes = NULL_TREE;
5921 : 333479937 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5922 : 333479937 : if (attrs && type != error_mark_node)
5923 : : {
5924 : 581 : if (CLASS_TYPE_P (type))
5925 : 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5926 : : "outside of definition", type);
5927 : 575 : else if (MAYBE_CLASS_TYPE_P (type))
5928 : : /* A template type parameter or other dependent type. */
5929 : 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5930 : : "type %qT without an associated declaration", type);
5931 : : else
5932 : 566 : cplus_decl_attributes (&type, attrs, 0);
5933 : : }
5934 : 333479937 : return type;
5935 : : }
5936 : :
5937 : : /* Process a DECLARATOR for a function-scope or namespace-scope
5938 : : variable or function declaration.
5939 : : (Function definitions go through start_function; class member
5940 : : declarations appearing in the body of the class go through
5941 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
5942 : : If an error occurs, the error_mark_node is returned instead.
5943 : :
5944 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5945 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5946 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
5947 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5948 : : implicitly initialized via a default constructor. It can also be
5949 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5950 : : mark the new decl as DECL_DECOMPOSITION_P.
5951 : :
5952 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5953 : : declaration.
5954 : :
5955 : : The scope represented by the context of the returned DECL is pushed
5956 : : (if it is not the global namespace) and is assigned to
5957 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
5958 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
5959 : :
5960 : : tree
5961 : 110367650 : start_decl (const cp_declarator *declarator,
5962 : : cp_decl_specifier_seq *declspecs,
5963 : : int initialized,
5964 : : tree attributes,
5965 : : tree prefix_attributes,
5966 : : tree *pushed_scope_p)
5967 : : {
5968 : 110367650 : tree decl;
5969 : 110367650 : tree context;
5970 : 110367650 : bool was_public;
5971 : 110367650 : int flags;
5972 : 110367650 : bool alias;
5973 : 110367650 : tree initial;
5974 : :
5975 : 110367650 : *pushed_scope_p = NULL_TREE;
5976 : :
5977 : 110367650 : if (prefix_attributes != error_mark_node)
5978 : 110367648 : attributes = attr_chainon (attributes, prefix_attributes);
5979 : :
5980 : 110367650 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5981 : : &attributes);
5982 : :
5983 : 110367650 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
5984 : 110367650 : || decl == error_mark_node
5985 : 110365533 : || prefix_attributes == error_mark_node)
5986 : 2119 : return error_mark_node;
5987 : :
5988 : 110365531 : context = CP_DECL_CONTEXT (decl);
5989 : 110365531 : if (context != global_namespace)
5990 : 17225376 : *pushed_scope_p = push_scope (context);
5991 : :
5992 : 110365531 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
5993 : : {
5994 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
5995 : : "typedef %qD is initialized (use %qs instead)",
5996 : : decl, "decltype");
5997 : 9 : return error_mark_node;
5998 : : }
5999 : :
6000 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6001 : : with attribute validation. */
6002 : 110365522 : initial = DECL_INITIAL (decl);
6003 : :
6004 : 110365522 : if (initialized)
6005 : : {
6006 : 48873756 : if (! toplevel_bindings_p ()
6007 : 48873756 : && DECL_EXTERNAL (decl))
6008 : 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6009 : : decl);
6010 : 48873756 : DECL_EXTERNAL (decl) = 0;
6011 : 48873756 : if (toplevel_bindings_p ())
6012 : 4212816 : TREE_STATIC (decl) = 1;
6013 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6014 : : even though we might not yet have the initializer expression. */
6015 : 48873756 : if (!DECL_INITIAL (decl))
6016 : 48529981 : DECL_INITIAL (decl) = error_mark_node;
6017 : : }
6018 : 110365522 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6019 : :
6020 : 110365522 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6021 : 3861 : record_key_method_defined (decl);
6022 : :
6023 : : /* If this is a typedef that names the class for linkage purposes
6024 : : (7.1.3p8), apply any attributes directly to the type. */
6025 : 110365522 : if (TREE_CODE (decl) == TYPE_DECL
6026 : 19585281 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6027 : 118018612 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6028 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
6029 : : else
6030 : : flags = 0;
6031 : :
6032 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
6033 : 110365522 : cplus_decl_attributes (&decl, attributes, flags);
6034 : :
6035 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6036 : : assist with attribute validation. */
6037 : 110365522 : DECL_INITIAL (decl) = initial;
6038 : :
6039 : : /* Dllimported symbols cannot be defined. Static data members (which
6040 : : can be initialized in-class and dllimported) go through grokfield,
6041 : : not here, so we don't need to exclude those decls when checking for
6042 : : a definition. */
6043 : 110365522 : if (initialized && DECL_DLLIMPORT_P (decl))
6044 : : {
6045 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
6046 : : "definition of %q#D is marked %<dllimport%>", decl);
6047 : 0 : DECL_DLLIMPORT_P (decl) = 0;
6048 : : }
6049 : :
6050 : : /* If #pragma weak was used, mark the decl weak now. */
6051 : 110365522 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6052 : 41791173 : maybe_apply_pragma_weak (decl);
6053 : :
6054 : 110365522 : if (TREE_CODE (decl) == FUNCTION_DECL
6055 : 36133927 : && DECL_DECLARED_INLINE_P (decl)
6056 : 1907337 : && DECL_UNINLINABLE (decl)
6057 : 110365522 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6058 : : {
6059 : 0 : auto_urlify_attributes sentinel;
6060 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6061 : : "inline function %qD given attribute %qs", decl, "noinline");
6062 : 0 : }
6063 : :
6064 : 110365522 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6065 : : {
6066 : 743856 : bool this_tmpl = (current_template_depth
6067 : 743856 : > template_class_depth (context));
6068 : 743856 : if (VAR_P (decl))
6069 : : {
6070 : 381159 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6071 : 381159 : if (field == NULL_TREE
6072 : 381159 : || !(VAR_P (field) || variable_template_p (field)))
6073 : 7 : error ("%q+#D is not a static data member of %q#T", decl, context);
6074 : 381152 : else if (variable_template_p (field)
6075 : 381152 : && (DECL_LANG_SPECIFIC (decl)
6076 : 111 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6077 : : /* OK, specialization was already checked. */;
6078 : 381089 : else if (variable_template_p (field) && !this_tmpl)
6079 : : {
6080 : 3 : auto_diagnostic_group d;
6081 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6082 : : "non-member-template declaration of %qD", decl);
6083 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6084 : : "member template declaration here");
6085 : 3 : return error_mark_node;
6086 : 3 : }
6087 : : else
6088 : : {
6089 : 381086 : if (variable_template_p (field))
6090 : 45 : field = DECL_TEMPLATE_RESULT (field);
6091 : :
6092 : 381086 : if (DECL_CONTEXT (field) != context)
6093 : : {
6094 : 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6095 : 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6096 : : "to be defined as %<%T::%D%>",
6097 : 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6098 : 3 : context, DECL_NAME (decl));
6099 : 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6100 : : }
6101 : : /* Static data member are tricky; an in-class initialization
6102 : : still doesn't provide a definition, so the in-class
6103 : : declaration will have DECL_EXTERNAL set, but will have an
6104 : : initialization. Thus, duplicate_decls won't warn
6105 : : about this situation, and so we check here. */
6106 : 381086 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6107 : 9 : error ("duplicate initialization of %qD", decl);
6108 : 381086 : field = duplicate_decls (decl, field);
6109 : 381086 : if (field == error_mark_node)
6110 : : return error_mark_node;
6111 : 381030 : else if (field)
6112 : 381030 : decl = field;
6113 : : }
6114 : : }
6115 : : else
6116 : : {
6117 : 362710 : tree field = check_classfn (context, decl,
6118 : : this_tmpl
6119 : 13 : ? current_template_parms
6120 : : : NULL_TREE);
6121 : 362625 : if (field && field != error_mark_node
6122 : 725322 : && duplicate_decls (decl, field))
6123 : 362624 : decl = field;
6124 : : }
6125 : :
6126 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6127 : 743797 : DECL_IN_AGGR_P (decl) = 0;
6128 : : /* Do not mark DECL as an explicit specialization if it was not
6129 : : already marked as an instantiation; a declaration should
6130 : : never be marked as a specialization unless we know what
6131 : : template is being specialized. */
6132 : 743797 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6133 : : {
6134 : 361131 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6135 : 361131 : if (TREE_CODE (decl) == FUNCTION_DECL)
6136 : 340196 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6137 : 340196 : && DECL_DECLARED_INLINE_P (decl));
6138 : : else
6139 : 20935 : DECL_COMDAT (decl) = false;
6140 : :
6141 : : /* [temp.expl.spec] An explicit specialization of a static data
6142 : : member of a template is a definition if the declaration
6143 : : includes an initializer; otherwise, it is a declaration.
6144 : :
6145 : : We check for processing_specialization so this only applies
6146 : : to the new specialization syntax. */
6147 : 361131 : if (!initialized && processing_specialization)
6148 : 360938 : DECL_EXTERNAL (decl) = 1;
6149 : : }
6150 : :
6151 : 1104804 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6152 : : /* Aliases are definitions. */
6153 : 743854 : && !alias)
6154 : : {
6155 : 30 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6156 : 11 : permerror (declarator->id_loc,
6157 : : "declaration of %q#D outside of class is not definition",
6158 : : decl);
6159 : 19 : else if (flag_contract_strict_declarations)
6160 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6161 : : "declaration of %q#D outside of class is not definition",
6162 : : decl);
6163 : : }
6164 : : }
6165 : :
6166 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6167 : 110365463 : if (initialized == SD_DECOMPOSITION)
6168 : 156822 : fit_decomposition_lang_decl (decl, NULL_TREE);
6169 : :
6170 : 110365463 : was_public = TREE_PUBLIC (decl);
6171 : :
6172 : 184364930 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6173 : 110709256 : && current_function_decl)
6174 : : {
6175 : : /* A function-scope decl of some namespace-scope decl. */
6176 : 36959 : DECL_LOCAL_DECL_P (decl) = true;
6177 : 36959 : if (named_module_attach_p ())
6178 : 3 : error_at (declarator->id_loc,
6179 : : "block-scope extern declaration %q#D must not be"
6180 : : " attached to a named module", decl);
6181 : : }
6182 : :
6183 : : /* Enter this declaration into the symbol table. Don't push the plain
6184 : : VAR_DECL for a variable template. */
6185 : 110365463 : if (!template_parm_scope_p ()
6186 : 110365463 : || !VAR_P (decl))
6187 : 107671248 : decl = maybe_push_decl (decl);
6188 : :
6189 : 110365463 : if (processing_template_decl)
6190 : 68568496 : decl = push_template_decl (decl);
6191 : :
6192 : 110365463 : if (decl == error_mark_node)
6193 : : return error_mark_node;
6194 : :
6195 : 110365081 : if (VAR_P (decl)
6196 : 54646055 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6197 : 65603 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6198 : : /* But not templated variables. */
6199 : 110392104 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6200 : : {
6201 : : /* This is a const variable with implicit 'static'. Set
6202 : : DECL_THIS_STATIC so we can tell it from variables that are
6203 : : !TREE_PUBLIC because of the anonymous namespace. */
6204 : 27023 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6205 : 27023 : DECL_THIS_STATIC (decl) = 1;
6206 : : }
6207 : :
6208 : 60900149 : if (current_function_decl && VAR_P (decl)
6209 : 49496850 : && maybe_constexpr_fn (current_function_decl)
6210 : 115874630 : && cxx_dialect < cxx23)
6211 : : {
6212 : 2744300 : bool ok = false;
6213 : 2744300 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6214 : : {
6215 : 10 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6216 : 20 : error_at (DECL_SOURCE_LOCATION (decl),
6217 : : "%qD defined %<thread_local%> in %qs function only "
6218 : : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6219 : 10 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6220 : : ? "consteval" : "constexpr");
6221 : : }
6222 : 2744290 : else if (TREE_STATIC (decl))
6223 : : {
6224 : 52 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6225 : 76 : error_at (DECL_SOURCE_LOCATION (decl),
6226 : : "%qD defined %<static%> in %qs function only available "
6227 : : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6228 : 38 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6229 : : ? "consteval" : "constexpr");
6230 : : }
6231 : : else
6232 : : ok = true;
6233 : 48 : if (!ok)
6234 : 62 : cp_function_chain->invalid_constexpr = true;
6235 : : }
6236 : :
6237 : 110365081 : if (!processing_template_decl && VAR_P (decl))
6238 : 5295743 : start_decl_1 (decl, initialized);
6239 : :
6240 : 110365078 : return decl;
6241 : : }
6242 : :
6243 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6244 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6245 : : variable is initialized via an implicitly-called constructor.)
6246 : : This function must be called for ordinary variables (including, for
6247 : : example, implicit instantiations of templates), but must not be
6248 : : called for template declarations. */
6249 : :
6250 : : void
6251 : 5303796 : start_decl_1 (tree decl, bool initialized)
6252 : : {
6253 : 5303796 : gcc_checking_assert (!processing_template_decl);
6254 : :
6255 : 5303796 : if (error_operand_p (decl))
6256 : : return;
6257 : :
6258 : 5303781 : gcc_checking_assert (VAR_P (decl));
6259 : :
6260 : 5303781 : tree type = TREE_TYPE (decl);
6261 : 5303781 : bool complete_p = COMPLETE_TYPE_P (type);
6262 : 5303781 : bool aggregate_definition_p
6263 : 5303781 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6264 : :
6265 : : /* If an explicit initializer is present, or if this is a definition
6266 : : of an aggregate, then we need a complete type at this point.
6267 : : (Scalars are always complete types, so there is nothing to
6268 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6269 : : are issued below. */
6270 : 3325085 : if ((initialized || aggregate_definition_p)
6271 : 4308953 : && !complete_p
6272 : 4234926 : && COMPLETE_TYPE_P (complete_type (type)))
6273 : : {
6274 : 332337 : complete_p = true;
6275 : : /* We will not yet have set TREE_READONLY on DECL if the type
6276 : : was "const", but incomplete, before this point. But, now, we
6277 : : have a complete type, so we can try again. */
6278 : 332337 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6279 : : }
6280 : :
6281 : 5303778 : if (initialized)
6282 : : /* Is it valid for this decl to have an initializer at all? */
6283 : : {
6284 : : /* Don't allow initializations for incomplete types except for
6285 : : arrays which might be completed by the initialization. */
6286 : 3687694 : if (complete_p)
6287 : : ; /* A complete type is ok. */
6288 : 577277 : else if (type_uses_auto (type))
6289 : : ; /* An auto type is ok. */
6290 : 63616 : else if (TREE_CODE (type) != ARRAY_TYPE)
6291 : : {
6292 : 24 : auto_diagnostic_group d;
6293 : 24 : error ("variable %q#D has initializer but incomplete type", decl);
6294 : 42 : maybe_suggest_missing_header (input_location,
6295 : 24 : TYPE_IDENTIFIER (type),
6296 : 24 : CP_TYPE_CONTEXT (type));
6297 : 24 : type = TREE_TYPE (decl) = error_mark_node;
6298 : 24 : }
6299 : 63592 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6300 : : {
6301 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6302 : 0 : error ("elements of array %q#D have incomplete type", decl);
6303 : : /* else we already gave an error in start_decl. */
6304 : : }
6305 : : }
6306 : 1616084 : else if (aggregate_definition_p && !complete_p)
6307 : : {
6308 : 224 : if (type_uses_auto (type))
6309 : 64 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6310 : : else
6311 : : {
6312 : 160 : auto_diagnostic_group d;
6313 : 160 : error ("aggregate %q#D has incomplete type and cannot be defined",
6314 : : decl);
6315 : 275 : maybe_suggest_missing_header (input_location,
6316 : 160 : TYPE_IDENTIFIER (type),
6317 : 160 : CP_TYPE_CONTEXT (type));
6318 : : /* Change the type so that assemble_variable will give
6319 : : DECL an rtl we can live with: (mem (const_int 0)). */
6320 : 160 : type = TREE_TYPE (decl) = error_mark_node;
6321 : 160 : }
6322 : : }
6323 : :
6324 : : /* Create a new scope to hold this declaration if necessary.
6325 : : Whether or not a new scope is necessary cannot be determined
6326 : : until after the type has been completed; if the type is a
6327 : : specialization of a class template it is not until after
6328 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6329 : : will be set correctly. */
6330 : 5303778 : maybe_push_cleanup_level (type);
6331 : : }
6332 : :
6333 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6334 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6335 : :
6336 : : tree
6337 : 239 : do_aggregate_paren_init (tree init, tree type)
6338 : : {
6339 : 239 : tree val = TREE_VALUE (init);
6340 : :
6341 : 239 : if (TREE_CHAIN (init) == NULL_TREE)
6342 : : {
6343 : : /* If the list has a single element and it's a string literal,
6344 : : then it's the initializer for the array as a whole. */
6345 : 124 : if (TREE_CODE (type) == ARRAY_TYPE
6346 : 124 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6347 : 155 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6348 : : == STRING_CST)
6349 : : return val;
6350 : : /* Handle non-standard extensions like compound literals. This also
6351 : : prevents triggering aggregate parenthesized-initialization in
6352 : : compiler-generated code for =default. */
6353 : 97 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6354 : 97 : TREE_TYPE (val)))
6355 : : return val;
6356 : : }
6357 : :
6358 : 187 : init = build_constructor_from_list (init_list_type_node, init);
6359 : 187 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6360 : 187 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6361 : 187 : return init;
6362 : : }
6363 : :
6364 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6365 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6366 : : but will be set to a new CLEANUP_STMT if a temporary is created
6367 : : that must be destroyed subsequently.
6368 : :
6369 : : Returns an initializer expression to use to initialize DECL, or
6370 : : NULL if the initialization can be performed statically.
6371 : :
6372 : : Quotes on semantics can be found in ARM 8.4.3. */
6373 : :
6374 : : static tree
6375 : 308447 : grok_reference_init (tree decl, tree type, tree init, int flags)
6376 : : {
6377 : 308447 : if (init == NULL_TREE)
6378 : : {
6379 : 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6380 : 3 : || DECL_IN_AGGR_P (decl) == 0)
6381 : 18 : && ! DECL_THIS_EXTERN (decl))
6382 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
6383 : : "%qD declared as reference but not initialized", decl);
6384 : 15 : return NULL_TREE;
6385 : : }
6386 : :
6387 : 308432 : tree ttype = TREE_TYPE (type);
6388 : 308432 : if (TREE_CODE (init) == TREE_LIST)
6389 : : {
6390 : : /* This handles (C++20 only) code like
6391 : :
6392 : : const A& r(1, 2, 3);
6393 : :
6394 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6395 : 1211 : if (TREE_TYPE (init) == NULL_TREE
6396 : 1211 : && CP_AGGREGATE_TYPE_P (ttype)
6397 : 54 : && !DECL_DECOMPOSITION_P (decl)
6398 : 1262 : && (cxx_dialect >= cxx20))
6399 : : {
6400 : : /* We don't know yet if we should treat const A& r(1) as
6401 : : const A& r{1}. */
6402 : 33 : if (list_length (init) == 1)
6403 : : {
6404 : 12 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6405 : 12 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6406 : : tf_warning_or_error);
6407 : : }
6408 : : /* If the list had more than one element, the code is ill-formed
6409 : : pre-C++20, so we can build a constructor right away. */
6410 : : else
6411 : 21 : init = do_aggregate_paren_init (init, ttype);
6412 : : }
6413 : : else
6414 : 1178 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6415 : : tf_warning_or_error);
6416 : : }
6417 : :
6418 : 308432 : if (TREE_CODE (ttype) != ARRAY_TYPE
6419 : 308432 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6420 : : /* Note: default conversion is only called in very special cases. */
6421 : 12 : init = decay_conversion (init, tf_warning_or_error);
6422 : :
6423 : : /* check_initializer handles this for non-reference variables, but for
6424 : : references we need to do it here or the initializer will get the
6425 : : incomplete array type and confuse later calls to
6426 : : cp_complete_array_type. */
6427 : 308432 : if (TREE_CODE (ttype) == ARRAY_TYPE
6428 : 7777 : && TYPE_DOMAIN (ttype) == NULL_TREE
6429 : 308515 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6430 : 50 : || TREE_CODE (init) == STRING_CST))
6431 : : {
6432 : 33 : cp_complete_array_type (&ttype, init, false);
6433 : 33 : if (ttype != TREE_TYPE (type))
6434 : 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6435 : : }
6436 : :
6437 : : /* Convert INIT to the reference type TYPE. This may involve the
6438 : : creation of a temporary, whose lifetime must be the same as that
6439 : : of the reference. If so, a DECL_EXPR for the temporary will be
6440 : : added just after the DECL_EXPR for DECL. That's why we don't set
6441 : : DECL_INITIAL for local references (instead assigning to them
6442 : : explicitly); we need to allow the temporary to be initialized
6443 : : first. */
6444 : 308432 : return initialize_reference (type, init, flags,
6445 : 308432 : tf_warning_or_error);
6446 : : }
6447 : :
6448 : : /* Designated initializers in arrays are not supported in GNU C++.
6449 : : The parser cannot detect this error since it does not know whether
6450 : : a given brace-enclosed initializer is for a class type or for an
6451 : : array. This function checks that CE does not use a designated
6452 : : initializer. If it does, an error is issued. Returns true if CE
6453 : : is valid, i.e., does not have a designated initializer. */
6454 : :
6455 : : bool
6456 : 46545809 : check_array_designated_initializer (constructor_elt *ce,
6457 : : unsigned HOST_WIDE_INT index)
6458 : : {
6459 : : /* Designated initializers for array elements are not supported. */
6460 : 46545809 : if (ce->index)
6461 : : {
6462 : : /* The parser only allows identifiers as designated
6463 : : initializers. */
6464 : 30107955 : if (ce->index == error_mark_node)
6465 : : {
6466 : 0 : error ("name used in a GNU-style designated "
6467 : : "initializer for an array");
6468 : 0 : return false;
6469 : : }
6470 : 30107955 : else if (identifier_p (ce->index))
6471 : : {
6472 : 17 : error ("name %qD used in a GNU-style designated "
6473 : : "initializer for an array", ce->index);
6474 : 17 : return false;
6475 : : }
6476 : :
6477 : 30107938 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6478 : : ce->index, true);
6479 : 30107938 : if (ce_index
6480 : 30107935 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6481 : 60215870 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6482 : : == INTEGER_CST))
6483 : : {
6484 : : /* A C99 designator is OK if it matches the current index. */
6485 : 30107932 : if (wi::to_wide (ce_index) == index)
6486 : : {
6487 : 30107926 : ce->index = ce_index;
6488 : 30107926 : return true;
6489 : : }
6490 : : else
6491 : 6 : sorry ("non-trivial designated initializers not supported");
6492 : : }
6493 : : else
6494 : 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
6495 : : "C99 designator %qE is not an integral constant-expression",
6496 : : ce->index);
6497 : :
6498 : 12 : return false;
6499 : : }
6500 : :
6501 : : return true;
6502 : : }
6503 : :
6504 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
6505 : : array until we finish parsing the initializer. If that's the
6506 : : situation we're in, update DECL accordingly. */
6507 : :
6508 : : static void
6509 : 54256093 : maybe_deduce_size_from_array_init (tree decl, tree init)
6510 : : {
6511 : 54256093 : tree type = TREE_TYPE (decl);
6512 : :
6513 : 54256093 : if (TREE_CODE (type) == ARRAY_TYPE
6514 : 780704 : && TYPE_DOMAIN (type) == NULL_TREE
6515 : 54758446 : && TREE_CODE (decl) != TYPE_DECL)
6516 : : {
6517 : : /* do_default is really a C-ism to deal with tentative definitions.
6518 : : But let's leave it here to ease the eventual merge. */
6519 : 502353 : int do_default = !DECL_EXTERNAL (decl);
6520 : 502353 : tree initializer = init ? init : DECL_INITIAL (decl);
6521 : 502353 : int failure = 0;
6522 : :
6523 : : /* Check that there are no designated initializers in INIT, as
6524 : : those are not supported in GNU C++, and as the middle-end
6525 : : will crash if presented with a non-numeric designated
6526 : : initializer. */
6527 : 502353 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6528 : : {
6529 : 91872 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6530 : 91872 : constructor_elt *ce;
6531 : 91872 : HOST_WIDE_INT i, j = 0;
6532 : 14796800 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6533 : : {
6534 : 14706376 : if (instantiation_dependent_expression_p (ce->index))
6535 : 54256093 : return;
6536 : 14706370 : if (!check_array_designated_initializer (ce, j))
6537 : 2 : failure = 1;
6538 : : /* If an un-designated initializer is type-dependent, we can't
6539 : : check brace elision yet. */
6540 : 14706370 : if (ce->index == NULL_TREE
6541 : 14706370 : && type_dependent_expression_p (ce->value))
6542 : : return;
6543 : 14704928 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
6544 : 193 : j += RAW_DATA_LENGTH (ce->value);
6545 : : else
6546 : 14704735 : ++j;
6547 : : }
6548 : : }
6549 : :
6550 : 90424 : if (failure)
6551 : 2 : TREE_TYPE (decl) = error_mark_node;
6552 : : else
6553 : : {
6554 : 500903 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6555 : : do_default);
6556 : 500903 : if (failure == 1)
6557 : : {
6558 : 33 : error_at (cp_expr_loc_or_loc (initializer,
6559 : 30 : DECL_SOURCE_LOCATION (decl)),
6560 : : "initializer fails to determine size of %qD", decl);
6561 : : }
6562 : 500873 : else if (failure == 2)
6563 : : {
6564 : 30 : if (do_default)
6565 : : {
6566 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6567 : : "array size missing in %qD", decl);
6568 : : }
6569 : : /* If a `static' var's size isn't known, make it extern as
6570 : : well as static, so it does not get allocated. If it's not
6571 : : `static', then don't mark it extern; finish_incomplete_decl
6572 : : will give it a default size and it will get allocated. */
6573 : 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6574 : 0 : DECL_EXTERNAL (decl) = 1;
6575 : : }
6576 : 500843 : else if (failure == 3)
6577 : : {
6578 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6579 : : "zero-size array %qD", decl);
6580 : : }
6581 : : }
6582 : :
6583 : 500905 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6584 : :
6585 : 500905 : relayout_decl (decl);
6586 : : }
6587 : : }
6588 : :
6589 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6590 : : any appropriate error messages regarding the layout. */
6591 : :
6592 : : static void
6593 : 45925327 : layout_var_decl (tree decl)
6594 : : {
6595 : 45925327 : tree type;
6596 : :
6597 : 45925327 : type = TREE_TYPE (decl);
6598 : 45925327 : if (type == error_mark_node)
6599 : : return;
6600 : :
6601 : : /* If we haven't already laid out this declaration, do so now.
6602 : : Note that we must not call complete type for an external object
6603 : : because it's type might involve templates that we are not
6604 : : supposed to instantiate yet. (And it's perfectly valid to say
6605 : : `extern X x' for some incomplete type `X'.) */
6606 : 45925279 : if (!DECL_EXTERNAL (decl))
6607 : 37597867 : complete_type (type);
6608 : 45925279 : if (!DECL_SIZE (decl)
6609 : 505888 : && TREE_TYPE (decl) != error_mark_node
6610 : 46431167 : && complete_or_array_type_p (type))
6611 : 463573 : layout_decl (decl, 0);
6612 : :
6613 : 45925279 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6614 : : {
6615 : : /* An automatic variable with an incomplete type: that is an error.
6616 : : Don't talk about array types here, since we took care of that
6617 : : message in grokdeclarator. */
6618 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
6619 : : "storage size of %qD isn%'t known", decl);
6620 : 17 : TREE_TYPE (decl) = error_mark_node;
6621 : : }
6622 : : #if 0
6623 : : /* Keep this code around in case we later want to control debug info
6624 : : based on whether a type is "used". (jason 1999-11-11) */
6625 : :
6626 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6627 : : /* Let debugger know it should output info for this type. */
6628 : : note_debug_info_needed (ttype);
6629 : :
6630 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6631 : : note_debug_info_needed (DECL_CONTEXT (decl));
6632 : : #endif
6633 : :
6634 : 83523146 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6635 : 23240708 : && DECL_SIZE (decl) != NULL_TREE
6636 : 68992849 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
6637 : : {
6638 : 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6639 : 6 : && !DECL_LOCAL_DECL_P (decl))
6640 : 0 : constant_expression_warning (DECL_SIZE (decl));
6641 : : else
6642 : : {
6643 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6644 : : "storage size of %qD isn%'t constant", decl);
6645 : 6 : TREE_TYPE (decl) = error_mark_node;
6646 : 6 : type = error_mark_node;
6647 : : }
6648 : : }
6649 : :
6650 : : /* If the final element initializes a flexible array field, adjust
6651 : : the size of the DECL with the initializer based on whether the
6652 : : DECL is a union or a structure. */
6653 : 45925279 : if (type != error_mark_node
6654 : 45925273 : && DECL_INITIAL (decl)
6655 : 23180626 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6656 : 3690421 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6657 : 3062486 : && DECL_SIZE (decl) != NULL_TREE
6658 : 3062486 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6659 : 3062486 : && COMPLETE_TYPE_P (type)
6660 : 3062486 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6661 : 48987765 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6662 : : {
6663 : 3062486 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6664 : 3062486 : if (elt.index)
6665 : : {
6666 : 2858295 : tree itype = TREE_TYPE (elt.index);
6667 : 2858295 : tree vtype = TREE_TYPE (elt.value);
6668 : 2858295 : if (TREE_CODE (itype) == ARRAY_TYPE
6669 : 49814 : && TYPE_DOMAIN (itype) == NULL
6670 : 272 : && TREE_CODE (vtype) == ARRAY_TYPE
6671 : 2858567 : && COMPLETE_TYPE_P (vtype))
6672 : : {
6673 : : /* For a structure, add the size of the initializer to the DECL's
6674 : : size. */
6675 : 272 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
6676 : : {
6677 : 257 : DECL_SIZE (decl)
6678 : 257 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
6679 : : TYPE_SIZE (vtype));
6680 : 257 : DECL_SIZE_UNIT (decl)
6681 : 514 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6682 : : TYPE_SIZE_UNIT (vtype));
6683 : : }
6684 : : /* For a union, the DECL's size is the maximum of the current size
6685 : : and the size of the initializer. */
6686 : : else
6687 : : {
6688 : 15 : DECL_SIZE (decl)
6689 : 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
6690 : : TYPE_SIZE (vtype));
6691 : 15 : DECL_SIZE_UNIT (decl)
6692 : 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
6693 : : TYPE_SIZE_UNIT (vtype));
6694 : : }
6695 : : }
6696 : : }
6697 : : }
6698 : : }
6699 : :
6700 : : /* If a local static variable is declared in an inline function, or if
6701 : : we have a weak definition, we must endeavor to create only one
6702 : : instance of the variable at link-time. */
6703 : :
6704 : : void
6705 : 51581949 : maybe_commonize_var (tree decl)
6706 : : {
6707 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
6708 : : bindings. */
6709 : 51581949 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6710 : : return;
6711 : :
6712 : : /* Static data in a function with comdat linkage also has comdat
6713 : : linkage. */
6714 : 46719609 : if ((TREE_STATIC (decl)
6715 : 23677193 : && DECL_FUNCTION_SCOPE_P (decl)
6716 : 138662 : && vague_linkage_p (DECL_CONTEXT (decl)))
6717 : 94251624 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6718 : : {
6719 : 19440076 : if (flag_weak)
6720 : : {
6721 : : /* With weak symbols, we simply make the variable COMDAT;
6722 : : that will cause copies in multiple translations units to
6723 : : be merged. */
6724 : 19440049 : comdat_linkage (decl);
6725 : : }
6726 : : else
6727 : : {
6728 : 27 : if (DECL_INITIAL (decl) == NULL_TREE
6729 : 27 : || DECL_INITIAL (decl) == error_mark_node)
6730 : : {
6731 : : /* Without weak symbols, we can use COMMON to merge
6732 : : uninitialized variables. */
6733 : 24 : TREE_PUBLIC (decl) = 1;
6734 : 24 : DECL_COMMON (decl) = 1;
6735 : : }
6736 : : else
6737 : : {
6738 : : /* While for initialized variables, we must use internal
6739 : : linkage -- which means that multiple copies will not
6740 : : be merged. */
6741 : 3 : TREE_PUBLIC (decl) = 0;
6742 : 3 : DECL_COMMON (decl) = 0;
6743 : 3 : DECL_INTERFACE_KNOWN (decl) = 1;
6744 : 3 : const char *msg;
6745 : 3 : if (DECL_INLINE_VAR_P (decl))
6746 : : msg = G_("sorry: semantics of inline variable "
6747 : : "%q#D are wrong (you%'ll wind up with "
6748 : : "multiple copies)");
6749 : : else
6750 : : msg = G_("sorry: semantics of inline function "
6751 : : "static data %q#D are wrong (you%'ll wind "
6752 : : "up with multiple copies)");
6753 : 3 : auto_diagnostic_group d;
6754 : 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6755 : : msg, decl))
6756 : 3 : inform (DECL_SOURCE_LOCATION (decl),
6757 : : "you can work around this by removing the initializer");
6758 : 3 : }
6759 : : }
6760 : : }
6761 : : }
6762 : :
6763 : : /* Issue an error message if DECL is an uninitialized const variable.
6764 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6765 : : context from potential_constant_expression. Returns true if all is well,
6766 : : false otherwise. */
6767 : :
6768 : : bool
6769 : 6413819 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6770 : : tsubst_flags_t complain)
6771 : : {
6772 : 6413819 : tree type = strip_array_types (TREE_TYPE (decl));
6773 : :
6774 : : /* ``Unless explicitly declared extern, a const object does not have
6775 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
6776 : : 7.1.6 */
6777 : 6413819 : if (VAR_P (decl)
6778 : 6413819 : && !TYPE_REF_P (type)
6779 : 6389235 : && (CP_TYPE_CONST_P (type)
6780 : : /* C++20 permits trivial default initialization in constexpr
6781 : : context (P1331R2). */
6782 : 5313557 : || (cxx_dialect < cxx20
6783 : 2751598 : && (constexpr_context_p
6784 : 2456223 : || var_in_constexpr_fn (decl))))
6785 : 7895943 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6786 : : {
6787 : 642615 : tree field = default_init_uninitialized_part (type);
6788 : 642615 : if (!field)
6789 : : return true;
6790 : :
6791 : 112834 : auto_diagnostic_group d;
6792 : 112834 : bool show_notes = true;
6793 : :
6794 : 112834 : if (!constexpr_context_p || cxx_dialect >= cxx20)
6795 : : {
6796 : 109076 : if (CP_TYPE_CONST_P (type))
6797 : : {
6798 : 90 : if (complain & tf_error)
6799 : 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6800 : : "uninitialized %<const %D%>", decl);
6801 : : }
6802 : : else
6803 : : {
6804 : 108986 : if (!is_instantiation_of_constexpr (current_function_decl)
6805 : 108986 : && (complain & tf_error))
6806 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
6807 : : "uninitialized variable %qD in %<constexpr%> "
6808 : : "function", decl);
6809 : : else
6810 : : show_notes = false;
6811 : 108986 : cp_function_chain->invalid_constexpr = true;
6812 : : }
6813 : : }
6814 : 3758 : else if (complain & tf_error)
6815 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
6816 : : "uninitialized variable %qD in %<constexpr%> context",
6817 : : decl);
6818 : :
6819 : 112834 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6820 : : {
6821 : 62 : tree defaulted_ctor;
6822 : :
6823 : 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6824 : : "%q#T has no user-provided default constructor", type);
6825 : 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
6826 : 62 : if (defaulted_ctor)
6827 : 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6828 : : "constructor is not user-provided because it is "
6829 : : "explicitly defaulted in the class body");
6830 : 62 : inform (DECL_SOURCE_LOCATION (field),
6831 : : "and the implicitly-defined constructor does not "
6832 : : "initialize %q#D", field);
6833 : : }
6834 : :
6835 : 112834 : return false;
6836 : 112834 : }
6837 : :
6838 : : return true;
6839 : : }
6840 : :
6841 : : /* Structure holding the current initializer being processed by reshape_init.
6842 : : CUR is a pointer to the current element being processed, END is a pointer
6843 : : after the last element present in the initializer and RAW_IDX is index into
6844 : : RAW_DATA_CST if that is CUR elt. */
6845 : : struct reshape_iter
6846 : : {
6847 : : constructor_elt *cur;
6848 : : constructor_elt *end;
6849 : : unsigned raw_idx;
6850 : : };
6851 : :
6852 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6853 : :
6854 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6855 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
6856 : : initialized as if for an aggregate class. If there are no more such fields,
6857 : : the return value will be NULL. */
6858 : :
6859 : : tree
6860 : 13816434 : next_aggregate_field (tree field)
6861 : : {
6862 : 13816434 : while (field
6863 : 48257010 : && (TREE_CODE (field) != FIELD_DECL
6864 : 8859962 : || DECL_UNNAMED_BIT_FIELD (field)
6865 : 8859840 : || (DECL_ARTIFICIAL (field)
6866 : : /* In C++17, aggregates can have bases. */
6867 : 72386 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6868 : 34440576 : field = DECL_CHAIN (field);
6869 : :
6870 : 13816434 : return field;
6871 : : }
6872 : :
6873 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6874 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6875 : : to a subobject. If there are no more such fields, the return value will be
6876 : : NULL. */
6877 : :
6878 : : tree
6879 : 52567422 : next_subobject_field (tree field)
6880 : : {
6881 : 52567422 : while (field
6882 : 630578717 : && (TREE_CODE (field) != FIELD_DECL
6883 : 24469405 : || DECL_UNNAMED_BIT_FIELD (field)
6884 : 24469325 : || (DECL_ARTIFICIAL (field)
6885 : 6772432 : && !DECL_FIELD_IS_BASE (field)
6886 : 1689570 : && !DECL_VIRTUAL_P (field))))
6887 : 578011295 : field = DECL_CHAIN (field);
6888 : :
6889 : 52567422 : return field;
6890 : : }
6891 : :
6892 : : /* Return true for [dcl.init.list] direct-list-initialization from
6893 : : single element of enumeration with a fixed underlying type. */
6894 : :
6895 : : bool
6896 : 121011664 : is_direct_enum_init (tree type, tree init)
6897 : : {
6898 : 121011664 : if (cxx_dialect >= cxx17
6899 : 119092353 : && TREE_CODE (type) == ENUMERAL_TYPE
6900 : 5953524 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6901 : 3769583 : && TREE_CODE (init) == CONSTRUCTOR
6902 : 401211 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
6903 : 202134 : && CONSTRUCTOR_NELTS (init) == 1
6904 : 218 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
6905 : : /* DR 2374: The single element needs to be implicitly
6906 : : convertible to the underlying type of the enum. */
6907 : 218 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6908 : 121011880 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6909 : 216 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6910 : 216 : CONSTRUCTOR_ELT (init, 0)->value,
6911 : : LOOKUP_IMPLICIT, tf_none))
6912 : : return true;
6913 : : return false;
6914 : : }
6915 : :
6916 : : /* Helper function for reshape_init*. Split first element of
6917 : : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
6918 : : to true if the whole d->cur has been consumed. */
6919 : :
6920 : : static tree
6921 : 21974689 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
6922 : : {
6923 : 21974689 : *inc_cur = true;
6924 : 21974689 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
6925 : : return NULL_TREE;
6926 : 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
6927 : 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
6928 : 30588 : *inc_cur = false;
6929 : : else
6930 : 132 : d->raw_idx = 0;
6931 : : return ret;
6932 : : }
6933 : :
6934 : : /* Wrapper around that which for RAW_DATA_CST in INIT
6935 : : (as well as in D->cur->value) peels off the first element
6936 : : of the raw data and returns it, otherwise increments
6937 : : D->cur and returns INIT. */
6938 : :
6939 : : static tree
6940 : 21974673 : consume_init (tree init, reshape_iter *d)
6941 : : {
6942 : 21974673 : bool inc_cur;
6943 : 21974673 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
6944 : 30720 : init = raw_init;
6945 : 21974673 : if (inc_cur)
6946 : 21944085 : d->cur++;
6947 : 21974673 : return init;
6948 : : }
6949 : :
6950 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
6951 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6952 : : INTEGER_CST representing the size of the array minus one (the maximum index),
6953 : : or NULL_TREE if the array was declared without specifying the size. D is
6954 : : the iterator within the constructor. */
6955 : :
6956 : : static tree
6957 : 352564 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6958 : : tree first_initializer_p, bool vector_p,
6959 : : tsubst_flags_t complain)
6960 : : {
6961 : 352564 : tree new_init;
6962 : 352564 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6963 : 352564 : unsigned HOST_WIDE_INT max_index_cst = 0;
6964 : 352564 : unsigned HOST_WIDE_INT index;
6965 : :
6966 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
6967 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6968 : : to build a new one. But don't reuse if not complaining; if this is
6969 : : tentative, we might also reshape to another type (95319). */
6970 : 352564 : bool reuse = (first_initializer_p
6971 : 306001 : && (complain & tf_error)
6972 : 304754 : && !CP_AGGREGATE_TYPE_P (elt_type)
6973 : 609327 : && !TREE_SIDE_EFFECTS (first_initializer_p));
6974 : 102727 : if (reuse)
6975 : : new_init = first_initializer_p;
6976 : : else
6977 : 102727 : new_init = build_constructor (init_list_type_node, NULL);
6978 : :
6979 : 352564 : if (sized_array_p)
6980 : : {
6981 : 244990 : poly_uint64 midx;
6982 : : /* Minus 1 is used for zero sized arrays. */
6983 : 244990 : if (integer_all_onesp (max_index))
6984 : 24 : return new_init;
6985 : :
6986 : 244966 : if (tree_fits_poly_uint64_p (max_index))
6987 : 244966 : midx = tree_to_poly_uint64 (max_index);
6988 : : /* sizetype is sign extended, not zero extended. */
6989 : : else
6990 : 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
6991 : :
6992 : : /* For VLA vectors, we restict the number of elements in the constructor
6993 : : to lower bound of the VLA elements. */
6994 : 244966 : max_index_cst = constant_lower_bound (midx);
6995 : : }
6996 : :
6997 : 352540 : constructor_elt *first_cur = d->cur;
6998 : : /* Loop until there are no more initializers. */
6999 : 17019674 : for (index = 0;
7000 : 17019674 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7001 : : ++index)
7002 : : {
7003 : 16667316 : tree elt_init;
7004 : 16667316 : constructor_elt *old_cur = d->cur;
7005 : 16667316 : unsigned int old_raw_idx = d->raw_idx;
7006 : 16667316 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7007 : :
7008 : 16667316 : if (d->cur->index)
7009 : 1457032 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7010 : 16667316 : check_array_designated_initializer (d->cur, index);
7011 : 16667316 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7012 : 32693 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7013 : 10233 : || is_byte_access_type (elt_type))
7014 : 22460 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7015 : 1799 : && (!sized_array_p || index < max_index_cst)
7016 : 16669103 : && !vector_p)
7017 : : {
7018 : 467 : elt_init = d->cur->value;
7019 : 467 : unsigned int off = d->raw_idx;
7020 : 467 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7021 : 467 : if (!sized_array_p || len <= max_index_cst - index + 1)
7022 : : {
7023 : 458 : d->cur++;
7024 : 458 : d->raw_idx = 0;
7025 : : }
7026 : : else
7027 : : {
7028 : 9 : len = max_index_cst - index + 1;
7029 : 9 : d->raw_idx += len;
7030 : : }
7031 : 467 : if (!reuse || off || d->cur == old_cur)
7032 : : {
7033 : 25 : elt_init = copy_node (elt_init);
7034 : 25 : RAW_DATA_LENGTH (elt_init) = len;
7035 : 25 : RAW_DATA_POINTER (elt_init) += off;
7036 : : }
7037 : 467 : TREE_TYPE (elt_init) = elt_type;
7038 : : }
7039 : : else
7040 : 16666849 : elt_init = reshape_init_r (elt_type, d,
7041 : : /*first_initializer_p=*/NULL_TREE,
7042 : : complain);
7043 : 16667316 : if (elt_init == error_mark_node)
7044 : : return error_mark_node;
7045 : 16667137 : tree idx = size_int (index);
7046 : 16667137 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7047 : : {
7048 : : /* We need to stop reusing as some RAW_DATA_CST in the original
7049 : : ctor had to be split. */
7050 : 72 : new_init = build_constructor (init_list_type_node, NULL);
7051 : 72 : if (index)
7052 : : {
7053 : 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7054 : 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7055 : 72 : (d->cur - first_cur)
7056 : : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7057 : 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7058 : : {
7059 : : unsigned int j;
7060 : : tree nidx, nval;
7061 : 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7062 : : j, nidx, nval)
7063 : 4 : if (nidx)
7064 : : {
7065 : 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7066 : 4 : (void) nval;
7067 : 4 : break;
7068 : : }
7069 : : }
7070 : : }
7071 : : reuse = false;
7072 : : }
7073 : 16667065 : if (reuse)
7074 : : {
7075 : 14949741 : old_cur->index = idx;
7076 : 14949741 : old_cur->value = elt_init;
7077 : : }
7078 : : else
7079 : 1717396 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7080 : : idx, elt_init);
7081 : 16667137 : if (!TREE_CONSTANT (elt_init))
7082 : 160735 : TREE_CONSTANT (new_init) = false;
7083 : :
7084 : : /* This can happen with an invalid initializer (c++/54501). */
7085 : 16667137 : if (d->cur == old_cur
7086 : 32082 : && !sized_array_p
7087 : 14085 : && d->raw_idx == old_raw_idx)
7088 : : break;
7089 : :
7090 : 16667134 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7091 : 467 : index += RAW_DATA_LENGTH (elt_init) - 1;
7092 : : }
7093 : :
7094 : : return new_init;
7095 : : }
7096 : :
7097 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7098 : : Parameters are the same of reshape_init_r. */
7099 : :
7100 : : static tree
7101 : 311568 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7102 : : tsubst_flags_t complain)
7103 : : {
7104 : 311568 : tree max_index = NULL_TREE;
7105 : :
7106 : 311568 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7107 : :
7108 : 311568 : if (TYPE_DOMAIN (type))
7109 : 204039 : max_index = array_type_nelts_minus_one (type);
7110 : :
7111 : 311568 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7112 : 311568 : first_initializer_p, false, complain);
7113 : : }
7114 : :
7115 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7116 : : Parameters are the same of reshape_init_r. */
7117 : :
7118 : : static tree
7119 : 40996 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7120 : : {
7121 : 40996 : tree max_index = NULL_TREE;
7122 : :
7123 : 40996 : gcc_assert (VECTOR_TYPE_P (type));
7124 : :
7125 : 40996 : if (COMPOUND_LITERAL_P (d->cur->value))
7126 : : {
7127 : 0 : tree value = d->cur->value;
7128 : 0 : if (!same_type_p (TREE_TYPE (value), type))
7129 : : {
7130 : 0 : if (complain & tf_error)
7131 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7132 : 0 : TREE_TYPE (d->cur->value), type);
7133 : 0 : value = error_mark_node;
7134 : : }
7135 : 0 : ++d->cur;
7136 : 0 : return value;
7137 : : }
7138 : :
7139 : : /* For a vector, we initialize it as an array of the appropriate size. */
7140 : 40996 : if (VECTOR_TYPE_P (type))
7141 : 40996 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7142 : :
7143 : 40996 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7144 : 40996 : NULL_TREE, true, complain);
7145 : : }
7146 : :
7147 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7148 : : INIT, in isolation from any designator or other initializers. */
7149 : :
7150 : : static tree
7151 : 561477 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7152 : : {
7153 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7154 : : calling reshape_init, but this way can just live on the stack. */
7155 : 561477 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7156 : 561477 : reshape_iter iter = { &elt, &elt + 1, 0 };
7157 : 561477 : return reshape_init_r (type, &iter,
7158 : : /*first_initializer_p=*/NULL_TREE,
7159 : 561477 : complain);
7160 : : }
7161 : :
7162 : : /* Subroutine of reshape_init_r, processes the initializers for classes
7163 : : or union. Parameters are the same of reshape_init_r. */
7164 : :
7165 : : static tree
7166 : 3494751 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7167 : : tsubst_flags_t complain)
7168 : : {
7169 : 3494751 : tree field;
7170 : 3494751 : tree new_init;
7171 : :
7172 : 3494751 : gcc_assert (CLASS_TYPE_P (type));
7173 : :
7174 : : /* The initializer for a class is always a CONSTRUCTOR. */
7175 : 3494751 : new_init = build_constructor (init_list_type_node, NULL);
7176 : :
7177 : 3494751 : int binfo_idx = -1;
7178 : 3494751 : tree binfo = TYPE_BINFO (type);
7179 : 3494751 : tree base_binfo = NULL_TREE;
7180 : 3494751 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7181 : : {
7182 : : /* We get here from maybe_aggr_guide for C++20 class template argument
7183 : : deduction. In this case we need to look through the binfo because a
7184 : : template doesn't have base fields. */
7185 : 337 : binfo_idx = 0;
7186 : 337 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7187 : : }
7188 : 67 : if (base_binfo)
7189 : : field = base_binfo;
7190 : : else
7191 : 3494684 : field = next_aggregate_field (TYPE_FIELDS (type));
7192 : :
7193 : 3494751 : if (!field)
7194 : : {
7195 : : /* [dcl.init.aggr]
7196 : :
7197 : : An initializer for an aggregate member that is an
7198 : : empty class shall have the form of an empty
7199 : : initializer-list {}. */
7200 : 324 : if (!first_initializer_p)
7201 : : {
7202 : 270 : if (complain & tf_error)
7203 : 13 : error ("initializer for %qT must be brace-enclosed", type);
7204 : 270 : return error_mark_node;
7205 : : }
7206 : : return new_init;
7207 : : }
7208 : :
7209 : : /* For C++20 CTAD, handle pack expansions in the base list. */
7210 : : tree last_was_pack_expansion = NULL_TREE;
7211 : :
7212 : : /* Loop through the initializable fields, gathering initializers. */
7213 : 10639498 : while (d->cur != d->end)
7214 : : {
7215 : 7254740 : tree field_init;
7216 : 7254740 : constructor_elt *old_cur = d->cur;
7217 : 7254740 : unsigned old_raw_idx = d->raw_idx;
7218 : 7254740 : bool direct_desig = false;
7219 : :
7220 : : /* Handle C++20 designated initializers. */
7221 : 7254740 : if (d->cur->index)
7222 : : {
7223 : 561637 : if (d->cur->index == error_mark_node)
7224 : : return error_mark_node;
7225 : :
7226 : 561631 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7227 : : {
7228 : : /* We already reshaped this; we should have returned early from
7229 : : reshape_init. */
7230 : 0 : gcc_checking_assert (false);
7231 : : if (field != d->cur->index)
7232 : : {
7233 : : if (tree id = DECL_NAME (d->cur->index))
7234 : : gcc_checking_assert (d->cur->index
7235 : : == get_class_binding (type, id));
7236 : : field = d->cur->index;
7237 : : }
7238 : : }
7239 : 561631 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7240 : : {
7241 : 561625 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7242 : 561625 : field = get_class_binding (type, d->cur->index);
7243 : 561625 : direct_desig = true;
7244 : : }
7245 : : else
7246 : : {
7247 : 6 : if (complain & tf_error)
7248 : 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7249 : : " for class %qT", d->cur->index, type);
7250 : 6 : return error_mark_node;
7251 : : }
7252 : :
7253 : 561625 : if (!field && ANON_AGGR_TYPE_P (type))
7254 : : /* Apparently the designator isn't for a member of this anonymous
7255 : : struct, so head back to the enclosing class. */
7256 : : break;
7257 : :
7258 : 561622 : if (!field || TREE_CODE (field) != FIELD_DECL)
7259 : : {
7260 : 18 : if (complain & tf_error)
7261 : : {
7262 : 6 : if (field && TREE_CODE (field) == TREE_LIST)
7263 : : {
7264 : 3 : auto_diagnostic_group g;
7265 : 3 : error ("request for member %qD is ambiguous",
7266 : 3 : d->cur->index);
7267 : 3 : print_candidates (field);
7268 : 3 : }
7269 : : else
7270 : 3 : error ("%qT has no non-static data member named %qD", type,
7271 : 3 : d->cur->index);
7272 : : }
7273 : 18 : return error_mark_node;
7274 : : }
7275 : :
7276 : : /* If the element is an anonymous union object and the initializer
7277 : : list is a designated-initializer-list, the anonymous union object
7278 : : is initialized by the designated-initializer-list { D }, where D
7279 : : is the designated-initializer-clause naming a member of the
7280 : : anonymous union object. */
7281 : 561604 : tree ictx = DECL_CONTEXT (field);
7282 : 561604 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7283 : : {
7284 : : /* Find the anon aggr that is a direct member of TYPE. */
7285 : 139 : while (ANON_AGGR_TYPE_P (ictx))
7286 : : {
7287 : 136 : tree cctx = TYPE_CONTEXT (ictx);
7288 : 136 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7289 : 124 : goto found;
7290 : : ictx = cctx;
7291 : : }
7292 : :
7293 : : /* Not found, e.g. FIELD is a member of a base class. */
7294 : 3 : if (complain & tf_error)
7295 : 3 : error ("%qD is not a direct member of %qT", field, type);
7296 : 3 : return error_mark_node;
7297 : :
7298 : 124 : found:
7299 : : /* Now find the TYPE member with that anon aggr type. */
7300 : 124 : tree aafield = TYPE_FIELDS (type);
7301 : 1067 : for (; aafield; aafield = TREE_CHAIN (aafield))
7302 : 1067 : if (TREE_TYPE (aafield) == ictx)
7303 : : break;
7304 : 0 : gcc_assert (aafield);
7305 : : field = aafield;
7306 : : direct_desig = false;
7307 : : }
7308 : : }
7309 : :
7310 : : /* If we processed all the member of the class, we are done. */
7311 : 7254580 : if (!field)
7312 : : break;
7313 : :
7314 : 14508607 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7315 : 7254313 : ? field : NULL_TREE);
7316 : 7254313 : if (last_was_pack_expansion)
7317 : : /* Each non-trailing aggregate element that is a pack expansion is
7318 : : assumed to correspond to no elements of the initializer list. */
7319 : 19 : goto continue_;
7320 : :
7321 : 7254294 : if (direct_desig)
7322 : : {
7323 : : /* The designated field F is initialized from this one element.
7324 : :
7325 : : Note that we don't want to do this if we found the designator
7326 : : inside an anon aggr above; we use the normal code to implement:
7327 : :
7328 : : "If the element is an anonymous union member and the initializer
7329 : : list is a brace-enclosed designated- initializer-list, the element
7330 : : is initialized by the designated-initializer-list { D }, where D
7331 : : is the designated- initializer-clause naming a member of the
7332 : : anonymous union member." */
7333 : 561477 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7334 : 561477 : field_init = reshape_single_init (TREE_TYPE (field),
7335 : : d->cur->value, complain);
7336 : 561477 : d->cur++;
7337 : : }
7338 : : else
7339 : 6692817 : field_init = reshape_init_r (TREE_TYPE (field), d,
7340 : : /*first_initializer_p=*/NULL_TREE,
7341 : : complain);
7342 : :
7343 : 7254294 : if (field_init == error_mark_node)
7344 : : return error_mark_node;
7345 : :
7346 : 7254005 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7347 : : {
7348 : : /* This can happen with an invalid initializer for a flexible
7349 : : array member (c++/54441). */
7350 : 0 : if (complain & tf_error)
7351 : 0 : error ("invalid initializer for %q#D", field);
7352 : 0 : return error_mark_node;
7353 : : }
7354 : :
7355 : 7254005 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7356 : :
7357 : : /* [dcl.init.aggr]
7358 : :
7359 : : When a union is initialized with a brace-enclosed
7360 : : initializer, the braces shall only contain an
7361 : : initializer for the first member of the union. */
7362 : 7254005 : if (TREE_CODE (type) == UNION_TYPE)
7363 : : break;
7364 : :
7365 : 7145052 : continue_:
7366 : 7145071 : if (base_binfo)
7367 : : {
7368 : 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7369 : : field = base_binfo;
7370 : : else
7371 : 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7372 : : }
7373 : : else
7374 : 7144989 : field = next_aggregate_field (DECL_CHAIN (field));
7375 : : }
7376 : :
7377 : : /* A trailing aggregate element that is a pack expansion is assumed to
7378 : : correspond to all remaining elements of the initializer list (if any). */
7379 : 3494105 : if (last_was_pack_expansion)
7380 : : {
7381 : 16 : tree init = d->cur->value;
7382 : 16 : bool inc_cur;
7383 : 16 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7384 : 0 : init = raw_init;
7385 : 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7386 : : last_was_pack_expansion, init);
7387 : 46 : while (d->cur != d->end)
7388 : 30 : d->cur++;
7389 : : }
7390 : :
7391 : : return new_init;
7392 : : }
7393 : :
7394 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7395 : : designators are not valid; either complain or return true to indicate
7396 : : that reshape_init_r should return error_mark_node. */
7397 : :
7398 : : static bool
7399 : 59703 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7400 : : {
7401 : 59703 : if (d->cur->index)
7402 : : {
7403 : 30 : if (complain & tf_error)
7404 : 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7405 : : "C99 designator %qE outside aggregate initializer",
7406 : : d->cur->index);
7407 : : else
7408 : : return true;
7409 : : }
7410 : : return false;
7411 : : }
7412 : :
7413 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7414 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7415 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7416 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7417 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7418 : :
7419 : : static tree
7420 : 27821659 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7421 : : tsubst_flags_t complain)
7422 : : {
7423 : 27821659 : tree init = d->cur->value;
7424 : :
7425 : 27821659 : if (error_operand_p (init))
7426 : 123 : return error_mark_node;
7427 : :
7428 : 3900396 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7429 : 27881073 : && has_designator_problem (d, complain))
7430 : 0 : return error_mark_node;
7431 : :
7432 : 27821536 : tree stripped_init = tree_strip_any_location_wrapper (init);
7433 : :
7434 : 27821536 : if (TREE_CODE (type) == COMPLEX_TYPE)
7435 : : {
7436 : : /* A complex type can be initialized from one or two initializers,
7437 : : but braces are not elided. */
7438 : 561 : init = consume_init (init, d);
7439 : 561 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7440 : : {
7441 : 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7442 : : {
7443 : 0 : if (complain & tf_error)
7444 : 0 : error ("too many initializers for %qT", type);
7445 : : else
7446 : 0 : return error_mark_node;
7447 : : }
7448 : : }
7449 : 552 : else if (first_initializer_p && d->cur != d->end)
7450 : : {
7451 : 36 : if (error_operand_p (d->cur->value)
7452 : 36 : || has_designator_problem (d, complain))
7453 : 0 : return error_mark_node;
7454 : 36 : vec<constructor_elt, va_gc> *v = 0;
7455 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7456 : 36 : init = consume_init (d->cur->value, d);
7457 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7458 : 36 : init = build_constructor (init_list_type_node, v);
7459 : : }
7460 : 561 : return init;
7461 : : }
7462 : :
7463 : : /* A non-aggregate type is always initialized with a single
7464 : : initializer. */
7465 : 55592273 : if (!CP_AGGREGATE_TYPE_P (type)
7466 : : /* As is an array with dependent bound, which we can see
7467 : : during C++20 aggregate CTAD. */
7468 : 33621544 : || (cxx_dialect >= cxx20
7469 : 3682411 : && TREE_CODE (type) == ARRAY_TYPE
7470 : 148573 : && uses_template_parms (TYPE_DOMAIN (type))))
7471 : : {
7472 : : /* It is invalid to initialize a non-aggregate type with a
7473 : : brace-enclosed initializer before C++0x.
7474 : : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7475 : : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7476 : : a CONSTRUCTOR (with a record type). */
7477 : 21970740 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
7478 : : /* Don't complain about a capture-init. */
7479 : 302718 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7480 : 22263564 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7481 : : {
7482 : 292785 : if (SCALAR_TYPE_P (type))
7483 : : {
7484 : 726 : if (cxx_dialect < cxx11)
7485 : : {
7486 : 16 : if (complain & tf_error)
7487 : 16 : error ("braces around scalar initializer for type %qT",
7488 : : type);
7489 : 16 : init = error_mark_node;
7490 : : }
7491 : 710 : else if (first_initializer_p
7492 : 710 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
7493 : 546 : && (BRACE_ENCLOSED_INITIALIZER_P
7494 : : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
7495 : : {
7496 : 13 : if (complain & tf_error)
7497 : 13 : error ("too many braces around scalar initializer "
7498 : : "for type %qT", type);
7499 : 13 : init = error_mark_node;
7500 : : }
7501 : : }
7502 : : else
7503 : 292059 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7504 : : }
7505 : 21970740 : return consume_init (init, d);
7506 : : }
7507 : :
7508 : : /* "If T is a class type and the initializer list has a single element of
7509 : : type cv U, where U is T or a class derived from T, the object is
7510 : : initialized from that element." Even if T is an aggregate. */
7511 : 5828202 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7512 : 5411481 : && first_initializer_p
7513 : : /* But not if it's a designated init. */
7514 : 3522108 : && !d->cur->index
7515 : 3283993 : && d->end - d->cur == 1
7516 : 421027 : && TREE_CODE (init) != RAW_DATA_CST
7517 : 6271262 : && reference_related_p (type, TREE_TYPE (init)))
7518 : : {
7519 : 416 : d->cur++;
7520 : 416 : return init;
7521 : : }
7522 : :
7523 : : /* [dcl.init.aggr]
7524 : :
7525 : : All implicit type conversions (clause _conv_) are considered when
7526 : : initializing the aggregate member with an initializer from an
7527 : : initializer-list. If the initializer can initialize a member,
7528 : : the member is initialized. Otherwise, if the member is itself a
7529 : : non-empty subaggregate, brace elision is assumed and the
7530 : : initializer is considered for the initialization of the first
7531 : : member of the subaggregate. */
7532 : 2549482 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7533 : : /* But don't try this for the first initializer, since that would be
7534 : : looking through the outermost braces; A a2 = { a1 }; is not a
7535 : : valid aggregate initialization. */
7536 : 3300397 : && !first_initializer_p
7537 : 5860898 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7538 : 7781 : || can_convert_arg (type, TREE_TYPE (init),
7539 : 7781 : TREE_CODE (init) == RAW_DATA_CST
7540 : 5851425 : ? build_int_cst (integer_type_node,
7541 : 1606 : *(const unsigned char *)
7542 : 1606 : RAW_DATA_POINTER (init))
7543 : : : init,
7544 : : LOOKUP_NORMAL, complain)))
7545 : 3336 : return consume_init (init, d);
7546 : :
7547 : : /* [dcl.init.string]
7548 : :
7549 : : A char array (whether plain char, signed char, or unsigned char)
7550 : : can be initialized by a string-literal (optionally enclosed in
7551 : : braces); a wchar_t array can be initialized by a wide
7552 : : string-literal (optionally enclosed in braces). */
7553 : 5846483 : if (TREE_CODE (type) == ARRAY_TYPE
7554 : 5846483 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7555 : : {
7556 : 86953 : tree str_init = init;
7557 : 86953 : tree stripped_str_init = stripped_init;
7558 : 86953 : reshape_iter stripd = {};
7559 : :
7560 : : /* Strip one level of braces if and only if they enclose a single
7561 : : element (as allowed by [dcl.init.string]). */
7562 : 86953 : if (!first_initializer_p
7563 : 12215 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7564 : 98085 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7565 : : {
7566 : 375 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7567 : 375 : str_init = stripd.cur->value;
7568 : 375 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
7569 : : }
7570 : :
7571 : : /* If it's a string literal, then it's the initializer for the array
7572 : : as a whole. Otherwise, continue with normal initialization for
7573 : : array types (one value per array element). */
7574 : 86953 : if (TREE_CODE (stripped_str_init) == STRING_CST)
7575 : : {
7576 : 72 : if ((first_initializer_p && has_designator_problem (d, complain))
7577 : 1025 : || (stripd.cur && has_designator_problem (&stripd, complain)))
7578 : 953 : return error_mark_node;
7579 : 953 : d->cur++;
7580 : 953 : return str_init;
7581 : : }
7582 : : }
7583 : :
7584 : : /* The following cases are about aggregates. If we are not within a full
7585 : : initializer already, and there is not a CONSTRUCTOR, it means that there
7586 : : is a missing set of braces (that is, we are processing the case for
7587 : : which reshape_init exists). */
7588 : 5845530 : bool braces_elided_p = false;
7589 : 5845530 : if (!first_initializer_p)
7590 : : {
7591 : 2005159 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7592 : : {
7593 : 1998245 : tree init_type = TREE_TYPE (init);
7594 : 1998245 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7595 : : /* There is no need to call reshape_init for pointer-to-member
7596 : : function initializers, as they are always constructed correctly
7597 : : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7598 : : which is missing outermost braces. We should warn below, and
7599 : : one of the routines below will wrap it in additional { }. */;
7600 : : /* For a nested compound literal, proceed to specialized routines,
7601 : : to handle initialization of arrays and similar. */
7602 : 1998236 : else if (COMPOUND_LITERAL_P (stripped_init))
7603 : 6 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7604 : : /* If we have an unresolved designator, we need to find the member it
7605 : : designates within TYPE, so proceed to the routines below. For
7606 : : FIELD_DECL or INTEGER_CST designators, we're already initializing
7607 : : the designated element. */
7608 : 1998230 : else if (d->cur->index
7609 : 27 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7610 : : /* Brace elision with designators is only permitted for anonymous
7611 : : aggregates. */
7612 : 15 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7613 : : /* A CONSTRUCTOR of the target's type is a previously
7614 : : digested initializer. */
7615 : 1998215 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7616 : : {
7617 : 0 : ++d->cur;
7618 : 0 : return init;
7619 : : }
7620 : : else
7621 : : {
7622 : : /* Something that hasn't been reshaped yet. */
7623 : 1998215 : ++d->cur;
7624 : 1998215 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7625 : 1998215 : return reshape_init (type, init, complain);
7626 : : }
7627 : : }
7628 : :
7629 : 6944 : if (complain & tf_warning)
7630 : 5795 : warning (OPT_Wmissing_braces,
7631 : : "missing braces around initializer for %qT",
7632 : : type);
7633 : : braces_elided_p = true;
7634 : : }
7635 : :
7636 : : /* Dispatch to specialized routines. */
7637 : 3847315 : tree new_init;
7638 : 3847315 : if (CLASS_TYPE_P (type))
7639 : 3494751 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
7640 : 352564 : else if (TREE_CODE (type) == ARRAY_TYPE)
7641 : 311568 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
7642 : 40996 : else if (VECTOR_TYPE_P (type))
7643 : 40996 : new_init = reshape_init_vector (type, d, complain);
7644 : : else
7645 : 0 : gcc_unreachable ();
7646 : :
7647 : 3847315 : if (braces_elided_p
7648 : 6944 : && TREE_CODE (new_init) == CONSTRUCTOR)
7649 : 6668 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7650 : :
7651 : : return new_init;
7652 : : }
7653 : :
7654 : : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7655 : : brace-enclosed aggregate initializer.
7656 : :
7657 : : INIT is the CONSTRUCTOR containing the list of initializers describing
7658 : : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7659 : : It may not presently match the shape of the TYPE; for example:
7660 : :
7661 : : struct S { int a; int b; };
7662 : : struct S a[] = { 1, 2, 3, 4 };
7663 : :
7664 : : Here INIT will hold a vector of four elements, rather than a
7665 : : vector of two elements, each itself a vector of two elements. This
7666 : : routine transforms INIT from the former form into the latter. The
7667 : : revised CONSTRUCTOR node is returned. */
7668 : :
7669 : : tree
7670 : 9219735 : reshape_init (tree type, tree init, tsubst_flags_t complain)
7671 : : {
7672 : 9219735 : vec<constructor_elt, va_gc> *v;
7673 : 9219735 : reshape_iter d;
7674 : 9219735 : tree new_init;
7675 : :
7676 : 9219735 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7677 : :
7678 : 9219735 : v = CONSTRUCTOR_ELTS (init);
7679 : :
7680 : : /* An empty constructor does not need reshaping, and it is always a valid
7681 : : initializer. */
7682 : 9220259 : if (vec_safe_is_empty (v))
7683 : : return init;
7684 : :
7685 : 3901245 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7686 : : /* Already reshaped. */
7687 : : return init;
7688 : :
7689 : : /* Brace elision is not performed for a CONSTRUCTOR representing
7690 : : parenthesized aggregate initialization. */
7691 : 3901108 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
7692 : : {
7693 : 416 : tree elt = (*v)[0].value;
7694 : : /* If we're initializing a char array from a string-literal that is
7695 : : enclosed in braces, unwrap it here. */
7696 : 416 : if (TREE_CODE (type) == ARRAY_TYPE
7697 : 221 : && vec_safe_length (v) == 1
7698 : 121 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7699 : 449 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7700 : : return elt;
7701 : 387 : return init;
7702 : : }
7703 : :
7704 : : /* Handle [dcl.init.list] direct-list-initialization from
7705 : : single element of enumeration with a fixed underlying type. */
7706 : 3900692 : if (is_direct_enum_init (type, init))
7707 : : {
7708 : 176 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7709 : 176 : type = cv_unqualified (type);
7710 : 176 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7711 : : {
7712 : 161 : warning_sentinel w (warn_useless_cast);
7713 : 161 : warning_sentinel w2 (warn_ignored_qualifiers);
7714 : 161 : return cp_build_c_cast (input_location, type, elt,
7715 : : tf_warning_or_error);
7716 : 161 : }
7717 : : else
7718 : 15 : return error_mark_node;
7719 : : }
7720 : :
7721 : : /* Recurse on this CONSTRUCTOR. */
7722 : 3900516 : d.cur = &(*v)[0];
7723 : 3900516 : d.end = d.cur + v->length ();
7724 : 3900516 : d.raw_idx = 0;
7725 : :
7726 : 3900516 : new_init = reshape_init_r (type, &d, init, complain);
7727 : 3900516 : if (new_init == error_mark_node)
7728 : : return error_mark_node;
7729 : :
7730 : : /* Make sure all the element of the constructor were used. Otherwise,
7731 : : issue an error about exceeding initializers. */
7732 : 3899895 : if (d.cur != d.end)
7733 : : {
7734 : 135 : if (complain & tf_error)
7735 : 63 : error ("too many initializers for %qT", type);
7736 : 135 : return error_mark_node;
7737 : : }
7738 : :
7739 : 3899760 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7740 : 3899760 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7741 : 448546 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7742 : 3899760 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7743 : 3899760 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7744 : 30136 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7745 : : || seen_error ());
7746 : :
7747 : : return new_init;
7748 : : }
7749 : :
7750 : : /* Verify array initializer. Returns true if errors have been reported. */
7751 : :
7752 : : bool
7753 : 852541 : check_array_initializer (tree decl, tree type, tree init)
7754 : : {
7755 : 852541 : tree element_type = TREE_TYPE (type);
7756 : :
7757 : : /* Structured binding when initialized with an array type needs
7758 : : to have complete type. */
7759 : 852541 : if (decl
7760 : 848228 : && DECL_DECOMPOSITION_P (decl)
7761 : 251 : && DECL_DECOMP_IS_BASE (decl)
7762 : 852792 : && !COMPLETE_TYPE_P (type))
7763 : : {
7764 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
7765 : : "structured binding has incomplete type %qT", type);
7766 : 0 : TREE_TYPE (decl) = error_mark_node;
7767 : 0 : return true;
7768 : : }
7769 : :
7770 : : /* The array type itself need not be complete, because the
7771 : : initializer may tell us how many elements are in the array.
7772 : : But, the elements of the array must be complete. */
7773 : 852541 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
7774 : : {
7775 : 15 : if (decl)
7776 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
7777 : : "elements of array %q#D have incomplete type", decl);
7778 : : else
7779 : 6 : error ("elements of array %q#T have incomplete type", type);
7780 : 15 : return true;
7781 : : }
7782 : :
7783 : 852526 : location_t loc = (decl ? location_of (decl) : input_location);
7784 : 852526 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7785 : : return true;
7786 : :
7787 : : /* A compound literal can't have variable size. */
7788 : 852526 : if (init && !decl
7789 : 852526 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7790 : 4304 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7791 : : {
7792 : 3 : error ("variable-sized compound literal");
7793 : 3 : return true;
7794 : : }
7795 : : return false;
7796 : : }
7797 : :
7798 : : /* Subroutine of check_initializer; args are passed down from that function.
7799 : : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7800 : :
7801 : : static tree
7802 : 2323639 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
7803 : : {
7804 : 2323639 : gcc_assert (stmts_are_full_exprs_p ());
7805 : 2323639 : if (init)
7806 : 1707448 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7807 : 2323639 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
7808 : : }
7809 : :
7810 : : /* Verify INIT (the initializer for DECL), and record the
7811 : : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7812 : : grok_reference_init.
7813 : :
7814 : : If the return value is non-NULL, it is an expression that must be
7815 : : evaluated dynamically to initialize DECL. */
7816 : :
7817 : : static tree
7818 : 40256617 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7819 : : {
7820 : 40256617 : tree type;
7821 : 40256617 : tree init_code = NULL;
7822 : 40256617 : tree core_type;
7823 : :
7824 : : /* Things that are going to be initialized need to have complete
7825 : : type. */
7826 : 40256617 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7827 : :
7828 : 40256617 : if (DECL_HAS_VALUE_EXPR_P (decl))
7829 : : {
7830 : : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7831 : : it doesn't have storage to be initialized. */
7832 : 138 : gcc_assert (init == NULL_TREE);
7833 : : return NULL_TREE;
7834 : : }
7835 : :
7836 : 40256479 : if (type == error_mark_node)
7837 : : /* We will have already complained. */
7838 : : return NULL_TREE;
7839 : :
7840 : 40256479 : if (TREE_CODE (type) == ARRAY_TYPE)
7841 : : {
7842 : 848228 : if (check_array_initializer (decl, type, init))
7843 : : return NULL_TREE;
7844 : : }
7845 : 39408251 : else if (!COMPLETE_TYPE_P (type))
7846 : : {
7847 : 37 : error_at (DECL_SOURCE_LOCATION (decl),
7848 : : "%q#D has incomplete type", decl);
7849 : 37 : TREE_TYPE (decl) = error_mark_node;
7850 : 37 : return NULL_TREE;
7851 : : }
7852 : : else
7853 : : /* There is no way to make a variable-sized class type in GNU C++. */
7854 : 39408214 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7855 : :
7856 : 40256433 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7857 : : {
7858 : 1447400 : int init_len = CONSTRUCTOR_NELTS (init);
7859 : 1447400 : if (SCALAR_TYPE_P (type))
7860 : : {
7861 : 49689 : if (init_len == 0)
7862 : : {
7863 : 43010 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7864 : 43010 : init = build_zero_init (type, NULL_TREE, false);
7865 : : }
7866 : 6679 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7867 : : {
7868 : 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7869 : : "scalar object %qD requires one element in "
7870 : : "initializer", decl);
7871 : 6 : TREE_TYPE (decl) = error_mark_node;
7872 : 6 : return NULL_TREE;
7873 : : }
7874 : : }
7875 : : }
7876 : :
7877 : 40256427 : if (TREE_CODE (decl) == CONST_DECL)
7878 : : {
7879 : 0 : gcc_assert (!TYPE_REF_P (type));
7880 : :
7881 : 0 : DECL_INITIAL (decl) = init;
7882 : :
7883 : 0 : gcc_assert (init != NULL_TREE);
7884 : : init = NULL_TREE;
7885 : : }
7886 : 40256427 : else if (!init && DECL_REALLY_EXTERN (decl))
7887 : : ;
7888 : 40256427 : else if (flag_openmp
7889 : 116229 : && VAR_P (decl)
7890 : 116229 : && DECL_LANG_SPECIFIC (decl)
7891 : 41592 : && DECL_OMP_DECLARE_MAPPER_P (decl)
7892 : 40256427 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
7893 : : return NULL_TREE;
7894 : 4194060 : else if (init || type_build_ctor_call (type)
7895 : 43834293 : || TYPE_REF_P (type))
7896 : : {
7897 : 36678576 : if (TYPE_REF_P (type))
7898 : : {
7899 : 308447 : init = grok_reference_init (decl, type, init, flags);
7900 : 308447 : flags |= LOOKUP_ALREADY_DIGESTED;
7901 : : }
7902 : 36370129 : else if (!init)
7903 : 616194 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7904 : : tf_warning_or_error);
7905 : : /* Do not reshape constructors of vectors (they don't need to be
7906 : : reshaped. */
7907 : 35753935 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7908 : : {
7909 : 1404078 : if (is_std_init_list (type))
7910 : : {
7911 : 322 : init = perform_implicit_conversion (type, init,
7912 : : tf_warning_or_error);
7913 : 322 : flags |= LOOKUP_ALREADY_DIGESTED;
7914 : : }
7915 : 1403756 : else if (TYPE_NON_AGGREGATE_CLASS (type))
7916 : : {
7917 : : /* Don't reshape if the class has constructors. */
7918 : 208141 : if (cxx_dialect == cxx98)
7919 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7920 : : "in C++98 %qD must be initialized by "
7921 : : "constructor, not by %<{...}%>",
7922 : : decl);
7923 : : }
7924 : 1195615 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7925 : : {
7926 : 0 : error ("opaque vector types cannot be initialized");
7927 : 0 : init = error_mark_node;
7928 : : }
7929 : : else
7930 : : {
7931 : 1195615 : init = reshape_init (type, init, tf_warning_or_error);
7932 : 1195615 : flags |= LOOKUP_NO_NARROWING;
7933 : : }
7934 : : }
7935 : : /* [dcl.init] "Otherwise, if the destination type is an array, the object
7936 : : is initialized as follows..." So handle things like
7937 : :
7938 : : int a[](1, 2, 3);
7939 : :
7940 : : which is permitted in C++20 by P0960. */
7941 : 34349857 : else if (TREE_CODE (init) == TREE_LIST
7942 : 611304 : && TREE_TYPE (init) == NULL_TREE
7943 : 611304 : && TREE_CODE (type) == ARRAY_TYPE
7944 : 165 : && !DECL_DECOMPOSITION_P (decl)
7945 : 34350001 : && (cxx_dialect >= cxx20))
7946 : 124 : init = do_aggregate_paren_init (init, type);
7947 : 34349733 : else if (TREE_CODE (init) == TREE_LIST
7948 : 611180 : && TREE_TYPE (init) != unknown_type_node
7949 : 34960913 : && !MAYBE_CLASS_TYPE_P (type))
7950 : : {
7951 : 132410 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7952 : :
7953 : : /* We get here with code like `int a (2);' */
7954 : 132410 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7955 : : tf_warning_or_error);
7956 : : }
7957 : :
7958 : : /* If DECL has an array type without a specific bound, deduce the
7959 : : array size from the initializer. */
7960 : 36678576 : maybe_deduce_size_from_array_init (decl, init);
7961 : 36678576 : type = TREE_TYPE (decl);
7962 : 36678576 : if (type == error_mark_node)
7963 : : return NULL_TREE;
7964 : :
7965 : 70626648 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7966 : 3364612 : && !(flags & LOOKUP_ALREADY_DIGESTED)
7967 : 3364290 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7968 : 1250388 : && CP_AGGREGATE_TYPE_P (type)
7969 : 1042247 : && (CLASS_TYPE_P (type)
7970 : : /* The call to build_aggr_init below could end up
7971 : : calling build_vec_init, which may break when we
7972 : : are processing a template. */
7973 : 1542 : || processing_template_decl
7974 : 1476 : || !TYPE_NEEDS_CONSTRUCTING (type)
7975 : 1444 : || type_has_extended_temps (type))))
7976 : 71033715 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7977 : : {
7978 : 2323639 : init_code = build_aggr_init_full_exprs (decl, init, flags);
7979 : :
7980 : : /* A constructor call is a non-trivial initializer even if
7981 : : it isn't explicitly written. */
7982 : 2323639 : if (TREE_SIDE_EFFECTS (init_code))
7983 : 2314366 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7984 : :
7985 : : /* If this is a constexpr initializer, expand_default_init will
7986 : : have returned an INIT_EXPR rather than a CALL_EXPR. In that
7987 : : case, pull the initializer back out and pass it down into
7988 : : store_init_value. */
7989 : 6030151 : while (true)
7990 : : {
7991 : 6030151 : if (TREE_CODE (init_code) == EXPR_STMT
7992 : 3718227 : || TREE_CODE (init_code) == STMT_EXPR
7993 : 3709073 : || TREE_CODE (init_code) == CONVERT_EXPR)
7994 : 3697403 : init_code = TREE_OPERAND (init_code, 0);
7995 : 2332748 : else if (TREE_CODE (init_code) == BIND_EXPR)
7996 : 9109 : init_code = BIND_EXPR_BODY (init_code);
7997 : : else
7998 : : break;
7999 : : }
8000 : 2323639 : if (TREE_CODE (init_code) == INIT_EXPR)
8001 : : {
8002 : : /* In C++20, the call to build_aggr_init could have created
8003 : : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8004 : : A(1, 2). */
8005 : 1381875 : tree rhs = TREE_OPERAND (init_code, 1);
8006 : 1381875 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8007 : : /* Avoid leaking TARGET_EXPR into template trees. */
8008 : 4569 : rhs = build_implicit_conv_flags (type, init, flags);
8009 : 1381875 : init = rhs;
8010 : :
8011 : 1381875 : init_code = NULL_TREE;
8012 : : /* Don't call digest_init; it's unnecessary and will complain
8013 : : about aggregate initialization of non-aggregate classes. */
8014 : 1381872 : flags |= LOOKUP_ALREADY_DIGESTED;
8015 : : }
8016 : 941764 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8017 : 1878582 : || DECL_DECLARED_CONSTINIT_P (decl))
8018 : : {
8019 : : /* Declared constexpr or constinit, but no suitable initializer;
8020 : : massage init appropriately so we can pass it into
8021 : : store_init_value for the error. */
8022 : 4964 : tree new_init = NULL_TREE;
8023 : 4964 : if (!processing_template_decl
8024 : 382 : && TREE_CODE (init_code) == CALL_EXPR)
8025 : 329 : new_init = build_cplus_new (type, init_code, tf_none);
8026 : 4611 : else if (CLASS_TYPE_P (type)
8027 : 9246 : && (!init || TREE_CODE (init) == TREE_LIST))
8028 : 4552 : new_init = build_functional_cast (input_location, type,
8029 : : init, tf_none);
8030 : 4881 : if (new_init)
8031 : : {
8032 : 4881 : init = new_init;
8033 : 4881 : if (TREE_CODE (init) == TARGET_EXPR
8034 : 347 : && !(flags & LOOKUP_ONLYCONVERTING))
8035 : 347 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8036 : : }
8037 : : init_code = NULL_TREE;
8038 : : }
8039 : : else
8040 : : init = NULL_TREE;
8041 : : }
8042 : :
8043 : 35741771 : if (init && TREE_CODE (init) != TREE_VEC)
8044 : : {
8045 : 35741756 : init_code = store_init_value (decl, init, cleanups, flags);
8046 : :
8047 : 35739059 : if (DECL_INITIAL (decl)
8048 : 22757645 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8049 : 37336819 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8050 : : {
8051 : 969771 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8052 : 969771 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8053 : 969771 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8054 : 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8055 : : }
8056 : :
8057 : 784628 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8058 : 47011 : && DECL_INITIAL (decl)
8059 : 46592 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8060 : 35782819 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8061 : 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8062 : 3 : DECL_SOURCE_LOCATION (decl)),
8063 : 3 : 0, "array %qD initialized by parenthesized "
8064 : : "string literal %qE",
8065 : 3 : decl, DECL_INITIAL (decl));
8066 : : init = NULL_TREE;
8067 : : }
8068 : : }
8069 : : else
8070 : : {
8071 : 7155702 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8072 : 4220582 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8073 : 642718 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8074 : 19 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8075 : : /*complain=*/true);
8076 : :
8077 : 3577851 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8078 : : tf_warning_or_error);
8079 : : }
8080 : :
8081 : 0 : if (init && init != error_mark_node)
8082 : 0 : init_code = cp_build_init_expr (decl, init);
8083 : :
8084 : 14032377 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8085 : 40272937 : && init_code != error_mark_node)
8086 : : init_code = NULL_TREE;
8087 : :
8088 : 40236712 : if (init_code)
8089 : : {
8090 : : /* We might have set these in cp_finish_decl. */
8091 : 14015364 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8092 : 14015364 : TREE_CONSTANT (decl) = false;
8093 : : }
8094 : :
8095 : 14015364 : if (init_code
8096 : 14015364 : && DECL_IN_AGGR_P (decl)
8097 : 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8098 : : {
8099 : 0 : static int explained = 0;
8100 : :
8101 : 0 : auto_diagnostic_group d;
8102 : 0 : if (cxx_dialect < cxx11)
8103 : 0 : error ("initializer invalid for static member with constructor");
8104 : 0 : else if (cxx_dialect < cxx17)
8105 : 0 : error ("non-constant in-class initialization invalid for static "
8106 : : "member %qD", decl);
8107 : : else
8108 : 0 : error ("non-constant in-class initialization invalid for non-inline "
8109 : : "static member %qD", decl);
8110 : 0 : if (!explained)
8111 : : {
8112 : 0 : inform (input_location,
8113 : : "(an out of class initialization is required)");
8114 : 0 : explained = 1;
8115 : : }
8116 : 0 : return NULL_TREE;
8117 : 0 : }
8118 : :
8119 : : return init_code;
8120 : : }
8121 : :
8122 : : /* If DECL is not a local variable, give it RTL. */
8123 : :
8124 : : static void
8125 : 96880205 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8126 : : {
8127 : 96880205 : int toplev = toplevel_bindings_p ();
8128 : 96880205 : int defer_p;
8129 : :
8130 : : /* Set the DECL_ASSEMBLER_NAME for the object. */
8131 : 96880205 : if (asmspec)
8132 : : {
8133 : : /* The `register' keyword, when used together with an
8134 : : asm-specification, indicates that the variable should be
8135 : : placed in a particular register. */
8136 : 959748 : if (VAR_P (decl) && DECL_REGISTER (decl))
8137 : : {
8138 : 81 : set_user_assembler_name (decl, asmspec);
8139 : 81 : DECL_HARD_REGISTER (decl) = 1;
8140 : : }
8141 : : else
8142 : : {
8143 : 959667 : if (TREE_CODE (decl) == FUNCTION_DECL
8144 : 959667 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8145 : 76044 : set_builtin_user_assembler_name (decl, asmspec);
8146 : 959667 : set_user_assembler_name (decl, asmspec);
8147 : 959667 : if (DECL_LOCAL_DECL_P (decl))
8148 : 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8149 : : /* We have to propagate the name to the ns-alias.
8150 : : This is horrible, as we're affecting a
8151 : : possibly-shared decl. Again, a one-true-decl
8152 : : model breaks down. */
8153 : 6 : if (ns_decl != error_mark_node)
8154 : 6 : set_user_assembler_name (ns_decl, asmspec);
8155 : : }
8156 : : }
8157 : :
8158 : : /* Handle non-variables up front. */
8159 : 96880205 : if (!VAR_P (decl))
8160 : : {
8161 : 50995669 : rest_of_decl_compilation (decl, toplev, at_eof);
8162 : 50995669 : return;
8163 : : }
8164 : :
8165 : : /* If we see a class member here, it should be a static data
8166 : : member. */
8167 : 45884536 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8168 : : {
8169 : 3809603 : gcc_assert (TREE_STATIC (decl));
8170 : : /* An in-class declaration of a static data member should be
8171 : : external; it is only a declaration, and not a definition. */
8172 : 3809603 : if (init == NULL_TREE)
8173 : 3809593 : gcc_assert (DECL_EXTERNAL (decl)
8174 : : || !TREE_PUBLIC (decl));
8175 : : }
8176 : :
8177 : : /* We don't create any RTL for local variables. */
8178 : 45884536 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8179 : : return;
8180 : :
8181 : : /* We defer emission of local statics until the corresponding
8182 : : DECL_EXPR is expanded. But with constexpr its function might never
8183 : : be expanded, so go ahead and tell cgraph about the variable now. */
8184 : 46400184 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8185 : 138781 : && !var_in_maybe_constexpr_fn (decl))
8186 : 46261513 : || DECL_VIRTUAL_P (decl));
8187 : :
8188 : : /* Defer template instantiations. */
8189 : 23200092 : if (DECL_LANG_SPECIFIC (decl)
8190 : 23200092 : && DECL_IMPLICIT_INSTANTIATION (decl))
8191 : : defer_p = 1;
8192 : :
8193 : : /* If we're not deferring, go ahead and assemble the variable. */
8194 : 14820538 : if (!defer_p)
8195 : 12539941 : rest_of_decl_compilation (decl, toplev, at_eof);
8196 : : }
8197 : :
8198 : : /* walk_tree helper for wrap_temporary_cleanups, below. */
8199 : :
8200 : : static tree
8201 : 10245026 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8202 : : {
8203 : : /* Stop at types or full-expression boundaries. */
8204 : 10245026 : if (TYPE_P (*stmt_p)
8205 : 10244815 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8206 : : {
8207 : 411 : *walk_subtrees = 0;
8208 : 411 : return NULL_TREE;
8209 : : }
8210 : :
8211 : 10244615 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8212 : : {
8213 : 174974 : tree guard = (tree)data;
8214 : 174974 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8215 : :
8216 : 96790 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8217 : 265317 : && !expr_noexcept_p (tcleanup, tf_none))
8218 : : {
8219 : 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8220 : : /* Tell honor_protect_cleanup_actions to handle this as a separate
8221 : : cleanup. */
8222 : 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8223 : 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8224 : : }
8225 : : }
8226 : :
8227 : : return NULL_TREE;
8228 : : }
8229 : :
8230 : : /* We're initializing a local variable which has a cleanup GUARD. If there
8231 : : are any temporaries used in the initializer INIT of this variable, we
8232 : : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8233 : : variable will be cleaned up properly if one of them throws.
8234 : :
8235 : : Unfortunately, there's no way to express this properly in terms of
8236 : : nesting, as the regions for the temporaries overlap the region for the
8237 : : variable itself; if there are two temporaries, the variable needs to be
8238 : : the first thing destroyed if either of the temporary destructors throws.
8239 : : However, we only want to run the variable's cleanup if it actually got
8240 : : constructed. So we need to guard the temporary cleanups with the
8241 : : variable's cleanup if they are run on the normal path, but not if they
8242 : : are run on the exceptional path. We implement this by telling
8243 : : honor_protect_cleanup_actions to strip the variable cleanup from the
8244 : : exceptional path.
8245 : :
8246 : : Another approach could be to make the variable cleanup region enclose
8247 : : initialization, but depend on a flag to indicate that the variable is
8248 : : initialized; that's effectively what we do for arrays. But the current
8249 : : approach works fine for non-arrays, and has no code overhead in the usual
8250 : : case where the temporary destructors are noexcept. */
8251 : :
8252 : : static void
8253 : 949971 : wrap_temporary_cleanups (tree init, tree guard)
8254 : : {
8255 : 949971 : if (TREE_CODE (guard) == BIND_EXPR)
8256 : : {
8257 : : /* An array cleanup region already encloses any temporary cleanups,
8258 : : don't wrap it around them again. */
8259 : 414 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8260 : : return;
8261 : : }
8262 : 949557 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8263 : : }
8264 : :
8265 : : /* Generate code to initialize DECL (a local variable). */
8266 : :
8267 : : static void
8268 : 22684426 : initialize_local_var (tree decl, tree init, bool decomp)
8269 : : {
8270 : 22684426 : tree type = TREE_TYPE (decl);
8271 : 22684426 : tree cleanup;
8272 : 22684426 : int already_used;
8273 : :
8274 : 22684426 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8275 : 22684426 : gcc_assert (!TREE_STATIC (decl));
8276 : :
8277 : 22684426 : if (DECL_SIZE (decl) == NULL_TREE)
8278 : : {
8279 : : /* If we used it already as memory, it must stay in memory. */
8280 : 0 : DECL_INITIAL (decl) = NULL_TREE;
8281 : 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8282 : 0 : return;
8283 : : }
8284 : :
8285 : 22684426 : if (type == error_mark_node)
8286 : : return;
8287 : :
8288 : : /* Compute and store the initial value. */
8289 : 22684426 : already_used = TREE_USED (decl) || TREE_USED (type);
8290 : 22684426 : if (TREE_USED (type))
8291 : 32 : DECL_READ_P (decl) = 1;
8292 : :
8293 : : /* Generate a cleanup, if necessary. */
8294 : 22684426 : cleanup = (decomp ? NULL_TREE
8295 : 22666560 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8296 : :
8297 : : /* Perform the initialization. */
8298 : 22684426 : if (init)
8299 : : {
8300 : 14001820 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8301 : 14001820 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8302 : 12819531 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8303 : 19961148 : && TREE_OPERAND (init, 0) == decl)
8304 : : {
8305 : : /* Stick simple initializers in DECL_INITIAL so that
8306 : : -Wno-init-self works (c++/34772). */
8307 : 7141617 : DECL_INITIAL (decl) = rinit;
8308 : :
8309 : 7141617 : if (warn_init_self && TYPE_REF_P (type))
8310 : : {
8311 : 2470 : STRIP_NOPS (rinit);
8312 : 2470 : if (rinit == decl)
8313 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
8314 : 3 : OPT_Winit_self,
8315 : : "reference %qD is initialized with itself", decl);
8316 : : }
8317 : : }
8318 : : else
8319 : : {
8320 : 6860203 : int saved_stmts_are_full_exprs_p;
8321 : :
8322 : : /* If we're only initializing a single object, guard the
8323 : : destructors of any temporaries used in its initializer with
8324 : : its destructor. */
8325 : 6860203 : if (cleanup)
8326 : 944094 : wrap_temporary_cleanups (init, cleanup);
8327 : :
8328 : 6860203 : gcc_assert (building_stmt_list_p ());
8329 : 6860203 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8330 : : /* Avoid CLEANUP_POINT_EXPR for the structured binding
8331 : : bases, those will have CLEANUP_POINT_EXPR at the end of
8332 : : code emitted by cp_finish_decomp. */
8333 : 6860203 : if (decomp)
8334 : 17634 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8335 : : else
8336 : 6842569 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8337 : 6860203 : finish_expr_stmt (init);
8338 : 6860203 : current_stmt_tree ()->stmts_are_full_exprs_p
8339 : 6860203 : = saved_stmts_are_full_exprs_p;
8340 : : }
8341 : : }
8342 : :
8343 : : /* Set this to 0 so we can tell whether an aggregate which was
8344 : : initialized was ever used. Don't do this if it has a
8345 : : destructor, so we don't complain about the 'resource
8346 : : allocation is initialization' idiom. Now set
8347 : : attribute((unused)) on types so decls of that type will be
8348 : : marked used. (see TREE_USED, above.) */
8349 : 22684426 : if (TYPE_NEEDS_CONSTRUCTING (type)
8350 : 1882094 : && ! already_used
8351 : 4485 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8352 : 22688486 : && DECL_NAME (decl))
8353 : 4034 : TREE_USED (decl) = 0;
8354 : 22680392 : else if (already_used)
8355 : 20533146 : TREE_USED (decl) = 1;
8356 : :
8357 : 22684426 : if (cleanup)
8358 : 956374 : finish_decl_cleanup (decl, cleanup);
8359 : : }
8360 : :
8361 : : /* DECL is a VAR_DECL for a compiler-generated variable with static
8362 : : storage duration (like a virtual table) whose initializer is a
8363 : : compile-time constant. Initialize the variable and provide it to the
8364 : : back end. */
8365 : :
8366 : : void
8367 : 2101852 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8368 : : {
8369 : 2101852 : tree init;
8370 : 2101852 : gcc_assert (DECL_ARTIFICIAL (decl));
8371 : 2101852 : init = build_constructor (TREE_TYPE (decl), v);
8372 : 2101852 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8373 : 2101852 : DECL_INITIAL (decl) = init;
8374 : 2101852 : DECL_INITIALIZED_P (decl) = 1;
8375 : : /* Mark the decl as constexpr so that we can access its content
8376 : : at compile time. */
8377 : 2101852 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8378 : 2101852 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
8379 : 2101852 : determine_visibility (decl);
8380 : 2101852 : layout_var_decl (decl);
8381 : 2101852 : maybe_commonize_var (decl);
8382 : 2101852 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8383 : 2101852 : }
8384 : :
8385 : : /* INIT is the initializer for a variable, as represented by the
8386 : : parser. Returns true iff INIT is value-dependent. */
8387 : :
8388 : : static bool
8389 : 7801497 : value_dependent_init_p (tree init)
8390 : : {
8391 : 7801497 : if (TREE_CODE (init) == TREE_LIST)
8392 : : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8393 : 10187 : return any_value_dependent_elements_p (init);
8394 : 7791310 : else if (TREE_CODE (init) == CONSTRUCTOR)
8395 : : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8396 : : {
8397 : 37271 : if (dependent_type_p (TREE_TYPE (init)))
8398 : : return true;
8399 : :
8400 : 37265 : vec<constructor_elt, va_gc> *elts;
8401 : 37265 : size_t nelts;
8402 : 37265 : size_t i;
8403 : :
8404 : 37265 : elts = CONSTRUCTOR_ELTS (init);
8405 : 37265 : nelts = vec_safe_length (elts);
8406 : 555478 : for (i = 0; i < nelts; ++i)
8407 : 530132 : if (value_dependent_init_p ((*elts)[i].value))
8408 : : return true;
8409 : : }
8410 : : else
8411 : : /* It must be a simple expression, e.g., int i = 3; */
8412 : 7754039 : return value_dependent_expression_p (init);
8413 : :
8414 : : return false;
8415 : : }
8416 : :
8417 : : /* A helper function to be called via walk_tree. If any label exists
8418 : : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8419 : :
8420 : : static tree
8421 : 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8422 : : {
8423 : 337 : if (TYPE_P (*tp))
8424 : 0 : *walk_subtrees = 0;
8425 : 337 : if (TREE_CODE (*tp) == LABEL_DECL)
8426 : 6 : cfun->has_forced_label_in_static = 1;
8427 : 337 : return NULL_TREE;
8428 : : }
8429 : :
8430 : : /* Return true if DECL has either a trivial destructor, or for C++20
8431 : : is constexpr and has a constexpr destructor. */
8432 : :
8433 : : static bool
8434 : 33635554 : decl_maybe_constant_destruction (tree decl, tree type)
8435 : : {
8436 : 33635554 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8437 : 33635554 : || (cxx_dialect >= cxx20
8438 : 1271313 : && VAR_P (decl)
8439 : 1271058 : && DECL_DECLARED_CONSTEXPR_P (decl)
8440 : 291 : && type_has_constexpr_destructor (strip_array_types (type))));
8441 : : }
8442 : :
8443 : : static tree declare_simd_adjust_this (tree *, int *, void *);
8444 : :
8445 : : /* Helper function of omp_declare_variant_finalize. Finalize one
8446 : : "omp declare variant base" attribute. Return true if it should be
8447 : : removed. */
8448 : :
8449 : : static bool
8450 : 1487 : omp_declare_variant_finalize_one (tree decl, tree attr)
8451 : : {
8452 : 1487 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8453 : : {
8454 : 116 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8455 : : DECL_ARGUMENTS (decl), NULL);
8456 : 116 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8457 : : DECL_ARGUMENTS (decl), NULL);
8458 : : }
8459 : :
8460 : 1487 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
8461 : 1487 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8462 : : OMP_TRAIT_CONSTRUCT_SIMD);
8463 : 1487 : if (simd)
8464 : : {
8465 : 51 : TREE_VALUE (simd)
8466 : 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8467 : 51 : OMP_TS_PROPERTIES (simd));
8468 : : /* FIXME, adjusting simd args unimplemented. */
8469 : 51 : return true;
8470 : : }
8471 : :
8472 : 1436 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
8473 : 1436 : location_t varid_loc
8474 : 1436 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8475 : 1436 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8476 : 1436 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8477 : 1436 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8478 : :
8479 : 1436 : location_t save_loc = input_location;
8480 : 1436 : input_location = varid_loc;
8481 : :
8482 : 1436 : releasing_vec args;
8483 : 1436 : tree parm = DECL_ARGUMENTS (decl);
8484 : 1436 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8485 : 116 : parm = DECL_CHAIN (parm);
8486 : 2644 : for (; parm; parm = DECL_CHAIN (parm))
8487 : 1208 : vec_safe_push (args, forward_parm (parm));
8488 : :
8489 : 1436 : unsigned nappend_args = 0;
8490 : 1436 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8491 : 1436 : if (append_args_list)
8492 : : {
8493 : 357 : append_args_list = TREE_VALUE (append_args_list);
8494 : 357 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
8495 : 594 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
8496 : : : NULL_TREE);
8497 : 744 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8498 : 387 : nappend_args++;
8499 : 357 : if (nappend_args)
8500 : : {
8501 : 237 : tree type;
8502 : 237 : if ((type = lookup_qualified_name (global_namespace,
8503 : : "omp_interop_t",
8504 : : LOOK_want::NORMAL,
8505 : : /*complain*/false)) == NULL_TREE
8506 : 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
8507 : : {
8508 : 24 : location_t loc = input_location;
8509 : 24 : variant = tree_strip_any_location_wrapper (variant);
8510 : 24 : if (!identifier_p (variant))
8511 : : {
8512 : 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
8513 : 21 : variant = OVL_FIRST (variant);
8514 : 21 : loc = EXPR_LOC_OR_LOC (variant,
8515 : : DECL_SOURCE_LOCATION (variant));
8516 : : }
8517 : 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
8518 : 24 : args->length () + 1, variant);
8519 : 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8520 : : "%<append_args%> specified here");
8521 : 24 : return true;
8522 : : }
8523 : 564 : for (unsigned i = 0; i < nappend_args; i++)
8524 : 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
8525 : : }
8526 : : }
8527 : :
8528 : 1412 : bool koenig_p = false;
8529 : 1412 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
8530 : : {
8531 : 1406 : if (identifier_p (variant)
8532 : : /* In C++20, we may need to perform ADL for a template
8533 : : name. */
8534 : 1382 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
8535 : 18 : && identifier_p (TREE_OPERAND (variant, 0))))
8536 : : {
8537 : 24 : if (!args->is_empty ())
8538 : : {
8539 : 24 : koenig_p = true;
8540 : 24 : if (!any_type_dependent_arguments_p (args))
8541 : 21 : variant = perform_koenig_lookup (variant, args,
8542 : : tf_warning_or_error);
8543 : : }
8544 : : else
8545 : 0 : variant = unqualified_fn_lookup_error (variant);
8546 : : }
8547 : 1382 : else if (!args->is_empty () && is_overloaded_fn (variant))
8548 : : {
8549 : 716 : tree fn = get_first_fn (variant);
8550 : 716 : fn = STRIP_TEMPLATE (fn);
8551 : 716 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8552 : 716 : || DECL_FUNCTION_MEMBER_P (fn)
8553 : 603 : || DECL_LOCAL_DECL_P (fn)))
8554 : : {
8555 : 603 : koenig_p = true;
8556 : 603 : if (!any_type_dependent_arguments_p (args))
8557 : 552 : variant = perform_koenig_lookup (variant, args,
8558 : : tf_warning_or_error);
8559 : : }
8560 : : }
8561 : : }
8562 : :
8563 : 1412 : if (idk == CP_ID_KIND_QUALIFIED)
8564 : 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8565 : : koenig_p, tf_warning_or_error);
8566 : : else
8567 : 1406 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8568 : : koenig_p, tf_warning_or_error);
8569 : 1412 : if (variant == error_mark_node && !processing_template_decl)
8570 : : return true;
8571 : :
8572 : 1367 : if (TREE_CODE (variant) == TARGET_EXPR)
8573 : 9 : variant = TARGET_EXPR_INITIAL (variant);
8574 : :
8575 : 2725 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
8576 : 1367 : input_location = save_loc;
8577 : :
8578 : 1367 : if (variant)
8579 : : {
8580 : 1283 : bool fail;
8581 : 1283 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8582 : 1283 : if (!nappend_args)
8583 : 1124 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8584 : : COMPARE_STRICT);
8585 : : else
8586 : : {
8587 : 159 : unsigned nbase_args = 0;
8588 : 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
8589 : 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
8590 : 240 : nbase_args++;
8591 : 159 : tree vargs, varg;
8592 : 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
8593 : 399 : for (unsigned i = 0; i < nbase_args && varg;
8594 : 240 : i++, varg = TREE_CHAIN (varg))
8595 : 240 : vargs = varg;
8596 : 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
8597 : 258 : varg = TREE_CHAIN (varg);
8598 : 159 : tree saved_vargs;
8599 : 159 : if (nbase_args)
8600 : : {
8601 : 111 : saved_vargs = TREE_CHAIN (vargs);
8602 : 111 : TREE_CHAIN (vargs) = varg;
8603 : : }
8604 : : else
8605 : : {
8606 : 48 : saved_vargs = vargs;
8607 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
8608 : : }
8609 : : /* Skip assert check that TYPE_CANONICAL is the same. */
8610 : 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8611 : : COMPARE_STRUCTURAL);
8612 : 159 : if (nbase_args)
8613 : 111 : TREE_CHAIN (vargs) = saved_vargs;
8614 : : else
8615 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
8616 : 159 : varg = saved_vargs;
8617 : 159 : if (!fail && !processing_template_decl)
8618 : 402 : for (unsigned i = 0; i < nappend_args;
8619 : 246 : i++, varg = TREE_CHAIN (varg))
8620 : 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
8621 : : {
8622 : 9 : error_at (DECL_SOURCE_LOCATION (variant),
8623 : : "argument %d of %qD must be of %<omp_interop_t%>",
8624 : 9 : nbase_args + i + 1, variant);
8625 : 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8626 : : "%<append_args%> specified here");
8627 : 9 : break;
8628 : : }
8629 : : }
8630 : 1283 : if (fail)
8631 : : {
8632 : 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
8633 : : "types", variant, decl);
8634 : 69 : return true;
8635 : : }
8636 : 1214 : if (fndecl_built_in_p (variant)
8637 : 1214 : && (startswith (varname, "__builtin_")
8638 : 0 : || startswith (varname, "__sync_")
8639 : 0 : || startswith (varname, "__atomic_")))
8640 : : {
8641 : 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
8642 : 6 : return true;
8643 : : }
8644 : : else
8645 : : {
8646 : 1208 : tree construct
8647 : 1208 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
8648 : 1208 : omp_mark_declare_variant (match_loc, variant, construct);
8649 : 1208 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
8650 : : return true;
8651 : 860 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8652 : :
8653 : : // Prepend adjust_args list to variant attributes
8654 : 860 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8655 : 860 : if (adjust_args_list != NULL_TREE)
8656 : : {
8657 : 513 : if (DECL_NONSTATIC_MEMBER_P (variant)
8658 : 288 : && TREE_VALUE (adjust_args_list))
8659 : : {
8660 : : /* Shift arg position for the added 'this' pointer. */
8661 : : /* Handle need_device_ptr */
8662 : 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
8663 : 51 : t; t = TREE_CHAIN (t))
8664 : 30 : TREE_VALUE (t)
8665 : 30 : = build_int_cst (TREE_TYPE (t),
8666 : 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
8667 : : }
8668 : 267 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
8669 : : {
8670 : : /* Shift likewise the number of args after which the
8671 : : interop object should be added. */
8672 : 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
8673 : 6 : TREE_PURPOSE (nargs)
8674 : 6 : = build_int_cst (TREE_TYPE (nargs),
8675 : 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
8676 : : }
8677 : 531 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8678 : 264 : TREE_VALUE (t)
8679 : 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
8680 : 534 : DECL_ATTRIBUTES (variant) = tree_cons (
8681 : : get_identifier ("omp declare variant variant args"),
8682 : 534 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
8683 : : }
8684 : : }
8685 : : }
8686 : 84 : else if (!processing_template_decl)
8687 : : {
8688 : 0 : error_at (varid_loc, "could not find variant declaration");
8689 : 0 : return true;
8690 : : }
8691 : :
8692 : : return false;
8693 : 1436 : }
8694 : :
8695 : : /* Helper function, finish up "omp declare variant base" attribute
8696 : : now that there is a DECL. ATTR is the first "omp declare variant base"
8697 : : attribute. */
8698 : :
8699 : : void
8700 : 1350 : omp_declare_variant_finalize (tree decl, tree attr)
8701 : : {
8702 : 1350 : size_t attr_len = strlen ("omp declare variant base");
8703 : 1350 : tree *list = &DECL_ATTRIBUTES (decl);
8704 : 1350 : bool remove_all = false;
8705 : 1350 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
8706 : 1350 : if (TREE_CHAIN (TREE_VALUE (attr))
8707 : 1350 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8708 : 2700 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8709 : 1350 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8710 : 2700 : if (DECL_CONSTRUCTOR_P (decl))
8711 : : {
8712 : 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8713 : 27 : remove_all = true;
8714 : : }
8715 : 1323 : else if (DECL_DESTRUCTOR_P (decl))
8716 : : {
8717 : 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8718 : 9 : remove_all = true;
8719 : : }
8720 : 1314 : else if (DECL_DEFAULTED_FN (decl))
8721 : : {
8722 : 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8723 : 2 : remove_all = true;
8724 : : }
8725 : 1312 : else if (DECL_DELETED_FN (decl))
8726 : : {
8727 : 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8728 : 2 : remove_all = true;
8729 : : }
8730 : 1310 : else if (DECL_VIRTUAL_P (decl))
8731 : : {
8732 : 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8733 : 0 : remove_all = true;
8734 : : }
8735 : : /* This loop is like private_lookup_attribute, except that it works
8736 : : with tree * rather than tree, as we might want to remove the
8737 : : attributes that are diagnosed as errorneous. */
8738 : 2889 : while (*list)
8739 : : {
8740 : 1539 : tree attr = get_attribute_name (*list);
8741 : 1539 : size_t ident_len = IDENTIFIER_LENGTH (attr);
8742 : 4022 : if (cmp_attribs ("omp declare variant base", attr_len,
8743 : 1539 : IDENTIFIER_POINTER (attr), ident_len))
8744 : : {
8745 : 1527 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8746 : : {
8747 : 583 : *list = TREE_CHAIN (*list);
8748 : 583 : continue;
8749 : : }
8750 : : }
8751 : 956 : list = &TREE_CHAIN (*list);
8752 : : }
8753 : 1350 : }
8754 : :
8755 : : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
8756 : :
8757 : : /* Finish processing of a declaration;
8758 : : install its line number and initial value.
8759 : : If the length of an array type is not known before,
8760 : : it must be determined now, from the initial value, or it is an error.
8761 : :
8762 : : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8763 : : true, then INIT is an integral constant expression.
8764 : :
8765 : : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8766 : : if the (init) syntax was used.
8767 : :
8768 : : DECOMP is first identifier's DECL and identifier count in a structured
8769 : : bindings, nullptr if not a structured binding. */
8770 : :
8771 : : void
8772 : 270696911 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8773 : : tree asmspec_tree, int flags, cp_decomp *decomp)
8774 : : {
8775 : 270696911 : vec<tree, va_gc> *cleanups = NULL;
8776 : 270696911 : const char *asmspec = NULL;
8777 : 270696911 : int was_readonly = 0;
8778 : 270696911 : bool var_definition_p = false;
8779 : 270696911 : tree auto_node;
8780 : 270696911 : auto_vec<tree> extra_cleanups;
8781 : 270696911 : tree aggregates1 = NULL_TREE;
8782 : 270696911 : struct decomp_cleanup {
8783 : : tree decl;
8784 : : cp_decomp *&decomp;
8785 : 270694214 : ~decomp_cleanup ()
8786 : : {
8787 : 270694214 : if (decomp && DECL_DECOMPOSITION_P (decl))
8788 : 72209 : cp_finish_decomp (decl, decomp);
8789 : 270694214 : }
8790 : 270696911 : } decomp_cl = { decl, decomp };
8791 : :
8792 : 270696911 : if (decl == error_mark_node)
8793 : : return;
8794 : 270696886 : else if (! decl)
8795 : : {
8796 : 0 : if (init)
8797 : 0 : error ("assignment (not initialization) in declaration");
8798 : 0 : return;
8799 : : }
8800 : :
8801 : 270696886 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8802 : : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8803 : 270696886 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
8804 : :
8805 : 270696886 : tree type = TREE_TYPE (decl);
8806 : 270696886 : if (type == error_mark_node)
8807 : : return;
8808 : :
8809 : 270696391 : if (VAR_P (decl) && is_copy_initialization (init))
8810 : 74217505 : flags |= LOOKUP_ONLYCONVERTING;
8811 : :
8812 : : /* Warn about register storage specifiers except when in GNU global
8813 : : or local register variable extension. */
8814 : 270696391 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8815 : : {
8816 : 1922 : if (cxx_dialect >= cxx17)
8817 : 1276 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8818 : : "ISO C++17 does not allow %<register%> storage "
8819 : : "class specifier");
8820 : : else
8821 : 646 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8822 : : "%<register%> storage class specifier used");
8823 : : }
8824 : :
8825 : : /* If a name was specified, get the string. */
8826 : 270696391 : if (at_namespace_scope_p ())
8827 : 55888189 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8828 : 270696391 : if (asmspec_tree && asmspec_tree != error_mark_node)
8829 : 959751 : asmspec = TREE_STRING_POINTER (asmspec_tree);
8830 : :
8831 : 270696391 : bool in_class_decl
8832 : 270696391 : = (current_class_type
8833 : 168022108 : && CP_DECL_CONTEXT (decl) == current_class_type
8834 : 112716756 : && TYPE_BEING_DEFINED (current_class_type)
8835 : 381920687 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8836 : :
8837 : 108186709 : if (in_class_decl
8838 : 108186709 : && (DECL_INITIAL (decl) || init))
8839 : 77650744 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8840 : :
8841 : 270696391 : if (VAR_P (decl)
8842 : 270696391 : && (auto_node = type_uses_auto (type)))
8843 : : {
8844 : 8299917 : tree d_init;
8845 : 8299917 : if (init == NULL_TREE)
8846 : : {
8847 : 1163 : if (DECL_LANG_SPECIFIC (decl)
8848 : 1085 : && DECL_TEMPLATE_INSTANTIATION (decl)
8849 : 2245 : && !DECL_TEMPLATE_INSTANTIATED (decl))
8850 : : {
8851 : : /* init is null because we're deferring instantiating the
8852 : : initializer until we need it. Well, we need it now. */
8853 : 1076 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8854 : 1076 : return;
8855 : : }
8856 : :
8857 : 87 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8858 : : /* Class deduction with no initializer is OK. */;
8859 : : else
8860 : : {
8861 : : /* Ordinary auto deduction without an initializer, a situation
8862 : : which grokdeclarator already detects and rejects for the most
8863 : : part. But we can still get here if we're instantiating a
8864 : : variable template before we've fully parsed (and attached) its
8865 : : initializer, e.g. template<class> auto x = x<int>; */
8866 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
8867 : : "declaration of %q#D has no initializer", decl);
8868 : 3 : TREE_TYPE (decl) = error_mark_node;
8869 : 3 : return;
8870 : : }
8871 : : }
8872 : 8298838 : d_init = init;
8873 : 8298838 : if (d_init)
8874 : : {
8875 : 8298754 : if (TREE_CODE (d_init) == TREE_LIST
8876 : 8340310 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8877 : 21871 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8878 : : tf_warning_or_error);
8879 : 8298754 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8880 : : /* Force auto deduction now. Use tf_none to avoid redundant warnings
8881 : : on deprecated-14.C. */
8882 : 8298754 : mark_single_function (d_init, tf_none);
8883 : : }
8884 : 8298838 : enum auto_deduction_context adc = adc_variable_type;
8885 : 8298838 : if (DECL_DECOMPOSITION_P (decl))
8886 : : adc = adc_decomp_type;
8887 : 8298838 : tree outer_targs = NULL_TREE;
8888 : 8298838 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8889 : 323 : && DECL_LANG_SPECIFIC (decl)
8890 : 126 : && DECL_TEMPLATE_INFO (decl)
8891 : 8298895 : && !DECL_FUNCTION_SCOPE_P (decl))
8892 : : /* The outer template arguments might be needed for satisfaction.
8893 : : (For function scope variables, do_auto_deduction will obtain the
8894 : : outer template arguments from current_function_decl.) */
8895 : 57 : outer_targs = DECL_TI_ARGS (decl);
8896 : 8298838 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8897 : : tf_warning_or_error, adc,
8898 : : outer_targs, flags);
8899 : 8298838 : if (type == error_mark_node)
8900 : : return;
8901 : 8297999 : if (TREE_CODE (type) == FUNCTION_TYPE)
8902 : : {
8903 : 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
8904 : : "did you forget the %<()%>?", decl);
8905 : 3 : TREE_TYPE (decl) = error_mark_node;
8906 : 3 : return;
8907 : : }
8908 : : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8909 : 8297996 : if (!processing_template_decl
8910 : 1903684 : && !type_uses_auto (type)
8911 : 10201673 : && !COMPLETE_TYPE_P (complete_type (type)))
8912 : : {
8913 : 15 : auto_diagnostic_group d;
8914 : 15 : error_at (location_of (decl),
8915 : : "deduced type %qT for %qD is incomplete", type, decl);
8916 : 15 : cxx_incomplete_type_inform (type);
8917 : 15 : TREE_TYPE (decl) = error_mark_node;
8918 : 15 : return;
8919 : 15 : }
8920 : :
8921 : : /* Now that we have a type, try these again. */
8922 : 8297981 : layout_decl (decl, 0);
8923 : 8297981 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8924 : :
8925 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
8926 : 8297981 : if (DECL_LANG_SPECIFIC (decl)
8927 : 127858 : && DECL_TEMPLATE_INFO (decl)
8928 : 8304066 : && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8929 : 4858 : TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8930 : : }
8931 : :
8932 : 270694455 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8933 : : {
8934 : 54 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8935 : 54 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8936 : : {
8937 : 6 : init = NULL_TREE;
8938 : 6 : DECL_EXTERNAL (decl) = 1;
8939 : : }
8940 : : }
8941 : :
8942 : 270694455 : if (VAR_P (decl)
8943 : 95178779 : && DECL_CLASS_SCOPE_P (decl)
8944 : 14133236 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
8945 : : TCTX_STATIC_STORAGE, type)
8946 : 284827691 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8947 : 13018567 : check_static_variable_definition (decl, type);
8948 : :
8949 : 270694455 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8950 : : {
8951 : 21100795 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8952 : 21100795 : ? TCTX_THREAD_STORAGE
8953 : : : TCTX_STATIC_STORAGE);
8954 : 21100795 : verify_type_context (input_location, context, TREE_TYPE (decl));
8955 : : }
8956 : :
8957 : 270694455 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
8958 : : {
8959 : 343784 : tree clone;
8960 : 343784 : if (init == ridpointers[(int)RID_DELETE]
8961 : 343784 : || (TREE_CODE (init) == STRING_CST
8962 : 24 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
8963 : : {
8964 : : /* FIXME check this is 1st decl. */
8965 : 321292 : if (UNLIKELY (DECL_MAIN_P (decl)))
8966 : : {
8967 : : /* [basic.start.main]/3: A program that defines main as deleted
8968 : : is ill-formed. */
8969 : 3 : error ("%<::main%> cannot be deleted");
8970 : 3 : DECL_INITIAL (decl) = NULL_TREE;
8971 : : }
8972 : : else
8973 : : {
8974 : 321289 : DECL_DELETED_FN (decl) = 1;
8975 : 321289 : DECL_DECLARED_INLINE_P (decl) = 1;
8976 : 321289 : DECL_INITIAL (decl)
8977 : 321289 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
8978 : 321307 : FOR_EACH_CLONE (clone, decl)
8979 : : {
8980 : 18 : DECL_DELETED_FN (clone) = 1;
8981 : 18 : DECL_DECLARED_INLINE_P (clone) = 1;
8982 : 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
8983 : : }
8984 : : }
8985 : 321292 : init = NULL_TREE;
8986 : : }
8987 : 22492 : else if (init == ridpointers[(int)RID_DEFAULT])
8988 : : {
8989 : 22480 : if (defaultable_fn_check (decl))
8990 : 22456 : DECL_DEFAULTED_FN (decl) = 1;
8991 : : else
8992 : 24 : DECL_INITIAL (decl) = NULL_TREE;
8993 : : }
8994 : : }
8995 : :
8996 : 270694455 : if (init && VAR_P (decl))
8997 : : {
8998 : 81111674 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8999 : : /* If DECL is a reference, then we want to know whether init is a
9000 : : reference constant; init_const_expr_p as passed tells us whether
9001 : : it's an rvalue constant. */
9002 : 81111674 : if (TYPE_REF_P (type))
9003 : 2180235 : init_const_expr_p = potential_constant_expression (init);
9004 : 81111674 : if (init_const_expr_p)
9005 : : {
9006 : : /* Set these flags now for templates. We'll update the flags in
9007 : : store_init_value for instantiations. */
9008 : 69295449 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9009 : 69295449 : if (decl_maybe_constant_var_p (decl)
9010 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9011 : 69295449 : && !TYPE_REF_P (type))
9012 : 24304854 : TREE_CONSTANT (decl) = true;
9013 : : }
9014 : : /* This is handled mostly by gimplify.cc, but we have to deal with
9015 : : not warning about int x = x; as it is a GCC extension to turn off
9016 : : this warning but only if warn_init_self is zero. */
9017 : 81111674 : if (!DECL_EXTERNAL (decl)
9018 : 79701089 : && !TREE_STATIC (decl)
9019 : 60893931 : && decl == tree_strip_any_location_wrapper (init)
9020 : 81114915 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9021 : 3217 : suppress_warning (decl, OPT_Winit_self);
9022 : : }
9023 : 189582781 : else if (VAR_P (decl)
9024 : 14067105 : && COMPLETE_TYPE_P (type)
9025 : 11034500 : && !TYPE_REF_P (type)
9026 : 11034136 : && !dependent_type_p (type)
9027 : 200333368 : && is_really_empty_class (type, /*ignore_vptr*/false))
9028 : : /* We have no initializer but there's nothing to initialize anyway.
9029 : : Treat DECL as constant due to c++/109876. */
9030 : 479070 : TREE_CONSTANT (decl) = true;
9031 : :
9032 : 270694455 : if (flag_openmp
9033 : 713685 : && TREE_CODE (decl) == FUNCTION_DECL
9034 : : /* #pragma omp declare variant on methods handled in finish_struct
9035 : : instead. */
9036 : 271021186 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9037 : 129106 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9038 : 198078 : if (tree attr = lookup_attribute ("omp declare variant base",
9039 : 198078 : DECL_ATTRIBUTES (decl)))
9040 : 967 : omp_declare_variant_finalize (decl, attr);
9041 : :
9042 : 270694455 : if (processing_template_decl)
9043 : : {
9044 : 158140188 : bool type_dependent_p;
9045 : :
9046 : : /* Add this declaration to the statement-tree. */
9047 : 158140188 : if (at_function_scope_p ())
9048 : 57532440 : add_decl_expr (decl);
9049 : :
9050 : 158140188 : type_dependent_p = dependent_type_p (type);
9051 : :
9052 : 158140188 : if (check_for_bare_parameter_packs (init))
9053 : : {
9054 : 3 : init = NULL_TREE;
9055 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9056 : : }
9057 : :
9058 : : /* Generally, initializers in templates are expanded when the
9059 : : template is instantiated. But, if DECL is a variable constant
9060 : : then it can be used in future constant expressions, so its value
9061 : : must be available. */
9062 : :
9063 : 158140188 : bool dep_init = false;
9064 : :
9065 : 158140188 : if (!VAR_P (decl) || type_dependent_p)
9066 : : /* We can't do anything if the decl has dependent type. */;
9067 : 19257809 : else if (init
9068 : 17712662 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9069 : 13996738 : && !TYPE_REF_P (type)
9070 : 13972752 : && decl_maybe_constant_var_p (decl)
9071 : 26529174 : && !(dep_init = value_dependent_init_p (init)))
9072 : : {
9073 : : /* This variable seems to be a non-dependent constant, so process
9074 : : its initializer. If check_initializer returns non-null the
9075 : : initialization wasn't constant after all. */
9076 : 1680292 : tree init_code;
9077 : 1680292 : cleanups = make_tree_vector ();
9078 : 1680292 : init_code = check_initializer (decl, init, flags, &cleanups);
9079 : 1680292 : if (init_code == NULL_TREE)
9080 : 1680292 : init = NULL_TREE;
9081 : 1680292 : release_tree_vector (cleanups);
9082 : : }
9083 : : else
9084 : : {
9085 : 17577517 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9086 : : /* Try to deduce array size. */
9087 : 17577517 : maybe_deduce_size_from_array_init (decl, init);
9088 : : /* And complain about multiple initializers. */
9089 : 16032370 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9090 : 17640322 : && !MAYBE_CLASS_TYPE_P (type))
9091 : 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9092 : : tf_warning_or_error);
9093 : : }
9094 : :
9095 : 158140188 : if (init)
9096 : 45871007 : DECL_INITIAL (decl) = init;
9097 : :
9098 : 158140188 : if (dep_init)
9099 : : {
9100 : 5591073 : retrofit_lang_decl (decl);
9101 : 5591073 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9102 : : }
9103 : :
9104 : 158140188 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9105 : : {
9106 : 3 : set_user_assembler_name (decl, asmspec);
9107 : 3 : DECL_HARD_REGISTER (decl) = 1;
9108 : : }
9109 : 158140188 : return;
9110 : : }
9111 : :
9112 : : /* Just store non-static data member initializers for later. */
9113 : 112554267 : if (init && TREE_CODE (decl) == FIELD_DECL)
9114 : 377326 : DECL_INITIAL (decl) = init;
9115 : :
9116 : : /* Take care of TYPE_DECLs up front. */
9117 : 112554267 : if (TREE_CODE (decl) == TYPE_DECL)
9118 : : {
9119 : 9430404 : if (type != error_mark_node
9120 : 9430404 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9121 : : {
9122 : 4287787 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9123 : 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9124 : 4287787 : set_identifier_type_value (DECL_NAME (decl), decl);
9125 : : }
9126 : :
9127 : : /* If we have installed this as the canonical typedef for this
9128 : : type, and that type has not been defined yet, delay emitting
9129 : : the debug information for it, as we will emit it later. */
9130 : 9430404 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9131 : 9430404 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9132 : 546002 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9133 : :
9134 : 9430404 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9135 : : at_eof);
9136 : 9430404 : return;
9137 : : }
9138 : :
9139 : : /* A reference will be modified here, as it is initialized. */
9140 : 103123863 : if (! DECL_EXTERNAL (decl)
9141 : 46006248 : && TREE_READONLY (decl)
9142 : 125868344 : && TYPE_REF_P (type))
9143 : : {
9144 : 225970 : was_readonly = 1;
9145 : 225970 : TREE_READONLY (decl) = 0;
9146 : : }
9147 : :
9148 : : /* This needs to happen before extend_ref_init_temps. */
9149 : 103123863 : if (VAR_OR_FUNCTION_DECL_P (decl))
9150 : : {
9151 : 94781062 : if (VAR_P (decl))
9152 : 43785393 : maybe_commonize_var (decl);
9153 : 94781062 : determine_visibility (decl);
9154 : : }
9155 : :
9156 : 103123863 : if (VAR_P (decl))
9157 : : {
9158 : 43785393 : duration_kind dk = decl_storage_duration (decl);
9159 : : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9160 : : only to a declaration of a variable with static or thread storage
9161 : : duration." */
9162 : 43785393 : if (DECL_DECLARED_CONSTINIT_P (decl)
9163 : 43785393 : && !(dk == dk_thread || dk == dk_static))
9164 : : {
9165 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
9166 : : "%<constinit%> can only be applied to a variable with "
9167 : : "static or thread storage duration");
9168 : 12 : return;
9169 : : }
9170 : :
9171 : 43785381 : if (decomp)
9172 : : {
9173 : 33937 : cp_maybe_mangle_decomp (decl, decomp);
9174 : 33937 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9175 : : {
9176 : 259 : if (CP_DECL_THREAD_LOCAL_P (decl))
9177 : 37 : aggregates1 = tls_aggregates;
9178 : : else
9179 : 222 : aggregates1 = static_aggregates;
9180 : : }
9181 : : }
9182 : :
9183 : : /* If this is a local variable that will need a mangled name,
9184 : : register it now. We must do this before processing the
9185 : : initializer for the variable, since the initialization might
9186 : : require a guard variable, and since the mangled name of the
9187 : : guard variable will depend on the mangled name of this
9188 : : variable. */
9189 : 87570762 : if (DECL_FUNCTION_SCOPE_P (decl)
9190 : 22823225 : && TREE_STATIC (decl)
9191 : 43924162 : && !DECL_ARTIFICIAL (decl))
9192 : : {
9193 : : /* The variable holding an anonymous union will have had its
9194 : : discriminator set in finish_anon_union, after which it's
9195 : : NAME will have been cleared. */
9196 : 138481 : if (DECL_NAME (decl))
9197 : 138438 : determine_local_discriminator (decl);
9198 : : /* Normally has_forced_label_in_static is set during GIMPLE
9199 : : lowering, but [cd]tors are never actually compiled directly.
9200 : : We need to set this early so we can deal with the label
9201 : : address extension. */
9202 : 138481 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
9203 : 138452 : || DECL_DESTRUCTOR_P (current_function_decl))
9204 : 138491 : && init)
9205 : : {
9206 : 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
9207 : 29 : add_local_decl (cfun, decl);
9208 : : }
9209 : : /* And make sure it's in the symbol table for
9210 : : c_parse_final_cleanups to find. */
9211 : 138481 : varpool_node::get_create (decl);
9212 : : }
9213 : :
9214 : 43785381 : if (flag_openmp
9215 : 120134 : && VAR_P (decl)
9216 : 120134 : && DECL_LANG_SPECIFIC (decl)
9217 : 45665 : && DECL_OMP_DECLARE_MAPPER_P (decl)
9218 : 43785385 : && init)
9219 : : {
9220 : 3 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9221 : 3 : DECL_INITIAL (decl) = init;
9222 : : }
9223 : : /* Convert the initializer to the type of DECL, if we have not
9224 : : already initialized DECL. */
9225 : 43785378 : else if (!DECL_INITIALIZED_P (decl)
9226 : : /* If !DECL_EXTERNAL then DECL is being defined. In the
9227 : : case of a static data member initialized inside the
9228 : : class-specifier, there can be an initializer even if DECL
9229 : : is *not* defined. */
9230 : 43785378 : && (!DECL_EXTERNAL (decl) || init))
9231 : : {
9232 : 38576325 : cleanups = make_tree_vector ();
9233 : 38576325 : init = check_initializer (decl, init, flags, &cleanups);
9234 : :
9235 : : /* Handle:
9236 : :
9237 : : [dcl.init]
9238 : :
9239 : : The memory occupied by any object of static storage
9240 : : duration is zero-initialized at program startup before
9241 : : any other initialization takes place.
9242 : :
9243 : : We cannot create an appropriate initializer until after
9244 : : the type of DECL is finalized. If DECL_INITIAL is set,
9245 : : then the DECL is statically initialized, and any
9246 : : necessary zero-initialization has already been performed. */
9247 : 38573628 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9248 : 408838 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9249 : : /*nelts=*/NULL_TREE,
9250 : : /*static_storage_p=*/true);
9251 : : /* Remember that the initialization for this variable has
9252 : : taken place. */
9253 : 38573628 : DECL_INITIALIZED_P (decl) = 1;
9254 : : /* This declaration is the definition of this variable,
9255 : : unless we are initializing a static data member within
9256 : : the class specifier. */
9257 : 38573628 : if (!DECL_EXTERNAL (decl))
9258 : 43782684 : var_definition_p = true;
9259 : : }
9260 : : /* If the variable has an array type, lay out the type, even if
9261 : : there is no initializer. It is valid to index through the
9262 : : array, and we must get TYPE_ALIGN set correctly on the array
9263 : : type. */
9264 : 5209053 : else if (TREE_CODE (type) == ARRAY_TYPE)
9265 : 215054 : layout_type (type);
9266 : :
9267 : 43782684 : if (TREE_STATIC (decl)
9268 : 20541082 : && !at_function_scope_p ()
9269 : 64184985 : && current_function_decl == NULL)
9270 : : /* So decl is a global variable or a static member of a
9271 : : non local class. Record the types it uses
9272 : : so that we can decide later to emit debug info for them. */
9273 : 20402289 : record_types_used_by_current_var_decl (decl);
9274 : : }
9275 : :
9276 : : /* Add this declaration to the statement-tree. This needs to happen
9277 : : after the call to check_initializer so that the DECL_EXPR for a
9278 : : reference temp is added before the DECL_EXPR for the reference itself. */
9279 : 103121154 : if (DECL_FUNCTION_SCOPE_P (decl))
9280 : : {
9281 : : /* If we're building a variable sized type, and we might be
9282 : : reachable other than via the top of the current binding
9283 : : level, then create a new BIND_EXPR so that we deallocate
9284 : : the object at the right time. */
9285 : 22823225 : if (VAR_P (decl)
9286 : 22823225 : && DECL_SIZE (decl)
9287 : 22774578 : && !TREE_CONSTANT (DECL_SIZE (decl))
9288 : 22824209 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9289 : : {
9290 : 3 : tree bind;
9291 : 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9292 : 3 : TREE_SIDE_EFFECTS (bind) = 1;
9293 : 3 : add_stmt (bind);
9294 : 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
9295 : : }
9296 : 22823225 : add_decl_expr (decl);
9297 : : }
9298 : :
9299 : 103121154 : tree decomp_init = NULL_TREE;
9300 : : /* Let the middle end know about variables and functions -- but not
9301 : : static data members in uninstantiated class templates. */
9302 : 103121154 : if (VAR_OR_FUNCTION_DECL_P (decl))
9303 : : {
9304 : 94778353 : if (VAR_P (decl))
9305 : : {
9306 : 43782684 : layout_var_decl (decl);
9307 : 43782684 : if (!flag_weak)
9308 : : /* Check again now that we have an initializer. */
9309 : 57 : maybe_commonize_var (decl);
9310 : : /* A class-scope constexpr variable with an out-of-class declaration.
9311 : : C++17 makes them implicitly inline, but still force it out. */
9312 : 59039568 : if (DECL_INLINE_VAR_P (decl)
9313 : 14345407 : && !DECL_VAR_DECLARED_INLINE_P (decl)
9314 : 8342857 : && !DECL_TEMPLATE_INSTANTIATION (decl)
9315 : 27681113 : && !in_class_decl)
9316 : 100 : mark_needed (decl);
9317 : : }
9318 : :
9319 : 94778353 : if (var_definition_p
9320 : : /* With -fmerge-all-constants, gimplify_init_constructor
9321 : : might add TREE_STATIC to aggregate variables. */
9322 : 37596625 : && (TREE_STATIC (decl)
9323 : 22684586 : || (flag_merge_constants >= 2
9324 : 38 : && AGGREGATE_TYPE_P (type))))
9325 : : {
9326 : : /* If a TREE_READONLY variable needs initialization
9327 : : at runtime, it is no longer readonly and we need to
9328 : : avoid MEM_READONLY_P being set on RTL created for it. */
9329 : 14912042 : if (init)
9330 : : {
9331 : 13535 : if (TREE_READONLY (decl))
9332 : 519 : TREE_READONLY (decl) = 0;
9333 : : was_readonly = 0;
9334 : : }
9335 : 14898507 : else if (was_readonly)
9336 : 1575 : TREE_READONLY (decl) = 1;
9337 : :
9338 : : /* Likewise if it needs destruction. */
9339 : 14912042 : if (!decl_maybe_constant_destruction (decl, type))
9340 : 3110 : TREE_READONLY (decl) = 0;
9341 : : }
9342 : 79866311 : else if (VAR_P (decl)
9343 : 28870642 : && CP_DECL_THREAD_LOCAL_P (decl)
9344 : 19674 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9345 : 19674 : && (was_readonly || TREE_READONLY (decl))
9346 : 79866326 : && var_needs_tls_wrapper (decl))
9347 : : {
9348 : : /* TLS variables need dynamic initialization by the TLS wrapper
9349 : : function, we don't want to hoist accesses to it before the
9350 : : wrapper. */
9351 : 6 : was_readonly = 0;
9352 : 6 : TREE_READONLY (decl) = 0;
9353 : : }
9354 : :
9355 : 94778353 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
9356 : :
9357 : : /* Check for abstractness of the type. */
9358 : 94778353 : if (var_definition_p)
9359 : 37596625 : abstract_virtuals_error (decl, type);
9360 : :
9361 : 94778353 : if (decomp && !cp_finish_decomp (decl, decomp, true))
9362 : 15912 : decomp = NULL;
9363 : :
9364 : 94778353 : if (TREE_TYPE (decl) == error_mark_node)
9365 : : /* No initialization required. */
9366 : : ;
9367 : 94778282 : else if (TREE_CODE (decl) == FUNCTION_DECL)
9368 : : {
9369 : 50995669 : if (init)
9370 : : {
9371 : 576 : if (init == ridpointers[(int)RID_DEFAULT])
9372 : : {
9373 : : /* An out-of-class default definition is defined at
9374 : : the point where it is explicitly defaulted. */
9375 : 564 : if (DECL_DELETED_FN (decl))
9376 : 6 : maybe_explain_implicit_delete (decl);
9377 : 558 : else if (DECL_INITIAL (decl) == error_mark_node)
9378 : 534 : synthesize_method (decl);
9379 : : }
9380 : : else
9381 : 24 : error_at (cp_expr_loc_or_loc (init,
9382 : 12 : DECL_SOURCE_LOCATION (decl)),
9383 : : "function %q#D is initialized like a variable",
9384 : : decl);
9385 : : }
9386 : : /* else no initialization required. */
9387 : : }
9388 : 43782613 : else if (DECL_EXTERNAL (decl)
9389 : 43782613 : && ! (DECL_LANG_SPECIFIC (decl)
9390 : 5880521 : && DECL_NOT_REALLY_EXTERN (decl)))
9391 : : {
9392 : : /* check_initializer will have done any constant initialization. */
9393 : : }
9394 : : /* A variable definition. */
9395 : 39397808 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9396 : : {
9397 : : /* Initialize the local variable. */
9398 : 22684426 : if (!decomp)
9399 : 22666560 : initialize_local_var (decl, init, false);
9400 : : else
9401 : : {
9402 : 17866 : tree cleanup = NULL_TREE;
9403 : 17866 : if (DECL_SIZE (decl))
9404 : 17866 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9405 : : /* If cp_finish_decomp needs to emit any code, we need to emit that
9406 : : code after code emitted by initialize_local_var in a single
9407 : : CLEANUP_POINT_EXPR, so that temporaries are destructed only
9408 : : after the cp_finish_decomp emitted code.
9409 : : If there are any cleanups, either extend_ref_init_temps
9410 : : created ones or e.g. array destruction, push those first
9411 : : with the cleanups guarded on a bool temporary, initially
9412 : : set to false and set to true after initialize_local_var
9413 : : emitted code. */
9414 : 17866 : tree guard = NULL_TREE;
9415 : 17866 : if (cleanups || cleanup)
9416 : : {
9417 : 17866 : guard = get_internal_target_expr (boolean_false_node);
9418 : 17866 : add_stmt (guard);
9419 : 17866 : guard = TARGET_EXPR_SLOT (guard);
9420 : : }
9421 : 17866 : tree sl = push_stmt_list ();
9422 : 17866 : initialize_local_var (decl, init, true);
9423 : 17866 : if (guard)
9424 : : {
9425 : 17866 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9426 : : guard, boolean_true_node));
9427 : 17884 : for (tree &t : *cleanups)
9428 : 18 : t = build3 (COND_EXPR, void_type_node,
9429 : : guard, t, void_node);
9430 : 17866 : if (cleanup)
9431 : 25 : cleanup = build3 (COND_EXPR, void_type_node,
9432 : : guard, cleanup, void_node);
9433 : : }
9434 : 17866 : unsigned before = stmt_list_stack->length ();
9435 : 17866 : cp_finish_decomp (decl, decomp);
9436 : 17866 : decomp = NULL;
9437 : 17866 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
9438 : 17866 : sl = pop_stmt_list (sl);
9439 : 17866 : if (n_extra_cleanups)
9440 : : {
9441 : : /* If cp_finish_decomp needs any cleanups, such as for
9442 : : extend_ref_init_temps created vars, pop_stmt_list
9443 : : popped that all, so push those extra cleanups around
9444 : : the whole sequence with a guard variable. */
9445 : 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
9446 : 15 : guard = get_internal_target_expr (integer_zero_node);
9447 : 15 : add_stmt (guard);
9448 : 15 : guard = TARGET_EXPR_SLOT (guard);
9449 : 63 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
9450 : : {
9451 : 48 : tree_stmt_iterator tsi = tsi_last (sl);
9452 : 48 : gcc_assert (!tsi_end_p (tsi));
9453 : 48 : tree last = tsi_stmt (tsi);
9454 : 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
9455 : : && !CLEANUP_EH_ONLY (last));
9456 : 48 : tree cst = build_int_cst (integer_type_node, i + 1);
9457 : 96 : tree cl = build3 (COND_EXPR, void_type_node,
9458 : : build2 (GE_EXPR, boolean_type_node,
9459 : : guard, cst),
9460 : 48 : CLEANUP_EXPR (last), void_node);
9461 : 48 : extra_cleanups.safe_push (cl);
9462 : 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
9463 : : integer_type_node,
9464 : : guard, cst),
9465 : : TSI_SAME_STMT);
9466 : 48 : tree sl2 = CLEANUP_BODY (last);
9467 : 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
9468 : 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
9469 : 48 : tsi_delink (&tsi);
9470 : : }
9471 : : }
9472 : 17866 : decomp_init = maybe_cleanup_point_expr_void (sl);
9473 : 17866 : if (cleanup)
9474 : 25 : finish_decl_cleanup (decl, cleanup);
9475 : : }
9476 : : }
9477 : :
9478 : : /* If a variable is defined, and then a subsequent
9479 : : definition with external linkage is encountered, we will
9480 : : get here twice for the same variable. We want to avoid
9481 : : calling expand_static_init more than once. For variables
9482 : : that are not static data members, we can call
9483 : : expand_static_init only when we actually process the
9484 : : initializer. It is not legal to redeclare a static data
9485 : : member, so this issue does not arise in that case. */
9486 : 16713382 : else if (var_definition_p && TREE_STATIC (decl))
9487 : : {
9488 : 14911996 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
9489 : : {
9490 : 54 : tree sl = push_stmt_list ();
9491 : 54 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
9492 : 54 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
9493 : 54 : expand_static_init (decl, init);
9494 : 54 : current_stmt_tree ()->stmts_are_full_exprs_p
9495 : 54 : = saved_stmts_are_full_exprs_p;
9496 : 54 : cp_finish_decomp (decl, decomp);
9497 : 54 : decomp = NULL;
9498 : 54 : sl = pop_stmt_list (sl);
9499 : 54 : sl = maybe_cleanup_point_expr_void (sl);
9500 : 54 : add_stmt (sl);
9501 : : }
9502 : : else
9503 : 14911942 : expand_static_init (decl, init);
9504 : : }
9505 : : }
9506 : :
9507 : : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9508 : : reference, insert it in the statement-tree now. */
9509 : 103121154 : if (cleanups)
9510 : : {
9511 : 38579505 : for (tree t : *cleanups)
9512 : : {
9513 : 5877 : push_cleanup (NULL_TREE, t, false);
9514 : : /* As in initialize_local_var. */
9515 : 5877 : wrap_temporary_cleanups (init, t);
9516 : : }
9517 : 38573628 : release_tree_vector (cleanups);
9518 : : }
9519 : :
9520 : 103121232 : for (tree t : &extra_cleanups)
9521 : 48 : push_cleanup (NULL_TREE, t, false);
9522 : :
9523 : 103121154 : if (decomp_init)
9524 : 17866 : add_stmt (decomp_init);
9525 : :
9526 : 103121154 : if (decomp
9527 : 105 : && var_definition_p
9528 : 105 : && TREE_STATIC (decl)
9529 : 103121259 : && !DECL_FUNCTION_SCOPE_P (decl))
9530 : : {
9531 : 105 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
9532 : 105 : ? tls_aggregates : static_aggregates);
9533 : 105 : tree aggregates2 = aggregates3;
9534 : 105 : if (aggregates2 != aggregates1)
9535 : : {
9536 : 66 : cp_finish_decomp (decl, decomp);
9537 : 66 : decomp = NULL;
9538 : 66 : if (aggregates3 != aggregates2)
9539 : : {
9540 : : /* If there are dynamic initializers for the structured
9541 : : binding base or associated extended ref temps and also
9542 : : dynamic initializers for the structured binding non-base
9543 : : vars, mark them. */
9544 : 315 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
9545 : 252 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
9546 : 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
9547 : 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
9548 : : }
9549 : : }
9550 : : }
9551 : :
9552 : 103121154 : if (was_readonly)
9553 : 225446 : TREE_READONLY (decl) = 1;
9554 : :
9555 : 103121154 : if (flag_openmp
9556 : 334175 : && VAR_P (decl)
9557 : 103241288 : && lookup_attribute ("omp declare target implicit",
9558 : 120134 : DECL_ATTRIBUTES (decl)))
9559 : : {
9560 : 52 : DECL_ATTRIBUTES (decl)
9561 : 52 : = remove_attribute ("omp declare target implicit",
9562 : 52 : DECL_ATTRIBUTES (decl));
9563 : 52 : complete_type (TREE_TYPE (decl));
9564 : 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
9565 : : {
9566 : 30 : auto_diagnostic_group d;
9567 : 30 : error ("%q+D in declare target directive does not have mappable"
9568 : : " type", decl);
9569 : 30 : if (TREE_TYPE (decl) != error_mark_node
9570 : 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9571 : 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
9572 : 30 : }
9573 : 22 : else if (!lookup_attribute ("omp declare target",
9574 : 22 : DECL_ATTRIBUTES (decl))
9575 : 44 : && !lookup_attribute ("omp declare target link",
9576 : 22 : DECL_ATTRIBUTES (decl)))
9577 : : {
9578 : 22 : DECL_ATTRIBUTES (decl)
9579 : 22 : = tree_cons (get_identifier ("omp declare target"),
9580 : 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
9581 : 22 : symtab_node *node = symtab_node::get (decl);
9582 : 22 : if (node != NULL)
9583 : : {
9584 : 19 : node->offloadable = 1;
9585 : 19 : if (ENABLE_OFFLOADING)
9586 : : {
9587 : : g->have_offload = true;
9588 : : if (is_a <varpool_node *> (node))
9589 : : vec_safe_push (offload_vars, decl);
9590 : : }
9591 : : }
9592 : : }
9593 : : }
9594 : :
9595 : : /* This is the last point we can lower alignment so give the target the
9596 : : chance to do so. */
9597 : 103121154 : if (VAR_P (decl)
9598 : 43782684 : && !is_global_var (decl)
9599 : 125805740 : && !DECL_HARD_REGISTER (decl))
9600 : 22684536 : targetm.lower_local_decl_alignment (decl);
9601 : :
9602 : 103121154 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
9603 : 270694214 : }
9604 : :
9605 : : /* For class TYPE return itself or some its bases that contain
9606 : : any direct non-static data members. Return error_mark_node if an
9607 : : error has been diagnosed. */
9608 : :
9609 : : static tree
9610 : 20418 : find_decomp_class_base (location_t loc, tree type, tree ret)
9611 : : {
9612 : 40836 : if (LAMBDA_TYPE_P (type))
9613 : : {
9614 : 9 : auto_diagnostic_group d;
9615 : 9 : error_at (loc, "cannot decompose lambda closure type %qT", type);
9616 : 9 : inform (location_of (type), "lambda declared here");
9617 : 9 : return error_mark_node;
9618 : 9 : }
9619 : :
9620 : 20409 : bool member_seen = false;
9621 : 287451 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9622 : 493013 : if (TREE_CODE (field) != FIELD_DECL
9623 : 41219 : || DECL_ARTIFICIAL (field)
9624 : 308200 : || DECL_UNNAMED_BIT_FIELD (field))
9625 : 225950 : continue;
9626 : 41113 : else if (ret)
9627 : : return type;
9628 : 41096 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9629 : : {
9630 : 2 : auto_diagnostic_group d;
9631 : 2 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9632 : 1 : error_at (loc, "cannot decompose class type %qT because it has an "
9633 : : "anonymous struct member", type);
9634 : : else
9635 : 1 : error_at (loc, "cannot decompose class type %qT because it has an "
9636 : : "anonymous union member", type);
9637 : 2 : inform (DECL_SOURCE_LOCATION (field), "declared here");
9638 : 2 : return error_mark_node;
9639 : 2 : }
9640 : 41094 : else if (!accessible_p (type, field, true))
9641 : : {
9642 : 2 : auto_diagnostic_group d;
9643 : 2 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
9644 : : field, type);
9645 : 2 : inform (DECL_SOURCE_LOCATION (field),
9646 : 2 : TREE_PRIVATE (field)
9647 : : ? G_("declared private here")
9648 : : : G_("declared protected here"));
9649 : 2 : return error_mark_node;
9650 : 2 : }
9651 : : else
9652 : : member_seen = true;
9653 : :
9654 : 20388 : tree base_binfo, binfo;
9655 : 20388 : tree orig_ret = ret;
9656 : 20388 : int i;
9657 : 20388 : if (member_seen)
9658 : 20339 : ret = type;
9659 : 20443 : for (binfo = TYPE_BINFO (type), i = 0;
9660 : 20443 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9661 : : {
9662 : 66 : auto_diagnostic_group d;
9663 : 66 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
9664 : 66 : if (t == error_mark_node)
9665 : : {
9666 : 3 : inform (location_of (type), "in base class of %qT", type);
9667 : 3 : return error_mark_node;
9668 : : }
9669 : 63 : if (t != NULL_TREE && t != ret)
9670 : : {
9671 : 36 : if (ret == type)
9672 : : {
9673 : 4 : error_at (loc, "cannot decompose class type %qT: both it and "
9674 : : "its base class %qT have non-static data members",
9675 : : type, t);
9676 : 4 : return error_mark_node;
9677 : : }
9678 : 32 : else if (orig_ret != NULL_TREE)
9679 : : return t;
9680 : 32 : else if (ret != NULL_TREE)
9681 : : {
9682 : 4 : error_at (loc, "cannot decompose class type %qT: its base "
9683 : : "classes %qT and %qT have non-static data "
9684 : : "members", type, ret, t);
9685 : 4 : return error_mark_node;
9686 : : }
9687 : : else
9688 : : ret = t;
9689 : : }
9690 : 66 : }
9691 : : return ret;
9692 : : }
9693 : :
9694 : : /* Return std::tuple_size<TYPE>::value. */
9695 : :
9696 : : static tree
9697 : 58995 : get_tuple_size (tree type)
9698 : : {
9699 : 58995 : tree args = make_tree_vec (1);
9700 : 58995 : TREE_VEC_ELT (args, 0) = type;
9701 : 58995 : tree inst = lookup_template_class (tuple_size_identifier, args,
9702 : : /*in_decl*/NULL_TREE,
9703 : : /*context*/std_node,
9704 : : tf_none);
9705 : 58995 : inst = complete_type (inst);
9706 : 58995 : if (inst == error_mark_node
9707 : 58393 : || !COMPLETE_TYPE_P (inst)
9708 : 97623 : || !CLASS_TYPE_P (type))
9709 : : return NULL_TREE;
9710 : 38628 : tree val = lookup_qualified_name (inst, value_identifier,
9711 : : LOOK_want::NORMAL, /*complain*/false);
9712 : 38628 : if (val == error_mark_node)
9713 : : return NULL_TREE;
9714 : 38622 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
9715 : 38622 : val = maybe_constant_value (val);
9716 : 38622 : if (TREE_CODE (val) == INTEGER_CST)
9717 : : return val;
9718 : : else
9719 : 0 : return error_mark_node;
9720 : : }
9721 : :
9722 : : /* Return std::tuple_element<I,TYPE>::type. */
9723 : :
9724 : : static tree
9725 : 41478 : get_tuple_element_type (tree type, unsigned i)
9726 : : {
9727 : 41478 : tree args = make_tree_vec (2);
9728 : 41478 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
9729 : 41478 : TREE_VEC_ELT (args, 1) = type;
9730 : 41478 : tree inst = lookup_template_class (tuple_element_identifier, args,
9731 : : /*in_decl*/NULL_TREE,
9732 : : /*context*/std_node,
9733 : : tf_warning_or_error);
9734 : 41478 : return make_typename_type (inst, type_identifier,
9735 : 41478 : none_type, tf_warning_or_error);
9736 : : }
9737 : :
9738 : : /* Return e.get<i>() or get<i>(e). */
9739 : :
9740 : : static tree
9741 : 41487 : get_tuple_decomp_init (tree decl, unsigned i)
9742 : : {
9743 : 41487 : tree targs = make_tree_vec (1);
9744 : 41487 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
9745 : :
9746 : 41487 : tree etype = TREE_TYPE (decl);
9747 : 41487 : tree e = convert_from_reference (decl);
9748 : :
9749 : : /* [The id-expression] e is an lvalue if the type of the entity e is an
9750 : : lvalue reference and an xvalue otherwise. */
9751 : 41487 : if (!TYPE_REF_P (etype)
9752 : 41487 : || TYPE_REF_IS_RVALUE (etype))
9753 : 40344 : e = move (e);
9754 : :
9755 : 41487 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
9756 : : LOOK_want::NORMAL, /*complain*/false);
9757 : 41487 : bool use_member_get = false;
9758 : :
9759 : : /* To use a member get, member lookup must find at least one
9760 : : declaration that is a function template
9761 : : whose first template parameter is a non-type parameter. */
9762 : 123243 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
9763 : : {
9764 : 41487 : tree fn = *iter;
9765 : 41487 : if (TREE_CODE (fn) == TEMPLATE_DECL)
9766 : : {
9767 : 1221 : tree tparms = DECL_TEMPLATE_PARMS (fn);
9768 : 1221 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
9769 : 1221 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
9770 : : {
9771 : : use_member_get = true;
9772 : : break;
9773 : : }
9774 : : }
9775 : : }
9776 : :
9777 : 41487 : if (use_member_get)
9778 : : {
9779 : 1218 : fns = lookup_template_function (fns, targs);
9780 : 1218 : return build_new_method_call (e, fns, /*args*/NULL,
9781 : : /*path*/NULL_TREE, LOOKUP_NORMAL,
9782 : 1218 : /*fn_p*/NULL, tf_warning_or_error);
9783 : : }
9784 : : else
9785 : : {
9786 : 40269 : releasing_vec args (make_tree_vector_single (e));
9787 : 40269 : fns = lookup_template_function (get__identifier, targs);
9788 : 40269 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
9789 : 40269 : return finish_call_expr (fns, &args, /*novirt*/false,
9790 : : /*koenig*/true, tf_warning_or_error);
9791 : 40269 : }
9792 : : }
9793 : :
9794 : : /* It's impossible to recover the decltype of a tuple decomposition variable
9795 : : based on the actual type of the variable, so store it in a hash table. */
9796 : :
9797 : : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
9798 : :
9799 : : tree
9800 : 54 : lookup_decomp_type (tree v)
9801 : : {
9802 : 54 : if (decomp_type_table)
9803 : 51 : if (tree *slot = decomp_type_table->get (v))
9804 : 51 : return *slot;
9805 : : return NULL_TREE;
9806 : : }
9807 : :
9808 : : /* Mangle a decomposition declaration if needed. Arguments like
9809 : : in cp_finish_decomp. */
9810 : :
9811 : : static void
9812 : 33937 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
9813 : : {
9814 : 33937 : if (!processing_template_decl
9815 : 33937 : && !error_operand_p (decl)
9816 : 67874 : && TREE_STATIC (decl))
9817 : : {
9818 : 382 : auto_vec<tree, 16> v;
9819 : 382 : v.safe_grow (decomp->count, true);
9820 : 382 : tree d = decomp->decl;
9821 : 1371 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
9822 : 989 : v[decomp->count - i - 1] = d;
9823 : 382 : if (DECL_FUNCTION_SCOPE_P (decl))
9824 : : {
9825 : : size_t sz = 3;
9826 : 453 : for (unsigned int i = 0; i < decomp->count; ++i)
9827 : 330 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
9828 : 123 : char *name = XALLOCAVEC (char, sz);
9829 : 123 : name[0] = 'D';
9830 : 123 : name[1] = 'C';
9831 : 123 : char *p = name + 2;
9832 : 453 : for (unsigned int i = 0; i < decomp->count; ++i)
9833 : : {
9834 : 330 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
9835 : 330 : *p++ = ' ';
9836 : 330 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
9837 : 330 : p += len;
9838 : : }
9839 : 123 : *p = '\0';
9840 : 123 : determine_local_discriminator (decl, get_identifier (name));
9841 : : }
9842 : 382 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
9843 : 382 : maybe_apply_pragma_weak (decl);
9844 : 382 : }
9845 : 33937 : }
9846 : :
9847 : : /* Finish a decomposition declaration. DECL is the underlying declaration
9848 : : "e", FIRST is the head of a chain of decls for the individual identifiers
9849 : : chained through DECL_CHAIN in reverse order and COUNT is the number of
9850 : : those decls. If TEST_P is true, return true if any code would need to be
9851 : : actually emitted but don't emit it. Return false otherwise. */
9852 : :
9853 : : bool
9854 : 124471 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
9855 : : {
9856 : 124471 : tree first = decomp->decl;
9857 : 124471 : unsigned count = decomp->count;
9858 : 124471 : if (error_operand_p (decl))
9859 : : {
9860 : 54 : error_out:
9861 : 436 : while (count--)
9862 : : {
9863 : 274 : TREE_TYPE (first) = error_mark_node;
9864 : 274 : if (DECL_HAS_VALUE_EXPR_P (first))
9865 : : {
9866 : 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
9867 : 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
9868 : : }
9869 : 274 : first = DECL_CHAIN (first);
9870 : : }
9871 : 162 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
9872 : 51 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
9873 : 162 : return false;
9874 : : }
9875 : :
9876 : 124417 : location_t loc = DECL_SOURCE_LOCATION (decl);
9877 : 124417 : if (type_dependent_expression_p (decl)
9878 : : /* This happens for range for when not in templates.
9879 : : Still add the DECL_VALUE_EXPRs for later processing. */
9880 : 124417 : || (!processing_template_decl
9881 : 52114 : && type_uses_auto (TREE_TYPE (decl))))
9882 : : {
9883 : 195341 : for (unsigned int i = 0; i < count; i++)
9884 : : {
9885 : 130312 : if (!DECL_HAS_VALUE_EXPR_P (first))
9886 : : {
9887 : 130312 : tree v = build_nt (ARRAY_REF, decl,
9888 : 130312 : size_int (count - i - 1),
9889 : : NULL_TREE, NULL_TREE);
9890 : 130312 : SET_DECL_VALUE_EXPR (first, v);
9891 : 130312 : DECL_HAS_VALUE_EXPR_P (first) = 1;
9892 : : }
9893 : 130312 : if (processing_template_decl)
9894 : 129832 : fit_decomposition_lang_decl (first, decl);
9895 : 130312 : first = DECL_CHAIN (first);
9896 : : }
9897 : : return false;
9898 : : }
9899 : :
9900 : 59388 : auto_vec<tree, 16> v;
9901 : 59388 : v.safe_grow (count, true);
9902 : 59388 : tree d = first;
9903 : 179324 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
9904 : : {
9905 : 119936 : v[count - i - 1] = d;
9906 : 119936 : fit_decomposition_lang_decl (d, decl);
9907 : : }
9908 : :
9909 : 59388 : tree type = TREE_TYPE (decl);
9910 : 59388 : tree dexp = decl;
9911 : :
9912 : 59388 : if (TYPE_REF_P (type))
9913 : : {
9914 : 1196 : dexp = convert_from_reference (dexp);
9915 : 1196 : type = complete_type (TREE_TYPE (type));
9916 : 1196 : if (type == error_mark_node)
9917 : 108 : goto error_out;
9918 : 1196 : if (!COMPLETE_TYPE_P (type))
9919 : : {
9920 : 3 : error_at (loc, "structured binding refers to incomplete type %qT",
9921 : : type);
9922 : 3 : goto error_out;
9923 : : }
9924 : : }
9925 : :
9926 : 59385 : tree eltype = NULL_TREE;
9927 : 59385 : unsigned HOST_WIDE_INT eltscnt = 0;
9928 : 59385 : if (TREE_CODE (type) == ARRAY_TYPE)
9929 : : {
9930 : 366 : tree nelts;
9931 : 366 : nelts = array_type_nelts_top (type);
9932 : 366 : if (nelts == error_mark_node)
9933 : 0 : goto error_out;
9934 : 366 : if (DECL_DECOMP_BASE (decl))
9935 : : {
9936 : 9 : error_at (loc, "array initializer for structured binding "
9937 : : "declaration in condition");
9938 : 9 : goto error_out;
9939 : : }
9940 : 357 : if (!tree_fits_uhwi_p (nelts))
9941 : : {
9942 : 0 : error_at (loc, "cannot decompose variable length array %qT", type);
9943 : 0 : goto error_out;
9944 : : }
9945 : 357 : eltscnt = tree_to_uhwi (nelts);
9946 : 357 : if (count != eltscnt)
9947 : : {
9948 : 8 : cnt_mismatch:
9949 : 23 : auto_diagnostic_group d;
9950 : 23 : if (count > eltscnt)
9951 : 12 : error_n (loc, count,
9952 : : "%u name provided for structured binding",
9953 : : "%u names provided for structured binding", count);
9954 : : else
9955 : 11 : error_n (loc, count,
9956 : : "only %u name provided for structured binding",
9957 : : "only %u names provided for structured binding", count);
9958 : 23 : inform_n (loc, eltscnt,
9959 : : "while %qT decomposes into %wu element",
9960 : : "while %qT decomposes into %wu elements",
9961 : : type, eltscnt);
9962 : 23 : goto error_out;
9963 : : }
9964 : 349 : eltype = TREE_TYPE (type);
9965 : 1188 : for (unsigned int i = 0; i < count; i++)
9966 : : {
9967 : 839 : TREE_TYPE (v[i]) = eltype;
9968 : 839 : layout_decl (v[i], 0);
9969 : 839 : if (processing_template_decl)
9970 : 60 : continue;
9971 : 779 : tree t = unshare_expr (dexp);
9972 : 779 : t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9973 : 779 : SET_DECL_VALUE_EXPR (v[i], t);
9974 : 779 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9975 : : }
9976 : : }
9977 : : /* 2 GNU extensions. */
9978 : 59019 : else if (TREE_CODE (type) == COMPLEX_TYPE)
9979 : : {
9980 : 15 : eltscnt = 2;
9981 : 15 : if (count != eltscnt)
9982 : 0 : goto cnt_mismatch;
9983 : 15 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9984 : 45 : for (unsigned int i = 0; i < count; i++)
9985 : : {
9986 : 30 : TREE_TYPE (v[i]) = eltype;
9987 : 30 : layout_decl (v[i], 0);
9988 : 30 : if (processing_template_decl)
9989 : 0 : continue;
9990 : 30 : tree t = unshare_expr (dexp);
9991 : 45 : t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
9992 : 30 : SET_DECL_VALUE_EXPR (v[i], t);
9993 : 30 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9994 : : }
9995 : : }
9996 : 59004 : else if (TREE_CODE (type) == VECTOR_TYPE)
9997 : : {
9998 : 9 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9999 : : {
10000 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10001 : : goto error_out;
10002 : : }
10003 : 9 : if (count != eltscnt)
10004 : 0 : goto cnt_mismatch;
10005 : 9 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10006 : 45 : for (unsigned int i = 0; i < count; i++)
10007 : : {
10008 : 36 : TREE_TYPE (v[i]) = eltype;
10009 : 36 : layout_decl (v[i], 0);
10010 : 36 : if (processing_template_decl)
10011 : 0 : continue;
10012 : 36 : tree t = unshare_expr (dexp);
10013 : 36 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10014 : 36 : &t, size_int (i));
10015 : 36 : t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
10016 : 36 : SET_DECL_VALUE_EXPR (v[i], t);
10017 : 36 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10018 : : }
10019 : : }
10020 : 58995 : else if (tree tsize = get_tuple_size (type))
10021 : : {
10022 : 38622 : if (tsize == error_mark_node)
10023 : : {
10024 : 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10025 : : "constant expression", type);
10026 : 0 : goto error_out;
10027 : : }
10028 : 38622 : if (!tree_fits_uhwi_p (tsize))
10029 : : {
10030 : 12 : auto_diagnostic_group d;
10031 : 12 : error_n (loc, count,
10032 : : "%u name provided for structured binding",
10033 : : "%u names provided for structured binding", count);
10034 : 12 : inform (loc, "while %qT decomposes into %E elements",
10035 : : type, tsize);
10036 : 12 : goto error_out;
10037 : 12 : }
10038 : 38610 : eltscnt = tree_to_uhwi (tsize);
10039 : 38610 : if (count != eltscnt)
10040 : 3 : goto cnt_mismatch;
10041 : 38607 : if (test_p)
10042 : 59280 : return true;
10043 : 20582 : if (!processing_template_decl && DECL_DECOMP_BASE (decl))
10044 : : {
10045 : : /* For structured bindings used in conditions we need to evaluate
10046 : : the conversion of decl (aka e in the standard) to bool or
10047 : : integral/enumeral type (the latter for switch conditions)
10048 : : before the get methods. */
10049 : 78 : tree cond = convert_from_reference (decl);
10050 : 78 : if (integer_onep (DECL_DECOMP_BASE (decl)))
10051 : : /* switch condition. */
10052 : 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10053 : : cond, true);
10054 : : else
10055 : : /* if/while/for condition. */
10056 : 60 : cond = contextual_conv_bool (cond, tf_warning_or_error);
10057 : 78 : if (cond && !error_operand_p (cond))
10058 : : {
10059 : : /* Wrap that value into a TARGET_EXPR, emit it right
10060 : : away and save for later uses in the cp_parse_condition
10061 : : or its instantiation. */
10062 : 78 : cond = get_internal_target_expr (cond);
10063 : 78 : add_stmt (cond);
10064 : 78 : DECL_DECOMP_BASE (decl) = cond;
10065 : : }
10066 : : }
10067 : 20582 : int save_read = DECL_READ_P (decl);
10068 : 62048 : for (unsigned i = 0; i < count; ++i)
10069 : : {
10070 : 41487 : location_t sloc = input_location;
10071 : 41487 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10072 : :
10073 : 41487 : input_location = dloc;
10074 : 41487 : tree init = get_tuple_decomp_init (decl, i);
10075 : 41487 : tree eltype = (init == error_mark_node ? error_mark_node
10076 : 41478 : : get_tuple_element_type (type, i));
10077 : 41487 : input_location = sloc;
10078 : :
10079 : 41487 : if (VOID_TYPE_P (eltype))
10080 : : {
10081 : 3 : error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
10082 : : i, type);
10083 : 3 : eltype = error_mark_node;
10084 : : }
10085 : 41487 : if (init == error_mark_node || eltype == error_mark_node)
10086 : : {
10087 : 21 : inform (dloc, "in initialization of structured binding "
10088 : 21 : "variable %qD", v[i]);
10089 : 21 : goto error_out;
10090 : : }
10091 : : /* Save the decltype away before reference collapse. */
10092 : 41466 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
10093 : 41466 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10094 : 41466 : TREE_TYPE (v[i]) = eltype;
10095 : 41466 : layout_decl (v[i], 0);
10096 : 41466 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
10097 : : {
10098 : : /* In this case the names are variables, not just proxies. */
10099 : 204 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10100 : 204 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10101 : : }
10102 : 41466 : if (!processing_template_decl)
10103 : : {
10104 : 36278 : copy_linkage (v[i], decl);
10105 : 36278 : cp_finish_decl (v[i], init, /*constexpr*/false,
10106 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10107 : : }
10108 : : }
10109 : : /* Ignore reads from the underlying decl performed during initialization
10110 : : of the individual variables. If those will be read, we'll mark
10111 : : the underlying decl as read at that point. */
10112 : 20561 : DECL_READ_P (decl) = save_read;
10113 : : }
10114 : 20373 : else if (TREE_CODE (type) == UNION_TYPE)
10115 : : {
10116 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10117 : 3 : goto error_out;
10118 : : }
10119 : 20370 : else if (!CLASS_TYPE_P (type))
10120 : : {
10121 : 16 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10122 : 16 : goto error_out;
10123 : : }
10124 : 20354 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10125 : 0 : goto error_out;
10126 : 20354 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10127 : 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10128 : : type);
10129 : : else
10130 : : {
10131 : 20352 : tree btype = find_decomp_class_base (loc, type, NULL_TREE);
10132 : 20352 : if (btype == error_mark_node)
10133 : 21 : goto error_out;
10134 : 20331 : else if (btype == NULL_TREE)
10135 : : {
10136 : 0 : error_at (loc, "cannot decompose class type %qT without non-static "
10137 : : "data members", type);
10138 : 0 : goto error_out;
10139 : : }
10140 : 286330 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10141 : 490917 : if (TREE_CODE (field) != FIELD_DECL
10142 : 41097 : || DECL_ARTIFICIAL (field)
10143 : 307092 : || DECL_UNNAMED_BIT_FIELD (field))
10144 : 224918 : continue;
10145 : : else
10146 : 41081 : eltscnt++;
10147 : 20331 : if (count != eltscnt)
10148 : 12 : goto cnt_mismatch;
10149 : 20319 : tree t = dexp;
10150 : 20319 : if (type != btype)
10151 : : {
10152 : 18 : t = convert_to_base (t, btype, /*check_access*/true,
10153 : : /*nonnull*/false, tf_warning_or_error);
10154 : 18 : type = btype;
10155 : : }
10156 : 20319 : unsigned int i = 0;
10157 : 286137 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10158 : 490580 : if (TREE_CODE (field) != FIELD_DECL
10159 : 41072 : || DECL_ARTIFICIAL (field)
10160 : 306886 : || DECL_UNNAMED_BIT_FIELD (field))
10161 : 224762 : continue;
10162 : : else
10163 : : {
10164 : 41056 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
10165 : : NULL_TREE);
10166 : 41056 : if (REFERENCE_REF_P (tt))
10167 : 30 : tt = TREE_OPERAND (tt, 0);
10168 : 41056 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
10169 : 41056 : layout_decl (v[i], 0);
10170 : 41056 : if (!processing_template_decl)
10171 : : {
10172 : 31371 : SET_DECL_VALUE_EXPR (v[i], tt);
10173 : 31371 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10174 : : }
10175 : 41056 : i++;
10176 : : }
10177 : : }
10178 : 41255 : if (processing_template_decl)
10179 : : {
10180 : 22352 : for (unsigned int i = 0; i < count; i++)
10181 : 14935 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
10182 : : {
10183 : 14800 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
10184 : : NULL_TREE, NULL_TREE);
10185 : 14800 : SET_DECL_VALUE_EXPR (v[i], a);
10186 : 14800 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10187 : : }
10188 : : }
10189 : : return false;
10190 : : }
10191 : :
10192 : : /* Returns a declaration for a VAR_DECL as if:
10193 : :
10194 : : extern "C" TYPE NAME;
10195 : :
10196 : : had been seen. Used to create compiler-generated global
10197 : : variables. */
10198 : :
10199 : : static tree
10200 : 2106 : declare_global_var (tree name, tree type)
10201 : : {
10202 : 2106 : auto cookie = push_abi_namespace (global_namespace);
10203 : 2106 : tree decl = build_decl (input_location, VAR_DECL, name, type);
10204 : 2106 : TREE_PUBLIC (decl) = 1;
10205 : 2106 : DECL_EXTERNAL (decl) = 1;
10206 : 2106 : DECL_ARTIFICIAL (decl) = 1;
10207 : 2106 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10208 : : /* If the user has explicitly declared this variable (perhaps
10209 : : because the code we are compiling is part of a low-level runtime
10210 : : library), then it is possible that our declaration will be merged
10211 : : with theirs by pushdecl. */
10212 : 2106 : decl = pushdecl (decl);
10213 : 2106 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
10214 : 2106 : pop_abi_namespace (cookie, global_namespace);
10215 : :
10216 : 2106 : return decl;
10217 : : }
10218 : :
10219 : : /* Returns the type for the argument to "atexit" corresponding to the function
10220 : : to be called when the program exits. */
10221 : :
10222 : : static tree
10223 : 24 : get_atexit_fn_ptr_type ()
10224 : : {
10225 : 24 : if (!atexit_fn_ptr_type_node)
10226 : : {
10227 : 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
10228 : 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
10229 : : }
10230 : :
10231 : 24 : return atexit_fn_ptr_type_node;
10232 : : }
10233 : :
10234 : : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
10235 : : or "__cxa_throw" corresponding to the destructor to be called when the
10236 : : program exits. */
10237 : :
10238 : : tree
10239 : 17849 : get_cxa_atexit_fn_ptr_type ()
10240 : : {
10241 : 17849 : if (!cleanup_type)
10242 : : {
10243 : 13555 : tree fntype = build_function_type_list (void_type_node,
10244 : : ptr_type_node, NULL_TREE);
10245 : 13555 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
10246 : 13555 : cleanup_type = build_pointer_type (fntype);
10247 : : }
10248 : :
10249 : 17849 : return cleanup_type;
10250 : : }
10251 : :
10252 : : /* Returns a pointer to the `atexit' function. Note that if
10253 : : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
10254 : : `__cxa_atexit' function specified in the IA64 C++ ABI. */
10255 : :
10256 : : static tree
10257 : 3108 : get_atexit_node (void)
10258 : : {
10259 : 3108 : tree atexit_fndecl;
10260 : 3108 : tree fn_type;
10261 : 3108 : tree fn_ptr_type;
10262 : 3108 : const char *name;
10263 : 3108 : bool use_aeabi_atexit;
10264 : 3108 : tree ctx = global_namespace;
10265 : :
10266 : 3108 : if (atexit_node)
10267 : : return atexit_node;
10268 : :
10269 : 2073 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
10270 : : {
10271 : : /* The declaration for `__cxa_atexit' is:
10272 : :
10273 : : int __cxa_atexit (void (*)(void *), void *, void *)
10274 : :
10275 : : We build up the argument types and then the function type
10276 : : itself. */
10277 : 2061 : tree argtype0, argtype1, argtype2;
10278 : :
10279 : 2061 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
10280 : : /* First, build the pointer-to-function type for the first
10281 : : argument. */
10282 : 2061 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
10283 : : /* Then, build the rest of the argument types. */
10284 : 2061 : argtype2 = ptr_type_node;
10285 : 2061 : if (use_aeabi_atexit)
10286 : : {
10287 : : argtype1 = fn_ptr_type;
10288 : : argtype0 = ptr_type_node;
10289 : : }
10290 : : else
10291 : : {
10292 : 2061 : argtype1 = ptr_type_node;
10293 : 2061 : argtype0 = fn_ptr_type;
10294 : : }
10295 : : /* And the final __cxa_atexit type. */
10296 : 2061 : fn_type = build_function_type_list (integer_type_node,
10297 : : argtype0, argtype1, argtype2,
10298 : : NULL_TREE);
10299 : : /* ... which needs noexcept. */
10300 : 2061 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10301 : 2061 : if (use_aeabi_atexit)
10302 : : {
10303 : 0 : name = "__aeabi_atexit";
10304 : 0 : push_to_top_level ();
10305 : 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
10306 : 0 : ctx = current_namespace;
10307 : 0 : while (n--)
10308 : 0 : pop_namespace ();
10309 : 0 : pop_from_top_level ();
10310 : : }
10311 : : else
10312 : : {
10313 : 2061 : name = "__cxa_atexit";
10314 : 2061 : ctx = abi_node;
10315 : : }
10316 : : }
10317 : : else
10318 : : {
10319 : : /* The declaration for `atexit' is:
10320 : :
10321 : : int atexit (void (*)());
10322 : :
10323 : : We build up the argument types and then the function type
10324 : : itself. */
10325 : 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
10326 : : /* Build the final atexit type. */
10327 : 12 : fn_type = build_function_type_list (integer_type_node,
10328 : : fn_ptr_type, NULL_TREE);
10329 : : /* ... which needs noexcept. */
10330 : 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10331 : 12 : name = "atexit";
10332 : : }
10333 : :
10334 : : /* Now, build the function declaration. */
10335 : 2073 : push_lang_context (lang_name_c);
10336 : 2073 : auto cookie = push_abi_namespace (ctx);
10337 : 2073 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
10338 : 2073 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10339 : : /* Install as hidden builtin so we're (a) more relaxed about
10340 : : exception spec matching and (b) will not give a confusing location
10341 : : in diagnostic and (c) won't magically appear in user-visible name
10342 : : lookups. */
10343 : 2073 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10344 : 2073 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10345 : 2073 : pop_abi_namespace (cookie, ctx);
10346 : 2073 : mark_used (atexit_fndecl);
10347 : 2073 : pop_lang_context ();
10348 : 2073 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10349 : :
10350 : 2073 : return atexit_node;
10351 : : }
10352 : :
10353 : : /* Like get_atexit_node, but for thread-local cleanups. */
10354 : :
10355 : : static tree
10356 : 85 : get_thread_atexit_node (void)
10357 : : {
10358 : 85 : if (thread_atexit_node)
10359 : : return thread_atexit_node;
10360 : :
10361 : : /* The declaration for `__cxa_thread_atexit' is:
10362 : :
10363 : : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
10364 : 55 : tree fn_type = build_function_type_list (integer_type_node,
10365 : : get_cxa_atexit_fn_ptr_type (),
10366 : : ptr_type_node, ptr_type_node,
10367 : : NULL_TREE);
10368 : :
10369 : : /* Now, build the function declaration, as with __cxa_atexit. */
10370 : 55 : unsigned flags = push_abi_namespace ();
10371 : 55 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
10372 : : ECF_LEAF | ECF_NOTHROW);
10373 : 55 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10374 : 55 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10375 : 55 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10376 : 55 : pop_abi_namespace (flags);
10377 : 55 : mark_used (atexit_fndecl);
10378 : 55 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10379 : :
10380 : 55 : return thread_atexit_node;
10381 : : }
10382 : :
10383 : : /* Returns the __dso_handle VAR_DECL. */
10384 : :
10385 : : static tree
10386 : 3181 : get_dso_handle_node (void)
10387 : : {
10388 : 3181 : if (dso_handle_node)
10389 : : return dso_handle_node;
10390 : :
10391 : : /* Declare the variable. */
10392 : 2106 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
10393 : : ptr_type_node);
10394 : :
10395 : : #ifdef HAVE_GAS_HIDDEN
10396 : 2106 : if (dso_handle_node != error_mark_node)
10397 : : {
10398 : 2103 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
10399 : 2103 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
10400 : : }
10401 : : #endif
10402 : :
10403 : 2106 : return dso_handle_node;
10404 : : }
10405 : :
10406 : : /* Begin a new function with internal linkage whose job will be simply
10407 : : to destroy some particular DECL. OB_PARM is true if object pointer
10408 : : is passed to the cleanup function, otherwise no argument is passed. */
10409 : :
10410 : : static tree
10411 : 367 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
10412 : : {
10413 : 367 : push_to_top_level ();
10414 : :
10415 : : /* No need to mangle this. */
10416 : 367 : push_lang_context (lang_name_c);
10417 : :
10418 : : /* Build the name of the function. */
10419 : 367 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
10420 : 367 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10421 : 367 : dname = targetm.strip_name_encoding (dname);
10422 : 731 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
10423 : :
10424 : 367 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
10425 : : : get_atexit_fn_ptr_type ());
10426 : : /* Build the function declaration. */
10427 : 367 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
10428 : 367 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
10429 : : /* It's a function with internal linkage, generated by the
10430 : : compiler. */
10431 : 367 : TREE_PUBLIC (fndecl) = 0;
10432 : 367 : DECL_ARTIFICIAL (fndecl) = 1;
10433 : : /* Make the function `inline' so that it is only emitted if it is
10434 : : actually needed. It is unlikely that it will be inlined, since
10435 : : it is only called via a function pointer, but we avoid unnecessary
10436 : : emissions this way. */
10437 : 367 : DECL_DECLARED_INLINE_P (fndecl) = 1;
10438 : 367 : DECL_INTERFACE_KNOWN (fndecl) = 1;
10439 : 367 : if (ob_parm)
10440 : : {
10441 : : /* Build the parameter. */
10442 : 355 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
10443 : 355 : TREE_USED (parmdecl) = 1;
10444 : 355 : DECL_READ_P (parmdecl) = 1;
10445 : 355 : DECL_ARGUMENTS (fndecl) = parmdecl;
10446 : : }
10447 : :
10448 : 367 : fndecl = pushdecl (fndecl, /*hidden=*/true);
10449 : 367 : if (omp_target)
10450 : : {
10451 : 3 : DECL_ATTRIBUTES (fndecl)
10452 : 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
10453 : 3 : DECL_ATTRIBUTES (fndecl));
10454 : 3 : DECL_ATTRIBUTES (fndecl)
10455 : 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
10456 : 3 : DECL_ATTRIBUTES (fndecl));
10457 : : }
10458 : 367 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
10459 : :
10460 : 367 : pop_lang_context ();
10461 : :
10462 : 367 : return current_function_decl;
10463 : : }
10464 : :
10465 : : /* Finish the cleanup function begun by start_cleanup_fn. */
10466 : :
10467 : : static void
10468 : 367 : end_cleanup_fn (void)
10469 : : {
10470 : 367 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
10471 : :
10472 : 367 : pop_from_top_level ();
10473 : 367 : }
10474 : :
10475 : : /* Generate code to handle the destruction of DECL, an object with
10476 : : static storage duration. */
10477 : :
10478 : : tree
10479 : 14122 : register_dtor_fn (tree decl, bool omp_target)
10480 : : {
10481 : 14122 : tree cleanup;
10482 : 14122 : tree addr;
10483 : 14122 : tree compound_stmt;
10484 : 14122 : tree fcall;
10485 : 14122 : tree type;
10486 : 14122 : bool ob_parm, dso_parm, use_dtor;
10487 : 14122 : tree arg0, arg1, arg2;
10488 : 14122 : tree atex_node;
10489 : :
10490 : 14122 : type = TREE_TYPE (decl);
10491 : 14122 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
10492 : 10929 : return void_node;
10493 : :
10494 : 3193 : if (decl_maybe_constant_destruction (decl, type)
10495 : 3193 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
10496 : : {
10497 : 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10498 : 0 : return void_node;
10499 : : }
10500 : :
10501 : : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
10502 : : "__aeabi_atexit"), and DECL is a class object, we can just pass the
10503 : : destructor to "__cxa_atexit"; we don't have to build a temporary
10504 : : function to do the cleanup. */
10505 : 6386 : dso_parm = (flag_use_cxa_atexit
10506 : 3193 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
10507 : 3193 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
10508 : 3181 : use_dtor = ob_parm && CLASS_TYPE_P (type);
10509 : 3193 : if (use_dtor)
10510 : : {
10511 : 2826 : cleanup = get_class_binding (type, complete_dtor_identifier);
10512 : :
10513 : : /* Make sure it is accessible. */
10514 : 2826 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
10515 : : tf_warning_or_error);
10516 : : }
10517 : : else
10518 : : {
10519 : : /* Call build_cleanup before we enter the anonymous function so
10520 : : that any access checks will be done relative to the current
10521 : : scope, rather than the scope of the anonymous function. */
10522 : 367 : build_cleanup (decl);
10523 : :
10524 : : /* Now start the function. */
10525 : 367 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
10526 : :
10527 : : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
10528 : : to the original function, rather than the anonymous one. That
10529 : : will make the back end think that nested functions are in use,
10530 : : which causes confusion. */
10531 : 367 : push_deferring_access_checks (dk_no_check);
10532 : 367 : fcall = build_cleanup (decl);
10533 : 367 : pop_deferring_access_checks ();
10534 : :
10535 : : /* Create the body of the anonymous function. */
10536 : 367 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
10537 : 367 : finish_expr_stmt (fcall);
10538 : 367 : finish_compound_stmt (compound_stmt);
10539 : 367 : end_cleanup_fn ();
10540 : : }
10541 : :
10542 : : /* Call atexit with the cleanup function. */
10543 : 3193 : mark_used (cleanup);
10544 : 3193 : cleanup = build_address (cleanup);
10545 : :
10546 : 3193 : if (CP_DECL_THREAD_LOCAL_P (decl))
10547 : 85 : atex_node = get_thread_atexit_node ();
10548 : : else
10549 : 3108 : atex_node = get_atexit_node ();
10550 : :
10551 : 3193 : if (use_dtor)
10552 : : {
10553 : : /* We must convert CLEANUP to the type that "__cxa_atexit"
10554 : : expects. */
10555 : 2826 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
10556 : : /* "__cxa_atexit" will pass the address of DECL to the
10557 : : cleanup function. */
10558 : 2826 : mark_used (decl);
10559 : 2826 : addr = build_address (decl);
10560 : : /* The declared type of the parameter to "__cxa_atexit" is
10561 : : "void *". For plain "T*", we could just let the
10562 : : machinery in cp_build_function_call convert it -- but if the
10563 : : type is "cv-qualified T *", then we need to convert it
10564 : : before passing it in, to avoid spurious errors. */
10565 : 2826 : addr = build_nop (ptr_type_node, addr);
10566 : : }
10567 : : else
10568 : : /* Since the cleanup functions we build ignore the address
10569 : : they're given, there's no reason to pass the actual address
10570 : : in, and, in general, it's cheaper to pass NULL than any
10571 : : other value. */
10572 : 367 : addr = null_pointer_node;
10573 : :
10574 : 3193 : if (dso_parm)
10575 : 3181 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
10576 : : tf_warning_or_error);
10577 : 12 : else if (ob_parm)
10578 : : /* Just pass NULL to the dso handle parm if we don't actually
10579 : : have a DSO handle on this target. */
10580 : 0 : arg2 = null_pointer_node;
10581 : : else
10582 : : arg2 = NULL_TREE;
10583 : :
10584 : 3181 : if (ob_parm)
10585 : : {
10586 : 3181 : if (!CP_DECL_THREAD_LOCAL_P (decl)
10587 : 3181 : && targetm.cxx.use_aeabi_atexit ())
10588 : : {
10589 : : arg1 = cleanup;
10590 : : arg0 = addr;
10591 : : }
10592 : : else
10593 : : {
10594 : : arg1 = addr;
10595 : : arg0 = cleanup;
10596 : : }
10597 : : }
10598 : : else
10599 : : {
10600 : : arg0 = cleanup;
10601 : : arg1 = NULL_TREE;
10602 : : }
10603 : 3193 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
10604 : 3193 : arg0, arg1, arg2, NULL_TREE);
10605 : : }
10606 : :
10607 : : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
10608 : : is its initializer. Generate code to handle the construction
10609 : : and destruction of DECL. */
10610 : :
10611 : : static void
10612 : 14911996 : expand_static_init (tree decl, tree init)
10613 : : {
10614 : 14911996 : gcc_assert (VAR_P (decl));
10615 : 14911996 : gcc_assert (TREE_STATIC (decl));
10616 : :
10617 : : /* Some variables require no dynamic initialization. */
10618 : 14911996 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
10619 : : {
10620 : : /* Make sure the destructor is callable. */
10621 : 14908889 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10622 : 14908889 : if (!init)
10623 : : return;
10624 : : }
10625 : :
10626 : 14840 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
10627 : 13995 : && !DECL_FUNCTION_SCOPE_P (decl))
10628 : : {
10629 : 15 : auto_diagnostic_group d;
10630 : 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
10631 : 15 : if (init)
10632 : 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
10633 : : "needs dynamic initialization", decl);
10634 : : else
10635 : 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
10636 : : "has a non-trivial destructor", decl);
10637 : 15 : static bool informed;
10638 : 15 : if (!informed)
10639 : : {
10640 : 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
10641 : : "initialization and destruction");
10642 : 6 : informed = true;
10643 : : }
10644 : 15 : return;
10645 : 15 : }
10646 : :
10647 : 13965 : if (DECL_FUNCTION_SCOPE_P (decl))
10648 : : {
10649 : : /* Emit code to perform this initialization but once. */
10650 : 3545 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
10651 : 3545 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
10652 : 3545 : tree guard, guard_addr;
10653 : 3545 : tree flag, begin;
10654 : : /* We don't need thread-safety code for thread-local vars. */
10655 : 3545 : bool thread_guard = (flag_threadsafe_statics
10656 : 3545 : && !CP_DECL_THREAD_LOCAL_P (decl));
10657 : :
10658 : : /* Emit code to perform this initialization but once. This code
10659 : : looks like:
10660 : :
10661 : : static <type> guard;
10662 : : if (!__atomic_load (guard.first_byte)) {
10663 : : if (__cxa_guard_acquire (&guard)) {
10664 : : bool flag = false;
10665 : : try {
10666 : : // Do initialization.
10667 : : flag = true; __cxa_guard_release (&guard);
10668 : : // Register variable for destruction at end of program.
10669 : : } catch {
10670 : : if (!flag) __cxa_guard_abort (&guard);
10671 : : }
10672 : : }
10673 : : }
10674 : :
10675 : : Note that the `flag' variable is only set to 1 *after* the
10676 : : initialization is complete. This ensures that an exception,
10677 : : thrown during the construction, will cause the variable to
10678 : : reinitialized when we pass through this code again, as per:
10679 : :
10680 : : [stmt.dcl]
10681 : :
10682 : : If the initialization exits by throwing an exception, the
10683 : : initialization is not complete, so it will be tried again
10684 : : the next time control enters the declaration.
10685 : :
10686 : : This process should be thread-safe, too; multiple threads
10687 : : should not be able to initialize the variable more than
10688 : : once. */
10689 : :
10690 : : /* Create the guard variable. */
10691 : 3545 : guard = get_guard (decl);
10692 : :
10693 : : /* Begin the conditional initialization. */
10694 : 3545 : if_stmt = begin_if_stmt ();
10695 : :
10696 : 3545 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
10697 : 3545 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10698 : :
10699 : 3545 : if (thread_guard)
10700 : : {
10701 : 3492 : tree vfntype = NULL_TREE;
10702 : 3492 : tree acquire_name, release_name, abort_name;
10703 : 3492 : tree acquire_fn, release_fn, abort_fn;
10704 : 3492 : guard_addr = build_address (guard);
10705 : :
10706 : 3492 : acquire_name = get_identifier ("__cxa_guard_acquire");
10707 : 3492 : release_name = get_identifier ("__cxa_guard_release");
10708 : 3492 : abort_name = get_identifier ("__cxa_guard_abort");
10709 : 3492 : acquire_fn = get_global_binding (acquire_name);
10710 : 3492 : release_fn = get_global_binding (release_name);
10711 : 3492 : abort_fn = get_global_binding (abort_name);
10712 : 3492 : if (!acquire_fn)
10713 : 1961 : acquire_fn = push_library_fn
10714 : 1961 : (acquire_name, build_function_type_list (integer_type_node,
10715 : 1961 : TREE_TYPE (guard_addr),
10716 : : NULL_TREE),
10717 : : NULL_TREE, ECF_NOTHROW);
10718 : 3492 : if (!release_fn || !abort_fn)
10719 : 1964 : vfntype = build_function_type_list (void_type_node,
10720 : 1964 : TREE_TYPE (guard_addr),
10721 : : NULL_TREE);
10722 : 1964 : if (!release_fn)
10723 : 1964 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
10724 : : ECF_NOTHROW);
10725 : 3492 : if (!abort_fn)
10726 : 1964 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
10727 : : ECF_NOTHROW | ECF_LEAF);
10728 : :
10729 : 3492 : inner_if_stmt = begin_if_stmt ();
10730 : 3492 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
10731 : : inner_if_stmt);
10732 : :
10733 : 3492 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10734 : 3492 : begin = get_internal_target_expr (boolean_false_node);
10735 : 3492 : flag = TARGET_EXPR_SLOT (begin);
10736 : :
10737 : 3492 : TARGET_EXPR_CLEANUP (begin)
10738 : 3492 : = build3 (COND_EXPR, void_type_node, flag,
10739 : : void_node,
10740 : : build_call_n (abort_fn, 1, guard_addr));
10741 : 3492 : CLEANUP_EH_ONLY (begin) = 1;
10742 : :
10743 : : /* Do the initialization itself. */
10744 : 3492 : init = add_stmt_to_compound (begin, init);
10745 : 3492 : init = add_stmt_to_compound (init,
10746 : : build2 (MODIFY_EXPR, void_type_node,
10747 : : flag, boolean_true_node));
10748 : :
10749 : : /* Use atexit to register a function for destroying this static
10750 : : variable. Do this before calling __cxa_guard_release. */
10751 : 3492 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
10752 : :
10753 : 3492 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
10754 : : guard_addr));
10755 : : }
10756 : : else
10757 : : {
10758 : 53 : init = add_stmt_to_compound (init, set_guard (guard));
10759 : :
10760 : : /* Use atexit to register a function for destroying this static
10761 : : variable. */
10762 : 53 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
10763 : : }
10764 : :
10765 : 3545 : finish_expr_stmt (init);
10766 : :
10767 : 3545 : if (thread_guard)
10768 : : {
10769 : 3492 : finish_compound_stmt (inner_then_clause);
10770 : 3492 : finish_then_clause (inner_if_stmt);
10771 : 3492 : finish_if_stmt (inner_if_stmt);
10772 : : }
10773 : :
10774 : 3545 : finish_compound_stmt (then_clause);
10775 : 3545 : finish_then_clause (if_stmt);
10776 : 3545 : finish_if_stmt (if_stmt);
10777 : : }
10778 : 10420 : else if (CP_DECL_THREAD_LOCAL_P (decl))
10779 : 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
10780 : : else
10781 : 9625 : static_aggregates = tree_cons (init, decl, static_aggregates);
10782 : : }
10783 : :
10784 : :
10785 : : /* Make TYPE a complete type based on INITIAL_VALUE.
10786 : : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10787 : : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
10788 : : 3 if the initializer list is empty (in pedantic mode). */
10789 : :
10790 : : int
10791 : 502163 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
10792 : : {
10793 : 502163 : int failure;
10794 : 502163 : tree type, elt_type;
10795 : :
10796 : : /* Don't get confused by a CONSTRUCTOR for some other type. */
10797 : 502133 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
10798 : 91624 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
10799 : 502480 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
10800 : : return 1;
10801 : :
10802 : 502160 : if (initial_value)
10803 : : {
10804 : : /* An array of character type can be initialized from a
10805 : : brace-enclosed string constant so call reshape_init to
10806 : : remove the optional braces from a braced string literal. */
10807 : 502130 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
10808 : 502130 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
10809 : 28331 : initial_value = reshape_init (*ptype, initial_value,
10810 : : tf_warning_or_error);
10811 : :
10812 : : /* If any of the elements are parameter packs, we can't actually
10813 : : complete this type now because the array size is dependent. */
10814 : 502130 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
10815 : 14983652 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
10816 : 14709154 : if (PACK_EXPANSION_P (e.value))
10817 : : return 0;
10818 : : }
10819 : :
10820 : 502160 : failure = complete_array_type (ptype, initial_value, do_default);
10821 : :
10822 : : /* We can create the array before the element type is complete, which
10823 : : means that we didn't have these two bits set in the original type
10824 : : either. In completing the type, we are expected to propagate these
10825 : : bits. See also complete_type which does the same thing for arrays
10826 : : of fixed size. */
10827 : 502160 : type = *ptype;
10828 : 1004317 : if (type != error_mark_node && TYPE_DOMAIN (type))
10829 : : {
10830 : 502133 : elt_type = TREE_TYPE (type);
10831 : 502133 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
10832 : 1004266 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10833 : 502133 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
10834 : : }
10835 : :
10836 : : return failure;
10837 : : }
10838 : :
10839 : : /* As above, but either give an error or reject zero-size arrays, depending
10840 : : on COMPLAIN. */
10841 : :
10842 : : int
10843 : 758 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
10844 : : bool do_default, tsubst_flags_t complain)
10845 : : {
10846 : 758 : int failure;
10847 : 758 : bool sfinae = !(complain & tf_error);
10848 : : /* In SFINAE context we can't be lenient about zero-size arrays. */
10849 : 758 : if (sfinae)
10850 : 3 : ++pedantic;
10851 : 758 : failure = cp_complete_array_type (ptype, initial_value, do_default);
10852 : 758 : if (sfinae)
10853 : 3 : --pedantic;
10854 : 758 : if (failure)
10855 : : {
10856 : 3 : if (sfinae)
10857 : : /* Not an error. */;
10858 : 0 : else if (failure == 1)
10859 : 0 : error ("initializer fails to determine size of %qT", *ptype);
10860 : 0 : else if (failure == 2)
10861 : : {
10862 : 0 : if (do_default)
10863 : 0 : error ("array size missing in %qT", *ptype);
10864 : : }
10865 : 0 : else if (failure == 3)
10866 : 0 : error ("zero-size array %qT", *ptype);
10867 : 3 : *ptype = error_mark_node;
10868 : : }
10869 : 758 : return failure;
10870 : : }
10871 : :
10872 : : /* Return zero if something is declared to be a member of type
10873 : : CTYPE when in the context of CUR_TYPE. STRING is the error
10874 : : message to print in that case. Otherwise, quietly return 1. */
10875 : :
10876 : : static int
10877 : 24078337 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
10878 : : {
10879 : 24078337 : if (ctype && ctype != cur_type)
10880 : : {
10881 : 0 : if (flags == DTOR_FLAG)
10882 : 0 : error ("destructor for alien class %qT cannot be a member", ctype);
10883 : : else
10884 : 0 : error ("constructor for alien class %qT cannot be a member", ctype);
10885 : 0 : return 0;
10886 : : }
10887 : : return 1;
10888 : : }
10889 : :
10890 : : /* Subroutine of `grokdeclarator'. */
10891 : :
10892 : : /* Generate errors possibly applicable for a given set of specifiers.
10893 : : This is for ARM $7.1.2. */
10894 : :
10895 : : static void
10896 : 394331504 : bad_specifiers (tree object,
10897 : : enum bad_spec_place type,
10898 : : int virtualp,
10899 : : int quals,
10900 : : int inlinep,
10901 : : int friendp,
10902 : : int raises,
10903 : : const location_t* locations)
10904 : : {
10905 : 394331504 : switch (type)
10906 : : {
10907 : 55135590 : case BSP_VAR:
10908 : 55135590 : if (virtualp)
10909 : 0 : error_at (locations[ds_virtual],
10910 : : "%qD declared as a %<virtual%> variable", object);
10911 : 55135590 : if (quals)
10912 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
10913 : : "%qD invalid in variable declaration", object);
10914 : : break;
10915 : 269619963 : case BSP_PARM:
10916 : 269619963 : if (virtualp)
10917 : 0 : error_at (locations[ds_virtual],
10918 : : "%qD declared as a %<virtual%> parameter", object);
10919 : 269619963 : if (inlinep)
10920 : 6 : error_at (locations[ds_inline],
10921 : : "%qD declared as an %<inline%> parameter", object);
10922 : 269619963 : if (quals)
10923 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
10924 : : "%qD invalid in parameter declaration", object);
10925 : : break;
10926 : 42894475 : case BSP_TYPE:
10927 : 42894475 : if (virtualp)
10928 : 0 : error_at (locations[ds_virtual],
10929 : : "%qD declared as a %<virtual%> type", object);
10930 : 42894475 : if (inlinep)
10931 : 6 : error_at (locations[ds_inline],
10932 : : "%qD declared as an %<inline%> type", object);
10933 : 42894475 : if (quals)
10934 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
10935 : : "%qD invalid in type declaration", object);
10936 : : break;
10937 : 26681476 : case BSP_FIELD:
10938 : 26681476 : if (virtualp)
10939 : 6 : error_at (locations[ds_virtual],
10940 : : "%qD declared as a %<virtual%> field", object);
10941 : 26681476 : if (inlinep)
10942 : 6 : error_at (locations[ds_inline],
10943 : : "%qD declared as an %<inline%> field", object);
10944 : 26681476 : if (quals)
10945 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
10946 : : "%qD invalid in field declaration", object);
10947 : : break;
10948 : 0 : default:
10949 : 0 : gcc_unreachable();
10950 : : }
10951 : 394331504 : if (friendp)
10952 : 0 : error ("%q+D declared as a friend", object);
10953 : 394331504 : if (raises
10954 : 109 : && !flag_noexcept_type
10955 : 394331525 : && (TREE_CODE (object) == TYPE_DECL
10956 : 15 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
10957 : 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
10958 : 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
10959 : 6 : error ("%q+D declared with an exception specification", object);
10960 : 394331504 : }
10961 : :
10962 : : /* DECL is a member function or static data member and is presently
10963 : : being defined. Check that the definition is taking place in a
10964 : : valid namespace. */
10965 : :
10966 : : static void
10967 : 82531194 : check_class_member_definition_namespace (tree decl)
10968 : : {
10969 : : /* These checks only apply to member functions and static data
10970 : : members. */
10971 : 82531194 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
10972 : : /* We check for problems with specializations in pt.cc in
10973 : : check_specialization_namespace, where we can issue better
10974 : : diagnostics. */
10975 : 82531194 : if (processing_specialization)
10976 : : return;
10977 : : /* We check this in check_explicit_instantiation_namespace. */
10978 : 82375903 : if (processing_explicit_instantiation)
10979 : : return;
10980 : : /* [class.mfct]
10981 : :
10982 : : A member function definition that appears outside of the
10983 : : class definition shall appear in a namespace scope enclosing
10984 : : the class definition.
10985 : :
10986 : : [class.static.data]
10987 : :
10988 : : The definition for a static data member shall appear in a
10989 : : namespace scope enclosing the member's class definition. */
10990 : 82375831 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
10991 : 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
10992 : 18 : decl, DECL_CONTEXT (decl));
10993 : : }
10994 : :
10995 : : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10996 : : METHOD_TYPE for a non-static member function; QUALS are the
10997 : : cv-qualifiers that apply to the function. */
10998 : :
10999 : : tree
11000 : 202079515 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
11001 : : {
11002 : 202079515 : tree this_type;
11003 : 202079515 : tree qual_type;
11004 : 202079515 : tree parm;
11005 : 202079515 : cp_cv_quals this_quals;
11006 : :
11007 : 202079515 : if (CLASS_TYPE_P (type))
11008 : : {
11009 : 90879288 : this_type
11010 : 90879288 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
11011 : 90879288 : this_type = build_pointer_type (this_type);
11012 : : }
11013 : : else
11014 : 111200227 : this_type = type_of_this_parm (type);
11015 : : /* The `this' parameter is implicitly `const'; it cannot be
11016 : : assigned to. */
11017 : 202079515 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
11018 : 202079515 : qual_type = cp_build_qualified_type (this_type, this_quals);
11019 : 202079515 : parm = build_artificial_parm (fn, this_identifier, qual_type);
11020 : 202079515 : cp_apply_type_quals_to_decl (this_quals, parm);
11021 : 202079515 : return parm;
11022 : : }
11023 : :
11024 : : /* DECL is a static member function. Complain if it was declared
11025 : : with function-cv-quals. */
11026 : :
11027 : : static void
11028 : 8434773 : check_static_quals (tree decl, cp_cv_quals quals)
11029 : : {
11030 : 0 : if (quals != TYPE_UNQUALIFIED)
11031 : 6 : error ("static member function %q#D declared with type qualifiers",
11032 : : decl);
11033 : 0 : }
11034 : :
11035 : : /* Helper function. Replace the temporary this parameter injected
11036 : : during cp_finish_omp_declare_simd with the real this parameter. */
11037 : :
11038 : : static tree
11039 : 2500 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11040 : : {
11041 : 2500 : tree this_parm = (tree) data;
11042 : 2500 : if (TREE_CODE (*tp) == PARM_DECL
11043 : 450 : && DECL_NAME (*tp) == this_identifier
11044 : 2773 : && *tp != this_parm)
11045 : 273 : *tp = this_parm;
11046 : 2227 : else if (TYPE_P (*tp))
11047 : 33 : *walk_subtrees = 0;
11048 : 2500 : return NULL_TREE;
11049 : : }
11050 : :
11051 : : /* CTYPE is class type, or null if non-class.
11052 : : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11053 : : or METHOD_TYPE.
11054 : : DECLARATOR is the function's name.
11055 : : PARMS is a chain of PARM_DECLs for the function.
11056 : : VIRTUALP is truthvalue of whether the function is virtual or not.
11057 : : FLAGS are to be passed through to `grokclassfn'.
11058 : : QUALS are qualifiers indicating whether the function is `const'
11059 : : or `volatile'.
11060 : : RAISES is a list of exceptions that this function can raise.
11061 : : CHECK is 1 if we must find this method in CTYPE, 0 if we should
11062 : : not look, and -1 if we should not call `grokclassfn' at all.
11063 : :
11064 : : SFK is the kind of special function (if any) for the new function.
11065 : :
11066 : : Returns `NULL_TREE' if something goes wrong, after issuing
11067 : : applicable error messages. */
11068 : :
11069 : : static tree
11070 : 168632007 : grokfndecl (tree ctype,
11071 : : tree type,
11072 : : tree declarator,
11073 : : tree parms,
11074 : : tree orig_declarator,
11075 : : const cp_decl_specifier_seq *declspecs,
11076 : : tree decl_reqs,
11077 : : int virtualp,
11078 : : enum overload_flags flags,
11079 : : cp_cv_quals quals,
11080 : : cp_ref_qualifier rqual,
11081 : : tree raises,
11082 : : int check,
11083 : : int friendp,
11084 : : int publicp,
11085 : : int inlinep,
11086 : : bool deletedp,
11087 : : bool xobj_func_p,
11088 : : special_function_kind sfk,
11089 : : bool funcdef_flag,
11090 : : bool late_return_type_p,
11091 : : int template_count,
11092 : : tree in_namespace,
11093 : : tree* attrlist,
11094 : : location_t location)
11095 : : {
11096 : 168632007 : tree decl;
11097 : 168632007 : tree t;
11098 : :
11099 : 168632007 : if (location == UNKNOWN_LOCATION)
11100 : 0 : location = input_location;
11101 : :
11102 : : /* Was the concept specifier present? */
11103 : 168632007 : bool concept_p = inlinep & 4;
11104 : :
11105 : 168632007 : if (concept_p)
11106 : : {
11107 : 15 : error_at (location, "function concepts are no longer supported");
11108 : 15 : return NULL_TREE;
11109 : : }
11110 : :
11111 : 168631992 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
11112 : :
11113 : 168631992 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
11114 : :
11115 : : /* Set the constraints on the declaration. */
11116 : 168631992 : if (flag_concepts)
11117 : : {
11118 : 47455410 : tree tmpl_reqs = NULL_TREE;
11119 : 47455410 : tree ctx = friendp ? current_class_type : ctype;
11120 : 47455410 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
11121 : 47455410 : bool memtmpl = (!block_local
11122 : 47455410 : && (current_template_depth
11123 : 47442971 : > template_class_depth (ctx)));
11124 : 13457880 : if (memtmpl)
11125 : : {
11126 : 13457880 : if (!current_template_parms)
11127 : : /* If there are no template parameters, something must have
11128 : : gone wrong. */
11129 : 0 : gcc_assert (seen_error ());
11130 : : else
11131 : 13457880 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
11132 : : }
11133 : 47455410 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
11134 : : /* C++20 CA378: Remove non-templated constrained functions. */
11135 : : /* [temp.friend]/9 A non-template friend declaration with a
11136 : : requires-clause shall be a definition. A friend function template with
11137 : : a constraint that depends on a template parameter from an enclosing
11138 : : template shall be a definition. */
11139 : 47455410 : if (ci
11140 : 3413520 : && (block_local
11141 : 3413517 : || !processing_template_decl
11142 : 3413462 : || (friendp && !memtmpl && !funcdef_flag)))
11143 : : {
11144 : 67 : if (!friendp || !processing_template_decl)
11145 : 58 : error_at (location, "constraints on a non-templated function");
11146 : : else
11147 : 9 : error_at (location, "constrained non-template friend declaration"
11148 : : " must be a definition");
11149 : : ci = NULL_TREE;
11150 : : }
11151 : 47455410 : set_constraints (decl, ci);
11152 : 448320 : if (ci && friendp && memtmpl && !funcdef_flag
11153 : 47464523 : && uses_outer_template_parms_in_constraints (decl, ctx))
11154 : 6 : error_at (location, "friend function template with constraints that "
11155 : : "depend on outer template parameters must be a definition");
11156 : : }
11157 : :
11158 : 168631992 : if (TREE_CODE (type) == METHOD_TYPE)
11159 : : {
11160 : 88989418 : tree parm = build_this_parm (decl, type, quals);
11161 : 88989418 : DECL_CHAIN (parm) = parms;
11162 : 88989418 : parms = parm;
11163 : :
11164 : : /* Allocate space to hold the vptr bit if needed. */
11165 : 88989418 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
11166 : : }
11167 : :
11168 : 168631992 : DECL_ARGUMENTS (decl) = parms;
11169 : 512234772 : for (t = parms; t; t = DECL_CHAIN (t))
11170 : 343602780 : DECL_CONTEXT (t) = decl;
11171 : :
11172 : : /* Propagate volatile out from type to decl. */
11173 : 168631992 : if (TYPE_VOLATILE (type))
11174 : 0 : TREE_THIS_VOLATILE (decl) = 1;
11175 : :
11176 : : /* Setup decl according to sfk. */
11177 : 168631992 : switch (sfk)
11178 : : {
11179 : 21937892 : case sfk_constructor:
11180 : 21937892 : case sfk_copy_constructor:
11181 : 21937892 : case sfk_move_constructor:
11182 : 21937892 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
11183 : 21937892 : DECL_NAME (decl) = ctor_identifier;
11184 : 21937892 : break;
11185 : 3095640 : case sfk_destructor:
11186 : 3095640 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
11187 : 3095640 : DECL_NAME (decl) = dtor_identifier;
11188 : 3095640 : break;
11189 : 1038538 : case sfk_deduction_guide:
11190 : : /* Give deduction guides a definition even though they don't really
11191 : : have one: the restriction that you can't repeat a deduction guide
11192 : : makes them more like a definition anyway. */
11193 : 1038538 : DECL_INITIAL (decl) = void_node;
11194 : : /* But to ensure that external-linkage deduction guides in header units
11195 : : don't fall afoul of [module.import] p6, mark them as inline. */
11196 : 1038538 : DECL_DECLARED_INLINE_P (decl) = true;
11197 : 1038538 : break;
11198 : : default:
11199 : : break;
11200 : : }
11201 : :
11202 : 168631992 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
11203 : : {
11204 : 14090 : if (funcdef_flag)
11205 : 3 : error_at (location,
11206 : : "defining explicit specialization %qD in friend declaration",
11207 : : orig_declarator);
11208 : : else
11209 : : {
11210 : 14087 : tree fns = TREE_OPERAND (orig_declarator, 0);
11211 : 14087 : tree args = TREE_OPERAND (orig_declarator, 1);
11212 : :
11213 : 14087 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11214 : : {
11215 : : /* Something like `template <class T> friend void f<T>()'. */
11216 : 6 : error_at (location,
11217 : : "invalid use of template-id %qD in declaration "
11218 : : "of primary template",
11219 : : orig_declarator);
11220 : 6 : return NULL_TREE;
11221 : : }
11222 : :
11223 : : /* A friend declaration of the form friend void f<>(). Record
11224 : : the information in the TEMPLATE_ID_EXPR. */
11225 : 14081 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
11226 : :
11227 : 14081 : gcc_assert (identifier_p (fns) || OVL_P (fns));
11228 : 14081 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
11229 : :
11230 : 68613 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
11231 : 54536 : if (TREE_PURPOSE (t)
11232 : 54536 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
11233 : : {
11234 : 4 : error_at (defparse_location (TREE_PURPOSE (t)),
11235 : : "default arguments are not allowed in declaration "
11236 : : "of friend template specialization %qD",
11237 : : decl);
11238 : 4 : return NULL_TREE;
11239 : : }
11240 : :
11241 : 14077 : if (inlinep & 1)
11242 : : {
11243 : 4 : error_at (declspecs->locations[ds_inline],
11244 : : "%<inline%> is not allowed in declaration of friend "
11245 : : "template specialization %qD",
11246 : : decl);
11247 : 4 : return NULL_TREE;
11248 : : }
11249 : : }
11250 : : }
11251 : :
11252 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
11253 : : expression, that declaration shall be a definition..." */
11254 : 168631978 : if (friendp && !funcdef_flag)
11255 : : {
11256 : 1224426 : bool has_errored = false;
11257 : 1224426 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
11258 : 3576222 : t && t != void_list_node; t = TREE_CHAIN (t))
11259 : 2351796 : if (TREE_PURPOSE (t))
11260 : : {
11261 : 48 : diagnostic_t diag_kind = DK_PERMERROR;
11262 : : /* For templates, mark the default argument as erroneous and give a
11263 : : hard error. */
11264 : 48 : if (processing_template_decl)
11265 : : {
11266 : 30 : diag_kind = DK_ERROR;
11267 : 30 : TREE_PURPOSE (t) = error_mark_node;
11268 : : }
11269 : 48 : if (!has_errored)
11270 : : {
11271 : 45 : has_errored = true;
11272 : 45 : emit_diagnostic (diag_kind,
11273 : 45 : DECL_SOURCE_LOCATION (decl),
11274 : 45 : /*diagnostic_option_id=*/0,
11275 : : "friend declaration of %qD specifies default "
11276 : : "arguments and isn%'t a definition", decl);
11277 : : }
11278 : : }
11279 : : }
11280 : :
11281 : : /* If this decl has namespace scope, set that up. */
11282 : 168631978 : if (in_namespace)
11283 : 145056 : set_decl_namespace (decl, in_namespace, friendp);
11284 : 168486922 : else if (ctype)
11285 : 97291848 : DECL_CONTEXT (decl) = ctype;
11286 : : else
11287 : 71195074 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
11288 : :
11289 : : /* `main' and builtins have implicit 'C' linkage. */
11290 : 168631978 : if (ctype == NULL_TREE
11291 : 71340130 : && DECL_FILE_SCOPE_P (decl)
11292 : 30315424 : && current_lang_name == lang_name_cplusplus
11293 : 171401465 : && (MAIN_NAME_P (declarator)
11294 : 2736600 : || (IDENTIFIER_LENGTH (declarator) > 10
11295 : 1911616 : && IDENTIFIER_POINTER (declarator)[0] == '_'
11296 : 1460731 : && IDENTIFIER_POINTER (declarator)[1] == '_'
11297 : 345105 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
11298 : : "builtin_"))
11299 : 2736582 : || (targetcm.cxx_implicit_extern_c
11300 : 0 : && (targetcm.cxx_implicit_extern_c
11301 : 0 : (IDENTIFIER_POINTER (declarator))))))
11302 : 32905 : SET_DECL_LANGUAGE (decl, lang_c);
11303 : :
11304 : 168631978 : DECL_STATIC_FUNCTION_P (decl)
11305 : 168631978 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
11306 : 168631978 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
11307 : :
11308 : 168631978 : if (deletedp)
11309 : 3470574 : DECL_DELETED_FN (decl) = 1;
11310 : :
11311 : 168631978 : if (ctype && funcdef_flag)
11312 : 82149954 : check_class_member_definition_namespace (decl);
11313 : :
11314 : 168631978 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11315 : : {
11316 : 32890 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
11317 : 9 : error_at (location, "cannot declare %<::main%> to be a template");
11318 : 32890 : if (inlinep & 1)
11319 : 3 : error_at (declspecs->locations[ds_inline],
11320 : : "cannot declare %<::main%> to be inline");
11321 : 32890 : if (inlinep & 2)
11322 : 6 : error_at (declspecs->locations[ds_constexpr],
11323 : : "cannot declare %<::main%> to be %qs", "constexpr");
11324 : 32890 : if (inlinep & 8)
11325 : 3 : error_at (declspecs->locations[ds_consteval],
11326 : : "cannot declare %<::main%> to be %qs", "consteval");
11327 : 32890 : if (!publicp)
11328 : 0 : error_at (location, "cannot declare %<::main%> to be static");
11329 : 32890 : if (current_lang_depth () != 0)
11330 : 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
11331 : : " linkage specification");
11332 : 32890 : if (module_attach_p ())
11333 : 3 : error_at (location, "cannot attach %<::main%> to a named module");
11334 : : inlinep = 0;
11335 : : publicp = 1;
11336 : : }
11337 : :
11338 : : /* Members of anonymous types and local classes have no linkage; make
11339 : : them internal. If a typedef is made later, this will be changed. */
11340 : 168631978 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
11341 : : publicp = 0;
11342 : 168604126 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
11343 : : /* But members of local classes in a module CMI should have their
11344 : : definitions exported, in case they are (directly or indirectly)
11345 : : used by an importer. We don't just use module_has_cmi_p here
11346 : : because for entities in the GMF we don't yet know whether this
11347 : : module will have a CMI, so we'll conservatively assume it might. */
11348 : 1041948 : publicp = module_maybe_has_cmi_p ();
11349 : :
11350 : 168604126 : if (publicp && cxx_dialect == cxx98)
11351 : : {
11352 : : /* [basic.link]: A name with no linkage (notably, the name of a class
11353 : : or enumeration declared in a local scope) shall not be used to
11354 : : declare an entity with linkage.
11355 : :
11356 : : DR 757 relaxes this restriction for C++0x. */
11357 : 925764 : no_linkage_error (decl);
11358 : : }
11359 : :
11360 : 168631978 : TREE_PUBLIC (decl) = publicp;
11361 : 168631978 : if (! publicp)
11362 : : {
11363 : 1172484 : DECL_INTERFACE_KNOWN (decl) = 1;
11364 : 1172484 : DECL_NOT_REALLY_EXTERN (decl) = 1;
11365 : : }
11366 : :
11367 : : /* If the declaration was declared inline, mark it as such. */
11368 : 168631978 : if (inlinep)
11369 : : {
11370 : 49977414 : DECL_DECLARED_INLINE_P (decl) = 1;
11371 : 49977414 : if (publicp)
11372 : 49834526 : DECL_COMDAT (decl) = 1;
11373 : : }
11374 : 49977414 : if (inlinep & 2)
11375 : 29311610 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
11376 : 139320368 : else if (inlinep & 8)
11377 : : {
11378 : 218526 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
11379 : 218526 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
11380 : : }
11381 : :
11382 : 168631978 : DECL_EXTERNAL (decl) = 1;
11383 : 168631978 : if (TREE_CODE (type) == FUNCTION_TYPE)
11384 : : {
11385 : 79642560 : if (quals || rqual)
11386 : 272 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
11387 : : TYPE_UNQUALIFIED,
11388 : : REF_QUAL_NONE);
11389 : 79642560 : auto_diagnostic_group d;
11390 : 79642560 : if (quals)
11391 : 423 : error (!ctype
11392 : : ? G_("non-member function %qD cannot have cv-qualifier")
11393 : : : !xobj_func_p
11394 : 198 : ? G_("static member function %qD cannot have cv-qualifier")
11395 : : : G_("explicit object member function "
11396 : : "%qD cannot have cv-qualifier"),
11397 : : decl);
11398 : 79642560 : if (rqual)
11399 : 355 : error (!ctype
11400 : : ? G_("non-member function %qD cannot have ref-qualifier")
11401 : : : !xobj_func_p
11402 : 176 : ? G_("static member function %qD cannot have ref-qualifier")
11403 : : : G_("explicit object member function "
11404 : : "%qD cannot have ref-qualifier"),
11405 : : decl);
11406 : :
11407 : 79642560 : if (xobj_func_p && (quals || rqual))
11408 : 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
11409 : : "explicit object parameter declared here");
11410 : 79642560 : quals = TYPE_UNQUALIFIED;
11411 : 79642560 : rqual = REF_QUAL_NONE;
11412 : :
11413 : 79642560 : }
11414 : :
11415 : 168631978 : if (deduction_guide_p (decl))
11416 : : {
11417 : 1038538 : tree type = TREE_TYPE (DECL_NAME (decl));
11418 : 1038538 : if (in_namespace == NULL_TREE
11419 : 1038538 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
11420 : : {
11421 : 3 : auto_diagnostic_group d;
11422 : 3 : error_at (location, "deduction guide %qD must be declared in the "
11423 : : "same scope as %qT", decl, type);
11424 : 3 : inform (location_of (type), " declared here");
11425 : 3 : return NULL_TREE;
11426 : 3 : }
11427 : 2077070 : if (DECL_CLASS_SCOPE_P (decl)
11428 : 1038574 : && current_access_specifier != declared_access (TYPE_NAME (type)))
11429 : : {
11430 : 3 : auto_diagnostic_group d;
11431 : 3 : error_at (location, "deduction guide %qD must have the same access "
11432 : : "as %qT", decl, type);
11433 : 3 : inform (location_of (type), " declared here");
11434 : 3 : }
11435 : 1038535 : if (funcdef_flag)
11436 : 3 : error_at (location,
11437 : : "deduction guide %qD must not have a function body", decl);
11438 : : }
11439 : 314633059 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
11440 : 177947058 : && !grok_op_properties (decl, /*complain=*/true))
11441 : : return NULL_TREE;
11442 : 167593290 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
11443 : : {
11444 : 276932 : bool long_long_unsigned_p;
11445 : 276932 : bool long_double_p;
11446 : 276932 : const char *suffix = NULL;
11447 : : /* [over.literal]/6: Literal operators shall not have C linkage. */
11448 : 276932 : if (DECL_LANGUAGE (decl) == lang_c)
11449 : : {
11450 : 6 : auto_diagnostic_group d;
11451 : 6 : error_at (location, "literal operator with C linkage");
11452 : 6 : maybe_show_extern_c_location ();
11453 : 6 : return NULL_TREE;
11454 : 6 : }
11455 : :
11456 : 276926 : if (DECL_NAMESPACE_SCOPE_P (decl))
11457 : : {
11458 : 276920 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
11459 : : &long_double_p))
11460 : : {
11461 : 42 : error_at (location, "%qD has invalid argument list", decl);
11462 : 42 : return NULL_TREE;
11463 : : }
11464 : :
11465 : 276878 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
11466 : 276878 : if (long_long_unsigned_p)
11467 : : {
11468 : 33457 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
11469 : 68 : warning_at (location, 0, "integer suffix %qs"
11470 : : " shadowed by implementation", suffix);
11471 : : }
11472 : 243421 : else if (long_double_p)
11473 : : {
11474 : 87834 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
11475 : 80 : warning_at (location, 0, "floating-point suffix %qs"
11476 : : " shadowed by implementation", suffix);
11477 : : }
11478 : : /* 17.6.3.3.5 */
11479 : 276878 : if (suffix[0] != '_'
11480 : 276334 : && !current_function_decl && !(friendp && !funcdef_flag))
11481 : 276328 : warning_at (location, OPT_Wliteral_suffix,
11482 : : "literal operator suffixes not preceded by %<_%>"
11483 : : " are reserved for future standardization");
11484 : : }
11485 : : else
11486 : : {
11487 : 6 : error_at (location, "%qD must be a non-member function", decl);
11488 : 6 : return NULL_TREE;
11489 : : }
11490 : : }
11491 : :
11492 : 167593239 : if (funcdef_flag)
11493 : : /* Make the init_value nonzero so pushdecl knows this is not
11494 : : tentative. error_mark_node is replaced later with the BLOCK. */
11495 : 115232769 : DECL_INITIAL (decl) = error_mark_node;
11496 : :
11497 : 168631771 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
11498 : 62753351 : TREE_NOTHROW (decl) = 1;
11499 : :
11500 : 168631771 : if (flag_openmp || flag_openmp_simd)
11501 : : {
11502 : : /* Adjust "omp declare simd" attributes. */
11503 : 440498 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
11504 : 440498 : if (ods)
11505 : : {
11506 : : tree attr;
11507 : 2006 : for (attr = ods; attr;
11508 : 1060 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
11509 : : {
11510 : 1060 : if (TREE_CODE (type) == METHOD_TYPE)
11511 : 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
11512 : : DECL_ARGUMENTS (decl), NULL);
11513 : 1060 : if (TREE_VALUE (attr) != NULL_TREE)
11514 : : {
11515 : 854 : tree cl = TREE_VALUE (TREE_VALUE (attr));
11516 : 854 : cl = c_omp_declare_simd_clauses_to_numbers
11517 : 854 : (DECL_ARGUMENTS (decl), cl);
11518 : 854 : if (cl)
11519 : 854 : TREE_VALUE (TREE_VALUE (attr)) = cl;
11520 : : else
11521 : 0 : TREE_VALUE (attr) = NULL_TREE;
11522 : : }
11523 : : }
11524 : : }
11525 : : }
11526 : :
11527 : : /* Caller will do the rest of this. */
11528 : 168631771 : if (check < 0)
11529 : 3880842 : return decl;
11530 : :
11531 : 164750929 : if (ctype != NULL_TREE)
11532 : 97291552 : grokclassfn (ctype, decl, flags);
11533 : :
11534 : : /* 12.4/3 */
11535 : 164750929 : if (cxx_dialect >= cxx11
11536 : 327654602 : && DECL_DESTRUCTOR_P (decl)
11537 : 3077973 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
11538 : 164919815 : && !processing_template_decl)
11539 : 85356 : deduce_noexcept_on_destructor (decl);
11540 : :
11541 : 164750929 : set_originating_module (decl);
11542 : :
11543 : 164750929 : decl = check_explicit_specialization (orig_declarator, decl,
11544 : : template_count,
11545 : 164750929 : 2 * funcdef_flag +
11546 : 164750929 : 4 * (friendp != 0),
11547 : : *attrlist);
11548 : 164750929 : if (decl == error_mark_node)
11549 : : return NULL_TREE;
11550 : :
11551 : 164750681 : if (DECL_STATIC_FUNCTION_P (decl))
11552 : 8277222 : check_static_quals (decl, quals);
11553 : :
11554 : 164750681 : if (attrlist)
11555 : : {
11556 : 164750681 : cplus_decl_attributes (&decl, *attrlist, 0);
11557 : 164750681 : *attrlist = NULL_TREE;
11558 : : }
11559 : :
11560 : 164750681 : if (DECL_HAS_CONTRACTS_P (decl))
11561 : 467 : rebuild_postconditions (decl);
11562 : :
11563 : : /* Check main's type after attributes have been applied. */
11564 : 164750681 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11565 : : {
11566 : 32878 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
11567 : : integer_type_node))
11568 : : {
11569 : 20 : tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
11570 : 20 : tree newtype;
11571 : 20 : error_at (declspecs->locations[ds_type_spec],
11572 : : "%<::main%> must return %<int%>");
11573 : 20 : newtype = build_function_type (integer_type_node, oldtypeargs);
11574 : 20 : TREE_TYPE (decl) = newtype;
11575 : : }
11576 : 32878 : if (warn_main)
11577 : 32856 : check_main_parameter_types (decl);
11578 : : }
11579 : :
11580 : 164750681 : if (ctype != NULL_TREE && check)
11581 : : {
11582 : 9003286 : tree old_decl = check_classfn (ctype, decl,
11583 : 9003286 : (current_template_depth
11584 : 9003286 : > template_class_depth (ctype))
11585 : 2115399 : ? current_template_parms
11586 : : : NULL_TREE);
11587 : :
11588 : 9003286 : if (old_decl == error_mark_node)
11589 : : return NULL_TREE;
11590 : :
11591 : 9003205 : if (old_decl)
11592 : : {
11593 : 8600525 : tree ok;
11594 : 8600525 : tree pushed_scope;
11595 : :
11596 : 8600525 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
11597 : : /* Because grokfndecl is always supposed to return a
11598 : : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11599 : : here. We depend on our callers to figure out that its
11600 : : really a template that's being returned. */
11601 : 2115387 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
11602 : :
11603 : 8600525 : if (DECL_STATIC_FUNCTION_P (old_decl)
11604 : 8600525 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
11605 : : {
11606 : : /* Remove the `this' parm added by grokclassfn. */
11607 : 157551 : revert_static_member_fn (decl);
11608 : 157551 : check_static_quals (decl, quals);
11609 : : }
11610 : 8600525 : if (DECL_ARTIFICIAL (old_decl))
11611 : : {
11612 : 9 : error ("definition of implicitly-declared %qD", old_decl);
11613 : 9 : return NULL_TREE;
11614 : : }
11615 : 8600516 : else if (DECL_DEFAULTED_FN (old_decl))
11616 : : {
11617 : 3 : auto_diagnostic_group d;
11618 : 3 : error ("definition of explicitly-defaulted %q+D", decl);
11619 : 3 : inform (DECL_SOURCE_LOCATION (old_decl),
11620 : : "%q#D explicitly defaulted here", old_decl);
11621 : 3 : return NULL_TREE;
11622 : 3 : }
11623 : :
11624 : : /* Since we've smashed OLD_DECL to its
11625 : : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
11626 : 8600513 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11627 : 947 : decl = DECL_TEMPLATE_RESULT (decl);
11628 : :
11629 : : /* Attempt to merge the declarations. This can fail, in
11630 : : the case of some invalid specialization declarations. */
11631 : 8600513 : pushed_scope = push_scope (ctype);
11632 : 8600513 : ok = duplicate_decls (decl, old_decl);
11633 : 8600513 : if (pushed_scope)
11634 : 8600513 : pop_scope (pushed_scope);
11635 : 8600513 : if (!ok)
11636 : : {
11637 : 0 : error ("no %q#D member function declared in class %qT",
11638 : : decl, ctype);
11639 : 0 : return NULL_TREE;
11640 : : }
11641 : 8600513 : if (ok == error_mark_node)
11642 : : return NULL_TREE;
11643 : : return old_decl;
11644 : : }
11645 : : }
11646 : :
11647 : 312300150 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
11648 : : return NULL_TREE;
11649 : :
11650 : 156150072 : if (ctype == NULL_TREE || check)
11651 : 67861866 : return decl;
11652 : :
11653 : 88288206 : if (virtualp)
11654 : 3989605 : DECL_VIRTUAL_P (decl) = 1;
11655 : :
11656 : 88288206 : return decl;
11657 : : }
11658 : :
11659 : : /* decl is a FUNCTION_DECL.
11660 : : specifiers are the parsed virt-specifiers.
11661 : :
11662 : : Set flags to reflect the virt-specifiers.
11663 : :
11664 : : Returns decl. */
11665 : :
11666 : : static tree
11667 : 92169146 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
11668 : : {
11669 : 92169146 : if (decl == NULL_TREE)
11670 : : return decl;
11671 : 92169048 : if (specifiers & VIRT_SPEC_OVERRIDE)
11672 : 175138 : DECL_OVERRIDE_P (decl) = 1;
11673 : 92169048 : if (specifiers & VIRT_SPEC_FINAL)
11674 : 2831 : DECL_FINAL_P (decl) = 1;
11675 : : return decl;
11676 : : }
11677 : :
11678 : : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
11679 : : the linkage that DECL will receive in the object file. */
11680 : :
11681 : : static void
11682 : 10308188 : set_linkage_for_static_data_member (tree decl)
11683 : : {
11684 : : /* A static data member always has static storage duration and
11685 : : external linkage. Note that static data members are forbidden in
11686 : : local classes -- the only situation in which a class has
11687 : : non-external linkage. */
11688 : 10308188 : TREE_PUBLIC (decl) = 1;
11689 : 10308188 : TREE_STATIC (decl) = 1;
11690 : : /* For non-template classes, static data members are always put
11691 : : out in exactly those files where they are defined, just as
11692 : : with ordinary namespace-scope variables. */
11693 : 10308188 : if (!processing_template_decl)
11694 : 7594633 : DECL_INTERFACE_KNOWN (decl) = 1;
11695 : 10308188 : }
11696 : :
11697 : : /* Create a VAR_DECL named NAME with the indicated TYPE.
11698 : :
11699 : : If SCOPE is non-NULL, it is the class type or namespace containing
11700 : : the variable. If SCOPE is NULL, the variable should is created in
11701 : : the innermost enclosing scope. */
11702 : :
11703 : : static tree
11704 : 55135639 : grokvardecl (tree type,
11705 : : tree name,
11706 : : tree orig_declarator,
11707 : : const cp_decl_specifier_seq *declspecs,
11708 : : int initialized,
11709 : : int type_quals,
11710 : : int inlinep,
11711 : : bool conceptp,
11712 : : int template_count,
11713 : : tree scope,
11714 : : location_t location)
11715 : : {
11716 : 55135639 : tree decl;
11717 : 55135639 : tree explicit_scope;
11718 : :
11719 : 55135639 : gcc_assert (!name || identifier_p (name));
11720 : :
11721 : 55135639 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
11722 : 55135639 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
11723 : :
11724 : : /* Compute the scope in which to place the variable, but remember
11725 : : whether or not that scope was explicitly specified by the user. */
11726 : 55135639 : explicit_scope = scope;
11727 : 55135639 : if (!scope)
11728 : : {
11729 : : /* An explicit "extern" specifier indicates a namespace-scope
11730 : : variable. */
11731 : 54750010 : if (declspecs->storage_class == sc_extern)
11732 : 555331 : scope = current_decl_namespace ();
11733 : 54194679 : else if (!at_function_scope_p ())
11734 : 4209840 : scope = current_scope ();
11735 : : }
11736 : :
11737 : 54750010 : if (scope
11738 : 5150800 : && (/* If the variable is a namespace-scope variable declared in a
11739 : : template, we need DECL_LANG_SPECIFIC. */
11740 : 5150800 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
11741 : : /* Similarly for namespace-scope variables with language linkage
11742 : : other than C++. */
11743 : 4769560 : || (TREE_CODE (scope) == NAMESPACE_DECL
11744 : 2416138 : && current_lang_name != lang_name_cplusplus)
11745 : : /* Similarly for static data members. */
11746 : 2564968 : || TYPE_P (scope)
11747 : : /* Similarly for explicit specializations. */
11748 : 2183728 : || (orig_declarator
11749 : 2183370 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
11750 : 3308080 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
11751 : : else
11752 : 51827559 : decl = build_decl (location, VAR_DECL, name, type);
11753 : :
11754 : 55135639 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
11755 : 4389 : set_decl_namespace (decl, explicit_scope, 0);
11756 : : else
11757 : 55131250 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
11758 : :
11759 : 55135639 : if (declspecs->storage_class == sc_extern)
11760 : : {
11761 : 555331 : DECL_THIS_EXTERN (decl) = 1;
11762 : 555331 : DECL_EXTERNAL (decl) = !initialized;
11763 : : }
11764 : :
11765 : 55135639 : if (DECL_CLASS_SCOPE_P (decl))
11766 : : {
11767 : 381240 : set_linkage_for_static_data_member (decl);
11768 : : /* This function is only called with out-of-class definitions. */
11769 : 381240 : DECL_EXTERNAL (decl) = 0;
11770 : 381240 : check_class_member_definition_namespace (decl);
11771 : : }
11772 : : /* At top level, either `static' or no s.c. makes a definition
11773 : : (perhaps tentative), and absence of `static' makes it public. */
11774 : 54754399 : else if (toplevel_bindings_p ())
11775 : : {
11776 : 9536844 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
11777 : 4768422 : && (DECL_THIS_EXTERN (decl)
11778 : 4175577 : || ! constp
11779 : 4175577 : || volatilep
11780 : 4102344 : || inlinep
11781 : 294189 : || in_template_context
11782 : 27228 : || processing_specialization
11783 : 27149 : || module_attach_p ()));
11784 : 4768422 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
11785 : : }
11786 : : /* Not at top level, only `static' makes a static definition. */
11787 : : else
11788 : : {
11789 : 49985977 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
11790 : 49985977 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
11791 : : }
11792 : :
11793 : 55135639 : set_originating_module (decl);
11794 : :
11795 : 55135639 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
11796 : : {
11797 : 20300 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
11798 : : {
11799 : 20292 : CP_DECL_THREAD_LOCAL_P (decl) = true;
11800 : 20292 : if (!processing_template_decl)
11801 : 20249 : set_decl_tls_model (decl, decl_default_tls_model (decl));
11802 : : }
11803 : 20300 : if (declspecs->gnu_thread_keyword_p)
11804 : 19406 : SET_DECL_GNU_TLS_P (decl);
11805 : : }
11806 : :
11807 : : /* If the type of the decl has no linkage, make sure that we'll
11808 : : notice that in mark_used. */
11809 : 55135639 : if (cxx_dialect > cxx98
11810 : 54892910 : && decl_linkage (decl) != lk_none
11811 : 54345234 : && DECL_LANG_SPECIFIC (decl) == NULL
11812 : 50967329 : && !DECL_EXTERN_C_P (decl)
11813 : 106102968 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
11814 : 3356 : retrofit_lang_decl (decl);
11815 : :
11816 : 55135639 : if (TREE_PUBLIC (decl))
11817 : : {
11818 : : /* [basic.link]: A name with no linkage (notably, the name of a class
11819 : : or enumeration declared in a local scope) shall not be used to
11820 : : declare an entity with linkage.
11821 : :
11822 : : DR 757 relaxes this restriction for C++0x. */
11823 : 5085020 : if (cxx_dialect < cxx11)
11824 : 20686 : no_linkage_error (decl);
11825 : : }
11826 : : else
11827 : 50050619 : DECL_INTERFACE_KNOWN (decl) = 1;
11828 : :
11829 : 55135639 : if (DECL_NAME (decl)
11830 : 55135639 : && MAIN_NAME_P (DECL_NAME (decl)))
11831 : : {
11832 : 12 : if (scope == global_namespace)
11833 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
11834 : : "cannot declare %<::main%> to be a global variable");
11835 : 6 : else if (DECL_EXTERN_C_P (decl))
11836 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
11837 : : "an entity named %<main%> cannot be declared with "
11838 : : "C language linkage");
11839 : : }
11840 : :
11841 : : /* Check if a variable is being declared as a concept. */
11842 : 55135639 : if (conceptp)
11843 : : {
11844 : 27 : if (!processing_template_decl)
11845 : 11 : error_at (declspecs->locations[ds_concept],
11846 : : "a non-template variable cannot be %<concept%>");
11847 : 16 : else if (!at_namespace_scope_p ())
11848 : 1 : error_at (declspecs->locations[ds_concept],
11849 : : "concept must be defined at namespace scope");
11850 : : else
11851 : 15 : error_at (declspecs->locations[ds_concept],
11852 : : "variable concepts are no longer supported");
11853 : 27 : return NULL_TREE;
11854 : : }
11855 : 55135612 : else if (flag_concepts
11856 : 55135612 : && current_template_depth > template_class_depth (scope))
11857 : : {
11858 : 14907538 : tree ci = current_template_constraints ();
11859 : 14907538 : set_constraints (decl, ci);
11860 : : }
11861 : :
11862 : : // Handle explicit specializations and instantiations of variable templates.
11863 : 55135612 : if (orig_declarator)
11864 : 54580816 : decl = check_explicit_specialization (orig_declarator, decl,
11865 : : template_count, 0);
11866 : :
11867 : 55135612 : return decl != error_mark_node ? decl : NULL_TREE;
11868 : : }
11869 : :
11870 : : /* Create and return a canonical pointer to member function type, for
11871 : : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
11872 : :
11873 : : tree
11874 : 3547349 : build_ptrmemfunc_type (tree type)
11875 : : {
11876 : 3547349 : tree field, fields;
11877 : 3547349 : tree t;
11878 : :
11879 : 3547349 : if (type == error_mark_node)
11880 : : return type;
11881 : :
11882 : : /* Make sure that we always have the unqualified pointer-to-member
11883 : : type first. */
11884 : 3547349 : if (cp_cv_quals quals = cp_type_quals (type))
11885 : : {
11886 : 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
11887 : 0 : return cp_build_qualified_type (unqual, quals);
11888 : : }
11889 : :
11890 : : /* If a canonical type already exists for this type, use it. We use
11891 : : this method instead of type_hash_canon, because it only does a
11892 : : simple equality check on the list of field members. */
11893 : :
11894 : 3547349 : t = TYPE_PTRMEMFUNC_TYPE (type);
11895 : 3547349 : if (t)
11896 : : return t;
11897 : :
11898 : 1126079 : t = make_node (RECORD_TYPE);
11899 : :
11900 : : /* Let the front end know this is a pointer to member function. */
11901 : 1126079 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
11902 : :
11903 : 1126079 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
11904 : 1126079 : DECL_NONADDRESSABLE_P (field) = 1;
11905 : 1126079 : fields = field;
11906 : :
11907 : 1126079 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
11908 : : delta_type_node);
11909 : 1126079 : DECL_NONADDRESSABLE_P (field) = 1;
11910 : 1126079 : DECL_CHAIN (field) = fields;
11911 : 1126079 : fields = field;
11912 : :
11913 : 1126079 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
11914 : :
11915 : : /* Zap out the name so that the back end will give us the debugging
11916 : : information for this anonymous RECORD_TYPE. */
11917 : 1126079 : TYPE_NAME (t) = NULL_TREE;
11918 : :
11919 : : /* Cache this pointer-to-member type so that we can find it again
11920 : : later. */
11921 : 1126079 : TYPE_PTRMEMFUNC_TYPE (type) = t;
11922 : :
11923 : 1126079 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
11924 : 612408 : SET_TYPE_STRUCTURAL_EQUALITY (t);
11925 : 513671 : else if (TYPE_CANONICAL (type) != type)
11926 : 332287 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
11927 : :
11928 : : return t;
11929 : : }
11930 : :
11931 : : /* Create and return a pointer to data member type. */
11932 : :
11933 : : tree
11934 : 312663 : build_ptrmem_type (tree class_type, tree member_type)
11935 : : {
11936 : 312663 : if (TREE_CODE (member_type) == METHOD_TYPE)
11937 : : {
11938 : 61615 : cp_cv_quals quals = type_memfn_quals (member_type);
11939 : 61615 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
11940 : 61615 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
11941 : 61615 : return build_ptrmemfunc_type (build_pointer_type (member_type));
11942 : : }
11943 : : else
11944 : : {
11945 : 251048 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
11946 : 251048 : return build_offset_type (class_type, member_type);
11947 : : }
11948 : : }
11949 : :
11950 : : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
11951 : : Check to see that the definition is valid. Issue appropriate error
11952 : : messages. */
11953 : :
11954 : : static void
11955 : 13018567 : check_static_variable_definition (tree decl, tree type)
11956 : : {
11957 : : /* Avoid redundant diagnostics on out-of-class definitions. */
11958 : 13018567 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
11959 : : ;
11960 : : /* Can't check yet if we don't know the type. */
11961 : 12110959 : else if (dependent_type_p (type))
11962 : : ;
11963 : : /* If DECL is declared constexpr, we'll do the appropriate checks
11964 : : in check_initializer. Similarly for inline static data members. */
11965 : 11843823 : else if (DECL_P (decl)
11966 : 11843823 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11967 : 2255808 : || DECL_VAR_DECLARED_INLINE_P (decl)))
11968 : : ;
11969 : 2255383 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11970 : : {
11971 : 52 : if (!COMPLETE_TYPE_P (type))
11972 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
11973 : : "in-class initialization of static data member %q#D of "
11974 : : "incomplete type", decl);
11975 : 41 : else if (literal_type_p (type))
11976 : 35 : permerror (DECL_SOURCE_LOCATION (decl),
11977 : : "%<constexpr%> needed for in-class initialization of "
11978 : : "static data member %q#D of non-integral type", decl);
11979 : : else
11980 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
11981 : : "in-class initialization of static data member %q#D of "
11982 : : "non-literal type", decl);
11983 : : }
11984 : : /* Motion 10 at San Diego: If a static const integral data member is
11985 : : initialized with an integral constant expression, the initializer
11986 : : may appear either in the declaration (within the class), or in
11987 : : the definition, but not both. If it appears in the class, the
11988 : : member is a member constant. The file-scope definition is always
11989 : : required. */
11990 : 2255331 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
11991 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
11992 : : "invalid in-class initialization of static data member "
11993 : : "of non-integral type %qT",
11994 : : type);
11995 : 2255320 : else if (!CP_TYPE_CONST_P (type))
11996 : 5 : error_at (DECL_SOURCE_LOCATION (decl),
11997 : : "ISO C++ forbids in-class initialization of non-const "
11998 : : "static member %qD",
11999 : : decl);
12000 : 2255315 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12001 : 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12002 : : "ISO C++ forbids initialization of member constant "
12003 : : "%qD of non-integral type %qT", decl, type);
12004 : 13018567 : }
12005 : :
12006 : : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
12007 : : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
12008 : : expressions out into temporary variables so that walk_tree doesn't
12009 : : step into them (c++/15764). */
12010 : :
12011 : : static tree
12012 : 3148 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
12013 : : {
12014 : 3148 : hash_set<tree> *pset = (hash_set<tree> *)data;
12015 : 3148 : tree expr = *expr_p;
12016 : 3148 : if (TREE_CODE (expr) == SAVE_EXPR)
12017 : : {
12018 : 1355 : tree op = TREE_OPERAND (expr, 0);
12019 : 1355 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
12020 : 1355 : if (TREE_SIDE_EFFECTS (op))
12021 : 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
12022 : 1355 : *walk_subtrees = 0;
12023 : : }
12024 : 1793 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
12025 : 1618 : *walk_subtrees = 0;
12026 : 3148 : return NULL;
12027 : : }
12028 : :
12029 : : /* Entry point for the above. */
12030 : :
12031 : : static void
12032 : 1414 : stabilize_vla_size (tree size)
12033 : : {
12034 : 1414 : hash_set<tree> pset;
12035 : : /* Break out any function calls into temporary variables. */
12036 : 1414 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12037 : 1414 : }
12038 : :
12039 : : /* Reduce a SIZEOF_EXPR to its value. */
12040 : :
12041 : : tree
12042 : 9346818 : fold_sizeof_expr (tree t)
12043 : : {
12044 : 9346818 : tree r;
12045 : 9346818 : if (SIZEOF_EXPR_TYPE_P (t))
12046 : 8647726 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12047 : 8647726 : TREE_TYPE (TREE_OPERAND (t, 0)),
12048 : : SIZEOF_EXPR, false, false);
12049 : 699092 : else if (TYPE_P (TREE_OPERAND (t, 0)))
12050 : 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12051 : 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12052 : : false, false);
12053 : : else
12054 : 699092 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12055 : 699092 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12056 : : false, false);
12057 : 9346818 : if (r == error_mark_node)
12058 : 63 : r = size_one_node;
12059 : 9346818 : r = cp_fold_convert (size_type_node, r);
12060 : 9346818 : return r;
12061 : : }
12062 : :
12063 : : /* Given the SIZE (i.e., number of elements) in an array, compute
12064 : : an appropriate index type for the array. If non-NULL, NAME is
12065 : : the name of the entity being declared. */
12066 : :
12067 : : static tree
12068 : 2677873 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12069 : : tsubst_flags_t complain)
12070 : : {
12071 : 2677873 : if (error_operand_p (size))
12072 : 37 : return error_mark_node;
12073 : :
12074 : : /* The type of the index being computed. */
12075 : 2677836 : tree itype;
12076 : :
12077 : : /* The original numeric size as seen in the source code before
12078 : : conversion to size_t. */
12079 : 2677836 : tree origsize = size;
12080 : :
12081 : 2677836 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
12082 : :
12083 : 2677836 : if (!type_dependent_expression_p (size))
12084 : : {
12085 : 2639447 : origsize = size = mark_rvalue_use (size);
12086 : :
12087 : 33924 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
12088 : 2639603 : && TREE_SIDE_EFFECTS (size))
12089 : : /* In C++98, we mark a non-constant array bound with a magic
12090 : : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
12091 : : else
12092 : : {
12093 : 2639446 : size = build_converted_constant_expr (size_type_node, size, complain);
12094 : : /* Pedantically a constant expression is required here and so
12095 : : __builtin_is_constant_evaluated () should fold to true if it
12096 : : is successfully folded into a constant. */
12097 : 2639446 : size = fold_non_dependent_expr (size, complain,
12098 : : /*manifestly_const_eval=*/true);
12099 : :
12100 : 2639446 : if (!TREE_CONSTANT (size))
12101 : 100839 : size = origsize;
12102 : : }
12103 : :
12104 : 2639447 : if (error_operand_p (size))
12105 : 3 : return error_mark_node;
12106 : :
12107 : : /* The array bound must be an integer type. */
12108 : 2639444 : tree type = TREE_TYPE (size);
12109 : 2639444 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
12110 : : {
12111 : 21 : if (!(complain & tf_error))
12112 : 0 : return error_mark_node;
12113 : 21 : if (name)
12114 : 18 : error_at (loc, "size of array %qD has non-integral type %qT",
12115 : : name, type);
12116 : : else
12117 : 3 : error_at (loc, "size of array has non-integral type %qT", type);
12118 : 21 : size = integer_one_node;
12119 : : }
12120 : : }
12121 : :
12122 : : /* A type is dependent if it is...an array type constructed from any
12123 : : dependent type or whose size is specified by a constant expression
12124 : : that is value-dependent. */
12125 : : /* We can only call value_dependent_expression_p on integral constant
12126 : : expressions. */
12127 : 2677833 : if (processing_template_decl
12128 : 1128706 : && potential_constant_expression (size)
12129 : 3806514 : && value_dependent_expression_p (size))
12130 : : {
12131 : : /* Just build the index type and mark that it requires
12132 : : structural equality checks. */
12133 : 643356 : in_template:
12134 : 643356 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
12135 : : size, size_one_node));
12136 : 643356 : TYPE_DEPENDENT_P (itype) = 1;
12137 : 643356 : TYPE_DEPENDENT_P_VALID (itype) = 1;
12138 : 643356 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
12139 : 643356 : return itype;
12140 : : }
12141 : :
12142 : 2034586 : if (TREE_CODE (size) != INTEGER_CST)
12143 : : {
12144 : 1922 : tree folded = cp_fully_fold (size);
12145 : 1922 : if (TREE_CODE (folded) == INTEGER_CST)
12146 : : {
12147 : 210 : if (name)
12148 : 136 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
12149 : : "integral constant-expression", name);
12150 : : else
12151 : 74 : pedwarn (loc, OPT_Wpedantic,
12152 : : "size of array is not an integral constant-expression");
12153 : : }
12154 : 1922 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
12155 : : /* We might have lost the TREE_CONSTANT flag e.g. when we are
12156 : : folding a conversion from a pointer to integral type. In that
12157 : : case issue an error below and don't treat this as a VLA. */;
12158 : : else
12159 : : /* Use the folded result for VLAs, too; it will have resolved
12160 : : SIZEOF_EXPR. */
12161 : : size = folded;
12162 : : }
12163 : :
12164 : : /* Normally, the array-bound will be a constant. */
12165 : 2034586 : if (TREE_CODE (size) == INTEGER_CST)
12166 : : {
12167 : : /* The size to use in diagnostics that reflects the constant
12168 : : size used in the source, rather than SIZE massaged above. */
12169 : 2032874 : tree diagsize = size;
12170 : :
12171 : : /* If the original size before conversion to size_t was signed
12172 : : and negative, convert it to ssizetype to restore the sign. */
12173 : 2032874 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
12174 : : && TREE_CODE (size) == INTEGER_CST
12175 : 2032874 : && tree_int_cst_sign_bit (size))
12176 : : {
12177 : 229 : diagsize = fold_convert (ssizetype, size);
12178 : :
12179 : : /* Clear the overflow bit that may have been set as a result
12180 : : of the conversion from the sizetype of the new size to
12181 : : ssizetype. */
12182 : 229 : TREE_OVERFLOW (diagsize) = false;
12183 : : }
12184 : :
12185 : : /* Verify that the array has a positive number of elements
12186 : : and issue the appropriate diagnostic if it doesn't. */
12187 : 2032874 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
12188 : : {
12189 : 460 : if (!(complain & tf_error))
12190 : 15 : return error_mark_node;
12191 : 445 : size = integer_one_node;
12192 : : }
12193 : : /* As an extension we allow zero-sized arrays. */
12194 : 2032414 : else if (integer_zerop (size))
12195 : : {
12196 : 3143 : if (!(complain & tf_error))
12197 : : /* We must fail if performing argument deduction (as
12198 : : indicated by the state of complain), so that
12199 : : another substitution can be found. */
12200 : 8 : return error_mark_node;
12201 : 3135 : else if (name)
12202 : 2871 : pedwarn (loc, OPT_Wpedantic,
12203 : : "ISO C++ forbids zero-size array %qD", name);
12204 : : else
12205 : 264 : pedwarn (loc, OPT_Wpedantic,
12206 : : "ISO C++ forbids zero-size array");
12207 : : }
12208 : : }
12209 : 1712 : else if (TREE_CONSTANT (size)
12210 : : /* We don't allow VLAs at non-function scopes, or during
12211 : : tentative template substitution. */
12212 : 1697 : || !at_function_scope_p ()
12213 : 3241 : || !(complain & tf_error))
12214 : : {
12215 : 189 : if (!(complain & tf_error))
12216 : 144 : return error_mark_node;
12217 : : /* `(int) &fn' is not a valid array bound. */
12218 : 45 : if (name)
12219 : 37 : error_at (loc,
12220 : : "size of array %qD is not an integral constant-expression",
12221 : : name);
12222 : : else
12223 : 8 : error_at (loc, "size of array is not an integral constant-expression");
12224 : 45 : size = integer_one_node;
12225 : : }
12226 : 1523 : else if (pedantic && warn_vla != 0)
12227 : : {
12228 : 30 : if (name)
12229 : 15 : pedwarn (name_loc, OPT_Wvla,
12230 : : "ISO C++ forbids variable length array %qD", name);
12231 : : else
12232 : 15 : pedwarn (input_location, OPT_Wvla,
12233 : : "ISO C++ forbids variable length array");
12234 : : }
12235 : 1493 : else if (warn_vla > 0)
12236 : : {
12237 : 3 : if (name)
12238 : 3 : warning_at (name_loc, OPT_Wvla,
12239 : : "variable length array %qD is used", name);
12240 : : else
12241 : 0 : warning (OPT_Wvla,
12242 : : "variable length array is used");
12243 : : }
12244 : :
12245 : 2034419 : if (processing_template_decl && !TREE_CONSTANT (size))
12246 : 109 : goto in_template;
12247 : : else
12248 : : {
12249 : 2034310 : if (!TREE_CONSTANT (size))
12250 : : {
12251 : : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
12252 : : of the MINUS_EXPR, which allows the -1 to get folded with the +1
12253 : : that happens when building TYPE_SIZE. */
12254 : 1414 : size = variable_size (size);
12255 : 1414 : stabilize_vla_size (size);
12256 : : }
12257 : :
12258 : : /* Compute the index of the largest element in the array. It is
12259 : : one less than the number of elements in the array. We save
12260 : : and restore PROCESSING_TEMPLATE_DECL so that computations in
12261 : : cp_build_binary_op will be appropriately folded. */
12262 : 2034310 : {
12263 : 2034310 : processing_template_decl_sentinel s;
12264 : 2034310 : itype = cp_build_binary_op (input_location,
12265 : : MINUS_EXPR,
12266 : : cp_convert (ssizetype, size, complain),
12267 : : cp_convert (ssizetype, integer_one_node,
12268 : : complain),
12269 : : complain);
12270 : 2034310 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
12271 : 2034310 : }
12272 : :
12273 : 2034310 : if (!TREE_CONSTANT (itype))
12274 : : {
12275 : 1414 : if (sanitize_flags_p (SANITIZE_VLA)
12276 : 1414 : && current_function_decl != NULL_TREE)
12277 : : {
12278 : : /* We have to add 1 -- in the ubsan routine we generate
12279 : : LE_EXPR rather than LT_EXPR. */
12280 : 69 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
12281 : : build_one_cst (TREE_TYPE (itype)));
12282 : 69 : t = ubsan_instrument_vla (input_location, t);
12283 : 69 : finish_expr_stmt (t);
12284 : : }
12285 : : }
12286 : : /* Make sure that there was no overflow when creating to a signed
12287 : : index type. (For example, on a 32-bit machine, an array with
12288 : : size 2^32 - 1 is too big.) */
12289 : 2032896 : else if (TREE_CODE (itype) == INTEGER_CST
12290 : 2032896 : && TREE_OVERFLOW (itype))
12291 : : {
12292 : 0 : if (!(complain & tf_error))
12293 : 0 : return error_mark_node;
12294 : 0 : error ("overflow in array dimension");
12295 : 0 : TREE_OVERFLOW (itype) = 0;
12296 : : }
12297 : : }
12298 : :
12299 : : /* Create and return the appropriate index type. */
12300 : 2034310 : itype = build_index_type (itype);
12301 : :
12302 : : /* If the index type were dependent, we would have returned early, so
12303 : : remember that it isn't. */
12304 : 2034310 : TYPE_DEPENDENT_P (itype) = 0;
12305 : 2034310 : TYPE_DEPENDENT_P_VALID (itype) = 1;
12306 : 2034310 : return itype;
12307 : : }
12308 : :
12309 : : tree
12310 : 314121 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
12311 : : {
12312 : 314121 : return compute_array_index_type_loc (input_location, name, size, complain);
12313 : : }
12314 : :
12315 : : /* Returns the scope (if any) in which the entity declared by
12316 : : DECLARATOR will be located. If the entity was declared with an
12317 : : unqualified name, NULL_TREE is returned. */
12318 : :
12319 : : tree
12320 : 667416681 : get_scope_of_declarator (const cp_declarator *declarator)
12321 : : {
12322 : 778869876 : while (declarator && declarator->kind != cdk_id)
12323 : 111453195 : declarator = declarator->declarator;
12324 : :
12325 : : /* If the declarator-id is a SCOPE_REF, the scope in which the
12326 : : declaration occurs is the first operand. */
12327 : 667416681 : if (declarator
12328 : 666153293 : && declarator->u.id.qualifying_scope)
12329 : : return declarator->u.id.qualifying_scope;
12330 : :
12331 : : /* Otherwise, the declarator is not a qualified name; the entity will
12332 : : be declared in the current scope. */
12333 : : return NULL_TREE;
12334 : : }
12335 : :
12336 : : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
12337 : : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
12338 : : with this type. */
12339 : :
12340 : : static tree
12341 : 3063865 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
12342 : : {
12343 : 3063865 : tree itype = NULL_TREE;
12344 : :
12345 : : /* If things have already gone awry, bail now. */
12346 : 3063865 : if (type == error_mark_node || size == error_mark_node)
12347 : : return error_mark_node;
12348 : :
12349 : : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
12350 : : for a deduced class type. */
12351 : 3063802 : if (template_placeholder_p (type))
12352 : : {
12353 : 6 : if (name)
12354 : 0 : error_at (loc, "%qD declared as array of template placeholder "
12355 : : "type %qT", name, type);
12356 : : else
12357 : 6 : error ("creating array of template placeholder type %qT", type);
12358 : 6 : return error_mark_node;
12359 : : }
12360 : :
12361 : : /* If there are some types which cannot be array elements,
12362 : : issue an error-message and return. */
12363 : 3063796 : switch (TREE_CODE (type))
12364 : : {
12365 : 3 : case VOID_TYPE:
12366 : 3 : if (name)
12367 : 0 : error_at (loc, "declaration of %qD as array of void", name);
12368 : : else
12369 : 3 : error ("creating array of void");
12370 : 3 : return error_mark_node;
12371 : :
12372 : 6 : case FUNCTION_TYPE:
12373 : 6 : if (name)
12374 : 6 : error_at (loc, "declaration of %qD as array of functions", name);
12375 : : else
12376 : 0 : error ("creating array of functions");
12377 : 6 : return error_mark_node;
12378 : :
12379 : 21 : case REFERENCE_TYPE:
12380 : 21 : if (name)
12381 : 15 : error_at (loc, "declaration of %qD as array of references", name);
12382 : : else
12383 : 6 : error ("creating array of references");
12384 : 21 : return error_mark_node;
12385 : :
12386 : 0 : case METHOD_TYPE:
12387 : 0 : if (name)
12388 : 0 : error_at (loc, "declaration of %qD as array of function members",
12389 : : name);
12390 : : else
12391 : 0 : error ("creating array of function members");
12392 : 0 : return error_mark_node;
12393 : :
12394 : 3063766 : default:
12395 : 3063766 : break;
12396 : : }
12397 : :
12398 : 3063766 : if (!verify_type_context (name ? loc : input_location,
12399 : : TCTX_ARRAY_ELEMENT, type))
12400 : 0 : return error_mark_node;
12401 : :
12402 : : /* [dcl.array]
12403 : :
12404 : : The constant expressions that specify the bounds of the arrays
12405 : : can be omitted only for the first member of the sequence. */
12406 : 3063766 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12407 : : {
12408 : 30 : if (name)
12409 : 24 : error_at (loc, "declaration of %qD as multidimensional array must "
12410 : : "have bounds for all dimensions except the first",
12411 : : name);
12412 : : else
12413 : 6 : error ("multidimensional array must have bounds for all "
12414 : : "dimensions except the first");
12415 : :
12416 : 30 : return error_mark_node;
12417 : : }
12418 : :
12419 : : /* Figure out the index type for the array. */
12420 : 3063736 : if (size)
12421 : : {
12422 : 2363752 : itype = compute_array_index_type_loc (loc, name, size,
12423 : : tf_warning_or_error);
12424 : 2363752 : if (type_uses_auto (type)
12425 : 2363752 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
12426 : : {
12427 : 9 : sorry_at (loc, "variable-length array of %<auto%>");
12428 : 9 : return error_mark_node;
12429 : : }
12430 : : }
12431 : :
12432 : 3063727 : return build_cplus_array_type (type, itype);
12433 : : }
12434 : :
12435 : : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
12436 : :
12437 : : static location_t
12438 : 899620087 : min_location (location_t loca, location_t locb)
12439 : : {
12440 : 899620087 : if (loca == UNKNOWN_LOCATION
12441 : 899620087 : || (locb != UNKNOWN_LOCATION
12442 : 100137533 : && linemap_location_before_p (line_table, locb, loca)))
12443 : 800901212 : return locb;
12444 : : return loca;
12445 : : }
12446 : :
12447 : : /* Returns the smallest location != UNKNOWN_LOCATION among the
12448 : : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
12449 : : and LOCATIONS[ds_restrict]. */
12450 : :
12451 : : static location_t
12452 : 898066174 : smallest_type_quals_location (int type_quals, const location_t* locations)
12453 : : {
12454 : 898066174 : location_t loc = UNKNOWN_LOCATION;
12455 : :
12456 : 898066174 : if (type_quals & TYPE_QUAL_CONST)
12457 : 98766813 : loc = locations[ds_const];
12458 : :
12459 : 898066174 : if (type_quals & TYPE_QUAL_VOLATILE)
12460 : 1553822 : loc = min_location (loc, locations[ds_volatile]);
12461 : :
12462 : 898066174 : if (type_quals & TYPE_QUAL_RESTRICT)
12463 : 43 : loc = min_location (loc, locations[ds_restrict]);
12464 : :
12465 : 898066174 : return loc;
12466 : : }
12467 : :
12468 : : /* Returns the smallest among the latter and locations[ds_type_spec]. */
12469 : :
12470 : : static location_t
12471 : 898066153 : smallest_type_location (int type_quals, const location_t* locations)
12472 : : {
12473 : 898066153 : location_t loc = smallest_type_quals_location (type_quals, locations);
12474 : 898066153 : return min_location (loc, locations[ds_type_spec]);
12475 : : }
12476 : :
12477 : : static location_t
12478 : 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
12479 : : {
12480 : 26 : int type_quals = get_type_quals (declspecs);
12481 : 26 : return smallest_type_location (type_quals, declspecs->locations);
12482 : : }
12483 : :
12484 : : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
12485 : : strip out the pointer/reference declarator(s). */
12486 : :
12487 : : static bool
12488 : 26693009 : maybe_strip_indirect_ref (const cp_declarator** declarator)
12489 : : {
12490 : 26693009 : bool indirect_ref_p = false;
12491 : 53386165 : while (declarator && *declarator
12492 : 26693156 : && ((*declarator)->kind == cdk_pointer
12493 : 26693156 : || (*declarator)->kind == cdk_reference))
12494 : : {
12495 : 147 : indirect_ref_p = true;
12496 : 147 : *declarator = (*declarator)->declarator;
12497 : : }
12498 : 26693009 : return indirect_ref_p;
12499 : : }
12500 : :
12501 : : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
12502 : : and DECLARATOR. SFK indicates the kind of special function (if any) that
12503 : : this function is. OPTYPE is the type given in a conversion operator
12504 : : declaration, or the class type for a constructor/destructor.
12505 : : Returns the actual return type of the function; that may be different
12506 : : than TYPE if an error occurs, or for certain special functions. */
12507 : :
12508 : : static tree
12509 : 26693009 : check_special_function_return_type (special_function_kind sfk,
12510 : : tree type,
12511 : : tree optype,
12512 : : int type_quals,
12513 : : const cp_declarator** declarator,
12514 : : const location_t* locations)
12515 : : {
12516 : 26693009 : gcc_assert (declarator);
12517 : 26693009 : location_t rettype_loc = (type
12518 : 26693009 : ? smallest_type_location (type_quals, locations)
12519 : 26692928 : : (*declarator)->id_loc);
12520 : 26693009 : switch (sfk)
12521 : : {
12522 : 21937934 : case sfk_constructor:
12523 : 21937934 : if (maybe_strip_indirect_ref (declarator) || type)
12524 : 81 : error_at (rettype_loc,
12525 : : "return type specification for constructor invalid");
12526 : 21937853 : else if (type_quals != TYPE_UNQUALIFIED)
12527 : 15 : error_at (smallest_type_quals_location (type_quals, locations),
12528 : : "qualifiers are not allowed on constructor declaration");
12529 : :
12530 : 21937934 : if (targetm.cxx.cdtor_returns_this ())
12531 : 0 : type = build_pointer_type (optype);
12532 : : else
12533 : 21937934 : type = void_type_node;
12534 : : break;
12535 : :
12536 : 3095691 : case sfk_destructor:
12537 : 3095691 : if (maybe_strip_indirect_ref (declarator) || type)
12538 : 45 : error_at (rettype_loc,
12539 : : "return type specification for destructor invalid");
12540 : 3095646 : else if (type_quals != TYPE_UNQUALIFIED)
12541 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
12542 : : "qualifiers are not allowed on destructor declaration");
12543 : :
12544 : : /* We can't use the proper return type here because we run into
12545 : : problems with ambiguous bases and covariant returns. */
12546 : 3095691 : if (targetm.cxx.cdtor_returns_this ())
12547 : 0 : type = build_pointer_type (void_type_node);
12548 : : else
12549 : 3095691 : type = void_type_node;
12550 : : break;
12551 : :
12552 : 620840 : case sfk_conversion:
12553 : 620840 : if (maybe_strip_indirect_ref (declarator) || type)
12554 : 42 : error_at (rettype_loc,
12555 : : "return type specified for %<operator %T%>", optype);
12556 : 620798 : else if (type_quals != TYPE_UNQUALIFIED)
12557 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
12558 : : "qualifiers are not allowed on declaration of "
12559 : : "%<operator %T%>", optype);
12560 : :
12561 : : type = optype;
12562 : : break;
12563 : :
12564 : 1038544 : case sfk_deduction_guide:
12565 : 1038544 : if (maybe_strip_indirect_ref (declarator) || type)
12566 : 0 : error_at (rettype_loc,
12567 : : "return type specified for deduction guide");
12568 : 1038544 : else if (type_quals != TYPE_UNQUALIFIED)
12569 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
12570 : : "qualifiers are not allowed on declaration of "
12571 : : "deduction guide");
12572 : 1038544 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
12573 : : {
12574 : 3 : error ("template template parameter %qT in declaration of "
12575 : : "deduction guide", optype);
12576 : 3 : type = error_mark_node;
12577 : : }
12578 : : else
12579 : 1038541 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
12580 : 28040688 : for (int i = 0; i < ds_last; ++i)
12581 : 27002144 : if (i != ds_explicit && locations[i])
12582 : 3 : error_at (locations[i],
12583 : : "%<decl-specifier%> in declaration of deduction guide");
12584 : : break;
12585 : :
12586 : 0 : default:
12587 : 0 : gcc_unreachable ();
12588 : : }
12589 : :
12590 : 26693009 : return type;
12591 : : }
12592 : :
12593 : : /* A variable or data member (whose unqualified name is IDENTIFIER)
12594 : : has been declared with the indicated TYPE. If the TYPE is not
12595 : : acceptable, issue an error message and return a type to use for
12596 : : error-recovery purposes. */
12597 : :
12598 : : tree
12599 : 277706201 : check_var_type (tree identifier, tree type, location_t loc)
12600 : : {
12601 : 277706201 : if (VOID_TYPE_P (type))
12602 : : {
12603 : 88 : if (!identifier)
12604 : 0 : error_at (loc, "unnamed variable or field declared void");
12605 : 88 : else if (identifier_p (identifier))
12606 : : {
12607 : 88 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
12608 : 88 : error_at (loc, "variable or field %qE declared void",
12609 : : identifier);
12610 : : }
12611 : : else
12612 : 0 : error_at (loc, "variable or field declared void");
12613 : 88 : type = error_mark_node;
12614 : : }
12615 : :
12616 : 277706201 : return type;
12617 : : }
12618 : :
12619 : : /* Handle declaring DECL as an inline variable. */
12620 : :
12621 : : static void
12622 : 3861045 : mark_inline_variable (tree decl, location_t loc)
12623 : : {
12624 : 3861045 : bool inlinep = true;
12625 : 3861045 : if (! toplevel_bindings_p ())
12626 : : {
12627 : 6 : error_at (loc, "%<inline%> specifier invalid for variable "
12628 : : "%qD declared at block scope", decl);
12629 : 6 : inlinep = false;
12630 : : }
12631 : 3861039 : else if (cxx_dialect < cxx17)
12632 : 644 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
12633 : : "with %<-std=c++17%> or %<-std=gnu++17%>");
12634 : 650 : if (inlinep)
12635 : : {
12636 : 3861039 : retrofit_lang_decl (decl);
12637 : 3861039 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
12638 : : }
12639 : 3861045 : }
12640 : :
12641 : :
12642 : : /* Assign a typedef-given name to a class or enumeration type declared
12643 : : as anonymous at first. This was split out of grokdeclarator
12644 : : because it is also used in libcc1. */
12645 : :
12646 : : void
12647 : 384403 : name_unnamed_type (tree type, tree decl)
12648 : : {
12649 : 1153209 : gcc_assert (TYPE_UNNAMED_P (type));
12650 : :
12651 : : /* Replace the anonymous decl with the real decl. Be careful not to
12652 : : rename other typedefs (such as the self-reference) of type. */
12653 : 384403 : tree orig = TYPE_NAME (type);
12654 : 1151646 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12655 : 767243 : if (TYPE_NAME (t) == orig)
12656 : 384403 : TYPE_NAME (t) = decl;
12657 : :
12658 : : /* If this is a typedef within a template class, the nested
12659 : : type is a (non-primary) template. The name for the
12660 : : template needs updating as well. */
12661 : 384403 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
12662 : 64 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
12663 : :
12664 : : /* Adjust linkage now that we aren't unnamed anymore. */
12665 : 384403 : reset_type_linkage (type);
12666 : :
12667 : : /* FIXME remangle member functions; member functions of a
12668 : : type with external linkage have external linkage. */
12669 : :
12670 : : /* Check that our job is done, and that it would fail if we
12671 : : attempted to do it again. */
12672 : 768806 : gcc_assert (!TYPE_UNNAMED_P (type));
12673 : 384403 : }
12674 : :
12675 : : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
12676 : : is allowed. TYPE might contain a decltype(auto). Returns true if
12677 : : there was a problem, false otherwise. */
12678 : :
12679 : : static bool
12680 : 565353823 : check_decltype_auto (location_t loc, tree type)
12681 : : {
12682 : 565353823 : if (tree a = type_uses_auto (type))
12683 : : {
12684 : 9380841 : if (AUTO_IS_DECLTYPE (a))
12685 : : {
12686 : 319323 : if (a != type)
12687 : : {
12688 : 33 : error_at (loc, "%qT as type rather than plain "
12689 : : "%<decltype(auto)%>", type);
12690 : 33 : return true;
12691 : : }
12692 : 319290 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12693 : : {
12694 : 35 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
12695 : 35 : return true;
12696 : : }
12697 : : }
12698 : : }
12699 : : return false;
12700 : : }
12701 : :
12702 : : /* Issue an error about two mutually incompatible declspecs
12703 : : with the given names and locations
12704 : : e.g. "error: `signed' and `unsigned' specified together" */
12705 : :
12706 : : static void
12707 : 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
12708 : : const char *name_b, location_t loc_b)
12709 : : {
12710 : 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
12711 : 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
12712 : : nullptr, highlight_colors::rhs);
12713 : 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
12714 : 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
12715 : 66 : error_at (&richloc, "%e and %e specified together",
12716 : : &e_name_a, &e_name_b);
12717 : 66 : }
12718 : :
12719 : : /* Given declspecs and a declarator (abstract or otherwise), determine
12720 : : the name and type of the object declared and construct a DECL node
12721 : : for it.
12722 : :
12723 : : DECLSPECS points to the representation of declaration-specifier
12724 : : sequence that precedes declarator.
12725 : :
12726 : : DECL_CONTEXT says which syntactic context this declaration is in:
12727 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
12728 : : FUNCDEF for a function definition. Like NORMAL but a few different
12729 : : error messages in each case. Return value may be zero meaning
12730 : : this definition is too screwy to try to parse.
12731 : : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
12732 : : handle member functions (which have FIELD context).
12733 : : Return value may be zero meaning this definition is too screwy to
12734 : : try to parse.
12735 : : PARM for a parameter declaration (either within a function prototype
12736 : : or before a function body). Make a PARM_DECL, or return void_type_node.
12737 : : TPARM for a template parameter declaration.
12738 : : CATCHPARM for a parameter declaration before a catch clause.
12739 : : TYPENAME if for a typename (in a cast or sizeof).
12740 : : Don't make a DECL node; just return the ..._TYPE node.
12741 : : FIELD for a struct or union field; make a FIELD_DECL.
12742 : : BITFIELD for a field with specified width.
12743 : :
12744 : : INITIALIZED is as for start_decl.
12745 : :
12746 : : ATTRLIST is a pointer to the list of attributes, which may be NULL
12747 : : if there are none; *ATTRLIST may be modified if attributes from inside
12748 : : the declarator should be applied to the declaration.
12749 : :
12750 : : When this function is called, scoping variables (such as
12751 : : CURRENT_CLASS_TYPE) should reflect the scope in which the
12752 : : declaration occurs, not the scope in which the new declaration will
12753 : : be placed. For example, on:
12754 : :
12755 : : void S::f() { ... }
12756 : :
12757 : : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
12758 : : should not be `S'.
12759 : :
12760 : : Returns a DECL (if a declarator is present), a TYPE (if there is no
12761 : : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
12762 : : error occurs. */
12763 : :
12764 : : tree
12765 : 898066046 : grokdeclarator (const cp_declarator *declarator,
12766 : : cp_decl_specifier_seq *declspecs,
12767 : : enum decl_context decl_context,
12768 : : int initialized,
12769 : : tree* attrlist)
12770 : : {
12771 : 898066046 : tree type = NULL_TREE;
12772 : 898066046 : int longlong = 0;
12773 : 898066046 : int explicit_intN = 0;
12774 : 898066046 : int int_n_alt = 0;
12775 : 898066046 : int virtualp, explicitp, friendp, inlinep, staticp;
12776 : 898066046 : int explicit_int = 0;
12777 : 898066046 : int explicit_char = 0;
12778 : 898066046 : int defaulted_int = 0;
12779 : :
12780 : 898066046 : tree typedef_decl = NULL_TREE;
12781 : 898066046 : const char *name = NULL;
12782 : 898066046 : tree typedef_type = NULL_TREE;
12783 : : /* True if this declarator is a function definition. */
12784 : 898066046 : bool funcdef_flag = false;
12785 : 898066046 : cp_declarator_kind innermost_code = cdk_error;
12786 : 898066046 : int bitfield = 0;
12787 : : #if 0
12788 : : /* See the code below that used this. */
12789 : : tree decl_attr = NULL_TREE;
12790 : : #endif
12791 : :
12792 : : /* Keep track of what sort of function is being processed
12793 : : so that we can warn about default return values, or explicit
12794 : : return values which do not match prescribed defaults. */
12795 : 898066046 : special_function_kind sfk = sfk_none;
12796 : :
12797 : 898066046 : tree dname = NULL_TREE;
12798 : 898066046 : tree ctor_return_type = NULL_TREE;
12799 : 898066046 : enum overload_flags flags = NO_SPECIAL;
12800 : : /* cv-qualifiers that apply to the declarator, for a declaration of
12801 : : a member function. */
12802 : 898066046 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
12803 : : /* virt-specifiers that apply to the declarator, for a declaration of
12804 : : a member function. */
12805 : 898066046 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
12806 : : /* ref-qualifier that applies to the declarator, for a declaration of
12807 : : a member function. */
12808 : 898066046 : cp_ref_qualifier rqual = REF_QUAL_NONE;
12809 : : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
12810 : 898066046 : int type_quals = get_type_quals (declspecs);
12811 : 898066046 : tree raises = NULL_TREE;
12812 : 898066046 : int template_count = 0;
12813 : 898066046 : tree returned_attrs = NULL_TREE;
12814 : 898066046 : tree parms = NULL_TREE;
12815 : 898066046 : const cp_declarator *id_declarator;
12816 : : /* The unqualified name of the declarator; either an
12817 : : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
12818 : 898066046 : tree unqualified_id;
12819 : : /* The class type, if any, in which this entity is located,
12820 : : or NULL_TREE if none. Note that this value may be different from
12821 : : the current class type; for example if an attempt is made to declare
12822 : : "A::f" inside "B", this value will be "A". */
12823 : 898066046 : tree ctype = current_class_type;
12824 : : /* The NAMESPACE_DECL for the namespace in which this entity is
12825 : : located. If an unqualified name is used to declare the entity,
12826 : : this value will be NULL_TREE, even if the entity is located at
12827 : : namespace scope. */
12828 : 898066046 : tree in_namespace = NULL_TREE;
12829 : 898066046 : cp_storage_class storage_class;
12830 : 898066046 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
12831 : 898066046 : bool type_was_error_mark_node = false;
12832 : 898066046 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
12833 : 898066046 : bool template_type_arg = false;
12834 : 898066046 : bool template_parm_flag = false;
12835 : 898066046 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
12836 : 898066046 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
12837 : 898066046 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
12838 : 898066046 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
12839 : 898066046 : bool late_return_type_p = false;
12840 : 898066046 : bool array_parameter_p = false;
12841 : 898066046 : tree reqs = NULL_TREE;
12842 : :
12843 : 898066046 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
12844 : 898066046 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
12845 : 898066046 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
12846 : 898066046 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
12847 : 898066046 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
12848 : 898066046 : explicit_intN = declspecs->explicit_intN_p;
12849 : 898066046 : int_n_alt = declspecs->int_n_alt;
12850 : 898066046 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
12851 : :
12852 : : // Was concept_p specified? Note that ds_concept
12853 : : // implies ds_constexpr!
12854 : 898066046 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
12855 : 898066046 : if (concept_p)
12856 : 114 : constexpr_p = true;
12857 : :
12858 : 898066046 : if (decl_context == FUNCDEF)
12859 : : funcdef_flag = true, decl_context = NORMAL;
12860 : : else if (decl_context == MEMFUNCDEF)
12861 : : funcdef_flag = true, decl_context = FIELD;
12862 : : else if (decl_context == BITFIELD)
12863 : : bitfield = 1, decl_context = FIELD;
12864 : : else if (decl_context == TEMPLATE_TYPE_ARG)
12865 : : template_type_arg = true, decl_context = TYPENAME;
12866 : : else if (decl_context == TPARM)
12867 : 11733647 : template_parm_flag = true, decl_context = PARM;
12868 : :
12869 : 898066046 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
12870 : 9138129 : funcdef_flag = true;
12871 : :
12872 : 898066046 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
12873 : : (type_quals,
12874 : 898066046 : declspecs->locations));
12875 : 898066046 : location_t id_loc;
12876 : 898066046 : location_t init_loc;
12877 : 898066046 : if (declarator)
12878 : : {
12879 : 555519948 : id_loc = loc_or_input_loc (declarator->id_loc);
12880 : 555519948 : init_loc = loc_or_input_loc (declarator->init_loc);
12881 : : }
12882 : : else
12883 : 342546098 : init_loc = id_loc = input_location;
12884 : :
12885 : : /* Look inside a declarator for the name being declared
12886 : : and get it as a string, for an error message. */
12887 : 898066046 : for (id_declarator = declarator;
12888 : 1248612962 : id_declarator;
12889 : 350546916 : id_declarator = id_declarator->declarator)
12890 : : {
12891 : 870256673 : if (id_declarator->kind != cdk_id)
12892 : 350546941 : innermost_code = id_declarator->kind;
12893 : :
12894 : 870256673 : switch (id_declarator->kind)
12895 : : {
12896 : 171165849 : case cdk_function:
12897 : 171165849 : if (id_declarator->declarator
12898 : 170688230 : && id_declarator->declarator->kind == cdk_id)
12899 : : {
12900 : 168775587 : sfk = id_declarator->declarator->u.id.sfk;
12901 : 168775587 : if (sfk == sfk_destructor)
12902 : 3095694 : flags = DTOR_FLAG;
12903 : : }
12904 : : break;
12905 : :
12906 : 519709732 : case cdk_id:
12907 : 519709732 : {
12908 : 519709732 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
12909 : 519709732 : tree decl = id_declarator->u.id.unqualified_name;
12910 : 519709732 : if (!decl)
12911 : : break;
12912 : 519709732 : if (qualifying_scope)
12913 : : {
12914 : 9534506 : if (check_for_bare_parameter_packs (qualifying_scope,
12915 : 9534506 : id_declarator->id_loc))
12916 : 6 : return error_mark_node;
12917 : 9534500 : if (at_function_scope_p ())
12918 : : {
12919 : : /* [dcl.meaning]
12920 : :
12921 : : A declarator-id shall not be qualified except
12922 : : for ...
12923 : :
12924 : : None of the cases are permitted in block
12925 : : scope. */
12926 : 0 : if (qualifying_scope == global_namespace)
12927 : 0 : error ("invalid use of qualified-name %<::%D%>",
12928 : : decl);
12929 : 0 : else if (TYPE_P (qualifying_scope))
12930 : 0 : error ("invalid use of qualified-name %<%T::%D%>",
12931 : : qualifying_scope, decl);
12932 : : else
12933 : 0 : error ("invalid use of qualified-name %<%D::%D%>",
12934 : : qualifying_scope, decl);
12935 : 0 : return error_mark_node;
12936 : : }
12937 : 9534500 : else if (TYPE_P (qualifying_scope))
12938 : : {
12939 : 9385013 : ctype = qualifying_scope;
12940 : 9385013 : if (!MAYBE_CLASS_TYPE_P (ctype))
12941 : : {
12942 : 6 : error_at (id_declarator->id_loc,
12943 : : "%q#T is not a class or namespace", ctype);
12944 : 6 : ctype = NULL_TREE;
12945 : : }
12946 : 9385007 : else if (innermost_code != cdk_function
12947 : 381292 : && current_class_type
12948 : 9385022 : && !uniquely_derived_from_p (ctype,
12949 : : current_class_type))
12950 : : {
12951 : 9 : error_at (id_declarator->id_loc,
12952 : : "invalid use of qualified-name %<%T::%D%>",
12953 : : qualifying_scope, decl);
12954 : 9 : return error_mark_node;
12955 : : }
12956 : : }
12957 : 149487 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
12958 : 519709717 : in_namespace = qualifying_scope;
12959 : : }
12960 : 519709717 : switch (TREE_CODE (decl))
12961 : : {
12962 : 3095709 : case BIT_NOT_EXPR:
12963 : 3095709 : {
12964 : 3095709 : if (innermost_code != cdk_function)
12965 : : {
12966 : 6 : error_at (EXPR_LOCATION (decl),
12967 : : "declaration of %qE as non-function", decl);
12968 : 6 : return error_mark_node;
12969 : : }
12970 : 3095703 : else if (!qualifying_scope
12971 : 3095703 : && !(current_class_type && at_class_scope_p ()))
12972 : : {
12973 : 9 : error_at (EXPR_LOCATION (decl),
12974 : : "declaration of %qE as non-member", decl);
12975 : 9 : return error_mark_node;
12976 : : }
12977 : :
12978 : 3095694 : tree type = TREE_OPERAND (decl, 0);
12979 : 3095694 : if (TYPE_P (type))
12980 : 3095691 : type = constructor_name (type);
12981 : 3095694 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
12982 : 3095694 : dname = decl;
12983 : : }
12984 : 3095694 : break;
12985 : :
12986 : 1867007 : case TEMPLATE_ID_EXPR:
12987 : 1867007 : {
12988 : 1867007 : tree fns = TREE_OPERAND (decl, 0);
12989 : :
12990 : 1867007 : dname = fns;
12991 : 2735613 : if (!identifier_p (dname))
12992 : 1866992 : dname = OVL_NAME (dname);
12993 : : }
12994 : : /* Fall through. */
12995 : :
12996 : 516614008 : case IDENTIFIER_NODE:
12997 : 516614008 : if (identifier_p (decl))
12998 : 514747001 : dname = decl;
12999 : :
13000 : 516614008 : if (IDENTIFIER_KEYWORD_P (dname))
13001 : : {
13002 : 0 : error ("declarator-id missing; using reserved word %qD",
13003 : : dname);
13004 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13005 : : }
13006 : 516614008 : else if (!IDENTIFIER_CONV_OP_P (dname))
13007 : 515993165 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13008 : : else
13009 : : {
13010 : 620843 : gcc_assert (flags == NO_SPECIAL);
13011 : 620843 : flags = TYPENAME_FLAG;
13012 : 620843 : sfk = sfk_conversion;
13013 : 620843 : tree glob = get_global_binding (dname);
13014 : 620843 : if (glob && TREE_CODE (glob) == TYPE_DECL)
13015 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13016 : : else
13017 : : name = "<invalid operator>";
13018 : : }
13019 : : break;
13020 : :
13021 : 0 : default:
13022 : 0 : gcc_unreachable ();
13023 : : }
13024 : : break;
13025 : : }
13026 : :
13027 : : case cdk_array:
13028 : : case cdk_pointer:
13029 : : case cdk_reference:
13030 : : case cdk_ptrmem:
13031 : : break;
13032 : :
13033 : : case cdk_decomp:
13034 : 870256618 : name = "structured binding";
13035 : : break;
13036 : :
13037 : 25 : case cdk_error:
13038 : 25 : return error_mark_node;
13039 : :
13040 : 0 : default:
13041 : 0 : gcc_unreachable ();
13042 : : }
13043 : 870256618 : if (id_declarator->kind == cdk_id)
13044 : : break;
13045 : : }
13046 : :
13047 : : /* [dcl.fct.edf]
13048 : :
13049 : : The declarator in a function-definition shall have the form
13050 : : D1 ( parameter-declaration-clause) ... */
13051 : 898065991 : if (funcdef_flag && innermost_code != cdk_function)
13052 : : {
13053 : 3 : error_at (id_loc, "function definition does not declare parameters");
13054 : 3 : return error_mark_node;
13055 : : }
13056 : :
13057 : 898065988 : if (flags == TYPENAME_FLAG
13058 : 898065988 : && innermost_code != cdk_function
13059 : 3 : && ! (ctype && !declspecs->any_specifiers_p))
13060 : : {
13061 : 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
13062 : 3 : return error_mark_node;
13063 : : }
13064 : :
13065 : 898065985 : if (dname && identifier_p (dname))
13066 : : {
13067 : 516614002 : if (UDLIT_OPER_P (dname)
13068 : 516614002 : && innermost_code != cdk_function)
13069 : : {
13070 : 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
13071 : 6 : return error_mark_node;
13072 : : }
13073 : :
13074 : 516613996 : if (IDENTIFIER_ANY_OP_P (dname))
13075 : : {
13076 : 30907497 : if (typedef_p)
13077 : : {
13078 : 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
13079 : 3 : return error_mark_node;
13080 : : }
13081 : 30907494 : else if (decl_context == PARM || decl_context == CATCHPARM)
13082 : : {
13083 : 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
13084 : 15 : return error_mark_node;
13085 : : }
13086 : : }
13087 : : }
13088 : :
13089 : : /* Anything declared one level down from the top level
13090 : : must be one of the parameters of a function
13091 : : (because the body is at least two levels down). */
13092 : :
13093 : : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
13094 : : by not allowing C++ class definitions to specify their parameters
13095 : : with xdecls (must be spec.d in the parmlist).
13096 : :
13097 : : Since we now wait to push a class scope until we are sure that
13098 : : we are in a legitimate method context, we must set oldcname
13099 : : explicitly (since current_class_name is not yet alive).
13100 : :
13101 : : We also want to avoid calling this a PARM if it is in a namespace. */
13102 : :
13103 : 898065961 : if (decl_context == NORMAL && !toplevel_bindings_p ())
13104 : : {
13105 : 60900548 : cp_binding_level *b = current_binding_level;
13106 : 60900548 : current_binding_level = b->level_chain;
13107 : 60900548 : if (current_binding_level != 0 && toplevel_bindings_p ())
13108 : : decl_context = PARM;
13109 : 60900548 : current_binding_level = b;
13110 : : }
13111 : :
13112 : 898065961 : if (name == NULL)
13113 : 378278247 : name = decl_context == PARM ? "parameter" : "type name";
13114 : :
13115 : 898065961 : if (consteval_p && constexpr_p)
13116 : : {
13117 : 6 : error_at (declspecs->locations[ds_consteval],
13118 : : "both %qs and %qs specified", "constexpr", "consteval");
13119 : 6 : return error_mark_node;
13120 : : }
13121 : :
13122 : 898065955 : if (concept_p && typedef_p)
13123 : : {
13124 : 9 : error_at (declspecs->locations[ds_concept],
13125 : : "%qs cannot appear in a typedef declaration", "concept");
13126 : 9 : return error_mark_node;
13127 : : }
13128 : :
13129 : 898065946 : if (constexpr_p && typedef_p)
13130 : : {
13131 : 3 : error_at (declspecs->locations[ds_constexpr],
13132 : : "%qs cannot appear in a typedef declaration", "constexpr");
13133 : 3 : return error_mark_node;
13134 : : }
13135 : :
13136 : 898065943 : if (consteval_p && typedef_p)
13137 : : {
13138 : 3 : error_at (declspecs->locations[ds_consteval],
13139 : : "%qs cannot appear in a typedef declaration", "consteval");
13140 : 3 : return error_mark_node;
13141 : : }
13142 : :
13143 : 898065940 : if (constinit_p && typedef_p)
13144 : : {
13145 : 3 : error_at (declspecs->locations[ds_constinit],
13146 : : "%qs cannot appear in a typedef declaration", "constinit");
13147 : 3 : return error_mark_node;
13148 : : }
13149 : :
13150 : : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
13151 : : keywords shall appear in a decl-specifier-seq." */
13152 : 898065937 : if (constinit_p && constexpr_p)
13153 : : {
13154 : 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
13155 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
13156 : 6 : error_at (&richloc,
13157 : : "can use at most one of the %<constinit%> and %<constexpr%> "
13158 : : "specifiers");
13159 : 6 : return error_mark_node;
13160 : 6 : }
13161 : :
13162 : : /* If there were multiple types specified in the decl-specifier-seq,
13163 : : issue an error message. */
13164 : 898065931 : if (declspecs->multiple_types_p)
13165 : : {
13166 : 1500 : error_at (typespec_loc,
13167 : : "two or more data types in declaration of %qs", name);
13168 : 1500 : return error_mark_node;
13169 : : }
13170 : :
13171 : 898064431 : if (declspecs->conflicting_specifiers_p)
13172 : 31 : return error_mark_node;
13173 : :
13174 : : /* Extract the basic type from the decl-specifier-seq. */
13175 : 898064400 : type = declspecs->type;
13176 : 898064400 : if (type == error_mark_node)
13177 : : {
13178 : 1165 : type = NULL_TREE;
13179 : 1165 : type_was_error_mark_node = true;
13180 : : }
13181 : :
13182 : : /* Ignore erroneous attributes. */
13183 : 898064400 : if (attrlist && *attrlist == error_mark_node)
13184 : 12 : *attrlist = NULL_TREE;
13185 : :
13186 : : /* An object declared as __attribute__((unavailable)) suppresses
13187 : : any reports of being declared with unavailable or deprecated
13188 : : items. An object declared as __attribute__((deprecated))
13189 : : suppresses warnings of uses of other deprecated items. */
13190 : 898064400 : auto ds = make_temp_override (deprecated_state);
13191 : 898064400 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
13192 : 159 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
13193 : 898064241 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
13194 : 693741 : deprecated_state = DEPRECATED_SUPPRESS;
13195 : :
13196 : 898064400 : cp_handle_deprecated_or_unavailable (type);
13197 : 898064400 : if (type && TREE_CODE (type) == TYPE_DECL)
13198 : : {
13199 : 618396476 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
13200 : 618396476 : typedef_decl = type;
13201 : 618396476 : type = TREE_TYPE (typedef_decl);
13202 : 618396476 : if (DECL_ARTIFICIAL (typedef_decl))
13203 : 447284871 : cp_handle_deprecated_or_unavailable (type);
13204 : : }
13205 : : /* No type at all: default to `int', and set DEFAULTED_INT
13206 : : because it was not a user-defined typedef. */
13207 : 898064400 : if (type == NULL_TREE)
13208 : : {
13209 : 34707114 : if (signed_p || unsigned_p || long_p || short_p)
13210 : : {
13211 : : /* These imply 'int'. */
13212 : 8012539 : type = integer_type_node;
13213 : 8012539 : defaulted_int = 1;
13214 : : }
13215 : : /* If we just have "complex", it is equivalent to "complex double". */
13216 : 26694575 : else if (!longlong && !explicit_intN
13217 : 26694575 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
13218 : : {
13219 : 81 : type = double_type_node;
13220 : 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
13221 : : "ISO C++ does not support plain %<complex%> meaning "
13222 : : "%<double complex%>");
13223 : : }
13224 : : }
13225 : : /* Gather flags. */
13226 : 898064400 : explicit_int = declspecs->explicit_int_p;
13227 : 898064400 : explicit_char = declspecs->explicit_char_p;
13228 : :
13229 : : #if 0
13230 : : /* See the code below that used this. */
13231 : : if (typedef_decl)
13232 : : decl_attr = DECL_ATTRIBUTES (typedef_decl);
13233 : : #endif
13234 : 898064400 : typedef_type = type;
13235 : :
13236 : 898064400 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
13237 : 1659384 : ctor_return_type = TREE_TYPE (dname);
13238 : : else
13239 : : ctor_return_type = ctype;
13240 : :
13241 : 898064400 : if (sfk != sfk_none)
13242 : : {
13243 : 26693009 : type = check_special_function_return_type (sfk, type,
13244 : : ctor_return_type,
13245 : : type_quals,
13246 : : &declarator,
13247 : : declspecs->locations);
13248 : 26693009 : type_quals = TYPE_UNQUALIFIED;
13249 : : }
13250 : 871371391 : else if (type == NULL_TREE)
13251 : : {
13252 : 1566 : int is_main;
13253 : :
13254 : 1566 : explicit_int = -1;
13255 : :
13256 : : /* We handle `main' specially here, because 'main () { }' is so
13257 : : common. With no options, it is allowed. With -Wreturn-type,
13258 : : it is a warning. It is only an error with -pedantic-errors. */
13259 : 3132 : is_main = (funcdef_flag
13260 : 3367 : && dname && identifier_p (dname)
13261 : 235 : && MAIN_NAME_P (dname)
13262 : 68 : && ctype == NULL_TREE
13263 : 68 : && in_namespace == NULL_TREE
13264 : 1634 : && current_namespace == global_namespace);
13265 : :
13266 : 1566 : if (type_was_error_mark_node)
13267 : : /* We've already issued an error, don't complain more. */;
13268 : 401 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
13269 : : /* Allow it, sigh. */;
13270 : 339 : else if (! is_main)
13271 : 277 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
13272 : : name);
13273 : 62 : else if (pedantic)
13274 : 4 : pedwarn (id_loc, OPT_Wpedantic,
13275 : : "ISO C++ forbids declaration of %qs with no type", name);
13276 : : else
13277 : 58 : warning_at (id_loc, OPT_Wreturn_type,
13278 : : "ISO C++ forbids declaration of %qs with no type", name);
13279 : :
13280 : 1566 : if (type_was_error_mark_node && template_parm_flag)
13281 : : /* FIXME we should be able to propagate the error_mark_node as is
13282 : : for other contexts too. */
13283 : 151 : type = error_mark_node;
13284 : : else
13285 : 1415 : type = integer_type_node;
13286 : : }
13287 : :
13288 : 898064400 : ctype = NULL_TREE;
13289 : :
13290 : 898064400 : if (explicit_intN)
13291 : : {
13292 : 898551 : if (! int_n_enabled_p[declspecs->int_n_idx])
13293 : : {
13294 : 0 : error_at (declspecs->locations[ds_type_spec],
13295 : : "%<__int%d%> is not supported by this target",
13296 : 0 : int_n_data[declspecs->int_n_idx].bitsize);
13297 : 0 : explicit_intN = false;
13298 : : }
13299 : : /* Don't pedwarn if the alternate "__intN__" form has been used instead
13300 : : of "__intN". */
13301 : 898551 : else if (!int_n_alt && pedantic)
13302 : 6010 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
13303 : : "ISO C++ does not support %<__int%d%> for %qs",
13304 : 6010 : int_n_data[declspecs->int_n_idx].bitsize, name);
13305 : : }
13306 : :
13307 : : /* Now process the modifiers that were specified
13308 : : and check for invalid combinations. */
13309 : :
13310 : : /* Long double is a special combination. */
13311 : 898064400 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
13312 : : {
13313 : 8587442 : long_p = false;
13314 : 8587442 : type = cp_build_qualified_type (long_double_type_node,
13315 : : cp_type_quals (type));
13316 : : }
13317 : :
13318 : : /* Check all other uses of type modifiers. */
13319 : :
13320 : 898064400 : if (unsigned_p || signed_p || long_p || short_p)
13321 : : {
13322 : 18701416 : location_t loc;
13323 : 18701416 : const char *key;
13324 : 18701416 : if (unsigned_p)
13325 : : {
13326 : 12591824 : key = "unsigned";
13327 : 12591824 : loc = declspecs->locations[ds_unsigned];
13328 : : }
13329 : 6109592 : else if (signed_p)
13330 : : {
13331 : 1057400 : key = "signed";
13332 : 1057400 : loc = declspecs->locations[ds_signed];
13333 : : }
13334 : 5052192 : else if (longlong)
13335 : : {
13336 : 1633241 : key = "long long";
13337 : 1633241 : loc = declspecs->locations[ds_long_long];
13338 : : }
13339 : 3418951 : else if (long_p)
13340 : : {
13341 : 2923115 : key = "long";
13342 : 2923115 : loc = declspecs->locations[ds_long];
13343 : : }
13344 : : else /* if (short_p) */
13345 : : {
13346 : 495836 : key = "short";
13347 : 495836 : loc = declspecs->locations[ds_short];
13348 : : }
13349 : :
13350 : 18701416 : int ok = 0;
13351 : :
13352 : 18701416 : if (signed_p && unsigned_p)
13353 : 54 : complain_about_incompatible_declspecs
13354 : 54 : ("signed", declspecs->locations[ds_signed],
13355 : : "unsigned", declspecs->locations[ds_unsigned]);
13356 : 18701362 : else if (long_p && short_p)
13357 : 12 : complain_about_incompatible_declspecs
13358 : 12 : ("long", declspecs->locations[ds_long],
13359 : : "short", declspecs->locations[ds_short]);
13360 : 18701350 : else if (TREE_CODE (type) != INTEGER_TYPE
13361 : 18701337 : || type == char8_type_node
13362 : 18701313 : || type == char16_type_node
13363 : 18701289 : || type == char32_type_node
13364 : 18701265 : || ((long_p || short_p)
13365 : 9956179 : && (explicit_char || explicit_intN)))
13366 : 418 : error_at (loc, "%qs specified with %qT", key, type);
13367 : 18700932 : else if (!explicit_int && !defaulted_int
13368 : 18700932 : && !explicit_char && !explicit_intN)
13369 : : {
13370 : 30 : if (typedef_decl)
13371 : : {
13372 : 27 : pedwarn (loc, OPT_Wpedantic,
13373 : : "%qs specified with typedef-name %qD",
13374 : : key, typedef_decl);
13375 : 27 : ok = !flag_pedantic_errors;
13376 : : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
13377 : : and is not built as a typedef. */
13378 : 27 : if (is_typedef_decl (typedef_decl))
13379 : 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
13380 : : }
13381 : 3 : else if (declspecs->decltype_p)
13382 : 0 : error_at (loc, "%qs specified with %<decltype%>", key);
13383 : : else
13384 : 3 : error_at (loc, "%qs specified with %<typeof%>", key);
13385 : : }
13386 : : else
13387 : : ok = 1;
13388 : :
13389 : : /* Discard the type modifiers if they are invalid. */
13390 : 514 : if (! ok)
13391 : : {
13392 : : unsigned_p = false;
13393 : : signed_p = false;
13394 : : long_p = false;
13395 : : short_p = false;
13396 : : longlong = 0;
13397 : : }
13398 : : }
13399 : :
13400 : : /* Decide whether an integer type is signed or not.
13401 : : Optionally treat bitfields as signed by default. */
13402 : 898063910 : if (unsigned_p
13403 : : /* [class.bit]
13404 : :
13405 : : It is implementation-defined whether a plain (neither
13406 : : explicitly signed or unsigned) char, short, int, or long
13407 : : bit-field is signed or unsigned.
13408 : :
13409 : : Naturally, we extend this to long long as well. Note that
13410 : : this does not include wchar_t. */
13411 : 898064400 : || (bitfield && !flag_signed_bitfields
13412 : 18 : && !signed_p
13413 : : /* A typedef for plain `int' without `signed' can be
13414 : : controlled just like plain `int', but a typedef for
13415 : : `signed int' cannot be so controlled. */
13416 : 18 : && !(typedef_decl
13417 : 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
13418 : 13 : && TREE_CODE (type) == INTEGER_TYPE
13419 : 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
13420 : : {
13421 : 12591609 : if (explicit_intN)
13422 : 419064 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
13423 : 12172545 : else if (longlong)
13424 : 1310910 : type = long_long_unsigned_type_node;
13425 : 10861635 : else if (long_p)
13426 : 2111327 : type = long_unsigned_type_node;
13427 : 8750308 : else if (short_p)
13428 : 1235196 : type = short_unsigned_type_node;
13429 : 7515112 : else if (type == char_type_node)
13430 : 1504647 : type = unsigned_char_type_node;
13431 : 6010465 : else if (typedef_decl)
13432 : 25 : type = c_common_unsigned_type (type);
13433 : : else
13434 : 6010440 : type = unsigned_type_node;
13435 : : }
13436 : 885472791 : else if (signed_p && type == char_type_node)
13437 : 652043 : type = signed_char_type_node;
13438 : 884820748 : else if (explicit_intN)
13439 : 479487 : type = int_n_trees[declspecs->int_n_idx].signed_type;
13440 : 884341261 : else if (longlong)
13441 : 1709486 : type = long_long_integer_type_node;
13442 : 882631775 : else if (long_p)
13443 : 3002785 : type = long_integer_type_node;
13444 : 879628990 : else if (short_p)
13445 : 586142 : type = short_integer_type_node;
13446 : 879042848 : else if (signed_p && typedef_decl)
13447 : 9 : type = c_common_signed_type (type);
13448 : :
13449 : 898064400 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
13450 : : {
13451 : 1385510 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
13452 : 3 : error_at (declspecs->locations[ds_complex],
13453 : : "complex invalid for %qs", name);
13454 : : /* If a modifier is specified, the resulting complex is the complex
13455 : : form of TYPE. E.g, "complex short" is "complex short int". */
13456 : 1385507 : else if (type == integer_type_node)
13457 : 164 : type = complex_integer_type_node;
13458 : 1385343 : else if (type == float_type_node)
13459 : 354963 : type = complex_float_type_node;
13460 : 1030380 : else if (type == double_type_node)
13461 : 355484 : type = complex_double_type_node;
13462 : 674896 : else if (type == long_double_type_node)
13463 : 355004 : type = complex_long_double_type_node;
13464 : : else
13465 : 319892 : type = build_complex_type (type);
13466 : : }
13467 : :
13468 : : /* If we're using the injected-class-name to form a compound type or a
13469 : : declaration, replace it with the underlying class so we don't get
13470 : : redundant typedefs in the debug output. But if we are returning the
13471 : : type unchanged, leave it alone so that it's available to
13472 : : maybe_get_template_decl_from_type_decl. */
13473 : 179051114 : if (CLASS_TYPE_P (type)
13474 : 179002288 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
13475 : 28504156 : && type == TREE_TYPE (TYPE_NAME (type))
13476 : 926568556 : && (declarator || type_quals))
13477 : 27368093 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
13478 : :
13479 : 898064400 : type_quals |= cp_type_quals (type);
13480 : 1796128800 : type = cp_build_qualified_type
13481 : 1796128800 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
13482 : 726952795 : || declspecs->decltype_p)
13483 : : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
13484 : : /* We might have ignored or rejected some of the qualifiers. */
13485 : 898064400 : type_quals = cp_type_quals (type);
13486 : :
13487 : 883686110 : if (cxx_dialect >= cxx17 && type && is_auto (type)
13488 : 12678970 : && innermost_code != cdk_function
13489 : : /* Placeholder in parm gets a better error below. */
13490 : 7675727 : && !(decl_context == PARM || decl_context == CATCHPARM)
13491 : 905716054 : && id_declarator && declarator != id_declarator)
13492 : 951669 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
13493 : : {
13494 : 16 : auto_diagnostic_group g;
13495 : 16 : gcc_rich_location richloc (typespec_loc);
13496 : 16 : richloc.add_fixit_insert_after ("<>");
13497 : 16 : error_at (&richloc, "missing template argument list after %qE; "
13498 : : "for deduction, template placeholder must be followed "
13499 : : "by a simple declarator-id", tmpl);
13500 : 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
13501 : 16 : type = error_mark_node;
13502 : 16 : }
13503 : :
13504 : 898064400 : staticp = 0;
13505 : 898064400 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
13506 : 898064400 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
13507 : 898064400 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
13508 : :
13509 : 898064400 : storage_class = declspecs->storage_class;
13510 : 898064400 : if (storage_class == sc_static)
13511 : 18512311 : staticp = 1 + (decl_context == FIELD);
13512 : 879552089 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
13513 : : /* Treat class-scope deduction guides as static member functions
13514 : : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
13515 : 18191417 : staticp = 2;
13516 : :
13517 : 898064400 : if (virtualp)
13518 : : {
13519 : 3989682 : if (staticp == 2)
13520 : : {
13521 : 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13522 : 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
13523 : 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
13524 : : "and %<static%>", dname);
13525 : 21 : storage_class = sc_none;
13526 : 21 : staticp = 0;
13527 : 21 : }
13528 : 3989682 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
13529 : : {
13530 : 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13531 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
13532 : 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
13533 : : "declared both %<virtual%> and %<constexpr%> only in "
13534 : : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
13535 : 6 : }
13536 : : }
13537 : 898064400 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
13538 : :
13539 : : /* Issue errors about use of storage classes for parameters. */
13540 : 898064400 : if (decl_context == PARM)
13541 : : {
13542 : 269620112 : if (typedef_p)
13543 : : {
13544 : 24 : error_at (declspecs->locations[ds_typedef],
13545 : : "typedef declaration invalid in parameter declaration");
13546 : 24 : return error_mark_node;
13547 : : }
13548 : 269620088 : else if (template_parm_flag && storage_class != sc_none)
13549 : : {
13550 : 16 : error_at (min_location (declspecs->locations[ds_thread],
13551 : : declspecs->locations[ds_storage_class]),
13552 : : "storage class specified for template parameter %qs",
13553 : : name);
13554 : 16 : return error_mark_node;
13555 : : }
13556 : 269620072 : else if (storage_class == sc_static
13557 : 269620072 : || storage_class == sc_extern
13558 : 269620069 : || thread_p)
13559 : : {
13560 : 9 : error_at (min_location (declspecs->locations[ds_thread],
13561 : : declspecs->locations[ds_storage_class]),
13562 : : "storage class specified for parameter %qs", name);
13563 : 9 : return error_mark_node;
13564 : : }
13565 : :
13566 : : /* Function parameters cannot be concept. */
13567 : 269620063 : if (concept_p)
13568 : : {
13569 : 9 : error_at (declspecs->locations[ds_concept],
13570 : : "a parameter cannot be declared %qs", "concept");
13571 : 9 : concept_p = 0;
13572 : 9 : constexpr_p = 0;
13573 : : }
13574 : : /* Function parameters cannot be constexpr. If we saw one, moan
13575 : : and pretend it wasn't there. */
13576 : 269620054 : else if (constexpr_p)
13577 : : {
13578 : 9 : error_at (declspecs->locations[ds_constexpr],
13579 : : "a parameter cannot be declared %qs", "constexpr");
13580 : 9 : constexpr_p = 0;
13581 : : }
13582 : 269620063 : if (constinit_p)
13583 : : {
13584 : 6 : error_at (declspecs->locations[ds_constinit],
13585 : : "a parameter cannot be declared %qs", "constinit");
13586 : 6 : constinit_p = 0;
13587 : : }
13588 : 269620063 : if (consteval_p)
13589 : : {
13590 : 3 : error_at (declspecs->locations[ds_consteval],
13591 : : "a parameter cannot be declared %qs", "consteval");
13592 : 3 : consteval_p = 0;
13593 : : }
13594 : : }
13595 : :
13596 : : /* Give error if `virtual' is used outside of class declaration. */
13597 : 898064351 : if (virtualp
13598 : 3989682 : && (current_class_name == NULL_TREE || decl_context != FIELD))
13599 : : {
13600 : 22 : error_at (declspecs->locations[ds_virtual],
13601 : : "%<virtual%> outside class declaration");
13602 : 22 : virtualp = 0;
13603 : : }
13604 : :
13605 : 898064351 : if (innermost_code == cdk_decomp)
13606 : : {
13607 : 67994 : location_t loc = (declarator->kind == cdk_reference
13608 : 78042 : ? declarator->declarator->id_loc : declarator->id_loc);
13609 : 78042 : if (inlinep)
13610 : 1 : error_at (declspecs->locations[ds_inline],
13611 : : "structured binding declaration cannot be %qs", "inline");
13612 : 78042 : if (typedef_p)
13613 : 1 : error_at (declspecs->locations[ds_typedef],
13614 : : "structured binding declaration cannot be %qs", "typedef");
13615 : 78042 : if (constexpr_p && !concept_p)
13616 : 4 : error_at (declspecs->locations[ds_constexpr], "structured "
13617 : : "binding declaration cannot be %qs", "constexpr");
13618 : 78042 : if (consteval_p)
13619 : 3 : error_at (declspecs->locations[ds_consteval], "structured "
13620 : : "binding declaration cannot be %qs", "consteval");
13621 : 78042 : if (thread_p && cxx_dialect < cxx20)
13622 : 29 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
13623 : : "structured binding declaration can be %qs only in "
13624 : : "%<-std=c++20%> or %<-std=gnu++20%>",
13625 : 29 : declspecs->gnu_thread_keyword_p
13626 : : ? "__thread" : "thread_local");
13627 : 78042 : if (concept_p)
13628 : 0 : error_at (declspecs->locations[ds_concept],
13629 : : "structured binding declaration cannot be %qs", "concept");
13630 : : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
13631 : 78042 : if (type_quals & TYPE_QUAL_VOLATILE)
13632 : 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
13633 : : "%<volatile%>-qualified structured binding is deprecated");
13634 : 78042 : switch (storage_class)
13635 : : {
13636 : : case sc_none:
13637 : : break;
13638 : 0 : case sc_register:
13639 : 0 : error_at (loc, "structured binding declaration cannot be %qs",
13640 : : "register");
13641 : 0 : break;
13642 : 134 : case sc_static:
13643 : 134 : if (cxx_dialect < cxx20)
13644 : 126 : pedwarn (loc, OPT_Wc__20_extensions,
13645 : : "structured binding declaration can be %qs only in "
13646 : : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
13647 : : break;
13648 : 1 : case sc_extern:
13649 : 1 : error_at (loc, "structured binding declaration cannot be %qs",
13650 : : "extern");
13651 : 1 : break;
13652 : 0 : case sc_mutable:
13653 : 0 : error_at (loc, "structured binding declaration cannot be %qs",
13654 : : "mutable");
13655 : 0 : break;
13656 : 0 : case sc_auto:
13657 : 0 : error_at (loc, "structured binding declaration cannot be "
13658 : : "C++98 %<auto%>");
13659 : 0 : break;
13660 : 0 : default:
13661 : 0 : gcc_unreachable ();
13662 : : }
13663 : 78042 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
13664 : 78042 : || TYPE_IDENTIFIER (type) != auto_identifier)
13665 : : {
13666 : 3 : if (type != error_mark_node)
13667 : : {
13668 : 3 : auto_diagnostic_group d;
13669 : 3 : error_at (loc, "structured binding declaration cannot have "
13670 : : "type %qT", type);
13671 : 3 : inform (loc,
13672 : : "type must be cv-qualified %<auto%> or reference to "
13673 : : "cv-qualified %<auto%>");
13674 : 3 : }
13675 : 3 : type = build_qualified_type (make_auto (), type_quals);
13676 : 3 : declspecs->type = type;
13677 : : }
13678 : 78039 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
13679 : 51 : pedwarn (loc, OPT_Wpedantic,
13680 : : "structured binding declaration cannot have constrained "
13681 : : "%<auto%> type %qT", type);
13682 : 78042 : inlinep = 0;
13683 : 78042 : typedef_p = 0;
13684 : 78042 : constexpr_p = 0;
13685 : 78042 : consteval_p = 0;
13686 : 78042 : concept_p = 0;
13687 : 78042 : if (storage_class != sc_static)
13688 : : {
13689 : 77908 : storage_class = sc_none;
13690 : 77908 : declspecs->storage_class = sc_none;
13691 : : }
13692 : : }
13693 : :
13694 : : /* Static anonymous unions are dealt with here. */
13695 : 898064351 : if (staticp && decl_context == TYPENAME
13696 : 0 : && declspecs->type
13697 : 898064351 : && ANON_AGGR_TYPE_P (declspecs->type))
13698 : : decl_context = FIELD;
13699 : :
13700 : : /* Warn about storage classes that are invalid for certain
13701 : : kinds of declarations (parameters, typenames, etc.). */
13702 : 898064351 : if (thread_p
13703 : 20738 : && ((storage_class
13704 : 20738 : && storage_class != sc_extern
13705 : 517 : && storage_class != sc_static)
13706 : 20727 : || typedef_p))
13707 : : {
13708 : 17 : location_t loc
13709 : 17 : = min_location (declspecs->locations[ds_thread],
13710 : : declspecs->locations[ds_storage_class]);
13711 : 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
13712 : 17 : thread_p = false;
13713 : : }
13714 : 898064351 : if (decl_context != NORMAL
13715 : 747368890 : && ((storage_class != sc_none
13716 : 747368890 : && storage_class != sc_mutable)
13717 : 729177485 : || thread_p))
13718 : : {
13719 : 18191408 : if ((decl_context == PARM || decl_context == CATCHPARM)
13720 : 36 : && (storage_class == sc_register
13721 : 36 : || storage_class == sc_auto))
13722 : : ;
13723 : 18191372 : else if (typedef_p)
13724 : : ;
13725 : 18191372 : else if (decl_context == FIELD
13726 : : /* C++ allows static class elements. */
13727 : 18191372 : && storage_class == sc_static)
13728 : : /* C++ also allows inlines and signed and unsigned elements,
13729 : : but in those cases we don't come in here. */
13730 : : ;
13731 : : else
13732 : : {
13733 : 15 : location_t loc
13734 : 15 : = min_location (declspecs->locations[ds_thread],
13735 : : declspecs->locations[ds_storage_class]);
13736 : 15 : if (decl_context == FIELD)
13737 : 15 : error_at (loc, "storage class specified for %qs", name);
13738 : 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
13739 : 0 : error_at (loc, "storage class specified for parameter %qs", name);
13740 : : else
13741 : 0 : error_at (loc, "storage class specified for typename");
13742 : 15 : if (storage_class == sc_register
13743 : 15 : || storage_class == sc_auto
13744 : 15 : || storage_class == sc_extern
13745 : 3 : || thread_p)
13746 : 15 : storage_class = sc_none;
13747 : : }
13748 : : }
13749 : 879872943 : else if (storage_class == sc_extern && funcdef_flag
13750 : 879872943 : && ! toplevel_bindings_p ())
13751 : 0 : error ("nested function %qs declared %<extern%>", name);
13752 : 879872943 : else if (toplevel_bindings_p ())
13753 : : {
13754 : 406286914 : if (storage_class == sc_auto)
13755 : 2 : error_at (declspecs->locations[ds_storage_class],
13756 : : "top-level declaration of %qs specifies %<auto%>", name);
13757 : : }
13758 : 473586029 : else if (thread_p
13759 : 473586029 : && storage_class != sc_extern
13760 : 97 : && storage_class != sc_static)
13761 : : {
13762 : 65 : if (declspecs->gnu_thread_keyword_p)
13763 : 3 : pedwarn (declspecs->locations[ds_thread],
13764 : 3 : 0, "function-scope %qs implicitly auto and "
13765 : : "declared %<__thread%>", name);
13766 : :
13767 : : /* When thread_local is applied to a variable of block scope the
13768 : : storage-class-specifier static is implied if it does not appear
13769 : : explicitly. */
13770 : 65 : storage_class = declspecs->storage_class = sc_static;
13771 : 65 : staticp = 1;
13772 : : }
13773 : :
13774 : 898064351 : if (storage_class && friendp)
13775 : : {
13776 : 12 : error_at (min_location (declspecs->locations[ds_thread],
13777 : : declspecs->locations[ds_storage_class]),
13778 : : "storage class specifiers invalid in friend function "
13779 : : "declarations");
13780 : 12 : storage_class = sc_none;
13781 : 12 : staticp = 0;
13782 : : }
13783 : :
13784 : 898064351 : if (!id_declarator)
13785 : : unqualified_id = NULL_TREE;
13786 : : else
13787 : : {
13788 : 519708141 : unqualified_id = id_declarator->u.id.unqualified_name;
13789 : 519708141 : switch (TREE_CODE (unqualified_id))
13790 : : {
13791 : 3095694 : case BIT_NOT_EXPR:
13792 : 3095694 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
13793 : 3095694 : if (TYPE_P (unqualified_id))
13794 : 3095691 : unqualified_id = constructor_name (unqualified_id);
13795 : : break;
13796 : :
13797 : : case IDENTIFIER_NODE:
13798 : : case TEMPLATE_ID_EXPR:
13799 : : break;
13800 : :
13801 : 0 : default:
13802 : 0 : gcc_unreachable ();
13803 : : }
13804 : : }
13805 : :
13806 : 898064351 : if (declspecs->std_attributes
13807 : 898064351 : && !diagnose_misapplied_contracts (declspecs->std_attributes))
13808 : : {
13809 : 651 : location_t attr_loc = declspecs->locations[ds_std_attribute];
13810 : 651 : auto_diagnostic_group d;
13811 : 651 : if (any_nonignored_attribute_p (declspecs->std_attributes)
13812 : 651 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
13813 : 648 : inform (attr_loc, "an attribute that appertains to a type-specifier "
13814 : : "is ignored");
13815 : 651 : }
13816 : :
13817 : 898064351 : if (attrlist)
13818 : 886092922 : diagnose_misapplied_contracts (*attrlist);
13819 : :
13820 : : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
13821 : : bool is_xobj_member_function = false;
13822 : : /* Determine the type of the entity declared by recurring on the
13823 : : declarator. */
13824 : 1248531218 : for (; declarator; declarator = declarator->declarator)
13825 : : {
13826 : 870253075 : const cp_declarator *inner_declarator;
13827 : 870253075 : tree attrs;
13828 : :
13829 : 870253075 : if (type == error_mark_node)
13830 : 599 : return error_mark_node;
13831 : :
13832 : 870252749 : attrs = declarator->attributes;
13833 : 870252749 : if (attrs)
13834 : : {
13835 : 12942 : int attr_flags;
13836 : :
13837 : 12942 : attr_flags = 0;
13838 : 12942 : if (declarator->kind == cdk_id)
13839 : 12266 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
13840 : 12942 : if (declarator->kind == cdk_function)
13841 : 656 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
13842 : 12942 : if (declarator->kind == cdk_array)
13843 : 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
13844 : 12942 : tree late_attrs = NULL_TREE;
13845 : 12942 : if (decl_context != PARM && decl_context != TYPENAME)
13846 : : /* Assume that any attributes that get applied late to
13847 : : templates will DTRT when applied to the declaration
13848 : : as a whole. */
13849 : 12930 : late_attrs = splice_template_attributes (&attrs, type);
13850 : 12942 : returned_attrs = decl_attributes (&type,
13851 : : attr_chainon (returned_attrs,
13852 : : attrs),
13853 : : attr_flags);
13854 : 12942 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
13855 : : }
13856 : :
13857 : 870252749 : inner_declarator = declarator->declarator;
13858 : :
13859 : : /* Check that contracts aren't misapplied. */
13860 : 870252749 : if (tree contract_attr = find_contract (declarator->std_attributes))
13861 : 484 : if (declarator->kind != cdk_function
13862 : 479 : || innermost_code != cdk_function)
13863 : 7 : diagnose_misapplied_contracts (contract_attr);
13864 : :
13865 : : /* We don't want to warn in parameter context because we don't
13866 : : yet know if the parse will succeed, and this might turn out
13867 : : to be a constructor call. */
13868 : 870252749 : if (decl_context != PARM
13869 : 870252749 : && decl_context != TYPENAME
13870 : 499307456 : && !typedef_p
13871 : 455162363 : && declarator->parenthesized != UNKNOWN_LOCATION
13872 : : /* If the type is class-like and the inner name used a
13873 : : global namespace qualifier, we need the parens.
13874 : : Unfortunately all we can tell is whether a qualified name
13875 : : was used or not. */
13876 : 870252930 : && !(inner_declarator
13877 : 126 : && inner_declarator->kind == cdk_id
13878 : 108 : && inner_declarator->u.id.qualifying_scope
13879 : 18 : && (MAYBE_CLASS_TYPE_P (type)
13880 : 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
13881 : : {
13882 : 169 : auto_diagnostic_group d;
13883 : 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
13884 : : "unnecessary parentheses in declaration of %qs",
13885 : : name))
13886 : : {
13887 : 42 : gcc_rich_location iloc (declarator->parenthesized);
13888 : 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
13889 : 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
13890 : 42 : inform (&iloc, "remove parentheses");
13891 : 42 : }
13892 : 169 : }
13893 : 870252749 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
13894 : : break;
13895 : :
13896 : 350467140 : switch (declarator->kind)
13897 : : {
13898 : 3063865 : case cdk_array:
13899 : 6127730 : type = create_array_type_for_decl (dname, type,
13900 : 3063865 : declarator->u.array.bounds,
13901 : 3063865 : declarator->id_loc);
13902 : 3063865 : if (!valid_array_size_p (dname
13903 : : ? declarator->id_loc : input_location,
13904 : : type, dname))
13905 : 267 : type = error_mark_node;
13906 : :
13907 : 3063865 : if (declarator->std_attributes)
13908 : : /* [dcl.array]/1:
13909 : :
13910 : : The optional attribute-specifier-seq appertains to the
13911 : : array type. */
13912 : 174 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
13913 : : break;
13914 : :
13915 : 171165813 : case cdk_function:
13916 : 171165813 : {
13917 : 171165813 : tree arg_types;
13918 : 171165813 : int funcdecl_p;
13919 : :
13920 : : /* Declaring a function type. */
13921 : :
13922 : : /* Pick up type qualifiers which should be applied to `this'. */
13923 : 171165813 : memfn_quals = declarator->u.function.qualifiers;
13924 : : /* Pick up virt-specifiers. */
13925 : 171165813 : virt_specifiers = declarator->u.function.virt_specifiers;
13926 : : /* And ref-qualifier, too */
13927 : 171165813 : rqual = declarator->u.function.ref_qualifier;
13928 : : /* And tx-qualifier. */
13929 : 171165813 : tree tx_qual = declarator->u.function.tx_qualifier;
13930 : : /* Pick up the exception specifications. */
13931 : 171165813 : raises = declarator->u.function.exception_specification;
13932 : : /* If the exception-specification is ill-formed, let's pretend
13933 : : there wasn't one. */
13934 : 171165813 : if (raises == error_mark_node)
13935 : 9 : raises = NULL_TREE;
13936 : :
13937 : 342331626 : auto find_xobj_parm = [](tree parm_list)
13938 : : {
13939 : : /* There is no need to iterate over the list,
13940 : : only the first parm can be a valid xobj parm. */
13941 : 342104285 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
13942 : : return NULL_TREE;
13943 : : /* If we make it here, we are looking at an xobj parm.
13944 : :
13945 : : Non-null 'purpose' usually means the parm has a default
13946 : : argument, we don't want to violate this assumption. */
13947 : 26290 : TREE_PURPOSE (parm_list) = NULL_TREE;
13948 : 26290 : return TREE_VALUE (parm_list);
13949 : : };
13950 : :
13951 : 171165813 : tree xobj_parm
13952 : 171165813 : = find_xobj_parm (declarator->u.function.parameters);
13953 : 171165813 : is_xobj_member_function = xobj_parm;
13954 : :
13955 : 171165813 : if (xobj_parm && cxx_dialect < cxx23)
13956 : 24 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
13957 : : "explicit object member function only available "
13958 : : "with %<-std=c++23%> or %<-std=gnu++23%>");
13959 : :
13960 : 171165813 : if (xobj_parm && decl_context == TYPENAME)
13961 : : {
13962 : : /* We inform in every case, just differently depending on what
13963 : : case it is. */
13964 : 6 : auto_diagnostic_group d;
13965 : 6 : bool ptr_type = true;
13966 : : /* If declarator->kind is cdk_function and we are at the end of
13967 : : the declarator chain, we are looking at a function type. */
13968 : 6 : if (!declarator->declarator)
13969 : : {
13970 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13971 : : "a function type cannot "
13972 : : "have an explicit object parameter");
13973 : 2 : ptr_type = false;
13974 : : }
13975 : 4 : else if (declarator->declarator->kind == cdk_pointer)
13976 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13977 : : "a pointer to function type cannot "
13978 : : "have an explicit object parameter");
13979 : 2 : else if (declarator->declarator->kind == cdk_ptrmem)
13980 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13981 : : "a pointer to member function type "
13982 : : "cannot have an explicit object parameter");
13983 : : else
13984 : 0 : gcc_unreachable ();
13985 : :
13986 : : /* The locations being used here are probably not correct. */
13987 : 4 : if (ptr_type)
13988 : 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13989 : : "the type of a pointer to explicit object member "
13990 : : "function is a regular pointer to function type");
13991 : : else
13992 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13993 : : "the type of an explicit object "
13994 : : "member function is a regular function type");
13995 : : /* Ideally we should synthesize the correct syntax
13996 : : for the user, perhaps this could be added later. */
13997 : 6 : }
13998 : : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
13999 : : the first parm node will never erroneously be detected here. */
14000 : 171165813 : {
14001 : 171165813 : auto_diagnostic_group d;
14002 : 171165813 : bool bad_xobj_parm_encountered = false;
14003 : 171165813 : for (tree parm = declarator->u.function.parameters;
14004 : 429662739 : parm && parm != void_list_node;
14005 : 258496926 : parm = TREE_CHAIN (parm))
14006 : : {
14007 : 258496926 : if (TREE_PURPOSE (parm) != this_identifier)
14008 : 258496920 : continue;
14009 : 6 : bad_xobj_parm_encountered = true;
14010 : 6 : TREE_PURPOSE (parm) = NULL_TREE;
14011 : 6 : gcc_rich_location bad_xobj_parm
14012 : 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
14013 : 6 : error_at (&bad_xobj_parm,
14014 : : "only the first parameter of a member function "
14015 : : "can be declared as an explicit object parameter");
14016 : 6 : }
14017 : 171165813 : if (bad_xobj_parm_encountered && xobj_parm)
14018 : 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14019 : : "valid explicit object parameter declared here");
14020 : 171165813 : }
14021 : :
14022 : 171165813 : if (reqs)
14023 : 3 : error_at (location_of (reqs), "requires-clause on return type");
14024 : 171165813 : reqs = declarator->u.function.requires_clause;
14025 : :
14026 : : /* Say it's a definition only for the CALL_EXPR
14027 : : closest to the identifier. */
14028 : 171165813 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
14029 : :
14030 : : /* Handle a late-specified return type. */
14031 : 171165813 : tree late_return_type = declarator->u.function.late_return_type;
14032 : 171165813 : if (tree auto_node = type_uses_auto (type))
14033 : : {
14034 : 5012777 : if (!late_return_type)
14035 : : {
14036 : 2075398 : if (!funcdecl_p)
14037 : : /* auto (*fp)() = f; is OK. */;
14038 : 2075366 : else if (current_class_type
14039 : 3821683 : && LAMBDA_TYPE_P (current_class_type))
14040 : : /* OK for C++11 lambdas. */;
14041 : 1507256 : else if (cxx_dialect < cxx14)
14042 : : {
14043 : 7 : auto_diagnostic_group d;
14044 : 7 : error_at (typespec_loc, "%qs function uses "
14045 : : "%<auto%> type specifier without "
14046 : : "trailing return type", name);
14047 : 7 : inform (typespec_loc,
14048 : : "deduced return type only available "
14049 : : "with %<-std=c++14%> or %<-std=gnu++14%>");
14050 : 7 : }
14051 : 1507249 : else if (virtualp)
14052 : : {
14053 : 6 : error_at (typespec_loc, "virtual function "
14054 : : "cannot have deduced return type");
14055 : 6 : virtualp = false;
14056 : : }
14057 : : }
14058 : 2937379 : else if (!is_auto (type) && sfk != sfk_conversion)
14059 : : {
14060 : 15 : error_at (typespec_loc, "%qs function with trailing "
14061 : : "return type has %qT as its type rather "
14062 : : "than plain %<auto%>", name, type);
14063 : 270 : return error_mark_node;
14064 : : }
14065 : 2937364 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
14066 : : {
14067 : 9 : if (funcdecl_p)
14068 : 3 : error_at (typespec_loc,
14069 : : "%qs function with trailing return type "
14070 : : "has %<decltype(auto)%> as its type "
14071 : : "rather than plain %<auto%>", name);
14072 : : else
14073 : 6 : error_at (typespec_loc,
14074 : : "invalid use of %<decltype(auto)%>");
14075 : 9 : return error_mark_node;
14076 : : }
14077 : 2937355 : else if (is_constrained_auto (type))
14078 : : {
14079 : 6 : if (funcdecl_p)
14080 : 3 : error_at (typespec_loc,
14081 : : "%qs function with trailing return type "
14082 : : "has constrained %<auto%> type specifier "
14083 : : "rather than plain %<auto%>",
14084 : : name);
14085 : : else
14086 : 3 : error_at (typespec_loc,
14087 : : "invalid use of constrained %<auto%> type");
14088 : 6 : return error_mark_node;
14089 : : }
14090 : 5012747 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
14091 : 5012747 : if (!tmpl)
14092 : 3974200 : if (tree late_auto = type_uses_auto (late_return_type))
14093 : 21067 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
14094 : 3974200 : if (tmpl)
14095 : : {
14096 : 1038551 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
14097 : : {
14098 : 10 : auto_diagnostic_group g;
14099 : 10 : error_at (typespec_loc, "deduced class "
14100 : : "type %qD in function return type",
14101 : 10 : DECL_NAME (tmpl));
14102 : 10 : inform (DECL_SOURCE_LOCATION (tmpl),
14103 : : "%qD declared here", tmpl);
14104 : 10 : return error_mark_node;
14105 : 10 : }
14106 : 1038541 : else if (!late_return_type)
14107 : : {
14108 : 3 : auto_diagnostic_group d;
14109 : 3 : error_at (declarator->id_loc, "deduction guide "
14110 : : "for %qT must have trailing return "
14111 : 3 : "type", TREE_TYPE (tmpl));
14112 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
14113 : : "%qD declared here", tmpl);
14114 : 3 : return error_mark_node;
14115 : 3 : }
14116 : 1038538 : else if (CLASS_TYPE_P (late_return_type)
14117 : 1038538 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
14118 : 2077076 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
14119 : : == tmpl))
14120 : : /* OK */;
14121 : : else
14122 : 0 : error ("trailing return type %qT of deduction guide "
14123 : : "is not a specialization of %qT",
14124 : 0 : late_return_type, TREE_TYPE (tmpl));
14125 : : }
14126 : : }
14127 : 166153036 : else if (late_return_type
14128 : 166153036 : && sfk != sfk_conversion)
14129 : : {
14130 : 37 : if (late_return_type == error_mark_node)
14131 : : return error_mark_node;
14132 : 37 : if (cxx_dialect < cxx11)
14133 : : /* Not using maybe_warn_cpp0x because this should
14134 : : always be an error. */
14135 : 1 : error_at (typespec_loc,
14136 : : "trailing return type only available "
14137 : : "with %<-std=c++11%> or %<-std=gnu++11%>");
14138 : : else
14139 : 36 : error_at (typespec_loc, "%qs function with trailing "
14140 : : "return type not declared with %<auto%> "
14141 : : "type specifier", name);
14142 : 37 : return error_mark_node;
14143 : : }
14144 : 171165733 : if (late_return_type && sfk == sfk_conversion)
14145 : : {
14146 : 14 : error ("a conversion function cannot have a trailing return type");
14147 : 14 : return error_mark_node;
14148 : : }
14149 : 171165719 : type = splice_late_return_type (type, late_return_type);
14150 : 171165719 : if (type == error_mark_node)
14151 : : return error_mark_node;
14152 : :
14153 : 171165643 : if (late_return_type)
14154 : : {
14155 : 2937261 : late_return_type_p = true;
14156 : 2937261 : type_quals = cp_type_quals (type);
14157 : : }
14158 : :
14159 : 171165643 : if (type_quals != TYPE_UNQUALIFIED)
14160 : : {
14161 : : /* It's wrong, for instance, to issue a -Wignored-qualifiers
14162 : : warning for
14163 : : static_assert(!is_same_v<void(*)(), const void(*)()>);
14164 : : because there the qualifier matters. */
14165 : 12186 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
14166 : 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
14167 : : "qualifiers ignored on function return type");
14168 : : /* [dcl.fct] "A volatile-qualified return type is
14169 : : deprecated." */
14170 : 12186 : if (type_quals & TYPE_QUAL_VOLATILE)
14171 : 598 : warning_at (typespec_loc, OPT_Wvolatile,
14172 : : "%<volatile%>-qualified return type is "
14173 : : "deprecated");
14174 : :
14175 : : /* We now know that the TYPE_QUALS don't apply to the
14176 : : decl, but to its return type. */
14177 : : type_quals = TYPE_UNQUALIFIED;
14178 : : }
14179 : :
14180 : : /* Error about some types functions can't return. */
14181 : :
14182 : 171165643 : if (TREE_CODE (type) == FUNCTION_TYPE)
14183 : : {
14184 : 30 : error_at (typespec_loc, "%qs declared as function returning "
14185 : : "a function", name);
14186 : 30 : return error_mark_node;
14187 : : }
14188 : 171165613 : if (TREE_CODE (type) == ARRAY_TYPE)
14189 : : {
14190 : 6 : error_at (typespec_loc, "%qs declared as function returning "
14191 : : "an array", name);
14192 : 6 : return error_mark_node;
14193 : : }
14194 : 171165607 : if (constinit_p && funcdecl_p)
14195 : : {
14196 : 15 : error_at (declspecs->locations[ds_constinit],
14197 : : "%<constinit%> on function return type is not "
14198 : : "allowed");
14199 : 15 : return error_mark_node;
14200 : : }
14201 : :
14202 : 171165592 : if (check_decltype_auto (typespec_loc, type))
14203 : 49 : return error_mark_node;
14204 : :
14205 : 171165543 : if (ctype == NULL_TREE
14206 : 171165543 : && decl_context == FIELD
14207 : : && funcdecl_p
14208 : 92469856 : && friendp == 0)
14209 : 88288493 : ctype = current_class_type;
14210 : :
14211 : 171165543 : if (ctype && (sfk == sfk_constructor
14212 : 88288493 : || sfk == sfk_destructor))
14213 : : {
14214 : : /* We are within a class's scope. If our declarator name
14215 : : is the same as the class name, and we are defining
14216 : : a function, then it is a constructor/destructor, and
14217 : : therefore returns a void type. */
14218 : :
14219 : : /* ISO C++ 12.4/2. A destructor may not be declared
14220 : : const or volatile. A destructor may not be static.
14221 : : A destructor may not be declared with ref-qualifier.
14222 : :
14223 : : ISO C++ 12.1. A constructor may not be declared
14224 : : const or volatile. A constructor may not be
14225 : : virtual. A constructor may not be static.
14226 : : A constructor may not be declared with ref-qualifier. */
14227 : 24078337 : if (staticp == 2)
14228 : 9 : error_at (declspecs->locations[ds_storage_class],
14229 : : (flags == DTOR_FLAG)
14230 : : ? G_("destructor cannot be static member "
14231 : : "function")
14232 : : : G_("constructor cannot be static member "
14233 : : "function"));
14234 : 24078337 : if (memfn_quals)
14235 : : {
14236 : 9 : error ((flags == DTOR_FLAG)
14237 : : ? G_("destructors may not be cv-qualified")
14238 : : : G_("constructors may not be cv-qualified"));
14239 : 9 : memfn_quals = TYPE_UNQUALIFIED;
14240 : : }
14241 : :
14242 : 24078337 : if (rqual)
14243 : : {
14244 : 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
14245 : 6 : error ((flags == DTOR_FLAG)
14246 : : ? G_("destructors may not be ref-qualified")
14247 : : : G_("constructors may not be ref-qualified"));
14248 : 6 : rqual = REF_QUAL_NONE;
14249 : : }
14250 : :
14251 : 24078337 : if (decl_context == FIELD
14252 : 24078337 : && !member_function_or_else (ctype,
14253 : : current_class_type,
14254 : : flags))
14255 : 0 : return error_mark_node;
14256 : :
14257 : 24078337 : if (flags != DTOR_FLAG)
14258 : : {
14259 : : /* It's a constructor. */
14260 : 21152857 : if (explicitp == 1)
14261 : 5090005 : explicitp = 2;
14262 : 21152857 : if (virtualp)
14263 : : {
14264 : 3 : permerror (declspecs->locations[ds_virtual],
14265 : : "constructors cannot be declared %<virtual%>");
14266 : 3 : virtualp = 0;
14267 : : }
14268 : 21152857 : if (decl_context == FIELD
14269 : 21152857 : && sfk != sfk_constructor)
14270 : 0 : return error_mark_node;
14271 : : }
14272 : 24078337 : if (decl_context == FIELD)
14273 : 24078337 : staticp = 0;
14274 : : }
14275 : 147087206 : else if (friendp)
14276 : : {
14277 : 3880931 : if (virtualp)
14278 : : {
14279 : : /* Cannot be both friend and virtual. */
14280 : 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14281 : 12 : richloc.add_range (declspecs->locations[ds_friend]);
14282 : 12 : error_at (&richloc, "virtual functions cannot be friends");
14283 : 12 : friendp = 0;
14284 : 12 : }
14285 : 3880931 : if (decl_context == NORMAL)
14286 : 0 : error_at (declarator->id_loc,
14287 : : "friend declaration not in class definition");
14288 : 3880931 : if (current_function_decl && funcdef_flag)
14289 : : {
14290 : 9 : error_at (declarator->id_loc,
14291 : : "cannot define friend function %qs in a local "
14292 : : "class definition", name);
14293 : 9 : friendp = 0;
14294 : : }
14295 : : /* [class.friend]/6: A function can be defined in a friend
14296 : : declaration if the function name is unqualified. */
14297 : 3880931 : if (funcdef_flag && in_namespace)
14298 : : {
14299 : 6 : if (in_namespace == global_namespace)
14300 : 3 : error_at (declarator->id_loc,
14301 : : "friend function definition %qs cannot have "
14302 : : "a name qualified with %<::%>", name);
14303 : : else
14304 : 3 : error_at (declarator->id_loc,
14305 : : "friend function definition %qs cannot have "
14306 : : "a name qualified with %<%D::%>", name,
14307 : : in_namespace);
14308 : : }
14309 : : }
14310 : 143206275 : else if (ctype && sfk == sfk_conversion)
14311 : : {
14312 : 620431 : if (explicitp == 1)
14313 : : {
14314 : 295094 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
14315 : 295094 : explicitp = 2;
14316 : : }
14317 : : }
14318 : 142585844 : else if (sfk == sfk_deduction_guide)
14319 : : {
14320 : 1038538 : if (explicitp == 1)
14321 : 171165543 : explicitp = 2;
14322 : : }
14323 : :
14324 : 171165543 : if (xobj_parm)
14325 : : {
14326 : 26290 : if (!ctype
14327 : 26290 : && decl_context == NORMAL
14328 : 49 : && (in_namespace
14329 : 49 : || !declarator->declarator->u.id.qualifying_scope))
14330 : 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14331 : : "a non-member function cannot have "
14332 : : "an explicit object parameter");
14333 : : else
14334 : : {
14335 : 26286 : if (virtualp)
14336 : : {
14337 : 16 : auto_diagnostic_group d;
14338 : 16 : error_at (declspecs->locations[ds_virtual],
14339 : : "an explicit object member function cannot "
14340 : : "be %<virtual%>");
14341 : 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14342 : : "explicit object parameter declared here");
14343 : 16 : virtualp = false;
14344 : 16 : }
14345 : 26286 : if (staticp >= 2)
14346 : : {
14347 : 2 : auto_diagnostic_group d;
14348 : 2 : error_at (declspecs->locations[ds_storage_class],
14349 : : "an explicit object member function cannot "
14350 : : "be %<static%>");
14351 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14352 : : "explicit object parameter declared here");
14353 : 2 : }
14354 : 26286 : if (unqualified_id
14355 : 171191823 : && identifier_p (unqualified_id)
14356 : 52566 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14357 : 8 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14358 : : "%qD cannot be an explicit object member "
14359 : : "function", unqualified_id);
14360 : : }
14361 : : }
14362 : 171165543 : tree pushed_scope = NULL_TREE;
14363 : 171165543 : if (funcdecl_p
14364 : 171165543 : && decl_context != FIELD
14365 : 76605903 : && inner_declarator->u.id.qualifying_scope
14366 : 180170241 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
14367 : 9003379 : pushed_scope
14368 : 9003379 : = push_scope (inner_declarator->u.id.qualifying_scope);
14369 : :
14370 : 171165543 : arg_types = grokparms (declarator->u.function.parameters, &parms);
14371 : :
14372 : 171165543 : if (pushed_scope)
14373 : 9003379 : pop_scope (pushed_scope);
14374 : :
14375 : 171165543 : if (inner_declarator
14376 : 170687940 : && inner_declarator->kind == cdk_id
14377 : 168775327 : && inner_declarator->u.id.sfk == sfk_destructor
14378 : 3095685 : && arg_types != void_list_node)
14379 : : {
14380 : 20 : error_at (declarator->id_loc,
14381 : : "destructors may not have parameters");
14382 : 20 : arg_types = void_list_node;
14383 : 20 : parms = NULL_TREE;
14384 : 20 : is_xobj_member_function = false;
14385 : : }
14386 : :
14387 : 171165543 : type = build_function_type (type, arg_types);
14388 : :
14389 : 171165543 : tree attrs = declarator->std_attributes;
14390 : 171165543 : if (tx_qual)
14391 : : {
14392 : 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
14393 : : /* transaction_safe applies to the type, but
14394 : : transaction_safe_dynamic applies to the function. */
14395 : 242 : if (is_attribute_p ("transaction_safe", tx_qual))
14396 : 213 : attrs = attr_chainon (attrs, att);
14397 : : else
14398 : 29 : returned_attrs = attr_chainon (returned_attrs, att);
14399 : : }
14400 : :
14401 : : /* Actually apply the contract attributes to the declaration. */
14402 : 171166697 : for (tree *p = &attrs; *p;)
14403 : : {
14404 : 1154 : tree l = *p;
14405 : 1154 : if (cxx_contract_attribute_p (l))
14406 : : {
14407 : 667 : *p = TREE_CHAIN (l);
14408 : : /* Intentionally reverse order of contracts so they're
14409 : : reversed back into their lexical order. */
14410 : 667 : TREE_CHAIN (l) = NULL_TREE;
14411 : 667 : returned_attrs = chainon (l, returned_attrs);
14412 : : }
14413 : : else
14414 : 487 : p = &TREE_CHAIN (l);
14415 : : }
14416 : :
14417 : 171165543 : if (attrs)
14418 : : /* [dcl.fct]/2:
14419 : :
14420 : : The optional attribute-specifier-seq appertains to
14421 : : the function type. */
14422 : 475 : cplus_decl_attributes (&type, attrs, 0);
14423 : :
14424 : 171165543 : if (raises)
14425 : 66564999 : type = build_exception_variant (type, raises);
14426 : : }
14427 : 171165543 : break;
14428 : :
14429 : 176237462 : case cdk_pointer:
14430 : 176237462 : case cdk_reference:
14431 : 176237462 : case cdk_ptrmem:
14432 : : /* Filter out pointers-to-references and references-to-references.
14433 : : We can get these if a TYPE_DECL is used. */
14434 : :
14435 : 176237462 : if (TYPE_REF_P (type))
14436 : : {
14437 : 236 : if (declarator->kind != cdk_reference)
14438 : : {
14439 : 3 : error ("cannot declare pointer to %q#T", type);
14440 : 3 : type = TREE_TYPE (type);
14441 : : }
14442 : :
14443 : : /* In C++0x, we allow reference to reference declarations
14444 : : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
14445 : : and template type arguments [14.3.1/4 temp.arg.type]. The
14446 : : check for direct reference to reference declarations, which
14447 : : are still forbidden, occurs below. Reasoning behind the change
14448 : : can be found in DR106, DR540, and the rvalue reference
14449 : : proposals. */
14450 : 233 : else if (cxx_dialect == cxx98)
14451 : : {
14452 : 0 : error ("cannot declare reference to %q#T", type);
14453 : 0 : type = TREE_TYPE (type);
14454 : : }
14455 : : }
14456 : 176237226 : else if (VOID_TYPE_P (type))
14457 : : {
14458 : 4535884 : if (declarator->kind == cdk_reference)
14459 : 4 : error ("cannot declare reference to %q#T", type);
14460 : 4535880 : else if (declarator->kind == cdk_ptrmem)
14461 : 3 : error ("cannot declare pointer to %q#T member", type);
14462 : : }
14463 : :
14464 : : /* We now know that the TYPE_QUALS don't apply to the decl,
14465 : : but to the target of the pointer. */
14466 : 176237462 : type_quals = TYPE_UNQUALIFIED;
14467 : :
14468 : : /* This code used to handle METHOD_TYPE, but I don't think it's
14469 : : possible to get it here anymore. */
14470 : 176237462 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14471 : 176237462 : if (declarator->kind == cdk_ptrmem
14472 : 1173263 : && TREE_CODE (type) == FUNCTION_TYPE)
14473 : : {
14474 : 977678 : memfn_quals |= type_memfn_quals (type);
14475 : 1955356 : type = build_memfn_type (type,
14476 : 977678 : declarator->u.pointer.class_type,
14477 : : memfn_quals,
14478 : : rqual);
14479 : 977678 : if (type == error_mark_node)
14480 : : return error_mark_node;
14481 : :
14482 : : rqual = REF_QUAL_NONE;
14483 : : memfn_quals = TYPE_UNQUALIFIED;
14484 : : }
14485 : :
14486 : 176237459 : if (TREE_CODE (type) == FUNCTION_TYPE
14487 : 176237459 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14488 : 992104 : || type_memfn_rqual (type) != REF_QUAL_NONE))
14489 : 15 : error (declarator->kind == cdk_reference
14490 : : ? G_("cannot declare reference to qualified function type %qT")
14491 : : : G_("cannot declare pointer to qualified function type %qT"),
14492 : : type);
14493 : :
14494 : : /* When the pointed-to type involves components of variable size,
14495 : : care must be taken to ensure that the size evaluation code is
14496 : : emitted early enough to dominate all the possible later uses
14497 : : and late enough for the variables on which it depends to have
14498 : : been assigned.
14499 : :
14500 : : This is expected to happen automatically when the pointed-to
14501 : : type has a name/declaration of it's own, but special attention
14502 : : is required if the type is anonymous.
14503 : :
14504 : : We handle the NORMAL and FIELD contexts here by inserting a
14505 : : dummy statement that just evaluates the size at a safe point
14506 : : and ensures it is not deferred until e.g. within a deeper
14507 : : conditional context (c++/43555).
14508 : :
14509 : : We expect nothing to be needed here for PARM or TYPENAME.
14510 : : Evaluating the size at this point for TYPENAME would
14511 : : actually be incorrect, as we might be in the middle of an
14512 : : expression with side effects on the pointed-to type size
14513 : : "arguments" prior to the pointer declaration point and the
14514 : : size evaluation could end up prior to the side effects. */
14515 : :
14516 : 176237459 : if (!TYPE_NAME (type)
14517 : 5297114 : && (decl_context == NORMAL || decl_context == FIELD)
14518 : 600083 : && at_function_scope_p ()
14519 : 176278162 : && variably_modified_type_p (type, NULL_TREE))
14520 : : {
14521 : 96 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
14522 : : NULL_TREE, type);
14523 : 96 : add_decl_expr (TYPE_NAME (type));
14524 : : }
14525 : :
14526 : 176237459 : if (declarator->kind == cdk_reference)
14527 : : {
14528 : : /* In C++0x, the type we are creating a reference to might be
14529 : : a typedef which is itself a reference type. In that case,
14530 : : we follow the reference collapsing rules in
14531 : : [7.1.3/8 dcl.typedef] to create the final reference type:
14532 : :
14533 : : "If a typedef TD names a type that is a reference to a type
14534 : : T, an attempt to create the type 'lvalue reference to cv TD'
14535 : : creates the type 'lvalue reference to T,' while an attempt
14536 : : to create the type "rvalue reference to cv TD' creates the
14537 : : type TD."
14538 : : */
14539 : 115710294 : if (VOID_TYPE_P (type))
14540 : : /* We already gave an error. */;
14541 : 115710290 : else if (TYPE_REF_P (type))
14542 : : {
14543 : 233 : if (declarator->u.reference.rvalue_ref)
14544 : : /* Leave type alone. */;
14545 : : else
14546 : 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
14547 : : }
14548 : : else
14549 : 115710057 : type = cp_build_reference_type
14550 : 115710057 : (type, declarator->u.reference.rvalue_ref);
14551 : :
14552 : : /* In C++0x, we need this check for direct reference to
14553 : : reference declarations, which are forbidden by
14554 : : [8.3.2/5 dcl.ref]. Reference to reference declarations
14555 : : are only allowed indirectly through typedefs and template
14556 : : type arguments. Example:
14557 : :
14558 : : void foo(int & &); // invalid ref-to-ref decl
14559 : :
14560 : : typedef int & int_ref;
14561 : : void foo(int_ref &); // valid ref-to-ref decl
14562 : : */
14563 : 115710294 : if (inner_declarator && inner_declarator->kind == cdk_reference)
14564 : 0 : error ("cannot declare reference to %q#T, which is not "
14565 : : "a typedef or a template type argument", type);
14566 : : }
14567 : 60527165 : else if (TREE_CODE (type) == METHOD_TYPE)
14568 : 977675 : type = build_ptrmemfunc_type (build_pointer_type (type));
14569 : 59549490 : else if (declarator->kind == cdk_ptrmem)
14570 : : {
14571 : 195585 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
14572 : : != NAMESPACE_DECL);
14573 : 195585 : if (declarator->u.pointer.class_type == error_mark_node)
14574 : : /* We will already have complained. */
14575 : 3 : type = error_mark_node;
14576 : : else
14577 : 195582 : type = build_ptrmem_type (declarator->u.pointer.class_type,
14578 : : type);
14579 : : }
14580 : : else
14581 : 59353905 : type = build_pointer_type (type);
14582 : :
14583 : : /* Process a list of type modifier keywords (such as
14584 : : const or volatile) that were given inside the `*' or `&'. */
14585 : :
14586 : 176237459 : if (declarator->u.pointer.qualifiers)
14587 : : {
14588 : 8250121 : type
14589 : 8250121 : = cp_build_qualified_type (type,
14590 : : declarator->u.pointer.qualifiers);
14591 : 8250121 : type_quals = cp_type_quals (type);
14592 : : }
14593 : :
14594 : : /* Apply C++11 attributes to the pointer, and not to the
14595 : : type pointed to. This is unlike what is done for GNU
14596 : : attributes above. It is to comply with [dcl.ptr]/1:
14597 : :
14598 : : [the optional attribute-specifier-seq (7.6.1) appertains
14599 : : to the pointer and not to the object pointed to]. */
14600 : 176237459 : if (declarator->std_attributes)
14601 : 157 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
14602 : :
14603 : : ctype = NULL_TREE;
14604 : : break;
14605 : :
14606 : : case cdk_error:
14607 : : break;
14608 : :
14609 : 0 : default:
14610 : 0 : gcc_unreachable ();
14611 : : }
14612 : : }
14613 : :
14614 : 898063752 : id_loc = declarator ? declarator->id_loc : input_location;
14615 : :
14616 : 898063752 : if (innermost_code != cdk_function
14617 : : /* Don't check this if it can be the artifical decltype(auto)
14618 : : we created when building a constraint in a compound-requirement:
14619 : : that the type-constraint is plain is going to be checked in
14620 : : cp_parser_compound_requirement. */
14621 : 898063752 : && decl_context != TYPENAME
14622 : 898063752 : && check_decltype_auto (id_loc, type))
14623 : 19 : return error_mark_node;
14624 : :
14625 : : /* A `constexpr' specifier used in an object declaration declares
14626 : : the object as `const'. */
14627 : 898063733 : if (constexpr_p && innermost_code != cdk_function)
14628 : : {
14629 : : /* DR1688 says that a `constexpr' specifier in combination with
14630 : : `volatile' is valid. */
14631 : :
14632 : 13009198 : if (!TYPE_REF_P (type))
14633 : : {
14634 : 12999408 : type_quals |= TYPE_QUAL_CONST;
14635 : 12999408 : type = cp_build_qualified_type (type, type_quals);
14636 : : }
14637 : : }
14638 : :
14639 : 519707551 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
14640 : 1867005 : && !FUNC_OR_METHOD_TYPE_P (type)
14641 : 899062117 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
14642 : : {
14643 : 0 : error ("template-id %qD used as a declarator",
14644 : : unqualified_id);
14645 : 0 : unqualified_id = dname;
14646 : : }
14647 : :
14648 : : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
14649 : : qualified with a class-name, turn it into a METHOD_TYPE, unless
14650 : : we know that the function is static. We take advantage of this
14651 : : opportunity to do other processing that pertains to entities
14652 : : explicitly declared to be class members. Note that if DECLARATOR
14653 : : is non-NULL, we know it is a cdk_id declarator; otherwise, we
14654 : : would not have exited the loop above. */
14655 : 898063733 : if (declarator
14656 : 519785593 : && declarator->kind == cdk_id
14657 : 519707551 : && declarator->u.id.qualifying_scope
14658 : 907598224 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
14659 : : {
14660 : 9384998 : ctype = declarator->u.id.qualifying_scope;
14661 : 9384998 : ctype = TYPE_MAIN_VARIANT (ctype);
14662 : 9384998 : template_count = num_template_headers_for_class (ctype);
14663 : :
14664 : 9384998 : if (ctype == current_class_type)
14665 : : {
14666 : 54 : if (friendp)
14667 : : {
14668 : 15 : permerror (declspecs->locations[ds_friend],
14669 : : "member functions are implicitly "
14670 : : "friends of their class");
14671 : 15 : friendp = 0;
14672 : : }
14673 : : else
14674 : 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
14675 : : ctype, name);
14676 : : }
14677 : 9384944 : else if (/* If the qualifying type is already complete, then we
14678 : : can skip the following checks. */
14679 : 9384944 : !COMPLETE_TYPE_P (ctype)
14680 : 150 : && (/* If the function is being defined, then
14681 : : qualifying type must certainly be complete. */
14682 : : funcdef_flag
14683 : : /* A friend declaration of "T::f" is OK, even if
14684 : : "T" is a template parameter. But, if this
14685 : : function is not a friend, the qualifying type
14686 : : must be a class. */
14687 : 111 : || (!friendp && !CLASS_TYPE_P (ctype))
14688 : : /* For a declaration, the type need not be
14689 : : complete, if either it is dependent (since there
14690 : : is no meaningful definition of complete in that
14691 : : case) or the qualifying class is currently being
14692 : : defined. */
14693 : 123 : || !(dependent_type_p (ctype)
14694 : 24 : || currently_open_class (ctype)))
14695 : : /* Check that the qualifying type is complete. */
14696 : 9385010 : && !complete_type_or_else (ctype, NULL_TREE))
14697 : 66 : return error_mark_node;
14698 : 9384878 : else if (TREE_CODE (type) == FUNCTION_TYPE)
14699 : : {
14700 : 9003616 : if (current_class_type
14701 : 264 : && (!friendp || funcdef_flag || initialized))
14702 : : {
14703 : 59 : error_at (id_loc, funcdef_flag || initialized
14704 : : ? G_("cannot define member function %<%T::%s%> "
14705 : : "within %qT")
14706 : : : G_("cannot declare member function %<%T::%s%> "
14707 : : "within %qT"),
14708 : : ctype, name, current_class_type);
14709 : 49 : return error_mark_node;
14710 : : }
14711 : : }
14712 : 381262 : else if (typedef_p && current_class_type)
14713 : : {
14714 : 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
14715 : : ctype, name, current_class_type);
14716 : 0 : return error_mark_node;
14717 : : }
14718 : : }
14719 : :
14720 : 898063618 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
14721 : 49990522 : ctype = current_class_type;
14722 : :
14723 : : /* Now TYPE has the actual type. */
14724 : :
14725 : 898063618 : if (returned_attrs)
14726 : : {
14727 : 3336 : if (attrlist)
14728 : 3336 : *attrlist = attr_chainon (returned_attrs, *attrlist);
14729 : : else
14730 : : attrlist = &returned_attrs;
14731 : : }
14732 : :
14733 : 898063618 : if (declarator
14734 : 519785478 : && declarator->kind == cdk_id
14735 : 519707436 : && declarator->std_attributes
14736 : 352881 : && attrlist != NULL)
14737 : : {
14738 : : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
14739 : : a declarator-id appertains to the entity that is declared. */
14740 : 352878 : if (declarator->std_attributes != error_mark_node)
14741 : 352860 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
14742 : : else
14743 : : /* We should have already diagnosed the issue (c++/78344). */
14744 : 18 : gcc_assert (seen_error ());
14745 : : }
14746 : :
14747 : : /* Handle parameter packs. */
14748 : 898063618 : if (parameter_pack_p)
14749 : : {
14750 : 2869793 : if (decl_context == PARM)
14751 : : /* Turn the type into a pack expansion.*/
14752 : 2869793 : type = make_pack_expansion (type);
14753 : : else
14754 : 0 : error ("non-parameter %qs cannot be a parameter pack", name);
14755 : : }
14756 : :
14757 : 898063618 : if ((decl_context == FIELD || decl_context == PARM)
14758 : 411779872 : && !processing_template_decl
14759 : 1028511786 : && variably_modified_type_p (type, NULL_TREE))
14760 : : {
14761 : 12 : if (decl_context == FIELD)
14762 : 6 : error_at (id_loc,
14763 : : "data member may not have variably modified type %qT", type);
14764 : : else
14765 : 6 : error_at (id_loc,
14766 : : "parameter may not have variably modified type %qT", type);
14767 : 12 : type = error_mark_node;
14768 : : }
14769 : :
14770 : 898063618 : if (explicitp == 1 || (explicitp && friendp))
14771 : : {
14772 : : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
14773 : : in the declaration of a constructor or conversion function within
14774 : : a class definition. */
14775 : 28 : if (!current_class_type)
14776 : 13 : error_at (declspecs->locations[ds_explicit],
14777 : : "%<explicit%> outside class declaration");
14778 : 15 : else if (friendp)
14779 : 12 : error_at (declspecs->locations[ds_explicit],
14780 : : "%<explicit%> in friend declaration");
14781 : : else
14782 : 3 : error_at (declspecs->locations[ds_explicit],
14783 : : "only declarations of constructors and conversion operators "
14784 : : "can be %<explicit%>");
14785 : : explicitp = 0;
14786 : : }
14787 : :
14788 : 898063618 : if (storage_class == sc_mutable)
14789 : : {
14790 : 143702 : location_t sloc = declspecs->locations[ds_storage_class];
14791 : 143702 : if (decl_context != FIELD || friendp)
14792 : : {
14793 : 12 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
14794 : : name);
14795 : 12 : storage_class = sc_none;
14796 : : }
14797 : 143690 : else if (decl_context == TYPENAME || typedef_p)
14798 : : {
14799 : 0 : error_at (sloc,
14800 : : "non-object member %qs cannot be declared %<mutable%>",
14801 : : name);
14802 : 0 : storage_class = sc_none;
14803 : : }
14804 : 143690 : else if (FUNC_OR_METHOD_TYPE_P (type))
14805 : : {
14806 : 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
14807 : : name);
14808 : 3 : storage_class = sc_none;
14809 : : }
14810 : 143687 : else if (staticp)
14811 : : {
14812 : 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
14813 : : name);
14814 : 0 : storage_class = sc_none;
14815 : : }
14816 : 143687 : else if (type_quals & TYPE_QUAL_CONST)
14817 : : {
14818 : 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
14819 : : name);
14820 : 9 : storage_class = sc_none;
14821 : : }
14822 : 143678 : else if (TYPE_REF_P (type))
14823 : : {
14824 : 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
14825 : : name);
14826 : 6 : storage_class = sc_none;
14827 : : }
14828 : : }
14829 : :
14830 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
14831 : 898063618 : if (typedef_p && decl_context != TYPENAME)
14832 : : {
14833 : 42894481 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
14834 : 42894481 : tree decl;
14835 : :
14836 : 42894481 : if (funcdef_flag)
14837 : : {
14838 : 6 : if (decl_context == NORMAL)
14839 : 3 : error_at (id_loc,
14840 : : "typedef may not be a function definition");
14841 : : else
14842 : 3 : error_at (id_loc,
14843 : : "typedef may not be a member function definition");
14844 : 6 : return error_mark_node;
14845 : : }
14846 : :
14847 : : /* This declaration:
14848 : :
14849 : : typedef void f(int) const;
14850 : :
14851 : : declares a function type which is not a member of any
14852 : : particular class, but which is cv-qualified; for
14853 : : example "f S::*" declares a pointer to a const-qualified
14854 : : member function of S. We record the cv-qualification in the
14855 : : function type. */
14856 : 42894475 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
14857 : : {
14858 : 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
14859 : :
14860 : : /* We have now dealt with these qualifiers. */
14861 : 62 : memfn_quals = TYPE_UNQUALIFIED;
14862 : 62 : rqual = REF_QUAL_NONE;
14863 : : }
14864 : :
14865 : 42894475 : if (type_uses_auto (type))
14866 : : {
14867 : 41 : if (alias_p)
14868 : 19 : error_at (declspecs->locations[ds_type_spec],
14869 : : "%<auto%> not allowed in alias declaration");
14870 : : else
14871 : 22 : error_at (declspecs->locations[ds_type_spec],
14872 : : "typedef declared %<auto%>");
14873 : 41 : type = error_mark_node;
14874 : : }
14875 : :
14876 : 42894475 : if (reqs)
14877 : 3 : error_at (location_of (reqs), "requires-clause on typedef");
14878 : :
14879 : 42894475 : if (id_declarator && declarator->u.id.qualifying_scope)
14880 : : {
14881 : 22 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
14882 : 22 : type = error_mark_node;
14883 : : }
14884 : :
14885 : 42894475 : if (decl_context == FIELD)
14886 : 23309185 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
14887 : : else
14888 : 19585290 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
14889 : :
14890 : 42894475 : if (decl_context != FIELD)
14891 : : {
14892 : 19585290 : if (!current_function_decl)
14893 : 8217773 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
14894 : 11367517 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
14895 : : /* The TYPE_DECL is "abstract" because there will be
14896 : : clones of this constructor/destructor, and there will
14897 : : be copies of this TYPE_DECL generated in those
14898 : : clones. The decloning optimization (for space) may
14899 : : revert this subsequently if it determines that
14900 : : the clones should share a common implementation. */
14901 : 153511 : DECL_ABSTRACT_P (decl) = true;
14902 : :
14903 : 19585290 : set_originating_module (decl);
14904 : : }
14905 : 23309185 : else if (current_class_type
14906 : 23309185 : && constructor_name_p (unqualified_id, current_class_type))
14907 : 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
14908 : : "as enclosing class",
14909 : : unqualified_id);
14910 : :
14911 : : /* If the user declares "typedef struct {...} foo" then the
14912 : : struct will have an anonymous name. Fill that name in now.
14913 : : Nothing can refer to it, so nothing needs know about the name
14914 : : change. */
14915 : 42894475 : if (type != error_mark_node
14916 : 42894412 : && unqualified_id
14917 : 42894409 : && TYPE_NAME (type)
14918 : 40061650 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14919 : 80203341 : && TYPE_UNNAMED_P (type)
14920 : 384494 : && declspecs->type_definition_p
14921 : 384424 : && attributes_naming_typedef_ok (*attrlist)
14922 : 43278896 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
14923 : 384403 : name_unnamed_type (type, decl);
14924 : :
14925 : 42894475 : if (signed_p
14926 : 42894475 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
14927 : 370819 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
14928 : :
14929 : 42894475 : bad_specifiers (decl, BSP_TYPE, virtualp,
14930 : : memfn_quals != TYPE_UNQUALIFIED,
14931 : : inlinep, friendp, raises != NULL_TREE,
14932 : : declspecs->locations);
14933 : :
14934 : 42894475 : if (alias_p)
14935 : : /* Acknowledge that this was written:
14936 : : `using analias = atype;'. */
14937 : 16251711 : TYPE_DECL_ALIAS_P (decl) = 1;
14938 : :
14939 : 42894475 : return decl;
14940 : : }
14941 : :
14942 : : /* Detect the case of an array type of unspecified size
14943 : : which came, as such, direct from a typedef name.
14944 : : We must copy the type, so that the array's domain can be
14945 : : individually set by the object's initializer. */
14946 : :
14947 : 855169137 : if (type && typedef_type
14948 : 828474843 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
14949 : 855676349 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
14950 : 79 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14951 : :
14952 : : /* Detect where we're using a typedef of function type to declare a
14953 : : function. PARMS will not be set, so we must create it now. */
14954 : :
14955 : 855169137 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
14956 : : {
14957 : 14996 : tree decls = NULL_TREE;
14958 : 14996 : tree args;
14959 : :
14960 : 14996 : for (args = TYPE_ARG_TYPES (type);
14961 : 32458 : args && args != void_list_node;
14962 : 17462 : args = TREE_CHAIN (args))
14963 : : {
14964 : 17462 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
14965 : 17462 : TREE_VALUE (args));
14966 : :
14967 : 17462 : DECL_CHAIN (decl) = decls;
14968 : 17462 : decls = decl;
14969 : : }
14970 : :
14971 : 14996 : parms = nreverse (decls);
14972 : :
14973 : 14996 : if (decl_context != TYPENAME)
14974 : : {
14975 : : /* The qualifiers on the function type become the qualifiers on
14976 : : the non-static member function. */
14977 : 14312 : memfn_quals |= type_memfn_quals (type);
14978 : 14312 : rqual = type_memfn_rqual (type);
14979 : 14312 : type_quals = TYPE_UNQUALIFIED;
14980 : 14312 : raises = TYPE_RAISES_EXCEPTIONS (type);
14981 : : }
14982 : : }
14983 : :
14984 : : /* If this is a type name (such as, in a cast or sizeof),
14985 : : compute the type and return it now. */
14986 : :
14987 : 855168453 : if (decl_context == TYPENAME)
14988 : : {
14989 : : /* Note that here we don't care about type_quals. */
14990 : :
14991 : : /* Special case: "friend class foo" looks like a TYPENAME context. */
14992 : 335099870 : if (friendp)
14993 : : {
14994 : 0 : if (inlinep)
14995 : : {
14996 : 0 : error ("%<inline%> specified for friend class declaration");
14997 : 0 : inlinep = 0;
14998 : : }
14999 : :
15000 : 0 : if (!current_aggr)
15001 : : {
15002 : : /* Don't allow friend declaration without a class-key. */
15003 : 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15004 : 0 : permerror (input_location, "template parameters cannot be friends");
15005 : 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
15006 : 0 : permerror (input_location, "friend declaration requires class-key, "
15007 : : "i.e. %<friend class %T::%D%>",
15008 : 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
15009 : : else
15010 : 0 : permerror (input_location, "friend declaration requires class-key, "
15011 : : "i.e. %<friend %#T%>",
15012 : : type);
15013 : : }
15014 : :
15015 : : /* Only try to do this stuff if we didn't already give up. */
15016 : 0 : if (type != integer_type_node)
15017 : : {
15018 : : /* A friendly class? */
15019 : 0 : if (current_class_type)
15020 : 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
15021 : : /*complain=*/true);
15022 : : else
15023 : 0 : error ("trying to make class %qT a friend of global scope",
15024 : : type);
15025 : :
15026 : 0 : type = void_type_node;
15027 : : }
15028 : : }
15029 : 335099870 : else if (memfn_quals || rqual)
15030 : : {
15031 : 72002 : if (ctype == NULL_TREE
15032 : 72002 : && TREE_CODE (type) == METHOD_TYPE)
15033 : 0 : ctype = TYPE_METHOD_BASETYPE (type);
15034 : :
15035 : 72002 : if (ctype)
15036 : 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
15037 : : /* Core issue #547: need to allow this in template type args.
15038 : : Allow it in general in C++11 for alias-declarations. */
15039 : 72002 : else if ((template_type_arg || cxx_dialect >= cxx11)
15040 : 72000 : && TREE_CODE (type) == FUNCTION_TYPE)
15041 : 71998 : type = apply_memfn_quals (type, memfn_quals, rqual);
15042 : : else
15043 : 4 : error ("invalid qualifiers on non-member function type");
15044 : : }
15045 : :
15046 : 335099870 : if (reqs)
15047 : 3 : error_at (location_of (reqs), "requires-clause on type-id");
15048 : :
15049 : 335099870 : return type;
15050 : : }
15051 : 520069267 : else if (unqualified_id == NULL_TREE && decl_context != PARM
15052 : 688754 : && decl_context != CATCHPARM
15053 : 212236 : && TREE_CODE (type) != UNION_TYPE
15054 : 212000 : && ! bitfield
15055 : 212000 : && innermost_code != cdk_decomp)
15056 : : {
15057 : 9 : error ("abstract declarator %qT used as declaration", type);
15058 : 9 : return error_mark_node;
15059 : : }
15060 : :
15061 : 520069258 : if (!FUNC_OR_METHOD_TYPE_P (type))
15062 : : {
15063 : : /* Only functions may be declared using an operator-function-id. */
15064 : 351331771 : if (dname && IDENTIFIER_ANY_OP_P (dname))
15065 : : {
15066 : 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
15067 : 8 : return error_mark_node;
15068 : : }
15069 : :
15070 : 351331763 : if (reqs)
15071 : 15 : error_at (location_of (reqs),
15072 : : "requires-clause on declaration of non-function type %qT",
15073 : : type);
15074 : : }
15075 : :
15076 : : /* We don't check parameter types here because we can emit a better
15077 : : error message later. */
15078 : 520069250 : if (decl_context != PARM)
15079 : : {
15080 : 250449287 : type = check_var_type (unqualified_id, type, id_loc);
15081 : 250449287 : if (type == error_mark_node)
15082 : : return error_mark_node;
15083 : : }
15084 : :
15085 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
15086 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
15087 : :
15088 : 520069168 : if (decl_context == PARM || decl_context == CATCHPARM)
15089 : : {
15090 : 270108816 : if (ctype || in_namespace)
15091 : 0 : error ("cannot use %<::%> in parameter declaration");
15092 : :
15093 : 270108816 : tree auto_node = type_uses_auto (type);
15094 : 270108816 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
15095 : : {
15096 : 119 : bool err_p = true;
15097 : 119 : if (cxx_dialect >= cxx14)
15098 : : {
15099 : 210 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
15100 : 3 : error_at (typespec_loc,
15101 : : "cannot declare a parameter with %<decltype(auto)%>");
15102 : 108 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15103 : : {
15104 : 4 : auto_diagnostic_group g;
15105 : 4 : gcc_rich_location richloc (typespec_loc);
15106 : 4 : richloc.add_fixit_insert_after ("<>");
15107 : 4 : error_at (&richloc,
15108 : : "missing template argument list after %qE; template "
15109 : : "placeholder not permitted in parameter", c);
15110 : 4 : if (decl_context == PARM && cxx_dialect >= cxx20)
15111 : 3 : inform (typespec_loc, "or use %<auto%> for an "
15112 : : "abbreviated function template");
15113 : 4 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
15114 : 4 : }
15115 : 104 : else if (decl_context == CATCHPARM || template_parm_flag)
15116 : 14 : error_at (typespec_loc,
15117 : : "%<auto%> parameter not permitted in this context");
15118 : : else
15119 : : /* Do not issue an error while tentatively parsing a function
15120 : : parameter: for T t(auto(a), 42);, when we just saw the 1st
15121 : : parameter, we don't know yet that this construct won't be
15122 : : a function declaration. Defer the checking to
15123 : : cp_parser_parameter_declaration_clause. */
15124 : : err_p = false;
15125 : : }
15126 : : else
15127 : 8 : error_at (typespec_loc, "parameter declared %<auto%>");
15128 : 29 : if (err_p)
15129 : 29 : type = error_mark_node;
15130 : : }
15131 : :
15132 : : /* A parameter declared as an array of T is really a pointer to T.
15133 : : One declared as a function is really a pointer to a function.
15134 : : One declared as a member is really a pointer to member. */
15135 : :
15136 : 270108816 : if (TREE_CODE (type) == ARRAY_TYPE)
15137 : : {
15138 : : /* Transfer const-ness of array into that of type pointed to. */
15139 : 721934 : type = build_pointer_type (TREE_TYPE (type));
15140 : 721934 : type_quals = TYPE_UNQUALIFIED;
15141 : 721934 : array_parameter_p = true;
15142 : : }
15143 : 269386882 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15144 : 105420 : type = build_pointer_type (type);
15145 : : }
15146 : :
15147 : 124354615 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
15148 : : /* Don't convert xobj member functions to METHOD_TYPE. */
15149 : 97291893 : && !is_xobj_member_function
15150 : 609070361 : && !(unqualified_id
15151 : 177990344 : && identifier_p (unqualified_id)
15152 : 89000881 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
15153 : : {
15154 : 88989463 : cp_cv_quals real_quals = memfn_quals;
15155 : 88989463 : if (cxx_dialect < cxx14 && constexpr_p
15156 : 628260 : && sfk != sfk_constructor && sfk != sfk_destructor)
15157 : 3758 : real_quals |= TYPE_QUAL_CONST;
15158 : 88989463 : type = build_memfn_type (type, ctype, real_quals, rqual);
15159 : : }
15160 : :
15161 : 520069168 : {
15162 : 520069168 : tree decl = NULL_TREE;
15163 : :
15164 : 520069168 : if (decl_context == PARM)
15165 : : {
15166 : 269619963 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
15167 : 269619963 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
15168 : :
15169 : 269619963 : bad_specifiers (decl, BSP_PARM, virtualp,
15170 : : memfn_quals != TYPE_UNQUALIFIED,
15171 : : inlinep, friendp, raises != NULL_TREE,
15172 : : declspecs->locations);
15173 : : }
15174 : 250449205 : else if (decl_context == FIELD)
15175 : : {
15176 : 118850705 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
15177 : 16754534 : if (tree auto_node = type_uses_auto (type))
15178 : : {
15179 : 19 : location_t tloc = declspecs->locations[ds_type_spec];
15180 : 19 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15181 : 4 : error_at (tloc, "invalid use of template-name %qE without an "
15182 : : "argument list",
15183 : 4 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
15184 : : else
15185 : 15 : error_at (tloc, "non-static data member declared with "
15186 : : "placeholder %qT", auto_node);
15187 : 19 : type = error_mark_node;
15188 : : }
15189 : :
15190 : : /* The C99 flexible array extension. */
15191 : 100659333 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
15192 : 119932664 : && TYPE_DOMAIN (type) == NULL_TREE)
15193 : : {
15194 : 1396 : if (ctype
15195 : 1396 : && (TREE_CODE (ctype) == UNION_TYPE
15196 : 1396 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
15197 : 84 : pedwarn (id_loc, OPT_Wpedantic,
15198 : : "flexible array member in union is a GCC extension");
15199 : :
15200 : : else
15201 : : {
15202 : : /* Array is a flexible member. */
15203 : 1312 : if (name)
15204 : 1312 : pedwarn (id_loc, OPT_Wpedantic,
15205 : : "ISO C++ forbids flexible array member %qs", name);
15206 : : else
15207 : : pedwarn (input_location, OPT_Wpedantic,
15208 : : "ISO C++ forbids flexible array members");
15209 : :
15210 : : /* Flexible array member has a null domain. */
15211 : 1312 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15212 : : }
15213 : : }
15214 : :
15215 : 118850705 : if (type == error_mark_node)
15216 : : {
15217 : : /* Happens when declaring arrays of sizes which
15218 : : are error_mark_node, for example. */
15219 : 19 : decl = NULL_TREE;
15220 : : }
15221 : 118850686 : else if (in_namespace && !friendp)
15222 : : {
15223 : : /* Something like struct S { int N::j; }; */
15224 : 6 : error_at (id_loc, "invalid use of %<::%>");
15225 : 6 : return error_mark_node;
15226 : : }
15227 : 118850680 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
15228 : : {
15229 : 92169200 : int publicp = 0;
15230 : 92169200 : tree function_context;
15231 : :
15232 : 92169200 : if (friendp == 0)
15233 : : {
15234 : : /* This should never happen in pure C++ (the check
15235 : : could be an assert). It could happen in
15236 : : Objective-C++ if someone writes invalid code that
15237 : : uses a function declaration for an instance
15238 : : variable or property (instance variables and
15239 : : properties are parsed as FIELD_DECLs, but they are
15240 : : part of an Objective-C class, not a C++ class).
15241 : : That code is invalid and is caught by this
15242 : : check. */
15243 : 88288326 : if (!ctype)
15244 : : {
15245 : 0 : error ("declaration of function %qD in invalid context",
15246 : : unqualified_id);
15247 : 0 : return error_mark_node;
15248 : : }
15249 : :
15250 : : /* ``A union may [ ... ] not [ have ] virtual functions.''
15251 : : ARM 9.5 */
15252 : 88288326 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
15253 : : {
15254 : 3 : error_at (declspecs->locations[ds_virtual],
15255 : : "function %qD declared %<virtual%> inside a union",
15256 : : unqualified_id);
15257 : 3 : return error_mark_node;
15258 : : }
15259 : :
15260 : 3989614 : if (virtualp
15261 : 96158811 : && identifier_p (unqualified_id)
15262 : 3989614 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15263 : : {
15264 : 9 : error_at (declspecs->locations[ds_virtual],
15265 : : "%qD cannot be declared %<virtual%>, since it "
15266 : : "is always static", unqualified_id);
15267 : 9 : virtualp = 0;
15268 : : }
15269 : : }
15270 : :
15271 : : /* Check that the name used for a destructor makes sense. */
15272 : 92169197 : if (sfk == sfk_destructor)
15273 : : {
15274 : 2925516 : tree uqname = id_declarator->u.id.unqualified_name;
15275 : :
15276 : 2925516 : if (!ctype)
15277 : : {
15278 : 12 : gcc_assert (friendp);
15279 : 12 : error_at (id_loc, "expected qualified name in friend "
15280 : : "declaration for destructor %qD", uqname);
15281 : 12 : return error_mark_node;
15282 : : }
15283 : :
15284 : 2925504 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
15285 : : {
15286 : 9 : error_at (id_loc, "declaration of %qD as member of %qT",
15287 : : uqname, ctype);
15288 : 9 : return error_mark_node;
15289 : : }
15290 : 2925495 : if (concept_p)
15291 : : {
15292 : 9 : error_at (declspecs->locations[ds_concept],
15293 : : "a destructor cannot be %qs", "concept");
15294 : 9 : return error_mark_node;
15295 : : }
15296 : 2925486 : if (constexpr_p && cxx_dialect < cxx20)
15297 : : {
15298 : 6 : error_at (declspecs->locations[ds_constexpr],
15299 : : "%<constexpr%> destructors only available"
15300 : : " with %<-std=c++20%> or %<-std=gnu++20%>");
15301 : 6 : return error_mark_node;
15302 : : }
15303 : 2925480 : if (consteval_p)
15304 : : {
15305 : 3 : error_at (declspecs->locations[ds_consteval],
15306 : : "a destructor cannot be %qs", "consteval");
15307 : 3 : return error_mark_node;
15308 : : }
15309 : : }
15310 : 89243681 : else if (sfk == sfk_constructor && friendp && !ctype)
15311 : : {
15312 : 0 : error ("expected qualified name in friend declaration "
15313 : : "for constructor %qD",
15314 : 0 : id_declarator->u.id.unqualified_name);
15315 : 0 : return error_mark_node;
15316 : : }
15317 : 92169158 : if (sfk == sfk_constructor)
15318 : 21152881 : if (concept_p)
15319 : : {
15320 : 9 : error_at (declspecs->locations[ds_concept],
15321 : : "a constructor cannot be %<concept%>");
15322 : 9 : return error_mark_node;
15323 : : }
15324 : 71016277 : if (concept_p)
15325 : : {
15326 : 9 : error_at (declspecs->locations[ds_concept],
15327 : : "a concept cannot be a member function");
15328 : 9 : concept_p = false;
15329 : : }
15330 : 92169140 : else if (consteval_p
15331 : 92349473 : && identifier_p (unqualified_id)
15332 : 92349464 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15333 : : {
15334 : 9 : error_at (declspecs->locations[ds_consteval],
15335 : : "%qD cannot be %qs", unqualified_id, "consteval");
15336 : 9 : consteval_p = false;
15337 : : }
15338 : :
15339 : 92169149 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15340 : : {
15341 : 14111 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
15342 : 14111 : if (variable_template_p (tmpl))
15343 : : {
15344 : 3 : auto_diagnostic_group d;
15345 : 3 : error_at (id_loc, "specialization of variable template "
15346 : : "%qD declared as function", tmpl);
15347 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15348 : : "variable template declared here");
15349 : 3 : return error_mark_node;
15350 : 3 : }
15351 : : }
15352 : :
15353 : : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
15354 : 92169146 : function_context
15355 : : = (ctype != NULL_TREE
15356 : 92169146 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
15357 : 184338292 : publicp = ((! friendp || ! staticp)
15358 : 92169146 : && function_context == NULL_TREE);
15359 : :
15360 : 92169146 : decl = grokfndecl (ctype, type,
15361 : 92169146 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
15362 : : ? unqualified_id : dname,
15363 : : parms,
15364 : : unqualified_id,
15365 : : declspecs,
15366 : : reqs,
15367 : : virtualp, flags, memfn_quals, rqual, raises,
15368 : : friendp ? -1 : 0, friendp, publicp,
15369 : 92169146 : inlinep | (2 * constexpr_p) | (4 * concept_p)
15370 : 92169146 : | (8 * consteval_p),
15371 : : initialized == SD_DELETED,
15372 : : is_xobj_member_function, sfk,
15373 : : funcdef_flag, late_return_type_p,
15374 : : template_count, in_namespace,
15375 : : attrlist, id_loc);
15376 : 92169146 : decl = set_virt_specifiers (decl, virt_specifiers);
15377 : 92169146 : if (decl == NULL_TREE)
15378 : 98 : return error_mark_node;
15379 : : #if 0
15380 : : /* This clobbers the attrs stored in `decl' from `attrlist'. */
15381 : : /* The decl and setting of decl_attr is also turned off. */
15382 : : decl = build_decl_attribute_variant (decl, decl_attr);
15383 : : #endif
15384 : :
15385 : : /* [class.conv.ctor]
15386 : :
15387 : : A constructor declared without the function-specifier
15388 : : explicit that can be called with a single parameter
15389 : : specifies a conversion from the type of its first
15390 : : parameter to the type of its class. Such a constructor
15391 : : is called a converting constructor. */
15392 : 92169048 : if (explicitp == 2)
15393 : 5385099 : DECL_NONCONVERTING_P (decl) = 1;
15394 : :
15395 : 92169048 : if (declspecs->explicit_specifier)
15396 : 148086 : store_explicit_specifier (decl, declspecs->explicit_specifier);
15397 : : }
15398 : 26681480 : else if (!staticp
15399 : 26681480 : && ((current_class_type
15400 : 16754532 : && same_type_p (TYPE_MAIN_VARIANT (type),
15401 : : current_class_type))
15402 : 16754436 : || (!dependent_type_p (type)
15403 : 10330879 : && !COMPLETE_TYPE_P (complete_type (type))
15404 : 1438 : && (!complete_or_array_type_p (type)
15405 : 1369 : || initialized == SD_UNINITIALIZED))))
15406 : : {
15407 : 1501 : if (TREE_CODE (type) != ARRAY_TYPE
15408 : 1501 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
15409 : : {
15410 : 165 : if (unqualified_id)
15411 : : {
15412 : 120 : auto_diagnostic_group d;
15413 : 120 : error_at (id_loc, "field %qD has incomplete type %qT",
15414 : : unqualified_id, type);
15415 : 120 : cxx_incomplete_type_inform (strip_array_types (type));
15416 : 120 : }
15417 : : else
15418 : 45 : error ("name %qT has incomplete type", type);
15419 : :
15420 : 165 : type = error_mark_node;
15421 : 165 : decl = NULL_TREE;
15422 : : }
15423 : : }
15424 : 43432998 : else if (!verify_type_context (input_location,
15425 : : staticp
15426 : : ? TCTX_STATIC_STORAGE
15427 : : : TCTX_FIELD, type))
15428 : : {
15429 : 0 : type = error_mark_node;
15430 : 0 : decl = NULL_TREE;
15431 : : }
15432 : : else
15433 : : {
15434 : 26679973 : if (friendp)
15435 : : {
15436 : 14 : if (unqualified_id)
15437 : 11 : error_at (id_loc,
15438 : : "%qE is neither function nor member function; "
15439 : : "cannot be declared friend", unqualified_id);
15440 : : else
15441 : 3 : error ("unnamed field is neither function nor member "
15442 : : "function; cannot be declared friend");
15443 : 14 : return error_mark_node;
15444 : : }
15445 : 26679959 : decl = NULL_TREE;
15446 : : }
15447 : :
15448 : 118850527 : if (friendp)
15449 : : {
15450 : : /* Packages tend to use GNU attributes on friends, so we only
15451 : : warn for standard attributes. */
15452 : 3880845 : if (attrlist
15453 : 3880845 : && !funcdef_flag
15454 : 1224426 : && cxx11_attribute_p (*attrlist)
15455 : 3880874 : && !all_attributes_are_contracts_p (*attrlist))
15456 : : {
15457 : 27 : *attrlist = NULL_TREE;
15458 : 27 : auto_diagnostic_group d;
15459 : 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
15460 : 27 : inform (id_loc, "an attribute that appertains to a friend "
15461 : : "declaration that is not a definition is ignored");
15462 : 27 : }
15463 : : /* Friends are treated specially. */
15464 : 3880845 : if (ctype == current_class_type)
15465 : : ; /* We already issued a permerror. */
15466 : 3880845 : else if (decl && DECL_NAME (decl))
15467 : : {
15468 : 3880842 : set_originating_module (decl, true);
15469 : :
15470 : 3880842 : if (initialized)
15471 : : /* Kludge: We need funcdef_flag to be true in do_friend for
15472 : : in-class defaulted functions, but that breaks grokfndecl.
15473 : : So set it here. */
15474 : 52941 : funcdef_flag = true;
15475 : :
15476 : 3880842 : cplus_decl_attributes (&decl, *attrlist, 0);
15477 : 3880842 : *attrlist = NULL_TREE;
15478 : :
15479 : 3880842 : tree scope = ctype ? ctype : in_namespace;
15480 : 3880842 : decl = do_friend (scope, unqualified_id, decl,
15481 : : flags, funcdef_flag);
15482 : 3880842 : return decl;
15483 : : }
15484 : : else
15485 : 3 : return error_mark_node;
15486 : : }
15487 : :
15488 : : /* Structure field. It may not be a function, except for C++. */
15489 : :
15490 : 114969682 : if (decl == NULL_TREE)
15491 : : {
15492 : 26681476 : if (staticp)
15493 : : {
15494 : : /* C++ allows static class members. All other work
15495 : : for this is done by grokfield. */
15496 : 9926948 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
15497 : : dname, type);
15498 : 9926948 : if (unqualified_id
15499 : 9926945 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15500 : : {
15501 : 81025 : decl = check_explicit_specialization (unqualified_id, decl,
15502 : : template_count,
15503 : : concept_p * 8);
15504 : 81025 : if (decl == error_mark_node)
15505 : : return error_mark_node;
15506 : : }
15507 : 9926948 : set_linkage_for_static_data_member (decl);
15508 : 9926948 : if (concept_p)
15509 : 15 : error_at (declspecs->locations[ds_concept],
15510 : : "static data member %qE declared %qs",
15511 : : unqualified_id, "concept");
15512 : 9926933 : else if (constexpr_p && !initialized)
15513 : : {
15514 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15515 : : "%<constexpr%> static data member %qD must "
15516 : : "have an initializer", decl);
15517 : 9 : constexpr_p = false;
15518 : : }
15519 : 9926948 : if (consteval_p)
15520 : 3 : error_at (declspecs->locations[ds_consteval],
15521 : : "static data member %qE declared %qs",
15522 : : unqualified_id, "consteval");
15523 : :
15524 : 9926948 : if (inlinep)
15525 : 8358 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
15526 : :
15527 : 9926948 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
15528 : 9926948 : && !(cxx_dialect >= cxx17 && constexpr_p))
15529 : : /* Even if there is an in-class initialization, DECL
15530 : : is considered undefined until an out-of-class
15531 : : definition is provided, unless this is an inline
15532 : : variable. */
15533 : 1695860 : DECL_EXTERNAL (decl) = 1;
15534 : :
15535 : 9926948 : if (thread_p)
15536 : : {
15537 : 414 : CP_DECL_THREAD_LOCAL_P (decl) = true;
15538 : 414 : if (!processing_template_decl)
15539 : 374 : set_decl_tls_model (decl, decl_default_tls_model (decl));
15540 : 414 : if (declspecs->gnu_thread_keyword_p)
15541 : 31 : SET_DECL_GNU_TLS_P (decl);
15542 : : }
15543 : :
15544 : : /* Set the constraints on the declaration. */
15545 : 9926948 : bool memtmpl = (current_template_depth
15546 : 9926948 : > template_class_depth (current_class_type));
15547 : 9926948 : if (memtmpl)
15548 : : {
15549 : 302970 : tree ci = current_template_constraints ();
15550 : 302970 : set_constraints (decl, ci);
15551 : : }
15552 : : }
15553 : : else
15554 : : {
15555 : 16754528 : if (concept_p)
15556 : : {
15557 : 12 : error_at (declspecs->locations[ds_concept],
15558 : : "non-static data member %qE declared %qs",
15559 : : unqualified_id, "concept");
15560 : 12 : concept_p = false;
15561 : 12 : constexpr_p = false;
15562 : : }
15563 : 16754516 : else if (constexpr_p)
15564 : : {
15565 : 9 : error_at (declspecs->locations[ds_constexpr],
15566 : : "non-static data member %qE declared %qs",
15567 : : unqualified_id, "constexpr");
15568 : 9 : constexpr_p = false;
15569 : : }
15570 : 16754528 : if (constinit_p)
15571 : : {
15572 : 6 : error_at (declspecs->locations[ds_constinit],
15573 : : "non-static data member %qE declared %qs",
15574 : : unqualified_id, "constinit");
15575 : 6 : constinit_p = false;
15576 : : }
15577 : 16754528 : if (consteval_p)
15578 : : {
15579 : 3 : error_at (declspecs->locations[ds_consteval],
15580 : : "non-static data member %qE declared %qs",
15581 : : unqualified_id, "consteval");
15582 : 3 : consteval_p = false;
15583 : : }
15584 : 16754528 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
15585 : 16754528 : DECL_NONADDRESSABLE_P (decl) = bitfield;
15586 : 16754528 : if (bitfield && !unqualified_id)
15587 : 133940 : DECL_PADDING_P (decl) = 1;
15588 : :
15589 : 16754528 : if (storage_class == sc_mutable)
15590 : : {
15591 : 143672 : DECL_MUTABLE_P (decl) = 1;
15592 : 143672 : storage_class = sc_none;
15593 : : }
15594 : :
15595 : 16754528 : if (initialized)
15596 : : {
15597 : : /* An attempt is being made to initialize a non-static
15598 : : member. This is new in C++11. */
15599 : 1177205 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
15600 : :
15601 : : /* If this has been parsed with static storage class, but
15602 : : errors forced staticp to be cleared, ensure NSDMI is
15603 : : not present. */
15604 : 1177205 : if (declspecs->storage_class == sc_static)
15605 : 3 : DECL_INITIAL (decl) = error_mark_node;
15606 : : }
15607 : : }
15608 : :
15609 : 43436004 : bad_specifiers (decl, BSP_FIELD, virtualp,
15610 : : memfn_quals != TYPE_UNQUALIFIED,
15611 : : staticp ? false : inlinep, friendp,
15612 : : raises != NULL_TREE,
15613 : : declspecs->locations);
15614 : : }
15615 : : }
15616 : 131598500 : else if (FUNC_OR_METHOD_TYPE_P (type))
15617 : : {
15618 : 76462861 : tree original_name;
15619 : 76462861 : int publicp = 0;
15620 : :
15621 : 76462861 : if (!unqualified_id)
15622 : 0 : return error_mark_node;
15623 : :
15624 : 76462861 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15625 : : original_name = dname;
15626 : : else
15627 : 75608357 : original_name = unqualified_id;
15628 : : // FIXME:gcc_assert (original_name == dname);
15629 : :
15630 : 76462861 : if (storage_class == sc_auto)
15631 : 1 : error_at (declspecs->locations[ds_storage_class],
15632 : : "storage class %<auto%> invalid for function %qs", name);
15633 : 76462860 : else if (storage_class == sc_register)
15634 : 7 : error_at (declspecs->locations[ds_storage_class],
15635 : : "storage class %<register%> invalid for function %qs",
15636 : : name);
15637 : 76462853 : else if (thread_p)
15638 : : {
15639 : 12 : if (declspecs->gnu_thread_keyword_p)
15640 : 12 : error_at (declspecs->locations[ds_thread],
15641 : : "storage class %<__thread%> invalid for function %qs",
15642 : : name);
15643 : : else
15644 : 0 : error_at (declspecs->locations[ds_thread],
15645 : : "storage class %<thread_local%> invalid for "
15646 : : "function %qs", name);
15647 : : }
15648 : 76462841 : else if (constinit_p)
15649 : 3 : error_at (declspecs->locations[ds_constinit],
15650 : : "%<constinit%> specifier invalid for function %qs", name);
15651 : :
15652 : 76462861 : if (virt_specifiers)
15653 : 6 : error ("virt-specifiers in %qs not allowed outside a class "
15654 : : "definition", name);
15655 : : /* Function declaration not at top level.
15656 : : Storage classes other than `extern' are not allowed
15657 : : and `extern' makes no difference. */
15658 : 76462861 : if (! toplevel_bindings_p ()
15659 : 35827 : && (storage_class == sc_static
15660 : 35818 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
15661 : 76462873 : && pedantic)
15662 : : {
15663 : 9 : if (storage_class == sc_static)
15664 : 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
15665 : : "%<static%> specifier invalid for function %qs "
15666 : : "declared out of global scope", name);
15667 : : else
15668 : 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
15669 : : "%<inline%> specifier invalid for function %qs "
15670 : : "declared out of global scope", name);
15671 : : }
15672 : :
15673 : 76462861 : if (ctype == NULL_TREE)
15674 : : {
15675 : 67459515 : if (virtualp)
15676 : : {
15677 : 0 : error ("virtual non-class function %qs", name);
15678 : 0 : virtualp = 0;
15679 : : }
15680 : 67459515 : else if (sfk == sfk_constructor
15681 : 67459515 : || sfk == sfk_destructor)
15682 : : {
15683 : 3 : error (funcdef_flag
15684 : : ? G_("%qs defined in a non-class scope")
15685 : : : G_("%qs declared in a non-class scope"), name);
15686 : 3 : sfk = sfk_none;
15687 : : }
15688 : : }
15689 : 76462861 : if (consteval_p
15690 : 76501084 : && identifier_p (unqualified_id)
15691 : 76501084 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15692 : : {
15693 : 9 : error_at (declspecs->locations[ds_consteval],
15694 : : "%qD cannot be %qs", unqualified_id, "consteval");
15695 : 9 : consteval_p = false;
15696 : : }
15697 : :
15698 : : /* Record whether the function is public. */
15699 : 76462861 : publicp = (ctype != NULL_TREE
15700 : 76462861 : || storage_class != sc_static);
15701 : :
15702 : 152925722 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
15703 : : declspecs,
15704 : : reqs, virtualp, flags, memfn_quals, rqual, raises,
15705 : : 1, friendp,
15706 : : publicp,
15707 : 76462861 : inlinep | (2 * constexpr_p) | (4 * concept_p)
15708 : 76462861 : | (8 * consteval_p),
15709 : : initialized == SD_DELETED,
15710 : : is_xobj_member_function, sfk,
15711 : : funcdef_flag,
15712 : : late_return_type_p,
15713 : : template_count, in_namespace, attrlist,
15714 : : id_loc);
15715 : 76462861 : if (decl == NULL_TREE)
15716 : 506 : return error_mark_node;
15717 : :
15718 : 76462355 : if (explicitp == 2)
15719 : 3662 : DECL_NONCONVERTING_P (decl) = 1;
15720 : 76462355 : if (staticp == 1)
15721 : : {
15722 : 106839 : int invalid_static = 0;
15723 : :
15724 : : /* Don't allow a static member function in a class, and forbid
15725 : : declaring main to be static. */
15726 : 106839 : if (TREE_CODE (type) == METHOD_TYPE)
15727 : : {
15728 : 0 : permerror (input_location, "cannot declare member function %qD to have "
15729 : : "static linkage", decl);
15730 : 0 : invalid_static = 1;
15731 : : }
15732 : 106839 : else if (current_function_decl)
15733 : : {
15734 : : /* 7.1.1: There can be no static function declarations within a
15735 : : block. */
15736 : 9 : error_at (declspecs->locations[ds_storage_class],
15737 : : "cannot declare static function inside another function");
15738 : 9 : invalid_static = 1;
15739 : : }
15740 : :
15741 : 9 : if (invalid_static)
15742 : : {
15743 : 76462355 : staticp = 0;
15744 : : storage_class = sc_none;
15745 : : }
15746 : : }
15747 : 76462355 : if (declspecs->explicit_specifier)
15748 : 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
15749 : : }
15750 : : else
15751 : : {
15752 : : /* It's a variable. */
15753 : :
15754 : : /* An uninitialized decl with `extern' is a reference. */
15755 : 109890038 : decl = grokvardecl (type, dname, unqualified_id,
15756 : : declspecs,
15757 : : initialized,
15758 : : type_quals,
15759 : : inlinep,
15760 : : concept_p,
15761 : : template_count,
15762 : : ctype ? ctype : in_namespace,
15763 : : id_loc);
15764 : 55135639 : if (decl == NULL_TREE)
15765 : 49 : return error_mark_node;
15766 : :
15767 : 55135590 : bad_specifiers (decl, BSP_VAR, virtualp,
15768 : : memfn_quals != TYPE_UNQUALIFIED,
15769 : : inlinep, friendp, raises != NULL_TREE,
15770 : : declspecs->locations);
15771 : :
15772 : 55135590 : if (ctype)
15773 : : {
15774 : 381234 : DECL_CONTEXT (decl) = ctype;
15775 : 381234 : if (staticp == 1)
15776 : : {
15777 : 6 : permerror (declspecs->locations[ds_storage_class],
15778 : : "%<static%> may not be used when defining "
15779 : : "(as opposed to declaring) a static data member");
15780 : 6 : staticp = 0;
15781 : 6 : storage_class = sc_none;
15782 : : }
15783 : 381234 : if (storage_class == sc_register && TREE_STATIC (decl))
15784 : : {
15785 : 0 : error ("static member %qD declared %<register%>", decl);
15786 : 0 : storage_class = sc_none;
15787 : : }
15788 : 381234 : if (storage_class == sc_extern && pedantic)
15789 : : {
15790 : 0 : pedwarn (input_location, OPT_Wpedantic,
15791 : : "cannot explicitly declare member %q#D to have "
15792 : : "extern linkage", decl);
15793 : 0 : storage_class = sc_none;
15794 : : }
15795 : : }
15796 : 54754356 : else if (constexpr_p && DECL_EXTERNAL (decl))
15797 : : {
15798 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
15799 : : "declaration of %<constexpr%> variable %qD "
15800 : : "is not a definition", decl);
15801 : 12 : constexpr_p = false;
15802 : : }
15803 : 55135590 : if (consteval_p)
15804 : : {
15805 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
15806 : : "a variable cannot be declared %<consteval%>");
15807 : 3 : consteval_p = false;
15808 : : }
15809 : :
15810 : 55135590 : if (inlinep)
15811 : 3852687 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
15812 : 55135590 : if (innermost_code == cdk_decomp)
15813 : : {
15814 : 78042 : gcc_assert (declarator && declarator->kind == cdk_decomp);
15815 : 78042 : DECL_SOURCE_LOCATION (decl) = id_loc;
15816 : 78042 : DECL_ARTIFICIAL (decl) = 1;
15817 : 78042 : fit_decomposition_lang_decl (decl, NULL_TREE);
15818 : : }
15819 : : }
15820 : :
15821 : 516187590 : if (VAR_P (decl) && !initialized)
15822 : 6691048 : if (tree auto_node = type_uses_auto (type))
15823 : 94 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15824 : : {
15825 : 18 : location_t loc = declspecs->locations[ds_type_spec];
15826 : 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
15827 : 18 : TREE_TYPE (decl) = error_mark_node;
15828 : : }
15829 : :
15830 : 516187590 : if (storage_class == sc_extern && initialized && !funcdef_flag)
15831 : : {
15832 : 241 : if (toplevel_bindings_p ())
15833 : : {
15834 : : /* It's common practice (and completely valid) to have a const
15835 : : be initialized and declared extern. */
15836 : 235 : if (!(type_quals & TYPE_QUAL_CONST))
15837 : 20 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
15838 : : "%qs initialized and declared %<extern%>", name);
15839 : : }
15840 : : else
15841 : : {
15842 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
15843 : : "%qs has both %<extern%> and initializer", name);
15844 : 6 : return error_mark_node;
15845 : : }
15846 : : }
15847 : :
15848 : : /* Record `register' declaration for warnings on &
15849 : : and in case doing stupid register allocation. */
15850 : :
15851 : 516187584 : if (storage_class == sc_register)
15852 : : {
15853 : 2042 : DECL_REGISTER (decl) = 1;
15854 : : /* Warn about register storage specifiers on PARM_DECLs. */
15855 : 2042 : if (TREE_CODE (decl) == PARM_DECL)
15856 : : {
15857 : 35 : if (cxx_dialect >= cxx17)
15858 : 22 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15859 : : "ISO C++17 does not allow %<register%> storage "
15860 : : "class specifier");
15861 : : else
15862 : 13 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15863 : : "%<register%> storage class specifier used");
15864 : : }
15865 : : }
15866 : 516185542 : else if (storage_class == sc_extern)
15867 : 29762611 : DECL_THIS_EXTERN (decl) = 1;
15868 : 486422931 : else if (storage_class == sc_static)
15869 : 18512274 : DECL_THIS_STATIC (decl) = 1;
15870 : :
15871 : 516187584 : if (VAR_P (decl))
15872 : : {
15873 : : /* Set constexpr flag on vars (functions got it in grokfndecl). */
15874 : 65062532 : if (constexpr_p)
15875 : 13009129 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
15876 : : /* And the constinit flag (which only applies to variables). */
15877 : 52053403 : else if (constinit_p)
15878 : 368 : DECL_DECLARED_CONSTINIT_P (decl) = true;
15879 : : }
15880 : 451125052 : else if (TREE_CODE (decl) == FUNCTION_DECL)
15881 : : {
15882 : : /* If we saw a return type, record its location. */
15883 : 164750561 : location_t loc = declspecs->locations[ds_type_spec];
15884 : 164750561 : if (loc == UNKNOWN_LOCATION)
15885 : : /* Build DECL_RESULT in start_preparsed_function. */;
15886 : 136057173 : else if (!DECL_RESULT (decl))
15887 : : {
15888 : 136021553 : tree restype = TREE_TYPE (TREE_TYPE (decl));
15889 : 136021553 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
15890 : 136021553 : DECL_ARTIFICIAL (resdecl) = 1;
15891 : 136021553 : DECL_IGNORED_P (resdecl) = 1;
15892 : 136021553 : DECL_RESULT (decl) = resdecl;
15893 : : }
15894 : 35620 : else if (funcdef_flag)
15895 : 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
15896 : : }
15897 : :
15898 : : /* Record constancy and volatility on the DECL itself . There's
15899 : : no need to do this when processing a template; we'll do this
15900 : : for the instantiated declaration based on the type of DECL. */
15901 : 516187584 : if (!processing_template_decl
15902 : : /* Don't do it for instantiated variable templates either,
15903 : : cp_apply_type_quals_to_decl should have been called on it
15904 : : already and might have been overridden in cp_finish_decl
15905 : : if initializer needs runtime initialization. */
15906 : 516187584 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
15907 : 173622314 : cp_apply_type_quals_to_decl (type_quals, decl);
15908 : :
15909 : 516187584 : return decl;
15910 : : }
15911 : 898064394 : }
15912 : :
15913 : : /* Subroutine of start_function. Ensure that each of the parameter
15914 : : types (as listed in PARMS) is complete, as is required for a
15915 : : function definition. */
15916 : :
15917 : : static void
15918 : 139343339 : require_complete_types_for_parms (tree parms)
15919 : : {
15920 : 413844402 : for (; parms; parms = DECL_CHAIN (parms))
15921 : : {
15922 : 274501063 : if (dependent_type_p (TREE_TYPE (parms)))
15923 : 158907791 : continue;
15924 : 115593272 : if (!VOID_TYPE_P (TREE_TYPE (parms))
15925 : 115593272 : && complete_type_or_else (TREE_TYPE (parms), parms))
15926 : : {
15927 : 115593191 : relayout_decl (parms);
15928 : 115593191 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
15929 : :
15930 : 115593191 : abstract_virtuals_error (parms, TREE_TYPE (parms));
15931 : 115593191 : maybe_warn_parm_abi (TREE_TYPE (parms),
15932 : 115593191 : DECL_SOURCE_LOCATION (parms));
15933 : : }
15934 : : else
15935 : : /* grokparms or complete_type_or_else will have already issued
15936 : : an error. */
15937 : 81 : TREE_TYPE (parms) = error_mark_node;
15938 : : }
15939 : 139343339 : }
15940 : :
15941 : : /* Returns nonzero if T is a local variable. */
15942 : :
15943 : : int
15944 : 146639492 : local_variable_p (const_tree t)
15945 : : {
15946 : 146639492 : if ((VAR_P (t)
15947 : 66050607 : && (DECL_LOCAL_DECL_P (t)
15948 : 66050607 : || !DECL_CONTEXT (t)
15949 : 66008719 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
15950 : 150058265 : || (TREE_CODE (t) == PARM_DECL))
15951 : 131409658 : return 1;
15952 : :
15953 : : return 0;
15954 : : }
15955 : :
15956 : : /* Like local_variable_p, but suitable for use as a tree-walking
15957 : : function. */
15958 : :
15959 : : static tree
15960 : 4910821 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
15961 : : void * /*data*/)
15962 : : {
15963 : 4910821 : if (unevaluated_p (TREE_CODE (*tp)))
15964 : : {
15965 : : /* DR 2082 permits local variables in unevaluated contexts
15966 : : within a default argument. */
15967 : 15 : *walk_subtrees = 0;
15968 : 15 : return NULL_TREE;
15969 : : }
15970 : :
15971 : 4910806 : if (local_variable_p (*tp)
15972 : 4910806 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
15973 : : return *tp;
15974 : 4910806 : else if (TYPE_P (*tp))
15975 : 30 : *walk_subtrees = 0;
15976 : :
15977 : : return NULL_TREE;
15978 : : }
15979 : :
15980 : : /* Check that ARG, which is a default-argument expression for a
15981 : : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
15982 : : something goes wrong. DECL may also be a _TYPE node, rather than a
15983 : : DECL, if there is no DECL available. */
15984 : :
15985 : : tree
15986 : 2523150 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
15987 : : {
15988 : 2523150 : tree var;
15989 : 2523150 : tree decl_type;
15990 : :
15991 : 2523150 : if (TREE_CODE (arg) == DEFERRED_PARSE)
15992 : : /* We get a DEFERRED_PARSE when looking at an in-class declaration
15993 : : with a default argument. Ignore the argument for now; we'll
15994 : : deal with it after the class is complete. */
15995 : : return arg;
15996 : :
15997 : 1818878 : if (TYPE_P (decl))
15998 : : {
15999 : : decl_type = decl;
16000 : : decl = NULL_TREE;
16001 : : }
16002 : : else
16003 : 550182 : decl_type = TREE_TYPE (decl);
16004 : :
16005 : 1818878 : if (arg == error_mark_node
16006 : 1818763 : || decl == error_mark_node
16007 : 1818754 : || TREE_TYPE (arg) == error_mark_node
16008 : 3637632 : || decl_type == error_mark_node)
16009 : : /* Something already went wrong. There's no need to check
16010 : : further. */
16011 : : return error_mark_node;
16012 : :
16013 : : /* [dcl.fct.default]
16014 : :
16015 : : A default argument expression is implicitly converted to the
16016 : : parameter type. */
16017 : 1818754 : ++cp_unevaluated_operand;
16018 : : /* Avoid digest_init clobbering the initializer. */
16019 : 1818754 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
16020 : 1818754 : perform_implicit_conversion_flags (decl_type, carg, complain,
16021 : : LOOKUP_IMPLICIT);
16022 : 1818754 : --cp_unevaluated_operand;
16023 : :
16024 : : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
16025 : : the call sites. */
16026 : 1350824 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
16027 : 467945 : && null_ptr_cst_p (arg)
16028 : : /* Don't lose side-effects as in PR90473. */
16029 : 2175077 : && !TREE_SIDE_EFFECTS (arg))
16030 : 356320 : return nullptr_node;
16031 : :
16032 : : /* [dcl.fct.default]
16033 : :
16034 : : Local variables shall not be used in default argument
16035 : : expressions.
16036 : :
16037 : : The keyword `this' shall not be used in a default argument of a
16038 : : member function. */
16039 : 1462434 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
16040 : 1462434 : if (var)
16041 : : {
16042 : 0 : if (complain & tf_warning_or_error)
16043 : : {
16044 : 0 : if (DECL_NAME (var) == this_identifier)
16045 : 0 : permerror (input_location, "default argument %qE uses %qD",
16046 : : arg, var);
16047 : : else
16048 : 0 : error ("default argument %qE uses local variable %qD", arg, var);
16049 : : }
16050 : 0 : return error_mark_node;
16051 : : }
16052 : :
16053 : : /* All is well. */
16054 : 1462434 : return arg;
16055 : : }
16056 : :
16057 : : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
16058 : :
16059 : : static tree
16060 : 256414463 : type_is_deprecated (tree type)
16061 : : {
16062 : 379398817 : enum tree_code code;
16063 : 379398817 : if (TREE_DEPRECATED (type))
16064 : : return type;
16065 : 379357552 : if (TYPE_NAME (type))
16066 : : {
16067 : 252165086 : if (TREE_DEPRECATED (TYPE_NAME (type)))
16068 : : return type;
16069 : : else
16070 : : {
16071 : 252165080 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16072 : 252165080 : return NULL_TREE;
16073 : : }
16074 : : }
16075 : :
16076 : : /* Do warn about using typedefs to a deprecated class. */
16077 : 127192466 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16078 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16079 : :
16080 : 127192466 : code = TREE_CODE (type);
16081 : :
16082 : 127192466 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
16083 : 127192466 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
16084 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
16085 : 122876143 : return type_is_deprecated (TREE_TYPE (type));
16086 : :
16087 : 108211 : if (TYPE_PTRMEMFUNC_P (type))
16088 : 108211 : return type_is_deprecated
16089 : 108211 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16090 : :
16091 : : return NULL_TREE;
16092 : : }
16093 : :
16094 : : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
16095 : :
16096 : : static tree
16097 : 257285831 : type_is_unavailable (tree type)
16098 : : {
16099 : 380802421 : enum tree_code code;
16100 : 380802421 : if (TREE_UNAVAILABLE (type))
16101 : : return type;
16102 : 380802415 : if (TYPE_NAME (type))
16103 : : {
16104 : 253048225 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
16105 : : return type;
16106 : : else
16107 : : {
16108 : 253048219 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16109 : 253048219 : return NULL_TREE;
16110 : : }
16111 : : }
16112 : :
16113 : : /* Do warn about using typedefs to a deprecated class. */
16114 : 127754190 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16115 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16116 : :
16117 : 127754190 : code = TREE_CODE (type);
16118 : :
16119 : 127754190 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
16120 : 127754190 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
16121 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
16122 : 123305587 : return type_is_unavailable (TREE_TYPE (type));
16123 : :
16124 : 211003 : if (TYPE_PTRMEMFUNC_P (type))
16125 : 211003 : return type_is_unavailable
16126 : 211003 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16127 : :
16128 : : return NULL_TREE;
16129 : : }
16130 : :
16131 : : /* Decode the list of parameter types for a function type.
16132 : : Given the list of things declared inside the parens,
16133 : : return a list of types.
16134 : :
16135 : : If this parameter does not end with an ellipsis, we append
16136 : : void_list_node.
16137 : :
16138 : : *PARMS is set to the chain of PARM_DECLs created. */
16139 : :
16140 : : tree
16141 : 171904201 : grokparms (tree parmlist, tree *parms)
16142 : : {
16143 : 171904201 : tree result = NULL_TREE;
16144 : 171904201 : tree decls = NULL_TREE;
16145 : 171904201 : tree parm;
16146 : 171904201 : int any_error = 0;
16147 : :
16148 : 429790135 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
16149 : : {
16150 : 428410647 : tree type = NULL_TREE;
16151 : 428410647 : tree init = TREE_PURPOSE (parm);
16152 : 428410647 : tree decl = TREE_VALUE (parm);
16153 : :
16154 : 428410647 : if (parm == void_list_node || parm == explicit_void_list_node)
16155 : : break;
16156 : :
16157 : 257885946 : if (! decl || TREE_TYPE (decl) == error_mark_node)
16158 : : {
16159 : 185 : any_error = 1;
16160 : 185 : continue;
16161 : : }
16162 : :
16163 : 257885761 : type = TREE_TYPE (decl);
16164 : 257885761 : if (VOID_TYPE_P (type))
16165 : : {
16166 : 78 : if (same_type_p (type, void_type_node)
16167 : 69 : && !init
16168 : 57 : && !DECL_NAME (decl) && !result
16169 : 102 : && TREE_CHAIN (parm) == void_list_node)
16170 : : /* DR 577: A parameter list consisting of a single
16171 : : unnamed parameter of non-dependent type 'void'. */
16172 : : break;
16173 : 66 : else if (cv_qualified_p (type))
16174 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
16175 : : "invalid use of cv-qualified type %qT in "
16176 : : "parameter declaration", type);
16177 : : else
16178 : 57 : error_at (DECL_SOURCE_LOCATION (decl),
16179 : : "invalid use of type %<void%> in parameter "
16180 : : "declaration");
16181 : : /* It's not a good idea to actually create parameters of
16182 : : type `void'; other parts of the compiler assume that a
16183 : : void type terminates the parameter list. */
16184 : 66 : type = error_mark_node;
16185 : 66 : TREE_TYPE (decl) = error_mark_node;
16186 : : }
16187 : :
16188 : 257885749 : if (type != error_mark_node)
16189 : : {
16190 : 257885683 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16191 : : {
16192 : 257285831 : tree unavailtype = type_is_unavailable (type);
16193 : 257285831 : if (unavailtype)
16194 : 12 : cp_handle_deprecated_or_unavailable (unavailtype);
16195 : : }
16196 : 257885683 : if (deprecated_state != DEPRECATED_SUPPRESS
16197 : 257885683 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16198 : : {
16199 : 256414463 : tree deptype = type_is_deprecated (type);
16200 : 256414463 : if (deptype)
16201 : 41271 : cp_handle_deprecated_or_unavailable (deptype);
16202 : : }
16203 : :
16204 : : /* [dcl.fct] "A parameter with volatile-qualified type is
16205 : : deprecated." */
16206 : 257885683 : if (CP_TYPE_VOLATILE_P (type))
16207 : 504 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
16208 : : "%<volatile%>-qualified parameter is "
16209 : : "deprecated");
16210 : :
16211 : : /* Top-level qualifiers on the parameters are
16212 : : ignored for function types. */
16213 : 257885683 : type = cp_build_qualified_type (type, 0);
16214 : 257885683 : if (TREE_CODE (type) == METHOD_TYPE)
16215 : : {
16216 : 0 : error ("parameter %qD invalidly declared method type", decl);
16217 : 0 : type = build_pointer_type (type);
16218 : 0 : TREE_TYPE (decl) = type;
16219 : : }
16220 : 257885683 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
16221 : : {
16222 : : /* Before C++17 DR 393:
16223 : : [dcl.fct]/6, parameter types cannot contain pointers
16224 : : (references) to arrays of unknown bound. */
16225 : 1554858 : tree t = TREE_TYPE (type);
16226 : 1554858 : int ptr = TYPE_PTR_P (type);
16227 : :
16228 : 1707522 : while (1)
16229 : : {
16230 : 1631190 : if (TYPE_PTR_P (t))
16231 : : ptr = 1;
16232 : 1557941 : else if (TREE_CODE (t) != ARRAY_TYPE)
16233 : : break;
16234 : 3093 : else if (!TYPE_DOMAIN (t))
16235 : : break;
16236 : 76332 : t = TREE_TYPE (t);
16237 : : }
16238 : 1554858 : if (TREE_CODE (t) == ARRAY_TYPE)
16239 : 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
16240 : : ptr
16241 : : ? G_("parameter %qD includes pointer to array of "
16242 : : "unknown bound %qT")
16243 : : : G_("parameter %qD includes reference to array of "
16244 : : "unknown bound %qT"),
16245 : : decl, t);
16246 : : }
16247 : :
16248 : 257885683 : if (init && !processing_template_decl)
16249 : 1254445 : init = check_default_argument (decl, init, tf_warning_or_error);
16250 : : }
16251 : :
16252 : 257885749 : DECL_CHAIN (decl) = decls;
16253 : 257885749 : decls = decl;
16254 : 257885749 : result = tree_cons (init, type, result);
16255 : : }
16256 : 171904201 : decls = nreverse (decls);
16257 : 171904201 : result = nreverse (result);
16258 : 171904201 : if (parm)
16259 : 170524713 : result = chainon (result, void_list_node);
16260 : 171904201 : *parms = decls;
16261 : 171904201 : if (any_error)
16262 : 171 : result = NULL_TREE;
16263 : :
16264 : 171 : if (any_error)
16265 : : /* We had parm errors, recover by giving the function (...) type. */
16266 : 171 : result = NULL_TREE;
16267 : :
16268 : 171904201 : return result;
16269 : : }
16270 : :
16271 : :
16272 : : /* D is a constructor or overloaded `operator='.
16273 : :
16274 : : Let T be the class in which D is declared. Then, this function
16275 : : returns:
16276 : :
16277 : : -1 if D's is an ill-formed constructor or copy assignment operator
16278 : : whose first parameter is of type `T'.
16279 : : 0 if D is not a copy constructor or copy assignment
16280 : : operator.
16281 : : 1 if D is a copy constructor or copy assignment operator whose
16282 : : first parameter is a reference to non-const qualified T.
16283 : : 2 if D is a copy constructor or copy assignment operator whose
16284 : : first parameter is a reference to const qualified T.
16285 : :
16286 : : This function can be used as a predicate. Positive values indicate
16287 : : a copy constructor and nonzero values indicate a copy assignment
16288 : : operator. */
16289 : :
16290 : : int
16291 : 280409250 : copy_fn_p (const_tree d)
16292 : : {
16293 : 280409250 : tree args;
16294 : 280409250 : tree arg_type;
16295 : 280409250 : int result = 1;
16296 : :
16297 : 280409250 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
16298 : :
16299 : 280409250 : if (TREE_CODE (d) == TEMPLATE_DECL
16300 : 280409250 : || (DECL_TEMPLATE_INFO (d)
16301 : 134741273 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16302 : : /* Instantiations of template member functions are never copy
16303 : : functions. Note that member functions of templated classes are
16304 : : represented as template functions internally, and we must
16305 : : accept those as copy functions. */
16306 : : return 0;
16307 : :
16308 : 236185859 : if (!DECL_CONSTRUCTOR_P (d)
16309 : 236185859 : && DECL_NAME (d) != assign_op_identifier)
16310 : : return 0;
16311 : :
16312 : 235434201 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16313 : : {
16314 : 154 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16315 : 154 : if (!TYPE_REF_P (object_param)
16316 : 148 : || TYPE_REF_IS_RVALUE (object_param)
16317 : : /* Reject unrelated object parameters. */
16318 : 136 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16319 : 290 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16320 : 24 : return 0;
16321 : 130 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16322 : : }
16323 : : else
16324 : 235434047 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
16325 : 235434177 : if (!args)
16326 : : return 0;
16327 : :
16328 : 235426731 : arg_type = TREE_VALUE (args);
16329 : 235426731 : if (arg_type == error_mark_node)
16330 : : return 0;
16331 : :
16332 : 235426696 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
16333 : : {
16334 : : /* Pass by value copy assignment operator. */
16335 : : result = -1;
16336 : : }
16337 : 235424728 : else if (TYPE_REF_P (arg_type)
16338 : 144870319 : && !TYPE_REF_IS_RVALUE (arg_type)
16339 : 328915367 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
16340 : : {
16341 : 73539146 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
16342 : 73452764 : result = 2;
16343 : : }
16344 : : else
16345 : : return 0;
16346 : :
16347 : 73541114 : args = TREE_CHAIN (args);
16348 : :
16349 : 76086583 : if (args && args != void_list_node && !TREE_PURPOSE (args))
16350 : : /* There are more non-optional args. */
16351 : : return 0;
16352 : :
16353 : : return result;
16354 : : }
16355 : :
16356 : : /* D is a constructor or overloaded `operator='.
16357 : :
16358 : : Let T be the class in which D is declared. Then, this function
16359 : : returns true when D is a move constructor or move assignment
16360 : : operator, false otherwise. */
16361 : :
16362 : : bool
16363 : 112947408 : move_fn_p (const_tree d)
16364 : : {
16365 : 112947408 : if (cxx_dialect == cxx98)
16366 : : /* There are no move constructors if we are in C++98 mode. */
16367 : : return false;
16368 : :
16369 : 112884717 : if (TREE_CODE (d) == TEMPLATE_DECL
16370 : 112884717 : || (DECL_TEMPLATE_INFO (d)
16371 : 51064479 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16372 : : /* Instantiations of template member functions are never move
16373 : : functions. Note that member functions of templated classes are
16374 : : represented as template functions internally, and we must
16375 : : accept those as move functions. */
16376 : : return 0;
16377 : :
16378 : 93519141 : return move_signature_fn_p (d);
16379 : : }
16380 : :
16381 : : /* D is a constructor or overloaded `operator='.
16382 : :
16383 : : Then, this function returns true when D has the same signature as a move
16384 : : constructor or move assignment operator (because either it is such a
16385 : : ctor/op= or it is a template specialization with the same signature),
16386 : : false otherwise. */
16387 : :
16388 : : bool
16389 : 93519141 : move_signature_fn_p (const_tree d)
16390 : : {
16391 : 93519141 : tree args;
16392 : 93519141 : tree arg_type;
16393 : 93519141 : bool result = false;
16394 : :
16395 : 93519141 : if (!DECL_CONSTRUCTOR_P (d)
16396 : 93519141 : && DECL_NAME (d) != assign_op_identifier)
16397 : : return 0;
16398 : :
16399 : 93519083 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16400 : : {
16401 : 88 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16402 : 88 : if (!TYPE_REF_P (object_param)
16403 : 84 : || TYPE_REF_IS_RVALUE (object_param)
16404 : : /* Reject unrelated object parameters. */
16405 : 76 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16406 : 164 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16407 : 16 : return 0;
16408 : 72 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16409 : : }
16410 : : else
16411 : 93518995 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
16412 : 93519067 : if (!args)
16413 : : return 0;
16414 : :
16415 : 93516333 : arg_type = TREE_VALUE (args);
16416 : 93516333 : if (arg_type == error_mark_node)
16417 : : return 0;
16418 : :
16419 : 93516325 : if (TYPE_REF_P (arg_type)
16420 : 45748208 : && TYPE_REF_IS_RVALUE (arg_type)
16421 : 129120701 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
16422 : : DECL_CONTEXT (d)))
16423 : : result = true;
16424 : :
16425 : 93516325 : args = TREE_CHAIN (args);
16426 : :
16427 : 103804692 : if (args && args != void_list_node && !TREE_PURPOSE (args))
16428 : : /* There are more non-optional args. */
16429 : : return false;
16430 : :
16431 : : return result;
16432 : : }
16433 : :
16434 : : /* Remember any special properties of member function DECL. */
16435 : :
16436 : : void
16437 : 285879215 : grok_special_member_properties (tree decl)
16438 : : {
16439 : 285879215 : tree class_type;
16440 : :
16441 : 285879215 : if (TREE_CODE (decl) == USING_DECL
16442 : 285879215 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16443 : : return;
16444 : :
16445 : 268852875 : class_type = DECL_CONTEXT (decl);
16446 : 268852875 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
16447 : : {
16448 : 118603853 : int ctor = copy_fn_p (decl);
16449 : :
16450 : 118603853 : if (!DECL_ARTIFICIAL (decl))
16451 : 89454453 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
16452 : :
16453 : 118603853 : if (ctor > 0)
16454 : : {
16455 : : /* [class.copy]
16456 : :
16457 : : A non-template constructor for class X is a copy
16458 : : constructor if its first parameter is of type X&, const
16459 : : X&, volatile X& or const volatile X&, and either there
16460 : : are no other parameters or else all other parameters have
16461 : : default arguments. */
16462 : 23085648 : TYPE_HAS_COPY_CTOR (class_type) = 1;
16463 : 23085648 : if (ctor > 1)
16464 : 23055891 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
16465 : : }
16466 : :
16467 : 118603853 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
16468 : 24551338 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
16469 : :
16470 : 118603853 : if (is_list_ctor (decl))
16471 : 1400761 : TYPE_HAS_LIST_CTOR (class_type) = 1;
16472 : :
16473 : 118603853 : if (maybe_constexpr_fn (decl)
16474 : 118603853 : && !ctor && !move_fn_p (decl))
16475 : 29094321 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
16476 : : }
16477 : 150249022 : else if (DECL_NAME (decl) == assign_op_identifier)
16478 : : {
16479 : : /* [class.copy]
16480 : :
16481 : : A non-template assignment operator for class X is a copy
16482 : : assignment operator if its parameter is of type X, X&, const
16483 : : X&, volatile X& or const volatile X&. */
16484 : :
16485 : 13021775 : int assop = copy_fn_p (decl);
16486 : :
16487 : 13021775 : if (assop)
16488 : : {
16489 : 6509555 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
16490 : 6509555 : if (assop != 1)
16491 : 6480188 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
16492 : : }
16493 : : }
16494 : 137227247 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
16495 : 1911445 : TYPE_HAS_CONVERSION (class_type) = true;
16496 : :
16497 : : /* Destructors are handled in check_methods. */
16498 : : }
16499 : :
16500 : : /* Check a constructor DECL has the correct form. Complains
16501 : : if the class has a constructor of the form X(X). */
16502 : :
16503 : : bool
16504 : 41835385 : grok_ctor_properties (const_tree ctype, const_tree decl)
16505 : : {
16506 : 41835385 : int ctor_parm = copy_fn_p (decl);
16507 : :
16508 : 41835385 : if (ctor_parm < 0)
16509 : : {
16510 : : /* [class.copy]
16511 : :
16512 : : A declaration of a constructor for a class X is ill-formed if
16513 : : its first parameter is of type (optionally cv-qualified) X
16514 : : and either there are no other parameters or else all other
16515 : : parameters have default arguments.
16516 : :
16517 : : We *don't* complain about member template instantiations that
16518 : : have this form, though; they can occur as we try to decide
16519 : : what constructor to use during overload resolution. Since
16520 : : overload resolution will never prefer such a constructor to
16521 : : the non-template copy constructor (which is either explicitly
16522 : : or implicitly defined), there's no need to worry about their
16523 : : existence. Theoretically, they should never even be
16524 : : instantiated, but that's hard to forestall. */
16525 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
16526 : : "invalid constructor; you probably meant %<%T (const %T&)%>",
16527 : : ctype, ctype);
16528 : 9 : return false;
16529 : : }
16530 : :
16531 : : return true;
16532 : : }
16533 : :
16534 : : /* DECL is a declaration for an overloaded or conversion operator. If
16535 : : COMPLAIN is true, errors are issued for invalid declarations. */
16536 : :
16537 : : bool
16538 : 49250021 : grok_op_properties (tree decl, bool complain)
16539 : : {
16540 : 49250021 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
16541 : 49250021 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
16542 : 49250021 : tree name = DECL_NAME (decl);
16543 : 49250021 : location_t loc = DECL_SOURCE_LOCATION (decl);
16544 : :
16545 : 49250021 : tree class_type = DECL_CONTEXT (decl);
16546 : 49250021 : if (class_type && !CLASS_TYPE_P (class_type))
16547 : : class_type = NULL_TREE;
16548 : :
16549 : 49250021 : tree_code operator_code;
16550 : 49250021 : unsigned op_flags;
16551 : 49250021 : if (IDENTIFIER_CONV_OP_P (name))
16552 : : {
16553 : : /* Conversion operators are TYPE_EXPR for the purposes of this
16554 : : function. */
16555 : : operator_code = TYPE_EXPR;
16556 : : op_flags = OVL_OP_FLAG_UNARY;
16557 : : }
16558 : : else
16559 : : {
16560 : 47667525 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
16561 : :
16562 : 47667525 : operator_code = ovl_op->tree_code;
16563 : 47667525 : op_flags = ovl_op->flags;
16564 : 47667525 : gcc_checking_assert (operator_code != ERROR_MARK);
16565 : 47667525 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16566 : : }
16567 : :
16568 : 47667525 : if (op_flags & OVL_OP_FLAG_ALLOC)
16569 : : {
16570 : : /* operator new and operator delete are quite special. */
16571 : 325696 : if (class_type)
16572 : 12860 : switch (op_flags)
16573 : : {
16574 : 9095 : case OVL_OP_FLAG_ALLOC:
16575 : 9095 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
16576 : 9095 : break;
16577 : :
16578 : 3498 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
16579 : 3498 : TYPE_GETS_DELETE (class_type) |= 1;
16580 : 3498 : break;
16581 : :
16582 : 130 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
16583 : 130 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
16584 : 130 : break;
16585 : :
16586 : 137 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
16587 : 137 : TYPE_GETS_DELETE (class_type) |= 2;
16588 : 137 : break;
16589 : :
16590 : 0 : default:
16591 : 0 : gcc_unreachable ();
16592 : : }
16593 : :
16594 : : /* [basic.std.dynamic.allocation]/1:
16595 : :
16596 : : A program is ill-formed if an allocation function is declared
16597 : : in a namespace scope other than global scope or declared
16598 : : static in global scope.
16599 : :
16600 : : The same also holds true for deallocation functions. */
16601 : 325696 : if (DECL_NAMESPACE_SCOPE_P (decl))
16602 : : {
16603 : 312836 : if (CP_DECL_CONTEXT (decl) != global_namespace)
16604 : : {
16605 : 6 : error_at (loc, "%qD may not be declared within a namespace",
16606 : : decl);
16607 : 6 : return false;
16608 : : }
16609 : :
16610 : 312830 : if (!TREE_PUBLIC (decl))
16611 : : {
16612 : 6 : error_at (loc, "%qD may not be declared as static", decl);
16613 : 6 : return false;
16614 : : }
16615 : : }
16616 : :
16617 : : /* Check for replaceable global new/delete operators with
16618 : : const std::nothrow_t & last argument, other replaceable global
16619 : : new/delete operators are marked in cxx_init_decl_processing. */
16620 : 325684 : if (CP_DECL_CONTEXT (decl) == global_namespace)
16621 : : {
16622 : 312824 : tree args = argtypes;
16623 : 312824 : if (args
16624 : 312812 : && args != void_list_node
16625 : 938430 : && same_type_p (TREE_VALUE (args),
16626 : : (op_flags & OVL_OP_FLAG_DELETE)
16627 : : ? ptr_type_node : size_type_node))
16628 : : {
16629 : 312782 : args = TREE_CHAIN (args);
16630 : 312782 : if (aligned_allocation_fn_p (decl))
16631 : 100812 : args = TREE_CHAIN (args);
16632 : 312782 : if (args
16633 : 312776 : && args != void_list_node
16634 : 521545 : && TREE_CHAIN (args) == void_list_node)
16635 : : {
16636 : 183516 : tree t = TREE_VALUE (args);
16637 : 183516 : if (TYPE_REF_P (t)
16638 : 104114 : && !TYPE_REF_IS_RVALUE (t)
16639 : 104114 : && (t = TREE_TYPE (t))
16640 : 104114 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
16641 : 287182 : && is_std_class (t, "nothrow_t"))
16642 : 103666 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
16643 : : }
16644 : : }
16645 : : }
16646 : :
16647 : 325684 : if (op_flags & OVL_OP_FLAG_DELETE)
16648 : : {
16649 : 184669 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
16650 : 184669 : coerce_delete_type (decl, loc);
16651 : : }
16652 : : else
16653 : : {
16654 : 141015 : DECL_SET_IS_OPERATOR_NEW (decl, true);
16655 : 141015 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
16656 : : }
16657 : :
16658 : 325684 : return true;
16659 : : }
16660 : :
16661 : : /* An operator function must either be a non-static member function
16662 : : or have at least one parameter of a class, a reference to a class,
16663 : : an enumeration, or a reference to an enumeration. 13.4.0.6 */
16664 : 48924325 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16665 : : {
16666 : 13670292 : if (operator_code == TYPE_EXPR
16667 : 13670292 : || operator_code == COMPONENT_REF
16668 : 13670271 : || operator_code == NOP_EXPR)
16669 : : {
16670 : 33 : error_at (loc, "%qD must be a non-static member function", decl);
16671 : 33 : return false;
16672 : : }
16673 : :
16674 : 13670259 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
16675 : : {
16676 : 106 : if (! DECL_STATIC_FUNCTION_P (decl))
16677 : : {
16678 : 12 : error_at (loc, "%qD must be a member function", decl);
16679 : 12 : return false;
16680 : : }
16681 : 94 : if (cxx_dialect < cxx23
16682 : : /* For lambdas we diagnose static lambda specifier elsewhere. */
16683 : 95 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
16684 : : /* For instantiations, we have diagnosed this already. */
16685 : 126 : && ! DECL_USE_TEMPLATE (decl))
16686 : 26 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
16687 : : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
16688 : : decl);
16689 : 94 : if (operator_code == ARRAY_REF)
16690 : : /* static operator[] should have exactly one argument
16691 : : for C++20 and earlier, so that it isn't multidimensional. */
16692 : : op_flags = OVL_OP_FLAG_UNARY;
16693 : : }
16694 : 13670153 : else if (DECL_STATIC_FUNCTION_P (decl))
16695 : : {
16696 : 9 : error_at (loc, "%qD must be either a non-static member "
16697 : : "function or a non-member function", decl);
16698 : 9 : return false;
16699 : : }
16700 : : else
16701 : 428184 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
16702 : : {
16703 : 14098328 : if (!arg || arg == void_list_node)
16704 : : {
16705 : 18 : if (complain)
16706 : 15 : error_at (loc, "%qD must have an argument of class or "
16707 : : "enumerated type", decl);
16708 : 18 : return false;
16709 : : }
16710 : :
16711 : 14098310 : tree type = non_reference (TREE_VALUE (arg));
16712 : 14098310 : if (type == error_mark_node)
16713 : : return false;
16714 : :
16715 : : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
16716 : : because these checks are performed even on template
16717 : : functions. */
16718 : 14098307 : if (MAYBE_CLASS_TYPE_P (type)
16719 : 1361496 : || TREE_CODE (type) == ENUMERAL_TYPE)
16720 : : break;
16721 : 428184 : }
16722 : : }
16723 : :
16724 : 48924214 : if (operator_code == CALL_EXPR)
16725 : : /* There are no further restrictions on the arguments to an overloaded
16726 : : "operator ()". */
16727 : : return true;
16728 : :
16729 : : /* C++23 allows an arbitrary number of parameters and default arguments for
16730 : : operator[], and none of the other checks below apply. */
16731 : 42853339 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
16732 : : return true;
16733 : :
16734 : 42518528 : if (operator_code == COND_EXPR)
16735 : : {
16736 : : /* 13.4.0.3 */
16737 : 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
16738 : 6 : return false;
16739 : : }
16740 : :
16741 : : /* Count the number of arguments and check for ellipsis. */
16742 : : int arity = 0;
16743 : 121728144 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
16744 : : {
16745 : 79209637 : if (!arg)
16746 : : {
16747 : 15 : error_at (loc, "%qD must not have variable number of arguments",
16748 : : decl);
16749 : 15 : return false;
16750 : : }
16751 : 79209622 : ++arity;
16752 : : }
16753 : : /* FIXME: We need tests for these errors with xobj member functions. */
16754 : : /* Verify correct number of arguments. */
16755 : 42518507 : switch (op_flags)
16756 : : {
16757 : 7313570 : case OVL_OP_FLAG_AMBIARY:
16758 : 7313570 : if (arity == 1)
16759 : : {
16760 : : /* We have a unary instance of an ambi-ary op. Remap to the
16761 : : unary one. */
16762 : 3336177 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
16763 : 3336177 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
16764 : 3336177 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
16765 : 3336177 : operator_code = ovl_op->tree_code;
16766 : 3336177 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16767 : : }
16768 : 3977393 : else if (arity != 2)
16769 : : {
16770 : : /* This was an ambiguous operator but is invalid. */
16771 : 3 : error_at (loc,
16772 : : methodp
16773 : : ? G_("%qD must have either zero or one argument")
16774 : : : G_("%qD must have either one or two arguments"), decl);
16775 : 3 : return false;
16776 : : }
16777 : 3977390 : else if ((operator_code == POSTINCREMENT_EXPR
16778 : 3977390 : || operator_code == POSTDECREMENT_EXPR)
16779 : 2026798 : && ! processing_template_decl
16780 : : /* x++ and x--'s second argument must be an int. */
16781 : 5320927 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
16782 : : integer_type_node))
16783 : : {
16784 : 15 : error_at (loc,
16785 : : methodp
16786 : : ? G_("postfix %qD must have %<int%> as its argument")
16787 : : : G_("postfix %qD must have %<int%> as its second argument"),
16788 : : decl);
16789 : 15 : return false;
16790 : : }
16791 : : break;
16792 : :
16793 : 2491245 : case OVL_OP_FLAG_UNARY:
16794 : 2491245 : if (arity != 1)
16795 : : {
16796 : 12 : error_at (loc,
16797 : : methodp
16798 : : ? G_("%qD must have no arguments")
16799 : : : G_("%qD must have exactly one argument"), decl);
16800 : 12 : return false;
16801 : : }
16802 : : break;
16803 : :
16804 : 32713692 : case OVL_OP_FLAG_BINARY:
16805 : 32713692 : if (arity != 2)
16806 : : {
16807 : 3 : error_at (loc,
16808 : : methodp
16809 : : ? G_("%qD must have exactly one argument")
16810 : : : G_("%qD must have exactly two arguments"), decl);
16811 : 3 : return false;
16812 : : }
16813 : : break;
16814 : :
16815 : 0 : default:
16816 : 0 : gcc_unreachable ();
16817 : : }
16818 : :
16819 : : /* There can be no default arguments. */
16820 : 121727997 : for (tree arg = argtypes; arg && arg != void_list_node;
16821 : 79209523 : arg = TREE_CHAIN (arg))
16822 : 79209538 : if (TREE_PURPOSE (arg))
16823 : : {
16824 : 15 : TREE_PURPOSE (arg) = NULL_TREE;
16825 : 15 : error_at (loc, "%qD cannot have default arguments", decl);
16826 : 15 : return false;
16827 : : }
16828 : :
16829 : : /* At this point the declaration is well-formed. It may not be
16830 : : sensible though. */
16831 : :
16832 : : /* Check member function warnings only on the in-class declaration.
16833 : : There's no point warning on an out-of-class definition. */
16834 : 42518459 : if (class_type && class_type != current_class_type)
16835 : : return true;
16836 : :
16837 : : /* Warn about conversion operators that will never be used. */
16838 : 40601716 : if (IDENTIFIER_CONV_OP_P (name)
16839 : 1582092 : && ! DECL_TEMPLATE_INFO (decl)
16840 : 41222138 : && warn_class_conversion)
16841 : : {
16842 : 620389 : tree t = TREE_TYPE (name);
16843 : 620389 : int ref = TYPE_REF_P (t);
16844 : :
16845 : 620389 : if (ref)
16846 : 22908 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
16847 : :
16848 : 620389 : if (VOID_TYPE_P (t))
16849 : 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
16850 : : "will never use a type conversion operator", class_type);
16851 : 620380 : else if (class_type)
16852 : : {
16853 : 620380 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
16854 : 24 : warning_at (loc, OPT_Wclass_conversion,
16855 : : ref
16856 : : ? G_("converting %qT to a reference to the same type "
16857 : : "will never use a type conversion operator")
16858 : : : G_("converting %qT to the same type "
16859 : : "will never use a type conversion operator"),
16860 : : class_type);
16861 : : /* Don't force t to be complete here. */
16862 : 620365 : else if (MAYBE_CLASS_TYPE_P (t)
16863 : 272456 : && COMPLETE_TYPE_P (t)
16864 : 57112 : && DERIVED_FROM_P (t, class_type))
16865 : 30 : warning_at (loc, OPT_Wclass_conversion,
16866 : : ref
16867 : : ? G_("converting %qT to a reference to a base class "
16868 : : "%qT will never use a type conversion operator")
16869 : : : G_("converting %qT to a base class %qT "
16870 : : "will never use a type conversion operator"),
16871 : : class_type, t);
16872 : : }
16873 : : }
16874 : :
16875 : 40601716 : if (!warn_ecpp)
16876 : : return true;
16877 : :
16878 : : /* Effective C++ rules below. */
16879 : :
16880 : : /* More Effective C++ rule 7. */
16881 : 54 : if (operator_code == TRUTH_ANDIF_EXPR
16882 : 54 : || operator_code == TRUTH_ORIF_EXPR
16883 : 54 : || operator_code == COMPOUND_EXPR)
16884 : 0 : warning_at (loc, OPT_Weffc__,
16885 : : "user-defined %qD always evaluates both arguments", decl);
16886 : :
16887 : : /* More Effective C++ rule 6. */
16888 : 54 : if (operator_code == POSTINCREMENT_EXPR
16889 : : || operator_code == POSTDECREMENT_EXPR
16890 : : || operator_code == PREINCREMENT_EXPR
16891 : 54 : || operator_code == PREDECREMENT_EXPR)
16892 : : {
16893 : 0 : tree arg = TREE_VALUE (argtypes);
16894 : 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
16895 : 0 : if (methodp || TYPE_REF_P (arg))
16896 : 0 : arg = TREE_TYPE (arg);
16897 : 0 : arg = TYPE_MAIN_VARIANT (arg);
16898 : :
16899 : 0 : if (operator_code == PREINCREMENT_EXPR
16900 : : || operator_code == PREDECREMENT_EXPR)
16901 : : {
16902 : 0 : if (!TYPE_REF_P (ret)
16903 : 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
16904 : 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
16905 : : build_reference_type (arg));
16906 : : }
16907 : : else
16908 : : {
16909 : 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
16910 : 0 : warning_at (loc, OPT_Weffc__,
16911 : : "postfix %qD should return %qT", decl, arg);
16912 : : }
16913 : : }
16914 : :
16915 : : /* Effective C++ rule 23. */
16916 : 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
16917 : : && (operator_code == PLUS_EXPR
16918 : 9 : || operator_code == MINUS_EXPR
16919 : 9 : || operator_code == TRUNC_DIV_EXPR
16920 : : || operator_code == MULT_EXPR
16921 : : || operator_code == TRUNC_MOD_EXPR)
16922 : 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
16923 : 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
16924 : :
16925 : : return true;
16926 : : }
16927 : :
16928 : : /* Return a string giving the keyword associate with CODE. */
16929 : :
16930 : : static const char *
16931 : 111 : tag_name (enum tag_types code)
16932 : : {
16933 : 111 : switch (code)
16934 : : {
16935 : : case record_type:
16936 : : return "struct";
16937 : 60 : case class_type:
16938 : 60 : return "class";
16939 : 0 : case union_type:
16940 : 0 : return "union";
16941 : 3 : case enum_type:
16942 : 3 : return "enum";
16943 : 0 : case typename_type:
16944 : 0 : return "typename";
16945 : 0 : default:
16946 : 0 : gcc_unreachable ();
16947 : : }
16948 : : }
16949 : :
16950 : : /* Name lookup in an elaborated-type-specifier (after the keyword
16951 : : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
16952 : : elaborated-type-specifier is invalid, issue a diagnostic and return
16953 : : error_mark_node; otherwise, return the *_TYPE to which it referred.
16954 : : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
16955 : :
16956 : : tree
16957 : 11144664 : check_elaborated_type_specifier (enum tag_types tag_code,
16958 : : tree decl,
16959 : : bool allow_template_p)
16960 : : {
16961 : 11144664 : tree type;
16962 : :
16963 : : /* In the case of:
16964 : :
16965 : : struct S { struct S *p; };
16966 : :
16967 : : name lookup will find the TYPE_DECL for the implicit "S::S"
16968 : : typedef. Adjust for that here. */
16969 : 11144664 : if (DECL_SELF_REFERENCE_P (decl))
16970 : 329281 : decl = TYPE_NAME (TREE_TYPE (decl));
16971 : :
16972 : 11144664 : type = TREE_TYPE (decl);
16973 : :
16974 : : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
16975 : : is false for this case as well. */
16976 : 11144664 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16977 : : {
16978 : 9 : error ("using template type parameter %qT after %qs",
16979 : : type, tag_name (tag_code));
16980 : 9 : return error_mark_node;
16981 : : }
16982 : : /* Accept template template parameters. */
16983 : 11144655 : else if (allow_template_p
16984 : 6379963 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
16985 : 6379951 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
16986 : : ;
16987 : : /* [dcl.type.elab]
16988 : :
16989 : : If the identifier resolves to a typedef-name or the
16990 : : simple-template-id resolves to an alias template
16991 : : specialization, the elaborated-type-specifier is ill-formed.
16992 : :
16993 : : In other words, the only legitimate declaration to use in the
16994 : : elaborated type specifier is the implicit typedef created when
16995 : : the type is declared. */
16996 : 11144640 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
16997 : 329392 : && !DECL_SELF_REFERENCE_P (decl)
16998 : 11144751 : && tag_code != typename_type)
16999 : : {
17000 : 78 : auto_diagnostic_group d;
17001 : 78 : if (alias_template_specialization_p (type, nt_opaque))
17002 : 21 : error ("using alias template specialization %qT after %qs",
17003 : : type, tag_name (tag_code));
17004 : : else
17005 : 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
17006 : 78 : inform (DECL_SOURCE_LOCATION (decl),
17007 : : "%qD has a previous declaration here", decl);
17008 : 78 : return error_mark_node;
17009 : 78 : }
17010 : 11144562 : else if (TREE_CODE (type) != RECORD_TYPE
17011 : 11144562 : && TREE_CODE (type) != UNION_TYPE
17012 : : && tag_code != enum_type
17013 : 62938 : && tag_code != typename_type)
17014 : : {
17015 : 6 : auto_diagnostic_group d;
17016 : 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
17017 : 6 : inform (location_of (type), "%qT has a previous declaration here", type);
17018 : 6 : return error_mark_node;
17019 : 6 : }
17020 : 11144556 : else if (TREE_CODE (type) != ENUMERAL_TYPE
17021 : 11081654 : && tag_code == enum_type)
17022 : : {
17023 : 3 : auto_diagnostic_group d;
17024 : 3 : error ("%qT referred to as enum", type);
17025 : 3 : inform (location_of (type), "%qT has a previous declaration here", type);
17026 : 3 : return error_mark_node;
17027 : 3 : }
17028 : 11144553 : else if (!allow_template_p
17029 : 4764632 : && TREE_CODE (type) == RECORD_TYPE
17030 : 15793403 : && CLASSTYPE_IS_TEMPLATE (type))
17031 : : {
17032 : : /* If a class template appears as elaborated type specifier
17033 : : without a template header such as:
17034 : :
17035 : : template <class T> class C {};
17036 : : void f(class C); // No template header here
17037 : :
17038 : : then the required template argument is missing. */
17039 : 18 : error ("template argument required for %<%s %T%>",
17040 : : tag_name (tag_code),
17041 : 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
17042 : 18 : return error_mark_node;
17043 : : }
17044 : :
17045 : : return type;
17046 : : }
17047 : :
17048 : : /* Lookup NAME of an elaborated type specifier according to SCOPE and
17049 : : issue diagnostics if necessary. Return *_TYPE node upon success,
17050 : : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
17051 : : error. */
17052 : :
17053 : : static tree
17054 : 21927018 : lookup_and_check_tag (enum tag_types tag_code, tree name,
17055 : : TAG_how how, bool template_header_p)
17056 : : {
17057 : 21927018 : tree decl;
17058 : 21927018 : if (how == TAG_how::GLOBAL)
17059 : : {
17060 : : /* First try ordinary name lookup, ignoring hidden class name
17061 : : injected via friend declaration. */
17062 : 1878267 : decl = lookup_name (name, LOOK_want::TYPE);
17063 : : /* If that fails, the name will be placed in the smallest
17064 : : non-class, non-function-prototype scope according to 3.3.1/5.
17065 : : We may already have a hidden name declared as friend in this
17066 : : scope. So lookup again but not ignoring hidden names.
17067 : : If we find one, that name will be made visible rather than
17068 : : creating a new tag. */
17069 : 1878267 : if (!decl)
17070 : 17951 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
17071 : : }
17072 : : else
17073 : 20048751 : decl = lookup_elaborated_type (name, how);
17074 : :
17075 : 20066702 : if (!decl)
17076 : : /* We found nothing. */
17077 : : return NULL_TREE;
17078 : :
17079 : 4921607 : if (TREE_CODE (decl) == TREE_LIST)
17080 : : {
17081 : 3 : auto_diagnostic_group d;
17082 : 3 : error ("reference to %qD is ambiguous", name);
17083 : 3 : print_candidates (decl);
17084 : 3 : return error_mark_node;
17085 : 3 : }
17086 : :
17087 : 2061994 : if (DECL_CLASS_TEMPLATE_P (decl)
17088 : 2061988 : && !template_header_p
17089 : 6983592 : && how == TAG_how::CURRENT_ONLY)
17090 : : {
17091 : 12 : auto_diagnostic_group d;
17092 : 12 : error ("class template %qD redeclared as non-template", name);
17093 : 12 : inform (location_of (decl), "previous declaration here");
17094 : 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
17095 : 12 : return error_mark_node;
17096 : 12 : }
17097 : :
17098 : 2061982 : if (DECL_CLASS_TEMPLATE_P (decl)
17099 : : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
17100 : : so ignore a template template parameter. */
17101 : 4921598 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
17102 : 2061979 : decl = DECL_TEMPLATE_RESULT (decl);
17103 : :
17104 : 4921592 : if (TREE_CODE (decl) != TYPE_DECL)
17105 : : /* Found not-a-type. */
17106 : : return NULL_TREE;
17107 : :
17108 : : /* Look for invalid nested type:
17109 : : class C {
17110 : : class C {};
17111 : : }; */
17112 : 4921571 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
17113 : : {
17114 : 6 : error ("%qD has the same name as the class in which it is "
17115 : : "declared", decl);
17116 : 6 : return error_mark_node;
17117 : : }
17118 : :
17119 : : /* Two cases we need to consider when deciding if a class
17120 : : template is allowed as an elaborated type specifier:
17121 : : 1. It is a self reference to its own class.
17122 : : 2. It comes with a template header.
17123 : :
17124 : : For example:
17125 : :
17126 : : template <class T> class C {
17127 : : class C *c1; // DECL_SELF_REFERENCE_P is true
17128 : : class D;
17129 : : };
17130 : : template <class U> class C; // template_header_p is true
17131 : : template <class T> class C<T>::D {
17132 : : class C *c2; // DECL_SELF_REFERENCE_P is true
17133 : : }; */
17134 : :
17135 : 14764695 : tree t = check_elaborated_type_specifier (tag_code, decl,
17136 : : template_header_p
17137 : 9513864 : | DECL_SELF_REFERENCE_P (decl));
17138 : 2180140 : if (template_header_p && t && CLASS_TYPE_P (t)
17139 : 7101687 : && (!CLASSTYPE_TEMPLATE_INFO (t)
17140 : 2180104 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
17141 : : {
17142 : 21 : auto_diagnostic_group d;
17143 : 21 : error ("%qT is not a template", t);
17144 : 21 : inform (location_of (t), "previous declaration here");
17145 : 42 : if (TYPE_CLASS_SCOPE_P (t)
17146 : 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
17147 : 3 : inform (input_location,
17148 : : "perhaps you want to explicitly add %<%T::%>",
17149 : 3 : TYPE_CONTEXT (t));
17150 : 21 : return error_mark_node;
17151 : 21 : }
17152 : :
17153 : : return t;
17154 : : }
17155 : :
17156 : : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
17157 : : Define the tag as a forward-reference if it is not defined.
17158 : :
17159 : : If a declaration is given, process it here, and report an error if
17160 : : multiple declarations are not identical.
17161 : :
17162 : : SCOPE is TS_CURRENT when this is also a definition. Only look in
17163 : : the current frame for the name (since C++ allows new names in any
17164 : : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
17165 : : declaration. Only look beginning from the current scope outward up
17166 : : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
17167 : :
17168 : : TEMPLATE_HEADER_P is true when this declaration is preceded by
17169 : : a set of template parameters. */
17170 : :
17171 : : tree
17172 : 21421443 : xref_tag (enum tag_types tag_code, tree name,
17173 : : TAG_how how, bool template_header_p)
17174 : : {
17175 : 21421443 : enum tree_code code;
17176 : 21421443 : tree context = NULL_TREE;
17177 : :
17178 : 21421443 : auto_cond_timevar tv (TV_NAME_LOOKUP);
17179 : :
17180 : 21421443 : gcc_assert (identifier_p (name));
17181 : :
17182 : 21421443 : switch (tag_code)
17183 : : {
17184 : : case record_type:
17185 : : case class_type:
17186 : : code = RECORD_TYPE;
17187 : : break;
17188 : : case union_type:
17189 : : code = UNION_TYPE;
17190 : : break;
17191 : : case enum_type:
17192 : : code = ENUMERAL_TYPE;
17193 : : break;
17194 : 0 : default:
17195 : 0 : gcc_unreachable ();
17196 : : }
17197 : :
17198 : : /* In case of anonymous name, xref_tag is only called to
17199 : : make type node and push name. Name lookup is not required. */
17200 : 21421443 : tree t = NULL_TREE;
17201 : 21421443 : if (!IDENTIFIER_ANON_P (name))
17202 : 19874811 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
17203 : :
17204 : 21421443 : if (t == error_mark_node)
17205 : : return error_mark_node;
17206 : :
17207 : 2455657 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
17208 : 1064067 : && template_class_depth (current_class_type)
17209 : 21825404 : && template_header_p)
17210 : : {
17211 : 272158 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
17212 : : return t;
17213 : :
17214 : : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
17215 : : a definition of this tag. Since, in addition, we are
17216 : : currently processing a (member) template declaration of a
17217 : : template class, we must be very careful; consider:
17218 : :
17219 : : template <class X> struct S1
17220 : :
17221 : : template <class U> struct S2
17222 : : {
17223 : : template <class V> friend struct S1;
17224 : : };
17225 : :
17226 : : Here, the S2::S1 declaration should not be confused with the
17227 : : outer declaration. In particular, the inner version should
17228 : : have a template parameter of level 2, not level 1.
17229 : :
17230 : : On the other hand, when presented with:
17231 : :
17232 : : template <class T> struct S1
17233 : : {
17234 : : template <class U> struct S2 {};
17235 : : template <class U> friend struct S2;
17236 : : };
17237 : :
17238 : : the friend must find S1::S2 eventually. We accomplish this
17239 : : by making sure that the new type we create to represent this
17240 : : declaration has the right TYPE_CONTEXT. */
17241 : 272155 : context = TYPE_CONTEXT (t);
17242 : 272155 : t = NULL_TREE;
17243 : : }
17244 : :
17245 : 21421329 : if (! t)
17246 : : {
17247 : : /* If no such tag is yet defined, create a forward-reference node
17248 : : and record it as the "definition".
17249 : : When a real declaration of this type is found,
17250 : : the forward-reference will be altered into a real type. */
17251 : 16806251 : if (code == ENUMERAL_TYPE)
17252 : : {
17253 : 14 : error ("use of enum %q#D without previous declaration", name);
17254 : 14 : return error_mark_node;
17255 : : }
17256 : :
17257 : 16806237 : t = make_class_type (code);
17258 : 16806237 : TYPE_CONTEXT (t) = context;
17259 : 16806237 : if (IDENTIFIER_LAMBDA_P (name))
17260 : : /* Mark it as a lambda type right now. Our caller will
17261 : : correct the value. */
17262 : 900872 : CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
17263 : 16806237 : t = pushtag (name, t, how);
17264 : : }
17265 : : else
17266 : : {
17267 : 4615078 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
17268 : : {
17269 : : /* Check that we aren't trying to overload a class with different
17270 : : constraints. */
17271 : 1907946 : if (!redeclare_class_template (t, current_template_parms,
17272 : : current_template_constraints ()))
17273 : 60 : return error_mark_node;
17274 : : }
17275 : 2707132 : else if (!processing_template_decl
17276 : 2554219 : && CLASS_TYPE_P (t)
17277 : 5235331 : && CLASSTYPE_IS_TEMPLATE (t))
17278 : : {
17279 : 0 : auto_diagnostic_group d;
17280 : 0 : error ("redeclaration of %qT as a non-template", t);
17281 : 0 : inform (location_of (t), "previous declaration %qD", t);
17282 : 0 : return error_mark_node;
17283 : 0 : }
17284 : :
17285 : 4615018 : if (modules_p ()
17286 : 4615018 : && how == TAG_how::CURRENT_ONLY)
17287 : : {
17288 : 8608 : tree decl = TYPE_NAME (t);
17289 : 8608 : if (!module_may_redeclare (decl))
17290 : 15 : return error_mark_node;
17291 : :
17292 : 8593 : tree not_tmpl = STRIP_TEMPLATE (decl);
17293 : 8593 : if (DECL_LANG_SPECIFIC (not_tmpl)
17294 : 3996 : && DECL_MODULE_ATTACH_P (not_tmpl)
17295 : 39 : && !DECL_MODULE_EXPORT_P (not_tmpl)
17296 : 8626 : && module_exporting_p ())
17297 : : {
17298 : 6 : auto_diagnostic_group d;
17299 : 6 : error ("conflicting exporting for declaration %qD", decl);
17300 : 6 : inform (DECL_SOURCE_LOCATION (decl),
17301 : : "previously declared here without exporting");
17302 : 6 : }
17303 : :
17304 : 8593 : tree maybe_tmpl = decl;
17305 : 8593 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
17306 : 6285 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
17307 : :
17308 : : /* FIXME: we should do a more precise check for redefinitions
17309 : : of a conflicting using-declaration here, as these diagnostics
17310 : : are not ideal. */
17311 : 8593 : if (DECL_LANG_SPECIFIC (decl)
17312 : 3996 : && DECL_MODULE_IMPORT_P (decl)
17313 : 8638 : && CP_DECL_CONTEXT (decl) == current_namespace)
17314 : : {
17315 : : /* Push it into this TU's symbol slot. */
17316 : 42 : if (maybe_tmpl != decl)
17317 : : /* We're in the template parm binding level.
17318 : : Pushtag has logic to slide under that, but we're
17319 : : not pushing a *new* type. */
17320 : 18 : push_nested_namespace (CP_DECL_CONTEXT (decl));
17321 : :
17322 : 42 : pushdecl (maybe_tmpl);
17323 : 42 : if (maybe_tmpl != decl)
17324 : 18 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
17325 : : }
17326 : :
17327 : 8593 : set_instantiating_module (maybe_tmpl);
17328 : : }
17329 : : }
17330 : :
17331 : : return t;
17332 : 21421443 : }
17333 : :
17334 : : /* Create the binfo hierarchy for REF with (possibly NULL) base list
17335 : : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
17336 : : access_* node, and the TREE_VALUE is the type of the base-class.
17337 : : Non-NULL TREE_TYPE indicates virtual inheritance. */
17338 : :
17339 : : void
17340 : 53806346 : xref_basetypes (tree ref, tree base_list)
17341 : : {
17342 : 53806346 : tree *basep;
17343 : 53806346 : tree binfo, base_binfo;
17344 : 53806346 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
17345 : 53806346 : unsigned max_bases = 0; /* Maximum direct bases. */
17346 : 53806346 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
17347 : 53806346 : int i;
17348 : 53806346 : tree default_access;
17349 : 53806346 : tree igo_prev; /* Track Inheritance Graph Order. */
17350 : :
17351 : 53806346 : if (ref == error_mark_node)
17352 : 12 : return;
17353 : :
17354 : : /* The base of a derived class is private by default, all others are
17355 : : public. */
17356 : 53806346 : default_access = (TREE_CODE (ref) == RECORD_TYPE
17357 : 53297472 : && CLASSTYPE_DECLARED_CLASS (ref)
17358 : 53806346 : ? access_private_node : access_public_node);
17359 : :
17360 : : /* First, make sure that any templates in base-classes are
17361 : : instantiated. This ensures that if we call ourselves recursively
17362 : : we do not get confused about which classes are marked and which
17363 : : are not. */
17364 : 53806346 : basep = &base_list;
17365 : 81685183 : while (*basep)
17366 : : {
17367 : 27878837 : tree basetype = TREE_VALUE (*basep);
17368 : :
17369 : : /* The dependent_type_p call below should really be dependent_scope_p
17370 : : so that we give a hard error about using an incomplete type as a
17371 : : base, but we allow it with a pedwarn for backward
17372 : : compatibility. */
17373 : 27878837 : if (processing_template_decl
17374 : 27878837 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
17375 : 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
17376 : 27878837 : if (!dependent_type_p (basetype)
17377 : 27878837 : && !complete_type_or_else (basetype, NULL))
17378 : : /* An incomplete type. Remove it from the list. */
17379 : 15 : *basep = TREE_CHAIN (*basep);
17380 : : else
17381 : : {
17382 : 27878822 : max_bases++;
17383 : 27878822 : if (TREE_TYPE (*basep))
17384 : 68840 : max_dvbases++;
17385 : 27878822 : if (CLASS_TYPE_P (basetype))
17386 : 27061910 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17387 : 27878822 : basep = &TREE_CHAIN (*basep);
17388 : : }
17389 : : }
17390 : 53806346 : max_vbases += max_dvbases;
17391 : :
17392 : 53806346 : TYPE_MARKED_P (ref) = 1;
17393 : :
17394 : : /* The binfo slot should be empty, unless this is an (ill-formed)
17395 : : redefinition. */
17396 : 53806346 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
17397 : :
17398 : 53806346 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
17399 : :
17400 : 53806346 : binfo = make_tree_binfo (max_bases);
17401 : :
17402 : 53806346 : TYPE_BINFO (ref) = binfo;
17403 : 53806346 : BINFO_OFFSET (binfo) = size_zero_node;
17404 : 53806346 : BINFO_TYPE (binfo) = ref;
17405 : :
17406 : : /* Apply base-class info set up to the variants of this type. */
17407 : 53806346 : fixup_type_variants (ref);
17408 : :
17409 : 53806346 : if (max_bases)
17410 : : {
17411 : 27004606 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
17412 : : /* A C++98 POD cannot have base classes. */
17413 : 27004606 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
17414 : :
17415 : 27004606 : if (TREE_CODE (ref) == UNION_TYPE)
17416 : : {
17417 : 12 : error ("derived union %qT invalid", ref);
17418 : 12 : return;
17419 : : }
17420 : : }
17421 : :
17422 : 27004594 : if (max_bases > 1)
17423 : 679171 : warning (OPT_Wmultiple_inheritance,
17424 : : "%qT defined with multiple direct bases", ref);
17425 : :
17426 : 53806334 : if (max_vbases)
17427 : : {
17428 : : /* An aggregate can't have virtual base classes. */
17429 : 208810 : CLASSTYPE_NON_AGGREGATE (ref) = true;
17430 : :
17431 : 208810 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
17432 : :
17433 : 208810 : if (max_dvbases)
17434 : 67840 : warning (OPT_Wvirtual_inheritance,
17435 : : "%qT defined with direct virtual base", ref);
17436 : : }
17437 : :
17438 : 81685144 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
17439 : : {
17440 : 27878810 : tree access = TREE_PURPOSE (base_list);
17441 : 27878810 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
17442 : 27878810 : tree basetype = TREE_VALUE (base_list);
17443 : :
17444 : 27878810 : if (access == access_default_node)
17445 : 4412801 : access = default_access;
17446 : :
17447 : : /* Before C++17, an aggregate cannot have base classes. In C++17, an
17448 : : aggregate can't have virtual, private, or protected base classes. */
17449 : 27878810 : if (cxx_dialect < cxx17
17450 : 27669907 : || access != access_public_node
17451 : 26770532 : || via_virtual)
17452 : 1174358 : CLASSTYPE_NON_AGGREGATE (ref) = true;
17453 : :
17454 : 27878810 : if (PACK_EXPANSION_P (basetype))
17455 : 20808 : basetype = PACK_EXPANSION_PATTERN (basetype);
17456 : 27878810 : if (TREE_CODE (basetype) == TYPE_DECL)
17457 : 0 : basetype = TREE_TYPE (basetype);
17458 : 27878810 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
17459 : : {
17460 : 24 : error ("base type %qT fails to be a struct or class type",
17461 : : basetype);
17462 : 24 : goto dropped_base;
17463 : : }
17464 : :
17465 : 27878786 : base_binfo = NULL_TREE;
17466 : 27878786 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
17467 : : {
17468 : 21344028 : base_binfo = TYPE_BINFO (basetype);
17469 : : /* The original basetype could have been a typedef'd type. */
17470 : 21344028 : basetype = BINFO_TYPE (base_binfo);
17471 : :
17472 : : /* Inherit flags from the base. */
17473 : 64032084 : TYPE_HAS_NEW_OPERATOR (ref)
17474 : 21344028 : |= TYPE_HAS_NEW_OPERATOR (basetype);
17475 : 64032084 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
17476 : 21344028 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
17477 : 21344028 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
17478 : 21344028 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
17479 : 64032084 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
17480 : 21344028 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
17481 : 64032084 : CLASSTYPE_REPEATED_BASE_P (ref)
17482 : 21344028 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
17483 : : }
17484 : :
17485 : : /* We must do this test after we've seen through a typedef
17486 : : type. */
17487 : 27878786 : if (TYPE_MARKED_P (basetype))
17488 : : {
17489 : 18 : if (basetype == ref)
17490 : 0 : error ("recursive type %qT undefined", basetype);
17491 : : else
17492 : 18 : error ("duplicate base type %qT invalid", basetype);
17493 : 18 : goto dropped_base;
17494 : : }
17495 : :
17496 : 27878768 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
17497 : : /* Regenerate the pack expansion for the bases. */
17498 : 20808 : basetype = make_pack_expansion (basetype);
17499 : :
17500 : 27878768 : TYPE_MARKED_P (basetype) = 1;
17501 : :
17502 : 27878768 : base_binfo = copy_binfo (base_binfo, basetype, ref,
17503 : : &igo_prev, via_virtual);
17504 : 27878768 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
17505 : 27809943 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
17506 : :
17507 : 27878768 : BINFO_BASE_APPEND (binfo, base_binfo);
17508 : 27878768 : BINFO_BASE_ACCESS_APPEND (binfo, access);
17509 : 27878768 : continue;
17510 : :
17511 : 42 : dropped_base:
17512 : : /* Update max_vbases to reflect the reality that we are dropping
17513 : : this base: if it reaches zero we want to undo the vec_alloc
17514 : : above to avoid inconsistencies during error-recovery: eg, in
17515 : : build_special_member_call, CLASSTYPE_VBASECLASSES non null
17516 : : and vtt null (c++/27952). */
17517 : 42 : if (via_virtual)
17518 : 12 : max_vbases--;
17519 : 42 : if (CLASS_TYPE_P (basetype))
17520 : 30 : max_vbases
17521 : 33 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17522 : 27878768 : }
17523 : :
17524 : 53806334 : if (CLASSTYPE_VBASECLASSES (ref)
17525 : 53806334 : && max_vbases == 0)
17526 : 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
17527 : :
17528 : 54015132 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
17529 : : /* If we didn't get max_vbases vbases, we must have shared at
17530 : : least one of them, and are therefore diamond shaped. */
17531 : 22320 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
17532 : :
17533 : : /* Unmark all the types. */
17534 : 81685102 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
17535 : 27878768 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
17536 : 53806334 : TYPE_MARKED_P (ref) = 0;
17537 : :
17538 : : /* Now see if we have a repeated base type. */
17539 : 53806334 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
17540 : : {
17541 : 138984804 : for (base_binfo = binfo; base_binfo;
17542 : 85178902 : base_binfo = TREE_CHAIN (base_binfo))
17543 : : {
17544 : 85179612 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
17545 : : {
17546 : 710 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
17547 : 710 : break;
17548 : : }
17549 : 85178902 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
17550 : : }
17551 : 138984804 : for (base_binfo = binfo; base_binfo;
17552 : 85178902 : base_binfo = TREE_CHAIN (base_binfo))
17553 : 85179612 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
17554 : 85178902 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
17555 : : else
17556 : : break;
17557 : : }
17558 : : }
17559 : :
17560 : :
17561 : : /* Copies the enum-related properties from type SRC to type DST.
17562 : : Used with the underlying type of an enum and the enum itself. */
17563 : : static void
17564 : 1733138 : copy_type_enum (tree dst, tree src)
17565 : : {
17566 : 1733138 : tree t;
17567 : 3466294 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
17568 : : {
17569 : 1733156 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
17570 : 1733156 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
17571 : 1733156 : TYPE_SIZE (t) = TYPE_SIZE (src);
17572 : 1733156 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
17573 : 1733156 : SET_TYPE_MODE (dst, TYPE_MODE (src));
17574 : 1733156 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
17575 : 1733156 : unsigned valign = TYPE_ALIGN (src);
17576 : 1733156 : if (TYPE_USER_ALIGN (t))
17577 : 12 : valign = MAX (valign, TYPE_ALIGN (t));
17578 : : else
17579 : 1733144 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
17580 : 1733156 : SET_TYPE_ALIGN (t, valign);
17581 : 1733156 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
17582 : : }
17583 : 1733138 : }
17584 : :
17585 : : /* Begin compiling the definition of an enumeration type.
17586 : : NAME is its name,
17587 : :
17588 : : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
17589 : :
17590 : : UNDERLYING_TYPE is the type that will be used as the storage for
17591 : : the enumeration type. This should be NULL_TREE if no storage type
17592 : : was specified.
17593 : :
17594 : : ATTRIBUTES are any attributes specified after the enum-key.
17595 : :
17596 : : SCOPED_ENUM_P is true if this is a scoped enumeration type.
17597 : :
17598 : : if IS_NEW is not NULL, gets TRUE iff a new type is created.
17599 : :
17600 : : Returns the type object, as yet incomplete.
17601 : : Also records info about it so that build_enumerator
17602 : : may be used to declare the individual values as they are read. */
17603 : :
17604 : : tree
17605 : 2052338 : start_enum (tree name, tree enumtype, tree underlying_type,
17606 : : tree attributes, bool scoped_enum_p, bool *is_new)
17607 : : {
17608 : 2052338 : tree prevtype = NULL_TREE;
17609 : 2052338 : gcc_assert (identifier_p (name));
17610 : :
17611 : 2052338 : if (is_new)
17612 : 1637550 : *is_new = false;
17613 : : /* [C++0x dcl.enum]p5:
17614 : :
17615 : : If not explicitly specified, the underlying type of a scoped
17616 : : enumeration type is int. */
17617 : 2052338 : if (!underlying_type && scoped_enum_p)
17618 : 118097 : underlying_type = integer_type_node;
17619 : :
17620 : 2052338 : if (underlying_type)
17621 : 430570 : underlying_type = cv_unqualified (underlying_type);
17622 : :
17623 : : /* If this is the real definition for a previous forward reference,
17624 : : fill in the contents in the same object that used to be the
17625 : : forward reference. */
17626 : 2052338 : if (!enumtype)
17627 : 2052207 : enumtype = lookup_and_check_tag (enum_type, name,
17628 : : /*tag_scope=*/TAG_how::CURRENT_ONLY,
17629 : : /*template_header_p=*/false);
17630 : :
17631 : : /* In case of a template_decl, the only check that should be deferred
17632 : : to instantiation time is the comparison of underlying types. */
17633 : 2052338 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
17634 : : {
17635 : : /* Attempt to set the declaring module. */
17636 : 34367 : if (modules_p ())
17637 : : {
17638 : 160 : tree decl = TYPE_NAME (enumtype);
17639 : 160 : if (!module_may_redeclare (decl))
17640 : 3 : enumtype = error_mark_node;
17641 : : else
17642 : 157 : set_instantiating_module (decl);
17643 : : }
17644 : :
17645 : 34367 : if (enumtype == error_mark_node)
17646 : : ;
17647 : 34554 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
17648 : : {
17649 : 6 : auto_diagnostic_group d;
17650 : 6 : error_at (input_location, "scoped/unscoped mismatch "
17651 : : "in enum %q#T", enumtype);
17652 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17653 : : "previous definition here");
17654 : 6 : enumtype = error_mark_node;
17655 : 6 : }
17656 : 34358 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
17657 : : {
17658 : 0 : auto_diagnostic_group d;
17659 : 0 : error_at (input_location, "underlying type mismatch "
17660 : : "in enum %q#T", enumtype);
17661 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17662 : : "previous definition here");
17663 : 0 : enumtype = error_mark_node;
17664 : 0 : }
17665 : 34342 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
17666 : 68700 : && !same_type_p (underlying_type,
17667 : : ENUM_UNDERLYING_TYPE (enumtype)))
17668 : : {
17669 : 26 : auto_diagnostic_group d;
17670 : 26 : error_at (input_location, "different underlying type "
17671 : : "in enum %q#T", enumtype);
17672 : 26 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17673 : : "previous definition here");
17674 : 26 : underlying_type = NULL_TREE;
17675 : 26 : }
17676 : : }
17677 : :
17678 : 2052338 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
17679 : 34358 : || processing_template_decl)
17680 : : {
17681 : : /* In case of error, make a dummy enum to allow parsing to
17682 : : continue. */
17683 : 2018068 : if (enumtype == error_mark_node)
17684 : : {
17685 : 9 : name = make_anon_name ();
17686 : 9 : enumtype = NULL_TREE;
17687 : : }
17688 : :
17689 : : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
17690 : : of an opaque enum, or an opaque enum of an already defined
17691 : : enumeration (C++11).
17692 : : In any other case, it'll be NULL_TREE. */
17693 : 2018068 : if (!enumtype)
17694 : : {
17695 : 2017977 : if (is_new)
17696 : 1603192 : *is_new = true;
17697 : : }
17698 : 2018068 : prevtype = enumtype;
17699 : :
17700 : : /* Do not push the decl more than once. */
17701 : 2018068 : if (!enumtype
17702 : 91 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
17703 : : {
17704 : 2017980 : enumtype = cxx_make_type (ENUMERAL_TYPE);
17705 : 2017980 : enumtype = pushtag (name, enumtype);
17706 : :
17707 : 2017980 : if (enumtype != error_mark_node)
17708 : : {
17709 : : /* The enum is considered opaque until the opening '{' of the
17710 : : enumerator list. */
17711 : 2017962 : SET_OPAQUE_ENUM_P (enumtype, true);
17712 : 2017962 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
17713 : :
17714 : : /* std::byte aliases anything. */
17715 : 2017962 : if (TYPE_CONTEXT (enumtype) == std_node
17716 : 2335475 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
17717 : 12660 : TYPE_ALIAS_SET (enumtype) = 0;
17718 : : }
17719 : : }
17720 : : else
17721 : 88 : enumtype = xref_tag (enum_type, name);
17722 : :
17723 : 2018068 : if (enumtype == error_mark_node)
17724 : : return error_mark_node;
17725 : : }
17726 : :
17727 : 2052320 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
17728 : :
17729 : 2052320 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
17730 : :
17731 : 2052320 : if (underlying_type)
17732 : : {
17733 : 430541 : if (ENUM_UNDERLYING_TYPE (enumtype))
17734 : : /* We already checked that it matches, don't change it to a different
17735 : : typedef variant. */;
17736 : 396225 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
17737 : : {
17738 : 396161 : copy_type_enum (enumtype, underlying_type);
17739 : 396161 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17740 : : }
17741 : 64 : else if (dependent_type_p (underlying_type))
17742 : 51 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17743 : : else
17744 : : {
17745 : 13 : error ("underlying type %qT of %qT must be an integral type",
17746 : : underlying_type, enumtype);
17747 : 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
17748 : : }
17749 : : }
17750 : :
17751 : : /* If into a template class, the returned enum is always the first
17752 : : declaration (opaque or not) seen. This way all the references to
17753 : : this type will be to the same declaration. The following ones are used
17754 : : only to check for definition errors. */
17755 : 2052320 : if (prevtype && processing_template_decl)
17756 : : return prevtype;
17757 : : else
17758 : 2052232 : return enumtype;
17759 : : }
17760 : :
17761 : : /* After processing and defining all the values of an enumeration type,
17762 : : install their decls in the enumeration type.
17763 : : ENUMTYPE is the type object. */
17764 : :
17765 : : void
17766 : 1953801 : finish_enum_value_list (tree enumtype)
17767 : : {
17768 : 1953801 : tree values;
17769 : 1953801 : tree underlying_type;
17770 : 1953801 : tree decl;
17771 : 1953801 : tree value;
17772 : 1953801 : tree minnode, maxnode;
17773 : 1953801 : tree t;
17774 : :
17775 : 1953801 : bool fixed_underlying_type_p
17776 : 1953801 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
17777 : :
17778 : : /* We built up the VALUES in reverse order. */
17779 : 1953801 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
17780 : :
17781 : : /* For an enum defined in a template, just set the type of the values;
17782 : : all further processing is postponed until the template is
17783 : : instantiated. We need to set the type so that tsubst of a CONST_DECL
17784 : : works. */
17785 : 1953801 : if (processing_template_decl)
17786 : : {
17787 : 358486 : for (values = TYPE_VALUES (enumtype);
17788 : 663543 : values;
17789 : 358486 : values = TREE_CHAIN (values))
17790 : 358486 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
17791 : : return;
17792 : : }
17793 : :
17794 : : /* Determine the minimum and maximum values of the enumerators. */
17795 : 1648744 : if (TYPE_VALUES (enumtype))
17796 : : {
17797 : : minnode = maxnode = NULL_TREE;
17798 : :
17799 : 10394115 : for (values = TYPE_VALUES (enumtype);
17800 : 12015117 : values;
17801 : 10394115 : values = TREE_CHAIN (values))
17802 : : {
17803 : 10394115 : decl = TREE_VALUE (values);
17804 : :
17805 : : /* [dcl.enum]: Following the closing brace of an enum-specifier,
17806 : : each enumerator has the type of its enumeration. Prior to the
17807 : : closing brace, the type of each enumerator is the type of its
17808 : : initializing value. */
17809 : 10394115 : TREE_TYPE (decl) = enumtype;
17810 : :
17811 : : /* Update the minimum and maximum values, if appropriate. */
17812 : 10394115 : value = DECL_INITIAL (decl);
17813 : 10394115 : if (TREE_CODE (value) != INTEGER_CST)
17814 : 4 : value = integer_zero_node;
17815 : : /* Figure out what the minimum and maximum values of the
17816 : : enumerators are. */
17817 : 10394115 : if (!minnode)
17818 : : minnode = maxnode = value;
17819 : 8773113 : else if (tree_int_cst_lt (maxnode, value))
17820 : : maxnode = value;
17821 : 1329153 : else if (tree_int_cst_lt (value, minnode))
17822 : 141396 : minnode = value;
17823 : : }
17824 : : }
17825 : : else
17826 : : /* [dcl.enum]
17827 : :
17828 : : If the enumerator-list is empty, the underlying type is as if
17829 : : the enumeration had a single enumerator with value 0. */
17830 : 27742 : minnode = maxnode = integer_zero_node;
17831 : :
17832 : 1648744 : if (!fixed_underlying_type_p)
17833 : : {
17834 : : /* Compute the number of bits require to represent all values of the
17835 : : enumeration. We must do this before the type of MINNODE and
17836 : : MAXNODE are transformed, since tree_int_cst_min_precision relies
17837 : : on the TREE_TYPE of the value it is passed. */
17838 : 1336977 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
17839 : 1336977 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
17840 : 1336977 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
17841 : 1336977 : int precision = MAX (lowprec, highprec);
17842 : 1336977 : unsigned int itk;
17843 : 1336977 : bool use_short_enum;
17844 : :
17845 : : /* Determine the underlying type of the enumeration.
17846 : :
17847 : : [dcl.enum]
17848 : :
17849 : : The underlying type of an enumeration is an integral type that
17850 : : can represent all the enumerator values defined in the
17851 : : enumeration. It is implementation-defined which integral type is
17852 : : used as the underlying type for an enumeration except that the
17853 : : underlying type shall not be larger than int unless the value of
17854 : : an enumerator cannot fit in an int or unsigned int.
17855 : :
17856 : : We use "int" or an "unsigned int" as the underlying type, even if
17857 : : a smaller integral type would work, unless the user has
17858 : : explicitly requested that we use the smallest possible type. The
17859 : : user can request that for all enumerations with a command line
17860 : : flag, or for just one enumeration with an attribute. */
17861 : :
17862 : 2673954 : use_short_enum = flag_short_enums
17863 : 1336977 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
17864 : :
17865 : : /* If the precision of the type was specified with an attribute and it
17866 : : was too small, give an error. Otherwise, use it. */
17867 : 1336977 : if (TYPE_PRECISION (enumtype))
17868 : : {
17869 : 11 : if (precision > TYPE_PRECISION (enumtype))
17870 : 0 : error ("specified mode too small for enumerated values");
17871 : : else
17872 : : {
17873 : 11 : use_short_enum = true;
17874 : 11 : precision = TYPE_PRECISION (enumtype);
17875 : : }
17876 : : }
17877 : :
17878 : 2598217 : for (itk = (use_short_enum ? itk_char : itk_int);
17879 : 2598217 : itk != itk_none;
17880 : : itk++)
17881 : : {
17882 : 2598214 : underlying_type = integer_types[itk];
17883 : 2598214 : if (underlying_type != NULL_TREE
17884 : 2598196 : && TYPE_PRECISION (underlying_type) >= precision
17885 : 5194481 : && TYPE_SIGN (underlying_type) == sgn)
17886 : : break;
17887 : : }
17888 : 1336977 : if (itk == itk_none)
17889 : : {
17890 : : /* DR 377
17891 : :
17892 : : IF no integral type can represent all the enumerator values, the
17893 : : enumeration is ill-formed. */
17894 : 3 : error ("no integral type can represent all of the enumerator values "
17895 : : "for %qT", enumtype);
17896 : 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
17897 : 3 : underlying_type = integer_types[itk_unsigned_long_long];
17898 : : }
17899 : :
17900 : : /* [dcl.enum]
17901 : :
17902 : : The value of sizeof() applied to an enumeration type, an object
17903 : : of an enumeration type, or an enumerator, is the value of sizeof()
17904 : : applied to the underlying type. */
17905 : 1336977 : copy_type_enum (enumtype, underlying_type);
17906 : :
17907 : : /* Compute the minimum and maximum values for the type.
17908 : :
17909 : : [dcl.enum]
17910 : :
17911 : : For an enumeration where emin is the smallest enumerator and emax
17912 : : is the largest, the values of the enumeration are the values of the
17913 : : underlying type in the range bmin to bmax, where bmin and bmax are,
17914 : : respectively, the smallest and largest values of the smallest bit-
17915 : : field that can store emin and emax. */
17916 : :
17917 : : /* The middle-end currently assumes that types with TYPE_PRECISION
17918 : : narrower than their underlying type are suitably zero or sign
17919 : : extended to fill their mode. Similarly, it assumes that the front
17920 : : end assures that a value of a particular type must be within
17921 : : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
17922 : :
17923 : : We used to set these fields based on bmin and bmax, but that led
17924 : : to invalid assumptions like optimizing away bounds checking. So
17925 : : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
17926 : : TYPE_MAX_VALUE to the values for the mode above and only restrict
17927 : : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
17928 : 1336977 : ENUM_UNDERLYING_TYPE (enumtype)
17929 : 1336977 : = build_distinct_type_copy (underlying_type);
17930 : 1336977 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
17931 : 1336977 : set_min_and_max_values_for_integral_type
17932 : 1336977 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
17933 : :
17934 : : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
17935 : 1336977 : if (flag_strict_enums)
17936 : 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
17937 : :
17938 : 1336977 : if (use_short_enum)
17939 : : {
17940 : 79 : TYPE_PACKED (enumtype) = use_short_enum;
17941 : 79 : fixup_attribute_variants (enumtype);
17942 : : }
17943 : : }
17944 : : else
17945 : 311767 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
17946 : :
17947 : : /* If the enum is exported, mark the consts too. */
17948 : 1648744 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
17949 : 1405778 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
17950 : 1649244 : && at_namespace_scope_p ());
17951 : :
17952 : : /* Convert each of the enumerators to the type of the underlying
17953 : : type of the enumeration. */
17954 : 12042859 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
17955 : : {
17956 : 10394115 : decl = TREE_VALUE (values);
17957 : 10394115 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
17958 : 10394115 : if (fixed_underlying_type_p)
17959 : : /* If the enumeration type has a fixed underlying type, we
17960 : : already checked all of the enumerator values. */
17961 : 2979897 : value = DECL_INITIAL (decl);
17962 : : else
17963 : 7414218 : value = perform_implicit_conversion (underlying_type,
17964 : 7414218 : DECL_INITIAL (decl),
17965 : : tf_warning_or_error);
17966 : : /* Do not clobber shared ints. */
17967 : 10394115 : if (value != error_mark_node)
17968 : : {
17969 : 10394113 : value = copy_node (value);
17970 : :
17971 : 10394113 : TREE_TYPE (value) = enumtype;
17972 : : }
17973 : 10394115 : DECL_INITIAL (decl) = value;
17974 : 10394115 : if (export_p)
17975 : 2824 : DECL_MODULE_EXPORT_P (decl) = true;
17976 : 10394115 : }
17977 : :
17978 : : /* Fix up all variant types of this enum type. */
17979 : 3297506 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
17980 : 1648762 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
17981 : :
17982 : 1648744 : if (at_class_scope_p ()
17983 : 909545 : && COMPLETE_TYPE_P (current_class_type)
17984 : 1648805 : && UNSCOPED_ENUM_P (enumtype))
17985 : : {
17986 : 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
17987 : : /* TYPE_FIELDS needs fixup. */
17988 : 42 : fixup_type_variants (current_class_type);
17989 : : }
17990 : :
17991 : : /* Finish debugging output for this type. */
17992 : 1648744 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
17993 : :
17994 : : /* Each enumerator now has the type of its enumeration. Clear the cache
17995 : : so that this change in types doesn't confuse us later on. */
17996 : 1648744 : clear_cv_and_fold_caches ();
17997 : : }
17998 : :
17999 : : /* Finishes the enum type. This is called only the first time an
18000 : : enumeration is seen, be it opaque or odinary.
18001 : : ENUMTYPE is the type object. */
18002 : :
18003 : : void
18004 : 1943521 : finish_enum (tree enumtype)
18005 : : {
18006 : 1943521 : if (processing_template_decl)
18007 : : {
18008 : 305085 : if (at_function_scope_p ())
18009 : 2418 : add_stmt (build_min (TAG_DEFN, enumtype));
18010 : 305085 : return;
18011 : : }
18012 : :
18013 : : /* If this is a forward declaration, there should not be any variants,
18014 : : though we can get a variant in the middle of an enum-specifier with
18015 : : wacky code like 'enum E { e = sizeof(const E*) };' */
18016 : 3276872 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
18017 : : && (TYPE_VALUES (enumtype)
18018 : : || !TYPE_NEXT_VARIANT (enumtype)));
18019 : : }
18020 : :
18021 : : /* Build and install a CONST_DECL for an enumeration constant of the
18022 : : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
18023 : : Apply ATTRIBUTES if available. LOC is the location of NAME.
18024 : : Assignment of sequential values by default is handled here. */
18025 : :
18026 : : tree
18027 : 10752578 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
18028 : : location_t loc)
18029 : : {
18030 : 10752578 : tree decl;
18031 : 10752578 : tree context;
18032 : 10752578 : tree type;
18033 : :
18034 : : /* scalar_constant_value will pull out this expression, so make sure
18035 : : it's folded as appropriate.
18036 : :
18037 : : Creating a TARGET_EXPR in a template breaks when substituting, and
18038 : : here we would create it for instance when using a class prvalue with
18039 : : a user-defined conversion function. So don't use such a tree. We
18040 : : instantiate VALUE here to get errors about bad enumerators even in
18041 : : a template that does not get instantiated. */
18042 : 10752578 : if (processing_template_decl)
18043 : 358486 : value = maybe_fold_non_dependent_expr (value);
18044 : :
18045 : : /* If the VALUE was erroneous, pretend it wasn't there; that will
18046 : : result in the enum being assigned the next value in sequence. */
18047 : 10752578 : if (value == error_mark_node)
18048 : : value = NULL_TREE;
18049 : :
18050 : : /* Remove no-op casts from the value. */
18051 : 10752540 : if (value)
18052 : 8161059 : STRIP_TYPE_NOPS (value);
18053 : :
18054 : 10752578 : if (! processing_template_decl)
18055 : : {
18056 : : /* Validate and default VALUE. */
18057 : 10394092 : if (value != NULL_TREE)
18058 : : {
18059 : 4626435 : if (!ENUM_UNDERLYING_TYPE (enumtype))
18060 : : {
18061 : 2533103 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
18062 : : value, true);
18063 : 2533103 : if (tmp_value)
18064 : 4626435 : value = tmp_value;
18065 : : }
18066 : 2093332 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18067 : : (TREE_TYPE (value)))
18068 : 39 : value = perform_implicit_conversion_flags
18069 : 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
18070 : : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
18071 : :
18072 : 4626435 : if (value == error_mark_node)
18073 : : value = NULL_TREE;
18074 : :
18075 : 4626414 : if (value != NULL_TREE)
18076 : : {
18077 : 4626414 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18078 : : (TREE_TYPE (value)))
18079 : : {
18080 : 17 : error_at (cp_expr_loc_or_input_loc (value),
18081 : : "enumerator value for %qD must have integral or "
18082 : : "unscoped enumeration type", name);
18083 : 17 : value = NULL_TREE;
18084 : : }
18085 : : else
18086 : : {
18087 : 4626397 : value = cxx_constant_value (value);
18088 : :
18089 : 4626397 : if (TREE_CODE (value) != INTEGER_CST)
18090 : : {
18091 : 50 : error ("enumerator value for %qD is not an integer "
18092 : : "constant", name);
18093 : 50 : value = NULL_TREE;
18094 : : }
18095 : : }
18096 : : }
18097 : : }
18098 : :
18099 : : /* Default based on previous value. */
18100 : 67 : if (value == NULL_TREE)
18101 : : {
18102 : 5767745 : if (TYPE_VALUES (enumtype))
18103 : : {
18104 : 5327040 : tree prev_value;
18105 : :
18106 : : /* C++03 7.2/4: If no initializer is specified for the first
18107 : : enumerator, the type is an unspecified integral
18108 : : type. Otherwise the type is the same as the type of the
18109 : : initializing value of the preceding enumerator unless the
18110 : : incremented value is not representable in that type, in
18111 : : which case the type is an unspecified integral type
18112 : : sufficient to contain the incremented value. */
18113 : 5327040 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
18114 : 5327040 : if (TREE_CODE (prev_value) != INTEGER_CST)
18115 : 2 : value = error_mark_node;
18116 : : else
18117 : : {
18118 : 5327038 : wi::overflow_type overflowed;
18119 : 5327038 : tree type = TREE_TYPE (prev_value);
18120 : 5327038 : signop sgn = TYPE_SIGN (type);
18121 : 5327038 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
18122 : 5327038 : &overflowed);
18123 : 5327038 : if (!overflowed)
18124 : : {
18125 : 5327038 : bool pos = !wi::neg_p (wi, sgn);
18126 : 5327038 : if (!wi::fits_to_tree_p (wi, type))
18127 : : {
18128 : : unsigned int itk;
18129 : 53 : for (itk = itk_int; itk != itk_none; itk++)
18130 : : {
18131 : 53 : type = integer_types[itk];
18132 : 53 : if (type != NULL_TREE
18133 : 53 : && (pos || !TYPE_UNSIGNED (type))
18134 : 106 : && wi::fits_to_tree_p (wi, type))
18135 : : break;
18136 : : }
18137 : 17 : if (type && cxx_dialect < cxx11
18138 : 6 : && itk > itk_unsigned_long)
18139 : 3 : pedwarn (input_location, OPT_Wlong_long,
18140 : : pos ? G_("\
18141 : : incremented enumerator value is too large for %<unsigned long%>") : G_("\
18142 : : incremented enumerator value is too large for %<long%>"));
18143 : : }
18144 : 5327038 : if (type == NULL_TREE)
18145 : 0 : overflowed = wi::OVF_UNKNOWN;
18146 : : else
18147 : 5327038 : value = wide_int_to_tree (type, wi);
18148 : : }
18149 : :
18150 : 5327038 : if (overflowed)
18151 : : {
18152 : 0 : error ("overflow in enumeration values at %qD", name);
18153 : 0 : value = error_mark_node;
18154 : : }
18155 : 5327038 : }
18156 : : }
18157 : : else
18158 : 440705 : value = integer_zero_node;
18159 : : }
18160 : :
18161 : : /* Remove no-op casts from the value. */
18162 : 10394092 : STRIP_TYPE_NOPS (value);
18163 : :
18164 : : /* If the underlying type of the enum is fixed, check whether
18165 : : the enumerator values fits in the underlying type. If it
18166 : : does not fit, the program is ill-formed [C++0x dcl.enum]. */
18167 : 10394092 : if (ENUM_UNDERLYING_TYPE (enumtype)
18168 : : && value
18169 : 10394092 : && TREE_CODE (value) == INTEGER_CST)
18170 : : {
18171 : 2979872 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
18172 : 9 : error ("enumerator value %qE is outside the range of underlying "
18173 : 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
18174 : :
18175 : : /* Convert the value to the appropriate type. */
18176 : 2979872 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
18177 : : }
18178 : : }
18179 : :
18180 : : /* C++ associates enums with global, function, or class declarations. */
18181 : 10752578 : context = current_scope ();
18182 : :
18183 : : /* Build the actual enumeration constant. Note that the enumeration
18184 : : constants have the underlying type of the enum (if it is fixed)
18185 : : or the type of their initializer (if the underlying type of the
18186 : : enum is not fixed):
18187 : :
18188 : : [ C++0x dcl.enum ]
18189 : :
18190 : : If the underlying type is fixed, the type of each enumerator
18191 : : prior to the closing brace is the underlying type; if the
18192 : : initializing value of an enumerator cannot be represented by
18193 : : the underlying type, the program is ill-formed. If the
18194 : : underlying type is not fixed, the type of each enumerator is
18195 : : the type of its initializing value.
18196 : :
18197 : : If the underlying type is not fixed, it will be computed by
18198 : : finish_enum and we will reset the type of this enumerator. Of
18199 : : course, if we're processing a template, there may be no value. */
18200 : 10752578 : type = value ? TREE_TYPE (value) : NULL_TREE;
18201 : :
18202 : 10752578 : decl = build_decl (loc, CONST_DECL, name, type);
18203 : :
18204 : 10752578 : DECL_CONTEXT (decl) = enumtype;
18205 : 10752578 : TREE_CONSTANT (decl) = 1;
18206 : 10752578 : TREE_READONLY (decl) = 1;
18207 : 10752578 : DECL_INITIAL (decl) = value;
18208 : :
18209 : 10752578 : if (attributes)
18210 : 43249 : cplus_decl_attributes (&decl, attributes, 0);
18211 : :
18212 : 10752578 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
18213 : : {
18214 : : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
18215 : : on the TYPE_FIELDS list for `S'. (That's so that you can say
18216 : : things like `S::i' later.) */
18217 : :
18218 : : /* The enumerator may be getting declared outside of its enclosing
18219 : : class, like so:
18220 : :
18221 : : class S { public: enum E : int; }; enum S::E : int { i = 7; };
18222 : :
18223 : : For which case we need to make sure that the access of `S::i'
18224 : : matches the access of `S::E'. */
18225 : 1916184 : auto cas = make_temp_override (current_access_specifier);
18226 : 1916184 : set_current_access_from_decl (TYPE_NAME (enumtype));
18227 : 1916184 : finish_member_declaration (decl);
18228 : 1916184 : }
18229 : : else
18230 : 8836394 : pushdecl (decl);
18231 : :
18232 : : /* Add this enumeration constant to the list for this type. */
18233 : 10752578 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
18234 : :
18235 : 10752578 : return decl;
18236 : : }
18237 : :
18238 : : /* Look for an enumerator with the given NAME within the enumeration
18239 : : type ENUMTYPE. This routine is used primarily for qualified name
18240 : : lookup into an enumerator in C++0x, e.g.,
18241 : :
18242 : : enum class Color { Red, Green, Blue };
18243 : :
18244 : : Color color = Color::Red;
18245 : :
18246 : : Returns the value corresponding to the enumerator, or
18247 : : NULL_TREE if no such enumerator was found. */
18248 : : tree
18249 : 7687505 : lookup_enumerator (tree enumtype, tree name)
18250 : : {
18251 : 7687505 : tree e;
18252 : 7687505 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
18253 : :
18254 : 7687505 : e = purpose_member (name, TYPE_VALUES (enumtype));
18255 : 15374914 : return e? TREE_VALUE (e) : NULL_TREE;
18256 : : }
18257 : :
18258 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
18259 : :
18260 : : tree
18261 : 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
18262 : : vec<string_int_pair> *values)
18263 : : {
18264 : 0 : location_t saved_loc = input_location;
18265 : 0 : input_location = loc;
18266 : :
18267 : 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
18268 : : NULL_TREE, false, NULL);
18269 : 0 : if (!OPAQUE_ENUM_P (enumtype))
18270 : : {
18271 : 0 : auto_diagnostic_group d;
18272 : 0 : error_at (loc, "multiple definition of %q#T", enumtype);
18273 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18274 : : "previous definition here");
18275 : 0 : return enumtype;
18276 : 0 : }
18277 : 0 : SET_OPAQUE_ENUM_P (enumtype, false);
18278 : 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
18279 : :
18280 : 0 : for (const string_int_pair &value : values)
18281 : 0 : build_enumerator (get_identifier (value.first),
18282 : 0 : build_int_cst (integer_type_node, value.second),
18283 : : enumtype, NULL_TREE, loc);
18284 : :
18285 : 0 : finish_enum_value_list (enumtype);
18286 : 0 : finish_enum (enumtype);
18287 : :
18288 : 0 : input_location = saved_loc;
18289 : 0 : return enumtype;
18290 : : }
18291 : :
18292 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
18293 : :
18294 : : tree
18295 : 0 : cxx_simulate_record_decl (location_t loc, const char *name,
18296 : : array_slice<const tree> fields)
18297 : : {
18298 : 0 : iloc_sentinel ils (loc);
18299 : :
18300 : 0 : tree ident = get_identifier (name);
18301 : 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
18302 : 0 : if (type != error_mark_node
18303 : 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
18304 : : {
18305 : 0 : error ("redefinition of %q#T", type);
18306 : 0 : type = error_mark_node;
18307 : : }
18308 : 0 : if (type == error_mark_node)
18309 : 0 : return lhd_simulate_record_decl (loc, name, fields);
18310 : :
18311 : 0 : xref_basetypes (type, NULL_TREE);
18312 : 0 : type = begin_class_definition (type);
18313 : 0 : if (type == error_mark_node)
18314 : 0 : return lhd_simulate_record_decl (loc, name, fields);
18315 : :
18316 : 0 : for (tree field : fields)
18317 : 0 : finish_member_declaration (field);
18318 : :
18319 : 0 : type = finish_struct (type, NULL_TREE);
18320 : :
18321 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
18322 : 0 : set_underlying_type (decl);
18323 : 0 : lang_hooks.decls.pushdecl (decl);
18324 : :
18325 : 0 : return type;
18326 : 0 : }
18327 : :
18328 : : /* We're defining DECL. Make sure that its type is OK. */
18329 : :
18330 : : static void
18331 : 139343339 : check_function_type (tree decl, tree current_function_parms)
18332 : : {
18333 : 139343339 : tree fntype = TREE_TYPE (decl);
18334 : 139343339 : tree return_type = complete_type (TREE_TYPE (fntype));
18335 : :
18336 : : /* In a function definition, arg types must be complete. */
18337 : 139343339 : require_complete_types_for_parms (current_function_parms);
18338 : :
18339 : 139343339 : if (dependent_type_p (return_type)
18340 : 139343339 : || type_uses_auto (return_type))
18341 : 42046188 : return;
18342 : 97297151 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
18343 : : {
18344 : 12 : tree args = TYPE_ARG_TYPES (fntype);
18345 : :
18346 : 12 : error ("return type %q#T is incomplete", return_type);
18347 : :
18348 : : /* Make it return void instead. */
18349 : 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
18350 : 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
18351 : : void_type_node,
18352 : 3 : TREE_CHAIN (args));
18353 : : else
18354 : 9 : fntype = build_function_type (void_type_node, args);
18355 : 12 : fntype = (cp_build_type_attribute_variant
18356 : 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
18357 : 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
18358 : 12 : TREE_TYPE (decl) = fntype;
18359 : : }
18360 : : else
18361 : : {
18362 : 97297139 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
18363 : 97297139 : maybe_warn_parm_abi (TREE_TYPE (fntype),
18364 : 97297139 : DECL_SOURCE_LOCATION (decl));
18365 : : }
18366 : : }
18367 : :
18368 : : /* True iff FN is an implicitly-defined default constructor. */
18369 : :
18370 : : static bool
18371 : 4660852 : implicit_default_ctor_p (tree fn)
18372 : : {
18373 : 4660852 : return (DECL_CONSTRUCTOR_P (fn)
18374 : 4660852 : && !user_provided_p (fn)
18375 : 5307905 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
18376 : : }
18377 : :
18378 : : /* Clobber the contents of *this to let the back end know that the object
18379 : : storage is dead when we enter the constructor or leave the destructor. */
18380 : :
18381 : : static tree
18382 : 5510458 : build_clobber_this (clobber_kind kind)
18383 : : {
18384 : : /* Clobbering an empty base is pointless, and harmful if its one byte
18385 : : TYPE_SIZE overlays real data. */
18386 : 5510458 : if (is_empty_class (current_class_type))
18387 : 0 : return void_node;
18388 : :
18389 : : /* If we have virtual bases, clobber the whole object, but only if we're in
18390 : : charge. If we don't have virtual bases, clobber the as-base type so we
18391 : : don't mess with tail padding. */
18392 : 5510458 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
18393 : :
18394 : 5510458 : tree ctype = current_class_type;
18395 : 5510458 : if (!vbases)
18396 : 5493182 : ctype = CLASSTYPE_AS_BASE (ctype);
18397 : :
18398 : 5510458 : tree clobber = build_clobber (ctype, kind);
18399 : :
18400 : 5510458 : tree thisref = current_class_ref;
18401 : 5510458 : if (ctype != current_class_type)
18402 : : {
18403 : 605175 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
18404 : 605175 : thisref = convert_from_reference (thisref);
18405 : : }
18406 : :
18407 : 5510458 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
18408 : 5510458 : if (vbases)
18409 : 17276 : exprstmt = build_if_in_charge (exprstmt);
18410 : :
18411 : : return exprstmt;
18412 : : }
18413 : :
18414 : : /* Create the FUNCTION_DECL for a function definition.
18415 : : DECLSPECS and DECLARATOR are the parts of the declaration;
18416 : : they describe the function's name and the type it returns,
18417 : : but twisted together in a fashion that parallels the syntax of C.
18418 : :
18419 : : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
18420 : : DECLARATOR is really the DECL for the function we are about to
18421 : : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
18422 : : indicating that the function is an inline defined in-class.
18423 : :
18424 : : This function creates a binding context for the function body
18425 : : as well as setting up the FUNCTION_DECL in current_function_decl.
18426 : :
18427 : : For C++, we must first check whether that datum makes any sense.
18428 : : For example, "class A local_a(1,2);" means that variable local_a
18429 : : is an aggregate of type A, which should have a constructor
18430 : : applied to it with the argument list [1, 2].
18431 : :
18432 : : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
18433 : : or may be a BLOCK if the function has been defined previously
18434 : : in this translation unit. On exit, DECL_INITIAL (decl1) will be
18435 : : error_mark_node if the function has never been defined, or
18436 : : a BLOCK if the function has been defined somewhere. */
18437 : :
18438 : : bool
18439 : 139343375 : start_preparsed_function (tree decl1, tree attrs, int flags)
18440 : : {
18441 : 139343375 : tree ctype = NULL_TREE;
18442 : 139343375 : bool doing_friend = false;
18443 : :
18444 : : /* Sanity check. */
18445 : 139343375 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
18446 : 139343375 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
18447 : :
18448 : 139343375 : tree fntype = TREE_TYPE (decl1);
18449 : 139343375 : if (DECL_CLASS_SCOPE_P (decl1))
18450 : 100673312 : ctype = DECL_CONTEXT (decl1);
18451 : : else
18452 : : {
18453 : 77340126 : ctype = DECL_FRIEND_CONTEXT (decl1);
18454 : :
18455 : 38670062 : if (ctype)
18456 : 139343375 : doing_friend = true;
18457 : : }
18458 : :
18459 : : /* Adjust for #pragma target/optimize if this is an artificial function that
18460 : : (probably) didn't go through grokfndecl. We particularly don't want this
18461 : : for deferred instantiations, which should match their template. */
18462 : 139343375 : if (DECL_ARTIFICIAL (decl1))
18463 : 2896549 : decl_attributes (&decl1, NULL_TREE, 0);
18464 : :
18465 : 139343375 : if (DECL_DECLARED_INLINE_P (decl1)
18466 : 139343375 : && lookup_attribute ("noinline", attrs))
18467 : : {
18468 : 0 : auto_urlify_attributes sentinel;
18469 : 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
18470 : : "inline function %qD given attribute %qs", decl1, "noinline");
18471 : 0 : }
18472 : :
18473 : : /* Handle gnu_inline attribute. */
18474 : 139343375 : if (GNU_INLINE_P (decl1))
18475 : : {
18476 : 1562635 : DECL_EXTERNAL (decl1) = 1;
18477 : 1562635 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
18478 : 1562635 : DECL_INTERFACE_KNOWN (decl1) = 1;
18479 : 1562635 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
18480 : : }
18481 : :
18482 : 139343375 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
18483 : : /* This is a constructor, we must ensure that any default args
18484 : : introduced by this definition are propagated to the clones
18485 : : now. The clones are used directly in overload resolution. */
18486 : 18352719 : adjust_clone_args (decl1);
18487 : :
18488 : : /* Sometimes we don't notice that a function is a static member, and
18489 : : build a METHOD_TYPE for it. Fix that up now. */
18490 : 139343375 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
18491 : : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
18492 : :
18493 : : /* Set up current_class_type, and enter the scope of the class, if
18494 : : appropriate. */
18495 : 139343375 : if (ctype)
18496 : 103331761 : push_nested_class (ctype);
18497 : :
18498 : : /* Now that we have entered the scope of the class, we must restore
18499 : : the bindings for any template parameters surrounding DECL1, if it
18500 : : is an inline member template. (Order is important; consider the
18501 : : case where a template parameter has the same name as a field of
18502 : : the class.) It is not until after this point that
18503 : : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
18504 : 139343375 : if (flags & SF_INCLASS_INLINE)
18505 : 68048523 : maybe_begin_member_template_processing (decl1);
18506 : :
18507 : : /* Effective C++ rule 15. */
18508 : 139343375 : if (warn_ecpp
18509 : 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
18510 : 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
18511 : 139343405 : && VOID_TYPE_P (TREE_TYPE (fntype)))
18512 : 0 : warning (OPT_Weffc__,
18513 : : "%<operator=%> should return a reference to %<*this%>");
18514 : :
18515 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
18516 : : error_mark_node is replaced below (in poplevel) with the BLOCK. */
18517 : 139343375 : if (!DECL_INITIAL (decl1))
18518 : 29045449 : DECL_INITIAL (decl1) = error_mark_node;
18519 : :
18520 : : /* This function exists in static storage.
18521 : : (This does not mean `static' in the C sense!) */
18522 : 139343375 : TREE_STATIC (decl1) = 1;
18523 : :
18524 : : /* We must call push_template_decl after current_class_type is set
18525 : : up. (If we are processing inline definitions after exiting a
18526 : : class scope, current_class_type will be NULL_TREE until set above
18527 : : by push_nested_class.) */
18528 : 139343375 : if (processing_template_decl)
18529 : : {
18530 : 84037386 : tree newdecl1 = push_template_decl (decl1, doing_friend);
18531 : 84037386 : if (newdecl1 == error_mark_node)
18532 : : {
18533 : 36 : if (ctype)
18534 : 9 : pop_nested_class ();
18535 : 36 : return false;
18536 : : }
18537 : 84037350 : decl1 = newdecl1;
18538 : : }
18539 : :
18540 : : /* Make sure the parameter and return types are reasonable. When
18541 : : you declare a function, these types can be incomplete, but they
18542 : : must be complete when you define the function. */
18543 : 139343339 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
18544 : :
18545 : : /* Build the return declaration for the function. */
18546 : 139343339 : tree restype = TREE_TYPE (fntype);
18547 : :
18548 : 139343339 : if (DECL_RESULT (decl1) == NULL_TREE)
18549 : : {
18550 : : /* In a template instantiation, copy the return type location. When
18551 : : parsing, the location will be set in grokdeclarator. */
18552 : 55230119 : location_t loc = input_location;
18553 : 55230119 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
18554 : : {
18555 : 26061172 : tree tmpl = template_for_substitution (decl1);
18556 : 26061172 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
18557 : 25003865 : loc = DECL_SOURCE_LOCATION (res);
18558 : : }
18559 : :
18560 : 55230119 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
18561 : 55230119 : DECL_ARTIFICIAL (resdecl) = 1;
18562 : 55230119 : DECL_IGNORED_P (resdecl) = 1;
18563 : 55230119 : DECL_RESULT (decl1) = resdecl;
18564 : :
18565 : 55230119 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
18566 : : }
18567 : :
18568 : : /* Record the decl so that the function name is defined.
18569 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
18570 : : use the old decl. */
18571 : 139343339 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
18572 : : {
18573 : : /* A specialization is not used to guide overload resolution. */
18574 : 19756613 : if (!DECL_FUNCTION_MEMBER_P (decl1)
18575 : 19743625 : && !(DECL_USE_TEMPLATE (decl1) &&
18576 : 588 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
18577 : : {
18578 : 9479452 : tree olddecl = pushdecl (decl1);
18579 : :
18580 : 9479452 : if (olddecl == error_mark_node)
18581 : : /* If something went wrong when registering the declaration,
18582 : : use DECL1; we have to have a FUNCTION_DECL to use when
18583 : : parsing the body of the function. */
18584 : : ;
18585 : : else
18586 : : {
18587 : : /* Otherwise, OLDDECL is either a previous declaration
18588 : : of the same function or DECL1 itself. */
18589 : :
18590 : 9479239 : if (warn_missing_declarations
18591 : 21 : && olddecl == decl1
18592 : 15 : && !DECL_MAIN_P (decl1)
18593 : 15 : && TREE_PUBLIC (decl1)
18594 : 9479251 : && !DECL_DECLARED_INLINE_P (decl1))
18595 : : {
18596 : 9 : tree context;
18597 : :
18598 : : /* Check whether DECL1 is in an anonymous
18599 : : namespace. */
18600 : 9 : for (context = DECL_CONTEXT (decl1);
18601 : 18 : context;
18602 : 9 : context = DECL_CONTEXT (context))
18603 : : {
18604 : 12 : if (TREE_CODE (context) == NAMESPACE_DECL
18605 : 12 : && DECL_NAME (context) == NULL_TREE)
18606 : : break;
18607 : : }
18608 : :
18609 : 9 : if (context == NULL)
18610 : 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
18611 : 6 : OPT_Wmissing_declarations,
18612 : : "no previous declaration for %qD", decl1);
18613 : : }
18614 : :
18615 : 9479239 : decl1 = olddecl;
18616 : : }
18617 : : }
18618 : : else
18619 : : {
18620 : : /* We need to set the DECL_CONTEXT. */
18621 : 784133 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
18622 : 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
18623 : : }
18624 : 10263585 : fntype = TREE_TYPE (decl1);
18625 : 10263585 : restype = TREE_TYPE (fntype);
18626 : :
18627 : : /* If #pragma weak applies, mark the decl appropriately now.
18628 : : The pragma only applies to global functions. Because
18629 : : determining whether or not the #pragma applies involves
18630 : : computing the mangled name for the declaration, we cannot
18631 : : apply the pragma until after we have merged this declaration
18632 : : with any previous declarations; if the original declaration
18633 : : has a linkage specification, that specification applies to
18634 : : the definition as well, and may affect the mangled name. */
18635 : 10263585 : if (DECL_FILE_SCOPE_P (decl1))
18636 : 2328688 : maybe_apply_pragma_weak (decl1);
18637 : : }
18638 : :
18639 : : /* We are now in the scope of the function being defined. */
18640 : 139343339 : current_function_decl = decl1;
18641 : :
18642 : : /* Save the parm names or decls from this function's declarator
18643 : : where store_parm_decls will find them. */
18644 : 139343339 : tree current_function_parms = DECL_ARGUMENTS (decl1);
18645 : :
18646 : : /* Let the user know we're compiling this function. */
18647 : 139343339 : announce_function (decl1);
18648 : :
18649 : 139343339 : gcc_assert (DECL_INITIAL (decl1));
18650 : :
18651 : : /* This function may already have been parsed, in which case just
18652 : : return; our caller will skip over the body without parsing. */
18653 : 139343339 : if (DECL_INITIAL (decl1) != error_mark_node)
18654 : : return true;
18655 : :
18656 : : /* Initialize RTL machinery. We cannot do this until
18657 : : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
18658 : : even when processing a template; this is how we get
18659 : : CFUN set up, and our per-function variables initialized.
18660 : : FIXME factor out the non-RTL stuff. */
18661 : 139343339 : cp_binding_level *bl = current_binding_level;
18662 : 139343339 : allocate_struct_function (decl1, processing_template_decl);
18663 : :
18664 : : /* Initialize the language data structures. Whenever we start
18665 : : a new function, we destroy temporaries in the usual way. */
18666 : 139343339 : cfun->language = ggc_cleared_alloc<language_function> ();
18667 : 139343339 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
18668 : 139343339 : current_binding_level = bl;
18669 : :
18670 : : /* If we are (erroneously) defining a function that we have already
18671 : : defined before, wipe out what we knew before. */
18672 : 139343339 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
18673 : 139343339 : FNDECL_USED_AUTO (decl1) = false;
18674 : 139343339 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
18675 : :
18676 : 139343339 : if (!processing_template_decl && type_uses_auto (restype))
18677 : : {
18678 : 781766 : FNDECL_USED_AUTO (decl1) = true;
18679 : 781766 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
18680 : : }
18681 : :
18682 : : /* Start the statement-tree, start the tree now. */
18683 : 139343339 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
18684 : :
18685 : 139343339 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
18686 : : {
18687 : : /* We know that this was set up by `grokclassfn'. We do not
18688 : : wait until `store_parm_decls', since evil parse errors may
18689 : : never get us to that point. Here we keep the consistency
18690 : : between `current_class_type' and `current_class_ptr'. */
18691 : 90006597 : tree t = DECL_ARGUMENTS (decl1);
18692 : :
18693 : 90006597 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
18694 : 90006597 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
18695 : :
18696 : 90006597 : cp_function_chain->x_current_class_ref
18697 : 90006597 : = cp_build_fold_indirect_ref (t);
18698 : : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
18699 : 90006597 : cp_function_chain->x_current_class_ptr = t;
18700 : :
18701 : : /* Constructors and destructors need to know whether they're "in
18702 : : charge" of initializing virtual base classes. */
18703 : 90006597 : t = DECL_CHAIN (t);
18704 : 90006597 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
18705 : : {
18706 : 19366 : current_in_charge_parm = t;
18707 : 19366 : t = DECL_CHAIN (t);
18708 : : }
18709 : 90006597 : if (DECL_HAS_VTT_PARM_P (decl1))
18710 : : {
18711 : 38745 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
18712 : 38745 : current_vtt_parm = t;
18713 : : }
18714 : : }
18715 : :
18716 : 139343339 : bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
18717 : : /* Implicitly-defined methods (like the
18718 : : destructor for a class in which no destructor
18719 : : is explicitly declared) must not be defined
18720 : : until their definition is needed. So, we
18721 : : ignore interface specifications for
18722 : : compiler-generated functions. */
18723 : 139343339 : && !DECL_ARTIFICIAL (decl1));
18724 : 139343339 : struct c_fileinfo *finfo
18725 : 139343339 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
18726 : :
18727 : 139343339 : if (processing_template_decl)
18728 : : /* Don't mess with interface flags. */;
18729 : 55305989 : else if (DECL_INTERFACE_KNOWN (decl1))
18730 : : {
18731 : 17207219 : tree ctx = decl_function_context (decl1);
18732 : :
18733 : 17207219 : if (DECL_NOT_REALLY_EXTERN (decl1))
18734 : 13763911 : DECL_EXTERNAL (decl1) = 0;
18735 : :
18736 : 17207219 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
18737 : : /* This is a function in a local class in an extern inline
18738 : : or template function. */
18739 : 2738981 : comdat_linkage (decl1);
18740 : : }
18741 : : /* If this function belongs to an interface, it is public.
18742 : : If it belongs to someone else's interface, it is also external.
18743 : : This only affects inlines and template instantiations. */
18744 : 38098770 : else if (!finfo->interface_unknown && honor_interface)
18745 : : {
18746 : 112 : if (DECL_DECLARED_INLINE_P (decl1)
18747 : 112 : || DECL_TEMPLATE_INSTANTIATION (decl1))
18748 : : {
18749 : 88 : DECL_EXTERNAL (decl1)
18750 : 176 : = (finfo->interface_only
18751 : 88 : || (DECL_DECLARED_INLINE_P (decl1)
18752 : 42 : && ! flag_implement_inlines
18753 : 0 : && !DECL_VINDEX (decl1)));
18754 : :
18755 : : /* For WIN32 we also want to put these in linkonce sections. */
18756 : 88 : maybe_make_one_only (decl1);
18757 : : }
18758 : : else
18759 : 24 : DECL_EXTERNAL (decl1) = 0;
18760 : 112 : DECL_INTERFACE_KNOWN (decl1) = 1;
18761 : : /* If this function is in an interface implemented in this file,
18762 : : make sure that the back end knows to emit this function
18763 : : here. */
18764 : 112 : if (!DECL_EXTERNAL (decl1))
18765 : 66 : mark_needed (decl1);
18766 : : }
18767 : 38098658 : else if (finfo->interface_unknown && finfo->interface_only
18768 : 0 : && honor_interface)
18769 : : {
18770 : : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
18771 : : interface, we will have both finfo->interface_unknown and
18772 : : finfo->interface_only set. In that case, we don't want to
18773 : : use the normal heuristics because someone will supply a
18774 : : #pragma implementation elsewhere, and deducing it here would
18775 : : produce a conflict. */
18776 : 0 : comdat_linkage (decl1);
18777 : 0 : DECL_EXTERNAL (decl1) = 0;
18778 : 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
18779 : 0 : DECL_DEFER_OUTPUT (decl1) = 1;
18780 : : }
18781 : : else
18782 : : {
18783 : : /* This is a definition, not a reference.
18784 : : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
18785 : 38098658 : if (!GNU_INLINE_P (decl1))
18786 : 38098655 : DECL_EXTERNAL (decl1) = 0;
18787 : :
18788 : 38098658 : if ((DECL_DECLARED_INLINE_P (decl1)
18789 : 2042044 : || DECL_TEMPLATE_INSTANTIATION (decl1))
18790 : 39909478 : && ! DECL_INTERFACE_KNOWN (decl1))
18791 : 37867434 : DECL_DEFER_OUTPUT (decl1) = 1;
18792 : : else
18793 : 231224 : DECL_INTERFACE_KNOWN (decl1) = 1;
18794 : : }
18795 : :
18796 : : /* Determine the ELF visibility attribute for the function. We must not
18797 : : do this before calling "pushdecl", as we must allow "duplicate_decls"
18798 : : to merge any attributes appropriately. We also need to wait until
18799 : : linkage is set. */
18800 : 139343339 : if (!DECL_CLONED_FUNCTION_P (decl1))
18801 : 125722498 : determine_visibility (decl1);
18802 : :
18803 : 139343339 : if (!processing_template_decl)
18804 : 55305989 : maybe_instantiate_noexcept (decl1);
18805 : :
18806 : 139343339 : begin_scope (sk_function_parms, decl1);
18807 : :
18808 : 139343339 : ++function_depth;
18809 : :
18810 : 139343339 : start_fname_decls ();
18811 : :
18812 : 139343339 : store_parm_decls (current_function_parms);
18813 : :
18814 : 139343339 : start_function_contracts (decl1);
18815 : :
18816 : 139343339 : if (!processing_template_decl
18817 : 55305989 : && (flag_lifetime_dse > 1)
18818 : 110594446 : && DECL_CONSTRUCTOR_P (decl1)
18819 : 15644704 : && !DECL_CLONED_FUNCTION_P (decl1)
18820 : : /* Clobbering an empty base is harmful if it overlays real data. */
18821 : 5212176 : && !is_empty_class (current_class_type)
18822 : : /* We can't clobber safely for an implicitly-defined default constructor
18823 : : because part of the initialization might happen before we enter the
18824 : : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
18825 : 144003594 : && !implicit_default_ctor_p (decl1))
18826 : 4239508 : finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN));
18827 : :
18828 : 139343339 : if (!processing_template_decl
18829 : 110611978 : && DECL_CONSTRUCTOR_P (decl1)
18830 : 15647230 : && sanitize_flags_p (SANITIZE_VPTR)
18831 : 1791 : && !DECL_CLONED_FUNCTION_P (decl1)
18832 : 139343936 : && !implicit_default_ctor_p (decl1))
18833 : 441 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
18834 : :
18835 : 139343339 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
18836 : 139342771 : start_lambda_scope (decl1);
18837 : :
18838 : : return true;
18839 : : }
18840 : :
18841 : :
18842 : : /* Like start_preparsed_function, except that instead of a
18843 : : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
18844 : :
18845 : : Returns true on success. If the DECLARATOR is not suitable
18846 : : for a function, we return false, which tells the parser to
18847 : : skip the entire function. */
18848 : :
18849 : : bool
18850 : 38338051 : start_function (cp_decl_specifier_seq *declspecs,
18851 : : const cp_declarator *declarator,
18852 : : tree attrs)
18853 : : {
18854 : 38338051 : tree decl1;
18855 : :
18856 : 38338051 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
18857 : 38338051 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
18858 : 38338051 : if (decl1 == error_mark_node)
18859 : : return false;
18860 : :
18861 : 38337762 : if (DECL_MAIN_P (decl1))
18862 : : /* main must return int. grokfndecl should have corrected it
18863 : : (and issued a diagnostic) if the user got it wrong. */
18864 : 32839 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
18865 : : integer_type_node));
18866 : :
18867 : 38337762 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
18868 : : }
18869 : :
18870 : : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
18871 : : FN. */
18872 : :
18873 : : bool
18874 : 266542220 : use_eh_spec_block (tree fn)
18875 : : {
18876 : 264637200 : return (flag_exceptions && flag_enforce_eh_specs
18877 : 264637200 : && !processing_template_decl
18878 : : /* We insert the EH_SPEC_BLOCK only in the original
18879 : : function; then, it is copied automatically to the
18880 : : clones. */
18881 : 96153312 : && !DECL_CLONED_FUNCTION_P (fn)
18882 : : /* Implicitly-generated constructors and destructors have
18883 : : exception specifications. However, those specifications
18884 : : are the union of the possible exceptions specified by the
18885 : : constructors/destructors for bases and members, so no
18886 : : unallowed exception will ever reach this function. By
18887 : : not creating the EH_SPEC_BLOCK we save a little memory,
18888 : : and we avoid spurious warnings about unreachable
18889 : : code. */
18890 : 82572319 : && !DECL_DEFAULTED_FN (fn)
18891 : 346825553 : && !type_throw_all_p (TREE_TYPE (fn)));
18892 : : }
18893 : :
18894 : : /* Helper function to push ARGS into the current lexical scope. DECL
18895 : : is the function declaration. NONPARMS is used to handle enum
18896 : : constants. */
18897 : :
18898 : : void
18899 : 136147863 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
18900 : : {
18901 : : /* If we're doing semantic analysis, then we'll call pushdecl
18902 : : for each of these. We must do them in reverse order so that
18903 : : they end in the correct forward order. */
18904 : 136147863 : args = nreverse (args);
18905 : :
18906 : 136147863 : tree next;
18907 : 416272272 : for (tree parm = args; parm; parm = next)
18908 : : {
18909 : 280124409 : next = DECL_CHAIN (parm);
18910 : 280124409 : if (TREE_CODE (parm) == PARM_DECL)
18911 : 280124409 : pushdecl (parm);
18912 : 0 : else if (nonparms)
18913 : : {
18914 : : /* If we find an enum constant or a type tag, put it aside for
18915 : : the moment. */
18916 : 0 : TREE_CHAIN (parm) = NULL_TREE;
18917 : 0 : *nonparms = chainon (*nonparms, parm);
18918 : : }
18919 : : }
18920 : :
18921 : : /* Get the decls in their original chain order and record in the
18922 : : function. This is all and only the PARM_DECLs that were
18923 : : pushed into scope by the loop above. */
18924 : 136147863 : DECL_ARGUMENTS (decl) = get_local_decls ();
18925 : 136147863 : }
18926 : :
18927 : : /* Store the parameter declarations into the current function declaration.
18928 : : This is called after parsing the parameter declarations, before
18929 : : digesting the body of the function.
18930 : :
18931 : : Also install to binding contour return value identifier, if any. */
18932 : :
18933 : : static void
18934 : 139343339 : store_parm_decls (tree current_function_parms)
18935 : : {
18936 : 139343339 : tree fndecl = current_function_decl;
18937 : :
18938 : : /* This is a chain of any other decls that came in among the parm
18939 : : declarations. If a parm is declared with enum {foo, bar} x;
18940 : : then CONST_DECLs for foo and bar are put here. */
18941 : 139343339 : tree nonparms = NULL_TREE;
18942 : :
18943 : 139343339 : if (current_function_parms)
18944 : : {
18945 : : /* This case is when the function was defined with an ANSI prototype.
18946 : : The parms already have decls, so we need not do anything here
18947 : : except record them as in effect
18948 : : and complain if any redundant old-style parm decls were written. */
18949 : :
18950 : 133620055 : tree specparms = current_function_parms;
18951 : :
18952 : : /* Must clear this because it might contain TYPE_DECLs declared
18953 : : at class level. */
18954 : 133620055 : current_binding_level->names = NULL;
18955 : :
18956 : 133620055 : do_push_parm_decls (fndecl, specparms, &nonparms);
18957 : : }
18958 : : else
18959 : 5723284 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
18960 : :
18961 : : /* Now store the final chain of decls for the arguments
18962 : : as the decl-chain of the current lexical scope.
18963 : : Put the enumerators in as well, at the front so that
18964 : : DECL_ARGUMENTS is not modified. */
18965 : 139343339 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
18966 : :
18967 : 139343339 : if (use_eh_spec_block (current_function_decl))
18968 : 18214436 : current_eh_spec_block = begin_eh_spec_block ();
18969 : 139343339 : }
18970 : :
18971 : :
18972 : : /* Mark CDTOR's implicit THIS argument for returning, if required by
18973 : : the ABI.. Return the decl for THIS, if it is to be returned, and
18974 : : NULL otherwise. */
18975 : :
18976 : : tree
18977 : 110568549 : maybe_prepare_return_this (tree cdtor)
18978 : : {
18979 : 110568549 : if (targetm.cxx.cdtor_returns_this ())
18980 : 0 : if (tree val = DECL_ARGUMENTS (cdtor))
18981 : : {
18982 : 0 : suppress_warning (val, OPT_Wuse_after_free);
18983 : 0 : return val;
18984 : : }
18985 : :
18986 : : return NULL_TREE;
18987 : : }
18988 : :
18989 : : /* Set the return value of the [cd]tor if the ABI wants that. */
18990 : :
18991 : : void
18992 : 6933693 : maybe_return_this ()
18993 : : {
18994 : 6933693 : if (tree val = maybe_prepare_return_this (current_function_decl))
18995 : : {
18996 : : /* Return the address of the object. */
18997 : 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
18998 : 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
18999 : 0 : DECL_RESULT (current_function_decl), val);
19000 : 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
19001 : 0 : add_stmt (exprstmt);
19002 : : }
19003 : 6933693 : }
19004 : :
19005 : : /* Do all the processing for the beginning of a destructor; set up the
19006 : : vtable pointers and cleanups for bases and members. */
19007 : :
19008 : : static void
19009 : 1463378 : begin_destructor_body (void)
19010 : : {
19011 : 1463378 : tree compound_stmt;
19012 : :
19013 : : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
19014 : : issued an error message. We still want to try to process the
19015 : : body of the function, but initialize_vtbl_ptrs will crash if
19016 : : TYPE_BINFO is NULL. */
19017 : 1463378 : if (COMPLETE_TYPE_P (current_class_type))
19018 : : {
19019 : 1463378 : compound_stmt = begin_compound_stmt (0);
19020 : : /* Make all virtual function table pointers in non-virtual base
19021 : : classes point to CURRENT_CLASS_TYPE's virtual function
19022 : : tables. */
19023 : 1463378 : initialize_vtbl_ptrs (current_class_ptr);
19024 : 1463378 : finish_compound_stmt (compound_stmt);
19025 : :
19026 : 1463378 : if (flag_lifetime_dse
19027 : : /* Clobbering an empty base is harmful if it overlays real data. */
19028 : 1463378 : && !is_empty_class (current_class_type))
19029 : : {
19030 : 1271013 : if (sanitize_flags_p (SANITIZE_VPTR)
19031 : 216 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
19032 : 1271076 : && TYPE_CONTAINS_VPTR_P (current_class_type))
19033 : : {
19034 : 63 : tree binfo = TYPE_BINFO (current_class_type);
19035 : 63 : tree ref
19036 : 63 : = cp_build_fold_indirect_ref (current_class_ptr);
19037 : :
19038 : 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
19039 : 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
19040 : 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
19041 : : NOP_EXPR, vtbl,
19042 : : tf_warning_or_error);
19043 : : /* If the vptr is shared with some virtual nearly empty base,
19044 : : don't clear it if not in charge, the dtor of the virtual
19045 : : nearly empty base will do that later. */
19046 : 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
19047 : : {
19048 : : tree c = current_class_type;
19049 : 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
19050 : : {
19051 : 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
19052 : : {
19053 : 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
19054 : : tf_warning_or_error);
19055 : 36 : stmt = build_if_in_charge (stmt);
19056 : 36 : break;
19057 : : }
19058 : 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
19059 : : }
19060 : : }
19061 : 63 : finish_decl_cleanup (NULL_TREE, stmt);
19062 : : }
19063 : : else
19064 : 1270950 : finish_decl_cleanup (NULL_TREE,
19065 : : build_clobber_this (CLOBBER_OBJECT_END));
19066 : : }
19067 : :
19068 : : /* And insert cleanups for our bases and members so that they
19069 : : will be properly destroyed if we throw. */
19070 : 1463378 : push_base_cleanups ();
19071 : : }
19072 : 1463378 : }
19073 : :
19074 : : /* Do the necessary processing for the beginning of a function body, which
19075 : : in this case includes member-initializers, but not the catch clauses of
19076 : : a function-try-block. Currently, this means opening a binding level
19077 : : for the member-initializers (in a ctor), member cleanups (in a dtor),
19078 : : and capture proxies (in a lambda operator()). */
19079 : :
19080 : : tree
19081 : 111090662 : begin_function_body (void)
19082 : : {
19083 : 227061730 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
19084 : : return NULL_TREE;
19085 : :
19086 : 22214919 : if (processing_template_decl)
19087 : : /* Do nothing now. */;
19088 : : else
19089 : : /* Always keep the BLOCK node associated with the outermost pair of
19090 : : curly braces of a function. These are needed for correct
19091 : : operation of dwarfout.c. */
19092 : 7116409 : keep_next_level (true);
19093 : :
19094 : 22214919 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
19095 : 22214919 : current_binding_level->artificial = 1;
19096 : :
19097 : 22214919 : if (processing_template_decl)
19098 : : /* Do nothing now. */;
19099 : 14232818 : else if (DECL_DESTRUCTOR_P (current_function_decl))
19100 : 1463378 : begin_destructor_body ();
19101 : :
19102 : : return stmt;
19103 : : }
19104 : :
19105 : : /* Do the processing for the end of a function body. Currently, this means
19106 : : closing out the cleanups for fully-constructed bases and members, and in
19107 : : the case of the destructor, deleting the object if desired. Again, this
19108 : : is only meaningful for [cd]tors, since they are the only functions where
19109 : : there is a significant distinction between the main body and any
19110 : : function catch clauses. Handling, say, main() return semantics here
19111 : : would be wrong, as flowing off the end of a function catch clause for
19112 : : main() would also need to return 0. */
19113 : :
19114 : : void
19115 : 111090638 : finish_function_body (tree compstmt)
19116 : : {
19117 : 111090638 : if (compstmt == NULL_TREE)
19118 : : return;
19119 : :
19120 : : /* Close the block. */
19121 : 22214919 : finish_compound_stmt (compstmt);
19122 : :
19123 : 22214919 : if (processing_template_decl)
19124 : : /* Do nothing now. */;
19125 : 7116409 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
19126 : 7116409 : || DECL_DESTRUCTOR_P (current_function_decl))
19127 : 6676396 : maybe_return_this ();
19128 : : }
19129 : :
19130 : : /* Given a function, returns the BLOCK corresponding to the outermost level
19131 : : of curly braces, skipping the artificial block created for constructor
19132 : : initializers. */
19133 : :
19134 : : tree
19135 : 301256 : outer_curly_brace_block (tree fndecl)
19136 : : {
19137 : 301256 : tree block = DECL_INITIAL (fndecl);
19138 : 301256 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
19139 : : return block;
19140 : 1347 : block = BLOCK_SUBBLOCKS (block);
19141 : 1347 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
19142 : : return block;
19143 : 0 : block = BLOCK_SUBBLOCKS (block);
19144 : 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
19145 : : return block;
19146 : : }
19147 : :
19148 : : /* If FNDECL is a class's key method, add the class to the list of
19149 : : keyed classes that should be emitted. */
19150 : :
19151 : : static void
19152 : 139347164 : record_key_method_defined (tree fndecl)
19153 : : {
19154 : 188687707 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
19155 : 90033015 : && DECL_VIRTUAL_P (fndecl)
19156 : 141883949 : && !processing_template_decl)
19157 : : {
19158 : 1230431 : tree fnclass = DECL_CONTEXT (fndecl);
19159 : 1230431 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
19160 : : {
19161 : 1608 : tree classdecl = TYPE_NAME (fnclass);
19162 : : /* Classes attached to a named module are already handled. */
19163 : 1608 : if (!DECL_LANG_SPECIFIC (classdecl)
19164 : 1656 : || !DECL_MODULE_ATTACH_P (classdecl))
19165 : 1563 : vec_safe_push (keyed_classes, fnclass);
19166 : : }
19167 : : }
19168 : 139347164 : }
19169 : :
19170 : : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
19171 : : of "return *this;" immediately before its location, using FNDECL's
19172 : : first statement (if any) to give the indentation, if appropriate. */
19173 : :
19174 : : static void
19175 : 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
19176 : : {
19177 : 18 : location_t indent = UNKNOWN_LOCATION;
19178 : 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
19179 : 18 : if (stmts)
19180 : 9 : indent = EXPR_LOCATION (stmts);
19181 : 18 : richloc->add_fixit_insert_formatted ("return *this;",
19182 : : richloc->get_loc (),
19183 : : indent);
19184 : 18 : }
19185 : :
19186 : : /* Finish up a function declaration and compile that function
19187 : : all the way to assembler language output. The free the storage
19188 : : for the function definition. INLINE_P is TRUE if we just
19189 : : finished processing the body of an in-class inline function
19190 : : definition. (This processing will have taken place after the
19191 : : class definition is complete.) */
19192 : :
19193 : : tree
19194 : 139343303 : finish_function (bool inline_p)
19195 : : {
19196 : 139343303 : tree fndecl = current_function_decl;
19197 : 139343303 : tree fntype, ctype = NULL_TREE;
19198 : 139343303 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
19199 : :
19200 : : /* When we get some parse errors, we can end up without a
19201 : : current_function_decl, so cope. */
19202 : 139343303 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
19203 : 0 : return error_mark_node;
19204 : :
19205 : 139343303 : bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
19206 : 139343303 : && !processing_template_decl);
19207 : :
19208 : 139343303 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19209 : 139342735 : finish_lambda_scope ();
19210 : :
19211 : 139343303 : if (c_dialect_objc ())
19212 : 0 : objc_finish_function ();
19213 : :
19214 : 139343303 : record_key_method_defined (fndecl);
19215 : :
19216 : 139343303 : fntype = TREE_TYPE (fndecl);
19217 : :
19218 : : /* TREE_READONLY (fndecl) = 1;
19219 : : This caused &foo to be of type ptr-to-const-function
19220 : : which then got a warning when stored in a ptr-to-function variable. */
19221 : :
19222 : 139343303 : gcc_assert (building_stmt_list_p ());
19223 : : /* The current function is being defined, so its DECL_INITIAL should
19224 : : be set, and unless there's a multiple definition, it should be
19225 : : error_mark_node. */
19226 : 139343303 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
19227 : :
19228 : 139343303 : cp_coroutine_transform *coroutine = nullptr;
19229 : 139343303 : if (flag_coroutines
19230 : 44929926 : && !processing_template_decl
19231 : 19627664 : && DECL_COROUTINE_P (fndecl)
19232 : 139344946 : && !DECL_RAMP_FN (fndecl))
19233 : : {
19234 : 1643 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
19235 : : && !DECL_DEFAULTED_FN (fndecl));
19236 : 1643 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
19237 : 1643 : if (coroutine && coroutine->cp_valid_coroutine ())
19238 : 1643 : coroutine->apply_transforms ();
19239 : :
19240 : : /* We should handle coroutine IFNs in middle end lowering. */
19241 : 1643 : cfun->coroutine_component = true;
19242 : :
19243 : : /* Do not try to process the ramp's EH unless outlining succeeded. */
19244 : 1643 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
19245 : 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19246 : : (TREE_TYPE (fndecl)),
19247 : 360 : current_eh_spec_block);
19248 : :
19249 : : /* If outlining succeeded, then add contracts handling if needed. */
19250 : 1643 : if (coroutine->cp_valid_coroutine () && do_contracts)
19251 : 6 : maybe_apply_function_contracts (fndecl);
19252 : : }
19253 : : else
19254 : : /* For a cloned function, we've already got all the code we need;
19255 : : there's no need to add any extra bits. */
19256 : 139341660 : if (!DECL_CLONED_FUNCTION_P (fndecl))
19257 : : {
19258 : : /* Make it so that `main' always returns 0 by default. */
19259 : 125720819 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
19260 : 125753643 : && !TREE_THIS_VOLATILE (current_function_decl))
19261 : 32824 : finish_return_stmt (integer_zero_node);
19262 : :
19263 : 125720819 : if (use_eh_spec_block (current_function_decl))
19264 : 18214073 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19265 : : (TREE_TYPE (current_function_decl)),
19266 : 18214073 : current_eh_spec_block);
19267 : :
19268 : 125720819 : if (do_contracts)
19269 : 420 : maybe_apply_function_contracts (current_function_decl);
19270 : :
19271 : : }
19272 : :
19273 : : /* If we're saving up tree structure, tie off the function now. */
19274 : 139343303 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
19275 : :
19276 : 139343303 : finish_fname_decls ();
19277 : :
19278 : : /* This must come after expand_function_end because cleanups might
19279 : : have declarations (from inline functions) that need to go into
19280 : : this function's blocks. */
19281 : :
19282 : : /* If the current binding level isn't the outermost binding level
19283 : : for this function, either there is a bug, or we have experienced
19284 : : syntax errors and the statement tree is malformed. */
19285 : 139343303 : if (current_binding_level->kind != sk_function_parms)
19286 : : {
19287 : : /* Make sure we have already experienced errors. */
19288 : 6 : gcc_assert (errorcount);
19289 : :
19290 : : /* Throw away the broken statement tree and extra binding
19291 : : levels. */
19292 : 6 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
19293 : :
19294 : 12 : while (current_binding_level->kind != sk_function_parms)
19295 : : {
19296 : 6 : if (current_binding_level->kind == sk_class)
19297 : 0 : pop_nested_class ();
19298 : : else
19299 : 6 : poplevel (0, 0, 0);
19300 : : }
19301 : : }
19302 : 139343303 : poplevel (1, 0, 1);
19303 : :
19304 : : /* Statements should always be full-expressions at the outermost set
19305 : : of curly braces for a function. */
19306 : 139343303 : gcc_assert (stmts_are_full_exprs_p ());
19307 : :
19308 : : /* If there are no return statements in a function with auto return type,
19309 : : the return type is void. But if the declared type is something like
19310 : : auto*, this is an error. */
19311 : 55305953 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
19312 : 140125069 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19313 : : {
19314 : 220031 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19315 : 220016 : && !current_function_returns_value
19316 : 439861 : && !current_function_returns_null)
19317 : : {
19318 : : /* We haven't applied return type deduction because we haven't
19319 : : seen any return statements. Do that now. */
19320 : 219830 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19321 : 219830 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
19322 : : void_node, node, tf_warning_or_error,
19323 : : adc_return_type);
19324 : :
19325 : 219830 : apply_deduced_return_type (fndecl, void_type_node);
19326 : 219830 : fntype = TREE_TYPE (fndecl);
19327 : : }
19328 : 201 : else if (!current_function_returns_value
19329 : 6 : && !current_function_returns_null)
19330 : : {
19331 : 6 : auto_diagnostic_group d;
19332 : 12 : error ("no return statements in function returning %qT",
19333 : 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19334 : 6 : inform (input_location, "only plain %<auto%> return type can be "
19335 : : "deduced to %<void%>");
19336 : 6 : }
19337 : : }
19338 : :
19339 : 139343303 : if (FNDECL_USED_AUTO (fndecl)
19340 : 139343303 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19341 : 781565 : if (location_t fcloc = failed_completion_location (fndecl))
19342 : : {
19343 : 6 : auto_diagnostic_group adg;
19344 : 6 : if (warning (OPT_Wsfinae_incomplete_,
19345 : : "defining %qD, which previously failed to be deduced "
19346 : : "in a SFINAE context", fndecl)
19347 : 6 : && warn_sfinae_incomplete == 1)
19348 : 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
19349 : : "-Wsfinae-incomplete=2");
19350 : 6 : }
19351 : :
19352 : : /* Remember that we were in class scope. */
19353 : 139343303 : if (current_class_name)
19354 : 103331749 : ctype = current_class_type;
19355 : :
19356 : 139343303 : if (DECL_DELETED_FN (fndecl))
19357 : : {
19358 : 87 : DECL_INITIAL (fndecl) = error_mark_node;
19359 : 87 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
19360 : 87 : goto cleanup;
19361 : : }
19362 : :
19363 : 139343216 : if (flag_openmp)
19364 : 285092 : if (tree attr = lookup_attribute ("omp declare variant base",
19365 : 285092 : DECL_ATTRIBUTES (fndecl)))
19366 : 157 : omp_declare_variant_finalize (fndecl, attr);
19367 : :
19368 : : /* Complain if there's just no return statement. */
19369 : 139343216 : if ((warn_return_type
19370 : 41388 : || (cxx_dialect >= cxx14
19371 : 33339 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
19372 : 139311742 : && !VOID_TYPE_P (TREE_TYPE (fntype))
19373 : 85639679 : && !dependent_type_p (TREE_TYPE (fntype))
19374 : 44380781 : && !current_function_returns_value && !current_function_returns_null
19375 : : /* Don't complain if we abort or throw. */
19376 : 63381 : && !current_function_returns_abnormally
19377 : : /* Don't complain if there's an infinite loop. */
19378 : 924 : && !current_function_infinite_loop
19379 : : /* Don't complain if we are declared noreturn. */
19380 : 856 : && !TREE_THIS_VOLATILE (fndecl)
19381 : 850 : && !DECL_NAME (DECL_RESULT (fndecl))
19382 : 850 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
19383 : : /* Structor return values (if any) are set by the compiler. */
19384 : 1230 : && !DECL_CONSTRUCTOR_P (fndecl)
19385 : 615 : && !DECL_DESTRUCTOR_P (fndecl)
19386 : 139343831 : && targetm.warn_func_return (fndecl))
19387 : : {
19388 : 609 : gcc_rich_location richloc (input_location);
19389 : : /* Potentially add a "return *this;" fix-it hint for
19390 : : assignment operators. */
19391 : 609 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
19392 : : {
19393 : 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
19394 : 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
19395 : 32 : && current_class_ref
19396 : 26 : && same_type_ignoring_top_level_qualifiers_p
19397 : 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
19398 : 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
19399 : 18 : add_return_star_this_fixit (&richloc, fndecl);
19400 : : }
19401 : 609 : if (cxx_dialect >= cxx14
19402 : 609 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
19403 : 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
19404 : : "returning non-void");
19405 : 607 : else if (warning_at (&richloc, OPT_Wreturn_type,
19406 : : "no return statement in function returning "
19407 : : "non-void"))
19408 : 162 : suppress_warning (fndecl, OPT_Wreturn_type);
19409 : 609 : }
19410 : :
19411 : : /* Lambda closure members are implicitly constexpr if possible. */
19412 : 139343216 : if (cxx_dialect >= cxx17
19413 : 238860802 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
19414 : 1179961 : DECL_DECLARED_CONSTEXPR_P (fndecl)
19415 : 1179961 : = ((processing_template_decl
19416 : 522225 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
19417 : 2358711 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
19418 : :
19419 : : /* Save constexpr function body before it gets munged by
19420 : : the NRV transformation. */
19421 : 139343216 : maybe_save_constexpr_fundef (fndecl);
19422 : :
19423 : : /* Invoke the pre-genericize plugin before we start munging things. */
19424 : 139343216 : if (!processing_template_decl)
19425 : 55305866 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
19426 : :
19427 : : /* Perform delayed folding before NRV transformation. */
19428 : 139343216 : if (!processing_template_decl
19429 : 110611732 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19430 : 194398525 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19431 : 55054871 : cp_fold_function (fndecl);
19432 : :
19433 : : /* Set up the named return value optimization, if we can. Candidate
19434 : : variables are selected in check_return_expr. */
19435 : 139343216 : if (tree r = current_function_return_value)
19436 : : {
19437 : 34490868 : if (r != error_mark_node)
19438 : 176266 : finalize_nrv (fndecl, r);
19439 : 34490868 : current_function_return_value = NULL_TREE;
19440 : : }
19441 : :
19442 : : /* Must mark the RESULT_DECL as being in this function. */
19443 : 139343216 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
19444 : :
19445 : : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
19446 : : to the FUNCTION_DECL node itself. */
19447 : 139343216 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
19448 : :
19449 : : /* Store the end of the function, so that we get good line number
19450 : : info for the epilogue. */
19451 : 139343216 : cfun->function_end_locus = input_location;
19452 : :
19453 : : /* Complain about parameters that are only set, but never otherwise used. */
19454 : 139343216 : if (warn_unused_but_set_parameter
19455 : 898236 : && !processing_template_decl
19456 : 459603 : && errorcount == unused_but_set_errorcount
19457 : 139802816 : && !DECL_CLONED_FUNCTION_P (fndecl))
19458 : : {
19459 : 359516 : tree decl;
19460 : :
19461 : 359516 : for (decl = DECL_ARGUMENTS (fndecl);
19462 : 959814 : decl;
19463 : 600298 : decl = DECL_CHAIN (decl))
19464 : 600298 : if (TREE_USED (decl)
19465 : 495600 : && TREE_CODE (decl) == PARM_DECL
19466 : 495600 : && !DECL_READ_P (decl)
19467 : 6 : && DECL_NAME (decl)
19468 : 6 : && !DECL_ARTIFICIAL (decl)
19469 : 6 : && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
19470 : 6 : && !DECL_IN_SYSTEM_HEADER (decl)
19471 : 6 : && TREE_TYPE (decl) != error_mark_node
19472 : 6 : && !TYPE_REF_P (TREE_TYPE (decl))
19473 : 600304 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
19474 : 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
19475 : 6 : warning_at (DECL_SOURCE_LOCATION (decl),
19476 : 6 : OPT_Wunused_but_set_parameter,
19477 : : "parameter %qD set but not used", decl);
19478 : 359516 : unused_but_set_errorcount = errorcount;
19479 : : }
19480 : :
19481 : : /* Complain about locally defined typedefs that are not used in this
19482 : : function. */
19483 : 139343216 : maybe_warn_unused_local_typedefs ();
19484 : :
19485 : : /* Possibly warn about unused parameters. */
19486 : 139343216 : if (warn_unused_parameter
19487 : 628974 : && !processing_template_decl
19488 : 139645352 : && !DECL_CLONED_FUNCTION_P (fndecl))
19489 : 231806 : do_warn_unused_parameter (fndecl);
19490 : :
19491 : : /* Genericize before inlining. */
19492 : 139343216 : if (!processing_template_decl
19493 : 110611732 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19494 : 194398375 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19495 : 55054721 : cp_genericize (fndecl);
19496 : :
19497 : : /* If this function can't throw any exceptions, remember that. */
19498 : 139343216 : if (!processing_template_decl
19499 : 55305866 : && !cp_function_chain->can_throw
19500 : 41694672 : && !flag_non_call_exceptions
19501 : 41690148 : && !decl_replaceable_p (fndecl,
19502 : 41690148 : opt_for_fn (fndecl, flag_semantic_interposition))
19503 : 180785314 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
19504 : 41439819 : TREE_NOTHROW (fndecl) = 1;
19505 : :
19506 : 139343303 : cleanup:
19507 : :
19508 : : /* We're leaving the context of this function, so zap cfun. It's still in
19509 : : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
19510 : 139343303 : set_cfun (NULL);
19511 : 139343303 : current_function_decl = NULL;
19512 : :
19513 : : /* If this is an in-class inline definition, we may have to pop the
19514 : : bindings for the template parameters that we added in
19515 : : maybe_begin_member_template_processing when start_function was
19516 : : called. */
19517 : 139343303 : if (inline_p)
19518 : 68048923 : maybe_end_member_template_processing ();
19519 : :
19520 : : /* Leave the scope of the class. */
19521 : 139343303 : if (ctype)
19522 : 103331749 : pop_nested_class ();
19523 : :
19524 : 139343303 : --function_depth;
19525 : :
19526 : : /* Clean up. */
19527 : 139343303 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
19528 : :
19529 : : /* Build outlined functions for coroutines and contracts. */
19530 : :
19531 : 139343303 : if (coroutine)
19532 : : {
19533 : : /* Emit the resumer and destroyer functions now, providing that we have
19534 : : not encountered some fatal error. */
19535 : 1643 : if (coroutine->cp_valid_coroutine ())
19536 : : {
19537 : 1559 : coroutine->finish_transforms ();
19538 : 1559 : resumer = coroutine->get_resumer ();
19539 : 1559 : destroyer = coroutine->get_destroyer ();
19540 : 1559 : expand_or_defer_fn (resumer);
19541 : 1559 : expand_or_defer_fn (destroyer);
19542 : : }
19543 : 1643 : delete coroutine;
19544 : : }
19545 : :
19546 : : /* If we have used outlined contracts checking functions, build and emit
19547 : : them here. */
19548 : 139343303 : finish_function_contracts (fndecl);
19549 : :
19550 : 139343303 : return fndecl;
19551 : : }
19552 : :
19553 : : /* Create the FUNCTION_DECL for a function definition.
19554 : : DECLSPECS and DECLARATOR are the parts of the declaration;
19555 : : they describe the return type and the name of the function,
19556 : : but twisted together in a fashion that parallels the syntax of C.
19557 : :
19558 : : This function creates a binding context for the function body
19559 : : as well as setting up the FUNCTION_DECL in current_function_decl.
19560 : :
19561 : : Returns a FUNCTION_DECL on success.
19562 : :
19563 : : If the DECLARATOR is not suitable for a function (it defines a datum
19564 : : instead), we return 0, which tells yyparse to report a parse error.
19565 : :
19566 : : May return void_type_node indicating that this method is actually
19567 : : a friend. See grokfield for more details.
19568 : :
19569 : : Came here with a `.pushlevel' .
19570 : :
19571 : : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
19572 : : CHANGES TO CODE IN `grokfield'. */
19573 : :
19574 : : tree
19575 : 67756853 : grokmethod (cp_decl_specifier_seq *declspecs,
19576 : : const cp_declarator *declarator, tree attrlist)
19577 : : {
19578 : 67756853 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
19579 : 67756853 : &attrlist);
19580 : :
19581 : 67756853 : if (fndecl == error_mark_node)
19582 : : return error_mark_node;
19583 : :
19584 : 67756750 : if (attrlist)
19585 : 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
19586 : :
19587 : : /* Pass friends other than inline friend functions back. */
19588 : 67756750 : if (fndecl == void_type_node)
19589 : : return fndecl;
19590 : :
19591 : 67756750 : if (DECL_IN_AGGR_P (fndecl))
19592 : : {
19593 : 0 : if (DECL_CLASS_SCOPE_P (fndecl))
19594 : 0 : error ("%qD is already defined in class %qT", fndecl,
19595 : 0 : DECL_CONTEXT (fndecl));
19596 : 0 : return error_mark_node;
19597 : : }
19598 : :
19599 : 67756750 : check_template_shadow (fndecl);
19600 : :
19601 : : /* p1779 ABI-Isolation makes inline not a default for in-class
19602 : : definitions attached to a named module. If the user explicitly
19603 : : made it inline, grokdeclarator will already have done the right
19604 : : things. */
19605 : 67756750 : if ((!named_module_attach_p ()
19606 : 388 : || flag_module_implicit_inline
19607 : : /* Lambda's operator function remains inline. */
19608 : 761 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
19609 : : /* If the user explicitly asked for this to be inline, we don't
19610 : : need to do more, but more importantly we want to warn if we
19611 : : can't inline it. */
19612 : 67756411 : && !DECL_DECLARED_INLINE_P (fndecl))
19613 : : {
19614 : 49702072 : if (TREE_PUBLIC (fndecl))
19615 : 48717276 : DECL_COMDAT (fndecl) = 1;
19616 : 49702072 : DECL_DECLARED_INLINE_P (fndecl) = 1;
19617 : : /* It's ok if we can't inline this. */
19618 : 49702072 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
19619 : : }
19620 : :
19621 : : /* We process method specializations in finish_struct_1. */
19622 : 67756750 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
19623 : : {
19624 : : /* Avoid calling decl_spec_seq... until we have to. */
19625 : 55915585 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
19626 : 55915585 : fndecl = push_template_decl (fndecl, friendp);
19627 : 55915585 : if (fndecl == error_mark_node)
19628 : : return fndecl;
19629 : : }
19630 : :
19631 : 67756743 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
19632 : : {
19633 : 0 : fndecl = copy_node (fndecl);
19634 : 0 : TREE_CHAIN (fndecl) = NULL_TREE;
19635 : : }
19636 : :
19637 : 67756743 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
19638 : :
19639 : 67756743 : DECL_IN_AGGR_P (fndecl) = 1;
19640 : 67756743 : return fndecl;
19641 : : }
19642 : :
19643 : :
19644 : : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
19645 : : we can lay it out later, when and if its type becomes complete.
19646 : :
19647 : : Also handle constexpr variables where the initializer involves
19648 : : an unlowered PTRMEM_CST because the class isn't complete yet. */
19649 : :
19650 : : void
19651 : 85204938 : maybe_register_incomplete_var (tree var)
19652 : : {
19653 : 85204938 : gcc_assert (VAR_P (var));
19654 : :
19655 : : /* Keep track of variables with incomplete types. */
19656 : 37782403 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
19657 : 122985764 : && DECL_EXTERNAL (var))
19658 : : {
19659 : 5462252 : tree inner_type = TREE_TYPE (var);
19660 : :
19661 : 5616676 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
19662 : 154424 : inner_type = TREE_TYPE (inner_type);
19663 : 5462252 : inner_type = TYPE_MAIN_VARIANT (inner_type);
19664 : :
19665 : 5504178 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
19666 : : /* RTTI TD entries are created while defining the type_info. */
19667 : 5462252 : || (TYPE_LANG_SPECIFIC (inner_type)
19668 : 2211648 : && TYPE_BEING_DEFINED (inner_type)))
19669 : : {
19670 : 41926 : incomplete_var iv = {var, inner_type};
19671 : 41926 : vec_safe_push (incomplete_vars, iv);
19672 : : }
19673 : 10537386 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
19674 : 3373629 : && DECL_CLASS_SCOPE_P (var)
19675 : 1061153 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
19676 : 1061153 : && decl_constant_var_p (var)
19677 : 6111047 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
19678 : : {
19679 : : /* When the outermost open class is complete we can resolve any
19680 : : pointers-to-members. */
19681 : 13 : tree context = outermost_open_class ();
19682 : 13 : incomplete_var iv = {var, context};
19683 : 13 : vec_safe_push (incomplete_vars, iv);
19684 : : }
19685 : : }
19686 : 85204938 : }
19687 : :
19688 : : /* Called when a class type (given by TYPE) is defined. If there are
19689 : : any existing VAR_DECLs whose type has been completed by this
19690 : : declaration, update them now. */
19691 : :
19692 : : void
19693 : 53697083 : complete_vars (tree type)
19694 : : {
19695 : 53697083 : unsigned ix;
19696 : 53697083 : incomplete_var *iv;
19697 : :
19698 : 54117535 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
19699 : : {
19700 : 420452 : if (same_type_p (type, iv->incomplete_type))
19701 : : {
19702 : 40822 : tree var = iv->decl;
19703 : 40822 : tree type = TREE_TYPE (var);
19704 : :
19705 : 40822 : if (type != error_mark_node
19706 : 40822 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
19707 : 40819 : == iv->incomplete_type))
19708 : : {
19709 : : /* Complete the type of the variable. */
19710 : 40806 : complete_type (type);
19711 : 40806 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
19712 : 40806 : if (COMPLETE_TYPE_P (type))
19713 : 40791 : layout_var_decl (var);
19714 : : }
19715 : :
19716 : : /* Remove this entry from the list. */
19717 : 40822 : incomplete_vars->unordered_remove (ix);
19718 : : }
19719 : : else
19720 : 379630 : ix++;
19721 : : }
19722 : 53697083 : }
19723 : :
19724 : : /* If DECL is of a type which needs a cleanup, build and return an
19725 : : expression to perform that cleanup here. Return NULL_TREE if no
19726 : : cleanup need be done. DECL can also be a _REF when called from
19727 : : split_nonconstant_init_1. */
19728 : :
19729 : : tree
19730 : 62594486 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
19731 : : {
19732 : 62594486 : tree type;
19733 : 62594486 : tree attr;
19734 : 62594486 : tree cleanup;
19735 : :
19736 : : /* Assume no cleanup is required. */
19737 : 62594486 : cleanup = NULL_TREE;
19738 : :
19739 : 62594486 : if (!decl || error_operand_p (decl))
19740 : : return cleanup;
19741 : :
19742 : : /* Handle "__attribute__((cleanup))". We run the cleanup function
19743 : : before the destructor since the destructor is what actually
19744 : : terminates the lifetime of the object. */
19745 : 62594486 : if (DECL_P (decl))
19746 : 62238332 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
19747 : : else
19748 : : attr = NULL_TREE;
19749 : 62238332 : if (attr)
19750 : : {
19751 : 157 : tree id;
19752 : 157 : tree fn;
19753 : 157 : tree arg;
19754 : :
19755 : : /* Get the name specified by the user for the cleanup function. */
19756 : 157 : id = TREE_VALUE (TREE_VALUE (attr));
19757 : : /* Look up the name to find the cleanup function to call. It is
19758 : : important to use lookup_name here because that is what is
19759 : : used in c-common.cc:handle_cleanup_attribute when performing
19760 : : initial checks on the attribute. Note that those checks
19761 : : include ensuring that the function found is not an overloaded
19762 : : function, or an object with an overloaded call operator,
19763 : : etc.; we can rely on the fact that the function found is an
19764 : : ordinary FUNCTION_DECL. */
19765 : 157 : fn = lookup_name (id);
19766 : 157 : arg = build_address (decl);
19767 : 157 : if (!mark_used (decl, complain) && !(complain & tf_error))
19768 : 0 : return error_mark_node;
19769 : 157 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
19770 : 157 : if (cleanup == error_mark_node)
19771 : : return error_mark_node;
19772 : : }
19773 : : /* Handle ordinary C++ destructors. */
19774 : 62594480 : type = TREE_TYPE (decl);
19775 : 62594480 : if (type_build_dtor_call (type))
19776 : : {
19777 : 5126229 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
19778 : 5126229 : tree addr;
19779 : 5126229 : tree call;
19780 : :
19781 : 5126229 : if (TREE_CODE (type) == ARRAY_TYPE)
19782 : : addr = decl;
19783 : : else
19784 : 5124533 : addr = build_address (decl);
19785 : :
19786 : 5126229 : call = build_delete (input_location, TREE_TYPE (addr), addr,
19787 : : sfk_complete_destructor, flags, 0, complain);
19788 : 5126229 : if (call == error_mark_node)
19789 : : cleanup = error_mark_node;
19790 : 5126190 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
19791 : : /* Discard the call. */;
19792 : 3808323 : else if (decl_maybe_constant_destruction (decl, type)
19793 : 3808454 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
19794 : 131 : cxx_constant_dtor (call, decl);
19795 : 3808192 : else if (cleanup)
19796 : 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
19797 : : else
19798 : : cleanup = call;
19799 : : }
19800 : :
19801 : : /* build_delete sets the location of the destructor call to the
19802 : : current location, even though the destructor is going to be
19803 : : called later, at the end of the current scope. This can lead to
19804 : : a "jumpy" behavior for users of debuggers when they step around
19805 : : the end of the block. So let's unset the location of the
19806 : : destructor call instead. */
19807 : 62594480 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
19808 : 62594480 : if (cleanup && CONVERT_EXPR_P (cleanup))
19809 : 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
19810 : :
19811 : 0 : if (cleanup
19812 : 3808379 : && DECL_P (decl)
19813 : 3807908 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
19814 : : /* Treat objects with destructors as used; the destructor may do
19815 : : something substantive. */
19816 : 3807902 : && !mark_used (decl, complain) && !(complain & tf_error))
19817 : 0 : return error_mark_node;
19818 : :
19819 : 3808379 : if (cleanup && cfun && !processing_template_decl
19820 : 65971998 : && !expr_noexcept_p (cleanup, tf_none))
19821 : 4313 : cp_function_chain->throwing_cleanup = true;
19822 : :
19823 : : return cleanup;
19824 : : }
19825 : :
19826 : :
19827 : : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
19828 : : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
19829 : : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
19830 : :
19831 : : tree
19832 : 2666802 : static_fn_type (tree memfntype)
19833 : : {
19834 : 2666802 : tree fntype;
19835 : 2666802 : tree args;
19836 : :
19837 : 2666802 : if (TYPE_PTRMEMFUNC_P (memfntype))
19838 : 21132 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
19839 : 2666802 : if (INDIRECT_TYPE_P (memfntype)
19840 : 2344358 : || TREE_CODE (memfntype) == FUNCTION_DECL)
19841 : 2491371 : memfntype = TREE_TYPE (memfntype);
19842 : 2666802 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
19843 : : return memfntype;
19844 : 542669 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
19845 : 542669 : args = TYPE_ARG_TYPES (memfntype);
19846 : 542669 : fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
19847 : 542669 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
19848 : 542669 : fntype = (cp_build_type_attribute_variant
19849 : 542669 : (fntype, TYPE_ATTRIBUTES (memfntype)));
19850 : 542669 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
19851 : 542669 : return fntype;
19852 : : }
19853 : :
19854 : : /* DECL was originally constructed as a non-static member function,
19855 : : but turned out to be static. Update it accordingly. */
19856 : :
19857 : : void
19858 : 158581 : revert_static_member_fn (tree decl)
19859 : : {
19860 : 158581 : tree stype = static_fn_type (decl);
19861 : 158581 : cp_cv_quals quals = type_memfn_quals (stype);
19862 : 158581 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
19863 : :
19864 : 158581 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
19865 : 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
19866 : :
19867 : 158581 : TREE_TYPE (decl) = stype;
19868 : :
19869 : 158581 : if (DECL_ARGUMENTS (decl))
19870 : 158581 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
19871 : 158581 : DECL_STATIC_FUNCTION_P (decl) = 1;
19872 : 158581 : }
19873 : :
19874 : : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
19875 : : one of the language-independent trees. */
19876 : :
19877 : : enum cp_tree_node_structure_enum
19878 : 31332683956 : cp_tree_node_structure (union lang_tree_node * t)
19879 : : {
19880 : 31332683956 : switch (TREE_CODE (&t->generic))
19881 : : {
19882 : : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
19883 : 135361884 : case BASELINK: return TS_CP_BASELINK;
19884 : 31261985 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
19885 : 10166570 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
19886 : 3225150 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
19887 : 1879291618 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
19888 : 2039985 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
19889 : 159120 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
19890 : 601975565 : case OVERLOAD: return TS_CP_OVERLOAD;
19891 : 770 : case PTRMEM_CST: return TS_CP_PTRMEM;
19892 : 13477377 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
19893 : 581242358 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
19894 : 1329588335 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
19895 : 577703902 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
19896 : 13970464 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
19897 : 3 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
19898 : 2423387 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
19899 : 26150795483 : default: return TS_CP_GENERIC;
19900 : : }
19901 : : }
19902 : :
19903 : : bool
19904 : 18994 : cp_missing_noreturn_ok_p (tree decl)
19905 : : {
19906 : : /* A missing noreturn is ok for the `main' function. */
19907 : 18994 : return DECL_MAIN_P (decl);
19908 : : }
19909 : :
19910 : : /* Return the decl used to identify the COMDAT group into which DECL should
19911 : : be placed. */
19912 : :
19913 : : tree
19914 : 80376425 : cxx_comdat_group (tree decl)
19915 : : {
19916 : : /* Virtual tables, construction virtual tables, and virtual table
19917 : : tables all go in a single COMDAT group, named after the primary
19918 : : virtual table. */
19919 : 80376425 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
19920 : 429005 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
19921 : : /* For all other DECLs, the COMDAT group is the mangled name of the
19922 : : declaration itself. */
19923 : : else
19924 : : {
19925 : 79947420 : while (DECL_THUNK_P (decl))
19926 : : {
19927 : : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
19928 : : into the same section as the target function. In that case
19929 : : we must return target's name. */
19930 : 0 : tree target = THUNK_TARGET (decl);
19931 : 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
19932 : 0 : && DECL_SECTION_NAME (target) != NULL
19933 : 0 : && DECL_ONE_ONLY (target))
19934 : : decl = target;
19935 : : else
19936 : : break;
19937 : : }
19938 : : /* If a ctor/dtor has already set the comdat group by
19939 : : maybe_clone_body, don't override it. */
19940 : 79947420 : if (SUPPORTS_ONE_ONLY
19941 : 79947420 : && TREE_CODE (decl) == FUNCTION_DECL
19942 : 79947420 : && DECL_CLONED_FUNCTION_P (decl))
19943 : 14865623 : if (tree comdat = DECL_COMDAT_GROUP (decl))
19944 : : return comdat;
19945 : : }
19946 : :
19947 : : return decl;
19948 : : }
19949 : :
19950 : : /* Returns the return type for FN as written by the user, which may include
19951 : : a placeholder for a deduced return type. */
19952 : :
19953 : : tree
19954 : 466199388 : fndecl_declared_return_type (tree fn)
19955 : : {
19956 : 466199388 : fn = STRIP_TEMPLATE (fn);
19957 : 466199388 : if (FNDECL_USED_AUTO (fn))
19958 : 4386 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
19959 : :
19960 : 466195002 : return TREE_TYPE (TREE_TYPE (fn));
19961 : : }
19962 : :
19963 : : /* Returns true iff DECL is a variable or function declared with an auto type
19964 : : that has not yet been deduced to a real type. */
19965 : :
19966 : : bool
19967 : 739272707 : undeduced_auto_decl (tree decl)
19968 : : {
19969 : 739272707 : if (cxx_dialect < cxx11)
19970 : : return false;
19971 : 737267313 : STRIP_ANY_LOCATION_WRAPPER (decl);
19972 : 311044960 : return ((VAR_OR_FUNCTION_DECL_P (decl)
19973 : 72709693 : || TREE_CODE (decl) == TEMPLATE_DECL)
19974 : 975732856 : && type_uses_auto (TREE_TYPE (decl)));
19975 : : }
19976 : :
19977 : : /* Complain if DECL has an undeduced return type. */
19978 : :
19979 : : bool
19980 : 235145851 : require_deduced_type (tree decl, tsubst_flags_t complain)
19981 : : {
19982 : 235145851 : if (undeduced_auto_decl (decl))
19983 : : {
19984 : 2927 : if (TREE_CODE (decl) == FUNCTION_DECL
19985 : 2900 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
19986 : 5244 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
19987 : : {
19988 : : /* Set the return type of __builtin_source_location. */
19989 : 2317 : tree type = get_source_location_impl_type ();
19990 : 2317 : if (type == error_mark_node)
19991 : : {
19992 : 33 : inform (input_location, "using %qs", "__builtin_source_location");
19993 : 33 : return false;
19994 : : }
19995 : 2284 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
19996 : 2284 : type = build_pointer_type (type);
19997 : 2284 : apply_deduced_return_type (decl, type);
19998 : 2284 : return true;
19999 : : }
20000 : :
20001 : 610 : if (warning_suppressed_p (decl) && seen_error ())
20002 : : /* We probably already complained about deduction failure. */;
20003 : 477 : else if (complain & tf_error)
20004 : 72 : error ("use of %qD before deduction of %<auto%>", decl);
20005 : 610 : note_failed_type_completion (decl, complain);
20006 : 610 : return false;
20007 : : }
20008 : : return true;
20009 : : }
20010 : :
20011 : : /* Create a representation of the explicit-specifier with
20012 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
20013 : :
20014 : : tree
20015 : 6172598 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
20016 : : {
20017 : 6172598 : if (check_for_bare_parameter_packs (expr))
20018 : 3 : return error_mark_node;
20019 : :
20020 : 6172595 : if (instantiation_dependent_expression_p (expr))
20021 : : /* Wait for instantiation, tsubst_function_decl will handle it. */
20022 : : return expr;
20023 : :
20024 : 5587332 : expr = build_converted_constant_bool_expr (expr, complain);
20025 : 5587332 : expr = instantiate_non_dependent_expr (expr, complain);
20026 : 5587332 : expr = cxx_constant_value (expr, complain);
20027 : 5587332 : return expr;
20028 : : }
20029 : :
20030 : : #include "gt-cp-decl.h"
|