Branch data Line data Source code
1 : : /* Process declarations and variables for -*- C++ -*- compiler.
2 : : Copyright (C) 1988-2024 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 : : #define INCLUDE_MEMORY
31 : : #include "system.h"
32 : : #include "coretypes.h"
33 : : #include "target.h"
34 : : #include "c-family/c-target.h"
35 : : #include "cp-tree.h"
36 : : #include "timevar.h"
37 : : #include "stringpool.h"
38 : : #include "cgraph.h"
39 : : #include "stor-layout.h"
40 : : #include "varasm.h"
41 : : #include "attribs.h"
42 : : #include "flags.h"
43 : : #include "tree-iterator.h"
44 : : #include "decl.h"
45 : : #include "intl.h"
46 : : #include "toplev.h"
47 : : #include "c-family/c-objc.h"
48 : : #include "c-family/c-pragma.h"
49 : : #include "c-family/c-ubsan.h"
50 : : #include "cp/cp-name-hint.h"
51 : : #include "debug.h"
52 : : #include "plugin.h"
53 : : #include "builtins.h"
54 : : #include "gimplify.h"
55 : : #include "asan.h"
56 : : #include "gcc-rich-location.h"
57 : : #include "langhooks.h"
58 : : #include "context.h" /* For 'g'. */
59 : : #include "omp-general.h"
60 : : #include "omp-offload.h" /* For offload_vars. */
61 : : #include "opts.h"
62 : : #include "langhooks-def.h" /* For lhd_simulate_record_decl */
63 : : #include "coroutines.h"
64 : :
65 : : /* Possible cases of bad specifiers type used by bad_specifiers. */
66 : : enum bad_spec_place {
67 : : BSP_VAR, /* variable */
68 : : BSP_PARM, /* parameter */
69 : : BSP_TYPE, /* type */
70 : : BSP_FIELD /* field */
71 : : };
72 : :
73 : : static const char *redeclaration_error_message (tree, tree);
74 : :
75 : : static bool decl_jump_unsafe (tree);
76 : : static void require_complete_types_for_parms (tree);
77 : : static tree grok_reference_init (tree, tree, tree, int);
78 : : static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
79 : : int, int, int, bool, int, tree, location_t);
80 : : static void check_static_variable_definition (tree, tree);
81 : : static void record_unknown_type (tree, const char *);
82 : : static int member_function_or_else (tree, tree, enum overload_flags);
83 : : static tree local_variable_p_walkfn (tree *, int *, void *);
84 : : static const char *tag_name (enum tag_types);
85 : : static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
86 : : static void maybe_deduce_size_from_array_init (tree, tree);
87 : : static void layout_var_decl (tree);
88 : : static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
89 : : static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
90 : : static void copy_type_enum (tree , tree);
91 : : static void check_function_type (tree, tree);
92 : : static void begin_destructor_body (void);
93 : : static void record_key_method_defined (tree);
94 : : static tree create_array_type_for_decl (tree, tree, tree, location_t);
95 : : static tree get_atexit_node (void);
96 : : static tree get_dso_handle_node (void);
97 : : static tree start_cleanup_fn (bool);
98 : : static void end_cleanup_fn (void);
99 : : static tree cp_make_fname_decl (location_t, tree, int);
100 : : static void initialize_predefined_identifiers (void);
101 : : static tree check_special_function_return_type
102 : : (special_function_kind, tree, tree, int, const location_t*);
103 : : static tree push_cp_library_fn (enum tree_code, tree, int);
104 : : static tree build_cp_library_fn (tree, enum tree_code, tree, int);
105 : : static void store_parm_decls (tree);
106 : : static void initialize_local_var (tree, tree, bool);
107 : : static void expand_static_init (tree, tree);
108 : : static location_t smallest_type_location (const cp_decl_specifier_seq*);
109 : : static bool identify_goto (tree, location_t, const location_t *,
110 : : diagnostic_t, bool);
111 : :
112 : : /* The following symbols are subsumed in the cp_global_trees array, and
113 : : listed here individually for documentation purposes.
114 : :
115 : : C++ extensions
116 : : tree wchar_decl_node;
117 : :
118 : : tree vtable_entry_type;
119 : : tree delta_type_node;
120 : : tree __t_desc_type_node;
121 : :
122 : : tree class_type_node;
123 : : tree unknown_type_node;
124 : :
125 : : Array type `vtable_entry_type[]'
126 : :
127 : : tree vtbl_type_node;
128 : : tree vtbl_ptr_type_node;
129 : :
130 : : Namespaces,
131 : :
132 : : tree std_node;
133 : : tree abi_node;
134 : :
135 : : A FUNCTION_DECL which can call `abort'. Not necessarily the
136 : : one that the user will declare, but sufficient to be called
137 : : by routines that want to abort the program.
138 : :
139 : : tree abort_fndecl;
140 : :
141 : : Used by RTTI
142 : : tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
143 : : tree tinfo_var_id; */
144 : :
145 : : tree cp_global_trees[CPTI_MAX];
146 : :
147 : : /* A list of objects which have constructors or destructors
148 : : which reside in namespace scope. The decl is stored in
149 : : the TREE_VALUE slot and the initializer is stored
150 : : in the TREE_PURPOSE slot. */
151 : : tree static_aggregates;
152 : :
153 : : /* Like static_aggregates, but for thread_local variables. */
154 : : tree tls_aggregates;
155 : :
156 : : /* A hash-map mapping from variable decls to the dynamic initializer for
157 : : the decl. This is currently only used by OpenMP. */
158 : : decl_tree_map *dynamic_initializers;
159 : :
160 : : /* -- end of C++ */
161 : :
162 : : /* A node for the integer constant 2. */
163 : :
164 : : tree integer_two_node;
165 : :
166 : : /* vector of static decls. */
167 : : vec<tree, va_gc> *static_decls;
168 : :
169 : : /* vector of keyed classes. */
170 : : vec<tree, va_gc> *keyed_classes;
171 : :
172 : : /* Used only for jumps to as-yet undefined labels, since jumps to
173 : : defined labels can have their validity checked immediately. */
174 : :
175 : : struct GTY((chain_next ("%h.next"))) named_label_use_entry {
176 : : struct named_label_use_entry *next;
177 : : /* The binding level to which this entry is *currently* attached.
178 : : This is initially the binding level in which the goto appeared,
179 : : but is modified as scopes are closed. */
180 : : cp_binding_level *binding_level;
181 : : /* The head of the names list that was current when the goto appeared,
182 : : or the inner scope popped. These are the decls that will *not* be
183 : : skipped when jumping to the label. */
184 : : tree names_in_scope;
185 : : /* If the use is a possible destination of a computed goto, a vec of decls
186 : : that aren't destroyed, filled in by poplevel_named_label_1. */
187 : : vec<tree,va_gc> *computed_goto;
188 : : /* The location of the goto, for error reporting. */
189 : : location_t o_goto_locus;
190 : : /* True if an OpenMP structured block scope has been closed since
191 : : the goto appeared. This means that the branch from the label will
192 : : illegally exit an OpenMP scope. */
193 : : bool in_omp_scope;
194 : : };
195 : :
196 : : /* A list of all LABEL_DECLs in the function that have names. Here so
197 : : we can clear out their names' definitions at the end of the
198 : : function, and so we can check the validity of jumps to these labels. */
199 : :
200 : : struct GTY((for_user)) named_label_entry {
201 : :
202 : : tree name; /* Name of decl. */
203 : :
204 : : tree label_decl; /* LABEL_DECL, unless deleted local label. */
205 : :
206 : : named_label_entry *outer; /* Outer shadowed chain. */
207 : :
208 : : /* The binding level to which the label is *currently* attached.
209 : : This is initially set to the binding level in which the label
210 : : is defined, but is modified as scopes are closed. */
211 : : cp_binding_level *binding_level;
212 : :
213 : : /* The head of the names list that was current when the label was
214 : : defined, or the inner scope popped. These are the decls that will
215 : : be skipped when jumping to the label. */
216 : : tree names_in_scope;
217 : :
218 : : /* A vector of all decls from all binding levels that would be
219 : : crossed by a backward branch to the label. */
220 : : vec<tree, va_gc> *bad_decls;
221 : :
222 : : /* A list of uses of the label, before the label is defined. */
223 : : named_label_use_entry *uses;
224 : :
225 : : /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for
226 : : this, it has a more specific meaning for LABEL_DECL. */
227 : : bool addressed;
228 : :
229 : : /* The following bits are set after the label is defined, and are
230 : : updated as scopes are popped. They indicate that a jump to the
231 : : label will illegally enter a scope of the given flavor. */
232 : : bool in_try_scope;
233 : : bool in_catch_scope;
234 : : bool in_omp_scope;
235 : : bool in_transaction_scope;
236 : : bool in_constexpr_if;
237 : : bool in_consteval_if;
238 : : bool in_stmt_expr;
239 : : };
240 : :
241 : : #define named_labels cp_function_chain->x_named_labels
242 : :
243 : : /* The number of function bodies which we are currently processing.
244 : : (Zero if we are at namespace scope, one inside the body of a
245 : : function, two inside the body of a function in a local class, etc.) */
246 : : int function_depth;
247 : :
248 : : /* Whether the exception-specifier is part of a function type (i.e. C++17). */
249 : : bool flag_noexcept_type;
250 : :
251 : : /* States indicating how grokdeclarator() should handle declspecs marked
252 : : with __attribute__((deprecated)). An object declared as
253 : : __attribute__((deprecated)) suppresses warnings of uses of other
254 : : deprecated items. */
255 : : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
256 : :
257 : :
258 : : /* A list of VAR_DECLs whose type was incomplete at the time the
259 : : variable was declared. */
260 : :
261 : : struct GTY(()) incomplete_var {
262 : : tree decl;
263 : : tree incomplete_type;
264 : : };
265 : :
266 : :
267 : : static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
268 : :
269 : : /* Returns the kind of template specialization we are currently
270 : : processing, given that it's declaration contained N_CLASS_SCOPES
271 : : explicit scope qualifications. */
272 : :
273 : : tmpl_spec_kind
274 : 212207455 : current_tmpl_spec_kind (int n_class_scopes)
275 : : {
276 : 212207455 : int n_template_parm_scopes = 0;
277 : 212207455 : int seen_specialization_p = 0;
278 : 212207455 : int innermost_specialization_p = 0;
279 : 212207455 : cp_binding_level *b;
280 : :
281 : : /* Scan through the template parameter scopes. */
282 : 212207455 : for (b = current_binding_level;
283 : 266513521 : b->kind == sk_template_parms;
284 : 54306066 : b = b->level_chain)
285 : : {
286 : : /* If we see a specialization scope inside a parameter scope,
287 : : then something is wrong. That corresponds to a declaration
288 : : like:
289 : :
290 : : template <class T> template <> ...
291 : :
292 : : which is always invalid since [temp.expl.spec] forbids the
293 : : specialization of a class member template if the enclosing
294 : : class templates are not explicitly specialized as well. */
295 : 54306066 : if (b->explicit_spec_p)
296 : : {
297 : 871221 : if (n_template_parm_scopes == 0)
298 : : innermost_specialization_p = 1;
299 : : else
300 : 1332 : seen_specialization_p = 1;
301 : : }
302 : 53434845 : else if (seen_specialization_p == 1)
303 : : return tsk_invalid_member_spec;
304 : :
305 : 54306066 : ++n_template_parm_scopes;
306 : : }
307 : :
308 : : /* Handle explicit instantiations. */
309 : 212207455 : if (processing_explicit_instantiation)
310 : : {
311 : 1983767 : if (n_template_parm_scopes != 0)
312 : : /* We've seen a template parameter list during an explicit
313 : : instantiation. For example:
314 : :
315 : : template <class T> template void f(int);
316 : :
317 : : This is erroneous. */
318 : : return tsk_invalid_expl_inst;
319 : : else
320 : 1983767 : return tsk_expl_inst;
321 : : }
322 : :
323 : 210223688 : if (n_template_parm_scopes < n_class_scopes)
324 : : /* We've not seen enough template headers to match all the
325 : : specialized classes present. For example:
326 : :
327 : : template <class T> void R<T>::S<T>::f(int);
328 : :
329 : : This is invalid; there needs to be one set of template
330 : : parameters for each class. */
331 : : return tsk_insufficient_parms;
332 : 210223688 : else if (n_template_parm_scopes == n_class_scopes)
333 : : /* We're processing a non-template declaration (even though it may
334 : : be a member of a template class.) For example:
335 : :
336 : : template <class T> void S<T>::f(int);
337 : :
338 : : The `class T' matches the `S<T>', leaving no template headers
339 : : corresponding to the `f'. */
340 : : return tsk_none;
341 : 46278463 : else if (n_template_parm_scopes > n_class_scopes + 1)
342 : : /* We've got too many template headers. For example:
343 : :
344 : : template <> template <class T> void f (T);
345 : :
346 : : There need to be more enclosing classes. */
347 : : return tsk_excessive_parms;
348 : : else
349 : : /* This must be a template. It's of the form:
350 : :
351 : : template <class T> template <class U> void S<T>::f(U);
352 : :
353 : : This is a specialization if the innermost level was a
354 : : specialization; otherwise it's just a definition of the
355 : : template. */
356 : 92179529 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
357 : : }
358 : :
359 : : /* Exit the current scope. */
360 : :
361 : : void
362 : 82579958 : finish_scope (void)
363 : : {
364 : 82579958 : poplevel (0, 0, 0);
365 : 82579958 : }
366 : :
367 : : /* When a label goes out of scope, check to see if that label was used
368 : : in a valid manner, and issue any appropriate warnings or errors. */
369 : :
370 : : static void
371 : 16654 : check_label_used (tree label)
372 : : {
373 : 16654 : if (!processing_template_decl)
374 : : {
375 : 16407 : if (DECL_INITIAL (label) == NULL_TREE)
376 : : {
377 : 9 : location_t location;
378 : :
379 : 9 : error ("label %q+D used but not defined", label);
380 : 9 : location = input_location;
381 : : /* FIXME want (LOCATION_FILE (input_location), (line)0) */
382 : : /* Avoid crashing later. */
383 : 9 : define_label (location, DECL_NAME (label));
384 : : }
385 : : else
386 : 16398 : warn_for_unused_label (label);
387 : : }
388 : 16654 : }
389 : :
390 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
391 : :
392 : : static int
393 : 183103 : sort_labels (const void *a, const void *b)
394 : : {
395 : 183103 : tree label1 = *(tree const *) a;
396 : 183103 : tree label2 = *(tree const *) b;
397 : :
398 : : /* DECL_UIDs can never be equal. */
399 : 183103 : return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
400 : : }
401 : :
402 : : /* At the end of a function, all labels declared within the function
403 : : go out of scope. BLOCK is the top-level block for the
404 : : function. */
405 : :
406 : : static void
407 : 132608323 : pop_labels (tree block)
408 : : {
409 : 132608323 : if (!named_labels)
410 : 132604549 : return;
411 : :
412 : : /* We need to add the labels to the block chain, so debug
413 : : information is emitted. But, we want the order to be stable so
414 : : need to sort them first. Otherwise the debug output could be
415 : : randomly ordered. I guess it's mostly stable, unless the hash
416 : : table implementation changes. */
417 : 3774 : auto_vec<tree, 32> labels (named_labels->elements ());
418 : 3774 : hash_table<named_label_hash>::iterator end (named_labels->end ());
419 : 20407 : for (hash_table<named_label_hash>::iterator iter
420 : 40814 : (named_labels->begin ()); iter != end; ++iter)
421 : : {
422 : 16633 : named_label_entry *ent = *iter;
423 : :
424 : 16633 : gcc_checking_assert (!ent->outer);
425 : 16633 : if (ent->label_decl)
426 : 16459 : labels.quick_push (ent->label_decl);
427 : 16633 : ggc_free (ent);
428 : : }
429 : 3774 : named_labels = NULL;
430 : 3774 : labels.qsort (sort_labels);
431 : :
432 : 20233 : while (labels.length ())
433 : : {
434 : 16459 : tree label = labels.pop ();
435 : :
436 : 16459 : DECL_CHAIN (label) = BLOCK_VARS (block);
437 : 16459 : BLOCK_VARS (block) = label;
438 : :
439 : 16459 : check_label_used (label);
440 : : }
441 : 3774 : }
442 : :
443 : : /* At the end of a block with local labels, restore the outer definition. */
444 : :
445 : : static void
446 : 195 : pop_local_label (tree id, tree label)
447 : : {
448 : 195 : check_label_used (label);
449 : 195 : named_label_entry **slot = named_labels->find_slot_with_hash
450 : 195 : (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
451 : 195 : named_label_entry *ent = *slot;
452 : :
453 : 195 : if (ent->outer)
454 : : ent = ent->outer;
455 : : else
456 : : {
457 : 186 : ent = ggc_cleared_alloc<named_label_entry> ();
458 : 186 : ent->name = id;
459 : : }
460 : 195 : *slot = ent;
461 : 195 : }
462 : :
463 : : /* The following two routines are used to interface to Objective-C++.
464 : : The binding level is purposely treated as an opaque type. */
465 : :
466 : : void *
467 : 0 : objc_get_current_scope (void)
468 : : {
469 : 0 : return current_binding_level;
470 : : }
471 : :
472 : : /* The following routine is used by the NeXT-style SJLJ exceptions;
473 : : variables get marked 'volatile' so as to not be clobbered by
474 : : _setjmp()/_longjmp() calls. All variables in the current scope,
475 : : as well as parent scopes up to (but not including) ENCLOSING_BLK
476 : : shall be thusly marked. */
477 : :
478 : : void
479 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
480 : : {
481 : 0 : cp_binding_level *scope;
482 : :
483 : 0 : for (scope = current_binding_level;
484 : 0 : scope && scope != enclosing_blk;
485 : 0 : scope = scope->level_chain)
486 : : {
487 : 0 : tree decl;
488 : :
489 : 0 : for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
490 : 0 : objc_volatilize_decl (decl);
491 : :
492 : : /* Do not climb up past the current function. */
493 : 0 : if (scope->kind == sk_function_parms)
494 : : break;
495 : : }
496 : 0 : }
497 : :
498 : : /* True if B is the level for the condition of a constexpr if. */
499 : :
500 : : static bool
501 : 1644264 : level_for_constexpr_if (cp_binding_level *b)
502 : : {
503 : 1641692 : return (b->kind == sk_cond && b->this_entity
504 : 261 : && TREE_CODE (b->this_entity) == IF_STMT
505 : 1644525 : && IF_STMT_CONSTEXPR_P (b->this_entity));
506 : : }
507 : :
508 : : /* True if B is the level for the condition of a consteval if. */
509 : :
510 : : static bool
511 : 1644240 : level_for_consteval_if (cp_binding_level *b)
512 : : {
513 : 1641668 : return (b->kind == sk_cond && b->this_entity
514 : 237 : && TREE_CODE (b->this_entity) == IF_STMT
515 : 1644477 : && IF_STMT_CONSTEVAL_P (b->this_entity));
516 : : }
517 : :
518 : : /* True if T is a non-static VAR_DECL that has a non-trivial destructor.
519 : : See [stmt.dcl]/2. */
520 : :
521 : : static bool
522 : 25 : automatic_var_with_nontrivial_dtor_p (const_tree t)
523 : : {
524 : 25 : if (error_operand_p (t))
525 : : return false;
526 : :
527 : 19 : return (VAR_P (t)
528 : 19 : && decl_storage_duration (CONST_CAST_TREE (t)) == dk_auto
529 : 38 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)));
530 : : }
531 : :
532 : : /* Update data for defined and undefined labels when leaving a scope. */
533 : :
534 : : int
535 : 86735 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
536 : : {
537 : 86735 : named_label_entry *ent = *slot;
538 : 86735 : cp_binding_level *obl = bl->level_chain;
539 : :
540 : 86735 : if (ent->binding_level == bl)
541 : : {
542 : 6116 : tree decl;
543 : :
544 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
545 : : TREE_LISTs representing OVERLOADs, so be careful. */
546 : 8985 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
547 : 2942 : ? DECL_CHAIN (decl)
548 : 73 : : TREE_CHAIN (decl)))
549 : 2869 : if (decl_jump_unsafe (decl))
550 : 982 : vec_safe_push (ent->bad_decls, decl);
551 : :
552 : 6116 : ent->binding_level = obl;
553 : 6116 : ent->names_in_scope = obl->names;
554 : 6116 : switch (bl->kind)
555 : : {
556 : 42 : case sk_try:
557 : 42 : ent->in_try_scope = true;
558 : 42 : break;
559 : 27 : case sk_catch:
560 : 27 : ent->in_catch_scope = true;
561 : 27 : break;
562 : 245 : case sk_omp:
563 : 245 : ent->in_omp_scope = true;
564 : 245 : break;
565 : 3 : case sk_transaction:
566 : 3 : ent->in_transaction_scope = true;
567 : 3 : break;
568 : 33 : case sk_stmt_expr:
569 : 33 : ent->in_stmt_expr = true;
570 : 33 : break;
571 : 2764 : case sk_block:
572 : 2764 : if (level_for_constexpr_if (bl->level_chain))
573 : 15 : ent->in_constexpr_if = true;
574 : 2749 : else if (level_for_consteval_if (bl->level_chain))
575 : 30 : ent->in_consteval_if = true;
576 : : break;
577 : : default:
578 : : break;
579 : : }
580 : : }
581 : 80619 : else if (ent->uses)
582 : : {
583 : : struct named_label_use_entry *use;
584 : :
585 : 7101 : for (use = ent->uses; use ; use = use->next)
586 : 3967 : if (use->binding_level == bl)
587 : : {
588 : 1668 : if (auto &cg = use->computed_goto)
589 : : {
590 : 45 : if (bl->kind == sk_catch)
591 : 3 : vec_safe_push (cg, get_identifier ("catch"));
592 : 54 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
593 : 9 : if (automatic_var_with_nontrivial_dtor_p (d))
594 : 6 : vec_safe_push (cg, d);
595 : : }
596 : :
597 : 1668 : use->binding_level = obl;
598 : 1668 : use->names_in_scope = obl->names;
599 : 1668 : if (bl->kind == sk_omp)
600 : 12 : use->in_omp_scope = true;
601 : : }
602 : : }
603 : :
604 : 86735 : return 1;
605 : : }
606 : :
607 : : /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
608 : : when errors were reported, except for -Werror-unused-but-set-*. */
609 : : static int unused_but_set_errorcount;
610 : :
611 : : /* Exit a binding level.
612 : : Pop the level off, and restore the state of the identifier-decl mappings
613 : : that were in effect when this level was entered.
614 : :
615 : : If KEEP == 1, this level had explicit declarations, so
616 : : and create a "block" (a BLOCK node) for the level
617 : : to record its declarations and subblocks for symbol table output.
618 : :
619 : : If FUNCTIONBODY is nonzero, this level is the body of a function,
620 : : so create a block as if KEEP were set and also clear out all
621 : : label names.
622 : :
623 : : If REVERSE is nonzero, reverse the order of decls before putting
624 : : them into the BLOCK. */
625 : :
626 : : tree
627 : 536963632 : poplevel (int keep, int reverse, int functionbody)
628 : : {
629 : 536963632 : tree link;
630 : : /* The chain of decls was accumulated in reverse order.
631 : : Put it into forward order, just for cleanliness. */
632 : 536963632 : tree decls;
633 : 536963632 : tree subblocks;
634 : 536963632 : tree block;
635 : 536963632 : tree decl;
636 : 536963632 : scope_kind kind;
637 : :
638 : 536963632 : auto_cond_timevar tv (TV_NAME_LOOKUP);
639 : 536964082 : restart:
640 : :
641 : 536964082 : block = NULL_TREE;
642 : :
643 : 536964082 : gcc_assert (current_binding_level->kind != sk_class
644 : : && current_binding_level->kind != sk_namespace);
645 : :
646 : 536964082 : if (current_binding_level->kind == sk_cleanup)
647 : : functionbody = 0;
648 : 536964082 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
649 : :
650 : 536964082 : gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
651 : :
652 : : /* We used to use KEEP == 2 to indicate that the new block should go
653 : : at the beginning of the list of blocks at this binding level,
654 : : rather than the end. This hack is no longer used. */
655 : 536964082 : gcc_assert (keep == 0 || keep == 1);
656 : :
657 : 536964082 : if (current_binding_level->keep)
658 : 6789800 : keep = 1;
659 : :
660 : : /* Any uses of undefined labels, and any defined labels, now operate
661 : : under constraints of next binding contour. */
662 : 536964082 : if (cfun && !functionbody && named_labels)
663 : 34968 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
664 : 34968 : (current_binding_level);
665 : :
666 : : /* Get the decls in the order they were written.
667 : : Usually current_binding_level->names is in reverse order.
668 : : But parameter decls were previously put in forward order. */
669 : :
670 : 536964082 : decls = current_binding_level->names;
671 : 536964082 : if (reverse)
672 : : {
673 : 287682933 : decls = nreverse (decls);
674 : 287682933 : current_binding_level->names = decls;
675 : : }
676 : :
677 : : /* If there were any declarations or structure tags in that level,
678 : : or if this level is a function body,
679 : : create a BLOCK to record them for the life of this function. */
680 : 536964082 : block = NULL_TREE;
681 : : /* Avoid function body block if possible. */
682 : 569272393 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
683 : : keep = 0;
684 : 504656026 : else if (keep == 1 || functionbody)
685 : 164574821 : block = make_node (BLOCK);
686 : 164574821 : if (block != NULL_TREE)
687 : : {
688 : 164574821 : BLOCK_VARS (block) = decls;
689 : 164574821 : BLOCK_SUBBLOCKS (block) = subblocks;
690 : : }
691 : :
692 : : /* In each subblock, record that this is its superior. */
693 : 536964082 : if (keep >= 0)
694 : 601239360 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
695 : 64275278 : BLOCK_SUPERCONTEXT (link) = block;
696 : :
697 : : /* Before we remove the declarations first check for unused variables. */
698 : 536964082 : if ((warn_unused_variable || warn_unused_but_set_variable)
699 : 6138890 : && current_binding_level->kind != sk_template_parms
700 : 5217249 : && !processing_template_decl)
701 : 6289395 : for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
702 : : {
703 : : /* There are cases where D itself is a TREE_LIST. See in
704 : : push_local_binding where the list of decls returned by
705 : : getdecls is built. */
706 : 3245223 : tree udecl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
707 : 3245223 : decl = strip_using_decl (udecl);
708 : :
709 : 3245223 : tree type = TREE_TYPE (decl);
710 : 3245223 : if (VAR_P (decl)
711 : 364870 : && (!TREE_USED (decl) || !DECL_READ_P (decl))
712 : 2766 : && !DECL_IN_SYSTEM_HEADER (udecl)
713 : : /* For structured bindings, consider only real variables, not
714 : : subobjects. */
715 : 822 : && (DECL_DECOMPOSITION_P (decl) ? DECL_DECOMP_IS_BASE (decl)
716 : 686 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
717 : : /* Don't warn about name-independent declarations. */
718 : 471 : && !name_independent_decl_p (decl)
719 : 381 : && type != error_mark_node
720 : 3246397 : && (!CLASS_TYPE_P (type)
721 : 51 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
722 : 3 : || lookup_attribute ("warn_unused",
723 : 3 : TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
724 : : {
725 : 352 : if (!TREE_USED (decl))
726 : : {
727 : 190 : if (TREE_CODE (udecl) == USING_DECL)
728 : 3 : warning_at (DECL_SOURCE_LOCATION (udecl),
729 : : OPT_Wunused_variable,
730 : : "unused using-declaration %qD", udecl);
731 : 187 : else if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
732 : 3 : warning_at (DECL_SOURCE_LOCATION (decl),
733 : : OPT_Wunused_variable,
734 : : "unused structured binding declaration");
735 : : else
736 : 184 : warning_at (DECL_SOURCE_LOCATION (decl),
737 : : OPT_Wunused_variable, "unused variable %qD", decl);
738 : 190 : suppress_warning (decl, OPT_Wunused_variable);
739 : : }
740 : 162 : else if (DECL_CONTEXT (decl) == current_function_decl
741 : : // For -Wunused-but-set-variable leave references alone.
742 : 138 : && !TYPE_REF_P (TREE_TYPE (decl))
743 : 300 : && errorcount == unused_but_set_errorcount)
744 : : {
745 : 129 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
746 : 12 : warning_at (DECL_SOURCE_LOCATION (decl),
747 : : OPT_Wunused_but_set_variable, "structured "
748 : : "binding declaration set but not used");
749 : : else
750 : 117 : warning_at (DECL_SOURCE_LOCATION (decl),
751 : : OPT_Wunused_but_set_variable,
752 : : "variable %qD set but not used", decl);
753 : 129 : unused_but_set_errorcount = errorcount;
754 : : }
755 : : }
756 : : }
757 : :
758 : : /* Remove declarations for all the DECLs in this level. */
759 : 1097770759 : for (link = decls; link; link = TREE_CHAIN (link))
760 : : {
761 : 560806677 : tree name;
762 : 560806677 : if (TREE_CODE (link) == TREE_LIST)
763 : : {
764 : 670106 : decl = TREE_VALUE (link);
765 : 670106 : name = TREE_PURPOSE (link);
766 : 670106 : gcc_checking_assert (name);
767 : : }
768 : : else
769 : : {
770 : 560136571 : decl = link;
771 : 560136571 : name = DECL_NAME (decl);
772 : : }
773 : :
774 : : /* Remove the binding. */
775 : 560806677 : if (TREE_CODE (decl) == LABEL_DECL)
776 : 195 : pop_local_label (name, decl);
777 : : else
778 : 560806482 : pop_local_binding (name, decl);
779 : : }
780 : :
781 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
782 : 536964082 : for (link = current_binding_level->type_shadowed;
783 : 734391698 : link; link = TREE_CHAIN (link))
784 : 197427616 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
785 : :
786 : : /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
787 : : list if a `using' declaration put them there. The debugging
788 : : back ends won't understand OVERLOAD, so we remove them here.
789 : : Because the BLOCK_VARS are (temporarily) shared with
790 : : CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
791 : : popped all the bindings. Also remove undeduced 'auto' decls,
792 : : which LTO doesn't understand, and can't have been used by anything. */
793 : 536964082 : if (block)
794 : : {
795 : 164574821 : tree* d;
796 : :
797 : 433355290 : for (d = &BLOCK_VARS (block); *d; )
798 : : {
799 : 268780469 : if (TREE_CODE (*d) == TREE_LIST
800 : 268780469 : || (!processing_template_decl
801 : 85962891 : && undeduced_auto_decl (*d)))
802 : 670134 : *d = TREE_CHAIN (*d);
803 : : else
804 : 268110335 : d = &DECL_CHAIN (*d);
805 : : }
806 : : }
807 : :
808 : : /* If the level being exited is the top level of a function,
809 : : check over all the labels. */
810 : 536964082 : if (functionbody)
811 : : {
812 : 132608323 : if (block)
813 : : {
814 : : /* Since this is the top level block of a function, the vars are
815 : : the function's parameters. Don't leave them in the BLOCK
816 : : because they are found in the FUNCTION_DECL instead. */
817 : 100300267 : BLOCK_VARS (block) = 0;
818 : 100300267 : pop_labels (block);
819 : : }
820 : : else
821 : 32308056 : pop_labels (subblocks);
822 : : }
823 : :
824 : 536964082 : kind = current_binding_level->kind;
825 : 536964082 : if (kind == sk_cleanup)
826 : : {
827 : 450 : tree stmt;
828 : :
829 : : /* If this is a temporary binding created for a cleanup, then we'll
830 : : have pushed a statement list level. Pop that, create a new
831 : : BIND_EXPR for the block, and insert it into the stream. */
832 : 450 : stmt = pop_stmt_list (current_binding_level->statement_list);
833 : 450 : stmt = c_build_bind_expr (input_location, block, stmt);
834 : 450 : add_stmt (stmt);
835 : : }
836 : :
837 : 536964082 : leave_scope ();
838 : 536964082 : if (functionbody)
839 : : {
840 : : /* The current function is being defined, so its DECL_INITIAL
841 : : should be error_mark_node. */
842 : 132608323 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
843 : 164916379 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
844 : 132608323 : if (subblocks)
845 : : {
846 : 65996229 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
847 : : {
848 : 7663149 : if (BLOCK_SUBBLOCKS (subblocks))
849 : 970214 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
850 : : }
851 : : else
852 : 24645162 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
853 : : }
854 : : }
855 : 404355759 : else if (block)
856 : 64274554 : current_binding_level->blocks
857 : 128549108 : = block_chainon (current_binding_level->blocks, block);
858 : :
859 : : /* If we did not make a block for the level just exited,
860 : : any blocks made for inner levels
861 : : (since they cannot be recorded as subblocks in that level)
862 : : must be carried forward so they will later become subblocks
863 : : of something else. */
864 : 340081205 : else if (subblocks)
865 : 9 : current_binding_level->blocks
866 : 18 : = block_chainon (current_binding_level->blocks, subblocks);
867 : :
868 : : /* Each and every BLOCK node created here in `poplevel' is important
869 : : (e.g. for proper debugging information) so if we created one
870 : : earlier, mark it as "used". */
871 : 536964082 : if (block)
872 : 164574821 : TREE_USED (block) = 1;
873 : :
874 : : /* All temporary bindings created for cleanups are popped silently. */
875 : 536964082 : if (kind == sk_cleanup)
876 : 450 : goto restart;
877 : :
878 : 1073927264 : return block;
879 : 536963632 : }
880 : :
881 : : /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
882 : : /* Diagnose odr-used extern inline variables without definitions
883 : : in the current TU. */
884 : :
885 : : int
886 : 135112 : wrapup_namespace_globals ()
887 : : {
888 : 135112 : if (vec<tree, va_gc> *statics = static_decls)
889 : : {
890 : 13668319 : for (tree decl : *statics)
891 : : {
892 : 13557104 : if (warn_unused_function
893 : 998346 : && TREE_CODE (decl) == FUNCTION_DECL
894 : 756667 : && DECL_INITIAL (decl) == 0
895 : 2485 : && DECL_EXTERNAL (decl)
896 : 2485 : && !TREE_PUBLIC (decl)
897 : 19 : && !DECL_ARTIFICIAL (decl)
898 : 19 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
899 : 13557110 : && !warning_suppressed_p (decl, OPT_Wunused_function))
900 : 6 : warning_at (DECL_SOURCE_LOCATION (decl),
901 : : OPT_Wunused_function,
902 : : "%qF declared %<static%> but never defined", decl);
903 : :
904 : 13557104 : if (VAR_P (decl)
905 : 4049183 : && DECL_EXTERNAL (decl)
906 : 1967202 : && DECL_INLINE_VAR_P (decl)
907 : 13557122 : && DECL_ODR_USED (decl))
908 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
909 : : "odr-used inline variable %qD is not defined", decl);
910 : : }
911 : :
912 : : /* Clear out the list, so we don't rescan next time. */
913 : 111215 : static_decls = NULL;
914 : :
915 : : /* Write out any globals that need to be output. */
916 : 111215 : return wrapup_global_declarations (statics->address (),
917 : 111215 : statics->length ());
918 : : }
919 : : return 0;
920 : : }
921 : :
922 : : /* In C++, you don't have to write `struct S' to refer to `S'; you
923 : : can just use `S'. We accomplish this by creating a TYPE_DECL as
924 : : if the user had written `typedef struct S S'. Create and return
925 : : the TYPE_DECL for TYPE. */
926 : :
927 : : tree
928 : 141750463 : create_implicit_typedef (tree name, tree type)
929 : : {
930 : 141750463 : tree decl;
931 : :
932 : 141750463 : decl = build_decl (input_location, TYPE_DECL, name, type);
933 : 141750463 : DECL_ARTIFICIAL (decl) = 1;
934 : : /* There are other implicit type declarations, like the one *within*
935 : : a class that allows you to write `S::S'. We must distinguish
936 : : amongst these. */
937 : 141750463 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
938 : 141750463 : TYPE_NAME (type) = decl;
939 : 141750463 : TYPE_STUB_DECL (type) = decl;
940 : :
941 : 141750463 : return decl;
942 : : }
943 : :
944 : : /* Function-scope local entities that need discriminators. Each entry
945 : : is a {decl,name} pair. VAR_DECLs for anon unions get their name
946 : : smashed, so we cannot rely on DECL_NAME. */
947 : :
948 : : static GTY((deletable)) vec<tree, va_gc> *local_entities;
949 : :
950 : : /* Determine the mangling discriminator of local DECL. There are
951 : : generally very few of these in any particular function. */
952 : :
953 : : void
954 : 664692 : determine_local_discriminator (tree decl, tree name)
955 : : {
956 : 664692 : auto_cond_timevar tv (TV_NAME_LOOKUP);
957 : 664692 : retrofit_lang_decl (decl);
958 : 664692 : tree ctx = DECL_CONTEXT (decl);
959 : 664692 : size_t nelts = vec_safe_length (local_entities);
960 : 664692 : if (name == NULL_TREE)
961 : 664618 : name = (TREE_CODE (decl) == TYPE_DECL
962 : 1094713 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
963 : 1208457 : ? NULL_TREE : DECL_NAME (decl));
964 : 18798072 : for (size_t i = 0; i < nelts; i += 2)
965 : : {
966 : 18133735 : tree *pair = &(*local_entities)[i];
967 : 18133735 : tree d = pair[0];
968 : 18133735 : tree n = pair[1];
969 : 18133735 : gcc_checking_assert (d != decl);
970 : 18133735 : if (name == n
971 : 3412030 : && TREE_CODE (decl) == TREE_CODE (d)
972 : 21545756 : && ctx == DECL_CONTEXT (d))
973 : : {
974 : 355 : tree disc = integer_one_node;
975 : 355 : if (DECL_DISCRIMINATOR (d))
976 : 179 : disc = build_int_cst (TREE_TYPE (disc),
977 : 179 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
978 : 355 : DECL_DISCRIMINATOR (decl) = disc;
979 : : /* Replace the saved decl. */
980 : 355 : pair[0] = decl;
981 : 355 : decl = NULL_TREE;
982 : 355 : break;
983 : : }
984 : : }
985 : :
986 : 664692 : if (decl)
987 : : {
988 : 664337 : vec_safe_reserve (local_entities, 2);
989 : 664337 : local_entities->quick_push (decl);
990 : 664337 : local_entities->quick_push (name);
991 : : }
992 : 664692 : }
993 : :
994 : :
995 : : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
996 : :
997 : : A non-template friend declaration with a requires-clause shall be a
998 : : definition. A friend function template with a constraint that depends on a
999 : : template parameter from an enclosing template shall be a definition. Such a
1000 : : constrained friend function or function template declaration does not
1001 : : declare the same function or function template as a declaration in any other
1002 : : scope.
1003 : :
1004 : : The ABI calls this a "member-like constrained friend" and mangles it like a
1005 : : member function to avoid collisions. */
1006 : :
1007 : : bool
1008 : 1314042424 : member_like_constrained_friend_p (tree decl)
1009 : : {
1010 : 1314042424 : return (TREE_CODE (decl) == FUNCTION_DECL
1011 : 407355335 : && DECL_UNIQUE_FRIEND_P (decl)
1012 : 70350148 : && DECL_FRIEND_CONTEXT (decl)
1013 : 31340202 : && get_constraints (decl)
1014 : 1314191443 : && (!DECL_TEMPLATE_INFO (decl)
1015 : 149019 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
1016 : 738 : || (uses_outer_template_parms_in_constraints
1017 : 738 : (most_general_template (decl)))));
1018 : : }
1019 : :
1020 : : /* Returns true if functions FN1 and FN2 have equivalent trailing
1021 : : requires clauses. */
1022 : :
1023 : : static bool
1024 : 11235009 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1025 : : {
1026 : : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1027 : : same function as a declaration in any other scope." So no need to
1028 : : actually compare the requirements. */
1029 : 11235009 : if (member_like_constrained_friend_p (newfn)
1030 : 11235009 : || member_like_constrained_friend_p (oldfn))
1031 : 107 : return false;
1032 : :
1033 : : /* Compare only trailing requirements. */
1034 : 11234902 : tree reqs1 = get_trailing_function_requirements (newfn);
1035 : 11234902 : tree reqs2 = get_trailing_function_requirements (oldfn);
1036 : 11234902 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1037 : : return false;
1038 : :
1039 : : /* Substitution is needed when friends are involved. */
1040 : 11198917 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1041 : 11198917 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1042 : :
1043 : 11198917 : return cp_tree_equal (reqs1, reqs2);
1044 : : }
1045 : :
1046 : : /* Two functions of the same name correspond [basic.scope.scope] if
1047 : :
1048 : : + both declare functions with the same non-object-parameter-type-list,
1049 : : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1050 : : specified in [temp.friend]), and, if both are non-static members, they have
1051 : : corresponding object parameters, or
1052 : :
1053 : : + both declare function templates with equivalent
1054 : : non-object-parameter-type-lists, return types (if any), template-heads, and
1055 : : trailing requires-clauses (if any), and, if both are non-static members,
1056 : : they have corresponding object parameters.
1057 : :
1058 : : This is a subset of decls_match: it identifies declarations that cannot be
1059 : : overloaded with one another. This function does not consider DECL_NAME. */
1060 : :
1061 : : bool
1062 : 200821715 : fns_correspond (tree newdecl, tree olddecl)
1063 : : {
1064 : 200821715 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1065 : : return false;
1066 : :
1067 : 186524393 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1068 : : {
1069 : 186508245 : if (!template_heads_equivalent_p (newdecl, olddecl))
1070 : : return 0;
1071 : 41892257 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1072 : 41892257 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1073 : : }
1074 : :
1075 : 41908405 : tree f1 = TREE_TYPE (newdecl);
1076 : 41908405 : tree f2 = TREE_TYPE (olddecl);
1077 : :
1078 : 41908405 : int rq1 = type_memfn_rqual (f1);
1079 : 41908405 : int rq2 = type_memfn_rqual (f2);
1080 : :
1081 : : /* If only one is a non-static member function, ignore ref-quals. */
1082 : 41908405 : if (TREE_CODE (f1) != TREE_CODE (f2))
1083 : : rq1 = rq2;
1084 : : /* Two non-static member functions have corresponding object parameters if:
1085 : : + exactly one is an implicit object member function with no ref-qualifier
1086 : : and the types of their object parameters ([dcl.fct]), after removing
1087 : : top-level references, are the same, or
1088 : : + their object parameters have the same type. */
1089 : : /* ??? We treat member functions of different classes as corresponding even
1090 : : though that means the object parameters have different types. */
1091 : 41908405 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1092 : 0 : rq1 = rq2;
1093 : :
1094 : 41908405 : bool types_match = rq1 == rq2;
1095 : :
1096 : 41908405 : if (types_match)
1097 : : {
1098 : 41908405 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1099 : 41908405 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1100 : 41908405 : types_match = compparms (p1, p2);
1101 : : }
1102 : :
1103 : : /* Two function declarations match if either has a requires-clause
1104 : : then both have a requires-clause and their constraints-expressions
1105 : : are equivalent. */
1106 : 41908405 : if (types_match && flag_concepts)
1107 : 5742542 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1108 : :
1109 : : return types_match;
1110 : : }
1111 : :
1112 : : /* Subroutine of duplicate_decls: return truthvalue of whether
1113 : : or not types of these decls match.
1114 : :
1115 : : For C++, we must compare the parameter list so that `int' can match
1116 : : `int&' in a parameter position, but `int&' is not confused with
1117 : : `const int&'. */
1118 : :
1119 : : int
1120 : 879816661 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1121 : : {
1122 : 879816661 : int types_match;
1123 : :
1124 : 879816661 : if (newdecl == olddecl)
1125 : : return 1;
1126 : :
1127 : 879760045 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1128 : : /* If the two DECLs are not even the same kind of thing, we're not
1129 : : interested in their types. */
1130 : : return 0;
1131 : :
1132 : 435007442 : gcc_assert (DECL_P (newdecl));
1133 : :
1134 : 435007442 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1135 : : {
1136 : : /* Specializations of different templates are different functions
1137 : : even if they have the same type. */
1138 : 240654172 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1139 : 240654172 : ? DECL_TI_TEMPLATE (newdecl)
1140 : 240654172 : : NULL_TREE);
1141 : 240654172 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1142 : 240654172 : ? DECL_TI_TEMPLATE (olddecl)
1143 : 240654172 : : NULL_TREE);
1144 : 240654172 : if (t1 != t2)
1145 : : return 0;
1146 : :
1147 : 238162162 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1148 : 241179880 : && ! (DECL_EXTERN_C_P (newdecl)
1149 : 3017718 : && DECL_EXTERN_C_P (olddecl)))
1150 : : return 0;
1151 : :
1152 : : /* A new declaration doesn't match a built-in one unless it
1153 : : is also extern "C". */
1154 : 220559294 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1155 : 220559294 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1156 : : return 0;
1157 : :
1158 : 219899060 : tree f1 = TREE_TYPE (newdecl);
1159 : 219899060 : tree f2 = TREE_TYPE (olddecl);
1160 : 219899060 : if (TREE_CODE (f1) != TREE_CODE (f2))
1161 : : return 0;
1162 : :
1163 : : /* A declaration with deduced return type should use its pre-deduction
1164 : : type for declaration matching. */
1165 : 219747679 : tree r2 = fndecl_declared_return_type (olddecl);
1166 : 219747679 : tree r1 = fndecl_declared_return_type (newdecl);
1167 : :
1168 : : /* For memchr et al, allow const void* return type (as specified by C++)
1169 : : when we expect void* (as in C). */
1170 : 219747679 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1171 : 6066727 : && DECL_EXTERN_C_P (olddecl)
1172 : 3781461 : && !same_type_p (r1, r2)
1173 : 61 : && TREE_CODE (r1) == POINTER_TYPE
1174 : 6 : && TREE_CODE (r2) == POINTER_TYPE
1175 : 219747682 : && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2)))
1176 : : r2 = r1;
1177 : :
1178 : 219747679 : tree p1 = TYPE_ARG_TYPES (f1);
1179 : 219747679 : tree p2 = TYPE_ARG_TYPES (f2);
1180 : :
1181 : 219747679 : if (same_type_p (r1, r2))
1182 : : {
1183 : 161604144 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1184 : 161604114 : && fndecl_built_in_p (olddecl))
1185 : : {
1186 : 52 : types_match = self_promoting_args_p (p1);
1187 : 52 : if (p1 == void_list_node)
1188 : 3 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1189 : : }
1190 : : else
1191 : 161604001 : types_match =
1192 : 161604001 : compparms (p1, p2)
1193 : 25381955 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1194 : 186985723 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1195 : 1196895 : || comp_type_attributes (TREE_TYPE (newdecl),
1196 : 1196895 : TREE_TYPE (olddecl)) != 0);
1197 : : }
1198 : : else
1199 : : types_match = 0;
1200 : :
1201 : : /* Two function declarations match if either has a requires-clause
1202 : : then both have a requires-clause and their constraints-expressions
1203 : : are equivalent. */
1204 : 25381749 : if (types_match && flag_concepts)
1205 : 5492443 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1206 : :
1207 : : /* The decls dont match if they correspond to two different versions
1208 : : of the same function. Disallow extern "C" functions to be
1209 : : versions for now. */
1210 : 25381749 : if (types_match
1211 : 25332577 : && !DECL_EXTERN_C_P (newdecl)
1212 : 19627202 : && !DECL_EXTERN_C_P (olddecl)
1213 : 45004965 : && targetm.target_option.function_versions (newdecl, olddecl))
1214 : : {
1215 : 8394 : if (record_versions)
1216 : 16638 : maybe_version_functions (newdecl, olddecl,
1217 : 8319 : (!DECL_FUNCTION_VERSIONED (newdecl)
1218 : 15831 : || !DECL_FUNCTION_VERSIONED (olddecl)));
1219 : 8394 : return 0;
1220 : : }
1221 : : }
1222 : 194353270 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1223 : : {
1224 : 193004402 : if (!template_heads_equivalent_p (newdecl, olddecl))
1225 : : return 0;
1226 : :
1227 : 61910874 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1228 : 61910874 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1229 : :
1230 : 61910874 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1231 : : return 0;
1232 : :
1233 : : /* Two template types match if they are the same. Otherwise, compare
1234 : : the underlying declarations. */
1235 : 61910868 : if (TREE_CODE (newres) == TYPE_DECL)
1236 : 30 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1237 : : else
1238 : 61910838 : types_match = decls_match (newres, oldres);
1239 : : }
1240 : : else
1241 : : {
1242 : : /* Need to check scope for variable declaration (VAR_DECL).
1243 : : For typedef (TYPE_DECL), scope is ignored. */
1244 : 1348868 : if (VAR_P (newdecl)
1245 : 912727 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1246 : : /* [dcl.link]
1247 : : Two declarations for an object with C language linkage
1248 : : with the same name (ignoring the namespace that qualify
1249 : : it) that appear in different namespace scopes refer to
1250 : : the same object. */
1251 : 1507403 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1252 : : return 0;
1253 : :
1254 : 1190381 : if (TREE_TYPE (newdecl) == error_mark_node)
1255 : 3 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1256 : 1190378 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1257 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1258 : 1190378 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1259 : : types_match = 0;
1260 : : else
1261 : 1190378 : types_match = comptypes (TREE_TYPE (newdecl),
1262 : 1190378 : TREE_TYPE (olddecl),
1263 : : COMPARE_REDECLARATION);
1264 : : }
1265 : :
1266 : : return types_match;
1267 : : }
1268 : :
1269 : : /* Mark DECL as versioned if it isn't already. */
1270 : :
1271 : : static void
1272 : 16728 : maybe_mark_function_versioned (tree decl)
1273 : : {
1274 : 16728 : if (!DECL_FUNCTION_VERSIONED (decl))
1275 : : {
1276 : 951 : DECL_FUNCTION_VERSIONED (decl) = 1;
1277 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1278 : : to include the version marker. */
1279 : 951 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1280 : 33 : mangle_decl (decl);
1281 : : }
1282 : 16728 : }
1283 : :
1284 : : /* NEWDECL and OLDDECL have identical signatures. If they are
1285 : : different versions adjust them and return true.
1286 : : If RECORD is set to true, record function versions. */
1287 : :
1288 : : bool
1289 : 138394 : maybe_version_functions (tree newdecl, tree olddecl, bool record)
1290 : : {
1291 : 138394 : if (!targetm.target_option.function_versions (newdecl, olddecl))
1292 : : return false;
1293 : :
1294 : 8346 : maybe_mark_function_versioned (olddecl);
1295 : 8346 : if (DECL_LOCAL_DECL_P (olddecl))
1296 : : {
1297 : 18 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1298 : 18 : maybe_mark_function_versioned (olddecl);
1299 : : }
1300 : :
1301 : 8346 : maybe_mark_function_versioned (newdecl);
1302 : 8346 : if (DECL_LOCAL_DECL_P (newdecl))
1303 : : {
1304 : : /* Unfortunately, we can get here before pushdecl naturally calls
1305 : : push_local_extern_decl_alias, so we need to call it directly. */
1306 : 18 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1307 : 6 : push_local_extern_decl_alias (newdecl);
1308 : 18 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1309 : 18 : maybe_mark_function_versioned (newdecl);
1310 : : }
1311 : :
1312 : 8346 : if (record)
1313 : 834 : cgraph_node::record_function_versions (olddecl, newdecl);
1314 : :
1315 : : return true;
1316 : : }
1317 : :
1318 : : /* If NEWDECL is `static' and an `extern' was seen previously,
1319 : : warn about it. OLDDECL is the previous declaration.
1320 : :
1321 : : Note that this does not apply to the C++ case of declaring
1322 : : a variable `extern const' and then later `const'.
1323 : :
1324 : : Don't complain about built-in functions, since they are beyond
1325 : : the user's control. */
1326 : :
1327 : : void
1328 : 17437135 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1329 : : {
1330 : 17437135 : if (TREE_CODE (newdecl) == TYPE_DECL
1331 : 17415829 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1332 : 14027720 : || TREE_CODE (newdecl) == CONST_DECL
1333 : 14027720 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1334 : 17437123 : return;
1335 : :
1336 : : /* Don't get confused by static member functions; that's a different
1337 : : use of `static'. */
1338 : 14027720 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1339 : 14027720 : && DECL_STATIC_FUNCTION_P (newdecl))
1340 : : return;
1341 : :
1342 : : /* If the old declaration was `static', or the new one isn't, then
1343 : : everything is OK. */
1344 : 13880873 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1345 : : return;
1346 : :
1347 : : /* It's OK to declare a builtin function as `static'. */
1348 : 12 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1349 : 12 : && DECL_ARTIFICIAL (olddecl))
1350 : : return;
1351 : :
1352 : 12 : auto_diagnostic_group d;
1353 : 12 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1354 : : "%qD was declared %<extern%> and later %<static%>", newdecl))
1355 : 12 : inform (DECL_SOURCE_LOCATION (olddecl),
1356 : : "previous declaration of %qD", olddecl);
1357 : 12 : }
1358 : :
1359 : : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1360 : : function templates. If their exception specifications do not
1361 : : match, issue a diagnostic. */
1362 : :
1363 : : static void
1364 : 17039080 : check_redeclaration_exception_specification (tree new_decl,
1365 : : tree old_decl)
1366 : : {
1367 : 17039080 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1368 : 17039080 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1369 : :
1370 : : /* Two default specs are equivalent, don't force evaluation. */
1371 : 5867210 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1372 : 17186054 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1373 : : return;
1374 : :
1375 : 17029084 : if (!type_dependent_expression_p (old_decl))
1376 : : {
1377 : 9701715 : maybe_instantiate_noexcept (new_decl);
1378 : 9701715 : maybe_instantiate_noexcept (old_decl);
1379 : : }
1380 : 17029084 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1381 : 17029084 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1382 : :
1383 : : /* [except.spec]
1384 : :
1385 : : If any declaration of a function has an exception-specification,
1386 : : all declarations, including the definition and an explicit
1387 : : specialization, of that function shall have an
1388 : : exception-specification with the same set of type-ids. */
1389 : 17029084 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1390 : 13374892 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1391 : 30403834 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1392 : : {
1393 : 328 : const char *const msg
1394 : : = G_("declaration of %qF has a different exception specifier");
1395 : 328 : bool complained = true;
1396 : 328 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1397 : 328 : auto_diagnostic_group d;
1398 : 328 : if (DECL_IN_SYSTEM_HEADER (old_decl))
1399 : 239 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1400 : 89 : else if (!flag_exceptions)
1401 : : /* We used to silently permit mismatched eh specs with
1402 : : -fno-exceptions, so make them a pedwarn now. */
1403 : 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1404 : : else
1405 : 86 : error_at (new_loc, msg, new_decl);
1406 : 328 : if (complained)
1407 : 92 : inform (DECL_SOURCE_LOCATION (old_decl),
1408 : : "from previous declaration %qF", old_decl);
1409 : 328 : }
1410 : : }
1411 : :
1412 : : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1413 : : Otherwise issue diagnostics. */
1414 : :
1415 : : bool
1416 : 17437616 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1417 : : {
1418 : 17437616 : old_decl = STRIP_TEMPLATE (old_decl);
1419 : 17437616 : new_decl = STRIP_TEMPLATE (new_decl);
1420 : 17437616 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1421 : 17416286 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1422 : : return true;
1423 : 17416286 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1424 : 17416286 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1425 : : {
1426 : 17382497 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1427 : : return true;
1428 : 34078948 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1429 : 17039474 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1430 : : return true;
1431 : : }
1432 : 33795 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1433 : : {
1434 : : /* With -fimplicit-constexpr, ignore changes in the constexpr
1435 : : keyword. */
1436 : 61 : if (flag_implicit_constexpr
1437 : 61 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1438 : 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1439 : : return true;
1440 : 61 : if (fndecl_built_in_p (old_decl))
1441 : : {
1442 : : /* Hide a built-in declaration. */
1443 : 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1444 : 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1445 : 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1446 : 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1447 : 0 : return true;
1448 : : }
1449 : : /* 7.1.5 [dcl.constexpr]
1450 : : Note: An explicit specialization can differ from the template
1451 : : declaration with respect to the constexpr specifier. */
1452 : 61 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1453 : 61 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1454 : : return true;
1455 : :
1456 : 42 : const char *kind = "constexpr";
1457 : 42 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1458 : 72 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1459 : : kind = "consteval";
1460 : 42 : auto_diagnostic_group d;
1461 : 42 : error_at (DECL_SOURCE_LOCATION (new_decl),
1462 : : "redeclaration %qD differs in %qs "
1463 : : "from previous declaration", new_decl,
1464 : : kind);
1465 : 42 : inform (DECL_SOURCE_LOCATION (old_decl),
1466 : : "previous declaration %qD", old_decl);
1467 : 42 : return false;
1468 : 42 : }
1469 : : return true;
1470 : : }
1471 : :
1472 : : /* DECL is a redeclaration of a function or function template. If
1473 : : it does have default arguments issue a diagnostic. Note: this
1474 : : function is used to enforce the requirements in C++11 8.3.6 about
1475 : : no default arguments in redeclarations. */
1476 : :
1477 : : static void
1478 : 10790799 : check_redeclaration_no_default_args (tree decl)
1479 : : {
1480 : 10790799 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1481 : :
1482 : 10790799 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1483 : 31893940 : t && t != void_list_node; t = TREE_CHAIN (t))
1484 : 21103195 : if (TREE_PURPOSE (t))
1485 : : {
1486 : 54 : permerror (DECL_SOURCE_LOCATION (decl),
1487 : : "redeclaration of %q#D may not have default "
1488 : : "arguments", decl);
1489 : 54 : return;
1490 : : }
1491 : : }
1492 : :
1493 : : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1494 : : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1495 : : the TEMPLATE_DECLs in case of function templates). This function is used
1496 : : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1497 : : "If a friend declaration specifies a default argument expression, that
1498 : : declaration shall be a definition and shall be the only declaration of
1499 : : the function or function template in the translation unit." */
1500 : :
1501 : : static void
1502 : 5372923 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1503 : : {
1504 : 10397879 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1505 : : return;
1506 : :
1507 : 1827250 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1508 : 913625 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1509 : 2441791 : t1 && t1 != void_list_node;
1510 : 1528166 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1511 : 2181412 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1512 : 4152892 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1513 : : {
1514 : 21 : auto_diagnostic_group d;
1515 : 21 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1516 : : "friend declaration of %q#D specifies default "
1517 : : "arguments and isn%'t the only declaration", newdecl))
1518 : 21 : inform (DECL_SOURCE_LOCATION (olddecl),
1519 : : "previous declaration of %q#D", olddecl);
1520 : 21 : return;
1521 : 21 : }
1522 : : }
1523 : :
1524 : : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1525 : : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1526 : :
1527 : : static void
1528 : 16475320 : merge_attribute_bits (tree newdecl, tree olddecl)
1529 : : {
1530 : 16475320 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1531 : 16475320 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1532 : 16475320 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1533 : 16475320 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1534 : 16475320 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1535 : 16475320 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1536 : 16475320 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1537 : 16475320 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1538 : 16475320 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1539 : 16475320 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1540 : 16475320 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1541 : 16475320 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1542 : 16475320 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1543 : 16475320 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1544 : 16475320 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1545 : 16475320 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1546 : 16475320 : }
1547 : :
1548 : : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1549 : : && lookup_attribute ("gnu_inline", \
1550 : : DECL_ATTRIBUTES (fn)))
1551 : :
1552 : : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1553 : : ambiguates olddecl. Returns true if an error occurs. */
1554 : :
1555 : : static bool
1556 : 189616221 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1557 : : {
1558 : 189616221 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1559 : 189616221 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1560 : : /* Function template declarations can be differentiated by parameter
1561 : : and return type. */
1562 : 189616221 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1563 : 189616221 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1564 : 189616221 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1565 : : TREE_TYPE (TREE_TYPE (olddecl))))
1566 : : {
1567 : : /* ... and also by their template-heads and requires-clauses. */
1568 : 2952 : if (template_heads_equivalent_p (newdecl, olddecl)
1569 : 2952 : && function_requirements_equivalent_p (newres, oldres))
1570 : : {
1571 : 0 : auto_diagnostic_group d;
1572 : 0 : error ("ambiguating new declaration %q+#D", newdecl);
1573 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1574 : : "old declaration %q#D", olddecl);
1575 : 0 : return true;
1576 : 0 : }
1577 : :
1578 : : /* FIXME: The types are the same but the are differences
1579 : : in either the template heads or function requirements.
1580 : : We should be able to diagnose a set of common errors
1581 : : stemming from these declarations. For example:
1582 : :
1583 : : template<typename T> requires C void f(...);
1584 : : template<typename T> void f(...) requires C;
1585 : :
1586 : : These are functionally equivalent but not equivalent. */
1587 : : }
1588 : :
1589 : : return false;
1590 : : }
1591 : :
1592 : : /* OLD_PARMS is the innermost set of template parameters for some template
1593 : : declaration, and NEW_PARMS is the corresponding set of template parameters
1594 : : for a redeclaration of that template. Merge the default arguments within
1595 : : these two sets of parameters. CLASS_P is true iff the template in
1596 : : question is a class template. */
1597 : :
1598 : : bool
1599 : 7530153 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1600 : : {
1601 : 7530153 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1602 : : == TREE_VEC_LENGTH (old_parms));
1603 : 21297802 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1604 : : {
1605 : 13767661 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1606 : 13767661 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1607 : 13767661 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1608 : 13767661 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1609 : 13767661 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1610 : : return false;
1611 : 13767658 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1612 : : {
1613 : 9 : auto_diagnostic_group d;
1614 : 9 : error ("redefinition of default argument for %q+#D", new_parm);
1615 : 9 : inform (DECL_SOURCE_LOCATION (old_parm),
1616 : : "original definition appeared here");
1617 : 9 : return false;
1618 : 9 : }
1619 : 13767649 : else if (new_default != NULL_TREE)
1620 : : /* Update the previous template parameters (which are the ones
1621 : : that will really count) with the new default value. */
1622 : 153233 : old_default = new_default;
1623 : 13614416 : else if (class_p && old_default != NULL_TREE)
1624 : : /* Update the new parameters, too; they'll be used as the
1625 : : parameters for any members. */
1626 : 939297 : new_default = old_default;
1627 : : }
1628 : : return true;
1629 : : }
1630 : :
1631 : : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1632 : : If the redeclaration is invalid, a diagnostic is issued, and the
1633 : : error_mark_node is returned. Otherwise, OLDDECL is returned.
1634 : :
1635 : : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1636 : : returned.
1637 : :
1638 : : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1639 : : if the old decl was hidden.
1640 : :
1641 : : Hidden decls can be anticipated builtins, injected friends, or
1642 : : (coming soon) injected from a local-extern decl. */
1643 : :
1644 : : tree
1645 : 320105258 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1646 : : {
1647 : 320105258 : unsigned olddecl_uid = DECL_UID (olddecl);
1648 : 320105258 : int types_match = 0;
1649 : 320105258 : int new_defines_function = 0;
1650 : 320105258 : tree new_template_info;
1651 : 320105258 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1652 : 320105258 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1653 : :
1654 : 320105258 : if (newdecl == olddecl)
1655 : : return olddecl;
1656 : :
1657 : 319265210 : types_match = decls_match (newdecl, olddecl);
1658 : :
1659 : : /* If either the type of the new decl or the type of the old decl is an
1660 : : error_mark_node, then that implies that we have already issued an
1661 : : error (earlier) for some bogus type specification, and in that case,
1662 : : it is rather pointless to harass the user with yet more error message
1663 : : about the same declaration, so just pretend the types match here. */
1664 : 319265210 : if (TREE_TYPE (newdecl) == error_mark_node
1665 : 319265210 : || TREE_TYPE (olddecl) == error_mark_node)
1666 : : return error_mark_node;
1667 : :
1668 : : /* Check for redeclaration and other discrepancies. */
1669 : 319265184 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1670 : 319265184 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1671 : : {
1672 : 5918103 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1673 : : {
1674 : : /* Avoid warnings redeclaring built-ins which have not been
1675 : : explicitly declared. */
1676 : 184189 : if (was_hidden)
1677 : : {
1678 : 184161 : if (TREE_PUBLIC (newdecl)
1679 : 184161 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1680 : 12 : warning_at (newdecl_loc,
1681 : : OPT_Wbuiltin_declaration_mismatch,
1682 : : "built-in function %qD declared as non-function",
1683 : : newdecl);
1684 : 184161 : return NULL_TREE;
1685 : : }
1686 : :
1687 : : /* If you declare a built-in or predefined function name as static,
1688 : : the old definition is overridden, but optionally warn this was a
1689 : : bad choice of name. */
1690 : 28 : if (! TREE_PUBLIC (newdecl))
1691 : : {
1692 : 28 : warning_at (newdecl_loc,
1693 : : OPT_Wshadow,
1694 : 28 : fndecl_built_in_p (olddecl)
1695 : : ? G_("shadowing built-in function %q#D")
1696 : : : G_("shadowing library function %q#D"), olddecl);
1697 : : /* Discard the old built-in function. */
1698 : 28 : return NULL_TREE;
1699 : : }
1700 : : /* If the built-in is not ansi, then programs can override
1701 : : it even globally without an error. */
1702 : 0 : else if (! fndecl_built_in_p (olddecl))
1703 : 0 : warning_at (newdecl_loc, 0,
1704 : : "library function %q#D redeclared as non-function %q#D",
1705 : : olddecl, newdecl);
1706 : : else
1707 : 0 : error_at (newdecl_loc,
1708 : : "declaration of %q#D conflicts with built-in "
1709 : : "declaration %q#D", newdecl, olddecl);
1710 : 0 : return NULL_TREE;
1711 : : }
1712 : 5733914 : else if (!types_match)
1713 : : {
1714 : : /* Avoid warnings redeclaring built-ins which have not been
1715 : : explicitly declared. */
1716 : 2079722 : if (was_hidden)
1717 : : {
1718 : 799949 : tree t1, t2;
1719 : :
1720 : : /* A new declaration doesn't match a built-in one unless it
1721 : : is also extern "C". */
1722 : 799949 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1723 : 799949 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1724 : 799949 : if (!DECL_EXTERN_C_P (newdecl))
1725 : : return NULL_TREE;
1726 : :
1727 : 139715 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1728 : 139715 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1729 : 240590 : t1 || t2;
1730 : 100875 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1731 : : {
1732 : 240572 : if (!t1 || !t2)
1733 : : break;
1734 : : /* FILE, tm types are not known at the time
1735 : : we create the builtins. */
1736 : 747699 : for (unsigned i = 0;
1737 : 988109 : i < sizeof (builtin_structptr_types)
1738 : : / sizeof (builtin_structptr_type);
1739 : : ++i)
1740 : 887228 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1741 : : {
1742 : 139529 : tree t = TREE_VALUE (t1);
1743 : :
1744 : 139529 : if (TYPE_PTR_P (t)
1745 : 279058 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1746 : 139529 : == get_identifier (builtin_structptr_types[i].str)
1747 : 279043 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1748 : : {
1749 : 139514 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1750 : :
1751 : 139514 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1752 : 139514 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1753 : 139514 : types_match = decls_match (newdecl, olddecl);
1754 : 139514 : if (types_match)
1755 : 139514 : return duplicate_decls (newdecl, olddecl,
1756 : 139514 : hiding, was_hidden);
1757 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1758 : : }
1759 : 15 : goto next_arg;
1760 : : }
1761 : :
1762 : 100881 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1763 : : break;
1764 : 100875 : next_arg:;
1765 : : }
1766 : :
1767 : 201 : warning_at (newdecl_loc,
1768 : : OPT_Wbuiltin_declaration_mismatch,
1769 : : "declaration of %q#D conflicts with built-in "
1770 : : "declaration %q#D", newdecl, olddecl);
1771 : : }
1772 : 1279773 : else if ((DECL_EXTERN_C_P (newdecl)
1773 : 15 : && DECL_EXTERN_C_P (olddecl))
1774 : 2559531 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1775 : 1279758 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1776 : : {
1777 : : /* Don't really override olddecl for __* prefixed builtins
1778 : : except for __[^b]*_chk, the compiler might be using those
1779 : : explicitly. */
1780 : 23 : if (fndecl_built_in_p (olddecl))
1781 : : {
1782 : 15 : tree id = DECL_NAME (olddecl);
1783 : 15 : const char *name = IDENTIFIER_POINTER (id);
1784 : 15 : size_t len;
1785 : :
1786 : 15 : if (name[0] == '_'
1787 : 15 : && name[1] == '_'
1788 : 30 : && (startswith (name + 2, "builtin_")
1789 : 6 : || (len = strlen (name)) <= strlen ("___chk")
1790 : 6 : || memcmp (name + len - strlen ("_chk"),
1791 : : "_chk", strlen ("_chk") + 1) != 0))
1792 : : {
1793 : 15 : if (DECL_INITIAL (newdecl))
1794 : : {
1795 : 6 : error_at (newdecl_loc,
1796 : : "definition of %q#D ambiguates built-in "
1797 : : "declaration %q#D", newdecl, olddecl);
1798 : 6 : return error_mark_node;
1799 : : }
1800 : 9 : auto_diagnostic_group d;
1801 : 9 : if (permerror (newdecl_loc,
1802 : : "new declaration %q#D ambiguates built-in"
1803 : : " declaration %q#D", newdecl, olddecl)
1804 : 9 : && flag_permissive)
1805 : 3 : inform (newdecl_loc,
1806 : : "ignoring the %q#D declaration", newdecl);
1807 : 9 : return flag_permissive ? olddecl : error_mark_node;
1808 : 9 : }
1809 : : }
1810 : :
1811 : : /* A near match; override the builtin. */
1812 : :
1813 : 8 : if (TREE_PUBLIC (newdecl))
1814 : 8 : warning_at (newdecl_loc,
1815 : : OPT_Wbuiltin_declaration_mismatch,
1816 : : "new declaration %q#D ambiguates built-in "
1817 : : "declaration %q#D", newdecl, olddecl);
1818 : : else
1819 : 0 : warning (OPT_Wshadow,
1820 : : fndecl_built_in_p (olddecl)
1821 : : ? G_("shadowing built-in function %q#D")
1822 : : : G_("shadowing library function %q#D"), olddecl);
1823 : : }
1824 : : else
1825 : : /* Discard the old built-in function. */
1826 : : return NULL_TREE;
1827 : :
1828 : : /* Replace the old RTL to avoid problems with inlining. */
1829 : 209 : COPY_DECL_RTL (newdecl, olddecl);
1830 : : }
1831 : : else
1832 : : {
1833 : : /* Even if the types match, prefer the new declarations type
1834 : : for built-ins which have not been explicitly declared,
1835 : : for exception lists, etc... */
1836 : 3654192 : tree type = TREE_TYPE (newdecl);
1837 : 3654192 : tree attribs = (*targetm.merge_type_attributes)
1838 : 3654192 : (TREE_TYPE (olddecl), type);
1839 : :
1840 : 3654192 : type = cp_build_type_attribute_variant (type, attribs);
1841 : 3654192 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1842 : : }
1843 : :
1844 : : /* If a function is explicitly declared "throw ()", propagate that to
1845 : : the corresponding builtin. */
1846 : 3654401 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1847 : 3502156 : && was_hidden
1848 : 3502113 : && TREE_NOTHROW (newdecl)
1849 : 6902819 : && !TREE_NOTHROW (olddecl))
1850 : : {
1851 : 27940 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1852 : 27940 : tree tmpdecl = builtin_decl_explicit (fncode);
1853 : 27940 : if (tmpdecl && tmpdecl != olddecl && types_match)
1854 : 27940 : TREE_NOTHROW (tmpdecl) = 1;
1855 : : }
1856 : :
1857 : : /* Whether or not the builtin can throw exceptions has no
1858 : : bearing on this declarator. */
1859 : 3654401 : TREE_NOTHROW (olddecl) = 0;
1860 : :
1861 : 3654401 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1862 : : {
1863 : : /* If a builtin function is redeclared as `static', merge
1864 : : the declarations, but make the original one static. */
1865 : 3 : DECL_THIS_STATIC (olddecl) = 1;
1866 : 3 : TREE_PUBLIC (olddecl) = 0;
1867 : :
1868 : : /* Make the old declaration consistent with the new one so
1869 : : that all remnants of the builtin-ness of this function
1870 : : will be banished. */
1871 : 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1872 : 3 : COPY_DECL_RTL (newdecl, olddecl);
1873 : : }
1874 : : }
1875 : 313347081 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1876 : : {
1877 : : /* C++ Standard, 3.3, clause 4:
1878 : : "[Note: a namespace name or a class template name must be unique
1879 : : in its declarative region (7.3.2, clause 14). ]" */
1880 : 77900108 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
1881 : 77900087 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1882 : : /* Namespace conflicts with not namespace. */;
1883 : 45383537 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
1884 : 123283612 : || DECL_TYPE_TEMPLATE_P (newdecl))
1885 : : /* Class template conflicts. */;
1886 : 77900069 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1887 : 45383537 : && DECL_TEMPLATE_RESULT (olddecl)
1888 : 45383537 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1889 : 123283600 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
1890 : 32475426 : && DECL_TEMPLATE_RESULT (newdecl)
1891 : 32475426 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1892 : : /* Variable template conflicts. */;
1893 : 77900060 : else if (concept_definition_p (olddecl)
1894 : 155800114 : || concept_definition_p (newdecl))
1895 : : /* Concept conflicts. */;
1896 : 77900048 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1897 : 45422233 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
1898 : 77938756 : || (TREE_CODE (olddecl) == FUNCTION_DECL
1899 : 32475433 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1900 : : {
1901 : : /* One is a function and the other is a template
1902 : : function. */
1903 : 77858927 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1904 : : return NULL_TREE;
1905 : :
1906 : : /* There can only be one! */
1907 : 57943 : auto_diagnostic_group d;
1908 : 57943 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
1909 : 57943 : && check_raw_literal_operator (olddecl))
1910 : 3 : error_at (newdecl_loc,
1911 : : "literal operator %q#D conflicts with"
1912 : : " raw literal operator", newdecl);
1913 : 57940 : else if (check_raw_literal_operator (newdecl))
1914 : 3 : error_at (newdecl_loc,
1915 : : "raw literal operator %q#D conflicts with"
1916 : : " literal operator template", newdecl);
1917 : : else
1918 : : return NULL_TREE;
1919 : :
1920 : 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1921 : 6 : return error_mark_node;
1922 : 57943 : }
1923 : 41121 : else if (DECL_DECOMPOSITION_P (olddecl) || DECL_DECOMPOSITION_P (newdecl))
1924 : : /* A structured binding must be unique in its declarative region. */;
1925 : 38846 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1926 : 41118 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1927 : : /* One is an implicit typedef, that's ok. */
1928 : : return NULL_TREE;
1929 : :
1930 : 147 : auto_diagnostic_group d;
1931 : 147 : error ("%q#D redeclared as different kind of entity", newdecl);
1932 : 147 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1933 : :
1934 : 147 : return error_mark_node;
1935 : 147 : }
1936 : 235446973 : else if (!types_match)
1937 : : {
1938 : 221634150 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1939 : : /* These are certainly not duplicate declarations; they're
1940 : : from different scopes. */
1941 : : return NULL_TREE;
1942 : :
1943 : 221634096 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1944 : : {
1945 : 189616239 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1946 : 189616239 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1947 : :
1948 : : /* The name of a class template may not be declared to refer to
1949 : : any other template, class, function, object, namespace, value,
1950 : : or type in the same scope. */
1951 : 189616239 : if (TREE_CODE (oldres) == TYPE_DECL
1952 : 189616233 : || TREE_CODE (newres) == TYPE_DECL)
1953 : : {
1954 : 9 : auto_diagnostic_group d;
1955 : 9 : error_at (newdecl_loc,
1956 : : "conflicting declaration of template %q#D", newdecl);
1957 : 9 : inform (olddecl_loc,
1958 : : "previous declaration %q#D", olddecl);
1959 : 9 : return error_mark_node;
1960 : 9 : }
1961 : :
1962 : 189616230 : else if (TREE_CODE (oldres) == FUNCTION_DECL
1963 : 189616221 : && TREE_CODE (newres) == FUNCTION_DECL)
1964 : : {
1965 : 189616221 : if (duplicate_function_template_decls (newdecl, olddecl))
1966 : 0 : return error_mark_node;
1967 : : return NULL_TREE;
1968 : : }
1969 : : return NULL_TREE;
1970 : : }
1971 : 32017857 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1972 : : {
1973 : 32017746 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1974 : : {
1975 : 57 : auto_diagnostic_group d;
1976 : 57 : error_at (newdecl_loc,
1977 : : "conflicting declaration of C function %q#D",
1978 : : newdecl);
1979 : 57 : inform (olddecl_loc,
1980 : : "previous declaration %q#D", olddecl);
1981 : 57 : return error_mark_node;
1982 : 57 : }
1983 : : /* For function versions, params and types match, but they
1984 : : are not ambiguous. */
1985 : 32017689 : else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1986 : 32009919 : && !DECL_FUNCTION_VERSIONED (olddecl))
1987 : : /* Let constrained hidden friends coexist for now, we'll
1988 : : check satisfaction later. */
1989 : 32009919 : && !member_like_constrained_friend_p (newdecl)
1990 : 31907599 : && !member_like_constrained_friend_p (olddecl)
1991 : : // The functions have the same parameter types.
1992 : 31896554 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1993 : 31896554 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1994 : : // And the same constraints.
1995 : 32017752 : && equivalently_constrained (newdecl, olddecl))
1996 : : {
1997 : 63 : auto_diagnostic_group d;
1998 : 63 : error_at (newdecl_loc,
1999 : : "ambiguating new declaration of %q#D", newdecl);
2000 : 63 : inform (olddecl_loc,
2001 : : "old declaration %q#D", olddecl);
2002 : 63 : return error_mark_node;
2003 : 63 : }
2004 : : else
2005 : 32017626 : return NULL_TREE;
2006 : : }
2007 : : else
2008 : : {
2009 : 111 : auto_diagnostic_group d;
2010 : 111 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2011 : 111 : inform (olddecl_loc,
2012 : : "previous declaration as %q#D", olddecl);
2013 : 111 : return error_mark_node;
2014 : 111 : }
2015 : : }
2016 : 13812823 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2017 : 13812823 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2018 : : {
2019 : : /* OMP UDRs are never duplicates. */
2020 : 27 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2021 : 27 : auto_diagnostic_group d;
2022 : 27 : error_at (newdecl_loc,
2023 : : "redeclaration of %<pragma omp declare reduction%>");
2024 : 27 : inform (olddecl_loc,
2025 : : "previous %<pragma omp declare reduction%> declaration");
2026 : 27 : return error_mark_node;
2027 : 27 : }
2028 : 13812796 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2029 : 13812796 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2030 : 220 : && (!DECL_TEMPLATE_INFO (newdecl)
2031 : 220 : || (DECL_TI_TEMPLATE (newdecl)
2032 : 220 : != DECL_TI_TEMPLATE (olddecl))))
2033 : 9997115 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2034 : 564019 : && (!DECL_TEMPLATE_INFO (olddecl)
2035 : 564019 : || (DECL_TI_TEMPLATE (olddecl)
2036 : 564019 : != DECL_TI_TEMPLATE (newdecl))))))
2037 : : /* It's OK to have a template specialization and a non-template
2038 : : with the same type, or to have specializations of two
2039 : : different templates with the same type. Note that if one is a
2040 : : specialization, and the other is an instantiation of the same
2041 : : template, that we do not exit at this point. That situation
2042 : : can occur if we instantiate a template class, and then
2043 : : specialize one of its methods. This situation is valid, but
2044 : : the declarations must be merged in the usual way. */
2045 : : return NULL_TREE;
2046 : 13812796 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2047 : 13812796 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2048 : 563856 : && !DECL_USE_TEMPLATE (newdecl))
2049 : 9997115 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2050 : 57 : && !DECL_USE_TEMPLATE (olddecl))))
2051 : : /* One of the declarations is a template instantiation, and the
2052 : : other is not a template at all. That's OK. */
2053 : : return NULL_TREE;
2054 : 13812796 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2055 : : {
2056 : : /* In [namespace.alias] we have:
2057 : :
2058 : : In a declarative region, a namespace-alias-definition can be
2059 : : used to redefine a namespace-alias declared in that declarative
2060 : : region to refer only to the namespace to which it already
2061 : : refers.
2062 : :
2063 : : Therefore, if we encounter a second alias directive for the same
2064 : : alias, we can just ignore the second directive. */
2065 : 18 : if (DECL_NAMESPACE_ALIAS (newdecl)
2066 : 18 : && (DECL_NAMESPACE_ALIAS (newdecl)
2067 : 15 : == DECL_NAMESPACE_ALIAS (olddecl)))
2068 : : return olddecl;
2069 : :
2070 : : /* Leave it to update_binding to merge or report error. */
2071 : : return NULL_TREE;
2072 : : }
2073 : : else
2074 : : {
2075 : 13812778 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2076 : 13812778 : if (errmsg)
2077 : : {
2078 : 523 : auto_diagnostic_group d;
2079 : 523 : error_at (newdecl_loc, errmsg, newdecl);
2080 : 523 : if (DECL_NAME (olddecl) != NULL_TREE)
2081 : 523 : inform (olddecl_loc,
2082 : 523 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2083 : : ? G_("%q#D previously defined here")
2084 : : : G_("%q#D previously declared here"), olddecl);
2085 : 523 : if (cxx_dialect >= cxx26
2086 : 138 : && DECL_NAME (newdecl)
2087 : 138 : && id_equal (DECL_NAME (newdecl), "_")
2088 : 536 : && !name_independent_decl_p (newdecl))
2089 : : {
2090 : 13 : if (TREE_CODE (newdecl) == PARM_DECL)
2091 : 2 : inform (newdecl_loc,
2092 : : "parameter declaration is not name-independent");
2093 : 11 : else if (DECL_DECOMPOSITION_P (newdecl))
2094 : : {
2095 : 3 : if (at_namespace_scope_p ())
2096 : 2 : inform (newdecl_loc,
2097 : : "structured binding at namespace scope is not "
2098 : : "name-independent");
2099 : 1 : else if (TREE_STATIC (newdecl))
2100 : 1 : inform (newdecl_loc,
2101 : : "static structured binding is not "
2102 : : "name-independent");
2103 : 0 : else if (DECL_EXTERNAL (newdecl))
2104 : 0 : inform (newdecl_loc,
2105 : : "extern structured binding is not "
2106 : : "name-independent");
2107 : : }
2108 : 8 : else if (at_class_scope_p ()
2109 : : && VAR_P (newdecl)
2110 : 8 : && TREE_STATIC (newdecl))
2111 : 0 : inform (newdecl_loc,
2112 : : "static data member is not name-independent");
2113 : 8 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2114 : 3 : inform (newdecl_loc,
2115 : : "variable at namespace scope is not name-independent");
2116 : 5 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2117 : 3 : inform (newdecl_loc,
2118 : : "static variable is not name-independent");
2119 : 2 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2120 : 0 : inform (newdecl_loc,
2121 : : "extern variable is not name-independent");
2122 : : }
2123 : 523 : return error_mark_node;
2124 : 523 : }
2125 : 13812255 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2126 : 9996875 : && DECL_INITIAL (olddecl) != NULL_TREE
2127 : 11379 : && !prototype_p (TREE_TYPE (olddecl))
2128 : 13812255 : && prototype_p (TREE_TYPE (newdecl)))
2129 : : {
2130 : : /* Prototype decl follows defn w/o prototype. */
2131 : 0 : auto_diagnostic_group d;
2132 : 0 : if (warning_at (newdecl_loc, 0,
2133 : : "prototype specified for %q#D", newdecl))
2134 : 0 : inform (olddecl_loc,
2135 : : "previous non-prototype definition here");
2136 : 0 : }
2137 : 13435766 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2138 : 23809130 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2139 : : {
2140 : : /* [dcl.link]
2141 : : If two declarations of the same function or object
2142 : : specify different linkage-specifications ..., the program
2143 : : is ill-formed.... Except for functions with C++ linkage,
2144 : : a function declaration without a linkage specification
2145 : : shall not precede the first linkage specification for
2146 : : that function. A function can be declared without a
2147 : : linkage specification after an explicit linkage
2148 : : specification has been seen; the linkage explicitly
2149 : : specified in the earlier declaration is not affected by
2150 : : such a function declaration.
2151 : :
2152 : : DR 563 raises the question why the restrictions on
2153 : : functions should not also apply to objects. Older
2154 : : versions of G++ silently ignore the linkage-specification
2155 : : for this example:
2156 : :
2157 : : namespace N {
2158 : : extern int i;
2159 : : extern "C" int i;
2160 : : }
2161 : :
2162 : : which is clearly wrong. Therefore, we now treat objects
2163 : : like functions. */
2164 : 1878 : if (current_lang_depth () == 0)
2165 : : {
2166 : : /* There is no explicit linkage-specification, so we use
2167 : : the linkage from the previous declaration. */
2168 : 1872 : retrofit_lang_decl (newdecl);
2169 : 1872 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2170 : : }
2171 : : else
2172 : : {
2173 : 6 : auto_diagnostic_group d;
2174 : 6 : error_at (newdecl_loc,
2175 : : "conflicting declaration of %q#D with %qL linkage",
2176 : 6 : newdecl, DECL_LANGUAGE (newdecl));
2177 : 6 : inform (olddecl_loc,
2178 : : "previous declaration with %qL linkage",
2179 : 6 : DECL_LANGUAGE (olddecl));
2180 : 6 : }
2181 : : }
2182 : :
2183 : 13812255 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2184 : : ;
2185 : 13227427 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2186 : : {
2187 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2188 : 11028241 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2189 : 9432808 : && (/* grokfndecl passes member function templates too
2190 : : as FUNCTION_DECLs. */
2191 : 7984103 : DECL_TEMPLATE_INFO (olddecl)
2192 : : /* C++11 8.3.6/6.
2193 : : Default arguments for a member function of a class
2194 : : template shall be specified on the initial declaration
2195 : : of the member function within the class template. */
2196 : 637994 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2197 : : {
2198 : 7425342 : check_redeclaration_no_default_args (newdecl);
2199 : :
2200 : 7425342 : if (DECL_TEMPLATE_INFO (olddecl)
2201 : 7425342 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2202 : : {
2203 : 2064154 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2204 : 2064154 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2205 : 2064154 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2206 : 2064154 : tree old_parms
2207 : 2064154 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2208 : 2064154 : merge_default_template_args (new_parms, old_parms,
2209 : : /*class_p=*/false);
2210 : : }
2211 : : }
2212 : : else
2213 : : {
2214 : 2007466 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2215 : 2007466 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2216 : 2007466 : int i = 1;
2217 : :
2218 : 5432488 : for (; t1 && t1 != void_list_node;
2219 : 3425022 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2220 : 3426959 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2221 : : {
2222 : 39 : if (simple_cst_equal (TREE_PURPOSE (t1),
2223 : 39 : TREE_PURPOSE (t2)) == 1)
2224 : : {
2225 : 12 : auto_diagnostic_group d;
2226 : 12 : if (permerror (newdecl_loc,
2227 : : "default argument given for parameter "
2228 : : "%d of %q#D", i, newdecl))
2229 : 12 : inform (olddecl_loc,
2230 : : "previous specification in %q#D here",
2231 : : olddecl);
2232 : 12 : }
2233 : : else
2234 : : {
2235 : 27 : auto_diagnostic_group d;
2236 : 27 : error_at (newdecl_loc,
2237 : : "default argument given for parameter %d "
2238 : : "of %q#D", i, newdecl);
2239 : 27 : inform (olddecl_loc,
2240 : : "previous specification in %q#D here",
2241 : : olddecl);
2242 : 27 : }
2243 : : }
2244 : :
2245 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2246 : : argument expression, that declaration... shall be the only
2247 : : declaration of the function or function template in the
2248 : : translation unit." */
2249 : 2007466 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2250 : : }
2251 : : }
2252 : : }
2253 : :
2254 : : /* Do not merge an implicit typedef with an explicit one. In:
2255 : :
2256 : : class A;
2257 : : ...
2258 : : typedef class A A __attribute__ ((foo));
2259 : :
2260 : : the attribute should apply only to the typedef. */
2261 : 17466656 : if (TREE_CODE (olddecl) == TYPE_DECL
2262 : 17466656 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2263 : 21309 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2264 : : return NULL_TREE;
2265 : :
2266 : 17437201 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2267 : : return NULL_TREE;
2268 : :
2269 : 17437189 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2270 : 30 : return error_mark_node;
2271 : :
2272 : 17437159 : if (modules_p ()
2273 : 70256 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2274 : 17476870 : && TREE_CODE (olddecl) != NAMESPACE_DECL)
2275 : : {
2276 : 39711 : if (!module_may_redeclare (olddecl, newdecl))
2277 : 24 : return error_mark_node;
2278 : :
2279 : 39687 : if (!hiding)
2280 : : {
2281 : : /* The old declaration should match the exportingness of the new
2282 : : declaration. But hidden friend declarations just keep the
2283 : : exportingness of the old declaration; see CWG2588. */
2284 : 36364 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2285 : 36364 : if (DECL_LANG_SPECIFIC (not_tmpl)
2286 : 36322 : && DECL_MODULE_ATTACH_P (not_tmpl)
2287 : : /* Typedefs are not entities and so are OK to be redeclared
2288 : : as exported: see [module.interface]/p6. */
2289 : 36556 : && TREE_CODE (olddecl) != TYPE_DECL)
2290 : : {
2291 : 180 : if (DECL_MODULE_EXPORT_P (newdecl)
2292 : 180 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2293 : : {
2294 : 15 : auto_diagnostic_group d;
2295 : 15 : error ("conflicting exporting for declaration %qD", newdecl);
2296 : 15 : inform (olddecl_loc,
2297 : : "previously declared here without exporting");
2298 : 15 : }
2299 : : }
2300 : 36184 : else if (DECL_MODULE_EXPORT_P (newdecl))
2301 : 17399 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2302 : : }
2303 : : }
2304 : :
2305 : : /* We have committed to returning OLDDECL at this point. */
2306 : :
2307 : : /* If new decl is `static' and an `extern' was seen previously,
2308 : : warn about it. */
2309 : 17437135 : warn_extern_redeclared_static (newdecl, olddecl);
2310 : :
2311 : : /* True to merge attributes between the declarations, false to
2312 : : set OLDDECL's attributes to those of NEWDECL (for template
2313 : : explicit specializations that specify their own attributes
2314 : : independent of those specified for the primary template). */
2315 : 17437135 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2316 : 13651231 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2317 : 18001139 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2318 : :
2319 : 17437135 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2320 : : {
2321 : 13651231 : if (merge_attr)
2322 : : {
2323 : 13087432 : {
2324 : 13087432 : auto_diagnostic_group d;
2325 : 13087432 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2326 : 36 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2327 : : ? G_("previous definition of %qD here")
2328 : : : G_("previous declaration of %qD here"), olddecl);
2329 : 13087432 : }
2330 : :
2331 : : /* [dcl.attr.noreturn]: The first declaration of a function shall
2332 : : specify the noreturn attribute if any declaration of that function
2333 : : specifies the noreturn attribute. */
2334 : 13087432 : tree a;
2335 : 13087432 : if (TREE_THIS_VOLATILE (newdecl)
2336 : 106602 : && !TREE_THIS_VOLATILE (olddecl)
2337 : : /* This applies to [[noreturn]] only, not its GNU variants. */
2338 : 76 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2339 : 54 : && cxx11_attribute_p (a)
2340 : 13087453 : && get_attribute_namespace (a) == NULL_TREE)
2341 : : {
2342 : 9 : auto_diagnostic_group d;
2343 : 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2344 : : "but its first declaration was not", newdecl);
2345 : 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2346 : 9 : }
2347 : : }
2348 : :
2349 : : /* Now that functions must hold information normally held
2350 : : by field decls, there is extra work to do so that
2351 : : declaration information does not get destroyed during
2352 : : definition. */
2353 : 13651231 : if (DECL_VINDEX (olddecl))
2354 : 160526 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2355 : 13651231 : if (DECL_CONTEXT (olddecl))
2356 : 13651231 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2357 : 13651231 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2358 : 13651231 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2359 : 13651231 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2360 : 13651231 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2361 : 13651231 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2362 : 13651231 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2363 : 13651231 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2364 : 13651231 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2365 : 13651231 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2366 : 13651231 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2367 : 13651231 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2368 : 2411535 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2369 : 4823070 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2370 : 13651231 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2371 : :
2372 : 13651231 : duplicate_contracts (newdecl, olddecl);
2373 : :
2374 : : /* Optionally warn about more than one declaration for the same
2375 : : name, but don't warn about a function declaration followed by a
2376 : : definition. */
2377 : 27 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2378 : 27 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2379 : : /* Don't warn about extern decl followed by definition. */
2380 : 27 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2381 : : /* Don't warn if at least one is/was hidden. */
2382 : 27 : && !(hiding || was_hidden)
2383 : : /* Don't warn about declaration followed by specialization. */
2384 : 13651243 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2385 : 9 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2386 : : {
2387 : 6 : auto_diagnostic_group d;
2388 : 6 : if (warning_at (newdecl_loc,
2389 : : OPT_Wredundant_decls,
2390 : : "redundant redeclaration of %qD in same scope",
2391 : : newdecl))
2392 : 6 : inform (olddecl_loc,
2393 : : "previous declaration of %qD", olddecl);
2394 : 6 : }
2395 : :
2396 : : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2397 : : first declaration of the function or, for an explicit specialization
2398 : : of a function template, the first declaration of that
2399 : : specialization. */
2400 : 13651231 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2401 : 563856 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2402 : : {
2403 : 13087432 : if (DECL_DELETED_FN (newdecl))
2404 : : {
2405 : 21 : auto_diagnostic_group d;
2406 : 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2407 : : "is not first declaration", newdecl))
2408 : 21 : inform (olddecl_loc,
2409 : : "previous declaration of %qD", olddecl);
2410 : 21 : }
2411 : 13087432 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2412 : 13087432 : if (DECL_DELETED_FN (olddecl)
2413 : 6 : && DECL_INITIAL (olddecl)
2414 : 13087438 : && TREE_CODE (DECL_INITIAL (olddecl)) == STRING_CST)
2415 : 3 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2416 : : }
2417 : : }
2418 : :
2419 : : /* Deal with C++: must preserve virtual function table size. */
2420 : 17437135 : if (TREE_CODE (olddecl) == TYPE_DECL)
2421 : : {
2422 : 21306 : tree newtype = TREE_TYPE (newdecl);
2423 : 21306 : tree oldtype = TREE_TYPE (olddecl);
2424 : :
2425 : 21306 : if (newtype != error_mark_node && oldtype != error_mark_node
2426 : 42612 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2427 : 52794 : CLASSTYPE_FRIEND_CLASSES (newtype)
2428 : 17598 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2429 : :
2430 : 21306 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2431 : : }
2432 : :
2433 : : /* Copy all the DECL_... slots specified in the new decl except for
2434 : : any that we copy here from the old type. */
2435 : 17437135 : if (merge_attr)
2436 : 16873336 : DECL_ATTRIBUTES (newdecl)
2437 : 33746672 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2438 : : else
2439 : 563799 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2440 : :
2441 : 17437135 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2442 : : {
2443 : 3388109 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2444 : 3388109 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2445 : 3388109 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2446 : :
2447 : : /* The new decl should not already have gathered any
2448 : : specializations. */
2449 : 3388109 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2450 : :
2451 : : /* Make sure the contracts are equivalent. */
2452 : 3388109 : duplicate_contracts (newdecl, olddecl);
2453 : :
2454 : : /* Remove contracts from old_result so they aren't appended to
2455 : : old_result by the merge function. */
2456 : 3388109 : remove_contract_attributes (old_result);
2457 : :
2458 : 3388109 : DECL_ATTRIBUTES (old_result)
2459 : 3388109 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2460 : :
2461 : 3388109 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2462 : : {
2463 : 3388082 : if (DECL_SOURCE_LOCATION (newdecl)
2464 : 3388082 : != DECL_SOURCE_LOCATION (olddecl))
2465 : : {
2466 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2467 : : later declarations of a function template. */
2468 : 3365457 : check_redeclaration_no_default_args (newdecl);
2469 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2470 : : argument expression, that declaration... shall be the only
2471 : : declaration of the function or function template in the
2472 : : translation unit." */
2473 : 3365457 : check_no_redeclaration_friend_default_args
2474 : 3365457 : (old_result, new_result);
2475 : :
2476 : 3365457 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2477 : 3365457 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2478 : 3365457 : merge_default_template_args (new_parms, old_parms,
2479 : : /*class_p=*/false);
2480 : : }
2481 : 3388082 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2482 : 2828656 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2483 : :
2484 : 3388082 : check_default_args (newdecl);
2485 : :
2486 : 6776152 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2487 : 3388082 : && DECL_INITIAL (new_result))
2488 : : {
2489 : 24 : if (DECL_INITIAL (old_result))
2490 : 12 : DECL_UNINLINABLE (old_result) = 1;
2491 : : else
2492 : 12 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2493 : 24 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2494 : 72 : DECL_NOT_REALLY_EXTERN (old_result)
2495 : 24 : = DECL_NOT_REALLY_EXTERN (new_result);
2496 : 48 : DECL_INTERFACE_KNOWN (old_result)
2497 : 24 : = DECL_INTERFACE_KNOWN (new_result);
2498 : 24 : DECL_DECLARED_INLINE_P (old_result)
2499 : 24 : = DECL_DECLARED_INLINE_P (new_result);
2500 : 24 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2501 : 24 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2502 : : }
2503 : : else
2504 : : {
2505 : 3388058 : DECL_DECLARED_INLINE_P (old_result)
2506 : 3388058 : |= DECL_DECLARED_INLINE_P (new_result);
2507 : 3388058 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2508 : 3388058 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2509 : 3388058 : check_redeclaration_exception_specification (newdecl, olddecl);
2510 : :
2511 : 3388058 : merge_attribute_bits (new_result, old_result);
2512 : : }
2513 : : }
2514 : :
2515 : : /* If the new declaration is a definition, update the file and
2516 : : line information on the declaration, and also make
2517 : : the old declaration the same definition. */
2518 : 3388109 : if (DECL_INITIAL (new_result) != NULL_TREE)
2519 : : {
2520 : 5244228 : DECL_SOURCE_LOCATION (olddecl)
2521 : 5244228 : = DECL_SOURCE_LOCATION (old_result)
2522 : 2622114 : = DECL_SOURCE_LOCATION (newdecl);
2523 : 2622114 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2524 : 2622114 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2525 : : {
2526 : 2622114 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2527 : 9336930 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2528 : 6714816 : DECL_CONTEXT (p) = old_result;
2529 : :
2530 : 5244228 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2531 : 14 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2532 : : }
2533 : : }
2534 : :
2535 : 3388109 : return olddecl;
2536 : : }
2537 : :
2538 : 14049026 : if (types_match)
2539 : : {
2540 : 14048817 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2541 : 13651022 : check_redeclaration_exception_specification (newdecl, olddecl);
2542 : :
2543 : : /* Automatically handles default parameters. */
2544 : 14048817 : tree oldtype = TREE_TYPE (olddecl);
2545 : 14048817 : tree newtype;
2546 : :
2547 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2548 : : at newdecl, which will be ggc_freed. */
2549 : 14048817 : if (TREE_CODE (newdecl) == TYPE_DECL)
2550 : : {
2551 : : /* But NEWTYPE might have an attribute, honor that. */
2552 : 21306 : tree tem = TREE_TYPE (newdecl);
2553 : 21306 : newtype = oldtype;
2554 : :
2555 : 21306 : if (TYPE_USER_ALIGN (tem))
2556 : : {
2557 : 21 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2558 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2559 : 21 : TYPE_USER_ALIGN (newtype) = true;
2560 : : }
2561 : :
2562 : : /* And remove the new type from the variants list. */
2563 : 21306 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2564 : : {
2565 : 30 : tree remove = TREE_TYPE (newdecl);
2566 : 30 : if (TYPE_MAIN_VARIANT (remove) == remove)
2567 : : {
2568 : 9 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2569 : : /* If remove is the main variant, no need to remove that
2570 : : from the list. One of the DECL_ORIGINAL_TYPE
2571 : : variants, e.g. created for aligned attribute, might still
2572 : : refer to the newdecl TYPE_DECL though, so remove that one
2573 : : in that case. */
2574 : 9 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2575 : 6 : if (orig != remove)
2576 : 6 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2577 : 0 : t = TYPE_MAIN_VARIANT (t))
2578 : 6 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2579 : : {
2580 : 12 : TYPE_NEXT_VARIANT (t)
2581 : 6 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2582 : 6 : break;
2583 : : }
2584 : : }
2585 : : else
2586 : 21 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2587 : 0 : t = TYPE_NEXT_VARIANT (t))
2588 : 21 : if (TYPE_NEXT_VARIANT (t) == remove)
2589 : : {
2590 : 21 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2591 : 21 : break;
2592 : : }
2593 : : }
2594 : : }
2595 : 14027511 : else if (merge_attr)
2596 : 13463712 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2597 : : else
2598 : 563799 : newtype = TREE_TYPE (newdecl);
2599 : :
2600 : 14048817 : if (VAR_P (newdecl))
2601 : : {
2602 : 376489 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2603 : : /* For already initialized vars, TREE_READONLY could have been
2604 : : cleared in cp_finish_decl, because the var needs runtime
2605 : : initialization or destruction. Make sure not to set
2606 : : TREE_READONLY on it again. */
2607 : 376489 : if (DECL_INITIALIZED_P (olddecl)
2608 : 1207 : && !DECL_EXTERNAL (olddecl)
2609 : 376572 : && !TREE_READONLY (olddecl))
2610 : 30 : TREE_READONLY (newdecl) = 0;
2611 : 376489 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2612 : 752978 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2613 : 376489 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2614 : 376489 : if (DECL_DEPENDENT_INIT_P (olddecl))
2615 : 102074 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2616 : 752978 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2617 : 376489 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2618 : 376489 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2619 : 376489 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2620 : 752978 : DECL_DECLARED_CONSTINIT_P (newdecl)
2621 : 376489 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2622 : :
2623 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2624 : 376489 : if (DECL_LANG_SPECIFIC (olddecl)
2625 : 749380 : && CP_DECL_THREADPRIVATE_P (olddecl))
2626 : : {
2627 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2628 : 31 : retrofit_lang_decl (newdecl);
2629 : 31 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2630 : : }
2631 : : }
2632 : :
2633 : : /* An explicit specialization of a function template or of a member
2634 : : function of a class template can be declared transaction_safe
2635 : : independently of whether the corresponding template entity is declared
2636 : : transaction_safe. */
2637 : 1106 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2638 : 1004 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2639 : 50 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2640 : 50 : && tx_safe_fn_type_p (newtype)
2641 : 14048817 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2642 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2643 : :
2644 : 14048817 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2645 : :
2646 : 14048817 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2647 : 13651022 : check_default_args (newdecl);
2648 : :
2649 : : /* Lay the type out, unless already done. */
2650 : 14048817 : if (! same_type_p (newtype, oldtype)
2651 : 377 : && TREE_TYPE (newdecl) != error_mark_node
2652 : 14049194 : && !(processing_template_decl && uses_template_parms (newdecl)))
2653 : 298 : layout_type (TREE_TYPE (newdecl));
2654 : :
2655 : 14048817 : if ((VAR_P (newdecl)
2656 : 13672328 : || TREE_CODE (newdecl) == PARM_DECL
2657 : 13672328 : || TREE_CODE (newdecl) == RESULT_DECL
2658 : 13672328 : || TREE_CODE (newdecl) == FIELD_DECL
2659 : 13672328 : || TREE_CODE (newdecl) == TYPE_DECL)
2660 : 14070123 : && !(processing_template_decl && uses_template_parms (newdecl)))
2661 : 240416 : layout_decl (newdecl, 0);
2662 : :
2663 : : /* Merge deprecatedness. */
2664 : 14048817 : if (TREE_DEPRECATED (newdecl))
2665 : 11678 : TREE_DEPRECATED (olddecl) = 1;
2666 : :
2667 : : /* Merge unavailability. */
2668 : 14048817 : if (TREE_UNAVAILABLE (newdecl))
2669 : 3 : TREE_UNAVAILABLE (olddecl) = 1;
2670 : :
2671 : : /* Preserve function specific target and optimization options */
2672 : 14048817 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2673 : : {
2674 : 13651022 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2675 : 13651022 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2676 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2677 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2678 : :
2679 : 13651022 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2680 : 13651022 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2681 : 31 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2682 : 31 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2683 : :
2684 : 13651022 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2685 : 13491221 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2686 : : }
2687 : : else
2688 : : {
2689 : : /* Merge the const type qualifier. */
2690 : 397795 : if (TREE_READONLY (newdecl))
2691 : 35063 : TREE_READONLY (olddecl) = 1;
2692 : : /* Merge the volatile type qualifier. */
2693 : 397795 : if (TREE_THIS_VOLATILE (newdecl))
2694 : 5 : TREE_THIS_VOLATILE (olddecl) = 1;
2695 : : }
2696 : :
2697 : : /* Merge the initialization information. */
2698 : 14048817 : if (DECL_INITIAL (newdecl) == NULL_TREE
2699 : 14048817 : && DECL_INITIAL (olddecl) != NULL_TREE)
2700 : : {
2701 : 170932 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2702 : 170932 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2703 : 170932 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2704 : : {
2705 : 11298 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2706 : 11298 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2707 : : }
2708 : : }
2709 : :
2710 : 14048817 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2711 : : {
2712 : 13651022 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2713 : 13651022 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2714 : 13651022 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2715 : 13651022 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2716 : 51478 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2717 : 13651022 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2718 : 13651022 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2719 : 13651022 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2720 : 13651022 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2721 : :
2722 : 13651022 : if (merge_attr)
2723 : 13087223 : merge_attribute_bits (newdecl, olddecl);
2724 : : else
2725 : : {
2726 : : /* Merge the noreturn bit. */
2727 : 563799 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2728 : 563799 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2729 : 563799 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2730 : 563799 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2731 : 563799 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2732 : : }
2733 : : /* Keep the old RTL. */
2734 : 13651022 : COPY_DECL_RTL (olddecl, newdecl);
2735 : : }
2736 : 397795 : else if (VAR_P (newdecl)
2737 : 397795 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2738 : : {
2739 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2740 : : declaration was for an incomplete object and the new
2741 : : declaration is not since many attributes of the RTL will
2742 : : change. */
2743 : 376415 : COPY_DECL_RTL (olddecl, newdecl);
2744 : : }
2745 : : }
2746 : : /* If cannot merge, then use the new type and qualifiers,
2747 : : and don't preserve the old rtl. */
2748 : : else
2749 : : {
2750 : : /* Clean out any memory we had of the old declaration. */
2751 : 209 : tree oldstatic = value_member (olddecl, static_aggregates);
2752 : 209 : if (oldstatic)
2753 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2754 : :
2755 : 209 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2756 : 209 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2757 : 209 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2758 : 209 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2759 : 209 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2760 : : }
2761 : :
2762 : : /* Merge the storage class information. */
2763 : 14049026 : merge_weak (newdecl, olddecl);
2764 : :
2765 : 14049026 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2766 : 14049026 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2767 : 14049026 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2768 : 14049026 : if (! DECL_EXTERNAL (olddecl))
2769 : 25900 : DECL_EXTERNAL (newdecl) = 0;
2770 : 14049026 : if (! DECL_COMDAT (olddecl))
2771 : 5644502 : DECL_COMDAT (newdecl) = 0;
2772 : :
2773 : 14049026 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2774 : : {
2775 : 75 : if (!DECL_LOCAL_DECL_P (olddecl))
2776 : : /* This can happen if olddecl was brought in from the
2777 : : enclosing namespace via a using-decl. The new decl is
2778 : : then not a block-scope extern at all. */
2779 : 3 : DECL_LOCAL_DECL_P (newdecl) = false;
2780 : : else
2781 : : {
2782 : 72 : retrofit_lang_decl (newdecl);
2783 : 72 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2784 : 72 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2785 : 72 : if (alias != error_mark_node)
2786 : : {
2787 : 69 : DECL_ATTRIBUTES (alias)
2788 : 69 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2789 : 69 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2790 : 39 : merge_attribute_bits (newdecl, alias);
2791 : : }
2792 : : }
2793 : : }
2794 : :
2795 : 14049026 : new_template_info = NULL_TREE;
2796 : 14049026 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2797 : : {
2798 : 14024176 : bool new_redefines_gnu_inline = false;
2799 : :
2800 : 14024176 : if (new_defines_function
2801 : 14024176 : && ((DECL_INTERFACE_KNOWN (olddecl)
2802 : 1218 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2803 : 8709233 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2804 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2805 : : == FUNCTION_DECL))))
2806 : 2436 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2807 : :
2808 : : if (!new_redefines_gnu_inline)
2809 : : {
2810 : 14024101 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2811 : 14024101 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2812 : 14024101 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2813 : : }
2814 : :
2815 : 14024176 : if (TREE_CODE (newdecl) != TYPE_DECL)
2816 : : {
2817 : 14024118 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2818 : 14024118 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2819 : 14024118 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2820 : :
2821 : : /* If the OLDDECL is an instantiation and/or specialization,
2822 : : then the NEWDECL must be too. But, it may not yet be marked
2823 : : as such if the caller has created NEWDECL, but has not yet
2824 : : figured out that it is a redeclaration. */
2825 : 14024118 : if (!DECL_USE_TEMPLATE (newdecl))
2826 : 13460048 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2827 : :
2828 : 14024118 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2829 : 13460095 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2830 : 26920190 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2831 : : }
2832 : :
2833 : : /* Don't really know how much of the language-specific
2834 : : values we should copy from old to new. */
2835 : 14024176 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2836 : :
2837 : 14024176 : if (LANG_DECL_HAS_MIN (newdecl))
2838 : : {
2839 : 14024176 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2840 : 14024176 : if (DECL_TEMPLATE_INFO (newdecl))
2841 : : {
2842 : 575452 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
2843 : 575452 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2844 : 575452 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2845 : : /* Remember the presence of explicit specialization args. */
2846 : 1127604 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2847 : 1127604 : = TINFO_USED_TEMPLATE_ID (new_template_info);
2848 : : }
2849 : :
2850 : : /* We don't want to copy template info from a non-templated friend
2851 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2852 : 14024176 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2853 : : || !non_templated_friend_p (olddecl));
2854 : 14024176 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2855 : : }
2856 : :
2857 : 14024176 : if (DECL_DECLARES_FUNCTION_P (newdecl))
2858 : : {
2859 : : /* Only functions have these fields. */
2860 : 13651231 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2861 : 13651231 : DECL_BEFRIENDING_CLASSES (newdecl)
2862 : 13651231 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2863 : 13651231 : DECL_BEFRIENDING_CLASSES (olddecl));
2864 : : /* DECL_THUNKS is only valid for virtual functions,
2865 : : otherwise it is a DECL_FRIEND_CONTEXT. */
2866 : 13651231 : if (DECL_VIRTUAL_P (newdecl))
2867 : 646958 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2868 : 26008546 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2869 : 78813 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2870 : : }
2871 : 372945 : else if (VAR_P (newdecl))
2872 : : {
2873 : : /* Only variables have this field. */
2874 : 372887 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2875 : 192 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2876 : : }
2877 : : }
2878 : :
2879 : 14049026 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2880 : : {
2881 : 13651231 : tree parm;
2882 : :
2883 : : /* Merge parameter attributes. */
2884 : 13651231 : tree oldarg, newarg;
2885 : 13651231 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2886 : 39165205 : oldarg && newarg;
2887 : 25513974 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2888 : : {
2889 : 25513974 : DECL_ATTRIBUTES (newarg)
2890 : 25513974 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
2891 : 25513974 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2892 : : }
2893 : :
2894 : 13651231 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2895 : 13651231 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2896 : : {
2897 : : /* If newdecl is not a specialization, then it is not a
2898 : : template-related function at all. And that means that we
2899 : : should have exited above, returning 0. */
2900 : 563799 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2901 : :
2902 : 563799 : if (DECL_ODR_USED (olddecl))
2903 : : /* From [temp.expl.spec]:
2904 : :
2905 : : If a template, a member template or the member of a class
2906 : : template is explicitly specialized then that
2907 : : specialization shall be declared before the first use of
2908 : : that specialization that would cause an implicit
2909 : : instantiation to take place, in every translation unit in
2910 : : which such a use occurs. */
2911 : 0 : error ("explicit specialization of %qD after first use",
2912 : : olddecl);
2913 : :
2914 : 563799 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2915 : 563799 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2916 : 1127460 : && DECL_DECLARED_INLINE_P (newdecl));
2917 : :
2918 : : /* Don't propagate visibility from the template to the
2919 : : specialization here. We'll do that in determine_visibility if
2920 : : appropriate. */
2921 : 563799 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2922 : :
2923 : : /* [temp.expl.spec/14] We don't inline explicit specialization
2924 : : just because the primary template says so. */
2925 : 563799 : gcc_assert (!merge_attr);
2926 : :
2927 : 563799 : DECL_DECLARED_INLINE_P (olddecl)
2928 : 563799 : = DECL_DECLARED_INLINE_P (newdecl);
2929 : :
2930 : 563799 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
2931 : 563799 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2932 : :
2933 : 563799 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2934 : : }
2935 : 13087432 : else if (new_defines_function && DECL_INITIAL (olddecl))
2936 : : {
2937 : : /* Never inline re-defined extern inline functions.
2938 : : FIXME: this could be better handled by keeping both
2939 : : function as separate declarations. */
2940 : 75 : DECL_UNINLINABLE (newdecl) = 1;
2941 : : }
2942 : : else
2943 : : {
2944 : 13087357 : if (DECL_PENDING_INLINE_P (olddecl))
2945 : : {
2946 : 6 : DECL_PENDING_INLINE_P (newdecl) = 1;
2947 : 6 : DECL_PENDING_INLINE_INFO (newdecl)
2948 : 12 : = DECL_PENDING_INLINE_INFO (olddecl);
2949 : : }
2950 : 13087351 : else if (DECL_PENDING_INLINE_P (newdecl))
2951 : : ;
2952 : 13087351 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2953 : 13087351 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2954 : 26174702 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2955 : :
2956 : 13087357 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2957 : :
2958 : 39262071 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2959 : 26174714 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2960 : :
2961 : 26174714 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
2962 : 13087357 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2963 : 13087357 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2964 : 25787989 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2965 : : }
2966 : :
2967 : : /* Preserve abstractness on cloned [cd]tors. */
2968 : 13651231 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2969 : :
2970 : : /* Update newdecl's parms to point at olddecl. */
2971 : 44662091 : for (parm = DECL_ARGUMENTS (newdecl); parm;
2972 : 31010860 : parm = DECL_CHAIN (parm))
2973 : 31010860 : DECL_CONTEXT (parm) = olddecl;
2974 : :
2975 : 13651231 : if (! types_match)
2976 : : {
2977 : 209 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2978 : 209 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2979 : 209 : COPY_DECL_RTL (newdecl, olddecl);
2980 : : }
2981 : 13651231 : if (! types_match || new_defines_function)
2982 : : {
2983 : : /* These are the final DECL_ARGUMENTS that will be used within the
2984 : : body; update any references to old DECL_ARGUMENTS in the
2985 : : contracts, if present. */
2986 : 8710660 : if (tree contracts = DECL_CONTRACTS (newdecl))
2987 : 131 : remap_contracts (olddecl, newdecl, contracts, true);
2988 : :
2989 : : /* These need to be copied so that the names are available.
2990 : : Note that if the types do match, we'll preserve inline
2991 : : info and other bits, but if not, we won't. */
2992 : 8710660 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2993 : 8710660 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2994 : :
2995 : : /* In some cases, duplicate_contracts will remove contracts from
2996 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
2997 : : shared. If we removed them, re-add them. */
2998 : 8710660 : if (!DECL_CONTRACTS (olddecl))
2999 : 8710613 : copy_contract_attributes (olddecl, newdecl);
3000 : : }
3001 : : /* If redeclaring a builtin function, it stays built in
3002 : : if newdecl is a gnu_inline definition, or if newdecl is just
3003 : : a declaration. */
3004 : 13651231 : if (fndecl_built_in_p (olddecl)
3005 : 13708404 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3006 : : {
3007 : 3635690 : copy_decl_built_in_function (newdecl, olddecl);
3008 : : /* If we're keeping the built-in definition, keep the rtl,
3009 : : regardless of declaration matches. */
3010 : 3635690 : COPY_DECL_RTL (olddecl, newdecl);
3011 : 3635690 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3012 : : {
3013 : 3635684 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3014 : 3635684 : if (builtin_decl_explicit_p (fncode))
3015 : : {
3016 : : /* A compatible prototype of these builtin functions
3017 : : is seen, assume the runtime implements it with
3018 : : the expected semantics. */
3019 : 3635684 : switch (fncode)
3020 : : {
3021 : 10067 : case BUILT_IN_STPCPY:
3022 : 10067 : set_builtin_decl_implicit_p (fncode, true);
3023 : 10067 : break;
3024 : 3625617 : default:
3025 : 3625617 : set_builtin_decl_declared_p (fncode, true);
3026 : 3625617 : break;
3027 : : }
3028 : : }
3029 : :
3030 : 3635684 : copy_attributes_to_builtin (newdecl);
3031 : : }
3032 : : }
3033 : 13651231 : if (new_defines_function)
3034 : : /* If defining a function declared with other language
3035 : : linkage, use the previously declared language linkage. */
3036 : 8710451 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3037 : 4940780 : else if (types_match)
3038 : : {
3039 : 4940571 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3040 : : /* Don't clear out the arguments if we're just redeclaring a
3041 : : function. */
3042 : 4940571 : if (DECL_ARGUMENTS (olddecl))
3043 : : {
3044 : : /* If we removed contracts from previous definition, re-attach
3045 : : them. Otherwise, rewrite the contracts so they match the
3046 : : parameters of the new declaration. */
3047 : 1217427 : if (DECL_INITIAL (olddecl)
3048 : 11191 : && DECL_CONTRACTS (newdecl)
3049 : 1217438 : && !DECL_CONTRACTS (olddecl))
3050 : 10 : copy_contract_attributes (olddecl, newdecl);
3051 : : else
3052 : : {
3053 : : /* Temporarily undo the re-contexting of parameters so we can
3054 : : actually remap parameters. The inliner won't replace
3055 : : parameters if we don't do this. */
3056 : 1217417 : tree args = DECL_ARGUMENTS (newdecl);
3057 : 4560918 : for (tree p = args; p; p = DECL_CHAIN (p))
3058 : 3343501 : DECL_CONTEXT (p) = newdecl;
3059 : :
3060 : : /* Save new argument names for use in contracts parsing,
3061 : : unless we've already started parsing the body of olddecl
3062 : : (particular issues arise when newdecl is from a prior
3063 : : friend decl with no argument names, see
3064 : : modules/contracts-tpl-friend-1). */
3065 : 1217417 : if (tree contracts = DECL_CONTRACTS (olddecl))
3066 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3067 : :
3068 : : /* And reverse this operation again. */
3069 : 4560918 : for (tree p = args; p; p = DECL_CHAIN (p))
3070 : 3343501 : DECL_CONTEXT (p) = olddecl;
3071 : : }
3072 : :
3073 : 1217427 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3074 : : }
3075 : : }
3076 : : }
3077 : 397795 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3078 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3079 : :
3080 : : /* Now preserve various other info from the definition. */
3081 : 14049026 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3082 : 14049026 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3083 : 14049026 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3084 : 14049026 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3085 : :
3086 : : /* Warn about conflicting visibility specifications. */
3087 : 14049026 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3088 : 4863744 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3089 : 14049543 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3090 : : {
3091 : 12 : auto_diagnostic_group d;
3092 : 12 : if (warning_at (newdecl_loc, OPT_Wattributes,
3093 : : "%qD: visibility attribute ignored because it "
3094 : : "conflicts with previous declaration", newdecl))
3095 : 12 : inform (olddecl_loc,
3096 : : "previous declaration of %qD", olddecl);
3097 : 12 : }
3098 : : /* Choose the declaration which specified visibility. */
3099 : 14049026 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3100 : : {
3101 : 4863744 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3102 : 4863744 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3103 : : }
3104 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3105 : : so keep this behavior. */
3106 : 14049026 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3107 : : {
3108 : 4 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3109 : 4 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3110 : : }
3111 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3112 : 14049026 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3113 : : {
3114 : 54 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3115 : 54 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3116 : : }
3117 : 14048972 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3118 : 14048972 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3119 : 0 : DECL_USER_ALIGN (newdecl) = 1;
3120 : :
3121 : 14049026 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3122 : 28098052 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3123 : 14049026 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3124 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3125 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3126 : 14049026 : if (TREE_CODE (newdecl) == FIELD_DECL)
3127 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3128 : :
3129 : : /* Merge module entity mapping information. */
3130 : 14049026 : if (DECL_LANG_SPECIFIC (olddecl)
3131 : 28073206 : && (DECL_MODULE_ENTITY_P (olddecl)
3132 : 14023958 : || DECL_MODULE_KEYED_DECLS_P (olddecl)))
3133 : : {
3134 : 222 : retrofit_lang_decl (newdecl);
3135 : 222 : DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl);
3136 : 222 : DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl);
3137 : : }
3138 : :
3139 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3140 : : with that from NEWDECL below. */
3141 : 14049026 : if (DECL_LANG_SPECIFIC (olddecl))
3142 : : {
3143 : 14024180 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3144 : : != DECL_LANG_SPECIFIC (newdecl));
3145 : 14024180 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3146 : : }
3147 : :
3148 : : /* Merge the USED information. */
3149 : 14049026 : if (TREE_USED (olddecl))
3150 : 469823 : TREE_USED (newdecl) = 1;
3151 : 13579203 : else if (TREE_USED (newdecl))
3152 : 51 : TREE_USED (olddecl) = 1;
3153 : :
3154 : 14049026 : if (VAR_P (newdecl))
3155 : : {
3156 : 376489 : if (DECL_READ_P (olddecl))
3157 : 33267 : DECL_READ_P (newdecl) = 1;
3158 : 343222 : else if (DECL_READ_P (newdecl))
3159 : 0 : DECL_READ_P (olddecl) = 1;
3160 : : }
3161 : :
3162 : 14049026 : if (DECL_PRESERVE_P (olddecl))
3163 : 48 : DECL_PRESERVE_P (newdecl) = 1;
3164 : 14048978 : else if (DECL_PRESERVE_P (newdecl))
3165 : 63 : DECL_PRESERVE_P (olddecl) = 1;
3166 : :
3167 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3168 : : to olddecl and deleted. */
3169 : 14049026 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3170 : 27700257 : && DECL_FUNCTION_VERSIONED (olddecl))
3171 : : {
3172 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3173 : 258 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3174 : : /* newdecl will be purged after copying to olddecl and is no longer
3175 : : a version. */
3176 : 258 : cgraph_node::delete_function_version_by_decl (newdecl);
3177 : : }
3178 : :
3179 : 14049026 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3180 : : {
3181 : 13651231 : int function_size;
3182 : 13651231 : struct symtab_node *snode = symtab_node::get (olddecl);
3183 : :
3184 : 13651231 : function_size = sizeof (struct tree_decl_common);
3185 : :
3186 : 13651231 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3187 : : (char *) newdecl + sizeof (struct tree_common),
3188 : : function_size - sizeof (struct tree_common));
3189 : :
3190 : 13651231 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3191 : : (char *) newdecl + sizeof (struct tree_decl_common),
3192 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3193 : :
3194 : : /* Preserve symtab node mapping. */
3195 : 13651231 : olddecl->decl_with_vis.symtab_node = snode;
3196 : :
3197 : 13651231 : if (new_template_info)
3198 : : /* If newdecl is a template instantiation, it is possible that
3199 : : the following sequence of events has occurred:
3200 : :
3201 : : o A friend function was declared in a class template. The
3202 : : class template was instantiated.
3203 : :
3204 : : o The instantiation of the friend declaration was
3205 : : recorded on the instantiation list, and is newdecl.
3206 : :
3207 : : o Later, however, instantiate_class_template called pushdecl
3208 : : on the newdecl to perform name injection. But, pushdecl in
3209 : : turn called duplicate_decls when it discovered that another
3210 : : declaration of a global function with the same name already
3211 : : existed.
3212 : :
3213 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3214 : :
3215 : : If we're going to do that, we'd better make sure that
3216 : : olddecl, and not newdecl, is on the list of
3217 : : instantiations so that if we try to do the instantiation
3218 : : again we won't get the clobbered declaration. */
3219 : 575437 : reregister_specialization (newdecl,
3220 : : new_template_info,
3221 : : olddecl);
3222 : : }
3223 : : else
3224 : : {
3225 : 397795 : size_t size = tree_code_size (TREE_CODE (newdecl));
3226 : :
3227 : 397795 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3228 : : (char *) newdecl + sizeof (struct tree_common),
3229 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3230 : :
3231 : 397795 : switch (TREE_CODE (newdecl))
3232 : : {
3233 : 397795 : case LABEL_DECL:
3234 : 397795 : case VAR_DECL:
3235 : 397795 : case RESULT_DECL:
3236 : 397795 : case PARM_DECL:
3237 : 397795 : case FIELD_DECL:
3238 : 397795 : case TYPE_DECL:
3239 : 397795 : case CONST_DECL:
3240 : 397795 : {
3241 : 397795 : struct symtab_node *snode = NULL;
3242 : :
3243 : 397795 : if (VAR_P (newdecl)
3244 : 397795 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3245 : 3 : || DECL_EXTERNAL (olddecl)))
3246 : 376489 : snode = symtab_node::get (olddecl);
3247 : 397795 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3248 : : (char *) newdecl + sizeof (struct tree_decl_common),
3249 : : size - sizeof (struct tree_decl_common)
3250 : 397795 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3251 : 397795 : if (VAR_P (newdecl))
3252 : 376489 : olddecl->decl_with_vis.symtab_node = snode;
3253 : : }
3254 : : break;
3255 : 0 : default:
3256 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3257 : : (char *) newdecl + sizeof (struct tree_decl_common),
3258 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3259 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3260 : 0 : break;
3261 : : }
3262 : : }
3263 : :
3264 : 14049026 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3265 : : {
3266 : 14027720 : if (DECL_EXTERNAL (olddecl)
3267 : : || TREE_PUBLIC (olddecl)
3268 : 14027720 : || TREE_STATIC (olddecl))
3269 : : {
3270 : : /* Merge the section attribute.
3271 : : We want to issue an error if the sections conflict but that must be
3272 : : done later in decl_attributes since we are called before attributes
3273 : : are assigned. */
3274 : 14027720 : if (DECL_SECTION_NAME (newdecl) != NULL)
3275 : 7 : set_decl_section_name (olddecl, newdecl);
3276 : :
3277 : 14027720 : if (DECL_ONE_ONLY (newdecl))
3278 : : {
3279 : 0 : struct symtab_node *oldsym, *newsym;
3280 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3281 : 0 : oldsym = cgraph_node::get_create (olddecl);
3282 : : else
3283 : 0 : oldsym = varpool_node::get_create (olddecl);
3284 : 0 : newsym = symtab_node::get (newdecl);
3285 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3286 : : }
3287 : : }
3288 : :
3289 : 14027720 : if (VAR_P (newdecl)
3290 : 14027720 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3291 : : {
3292 : 383 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3293 : 383 : if (!processing_template_decl)
3294 : 368 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3295 : : }
3296 : : }
3297 : :
3298 : 14049026 : DECL_UID (olddecl) = olddecl_uid;
3299 : :
3300 : : /* NEWDECL contains the merged attribute lists.
3301 : : Update OLDDECL to be the same. */
3302 : 14049026 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3303 : :
3304 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3305 : : so that encode_section_info has a chance to look at the new decl
3306 : : flags and attributes. */
3307 : 14049026 : if (DECL_RTL_SET_P (olddecl)
3308 : 14049026 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3309 : 0 : || (VAR_P (olddecl)
3310 : 0 : && TREE_STATIC (olddecl))))
3311 : 0 : make_decl_rtl (olddecl);
3312 : :
3313 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3314 : : declaration of a member results in a call to duplicate_decls,
3315 : : freeing these nodes represents in a significant savings.
3316 : :
3317 : : Before releasing the node, be sore to remove function from symbol
3318 : : table that might have been inserted there to record comdat group.
3319 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3320 : : structure is shared in between newdecl and oldecl. */
3321 : 14049026 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3322 : 13651231 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3323 : 14049026 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3324 : : {
3325 : 14027720 : struct symtab_node *snode = symtab_node::get (newdecl);
3326 : 14027720 : if (snode)
3327 : 390 : snode->remove ();
3328 : : }
3329 : :
3330 : 14049026 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3331 : : {
3332 : 13651231 : tree clone;
3333 : 14153308 : FOR_EACH_CLONE (clone, olddecl)
3334 : : {
3335 : 502077 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3336 : 502077 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3337 : : }
3338 : : }
3339 : :
3340 : : /* Remove the associated constraints for newdecl, if any, before
3341 : : reclaiming memory. */
3342 : 14049026 : if (flag_concepts)
3343 : 3023666 : remove_constraints (newdecl);
3344 : :
3345 : : /* And similarly for any module tracking data. */
3346 : 14049026 : if (modules_p ())
3347 : 50143 : remove_defining_module (newdecl);
3348 : :
3349 : 14049026 : ggc_free (newdecl);
3350 : :
3351 : 14049026 : return olddecl;
3352 : : }
3353 : :
3354 : : /* Return zero if the declaration NEWDECL is valid
3355 : : when the declaration OLDDECL (assumed to be for the same name)
3356 : : has already been seen.
3357 : : Otherwise return an error message format string with a %s
3358 : : where the identifier should go. */
3359 : :
3360 : : static const char *
3361 : 13812778 : redeclaration_error_message (tree newdecl, tree olddecl)
3362 : : {
3363 : 13812811 : if (TREE_CODE (newdecl) == TYPE_DECL)
3364 : : {
3365 : : /* Because C++ can put things into name space for free,
3366 : : constructs like "typedef struct foo { ... } foo"
3367 : : would look like an erroneous redeclaration. */
3368 : 50797 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3369 : : return NULL;
3370 : : else
3371 : : return G_("redefinition of %q#D");
3372 : : }
3373 : 13762014 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3374 : : {
3375 : : /* If this is a pure function, its olddecl will actually be
3376 : : the original initialization to `0' (which we force to call
3377 : : abort()). Don't complain about redefinition in this case. */
3378 : 19994230 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3379 : 9997133 : && DECL_INITIAL (olddecl) == NULL_TREE)
3380 : : return NULL;
3381 : :
3382 : : /* If both functions come from different namespaces, this is not
3383 : : a redeclaration - this is a conflict with a used function. */
3384 : 19994200 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3385 : 1541044 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3386 : 9997103 : && ! decls_match (olddecl, newdecl))
3387 : : return G_("%qD conflicts with used function");
3388 : :
3389 : : /* We'll complain about linkage mismatches in
3390 : : warn_extern_redeclared_static. */
3391 : :
3392 : : /* Defining the same name twice is no good. */
3393 : 9997100 : if (decl_defined_p (olddecl)
3394 : 9997100 : && decl_defined_p (newdecl))
3395 : : {
3396 : 303 : if (DECL_NAME (olddecl) == NULL_TREE)
3397 : : return G_("%q#D not declared in class");
3398 : 432 : else if (!GNU_INLINE_P (olddecl)
3399 : 387 : || GNU_INLINE_P (newdecl))
3400 : 222 : return G_("redefinition of %q#D");
3401 : : }
3402 : :
3403 : 10631071 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3404 : : {
3405 : 588638 : bool olda = GNU_INLINE_P (olddecl);
3406 : 588638 : bool newa = GNU_INLINE_P (newdecl);
3407 : :
3408 : 588638 : if (olda != newa)
3409 : : {
3410 : 15 : if (newa)
3411 : : return G_("%q+D redeclared inline with "
3412 : : "%<gnu_inline%> attribute");
3413 : : else
3414 : : return G_("%q+D redeclared inline without "
3415 : : "%<gnu_inline%> attribute");
3416 : : }
3417 : : }
3418 : :
3419 : : /* [class.compare.default]: A definition of a comparison operator as
3420 : : defaulted that appears in a class shall be the first declaration of
3421 : : that function. */
3422 : 9996863 : special_function_kind sfk = special_function_p (olddecl);
3423 : 9996863 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3424 : : return G_("comparison operator %q+D defaulted after "
3425 : : "its first declaration");
3426 : :
3427 : 9996860 : check_abi_tag_redeclaration
3428 : 9996860 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3429 : 9996860 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3430 : :
3431 : 9996860 : return NULL;
3432 : : }
3433 : 3764899 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3434 : : {
3435 : 3388148 : tree nt, ot;
3436 : :
3437 : 3388148 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3438 : : return G_("redefinition of %q#D");
3439 : :
3440 : 3388142 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3441 : 33 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3442 : 33 : DECL_TEMPLATE_RESULT (olddecl));
3443 : :
3444 : 3388109 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3445 : : return NULL;
3446 : :
3447 : 3388097 : nt = DECL_TEMPLATE_RESULT (newdecl);
3448 : 3388097 : if (DECL_TEMPLATE_INFO (nt))
3449 : 446698 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3450 : 3388097 : ot = DECL_TEMPLATE_RESULT (olddecl);
3451 : 3388097 : if (DECL_TEMPLATE_INFO (ot))
3452 : 3388097 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3453 : 6010250 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3454 : 3388136 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3455 : 15 : return G_("redefinition of %q#D");
3456 : :
3457 : 4537947 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3458 : : {
3459 : 711670 : bool olda = GNU_INLINE_P (ot);
3460 : 711670 : bool newa = GNU_INLINE_P (nt);
3461 : :
3462 : 711670 : if (olda != newa)
3463 : : {
3464 : 0 : if (newa)
3465 : : return G_("%q+D redeclared inline with "
3466 : : "%<gnu_inline%> attribute");
3467 : : else
3468 : : return G_("%q+D redeclared inline without "
3469 : : "%<gnu_inline%> attribute");
3470 : : }
3471 : : }
3472 : :
3473 : : /* Core issue #226 (C++11):
3474 : :
3475 : : If a friend function template declaration specifies a
3476 : : default template-argument, that declaration shall be a
3477 : : definition and shall be the only declaration of the
3478 : : function template in the translation unit. */
3479 : 3388082 : if ((cxx_dialect != cxx98)
3480 : 3369476 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3481 : 3596799 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3482 : : /*is_primary=*/true,
3483 : : /*is_partial=*/false,
3484 : : /*is_friend_decl=*/2))
3485 : : return G_("redeclaration of friend %q#D "
3486 : : "may not have default template arguments");
3487 : :
3488 : 3388079 : return NULL;
3489 : : }
3490 : 376751 : else if (VAR_P (newdecl)
3491 : 376667 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3492 : 376667 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3493 : 376788 : && (! DECL_LANG_SPECIFIC (olddecl)
3494 : 34 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3495 : 31 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3496 : : {
3497 : : /* Only variables can be thread-local, and all declarations must
3498 : : agree on this property. */
3499 : 6 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3500 : : return G_("thread-local declaration of %q#D follows "
3501 : : "non-thread-local declaration");
3502 : : else
3503 : 3 : return G_("non-thread-local declaration of %q#D follows "
3504 : : "thread-local declaration");
3505 : : }
3506 : 376745 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3507 : : {
3508 : : /* The objects have been declared at namespace scope. If either
3509 : : is a member of an anonymous union, then this is an invalid
3510 : : redeclaration. For example:
3511 : :
3512 : : int i;
3513 : : union { int i; };
3514 : :
3515 : : is invalid. */
3516 : 376595 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3517 : 753259 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3518 : : return G_("redeclaration of %q#D");
3519 : : /* If at least one declaration is a reference, there is no
3520 : : conflict. For example:
3521 : :
3522 : : int i = 3;
3523 : : extern int i;
3524 : :
3525 : : is valid. */
3526 : 376661 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3527 : : return NULL;
3528 : :
3529 : : /* Static data member declared outside a class definition
3530 : : if the variable is defined within the class with constexpr
3531 : : specifier is declaration rather than definition (and
3532 : : deprecated). */
3533 : 2804 : if (cxx_dialect >= cxx17
3534 : 2735 : && VAR_P (olddecl)
3535 : 2679 : && DECL_CLASS_SCOPE_P (olddecl)
3536 : 2647 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3537 : 5439 : && !DECL_INITIAL (newdecl))
3538 : : {
3539 : 2635 : DECL_EXTERNAL (newdecl) = 1;
3540 : : /* For now, only warn with explicit -Wdeprecated. */
3541 : 2635 : if (OPTION_SET_P (warn_deprecated))
3542 : : {
3543 : 40 : auto_diagnostic_group d;
3544 : 40 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3545 : : "redundant redeclaration of %<constexpr%> "
3546 : : "static data member %qD", newdecl))
3547 : 4 : inform (DECL_SOURCE_LOCATION (olddecl),
3548 : : "previous declaration of %qD", olddecl);
3549 : 40 : }
3550 : 2635 : return NULL;
3551 : : }
3552 : :
3553 : : /* Reject two definitions. */
3554 : : return G_("redefinition of %q#D");
3555 : : }
3556 : : else
3557 : : {
3558 : : /* Objects declared with block scope: */
3559 : : /* Reject two definitions, and reject a definition
3560 : : together with an external reference. */
3561 : 69 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3562 : : return G_("redeclaration of %q#D");
3563 : : return NULL;
3564 : : }
3565 : : }
3566 : :
3567 : :
3568 : : /* Hash and equality functions for the named_label table. */
3569 : :
3570 : : hashval_t
3571 : 57750 : named_label_hash::hash (const value_type entry)
3572 : : {
3573 : 57750 : return IDENTIFIER_HASH_VALUE (entry->name);
3574 : : }
3575 : :
3576 : : bool
3577 : 56677 : named_label_hash::equal (const value_type entry, compare_type name)
3578 : : {
3579 : 56677 : return name == entry->name;
3580 : : }
3581 : :
3582 : : /* Look for a label named ID in the current function. If one cannot
3583 : : be found, create one. Return the named_label_entry, or NULL on
3584 : : failure. */
3585 : :
3586 : : static named_label_entry *
3587 : 18778 : lookup_label_1 (tree id, bool making_local_p)
3588 : : {
3589 : 18778 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3590 : :
3591 : : /* You can't use labels at global scope. */
3592 : 18778 : if (current_function_decl == NULL_TREE)
3593 : : {
3594 : 3 : error ("label %qE referenced outside of any function", id);
3595 : 3 : return NULL;
3596 : : }
3597 : :
3598 : 18775 : if (!named_labels)
3599 : 3783 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3600 : :
3601 : 18775 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3602 : 18775 : named_label_entry **slot
3603 : 18775 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3604 : 18775 : named_label_entry *old = *slot;
3605 : :
3606 : 18775 : if (old && old->label_decl)
3607 : : {
3608 : 2121 : if (!making_local_p)
3609 : : return old;
3610 : :
3611 : 9 : if (old->binding_level == current_binding_level)
3612 : : {
3613 : 0 : auto_diagnostic_group d;
3614 : 0 : error ("local label %qE conflicts with existing label", id);
3615 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3616 : 0 : return NULL;
3617 : 0 : }
3618 : : }
3619 : :
3620 : : /* We are making a new decl, create or reuse the named_label_entry */
3621 : 16672 : named_label_entry *ent = NULL;
3622 : 21 : if (old && !old->label_decl)
3623 : : ent = old;
3624 : : else
3625 : : {
3626 : 16651 : ent = ggc_cleared_alloc<named_label_entry> ();
3627 : 16651 : ent->name = id;
3628 : 16651 : ent->outer = old;
3629 : 16651 : *slot = ent;
3630 : : }
3631 : :
3632 : : /* Now create the LABEL_DECL. */
3633 : 16663 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3634 : :
3635 : 16663 : DECL_CONTEXT (decl) = current_function_decl;
3636 : 16663 : SET_DECL_MODE (decl, VOIDmode);
3637 : 16663 : if (making_local_p)
3638 : : {
3639 : 195 : C_DECLARED_LABEL_FLAG (decl) = true;
3640 : 195 : DECL_CHAIN (decl) = current_binding_level->names;
3641 : 195 : current_binding_level->names = decl;
3642 : : }
3643 : :
3644 : 16663 : ent->label_decl = decl;
3645 : :
3646 : 16663 : return ent;
3647 : 18778 : }
3648 : :
3649 : : /* Wrapper for lookup_label_1. */
3650 : :
3651 : : tree
3652 : 1706 : lookup_label (tree id)
3653 : : {
3654 : 1706 : named_label_entry *ent = lookup_label_1 (id, false);
3655 : 1706 : return ent ? ent->label_decl : NULL_TREE;
3656 : : }
3657 : :
3658 : : /* Remember that we've seen &&ID. */
3659 : :
3660 : : void
3661 : 217 : mark_label_addressed (tree id)
3662 : : {
3663 : 217 : named_label_entry *ent = lookup_label_1 (id, false);
3664 : 217 : ent->addressed = true;
3665 : 217 : }
3666 : :
3667 : : tree
3668 : 195 : declare_local_label (tree id)
3669 : : {
3670 : 195 : named_label_entry *ent = lookup_label_1 (id, true);
3671 : 195 : return ent ? ent->label_decl : NULL_TREE;
3672 : : }
3673 : :
3674 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3675 : :
3676 : : static bool
3677 : 12849 : decl_jump_unsafe (tree decl)
3678 : : {
3679 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3680 : : with automatic storage duration is not in scope to a point where it is
3681 : : in scope is ill-formed unless the variable has scalar type, class type
3682 : : with a trivial default constructor and a trivial destructor, a
3683 : : cv-qualified version of one of these types, or an array of one of the
3684 : : preceding types and is declared without an initializer (8.5). */
3685 : 12849 : tree type = TREE_TYPE (decl);
3686 : :
3687 : 12849 : return (type != error_mark_node
3688 : : && VAR_P (decl)
3689 : 12831 : && !TREE_STATIC (decl)
3690 : 24559 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3691 : 10686 : || variably_modified_type_p (type, NULL_TREE)));
3692 : : }
3693 : :
3694 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3695 : : to the user. */
3696 : :
3697 : : static bool
3698 : 268 : identify_goto (tree decl, location_t loc, const location_t *locus,
3699 : : diagnostic_t diag_kind, bool computed)
3700 : : {
3701 : 268 : if (computed)
3702 : 25 : diag_kind = DK_WARNING;
3703 : 268 : bool complained
3704 : 355 : = emit_diagnostic (diag_kind, loc, 0,
3705 : : decl ? G_("jump to label %qD")
3706 : : : G_("jump to case label"), decl);
3707 : 268 : if (complained && locus)
3708 : : {
3709 : 181 : if (computed)
3710 : 25 : inform (*locus, " as a possible target of computed goto");
3711 : : else
3712 : 156 : inform (*locus, " from here");
3713 : : }
3714 : 268 : return complained;
3715 : : }
3716 : :
3717 : : /* Check that a single previously seen jump to a newly defined label
3718 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3719 : : the jump context; NAMES are the names in scope in LEVEL at the jump
3720 : : context; LOCUS is the source position of the jump or 0. COMPUTED
3721 : : is a vec of decls if the jump is a computed goto. Returns
3722 : : true if all is well. */
3723 : :
3724 : : static bool
3725 : 1665053 : check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3726 : : bool exited_omp, const location_t *locus,
3727 : : vec<tree,va_gc> *computed)
3728 : : {
3729 : 1665053 : auto_diagnostic_group d;
3730 : 1665053 : cp_binding_level *b;
3731 : 1665053 : bool complained = false;
3732 : 1665053 : int identified = 0;
3733 : 1665053 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3734 : 1665053 : bool saw_ceif = false, saw_se = false;
3735 : :
3736 : 1665053 : if (exited_omp)
3737 : : {
3738 : 12 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3739 : : computed);
3740 : 12 : if (complained)
3741 : 12 : inform (input_location, " exits OpenMP structured block");
3742 : : saw_omp = true;
3743 : : identified = 2;
3744 : : }
3745 : :
3746 : 3306789 : for (b = current_binding_level; b ; b = b->level_chain)
3747 : : {
3748 : 3306789 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3749 : :
3750 : 3316763 : for (new_decls = b->names; new_decls != old_decls;
3751 : 10367 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3752 : 393 : : TREE_CHAIN (new_decls)))
3753 : : {
3754 : 9974 : bool problem = decl_jump_unsafe (new_decls);
3755 : 9974 : if (! problem)
3756 : 9929 : continue;
3757 : :
3758 : 45 : if (!identified)
3759 : : {
3760 : 45 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3761 : : computed);
3762 : 45 : identified = 2;
3763 : : }
3764 : 45 : if (complained)
3765 : 45 : inform (DECL_SOURCE_LOCATION (new_decls),
3766 : : " crosses initialization of %q#D", new_decls);
3767 : : }
3768 : :
3769 : 3306789 : if (b == level)
3770 : : break;
3771 : :
3772 : 1641736 : const char *inf = NULL;
3773 : 1641736 : location_t loc = input_location;
3774 : 1641736 : switch (b->kind)
3775 : : {
3776 : 3 : case sk_try:
3777 : 3 : if (!saw_eh)
3778 : 1641736 : inf = G_(" enters %<try%> block");
3779 : : saw_eh = true;
3780 : : break;
3781 : :
3782 : 9 : case sk_catch:
3783 : 9 : if (!saw_eh)
3784 : 1641736 : inf = G_(" enters %<catch%> block");
3785 : : saw_eh = true;
3786 : : break;
3787 : :
3788 : 102 : case sk_omp:
3789 : 102 : if (!saw_omp)
3790 : 81 : inf = G_(" enters OpenMP structured block");
3791 : : saw_omp = true;
3792 : : break;
3793 : :
3794 : 3 : case sk_transaction:
3795 : 3 : if (!saw_tm)
3796 : 3 : inf = G_(" enters synchronized or atomic statement");
3797 : : saw_tm = true;
3798 : : break;
3799 : :
3800 : 12 : case sk_stmt_expr:
3801 : 12 : if (!saw_se)
3802 : 12 : inf = G_(" enters statement expression");
3803 : : saw_se = true;
3804 : : break;
3805 : :
3806 : 1641503 : case sk_block:
3807 : 1641503 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3808 : : {
3809 : 9 : inf = G_(" enters %<constexpr if%> statement");
3810 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3811 : : saw_cxif = true;
3812 : : }
3813 : 1641494 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3814 : : {
3815 : 18 : inf = G_(" enters %<consteval if%> statement");
3816 : 18 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3817 : : saw_ceif = true;
3818 : : }
3819 : : break;
3820 : :
3821 : : default:
3822 : : break;
3823 : : }
3824 : :
3825 : 1641736 : if (inf)
3826 : : {
3827 : 135 : if (identified < 2)
3828 : 135 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3829 : : computed);
3830 : 135 : identified = 2;
3831 : 135 : if (complained)
3832 : 135 : inform (loc, inf);
3833 : : }
3834 : : }
3835 : :
3836 : 1665053 : if (!vec_safe_is_empty (computed))
3837 : : {
3838 : 9 : if (!identified)
3839 : 6 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3840 : : computed);
3841 : 9 : identified = 2;
3842 : 9 : if (complained)
3843 : 18 : for (tree d : computed)
3844 : : {
3845 : 9 : if (DECL_P (d))
3846 : 6 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3847 : 3 : else if (d == get_identifier ("catch"))
3848 : 3 : inform (*locus, " does not clean up handled exception");
3849 : : }
3850 : : }
3851 : :
3852 : 3330106 : return !identified;
3853 : 1665053 : }
3854 : :
3855 : : static void
3856 : 1057 : check_previous_goto (tree decl, struct named_label_use_entry *use)
3857 : : {
3858 : 1057 : check_previous_goto_1 (decl, use->binding_level,
3859 : 1057 : use->names_in_scope, use->in_omp_scope,
3860 : 1057 : &use->o_goto_locus, use->computed_goto);
3861 : 1057 : }
3862 : :
3863 : : static bool
3864 : 1663996 : check_switch_goto (cp_binding_level* level)
3865 : : {
3866 : 1663996 : return check_previous_goto_1 (NULL_TREE, level, level->names,
3867 : 1663996 : false, NULL, nullptr);
3868 : : }
3869 : :
3870 : : /* Check that a new jump to a label ENT is OK. COMPUTED is true
3871 : : if this is a possible target of a computed goto. */
3872 : :
3873 : : void
3874 : 1599 : check_goto_1 (named_label_entry *ent, bool computed)
3875 : : {
3876 : 1599 : auto_diagnostic_group d;
3877 : 1599 : tree decl = ent->label_decl;
3878 : :
3879 : : /* If the label hasn't been defined yet, defer checking. */
3880 : 1599 : if (! DECL_INITIAL (decl))
3881 : : {
3882 : : /* Don't bother creating another use if the last goto had the
3883 : : same data, and will therefore create the same set of errors. */
3884 : 1210 : if (ent->uses
3885 : 240 : && ent->uses->names_in_scope == current_binding_level->names)
3886 : : return;
3887 : :
3888 : 1066 : named_label_use_entry *new_use
3889 : 1066 : = ggc_alloc<named_label_use_entry> ();
3890 : 1066 : new_use->binding_level = current_binding_level;
3891 : 1066 : new_use->names_in_scope = current_binding_level->names;
3892 : 1066 : new_use->o_goto_locus = input_location;
3893 : 1066 : new_use->in_omp_scope = false;
3894 : 1066 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3895 : :
3896 : 1066 : new_use->next = ent->uses;
3897 : 1066 : ent->uses = new_use;
3898 : 1066 : return;
3899 : : }
3900 : :
3901 : 389 : bool saw_catch = false, complained = false;
3902 : 389 : int identified = 0;
3903 : 389 : tree bad;
3904 : 389 : unsigned ix;
3905 : :
3906 : 380 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3907 : 374 : || ent->in_constexpr_if || ent->in_consteval_if
3908 : 368 : || ent->in_omp_scope || ent->in_stmt_expr
3909 : 751 : || !vec_safe_is_empty (ent->bad_decls))
3910 : : {
3911 : 33 : diagnostic_t diag_kind = DK_PERMERROR;
3912 : 33 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3913 : 18 : || ent->in_consteval_if || ent->in_transaction_scope
3914 : 12 : || ent->in_omp_scope || ent->in_stmt_expr)
3915 : 27 : diag_kind = DK_ERROR;
3916 : 33 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3917 : : &input_location, diag_kind, computed);
3918 : 33 : identified = 1 + (diag_kind == DK_ERROR);
3919 : : }
3920 : :
3921 : 395 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3922 : : {
3923 : 6 : bool problem = decl_jump_unsafe (bad);
3924 : :
3925 : 6 : if (problem && DECL_ARTIFICIAL (bad))
3926 : : {
3927 : : /* Can't skip init of __exception_info. */
3928 : 0 : if (identified == 1)
3929 : : {
3930 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3931 : : &input_location, DK_ERROR, computed);
3932 : 0 : identified = 2;
3933 : : }
3934 : 0 : if (complained)
3935 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3936 : : saw_catch = true;
3937 : : }
3938 : 6 : else if (complained)
3939 : 6 : inform (DECL_SOURCE_LOCATION (bad),
3940 : : " skips initialization of %q#D", bad);
3941 : : }
3942 : :
3943 : 389 : if (complained)
3944 : : {
3945 : 33 : if (ent->in_try_scope)
3946 : 9 : inform (input_location, " enters %<try%> block");
3947 : 24 : else if (ent->in_catch_scope && !saw_catch)
3948 : 3 : inform (input_location, " enters %<catch%> block");
3949 : 21 : else if (ent->in_transaction_scope)
3950 : 3 : inform (input_location, " enters synchronized or atomic statement");
3951 : 18 : else if (ent->in_constexpr_if)
3952 : 3 : inform (input_location, " enters %<constexpr if%> statement");
3953 : 15 : else if (ent->in_consteval_if)
3954 : 3 : inform (input_location, " enters %<consteval if%> statement");
3955 : 12 : else if (ent->in_stmt_expr)
3956 : 3 : inform (input_location, " enters statement expression");
3957 : : }
3958 : :
3959 : 389 : if (ent->in_omp_scope)
3960 : : {
3961 : 3 : if (complained)
3962 : 3 : inform (input_location, " enters OpenMP structured block");
3963 : : }
3964 : 386 : else if (flag_openmp)
3965 : 42 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3966 : : {
3967 : 42 : if (b == ent->binding_level)
3968 : : break;
3969 : 36 : if (b->kind == sk_omp)
3970 : : {
3971 : 21 : if (identified < 2)
3972 : : {
3973 : 42 : complained = identify_goto (decl,
3974 : 21 : DECL_SOURCE_LOCATION (decl),
3975 : : &input_location, DK_ERROR,
3976 : : computed);
3977 : 21 : identified = 2;
3978 : : }
3979 : 21 : if (complained)
3980 : 21 : inform (input_location, " exits OpenMP structured block");
3981 : : break;
3982 : : }
3983 : : }
3984 : :
3985 : : /* Warn if a computed goto might involve a local variable going out of scope
3986 : : without being cleaned up. */
3987 : 389 : if (computed)
3988 : : {
3989 : 59 : auto level = ent->binding_level;
3990 : 59 : auto names = ent->names_in_scope;
3991 : 59 : for (auto b = current_binding_level; ; b = b->level_chain)
3992 : : {
3993 : 99 : if (b->kind == sk_catch)
3994 : : {
3995 : 6 : if (!identified)
3996 : : {
3997 : 6 : complained
3998 : 6 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3999 : : &input_location, DK_ERROR, computed);
4000 : 6 : identified = 2;
4001 : : }
4002 : 6 : if (complained)
4003 : 6 : inform (input_location,
4004 : : " does not clean up handled exception");
4005 : : }
4006 : 99 : tree end = b == level ? names : NULL_TREE;
4007 : 115 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
4008 : : {
4009 : 16 : if (automatic_var_with_nontrivial_dtor_p (d))
4010 : : {
4011 : 10 : if (!identified)
4012 : : {
4013 : 10 : complained
4014 : 10 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
4015 : : &input_location, DK_ERROR, computed);
4016 : 10 : identified = 2;
4017 : : }
4018 : 10 : if (complained)
4019 : 10 : inform (DECL_SOURCE_LOCATION (d),
4020 : : " does not destroy %qD", d);
4021 : : }
4022 : : }
4023 : 99 : if (b == level)
4024 : : break;
4025 : 40 : }
4026 : : }
4027 : 1599 : }
4028 : :
4029 : : /* Check that a new jump to a label DECL is OK. Called by
4030 : : finish_goto_stmt. */
4031 : :
4032 : : void
4033 : 1565 : check_goto (tree decl)
4034 : : {
4035 : 1565 : if (!named_labels)
4036 : : return;
4037 : 1550 : if (TREE_CODE (decl) != LABEL_DECL)
4038 : : {
4039 : : /* We don't know where a computed goto is jumping,
4040 : : so check all addressable labels. */
4041 : 276 : for (auto iter = named_labels->begin ();
4042 : 374 : iter != named_labels->end ();
4043 : 178 : ++iter)
4044 : : {
4045 : 178 : auto ent = *iter;
4046 : 178 : if (ent->addressed)
4047 : 147 : check_goto_1 (ent, true);
4048 : : }
4049 : : }
4050 : : else
4051 : : {
4052 : 1452 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4053 : 1452 : named_label_entry **slot
4054 : 1452 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4055 : 1452 : named_label_entry *ent = *slot;
4056 : 1452 : check_goto_1 (ent, false);
4057 : : }
4058 : : }
4059 : :
4060 : : /* Check that a return is ok wrt OpenMP structured blocks.
4061 : : Called by finish_return_stmt. Returns true if all is well. */
4062 : :
4063 : : bool
4064 : 88865 : check_omp_return (void)
4065 : : {
4066 : 268842 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4067 : 268842 : if (b->kind == sk_omp)
4068 : : {
4069 : 15 : error ("invalid exit from OpenMP structured block");
4070 : 15 : return false;
4071 : : }
4072 : 268827 : else if (b->kind == sk_function_parms)
4073 : : break;
4074 : : return true;
4075 : : }
4076 : :
4077 : : /* Define a label, specifying the location in the source file.
4078 : : Return the LABEL_DECL node for the label. */
4079 : :
4080 : : tree
4081 : 16660 : define_label (location_t location, tree name)
4082 : : {
4083 : 16660 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4084 : :
4085 : : /* After labels, make any new cleanups in the function go into their
4086 : : own new (temporary) binding contour. */
4087 : 16660 : for (cp_binding_level *p = current_binding_level;
4088 : 23190 : p->kind != sk_function_parms;
4089 : 6530 : p = p->level_chain)
4090 : 6530 : p->more_cleanups_ok = 0;
4091 : :
4092 : 16660 : named_label_entry *ent = lookup_label_1 (name, false);
4093 : 16660 : tree decl = ent->label_decl;
4094 : :
4095 : 16660 : if (DECL_INITIAL (decl) != NULL_TREE)
4096 : : {
4097 : 6 : error ("duplicate label %qD", decl);
4098 : 6 : return error_mark_node;
4099 : : }
4100 : : else
4101 : : {
4102 : : /* Mark label as having been defined. */
4103 : 16654 : DECL_INITIAL (decl) = error_mark_node;
4104 : : /* Say where in the source. */
4105 : 16654 : DECL_SOURCE_LOCATION (decl) = location;
4106 : :
4107 : 16654 : ent->binding_level = current_binding_level;
4108 : 16654 : ent->names_in_scope = current_binding_level->names;
4109 : :
4110 : 17711 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4111 : 1057 : check_previous_goto (decl, use);
4112 : 16654 : ent->uses = NULL;
4113 : : }
4114 : :
4115 : 16654 : return decl;
4116 : 16660 : }
4117 : :
4118 : : struct cp_switch
4119 : : {
4120 : : cp_binding_level *level;
4121 : : struct cp_switch *next;
4122 : : /* The SWITCH_STMT being built. */
4123 : : tree switch_stmt;
4124 : : /* A splay-tree mapping the low element of a case range to the high
4125 : : element, or NULL_TREE if there is no high element. Used to
4126 : : determine whether or not a new case label duplicates an old case
4127 : : label. We need a tree, rather than simply a hash table, because
4128 : : of the GNU case range extension. */
4129 : : splay_tree cases;
4130 : : /* Remember whether a default: case label has been seen. */
4131 : : bool has_default_p;
4132 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4133 : : bool break_stmt_seen_p;
4134 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4135 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4136 : : bool in_loop_body_p;
4137 : : };
4138 : :
4139 : : /* A stack of the currently active switch statements. The innermost
4140 : : switch statement is on the top of the stack. There is no need to
4141 : : mark the stack for garbage collection because it is only active
4142 : : during the processing of the body of a function, and we never
4143 : : collect at that point. */
4144 : :
4145 : : static struct cp_switch *switch_stack;
4146 : :
4147 : : /* Called right after a switch-statement condition is parsed.
4148 : : SWITCH_STMT is the switch statement being parsed. */
4149 : :
4150 : : void
4151 : 417566 : push_switch (tree switch_stmt)
4152 : : {
4153 : 417566 : struct cp_switch *p = XNEW (struct cp_switch);
4154 : 417566 : p->level = current_binding_level;
4155 : 417566 : p->next = switch_stack;
4156 : 417566 : p->switch_stmt = switch_stmt;
4157 : 417566 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4158 : 417566 : p->has_default_p = false;
4159 : 417566 : p->break_stmt_seen_p = false;
4160 : 417566 : p->in_loop_body_p = false;
4161 : 417566 : switch_stack = p;
4162 : 417566 : }
4163 : :
4164 : : void
4165 : 417566 : pop_switch (void)
4166 : : {
4167 : 417566 : struct cp_switch *cs = switch_stack;
4168 : :
4169 : : /* Emit warnings as needed. */
4170 : 417566 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4171 : 417566 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4172 : 417566 : const bool bool_cond_p
4173 : 417566 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4174 : 417566 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4175 : 417566 : if (!processing_template_decl)
4176 : 268109 : c_do_switch_warnings (cs->cases, switch_location,
4177 : 268109 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4178 : : bool_cond_p);
4179 : :
4180 : : /* For the benefit of block_may_fallthru remember if the switch body
4181 : : case labels cover all possible values and if there are break; stmts. */
4182 : 417566 : if (cs->has_default_p
4183 : 417566 : || (!processing_template_decl
4184 : 33100 : && c_switch_covers_all_cases_p (cs->cases,
4185 : 33100 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4186 : 326783 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4187 : 417566 : if (!cs->break_stmt_seen_p)
4188 : 204368 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4189 : : /* Now that we're done with the switch warnings, set the switch type
4190 : : to the type of the condition if the index type was of scoped enum type.
4191 : : (Such types don't participate in the integer promotions.) We do this
4192 : : because of bit-fields whose declared type is a scoped enum type:
4193 : : gimplification will use the lowered index type, but convert the
4194 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4195 : : and verify_gimple_switch doesn't accept that. */
4196 : 417566 : if (is_bitfield_expr_with_lowered_type (cond))
4197 : 13 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4198 : 417566 : gcc_assert (!cs->in_loop_body_p);
4199 : 417566 : splay_tree_delete (cs->cases);
4200 : 417566 : switch_stack = switch_stack->next;
4201 : 417566 : free (cs);
4202 : 417566 : }
4203 : :
4204 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4205 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4206 : : in switch_stack we've seen a BREAK_STMT. */
4207 : :
4208 : : void
4209 : 3308418 : note_break_stmt (void)
4210 : : {
4211 : 3308418 : if (switch_stack && !switch_stack->in_loop_body_p)
4212 : 1377971 : switch_stack->break_stmt_seen_p = true;
4213 : 3308418 : }
4214 : :
4215 : : /* Note the start of processing of an iteration statement's body.
4216 : : The note_break_stmt function will do nothing while processing it.
4217 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4218 : :
4219 : : bool
4220 : 14473703 : note_iteration_stmt_body_start (void)
4221 : : {
4222 : 14473703 : if (!switch_stack)
4223 : : return false;
4224 : 107308 : bool ret = switch_stack->in_loop_body_p;
4225 : 107308 : switch_stack->in_loop_body_p = true;
4226 : 107308 : return ret;
4227 : : }
4228 : :
4229 : : /* Note the end of processing of an iteration statement's body. */
4230 : :
4231 : : void
4232 : 14473703 : note_iteration_stmt_body_end (bool prev)
4233 : : {
4234 : 14473703 : if (switch_stack)
4235 : 107308 : switch_stack->in_loop_body_p = prev;
4236 : 14473703 : }
4237 : :
4238 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4239 : : condition. Note that if TYPE and VALUE are already integral we don't
4240 : : really do the conversion because the language-independent
4241 : : warning/optimization code will work better that way. */
4242 : :
4243 : : static tree
4244 : 3327746 : case_conversion (tree type, tree value)
4245 : : {
4246 : 3327746 : if (value == NULL_TREE)
4247 : : return value;
4248 : :
4249 : 1429026 : value = mark_rvalue_use (value);
4250 : :
4251 : 1429026 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4252 : 1415569 : type = type_promotes_to (type);
4253 : :
4254 : 1429026 : tree ovalue = value;
4255 : : /* The constant-expression VALUE shall be a converted constant expression
4256 : : of the adjusted type of the switch condition, which doesn't allow
4257 : : narrowing conversions. */
4258 : 1429026 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4259 : :
4260 : 1429026 : if (cxx_dialect >= cxx11
4261 : 1429026 : && (SCOPED_ENUM_P (type)
4262 : 1412856 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4263 : : /* Use the converted value. */;
4264 : : else
4265 : : /* The already integral case. */
4266 : : value = ovalue;
4267 : :
4268 : 1429026 : return cxx_constant_value (value);
4269 : : }
4270 : :
4271 : : /* Note that we've seen a definition of a case label, and complain if this
4272 : : is a bad place for one. */
4273 : :
4274 : : tree
4275 : 3081377 : finish_case_label (location_t loc, tree low_value, tree high_value)
4276 : : {
4277 : 3081377 : tree cond, r;
4278 : 3081377 : cp_binding_level *p;
4279 : 3081377 : tree type;
4280 : :
4281 : 3081377 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4282 : 326747 : switch_stack->has_default_p = true;
4283 : :
4284 : 3081377 : if (processing_template_decl)
4285 : : {
4286 : 1417381 : tree label;
4287 : :
4288 : : /* For templates, just add the case label; we'll do semantic
4289 : : analysis at instantiation-time. */
4290 : 1417381 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4291 : 1417381 : return add_stmt (build_case_label (low_value, high_value, label));
4292 : : }
4293 : :
4294 : : /* Find the condition on which this switch statement depends. */
4295 : 1663996 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4296 : 1663996 : if (cond && TREE_CODE (cond) == TREE_LIST)
4297 : 0 : cond = TREE_VALUE (cond);
4298 : :
4299 : 1663996 : if (!check_switch_goto (switch_stack->level))
4300 : 87 : return error_mark_node;
4301 : :
4302 : 1663909 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4303 : 1663909 : if (type == error_mark_node)
4304 : : return error_mark_node;
4305 : :
4306 : 1663873 : low_value = case_conversion (type, low_value);
4307 : 1663873 : high_value = case_conversion (type, high_value);
4308 : :
4309 : 1663873 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4310 : :
4311 : : /* After labels, make any new cleanups in the function go into their
4312 : : own new (temporary) binding contour. */
4313 : 1663873 : for (p = current_binding_level;
4314 : 7012349 : p->kind != sk_function_parms;
4315 : 5348476 : p = p->level_chain)
4316 : 5348476 : p->more_cleanups_ok = 0;
4317 : :
4318 : : return r;
4319 : : }
4320 : :
4321 : : struct typename_info {
4322 : : tree scope;
4323 : : tree name;
4324 : : tree template_id;
4325 : : bool enum_p;
4326 : : bool class_p;
4327 : : };
4328 : :
4329 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4330 : : {
4331 : : typedef typename_info *compare_type;
4332 : :
4333 : : /* Hash a TYPENAME_TYPE. */
4334 : :
4335 : : static hashval_t
4336 : 358707640 : hash (tree context, tree fullname)
4337 : : {
4338 : 358707640 : hashval_t hash = 0;
4339 : 358707640 : hash = iterative_hash_object (context, hash);
4340 : 358707640 : hash = iterative_hash_object (fullname, hash);
4341 : 358707640 : return hash;
4342 : : }
4343 : :
4344 : : static hashval_t
4345 : 51835357 : hash (const typename_info *ti)
4346 : : {
4347 : 51835357 : return typename_hasher::hash (ti->scope, ti->template_id);
4348 : : }
4349 : :
4350 : : static hashval_t
4351 : 306872283 : hash (tree t)
4352 : : {
4353 : 306872283 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4354 : : }
4355 : :
4356 : : /* Compare two TYPENAME_TYPEs. */
4357 : :
4358 : : static bool
4359 : 356067547 : equal (tree t1, const typename_info *t2)
4360 : : {
4361 : 356067547 : return (TYPE_IDENTIFIER (t1) == t2->name
4362 : 106521287 : && TYPE_CONTEXT (t1) == t2->scope
4363 : 26023524 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4364 : 26016210 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4365 : 382083703 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4366 : : }
4367 : : };
4368 : :
4369 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4370 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4371 : :
4372 : : Returns the new TYPENAME_TYPE. */
4373 : :
4374 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4375 : :
4376 : : tree
4377 : 51835357 : build_typename_type (tree context, tree name, tree fullname,
4378 : : enum tag_types tag_type)
4379 : : {
4380 : 51835357 : typename_info ti;
4381 : :
4382 : 51835357 : if (typename_htab == NULL)
4383 : 18384 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4384 : :
4385 : 51835357 : ti.scope = FROB_CONTEXT (context);
4386 : 51835357 : ti.name = name;
4387 : 51835357 : ti.template_id = fullname;
4388 : 51835357 : ti.enum_p = tag_type == enum_type;
4389 : 51835357 : ti.class_p = (tag_type == class_type
4390 : : || tag_type == record_type
4391 : 51835357 : || tag_type == union_type);
4392 : 51835357 : hashval_t hash = typename_hasher::hash (&ti);
4393 : :
4394 : : /* See if we already have this type. */
4395 : 51835357 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4396 : 51835357 : tree t = *e;
4397 : 51835357 : if (*e)
4398 : : t = *e;
4399 : : else
4400 : : {
4401 : : /* Build the TYPENAME_TYPE. */
4402 : 25819225 : t = cxx_make_type (TYPENAME_TYPE);
4403 : 25819225 : TYPE_CONTEXT (t) = ti.scope;
4404 : 25819225 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4405 : 25819225 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4406 : 25819225 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4407 : :
4408 : : /* Build the corresponding TYPE_DECL. */
4409 : 25819225 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4410 : 25819225 : TYPE_NAME (t) = d;
4411 : 25819225 : TYPE_STUB_DECL (t) = d;
4412 : 25819225 : DECL_CONTEXT (d) = ti.scope;
4413 : 25819225 : DECL_ARTIFICIAL (d) = 1;
4414 : :
4415 : : /* Store it in the hash table. */
4416 : 25819225 : *e = t;
4417 : :
4418 : : /* TYPENAME_TYPEs must always be compared structurally, because
4419 : : they may or may not resolve down to another type depending on
4420 : : the currently open classes. */
4421 : 25819225 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4422 : : }
4423 : :
4424 : 51835357 : return t;
4425 : : }
4426 : :
4427 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4428 : : provided to name the type. Returns an appropriate type, unless an
4429 : : error occurs, in which case error_mark_node is returned. If we
4430 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4431 : : return that, rather than the _TYPE it corresponds to, in other
4432 : : cases we look through the type decl. If TF_ERROR is set, complain
4433 : : about errors, otherwise be quiet. */
4434 : :
4435 : : tree
4436 : 161763640 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4437 : : tsubst_flags_t complain)
4438 : : {
4439 : 161763640 : tree fullname;
4440 : 161763640 : tree t;
4441 : 161763640 : bool want_template;
4442 : :
4443 : 161763640 : if (name == error_mark_node
4444 : 161763638 : || context == NULL_TREE
4445 : 161763638 : || context == error_mark_node)
4446 : : return error_mark_node;
4447 : :
4448 : 161763625 : if (TYPE_P (name))
4449 : : {
4450 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4451 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4452 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4453 : 0 : name = TYPE_IDENTIFIER (name);
4454 : : else
4455 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4456 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4457 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4458 : 0 : CLASSTYPE_TI_ARGS (name));
4459 : : }
4460 : 161763625 : else if (TREE_CODE (name) == TYPE_DECL)
4461 : 0 : name = DECL_NAME (name);
4462 : :
4463 : 161763625 : fullname = name;
4464 : :
4465 : 161763625 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4466 : : {
4467 : 4595160 : name = TREE_OPERAND (name, 0);
4468 : 4595160 : if (DECL_TYPE_TEMPLATE_P (name))
4469 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4470 : 4595160 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4471 : : {
4472 : 0 : if (complain & tf_error)
4473 : 0 : error ("%qD is not a type", name);
4474 : 0 : return error_mark_node;
4475 : : }
4476 : : }
4477 : 161763625 : if (TREE_CODE (name) == TEMPLATE_DECL)
4478 : : {
4479 : 0 : if (complain & tf_error)
4480 : 0 : error ("%qD used without template arguments", name);
4481 : 0 : return error_mark_node;
4482 : : }
4483 : 161763625 : else if (is_overloaded_fn (name))
4484 : : {
4485 : 3 : if (complain & tf_error)
4486 : 3 : error ("%qD is a function, not a type", name);
4487 : 3 : return error_mark_node;
4488 : : }
4489 : 161763622 : gcc_assert (identifier_p (name));
4490 : 161763622 : gcc_assert (TYPE_P (context));
4491 : :
4492 : 161763622 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4493 : : /* This can happen for C++17 variadic using (c++/88986). */;
4494 : 150739930 : else if (!MAYBE_CLASS_TYPE_P (context))
4495 : : {
4496 : 6 : if (complain & tf_error)
4497 : 6 : error ("%q#T is not a class", context);
4498 : 6 : return error_mark_node;
4499 : : }
4500 : :
4501 : : /* When the CONTEXT is a dependent type, NAME could refer to a
4502 : : dependent base class of CONTEXT. But look inside it anyway
4503 : : if CONTEXT is a currently open scope, in case it refers to a
4504 : : member of the current instantiation or a non-dependent base;
4505 : : lookup will stop when we hit a dependent base. */
4506 : 161763616 : if (!dependent_scope_p (context))
4507 : : {
4508 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4509 : : (as per [temp.res.general]/3), unless
4510 : : - the tag corresponds to a class-key or 'enum' so
4511 : : [basic.lookup.elab] applies, or
4512 : : - the tag corresponds to scope_type or tf_qualifying_scope is
4513 : : set so [basic.lookup.qual]/1 applies.
4514 : : TODO: If we'd set/track the scope_type tag thoroughly on all
4515 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4516 : : tf_qualifying_scope flag. */
4517 : 225858544 : bool want_type = (tag_type != none_type && tag_type != typename_type)
4518 : 112929272 : || (complain & tf_qualifying_scope);
4519 : 112929272 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4520 : : }
4521 : : else
4522 : : t = NULL_TREE;
4523 : :
4524 : 177797170 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependentish_scope_p (context))
4525 : 48835307 : return build_typename_type (context, name, fullname, tag_type);
4526 : :
4527 : 112928309 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4528 : :
4529 : 112928309 : if (!t)
4530 : : {
4531 : 16032591 : if (complain & tf_error)
4532 : : {
4533 : 232 : if (!COMPLETE_TYPE_P (context))
4534 : 18 : cxx_incomplete_type_error (NULL_TREE, context);
4535 : : else
4536 : 427 : error (want_template ? G_("no class template named %q#T in %q#T")
4537 : : : G_("no type named %q#T in %q#T"), name, context);
4538 : : }
4539 : 16032591 : return error_mark_node;
4540 : : }
4541 : :
4542 : : /* Pull out the template from an injected-class-name (or multiple). */
4543 : 96895718 : if (want_template)
4544 : 3460841 : t = maybe_get_template_decl_from_type_decl (t);
4545 : :
4546 : 96895718 : if (TREE_CODE (t) == TREE_LIST)
4547 : : {
4548 : 3 : if (complain & tf_error)
4549 : : {
4550 : 3 : auto_diagnostic_group d;
4551 : 3 : error ("lookup of %qT in %qT is ambiguous", name, context);
4552 : 3 : print_candidates (t);
4553 : 3 : }
4554 : 3 : return error_mark_node;
4555 : : }
4556 : :
4557 : 96895715 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4558 : : {
4559 : 6 : if (complain & tf_error)
4560 : 6 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
4561 : : context, name, t);
4562 : 6 : return error_mark_node;
4563 : : }
4564 : 96895709 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
4565 : : {
4566 : 15 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4567 : 33 : && DECL_TYPE_TEMPLATE_P (t))
4568 : : /* The caller permits this typename-specifier to name a template
4569 : : (because it appears in a CTAD-enabled context). */;
4570 : : else
4571 : : {
4572 : 23 : if (complain & tf_error)
4573 : 17 : error ("%<typename %T::%D%> names %q#D, which is not a type",
4574 : : context, name, t);
4575 : 23 : return error_mark_node;
4576 : : }
4577 : : }
4578 : :
4579 : 96895686 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4580 : : context, complain))
4581 : 45 : return error_mark_node;
4582 : :
4583 : 96895641 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4584 : 4 : return make_template_placeholder (t);
4585 : :
4586 : 96895637 : if (want_template)
4587 : : {
4588 : 3460835 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4589 : : NULL_TREE, context,
4590 : : complain | tf_user);
4591 : 3460835 : if (t == error_mark_node)
4592 : : return error_mark_node;
4593 : 3460830 : t = TYPE_NAME (t);
4594 : : }
4595 : :
4596 : 96895632 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4597 : 2694039 : t = TREE_TYPE (t);
4598 : :
4599 : 96895632 : maybe_record_typedef_use (t);
4600 : :
4601 : 96895632 : return t;
4602 : : }
4603 : :
4604 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4605 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4606 : : in which case error_mark_node is returned.
4607 : :
4608 : : If PARM_LIST is non-NULL, also make sure that the template parameter
4609 : : list of TEMPLATE_DECL matches.
4610 : :
4611 : : If COMPLAIN zero, don't complain about any errors that occur. */
4612 : :
4613 : : tree
4614 : 71930 : make_unbound_class_template (tree context, tree name, tree parm_list,
4615 : : tsubst_flags_t complain)
4616 : : {
4617 : 71930 : if (TYPE_P (name))
4618 : 0 : name = TYPE_IDENTIFIER (name);
4619 : 71930 : else if (DECL_P (name))
4620 : 0 : name = DECL_NAME (name);
4621 : 71930 : gcc_assert (identifier_p (name));
4622 : :
4623 : 71930 : if (!dependent_type_p (context)
4624 : 71930 : || currently_open_class (context))
4625 : : {
4626 : 21507 : tree tmpl = NULL_TREE;
4627 : :
4628 : 21507 : if (MAYBE_CLASS_TYPE_P (context))
4629 : 21501 : tmpl = lookup_field (context, name, 0, false);
4630 : :
4631 : 21501 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4632 : 3 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4633 : :
4634 : 21495 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4635 : : {
4636 : 12 : if (complain & tf_error)
4637 : 9 : error ("no class template named %q#T in %q#T", name, context);
4638 : 12 : return error_mark_node;
4639 : : }
4640 : :
4641 : 21495 : if (parm_list
4642 : 21495 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4643 : : {
4644 : 0 : if (complain & tf_error)
4645 : : {
4646 : 0 : auto_diagnostic_group d;
4647 : 0 : error ("template parameters do not match template %qD", tmpl);
4648 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
4649 : : "%qD declared here", tmpl);
4650 : 0 : }
4651 : 0 : return error_mark_node;
4652 : : }
4653 : :
4654 : 21495 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4655 : : complain))
4656 : 0 : return error_mark_node;
4657 : :
4658 : : return tmpl;
4659 : : }
4660 : :
4661 : 50423 : return make_unbound_class_template_raw (context, name, parm_list);
4662 : : }
4663 : :
4664 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
4665 : :
4666 : : tree
4667 : 50492 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4668 : : {
4669 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
4670 : 50492 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4671 : 50492 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4672 : 50492 : TREE_TYPE (t) = NULL_TREE;
4673 : 50492 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4674 : :
4675 : : /* Build the corresponding TEMPLATE_DECL. */
4676 : 50492 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4677 : 50492 : TYPE_NAME (t) = d;
4678 : 50492 : TYPE_STUB_DECL (t) = d;
4679 : 50492 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4680 : 50492 : DECL_ARTIFICIAL (d) = 1;
4681 : 50492 : DECL_TEMPLATE_PARMS (d) = parm_list;
4682 : :
4683 : 50492 : return t;
4684 : : }
4685 : :
4686 : :
4687 : :
4688 : : /* Push the declarations of builtin types into the global namespace.
4689 : : RID_INDEX is the index of the builtin type in the array
4690 : : RID_POINTERS. NAME is the name used when looking up the builtin
4691 : : type. TYPE is the _TYPE node for the builtin type.
4692 : :
4693 : : The calls to set_global_binding below should be
4694 : : eliminated. Built-in types should not be looked up name; their
4695 : : names are keywords that the parser can recognize. However, there
4696 : : is code in c-common.cc that uses identifier_global_value to look up
4697 : : built-in types by name. */
4698 : :
4699 : : void
4700 : 3683399 : record_builtin_type (enum rid rid_index,
4701 : : const char* name,
4702 : : tree type)
4703 : : {
4704 : 3683399 : tree decl = NULL_TREE;
4705 : :
4706 : 3683399 : if (name)
4707 : : {
4708 : 2210295 : tree tname = get_identifier (name);
4709 : 2210295 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4710 : 2210295 : DECL_ARTIFICIAL (tdecl) = 1;
4711 : 2210295 : set_global_binding (tdecl);
4712 : 2210295 : decl = tdecl;
4713 : : }
4714 : :
4715 : 3683399 : if ((int) rid_index < (int) RID_MAX)
4716 : 2210295 : if (tree rname = ridpointers[(int) rid_index])
4717 : 1749950 : if (!decl || DECL_NAME (decl) != rname)
4718 : : {
4719 : 1288966 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4720 : 1288966 : DECL_ARTIFICIAL (rdecl) = 1;
4721 : 1288966 : set_global_binding (rdecl);
4722 : 1288966 : if (!decl)
4723 : : decl = rdecl;
4724 : : }
4725 : :
4726 : 3407192 : if (decl)
4727 : : {
4728 : 3223054 : if (!TYPE_NAME (type))
4729 : 2670640 : TYPE_NAME (type) = decl;
4730 : 3223054 : debug_hooks->type_decl (decl, 0);
4731 : : }
4732 : 3683399 : }
4733 : :
4734 : : /* Push a type into the namespace so that the back ends ignore it. */
4735 : :
4736 : : static void
4737 : 276207 : record_unknown_type (tree type, const char* name)
4738 : : {
4739 : 276207 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4740 : : TYPE_DECL, get_identifier (name), type));
4741 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4742 : 276207 : DECL_IGNORED_P (decl) = 1;
4743 : 276207 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4744 : 276207 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4745 : 276207 : SET_TYPE_ALIGN (type, 1);
4746 : 276207 : TYPE_USER_ALIGN (type) = 0;
4747 : 276207 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4748 : 276207 : }
4749 : :
4750 : : /* Create all the predefined identifiers. */
4751 : :
4752 : : static void
4753 : 92069 : initialize_predefined_identifiers (void)
4754 : : {
4755 : 92069 : struct predefined_identifier
4756 : : {
4757 : : const char *name; /* Name. */
4758 : : tree *node; /* Node to store it in. */
4759 : : cp_identifier_kind kind; /* Kind of identifier. */
4760 : : };
4761 : :
4762 : : /* A table of identifiers to create at startup. */
4763 : 92069 : static const predefined_identifier predefined_identifiers[] = {
4764 : : {"C++", &lang_name_cplusplus, cik_normal},
4765 : : {"C", &lang_name_c, cik_normal},
4766 : : /* Some of these names have a trailing space so that it is
4767 : : impossible for them to conflict with names written by users. */
4768 : : {"__ct ", &ctor_identifier, cik_ctor},
4769 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
4770 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4771 : : {"__dt ", &dtor_identifier, cik_dtor},
4772 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
4773 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4774 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4775 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
4776 : : {"__in_chrg", &in_charge_identifier, cik_normal},
4777 : : {"__as_base ", &as_base_identifier, cik_normal},
4778 : : {"this", &this_identifier, cik_normal},
4779 : : {"__delta", &delta_identifier, cik_normal},
4780 : : {"__pfn", &pfn_identifier, cik_normal},
4781 : : {"_vptr", &vptr_identifier, cik_normal},
4782 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4783 : : {"::", &global_identifier, cik_normal},
4784 : : /* The demangler expects anonymous namespaces to be called
4785 : : something starting with '_GLOBAL__N_'. It no longer needs
4786 : : to be unique to the TU. */
4787 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4788 : : {"auto", &auto_identifier, cik_normal},
4789 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4790 : : {"initializer_list", &init_list_identifier, cik_normal},
4791 : : {"__for_range ", &for_range__identifier, cik_normal},
4792 : : {"__for_begin ", &for_begin__identifier, cik_normal},
4793 : : {"__for_end ", &for_end__identifier, cik_normal},
4794 : : {"__for_range", &for_range_identifier, cik_normal},
4795 : : {"__for_begin", &for_begin_identifier, cik_normal},
4796 : : {"__for_end", &for_end_identifier, cik_normal},
4797 : : {"abi_tag", &abi_tag_identifier, cik_normal},
4798 : : {"aligned", &aligned_identifier, cik_normal},
4799 : : {"begin", &begin_identifier, cik_normal},
4800 : : {"end", &end_identifier, cik_normal},
4801 : : {"get", &get__identifier, cik_normal},
4802 : : {"gnu", &gnu_identifier, cik_normal},
4803 : : {"tuple_element", &tuple_element_identifier, cik_normal},
4804 : : {"tuple_size", &tuple_size_identifier, cik_normal},
4805 : : {"type", &type_identifier, cik_normal},
4806 : : {"value", &value_identifier, cik_normal},
4807 : : {"_FUN", &fun_identifier, cik_normal},
4808 : : {"__closure", &closure_identifier, cik_normal},
4809 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
4810 : : {"heap ", &heap_identifier, cik_normal},
4811 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
4812 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4813 : : {"heap []", &heap_vec_identifier, cik_normal},
4814 : : {"omp", &omp_identifier, cik_normal},
4815 : : {NULL, NULL, cik_normal}
4816 : : };
4817 : :
4818 : 4327243 : for (const predefined_identifier *pid = predefined_identifiers;
4819 : 4327243 : pid->name; ++pid)
4820 : : {
4821 : 4235174 : *pid->node = get_identifier (pid->name);
4822 : : /* Some of these identifiers already have a special kind. */
4823 : 4235174 : if (pid->kind != cik_normal)
4824 : 736552 : set_identifier_kind (*pid->node, pid->kind);
4825 : : }
4826 : 92069 : }
4827 : :
4828 : : /* Create the predefined scalar types of C,
4829 : : and some nodes representing standard constants (0, 1, (void *)0).
4830 : : Initialize the global binding level.
4831 : : Make definitions for built-in primitive functions. */
4832 : :
4833 : : void
4834 : 92069 : cxx_init_decl_processing (void)
4835 : : {
4836 : 92069 : tree void_ftype;
4837 : 92069 : tree void_ftype_ptr;
4838 : :
4839 : : /* Create all the identifiers we need. */
4840 : 92069 : initialize_predefined_identifiers ();
4841 : :
4842 : : /* Create the global variables. */
4843 : 92069 : push_to_top_level ();
4844 : :
4845 : 92069 : current_function_decl = NULL_TREE;
4846 : 92069 : current_binding_level = NULL;
4847 : : /* Enter the global namespace. */
4848 : 92069 : gcc_assert (global_namespace == NULL_TREE);
4849 : 92069 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4850 : : void_type_node);
4851 : 92069 : TREE_PUBLIC (global_namespace) = true;
4852 : 92069 : DECL_MODULE_EXPORT_P (global_namespace) = true;
4853 : 92069 : DECL_CONTEXT (global_namespace)
4854 : 92069 : = build_translation_unit_decl (get_identifier (main_input_filename));
4855 : : /* Remember whether we want the empty class passing ABI change warning
4856 : : in this TU. */
4857 : 92069 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4858 : 92708 : = warn_abi && abi_version_crosses (12);
4859 : 92069 : debug_hooks->register_main_translation_unit
4860 : 92069 : (DECL_CONTEXT (global_namespace));
4861 : 92069 : begin_scope (sk_namespace, global_namespace);
4862 : 92069 : current_namespace = global_namespace;
4863 : :
4864 : 92069 : if (flag_visibility_ms_compat)
4865 : 6 : default_visibility = VISIBILITY_HIDDEN;
4866 : :
4867 : : /* Initially, C. */
4868 : 92069 : current_lang_name = lang_name_c;
4869 : :
4870 : : /* Create the `std' namespace. */
4871 : 92069 : push_namespace (get_identifier ("std"));
4872 : 92069 : std_node = current_namespace;
4873 : 92069 : pop_namespace ();
4874 : :
4875 : 92069 : flag_noexcept_type = (cxx_dialect >= cxx17);
4876 : :
4877 : 92069 : c_common_nodes_and_builtins ();
4878 : :
4879 : 92069 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4880 : 92069 : tree decl
4881 : 92069 : = add_builtin_function ("__builtin_is_constant_evaluated",
4882 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4883 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4884 : 92069 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4885 : :
4886 : : /* The concrete return type of __builtin_source_location is
4887 : : const std::source_location::__impl*, but we can't form the type
4888 : : at this point. So we initially declare it with an auto return
4889 : : type which we then "deduce" from require_deduced_type upon first use. */
4890 : 92069 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
4891 : 92069 : decl = add_builtin_function ("__builtin_source_location",
4892 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4893 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4894 : 92069 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4895 : :
4896 : 92069 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4897 : : NULL_TREE);
4898 : 92069 : decl
4899 : 92069 : = add_builtin_function ("__builtin_is_corresponding_member",
4900 : : bool_vaftype,
4901 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4902 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4903 : 92069 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4904 : :
4905 : 92069 : decl
4906 : 92069 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4907 : : bool_vaftype,
4908 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4909 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4910 : 92069 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4911 : :
4912 : 92069 : integer_two_node = build_int_cst (NULL_TREE, 2);
4913 : :
4914 : : /* Guess at the initial static decls size. */
4915 : 92069 : vec_alloc (static_decls, 500);
4916 : :
4917 : : /* ... and keyed classes. */
4918 : 92069 : vec_alloc (keyed_classes, 100);
4919 : :
4920 : 92069 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4921 : 92069 : truthvalue_type_node = boolean_type_node;
4922 : 92069 : truthvalue_false_node = boolean_false_node;
4923 : 92069 : truthvalue_true_node = boolean_true_node;
4924 : :
4925 : 92069 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4926 : 92069 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4927 : 92069 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4928 : 92069 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4929 : : NULL_TREE);
4930 : :
4931 : : #if 0
4932 : : record_builtin_type (RID_MAX, NULL, string_type_node);
4933 : : #endif
4934 : :
4935 : 92069 : delta_type_node = ptrdiff_type_node;
4936 : 92069 : vtable_index_type = ptrdiff_type_node;
4937 : :
4938 : 92069 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
4939 : 92069 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4940 : 92069 : void_ftype_ptr = build_function_type_list (void_type_node,
4941 : : ptr_type_node, NULL_TREE);
4942 : 92069 : void_ftype_ptr
4943 : 92069 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
4944 : :
4945 : : /* Create the conversion operator marker. This operator's DECL_NAME
4946 : : is in the identifier table, so we can use identifier equality to
4947 : : find it. */
4948 : 92069 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4949 : : void_ftype);
4950 : :
4951 : : /* C++ extensions */
4952 : :
4953 : 92069 : unknown_type_node = make_node (LANG_TYPE);
4954 : 92069 : record_unknown_type (unknown_type_node, "unknown type");
4955 : :
4956 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4957 : 92069 : TREE_TYPE (unknown_type_node) = unknown_type_node;
4958 : :
4959 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4960 : : result. */
4961 : 92069 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4962 : 92069 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4963 : :
4964 : 92069 : init_list_type_node = make_node (LANG_TYPE);
4965 : 92069 : record_unknown_type (init_list_type_node, "init list");
4966 : :
4967 : : /* Used when parsing to distinguish parameter-lists () and (void). */
4968 : 92069 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
4969 : :
4970 : 92069 : {
4971 : : /* Make sure we get a unique function type, so we can give
4972 : : its pointer type a name. (This wins for gdb.) */
4973 : 92069 : tree vfunc_type = make_node (FUNCTION_TYPE);
4974 : 92069 : TREE_TYPE (vfunc_type) = integer_type_node;
4975 : 92069 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4976 : 92069 : layout_type (vfunc_type);
4977 : :
4978 : 92069 : vtable_entry_type = build_pointer_type (vfunc_type);
4979 : : }
4980 : 92069 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4981 : :
4982 : 92069 : vtbl_type_node
4983 : 92069 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4984 : 92069 : layout_type (vtbl_type_node);
4985 : 92069 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4986 : 92069 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4987 : 92069 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4988 : 92069 : layout_type (vtbl_ptr_type_node);
4989 : 92069 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4990 : :
4991 : 92069 : push_namespace (get_identifier ("__cxxabiv1"));
4992 : 92069 : abi_node = current_namespace;
4993 : 92069 : pop_namespace ();
4994 : :
4995 : 92069 : any_targ_node = make_node (LANG_TYPE);
4996 : 92069 : record_unknown_type (any_targ_node, "any type");
4997 : :
4998 : : /* Now, C++. */
4999 : 92069 : current_lang_name = lang_name_cplusplus;
5000 : :
5001 : 92069 : if (aligned_new_threshold > 1
5002 : 92069 : && !pow2p_hwi (aligned_new_threshold))
5003 : : {
5004 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
5005 : : aligned_new_threshold);
5006 : 0 : aligned_new_threshold = 1;
5007 : : }
5008 : 92069 : if (aligned_new_threshold == -1)
5009 : 112625 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
5010 : 92069 : if (aligned_new_threshold == 1)
5011 : 71496 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
5012 : :
5013 : 92069 : {
5014 : 92069 : tree newattrs, extvisattr;
5015 : 92069 : tree newtype, deltype;
5016 : 92069 : tree ptr_ftype_sizetype;
5017 : 92069 : tree new_eh_spec;
5018 : :
5019 : 92069 : ptr_ftype_sizetype
5020 : 92069 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5021 : 92069 : if (cxx_dialect == cxx98)
5022 : : {
5023 : 13589 : tree bad_alloc_id;
5024 : 13589 : tree bad_alloc_type_node;
5025 : 13589 : tree bad_alloc_decl;
5026 : :
5027 : 13589 : push_nested_namespace (std_node);
5028 : 13589 : bad_alloc_id = get_identifier ("bad_alloc");
5029 : 13589 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5030 : 13589 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5031 : 13589 : bad_alloc_decl
5032 : 13589 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5033 : 13589 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5034 : 13589 : pop_nested_namespace (std_node);
5035 : :
5036 : 13589 : new_eh_spec
5037 : 13589 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5038 : : }
5039 : : else
5040 : 78480 : new_eh_spec = noexcept_false_spec;
5041 : :
5042 : : /* Ensure attribs.cc is initialized. */
5043 : 92069 : init_attributes ();
5044 : :
5045 : 92069 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5046 : : NULL_TREE);
5047 : 92069 : newattrs = tree_cons (get_identifier ("alloc_size"),
5048 : : build_tree_list (NULL_TREE, integer_one_node),
5049 : : extvisattr);
5050 : 92069 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5051 : 92069 : newtype = build_exception_variant (newtype, new_eh_spec);
5052 : 92069 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5053 : 92069 : deltype = build_exception_variant (deltype, empty_except_spec);
5054 : 92069 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5055 : 92069 : DECL_IS_MALLOC (opnew) = 1;
5056 : 92069 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5057 : 92069 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5058 : 92069 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5059 : 92069 : DECL_IS_MALLOC (opnew) = 1;
5060 : 92069 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5061 : 92069 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5062 : 92069 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5063 : 92069 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5064 : 92069 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5065 : 92069 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5066 : 92069 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5067 : 92069 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5068 : 92069 : if (flag_sized_deallocation)
5069 : : {
5070 : : /* Also push the sized deallocation variants:
5071 : : void operator delete(void*, std::size_t) throw();
5072 : : void operator delete[](void*, std::size_t) throw(); */
5073 : 73218 : tree void_ftype_ptr_size
5074 : 73218 : = build_function_type_list (void_type_node, ptr_type_node,
5075 : : size_type_node, NULL_TREE);
5076 : 73218 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5077 : : extvisattr);
5078 : 73218 : deltype = build_exception_variant (deltype, empty_except_spec);
5079 : 73218 : opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5080 : 73218 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5081 : 73218 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5082 : 73218 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5083 : 73218 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5084 : 73218 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5085 : : }
5086 : :
5087 : 92069 : if (aligned_new_threshold)
5088 : : {
5089 : 71499 : push_nested_namespace (std_node);
5090 : 71499 : tree align_id = get_identifier ("align_val_t");
5091 : 71499 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5092 : : NULL_TREE, /*scoped*/true, NULL);
5093 : 71499 : pop_nested_namespace (std_node);
5094 : :
5095 : : /* operator new (size_t, align_val_t); */
5096 : 71499 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5097 : : align_type_node, NULL_TREE);
5098 : 71499 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5099 : 71499 : newtype = build_exception_variant (newtype, new_eh_spec);
5100 : 71499 : opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5101 : 71499 : DECL_IS_MALLOC (opnew) = 1;
5102 : 71499 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5103 : 71499 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5104 : 71499 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5105 : 71499 : DECL_IS_MALLOC (opnew) = 1;
5106 : 71499 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5107 : 71499 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5108 : :
5109 : : /* operator delete (void *, align_val_t); */
5110 : 71499 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5111 : : align_type_node, NULL_TREE);
5112 : 71499 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5113 : 71499 : deltype = build_exception_variant (deltype, empty_except_spec);
5114 : 71499 : opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5115 : 71499 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5116 : 71499 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5117 : 71499 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5118 : 71499 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5119 : 71499 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5120 : :
5121 : 71499 : if (flag_sized_deallocation)
5122 : : {
5123 : : /* operator delete (void *, size_t, align_val_t); */
5124 : 71494 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5125 : : size_type_node, align_type_node,
5126 : : NULL_TREE);
5127 : 71494 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5128 : 71494 : deltype = build_exception_variant (deltype, empty_except_spec);
5129 : 71494 : opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5130 : 71494 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5131 : 71494 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5132 : 71494 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5133 : 71494 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5134 : 71494 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5135 : : }
5136 : : }
5137 : :
5138 : : /* C++-specific nullptr initialization. */
5139 : 92069 : if (abi_version_at_least (9))
5140 : 91898 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5141 : 92069 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5142 : : }
5143 : :
5144 : 92069 : if (! supports_one_only ())
5145 : 0 : flag_weak = 0;
5146 : :
5147 : 92069 : abort_fndecl
5148 : 92069 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5149 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5150 : 92069 : if (flag_weak)
5151 : : /* If no definition is available, resolve references to NULL. */
5152 : 92039 : declare_weak (abort_fndecl);
5153 : :
5154 : : /* Perform other language dependent initializations. */
5155 : 92069 : init_class_processing ();
5156 : 92069 : init_rtti_processing ();
5157 : 92069 : init_template_processing ();
5158 : :
5159 : 92069 : if (flag_exceptions)
5160 : 90997 : init_exception_processing ();
5161 : :
5162 : 92069 : if (flag_contracts)
5163 : 140 : init_terminate_fn ();
5164 : :
5165 : 92069 : if (modules_p ())
5166 : 3604 : init_modules (parse_in);
5167 : :
5168 : 92069 : make_fname_decl = cp_make_fname_decl;
5169 : 92069 : start_fname_decls ();
5170 : :
5171 : : /* Show we use EH for cleanups. */
5172 : 92069 : if (flag_exceptions)
5173 : 90997 : using_eh_for_cleanups ();
5174 : :
5175 : : /* Check that the hardware interference sizes are at least
5176 : : alignof(max_align_t), as required by the standard. */
5177 : 92069 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5178 : 92069 : if (OPTION_SET_P (param_destruct_interfere_size))
5179 : : {
5180 : 0 : if (param_destruct_interfere_size < max_align)
5181 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5182 : : "%d", param_destruct_interfere_size, max_align);
5183 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5184 : 0 : warning (OPT_Winterference_size,
5185 : : "%<--param destructive-interference-size=%d%> "
5186 : : "is less than %<--param l1-cache-line-size=%d%>",
5187 : : param_destruct_interfere_size, param_l1_cache_line_size);
5188 : : }
5189 : 92069 : else if (param_destruct_interfere_size)
5190 : : /* Assume the internal value is OK. */;
5191 : 0 : else if (param_l1_cache_line_size >= max_align)
5192 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5193 : : /* else leave it unset. */
5194 : :
5195 : 92069 : if (OPTION_SET_P (param_construct_interfere_size))
5196 : : {
5197 : 0 : if (param_construct_interfere_size < max_align)
5198 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5199 : : "%d", param_construct_interfere_size, max_align);
5200 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5201 : 0 : && param_l1_cache_line_size >= max_align)
5202 : 0 : warning (OPT_Winterference_size,
5203 : : "%<--param constructive-interference-size=%d%> "
5204 : : "is greater than %<--param l1-cache-line-size=%d%>",
5205 : : param_construct_interfere_size, param_l1_cache_line_size);
5206 : : }
5207 : 92069 : else if (param_construct_interfere_size)
5208 : : /* Assume the internal value is OK. */;
5209 : 0 : else if (param_l1_cache_line_size >= max_align)
5210 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5211 : 92069 : }
5212 : :
5213 : : /* Enter an abi node in global-module context. returns a cookie to
5214 : : give to pop_abi_namespace. */
5215 : :
5216 : : unsigned
5217 : 58183 : push_abi_namespace (tree node)
5218 : : {
5219 : 58183 : push_nested_namespace (node);
5220 : 58183 : push_visibility ("default", 2);
5221 : 58183 : unsigned flags = module_kind;
5222 : 58183 : module_kind = 0;
5223 : 58183 : return flags;
5224 : : }
5225 : :
5226 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5227 : : you. */
5228 : :
5229 : : void
5230 : 58183 : pop_abi_namespace (unsigned flags, tree node)
5231 : : {
5232 : 58183 : module_kind = flags;
5233 : 58183 : pop_visibility (2);
5234 : 58183 : pop_nested_namespace (node);
5235 : 58183 : }
5236 : :
5237 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5238 : : the decl, LOC is the location to give the decl, NAME is the
5239 : : initialization string and TYPE_DEP indicates whether NAME depended
5240 : : on the type of the function. We make use of that to detect
5241 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5242 : : at the point of first use, so we mustn't push the decl now. */
5243 : :
5244 : : static tree
5245 : 56762 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5246 : : {
5247 : 56762 : tree domain = NULL_TREE;
5248 : 56762 : tree init = NULL_TREE;
5249 : :
5250 : 56762 : if (!(type_dep && current_function_decl && in_template_context))
5251 : : {
5252 : 19100 : const char *name = NULL;
5253 : 19100 : bool release_name = false;
5254 : :
5255 : 19100 : if (current_function_decl == NULL_TREE)
5256 : : name = "top level";
5257 : 19086 : else if (type_dep == 0)
5258 : : {
5259 : : /* __FUNCTION__ */
5260 : 580 : name = fname_as_string (type_dep);
5261 : 580 : release_name = true;
5262 : : }
5263 : : else
5264 : : {
5265 : : /* __PRETTY_FUNCTION__ */
5266 : 18506 : gcc_checking_assert (type_dep == 1);
5267 : 18506 : name = cxx_printable_name (current_function_decl, 2);
5268 : : }
5269 : :
5270 : 19100 : size_t length = strlen (name);
5271 : 19100 : domain = build_index_type (size_int (length));
5272 : 19100 : init = build_string (length + 1, name);
5273 : 19100 : if (release_name)
5274 : 580 : free (const_cast<char *> (name));
5275 : : }
5276 : :
5277 : 56762 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5278 : 56762 : type = build_cplus_array_type (type, domain);
5279 : :
5280 : 56762 : if (init)
5281 : 19100 : TREE_TYPE (init) = type;
5282 : : else
5283 : 37662 : init = error_mark_node;
5284 : :
5285 : 56762 : tree decl = build_decl (loc, VAR_DECL, id, type);
5286 : :
5287 : 56762 : TREE_READONLY (decl) = 1;
5288 : 56762 : DECL_ARTIFICIAL (decl) = 1;
5289 : 56762 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5290 : 56762 : TREE_STATIC (decl) = 1;
5291 : :
5292 : 56762 : TREE_USED (decl) = 1;
5293 : :
5294 : 56762 : SET_DECL_VALUE_EXPR (decl, init);
5295 : 56762 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5296 : : /* For decl_constant_var_p. */
5297 : 56762 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5298 : :
5299 : 56762 : if (current_function_decl)
5300 : : {
5301 : 56748 : DECL_CONTEXT (decl) = current_function_decl;
5302 : 56748 : decl = pushdecl_outermost_localscope (decl);
5303 : 56748 : if (decl != error_mark_node)
5304 : 56745 : add_decl_expr (decl);
5305 : : }
5306 : : else
5307 : : {
5308 : 14 : DECL_THIS_STATIC (decl) = true;
5309 : 14 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5310 : : }
5311 : :
5312 : 56762 : return decl;
5313 : : }
5314 : :
5315 : : /* Install DECL as a builtin function at current global scope. Return
5316 : : the new decl (if we found an existing version). Also installs it
5317 : : into ::std, if it's not '_*'. */
5318 : :
5319 : : tree
5320 : 203145575 : cxx_builtin_function (tree decl)
5321 : : {
5322 : 203145575 : retrofit_lang_decl (decl);
5323 : :
5324 : 203145575 : DECL_ARTIFICIAL (decl) = 1;
5325 : 203145575 : SET_DECL_LANGUAGE (decl, lang_c);
5326 : : /* Runtime library routines are, by definition, available in an
5327 : : external shared object. */
5328 : 203145575 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5329 : 203145575 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5330 : :
5331 : 203145575 : tree id = DECL_NAME (decl);
5332 : 203145575 : const char *name = IDENTIFIER_POINTER (id);
5333 : 203145575 : bool hiding = false;
5334 : 203145575 : if (name[0] != '_' || name[1] != '_')
5335 : : /* In the user's namespace, it must be declared before use. */
5336 : : hiding = true;
5337 : 169918904 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5338 : 169918904 : && !startswith (name + 2, "builtin_")
5339 : 191388202 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5340 : : "_chk", strlen ("_chk") + 1))
5341 : : /* Treat __*_chk fortification functions as anticipated as well,
5342 : : unless they are __builtin_*_chk. */
5343 : : hiding = true;
5344 : :
5345 : : /* All builtins that don't begin with an '_' should additionally
5346 : : go in the 'std' namespace. */
5347 : 203145575 : if (name[0] != '_')
5348 : : {
5349 : 33099947 : tree std_decl = copy_decl (decl);
5350 : :
5351 : 33099947 : push_nested_namespace (std_node);
5352 : 33099947 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5353 : 33099947 : pushdecl (std_decl, hiding);
5354 : 33099947 : pop_nested_namespace (std_node);
5355 : : }
5356 : :
5357 : 203145575 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5358 : 203145575 : decl = pushdecl (decl, hiding);
5359 : :
5360 : 203145575 : return decl;
5361 : : }
5362 : :
5363 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5364 : : scope. This is to allow function specific options to add new machine
5365 : : dependent builtins when the target ISA changes via attribute((target(...)))
5366 : : which saves space on program startup if the program does not use non-generic
5367 : : ISAs. */
5368 : :
5369 : : tree
5370 : 662763 : cxx_builtin_function_ext_scope (tree decl)
5371 : : {
5372 : 662763 : push_nested_namespace (global_namespace);
5373 : 662763 : decl = cxx_builtin_function (decl);
5374 : 662763 : pop_nested_namespace (global_namespace);
5375 : :
5376 : 662763 : return decl;
5377 : : }
5378 : :
5379 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5380 : :
5381 : : tree
5382 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5383 : : {
5384 : 0 : retrofit_lang_decl (decl);
5385 : :
5386 : 0 : DECL_ARTIFICIAL (decl) = 1;
5387 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5388 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5389 : 0 : return pushdecl (decl);
5390 : : }
5391 : :
5392 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5393 : : function. Not called directly. */
5394 : :
5395 : : static tree
5396 : 1481032 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5397 : : int ecf_flags)
5398 : : {
5399 : 1481032 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5400 : 1481032 : DECL_EXTERNAL (fn) = 1;
5401 : 1481032 : TREE_PUBLIC (fn) = 1;
5402 : 1481032 : DECL_ARTIFICIAL (fn) = 1;
5403 : 1481032 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5404 : 1481032 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5405 : 1481032 : SET_DECL_LANGUAGE (fn, lang_c);
5406 : : /* Runtime library routines are, by definition, available in an
5407 : : external shared object. */
5408 : 1481032 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5409 : 1481032 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5410 : 1481032 : set_call_expr_flags (fn, ecf_flags);
5411 : 1481032 : return fn;
5412 : : }
5413 : :
5414 : : /* Returns the _DECL for a library function with C++ linkage. */
5415 : :
5416 : : static tree
5417 : 1034775 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5418 : : int ecf_flags)
5419 : : {
5420 : 1034775 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5421 : 1034775 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5422 : 1034775 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5423 : 1034775 : return fn;
5424 : : }
5425 : :
5426 : : /* Like build_library_fn, but takes a C string instead of an
5427 : : IDENTIFIER_NODE. */
5428 : :
5429 : : tree
5430 : 97783 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5431 : : {
5432 : 97783 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5433 : : }
5434 : :
5435 : : /* Like build_cp_library_fn, but takes a C string instead of an
5436 : : IDENTIFIER_NODE. */
5437 : :
5438 : : tree
5439 : 91079 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5440 : : {
5441 : 91079 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5442 : 91079 : ecf_flags);
5443 : : }
5444 : :
5445 : : /* Like build_library_fn, but also pushes the function so that we will
5446 : : be able to find it via get_global_binding. Also, the function
5447 : : may throw exceptions listed in RAISES. */
5448 : :
5449 : : tree
5450 : 348474 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5451 : : {
5452 : 348474 : if (raises)
5453 : 39519 : type = build_exception_variant (type, raises);
5454 : :
5455 : 348474 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5456 : 348474 : return pushdecl_top_level (fn);
5457 : : }
5458 : :
5459 : : /* Like build_cp_library_fn, but also pushes the function so that it
5460 : : will be found by normal lookup. */
5461 : :
5462 : : static tree
5463 : 943696 : push_cp_library_fn (enum tree_code operator_code, tree type,
5464 : : int ecf_flags)
5465 : : {
5466 : 943696 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5467 : : operator_code, type, ecf_flags);
5468 : 943696 : pushdecl (fn);
5469 : 943696 : if (flag_tm)
5470 : 3280 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
5471 : 943696 : return fn;
5472 : : }
5473 : :
5474 : : /* Like push_library_fn, but also note that this function throws
5475 : : and does not return. Used for __throw_foo and the like. */
5476 : :
5477 : : tree
5478 : 101835 : push_throw_library_fn (tree name, tree type)
5479 : : {
5480 : 101835 : tree fn = push_library_fn (name, type, NULL_TREE,
5481 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5482 : 101835 : return fn;
5483 : : }
5484 : :
5485 : : /* When we call finish_struct for an anonymous union, we create
5486 : : default copy constructors and such. But, an anonymous union
5487 : : shouldn't have such things; this function undoes the damage to the
5488 : : anonymous union type T.
5489 : :
5490 : : (The reason that we create the synthesized methods is that we don't
5491 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
5492 : : The first is an anonymous union; the second is just an ordinary
5493 : : union type.) */
5494 : :
5495 : : void
5496 : 89965 : fixup_anonymous_aggr (tree t)
5497 : : {
5498 : : /* Wipe out memory of synthesized methods. */
5499 : 89965 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5500 : 89965 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5501 : 89965 : TYPE_HAS_COPY_CTOR (t) = 0;
5502 : 89965 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5503 : 89965 : TYPE_HAS_COPY_ASSIGN (t) = 0;
5504 : 89965 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5505 : :
5506 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5507 : : invalid members. */
5508 : 467625 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5509 : : {
5510 : 377660 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5511 : 0 : *prev_p = DECL_CHAIN (probe);
5512 : : else
5513 : 377660 : prev_p = &DECL_CHAIN (probe);
5514 : :
5515 : 377660 : if (DECL_ARTIFICIAL (probe)
5516 : 377660 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5517 : 40928 : || TYPE_ANON_P (TREE_TYPE (probe))))
5518 : 133912 : continue;
5519 : :
5520 : 243748 : if (TREE_CODE (probe) != FIELD_DECL
5521 : 243748 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5522 : : {
5523 : : /* We already complained about static data members in
5524 : : finish_static_data_member_decl. */
5525 : 66 : if (!VAR_P (probe))
5526 : : {
5527 : 51 : auto_diagnostic_group d;
5528 : 51 : if (permerror (DECL_SOURCE_LOCATION (probe),
5529 : 51 : TREE_CODE (t) == UNION_TYPE
5530 : : ? "%q#D invalid; an anonymous union may "
5531 : : "only have public non-static data members"
5532 : : : "%q#D invalid; an anonymous struct may "
5533 : : "only have public non-static data members", probe))
5534 : : {
5535 : 51 : static bool hint;
5536 : 51 : if (flag_permissive && !hint)
5537 : : {
5538 : 6 : hint = true;
5539 : 6 : inform (DECL_SOURCE_LOCATION (probe),
5540 : : "this flexibility is deprecated and will be "
5541 : : "removed");
5542 : : }
5543 : : }
5544 : 51 : }
5545 : : }
5546 : : }
5547 : :
5548 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5549 : 89965 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5550 : 89965 : unsigned store = 0;
5551 : 197945 : for (tree elt : vec)
5552 : 107980 : if (!is_overloaded_fn (elt))
5553 : 107959 : (*vec)[store++] = elt;
5554 : 89965 : vec_safe_truncate (vec, store);
5555 : :
5556 : : /* Wipe RTTI info. */
5557 : 89965 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5558 : :
5559 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5560 : : assignment operators (because they cannot have these methods themselves).
5561 : : For anonymous unions this is already checked because they are not allowed
5562 : : in any union, otherwise we have to check it. */
5563 : 89965 : if (TREE_CODE (t) != UNION_TYPE)
5564 : : {
5565 : 19398 : tree field, type;
5566 : :
5567 : 19398 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5568 : : {
5569 : 9 : error_at (location_of (t), "anonymous struct with base classes");
5570 : : /* Avoid ICE after error on anon-struct9.C. */
5571 : 9 : TYPE_NEEDS_CONSTRUCTING (t) = false;
5572 : : }
5573 : :
5574 : 87728 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5575 : 68330 : if (TREE_CODE (field) == FIELD_DECL)
5576 : : {
5577 : 48831 : type = TREE_TYPE (field);
5578 : 48831 : if (CLASS_TYPE_P (type))
5579 : : {
5580 : 58 : if (TYPE_NEEDS_CONSTRUCTING (type))
5581 : 3 : error ("member %q+#D with constructor not allowed "
5582 : : "in anonymous aggregate", field);
5583 : 58 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5584 : 0 : error ("member %q+#D with destructor not allowed "
5585 : : "in anonymous aggregate", field);
5586 : 58 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5587 : 0 : error ("member %q+#D with copy assignment operator "
5588 : : "not allowed in anonymous aggregate", field);
5589 : : }
5590 : : }
5591 : : }
5592 : 89965 : }
5593 : :
5594 : : /* Warn for an attribute located at LOCATION that appertains to the
5595 : : class type CLASS_TYPE that has not been properly placed after its
5596 : : class-key, in it class-specifier. */
5597 : :
5598 : : void
5599 : 31 : warn_misplaced_attr_for_class_type (location_t location,
5600 : : tree class_type)
5601 : : {
5602 : 31 : gcc_assert (OVERLOAD_TYPE_P (class_type));
5603 : :
5604 : 31 : auto_diagnostic_group d;
5605 : 31 : if (warning_at (location, OPT_Wattributes,
5606 : : "attribute ignored in declaration "
5607 : : "of %q#T", class_type))
5608 : 31 : inform (location,
5609 : : "attribute for %q#T must follow the %qs keyword",
5610 : : class_type, class_key_or_enum_as_string (class_type));
5611 : 31 : }
5612 : :
5613 : : /* Returns the cv-qualifiers that apply to the type specified
5614 : : by the DECLSPECS. */
5615 : :
5616 : : static int
5617 : 866460909 : get_type_quals (const cp_decl_specifier_seq *declspecs)
5618 : : {
5619 : 866460909 : int type_quals = TYPE_UNQUALIFIED;
5620 : :
5621 : 866460909 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5622 : 96059259 : type_quals |= TYPE_QUAL_CONST;
5623 : 866460909 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5624 : 1478789 : type_quals |= TYPE_QUAL_VOLATILE;
5625 : 866460909 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5626 : 34 : type_quals |= TYPE_QUAL_RESTRICT;
5627 : :
5628 : 866460909 : return type_quals;
5629 : : }
5630 : :
5631 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
5632 : : just declares a tagged type or anonymous union.
5633 : :
5634 : : Returns the type declared; or NULL_TREE if none. */
5635 : :
5636 : : tree
5637 : 30776829 : check_tag_decl (cp_decl_specifier_seq *declspecs,
5638 : : bool explicit_type_instantiation_p)
5639 : : {
5640 : 30776829 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5641 : 30776829 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5642 : : /* If a class, struct, or enum type is declared by the DECLSPECS
5643 : : (i.e, if a class-specifier, enum-specifier, or non-typename
5644 : : elaborated-type-specifier appears in the DECLSPECS),
5645 : : DECLARED_TYPE is set to the corresponding type. */
5646 : 30776829 : tree declared_type = NULL_TREE;
5647 : 30776829 : bool error_p = false;
5648 : :
5649 : 30776829 : if (declspecs->multiple_types_p)
5650 : 26 : error_at (smallest_type_location (declspecs),
5651 : : "multiple types in one declaration");
5652 : 30776803 : else if (declspecs->redefined_builtin_type)
5653 : : {
5654 : 21 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5655 : 21 : if (!in_system_header_at (loc))
5656 : 9 : permerror (loc, "redeclaration of C++ built-in type %qT",
5657 : : declspecs->redefined_builtin_type);
5658 : 21 : return NULL_TREE;
5659 : : }
5660 : :
5661 : 30776808 : if (declspecs->type
5662 : 30776793 : && TYPE_P (declspecs->type)
5663 : 61413519 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5664 : 30635524 : && MAYBE_CLASS_TYPE_P (declspecs->type))
5665 : 1543466 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5666 : : declared_type = declspecs->type;
5667 : 141326 : else if (declspecs->type == error_mark_node)
5668 : 750 : error_p = true;
5669 : :
5670 : 30776808 : if (type_uses_auto (declared_type))
5671 : : {
5672 : 8 : error_at (declspecs->locations[ds_type_spec],
5673 : : "%<auto%> can only be specified for variables "
5674 : : "or function declarations");
5675 : 8 : return error_mark_node;
5676 : : }
5677 : :
5678 : 30776800 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5679 : : declared_type = NULL_TREE;
5680 : :
5681 : 30776800 : if (!declared_type && !saw_friend && !error_p)
5682 : 88 : permerror (input_location, "declaration does not declare anything");
5683 : : /* Check for an anonymous union. */
5684 : 30635447 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5685 : 89053103 : && TYPE_UNNAMED_P (declared_type))
5686 : : {
5687 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
5688 : : can be omitted only when declaring a class (clause 9) or
5689 : : enumeration (7.2), that is, when the decl-specifier-seq contains
5690 : : either a class-specifier, an elaborated-type-specifier with
5691 : : a class-key (9.1), or an enum-specifier. In these cases and
5692 : : whenever a class-specifier or enum-specifier is present in the
5693 : : decl-specifier-seq, the identifiers in these specifiers are among
5694 : : the names being declared by the declaration (as class-name,
5695 : : enum-names, or enumerators, depending on the syntax). In such
5696 : : cases, and except for the declaration of an unnamed bit-field (9.6),
5697 : : the decl-specifier-seq shall introduce one or more names into the
5698 : : program, or shall redeclare a name introduced by a previous
5699 : : declaration. [Example:
5700 : : enum { }; // ill-formed
5701 : : typedef class { }; // ill-formed
5702 : : --end example] */
5703 : 89971 : if (saw_typedef)
5704 : : {
5705 : 6 : error_at (declspecs->locations[ds_typedef],
5706 : : "missing type-name in typedef-declaration");
5707 : 6 : return NULL_TREE;
5708 : : }
5709 : 89965 : /* Anonymous unions are objects, so they can have specifiers. */;
5710 : 89965 : SET_ANON_AGGR_TYPE_P (declared_type);
5711 : :
5712 : 89965 : if (TREE_CODE (declared_type) != UNION_TYPE)
5713 : 19398 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5714 : : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5715 : : }
5716 : :
5717 : : else
5718 : : {
5719 : 30686741 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5720 : 9 : error_at (declspecs->locations[ds_inline],
5721 : : "%<inline%> can only be specified for functions");
5722 : 30686732 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5723 : 3 : error_at (declspecs->locations[ds_virtual],
5724 : : "%<virtual%> can only be specified for functions");
5725 : 30686729 : else if (saw_friend
5726 : 30686729 : && (!current_class_type
5727 : 1488738 : || current_scope () != current_class_type))
5728 : 0 : error_at (declspecs->locations[ds_friend],
5729 : : "%<friend%> can only be specified inside a class");
5730 : 30686729 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5731 : 3 : error_at (declspecs->locations[ds_explicit],
5732 : : "%<explicit%> can only be specified for constructors");
5733 : 30686726 : else if (declspecs->storage_class)
5734 : 3 : error_at (declspecs->locations[ds_storage_class],
5735 : : "a storage class can only be specified for objects "
5736 : : "and functions");
5737 : 30686723 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5738 : 9 : error_at (declspecs->locations[ds_const],
5739 : : "%<const%> can only be specified for objects and "
5740 : : "functions");
5741 : 30686714 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5742 : 3 : error_at (declspecs->locations[ds_volatile],
5743 : : "%<volatile%> can only be specified for objects and "
5744 : : "functions");
5745 : 30686711 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5746 : 3 : error_at (declspecs->locations[ds_restrict],
5747 : : "%<__restrict%> can only be specified for objects and "
5748 : : "functions");
5749 : 30686708 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5750 : 3 : error_at (declspecs->locations[ds_thread],
5751 : : "%<__thread%> can only be specified for objects "
5752 : : "and functions");
5753 : 30686705 : else if (saw_typedef)
5754 : 24 : warning_at (declspecs->locations[ds_typedef], 0,
5755 : : "%<typedef%> was ignored in this declaration");
5756 : 30686681 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5757 : 9 : error_at (declspecs->locations[ds_constexpr],
5758 : : "%qs cannot be used for type declarations", "constexpr");
5759 : 30686672 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5760 : 3 : error_at (declspecs->locations[ds_constinit],
5761 : : "%qs cannot be used for type declarations", "constinit");
5762 : 30686669 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5763 : 3 : error_at (declspecs->locations[ds_consteval],
5764 : : "%qs cannot be used for type declarations", "consteval");
5765 : : }
5766 : :
5767 : 30776794 : if (declspecs->attributes && warn_attributes && declared_type)
5768 : : {
5769 : 31 : location_t loc;
5770 : 25 : if (!CLASS_TYPE_P (declared_type)
5771 : 56 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5772 : : /* For a non-template class, use the name location. */
5773 : 22 : loc = location_of (declared_type);
5774 : : else
5775 : : /* For a template class (an explicit instantiation), use the
5776 : : current location. */
5777 : 9 : loc = input_location;
5778 : :
5779 : 31 : if (explicit_type_instantiation_p)
5780 : : /* [dcl.attr.grammar]/4:
5781 : :
5782 : : No attribute-specifier-seq shall appertain to an explicit
5783 : : instantiation. */
5784 : : {
5785 : 6 : auto_diagnostic_group d;
5786 : 6 : if (warning_at (loc, OPT_Wattributes,
5787 : : "attribute ignored in explicit instantiation %q#T",
5788 : : declared_type))
5789 : 6 : inform (loc,
5790 : : "no attribute can be applied to "
5791 : : "an explicit instantiation");
5792 : 6 : }
5793 : : else
5794 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
5795 : : }
5796 : :
5797 : : /* Diagnose invalid application of contracts, if any. */
5798 : 30776794 : if (find_contract (declspecs->attributes))
5799 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
5800 : : else
5801 : 30776793 : diagnose_misapplied_contracts (declspecs->std_attributes);
5802 : :
5803 : : return declared_type;
5804 : : }
5805 : :
5806 : : /* Called when a declaration is seen that contains no names to declare.
5807 : : If its type is a reference to a structure, union or enum inherited
5808 : : from a containing scope, shadow that tag name for the current scope
5809 : : with a forward reference.
5810 : : If its type defines a new named structure or union
5811 : : or defines an enum, it is valid but we need not do anything here.
5812 : : Otherwise, it is an error.
5813 : :
5814 : : C++: may have to grok the declspecs to learn about static,
5815 : : complain for anonymous unions.
5816 : :
5817 : : Returns the TYPE declared -- or NULL_TREE if none. */
5818 : :
5819 : : tree
5820 : 27105290 : shadow_tag (cp_decl_specifier_seq *declspecs)
5821 : : {
5822 : 27105290 : tree t = check_tag_decl (declspecs,
5823 : : /*explicit_type_instantiation_p=*/false);
5824 : :
5825 : 27105290 : if (!t)
5826 : : return NULL_TREE;
5827 : :
5828 : 27104532 : t = maybe_process_partial_specialization (t);
5829 : 27104532 : if (t == error_mark_node)
5830 : : return NULL_TREE;
5831 : :
5832 : : /* This is where the variables in an anonymous union are
5833 : : declared. An anonymous union declaration looks like:
5834 : : union { ... } ;
5835 : : because there is no declarator after the union, the parser
5836 : : sends that declaration here. */
5837 : 27103314 : if (ANON_AGGR_TYPE_P (t))
5838 : : {
5839 : 236 : fixup_anonymous_aggr (t);
5840 : :
5841 : 236 : if (TYPE_FIELDS (t))
5842 : : {
5843 : 236 : tree decl = grokdeclarator (/*declarator=*/NULL,
5844 : : declspecs, NORMAL, 0, NULL);
5845 : 236 : finish_anon_union (decl);
5846 : : }
5847 : : }
5848 : :
5849 : : return t;
5850 : : }
5851 : :
5852 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5853 : :
5854 : : tree
5855 : 317898189 : groktypename (cp_decl_specifier_seq *type_specifiers,
5856 : : const cp_declarator *declarator,
5857 : : bool is_template_arg)
5858 : : {
5859 : 317898189 : tree attrs;
5860 : 317898189 : tree type;
5861 : 635796378 : enum decl_context context
5862 : 317898189 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5863 : 317898189 : attrs = type_specifiers->attributes;
5864 : 317898189 : type_specifiers->attributes = NULL_TREE;
5865 : 317898189 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5866 : 317898189 : if (attrs && type != error_mark_node)
5867 : : {
5868 : 577 : if (CLASS_TYPE_P (type))
5869 : 6 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5870 : : "outside of definition", type);
5871 : 571 : else if (MAYBE_CLASS_TYPE_P (type))
5872 : : /* A template type parameter or other dependent type. */
5873 : 9 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5874 : : "type %qT without an associated declaration", type);
5875 : : else
5876 : 562 : cplus_decl_attributes (&type, attrs, 0);
5877 : : }
5878 : 317898189 : return type;
5879 : : }
5880 : :
5881 : : /* Process a DECLARATOR for a function-scope or namespace-scope
5882 : : variable or function declaration.
5883 : : (Function definitions go through start_function; class member
5884 : : declarations appearing in the body of the class go through
5885 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
5886 : : If an error occurs, the error_mark_node is returned instead.
5887 : :
5888 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5889 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5890 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
5891 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5892 : : implicitly initialized via a default constructor. It can also be
5893 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5894 : : mark the new decl as DECL_DECOMPOSITION_P.
5895 : :
5896 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5897 : : declaration.
5898 : :
5899 : : The scope represented by the context of the returned DECL is pushed
5900 : : (if it is not the global namespace) and is assigned to
5901 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
5902 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
5903 : :
5904 : : tree
5905 : 106913915 : start_decl (const cp_declarator *declarator,
5906 : : cp_decl_specifier_seq *declspecs,
5907 : : int initialized,
5908 : : tree attributes,
5909 : : tree prefix_attributes,
5910 : : tree *pushed_scope_p)
5911 : : {
5912 : 106913915 : tree decl;
5913 : 106913915 : tree context;
5914 : 106913915 : bool was_public;
5915 : 106913915 : int flags;
5916 : 106913915 : bool alias;
5917 : 106913915 : tree initial;
5918 : :
5919 : 106913915 : *pushed_scope_p = NULL_TREE;
5920 : :
5921 : 106913915 : if (prefix_attributes != error_mark_node)
5922 : 106913913 : attributes = attr_chainon (attributes, prefix_attributes);
5923 : :
5924 : 106913915 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5925 : : &attributes);
5926 : :
5927 : 106913915 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
5928 : 106913915 : || decl == error_mark_node
5929 : 106911801 : || prefix_attributes == error_mark_node)
5930 : 2116 : return error_mark_node;
5931 : :
5932 : 106911799 : context = CP_DECL_CONTEXT (decl);
5933 : 106911799 : if (context != global_namespace)
5934 : 16520526 : *pushed_scope_p = push_scope (context);
5935 : :
5936 : 106911799 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
5937 : : {
5938 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
5939 : : "typedef %qD is initialized (use %qs instead)",
5940 : : decl, "decltype");
5941 : 9 : return error_mark_node;
5942 : : }
5943 : :
5944 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
5945 : : with attribute validation. */
5946 : 106911790 : initial = DECL_INITIAL (decl);
5947 : :
5948 : 106911790 : if (initialized)
5949 : : {
5950 : 46760689 : if (! toplevel_bindings_p ()
5951 : 46760689 : && DECL_EXTERNAL (decl))
5952 : 3 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
5953 : : decl);
5954 : 46760689 : DECL_EXTERNAL (decl) = 0;
5955 : 46760689 : if (toplevel_bindings_p ())
5956 : 3958308 : TREE_STATIC (decl) = 1;
5957 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
5958 : : even though we might not yet have the initializer expression. */
5959 : 46760689 : if (!DECL_INITIAL (decl))
5960 : 46357728 : DECL_INITIAL (decl) = error_mark_node;
5961 : : }
5962 : 106911790 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5963 : :
5964 : 106911790 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5965 : 3820 : record_key_method_defined (decl);
5966 : :
5967 : : /* If this is a typedef that names the class for linkage purposes
5968 : : (7.1.3p8), apply any attributes directly to the type. */
5969 : 106911790 : if (TREE_CODE (decl) == TYPE_DECL
5970 : 18581642 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5971 : 114309717 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5972 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
5973 : : else
5974 : : flags = 0;
5975 : :
5976 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5977 : 106911790 : cplus_decl_attributes (&decl, attributes, flags);
5978 : :
5979 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5980 : : assist with attribute validation. */
5981 : 106911790 : DECL_INITIAL (decl) = initial;
5982 : :
5983 : : /* Dllimported symbols cannot be defined. Static data members (which
5984 : : can be initialized in-class and dllimported) go through grokfield,
5985 : : not here, so we don't need to exclude those decls when checking for
5986 : : a definition. */
5987 : 106911790 : if (initialized && DECL_DLLIMPORT_P (decl))
5988 : : {
5989 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
5990 : : "definition of %q#D is marked %<dllimport%>", decl);
5991 : 0 : DECL_DLLIMPORT_P (decl) = 0;
5992 : : }
5993 : :
5994 : : /* If #pragma weak was used, mark the decl weak now. */
5995 : 106911790 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5996 : 41289724 : maybe_apply_pragma_weak (decl);
5997 : :
5998 : 106911790 : if (TREE_CODE (decl) == FUNCTION_DECL
5999 : 35947877 : && DECL_DECLARED_INLINE_P (decl)
6000 : 798564 : && DECL_UNINLINABLE (decl)
6001 : 106911790 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6002 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
6003 : : "inline function %qD given attribute %qs", decl, "noinline");
6004 : :
6005 : 106911790 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
6006 : : {
6007 : 734891 : bool this_tmpl = (current_template_depth
6008 : 734891 : > template_class_depth (context));
6009 : 734891 : if (VAR_P (decl))
6010 : : {
6011 : 372869 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6012 : 372869 : if (field == NULL_TREE
6013 : 372869 : || !(VAR_P (field) || variable_template_p (field)))
6014 : 7 : error ("%q+#D is not a static data member of %q#T", decl, context);
6015 : 372862 : else if (variable_template_p (field)
6016 : 372862 : && (DECL_LANG_SPECIFIC (decl)
6017 : 93 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
6018 : : /* OK, specialization was already checked. */;
6019 : 372805 : else if (variable_template_p (field) && !this_tmpl)
6020 : : {
6021 : 3 : auto_diagnostic_group d;
6022 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6023 : : "non-member-template declaration of %qD", decl);
6024 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6025 : : "member template declaration here");
6026 : 3 : return error_mark_node;
6027 : 3 : }
6028 : : else
6029 : : {
6030 : 372802 : if (variable_template_p (field))
6031 : 33 : field = DECL_TEMPLATE_RESULT (field);
6032 : :
6033 : 372802 : if (DECL_CONTEXT (field) != context)
6034 : : {
6035 : 3 : if (!same_type_p (DECL_CONTEXT (field), context))
6036 : 3 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6037 : : "to be defined as %<%T::%D%>",
6038 : 3 : DECL_CONTEXT (field), DECL_NAME (decl),
6039 : 3 : context, DECL_NAME (decl));
6040 : 3 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6041 : : }
6042 : : /* Static data member are tricky; an in-class initialization
6043 : : still doesn't provide a definition, so the in-class
6044 : : declaration will have DECL_EXTERNAL set, but will have an
6045 : : initialization. Thus, duplicate_decls won't warn
6046 : : about this situation, and so we check here. */
6047 : 372802 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6048 : 9 : error ("duplicate initialization of %qD", decl);
6049 : 372802 : field = duplicate_decls (decl, field);
6050 : 372802 : if (field == error_mark_node)
6051 : : return error_mark_node;
6052 : 372746 : else if (field)
6053 : 372746 : decl = field;
6054 : : }
6055 : : }
6056 : : else
6057 : : {
6058 : 362035 : tree field = check_classfn (context, decl,
6059 : : this_tmpl
6060 : 13 : ? current_template_parms
6061 : : : NULL_TREE);
6062 : 361950 : if (field && field != error_mark_node
6063 : 723972 : && duplicate_decls (decl, field))
6064 : 361949 : decl = field;
6065 : : }
6066 : :
6067 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6068 : 734832 : DECL_IN_AGGR_P (decl) = 0;
6069 : : /* Do not mark DECL as an explicit specialization if it was not
6070 : : already marked as an instantiation; a declaration should
6071 : : never be marked as a specialization unless we know what
6072 : : template is being specialized. */
6073 : 734832 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6074 : : {
6075 : 360317 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6076 : 360317 : if (TREE_CODE (decl) == FUNCTION_DECL)
6077 : 339526 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6078 : 339526 : && DECL_DECLARED_INLINE_P (decl));
6079 : : else
6080 : 20791 : DECL_COMDAT (decl) = false;
6081 : :
6082 : : /* [temp.expl.spec] An explicit specialization of a static data
6083 : : member of a template is a definition if the declaration
6084 : : includes an initializer; otherwise, it is a declaration.
6085 : :
6086 : : We check for processing_specialization so this only applies
6087 : : to the new specialization syntax. */
6088 : 360317 : if (!initialized && processing_specialization)
6089 : 360130 : DECL_EXTERNAL (decl) = 1;
6090 : : }
6091 : :
6092 : 1095031 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6093 : : /* Aliases are definitions. */
6094 : 734889 : && !alias)
6095 : : {
6096 : 30 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6097 : 11 : permerror (declarator->id_loc,
6098 : : "declaration of %q#D outside of class is not definition",
6099 : : decl);
6100 : 19 : else if (flag_contract_strict_declarations)
6101 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6102 : : "declaration of %q#D outside of class is not definition",
6103 : : decl);
6104 : : }
6105 : : }
6106 : :
6107 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6108 : 106911731 : if (initialized == SD_DECOMPOSITION)
6109 : 119543 : fit_decomposition_lang_decl (decl, NULL_TREE);
6110 : :
6111 : 106911731 : was_public = TREE_PUBLIC (decl);
6112 : :
6113 : 177705676 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6114 : 107314710 : && current_function_decl)
6115 : : {
6116 : : /* A function-scope decl of some namespace-scope decl. */
6117 : 35739 : DECL_LOCAL_DECL_P (decl) = true;
6118 : 35739 : if (named_module_attach_p ())
6119 : 3 : error_at (declarator->id_loc,
6120 : : "block-scope extern declaration %q#D must not be"
6121 : : " attached to a named module", decl);
6122 : : }
6123 : :
6124 : : /* Enter this declaration into the symbol table. Don't push the plain
6125 : : VAR_DECL for a variable template. */
6126 : 106911731 : if (!template_parm_scope_p ()
6127 : 106911731 : || !VAR_P (decl))
6128 : 104443774 : decl = maybe_push_decl (decl);
6129 : :
6130 : 106911731 : if (processing_template_decl)
6131 : 65617192 : decl = push_template_decl (decl);
6132 : :
6133 : 106911731 : if (decl == error_mark_node)
6134 : : return error_mark_node;
6135 : :
6136 : 106911352 : if (VAR_P (decl)
6137 : 52382015 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6138 : 107733 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6139 : : /* But not templated variables. */
6140 : 106937811 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6141 : : {
6142 : : /* This is a const variable with implicit 'static'. Set
6143 : : DECL_THIS_STATIC so we can tell it from variables that are
6144 : : !TREE_PUBLIC because of the anonymous namespace. */
6145 : 26459 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6146 : 26459 : DECL_THIS_STATIC (decl) = 1;
6147 : : }
6148 : :
6149 : 58387294 : if (current_function_decl && VAR_P (decl)
6150 : 47570250 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
6151 : 111492014 : && cxx_dialect < cxx23)
6152 : : {
6153 : 2727242 : bool ok = false;
6154 : 2727242 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6155 : 20 : error_at (DECL_SOURCE_LOCATION (decl),
6156 : : "%qD defined %<thread_local%> in %qs function only "
6157 : : "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
6158 : 10 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6159 : : ? "consteval" : "constexpr");
6160 : 2727232 : else if (TREE_STATIC (decl))
6161 : 76 : error_at (DECL_SOURCE_LOCATION (decl),
6162 : : "%qD defined %<static%> in %qs function only available "
6163 : : "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
6164 : 38 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6165 : : ? "consteval" : "constexpr");
6166 : : else
6167 : : ok = true;
6168 : 48 : if (!ok)
6169 : 48 : cp_function_chain->invalid_constexpr = true;
6170 : : }
6171 : :
6172 : 106911352 : if (!processing_template_decl && VAR_P (decl))
6173 : 5065770 : start_decl_1 (decl, initialized);
6174 : :
6175 : 106911349 : return decl;
6176 : : }
6177 : :
6178 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6179 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6180 : : variable is initialized via an implicitly-called constructor.)
6181 : : This function must be called for ordinary variables (including, for
6182 : : example, implicit instantiations of templates), but must not be
6183 : : called for template declarations. */
6184 : :
6185 : : void
6186 : 5072795 : start_decl_1 (tree decl, bool initialized)
6187 : : {
6188 : 5072795 : gcc_checking_assert (!processing_template_decl);
6189 : :
6190 : 5072795 : if (error_operand_p (decl))
6191 : : return;
6192 : :
6193 : 5072780 : gcc_checking_assert (VAR_P (decl));
6194 : :
6195 : 5072780 : tree type = TREE_TYPE (decl);
6196 : 5072780 : bool complete_p = COMPLETE_TYPE_P (type);
6197 : 5072780 : bool aggregate_definition_p
6198 : 5072780 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6199 : :
6200 : : /* If an explicit initializer is present, or if this is a definition
6201 : : of an aggregate, then we need a complete type at this point.
6202 : : (Scalars are always complete types, so there is nothing to
6203 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6204 : : are issued below. */
6205 : 5072780 : if ((initialized || aggregate_definition_p)
6206 : 4050924 : && !complete_p
6207 : 5927845 : && COMPLETE_TYPE_P (complete_type (type)))
6208 : : {
6209 : 323620 : complete_p = true;
6210 : : /* We will not yet have set TREE_READONLY on DECL if the type
6211 : : was "const", but incomplete, before this point. But, now, we
6212 : : have a complete type, so we can try again. */
6213 : 323620 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6214 : : }
6215 : :
6216 : 5072777 : if (initialized)
6217 : : /* Is it valid for this decl to have an initializer at all? */
6218 : : {
6219 : : /* Don't allow initializations for incomplete types except for
6220 : : arrays which might be completed by the initialization. */
6221 : 3453564 : if (complete_p)
6222 : : ; /* A complete type is ok. */
6223 : 531218 : else if (type_uses_auto (type))
6224 : : ; /* An auto type is ok. */
6225 : 49228 : else if (TREE_CODE (type) != ARRAY_TYPE)
6226 : : {
6227 : 24 : auto_diagnostic_group d;
6228 : 24 : error ("variable %q#D has initializer but incomplete type", decl);
6229 : 42 : maybe_suggest_missing_header (input_location,
6230 : 24 : TYPE_IDENTIFIER (type),
6231 : 24 : CP_TYPE_CONTEXT (type));
6232 : 24 : type = TREE_TYPE (decl) = error_mark_node;
6233 : 24 : }
6234 : 49204 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6235 : : {
6236 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6237 : 0 : error ("elements of array %q#D have incomplete type", decl);
6238 : : /* else we already gave an error in start_decl. */
6239 : : }
6240 : : }
6241 : 1619213 : else if (aggregate_definition_p && !complete_p)
6242 : : {
6243 : 224 : if (type_uses_auto (type))
6244 : 64 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6245 : : else
6246 : : {
6247 : 160 : auto_diagnostic_group d;
6248 : 160 : error ("aggregate %q#D has incomplete type and cannot be defined",
6249 : : decl);
6250 : 275 : maybe_suggest_missing_header (input_location,
6251 : 160 : TYPE_IDENTIFIER (type),
6252 : 160 : CP_TYPE_CONTEXT (type));
6253 : : /* Change the type so that assemble_variable will give
6254 : : DECL an rtl we can live with: (mem (const_int 0)). */
6255 : 160 : type = TREE_TYPE (decl) = error_mark_node;
6256 : 160 : }
6257 : : }
6258 : :
6259 : : /* Create a new scope to hold this declaration if necessary.
6260 : : Whether or not a new scope is necessary cannot be determined
6261 : : until after the type has been completed; if the type is a
6262 : : specialization of a class template it is not until after
6263 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6264 : : will be set correctly. */
6265 : 5072777 : maybe_push_cleanup_level (type);
6266 : : }
6267 : :
6268 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6269 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6270 : :
6271 : : tree
6272 : 238 : do_aggregate_paren_init (tree init, tree type)
6273 : : {
6274 : 238 : tree val = TREE_VALUE (init);
6275 : :
6276 : 238 : if (TREE_CHAIN (init) == NULL_TREE)
6277 : : {
6278 : : /* If the list has a single element and it's a string literal,
6279 : : then it's the initializer for the array as a whole. */
6280 : 123 : if (TREE_CODE (type) == ARRAY_TYPE
6281 : 123 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6282 : 154 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6283 : : == STRING_CST)
6284 : : return val;
6285 : : /* Handle non-standard extensions like compound literals. This also
6286 : : prevents triggering aggregate parenthesized-initialization in
6287 : : compiler-generated code for =default. */
6288 : 96 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6289 : 96 : TREE_TYPE (val)))
6290 : : return val;
6291 : : }
6292 : :
6293 : 187 : init = build_constructor_from_list (init_list_type_node, init);
6294 : 187 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6295 : 187 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6296 : 187 : return init;
6297 : : }
6298 : :
6299 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6300 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6301 : : but will be set to a new CLEANUP_STMT if a temporary is created
6302 : : that must be destroyed subsequently.
6303 : :
6304 : : Returns an initializer expression to use to initialize DECL, or
6305 : : NULL if the initialization can be performed statically.
6306 : :
6307 : : Quotes on semantics can be found in ARM 8.4.3. */
6308 : :
6309 : : static tree
6310 : 280724 : grok_reference_init (tree decl, tree type, tree init, int flags)
6311 : : {
6312 : 280724 : if (init == NULL_TREE)
6313 : : {
6314 : 15 : if ((DECL_LANG_SPECIFIC (decl) == 0
6315 : 3 : || DECL_IN_AGGR_P (decl) == 0)
6316 : 18 : && ! DECL_THIS_EXTERN (decl))
6317 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
6318 : : "%qD declared as reference but not initialized", decl);
6319 : 15 : return NULL_TREE;
6320 : : }
6321 : :
6322 : 280709 : tree ttype = TREE_TYPE (type);
6323 : 280709 : if (TREE_CODE (init) == TREE_LIST)
6324 : : {
6325 : : /* This handles (C++20 only) code like
6326 : :
6327 : : const A& r(1, 2, 3);
6328 : :
6329 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6330 : 1334 : if (TREE_TYPE (init) == NULL_TREE
6331 : 1334 : && CP_AGGREGATE_TYPE_P (ttype)
6332 : 54 : && !DECL_DECOMPOSITION_P (decl)
6333 : 1385 : && (cxx_dialect >= cxx20))
6334 : : {
6335 : : /* We don't know yet if we should treat const A& r(1) as
6336 : : const A& r{1}. */
6337 : 33 : if (list_length (init) == 1)
6338 : : {
6339 : 12 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6340 : 12 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6341 : : tf_warning_or_error);
6342 : : }
6343 : : /* If the list had more than one element, the code is ill-formed
6344 : : pre-C++20, so we can build a constructor right away. */
6345 : : else
6346 : 21 : init = do_aggregate_paren_init (init, ttype);
6347 : : }
6348 : : else
6349 : 1301 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6350 : : tf_warning_or_error);
6351 : : }
6352 : :
6353 : 280709 : if (TREE_CODE (ttype) != ARRAY_TYPE
6354 : 280709 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6355 : : /* Note: default conversion is only called in very special cases. */
6356 : 12 : init = decay_conversion (init, tf_warning_or_error);
6357 : :
6358 : : /* check_initializer handles this for non-reference variables, but for
6359 : : references we need to do it here or the initializer will get the
6360 : : incomplete array type and confuse later calls to
6361 : : cp_complete_array_type. */
6362 : 280709 : if (TREE_CODE (ttype) == ARRAY_TYPE
6363 : 9108 : && TYPE_DOMAIN (ttype) == NULL_TREE
6364 : 280792 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6365 : 50 : || TREE_CODE (init) == STRING_CST))
6366 : : {
6367 : 33 : cp_complete_array_type (&ttype, init, false);
6368 : 33 : if (ttype != TREE_TYPE (type))
6369 : 33 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6370 : : }
6371 : :
6372 : : /* Convert INIT to the reference type TYPE. This may involve the
6373 : : creation of a temporary, whose lifetime must be the same as that
6374 : : of the reference. If so, a DECL_EXPR for the temporary will be
6375 : : added just after the DECL_EXPR for DECL. That's why we don't set
6376 : : DECL_INITIAL for local references (instead assigning to them
6377 : : explicitly); we need to allow the temporary to be initialized
6378 : : first. */
6379 : 280709 : return initialize_reference (type, init, flags,
6380 : 280709 : tf_warning_or_error);
6381 : : }
6382 : :
6383 : : /* Designated initializers in arrays are not supported in GNU C++.
6384 : : The parser cannot detect this error since it does not know whether
6385 : : a given brace-enclosed initializer is for a class type or for an
6386 : : array. This function checks that CE does not use a designated
6387 : : initializer. If it does, an error is issued. Returns true if CE
6388 : : is valid, i.e., does not have a designated initializer. */
6389 : :
6390 : : bool
6391 : 28689698 : check_array_designated_initializer (constructor_elt *ce,
6392 : : unsigned HOST_WIDE_INT index)
6393 : : {
6394 : : /* Designated initializers for array elements are not supported. */
6395 : 28689698 : if (ce->index)
6396 : : {
6397 : : /* The parser only allows identifiers as designated
6398 : : initializers. */
6399 : 18190651 : if (ce->index == error_mark_node)
6400 : : {
6401 : 0 : error ("name used in a GNU-style designated "
6402 : : "initializer for an array");
6403 : 0 : return false;
6404 : : }
6405 : 18190651 : else if (identifier_p (ce->index))
6406 : : {
6407 : 17 : error ("name %qD used in a GNU-style designated "
6408 : : "initializer for an array", ce->index);
6409 : 17 : return false;
6410 : : }
6411 : :
6412 : 18190634 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6413 : : ce->index, true);
6414 : 18190634 : if (ce_index
6415 : 18190631 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6416 : 36381262 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6417 : : == INTEGER_CST))
6418 : : {
6419 : : /* A C99 designator is OK if it matches the current index. */
6420 : 18190628 : if (wi::to_wide (ce_index) == index)
6421 : : {
6422 : 18190622 : ce->index = ce_index;
6423 : 18190622 : return true;
6424 : : }
6425 : : else
6426 : 6 : sorry ("non-trivial designated initializers not supported");
6427 : : }
6428 : : else
6429 : 6 : error_at (cp_expr_loc_or_input_loc (ce->index),
6430 : : "C99 designator %qE is not an integral constant-expression",
6431 : : ce->index);
6432 : :
6433 : 12 : return false;
6434 : : }
6435 : :
6436 : : return true;
6437 : : }
6438 : :
6439 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
6440 : : array until we finish parsing the initializer. If that's the
6441 : : situation we're in, update DECL accordingly. */
6442 : :
6443 : : static void
6444 : 51178487 : maybe_deduce_size_from_array_init (tree decl, tree init)
6445 : : {
6446 : 51178487 : tree type = TREE_TYPE (decl);
6447 : :
6448 : 51178487 : if (TREE_CODE (type) == ARRAY_TYPE
6449 : 751501 : && TYPE_DOMAIN (type) == NULL_TREE
6450 : 51642912 : && TREE_CODE (decl) != TYPE_DECL)
6451 : : {
6452 : : /* do_default is really a C-ism to deal with tentative definitions.
6453 : : But let's leave it here to ease the eventual merge. */
6454 : 464425 : int do_default = !DECL_EXTERNAL (decl);
6455 : 464425 : tree initializer = init ? init : DECL_INITIAL (decl);
6456 : 464425 : int failure = 0;
6457 : :
6458 : : /* Check that there are no designated initializers in INIT, as
6459 : : those are not supported in GNU C++, and as the middle-end
6460 : : will crash if presented with a non-numeric designated
6461 : : initializer. */
6462 : 464425 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6463 : : {
6464 : 76341 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6465 : 76341 : constructor_elt *ce;
6466 : 76341 : HOST_WIDE_INT i;
6467 : 8895320 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6468 : : {
6469 : 8821366 : if (instantiation_dependent_expression_p (ce->index))
6470 : 51178487 : return;
6471 : 8821360 : if (!check_array_designated_initializer (ce, i))
6472 : 2 : failure = 1;
6473 : : /* If an un-designated initializer is type-dependent, we can't
6474 : : check brace elision yet. */
6475 : 8821360 : if (ce->index == NULL_TREE
6476 : 8821360 : && type_dependent_expression_p (ce->value))
6477 : : return;
6478 : : }
6479 : : }
6480 : :
6481 : 73954 : if (failure)
6482 : 2 : TREE_TYPE (decl) = error_mark_node;
6483 : : else
6484 : : {
6485 : 462036 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6486 : : do_default);
6487 : 462036 : if (failure == 1)
6488 : : {
6489 : 33 : error_at (cp_expr_loc_or_loc (initializer,
6490 : 30 : DECL_SOURCE_LOCATION (decl)),
6491 : : "initializer fails to determine size of %qD", decl);
6492 : : }
6493 : 462006 : else if (failure == 2)
6494 : : {
6495 : 30 : if (do_default)
6496 : : {
6497 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6498 : : "array size missing in %qD", decl);
6499 : : }
6500 : : /* If a `static' var's size isn't known, make it extern as
6501 : : well as static, so it does not get allocated. If it's not
6502 : : `static', then don't mark it extern; finish_incomplete_decl
6503 : : will give it a default size and it will get allocated. */
6504 : 24 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6505 : 0 : DECL_EXTERNAL (decl) = 1;
6506 : : }
6507 : 461976 : else if (failure == 3)
6508 : : {
6509 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6510 : : "zero-size array %qD", decl);
6511 : : }
6512 : : }
6513 : :
6514 : 462038 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6515 : :
6516 : 462038 : relayout_decl (decl);
6517 : : }
6518 : : }
6519 : :
6520 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6521 : : any appropriate error messages regarding the layout. */
6522 : :
6523 : : static void
6524 : 42892298 : layout_var_decl (tree decl)
6525 : : {
6526 : 42892298 : tree type;
6527 : :
6528 : 42892298 : type = TREE_TYPE (decl);
6529 : 42892298 : if (type == error_mark_node)
6530 : : return;
6531 : :
6532 : : /* If we haven't already laid out this declaration, do so now.
6533 : : Note that we must not call complete type for an external object
6534 : : because it's type might involve templates that we are not
6535 : : supposed to instantiate yet. (And it's perfectly valid to say
6536 : : `extern X x' for some incomplete type `X'.) */
6537 : 42892253 : if (!DECL_EXTERNAL (decl))
6538 : 34599075 : complete_type (type);
6539 : 42892253 : if (!DECL_SIZE (decl)
6540 : 2034326 : && TREE_TYPE (decl) != error_mark_node
6541 : 44926579 : && complete_or_array_type_p (type))
6542 : 1998700 : layout_decl (decl, 0);
6543 : :
6544 : 42892253 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6545 : : {
6546 : : /* An automatic variable with an incomplete type: that is an error.
6547 : : Don't talk about array types here, since we took care of that
6548 : : message in grokdeclarator. */
6549 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
6550 : : "storage size of %qD isn%'t known", decl);
6551 : 17 : TREE_TYPE (decl) = error_mark_node;
6552 : : }
6553 : : #if 0
6554 : : /* Keep this code around in case we later want to control debug info
6555 : : based on whether a type is "used". (jason 1999-11-11) */
6556 : :
6557 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6558 : : /* Let debugger know it should output info for this type. */
6559 : : note_debug_info_needed (ttype);
6560 : :
6561 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6562 : : note_debug_info_needed (DECL_CONTEXT (decl));
6563 : : #endif
6564 : :
6565 : 77491328 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6566 : 21421610 : && DECL_SIZE (decl) != NULL_TREE
6567 : 64147758 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
6568 : : {
6569 : 6 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6570 : 6 : && !DECL_LOCAL_DECL_P (decl))
6571 : 0 : constant_expression_warning (DECL_SIZE (decl));
6572 : : else
6573 : : {
6574 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
6575 : : "storage size of %qD isn%'t constant", decl);
6576 : 6 : TREE_TYPE (decl) = error_mark_node;
6577 : 6 : type = error_mark_node;
6578 : : }
6579 : : }
6580 : :
6581 : : /* If the final element initializes a flexible array field, adjust
6582 : : the size of the DECL with the initializer based on whether the
6583 : : DECL is a union or a structure. */
6584 : 42892253 : if (type != error_mark_node
6585 : 42892247 : && DECL_INITIAL (decl)
6586 : 21260920 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6587 : 3458475 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6588 : 2917965 : && DECL_SIZE (decl) != NULL_TREE
6589 : 2917965 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6590 : 2917965 : && COMPLETE_TYPE_P (type)
6591 : 2917965 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6592 : 45810218 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6593 : : {
6594 : 2917965 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6595 : 2917965 : if (elt.index)
6596 : : {
6597 : 2715339 : tree itype = TREE_TYPE (elt.index);
6598 : 2715339 : tree vtype = TREE_TYPE (elt.value);
6599 : 2715339 : if (TREE_CODE (itype) == ARRAY_TYPE
6600 : 46820 : && TYPE_DOMAIN (itype) == NULL
6601 : 250 : && TREE_CODE (vtype) == ARRAY_TYPE
6602 : 2715589 : && COMPLETE_TYPE_P (vtype))
6603 : : {
6604 : : /* For a structure, add the size of the initializer to the DECL's
6605 : : size. */
6606 : 250 : if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
6607 : : {
6608 : 235 : DECL_SIZE (decl)
6609 : 235 : = size_binop (PLUS_EXPR, DECL_SIZE (decl),
6610 : : TYPE_SIZE (vtype));
6611 : 235 : DECL_SIZE_UNIT (decl)
6612 : 470 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6613 : : TYPE_SIZE_UNIT (vtype));
6614 : : }
6615 : : /* For a union, the DECL's size is the maximum of the current size
6616 : : and the size of the initializer. */
6617 : : else
6618 : : {
6619 : 15 : DECL_SIZE (decl)
6620 : 15 : = size_binop (MAX_EXPR, DECL_SIZE (decl),
6621 : : TYPE_SIZE (vtype));
6622 : 15 : DECL_SIZE_UNIT (decl)
6623 : 30 : = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
6624 : : TYPE_SIZE_UNIT (vtype));
6625 : : }
6626 : : }
6627 : : }
6628 : : }
6629 : : }
6630 : :
6631 : : /* If a local static variable is declared in an inline function, or if
6632 : : we have a weak definition, we must endeavor to create only one
6633 : : instance of the variable at link-time. */
6634 : :
6635 : : void
6636 : 47126904 : maybe_commonize_var (tree decl)
6637 : : {
6638 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
6639 : : bindings. */
6640 : 47126904 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6641 : : return;
6642 : :
6643 : : /* Static data in a function with comdat linkage also has comdat
6644 : : linkage. */
6645 : 42410736 : if ((TREE_STATIC (decl)
6646 : 20550299 : && DECL_FUNCTION_SCOPE_P (decl)
6647 : 118566 : && vague_linkage_p (DECL_CONTEXT (decl)))
6648 : 83718255 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6649 : : {
6650 : 16471653 : if (flag_weak)
6651 : : {
6652 : : /* With weak symbols, we simply make the variable COMDAT;
6653 : : that will cause copies in multiple translations units to
6654 : : be merged. */
6655 : 16471626 : comdat_linkage (decl);
6656 : : }
6657 : : else
6658 : : {
6659 : 27 : if (DECL_INITIAL (decl) == NULL_TREE
6660 : 27 : || DECL_INITIAL (decl) == error_mark_node)
6661 : : {
6662 : : /* Without weak symbols, we can use COMMON to merge
6663 : : uninitialized variables. */
6664 : 24 : TREE_PUBLIC (decl) = 1;
6665 : 24 : DECL_COMMON (decl) = 1;
6666 : : }
6667 : : else
6668 : : {
6669 : : /* While for initialized variables, we must use internal
6670 : : linkage -- which means that multiple copies will not
6671 : : be merged. */
6672 : 3 : TREE_PUBLIC (decl) = 0;
6673 : 3 : DECL_COMMON (decl) = 0;
6674 : 3 : DECL_INTERFACE_KNOWN (decl) = 1;
6675 : 3 : const char *msg;
6676 : 3 : if (DECL_INLINE_VAR_P (decl))
6677 : : msg = G_("sorry: semantics of inline variable "
6678 : : "%q#D are wrong (you%'ll wind up with "
6679 : : "multiple copies)");
6680 : : else
6681 : : msg = G_("sorry: semantics of inline function "
6682 : : "static data %q#D are wrong (you%'ll wind "
6683 : : "up with multiple copies)");
6684 : 3 : auto_diagnostic_group d;
6685 : 3 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6686 : : msg, decl))
6687 : 3 : inform (DECL_SOURCE_LOCATION (decl),
6688 : : "you can work around this by removing the initializer");
6689 : 3 : }
6690 : : }
6691 : : }
6692 : : }
6693 : :
6694 : : /* Issue an error message if DECL is an uninitialized const variable.
6695 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6696 : : context from potential_constant_expression. Returns true if all is well,
6697 : : false otherwise. */
6698 : :
6699 : : bool
6700 : 5847858 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6701 : : tsubst_flags_t complain)
6702 : : {
6703 : 5847858 : tree type = strip_array_types (TREE_TYPE (decl));
6704 : :
6705 : : /* ``Unless explicitly declared extern, a const object does not have
6706 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
6707 : : 7.1.6 */
6708 : 5847858 : if (VAR_P (decl)
6709 : 5847858 : && !TYPE_REF_P (type)
6710 : 5827635 : && (CP_TYPE_CONST_P (type)
6711 : : /* C++20 permits trivial default initialization in constexpr
6712 : : context (P1331R2). */
6713 : 4832830 : || (cxx_dialect < cxx20
6714 : 2820855 : && (constexpr_context_p
6715 : 2519412 : || var_in_constexpr_fn (decl))))
6716 : 7256018 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6717 : : {
6718 : 625676 : tree field = default_init_uninitialized_part (type);
6719 : 625676 : if (!field)
6720 : : return true;
6721 : :
6722 : 113756 : auto_diagnostic_group d;
6723 : 113756 : bool show_notes = true;
6724 : :
6725 : 113756 : if (!constexpr_context_p || cxx_dialect >= cxx20)
6726 : : {
6727 : 110071 : if (CP_TYPE_CONST_P (type))
6728 : : {
6729 : 90 : if (complain & tf_error)
6730 : 84 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6731 : : "uninitialized %<const %D%>", decl);
6732 : : }
6733 : : else
6734 : : {
6735 : 109981 : if (!is_instantiation_of_constexpr (current_function_decl)
6736 : 109981 : && (complain & tf_error))
6737 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
6738 : : "uninitialized variable %qD in %<constexpr%> "
6739 : : "function", decl);
6740 : : else
6741 : : show_notes = false;
6742 : 109981 : cp_function_chain->invalid_constexpr = true;
6743 : : }
6744 : : }
6745 : 3685 : else if (complain & tf_error)
6746 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
6747 : : "uninitialized variable %qD in %<constexpr%> context",
6748 : : decl);
6749 : :
6750 : 113756 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6751 : : {
6752 : 62 : tree defaulted_ctor;
6753 : :
6754 : 62 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6755 : : "%q#T has no user-provided default constructor", type);
6756 : 62 : defaulted_ctor = in_class_defaulted_default_constructor (type);
6757 : 62 : if (defaulted_ctor)
6758 : 33 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6759 : : "constructor is not user-provided because it is "
6760 : : "explicitly defaulted in the class body");
6761 : 62 : inform (DECL_SOURCE_LOCATION (field),
6762 : : "and the implicitly-defined constructor does not "
6763 : : "initialize %q#D", field);
6764 : : }
6765 : :
6766 : 113756 : return false;
6767 : 113756 : }
6768 : :
6769 : : return true;
6770 : : }
6771 : :
6772 : : /* Structure holding the current initializer being processed by reshape_init.
6773 : : CUR is a pointer to the current element being processed, END is a pointer
6774 : : after the last element present in the initializer. */
6775 : : struct reshape_iter
6776 : : {
6777 : : constructor_elt *cur;
6778 : : constructor_elt *end;
6779 : : };
6780 : :
6781 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6782 : :
6783 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6784 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
6785 : : initialized as if for an aggregate class. If there are no more such fields,
6786 : : the return value will be NULL. */
6787 : :
6788 : : tree
6789 : 12555390 : next_aggregate_field (tree field)
6790 : : {
6791 : 12555390 : while (field
6792 : 43162464 : && (TREE_CODE (field) != FIELD_DECL
6793 : 8052792 : || DECL_UNNAMED_BIT_FIELD (field)
6794 : 8052664 : || (DECL_ARTIFICIAL (field)
6795 : : /* In C++17, aggregates can have bases. */
6796 : 61523 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6797 : 30607074 : field = DECL_CHAIN (field);
6798 : :
6799 : 12555390 : return field;
6800 : : }
6801 : :
6802 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6803 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6804 : : to a subobject. If there are no more such fields, the return value will be
6805 : : NULL. */
6806 : :
6807 : : tree
6808 : 1361185 : next_subobject_field (tree field)
6809 : : {
6810 : 1361185 : while (field
6811 : 24041418 : && (TREE_CODE (field) != FIELD_DECL
6812 : 746433 : || DECL_UNNAMED_BIT_FIELD (field)
6813 : 746426 : || (DECL_ARTIFICIAL (field)
6814 : 94243 : && !DECL_FIELD_IS_BASE (field)
6815 : 821 : && !DECL_VIRTUAL_P (field))))
6816 : 22680233 : field = DECL_CHAIN (field);
6817 : :
6818 : 1361185 : return field;
6819 : : }
6820 : :
6821 : : /* Return true for [dcl.init.list] direct-list-initialization from
6822 : : single element of enumeration with a fixed underlying type. */
6823 : :
6824 : : bool
6825 : 109257365 : is_direct_enum_init (tree type, tree init)
6826 : : {
6827 : 109257365 : if (cxx_dialect >= cxx17
6828 : 106812568 : && TREE_CODE (type) == ENUMERAL_TYPE
6829 : 5642038 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6830 : 3230633 : && TREE_CODE (init) == CONSTRUCTOR
6831 : 381243 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
6832 : 187743 : && CONSTRUCTOR_NELTS (init) == 1
6833 : : /* DR 2374: The single element needs to be implicitly
6834 : : convertible to the underlying type of the enum. */
6835 : 218 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6836 : 109257581 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6837 : 216 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6838 : 216 : CONSTRUCTOR_ELT (init, 0)->value,
6839 : : LOOKUP_IMPLICIT, tf_none))
6840 : : return true;
6841 : : return false;
6842 : : }
6843 : :
6844 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
6845 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6846 : : INTEGER_CST representing the size of the array minus one (the maximum index),
6847 : : or NULL_TREE if the array was declared without specifying the size. D is
6848 : : the iterator within the constructor. */
6849 : :
6850 : : static tree
6851 : 339489 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6852 : : tree first_initializer_p, tsubst_flags_t complain)
6853 : : {
6854 : 339489 : tree new_init;
6855 : 339489 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6856 : 339489 : unsigned HOST_WIDE_INT max_index_cst = 0;
6857 : 339489 : unsigned HOST_WIDE_INT index;
6858 : :
6859 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
6860 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6861 : : to build a new one. But don't reuse if not complaining; if this is
6862 : : tentative, we might also reshape to another type (95319). */
6863 : 339489 : bool reuse = (first_initializer_p
6864 : 296524 : && (complain & tf_error)
6865 : 295726 : && !CP_AGGREGATE_TYPE_P (elt_type)
6866 : 588174 : && !TREE_SIDE_EFFECTS (first_initializer_p));
6867 : 95342 : if (reuse)
6868 : : new_init = first_initializer_p;
6869 : : else
6870 : 95342 : new_init = build_constructor (init_list_type_node, NULL);
6871 : :
6872 : 339489 : if (sized_array_p)
6873 : : {
6874 : : /* Minus 1 is used for zero sized arrays. */
6875 : 250294 : if (integer_all_onesp (max_index))
6876 : : return new_init;
6877 : :
6878 : 250270 : if (tree_fits_uhwi_p (max_index))
6879 : 250270 : max_index_cst = tree_to_uhwi (max_index);
6880 : : /* sizetype is sign extended, not zero extended. */
6881 : : else
6882 : 0 : max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6883 : : }
6884 : :
6885 : : /* Loop until there are no more initializers. */
6886 : 10934891 : for (index = 0;
6887 : 10934891 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6888 : : ++index)
6889 : : {
6890 : 10595608 : tree elt_init;
6891 : 10595608 : constructor_elt *old_cur = d->cur;
6892 : :
6893 : 10595608 : if (d->cur->index)
6894 : 1296601 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6895 : 10595608 : check_array_designated_initializer (d->cur, index);
6896 : 10595608 : elt_init = reshape_init_r (elt_type, d,
6897 : : /*first_initializer_p=*/NULL_TREE,
6898 : : complain);
6899 : 10595608 : if (elt_init == error_mark_node)
6900 : 179 : return error_mark_node;
6901 : 10595429 : tree idx = size_int (index);
6902 : 10595429 : if (reuse)
6903 : : {
6904 : 9127168 : old_cur->index = idx;
6905 : 9127168 : old_cur->value = elt_init;
6906 : : }
6907 : : else
6908 : 1468261 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6909 : : idx, elt_init);
6910 : 10595429 : if (!TREE_CONSTANT (elt_init))
6911 : 130564 : TREE_CONSTANT (new_init) = false;
6912 : :
6913 : : /* This can happen with an invalid initializer (c++/54501). */
6914 : 10595429 : if (d->cur == old_cur && !sized_array_p)
6915 : : break;
6916 : : }
6917 : :
6918 : : return new_init;
6919 : : }
6920 : :
6921 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
6922 : : Parameters are the same of reshape_init_r. */
6923 : :
6924 : : static tree
6925 : 298702 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6926 : : tsubst_flags_t complain)
6927 : : {
6928 : 298702 : tree max_index = NULL_TREE;
6929 : :
6930 : 298702 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6931 : :
6932 : 298702 : if (TYPE_DOMAIN (type))
6933 : 209552 : max_index = array_type_nelts (type);
6934 : :
6935 : 298702 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6936 : 298702 : first_initializer_p, complain);
6937 : : }
6938 : :
6939 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
6940 : : Parameters are the same of reshape_init_r. */
6941 : :
6942 : : static tree
6943 : 40787 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6944 : : {
6945 : 40787 : tree max_index = NULL_TREE;
6946 : :
6947 : 40787 : gcc_assert (VECTOR_TYPE_P (type));
6948 : :
6949 : 40787 : if (COMPOUND_LITERAL_P (d->cur->value))
6950 : : {
6951 : 0 : tree value = d->cur->value;
6952 : 0 : if (!same_type_p (TREE_TYPE (value), type))
6953 : : {
6954 : 0 : if (complain & tf_error)
6955 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
6956 : 0 : TREE_TYPE (d->cur->value), type);
6957 : 0 : value = error_mark_node;
6958 : : }
6959 : 0 : ++d->cur;
6960 : 0 : return value;
6961 : : }
6962 : :
6963 : : /* For a vector, we initialize it as an array of the appropriate size. */
6964 : 40787 : if (VECTOR_TYPE_P (type))
6965 : 40787 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6966 : :
6967 : 40787 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6968 : 40787 : NULL_TREE, complain);
6969 : : }
6970 : :
6971 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6972 : : INIT, in isolation from any designator or other initializers. */
6973 : :
6974 : : static tree
6975 : 492093 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6976 : : {
6977 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6978 : : calling reshape_init, but this way can just live on the stack. */
6979 : 492093 : constructor_elt elt = { /*index=*/NULL_TREE, init };
6980 : 492093 : reshape_iter iter = { &elt, &elt + 1 };
6981 : 492093 : return reshape_init_r (type, &iter,
6982 : : /*first_initializer_p=*/NULL_TREE,
6983 : 492093 : complain);
6984 : : }
6985 : :
6986 : : /* Subroutine of reshape_init_r, processes the initializers for classes
6987 : : or union. Parameters are the same of reshape_init_r. */
6988 : :
6989 : : static tree
6990 : 3178498 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6991 : : tsubst_flags_t complain)
6992 : : {
6993 : 3178498 : tree field;
6994 : 3178498 : tree new_init;
6995 : :
6996 : 3178498 : gcc_assert (CLASS_TYPE_P (type));
6997 : :
6998 : : /* The initializer for a class is always a CONSTRUCTOR. */
6999 : 3178498 : new_init = build_constructor (init_list_type_node, NULL);
7000 : :
7001 : 3178498 : int binfo_idx = -1;
7002 : 3178498 : tree binfo = TYPE_BINFO (type);
7003 : 3178498 : tree base_binfo = NULL_TREE;
7004 : 3178498 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
7005 : : {
7006 : : /* We get here from maybe_aggr_guide for C++20 class template argument
7007 : : deduction. In this case we need to look through the binfo because a
7008 : : template doesn't have base fields. */
7009 : 304 : binfo_idx = 0;
7010 : 304 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
7011 : : }
7012 : 67 : if (base_binfo)
7013 : : field = base_binfo;
7014 : : else
7015 : 3178431 : field = next_aggregate_field (TYPE_FIELDS (type));
7016 : :
7017 : 3178498 : if (!field)
7018 : : {
7019 : : /* [dcl.init.aggr]
7020 : :
7021 : : An initializer for an aggregate member that is an
7022 : : empty class shall have the form of an empty
7023 : : initializer-list {}. */
7024 : 324 : if (!first_initializer_p)
7025 : : {
7026 : 270 : if (complain & tf_error)
7027 : 13 : error ("initializer for %qT must be brace-enclosed", type);
7028 : 270 : return error_mark_node;
7029 : : }
7030 : : return new_init;
7031 : : }
7032 : :
7033 : : /* For C++20 CTAD, handle pack expansions in the base list. */
7034 : : tree last_was_pack_expansion = NULL_TREE;
7035 : :
7036 : : /* Loop through the initializable fields, gathering initializers. */
7037 : 9707912 : while (d->cur != d->end)
7038 : : {
7039 : 6637582 : tree field_init;
7040 : 6637582 : constructor_elt *old_cur = d->cur;
7041 : 6637582 : bool direct_desig = false;
7042 : :
7043 : : /* Handle C++20 designated initializers. */
7044 : 6637582 : if (d->cur->index)
7045 : : {
7046 : 492253 : if (d->cur->index == error_mark_node)
7047 : 6 : return error_mark_node;
7048 : :
7049 : 492247 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7050 : : {
7051 : : /* We already reshaped this; we should have returned early from
7052 : : reshape_init. */
7053 : 0 : gcc_checking_assert (false);
7054 : : if (field != d->cur->index)
7055 : : {
7056 : : if (tree id = DECL_NAME (d->cur->index))
7057 : : gcc_checking_assert (d->cur->index
7058 : : == get_class_binding (type, id));
7059 : : field = d->cur->index;
7060 : : }
7061 : : }
7062 : 492247 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7063 : : {
7064 : 492241 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7065 : 492241 : field = get_class_binding (type, d->cur->index);
7066 : 492241 : direct_desig = true;
7067 : : }
7068 : : else
7069 : : {
7070 : 6 : if (complain & tf_error)
7071 : 6 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7072 : : " for class %qT", d->cur->index, type);
7073 : 6 : return error_mark_node;
7074 : : }
7075 : :
7076 : 492241 : if (!field && ANON_AGGR_TYPE_P (type))
7077 : : /* Apparently the designator isn't for a member of this anonymous
7078 : : struct, so head back to the enclosing class. */
7079 : : break;
7080 : :
7081 : 492238 : if (!field || TREE_CODE (field) != FIELD_DECL)
7082 : : {
7083 : 18 : if (complain & tf_error)
7084 : : {
7085 : 6 : if (field && TREE_CODE (field) == TREE_LIST)
7086 : : {
7087 : 3 : auto_diagnostic_group g;
7088 : 3 : error ("request for member %qD is ambiguous",
7089 : 3 : d->cur->index);
7090 : 3 : print_candidates (field);
7091 : 3 : }
7092 : : else
7093 : 3 : error ("%qT has no non-static data member named %qD", type,
7094 : 3 : d->cur->index);
7095 : : }
7096 : 18 : return error_mark_node;
7097 : : }
7098 : :
7099 : : /* If the element is an anonymous union object and the initializer
7100 : : list is a designated-initializer-list, the anonymous union object
7101 : : is initialized by the designated-initializer-list { D }, where D
7102 : : is the designated-initializer-clause naming a member of the
7103 : : anonymous union object. */
7104 : 492220 : tree ictx = DECL_CONTEXT (field);
7105 : 492220 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7106 : : {
7107 : : /* Find the anon aggr that is a direct member of TYPE. */
7108 : 139 : while (ANON_AGGR_TYPE_P (ictx))
7109 : : {
7110 : 136 : tree cctx = TYPE_CONTEXT (ictx);
7111 : 136 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7112 : 124 : goto found;
7113 : : ictx = cctx;
7114 : : }
7115 : :
7116 : : /* Not found, e.g. FIELD is a member of a base class. */
7117 : 3 : if (complain & tf_error)
7118 : 3 : error ("%qD is not a direct member of %qT", field, type);
7119 : 3 : return error_mark_node;
7120 : :
7121 : 124 : found:
7122 : : /* Now find the TYPE member with that anon aggr type. */
7123 : 124 : tree aafield = TYPE_FIELDS (type);
7124 : 1067 : for (; aafield; aafield = TREE_CHAIN (aafield))
7125 : 1067 : if (TREE_TYPE (aafield) == ictx)
7126 : : break;
7127 : 0 : gcc_assert (aafield);
7128 : : field = aafield;
7129 : : direct_desig = false;
7130 : : }
7131 : : }
7132 : :
7133 : : /* If we processed all the member of the class, we are done. */
7134 : 6637422 : if (!field)
7135 : : break;
7136 : :
7137 : 13274371 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7138 : 6637195 : ? field : NULL_TREE);
7139 : 6637195 : if (last_was_pack_expansion)
7140 : : /* Each non-trailing aggregate element that is a pack expansion is
7141 : : assumed to correspond to no elements of the initializer list. */
7142 : 19 : goto continue_;
7143 : :
7144 : 6637176 : if (direct_desig)
7145 : : {
7146 : : /* The designated field F is initialized from this one element.
7147 : :
7148 : : Note that we don't want to do this if we found the designator
7149 : : inside an anon aggr above; we use the normal code to implement:
7150 : :
7151 : : "If the element is an anonymous union member and the initializer
7152 : : list is a brace-enclosed designated- initializer-list, the element
7153 : : is initialized by the designated-initializer-list { D }, where D
7154 : : is the designated- initializer-clause naming a member of the
7155 : : anonymous union member." */
7156 : 492093 : field_init = reshape_single_init (TREE_TYPE (field),
7157 : 492093 : d->cur->value, complain);
7158 : 492093 : d->cur++;
7159 : : }
7160 : : else
7161 : 6145083 : field_init = reshape_init_r (TREE_TYPE (field), d,
7162 : : /*first_initializer_p=*/NULL_TREE,
7163 : : complain);
7164 : :
7165 : 6637176 : if (field_init == error_mark_node)
7166 : 289 : return error_mark_node;
7167 : :
7168 : 6636887 : if (d->cur == old_cur && d->cur->index)
7169 : : {
7170 : : /* This can happen with an invalid initializer for a flexible
7171 : : array member (c++/54441). */
7172 : 0 : if (complain & tf_error)
7173 : 0 : error ("invalid initializer for %q#D", field);
7174 : 0 : return error_mark_node;
7175 : : }
7176 : :
7177 : 6636887 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7178 : :
7179 : : /* [dcl.init.aggr]
7180 : :
7181 : : When a union is initialized with a brace-enclosed
7182 : : initializer, the braces shall only contain an
7183 : : initializer for the first member of the union. */
7184 : 6636887 : if (TREE_CODE (type) == UNION_TYPE)
7185 : : break;
7186 : :
7187 : 6529719 : continue_:
7188 : 6529738 : if (base_binfo)
7189 : : {
7190 : 82 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7191 : : field = base_binfo;
7192 : : else
7193 : 35 : field = next_aggregate_field (TYPE_FIELDS (type));
7194 : : }
7195 : : else
7196 : 6529656 : field = next_aggregate_field (DECL_CHAIN (field));
7197 : : }
7198 : :
7199 : : /* A trailing aggregate element that is a pack expansion is assumed to
7200 : : correspond to all remaining elements of the initializer list (if any). */
7201 : 3177852 : if (last_was_pack_expansion)
7202 : : {
7203 : 16 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7204 : : last_was_pack_expansion, d->cur->value);
7205 : 46 : while (d->cur != d->end)
7206 : 30 : d->cur++;
7207 : : }
7208 : :
7209 : : return new_init;
7210 : : }
7211 : :
7212 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7213 : : designators are not valid; either complain or return true to indicate
7214 : : that reshape_init_r should return error_mark_node. */
7215 : :
7216 : : static bool
7217 : 52131 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7218 : : {
7219 : 52131 : if (d->cur->index)
7220 : : {
7221 : 30 : if (complain & tf_error)
7222 : 45 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7223 : : "C99 designator %qE outside aggregate initializer",
7224 : : d->cur->index);
7225 : : else
7226 : : return true;
7227 : : }
7228 : : return false;
7229 : : }
7230 : :
7231 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7232 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7233 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7234 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7235 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7236 : :
7237 : : static tree
7238 : 20799861 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7239 : : tsubst_flags_t complain)
7240 : : {
7241 : 20799861 : tree init = d->cur->value;
7242 : :
7243 : 20799861 : if (error_operand_p (init))
7244 : 122 : return error_mark_node;
|