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 : : enum diagnostics::kind, 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 : 225503404 : current_tmpl_spec_kind (int n_class_scopes)
278 : : {
279 : 225503404 : int n_template_parm_scopes = 0;
280 : 225503404 : int seen_specialization_p = 0;
281 : 225503404 : int innermost_specialization_p = 0;
282 : 225503404 : cp_binding_level *b;
283 : :
284 : : /* Scan through the template parameter scopes. */
285 : 225503404 : for (b = current_binding_level;
286 : 282290482 : b->kind == sk_template_parms;
287 : 56787078 : 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 : 56787078 : if (b->explicit_spec_p)
299 : : {
300 : 949135 : if (n_template_parm_scopes == 0)
301 : : innermost_specialization_p = 1;
302 : : else
303 : 1149 : seen_specialization_p = 1;
304 : : }
305 : 55837943 : else if (seen_specialization_p == 1)
306 : : return tsk_invalid_member_spec;
307 : :
308 : 56787078 : ++n_template_parm_scopes;
309 : : }
310 : :
311 : : /* Handle explicit instantiations. */
312 : 225503404 : if (processing_explicit_instantiation)
313 : : {
314 : 2023165 : 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 : 2023165 : return tsk_expl_inst;
324 : : }
325 : :
326 : 223480239 : 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 : 223480239 : 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 : 48512652 : 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 : 96580249 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
360 : : }
361 : :
362 : : /* Exit the current scope. */
363 : :
364 : : void
365 : 86712517 : finish_scope (void)
366 : : {
367 : 86712517 : poplevel (0, 0, 0);
368 : 86712517 : }
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 : 20267 : check_label_used (tree label)
375 : : {
376 : 20267 : if (!processing_template_decl)
377 : : {
378 : 19994 : 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 : 19985 : warn_for_unused_label (label);
390 : : }
391 : 20267 : }
392 : :
393 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
394 : :
395 : : static int
396 : 228599 : sort_labels (const void *a, const void *b)
397 : : {
398 : 228599 : tree label1 = *(tree const *) a;
399 : 228599 : tree label2 = *(tree const *) b;
400 : :
401 : : /* DECL_UIDs can never be equal. */
402 : 228599 : 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 : 142129113 : pop_labels (tree block)
411 : : {
412 : 142129113 : if (!named_labels)
413 : 142124558 : 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 : 4555 : auto_vec<tree, 32> labels (named_labels->elements ());
421 : 4555 : hash_table<named_label_hash>::iterator end (named_labels->end ());
422 : 24801 : for (hash_table<named_label_hash>::iterator iter
423 : 49602 : (named_labels->begin ()); iter != end; ++iter)
424 : : {
425 : 20246 : named_label_entry *ent = *iter;
426 : :
427 : 20246 : gcc_checking_assert (!ent->outer);
428 : 20246 : if (ent->label_decl)
429 : 20060 : labels.quick_push (ent->label_decl);
430 : 20246 : ggc_free (ent);
431 : : }
432 : 4555 : named_labels = NULL;
433 : 4555 : labels.qsort (sort_labels);
434 : :
435 : 24615 : while (labels.length ())
436 : : {
437 : 20060 : tree label = labels.pop ();
438 : :
439 : 20060 : DECL_CHAIN (label) = BLOCK_VARS (block);
440 : 20060 : BLOCK_VARS (block) = label;
441 : :
442 : 20060 : check_label_used (label);
443 : : }
444 : 4555 : }
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 : 1960509 : level_for_constexpr_if (cp_binding_level *b)
505 : : {
506 : 1956962 : return (b->kind == sk_cond && b->this_entity
507 : 262 : && TREE_CODE (b->this_entity) == IF_STMT
508 : 1960771 : && 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 : 1960485 : level_for_consteval_if (cp_binding_level *b)
515 : : {
516 : 1956938 : return (b->kind == sk_cond && b->this_entity
517 : 238 : && TREE_CODE (b->this_entity) == IF_STMT
518 : 1960723 : && 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 : 149080 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
539 : : {
540 : 149080 : named_label_entry *ent = *slot;
541 : 149080 : cp_binding_level *obl = bl->level_chain;
542 : :
543 : 149080 : if (ent->binding_level == bl)
544 : : {
545 : 9691 : tree decl;
546 : :
547 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
548 : : TREE_LISTs representing OVERLOADs, so be careful. */
549 : 13751 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
550 : 4133 : ? DECL_CHAIN (decl)
551 : 73 : : TREE_CHAIN (decl)))
552 : 4060 : if (decl_jump_unsafe (decl))
553 : 1775 : vec_safe_push (ent->bad_decls, decl);
554 : :
555 : 9691 : ent->binding_level = obl;
556 : 9691 : ent->names_in_scope = obl->names;
557 : 9691 : 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 : 3740 : case sk_block:
575 : 3740 : if (level_for_constexpr_if (obl))
576 : 15 : ent->in_constexpr_if = true;
577 : 3725 : else if (level_for_consteval_if (obl))
578 : 30 : ent->in_consteval_if = true;
579 : : break;
580 : : default:
581 : : break;
582 : : }
583 : : }
584 : 139389 : else if (ent->uses)
585 : : {
586 : : struct named_label_use_entry *use;
587 : :
588 : 17393 : for (use = ent->uses; use ; use = use->next)
589 : 10912 : if (use->binding_level == bl)
590 : : {
591 : 4870 : 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 : 4870 : use->binding_level = obl;
601 : 4870 : use->names_in_scope = obl->names;
602 : 4870 : if (bl->kind == sk_omp)
603 : 12 : use->in_omp_scope = true;
604 : : }
605 : : }
606 : :
607 : 149080 : 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 : 573260337 : poplevel (int keep, int reverse, int functionbody)
631 : : {
632 : 573260337 : tree link;
633 : : /* The chain of decls was accumulated in reverse order.
634 : : Put it into forward order, just for cleanliness. */
635 : 573260337 : tree decls;
636 : 573260337 : tree subblocks;
637 : 573260337 : tree block;
638 : 573260337 : tree decl;
639 : 573260337 : scope_kind kind;
640 : :
641 : 573260337 : auto_cond_timevar tv (TV_NAME_LOOKUP);
642 : 573260649 : restart:
643 : :
644 : 573260649 : block = NULL_TREE;
645 : :
646 : 573260649 : gcc_assert (current_binding_level->kind != sk_class
647 : : && current_binding_level->kind != sk_namespace);
648 : :
649 : 573260649 : if (current_binding_level->kind == sk_cleanup)
650 : : functionbody = 0;
651 : 573260649 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
652 : :
653 : 573260649 : 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 : 573260649 : gcc_assert (keep == 0 || keep == 1);
659 : :
660 : 573260649 : if (current_binding_level->keep)
661 : 7300174 : keep = 1;
662 : :
663 : : /* Any uses of undefined labels, and any defined labels, now operate
664 : : under constraints of next binding contour. */
665 : 573260649 : if (cfun && !functionbody && named_labels)
666 : 41891 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
667 : 190971 : (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 : 573260649 : decls = current_binding_level->names;
674 : 573260649 : if (reverse)
675 : : {
676 : 307991046 : decls = nreverse (decls);
677 : 307991046 : 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 : 573260649 : block = NULL_TREE;
684 : : /* Avoid function body block if possible. */
685 : 608342057 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
686 : : keep = 0;
687 : 538179502 : else if (keep == 1 || functionbody)
688 : 177084220 : block = make_node (BLOCK);
689 : 177084220 : if (block != NULL_TREE)
690 : : {
691 : 177084220 : BLOCK_VARS (block) = decls;
692 : 177084220 : BLOCK_SUBBLOCKS (block) = subblocks;
693 : : }
694 : :
695 : : /* In each subblock, record that this is its superior. */
696 : 573260649 : if (keep >= 0)
697 : 643296187 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
698 : 70035538 : BLOCK_SUPERCONTEXT (link) = block;
699 : :
700 : : /* Before we remove the declarations first check for unused variables. */
701 : 573260649 : if ((warn_unused_variable || warn_unused_but_set_variable)
702 : 6313141 : && current_binding_level->kind != sk_template_parms
703 : 5338925 : && !processing_template_decl)
704 : 6246105 : 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 : 3192266 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
710 : 3192266 : decl = strip_using_decl (udecl);
711 : :
712 : 3192266 : tree type = TREE_TYPE (decl);
713 : 3192266 : if (VAR_P (decl)
714 : 371159 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
715 : 3385 : && !DECL_IN_SYSTEM_HEADER (udecl)
716 : : /* For structured bindings, consider only real variables, not
717 : : subobjects. */
718 : 3373 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
719 : 3119 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
720 : : /* Don't warn about name-independent declarations. */
721 : 885 : && !name_independent_decl_p (decl)
722 : 729 : && type != error_mark_node
723 : 3196336 : && (!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 : 697 : 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 : 395 : else if (DECL_CONTEXT (decl) == current_function_decl
744 : : // For -Wunused-but-set-variable leave references alone.
745 : 371 : && !TYPE_REF_P (TREE_TYPE (decl))
746 : 766 : && errorcount == unused_but_set_errorcount)
747 : : {
748 : 362 : 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 : 350 : warning_at (DECL_SOURCE_LOCATION (decl),
754 : 350 : OPT_Wunused_but_set_variable_,
755 : : "variable %qD set but not used", decl);
756 : 362 : unused_but_set_errorcount = errorcount;
757 : : }
758 : : }
759 : : }
760 : :
761 : : /* Remove declarations for all the DECLs in this level. */
762 : 1169780946 : for (link = decls; link; link = TREE_CHAIN (link))
763 : : {
764 : 596520297 : tree name;
765 : 596520297 : if (TREE_CODE (link) == TREE_LIST)
766 : : {
767 : 3886424 : decl = TREE_VALUE (link);
768 : 3886424 : name = TREE_PURPOSE (link);
769 : 3886424 : gcc_checking_assert (name);
770 : : }
771 : : else
772 : : {
773 : 592633873 : decl = link;
774 : 592633873 : name = DECL_NAME (decl);
775 : : }
776 : :
777 : : /* Remove the binding. */
778 : 596520297 : if (TREE_CODE (decl) == LABEL_DECL)
779 : 207 : pop_local_label (name, decl);
780 : : else
781 : 596520090 : pop_local_binding (name, decl);
782 : : }
783 : :
784 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
785 : 573260649 : for (link = current_binding_level->type_shadowed;
786 : 779955127 : link; link = TREE_CHAIN (link))
787 : 206694478 : 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 : 573260649 : if (block)
797 : : {
798 : 177084220 : tree* d;
799 : :
800 : 466020213 : for (d = &BLOCK_VARS (block); *d; )
801 : : {
802 : 288935993 : if (TREE_CODE (*d) == TREE_LIST
803 : 288935993 : || (!processing_template_decl
804 : 91818020 : && undeduced_auto_decl (*d)))
805 : 3886476 : *d = TREE_CHAIN (*d);
806 : : else
807 : 285049517 : 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 : 573260649 : if (functionbody)
814 : : {
815 : 142129113 : 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 : 107047966 : BLOCK_VARS (block) = 0;
821 : 107047966 : pop_labels (block);
822 : : }
823 : : else
824 : 35081147 : pop_labels (subblocks);
825 : : }
826 : :
827 : 573260649 : kind = current_binding_level->kind;
828 : 573260649 : if (kind == sk_cleanup)
829 : : {
830 : 312 : 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 : 312 : stmt = pop_stmt_list (current_binding_level->statement_list);
836 : 312 : stmt = c_build_bind_expr (input_location, block, stmt);
837 : 312 : add_stmt (stmt);
838 : : }
839 : :
840 : 573260649 : leave_scope ();
841 : 573260649 : if (functionbody)
842 : : {
843 : : /* The current function is being defined, so its DECL_INITIAL
844 : : should be error_mark_node. */
845 : 142129113 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
846 : 177210260 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
847 : 142129113 : if (subblocks)
848 : : {
849 : 66006169 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)
850 : 43424317 : && BLOCK_SUBBLOCKS (subblocks))
851 : 1186422 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
852 : : else
853 : 33894986 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
854 : : }
855 : : }
856 : 431131536 : else if (block)
857 : 70036254 : current_binding_level->blocks
858 : 140072508 : = 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 : 361095282 : else if (subblocks)
866 : 18 : current_binding_level->blocks
867 : 36 : = 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 : 573260649 : if (block)
873 : 177084220 : TREE_USED (block) = 1;
874 : :
875 : : /* All temporary bindings created for cleanups are popped silently. */
876 : 573260649 : if (kind == sk_cleanup)
877 : 312 : goto restart;
878 : :
879 : 1146520674 : return block;
880 : 573260337 : }
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 : 140603 : wrapup_namespace_globals ()
888 : : {
889 : 140603 : if (vec<tree, va_gc> *statics = static_decls)
890 : : {
891 : 14998184 : for (tree decl : *statics)
892 : : {
893 : 14882848 : if (warn_unused_function
894 : 986251 : && TREE_CODE (decl) == FUNCTION_DECL
895 : 741376 : && DECL_INITIAL (decl) == 0
896 : 7573 : && DECL_EXTERNAL (decl)
897 : 7573 : && !TREE_PUBLIC (decl)
898 : 29 : && !DECL_ARTIFICIAL (decl)
899 : 29 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
900 : 14882854 : && !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 : 14882848 : if (VAR_P (decl)
906 : 4291767 : && DECL_EXTERNAL (decl)
907 : 1997404 : && DECL_INLINE_VAR_P (decl)
908 : 14882866 : && 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 : 115336 : static_decls = NULL;
915 : :
916 : : /* Write out any globals that need to be output. */
917 : 115336 : return wrapup_global_declarations (statics->address (),
918 : 115336 : 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 : 151390084 : create_implicit_typedef (tree name, tree type)
930 : : {
931 : 151390084 : tree decl;
932 : :
933 : 151390084 : decl = build_decl (input_location, TYPE_DECL, name, type);
934 : 151390084 : 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 : 151390084 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
939 : 151390084 : TYPE_NAME (type) = decl;
940 : 151390084 : TYPE_STUB_DECL (type) = decl;
941 : :
942 : 151390084 : 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 : 703743 : determine_local_discriminator (tree decl, tree name)
956 : : {
957 : 703743 : auto_cond_timevar tv (TV_NAME_LOOKUP);
958 : 703743 : retrofit_lang_decl (decl);
959 : 703743 : tree ctx = DECL_CONTEXT (decl);
960 : 703743 : size_t nelts = vec_safe_length (local_entities);
961 : 703743 : if (name == NULL_TREE)
962 : 703484 : name = (TREE_CODE (decl) == TYPE_DECL
963 : 1128299 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
964 : 1264029 : ? NULL_TREE : DECL_NAME (decl));
965 : 21070085 : for (size_t i = 0; i < nelts; i += 2)
966 : : {
967 : 20366807 : tree *pair = &(*local_entities)[i];
968 : 20366807 : tree d = pair[0];
969 : 20366807 : tree n = pair[1];
970 : 20366807 : gcc_checking_assert (d != decl);
971 : 20366807 : if (name == n
972 : 3641053 : && TREE_CODE (decl) == TREE_CODE (d)
973 : 24007851 : && ctx == DECL_CONTEXT (d))
974 : : {
975 : 465 : tree disc = integer_one_node;
976 : 465 : if (DECL_DISCRIMINATOR (d))
977 : 244 : disc = build_int_cst (TREE_TYPE (disc),
978 : 244 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
979 : 465 : DECL_DISCRIMINATOR (decl) = disc;
980 : : /* Replace the saved decl. */
981 : 465 : pair[0] = decl;
982 : 465 : decl = NULL_TREE;
983 : 465 : break;
984 : : }
985 : : }
986 : :
987 : 703743 : if (decl)
988 : : {
989 : 703278 : vec_safe_reserve (local_entities, 2);
990 : 703278 : local_entities->quick_push (decl);
991 : 703278 : local_entities->quick_push (name);
992 : : }
993 : 703743 : }
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 : 1965118013 : member_like_constrained_friend_p (tree decl)
1010 : : {
1011 : 1965118013 : return (TREE_CODE (decl) == FUNCTION_DECL
1012 : 592895071 : && DECL_UNIQUE_FRIEND_P (decl)
1013 : 85014076 : && DECL_FRIEND_CONTEXT (decl)
1014 : 37918231 : && get_constraints (decl)
1015 : 1965607311 : && (!DECL_TEMPLATE_INFO (decl)
1016 : 489298 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1017 : 3317 : || (uses_outer_template_parms_in_constraints
1018 : 3317 : (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 : 23997303 : 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 : 23997303 : if (member_like_constrained_friend_p (newfn)
1031 : 23997303 : || member_like_constrained_friend_p (oldfn))
1032 : 107 : return false;
1033 : :
1034 : : /* Compare only trailing requirements. */
1035 : 23997196 : tree reqs1 = get_trailing_function_requirements (newfn);
1036 : 23997196 : tree reqs2 = get_trailing_function_requirements (oldfn);
1037 : 23997196 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1038 : : return false;
1039 : :
1040 : : /* Substitution is needed when friends are involved. */
1041 : 23948069 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1042 : 23948069 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1043 : :
1044 : 23948069 : 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 : 641175159 : fns_correspond (tree newdecl, tree olddecl)
1064 : : {
1065 : 641175159 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1066 : : return false;
1067 : :
1068 : 594601014 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1069 : : {
1070 : 594574856 : if (!template_heads_equivalent_p (newdecl, olddecl))
1071 : : return 0;
1072 : 137272069 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1073 : 137272069 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1074 : : }
1075 : :
1076 : 137298227 : tree f1 = TREE_TYPE (newdecl);
1077 : 137298227 : tree f2 = TREE_TYPE (olddecl);
1078 : :
1079 : 137298227 : int rq1 = type_memfn_rqual (f1);
1080 : 137298227 : int rq2 = type_memfn_rqual (f2);
1081 : :
1082 : : /* If only one is a non-static member function, ignore ref-quals. */
1083 : 137298227 : 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 : 137298227 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1093 : 0 : rq1 = rq2;
1094 : :
1095 : 137298227 : bool types_match = rq1 == rq2;
1096 : :
1097 : 137298227 : if (types_match)
1098 : : {
1099 : 137298227 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1100 : 137298227 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1101 : 137298227 : 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 : 137298227 : if (types_match && flag_concepts)
1108 : 16915524 : 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 : 939163610 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1122 : : {
1123 : 939163610 : int types_match;
1124 : :
1125 : 939163610 : if (newdecl == olddecl)
1126 : : return 1;
1127 : :
1128 : 939098141 : 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 : 465002820 : gcc_assert (DECL_P (newdecl));
1134 : :
1135 : 465002820 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1136 : : {
1137 : : /* Specializations of different templates are different functions
1138 : : even if they have the same type. */
1139 : 261378494 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1140 : 261378494 : ? DECL_TI_TEMPLATE (newdecl)
1141 : 600732 : : NULL_TREE);
1142 : 261378494 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1143 : 261378494 : ? DECL_TI_TEMPLATE (olddecl)
1144 : 3191250 : : NULL_TREE);
1145 : 261378494 : if (t1 != t2)
1146 : : return 0;
1147 : :
1148 : 258787910 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1149 : 261916569 : && ! (DECL_EXTERN_C_P (newdecl)
1150 : 3128659 : && 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 : 239372501 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1156 : 239372501 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1157 : : return 0;
1158 : :
1159 : 238690447 : tree f1 = TREE_TYPE (newdecl);
1160 : 238690447 : tree f2 = TREE_TYPE (olddecl);
1161 : 238690447 : 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 : 238526269 : tree r2 = fndecl_declared_return_type (olddecl);
1167 : 238526269 : 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 : 238526269 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1172 : 6520701 : && DECL_EXTERN_C_P (olddecl)
1173 : 4143550 : && !same_type_p (r1, r2)
1174 : 70 : && TREE_CODE (r1) == POINTER_TYPE
1175 : 15 : && TREE_CODE (r2) == POINTER_TYPE
1176 : 238526281 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1177 : : r2 = r1;
1178 : :
1179 : 238526269 : tree p1 = TYPE_ARG_TYPES (f1);
1180 : 238526269 : tree p2 = TYPE_ARG_TYPES (f2);
1181 : :
1182 : 238526269 : if (same_type_p (r1, r2))
1183 : : {
1184 : 175503608 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1185 : 175503570 : && 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 : 175503461 : types_match =
1193 : 175503461 : compparms (p1, p2)
1194 : 28460837 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1195 : 203964062 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1196 : 1220411 : || comp_type_attributes (TREE_TYPE (newdecl),
1197 : 1220411 : 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 : 28460642 : if (types_match && flag_concepts)
1206 : 7081753 : 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 : 28460642 : if (types_match
1212 : 28395979 : && !DECL_EXTERN_C_P (newdecl)
1213 : 20348496 : && !DECL_EXTERN_C_P (olddecl)
1214 : 48805344 : && targetm.target_option.function_versions (newdecl, olddecl))
1215 : : {
1216 : 8733 : if (record_versions)
1217 : 8646 : maybe_version_functions (newdecl, olddecl);
1218 : 8733 : return 0;
1219 : : }
1220 : : }
1221 : 203624326 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1222 : : {
1223 : 202185727 : if (!template_heads_equivalent_p (newdecl, olddecl))
1224 : : return 0;
1225 : :
1226 : 65412406 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1227 : 65412406 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1228 : :
1229 : 65412406 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1230 : : return 0;
1231 : :
1232 : : /* Two template types match if they are the same. Otherwise, compare
1233 : : the underlying declarations. */
1234 : 65412400 : if (TREE_CODE (newres) == TYPE_DECL)
1235 : 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1236 : : else
1237 : 65412370 : types_match = decls_match (newres, oldres);
1238 : : }
1239 : : else
1240 : : {
1241 : : /* Need to check scope for variable declaration (VAR_DECL).
1242 : : For typedef (TYPE_DECL), scope is ignored. */
1243 : 1438599 : if (VAR_P (newdecl)
1244 : 961118 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1245 : : /* [dcl.link]
1246 : : Two declarations for an object with C language linkage
1247 : : with the same name (ignoring the namespace that qualify
1248 : : it) that appear in different namespace scopes refer to
1249 : : the same object. */
1250 : 1615054 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1251 : : return 0;
1252 : :
1253 : 1262192 : if (TREE_TYPE (newdecl) == error_mark_node)
1254 : 0 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1255 : 1262192 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1256 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1257 : 1262192 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1258 : : types_match = 0;
1259 : : else
1260 : 1262192 : types_match = comptypes (TREE_TYPE (newdecl),
1261 : 1262192 : TREE_TYPE (olddecl),
1262 : : COMPARE_REDECLARATION);
1263 : : }
1264 : :
1265 : : return types_match;
1266 : : }
1267 : :
1268 : : /* Mark DECL as versioned if it isn't already. */
1269 : :
1270 : : static void
1271 : 17382 : maybe_mark_function_versioned (tree decl)
1272 : : {
1273 : 17382 : if (!DECL_FUNCTION_VERSIONED (decl))
1274 : : {
1275 : 1047 : DECL_FUNCTION_VERSIONED (decl) = 1;
1276 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1277 : : to include the version marker. */
1278 : 1047 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1279 : 42 : mangle_decl (decl);
1280 : : }
1281 : 17382 : }
1282 : :
1283 : : /* NEWDECL and OLDDECL have identical signatures. If they are
1284 : : different versions adjust them, record function versions, and return
1285 : : true. */
1286 : :
1287 : : bool
1288 : 163801 : maybe_version_functions (tree newdecl, tree olddecl)
1289 : : {
1290 : 163801 : if (!targetm.target_option.function_versions (newdecl, olddecl))
1291 : : return false;
1292 : :
1293 : 8673 : maybe_mark_function_versioned (olddecl);
1294 : 8673 : if (DECL_LOCAL_DECL_P (olddecl))
1295 : : {
1296 : 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1297 : 18 : maybe_mark_function_versioned (olddecl);
1298 : : }
1299 : :
1300 : 8673 : maybe_mark_function_versioned (newdecl);
1301 : 8673 : if (DECL_LOCAL_DECL_P (newdecl))
1302 : : {
1303 : : /* Unfortunately, we can get here before pushdecl naturally calls
1304 : : push_local_extern_decl_alias, so we need to call it directly. */
1305 : 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1306 : 6 : push_local_extern_decl_alias (newdecl);
1307 : 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1308 : 18 : maybe_mark_function_versioned (newdecl);
1309 : : }
1310 : :
1311 : : /* Add the new version to the function version structure. */
1312 : 8673 : cgraph_node *fn_node = cgraph_node::get_create (olddecl);
1313 : 8673 : cgraph_function_version_info *fn_v = fn_node->function_version ();
1314 : 8673 : if (!fn_v)
1315 : 171 : fn_v = fn_node->insert_new_function_version ();
1316 : :
1317 : 8673 : cgraph_node::add_function_version (fn_v, newdecl);
1318 : :
1319 : 8673 : return true;
1320 : : }
1321 : :
1322 : : /* If NEWDECL is `static' and an `extern' was seen previously,
1323 : : warn about it. OLDDECL is the previous declaration.
1324 : :
1325 : : Note that this does not apply to the C++ case of declaring
1326 : : a variable `extern const' and then later `const'.
1327 : :
1328 : : Don't complain about built-in functions, since they are beyond
1329 : : the user's control. */
1330 : :
1331 : : void
1332 : 18985548 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1333 : : {
1334 : 18985548 : if (TREE_CODE (newdecl) == TYPE_DECL
1335 : 18960195 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1336 : 15396968 : || TREE_CODE (newdecl) == CONST_DECL
1337 : 15396968 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1338 : 18985536 : return;
1339 : :
1340 : : /* Don't get confused by static member functions; that's a different
1341 : : use of `static'. */
1342 : 15396968 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1343 : 15396968 : && DECL_STATIC_FUNCTION_P (newdecl))
1344 : : return;
1345 : :
1346 : : /* If the old declaration was `static', or the new one isn't, then
1347 : : everything is OK. */
1348 : 15236659 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1349 : : return;
1350 : :
1351 : : /* It's OK to declare a builtin function as `static'. */
1352 : 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1353 : 12 : && DECL_ARTIFICIAL (olddecl))
1354 : : return;
1355 : :
1356 : 12 : auto_diagnostic_group d;
1357 : 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1358 : : "%qD was declared %<extern%> and later %<static%>", newdecl))
1359 : 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1360 : : "previous declaration of %qD", olddecl);
1361 : 12 : }
1362 : :
1363 : : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1364 : : function templates. If their exception specifications do not
1365 : : match, issue a diagnostic. */
1366 : :
1367 : : static void
1368 : 18568397 : check_redeclaration_exception_specification (tree new_decl,
1369 : : tree old_decl)
1370 : : {
1371 : 18568397 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1372 : 18568397 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1373 : :
1374 : : /* Two default specs are equivalent, don't force evaluation. */
1375 : 7019035 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1376 : 18718349 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1377 : : return;
1378 : :
1379 : 18558122 : if (!type_dependent_expression_p (old_decl))
1380 : : {
1381 : 10996921 : maybe_instantiate_noexcept (new_decl);
1382 : 10996921 : maybe_instantiate_noexcept (old_decl);
1383 : : }
1384 : 18558122 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1385 : 18558122 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1386 : :
1387 : : /* [except.spec]
1388 : :
1389 : : If any declaration of a function has an exception-specification,
1390 : : all declarations, including the definition and an explicit
1391 : : specialization, of that function shall have an
1392 : : exception-specification with the same set of type-ids. */
1393 : 18558122 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1394 : 14542810 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1395 : 33100835 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1396 : : {
1397 : 326 : const char *const msg
1398 : : = G_("declaration of %qF has a different exception specifier");
1399 : 326 : bool complained = true;
1400 : 326 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1401 : 326 : auto_diagnostic_group d;
1402 : :
1403 : : /* Be permissive about C++98 vs C++11 operator new declarations. */
1404 : 652 : bool global_new = (IDENTIFIER_NEW_OP_P (DECL_NAME (new_decl))
1405 : 1 : && CP_DECL_CONTEXT (new_decl) == global_namespace
1406 : 328 : && (nothrow_spec_p (new_exceptions)
1407 : 1 : == nothrow_spec_p (old_exceptions)));
1408 : :
1409 : 326 : if (DECL_IN_SYSTEM_HEADER (old_decl)
1410 : 326 : && (global_new || DECL_EXTERN_C_P (old_decl)))
1411 : : /* Don't fuss about the C library; the C library functions are not
1412 : : specified to have exception specifications (just behave as if they
1413 : : have them), but some implementations include them. */
1414 : 233 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1415 : 93 : else if (!flag_exceptions)
1416 : : /* We used to silently permit mismatched eh specs with
1417 : : -fno-exceptions, so only complain if -pedantic. */
1418 : 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1419 : 90 : else if (!new_exceptions || global_new)
1420 : : /* Reduce to pedwarn for omitted exception specification. No warning
1421 : : flag for this; silence the warning by correcting the code. */
1422 : 21 : complained = pedwarn (new_loc, 0, msg, new_decl);
1423 : : else
1424 : 69 : error_at (new_loc, msg, new_decl);
1425 : :
1426 : 326 : if (complained)
1427 : 96 : inform (DECL_SOURCE_LOCATION (old_decl),
1428 : : "from previous declaration %qF", old_decl);
1429 : :
1430 : : /* Copy the old exception specification if new_decl has none. Unless the
1431 : : old decl is extern "C", as obscure code might depend on the type of
1432 : : the new declaration (e.g. noexcept-type19.C). */
1433 : 326 : if (!new_exceptions && !DECL_EXTERN_C_P (old_decl))
1434 : 20 : TREE_TYPE (new_decl)
1435 : 40 : = build_exception_variant (TREE_TYPE (new_decl), old_exceptions);
1436 : 326 : }
1437 : : }
1438 : :
1439 : : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1440 : : Otherwise issue diagnostics. */
1441 : :
1442 : : bool
1443 : 18986035 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1444 : : {
1445 : 18986035 : old_decl = STRIP_TEMPLATE (old_decl);
1446 : 18986035 : new_decl = STRIP_TEMPLATE (new_decl);
1447 : 18986035 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1448 : 18960658 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1449 : : return true;
1450 : 18960658 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1451 : 18960658 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1452 : : {
1453 : 18919419 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1454 : : return true;
1455 : 37137578 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1456 : 18568789 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1457 : : return true;
1458 : : }
1459 : 41245 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1460 : : {
1461 : : /* With -fimplicit-constexpr, ignore changes in the constexpr
1462 : : keyword. */
1463 : 61 : if (flag_implicit_constexpr
1464 : 61 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1465 : 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1466 : : return true;
1467 : 61 : if (fndecl_built_in_p (old_decl))
1468 : : {
1469 : : /* Hide a built-in declaration. */
1470 : 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1471 : 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1472 : 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1473 : 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1474 : 0 : return true;
1475 : : }
1476 : : /* 7.1.5 [dcl.constexpr]
1477 : : Note: An explicit specialization can differ from the template
1478 : : declaration with respect to the constexpr specifier. */
1479 : 61 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1480 : 61 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1481 : : return true;
1482 : :
1483 : 42 : const char *kind = "constexpr";
1484 : 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1485 : 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1486 : : kind = "consteval";
1487 : 42 : auto_diagnostic_group d;
1488 : 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1489 : : "redeclaration %qD differs in %qs "
1490 : : "from previous declaration", new_decl,
1491 : : kind);
1492 : 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1493 : : "previous declaration %qD", old_decl);
1494 : 42 : return false;
1495 : 42 : }
1496 : : return true;
1497 : : }
1498 : :
1499 : : /* DECL is a redeclaration of a function or function template. If
1500 : : it does have default arguments issue a diagnostic. Note: this
1501 : : function is used to enforce the requirements in C++11 8.3.6 about
1502 : : no default arguments in redeclarations. */
1503 : :
1504 : : static void
1505 : 11201009 : check_redeclaration_no_default_args (tree decl)
1506 : : {
1507 : 11201009 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1508 : :
1509 : 11201009 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1510 : 32976595 : t && t != void_list_node; t = TREE_CHAIN (t))
1511 : 21775643 : if (TREE_PURPOSE (t))
1512 : : {
1513 : 57 : permerror (DECL_SOURCE_LOCATION (decl),
1514 : : "redeclaration of %q#D may not have default "
1515 : : "arguments", decl);
1516 : 57 : return;
1517 : : }
1518 : : }
1519 : :
1520 : : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1521 : : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1522 : : the TEMPLATE_DECLs in case of function templates). This function is used
1523 : : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1524 : : "If a friend declaration specifies a default argument expression, that
1525 : : declaration shall be a definition and shall be the only declaration of
1526 : : the function or function template in the translation unit." */
1527 : :
1528 : : static void
1529 : 6268855 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1530 : : {
1531 : 12173360 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1532 : : return;
1533 : :
1534 : 2063962 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1535 : 1031981 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1536 : 2771578 : t1 && t1 != void_list_node;
1537 : 1739597 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1538 : 2427117 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1539 : 4783948 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1540 : : {
1541 : 36 : auto_diagnostic_group d;
1542 : 36 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1543 : : "friend declaration of %q#D specifies default "
1544 : : "arguments and isn%'t the only declaration", newdecl))
1545 : 36 : inform (DECL_SOURCE_LOCATION (olddecl),
1546 : : "previous declaration of %q#D", olddecl);
1547 : 36 : return;
1548 : 36 : }
1549 : : }
1550 : :
1551 : : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1552 : : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1553 : :
1554 : : static void
1555 : 17990451 : merge_attribute_bits (tree newdecl, tree olddecl)
1556 : : {
1557 : 17990451 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1558 : 17990451 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1559 : 17990451 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1560 : 17990451 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1561 : 17990451 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1562 : 17990451 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1563 : 17990451 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1564 : 17990451 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1565 : 17990451 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1566 : 17990451 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1567 : 17990451 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1568 : 17990451 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1569 : 17990451 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1570 : 17990451 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1571 : 17990451 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1572 : 17990451 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1573 : 17990451 : }
1574 : :
1575 : : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1576 : : && lookup_attribute ("gnu_inline", \
1577 : : DECL_ATTRIBUTES (fn)))
1578 : :
1579 : : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1580 : : ambiguates olddecl. Returns true if an error occurs. */
1581 : :
1582 : : static bool
1583 : 198622434 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1584 : : {
1585 : 198622434 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1586 : 198622434 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1587 : : /* Function template declarations can be differentiated by parameter
1588 : : and return type. */
1589 : 198622434 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1590 : 198622434 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1591 : 198622434 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1592 : : TREE_TYPE (TREE_TYPE (olddecl))))
1593 : : {
1594 : : /* ... and also by their template-heads and requires-clauses. */
1595 : 4113 : if (template_heads_equivalent_p (newdecl, olddecl)
1596 : 4113 : && function_requirements_equivalent_p (newres, oldres))
1597 : : {
1598 : 0 : auto_diagnostic_group d;
1599 : 0 : error ("ambiguating new declaration %q+#D", newdecl);
1600 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1601 : : "old declaration %q#D", olddecl);
1602 : 0 : return true;
1603 : 0 : }
1604 : :
1605 : : /* FIXME: The types are the same but the are differences
1606 : : in either the template heads or function requirements.
1607 : : We should be able to diagnose a set of common errors
1608 : : stemming from these declarations. For example:
1609 : :
1610 : : template<typename T> requires C void f(...);
1611 : : template<typename T> void f(...) requires C;
1612 : :
1613 : : These are functionally equivalent but not equivalent. */
1614 : : }
1615 : :
1616 : : return false;
1617 : : }
1618 : :
1619 : : /* OLD_PARMS is the innermost set of template parameters for some template
1620 : : declaration, and NEW_PARMS is the corresponding set of template parameters
1621 : : for a redeclaration of that template. Merge the default arguments within
1622 : : these two sets of parameters. CLASS_P is true iff the template in
1623 : : question is a class template. */
1624 : :
1625 : : bool
1626 : 8027176 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1627 : : {
1628 : 8027176 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1629 : : == TREE_VEC_LENGTH (old_parms));
1630 : 22734564 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1631 : : {
1632 : 14707418 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1633 : 14707418 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1634 : 14707418 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1635 : 14707418 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1636 : 14707418 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1637 : : return false;
1638 : 14707415 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1639 : : {
1640 : 27 : auto_diagnostic_group d;
1641 : 27 : error ("redefinition of default argument for %q+#D", new_parm);
1642 : 27 : inform (DECL_SOURCE_LOCATION (old_parm),
1643 : : "original definition appeared here");
1644 : 27 : return false;
1645 : 27 : }
1646 : 14707388 : else if (new_default != NULL_TREE)
1647 : : /* Update the previous template parameters (which are the ones
1648 : : that will really count) with the new default value. */
1649 : 197163 : old_default = new_default;
1650 : 14510225 : else if (class_p && old_default != NULL_TREE)
1651 : : /* Update the new parameters, too; they'll be used as the
1652 : : parameters for any members. */
1653 : 1002184 : new_default = old_default;
1654 : : }
1655 : : return true;
1656 : : }
1657 : :
1658 : : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1659 : : If the redeclaration is invalid, a diagnostic is issued, and the
1660 : : error_mark_node is returned. Otherwise, OLDDECL is returned.
1661 : :
1662 : : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1663 : : returned.
1664 : :
1665 : : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1666 : : if the old decl was hidden.
1667 : :
1668 : : Hidden decls can be anticipated builtins, injected friends, or
1669 : : (coming soon) injected from a local-extern decl. */
1670 : :
1671 : : tree
1672 : 342276656 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1673 : : {
1674 : 342276656 : unsigned olddecl_uid = DECL_UID (olddecl);
1675 : 342276656 : int types_match = 0;
1676 : 342276656 : int new_defines_function = 0;
1677 : 342276656 : tree new_template_info;
1678 : 342276656 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1679 : 342276656 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1680 : :
1681 : 342276656 : if (newdecl == olddecl)
1682 : : return olddecl;
1683 : :
1684 : 341419750 : types_match = decls_match (newdecl, olddecl);
1685 : :
1686 : : /* If either the type of the new decl or the type of the old decl is an
1687 : : error_mark_node, then that implies that we have already issued an
1688 : : error (earlier) for some bogus type specification, and in that case,
1689 : : it is rather pointless to harass the user with yet more error message
1690 : : about the same declaration, so just pretend the types match here. */
1691 : 341419750 : if (TREE_TYPE (newdecl) == error_mark_node
1692 : 341419750 : || TREE_TYPE (olddecl) == error_mark_node)
1693 : : return error_mark_node;
1694 : :
1695 : : /* Check for redeclaration and other discrepancies. */
1696 : 341419715 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1697 : 341419715 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1698 : : {
1699 : 6365417 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1700 : : {
1701 : : /* Avoid warnings redeclaring built-ins which have not been
1702 : : explicitly declared. */
1703 : 186637 : if (was_hidden)
1704 : : {
1705 : 186609 : if (TREE_PUBLIC (newdecl)
1706 : 186609 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1707 : 12 : warning_at (newdecl_loc,
1708 : 12 : OPT_Wbuiltin_declaration_mismatch,
1709 : : "built-in function %qD declared as non-function",
1710 : : newdecl);
1711 : 186609 : return NULL_TREE;
1712 : : }
1713 : :
1714 : : /* If you declare a built-in or predefined function name as static,
1715 : : the old definition is overridden, but optionally warn this was a
1716 : : bad choice of name. */
1717 : 28 : if (! TREE_PUBLIC (newdecl))
1718 : : {
1719 : 28 : warning_at (newdecl_loc,
1720 : 28 : OPT_Wshadow,
1721 : 28 : fndecl_built_in_p (olddecl)
1722 : : ? G_("shadowing built-in function %q#D")
1723 : : : G_("shadowing library function %q#D"), olddecl);
1724 : : /* Discard the old built-in function. */
1725 : 28 : return NULL_TREE;
1726 : : }
1727 : : /* If the built-in is not ansi, then programs can override
1728 : : it even globally without an error. */
1729 : 0 : else if (! fndecl_built_in_p (olddecl))
1730 : 0 : warning_at (newdecl_loc, 0,
1731 : : "library function %q#D redeclared as non-function %q#D",
1732 : : olddecl, newdecl);
1733 : : else
1734 : 0 : error_at (newdecl_loc,
1735 : : "declaration of %q#D conflicts with built-in "
1736 : : "declaration %q#D", newdecl, olddecl);
1737 : 0 : return NULL_TREE;
1738 : : }
1739 : 6178780 : else if (!types_match)
1740 : : {
1741 : : /* Avoid warnings redeclaring built-ins which have not been
1742 : : explicitly declared. */
1743 : 2163468 : if (was_hidden)
1744 : : {
1745 : 824653 : tree t1, t2;
1746 : :
1747 : : /* A new declaration doesn't match a built-in one unless it
1748 : : is also extern "C". */
1749 : 824653 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1750 : 824653 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1751 : 824653 : if (!DECL_EXTERN_C_P (newdecl))
1752 : : return NULL_TREE;
1753 : :
1754 : 142599 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1755 : 142599 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1756 : 244940 : t1 || t2;
1757 : 102341 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1758 : : {
1759 : 244922 : if (!t1 || !t2)
1760 : : break;
1761 : : /* FILE, tm types are not known at the time
1762 : : we create the builtins. */
1763 : 761192 : for (unsigned i = 0;
1764 : 1005952 : i < sizeof (builtin_structptr_types)
1765 : : / sizeof (builtin_structptr_type);
1766 : : ++i)
1767 : 903605 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1768 : : {
1769 : 142413 : tree t = TREE_VALUE (t1);
1770 : :
1771 : 142413 : if (TYPE_PTR_P (t)
1772 : 284826 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1773 : 142413 : == get_identifier (builtin_structptr_types[i].str)
1774 : 284811 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1775 : : {
1776 : 142398 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1777 : :
1778 : 142398 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1779 : 142398 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1780 : 142398 : types_match = decls_match (newdecl, olddecl);
1781 : 142398 : if (types_match)
1782 : 142398 : return duplicate_decls (newdecl, olddecl,
1783 : 142398 : hiding, was_hidden);
1784 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1785 : : }
1786 : 15 : goto next_arg;
1787 : : }
1788 : :
1789 : 102347 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1790 : : break;
1791 : 102341 : next_arg:;
1792 : : }
1793 : :
1794 : 201 : warning_at (newdecl_loc,
1795 : 201 : OPT_Wbuiltin_declaration_mismatch,
1796 : : "declaration of %q#D conflicts with built-in "
1797 : : "declaration %q#D", newdecl, olddecl);
1798 : : }
1799 : 1338815 : else if ((DECL_EXTERN_C_P (newdecl)
1800 : 15 : && DECL_EXTERN_C_P (olddecl))
1801 : 2677615 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1802 : 1338800 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1803 : : {
1804 : : /* Don't really override olddecl for __* prefixed builtins
1805 : : except for __[^b]*_chk, the compiler might be using those
1806 : : explicitly. */
1807 : 15 : if (fndecl_built_in_p (olddecl))
1808 : : {
1809 : 15 : tree id = DECL_NAME (olddecl);
1810 : 15 : const char *name = IDENTIFIER_POINTER (id);
1811 : 15 : size_t len;
1812 : :
1813 : 15 : if (name[0] == '_'
1814 : 15 : && name[1] == '_'
1815 : 30 : && (startswith (name + 2, "builtin_")
1816 : 6 : || (len = strlen (name)) <= strlen ("___chk")
1817 : 6 : || memcmp (name + len - strlen ("_chk"),
1818 : : "_chk", strlen ("_chk") + 1) != 0))
1819 : : {
1820 : 15 : if (DECL_INITIAL (newdecl))
1821 : : {
1822 : 6 : error_at (newdecl_loc,
1823 : : "definition of %q#D ambiguates built-in "
1824 : : "declaration %q#D", newdecl, olddecl);
1825 : 6 : return error_mark_node;
1826 : : }
1827 : 9 : auto_diagnostic_group d;
1828 : 9 : if (permerror (newdecl_loc,
1829 : : "new declaration %q#D ambiguates built-in"
1830 : : " declaration %q#D", newdecl, olddecl)
1831 : 9 : && flag_permissive)
1832 : 3 : inform (newdecl_loc,
1833 : : "ignoring the %q#D declaration", newdecl);
1834 : 9 : return flag_permissive ? olddecl : error_mark_node;
1835 : 9 : }
1836 : : }
1837 : :
1838 : : /* A near match; override the builtin. */
1839 : :
1840 : 0 : if (TREE_PUBLIC (newdecl))
1841 : 0 : warning_at (newdecl_loc,
1842 : 0 : OPT_Wbuiltin_declaration_mismatch,
1843 : : "new declaration %q#D ambiguates built-in "
1844 : : "declaration %q#D", newdecl, olddecl);
1845 : : else
1846 : 0 : warning (OPT_Wshadow,
1847 : : fndecl_built_in_p (olddecl)
1848 : : ? G_("shadowing built-in function %q#D")
1849 : : : G_("shadowing library function %q#D"), olddecl);
1850 : : }
1851 : : else
1852 : : /* Discard the old built-in function. */
1853 : : return NULL_TREE;
1854 : :
1855 : : /* Replace the old RTL to avoid problems with inlining. */
1856 : 201 : COPY_DECL_RTL (newdecl, olddecl);
1857 : : }
1858 : : else
1859 : : {
1860 : : /* Even if the types match, prefer the new declarations type
1861 : : for built-ins which have not been explicitly declared,
1862 : : for exception lists, etc... */
1863 : 4015312 : tree type = TREE_TYPE (newdecl);
1864 : 4015312 : tree attribs = (*targetm.merge_type_attributes)
1865 : 4015312 : (TREE_TYPE (olddecl), type);
1866 : :
1867 : 4015312 : type = cp_build_type_attribute_variant (type, attribs);
1868 : 4015312 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1869 : : }
1870 : :
1871 : : /* If a function is explicitly declared "throw ()", propagate that to
1872 : : the corresponding builtin. */
1873 : 4015513 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1874 : 3858474 : && was_hidden
1875 : 3858431 : && TREE_NOTHROW (newdecl)
1876 : 7616428 : && !TREE_NOTHROW (olddecl))
1877 : : {
1878 : 28915 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1879 : 28915 : tree tmpdecl = builtin_decl_explicit (fncode);
1880 : 28915 : if (tmpdecl && tmpdecl != olddecl && types_match)
1881 : 28915 : TREE_NOTHROW (tmpdecl) = 1;
1882 : : }
1883 : :
1884 : : /* Whether or not the builtin can throw exceptions has no
1885 : : bearing on this declarator. */
1886 : 4015513 : TREE_NOTHROW (olddecl) = 0;
1887 : :
1888 : 4015513 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1889 : : {
1890 : : /* If a builtin function is redeclared as `static', merge
1891 : : the declarations, but make the original one static. */
1892 : 3 : DECL_THIS_STATIC (olddecl) = 1;
1893 : 3 : TREE_PUBLIC (olddecl) = 0;
1894 : :
1895 : : /* Make the old declaration consistent with the new one so
1896 : : that all remnants of the builtin-ness of this function
1897 : : will be banished. */
1898 : 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1899 : 3 : COPY_DECL_RTL (newdecl, olddecl);
1900 : : }
1901 : : }
1902 : 335054298 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1903 : : {
1904 : : /* C++ Standard, 3.3, clause 4:
1905 : : "[Note: a namespace name or a class template name must be unique
1906 : : in its declarative region (7.3.2, clause 14). ]" */
1907 : 84659528 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
1908 : 84659507 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1909 : : /* Namespace conflicts with not namespace. */;
1910 : 48683424 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
1911 : 133342919 : || DECL_TYPE_TEMPLATE_P (newdecl))
1912 : : /* Class template conflicts. */;
1913 : 84659489 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1914 : 48683424 : && DECL_TEMPLATE_RESULT (olddecl)
1915 : 48683424 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1916 : 133342907 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
1917 : 35933375 : && DECL_TEMPLATE_RESULT (newdecl)
1918 : 35933375 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1919 : : /* Variable template conflicts. */;
1920 : 84659480 : else if (concept_definition_p (olddecl)
1921 : 169318954 : || concept_definition_p (newdecl))
1922 : : /* Concept conflicts. */;
1923 : 84659468 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1924 : 48723399 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
1925 : 84699455 : || (TREE_CODE (olddecl) == FUNCTION_DECL
1926 : 35933382 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1927 : : {
1928 : : /* One is a function and the other is a template
1929 : : function. */
1930 : 84616763 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1931 : : return NULL_TREE;
1932 : :
1933 : : /* There can only be one! */
1934 : 59659 : auto_diagnostic_group d;
1935 : 59659 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
1936 : 59659 : && check_raw_literal_operator (olddecl))
1937 : 3 : error_at (newdecl_loc,
1938 : : "literal operator %q#D conflicts with"
1939 : : " raw literal operator", newdecl);
1940 : 59656 : else if (check_raw_literal_operator (newdecl))
1941 : 3 : error_at (newdecl_loc,
1942 : : "raw literal operator %q#D conflicts with"
1943 : : " literal operator template", newdecl);
1944 : : else
1945 : : return NULL_TREE;
1946 : :
1947 : 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1948 : 6 : return error_mark_node;
1949 : 59659 : }
1950 : 42705 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
1951 : : /* A structured binding must be unique in its declarative region. */;
1952 : 40125 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1953 : 42702 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1954 : : /* One is an implicit typedef, that's ok. */
1955 : : return NULL_TREE;
1956 : :
1957 : 147 : auto_diagnostic_group d;
1958 : 147 : error ("%q#D redeclared as different kind of entity", newdecl);
1959 : 147 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1960 : :
1961 : 147 : return error_mark_node;
1962 : 147 : }
1963 : 250394770 : else if (!types_match)
1964 : : {
1965 : 235392863 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1966 : : /* These are certainly not duplicate declarations; they're
1967 : : from different scopes. */
1968 : : return NULL_TREE;
1969 : :
1970 : 235392803 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1971 : : {
1972 : 198622452 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1973 : 198622452 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1974 : :
1975 : : /* The name of a class template may not be declared to refer to
1976 : : any other template, class, function, object, namespace, value,
1977 : : or type in the same scope. */
1978 : 198622452 : if (TREE_CODE (oldres) == TYPE_DECL
1979 : 198622446 : || TREE_CODE (newres) == TYPE_DECL)
1980 : : {
1981 : 9 : auto_diagnostic_group d;
1982 : 9 : error_at (newdecl_loc,
1983 : : "conflicting declaration of template %q#D", newdecl);
1984 : 9 : inform (olddecl_loc,
1985 : : "previous declaration %q#D", olddecl);
1986 : 9 : return error_mark_node;
1987 : 9 : }
1988 : :
1989 : 198622443 : else if (TREE_CODE (oldres) == FUNCTION_DECL
1990 : 198622434 : && TREE_CODE (newres) == FUNCTION_DECL)
1991 : : {
1992 : 198622434 : if (duplicate_function_template_decls (newdecl, olddecl))
1993 : 0 : return error_mark_node;
1994 : : return NULL_TREE;
1995 : : }
1996 : : return NULL_TREE;
1997 : : }
1998 : 36770351 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1999 : : {
2000 : 36770206 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
2001 : : {
2002 : 57 : auto_diagnostic_group d;
2003 : 57 : error_at (newdecl_loc,
2004 : : "conflicting declaration of C function %q#D",
2005 : : newdecl);
2006 : 57 : inform (olddecl_loc,
2007 : : "previous declaration %q#D", olddecl);
2008 : 57 : return error_mark_node;
2009 : 57 : }
2010 : : /* For function versions, params and types match, but they
2011 : : are not ambiguous. */
2012 : 36770149 : else if (((!DECL_FUNCTION_VERSIONED (newdecl)
2013 : 36761962 : && !DECL_FUNCTION_VERSIONED (olddecl))
2014 : 8232 : || !same_type_p (fndecl_declared_return_type (newdecl),
2015 : : fndecl_declared_return_type (olddecl)))
2016 : : /* Let constrained hidden friends coexist for now, we'll
2017 : : check satisfaction later. */
2018 : 36761917 : && !member_like_constrained_friend_p (newdecl)
2019 : 36366425 : && !member_like_constrained_friend_p (olddecl)
2020 : : // The functions have the same parameter types.
2021 : 36333844 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2022 : 36333844 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2023 : : // And the same constraints.
2024 : 36770212 : && equivalently_constrained (newdecl, olddecl))
2025 : : {
2026 : 63 : auto_diagnostic_group d;
2027 : 63 : error_at (newdecl_loc,
2028 : : "ambiguating new declaration of %q#D", newdecl);
2029 : 63 : inform (olddecl_loc,
2030 : : "old declaration %q#D", olddecl);
2031 : 63 : return error_mark_node;
2032 : 63 : }
2033 : : else
2034 : 36770086 : return NULL_TREE;
2035 : : }
2036 : : else
2037 : : {
2038 : 145 : auto_diagnostic_group d;
2039 : 145 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2040 : 145 : inform (olddecl_loc,
2041 : : "previous declaration as %q#D", olddecl);
2042 : 145 : return error_mark_node;
2043 : 145 : }
2044 : : }
2045 : 15001907 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2046 : 15001907 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2047 : : {
2048 : : /* OMP UDRs are never duplicates. */
2049 : 27 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2050 : 27 : auto_diagnostic_group d;
2051 : 27 : error_at (newdecl_loc,
2052 : : "redeclaration of %<pragma omp declare reduction%>");
2053 : 27 : inform (olddecl_loc,
2054 : : "previous %<pragma omp declare reduction%> declaration");
2055 : 27 : return error_mark_node;
2056 : 27 : }
2057 : 15001880 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2058 : 15001880 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2059 : 220 : && (!DECL_TEMPLATE_INFO (newdecl)
2060 : 220 : || (DECL_TI_TEMPLATE (newdecl)
2061 : 220 : != DECL_TI_TEMPLATE (olddecl))))
2062 : 10990194 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2063 : 578208 : && (!DECL_TEMPLATE_INFO (olddecl)
2064 : 578208 : || (DECL_TI_TEMPLATE (olddecl)
2065 : 578208 : != DECL_TI_TEMPLATE (newdecl))))))
2066 : : /* It's OK to have a template specialization and a non-template
2067 : : with the same type, or to have specializations of two
2068 : : different templates with the same type. Note that if one is a
2069 : : specialization, and the other is an instantiation of the same
2070 : : template, that we do not exit at this point. That situation
2071 : : can occur if we instantiate a template class, and then
2072 : : specialize one of its methods. This situation is valid, but
2073 : : the declarations must be merged in the usual way. */
2074 : : return NULL_TREE;
2075 : 15001880 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2076 : 15001880 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2077 : 600446 : && !DECL_USE_TEMPLATE (newdecl))
2078 : 10990194 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2079 : 22458 : && !DECL_USE_TEMPLATE (olddecl))))
2080 : : /* One of the declarations is a template instantiation, and the
2081 : : other is not a template at all. That's OK. */
2082 : : return NULL_TREE;
2083 : 15001880 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2084 : : {
2085 : : /* In [namespace.alias] we have:
2086 : :
2087 : : In a declarative region, a namespace-alias-definition can be
2088 : : used to redefine a namespace-alias declared in that declarative
2089 : : region to refer only to the namespace to which it already
2090 : : refers.
2091 : :
2092 : : Therefore, if we encounter a second alias directive for the same
2093 : : alias, we can just ignore the second directive. */
2094 : 21 : if (DECL_NAMESPACE_ALIAS (newdecl)
2095 : 21 : && (DECL_NAMESPACE_ALIAS (newdecl)
2096 : 18 : == DECL_NAMESPACE_ALIAS (olddecl)))
2097 : : return olddecl;
2098 : :
2099 : : /* Leave it to update_binding to merge or report error. */
2100 : : return NULL_TREE;
2101 : : }
2102 : : else
2103 : : {
2104 : 15001859 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2105 : 15001859 : if (errmsg)
2106 : : {
2107 : 608 : auto_diagnostic_group d;
2108 : 608 : error_at (newdecl_loc, errmsg, newdecl);
2109 : 608 : if (DECL_NAME (olddecl) != NULL_TREE)
2110 : 608 : inform (olddecl_loc,
2111 : 608 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2112 : : ? G_("%q#D previously defined here")
2113 : : : G_("%q#D previously declared here"), olddecl);
2114 : 608 : if (cxx_dialect >= cxx26
2115 : 147 : && DECL_NAME (newdecl)
2116 : 147 : && id_equal (DECL_NAME (newdecl), "_")
2117 : 621 : && !name_independent_decl_p (newdecl))
2118 : : {
2119 : 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2120 : 2 : inform (newdecl_loc,
2121 : : "parameter declaration is not name-independent");
2122 : 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2123 : : {
2124 : 3 : if (at_namespace_scope_p ())
2125 : 2 : inform (newdecl_loc,
2126 : : "structured binding at namespace scope is not "
2127 : : "name-independent");
2128 : 1 : else if (TREE_STATIC (newdecl))
2129 : 1 : inform (newdecl_loc,
2130 : : "static structured binding is not "
2131 : : "name-independent");
2132 : 0 : else if (DECL_EXTERNAL (newdecl))
2133 : 0 : inform (newdecl_loc,
2134 : : "extern structured binding is not "
2135 : : "name-independent");
2136 : : }
2137 : 8 : else if (at_class_scope_p ()
2138 : 0 : && VAR_P (newdecl)
2139 : 8 : && TREE_STATIC (newdecl))
2140 : 0 : inform (newdecl_loc,
2141 : : "static data member is not name-independent");
2142 : 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2143 : 3 : inform (newdecl_loc,
2144 : : "variable at namespace scope is not name-independent");
2145 : 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2146 : 3 : inform (newdecl_loc,
2147 : : "static variable is not name-independent");
2148 : 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2149 : 0 : inform (newdecl_loc,
2150 : : "extern variable is not name-independent");
2151 : : }
2152 : 608 : return error_mark_node;
2153 : 608 : }
2154 : 15001251 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2155 : 10989957 : && DECL_INITIAL (olddecl) != NULL_TREE
2156 : 11723 : && !prototype_p (TREE_TYPE (olddecl))
2157 : 15001251 : && prototype_p (TREE_TYPE (newdecl)))
2158 : : {
2159 : : /* Prototype decl follows defn w/o prototype. */
2160 : 0 : auto_diagnostic_group d;
2161 : 0 : if (warning_at (newdecl_loc, 0,
2162 : : "prototype specified for %q#D", newdecl))
2163 : 0 : inform (olddecl_loc,
2164 : : "previous non-prototype definition here");
2165 : 0 : }
2166 : 14609708 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2167 : 25991208 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2168 : : {
2169 : : /* [dcl.link]
2170 : : If two declarations of the same function or object
2171 : : specify different linkage-specifications ..., the program
2172 : : is ill-formed.... Except for functions with C++ linkage,
2173 : : a function declaration without a linkage specification
2174 : : shall not precede the first linkage specification for
2175 : : that function. A function can be declared without a
2176 : : linkage specification after an explicit linkage
2177 : : specification has been seen; the linkage explicitly
2178 : : specified in the earlier declaration is not affected by
2179 : : such a function declaration.
2180 : :
2181 : : DR 563 raises the question why the restrictions on
2182 : : functions should not also apply to objects. Older
2183 : : versions of G++ silently ignore the linkage-specification
2184 : : for this example:
2185 : :
2186 : : namespace N {
2187 : : extern int i;
2188 : : extern "C" int i;
2189 : : }
2190 : :
2191 : : which is clearly wrong. Therefore, we now treat objects
2192 : : like functions. */
2193 : 1782 : if (current_lang_depth () == 0)
2194 : : {
2195 : : /* There is no explicit linkage-specification, so we use
2196 : : the linkage from the previous declaration. */
2197 : 1776 : retrofit_lang_decl (newdecl);
2198 : 1776 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2199 : : }
2200 : : else
2201 : : {
2202 : 6 : auto_diagnostic_group d;
2203 : 6 : error_at (newdecl_loc,
2204 : : "conflicting declaration of %q#D with %qL linkage",
2205 : 6 : newdecl, DECL_LANGUAGE (newdecl));
2206 : 6 : inform (olddecl_loc,
2207 : : "previous declaration with %qL linkage",
2208 : 6 : DECL_LANGUAGE (olddecl));
2209 : 6 : }
2210 : : }
2211 : :
2212 : 15001251 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2213 : : ;
2214 : 14379375 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2215 : : {
2216 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2217 : 12699770 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2218 : 10389300 : && (/* grokfndecl passes member function templates too
2219 : : as FUNCTION_DECLs. */
2220 : 8239014 : DECL_TEMPLATE_INFO (olddecl)
2221 : : /* C++11 8.3.6/6.
2222 : : Default arguments for a member function of a class
2223 : : template shall be specified on the initial declaration
2224 : : of the member function within the class template. */
2225 : 658428 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2226 : : {
2227 : 7660727 : check_redeclaration_no_default_args (newdecl);
2228 : :
2229 : 7660727 : if (DECL_TEMPLATE_INFO (olddecl)
2230 : 7660727 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2231 : : {
2232 : 2141276 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2233 : 2141276 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2234 : 2141276 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2235 : 2141276 : tree old_parms
2236 : 2141276 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2237 : 2141276 : merge_default_template_args (new_parms, old_parms,
2238 : : /*class_p=*/false);
2239 : : }
2240 : : }
2241 : : else
2242 : : {
2243 : 2728573 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2244 : 2728573 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2245 : 2728573 : int i = 1;
2246 : :
2247 : 7071402 : for (; t1 && t1 != void_list_node;
2248 : 4342829 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2249 : 4344616 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2250 : : {
2251 : 57 : if (simple_cst_equal (TREE_PURPOSE (t1),
2252 : 57 : TREE_PURPOSE (t2)) == 1)
2253 : : {
2254 : 24 : auto_diagnostic_group d;
2255 : 24 : if (permerror (newdecl_loc,
2256 : : "default argument given for parameter "
2257 : : "%d of %q#D", i, newdecl))
2258 : 24 : inform (olddecl_loc,
2259 : : "previous specification in %q#D here",
2260 : : olddecl);
2261 : 24 : }
2262 : : else
2263 : : {
2264 : 33 : auto_diagnostic_group d;
2265 : 33 : error_at (newdecl_loc,
2266 : : "default argument given for parameter %d "
2267 : : "of %q#D", i, newdecl);
2268 : 33 : inform (olddecl_loc,
2269 : : "previous specification in %q#D here",
2270 : : olddecl);
2271 : 33 : }
2272 : : }
2273 : :
2274 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2275 : : argument expression, that declaration... shall be the only
2276 : : declaration of the function or function template in the
2277 : : translation unit." */
2278 : 2728573 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2279 : : }
2280 : : }
2281 : : }
2282 : :
2283 : : /* Do not merge an implicit typedef with an explicit one. In:
2284 : :
2285 : : class A;
2286 : : ...
2287 : : typedef class A A __attribute__ ((foo));
2288 : :
2289 : : the attribute should apply only to the typedef. */
2290 : 19016764 : if (TREE_CODE (olddecl) == TYPE_DECL
2291 : 19016764 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2292 : 25356 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2293 : : return NULL_TREE;
2294 : :
2295 : 18985614 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2296 : : return NULL_TREE;
2297 : :
2298 : 18985602 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2299 : 30 : return error_mark_node;
2300 : :
2301 : 18985572 : if (modules_p ()
2302 : 41374 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2303 : 19010244 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2304 : : {
2305 : 24672 : if (!module_may_redeclare (olddecl, newdecl))
2306 : 24 : return error_mark_node;
2307 : :
2308 : 24648 : if (!hiding)
2309 : : {
2310 : : /* The old declaration should match the exportingness of the new
2311 : : declaration. But hidden friend declarations just keep the
2312 : : exportingness of the old declaration; see CWG2588. */
2313 : 21844 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2314 : 21844 : if (DECL_LANG_SPECIFIC (not_tmpl)
2315 : 21802 : && DECL_MODULE_ATTACH_P (not_tmpl)
2316 : : /* Typedefs are not entities and so are OK to be redeclared
2317 : : as exported: see [module.interface]/p6. */
2318 : 22057 : && TREE_CODE (olddecl) != TYPE_DECL)
2319 : : {
2320 : 201 : if (DECL_MODULE_EXPORT_P (newdecl)
2321 : 201 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2322 : : {
2323 : 15 : auto_diagnostic_group d;
2324 : 15 : error ("conflicting exporting for declaration %qD", newdecl);
2325 : 15 : inform (olddecl_loc,
2326 : : "previously declared here without exporting");
2327 : 15 : }
2328 : : }
2329 : 21643 : else if (DECL_MODULE_EXPORT_P (newdecl))
2330 : 9032 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2331 : : }
2332 : : }
2333 : :
2334 : : /* We have committed to returning OLDDECL at this point. */
2335 : :
2336 : : /* If new decl is `static' and an `extern' was seen previously,
2337 : : warn about it. */
2338 : 18985548 : warn_extern_redeclared_static (newdecl, olddecl);
2339 : :
2340 : : /* True to merge attributes between the declarations, false to
2341 : : set OLDDECL's attributes to those of NEWDECL (for template
2342 : : explicit specializations that specify their own attributes
2343 : : independent of those specified for the primary template). */
2344 : 18985548 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2345 : 15005425 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2346 : 19563741 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2347 : :
2348 : 18985548 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2349 : : {
2350 : 15005425 : if (merge_attr)
2351 : : {
2352 : 14427437 : {
2353 : 14427437 : auto_diagnostic_group d;
2354 : 14427437 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2355 : 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2356 : : ? G_("previous definition of %qD here")
2357 : : : G_("previous declaration of %qD here"), olddecl);
2358 : 14427437 : }
2359 : :
2360 : : /* [dcl.attr.noreturn]: The first declaration of a function shall
2361 : : specify the noreturn attribute if any declaration of that function
2362 : : specifies the noreturn attribute. */
2363 : 14427437 : tree a;
2364 : 14427437 : if (TREE_THIS_VOLATILE (newdecl)
2365 : 111469 : && !TREE_THIS_VOLATILE (olddecl)
2366 : : /* This applies to [[noreturn]] only, not its GNU variants. */
2367 : 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2368 : 54 : && cxx11_attribute_p (a)
2369 : 14427458 : && get_attribute_namespace (a) == NULL_TREE)
2370 : : {
2371 : 9 : auto_diagnostic_group d;
2372 : 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2373 : : "but its first declaration was not", newdecl);
2374 : 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2375 : 9 : }
2376 : : }
2377 : :
2378 : : /* Now that functions must hold information normally held
2379 : : by field decls, there is extra work to do so that
2380 : : declaration information does not get destroyed during
2381 : : definition. */
2382 : 15005425 : if (DECL_VINDEX (olddecl))
2383 : 163377 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2384 : 15005425 : if (DECL_CONTEXT (olddecl))
2385 : 15005425 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2386 : 15005425 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2387 : 15005425 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2388 : 15005425 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2389 : 15005425 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2390 : 15005425 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2391 : 15005425 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2392 : 15005425 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2393 : 15005425 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2394 : 15005425 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2395 : 15005425 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2396 : 15005425 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2397 : 2505658 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2398 : 5011316 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2399 : 15005425 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2400 : :
2401 : 15005425 : duplicate_contracts (newdecl, olddecl);
2402 : :
2403 : : /* Optionally warn about more than one declaration for the same
2404 : : name, but don't warn about a function declaration followed by a
2405 : : definition. */
2406 : 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2407 : 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2408 : : /* Don't warn about extern decl followed by definition. */
2409 : 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2410 : : /* Don't warn if at least one is/was hidden. */
2411 : 27 : && !(hiding || was_hidden)
2412 : : /* Don't warn about declaration followed by specialization. */
2413 : 15005437 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2414 : 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2415 : : {
2416 : 6 : auto_diagnostic_group d;
2417 : 6 : if (warning_at (newdecl_loc,
2418 : 6 : OPT_Wredundant_decls,
2419 : : "redundant redeclaration of %qD in same scope",
2420 : : newdecl))
2421 : 6 : inform (olddecl_loc,
2422 : : "previous declaration of %qD", olddecl);
2423 : 6 : }
2424 : :
2425 : : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2426 : : first declaration of the function or, for an explicit specialization
2427 : : of a function template, the first declaration of that
2428 : : specialization. */
2429 : 15005425 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2430 : 600446 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2431 : : {
2432 : 14427437 : if (DECL_DELETED_FN (newdecl))
2433 : : {
2434 : 21 : auto_diagnostic_group d;
2435 : 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2436 : : "is not first declaration", newdecl))
2437 : 21 : inform (olddecl_loc,
2438 : : "previous declaration of %qD", olddecl);
2439 : 21 : }
2440 : 14427437 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2441 : 14427437 : if (DECL_DELETED_FN (olddecl)
2442 : 6 : && DECL_INITIAL (olddecl)
2443 : 14427443 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2444 : 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2445 : : }
2446 : : }
2447 : :
2448 : : /* Deal with C++: must preserve virtual function table size. */
2449 : 18985548 : if (TREE_CODE (olddecl) == TYPE_DECL)
2450 : : {
2451 : 25353 : tree newtype = TREE_TYPE (newdecl);
2452 : 25353 : tree oldtype = TREE_TYPE (olddecl);
2453 : :
2454 : 25353 : if (newtype != error_mark_node && oldtype != error_mark_node
2455 : 50706 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2456 : 64860 : CLASSTYPE_FRIEND_CLASSES (newtype)
2457 : 21620 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2458 : :
2459 : 25353 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2460 : : }
2461 : :
2462 : : /* Copy all the DECL_... slots specified in the new decl except for
2463 : : any that we copy here from the old type. */
2464 : 18985548 : if (merge_attr)
2465 : 18407560 : DECL_ATTRIBUTES (newdecl)
2466 : 36815120 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2467 : : else
2468 : 577988 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2469 : :
2470 : 18985548 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2471 : : {
2472 : 3563227 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2473 : 3563227 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2474 : 3563227 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2475 : :
2476 : : /* The new decl should not already have gathered any
2477 : : specializations. */
2478 : 3563227 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2479 : :
2480 : : /* Make sure the contracts are equivalent. */
2481 : 3563227 : duplicate_contracts (newdecl, olddecl);
2482 : :
2483 : : /* Remove contracts from old_result so they aren't appended to
2484 : : old_result by the merge function. */
2485 : 3563227 : remove_contract_attributes (old_result);
2486 : :
2487 : 3563227 : DECL_ATTRIBUTES (old_result)
2488 : 3563227 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2489 : :
2490 : 3563227 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2491 : : {
2492 : 3563197 : if (DECL_SOURCE_LOCATION (newdecl)
2493 : 3563197 : != DECL_SOURCE_LOCATION (olddecl))
2494 : : {
2495 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2496 : : later declarations of a function template. */
2497 : 3540282 : check_redeclaration_no_default_args (newdecl);
2498 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2499 : : argument expression, that declaration... shall be the only
2500 : : declaration of the function or function template in the
2501 : : translation unit." */
2502 : 3540282 : check_no_redeclaration_friend_default_args
2503 : 3540282 : (old_result, new_result);
2504 : :
2505 : 3540282 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2506 : 3540282 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2507 : 3540282 : merge_default_template_args (new_parms, old_parms,
2508 : : /*class_p=*/false);
2509 : : }
2510 : 3563197 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2511 : 2912376 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2512 : :
2513 : 3563197 : check_default_args (newdecl);
2514 : :
2515 : 7126382 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2516 : 3563197 : && DECL_INITIAL (new_result))
2517 : : {
2518 : 24 : if (DECL_INITIAL (old_result))
2519 : 12 : DECL_UNINLINABLE (old_result) = 1;
2520 : : else
2521 : 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2522 : 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2523 : 72 : DECL_NOT_REALLY_EXTERN (old_result)
2524 : 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2525 : 48 : DECL_INTERFACE_KNOWN (old_result)
2526 : 24 : = DECL_INTERFACE_KNOWN (new_result);
2527 : 24 : DECL_DECLARED_INLINE_P (old_result)
2528 : 24 : = DECL_DECLARED_INLINE_P (new_result);
2529 : 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2530 : 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2531 : : }
2532 : : else
2533 : : {
2534 : 3563173 : DECL_DECLARED_INLINE_P (old_result)
2535 : 3563173 : |= DECL_DECLARED_INLINE_P (new_result);
2536 : 3563173 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2537 : 3563173 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2538 : 3563173 : check_redeclaration_exception_specification (newdecl, olddecl);
2539 : :
2540 : 3563173 : merge_attribute_bits (new_result, old_result);
2541 : : }
2542 : : }
2543 : :
2544 : : /* Propagate purviewness and importingness as with
2545 : : set_instantiating_module, unless newdecl is a friend injection. */
2546 : 3563227 : if (modules_p () && DECL_LANG_SPECIFIC (new_result)
2547 : 3574809 : && !(TREE_CODE (new_result) == FUNCTION_DECL
2548 : 11576 : && DECL_UNIQUE_FRIEND_P (new_result)))
2549 : : {
2550 : 9756 : if (DECL_MODULE_PURVIEW_P (new_result))
2551 : 4755 : DECL_MODULE_PURVIEW_P (old_result) = true;
2552 : 9756 : if (!DECL_MODULE_IMPORT_P (new_result))
2553 : 9756 : DECL_MODULE_IMPORT_P (old_result) = false;
2554 : : }
2555 : :
2556 : : /* If the new declaration is a definition, update the file and
2557 : : line information on the declaration, and also make
2558 : : the old declaration the same definition. */
2559 : 3563227 : if (DECL_INITIAL (new_result) != NULL_TREE)
2560 : : {
2561 : 5402910 : DECL_SOURCE_LOCATION (olddecl)
2562 : 5402910 : = DECL_SOURCE_LOCATION (old_result)
2563 : 2701455 : = DECL_SOURCE_LOCATION (newdecl);
2564 : 2701455 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2565 : 2701455 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2566 : : {
2567 : 2701455 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2568 : 9654310 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2569 : 6952855 : DECL_CONTEXT (p) = old_result;
2570 : :
2571 : 5402910 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2572 : 14 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2573 : : }
2574 : : }
2575 : :
2576 : 3563227 : return olddecl;
2577 : : }
2578 : :
2579 : 15422321 : if (types_match)
2580 : : {
2581 : 15422120 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2582 : 15005224 : check_redeclaration_exception_specification (newdecl, olddecl);
2583 : :
2584 : : /* Automatically handles default parameters. */
2585 : 15422120 : tree oldtype = TREE_TYPE (olddecl);
2586 : 15422120 : tree newtype;
2587 : :
2588 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2589 : : at newdecl, which will be ggc_freed. */
2590 : 15422120 : if (TREE_CODE (newdecl) == TYPE_DECL)
2591 : : {
2592 : : /* But NEWTYPE might have an attribute, honor that. */
2593 : 25353 : tree tem = TREE_TYPE (newdecl);
2594 : 25353 : newtype = oldtype;
2595 : :
2596 : 25353 : if (TYPE_USER_ALIGN (tem))
2597 : : {
2598 : 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2599 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2600 : 21 : TYPE_USER_ALIGN (newtype) = true;
2601 : : }
2602 : :
2603 : : /* And remove the new type from the variants list. */
2604 : 25353 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2605 : : {
2606 : 24 : tree remove = TREE_TYPE (newdecl);
2607 : 24 : if (TYPE_MAIN_VARIANT (remove) == remove)
2608 : : {
2609 : 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2610 : : /* If remove is the main variant, no need to remove that
2611 : : from the list. One of the DECL_ORIGINAL_TYPE
2612 : : variants, e.g. created for aligned attribute, might still
2613 : : refer to the newdecl TYPE_DECL though, so remove that one
2614 : : in that case. */
2615 : 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2616 : 6 : if (orig != remove)
2617 : 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2618 : 0 : t = TYPE_MAIN_VARIANT (t))
2619 : 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2620 : : {
2621 : 12 : TYPE_NEXT_VARIANT (t)
2622 : 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2623 : 6 : break;
2624 : : }
2625 : : }
2626 : : else
2627 : 15 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2628 : 0 : t = TYPE_NEXT_VARIANT (t))
2629 : 15 : if (TYPE_NEXT_VARIANT (t) == remove)
2630 : : {
2631 : 15 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2632 : 15 : break;
2633 : : }
2634 : : }
2635 : : }
2636 : 15396767 : else if (merge_attr)
2637 : 14818779 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2638 : : else
2639 : 577988 : newtype = TREE_TYPE (newdecl);
2640 : :
2641 : 15422120 : if (VAR_P (newdecl))
2642 : : {
2643 : 391543 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2644 : : /* For already initialized vars, TREE_READONLY could have been
2645 : : cleared in cp_finish_decl, because the var needs runtime
2646 : : initialization or destruction. Make sure not to set
2647 : : TREE_READONLY on it again. */
2648 : 391543 : if (DECL_INITIALIZED_P (olddecl)
2649 : 1213 : && !DECL_EXTERNAL (olddecl)
2650 : 391628 : && !TREE_READONLY (olddecl))
2651 : 30 : TREE_READONLY (newdecl) = 0;
2652 : 391543 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2653 : 783086 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2654 : 391543 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2655 : 391543 : if (DECL_DEPENDENT_INIT_P (olddecl))
2656 : 104529 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2657 : 783086 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2658 : 391543 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2659 : 391543 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2660 : 391543 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2661 : 783086 : DECL_DECLARED_CONSTINIT_P (newdecl)
2662 : 391543 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2663 : :
2664 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2665 : 391543 : if (DECL_LANG_SPECIFIC (olddecl)
2666 : 779509 : && CP_DECL_THREADPRIVATE_P (olddecl))
2667 : : {
2668 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2669 : 41 : retrofit_lang_decl (newdecl);
2670 : 41 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2671 : : }
2672 : : }
2673 : :
2674 : : /* An explicit specialization of a function template or of a member
2675 : : function of a class template can be declared transaction_safe
2676 : : independently of whether the corresponding template entity is declared
2677 : : transaction_safe. */
2678 : 1145 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2679 : 1043 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2680 : 57 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2681 : 51 : && tx_safe_fn_type_p (newtype)
2682 : 15422120 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2683 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2684 : :
2685 : 15422120 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2686 : :
2687 : 15422120 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2688 : 15005224 : check_default_args (newdecl);
2689 : :
2690 : : /* Lay the type out, unless already done. */
2691 : 15422120 : if (! same_type_p (newtype, oldtype)
2692 : 366 : && TREE_TYPE (newdecl) != error_mark_node
2693 : 15422486 : && !(processing_template_decl && uses_template_parms (newdecl)))
2694 : 285 : layout_type (TREE_TYPE (newdecl));
2695 : :
2696 : 15422120 : if ((VAR_P (newdecl)
2697 : 15030577 : || TREE_CODE (newdecl) == PARM_DECL
2698 : 15030577 : || TREE_CODE (newdecl) == RESULT_DECL
2699 : 15030577 : || TREE_CODE (newdecl) == FIELD_DECL
2700 : 15030577 : || TREE_CODE (newdecl) == TYPE_DECL)
2701 : 15447473 : && !(processing_template_decl && uses_template_parms (newdecl)))
2702 : 256129 : layout_decl (newdecl, 0);
2703 : :
2704 : : /* Merge deprecatedness. */
2705 : 15422120 : if (TREE_DEPRECATED (newdecl))
2706 : 11850 : TREE_DEPRECATED (olddecl) = 1;
2707 : :
2708 : : /* Merge unavailability. */
2709 : 15422120 : if (TREE_UNAVAILABLE (newdecl))
2710 : 3 : TREE_UNAVAILABLE (olddecl) = 1;
2711 : :
2712 : : /* Preserve function specific target and optimization options */
2713 : 15422120 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2714 : : {
2715 : 15005224 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2716 : 15005224 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2717 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2718 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2719 : :
2720 : 15005224 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2721 : 15005224 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2722 : 55 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2723 : 55 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2724 : :
2725 : 15005224 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2726 : 14834176 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2727 : : }
2728 : : else
2729 : : {
2730 : : /* Merge the const type qualifier. */
2731 : 416896 : if (TREE_READONLY (newdecl))
2732 : 42519 : TREE_READONLY (olddecl) = 1;
2733 : : /* Merge the volatile type qualifier. */
2734 : 416896 : if (TREE_THIS_VOLATILE (newdecl))
2735 : 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2736 : : }
2737 : :
2738 : : /* Merge the initialization information. */
2739 : 15422120 : if (DECL_INITIAL (newdecl) == NULL_TREE
2740 : 15422120 : && DECL_INITIAL (olddecl) != NULL_TREE)
2741 : : {
2742 : 174661 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2743 : 174661 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2744 : 174661 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2745 : : {
2746 : 11639 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2747 : 11639 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2748 : : }
2749 : : }
2750 : :
2751 : 15422120 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2752 : : {
2753 : 15005224 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2754 : 15005224 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2755 : 15005224 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2756 : 15005224 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2757 : 53084 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2758 : 15005224 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2759 : 15005224 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2760 : 15005224 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2761 : 15005224 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2762 : :
2763 : 15005224 : if (merge_attr)
2764 : 14427236 : merge_attribute_bits (newdecl, olddecl);
2765 : : else
2766 : : {
2767 : : /* Merge the noreturn bit. */
2768 : 577988 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2769 : 577988 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2770 : 577988 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2771 : 577988 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2772 : 577988 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2773 : : }
2774 : : /* Keep the old RTL. */
2775 : 15005224 : COPY_DECL_RTL (olddecl, newdecl);
2776 : : }
2777 : 416896 : else if (VAR_P (newdecl)
2778 : 416896 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2779 : : {
2780 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2781 : : declaration was for an incomplete object and the new
2782 : : declaration is not since many attributes of the RTL will
2783 : : change. */
2784 : 391469 : COPY_DECL_RTL (olddecl, newdecl);
2785 : : }
2786 : : }
2787 : : /* If cannot merge, then use the new type and qualifiers,
2788 : : and don't preserve the old rtl. */
2789 : : else
2790 : : {
2791 : : /* Clean out any memory we had of the old declaration. */
2792 : 201 : tree oldstatic = value_member (olddecl, static_aggregates);
2793 : 201 : if (oldstatic)
2794 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2795 : :
2796 : 201 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2797 : 201 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2798 : 201 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2799 : 201 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2800 : 201 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2801 : : }
2802 : :
2803 : : /* Merge the storage class information. */
2804 : 15422321 : merge_weak (newdecl, olddecl);
2805 : :
2806 : 15422321 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2807 : 15422321 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2808 : 15422321 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2809 : 15422321 : if (! DECL_EXTERNAL (olddecl))
2810 : 29970 : DECL_EXTERNAL (newdecl) = 0;
2811 : 15422321 : if (! DECL_COMDAT (olddecl))
2812 : 6710230 : DECL_COMDAT (newdecl) = 0;
2813 : :
2814 : 15422321 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2815 : : {
2816 : 78 : if (!DECL_LOCAL_DECL_P (olddecl))
2817 : : /* This can happen if olddecl was brought in from the
2818 : : enclosing namespace via a using-decl. The new decl is
2819 : : then not a block-scope extern at all. */
2820 : 3 : DECL_LOCAL_DECL_P (newdecl) = false;
2821 : : else
2822 : : {
2823 : 75 : retrofit_lang_decl (newdecl);
2824 : 75 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2825 : 75 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2826 : 75 : if (alias != error_mark_node)
2827 : : {
2828 : 72 : DECL_ATTRIBUTES (alias)
2829 : 72 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2830 : 72 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2831 : 42 : merge_attribute_bits (newdecl, alias);
2832 : : }
2833 : : }
2834 : : }
2835 : :
2836 : 15422321 : new_template_info = NULL_TREE;
2837 : 15422321 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2838 : : {
2839 : 15393400 : bool new_redefines_gnu_inline = false;
2840 : :
2841 : 15393400 : if (new_defines_function
2842 : 15393400 : && ((DECL_INTERFACE_KNOWN (olddecl)
2843 : 1231 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2844 : 9001352 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2845 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2846 : : == FUNCTION_DECL))))
2847 : 2462 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2848 : :
2849 : : if (!new_redefines_gnu_inline)
2850 : : {
2851 : 15393322 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2852 : 15393322 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2853 : 15393322 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2854 : : }
2855 : :
2856 : 15393400 : if (TREE_CODE (newdecl) != TYPE_DECL)
2857 : : {
2858 : 15393348 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2859 : 15393348 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2860 : 15393348 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2861 : :
2862 : : /* If the OLDDECL is an instantiation and/or specialization,
2863 : : then the NEWDECL must be too. But, it may not yet be marked
2864 : : as such if the caller has created NEWDECL, but has not yet
2865 : : figured out that it is a redeclaration. */
2866 : 15393348 : if (!DECL_USE_TEMPLATE (newdecl))
2867 : 14792688 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2868 : :
2869 : 15393348 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2870 : 14815136 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2871 : 29630272 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2872 : : }
2873 : :
2874 : : /* Don't really know how much of the language-specific
2875 : : values we should copy from old to new. */
2876 : 15393400 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2877 : :
2878 : 15393400 : if (LANG_DECL_HAS_MIN (newdecl))
2879 : : {
2880 : 15393400 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2881 : 15393400 : if (DECL_TEMPLATE_INFO (newdecl))
2882 : : {
2883 : 612184 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
2884 : 612184 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2885 : 612184 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2886 : : /* Remember the presence of explicit specialization args. */
2887 : 1155982 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2888 : 1155982 : = TINFO_USED_TEMPLATE_ID (new_template_info);
2889 : : }
2890 : :
2891 : : /* We don't want to copy template info from a non-templated friend
2892 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2893 : 15393400 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2894 : : || !non_templated_friend_p (olddecl));
2895 : 15393400 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2896 : : }
2897 : :
2898 : 15393400 : if (DECL_DECLARES_FUNCTION_P (newdecl))
2899 : : {
2900 : : /* Only functions have these fields. */
2901 : 15005425 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2902 : 15005425 : DECL_BEFRIENDING_CLASSES (newdecl)
2903 : 15005425 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2904 : 15005425 : DECL_BEFRIENDING_CLASSES (olddecl));
2905 : : /* DECL_THUNKS is only valid for virtual functions,
2906 : : otherwise it is a DECL_FRIEND_CONTEXT. */
2907 : 15005425 : if (DECL_VIRTUAL_P (newdecl))
2908 : 650427 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2909 : 28709996 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2910 : 102897 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2911 : : }
2912 : 387975 : else if (VAR_P (newdecl))
2913 : : {
2914 : : /* Only variables have this field. */
2915 : 387923 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2916 : 194 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2917 : : }
2918 : : }
2919 : :
2920 : 15422321 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2921 : : {
2922 : 15005425 : tree parm;
2923 : :
2924 : : /* Merge parameter attributes. */
2925 : 15005425 : tree oldarg, newarg;
2926 : 15005425 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2927 : 41986374 : oldarg && newarg;
2928 : 26980949 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2929 : : {
2930 : 26980949 : DECL_ATTRIBUTES (newarg)
2931 : 26980949 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
2932 : 26980949 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2933 : : }
2934 : :
2935 : 15005425 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2936 : 15005425 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2937 : : {
2938 : : /* If newdecl is not a specialization, then it is not a
2939 : : template-related function at all. And that means that we
2940 : : should have exited above, returning 0. */
2941 : 577988 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2942 : :
2943 : 577988 : if (DECL_ODR_USED (olddecl))
2944 : : /* From [temp.expl.spec]:
2945 : :
2946 : : If a template, a member template or the member of a class
2947 : : template is explicitly specialized then that
2948 : : specialization shall be declared before the first use of
2949 : : that specialization that would cause an implicit
2950 : : instantiation to take place, in every translation unit in
2951 : : which such a use occurs. */
2952 : 0 : error ("explicit specialization of %qD after first use",
2953 : : olddecl);
2954 : :
2955 : 577988 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2956 : 577988 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2957 : 1155823 : && DECL_DECLARED_INLINE_P (newdecl));
2958 : :
2959 : : /* Don't propagate visibility from the template to the
2960 : : specialization here. We'll do that in determine_visibility if
2961 : : appropriate. */
2962 : 577988 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2963 : :
2964 : : /* [temp.expl.spec/14] We don't inline explicit specialization
2965 : : just because the primary template says so. */
2966 : 577988 : gcc_assert (!merge_attr);
2967 : :
2968 : 577988 : DECL_DECLARED_INLINE_P (olddecl)
2969 : 577988 : = DECL_DECLARED_INLINE_P (newdecl);
2970 : :
2971 : 577988 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
2972 : 577988 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2973 : :
2974 : 577988 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2975 : : }
2976 : 14427437 : else if (new_defines_function && DECL_INITIAL (olddecl))
2977 : : {
2978 : : /* Never inline re-defined extern inline functions.
2979 : : FIXME: this could be better handled by keeping both
2980 : : function as separate declarations. */
2981 : 78 : DECL_UNINLINABLE (newdecl) = 1;
2982 : : }
2983 : : else
2984 : : {
2985 : 14427359 : if (DECL_PENDING_INLINE_P (olddecl))
2986 : : {
2987 : 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
2988 : 6 : DECL_PENDING_INLINE_INFO (newdecl)
2989 : 12 : = DECL_PENDING_INLINE_INFO (olddecl);
2990 : : }
2991 : 14427353 : else if (DECL_PENDING_INLINE_P (newdecl))
2992 : : ;
2993 : 14427353 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2994 : 14427353 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2995 : 28854706 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2996 : :
2997 : 14427359 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2998 : :
2999 : 43282077 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3000 : 28854718 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3001 : :
3002 : 28854718 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
3003 : 14427359 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
3004 : 14427359 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
3005 : 28458060 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
3006 : : }
3007 : :
3008 : : /* Preserve abstractness on cloned [cd]tors. */
3009 : 15005425 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
3010 : :
3011 : : /* Update newdecl's parms to point at olddecl. */
3012 : 47924841 : for (parm = DECL_ARGUMENTS (newdecl); parm;
3013 : 32919416 : parm = DECL_CHAIN (parm))
3014 : 32919416 : DECL_CONTEXT (parm) = olddecl;
3015 : :
3016 : 15005425 : if (! types_match)
3017 : : {
3018 : 201 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3019 : 201 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3020 : 201 : COPY_DECL_RTL (newdecl, olddecl);
3021 : : }
3022 : 15005425 : if (! types_match || new_defines_function)
3023 : : {
3024 : : /* These are the final DECL_ARGUMENTS that will be used within the
3025 : : body; update any references to old DECL_ARGUMENTS in the
3026 : : contracts, if present. */
3027 : 9002784 : if (tree contracts = DECL_CONTRACTS (newdecl))
3028 : 132 : remap_contracts (olddecl, newdecl, contracts, true);
3029 : :
3030 : : /* These need to be copied so that the names are available.
3031 : : Note that if the types do match, we'll preserve inline
3032 : : info and other bits, but if not, we won't. */
3033 : 9002784 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3034 : 9002784 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3035 : :
3036 : : /* In some cases, duplicate_contracts will remove contracts from
3037 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
3038 : : shared. If we removed them, re-add them. */
3039 : 9002784 : if (!DECL_CONTRACTS (olddecl))
3040 : 9002737 : copy_contract_attributes (olddecl, newdecl);
3041 : : }
3042 : : /* If redeclaring a builtin function, it stays built in
3043 : : if newdecl is a gnu_inline definition, or if newdecl is just
3044 : : a declaration. */
3045 : 15005425 : if (fndecl_built_in_p (olddecl)
3046 : 15063551 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3047 : : {
3048 : 4113490 : copy_decl_built_in_function (newdecl, olddecl);
3049 : : /* If we're keeping the built-in definition, keep the rtl,
3050 : : regardless of declaration matches. */
3051 : 4113490 : COPY_DECL_RTL (olddecl, newdecl);
3052 : 4113490 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3053 : : {
3054 : 4113484 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3055 : 4113484 : if (builtin_decl_explicit_p (fncode))
3056 : : {
3057 : : /* A compatible prototype of these builtin functions
3058 : : is seen, assume the runtime implements it with
3059 : : the expected semantics. */
3060 : 4113484 : switch (fncode)
3061 : : {
3062 : 10404 : case BUILT_IN_STPCPY:
3063 : 10404 : set_builtin_decl_implicit_p (fncode, true);
3064 : 10404 : break;
3065 : 4103080 : default:
3066 : 4103080 : set_builtin_decl_declared_p (fncode, true);
3067 : 4103080 : break;
3068 : : }
3069 : : }
3070 : :
3071 : 4113484 : copy_attributes_to_builtin (newdecl);
3072 : : }
3073 : : }
3074 : 15005425 : if (new_defines_function)
3075 : : /* If defining a function declared with other language
3076 : : linkage, use the previously declared language linkage. */
3077 : 9002583 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3078 : 6002842 : else if (types_match)
3079 : : {
3080 : 6002641 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3081 : : /* Don't clear out the arguments if we're just redeclaring a
3082 : : function. */
3083 : 6002641 : if (DECL_ARGUMENTS (olddecl))
3084 : : {
3085 : : /* If we removed contracts from previous definition, re-attach
3086 : : them. Otherwise, rewrite the contracts so they match the
3087 : : parameters of the new declaration. */
3088 : 1914667 : if (DECL_INITIAL (olddecl)
3089 : 11532 : && DECL_CONTRACTS (newdecl)
3090 : 1914678 : && !DECL_CONTRACTS (olddecl))
3091 : 10 : copy_contract_attributes (olddecl, newdecl);
3092 : : else
3093 : : {
3094 : : /* Temporarily undo the re-contexting of parameters so we can
3095 : : actually remap parameters. The inliner won't replace
3096 : : parameters if we don't do this. */
3097 : 1914657 : tree args = DECL_ARGUMENTS (newdecl);
3098 : 6158139 : for (tree p = args; p; p = DECL_CHAIN (p))
3099 : 4243482 : DECL_CONTEXT (p) = newdecl;
3100 : :
3101 : : /* Save new argument names for use in contracts parsing,
3102 : : unless we've already started parsing the body of olddecl
3103 : : (particular issues arise when newdecl is from a prior
3104 : : friend decl with no argument names, see
3105 : : modules/contracts-tpl-friend-1). */
3106 : 1914657 : if (tree contracts = DECL_CONTRACTS (olddecl))
3107 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3108 : :
3109 : : /* And reverse this operation again. */
3110 : 6158139 : for (tree p = args; p; p = DECL_CHAIN (p))
3111 : 4243482 : DECL_CONTEXT (p) = olddecl;
3112 : : }
3113 : :
3114 : 1914667 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3115 : : }
3116 : : }
3117 : : }
3118 : 416896 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3119 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3120 : :
3121 : : /* Now preserve various other info from the definition. */
3122 : 15422321 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3123 : 15422321 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3124 : 15422321 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3125 : 15422321 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3126 : :
3127 : : /* Warn about conflicting visibility specifications. */
3128 : 15422321 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3129 : 5422902 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3130 : 15422860 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3131 : : {
3132 : 12 : auto_diagnostic_group d;
3133 : 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3134 : : "%qD: visibility attribute ignored because it "
3135 : : "conflicts with previous declaration", newdecl))
3136 : 12 : inform (olddecl_loc,
3137 : : "previous declaration of %qD", olddecl);
3138 : 12 : }
3139 : : /* Choose the declaration which specified visibility. */
3140 : 15422321 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3141 : : {
3142 : 5422902 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3143 : 5422902 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3144 : : }
3145 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3146 : : so keep this behavior. */
3147 : 15422321 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3148 : : {
3149 : 10 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3150 : 10 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3151 : : }
3152 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3153 : 15422321 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3154 : : {
3155 : 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3156 : 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3157 : : }
3158 : 15422267 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3159 : 15422267 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3160 : 3 : DECL_USER_ALIGN (newdecl) = 1;
3161 : :
3162 : 15422321 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3163 : 30844642 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3164 : 15422321 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3165 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3166 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3167 : 15422321 : if (TREE_CODE (newdecl) == FIELD_DECL)
3168 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3169 : :
3170 : : /* Merge module entity mapping information. */
3171 : 15422321 : if (DECL_LANG_SPECIFIC (olddecl)
3172 : 30815764 : && (DECL_MODULE_ENTITY_P (olddecl)
3173 : 15393167 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3174 : : {
3175 : 276 : retrofit_lang_decl (newdecl);
3176 : 276 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3177 : 276 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3178 : : }
3179 : :
3180 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3181 : : with that from NEWDECL below. */
3182 : 15422321 : if (DECL_LANG_SPECIFIC (olddecl))
3183 : : {
3184 : 15393443 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3185 : : != DECL_LANG_SPECIFIC (newdecl));
3186 : 15393443 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3187 : : }
3188 : :
3189 : : /* Merge the USED information. */
3190 : 15422321 : if (TREE_USED (olddecl))
3191 : 486677 : TREE_USED (newdecl) = 1;
3192 : 14935644 : else if (TREE_USED (newdecl))
3193 : 54 : TREE_USED (olddecl) = 1;
3194 : :
3195 : 15422321 : if (VAR_P (newdecl))
3196 : : {
3197 : 391543 : if (DECL_READ_P (olddecl))
3198 : 35231 : DECL_READ_P (newdecl) = 1;
3199 : 356312 : else if (DECL_READ_P (newdecl))
3200 : 3 : DECL_READ_P (olddecl) = 1;
3201 : : }
3202 : :
3203 : 15422321 : if (DECL_PRESERVE_P (olddecl))
3204 : 48 : DECL_PRESERVE_P (newdecl) = 1;
3205 : 15422273 : else if (DECL_PRESERVE_P (newdecl))
3206 : 63 : DECL_PRESERVE_P (olddecl) = 1;
3207 : :
3208 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3209 : : to olddecl and deleted. */
3210 : 15422321 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3211 : 30427746 : && DECL_FUNCTION_VERSIONED (olddecl))
3212 : : {
3213 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3214 : 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3215 : : /* newdecl will be purged after copying to olddecl and is no longer
3216 : : a version. */
3217 : 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3218 : : }
3219 : :
3220 : 15422321 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3221 : : {
3222 : 15005425 : int function_size;
3223 : 15005425 : struct symtab_node *snode = symtab_node::get (olddecl);
3224 : :
3225 : 15005425 : function_size = sizeof (struct tree_decl_common);
3226 : :
3227 : 15005425 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3228 : : (char *) newdecl + sizeof (struct tree_common),
3229 : : function_size - sizeof (struct tree_common));
3230 : :
3231 : 15005425 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3232 : : (char *) newdecl + sizeof (struct tree_decl_common),
3233 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3234 : :
3235 : : /* Preserve symtab node mapping. */
3236 : 15005425 : olddecl->decl_with_vis.symtab_node = snode;
3237 : :
3238 : 15005425 : if (new_template_info)
3239 : : /* If newdecl is a template instantiation, it is possible that
3240 : : the following sequence of events has occurred:
3241 : :
3242 : : o A friend function was declared in a class template. The
3243 : : class template was instantiated.
3244 : :
3245 : : o The instantiation of the friend declaration was
3246 : : recorded on the instantiation list, and is newdecl.
3247 : :
3248 : : o Later, however, instantiate_class_template called pushdecl
3249 : : on the newdecl to perform name injection. But, pushdecl in
3250 : : turn called duplicate_decls when it discovered that another
3251 : : declaration of a global function with the same name already
3252 : : existed.
3253 : :
3254 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3255 : :
3256 : : If we're going to do that, we'd better make sure that
3257 : : olddecl, and not newdecl, is on the list of
3258 : : instantiations so that if we try to do the instantiation
3259 : : again we won't get the clobbered declaration. */
3260 : 612175 : reregister_specialization (newdecl,
3261 : : new_template_info,
3262 : : olddecl);
3263 : : }
3264 : : else
3265 : : {
3266 : 416896 : size_t size = tree_code_size (TREE_CODE (newdecl));
3267 : :
3268 : 416896 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3269 : : (char *) newdecl + sizeof (struct tree_common),
3270 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3271 : :
3272 : 416896 : switch (TREE_CODE (newdecl))
3273 : : {
3274 : 416896 : case LABEL_DECL:
3275 : 416896 : case VAR_DECL:
3276 : 416896 : case RESULT_DECL:
3277 : 416896 : case PARM_DECL:
3278 : 416896 : case FIELD_DECL:
3279 : 416896 : case TYPE_DECL:
3280 : 416896 : case CONST_DECL:
3281 : 416896 : {
3282 : 416896 : struct symtab_node *snode = NULL;
3283 : :
3284 : 416896 : if (VAR_P (newdecl)
3285 : 416896 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3286 : 3 : || DECL_EXTERNAL (olddecl)))
3287 : 391543 : snode = symtab_node::get (olddecl);
3288 : 416896 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3289 : : (char *) newdecl + sizeof (struct tree_decl_common),
3290 : : size - sizeof (struct tree_decl_common)
3291 : 416896 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3292 : 416896 : if (VAR_P (newdecl))
3293 : 391543 : olddecl->decl_with_vis.symtab_node = snode;
3294 : : }
3295 : : break;
3296 : 0 : default:
3297 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3298 : : (char *) newdecl + sizeof (struct tree_decl_common),
3299 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3300 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3301 : 0 : break;
3302 : : }
3303 : : }
3304 : :
3305 : 15422321 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3306 : : {
3307 : 15396968 : if (DECL_EXTERNAL (olddecl)
3308 : 392906 : || TREE_PUBLIC (olddecl)
3309 : 15397037 : || TREE_STATIC (olddecl))
3310 : : {
3311 : : /* Merge the section attribute.
3312 : : We want to issue an error if the sections conflict but that must be
3313 : : done later in decl_attributes since we are called before attributes
3314 : : are assigned. */
3315 : 15396968 : if (DECL_SECTION_NAME (newdecl) != NULL)
3316 : 7 : set_decl_section_name (olddecl, newdecl);
3317 : :
3318 : 15396968 : if (DECL_ONE_ONLY (newdecl))
3319 : : {
3320 : 0 : struct symtab_node *oldsym, *newsym;
3321 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3322 : 0 : oldsym = cgraph_node::get_create (olddecl);
3323 : : else
3324 : 0 : oldsym = varpool_node::get_create (olddecl);
3325 : 0 : newsym = symtab_node::get (newdecl);
3326 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3327 : : }
3328 : : }
3329 : :
3330 : 15396968 : if (VAR_P (newdecl)
3331 : 15396968 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3332 : : {
3333 : 398 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3334 : 398 : if (!processing_template_decl)
3335 : 371 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3336 : : }
3337 : : }
3338 : :
3339 : 15422321 : DECL_UID (olddecl) = olddecl_uid;
3340 : :
3341 : : /* NEWDECL contains the merged attribute lists.
3342 : : Update OLDDECL to be the same. */
3343 : 15422321 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3344 : :
3345 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3346 : : so that encode_section_info has a chance to look at the new decl
3347 : : flags and attributes. */
3348 : 15422321 : if (DECL_RTL_SET_P (olddecl)
3349 : 15422321 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3350 : 0 : || (VAR_P (olddecl)
3351 : 0 : && TREE_STATIC (olddecl))))
3352 : 0 : make_decl_rtl (olddecl);
3353 : :
3354 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3355 : : declaration of a member results in a call to duplicate_decls,
3356 : : freeing these nodes represents in a significant savings.
3357 : :
3358 : : Before releasing the node, be sore to remove function from symbol
3359 : : table that might have been inserted there to record comdat group.
3360 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3361 : : structure is shared in between newdecl and oldecl. */
3362 : 15422321 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3363 : 15005425 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3364 : 15422321 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3365 : : {
3366 : 15396968 : struct symtab_node *snode = symtab_node::get (newdecl);
3367 : 15396968 : if (snode)
3368 : 399 : snode->remove ();
3369 : : }
3370 : :
3371 : 15422321 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3372 : : {
3373 : 15005425 : tree clone;
3374 : 15519129 : FOR_EACH_CLONE (clone, olddecl)
3375 : : {
3376 : 513704 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3377 : 513704 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3378 : : }
3379 : : }
3380 : :
3381 : : /* Remove the associated constraints for newdecl, if any, before
3382 : : reclaiming memory. */
3383 : 15422321 : if (flag_concepts)
3384 : 3803940 : remove_constraints (newdecl);
3385 : :
3386 : : /* And similarly for any module tracking data. */
3387 : 15422321 : if (modules_p ())
3388 : 29768 : remove_defining_module (newdecl);
3389 : :
3390 : 15422321 : ggc_free (newdecl);
3391 : :
3392 : 15422321 : return olddecl;
3393 : : }
3394 : :
3395 : : /* Return zero if the declaration NEWDECL is valid
3396 : : when the declaration OLDDECL (assumed to be for the same name)
3397 : : has already been seen.
3398 : : Otherwise return an error message format string with a %s
3399 : : where the identifier should go. */
3400 : :
3401 : : static const char *
3402 : 15001859 : redeclaration_error_message (tree newdecl, tree olddecl)
3403 : : {
3404 : 15001895 : if (TREE_CODE (newdecl) == TYPE_DECL)
3405 : : {
3406 : : /* Because C++ can put things into name space for free,
3407 : : constructs like "typedef struct foo { ... } foo"
3408 : : would look like an erroneous redeclaration. */
3409 : 56539 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3410 : : return NULL;
3411 : : else
3412 : : return G_("redefinition of %q#D");
3413 : : }
3414 : 14945356 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3415 : : {
3416 : : /* If this is a pure function, its olddecl will actually be
3417 : : the original initialization to `0' (which we force to call
3418 : : abort()). Don't complain about redefinition in this case. */
3419 : 21980388 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3420 : 10990212 : && DECL_INITIAL (olddecl) == NULL_TREE)
3421 : : return NULL;
3422 : :
3423 : : /* If both functions come from different namespaces, this is not
3424 : : a redeclaration - this is a conflict with a used function. */
3425 : 21980358 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3426 : 2269183 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3427 : 10990182 : && ! decls_match (olddecl, newdecl))
3428 : : return G_("%qD conflicts with used function");
3429 : :
3430 : : /* We'll complain about linkage mismatches in
3431 : : warn_extern_redeclared_static. */
3432 : :
3433 : : /* Defining the same name twice is no good. */
3434 : 10990179 : if (decl_defined_p (olddecl)
3435 : 10990179 : && decl_defined_p (newdecl))
3436 : : {
3437 : 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3438 : : return G_("%q#D not declared in class");
3439 : 444 : else if (!GNU_INLINE_P (olddecl)
3440 : 390 : || GNU_INLINE_P (newdecl))
3441 : 219 : return G_("redefinition of %q#D");
3442 : : }
3443 : :
3444 : 11721602 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3445 : : {
3446 : 685500 : bool olda = GNU_INLINE_P (olddecl);
3447 : 685500 : bool newa = GNU_INLINE_P (newdecl);
3448 : :
3449 : 685500 : if (olda != newa)
3450 : : {
3451 : 15 : if (newa)
3452 : : return G_("%q+D redeclared inline with "
3453 : : "%<gnu_inline%> attribute");
3454 : : else
3455 : : return G_("%q+D redeclared inline without "
3456 : : "%<gnu_inline%> attribute");
3457 : : }
3458 : : }
3459 : :
3460 : : /* [class.compare.default]: A definition of a comparison operator as
3461 : : defaulted that appears in a class shall be the first declaration of
3462 : : that function. */
3463 : 10989945 : special_function_kind sfk = special_function_p (olddecl);
3464 : 10989945 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3465 : : return G_("comparison operator %q+D defaulted after "
3466 : : "its first declaration");
3467 : :
3468 : 10989942 : check_abi_tag_redeclaration
3469 : 10989942 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3470 : 10989942 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3471 : :
3472 : 10989942 : return NULL;
3473 : : }
3474 : 3955162 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3475 : : {
3476 : 3563266 : tree nt, ot;
3477 : :
3478 : 3563266 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3479 : : return G_("redefinition of %q#D");
3480 : :
3481 : 3563260 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3482 : 36 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3483 : 36 : DECL_TEMPLATE_RESULT (olddecl));
3484 : :
3485 : 3563224 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3486 : : return NULL;
3487 : :
3488 : 3563212 : nt = DECL_TEMPLATE_RESULT (newdecl);
3489 : 3563212 : if (DECL_TEMPLATE_INFO (nt))
3490 : 534487 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3491 : 3563212 : ot = DECL_TEMPLATE_RESULT (olddecl);
3492 : 3563212 : if (DECL_TEMPLATE_INFO (ot))
3493 : 3563212 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3494 : 6264718 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3495 : 3563251 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3496 : 15 : return G_("redefinition of %q#D");
3497 : :
3498 : 4872661 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3499 : : {
3500 : 834193 : bool olda = GNU_INLINE_P (ot);
3501 : 834193 : bool newa = GNU_INLINE_P (nt);
3502 : :
3503 : 834193 : if (olda != newa)
3504 : : {
3505 : 0 : if (newa)
3506 : : return G_("%q+D redeclared inline with "
3507 : : "%<gnu_inline%> attribute");
3508 : : else
3509 : : return G_("%q+D redeclared inline without "
3510 : : "%<gnu_inline%> attribute");
3511 : : }
3512 : : }
3513 : :
3514 : : /* Core issue #226 (C++11):
3515 : :
3516 : : If a friend function template declaration specifies a
3517 : : default template-argument, that declaration shall be a
3518 : : definition and shall be the only declaration of the
3519 : : function template in the translation unit. */
3520 : 3563197 : if ((cxx_dialect != cxx98)
3521 : 3547246 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3522 : 3777681 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3523 : : /*is_primary=*/true,
3524 : : /*is_partial=*/false,
3525 : : /*is_friend_decl=*/2))
3526 : : return G_("redeclaration of friend %q#D "
3527 : : "may not have default template arguments");
3528 : :
3529 : 3563194 : return NULL;
3530 : : }
3531 : 391896 : else if (VAR_P (newdecl)
3532 : 391812 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3533 : 391812 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3534 : 391943 : && (! DECL_LANG_SPECIFIC (olddecl)
3535 : 44 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3536 : 41 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3537 : : {
3538 : : /* Only variables can be thread-local, and all declarations must
3539 : : agree on this property. */
3540 : 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3541 : : return G_("thread-local declaration of %q#D follows "
3542 : : "non-thread-local declaration");
3543 : : else
3544 : 3 : return G_("non-thread-local declaration of %q#D follows "
3545 : : "thread-local declaration");
3546 : : }
3547 : 391890 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3548 : : {
3549 : : /* The objects have been declared at namespace scope. If either
3550 : : is a member of an anonymous union, then this is an invalid
3551 : : redeclaration. For example:
3552 : :
3553 : : int i;
3554 : : union { int i; };
3555 : :
3556 : : is invalid. */
3557 : 391670 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3558 : 783409 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3559 : : return G_("redeclaration of %q#D");
3560 : : /* If at least one declaration is a reference, there is no
3561 : : conflict. For example:
3562 : :
3563 : : int i = 3;
3564 : : extern int i;
3565 : :
3566 : : is valid. */
3567 : 391736 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3568 : : return NULL;
3569 : :
3570 : : /* Static data member declared outside a class definition
3571 : : if the variable is defined within the class with constexpr
3572 : : specifier is declaration rather than definition (and
3573 : : deprecated). */
3574 : 2824 : if (cxx_dialect >= cxx17
3575 : 2749 : && VAR_P (olddecl)
3576 : 2693 : && DECL_CLASS_SCOPE_P (olddecl)
3577 : 2649 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3578 : 5461 : && !DECL_INITIAL (newdecl))
3579 : : {
3580 : 2637 : DECL_EXTERNAL (newdecl) = 1;
3581 : : /* For now, only warn with explicit -Wdeprecated. */
3582 : 2637 : if (OPTION_SET_P (warn_deprecated))
3583 : : {
3584 : 40 : auto_diagnostic_group d;
3585 : 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3586 : : "redundant redeclaration of %<constexpr%> "
3587 : : "static data member %qD", newdecl))
3588 : 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3589 : : "previous declaration of %qD", olddecl);
3590 : 40 : }
3591 : 2637 : return NULL;
3592 : : }
3593 : :
3594 : : /* Reject two definitions. */
3595 : : return G_("redefinition of %q#D");
3596 : : }
3597 : : else
3598 : : {
3599 : : /* Objects declared with block scope: */
3600 : : /* Reject two definitions, and reject a definition
3601 : : together with an external reference. */
3602 : 139 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3603 : : return G_("redeclaration of %q#D");
3604 : : return NULL;
3605 : : }
3606 : : }
3607 : :
3608 : :
3609 : : /* Hash and equality functions for the named_label table. */
3610 : :
3611 : : hashval_t
3612 : 74171 : named_label_hash::hash (const value_type entry)
3613 : : {
3614 : 74171 : return IDENTIFIER_HASH_VALUE (entry->name);
3615 : : }
3616 : :
3617 : : bool
3618 : 71227 : named_label_hash::equal (const value_type entry, compare_type name)
3619 : : {
3620 : 71227 : return name == entry->name;
3621 : : }
3622 : :
3623 : : /* Look for a label named ID in the current function. If one cannot
3624 : : be found, create one. Return the named_label_entry, or NULL on
3625 : : failure. */
3626 : :
3627 : : static named_label_entry *
3628 : 23206 : lookup_label_1 (tree id, bool making_local_p)
3629 : : {
3630 : 23206 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3631 : :
3632 : : /* You can't use labels at global scope. */
3633 : 23206 : if (current_function_decl == NULL_TREE)
3634 : : {
3635 : 3 : error ("label %qE referenced outside of any function", id);
3636 : 3 : return NULL;
3637 : : }
3638 : :
3639 : 23203 : if (!named_labels)
3640 : 4564 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3641 : :
3642 : 23203 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3643 : 23203 : named_label_entry **slot
3644 : 23203 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3645 : 23203 : named_label_entry *old = *slot;
3646 : :
3647 : 23203 : if (old && old->label_decl)
3648 : : {
3649 : 2936 : if (!making_local_p)
3650 : : return old;
3651 : :
3652 : 9 : if (old->binding_level == current_binding_level)
3653 : : {
3654 : 0 : auto_diagnostic_group d;
3655 : 0 : error ("local label %qE conflicts with existing label", id);
3656 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3657 : 0 : return NULL;
3658 : 0 : }
3659 : : }
3660 : :
3661 : : /* We are making a new decl, create or reuse the named_label_entry */
3662 : 20285 : named_label_entry *ent = NULL;
3663 : 21 : if (old && !old->label_decl)
3664 : : ent = old;
3665 : : else
3666 : : {
3667 : 20264 : ent = ggc_cleared_alloc<named_label_entry> ();
3668 : 20264 : ent->name = id;
3669 : 20264 : ent->outer = old;
3670 : 20264 : *slot = ent;
3671 : : }
3672 : :
3673 : : /* Now create the LABEL_DECL. */
3674 : 20276 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3675 : :
3676 : 20276 : DECL_CONTEXT (decl) = current_function_decl;
3677 : 20276 : SET_DECL_MODE (decl, VOIDmode);
3678 : 20276 : if (making_local_p)
3679 : : {
3680 : 207 : C_DECLARED_LABEL_FLAG (decl) = true;
3681 : 207 : DECL_CHAIN (decl) = current_binding_level->names;
3682 : 207 : current_binding_level->names = decl;
3683 : : }
3684 : :
3685 : 20276 : ent->label_decl = decl;
3686 : :
3687 : 20276 : return ent;
3688 : 23206 : }
3689 : :
3690 : : /* Wrapper for lookup_label_1. */
3691 : :
3692 : : tree
3693 : 2476 : lookup_label (tree id)
3694 : : {
3695 : 2476 : named_label_entry *ent = lookup_label_1 (id, false);
3696 : 2476 : return ent ? ent->label_decl : NULL_TREE;
3697 : : }
3698 : :
3699 : : /* Remember that we've seen &&ID. */
3700 : :
3701 : : void
3702 : 250 : mark_label_addressed (tree id)
3703 : : {
3704 : 250 : named_label_entry *ent = lookup_label_1 (id, false);
3705 : 250 : ent->addressed = true;
3706 : 250 : }
3707 : :
3708 : : tree
3709 : 207 : declare_local_label (tree id)
3710 : : {
3711 : 207 : named_label_entry *ent = lookup_label_1 (id, true);
3712 : 207 : return ent ? ent->label_decl : NULL_TREE;
3713 : : }
3714 : :
3715 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3716 : :
3717 : : static bool
3718 : 136354 : decl_jump_unsafe (tree decl)
3719 : : {
3720 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3721 : : with automatic storage duration is not in scope to a point where it is
3722 : : in scope is ill-formed unless the variable has scalar type, class type
3723 : : with a trivial default constructor and a trivial destructor, a
3724 : : cv-qualified version of one of these types, or an array of one of the
3725 : : preceding types and is declared without an initializer (8.5). */
3726 : 136354 : tree type = TREE_TYPE (decl);
3727 : :
3728 : 136354 : return (type != error_mark_node
3729 : 136336 : && VAR_P (decl)
3730 : 12862 : && !TREE_STATIC (decl)
3731 : 149080 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3732 : 10909 : || variably_modified_type_p (type, NULL_TREE)));
3733 : : }
3734 : :
3735 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3736 : : to the user. */
3737 : :
3738 : : static bool
3739 : 286 : identify_goto (tree decl, location_t loc, const location_t *locus,
3740 : : enum diagnostics::kind diag_kind, bool computed)
3741 : : {
3742 : 286 : if (computed)
3743 : 43 : diag_kind = diagnostics::kind::warning;
3744 : 286 : bool complained
3745 : 373 : = emit_diagnostic (diag_kind, loc, 0,
3746 : : decl ? G_("jump to label %qD")
3747 : : : G_("jump to case label"), decl);
3748 : 286 : if (complained && locus)
3749 : : {
3750 : 199 : if (computed)
3751 : 43 : inform (*locus, " as a possible target of computed goto");
3752 : : else
3753 : 156 : inform (*locus, " from here");
3754 : : }
3755 : 286 : return complained;
3756 : : }
3757 : :
3758 : : /* Check that a single previously seen jump to a newly defined label
3759 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3760 : : the jump context; NAMES are the names in scope in LEVEL at the jump
3761 : : context; LOCUS is the source position of the jump or 0. COMPUTED
3762 : : is a vec of decls if the jump is a computed goto. Returns
3763 : : true if all is well. */
3764 : :
3765 : : static bool
3766 : 1985537 : check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3767 : : bool exited_omp, const location_t *locus,
3768 : : vec<tree,va_gc> *computed)
3769 : : {
3770 : 1985537 : auto_diagnostic_group d;
3771 : 1985537 : cp_binding_level *b;
3772 : 1985537 : bool complained = false;
3773 : 1985537 : int identified = 0;
3774 : 1985537 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3775 : 1985537 : bool saw_ceif = false, saw_se = false;
3776 : :
3777 : 1985537 : if (exited_omp)
3778 : : {
3779 : 12 : complained = identify_goto (decl, input_location, locus,
3780 : : diagnostics::kind::error,
3781 : : computed);
3782 : 12 : if (complained)
3783 : 12 : inform (input_location, " exits OpenMP structured block");
3784 : : saw_omp = true;
3785 : : identified = 2;
3786 : : }
3787 : :
3788 : 5928085 : for (b = current_binding_level; b ; b = b->level_chain)
3789 : : {
3790 : 3942548 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3791 : :
3792 : 4207124 : for (new_decls = b->names; new_decls != old_decls;
3793 : 132288 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3794 : 122707 : : TREE_CHAIN (new_decls)))
3795 : : {
3796 : 132288 : bool problem = decl_jump_unsafe (new_decls);
3797 : 132288 : if (! problem)
3798 : 132243 : continue;
3799 : :
3800 : 45 : if (!identified)
3801 : : {
3802 : 45 : complained = identify_goto (decl, input_location, locus,
3803 : : diagnostics::kind::error,
3804 : : computed);
3805 : 45 : identified = 2;
3806 : : }
3807 : 45 : if (complained)
3808 : 45 : inform (DECL_SOURCE_LOCATION (new_decls),
3809 : : " crosses initialization of %q#D", new_decls);
3810 : : }
3811 : :
3812 : 3942548 : if (b == level)
3813 : : break;
3814 : :
3815 : 1957011 : const char *inf = NULL;
3816 : 1957011 : location_t loc = input_location;
3817 : 1957011 : switch (b->kind)
3818 : : {
3819 : 3 : case sk_try:
3820 : 3 : if (!saw_eh)
3821 : : inf = G_(" enters %<try%> block");
3822 : : saw_eh = true;
3823 : : break;
3824 : :
3825 : 9 : case sk_catch:
3826 : 9 : if (!saw_eh)
3827 : : inf = G_(" enters %<catch%> block");
3828 : : saw_eh = true;
3829 : : break;
3830 : :
3831 : 102 : case sk_omp:
3832 : 102 : if (!saw_omp)
3833 : : inf = G_(" enters OpenMP structured block");
3834 : : saw_omp = true;
3835 : : break;
3836 : :
3837 : 3 : case sk_transaction:
3838 : 3 : if (!saw_tm)
3839 : : inf = G_(" enters synchronized or atomic statement");
3840 : : saw_tm = true;
3841 : : break;
3842 : :
3843 : 18 : case sk_stmt_expr:
3844 : 18 : if (!saw_se)
3845 : : inf = G_(" enters statement expression");
3846 : : saw_se = true;
3847 : : break;
3848 : :
3849 : 1956772 : case sk_block:
3850 : 1956772 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3851 : : {
3852 : 9 : inf = G_(" enters %<constexpr if%> statement");
3853 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3854 : : saw_cxif = true;
3855 : : }
3856 : 1956763 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3857 : : {
3858 : 18 : inf = G_(" enters %<consteval if%> statement");
3859 : 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3860 : : saw_ceif = true;
3861 : : }
3862 : : break;
3863 : :
3864 : : default:
3865 : : break;
3866 : : }
3867 : :
3868 : : if (inf)
3869 : : {
3870 : 141 : if (identified < 2)
3871 : 141 : complained = identify_goto (decl, input_location, locus,
3872 : : diagnostics::kind::error,
3873 : : computed);
3874 : 141 : identified = 2;
3875 : 141 : if (complained)
3876 : 141 : inform (loc, inf);
3877 : : }
3878 : : }
3879 : :
3880 : 1985537 : if (!vec_safe_is_empty (computed))
3881 : : {
3882 : 9 : if (!identified)
3883 : 6 : complained = identify_goto (decl, input_location, locus,
3884 : : diagnostics::kind::error,
3885 : : computed);
3886 : 9 : identified = 2;
3887 : 9 : if (complained)
3888 : 18 : for (tree d : computed)
3889 : : {
3890 : 9 : if (DECL_P (d))
3891 : 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3892 : 3 : else if (d == get_identifier ("catch"))
3893 : 3 : inform (*locus, " does not clean up handled exception");
3894 : : }
3895 : : }
3896 : :
3897 : 3971074 : return !identified;
3898 : 1985537 : }
3899 : :
3900 : : static void
3901 : 1736 : check_previous_goto (tree decl, struct named_label_use_entry *use)
3902 : : {
3903 : 1736 : check_previous_goto_1 (decl, use->binding_level,
3904 : 1736 : use->names_in_scope, use->in_omp_scope,
3905 : 1736 : &use->o_goto_locus, use->computed_goto);
3906 : 1736 : }
3907 : :
3908 : : static bool
3909 : 1983801 : check_switch_goto (cp_binding_level* level)
3910 : : {
3911 : 1983801 : return check_previous_goto_1 (NULL_TREE, level, level->names,
3912 : 1983801 : false, NULL, nullptr);
3913 : : }
3914 : :
3915 : : /* Check that a new jump to a label ENT is OK. COMPUTED is true
3916 : : if this is a possible target of a computed goto. */
3917 : :
3918 : : void
3919 : 2354 : check_goto_1 (named_label_entry *ent, bool computed)
3920 : : {
3921 : 2354 : auto_diagnostic_group d;
3922 : 2354 : tree decl = ent->label_decl;
3923 : :
3924 : : /* If the label hasn't been defined yet, defer checking. */
3925 : 2354 : if (! DECL_INITIAL (decl))
3926 : : {
3927 : : /* Don't bother creating another use if the last goto had the
3928 : : same data, and will therefore create the same set of errors. */
3929 : 1936 : if (ent->uses
3930 : 493 : && ent->uses->names_in_scope == current_binding_level->names)
3931 : : return;
3932 : :
3933 : 1745 : named_label_use_entry *new_use
3934 : 1745 : = ggc_alloc<named_label_use_entry> ();
3935 : 1745 : new_use->binding_level = current_binding_level;
3936 : 1745 : new_use->names_in_scope = current_binding_level->names;
3937 : 1745 : new_use->o_goto_locus = input_location;
3938 : 1745 : new_use->in_omp_scope = false;
3939 : 1745 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3940 : :
3941 : 1745 : new_use->next = ent->uses;
3942 : 1745 : ent->uses = new_use;
3943 : 1745 : return;
3944 : : }
3945 : :
3946 : 418 : bool saw_catch = false, complained = false;
3947 : 418 : int identified = 0;
3948 : 418 : tree bad;
3949 : 418 : unsigned ix;
3950 : :
3951 : 409 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3952 : 403 : || ent->in_constexpr_if || ent->in_consteval_if
3953 : 397 : || ent->in_omp_scope || ent->in_stmt_expr
3954 : 797 : || !vec_safe_is_empty (ent->bad_decls))
3955 : : {
3956 : 45 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
3957 : 45 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3958 : 30 : || ent->in_consteval_if || ent->in_transaction_scope
3959 : 24 : || ent->in_omp_scope || ent->in_stmt_expr)
3960 : 39 : diag_kind = diagnostics::kind::error;
3961 : 45 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3962 : : &input_location, diag_kind, computed);
3963 : 45 : identified = 1 + (diag_kind == diagnostics::kind::error);
3964 : : }
3965 : :
3966 : 424 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3967 : : {
3968 : 6 : bool problem = decl_jump_unsafe (bad);
3969 : :
3970 : 6 : if (problem && DECL_ARTIFICIAL (bad))
3971 : : {
3972 : : /* Can't skip init of __exception_info. */
3973 : 0 : if (identified == 1)
3974 : : {
3975 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3976 : : &input_location,
3977 : : diagnostics::kind::error,
3978 : : computed);
3979 : 0 : identified = 2;
3980 : : }
3981 : 0 : if (complained)
3982 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3983 : : saw_catch = true;
3984 : : }
3985 : 6 : else if (complained)
3986 : 6 : inform (DECL_SOURCE_LOCATION (bad),
3987 : : " skips initialization of %q#D", bad);
3988 : : }
3989 : :
3990 : 418 : if (complained)
3991 : : {
3992 : 45 : if (ent->in_try_scope)
3993 : 9 : inform (input_location, " enters %<try%> block");
3994 : 36 : else if (ent->in_catch_scope && !saw_catch)
3995 : 3 : inform (input_location, " enters %<catch%> block");
3996 : 33 : else if (ent->in_transaction_scope)
3997 : 3 : inform (input_location, " enters synchronized or atomic statement");
3998 : 30 : else if (ent->in_constexpr_if)
3999 : 3 : inform (input_location, " enters %<constexpr if%> statement");
4000 : 27 : else if (ent->in_consteval_if)
4001 : 3 : inform (input_location, " enters %<consteval if%> statement");
4002 : 24 : else if (ent->in_stmt_expr)
4003 : 15 : inform (input_location, " enters statement expression");
4004 : : }
4005 : :
4006 : 418 : if (ent->in_omp_scope)
4007 : : {
4008 : 3 : if (complained)
4009 : 3 : inform (input_location, " enters OpenMP structured block");
4010 : : }
4011 : 415 : else if (flag_openmp)
4012 : 69 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4013 : : {
4014 : 42 : if (b == ent->binding_level)
4015 : : break;
4016 : 36 : if (b->kind == sk_omp)
4017 : : {
4018 : 21 : if (identified < 2)
4019 : : {
4020 : 42 : complained = identify_goto (decl,
4021 : 21 : DECL_SOURCE_LOCATION (decl),
4022 : : &input_location,
4023 : : diagnostics::kind::error,
4024 : : computed);
4025 : 21 : identified = 2;
4026 : : }
4027 : 21 : if (complained)
4028 : 21 : inform (input_location, " exits OpenMP structured block");
4029 : : break;
4030 : : }
4031 : : }
4032 : :
4033 : : /* Warn if a computed goto might involve a local variable going out of scope
4034 : : without being cleaned up. */
4035 : 418 : if (computed)
4036 : : {
4037 : 71 : auto level = ent->binding_level;
4038 : 71 : auto names = ent->names_in_scope;
4039 : 71 : for (auto b = current_binding_level; ; b = b->level_chain)
4040 : : {
4041 : 111 : if (b->kind == sk_catch)
4042 : : {
4043 : 6 : if (!identified)
4044 : : {
4045 : 6 : complained
4046 : 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4047 : : &input_location, diagnostics::kind::error,
4048 : : computed);
4049 : 6 : identified = 2;
4050 : : }
4051 : 6 : if (complained)
4052 : 6 : inform (input_location,
4053 : : " does not clean up handled exception");
4054 : : }
4055 : 111 : tree end = b == level ? names : NULL_TREE;
4056 : 127 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4057 : : {
4058 : 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4059 : : {
4060 : 10 : if (!identified)
4061 : : {
4062 : 10 : complained
4063 : 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4064 : : &input_location, diagnostics::kind::error,
4065 : : computed);
4066 : 10 : identified = 2;
4067 : : }
4068 : 10 : if (complained)
4069 : 10 : inform (DECL_SOURCE_LOCATION (d),
4070 : : " does not destroy %qD", d);
4071 : : }
4072 : : }
4073 : 111 : if (b == level)
4074 : : break;
4075 : 40 : }
4076 : : }
4077 : 2354 : }
4078 : :
4079 : : /* Check that a new jump to a label DECL is OK. Called by
4080 : : finish_goto_stmt. */
4081 : :
4082 : : void
4083 : 2314 : check_goto (tree decl)
4084 : : {
4085 : 2314 : if (!named_labels)
4086 : : return;
4087 : 2299 : if (TREE_CODE (decl) != LABEL_DECL)
4088 : : {
4089 : : /* We don't know where a computed goto is jumping,
4090 : : so check all addressable labels. */
4091 : 306 : for (auto iter = named_labels->begin ();
4092 : 416 : iter != named_labels->end ();
4093 : 196 : ++iter)
4094 : : {
4095 : 196 : auto ent = *iter;
4096 : 196 : if (ent->addressed)
4097 : 165 : check_goto_1 (ent, true);
4098 : : }
4099 : : }
4100 : : else
4101 : : {
4102 : 2189 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4103 : 2189 : named_label_entry **slot
4104 : 2189 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4105 : 2189 : named_label_entry *ent = *slot;
4106 : 2189 : check_goto_1 (ent, false);
4107 : : }
4108 : : }
4109 : :
4110 : : /* Check that a return is ok wrt OpenMP structured blocks.
4111 : : Called by finish_return_stmt. Returns true if all is well. */
4112 : :
4113 : : bool
4114 : 232304 : check_omp_return (void)
4115 : : {
4116 : 889564 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4117 : 657260 : if (b->kind == sk_omp)
4118 : : {
4119 : 15 : error ("invalid exit from OpenMP structured block");
4120 : 15 : return false;
4121 : : }
4122 : 657245 : else if (b->kind == sk_function_parms)
4123 : : break;
4124 : : return true;
4125 : : }
4126 : :
4127 : : /* Define a label, specifying the location in the source file.
4128 : : Return the LABEL_DECL node for the label. */
4129 : :
4130 : : tree
4131 : 20273 : define_label (location_t location, tree name)
4132 : : {
4133 : 20273 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4134 : :
4135 : : /* After labels, make any new cleanups in the function go into their
4136 : : own new (temporary) binding contour. */
4137 : 20273 : for (cp_binding_level *p = current_binding_level;
4138 : 30378 : p->kind != sk_function_parms;
4139 : 10105 : p = p->level_chain)
4140 : 10105 : p->more_cleanups_ok = 0;
4141 : :
4142 : 20273 : named_label_entry *ent = lookup_label_1 (name, false);
4143 : 20273 : tree decl = ent->label_decl;
4144 : :
4145 : 20273 : if (DECL_INITIAL (decl) != NULL_TREE)
4146 : : {
4147 : 6 : error ("duplicate label %qD", decl);
4148 : 6 : return error_mark_node;
4149 : : }
4150 : : else
4151 : : {
4152 : : /* Mark label as having been defined. */
4153 : 20267 : DECL_INITIAL (decl) = error_mark_node;
4154 : : /* Say where in the source. */
4155 : 20267 : DECL_SOURCE_LOCATION (decl) = location;
4156 : :
4157 : 20267 : ent->binding_level = current_binding_level;
4158 : 20267 : ent->names_in_scope = current_binding_level->names;
4159 : :
4160 : 22003 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4161 : 1736 : check_previous_goto (decl, use);
4162 : 20267 : ent->uses = NULL;
4163 : : }
4164 : :
4165 : 20267 : return decl;
4166 : 20273 : }
4167 : :
4168 : : struct cp_switch
4169 : : {
4170 : : cp_binding_level *level;
4171 : : struct cp_switch *next;
4172 : : /* The SWITCH_STMT being built. */
4173 : : tree switch_stmt;
4174 : : /* A splay-tree mapping the low element of a case range to the high
4175 : : element, or NULL_TREE if there is no high element. Used to
4176 : : determine whether or not a new case label duplicates an old case
4177 : : label. We need a tree, rather than simply a hash table, because
4178 : : of the GNU case range extension. */
4179 : : splay_tree cases;
4180 : : /* Remember whether a default: case label has been seen. */
4181 : : bool has_default_p;
4182 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4183 : : bool break_stmt_seen_p;
4184 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4185 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4186 : : bool in_loop_body_p;
4187 : : };
4188 : :
4189 : : /* A stack of the currently active switch statements. The innermost
4190 : : switch statement is on the top of the stack. There is no need to
4191 : : mark the stack for garbage collection because it is only active
4192 : : during the processing of the body of a function, and we never
4193 : : collect at that point. */
4194 : :
4195 : : static struct cp_switch *switch_stack;
4196 : :
4197 : : /* Called right after a switch-statement condition is parsed.
4198 : : SWITCH_STMT is the switch statement being parsed. */
4199 : :
4200 : : void
4201 : 496579 : push_switch (tree switch_stmt)
4202 : : {
4203 : 496579 : struct cp_switch *p = XNEW (struct cp_switch);
4204 : 496579 : p->level = current_binding_level;
4205 : 496579 : p->next = switch_stack;
4206 : 496579 : p->switch_stmt = switch_stmt;
4207 : 496579 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4208 : 496579 : p->has_default_p = false;
4209 : 496579 : p->break_stmt_seen_p = false;
4210 : 496579 : p->in_loop_body_p = false;
4211 : 496579 : switch_stack = p;
4212 : 496579 : }
4213 : :
4214 : : void
4215 : 496579 : pop_switch (void)
4216 : : {
4217 : 496579 : struct cp_switch *cs = switch_stack;
4218 : :
4219 : : /* Emit warnings as needed. */
4220 : 496579 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4221 : 496579 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4222 : 496579 : const bool bool_cond_p
4223 : 496579 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4224 : 496579 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4225 : 496579 : if (!processing_template_decl)
4226 : 313346 : c_do_switch_warnings (cs->cases, switch_location,
4227 : 313346 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4228 : : bool_cond_p);
4229 : :
4230 : : /* For the benefit of block_may_fallthru remember if the switch body
4231 : : case labels cover all possible values and if there are break; stmts. */
4232 : 496579 : if (cs->has_default_p
4233 : 496579 : || (!processing_template_decl
4234 : 43172 : && c_switch_covers_all_cases_p (cs->cases,
4235 : 43172 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4236 : 383841 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4237 : 496579 : if (!cs->break_stmt_seen_p)
4238 : 255456 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4239 : : /* Now that we're done with the switch warnings, set the switch type
4240 : : to the type of the condition if the index type was of scoped enum type.
4241 : : (Such types don't participate in the integer promotions.) We do this
4242 : : because of bit-fields whose declared type is a scoped enum type:
4243 : : gimplification will use the lowered index type, but convert the
4244 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4245 : : and verify_gimple_switch doesn't accept that. */
4246 : 496579 : if (is_bitfield_expr_with_lowered_type (cond))
4247 : 55482 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4248 : 496579 : gcc_assert (!cs->in_loop_body_p);
4249 : 496579 : splay_tree_delete (cs->cases);
4250 : 496579 : switch_stack = switch_stack->next;
4251 : 496579 : free (cs);
4252 : 496579 : }
4253 : :
4254 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4255 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4256 : : in switch_stack we've seen a BREAK_STMT. */
4257 : :
4258 : : void
4259 : 3538486 : note_break_stmt (void)
4260 : : {
4261 : 3538486 : if (switch_stack && !switch_stack->in_loop_body_p)
4262 : 1544673 : switch_stack->break_stmt_seen_p = true;
4263 : 3538486 : }
4264 : :
4265 : : /* Note the start of processing of an iteration statement's body.
4266 : : The note_break_stmt function will do nothing while processing it.
4267 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4268 : :
4269 : : bool
4270 : 15444448 : note_iteration_stmt_body_start (void)
4271 : : {
4272 : 15444448 : if (!switch_stack)
4273 : : return false;
4274 : 118249 : bool ret = switch_stack->in_loop_body_p;
4275 : 118249 : switch_stack->in_loop_body_p = true;
4276 : 118249 : return ret;
4277 : : }
4278 : :
4279 : : /* Note the end of processing of an iteration statement's body. */
4280 : :
4281 : : void
4282 : 15444448 : note_iteration_stmt_body_end (bool prev)
4283 : : {
4284 : 15444448 : if (switch_stack)
4285 : 118249 : switch_stack->in_loop_body_p = prev;
4286 : 15444448 : }
4287 : :
4288 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4289 : : condition. Note that if TYPE and VALUE are already integral we don't
4290 : : really do the conversion because the language-independent
4291 : : warning/optimization code will work better that way. */
4292 : :
4293 : : static tree
4294 : 3967356 : case_conversion (tree type, tree value)
4295 : : {
4296 : 3967356 : if (value == NULL_TREE)
4297 : : return value;
4298 : :
4299 : 1713672 : value = mark_rvalue_use (value);
4300 : :
4301 : 1713672 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4302 : 968861 : type = type_promotes_to (type);
4303 : :
4304 : 1713672 : tree ovalue = value;
4305 : : /* The constant-expression VALUE shall be a converted constant expression
4306 : : of the adjusted type of the switch condition, which doesn't allow
4307 : : narrowing conversions. */
4308 : 1713672 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4309 : :
4310 : 1713672 : if (cxx_dialect >= cxx11
4311 : 1713672 : && (SCOPED_ENUM_P (type)
4312 : 966143 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4313 : : /* Use the converted value. */;
4314 : : else
4315 : : /* The already integral case. */
4316 : : value = ovalue;
4317 : :
4318 : 1713672 : return cxx_constant_value (value);
4319 : : }
4320 : :
4321 : : /* Note that we've seen a definition of a case label, and complain if this
4322 : : is a bad place for one. */
4323 : :
4324 : : tree
4325 : 3671906 : finish_case_label (location_t loc, tree low_value, tree high_value)
4326 : : {
4327 : 3671906 : tree cond, r;
4328 : 3671906 : cp_binding_level *p;
4329 : 3671906 : tree type;
4330 : :
4331 : 3671906 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4332 : 383805 : switch_stack->has_default_p = true;
4333 : :
4334 : 3671906 : if (processing_template_decl)
4335 : : {
4336 : 1688105 : tree label;
4337 : :
4338 : : /* For templates, just add the case label; we'll do semantic
4339 : : analysis at instantiation-time. But diagnose case labels
4340 : : in expansion statements with switch outside of it here. */
4341 : 1688105 : if (in_expansion_stmt)
4342 : 39 : for (cp_binding_level *b = current_binding_level;
4343 : 78 : b != switch_stack->level; b = b->level_chain)
4344 : 63 : if (b->kind == sk_template_for && b->this_entity)
4345 : : {
4346 : 24 : auto_diagnostic_group d;
4347 : 24 : error ("jump to case label");
4348 : 24 : inform (EXPR_LOCATION (b->this_entity),
4349 : : " enters %<template for%> statement");
4350 : 24 : return error_mark_node;
4351 : 24 : }
4352 : 1688081 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4353 : 1688081 : return add_stmt (build_case_label (low_value, high_value, label));
4354 : : }
4355 : :
4356 : : /* Find the condition on which this switch statement depends. */
4357 : 1983801 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4358 : 1983801 : if (cond && TREE_CODE (cond) == TREE_LIST)
4359 : 0 : cond = TREE_VALUE (cond);
4360 : :
4361 : 1983801 : if (!check_switch_goto (switch_stack->level))
4362 : 87 : return error_mark_node;
4363 : :
4364 : 1983714 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4365 : 1983714 : if (type == error_mark_node)
4366 : : return error_mark_node;
4367 : :
4368 : 1983678 : low_value = case_conversion (type, low_value);
4369 : 1983678 : high_value = case_conversion (type, high_value);
4370 : :
4371 : 1983678 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4372 : :
4373 : : /* After labels, make any new cleanups in the function go into their
4374 : : own new (temporary) binding contour. */
4375 : 1983678 : for (p = current_binding_level;
4376 : 8350193 : p->kind != sk_function_parms;
4377 : 6366515 : p = p->level_chain)
4378 : 6366515 : p->more_cleanups_ok = 0;
4379 : :
4380 : : return r;
4381 : : }
4382 : :
4383 : : struct typename_info {
4384 : : tree scope;
4385 : : tree name;
4386 : : tree template_id;
4387 : : bool enum_p;
4388 : : bool class_p;
4389 : : bool union_p;
4390 : : };
4391 : :
4392 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4393 : : {
4394 : : typedef typename_info *compare_type;
4395 : :
4396 : : /* Hash a TYPENAME_TYPE. */
4397 : :
4398 : : static hashval_t
4399 : 389136458 : hash (tree context, tree fullname)
4400 : : {
4401 : 389136458 : hashval_t hash = 0;
4402 : 389136458 : hash = iterative_hash_object (context, hash);
4403 : 389136458 : hash = iterative_hash_object (fullname, hash);
4404 : 389136458 : return hash;
4405 : : }
4406 : :
4407 : : static hashval_t
4408 : 56464973 : hash (const typename_info *ti)
4409 : : {
4410 : 56464973 : return typename_hasher::hash (ti->scope, ti->template_id);
4411 : : }
4412 : :
4413 : : static hashval_t
4414 : 332671485 : hash (tree t)
4415 : : {
4416 : 332671485 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4417 : : }
4418 : :
4419 : : /* Compare two TYPENAME_TYPEs. */
4420 : :
4421 : : static bool
4422 : 386208871 : equal (tree t1, const typename_info *t2)
4423 : : {
4424 : 386208871 : return (TYPE_IDENTIFIER (t1) == t2->name
4425 : 114368946 : && TYPE_CONTEXT (t1) == t2->scope
4426 : 28789130 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4427 : 28778439 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4428 : 28778385 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p
4429 : 414987226 : && TYPENAME_IS_UNION_P (t1) == t2->union_p);
4430 : : }
4431 : : };
4432 : :
4433 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4434 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4435 : :
4436 : : Returns the new TYPENAME_TYPE. */
4437 : :
4438 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4439 : :
4440 : : tree
4441 : 56464973 : build_typename_type (tree context, tree name, tree fullname,
4442 : : enum tag_types tag_type)
4443 : : {
4444 : 56464973 : typename_info ti;
4445 : :
4446 : 56464973 : if (typename_htab == NULL)
4447 : 19100 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4448 : :
4449 : 56464973 : ti.scope = FROB_CONTEXT (context);
4450 : 56464973 : ti.name = name;
4451 : 56464973 : ti.template_id = fullname;
4452 : 56464973 : ti.enum_p = tag_type == enum_type;
4453 : 56464973 : ti.class_p = (tag_type == class_type || tag_type == record_type);
4454 : 56464973 : ti.union_p = tag_type == union_type;
4455 : 56464973 : hashval_t hash = typename_hasher::hash (&ti);
4456 : :
4457 : : /* See if we already have this type. */
4458 : 56464973 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4459 : 56464973 : tree t = *e;
4460 : 56464973 : if (*e)
4461 : : t = *e;
4462 : : else
4463 : : {
4464 : : /* Build the TYPENAME_TYPE. */
4465 : 27686618 : t = cxx_make_type (TYPENAME_TYPE);
4466 : 27686618 : TYPE_CONTEXT (t) = ti.scope;
4467 : 27686618 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4468 : 27686618 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4469 : 27686618 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4470 : 27686618 : TYPENAME_IS_UNION_P (t) = ti.union_p;
4471 : :
4472 : : /* Build the corresponding TYPE_DECL. */
4473 : 27686618 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4474 : 27686618 : TYPE_NAME (t) = d;
4475 : 27686618 : TYPE_STUB_DECL (t) = d;
4476 : 27686618 : DECL_CONTEXT (d) = ti.scope;
4477 : 27686618 : DECL_ARTIFICIAL (d) = 1;
4478 : :
4479 : : /* Store it in the hash table. */
4480 : 27686618 : *e = t;
4481 : :
4482 : : /* TYPENAME_TYPEs must always be compared structurally, because
4483 : : they may or may not resolve down to another type depending on
4484 : : the currently open classes. */
4485 : 27686618 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4486 : : }
4487 : :
4488 : 56464973 : return t;
4489 : : }
4490 : :
4491 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4492 : : provided to name the type. Returns an appropriate type, unless an
4493 : : error occurs, in which case error_mark_node is returned. If we
4494 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4495 : : return that, rather than the _TYPE it corresponds to, in other
4496 : : cases we look through the type decl. If TF_ERROR is set, complain
4497 : : about errors, otherwise be quiet. */
4498 : :
4499 : : tree
4500 : 197061778 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4501 : : tsubst_flags_t complain)
4502 : : {
4503 : 197061778 : tree fullname;
4504 : 197061778 : tree t;
4505 : 197061778 : bool want_template;
4506 : :
4507 : 197061778 : if (name == error_mark_node
4508 : 197061776 : || context == NULL_TREE
4509 : 197061776 : || context == error_mark_node)
4510 : : return error_mark_node;
4511 : :
4512 : 197061760 : if (TYPE_P (name))
4513 : : {
4514 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4515 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4516 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4517 : 0 : name = TYPE_IDENTIFIER (name);
4518 : : else
4519 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4520 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4521 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4522 : 0 : CLASSTYPE_TI_ARGS (name));
4523 : : }
4524 : 197061760 : else if (TREE_CODE (name) == TYPE_DECL)
4525 : 0 : name = DECL_NAME (name);
4526 : :
4527 : 197061760 : fullname = name;
4528 : :
4529 : 197061760 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4530 : : {
4531 : 6259986 : name = TREE_OPERAND (name, 0);
4532 : 6259986 : if (DECL_TYPE_TEMPLATE_P (name))
4533 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4534 : 6259986 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4535 : : {
4536 : 0 : if (complain & tf_error)
4537 : 0 : error ("%qD is not a type", name);
4538 : 0 : return error_mark_node;
4539 : : }
4540 : : }
4541 : 197061760 : if (TREE_CODE (name) == TEMPLATE_DECL)
4542 : : {
4543 : 0 : if (complain & tf_error)
4544 : 0 : error ("%qD used without template arguments", name);
4545 : 0 : return error_mark_node;
4546 : : }
4547 : 197061760 : else if (is_overloaded_fn (name))
4548 : : {
4549 : 3 : if (complain & tf_error)
4550 : 3 : error ("%qD is a function, not a type", name);
4551 : 3 : return error_mark_node;
4552 : : }
4553 : 197061757 : gcc_assert (identifier_p (name));
4554 : 197061757 : gcc_assert (TYPE_P (context));
4555 : :
4556 : 197061757 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4557 : : /* This can happen for C++17 variadic using (c++/88986). */;
4558 : 185401296 : else if (!MAYBE_CLASS_TYPE_P (context))
4559 : : {
4560 : 6 : if (complain & tf_error)
4561 : 6 : error ("%q#T is not a class", context);
4562 : 6 : return error_mark_node;
4563 : : }
4564 : :
4565 : : /* When the CONTEXT is a dependent type, NAME could refer to a
4566 : : dependent base class of CONTEXT. But look inside it anyway
4567 : : if CONTEXT is a currently open scope, in case it refers to a
4568 : : member of the current instantiation or a non-dependent base;
4569 : : lookup will stop when we hit a dependent base. */
4570 : 197061751 : if (!dependent_scope_p (context))
4571 : : {
4572 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4573 : : (as per [temp.res.general]/3), unless
4574 : : - the tag corresponds to a class-key or 'enum' so
4575 : : [basic.lookup.elab] applies, or
4576 : : - the tag corresponds to scope_type or tf_qualifying_scope is
4577 : : set so [basic.lookup.qual]/1 applies.
4578 : : TODO: If we'd set/track the scope_type tag thoroughly on all
4579 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4580 : : tf_qualifying_scope flag. */
4581 : 289567282 : bool want_type = (tag_type != none_type && tag_type != typename_type)
4582 : 144783641 : || (complain & tf_qualifying_scope);
4583 : 144783641 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4584 : : }
4585 : : else
4586 : : t = NULL_TREE;
4587 : :
4588 : 214523903 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
4589 : 52279028 : return build_typename_type (context, name, fullname, tag_type);
4590 : :
4591 : 144782723 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4592 : :
4593 : 144782723 : if (!t)
4594 : : {
4595 : 17461234 : if (complain & tf_error)
4596 : : {
4597 : 242 : if (!COMPLETE_TYPE_P (context))
4598 : 18 : cxx_incomplete_type_error (NULL_TREE, context);
4599 : : else
4600 : 447 : error (want_template ? G_("no class template named %q#T in %q#T")
4601 : : : G_("no type named %q#T in %q#T"), name, context);
4602 : : }
4603 : 17461234 : return error_mark_node;
4604 : : }
4605 : :
4606 : : /* Pull out the template from an injected-class-name (or multiple). */
4607 : 127321489 : if (want_template)
4608 : 4793243 : t = maybe_get_template_decl_from_type_decl (t);
4609 : :
4610 : 127321489 : if (TREE_CODE (t) == TREE_LIST)
4611 : : {
4612 : 3 : if (complain & tf_error)
4613 : : {
4614 : 3 : auto_diagnostic_group d;
4615 : 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
4616 : 3 : print_candidates (t);
4617 : 3 : }
4618 : 3 : return error_mark_node;
4619 : : }
4620 : :
4621 : 127321486 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4622 : : {
4623 : 6 : if (complain & tf_error)
4624 : 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
4625 : : context, name, t);
4626 : 6 : return error_mark_node;
4627 : : }
4628 : 127321480 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
4629 : : {
4630 : 327 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4631 : 657 : && DECL_TYPE_TEMPLATE_P (t))
4632 : : /* The caller permits this typename-specifier to name a template
4633 : : (because it appears in a CTAD-enabled context). */;
4634 : : else
4635 : : {
4636 : 23 : if (complain & tf_error)
4637 : 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
4638 : : context, name, t);
4639 : 23 : return error_mark_node;
4640 : : }
4641 : : }
4642 : :
4643 : 127321457 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4644 : : context, complain))
4645 : 49 : return error_mark_node;
4646 : :
4647 : 127321408 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4648 : 316 : return make_template_placeholder (t);
4649 : :
4650 : 127321092 : if (want_template)
4651 : : {
4652 : 4793237 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4653 : : NULL_TREE, context,
4654 : : complain | tf_user);
4655 : 4793237 : if (t == error_mark_node)
4656 : : return error_mark_node;
4657 : 4793232 : t = TYPE_NAME (t);
4658 : : }
4659 : :
4660 : 127321087 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4661 : 3389971 : t = TREE_TYPE (t);
4662 : :
4663 : 127321087 : maybe_record_typedef_use (t);
4664 : :
4665 : 127321087 : return t;
4666 : : }
4667 : :
4668 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4669 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4670 : : in which case error_mark_node is returned.
4671 : :
4672 : : If PARM_LIST is non-NULL, also make sure that the template parameter
4673 : : list of TEMPLATE_DECL matches.
4674 : :
4675 : : If COMPLAIN zero, don't complain about any errors that occur. */
4676 : :
4677 : : tree
4678 : 81860 : make_unbound_class_template (tree context, tree name, tree parm_list,
4679 : : tsubst_flags_t complain)
4680 : : {
4681 : 81860 : if (TYPE_P (name))
4682 : 0 : name = TYPE_IDENTIFIER (name);
4683 : 81860 : else if (DECL_P (name))
4684 : 0 : name = DECL_NAME (name);
4685 : 81860 : gcc_assert (identifier_p (name));
4686 : :
4687 : 81860 : if (!dependent_type_p (context)
4688 : 81860 : || currently_open_class (context))
4689 : : {
4690 : 27314 : tree tmpl = NULL_TREE;
4691 : :
4692 : 27314 : if (MAYBE_CLASS_TYPE_P (context))
4693 : 27308 : tmpl = lookup_field (context, name, 0, false);
4694 : :
4695 : 27308 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4696 : 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4697 : :
4698 : 27302 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4699 : : {
4700 : 12 : if (complain & tf_error)
4701 : 9 : error ("no class template named %q#T in %q#T", name, context);
4702 : 12 : return error_mark_node;
4703 : : }
4704 : :
4705 : 27302 : if (parm_list
4706 : 27302 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4707 : : {
4708 : 0 : if (complain & tf_error)
4709 : : {
4710 : 0 : auto_diagnostic_group d;
4711 : 0 : error ("template parameters do not match template %qD", tmpl);
4712 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
4713 : : "%qD declared here", tmpl);
4714 : 0 : }
4715 : 0 : return error_mark_node;
4716 : : }
4717 : :
4718 : 27302 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4719 : : complain))
4720 : 0 : return error_mark_node;
4721 : :
4722 : : return tmpl;
4723 : : }
4724 : :
4725 : 54546 : return make_unbound_class_template_raw (context, name, parm_list);
4726 : : }
4727 : :
4728 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
4729 : :
4730 : : tree
4731 : 54590 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4732 : : {
4733 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
4734 : 54590 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4735 : 54590 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4736 : 54590 : TREE_TYPE (t) = NULL_TREE;
4737 : 54590 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4738 : :
4739 : : /* Build the corresponding TEMPLATE_DECL. */
4740 : 54590 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4741 : 54590 : TYPE_NAME (t) = d;
4742 : 54590 : TYPE_STUB_DECL (t) = d;
4743 : 54590 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4744 : 54590 : DECL_ARTIFICIAL (d) = 1;
4745 : 54590 : DECL_TEMPLATE_PARMS (d) = parm_list;
4746 : :
4747 : 54590 : return t;
4748 : : }
4749 : :
4750 : :
4751 : :
4752 : : /* Push the declarations of builtin types into the global namespace.
4753 : : RID_INDEX is the index of the builtin type in the array
4754 : : RID_POINTERS. NAME is the name used when looking up the builtin
4755 : : type. TYPE is the _TYPE node for the builtin type.
4756 : :
4757 : : The calls to set_global_binding below should be
4758 : : eliminated. Built-in types should not be looked up name; their
4759 : : names are keywords that the parser can recognize. However, there
4760 : : is code in c-common.cc that uses identifier_global_value to look up
4761 : : built-in types by name. */
4762 : :
4763 : : void
4764 : 3936050 : record_builtin_type (enum rid rid_index,
4765 : : const char* name,
4766 : : tree type)
4767 : : {
4768 : 3936050 : tree decl = NULL_TREE;
4769 : :
4770 : 3936050 : if (name)
4771 : : {
4772 : 2305036 : tree tname = get_identifier (name);
4773 : 2305036 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4774 : 2305036 : DECL_ARTIFICIAL (tdecl) = 1;
4775 : 2305036 : set_global_binding (tdecl);
4776 : 2305036 : decl = tdecl;
4777 : : }
4778 : :
4779 : 3936050 : if ((int) rid_index < (int) RID_MAX)
4780 : 2400978 : if (tree rname = ridpointers[(int) rid_index])
4781 : 1825326 : if (!decl || DECL_NAME (decl) != rname)
4782 : : {
4783 : 1343188 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4784 : 1343188 : DECL_ARTIFICIAL (rdecl) = 1;
4785 : 1343188 : set_global_binding (rdecl);
4786 : 1343188 : if (!decl)
4787 : : decl = rdecl;
4788 : : }
4789 : :
4790 : 3648224 : if (decl)
4791 : : {
4792 : 3360398 : if (!TYPE_NAME (type))
4793 : 2784746 : TYPE_NAME (type) = decl;
4794 : 3360398 : debug_hooks->type_decl (decl, 0);
4795 : : }
4796 : 3936050 : }
4797 : :
4798 : : /* Push a type into the namespace so that the back ends ignore it. */
4799 : :
4800 : : static void
4801 : 287826 : record_unknown_type (tree type, const char* name)
4802 : : {
4803 : 287826 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4804 : : TYPE_DECL, get_identifier (name), type));
4805 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4806 : 287826 : DECL_IGNORED_P (decl) = 1;
4807 : 287826 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4808 : 287826 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4809 : 287826 : SET_TYPE_ALIGN (type, 1);
4810 : 287826 : TYPE_USER_ALIGN (type) = 0;
4811 : 287826 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4812 : 287826 : }
4813 : :
4814 : : /* Create all the predefined identifiers. */
4815 : :
4816 : : static void
4817 : 95942 : initialize_predefined_identifiers (void)
4818 : : {
4819 : 95942 : struct predefined_identifier
4820 : : {
4821 : : const char *name; /* Name. */
4822 : : tree *node; /* Node to store it in. */
4823 : : cp_identifier_kind kind; /* Kind of identifier. */
4824 : : };
4825 : :
4826 : : /* A table of identifiers to create at startup. */
4827 : 95942 : static const predefined_identifier predefined_identifiers[] = {
4828 : : {"C++", &lang_name_cplusplus, cik_normal},
4829 : : {"C", &lang_name_c, cik_normal},
4830 : : /* Some of these names have a trailing space so that it is
4831 : : impossible for them to conflict with names written by users. */
4832 : : {"__ct ", &ctor_identifier, cik_ctor},
4833 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
4834 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4835 : : {"__dt ", &dtor_identifier, cik_dtor},
4836 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
4837 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4838 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4839 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
4840 : : {"__in_chrg", &in_charge_identifier, cik_normal},
4841 : : {"__as_base ", &as_base_identifier, cik_normal},
4842 : : {"this", &this_identifier, cik_normal},
4843 : : {"__delta", &delta_identifier, cik_normal},
4844 : : {"__pfn", &pfn_identifier, cik_normal},
4845 : : {"_vptr", &vptr_identifier, cik_normal},
4846 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4847 : : {"::", &global_identifier, cik_normal},
4848 : : /* The demangler expects anonymous namespaces to be called
4849 : : something starting with '_GLOBAL__N_'. It no longer needs
4850 : : to be unique to the TU. */
4851 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4852 : : {"auto", &auto_identifier, cik_normal},
4853 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4854 : : {"initializer_list", &init_list_identifier, cik_normal},
4855 : : {"__for_range ", &for_range__identifier, cik_normal},
4856 : : {"__for_begin ", &for_begin__identifier, cik_normal},
4857 : : {"__for_end ", &for_end__identifier, cik_normal},
4858 : : {"__for_range", &for_range_identifier, cik_normal},
4859 : : {"__for_begin", &for_begin_identifier, cik_normal},
4860 : : {"__for_end", &for_end_identifier, cik_normal},
4861 : : {"abi_tag", &abi_tag_identifier, cik_normal},
4862 : : {"aligned", &aligned_identifier, cik_normal},
4863 : : {"begin", &begin_identifier, cik_normal},
4864 : : {"end", &end_identifier, cik_normal},
4865 : : {"get", &get__identifier, cik_normal},
4866 : : {"gnu", &gnu_identifier, cik_normal},
4867 : : {"tuple_element", &tuple_element_identifier, cik_normal},
4868 : : {"tuple_size", &tuple_size_identifier, cik_normal},
4869 : : {"type", &type_identifier, cik_normal},
4870 : : {"value", &value_identifier, cik_normal},
4871 : : {"_FUN", &fun_identifier, cik_normal},
4872 : : {"__closure", &closure_identifier, cik_normal},
4873 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
4874 : : {"heap ", &heap_identifier, cik_normal},
4875 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
4876 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4877 : : {"heap []", &heap_vec_identifier, cik_normal},
4878 : : {"omp", &omp_identifier, cik_normal},
4879 : : {"internal ", &internal_identifier, cik_normal},
4880 : : {NULL, NULL, cik_normal}
4881 : : };
4882 : :
4883 : 4605216 : for (const predefined_identifier *pid = predefined_identifiers;
4884 : 4605216 : pid->name; ++pid)
4885 : : {
4886 : 4509274 : *pid->node = get_identifier (pid->name);
4887 : : /* Some of these identifiers already have a special kind. */
4888 : 4509274 : if (pid->kind != cik_normal)
4889 : 767536 : set_identifier_kind (*pid->node, pid->kind);
4890 : : }
4891 : 95942 : }
4892 : :
4893 : : /* Build a specific variant of operator new. */
4894 : :
4895 : : static void
4896 : 171035 : cxx_build_operator_new (tree newtype)
4897 : : {
4898 : 171035 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4899 : 171035 : DECL_IS_MALLOC (opnew) = 1;
4900 : 171035 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
4901 : 171035 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4902 : 171035 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4903 : 171035 : DECL_IS_MALLOC (opnew) = 1;
4904 : 171035 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
4905 : 171035 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4906 : 171035 : }
4907 : :
4908 : : /* Build a specific variant of operator delete. */
4909 : :
4910 : : static void
4911 : 322488 : cxx_build_operator_delete (tree deltype)
4912 : : {
4913 : 322488 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4914 : 322488 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4915 : 322488 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4916 : 322488 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4917 : 322488 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4918 : 322488 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4919 : 322488 : }
4920 : :
4921 : : /* Declare all variants of operator new and delete. */
4922 : :
4923 : : static void
4924 : 95942 : cxx_init_operator_new_delete_decls (void)
4925 : : {
4926 : 95942 : tree newattrs, extvisattr;
4927 : 95942 : tree newtype, deltype;
4928 : 95942 : tree ptr_ftype_sizetype;
4929 : 95942 : tree new_eh_spec;
4930 : 95942 : tree void_ftype_ptr = build_function_type_list (void_type_node,
4931 : : ptr_type_node, NULL_TREE);
4932 : 95942 : void_ftype_ptr
4933 : 95942 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
4934 : :
4935 : 95942 : ptr_ftype_sizetype
4936 : 95942 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4937 : 95942 : if (cxx_dialect == cxx98)
4938 : : {
4939 : 13990 : tree bad_alloc_id;
4940 : 13990 : tree bad_alloc_type_node;
4941 : 13990 : tree bad_alloc_decl;
4942 : :
4943 : 13990 : push_nested_namespace (std_node);
4944 : 13990 : bad_alloc_id = get_identifier ("bad_alloc");
4945 : 13990 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
4946 : 13990 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4947 : 13990 : bad_alloc_decl
4948 : 13990 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4949 : 13990 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4950 : 13990 : pop_nested_namespace (std_node);
4951 : :
4952 : 13990 : new_eh_spec
4953 : 13990 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4954 : : }
4955 : : else
4956 : 81952 : new_eh_spec = noexcept_false_spec;
4957 : :
4958 : 95942 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
4959 : : NULL_TREE);
4960 : 95942 : newattrs = tree_cons (get_identifier ("alloc_size"),
4961 : : build_tree_list (NULL_TREE, integer_one_node),
4962 : : extvisattr);
4963 : 95942 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4964 : 95942 : newtype = build_exception_variant (newtype, new_eh_spec);
4965 : 95942 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4966 : 95942 : deltype = build_exception_variant (deltype, empty_except_spec);
4967 : 95942 : cxx_build_operator_new (newtype);
4968 : 95942 : cxx_build_operator_delete (deltype);
4969 : 95942 : if (flag_sized_deallocation)
4970 : : {
4971 : : /* Also push the sized deallocation variants:
4972 : : void operator delete(void*, std::size_t) throw();
4973 : : void operator delete[](void*, std::size_t) throw(); */
4974 : 76365 : tree void_ftype_ptr_size
4975 : 76365 : = build_function_type_list (void_type_node, ptr_type_node,
4976 : : size_type_node, NULL_TREE);
4977 : 76365 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4978 : : extvisattr);
4979 : 76365 : deltype = build_exception_variant (deltype, empty_except_spec);
4980 : 76365 : cxx_build_operator_delete (deltype);
4981 : : }
4982 : :
4983 : 95942 : if (aligned_new_threshold)
4984 : : {
4985 : 75093 : push_nested_namespace (std_node);
4986 : 75093 : tree align_id = get_identifier ("align_val_t");
4987 : 75093 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4988 : : NULL_TREE, /*scoped*/true, NULL);
4989 : 75093 : pop_nested_namespace (std_node);
4990 : :
4991 : : /* operator new (size_t, align_val_t); */
4992 : 75093 : newtype = build_function_type_list (ptr_type_node, size_type_node,
4993 : : align_type_node, NULL_TREE);
4994 : 75093 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
4995 : 75093 : newtype = build_exception_variant (newtype, new_eh_spec);
4996 : 75093 : cxx_build_operator_new (newtype);
4997 : :
4998 : : /* operator delete (void *, align_val_t); */
4999 : 75093 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5000 : : align_type_node, NULL_TREE);
5001 : 75093 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5002 : 75093 : deltype = build_exception_variant (deltype, empty_except_spec);
5003 : 75093 : cxx_build_operator_delete (deltype);
5004 : :
5005 : 75093 : if (flag_sized_deallocation)
5006 : : {
5007 : : /* operator delete (void *, size_t, align_val_t); */
5008 : 75088 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5009 : : size_type_node, align_type_node,
5010 : : NULL_TREE);
5011 : 75088 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5012 : 75088 : deltype = build_exception_variant (deltype, empty_except_spec);
5013 : 75088 : cxx_build_operator_delete (deltype);
5014 : : }
5015 : : }
5016 : 95942 : }
5017 : :
5018 : : /* Create the predefined scalar types of C,
5019 : : and some nodes representing standard constants (0, 1, (void *)0).
5020 : : Initialize the global binding level.
5021 : : Make definitions for built-in primitive functions. */
5022 : :
5023 : : void
5024 : 95942 : cxx_init_decl_processing (void)
5025 : : {
5026 : 95942 : tree void_ftype;
5027 : :
5028 : : /* Create all the identifiers we need. */
5029 : 95942 : initialize_predefined_identifiers ();
5030 : :
5031 : : /* Create the global variables. */
5032 : 95942 : push_to_top_level ();
5033 : :
5034 : 95942 : current_function_decl = NULL_TREE;
5035 : 95942 : current_binding_level = NULL;
5036 : : /* Enter the global namespace. */
5037 : 95942 : gcc_assert (global_namespace == NULL_TREE);
5038 : 95942 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
5039 : : void_type_node);
5040 : 95942 : TREE_PUBLIC (global_namespace) = true;
5041 : 95942 : DECL_MODULE_EXPORT_P (global_namespace) = true;
5042 : 95942 : DECL_CONTEXT (global_namespace)
5043 : 95942 : = build_translation_unit_decl (get_identifier (main_input_filename));
5044 : : /* Remember whether we want the empty class passing ABI change warning
5045 : : in this TU. */
5046 : 95942 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
5047 : 122223 : = warn_abi && abi_version_crosses (12);
5048 : 95942 : debug_hooks->register_main_translation_unit
5049 : 95942 : (DECL_CONTEXT (global_namespace));
5050 : 95942 : begin_scope (sk_namespace, global_namespace);
5051 : 95942 : current_namespace = global_namespace;
5052 : :
5053 : 95942 : if (flag_visibility_ms_compat)
5054 : 6 : default_visibility = VISIBILITY_HIDDEN;
5055 : :
5056 : : /* Initially, C. */
5057 : 95942 : current_lang_name = lang_name_c;
5058 : :
5059 : : /* Create the `std' namespace. */
5060 : 95942 : push_namespace (get_identifier ("std"));
5061 : 95942 : std_node = current_namespace;
5062 : 95942 : pop_namespace ();
5063 : :
5064 : 95942 : flag_noexcept_type = (cxx_dialect >= cxx17);
5065 : :
5066 : 95942 : c_common_nodes_and_builtins ();
5067 : :
5068 : 95942 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
5069 : 95942 : tree decl
5070 : 95942 : = add_builtin_function ("__builtin_is_constant_evaluated",
5071 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
5072 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5073 : 95942 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5074 : :
5075 : : /* The concrete return type of __builtin_source_location is
5076 : : const std::source_location::__impl*, but we can't form the type
5077 : : at this point. So we initially declare it with an auto return
5078 : : type which we then "deduce" from require_deduced_type upon first use. */
5079 : 95942 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
5080 : 95942 : decl = add_builtin_function ("__builtin_source_location",
5081 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
5082 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5083 : 95942 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5084 : :
5085 : 95942 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
5086 : : NULL_TREE);
5087 : 95942 : decl
5088 : 95942 : = add_builtin_function ("__builtin_is_corresponding_member",
5089 : : bool_vaftype,
5090 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
5091 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5092 : 95942 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5093 : :
5094 : 95942 : decl
5095 : 95942 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
5096 : : bool_vaftype,
5097 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
5098 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5099 : 95942 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
5100 : :
5101 : 95942 : if (cxx_dialect >= cxx26)
5102 : : {
5103 : 23467 : tree void_ptrintftype
5104 : 23467 : = build_function_type_list (void_type_node, ptr_type_node,
5105 : : integer_type_node, NULL_TREE);
5106 : 23467 : decl = add_builtin_function ("__builtin_eh_ptr_adjust_ref",
5107 : : void_ptrintftype,
5108 : : CP_BUILT_IN_EH_PTR_ADJUST_REF,
5109 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
5110 : 23467 : set_call_expr_flags (decl, ECF_NOTHROW | ECF_LEAF);
5111 : : }
5112 : :
5113 : 95942 : integer_two_node = build_int_cst (NULL_TREE, 2);
5114 : :
5115 : : /* Guess at the initial static decls size. */
5116 : 95942 : vec_alloc (static_decls, 500);
5117 : :
5118 : : /* ... and keyed classes. */
5119 : 95942 : vec_alloc (keyed_classes, 100);
5120 : :
5121 : 95942 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
5122 : 95942 : truthvalue_type_node = boolean_type_node;
5123 : 95942 : truthvalue_false_node = boolean_false_node;
5124 : 95942 : truthvalue_true_node = boolean_true_node;
5125 : :
5126 : 95942 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
5127 : 95942 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
5128 : 95942 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
5129 : 95942 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
5130 : : NULL_TREE);
5131 : :
5132 : : #if 0
5133 : : record_builtin_type (RID_MAX, NULL, string_type_node);
5134 : : #endif
5135 : :
5136 : 95942 : delta_type_node = ptrdiff_type_node;
5137 : 95942 : vtable_index_type = ptrdiff_type_node;
5138 : :
5139 : 95942 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
5140 : 95942 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
5141 : :
5142 : : /* Create the conversion operator marker. This operator's DECL_NAME
5143 : : is in the identifier table, so we can use identifier equality to
5144 : : find it. */
5145 : 95942 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
5146 : : void_ftype);
5147 : :
5148 : : /* C++ extensions */
5149 : :
5150 : 95942 : unknown_type_node = make_node (LANG_TYPE);
5151 : 95942 : record_unknown_type (unknown_type_node, "unknown type");
5152 : :
5153 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
5154 : 95942 : TREE_TYPE (unknown_type_node) = unknown_type_node;
5155 : :
5156 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
5157 : : result. */
5158 : 95942 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
5159 : 95942 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
5160 : :
5161 : 95942 : init_list_type_node = make_node (LANG_TYPE);
5162 : 95942 : record_unknown_type (init_list_type_node, "init list");
5163 : :
5164 : : /* Used when parsing to distinguish parameter-lists () and (void). */
5165 : 95942 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
5166 : :
5167 : 95942 : {
5168 : : /* Make sure we get a unique function type, so we can give
5169 : : its pointer type a name. (This wins for gdb.) */
5170 : 95942 : tree vfunc_type = make_node (FUNCTION_TYPE);
5171 : 95942 : TREE_TYPE (vfunc_type) = integer_type_node;
5172 : 95942 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
5173 : 95942 : layout_type (vfunc_type);
5174 : :
5175 : 95942 : vtable_entry_type = build_pointer_type (vfunc_type);
5176 : : }
5177 : 95942 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
5178 : :
5179 : 95942 : vtbl_type_node
5180 : 95942 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
5181 : 95942 : layout_type (vtbl_type_node);
5182 : 95942 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
5183 : 95942 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
5184 : 95942 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
5185 : 95942 : layout_type (vtbl_ptr_type_node);
5186 : 95942 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
5187 : :
5188 : 95942 : push_namespace (get_identifier ("__cxxabiv1"));
5189 : 95942 : abi_node = current_namespace;
5190 : 95942 : pop_namespace ();
5191 : :
5192 : 95942 : any_targ_node = make_node (LANG_TYPE);
5193 : 95942 : record_unknown_type (any_targ_node, "any type");
5194 : :
5195 : : /* Now, C++. */
5196 : 95942 : current_lang_name = lang_name_cplusplus;
5197 : :
5198 : 95942 : if (aligned_new_threshold > 1
5199 : 95942 : && !pow2p_hwi (aligned_new_threshold))
5200 : : {
5201 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
5202 : : aligned_new_threshold);
5203 : 0 : aligned_new_threshold = 1;
5204 : : }
5205 : 95942 : if (aligned_new_threshold == -1)
5206 : 116777 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5207 : 95942 : if (aligned_new_threshold == 1)
5208 : 75090 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5209 : :
5210 : : /* Ensure attribs.cc is initialized. */
5211 : 95942 : init_attributes ();
5212 : 95942 : cxx_init_operator_new_delete_decls ();
5213 : :
5214 : : /* C++-specific nullptr initialization. */
5215 : 95942 : if (abi_version_at_least (9))
5216 : 95771 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5217 : 95942 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5218 : :
5219 : 95942 : if (! supports_one_only ())
5220 : 0 : flag_weak = 0;
5221 : :
5222 : 95942 : abort_fndecl
5223 : 95942 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5224 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5225 : 95942 : if (flag_weak)
5226 : : /* If no definition is available, resolve references to NULL. */
5227 : 95912 : declare_weak (abort_fndecl);
5228 : :
5229 : : /* Perform other language dependent initializations. */
5230 : 95942 : init_class_processing ();
5231 : 95942 : init_rtti_processing ();
5232 : 95942 : init_template_processing ();
5233 : :
5234 : 95942 : if (flag_exceptions)
5235 : 94702 : init_exception_processing ();
5236 : :
5237 : 95942 : if (flag_contracts)
5238 : 155 : init_terminate_fn ();
5239 : :
5240 : 95942 : if (modules_p ())
5241 : 4396 : init_modules (parse_in);
5242 : :
5243 : 95942 : make_fname_decl = cp_make_fname_decl;
5244 : 95942 : start_fname_decls ();
5245 : :
5246 : : /* Show we use EH for cleanups. */
5247 : 95942 : if (flag_exceptions)
5248 : 94702 : using_eh_for_cleanups ();
5249 : :
5250 : : /* Check that the hardware interference sizes are at least
5251 : : alignof(max_align_t), as required by the standard. */
5252 : 95942 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5253 : 95942 : if (OPTION_SET_P (param_destruct_interfere_size))
5254 : : {
5255 : 0 : if (param_destruct_interfere_size < max_align)
5256 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5257 : : "%d", param_destruct_interfere_size, max_align);
5258 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5259 : 0 : warning (OPT_Winterference_size,
5260 : : "%<--param destructive-interference-size=%d%> "
5261 : : "is less than %<--param l1-cache-line-size=%d%>",
5262 : : param_destruct_interfere_size, param_l1_cache_line_size);
5263 : : }
5264 : 95942 : else if (param_destruct_interfere_size)
5265 : : /* Assume the internal value is OK. */;
5266 : 0 : else if (param_l1_cache_line_size >= max_align)
5267 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5268 : : /* else leave it unset. */
5269 : :
5270 : 95942 : if (OPTION_SET_P (param_construct_interfere_size))
5271 : : {
5272 : 0 : if (param_construct_interfere_size < max_align)
5273 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5274 : : "%d", param_construct_interfere_size, max_align);
5275 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5276 : 0 : && param_l1_cache_line_size >= max_align)
5277 : 0 : warning (OPT_Winterference_size,
5278 : : "%<--param constructive-interference-size=%d%> "
5279 : : "is greater than %<--param l1-cache-line-size=%d%>",
5280 : : param_construct_interfere_size, param_l1_cache_line_size);
5281 : : }
5282 : 95942 : else if (param_construct_interfere_size)
5283 : : /* Assume the internal value is OK. */;
5284 : 0 : else if (param_l1_cache_line_size >= max_align)
5285 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5286 : 95942 : }
5287 : :
5288 : : /* Enter an abi node in global-module context. returns a cookie to
5289 : : give to pop_abi_namespace. */
5290 : :
5291 : : unsigned
5292 : 60970 : push_abi_namespace (tree node)
5293 : : {
5294 : 60970 : push_nested_namespace (node);
5295 : 60970 : push_visibility ("default", 2);
5296 : 60970 : unsigned flags = module_kind;
5297 : 60970 : module_kind = 0;
5298 : 60970 : return flags;
5299 : : }
5300 : :
5301 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5302 : : you. */
5303 : :
5304 : : void
5305 : 60970 : pop_abi_namespace (unsigned flags, tree node)
5306 : : {
5307 : 60970 : module_kind = flags;
5308 : 60970 : pop_visibility (2);
5309 : 60970 : pop_nested_namespace (node);
5310 : 60970 : }
5311 : :
5312 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5313 : : the decl, LOC is the location to give the decl, NAME is the
5314 : : initialization string and TYPE_DEP indicates whether NAME depended
5315 : : on the type of the function. We make use of that to detect
5316 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5317 : : at the point of first use, so we mustn't push the decl now. */
5318 : :
5319 : : static tree
5320 : 114200 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5321 : : {
5322 : 114200 : tree domain = NULL_TREE;
5323 : 114200 : tree init = NULL_TREE;
5324 : :
5325 : 114200 : if (!(type_dep && current_function_decl && in_template_context))
5326 : : {
5327 : 35790 : const char *name = NULL;
5328 : 35790 : bool release_name = false;
5329 : :
5330 : 35790 : if (current_function_decl == NULL_TREE)
5331 : : name = "top level";
5332 : 35776 : else if (type_dep == 0)
5333 : : {
5334 : : /* __FUNCTION__ */
5335 : 618 : name = fname_as_string (type_dep);
5336 : 618 : release_name = true;
5337 : : }
5338 : : else
5339 : : {
5340 : : /* __PRETTY_FUNCTION__ */
5341 : 35158 : gcc_checking_assert (type_dep == 1);
5342 : 35158 : name = cxx_printable_name (current_function_decl, 2);
5343 : : }
5344 : :
5345 : 35790 : size_t length = strlen (name);
5346 : 35790 : domain = build_index_type (size_int (length));
5347 : 35790 : init = build_string (length + 1, name);
5348 : 35790 : if (release_name)
5349 : 618 : free (const_cast<char *> (name));
5350 : : }
5351 : :
5352 : 114200 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5353 : 114200 : type = build_cplus_array_type (type, domain);
5354 : :
5355 : 114200 : if (init)
5356 : 35790 : TREE_TYPE (init) = type;
5357 : : else
5358 : 78410 : init = error_mark_node;
5359 : :
5360 : 114200 : tree decl = build_decl (loc, VAR_DECL, id, type);
5361 : :
5362 : 114200 : TREE_READONLY (decl) = 1;
5363 : 114200 : DECL_ARTIFICIAL (decl) = 1;
5364 : 114200 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5365 : 114200 : TREE_STATIC (decl) = 1;
5366 : :
5367 : 114200 : TREE_USED (decl) = 1;
5368 : :
5369 : 114200 : SET_DECL_VALUE_EXPR (decl, init);
5370 : 114200 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5371 : : /* For decl_constant_var_p. */
5372 : 114200 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5373 : :
5374 : 114200 : if (current_function_decl)
5375 : : {
5376 : 114186 : DECL_CONTEXT (decl) = current_function_decl;
5377 : 114186 : decl = pushdecl_outermost_localscope (decl);
5378 : 114186 : if (decl != error_mark_node)
5379 : 114183 : add_decl_expr (decl);
5380 : : else
5381 : 3 : gcc_assert (seen_error ());
5382 : : }
5383 : : else
5384 : : {
5385 : 14 : DECL_THIS_STATIC (decl) = true;
5386 : 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5387 : : }
5388 : :
5389 : 114200 : return decl;
5390 : : }
5391 : :
5392 : : /* Install DECL as a builtin function at current global scope. Return
5393 : : the new decl (if we found an existing version). Also installs it
5394 : : into ::std, if it's not '_*'. */
5395 : :
5396 : : tree
5397 : 221537306 : cxx_builtin_function (tree decl)
5398 : : {
5399 : 221537306 : retrofit_lang_decl (decl);
5400 : :
5401 : 221537306 : DECL_ARTIFICIAL (decl) = 1;
5402 : 221537306 : SET_DECL_LANGUAGE (decl, lang_c);
5403 : : /* Runtime library routines are, by definition, available in an
5404 : : external shared object. */
5405 : 221537306 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5406 : 221537306 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5407 : :
5408 : 221537306 : tree id = DECL_NAME (decl);
5409 : 221537306 : const char *name = IDENTIFIER_POINTER (id);
5410 : 221537306 : bool hiding = false;
5411 : 221537306 : if (name[0] != '_' || name[1] != '_')
5412 : : /* In the user's namespace, it must be declared before use. */
5413 : : hiding = true;
5414 : 185769026 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5415 : 185769026 : && !startswith (name + 2, "builtin_")
5416 : 208090729 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5417 : : "_chk", strlen ("_chk") + 1))
5418 : : /* Treat __*_chk fortification functions as anticipated as well,
5419 : : unless they are __builtin_*_chk. */
5420 : : hiding = true;
5421 : :
5422 : : /* All builtins that don't begin with an '_' should additionally
5423 : : go in the 'std' namespace. */
5424 : 221537306 : if (name[0] != '_')
5425 : : {
5426 : 35637022 : tree std_decl = copy_decl (decl);
5427 : :
5428 : 35637022 : push_nested_namespace (std_node);
5429 : 35637022 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5430 : 35637022 : pushdecl (std_decl, hiding);
5431 : 35637022 : pop_nested_namespace (std_node);
5432 : : }
5433 : :
5434 : 221537306 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5435 : 221537306 : decl = pushdecl (decl, hiding);
5436 : :
5437 : 221537306 : return decl;
5438 : : }
5439 : :
5440 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5441 : : scope. This is to allow function specific options to add new machine
5442 : : dependent builtins when the target ISA changes via attribute((target(...)))
5443 : : which saves space on program startup if the program does not use non-generic
5444 : : ISAs. */
5445 : :
5446 : : tree
5447 : 629403 : cxx_builtin_function_ext_scope (tree decl)
5448 : : {
5449 : 629403 : push_nested_namespace (global_namespace);
5450 : 629403 : decl = cxx_builtin_function (decl);
5451 : 629403 : pop_nested_namespace (global_namespace);
5452 : :
5453 : 629403 : return decl;
5454 : : }
5455 : :
5456 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5457 : :
5458 : : tree
5459 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5460 : : {
5461 : 0 : retrofit_lang_decl (decl);
5462 : :
5463 : 0 : DECL_ARTIFICIAL (decl) = 1;
5464 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5465 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5466 : 0 : return pushdecl (decl);
5467 : : }
5468 : :
5469 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5470 : : function. Not called directly. */
5471 : :
5472 : : static tree
5473 : 1546656 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5474 : : int ecf_flags)
5475 : : {
5476 : 1546656 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5477 : 1546656 : DECL_EXTERNAL (fn) = 1;
5478 : 1546656 : TREE_PUBLIC (fn) = 1;
5479 : 1546656 : DECL_ARTIFICIAL (fn) = 1;
5480 : 1546656 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5481 : 1546656 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5482 : 1546656 : SET_DECL_LANGUAGE (fn, lang_c);
5483 : : /* Runtime library routines are, by definition, available in an
5484 : : external shared object. */
5485 : 1546656 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5486 : 1546656 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5487 : 1546656 : set_call_expr_flags (fn, ecf_flags);
5488 : 1546656 : return fn;
5489 : : }
5490 : :
5491 : : /* Returns the _DECL for a library function with C++ linkage. */
5492 : :
5493 : : static tree
5494 : 1081839 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5495 : : int ecf_flags)
5496 : : {
5497 : 1081839 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5498 : 1081839 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5499 : 1081839 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5500 : 1081839 : return fn;
5501 : : }
5502 : :
5503 : : /* Like build_library_fn, but takes a C string instead of an
5504 : : IDENTIFIER_NODE. */
5505 : :
5506 : : tree
5507 : 102148 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5508 : : {
5509 : 102148 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5510 : : }
5511 : :
5512 : : /* Like build_cp_library_fn, but takes a C string instead of an
5513 : : IDENTIFIER_NODE. */
5514 : :
5515 : : tree
5516 : 94793 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5517 : : {
5518 : 94793 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5519 : 94793 : ecf_flags);
5520 : : }
5521 : :
5522 : : /* Like build_library_fn, but also pushes the function so that we will
5523 : : be able to find it via get_global_binding. Also, the function
5524 : : may throw exceptions listed in RAISES. */
5525 : :
5526 : : tree
5527 : 362669 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5528 : : {
5529 : 362669 : if (raises)
5530 : 40886 : type = build_exception_variant (type, raises);
5531 : :
5532 : 362669 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5533 : 362669 : return pushdecl_top_level (fn);
5534 : : }
5535 : :
5536 : : /* Like build_cp_library_fn, but also pushes the function so that it
5537 : : will be found by normal lookup. */
5538 : :
5539 : : static tree
5540 : 987046 : push_cp_library_fn (enum tree_code operator_code, tree type,
5541 : : int ecf_flags)
5542 : : {
5543 : 987046 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5544 : : operator_code, type, ecf_flags);
5545 : 987046 : pushdecl (fn);
5546 : 987046 : if (flag_tm)
5547 : 2980 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
5548 : 987046 : return fn;
5549 : : }
5550 : :
5551 : : /* Like push_library_fn, but also note that this function throws
5552 : : and does not return. Used for __throw_foo and the like. */
5553 : :
5554 : : tree
5555 : 105853 : push_throw_library_fn (tree name, tree type)
5556 : : {
5557 : 105853 : tree fn = push_library_fn (name, type, NULL_TREE,
5558 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5559 : 105853 : return fn;
5560 : : }
5561 : :
5562 : : /* When we call finish_struct for an anonymous union, we create
5563 : : default copy constructors and such. But, an anonymous union
5564 : : shouldn't have such things; this function undoes the damage to the
5565 : : anonymous union type T.
5566 : :
5567 : : (The reason that we create the synthesized methods is that we don't
5568 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
5569 : : The first is an anonymous union; the second is just an ordinary
5570 : : union type.) */
5571 : :
5572 : : void
5573 : 95464 : fixup_anonymous_aggr (tree t)
5574 : : {
5575 : : /* Wipe out memory of synthesized methods. */
5576 : 95464 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5577 : 95464 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5578 : 95464 : TYPE_HAS_COPY_CTOR (t) = 0;
5579 : 95464 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5580 : 95464 : TYPE_HAS_COPY_ASSIGN (t) = 0;
5581 : 95464 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5582 : :
5583 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5584 : : invalid members. */
5585 : 510015 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5586 : : {
5587 : 414551 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5588 : 0 : *prev_p = DECL_CHAIN (probe);
5589 : : else
5590 : 414551 : prev_p = &DECL_CHAIN (probe);
5591 : :
5592 : 414551 : if (DECL_ARTIFICIAL (probe)
5593 : 414551 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5594 : 42950 : || TYPE_ANON_P (TREE_TYPE (probe))))
5595 : 143970 : continue;
5596 : :
5597 : 270581 : if (TREE_CODE (probe) != FIELD_DECL
5598 : 270527 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5599 : : {
5600 : : /* We already complained about static data members in
5601 : : finish_static_data_member_decl. */
5602 : 66 : if (!VAR_P (probe))
5603 : : {
5604 : 51 : auto_diagnostic_group d;
5605 : 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
5606 : 51 : TREE_CODE (t) == UNION_TYPE
5607 : : ? "%q#D invalid; an anonymous union may "
5608 : : "only have public non-static data members"
5609 : : : "%q#D invalid; an anonymous struct may "
5610 : : "only have public non-static data members", probe))
5611 : : {
5612 : 51 : static bool hint;
5613 : 51 : if (flag_permissive && !hint)
5614 : : {
5615 : 6 : hint = true;
5616 : 6 : inform (DECL_SOURCE_LOCATION (probe),
5617 : : "this flexibility is deprecated and will be "
5618 : : "removed");
5619 : : }
5620 : : }
5621 : 51 : }
5622 : : }
5623 : : }
5624 : :
5625 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5626 : 95464 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5627 : 95464 : unsigned store = 0;
5628 : 222805 : for (tree elt : vec)
5629 : 127341 : if (!is_overloaded_fn (elt))
5630 : 127320 : (*vec)[store++] = elt;
5631 : 95464 : vec_safe_truncate (vec, store);
5632 : :
5633 : : /* Wipe RTTI info. */
5634 : 95464 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5635 : :
5636 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5637 : : assignment operators (because they cannot have these methods themselves).
5638 : : For anonymous unions this is already checked because they are not allowed
5639 : : in any union, otherwise we have to check it. */
5640 : 95464 : if (TREE_CODE (t) != UNION_TYPE)
5641 : : {
5642 : 20063 : tree field, type;
5643 : :
5644 : 20063 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5645 : : {
5646 : 9 : error_at (location_of (t), "anonymous struct with base classes");
5647 : : /* Avoid ICE after error on anon-struct9.C. */
5648 : 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
5649 : : }
5650 : :
5651 : 90719 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5652 : 70656 : if (TREE_CODE (field) == FIELD_DECL)
5653 : : {
5654 : 50492 : type = TREE_TYPE (field);
5655 : 50492 : if (CLASS_TYPE_P (type))
5656 : : {
5657 : 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
5658 : 6 : error ("member %q+#D with constructor not allowed "
5659 : : "in anonymous aggregate", field);
5660 : 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5661 : 0 : error ("member %q+#D with destructor not allowed "
5662 : : "in anonymous aggregate", field);
5663 : 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5664 : 0 : error ("member %q+#D with copy assignment operator "
5665 : : "not allowed in anonymous aggregate", field);
5666 : : }
5667 : : }
5668 : : }
5669 : 95464 : }
5670 : :
5671 : : /* Warn for an attribute located at LOCATION that appertains to the
5672 : : class type CLASS_TYPE that has not been properly placed after its
5673 : : class-key, in it class-specifier. */
5674 : :
5675 : : void
5676 : 102 : warn_misplaced_attr_for_class_type (location_t location,
5677 : : tree class_type)
5678 : : {
5679 : 102 : gcc_assert (OVERLOAD_TYPE_P (class_type));
5680 : :
5681 : 102 : auto_diagnostic_group d;
5682 : 102 : if (warning_at (location, OPT_Wattributes,
5683 : : "attribute ignored in declaration "
5684 : : "of %q#T", class_type))
5685 : 102 : inform (location,
5686 : : "attribute for %q#T must follow the %qs keyword",
5687 : : class_type, class_key_or_enum_as_string (class_type));
5688 : 102 : }
5689 : :
5690 : : /* Returns the cv-qualifiers that apply to the type specified
5691 : : by the DECLSPECS. */
5692 : :
5693 : : static int
5694 : 919711570 : get_type_quals (const cp_decl_specifier_seq *declspecs)
5695 : : {
5696 : 919711570 : int type_quals = TYPE_UNQUALIFIED;
5697 : :
5698 : 919711570 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5699 : 100551878 : type_quals |= TYPE_QUAL_CONST;
5700 : 919711570 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5701 : 1579378 : type_quals |= TYPE_QUAL_VOLATILE;
5702 : 919711570 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5703 : 34 : type_quals |= TYPE_QUAL_RESTRICT;
5704 : :
5705 : 919711570 : return type_quals;
5706 : : }
5707 : :
5708 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
5709 : : just declares a tagged type or anonymous union.
5710 : :
5711 : : Returns the type declared; or NULL_TREE if none. */
5712 : :
5713 : : tree
5714 : 32358633 : check_tag_decl (cp_decl_specifier_seq *declspecs,
5715 : : bool explicit_type_instantiation_p)
5716 : : {
5717 : 32358633 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5718 : 32358633 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5719 : : /* If a class, struct, or enum type is declared by the DECLSPECS
5720 : : (i.e, if a class-specifier, enum-specifier, or non-typename
5721 : : elaborated-type-specifier appears in the DECLSPECS),
5722 : : DECLARED_TYPE is set to the corresponding type. */
5723 : 32358633 : tree declared_type = NULL_TREE;
5724 : 32358633 : bool error_p = false;
5725 : :
5726 : 32358633 : if (declspecs->multiple_types_p)
5727 : 26 : error_at (smallest_type_location (declspecs),
5728 : : "multiple types in one declaration");
5729 : 32358607 : else if (declspecs->redefined_builtin_type)
5730 : : {
5731 : 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5732 : 21 : if (!in_system_header_at (loc))
5733 : 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
5734 : : declspecs->redefined_builtin_type);
5735 : 21 : return NULL_TREE;
5736 : : }
5737 : :
5738 : 32358612 : if (declspecs->type
5739 : 32358597 : && TYPE_P (declspecs->type)
5740 : 64526700 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5741 : 32166576 : && MAYBE_CLASS_TYPE_P (declspecs->type))
5742 : 1656112 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5743 : : declared_type = declspecs->type;
5744 : 192078 : else if (declspecs->type == error_mark_node)
5745 : 1465 : error_p = true;
5746 : :
5747 : 32358612 : if (type_uses_auto (declared_type))
5748 : : {
5749 : 8 : error_at (declspecs->locations[ds_type_spec],
5750 : : "%<auto%> can only be specified for variables "
5751 : : "or function declarations");
5752 : 8 : return error_mark_node;
5753 : : }
5754 : :
5755 : 32358604 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5756 : : declared_type = NULL_TREE;
5757 : :
5758 : 32358604 : if (!declared_type && !saw_friend && !error_p)
5759 : 88 : permerror (input_location, "declaration does not declare anything");
5760 : : /* Check for an anonymous union. */
5761 : 32166499 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5762 : 93477868 : && TYPE_UNNAMED_P (declared_type))
5763 : : {
5764 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
5765 : : can be omitted only when declaring a class (clause 9) or
5766 : : enumeration (7.2), that is, when the decl-specifier-seq contains
5767 : : either a class-specifier, an elaborated-type-specifier with
5768 : : a class-key (9.1), or an enum-specifier. In these cases and
5769 : : whenever a class-specifier or enum-specifier is present in the
5770 : : decl-specifier-seq, the identifiers in these specifiers are among
5771 : : the names being declared by the declaration (as class-name,
5772 : : enum-names, or enumerators, depending on the syntax). In such
5773 : : cases, and except for the declaration of an unnamed bit-field (9.6),
5774 : : the decl-specifier-seq shall introduce one or more names into the
5775 : : program, or shall redeclare a name introduced by a previous
5776 : : declaration. [Example:
5777 : : enum { }; // ill-formed
5778 : : typedef class { }; // ill-formed
5779 : : --end example] */
5780 : 95470 : if (saw_typedef)
5781 : : {
5782 : 6 : error_at (declspecs->locations[ds_typedef],
5783 : : "missing type-name in typedef-declaration");
5784 : 6 : return NULL_TREE;
5785 : : }
5786 : 95464 : /* Anonymous unions are objects, so they can have specifiers. */;
5787 : 95464 : SET_ANON_AGGR_TYPE_P (declared_type);
5788 : :
5789 : 95464 : if (TREE_CODE (declared_type) != UNION_TYPE)
5790 : 20063 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5791 : 20063 : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5792 : : }
5793 : :
5794 : : else
5795 : : {
5796 : 32263046 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5797 : 9 : error_at (declspecs->locations[ds_inline],
5798 : : "%<inline%> can only be specified for functions");
5799 : 32263037 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5800 : 3 : error_at (declspecs->locations[ds_virtual],
5801 : : "%<virtual%> can only be specified for functions");
5802 : 32263034 : else if (saw_friend
5803 : 32263034 : && (!current_class_type
5804 : 1653928 : || current_scope () != current_class_type))
5805 : 0 : error_at (declspecs->locations[ds_friend],
5806 : : "%<friend%> can only be specified inside a class");
5807 : 32263034 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5808 : 3 : error_at (declspecs->locations[ds_explicit],
5809 : : "%<explicit%> can only be specified for constructors");
5810 : 32263031 : else if (declspecs->storage_class)
5811 : 3 : error_at (declspecs->locations[ds_storage_class],
5812 : : "a storage class can only be specified for objects "
5813 : : "and functions");
5814 : 32263028 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5815 : 9 : error_at (declspecs->locations[ds_const],
5816 : : "%<const%> can only be specified for objects and "
5817 : : "functions");
5818 : 32263019 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5819 : 3 : error_at (declspecs->locations[ds_volatile],
5820 : : "%<volatile%> can only be specified for objects and "
5821 : : "functions");
5822 : 32263016 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5823 : 3 : error_at (declspecs->locations[ds_restrict],
5824 : : "%<__restrict%> can only be specified for objects and "
5825 : : "functions");
5826 : 32263013 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5827 : 3 : error_at (declspecs->locations[ds_thread],
5828 : : "%<__thread%> can only be specified for objects "
5829 : : "and functions");
5830 : 32263010 : else if (saw_typedef)
5831 : 24 : warning_at (declspecs->locations[ds_typedef], 0,
5832 : : "%<typedef%> was ignored in this declaration");
5833 : 32262986 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5834 : 9 : error_at (declspecs->locations[ds_constexpr],
5835 : : "%qs cannot be used for type declarations", "constexpr");
5836 : 32262977 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5837 : 3 : error_at (declspecs->locations[ds_constinit],
5838 : : "%qs cannot be used for type declarations", "constinit");
5839 : 32262974 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5840 : 3 : error_at (declspecs->locations[ds_consteval],
5841 : : "%qs cannot be used for type declarations", "consteval");
5842 : : }
5843 : :
5844 : 32358598 : if (declspecs->attributes && warn_attributes && declared_type)
5845 : : {
5846 : 31 : location_t loc;
5847 : 25 : if (!CLASS_TYPE_P (declared_type)
5848 : 56 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5849 : : /* For a non-template class, use the name location. */
5850 : 22 : loc = location_of (declared_type);
5851 : : else
5852 : : /* For a template class (an explicit instantiation), use the
5853 : : current location. */
5854 : 9 : loc = input_location;
5855 : :
5856 : 31 : if (explicit_type_instantiation_p)
5857 : : /* [dcl.attr.grammar]/4:
5858 : :
5859 : : No attribute-specifier-seq shall appertain to an explicit
5860 : : instantiation. */
5861 : : {
5862 : 6 : auto_diagnostic_group d;
5863 : 6 : if (warning_at (loc, OPT_Wattributes,
5864 : : "attribute ignored in explicit instantiation %q#T",
5865 : : declared_type))
5866 : 6 : inform (loc,
5867 : : "no attribute can be applied to "
5868 : : "an explicit instantiation");
5869 : 6 : }
5870 : : else
5871 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
5872 : : }
5873 : :
5874 : 32358598 : if (declspecs->std_attributes
5875 : 77 : && declared_type
5876 : 32358672 : && any_nonignored_attribute_p (declspecs->std_attributes))
5877 : : {
5878 : 74 : auto_diagnostic_group d;
5879 : 74 : if (warning_at (declspecs->locations[ds_std_attribute], OPT_Wattributes,
5880 : : "attribute ignored"))
5881 : 74 : inform (declspecs->locations[ds_std_attribute],
5882 : : "an attribute that appertains to a type-specifier is ignored");
5883 : 74 : }
5884 : :
5885 : : /* Diagnose invalid application of contracts, if any. */
5886 : 32358598 : if (find_contract (declspecs->attributes))
5887 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
5888 : : else
5889 : 32358597 : diagnose_misapplied_contracts (declspecs->std_attributes);
5890 : :
5891 : : return declared_type;
5892 : : }
5893 : :
5894 : : /* Called when a declaration is seen that contains no names to declare.
5895 : : If its type is a reference to a structure, union or enum inherited
5896 : : from a containing scope, shadow that tag name for the current scope
5897 : : with a forward reference.
5898 : : If its type defines a new named structure or union
5899 : : or defines an enum, it is valid but we need not do anything here.
5900 : : Otherwise, it is an error.
5901 : :
5902 : : C++: may have to grok the declspecs to learn about static,
5903 : : complain for anonymous unions.
5904 : :
5905 : : Returns the TYPE declared -- or NULL_TREE if none. */
5906 : :
5907 : : tree
5908 : 28371259 : shadow_tag (cp_decl_specifier_seq *declspecs)
5909 : : {
5910 : 28371259 : tree t = check_tag_decl (declspecs,
5911 : : /*explicit_type_instantiation_p=*/false);
5912 : :
5913 : 28371259 : if (!t)
5914 : : return NULL_TREE;
5915 : :
5916 : 28369784 : t = maybe_process_partial_specialization (t);
5917 : 28369784 : if (t == error_mark_node)
5918 : : return NULL_TREE;
5919 : :
5920 : : /* This is where the variables in an anonymous union are
5921 : : declared. An anonymous union declaration looks like:
5922 : : union { ... } ;
5923 : : because there is no declarator after the union, the parser
5924 : : sends that declaration here. */
5925 : 28368561 : if (ANON_AGGR_TYPE_P (t))
5926 : : {
5927 : 248 : fixup_anonymous_aggr (t);
5928 : :
5929 : 248 : if (TYPE_FIELDS (t))
5930 : : {
5931 : 248 : tree decl = grokdeclarator (/*declarator=*/NULL,
5932 : : declspecs, NORMAL, 0, NULL);
5933 : 248 : finish_anon_union (decl);
5934 : : }
5935 : : }
5936 : :
5937 : : return t;
5938 : : }
5939 : :
5940 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5941 : :
5942 : : tree
5943 : 339677804 : groktypename (cp_decl_specifier_seq *type_specifiers,
5944 : : const cp_declarator *declarator,
5945 : : bool is_template_arg)
5946 : : {
5947 : 339677804 : tree attrs;
5948 : 339677804 : tree type;
5949 : 53481566 : enum decl_context context
5950 : 339677804 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5951 : 339677804 : attrs = type_specifiers->attributes;
5952 : 339677804 : type_specifiers->attributes = NULL_TREE;
5953 : 339677804 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5954 : 339677804 : if (attrs && type != error_mark_node)
5955 : : {
5956 : 600 : if (CLASS_TYPE_P (type))
5957 : 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5958 : : "outside of definition", type);
5959 : 594 : else if (MAYBE_CLASS_TYPE_P (type))
5960 : : /* A template type parameter or other dependent type. */
5961 : 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5962 : : "type %qT without an associated declaration", type);
5963 : : else
5964 : 585 : cplus_decl_attributes (&type, attrs, 0);
5965 : : }
5966 : 339677804 : return type;
5967 : : }
5968 : :
5969 : : /* Process a DECLARATOR for a function-scope or namespace-scope
5970 : : variable or function declaration.
5971 : : (Function definitions go through start_function; class member
5972 : : declarations appearing in the body of the class go through
5973 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
5974 : : If an error occurs, the error_mark_node is returned instead.
5975 : :
5976 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5977 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5978 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
5979 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5980 : : implicitly initialized via a default constructor. It can also be
5981 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5982 : : mark the new decl as DECL_DECOMPOSITION_P.
5983 : :
5984 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5985 : : declaration.
5986 : :
5987 : : The scope represented by the context of the returned DECL is pushed
5988 : : (if it is not the global namespace) and is assigned to
5989 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
5990 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
5991 : :
5992 : : tree
5993 : 114021520 : start_decl (const cp_declarator *declarator,
5994 : : cp_decl_specifier_seq *declspecs,
5995 : : int initialized,
5996 : : tree attributes,
5997 : : tree prefix_attributes,
5998 : : tree *pushed_scope_p)
5999 : : {
6000 : 114021520 : tree decl;
6001 : 114021520 : tree context;
6002 : 114021520 : bool was_public;
6003 : 114021520 : int flags;
6004 : 114021520 : bool alias;
6005 : 114021520 : tree initial;
6006 : :
6007 : 114021520 : *pushed_scope_p = NULL_TREE;
6008 : :
6009 : 114021520 : if (prefix_attributes != error_mark_node)
6010 : 114021518 : attributes = attr_chainon (attributes, prefix_attributes);
6011 : :
6012 : 114021520 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6013 : : &attributes);
6014 : :
6015 : 114021520 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
6016 : 114021520 : || decl == error_mark_node
6017 : 114019385 : || prefix_attributes == error_mark_node)
6018 : 2137 : return error_mark_node;
6019 : :
6020 : 114019383 : context = CP_DECL_CONTEXT (decl);
6021 : 114019383 : if (context != global_namespace)
6022 : 17573425 : *pushed_scope_p = push_scope (context);
6023 : :
6024 : 114019383 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
6025 : : {
6026 : 27 : error_at (DECL_SOURCE_LOCATION (decl),
6027 : : "typedef %qD is initialized (use %qs instead)",
6028 : : decl, "decltype");
6029 : 27 : return error_mark_node;
6030 : : }
6031 : :
6032 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
6033 : : with attribute validation. */
6034 : 114019356 : initial = DECL_INITIAL (decl);
6035 : :
6036 : 114019356 : if (initialized)
6037 : : {
6038 : 50017757 : if (! toplevel_bindings_p ()
6039 : 50017757 : && DECL_EXTERNAL (decl))
6040 : 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
6041 : : decl);
6042 : 50017757 : DECL_EXTERNAL (decl) = 0;
6043 : 50017757 : if (toplevel_bindings_p ())
6044 : 4308196 : TREE_STATIC (decl) = 1;
6045 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
6046 : : even though we might not yet have the initializer expression. */
6047 : 50017757 : if (!DECL_INITIAL (decl))
6048 : 49666362 : DECL_INITIAL (decl) = error_mark_node;
6049 : : }
6050 : 114019356 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
6051 : :
6052 : 114019356 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
6053 : 3862 : record_key_method_defined (decl);
6054 : :
6055 : : /* If this is a typedef that names the class for linkage purposes
6056 : : (7.1.3p8), apply any attributes directly to the type. */
6057 : 114019356 : if (TREE_CODE (decl) == TYPE_DECL
6058 : 19942738 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
6059 : 121812291 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
6060 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
6061 : : else
6062 : : flags = 0;
6063 : :
6064 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
6065 : 114019356 : cplus_decl_attributes (&decl, attributes, flags);
6066 : :
6067 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
6068 : : assist with attribute validation. */
6069 : 114019356 : DECL_INITIAL (decl) = initial;
6070 : :
6071 : : /* Dllimported symbols cannot be defined. Static data members (which
6072 : : can be initialized in-class and dllimported) go through grokfield,
6073 : : not here, so we don't need to exclude those decls when checking for
6074 : : a definition. */
6075 : 114019356 : if (initialized && DECL_DLLIMPORT_P (decl))
6076 : : {
6077 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
6078 : : "definition of %q#D is marked %<dllimport%>", decl);
6079 : 0 : DECL_DLLIMPORT_P (decl) = 0;
6080 : : }
6081 : :
6082 : : /* If #pragma weak was used, mark the decl weak now. */
6083 : 114019356 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
6084 : 43932616 : maybe_apply_pragma_weak (decl);
6085 : :
6086 : 114019356 : if (TREE_CODE (decl) == FUNCTION_DECL
6087 : 38196520 : && DECL_DECLARED_INLINE_P (decl)
6088 : 1961029 : && DECL_UNINLINABLE (decl)
6089 : 114019356 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6090 : : {
6091 : 0 : auto_urlify_attributes sentinel;
6092 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6093 : : "inline function %qD given attribute %qs", decl, "noinline");
6094 : 0 : }
6095 : :
6096 : 114019356 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6097 : : {
6098 : 756688 : bool this_tmpl = (current_template_depth
6099 : 756688 : > template_class_depth (context));
6100 : 756688 : if (VAR_P (decl))
6101 : : {
6102 : 387911 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6103 : 387911 : if (field == NULL_TREE
6104 : 387911 : || !(VAR_P (field) || variable_template_p (field)))
6105 : 7 : error ("%q+#D is not a static data member of %q#T", decl, context);
6106 : 387904 : else if (variable_template_p (field)
6107 : 387904 : && (DECL_LANG_SPECIFIC (decl)
6108 : 111 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6109 : : /* OK, specialization was already checked. */;
6110 : 387841 : else if (variable_template_p (field) && !this_tmpl)
6111 : : {
6112 : 3 : auto_diagnostic_group d;
6113 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6114 : : "non-member-template declaration of %qD", decl);
6115 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6116 : : "member template declaration here");
6117 : 3 : return error_mark_node;
6118 : 3 : }
6119 : : else
6120 : : {
6121 : 387838 : if (variable_template_p (field))
6122 : 45 : field = DECL_TEMPLATE_RESULT (field);
6123 : :
6124 : 387838 : if (DECL_CONTEXT (field) != context)
6125 : : {
6126 : 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6127 : 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6128 : : "to be defined as %<%T::%D%>",
6129 : 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6130 : 3 : context, DECL_NAME (decl));
6131 : 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6132 : : }
6133 : : /* Static data member are tricky; an in-class initialization
6134 : : still doesn't provide a definition, so the in-class
6135 : : declaration will have DECL_EXTERNAL set, but will have an
6136 : : initialization. Thus, duplicate_decls won't warn
6137 : : about this situation, and so we check here. */
6138 : 387838 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6139 : 9 : error ("duplicate initialization of %qD", decl);
6140 : 387838 : field = duplicate_decls (decl, field);
6141 : 387838 : if (field == error_mark_node)
6142 : : return error_mark_node;
6143 : 387782 : else if (field)
6144 : 387782 : decl = field;
6145 : : }
6146 : : }
6147 : : else
6148 : : {
6149 : 368790 : tree field = check_classfn (context, decl,
6150 : : this_tmpl
6151 : 13 : ? current_template_parms
6152 : : : NULL_TREE);
6153 : 368705 : if (field && field != error_mark_node
6154 : 737482 : && duplicate_decls (decl, field))
6155 : 368704 : decl = field;
6156 : : }
6157 : :
6158 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6159 : 756629 : DECL_IN_AGGR_P (decl) = 0;
6160 : : /* Do not mark DECL as an explicit specialization if it was not
6161 : : already marked as an instantiation; a declaration should
6162 : : never be marked as a specialization unless we know what
6163 : : template is being specialized. */
6164 : 756629 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6165 : : {
6166 : 367141 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6167 : 367141 : if (TREE_CODE (decl) == FUNCTION_DECL)
6168 : 345880 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6169 : 345880 : && DECL_DECLARED_INLINE_P (decl));
6170 : : else
6171 : 21261 : DECL_COMDAT (decl) = false;
6172 : :
6173 : : /* [temp.expl.spec] An explicit specialization of a static data
6174 : : member of a template is a definition if the declaration
6175 : : includes an initializer; otherwise, it is a declaration.
6176 : :
6177 : : We check for processing_specialization so this only applies
6178 : : to the new specialization syntax. */
6179 : 367141 : if (!initialized && processing_specialization)
6180 : 366948 : DECL_EXTERNAL (decl) = 1;
6181 : : }
6182 : :
6183 : 1123646 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6184 : : /* Aliases are definitions. */
6185 : 756686 : && !alias)
6186 : : {
6187 : 30 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6188 : 11 : permerror (declarator->id_loc,
6189 : : "declaration of %q#D outside of class is not definition",
6190 : : decl);
6191 : 19 : else if (flag_contract_strict_declarations)
6192 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6193 : : "declaration of %q#D outside of class is not definition",
6194 : : decl);
6195 : : }
6196 : : }
6197 : :
6198 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6199 : 114019297 : if (initialized == SD_DECOMPOSITION)
6200 : 170609 : fit_decomposition_lang_decl (decl, NULL_TREE);
6201 : :
6202 : 114019297 : was_public = TREE_PUBLIC (decl);
6203 : :
6204 : 189612742 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6205 : 114370710 : && current_function_decl)
6206 : : {
6207 : : /* A function-scope decl of some namespace-scope decl. */
6208 : 37628 : DECL_LOCAL_DECL_P (decl) = true;
6209 : 37628 : if (named_module_attach_p ())
6210 : 3 : error_at (declarator->id_loc,
6211 : : "block-scope extern declaration %q#D must not be"
6212 : : " attached to a named module", decl);
6213 : : }
6214 : :
6215 : : /* Enter this declaration into the symbol table. Don't push the plain
6216 : : VAR_DECL for a variable template. */
6217 : 114019297 : if (!template_parm_scope_p ()
6218 : 114019297 : || !VAR_P (decl))
6219 : 111264893 : decl = maybe_push_decl (decl);
6220 : :
6221 : 114019297 : if (processing_template_decl)
6222 : 70080468 : decl = push_template_decl (decl);
6223 : :
6224 : 114019297 : if (decl == error_mark_node)
6225 : : return error_mark_node;
6226 : :
6227 : 114018874 : if (VAR_P (decl)
6228 : 55879798 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6229 : 66004 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6230 : : /* But not templated variables. */
6231 : 114046259 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6232 : : {
6233 : : /* This is a const variable with implicit 'static'. Set
6234 : : DECL_THIS_STATIC so we can tell it from variables that are
6235 : : !TREE_PUBLIC because of the anonymous namespace. */
6236 : 27385 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6237 : 27385 : DECL_THIS_STATIC (decl) = 1;
6238 : : }
6239 : :
6240 : 62226414 : if (current_function_decl && VAR_P (decl)
6241 : 50627058 : && maybe_constexpr_fn (current_function_decl)
6242 : 119933105 : && cxx_dialect < cxx23)
6243 : : {
6244 : 2822264 : bool ok = false;
6245 : 2822264 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6246 : : {
6247 : 10 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6248 : 20 : error_at (DECL_SOURCE_LOCATION (decl),
6249 : : "%qD defined %<thread_local%> in %qs function only "
6250 : : "available with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6251 : 10 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6252 : : ? "consteval" : "constexpr");
6253 : : }
6254 : 2822254 : else if (TREE_STATIC (decl))
6255 : : {
6256 : 52 : if (DECL_DECLARED_CONSTEXPR_P (current_function_decl))
6257 : 76 : error_at (DECL_SOURCE_LOCATION (decl),
6258 : : "%qD defined %<static%> in %qs function only available "
6259 : : "with %<-std=c++23%> or %<-std=gnu++23%>", decl,
6260 : 38 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6261 : : ? "consteval" : "constexpr");
6262 : : }
6263 : : else
6264 : : ok = true;
6265 : 48 : if (!ok)
6266 : 62 : cp_function_chain->invalid_constexpr = true;
6267 : : }
6268 : :
6269 : 114018874 : if (!processing_template_decl && VAR_P (decl))
6270 : 5389825 : start_decl_1 (decl, initialized);
6271 : :
6272 : 114018871 : return decl;
6273 : : }
6274 : :
6275 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6276 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6277 : : variable is initialized via an implicitly-called constructor.)
6278 : : This function must be called for ordinary variables (including, for
6279 : : example, implicit instantiations of templates), but must not be
6280 : : called for template declarations. */
6281 : :
6282 : : void
6283 : 5398557 : start_decl_1 (tree decl, bool initialized)
6284 : : {
6285 : 5398557 : gcc_checking_assert (!processing_template_decl);
6286 : :
6287 : 5398557 : if (error_operand_p (decl))
6288 : : return;
6289 : :
6290 : 5398542 : gcc_checking_assert (VAR_P (decl));
6291 : :
6292 : 5398542 : tree type = TREE_TYPE (decl);
6293 : 5398542 : bool complete_p = COMPLETE_TYPE_P (type);
6294 : 5398542 : bool aggregate_definition_p
6295 : 5398542 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6296 : :
6297 : : /* If an explicit initializer is present, or if this is a definition
6298 : : of an aggregate, then we need a complete type at this point.
6299 : : (Scalars are always complete types, so there is nothing to
6300 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6301 : : are issued below. */
6302 : 3373000 : if ((initialized || aggregate_definition_p)
6303 : 4394475 : && !complete_p
6304 : 4303508 : && COMPLETE_TYPE_P (complete_type (type)))
6305 : : {
6306 : 336376 : complete_p = true;
6307 : : /* We will not yet have set TREE_READONLY on DECL if the type
6308 : : was "const", but incomplete, before this point. But, now, we
6309 : : have a complete type, so we can try again. */
6310 : 336376 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6311 : : }
6312 : :
6313 : 5398539 : if (initialized)
6314 : : /* Is it valid for this decl to have an initializer at all? */
6315 : : {
6316 : : /* Don't allow initializations for incomplete types except for
6317 : : arrays which might be completed by the initialization. */
6318 : 3763765 : if (complete_p)
6319 : : ; /* A complete type is ok. */
6320 : 593905 : else if (type_uses_auto (type))
6321 : : ; /* An auto type is ok. */
6322 : 64265 : else if (TREE_CODE (type) != ARRAY_TYPE)
6323 : : {
6324 : 33 : auto_diagnostic_group d;
6325 : 33 : error ("variable %q#D has initializer but incomplete type", decl);
6326 : 60 : maybe_suggest_missing_header (input_location,
6327 : 33 : TYPE_IDENTIFIER (type),
6328 : 33 : CP_TYPE_CONTEXT (type));
6329 : 33 : type = TREE_TYPE (decl) = error_mark_node;
6330 : 33 : }
6331 : 64232 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6332 : : {
6333 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6334 : 0 : error ("elements of array %q#D have incomplete type", decl);
6335 : : /* else we already gave an error in start_decl. */
6336 : : }
6337 : : }
6338 : 1634774 : else if (aggregate_definition_p && !complete_p)
6339 : : {
6340 : 224 : if (type_uses_auto (type))
6341 : 64 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6342 : : else
6343 : : {
6344 : 160 : auto_diagnostic_group d;
6345 : 160 : error ("aggregate %q#D has incomplete type and cannot be defined",
6346 : : decl);
6347 : 275 : maybe_suggest_missing_header (input_location,
6348 : 160 : TYPE_IDENTIFIER (type),
6349 : 160 : CP_TYPE_CONTEXT (type));
6350 : : /* Change the type so that assemble_variable will give
6351 : : DECL an rtl we can live with: (mem (const_int 0)). */
6352 : 160 : type = TREE_TYPE (decl) = error_mark_node;
6353 : 160 : }
6354 : : }
6355 : :
6356 : : /* Create a new scope to hold this declaration if necessary.
6357 : : Whether or not a new scope is necessary cannot be determined
6358 : : until after the type has been completed; if the type is a
6359 : : specialization of a class template it is not until after
6360 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6361 : : will be set correctly. */
6362 : 5398539 : maybe_push_cleanup_level (type);
6363 : : }
6364 : :
6365 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6366 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6367 : :
6368 : : tree
6369 : 239 : do_aggregate_paren_init (tree init, tree type)
6370 : : {
6371 : 239 : tree val = TREE_VALUE (init);
6372 : :
6373 : 239 : if (TREE_CHAIN (init) == NULL_TREE)
6374 : : {
6375 : : /* If the list has a single element and it's a string literal,
6376 : : then it's the initializer for the array as a whole. */
6377 : 124 : if (TREE_CODE (type) == ARRAY_TYPE
6378 : 124 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6379 : 155 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6380 : : == STRING_CST)
6381 : : return val;
6382 : : /* Handle non-standard extensions like compound literals. This also
6383 : : prevents triggering aggregate parenthesized-initialization in
6384 : : compiler-generated code for =default. */
6385 : 97 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6386 : 97 : TREE_TYPE (val)))
6387 : : return val;
6388 : : }
6389 : :
6390 : 187 : init = build_constructor_from_list (init_list_type_node, init);
6391 : 187 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6392 : 187 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6393 : 187 : return init;
6394 : : }
6395 : :
6396 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6397 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6398 : : but will be set to a new CLEANUP_STMT if a temporary is created
6399 : : that must be destroyed subsequently.
6400 : :
6401 : : Returns an initializer expression to use to initialize DECL, or
6402 : : NULL if the initialization can be performed statically.
6403 : :
6404 : : Quotes on semantics can be found in ARM 8.4.3. */
6405 : :
6406 : : static tree
6407 : 317912 : grok_reference_init (tree decl, tree type, tree init, int flags)
6408 : : {
6409 : 317912 : if (init == NULL_TREE)
6410 : : {
6411 : 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6412 : 3 : || DECL_IN_AGGR_P (decl) == 0)
6413 : 18 : && ! DECL_THIS_EXTERN (decl))
6414 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
6415 : : "%qD declared as reference but not initialized", decl);
6416 : 15 : return NULL_TREE;
6417 : : }
6418 : :
6419 : 317897 : tree ttype = TREE_TYPE (type);
6420 : 317897 : if (TREE_CODE (init) == TREE_LIST)
6421 : : {
6422 : : /* This handles (C++20 only) code like
6423 : :
6424 : : const A& r(1, 2, 3);
6425 : :
6426 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6427 : 1211 : if (TREE_TYPE (init) == NULL_TREE
6428 : 1211 : && CP_AGGREGATE_TYPE_P (ttype)
6429 : 54 : && !DECL_DECOMPOSITION_P (decl)
6430 : 1262 : && (cxx_dialect >= cxx20))
6431 : : {
6432 : : /* We don't know yet if we should treat const A& r(1) as
6433 : : const A& r{1}. */
6434 : 33 : if (list_length (init) == 1)
6435 : : {
6436 : 12 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6437 : 12 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6438 : : tf_warning_or_error);
6439 : : }
6440 : : /* If the list had more than one element, the code is ill-formed
6441 : : pre-C++20, so we can build a constructor right away. */
6442 : : else
6443 : 21 : init = do_aggregate_paren_init (init, ttype);
6444 : : }
6445 : : else
6446 : 1178 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6447 : : tf_warning_or_error);
6448 : : }
6449 : :
6450 : 317897 : if (TREE_CODE (ttype) != ARRAY_TYPE
6451 : 317897 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6452 : : /* Note: default conversion is only called in very special cases. */
6453 : 12 : init = decay_conversion (init, tf_warning_or_error);
6454 : :
6455 : : /* check_initializer handles this for non-reference variables, but for
6456 : : references we need to do it here or the initializer will get the
6457 : : incomplete array type and confuse later calls to
6458 : : cp_complete_array_type. */
6459 : 317897 : if (TREE_CODE (ttype) == ARRAY_TYPE
6460 : 8496 : && TYPE_DOMAIN (ttype) == NULL_TREE
6461 : 317980 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6462 : 50 : || TREE_CODE (init) == STRING_CST))
6463 : : {
6464 : 33 : cp_complete_array_type (&ttype, init, false);
6465 : 33 : if (ttype != TREE_TYPE (type))
6466 : 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6467 : : }
6468 : :
6469 : : /* Convert INIT to the reference type TYPE. This may involve the
6470 : : creation of a temporary, whose lifetime must be the same as that
6471 : : of the reference. If so, a DECL_EXPR for the temporary will be
6472 : : added just after the DECL_EXPR for DECL. That's why we don't set
6473 : : DECL_INITIAL for local references (instead assigning to them
6474 : : explicitly); we need to allow the temporary to be initialized
6475 : : first. */
6476 : 317897 : return initialize_reference (type, init, flags,
6477 : 317897 : tf_warning_or_error);
6478 : : }
6479 : :
6480 : : /* Designated initializers in arrays are not supported in GNU C++.
6481 : : The parser cannot detect this error since it does not know whether
6482 : : a given brace-enclosed initializer is for a class type or for an
6483 : : array. This function checks that CE does not use a designated
6484 : : initializer. If it does, an error is issued. Returns true if CE
6485 : : is valid, i.e., does not have a designated initializer. */
6486 : :
6487 : : bool
6488 : 47746213 : check_array_designated_initializer (constructor_elt *ce,
6489 : : unsigned HOST_WIDE_INT index)
6490 : : {
6491 : : /* Designated initializers for array elements are not supported. */
6492 : 47746213 : if (ce->index)
6493 : : {
6494 : : /* The parser only allows identifiers as designated
6495 : : initializers. */
6496 : 30899585 : if (ce->index == error_mark_node)
6497 : : {
6498 : 0 : error ("name used in a GNU-style designated "
6499 : : "initializer for an array");
6500 : 0 : return false;
6501 : : }
6502 : 30899585 : else if (identifier_p (ce->index))
6503 : : {
6504 : 17 : error ("name %qD used in a GNU-style designated "
6505 : : "initializer for an array", ce->index);
6506 : 17 : return false;
6507 : : }
6508 : :
6509 : 30899568 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6510 : : ce->index, true);
6511 : 30899568 : if (ce_index
6512 : 30899565 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6513 : 61799130 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6514 : : == INTEGER_CST))
6515 : : {
6516 : : /* A C99 designator is OK if it matches the current index. */
6517 : 30899562 : if (wi::to_wide (ce_index) == index)
6518 : : {
6519 : 30899556 : ce->index = ce_index;
6520 : 30899556 : return true;
6521 : : }
6522 : : else
6523 : 6 : sorry ("non-trivial designated initializers not supported");
6524 : : }
6525 : : else
6526 : 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
6527 : : "C99 designator %qE is not an integral constant-expression",
6528 : : ce->index);
6529 : :
6530 : 12 : return false;
6531 : : }
6532 : :
6533 : : return true;
6534 : : }
6535 : :
6536 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
6537 : : array until we finish parsing the initializer. If that's the
6538 : : situation we're in, update DECL accordingly. */
6539 : :
6540 : : static void
6541 : 56371185 : maybe_deduce_size_from_array_init (tree decl, tree init)
6542 : : {
6543 : 56371185 : tree type = TREE_TYPE (decl);
6544 : :
6545 : 56371185 : if (TREE_CODE (type) == ARRAY_TYPE
6546 : 804842 : && TYPE_DOMAIN (type) == NULL_TREE
6547 : 56892870 : && TREE_CODE (decl) != TYPE_DECL)
6548 : : {
6549 : : /* do_default is really a C-ism to deal with tentative definitions.
6550 : : But let's leave it here to ease the eventual merge. */
6551 : 521685 : int do_default = !DECL_EXTERNAL (decl);
6552 : 521685 : tree initializer = init ? init : DECL_INITIAL (decl);
6553 : 521685 : int failure = 0;
6554 : :
6555 : : /* Check that there are no designated initializers in INIT, as
6556 : : those are not supported in GNU C++, and as the middle-end
6557 : : will crash if presented with a non-numeric designated
6558 : : initializer. */
6559 : 521685 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6560 : : {
6561 : 93811 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6562 : 93811 : constructor_elt *ce;
6563 : 93811 : HOST_WIDE_INT i, j = 0;
6564 : 15180388 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6565 : : {
6566 : 15088077 : if (instantiation_dependent_expression_p (ce->index))
6567 : 56371185 : return;
6568 : 15088071 : if (!check_array_designated_initializer (ce, j))
6569 : 2 : failure = 1;
6570 : : /* If an un-designated initializer is type-dependent, we can't
6571 : : check brace elision yet. */
6572 : 15088071 : if (ce->index == NULL_TREE
6573 : 15088071 : && type_dependent_expression_p (ce->value))
6574 : : return;
6575 : 15086577 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
6576 : 193 : j += RAW_DATA_LENGTH (ce->value);
6577 : : else
6578 : 15086384 : ++j;
6579 : : }
6580 : : }
6581 : :
6582 : 92311 : if (failure)
6583 : 2 : TREE_TYPE (decl) = error_mark_node;
6584 : : else
6585 : : {
6586 : 520183 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6587 : : do_default);
6588 : 520183 : if (failure == 1)
6589 : : {
6590 : 33 : error_at (cp_expr_loc_or_loc (initializer,
6591 : 30 : DECL_SOURCE_LOCATION (decl)),
6592 : : "initializer fails to determine size of %qD", decl);
6593 : : }
6594 : 520153 : else if (failure == 2)
6595 : : {
6596 : 30 : if (do_default)
6597 : : {
6598 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6599 : : "array size missing in %qD", decl);
6600 : : }
6601 : : /* If a `static' var's size isn't known, make it extern as
6602 : : well as static, so it does not get allocated. If it's not
6603 : : `static', then don't mark it extern; finish_incomplete_decl
6604 : : will give it a default size and it will get allocated. */
6605 : 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6606 : 0 : DECL_EXTERNAL (decl) = 1;
6607 : : }
6608 : 520123 : else if (failure == 3)
6609 : : {
6610 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6611 : : "zero-size array %qD", decl);
6612 : : }
6613 : : }
6614 : :
6615 : 520185 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6616 : :
6617 : 520185 : relayout_decl (decl);
6618 : : }
6619 : : }
6620 : :
6621 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6622 : : any appropriate error messages regarding the layout. */
6623 : :
6624 : : static void
6625 : 47840367 : layout_var_decl (tree decl)
6626 : : {
6627 : 47840367 : tree type;
6628 : :
6629 : 47840367 : type = TREE_TYPE (decl);
6630 : 47840367 : if (type == error_mark_node)
6631 : : return;
6632 : :
6633 : : /* If we haven't already laid out this declaration, do so now.
6634 : : Note that we must not call complete type for an external object
6635 : : because it's type might involve templates that we are not
6636 : : supposed to instantiate yet. (And it's perfectly valid to say
6637 : : `extern X x' for some incomplete type `X'.) */
6638 : 47840319 : if (!DECL_EXTERNAL (decl))
6639 : 39352021 : complete_type (type);
6640 : 47840319 : if (!DECL_SIZE (decl)
6641 : 514055 : && TREE_TYPE (decl) != error_mark_node
6642 : 48354374 : && complete_or_array_type_p (type))
6643 : 470927 : layout_decl (decl, 0);
6644 : :
6645 : 47840319 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6646 : : {
6647 : : /* An automatic variable with an incomplete type: that is an error.
6648 : : Don't talk about array types here, since we took care of that
6649 : : message in grokdeclarator. */
6650 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
6651 : : "storage size of %qD isn%'t known", decl);
6652 : 17 : TREE_TYPE (decl) = error_mark_node;
6653 : : }
6654 : : #if 0
6655 : : /* Keep this code around in case we later want to control debug info
6656 : : based on whether a type is "used". (jason 1999-11-11) */
6657 : :
6658 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6659 : : /* Let debugger know it should output info for this type. */
6660 : : note_debug_info_needed (ttype);
6661 : :
6662 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6663 : : note_debug_info_needed (DECL_CONTEXT (decl));
6664 : : #endif
6665 : :
6666 : 87192340 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6667 : 24843782 : && DECL_SIZE (decl) != NULL_TREE
6668 : 72506835 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
6669 : : {
6670 : 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6671 : 6 : && !DECL_LOCAL_DECL_P (decl))
6672 : 0 : constant_expression_warning (DECL_SIZE (decl));
6673 : : else
6674 : : {
6675 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6676 : : "storage size of %qD isn%'t constant", decl);
6677 : 6 : TREE_TYPE (decl) = error_mark_node;
6678 : 6 : type = error_mark_node;
6679 : : }
6680 : : }
6681 : :
6682 : : /* If the final element initializes a flexible array field, adjust
6683 : : the size of the DECL with the initializer based on whether the
6684 : : DECL is a union or a structure. */
6685 : 47840319 : if (type != error_mark_node
6686 : 47840313 : && DECL_INITIAL (decl)
6687 : 24745505 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6688 : 3767380 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6689 : 3127260 : && DECL_SIZE (decl) != NULL_TREE
6690 : 3127260 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6691 : 3127260 : && COMPLETE_TYPE_P (type)
6692 : 3127260 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6693 : 50967579 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6694 : : {
6695 : 3127260 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6696 : 3127260 : if (elt.index)
6697 : : {
6698 : 991787 : tree itype = TREE_TYPE (elt.index);
6699 : 991787 : tree vtype = TREE_TYPE (elt.value);
6700 : 991787 : if (TREE_CODE (itype) == ARRAY_TYPE
6701 : 50856 : && TYPE_DOMAIN (itype) == NULL
6702 : 272 : && TREE_CODE (vtype) == ARRAY_TYPE
6703 : 992059 : && COMPLETE_TYPE_P (vtype))
6704 : : {
6705 : : /* For a structure, add the size of the initializer to the DECL's
6706 : : size. */
6707 : 272 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
6708 : : {
6709 : 257 : DECL_SIZE (decl)
6710 : 257 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
6711 : : TYPE_SIZE (vtype));
6712 : 257 : DECL_SIZE_UNIT (decl)
6713 : 514 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6714 : : TYPE_SIZE_UNIT (vtype));
6715 : : }
6716 : : /* For a union, the DECL's size is the maximum of the current size
6717 : : and the size of the initializer. */
6718 : : else
6719 : : {
6720 : 15 : DECL_SIZE (decl)
6721 : 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
6722 : : TYPE_SIZE (vtype));
6723 : 15 : DECL_SIZE_UNIT (decl)
6724 : 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
6725 : : TYPE_SIZE_UNIT (vtype));
6726 : : }
6727 : : }
6728 : : }
6729 : : }
6730 : : }
6731 : :
6732 : : /* If a local static variable is declared in an inline function, or if
6733 : : we have a weak definition, we must endeavor to create only one
6734 : : instance of the variable at link-time. */
6735 : :
6736 : : void
6737 : 54376707 : maybe_commonize_var (tree decl)
6738 : : {
6739 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
6740 : : bindings. */
6741 : 54376707 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6742 : : return;
6743 : :
6744 : : /* Static data in a function with comdat linkage also has comdat
6745 : : linkage. */
6746 : 49401611 : if ((TREE_STATIC (decl)
6747 : 26056267 : && DECL_FUNCTION_SCOPE_P (decl)
6748 : 140853 : && vague_linkage_p (DECL_CONTEXT (decl)))
6749 : 101693141 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6750 : : {
6751 : 21663121 : if (flag_weak)
6752 : : {
6753 : : /* With weak symbols, we simply make the variable COMDAT;
6754 : : that will cause copies in multiple translations units to
6755 : : be merged. */
6756 : 21663094 : comdat_linkage (decl);
6757 : : }
6758 : : else
6759 : : {
6760 : 27 : if (DECL_INITIAL (decl) == NULL_TREE
6761 : 27 : || DECL_INITIAL (decl) == error_mark_node)
6762 : : {
6763 : : /* Without weak symbols, we can use COMMON to merge
6764 : : uninitialized variables. */
6765 : 24 : TREE_PUBLIC (decl) = 1;
6766 : 24 : DECL_COMMON (decl) = 1;
6767 : : }
6768 : : else
6769 : : {
6770 : : /* While for initialized variables, we must use internal
6771 : : linkage -- which means that multiple copies will not
6772 : : be merged. */
6773 : 3 : TREE_PUBLIC (decl) = 0;
6774 : 3 : DECL_COMMON (decl) = 0;
6775 : 3 : DECL_INTERFACE_KNOWN (decl) = 1;
6776 : 3 : const char *msg;
6777 : 3 : if (DECL_INLINE_VAR_P (decl))
6778 : : msg = G_("sorry: semantics of inline variable "
6779 : : "%q#D are wrong (you%'ll wind up with "
6780 : : "multiple copies)");
6781 : : else
6782 : : msg = G_("sorry: semantics of inline function "
6783 : : "static data %q#D are wrong (you%'ll wind "
6784 : : "up with multiple copies)");
6785 : 3 : auto_diagnostic_group d;
6786 : 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6787 : : msg, decl))
6788 : 3 : inform (DECL_SOURCE_LOCATION (decl),
6789 : : "you can work around this by removing the initializer");
6790 : 3 : }
6791 : : }
6792 : : }
6793 : : }
6794 : :
6795 : : /* Issue an error message if DECL is an uninitialized const variable.
6796 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6797 : : context from potential_constant_expression. Returns true if all is well,
6798 : : false otherwise. */
6799 : :
6800 : : bool
6801 : 6408990 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6802 : : tsubst_flags_t complain)
6803 : : {
6804 : 6408990 : tree type = strip_array_types (TREE_TYPE (decl));
6805 : :
6806 : : /* ``Unless explicitly declared extern, a const object does not have
6807 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
6808 : : 7.1.6 */
6809 : 6408990 : if (VAR_P (decl)
6810 : 6408990 : && !TYPE_REF_P (type)
6811 : 6392019 : && (CP_TYPE_CONST_P (type)
6812 : : /* C++20 permits trivial default initialization in constexpr
6813 : : context (P1331R2). */
6814 : 5352111 : || (cxx_dialect < cxx20
6815 : 2774360 : && (constexpr_context_p
6816 : 2475442 : || var_in_constexpr_fn (decl))))
6817 : 7859671 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6818 : : {
6819 : 652413 : tree field = default_init_uninitialized_part (type);
6820 : 652413 : if (!field)
6821 : : return true;
6822 : :
6823 : 113571 : auto_diagnostic_group d;
6824 : 113571 : bool show_notes = true;
6825 : :
6826 : 113571 : if (!constexpr_context_p || cxx_dialect >= cxx20)
6827 : : {
6828 : 109811 : if (CP_TYPE_CONST_P (type))
6829 : : {
6830 : 90 : if (complain & tf_error)
6831 : 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6832 : : "uninitialized %<const %D%>", decl);
6833 : : }
6834 : : else
6835 : : {
6836 : 109721 : if (!is_instantiation_of_constexpr (current_function_decl)
6837 : 109721 : && (complain & tf_error))
6838 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
6839 : : "uninitialized variable %qD in %<constexpr%> "
6840 : : "function", decl);
6841 : : else
6842 : : show_notes = false;
6843 : 109721 : cp_function_chain->invalid_constexpr = true;
6844 : : }
6845 : : }
6846 : 3760 : else if (complain & tf_error)
6847 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
6848 : : "uninitialized variable %qD in %<constexpr%> context",
6849 : : decl);
6850 : :
6851 : 113571 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6852 : : {
6853 : 62 : tree defaulted_ctor;
6854 : :
6855 : 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6856 : : "%q#T has no user-provided default constructor", type);
6857 : 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
6858 : 62 : if (defaulted_ctor)
6859 : 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6860 : : "constructor is not user-provided because it is "
6861 : : "explicitly defaulted in the class body");
6862 : 62 : inform (DECL_SOURCE_LOCATION (field),
6863 : : "and the implicitly-defined constructor does not "
6864 : : "initialize %q#D", field);
6865 : : }
6866 : :
6867 : 113571 : return false;
6868 : 113571 : }
6869 : :
6870 : : return true;
6871 : : }
6872 : :
6873 : : /* Structure holding the current initializer being processed by reshape_init.
6874 : : CUR is a pointer to the current element being processed, END is a pointer
6875 : : after the last element present in the initializer and RAW_IDX is index into
6876 : : RAW_DATA_CST if that is CUR elt. */
6877 : : struct reshape_iter
6878 : : {
6879 : : constructor_elt *cur;
6880 : : constructor_elt *end;
6881 : : unsigned raw_idx;
6882 : : };
6883 : :
6884 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6885 : :
6886 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6887 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
6888 : : initialized as if for an aggregate class. If there are no more such fields,
6889 : : the return value will be NULL. */
6890 : :
6891 : : tree
6892 : 14274409 : next_aggregate_field (tree field)
6893 : : {
6894 : 14274409 : while (field
6895 : 49971882 : && (TREE_CODE (field) != FIELD_DECL
6896 : 9161810 : || DECL_UNNAMED_BIT_FIELD (field)
6897 : 9161688 : || (DECL_ARTIFICIAL (field)
6898 : : /* In C++17, aggregates can have bases. */
6899 : 74683 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6900 : 35697473 : field = DECL_CHAIN (field);
6901 : :
6902 : 14274409 : return field;
6903 : : }
6904 : :
6905 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6906 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6907 : : to a subobject. If there are no more such fields, the return value will be
6908 : : NULL. */
6909 : :
6910 : : tree
6911 : 54672558 : next_subobject_field (tree field)
6912 : : {
6913 : 54672558 : while (field
6914 : 663819884 : && (TREE_CODE (field) != FIELD_DECL
6915 : 25251573 : || DECL_UNNAMED_BIT_FIELD (field)
6916 : 25251493 : || (DECL_ARTIFICIAL (field)
6917 : 6940369 : && !DECL_FIELD_IS_BASE (field)
6918 : 1726015 : && !DECL_VIRTUAL_P (field))))
6919 : 609147326 : field = DECL_CHAIN (field);
6920 : :
6921 : 54672558 : return field;
6922 : : }
6923 : :
6924 : : /* Return true for [dcl.init.list] direct-list-initialization from
6925 : : single element of enumeration with a fixed underlying type. */
6926 : :
6927 : : bool
6928 : 124918482 : is_direct_enum_init (tree type, tree init)
6929 : : {
6930 : 124918482 : if (cxx_dialect >= cxx17
6931 : 122980806 : && TREE_CODE (type) == ENUMERAL_TYPE
6932 : 6182804 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6933 : 3932967 : && TREE_CODE (init) == CONSTRUCTOR
6934 : 407775 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
6935 : 206088 : && CONSTRUCTOR_NELTS (init) == 1
6936 : 218 : && TREE_CODE (CONSTRUCTOR_ELT (init, 0)->value) != RAW_DATA_CST
6937 : : /* DR 2374: The single element needs to be implicitly
6938 : : convertible to the underlying type of the enum. */
6939 : 218 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6940 : 124918698 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6941 : 216 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6942 : 216 : CONSTRUCTOR_ELT (init, 0)->value,
6943 : : LOOKUP_IMPLICIT, tf_none))
6944 : : return true;
6945 : : return false;
6946 : : }
6947 : :
6948 : : /* Helper function for reshape_init*. Split first element of
6949 : : RAW_DATA_CST or return NULL for other elements. Set *INC_CUR
6950 : : to true if the whole d->cur has been consumed. */
6951 : :
6952 : : static tree
6953 : 22592490 : cp_maybe_split_raw_data (reshape_iter *d, bool *inc_cur)
6954 : : {
6955 : 22592490 : *inc_cur = true;
6956 : 22592490 : if (TREE_CODE (d->cur->value) != RAW_DATA_CST)
6957 : : return NULL_TREE;
6958 : 30720 : tree ret = *raw_data_iterator (d->cur->value, d->raw_idx++);
6959 : 30720 : if (d->raw_idx != (unsigned) RAW_DATA_LENGTH (d->cur->value))
6960 : 30588 : *inc_cur = false;
6961 : : else
6962 : 132 : d->raw_idx = 0;
6963 : : return ret;
6964 : : }
6965 : :
6966 : : /* Wrapper around that which for RAW_DATA_CST in INIT
6967 : : (as well as in D->cur->value) peels off the first element
6968 : : of the raw data and returns it, otherwise increments
6969 : : D->cur and returns INIT. */
6970 : :
6971 : : static tree
6972 : 22592474 : consume_init (tree init, reshape_iter *d)
6973 : : {
6974 : 22592474 : bool inc_cur;
6975 : 22592474 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
6976 : 30720 : init = raw_init;
6977 : 22592474 : if (inc_cur)
6978 : 22561886 : d->cur++;
6979 : 22592474 : return init;
6980 : : }
6981 : :
6982 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
6983 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6984 : : INTEGER_CST representing the size of the array minus one (the maximum index),
6985 : : or NULL_TREE if the array was declared without specifying the size. D is
6986 : : the iterator within the constructor. */
6987 : :
6988 : : static tree
6989 : 360341 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6990 : : tree first_initializer_p, bool vector_p,
6991 : : tsubst_flags_t complain)
6992 : : {
6993 : 360341 : tree new_init;
6994 : 360341 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6995 : 360341 : unsigned HOST_WIDE_INT max_index_cst = 0;
6996 : 360341 : unsigned HOST_WIDE_INT index;
6997 : :
6998 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
6999 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
7000 : : to build a new one. But don't reuse if not complaining; if this is
7001 : : tentative, we might also reshape to another type (95319). */
7002 : 360341 : bool reuse = (first_initializer_p
7003 : 312196 : && (complain & tf_error)
7004 : 310943 : && !CP_AGGREGATE_TYPE_P (elt_type)
7005 : 622292 : && !TREE_SIDE_EFFECTS (first_initializer_p));
7006 : 105991 : if (reuse)
7007 : : new_init = first_initializer_p;
7008 : : else
7009 : 105991 : new_init = build_constructor (init_list_type_node, NULL);
7010 : :
7011 : 360341 : if (sized_array_p)
7012 : : {
7013 : 249996 : poly_uint64 midx;
7014 : : /* Minus 1 is used for zero sized arrays. */
7015 : 249996 : if (integer_all_onesp (max_index))
7016 : 24 : return new_init;
7017 : :
7018 : 249972 : if (tree_fits_poly_uint64_p (max_index))
7019 : 249972 : midx = tree_to_poly_uint64 (max_index);
7020 : : /* sizetype is sign extended, not zero extended. */
7021 : : else
7022 : 0 : midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
7023 : :
7024 : : /* For VLA vectors, we restict the number of elements in the constructor
7025 : : to lower bound of the VLA elements. */
7026 : 249972 : max_index_cst = constant_lower_bound (midx);
7027 : : }
7028 : :
7029 : 360317 : constructor_elt *first_cur = d->cur;
7030 : : /* Loop until there are no more initializers. */
7031 : 17453084 : for (index = 0;
7032 : 17453084 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
7033 : : ++index)
7034 : : {
7035 : 17092949 : tree elt_init;
7036 : 17092949 : constructor_elt *old_cur = d->cur;
7037 : 17092949 : unsigned int old_raw_idx = d->raw_idx;
7038 : 17092949 : bool old_raw_data_cst = TREE_CODE (d->cur->value) == RAW_DATA_CST;
7039 : :
7040 : 17092949 : if (d->cur->index)
7041 : 1488108 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7042 : 17092949 : check_array_designated_initializer (d->cur, index);
7043 : 17092949 : if (TREE_CODE (d->cur->value) == RAW_DATA_CST
7044 : 32693 : && (TREE_CODE (elt_type) == INTEGER_TYPE
7045 : 10233 : || is_byte_access_type (elt_type))
7046 : 22460 : && TYPE_PRECISION (elt_type) == CHAR_BIT
7047 : 1799 : && (!sized_array_p || index < max_index_cst)
7048 : 17094736 : && !vector_p)
7049 : : {
7050 : 467 : elt_init = d->cur->value;
7051 : 467 : unsigned int off = d->raw_idx;
7052 : 467 : unsigned int len = RAW_DATA_LENGTH (elt_init) - off;
7053 : 467 : if (!sized_array_p || len <= max_index_cst - index + 1)
7054 : : {
7055 : 458 : d->cur++;
7056 : 458 : d->raw_idx = 0;
7057 : : }
7058 : : else
7059 : : {
7060 : 9 : len = max_index_cst - index + 1;
7061 : 9 : d->raw_idx += len;
7062 : : }
7063 : 467 : if (!reuse || off || d->cur == old_cur)
7064 : : {
7065 : 25 : elt_init = copy_node (elt_init);
7066 : 25 : RAW_DATA_LENGTH (elt_init) = len;
7067 : 25 : RAW_DATA_POINTER (elt_init) += off;
7068 : : }
7069 : 467 : TREE_TYPE (elt_init) = elt_type;
7070 : : }
7071 : : else
7072 : 17092482 : elt_init = reshape_init_r (elt_type, d,
7073 : : /*first_initializer_p=*/NULL_TREE,
7074 : : complain);
7075 : 17092949 : if (elt_init == error_mark_node)
7076 : : return error_mark_node;
7077 : 17092770 : tree idx = size_int (index);
7078 : 17092770 : if (reuse && old_raw_data_cst && d->cur == old_cur)
7079 : : {
7080 : : /* We need to stop reusing as some RAW_DATA_CST in the original
7081 : : ctor had to be split. */
7082 : 72 : new_init = build_constructor (init_list_type_node, NULL);
7083 : 72 : if (index)
7084 : : {
7085 : 72 : vec_safe_grow (CONSTRUCTOR_ELTS (new_init), index);
7086 : 72 : memcpy (CONSTRUCTOR_ELT (new_init, 0), first_cur,
7087 : 72 : (d->cur - first_cur)
7088 : : * sizeof (*CONSTRUCTOR_ELT (new_init, 0)));
7089 : 72 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (first_initializer_p))
7090 : : {
7091 : : unsigned int j;
7092 : : tree nidx, nval;
7093 : 4 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (new_init),
7094 : : j, nidx, nval)
7095 : 4 : if (nidx)
7096 : : {
7097 : 4 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = 1;
7098 : 4 : (void) nval;
7099 : 4 : break;
7100 : : }
7101 : : }
7102 : : }
7103 : : reuse = false;
7104 : : }
7105 : 17092698 : if (reuse)
7106 : : {
7107 : 15317639 : old_cur->index = idx;
7108 : 15317639 : old_cur->value = elt_init;
7109 : : }
7110 : : else
7111 : 1775131 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7112 : : idx, elt_init);
7113 : 17092770 : if (!TREE_CONSTANT (elt_init))
7114 : 167906 : TREE_CONSTANT (new_init) = false;
7115 : :
7116 : : /* This can happen with an invalid initializer (c++/54501). */
7117 : 17092770 : if (d->cur == old_cur
7118 : 32082 : && !sized_array_p
7119 : 14085 : && d->raw_idx == old_raw_idx)
7120 : : break;
7121 : :
7122 : 17092767 : if (TREE_CODE (elt_init) == RAW_DATA_CST)
7123 : 467 : index += RAW_DATA_LENGTH (elt_init) - 1;
7124 : : }
7125 : :
7126 : : return new_init;
7127 : : }
7128 : :
7129 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
7130 : : Parameters are the same of reshape_init_r. */
7131 : :
7132 : : static tree
7133 : 319149 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
7134 : : tsubst_flags_t complain)
7135 : : {
7136 : 319149 : tree max_index = NULL_TREE;
7137 : :
7138 : 319149 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7139 : :
7140 : 319149 : if (TYPE_DOMAIN (type))
7141 : 208849 : max_index = array_type_nelts_minus_one (type);
7142 : :
7143 : 319149 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7144 : 319149 : first_initializer_p, false, complain);
7145 : : }
7146 : :
7147 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
7148 : : Parameters are the same of reshape_init_r. */
7149 : :
7150 : : static tree
7151 : 41192 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
7152 : : {
7153 : 41192 : tree max_index = NULL_TREE;
7154 : :
7155 : 41192 : gcc_assert (VECTOR_TYPE_P (type));
7156 : :
7157 : 41192 : if (COMPOUND_LITERAL_P (d->cur->value))
7158 : : {
7159 : 0 : tree value = d->cur->value;
7160 : 0 : if (!same_type_p (TREE_TYPE (value), type))
7161 : : {
7162 : 0 : if (complain & tf_error)
7163 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
7164 : 0 : TREE_TYPE (d->cur->value), type);
7165 : 0 : value = error_mark_node;
7166 : : }
7167 : 0 : ++d->cur;
7168 : 0 : return value;
7169 : : }
7170 : :
7171 : : /* For a vector, we initialize it as an array of the appropriate size. */
7172 : 41192 : if (VECTOR_TYPE_P (type))
7173 : 41192 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
7174 : :
7175 : 41192 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
7176 : 41192 : NULL_TREE, true, complain);
7177 : : }
7178 : :
7179 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
7180 : : INIT, in isolation from any designator or other initializers. */
7181 : :
7182 : : static tree
7183 : 575978 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
7184 : : {
7185 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
7186 : : calling reshape_init, but this way can just live on the stack. */
7187 : 575978 : constructor_elt elt = { /*index=*/NULL_TREE, init };
7188 : 575978 : reshape_iter iter = { &elt, &elt + 1, 0 };
7189 : 575978 : return reshape_init_r (type, &iter,
7190 : : /*first_initializer_p=*/NULL_TREE,
7191 : 575978 : complain);
7192 : : }
7193 : :
7194 : : /* Subroutine of reshape_init_r, processes the initializers for classes
7195 : : or union. Parameters are the same of reshape_init_r. */
7196 : :
7197 : : static tree
7198 : 3604819 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
7199 : : tsubst_flags_t complain)
7200 : : {
7201 : 3604819 : tree field;
7202 : 3604819 : tree new_init;
7203 : :
7204 : 3604819 : gcc_assert (CLASS_TYPE_P (type));
7205 : :
7206 : : /* The initializer for a class is always a CONSTRUCTOR. */
7207 : 3604819 : new_init = build_constructor (init_list_type_node, NULL);
7208 : :
7209 : 3604819 : int binfo_idx = -1;
7210 : 3604819 : tree binfo = TYPE_BINFO (type);
7211 : 3604819 : tree base_binfo = NULL_TREE;
7212 : 3604819 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7213 : : {
7214 : : /* We get here from maybe_aggr_guide for C++20 class template argument
7215 : : deduction. In this case we need to look through the binfo because a
7216 : : template doesn't have base fields. */
7217 : 338 : binfo_idx = 0;
7218 : 338 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7219 : : }
7220 : 67 : if (base_binfo)
7221 : : field = base_binfo;
7222 : : else
7223 : 3604752 : field = next_aggregate_field (TYPE_FIELDS (type));
7224 : :
7225 : 3604819 : if (!field)
7226 : : {
7227 : : /* [dcl.init.aggr]
7228 : :
7229 : : An initializer for an aggregate member that is an
7230 : : empty class shall have the form of an empty
7231 : : initializer-list {}. */
7232 : 324 : if (!first_initializer_p)
7233 : : {
7234 : 270 : if (complain & tf_error)
7235 : 13 : error ("initializer for %qT must be brace-enclosed", type);
7236 : 270 : return error_mark_node;
7237 : : }
7238 : : return new_init;
7239 : : }
7240 : :
7241 : : /* For C++20 CTAD, handle pack expansions in the base list. */
7242 : : tree last_was_pack_expansion = NULL_TREE;
7243 : :
7244 : : /* Loop through the initializable fields, gathering initializers. */
7245 : 11007078 : while (d->cur != d->end)
7246 : : {
7247 : 7513578 : tree field_init;
7248 : 7513578 : constructor_elt *old_cur = d->cur;
7249 : 7513578 : unsigned old_raw_idx = d->raw_idx;
7250 : 7513578 : bool direct_desig = false;
7251 : :
7252 : : /* Handle C++20 designated initializers. */
7253 : 7513578 : if (d->cur->index)
7254 : : {
7255 : 576138 : if (d->cur->index == error_mark_node)
7256 : : return error_mark_node;
7257 : :
7258 : 576132 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7259 : : {
7260 : : /* We already reshaped this; we should have returned early from
7261 : : reshape_init. */
7262 : 0 : gcc_checking_assert (false);
7263 : : if (field != d->cur->index)
7264 : : {
7265 : : if (tree id = DECL_NAME (d->cur->index))
7266 : : gcc_checking_assert (d->cur->index
7267 : : == get_class_binding (type, id));
7268 : : field = d->cur->index;
7269 : : }
7270 : : }
7271 : 576132 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7272 : : {
7273 : 576126 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7274 : 576126 : field = get_class_binding (type, d->cur->index);
7275 : 576126 : direct_desig = true;
7276 : : }
7277 : : else
7278 : : {
7279 : 6 : if (complain & tf_error)
7280 : 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7281 : : " for class %qT", d->cur->index, type);
7282 : 6 : return error_mark_node;
7283 : : }
7284 : :
7285 : 576126 : if (!field && ANON_AGGR_TYPE_P (type))
7286 : : /* Apparently the designator isn't for a member of this anonymous
7287 : : struct, so head back to the enclosing class. */
7288 : : break;
7289 : :
7290 : 576123 : if (!field || TREE_CODE (field) != FIELD_DECL)
7291 : : {
7292 : 18 : if (complain & tf_error)
7293 : : {
7294 : 6 : if (field && TREE_CODE (field) == TREE_LIST)
7295 : : {
7296 : 3 : auto_diagnostic_group g;
7297 : 3 : error ("request for member %qD is ambiguous",
7298 : 3 : d->cur->index);
7299 : 3 : print_candidates (field);
7300 : 3 : }
7301 : : else
7302 : 3 : error ("%qT has no non-static data member named %qD", type,
7303 : 3 : d->cur->index);
7304 : : }
7305 : 18 : return error_mark_node;
7306 : : }
7307 : :
7308 : : /* If the element is an anonymous union object and the initializer
7309 : : list is a designated-initializer-list, the anonymous union object
7310 : : is initialized by the designated-initializer-list { D }, where D
7311 : : is the designated-initializer-clause naming a member of the
7312 : : anonymous union object. */
7313 : 576105 : tree ictx = DECL_CONTEXT (field);
7314 : 576105 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7315 : : {
7316 : : /* Find the anon aggr that is a direct member of TYPE. */
7317 : 139 : while (ANON_AGGR_TYPE_P (ictx))
7318 : : {
7319 : 136 : tree cctx = TYPE_CONTEXT (ictx);
7320 : 136 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7321 : 124 : goto found;
7322 : : ictx = cctx;
7323 : : }
7324 : :
7325 : : /* Not found, e.g. FIELD is a member of a base class. */
7326 : 3 : if (complain & tf_error)
7327 : 3 : error ("%qD is not a direct member of %qT", field, type);
7328 : 3 : return error_mark_node;
7329 : :
7330 : 124 : found:
7331 : : /* Now find the TYPE member with that anon aggr type. */
7332 : 124 : tree aafield = TYPE_FIELDS (type);
7333 : 1067 : for (; aafield; aafield = TREE_CHAIN (aafield))
7334 : 1067 : if (TREE_TYPE (aafield) == ictx)
7335 : : break;
7336 : 0 : gcc_assert (aafield);
7337 : : field = aafield;
7338 : : direct_desig = false;
7339 : : }
7340 : : }
7341 : :
7342 : : /* If we processed all the member of the class, we are done. */
7343 : 7513418 : if (!field)
7344 : : break;
7345 : :
7346 : 15026235 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7347 : 7513127 : ? field : NULL_TREE);
7348 : 7513127 : if (last_was_pack_expansion)
7349 : : /* Each non-trailing aggregate element that is a pack expansion is
7350 : : assumed to correspond to no elements of the initializer list. */
7351 : 19 : goto continue_;
7352 : :
7353 : 7513108 : if (direct_desig)
7354 : : {
7355 : : /* The designated field F is initialized from this one element.
7356 : :
7357 : : Note that we don't want to do this if we found the designator
7358 : : inside an anon aggr above; we use the normal code to implement:
7359 : :
7360 : : "If the element is an anonymous union member and the initializer
7361 : : list is a brace-enclosed designated- initializer-list, the element
7362 : : is initialized by the designated-initializer-list { D }, where D
7363 : : is the designated- initializer-clause naming a member of the
7364 : : anonymous union member." */
7365 : 575978 : gcc_checking_assert (TREE_CODE (d->cur->value) != RAW_DATA_CST);
7366 : 575978 : field_init = reshape_single_init (TREE_TYPE (field),
7367 : : d->cur->value, complain);
7368 : 575978 : d->cur++;
7369 : : }
7370 : : else
7371 : 6937130 : field_init = reshape_init_r (TREE_TYPE (field), d,
7372 : : /*first_initializer_p=*/NULL_TREE,
7373 : : complain);
7374 : :
7375 : 7513108 : if (field_init == error_mark_node)
7376 : : return error_mark_node;
7377 : :
7378 : 7512819 : if (d->cur == old_cur && d->cur->index && d->raw_idx == old_raw_idx)
7379 : : {
7380 : : /* This can happen with an invalid initializer for a flexible
7381 : : array member (c++/54441). */
7382 : 0 : if (complain & tf_error)
7383 : 0 : error ("invalid initializer for %q#D", field);
7384 : 0 : return error_mark_node;
7385 : : }
7386 : :
7387 : 7512819 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7388 : :
7389 : : /* [dcl.init.aggr]
7390 : :
7391 : : When a union is initialized with a brace-enclosed
7392 : : initializer, the braces shall only contain an
7393 : : initializer for the first member of the union. */
7394 : 7512819 : if (TREE_CODE (type) == UNION_TYPE)
7395 : : break;
7396 : :
7397 : 7402564 : continue_:
7398 : 7402583 : if (base_binfo)
7399 : : {
7400 : 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7401 : : field = base_binfo;
7402 : : else
7403 : 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7404 : : }
7405 : : else
7406 : 7402501 : field = next_aggregate_field (DECL_CHAIN (field));
7407 : : }
7408 : :
7409 : : /* A trailing aggregate element that is a pack expansion is assumed to
7410 : : correspond to all remaining elements of the initializer list (if any). */
7411 : 3604173 : if (last_was_pack_expansion)
7412 : : {
7413 : 16 : tree init = d->cur->value;
7414 : 16 : bool inc_cur;
7415 : 16 : if (tree raw_init = cp_maybe_split_raw_data (d, &inc_cur))
7416 : 0 : init = raw_init;
7417 : 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7418 : : last_was_pack_expansion, init);
7419 : 46 : while (d->cur != d->end)
7420 : 30 : d->cur++;
7421 : : }
7422 : :
7423 : : return new_init;
7424 : : }
7425 : :
7426 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7427 : : designators are not valid; either complain or return true to indicate
7428 : : that reshape_init_r should return error_mark_node. */
7429 : :
7430 : : static bool
7431 : 63645 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7432 : : {
7433 : 63645 : if (d->cur->index)
7434 : : {
7435 : 30 : if (complain & tf_error)
7436 : 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7437 : : "C99 designator %qE outside aggregate initializer",
7438 : : d->cur->index);
7439 : : else
7440 : : return true;
7441 : : }
7442 : : return false;
7443 : : }
7444 : :
7445 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7446 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7447 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7448 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7449 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7450 : :
7451 : : static tree
7452 : 28626502 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7453 : : tsubst_flags_t complain)
7454 : : {
7455 : 28626502 : tree init = d->cur->value;
7456 : :
7457 : 28626502 : if (error_operand_p (init))
7458 : 120 : return error_mark_node;
7459 : :
7460 : 4020795 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7461 : 28689861 : && has_designator_problem (d, complain))
7462 : 0 : return error_mark_node;
7463 : :
7464 : 28626382 : tree stripped_init = tree_strip_any_location_wrapper (init);
7465 : :
7466 : 28626382 : if (TREE_CODE (type) == COMPLEX_TYPE)
7467 : : {
7468 : : /* A complex type can be initialized from one or two initializers,
7469 : : but braces are not elided. */
7470 : 588 : init = consume_init (init, d);
7471 : 588 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7472 : : {
7473 : 9 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7474 : : {
7475 : 0 : if (complain & tf_error)
7476 : 0 : error ("too many initializers for %qT", type);
7477 : : else
7478 : 0 : return error_mark_node;
7479 : : }
7480 : : }
7481 : 579 : else if (first_initializer_p && d->cur != d->end)
7482 : : {
7483 : 36 : if (error_operand_p (d->cur->value)
7484 : 36 : || has_designator_problem (d, complain))
7485 : 0 : return error_mark_node;
7486 : 36 : vec<constructor_elt, va_gc> *v = 0;
7487 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7488 : 36 : init = consume_init (d->cur->value, d);
7489 : 36 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7490 : 36 : init = build_constructor (init_list_type_node, v);
7491 : : }
7492 : 588 : return init;
7493 : : }
7494 : :
7495 : : /* A non-aggregate type is always initialized with a single
7496 : : initializer. */
7497 : 57201688 : if (!CP_AGGREGATE_TYPE_P (type)
7498 : : /* As is an array with dependent bound, which we can see
7499 : : during C++20 aggregate CTAD. */
7500 : 34613323 : || (cxx_dialect >= cxx20
7501 : 3852371 : && TREE_CODE (type) == ARRAY_TYPE
7502 : 155537 : && uses_template_parms (TYPE_DOMAIN (type))))
7503 : : {
7504 : : /* It is invalid to initialize a non-aggregate type with a
7505 : : brace-enclosed initializer before C++0x.
7506 : : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7507 : : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7508 : : a CONSTRUCTOR (with a record type). */
7509 : 22588376 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
7510 : : /* Don't complain about a capture-init. */
7511 : 306061 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7512 : 22884430 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7513 : : {
7514 : 296015 : if (SCALAR_TYPE_P (type))
7515 : : {
7516 : 726 : if (cxx_dialect < cxx11)
7517 : : {
7518 : 16 : if (complain & tf_error)
7519 : 16 : error ("braces around scalar initializer for type %qT",
7520 : : type);
7521 : 16 : init = error_mark_node;
7522 : : }
7523 : 710 : else if (first_initializer_p
7524 : 710 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
7525 : 546 : && (BRACE_ENCLOSED_INITIALIZER_P
7526 : : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
7527 : : {
7528 : 13 : if (complain & tf_error)
7529 : 13 : error ("too many braces around scalar initializer "
7530 : : "for type %qT", type);
7531 : 13 : init = error_mark_node;
7532 : : }
7533 : : }
7534 : : else
7535 : 295289 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7536 : : }
7537 : 22588376 : return consume_init (init, d);
7538 : : }
7539 : :
7540 : : /* "If T is a class type and the initializer list has a single element of
7541 : : type cv U, where U is T or a class derived from T, the object is
7542 : : initialized from that element." Even if T is an aggregate. */
7543 : 6015323 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7544 : 5589674 : && first_initializer_p
7545 : : /* But not if it's a designated init. */
7546 : 3632333 : && !d->cur->index
7547 : 3388304 : && d->end - d->cur == 1
7548 : 430869 : && TREE_CODE (init) != RAW_DATA_CST
7549 : 6468287 : && reference_related_p (type, TREE_TYPE (init)))
7550 : : {
7551 : 416 : d->cur++;
7552 : 416 : return init;
7553 : : }
7554 : :
7555 : : /* [dcl.init.aggr]
7556 : :
7557 : : All implicit type conversions (clause _conv_) are considered when
7558 : : initializing the aggregate member with an initializer from an
7559 : : initializer-list. If the initializer can initialize a member,
7560 : : the member is initialized. Otherwise, if the member is itself a
7561 : : non-empty subaggregate, brace elision is assumed and the
7562 : : initializer is considered for the initialization of the first
7563 : : member of the subaggregate. */
7564 : 2638547 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7565 : : /* But don't try this for the first initializer, since that would be
7566 : : looking through the outermost braces; A a2 = { a1 }; is not a
7567 : : valid aggregate initialization. */
7568 : 3398515 : && !first_initializer_p
7569 : 6049607 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7570 : 9169 : || can_convert_arg (type, TREE_TYPE (init),
7571 : 9169 : TREE_CODE (init) == RAW_DATA_CST
7572 : 6038608 : ? build_int_cst (integer_type_node,
7573 : 1606 : *(const unsigned char *)
7574 : 1606 : RAW_DATA_POINTER (init))
7575 : : : init,
7576 : : LOOKUP_NORMAL, complain)))
7577 : 3474 : return consume_init (init, d);
7578 : :
7579 : : /* [dcl.init.string]
7580 : :
7581 : : A char array (whether plain char, signed char, or unsigned char)
7582 : : can be initialized by a string-literal (optionally enclosed in
7583 : : braces); a wchar_t array can be initialized by a wide
7584 : : string-literal (optionally enclosed in braces). */
7585 : 6033528 : if (TREE_CODE (type) == ARRAY_TYPE
7586 : 6033528 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7587 : : {
7588 : 88268 : tree str_init = init;
7589 : 88268 : tree stripped_str_init = stripped_init;
7590 : 88268 : reshape_iter stripd = {};
7591 : :
7592 : : /* Strip one level of braces if and only if they enclose a single
7593 : : element (as allowed by [dcl.init.string]). */
7594 : 88268 : if (!first_initializer_p
7595 : 12357 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7596 : 99525 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7597 : : {
7598 : 375 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7599 : 375 : str_init = stripd.cur->value;
7600 : 375 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
7601 : : }
7602 : :
7603 : : /* If it's a string literal, then it's the initializer for the array
7604 : : as a whole. Otherwise, continue with normal initialization for
7605 : : array types (one value per array element). */
7606 : 88268 : if (TREE_CODE (stripped_str_init) == STRING_CST)
7607 : : {
7608 : 72 : if ((first_initializer_p && has_designator_problem (d, complain))
7609 : 1025 : || (stripd.cur && has_designator_problem (&stripd, complain)))
7610 : 953 : return error_mark_node;
7611 : 953 : d->cur++;
7612 : 953 : return str_init;
7613 : : }
7614 : : }
7615 : :
7616 : : /* The following cases are about aggregates. If we are not within a full
7617 : : initializer already, and there is not a CONSTRUCTOR, it means that there
7618 : : is a missing set of braces (that is, we are processing the case for
7619 : : which reshape_init exists). */
7620 : 6032575 : bool braces_elided_p = false;
7621 : 6032575 : if (!first_initializer_p)
7622 : : {
7623 : 2075747 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7624 : : {
7625 : 2067445 : tree init_type = TREE_TYPE (init);
7626 : 2067445 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7627 : : /* There is no need to call reshape_init for pointer-to-member
7628 : : function initializers, as they are always constructed correctly
7629 : : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7630 : : which is missing outermost braces. We should warn below, and
7631 : : one of the routines below will wrap it in additional { }. */;
7632 : : /* For a nested compound literal, proceed to specialized routines,
7633 : : to handle initialization of arrays and similar. */
7634 : 2067436 : else if (COMPOUND_LITERAL_P (stripped_init))
7635 : 6 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7636 : : /* If we have an unresolved designator, we need to find the member it
7637 : : designates within TYPE, so proceed to the routines below. For
7638 : : FIELD_DECL or INTEGER_CST designators, we're already initializing
7639 : : the designated element. */
7640 : 2067430 : else if (d->cur->index
7641 : 27 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7642 : : /* Brace elision with designators is only permitted for anonymous
7643 : : aggregates. */
7644 : 15 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7645 : : /* A CONSTRUCTOR of the target's type is a previously
7646 : : digested initializer. */
7647 : 2067415 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7648 : : {
7649 : 0 : ++d->cur;
7650 : 0 : return init;
7651 : : }
7652 : : else
7653 : : {
7654 : : /* Something that hasn't been reshaped yet. */
7655 : 2067415 : ++d->cur;
7656 : 2067415 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7657 : 2067415 : return reshape_init (type, init, complain);
7658 : : }
7659 : : }
7660 : :
7661 : 8332 : if (complain & tf_warning)
7662 : 7183 : warning (OPT_Wmissing_braces,
7663 : : "missing braces around initializer for %qT",
7664 : : type);
7665 : : braces_elided_p = true;
7666 : : }
7667 : :
7668 : : /* Dispatch to specialized routines. */
7669 : 3965160 : tree new_init;
7670 : 3965160 : if (CLASS_TYPE_P (type))
7671 : 3604819 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
7672 : 360341 : else if (TREE_CODE (type) == ARRAY_TYPE)
7673 : 319149 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
7674 : 41192 : else if (VECTOR_TYPE_P (type))
7675 : 41192 : new_init = reshape_init_vector (type, d, complain);
7676 : : else
7677 : 0 : gcc_unreachable ();
7678 : :
7679 : 3965160 : if (braces_elided_p
7680 : 8332 : && TREE_CODE (new_init) == CONSTRUCTOR)
7681 : 8056 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7682 : :
7683 : : return new_init;
7684 : : }
7685 : :
7686 : : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7687 : : brace-enclosed aggregate initializer.
7688 : :
7689 : : INIT is the CONSTRUCTOR containing the list of initializers describing
7690 : : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7691 : : It may not presently match the shape of the TYPE; for example:
7692 : :
7693 : : struct S { int a; int b; };
7694 : : struct S a[] = { 1, 2, 3, 4 };
7695 : :
7696 : : Here INIT will hold a vector of four elements, rather than a
7697 : : vector of two elements, each itself a vector of two elements. This
7698 : : routine transforms INIT from the former form into the latter. The
7699 : : revised CONSTRUCTOR node is returned. */
7700 : :
7701 : : tree
7702 : 9545218 : reshape_init (tree type, tree init, tsubst_flags_t complain)
7703 : : {
7704 : 9545218 : vec<constructor_elt, va_gc> *v;
7705 : 9545218 : reshape_iter d;
7706 : 9545218 : tree new_init;
7707 : :
7708 : 9545218 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7709 : :
7710 : 9545218 : v = CONSTRUCTOR_ELTS (init);
7711 : :
7712 : : /* An empty constructor does not need reshaping, and it is always a valid
7713 : : initializer. */
7714 : 9545781 : if (vec_safe_is_empty (v))
7715 : : return init;
7716 : :
7717 : 4021680 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7718 : : /* Already reshaped. */
7719 : : return init;
7720 : :
7721 : : /* Brace elision is not performed for a CONSTRUCTOR representing
7722 : : parenthesized aggregate initialization. */
7723 : 4021543 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
7724 : : {
7725 : 455 : tree elt = (*v)[0].value;
7726 : : /* If we're initializing a char array from a string-literal that is
7727 : : enclosed in braces, unwrap it here. */
7728 : 455 : if (TREE_CODE (type) == ARRAY_TYPE
7729 : 221 : && vec_safe_length (v) == 1
7730 : 121 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7731 : 488 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7732 : : return elt;
7733 : 426 : return init;
7734 : : }
7735 : :
7736 : : /* Handle [dcl.init.list] direct-list-initialization from
7737 : : single element of enumeration with a fixed underlying type. */
7738 : 4021088 : if (is_direct_enum_init (type, init))
7739 : : {
7740 : 176 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7741 : 176 : type = cv_unqualified (type);
7742 : 176 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7743 : : {
7744 : 161 : warning_sentinel w (warn_useless_cast);
7745 : 161 : warning_sentinel w2 (warn_ignored_qualifiers);
7746 : 161 : return cp_build_c_cast (input_location, type, elt,
7747 : : tf_warning_or_error);
7748 : 161 : }
7749 : : else
7750 : 15 : return error_mark_node;
7751 : : }
7752 : :
7753 : : /* Recurse on this CONSTRUCTOR. */
7754 : 4020912 : d.cur = &(*v)[0];
7755 : 4020912 : d.end = d.cur + v->length ();
7756 : 4020912 : d.raw_idx = 0;
7757 : :
7758 : 4020912 : new_init = reshape_init_r (type, &d, init, complain);
7759 : 4020912 : if (new_init == error_mark_node)
7760 : : return error_mark_node;
7761 : :
7762 : : /* Make sure all the element of the constructor were used. Otherwise,
7763 : : issue an error about exceeding initializers. */
7764 : 4020294 : if (d.cur != d.end)
7765 : : {
7766 : 159 : if (complain & tf_error)
7767 : 63 : error ("too many initializers for %qT", type);
7768 : 159 : return error_mark_node;
7769 : : }
7770 : :
7771 : 4020135 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7772 : 4020135 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7773 : 460341 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7774 : 4020135 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7775 : 4020135 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7776 : 30567 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7777 : : || seen_error ());
7778 : :
7779 : : return new_init;
7780 : : }
7781 : :
7782 : : /* Verify array initializer. Returns true if errors have been reported. */
7783 : :
7784 : : bool
7785 : 871858 : check_array_initializer (tree decl, tree type, tree init)
7786 : : {
7787 : 871858 : tree element_type = TREE_TYPE (type);
7788 : :
7789 : : /* Structured binding when initialized with an array type needs
7790 : : to have complete type. */
7791 : 871858 : if (decl
7792 : 866562 : && DECL_DECOMPOSITION_P (decl)
7793 : 360 : && DECL_DECOMP_IS_BASE (decl)
7794 : 872218 : && !COMPLETE_TYPE_P (type))
7795 : : {
7796 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
7797 : : "structured binding has incomplete type %qT", type);
7798 : 0 : TREE_TYPE (decl) = error_mark_node;
7799 : 0 : return true;
7800 : : }
7801 : :
7802 : : /* The array type itself need not be complete, because the
7803 : : initializer may tell us how many elements are in the array.
7804 : : But, the elements of the array must be complete. */
7805 : 871858 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
7806 : : {
7807 : 15 : if (decl)
7808 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
7809 : : "elements of array %q#D have incomplete type", decl);
7810 : : else
7811 : 6 : error ("elements of array %q#T have incomplete type", type);
7812 : 15 : return true;
7813 : : }
7814 : :
7815 : 871843 : location_t loc = (decl ? location_of (decl) : input_location);
7816 : 871843 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7817 : : return true;
7818 : :
7819 : : /* A compound literal can't have variable size. */
7820 : 871843 : if (init && !decl
7821 : 871843 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7822 : 5287 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7823 : : {
7824 : 3 : error ("variable-sized compound literal");
7825 : 3 : return true;
7826 : : }
7827 : : return false;
7828 : : }
7829 : :
7830 : : /* Subroutine of check_initializer; args are passed down from that function.
7831 : : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7832 : :
7833 : : static tree
7834 : 2350991 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
7835 : : {
7836 : 2350991 : gcc_assert (stmts_are_full_exprs_p ());
7837 : 2350991 : if (init)
7838 : 1721878 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7839 : 2350991 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
7840 : : }
7841 : :
7842 : : /* Verify INIT (the initializer for DECL), and record the
7843 : : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7844 : : grok_reference_init.
7845 : :
7846 : : If the return value is non-NULL, it is an expression that must be
7847 : : evaluated dynamically to initialize DECL. */
7848 : :
7849 : : static tree
7850 : 42064033 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7851 : : {
7852 : 42064033 : tree type;
7853 : 42064033 : tree init_code = NULL;
7854 : 42064033 : tree core_type;
7855 : :
7856 : : /* Things that are going to be initialized need to have complete
7857 : : type. */
7858 : 42064033 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7859 : :
7860 : 42064033 : if (DECL_HAS_VALUE_EXPR_P (decl))
7861 : : {
7862 : : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7863 : : it doesn't have storage to be initialized. */
7864 : 138 : gcc_assert (init == NULL_TREE);
7865 : : return NULL_TREE;
7866 : : }
7867 : :
7868 : 42063895 : if (type == error_mark_node)
7869 : : /* We will have already complained. */
7870 : : return NULL_TREE;
7871 : :
7872 : 42063895 : if (TREE_CODE (type) == ARRAY_TYPE)
7873 : : {
7874 : 866562 : if (check_array_initializer (decl, type, init))
7875 : : return NULL_TREE;
7876 : : }
7877 : 41197333 : else if (!COMPLETE_TYPE_P (type))
7878 : : {
7879 : 37 : error_at (DECL_SOURCE_LOCATION (decl),
7880 : : "%q#D has incomplete type", decl);
7881 : 37 : TREE_TYPE (decl) = error_mark_node;
7882 : 37 : return NULL_TREE;
7883 : : }
7884 : : else
7885 : : /* There is no way to make a variable-sized class type in GNU C++. */
7886 : 41197296 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7887 : :
7888 : 42063849 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7889 : : {
7890 : 1488446 : int init_len = CONSTRUCTOR_NELTS (init);
7891 : 1488446 : if (SCALAR_TYPE_P (type))
7892 : : {
7893 : 52737 : if (init_len == 0)
7894 : : {
7895 : 43669 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7896 : 43669 : init = build_zero_init (type, NULL_TREE, false);
7897 : : }
7898 : 9068 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7899 : : {
7900 : 12 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7901 : : "scalar object %qD requires one element in "
7902 : : "initializer", decl);
7903 : 6 : TREE_TYPE (decl) = error_mark_node;
7904 : 6 : return NULL_TREE;
7905 : : }
7906 : : }
7907 : : }
7908 : :
7909 : 42063843 : if (TREE_CODE (decl) == CONST_DECL)
7910 : : {
7911 : 0 : gcc_assert (!TYPE_REF_P (type));
7912 : :
7913 : 0 : DECL_INITIAL (decl) = init;
7914 : :
7915 : 0 : gcc_assert (init != NULL_TREE);
7916 : : init = NULL_TREE;
7917 : : }
7918 : 42063843 : else if (!init && DECL_REALLY_EXTERN (decl))
7919 : : ;
7920 : 42063843 : else if (flag_openmp
7921 : 131397 : && VAR_P (decl)
7922 : 131397 : && DECL_LANG_SPECIFIC (decl)
7923 : 56640 : && DECL_OMP_DECLARE_MAPPER_P (decl)
7924 : 42063843 : && TREE_CODE (init) == OMP_DECLARE_MAPPER)
7925 : : return NULL_TREE;
7926 : 4248406 : else if (init || type_build_ctor_call (type)
7927 : 45683133 : || TYPE_REF_P (type))
7928 : : {
7929 : 38444568 : if (TYPE_REF_P (type))
7930 : : {
7931 : 317912 : init = grok_reference_init (decl, type, init, flags);
7932 : 317912 : flags |= LOOKUP_ALREADY_DIGESTED;
7933 : : }
7934 : 38126656 : else if (!init)
7935 : 629116 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7936 : : tf_warning_or_error);
7937 : : /* Do not reshape constructors of vectors (they don't need to be
7938 : : reshaped. */
7939 : 37497540 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7940 : : {
7941 : 1444459 : if (is_std_init_list (type))
7942 : : {
7943 : 331 : init = perform_implicit_conversion (type, init,
7944 : : tf_warning_or_error);
7945 : 331 : flags |= LOOKUP_ALREADY_DIGESTED;
7946 : : }
7947 : 1444128 : else if (TYPE_NON_AGGREGATE_CLASS (type))
7948 : : {
7949 : : /* Don't reshape if the class has constructors. */
7950 : 212824 : if (cxx_dialect == cxx98)
7951 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7952 : : "in C++98 %qD must be initialized by "
7953 : : "constructor, not by %<{...}%>",
7954 : : decl);
7955 : : }
7956 : 1231304 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7957 : : {
7958 : 0 : error ("opaque vector types cannot be initialized");
7959 : 0 : init = error_mark_node;
7960 : : }
7961 : : else
7962 : : {
7963 : 1231304 : init = reshape_init (type, init, tf_warning_or_error);
7964 : 1231304 : flags |= LOOKUP_NO_NARROWING;
7965 : : }
7966 : : }
7967 : : /* [dcl.init] "Otherwise, if the destination type is an array, the object
7968 : : is initialized as follows..." So handle things like
7969 : :
7970 : : int a[](1, 2, 3);
7971 : :
7972 : : which is permitted in C++20 by P0960. */
7973 : 36053081 : else if (TREE_CODE (init) == TREE_LIST
7974 : 615889 : && TREE_TYPE (init) == NULL_TREE
7975 : 615889 : && TREE_CODE (type) == ARRAY_TYPE
7976 : 165 : && !DECL_DECOMPOSITION_P (decl)
7977 : 36053225 : && (cxx_dialect >= cxx20))
7978 : 124 : init = do_aggregate_paren_init (init, type);
7979 : 36052957 : else if (TREE_CODE (init) == TREE_LIST
7980 : 615765 : && TREE_TYPE (init) != unknown_type_node
7981 : 36668722 : && !MAYBE_CLASS_TYPE_P (type))
7982 : : {
7983 : 134116 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7984 : :
7985 : : /* We get here with code like `int a (2);' */
7986 : 134116 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7987 : : tf_warning_or_error);
7988 : : }
7989 : :
7990 : : /* If DECL has an array type without a specific bound, deduce the
7991 : : array size from the initializer. */
7992 : 38444568 : maybe_deduce_size_from_array_init (decl, init);
7993 : 38444568 : type = TREE_TYPE (decl);
7994 : 38444568 : if (type == error_mark_node)
7995 : : return NULL_TREE;
7996 : :
7997 : 74115119 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7998 : 3422358 : && !(flags & LOOKUP_ALREADY_DIGESTED)
7999 : 3422027 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
8000 : 1285609 : && CP_AGGREGATE_TYPE_P (type)
8001 : 1072785 : && (CLASS_TYPE_P (type)
8002 : : /* The call to build_aggr_init below could end up
8003 : : calling build_vec_init, which may break when we
8004 : : are processing a template. */
8005 : 1586 : || processing_template_decl
8006 : 1520 : || !TYPE_NEEDS_CONSTRUCTING (type)
8007 : 1488 : || type_has_extended_temps (type))))
8008 : 74538456 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
8009 : : {
8010 : 2350991 : init_code = build_aggr_init_full_exprs (decl, init, flags);
8011 : :
8012 : : /* A constructor call is a non-trivial initializer even if
8013 : : it isn't explicitly written. */
8014 : 2350991 : if (TREE_SIDE_EFFECTS (init_code))
8015 : 2342613 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
8016 : :
8017 : : /* If this is a constexpr initializer, expand_default_init will
8018 : : have returned an INIT_EXPR rather than a CALL_EXPR. In that
8019 : : case, pull the initializer back out and pass it down into
8020 : : store_init_value. */
8021 : 6101118 : while (true)
8022 : : {
8023 : 6101118 : if (TREE_CODE (init_code) == EXPR_STMT
8024 : 3761125 : || TREE_CODE (init_code) == STMT_EXPR
8025 : 3751693 : || TREE_CODE (init_code) == CONVERT_EXPR)
8026 : 3740741 : init_code = TREE_OPERAND (init_code, 0);
8027 : 2360377 : else if (TREE_CODE (init_code) == BIND_EXPR)
8028 : 9386 : init_code = BIND_EXPR_BODY (init_code);
8029 : : else
8030 : : break;
8031 : : }
8032 : 2350991 : if (TREE_CODE (init_code) == INIT_EXPR)
8033 : : {
8034 : : /* In C++20, the call to build_aggr_init could have created
8035 : : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
8036 : : A(1, 2). */
8037 : 1397200 : tree rhs = TREE_OPERAND (init_code, 1);
8038 : 1397200 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
8039 : : /* Avoid leaking TARGET_EXPR into template trees. */
8040 : 4712 : rhs = build_implicit_conv_flags (type, init, flags);
8041 : 1397200 : init = rhs;
8042 : :
8043 : 1397200 : init_code = NULL_TREE;
8044 : : /* Don't call digest_init; it's unnecessary and will complain
8045 : : about aggregate initialization of non-aggregate classes. */
8046 : 1397197 : flags |= LOOKUP_ALREADY_DIGESTED;
8047 : : }
8048 : 953791 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
8049 : 1902499 : || DECL_DECLARED_CONSTINIT_P (decl))
8050 : : {
8051 : : /* Declared constexpr or constinit, but no suitable initializer;
8052 : : massage init appropriately so we can pass it into
8053 : : store_init_value for the error. */
8054 : 5101 : tree new_init = NULL_TREE;
8055 : 5101 : if (!processing_template_decl
8056 : 384 : && TREE_CODE (init_code) == CALL_EXPR)
8057 : 328 : new_init = build_cplus_new (type, init_code, tf_none);
8058 : 4749 : else if (CLASS_TYPE_P (type)
8059 : 9522 : && (!init || TREE_CODE (init) == TREE_LIST))
8060 : 4689 : new_init = build_functional_cast (input_location, type,
8061 : : init, tf_none);
8062 : 5017 : if (new_init)
8063 : : {
8064 : 5017 : init = new_init;
8065 : 5017 : if (TREE_CODE (init) == TARGET_EXPR
8066 : 349 : && !(flags & LOOKUP_ONLYCONVERTING))
8067 : 349 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
8068 : : }
8069 : : init_code = NULL_TREE;
8070 : : }
8071 : : else
8072 : : init = NULL_TREE;
8073 : : }
8074 : :
8075 : 37495873 : if (init && TREE_CODE (init) != TREE_VEC)
8076 : : {
8077 : 37495858 : init_code = store_init_value (decl, init, cleanups, flags);
8078 : :
8079 : 37493161 : if (DECL_INITIAL (decl)
8080 : 24325250 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
8081 : 39134540 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
8082 : : {
8083 : 1001205 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
8084 : 1001205 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
8085 : 1001205 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
8086 : 36 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
8087 : : }
8088 : :
8089 : 909415 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
8090 : 49203 : && DECL_INITIAL (decl)
8091 : 48779 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8092 : 37539098 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
8093 : 6 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
8094 : 3 : DECL_SOURCE_LOCATION (decl)),
8095 : 3 : 0, "array %qD initialized by parenthesized "
8096 : : "string literal %qE",
8097 : 3 : decl, DECL_INITIAL (decl));
8098 : : init = NULL_TREE;
8099 : : }
8100 : : }
8101 : : else
8102 : : {
8103 : 7238550 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
8104 : 4270533 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
8105 : 651245 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
8106 : 19 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
8107 : : /*complain=*/true);
8108 : :
8109 : 3619275 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
8110 : : tf_warning_or_error);
8111 : : }
8112 : :
8113 : 0 : if (init && init != error_mark_node)
8114 : 0 : init_code = cp_build_init_expr (decl, init);
8115 : :
8116 : 14233354 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
8117 : 42079698 : && init_code != error_mark_node)
8118 : : init_code = NULL_TREE;
8119 : :
8120 : 42044807 : if (init_code)
8121 : : {
8122 : : /* We might have set these in cp_finish_decl. */
8123 : 14217020 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
8124 : 14217020 : TREE_CONSTANT (decl) = false;
8125 : : }
8126 : :
8127 : 14217020 : if (init_code
8128 : 14217020 : && DECL_IN_AGGR_P (decl)
8129 : 6 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8130 : : {
8131 : 0 : static int explained = 0;
8132 : :
8133 : 0 : auto_diagnostic_group d;
8134 : 0 : if (cxx_dialect < cxx11)
8135 : 0 : error ("initializer invalid for static member with constructor");
8136 : 0 : else if (cxx_dialect < cxx17)
8137 : 0 : error ("non-constant in-class initialization invalid for static "
8138 : : "member %qD", decl);
8139 : : else
8140 : 0 : error ("non-constant in-class initialization invalid for non-inline "
8141 : : "static member %qD", decl);
8142 : 0 : if (!explained)
8143 : : {
8144 : 0 : inform (input_location,
8145 : : "(an out of class initialization is required)");
8146 : 0 : explained = 1;
8147 : : }
8148 : 0 : return NULL_TREE;
8149 : 0 : }
8150 : :
8151 : : return init_code;
8152 : : }
8153 : :
8154 : : /* If DECL is not a local variable, give it RTL. */
8155 : :
8156 : : static void
8157 : 101429108 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
8158 : : {
8159 : 101429108 : int toplev = toplevel_bindings_p ();
8160 : 101429108 : int defer_p;
8161 : :
8162 : : /* Set the DECL_ASSEMBLER_NAME for the object. */
8163 : 101429108 : if (asmspec)
8164 : : {
8165 : : /* The `register' keyword, when used together with an
8166 : : asm-specification, indicates that the variable should be
8167 : : placed in a particular register. */
8168 : 975503 : if (VAR_P (decl) && DECL_REGISTER (decl))
8169 : : {
8170 : 81 : set_user_assembler_name (decl, asmspec);
8171 : 81 : DECL_HARD_REGISTER (decl) = 1;
8172 : : }
8173 : : else
8174 : : {
8175 : 975422 : if (TREE_CODE (decl) == FUNCTION_DECL
8176 : 975422 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
8177 : 77352 : set_builtin_user_assembler_name (decl, asmspec);
8178 : 975422 : set_user_assembler_name (decl, asmspec);
8179 : 975422 : if (DECL_LOCAL_DECL_P (decl))
8180 : 6 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
8181 : : /* We have to propagate the name to the ns-alias.
8182 : : This is horrible, as we're affecting a
8183 : : possibly-shared decl. Again, a one-true-decl
8184 : : model breaks down. */
8185 : 6 : if (ns_decl != error_mark_node)
8186 : 6 : set_user_assembler_name (ns_decl, asmspec);
8187 : : }
8188 : : }
8189 : :
8190 : : /* Handle non-variables up front. */
8191 : 101429108 : if (!VAR_P (decl))
8192 : : {
8193 : 53630316 : rest_of_decl_compilation (decl, toplev, at_eof);
8194 : 53630316 : return;
8195 : : }
8196 : :
8197 : : /* If we see a class member here, it should be a static data
8198 : : member. */
8199 : 47798792 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8200 : : {
8201 : 3901377 : gcc_assert (TREE_STATIC (decl));
8202 : : /* An in-class declaration of a static data member should be
8203 : : external; it is only a declaration, and not a definition. */
8204 : 3901377 : if (init == NULL_TREE)
8205 : 3901367 : gcc_assert (DECL_EXTERNAL (decl)
8206 : : || !TREE_PUBLIC (decl));
8207 : : }
8208 : :
8209 : : /* We don't create any RTL for local variables. */
8210 : 47798792 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8211 : : return;
8212 : :
8213 : : /* We defer emission of local statics until the corresponding
8214 : : DECL_EXPR is expanded. But with constexpr its function might never
8215 : : be expanded, so go ahead and tell cgraph about the variable now. */
8216 : 49604764 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
8217 : 141422 : && !var_in_maybe_constexpr_fn (decl))
8218 : 49463472 : || DECL_VIRTUAL_P (decl));
8219 : :
8220 : : /* Defer template instantiations. */
8221 : 24802382 : if (DECL_LANG_SPECIFIC (decl)
8222 : 24802382 : && DECL_IMPLICIT_INSTANTIATION (decl))
8223 : : defer_p = 1;
8224 : :
8225 : : /* If we're not deferring, go ahead and assemble the variable. */
8226 : 15471486 : if (!defer_p)
8227 : 13153270 : rest_of_decl_compilation (decl, toplev, at_eof);
8228 : : }
8229 : :
8230 : : /* walk_tree helper for wrap_temporary_cleanups, below. */
8231 : :
8232 : : static tree
8233 : 10343141 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
8234 : : {
8235 : : /* Stop at types or full-expression boundaries. */
8236 : 10343141 : if (TYPE_P (*stmt_p)
8237 : 10342930 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
8238 : : {
8239 : 439 : *walk_subtrees = 0;
8240 : 439 : return NULL_TREE;
8241 : : }
8242 : :
8243 : 10342702 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
8244 : : {
8245 : 177854 : tree guard = (tree)data;
8246 : 177854 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
8247 : :
8248 : 98485 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
8249 : 269724 : && !expr_noexcept_p (tcleanup, tf_none))
8250 : : {
8251 : 145 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
8252 : : /* Tell honor_protect_cleanup_actions to handle this as a separate
8253 : : cleanup. */
8254 : 145 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
8255 : 145 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
8256 : : }
8257 : : }
8258 : :
8259 : : return NULL_TREE;
8260 : : }
8261 : :
8262 : : /* We're initializing a local variable which has a cleanup GUARD. If there
8263 : : are any temporaries used in the initializer INIT of this variable, we
8264 : : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
8265 : : variable will be cleaned up properly if one of them throws.
8266 : :
8267 : : Unfortunately, there's no way to express this properly in terms of
8268 : : nesting, as the regions for the temporaries overlap the region for the
8269 : : variable itself; if there are two temporaries, the variable needs to be
8270 : : the first thing destroyed if either of the temporary destructors throws.
8271 : : However, we only want to run the variable's cleanup if it actually got
8272 : : constructed. So we need to guard the temporary cleanups with the
8273 : : variable's cleanup if they are run on the normal path, but not if they
8274 : : are run on the exceptional path. We implement this by telling
8275 : : honor_protect_cleanup_actions to strip the variable cleanup from the
8276 : : exceptional path.
8277 : :
8278 : : Another approach could be to make the variable cleanup region enclose
8279 : : initialization, but depend on a flag to indicate that the variable is
8280 : : initialized; that's effectively what we do for arrays. But the current
8281 : : approach works fine for non-arrays, and has no code overhead in the usual
8282 : : case where the temporary destructors are noexcept. */
8283 : :
8284 : : static void
8285 : 959442 : wrap_temporary_cleanups (tree init, tree guard)
8286 : : {
8287 : 959442 : if (TREE_CODE (guard) == BIND_EXPR)
8288 : : {
8289 : : /* An array cleanup region already encloses any temporary cleanups,
8290 : : don't wrap it around them again. */
8291 : 422 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8292 : : return;
8293 : : }
8294 : 959020 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8295 : : }
8296 : :
8297 : : /* Generate code to initialize DECL (a local variable). */
8298 : :
8299 : : static void
8300 : 22996392 : initialize_local_var (tree decl, tree init, bool decomp)
8301 : : {
8302 : 22996392 : tree type = TREE_TYPE (decl);
8303 : 22996392 : tree cleanup;
8304 : 22996392 : int already_used;
8305 : :
8306 : 22996392 : gcc_assert (VAR_P (decl) || TREE_CODE (decl) == RESULT_DECL);
8307 : 22996392 : gcc_assert (!TREE_STATIC (decl));
8308 : :
8309 : 22996392 : if (DECL_SIZE (decl) == NULL_TREE)
8310 : : {
8311 : : /* If we used it already as memory, it must stay in memory. */
8312 : 0 : DECL_INITIAL (decl) = NULL_TREE;
8313 : 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8314 : 0 : return;
8315 : : }
8316 : :
8317 : 22996392 : if (type == error_mark_node)
8318 : : return;
8319 : :
8320 : : /* Compute and store the initial value. */
8321 : 22996392 : already_used = TREE_USED (decl) || TREE_USED (type);
8322 : 22996392 : if (TREE_USED (type))
8323 : 32 : DECL_READ_P (decl) = 1;
8324 : :
8325 : : /* Generate a cleanup, if necessary. */
8326 : 22996392 : cleanup = (decomp ? NULL_TREE
8327 : 22977820 : : cxx_maybe_build_cleanup (decl, tf_warning_or_error));
8328 : :
8329 : : /* Perform the initialization. */
8330 : 22996392 : if (init)
8331 : : {
8332 : 14202775 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8333 : 14202775 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8334 : 13001231 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8335 : 20253936 : && TREE_OPERAND (init, 0) == decl)
8336 : : {
8337 : : /* Stick simple initializers in DECL_INITIAL so that
8338 : : -Wno-init-self works (c++/34772). */
8339 : 7252705 : DECL_INITIAL (decl) = rinit;
8340 : :
8341 : 7252705 : if (warn_init_self && TYPE_REF_P (type))
8342 : : {
8343 : 2514 : STRIP_NOPS (rinit);
8344 : 2514 : if (rinit == decl)
8345 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
8346 : 3 : OPT_Winit_self,
8347 : : "reference %qD is initialized with itself", decl);
8348 : : }
8349 : : }
8350 : : else
8351 : : {
8352 : 6950070 : int saved_stmts_are_full_exprs_p;
8353 : :
8354 : : /* If we're only initializing a single object, guard the
8355 : : destructors of any temporaries used in its initializer with
8356 : : its destructor. */
8357 : 6950070 : if (cleanup)
8358 : 953415 : wrap_temporary_cleanups (init, cleanup);
8359 : :
8360 : 6950070 : gcc_assert (building_stmt_list_p ());
8361 : 6950070 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8362 : : /* Avoid CLEANUP_POINT_EXPR for the structured binding
8363 : : bases, those will have CLEANUP_POINT_EXPR at the end of
8364 : : code emitted by cp_finish_decomp. */
8365 : 6950070 : if (decomp)
8366 : 18148 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8367 : : else
8368 : 6931922 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8369 : 6950070 : finish_expr_stmt (init);
8370 : 6950070 : current_stmt_tree ()->stmts_are_full_exprs_p
8371 : 6950070 : = saved_stmts_are_full_exprs_p;
8372 : : }
8373 : : }
8374 : :
8375 : : /* Set this to 0 so we can tell whether an aggregate which was
8376 : : initialized was ever used. Don't do this if it has a
8377 : : destructor, so we don't complain about the 'resource
8378 : : allocation is initialization' idiom. Now set
8379 : : attribute((unused)) on types so decls of that type will be
8380 : : marked used. (see TREE_USED, above.) */
8381 : 22996392 : if (TYPE_NEEDS_CONSTRUCTING (type)
8382 : 1896540 : && ! already_used
8383 : 4567 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8384 : 23000533 : && DECL_NAME (decl))
8385 : 4113 : TREE_USED (decl) = 0;
8386 : 22992279 : else if (already_used)
8387 : 20807949 : TREE_USED (decl) = 1;
8388 : :
8389 : 22996392 : if (cleanup)
8390 : 965876 : finish_decl_cleanup (decl, cleanup);
8391 : : }
8392 : :
8393 : : /* DECL is a VAR_DECL for a compiler-generated variable with static
8394 : : storage duration (like a virtual table) whose initializer is a
8395 : : compile-time constant. Initialize the variable and provide it to the
8396 : : back end. */
8397 : :
8398 : : void
8399 : 2135473 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8400 : : {
8401 : 2135473 : tree init;
8402 : 2135473 : gcc_assert (DECL_ARTIFICIAL (decl));
8403 : 2135473 : init = build_constructor (TREE_TYPE (decl), v);
8404 : 2135473 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8405 : 2135473 : DECL_INITIAL (decl) = init;
8406 : 2135473 : DECL_INITIALIZED_P (decl) = 1;
8407 : : /* Mark the decl as constexpr so that we can access its content
8408 : : at compile time. */
8409 : 2135473 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8410 : 2135473 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
8411 : 2135473 : determine_visibility (decl);
8412 : 2135473 : layout_var_decl (decl);
8413 : 2135473 : maybe_commonize_var (decl);
8414 : 2135473 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8415 : 2135473 : }
8416 : :
8417 : : /* INIT is the initializer for a variable, as represented by the
8418 : : parser. Returns true iff INIT is value-dependent. */
8419 : :
8420 : : static bool
8421 : 7974185 : value_dependent_init_p (tree init)
8422 : : {
8423 : 7974185 : if (TREE_CODE (init) == TREE_LIST)
8424 : : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8425 : 10563 : return any_value_dependent_elements_p (init);
8426 : 7963622 : else if (TREE_CODE (init) == CONSTRUCTOR)
8427 : : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8428 : : {
8429 : 40428 : if (dependent_type_p (TREE_TYPE (init)))
8430 : : return true;
8431 : :
8432 : 40422 : vec<constructor_elt, va_gc> *elts;
8433 : 40422 : size_t nelts;
8434 : 40422 : size_t i;
8435 : :
8436 : 40422 : elts = CONSTRUCTOR_ELTS (init);
8437 : 40422 : nelts = vec_safe_length (elts);
8438 : 564654 : for (i = 0; i < nelts; ++i)
8439 : 538942 : if (value_dependent_init_p ((*elts)[i].value))
8440 : : return true;
8441 : : }
8442 : : else
8443 : : /* It must be a simple expression, e.g., int i = 3; */
8444 : 7923194 : return value_dependent_expression_p (init);
8445 : :
8446 : : return false;
8447 : : }
8448 : :
8449 : : /* A helper function to be called via walk_tree. If any label exists
8450 : : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8451 : :
8452 : : static tree
8453 : 337 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8454 : : {
8455 : 337 : if (TYPE_P (*tp))
8456 : 0 : *walk_subtrees = 0;
8457 : 337 : if (TREE_CODE (*tp) == LABEL_DECL)
8458 : 6 : cfun->has_forced_label_in_static = 1;
8459 : 337 : return NULL_TREE;
8460 : : }
8461 : :
8462 : : /* Return true if DECL has either a trivial destructor, or for C++20
8463 : : is constexpr and has a constexpr destructor. */
8464 : :
8465 : : static bool
8466 : 36600217 : decl_maybe_constant_destruction (tree decl, tree type)
8467 : : {
8468 : 36600217 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8469 : 36600217 : || (cxx_dialect >= cxx20
8470 : 1304047 : && VAR_P (decl)
8471 : 1303792 : && DECL_DECLARED_CONSTEXPR_P (decl)
8472 : 339 : && type_has_constexpr_destructor (strip_array_types (type))));
8473 : : }
8474 : :
8475 : : static tree declare_simd_adjust_this (tree *, int *, void *);
8476 : :
8477 : : /* Helper function of omp_declare_variant_finalize. Finalize one
8478 : : "omp declare variant base" attribute. Return true if it should be
8479 : : removed. */
8480 : :
8481 : : static bool
8482 : 1487 : omp_declare_variant_finalize_one (tree decl, tree attr)
8483 : : {
8484 : 1487 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8485 : : {
8486 : 116 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8487 : : DECL_ARGUMENTS (decl), NULL);
8488 : 116 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8489 : : DECL_ARGUMENTS (decl), NULL);
8490 : : }
8491 : :
8492 : 1487 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
8493 : 1487 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8494 : : OMP_TRAIT_CONSTRUCT_SIMD);
8495 : 1487 : if (simd)
8496 : : {
8497 : 51 : TREE_VALUE (simd)
8498 : 51 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8499 : 51 : OMP_TS_PROPERTIES (simd));
8500 : : /* FIXME, adjusting simd args unimplemented. */
8501 : 51 : return true;
8502 : : }
8503 : :
8504 : 1436 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
8505 : 1436 : location_t varid_loc
8506 : 1436 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8507 : 1436 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8508 : 1436 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8509 : 1436 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8510 : :
8511 : 1436 : location_t save_loc = input_location;
8512 : 1436 : input_location = varid_loc;
8513 : :
8514 : 1436 : releasing_vec args;
8515 : 1436 : tree parm = DECL_ARGUMENTS (decl);
8516 : 1436 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8517 : 116 : parm = DECL_CHAIN (parm);
8518 : 2644 : for (; parm; parm = DECL_CHAIN (parm))
8519 : 1208 : vec_safe_push (args, forward_parm (parm));
8520 : :
8521 : 1436 : unsigned nappend_args = 0;
8522 : 1436 : tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8523 : 1436 : if (append_args_list)
8524 : : {
8525 : 354 : append_args_list = TREE_VALUE (append_args_list);
8526 : 354 : append_args_list = (append_args_list && TREE_CHAIN (append_args_list)
8527 : 591 : ? TREE_VALUE (TREE_CHAIN (append_args_list))
8528 : : : NULL_TREE);
8529 : 741 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8530 : 387 : nappend_args++;
8531 : 354 : if (nappend_args)
8532 : : {
8533 : 237 : tree type;
8534 : 237 : if ((type = lookup_qualified_name (global_namespace,
8535 : : "omp_interop_t",
8536 : : LOOK_want::NORMAL,
8537 : : /*complain*/false)) == NULL_TREE
8538 : 237 : || !c_omp_interop_t_p (TREE_TYPE (type)))
8539 : : {
8540 : 24 : location_t loc = input_location;
8541 : 24 : variant = tree_strip_any_location_wrapper (variant);
8542 : 24 : if (!identifier_p (variant))
8543 : : {
8544 : 21 : if (TREE_CODE (variant) == OVERLOAD && OVL_SINGLE_P (variant))
8545 : 21 : variant = OVL_FIRST (variant);
8546 : 21 : loc = EXPR_LOC_OR_LOC (variant,
8547 : : DECL_SOURCE_LOCATION (variant));
8548 : : }
8549 : 24 : error_at (loc, "argument %d of %qE must be of %<omp_interop_t%>",
8550 : 24 : args->length () + 1, variant);
8551 : 24 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8552 : : "%<append_args%> specified here");
8553 : 24 : return true;
8554 : : }
8555 : 564 : for (unsigned i = 0; i < nappend_args; i++)
8556 : 351 : vec_safe_push (args, build_stub_object (TREE_TYPE (type)));
8557 : : }
8558 : : }
8559 : :
8560 : 1412 : bool koenig_p = false;
8561 : 1412 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
8562 : : {
8563 : 1406 : if (identifier_p (variant)
8564 : : /* In C++20, we may need to perform ADL for a template
8565 : : name. */
8566 : 1382 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
8567 : 18 : && identifier_p (TREE_OPERAND (variant, 0))))
8568 : : {
8569 : 24 : if (!args->is_empty ())
8570 : : {
8571 : 24 : koenig_p = true;
8572 : 24 : if (!any_type_dependent_arguments_p (args))
8573 : 21 : variant = perform_koenig_lookup (variant, args,
8574 : : tf_warning_or_error);
8575 : : }
8576 : : else
8577 : 0 : variant = unqualified_fn_lookup_error (variant);
8578 : : }
8579 : 1382 : else if (!args->is_empty () && is_overloaded_fn (variant))
8580 : : {
8581 : 716 : tree fn = get_first_fn (variant);
8582 : 716 : fn = STRIP_TEMPLATE (fn);
8583 : 716 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8584 : 716 : || DECL_FUNCTION_MEMBER_P (fn)
8585 : 603 : || DECL_LOCAL_DECL_P (fn)))
8586 : : {
8587 : 603 : koenig_p = true;
8588 : 603 : if (!any_type_dependent_arguments_p (args))
8589 : 552 : variant = perform_koenig_lookup (variant, args,
8590 : : tf_warning_or_error);
8591 : : }
8592 : : }
8593 : : }
8594 : :
8595 : 1412 : if (idk == CP_ID_KIND_QUALIFIED)
8596 : 6 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8597 : : koenig_p, tf_warning_or_error);
8598 : : else
8599 : 1406 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8600 : : koenig_p, tf_warning_or_error);
8601 : 1412 : if (variant == error_mark_node && !processing_template_decl)
8602 : : return true;
8603 : :
8604 : 1367 : if (TREE_CODE (variant) == TARGET_EXPR)
8605 : 9 : variant = TARGET_EXPR_INITIAL (variant);
8606 : :
8607 : 2725 : variant = cp_get_callee_fndecl_nofold (STRIP_REFERENCE_REF (variant));
8608 : 1367 : input_location = save_loc;
8609 : :
8610 : 1367 : if (variant == decl)
8611 : : {
8612 : 0 : error_at (varid_loc, "variant %qD is the same as base function",
8613 : : variant);
8614 : 0 : return true;
8615 : : }
8616 : :
8617 : 1367 : if (variant)
8618 : : {
8619 : 1283 : bool fail;
8620 : 1283 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8621 : 1283 : if (!nappend_args)
8622 : 1124 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8623 : : COMPARE_STRICT);
8624 : : else
8625 : : {
8626 : 159 : unsigned nbase_args = 0;
8627 : 159 : for (tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
8628 : 756 : t && TREE_VALUE (t) != void_type_node; t = TREE_CHAIN (t))
8629 : 240 : nbase_args++;
8630 : 159 : tree vargs, varg;
8631 : 159 : vargs = varg = TYPE_ARG_TYPES (TREE_TYPE (variant));
8632 : 399 : for (unsigned i = 0; i < nbase_args && varg;
8633 : 240 : i++, varg = TREE_CHAIN (varg))
8634 : 240 : vargs = varg;
8635 : 417 : for (unsigned i = 0; i < nappend_args && varg; i++)
8636 : 258 : varg = TREE_CHAIN (varg);
8637 : 159 : tree saved_vargs;
8638 : 159 : if (nbase_args)
8639 : : {
8640 : 111 : saved_vargs = TREE_CHAIN (vargs);
8641 : 111 : TREE_CHAIN (vargs) = varg;
8642 : : }
8643 : : else
8644 : : {
8645 : 48 : saved_vargs = vargs;
8646 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = varg;
8647 : : }
8648 : : /* Skip assert check that TYPE_CANONICAL is the same. */
8649 : 159 : fail = !comptypes (TREE_TYPE (decl), TREE_TYPE (variant),
8650 : : COMPARE_STRUCTURAL);
8651 : 159 : if (nbase_args)
8652 : 111 : TREE_CHAIN (vargs) = saved_vargs;
8653 : : else
8654 : 48 : TYPE_ARG_TYPES (TREE_TYPE (variant)) = saved_vargs;
8655 : 159 : varg = saved_vargs;
8656 : 159 : if (!fail && !processing_template_decl)
8657 : 402 : for (unsigned i = 0; i < nappend_args;
8658 : 246 : i++, varg = TREE_CHAIN (varg))
8659 : 507 : if (!varg || !c_omp_interop_t_p (TREE_VALUE (varg)))
8660 : : {
8661 : 9 : error_at (DECL_SOURCE_LOCATION (variant),
8662 : : "argument %d of %qD must be of %<omp_interop_t%>",
8663 : 9 : nbase_args + i + 1, variant);
8664 : 9 : inform (EXPR_LOCATION (TREE_PURPOSE (append_args_list)),
8665 : : "%<append_args%> specified here");
8666 : 9 : break;
8667 : : }
8668 : : }
8669 : 1283 : if (fail)
8670 : : {
8671 : 69 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
8672 : : "types", variant, decl);
8673 : 69 : return true;
8674 : : }
8675 : 1214 : if (fndecl_built_in_p (variant)
8676 : 1214 : && (startswith (varname, "__builtin_")
8677 : 0 : || startswith (varname, "__sync_")
8678 : 0 : || startswith (varname, "__atomic_")))
8679 : : {
8680 : 6 : error_at (varid_loc, "variant %qD is a built-in", variant);
8681 : 6 : return true;
8682 : : }
8683 : : else
8684 : : {
8685 : 1208 : tree construct
8686 : 1208 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
8687 : 1208 : omp_mark_declare_variant (match_loc, variant, construct);
8688 : 1208 : if (!omp_context_selector_matches (ctx, NULL_TREE, false))
8689 : : return true;
8690 : 860 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8691 : :
8692 : : // Prepend adjust_args list to variant attributes
8693 : 860 : tree adjust_args_list = TREE_CHAIN (TREE_CHAIN (chain));
8694 : 860 : if (adjust_args_list != NULL_TREE)
8695 : : {
8696 : 507 : if (DECL_NONSTATIC_MEMBER_P (variant)
8697 : 285 : && TREE_VALUE (adjust_args_list))
8698 : : {
8699 : : /* Shift arg position for the added 'this' pointer. */
8700 : : /* Handle need_device_ptr */
8701 : 21 : for (tree t = TREE_PURPOSE (TREE_VALUE (adjust_args_list));
8702 : 51 : t; t = TREE_CHAIN (t))
8703 : 30 : TREE_VALUE (t)
8704 : 30 : = build_int_cst (TREE_TYPE (t),
8705 : 30 : tree_to_uhwi (TREE_VALUE (t)) + 1);
8706 : : }
8707 : 264 : if (DECL_NONSTATIC_MEMBER_P (variant) && append_args_list)
8708 : : {
8709 : : /* Shift likewise the number of args after which the
8710 : : interop object should be added. */
8711 : 6 : tree nargs = TREE_CHAIN (TREE_VALUE (adjust_args_list));
8712 : 6 : TREE_PURPOSE (nargs)
8713 : 6 : = build_int_cst (TREE_TYPE (nargs),
8714 : 6 : tree_to_uhwi (TREE_PURPOSE (nargs)) + 1);
8715 : : }
8716 : 528 : for (tree t = append_args_list; t; t = TREE_CHAIN (t))
8717 : 264 : TREE_VALUE (t)
8718 : 264 : = cp_finish_omp_init_prefer_type (TREE_VALUE (t));
8719 : 528 : DECL_ATTRIBUTES (variant) = tree_cons (
8720 : : get_identifier ("omp declare variant variant args"),
8721 : 528 : TREE_VALUE (adjust_args_list), DECL_ATTRIBUTES (variant));
8722 : : }
8723 : : }
8724 : : }
8725 : 84 : else if (!processing_template_decl)
8726 : : {
8727 : 0 : error_at (varid_loc, "could not find variant declaration");
8728 : 0 : return true;
8729 : : }
8730 : :
8731 : : return false;
8732 : 1436 : }
8733 : :
8734 : : /* Helper function, finish up "omp declare variant base" attribute
8735 : : now that there is a DECL. ATTR is the first "omp declare variant base"
8736 : : attribute. */
8737 : :
8738 : : void
8739 : 1350 : omp_declare_variant_finalize (tree decl, tree attr)
8740 : : {
8741 : 1350 : size_t attr_len = strlen ("omp declare variant base");
8742 : 1350 : tree *list = &DECL_ATTRIBUTES (decl);
8743 : 1350 : bool remove_all = false;
8744 : 1350 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
8745 : 1350 : if (TREE_CHAIN (TREE_VALUE (attr))
8746 : 1350 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8747 : 2700 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8748 : 1350 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8749 : 2700 : if (DECL_CONSTRUCTOR_P (decl))
8750 : : {
8751 : 27 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8752 : 27 : remove_all = true;
8753 : : }
8754 : 1323 : else if (DECL_DESTRUCTOR_P (decl))
8755 : : {
8756 : 9 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8757 : 9 : remove_all = true;
8758 : : }
8759 : 1314 : else if (DECL_DEFAULTED_FN (decl))
8760 : : {
8761 : 2 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8762 : 2 : remove_all = true;
8763 : : }
8764 : 1312 : else if (DECL_DELETED_FN (decl))
8765 : : {
8766 : 2 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8767 : 2 : remove_all = true;
8768 : : }
8769 : 1310 : else if (DECL_VIRTUAL_P (decl))
8770 : : {
8771 : 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8772 : 0 : remove_all = true;
8773 : : }
8774 : : /* This loop is like private_lookup_attribute, except that it works
8775 : : with tree * rather than tree, as we might want to remove the
8776 : : attributes that are diagnosed as errorneous. */
8777 : 2889 : while (*list)
8778 : : {
8779 : 1539 : tree attr = get_attribute_name (*list);
8780 : 1539 : size_t ident_len = IDENTIFIER_LENGTH (attr);
8781 : 4022 : if (cmp_attribs ("omp declare variant base", attr_len,
8782 : 1539 : IDENTIFIER_POINTER (attr), ident_len))
8783 : : {
8784 : 1527 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8785 : : {
8786 : 583 : *list = TREE_CHAIN (*list);
8787 : 583 : continue;
8788 : : }
8789 : : }
8790 : 956 : list = &TREE_CHAIN (*list);
8791 : : }
8792 : 1350 : }
8793 : :
8794 : : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
8795 : :
8796 : : /* Finish processing of a declaration;
8797 : : install its line number and initial value.
8798 : : If the length of an array type is not known before,
8799 : : it must be determined now, from the initial value, or it is an error.
8800 : :
8801 : : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8802 : : true, then INIT is an integral constant expression.
8803 : :
8804 : : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8805 : : if the (init) syntax was used.
8806 : :
8807 : : DECOMP is first identifier's DECL and identifier count in a structured
8808 : : bindings, nullptr if not a structured binding. */
8809 : :
8810 : : void
8811 : 279393252 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8812 : : tree asmspec_tree, int flags, cp_decomp *decomp)
8813 : : {
8814 : 279393252 : vec<tree, va_gc> *cleanups = NULL;
8815 : 279393252 : const char *asmspec = NULL;
8816 : 279393252 : int was_readonly = 0;
8817 : 279393252 : bool var_definition_p = false;
8818 : 279393252 : tree auto_node;
8819 : 279393252 : auto_vec<tree> extra_cleanups;
8820 : 279393252 : tree aggregates1 = NULL_TREE;
8821 : 279393252 : struct decomp_cleanup {
8822 : : tree decl;
8823 : : cp_decomp *&decomp;
8824 : 279390555 : ~decomp_cleanup ()
8825 : : {
8826 : 279390555 : if (decomp && DECL_DECOMPOSITION_P (decl))
8827 : 78243 : cp_finish_decomp (decl, decomp);
8828 : 279390555 : }
8829 : 279393252 : } decomp_cl = { decl, decomp };
8830 : :
8831 : 279393252 : if (decl == error_mark_node)
8832 : : return;
8833 : 279393215 : else if (! decl)
8834 : : {
8835 : 0 : if (init)
8836 : 0 : error ("assignment (not initialization) in declaration");
8837 : 0 : return;
8838 : : }
8839 : :
8840 : 279393215 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8841 : : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8842 : 279393215 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
8843 : :
8844 : 279393215 : tree type = TREE_TYPE (decl);
8845 : 279393215 : if (type == error_mark_node)
8846 : : return;
8847 : :
8848 : 279392684 : if (VAR_P (decl) && is_copy_initialization (init))
8849 : 76860351 : flags |= LOOKUP_ONLYCONVERTING;
8850 : :
8851 : : /* Warn about register storage specifiers except when in GNU global
8852 : : or local register variable extension. */
8853 : 279392684 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8854 : : {
8855 : 1928 : if (cxx_dialect >= cxx17)
8856 : 1280 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8857 : : "ISO C++17 does not allow %<register%> storage "
8858 : : "class specifier");
8859 : : else
8860 : 648 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8861 : : "%<register%> storage class specifier used");
8862 : : }
8863 : :
8864 : : /* If a name was specified, get the string. */
8865 : 279392684 : if (at_namespace_scope_p ())
8866 : 59089003 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8867 : 279392684 : if (asmspec_tree && asmspec_tree != error_mark_node)
8868 : 975506 : asmspec = TREE_STRING_POINTER (asmspec_tree);
8869 : :
8870 : 279392684 : bool in_class_decl
8871 : 279392684 : = (current_class_type
8872 : 172782004 : && CP_DECL_CONTEXT (decl) == current_class_type
8873 : 115996731 : && TYPE_BEING_DEFINED (current_class_type)
8874 : 393897024 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8875 : :
8876 : 111323768 : if (in_class_decl
8877 : 111323768 : && (DECL_INITIAL (decl) || init))
8878 : 80162117 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8879 : :
8880 : 279392684 : if (VAR_P (decl)
8881 : 279392684 : && (auto_node = type_uses_auto (type)))
8882 : : {
8883 : 8525412 : tree d_init;
8884 : 8525412 : if (init == NULL_TREE)
8885 : : {
8886 : 1031 : if (DECL_LANG_SPECIFIC (decl)
8887 : 953 : && DECL_TEMPLATE_INSTANTIATION (decl)
8888 : 1981 : && !DECL_TEMPLATE_INSTANTIATED (decl))
8889 : : {
8890 : : /* init is null because we're deferring instantiating the
8891 : : initializer until we need it. Well, we need it now. */
8892 : 944 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8893 : 944 : return;
8894 : : }
8895 : :
8896 : 87 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8897 : : /* Class deduction with no initializer is OK. */;
8898 : : else
8899 : : {
8900 : : /* Ordinary auto deduction without an initializer, a situation
8901 : : which grokdeclarator already detects and rejects for the most
8902 : : part. But we can still get here if we're instantiating a
8903 : : variable template before we've fully parsed (and attached) its
8904 : : initializer, e.g. template<class> auto x = x<int>; */
8905 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
8906 : : "declaration of %q#D has no initializer", decl);
8907 : 3 : TREE_TYPE (decl) = error_mark_node;
8908 : 3 : return;
8909 : : }
8910 : : }
8911 : 8524465 : d_init = init;
8912 : 8524465 : if (d_init)
8913 : : {
8914 : 8524381 : if (TREE_CODE (d_init) == TREE_LIST
8915 : 8564508 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8916 : 22156 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8917 : : tf_warning_or_error);
8918 : 8524381 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8919 : : /* Force auto deduction now. Use tf_none to avoid redundant warnings
8920 : : on deprecated-14.C. */
8921 : 8524381 : mark_single_function (d_init, tf_none);
8922 : : }
8923 : 8524465 : enum auto_deduction_context adc = adc_variable_type;
8924 : 8524465 : if (DECL_DECOMPOSITION_P (decl))
8925 : : adc = adc_decomp_type;
8926 : 8524465 : tree outer_targs = NULL_TREE;
8927 : 8524465 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8928 : 323 : && DECL_LANG_SPECIFIC (decl)
8929 : 126 : && DECL_TEMPLATE_INFO (decl)
8930 : 8524522 : && !DECL_FUNCTION_SCOPE_P (decl))
8931 : : /* The outer template arguments might be needed for satisfaction.
8932 : : (For function scope variables, do_auto_deduction will obtain the
8933 : : outer template arguments from current_function_decl.) */
8934 : 57 : outer_targs = DECL_TI_ARGS (decl);
8935 : 8524465 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8936 : : tf_warning_or_error, adc,
8937 : : outer_targs, flags);
8938 : 8524465 : if (type == error_mark_node)
8939 : : return;
8940 : 8523624 : if (TREE_CODE (type) == FUNCTION_TYPE)
8941 : : {
8942 : 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
8943 : : "did you forget the %<()%>?", decl);
8944 : 3 : TREE_TYPE (decl) = error_mark_node;
8945 : 3 : return;
8946 : : }
8947 : : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8948 : 8523621 : if (!processing_template_decl
8949 : 1959640 : && !type_uses_auto (type)
8950 : 10483254 : && !COMPLETE_TYPE_P (complete_type (type)))
8951 : : {
8952 : 18 : auto_diagnostic_group d;
8953 : 18 : error_at (location_of (decl),
8954 : : "deduced type %qT for %qD is incomplete", type, decl);
8955 : 18 : cxx_incomplete_type_inform (type);
8956 : 18 : TREE_TYPE (decl) = error_mark_node;
8957 : 18 : return;
8958 : 18 : }
8959 : :
8960 : : /* Now that we have a type, try these again. */
8961 : 8523603 : layout_decl (decl, 0);
8962 : 8523603 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8963 : :
8964 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
8965 : 8523603 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
8966 : : {
8967 : 11463 : tree tmpl = template_for_substitution (decl);
8968 : 11463 : if (DECL_TEMPLATE_RESULT (tmpl) == decl)
8969 : 10199 : TREE_TYPE (tmpl) = type;
8970 : : }
8971 : : }
8972 : :
8973 : 279390875 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8974 : : {
8975 : 61 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8976 : 61 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8977 : : {
8978 : 6 : init = NULL_TREE;
8979 : 6 : DECL_EXTERNAL (decl) = 1;
8980 : : }
8981 : : }
8982 : :
8983 : 279390875 : if (VAR_P (decl)
8984 : 98242438 : && DECL_CLASS_SCOPE_P (decl)
8985 : 14835383 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
8986 : : TCTX_STATIC_STORAGE, type)
8987 : 294226258 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8988 : 13700729 : check_static_variable_definition (decl, type);
8989 : :
8990 : 279390875 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8991 : : {
8992 : 22669464 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8993 : 22669464 : ? TCTX_THREAD_STORAGE
8994 : : : TCTX_STATIC_STORAGE);
8995 : 22669464 : verify_type_context (input_location, context, TREE_TYPE (decl));
8996 : : }
8997 : :
8998 : 279390875 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
8999 : : {
9000 : 351404 : tree clone;
9001 : 351404 : if (init == ridpointers[(int)RID_DELETE]
9002 : 351404 : || (TREE_CODE (init) == STRING_CST
9003 : 24 : && TREE_TYPE (init) == ridpointers[(int)RID_DELETE]))
9004 : : {
9005 : : /* FIXME check this is 1st decl. */
9006 : 328516 : if (UNLIKELY (DECL_MAIN_P (decl)))
9007 : : {
9008 : : /* [basic.start.main]/3: A program that defines main as deleted
9009 : : is ill-formed. */
9010 : 3 : error ("%<::main%> cannot be deleted");
9011 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9012 : : }
9013 : : else
9014 : : {
9015 : 328513 : DECL_DELETED_FN (decl) = 1;
9016 : 328513 : DECL_DECLARED_INLINE_P (decl) = 1;
9017 : 328513 : DECL_INITIAL (decl)
9018 : 328513 : = TREE_CODE (init) == STRING_CST ? init : error_mark_node;
9019 : 328531 : FOR_EACH_CLONE (clone, decl)
9020 : : {
9021 : 18 : DECL_DELETED_FN (clone) = 1;
9022 : 18 : DECL_DECLARED_INLINE_P (clone) = 1;
9023 : 18 : DECL_INITIAL (clone) = DECL_INITIAL (decl);
9024 : : }
9025 : : }
9026 : 328516 : init = NULL_TREE;
9027 : : }
9028 : 22888 : else if (init == ridpointers[(int)RID_DEFAULT])
9029 : : {
9030 : 22876 : if (defaultable_fn_check (decl))
9031 : 22852 : DECL_DEFAULTED_FN (decl) = 1;
9032 : : else
9033 : 24 : DECL_INITIAL (decl) = NULL_TREE;
9034 : : }
9035 : : }
9036 : :
9037 : 279390875 : if (init && VAR_P (decl))
9038 : : {
9039 : 83930072 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
9040 : : /* If DECL is a reference, then we want to know whether init is a
9041 : : reference constant; init_const_expr_p as passed tells us whether
9042 : : it's an rvalue constant. */
9043 : 83930072 : if (TYPE_REF_P (type))
9044 : 2218446 : init_const_expr_p = potential_constant_expression (init);
9045 : 83930072 : if (init_const_expr_p)
9046 : : {
9047 : : /* Set these flags now for templates. We'll update the flags in
9048 : : store_init_value for instantiations. */
9049 : 71946131 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
9050 : 71946131 : if (decl_maybe_constant_var_p (decl)
9051 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
9052 : 71946131 : && !TYPE_REF_P (type))
9053 : 25955282 : TREE_CONSTANT (decl) = true;
9054 : : }
9055 : : /* This is handled mostly by gimplify.cc, but we have to deal with
9056 : : not warning about int x = x; as it is a GCC extension to turn off
9057 : : this warning but only if warn_init_self is zero. */
9058 : 83930072 : if (!DECL_EXTERNAL (decl)
9059 : 82495689 : && !TREE_STATIC (decl)
9060 : 62147946 : && decl == tree_strip_any_location_wrapper (init)
9061 : 83933331 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
9062 : 3235 : suppress_warning (decl, OPT_Winit_self);
9063 : : }
9064 : 195460803 : else if (VAR_P (decl)
9065 : 14312366 : && COMPLETE_TYPE_P (type)
9066 : 11220063 : && !TYPE_REF_P (type)
9067 : 11219699 : && !dependent_type_p (type)
9068 : 206392535 : && is_really_empty_class (type, /*ignore_vptr*/false))
9069 : : /* We have no initializer but there's nothing to initialize anyway.
9070 : : Treat DECL as constant due to c++/109876. */
9071 : 486234 : TREE_CONSTANT (decl) = true;
9072 : :
9073 : 279390875 : if (flag_openmp
9074 : 743831 : && TREE_CODE (decl) == FUNCTION_DECL
9075 : : /* #pragma omp declare variant on methods handled in finish_struct
9076 : : instead. */
9077 : 279729656 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
9078 : 129783 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
9079 : 209451 : if (tree attr = lookup_attribute ("omp declare variant base",
9080 : 209451 : DECL_ATTRIBUTES (decl)))
9081 : 967 : omp_declare_variant_finalize (decl, attr);
9082 : :
9083 : 279390875 : if (processing_template_decl)
9084 : : {
9085 : 161976782 : bool type_dependent_p;
9086 : :
9087 : : /* Add this declaration to the statement-tree. */
9088 : 161976782 : if (at_function_scope_p ())
9089 : 58791246 : add_decl_expr (decl);
9090 : :
9091 : 161976782 : type_dependent_p = dependent_type_p (type);
9092 : :
9093 : 161976782 : if (check_for_bare_parameter_packs (init))
9094 : : {
9095 : 3 : init = NULL_TREE;
9096 : 3 : DECL_INITIAL (decl) = NULL_TREE;
9097 : : }
9098 : :
9099 : : /* Generally, initializers in templates are expanded when the
9100 : : template is instantiated. But, if DECL is a variable constant
9101 : : then it can be used in future constant expressions, so its value
9102 : : must be available. */
9103 : :
9104 : 161976782 : bool dep_init = false;
9105 : :
9106 : 161976782 : if (!VAR_P (decl) || type_dependent_p)
9107 : : /* We can't do anything if the decl has dependent type. */;
9108 : 19643256 : else if (init
9109 : 18076390 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
9110 : 14274501 : && !TYPE_REF_P (type)
9111 : 14248535 : && decl_maybe_constant_var_p (decl)
9112 : 27078499 : && !(dep_init = value_dependent_init_p (init)))
9113 : : {
9114 : : /* This variable seems to be a non-dependent constant, so process
9115 : : its initializer. If check_initializer returns non-null the
9116 : : initialization wasn't constant after all. */
9117 : 1716639 : tree init_code;
9118 : 1716639 : cleanups = make_tree_vector ();
9119 : 1716639 : init_code = check_initializer (decl, init, flags, &cleanups);
9120 : 1716639 : if (init_code == NULL_TREE)
9121 : 1716639 : init = NULL_TREE;
9122 : 1716639 : release_tree_vector (cleanups);
9123 : : }
9124 : : else
9125 : : {
9126 : 17926617 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
9127 : : /* Try to deduce array size. */
9128 : 17926617 : maybe_deduce_size_from_array_init (decl, init);
9129 : : /* And complain about multiple initializers. */
9130 : 16359751 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
9131 : 17985997 : && !MAYBE_CLASS_TYPE_P (type))
9132 : 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
9133 : : tf_warning_or_error);
9134 : : }
9135 : :
9136 : 161976782 : if (init)
9137 : 46971344 : DECL_INITIAL (decl) = init;
9138 : :
9139 : 161976782 : if (dep_init)
9140 : : {
9141 : 5718604 : retrofit_lang_decl (decl);
9142 : 5718604 : SET_DECL_DEPENDENT_INIT_P (decl, true);
9143 : : }
9144 : :
9145 : 161976782 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
9146 : : {
9147 : 3 : set_user_assembler_name (decl, asmspec);
9148 : 3 : DECL_HARD_REGISTER (decl) = 1;
9149 : : }
9150 : 161976782 : return;
9151 : : }
9152 : :
9153 : : /* Just store non-static data member initializers for later. */
9154 : 117414093 : if (init && TREE_CODE (decl) == FIELD_DECL)
9155 : 383185 : DECL_INITIAL (decl) = init;
9156 : :
9157 : : /* Take care of TYPE_DECLs up front. */
9158 : 117414093 : if (TREE_CODE (decl) == TYPE_DECL)
9159 : : {
9160 : 9576895 : if (type != error_mark_node
9161 : 9576895 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
9162 : : {
9163 : 4339648 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
9164 : 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
9165 : 4339648 : set_identifier_type_value (DECL_NAME (decl), decl);
9166 : : }
9167 : :
9168 : : /* If we have installed this as the canonical typedef for this
9169 : : type, and that type has not been defined yet, delay emitting
9170 : : the debug information for it, as we will emit it later. */
9171 : 9576895 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
9172 : 9576895 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9173 : 556954 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
9174 : :
9175 : 9576895 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
9176 : : at_eof);
9177 : 9576895 : return;
9178 : : }
9179 : :
9180 : : /* A reference will be modified here, as it is initialized. */
9181 : 107837198 : if (! DECL_EXTERNAL (decl)
9182 : 47959685 : && TREE_READONLY (decl)
9183 : 132129045 : && TYPE_REF_P (type))
9184 : : {
9185 : 229188 : was_readonly = 1;
9186 : 229188 : TREE_READONLY (decl) = 0;
9187 : : }
9188 : :
9189 : : /* This needs to happen before extend_ref_init_temps. */
9190 : 107837198 : if (VAR_OR_FUNCTION_DECL_P (decl))
9191 : : {
9192 : 99296356 : if (VAR_P (decl))
9193 : 45666040 : maybe_commonize_var (decl);
9194 : 99296356 : determine_visibility (decl);
9195 : : }
9196 : :
9197 : 107837198 : if (VAR_P (decl))
9198 : : {
9199 : 45666040 : duration_kind dk = decl_storage_duration (decl);
9200 : : /* [dcl.constinit]/1 "The constinit specifier shall be applied
9201 : : only to a declaration of a variable with static or thread storage
9202 : : duration." */
9203 : 45666040 : if (DECL_DECLARED_CONSTINIT_P (decl)
9204 : 45666040 : && !(dk == dk_thread || dk == dk_static))
9205 : : {
9206 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
9207 : : "%<constinit%> can only be applied to a variable with "
9208 : : "static or thread storage duration");
9209 : 24 : return;
9210 : : }
9211 : :
9212 : 45666016 : if (decomp)
9213 : : {
9214 : 36771 : if (DECL_DECLARED_CONSTINIT_P (decl) && cxx_dialect < cxx26)
9215 : 40 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__26_extensions,
9216 : : "%<constinit%> can be applied to structured binding "
9217 : : "only with %<-std=c++2c%> or %<-std=gnu++2c%>");
9218 : 36771 : cp_maybe_mangle_decomp (decl, decomp);
9219 : 36771 : if (TREE_STATIC (decl) && !DECL_FUNCTION_SCOPE_P (decl))
9220 : : {
9221 : 343 : if (CP_DECL_THREAD_LOCAL_P (decl))
9222 : 37 : aggregates1 = tls_aggregates;
9223 : : else
9224 : 306 : aggregates1 = static_aggregates;
9225 : : }
9226 : : }
9227 : :
9228 : : /* If this is a local variable that will need a mangled name,
9229 : : register it now. We must do this before processing the
9230 : : initializer for the variable, since the initialization might
9231 : : require a guard variable, and since the mangled name of the
9232 : : guard variable will depend on the mangled name of this
9233 : : variable. */
9234 : 91332032 : if (DECL_FUNCTION_SCOPE_P (decl)
9235 : 23137832 : && TREE_STATIC (decl)
9236 : 45807438 : && !DECL_ARTIFICIAL (decl))
9237 : : {
9238 : : /* The variable holding an anonymous union will have had its
9239 : : discriminator set in finish_anon_union, after which it's
9240 : : NAME will have been cleared. */
9241 : 140536 : if (DECL_NAME (decl))
9242 : 140493 : determine_local_discriminator (decl);
9243 : : /* Normally has_forced_label_in_static is set during GIMPLE
9244 : : lowering, but [cd]tors are never actually compiled directly.
9245 : : We need to set this early so we can deal with the label
9246 : : address extension. */
9247 : 140536 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
9248 : 140507 : || DECL_DESTRUCTOR_P (current_function_decl))
9249 : 140546 : && init)
9250 : : {
9251 : 29 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
9252 : 29 : add_local_decl (cfun, decl);
9253 : : }
9254 : : /* And make sure it's in the symbol table for
9255 : : c_parse_final_cleanups to find. */
9256 : 140536 : varpool_node::get_create (decl);
9257 : : }
9258 : :
9259 : 45666016 : if (flag_openmp
9260 : 135407 : && VAR_P (decl)
9261 : 135407 : && DECL_LANG_SPECIFIC (decl)
9262 : 60827 : && DECL_OMP_DECLARE_MAPPER_P (decl)
9263 : 45666020 : && init)
9264 : : {
9265 : 3 : gcc_assert (TREE_CODE (init) == OMP_DECLARE_MAPPER);
9266 : 3 : DECL_INITIAL (decl) = init;
9267 : : }
9268 : : /* Convert the initializer to the type of DECL, if we have not
9269 : : already initialized DECL. */
9270 : 45666013 : else if (!DECL_INITIALIZED_P (decl)
9271 : : /* If !DECL_EXTERNAL then DECL is being defined. In the
9272 : : case of a static data member initialized inside the
9273 : : class-specifier, there can be an initializer even if DECL
9274 : : is *not* defined. */
9275 : 45666013 : && (!DECL_EXTERNAL (decl) || init))
9276 : : {
9277 : 40347394 : cleanups = make_tree_vector ();
9278 : 40347394 : init = check_initializer (decl, init, flags, &cleanups);
9279 : :
9280 : : /* Handle:
9281 : :
9282 : : [dcl.init]
9283 : :
9284 : : The memory occupied by any object of static storage
9285 : : duration is zero-initialized at program startup before
9286 : : any other initialization takes place.
9287 : :
9288 : : We cannot create an appropriate initializer until after
9289 : : the type of DECL is finalized. If DECL_INITIAL is set,
9290 : : then the DECL is statically initialized, and any
9291 : : necessary zero-initialization has already been performed. */
9292 : 40344697 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
9293 : 415307 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9294 : : /*nelts=*/NULL_TREE,
9295 : : /*static_storage_p=*/true);
9296 : : /* Remember that the initialization for this variable has
9297 : : taken place. */
9298 : 40344697 : DECL_INITIALIZED_P (decl) = 1;
9299 : : /* This declaration is the definition of this variable,
9300 : : unless we are initializing a static data member within
9301 : : the class specifier. */
9302 : 40344697 : if (!DECL_EXTERNAL (decl))
9303 : 45663319 : var_definition_p = true;
9304 : : }
9305 : : /* If the variable has an array type, lay out the type, even if
9306 : : there is no initializer. It is valid to index through the
9307 : : array, and we must get TYPE_ALIGN set correctly on the array
9308 : : type. */
9309 : 5318619 : else if (TREE_CODE (type) == ARRAY_TYPE)
9310 : 219707 : layout_type (type);
9311 : :
9312 : 45663319 : if (TREE_STATIC (decl)
9313 : 22105140 : && !at_function_scope_p ()
9314 : 67627037 : && current_function_decl == NULL)
9315 : : /* So decl is a global variable or a static member of a
9316 : : non local class. Record the types it uses
9317 : : so that we can decide later to emit debug info for them. */
9318 : 21963706 : record_types_used_by_current_var_decl (decl);
9319 : : }
9320 : :
9321 : : /* Add this declaration to the statement-tree. This needs to happen
9322 : : after the call to check_initializer so that the DECL_EXPR for a
9323 : : reference temp is added before the DECL_EXPR for the reference itself. */
9324 : 107834477 : if (DECL_FUNCTION_SCOPE_P (decl))
9325 : : {
9326 : : /* If we're building a variable sized type, and we might be
9327 : : reachable other than via the top of the current binding
9328 : : level, then create a new BIND_EXPR so that we deallocate
9329 : : the object at the right time. */
9330 : 23137832 : if (VAR_P (decl)
9331 : 23137832 : && DECL_SIZE (decl)
9332 : 23088466 : && !TREE_CONSTANT (DECL_SIZE (decl))
9333 : 23138825 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
9334 : : {
9335 : 3 : tree bind;
9336 : 3 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
9337 : 3 : TREE_SIDE_EFFECTS (bind) = 1;
9338 : 3 : add_stmt (bind);
9339 : 3 : BIND_EXPR_BODY (bind) = push_stmt_list ();
9340 : : }
9341 : 23137832 : add_decl_expr (decl);
9342 : : }
9343 : :
9344 : 107834477 : tree decomp_init = NULL_TREE;
9345 : : /* Let the middle end know about variables and functions -- but not
9346 : : static data members in uninstantiated class templates. */
9347 : 107834477 : if (VAR_OR_FUNCTION_DECL_P (decl))
9348 : : {
9349 : 99293635 : if (VAR_P (decl))
9350 : : {
9351 : 45663319 : layout_var_decl (decl);
9352 : 45663319 : if (!flag_weak)
9353 : : /* Check again now that we have an initializer. */
9354 : 57 : maybe_commonize_var (decl);
9355 : : /* A class-scope constexpr variable with an out-of-class declaration.
9356 : : C++17 makes them implicitly inline, but still force it out. */
9357 : 61695611 : if (DECL_INLINE_VAR_P (decl)
9358 : 15738898 : && !DECL_VAR_DECLARED_INLINE_P (decl)
9359 : 8923722 : && !DECL_TEMPLATE_INSTANTIATION (decl)
9360 : 29754002 : && !in_class_decl)
9361 : 103 : mark_needed (decl);
9362 : : }
9363 : :
9364 : 99293635 : if (var_definition_p
9365 : : /* With -fmerge-all-constants, gimplify_init_constructor
9366 : : might add TREE_STATIC to aggregate variables. */
9367 : 39350779 : && (TREE_STATIC (decl)
9368 : 22996552 : || (flag_merge_constants >= 2
9369 : 38 : && AGGREGATE_TYPE_P (type))))
9370 : : {
9371 : : /* If a TREE_READONLY variable needs initialization
9372 : : at runtime, it is no longer readonly and we need to
9373 : : avoid MEM_READONLY_P being set on RTL created for it. */
9374 : 16354230 : if (init)
9375 : : {
9376 : 14237 : if (TREE_READONLY (decl))
9377 : 521 : TREE_READONLY (decl) = 0;
9378 : : was_readonly = 0;
9379 : : }
9380 : 16339993 : else if (was_readonly)
9381 : 1728 : TREE_READONLY (decl) = 1;
9382 : :
9383 : : /* Likewise if it needs destruction. */
9384 : 16354230 : if (!decl_maybe_constant_destruction (decl, type))
9385 : 3424 : TREE_READONLY (decl) = 0;
9386 : : }
9387 : 82939405 : else if (VAR_P (decl)
9388 : 29309089 : && CP_DECL_THREAD_LOCAL_P (decl)
9389 : 19894 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
9390 : 19894 : && (was_readonly || TREE_READONLY (decl))
9391 : 82939420 : && var_needs_tls_wrapper (decl))
9392 : : {
9393 : : /* TLS variables need dynamic initialization by the TLS wrapper
9394 : : function, we don't want to hoist accesses to it before the
9395 : : wrapper. */
9396 : 6 : was_readonly = 0;
9397 : 6 : TREE_READONLY (decl) = 0;
9398 : : }
9399 : :
9400 : 99293635 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
9401 : :
9402 : : /* Check for abstractness of the type. */
9403 : 99293635 : if (var_definition_p)
9404 : 39350779 : abstract_virtuals_error (decl, type);
9405 : :
9406 : 99293635 : if (decomp && !cp_finish_decomp (decl, decomp, true))
9407 : 17960 : decomp = NULL;
9408 : :
9409 : 99293635 : if (TREE_TYPE (decl) == error_mark_node)
9410 : : /* No initialization required. */
9411 : : ;
9412 : 99293564 : else if (TREE_CODE (decl) == FUNCTION_DECL)
9413 : : {
9414 : 53630316 : if (init)
9415 : : {
9416 : 582 : if (init == ridpointers[(int)RID_DEFAULT])
9417 : : {
9418 : : /* An out-of-class default definition is defined at
9419 : : the point where it is explicitly defaulted. */
9420 : 570 : if (DECL_DELETED_FN (decl))
9421 : 6 : maybe_explain_implicit_delete (decl);
9422 : 564 : else if (DECL_INITIAL (decl) == error_mark_node)
9423 : 540 : synthesize_method (decl);
9424 : : }
9425 : : else
9426 : 24 : error_at (cp_expr_loc_or_loc (init,
9427 : 12 : DECL_SOURCE_LOCATION (decl)),
9428 : : "function %q#D is initialized like a variable",
9429 : : decl);
9430 : : }
9431 : : /* else no initialization required. */
9432 : : }
9433 : 45663248 : else if (DECL_EXTERNAL (decl)
9434 : 45663248 : && ! (DECL_LANG_SPECIFIC (decl)
9435 : 6006608 : && DECL_NOT_REALLY_EXTERN (decl)))
9436 : : {
9437 : : /* check_initializer will have done any constant initialization. */
9438 : : }
9439 : : /* A variable definition. */
9440 : 41181740 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9441 : : {
9442 : : /* Initialize the local variable. */
9443 : 22996392 : if (!decomp)
9444 : 22977820 : initialize_local_var (decl, init, false);
9445 : : else
9446 : : {
9447 : 18572 : tree cleanup = NULL_TREE;
9448 : 18572 : if (DECL_SIZE (decl))
9449 : 18572 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9450 : : /* If cp_finish_decomp needs to emit any code, we need to emit that
9451 : : code after code emitted by initialize_local_var in a single
9452 : : CLEANUP_POINT_EXPR, so that temporaries are destructed only
9453 : : after the cp_finish_decomp emitted code.
9454 : : If there are any cleanups, either extend_ref_init_temps
9455 : : created ones or e.g. array destruction, push those first
9456 : : with the cleanups guarded on a bool temporary, initially
9457 : : set to false and set to true after initialize_local_var
9458 : : emitted code. */
9459 : 18572 : tree guard = NULL_TREE;
9460 : 18572 : if (cleanups || cleanup)
9461 : : {
9462 : 18572 : guard = get_internal_target_expr (boolean_false_node);
9463 : 18572 : add_stmt (guard);
9464 : 18572 : guard = TARGET_EXPR_SLOT (guard);
9465 : : }
9466 : 18572 : tree sl = push_stmt_list ();
9467 : 18572 : initialize_local_var (decl, init, true);
9468 : 18572 : if (guard)
9469 : : {
9470 : 18572 : add_stmt (build2 (MODIFY_EXPR, boolean_type_node,
9471 : : guard, boolean_true_node));
9472 : 18590 : for (tree &t : *cleanups)
9473 : 18 : t = build3 (COND_EXPR, void_type_node,
9474 : : guard, t, void_node);
9475 : 18572 : if (cleanup)
9476 : 25 : cleanup = build3 (COND_EXPR, void_type_node,
9477 : : guard, cleanup, void_node);
9478 : : }
9479 : 18572 : unsigned before = stmt_list_stack->length ();
9480 : 18572 : cp_finish_decomp (decl, decomp);
9481 : 18572 : decomp = NULL;
9482 : 18572 : unsigned n_extra_cleanups = stmt_list_stack->length () - before;
9483 : 18572 : sl = pop_stmt_list (sl);
9484 : 18572 : if (n_extra_cleanups)
9485 : : {
9486 : : /* If cp_finish_decomp needs any cleanups, such as for
9487 : : extend_ref_init_temps created vars, pop_stmt_list
9488 : : popped that all, so push those extra cleanups around
9489 : : the whole sequence with a guard variable. */
9490 : 15 : gcc_assert (TREE_CODE (sl) == STATEMENT_LIST);
9491 : 15 : guard = get_internal_target_expr (integer_zero_node);
9492 : 15 : add_stmt (guard);
9493 : 15 : guard = TARGET_EXPR_SLOT (guard);
9494 : 63 : for (unsigned i = 0; i < n_extra_cleanups; ++i)
9495 : : {
9496 : 48 : tree_stmt_iterator tsi = tsi_last (sl);
9497 : 48 : gcc_assert (!tsi_end_p (tsi));
9498 : 48 : tree last = tsi_stmt (tsi);
9499 : 48 : gcc_assert (TREE_CODE (last) == CLEANUP_STMT
9500 : : && !CLEANUP_EH_ONLY (last));
9501 : 48 : tree cst = build_int_cst (integer_type_node, i + 1);
9502 : 96 : tree cl = build3 (COND_EXPR, void_type_node,
9503 : : build2 (GE_EXPR, boolean_type_node,
9504 : : guard, cst),
9505 : 48 : CLEANUP_EXPR (last), void_node);
9506 : 48 : extra_cleanups.safe_push (cl);
9507 : 48 : tsi_link_before (&tsi, build2 (MODIFY_EXPR,
9508 : : integer_type_node,
9509 : : guard, cst),
9510 : : TSI_SAME_STMT);
9511 : 48 : tree sl2 = CLEANUP_BODY (last);
9512 : 48 : gcc_assert (TREE_CODE (sl2) == STATEMENT_LIST);
9513 : 48 : tsi_link_before (&tsi, sl2, TSI_SAME_STMT);
9514 : 48 : tsi_delink (&tsi);
9515 : : }
9516 : : }
9517 : 18572 : decomp_init = maybe_cleanup_point_expr_void (sl);
9518 : 18572 : if (cleanup)
9519 : 25 : finish_decl_cleanup (decl, cleanup);
9520 : : }
9521 : : }
9522 : :
9523 : : /* If a variable is defined, and then a subsequent
9524 : : definition with external linkage is encountered, we will
9525 : : get here twice for the same variable. We want to avoid
9526 : : calling expand_static_init more than once. For variables
9527 : : that are not static data members, we can call
9528 : : expand_static_init only when we actually process the
9529 : : initializer. It is not legal to redeclare a static data
9530 : : member, so this issue does not arise in that case. */
9531 : 18185348 : else if (var_definition_p && TREE_STATIC (decl))
9532 : : {
9533 : 16354184 : if (decomp && DECL_FUNCTION_SCOPE_P (decl))
9534 : : {
9535 : 98 : tree sl = push_stmt_list ();
9536 : 98 : auto saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
9537 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p = 0;
9538 : 98 : expand_static_init (decl, init);
9539 : 98 : current_stmt_tree ()->stmts_are_full_exprs_p
9540 : 98 : = saved_stmts_are_full_exprs_p;
9541 : 98 : cp_finish_decomp (decl, decomp);
9542 : 98 : decomp = NULL;
9543 : 98 : sl = pop_stmt_list (sl);
9544 : 98 : sl = maybe_cleanup_point_expr_void (sl);
9545 : 98 : add_stmt (sl);
9546 : : }
9547 : : else
9548 : 16354086 : expand_static_init (decl, init);
9549 : : }
9550 : : }
9551 : :
9552 : : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9553 : : reference, insert it in the statement-tree now. */
9554 : 107834477 : if (cleanups)
9555 : : {
9556 : 40350724 : for (tree t : *cleanups)
9557 : : {
9558 : 6027 : push_cleanup (NULL_TREE, t, false);
9559 : : /* As in initialize_local_var. */
9560 : 6027 : wrap_temporary_cleanups (init, t);
9561 : : }
9562 : 40344697 : release_tree_vector (cleanups);
9563 : : }
9564 : :
9565 : 107834555 : for (tree t : &extra_cleanups)
9566 : 48 : push_cleanup (NULL_TREE, t, false);
9567 : :
9568 : 107834477 : if (decomp_init)
9569 : 18572 : add_stmt (decomp_init);
9570 : :
9571 : 107834477 : if (decomp
9572 : 141 : && var_definition_p
9573 : 141 : && TREE_STATIC (decl)
9574 : 107834618 : && !DECL_FUNCTION_SCOPE_P (decl))
9575 : : {
9576 : 141 : tree &aggregates3 = (CP_DECL_THREAD_LOCAL_P (decl)
9577 : 141 : ? tls_aggregates : static_aggregates);
9578 : 141 : tree aggregates2 = aggregates3;
9579 : 141 : if (aggregates2 != aggregates1)
9580 : : {
9581 : 66 : cp_finish_decomp (decl, decomp);
9582 : 66 : decomp = NULL;
9583 : 66 : if (aggregates3 != aggregates2)
9584 : : {
9585 : : /* If there are dynamic initializers for the structured
9586 : : binding base or associated extended ref temps and also
9587 : : dynamic initializers for the structured binding non-base
9588 : : vars, mark them. */
9589 : 315 : for (tree t = aggregates3; t != aggregates2; t = TREE_CHAIN (t))
9590 : 252 : STATIC_INIT_DECOMP_NONBASE_P (t) = 1;
9591 : 150 : for (tree t = aggregates2; t != aggregates1; t = TREE_CHAIN (t))
9592 : 87 : STATIC_INIT_DECOMP_BASE_P (t) = 1;
9593 : : }
9594 : : }
9595 : : }
9596 : :
9597 : 107834477 : if (was_readonly)
9598 : 228606 : TREE_READONLY (decl) = 1;
9599 : :
9600 : 107834477 : if (flag_openmp
9601 : 360279 : && VAR_P (decl)
9602 : 107969884 : && lookup_attribute ("omp declare target implicit",
9603 : 135407 : DECL_ATTRIBUTES (decl)))
9604 : : {
9605 : 52 : DECL_ATTRIBUTES (decl)
9606 : 52 : = remove_attribute ("omp declare target implicit",
9607 : 52 : DECL_ATTRIBUTES (decl));
9608 : 52 : complete_type (TREE_TYPE (decl));
9609 : 52 : if (!omp_mappable_type (TREE_TYPE (decl)))
9610 : : {
9611 : 30 : auto_diagnostic_group d;
9612 : 30 : error ("%q+D in declare target directive does not have mappable"
9613 : : " type", decl);
9614 : 30 : if (TREE_TYPE (decl) != error_mark_node
9615 : 30 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9616 : 30 : cxx_incomplete_type_inform (TREE_TYPE (decl));
9617 : 30 : }
9618 : 22 : else if (!lookup_attribute ("omp declare target",
9619 : 22 : DECL_ATTRIBUTES (decl))
9620 : 44 : && !lookup_attribute ("omp declare target link",
9621 : 22 : DECL_ATTRIBUTES (decl)))
9622 : : {
9623 : 22 : DECL_ATTRIBUTES (decl)
9624 : 22 : = tree_cons (get_identifier ("omp declare target"),
9625 : 22 : NULL_TREE, DECL_ATTRIBUTES (decl));
9626 : 22 : symtab_node *node = symtab_node::get (decl);
9627 : 22 : if (node != NULL)
9628 : : {
9629 : 19 : node->offloadable = 1;
9630 : 19 : if (ENABLE_OFFLOADING)
9631 : : {
9632 : : g->have_offload = true;
9633 : : if (is_a <varpool_node *> (node))
9634 : : vec_safe_push (offload_vars, decl);
9635 : : }
9636 : : }
9637 : : }
9638 : : }
9639 : :
9640 : : /* This is the last point we can lower alignment so give the target the
9641 : : chance to do so. */
9642 : 107834477 : if (VAR_P (decl)
9643 : 45663319 : && !is_global_var (decl)
9644 : 130831029 : && !DECL_HARD_REGISTER (decl))
9645 : 22996502 : targetm.lower_local_decl_alignment (decl);
9646 : :
9647 : 107834477 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
9648 : 279390555 : }
9649 : :
9650 : : /* For class TYPE return itself or some its bases that contain
9651 : : any direct non-static data members. Return error_mark_node if an
9652 : : error has been diagnosed. */
9653 : :
9654 : : static tree
9655 : 22404 : find_decomp_class_base (location_t loc, tree type, tree ret,
9656 : : tsubst_flags_t complain)
9657 : : {
9658 : 44808 : if (LAMBDA_TYPE_P (type))
9659 : : {
9660 : 12 : if (complain & tf_error)
9661 : : {
9662 : 12 : auto_diagnostic_group d;
9663 : 12 : error_at (loc, "cannot decompose lambda closure type %qT", type);
9664 : 12 : inform (location_of (type), "lambda declared here");
9665 : 12 : }
9666 : 12 : return error_mark_node;
9667 : : }
9668 : :
9669 : 22392 : bool member_seen = false;
9670 : 316420 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9671 : 542676 : if (TREE_CODE (field) != FIELD_DECL
9672 : 45588 : || DECL_ARTIFICIAL (field)
9673 : 339561 : || DECL_UNNAMED_BIT_FIELD (field))
9674 : 248609 : continue;
9675 : 45458 : else if (ret)
9676 : : return type;
9677 : 45432 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9678 : : {
9679 : 8 : if ((complain & tf_error) == 0)
9680 : 0 : return error_mark_node;
9681 : 8 : auto_diagnostic_group d;
9682 : 8 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9683 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
9684 : : "anonymous struct member", type);
9685 : : else
9686 : 4 : error_at (loc, "cannot decompose class type %qT because it has an "
9687 : : "anonymous union member", type);
9688 : 8 : inform (DECL_SOURCE_LOCATION (field), "declared here");
9689 : 8 : return error_mark_node;
9690 : 8 : }
9691 : 45424 : else if (!accessible_p (type, field, true))
9692 : : {
9693 : 5 : if ((complain & tf_error) == 0)
9694 : 0 : return error_mark_node;
9695 : 5 : auto_diagnostic_group d;
9696 : 5 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
9697 : : field, type);
9698 : 5 : inform (DECL_SOURCE_LOCATION (field),
9699 : 5 : TREE_PRIVATE (field)
9700 : : ? G_("declared private here")
9701 : : : G_("declared protected here"));
9702 : 5 : return error_mark_node;
9703 : 5 : }
9704 : : else
9705 : : member_seen = true;
9706 : :
9707 : 22353 : tree base_binfo, binfo;
9708 : 22353 : tree orig_ret = ret;
9709 : 22353 : int i;
9710 : 22353 : if (member_seen)
9711 : 22264 : ret = type;
9712 : 22411 : for (binfo = TYPE_BINFO (type), i = 0;
9713 : 22411 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9714 : : {
9715 : 78 : auto_diagnostic_group d;
9716 : 78 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret,
9717 : : complain);
9718 : 78 : if (t == error_mark_node)
9719 : : {
9720 : 3 : if (complain & tf_error)
9721 : 3 : inform (location_of (type), "in base class of %qT", type);
9722 : 3 : return error_mark_node;
9723 : : }
9724 : 75 : if (t != NULL_TREE && t != ret)
9725 : : {
9726 : 48 : if (ret == type)
9727 : : {
9728 : 10 : if (complain & tf_error)
9729 : 10 : error_at (loc, "cannot decompose class type %qT: both it and "
9730 : : "its base class %qT have non-static data "
9731 : : "members", type, t);
9732 : 10 : return error_mark_node;
9733 : : }
9734 : 38 : else if (orig_ret != NULL_TREE)
9735 : : return t;
9736 : 38 : else if (ret != NULL_TREE)
9737 : : {
9738 : 7 : if (complain & tf_error)
9739 : 7 : error_at (loc, "cannot decompose class type %qT: its base "
9740 : : "classes %qT and %qT have non-static data "
9741 : : "members", type, ret, t);
9742 : 7 : return error_mark_node;
9743 : : }
9744 : : else
9745 : : ret = t;
9746 : : }
9747 : 78 : }
9748 : : return ret;
9749 : : }
9750 : :
9751 : : /* Return std::tuple_size<TYPE>::value. */
9752 : :
9753 : : static tree
9754 : 62750 : get_tuple_size (tree type)
9755 : : {
9756 : 62750 : tree args = make_tree_vec (1);
9757 : 62750 : TREE_VEC_ELT (args, 0) = type;
9758 : 62750 : tree inst = lookup_template_class (tuple_size_identifier, args,
9759 : : /*in_decl*/NULL_TREE,
9760 : : /*context*/std_node,
9761 : : tf_none);
9762 : 62750 : inst = complete_type (inst);
9763 : 62750 : if (inst == error_mark_node
9764 : 61337 : || !COMPLETE_TYPE_P (inst)
9765 : 103132 : || !CLASS_TYPE_P (type))
9766 : : return NULL_TREE;
9767 : 40382 : tree val = lookup_qualified_name (inst, value_identifier,
9768 : : LOOK_want::NORMAL, /*complain*/false);
9769 : 40382 : if (val == error_mark_node)
9770 : : return NULL_TREE;
9771 : 40376 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
9772 : 40376 : val = maybe_constant_value (val, NULL_TREE, mce_true);
9773 : 40376 : if (TREE_CODE (val) == INTEGER_CST)
9774 : : return val;
9775 : : else
9776 : 6 : return error_mark_node;
9777 : : }
9778 : :
9779 : : /* Return std::tuple_element<I,TYPE>::type. */
9780 : :
9781 : : static tree
9782 : 43857 : get_tuple_element_type (tree type, unsigned HOST_WIDE_INT i)
9783 : : {
9784 : 43857 : tree args = make_tree_vec (2);
9785 : 43857 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
9786 : 43857 : TREE_VEC_ELT (args, 1) = type;
9787 : 43857 : tree inst = lookup_template_class (tuple_element_identifier, args,
9788 : : /*in_decl*/NULL_TREE,
9789 : : /*context*/std_node,
9790 : : tf_warning_or_error);
9791 : 43857 : return make_typename_type (inst, type_identifier,
9792 : 43857 : none_type, tf_warning_or_error);
9793 : : }
9794 : :
9795 : : /* Return e.get<i>() or get<i>(e). */
9796 : :
9797 : : static tree
9798 : 43866 : get_tuple_decomp_init (tree decl, unsigned HOST_WIDE_INT i)
9799 : : {
9800 : 43866 : tree targs = make_tree_vec (1);
9801 : 43866 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
9802 : :
9803 : 43866 : tree etype = TREE_TYPE (decl);
9804 : 43866 : tree e = convert_from_reference (decl);
9805 : :
9806 : : /* [The id-expression] e is an lvalue if the type of the entity e is an
9807 : : lvalue reference and an xvalue otherwise. */
9808 : 43866 : if (!TYPE_REF_P (etype)
9809 : 43866 : || TYPE_REF_IS_RVALUE (etype))
9810 : 42576 : e = move (e);
9811 : :
9812 : 43866 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
9813 : : LOOK_want::NORMAL, /*complain*/false);
9814 : 43866 : bool use_member_get = false;
9815 : :
9816 : : /* To use a member get, member lookup must find at least one
9817 : : declaration that is a function template
9818 : : whose first template parameter is a non-type parameter. */
9819 : 129161 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
9820 : : {
9821 : 43866 : tree fn = *iter;
9822 : 43866 : if (TREE_CODE (fn) == TEMPLATE_DECL)
9823 : : {
9824 : 2440 : tree tparms = DECL_TEMPLATE_PARMS (fn);
9825 : 2440 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
9826 : 2440 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
9827 : : {
9828 : : use_member_get = true;
9829 : : break;
9830 : : }
9831 : : }
9832 : : }
9833 : :
9834 : 43866 : if (use_member_get)
9835 : : {
9836 : 2437 : fns = lookup_template_function (fns, targs);
9837 : 2437 : return build_new_method_call (e, fns, /*args*/NULL,
9838 : : /*path*/NULL_TREE, LOOKUP_NORMAL,
9839 : 2437 : /*fn_p*/NULL, tf_warning_or_error);
9840 : : }
9841 : : else
9842 : : {
9843 : 41429 : releasing_vec args (make_tree_vector_single (e));
9844 : 41429 : fns = lookup_template_function (get__identifier, targs);
9845 : 41429 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
9846 : 41429 : return finish_call_expr (fns, &args, /*novirt*/false,
9847 : : /*koenig*/true, tf_warning_or_error);
9848 : 41429 : }
9849 : : }
9850 : :
9851 : : /* It's impossible to recover the decltype of a tuple decomposition variable
9852 : : based on the actual type of the variable, so store it in a hash table. */
9853 : :
9854 : : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
9855 : :
9856 : : tree
9857 : 546 : lookup_decomp_type (tree v)
9858 : : {
9859 : 546 : if (decomp_type_table)
9860 : 419 : if (tree *slot = decomp_type_table->get (v))
9861 : 355 : return *slot;
9862 : : return NULL_TREE;
9863 : : }
9864 : :
9865 : : /* Mangle a decomposition declaration if needed. Arguments like
9866 : : in cp_finish_decomp. */
9867 : :
9868 : : static void
9869 : 36771 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
9870 : : {
9871 : 36771 : if (!processing_template_decl
9872 : 36771 : && !error_operand_p (decl)
9873 : 73542 : && TREE_STATIC (decl))
9874 : : {
9875 : 602 : auto_vec<tree, 16> v;
9876 : 602 : v.safe_grow (decomp->count, true);
9877 : 602 : tree d = decomp->decl;
9878 : 2187 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
9879 : 1585 : v[decomp->count - i - 1] = d;
9880 : 602 : if (DECL_FUNCTION_SCOPE_P (decl))
9881 : : {
9882 : : size_t sz = 3;
9883 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
9884 : 650 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
9885 : 259 : char *name = XALLOCAVEC (char, sz);
9886 : 259 : name[0] = 'D';
9887 : 259 : name[1] = 'C';
9888 : 259 : char *p = name + 2;
9889 : 909 : for (unsigned int i = 0; i < decomp->count; ++i)
9890 : : {
9891 : 650 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
9892 : 650 : *p++ = ' ';
9893 : 650 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
9894 : 650 : p += len;
9895 : : }
9896 : 259 : *p = '\0';
9897 : 259 : determine_local_discriminator (decl, get_identifier (name));
9898 : : }
9899 : 602 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
9900 : 602 : maybe_apply_pragma_weak (decl);
9901 : 602 : }
9902 : 36771 : }
9903 : :
9904 : : /* Append #i to DECL_NAME (decl) or for name independent decls
9905 : : clear DECL_NAME (decl). */
9906 : :
9907 : : static void
9908 : 2021 : set_sb_pack_name (tree decl, unsigned HOST_WIDE_INT i)
9909 : : {
9910 : 2021 : if (name_independent_decl_p (decl))
9911 : : /* Only "_" names are treated as name independent, "_#0" etc. is not and
9912 : : because we pushdecl the individual decl elements of structured binding
9913 : : pack, we could get redeclaration errors if there are 2 or more name
9914 : : independent structured binding packs in the same scope. */
9915 : 312 : DECL_NAME (decl) = NULL_TREE;
9916 : : else
9917 : : {
9918 : 1709 : tree name = DECL_NAME (decl);
9919 : 1709 : size_t len = IDENTIFIER_LENGTH (name) + 22;
9920 : 1709 : char *n = XALLOCAVEC (char, len);
9921 : 1709 : snprintf (n, len, "%s#" HOST_WIDE_INT_PRINT_UNSIGNED,
9922 : 1709 : IDENTIFIER_POINTER (name), i);
9923 : 1709 : DECL_NAME (decl) = get_identifier (n);
9924 : : }
9925 : 2021 : }
9926 : :
9927 : : /* Return structured binding size of TYPE or -1 if erroneous. */
9928 : :
9929 : : HOST_WIDE_INT
9930 : 457 : cp_decomp_size (location_t loc, tree type, tsubst_flags_t complain)
9931 : : {
9932 : 457 : if (TYPE_REF_P (type))
9933 : : {
9934 : 0 : type = complete_type (TREE_TYPE (type));
9935 : 0 : if (type == error_mark_node)
9936 : : return -1;
9937 : 0 : if (!COMPLETE_TYPE_P (type))
9938 : : {
9939 : 0 : if (complain & tf_error)
9940 : 0 : error_at (loc, "structured binding refers to incomplete type %qT",
9941 : : type);
9942 : 0 : return -1;
9943 : : }
9944 : : }
9945 : :
9946 : 457 : unsigned HOST_WIDE_INT eltscnt = 0;
9947 : 457 : if (TREE_CODE (type) == ARRAY_TYPE)
9948 : : {
9949 : 255 : if (TYPE_DOMAIN (type) == NULL_TREE)
9950 : : {
9951 : 9 : if (complain & tf_error)
9952 : 6 : error_at (loc, "cannot decompose array of unknown bound %qT",
9953 : : type);
9954 : 9 : return -1;
9955 : : }
9956 : 246 : tree nelts = array_type_nelts_top (type);
9957 : 246 : if (nelts == error_mark_node)
9958 : : return -1;
9959 : 246 : if (!tree_fits_shwi_p (nelts))
9960 : : {
9961 : 3 : if (complain & tf_error)
9962 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
9963 : 3 : return -1;
9964 : : }
9965 : 243 : return tree_to_shwi (nelts);
9966 : : }
9967 : : /* 2 GNU extensions. */
9968 : 202 : else if (TREE_CODE (type) == COMPLEX_TYPE)
9969 : : return 2;
9970 : 196 : else if (TREE_CODE (type) == VECTOR_TYPE)
9971 : : {
9972 : 12 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9973 : : {
9974 : : if (complain & tf_error)
9975 : : error_at (loc, "cannot decompose variable length vector %qT", type);
9976 : : return -1;
9977 : : }
9978 : 12 : return eltscnt;
9979 : : }
9980 : 184 : else if (tree tsize = get_tuple_size (type))
9981 : : {
9982 : 28 : if (tsize == error_mark_node
9983 : 22 : || !tree_fits_shwi_p (tsize)
9984 : 50 : || tree_int_cst_sgn (tsize) < 0)
9985 : : {
9986 : 9 : if (complain & tf_error)
9987 : 9 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9988 : : "constant expression", type);
9989 : 9 : return -1;
9990 : : }
9991 : 19 : return tree_to_shwi (tsize);
9992 : : }
9993 : 156 : else if (TREE_CODE (type) == UNION_TYPE)
9994 : : {
9995 : 3 : if (complain & tf_error)
9996 : 3 : error_at (loc, "cannot decompose union type %qT", type);
9997 : 3 : return -1;
9998 : : }
9999 : 153 : else if (!CLASS_TYPE_P (type))
10000 : : {
10001 : 21 : if (complain & tf_error)
10002 : 18 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10003 : 21 : return -1;
10004 : : }
10005 : 132 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10006 : : return -1;
10007 : 132 : else if (!COMPLETE_TYPE_P (type))
10008 : : {
10009 : 3 : if (complain & tf_error)
10010 : 3 : error_at (loc, "structured binding refers to incomplete class type "
10011 : : "%qT", type);
10012 : 3 : return -1;
10013 : : }
10014 : : else
10015 : : {
10016 : 129 : tree btype = find_decomp_class_base (loc, type, NULL_TREE, complain);
10017 : 129 : if (btype == error_mark_node)
10018 : : return -1;
10019 : 108 : else if (btype == NULL_TREE)
10020 : : return 0;
10021 : 1011 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10022 : 1533 : if (TREE_CODE (field) != FIELD_DECL
10023 : 327 : || DECL_ARTIFICIAL (field)
10024 : 1251 : || DECL_UNNAMED_BIT_FIELD (field))
10025 : 609 : continue;
10026 : : else
10027 : 315 : eltscnt++;
10028 : 87 : return eltscnt;
10029 : : }
10030 : : }
10031 : :
10032 : : /* Finish a decomposition declaration. DECL is the underlying declaration
10033 : : "e", FIRST is the head of a chain of decls for the individual identifiers
10034 : : chained through DECL_CHAIN in reverse order and COUNT is the number of
10035 : : those decls. If TEST_P is true, return true if any code would need to be
10036 : : actually emitted but don't emit it. Return false otherwise. */
10037 : :
10038 : : bool
10039 : 134590 : cp_finish_decomp (tree decl, cp_decomp *decomp, bool test_p)
10040 : : {
10041 : 134590 : tree first = decomp->decl;
10042 : 134590 : unsigned count = decomp->count;
10043 : 134590 : if (error_operand_p (decl))
10044 : : {
10045 : 57 : error_out:
10046 : 583 : while (count--)
10047 : : {
10048 : 400 : TREE_TYPE (first) = error_mark_node;
10049 : 400 : if (DECL_HAS_VALUE_EXPR_P (first))
10050 : : {
10051 : 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
10052 : 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
10053 : : }
10054 : 400 : first = DECL_CHAIN (first);
10055 : : }
10056 : 183 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
10057 : 51 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
10058 : 183 : return false;
10059 : : }
10060 : :
10061 : 134533 : location_t loc = DECL_SOURCE_LOCATION (decl);
10062 : 134533 : if (type_dependent_expression_p (decl)
10063 : : /* This happens for range for when not in templates.
10064 : : Still add the DECL_VALUE_EXPRs for later processing. */
10065 : 134533 : || (!processing_template_decl
10066 : 55854 : && type_uses_auto (TREE_TYPE (decl))))
10067 : : {
10068 : 213215 : for (unsigned int i = 0; i < count; i++)
10069 : : {
10070 : 142180 : if (!DECL_HAS_VALUE_EXPR_P (first))
10071 : : {
10072 : 142180 : tree v = build_nt (ARRAY_REF, decl,
10073 : 142180 : size_int (count - i - 1),
10074 : : NULL_TREE, NULL_TREE);
10075 : 142180 : SET_DECL_VALUE_EXPR (first, v);
10076 : 142180 : DECL_HAS_VALUE_EXPR_P (first) = 1;
10077 : : }
10078 : 142180 : if (processing_template_decl)
10079 : 141566 : fit_decomposition_lang_decl (first, decl);
10080 : 142180 : first = DECL_CHAIN (first);
10081 : : }
10082 : : return false;
10083 : : }
10084 : :
10085 : 63498 : auto_vec<tree, 16> v;
10086 : 63498 : v.safe_grow (count, true);
10087 : 63498 : tree d = first;
10088 : 63498 : int pack = -1;
10089 : 192610 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
10090 : : {
10091 : 129112 : v[count - i - 1] = d;
10092 : 129112 : fit_decomposition_lang_decl (d, decl);
10093 : 129112 : if (DECL_PACK_P (d))
10094 : 1005 : pack = count - i - 1;
10095 : : }
10096 : :
10097 : 63498 : tree type = TREE_TYPE (decl);
10098 : 63498 : tree dexp = decl;
10099 : :
10100 : 63498 : if (TYPE_REF_P (type))
10101 : : {
10102 : 1758 : dexp = convert_from_reference (dexp);
10103 : 1758 : type = complete_type (TREE_TYPE (type));
10104 : 1758 : if (type == error_mark_node)
10105 : 126 : goto error_out;
10106 : 1758 : if (!COMPLETE_TYPE_P (type))
10107 : : {
10108 : 3 : error_at (loc, "structured binding refers to incomplete type %qT",
10109 : : type);
10110 : 3 : goto error_out;
10111 : : }
10112 : : }
10113 : :
10114 : 63495 : tree eltype = NULL_TREE;
10115 : 63495 : unsigned HOST_WIDE_INT eltscnt = 0;
10116 : : /* Structured binding packs when initializer is non-dependent should
10117 : : have their DECL_VALUE_EXPR set to a TREE_VEC. First two elements
10118 : : of that TREE_VEC are the base and index, what is normally represented
10119 : : as DECL_VALUE_EXPR ARRAY_REF <base, index> where index is the index
10120 : : of the pack first element. The remaining elements of the TREE_VEC
10121 : : are VAR_DECLs for the pack elements. */
10122 : 63495 : tree packv = NULL_TREE;
10123 : :
10124 : 63495 : if (TREE_CODE (type) == ARRAY_TYPE)
10125 : : {
10126 : 791 : tree nelts;
10127 : 791 : nelts = array_type_nelts_top (type);
10128 : 791 : if (nelts == error_mark_node)
10129 : 0 : goto error_out;
10130 : 791 : if (DECL_DECOMP_BASE (decl))
10131 : : {
10132 : 9 : error_at (loc, "array initializer for structured binding "
10133 : : "declaration in condition");
10134 : 9 : goto error_out;
10135 : : }
10136 : 782 : if (!tree_fits_uhwi_p (nelts))
10137 : : {
10138 : 3 : error_at (loc, "cannot decompose variable length array %qT", type);
10139 : 3 : goto error_out;
10140 : : }
10141 : 779 : eltscnt = tree_to_uhwi (nelts);
10142 : 779 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10143 : : {
10144 : 11 : cnt_mismatch:
10145 : 38 : auto_diagnostic_group d;
10146 : 38 : if (count > eltscnt)
10147 : 27 : error_n (loc, count,
10148 : : "%u name provided for structured binding",
10149 : : "%u names provided for structured binding", count);
10150 : : else
10151 : 11 : error_n (loc, count,
10152 : : "only %u name provided for structured binding",
10153 : : "only %u names provided for structured binding", count);
10154 : 38 : inform_n (loc, eltscnt,
10155 : : "while %qT decomposes into %wu element",
10156 : : "while %qT decomposes into %wu elements",
10157 : : type, eltscnt);
10158 : 38 : goto error_out;
10159 : : }
10160 : 768 : eltype = TREE_TYPE (type);
10161 : 2766 : for (unsigned int i = 0; i < count; i++)
10162 : : {
10163 : 1998 : if ((unsigned) pack == i)
10164 : : {
10165 : 152 : packv = make_tree_vec (eltscnt - count + 3);
10166 : 400 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10167 : : {
10168 : 248 : tree t;
10169 : 248 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10170 : 248 : set_sb_pack_name (t, j);
10171 : 248 : maybe_push_decl (t);
10172 : 248 : TREE_TYPE (t) = eltype;
10173 : 248 : layout_decl (t, 0);
10174 : 248 : if (!processing_template_decl)
10175 : : {
10176 : 172 : tree a = unshare_expr (dexp);
10177 : 172 : a = build4 (ARRAY_REF, eltype, a, size_int (j + pack),
10178 : : NULL_TREE, NULL_TREE);
10179 : 172 : SET_DECL_VALUE_EXPR (t, a);
10180 : 172 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10181 : : }
10182 : : }
10183 : 1998 : continue;
10184 : 152 : }
10185 : 1846 : TREE_TYPE (v[i]) = eltype;
10186 : 1846 : layout_decl (v[i], 0);
10187 : 1846 : if (processing_template_decl)
10188 : 132 : continue;
10189 : 1714 : tree t = unshare_expr (dexp);
10190 : 1714 : unsigned HOST_WIDE_INT j = i;
10191 : 1714 : if (pack != -1 && (unsigned) pack < i)
10192 : 64 : j = i + eltscnt - count;
10193 : 1714 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10194 : 1714 : SET_DECL_VALUE_EXPR (v[i], t);
10195 : 1714 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10196 : : }
10197 : : }
10198 : : /* 2 GNU extensions. */
10199 : 62704 : else if (TREE_CODE (type) == COMPLEX_TYPE)
10200 : : {
10201 : 78 : eltscnt = 2;
10202 : 78 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10203 : 3 : goto cnt_mismatch;
10204 : 75 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10205 : 213 : for (unsigned int i = 0; i < count; i++)
10206 : : {
10207 : 138 : if ((unsigned) pack == i)
10208 : : {
10209 : 60 : packv = make_tree_vec (eltscnt - count + 3);
10210 : 132 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10211 : : {
10212 : 72 : tree t;
10213 : 72 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10214 : 72 : set_sb_pack_name (t, j);
10215 : 72 : maybe_push_decl (t);
10216 : 72 : TREE_TYPE (t) = eltype;
10217 : 72 : layout_decl (t, 0);
10218 : 72 : if (!processing_template_decl)
10219 : : {
10220 : 72 : tree a = build1 (pack + j ? IMAGPART_EXPR : REALPART_EXPR, eltype,
10221 : : unshare_expr (dexp));
10222 : 48 : SET_DECL_VALUE_EXPR (t, a);
10223 : 48 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10224 : : }
10225 : : }
10226 : 60 : continue;
10227 : 60 : }
10228 : 78 : TREE_TYPE (v[i]) = eltype;
10229 : 78 : layout_decl (v[i], 0);
10230 : 78 : if (processing_template_decl)
10231 : 16 : continue;
10232 : 62 : tree t = unshare_expr (dexp);
10233 : 62 : unsigned HOST_WIDE_INT j = i;
10234 : 62 : if (pack != -1 && (unsigned) pack < i)
10235 : 16 : j = i + eltscnt - count;
10236 : 93 : t = build1 (j ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
10237 : 62 : SET_DECL_VALUE_EXPR (v[i], t);
10238 : 62 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10239 : : }
10240 : : }
10241 : 62626 : else if (TREE_CODE (type) == VECTOR_TYPE)
10242 : : {
10243 : 60 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
10244 : : {
10245 : : error_at (loc, "cannot decompose variable length vector %qT", type);
10246 : : goto error_out;
10247 : : }
10248 : 60 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10249 : 3 : goto cnt_mismatch;
10250 : 57 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
10251 : 249 : for (unsigned int i = 0; i < count; i++)
10252 : : {
10253 : 192 : if ((unsigned) pack == i)
10254 : : {
10255 : 48 : packv = make_tree_vec (eltscnt - count + 3);
10256 : 708 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10257 : : {
10258 : 660 : tree t;
10259 : 660 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10260 : 660 : set_sb_pack_name (t, j);
10261 : 660 : maybe_push_decl (t);
10262 : 660 : TREE_TYPE (t) = eltype;
10263 : 660 : layout_decl (t, 0);
10264 : 660 : if (!processing_template_decl)
10265 : : {
10266 : 440 : tree a = unshare_expr (dexp);
10267 : 440 : location_t loc = DECL_SOURCE_LOCATION (t);
10268 : 440 : tree s = size_int (j + pack);
10269 : 440 : convert_vector_to_array_for_subscript (loc, &a, s);
10270 : 440 : a = build4 (ARRAY_REF, eltype, a, s,
10271 : : NULL_TREE, NULL_TREE);
10272 : 440 : SET_DECL_VALUE_EXPR (t, a);
10273 : 440 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10274 : : }
10275 : : }
10276 : 84 : continue;
10277 : 48 : }
10278 : 144 : TREE_TYPE (v[i]) = eltype;
10279 : 144 : layout_decl (v[i], 0);
10280 : 144 : if (processing_template_decl)
10281 : 36 : continue;
10282 : 108 : tree t = unshare_expr (dexp);
10283 : 108 : unsigned HOST_WIDE_INT j = i;
10284 : 108 : if (pack != -1 && (unsigned) pack < i)
10285 : 24 : j = i + eltscnt - count;
10286 : 108 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
10287 : 108 : &t, size_int (j));
10288 : 108 : t = build4 (ARRAY_REF, eltype, t, size_int (j), NULL_TREE, NULL_TREE);
10289 : 108 : SET_DECL_VALUE_EXPR (v[i], t);
10290 : 108 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10291 : : }
10292 : : }
10293 : 62566 : else if (tree tsize = get_tuple_size (type))
10294 : : {
10295 : 40348 : if (tsize == error_mark_node)
10296 : : {
10297 : 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
10298 : : "constant expression", type);
10299 : 0 : goto error_out;
10300 : : }
10301 : 40348 : if (!tree_fits_uhwi_p (tsize))
10302 : : {
10303 : 12 : auto_diagnostic_group d;
10304 : 12 : error_n (loc, count,
10305 : : "%u name provided for structured binding",
10306 : : "%u names provided for structured binding", count);
10307 : 12 : inform (loc, "while %qT decomposes into %E elements",
10308 : : type, tsize);
10309 : 12 : goto error_out;
10310 : 12 : }
10311 : 40336 : eltscnt = tree_to_uhwi (tsize);
10312 : 40336 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10313 : 6 : goto cnt_mismatch;
10314 : 40330 : if (test_p && eltscnt)
10315 : 63372 : return true;
10316 : 21519 : if (!processing_template_decl && DECL_DECOMP_BASE (decl) && eltscnt)
10317 : : {
10318 : : /* For structured bindings used in conditions we need to evaluate
10319 : : the conversion of decl (aka e in the standard) to bool or
10320 : : integral/enumeral type (the latter for switch conditions)
10321 : : before the get methods. */
10322 : 86 : tree cond = convert_from_reference (decl);
10323 : 86 : if (integer_onep (DECL_DECOMP_BASE (decl)))
10324 : : /* switch condition. */
10325 : 18 : cond = build_expr_type_conversion (WANT_INT | WANT_ENUM,
10326 : : cond, true);
10327 : : else
10328 : : /* if/while/for condition. */
10329 : 68 : cond = contextual_conv_bool (cond, tf_warning_or_error);
10330 : 86 : if (cond && !error_operand_p (cond))
10331 : : {
10332 : : /* Wrap that value into a TARGET_EXPR, emit it right
10333 : : away and save for later uses in the cp_parse_condition
10334 : : or its instantiation. */
10335 : 86 : cond = get_internal_target_expr (cond);
10336 : 86 : add_stmt (cond);
10337 : 86 : DECL_DECOMP_BASE (decl) = cond;
10338 : : }
10339 : : }
10340 : 21519 : int save_read = DECL_READ_P (decl);
10341 : 65084 : for (unsigned i = 0; i < count; ++i)
10342 : : {
10343 : 43586 : location_t sloc = input_location;
10344 : 43586 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
10345 : :
10346 : 43586 : if ((unsigned) pack == i)
10347 : : {
10348 : 276 : packv = make_tree_vec (eltscnt - count + 3);
10349 : 832 : for (unsigned HOST_WIDE_INT j = 0; j < eltscnt - count + 1; ++j)
10350 : : {
10351 : 556 : tree t;
10352 : 556 : TREE_VEC_ELT (packv, j + 2) = t = copy_node (v[pack]);
10353 : 556 : set_sb_pack_name (t, j);
10354 : 556 : input_location = dloc;
10355 : 556 : tree init = get_tuple_decomp_init (decl, j + pack);
10356 : 556 : tree eltype = (init == error_mark_node ? error_mark_node
10357 : 556 : : get_tuple_element_type (type, j + pack));
10358 : 556 : input_location = sloc;
10359 : :
10360 : 556 : if (VOID_TYPE_P (eltype))
10361 : : {
10362 : 0 : error ("%<std::tuple_element<%wu, %T>::type%> is "
10363 : : "%<void%>", j + pack, type);
10364 : 0 : eltype = error_mark_node;
10365 : : }
10366 : 556 : if (init == error_mark_node || eltype == error_mark_node)
10367 : : {
10368 : 0 : inform (dloc, "in initialization of structured binding "
10369 : 0 : "pack %qD", v[pack]);
10370 : 0 : goto error_out;
10371 : : }
10372 : 556 : maybe_push_decl (t);
10373 : : /* Save the decltype away before reference collapse. */
10374 : 556 : hash_map_safe_put<hm_ggc> (decomp_type_table, t, eltype);
10375 : 556 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10376 : 556 : TREE_TYPE (t) = eltype;
10377 : 556 : layout_decl (t, 0);
10378 : 556 : DECL_HAS_VALUE_EXPR_P (t) = 0;
10379 : 556 : if (!processing_template_decl)
10380 : : {
10381 : 456 : copy_linkage (t, decl);
10382 : 456 : tree name = DECL_NAME (t);
10383 : 456 : if (TREE_STATIC (decl))
10384 : 36 : DECL_NAME (t) = DECL_NAME (v[pack]);
10385 : 456 : cp_finish_decl (t, init, /*constexpr*/false,
10386 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10387 : 456 : if (TREE_STATIC (decl))
10388 : : {
10389 : 36 : DECL_ASSEMBLER_NAME (t);
10390 : 36 : DECL_NAME (t) = name;
10391 : : }
10392 : : }
10393 : : }
10394 : 276 : continue;
10395 : 276 : }
10396 : :
10397 : 43310 : unsigned HOST_WIDE_INT j = i;
10398 : 43310 : if (pack != -1 && (unsigned) pack < i)
10399 : 238 : j = i + eltscnt - count;
10400 : 43310 : input_location = dloc;
10401 : 43310 : tree init = get_tuple_decomp_init (decl, j);
10402 : 43310 : tree eltype = (init == error_mark_node ? error_mark_node
10403 : 43301 : : get_tuple_element_type (type, j));
10404 : 43310 : input_location = sloc;
10405 : :
10406 : 43310 : if (VOID_TYPE_P (eltype))
10407 : : {
10408 : 3 : error ("%<std::tuple_element<%wu, %T>::type%> is %<void%>",
10409 : : j, type);
10410 : 3 : eltype = error_mark_node;
10411 : : }
10412 : 43310 : if (init == error_mark_node || eltype == error_mark_node)
10413 : : {
10414 : 21 : inform (dloc, "in initialization of structured binding "
10415 : 21 : "variable %qD", v[i]);
10416 : 21 : goto error_out;
10417 : : }
10418 : : /* Save the decltype away before reference collapse. */
10419 : 43289 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
10420 : 43289 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
10421 : 43289 : TREE_TYPE (v[i]) = eltype;
10422 : 43289 : layout_decl (v[i], 0);
10423 : 43289 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
10424 : : {
10425 : : /* In this case the names are variables, not just proxies. */
10426 : 220 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
10427 : 220 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
10428 : : }
10429 : 43289 : if (!processing_template_decl)
10430 : : {
10431 : 37849 : copy_linkage (v[i], decl);
10432 : 37849 : cp_finish_decl (v[i], init, /*constexpr*/false,
10433 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
10434 : : }
10435 : : }
10436 : : /* Ignore reads from the underlying decl performed during initialization
10437 : : of the individual variables. If those will be read, we'll mark
10438 : : the underlying decl as read at that point. */
10439 : 21498 : DECL_READ_P (decl) = save_read;
10440 : : }
10441 : 22218 : else if (TREE_CODE (type) == UNION_TYPE)
10442 : : {
10443 : 3 : error_at (loc, "cannot decompose union type %qT", type);
10444 : 3 : goto error_out;
10445 : : }
10446 : 22215 : else if (!CLASS_TYPE_P (type))
10447 : : {
10448 : 16 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
10449 : 16 : goto error_out;
10450 : : }
10451 : 22199 : else if (processing_template_decl && complete_type (type) == error_mark_node)
10452 : 0 : goto error_out;
10453 : 22199 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
10454 : 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
10455 : : type);
10456 : : else
10457 : : {
10458 : 22197 : tree btype = find_decomp_class_base (loc, type, NULL_TREE,
10459 : : tf_warning_or_error);
10460 : 22197 : if (btype == error_mark_node)
10461 : 21 : goto error_out;
10462 : 22176 : else if (btype == NULL_TREE)
10463 : : {
10464 : 16 : if (pack == 0 && count == 1)
10465 : : {
10466 : 16 : eltscnt = 0;
10467 : 16 : packv = make_tree_vec (2);
10468 : 16 : goto done;
10469 : : }
10470 : 0 : error_at (loc, "cannot decompose class type %qT without non-static "
10471 : : "data members", type);
10472 : 0 : goto error_out;
10473 : : }
10474 : 313886 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10475 : 538368 : if (TREE_CODE (field) != FIELD_DECL
10476 : 45100 : || DECL_ARTIFICIAL (field)
10477 : 336822 : || DECL_UNNAMED_BIT_FIELD (field))
10478 : 246642 : continue;
10479 : : else
10480 : 45084 : eltscnt++;
10481 : 22160 : if (pack != -1 ? count - 1 > eltscnt : count != eltscnt)
10482 : 15 : goto cnt_mismatch;
10483 : 22145 : tree t = dexp;
10484 : 22145 : if (type != btype)
10485 : : {
10486 : 18 : t = convert_to_base (t, btype, /*check_access*/true,
10487 : : /*nonnull*/false, tf_warning_or_error);
10488 : 18 : type = btype;
10489 : : }
10490 : 22145 : unsigned HOST_WIDE_INT j = 0;
10491 : 22145 : unsigned int i = 0;
10492 : 313675 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
10493 : 538013 : if (TREE_CODE (field) != FIELD_DECL
10494 : 45063 : || DECL_ARTIFICIAL (field)
10495 : 336589 : || DECL_UNNAMED_BIT_FIELD (field))
10496 : 246483 : continue;
10497 : : else
10498 : : {
10499 : 45047 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
10500 : : NULL_TREE);
10501 : 45047 : if (REFERENCE_REF_P (tt))
10502 : 30 : tt = TREE_OPERAND (tt, 0);
10503 : 45047 : if (pack != -1 && j >= (unsigned) pack)
10504 : : {
10505 : 666 : if (j == (unsigned) pack)
10506 : : {
10507 : 220 : packv = make_tree_vec (eltscnt - count + 3);
10508 : 220 : i++;
10509 : : }
10510 : 666 : if (j < (unsigned) pack + eltscnt - (count - 1))
10511 : : {
10512 : 485 : tree t;
10513 : 485 : TREE_VEC_ELT (packv, j + 3 - i) = t = copy_node (v[pack]);
10514 : 485 : set_sb_pack_name (t, j + 1 - i);
10515 : 485 : maybe_push_decl (t);
10516 : 485 : TREE_TYPE (t) = TREE_TYPE (tt);
10517 : 485 : layout_decl (t, 0);
10518 : 485 : if (!processing_template_decl)
10519 : : {
10520 : 364 : SET_DECL_VALUE_EXPR (t, tt);
10521 : 364 : DECL_HAS_VALUE_EXPR_P (t) = 1;
10522 : : }
10523 : : else
10524 : 121 : DECL_HAS_VALUE_EXPR_P (t) = 0;
10525 : 485 : j++;
10526 : 485 : continue;
10527 : 485 : }
10528 : : }
10529 : 44562 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
10530 : 44562 : layout_decl (v[i], 0);
10531 : 44562 : if (!processing_template_decl)
10532 : : {
10533 : 34465 : SET_DECL_VALUE_EXPR (v[i], tt);
10534 : 34465 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10535 : : }
10536 : 44562 : i++;
10537 : 44562 : j++;
10538 : : }
10539 : 22145 : if (pack != -1 && j == (unsigned) pack)
10540 : : {
10541 : 20 : gcc_checking_assert (eltscnt == count - 1);
10542 : 20 : packv = make_tree_vec (2);
10543 : : }
10544 : : }
10545 : 44561 : done:
10546 : 44561 : if (packv)
10547 : : {
10548 : 792 : gcc_checking_assert (pack != -1);
10549 : 792 : TREE_VEC_ELT (packv, 0) = decl;
10550 : 792 : TREE_VEC_ELT (packv, 1) = size_int (pack);
10551 : 792 : SET_DECL_VALUE_EXPR (v[pack], packv);
10552 : 792 : DECL_HAS_VALUE_EXPR_P (v[pack]) = 1;
10553 : 792 : DECL_IGNORED_P (v[pack]) = 1;
10554 : 792 : if (!processing_template_decl)
10555 : 2034 : for (unsigned int i = 0; i < TREE_VEC_LENGTH (packv) - 2U; ++i)
10556 : 1480 : pushdecl (TREE_VEC_ELT (packv, 2 + i));
10557 : : }
10558 : 44561 : if (processing_template_decl)
10559 : : {
10560 : 23840 : for (unsigned int i = 0; i < count; i++)
10561 : 15961 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
10562 : : {
10563 : 15567 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
10564 : : NULL_TREE, NULL_TREE);
10565 : 15567 : SET_DECL_VALUE_EXPR (v[i], a);
10566 : 15567 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
10567 : : }
10568 : : }
10569 : : return false;
10570 : : }
10571 : :
10572 : : /* Returns a declaration for a VAR_DECL as if:
10573 : :
10574 : : extern "C" TYPE NAME;
10575 : :
10576 : : had been seen. Used to create compiler-generated global
10577 : : variables. */
10578 : :
10579 : : static tree
10580 : 2160 : declare_global_var (tree name, tree type)
10581 : : {
10582 : 2160 : auto cookie = push_abi_namespace (global_namespace);
10583 : 2160 : tree decl = build_decl (input_location, VAR_DECL, name, type);
10584 : 2160 : TREE_PUBLIC (decl) = 1;
10585 : 2160 : DECL_EXTERNAL (decl) = 1;
10586 : 2160 : DECL_ARTIFICIAL (decl) = 1;
10587 : 2160 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10588 : : /* If the user has explicitly declared this variable (perhaps
10589 : : because the code we are compiling is part of a low-level runtime
10590 : : library), then it is possible that our declaration will be merged
10591 : : with theirs by pushdecl. */
10592 : 2160 : decl = pushdecl (decl);
10593 : 2160 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
10594 : 2160 : pop_abi_namespace (cookie, global_namespace);
10595 : :
10596 : 2160 : return decl;
10597 : : }
10598 : :
10599 : : /* Returns the type for the argument to "atexit" corresponding to the function
10600 : : to be called when the program exits. */
10601 : :
10602 : : static tree
10603 : 24 : get_atexit_fn_ptr_type ()
10604 : : {
10605 : 24 : if (!atexit_fn_ptr_type_node)
10606 : : {
10607 : 12 : tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
10608 : 12 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
10609 : : }
10610 : :
10611 : 24 : return atexit_fn_ptr_type_node;
10612 : : }
10613 : :
10614 : : /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit"
10615 : : or "__cxa_throw" corresponding to the destructor to be called when the
10616 : : program exits. */
10617 : :
10618 : : tree
10619 : 18431 : get_cxa_atexit_fn_ptr_type ()
10620 : : {
10621 : 18431 : if (!cleanup_type)
10622 : : {
10623 : 13813 : tree fntype = build_function_type_list (void_type_node,
10624 : : ptr_type_node, NULL_TREE);
10625 : 13813 : fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype);
10626 : 13813 : cleanup_type = build_pointer_type (fntype);
10627 : : }
10628 : :
10629 : 18431 : return cleanup_type;
10630 : : }
10631 : :
10632 : : /* Returns a pointer to the `atexit' function. Note that if
10633 : : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
10634 : : `__cxa_atexit' function specified in the IA64 C++ ABI. */
10635 : :
10636 : : static tree
10637 : 3421 : get_atexit_node (void)
10638 : : {
10639 : 3421 : tree atexit_fndecl;
10640 : 3421 : tree fn_type;
10641 : 3421 : tree fn_ptr_type;
10642 : 3421 : const char *name;
10643 : 3421 : bool use_aeabi_atexit;
10644 : 3421 : tree ctx = global_namespace;
10645 : :
10646 : 3421 : if (atexit_node)
10647 : : return atexit_node;
10648 : :
10649 : 2126 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
10650 : : {
10651 : : /* The declaration for `__cxa_atexit' is:
10652 : :
10653 : : int __cxa_atexit (void (*)(void *), void *, void *)
10654 : :
10655 : : We build up the argument types and then the function type
10656 : : itself. */
10657 : 2114 : tree argtype0, argtype1, argtype2;
10658 : :
10659 : 2114 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
10660 : : /* First, build the pointer-to-function type for the first
10661 : : argument. */
10662 : 2114 : fn_ptr_type = get_cxa_atexit_fn_ptr_type ();
10663 : : /* Then, build the rest of the argument types. */
10664 : 2114 : argtype2 = ptr_type_node;
10665 : 2114 : if (use_aeabi_atexit)
10666 : : {
10667 : : argtype1 = fn_ptr_type;
10668 : : argtype0 = ptr_type_node;
10669 : : }
10670 : : else
10671 : : {
10672 : 2114 : argtype1 = ptr_type_node;
10673 : 2114 : argtype0 = fn_ptr_type;
10674 : : }
10675 : : /* And the final __cxa_atexit type. */
10676 : 2114 : fn_type = build_function_type_list (integer_type_node,
10677 : : argtype0, argtype1, argtype2,
10678 : : NULL_TREE);
10679 : : /* ... which needs noexcept. */
10680 : 2114 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10681 : 2114 : if (use_aeabi_atexit)
10682 : : {
10683 : 0 : name = "__aeabi_atexit";
10684 : 0 : push_to_top_level ();
10685 : 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
10686 : 0 : ctx = current_namespace;
10687 : 0 : while (n--)
10688 : 0 : pop_namespace ();
10689 : 0 : pop_from_top_level ();
10690 : : }
10691 : : else
10692 : : {
10693 : 2114 : name = "__cxa_atexit";
10694 : 2114 : ctx = abi_node;
10695 : : }
10696 : : }
10697 : : else
10698 : : {
10699 : : /* The declaration for `atexit' is:
10700 : :
10701 : : int atexit (void (*)());
10702 : :
10703 : : We build up the argument types and then the function type
10704 : : itself. */
10705 : 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
10706 : : /* Build the final atexit type. */
10707 : 12 : fn_type = build_function_type_list (integer_type_node,
10708 : : fn_ptr_type, NULL_TREE);
10709 : : /* ... which needs noexcept. */
10710 : 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
10711 : 12 : name = "atexit";
10712 : : }
10713 : :
10714 : : /* Now, build the function declaration. */
10715 : 2126 : push_lang_context (lang_name_c);
10716 : 2126 : auto cookie = push_abi_namespace (ctx);
10717 : 2126 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
10718 : 2126 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10719 : : /* Install as hidden builtin so we're (a) more relaxed about
10720 : : exception spec matching and (b) will not give a confusing location
10721 : : in diagnostic and (c) won't magically appear in user-visible name
10722 : : lookups. */
10723 : 2126 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10724 : 2126 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10725 : 2126 : pop_abi_namespace (cookie, ctx);
10726 : 2126 : mark_used (atexit_fndecl);
10727 : 2126 : pop_lang_context ();
10728 : 2126 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10729 : :
10730 : 2126 : return atexit_node;
10731 : : }
10732 : :
10733 : : /* Like get_atexit_node, but for thread-local cleanups. */
10734 : :
10735 : : static tree
10736 : 86 : get_thread_atexit_node (void)
10737 : : {
10738 : 86 : if (thread_atexit_node)
10739 : : return thread_atexit_node;
10740 : :
10741 : : /* The declaration for `__cxa_thread_atexit' is:
10742 : :
10743 : : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
10744 : 56 : tree fn_type = build_function_type_list (integer_type_node,
10745 : : get_cxa_atexit_fn_ptr_type (),
10746 : : ptr_type_node, ptr_type_node,
10747 : : NULL_TREE);
10748 : :
10749 : : /* Now, build the function declaration, as with __cxa_atexit. */
10750 : 56 : unsigned flags = push_abi_namespace ();
10751 : 56 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
10752 : : ECF_LEAF | ECF_NOTHROW);
10753 : 56 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
10754 : 56 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
10755 : 56 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
10756 : 56 : pop_abi_namespace (flags);
10757 : 56 : mark_used (atexit_fndecl);
10758 : 56 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
10759 : :
10760 : 56 : return thread_atexit_node;
10761 : : }
10762 : :
10763 : : /* Returns the __dso_handle VAR_DECL. */
10764 : :
10765 : : static tree
10766 : 3495 : get_dso_handle_node (void)
10767 : : {
10768 : 3495 : if (dso_handle_node)
10769 : : return dso_handle_node;
10770 : :
10771 : : /* Declare the variable. */
10772 : 2160 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
10773 : : ptr_type_node);
10774 : :
10775 : : #ifdef HAVE_GAS_HIDDEN
10776 : 2160 : if (dso_handle_node != error_mark_node)
10777 : : {
10778 : 2157 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
10779 : 2157 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
10780 : : }
10781 : : #endif
10782 : :
10783 : 2160 : return dso_handle_node;
10784 : : }
10785 : :
10786 : : /* Begin a new function with internal linkage whose job will be simply
10787 : : to destroy some particular DECL. OB_PARM is true if object pointer
10788 : : is passed to the cleanup function, otherwise no argument is passed. */
10789 : :
10790 : : static tree
10791 : 367 : start_cleanup_fn (tree decl, bool ob_parm, bool omp_target)
10792 : : {
10793 : 367 : push_to_top_level ();
10794 : :
10795 : : /* No need to mangle this. */
10796 : 367 : push_lang_context (lang_name_c);
10797 : :
10798 : : /* Build the name of the function. */
10799 : 367 : gcc_checking_assert (HAS_DECL_ASSEMBLER_NAME_P (decl));
10800 : 367 : const char *dname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10801 : 367 : dname = targetm.strip_name_encoding (dname);
10802 : 731 : char *name = ACONCAT ((omp_target ? "__omp_tcf" : "__tcf", dname, NULL));
10803 : :
10804 : 367 : tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type ()
10805 : : : get_atexit_fn_ptr_type ());
10806 : : /* Build the function declaration. */
10807 : 367 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
10808 : 367 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
10809 : : /* It's a function with internal linkage, generated by the
10810 : : compiler. */
10811 : 367 : TREE_PUBLIC (fndecl) = 0;
10812 : 367 : DECL_ARTIFICIAL (fndecl) = 1;
10813 : : /* Make the function `inline' so that it is only emitted if it is
10814 : : actually needed. It is unlikely that it will be inlined, since
10815 : : it is only called via a function pointer, but we avoid unnecessary
10816 : : emissions this way. */
10817 : 367 : DECL_DECLARED_INLINE_P (fndecl) = 1;
10818 : 367 : DECL_INTERFACE_KNOWN (fndecl) = 1;
10819 : 367 : if (ob_parm)
10820 : : {
10821 : : /* Build the parameter. */
10822 : 355 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
10823 : 355 : TREE_USED (parmdecl) = 1;
10824 : 355 : DECL_READ_P (parmdecl) = 1;
10825 : 355 : DECL_ARGUMENTS (fndecl) = parmdecl;
10826 : : }
10827 : :
10828 : 367 : fndecl = pushdecl (fndecl, /*hidden=*/true);
10829 : 367 : if (omp_target)
10830 : : {
10831 : 3 : DECL_ATTRIBUTES (fndecl)
10832 : 3 : = tree_cons (get_identifier ("omp declare target"), NULL_TREE,
10833 : 3 : DECL_ATTRIBUTES (fndecl));
10834 : 3 : DECL_ATTRIBUTES (fndecl)
10835 : 6 : = tree_cons (get_identifier ("omp declare target nohost"), NULL_TREE,
10836 : 3 : DECL_ATTRIBUTES (fndecl));
10837 : : }
10838 : 367 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
10839 : :
10840 : 367 : pop_lang_context ();
10841 : :
10842 : 367 : return current_function_decl;
10843 : : }
10844 : :
10845 : : /* Finish the cleanup function begun by start_cleanup_fn. */
10846 : :
10847 : : static void
10848 : 367 : end_cleanup_fn (void)
10849 : : {
10850 : 367 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
10851 : :
10852 : 367 : pop_from_top_level ();
10853 : 367 : }
10854 : :
10855 : : /* Generate code to handle the destruction of DECL, an object with
10856 : : static storage duration. */
10857 : :
10858 : : tree
10859 : 14872 : register_dtor_fn (tree decl, bool omp_target)
10860 : : {
10861 : 14872 : tree cleanup;
10862 : 14872 : tree addr;
10863 : 14872 : tree compound_stmt;
10864 : 14872 : tree fcall;
10865 : 14872 : tree type;
10866 : 14872 : bool ob_parm, dso_parm, use_dtor;
10867 : 14872 : tree arg0, arg1, arg2;
10868 : 14872 : tree atex_node;
10869 : :
10870 : 14872 : type = TREE_TYPE (decl);
10871 : 14872 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
10872 : 11365 : return void_node;
10873 : :
10874 : 3507 : if (decl_maybe_constant_destruction (decl, type)
10875 : 3507 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
10876 : : {
10877 : 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10878 : 0 : return void_node;
10879 : : }
10880 : :
10881 : : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
10882 : : "__aeabi_atexit"), and DECL is a class object, we can just pass the
10883 : : destructor to "__cxa_atexit"; we don't have to build a temporary
10884 : : function to do the cleanup. */
10885 : 7014 : dso_parm = (flag_use_cxa_atexit
10886 : 3507 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
10887 : 3507 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
10888 : 3495 : use_dtor = ob_parm && CLASS_TYPE_P (type);
10889 : 3507 : if (use_dtor)
10890 : : {
10891 : 3140 : cleanup = get_class_binding (type, complete_dtor_identifier);
10892 : :
10893 : : /* Make sure it is accessible. */
10894 : 3140 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
10895 : : tf_warning_or_error);
10896 : : }
10897 : : else
10898 : : {
10899 : : /* Call build_cleanup before we enter the anonymous function so
10900 : : that any access checks will be done relative to the current
10901 : : scope, rather than the scope of the anonymous function. */
10902 : 367 : build_cleanup (decl);
10903 : :
10904 : : /* Now start the function. */
10905 : 367 : cleanup = start_cleanup_fn (decl, ob_parm, omp_target);
10906 : :
10907 : : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
10908 : : to the original function, rather than the anonymous one. That
10909 : : will make the back end think that nested functions are in use,
10910 : : which causes confusion. */
10911 : 367 : push_deferring_access_checks (dk_no_check);
10912 : 367 : fcall = build_cleanup (decl);
10913 : 367 : pop_deferring_access_checks ();
10914 : :
10915 : : /* Create the body of the anonymous function. */
10916 : 367 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
10917 : 367 : finish_expr_stmt (fcall);
10918 : 367 : finish_compound_stmt (compound_stmt);
10919 : 367 : end_cleanup_fn ();
10920 : : }
10921 : :
10922 : : /* Call atexit with the cleanup function. */
10923 : 3507 : mark_used (cleanup);
10924 : 3507 : cleanup = build_address (cleanup);
10925 : :
10926 : 3507 : if (CP_DECL_THREAD_LOCAL_P (decl))
10927 : 86 : atex_node = get_thread_atexit_node ();
10928 : : else
10929 : 3421 : atex_node = get_atexit_node ();
10930 : :
10931 : 3507 : if (use_dtor)
10932 : : {
10933 : : /* We must convert CLEANUP to the type that "__cxa_atexit"
10934 : : expects. */
10935 : 3140 : cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup);
10936 : : /* "__cxa_atexit" will pass the address of DECL to the
10937 : : cleanup function. */
10938 : 3140 : mark_used (decl);
10939 : 3140 : addr = build_address (decl);
10940 : : /* The declared type of the parameter to "__cxa_atexit" is
10941 : : "void *". For plain "T*", we could just let the
10942 : : machinery in cp_build_function_call convert it -- but if the
10943 : : type is "cv-qualified T *", then we need to convert it
10944 : : before passing it in, to avoid spurious errors. */
10945 : 3140 : addr = build_nop (ptr_type_node, addr);
10946 : : }
10947 : : else
10948 : : /* Since the cleanup functions we build ignore the address
10949 : : they're given, there's no reason to pass the actual address
10950 : : in, and, in general, it's cheaper to pass NULL than any
10951 : : other value. */
10952 : 367 : addr = null_pointer_node;
10953 : :
10954 : 3507 : if (dso_parm)
10955 : 3495 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
10956 : : tf_warning_or_error);
10957 : 12 : else if (ob_parm)
10958 : : /* Just pass NULL to the dso handle parm if we don't actually
10959 : : have a DSO handle on this target. */
10960 : 0 : arg2 = null_pointer_node;
10961 : : else
10962 : : arg2 = NULL_TREE;
10963 : :
10964 : 3495 : if (ob_parm)
10965 : : {
10966 : 3495 : if (!CP_DECL_THREAD_LOCAL_P (decl)
10967 : 3495 : && targetm.cxx.use_aeabi_atexit ())
10968 : : {
10969 : : arg1 = cleanup;
10970 : : arg0 = addr;
10971 : : }
10972 : : else
10973 : : {
10974 : : arg1 = addr;
10975 : : arg0 = cleanup;
10976 : : }
10977 : : }
10978 : : else
10979 : : {
10980 : : arg0 = cleanup;
10981 : : arg1 = NULL_TREE;
10982 : : }
10983 : 3507 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
10984 : 3507 : arg0, arg1, arg2, NULL_TREE);
10985 : : }
10986 : :
10987 : : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
10988 : : is its initializer. Generate code to handle the construction
10989 : : and destruction of DECL. */
10990 : :
10991 : : static void
10992 : 16354184 : expand_static_init (tree decl, tree init)
10993 : : {
10994 : 16354184 : gcc_assert (VAR_P (decl));
10995 : 16354184 : gcc_assert (TREE_STATIC (decl));
10996 : :
10997 : : /* Some variables require no dynamic initialization. */
10998 : 16354184 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
10999 : : {
11000 : : /* Make sure the destructor is callable. */
11001 : 16350763 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
11002 : 16350763 : if (!init)
11003 : : return;
11004 : : }
11005 : :
11006 : 15652 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
11007 : 14732 : && !DECL_FUNCTION_SCOPE_P (decl))
11008 : : {
11009 : 15 : auto_diagnostic_group d;
11010 : 15 : location_t dloc = DECL_SOURCE_LOCATION (decl);
11011 : 15 : if (init)
11012 : 12 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11013 : : "needs dynamic initialization", decl);
11014 : : else
11015 : 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
11016 : : "has a non-trivial destructor", decl);
11017 : 15 : static bool informed;
11018 : 15 : if (!informed)
11019 : : {
11020 : 6 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
11021 : : "initialization and destruction");
11022 : 6 : informed = true;
11023 : : }
11024 : 15 : return;
11025 : 15 : }
11026 : :
11027 : 14688 : if (DECL_FUNCTION_SCOPE_P (decl))
11028 : : {
11029 : : /* Emit code to perform this initialization but once. */
11030 : 3802 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
11031 : 3802 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
11032 : 3802 : tree guard, guard_addr;
11033 : 3802 : tree flag, begin;
11034 : : /* We don't need thread-safety code for thread-local vars. */
11035 : 3802 : bool thread_guard = (flag_threadsafe_statics
11036 : 3802 : && !CP_DECL_THREAD_LOCAL_P (decl));
11037 : :
11038 : : /* Emit code to perform this initialization but once. This code
11039 : : looks like:
11040 : :
11041 : : static <type> guard;
11042 : : if (!__atomic_load (guard.first_byte)) {
11043 : : if (__cxa_guard_acquire (&guard)) {
11044 : : bool flag = false;
11045 : : try {
11046 : : // Do initialization.
11047 : : flag = true; __cxa_guard_release (&guard);
11048 : : // Register variable for destruction at end of program.
11049 : : } catch {
11050 : : if (!flag) __cxa_guard_abort (&guard);
11051 : : }
11052 : : }
11053 : : }
11054 : :
11055 : : Note that the `flag' variable is only set to 1 *after* the
11056 : : initialization is complete. This ensures that an exception,
11057 : : thrown during the construction, will cause the variable to
11058 : : reinitialized when we pass through this code again, as per:
11059 : :
11060 : : [stmt.dcl]
11061 : :
11062 : : If the initialization exits by throwing an exception, the
11063 : : initialization is not complete, so it will be tried again
11064 : : the next time control enters the declaration.
11065 : :
11066 : : This process should be thread-safe, too; multiple threads
11067 : : should not be able to initialize the variable more than
11068 : : once. */
11069 : :
11070 : : /* Create the guard variable. */
11071 : 3802 : guard = get_guard (decl);
11072 : :
11073 : : /* Begin the conditional initialization. */
11074 : 3802 : if_stmt = begin_if_stmt ();
11075 : :
11076 : 3802 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
11077 : 3802 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11078 : :
11079 : 3802 : if (thread_guard)
11080 : : {
11081 : 3660 : tree vfntype = NULL_TREE;
11082 : 3660 : tree acquire_name, release_name, abort_name;
11083 : 3660 : tree acquire_fn, release_fn, abort_fn;
11084 : 3660 : guard_addr = build_address (guard);
11085 : :
11086 : 3660 : acquire_name = get_identifier ("__cxa_guard_acquire");
11087 : 3660 : release_name = get_identifier ("__cxa_guard_release");
11088 : 3660 : abort_name = get_identifier ("__cxa_guard_abort");
11089 : 3660 : acquire_fn = get_global_binding (acquire_name);
11090 : 3660 : release_fn = get_global_binding (release_name);
11091 : 3660 : abort_fn = get_global_binding (abort_name);
11092 : 3660 : if (!acquire_fn)
11093 : 2044 : acquire_fn = push_library_fn
11094 : 2044 : (acquire_name, build_function_type_list (integer_type_node,
11095 : 2044 : TREE_TYPE (guard_addr),
11096 : : NULL_TREE),
11097 : : NULL_TREE, ECF_NOTHROW);
11098 : 3660 : if (!release_fn || !abort_fn)
11099 : 2047 : vfntype = build_function_type_list (void_type_node,
11100 : 2047 : TREE_TYPE (guard_addr),
11101 : : NULL_TREE);
11102 : 2047 : if (!release_fn)
11103 : 2047 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
11104 : : ECF_NOTHROW);
11105 : 3660 : if (!abort_fn)
11106 : 2047 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
11107 : : ECF_NOTHROW | ECF_LEAF);
11108 : :
11109 : 3660 : inner_if_stmt = begin_if_stmt ();
11110 : 3660 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
11111 : : inner_if_stmt);
11112 : :
11113 : 3660 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
11114 : 3660 : begin = get_internal_target_expr (boolean_false_node);
11115 : 3660 : flag = TARGET_EXPR_SLOT (begin);
11116 : :
11117 : 3660 : TARGET_EXPR_CLEANUP (begin)
11118 : 3660 : = build3 (COND_EXPR, void_type_node, flag,
11119 : : void_node,
11120 : : build_call_n (abort_fn, 1, guard_addr));
11121 : 3660 : CLEANUP_EH_ONLY (begin) = 1;
11122 : :
11123 : : /* Do the initialization itself. */
11124 : 3660 : init = add_stmt_to_compound (begin, init);
11125 : 3660 : init = add_stmt_to_compound (init,
11126 : : build2 (MODIFY_EXPR, void_type_node,
11127 : : flag, boolean_true_node));
11128 : :
11129 : : /* Use atexit to register a function for destroying this static
11130 : : variable. Do this before calling __cxa_guard_release. */
11131 : 3660 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11132 : :
11133 : 3660 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
11134 : : guard_addr));
11135 : : }
11136 : : else
11137 : : {
11138 : 142 : init = add_stmt_to_compound (init, set_guard (guard));
11139 : :
11140 : : /* Use atexit to register a function for destroying this static
11141 : : variable. */
11142 : 142 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
11143 : : }
11144 : :
11145 : 3802 : finish_expr_stmt (init);
11146 : :
11147 : 3802 : if (thread_guard)
11148 : : {
11149 : 3660 : finish_compound_stmt (inner_then_clause);
11150 : 3660 : finish_then_clause (inner_if_stmt);
11151 : 3660 : finish_if_stmt (inner_if_stmt);
11152 : : }
11153 : :
11154 : 3802 : finish_compound_stmt (then_clause);
11155 : 3802 : finish_then_clause (if_stmt);
11156 : 3802 : finish_if_stmt (if_stmt);
11157 : : }
11158 : 10886 : else if (CP_DECL_THREAD_LOCAL_P (decl))
11159 : 795 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
11160 : : else
11161 : 10091 : static_aggregates = tree_cons (init, decl, static_aggregates);
11162 : : }
11163 : :
11164 : :
11165 : : /* Make TYPE a complete type based on INITIAL_VALUE.
11166 : : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11167 : : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
11168 : : 3 if the initializer list is empty (in pedantic mode). */
11169 : :
11170 : : int
11171 : 522099 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
11172 : : {
11173 : 522099 : int failure;
11174 : 522099 : tree type, elt_type;
11175 : :
11176 : : /* Don't get confused by a CONSTRUCTOR for some other type. */
11177 : 522069 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
11178 : 94167 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
11179 : 522421 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
11180 : : return 1;
11181 : :
11182 : 522096 : if (initial_value)
11183 : : {
11184 : : /* An array of character type can be initialized from a
11185 : : brace-enclosed string constant so call reshape_init to
11186 : : remove the optional braces from a braced string literal. */
11187 : 522066 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
11188 : 522066 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
11189 : 28756 : initial_value = reshape_init (*ptype, initial_value,
11190 : : tf_warning_or_error);
11191 : :
11192 : : /* If any of the elements are parameter packs, we can't actually
11193 : : complete this type now because the array size is dependent. */
11194 : 522066 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
11195 : 15375509 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
11196 : 15093382 : if (PACK_EXPANSION_P (e.value))
11197 : : return 0;
11198 : : }
11199 : :
11200 : 522096 : failure = complete_array_type (ptype, initial_value, do_default);
11201 : :
11202 : : /* We can create the array before the element type is complete, which
11203 : : means that we didn't have these two bits set in the original type
11204 : : either. In completing the type, we are expected to propagate these
11205 : : bits. See also complete_type which does the same thing for arrays
11206 : : of fixed size. */
11207 : 522096 : type = *ptype;
11208 : 1044189 : if (type != error_mark_node && TYPE_DOMAIN (type))
11209 : : {
11210 : 522069 : elt_type = TREE_TYPE (type);
11211 : 522069 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
11212 : 1044138 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
11213 : 522069 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
11214 : : }
11215 : :
11216 : : return failure;
11217 : : }
11218 : :
11219 : : /* As above, but either give an error or reject zero-size arrays, depending
11220 : : on COMPLAIN. */
11221 : :
11222 : : int
11223 : 1409 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
11224 : : bool do_default, tsubst_flags_t complain)
11225 : : {
11226 : 1409 : int failure;
11227 : 1409 : bool sfinae = !(complain & tf_error);
11228 : : /* In SFINAE context we can't be lenient about zero-size arrays. */
11229 : 1409 : if (sfinae)
11230 : 3 : ++pedantic;
11231 : 1409 : failure = cp_complete_array_type (ptype, initial_value, do_default);
11232 : 1409 : if (sfinae)
11233 : 3 : --pedantic;
11234 : 1409 : if (failure)
11235 : : {
11236 : 3 : if (sfinae)
11237 : : /* Not an error. */;
11238 : 0 : else if (failure == 1)
11239 : 0 : error ("initializer fails to determine size of %qT", *ptype);
11240 : 0 : else if (failure == 2)
11241 : : {
11242 : 0 : if (do_default)
11243 : 0 : error ("array size missing in %qT", *ptype);
11244 : : }
11245 : 0 : else if (failure == 3)
11246 : 0 : error ("zero-size array %qT", *ptype);
11247 : 3 : *ptype = error_mark_node;
11248 : : }
11249 : 1409 : return failure;
11250 : : }
11251 : :
11252 : : /* Return zero if something is declared to be a member of type
11253 : : CTYPE when in the context of CUR_TYPE. STRING is the error
11254 : : message to print in that case. Otherwise, quietly return 1. */
11255 : :
11256 : : static int
11257 : 24492860 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
11258 : : {
11259 : 24492860 : if (ctype && ctype != cur_type)
11260 : : {
11261 : 0 : if (flags == DTOR_FLAG)
11262 : 0 : error ("destructor for alien class %qT cannot be a member", ctype);
11263 : : else
11264 : 0 : error ("constructor for alien class %qT cannot be a member", ctype);
11265 : 0 : return 0;
11266 : : }
11267 : : return 1;
11268 : : }
11269 : :
11270 : : /* Subroutine of `grokdeclarator'. */
11271 : :
11272 : : /* Generate errors possibly applicable for a given set of specifiers.
11273 : : This is for ARM $7.1.2. */
11274 : :
11275 : : static void
11276 : 404613667 : bad_specifiers (tree object,
11277 : : enum bad_spec_place type,
11278 : : int virtualp,
11279 : : int quals,
11280 : : int inlinep,
11281 : : int friendp,
11282 : : int raises,
11283 : : const location_t* locations)
11284 : : {
11285 : 404613667 : switch (type)
11286 : : {
11287 : 56378805 : case BSP_VAR:
11288 : 56378805 : if (virtualp)
11289 : 0 : error_at (locations[ds_virtual],
11290 : : "%qD declared as a %<virtual%> variable", object);
11291 : 56378805 : if (quals)
11292 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11293 : : "%qD invalid in variable declaration", object);
11294 : : break;
11295 : 276846687 : case BSP_PARM:
11296 : 276846687 : if (virtualp)
11297 : 0 : error_at (locations[ds_virtual],
11298 : : "%qD declared as a %<virtual%> parameter", object);
11299 : 276846687 : if (inlinep)
11300 : 6 : error_at (locations[ds_inline],
11301 : : "%qD declared as an %<inline%> parameter", object);
11302 : 276846687 : if (quals)
11303 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
11304 : : "%qD invalid in parameter declaration", object);
11305 : : break;
11306 : 43753530 : case BSP_TYPE:
11307 : 43753530 : if (virtualp)
11308 : 0 : error_at (locations[ds_virtual],
11309 : : "%qD declared as a %<virtual%> type", object);
11310 : 43753530 : if (inlinep)
11311 : 6 : error_at (locations[ds_inline],
11312 : : "%qD declared as an %<inline%> type", object);
11313 : 43753530 : if (quals)
11314 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11315 : : "%qD invalid in type declaration", object);
11316 : : break;
11317 : 27634645 : case BSP_FIELD:
11318 : 27634645 : if (virtualp)
11319 : 6 : error_at (locations[ds_virtual],
11320 : : "%qD declared as a %<virtual%> field", object);
11321 : 27634645 : if (inlinep)
11322 : 6 : error_at (locations[ds_inline],
11323 : : "%qD declared as an %<inline%> field", object);
11324 : 27634645 : if (quals)
11325 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
11326 : : "%qD invalid in field declaration", object);
11327 : : break;
11328 : 0 : default:
11329 : 0 : gcc_unreachable();
11330 : : }
11331 : 404613667 : if (friendp)
11332 : 0 : error ("%q+D declared as a friend", object);
11333 : 404613667 : if (raises
11334 : 109 : && !flag_noexcept_type
11335 : 404613688 : && (TREE_CODE (object) == TYPE_DECL
11336 : 15 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
11337 : 5 : && !TYPE_REFFN_P (TREE_TYPE (object))
11338 : 4 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
11339 : 6 : error ("%q+D declared with an exception specification", object);
11340 : 404613667 : }
11341 : :
11342 : : /* DECL is a member function or static data member and is presently
11343 : : being defined. Check that the definition is taking place in a
11344 : : valid namespace. */
11345 : :
11346 : : static void
11347 : 84724364 : check_class_member_definition_namespace (tree decl)
11348 : : {
11349 : : /* These checks only apply to member functions and static data
11350 : : members. */
11351 : 84724364 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
11352 : : /* We check for problems with specializations in pt.cc in
11353 : : check_specialization_namespace, where we can issue better
11354 : : diagnostics. */
11355 : 84724364 : if (processing_specialization)
11356 : : return;
11357 : : /* We check this in check_explicit_instantiation_namespace. */
11358 : 84567016 : if (processing_explicit_instantiation)
11359 : : return;
11360 : : /* [class.mfct]
11361 : :
11362 : : A member function definition that appears outside of the
11363 : : class definition shall appear in a namespace scope enclosing
11364 : : the class definition.
11365 : :
11366 : : [class.static.data]
11367 : :
11368 : : The definition for a static data member shall appear in a
11369 : : namespace scope enclosing the member's class definition. */
11370 : 84566944 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
11371 : 18 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
11372 : 18 : decl, DECL_CONTEXT (decl));
11373 : : }
11374 : :
11375 : : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
11376 : : METHOD_TYPE for a non-static member function; QUALS are the
11377 : : cv-qualifiers that apply to the function. */
11378 : :
11379 : : tree
11380 : 206321154 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
11381 : : {
11382 : 206321154 : tree this_type;
11383 : 206321154 : tree qual_type;
11384 : 206321154 : tree parm;
11385 : 206321154 : cp_cv_quals this_quals;
11386 : :
11387 : 206321154 : if (CLASS_TYPE_P (type))
11388 : : {
11389 : 92595556 : this_type
11390 : 92595556 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
11391 : 92595556 : this_type = build_pointer_type (this_type);
11392 : : }
11393 : : else
11394 : 113725598 : this_type = type_of_this_parm (type);
11395 : : /* The `this' parameter is implicitly `const'; it cannot be
11396 : : assigned to. */
11397 : 206321154 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
11398 : 206321154 : qual_type = cp_build_qualified_type (this_type, this_quals);
11399 : 206321154 : parm = build_artificial_parm (fn, this_identifier, qual_type);
11400 : 206321154 : cp_apply_type_quals_to_decl (this_quals, parm);
11401 : 206321154 : return parm;
11402 : : }
11403 : :
11404 : : /* DECL is a static member function. Complain if it was declared
11405 : : with function-cv-quals. */
11406 : :
11407 : : static void
11408 : 8752247 : check_static_quals (tree decl, cp_cv_quals quals)
11409 : : {
11410 : 0 : if (quals != TYPE_UNQUALIFIED)
11411 : 6 : error ("static member function %q#D declared with type qualifiers",
11412 : : decl);
11413 : 0 : }
11414 : :
11415 : : /* Helper function. Replace the temporary this parameter injected
11416 : : during cp_finish_omp_declare_simd with the real this parameter. */
11417 : :
11418 : : static tree
11419 : 2500 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
11420 : : {
11421 : 2500 : tree this_parm = (tree) data;
11422 : 2500 : if (TREE_CODE (*tp) == PARM_DECL
11423 : 450 : && DECL_NAME (*tp) == this_identifier
11424 : 2773 : && *tp != this_parm)
11425 : 273 : *tp = this_parm;
11426 : 2227 : else if (TYPE_P (*tp))
11427 : 33 : *walk_subtrees = 0;
11428 : 2500 : return NULL_TREE;
11429 : : }
11430 : :
11431 : : /* CTYPE is class type, or null if non-class.
11432 : : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
11433 : : or METHOD_TYPE.
11434 : : DECLARATOR is the function's name.
11435 : : PARMS is a chain of PARM_DECLs for the function.
11436 : : VIRTUALP is truthvalue of whether the function is virtual or not.
11437 : : FLAGS are to be passed through to `grokclassfn'.
11438 : : QUALS are qualifiers indicating whether the function is `const'
11439 : : or `volatile'.
11440 : : RAISES is a list of exceptions that this function can raise.
11441 : : CHECK is 1 if we must find this method in CTYPE, 0 if we should
11442 : : not look, and -1 if we should not call `grokclassfn' at all.
11443 : :
11444 : : SFK is the kind of special function (if any) for the new function.
11445 : :
11446 : : Returns `NULL_TREE' if something goes wrong, after issuing
11447 : : applicable error messages. */
11448 : :
11449 : : static tree
11450 : 173764489 : grokfndecl (tree ctype,
11451 : : tree type,
11452 : : tree declarator,
11453 : : tree parms,
11454 : : tree orig_declarator,
11455 : : const cp_decl_specifier_seq *declspecs,
11456 : : tree decl_reqs,
11457 : : int virtualp,
11458 : : enum overload_flags flags,
11459 : : cp_cv_quals quals,
11460 : : cp_ref_qualifier rqual,
11461 : : tree raises,
11462 : : int check,
11463 : : int friendp,
11464 : : int publicp,
11465 : : int inlinep,
11466 : : bool deletedp,
11467 : : bool xobj_func_p,
11468 : : special_function_kind sfk,
11469 : : bool funcdef_flag,
11470 : : bool late_return_type_p,
11471 : : int template_count,
11472 : : tree in_namespace,
11473 : : tree* attrlist,
11474 : : location_t location)
11475 : : {
11476 : 173764489 : tree decl;
11477 : 173764489 : tree t;
11478 : :
11479 : 173764489 : if (location == UNKNOWN_LOCATION)
11480 : 0 : location = input_location;
11481 : :
11482 : : /* Was the concept specifier present? */
11483 : 173764489 : bool concept_p = inlinep & 4;
11484 : :
11485 : 173764489 : if (concept_p)
11486 : : {
11487 : 15 : error_at (location, "function concepts are no longer supported");
11488 : 15 : return NULL_TREE;
11489 : : }
11490 : :
11491 : 173764474 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
11492 : :
11493 : 173764474 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
11494 : :
11495 : : /* Set the constraints on the declaration. */
11496 : 173764474 : if (flag_concepts)
11497 : : {
11498 : 50082018 : tree tmpl_reqs = NULL_TREE;
11499 : 50082018 : tree ctx = friendp ? current_class_type : ctype;
11500 : 50082018 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
11501 : 50082018 : bool memtmpl = (!block_local
11502 : 50082018 : && (current_template_depth
11503 : 50069280 : > template_class_depth (ctx)));
11504 : 14018380 : if (memtmpl)
11505 : : {
11506 : 14018380 : if (!current_template_parms)
11507 : : /* If there are no template parameters, something must have
11508 : : gone wrong. */
11509 : 0 : gcc_assert (seen_error ());
11510 : : else
11511 : 14018380 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
11512 : : }
11513 : 50082018 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
11514 : : /* C++20 CA378: Remove non-templated constrained functions. */
11515 : : /* [temp.friend]/9 A non-template friend declaration with a
11516 : : requires-clause shall be a definition. A friend function template with
11517 : : a constraint that depends on a template parameter from an enclosing
11518 : : template shall be a definition. */
11519 : 50082018 : if (ci
11520 : 3600663 : && (block_local
11521 : 3600660 : || !processing_template_decl
11522 : 3600605 : || (friendp && !memtmpl && !funcdef_flag)))
11523 : : {
11524 : 67 : if (!friendp || !processing_template_decl)
11525 : 58 : error_at (location, "constraints on a non-templated function");
11526 : : else
11527 : 9 : error_at (location, "constrained non-template friend declaration"
11528 : : " must be a definition");
11529 : : ci = NULL_TREE;
11530 : : }
11531 : 50082018 : set_constraints (decl, ci);
11532 : 474974 : if (ci && friendp && memtmpl && !funcdef_flag
11533 : 50091391 : && uses_outer_template_parms_in_constraints (decl, ctx))
11534 : 6 : error_at (location, "friend function template with constraints that "
11535 : : "depend on outer template parameters must be a definition");
11536 : : }
11537 : :
11538 : 173764474 : if (TREE_CODE (type) == METHOD_TYPE)
11539 : : {
11540 : 91082002 : tree parm = build_this_parm (decl, type, quals);
11541 : 91082002 : DECL_CHAIN (parm) = parms;
11542 : 91082002 : parms = parm;
11543 : :
11544 : : /* Allocate space to hold the vptr bit if needed. */
11545 : 91082002 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
11546 : : }
11547 : :
11548 : 173764474 : DECL_ARGUMENTS (decl) = parms;
11549 : 526422747 : for (t = parms; t; t = DECL_CHAIN (t))
11550 : 352658273 : DECL_CONTEXT (t) = decl;
11551 : :
11552 : : /* Propagate volatile out from type to decl. */
11553 : 173764474 : if (TYPE_VOLATILE (type))
11554 : 0 : TREE_THIS_VOLATILE (decl) = 1;
11555 : :
11556 : : /* Setup decl according to sfk. */
11557 : 173764474 : switch (sfk)
11558 : : {
11559 : 22314122 : case sfk_constructor:
11560 : 22314122 : case sfk_copy_constructor:
11561 : 22314122 : case sfk_move_constructor:
11562 : 22314122 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
11563 : 22314122 : DECL_NAME (decl) = ctor_identifier;
11564 : 22314122 : break;
11565 : 3146751 : case sfk_destructor:
11566 : 3146751 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
11567 : 3146751 : DECL_NAME (decl) = dtor_identifier;
11568 : 3146751 : break;
11569 : 1069265 : case sfk_deduction_guide:
11570 : : /* Give deduction guides a definition even though they don't really
11571 : : have one: the restriction that you can't repeat a deduction guide
11572 : : makes them more like a definition anyway. */
11573 : 1069265 : DECL_INITIAL (decl) = void_node;
11574 : : /* But to ensure that external-linkage deduction guides in header units
11575 : : don't fall afoul of [module.import] p6, mark them as inline. */
11576 : 1069265 : DECL_DECLARED_INLINE_P (decl) = true;
11577 : 1069265 : break;
11578 : : default:
11579 : : break;
11580 : : }
11581 : :
11582 : 173764474 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
11583 : : {
11584 : 36819 : if (funcdef_flag)
11585 : 3 : error_at (location,
11586 : : "defining explicit specialization %qD in friend declaration",
11587 : : orig_declarator);
11588 : : else
11589 : : {
11590 : 36816 : tree fns = TREE_OPERAND (orig_declarator, 0);
11591 : 36816 : tree args = TREE_OPERAND (orig_declarator, 1);
11592 : :
11593 : 36816 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11594 : : {
11595 : : /* Something like `template <class T> friend void f<T>()'. */
11596 : 6 : error_at (location,
11597 : : "invalid use of template-id %qD in declaration "
11598 : : "of primary template",
11599 : : orig_declarator);
11600 : 6 : return NULL_TREE;
11601 : : }
11602 : :
11603 : : /* A friend declaration of the form friend void f<>(). Record
11604 : : the information in the TEMPLATE_ID_EXPR. */
11605 : 36810 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
11606 : :
11607 : 36810 : gcc_assert (identifier_p (fns) || OVL_P (fns));
11608 : 36810 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
11609 : :
11610 : 134604 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
11611 : 97798 : if (TREE_PURPOSE (t)
11612 : 97798 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
11613 : : {
11614 : 4 : error_at (defparse_location (TREE_PURPOSE (t)),
11615 : : "default arguments are not allowed in declaration "
11616 : : "of friend template specialization %qD",
11617 : : decl);
11618 : 4 : return NULL_TREE;
11619 : : }
11620 : :
11621 : 36806 : if (inlinep & 1)
11622 : : {
11623 : 4 : error_at (declspecs->locations[ds_inline],
11624 : : "%<inline%> is not allowed in declaration of friend "
11625 : : "template specialization %qD",
11626 : : decl);
11627 : 4 : return NULL_TREE;
11628 : : }
11629 : : }
11630 : : }
11631 : :
11632 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
11633 : : expression, that declaration shall be a definition..." */
11634 : 173764460 : if (friendp && !funcdef_flag)
11635 : : {
11636 : 1269222 : bool has_errored = false;
11637 : 1269222 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
11638 : 3679531 : t && t != void_list_node; t = TREE_CHAIN (t))
11639 : 2410309 : if (TREE_PURPOSE (t))
11640 : : {
11641 : 48 : enum diagnostics::kind diag_kind = diagnostics::kind::permerror;
11642 : : /* For templates, mark the default argument as erroneous and give a
11643 : : hard error. */
11644 : 48 : if (processing_template_decl)
11645 : : {
11646 : 30 : diag_kind = diagnostics::kind::error;
11647 : 30 : TREE_PURPOSE (t) = error_mark_node;
11648 : : }
11649 : 48 : if (!has_errored)
11650 : : {
11651 : 45 : has_errored = true;
11652 : 45 : emit_diagnostic (diag_kind,
11653 : 45 : DECL_SOURCE_LOCATION (decl),
11654 : 45 : /*diagnostics::option_id=*/0,
11655 : : "friend declaration of %qD specifies default "
11656 : : "arguments and isn%'t a definition", decl);
11657 : : }
11658 : : }
11659 : : }
11660 : :
11661 : : /* If this decl has namespace scope, set that up. */
11662 : 173764460 : if (in_namespace)
11663 : 172711 : set_decl_namespace (decl, in_namespace, friendp);
11664 : 173591749 : else if (ctype)
11665 : 99700266 : DECL_CONTEXT (decl) = ctype;
11666 : : else
11667 : 73891483 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
11668 : :
11669 : : /* `main' and builtins have implicit 'C' linkage. */
11670 : 173764460 : if (ctype == NULL_TREE
11671 : 74064194 : && DECL_FILE_SCOPE_P (decl)
11672 : 32252001 : && current_lang_name == lang_name_cplusplus
11673 : 176565312 : && (MAIN_NAME_P (declarator)
11674 : 2767880 : || (IDENTIFIER_LENGTH (declarator) > 10
11675 : 1933392 : && IDENTIFIER_POINTER (declarator)[0] == '_'
11676 : 1475384 : && IDENTIFIER_POINTER (declarator)[1] == '_'
11677 : 352607 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
11678 : : "builtin_"))
11679 : 2767862 : || (targetcm.cxx_implicit_extern_c
11680 : 0 : && (targetcm.cxx_implicit_extern_c
11681 : 0 : (IDENTIFIER_POINTER (declarator))))))
11682 : 32990 : SET_DECL_LANGUAGE (decl, lang_c);
11683 : :
11684 : 173764460 : DECL_STATIC_FUNCTION_P (decl)
11685 : 173764460 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
11686 : 173764460 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
11687 : :
11688 : 173764460 : if (deletedp)
11689 : 3525854 : DECL_DELETED_FN (decl) = 1;
11690 : :
11691 : 173764460 : if (ctype && funcdef_flag)
11692 : 84336372 : check_class_member_definition_namespace (decl);
11693 : :
11694 : 173764460 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11695 : : {
11696 : 32978 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
11697 : 9 : error_at (location, "cannot declare %<::main%> to be a template");
11698 : 32978 : if (inlinep & 1)
11699 : 3 : error_at (declspecs->locations[ds_inline],
11700 : : "cannot declare %<::main%> to be inline");
11701 : 32978 : if (inlinep & 2)
11702 : 6 : error_at (declspecs->locations[ds_constexpr],
11703 : : "cannot declare %<::main%> to be %qs", "constexpr");
11704 : 32978 : if (inlinep & 8)
11705 : 3 : error_at (declspecs->locations[ds_consteval],
11706 : : "cannot declare %<::main%> to be %qs", "consteval");
11707 : 32978 : if (!publicp)
11708 : 0 : error_at (location, "cannot declare %<::main%> to be static");
11709 : 32978 : if (current_lang_name != lang_name_cplusplus)
11710 : 12 : pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
11711 : : " linkage specification other than %<extern \"C++\"%>");
11712 : 32978 : if (module_attach_p ())
11713 : : {
11714 : 3 : auto_diagnostic_group adg;
11715 : 3 : error_at (location, "cannot attach %<::main%> to a named module");
11716 : 3 : inform (location, "use %<extern \"C++\"%> to attach it to the "
11717 : : "global module instead");
11718 : 3 : }
11719 : : inlinep = 0;
11720 : : publicp = 1;
11721 : : }
11722 : :
11723 : : /* Members of anonymous types and local classes have no linkage; make
11724 : : them internal. If a typedef is made later, this will be changed. */
11725 : 173764460 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
11726 : : publicp = 0;
11727 : 173724051 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
11728 : : /* But members of local classes in a module CMI should have their
11729 : : definitions exported, in case they are (directly or indirectly)
11730 : : used by an importer. We don't just use module_has_cmi_p here
11731 : : because for entities in the GMF we don't yet know whether this
11732 : : module will have a CMI, so we'll conservatively assume it might. */
11733 : 1476086 : publicp = module_maybe_has_cmi_p ();
11734 : :
11735 : 173724051 : if (publicp && cxx_dialect == cxx98)
11736 : : {
11737 : : /* [basic.link]: A name with no linkage (notably, the name of a class
11738 : : or enumeration declared in a local scope) shall not be used to
11739 : : declare an entity with linkage.
11740 : :
11741 : : DR 757 relaxes this restriction for C++0x. */
11742 : 976750 : no_linkage_error (decl);
11743 : : }
11744 : :
11745 : 173764460 : TREE_PUBLIC (decl) = publicp;
11746 : 173764460 : if (! publicp)
11747 : : {
11748 : 1619192 : DECL_INTERFACE_KNOWN (decl) = 1;
11749 : 1619192 : DECL_NOT_REALLY_EXTERN (decl) = 1;
11750 : : }
11751 : :
11752 : : /* If the declaration was declared inline, mark it as such. */
11753 : 173764460 : if (inlinep)
11754 : : {
11755 : 51490451 : DECL_DECLARED_INLINE_P (decl) = 1;
11756 : 51490451 : if (publicp)
11757 : 51344890 : DECL_COMDAT (decl) = 1;
11758 : : }
11759 : 51490451 : if (inlinep & 2)
11760 : 30573096 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
11761 : 143191364 : else if (inlinep & 8)
11762 : : {
11763 : 233235 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
11764 : 233235 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
11765 : : }
11766 : :
11767 : 173764460 : DECL_EXTERNAL (decl) = 1;
11768 : 173764460 : if (TREE_CODE (type) == FUNCTION_TYPE)
11769 : : {
11770 : 82682458 : if (quals || rqual)
11771 : 272 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
11772 : : TYPE_UNQUALIFIED,
11773 : : REF_QUAL_NONE);
11774 : 82682458 : auto_diagnostic_group d;
11775 : 82682458 : if (quals)
11776 : 423 : error (!ctype
11777 : : ? G_("non-member function %qD cannot have cv-qualifier")
11778 : : : !xobj_func_p
11779 : 198 : ? G_("static member function %qD cannot have cv-qualifier")
11780 : : : G_("explicit object member function "
11781 : : "%qD cannot have cv-qualifier"),
11782 : : decl);
11783 : 82682458 : if (rqual)
11784 : 355 : error (!ctype
11785 : : ? G_("non-member function %qD cannot have ref-qualifier")
11786 : : : !xobj_func_p
11787 : 176 : ? G_("static member function %qD cannot have ref-qualifier")
11788 : : : G_("explicit object member function "
11789 : : "%qD cannot have ref-qualifier"),
11790 : : decl);
11791 : :
11792 : 82682458 : if (xobj_func_p && (quals || rqual))
11793 : 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
11794 : : "explicit object parameter declared here");
11795 : 82682458 : quals = TYPE_UNQUALIFIED;
11796 : 82682458 : rqual = REF_QUAL_NONE;
11797 : :
11798 : 82682458 : }
11799 : :
11800 : 173764460 : if (deduction_guide_p (decl))
11801 : : {
11802 : 1069265 : tree type = TREE_TYPE (DECL_NAME (decl));
11803 : 1069265 : if (in_namespace == NULL_TREE
11804 : 1069265 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
11805 : : {
11806 : 3 : auto_diagnostic_group d;
11807 : 3 : error_at (location, "deduction guide %qD must be declared in the "
11808 : : "same scope as %qT", decl, type);
11809 : 3 : inform (location_of (type), " declared here");
11810 : 3 : return NULL_TREE;
11811 : 3 : }
11812 : 2138524 : if (DECL_CLASS_SCOPE_P (decl)
11813 : 1069301 : && current_access_specifier != declared_access (TYPE_NAME (type)))
11814 : : {
11815 : 3 : auto_diagnostic_group d;
11816 : 3 : error_at (location, "deduction guide %qD must have the same access "
11817 : : "as %qT", decl, type);
11818 : 3 : inform (location_of (type), " declared here");
11819 : 3 : }
11820 : 1069262 : if (funcdef_flag)
11821 : 3 : error_at (location,
11822 : : "deduction guide %qD must not have a function body", decl);
11823 : : }
11824 : 324063273 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
11825 : 183218481 : && !grok_op_properties (decl, /*complain=*/true))
11826 : : return NULL_TREE;
11827 : 172695045 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
11828 : : {
11829 : 281151 : bool long_long_unsigned_p;
11830 : 281151 : bool long_double_p;
11831 : 281151 : const char *suffix = NULL;
11832 : : /* [over.literal]/6: Literal operators shall not have C linkage. */
11833 : 281151 : if (DECL_LANGUAGE (decl) == lang_c)
11834 : : {
11835 : 6 : auto_diagnostic_group d;
11836 : 6 : error_at (location, "literal operator with C linkage");
11837 : 6 : maybe_show_extern_c_location ();
11838 : 6 : return NULL_TREE;
11839 : 6 : }
11840 : :
11841 : 281145 : if (DECL_NAMESPACE_SCOPE_P (decl))
11842 : : {
11843 : 281139 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
11844 : : &long_double_p))
11845 : : {
11846 : 42 : error_at (location, "%qD has invalid argument list", decl);
11847 : 42 : return NULL_TREE;
11848 : : }
11849 : :
11850 : 281097 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
11851 : 281097 : if (long_long_unsigned_p)
11852 : : {
11853 : 33918 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
11854 : 68 : warning_at (location, 0, "integer suffix %qs"
11855 : : " shadowed by implementation", suffix);
11856 : : }
11857 : 247179 : else if (long_double_p)
11858 : : {
11859 : 88929 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
11860 : 80 : warning_at (location, 0, "floating-point suffix %qs"
11861 : : " shadowed by implementation", suffix);
11862 : : }
11863 : : /* 17.6.3.3.5 */
11864 : 281097 : if (suffix[0] != '_'
11865 : 280548 : && !current_function_decl && !(friendp && !funcdef_flag))
11866 : 280542 : warning_at (location, OPT_Wliteral_suffix,
11867 : : "literal operator suffixes not preceded by %<_%>"
11868 : : " are reserved for future standardization");
11869 : : }
11870 : : else
11871 : : {
11872 : 6 : error_at (location, "%qD must be a non-member function", decl);
11873 : 6 : return NULL_TREE;
11874 : : }
11875 : : }
11876 : :
11877 : 172694994 : if (funcdef_flag)
11878 : : /* Make the init_value nonzero so pushdecl knows this is not
11879 : : tentative. error_mark_node is replaced later with the BLOCK. */
11880 : 118023863 : DECL_INITIAL (decl) = error_mark_node;
11881 : :
11882 : 173764253 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
11883 : 65508206 : TREE_NOTHROW (decl) = 1;
11884 : :
11885 : 173764253 : if (flag_openmp || flag_openmp_simd)
11886 : : {
11887 : : /* Adjust "omp declare simd" attributes. */
11888 : 451749 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
11889 : 451749 : if (ods)
11890 : : {
11891 : : tree attr;
11892 : 2006 : for (attr = ods; attr;
11893 : 1060 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
11894 : : {
11895 : 1060 : if (TREE_CODE (type) == METHOD_TYPE)
11896 : 204 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
11897 : : DECL_ARGUMENTS (decl), NULL);
11898 : 1060 : if (TREE_VALUE (attr) != NULL_TREE)
11899 : : {
11900 : 854 : tree cl = TREE_VALUE (TREE_VALUE (attr));
11901 : 854 : cl = c_omp_declare_simd_clauses_to_numbers
11902 : 854 : (DECL_ARGUMENTS (decl), cl);
11903 : 854 : if (cl)
11904 : 854 : TREE_VALUE (TREE_VALUE (attr)) = cl;
11905 : : else
11906 : 0 : TREE_VALUE (attr) = NULL_TREE;
11907 : : }
11908 : : }
11909 : : }
11910 : : }
11911 : :
11912 : : /* Caller will do the rest of this. */
11913 : 173764253 : if (check < 0)
11914 : 4154064 : return decl;
11915 : :
11916 : 169610189 : if (ctype != NULL_TREE)
11917 : 99699970 : grokclassfn (ctype, decl, flags);
11918 : :
11919 : : /* 12.4/3 */
11920 : 169610189 : if (cxx_dialect >= cxx11
11921 : 337282514 : && DECL_DESTRUCTOR_P (decl)
11922 : 3128077 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
11923 : 169781767 : && !processing_template_decl)
11924 : 86780 : deduce_noexcept_on_destructor (decl);
11925 : :
11926 : 169610189 : set_originating_module (decl);
11927 : :
11928 : 169610189 : decl = check_explicit_specialization (orig_declarator, decl,
11929 : : template_count,
11930 : 169610189 : 2 * funcdef_flag +
11931 : 169610189 : 4 * (friendp != 0),
11932 : : *attrlist);
11933 : 169610189 : if (decl == error_mark_node)
11934 : : return NULL_TREE;
11935 : :
11936 : 169609941 : if (DECL_STATIC_FUNCTION_P (decl))
11937 : 8592158 : check_static_quals (decl, quals);
11938 : :
11939 : 169609941 : if (attrlist)
11940 : : {
11941 : 169609941 : cplus_decl_attributes (&decl, *attrlist, 0);
11942 : 169609941 : *attrlist = NULL_TREE;
11943 : : }
11944 : :
11945 : 169609941 : if (DECL_HAS_CONTRACTS_P (decl))
11946 : 467 : rebuild_postconditions (decl);
11947 : :
11948 : : /* Check main's type after attributes have been applied. */
11949 : 169609941 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
11950 : : {
11951 : 32966 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
11952 : : integer_type_node))
11953 : : {
11954 : 19 : tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
11955 : 19 : tree newtype;
11956 : 19 : error_at (declspecs->locations[ds_type_spec],
11957 : : "%<::main%> must return %<int%>");
11958 : 19 : newtype = build_function_type (integer_type_node, oldtypeargs);
11959 : 19 : TREE_TYPE (decl) = newtype;
11960 : : }
11961 : 32966 : if (warn_main)
11962 : 32944 : check_main_parameter_types (decl);
11963 : : }
11964 : :
11965 : 169609941 : if (ctype != NULL_TREE && check)
11966 : : {
11967 : 9136743 : tree old_decl = check_classfn (ctype, decl,
11968 : 9136743 : (current_template_depth
11969 : 9136743 : > template_class_depth (ctype))
11970 : 2142308 : ? current_template_parms
11971 : : : NULL_TREE);
11972 : :
11973 : 9136743 : if (old_decl == error_mark_node)
11974 : : return NULL_TREE;
11975 : :
11976 : 9136662 : if (old_decl)
11977 : : {
11978 : 8727208 : tree ok;
11979 : 8727208 : tree pushed_scope;
11980 : :
11981 : 8727208 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
11982 : : /* Because grokfndecl is always supposed to return a
11983 : : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11984 : : here. We depend on our callers to figure out that its
11985 : : really a template that's being returned. */
11986 : 2142296 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
11987 : :
11988 : 8727208 : if (DECL_STATIC_FUNCTION_P (old_decl)
11989 : 8727208 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
11990 : : {
11991 : : /* Remove the `this' parm added by grokclassfn. */
11992 : 160089 : revert_static_member_fn (decl);
11993 : 160089 : check_static_quals (decl, quals);
11994 : : }
11995 : 8727208 : if (DECL_ARTIFICIAL (old_decl))
11996 : : {
11997 : 9 : error ("definition of implicitly-declared %qD", old_decl);
11998 : 9 : return NULL_TREE;
11999 : : }
12000 : 8727199 : else if (DECL_DEFAULTED_FN (old_decl))
12001 : : {
12002 : 3 : auto_diagnostic_group d;
12003 : 3 : error ("definition of explicitly-defaulted %q+D", decl);
12004 : 3 : inform (DECL_SOURCE_LOCATION (old_decl),
12005 : : "%q#D explicitly defaulted here", old_decl);
12006 : 3 : return NULL_TREE;
12007 : 3 : }
12008 : :
12009 : : /* Since we've smashed OLD_DECL to its
12010 : : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
12011 : 8727196 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12012 : 1011 : decl = DECL_TEMPLATE_RESULT (decl);
12013 : :
12014 : : /* Attempt to merge the declarations. This can fail, in
12015 : : the case of some invalid specialization declarations. */
12016 : 8727196 : pushed_scope = push_scope (ctype);
12017 : 8727196 : ok = duplicate_decls (decl, old_decl);
12018 : 8727196 : if (pushed_scope)
12019 : 8727196 : pop_scope (pushed_scope);
12020 : 8727196 : if (!ok)
12021 : : {
12022 : 0 : error ("no %q#D member function declared in class %qT",
12023 : : decl, ctype);
12024 : 0 : return NULL_TREE;
12025 : : }
12026 : 8727196 : if (ok == error_mark_node)
12027 : : return NULL_TREE;
12028 : : return old_decl;
12029 : : }
12030 : : }
12031 : :
12032 : 321765304 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
12033 : : return NULL_TREE;
12034 : :
12035 : 160882649 : if (ctype == NULL_TREE || check)
12036 : 70319482 : return decl;
12037 : :
12038 : 90563167 : if (virtualp)
12039 : 4054421 : DECL_VIRTUAL_P (decl) = 1;
12040 : :
12041 : 90563167 : return decl;
12042 : : }
12043 : :
12044 : : /* decl is a FUNCTION_DECL.
12045 : : specifiers are the parsed virt-specifiers.
12046 : :
12047 : : Set flags to reflect the virt-specifiers.
12048 : :
12049 : : Returns decl. */
12050 : :
12051 : : static tree
12052 : 94717329 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
12053 : : {
12054 : 94717329 : if (decl == NULL_TREE)
12055 : : return decl;
12056 : 94717231 : if (specifiers & VIRT_SPEC_OVERRIDE)
12057 : 178313 : DECL_OVERRIDE_P (decl) = 1;
12058 : 94717231 : if (specifiers & VIRT_SPEC_FINAL)
12059 : 2900 : DECL_FINAL_P (decl) = 1;
12060 : : return decl;
12061 : : }
12062 : :
12063 : : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
12064 : : the linkage that DECL will receive in the object file. */
12065 : :
12066 : : static void
12067 : 10878742 : set_linkage_for_static_data_member (tree decl)
12068 : : {
12069 : : /* A static data member always has static storage duration and
12070 : : external linkage. Note that static data members are forbidden in
12071 : : local classes -- the only situation in which a class has
12072 : : non-external linkage. */
12073 : 10878742 : TREE_PUBLIC (decl) = 1;
12074 : 10878742 : TREE_STATIC (decl) = 1;
12075 : : /* For non-template classes, static data members are always put
12076 : : out in exactly those files where they are defined, just as
12077 : : with ordinary namespace-scope variables. */
12078 : 10878742 : if (!processing_template_decl)
12079 : 8100156 : DECL_INTERFACE_KNOWN (decl) = 1;
12080 : 10878742 : }
12081 : :
12082 : : /* Create a VAR_DECL named NAME with the indicated TYPE.
12083 : :
12084 : : If SCOPE is non-NULL, it is the class type or namespace containing
12085 : : the variable. If SCOPE is NULL, the variable should is created in
12086 : : the innermost enclosing scope. */
12087 : :
12088 : : static tree
12089 : 56378854 : grokvardecl (tree type,
12090 : : tree name,
12091 : : tree orig_declarator,
12092 : : const cp_decl_specifier_seq *declspecs,
12093 : : int initialized,
12094 : : int type_quals,
12095 : : int inlinep,
12096 : : bool conceptp,
12097 : : int template_count,
12098 : : tree scope,
12099 : : location_t location)
12100 : : {
12101 : 56378854 : tree decl;
12102 : 56378854 : tree explicit_scope;
12103 : :
12104 : 56378854 : gcc_assert (!name || identifier_p (name));
12105 : :
12106 : 56378854 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
12107 : 56378854 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
12108 : :
12109 : : /* Compute the scope in which to place the variable, but remember
12110 : : whether or not that scope was explicitly specified by the user. */
12111 : 56378854 : explicit_scope = scope;
12112 : 56378854 : if (!scope)
12113 : : {
12114 : : /* An explicit "extern" specifier indicates a namespace-scope
12115 : : variable. */
12116 : 55986348 : if (declspecs->storage_class == sc_extern)
12117 : 559905 : scope = current_decl_namespace ();
12118 : 55426443 : else if (!at_function_scope_p ())
12119 : 4301954 : scope = current_scope ();
12120 : : }
12121 : :
12122 : 55986348 : if (scope
12123 : 5254365 : && (/* If the variable is a namespace-scope variable declared in a
12124 : : template, we need DECL_LANG_SPECIFIC. */
12125 : 5254365 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
12126 : : /* Similarly for namespace-scope variables with language linkage
12127 : : other than C++. */
12128 : 4866373 : || (TREE_CODE (scope) == NAMESPACE_DECL
12129 : 2460341 : && current_lang_name != lang_name_cplusplus)
12130 : : /* Similarly for static data members. */
12131 : 2611927 : || TYPE_P (scope)
12132 : : /* Similarly for explicit specializations. */
12133 : 2223935 : || (orig_declarator
12134 : 2223493 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
12135 : 3379017 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
12136 : : else
12137 : 52999837 : decl = build_decl (location, VAR_DECL, name, type);
12138 : :
12139 : 56378854 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
12140 : 4514 : set_decl_namespace (decl, explicit_scope, 0);
12141 : : else
12142 : 56374340 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
12143 : :
12144 : 56378854 : if (declspecs->storage_class == sc_extern)
12145 : : {
12146 : 559905 : DECL_THIS_EXTERN (decl) = 1;
12147 : 559905 : DECL_EXTERNAL (decl) = !initialized;
12148 : : }
12149 : :
12150 : 56378854 : if (DECL_CLASS_SCOPE_P (decl))
12151 : : {
12152 : 387992 : set_linkage_for_static_data_member (decl);
12153 : : /* This function is only called with out-of-class definitions. */
12154 : 387992 : DECL_EXTERNAL (decl) = 0;
12155 : 387992 : check_class_member_definition_namespace (decl);
12156 : : }
12157 : : /* At top level, either `static' or no s.c. makes a definition
12158 : : (perhaps tentative), and absence of `static' makes it public. */
12159 : 55990862 : else if (toplevel_bindings_p ())
12160 : : {
12161 : 9730434 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
12162 : 4865217 : && (DECL_THIS_EXTERN (decl)
12163 : 4267811 : || ! constp
12164 : 4267811 : || volatilep
12165 : 4193699 : || inlinep
12166 : 307677 : || in_template_context
12167 : 27636 : || processing_specialization
12168 : 27545 : || module_attach_p ()));
12169 : 4865217 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
12170 : : }
12171 : : /* Not at top level, only `static' makes a static definition. */
12172 : : else
12173 : : {
12174 : 51125645 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
12175 : 51125645 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
12176 : : }
12177 : :
12178 : 56378854 : set_originating_module (decl);
12179 : :
12180 : 56378854 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
12181 : : {
12182 : 20636 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
12183 : : {
12184 : 20628 : CP_DECL_THREAD_LOCAL_P (decl) = true;
12185 : 20628 : if (!processing_template_decl)
12186 : 20515 : set_decl_tls_model (decl, decl_default_tls_model (decl));
12187 : : }
12188 : 20636 : if (declspecs->gnu_thread_keyword_p)
12189 : 19665 : SET_DECL_GNU_TLS_P (decl);
12190 : : }
12191 : :
12192 : : /* If the type of the decl has no linkage, make sure that we'll
12193 : : notice that in mark_used. */
12194 : 56378854 : if (cxx_dialect > cxx98
12195 : 56123725 : && decl_linkage (decl) != lk_none
12196 : 55562164 : && DECL_LANG_SPECIFIC (decl) == NULL
12197 : 52112838 : && !DECL_EXTERN_C_P (decl)
12198 : 108491692 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
12199 : 3396 : retrofit_lang_decl (decl);
12200 : :
12201 : 56378854 : if (TREE_PUBLIC (decl))
12202 : : {
12203 : : /* [basic.link]: A name with no linkage (notably, the name of a class
12204 : : or enumeration declared in a local scope) shall not be used to
12205 : : declare an entity with linkage.
12206 : :
12207 : : DR 757 relaxes this restriction for C++0x. */
12208 : 5188166 : if (cxx_dialect < cxx11)
12209 : 21291 : no_linkage_error (decl);
12210 : : }
12211 : : else
12212 : 51190688 : DECL_INTERFACE_KNOWN (decl) = 1;
12213 : :
12214 : 56378854 : if (DECL_NAME (decl)
12215 : 56378854 : && MAIN_NAME_P (DECL_NAME (decl)))
12216 : : {
12217 : 12 : if (scope == global_namespace)
12218 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12219 : : "cannot declare %<::main%> to be a global variable");
12220 : 6 : else if (DECL_EXTERN_C_P (decl))
12221 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
12222 : : "an entity named %<main%> cannot be declared with "
12223 : : "C language linkage");
12224 : : }
12225 : :
12226 : : /* Check if a variable is being declared as a concept. */
12227 : 56378854 : if (conceptp)
12228 : : {
12229 : 27 : if (!processing_template_decl)
12230 : 11 : error_at (declspecs->locations[ds_concept],
12231 : : "a non-template variable cannot be %<concept%>");
12232 : 16 : else if (!at_namespace_scope_p ())
12233 : 1 : error_at (declspecs->locations[ds_concept],
12234 : : "concept must be defined at namespace scope");
12235 : : else
12236 : 15 : error_at (declspecs->locations[ds_concept],
12237 : : "variable concepts are no longer supported");
12238 : 27 : return NULL_TREE;
12239 : : }
12240 : 56378827 : else if (flag_concepts
12241 : 56378827 : && current_template_depth > template_class_depth (scope))
12242 : : {
12243 : 15686557 : tree ci = current_template_constraints ();
12244 : 15686557 : set_constraints (decl, ci);
12245 : : }
12246 : :
12247 : : // Handle explicit specializations and instantiations of variable templates.
12248 : 56378827 : if (orig_declarator)
12249 : 55809586 : decl = check_explicit_specialization (orig_declarator, decl,
12250 : : template_count, 0);
12251 : :
12252 : 56378827 : return decl != error_mark_node ? decl : NULL_TREE;
12253 : : }
12254 : :
12255 : : /* Create and return a canonical pointer to member function type, for
12256 : : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
12257 : :
12258 : : tree
12259 : 3609837 : build_ptrmemfunc_type (tree type)
12260 : : {
12261 : 3609837 : tree field, fields;
12262 : 3609837 : tree t;
12263 : :
12264 : 3609837 : if (type == error_mark_node)
12265 : : return type;
12266 : :
12267 : : /* Make sure that we always have the unqualified pointer-to-member
12268 : : type first. */
12269 : 3609837 : if (cp_cv_quals quals = cp_type_quals (type))
12270 : : {
12271 : 0 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
12272 : 0 : return cp_build_qualified_type (unqual, quals);
12273 : : }
12274 : :
12275 : : /* If a canonical type already exists for this type, use it. We use
12276 : : this method instead of type_hash_canon, because it only does a
12277 : : simple equality check on the list of field members. */
12278 : :
12279 : 3609837 : t = TYPE_PTRMEMFUNC_TYPE (type);
12280 : 3609837 : if (t)
12281 : : return t;
12282 : :
12283 : 1145625 : t = make_node (RECORD_TYPE);
12284 : :
12285 : : /* Let the front end know this is a pointer to member function. */
12286 : 1145625 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
12287 : :
12288 : 1145625 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
12289 : 1145625 : DECL_NONADDRESSABLE_P (field) = 1;
12290 : 1145625 : fields = field;
12291 : :
12292 : 1145625 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
12293 : : delta_type_node);
12294 : 1145625 : DECL_NONADDRESSABLE_P (field) = 1;
12295 : 1145625 : DECL_CHAIN (field) = fields;
12296 : 1145625 : fields = field;
12297 : :
12298 : 1145625 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
12299 : :
12300 : : /* Zap out the name so that the back end will give us the debugging
12301 : : information for this anonymous RECORD_TYPE. */
12302 : 1145625 : TYPE_NAME (t) = NULL_TREE;
12303 : :
12304 : : /* Cache this pointer-to-member type so that we can find it again
12305 : : later. */
12306 : 1145625 : TYPE_PTRMEMFUNC_TYPE (type) = t;
12307 : :
12308 : 1145625 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
12309 : 623808 : SET_TYPE_STRUCTURAL_EQUALITY (t);
12310 : 521817 : else if (TYPE_CANONICAL (type) != type)
12311 : 337849 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
12312 : :
12313 : : return t;
12314 : : }
12315 : :
12316 : : /* Create and return a pointer to data member type. */
12317 : :
12318 : : tree
12319 : 316696 : build_ptrmem_type (tree class_type, tree member_type)
12320 : : {
12321 : 316696 : if (TREE_CODE (member_type) == METHOD_TYPE)
12322 : : {
12323 : 62302 : cp_cv_quals quals = type_memfn_quals (member_type);
12324 : 62302 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
12325 : 62302 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
12326 : 62302 : return build_ptrmemfunc_type (build_pointer_type (member_type));
12327 : : }
12328 : : else
12329 : : {
12330 : 254394 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
12331 : 254394 : return build_offset_type (class_type, member_type);
12332 : : }
12333 : : }
12334 : :
12335 : : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
12336 : : Check to see that the definition is valid. Issue appropriate error
12337 : : messages. */
12338 : :
12339 : : static void
12340 : 13700729 : check_static_variable_definition (tree decl, tree type)
12341 : : {
12342 : : /* Avoid redundant diagnostics on out-of-class definitions. */
12343 : 13700729 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
12344 : : ;
12345 : : /* Can't check yet if we don't know the type. */
12346 : 12803233 : else if (dependent_type_p (type))
12347 : : ;
12348 : : /* If DECL is declared constexpr, we'll do the appropriate checks
12349 : : in check_initializer. Similarly for inline static data members. */
12350 : 12519655 : else if (DECL_P (decl)
12351 : 12519655 : && (DECL_DECLARED_CONSTEXPR_P (decl)
12352 : 2295787 : || DECL_VAR_DECLARED_INLINE_P (decl)))
12353 : : ;
12354 : 2295244 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12355 : : {
12356 : 56 : if (!COMPLETE_TYPE_P (type))
12357 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12358 : : "in-class initialization of static data member %q#D of "
12359 : : "incomplete type", decl);
12360 : 45 : else if (literal_type_p (type))
12361 : 39 : permerror (DECL_SOURCE_LOCATION (decl),
12362 : : "%<constexpr%> needed for in-class initialization of "
12363 : : "static data member %q#D of non-integral type", decl);
12364 : : else
12365 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12366 : : "in-class initialization of static data member %q#D of "
12367 : : "non-literal type", decl);
12368 : : }
12369 : : /* Motion 10 at San Diego: If a static const integral data member is
12370 : : initialized with an integral constant expression, the initializer
12371 : : may appear either in the declaration (within the class), or in
12372 : : the definition, but not both. If it appears in the class, the
12373 : : member is a member constant. The file-scope definition is always
12374 : : required. */
12375 : 2295188 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
12376 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
12377 : : "invalid in-class initialization of static data member "
12378 : : "of non-integral type %qT",
12379 : : type);
12380 : 2295177 : else if (!CP_TYPE_CONST_P (type))
12381 : 5 : error_at (DECL_SOURCE_LOCATION (decl),
12382 : : "ISO C++ forbids in-class initialization of non-const "
12383 : : "static member %qD",
12384 : : decl);
12385 : 2295172 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
12386 : 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
12387 : : "ISO C++ forbids initialization of member constant "
12388 : : "%qD of non-integral type %qT", decl, type);
12389 : 13700729 : }
12390 : :
12391 : : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
12392 : : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
12393 : : expressions out into temporary variables so that walk_tree doesn't
12394 : : step into them (c++/15764). */
12395 : :
12396 : : static tree
12397 : 3160 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
12398 : : {
12399 : 3160 : hash_set<tree> *pset = (hash_set<tree> *)data;
12400 : 3160 : tree expr = *expr_p;
12401 : 3160 : if (TREE_CODE (expr) == SAVE_EXPR)
12402 : : {
12403 : 1361 : tree op = TREE_OPERAND (expr, 0);
12404 : 1361 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
12405 : 1361 : if (TREE_SIDE_EFFECTS (op))
12406 : 56 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
12407 : 1361 : *walk_subtrees = 0;
12408 : : }
12409 : 1799 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
12410 : 1624 : *walk_subtrees = 0;
12411 : 3160 : return NULL;
12412 : : }
12413 : :
12414 : : /* Entry point for the above. */
12415 : :
12416 : : static void
12417 : 1420 : stabilize_vla_size (tree size)
12418 : : {
12419 : 1420 : hash_set<tree> pset;
12420 : : /* Break out any function calls into temporary variables. */
12421 : 1420 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
12422 : 1420 : }
12423 : :
12424 : : /* Reduce a SIZEOF_EXPR to its value. */
12425 : :
12426 : : tree
12427 : 9389707 : fold_sizeof_expr (tree t)
12428 : : {
12429 : 9389707 : tree r;
12430 : 9389707 : if (SIZEOF_EXPR_TYPE_P (t))
12431 : 8680030 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12432 : 8680030 : TREE_TYPE (TREE_OPERAND (t, 0)),
12433 : : SIZEOF_EXPR, false, false);
12434 : 709677 : else if (TYPE_P (TREE_OPERAND (t, 0)))
12435 : 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
12436 : 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12437 : : false, false);
12438 : : else
12439 : 709677 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
12440 : 709677 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
12441 : : false, false);
12442 : 9389707 : if (r == error_mark_node)
12443 : 63 : r = size_one_node;
12444 : 9389707 : r = cp_fold_convert (size_type_node, r);
12445 : 9389707 : return r;
12446 : : }
12447 : :
12448 : : /* Given the SIZE (i.e., number of elements) in an array, compute
12449 : : an appropriate index type for the array. If non-NULL, NAME is
12450 : : the name of the entity being declared. */
12451 : :
12452 : : static tree
12453 : 2729882 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
12454 : : tsubst_flags_t complain)
12455 : : {
12456 : 2729882 : if (error_operand_p (size))
12457 : 37 : return error_mark_node;
12458 : :
12459 : : /* The type of the index being computed. */
12460 : 2729845 : tree itype;
12461 : :
12462 : : /* The original numeric size as seen in the source code before
12463 : : conversion to size_t. */
12464 : 2729845 : tree origsize = size;
12465 : :
12466 : 2729845 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
12467 : :
12468 : 2729845 : if (!type_dependent_expression_p (size))
12469 : : {
12470 : 2690746 : origsize = size = mark_rvalue_use (size);
12471 : :
12472 : 34929 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
12473 : 2690902 : && TREE_SIDE_EFFECTS (size))
12474 : : /* In C++98, we mark a non-constant array bound with a magic
12475 : : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
12476 : : else
12477 : : {
12478 : 2690745 : size = build_converted_constant_expr (size_type_node, size, complain);
12479 : : /* Pedantically a constant expression is required here and so
12480 : : __builtin_is_constant_evaluated () should fold to true if it
12481 : : is successfully folded into a constant. */
12482 : 2690745 : size = fold_non_dependent_expr (size, complain,
12483 : : /*manifestly_const_eval=*/true);
12484 : :
12485 : 2690745 : if (!TREE_CONSTANT (size))
12486 : 102227 : size = origsize;
12487 : : }
12488 : :
12489 : 2690746 : if (error_operand_p (size))
12490 : 3 : return error_mark_node;
12491 : :
12492 : : /* The array bound must be an integer type. */
12493 : 2690743 : tree type = TREE_TYPE (size);
12494 : 2690743 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
12495 : : {
12496 : 21 : if (!(complain & tf_error))
12497 : 0 : return error_mark_node;
12498 : 21 : if (name)
12499 : 18 : error_at (loc, "size of array %qD has non-integral type %qT",
12500 : : name, type);
12501 : : else
12502 : 3 : error_at (loc, "size of array has non-integral type %qT", type);
12503 : 21 : size = integer_one_node;
12504 : : }
12505 : : }
12506 : :
12507 : : /* A type is dependent if it is...an array type constructed from any
12508 : : dependent type or whose size is specified by a constant expression
12509 : : that is value-dependent. */
12510 : : /* We can only call value_dependent_expression_p on integral constant
12511 : : expressions. */
12512 : 2729842 : if (processing_template_decl
12513 : 1147720 : && potential_constant_expression (size)
12514 : 3877537 : && value_dependent_expression_p (size))
12515 : : {
12516 : : /* Just build the index type and mark that it requires
12517 : : structural equality checks. */
12518 : 655332 : in_template:
12519 : 655332 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
12520 : : size, size_one_node));
12521 : 655332 : TYPE_DEPENDENT_P (itype) = 1;
12522 : 655332 : TYPE_DEPENDENT_P_VALID (itype) = 1;
12523 : 655332 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
12524 : 655332 : return itype;
12525 : : }
12526 : :
12527 : 2074619 : if (TREE_CODE (size) != INTEGER_CST)
12528 : : {
12529 : 1917 : tree folded = cp_fully_fold (size);
12530 : 1917 : if (TREE_CODE (folded) == INTEGER_CST)
12531 : : {
12532 : 199 : if (name)
12533 : 125 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
12534 : : "integral constant-expression", name);
12535 : : else
12536 : 74 : pedwarn (loc, OPT_Wpedantic,
12537 : : "size of array is not an integral constant-expression");
12538 : : }
12539 : 1917 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
12540 : : /* We might have lost the TREE_CONSTANT flag e.g. when we are
12541 : : folding a conversion from a pointer to integral type. In that
12542 : : case issue an error below and don't treat this as a VLA. */;
12543 : : else
12544 : : /* Use the folded result for VLAs, too; it will have resolved
12545 : : SIZEOF_EXPR. */
12546 : : size = folded;
12547 : : }
12548 : :
12549 : : /* Normally, the array-bound will be a constant. */
12550 : 2074619 : if (TREE_CODE (size) == INTEGER_CST)
12551 : : {
12552 : : /* The size to use in diagnostics that reflects the constant
12553 : : size used in the source, rather than SIZE massaged above. */
12554 : 2072901 : tree diagsize = size;
12555 : :
12556 : : /* If the original size before conversion to size_t was signed
12557 : : and negative, convert it to ssizetype to restore the sign. */
12558 : 2072901 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
12559 : : && TREE_CODE (size) == INTEGER_CST
12560 : 2072901 : && tree_int_cst_sign_bit (size))
12561 : : {
12562 : 218 : diagsize = fold_convert (ssizetype, size);
12563 : :
12564 : : /* Clear the overflow bit that may have been set as a result
12565 : : of the conversion from the sizetype of the new size to
12566 : : ssizetype. */
12567 : 218 : TREE_OVERFLOW (diagsize) = false;
12568 : : }
12569 : :
12570 : : /* Verify that the array has a positive number of elements
12571 : : and issue the appropriate diagnostic if it doesn't. */
12572 : 2072901 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
12573 : : {
12574 : 449 : if (!(complain & tf_error))
12575 : 15 : return error_mark_node;
12576 : 434 : size = integer_one_node;
12577 : : }
12578 : : /* As an extension we allow zero-sized arrays. */
12579 : 2072452 : else if (integer_zerop (size))
12580 : : {
12581 : 3166 : if (!(complain & tf_error))
12582 : : /* We must fail if performing argument deduction (as
12583 : : indicated by the state of complain), so that
12584 : : another substitution can be found. */
12585 : 8 : return error_mark_node;
12586 : 3158 : else if (name)
12587 : 2891 : pedwarn (loc, OPT_Wpedantic,
12588 : : "ISO C++ forbids zero-size array %qD", name);
12589 : : else
12590 : 267 : pedwarn (loc, OPT_Wpedantic,
12591 : : "ISO C++ forbids zero-size array");
12592 : : }
12593 : : }
12594 : 1718 : else if (TREE_CONSTANT (size)
12595 : : /* We don't allow VLAs at non-function scopes, or during
12596 : : tentative template substitution. */
12597 : 1703 : || !at_function_scope_p ()
12598 : 3253 : || !(complain & tf_error))
12599 : : {
12600 : 189 : if (!(complain & tf_error))
12601 : 144 : return error_mark_node;
12602 : : /* `(int) &fn' is not a valid array bound. */
12603 : 45 : if (name)
12604 : 37 : error_at (loc,
12605 : : "size of array %qD is not an integral constant-expression",
12606 : : name);
12607 : : else
12608 : 8 : error_at (loc, "size of array is not an integral constant-expression");
12609 : 45 : size = integer_one_node;
12610 : : }
12611 : 1529 : else if (pedantic && warn_vla != 0)
12612 : : {
12613 : 30 : if (name)
12614 : 15 : pedwarn (name_loc, OPT_Wvla,
12615 : : "ISO C++ forbids variable length array %qD", name);
12616 : : else
12617 : 15 : pedwarn (input_location, OPT_Wvla,
12618 : : "ISO C++ forbids variable length array");
12619 : : }
12620 : 1499 : else if (warn_vla > 0)
12621 : : {
12622 : 3 : if (name)
12623 : 3 : warning_at (name_loc, OPT_Wvla,
12624 : : "variable length array %qD is used", name);
12625 : : else
12626 : 0 : warning (OPT_Wvla,
12627 : : "variable length array is used");
12628 : : }
12629 : :
12630 : 2074452 : if (processing_template_decl && !TREE_CONSTANT (size))
12631 : 109 : goto in_template;
12632 : : else
12633 : : {
12634 : 2074343 : if (!TREE_CONSTANT (size))
12635 : : {
12636 : : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
12637 : : of the MINUS_EXPR, which allows the -1 to get folded with the +1
12638 : : that happens when building TYPE_SIZE. */
12639 : 1420 : size = variable_size (size);
12640 : 1420 : stabilize_vla_size (size);
12641 : : }
12642 : :
12643 : : /* Compute the index of the largest element in the array. It is
12644 : : one less than the number of elements in the array. We save
12645 : : and restore PROCESSING_TEMPLATE_DECL so that computations in
12646 : : cp_build_binary_op will be appropriately folded. */
12647 : 2074343 : {
12648 : 2074343 : processing_template_decl_sentinel s;
12649 : 2074343 : itype = cp_build_binary_op (input_location,
12650 : : MINUS_EXPR,
12651 : : cp_convert (ssizetype, size, complain),
12652 : : cp_convert (ssizetype, integer_one_node,
12653 : : complain),
12654 : : complain);
12655 : 2074343 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
12656 : 2074343 : }
12657 : :
12658 : 2074343 : if (!TREE_CONSTANT (itype))
12659 : : {
12660 : 1420 : if (sanitize_flags_p (SANITIZE_VLA)
12661 : 1420 : && current_function_decl != NULL_TREE)
12662 : : {
12663 : : /* We have to add 1 -- in the ubsan routine we generate
12664 : : LE_EXPR rather than LT_EXPR. */
12665 : 69 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
12666 : : build_one_cst (TREE_TYPE (itype)));
12667 : 69 : t = ubsan_instrument_vla (input_location, t);
12668 : 69 : finish_expr_stmt (t);
12669 : : }
12670 : : }
12671 : : /* Make sure that there was no overflow when creating to a signed
12672 : : index type. (For example, on a 32-bit machine, an array with
12673 : : size 2^32 - 1 is too big.) */
12674 : 2072923 : else if (TREE_CODE (itype) == INTEGER_CST
12675 : 2072923 : && TREE_OVERFLOW (itype))
12676 : : {
12677 : 0 : if (!(complain & tf_error))
12678 : 0 : return error_mark_node;
12679 : 0 : error ("overflow in array dimension");
12680 : 0 : TREE_OVERFLOW (itype) = 0;
12681 : : }
12682 : : }
12683 : :
12684 : : /* Create and return the appropriate index type. */
12685 : 2074343 : itype = build_index_type (itype);
12686 : :
12687 : : /* If the index type were dependent, we would have returned early, so
12688 : : remember that it isn't. */
12689 : 2074343 : TYPE_DEPENDENT_P (itype) = 0;
12690 : 2074343 : TYPE_DEPENDENT_P_VALID (itype) = 1;
12691 : 2074343 : return itype;
12692 : : }
12693 : :
12694 : : tree
12695 : 322303 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
12696 : : {
12697 : 322303 : return compute_array_index_type_loc (input_location, name, size, complain);
12698 : : }
12699 : :
12700 : : /* Returns the scope (if any) in which the entity declared by
12701 : : DECLARATOR will be located. If the entity was declared with an
12702 : : unqualified name, NULL_TREE is returned. */
12703 : :
12704 : : tree
12705 : 685672859 : get_scope_of_declarator (const cp_declarator *declarator)
12706 : : {
12707 : 800439893 : while (declarator && declarator->kind != cdk_id)
12708 : 114767034 : declarator = declarator->declarator;
12709 : :
12710 : : /* If the declarator-id is a SCOPE_REF, the scope in which the
12711 : : declaration occurs is the first operand. */
12712 : 685672859 : if (declarator
12713 : 684378498 : && declarator->u.id.qualifying_scope)
12714 : : return declarator->u.id.qualifying_scope;
12715 : :
12716 : : /* Otherwise, the declarator is not a qualified name; the entity will
12717 : : be declared in the current scope. */
12718 : : return NULL_TREE;
12719 : : }
12720 : :
12721 : : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
12722 : : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
12723 : : with this type. */
12724 : :
12725 : : static tree
12726 : 3135000 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
12727 : : {
12728 : 3135000 : tree itype = NULL_TREE;
12729 : :
12730 : : /* If things have already gone awry, bail now. */
12731 : 3135000 : if (type == error_mark_node || size == error_mark_node)
12732 : : return error_mark_node;
12733 : :
12734 : : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
12735 : : for a deduced class type. */
12736 : 3134937 : if (template_placeholder_p (type))
12737 : : {
12738 : 6 : if (name)
12739 : 0 : error_at (loc, "%qD declared as array of template placeholder "
12740 : : "type %qT", name, type);
12741 : : else
12742 : 6 : error ("creating array of template placeholder type %qT", type);
12743 : 6 : return error_mark_node;
12744 : : }
12745 : :
12746 : : /* If there are some types which cannot be array elements,
12747 : : issue an error-message and return. */
12748 : 3134931 : switch (TREE_CODE (type))
12749 : : {
12750 : 3 : case VOID_TYPE:
12751 : 3 : if (name)
12752 : 0 : error_at (loc, "declaration of %qD as array of void", name);
12753 : : else
12754 : 3 : error ("creating array of void");
12755 : 3 : return error_mark_node;
12756 : :
12757 : 6 : case FUNCTION_TYPE:
12758 : 6 : if (name)
12759 : 6 : error_at (loc, "declaration of %qD as array of functions", name);
12760 : : else
12761 : 0 : error ("creating array of functions");
12762 : 6 : return error_mark_node;
12763 : :
12764 : 21 : case REFERENCE_TYPE:
12765 : 21 : if (name)
12766 : 15 : error_at (loc, "declaration of %qD as array of references", name);
12767 : : else
12768 : 6 : error ("creating array of references");
12769 : 21 : return error_mark_node;
12770 : :
12771 : 0 : case METHOD_TYPE:
12772 : 0 : if (name)
12773 : 0 : error_at (loc, "declaration of %qD as array of function members",
12774 : : name);
12775 : : else
12776 : 0 : error ("creating array of function members");
12777 : 0 : return error_mark_node;
12778 : :
12779 : 3134901 : default:
12780 : 3134901 : break;
12781 : : }
12782 : :
12783 : 3134901 : if (!verify_type_context (name ? loc : input_location,
12784 : : TCTX_ARRAY_ELEMENT, type))
12785 : 0 : return error_mark_node;
12786 : :
12787 : : /* [dcl.array]
12788 : :
12789 : : The constant expressions that specify the bounds of the arrays
12790 : : can be omitted only for the first member of the sequence. */
12791 : 3134901 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12792 : : {
12793 : 30 : if (name)
12794 : 24 : error_at (loc, "declaration of %qD as multidimensional array must "
12795 : : "have bounds for all dimensions except the first",
12796 : : name);
12797 : : else
12798 : 6 : error ("multidimensional array must have bounds for all "
12799 : : "dimensions except the first");
12800 : :
12801 : 30 : return error_mark_node;
12802 : : }
12803 : :
12804 : : /* Figure out the index type for the array. */
12805 : 3134871 : if (size)
12806 : : {
12807 : 2407579 : itype = compute_array_index_type_loc (loc, name, size,
12808 : : tf_warning_or_error);
12809 : 2407579 : if (type_uses_auto (type)
12810 : 2407579 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
12811 : : {
12812 : 9 : sorry_at (loc, "variable-length array of %<auto%>");
12813 : 9 : return error_mark_node;
12814 : : }
12815 : : }
12816 : :
12817 : 3134862 : return build_cplus_array_type (type, itype);
12818 : : }
12819 : :
12820 : : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
12821 : :
12822 : : static location_t
12823 : 921291171 : min_location (location_t loca, location_t locb)
12824 : : {
12825 : 921291171 : if (loca == UNKNOWN_LOCATION
12826 : 921291171 : || (locb != UNKNOWN_LOCATION
12827 : 101945521 : && linemap_location_before_p (line_table, locb, loca)))
12828 : 820787449 : return locb;
12829 : : return loca;
12830 : : }
12831 : :
12832 : : /* Returns the smallest location != UNKNOWN_LOCATION among the
12833 : : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
12834 : : and LOCATIONS[ds_restrict]. */
12835 : :
12836 : : static location_t
12837 : 919711672 : smallest_type_quals_location (int type_quals, const location_t* locations)
12838 : : {
12839 : 919711672 : location_t loc = UNKNOWN_LOCATION;
12840 : :
12841 : 919711672 : if (type_quals & TYPE_QUAL_CONST)
12842 : 100551911 : loc = locations[ds_const];
12843 : :
12844 : 919711672 : if (type_quals & TYPE_QUAL_VOLATILE)
12845 : 1579408 : loc = min_location (loc, locations[ds_volatile]);
12846 : :
12847 : 919711672 : if (type_quals & TYPE_QUAL_RESTRICT)
12848 : 43 : loc = min_location (loc, locations[ds_restrict]);
12849 : :
12850 : 919711672 : return loc;
12851 : : }
12852 : :
12853 : : /* Returns the smallest among the latter and locations[ds_type_spec]. */
12854 : :
12855 : : static location_t
12856 : 919711651 : smallest_type_location (int type_quals, const location_t* locations)
12857 : : {
12858 : 919711651 : location_t loc = smallest_type_quals_location (type_quals, locations);
12859 : 919711651 : return min_location (loc, locations[ds_type_spec]);
12860 : : }
12861 : :
12862 : : static location_t
12863 : 26 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
12864 : : {
12865 : 26 : int type_quals = get_type_quals (declspecs);
12866 : 26 : return smallest_type_location (type_quals, declspecs->locations);
12867 : : }
12868 : :
12869 : : /* Returns whether DECLARATOR represented a pointer or a reference and if so,
12870 : : strip out the pointer/reference declarator(s). */
12871 : :
12872 : : static bool
12873 : 27165715 : maybe_strip_indirect_ref (const cp_declarator** declarator)
12874 : : {
12875 : 27165715 : bool indirect_ref_p = false;
12876 : 54331577 : while (declarator && *declarator
12877 : 27165862 : && ((*declarator)->kind == cdk_pointer
12878 : 27165862 : || (*declarator)->kind == cdk_reference))
12879 : : {
12880 : 147 : indirect_ref_p = true;
12881 : 147 : *declarator = (*declarator)->declarator;
12882 : : }
12883 : 27165715 : return indirect_ref_p;
12884 : : }
12885 : :
12886 : : /* Check that it's OK to declare a function with the indicated TYPE, TYPE_QUALS
12887 : : and DECLARATOR. SFK indicates the kind of special function (if any) that
12888 : : this function is. OPTYPE is the type given in a conversion operator
12889 : : declaration, or the class type for a constructor/destructor.
12890 : : Returns the actual return type of the function; that may be different
12891 : : than TYPE if an error occurs, or for certain special functions. */
12892 : :
12893 : : static tree
12894 : 27165715 : check_special_function_return_type (special_function_kind sfk,
12895 : : tree type,
12896 : : tree optype,
12897 : : int type_quals,
12898 : : const cp_declarator** declarator,
12899 : : const location_t* locations)
12900 : : {
12901 : 27165715 : gcc_assert (declarator);
12902 : 27165715 : location_t rettype_loc = (type
12903 : 27165715 : ? smallest_type_location (type_quals, locations)
12904 : 27165634 : : (*declarator)->id_loc);
12905 : 27165715 : switch (sfk)
12906 : : {
12907 : 22314164 : case sfk_constructor:
12908 : 22314164 : if (maybe_strip_indirect_ref (declarator) || type)
12909 : 81 : error_at (rettype_loc,
12910 : : "return type specification for constructor invalid");
12911 : 22314083 : else if (type_quals != TYPE_UNQUALIFIED)
12912 : 15 : error_at (smallest_type_quals_location (type_quals, locations),
12913 : : "qualifiers are not allowed on constructor declaration");
12914 : :
12915 : 22314164 : if (targetm.cxx.cdtor_returns_this ())
12916 : 0 : type = build_pointer_type (optype);
12917 : : else
12918 : 22314164 : type = void_type_node;
12919 : : break;
12920 : :
12921 : 3146802 : case sfk_destructor:
12922 : 3146802 : if (maybe_strip_indirect_ref (declarator) || type)
12923 : 45 : error_at (rettype_loc,
12924 : : "return type specification for destructor invalid");
12925 : 3146757 : else if (type_quals != TYPE_UNQUALIFIED)
12926 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
12927 : : "qualifiers are not allowed on destructor declaration");
12928 : :
12929 : : /* We can't use the proper return type here because we run into
12930 : : problems with ambiguous bases and covariant returns. */
12931 : 3146802 : if (targetm.cxx.cdtor_returns_this ())
12932 : 0 : type = build_pointer_type (void_type_node);
12933 : : else
12934 : 3146802 : type = void_type_node;
12935 : : break;
12936 : :
12937 : 635478 : case sfk_conversion:
12938 : 635478 : if (maybe_strip_indirect_ref (declarator) || type)
12939 : 42 : error_at (rettype_loc,
12940 : : "return type specified for %<operator %T%>", optype);
12941 : 635436 : else if (type_quals != TYPE_UNQUALIFIED)
12942 : 3 : error_at (smallest_type_quals_location (type_quals, locations),
12943 : : "qualifiers are not allowed on declaration of "
12944 : : "%<operator %T%>", optype);
12945 : :
12946 : : type = optype;
12947 : : break;
12948 : :
12949 : 1069271 : case sfk_deduction_guide:
12950 : 1069271 : if (maybe_strip_indirect_ref (declarator) || type)
12951 : 0 : error_at (rettype_loc,
12952 : : "return type specified for deduction guide");
12953 : 1069271 : else if (type_quals != TYPE_UNQUALIFIED)
12954 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
12955 : : "qualifiers are not allowed on declaration of "
12956 : : "deduction guide");
12957 : 1069271 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
12958 : : {
12959 : 3 : error ("template template parameter %qT in declaration of "
12960 : : "deduction guide", optype);
12961 : 3 : type = error_mark_node;
12962 : : }
12963 : : else
12964 : 1069268 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
12965 : 28870317 : for (int i = 0; i < ds_last; ++i)
12966 : 27801046 : if (i != ds_explicit && locations[i])
12967 : 3 : error_at (locations[i],
12968 : : "%<decl-specifier%> in declaration of deduction guide");
12969 : : break;
12970 : :
12971 : 0 : default:
12972 : 0 : gcc_unreachable ();
12973 : : }
12974 : :
12975 : 27165715 : return type;
12976 : : }
12977 : :
12978 : : /* A variable or data member (whose unqualified name is IDENTIFIER)
12979 : : has been declared with the indicated TYPE. If the TYPE is not
12980 : : acceptable, issue an error message and return a type to use for
12981 : : error-recovery purposes. */
12982 : :
12983 : : tree
12984 : 286187081 : check_var_type (tree identifier, tree type, location_t loc)
12985 : : {
12986 : 286187081 : if (VOID_TYPE_P (type))
12987 : : {
12988 : 88 : if (!identifier)
12989 : 0 : error_at (loc, "unnamed variable or field declared void");
12990 : 88 : else if (identifier_p (identifier))
12991 : : {
12992 : 88 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
12993 : 88 : error_at (loc, "variable or field %qE declared void",
12994 : : identifier);
12995 : : }
12996 : : else
12997 : 0 : error_at (loc, "variable or field declared void");
12998 : 88 : type = error_mark_node;
12999 : : }
13000 : :
13001 : 286187081 : return type;
13002 : : }
13003 : :
13004 : : /* Handle declaring DECL as an inline variable. */
13005 : :
13006 : : static void
13007 : 3942401 : mark_inline_variable (tree decl, location_t loc)
13008 : : {
13009 : 3942401 : bool inlinep = true;
13010 : 3942401 : if (! toplevel_bindings_p ())
13011 : : {
13012 : 24 : error_at (loc, "%<inline%> specifier invalid for variable "
13013 : : "%qD declared at block scope", decl);
13014 : 24 : inlinep = false;
13015 : : }
13016 : 3942377 : else if (cxx_dialect < cxx17)
13017 : 690 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
13018 : : "with %<-std=c++17%> or %<-std=gnu++17%>");
13019 : 714 : if (inlinep)
13020 : : {
13021 : 3942377 : retrofit_lang_decl (decl);
13022 : 3942377 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
13023 : : }
13024 : 3942401 : }
13025 : :
13026 : :
13027 : : /* Diagnose -Wnon-c-typedef-for-linkage pedwarn. TYPE is the unnamed class
13028 : : with a typedef name for linkage purposes with freshly updated TYPE_NAME,
13029 : : ORIG is the anonymous TYPE_NAME before that change. */
13030 : :
13031 : : static bool
13032 : 42 : diagnose_non_c_class_typedef_for_linkage (tree type, tree orig)
13033 : : {
13034 : 42 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (orig));
13035 : 42 : tree name = DECL_NAME (TYPE_NAME (type));
13036 : 42 : richloc.add_fixit_insert_before (IDENTIFIER_POINTER (name));
13037 : 42 : return pedwarn (&richloc, OPT_Wnon_c_typedef_for_linkage,
13038 : : "anonymous non-C-compatible type given name for linkage "
13039 : 42 : "purposes by %<typedef%> declaration");
13040 : 42 : }
13041 : :
13042 : : /* Diagnose -Wnon-c-typedef-for-linkage violations on T. TYPE and ORIG
13043 : : like for diagnose_non_c_class_typedef_for_linkage, T is initially equal
13044 : : to TYPE but during recursion can be set to nested classes. */
13045 : :
13046 : : static bool
13047 : 516614 : maybe_diagnose_non_c_class_typedef_for_linkage (tree type, tree orig, tree t)
13048 : : {
13049 : 516614 : if (!BINFO_BASE_BINFOS (TYPE_BINFO (t))->is_empty ())
13050 : : {
13051 : 6 : auto_diagnostic_group d;
13052 : 6 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13053 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
13054 : : "type is not C-compatible because it has a base class");
13055 : 6 : return true;
13056 : 6 : }
13057 : 2448068 : for (tree field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
13058 : 1931521 : switch (TREE_CODE (field))
13059 : : {
13060 : 9 : case VAR_DECL:
13061 : : /* static data members have been diagnosed already. */
13062 : 9 : continue;
13063 : 1287161 : case FIELD_DECL:
13064 : 1287161 : if (DECL_INITIAL (field))
13065 : : {
13066 : 4 : auto_diagnostic_group d;
13067 : 4 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13068 : 4 : inform (DECL_SOURCE_LOCATION (field),
13069 : : "type is not C-compatible because %qD has default "
13070 : : "member initializer", field);
13071 : 4 : return true;
13072 : 4 : }
13073 : 1287157 : continue;
13074 : 9 : case CONST_DECL:
13075 : 9 : continue;
13076 : 644265 : case TYPE_DECL:
13077 : 644265 : if (DECL_SELF_REFERENCE_P (field))
13078 : 516582 : continue;
13079 : 127683 : if (DECL_IMPLICIT_TYPEDEF_P (field))
13080 : : {
13081 : 127673 : if (TREE_CODE (TREE_TYPE (field)) == ENUMERAL_TYPE)
13082 : 3 : continue;
13083 : 127670 : if (CLASS_TYPE_P (TREE_TYPE (field)))
13084 : : {
13085 : 127670 : tree tf = TREE_TYPE (field);
13086 : 127670 : if (maybe_diagnose_non_c_class_typedef_for_linkage (type, orig,
13087 : : tf))
13088 : : return true;
13089 : 127645 : continue;
13090 : 127645 : }
13091 : : }
13092 : : /* FALLTHRU */
13093 : 32 : case FUNCTION_DECL:
13094 : 32 : case TEMPLATE_DECL:
13095 : 32 : {
13096 : 32 : auto_diagnostic_group d;
13097 : 32 : if (diagnose_non_c_class_typedef_for_linkage (type, orig))
13098 : 32 : inform (DECL_SOURCE_LOCATION (field),
13099 : : "type is not C-compatible because it contains %qD "
13100 : : "declaration", field);
13101 : 32 : return true;
13102 : 32 : }
13103 : : default:
13104 : : break;
13105 : 1287175 : }
13106 : : return false;
13107 : : }
13108 : :
13109 : : /* Assign a typedef-given name to a class or enumeration type declared
13110 : : as anonymous at first. This was split out of grokdeclarator
13111 : : because it is also used in libcc1. */
13112 : :
13113 : : void
13114 : 390574 : name_unnamed_type (tree type, tree decl)
13115 : : {
13116 : 1171722 : gcc_assert (TYPE_UNNAMED_P (type)
13117 : : || enum_with_enumerator_for_linkage_p (type));
13118 : :
13119 : : /* Replace the anonymous decl with the real decl. Be careful not to
13120 : : rename other typedefs (such as the self-reference) of type. */
13121 : 390574 : tree orig = TYPE_NAME (type);
13122 : 1170151 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13123 : 779577 : if (TYPE_NAME (t) == orig)
13124 : 390574 : TYPE_NAME (t) = decl;
13125 : :
13126 : : /* If this is a typedef within a template class, the nested
13127 : : type is a (non-primary) template. The name for the
13128 : : template needs updating as well. */
13129 : 390574 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
13130 : 64 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
13131 : :
13132 : : /* Adjust linkage now that we aren't unnamed anymore. */
13133 : 390574 : reset_type_linkage (type);
13134 : :
13135 : 390574 : if (CLASS_TYPE_P (type) && warn_non_c_typedef_for_linkage)
13136 : 388944 : maybe_diagnose_non_c_class_typedef_for_linkage (type, orig, type);
13137 : :
13138 : : /* FIXME remangle member functions; member functions of a
13139 : : type with external linkage have external linkage. */
13140 : :
13141 : : /* Check that our job is done, and that it would fail if we
13142 : : attempted to do it again. */
13143 : 781148 : gcc_assert (!TYPE_UNNAMED_P (type)
13144 : : && !enum_with_enumerator_for_linkage_p (type));
13145 : 390574 : }
13146 : :
13147 : : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
13148 : : is allowed. TYPE might contain a decltype(auto). Returns true if
13149 : : there was a problem, false otherwise. */
13150 : :
13151 : : static bool
13152 : 580811643 : check_decltype_auto (location_t loc, tree type)
13153 : : {
13154 : 580811643 : if (tree a = type_uses_auto (type))
13155 : : {
13156 : 10065717 : if (AUTO_IS_DECLTYPE (a))
13157 : : {
13158 : 328161 : if (a != type)
13159 : : {
13160 : 33 : error_at (loc, "%qT as type rather than plain "
13161 : : "%<decltype(auto)%>", type);
13162 : 33 : return true;
13163 : : }
13164 : 328128 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
13165 : : {
13166 : 35 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
13167 : 35 : return true;
13168 : : }
13169 : : }
13170 : : }
13171 : : return false;
13172 : : }
13173 : :
13174 : : /* Issue an error about two mutually incompatible declspecs
13175 : : with the given names and locations
13176 : : e.g. "error: `signed' and `unsigned' specified together" */
13177 : :
13178 : : static void
13179 : 66 : complain_about_incompatible_declspecs (const char *name_a, location_t loc_a,
13180 : : const char *name_b, location_t loc_b)
13181 : : {
13182 : 66 : gcc_rich_location richloc (loc_a, nullptr, highlight_colors::lhs);
13183 : 66 : richloc.add_range (loc_b, SHOW_RANGE_WITHOUT_CARET,
13184 : : nullptr, highlight_colors::rhs);
13185 : 66 : pp_element_quoted_string e_name_a (name_a, highlight_colors::lhs);
13186 : 66 : pp_element_quoted_string e_name_b (name_b, highlight_colors::rhs);
13187 : 66 : error_at (&richloc, "%e and %e specified together",
13188 : : &e_name_a, &e_name_b);
13189 : 66 : }
13190 : :
13191 : : /* Given declspecs and a declarator (abstract or otherwise), determine
13192 : : the name and type of the object declared and construct a DECL node
13193 : : for it.
13194 : :
13195 : : DECLSPECS points to the representation of declaration-specifier
13196 : : sequence that precedes declarator.
13197 : :
13198 : : DECL_CONTEXT says which syntactic context this declaration is in:
13199 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
13200 : : FUNCDEF for a function definition. Like NORMAL but a few different
13201 : : error messages in each case. Return value may be zero meaning
13202 : : this definition is too screwy to try to parse.
13203 : : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
13204 : : handle member functions (which have FIELD context).
13205 : : Return value may be zero meaning this definition is too screwy to
13206 : : try to parse.
13207 : : PARM for a parameter declaration (either within a function prototype
13208 : : or before a function body). Make a PARM_DECL, or return void_type_node.
13209 : : TPARM for a template parameter declaration.
13210 : : CATCHPARM for a parameter declaration before a catch clause.
13211 : : TYPENAME if for a typename (in a cast or sizeof).
13212 : : Don't make a DECL node; just return the ..._TYPE node.
13213 : : FIELD for a struct or union field; make a FIELD_DECL.
13214 : : BITFIELD for a field with specified width.
13215 : :
13216 : : INITIALIZED is as for start_decl.
13217 : :
13218 : : ATTRLIST is a pointer to the list of attributes, which may be NULL
13219 : : if there are none; *ATTRLIST may be modified if attributes from inside
13220 : : the declarator should be applied to the declaration.
13221 : :
13222 : : When this function is called, scoping variables (such as
13223 : : CURRENT_CLASS_TYPE) should reflect the scope in which the
13224 : : declaration occurs, not the scope in which the new declaration will
13225 : : be placed. For example, on:
13226 : :
13227 : : void S::f() { ... }
13228 : :
13229 : : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
13230 : : should not be `S'.
13231 : :
13232 : : Returns a DECL (if a declarator is present), a TYPE (if there is no
13233 : : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
13234 : : error occurs. */
13235 : :
13236 : : tree
13237 : 919711544 : grokdeclarator (const cp_declarator *declarator,
13238 : : cp_decl_specifier_seq *declspecs,
13239 : : enum decl_context decl_context,
13240 : : int initialized,
13241 : : tree* attrlist)
13242 : : {
13243 : 919711544 : tree type = NULL_TREE;
13244 : 919711544 : int longlong = 0;
13245 : 919711544 : int explicit_intN = 0;
13246 : 919711544 : int int_n_alt = 0;
13247 : 919711544 : int virtualp, explicitp, friendp, inlinep, staticp;
13248 : 919711544 : int explicit_int = 0;
13249 : 919711544 : int explicit_char = 0;
13250 : 919711544 : int defaulted_int = 0;
13251 : :
13252 : 919711544 : tree typedef_decl = NULL_TREE;
13253 : 919711544 : const char *name = NULL;
13254 : 919711544 : tree typedef_type = NULL_TREE;
13255 : : /* True if this declarator is a function definition. */
13256 : 919711544 : bool funcdef_flag = false;
13257 : 919711544 : cp_declarator_kind innermost_code = cdk_error;
13258 : 919711544 : int bitfield = 0;
13259 : : #if 0
13260 : : /* See the code below that used this. */
13261 : : tree decl_attr = NULL_TREE;
13262 : : #endif
13263 : :
13264 : : /* Keep track of what sort of function is being processed
13265 : : so that we can warn about default return values, or explicit
13266 : : return values which do not match prescribed defaults. */
13267 : 919711544 : special_function_kind sfk = sfk_none;
13268 : :
13269 : 919711544 : tree dname = NULL_TREE;
13270 : 919711544 : tree ctor_return_type = NULL_TREE;
13271 : 919711544 : enum overload_flags flags = NO_SPECIAL;
13272 : : /* cv-qualifiers that apply to the declarator, for a declaration of
13273 : : a member function. */
13274 : 919711544 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
13275 : : /* virt-specifiers that apply to the declarator, for a declaration of
13276 : : a member function. */
13277 : 919711544 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
13278 : : /* ref-qualifier that applies to the declarator, for a declaration of
13279 : : a member function. */
13280 : 919711544 : cp_ref_qualifier rqual = REF_QUAL_NONE;
13281 : : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
13282 : 919711544 : int type_quals = get_type_quals (declspecs);
13283 : 919711544 : tree raises = NULL_TREE;
13284 : 919711544 : int template_count = 0;
13285 : 919711544 : tree returned_attrs = NULL_TREE;
13286 : 919711544 : tree parms = NULL_TREE;
13287 : 919711544 : const cp_declarator *id_declarator;
13288 : : /* The unqualified name of the declarator; either an
13289 : : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
13290 : 919711544 : tree unqualified_id;
13291 : : /* The class type, if any, in which this entity is located,
13292 : : or NULL_TREE if none. Note that this value may be different from
13293 : : the current class type; for example if an attempt is made to declare
13294 : : "A::f" inside "B", this value will be "A". */
13295 : 919711544 : tree ctype = current_class_type;
13296 : : /* The NAMESPACE_DECL for the namespace in which this entity is
13297 : : located. If an unqualified name is used to declare the entity,
13298 : : this value will be NULL_TREE, even if the entity is located at
13299 : : namespace scope. */
13300 : 919711544 : tree in_namespace = NULL_TREE;
13301 : 919711544 : cp_storage_class storage_class;
13302 : 919711544 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
13303 : 919711544 : bool type_was_error_mark_node = false;
13304 : 919711544 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
13305 : 919711544 : bool template_type_arg = false;
13306 : 919711544 : bool template_parm_flag = false;
13307 : 919711544 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
13308 : 919711544 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
13309 : 919711544 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
13310 : 919711544 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
13311 : 919711544 : bool late_return_type_p = false;
13312 : 919711544 : bool array_parameter_p = false;
13313 : 919711544 : tree reqs = NULL_TREE;
13314 : :
13315 : 919711544 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
13316 : 919711544 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
13317 : 919711544 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
13318 : 919711544 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
13319 : 919711544 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
13320 : 919711544 : explicit_intN = declspecs->explicit_intN_p;
13321 : 919711544 : int_n_alt = declspecs->int_n_alt;
13322 : 919711544 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
13323 : :
13324 : : // Was concept_p specified? Note that ds_concept
13325 : : // implies ds_constexpr!
13326 : 919711544 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
13327 : 919711544 : if (concept_p)
13328 : 114 : constexpr_p = true;
13329 : :
13330 : 919711544 : if (decl_context == FUNCDEF)
13331 : : funcdef_flag = true, decl_context = NORMAL;
13332 : : else if (decl_context == MEMFUNCDEF)
13333 : : funcdef_flag = true, decl_context = FIELD;
13334 : : else if (decl_context == BITFIELD)
13335 : : bitfield = 1, decl_context = FIELD;
13336 : : else if (decl_context == TEMPLATE_TYPE_ARG)
13337 : : template_type_arg = true, decl_context = TYPENAME;
13338 : : else if (decl_context == TPARM)
13339 : 11935346 : template_parm_flag = true, decl_context = PARM;
13340 : :
13341 : 919711544 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
13342 : 9303040 : funcdef_flag = true;
13343 : :
13344 : 919711544 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
13345 : : (type_quals,
13346 : 919711544 : declspecs->locations));
13347 : 919711544 : location_t id_loc;
13348 : 919711544 : location_t init_loc;
13349 : 919711544 : if (declarator)
13350 : : {
13351 : 571016855 : id_loc = loc_or_input_loc (declarator->id_loc);
13352 : 571016855 : init_loc = loc_or_input_loc (declarator->init_loc);
13353 : : }
13354 : : else
13355 : 348694689 : init_loc = id_loc = input_location;
13356 : :
13357 : : /* Look inside a declarator for the name being declared
13358 : : and get it as a string, for an error message. */
13359 : 919711544 : for (id_declarator = declarator;
13360 : 1278717871 : id_declarator;
13361 : 359006327 : id_declarator = id_declarator->declarator)
13362 : : {
13363 : 893341110 : if (id_declarator->kind != cdk_id)
13364 : 359006352 : innermost_code = id_declarator->kind;
13365 : :
13366 : 893341110 : switch (id_declarator->kind)
13367 : : {
13368 : 176343348 : case cdk_function:
13369 : 176343348 : if (id_declarator->declarator
13370 : 175856293 : && id_declarator->declarator->kind == cdk_id)
13371 : : {
13372 : 173909911 : sfk = id_declarator->declarator->u.id.sfk;
13373 : 173909911 : if (sfk == sfk_destructor)
13374 : 3146805 : flags = DTOR_FLAG;
13375 : : }
13376 : : break;
13377 : :
13378 : 534334758 : case cdk_id:
13379 : 534334758 : {
13380 : 534334758 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
13381 : 534334758 : tree decl = id_declarator->u.id.unqualified_name;
13382 : 534334758 : if (!decl)
13383 : : break;
13384 : 534334758 : if (qualifying_scope)
13385 : : {
13386 : 9702495 : if (check_for_bare_parameter_packs (qualifying_scope,
13387 : 9702495 : id_declarator->id_loc))
13388 : 6 : return error_mark_node;
13389 : 9702489 : if (at_function_scope_p ())
13390 : : {
13391 : : /* [dcl.meaning]
13392 : :
13393 : : A declarator-id shall not be qualified except
13394 : : for ...
13395 : :
13396 : : None of the cases are permitted in block
13397 : : scope. */
13398 : 0 : if (qualifying_scope == global_namespace)
13399 : 0 : error ("invalid use of qualified-name %<::%D%>",
13400 : : decl);
13401 : 0 : else if (TYPE_P (qualifying_scope))
13402 : 0 : error ("invalid use of qualified-name %<%T::%D%>",
13403 : : qualifying_scope, decl);
13404 : : else
13405 : 0 : error ("invalid use of qualified-name %<%D::%D%>",
13406 : : qualifying_scope, decl);
13407 : 0 : return error_mark_node;
13408 : : }
13409 : 9702489 : else if (TYPE_P (qualifying_scope))
13410 : : {
13411 : 9525222 : ctype = qualifying_scope;
13412 : 9525222 : if (!MAYBE_CLASS_TYPE_P (ctype))
13413 : : {
13414 : 6 : error_at (id_declarator->id_loc,
13415 : : "%q#T is not a class or namespace", ctype);
13416 : 6 : ctype = NULL_TREE;
13417 : : }
13418 : 9525216 : else if (innermost_code != cdk_function
13419 : 388044 : && current_class_type
13420 : 9525231 : && !uniquely_derived_from_p (ctype,
13421 : : current_class_type))
13422 : : {
13423 : 9 : error_at (id_declarator->id_loc,
13424 : : "invalid use of qualified-name %<%T::%D%>",
13425 : : qualifying_scope, decl);
13426 : 9 : return error_mark_node;
13427 : : }
13428 : : }
13429 : 177267 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
13430 : 534334743 : in_namespace = qualifying_scope;
13431 : : }
13432 : 534334743 : switch (TREE_CODE (decl))
13433 : : {
13434 : 3146820 : case BIT_NOT_EXPR:
13435 : 3146820 : {
13436 : 3146820 : if (innermost_code != cdk_function)
13437 : : {
13438 : 6 : error_at (EXPR_LOCATION (decl),
13439 : : "declaration of %qE as non-function", decl);
13440 : 6 : return error_mark_node;
13441 : : }
13442 : 3146814 : else if (!qualifying_scope
13443 : 3146814 : && !(current_class_type && at_class_scope_p ()))
13444 : : {
13445 : 9 : error_at (EXPR_LOCATION (decl),
13446 : : "declaration of %qE as non-member", decl);
13447 : 9 : return error_mark_node;
13448 : : }
13449 : :
13450 : 3146805 : tree type = TREE_OPERAND (decl, 0);
13451 : 3146805 : if (TYPE_P (type))
13452 : 3146802 : type = constructor_name (type);
13453 : 3146805 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
13454 : 3146805 : dname = decl;
13455 : : }
13456 : 3146805 : break;
13457 : :
13458 : 1928076 : case TEMPLATE_ID_EXPR:
13459 : 1928076 : {
13460 : 1928076 : tree fns = TREE_OPERAND (decl, 0);
13461 : :
13462 : 1928076 : dname = fns;
13463 : 2833954 : if (!identifier_p (dname))
13464 : 1928061 : dname = OVL_NAME (dname);
13465 : : }
13466 : : /* Fall through. */
13467 : :
13468 : 531187923 : case IDENTIFIER_NODE:
13469 : 531187923 : if (identifier_p (decl))
13470 : 529259847 : dname = decl;
13471 : :
13472 : 531187923 : if (IDENTIFIER_KEYWORD_P (dname))
13473 : : {
13474 : 0 : error ("declarator-id missing; using reserved word %qD",
13475 : : dname);
13476 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13477 : : }
13478 : 531187923 : else if (!IDENTIFIER_CONV_OP_P (dname))
13479 : 530552442 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13480 : : else
13481 : : {
13482 : 635481 : gcc_assert (flags == NO_SPECIAL);
13483 : 635481 : flags = TYPENAME_FLAG;
13484 : 635481 : sfk = sfk_conversion;
13485 : 635481 : tree glob = get_global_binding (dname);
13486 : 635481 : if (glob && TREE_CODE (glob) == TYPE_DECL)
13487 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
13488 : : else
13489 : : name = "<invalid operator>";
13490 : : }
13491 : : break;
13492 : :
13493 : 0 : default:
13494 : 0 : gcc_unreachable ();
13495 : : }
13496 : : break;
13497 : : }
13498 : :
13499 : : case cdk_array:
13500 : : case cdk_pointer:
13501 : : case cdk_reference:
13502 : : case cdk_ptrmem:
13503 : : break;
13504 : :
13505 : : case cdk_decomp:
13506 : 893341055 : name = "structured binding";
13507 : : break;
13508 : :
13509 : 25 : case cdk_error:
13510 : 25 : return error_mark_node;
13511 : :
13512 : 0 : default:
13513 : 0 : gcc_unreachable ();
13514 : : }
13515 : 893341055 : if (id_declarator->kind == cdk_id)
13516 : : break;
13517 : : }
13518 : :
13519 : : /* [dcl.fct.edf]
13520 : :
13521 : : The declarator in a function-definition shall have the form
13522 : : D1 ( parameter-declaration-clause) ... */
13523 : 919711489 : if (funcdef_flag && innermost_code != cdk_function)
13524 : : {
13525 : 3 : error_at (id_loc, "function definition does not declare parameters");
13526 : 3 : return error_mark_node;
13527 : : }
13528 : :
13529 : 919711486 : if (flags == TYPENAME_FLAG
13530 : 919711486 : && innermost_code != cdk_function
13531 : 3 : && ! (ctype && !declspecs->any_specifiers_p))
13532 : : {
13533 : 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
13534 : 3 : return error_mark_node;
13535 : : }
13536 : :
13537 : 919711483 : if (dname && identifier_p (dname))
13538 : : {
13539 : 531187917 : if (UDLIT_OPER_P (dname)
13540 : 531187917 : && innermost_code != cdk_function)
13541 : : {
13542 : 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
13543 : 6 : return error_mark_node;
13544 : : }
13545 : :
13546 : 531187911 : if (IDENTIFIER_ANY_OP_P (dname))
13547 : : {
13548 : 31850461 : if (typedef_p)
13549 : : {
13550 : 3 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
13551 : 3 : return error_mark_node;
13552 : : }
13553 : 31850458 : else if (decl_context == PARM || decl_context == CATCHPARM)
13554 : : {
13555 : 15 : error_at (id_loc, "declaration of %qD as parameter", dname);
13556 : 15 : return error_mark_node;
13557 : : }
13558 : : }
13559 : : }
13560 : :
13561 : : /* Anything declared one level down from the top level
13562 : : must be one of the parameters of a function
13563 : : (because the body is at least two levels down). */
13564 : :
13565 : : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
13566 : : by not allowing C++ class definitions to specify their parameters
13567 : : with xdecls (must be spec.d in the parmlist).
13568 : :
13569 : : Since we now wait to push a class scope until we are sure that
13570 : : we are in a legitimate method context, we must set oldcname
13571 : : explicitly (since current_class_name is not yet alive).
13572 : :
13573 : : We also want to avoid calling this a PARM if it is in a namespace. */
13574 : :
13575 : 919711459 : if (decl_context == NORMAL && !toplevel_bindings_p ())
13576 : : {
13577 : 62226881 : cp_binding_level *b = current_binding_level;
13578 : 62226881 : current_binding_level = b->level_chain;
13579 : 62226881 : if (current_binding_level != 0 && toplevel_bindings_p ())
13580 : : decl_context = PARM;
13581 : 62226881 : current_binding_level = b;
13582 : : }
13583 : :
13584 : 919711459 : if (name == NULL)
13585 : 385291951 : name = decl_context == PARM ? "parameter" : "type name";
13586 : :
13587 : 919711459 : if (consteval_p && constexpr_p)
13588 : : {
13589 : 6 : error_at (declspecs->locations[ds_consteval],
13590 : : "both %qs and %qs specified", "constexpr", "consteval");
13591 : 6 : return error_mark_node;
13592 : : }
13593 : :
13594 : 919711453 : if (concept_p && typedef_p)
13595 : : {
13596 : 9 : error_at (declspecs->locations[ds_concept],
13597 : : "%qs cannot appear in a typedef declaration", "concept");
13598 : 9 : return error_mark_node;
13599 : : }
13600 : :
13601 : 919711444 : if (constexpr_p && typedef_p)
13602 : : {
13603 : 3 : error_at (declspecs->locations[ds_constexpr],
13604 : : "%qs cannot appear in a typedef declaration", "constexpr");
13605 : 3 : return error_mark_node;
13606 : : }
13607 : :
13608 : 919711441 : if (consteval_p && typedef_p)
13609 : : {
13610 : 3 : error_at (declspecs->locations[ds_consteval],
13611 : : "%qs cannot appear in a typedef declaration", "consteval");
13612 : 3 : return error_mark_node;
13613 : : }
13614 : :
13615 : 919711438 : if (constinit_p && typedef_p)
13616 : : {
13617 : 3 : error_at (declspecs->locations[ds_constinit],
13618 : : "%qs cannot appear in a typedef declaration", "constinit");
13619 : 3 : return error_mark_node;
13620 : : }
13621 : :
13622 : : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
13623 : : keywords shall appear in a decl-specifier-seq." */
13624 : 919711435 : if (constinit_p && constexpr_p)
13625 : : {
13626 : 6 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
13627 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
13628 : 6 : error_at (&richloc,
13629 : : "can use at most one of the %<constinit%> and %<constexpr%> "
13630 : : "specifiers");
13631 : 6 : return error_mark_node;
13632 : 6 : }
13633 : :
13634 : : /* If there were multiple types specified in the decl-specifier-seq,
13635 : : issue an error message. */
13636 : 919711429 : if (declspecs->multiple_types_p)
13637 : : {
13638 : 1500 : error_at (typespec_loc,
13639 : : "two or more data types in declaration of %qs", name);
13640 : 1500 : return error_mark_node;
13641 : : }
13642 : :
13643 : 919709929 : if (declspecs->conflicting_specifiers_p)
13644 : 31 : return error_mark_node;
13645 : :
13646 : : /* Extract the basic type from the decl-specifier-seq. */
13647 : 919709898 : type = declspecs->type;
13648 : 919709898 : if (type == error_mark_node)
13649 : : {
13650 : 1175 : type = NULL_TREE;
13651 : 1175 : type_was_error_mark_node = true;
13652 : : }
13653 : :
13654 : : /* Ignore erroneous attributes. */
13655 : 919709898 : if (attrlist && *attrlist == error_mark_node)
13656 : 12 : *attrlist = NULL_TREE;
13657 : :
13658 : : /* An object declared as __attribute__((unavailable)) suppresses
13659 : : any reports of being declared with unavailable or deprecated
13660 : : items. An object declared as __attribute__((deprecated))
13661 : : suppresses warnings of uses of other deprecated items. */
13662 : 919709898 : auto ds = make_temp_override (deprecated_state);
13663 : 919709898 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
13664 : 159 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
13665 : 919709739 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
13666 : 704988 : deprecated_state = DEPRECATED_SUPPRESS;
13667 : :
13668 : 919709898 : cp_handle_deprecated_or_unavailable (type);
13669 : 919709898 : if (type && TREE_CODE (type) == TYPE_DECL)
13670 : : {
13671 : 630287934 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
13672 : 630287934 : typedef_decl = type;
13673 : 630287934 : type = TREE_TYPE (typedef_decl);
13674 : 630287934 : if (DECL_ARTIFICIAL (typedef_decl))
13675 : 455598846 : cp_handle_deprecated_or_unavailable (type);
13676 : : }
13677 : : /* No type at all: default to `int', and set DEFAULTED_INT
13678 : : because it was not a user-defined typedef. */
13679 : 919709898 : if (type == NULL_TREE)
13680 : : {
13681 : 35387414 : if (signed_p || unsigned_p || long_p || short_p)
13682 : : {
13683 : : /* These imply 'int'. */
13684 : 8220123 : type = integer_type_node;
13685 : 8220123 : defaulted_int = 1;
13686 : : }
13687 : : /* If we just have "complex", it is equivalent to "complex double". */
13688 : 27167291 : else if (!longlong && !explicit_intN
13689 : 27167291 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
13690 : : {
13691 : 81 : type = double_type_node;
13692 : 81 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
13693 : : "ISO C++ does not support plain %<complex%> meaning "
13694 : : "%<double complex%>");
13695 : : }
13696 : : }
13697 : : /* Gather flags. */
13698 : 919709898 : explicit_int = declspecs->explicit_int_p;
13699 : 919709898 : explicit_char = declspecs->explicit_char_p;
13700 : :
13701 : : #if 0
13702 : : /* See the code below that used this. */
13703 : : if (typedef_decl)
13704 : : decl_attr = DECL_ATTRIBUTES (typedef_decl);
13705 : : #endif
13706 : 919709898 : typedef_type = type;
13707 : :
13708 : 919709898 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
13709 : 1704749 : ctor_return_type = TREE_TYPE (dname);
13710 : : else
13711 : : ctor_return_type = ctype;
13712 : :
13713 : 919709898 : if (sfk != sfk_none)
13714 : : {
13715 : 27165715 : type = check_special_function_return_type (sfk, type,
13716 : : ctor_return_type,
13717 : : type_quals,
13718 : : &declarator,
13719 : : declspecs->locations);
13720 : 27165715 : type_quals = TYPE_UNQUALIFIED;
13721 : : }
13722 : 892544183 : else if (type == NULL_TREE)
13723 : : {
13724 : 1576 : int is_main;
13725 : :
13726 : 1576 : explicit_int = -1;
13727 : :
13728 : : /* We handle `main' specially here, because 'main () { }' is so
13729 : : common. With no options, it is allowed. With -Wreturn-type,
13730 : : it is a warning. It is only an error with -pedantic-errors. */
13731 : 3152 : is_main = (funcdef_flag
13732 : 3387 : && dname && identifier_p (dname)
13733 : 235 : && MAIN_NAME_P (dname)
13734 : 68 : && ctype == NULL_TREE
13735 : 68 : && in_namespace == NULL_TREE
13736 : 1644 : && current_namespace == global_namespace);
13737 : :
13738 : 1576 : if (type_was_error_mark_node)
13739 : : /* We've already issued an error, don't complain more. */;
13740 : 401 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
13741 : : /* Allow it, sigh. */;
13742 : 339 : else if (! is_main)
13743 : 277 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
13744 : : name);
13745 : 62 : else if (pedantic)
13746 : 4 : pedwarn (id_loc, OPT_Wpedantic,
13747 : : "ISO C++ forbids declaration of %qs with no type", name);
13748 : : else
13749 : 58 : warning_at (id_loc, OPT_Wreturn_type,
13750 : : "ISO C++ forbids declaration of %qs with no type", name);
13751 : :
13752 : 1576 : if (type_was_error_mark_node && template_parm_flag)
13753 : : /* FIXME we should be able to propagate the error_mark_node as is
13754 : : for other contexts too. */
13755 : 151 : type = error_mark_node;
13756 : : else
13757 : 1425 : type = integer_type_node;
13758 : : }
13759 : :
13760 : 919709898 : ctype = NULL_TREE;
13761 : :
13762 : 919709898 : if (explicit_intN)
13763 : : {
13764 : 957166 : if (! int_n_enabled_p[declspecs->int_n_idx])
13765 : : {
13766 : 0 : error_at (declspecs->locations[ds_type_spec],
13767 : : "%<__int%d%> is not supported by this target",
13768 : 0 : int_n_data[declspecs->int_n_idx].bitsize);
13769 : 0 : explicit_intN = false;
13770 : : }
13771 : : /* Don't pedwarn if the alternate "__intN__" form has been used instead
13772 : : of "__intN". */
13773 : 957166 : else if (!int_n_alt && pedantic)
13774 : 3914 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
13775 : : "ISO C++ does not support %<__int%d%> for %qs",
13776 : 3914 : int_n_data[declspecs->int_n_idx].bitsize, name);
13777 : : }
13778 : :
13779 : : /* Now process the modifiers that were specified
13780 : : and check for invalid combinations. */
13781 : :
13782 : : /* Long double is a special combination. */
13783 : 919709898 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
13784 : : {
13785 : 9098538 : long_p = false;
13786 : 9098538 : type = cp_build_qualified_type (long_double_type_node,
13787 : : cp_type_quals (type));
13788 : : }
13789 : :
13790 : : /* Check all other uses of type modifiers. */
13791 : :
13792 : 919709898 : if (unsigned_p || signed_p || long_p || short_p)
13793 : : {
13794 : 19729296 : location_t loc;
13795 : 19729296 : const char *key;
13796 : 19729296 : if (unsigned_p)
13797 : : {
13798 : 12977052 : key = "unsigned";
13799 : 12977052 : loc = declspecs->locations[ds_unsigned];
13800 : : }
13801 : 6752244 : else if (signed_p)
13802 : : {
13803 : 1081256 : key = "signed";
13804 : 1081256 : loc = declspecs->locations[ds_signed];
13805 : : }
13806 : 5670988 : else if (longlong)
13807 : : {
13808 : 2171178 : key = "long long";
13809 : 2171178 : loc = declspecs->locations[ds_long_long];
13810 : : }
13811 : 3499810 : else if (long_p)
13812 : : {
13813 : 2989847 : key = "long";
13814 : 2989847 : loc = declspecs->locations[ds_long];
13815 : : }
13816 : : else /* if (short_p) */
13817 : : {
13818 : 509963 : key = "short";
13819 : 509963 : loc = declspecs->locations[ds_short];
13820 : : }
13821 : :
13822 : 19729296 : int ok = 0;
13823 : :
13824 : 19729296 : if (signed_p && unsigned_p)
13825 : 54 : complain_about_incompatible_declspecs
13826 : 54 : ("signed", declspecs->locations[ds_signed],
13827 : : "unsigned", declspecs->locations[ds_unsigned]);
13828 : 19729242 : else if (long_p && short_p)
13829 : 12 : complain_about_incompatible_declspecs
13830 : 12 : ("long", declspecs->locations[ds_long],
13831 : : "short", declspecs->locations[ds_short]);
13832 : 19729230 : else if (TREE_CODE (type) != INTEGER_TYPE
13833 : 19729217 : || type == char8_type_node
13834 : 19729193 : || type == char16_type_node
13835 : 19729169 : || type == char32_type_node
13836 : 19729145 : || ((long_p || short_p)
13837 : 10725431 : && (explicit_char || explicit_intN)))
13838 : 418 : error_at (loc, "%qs specified with %qT", key, type);
13839 : 19728812 : else if (!explicit_int && !defaulted_int
13840 : 19728812 : && !explicit_char && !explicit_intN)
13841 : : {
13842 : 30 : if (typedef_decl)
13843 : : {
13844 : 27 : pedwarn (loc, OPT_Wpedantic,
13845 : : "%qs specified with typedef-name %qD",
13846 : : key, typedef_decl);
13847 : 27 : ok = !flag_pedantic_errors;
13848 : : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
13849 : : and is not built as a typedef. */
13850 : 27 : if (is_typedef_decl (typedef_decl))
13851 : 18 : type = DECL_ORIGINAL_TYPE (typedef_decl);
13852 : : }
13853 : 3 : else if (declspecs->decltype_p)
13854 : 0 : error_at (loc, "%qs specified with %<decltype%>", key);
13855 : : else
13856 : 3 : error_at (loc, "%qs specified with %<typeof%>", key);
13857 : : }
13858 : : else
13859 : : ok = 1;
13860 : :
13861 : : /* Discard the type modifiers if they are invalid. */
13862 : 514 : if (! ok)
13863 : : {
13864 : : unsigned_p = false;
13865 : : signed_p = false;
13866 : : long_p = false;
13867 : : short_p = false;
13868 : : longlong = 0;
13869 : : }
13870 : : }
13871 : :
13872 : : /* Decide whether an integer type is signed or not.
13873 : : Optionally treat bitfields as signed by default. */
13874 : 919709408 : if (unsigned_p
13875 : : /* [class.bit]
13876 : :
13877 : : It is implementation-defined whether a plain (neither
13878 : : explicitly signed or unsigned) char, short, int, or long
13879 : : bit-field is signed or unsigned.
13880 : :
13881 : : Naturally, we extend this to long long as well. Note that
13882 : : this does not include wchar_t. */
13883 : 919709898 : || (bitfield && !flag_signed_bitfields
13884 : 18 : && !signed_p
13885 : : /* A typedef for plain `int' without `signed' can be
13886 : : controlled just like plain `int', but a typedef for
13887 : : `signed int' cannot be so controlled. */
13888 : 18 : && !(typedef_decl
13889 : 18 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
13890 : 13 : && TREE_CODE (type) == INTEGER_TYPE
13891 : 10 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
13892 : : {
13893 : 12976837 : if (explicit_intN)
13894 : 453957 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
13895 : 12522880 : else if (longlong)
13896 : 1354544 : type = long_long_unsigned_type_node;
13897 : 11168336 : else if (long_p)
13898 : 2171202 : type = long_unsigned_type_node;
13899 : 8997134 : else if (short_p)
13900 : 1278873 : type = short_unsigned_type_node;
13901 : 7718261 : else if (type == char_type_node)
13902 : 1545019 : type = unsigned_char_type_node;
13903 : 6173242 : else if (typedef_decl)
13904 : 25 : type = c_common_unsigned_type (type);
13905 : : else
13906 : 6173217 : type = unsigned_type_node;
13907 : : }
13908 : 906733061 : else if (signed_p && type == char_type_node)
13909 : 670238 : type = signed_char_type_node;
13910 : 906062823 : else if (explicit_intN)
13911 : 503209 : type = int_n_trees[declspecs->int_n_idx].signed_type;
13912 : 905559614 : else if (longlong)
13913 : 2248408 : type = long_long_integer_type_node;
13914 : 903311206 : else if (long_p)
13915 : 3070547 : type = long_integer_type_node;
13916 : 900240659 : else if (short_p)
13917 : 601524 : type = short_integer_type_node;
13918 : 899639135 : else if (signed_p && typedef_decl)
13919 : 9 : type = c_common_signed_type (type);
13920 : :
13921 : 919709898 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
13922 : : {
13923 : 1410765 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
13924 : 3 : error_at (declspecs->locations[ds_complex],
13925 : : "complex invalid for %qs", name);
13926 : : /* If a modifier is specified, the resulting complex is the complex
13927 : : form of TYPE. E.g, "complex short" is "complex short int". */
13928 : 1410762 : else if (type == integer_type_node)
13929 : 164 : type = complex_integer_type_node;
13930 : 1410598 : else if (type == float_type_node)
13931 : 358901 : type = complex_float_type_node;
13932 : 1051697 : else if (type == double_type_node)
13933 : 359431 : type = complex_double_type_node;
13934 : 692266 : else if (type == long_double_type_node)
13935 : 358928 : type = complex_long_double_type_node;
13936 : : else
13937 : 333338 : type = build_complex_type (type);
13938 : : }
13939 : :
13940 : : /* If we're using the injected-class-name to form a compound type or a
13941 : : declaration, replace it with the underlying class so we don't get
13942 : : redundant typedefs in the debug output. But if we are returning the
13943 : : type unchanged, leave it alone so that it's available to
13944 : : maybe_get_template_decl_from_type_decl. */
13945 : 182424892 : if (CLASS_TYPE_P (type)
13946 : 182375278 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
13947 : 29335212 : && type == TREE_TYPE (TYPE_NAME (type))
13948 : 949045110 : && (declarator || type_quals))
13949 : 28138314 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
13950 : :
13951 : 919709898 : type_quals |= cp_type_quals (type);
13952 : 1839419796 : type = cp_build_qualified_type
13953 : 1839419796 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
13954 : 745020810 : || declspecs->decltype_p)
13955 : : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
13956 : : /* We might have ignored or rejected some of the qualifiers. */
13957 : 919709898 : type_quals = cp_type_quals (type);
13958 : :
13959 : 905086303 : if (cxx_dialect >= cxx17 && type && is_auto (type)
13960 : 13431777 : && innermost_code != cdk_function
13961 : : /* Placeholder in parm gets a better error below. */
13962 : 7895317 : && !(decl_context == PARM || decl_context == CATCHPARM)
13963 : 927574612 : && id_declarator && declarator != id_declarator)
13964 : 963379 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
13965 : : {
13966 : 16 : auto_diagnostic_group g;
13967 : 16 : gcc_rich_location richloc (typespec_loc);
13968 : 16 : richloc.add_fixit_insert_after ("<>");
13969 : 16 : error_at (&richloc, "missing template argument list after %qE; "
13970 : : "for deduction, template placeholder must be followed "
13971 : : "by a simple declarator-id", tmpl);
13972 : 16 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
13973 : 16 : type = error_mark_node;
13974 : 16 : }
13975 : :
13976 : 919709898 : staticp = 0;
13977 : 919709898 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
13978 : 919709898 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
13979 : 919709898 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
13980 : :
13981 : 919709898 : storage_class = declspecs->storage_class;
13982 : 919709898 : if (storage_class == sc_static)
13983 : 19394752 : staticp = 1 + (decl_context == FIELD);
13984 : 900315146 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
13985 : : /* Treat class-scope deduction guides as static member functions
13986 : : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
13987 : 19069611 : staticp = 2;
13988 : :
13989 : 919709898 : if (virtualp)
13990 : : {
13991 : 4054544 : if (staticp == 2)
13992 : : {
13993 : 21 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13994 : 21 : richloc.add_range (declspecs->locations[ds_storage_class]);
13995 : 21 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
13996 : : "and %<static%>", dname);
13997 : 21 : storage_class = sc_none;
13998 : 21 : staticp = 0;
13999 : 21 : }
14000 : 4054544 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
14001 : : {
14002 : 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14003 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
14004 : 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
14005 : : "declared both %<virtual%> and %<constexpr%> only in "
14006 : : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
14007 : 6 : }
14008 : : }
14009 : 919709898 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
14010 : :
14011 : : /* Issue errors about use of storage classes for parameters. */
14012 : 919709898 : if (decl_context == PARM)
14013 : : {
14014 : 276846836 : if (typedef_p)
14015 : : {
14016 : 24 : error_at (declspecs->locations[ds_typedef],
14017 : : "typedef declaration invalid in parameter declaration");
14018 : 24 : return error_mark_node;
14019 : : }
14020 : 276846812 : else if (template_parm_flag && storage_class != sc_none)
14021 : : {
14022 : 16 : error_at (min_location (declspecs->locations[ds_thread],
14023 : : declspecs->locations[ds_storage_class]),
14024 : : "storage class specified for template parameter %qs",
14025 : : name);
14026 : 16 : return error_mark_node;
14027 : : }
14028 : 276846796 : else if (storage_class == sc_static
14029 : 276846796 : || storage_class == sc_extern
14030 : 276846793 : || thread_p)
14031 : : {
14032 : 9 : error_at (min_location (declspecs->locations[ds_thread],
14033 : : declspecs->locations[ds_storage_class]),
14034 : : "storage class specified for parameter %qs", name);
14035 : 9 : return error_mark_node;
14036 : : }
14037 : :
14038 : : /* Function parameters cannot be concept. */
14039 : 276846787 : if (concept_p)
14040 : : {
14041 : 9 : error_at (declspecs->locations[ds_concept],
14042 : : "a parameter cannot be declared %qs", "concept");
14043 : 9 : concept_p = 0;
14044 : 9 : constexpr_p = 0;
14045 : : }
14046 : : /* Function parameters cannot be constexpr. If we saw one, moan
14047 : : and pretend it wasn't there. */
14048 : 276846778 : else if (constexpr_p)
14049 : : {
14050 : 9 : error_at (declspecs->locations[ds_constexpr],
14051 : : "a parameter cannot be declared %qs", "constexpr");
14052 : 9 : constexpr_p = 0;
14053 : : }
14054 : 276846787 : if (constinit_p)
14055 : : {
14056 : 6 : error_at (declspecs->locations[ds_constinit],
14057 : : "a parameter cannot be declared %qs", "constinit");
14058 : 6 : constinit_p = 0;
14059 : : }
14060 : 276846787 : if (consteval_p)
14061 : : {
14062 : 3 : error_at (declspecs->locations[ds_consteval],
14063 : : "a parameter cannot be declared %qs", "consteval");
14064 : 3 : consteval_p = 0;
14065 : : }
14066 : : }
14067 : :
14068 : : /* Give error if `virtual' is used outside of class declaration. */
14069 : 919709849 : if (virtualp
14070 : 4054544 : && (current_class_name == NULL_TREE || decl_context != FIELD))
14071 : : {
14072 : 68 : error_at (declspecs->locations[ds_virtual],
14073 : : "%<virtual%> outside class declaration");
14074 : 68 : virtualp = 0;
14075 : : }
14076 : :
14077 : 919709849 : if (innermost_code == cdk_decomp)
14078 : : {
14079 : 74509 : location_t loc = (declarator->kind == cdk_reference
14080 : 84810 : ? declarator->declarator->id_loc : declarator->id_loc);
14081 : 84810 : if (inlinep)
14082 : 29 : error_at (declspecs->locations[ds_inline],
14083 : : "structured binding declaration cannot be %qs", "inline");
14084 : 84810 : if (typedef_p)
14085 : 29 : error_at (declspecs->locations[ds_typedef],
14086 : : "structured binding declaration cannot be %qs", "typedef");
14087 : 84810 : if (constexpr_p && !concept_p && cxx_dialect < cxx26)
14088 : 96 : pedwarn (declspecs->locations[ds_constexpr], OPT_Wc__26_extensions,
14089 : : "structured binding declaration can be %qs only with "
14090 : : "%<-std=c++2c%> or %<-std=gnu++2c%>", "constexpr");
14091 : 84810 : if (consteval_p)
14092 : 13 : error_at (declspecs->locations[ds_consteval], "structured "
14093 : : "binding declaration cannot be %qs", "consteval");
14094 : 84810 : if (thread_p && cxx_dialect < cxx20)
14095 : 77 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
14096 : : "structured binding declaration can be %qs only in "
14097 : : "%<-std=c++20%> or %<-std=gnu++20%>",
14098 : 77 : declspecs->gnu_thread_keyword_p
14099 : : ? "__thread" : "thread_local");
14100 : 84810 : if (concept_p)
14101 : : {
14102 : 0 : error_at (declspecs->locations[ds_concept],
14103 : : "structured binding declaration cannot be %qs", "concept");
14104 : 0 : constexpr_p = 0;
14105 : : }
14106 : : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
14107 : 84810 : if (type_quals & TYPE_QUAL_VOLATILE)
14108 : 26 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
14109 : : "%<volatile%>-qualified structured binding is deprecated");
14110 : 84810 : switch (storage_class)
14111 : : {
14112 : : case sc_none:
14113 : : break;
14114 : 28 : case sc_register:
14115 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14116 : : "register");
14117 : 28 : break;
14118 : 235 : case sc_static:
14119 : 235 : if (cxx_dialect < cxx20)
14120 : 186 : pedwarn (loc, OPT_Wc__20_extensions,
14121 : : "structured binding declaration can be %qs only in "
14122 : : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
14123 : : break;
14124 : 29 : case sc_extern:
14125 : 29 : error_at (loc, "structured binding declaration cannot be %qs",
14126 : : "extern");
14127 : 29 : break;
14128 : 28 : case sc_mutable:
14129 : 28 : error_at (loc, "structured binding declaration cannot be %qs",
14130 : : "mutable");
14131 : 28 : break;
14132 : 0 : case sc_auto:
14133 : 0 : error_at (loc, "structured binding declaration cannot be "
14134 : : "C++98 %<auto%>");
14135 : 0 : break;
14136 : 0 : default:
14137 : 0 : gcc_unreachable ();
14138 : : }
14139 : 84810 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
14140 : 84810 : || TYPE_IDENTIFIER (type) != auto_identifier)
14141 : : {
14142 : 3 : if (type != error_mark_node)
14143 : : {
14144 : 3 : auto_diagnostic_group d;
14145 : 3 : error_at (loc, "structured binding declaration cannot have "
14146 : : "type %qT", type);
14147 : 3 : inform (loc,
14148 : : "type must be cv-qualified %<auto%> or reference to "
14149 : : "cv-qualified %<auto%>");
14150 : 3 : }
14151 : 3 : type = build_qualified_type (make_auto (), type_quals);
14152 : 3 : declspecs->type = type;
14153 : : }
14154 : 84807 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
14155 : 51 : pedwarn (loc, OPT_Wpedantic,
14156 : : "structured binding declaration cannot have constrained "
14157 : : "%<auto%> type %qT", type);
14158 : 84810 : inlinep = 0;
14159 : 84810 : typedef_p = 0;
14160 : 84810 : consteval_p = 0;
14161 : 84810 : concept_p = 0;
14162 : 84810 : if (storage_class != sc_static)
14163 : : {
14164 : 84575 : storage_class = sc_none;
14165 : 84575 : declspecs->storage_class = sc_none;
14166 : : }
14167 : : }
14168 : :
14169 : : /* Static anonymous unions are dealt with here. */
14170 : 919709849 : if (staticp && decl_context == TYPENAME
14171 : 0 : && declspecs->type
14172 : 919709849 : && ANON_AGGR_TYPE_P (declspecs->type))
14173 : : decl_context = FIELD;
14174 : :
14175 : : /* Warn about storage classes that are invalid for certain
14176 : : kinds of declarations (parameters, typenames, etc.). */
14177 : 919709849 : if (thread_p
14178 : 21074 : && ((storage_class
14179 : 21074 : && storage_class != sc_extern
14180 : 518 : && storage_class != sc_static)
14181 : 21063 : || typedef_p))
14182 : : {
14183 : 17 : location_t loc
14184 : 17 : = min_location (declspecs->locations[ds_thread],
14185 : : declspecs->locations[ds_storage_class]);
14186 : 17 : error_at (loc, "multiple storage classes in declaration of %qs", name);
14187 : 17 : thread_p = false;
14188 : : }
14189 : 919709849 : if (decl_context != NORMAL
14190 : 764838809 : && ((storage_class != sc_none
14191 : 764838809 : && storage_class != sc_mutable)
14192 : 745769210 : || thread_p))
14193 : : {
14194 : 19069602 : if ((decl_context == PARM || decl_context == CATCHPARM)
14195 : 36 : && (storage_class == sc_register
14196 : 36 : || storage_class == sc_auto))
14197 : : ;
14198 : 19069566 : else if (typedef_p)
14199 : : ;
14200 : 19069566 : else if (decl_context == FIELD
14201 : : /* C++ allows static class elements. */
14202 : 19069566 : && storage_class == sc_static)
14203 : : /* C++ also allows inlines and signed and unsigned elements,
14204 : : but in those cases we don't come in here. */
14205 : : ;
14206 : : else
14207 : : {
14208 : 15 : location_t loc
14209 : 15 : = min_location (declspecs->locations[ds_thread],
14210 : : declspecs->locations[ds_storage_class]);
14211 : 15 : if (decl_context == FIELD)
14212 : 15 : error_at (loc, "storage class specified for %qs", name);
14213 : 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
14214 : 0 : error_at (loc, "storage class specified for parameter %qs", name);
14215 : : else
14216 : 0 : error_at (loc, "storage class specified for typename");
14217 : 15 : if (storage_class == sc_register
14218 : 15 : || storage_class == sc_auto
14219 : 15 : || storage_class == sc_extern
14220 : 3 : || thread_p)
14221 : 15 : storage_class = sc_none;
14222 : : }
14223 : : }
14224 : 900640247 : else if (storage_class == sc_extern && funcdef_flag
14225 : 900640247 : && ! toplevel_bindings_p ())
14226 : 0 : error ("nested function %qs declared %<extern%>", name);
14227 : 900640247 : else if (toplevel_bindings_p ())
14228 : : {
14229 : 415726868 : if (storage_class == sc_auto)
14230 : 2 : error_at (declspecs->locations[ds_storage_class],
14231 : : "top-level declaration of %qs specifies %<auto%>", name);
14232 : : }
14233 : 484913379 : else if (thread_p
14234 : 484913379 : && storage_class != sc_extern
14235 : 220 : && storage_class != sc_static)
14236 : : {
14237 : 187 : if (declspecs->gnu_thread_keyword_p)
14238 : 49 : pedwarn (declspecs->locations[ds_thread],
14239 : 49 : 0, "function-scope %qs implicitly auto and "
14240 : : "declared %<__thread%>", name);
14241 : :
14242 : : /* When thread_local is applied to a variable of block scope the
14243 : : storage-class-specifier static is implied if it does not appear
14244 : : explicitly. */
14245 : 187 : storage_class = declspecs->storage_class = sc_static;
14246 : 187 : staticp = 1;
14247 : : }
14248 : :
14249 : 919709849 : if (storage_class && friendp)
14250 : : {
14251 : 12 : error_at (min_location (declspecs->locations[ds_thread],
14252 : : declspecs->locations[ds_storage_class]),
14253 : : "storage class specifiers invalid in friend function "
14254 : : "declarations");
14255 : 12 : storage_class = sc_none;
14256 : 12 : staticp = 0;
14257 : : }
14258 : :
14259 : 919709849 : if (!id_declarator)
14260 : : unqualified_id = NULL_TREE;
14261 : : else
14262 : : {
14263 : 534333167 : unqualified_id = id_declarator->u.id.unqualified_name;
14264 : 534333167 : switch (TREE_CODE (unqualified_id))
14265 : : {
14266 : 3146805 : case BIT_NOT_EXPR:
14267 : 3146805 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
14268 : 3146805 : if (TYPE_P (unqualified_id))
14269 : 3146802 : unqualified_id = constructor_name (unqualified_id);
14270 : : break;
14271 : :
14272 : : case IDENTIFIER_NODE:
14273 : : case TEMPLATE_ID_EXPR:
14274 : : break;
14275 : :
14276 : 0 : default:
14277 : 0 : gcc_unreachable ();
14278 : : }
14279 : : }
14280 : :
14281 : 919709849 : if (declspecs->std_attributes
14282 : 919709849 : && !diagnose_misapplied_contracts (declspecs->std_attributes))
14283 : : {
14284 : 651 : location_t attr_loc = declspecs->locations[ds_std_attribute];
14285 : 651 : auto_diagnostic_group d;
14286 : 651 : if (any_nonignored_attribute_p (declspecs->std_attributes)
14287 : 651 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
14288 : 648 : inform (attr_loc, "an attribute that appertains to a type-specifier "
14289 : : "is ignored");
14290 : 651 : }
14291 : :
14292 : 919709849 : if (attrlist)
14293 : 907534677 : diagnose_misapplied_contracts (*attrlist);
14294 : :
14295 : : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
14296 : : bool is_xobj_member_function = false;
14297 : : /* Determine the type of the entity declared by recurring on the
14298 : : declarator. */
14299 : 1278629359 : for (; declarator; declarator = declarator->declarator)
14300 : : {
14301 : 893337512 : const cp_declarator *inner_declarator;
14302 : 893337512 : tree attrs;
14303 : :
14304 : 893337512 : if (type == error_mark_node)
14305 : 599 : return error_mark_node;
14306 : :
14307 : 893337186 : attrs = declarator->attributes;
14308 : 893337186 : if (attrs)
14309 : : {
14310 : 13097 : int attr_flags;
14311 : :
14312 : 13097 : attr_flags = 0;
14313 : 13097 : if (declarator->kind == cdk_id)
14314 : 12423 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
14315 : 13097 : if (declarator->kind == cdk_function)
14316 : 657 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
14317 : 13097 : if (declarator->kind == cdk_array)
14318 : 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
14319 : 13097 : tree late_attrs = NULL_TREE;
14320 : 13097 : if (decl_context != PARM && decl_context != TYPENAME)
14321 : : /* Assume that any attributes that get applied late to
14322 : : templates will DTRT when applied to the declaration
14323 : : as a whole. */
14324 : 13085 : late_attrs = splice_template_attributes (&attrs, type);
14325 : 13097 : returned_attrs = decl_attributes (&type,
14326 : : attr_chainon (returned_attrs,
14327 : : attrs),
14328 : : attr_flags);
14329 : 13097 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
14330 : : }
14331 : :
14332 : 893337186 : inner_declarator = declarator->declarator;
14333 : :
14334 : : /* Check that contracts aren't misapplied. */
14335 : 893337186 : if (tree contract_attr = find_contract (declarator->std_attributes))
14336 : 484 : if (declarator->kind != cdk_function
14337 : 479 : || innermost_code != cdk_function)
14338 : 7 : diagnose_misapplied_contracts (contract_attr);
14339 : :
14340 : : /* We don't want to warn in parameter context because we don't
14341 : : yet know if the parse will succeed, and this might turn out
14342 : : to be a constructor call. */
14343 : 893337186 : if (decl_context != PARM
14344 : 893337186 : && decl_context != TYPENAME
14345 : 513296485 : && !typedef_p
14346 : 468274429 : && declarator->parenthesized != UNKNOWN_LOCATION
14347 : : /* If the type is class-like and the inner name used a
14348 : : global namespace qualifier, we need the parens.
14349 : : Unfortunately all we can tell is whether a qualified name
14350 : : was used or not. */
14351 : 893337367 : && !(inner_declarator
14352 : 126 : && inner_declarator->kind == cdk_id
14353 : 108 : && inner_declarator->u.id.qualifying_scope
14354 : 18 : && (MAYBE_CLASS_TYPE_P (type)
14355 : 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
14356 : : {
14357 : 169 : auto_diagnostic_group d;
14358 : 169 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
14359 : : "unnecessary parentheses in declaration of %qs",
14360 : : name))
14361 : : {
14362 : 42 : gcc_rich_location iloc (declarator->parenthesized);
14363 : 42 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
14364 : 42 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
14365 : 42 : inform (&iloc, "remove parentheses");
14366 : 42 : }
14367 : 169 : }
14368 : 893337186 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
14369 : : break;
14370 : :
14371 : 358919783 : switch (declarator->kind)
14372 : : {
14373 : 3135000 : case cdk_array:
14374 : 6270000 : type = create_array_type_for_decl (dname, type,
14375 : 3135000 : declarator->u.array.bounds,
14376 : 3135000 : declarator->id_loc);
14377 : 3135000 : if (!valid_array_size_p (dname
14378 : : ? declarator->id_loc : input_location,
14379 : : type, dname))
14380 : 267 : type = error_mark_node;
14381 : :
14382 : 3135000 : if (declarator->std_attributes)
14383 : : /* [dcl.array]/1:
14384 : :
14385 : : The optional attribute-specifier-seq appertains to the
14386 : : array type. */
14387 : 177 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
14388 : : break;
14389 : :
14390 : 176343312 : case cdk_function:
14391 : 176343312 : {
14392 : 176343312 : tree arg_types;
14393 : 176343312 : int funcdecl_p;
14394 : :
14395 : : /* Declaring a function type. */
14396 : :
14397 : : /* Pick up type qualifiers which should be applied to `this'. */
14398 : 176343312 : memfn_quals = declarator->u.function.qualifiers;
14399 : : /* Pick up virt-specifiers. */
14400 : 176343312 : virt_specifiers = declarator->u.function.virt_specifiers;
14401 : : /* And ref-qualifier, too */
14402 : 176343312 : rqual = declarator->u.function.ref_qualifier;
14403 : : /* And tx-qualifier. */
14404 : 176343312 : tree tx_qual = declarator->u.function.tx_qualifier;
14405 : : /* Pick up the exception specifications. */
14406 : 176343312 : raises = declarator->u.function.exception_specification;
14407 : : /* If the exception-specification is ill-formed, let's pretend
14408 : : there wasn't one. */
14409 : 176343312 : if (raises == error_mark_node)
14410 : 9 : raises = NULL_TREE;
14411 : :
14412 : 352686624 : auto find_xobj_parm = [](tree parm_list)
14413 : : {
14414 : : /* There is no need to iterate over the list,
14415 : : only the first parm can be a valid xobj parm. */
14416 : 352455444 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
14417 : : return NULL_TREE;
14418 : : /* If we make it here, we are looking at an xobj parm.
14419 : :
14420 : : Non-null 'purpose' usually means the parm has a default
14421 : : argument, we don't want to violate this assumption. */
14422 : 27252 : TREE_PURPOSE (parm_list) = NULL_TREE;
14423 : 27252 : return TREE_VALUE (parm_list);
14424 : : };
14425 : :
14426 : 176343312 : tree xobj_parm
14427 : 176343312 : = find_xobj_parm (declarator->u.function.parameters);
14428 : 176343312 : is_xobj_member_function = xobj_parm;
14429 : :
14430 : 176343312 : if (xobj_parm && cxx_dialect < cxx23)
14431 : 24 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
14432 : : "explicit object member function only available "
14433 : : "with %<-std=c++23%> or %<-std=gnu++23%>");
14434 : :
14435 : 176343312 : if (xobj_parm && decl_context == TYPENAME)
14436 : : {
14437 : : /* We inform in every case, just differently depending on what
14438 : : case it is. */
14439 : 6 : auto_diagnostic_group d;
14440 : 6 : bool ptr_type = true;
14441 : : /* If declarator->kind is cdk_function and we are at the end of
14442 : : the declarator chain, we are looking at a function type. */
14443 : 6 : if (!declarator->declarator)
14444 : : {
14445 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14446 : : "a function type cannot "
14447 : : "have an explicit object parameter");
14448 : 2 : ptr_type = false;
14449 : : }
14450 : 4 : else if (declarator->declarator->kind == cdk_pointer)
14451 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14452 : : "a pointer to function type cannot "
14453 : : "have an explicit object parameter");
14454 : 2 : else if (declarator->declarator->kind == cdk_ptrmem)
14455 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14456 : : "a pointer to member function type "
14457 : : "cannot have an explicit object parameter");
14458 : : else
14459 : 0 : gcc_unreachable ();
14460 : :
14461 : : /* The locations being used here are probably not correct. */
14462 : 4 : if (ptr_type)
14463 : 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14464 : : "the type of a pointer to explicit object member "
14465 : : "function is a regular pointer to function type");
14466 : : else
14467 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14468 : : "the type of an explicit object "
14469 : : "member function is a regular function type");
14470 : : /* Ideally we should synthesize the correct syntax
14471 : : for the user, perhaps this could be added later. */
14472 : 6 : }
14473 : : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
14474 : : the first parm node will never erroneously be detected here. */
14475 : 176343312 : {
14476 : 176343312 : auto_diagnostic_group d;
14477 : 176343312 : bool bad_xobj_parm_encountered = false;
14478 : 176343312 : for (tree parm = declarator->u.function.parameters;
14479 : 441867399 : parm && parm != void_list_node;
14480 : 265524087 : parm = TREE_CHAIN (parm))
14481 : : {
14482 : 265524087 : if (TREE_PURPOSE (parm) != this_identifier)
14483 : 265524081 : continue;
14484 : 6 : bad_xobj_parm_encountered = true;
14485 : 6 : TREE_PURPOSE (parm) = NULL_TREE;
14486 : 6 : gcc_rich_location bad_xobj_parm
14487 : 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
14488 : 6 : error_at (&bad_xobj_parm,
14489 : : "only the first parameter of a member function "
14490 : : "can be declared as an explicit object parameter");
14491 : 6 : }
14492 : 176343312 : if (bad_xobj_parm_encountered && xobj_parm)
14493 : 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14494 : : "valid explicit object parameter declared here");
14495 : 176343312 : }
14496 : :
14497 : 176343312 : if (reqs)
14498 : 3 : error_at (location_of (reqs), "requires-clause on return type");
14499 : 176343312 : reqs = declarator->u.function.requires_clause;
14500 : :
14501 : : /* Say it's a definition only for the CALL_EXPR
14502 : : closest to the identifier. */
14503 : 176343312 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
14504 : :
14505 : : /* Handle a late-specified return type. */
14506 : 176343312 : tree late_return_type = declarator->u.function.late_return_type;
14507 : 176343312 : if (tree auto_node = type_uses_auto (type))
14508 : : {
14509 : 5547881 : if (!late_return_type)
14510 : : {
14511 : 2550568 : if (!funcdecl_p)
14512 : : /* auto (*fp)() = f; is OK. */;
14513 : 2550536 : else if (current_class_type
14514 : 4762661 : && LAMBDA_TYPE_P (current_class_type))
14515 : : /* OK for C++11 lambdas. */;
14516 : 1554390 : else if (cxx_dialect < cxx14)
14517 : : {
14518 : 7 : auto_diagnostic_group d;
14519 : 7 : error_at (typespec_loc, "%qs function uses "
14520 : : "%<auto%> type specifier without "
14521 : : "trailing return type", name);
14522 : 7 : inform (typespec_loc,
14523 : : "deduced return type only available "
14524 : : "with %<-std=c++14%> or %<-std=gnu++14%>");
14525 : 7 : }
14526 : 1554383 : else if (virtualp)
14527 : : {
14528 : 6 : error_at (typespec_loc, "virtual function "
14529 : : "cannot have deduced return type");
14530 : 6 : virtualp = false;
14531 : : }
14532 : : }
14533 : 2997313 : else if (!is_auto (type) && sfk != sfk_conversion)
14534 : : {
14535 : 15 : error_at (typespec_loc, "%qs function with trailing "
14536 : : "return type has %qT as its type rather "
14537 : : "than plain %<auto%>", name, type);
14538 : 270 : return error_mark_node;
14539 : : }
14540 : 2997298 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
14541 : : {
14542 : 9 : if (funcdecl_p)
14543 : 3 : error_at (typespec_loc,
14544 : : "%qs function with trailing return type "
14545 : : "has %<decltype(auto)%> as its type "
14546 : : "rather than plain %<auto%>", name);
14547 : : else
14548 : 6 : error_at (typespec_loc,
14549 : : "invalid use of %<decltype(auto)%>");
14550 : 9 : return error_mark_node;
14551 : : }
14552 : 2997289 : else if (is_constrained_auto (type))
14553 : : {
14554 : 6 : if (funcdecl_p)
14555 : 3 : error_at (typespec_loc,
14556 : : "%qs function with trailing return type "
14557 : : "has constrained %<auto%> type specifier "
14558 : : "rather than plain %<auto%>",
14559 : : name);
14560 : : else
14561 : 3 : error_at (typespec_loc,
14562 : : "invalid use of constrained %<auto%> type");
14563 : 6 : return error_mark_node;
14564 : : }
14565 : 5547851 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
14566 : 5547851 : if (!tmpl)
14567 : 4478577 : if (tree late_auto = type_uses_auto (late_return_type))
14568 : 21866 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
14569 : 4478577 : if (tmpl)
14570 : : {
14571 : 1069278 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
14572 : : {
14573 : 10 : auto_diagnostic_group g;
14574 : 10 : error_at (typespec_loc, "deduced class "
14575 : : "type %qD in function return type",
14576 : 10 : DECL_NAME (tmpl));
14577 : 10 : inform (DECL_SOURCE_LOCATION (tmpl),
14578 : : "%qD declared here", tmpl);
14579 : 10 : return error_mark_node;
14580 : 10 : }
14581 : 1069268 : else if (!late_return_type)
14582 : : {
14583 : 3 : auto_diagnostic_group d;
14584 : 3 : error_at (declarator->id_loc, "deduction guide "
14585 : : "for %qT must have trailing return "
14586 : 3 : "type", TREE_TYPE (tmpl));
14587 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
14588 : : "%qD declared here", tmpl);
14589 : 3 : return error_mark_node;
14590 : 3 : }
14591 : 1069265 : else if (CLASS_TYPE_P (late_return_type)
14592 : 1069265 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
14593 : 2138530 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
14594 : : == tmpl))
14595 : : /* OK */;
14596 : : else
14597 : 0 : error ("trailing return type %qT of deduction guide "
14598 : : "is not a specialization of %qT",
14599 : 0 : late_return_type, TREE_TYPE (tmpl));
14600 : : }
14601 : : }
14602 : 170795431 : else if (late_return_type
14603 : 170795431 : && sfk != sfk_conversion)
14604 : : {
14605 : 37 : if (late_return_type == error_mark_node)
14606 : : return error_mark_node;
14607 : 37 : if (cxx_dialect < cxx11)
14608 : : /* Not using maybe_warn_cpp0x because this should
14609 : : always be an error. */
14610 : 1 : error_at (typespec_loc,
14611 : : "trailing return type only available "
14612 : : "with %<-std=c++11%> or %<-std=gnu++11%>");
14613 : : else
14614 : 36 : error_at (typespec_loc, "%qs function with trailing "
14615 : : "return type not declared with %<auto%> "
14616 : : "type specifier", name);
14617 : 37 : return error_mark_node;
14618 : : }
14619 : 176343232 : if (late_return_type && sfk == sfk_conversion)
14620 : : {
14621 : 14 : error ("a conversion function cannot have a trailing return type");
14622 : 14 : return error_mark_node;
14623 : : }
14624 : 176343218 : type = splice_late_return_type (type, late_return_type);
14625 : 176343218 : if (type == error_mark_node)
14626 : : return error_mark_node;
14627 : :
14628 : 176343142 : if (late_return_type)
14629 : : {
14630 : 2997195 : late_return_type_p = true;
14631 : 2997195 : type_quals = cp_type_quals (type);
14632 : : }
14633 : :
14634 : 176343142 : if (type_quals != TYPE_UNQUALIFIED)
14635 : : {
14636 : : /* It's wrong, for instance, to issue a -Wignored-qualifiers
14637 : : warning for
14638 : : static_assert(!is_same_v<void(*)(), const void(*)()>);
14639 : : because there the qualifier matters. */
14640 : 12313 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
14641 : 232 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
14642 : : "qualifiers ignored on function return type");
14643 : : /* [dcl.fct] "A volatile-qualified return type is
14644 : : deprecated." */
14645 : 12313 : if (type_quals & TYPE_QUAL_VOLATILE)
14646 : 599 : warning_at (typespec_loc, OPT_Wvolatile,
14647 : : "%<volatile%>-qualified return type is "
14648 : : "deprecated");
14649 : :
14650 : : /* We now know that the TYPE_QUALS don't apply to the
14651 : : decl, but to its return type. */
14652 : : type_quals = TYPE_UNQUALIFIED;
14653 : : }
14654 : :
14655 : : /* Error about some types functions can't return. */
14656 : :
14657 : 176343142 : if (TREE_CODE (type) == FUNCTION_TYPE)
14658 : : {
14659 : 30 : error_at (typespec_loc, "%qs declared as function returning "
14660 : : "a function", name);
14661 : 30 : return error_mark_node;
14662 : : }
14663 : 176343112 : if (TREE_CODE (type) == ARRAY_TYPE)
14664 : : {
14665 : 6 : error_at (typespec_loc, "%qs declared as function returning "
14666 : : "an array", name);
14667 : 6 : return error_mark_node;
14668 : : }
14669 : 176343106 : if (constinit_p && funcdecl_p)
14670 : : {
14671 : 15 : error_at (declspecs->locations[ds_constinit],
14672 : : "%<constinit%> on function return type is not "
14673 : : "allowed");
14674 : 15 : return error_mark_node;
14675 : : }
14676 : :
14677 : 176343091 : if (check_decltype_auto (typespec_loc, type))
14678 : 49 : return error_mark_node;
14679 : :
14680 : 176343042 : if (ctype == NULL_TREE
14681 : 176343042 : && decl_context == FIELD
14682 : : && funcdecl_p
14683 : 95023064 : && friendp == 0)
14684 : 90563454 : ctype = current_class_type;
14685 : :
14686 : 176343042 : if (ctype && (sfk == sfk_constructor
14687 : 90563454 : || sfk == sfk_destructor))
14688 : : {
14689 : : /* We are within a class's scope. If our declarator name
14690 : : is the same as the class name, and we are defining
14691 : : a function, then it is a constructor/destructor, and
14692 : : therefore returns a void type. */
14693 : :
14694 : : /* ISO C++ 12.4/2. A destructor may not be declared
14695 : : const or volatile. A destructor may not be static.
14696 : : A destructor may not be declared with ref-qualifier.
14697 : :
14698 : : ISO C++ 12.1. A constructor may not be declared
14699 : : const or volatile. A constructor may not be
14700 : : virtual. A constructor may not be static.
14701 : : A constructor may not be declared with ref-qualifier. */
14702 : 24492860 : if (staticp == 2)
14703 : 9 : error_at (declspecs->locations[ds_storage_class],
14704 : : (flags == DTOR_FLAG)
14705 : : ? G_("destructor cannot be static member "
14706 : : "function")
14707 : : : G_("constructor cannot be static member "
14708 : : "function"));
14709 : 24492860 : if (memfn_quals)
14710 : : {
14711 : 9 : error ((flags == DTOR_FLAG)
14712 : : ? G_("destructors may not be cv-qualified")
14713 : : : G_("constructors may not be cv-qualified"));
14714 : 9 : memfn_quals = TYPE_UNQUALIFIED;
14715 : : }
14716 : :
14717 : 24492860 : if (rqual)
14718 : : {
14719 : 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
14720 : 6 : error ((flags == DTOR_FLAG)
14721 : : ? G_("destructors may not be ref-qualified")
14722 : : : G_("constructors may not be ref-qualified"));
14723 : 6 : rqual = REF_QUAL_NONE;
14724 : : }
14725 : :
14726 : 24492860 : if (decl_context == FIELD
14727 : 24492860 : && !member_function_or_else (ctype,
14728 : : current_class_type,
14729 : : flags))
14730 : 0 : return error_mark_node;
14731 : :
14732 : 24492860 : if (flags != DTOR_FLAG)
14733 : : {
14734 : : /* It's a constructor. */
14735 : 21519009 : if (explicitp == 1)
14736 : 5207948 : explicitp = 2;
14737 : 21519009 : if (virtualp)
14738 : : {
14739 : 3 : permerror (declspecs->locations[ds_virtual],
14740 : : "constructors cannot be declared %<virtual%>");
14741 : 3 : virtualp = 0;
14742 : : }
14743 : 21519009 : if (decl_context == FIELD
14744 : 21519009 : && sfk != sfk_constructor)
14745 : 0 : return error_mark_node;
14746 : : }
14747 : 24492860 : if (decl_context == FIELD)
14748 : 24492860 : staticp = 0;
14749 : : }
14750 : 151850182 : else if (friendp)
14751 : : {
14752 : 4154153 : if (virtualp)
14753 : : {
14754 : : /* Cannot be both friend and virtual. */
14755 : 12 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
14756 : 12 : richloc.add_range (declspecs->locations[ds_friend]);
14757 : 12 : error_at (&richloc, "virtual functions cannot be friends");
14758 : 12 : friendp = 0;
14759 : 12 : }
14760 : 4154153 : if (decl_context == NORMAL)
14761 : 0 : error_at (declarator->id_loc,
14762 : : "friend declaration not in class definition");
14763 : 4154153 : if (current_function_decl && funcdef_flag)
14764 : : {
14765 : 9 : error_at (declarator->id_loc,
14766 : : "cannot define friend function %qs in a local "
14767 : : "class definition", name);
14768 : 9 : friendp = 0;
14769 : : }
14770 : : /* [class.friend]/6: A function can be defined in a friend
14771 : : declaration if the function name is unqualified. */
14772 : 4154153 : if (funcdef_flag && in_namespace)
14773 : : {
14774 : 6 : if (in_namespace == global_namespace)
14775 : 3 : error_at (declarator->id_loc,
14776 : : "friend function definition %qs cannot have "
14777 : : "a name qualified with %<::%>", name);
14778 : : else
14779 : 3 : error_at (declarator->id_loc,
14780 : : "friend function definition %qs cannot have "
14781 : : "a name qualified with %<%D::%>", name,
14782 : : in_namespace);
14783 : : }
14784 : : }
14785 : 147696029 : else if (ctype && sfk == sfk_conversion)
14786 : : {
14787 : 635053 : if (explicitp == 1)
14788 : : {
14789 : 300765 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
14790 : 300765 : explicitp = 2;
14791 : : }
14792 : : }
14793 : 147060976 : else if (sfk == sfk_deduction_guide)
14794 : : {
14795 : 1069265 : if (explicitp == 1)
14796 : 176343042 : explicitp = 2;
14797 : : }
14798 : :
14799 : 176343042 : if (xobj_parm)
14800 : : {
14801 : 27252 : if (!ctype
14802 : 27252 : && decl_context == NORMAL
14803 : 49 : && (in_namespace
14804 : 49 : || !declarator->declarator->u.id.qualifying_scope))
14805 : 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14806 : : "a non-member function cannot have "
14807 : : "an explicit object parameter");
14808 : : else
14809 : : {
14810 : 27248 : if (virtualp)
14811 : : {
14812 : 16 : auto_diagnostic_group d;
14813 : 16 : error_at (declspecs->locations[ds_virtual],
14814 : : "an explicit object member function cannot "
14815 : : "be %<virtual%>");
14816 : 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14817 : : "explicit object parameter declared here");
14818 : 16 : virtualp = false;
14819 : 16 : }
14820 : 27248 : if (staticp >= 2)
14821 : : {
14822 : 2 : auto_diagnostic_group d;
14823 : 2 : error_at (declspecs->locations[ds_storage_class],
14824 : : "an explicit object member function cannot "
14825 : : "be %<static%>");
14826 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
14827 : : "explicit object parameter declared here");
14828 : 2 : }
14829 : 27248 : if (unqualified_id
14830 : 176370284 : && identifier_p (unqualified_id)
14831 : 54490 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14832 : 8 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
14833 : : "%qD cannot be an explicit object member "
14834 : : "function", unqualified_id);
14835 : : }
14836 : : }
14837 : 176343042 : tree pushed_scope = NULL_TREE;
14838 : 176343042 : if (funcdecl_p
14839 : 176343042 : && decl_context != FIELD
14840 : 79192044 : && inner_declarator->u.id.qualifying_scope
14841 : 185481204 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
14842 : 9136836 : pushed_scope
14843 : 9136836 : = push_scope (inner_declarator->u.id.qualifying_scope);
14844 : :
14845 : 176343042 : arg_types = grokparms (declarator->u.function.parameters, &parms);
14846 : :
14847 : 176343042 : if (pushed_scope)
14848 : 9136836 : pop_scope (pushed_scope);
14849 : :
14850 : 176343042 : if (inner_declarator
14851 : 175856003 : && inner_declarator->kind == cdk_id
14852 : 173909651 : && inner_declarator->u.id.sfk == sfk_destructor
14853 : 3146796 : && arg_types != void_list_node)
14854 : : {
14855 : 20 : error_at (declarator->id_loc,
14856 : : "destructors may not have parameters");
14857 : 20 : arg_types = void_list_node;
14858 : 20 : parms = NULL_TREE;
14859 : 20 : is_xobj_member_function = false;
14860 : : }
14861 : :
14862 : 176343042 : type = build_function_type (type, arg_types);
14863 : :
14864 : 176343042 : tree attrs = declarator->std_attributes;
14865 : 176343042 : if (tx_qual)
14866 : : {
14867 : 242 : tree att = build_tree_list (tx_qual, NULL_TREE);
14868 : : /* transaction_safe applies to the type, but
14869 : : transaction_safe_dynamic applies to the function. */
14870 : 242 : if (is_attribute_p ("transaction_safe", tx_qual))
14871 : 213 : attrs = attr_chainon (attrs, att);
14872 : : else
14873 : 29 : returned_attrs = attr_chainon (returned_attrs, att);
14874 : : }
14875 : :
14876 : : /* Actually apply the contract attributes to the declaration. */
14877 : 176344196 : for (tree *p = &attrs; *p;)
14878 : : {
14879 : 1154 : tree l = *p;
14880 : 1154 : if (cxx_contract_attribute_p (l))
14881 : : {
14882 : 667 : *p = TREE_CHAIN (l);
14883 : : /* Intentionally reverse order of contracts so they're
14884 : : reversed back into their lexical order. */
14885 : 667 : TREE_CHAIN (l) = NULL_TREE;
14886 : 667 : returned_attrs = chainon (l, returned_attrs);
14887 : : }
14888 : : else
14889 : 487 : p = &TREE_CHAIN (l);
14890 : : }
14891 : :
14892 : 176343042 : if (attrs)
14893 : : /* [dcl.fct]/2:
14894 : :
14895 : : The optional attribute-specifier-seq appertains to
14896 : : the function type. */
14897 : 475 : cplus_decl_attributes (&type, attrs, 0);
14898 : :
14899 : 176343042 : if (raises)
14900 : 69404690 : type = build_exception_variant (type, raises);
14901 : : }
14902 : 176343042 : break;
14903 : :
14904 : 179441471 : case cdk_pointer:
14905 : 179441471 : case cdk_reference:
14906 : 179441471 : case cdk_ptrmem:
14907 : : /* Filter out pointers-to-references and references-to-references.
14908 : : We can get these if a TYPE_DECL is used. */
14909 : :
14910 : 179441471 : if (TYPE_REF_P (type))
14911 : : {
14912 : 238 : if (declarator->kind != cdk_reference)
14913 : : {
14914 : 3 : error ("cannot declare pointer to %q#T", type);
14915 : 3 : type = TREE_TYPE (type);
14916 : : }
14917 : :
14918 : : /* In C++0x, we allow reference to reference declarations
14919 : : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
14920 : : and template type arguments [14.3.1/4 temp.arg.type]. The
14921 : : check for direct reference to reference declarations, which
14922 : : are still forbidden, occurs below. Reasoning behind the change
14923 : : can be found in DR106, DR540, and the rvalue reference
14924 : : proposals. */
14925 : 235 : else if (cxx_dialect == cxx98)
14926 : : {
14927 : 0 : error ("cannot declare reference to %q#T", type);
14928 : 0 : type = TREE_TYPE (type);
14929 : : }
14930 : : }
14931 : 179441233 : else if (VOID_TYPE_P (type))
14932 : : {
14933 : 4621468 : if (declarator->kind == cdk_reference)
14934 : 4 : error ("cannot declare reference to %q#T", type);
14935 : 4621464 : else if (declarator->kind == cdk_ptrmem)
14936 : 3 : error ("cannot declare pointer to %q#T member", type);
14937 : : }
14938 : :
14939 : : /* We now know that the TYPE_QUALS don't apply to the decl,
14940 : : but to the target of the pointer. */
14941 : 179441471 : type_quals = TYPE_UNQUALIFIED;
14942 : :
14943 : : /* This code used to handle METHOD_TYPE, but I don't think it's
14944 : : possible to get it here anymore. */
14945 : 179441471 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14946 : 179441471 : if (declarator->kind == cdk_ptrmem
14947 : 1194365 : && TREE_CODE (type) == FUNCTION_TYPE)
14948 : : {
14949 : 996002 : memfn_quals |= type_memfn_quals (type);
14950 : 1992004 : type = build_memfn_type (type,
14951 : 996002 : declarator->u.pointer.class_type,
14952 : : memfn_quals,
14953 : : rqual);
14954 : 996002 : if (type == error_mark_node)
14955 : : return error_mark_node;
14956 : :
14957 : : rqual = REF_QUAL_NONE;
14958 : : memfn_quals = TYPE_UNQUALIFIED;
14959 : : }
14960 : :
14961 : 179441468 : if (TREE_CODE (type) == FUNCTION_TYPE
14962 : 179441468 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14963 : 1008527 : || type_memfn_rqual (type) != REF_QUAL_NONE))
14964 : 15 : error (declarator->kind == cdk_reference
14965 : : ? G_("cannot declare reference to qualified function type %qT")
14966 : : : G_("cannot declare pointer to qualified function type %qT"),
14967 : : type);
14968 : :
14969 : : /* When the pointed-to type involves components of variable size,
14970 : : care must be taken to ensure that the size evaluation code is
14971 : : emitted early enough to dominate all the possible later uses
14972 : : and late enough for the variables on which it depends to have
14973 : : been assigned.
14974 : :
14975 : : This is expected to happen automatically when the pointed-to
14976 : : type has a name/declaration of it's own, but special attention
14977 : : is required if the type is anonymous.
14978 : :
14979 : : We handle the NORMAL and FIELD contexts here by inserting a
14980 : : dummy statement that just evaluates the size at a safe point
14981 : : and ensures it is not deferred until e.g. within a deeper
14982 : : conditional context (c++/43555).
14983 : :
14984 : : We expect nothing to be needed here for PARM or TYPENAME.
14985 : : Evaluating the size at this point for TYPENAME would
14986 : : actually be incorrect, as we might be in the middle of an
14987 : : expression with side effects on the pointed-to type size
14988 : : "arguments" prior to the pointer declaration point and the
14989 : : size evaluation could end up prior to the side effects. */
14990 : :
14991 : 179441468 : if (!TYPE_NAME (type)
14992 : 5396762 : && (decl_context == NORMAL || decl_context == FIELD)
14993 : 610116 : && at_function_scope_p ()
14994 : 179482780 : && variably_modified_type_p (type, NULL_TREE))
14995 : : {
14996 : 96 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
14997 : : NULL_TREE, type);
14998 : 96 : add_decl_expr (TYPE_NAME (type));
14999 : : }
15000 : :
15001 : 179441468 : if (declarator->kind == cdk_reference)
15002 : : {
15003 : : /* In C++0x, the type we are creating a reference to might be
15004 : : a typedef which is itself a reference type. In that case,
15005 : : we follow the reference collapsing rules in
15006 : : [7.1.3/8 dcl.typedef] to create the final reference type:
15007 : :
15008 : : "If a typedef TD names a type that is a reference to a type
15009 : : T, an attempt to create the type 'lvalue reference to cv TD'
15010 : : creates the type 'lvalue reference to T,' while an attempt
15011 : : to create the type "rvalue reference to cv TD' creates the
15012 : : type TD."
15013 : : */
15014 : 117867464 : if (VOID_TYPE_P (type))
15015 : : /* We already gave an error. */;
15016 : 117867460 : else if (TYPE_REF_P (type))
15017 : : {
15018 : 235 : if (declarator->u.reference.rvalue_ref)
15019 : : /* Leave type alone. */;
15020 : : else
15021 : 116 : type = cp_build_reference_type (TREE_TYPE (type), false);
15022 : : }
15023 : : else
15024 : 117867225 : type = cp_build_reference_type
15025 : 117867225 : (type, declarator->u.reference.rvalue_ref);
15026 : :
15027 : : /* In C++0x, we need this check for direct reference to
15028 : : reference declarations, which are forbidden by
15029 : : [8.3.2/5 dcl.ref]. Reference to reference declarations
15030 : : are only allowed indirectly through typedefs and template
15031 : : type arguments. Example:
15032 : :
15033 : : void foo(int & &); // invalid ref-to-ref decl
15034 : :
15035 : : typedef int & int_ref;
15036 : : void foo(int_ref &); // valid ref-to-ref decl
15037 : : */
15038 : 117867464 : if (inner_declarator && inner_declarator->kind == cdk_reference)
15039 : 0 : error ("cannot declare reference to %q#T, which is not "
15040 : : "a typedef or a template type argument", type);
15041 : : }
15042 : 61574004 : else if (TREE_CODE (type) == METHOD_TYPE)
15043 : 995999 : type = build_ptrmemfunc_type (build_pointer_type (type));
15044 : 60578005 : else if (declarator->kind == cdk_ptrmem)
15045 : : {
15046 : 198363 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
15047 : : != NAMESPACE_DECL);
15048 : 198363 : if (declarator->u.pointer.class_type == error_mark_node)
15049 : : /* We will already have complained. */
15050 : 3 : type = error_mark_node;
15051 : : else
15052 : 198360 : type = build_ptrmem_type (declarator->u.pointer.class_type,
15053 : : type);
15054 : : }
15055 : : else
15056 : 60379642 : type = build_pointer_type (type);
15057 : :
15058 : : /* Process a list of type modifier keywords (such as
15059 : : const or volatile) that were given inside the `*' or `&'. */
15060 : :
15061 : 179441468 : if (declarator->u.pointer.qualifiers)
15062 : : {
15063 : 8390536 : type
15064 : 8390536 : = cp_build_qualified_type (type,
15065 : : declarator->u.pointer.qualifiers);
15066 : 8390536 : type_quals = cp_type_quals (type);
15067 : : }
15068 : :
15069 : : /* Apply C++11 attributes to the pointer, and not to the
15070 : : type pointed to. This is unlike what is done for GNU
15071 : : attributes above. It is to comply with [dcl.ptr]/1:
15072 : :
15073 : : [the optional attribute-specifier-seq (7.6.1) appertains
15074 : : to the pointer and not to the object pointed to]. */
15075 : 179441468 : if (declarator->std_attributes)
15076 : 157 : cplus_decl_attributes (&type, declarator->std_attributes, 0);
15077 : :
15078 : : ctype = NULL_TREE;
15079 : : break;
15080 : :
15081 : : case cdk_error:
15082 : : break;
15083 : :
15084 : 0 : default:
15085 : 0 : gcc_unreachable ();
15086 : : }
15087 : : }
15088 : :
15089 : 919709250 : id_loc = declarator ? declarator->id_loc : input_location;
15090 : :
15091 : 919709250 : if (innermost_code != cdk_function
15092 : : /* Don't check this if it can be the artifical decltype(auto)
15093 : : we created when building a constraint in a compound-requirement:
15094 : : that the type-constraint is plain is going to be checked in
15095 : : cp_parser_compound_requirement. */
15096 : 919709250 : && decl_context != TYPENAME
15097 : 919709250 : && check_decltype_auto (id_loc, type))
15098 : 19 : return error_mark_node;
15099 : :
15100 : : /* A `constexpr' specifier used in an object declaration declares
15101 : : the object as `const'. */
15102 : 919709231 : if (constexpr_p && innermost_code != cdk_function)
15103 : : {
15104 : : /* DR1688 says that a `constexpr' specifier in combination with
15105 : : `volatile' is valid. */
15106 : :
15107 : 13670816 : if (!TYPE_REF_P (type))
15108 : : {
15109 : 13658076 : type_quals |= TYPE_QUAL_CONST;
15110 : 13658076 : type = cp_build_qualified_type (type, type_quals);
15111 : : }
15112 : : }
15113 : :
15114 : 534332577 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
15115 : 1928074 : && !FUNC_OR_METHOD_TYPE_P (type)
15116 : 920731412 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
15117 : : {
15118 : 0 : error ("template-id %qD used as a declarator",
15119 : : unqualified_id);
15120 : 0 : unqualified_id = dname;
15121 : : }
15122 : :
15123 : : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
15124 : : qualified with a class-name, turn it into a METHOD_TYPE, unless
15125 : : we know that the function is static. We take advantage of this
15126 : : opportunity to do other processing that pertains to entities
15127 : : explicitly declared to be class members. Note that if DECLARATOR
15128 : : is non-NULL, we know it is a cdk_id declarator; otherwise, we
15129 : : would not have exited the loop above. */
15130 : 919709231 : if (declarator
15131 : 534417387 : && declarator->kind == cdk_id
15132 : 534332577 : && declarator->u.id.qualifying_scope
15133 : 929411711 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
15134 : : {
15135 : 9525207 : ctype = declarator->u.id.qualifying_scope;
15136 : 9525207 : ctype = TYPE_MAIN_VARIANT (ctype);
15137 : 9525207 : template_count = num_template_headers_for_class (ctype);
15138 : :
15139 : 9525207 : if (ctype == current_class_type)
15140 : : {
15141 : 54 : if (friendp)
15142 : : {
15143 : 15 : permerror (declspecs->locations[ds_friend],
15144 : : "member functions are implicitly "
15145 : : "friends of their class");
15146 : 15 : friendp = 0;
15147 : : }
15148 : : else
15149 : 39 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
15150 : : ctype, name);
15151 : : }
15152 : 9525153 : else if (/* If the qualifying type is already complete, then we
15153 : : can skip the following checks. */
15154 : 9525153 : !COMPLETE_TYPE_P (ctype)
15155 : 150 : && (/* If the function is being defined, then
15156 : : qualifying type must certainly be complete. */
15157 : : funcdef_flag
15158 : : /* A friend declaration of "T::f" is OK, even if
15159 : : "T" is a template parameter. But, if this
15160 : : function is not a friend, the qualifying type
15161 : : must be a class. */
15162 : 111 : || (!friendp && !CLASS_TYPE_P (ctype))
15163 : : /* For a declaration, the type need not be
15164 : : complete, if either it is dependent (since there
15165 : : is no meaningful definition of complete in that
15166 : : case) or the qualifying class is currently being
15167 : : defined. */
15168 : 123 : || !(dependent_type_p (ctype)
15169 : 24 : || currently_open_class (ctype)))
15170 : : /* Check that the qualifying type is complete. */
15171 : 9525219 : && !complete_type_or_else (ctype, NULL_TREE))
15172 : 66 : return error_mark_node;
15173 : 9525087 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15174 : : {
15175 : 9137073 : if (current_class_type
15176 : 264 : && (!friendp || funcdef_flag || initialized))
15177 : : {
15178 : 59 : error_at (id_loc, funcdef_flag || initialized
15179 : : ? G_("cannot define member function %<%T::%s%> "
15180 : : "within %qT")
15181 : : : G_("cannot declare member function %<%T::%s%> "
15182 : : "within %qT"),
15183 : : ctype, name, current_class_type);
15184 : 49 : return error_mark_node;
15185 : : }
15186 : : }
15187 : 388014 : else if (typedef_p && current_class_type)
15188 : : {
15189 : 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
15190 : : ctype, name, current_class_type);
15191 : 0 : return error_mark_node;
15192 : : }
15193 : : }
15194 : :
15195 : 919709116 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
15196 : 51445271 : ctype = current_class_type;
15197 : :
15198 : : /* Now TYPE has the actual type. */
15199 : :
15200 : 919709116 : if (returned_attrs)
15201 : : {
15202 : 3337 : if (attrlist)
15203 : 3337 : *attrlist = attr_chainon (returned_attrs, *attrlist);
15204 : : else
15205 : : attrlist = &returned_attrs;
15206 : : }
15207 : :
15208 : 919709116 : if (declarator
15209 : 534417272 : && declarator->kind == cdk_id
15210 : 534332462 : && declarator->std_attributes
15211 : 359587 : && attrlist != NULL)
15212 : : {
15213 : : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
15214 : : a declarator-id appertains to the entity that is declared. */
15215 : 359584 : if (declarator->std_attributes != error_mark_node)
15216 : 359566 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
15217 : : else
15218 : : /* We should have already diagnosed the issue (c++/78344). */
15219 : 18 : gcc_assert (seen_error ());
15220 : : }
15221 : :
15222 : : /* Handle parameter packs. */
15223 : 919709116 : if (parameter_pack_p)
15224 : : {
15225 : 2935076 : if (decl_context == PARM)
15226 : : /* Turn the type into a pack expansion.*/
15227 : 2935076 : type = make_pack_expansion (type);
15228 : : else
15229 : 0 : error ("non-parameter %qs cannot be a parameter pack", name);
15230 : : }
15231 : :
15232 : 919709116 : if ((decl_context == FIELD || decl_context == PARM)
15233 : 423009528 : && !processing_template_decl
15234 : 1055375480 : && variably_modified_type_p (type, NULL_TREE))
15235 : : {
15236 : 12 : if (decl_context == FIELD)
15237 : 6 : error_at (id_loc,
15238 : : "data member may not have variably modified type %qT", type);
15239 : : else
15240 : 6 : error_at (id_loc,
15241 : : "parameter may not have variably modified type %qT", type);
15242 : 12 : type = error_mark_node;
15243 : : }
15244 : :
15245 : 919709116 : if (explicitp == 1 || (explicitp && friendp))
15246 : : {
15247 : : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
15248 : : in the declaration of a constructor or conversion function within
15249 : : a class definition. */
15250 : 74 : if (!current_class_type)
15251 : 59 : error_at (declspecs->locations[ds_explicit],
15252 : : "%<explicit%> outside class declaration");
15253 : 15 : else if (friendp)
15254 : 12 : error_at (declspecs->locations[ds_explicit],
15255 : : "%<explicit%> in friend declaration");
15256 : : else
15257 : 3 : error_at (declspecs->locations[ds_explicit],
15258 : : "only declarations of constructors and conversion operators "
15259 : : "can be %<explicit%>");
15260 : : explicitp = 0;
15261 : : }
15262 : :
15263 : 919709116 : if (storage_class == sc_mutable)
15264 : : {
15265 : 146194 : location_t sloc = declspecs->locations[ds_storage_class];
15266 : 146194 : if (decl_context != FIELD || friendp)
15267 : : {
15268 : 30 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
15269 : : name);
15270 : 30 : storage_class = sc_none;
15271 : : }
15272 : 146164 : else if (decl_context == TYPENAME || typedef_p)
15273 : : {
15274 : 0 : error_at (sloc,
15275 : : "non-object member %qs cannot be declared %<mutable%>",
15276 : : name);
15277 : 0 : storage_class = sc_none;
15278 : : }
15279 : 146164 : else if (FUNC_OR_METHOD_TYPE_P (type))
15280 : : {
15281 : 3 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
15282 : : name);
15283 : 3 : storage_class = sc_none;
15284 : : }
15285 : 146161 : else if (staticp)
15286 : : {
15287 : 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
15288 : : name);
15289 : 0 : storage_class = sc_none;
15290 : : }
15291 : 146161 : else if (type_quals & TYPE_QUAL_CONST)
15292 : : {
15293 : 9 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
15294 : : name);
15295 : 9 : storage_class = sc_none;
15296 : : }
15297 : 146152 : else if (TYPE_REF_P (type))
15298 : : {
15299 : 6 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
15300 : : name);
15301 : 6 : storage_class = sc_none;
15302 : : }
15303 : : }
15304 : :
15305 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
15306 : 919709116 : if (typedef_p && decl_context != TYPENAME)
15307 : : {
15308 : 43753536 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
15309 : 43753536 : tree decl;
15310 : :
15311 : 43753536 : if (funcdef_flag)
15312 : : {
15313 : 6 : if (decl_context == NORMAL)
15314 : 3 : error_at (id_loc,
15315 : : "typedef may not be a function definition");
15316 : : else
15317 : 3 : error_at (id_loc,
15318 : : "typedef may not be a member function definition");
15319 : 6 : return error_mark_node;
15320 : : }
15321 : :
15322 : : /* This declaration:
15323 : :
15324 : : typedef void f(int) const;
15325 : :
15326 : : declares a function type which is not a member of any
15327 : : particular class, but which is cv-qualified; for
15328 : : example "f S::*" declares a pointer to a const-qualified
15329 : : member function of S. We record the cv-qualification in the
15330 : : function type. */
15331 : 43753530 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
15332 : : {
15333 : 62 : type = apply_memfn_quals (type, memfn_quals, rqual);
15334 : :
15335 : : /* We have now dealt with these qualifiers. */
15336 : 62 : memfn_quals = TYPE_UNQUALIFIED;
15337 : 62 : rqual = REF_QUAL_NONE;
15338 : : }
15339 : :
15340 : 43753530 : if (type_uses_auto (type))
15341 : : {
15342 : 59 : if (alias_p)
15343 : 19 : error_at (declspecs->locations[ds_type_spec],
15344 : : "%<auto%> not allowed in alias declaration");
15345 : : else
15346 : 40 : error_at (declspecs->locations[ds_type_spec],
15347 : : "typedef declared %<auto%>");
15348 : 59 : type = error_mark_node;
15349 : : }
15350 : :
15351 : 43753530 : if (reqs)
15352 : 3 : error_at (location_of (reqs), "requires-clause on typedef");
15353 : :
15354 : 43753530 : if (id_declarator && declarator->u.id.qualifying_scope)
15355 : : {
15356 : 22 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
15357 : 22 : type = error_mark_node;
15358 : : }
15359 : :
15360 : 43753530 : if (decl_context == FIELD)
15361 : 23810765 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
15362 : : else
15363 : 19942765 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
15364 : :
15365 : 43753530 : if (decl_context != FIELD)
15366 : : {
15367 : 19942765 : if (!current_function_decl)
15368 : 8379842 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
15369 : 11562923 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
15370 : : /* The TYPE_DECL is "abstract" because there will be
15371 : : clones of this constructor/destructor, and there will
15372 : : be copies of this TYPE_DECL generated in those
15373 : : clones. The decloning optimization (for space) may
15374 : : revert this subsequently if it determines that
15375 : : the clones should share a common implementation. */
15376 : 155996 : DECL_ABSTRACT_P (decl) = true;
15377 : :
15378 : 19942765 : set_originating_module (decl);
15379 : : }
15380 : 23810765 : else if (current_class_type
15381 : 23810765 : && constructor_name_p (unqualified_id, current_class_type))
15382 : 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
15383 : : "as enclosing class",
15384 : : unqualified_id);
15385 : :
15386 : : /* If the user declares "typedef struct {...} foo" then the
15387 : : struct will have an anonymous name. Fill that name in now.
15388 : : Nothing can refer to it, so nothing needs know about the name
15389 : : change. */
15390 : 43753530 : if (type != error_mark_node
15391 : 43753449 : && unqualified_id
15392 : 43753446 : && TYPE_NAME (type)
15393 : 40811875 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15394 : 81707552 : && (TYPE_UNNAMED_P (type)
15395 : : /* An enum may have previously used an enumerator for linkage
15396 : : purposes, but we want the typedef name to take priority. */
15397 : 40421326 : || enum_with_enumerator_for_linkage_p (type))
15398 : 390665 : && declspecs->type_definition_p
15399 : 390595 : && attributes_naming_typedef_ok (*attrlist)
15400 : 44144122 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
15401 : 390574 : name_unnamed_type (type, decl);
15402 : :
15403 : 43753530 : if (signed_p
15404 : 43753530 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
15405 : 376619 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
15406 : :
15407 : 43753530 : bad_specifiers (decl, BSP_TYPE, virtualp,
15408 : : memfn_quals != TYPE_UNQUALIFIED,
15409 : : inlinep, friendp, raises != NULL_TREE,
15410 : : declspecs->locations);
15411 : :
15412 : 43753530 : if (alias_p)
15413 : : /* Acknowledge that this was written:
15414 : : `using analias = atype;'. */
15415 : 16718891 : TYPE_DECL_ALIAS_P (decl) = 1;
15416 : :
15417 : 43753530 : return decl;
15418 : : }
15419 : :
15420 : : /* Detect the case of an array type of unspecified size
15421 : : which came, as such, direct from a typedef name.
15422 : : We must copy the type, so that the array's domain can be
15423 : : individually set by the object's initializer. */
15424 : :
15425 : 875955580 : if (type && typedef_type
15426 : 848788570 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
15427 : 876477672 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
15428 : 79 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15429 : :
15430 : : /* Detect where we're using a typedef of function type to declare a
15431 : : function. PARMS will not be set, so we must create it now. */
15432 : :
15433 : 875955580 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
15434 : : {
15435 : 15237 : tree decls = NULL_TREE;
15436 : 15237 : tree args;
15437 : :
15438 : 15237 : for (args = TYPE_ARG_TYPES (type);
15439 : 32940 : args && args != void_list_node;
15440 : 17703 : args = TREE_CHAIN (args))
15441 : : {
15442 : 17703 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
15443 : 17703 : TREE_VALUE (args));
15444 : :
15445 : 17703 : DECL_CHAIN (decl) = decls;
15446 : 17703 : decls = decl;
15447 : : }
15448 : :
15449 : 15237 : parms = nreverse (decls);
15450 : :
15451 : 15237 : if (decl_context != TYPENAME)
15452 : : {
15453 : : /* The qualifiers on the function type become the qualifiers on
15454 : : the non-static member function. */
15455 : 14547 : memfn_quals |= type_memfn_quals (type);
15456 : 14547 : rqual = type_memfn_rqual (type);
15457 : 14547 : type_quals = TYPE_UNQUALIFIED;
15458 : 14547 : raises = TYPE_RAISES_EXCEPTIONS (type);
15459 : : }
15460 : : }
15461 : :
15462 : : /* If this is a type name (such as, in a cast or sizeof),
15463 : : compute the type and return it now. */
15464 : :
15465 : 875954890 : if (decl_context == TYPENAME)
15466 : : {
15467 : : /* Note that here we don't care about type_quals. */
15468 : :
15469 : : /* Special case: "friend class foo" looks like a TYPENAME context. */
15470 : 341330723 : if (friendp)
15471 : : {
15472 : 0 : if (inlinep)
15473 : : {
15474 : 0 : error ("%<inline%> specified for friend class declaration");
15475 : 0 : inlinep = 0;
15476 : : }
15477 : :
15478 : 0 : if (!current_aggr)
15479 : : {
15480 : : /* Don't allow friend declaration without a class-key. */
15481 : 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15482 : 0 : permerror (input_location, "template parameters cannot be friends");
15483 : 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
15484 : 0 : permerror (input_location, "friend declaration requires class-key, "
15485 : : "i.e. %<friend class %T::%D%>",
15486 : 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
15487 : : else
15488 : 0 : permerror (input_location, "friend declaration requires class-key, "
15489 : : "i.e. %<friend %#T%>",
15490 : : type);
15491 : : }
15492 : :
15493 : : /* Only try to do this stuff if we didn't already give up. */
15494 : 0 : if (type != integer_type_node)
15495 : : {
15496 : : /* A friendly class? */
15497 : 0 : if (current_class_type)
15498 : 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
15499 : : /*complain=*/true);
15500 : : else
15501 : 0 : error ("trying to make class %qT a friend of global scope",
15502 : : type);
15503 : :
15504 : 0 : type = void_type_node;
15505 : : }
15506 : : }
15507 : 341330723 : else if (memfn_quals || rqual)
15508 : : {
15509 : 73540 : if (ctype == NULL_TREE
15510 : 73540 : && TREE_CODE (type) == METHOD_TYPE)
15511 : 0 : ctype = TYPE_METHOD_BASETYPE (type);
15512 : :
15513 : 73540 : if (ctype)
15514 : 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
15515 : : /* Core issue #547: need to allow this in template type args.
15516 : : Allow it in general in C++11 for alias-declarations. */
15517 : 73540 : else if ((template_type_arg || cxx_dialect >= cxx11)
15518 : 73538 : && TREE_CODE (type) == FUNCTION_TYPE)
15519 : 73536 : type = apply_memfn_quals (type, memfn_quals, rqual);
15520 : : else
15521 : 4 : error ("invalid qualifiers on non-member function type");
15522 : : }
15523 : :
15524 : 341330723 : if (reqs)
15525 : 3 : error_at (location_of (reqs), "requires-clause on type-id");
15526 : :
15527 : 341330723 : return type;
15528 : : }
15529 : 534624857 : else if (unqualified_id == NULL_TREE && decl_context != PARM
15530 : 705803 : && decl_context != CATCHPARM
15531 : 221611 : && TREE_CODE (type) != UNION_TYPE
15532 : 221372 : && ! bitfield
15533 : 221372 : && innermost_code != cdk_decomp)
15534 : : {
15535 : 9 : error ("abstract declarator %qT used as declaration", type);
15536 : 9 : return error_mark_node;
15537 : : }
15538 : :
15539 : 534624848 : if (!FUNC_OR_METHOD_TYPE_P (type))
15540 : : {
15541 : : /* Only functions may be declared using an operator-function-id. */
15542 : 360753680 : if (dname && IDENTIFIER_ANY_OP_P (dname))
15543 : : {
15544 : 8 : error_at (id_loc, "declaration of %qD as non-function", dname);
15545 : 8 : return error_mark_node;
15546 : : }
15547 : :
15548 : 360753672 : if (reqs)
15549 : 15 : error_at (location_of (reqs),
15550 : : "requires-clause on declaration of non-function type %qT",
15551 : : type);
15552 : : }
15553 : :
15554 : : /* We don't check parameter types here because we can emit a better
15555 : : error message later. */
15556 : 534624840 : if (decl_context != PARM)
15557 : : {
15558 : 257778153 : type = check_var_type (unqualified_id, type, id_loc);
15559 : 257778153 : if (type == error_mark_node)
15560 : : return error_mark_node;
15561 : : }
15562 : :
15563 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
15564 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
15565 : :
15566 : 534624758 : if (decl_context == PARM || decl_context == CATCHPARM)
15567 : : {
15568 : 277344950 : if (ctype || in_namespace)
15569 : 0 : error ("cannot use %<::%> in parameter declaration");
15570 : :
15571 : 277344950 : tree auto_node = type_uses_auto (type);
15572 : 277344950 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
15573 : : {
15574 : 119 : bool err_p = true;
15575 : 119 : if (cxx_dialect >= cxx14)
15576 : : {
15577 : 210 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
15578 : 3 : error_at (typespec_loc,
15579 : : "cannot declare a parameter with %<decltype(auto)%>");
15580 : 108 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15581 : : {
15582 : 4 : auto_diagnostic_group g;
15583 : 4 : gcc_rich_location richloc (typespec_loc);
15584 : 4 : richloc.add_fixit_insert_after ("<>");
15585 : 4 : error_at (&richloc,
15586 : : "missing template argument list after %qE; template "
15587 : : "placeholder not permitted in parameter", c);
15588 : 4 : if (decl_context == PARM && cxx_dialect >= cxx20)
15589 : 3 : inform (typespec_loc, "or use %<auto%> for an "
15590 : : "abbreviated function template");
15591 : 4 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
15592 : 4 : }
15593 : 104 : else if (decl_context == CATCHPARM || template_parm_flag)
15594 : 14 : error_at (typespec_loc,
15595 : : "%<auto%> parameter not permitted in this context");
15596 : : else
15597 : : /* Do not issue an error while tentatively parsing a function
15598 : : parameter: for T t(auto(a), 42);, when we just saw the 1st
15599 : : parameter, we don't know yet that this construct won't be
15600 : : a function declaration. Defer the checking to
15601 : : cp_parser_parameter_declaration_clause. */
15602 : : err_p = false;
15603 : : }
15604 : : else
15605 : 8 : error_at (typespec_loc, "parameter declared %<auto%>");
15606 : 29 : if (err_p)
15607 : 29 : type = error_mark_node;
15608 : : }
15609 : :
15610 : : /* A parameter declared as an array of T is really a pointer to T.
15611 : : One declared as a function is really a pointer to a function.
15612 : : One declared as a member is really a pointer to member. */
15613 : :
15614 : 277344950 : if (TREE_CODE (type) == ARRAY_TYPE)
15615 : : {
15616 : : /* Transfer const-ness of array into that of type pointed to. */
15617 : 733893 : type = build_pointer_type (TREE_TYPE (type));
15618 : 733893 : type_quals = TYPE_UNQUALIFIED;
15619 : 733893 : array_parameter_p = true;
15620 : : }
15621 : 276611057 : else if (TREE_CODE (type) == FUNCTION_TYPE)
15622 : 106619 : type = build_pointer_type (type);
15623 : : }
15624 : :
15625 : 127722954 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
15626 : : /* Don't convert xobj member functions to METHOD_TYPE. */
15627 : 99700311 : && !is_xobj_member_function
15628 : 625719015 : && !(unqualified_id
15629 : 182175944 : && identifier_p (unqualified_id)
15630 : 91093897 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
15631 : : {
15632 : 91082047 : cp_cv_quals real_quals = memfn_quals;
15633 : 91082047 : if (cxx_dialect < cxx14 && constexpr_p
15634 : 653404 : && sfk != sfk_constructor && sfk != sfk_destructor)
15635 : 3798 : real_quals |= TYPE_QUAL_CONST;
15636 : 91082047 : type = build_memfn_type (type, ctype, real_quals, rqual);
15637 : : }
15638 : :
15639 : 534624758 : {
15640 : 534624758 : tree decl = NULL_TREE;
15641 : :
15642 : 534624758 : if (decl_context == PARM)
15643 : : {
15644 : 276846687 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
15645 : 276846687 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
15646 : :
15647 : 276846687 : bad_specifiers (decl, BSP_PARM, virtualp,
15648 : : memfn_quals != TYPE_UNQUALIFIED,
15649 : : inlinep, friendp, raises != NULL_TREE,
15650 : : declspecs->locations);
15651 : : }
15652 : 257778071 : else if (decl_context == FIELD)
15653 : : {
15654 : 122352057 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
15655 : 17143901 : if (tree auto_node = type_uses_auto (type))
15656 : : {
15657 : 19 : location_t tloc = declspecs->locations[ds_type_spec];
15658 : 19 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15659 : 4 : error_at (tloc, "invalid use of template-name %qE without an "
15660 : : "argument list",
15661 : 4 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
15662 : : else
15663 : 15 : error_at (tloc, "non-static data member declared with "
15664 : : "placeholder %qT", auto_node);
15665 : 19 : type = error_mark_node;
15666 : : }
15667 : :
15668 : : /* The C99 flexible array extension. */
15669 : 103282491 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
15670 : 123458093 : && TYPE_DOMAIN (type) == NULL_TREE)
15671 : : {
15672 : 1395 : if (ctype
15673 : 1395 : && (TREE_CODE (ctype) == UNION_TYPE
15674 : 1395 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
15675 : 84 : pedwarn (id_loc, OPT_Wpedantic,
15676 : : "flexible array member in union is a GCC extension");
15677 : :
15678 : : else
15679 : : {
15680 : : /* Array is a flexible member. */
15681 : 1311 : if (name)
15682 : 1311 : pedwarn (id_loc, OPT_Wpedantic,
15683 : : "ISO C++ forbids flexible array member %qs", name);
15684 : : else
15685 : : pedwarn (input_location, OPT_Wpedantic,
15686 : : "ISO C++ forbids flexible array members");
15687 : :
15688 : : /* Flexible array member has a null domain. */
15689 : 1311 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
15690 : : }
15691 : : }
15692 : :
15693 : 122352057 : if (type == error_mark_node)
15694 : : {
15695 : : /* Happens when declaring arrays of sizes which
15696 : : are error_mark_node, for example. */
15697 : 19 : decl = NULL_TREE;
15698 : : }
15699 : 122352038 : else if (in_namespace && !friendp)
15700 : : {
15701 : : /* Something like struct S { int N::j; }; */
15702 : 6 : error_at (id_loc, "invalid use of %<::%>");
15703 : 6 : return error_mark_node;
15704 : : }
15705 : 122352032 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
15706 : : {
15707 : 94717383 : int publicp = 0;
15708 : 94717383 : tree function_context;
15709 : :
15710 : 94717383 : if (friendp == 0)
15711 : : {
15712 : : /* This should never happen in pure C++ (the check
15713 : : could be an assert). It could happen in
15714 : : Objective-C++ if someone writes invalid code that
15715 : : uses a function declaration for an instance
15716 : : variable or property (instance variables and
15717 : : properties are parsed as FIELD_DECLs, but they are
15718 : : part of an Objective-C class, not a C++ class).
15719 : : That code is invalid and is caught by this
15720 : : check. */
15721 : 90563287 : if (!ctype)
15722 : : {
15723 : 0 : error ("declaration of function %qD in invalid context",
15724 : : unqualified_id);
15725 : 0 : return error_mark_node;
15726 : : }
15727 : :
15728 : : /* ``A union may [ ... ] not [ have ] virtual functions.''
15729 : : ARM 9.5 */
15730 : 90563287 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
15731 : : {
15732 : 3 : error_at (declspecs->locations[ds_virtual],
15733 : : "function %qD declared %<virtual%> inside a union",
15734 : : unqualified_id);
15735 : 3 : return error_mark_node;
15736 : : }
15737 : :
15738 : 4054430 : if (virtualp
15739 : 98771810 : && identifier_p (unqualified_id)
15740 : 4054430 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15741 : : {
15742 : 9 : error_at (declspecs->locations[ds_virtual],
15743 : : "%qD cannot be declared %<virtual%>, since it "
15744 : : "is always static", unqualified_id);
15745 : 9 : virtualp = 0;
15746 : : }
15747 : : }
15748 : :
15749 : : /* Check that the name used for a destructor makes sense. */
15750 : 94717380 : if (sfk == sfk_destructor)
15751 : : {
15752 : 2973887 : tree uqname = id_declarator->u.id.unqualified_name;
15753 : :
15754 : 2973887 : if (!ctype)
15755 : : {
15756 : 12 : gcc_assert (friendp);
15757 : 12 : error_at (id_loc, "expected qualified name in friend "
15758 : : "declaration for destructor %qD", uqname);
15759 : 12 : return error_mark_node;
15760 : : }
15761 : :
15762 : 2973875 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
15763 : : {
15764 : 9 : error_at (id_loc, "declaration of %qD as member of %qT",
15765 : : uqname, ctype);
15766 : 9 : return error_mark_node;
15767 : : }
15768 : 2973866 : if (concept_p)
15769 : : {
15770 : 9 : error_at (declspecs->locations[ds_concept],
15771 : : "a destructor cannot be %qs", "concept");
15772 : 9 : return error_mark_node;
15773 : : }
15774 : 2973857 : if (constexpr_p && cxx_dialect < cxx20)
15775 : : {
15776 : 6 : error_at (declspecs->locations[ds_constexpr],
15777 : : "%<constexpr%> destructors only available"
15778 : : " with %<-std=c++20%> or %<-std=gnu++20%>");
15779 : 6 : return error_mark_node;
15780 : : }
15781 : 2973851 : if (consteval_p)
15782 : : {
15783 : 3 : error_at (declspecs->locations[ds_consteval],
15784 : : "a destructor cannot be %qs", "consteval");
15785 : 3 : return error_mark_node;
15786 : : }
15787 : : }
15788 : 91743493 : else if (sfk == sfk_constructor && friendp && !ctype)
15789 : : {
15790 : 0 : error ("expected qualified name in friend declaration "
15791 : : "for constructor %qD",
15792 : 0 : id_declarator->u.id.unqualified_name);
15793 : 0 : return error_mark_node;
15794 : : }
15795 : 94717341 : if (sfk == sfk_constructor)
15796 : 21519033 : if (concept_p)
15797 : : {
15798 : 9 : error_at (declspecs->locations[ds_concept],
15799 : : "a constructor cannot be %<concept%>");
15800 : 9 : return error_mark_node;
15801 : : }
15802 : 73198308 : if (concept_p)
15803 : : {
15804 : 9 : error_at (declspecs->locations[ds_concept],
15805 : : "a concept cannot be a member function");
15806 : 9 : concept_p = false;
15807 : : }
15808 : 94717323 : else if (consteval_p
15809 : 94908287 : && identifier_p (unqualified_id)
15810 : 94908278 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15811 : : {
15812 : 9 : error_at (declspecs->locations[ds_consteval],
15813 : : "%qD cannot be %qs", unqualified_id, "consteval");
15814 : 9 : consteval_p = false;
15815 : : }
15816 : :
15817 : 94717332 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15818 : : {
15819 : 36840 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
15820 : 36840 : if (variable_template_p (tmpl))
15821 : : {
15822 : 3 : auto_diagnostic_group d;
15823 : 3 : error_at (id_loc, "specialization of variable template "
15824 : : "%qD declared as function", tmpl);
15825 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
15826 : : "variable template declared here");
15827 : 3 : return error_mark_node;
15828 : 3 : }
15829 : : }
15830 : :
15831 : : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
15832 : 94717329 : function_context
15833 : : = (ctype != NULL_TREE
15834 : 94717329 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
15835 : 189434658 : publicp = ((! friendp || ! staticp)
15836 : 94717329 : && function_context == NULL_TREE);
15837 : :
15838 : 94717329 : decl = grokfndecl (ctype, type,
15839 : 94717329 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
15840 : : ? unqualified_id : dname,
15841 : : parms,
15842 : : unqualified_id,
15843 : : declspecs,
15844 : : reqs,
15845 : : virtualp, flags, memfn_quals, rqual, raises,
15846 : : friendp ? -1 : 0, friendp, publicp,
15847 : 94717329 : inlinep | (2 * constexpr_p) | (4 * concept_p)
15848 : 94717329 : | (8 * consteval_p),
15849 : : initialized == SD_DELETED,
15850 : : is_xobj_member_function, sfk,
15851 : : funcdef_flag, late_return_type_p,
15852 : : template_count, in_namespace,
15853 : : attrlist, id_loc);
15854 : 94717329 : decl = set_virt_specifiers (decl, virt_specifiers);
15855 : 94717329 : if (decl == NULL_TREE)
15856 : 98 : return error_mark_node;
15857 : : #if 0
15858 : : /* This clobbers the attrs stored in `decl' from `attrlist'. */
15859 : : /* The decl and setting of decl_attr is also turned off. */
15860 : : decl = build_decl_attribute_variant (decl, decl_attr);
15861 : : #endif
15862 : :
15863 : : /* [class.conv.ctor]
15864 : :
15865 : : A constructor declared without the function-specifier
15866 : : explicit that can be called with a single parameter
15867 : : specifies a conversion from the type of its first
15868 : : parameter to the type of its class. Such a constructor
15869 : : is called a converting constructor. */
15870 : 94717231 : if (explicitp == 2)
15871 : 5508713 : DECL_NONCONVERTING_P (decl) = 1;
15872 : :
15873 : 94717231 : if (declspecs->explicit_specifier)
15874 : 156844 : store_explicit_specifier (decl, declspecs->explicit_specifier);
15875 : : }
15876 : 27634649 : else if (!staticp
15877 : 27634649 : && ((current_class_type
15878 : 17143899 : && same_type_p (TYPE_MAIN_VARIANT (type),
15879 : : current_class_type))
15880 : 17143803 : || (!dependent_type_p (type)
15881 : 10605578 : && !COMPLETE_TYPE_P (complete_type (type))
15882 : 1443 : && (!complete_or_array_type_p (type)
15883 : 1368 : || initialized == SD_UNINITIALIZED))))
15884 : : {
15885 : 1506 : if (TREE_CODE (type) != ARRAY_TYPE
15886 : 1506 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
15887 : : {
15888 : 171 : if (unqualified_id)
15889 : : {
15890 : 126 : auto_diagnostic_group d;
15891 : 126 : error_at (id_loc, "field %qD has incomplete type %qT",
15892 : : unqualified_id, type);
15893 : 126 : cxx_incomplete_type_inform (strip_array_types (type));
15894 : 126 : }
15895 : : else
15896 : 45 : error ("name %qT has incomplete type", type);
15897 : :
15898 : 171 : type = error_mark_node;
15899 : 171 : decl = NULL_TREE;
15900 : : }
15901 : : }
15902 : 44775524 : else if (!verify_type_context (input_location,
15903 : : staticp
15904 : : ? TCTX_STATIC_STORAGE
15905 : : : TCTX_FIELD, type))
15906 : : {
15907 : 0 : type = error_mark_node;
15908 : 0 : decl = NULL_TREE;
15909 : : }
15910 : : else
15911 : : {
15912 : 27633137 : if (friendp)
15913 : : {
15914 : 14 : if (unqualified_id)
15915 : 11 : error_at (id_loc,
15916 : : "%qE is neither function nor member function; "
15917 : : "cannot be declared friend", unqualified_id);
15918 : : else
15919 : 3 : error ("unnamed field is neither function nor member "
15920 : : "function; cannot be declared friend");
15921 : 14 : return error_mark_node;
15922 : : }
15923 : 27633123 : decl = NULL_TREE;
15924 : : }
15925 : :
15926 : 122351879 : if (friendp)
15927 : : {
15928 : : /* Packages tend to use GNU attributes on friends, so we only
15929 : : warn for standard attributes. */
15930 : 4154067 : if (attrlist
15931 : 4154067 : && !funcdef_flag
15932 : 1269222 : && cxx11_attribute_p (*attrlist)
15933 : 4154096 : && !all_attributes_are_contracts_p (*attrlist))
15934 : : {
15935 : 27 : *attrlist = NULL_TREE;
15936 : 27 : auto_diagnostic_group d;
15937 : 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
15938 : 27 : inform (id_loc, "an attribute that appertains to a friend "
15939 : : "declaration that is not a definition is ignored");
15940 : 27 : }
15941 : : /* Friends are treated specially. */
15942 : 4154067 : if (ctype == current_class_type)
15943 : : ; /* We already issued a permerror. */
15944 : 4154067 : else if (decl && DECL_NAME (decl))
15945 : : {
15946 : 4154064 : set_originating_module (decl, true);
15947 : :
15948 : 4154064 : if (initialized)
15949 : : /* Kludge: We need funcdef_flag to be true in do_friend for
15950 : : in-class defaulted functions, but that breaks grokfndecl.
15951 : : So set it here. */
15952 : 54366 : funcdef_flag = true;
15953 : :
15954 : 4154064 : cplus_decl_attributes (&decl, *attrlist, 0);
15955 : 4154064 : *attrlist = NULL_TREE;
15956 : :
15957 : 4154064 : tree scope = ctype ? ctype : in_namespace;
15958 : 4154064 : decl = do_friend (scope, unqualified_id, decl,
15959 : : flags, funcdef_flag);
15960 : 4154064 : return decl;
15961 : : }
15962 : : else
15963 : 3 : return error_mark_node;
15964 : : }
15965 : :
15966 : : /* Structure field. It may not be a function, except for C++. */
15967 : :
15968 : 118197812 : if (decl == NULL_TREE)
15969 : : {
15970 : 27634645 : if (staticp)
15971 : : {
15972 : : /* C++ allows static class members. All other work
15973 : : for this is done by grokfield. */
15974 : 10490750 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
15975 : : dname, type);
15976 : 10490750 : if (unqualified_id
15977 : 10490747 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
15978 : : {
15979 : 83629 : decl = check_explicit_specialization (unqualified_id, decl,
15980 : : template_count,
15981 : : concept_p * 8);
15982 : 83629 : if (decl == error_mark_node)
15983 : : return error_mark_node;
15984 : : }
15985 : 10490750 : set_linkage_for_static_data_member (decl);
15986 : 10490750 : if (concept_p)
15987 : 15 : error_at (declspecs->locations[ds_concept],
15988 : : "static data member %qE declared %qs",
15989 : : unqualified_id, "concept");
15990 : 10490735 : else if (constexpr_p && !initialized)
15991 : : {
15992 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15993 : : "%<constexpr%> static data member %qD must "
15994 : : "have an initializer", decl);
15995 : 9 : constexpr_p = false;
15996 : : }
15997 : 10490750 : if (consteval_p)
15998 : 3 : error_at (declspecs->locations[ds_consteval],
15999 : : "static data member %qE declared %qs",
16000 : : unqualified_id, "consteval");
16001 : :
16002 : 10490750 : if (inlinep)
16003 : 10961 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16004 : :
16005 : 10490750 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
16006 : 10490750 : && !(cxx_dialect >= cxx17 && constexpr_p))
16007 : : /* Even if there is an in-class initialization, DECL
16008 : : is considered undefined until an out-of-class
16009 : : definition is provided, unless this is an inline
16010 : : variable. */
16011 : 1724555 : DECL_EXTERNAL (decl) = 1;
16012 : :
16013 : 10490750 : if (thread_p)
16014 : : {
16015 : 414 : CP_DECL_THREAD_LOCAL_P (decl) = true;
16016 : 414 : if (!processing_template_decl)
16017 : 374 : set_decl_tls_model (decl, decl_default_tls_model (decl));
16018 : 414 : if (declspecs->gnu_thread_keyword_p)
16019 : 31 : SET_DECL_GNU_TLS_P (decl);
16020 : : }
16021 : :
16022 : : /* Set the constraints on the declaration. */
16023 : 10490750 : bool memtmpl = (current_template_depth
16024 : 10490750 : > template_class_depth (current_class_type));
16025 : 10490750 : if (memtmpl)
16026 : : {
16027 : 311468 : tree ci = current_template_constraints ();
16028 : 311468 : set_constraints (decl, ci);
16029 : : }
16030 : : }
16031 : : else
16032 : : {
16033 : 17143895 : if (concept_p)
16034 : : {
16035 : 12 : error_at (declspecs->locations[ds_concept],
16036 : : "non-static data member %qE declared %qs",
16037 : : unqualified_id, "concept");
16038 : 12 : concept_p = false;
16039 : 12 : constexpr_p = false;
16040 : : }
16041 : 17143883 : else if (constexpr_p)
16042 : : {
16043 : 9 : error_at (declspecs->locations[ds_constexpr],
16044 : : "non-static data member %qE declared %qs",
16045 : : unqualified_id, "constexpr");
16046 : 9 : constexpr_p = false;
16047 : : }
16048 : 17143895 : if (constinit_p)
16049 : : {
16050 : 6 : error_at (declspecs->locations[ds_constinit],
16051 : : "non-static data member %qE declared %qs",
16052 : : unqualified_id, "constinit");
16053 : 6 : constinit_p = false;
16054 : : }
16055 : 17143895 : if (consteval_p)
16056 : : {
16057 : 3 : error_at (declspecs->locations[ds_consteval],
16058 : : "non-static data member %qE declared %qs",
16059 : : unqualified_id, "consteval");
16060 : 3 : consteval_p = false;
16061 : : }
16062 : 17143895 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
16063 : 17143895 : DECL_NONADDRESSABLE_P (decl) = bitfield;
16064 : 17143895 : if (bitfield && !unqualified_id)
16065 : 136544 : DECL_PADDING_P (decl) = 1;
16066 : :
16067 : 17143895 : if (storage_class == sc_mutable)
16068 : : {
16069 : 146146 : DECL_MUTABLE_P (decl) = 1;
16070 : 146146 : storage_class = sc_none;
16071 : : }
16072 : :
16073 : 17143895 : if (initialized)
16074 : : {
16075 : : /* An attempt is being made to initialize a non-static
16076 : : member. This is new in C++11. */
16077 : 1217695 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
16078 : :
16079 : : /* If this has been parsed with static storage class, but
16080 : : errors forced staticp to be cleared, ensure NSDMI is
16081 : : not present. */
16082 : 1217695 : if (declspecs->storage_class == sc_static)
16083 : 3 : DECL_INITIAL (decl) = error_mark_node;
16084 : : }
16085 : : }
16086 : :
16087 : 44778540 : bad_specifiers (decl, BSP_FIELD, virtualp,
16088 : : memfn_quals != TYPE_UNQUALIFIED,
16089 : : staticp ? false : inlinep, friendp,
16090 : : raises != NULL_TREE,
16091 : : declspecs->locations);
16092 : : }
16093 : : }
16094 : 135426014 : else if (FUNC_OR_METHOD_TYPE_P (type))
16095 : : {
16096 : 79047160 : tree original_name;
16097 : 79047160 : int publicp = 0;
16098 : :
16099 : 79047160 : if (!unqualified_id)
16100 : 0 : return error_mark_node;
16101 : :
16102 : 79047160 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
16103 : : original_name = dname;
16104 : : else
16105 : 78178113 : original_name = unqualified_id;
16106 : : // FIXME:gcc_assert (original_name == dname);
16107 : :
16108 : 79047160 : if (storage_class == sc_auto)
16109 : 1 : error_at (declspecs->locations[ds_storage_class],
16110 : : "storage class %<auto%> invalid for function %qs", name);
16111 : 79047159 : else if (storage_class == sc_register)
16112 : 7 : error_at (declspecs->locations[ds_storage_class],
16113 : : "storage class %<register%> invalid for function %qs",
16114 : : name);
16115 : 79047152 : else if (thread_p)
16116 : : {
16117 : 12 : if (declspecs->gnu_thread_keyword_p)
16118 : 12 : error_at (declspecs->locations[ds_thread],
16119 : : "storage class %<__thread%> invalid for function %qs",
16120 : : name);
16121 : : else
16122 : 0 : error_at (declspecs->locations[ds_thread],
16123 : : "storage class %<thread_local%> invalid for "
16124 : : "function %qs", name);
16125 : : }
16126 : 79047140 : else if (constinit_p)
16127 : 3 : error_at (declspecs->locations[ds_constinit],
16128 : : "%<constinit%> specifier invalid for function %qs", name);
16129 : :
16130 : 79047160 : if (virt_specifiers)
16131 : 6 : error ("virt-specifiers in %qs not allowed outside a class "
16132 : : "definition", name);
16133 : : /* Function declaration not at top level.
16134 : : Storage classes other than `extern' are not allowed
16135 : : and `extern' makes no difference. */
16136 : 79047160 : if (! toplevel_bindings_p ()
16137 : 36496 : && (storage_class == sc_static
16138 : 36487 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
16139 : 79047172 : && pedantic)
16140 : : {
16141 : 9 : if (storage_class == sc_static)
16142 : 6 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
16143 : : "%<static%> specifier invalid for function %qs "
16144 : : "declared out of global scope", name);
16145 : : else
16146 : 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
16147 : : "%<inline%> specifier invalid for function %qs "
16148 : : "declared out of global scope", name);
16149 : : }
16150 : :
16151 : 79047160 : if (ctype == NULL_TREE)
16152 : : {
16153 : 69910357 : if (virtualp)
16154 : : {
16155 : 0 : error ("virtual non-class function %qs", name);
16156 : 0 : virtualp = 0;
16157 : : }
16158 : 69910357 : else if (sfk == sfk_constructor
16159 : 69910357 : || sfk == sfk_destructor)
16160 : : {
16161 : 3 : error (funcdef_flag
16162 : : ? G_("%qs defined in a non-class scope")
16163 : : : G_("%qs declared in a non-class scope"), name);
16164 : 3 : sfk = sfk_none;
16165 : : }
16166 : : }
16167 : 79047160 : if (consteval_p
16168 : 79089461 : && identifier_p (unqualified_id)
16169 : 79089461 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
16170 : : {
16171 : 9 : error_at (declspecs->locations[ds_consteval],
16172 : : "%qD cannot be %qs", unqualified_id, "consteval");
16173 : 9 : consteval_p = false;
16174 : : }
16175 : :
16176 : : /* Record whether the function is public. */
16177 : 79047160 : publicp = (ctype != NULL_TREE
16178 : 79047160 : || storage_class != sc_static);
16179 : :
16180 : 158094320 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
16181 : : declspecs,
16182 : : reqs, virtualp, flags, memfn_quals, rqual, raises,
16183 : : 1, friendp,
16184 : : publicp,
16185 : 79047160 : inlinep | (2 * constexpr_p) | (4 * concept_p)
16186 : 79047160 : | (8 * consteval_p),
16187 : : initialized == SD_DELETED,
16188 : : is_xobj_member_function, sfk,
16189 : : funcdef_flag,
16190 : : late_return_type_p,
16191 : : template_count, in_namespace, attrlist,
16192 : : id_loc);
16193 : 79047160 : if (decl == NULL_TREE)
16194 : 506 : return error_mark_node;
16195 : :
16196 : 79046654 : if (explicitp == 2)
16197 : 5186 : DECL_NONCONVERTING_P (decl) = 1;
16198 : 79046654 : if (staticp == 1)
16199 : : {
16200 : 108344 : int invalid_static = 0;
16201 : :
16202 : : /* Don't allow a static member function in a class, and forbid
16203 : : declaring main to be static. */
16204 : 108344 : if (TREE_CODE (type) == METHOD_TYPE)
16205 : : {
16206 : 0 : permerror (input_location, "cannot declare member function %qD to have "
16207 : : "static linkage", decl);
16208 : 0 : invalid_static = 1;
16209 : : }
16210 : 108344 : else if (current_function_decl)
16211 : : {
16212 : : /* 7.1.1: There can be no static function declarations within a
16213 : : block. */
16214 : 9 : error_at (declspecs->locations[ds_storage_class],
16215 : : "cannot declare static function inside another function");
16216 : 9 : invalid_static = 1;
16217 : : }
16218 : :
16219 : 9 : if (invalid_static)
16220 : : {
16221 : 79046654 : staticp = 0;
16222 : : storage_class = sc_none;
16223 : : }
16224 : : }
16225 : 79046654 : if (declspecs->explicit_specifier)
16226 : 3 : store_explicit_specifier (decl, declspecs->explicit_specifier);
16227 : : }
16228 : : else
16229 : : {
16230 : : /* It's a variable. */
16231 : :
16232 : : /* An uninitialized decl with `extern' is a reference. */
16233 : 112369716 : decl = grokvardecl (type, dname, unqualified_id,
16234 : : declspecs,
16235 : : initialized,
16236 : : type_quals,
16237 : : inlinep,
16238 : : concept_p,
16239 : : template_count,
16240 : : ctype ? ctype : in_namespace,
16241 : : id_loc);
16242 : 56378854 : if (decl == NULL_TREE)
16243 : 49 : return error_mark_node;
16244 : :
16245 : 56378805 : bad_specifiers (decl, BSP_VAR, virtualp,
16246 : : memfn_quals != TYPE_UNQUALIFIED,
16247 : : inlinep, friendp, raises != NULL_TREE,
16248 : : declspecs->locations);
16249 : :
16250 : 56378805 : if (ctype)
16251 : : {
16252 : 387986 : DECL_CONTEXT (decl) = ctype;
16253 : 387986 : if (staticp == 1)
16254 : : {
16255 : 6 : permerror (declspecs->locations[ds_storage_class],
16256 : : "%<static%> may not be used when defining "
16257 : : "(as opposed to declaring) a static data member");
16258 : 6 : staticp = 0;
16259 : 6 : storage_class = sc_none;
16260 : : }
16261 : 387986 : if (storage_class == sc_register && TREE_STATIC (decl))
16262 : : {
16263 : 0 : error ("static member %qD declared %<register%>", decl);
16264 : 0 : storage_class = sc_none;
16265 : : }
16266 : 387986 : if (storage_class == sc_extern && pedantic)
16267 : : {
16268 : 0 : pedwarn (input_location, OPT_Wpedantic,
16269 : : "cannot explicitly declare member %q#D to have "
16270 : : "extern linkage", decl);
16271 : 0 : storage_class = sc_none;
16272 : : }
16273 : : }
16274 : 55990819 : else if (constexpr_p && DECL_EXTERNAL (decl))
16275 : : {
16276 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
16277 : : "declaration of %<constexpr%> variable %qD "
16278 : : "is not a definition", decl);
16279 : 12 : constexpr_p = false;
16280 : : }
16281 : 56378805 : if (consteval_p)
16282 : : {
16283 : 13 : error_at (DECL_SOURCE_LOCATION (decl),
16284 : : "a variable cannot be declared %<consteval%>");
16285 : 13 : consteval_p = false;
16286 : : }
16287 : :
16288 : 56378805 : if (inlinep)
16289 : 3931440 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
16290 : 56378805 : if (innermost_code == cdk_decomp)
16291 : : {
16292 : 84810 : gcc_assert (declarator && declarator->kind == cdk_decomp);
16293 : 84810 : DECL_SOURCE_LOCATION (decl) = id_loc;
16294 : 84810 : DECL_ARTIFICIAL (decl) = 1;
16295 : 84810 : fit_decomposition_lang_decl (decl, NULL_TREE);
16296 : : }
16297 : : }
16298 : :
16299 : 530469958 : if (VAR_P (decl) && !initialized)
16300 : 6798445 : if (tree auto_node = type_uses_auto (type))
16301 : 94 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16302 : : {
16303 : 18 : location_t loc = declspecs->locations[ds_type_spec];
16304 : 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
16305 : 18 : TREE_TYPE (decl) = error_mark_node;
16306 : : }
16307 : :
16308 : 530469958 : if (storage_class == sc_extern && initialized && !funcdef_flag)
16309 : : {
16310 : 258 : if (toplevel_bindings_p ())
16311 : : {
16312 : : /* It's common practice (and completely valid) to have a const
16313 : : be initialized and declared extern. */
16314 : 234 : if (!(type_quals & TYPE_QUAL_CONST))
16315 : 19 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
16316 : : "%qs initialized and declared %<extern%>", name);
16317 : : }
16318 : : else
16319 : : {
16320 : 24 : error_at (DECL_SOURCE_LOCATION (decl),
16321 : : "%qs has both %<extern%> and initializer", name);
16322 : 24 : return error_mark_node;
16323 : : }
16324 : : }
16325 : :
16326 : : /* Record `register' declaration for warnings on &
16327 : : and in case doing stupid register allocation. */
16328 : :
16329 : 530469934 : if (storage_class == sc_register)
16330 : : {
16331 : 2060 : DECL_REGISTER (decl) = 1;
16332 : : /* Warn about register storage specifiers on PARM_DECLs. */
16333 : 2060 : if (TREE_CODE (decl) == PARM_DECL)
16334 : : {
16335 : 35 : if (cxx_dialect >= cxx17)
16336 : 22 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16337 : : "ISO C++17 does not allow %<register%> storage "
16338 : : "class specifier");
16339 : : else
16340 : 13 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
16341 : : "%<register%> storage class specifier used");
16342 : : }
16343 : : }
16344 : 530467874 : else if (storage_class == sc_extern)
16345 : 31687851 : DECL_THIS_EXTERN (decl) = 1;
16346 : 498780023 : else if (storage_class == sc_static)
16347 : 19394837 : DECL_THIS_STATIC (decl) = 1;
16348 : :
16349 : 530469934 : if (VAR_P (decl))
16350 : : {
16351 : : /* Set constexpr flag on vars (functions got it in grokfndecl). */
16352 : 66869531 : if (constexpr_p)
16353 : 13670747 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
16354 : : /* And the constinit flag (which only applies to variables). */
16355 : 53198784 : else if (constinit_p)
16356 : 436 : DECL_DECLARED_CONSTINIT_P (decl) = true;
16357 : : }
16358 : 463600403 : else if (TREE_CODE (decl) == FUNCTION_DECL)
16359 : : {
16360 : : /* If we saw a return type, record its location. */
16361 : 169609821 : location_t loc = declspecs->locations[ds_type_spec];
16362 : 169609821 : if (loc == UNKNOWN_LOCATION)
16363 : : /* Build DECL_RESULT in start_preparsed_function. */;
16364 : 139971179 : else if (!DECL_RESULT (decl))
16365 : : {
16366 : 139934854 : tree restype = TREE_TYPE (TREE_TYPE (decl));
16367 : 139934854 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
16368 : 139934854 : DECL_ARTIFICIAL (resdecl) = 1;
16369 : 139934854 : DECL_IGNORED_P (resdecl) = 1;
16370 : 139934854 : DECL_RESULT (decl) = resdecl;
16371 : : }
16372 : 36325 : else if (funcdef_flag)
16373 : 3 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
16374 : : }
16375 : :
16376 : : /* Record constancy and volatility on the DECL itself . There's
16377 : : no need to do this when processing a template; we'll do this
16378 : : for the instantiated declaration based on the type of DECL. */
16379 : 530469934 : if (!processing_template_decl
16380 : : /* Don't do it for instantiated variable templates either,
16381 : : cp_apply_type_quals_to_decl should have been called on it
16382 : : already and might have been overridden in cp_finish_decl
16383 : : if initializer needs runtime initialization. */
16384 : 530469934 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
16385 : 180966172 : cp_apply_type_quals_to_decl (type_quals, decl);
16386 : :
16387 : 530469934 : return decl;
16388 : : }
16389 : 919709892 : }
16390 : :
16391 : : /* Subroutine of start_function. Ensure that each of the parameter
16392 : : types (as listed in PARMS) is complete, as is required for a
16393 : : function definition. */
16394 : :
16395 : : static void
16396 : 142129149 : require_complete_types_for_parms (tree parms)
16397 : : {
16398 : 421792608 : for (; parms; parms = DECL_CHAIN (parms))
16399 : : {
16400 : 279663459 : if (dependent_type_p (TREE_TYPE (parms)))
16401 : 162129732 : continue;
16402 : 117533727 : if (!VOID_TYPE_P (TREE_TYPE (parms))
16403 : 117533727 : && complete_type_or_else (TREE_TYPE (parms), parms))
16404 : : {
16405 : 117533646 : relayout_decl (parms);
16406 : 117533646 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
16407 : :
16408 : 117533646 : abstract_virtuals_error (parms, TREE_TYPE (parms));
16409 : 117533646 : maybe_warn_parm_abi (TREE_TYPE (parms),
16410 : 117533646 : DECL_SOURCE_LOCATION (parms));
16411 : : }
16412 : : else
16413 : : /* grokparms or complete_type_or_else will have already issued
16414 : : an error. */
16415 : 81 : TREE_TYPE (parms) = error_mark_node;
16416 : : }
16417 : 142129149 : }
16418 : :
16419 : : /* Returns nonzero if T is a local variable. */
16420 : :
16421 : : int
16422 : 150655535 : local_variable_p (const_tree t)
16423 : : {
16424 : 150655535 : if ((VAR_P (t)
16425 : 67125357 : && (DECL_LOCAL_DECL_P (t)
16426 : 67125357 : || !DECL_CONTEXT (t)
16427 : 67082947 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
16428 : 154232568 : || (TREE_CODE (t) == PARM_DECL))
16429 : 135060517 : return 1;
16430 : :
16431 : : return 0;
16432 : : }
16433 : :
16434 : : /* Like local_variable_p, but suitable for use as a tree-walking
16435 : : function. */
16436 : :
16437 : : static tree
16438 : 4984680 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
16439 : : void * /*data*/)
16440 : : {
16441 : 4984680 : if (unevaluated_p (TREE_CODE (*tp)))
16442 : : {
16443 : : /* DR 2082 permits local variables in unevaluated contexts
16444 : : within a default argument. */
16445 : 15 : *walk_subtrees = 0;
16446 : 15 : return NULL_TREE;
16447 : : }
16448 : :
16449 : 4984665 : if (local_variable_p (*tp)
16450 : 4984665 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
16451 : : return *tp;
16452 : 4984665 : else if (TYPE_P (*tp))
16453 : 30 : *walk_subtrees = 0;
16454 : :
16455 : : return NULL_TREE;
16456 : : }
16457 : :
16458 : : /* Check that ARG, which is a default-argument expression for a
16459 : : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
16460 : : something goes wrong. DECL may also be a _TYPE node, rather than a
16461 : : DECL, if there is no DECL available. */
16462 : :
16463 : : tree
16464 : 2566745 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
16465 : : {
16466 : 2566745 : tree var;
16467 : 2566745 : tree decl_type;
16468 : :
16469 : 2566745 : if (TREE_CODE (arg) == DEFERRED_PARSE)
16470 : : /* We get a DEFERRED_PARSE when looking at an in-class declaration
16471 : : with a default argument. Ignore the argument for now; we'll
16472 : : deal with it after the class is complete. */
16473 : : return arg;
16474 : :
16475 : 1851494 : if (TYPE_P (decl))
16476 : : {
16477 : : decl_type = decl;
16478 : : decl = NULL_TREE;
16479 : : }
16480 : : else
16481 : 559373 : decl_type = TREE_TYPE (decl);
16482 : :
16483 : 1851494 : if (arg == error_mark_node
16484 : 1851376 : || decl == error_mark_node
16485 : 1851367 : || TREE_TYPE (arg) == error_mark_node
16486 : 3702861 : || decl_type == error_mark_node)
16487 : : /* Something already went wrong. There's no need to check
16488 : : further. */
16489 : : return error_mark_node;
16490 : :
16491 : : /* [dcl.fct.default]
16492 : :
16493 : : A default argument expression is implicitly converted to the
16494 : : parameter type. */
16495 : 1851367 : ++cp_unevaluated_operand;
16496 : : /* Avoid digest_init clobbering the initializer. */
16497 : 1851367 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
16498 : 1851367 : perform_implicit_conversion_flags (decl_type, carg, complain,
16499 : : LOOKUP_IMPLICIT);
16500 : 1851367 : --cp_unevaluated_operand;
16501 : :
16502 : : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
16503 : : the call sites. */
16504 : 1370876 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
16505 : 480506 : && null_ptr_cst_p (arg)
16506 : : /* Don't lose side-effects as in PR90473. */
16507 : 2218405 : && !TREE_SIDE_EFFECTS (arg))
16508 : 367035 : return nullptr_node;
16509 : :
16510 : : /* [dcl.fct.default]
16511 : :
16512 : : Local variables shall not be used in default argument
16513 : : expressions.
16514 : :
16515 : : The keyword `this' shall not be used in a default argument of a
16516 : : member function. */
16517 : 1484332 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
16518 : 1484332 : if (var)
16519 : : {
16520 : 0 : if (complain & tf_warning_or_error)
16521 : : {
16522 : 0 : if (DECL_NAME (var) == this_identifier)
16523 : 0 : permerror (input_location, "default argument %qE uses %qD",
16524 : : arg, var);
16525 : : else
16526 : 0 : error ("default argument %qE uses local variable %qD", arg, var);
16527 : : }
16528 : 0 : return error_mark_node;
16529 : : }
16530 : :
16531 : : /* All is well. */
16532 : 1484332 : return arg;
16533 : : }
16534 : :
16535 : : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
16536 : :
16537 : : static tree
16538 : 263412501 : type_is_deprecated (tree type)
16539 : : {
16540 : 388433354 : enum tree_code code;
16541 : 388433354 : if (TREE_DEPRECATED (type))
16542 : : return type;
16543 : 388391437 : if (TYPE_NAME (type))
16544 : : {
16545 : 259069757 : if (TREE_DEPRECATED (TYPE_NAME (type)))
16546 : : return type;
16547 : : else
16548 : : {
16549 : 259069751 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16550 : 259069751 : return NULL_TREE;
16551 : : }
16552 : : }
16553 : :
16554 : : /* Do warn about using typedefs to a deprecated class. */
16555 : 129321680 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16556 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16557 : :
16558 : 129321680 : code = TREE_CODE (type);
16559 : :
16560 : 129321680 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
16561 : 129321680 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
16562 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
16563 : 124910471 : return type_is_deprecated (TREE_TYPE (type));
16564 : :
16565 : 110382 : if (TYPE_PTRMEMFUNC_P (type))
16566 : 110382 : return type_is_deprecated
16567 : 110382 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16568 : :
16569 : : return NULL_TREE;
16570 : : }
16571 : :
16572 : : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
16573 : :
16574 : : static tree
16575 : 264299139 : type_is_unavailable (tree type)
16576 : : {
16577 : 389862198 : enum tree_code code;
16578 : 389862198 : if (TREE_UNAVAILABLE (type))
16579 : : return type;
16580 : 389862192 : if (TYPE_NAME (type))
16581 : : {
16582 : 259968638 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
16583 : : return type;
16584 : : else
16585 : : {
16586 : 259968632 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
16587 : 259968632 : return NULL_TREE;
16588 : : }
16589 : : }
16590 : :
16591 : : /* Do warn about using typedefs to a deprecated class. */
16592 : 129893554 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
16593 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
16594 : :
16595 : 129893554 : code = TREE_CODE (type);
16596 : :
16597 : 129893554 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
16598 : 129893554 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
16599 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
16600 : 125347973 : return type_is_unavailable (TREE_TYPE (type));
16601 : :
16602 : 215086 : if (TYPE_PTRMEMFUNC_P (type))
16603 : 215086 : return type_is_unavailable
16604 : 215086 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16605 : :
16606 : : return NULL_TREE;
16607 : : }
16608 : :
16609 : : /* Decode the list of parameter types for a function type.
16610 : : Given the list of things declared inside the parens,
16611 : : return a list of types.
16612 : :
16613 : : If this parameter does not end with an ellipsis, we append
16614 : : void_list_node.
16615 : :
16616 : : *PARMS is set to the chain of PARM_DECLs created. */
16617 : :
16618 : : tree
16619 : 177096169 : grokparms (tree parmlist, tree *parms)
16620 : : {
16621 : 177096169 : tree result = NULL_TREE;
16622 : 177096169 : tree decls = NULL_TREE;
16623 : 177096169 : tree parm;
16624 : 177096169 : int any_error = 0;
16625 : :
16626 : 442005908 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
16627 : : {
16628 : 440605818 : tree type = NULL_TREE;
16629 : 440605818 : tree init = TREE_PURPOSE (parm);
16630 : 440605818 : tree decl = TREE_VALUE (parm);
16631 : :
16632 : 440605818 : if (parm == void_list_node || parm == explicit_void_list_node)
16633 : : break;
16634 : :
16635 : 264909751 : if (! decl || TREE_TYPE (decl) == error_mark_node)
16636 : : {
16637 : 185 : any_error = 1;
16638 : 185 : continue;
16639 : : }
16640 : :
16641 : 264909566 : type = TREE_TYPE (decl);
16642 : 264909566 : if (VOID_TYPE_P (type))
16643 : : {
16644 : 78 : if (same_type_p (type, void_type_node)
16645 : 69 : && !init
16646 : 57 : && !DECL_NAME (decl) && !result
16647 : 102 : && TREE_CHAIN (parm) == void_list_node)
16648 : : /* DR 577: A parameter list consisting of a single
16649 : : unnamed parameter of non-dependent type 'void'. */
16650 : : break;
16651 : 66 : else if (cv_qualified_p (type))
16652 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
16653 : : "invalid use of cv-qualified type %qT in "
16654 : : "parameter declaration", type);
16655 : : else
16656 : 57 : error_at (DECL_SOURCE_LOCATION (decl),
16657 : : "invalid use of type %<void%> in parameter "
16658 : : "declaration");
16659 : : /* It's not a good idea to actually create parameters of
16660 : : type `void'; other parts of the compiler assume that a
16661 : : void type terminates the parameter list. */
16662 : 66 : type = error_mark_node;
16663 : 66 : TREE_TYPE (decl) = error_mark_node;
16664 : : }
16665 : :
16666 : 264909554 : if (type != error_mark_node)
16667 : : {
16668 : 264909488 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16669 : : {
16670 : 264299139 : tree unavailtype = type_is_unavailable (type);
16671 : 264299139 : if (unavailtype)
16672 : 12 : cp_handle_deprecated_or_unavailable (unavailtype);
16673 : : }
16674 : 264909488 : if (deprecated_state != DEPRECATED_SUPPRESS
16675 : 264909488 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
16676 : : {
16677 : 263412501 : tree deptype = type_is_deprecated (type);
16678 : 263412501 : if (deptype)
16679 : 41923 : cp_handle_deprecated_or_unavailable (deptype);
16680 : : }
16681 : :
16682 : : /* [dcl.fct] "A parameter with volatile-qualified type is
16683 : : deprecated." */
16684 : 264909488 : if (CP_TYPE_VOLATILE_P (type))
16685 : 504 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
16686 : : "%<volatile%>-qualified parameter is "
16687 : : "deprecated");
16688 : :
16689 : : /* Top-level qualifiers on the parameters are
16690 : : ignored for function types. */
16691 : 264909488 : type = cp_build_qualified_type (type, 0);
16692 : 264909488 : if (TREE_CODE (type) == METHOD_TYPE)
16693 : : {
16694 : 0 : error ("parameter %qD invalidly declared method type", decl);
16695 : 0 : type = build_pointer_type (type);
16696 : 0 : TREE_TYPE (decl) = type;
16697 : : }
16698 : 264909488 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
16699 : : {
16700 : : /* Before C++17 DR 393:
16701 : : [dcl.fct]/6, parameter types cannot contain pointers
16702 : : (references) to arrays of unknown bound. */
16703 : 1598593 : tree t = TREE_TYPE (type);
16704 : 1598593 : int ptr = TYPE_PTR_P (type);
16705 : :
16706 : 1754929 : while (1)
16707 : : {
16708 : 1676761 : if (TYPE_PTR_P (t))
16709 : : ptr = 1;
16710 : 1601723 : else if (TREE_CODE (t) != ARRAY_TYPE)
16711 : : break;
16712 : 3140 : else if (!TYPE_DOMAIN (t))
16713 : : break;
16714 : 78168 : t = TREE_TYPE (t);
16715 : : }
16716 : 1598593 : if (TREE_CODE (t) == ARRAY_TYPE)
16717 : 16 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
16718 : : ptr
16719 : : ? G_("parameter %qD includes pointer to array of "
16720 : : "unknown bound %qT")
16721 : : : G_("parameter %qD includes reference to array of "
16722 : : "unknown bound %qT"),
16723 : : decl, t);
16724 : : }
16725 : :
16726 : 264909488 : if (init && !processing_template_decl)
16727 : 1274615 : init = check_default_argument (decl, init, tf_warning_or_error);
16728 : : }
16729 : :
16730 : 264909554 : DECL_CHAIN (decl) = decls;
16731 : 264909554 : decls = decl;
16732 : 264909554 : result = tree_cons (init, type, result);
16733 : : }
16734 : 177096169 : decls = nreverse (decls);
16735 : 177096169 : result = nreverse (result);
16736 : 177096169 : if (parm)
16737 : 175696079 : result = chainon (result, void_list_node);
16738 : 177096169 : *parms = decls;
16739 : 177096169 : if (any_error)
16740 : 171 : result = NULL_TREE;
16741 : :
16742 : 171 : if (any_error)
16743 : : /* We had parm errors, recover by giving the function (...) type. */
16744 : 171 : result = NULL_TREE;
16745 : :
16746 : 177096169 : return result;
16747 : : }
16748 : :
16749 : :
16750 : : /* D is a constructor or overloaded `operator='.
16751 : :
16752 : : Let T be the class in which D is declared. Then, this function
16753 : : returns:
16754 : :
16755 : : -1 if D's is an ill-formed constructor or copy assignment operator
16756 : : whose first parameter is of type `T'.
16757 : : 0 if D is not a copy constructor or copy assignment
16758 : : operator.
16759 : : 1 if D is a copy constructor or copy assignment operator whose
16760 : : first parameter is a reference to non-const qualified T.
16761 : : 2 if D is a copy constructor or copy assignment operator whose
16762 : : first parameter is a reference to const qualified T.
16763 : :
16764 : : This function can be used as a predicate. Positive values indicate
16765 : : a copy constructor and nonzero values indicate a copy assignment
16766 : : operator. */
16767 : :
16768 : : int
16769 : 285843551 : copy_fn_p (const_tree d)
16770 : : {
16771 : 285843551 : tree args;
16772 : 285843551 : tree arg_type;
16773 : 285843551 : int result = 1;
16774 : :
16775 : 285843551 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
16776 : :
16777 : 285843551 : if (TREE_CODE (d) == TEMPLATE_DECL
16778 : 285843551 : || (DECL_TEMPLATE_INFO (d)
16779 : 137013571 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16780 : : /* Instantiations of template member functions are never copy
16781 : : functions. Note that member functions of templated classes are
16782 : : represented as template functions internally, and we must
16783 : : accept those as copy functions. */
16784 : : return 0;
16785 : :
16786 : 240754238 : if (!DECL_CONSTRUCTOR_P (d)
16787 : 240754238 : && DECL_NAME (d) != assign_op_identifier)
16788 : : return 0;
16789 : :
16790 : 239990696 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16791 : : {
16792 : 154 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16793 : 154 : if (!TYPE_REF_P (object_param)
16794 : 148 : || TYPE_REF_IS_RVALUE (object_param)
16795 : : /* Reject unrelated object parameters. */
16796 : 136 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16797 : 290 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16798 : 24 : return 0;
16799 : 130 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16800 : : }
16801 : : else
16802 : 239990542 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
16803 : 239990672 : if (!args)
16804 : : return 0;
16805 : :
16806 : 239983226 : arg_type = TREE_VALUE (args);
16807 : 239983226 : if (arg_type == error_mark_node)
16808 : : return 0;
16809 : :
16810 : 239983191 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
16811 : : {
16812 : : /* Pass by value copy assignment operator. */
16813 : : result = -1;
16814 : : }
16815 : 239981145 : else if (TYPE_REF_P (arg_type)
16816 : 147703862 : && !TYPE_REF_IS_RVALUE (arg_type)
16817 : 335104879 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
16818 : : {
16819 : 74941641 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
16820 : 74853935 : result = 2;
16821 : : }
16822 : : else
16823 : : return 0;
16824 : :
16825 : 74943687 : args = TREE_CHAIN (args);
16826 : :
16827 : 77531799 : if (args && args != void_list_node && !TREE_PURPOSE (args))
16828 : : /* There are more non-optional args. */
16829 : : return 0;
16830 : :
16831 : : return result;
16832 : : }
16833 : :
16834 : : /* D is a constructor or overloaded `operator='.
16835 : :
16836 : : Let T be the class in which D is declared. Then, this function
16837 : : returns true when D is a move constructor or move assignment
16838 : : operator, false otherwise. */
16839 : :
16840 : : bool
16841 : 115711855 : move_fn_p (const_tree d)
16842 : : {
16843 : 115711855 : if (cxx_dialect == cxx98)
16844 : : /* There are no move constructors if we are in C++98 mode. */
16845 : : return false;
16846 : :
16847 : 115645352 : if (TREE_CODE (d) == TEMPLATE_DECL
16848 : 115645352 : || (DECL_TEMPLATE_INFO (d)
16849 : 52183374 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
16850 : : /* Instantiations of template member functions are never move
16851 : : functions. Note that member functions of templated classes are
16852 : : represented as template functions internally, and we must
16853 : : accept those as move functions. */
16854 : : return 0;
16855 : :
16856 : 95751716 : return move_signature_fn_p (d);
16857 : : }
16858 : :
16859 : : /* D is a constructor or overloaded `operator='.
16860 : :
16861 : : Then, this function returns true when D has the same signature as a move
16862 : : constructor or move assignment operator (because either it is such a
16863 : : ctor/op= or it is a template specialization with the same signature),
16864 : : false otherwise. */
16865 : :
16866 : : bool
16867 : 95751716 : move_signature_fn_p (const_tree d)
16868 : : {
16869 : 95751716 : tree args;
16870 : 95751716 : tree arg_type;
16871 : 95751716 : bool result = false;
16872 : :
16873 : 95751716 : if (!DECL_CONSTRUCTOR_P (d)
16874 : 95751716 : && DECL_NAME (d) != assign_op_identifier)
16875 : : return 0;
16876 : :
16877 : 95751652 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
16878 : : {
16879 : 88 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
16880 : 88 : if (!TYPE_REF_P (object_param)
16881 : 84 : || TYPE_REF_IS_RVALUE (object_param)
16882 : : /* Reject unrelated object parameters. */
16883 : 76 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
16884 : 164 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
16885 : 16 : return 0;
16886 : 72 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
16887 : : }
16888 : : else
16889 : 95751564 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
16890 : 95751636 : if (!args)
16891 : : return 0;
16892 : :
16893 : 95748902 : arg_type = TREE_VALUE (args);
16894 : 95748902 : if (arg_type == error_mark_node)
16895 : : return 0;
16896 : :
16897 : 95748894 : if (TYPE_REF_P (arg_type)
16898 : 46824558 : && TYPE_REF_IS_RVALUE (arg_type)
16899 : 132326590 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
16900 : : DECL_CONTEXT (d)))
16901 : : result = true;
16902 : :
16903 : 95748894 : args = TREE_CHAIN (args);
16904 : :
16905 : 106291437 : if (args && args != void_list_node && !TREE_PURPOSE (args))
16906 : : /* There are more non-optional args. */
16907 : : return false;
16908 : :
16909 : : return result;
16910 : : }
16911 : :
16912 : : /* Remember any special properties of member function DECL. */
16913 : :
16914 : : void
16915 : 291573910 : grok_special_member_properties (tree decl)
16916 : : {
16917 : 291573910 : tree class_type;
16918 : :
16919 : 291573910 : if (TREE_CODE (decl) == USING_DECL
16920 : 291573910 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16921 : : return;
16922 : :
16923 : 274079232 : class_type = DECL_CONTEXT (decl);
16924 : 274079232 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
16925 : : {
16926 : 120613358 : int ctor = copy_fn_p (decl);
16927 : :
16928 : 120613358 : if (!DECL_ARTIFICIAL (decl))
16929 : 90863079 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
16930 : :
16931 : 120613358 : if (ctor > 0)
16932 : : {
16933 : : /* [class.copy]
16934 : :
16935 : : A non-template constructor for class X is a copy
16936 : : constructor if its first parameter is of type X&, const
16937 : : X&, volatile X& or const volatile X&, and either there
16938 : : are no other parameters or else all other parameters have
16939 : : default arguments. */
16940 : 23500940 : TYPE_HAS_COPY_CTOR (class_type) = 1;
16941 : 23500940 : if (ctor > 1)
16942 : 23470621 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
16943 : : }
16944 : :
16945 : 120613358 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
16946 : 24986415 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
16947 : :
16948 : 120613358 : if (is_list_ctor (decl))
16949 : 1425307 : TYPE_HAS_LIST_CTOR (class_type) = 1;
16950 : :
16951 : 120613358 : if (maybe_constexpr_fn (decl)
16952 : 120613358 : && !ctor && !move_fn_p (decl))
16953 : 29832363 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
16954 : : }
16955 : 153465874 : else if (DECL_NAME (decl) == assign_op_identifier)
16956 : : {
16957 : : /* [class.copy]
16958 : :
16959 : : A non-template assignment operator for class X is a copy
16960 : : assignment operator if its parameter is of type X, X&, const
16961 : : X&, volatile X& or const volatile X&. */
16962 : :
16963 : 13239666 : int assop = copy_fn_p (decl);
16964 : :
16965 : 13239666 : if (assop)
16966 : : {
16967 : 6617492 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
16968 : 6617492 : if (assop != 1)
16969 : 6587793 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
16970 : : }
16971 : : }
16972 : 140226208 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
16973 : 1916059 : TYPE_HAS_CONVERSION (class_type) = true;
16974 : :
16975 : : /* Destructors are handled in check_methods. */
16976 : : }
16977 : :
16978 : : /* Check a constructor DECL has the correct form. Complains
16979 : : if the class has a constructor of the form X(X). */
16980 : :
16981 : : bool
16982 : 42521387 : grok_ctor_properties (const_tree ctype, const_tree decl)
16983 : : {
16984 : 42521387 : int ctor_parm = copy_fn_p (decl);
16985 : :
16986 : 42521387 : if (ctor_parm < 0)
16987 : : {
16988 : : /* [class.copy]
16989 : :
16990 : : A declaration of a constructor for a class X is ill-formed if
16991 : : its first parameter is of type (optionally cv-qualified) X
16992 : : and either there are no other parameters or else all other
16993 : : parameters have default arguments.
16994 : :
16995 : : We *don't* complain about member template instantiations that
16996 : : have this form, though; they can occur as we try to decide
16997 : : what constructor to use during overload resolution. Since
16998 : : overload resolution will never prefer such a constructor to
16999 : : the non-template copy constructor (which is either explicitly
17000 : : or implicitly defined), there's no need to worry about their
17001 : : existence. Theoretically, they should never even be
17002 : : instantiated, but that's hard to forestall. */
17003 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
17004 : : "invalid constructor; you probably meant %<%T (const %T&)%>",
17005 : : ctype, ctype);
17006 : 9 : return false;
17007 : : }
17008 : :
17009 : : return true;
17010 : : }
17011 : :
17012 : : /* DECL is a declaration for an overloaded or conversion operator. If
17013 : : COMPLAIN is true, errors are issued for invalid declarations. */
17014 : :
17015 : : bool
17016 : 51295503 : grok_op_properties (tree decl, bool complain)
17017 : : {
17018 : 51295503 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
17019 : 51295503 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
17020 : 51295503 : tree name = DECL_NAME (decl);
17021 : 51295503 : location_t loc = DECL_SOURCE_LOCATION (decl);
17022 : :
17023 : 51295503 : tree class_type = DECL_CONTEXT (decl);
17024 : 51295503 : if (class_type && !CLASS_TYPE_P (class_type))
17025 : : class_type = NULL_TREE;
17026 : :
17027 : 51295503 : tree_code operator_code;
17028 : 51295503 : unsigned op_flags;
17029 : 51295503 : if (IDENTIFIER_CONV_OP_P (name))
17030 : : {
17031 : : /* Conversion operators are TYPE_EXPR for the purposes of this
17032 : : function. */
17033 : : operator_code = TYPE_EXPR;
17034 : : op_flags = OVL_OP_FLAG_UNARY;
17035 : : }
17036 : : else
17037 : : {
17038 : 49712614 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
17039 : :
17040 : 49712614 : operator_code = ovl_op->tree_code;
17041 : 49712614 : op_flags = ovl_op->flags;
17042 : 49712614 : gcc_checking_assert (operator_code != ERROR_MARK);
17043 : 49712614 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17044 : : }
17045 : :
17046 : 49712614 : if (op_flags & OVL_OP_FLAG_ALLOC)
17047 : : {
17048 : : /* operator new and operator delete are quite special. */
17049 : 331846 : if (class_type)
17050 : 13356 : switch (op_flags)
17051 : : {
17052 : 9463 : case OVL_OP_FLAG_ALLOC:
17053 : 9463 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
17054 : 9463 : break;
17055 : :
17056 : 3626 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
17057 : 3626 : TYPE_GETS_DELETE (class_type) |= 1;
17058 : 3626 : break;
17059 : :
17060 : 130 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
17061 : 130 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
17062 : 130 : break;
17063 : :
17064 : 137 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
17065 : 137 : TYPE_GETS_DELETE (class_type) |= 2;
17066 : 137 : break;
17067 : :
17068 : 0 : default:
17069 : 0 : gcc_unreachable ();
17070 : : }
17071 : :
17072 : : /* [basic.std.dynamic.allocation]/1:
17073 : :
17074 : : A program is ill-formed if an allocation function is declared
17075 : : in a namespace scope other than global scope or declared
17076 : : static in global scope.
17077 : :
17078 : : The same also holds true for deallocation functions. */
17079 : 331846 : if (DECL_NAMESPACE_SCOPE_P (decl))
17080 : : {
17081 : 318490 : if (CP_DECL_CONTEXT (decl) != global_namespace)
17082 : : {
17083 : 6 : error_at (loc, "%qD may not be declared within a namespace",
17084 : : decl);
17085 : 6 : return false;
17086 : : }
17087 : :
17088 : 318484 : if (!TREE_PUBLIC (decl))
17089 : : {
17090 : 6 : error_at (loc, "%qD may not be declared as static", decl);
17091 : 6 : return false;
17092 : : }
17093 : : }
17094 : :
17095 : : /* Check for replaceable global new/delete operators with
17096 : : const std::nothrow_t & last argument, other replaceable global
17097 : : new/delete operators are marked in cxx_init_decl_processing. */
17098 : 331834 : if (CP_DECL_CONTEXT (decl) == global_namespace)
17099 : : {
17100 : 318478 : tree args = argtypes;
17101 : 318478 : if (args
17102 : 318466 : && args != void_list_node
17103 : 955392 : && same_type_p (TREE_VALUE (args),
17104 : : (op_flags & OVL_OP_FLAG_DELETE)
17105 : : ? ptr_type_node : size_type_node))
17106 : : {
17107 : 318436 : args = TREE_CHAIN (args);
17108 : 318436 : if (aligned_allocation_fn_p (decl))
17109 : 102620 : args = TREE_CHAIN (args);
17110 : 318436 : if (args
17111 : 318430 : && args != void_list_node
17112 : 530969 : && TREE_CHAIN (args) == void_list_node)
17113 : : {
17114 : 186834 : tree t = TREE_VALUE (args);
17115 : 186834 : if (TYPE_REF_P (t)
17116 : 105998 : && !TYPE_REF_IS_RVALUE (t)
17117 : 105998 : && (t = TREE_TYPE (t))
17118 : 105998 : && TYPE_QUALS (t) == TYPE_QUAL_CONST
17119 : 292384 : && is_std_class (t, "nothrow_t"))
17120 : 105550 : DECL_IS_REPLACEABLE_OPERATOR (decl) = 1;
17121 : : }
17122 : : }
17123 : : }
17124 : :
17125 : 331834 : if (op_flags & OVL_OP_FLAG_DELETE)
17126 : : {
17127 : 188075 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
17128 : 188075 : coerce_delete_type (decl, loc);
17129 : : }
17130 : : else
17131 : : {
17132 : 143759 : DECL_SET_IS_OPERATOR_NEW (decl, true);
17133 : 143759 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
17134 : : }
17135 : :
17136 : 331834 : return true;
17137 : : }
17138 : :
17139 : : /* An operator function must either be a non-static member function
17140 : : or have at least one parameter of a class, a reference to a class,
17141 : : an enumeration, or a reference to an enumeration. 13.4.0.6 */
17142 : 50963657 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
17143 : : {
17144 : 14732625 : if (operator_code == TYPE_EXPR
17145 : 14732625 : || operator_code == COMPONENT_REF
17146 : 14732604 : || operator_code == NOP_EXPR)
17147 : : {
17148 : 33 : error_at (loc, "%qD must be a non-static member function", decl);
17149 : 33 : return false;
17150 : : }
17151 : :
17152 : 14732592 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
17153 : : {
17154 : 183 : if (! DECL_STATIC_FUNCTION_P (decl))
17155 : : {
17156 : 12 : error_at (loc, "%qD must be a member function", decl);
17157 : 12 : return false;
17158 : : }
17159 : 171 : if (cxx_dialect < cxx23
17160 : : /* For lambdas we diagnose static lambda specifier elsewhere. */
17161 : 95 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
17162 : : /* For instantiations, we have diagnosed this already. */
17163 : 203 : && ! DECL_USE_TEMPLATE (decl))
17164 : 26 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
17165 : : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
17166 : : decl);
17167 : 171 : if (operator_code == ARRAY_REF)
17168 : : /* static operator[] should have exactly one argument
17169 : : for C++20 and earlier, so that it isn't multidimensional. */
17170 : : op_flags = OVL_OP_FLAG_UNARY;
17171 : : }
17172 : 14732409 : else if (DECL_STATIC_FUNCTION_P (decl))
17173 : : {
17174 : 9 : error_at (loc, "%qD must be either a non-static member "
17175 : : "function or a non-member function", decl);
17176 : 9 : return false;
17177 : : }
17178 : : else
17179 : 498577 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
17180 : : {
17181 : 15230977 : if (!arg || arg == void_list_node)
17182 : : {
17183 : 18 : if (complain)
17184 : 15 : error_at (loc, "%qD must have an argument of class or "
17185 : : "enumerated type", decl);
17186 : 18 : return false;
17187 : : }
17188 : :
17189 : 15230959 : tree type = non_reference (TREE_VALUE (arg));
17190 : 15230959 : if (type == error_mark_node)
17191 : : return false;
17192 : :
17193 : : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
17194 : : because these checks are performed even on template
17195 : : functions. */
17196 : 15230956 : if (MAYBE_CLASS_TYPE_P (type)
17197 : 1457049 : || TREE_CODE (type) == ENUMERAL_TYPE)
17198 : : break;
17199 : 498577 : }
17200 : : }
17201 : :
17202 : 50963546 : if (operator_code == CALL_EXPR)
17203 : : /* There are no further restrictions on the arguments to an overloaded
17204 : : "operator ()". */
17205 : : return true;
17206 : :
17207 : : /* C++23 allows an arbitrary number of parameters and default arguments for
17208 : : operator[], and none of the other checks below apply. */
17209 : 44357043 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
17210 : : return true;
17211 : :
17212 : 43998398 : if (operator_code == COND_EXPR)
17213 : : {
17214 : : /* 13.4.0.3 */
17215 : 6 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
17216 : 6 : return false;
17217 : : }
17218 : :
17219 : : /* Count the number of arguments and check for ellipsis. */
17220 : : int arity = 0;
17221 : 126099155 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
17222 : : {
17223 : 82100778 : if (!arg)
17224 : : {
17225 : 15 : error_at (loc, "%qD must not have variable number of arguments",
17226 : : decl);
17227 : 15 : return false;
17228 : : }
17229 : 82100763 : ++arity;
17230 : : }
17231 : : /* FIXME: We need tests for these errors with xobj member functions. */
17232 : : /* Verify correct number of arguments. */
17233 : 43998377 : switch (op_flags)
17234 : : {
17235 : 7634121 : case OVL_OP_FLAG_AMBIARY:
17236 : 7634121 : if (arity == 1)
17237 : : {
17238 : : /* We have a unary instance of an ambi-ary op. Remap to the
17239 : : unary one. */
17240 : 3390730 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
17241 : 3390730 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
17242 : 3390730 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
17243 : 3390730 : operator_code = ovl_op->tree_code;
17244 : 3390730 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
17245 : : }
17246 : 4243391 : else if (arity != 2)
17247 : : {
17248 : : /* This was an ambiguous operator but is invalid. */
17249 : 3 : error_at (loc,
17250 : : methodp
17251 : : ? G_("%qD must have either zero or one argument")
17252 : : : G_("%qD must have either one or two arguments"), decl);
17253 : 3 : return false;
17254 : : }
17255 : 4243388 : else if ((operator_code == POSTINCREMENT_EXPR
17256 : 4243388 : || operator_code == POSTDECREMENT_EXPR)
17257 : 2055489 : && ! processing_template_decl
17258 : : /* x++ and x--'s second argument must be an int. */
17259 : 5603221 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
17260 : : integer_type_node))
17261 : : {
17262 : 15 : error_at (loc,
17263 : : methodp
17264 : : ? G_("postfix %qD must have %<int%> as its argument")
17265 : : : G_("postfix %qD must have %<int%> as its second argument"),
17266 : : decl);
17267 : 15 : return false;
17268 : : }
17269 : : break;
17270 : :
17271 : 2505291 : case OVL_OP_FLAG_UNARY:
17272 : 2505291 : if (arity != 1)
17273 : : {
17274 : 12 : error_at (loc,
17275 : : methodp
17276 : : ? G_("%qD must have no arguments")
17277 : : : G_("%qD must have exactly one argument"), decl);
17278 : 12 : return false;
17279 : : }
17280 : : break;
17281 : :
17282 : 33858965 : case OVL_OP_FLAG_BINARY:
17283 : 33858965 : if (arity != 2)
17284 : : {
17285 : 3 : error_at (loc,
17286 : : methodp
17287 : : ? G_("%qD must have exactly one argument")
17288 : : : G_("%qD must have exactly two arguments"), decl);
17289 : 3 : return false;
17290 : : }
17291 : : break;
17292 : :
17293 : 0 : default:
17294 : 0 : gcc_unreachable ();
17295 : : }
17296 : :
17297 : : /* There can be no default arguments. */
17298 : 126099008 : for (tree arg = argtypes; arg && arg != void_list_node;
17299 : 82100664 : arg = TREE_CHAIN (arg))
17300 : 82100679 : if (TREE_PURPOSE (arg))
17301 : : {
17302 : 15 : TREE_PURPOSE (arg) = NULL_TREE;
17303 : 15 : error_at (loc, "%qD cannot have default arguments", decl);
17304 : 15 : return false;
17305 : : }
17306 : :
17307 : : /* At this point the declaration is well-formed. It may not be
17308 : : sensible though. */
17309 : :
17310 : : /* Check member function warnings only on the in-class declaration.
17311 : : There's no point warning on an out-of-class definition. */
17312 : 43998329 : if (class_type && class_type != current_class_type)
17313 : : return true;
17314 : :
17315 : : /* Warn about conversion operators that will never be used. */
17316 : 42059732 : if (IDENTIFIER_CONV_OP_P (name)
17317 : 1582469 : && ! DECL_TEMPLATE_INFO (decl)
17318 : 42694776 : && warn_class_conversion)
17319 : : {
17320 : 635011 : tree t = TREE_TYPE (name);
17321 : 635011 : int ref = TYPE_REF_P (t);
17322 : :
17323 : 635011 : if (ref)
17324 : 23253 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
17325 : :
17326 : 635011 : if (VOID_TYPE_P (t))
17327 : 9 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
17328 : : "will never use a type conversion operator", class_type);
17329 : 635002 : else if (class_type)
17330 : : {
17331 : 635002 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
17332 : 24 : warning_at (loc, OPT_Wclass_conversion,
17333 : : ref
17334 : : ? G_("converting %qT to a reference to the same type "
17335 : : "will never use a type conversion operator")
17336 : : : G_("converting %qT to the same type "
17337 : : "will never use a type conversion operator"),
17338 : : class_type);
17339 : : /* Don't force t to be complete here. */
17340 : 634987 : else if (MAYBE_CLASS_TYPE_P (t)
17341 : 280940 : && COMPLETE_TYPE_P (t)
17342 : 60189 : && DERIVED_FROM_P (t, class_type))
17343 : 30 : warning_at (loc, OPT_Wclass_conversion,
17344 : : ref
17345 : : ? G_("converting %qT to a reference to a base class "
17346 : : "%qT will never use a type conversion operator")
17347 : : : G_("converting %qT to a base class %qT "
17348 : : "will never use a type conversion operator"),
17349 : : class_type, t);
17350 : : }
17351 : : }
17352 : :
17353 : 42059732 : if (!warn_ecpp)
17354 : : return true;
17355 : :
17356 : : /* Effective C++ rules below. */
17357 : :
17358 : : /* More Effective C++ rule 7. */
17359 : 54 : if (operator_code == TRUTH_ANDIF_EXPR
17360 : 54 : || operator_code == TRUTH_ORIF_EXPR
17361 : 54 : || operator_code == COMPOUND_EXPR)
17362 : 0 : warning_at (loc, OPT_Weffc__,
17363 : : "user-defined %qD always evaluates both arguments", decl);
17364 : :
17365 : : /* More Effective C++ rule 6. */
17366 : 54 : if (operator_code == POSTINCREMENT_EXPR
17367 : : || operator_code == POSTDECREMENT_EXPR
17368 : : || operator_code == PREINCREMENT_EXPR
17369 : 54 : || operator_code == PREDECREMENT_EXPR)
17370 : : {
17371 : 0 : tree arg = TREE_VALUE (argtypes);
17372 : 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
17373 : 0 : if (methodp || TYPE_REF_P (arg))
17374 : 0 : arg = TREE_TYPE (arg);
17375 : 0 : arg = TYPE_MAIN_VARIANT (arg);
17376 : :
17377 : 0 : if (operator_code == PREINCREMENT_EXPR
17378 : : || operator_code == PREDECREMENT_EXPR)
17379 : : {
17380 : 0 : if (!TYPE_REF_P (ret)
17381 : 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
17382 : 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
17383 : : build_reference_type (arg));
17384 : : }
17385 : : else
17386 : : {
17387 : 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
17388 : 0 : warning_at (loc, OPT_Weffc__,
17389 : : "postfix %qD should return %qT", decl, arg);
17390 : : }
17391 : : }
17392 : :
17393 : : /* Effective C++ rule 23. */
17394 : 54 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
17395 : : && (operator_code == PLUS_EXPR
17396 : 9 : || operator_code == MINUS_EXPR
17397 : 9 : || operator_code == TRUNC_DIV_EXPR
17398 : : || operator_code == MULT_EXPR
17399 : : || operator_code == TRUNC_MOD_EXPR)
17400 : 60 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
17401 : 3 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
17402 : :
17403 : : return true;
17404 : : }
17405 : :
17406 : : /* Return a string giving the keyword associate with CODE. */
17407 : :
17408 : : static const char *
17409 : 111 : tag_name (enum tag_types code)
17410 : : {
17411 : 111 : switch (code)
17412 : : {
17413 : : case record_type:
17414 : : return "struct";
17415 : 60 : case class_type:
17416 : 60 : return "class";
17417 : 0 : case union_type:
17418 : 0 : return "union";
17419 : 3 : case enum_type:
17420 : 3 : return "enum";
17421 : 0 : case typename_type:
17422 : 0 : return "typename";
17423 : 0 : default:
17424 : 0 : gcc_unreachable ();
17425 : : }
17426 : : }
17427 : :
17428 : : /* Name lookup in an elaborated-type-specifier (after the keyword
17429 : : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
17430 : : elaborated-type-specifier is invalid, issue a diagnostic and return
17431 : : error_mark_node; otherwise, return the *_TYPE to which it referred.
17432 : : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
17433 : :
17434 : : tree
17435 : 11245165 : check_elaborated_type_specifier (enum tag_types tag_code,
17436 : : tree decl,
17437 : : bool allow_template_p)
17438 : : {
17439 : 11245165 : tree type;
17440 : :
17441 : : /* In the case of:
17442 : :
17443 : : struct S { struct S *p; };
17444 : :
17445 : : name lookup will find the TYPE_DECL for the implicit "S::S"
17446 : : typedef. Adjust for that here. */
17447 : 11245165 : if (DECL_SELF_REFERENCE_P (decl))
17448 : 335405 : decl = TYPE_NAME (TREE_TYPE (decl));
17449 : :
17450 : 11245165 : type = TREE_TYPE (decl);
17451 : :
17452 : : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
17453 : : is false for this case as well. */
17454 : 11245165 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17455 : : {
17456 : 9 : error ("using template type parameter %qT after %qs",
17457 : : type, tag_name (tag_code));
17458 : 9 : return error_mark_node;
17459 : : }
17460 : : /* Accept template template parameters. */
17461 : 11245156 : else if (allow_template_p
17462 : 6443766 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
17463 : 6443754 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
17464 : : ;
17465 : : /* [dcl.type.elab]
17466 : :
17467 : : If the identifier resolves to a typedef-name or the
17468 : : simple-template-id resolves to an alias template
17469 : : specialization, the elaborated-type-specifier is ill-formed.
17470 : :
17471 : : In other words, the only legitimate declaration to use in the
17472 : : elaborated type specifier is the implicit typedef created when
17473 : : the type is declared. */
17474 : 11245141 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
17475 : 335521 : && !DECL_SELF_REFERENCE_P (decl)
17476 : 11245257 : && tag_code != typename_type)
17477 : : {
17478 : 78 : auto_diagnostic_group d;
17479 : 78 : if (alias_template_specialization_p (type, nt_opaque))
17480 : 21 : error ("using alias template specialization %qT after %qs",
17481 : : type, tag_name (tag_code));
17482 : : else
17483 : 57 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
17484 : 78 : inform (DECL_SOURCE_LOCATION (decl),
17485 : : "%qD has a previous declaration here", decl);
17486 : 78 : return error_mark_node;
17487 : 78 : }
17488 : 11245063 : else if (TREE_CODE (type) != RECORD_TYPE
17489 : 11245063 : && TREE_CODE (type) != UNION_TYPE
17490 : : && tag_code != enum_type
17491 : 63627 : && tag_code != typename_type)
17492 : : {
17493 : 6 : auto_diagnostic_group d;
17494 : 6 : error ("%qT referred to as %qs", type, tag_name (tag_code));
17495 : 6 : inform (location_of (type), "%qT has a previous declaration here", type);
17496 : 6 : return error_mark_node;
17497 : 6 : }
17498 : 11245057 : else if (TREE_CODE (type) != ENUMERAL_TYPE
17499 : 11181466 : && tag_code == enum_type)
17500 : : {
17501 : 3 : auto_diagnostic_group d;
17502 : 3 : error ("%qT referred to as enum", type);
17503 : 3 : inform (location_of (type), "%qT has a previous declaration here", type);
17504 : 3 : return error_mark_node;
17505 : 3 : }
17506 : 11245054 : else if (!allow_template_p
17507 : 4801330 : && TREE_CODE (type) == RECORD_TYPE
17508 : 15929279 : && CLASSTYPE_IS_TEMPLATE (type))
17509 : : {
17510 : : /* If a class template appears as elaborated type specifier
17511 : : without a template header such as:
17512 : :
17513 : : template <class T> class C {};
17514 : : void f(class C); // No template header here
17515 : :
17516 : : then the required template argument is missing. */
17517 : 18 : error ("template argument required for %<%s %T%>",
17518 : : tag_name (tag_code),
17519 : 18 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
17520 : 18 : return error_mark_node;
17521 : : }
17522 : :
17523 : : return type;
17524 : : }
17525 : :
17526 : : /* Lookup NAME of an elaborated type specifier according to SCOPE and
17527 : : issue diagnostics if necessary. Return *_TYPE node upon success,
17528 : : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
17529 : : error. */
17530 : :
17531 : : static tree
17532 : 22273546 : lookup_and_check_tag (enum tag_types tag_code, tree name,
17533 : : TAG_how how, bool template_header_p)
17534 : : {
17535 : 22273546 : tree decl;
17536 : 22273546 : if (how == TAG_how::GLOBAL)
17537 : : {
17538 : : /* First try ordinary name lookup, ignoring hidden class name
17539 : : injected via friend declaration. */
17540 : 1890022 : decl = lookup_name (name, LOOK_want::TYPE);
17541 : : /* If that fails, the name will be placed in the smallest
17542 : : non-class, non-function-prototype scope according to 3.3.1/5.
17543 : : We may already have a hidden name declared as friend in this
17544 : : scope. So lookup again but not ignoring hidden names.
17545 : : If we find one, that name will be made visible rather than
17546 : : creating a new tag. */
17547 : 1890022 : if (!decl)
17548 : 18265 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
17549 : : }
17550 : : else
17551 : 20383524 : decl = lookup_elaborated_type (name, how);
17552 : :
17553 : 20401789 : if (!decl)
17554 : : /* We found nothing. */
17555 : : return NULL_TREE;
17556 : :
17557 : 4945027 : if (TREE_CODE (decl) == TREE_LIST)
17558 : : {
17559 : 3 : auto_diagnostic_group d;
17560 : 3 : error ("reference to %qD is ambiguous", name);
17561 : 3 : print_candidates (decl);
17562 : 3 : return error_mark_node;
17563 : 3 : }
17564 : :
17565 : 2068736 : if (DECL_CLASS_TEMPLATE_P (decl)
17566 : 2068730 : && !template_header_p
17567 : 7013754 : && how == TAG_how::CURRENT_ONLY)
17568 : : {
17569 : 12 : auto_diagnostic_group d;
17570 : 12 : error ("class template %qD redeclared as non-template", name);
17571 : 12 : inform (location_of (decl), "previous declaration here");
17572 : 12 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
17573 : 12 : return error_mark_node;
17574 : 12 : }
17575 : :
17576 : 2068724 : if (DECL_CLASS_TEMPLATE_P (decl)
17577 : : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
17578 : : so ignore a template template parameter. */
17579 : 4945018 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
17580 : 2068721 : decl = DECL_TEMPLATE_RESULT (decl);
17581 : :
17582 : 4945012 : if (TREE_CODE (decl) != TYPE_DECL)
17583 : : /* Found not-a-type. */
17584 : : return NULL_TREE;
17585 : :
17586 : : /* Look for invalid nested type:
17587 : : class C {
17588 : : class C {};
17589 : : }; */
17590 : 4944991 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
17591 : : {
17592 : 6 : error ("%qD has the same name as the class in which it is "
17593 : : "declared", decl);
17594 : 6 : return error_mark_node;
17595 : : }
17596 : :
17597 : : /* Two cases we need to consider when deciding if a class
17598 : : template is allowed as an elaborated type specifier:
17599 : : 1. It is a self reference to its own class.
17600 : : 2. It comes with a template header.
17601 : :
17602 : : For example:
17603 : :
17604 : : template <class T> class C {
17605 : : class C *c1; // DECL_SELF_REFERENCE_P is true
17606 : : class D;
17607 : : };
17608 : : template <class U> class C; // template_header_p is true
17609 : : template <class T> class C<T>::D {
17610 : : class C *c2; // DECL_SELF_REFERENCE_P is true
17611 : : }; */
17612 : :
17613 : 14834955 : tree t = check_elaborated_type_specifier (tag_code, decl,
17614 : : template_header_p
17615 : 9554580 : | DECL_SELF_REFERENCE_P (decl));
17616 : 2189001 : if (template_header_p && t && CLASS_TYPE_P (t)
17617 : 7133968 : && (!CLASSTYPE_TEMPLATE_INFO (t)
17618 : 2188965 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
17619 : : {
17620 : 21 : auto_diagnostic_group d;
17621 : 21 : error ("%qT is not a template", t);
17622 : 21 : inform (location_of (t), "previous declaration here");
17623 : 42 : if (TYPE_CLASS_SCOPE_P (t)
17624 : 24 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
17625 : 3 : inform (input_location,
17626 : : "perhaps you want to explicitly add %<%T::%>",
17627 : 3 : TYPE_CONTEXT (t));
17628 : 21 : return error_mark_node;
17629 : 21 : }
17630 : :
17631 : : return t;
17632 : : }
17633 : :
17634 : : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
17635 : : Define the tag as a forward-reference if it is not defined.
17636 : :
17637 : : If a declaration is given, process it here, and report an error if
17638 : : multiple declarations are not identical.
17639 : :
17640 : : SCOPE is TS_CURRENT when this is also a definition. Only look in
17641 : : the current frame for the name (since C++ allows new names in any
17642 : : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
17643 : : declaration. Only look beginning from the current scope outward up
17644 : : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
17645 : :
17646 : : TEMPLATE_HEADER_P is true when this declaration is preceded by
17647 : : a set of template parameters. */
17648 : :
17649 : : tree
17650 : 21758231 : xref_tag (enum tag_types tag_code, tree name,
17651 : : TAG_how how, bool template_header_p)
17652 : : {
17653 : 21758231 : enum tree_code code;
17654 : 21758231 : tree context = NULL_TREE;
17655 : :
17656 : 21758231 : auto_cond_timevar tv (TV_NAME_LOOKUP);
17657 : :
17658 : 21758231 : gcc_assert (identifier_p (name));
17659 : :
17660 : 21758231 : switch (tag_code)
17661 : : {
17662 : : case record_type:
17663 : : case class_type:
17664 : : code = RECORD_TYPE;
17665 : : break;
17666 : : case union_type:
17667 : : code = UNION_TYPE;
17668 : : break;
17669 : : case enum_type:
17670 : : code = ENUMERAL_TYPE;
17671 : : break;
17672 : 0 : default:
17673 : 0 : gcc_unreachable ();
17674 : : }
17675 : :
17676 : : /* In case of anonymous name, xref_tag is only called to
17677 : : make type node and push name. Name lookup is not required. */
17678 : 21758231 : tree t = NULL_TREE;
17679 : 21758231 : if (!IDENTIFIER_ANON_P (name))
17680 : 20180231 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
17681 : :
17682 : 21758231 : if (t == error_mark_node)
17683 : : return error_mark_node;
17684 : :
17685 : 2473243 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
17686 : 1059591 : && template_class_depth (current_class_type)
17687 : 22151237 : && template_header_p)
17688 : : {
17689 : 276548 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
17690 : : return t;
17691 : :
17692 : : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
17693 : : a definition of this tag. Since, in addition, we are
17694 : : currently processing a (member) template declaration of a
17695 : : template class, we must be very careful; consider:
17696 : :
17697 : : template <class X> struct S1
17698 : :
17699 : : template <class U> struct S2
17700 : : {
17701 : : template <class V> friend struct S1;
17702 : : };
17703 : :
17704 : : Here, the S2::S1 declaration should not be confused with the
17705 : : outer declaration. In particular, the inner version should
17706 : : have a template parameter of level 2, not level 1.
17707 : :
17708 : : On the other hand, when presented with:
17709 : :
17710 : : template <class T> struct S1
17711 : : {
17712 : : template <class U> struct S2 {};
17713 : : template <class U> friend struct S2;
17714 : : };
17715 : :
17716 : : the friend must find S1::S2 eventually. We accomplish this
17717 : : by making sure that the new type we create to represent this
17718 : : declaration has the right TYPE_CONTEXT. */
17719 : 276545 : context = TYPE_CONTEXT (t);
17720 : 276545 : t = NULL_TREE;
17721 : : }
17722 : :
17723 : 21758117 : if (! t)
17724 : : {
17725 : : /* If no such tag is yet defined, create a forward-reference node
17726 : : and record it as the "definition".
17727 : : When a real declaration of this type is found,
17728 : : the forward-reference will be altered into a real type. */
17729 : 17124569 : if (code == ENUMERAL_TYPE)
17730 : : {
17731 : 14 : error ("use of enum %q#D without previous declaration", name);
17732 : 14 : return error_mark_node;
17733 : : }
17734 : :
17735 : 17124555 : t = make_class_type (code);
17736 : 17124555 : TYPE_CONTEXT (t) = context;
17737 : 17124555 : if (IDENTIFIER_LAMBDA_P (name))
17738 : : /* Mark it as a lambda type right now. Our caller will
17739 : : correct the value. */
17740 : 921008 : SET_CLASSTYPE_LAMBDA_EXPR (t, error_mark_node);
17741 : 17124555 : t = pushtag (name, t, how);
17742 : : }
17743 : : else
17744 : : {
17745 : 4633548 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
17746 : : {
17747 : : /* Check that we aren't trying to overload a class with different
17748 : : constraints. */
17749 : 1912417 : if (!redeclare_class_template (t, current_template_parms,
17750 : : current_template_constraints ()))
17751 : 60 : return error_mark_node;
17752 : : }
17753 : 2721131 : else if (!processing_template_decl
17754 : 2583116 : && CLASS_TYPE_P (t)
17755 : 5278170 : && CLASSTYPE_IS_TEMPLATE (t))
17756 : : {
17757 : 0 : auto_diagnostic_group d;
17758 : 0 : error ("redeclaration of %qT as a non-template", t);
17759 : 0 : inform (location_of (t), "previous declaration %qD", t);
17760 : 0 : return error_mark_node;
17761 : 0 : }
17762 : :
17763 : 4633488 : if (modules_p ()
17764 : 4633488 : && how == TAG_how::CURRENT_ONLY)
17765 : : {
17766 : 8361 : tree decl = TYPE_NAME (t);
17767 : 8361 : if (!module_may_redeclare (decl))
17768 : 15 : return error_mark_node;
17769 : :
17770 : 8346 : tree not_tmpl = STRIP_TEMPLATE (decl);
17771 : 8346 : if (DECL_LANG_SPECIFIC (not_tmpl)
17772 : 3894 : && DECL_MODULE_ATTACH_P (not_tmpl)
17773 : 39 : && !DECL_MODULE_EXPORT_P (not_tmpl)
17774 : 8379 : && module_exporting_p ())
17775 : : {
17776 : 6 : auto_diagnostic_group d;
17777 : 6 : error ("conflicting exporting for declaration %qD", decl);
17778 : 6 : inform (DECL_SOURCE_LOCATION (decl),
17779 : : "previously declared here without exporting");
17780 : 6 : }
17781 : :
17782 : 8346 : tree maybe_tmpl = decl;
17783 : 8346 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
17784 : 6168 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
17785 : :
17786 : : /* FIXME: we should do a more precise check for redefinitions
17787 : : of a conflicting using-declaration here, as these diagnostics
17788 : : are not ideal. */
17789 : 8346 : if (DECL_LANG_SPECIFIC (decl)
17790 : 3894 : && DECL_MODULE_IMPORT_P (decl)
17791 : 8393 : && CP_DECL_CONTEXT (decl) == current_namespace)
17792 : : {
17793 : : /* Push it into this TU's symbol slot. */
17794 : 44 : if (maybe_tmpl != decl)
17795 : : /* We're in the template parm binding level.
17796 : : Pushtag has logic to slide under that, but we're
17797 : : not pushing a *new* type. */
17798 : 18 : push_nested_namespace (CP_DECL_CONTEXT (decl));
17799 : :
17800 : 44 : pushdecl (maybe_tmpl);
17801 : 44 : if (maybe_tmpl != decl)
17802 : 18 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
17803 : : }
17804 : :
17805 : 8346 : set_instantiating_module (maybe_tmpl);
17806 : : }
17807 : : }
17808 : :
17809 : : return t;
17810 : 21758231 : }
17811 : :
17812 : : /* Create the binfo hierarchy for REF with (possibly NULL) base list
17813 : : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
17814 : : access_* node, and the TREE_VALUE is the type of the base-class.
17815 : : Non-NULL TREE_TYPE indicates virtual inheritance. */
17816 : :
17817 : : void
17818 : 55961171 : xref_basetypes (tree ref, tree base_list)
17819 : : {
17820 : 55961171 : tree *basep;
17821 : 55961171 : tree binfo, base_binfo;
17822 : 55961171 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
17823 : 55961171 : unsigned max_bases = 0; /* Maximum direct bases. */
17824 : 55961171 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
17825 : 55961171 : int i;
17826 : 55961171 : tree default_access;
17827 : 55961171 : tree igo_prev; /* Track Inheritance Graph Order. */
17828 : :
17829 : 55961171 : if (ref == error_mark_node)
17830 : 12 : return;
17831 : :
17832 : : /* The base of a derived class is private by default, all others are
17833 : : public. */
17834 : 55961171 : default_access = (TREE_CODE (ref) == RECORD_TYPE
17835 : 55442778 : && CLASSTYPE_DECLARED_CLASS (ref)
17836 : 55961171 : ? access_private_node : access_public_node);
17837 : :
17838 : : /* First, make sure that any templates in base-classes are
17839 : : instantiated. This ensures that if we call ourselves recursively
17840 : : we do not get confused about which classes are marked and which
17841 : : are not. */
17842 : 55961171 : basep = &base_list;
17843 : 85013811 : while (*basep)
17844 : : {
17845 : 29052640 : tree basetype = TREE_VALUE (*basep);
17846 : :
17847 : : /* The dependent_type_p call below should really be dependent_scope_p
17848 : : so that we give a hard error about using an incomplete type as a
17849 : : base, but we allow it with a pedwarn for backward
17850 : : compatibility. */
17851 : 29052640 : if (processing_template_decl
17852 : 29052640 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
17853 : 3 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype,
17854 : : diagnostics::kind::pedwarn);
17855 : 29052640 : if (!dependent_type_p (basetype)
17856 : 29052640 : && !complete_type_or_else (basetype, NULL))
17857 : : /* An incomplete type. Remove it from the list. */
17858 : 15 : *basep = TREE_CHAIN (*basep);
17859 : : else
17860 : : {
17861 : 29052625 : max_bases++;
17862 : 29052625 : if (TREE_TYPE (*basep))
17863 : 70338 : max_dvbases++;
17864 : 29052625 : if (CLASS_TYPE_P (basetype))
17865 : 28220958 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
17866 : 29052625 : basep = &TREE_CHAIN (*basep);
17867 : : }
17868 : : }
17869 : 55961171 : max_vbases += max_dvbases;
17870 : :
17871 : 55961171 : TYPE_MARKED_P (ref) = 1;
17872 : :
17873 : : /* The binfo slot should be empty, unless this is an (ill-formed)
17874 : : redefinition. */
17875 : 55961171 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
17876 : :
17877 : 55961171 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
17878 : :
17879 : 55961171 : binfo = make_tree_binfo (max_bases);
17880 : :
17881 : 55961171 : TYPE_BINFO (ref) = binfo;
17882 : 55961171 : BINFO_OFFSET (binfo) = size_zero_node;
17883 : 55961171 : BINFO_TYPE (binfo) = ref;
17884 : :
17885 : : /* Apply base-class info set up to the variants of this type. */
17886 : 55961171 : fixup_type_variants (ref);
17887 : :
17888 : 55961171 : if (max_bases)
17889 : : {
17890 : 28165542 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
17891 : : /* A C++98 POD cannot have base classes. */
17892 : 28165542 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
17893 : :
17894 : 28165542 : if (TREE_CODE (ref) == UNION_TYPE)
17895 : : {
17896 : 12 : error ("derived union %qT invalid", ref);
17897 : 12 : return;
17898 : : }
17899 : : }
17900 : :
17901 : 28165530 : if (max_bases > 1)
17902 : 689260 : warning (OPT_Wmultiple_inheritance,
17903 : : "%qT defined with multiple direct bases", ref);
17904 : :
17905 : 55961159 : if (max_vbases)
17906 : : {
17907 : : /* An aggregate can't have virtual base classes. */
17908 : 212089 : CLASSTYPE_NON_AGGREGATE (ref) = true;
17909 : :
17910 : 212089 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
17911 : :
17912 : 212089 : if (max_dvbases)
17913 : 69143 : warning (OPT_Wvirtual_inheritance,
17914 : : "%qT defined with direct virtual base", ref);
17915 : : }
17916 : :
17917 : 85013772 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
17918 : : {
17919 : 29052613 : tree access = TREE_PURPOSE (base_list);
17920 : 29052613 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
17921 : 29052613 : tree basetype = TREE_VALUE (base_list);
17922 : :
17923 : 29052613 : if (access == access_default_node)
17924 : 4490211 : access = default_access;
17925 : :
17926 : : /* Before C++17, an aggregate cannot have base classes. In C++17, an
17927 : : aggregate can't have virtual, private, or protected base classes. */
17928 : 29052613 : if (cxx_dialect < cxx17
17929 : 28838735 : || access != access_public_node
17930 : 27919582 : || via_virtual)
17931 : 1200291 : CLASSTYPE_NON_AGGREGATE (ref) = true;
17932 : :
17933 : 29052613 : if (PACK_EXPANSION_P (basetype))
17934 : 23433 : basetype = PACK_EXPANSION_PATTERN (basetype);
17935 : 29052613 : if (TREE_CODE (basetype) == TYPE_DECL)
17936 : 0 : basetype = TREE_TYPE (basetype);
17937 : 29052613 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
17938 : : {
17939 : 24 : error ("base type %qT fails to be a struct or class type",
17940 : : basetype);
17941 : 24 : goto dropped_base;
17942 : : }
17943 : :
17944 : 29052589 : base_binfo = NULL_TREE;
17945 : 29052589 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
17946 : : {
17947 : 22405234 : base_binfo = TYPE_BINFO (basetype);
17948 : : /* The original basetype could have been a typedef'd type. */
17949 : 22405234 : basetype = BINFO_TYPE (base_binfo);
17950 : :
17951 : : /* Inherit flags from the base. */
17952 : 67215702 : TYPE_HAS_NEW_OPERATOR (ref)
17953 : 22405234 : |= TYPE_HAS_NEW_OPERATOR (basetype);
17954 : 67215702 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
17955 : 22405234 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
17956 : 22405234 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
17957 : 22405234 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
17958 : 67215702 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
17959 : 22405234 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
17960 : 67215702 : CLASSTYPE_REPEATED_BASE_P (ref)
17961 : 22405234 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
17962 : : }
17963 : :
17964 : : /* We must do this test after we've seen through a typedef
17965 : : type. */
17966 : 29052589 : if (TYPE_MARKED_P (basetype))
17967 : : {
17968 : 18 : if (basetype == ref)
17969 : 0 : error ("recursive type %qT undefined", basetype);
17970 : : else
17971 : 18 : error ("duplicate base type %qT invalid", basetype);
17972 : 18 : goto dropped_base;
17973 : : }
17974 : :
17975 : 29052571 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
17976 : : /* Regenerate the pack expansion for the bases. */
17977 : 23433 : basetype = make_pack_expansion (basetype);
17978 : :
17979 : 29052571 : TYPE_MARKED_P (basetype) = 1;
17980 : :
17981 : 29052571 : base_binfo = copy_binfo (base_binfo, basetype, ref,
17982 : : &igo_prev, via_virtual);
17983 : 29052571 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
17984 : 28982248 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
17985 : :
17986 : 29052571 : BINFO_BASE_APPEND (binfo, base_binfo);
17987 : 29052571 : BINFO_BASE_ACCESS_APPEND (binfo, access);
17988 : 29052571 : continue;
17989 : :
17990 : 42 : dropped_base:
17991 : : /* Update max_vbases to reflect the reality that we are dropping
17992 : : this base: if it reaches zero we want to undo the vec_alloc
17993 : : above to avoid inconsistencies during error-recovery: eg, in
17994 : : build_special_member_call, CLASSTYPE_VBASECLASSES non null
17995 : : and vtt null (c++/27952). */
17996 : 42 : if (via_virtual)
17997 : 12 : max_vbases--;
17998 : 42 : if (CLASS_TYPE_P (basetype))
17999 : 30 : max_vbases
18000 : 33 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
18001 : 29052571 : }
18002 : :
18003 : 55961159 : if (CLASSTYPE_VBASECLASSES (ref)
18004 : 55961159 : && max_vbases == 0)
18005 : 24 : vec_free (CLASSTYPE_VBASECLASSES (ref));
18006 : :
18007 : 56173236 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
18008 : : /* If we didn't get max_vbases vbases, we must have shared at
18009 : : least one of them, and are therefore diamond shaped. */
18010 : 22805 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
18011 : :
18012 : : /* Unmark all the types. */
18013 : 85013730 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
18014 : 29052571 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18015 : 55961159 : TYPE_MARKED_P (ref) = 0;
18016 : :
18017 : : /* Now see if we have a repeated base type. */
18018 : 55961159 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
18019 : : {
18020 : 144547636 : for (base_binfo = binfo; base_binfo;
18021 : 88586909 : base_binfo = TREE_CHAIN (base_binfo))
18022 : : {
18023 : 88587620 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18024 : : {
18025 : 711 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
18026 : 711 : break;
18027 : : }
18028 : 88586909 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
18029 : : }
18030 : 144547636 : for (base_binfo = binfo; base_binfo;
18031 : 88586909 : base_binfo = TREE_CHAIN (base_binfo))
18032 : 88587620 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
18033 : 88586909 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
18034 : : else
18035 : : break;
18036 : : }
18037 : : }
18038 : :
18039 : :
18040 : : /* Copies the enum-related properties from type SRC to type DST.
18041 : : Used with the underlying type of an enum and the enum itself. */
18042 : : static void
18043 : 1768540 : copy_type_enum (tree dst, tree src)
18044 : : {
18045 : 1768540 : tree t;
18046 : 3537098 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
18047 : : {
18048 : 1768558 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
18049 : 1768558 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
18050 : 1768558 : TYPE_SIZE (t) = TYPE_SIZE (src);
18051 : 1768558 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
18052 : 1768558 : SET_TYPE_MODE (dst, TYPE_MODE (src));
18053 : 1768558 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
18054 : 1768558 : unsigned valign = TYPE_ALIGN (src);
18055 : 1768558 : if (TYPE_USER_ALIGN (t))
18056 : 12 : valign = MAX (valign, TYPE_ALIGN (t));
18057 : : else
18058 : 1768546 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
18059 : 1768558 : SET_TYPE_ALIGN (t, valign);
18060 : 1768558 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
18061 : : }
18062 : 1768540 : }
18063 : :
18064 : : /* Begin compiling the definition of an enumeration type.
18065 : : NAME is its name,
18066 : :
18067 : : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
18068 : :
18069 : : UNDERLYING_TYPE is the type that will be used as the storage for
18070 : : the enumeration type. This should be NULL_TREE if no storage type
18071 : : was specified.
18072 : :
18073 : : ATTRIBUTES are any attributes specified after the enum-key.
18074 : :
18075 : : SCOPED_ENUM_P is true if this is a scoped enumeration type.
18076 : :
18077 : : if IS_NEW is not NULL, gets TRUE iff a new type is created.
18078 : :
18079 : : Returns the type object, as yet incomplete.
18080 : : Also records info about it so that build_enumerator
18081 : : may be used to declare the individual values as they are read. */
18082 : :
18083 : : tree
18084 : 2093446 : start_enum (tree name, tree enumtype, tree underlying_type,
18085 : : tree attributes, bool scoped_enum_p, bool *is_new)
18086 : : {
18087 : 2093446 : tree prevtype = NULL_TREE;
18088 : 2093446 : gcc_assert (identifier_p (name));
18089 : :
18090 : 2093446 : if (is_new)
18091 : 1671042 : *is_new = false;
18092 : : /* [C++0x dcl.enum]p5:
18093 : :
18094 : : If not explicitly specified, the underlying type of a scoped
18095 : : enumeration type is int. */
18096 : 2093446 : if (!underlying_type && scoped_enum_p)
18097 : 119873 : underlying_type = integer_type_node;
18098 : :
18099 : 2093446 : if (underlying_type)
18100 : 435082 : underlying_type = cv_unqualified (underlying_type);
18101 : :
18102 : : /* If this is the real definition for a previous forward reference,
18103 : : fill in the contents in the same object that used to be the
18104 : : forward reference. */
18105 : 2093446 : if (!enumtype)
18106 : 2093315 : enumtype = lookup_and_check_tag (enum_type, name,
18107 : : /*tag_scope=*/TAG_how::CURRENT_ONLY,
18108 : : /*template_header_p=*/false);
18109 : :
18110 : : /* In case of a template_decl, the only check that should be deferred
18111 : : to instantiation time is the comparison of underlying types. */
18112 : 2093446 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
18113 : : {
18114 : : /* Attempt to set the declaring module. */
18115 : 34927 : if (modules_p ())
18116 : : {
18117 : 160 : tree decl = TYPE_NAME (enumtype);
18118 : 160 : if (!module_may_redeclare (decl))
18119 : 3 : enumtype = error_mark_node;
18120 : : else
18121 : 157 : set_instantiating_module (decl);
18122 : : }
18123 : :
18124 : 34927 : if (enumtype == error_mark_node)
18125 : : ;
18126 : 35114 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
18127 : : {
18128 : 6 : auto_diagnostic_group d;
18129 : 6 : error_at (input_location, "scoped/unscoped mismatch "
18130 : : "in enum %q#T", enumtype);
18131 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18132 : : "previous definition here");
18133 : 6 : enumtype = error_mark_node;
18134 : 6 : }
18135 : 34918 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
18136 : : {
18137 : 0 : auto_diagnostic_group d;
18138 : 0 : error_at (input_location, "underlying type mismatch "
18139 : : "in enum %q#T", enumtype);
18140 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18141 : : "previous definition here");
18142 : 0 : enumtype = error_mark_node;
18143 : 0 : }
18144 : 34902 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
18145 : 69820 : && !same_type_p (underlying_type,
18146 : : ENUM_UNDERLYING_TYPE (enumtype)))
18147 : : {
18148 : 26 : auto_diagnostic_group d;
18149 : 26 : error_at (input_location, "different underlying type "
18150 : : "in enum %q#T", enumtype);
18151 : 26 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18152 : : "previous definition here");
18153 : 26 : underlying_type = NULL_TREE;
18154 : 26 : }
18155 : : }
18156 : :
18157 : 2093446 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
18158 : 34918 : || processing_template_decl)
18159 : : {
18160 : : /* In case of error, make a dummy enum to allow parsing to
18161 : : continue. */
18162 : 2058616 : if (enumtype == error_mark_node)
18163 : : {
18164 : 9 : name = make_anon_name ();
18165 : 9 : enumtype = NULL_TREE;
18166 : : }
18167 : :
18168 : : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
18169 : : of an opaque enum, or an opaque enum of an already defined
18170 : : enumeration (C++11).
18171 : : In any other case, it'll be NULL_TREE. */
18172 : 2058616 : if (!enumtype)
18173 : : {
18174 : 2058525 : if (is_new)
18175 : 1636124 : *is_new = true;
18176 : : }
18177 : 2058616 : prevtype = enumtype;
18178 : :
18179 : : /* Do not push the decl more than once. */
18180 : 2058616 : if (!enumtype
18181 : 91 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
18182 : : {
18183 : 2058528 : enumtype = cxx_make_type (ENUMERAL_TYPE);
18184 : 2058528 : enumtype = pushtag (name, enumtype);
18185 : :
18186 : 2058528 : if (enumtype != error_mark_node)
18187 : : {
18188 : : /* The enum is considered opaque until the opening '{' of the
18189 : : enumerator list. */
18190 : 2058510 : SET_OPAQUE_ENUM_P (enumtype, true);
18191 : 2058510 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
18192 : :
18193 : : /* std::byte aliases anything. */
18194 : 2058510 : if (TYPE_CONTEXT (enumtype) == std_node
18195 : 2381216 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
18196 : 12891 : TYPE_ALIAS_SET (enumtype) = 0;
18197 : : }
18198 : : }
18199 : : else
18200 : 88 : enumtype = xref_tag (enum_type, name);
18201 : :
18202 : 2058616 : if (enumtype == error_mark_node)
18203 : : return error_mark_node;
18204 : : }
18205 : :
18206 : 2093428 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
18207 : :
18208 : 2093428 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18209 : :
18210 : 2093428 : if (underlying_type)
18211 : : {
18212 : 435053 : if (ENUM_UNDERLYING_TYPE (enumtype))
18213 : : /* We already checked that it matches, don't change it to a different
18214 : : typedef variant. */;
18215 : 400177 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
18216 : : {
18217 : 400113 : copy_type_enum (enumtype, underlying_type);
18218 : 400113 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18219 : : }
18220 : 64 : else if (dependent_type_p (underlying_type))
18221 : 51 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
18222 : : else
18223 : : {
18224 : 13 : error ("underlying type %qT of %qT must be an integral type",
18225 : : underlying_type, enumtype);
18226 : 13 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
18227 : : }
18228 : : }
18229 : :
18230 : : /* If into a template class, the returned enum is always the first
18231 : : declaration (opaque or not) seen. This way all the references to
18232 : : this type will be to the same declaration. The following ones are used
18233 : : only to check for definition errors. */
18234 : 2093428 : if (prevtype && processing_template_decl)
18235 : : return prevtype;
18236 : : else
18237 : 2093340 : return enumtype;
18238 : : }
18239 : :
18240 : : /* Returns true if TYPE is an enum that uses an enumerator name for
18241 : : linkage purposes. */
18242 : :
18243 : : bool
18244 : 345364577 : enum_with_enumerator_for_linkage_p (tree type)
18245 : : {
18246 : 345364577 : return (cxx_dialect >= cxx20
18247 : 112820506 : && UNSCOPED_ENUM_P (type)
18248 : 5579726 : && TYPE_ANON_P (type)
18249 : 346446027 : && TYPE_VALUES (type));
18250 : : }
18251 : :
18252 : : /* After processing and defining all the values of an enumeration type,
18253 : : install their decls in the enumeration type.
18254 : : ENUMTYPE is the type object. */
18255 : :
18256 : : void
18257 : 1993875 : finish_enum_value_list (tree enumtype)
18258 : : {
18259 : 1993875 : tree values;
18260 : 1993875 : tree underlying_type;
18261 : 1993875 : tree decl;
18262 : 1993875 : tree value;
18263 : 1993875 : tree minnode, maxnode;
18264 : 1993875 : tree t;
18265 : :
18266 : 1993875 : bool fixed_underlying_type_p
18267 : 1993875 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
18268 : :
18269 : : /* We built up the VALUES in reverse order. */
18270 : 1993875 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
18271 : :
18272 : : /* For an enum defined in a template, just set the type of the values;
18273 : : all further processing is postponed until the template is
18274 : : instantiated. We need to set the type so that tsubst of a CONST_DECL
18275 : : works. */
18276 : 1993875 : if (processing_template_decl)
18277 : : {
18278 : 364791 : for (values = TYPE_VALUES (enumtype);
18279 : 675262 : values;
18280 : 364791 : values = TREE_CHAIN (values))
18281 : 364791 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
18282 : : return;
18283 : : }
18284 : :
18285 : : /* Determine the minimum and maximum values of the enumerators. */
18286 : 1683404 : if (TYPE_VALUES (enumtype))
18287 : : {
18288 : : minnode = maxnode = NULL_TREE;
18289 : :
18290 : 10609283 : for (values = TYPE_VALUES (enumtype);
18291 : 12264404 : values;
18292 : 10609283 : values = TREE_CHAIN (values))
18293 : : {
18294 : 10609283 : decl = TREE_VALUE (values);
18295 : :
18296 : : /* [dcl.enum]: Following the closing brace of an enum-specifier,
18297 : : each enumerator has the type of its enumeration. Prior to the
18298 : : closing brace, the type of each enumerator is the type of its
18299 : : initializing value. */
18300 : 10609283 : TREE_TYPE (decl) = enumtype;
18301 : :
18302 : : /* Update the minimum and maximum values, if appropriate. */
18303 : 10609283 : value = DECL_INITIAL (decl);
18304 : 10609283 : if (TREE_CODE (value) != INTEGER_CST)
18305 : 4 : value = integer_zero_node;
18306 : : /* Figure out what the minimum and maximum values of the
18307 : : enumerators are. */
18308 : 10609283 : if (!minnode)
18309 : : minnode = maxnode = value;
18310 : 8954162 : else if (tree_int_cst_lt (maxnode, value))
18311 : : maxnode = value;
18312 : 1358479 : else if (tree_int_cst_lt (value, minnode))
18313 : 147513 : minnode = value;
18314 : : }
18315 : : }
18316 : : else
18317 : : /* [dcl.enum]
18318 : :
18319 : : If the enumerator-list is empty, the underlying type is as if
18320 : : the enumeration had a single enumerator with value 0. */
18321 : 28283 : minnode = maxnode = integer_zero_node;
18322 : :
18323 : 1683404 : if (!fixed_underlying_type_p)
18324 : : {
18325 : : /* Compute the number of bits require to represent all values of the
18326 : : enumeration. We must do this before the type of MINNODE and
18327 : : MAXNODE are transformed, since tree_int_cst_min_precision relies
18328 : : on the TREE_TYPE of the value it is passed. */
18329 : 1368427 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
18330 : 1368427 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
18331 : 1368427 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
18332 : 1368427 : int precision = MAX (lowprec, highprec);
18333 : 1368427 : unsigned int itk;
18334 : 1368427 : bool use_short_enum;
18335 : :
18336 : : /* Determine the underlying type of the enumeration.
18337 : :
18338 : : [dcl.enum]
18339 : :
18340 : : The underlying type of an enumeration is an integral type that
18341 : : can represent all the enumerator values defined in the
18342 : : enumeration. It is implementation-defined which integral type is
18343 : : used as the underlying type for an enumeration except that the
18344 : : underlying type shall not be larger than int unless the value of
18345 : : an enumerator cannot fit in an int or unsigned int.
18346 : :
18347 : : We use "int" or an "unsigned int" as the underlying type, even if
18348 : : a smaller integral type would work, unless the user has
18349 : : explicitly requested that we use the smallest possible type. The
18350 : : user can request that for all enumerations with a command line
18351 : : flag, or for just one enumeration with an attribute. */
18352 : :
18353 : 2736854 : use_short_enum = flag_short_enums
18354 : 1368427 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
18355 : :
18356 : : /* If the precision of the type was specified with an attribute and it
18357 : : was too small, give an error. Otherwise, use it. */
18358 : 1368427 : if (TYPE_PRECISION (enumtype))
18359 : : {
18360 : 11 : if (precision > TYPE_PRECISION (enumtype))
18361 : 0 : error ("specified mode too small for enumerated values");
18362 : : else
18363 : : {
18364 : 11 : use_short_enum = true;
18365 : 11 : precision = TYPE_PRECISION (enumtype);
18366 : : }
18367 : : }
18368 : :
18369 : 2658947 : for (itk = (use_short_enum ? itk_char : itk_int);
18370 : 2658947 : itk != itk_none;
18371 : : itk++)
18372 : : {
18373 : 2658944 : underlying_type = integer_types[itk];
18374 : 2658944 : if (underlying_type != NULL_TREE
18375 : 2658926 : && TYPE_PRECISION (underlying_type) >= precision
18376 : 5315929 : && TYPE_SIGN (underlying_type) == sgn)
18377 : : break;
18378 : : }
18379 : 1368427 : if (itk == itk_none)
18380 : : {
18381 : : /* DR 377
18382 : :
18383 : : IF no integral type can represent all the enumerator values, the
18384 : : enumeration is ill-formed. */
18385 : 3 : error ("no integral type can represent all of the enumerator values "
18386 : : "for %qT", enumtype);
18387 : 3 : precision = TYPE_PRECISION (long_long_integer_type_node);
18388 : 3 : underlying_type = integer_types[itk_unsigned_long_long];
18389 : : }
18390 : :
18391 : : /* [dcl.enum]
18392 : :
18393 : : The value of sizeof() applied to an enumeration type, an object
18394 : : of an enumeration type, or an enumerator, is the value of sizeof()
18395 : : applied to the underlying type. */
18396 : 1368427 : copy_type_enum (enumtype, underlying_type);
18397 : :
18398 : : /* Compute the minimum and maximum values for the type.
18399 : :
18400 : : [dcl.enum]
18401 : :
18402 : : For an enumeration where emin is the smallest enumerator and emax
18403 : : is the largest, the values of the enumeration are the values of the
18404 : : underlying type in the range bmin to bmax, where bmin and bmax are,
18405 : : respectively, the smallest and largest values of the smallest bit-
18406 : : field that can store emin and emax. */
18407 : :
18408 : : /* The middle-end currently assumes that types with TYPE_PRECISION
18409 : : narrower than their underlying type are suitably zero or sign
18410 : : extended to fill their mode. Similarly, it assumes that the front
18411 : : end assures that a value of a particular type must be within
18412 : : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
18413 : :
18414 : : We used to set these fields based on bmin and bmax, but that led
18415 : : to invalid assumptions like optimizing away bounds checking. So
18416 : : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
18417 : : TYPE_MAX_VALUE to the values for the mode above and only restrict
18418 : : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
18419 : 1368427 : ENUM_UNDERLYING_TYPE (enumtype)
18420 : 1368427 : = build_distinct_type_copy (underlying_type);
18421 : 1368427 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
18422 : 1368427 : set_min_and_max_values_for_integral_type
18423 : 1368427 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
18424 : :
18425 : : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
18426 : 1368427 : if (flag_strict_enums)
18427 : 38 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
18428 : :
18429 : 1368427 : if (use_short_enum)
18430 : : {
18431 : 79 : TYPE_PACKED (enumtype) = use_short_enum;
18432 : 79 : fixup_attribute_variants (enumtype);
18433 : : }
18434 : : }
18435 : : else
18436 : 314977 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
18437 : :
18438 : : /* If the enum is exported, mark the consts too. */
18439 : 1683404 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
18440 : 1438026 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
18441 : 1683925 : && at_namespace_scope_p ());
18442 : :
18443 : : /* Convert each of the enumerators to the type of the underlying
18444 : : type of the enumeration. */
18445 : 12292687 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
18446 : : {
18447 : 10609283 : decl = TREE_VALUE (values);
18448 : 10609283 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
18449 : 10609283 : if (fixed_underlying_type_p)
18450 : : /* If the enumeration type has a fixed underlying type, we
18451 : : already checked all of the enumerator values. */
18452 : 3093536 : value = DECL_INITIAL (decl);
18453 : : else
18454 : 7515747 : value = perform_implicit_conversion (underlying_type,
18455 : 7515747 : DECL_INITIAL (decl),
18456 : : tf_warning_or_error);
18457 : : /* Do not clobber shared ints. */
18458 : 10609283 : if (value != error_mark_node)
18459 : : {
18460 : 10609281 : value = copy_node (value);
18461 : :
18462 : 10609281 : TREE_TYPE (value) = enumtype;
18463 : : }
18464 : 10609283 : DECL_INITIAL (decl) = value;
18465 : 10609283 : if (export_p)
18466 : 2890 : DECL_MODULE_EXPORT_P (decl) = true;
18467 : 10609283 : }
18468 : :
18469 : : /* Fix up all variant types of this enum type. */
18470 : 3366826 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
18471 : 1683422 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
18472 : :
18473 : 1683404 : if (at_class_scope_p ()
18474 : 936036 : && COMPLETE_TYPE_P (current_class_type)
18475 : 1683465 : && UNSCOPED_ENUM_P (enumtype))
18476 : : {
18477 : 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
18478 : : /* TYPE_FIELDS needs fixup. */
18479 : 42 : fixup_type_variants (current_class_type);
18480 : : }
18481 : :
18482 : : /* P2115: An unnamed enum uses the name of its first enumerator for
18483 : : linkage purposes; reset the type linkage if that is the case. */
18484 : 1683404 : if (enum_with_enumerator_for_linkage_p (enumtype))
18485 : 324032 : reset_type_linkage (enumtype);
18486 : :
18487 : : /* Finish debugging output for this type. */
18488 : 1683404 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
18489 : :
18490 : : /* Each enumerator now has the type of its enumeration. Clear the cache
18491 : : so that this change in types doesn't confuse us later on. */
18492 : 1683404 : clear_cv_and_fold_caches ();
18493 : : }
18494 : :
18495 : : /* Finishes the enum type. This is called only the first time an
18496 : : enumeration is seen, be it opaque or odinary.
18497 : : ENUMTYPE is the type object. */
18498 : :
18499 : : void
18500 : 1983375 : finish_enum (tree enumtype)
18501 : : {
18502 : 1983375 : if (processing_template_decl)
18503 : : {
18504 : 310499 : if (at_function_scope_p ())
18505 : 2486 : add_stmt (build_min (TAG_DEFN, enumtype));
18506 : 310499 : return;
18507 : : }
18508 : :
18509 : : /* If this is a forward declaration, there should not be any variants,
18510 : : though we can get a variant in the middle of an enum-specifier with
18511 : : wacky code like 'enum E { e = sizeof(const E*) };' */
18512 : 3345752 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
18513 : : && (TYPE_VALUES (enumtype)
18514 : : || !TYPE_NEXT_VARIANT (enumtype)));
18515 : : }
18516 : :
18517 : : /* Build and install a CONST_DECL for an enumeration constant of the
18518 : : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
18519 : : Apply ATTRIBUTES if available. LOC is the location of NAME.
18520 : : Assignment of sequential values by default is handled here. */
18521 : :
18522 : : tree
18523 : 10974051 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
18524 : : location_t loc)
18525 : : {
18526 : 10974051 : tree decl;
18527 : 10974051 : tree context;
18528 : 10974051 : tree type;
18529 : :
18530 : : /* scalar_constant_value will pull out this expression, so make sure
18531 : : it's folded as appropriate.
18532 : :
18533 : : Creating a TARGET_EXPR in a template breaks when substituting, and
18534 : : here we would create it for instance when using a class prvalue with
18535 : : a user-defined conversion function. So don't use such a tree. We
18536 : : instantiate VALUE here to get errors about bad enumerators even in
18537 : : a template that does not get instantiated. */
18538 : 10974051 : if (processing_template_decl)
18539 : 364791 : value = maybe_fold_non_dependent_expr (value);
18540 : :
18541 : : /* If the VALUE was erroneous, pretend it wasn't there; that will
18542 : : result in the enum being assigned the next value in sequence. */
18543 : 10974051 : if (value == error_mark_node)
18544 : : value = NULL_TREE;
18545 : :
18546 : : /* Remove no-op casts from the value. */
18547 : 10974013 : if (value)
18548 : 8359729 : STRIP_TYPE_NOPS (value);
18549 : :
18550 : 10974051 : if (! processing_template_decl)
18551 : : {
18552 : : /* Validate and default VALUE. */
18553 : 10609260 : if (value != NULL_TREE)
18554 : : {
18555 : 4761867 : if (!ENUM_UNDERLYING_TYPE (enumtype))
18556 : : {
18557 : 2567862 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
18558 : : value, true);
18559 : 2567862 : if (tmp_value)
18560 : 4761867 : value = tmp_value;
18561 : : }
18562 : 2194005 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18563 : : (TREE_TYPE (value)))
18564 : 39 : value = perform_implicit_conversion_flags
18565 : 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
18566 : : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
18567 : :
18568 : 4761867 : if (value == error_mark_node)
18569 : : value = NULL_TREE;
18570 : :
18571 : 4761846 : if (value != NULL_TREE)
18572 : : {
18573 : 4761846 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
18574 : : (TREE_TYPE (value)))
18575 : : {
18576 : 17 : error_at (cp_expr_loc_or_input_loc (value),
18577 : : "enumerator value for %qD must have integral or "
18578 : : "unscoped enumeration type", name);
18579 : 17 : value = NULL_TREE;
18580 : : }
18581 : : else
18582 : : {
18583 : 4761829 : value = cxx_constant_value (value);
18584 : :
18585 : 4761829 : if (TREE_CODE (value) != INTEGER_CST)
18586 : : {
18587 : 50 : error ("enumerator value for %qD is not an integer "
18588 : : "constant", name);
18589 : 50 : value = NULL_TREE;
18590 : : }
18591 : : }
18592 : : }
18593 : : }
18594 : :
18595 : : /* Default based on previous value. */
18596 : 67 : if (value == NULL_TREE)
18597 : : {
18598 : 5847481 : if (TYPE_VALUES (enumtype))
18599 : : {
18600 : 5399976 : tree prev_value;
18601 : :
18602 : : /* C++03 7.2/4: If no initializer is specified for the first
18603 : : enumerator, the type is an unspecified integral
18604 : : type. Otherwise the type is the same as the type of the
18605 : : initializing value of the preceding enumerator unless the
18606 : : incremented value is not representable in that type, in
18607 : : which case the type is an unspecified integral type
18608 : : sufficient to contain the incremented value. */
18609 : 5399976 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
18610 : 5399976 : if (TREE_CODE (prev_value) != INTEGER_CST)
18611 : 2 : value = error_mark_node;
18612 : : else
18613 : : {
18614 : 5399974 : wi::overflow_type overflowed;
18615 : 5399974 : tree type = TREE_TYPE (prev_value);
18616 : 5399974 : signop sgn = TYPE_SIGN (type);
18617 : 5399974 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
18618 : 5399974 : &overflowed);
18619 : 5399974 : if (!overflowed)
18620 : : {
18621 : 5399974 : bool pos = !wi::neg_p (wi, sgn);
18622 : 5399974 : if (!wi::fits_to_tree_p (wi, type))
18623 : : {
18624 : : unsigned int itk;
18625 : 53 : for (itk = itk_int; itk != itk_none; itk++)
18626 : : {
18627 : 53 : type = integer_types[itk];
18628 : 53 : if (type != NULL_TREE
18629 : 53 : && (pos || !TYPE_UNSIGNED (type))
18630 : 106 : && wi::fits_to_tree_p (wi, type))
18631 : : break;
18632 : : }
18633 : 17 : if (type && cxx_dialect < cxx11
18634 : 6 : && itk > itk_unsigned_long)
18635 : 3 : pedwarn (input_location, OPT_Wlong_long,
18636 : : pos ? G_("\
18637 : : incremented enumerator value is too large for %<unsigned long%>") : G_("\
18638 : : incremented enumerator value is too large for %<long%>"));
18639 : : }
18640 : 5399974 : if (type == NULL_TREE)
18641 : 0 : overflowed = wi::OVF_UNKNOWN;
18642 : : else
18643 : 5399974 : value = wide_int_to_tree (type, wi);
18644 : : }
18645 : :
18646 : 5399974 : if (overflowed)
18647 : : {
18648 : 0 : error ("overflow in enumeration values at %qD", name);
18649 : 0 : value = error_mark_node;
18650 : : }
18651 : 5399974 : }
18652 : : }
18653 : : else
18654 : 447505 : value = integer_zero_node;
18655 : : }
18656 : :
18657 : : /* Remove no-op casts from the value. */
18658 : 10609260 : STRIP_TYPE_NOPS (value);
18659 : :
18660 : : /* If the underlying type of the enum is fixed, check whether
18661 : : the enumerator values fits in the underlying type. If it
18662 : : does not fit, the program is ill-formed [C++0x dcl.enum]. */
18663 : 10609260 : if (ENUM_UNDERLYING_TYPE (enumtype)
18664 : : && value
18665 : 10609260 : && TREE_CODE (value) == INTEGER_CST)
18666 : : {
18667 : 3093511 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
18668 : 9 : error ("enumerator value %qE is outside the range of underlying "
18669 : 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
18670 : :
18671 : : /* Convert the value to the appropriate type. */
18672 : 3093511 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
18673 : : }
18674 : : }
18675 : :
18676 : : /* C++ associates enums with global, function, or class declarations. */
18677 : 10974051 : context = current_scope ();
18678 : :
18679 : : /* Build the actual enumeration constant. Note that the enumeration
18680 : : constants have the underlying type of the enum (if it is fixed)
18681 : : or the type of their initializer (if the underlying type of the
18682 : : enum is not fixed):
18683 : :
18684 : : [ C++0x dcl.enum ]
18685 : :
18686 : : If the underlying type is fixed, the type of each enumerator
18687 : : prior to the closing brace is the underlying type; if the
18688 : : initializing value of an enumerator cannot be represented by
18689 : : the underlying type, the program is ill-formed. If the
18690 : : underlying type is not fixed, the type of each enumerator is
18691 : : the type of its initializing value.
18692 : :
18693 : : If the underlying type is not fixed, it will be computed by
18694 : : finish_enum and we will reset the type of this enumerator. Of
18695 : : course, if we're processing a template, there may be no value. */
18696 : 10974051 : type = value ? TREE_TYPE (value) : NULL_TREE;
18697 : :
18698 : 10974051 : decl = build_decl (loc, CONST_DECL, name, type);
18699 : :
18700 : 10974051 : DECL_CONTEXT (decl) = enumtype;
18701 : 10974051 : TREE_CONSTANT (decl) = 1;
18702 : 10974051 : TREE_READONLY (decl) = 1;
18703 : 10974051 : DECL_INITIAL (decl) = value;
18704 : :
18705 : 10974051 : if (attributes)
18706 : 44031 : cplus_decl_attributes (&decl, attributes, 0);
18707 : :
18708 : 10974051 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
18709 : : {
18710 : : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
18711 : : on the TYPE_FIELDS list for `S'. (That's so that you can say
18712 : : things like `S::i' later.) */
18713 : :
18714 : : /* The enumerator may be getting declared outside of its enclosing
18715 : : class, like so:
18716 : :
18717 : : class S { public: enum E : int; }; enum S::E : int { i = 7; };
18718 : :
18719 : : For which case we need to make sure that the access of `S::i'
18720 : : matches the access of `S::E'. */
18721 : 1958997 : auto cas = make_temp_override (current_access_specifier);
18722 : 1958997 : set_current_access_from_decl (TYPE_NAME (enumtype));
18723 : 1958997 : finish_member_declaration (decl);
18724 : 1958997 : }
18725 : : else
18726 : 9015054 : pushdecl (decl);
18727 : :
18728 : : /* Add this enumeration constant to the list for this type. */
18729 : 10974051 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
18730 : :
18731 : 10974051 : return decl;
18732 : : }
18733 : :
18734 : : /* Look for an enumerator with the given NAME within the enumeration
18735 : : type ENUMTYPE. This routine is used primarily for qualified name
18736 : : lookup into an enumerator in C++0x, e.g.,
18737 : :
18738 : : enum class Color { Red, Green, Blue };
18739 : :
18740 : : Color color = Color::Red;
18741 : :
18742 : : Returns the value corresponding to the enumerator, or
18743 : : NULL_TREE if no such enumerator was found. */
18744 : : tree
18745 : 11070632 : lookup_enumerator (tree enumtype, tree name)
18746 : : {
18747 : 11070632 : tree e;
18748 : 11070632 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
18749 : :
18750 : 11070632 : e = purpose_member (name, TYPE_VALUES (enumtype));
18751 : 22141168 : return e? TREE_VALUE (e) : NULL_TREE;
18752 : : }
18753 : :
18754 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
18755 : :
18756 : : tree
18757 : 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
18758 : : vec<string_int_pair> *values)
18759 : : {
18760 : 0 : location_t saved_loc = input_location;
18761 : 0 : input_location = loc;
18762 : :
18763 : 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
18764 : : NULL_TREE, false, NULL);
18765 : 0 : if (!OPAQUE_ENUM_P (enumtype))
18766 : : {
18767 : 0 : auto_diagnostic_group d;
18768 : 0 : error_at (loc, "multiple definition of %q#T", enumtype);
18769 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
18770 : : "previous definition here");
18771 : 0 : return enumtype;
18772 : 0 : }
18773 : 0 : SET_OPAQUE_ENUM_P (enumtype, false);
18774 : 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
18775 : :
18776 : 0 : for (const string_int_pair &value : values)
18777 : 0 : build_enumerator (get_identifier (value.first),
18778 : 0 : build_int_cst (integer_type_node, value.second),
18779 : : enumtype, NULL_TREE, loc);
18780 : :
18781 : 0 : finish_enum_value_list (enumtype);
18782 : 0 : finish_enum (enumtype);
18783 : :
18784 : 0 : input_location = saved_loc;
18785 : 0 : return enumtype;
18786 : : }
18787 : :
18788 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
18789 : :
18790 : : tree
18791 : 0 : cxx_simulate_record_decl (location_t loc, const char *name,
18792 : : array_slice<const tree> fields)
18793 : : {
18794 : 0 : iloc_sentinel ils (loc);
18795 : :
18796 : 0 : tree ident = get_identifier (name);
18797 : 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
18798 : 0 : if (type != error_mark_node
18799 : 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
18800 : : {
18801 : 0 : error ("redefinition of %q#T", type);
18802 : 0 : type = error_mark_node;
18803 : : }
18804 : 0 : if (type == error_mark_node)
18805 : 0 : return lhd_simulate_record_decl (loc, name, fields);
18806 : :
18807 : 0 : xref_basetypes (type, NULL_TREE);
18808 : 0 : type = begin_class_definition (type);
18809 : 0 : if (type == error_mark_node)
18810 : 0 : return lhd_simulate_record_decl (loc, name, fields);
18811 : :
18812 : 0 : for (tree field : fields)
18813 : 0 : finish_member_declaration (field);
18814 : :
18815 : 0 : type = finish_struct (type, NULL_TREE);
18816 : :
18817 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
18818 : 0 : set_underlying_type (decl);
18819 : 0 : lang_hooks.decls.pushdecl (decl);
18820 : :
18821 : 0 : return type;
18822 : 0 : }
18823 : :
18824 : : /* We're defining DECL. Make sure that its type is OK. */
18825 : :
18826 : : static void
18827 : 142129149 : check_function_type (tree decl, tree current_function_parms)
18828 : : {
18829 : 142129149 : tree fntype = TREE_TYPE (decl);
18830 : 142129149 : tree return_type = complete_type (TREE_TYPE (fntype));
18831 : :
18832 : : /* In a function definition, arg types must be complete. */
18833 : 142129149 : require_complete_types_for_parms (current_function_parms);
18834 : :
18835 : 142129149 : if (dependent_type_p (return_type)
18836 : 142129149 : || type_uses_auto (return_type))
18837 : 42905257 : return;
18838 : 99223892 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
18839 : : {
18840 : 12 : tree args = TYPE_ARG_TYPES (fntype);
18841 : :
18842 : 12 : error ("return type %q#T is incomplete", return_type);
18843 : :
18844 : : /* Make it return void instead. */
18845 : 12 : if (TREE_CODE (fntype) == METHOD_TYPE)
18846 : 3 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
18847 : : void_type_node,
18848 : 3 : TREE_CHAIN (args));
18849 : : else
18850 : 9 : fntype = build_function_type (void_type_node, args);
18851 : 12 : fntype = (cp_build_type_attribute_variant
18852 : 12 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
18853 : 12 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
18854 : 12 : TREE_TYPE (decl) = fntype;
18855 : : }
18856 : : else
18857 : : {
18858 : 99223880 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
18859 : 99223880 : maybe_warn_parm_abi (TREE_TYPE (fntype),
18860 : 99223880 : DECL_SOURCE_LOCATION (decl));
18861 : : }
18862 : : }
18863 : :
18864 : : /* True iff FN is an implicitly-defined default constructor. */
18865 : :
18866 : : static bool
18867 : 4731640 : implicit_default_ctor_p (tree fn)
18868 : : {
18869 : 4731640 : return (DECL_CONSTRUCTOR_P (fn)
18870 : 4731640 : && !user_provided_p (fn)
18871 : 5388795 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
18872 : : }
18873 : :
18874 : : /* Clobber the contents of *this to let the back end know that the object
18875 : : storage is dead when we enter the constructor or leave the destructor. */
18876 : :
18877 : : static tree
18878 : 5606605 : build_clobber_this (clobber_kind kind)
18879 : : {
18880 : : /* Clobbering an empty base is pointless, and harmful if its one byte
18881 : : TYPE_SIZE overlays real data. */
18882 : 5606605 : if (is_empty_class (current_class_type))
18883 : 0 : return void_node;
18884 : :
18885 : : /* If we have virtual bases, clobber the whole object, but only if we're in
18886 : : charge. If we don't have virtual bases, clobber the as-base type so we
18887 : : don't mess with tail padding. */
18888 : 5606605 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
18889 : :
18890 : 5606605 : tree ctype = current_class_type;
18891 : 5606605 : if (!vbases)
18892 : 5588643 : ctype = CLASSTYPE_AS_BASE (ctype);
18893 : :
18894 : 5606605 : tree clobber = build_clobber (ctype, kind);
18895 : :
18896 : 5606605 : tree thisref = current_class_ref;
18897 : 5606605 : if (ctype != current_class_type)
18898 : : {
18899 : 624495 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
18900 : 624495 : thisref = convert_from_reference (thisref);
18901 : : }
18902 : :
18903 : 5606605 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
18904 : 5606605 : if (kind == CLOBBER_OBJECT_BEGIN)
18905 : 4303004 : TREE_SET_CODE (exprstmt, INIT_EXPR);
18906 : 5606605 : if (vbases)
18907 : 17962 : exprstmt = build_if_in_charge (exprstmt);
18908 : :
18909 : : return exprstmt;
18910 : : }
18911 : :
18912 : : /* Create the FUNCTION_DECL for a function definition.
18913 : : DECLSPECS and DECLARATOR are the parts of the declaration;
18914 : : they describe the function's name and the type it returns,
18915 : : but twisted together in a fashion that parallels the syntax of C.
18916 : :
18917 : : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
18918 : : DECLARATOR is really the DECL for the function we are about to
18919 : : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
18920 : : indicating that the function is an inline defined in-class.
18921 : :
18922 : : This function creates a binding context for the function body
18923 : : as well as setting up the FUNCTION_DECL in current_function_decl.
18924 : :
18925 : : For C++, we must first check whether that datum makes any sense.
18926 : : For example, "class A local_a(1,2);" means that variable local_a
18927 : : is an aggregate of type A, which should have a constructor
18928 : : applied to it with the argument list [1, 2].
18929 : :
18930 : : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
18931 : : or may be a BLOCK if the function has been defined previously
18932 : : in this translation unit. On exit, DECL_INITIAL (decl1) will be
18933 : : error_mark_node if the function has never been defined, or
18934 : : a BLOCK if the function has been defined somewhere. */
18935 : :
18936 : : bool
18937 : 142129185 : start_preparsed_function (tree decl1, tree attrs, int flags)
18938 : : {
18939 : 142129185 : tree ctype = NULL_TREE;
18940 : 142129185 : bool doing_friend = false;
18941 : :
18942 : : /* Sanity check. */
18943 : 142129185 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
18944 : 142129185 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
18945 : :
18946 : 142129185 : tree fntype = TREE_TYPE (decl1);
18947 : 142129185 : if (DECL_CLASS_SCOPE_P (decl1))
18948 : 102800864 : ctype = DECL_CONTEXT (decl1);
18949 : : else
18950 : : {
18951 : 78656642 : ctype = DECL_FRIEND_CONTEXT (decl1);
18952 : :
18953 : 39328320 : if (ctype)
18954 : 142129185 : doing_friend = true;
18955 : : }
18956 : :
18957 : : /* Adjust for #pragma target/optimize if this is an artificial function that
18958 : : (probably) didn't go through grokfndecl. We particularly don't want this
18959 : : for deferred instantiations, which should match their template. */
18960 : 142129185 : if (DECL_ARTIFICIAL (decl1))
18961 : 2947716 : decl_attributes (&decl1, NULL_TREE, 0);
18962 : :
18963 : 142129185 : if (DECL_DECLARED_INLINE_P (decl1)
18964 : 142129185 : && lookup_attribute ("noinline", attrs))
18965 : : {
18966 : 0 : auto_urlify_attributes sentinel;
18967 : 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
18968 : : "inline function %qD given attribute %qs", decl1, "noinline");
18969 : 0 : }
18970 : :
18971 : : /* Handle gnu_inline attribute. */
18972 : 142129185 : if (GNU_INLINE_P (decl1))
18973 : : {
18974 : 1576494 : DECL_EXTERNAL (decl1) = 1;
18975 : 1576494 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
18976 : 1576494 : DECL_INTERFACE_KNOWN (decl1) = 1;
18977 : 1576494 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
18978 : : }
18979 : :
18980 : 142129185 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
18981 : : /* This is a constructor, we must ensure that any default args
18982 : : introduced by this definition are propagated to the clones
18983 : : now. The clones are used directly in overload resolution. */
18984 : 18664542 : adjust_clone_args (decl1);
18985 : :
18986 : : /* Sometimes we don't notice that a function is a static member, and
18987 : : build a METHOD_TYPE for it. Fix that up now. */
18988 : 142129185 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
18989 : : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
18990 : :
18991 : : /* Set up current_class_type, and enter the scope of the class, if
18992 : : appropriate. */
18993 : 142129185 : if (ctype)
18994 : 105707469 : push_nested_class (ctype);
18995 : :
18996 : : /* Now that we have entered the scope of the class, we must restore
18997 : : the bindings for any template parameters surrounding DECL1, if it
18998 : : is an inline member template. (Order is important; consider the
18999 : : case where a template parameter has the same name as a field of
19000 : : the class.) It is not until after this point that
19001 : : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
19002 : 142129185 : if (flags & SF_INCLASS_INLINE)
19003 : 69777546 : maybe_begin_member_template_processing (decl1);
19004 : :
19005 : : /* Effective C++ rule 15. */
19006 : 142129185 : if (warn_ecpp
19007 : 123 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
19008 : 30 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
19009 : 142129215 : && VOID_TYPE_P (TREE_TYPE (fntype)))
19010 : 0 : warning (OPT_Weffc__,
19011 : : "%<operator=%> should return a reference to %<*this%>");
19012 : :
19013 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
19014 : : error_mark_node is replaced below (in poplevel) with the BLOCK. */
19015 : 142129185 : if (!DECL_INITIAL (decl1))
19016 : 29525449 : DECL_INITIAL (decl1) = error_mark_node;
19017 : :
19018 : : /* This function exists in static storage.
19019 : : (This does not mean `static' in the C sense!) */
19020 : 142129185 : TREE_STATIC (decl1) = 1;
19021 : :
19022 : : /* We must call push_template_decl after current_class_type is set
19023 : : up. (If we are processing inline definitions after exiting a
19024 : : class scope, current_class_type will be NULL_TREE until set above
19025 : : by push_nested_class.) */
19026 : 142129185 : if (processing_template_decl)
19027 : : {
19028 : 85725207 : tree newdecl1 = push_template_decl (decl1, doing_friend);
19029 : 85725207 : if (newdecl1 == error_mark_node)
19030 : : {
19031 : 36 : if (ctype)
19032 : 9 : pop_nested_class ();
19033 : 36 : return false;
19034 : : }
19035 : 85725171 : decl1 = newdecl1;
19036 : : }
19037 : :
19038 : : /* Make sure the parameter and return types are reasonable. When
19039 : : you declare a function, these types can be incomplete, but they
19040 : : must be complete when you define the function. */
19041 : 142129149 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
19042 : :
19043 : : /* Build the return declaration for the function. */
19044 : 142129149 : tree restype = TREE_TYPE (fntype);
19045 : :
19046 : 142129149 : if (DECL_RESULT (decl1) == NULL_TREE)
19047 : : {
19048 : : /* In a template instantiation, copy the return type location. When
19049 : : parsing, the location will be set in grokdeclarator. */
19050 : 56400222 : location_t loc = input_location;
19051 : 56400222 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
19052 : : {
19053 : 26477177 : tree tmpl = template_for_substitution (decl1);
19054 : 26477177 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
19055 : 25401417 : loc = DECL_SOURCE_LOCATION (res);
19056 : : }
19057 : :
19058 : 56400222 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
19059 : 56400222 : DECL_ARTIFICIAL (resdecl) = 1;
19060 : 56400222 : DECL_IGNORED_P (resdecl) = 1;
19061 : 56400222 : DECL_RESULT (decl1) = resdecl;
19062 : :
19063 : 56400222 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
19064 : : }
19065 : :
19066 : : /* Record the decl so that the function name is defined.
19067 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
19068 : : use the old decl. */
19069 : 142129149 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
19070 : : {
19071 : : /* A specialization is not used to guide overload resolution. */
19072 : 20092253 : if (!DECL_FUNCTION_MEMBER_P (decl1)
19073 : 20079085 : && !(DECL_USE_TEMPLATE (decl1) &&
19074 : 3598 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
19075 : : {
19076 : 9638978 : tree olddecl = pushdecl (decl1);
19077 : :
19078 : 9638978 : if (olddecl == error_mark_node)
19079 : : /* If something went wrong when registering the declaration,
19080 : : use DECL1; we have to have a FUNCTION_DECL to use when
19081 : : parsing the body of the function. */
19082 : : ;
19083 : : else
19084 : : {
19085 : : /* Otherwise, OLDDECL is either a previous declaration
19086 : : of the same function or DECL1 itself. */
19087 : :
19088 : 9638768 : if (warn_missing_declarations
19089 : 21 : && olddecl == decl1
19090 : 15 : && !DECL_MAIN_P (decl1)
19091 : 15 : && TREE_PUBLIC (decl1)
19092 : 9638780 : && !DECL_DECLARED_INLINE_P (decl1))
19093 : : {
19094 : 9 : tree context;
19095 : :
19096 : : /* Check whether DECL1 is in an anonymous
19097 : : namespace. */
19098 : 9 : for (context = DECL_CONTEXT (decl1);
19099 : 18 : context;
19100 : 9 : context = DECL_CONTEXT (context))
19101 : : {
19102 : 12 : if (TREE_CODE (context) == NAMESPACE_DECL
19103 : 12 : && DECL_NAME (context) == NULL_TREE)
19104 : : break;
19105 : : }
19106 : :
19107 : 9 : if (context == NULL)
19108 : 6 : warning_at (DECL_SOURCE_LOCATION (decl1),
19109 : 6 : OPT_Wmissing_declarations,
19110 : : "no previous declaration for %qD", decl1);
19111 : : }
19112 : :
19113 : 9638768 : decl1 = olddecl;
19114 : : }
19115 : : }
19116 : : else
19117 : : {
19118 : : /* We need to set the DECL_CONTEXT. */
19119 : 797531 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
19120 : 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
19121 : : }
19122 : 10436509 : fntype = TREE_TYPE (decl1);
19123 : 10436509 : restype = TREE_TYPE (fntype);
19124 : :
19125 : : /* If #pragma weak applies, mark the decl appropriately now.
19126 : : The pragma only applies to global functions. Because
19127 : : determining whether or not the #pragma applies involves
19128 : : computing the mangled name for the declaration, we cannot
19129 : : apply the pragma until after we have merged this declaration
19130 : : with any previous declarations; if the original declaration
19131 : : has a linkage specification, that specification applies to
19132 : : the definition as well, and may affect the mangled name. */
19133 : 10436509 : if (DECL_FILE_SCOPE_P (decl1))
19134 : 2353215 : maybe_apply_pragma_weak (decl1);
19135 : : }
19136 : :
19137 : : /* We are now in the scope of the function being defined. */
19138 : 142129149 : current_function_decl = decl1;
19139 : :
19140 : : /* Save the parm names or decls from this function's declarator
19141 : : where store_parm_decls will find them. */
19142 : 142129149 : tree current_function_parms = DECL_ARGUMENTS (decl1);
19143 : :
19144 : : /* Let the user know we're compiling this function. */
19145 : 142129149 : announce_function (decl1);
19146 : :
19147 : 142129149 : gcc_assert (DECL_INITIAL (decl1));
19148 : :
19149 : : /* This function may already have been parsed, in which case just
19150 : : return; our caller will skip over the body without parsing. */
19151 : 142129149 : if (DECL_INITIAL (decl1) != error_mark_node)
19152 : : return true;
19153 : :
19154 : : /* Initialize RTL machinery. We cannot do this until
19155 : : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
19156 : : even when processing a template; this is how we get
19157 : : CFUN set up, and our per-function variables initialized.
19158 : : FIXME factor out the non-RTL stuff. */
19159 : 142129149 : cp_binding_level *bl = current_binding_level;
19160 : 142129149 : allocate_struct_function (decl1, processing_template_decl);
19161 : :
19162 : : /* Initialize the language data structures. Whenever we start
19163 : : a new function, we destroy temporaries in the usual way. */
19164 : 142129149 : cfun->language = ggc_cleared_alloc<language_function> ();
19165 : 142129149 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
19166 : 142129149 : current_binding_level = bl;
19167 : :
19168 : : /* If we are (erroneously) defining a function that we have already
19169 : : defined before, wipe out what we knew before. */
19170 : 142129149 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
19171 : 142129149 : FNDECL_USED_AUTO (decl1) = false;
19172 : 142129149 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
19173 : :
19174 : 142129149 : if (!processing_template_decl && type_uses_auto (restype))
19175 : : {
19176 : 805729 : FNDECL_USED_AUTO (decl1) = true;
19177 : 805729 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
19178 : : }
19179 : :
19180 : : /* Start the statement-tree, start the tree now. */
19181 : 142129149 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
19182 : :
19183 : 142129149 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
19184 : : {
19185 : : /* We know that this was set up by `grokclassfn'. We do not
19186 : : wait until `store_parm_decls', since evil parse errors may
19187 : : never get us to that point. Here we keep the consistency
19188 : : between `current_class_type' and `current_class_ptr'. */
19189 : 91764026 : tree t = DECL_ARGUMENTS (decl1);
19190 : :
19191 : 91764026 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
19192 : 91764026 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
19193 : :
19194 : 91764026 : cp_function_chain->x_current_class_ref
19195 : 91764026 : = cp_build_fold_indirect_ref (t);
19196 : : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
19197 : 91764026 : cp_function_chain->x_current_class_ptr = t;
19198 : :
19199 : : /* Constructors and destructors need to know whether they're "in
19200 : : charge" of initializing virtual base classes. */
19201 : 91764026 : t = DECL_CHAIN (t);
19202 : 91764026 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
19203 : : {
19204 : 20057 : current_in_charge_parm = t;
19205 : 20057 : t = DECL_CHAIN (t);
19206 : : }
19207 : 91764026 : if (DECL_HAS_VTT_PARM_P (decl1))
19208 : : {
19209 : 40127 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
19210 : 40127 : current_vtt_parm = t;
19211 : : }
19212 : : }
19213 : :
19214 : 142129149 : bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
19215 : : /* Implicitly-defined methods (like the
19216 : : destructor for a class in which no destructor
19217 : : is explicitly declared) must not be defined
19218 : : until their definition is needed. So, we
19219 : : ignore interface specifications for
19220 : : compiler-generated functions. */
19221 : 142129149 : && !DECL_ARTIFICIAL (decl1));
19222 : 142129149 : struct c_fileinfo *finfo
19223 : 142129149 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
19224 : :
19225 : 142129149 : if (processing_template_decl)
19226 : : /* Don't mess with interface flags. */;
19227 : 56403978 : else if (DECL_INTERFACE_KNOWN (decl1))
19228 : : {
19229 : 17476184 : tree ctx = decl_function_context (decl1);
19230 : :
19231 : 17476184 : if (DECL_NOT_REALLY_EXTERN (decl1))
19232 : 13984393 : DECL_EXTERNAL (decl1) = 0;
19233 : :
19234 : 17476184 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
19235 : : /* This is a function in a local class in an extern inline
19236 : : or template function. */
19237 : 2797337 : comdat_linkage (decl1);
19238 : : }
19239 : : /* If this function belongs to an interface, it is public.
19240 : : If it belongs to someone else's interface, it is also external.
19241 : : This only affects inlines and template instantiations. */
19242 : 38927794 : else if (!finfo->interface_unknown && honor_interface)
19243 : : {
19244 : 112 : if (DECL_DECLARED_INLINE_P (decl1)
19245 : 112 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19246 : : {
19247 : 88 : DECL_EXTERNAL (decl1)
19248 : 176 : = (finfo->interface_only
19249 : 88 : || (DECL_DECLARED_INLINE_P (decl1)
19250 : 42 : && ! flag_implement_inlines
19251 : 0 : && !DECL_VINDEX (decl1)));
19252 : :
19253 : : /* For WIN32 we also want to put these in linkonce sections. */
19254 : 88 : maybe_make_one_only (decl1);
19255 : : }
19256 : : else
19257 : 24 : DECL_EXTERNAL (decl1) = 0;
19258 : 112 : DECL_INTERFACE_KNOWN (decl1) = 1;
19259 : : /* If this function is in an interface implemented in this file,
19260 : : make sure that the back end knows to emit this function
19261 : : here. */
19262 : 112 : if (!DECL_EXTERNAL (decl1))
19263 : 66 : mark_needed (decl1);
19264 : : }
19265 : 38927682 : else if (finfo->interface_unknown && finfo->interface_only
19266 : 0 : && honor_interface)
19267 : : {
19268 : : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
19269 : : interface, we will have both finfo->interface_unknown and
19270 : : finfo->interface_only set. In that case, we don't want to
19271 : : use the normal heuristics because someone will supply a
19272 : : #pragma implementation elsewhere, and deducing it here would
19273 : : produce a conflict. */
19274 : 0 : comdat_linkage (decl1);
19275 : 0 : DECL_EXTERNAL (decl1) = 0;
19276 : 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
19277 : 0 : DECL_DEFER_OUTPUT (decl1) = 1;
19278 : : }
19279 : : else
19280 : : {
19281 : : /* This is a definition, not a reference.
19282 : : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
19283 : 38927682 : if (!GNU_INLINE_P (decl1))
19284 : 38927679 : DECL_EXTERNAL (decl1) = 0;
19285 : :
19286 : 38927682 : if ((DECL_DECLARED_INLINE_P (decl1)
19287 : 2067663 : || DECL_TEMPLATE_INSTANTIATION (decl1))
19288 : 40761947 : && ! DECL_INTERFACE_KNOWN (decl1))
19289 : 38694284 : DECL_DEFER_OUTPUT (decl1) = 1;
19290 : : else
19291 : 233398 : DECL_INTERFACE_KNOWN (decl1) = 1;
19292 : : }
19293 : :
19294 : : /* Determine the ELF visibility attribute for the function. We must not
19295 : : do this before calling "pushdecl", as we must allow "duplicate_decls"
19296 : : to merge any attributes appropriately. We also need to wait until
19297 : : linkage is set. */
19298 : 142129149 : if (!DECL_CLONED_FUNCTION_P (decl1))
19299 : 128261041 : determine_visibility (decl1);
19300 : :
19301 : 142129149 : if (!processing_template_decl)
19302 : 56403978 : maybe_instantiate_noexcept (decl1);
19303 : :
19304 : 142129149 : begin_scope (sk_function_parms, decl1);
19305 : :
19306 : 142129149 : ++function_depth;
19307 : :
19308 : 142129149 : start_fname_decls ();
19309 : :
19310 : 142129149 : store_parm_decls (current_function_parms);
19311 : :
19312 : 142129149 : start_function_contracts (decl1);
19313 : :
19314 : 142129149 : if (!processing_template_decl
19315 : 56403978 : && (flag_lifetime_dse > 1)
19316 : 112788340 : && DECL_CONSTRUCTOR_P (decl1)
19317 : 15884866 : && !DECL_CLONED_FUNCTION_P (decl1)
19318 : : /* Clobbering an empty base is harmful if it overlays real data. */
19319 : 5292262 : && !is_empty_class (current_class_type)
19320 : : /* We can't clobber safely for an implicitly-defined default constructor
19321 : : because part of the initialization might happen before we enter the
19322 : : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
19323 : 146860192 : && !implicit_default_ctor_p (decl1))
19324 : 4303004 : finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN));
19325 : :
19326 : 142129149 : if (!processing_template_decl
19327 : 112807956 : && DECL_CONSTRUCTOR_P (decl1)
19328 : 15887386 : && sanitize_flags_p (SANITIZE_VPTR)
19329 : 1791 : && !DECL_CLONED_FUNCTION_P (decl1)
19330 : 142129746 : && !implicit_default_ctor_p (decl1))
19331 : 441 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
19332 : :
19333 : 142129149 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
19334 : 142128581 : start_lambda_scope (decl1);
19335 : :
19336 : : return true;
19337 : : }
19338 : :
19339 : :
19340 : : /* Like start_preparsed_function, except that instead of a
19341 : : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
19342 : :
19343 : : Returns true on success. If the DECLARATOR is not suitable
19344 : : for a function, we return false, which tells the parser to
19345 : : skip the entire function. */
19346 : :
19347 : : bool
19348 : 38827593 : start_function (cp_decl_specifier_seq *declspecs,
19349 : : const cp_declarator *declarator,
19350 : : tree attrs)
19351 : : {
19352 : 38827593 : tree decl1;
19353 : :
19354 : 38827593 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
19355 : 38827593 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
19356 : 38827593 : if (decl1 == error_mark_node)
19357 : : return false;
19358 : :
19359 : 38827304 : if (DECL_MAIN_P (decl1))
19360 : : /* main must return int. grokfndecl should have corrected it
19361 : : (and issued a diagnostic) if the user got it wrong. */
19362 : 32921 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
19363 : : integer_type_node));
19364 : :
19365 : 38827304 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
19366 : : }
19367 : :
19368 : : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
19369 : : FN. */
19370 : :
19371 : : bool
19372 : 271914697 : use_eh_spec_block (tree fn)
19373 : : {
19374 : 270007239 : return (flag_exceptions && flag_enforce_eh_specs
19375 : 270007239 : && !processing_template_decl
19376 : : /* We insert the EH_SPEC_BLOCK only in the original
19377 : : function; then, it is copied automatically to the
19378 : : clones. */
19379 : 98119690 : && !DECL_CLONED_FUNCTION_P (fn)
19380 : : /* Implicitly-generated constructors and destructors have
19381 : : exception specifications. However, those specifications
19382 : : are the union of the possible exceptions specified by the
19383 : : constructors/destructors for bases and members, so no
19384 : : unallowed exception will ever reach this function. By
19385 : : not creating the EH_SPEC_BLOCK we save a little memory,
19386 : : and we avoid spurious warnings about unreachable
19387 : : code. */
19388 : 84291420 : && !DECL_DEFAULTED_FN (fn)
19389 : 353882741 : && !type_throw_all_p (TREE_TYPE (fn)));
19390 : : }
19391 : :
19392 : : /* Helper function to push ARGS into the current lexical scope. DECL
19393 : : is the function declaration. NONPARMS is used to handle enum
19394 : : constants. */
19395 : :
19396 : : void
19397 : 138718778 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
19398 : : {
19399 : : /* If we're doing semantic analysis, then we'll call pushdecl
19400 : : for each of these. We must do them in reverse order so that
19401 : : they end in the correct forward order. */
19402 : 138718778 : args = nreverse (args);
19403 : :
19404 : 138718778 : tree next;
19405 : 424127270 : for (tree parm = args; parm; parm = next)
19406 : : {
19407 : 285408492 : next = DECL_CHAIN (parm);
19408 : 285408492 : if (TREE_CODE (parm) == PARM_DECL)
19409 : 285408492 : pushdecl (parm);
19410 : 0 : else if (nonparms)
19411 : : {
19412 : : /* If we find an enum constant or a type tag, put it aside for
19413 : : the moment. */
19414 : 0 : TREE_CHAIN (parm) = NULL_TREE;
19415 : 0 : *nonparms = chainon (*nonparms, parm);
19416 : : }
19417 : : }
19418 : :
19419 : : /* Get the decls in their original chain order and record in the
19420 : : function. This is all and only the PARM_DECLs that were
19421 : : pushed into scope by the loop above. */
19422 : 138718778 : DECL_ARGUMENTS (decl) = get_local_decls ();
19423 : 138718778 : }
19424 : :
19425 : : /* Store the parameter declarations into the current function declaration.
19426 : : This is called after parsing the parameter declarations, before
19427 : : digesting the body of the function.
19428 : :
19429 : : Also install to binding contour return value identifier, if any. */
19430 : :
19431 : : static void
19432 : 142129149 : store_parm_decls (tree current_function_parms)
19433 : : {
19434 : 142129149 : tree fndecl = current_function_decl;
19435 : :
19436 : : /* This is a chain of any other decls that came in among the parm
19437 : : declarations. If a parm is declared with enum {foo, bar} x;
19438 : : then CONST_DECLs for foo and bar are put here. */
19439 : 142129149 : tree nonparms = NULL_TREE;
19440 : :
19441 : 142129149 : if (current_function_parms)
19442 : : {
19443 : : /* This case is when the function was defined with an ANSI prototype.
19444 : : The parms already have decls, so we need not do anything here
19445 : : except record them as in effect
19446 : : and complain if any redundant old-style parm decls were written. */
19447 : :
19448 : 136129702 : tree specparms = current_function_parms;
19449 : :
19450 : : /* Must clear this because it might contain TYPE_DECLs declared
19451 : : at class level. */
19452 : 136129702 : current_binding_level->names = NULL;
19453 : :
19454 : 136129702 : do_push_parm_decls (fndecl, specparms, &nonparms);
19455 : : }
19456 : : else
19457 : 5999447 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
19458 : :
19459 : : /* Now store the final chain of decls for the arguments
19460 : : as the decl-chain of the current lexical scope.
19461 : : Put the enumerators in as well, at the front so that
19462 : : DECL_ARGUMENTS is not modified. */
19463 : 142129149 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
19464 : :
19465 : 142129149 : if (use_eh_spec_block (current_function_decl))
19466 : 18728786 : current_eh_spec_block = begin_eh_spec_block ();
19467 : 142129149 : }
19468 : :
19469 : :
19470 : : /* Mark CDTOR's implicit THIS argument for returning, if required by
19471 : : the ABI.. Return the decl for THIS, if it is to be returned, and
19472 : : NULL otherwise. */
19473 : :
19474 : : tree
19475 : 112436567 : maybe_prepare_return_this (tree cdtor)
19476 : : {
19477 : 112436567 : if (targetm.cxx.cdtor_returns_this ())
19478 : 0 : if (tree val = DECL_ARGUMENTS (cdtor))
19479 : : {
19480 : 0 : suppress_warning (val, OPT_Wuse_after_free);
19481 : 0 : return val;
19482 : : }
19483 : :
19484 : : return NULL_TREE;
19485 : : }
19486 : :
19487 : : /* Set the return value of the [cd]tor if the ABI wants that. */
19488 : :
19489 : : void
19490 : 7065225 : maybe_return_this ()
19491 : : {
19492 : 7065225 : if (tree val = maybe_prepare_return_this (current_function_decl))
19493 : : {
19494 : : /* Return the address of the object. */
19495 : 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
19496 : 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
19497 : 0 : DECL_RESULT (current_function_decl), val);
19498 : 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
19499 : 0 : add_stmt (exprstmt);
19500 : : }
19501 : 7065225 : }
19502 : :
19503 : : /* Do all the processing for the beginning of a destructor; set up the
19504 : : vtable pointers and cleanups for bases and members. */
19505 : :
19506 : : static void
19507 : 1499021 : begin_destructor_body (void)
19508 : : {
19509 : 1499021 : tree compound_stmt;
19510 : :
19511 : : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
19512 : : issued an error message. We still want to try to process the
19513 : : body of the function, but initialize_vtbl_ptrs will crash if
19514 : : TYPE_BINFO is NULL. */
19515 : 1499021 : if (COMPLETE_TYPE_P (current_class_type))
19516 : : {
19517 : 1499021 : compound_stmt = begin_compound_stmt (0);
19518 : : /* Make all virtual function table pointers in non-virtual base
19519 : : classes point to CURRENT_CLASS_TYPE's virtual function
19520 : : tables. */
19521 : 1499021 : initialize_vtbl_ptrs (current_class_ptr);
19522 : 1499021 : finish_compound_stmt (compound_stmt);
19523 : :
19524 : 1499021 : if (flag_lifetime_dse
19525 : : /* Clobbering an empty base is harmful if it overlays real data. */
19526 : 1499021 : && !is_empty_class (current_class_type))
19527 : : {
19528 : 1303664 : if (sanitize_flags_p (SANITIZE_VPTR)
19529 : 223 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
19530 : 1303727 : && TYPE_CONTAINS_VPTR_P (current_class_type))
19531 : : {
19532 : 63 : tree binfo = TYPE_BINFO (current_class_type);
19533 : 63 : tree ref
19534 : 63 : = cp_build_fold_indirect_ref (current_class_ptr);
19535 : :
19536 : 63 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
19537 : 63 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
19538 : 63 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
19539 : : NOP_EXPR, vtbl,
19540 : : tf_warning_or_error);
19541 : : /* If the vptr is shared with some virtual nearly empty base,
19542 : : don't clear it if not in charge, the dtor of the virtual
19543 : : nearly empty base will do that later. */
19544 : 63 : if (CLASSTYPE_VBASECLASSES (current_class_type))
19545 : : {
19546 : : tree c = current_class_type;
19547 : 66 : while (CLASSTYPE_PRIMARY_BINFO (c))
19548 : : {
19549 : 63 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
19550 : : {
19551 : 36 : stmt = convert_to_void (stmt, ICV_STATEMENT,
19552 : : tf_warning_or_error);
19553 : 36 : stmt = build_if_in_charge (stmt);
19554 : 36 : break;
19555 : : }
19556 : 27 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
19557 : : }
19558 : : }
19559 : 63 : finish_decl_cleanup (NULL_TREE, stmt);
19560 : : }
19561 : : else
19562 : 1303601 : finish_decl_cleanup (NULL_TREE,
19563 : : build_clobber_this (CLOBBER_OBJECT_END));
19564 : : }
19565 : :
19566 : : /* And insert cleanups for our bases and members so that they
19567 : : will be properly destroyed if we throw. */
19568 : 1499021 : push_base_cleanups ();
19569 : : }
19570 : 1499021 : }
19571 : :
19572 : : /* Do the necessary processing for the beginning of a function body, which
19573 : : in this case includes member-initializers, but not the catch clauses of
19574 : : a function-try-block. Currently, this means opening a binding level
19575 : : for the member-initializers (in a ctor), member cleanups (in a dtor),
19576 : : and capture proxies (in a lambda operator()). */
19577 : :
19578 : : tree
19579 : 113388465 : begin_function_body (void)
19580 : : {
19581 : 231768071 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
19582 : : return NULL_TREE;
19583 : :
19584 : 22608790 : if (processing_template_decl)
19585 : : /* Do nothing now. */;
19586 : : else
19587 : : /* Always keep the BLOCK node associated with the outermost pair of
19588 : : curly braces of a function. These are needed for correct
19589 : : operation of dwarfout.c. */
19590 : 7243438 : keep_next_level (true);
19591 : :
19592 : 22608790 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
19593 : 22608790 : current_binding_level->artificial = 1;
19594 : :
19595 : 22608790 : if (processing_template_decl)
19596 : : /* Do nothing now. */;
19597 : 14486876 : else if (DECL_DESTRUCTOR_P (current_function_decl))
19598 : 1499021 : begin_destructor_body ();
19599 : :
19600 : : return stmt;
19601 : : }
19602 : :
19603 : : /* Do the processing for the end of a function body. Currently, this means
19604 : : closing out the cleanups for fully-constructed bases and members, and in
19605 : : the case of the destructor, deleting the object if desired. Again, this
19606 : : is only meaningful for [cd]tors, since they are the only functions where
19607 : : there is a significant distinction between the main body and any
19608 : : function catch clauses. Handling, say, main() return semantics here
19609 : : would be wrong, as flowing off the end of a function catch clause for
19610 : : main() would also need to return 0. */
19611 : :
19612 : : void
19613 : 113388441 : finish_function_body (tree compstmt)
19614 : : {
19615 : 113388441 : if (compstmt == NULL_TREE)
19616 : : return;
19617 : :
19618 : : /* Close the block. */
19619 : 22608790 : finish_compound_stmt (compstmt);
19620 : :
19621 : 22608790 : if (processing_template_decl)
19622 : : /* Do nothing now. */;
19623 : 7243438 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
19624 : 7243438 : || DECL_DESTRUCTOR_P (current_function_decl))
19625 : 6792123 : maybe_return_this ();
19626 : : }
19627 : :
19628 : : /* Given a function, returns the BLOCK corresponding to the outermost level
19629 : : of curly braces, skipping the artificial block created for constructor
19630 : : initializers. */
19631 : :
19632 : : tree
19633 : 306791 : outer_curly_brace_block (tree fndecl)
19634 : : {
19635 : 306791 : tree block = DECL_INITIAL (fndecl);
19636 : 306791 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
19637 : : return block;
19638 : 992 : block = BLOCK_SUBBLOCKS (block);
19639 : 992 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
19640 : : return block;
19641 : 0 : block = BLOCK_SUBBLOCKS (block);
19642 : 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
19643 : : return block;
19644 : : }
19645 : :
19646 : : /* If FNDECL is a class's key method, add the class to the list of
19647 : : keyed classes that should be emitted. */
19648 : :
19649 : : static void
19650 : 142132975 : record_key_method_defined (tree fndecl)
19651 : : {
19652 : 192501900 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
19653 : 91791640 : && DECL_VIRTUAL_P (fndecl)
19654 : 144756655 : && !processing_template_decl)
19655 : : {
19656 : 1295948 : tree fnclass = DECL_CONTEXT (fndecl);
19657 : 1295948 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
19658 : : {
19659 : 1609 : tree classdecl = TYPE_NAME (fnclass);
19660 : : /* Classes attached to a named module are already handled. */
19661 : 1609 : if (!DECL_LANG_SPECIFIC (classdecl)
19662 : 1657 : || !DECL_MODULE_ATTACH_P (classdecl))
19663 : 1564 : vec_safe_push (keyed_classes, fnclass);
19664 : : }
19665 : : }
19666 : 142132975 : }
19667 : :
19668 : : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
19669 : : of "return *this;" immediately before its location, using FNDECL's
19670 : : first statement (if any) to give the indentation, if appropriate. */
19671 : :
19672 : : static void
19673 : 18 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
19674 : : {
19675 : 18 : location_t indent = UNKNOWN_LOCATION;
19676 : 18 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
19677 : 18 : if (stmts)
19678 : 9 : indent = EXPR_LOCATION (stmts);
19679 : 18 : richloc->add_fixit_insert_formatted ("return *this;",
19680 : : richloc->get_loc (),
19681 : : indent);
19682 : 18 : }
19683 : :
19684 : : /* Finish up a function declaration and compile that function
19685 : : all the way to assembler language output. The free the storage
19686 : : for the function definition. INLINE_P is TRUE if we just
19687 : : finished processing the body of an in-class inline function
19688 : : definition. (This processing will have taken place after the
19689 : : class definition is complete.) */
19690 : :
19691 : : tree
19692 : 142129113 : finish_function (bool inline_p)
19693 : : {
19694 : 142129113 : tree fndecl = current_function_decl;
19695 : 142129113 : tree fntype, ctype = NULL_TREE;
19696 : 142129113 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
19697 : :
19698 : : /* When we get some parse errors, we can end up without a
19699 : : current_function_decl, so cope. */
19700 : 142129113 : if (fndecl == NULL_TREE || fndecl == error_mark_node)
19701 : 0 : return error_mark_node;
19702 : :
19703 : 142129113 : bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
19704 : 142129113 : && !processing_template_decl);
19705 : :
19706 : 142129113 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19707 : 142128545 : finish_lambda_scope ();
19708 : :
19709 : 142129113 : if (c_dialect_objc ())
19710 : 0 : objc_finish_function ();
19711 : :
19712 : 142129113 : record_key_method_defined (fndecl);
19713 : :
19714 : 142129113 : fntype = TREE_TYPE (fndecl);
19715 : :
19716 : : /* TREE_READONLY (fndecl) = 1;
19717 : : This caused &foo to be of type ptr-to-const-function
19718 : : which then got a warning when stored in a ptr-to-function variable. */
19719 : :
19720 : 142129113 : gcc_assert (building_stmt_list_p ());
19721 : : /* The current function is being defined, so its DECL_INITIAL should
19722 : : be set, and unless there's a multiple definition, it should be
19723 : : error_mark_node. */
19724 : 142129113 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
19725 : :
19726 : 142129113 : cp_coroutine_transform *coroutine = nullptr;
19727 : 142129113 : if (flag_coroutines
19728 : 46818732 : && !processing_template_decl
19729 : 20382951 : && DECL_COROUTINE_P (fndecl)
19730 : 142130778 : && !DECL_RAMP_FN (fndecl))
19731 : : {
19732 : 1665 : gcc_checking_assert (!DECL_CLONED_FUNCTION_P (fndecl)
19733 : : && !DECL_DEFAULTED_FN (fndecl));
19734 : 1665 : coroutine = new cp_coroutine_transform (fndecl, inline_p);
19735 : 1665 : if (coroutine && coroutine->cp_valid_coroutine ())
19736 : 1665 : coroutine->apply_transforms ();
19737 : :
19738 : : /* We should handle coroutine IFNs in middle end lowering. */
19739 : 1665 : cfun->coroutine_component = true;
19740 : :
19741 : : /* Do not try to process the ramp's EH unless outlining succeeded. */
19742 : 1665 : if (coroutine->cp_valid_coroutine () && use_eh_spec_block (fndecl))
19743 : 360 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19744 : : (TREE_TYPE (fndecl)),
19745 : 360 : current_eh_spec_block);
19746 : :
19747 : : /* If outlining succeeded, then add contracts handling if needed. */
19748 : 1665 : if (coroutine->cp_valid_coroutine () && do_contracts)
19749 : 6 : maybe_apply_function_contracts (fndecl);
19750 : : }
19751 : : else
19752 : : /* For a cloned function, we've already got all the code we need;
19753 : : there's no need to add any extra bits. */
19754 : 142127448 : if (!DECL_CLONED_FUNCTION_P (fndecl))
19755 : : {
19756 : : /* Make it so that `main' always returns 0 by default. */
19757 : 128259340 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
19758 : 128292246 : && !TREE_THIS_VOLATILE (current_function_decl))
19759 : 32906 : finish_return_stmt (integer_zero_node);
19760 : :
19761 : 128259340 : if (use_eh_spec_block (current_function_decl))
19762 : 18728423 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
19763 : : (TREE_TYPE (current_function_decl)),
19764 : 18728423 : current_eh_spec_block);
19765 : :
19766 : 128259340 : if (do_contracts)
19767 : 420 : maybe_apply_function_contracts (current_function_decl);
19768 : :
19769 : : }
19770 : :
19771 : : /* If we're saving up tree structure, tie off the function now. */
19772 : 142129113 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
19773 : :
19774 : 142129113 : finish_fname_decls ();
19775 : :
19776 : : /* This must come after expand_function_end because cleanups might
19777 : : have declarations (from inline functions) that need to go into
19778 : : this function's blocks. */
19779 : :
19780 : : /* If the current binding level isn't the outermost binding level
19781 : : for this function, either there is a bug, or we have experienced
19782 : : syntax errors and the statement tree is malformed. */
19783 : 142129113 : if (current_binding_level->kind != sk_function_parms)
19784 : : {
19785 : : /* Make sure we have already experienced errors. */
19786 : 12 : gcc_assert (errorcount);
19787 : :
19788 : : /* Throw away the broken statement tree and extra binding
19789 : : levels. */
19790 : 12 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
19791 : :
19792 : 27 : while (current_binding_level->kind != sk_function_parms)
19793 : : {
19794 : 15 : if (current_binding_level->kind == sk_class)
19795 : 0 : pop_nested_class ();
19796 : : else
19797 : 15 : poplevel (0, 0, 0);
19798 : : }
19799 : : }
19800 : 142129113 : poplevel (1, 0, 1);
19801 : :
19802 : : /* Statements should always be full-expressions at the outermost set
19803 : : of curly braces for a function. */
19804 : 142129113 : gcc_assert (stmts_are_full_exprs_p ());
19805 : :
19806 : : /* If there are no return statements in a function with auto return type,
19807 : : the return type is void. But if the declared type is something like
19808 : : auto*, this is an error. */
19809 : 56403942 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
19810 : 142934842 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19811 : : {
19812 : 226105 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19813 : 226090 : && !current_function_returns_value
19814 : 452008 : && !current_function_returns_null)
19815 : : {
19816 : : /* We haven't applied return type deduction because we haven't
19817 : : seen any return statements. Do that now. */
19818 : 225903 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19819 : 225903 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
19820 : : void_node, node, tf_warning_or_error,
19821 : : adc_return_type);
19822 : :
19823 : 225903 : apply_deduced_return_type (fndecl, void_type_node);
19824 : 225903 : fntype = TREE_TYPE (fndecl);
19825 : : }
19826 : 202 : else if (!current_function_returns_value
19827 : 6 : && !current_function_returns_null)
19828 : : {
19829 : 6 : auto_diagnostic_group d;
19830 : 12 : error ("no return statements in function returning %qT",
19831 : 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
19832 : 6 : inform (input_location, "only plain %<auto%> return type can be "
19833 : : "deduced to %<void%>");
19834 : 6 : }
19835 : : }
19836 : :
19837 : 142129113 : if (FNDECL_USED_AUTO (fndecl)
19838 : 142129113 : && TREE_TYPE (fntype) != DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
19839 : 805527 : if (location_t fcloc = failed_completion_location (fndecl))
19840 : : {
19841 : 6 : auto_diagnostic_group adg;
19842 : 6 : if (warning (OPT_Wsfinae_incomplete_,
19843 : : "defining %qD, which previously failed to be deduced "
19844 : : "in a SFINAE context", fndecl)
19845 : 6 : && warn_sfinae_incomplete == 1)
19846 : 6 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
19847 : : "-Wsfinae-incomplete=2");
19848 : 6 : }
19849 : :
19850 : : /* Remember that we were in class scope. */
19851 : 142129113 : if (current_class_name)
19852 : 105707457 : ctype = current_class_type;
19853 : :
19854 : 142129113 : if (DECL_DELETED_FN (fndecl))
19855 : : {
19856 : 87 : DECL_INITIAL (fndecl) = error_mark_node;
19857 : 87 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
19858 : 87 : goto cleanup;
19859 : : }
19860 : :
19861 : 142129026 : if (flag_openmp)
19862 : 290910 : if (tree attr = lookup_attribute ("omp declare variant base",
19863 : 290910 : DECL_ATTRIBUTES (fndecl)))
19864 : 157 : omp_declare_variant_finalize (fndecl, attr);
19865 : :
19866 : : /* Complain if there's just no return statement. */
19867 : 142129026 : if ((warn_return_type
19868 : 42054 : || (cxx_dialect >= cxx14
19869 : 33998 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
19870 : 142097603 : && !VOID_TYPE_P (TREE_TYPE (fntype))
19871 : 87409595 : && !dependent_type_p (TREE_TYPE (fntype))
19872 : 45315592 : && !current_function_returns_value && !current_function_returns_null
19873 : : /* Don't complain if we abort or throw. */
19874 : 63553 : && !current_function_returns_abnormally
19875 : : /* Don't complain if there's an infinite loop. */
19876 : 922 : && !current_function_infinite_loop
19877 : : /* Don't complain if we are declared noreturn. */
19878 : 854 : && !TREE_THIS_VOLATILE (fndecl)
19879 : 848 : && !DECL_NAME (DECL_RESULT (fndecl))
19880 : 848 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
19881 : : /* Structor return values (if any) are set by the compiler. */
19882 : 1226 : && !DECL_CONSTRUCTOR_P (fndecl)
19883 : 613 : && !DECL_DESTRUCTOR_P (fndecl)
19884 : 142129639 : && targetm.warn_func_return (fndecl))
19885 : : {
19886 : 607 : gcc_rich_location richloc (input_location);
19887 : : /* Potentially add a "return *this;" fix-it hint for
19888 : : assignment operators. */
19889 : 607 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
19890 : : {
19891 : 39 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
19892 : 39 : if (TREE_CODE (valtype) == REFERENCE_TYPE
19893 : 32 : && current_class_ref
19894 : 26 : && same_type_ignoring_top_level_qualifiers_p
19895 : 26 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
19896 : 71 : && global_dc->option_enabled_p (OPT_Wreturn_type))
19897 : 18 : add_return_star_this_fixit (&richloc, fndecl);
19898 : : }
19899 : 607 : if (cxx_dialect >= cxx14
19900 : 607 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
19901 : 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
19902 : : "returning non-void");
19903 : 605 : else if (warning_at (&richloc, OPT_Wreturn_type,
19904 : : "no return statement in function returning "
19905 : : "non-void"))
19906 : 159 : suppress_warning (fndecl, OPT_Wreturn_type);
19907 : 607 : }
19908 : :
19909 : : /* Lambda closure members are implicitly constexpr if possible. */
19910 : 142129026 : if (cxx_dialect >= cxx17
19911 : 243743959 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
19912 : 1207086 : DECL_DECLARED_CONSTEXPR_P (fndecl)
19913 : 1207086 : = ((processing_template_decl
19914 : 535239 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
19915 : 2413143 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
19916 : :
19917 : : /* Save constexpr function body before it gets munged by
19918 : : the NRV transformation. */
19919 : 142129026 : maybe_save_constexpr_fundef (fndecl);
19920 : :
19921 : : /* Invoke the pre-genericize plugin before we start munging things. */
19922 : 142129026 : if (!processing_template_decl)
19923 : 56403855 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
19924 : :
19925 : : /* Perform delayed folding before NRV transformation. */
19926 : 142129026 : if (!processing_template_decl
19927 : 112807710 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19928 : 198271947 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19929 : 56142483 : cp_fold_function (fndecl);
19930 : :
19931 : : /* Set up the named return value optimization, if we can. Candidate
19932 : : variables are selected in check_return_expr. */
19933 : 142129026 : if (tree r = current_function_return_value)
19934 : : {
19935 : 35248586 : if (r != error_mark_node)
19936 : 179471 : finalize_nrv (fndecl, r);
19937 : 35248586 : current_function_return_value = NULL_TREE;
19938 : : }
19939 : :
19940 : : /* Must mark the RESULT_DECL as being in this function. */
19941 : 142129026 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
19942 : :
19943 : : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
19944 : : to the FUNCTION_DECL node itself. */
19945 : 142129026 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
19946 : :
19947 : : /* Store the end of the function, so that we get good line number
19948 : : info for the epilogue. */
19949 : 142129026 : cfun->function_end_locus = input_location;
19950 : :
19951 : : /* Complain about parameters that are only set, but never otherwise used. */
19952 : 142129026 : if (warn_unused_but_set_parameter
19953 : 928464 : && !processing_template_decl
19954 : 476004 : && errorcount == unused_but_set_errorcount
19955 : 142605027 : && !DECL_CLONED_FUNCTION_P (fndecl))
19956 : : {
19957 : 372487 : tree decl;
19958 : :
19959 : 372487 : for (decl = DECL_ARGUMENTS (fndecl);
19960 : 992184 : decl;
19961 : 619697 : decl = DECL_CHAIN (decl))
19962 : 619697 : if (TREE_USED (decl)
19963 : 511897 : && TREE_CODE (decl) == PARM_DECL
19964 : 511897 : && !DECL_READ_P (decl)
19965 : 225 : && DECL_NAME (decl)
19966 : 225 : && !DECL_ARTIFICIAL (decl)
19967 : 225 : && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter_)
19968 : 225 : && !DECL_IN_SYSTEM_HEADER (decl)
19969 : 225 : && TREE_TYPE (decl) != error_mark_node
19970 : 225 : && !TYPE_REF_P (TREE_TYPE (decl))
19971 : 619922 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
19972 : 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
19973 : 225 : warning_at (DECL_SOURCE_LOCATION (decl),
19974 : 225 : OPT_Wunused_but_set_parameter_,
19975 : : "parameter %qD set but not used", decl);
19976 : 372487 : unused_but_set_errorcount = errorcount;
19977 : : }
19978 : :
19979 : : /* Complain about locally defined typedefs that are not used in this
19980 : : function. */
19981 : 142129026 : maybe_warn_unused_local_typedefs ();
19982 : :
19983 : : /* Possibly warn about unused parameters. */
19984 : 142129026 : if (warn_unused_parameter
19985 : 659148 : && !processing_template_decl
19986 : 142447521 : && !DECL_CLONED_FUNCTION_P (fndecl))
19987 : 244735 : do_warn_unused_parameter (fndecl);
19988 : :
19989 : : /* Genericize before inlining. */
19990 : 142129026 : if (!processing_template_decl
19991 : 112807710 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
19992 : 198271655 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
19993 : 56142191 : cp_genericize (fndecl);
19994 : :
19995 : : /* If this function can't throw any exceptions, remember that. */
19996 : 142129026 : if (!processing_template_decl
19997 : 56403855 : && !cp_function_chain->can_throw
19998 : 42622953 : && !flag_non_call_exceptions
19999 : 42618147 : && !decl_replaceable_p (fndecl,
20000 : 42618147 : opt_for_fn (fndecl, flag_semantic_interposition))
20001 : 184495595 : && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl)))
20002 : 42364205 : TREE_NOTHROW (fndecl) = 1;
20003 : :
20004 : 142129113 : cleanup:
20005 : :
20006 : : /* We're leaving the context of this function, so zap cfun. It's still in
20007 : : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
20008 : 142129113 : set_cfun (NULL);
20009 : 142129113 : current_function_decl = NULL;
20010 : :
20011 : : /* If this is an in-class inline definition, we may have to pop the
20012 : : bindings for the template parameters that we added in
20013 : : maybe_begin_member_template_processing when start_function was
20014 : : called. */
20015 : 142129113 : if (inline_p)
20016 : 69777974 : maybe_end_member_template_processing ();
20017 : :
20018 : : /* Leave the scope of the class. */
20019 : 142129113 : if (ctype)
20020 : 105707457 : pop_nested_class ();
20021 : :
20022 : 142129113 : --function_depth;
20023 : :
20024 : : /* Clean up. */
20025 : 142129113 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
20026 : :
20027 : : /* Build outlined functions for coroutines and contracts. */
20028 : :
20029 : 142129113 : if (coroutine)
20030 : : {
20031 : : /* Emit the resumer and destroyer functions now, providing that we have
20032 : : not encountered some fatal error. */
20033 : 1665 : if (coroutine->cp_valid_coroutine ())
20034 : : {
20035 : 1581 : coroutine->finish_transforms ();
20036 : 1581 : resumer = coroutine->get_resumer ();
20037 : 1581 : destroyer = coroutine->get_destroyer ();
20038 : 1581 : expand_or_defer_fn (resumer);
20039 : 1581 : expand_or_defer_fn (destroyer);
20040 : : }
20041 : 1665 : delete coroutine;
20042 : : }
20043 : :
20044 : : /* If we have used outlined contracts checking functions, build and emit
20045 : : them here. */
20046 : 142129113 : finish_function_contracts (fndecl);
20047 : :
20048 : 142129113 : return fndecl;
20049 : : }
20050 : :
20051 : : /* Create the FUNCTION_DECL for a function definition.
20052 : : DECLSPECS and DECLARATOR are the parts of the declaration;
20053 : : they describe the return type and the name of the function,
20054 : : but twisted together in a fashion that parallels the syntax of C.
20055 : :
20056 : : This function creates a binding context for the function body
20057 : : as well as setting up the FUNCTION_DECL in current_function_decl.
20058 : :
20059 : : Returns a FUNCTION_DECL on success.
20060 : :
20061 : : If the DECLARATOR is not suitable for a function (it defines a datum
20062 : : instead), we return 0, which tells yyparse to report a parse error.
20063 : :
20064 : : May return void_type_node indicating that this method is actually
20065 : : a friend. See grokfield for more details.
20066 : :
20067 : : Came here with a `.pushlevel' .
20068 : :
20069 : : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
20070 : : CHANGES TO CODE IN `grokfield'. */
20071 : :
20072 : : tree
20073 : 69893494 : grokmethod (cp_decl_specifier_seq *declspecs,
20074 : : const cp_declarator *declarator, tree attrlist)
20075 : : {
20076 : 69893494 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
20077 : 69893494 : &attrlist);
20078 : :
20079 : 69893494 : if (fndecl == error_mark_node)
20080 : : return error_mark_node;
20081 : :
20082 : 69893391 : if (attrlist)
20083 : 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
20084 : :
20085 : : /* Pass friends other than inline friend functions back. */
20086 : 69893391 : if (fndecl == void_type_node)
20087 : : return fndecl;
20088 : :
20089 : 69893391 : if (DECL_IN_AGGR_P (fndecl))
20090 : : {
20091 : 0 : if (DECL_CLASS_SCOPE_P (fndecl))
20092 : 0 : error ("%qD is already defined in class %qT", fndecl,
20093 : 0 : DECL_CONTEXT (fndecl));
20094 : 0 : return error_mark_node;
20095 : : }
20096 : :
20097 : 69893391 : check_template_shadow (fndecl);
20098 : :
20099 : : /* p1779 ABI-Isolation makes inline not a default for in-class
20100 : : definitions attached to a named module. If the user explicitly
20101 : : made it inline, grokdeclarator will already have done the right
20102 : : things. */
20103 : 69893391 : if ((!named_module_attach_p ()
20104 : 582 : || flag_module_implicit_inline
20105 : : /* Lambda's operator function remains inline. */
20106 : 1149 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
20107 : : /* If the user explicitly asked for this to be inline, we don't
20108 : : need to do more, but more importantly we want to warn if we
20109 : : can't inline it. */
20110 : 69893048 : && !DECL_DECLARED_INLINE_P (fndecl))
20111 : : {
20112 : 50786482 : if (TREE_PUBLIC (fndecl))
20113 : 49358223 : DECL_COMDAT (fndecl) = 1;
20114 : 50786482 : DECL_DECLARED_INLINE_P (fndecl) = 1;
20115 : : /* It's ok if we can't inline this. */
20116 : 50786482 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
20117 : : }
20118 : :
20119 : : /* We process method specializations in finish_struct_1. */
20120 : 69893391 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
20121 : : {
20122 : : /* Avoid calling decl_spec_seq... until we have to. */
20123 : 57494789 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
20124 : 57494789 : fndecl = push_template_decl (fndecl, friendp);
20125 : 57494789 : if (fndecl == error_mark_node)
20126 : : return fndecl;
20127 : : }
20128 : :
20129 : 69893384 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
20130 : : {
20131 : 0 : fndecl = copy_node (fndecl);
20132 : 0 : TREE_CHAIN (fndecl) = NULL_TREE;
20133 : : }
20134 : :
20135 : 69893384 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
20136 : :
20137 : 69893384 : DECL_IN_AGGR_P (fndecl) = 1;
20138 : 69893384 : return fndecl;
20139 : : }
20140 : :
20141 : :
20142 : : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
20143 : : we can lay it out later, when and if its type becomes complete.
20144 : :
20145 : : Also handle constexpr variables where the initializer involves
20146 : : an unlowered PTRMEM_CST because the class isn't complete yet. */
20147 : :
20148 : : void
20149 : 87872369 : maybe_register_incomplete_var (tree var)
20150 : : {
20151 : 87872369 : gcc_assert (VAR_P (var));
20152 : :
20153 : : /* Keep track of variables with incomplete types. */
20154 : 39153869 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
20155 : 127024621 : && DECL_EXTERNAL (var))
20156 : : {
20157 : 5510615 : tree inner_type = TREE_TYPE (var);
20158 : :
20159 : 5667989 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
20160 : 157374 : inner_type = TREE_TYPE (inner_type);
20161 : 5510615 : inner_type = TYPE_MAIN_VARIANT (inner_type);
20162 : :
20163 : 5553354 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
20164 : : /* RTTI TD entries are created while defining the type_info. */
20165 : 5510615 : || (TYPE_LANG_SPECIFIC (inner_type)
20166 : 2247741 : && TYPE_BEING_DEFINED (inner_type)))
20167 : : {
20168 : 42739 : incomplete_var iv = {var, inner_type};
20169 : 42739 : vec_safe_push (incomplete_vars, iv);
20170 : : }
20171 : 10632121 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
20172 : 3424996 : && DECL_CLASS_SCOPE_P (var)
20173 : 1079545 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
20174 : 1079545 : && decl_constant_var_p (var)
20175 : 6170418 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
20176 : : {
20177 : : /* When the outermost open class is complete we can resolve any
20178 : : pointers-to-members. */
20179 : 13 : tree context = outermost_open_class ();
20180 : 13 : incomplete_var iv = {var, context};
20181 : 13 : vec_safe_push (incomplete_vars, iv);
20182 : : }
20183 : : }
20184 : 87872369 : }
20185 : :
20186 : : /* Called when a class type (given by TYPE) is defined. If there are
20187 : : any existing VAR_DECLs whose type has been completed by this
20188 : : declaration, update them now. */
20189 : :
20190 : : void
20191 : 55850139 : complete_vars (tree type)
20192 : : {
20193 : 55850139 : unsigned ix;
20194 : 55850139 : incomplete_var *iv;
20195 : :
20196 : 56274289 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
20197 : : {
20198 : 424150 : if (same_type_p (type, iv->incomplete_type))
20199 : : {
20200 : 41606 : tree var = iv->decl;
20201 : 41606 : tree type = TREE_TYPE (var);
20202 : :
20203 : 41606 : if (type != error_mark_node
20204 : 41606 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
20205 : 41603 : == iv->incomplete_type))
20206 : : {
20207 : : /* Complete the type of the variable. */
20208 : 41590 : complete_type (type);
20209 : 41590 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
20210 : 41590 : if (COMPLETE_TYPE_P (type))
20211 : 41575 : layout_var_decl (var);
20212 : : }
20213 : :
20214 : : /* Remove this entry from the list. */
20215 : 41606 : incomplete_vars->unordered_remove (ix);
20216 : : }
20217 : : else
20218 : 382544 : ix++;
20219 : : }
20220 : 55850139 : }
20221 : :
20222 : : /* If DECL is of a type which needs a cleanup, build and return an
20223 : : expression to perform that cleanup here. Return NULL_TREE if no
20224 : : cleanup need be done. DECL can also be a _REF when called from
20225 : : split_nonconstant_init_1. */
20226 : :
20227 : : tree
20228 : 65157056 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
20229 : : {
20230 : 65157056 : tree type;
20231 : 65157056 : tree attr;
20232 : 65157056 : tree cleanup;
20233 : :
20234 : : /* Assume no cleanup is required. */
20235 : 65157056 : cleanup = NULL_TREE;
20236 : :
20237 : 65157056 : if (!decl || error_operand_p (decl))
20238 : : return cleanup;
20239 : :
20240 : : /* Handle "__attribute__((cleanup))". We run the cleanup function
20241 : : before the destructor since the destructor is what actually
20242 : : terminates the lifetime of the object. */
20243 : 65157056 : if (DECL_P (decl))
20244 : 64791336 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
20245 : : else
20246 : : attr = NULL_TREE;
20247 : 64791336 : if (attr)
20248 : : {
20249 : 157 : tree id;
20250 : 157 : tree fn;
20251 : 157 : tree arg;
20252 : :
20253 : : /* Get the name specified by the user for the cleanup function. */
20254 : 157 : id = TREE_VALUE (TREE_VALUE (attr));
20255 : : /* Look up the name to find the cleanup function to call. It is
20256 : : important to use lookup_name here because that is what is
20257 : : used in c-common.cc:handle_cleanup_attribute when performing
20258 : : initial checks on the attribute. Note that those checks
20259 : : include ensuring that the function found is not an overloaded
20260 : : function, or an object with an overloaded call operator,
20261 : : etc.; we can rely on the fact that the function found is an
20262 : : ordinary FUNCTION_DECL. */
20263 : 157 : fn = lookup_name (id);
20264 : 157 : arg = build_address (decl);
20265 : 157 : if (!mark_used (decl, complain) && !(complain & tf_error))
20266 : 0 : return error_mark_node;
20267 : 157 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
20268 : 157 : if (cleanup == error_mark_node)
20269 : : return error_mark_node;
20270 : : }
20271 : : /* Handle ordinary C++ destructors. */
20272 : 65157044 : type = TREE_TYPE (decl);
20273 : 65157044 : if (type_build_dtor_call (type))
20274 : : {
20275 : 5187644 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
20276 : 5187644 : tree addr;
20277 : 5187644 : tree call;
20278 : :
20279 : 5187644 : if (TREE_CODE (type) == ARRAY_TYPE)
20280 : : addr = decl;
20281 : : else
20282 : 5185902 : addr = build_address (decl);
20283 : :
20284 : 5187644 : call = build_delete (input_location, TREE_TYPE (addr), addr,
20285 : : sfk_complete_destructor, flags, 0, complain);
20286 : 5187644 : if (call == error_mark_node)
20287 : : cleanup = error_mark_node;
20288 : 5187605 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
20289 : : /* Discard the call. */;
20290 : 3888296 : else if (decl_maybe_constant_destruction (decl, type)
20291 : 3888443 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
20292 : 147 : cxx_constant_dtor (call, decl);
20293 : 3888149 : else if (cleanup)
20294 : 3 : cleanup = cp_build_compound_expr (cleanup, call, complain);
20295 : : else
20296 : : cleanup = call;
20297 : : }
20298 : :
20299 : : /* build_delete sets the location of the destructor call to the
20300 : : current location, even though the destructor is going to be
20301 : : called later, at the end of the current scope. This can lead to
20302 : : a "jumpy" behavior for users of debuggers when they step around
20303 : : the end of the block. So let's unset the location of the
20304 : : destructor call instead. */
20305 : 65157044 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
20306 : 65157044 : if (cleanup && CONVERT_EXPR_P (cleanup))
20307 : 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
20308 : :
20309 : 0 : if (cleanup
20310 : 3888330 : && DECL_P (decl)
20311 : 3887859 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
20312 : : /* Treat objects with destructors as used; the destructor may do
20313 : : something substantive. */
20314 : 3887853 : && !mark_used (decl, complain) && !(complain & tf_error))
20315 : 0 : return error_mark_node;
20316 : :
20317 : 3888330 : if (cleanup && cfun && !processing_template_decl
20318 : 68608417 : && !expr_noexcept_p (cleanup, tf_none))
20319 : 4442 : cp_function_chain->throwing_cleanup = true;
20320 : :
20321 : : return cleanup;
20322 : : }
20323 : :
20324 : :
20325 : : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
20326 : : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
20327 : : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
20328 : :
20329 : : tree
20330 : 2720296 : static_fn_type (tree memfntype)
20331 : : {
20332 : 2720296 : tree fntype;
20333 : 2720296 : tree args;
20334 : :
20335 : 2720296 : if (TYPE_PTRMEMFUNC_P (memfntype))
20336 : 21350 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
20337 : 2720296 : if (INDIRECT_TYPE_P (memfntype)
20338 : 2389547 : || TREE_CODE (memfntype) == FUNCTION_DECL)
20339 : 2542888 : memfntype = TREE_TYPE (memfntype);
20340 : 2720296 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
20341 : : return memfntype;
20342 : 552085 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
20343 : 552085 : args = TYPE_ARG_TYPES (memfntype);
20344 : 552085 : fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
20345 : 552085 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
20346 : 552085 : fntype = (cp_build_type_attribute_variant
20347 : 552085 : (fntype, TYPE_ATTRIBUTES (memfntype)));
20348 : 552085 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
20349 : 552085 : return fntype;
20350 : : }
20351 : :
20352 : : /* DECL was originally constructed as a non-static member function,
20353 : : but turned out to be static. Update it accordingly. */
20354 : :
20355 : : void
20356 : 161183 : revert_static_member_fn (tree decl)
20357 : : {
20358 : 161183 : tree stype = static_fn_type (decl);
20359 : 161183 : cp_cv_quals quals = type_memfn_quals (stype);
20360 : 161183 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
20361 : :
20362 : 161183 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
20363 : 9 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
20364 : :
20365 : 161183 : TREE_TYPE (decl) = stype;
20366 : :
20367 : 161183 : if (DECL_ARGUMENTS (decl))
20368 : 161183 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
20369 : 161183 : DECL_STATIC_FUNCTION_P (decl) = 1;
20370 : 161183 : }
20371 : :
20372 : : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
20373 : : one of the language-independent trees. */
20374 : :
20375 : : enum cp_tree_node_structure_enum
20376 : 32148682226 : cp_tree_node_structure (union lang_tree_node * t)
20377 : : {
20378 : 32148682226 : switch (TREE_CODE (&t->generic))
20379 : : {
20380 : : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
20381 : 139399691 : case BASELINK: return TS_CP_BASELINK;
20382 : 32991944 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
20383 : 10615539 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
20384 : 3308745 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
20385 : 1929992619 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
20386 : 2059010 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
20387 : 157952 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
20388 : 613353127 : case OVERLOAD: return TS_CP_OVERLOAD;
20389 : 774 : case PTRMEM_CST: return TS_CP_PTRMEM;
20390 : 13840618 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
20391 : 594628086 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
20392 : 1364627518 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
20393 : 589647269 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
20394 : 14246623 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
20395 : 3 : case TU_LOCAL_ENTITY: return TS_CP_TU_LOCAL_ENTITY;
20396 : 2447981 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
20397 : 26837364727 : default: return TS_CP_GENERIC;
20398 : : }
20399 : : }
20400 : :
20401 : : bool
20402 : 9977 : cp_missing_noreturn_ok_p (tree decl)
20403 : : {
20404 : : /* A missing noreturn is ok for the `main' function. */
20405 : 9977 : return DECL_MAIN_P (decl);
20406 : : }
20407 : :
20408 : : /* Return the decl used to identify the COMDAT group into which DECL should
20409 : : be placed. */
20410 : :
20411 : : tree
20412 : 84677880 : cxx_comdat_group (tree decl)
20413 : : {
20414 : : /* Virtual tables, construction virtual tables, and virtual table
20415 : : tables all go in a single COMDAT group, named after the primary
20416 : : virtual table. */
20417 : 84677880 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
20418 : 447659 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
20419 : : /* For all other DECLs, the COMDAT group is the mangled name of the
20420 : : declaration itself. */
20421 : : else
20422 : : {
20423 : 84230221 : while (DECL_THUNK_P (decl))
20424 : : {
20425 : : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
20426 : : into the same section as the target function. In that case
20427 : : we must return target's name. */
20428 : 0 : tree target = THUNK_TARGET (decl);
20429 : 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
20430 : 0 : && DECL_SECTION_NAME (target) != NULL
20431 : 0 : && DECL_ONE_ONLY (target))
20432 : : decl = target;
20433 : : else
20434 : : break;
20435 : : }
20436 : : /* If a ctor/dtor has already set the comdat group by
20437 : : maybe_clone_body, don't override it. */
20438 : 84230221 : if (SUPPORTS_ONE_ONLY
20439 : 84230221 : && TREE_CODE (decl) == FUNCTION_DECL
20440 : 84230221 : && DECL_CLONED_FUNCTION_P (decl))
20441 : 15170833 : if (tree comdat = DECL_COMDAT_GROUP (decl))
20442 : : return comdat;
20443 : : }
20444 : :
20445 : : return decl;
20446 : : }
20447 : :
20448 : : /* Returns the return type for FN as written by the user, which may include
20449 : : a placeholder for a deduced return type. */
20450 : :
20451 : : tree
20452 : 484990061 : fndecl_declared_return_type (tree fn)
20453 : : {
20454 : 484990061 : fn = STRIP_TEMPLATE (fn);
20455 : 484990061 : if (FNDECL_USED_AUTO (fn))
20456 : 4627 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
20457 : :
20458 : 484985434 : return TREE_TYPE (TREE_TYPE (fn));
20459 : : }
20460 : :
20461 : : /* Returns true iff DECL is a variable or function declared with an auto type
20462 : : that has not yet been deduced to a real type. */
20463 : :
20464 : : bool
20465 : 765701534 : undeduced_auto_decl (tree decl)
20466 : : {
20467 : 765701534 : if (cxx_dialect < cxx11)
20468 : : return false;
20469 : 763602907 : STRIP_ANY_LOCATION_WRAPPER (decl);
20470 : 325037409 : return ((VAR_OR_FUNCTION_DECL_P (decl)
20471 : 74721904 : || TREE_CODE (decl) == TEMPLATE_DECL)
20472 : 1014052451 : && type_uses_auto (TREE_TYPE (decl)));
20473 : : }
20474 : :
20475 : : /* Complain if DECL has an undeduced return type. */
20476 : :
20477 : : bool
20478 : 244762399 : require_deduced_type (tree decl, tsubst_flags_t complain)
20479 : : {
20480 : 244762399 : if (undeduced_auto_decl (decl))
20481 : : {
20482 : 2995 : if (TREE_CODE (decl) == FUNCTION_DECL
20483 : 2968 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
20484 : 5379 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
20485 : : {
20486 : : /* Set the return type of __builtin_source_location. */
20487 : 2384 : tree type = get_source_location_impl_type ();
20488 : 2384 : if (type == error_mark_node)
20489 : : {
20490 : 33 : inform (input_location, "using %qs", "__builtin_source_location");
20491 : 33 : return false;
20492 : : }
20493 : 2351 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
20494 : 2351 : type = build_pointer_type (type);
20495 : 2351 : apply_deduced_return_type (decl, type);
20496 : 2351 : return true;
20497 : : }
20498 : :
20499 : 611 : if (warning_suppressed_p (decl) && seen_error ())
20500 : : /* We probably already complained about deduction failure. */;
20501 : 477 : else if (complain & tf_error)
20502 : 72 : error ("use of %qD before deduction of %<auto%>", decl);
20503 : 611 : note_failed_type_completion (decl, complain);
20504 : 611 : return false;
20505 : : }
20506 : : return true;
20507 : : }
20508 : :
20509 : : /* Create a representation of the explicit-specifier with
20510 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
20511 : :
20512 : : tree
20513 : 6325300 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
20514 : : {
20515 : 6325300 : if (check_for_bare_parameter_packs (expr))
20516 : 3 : return error_mark_node;
20517 : :
20518 : 6325297 : if (instantiation_dependent_expression_p (expr))
20519 : : /* Wait for instantiation, tsubst_function_decl will handle it. */
20520 : : return expr;
20521 : :
20522 : 5717991 : expr = build_converted_constant_bool_expr (expr, complain);
20523 : 5717991 : expr = instantiate_non_dependent_expr (expr, complain);
20524 : 5717991 : expr = cxx_constant_value (expr, complain);
20525 : 5717991 : return expr;
20526 : : }
20527 : :
20528 : : #include "gt-cp-decl.h"
|