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 : :
64 : : /* Possible cases of bad specifiers type used by bad_specifiers. */
65 : : enum bad_spec_place {
66 : : BSP_VAR, /* variable */
67 : : BSP_PARM, /* parameter */
68 : : BSP_TYPE, /* type */
69 : : BSP_FIELD /* field */
70 : : };
71 : :
72 : : static const char *redeclaration_error_message (tree, tree);
73 : :
74 : : static bool decl_jump_unsafe (tree);
75 : : static void require_complete_types_for_parms (tree);
76 : : static tree grok_reference_init (tree, tree, tree, int);
77 : : static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
78 : : int, int, int, bool, int, tree, location_t);
79 : : static void check_static_variable_definition (tree, tree);
80 : : static void record_unknown_type (tree, const char *);
81 : : static int member_function_or_else (tree, tree, enum overload_flags);
82 : : static tree local_variable_p_walkfn (tree *, int *, void *);
83 : : static const char *tag_name (enum tag_types);
84 : : static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
85 : : static void maybe_deduce_size_from_array_init (tree, tree);
86 : : static void layout_var_decl (tree);
87 : : static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
88 : : static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
89 : : static void copy_type_enum (tree , tree);
90 : : static void check_function_type (tree, tree);
91 : : static void begin_destructor_body (void);
92 : : static void record_key_method_defined (tree);
93 : : static tree create_array_type_for_decl (tree, tree, tree, location_t);
94 : : static tree get_atexit_node (void);
95 : : static tree get_dso_handle_node (void);
96 : : static tree start_cleanup_fn (void);
97 : : static void end_cleanup_fn (void);
98 : : static tree cp_make_fname_decl (location_t, tree, int);
99 : : static void initialize_predefined_identifiers (void);
100 : : static tree check_special_function_return_type
101 : : (special_function_kind, tree, tree, int, const location_t*);
102 : : static tree push_cp_library_fn (enum tree_code, tree, int);
103 : : static tree build_cp_library_fn (tree, enum tree_code, tree, int);
104 : : static void store_parm_decls (tree);
105 : : static void initialize_local_var (tree, tree);
106 : : static void expand_static_init (tree, tree);
107 : : static location_t smallest_type_location (const cp_decl_specifier_seq*);
108 : : static bool identify_goto (tree, location_t, const location_t *,
109 : : diagnostic_t, bool);
110 : :
111 : : /* The following symbols are subsumed in the cp_global_trees array, and
112 : : listed here individually for documentation purposes.
113 : :
114 : : C++ extensions
115 : : tree wchar_decl_node;
116 : :
117 : : tree vtable_entry_type;
118 : : tree delta_type_node;
119 : : tree __t_desc_type_node;
120 : :
121 : : tree class_type_node;
122 : : tree unknown_type_node;
123 : :
124 : : Array type `vtable_entry_type[]'
125 : :
126 : : tree vtbl_type_node;
127 : : tree vtbl_ptr_type_node;
128 : :
129 : : Namespaces,
130 : :
131 : : tree std_node;
132 : : tree abi_node;
133 : :
134 : : A FUNCTION_DECL which can call `abort'. Not necessarily the
135 : : one that the user will declare, but sufficient to be called
136 : : by routines that want to abort the program.
137 : :
138 : : tree abort_fndecl;
139 : :
140 : : Used by RTTI
141 : : tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
142 : : tree tinfo_var_id; */
143 : :
144 : : tree cp_global_trees[CPTI_MAX];
145 : :
146 : : /* A list of objects which have constructors or destructors
147 : : which reside in namespace scope. The decl is stored in
148 : : the TREE_VALUE slot and the initializer is stored
149 : : in the TREE_PURPOSE slot. */
150 : : tree static_aggregates;
151 : :
152 : : /* Like static_aggregates, but for thread_local variables. */
153 : : tree tls_aggregates;
154 : :
155 : : /* A hash-map mapping from variable decls to the dynamic initializer for
156 : : the decl. This is currently only used by OpenMP. */
157 : : decl_tree_map *dynamic_initializers;
158 : :
159 : : /* -- end of C++ */
160 : :
161 : : /* A node for the integer constant 2. */
162 : :
163 : : tree integer_two_node;
164 : :
165 : : /* vector of static decls. */
166 : : vec<tree, va_gc> *static_decls;
167 : :
168 : : /* vector of keyed classes. */
169 : : vec<tree, va_gc> *keyed_classes;
170 : :
171 : : /* Used only for jumps to as-yet undefined labels, since jumps to
172 : : defined labels can have their validity checked immediately. */
173 : :
174 : : struct GTY((chain_next ("%h.next"))) named_label_use_entry {
175 : : struct named_label_use_entry *next;
176 : : /* The binding level to which this entry is *currently* attached.
177 : : This is initially the binding level in which the goto appeared,
178 : : but is modified as scopes are closed. */
179 : : cp_binding_level *binding_level;
180 : : /* The head of the names list that was current when the goto appeared,
181 : : or the inner scope popped. These are the decls that will *not* be
182 : : skipped when jumping to the label. */
183 : : tree names_in_scope;
184 : : /* If the use is a possible destination of a computed goto, a vec of decls
185 : : that aren't destroyed, filled in by poplevel_named_label_1. */
186 : : vec<tree,va_gc> *computed_goto;
187 : : /* The location of the goto, for error reporting. */
188 : : location_t o_goto_locus;
189 : : /* True if an OpenMP structured block scope has been closed since
190 : : the goto appeared. This means that the branch from the label will
191 : : illegally exit an OpenMP scope. */
192 : : bool in_omp_scope;
193 : : };
194 : :
195 : : /* A list of all LABEL_DECLs in the function that have names. Here so
196 : : we can clear out their names' definitions at the end of the
197 : : function, and so we can check the validity of jumps to these labels. */
198 : :
199 : : struct GTY((for_user)) named_label_entry {
200 : :
201 : : tree name; /* Name of decl. */
202 : :
203 : : tree label_decl; /* LABEL_DECL, unless deleted local label. */
204 : :
205 : : named_label_entry *outer; /* Outer shadowed chain. */
206 : :
207 : : /* The binding level to which the label is *currently* attached.
208 : : This is initially set to the binding level in which the label
209 : : is defined, but is modified as scopes are closed. */
210 : : cp_binding_level *binding_level;
211 : :
212 : : /* The head of the names list that was current when the label was
213 : : defined, or the inner scope popped. These are the decls that will
214 : : be skipped when jumping to the label. */
215 : : tree names_in_scope;
216 : :
217 : : /* A vector of all decls from all binding levels that would be
218 : : crossed by a backward branch to the label. */
219 : : vec<tree, va_gc> *bad_decls;
220 : :
221 : : /* A list of uses of the label, before the label is defined. */
222 : : named_label_use_entry *uses;
223 : :
224 : : /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for
225 : : this, it has a more specific meaning for LABEL_DECL. */
226 : : bool addressed;
227 : :
228 : : /* The following bits are set after the label is defined, and are
229 : : updated as scopes are popped. They indicate that a jump to the
230 : : label will illegally enter a scope of the given flavor. */
231 : : bool in_try_scope;
232 : : bool in_catch_scope;
233 : : bool in_omp_scope;
234 : : bool in_transaction_scope;
235 : : bool in_constexpr_if;
236 : : bool in_consteval_if;
237 : : bool in_stmt_expr;
238 : : };
239 : :
240 : : #define named_labels cp_function_chain->x_named_labels
241 : :
242 : : /* The number of function bodies which we are currently processing.
243 : : (Zero if we are at namespace scope, one inside the body of a
244 : : function, two inside the body of a function in a local class, etc.) */
245 : : int function_depth;
246 : :
247 : : /* Whether the exception-specifier is part of a function type (i.e. C++17). */
248 : : bool flag_noexcept_type;
249 : :
250 : : /* States indicating how grokdeclarator() should handle declspecs marked
251 : : with __attribute__((deprecated)). An object declared as
252 : : __attribute__((deprecated)) suppresses warnings of uses of other
253 : : deprecated items. */
254 : : enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
255 : :
256 : :
257 : : /* A list of VAR_DECLs whose type was incomplete at the time the
258 : : variable was declared. */
259 : :
260 : : struct GTY(()) incomplete_var {
261 : : tree decl;
262 : : tree incomplete_type;
263 : : };
264 : :
265 : :
266 : : static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
267 : :
268 : : /* Returns the kind of template specialization we are currently
269 : : processing, given that it's declaration contained N_CLASS_SCOPES
270 : : explicit scope qualifications. */
271 : :
272 : : tmpl_spec_kind
273 : 205051453 : current_tmpl_spec_kind (int n_class_scopes)
274 : : {
275 : 205051453 : int n_template_parm_scopes = 0;
276 : 205051453 : int seen_specialization_p = 0;
277 : 205051453 : int innermost_specialization_p = 0;
278 : 205051453 : cp_binding_level *b;
279 : :
280 : : /* Scan through the template parameter scopes. */
281 : 205051453 : for (b = current_binding_level;
282 : 257724021 : b->kind == sk_template_parms;
283 : 52672568 : b = b->level_chain)
284 : : {
285 : : /* If we see a specialization scope inside a parameter scope,
286 : : then something is wrong. That corresponds to a declaration
287 : : like:
288 : :
289 : : template <class T> template <> ...
290 : :
291 : : which is always invalid since [temp.expl.spec] forbids the
292 : : specialization of a class member template if the enclosing
293 : : class templates are not explicitly specialized as well. */
294 : 52672568 : if (b->explicit_spec_p)
295 : : {
296 : 827395 : if (n_template_parm_scopes == 0)
297 : : innermost_specialization_p = 1;
298 : : else
299 : 1091 : seen_specialization_p = 1;
300 : : }
301 : 51845173 : else if (seen_specialization_p == 1)
302 : : return tsk_invalid_member_spec;
303 : :
304 : 52672568 : ++n_template_parm_scopes;
305 : : }
306 : :
307 : : /* Handle explicit instantiations. */
308 : 205051453 : if (processing_explicit_instantiation)
309 : : {
310 : 1908973 : if (n_template_parm_scopes != 0)
311 : : /* We've seen a template parameter list during an explicit
312 : : instantiation. For example:
313 : :
314 : : template <class T> template void f(int);
315 : :
316 : : This is erroneous. */
317 : : return tsk_invalid_expl_inst;
318 : : else
319 : 1908973 : return tsk_expl_inst;
320 : : }
321 : :
322 : 203142480 : if (n_template_parm_scopes < n_class_scopes)
323 : : /* We've not seen enough template headers to match all the
324 : : specialized classes present. For example:
325 : :
326 : : template <class T> void R<T>::S<T>::f(int);
327 : :
328 : : This is invalid; there needs to be one set of template
329 : : parameters for each class. */
330 : : return tsk_insufficient_parms;
331 : 203142480 : else if (n_template_parm_scopes == n_class_scopes)
332 : : /* We're processing a non-template declaration (even though it may
333 : : be a member of a template class.) For example:
334 : :
335 : : template <class T> void S<T>::f(int);
336 : :
337 : : The `class T' matches the `S<T>', leaving no template headers
338 : : corresponding to the `f'. */
339 : : return tsk_none;
340 : 44845036 : else if (n_template_parm_scopes > n_class_scopes + 1)
341 : : /* We've got too many template headers. For example:
342 : :
343 : : template <> template <class T> void f (T);
344 : :
345 : : There need to be more enclosing classes. */
346 : : return tsk_excessive_parms;
347 : : else
348 : : /* This must be a template. It's of the form:
349 : :
350 : : template <class T> template <class U> void S<T>::f(U);
351 : :
352 : : This is a specialization if the innermost level was a
353 : : specialization; otherwise it's just a definition of the
354 : : template. */
355 : 89337990 : return innermost_specialization_p ? tsk_expl_spec : tsk_template;
356 : : }
357 : :
358 : : /* Exit the current scope. */
359 : :
360 : : void
361 : 80182029 : finish_scope (void)
362 : : {
363 : 80182029 : poplevel (0, 0, 0);
364 : 80182029 : }
365 : :
366 : : /* When a label goes out of scope, check to see if that label was used
367 : : in a valid manner, and issue any appropriate warnings or errors. */
368 : :
369 : : static void
370 : 16220 : check_label_used (tree label)
371 : : {
372 : 16220 : if (!processing_template_decl)
373 : : {
374 : 15891 : if (DECL_INITIAL (label) == NULL_TREE)
375 : : {
376 : 8 : location_t location;
377 : :
378 : 8 : error ("label %q+D used but not defined", label);
379 : 8 : location = input_location;
380 : : /* FIXME want (LOCATION_FILE (input_location), (line)0) */
381 : : /* Avoid crashing later. */
382 : 8 : define_label (location, DECL_NAME (label));
383 : : }
384 : : else
385 : 15883 : warn_for_unused_label (label);
386 : : }
387 : 16220 : }
388 : :
389 : : /* Helper function to sort named label entries in a vector by DECL_UID. */
390 : :
391 : : static int
392 : 206942 : sort_labels (const void *a, const void *b)
393 : : {
394 : 206942 : tree label1 = *(tree const *) a;
395 : 206942 : tree label2 = *(tree const *) b;
396 : :
397 : : /* DECL_UIDs can never be equal. */
398 : 206942 : return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
399 : : }
400 : :
401 : : /* At the end of a function, all labels declared within the function
402 : : go out of scope. BLOCK is the top-level block for the
403 : : function. */
404 : :
405 : : static void
406 : 128404494 : pop_labels (tree block)
407 : : {
408 : 128404494 : if (!named_labels)
409 : 128401859 : return;
410 : :
411 : : /* We need to add the labels to the block chain, so debug
412 : : information is emitted. But, we want the order to be stable so
413 : : need to sort them first. Otherwise the debug output could be
414 : : randomly ordered. I guess it's mostly stable, unless the hash
415 : : table implementation changes. */
416 : 2635 : auto_vec<tree, 32> labels (named_labels->elements ());
417 : 2635 : hash_table<named_label_hash>::iterator end (named_labels->end ());
418 : 18827 : for (hash_table<named_label_hash>::iterator iter
419 : 37654 : (named_labels->begin ()); iter != end; ++iter)
420 : : {
421 : 16192 : named_label_entry *ent = *iter;
422 : :
423 : 16192 : gcc_checking_assert (!ent->outer);
424 : 16192 : if (ent->label_decl)
425 : 15960 : labels.quick_push (ent->label_decl);
426 : 16192 : ggc_free (ent);
427 : : }
428 : 2635 : named_labels = NULL;
429 : 2635 : labels.qsort (sort_labels);
430 : :
431 : 18595 : while (labels.length ())
432 : : {
433 : 15960 : tree label = labels.pop ();
434 : :
435 : 15960 : DECL_CHAIN (label) = BLOCK_VARS (block);
436 : 15960 : BLOCK_VARS (block) = label;
437 : :
438 : 15960 : check_label_used (label);
439 : : }
440 : 2635 : }
441 : :
442 : : /* At the end of a block with local labels, restore the outer definition. */
443 : :
444 : : static void
445 : 260 : pop_local_label (tree id, tree label)
446 : : {
447 : 260 : check_label_used (label);
448 : 260 : named_label_entry **slot = named_labels->find_slot_with_hash
449 : 260 : (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
450 : 260 : named_label_entry *ent = *slot;
451 : :
452 : 260 : if (ent->outer)
453 : : ent = ent->outer;
454 : : else
455 : : {
456 : 248 : ent = ggc_cleared_alloc<named_label_entry> ();
457 : 248 : ent->name = id;
458 : : }
459 : 260 : *slot = ent;
460 : 260 : }
461 : :
462 : : /* The following two routines are used to interface to Objective-C++.
463 : : The binding level is purposely treated as an opaque type. */
464 : :
465 : : void *
466 : 0 : objc_get_current_scope (void)
467 : : {
468 : 0 : return current_binding_level;
469 : : }
470 : :
471 : : /* The following routine is used by the NeXT-style SJLJ exceptions;
472 : : variables get marked 'volatile' so as to not be clobbered by
473 : : _setjmp()/_longjmp() calls. All variables in the current scope,
474 : : as well as parent scopes up to (but not including) ENCLOSING_BLK
475 : : shall be thusly marked. */
476 : :
477 : : void
478 : 0 : objc_mark_locals_volatile (void *enclosing_blk)
479 : : {
480 : 0 : cp_binding_level *scope;
481 : :
482 : 0 : for (scope = current_binding_level;
483 : 0 : scope && scope != enclosing_blk;
484 : 0 : scope = scope->level_chain)
485 : : {
486 : 0 : tree decl;
487 : :
488 : 0 : for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
489 : 0 : objc_volatilize_decl (decl);
490 : :
491 : : /* Do not climb up past the current function. */
492 : 0 : if (scope->kind == sk_function_parms)
493 : : break;
494 : : }
495 : 0 : }
496 : :
497 : : /* True if B is the level for the condition of a constexpr if. */
498 : :
499 : : static bool
500 : 1582767 : level_for_constexpr_if (cp_binding_level *b)
501 : : {
502 : 1566821 : return (b->kind == sk_cond && b->this_entity
503 : 235 : && TREE_CODE (b->this_entity) == IF_STMT
504 : 1583002 : && IF_STMT_CONSTEXPR_P (b->this_entity));
505 : : }
506 : :
507 : : /* True if B is the level for the condition of a consteval if. */
508 : :
509 : : static bool
510 : 1582745 : level_for_consteval_if (cp_binding_level *b)
511 : : {
512 : 1566799 : return (b->kind == sk_cond && b->this_entity
513 : 211 : && TREE_CODE (b->this_entity) == IF_STMT
514 : 1582956 : && IF_STMT_CONSTEVAL_P (b->this_entity));
515 : : }
516 : :
517 : : /* Update data for defined and undefined labels when leaving a scope. */
518 : :
519 : : int
520 : 124192 : poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
521 : : {
522 : 124192 : named_label_entry *ent = *slot;
523 : 124192 : cp_binding_level *obl = bl->level_chain;
524 : :
525 : 124192 : if (ent->binding_level == bl)
526 : : {
527 : 19484 : tree decl;
528 : :
529 : : /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
530 : : TREE_LISTs representing OVERLOADs, so be careful. */
531 : 23202 : for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
532 : 3841 : ? DECL_CHAIN (decl)
533 : 123 : : TREE_CHAIN (decl)))
534 : 3718 : if (decl_jump_unsafe (decl))
535 : 1253 : vec_safe_push (ent->bad_decls, decl);
536 : :
537 : 19484 : ent->binding_level = obl;
538 : 19484 : ent->names_in_scope = obl->names;
539 : 19484 : switch (bl->kind)
540 : : {
541 : 53 : case sk_try:
542 : 53 : ent->in_try_scope = true;
543 : 53 : break;
544 : 34 : case sk_catch:
545 : 34 : ent->in_catch_scope = true;
546 : 34 : break;
547 : 320 : case sk_omp:
548 : 320 : ent->in_omp_scope = true;
549 : 320 : break;
550 : 4 : case sk_transaction:
551 : 4 : ent->in_transaction_scope = true;
552 : 4 : break;
553 : 42 : case sk_stmt_expr:
554 : 42 : ent->in_stmt_expr = true;
555 : 42 : break;
556 : 16128 : case sk_block:
557 : 16128 : if (level_for_constexpr_if (bl->level_chain))
558 : 15 : ent->in_constexpr_if = true;
559 : 16113 : else if (level_for_consteval_if (bl->level_chain))
560 : 10 : ent->in_consteval_if = true;
561 : : break;
562 : : default:
563 : : break;
564 : : }
565 : : }
566 : 104708 : else if (ent->uses)
567 : : {
568 : : struct named_label_use_entry *use;
569 : :
570 : 9426 : for (use = ent->uses; use ; use = use->next)
571 : 5256 : if (use->binding_level == bl)
572 : : {
573 : 2192 : if (auto &cg = use->computed_goto)
574 : : {
575 : 56 : if (bl->kind == sk_catch)
576 : 4 : vec_safe_push (cg, get_identifier ("catch"));
577 : 64 : for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d))
578 : 8 : if (TREE_CODE (d) == VAR_DECL && !TREE_STATIC (d)
579 : 8 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d)))
580 : 8 : vec_safe_push (cg, d);
581 : : }
582 : :
583 : 2192 : use->binding_level = obl;
584 : 2192 : use->names_in_scope = obl->names;
585 : 2192 : if (bl->kind == sk_omp)
586 : 16 : use->in_omp_scope = true;
587 : : }
588 : : }
589 : :
590 : 124192 : return 1;
591 : : }
592 : :
593 : : /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
594 : : when errors were reported, except for -Werror-unused-but-set-*. */
595 : : static int unused_but_set_errorcount;
596 : :
597 : : /* Exit a binding level.
598 : : Pop the level off, and restore the state of the identifier-decl mappings
599 : : that were in effect when this level was entered.
600 : :
601 : : If KEEP == 1, this level had explicit declarations, so
602 : : and create a "block" (a BLOCK node) for the level
603 : : to record its declarations and subblocks for symbol table output.
604 : :
605 : : If FUNCTIONBODY is nonzero, this level is the body of a function,
606 : : so create a block as if KEEP were set and also clear out all
607 : : label names.
608 : :
609 : : If REVERSE is nonzero, reverse the order of decls before putting
610 : : them into the BLOCK. */
611 : :
612 : : tree
613 : 518235706 : poplevel (int keep, int reverse, int functionbody)
614 : : {
615 : 518235706 : tree link;
616 : : /* The chain of decls was accumulated in reverse order.
617 : : Put it into forward order, just for cleanliness. */
618 : 518235706 : tree decls;
619 : 518235706 : tree subblocks;
620 : 518235706 : tree block;
621 : 518235706 : tree decl;
622 : 518235706 : scope_kind kind;
623 : :
624 : 518235706 : auto_cond_timevar tv (TV_NAME_LOOKUP);
625 : 518235967 : restart:
626 : :
627 : 518235967 : block = NULL_TREE;
628 : :
629 : 518235967 : gcc_assert (current_binding_level->kind != sk_class
630 : : && current_binding_level->kind != sk_namespace);
631 : :
632 : 518235967 : if (current_binding_level->kind == sk_cleanup)
633 : : functionbody = 0;
634 : 518235967 : subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
635 : :
636 : 518235967 : gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
637 : :
638 : : /* We used to use KEEP == 2 to indicate that the new block should go
639 : : at the beginning of the list of blocks at this binding level,
640 : : rather than the end. This hack is no longer used. */
641 : 518235967 : gcc_assert (keep == 0 || keep == 1);
642 : :
643 : 518235967 : if (current_binding_level->keep)
644 : 6563648 : keep = 1;
645 : :
646 : : /* Any uses of undefined labels, and any defined labels, now operate
647 : : under constraints of next binding contour. */
648 : 518235967 : if (cfun && !functionbody && named_labels)
649 : 39232 : named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
650 : 39232 : (current_binding_level);
651 : :
652 : : /* Get the decls in the order they were written.
653 : : Usually current_binding_level->names is in reverse order.
654 : : But parameter decls were previously put in forward order. */
655 : :
656 : 518235967 : decls = current_binding_level->names;
657 : 518235967 : if (reverse)
658 : : {
659 : 276370021 : decls = nreverse (decls);
660 : 276370021 : current_binding_level->names = decls;
661 : : }
662 : :
663 : : /* If there were any declarations or structure tags in that level,
664 : : or if this level is a function body,
665 : : create a BLOCK to record them for the life of this function. */
666 : 518235967 : block = NULL_TREE;
667 : : /* Avoid function body block if possible. */
668 : 549473866 : if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
669 : : keep = 0;
670 : 486998359 : else if (keep == 1 || functionbody)
671 : 159377382 : block = make_node (BLOCK);
672 : 159377382 : if (block != NULL_TREE)
673 : : {
674 : 159377382 : BLOCK_VARS (block) = decls;
675 : 159377382 : BLOCK_SUBBLOCKS (block) = subblocks;
676 : : }
677 : :
678 : : /* In each subblock, record that this is its superior. */
679 : 518235967 : if (keep >= 0)
680 : 580447149 : for (link = subblocks; link; link = BLOCK_CHAIN (link))
681 : 62211182 : BLOCK_SUPERCONTEXT (link) = block;
682 : :
683 : : /* Before we remove the declarations first check for unused variables. */
684 : 518235967 : if ((warn_unused_variable || warn_unused_but_set_variable)
685 : 5731685 : && current_binding_level->kind != sk_template_parms
686 : 4847979 : && !processing_template_decl)
687 : 5679270 : for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
688 : : {
689 : : /* There are cases where D itself is a TREE_LIST. See in
690 : : push_local_binding where the list of decls returned by
691 : : getdecls is built. */
692 : 2912322 : decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
693 : :
694 : 2912322 : tree type = TREE_TYPE (decl);
695 : 2912322 : if (VAR_P (decl)
696 : 338415 : && (! TREE_USED (decl) || !DECL_READ_P (decl))
697 : 2625 : && ! DECL_IN_SYSTEM_HEADER (decl)
698 : : /* For structured bindings, consider only real variables, not
699 : : subobjects. */
700 : 883 : && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
701 : 780 : : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
702 : : /* Don't warn about name-independent declarations. */
703 : 553 : && !name_independent_decl_p (decl)
704 : 463 : && type != error_mark_node
705 : 2913630 : && (!CLASS_TYPE_P (type)
706 : 57 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
707 : 4 : || lookup_attribute ("warn_unused",
708 : 4 : TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
709 : : {
710 : 425 : if (! TREE_USED (decl))
711 : : {
712 : 226 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
713 : 2 : warning_at (DECL_SOURCE_LOCATION (decl),
714 : : OPT_Wunused_variable,
715 : : "unused structured binding declaration");
716 : : else
717 : 224 : warning_at (DECL_SOURCE_LOCATION (decl),
718 : : OPT_Wunused_variable, "unused variable %qD", decl);
719 : 226 : suppress_warning (decl, OPT_Wunused_variable);
720 : : }
721 : 199 : else if (DECL_CONTEXT (decl) == current_function_decl
722 : : // For -Wunused-but-set-variable leave references alone.
723 : 171 : && !TYPE_REF_P (TREE_TYPE (decl))
724 : 370 : && errorcount == unused_but_set_errorcount)
725 : : {
726 : 159 : if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
727 : 8 : warning_at (DECL_SOURCE_LOCATION (decl),
728 : : OPT_Wunused_but_set_variable, "structured "
729 : : "binding declaration set but not used");
730 : : else
731 : 151 : warning_at (DECL_SOURCE_LOCATION (decl),
732 : : OPT_Wunused_but_set_variable,
733 : : "variable %qD set but not used", decl);
734 : 159 : unused_but_set_errorcount = errorcount;
735 : : }
736 : : }
737 : : }
738 : :
739 : : /* Remove declarations for all the DECLs in this level. */
740 : 1063113680 : for (link = decls; link; link = TREE_CHAIN (link))
741 : : {
742 : 544877713 : tree name;
743 : 544877713 : if (TREE_CODE (link) == TREE_LIST)
744 : : {
745 : 641330 : decl = TREE_VALUE (link);
746 : 641330 : name = TREE_PURPOSE (link);
747 : 641330 : gcc_checking_assert (name);
748 : : }
749 : : else
750 : : {
751 : 544236383 : decl = link;
752 : 544236383 : name = DECL_NAME (decl);
753 : : }
754 : :
755 : : /* Remove the binding. */
756 : 544877713 : if (TREE_CODE (decl) == LABEL_DECL)
757 : 260 : pop_local_label (name, decl);
758 : : else
759 : 544877453 : pop_local_binding (name, decl);
760 : : }
761 : :
762 : : /* Restore the IDENTIFIER_TYPE_VALUEs. */
763 : 518235967 : for (link = current_binding_level->type_shadowed;
764 : 709686242 : link; link = TREE_CHAIN (link))
765 : 191450275 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
766 : :
767 : : /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
768 : : list if a `using' declaration put them there. The debugging
769 : : back ends won't understand OVERLOAD, so we remove them here.
770 : : Because the BLOCK_VARS are (temporarily) shared with
771 : : CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
772 : : popped all the bindings. Also remove undeduced 'auto' decls,
773 : : which LTO doesn't understand, and can't have been used by anything. */
774 : 518235967 : if (block)
775 : : {
776 : 159377382 : tree* d;
777 : :
778 : 419637104 : for (d = &BLOCK_VARS (block); *d; )
779 : : {
780 : 260259722 : if (TREE_CODE (*d) == TREE_LIST
781 : 260259722 : || (!processing_template_decl
782 : 82874034 : && undeduced_auto_decl (*d)))
783 : 641348 : *d = TREE_CHAIN (*d);
784 : : else
785 : 259618374 : d = &DECL_CHAIN (*d);
786 : : }
787 : : }
788 : :
789 : : /* If the level being exited is the top level of a function,
790 : : check over all the labels. */
791 : 518235967 : if (functionbody)
792 : : {
793 : 128404494 : if (block)
794 : : {
795 : : /* Since this is the top level block of a function, the vars are
796 : : the function's parameters. Don't leave them in the BLOCK
797 : : because they are found in the FUNCTION_DECL instead. */
798 : 97166886 : BLOCK_VARS (block) = 0;
799 : 97166886 : pop_labels (block);
800 : : }
801 : : else
802 : 31237608 : pop_labels (subblocks);
803 : : }
804 : :
805 : 518235967 : kind = current_binding_level->kind;
806 : 518235967 : if (kind == sk_cleanup)
807 : : {
808 : 261 : tree stmt;
809 : :
810 : : /* If this is a temporary binding created for a cleanup, then we'll
811 : : have pushed a statement list level. Pop that, create a new
812 : : BIND_EXPR for the block, and insert it into the stream. */
813 : 261 : stmt = pop_stmt_list (current_binding_level->statement_list);
814 : 261 : stmt = c_build_bind_expr (input_location, block, stmt);
815 : 261 : add_stmt (stmt);
816 : : }
817 : :
818 : 518235967 : leave_scope ();
819 : 518235967 : if (functionbody)
820 : : {
821 : : /* The current function is being defined, so its DECL_INITIAL
822 : : should be error_mark_node. */
823 : 128404494 : gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
824 : 159642102 : DECL_INITIAL (current_function_decl) = block ? block : subblocks;
825 : 128404494 : if (subblocks)
826 : : {
827 : 63823471 : if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
828 : : {
829 : 7427046 : if (BLOCK_SUBBLOCKS (subblocks))
830 : 975220 : BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
831 : : }
832 : : else
833 : 23810853 : BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
834 : : }
835 : : }
836 : 389831473 : else if (block)
837 : 62210496 : current_binding_level->blocks
838 : 124420992 : = block_chainon (current_binding_level->blocks, block);
839 : :
840 : : /* If we did not make a block for the level just exited,
841 : : any blocks made for inner levels
842 : : (since they cannot be recorded as subblocks in that level)
843 : : must be carried forward so they will later become subblocks
844 : : of something else. */
845 : 327620977 : else if (subblocks)
846 : 12 : current_binding_level->blocks
847 : 24 : = block_chainon (current_binding_level->blocks, subblocks);
848 : :
849 : : /* Each and every BLOCK node created here in `poplevel' is important
850 : : (e.g. for proper debugging information) so if we created one
851 : : earlier, mark it as "used". */
852 : 518235967 : if (block)
853 : 159377382 : TREE_USED (block) = 1;
854 : :
855 : : /* All temporary bindings created for cleanups are popped silently. */
856 : 518235967 : if (kind == sk_cleanup)
857 : 261 : goto restart;
858 : :
859 : 1036471412 : return block;
860 : 518235706 : }
861 : :
862 : : /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
863 : : /* Diagnose odr-used extern inline variables without definitions
864 : : in the current TU. */
865 : :
866 : : int
867 : 143762 : wrapup_namespace_globals ()
868 : : {
869 : 143762 : if (vec<tree, va_gc> *statics = static_decls)
870 : : {
871 : 13207934 : for (tree decl : *statics)
872 : : {
873 : 13088143 : if (warn_unused_function
874 : 906793 : && TREE_CODE (decl) == FUNCTION_DECL
875 : 666611 : && DECL_INITIAL (decl) == 0
876 : 2458 : && DECL_EXTERNAL (decl)
877 : 2458 : && !TREE_PUBLIC (decl)
878 : 22 : && !DECL_ARTIFICIAL (decl)
879 : 22 : && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
880 : 13088151 : && !warning_suppressed_p (decl, OPT_Wunused_function))
881 : 8 : warning_at (DECL_SOURCE_LOCATION (decl),
882 : : OPT_Wunused_function,
883 : : "%qF declared %<static%> but never defined", decl);
884 : :
885 : 13088143 : if (VAR_P (decl)
886 : 3941845 : && DECL_EXTERNAL (decl)
887 : 1902381 : && DECL_INLINE_VAR_P (decl)
888 : 13088158 : && DECL_ODR_USED (decl))
889 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
890 : : "odr-used inline variable %qD is not defined", decl);
891 : : }
892 : :
893 : : /* Clear out the list, so we don't rescan next time. */
894 : 119791 : static_decls = NULL;
895 : :
896 : : /* Write out any globals that need to be output. */
897 : 119791 : return wrapup_global_declarations (statics->address (),
898 : 119791 : statics->length ());
899 : : }
900 : : return 0;
901 : : }
902 : :
903 : : /* In C++, you don't have to write `struct S' to refer to `S'; you
904 : : can just use `S'. We accomplish this by creating a TYPE_DECL as
905 : : if the user had written `typedef struct S S'. Create and return
906 : : the TYPE_DECL for TYPE. */
907 : :
908 : : tree
909 : 139097309 : create_implicit_typedef (tree name, tree type)
910 : : {
911 : 139097309 : tree decl;
912 : :
913 : 139097309 : decl = build_decl (input_location, TYPE_DECL, name, type);
914 : 139097309 : DECL_ARTIFICIAL (decl) = 1;
915 : : /* There are other implicit type declarations, like the one *within*
916 : : a class that allows you to write `S::S'. We must distinguish
917 : : amongst these. */
918 : 139097309 : SET_DECL_IMPLICIT_TYPEDEF_P (decl);
919 : 139097309 : TYPE_NAME (type) = decl;
920 : 139097309 : TYPE_STUB_DECL (type) = decl;
921 : :
922 : 139097309 : return decl;
923 : : }
924 : :
925 : : /* Function-scope local entities that need discriminators. Each entry
926 : : is a {decl,name} pair. VAR_DECLs for anon unions get their name
927 : : smashed, so we cannot rely on DECL_NAME. */
928 : :
929 : : static GTY((deletable)) vec<tree, va_gc> *local_entities;
930 : :
931 : : /* Determine the mangling discriminator of local DECL. There are
932 : : generally very few of these in any particular function. */
933 : :
934 : : void
935 : 641725 : determine_local_discriminator (tree decl, tree name)
936 : : {
937 : 641725 : auto_cond_timevar tv (TV_NAME_LOOKUP);
938 : 641725 : retrofit_lang_decl (decl);
939 : 641725 : tree ctx = DECL_CONTEXT (decl);
940 : 641725 : size_t nelts = vec_safe_length (local_entities);
941 : 641725 : if (name == NULL_TREE)
942 : 641654 : name = (TREE_CODE (decl) == TYPE_DECL
943 : 1051621 : && TYPE_UNNAMED_P (TREE_TYPE (decl))
944 : 1163680 : ? NULL_TREE : DECL_NAME (decl));
945 : 19180181 : for (size_t i = 0; i < nelts; i += 2)
946 : : {
947 : 18538875 : tree *pair = &(*local_entities)[i];
948 : 18538875 : tree d = pair[0];
949 : 18538875 : tree n = pair[1];
950 : 18538875 : gcc_checking_assert (d != decl);
951 : 18538875 : if (name == n
952 : 3323817 : && TREE_CODE (decl) == TREE_CODE (d)
953 : 21862680 : && ctx == DECL_CONTEXT (d))
954 : : {
955 : 419 : tree disc = integer_one_node;
956 : 419 : if (DECL_DISCRIMINATOR (d))
957 : 218 : disc = build_int_cst (TREE_TYPE (disc),
958 : 218 : TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
959 : 419 : DECL_DISCRIMINATOR (decl) = disc;
960 : : /* Replace the saved decl. */
961 : 419 : pair[0] = decl;
962 : 419 : decl = NULL_TREE;
963 : 419 : break;
964 : : }
965 : : }
966 : :
967 : 641725 : if (decl)
968 : : {
969 : 641306 : vec_safe_reserve (local_entities, 2);
970 : 641306 : local_entities->quick_push (decl);
971 : 641306 : local_entities->quick_push (name);
972 : : }
973 : 641725 : }
974 : :
975 : :
976 : : /* True if DECL is a constrained hidden friend as per [temp.friend]/9:
977 : :
978 : : A non-template friend declaration with a requires-clause shall be a
979 : : definition. A friend function template with a constraint that depends on a
980 : : template parameter from an enclosing template shall be a definition. Such a
981 : : constrained friend function or function template declaration does not
982 : : declare the same function or function template as a declaration in any other
983 : : scope.
984 : :
985 : : The ABI calls this a "member-like constrained friend" and mangles it like a
986 : : member function to avoid collisions. */
987 : :
988 : : bool
989 : 1265186887 : member_like_constrained_friend_p (tree decl)
990 : : {
991 : 1265186887 : return (TREE_CODE (decl) == FUNCTION_DECL
992 : 393318263 : && DECL_UNIQUE_FRIEND_P (decl)
993 : 68418340 : && DECL_FRIEND_CONTEXT (decl)
994 : 30479486 : && get_constraints (decl)
995 : 1265309588 : && (!DECL_TEMPLATE_INFO (decl)
996 : 122701 : || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))
997 : 666 : || (uses_outer_template_parms_in_constraints
998 : 666 : (most_general_template (decl)))));
999 : : }
1000 : :
1001 : : /* Returns true if functions FN1 and FN2 have equivalent trailing
1002 : : requires clauses. */
1003 : :
1004 : : static bool
1005 : 10785487 : function_requirements_equivalent_p (tree newfn, tree oldfn)
1006 : : {
1007 : : /* In the concepts TS, the combined constraints are compared. */
1008 : 10785487 : if (cxx_dialect < cxx20)
1009 : : {
1010 : 632 : tree ci1 = get_constraints (oldfn);
1011 : 632 : tree ci2 = get_constraints (newfn);
1012 : 670 : tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
1013 : 679 : tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
1014 : 632 : return cp_tree_equal (req1, req2);
1015 : : }
1016 : :
1017 : : /* [temp.friend]/9 "Such a constrained friend function does not declare the
1018 : : same function as a declaration in any other scope." So no need to
1019 : : actually compare the requirements. */
1020 : 10784855 : if (member_like_constrained_friend_p (newfn)
1021 : 10784855 : || member_like_constrained_friend_p (oldfn))
1022 : 21 : return false;
1023 : :
1024 : : /* Compare only trailing requirements. */
1025 : 10784834 : tree reqs1 = get_trailing_function_requirements (newfn);
1026 : 10784834 : tree reqs2 = get_trailing_function_requirements (oldfn);
1027 : 10784834 : if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
1028 : : return false;
1029 : :
1030 : : /* Substitution is needed when friends are involved. */
1031 : 10756743 : reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
1032 : 10756743 : reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
1033 : :
1034 : 10756743 : return cp_tree_equal (reqs1, reqs2);
1035 : : }
1036 : :
1037 : : /* Two functions of the same name correspond [basic.scope.scope] if
1038 : :
1039 : : + both declare functions with the same non-object-parameter-type-list,
1040 : : equivalent ([temp.over.link]) trailing requires-clauses (if any, except as
1041 : : specified in [temp.friend]), and, if both are non-static members, they have
1042 : : corresponding object parameters, or
1043 : :
1044 : : + both declare function templates with equivalent
1045 : : non-object-parameter-type-lists, return types (if any), template-heads, and
1046 : : trailing requires-clauses (if any), and, if both are non-static members,
1047 : : they have corresponding object parameters.
1048 : :
1049 : : This is a subset of decls_match: it identifies declarations that cannot be
1050 : : overloaded with one another. This function does not consider DECL_NAME. */
1051 : :
1052 : : bool
1053 : 180997098 : fns_correspond (tree newdecl, tree olddecl)
1054 : : {
1055 : 180997098 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1056 : : return false;
1057 : :
1058 : 167755937 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1059 : : {
1060 : 167741605 : if (!template_heads_equivalent_p (newdecl, olddecl))
1061 : : return 0;
1062 : 53319217 : newdecl = DECL_TEMPLATE_RESULT (newdecl);
1063 : 53319217 : olddecl = DECL_TEMPLATE_RESULT (olddecl);
1064 : : }
1065 : :
1066 : 53333549 : tree f1 = TREE_TYPE (newdecl);
1067 : 53333549 : tree f2 = TREE_TYPE (olddecl);
1068 : :
1069 : 53333549 : int rq1 = type_memfn_rqual (f1);
1070 : 53333549 : int rq2 = type_memfn_rqual (f2);
1071 : :
1072 : : /* If only one is a non-static member function, ignore ref-quals. */
1073 : 53333549 : if (TREE_CODE (f1) != TREE_CODE (f2))
1074 : : rq1 = rq2;
1075 : : /* Two non-static member functions have corresponding object parameters if:
1076 : : + exactly one is an implicit object member function with no ref-qualifier
1077 : : and the types of their object parameters ([dcl.fct]), after removing
1078 : : top-level references, are the same, or
1079 : : + their object parameters have the same type. */
1080 : : /* ??? We treat member functions of different classes as corresponding even
1081 : : though that means the object parameters have different types. */
1082 : 53333549 : else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE))
1083 : 0 : rq1 = rq2;
1084 : :
1085 : 53333549 : bool types_match = rq1 == rq2;
1086 : :
1087 : 53333549 : if (types_match)
1088 : : {
1089 : 53333549 : tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1090 : 53333549 : tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1091 : 53333549 : types_match = compparms (p1, p2);
1092 : : }
1093 : :
1094 : : /* Two function declarations match if either has a requires-clause
1095 : : then both have a requires-clause and their constraints-expressions
1096 : : are equivalent. */
1097 : 53333549 : if (types_match && flag_concepts)
1098 : 5472650 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1099 : :
1100 : : return types_match;
1101 : : }
1102 : :
1103 : : /* Subroutine of duplicate_decls: return truthvalue of whether
1104 : : or not types of these decls match.
1105 : :
1106 : : For C++, we must compare the parameter list so that `int' can match
1107 : : `int&' in a parameter position, but `int&' is not confused with
1108 : : `const int&'. */
1109 : :
1110 : : int
1111 : 858093858 : decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
1112 : : {
1113 : 858093858 : int types_match;
1114 : :
1115 : 858093858 : if (newdecl == olddecl)
1116 : : return 1;
1117 : :
1118 : 858038508 : if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1119 : : /* If the two DECLs are not even the same kind of thing, we're not
1120 : : interested in their types. */
1121 : : return 0;
1122 : :
1123 : 424191119 : gcc_assert (DECL_P (newdecl));
1124 : :
1125 : 424191119 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1126 : : {
1127 : : /* Specializations of different templates are different functions
1128 : : even if they have the same type. */
1129 : 234416817 : tree t1 = (DECL_USE_TEMPLATE (newdecl)
1130 : 234416817 : ? DECL_TI_TEMPLATE (newdecl)
1131 : 234416817 : : NULL_TREE);
1132 : 234416817 : tree t2 = (DECL_USE_TEMPLATE (olddecl)
1133 : 234416817 : ? DECL_TI_TEMPLATE (olddecl)
1134 : 234416817 : : NULL_TREE);
1135 : 234416817 : if (t1 != t2)
1136 : : return 0;
1137 : :
1138 : 232033894 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1139 : 234979734 : && ! (DECL_EXTERN_C_P (newdecl)
1140 : 2945840 : && DECL_EXTERN_C_P (olddecl)))
1141 : : return 0;
1142 : :
1143 : : /* A new declaration doesn't match a built-in one unless it
1144 : : is also extern "C". */
1145 : 214696242 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1146 : 214696242 : && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1147 : : return 0;
1148 : :
1149 : 214048709 : tree f1 = TREE_TYPE (newdecl);
1150 : 214048709 : tree f2 = TREE_TYPE (olddecl);
1151 : 214048709 : if (TREE_CODE (f1) != TREE_CODE (f2))
1152 : : return 0;
1153 : :
1154 : : /* A declaration with deduced return type should use its pre-deduction
1155 : : type for declaration matching. */
1156 : 213899794 : tree r2 = fndecl_declared_return_type (olddecl);
1157 : 213899794 : tree r1 = fndecl_declared_return_type (newdecl);
1158 : :
1159 : 213899794 : tree p1 = TYPE_ARG_TYPES (f1);
1160 : 213899794 : tree p2 = TYPE_ARG_TYPES (f2);
1161 : :
1162 : 213899794 : if (same_type_p (r1, r2))
1163 : : {
1164 : 156996774 : if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1165 : 156996742 : && fndecl_built_in_p (olddecl))
1166 : : {
1167 : 57 : types_match = self_promoting_args_p (p1);
1168 : 57 : if (p1 == void_list_node)
1169 : 4 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1170 : : }
1171 : : else
1172 : 156996616 : types_match =
1173 : 156996616 : compparms (p1, p2)
1174 : 24693670 : && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1175 : 181690053 : && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1176 : 1168461 : || comp_type_attributes (TREE_TYPE (newdecl),
1177 : 1168461 : TREE_TYPE (olddecl)) != 0);
1178 : : }
1179 : : else
1180 : : types_match = 0;
1181 : :
1182 : : /* Two function declarations match if either has a requires-clause
1183 : : then both have a requires-clause and their constraints-expressions
1184 : : are equivalent. */
1185 : 24693466 : if (types_match && flag_concepts)
1186 : 5312814 : types_match = function_requirements_equivalent_p (newdecl, olddecl);
1187 : :
1188 : : /* The decls dont match if they correspond to two different versions
1189 : : of the same function. Disallow extern "C" functions to be
1190 : : versions for now. */
1191 : 24693466 : if (types_match
1192 : 24652683 : && !DECL_EXTERN_C_P (newdecl)
1193 : 19103817 : && !DECL_EXTERN_C_P (olddecl)
1194 : 43793551 : && targetm.target_option.function_versions (newdecl, olddecl))
1195 : : {
1196 : 10800 : if (record_versions)
1197 : 21424 : maybe_version_functions (newdecl, olddecl,
1198 : 10712 : (!DECL_FUNCTION_VERSIONED (newdecl)
1199 : 20432 : || !DECL_FUNCTION_VERSIONED (olddecl)));
1200 : 10800 : return 0;
1201 : : }
1202 : : }
1203 : 189774302 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1204 : : {
1205 : 188470195 : if (!template_heads_equivalent_p (newdecl, olddecl))
1206 : : return 0;
1207 : :
1208 : 60741836 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1209 : 60741836 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1210 : :
1211 : 60741836 : if (TREE_CODE (newres) != TREE_CODE (oldres))
1212 : : return 0;
1213 : :
1214 : : /* Two template types match if they are the same. Otherwise, compare
1215 : : the underlying declarations. */
1216 : 60741829 : if (TREE_CODE (newres) == TYPE_DECL)
1217 : 28 : types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1218 : : else
1219 : 60741801 : types_match = decls_match (newres, oldres);
1220 : : }
1221 : : else
1222 : : {
1223 : : /* Need to check scope for variable declaration (VAR_DECL).
1224 : : For typedef (TYPE_DECL), scope is ignored. */
1225 : 1304107 : if (VAR_P (newdecl)
1226 : 880427 : && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1227 : : /* [dcl.link]
1228 : : Two declarations for an object with C language linkage
1229 : : with the same name (ignoring the namespace that qualify
1230 : : it) that appear in different namespace scopes refer to
1231 : : the same object. */
1232 : 1458845 : && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1233 : : return 0;
1234 : :
1235 : 1149433 : if (TREE_TYPE (newdecl) == error_mark_node)
1236 : 3 : types_match = TREE_TYPE (olddecl) == error_mark_node;
1237 : 1149430 : else if (TREE_TYPE (olddecl) == NULL_TREE)
1238 : 0 : types_match = TREE_TYPE (newdecl) == NULL_TREE;
1239 : 1149430 : else if (TREE_TYPE (newdecl) == NULL_TREE)
1240 : : types_match = 0;
1241 : : else
1242 : 1149430 : types_match = comptypes (TREE_TYPE (newdecl),
1243 : 1149430 : TREE_TYPE (olddecl),
1244 : : COMPARE_REDECLARATION);
1245 : : }
1246 : :
1247 : : return types_match;
1248 : : }
1249 : :
1250 : : /* Mark DECL as versioned if it isn't already. */
1251 : :
1252 : : static void
1253 : 21544 : maybe_mark_function_versioned (tree decl)
1254 : : {
1255 : 21544 : if (!DECL_FUNCTION_VERSIONED (decl))
1256 : : {
1257 : 1172 : DECL_FUNCTION_VERSIONED (decl) = 1;
1258 : : /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1259 : : to include the version marker. */
1260 : 1172 : if (DECL_ASSEMBLER_NAME_SET_P (decl))
1261 : 44 : mangle_decl (decl);
1262 : : }
1263 : 21544 : }
1264 : :
1265 : : /* NEWDECL and OLDDECL have identical signatures. If they are
1266 : : different versions adjust them and return true.
1267 : : If RECORD is set to true, record function versions. */
1268 : :
1269 : : bool
1270 : 136811 : maybe_version_functions (tree newdecl, tree olddecl, bool record)
1271 : : {
1272 : 136811 : if (!targetm.target_option.function_versions (newdecl, olddecl))
1273 : : return false;
1274 : :
1275 : 10748 : maybe_mark_function_versioned (olddecl);
1276 : 10748 : if (DECL_LOCAL_DECL_P (olddecl))
1277 : : {
1278 : 24 : olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1279 : 24 : maybe_mark_function_versioned (olddecl);
1280 : : }
1281 : :
1282 : 10748 : maybe_mark_function_versioned (newdecl);
1283 : 10748 : if (DECL_LOCAL_DECL_P (newdecl))
1284 : : {
1285 : : /* Unfortunately, we can get here before pushdecl naturally calls
1286 : : push_local_extern_decl_alias, so we need to call it directly. */
1287 : 24 : if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1288 : 8 : push_local_extern_decl_alias (newdecl);
1289 : 24 : newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1290 : 24 : maybe_mark_function_versioned (newdecl);
1291 : : }
1292 : :
1293 : 10748 : if (record)
1294 : 1028 : cgraph_node::record_function_versions (olddecl, newdecl);
1295 : :
1296 : : return true;
1297 : : }
1298 : :
1299 : : /* If NEWDECL is `static' and an `extern' was seen previously,
1300 : : warn about it. OLDDECL is the previous declaration.
1301 : :
1302 : : Note that this does not apply to the C++ case of declaring
1303 : : a variable `extern const' and then later `const'.
1304 : :
1305 : : Don't complain about built-in functions, since they are beyond
1306 : : the user's control. */
1307 : :
1308 : : void
1309 : 16955572 : warn_extern_redeclared_static (tree newdecl, tree olddecl)
1310 : : {
1311 : 16955572 : if (TREE_CODE (newdecl) == TYPE_DECL
1312 : 16932797 : || TREE_CODE (newdecl) == TEMPLATE_DECL
1313 : 13670842 : || TREE_CODE (newdecl) == CONST_DECL
1314 : 13670842 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1315 : 16955556 : return;
1316 : :
1317 : : /* Don't get confused by static member functions; that's a different
1318 : : use of `static'. */
1319 : 13670842 : if (TREE_CODE (newdecl) == FUNCTION_DECL
1320 : 13670842 : && DECL_STATIC_FUNCTION_P (newdecl))
1321 : : return;
1322 : :
1323 : : /* If the old declaration was `static', or the new one isn't, then
1324 : : everything is OK. */
1325 : 13527288 : if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1326 : : return;
1327 : :
1328 : : /* It's OK to declare a builtin function as `static'. */
1329 : 16 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1330 : 16 : && DECL_ARTIFICIAL (olddecl))
1331 : : return;
1332 : :
1333 : 16 : auto_diagnostic_group d;
1334 : 16 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1335 : : "%qD was declared %<extern%> and later %<static%>", newdecl))
1336 : 16 : inform (DECL_SOURCE_LOCATION (olddecl),
1337 : : "previous declaration of %qD", olddecl);
1338 : 16 : }
1339 : :
1340 : : /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1341 : : function templates. If their exception specifications do not
1342 : : match, issue a diagnostic. */
1343 : :
1344 : : static void
1345 : 16570302 : check_redeclaration_exception_specification (tree new_decl,
1346 : : tree old_decl)
1347 : : {
1348 : 16570302 : tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1349 : 16570302 : tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1350 : :
1351 : : /* Two default specs are equivalent, don't force evaluation. */
1352 : 5699417 : if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1353 : 16711460 : && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1354 : : return;
1355 : :
1356 : 16560435 : if (!type_dependent_expression_p (old_decl))
1357 : : {
1358 : 9407690 : maybe_instantiate_noexcept (new_decl);
1359 : 9407690 : maybe_instantiate_noexcept (old_decl);
1360 : : }
1361 : 16560435 : new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1362 : 16560435 : old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1363 : :
1364 : : /* [except.spec]
1365 : :
1366 : : If any declaration of a function has an exception-specification,
1367 : : all declarations, including the definition and an explicit
1368 : : specialization, of that function shall have an
1369 : : exception-specification with the same set of type-ids. */
1370 : 16560435 : if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1371 : 12995103 : && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1372 : 29555461 : && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1373 : : {
1374 : 346 : const char *const msg
1375 : : = G_("declaration of %qF has a different exception specifier");
1376 : 346 : bool complained = true;
1377 : 346 : location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1378 : 346 : auto_diagnostic_group d;
1379 : 346 : if (DECL_IN_SYSTEM_HEADER (old_decl))
1380 : 245 : complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1381 : 101 : else if (!flag_exceptions)
1382 : : /* We used to silently permit mismatched eh specs with
1383 : : -fno-exceptions, so make them a pedwarn now. */
1384 : 3 : complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1385 : : else
1386 : 98 : error_at (new_loc, msg, new_decl);
1387 : 346 : if (complained)
1388 : 105 : inform (DECL_SOURCE_LOCATION (old_decl),
1389 : : "from previous declaration %qF", old_decl);
1390 : 346 : }
1391 : : }
1392 : :
1393 : : /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1394 : : Otherwise issue diagnostics. */
1395 : :
1396 : : static bool
1397 : 16955588 : validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1398 : : {
1399 : 16955588 : old_decl = STRIP_TEMPLATE (old_decl);
1400 : 16955588 : new_decl = STRIP_TEMPLATE (new_decl);
1401 : 16955588 : if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1402 : 16932789 : || !VAR_OR_FUNCTION_DECL_P (new_decl))
1403 : : return true;
1404 : 16932789 : if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1405 : 16932789 : == DECL_DECLARED_CONSTEXPR_P (new_decl))
1406 : : {
1407 : 16901570 : if (TREE_CODE (old_decl) != FUNCTION_DECL)
1408 : : return true;
1409 : 33141212 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1410 : 16570606 : == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1411 : : return true;
1412 : : }
1413 : 31221 : if (TREE_CODE (old_decl) == FUNCTION_DECL)
1414 : : {
1415 : : /* With -fimplicit-constexpr, ignore changes in the constexpr
1416 : : keyword. */
1417 : 21 : if (flag_implicit_constexpr
1418 : 21 : && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1419 : 0 : == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1420 : : return true;
1421 : 21 : if (fndecl_built_in_p (old_decl))
1422 : : {
1423 : : /* Hide a built-in declaration. */
1424 : 0 : DECL_DECLARED_CONSTEXPR_P (old_decl)
1425 : 0 : = DECL_DECLARED_CONSTEXPR_P (new_decl);
1426 : 0 : if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1427 : 0 : SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1428 : 0 : return true;
1429 : : }
1430 : : /* 7.1.5 [dcl.constexpr]
1431 : : Note: An explicit specialization can differ from the template
1432 : : declaration with respect to the constexpr specifier. */
1433 : 21 : if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1434 : 21 : && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1435 : : return true;
1436 : :
1437 : 10 : const char *kind = "constexpr";
1438 : 10 : if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1439 : 18 : || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1440 : : kind = "consteval";
1441 : 10 : error_at (DECL_SOURCE_LOCATION (new_decl),
1442 : : "redeclaration %qD differs in %qs "
1443 : : "from previous declaration", new_decl,
1444 : : kind);
1445 : 10 : inform (DECL_SOURCE_LOCATION (old_decl),
1446 : : "previous declaration %qD", old_decl);
1447 : 10 : return false;
1448 : : }
1449 : : return true;
1450 : : }
1451 : :
1452 : : // If OLDDECL and NEWDECL are concept declarations with the same type
1453 : : // (i.e., and template parameters), but different requirements,
1454 : : // emit diagnostics and return true. Otherwise, return false.
1455 : : static inline bool
1456 : 5 : check_concept_refinement (tree olddecl, tree newdecl)
1457 : : {
1458 : 5 : if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1459 : : return false;
1460 : :
1461 : 0 : tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1462 : 0 : tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1463 : 0 : if (TREE_CODE (d1) != TREE_CODE (d2))
1464 : : return false;
1465 : :
1466 : 0 : tree t1 = TREE_TYPE (d1);
1467 : 0 : tree t2 = TREE_TYPE (d2);
1468 : 0 : if (TREE_CODE (d1) == FUNCTION_DECL)
1469 : : {
1470 : 0 : if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1471 : 0 : && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1472 : 0 : DECL_TEMPLATE_PARMS (newdecl))
1473 : 0 : && !equivalently_constrained (olddecl, newdecl))
1474 : : {
1475 : 0 : error ("cannot specialize concept %q#D", olddecl);
1476 : 0 : return true;
1477 : : }
1478 : : }
1479 : : return false;
1480 : : }
1481 : :
1482 : : /* DECL is a redeclaration of a function or function template. If
1483 : : it does have default arguments issue a diagnostic. Note: this
1484 : : function is used to enforce the requirements in C++11 8.3.6 about
1485 : : no default arguments in redeclarations. */
1486 : :
1487 : : static void
1488 : 10488060 : check_redeclaration_no_default_args (tree decl)
1489 : : {
1490 : 10488060 : gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1491 : :
1492 : 10488060 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1493 : 30916446 : t && t != void_list_node; t = TREE_CHAIN (t))
1494 : 20428458 : if (TREE_PURPOSE (t))
1495 : : {
1496 : 72 : permerror (DECL_SOURCE_LOCATION (decl),
1497 : : "redeclaration of %q#D may not have default "
1498 : : "arguments", decl);
1499 : 72 : return;
1500 : : }
1501 : : }
1502 : :
1503 : : /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1504 : : in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1505 : : the TEMPLATE_DECLs in case of function templates). This function is used
1506 : : to enforce the final part of C++17 11.3.6/4, about a single declaration:
1507 : : "If a friend declaration specifies a default argument expression, that
1508 : : declaration shall be a definition and shall be the only declaration of
1509 : : the function or function template in the translation unit." */
1510 : :
1511 : : static void
1512 : 5192681 : check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1513 : : {
1514 : 10047291 : if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1515 : : return;
1516 : :
1517 : 1767392 : for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1518 : 883696 : t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1519 : 2368926 : t1 && t1 != void_list_node;
1520 : 1485230 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1521 : 2120024 : if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1522 : 4035740 : || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1523 : : {
1524 : 28 : auto_diagnostic_group d;
1525 : 28 : if (permerror (DECL_SOURCE_LOCATION (newdecl),
1526 : : "friend declaration of %q#D specifies default "
1527 : : "arguments and isn%'t the only declaration", newdecl))
1528 : 28 : inform (DECL_SOURCE_LOCATION (olddecl),
1529 : : "previous declaration of %q#D", olddecl);
1530 : 28 : return;
1531 : 28 : }
1532 : : }
1533 : :
1534 : : /* Merge tree bits that correspond to attributes noreturn, nothrow,
1535 : : const, malloc, and pure from NEWDECL with those of OLDDECL. */
1536 : :
1537 : : static void
1538 : 16027745 : merge_attribute_bits (tree newdecl, tree olddecl)
1539 : : {
1540 : 16027745 : TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1541 : 16027745 : TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1542 : 16027745 : TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1543 : 16027745 : TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1544 : 16027745 : TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1545 : 16027745 : TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1546 : 16027745 : DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1547 : 16027745 : DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1548 : 16027745 : DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1549 : 16027745 : DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1550 : 16027745 : DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1551 : 16027745 : DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1552 : 16027745 : TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl);
1553 : 16027745 : TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl);
1554 : 16027745 : TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl);
1555 : 16027745 : TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl);
1556 : 16027745 : }
1557 : :
1558 : : #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1559 : : && lookup_attribute ("gnu_inline", \
1560 : : DECL_ATTRIBUTES (fn)))
1561 : :
1562 : : /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1563 : : ambiguates olddecl. Returns true if an error occurs. */
1564 : :
1565 : : static bool
1566 : 185208180 : duplicate_function_template_decls (tree newdecl, tree olddecl)
1567 : : {
1568 : 185208180 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1569 : 185208180 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1570 : : /* Function template declarations can be differentiated by parameter
1571 : : and return type. */
1572 : 185208180 : if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1573 : 185208180 : TYPE_ARG_TYPES (TREE_TYPE (newres)))
1574 : 185208180 : && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1575 : : TREE_TYPE (TREE_TYPE (olddecl))))
1576 : : {
1577 : : /* ... and also by their template-heads and requires-clauses. */
1578 : 2554 : if (template_heads_equivalent_p (newdecl, olddecl)
1579 : 2554 : && function_requirements_equivalent_p (newres, oldres))
1580 : : {
1581 : 0 : error ("ambiguating new declaration %q+#D", newdecl);
1582 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
1583 : : "old declaration %q#D", olddecl);
1584 : 0 : return true;
1585 : : }
1586 : :
1587 : : /* FIXME: The types are the same but the are differences
1588 : : in either the template heads or function requirements.
1589 : : We should be able to diagnose a set of common errors
1590 : : stemming from these declarations. For example:
1591 : :
1592 : : template<typename T> requires C void f(...);
1593 : : template<typename T> void f(...) requires C;
1594 : :
1595 : : These are functionally equivalent but not equivalent. */
1596 : : }
1597 : :
1598 : : return false;
1599 : : }
1600 : :
1601 : : /* OLD_PARMS is the innermost set of template parameters for some template
1602 : : declaration, and NEW_PARMS is the corresponding set of template parameters
1603 : : for a redeclaration of that template. Merge the default arguments within
1604 : : these two sets of parameters. CLASS_P is true iff the template in
1605 : : question is a class template. */
1606 : :
1607 : : bool
1608 : 7269846 : merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1609 : : {
1610 : 7269846 : gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1611 : : == TREE_VEC_LENGTH (old_parms));
1612 : 20550537 : for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1613 : : {
1614 : 13280704 : tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1615 : 13280704 : tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1616 : 13280704 : tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1617 : 13280704 : tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1618 : 13280704 : if (error_operand_p (new_parm) || error_operand_p (old_parm))
1619 : : return false;
1620 : 13280701 : if (new_default != NULL_TREE && old_default != NULL_TREE)
1621 : : {
1622 : 10 : auto_diagnostic_group d;
1623 : 10 : error ("redefinition of default argument for %q+#D", new_parm);
1624 : 10 : inform (DECL_SOURCE_LOCATION (old_parm),
1625 : : "original definition appeared here");
1626 : 10 : return false;
1627 : 10 : }
1628 : 13280691 : else if (new_default != NULL_TREE)
1629 : : /* Update the previous template parameters (which are the ones
1630 : : that will really count) with the new default value. */
1631 : 149033 : old_default = new_default;
1632 : 13131658 : else if (class_p && old_default != NULL_TREE)
1633 : : /* Update the new parameters, too; they'll be used as the
1634 : : parameters for any members. */
1635 : 886009 : new_default = old_default;
1636 : : }
1637 : : return true;
1638 : : }
1639 : :
1640 : : /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1641 : : If the redeclaration is invalid, a diagnostic is issued, and the
1642 : : error_mark_node is returned. Otherwise, OLDDECL is returned.
1643 : :
1644 : : If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1645 : : returned.
1646 : :
1647 : : HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1648 : : if the old decl was hidden.
1649 : :
1650 : : Hidden decls can be anticipated builtins, injected friends, or
1651 : : (coming soon) injected from a local-extern decl. */
1652 : :
1653 : : tree
1654 : 312231799 : duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1655 : : {
1656 : 312231799 : unsigned olddecl_uid = DECL_UID (olddecl);
1657 : 312231799 : int types_match = 0;
1658 : 312231799 : int new_defines_function = 0;
1659 : 312231799 : tree new_template_info;
1660 : 312231799 : location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1661 : 312231799 : location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1662 : :
1663 : 312231799 : if (newdecl == olddecl)
1664 : : return olddecl;
1665 : :
1666 : 311424806 : types_match = decls_match (newdecl, olddecl);
1667 : :
1668 : : /* If either the type of the new decl or the type of the old decl is an
1669 : : error_mark_node, then that implies that we have already issued an
1670 : : error (earlier) for some bogus type specification, and in that case,
1671 : : it is rather pointless to harass the user with yet more error message
1672 : : about the same declaration, so just pretend the types match here. */
1673 : 311424806 : if (TREE_TYPE (newdecl) == error_mark_node
1674 : 311424806 : || TREE_TYPE (olddecl) == error_mark_node)
1675 : : return error_mark_node;
1676 : :
1677 : : /* Check for redeclaration and other discrepancies. */
1678 : 311424773 : if (TREE_CODE (olddecl) == FUNCTION_DECL
1679 : 311424773 : && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1680 : : {
1681 : 5777051 : if (TREE_CODE (newdecl) != FUNCTION_DECL)
1682 : : {
1683 : : /* Avoid warnings redeclaring built-ins which have not been
1684 : : explicitly declared. */
1685 : 176135 : if (was_hidden)
1686 : : {
1687 : 176101 : if (TREE_PUBLIC (newdecl)
1688 : 176101 : && CP_DECL_CONTEXT (newdecl) == global_namespace)
1689 : 16 : warning_at (newdecl_loc,
1690 : : OPT_Wbuiltin_declaration_mismatch,
1691 : : "built-in function %qD declared as non-function",
1692 : : newdecl);
1693 : 176101 : return NULL_TREE;
1694 : : }
1695 : :
1696 : : /* If you declare a built-in or predefined function name as static,
1697 : : the old definition is overridden, but optionally warn this was a
1698 : : bad choice of name. */
1699 : 34 : if (! TREE_PUBLIC (newdecl))
1700 : : {
1701 : 34 : warning_at (newdecl_loc,
1702 : : OPT_Wshadow,
1703 : 34 : fndecl_built_in_p (olddecl)
1704 : : ? G_("shadowing built-in function %q#D")
1705 : : : G_("shadowing library function %q#D"), olddecl);
1706 : : /* Discard the old built-in function. */
1707 : 34 : return NULL_TREE;
1708 : : }
1709 : : /* If the built-in is not ansi, then programs can override
1710 : : it even globally without an error. */
1711 : 0 : else if (! fndecl_built_in_p (olddecl))
1712 : 0 : warning_at (newdecl_loc, 0,
1713 : : "library function %q#D redeclared as non-function %q#D",
1714 : : olddecl, newdecl);
1715 : : else
1716 : 0 : error_at (newdecl_loc,
1717 : : "declaration of %q#D conflicts with built-in "
1718 : : "declaration %q#D", newdecl, olddecl);
1719 : 0 : return NULL_TREE;
1720 : : }
1721 : 5600916 : else if (!types_match)
1722 : : {
1723 : : /* Avoid warnings redeclaring built-ins which have not been
1724 : : explicitly declared. */
1725 : 2035584 : if (was_hidden)
1726 : : {
1727 : 783649 : tree t1, t2;
1728 : :
1729 : : /* A new declaration doesn't match a built-in one unless it
1730 : : is also extern "C". */
1731 : 783649 : gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1732 : 783649 : gcc_assert (DECL_EXTERN_C_P (olddecl));
1733 : 783649 : if (!DECL_EXTERN_C_P (newdecl))
1734 : : return NULL_TREE;
1735 : :
1736 : 136116 : for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1737 : 136116 : t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1738 : 233689 : t1 || t2;
1739 : 97573 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1740 : : {
1741 : 233665 : if (!t1 || !t2)
1742 : : break;
1743 : : /* FILE, tm types are not known at the time
1744 : : we create the builtins. */
1745 : 724673 : for (unsigned i = 0;
1746 : 958122 : i < sizeof (builtin_structptr_types)
1747 : : / sizeof (builtin_structptr_type);
1748 : : ++i)
1749 : 860542 : if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1750 : : {
1751 : 135869 : tree t = TREE_VALUE (t1);
1752 : :
1753 : 135869 : if (TYPE_PTR_P (t)
1754 : 271738 : && TYPE_IDENTIFIER (TREE_TYPE (t))
1755 : 135869 : == get_identifier (builtin_structptr_types[i].str)
1756 : 271718 : && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1757 : : {
1758 : 135849 : tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1759 : :
1760 : 135849 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1761 : 135849 : = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1762 : 135849 : types_match = decls_match (newdecl, olddecl);
1763 : 135849 : if (types_match)
1764 : 135849 : return duplicate_decls (newdecl, olddecl,
1765 : 135849 : hiding, was_hidden);
1766 : 0 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1767 : : }
1768 : 20 : goto next_arg;
1769 : : }
1770 : :
1771 : 97580 : if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1772 : : break;
1773 : 97573 : next_arg:;
1774 : : }
1775 : :
1776 : 267 : warning_at (newdecl_loc,
1777 : : OPT_Wbuiltin_declaration_mismatch,
1778 : : "declaration of %q#D conflicts with built-in "
1779 : : "declaration %q#D", newdecl, olddecl);
1780 : : }
1781 : 1251935 : else if ((DECL_EXTERN_C_P (newdecl)
1782 : 20 : && DECL_EXTERN_C_P (olddecl))
1783 : 2503850 : || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1784 : 1251915 : TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1785 : : {
1786 : : /* Don't really override olddecl for __* prefixed builtins
1787 : : except for __[^b]*_chk, the compiler might be using those
1788 : : explicitly. */
1789 : 28 : if (fndecl_built_in_p (olddecl))
1790 : : {
1791 : 20 : tree id = DECL_NAME (olddecl);
1792 : 20 : const char *name = IDENTIFIER_POINTER (id);
1793 : 20 : size_t len;
1794 : :
1795 : 20 : if (name[0] == '_'
1796 : 20 : && name[1] == '_'
1797 : 40 : && (startswith (name + 2, "builtin_")
1798 : 8 : || (len = strlen (name)) <= strlen ("___chk")
1799 : 8 : || memcmp (name + len - strlen ("_chk"),
1800 : : "_chk", strlen ("_chk") + 1) != 0))
1801 : : {
1802 : 20 : if (DECL_INITIAL (newdecl))
1803 : : {
1804 : 8 : error_at (newdecl_loc,
1805 : : "definition of %q#D ambiguates built-in "
1806 : : "declaration %q#D", newdecl, olddecl);
1807 : 8 : return error_mark_node;
1808 : : }
1809 : 12 : auto_diagnostic_group d;
1810 : 12 : if (permerror (newdecl_loc,
1811 : : "new declaration %q#D ambiguates built-in"
1812 : : " declaration %q#D", newdecl, olddecl)
1813 : 12 : && flag_permissive)
1814 : 4 : inform (newdecl_loc,
1815 : : "ignoring the %q#D declaration", newdecl);
1816 : 12 : return flag_permissive ? olddecl : error_mark_node;
1817 : 12 : }
1818 : : }
1819 : :
1820 : : /* A near match; override the builtin. */
1821 : :
1822 : 8 : if (TREE_PUBLIC (newdecl))
1823 : 8 : warning_at (newdecl_loc,
1824 : : OPT_Wbuiltin_declaration_mismatch,
1825 : : "new declaration %q#D ambiguates built-in "
1826 : : "declaration %q#D", newdecl, olddecl);
1827 : : else
1828 : 0 : warning (OPT_Wshadow,
1829 : : fndecl_built_in_p (olddecl)
1830 : : ? G_("shadowing built-in function %q#D")
1831 : : : G_("shadowing library function %q#D"), olddecl);
1832 : : }
1833 : : else
1834 : : /* Discard the old built-in function. */
1835 : : return NULL_TREE;
1836 : :
1837 : : /* Replace the old RTL to avoid problems with inlining. */
1838 : 275 : COPY_DECL_RTL (newdecl, olddecl);
1839 : : }
1840 : : else
1841 : : {
1842 : : /* Even if the types match, prefer the new declarations type
1843 : : for built-ins which have not been explicitly declared,
1844 : : for exception lists, etc... */
1845 : 3565332 : tree type = TREE_TYPE (newdecl);
1846 : 3565332 : tree attribs = (*targetm.merge_type_attributes)
1847 : 3565332 : (TREE_TYPE (olddecl), type);
1848 : :
1849 : 3565332 : type = cp_build_type_attribute_variant (type, attribs);
1850 : 3565332 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1851 : : }
1852 : :
1853 : : /* If a function is explicitly declared "throw ()", propagate that to
1854 : : the corresponding builtin. */
1855 : 3565607 : if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1856 : 3420040 : && was_hidden
1857 : 3419997 : && TREE_NOTHROW (newdecl)
1858 : 6738943 : && !TREE_NOTHROW (olddecl))
1859 : : {
1860 : 27370 : enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1861 : 27370 : tree tmpdecl = builtin_decl_explicit (fncode);
1862 : 27370 : if (tmpdecl && tmpdecl != olddecl && types_match)
1863 : 27370 : TREE_NOTHROW (tmpdecl) = 1;
1864 : : }
1865 : :
1866 : : /* Whether or not the builtin can throw exceptions has no
1867 : : bearing on this declarator. */
1868 : 3565607 : TREE_NOTHROW (olddecl) = 0;
1869 : :
1870 : 3565607 : if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1871 : : {
1872 : : /* If a builtin function is redeclared as `static', merge
1873 : : the declarations, but make the original one static. */
1874 : 3 : DECL_THIS_STATIC (olddecl) = 1;
1875 : 3 : TREE_PUBLIC (olddecl) = 0;
1876 : :
1877 : : /* Make the old declaration consistent with the new one so
1878 : : that all remnants of the builtin-ness of this function
1879 : : will be banished. */
1880 : 3 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1881 : 3 : COPY_DECL_RTL (newdecl, olddecl);
1882 : : }
1883 : : }
1884 : 305647722 : else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1885 : : {
1886 : : /* C++ Standard, 3.3, clause 4:
1887 : : "[Note: a namespace name or a class template name must be unique
1888 : : in its declarative region (7.3.2, clause 14). ]" */
1889 : 76036112 : if (TREE_CODE (olddecl) == NAMESPACE_DECL
1890 : 76036085 : || TREE_CODE (newdecl) == NAMESPACE_DECL)
1891 : : /* Namespace conflicts with not namespace. */;
1892 : 44303875 : else if (DECL_TYPE_TEMPLATE_P (olddecl)
1893 : 120339944 : || DECL_TYPE_TEMPLATE_P (newdecl))
1894 : : /* Class template conflicts. */;
1895 : 76036061 : else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1896 : 44303875 : && DECL_TEMPLATE_RESULT (olddecl)
1897 : 44303875 : && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1898 : 120339930 : || (TREE_CODE (newdecl) == TEMPLATE_DECL
1899 : 31692053 : && DECL_TEMPLATE_RESULT (newdecl)
1900 : 31692053 : && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1901 : : /* Variable template conflicts. */;
1902 : 76036052 : else if (concept_definition_p (olddecl)
1903 : 76036052 : || concept_definition_p (newdecl))
1904 : : /* Concept conflicts. */;
1905 : 76036048 : else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1906 : 44341672 : && DECL_FUNCTION_TEMPLATE_P (olddecl))
1907 : 76073853 : || (TREE_CODE (olddecl) == FUNCTION_DECL
1908 : 31692064 : && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1909 : : {
1910 : : /* One is a function and the other is a template
1911 : : function. */
1912 : 75995895 : if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1913 : : return NULL_TREE;
1914 : :
1915 : : /* There can only be one! */
1916 : 56316 : if (TREE_CODE (newdecl) == TEMPLATE_DECL
1917 : 56316 : && check_raw_literal_operator (olddecl))
1918 : 3 : error_at (newdecl_loc,
1919 : : "literal operator %q#D conflicts with"
1920 : : " raw literal operator", newdecl);
1921 : 56313 : else if (check_raw_literal_operator (newdecl))
1922 : 3 : error_at (newdecl_loc,
1923 : : "raw literal operator %q#D conflicts with"
1924 : : " literal operator template", newdecl);
1925 : : else
1926 : : return NULL_TREE;
1927 : :
1928 : 6 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1929 : 6 : return error_mark_node;
1930 : : }
1931 : 2136 : else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1932 : 42287 : || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1933 : : /* A structured binding must be unique in its declarative region. */;
1934 : 37975 : else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1935 : 40152 : || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1936 : : /* One is an implicit typedef, that's ok. */
1937 : : return NULL_TREE;
1938 : :
1939 : 155 : error ("%q#D redeclared as different kind of entity", newdecl);
1940 : 155 : inform (olddecl_loc, "previous declaration %q#D", olddecl);
1941 : :
1942 : 155 : return error_mark_node;
1943 : : }
1944 : 229611610 : else if (!types_match)
1945 : : {
1946 : 216185273 : if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1947 : : /* These are certainly not duplicate declarations; they're
1948 : : from different scopes. */
1949 : : return NULL_TREE;
1950 : :
1951 : 216185213 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1952 : : {
1953 : 185208192 : tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1954 : 185208192 : tree newres = DECL_TEMPLATE_RESULT (newdecl);
1955 : :
1956 : : /* The name of a class template may not be declared to refer to
1957 : : any other template, class, function, object, namespace, value,
1958 : : or type in the same scope. */
1959 : 185208192 : if (TREE_CODE (oldres) == TYPE_DECL
1960 : 185208189 : || TREE_CODE (newres) == TYPE_DECL)
1961 : : {
1962 : 7 : error_at (newdecl_loc,
1963 : : "conflicting declaration of template %q#D", newdecl);
1964 : 7 : inform (olddecl_loc,
1965 : : "previous declaration %q#D", olddecl);
1966 : 7 : return error_mark_node;
1967 : : }
1968 : :
1969 : 185208185 : else if (TREE_CODE (oldres) == FUNCTION_DECL
1970 : 185208180 : && TREE_CODE (newres) == FUNCTION_DECL)
1971 : : {
1972 : 185208180 : if (duplicate_function_template_decls (newdecl, olddecl))
1973 : 0 : return error_mark_node;
1974 : : return NULL_TREE;
1975 : : }
1976 : 5 : else if (check_concept_refinement (olddecl, newdecl))
1977 : 0 : return error_mark_node;
1978 : : return NULL_TREE;
1979 : : }
1980 : 30977021 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
1981 : : {
1982 : 30976886 : if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1983 : : {
1984 : 68 : error_at (newdecl_loc,
1985 : : "conflicting declaration of C function %q#D",
1986 : : newdecl);
1987 : 68 : inform (olddecl_loc,
1988 : : "previous declaration %q#D", olddecl);
1989 : 68 : return error_mark_node;
1990 : : }
1991 : : /* For function versions, params and types match, but they
1992 : : are not ambiguous. */
1993 : 30976818 : else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1994 : 30966694 : && !DECL_FUNCTION_VERSIONED (olddecl))
1995 : : /* Let constrained hidden friends coexist for now, we'll
1996 : : check satisfaction later. */
1997 : 30966694 : && !member_like_constrained_friend_p (newdecl)
1998 : 30885405 : && !member_like_constrained_friend_p (olddecl)
1999 : : // The functions have the same parameter types.
2000 : 30875346 : && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2001 : 30875346 : TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
2002 : : // And the same constraints.
2003 : 30976873 : && equivalently_constrained (newdecl, olddecl))
2004 : : {
2005 : 55 : error_at (newdecl_loc,
2006 : : "ambiguating new declaration of %q#D", newdecl);
2007 : 55 : inform (olddecl_loc,
2008 : : "old declaration %q#D", olddecl);
2009 : 55 : return error_mark_node;
2010 : : }
2011 : : else
2012 : 30976763 : return NULL_TREE;
2013 : : }
2014 : : else
2015 : : {
2016 : 135 : error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
2017 : 135 : inform (olddecl_loc,
2018 : : "previous declaration as %q#D", olddecl);
2019 : 135 : return error_mark_node;
2020 : : }
2021 : : }
2022 : 13426337 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2023 : 13426337 : && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
2024 : : {
2025 : : /* OMP UDRs are never duplicates. */
2026 : 36 : gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
2027 : 36 : error_at (newdecl_loc,
2028 : : "redeclaration of %<pragma omp declare reduction%>");
2029 : 36 : inform (olddecl_loc,
2030 : : "previous %<pragma omp declare reduction%> declaration");
2031 : 36 : return error_mark_node;
2032 : : }
2033 : 13426301 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2034 : 13426301 : && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
2035 : 232 : && (!DECL_TEMPLATE_INFO (newdecl)
2036 : 232 : || (DECL_TI_TEMPLATE (newdecl)
2037 : 232 : != DECL_TI_TEMPLATE (olddecl))))
2038 : 9743381 : || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
2039 : 542840 : && (!DECL_TEMPLATE_INFO (olddecl)
2040 : 542840 : || (DECL_TI_TEMPLATE (olddecl)
2041 : 542840 : != DECL_TI_TEMPLATE (newdecl))))))
2042 : : /* It's OK to have a template specialization and a non-template
2043 : : with the same type, or to have specializations of two
2044 : : different templates with the same type. Note that if one is a
2045 : : specialization, and the other is an instantiation of the same
2046 : : template, that we do not exit at this point. That situation
2047 : : can occur if we instantiate a template class, and then
2048 : : specialize one of its methods. This situation is valid, but
2049 : : the declarations must be merged in the usual way. */
2050 : : return NULL_TREE;
2051 : 13426301 : else if (TREE_CODE (newdecl) == FUNCTION_DECL
2052 : 13426301 : && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
2053 : 542679 : && !DECL_USE_TEMPLATE (newdecl))
2054 : 9743381 : || (DECL_TEMPLATE_INSTANTIATION (newdecl)
2055 : 71 : && !DECL_USE_TEMPLATE (olddecl))))
2056 : : /* One of the declarations is a template instantiation, and the
2057 : : other is not a template at all. That's OK. */
2058 : : return NULL_TREE;
2059 : 13426301 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2060 : : {
2061 : : /* In [namespace.alias] we have:
2062 : :
2063 : : In a declarative region, a namespace-alias-definition can be
2064 : : used to redefine a namespace-alias declared in that declarative
2065 : : region to refer only to the namespace to which it already
2066 : : refers.
2067 : :
2068 : : Therefore, if we encounter a second alias directive for the same
2069 : : alias, we can just ignore the second directive. */
2070 : 24 : if (DECL_NAMESPACE_ALIAS (newdecl)
2071 : 24 : && (DECL_NAMESPACE_ALIAS (newdecl)
2072 : 20 : == DECL_NAMESPACE_ALIAS (olddecl)))
2073 : : return olddecl;
2074 : :
2075 : : /* Leave it to update_binding to merge or report error. */
2076 : : return NULL_TREE;
2077 : : }
2078 : : else
2079 : : {
2080 : 13426277 : const char *errmsg = redeclaration_error_message (newdecl, olddecl);
2081 : 13426277 : if (errmsg)
2082 : : {
2083 : 577 : auto_diagnostic_group d;
2084 : 577 : error_at (newdecl_loc, errmsg, newdecl);
2085 : 577 : if (DECL_NAME (olddecl) != NULL_TREE)
2086 : 577 : inform (olddecl_loc,
2087 : 577 : (DECL_INITIAL (olddecl) && namespace_bindings_p ())
2088 : : ? G_("%q#D previously defined here")
2089 : : : G_("%q#D previously declared here"), olddecl);
2090 : 577 : if (cxx_dialect >= cxx26
2091 : 0 : && DECL_NAME (newdecl)
2092 : 0 : && id_equal (DECL_NAME (newdecl), "_")
2093 : 577 : && !name_independent_decl_p (newdecl))
2094 : : {
2095 : 0 : if (TREE_CODE (newdecl) == PARM_DECL)
2096 : 0 : inform (newdecl_loc,
2097 : : "parameter declaration is not name-independent");
2098 : 0 : else if (DECL_DECOMPOSITION_P (newdecl))
2099 : : {
2100 : 0 : if (at_namespace_scope_p ())
2101 : 0 : inform (newdecl_loc,
2102 : : "structured binding at namespace scope is not "
2103 : : "name-independent");
2104 : 0 : else if (TREE_STATIC (newdecl))
2105 : 0 : inform (newdecl_loc,
2106 : : "static structured binding is not "
2107 : : "name-independent");
2108 : 0 : else if (DECL_EXTERNAL (newdecl))
2109 : 0 : inform (newdecl_loc,
2110 : : "extern structured binding is not "
2111 : : "name-independent");
2112 : : }
2113 : 0 : else if (at_class_scope_p ()
2114 : : && VAR_P (newdecl)
2115 : 0 : && TREE_STATIC (newdecl))
2116 : 0 : inform (newdecl_loc,
2117 : : "static data member is not name-independent");
2118 : 0 : else if (VAR_P (newdecl) && at_namespace_scope_p ())
2119 : 0 : inform (newdecl_loc,
2120 : : "variable at namespace scope is not name-independent");
2121 : 0 : else if (VAR_P (newdecl) && TREE_STATIC (newdecl))
2122 : 0 : inform (newdecl_loc,
2123 : : "static variable is not name-independent");
2124 : 0 : else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl))
2125 : 0 : inform (newdecl_loc,
2126 : : "extern variable is not name-independent");
2127 : : }
2128 : 577 : return error_mark_node;
2129 : 577 : }
2130 : 13425700 : else if (TREE_CODE (olddecl) == FUNCTION_DECL
2131 : 9743088 : && DECL_INITIAL (olddecl) != NULL_TREE
2132 : 11832 : && !prototype_p (TREE_TYPE (olddecl))
2133 : 13425700 : && prototype_p (TREE_TYPE (newdecl)))
2134 : : {
2135 : : /* Prototype decl follows defn w/o prototype. */
2136 : 0 : auto_diagnostic_group d;
2137 : 0 : if (warning_at (newdecl_loc, 0,
2138 : : "prototype specified for %q#D", newdecl))
2139 : 0 : inform (olddecl_loc,
2140 : : "previous non-prototype definition here");
2141 : 0 : }
2142 : 13063537 : else if (VAR_OR_FUNCTION_DECL_P (olddecl)
2143 : 23168788 : && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2144 : : {
2145 : : /* [dcl.link]
2146 : : If two declarations of the same function or object
2147 : : specify different linkage-specifications ..., the program
2148 : : is ill-formed.... Except for functions with C++ linkage,
2149 : : a function declaration without a linkage specification
2150 : : shall not precede the first linkage specification for
2151 : : that function. A function can be declared without a
2152 : : linkage specification after an explicit linkage
2153 : : specification has been seen; the linkage explicitly
2154 : : specified in the earlier declaration is not affected by
2155 : : such a function declaration.
2156 : :
2157 : : DR 563 raises the question why the restrictions on
2158 : : functions should not also apply to objects. Older
2159 : : versions of G++ silently ignore the linkage-specification
2160 : : for this example:
2161 : :
2162 : : namespace N {
2163 : : extern int i;
2164 : : extern "C" int i;
2165 : : }
2166 : :
2167 : : which is clearly wrong. Therefore, we now treat objects
2168 : : like functions. */
2169 : 1752 : if (current_lang_depth () == 0)
2170 : : {
2171 : : /* There is no explicit linkage-specification, so we use
2172 : : the linkage from the previous declaration. */
2173 : 1744 : retrofit_lang_decl (newdecl);
2174 : 1744 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2175 : : }
2176 : : else
2177 : : {
2178 : 8 : auto_diagnostic_group d;
2179 : 8 : error_at (newdecl_loc,
2180 : : "conflicting declaration of %q#D with %qL linkage",
2181 : 8 : newdecl, DECL_LANGUAGE (newdecl));
2182 : 8 : inform (olddecl_loc,
2183 : : "previous declaration with %qL linkage",
2184 : 8 : DECL_LANGUAGE (olddecl));
2185 : 8 : }
2186 : : }
2187 : :
2188 : 13425700 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2189 : : ;
2190 : 12862958 : else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2191 : : {
2192 : : /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2193 : 10749723 : if (DECL_FUNCTION_MEMBER_P (olddecl)
2194 : 9200187 : && (/* grokfndecl passes member function templates too
2195 : : as FUNCTION_DECLs. */
2196 : 7794071 : DECL_TEMPLATE_INFO (olddecl)
2197 : : /* C++11 8.3.6/6.
2198 : : Default arguments for a member function of a class
2199 : : template shall be specified on the initial declaration
2200 : : of the member function within the class template. */
2201 : 622999 : || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2202 : : {
2203 : 7247783 : check_redeclaration_no_default_args (newdecl);
2204 : :
2205 : 7247783 : if (DECL_TEMPLATE_INFO (olddecl)
2206 : 7247783 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2207 : : {
2208 : 2014984 : tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2209 : 2014984 : ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2210 : 2014984 : : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2211 : 2014984 : tree old_parms
2212 : 2014984 : = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2213 : 2014984 : merge_default_template_args (new_parms, old_parms,
2214 : : /*class_p=*/false);
2215 : : }
2216 : : }
2217 : : else
2218 : : {
2219 : 1952404 : tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2220 : 1952404 : tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2221 : 1952404 : int i = 1;
2222 : :
2223 : 5280802 : for (; t1 && t1 != void_list_node;
2224 : 3328398 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2225 : 3330161 : if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2226 : : {
2227 : 52 : if (simple_cst_equal (TREE_PURPOSE (t1),
2228 : 52 : TREE_PURPOSE (t2)) == 1)
2229 : : {
2230 : 16 : auto_diagnostic_group d;
2231 : 16 : if (permerror (newdecl_loc,
2232 : : "default argument given for parameter "
2233 : : "%d of %q#D", i, newdecl))
2234 : 16 : inform (olddecl_loc,
2235 : : "previous specification in %q#D here",
2236 : : olddecl);
2237 : 16 : }
2238 : : else
2239 : : {
2240 : 36 : auto_diagnostic_group d;
2241 : 36 : error_at (newdecl_loc,
2242 : : "default argument given for parameter %d "
2243 : : "of %q#D", i, newdecl);
2244 : 36 : inform (olddecl_loc,
2245 : : "previous specification in %q#D here",
2246 : : olddecl);
2247 : 36 : }
2248 : : }
2249 : :
2250 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2251 : : argument expression, that declaration... shall be the only
2252 : : declaration of the function or function template in the
2253 : : translation unit." */
2254 : 1952404 : check_no_redeclaration_friend_default_args (olddecl, newdecl);
2255 : : }
2256 : : }
2257 : : }
2258 : :
2259 : : /* Do not merge an implicit typedef with an explicit one. In:
2260 : :
2261 : : class A;
2262 : : ...
2263 : : typedef class A A __attribute__ ((foo));
2264 : :
2265 : : the attribute should apply only to the typedef. */
2266 : 16991307 : if (TREE_CODE (olddecl) == TYPE_DECL
2267 : 16991307 : && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2268 : 22778 : || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2269 : : return NULL_TREE;
2270 : :
2271 : 16955602 : if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2272 : : return NULL_TREE;
2273 : :
2274 : 16955588 : if (!validate_constexpr_redeclaration (olddecl, newdecl))
2275 : 10 : return error_mark_node;
2276 : :
2277 : 16955578 : if (modules_p ()
2278 : 71046 : && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2279 : 40114 : && TREE_CODE (olddecl) != NAMESPACE_DECL
2280 : 16995692 : && !hiding)
2281 : : {
2282 : 36787 : if (!module_may_redeclare (olddecl))
2283 : : {
2284 : 6 : if (DECL_ARTIFICIAL (olddecl))
2285 : 0 : error ("declaration %qD conflicts with builtin", newdecl);
2286 : : else
2287 : : {
2288 : 6 : error ("declaration %qD conflicts with import", newdecl);
2289 : 6 : inform (olddecl_loc, "import declared %q#D here", olddecl);
2290 : : }
2291 : :
2292 : 6 : return error_mark_node;
2293 : : }
2294 : :
2295 : 36781 : tree not_tmpl = STRIP_TEMPLATE (olddecl);
2296 : 36781 : if (DECL_LANG_SPECIFIC (not_tmpl)
2297 : 36739 : && DECL_MODULE_ATTACH_P (not_tmpl)
2298 : : /* Typedefs are not entities and so are OK to be redeclared
2299 : : as exported: see [module.interface]/p6. */
2300 : 36970 : && TREE_CODE (olddecl) != TYPE_DECL)
2301 : : {
2302 : 177 : if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (newdecl))
2303 : 177 : && !DECL_MODULE_EXPORT_P (not_tmpl))
2304 : : {
2305 : 15 : auto_diagnostic_group d;
2306 : 15 : error ("conflicting exporting for declaration %qD", newdecl);
2307 : 15 : inform (olddecl_loc,
2308 : : "previously declared here without exporting");
2309 : 15 : }
2310 : : }
2311 : 36604 : else if (DECL_MODULE_EXPORT_P (newdecl))
2312 : 17865 : DECL_MODULE_EXPORT_P (not_tmpl) = true;
2313 : : }
2314 : :
2315 : : /* We have committed to returning OLDDECL at this point. */
2316 : :
2317 : : /* If new decl is `static' and an `extern' was seen previously,
2318 : : warn about it. */
2319 : 16955572 : warn_extern_redeclared_static (newdecl, olddecl);
2320 : :
2321 : : /* True to merge attributes between the declarations, false to
2322 : : set OLDDECL's attributes to those of NEWDECL (for template
2323 : : explicit specializations that specify their own attributes
2324 : : independent of those specified for the primary template). */
2325 : 16955572 : const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2326 : 13308679 : || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2327 : 17498396 : || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2328 : :
2329 : 16955572 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2330 : : {
2331 : 13308679 : if (merge_attr)
2332 : : {
2333 : 12766071 : if (diagnose_mismatched_attributes (olddecl, newdecl))
2334 : 48 : inform (olddecl_loc, DECL_INITIAL (olddecl)
2335 : : ? G_("previous definition of %qD here")
2336 : : : G_("previous declaration of %qD here"), olddecl);
2337 : :
2338 : : /* [dcl.attr.noreturn]: The first declaration of a function shall
2339 : : specify the noreturn attribute if any declaration of that function
2340 : : specifies the noreturn attribute. */
2341 : 12766071 : tree a;
2342 : 12766071 : if (TREE_THIS_VOLATILE (newdecl)
2343 : 104918 : && !TREE_THIS_VOLATILE (olddecl)
2344 : : /* This applies to [[noreturn]] only, not its GNU variants. */
2345 : 91 : && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2346 : 62 : && cxx11_attribute_p (a)
2347 : 12766092 : && get_attribute_namespace (a) == NULL_TREE)
2348 : : {
2349 : 9 : error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2350 : : "but its first declaration was not", newdecl);
2351 : 9 : inform (olddecl_loc, "previous declaration of %qD", olddecl);
2352 : : }
2353 : : }
2354 : :
2355 : : /* Now that functions must hold information normally held
2356 : : by field decls, there is extra work to do so that
2357 : : declaration information does not get destroyed during
2358 : : definition. */
2359 : 13308679 : if (DECL_VINDEX (olddecl))
2360 : 153726 : DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2361 : 13308679 : if (DECL_CONTEXT (olddecl))
2362 : 13308679 : DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2363 : 13308679 : DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2364 : 13308679 : DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2365 : 13308679 : DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2366 : 13308679 : DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2367 : 13308679 : DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2368 : 13308679 : DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2369 : 13308679 : DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2370 : 13308679 : DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2371 : 13308679 : DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2372 : 13308679 : |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2373 : 13308679 : if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2374 : 2357114 : DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2375 : 4714228 : = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2376 : 13308679 : new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2377 : :
2378 : 13308679 : duplicate_contracts (newdecl, olddecl);
2379 : :
2380 : : /* Optionally warn about more than one declaration for the same
2381 : : name, but don't warn about a function declaration followed by a
2382 : : definition. */
2383 : 36 : if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2384 : 36 : && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2385 : : /* Don't warn about extern decl followed by definition. */
2386 : 36 : && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2387 : : /* Don't warn if at least one is/was hidden. */
2388 : 36 : && !(hiding || was_hidden)
2389 : : /* Don't warn about declaration followed by specialization. */
2390 : 13308695 : && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2391 : 12 : || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2392 : : {
2393 : 8 : auto_diagnostic_group d;
2394 : 8 : if (warning_at (newdecl_loc,
2395 : : OPT_Wredundant_decls,
2396 : : "redundant redeclaration of %qD in same scope",
2397 : : newdecl))
2398 : 8 : inform (olddecl_loc,
2399 : : "previous declaration of %qD", olddecl);
2400 : 8 : }
2401 : :
2402 : : /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2403 : : first declaration of the function or, for an explicit specialization
2404 : : of a function template, the first declaration of that
2405 : : specialization. */
2406 : 13308679 : if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2407 : 542679 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2408 : : {
2409 : 12766071 : if (DECL_DELETED_FN (newdecl))
2410 : : {
2411 : 21 : auto_diagnostic_group d;
2412 : 21 : if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2413 : : "is not first declaration", newdecl))
2414 : 21 : inform (olddecl_loc,
2415 : : "previous declaration of %qD", olddecl);
2416 : 21 : }
2417 : 12766071 : DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2418 : : }
2419 : : }
2420 : :
2421 : : /* Deal with C++: must preserve virtual function table size. */
2422 : 16955572 : if (TREE_CODE (olddecl) == TYPE_DECL)
2423 : : {
2424 : 22775 : tree newtype = TREE_TYPE (newdecl);
2425 : 22775 : tree oldtype = TREE_TYPE (olddecl);
2426 : :
2427 : 22775 : if (newtype != error_mark_node && oldtype != error_mark_node
2428 : 45550 : && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2429 : 49902 : CLASSTYPE_FRIEND_CLASSES (newtype)
2430 : 16634 : = CLASSTYPE_FRIEND_CLASSES (oldtype);
2431 : :
2432 : 22775 : DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2433 : : }
2434 : :
2435 : : /* Copy all the DECL_... slots specified in the new decl except for
2436 : : any that we copy here from the old type. */
2437 : 16955572 : if (merge_attr)
2438 : 16412964 : DECL_ATTRIBUTES (newdecl)
2439 : 32825928 : = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2440 : : else
2441 : 542608 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2442 : :
2443 : 16955572 : if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2444 : : {
2445 : 3261955 : tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2446 : 3261955 : tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2447 : 3261955 : TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2448 : :
2449 : : /* The new decl should not already have gathered any
2450 : : specializations. */
2451 : 3261955 : gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2452 : :
2453 : : /* Make sure the contracts are equivalent. */
2454 : 3261955 : duplicate_contracts (newdecl, olddecl);
2455 : :
2456 : : /* Remove contracts from old_result so they aren't appended to
2457 : : old_result by the merge function. */
2458 : 3261955 : remove_contract_attributes (old_result);
2459 : :
2460 : 3261955 : DECL_ATTRIBUTES (old_result)
2461 : 3261955 : = (*targetm.merge_decl_attributes) (old_result, new_result);
2462 : :
2463 : 3261955 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2464 : : {
2465 : 3261930 : if (DECL_SOURCE_LOCATION (newdecl)
2466 : 3261930 : != DECL_SOURCE_LOCATION (olddecl))
2467 : : {
2468 : : /* Per C++11 8.3.6/4, default arguments cannot be added in
2469 : : later declarations of a function template. */
2470 : 3240277 : check_redeclaration_no_default_args (newdecl);
2471 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default
2472 : : argument expression, that declaration... shall be the only
2473 : : declaration of the function or function template in the
2474 : : translation unit." */
2475 : 3240277 : check_no_redeclaration_friend_default_args
2476 : 3240277 : (old_result, new_result);
2477 : :
2478 : 3240277 : tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2479 : 3240277 : tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2480 : 3240277 : merge_default_template_args (new_parms, old_parms,
2481 : : /*class_p=*/false);
2482 : : }
2483 : 3261930 : if (!DECL_UNIQUE_FRIEND_P (new_result))
2484 : 2722936 : DECL_UNIQUE_FRIEND_P (old_result) = false;
2485 : :
2486 : 3261930 : check_default_args (newdecl);
2487 : :
2488 : 6523844 : if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2489 : 3261930 : && DECL_INITIAL (new_result))
2490 : : {
2491 : 32 : if (DECL_INITIAL (old_result))
2492 : 16 : DECL_UNINLINABLE (old_result) = 1;
2493 : : else
2494 : 16 : DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2495 : 32 : DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2496 : 96 : DECL_NOT_REALLY_EXTERN (old_result)
2497 : 32 : = DECL_NOT_REALLY_EXTERN (new_result);
2498 : 64 : DECL_INTERFACE_KNOWN (old_result)
2499 : 32 : = DECL_INTERFACE_KNOWN (new_result);
2500 : 32 : DECL_DECLARED_INLINE_P (old_result)
2501 : 32 : = DECL_DECLARED_INLINE_P (new_result);
2502 : 32 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2503 : 32 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2504 : : }
2505 : : else
2506 : : {
2507 : 3261898 : DECL_DECLARED_INLINE_P (old_result)
2508 : 3261898 : |= DECL_DECLARED_INLINE_P (new_result);
2509 : 3261898 : DECL_DISREGARD_INLINE_LIMITS (old_result)
2510 : 3261898 : |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2511 : 3261898 : check_redeclaration_exception_specification (newdecl, olddecl);
2512 : :
2513 : 3261898 : merge_attribute_bits (new_result, old_result);
2514 : : }
2515 : : }
2516 : :
2517 : : /* If the new declaration is a definition, update the file and
2518 : : line information on the declaration, and also make
2519 : : the old declaration the same definition. */
2520 : 3261955 : if (DECL_INITIAL (new_result) != NULL_TREE)
2521 : : {
2522 : 5050118 : DECL_SOURCE_LOCATION (olddecl)
2523 : 5050118 : = DECL_SOURCE_LOCATION (old_result)
2524 : 2525059 : = DECL_SOURCE_LOCATION (newdecl);
2525 : 2525059 : DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2526 : 2525059 : if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2527 : : {
2528 : 2525059 : DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result);
2529 : 8987344 : for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p))
2530 : 6462285 : DECL_CONTEXT (p) = old_result;
2531 : :
2532 : 5050118 : if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2533 : 18 : SET_DECL_FRIEND_CONTEXT (old_result, fc);
2534 : : }
2535 : : }
2536 : :
2537 : 3261955 : return olddecl;
2538 : : }
2539 : :
2540 : 13693617 : if (types_match)
2541 : : {
2542 : 13693342 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2543 : 13308404 : check_redeclaration_exception_specification (newdecl, olddecl);
2544 : :
2545 : : /* Automatically handles default parameters. */
2546 : 13693342 : tree oldtype = TREE_TYPE (olddecl);
2547 : 13693342 : tree newtype;
2548 : :
2549 : : /* For typedefs use the old type, as the new type's DECL_NAME points
2550 : : at newdecl, which will be ggc_freed. */
2551 : 13693342 : if (TREE_CODE (newdecl) == TYPE_DECL)
2552 : : {
2553 : : /* But NEWTYPE might have an attribute, honor that. */
2554 : 22775 : tree tem = TREE_TYPE (newdecl);
2555 : 22775 : newtype = oldtype;
2556 : :
2557 : 22775 : if (TYPE_USER_ALIGN (tem))
2558 : : {
2559 : 25 : if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2560 : 6 : SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2561 : 25 : TYPE_USER_ALIGN (newtype) = true;
2562 : : }
2563 : :
2564 : : /* And remove the new type from the variants list. */
2565 : 22775 : if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2566 : : {
2567 : 35 : tree remove = TREE_TYPE (newdecl);
2568 : 35 : if (TYPE_MAIN_VARIANT (remove) == remove)
2569 : : {
2570 : 12 : gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2571 : : /* If remove is the main variant, no need to remove that
2572 : : from the list. One of the DECL_ORIGINAL_TYPE
2573 : : variants, e.g. created for aligned attribute, might still
2574 : : refer to the newdecl TYPE_DECL though, so remove that one
2575 : : in that case. */
2576 : 12 : if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2577 : 8 : if (orig != remove)
2578 : 8 : for (tree t = TYPE_MAIN_VARIANT (orig); t;
2579 : 0 : t = TYPE_MAIN_VARIANT (t))
2580 : 8 : if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2581 : : {
2582 : 16 : TYPE_NEXT_VARIANT (t)
2583 : 8 : = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2584 : 8 : break;
2585 : : }
2586 : : }
2587 : : else
2588 : 23 : for (tree t = TYPE_MAIN_VARIANT (remove); ;
2589 : 0 : t = TYPE_NEXT_VARIANT (t))
2590 : 23 : if (TYPE_NEXT_VARIANT (t) == remove)
2591 : : {
2592 : 23 : TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2593 : 23 : break;
2594 : : }
2595 : : }
2596 : : }
2597 : 13670567 : else if (merge_attr)
2598 : 13127959 : newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2599 : : else
2600 : 542608 : newtype = TREE_TYPE (newdecl);
2601 : :
2602 : 13693342 : if (VAR_P (newdecl))
2603 : : {
2604 : 362163 : DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2605 : : /* For already initialized vars, TREE_READONLY could have been
2606 : : cleared in cp_finish_decl, because the var needs runtime
2607 : : initialization or destruction. Make sure not to set
2608 : : TREE_READONLY on it again. */
2609 : 362163 : if (DECL_INITIALIZED_P (olddecl)
2610 : 1214 : && !DECL_EXTERNAL (olddecl)
2611 : 362248 : && !TREE_READONLY (olddecl))
2612 : 37 : TREE_READONLY (newdecl) = 0;
2613 : 362163 : DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2614 : 724326 : DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2615 : 362163 : |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2616 : 362163 : if (DECL_DEPENDENT_INIT_P (olddecl))
2617 : 98174 : SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2618 : 724326 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2619 : 362163 : |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2620 : 362163 : DECL_DECLARED_CONSTEXPR_P (newdecl)
2621 : 362163 : |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2622 : 724326 : DECL_DECLARED_CONSTINIT_P (newdecl)
2623 : 362163 : |= DECL_DECLARED_CONSTINIT_P (olddecl);
2624 : :
2625 : : /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2626 : 362163 : if (DECL_LANG_SPECIFIC (olddecl)
2627 : 720742 : && CP_DECL_THREADPRIVATE_P (olddecl))
2628 : : {
2629 : : /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2630 : 37 : retrofit_lang_decl (newdecl);
2631 : 37 : CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2632 : : }
2633 : : }
2634 : :
2635 : : /* An explicit specialization of a function template or of a member
2636 : : function of a class template can be declared transaction_safe
2637 : : independently of whether the corresponding template entity is declared
2638 : : transaction_safe. */
2639 : 1227 : if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2640 : 1107 : && DECL_TEMPLATE_INSTANTIATION (olddecl)
2641 : 51 : && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2642 : 51 : && tx_safe_fn_type_p (newtype)
2643 : 13693342 : && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2644 : 0 : newtype = tx_unsafe_fn_variant (newtype);
2645 : :
2646 : 13693342 : TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2647 : :
2648 : 13693342 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2649 : 13308404 : check_default_args (newdecl);
2650 : :
2651 : : /* Lay the type out, unless already done. */
2652 : 13693342 : if (! same_type_p (newtype, oldtype)
2653 : 399 : && TREE_TYPE (newdecl) != error_mark_node
2654 : 13693741 : && !(processing_template_decl && uses_template_parms (newdecl)))
2655 : 319 : layout_type (TREE_TYPE (newdecl));
2656 : :
2657 : 13693342 : if ((VAR_P (newdecl)
2658 : 13331179 : || TREE_CODE (newdecl) == PARM_DECL
2659 : 13331179 : || TREE_CODE (newdecl) == RESULT_DECL
2660 : 13331179 : || TREE_CODE (newdecl) == FIELD_DECL
2661 : 13331179 : || TREE_CODE (newdecl) == TYPE_DECL)
2662 : 13716117 : && !(processing_template_decl && uses_template_parms (newdecl)))
2663 : 231251 : layout_decl (newdecl, 0);
2664 : :
2665 : : /* Merge deprecatedness. */
2666 : 13693342 : if (TREE_DEPRECATED (newdecl))
2667 : 11200 : TREE_DEPRECATED (olddecl) = 1;
2668 : :
2669 : : /* Merge unavailability. */
2670 : 13693342 : if (TREE_UNAVAILABLE (newdecl))
2671 : 4 : TREE_UNAVAILABLE (olddecl) = 1;
2672 : :
2673 : : /* Preserve function specific target and optimization options */
2674 : 13693342 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2675 : : {
2676 : 13308404 : if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2677 : 13308404 : && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2678 : 0 : DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2679 : 0 : = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2680 : :
2681 : 13308404 : if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2682 : 13308404 : && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2683 : 35 : DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2684 : 35 : = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2685 : :
2686 : 13308404 : if (!DECL_UNIQUE_FRIEND_P (olddecl))
2687 : 13153012 : DECL_UNIQUE_FRIEND_P (newdecl) = false;
2688 : : }
2689 : : else
2690 : : {
2691 : : /* Merge the const type qualifier. */
2692 : 384938 : if (TREE_READONLY (newdecl))
2693 : 32565 : TREE_READONLY (olddecl) = 1;
2694 : : /* Merge the volatile type qualifier. */
2695 : 384938 : if (TREE_THIS_VOLATILE (newdecl))
2696 : 6 : TREE_THIS_VOLATILE (olddecl) = 1;
2697 : : }
2698 : :
2699 : : /* Merge the initialization information. */
2700 : 13693342 : if (DECL_INITIAL (newdecl) == NULL_TREE
2701 : 13693342 : && DECL_INITIAL (olddecl) != NULL_TREE)
2702 : : {
2703 : 166599 : DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2704 : 166599 : DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2705 : 166599 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2706 : : {
2707 : 11733 : DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2708 : 11733 : DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2709 : : }
2710 : : }
2711 : :
2712 : 13693342 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2713 : : {
2714 : 13308404 : DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2715 : 13308404 : |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2716 : 13308404 : DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2717 : 13308404 : if (DECL_IS_OPERATOR_NEW_P (olddecl))
2718 : 49177 : DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2719 : 13308404 : DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2720 : 13308404 : |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2721 : 13308404 : DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2722 : 13308404 : |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2723 : :
2724 : 13308404 : if (merge_attr)
2725 : 12765796 : merge_attribute_bits (newdecl, olddecl);
2726 : : else
2727 : : {
2728 : : /* Merge the noreturn bit. */
2729 : 542608 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2730 : 542608 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2731 : 542608 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2732 : 542608 : DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2733 : 542608 : DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2734 : : }
2735 : : /* Keep the old RTL. */
2736 : 13308404 : COPY_DECL_RTL (olddecl, newdecl);
2737 : : }
2738 : 384938 : else if (VAR_P (newdecl)
2739 : 384938 : && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2740 : : {
2741 : : /* Keep the old RTL. We cannot keep the old RTL if the old
2742 : : declaration was for an incomplete object and the new
2743 : : declaration is not since many attributes of the RTL will
2744 : : change. */
2745 : 362068 : COPY_DECL_RTL (olddecl, newdecl);
2746 : : }
2747 : : }
2748 : : /* If cannot merge, then use the new type and qualifiers,
2749 : : and don't preserve the old rtl. */
2750 : : else
2751 : : {
2752 : : /* Clean out any memory we had of the old declaration. */
2753 : 275 : tree oldstatic = value_member (olddecl, static_aggregates);
2754 : 275 : if (oldstatic)
2755 : 0 : TREE_VALUE (oldstatic) = error_mark_node;
2756 : :
2757 : 275 : TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2758 : 275 : TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2759 : 275 : TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2760 : 275 : TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2761 : 275 : TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2762 : : }
2763 : :
2764 : : /* Merge the storage class information. */
2765 : 13693617 : merge_weak (newdecl, olddecl);
2766 : :
2767 : 13693617 : DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2768 : 13693617 : TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2769 : 13693617 : TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2770 : 13693617 : if (! DECL_EXTERNAL (olddecl))
2771 : 28272 : DECL_EXTERNAL (newdecl) = 0;
2772 : 13693617 : if (! DECL_COMDAT (olddecl))
2773 : 5501852 : DECL_COMDAT (newdecl) = 0;
2774 : :
2775 : 13693617 : if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2776 : : {
2777 : 91 : if (!DECL_LOCAL_DECL_P (olddecl))
2778 : : /* This can happen if olddecl was brought in from the
2779 : : enclosing namespace via a using-decl. The new decl is
2780 : : then not a block-scope extern at all. */
2781 : 4 : DECL_LOCAL_DECL_P (newdecl) = false;
2782 : : else
2783 : : {
2784 : 87 : retrofit_lang_decl (newdecl);
2785 : 87 : tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2786 : 87 : = DECL_LOCAL_DECL_ALIAS (olddecl);
2787 : 87 : DECL_ATTRIBUTES (alias)
2788 : 87 : = (*targetm.merge_decl_attributes) (alias, newdecl);
2789 : 87 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2790 : 51 : merge_attribute_bits (newdecl, alias);
2791 : : }
2792 : : }
2793 : :
2794 : 13693617 : new_template_info = NULL_TREE;
2795 : 13693617 : if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2796 : : {
2797 : 13667311 : bool new_redefines_gnu_inline = false;
2798 : :
2799 : 13667311 : if (new_defines_function
2800 : 13667311 : && ((DECL_INTERFACE_KNOWN (olddecl)
2801 : 1396 : && TREE_CODE (olddecl) == FUNCTION_DECL)
2802 : 8497224 : || (TREE_CODE (olddecl) == TEMPLATE_DECL
2803 : 0 : && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2804 : : == FUNCTION_DECL))))
2805 : 2792 : new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2806 : :
2807 : : if (!new_redefines_gnu_inline)
2808 : : {
2809 : 13667211 : DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2810 : 13667211 : DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2811 : 13667211 : DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2812 : : }
2813 : :
2814 : 13667311 : if (TREE_CODE (newdecl) != TYPE_DECL)
2815 : : {
2816 : 13667253 : DECL_TEMPLATE_INSTANTIATED (newdecl)
2817 : 13667253 : |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2818 : 13667253 : DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2819 : :
2820 : : /* If the OLDDECL is an instantiation and/or specialization,
2821 : : then the NEWDECL must be too. But, it may not yet be marked
2822 : : as such if the caller has created NEWDECL, but has not yet
2823 : : figured out that it is a redeclaration. */
2824 : 13667253 : if (!DECL_USE_TEMPLATE (newdecl))
2825 : 13124349 : DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2826 : :
2827 : 13667253 : if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2828 : 13124408 : DECL_INITIALIZED_IN_CLASS_P (newdecl)
2829 : 26248816 : |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2830 : : }
2831 : :
2832 : : /* Don't really know how much of the language-specific
2833 : : values we should copy from old to new. */
2834 : 13667311 : DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2835 : :
2836 : 13667311 : if (LANG_DECL_HAS_MIN (newdecl))
2837 : : {
2838 : 13667311 : DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2839 : 13667311 : if (DECL_TEMPLATE_INFO (newdecl))
2840 : : {
2841 : 553804 : new_template_info = DECL_TEMPLATE_INFO (newdecl);
2842 : 553804 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2843 : 553804 : && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2844 : : /* Remember the presence of explicit specialization args. */
2845 : 1085222 : TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2846 : 1085222 : = TINFO_USED_TEMPLATE_ID (new_template_info);
2847 : : }
2848 : :
2849 : : /* We don't want to copy template info from a non-templated friend
2850 : : (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */
2851 : 13667311 : gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl)
2852 : : || !non_templated_friend_p (olddecl));
2853 : 13667311 : DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2854 : : }
2855 : :
2856 : 13667311 : if (DECL_DECLARES_FUNCTION_P (newdecl))
2857 : : {
2858 : : /* Only functions have these fields. */
2859 : 13308679 : DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2860 : 13308679 : DECL_BEFRIENDING_CLASSES (newdecl)
2861 : 13308679 : = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2862 : 13308679 : DECL_BEFRIENDING_CLASSES (olddecl));
2863 : : /* DECL_THUNKS is only valid for virtual functions,
2864 : : otherwise it is a DECL_FRIEND_CONTEXT. */
2865 : 13308679 : if (DECL_VIRTUAL_P (newdecl))
2866 : 621860 : SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2867 : 25373638 : else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2868 : 76742 : SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2869 : : }
2870 : 358632 : else if (VAR_P (newdecl))
2871 : : {
2872 : : /* Only variables have this field. */
2873 : 358574 : if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2874 : 213 : SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2875 : : }
2876 : : }
2877 : :
2878 : 13693617 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
2879 : : {
2880 : 13308679 : tree parm;
2881 : :
2882 : : /* Merge parameter attributes. */
2883 : 13308679 : tree oldarg, newarg;
2884 : 13308679 : for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2885 : 38194654 : oldarg && newarg;
2886 : 24885975 : oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2887 : : {
2888 : 24885975 : DECL_ATTRIBUTES (newarg)
2889 : 24885975 : = (*targetm.merge_decl_attributes) (oldarg, newarg);
2890 : 24885975 : DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2891 : : }
2892 : :
2893 : 13308679 : if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2894 : 13308679 : && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2895 : : {
2896 : : /* If newdecl is not a specialization, then it is not a
2897 : : template-related function at all. And that means that we
2898 : : should have exited above, returning 0. */
2899 : 542608 : gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2900 : :
2901 : 542608 : if (DECL_ODR_USED (olddecl))
2902 : : /* From [temp.expl.spec]:
2903 : :
2904 : : If a template, a member template or the member of a class
2905 : : template is explicitly specialized then that
2906 : : specialization shall be declared before the first use of
2907 : : that specialization that would cause an implicit
2908 : : instantiation to take place, in every translation unit in
2909 : : which such a use occurs. */
2910 : 0 : error ("explicit specialization of %qD after first use",
2911 : : olddecl);
2912 : :
2913 : 542608 : SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2914 : 542608 : DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2915 : 1085072 : && DECL_DECLARED_INLINE_P (newdecl));
2916 : :
2917 : : /* Don't propagate visibility from the template to the
2918 : : specialization here. We'll do that in determine_visibility if
2919 : : appropriate. */
2920 : 542608 : DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2921 : :
2922 : : /* [temp.expl.spec/14] We don't inline explicit specialization
2923 : : just because the primary template says so. */
2924 : 542608 : gcc_assert (!merge_attr);
2925 : :
2926 : 542608 : DECL_DECLARED_INLINE_P (olddecl)
2927 : 542608 : = DECL_DECLARED_INLINE_P (newdecl);
2928 : :
2929 : 542608 : DECL_DISREGARD_INLINE_LIMITS (olddecl)
2930 : 542608 : = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2931 : :
2932 : 542608 : DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2933 : : }
2934 : 12766071 : else if (new_defines_function && DECL_INITIAL (olddecl))
2935 : : {
2936 : : /* Never inline re-defined extern inline functions.
2937 : : FIXME: this could be better handled by keeping both
2938 : : function as separate declarations. */
2939 : 100 : DECL_UNINLINABLE (newdecl) = 1;
2940 : : }
2941 : : else
2942 : : {
2943 : 12765971 : if (DECL_PENDING_INLINE_P (olddecl))
2944 : : {
2945 : 8 : DECL_PENDING_INLINE_P (newdecl) = 1;
2946 : 8 : DECL_PENDING_INLINE_INFO (newdecl)
2947 : 16 : = DECL_PENDING_INLINE_INFO (olddecl);
2948 : : }
2949 : 12765963 : else if (DECL_PENDING_INLINE_P (newdecl))
2950 : : ;
2951 : 12765963 : else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2952 : 12765963 : DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2953 : 25531926 : = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2954 : :
2955 : 12765971 : DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2956 : :
2957 : 38297913 : DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2958 : 25531942 : = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2959 : :
2960 : 25531942 : DECL_DISREGARD_INLINE_LIMITS (newdecl)
2961 : 12765971 : = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2962 : 12765971 : = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2963 : 25159394 : || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2964 : : }
2965 : :
2966 : : /* Preserve abstractness on cloned [cd]tors. */
2967 : 13308679 : DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2968 : :
2969 : : /* Update newdecl's parms to point at olddecl. */
2970 : 43558159 : for (parm = DECL_ARGUMENTS (newdecl); parm;
2971 : 30249480 : parm = DECL_CHAIN (parm))
2972 : 30249480 : DECL_CONTEXT (parm) = olddecl;
2973 : :
2974 : 13308679 : if (! types_match)
2975 : : {
2976 : 275 : SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2977 : 275 : COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2978 : 275 : COPY_DECL_RTL (newdecl, olddecl);
2979 : : }
2980 : 13308679 : if (! types_match || new_defines_function)
2981 : : {
2982 : : /* These are the final DECL_ARGUMENTS that will be used within the
2983 : : body; update any references to old DECL_ARGUMENTS in the
2984 : : contracts, if present. */
2985 : 8498895 : if (tree contracts = DECL_CONTRACTS (newdecl))
2986 : 131 : remap_contracts (olddecl, newdecl, contracts, true);
2987 : :
2988 : : /* These need to be copied so that the names are available.
2989 : : Note that if the types do match, we'll preserve inline
2990 : : info and other bits, but if not, we won't. */
2991 : 8498895 : DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2992 : 8498895 : DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2993 : :
2994 : : /* In some cases, duplicate_contracts will remove contracts from
2995 : : OLDDECL, to avoid duplications. Sometimes, the contracts end up
2996 : : shared. If we removed them, re-add them. */
2997 : 8498895 : if (!DECL_CONTRACTS (olddecl))
2998 : 8498848 : copy_contract_attributes (olddecl, newdecl);
2999 : : }
3000 : : /* If redeclaring a builtin function, it stays built in
3001 : : if newdecl is a gnu_inline definition, or if newdecl is just
3002 : : a declaration. */
3003 : 13308679 : if (fndecl_built_in_p (olddecl)
3004 : 13363131 : && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
3005 : : {
3006 : 3548805 : copy_decl_built_in_function (newdecl, olddecl);
3007 : : /* If we're keeping the built-in definition, keep the rtl,
3008 : : regardless of declaration matches. */
3009 : 3548805 : COPY_DECL_RTL (olddecl, newdecl);
3010 : 3548805 : if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
3011 : : {
3012 : 3548797 : enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
3013 : 3548797 : if (builtin_decl_explicit_p (fncode))
3014 : : {
3015 : : /* A compatible prototype of these builtin functions
3016 : : is seen, assume the runtime implements it with
3017 : : the expected semantics. */
3018 : 3548797 : switch (fncode)
3019 : : {
3020 : 9911 : case BUILT_IN_STPCPY:
3021 : 9911 : set_builtin_decl_implicit_p (fncode, true);
3022 : 9911 : break;
3023 : 3538886 : default:
3024 : 3538886 : set_builtin_decl_declared_p (fncode, true);
3025 : 3538886 : break;
3026 : : }
3027 : : }
3028 : :
3029 : 3548797 : copy_attributes_to_builtin (newdecl);
3030 : : }
3031 : : }
3032 : 13308679 : if (new_defines_function)
3033 : : /* If defining a function declared with other language
3034 : : linkage, use the previously declared language linkage. */
3035 : 8498620 : SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3036 : 4810059 : else if (types_match)
3037 : : {
3038 : 4809784 : DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3039 : : /* Don't clear out the arguments if we're just redeclaring a
3040 : : function. */
3041 : 4809784 : if (DECL_ARGUMENTS (olddecl))
3042 : : {
3043 : : /* If we removed contracts from previous definition, re-attach
3044 : : them. Otherwise, rewrite the contracts so they match the
3045 : : parameters of the new declaration. */
3046 : 1177774 : if (DECL_INITIAL (olddecl)
3047 : 11596 : && DECL_CONTRACTS (newdecl)
3048 : 1177785 : && !DECL_CONTRACTS (olddecl))
3049 : 10 : copy_contract_attributes (olddecl, newdecl);
3050 : : else
3051 : : {
3052 : : /* Temporarily undo the re-contexting of parameters so we can
3053 : : actually remap parameters. The inliner won't replace
3054 : : parameters if we don't do this. */
3055 : 1177764 : tree args = DECL_ARGUMENTS (newdecl);
3056 : 4472230 : for (tree p = args; p; p = DECL_CHAIN (p))
3057 : 3294466 : DECL_CONTEXT (p) = newdecl;
3058 : :
3059 : : /* Save new argument names for use in contracts parsing,
3060 : : unless we've already started parsing the body of olddecl
3061 : : (particular issues arise when newdecl is from a prior
3062 : : friend decl with no argument names, see
3063 : : modules/contracts-tpl-friend-1). */
3064 : 1177764 : if (tree contracts = DECL_CONTRACTS (olddecl))
3065 : 29 : remap_contracts (newdecl, olddecl, contracts, true);
3066 : :
3067 : : /* And reverse this operation again. */
3068 : 4472230 : for (tree p = args; p; p = DECL_CHAIN (p))
3069 : 3294466 : DECL_CONTEXT (p) = olddecl;
3070 : : }
3071 : :
3072 : 1177774 : DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3073 : : }
3074 : : }
3075 : : }
3076 : 384938 : else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3077 : 0 : NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3078 : :
3079 : : /* Now preserve various other info from the definition. */
3080 : 13693617 : TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3081 : 13693617 : TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3082 : 13693617 : DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3083 : 13693617 : COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3084 : :
3085 : : /* Warn about conflicting visibility specifications. */
3086 : 13693617 : if (DECL_VISIBILITY_SPECIFIED (olddecl)
3087 : 4740921 : && DECL_VISIBILITY_SPECIFIED (newdecl)
3088 : 13694071 : && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
3089 : : {
3090 : 14 : auto_diagnostic_group d;
3091 : 14 : if (warning_at (newdecl_loc, OPT_Wattributes,
3092 : : "%qD: visibility attribute ignored because it "
3093 : : "conflicts with previous declaration", newdecl))
3094 : 14 : inform (olddecl_loc,
3095 : : "previous declaration of %qD", olddecl);
3096 : 14 : }
3097 : : /* Choose the declaration which specified visibility. */
3098 : 13693617 : if (DECL_VISIBILITY_SPECIFIED (olddecl))
3099 : : {
3100 : 4740921 : DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
3101 : 4740921 : DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
3102 : : }
3103 : : /* Init priority used to be merged from newdecl to olddecl by the memcpy,
3104 : : so keep this behavior. */
3105 : 13693617 : if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
3106 : : {
3107 : 4 : SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
3108 : 4 : DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
3109 : : }
3110 : : /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
3111 : 13693617 : if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
3112 : : {
3113 : 71 : SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
3114 : 71 : DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
3115 : : }
3116 : 13693546 : else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
3117 : 13693546 : && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
3118 : 0 : DECL_USER_ALIGN (newdecl) = 1;
3119 : :
3120 : 13693617 : DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
3121 : 27387234 : if (DECL_WARN_IF_NOT_ALIGN (olddecl)
3122 : 13693617 : > DECL_WARN_IF_NOT_ALIGN (newdecl))
3123 : 0 : SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
3124 : : DECL_WARN_IF_NOT_ALIGN (olddecl));
3125 : 13693617 : if (TREE_CODE (newdecl) == FIELD_DECL)
3126 : 0 : DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
3127 : :
3128 : : /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
3129 : : with that from NEWDECL below. */
3130 : 13693617 : if (DECL_LANG_SPECIFIC (olddecl))
3131 : : {
3132 : 13667316 : gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
3133 : : != DECL_LANG_SPECIFIC (newdecl));
3134 : 13667316 : ggc_free (DECL_LANG_SPECIFIC (olddecl));
3135 : : }
3136 : :
3137 : : /* Merge the USED information. */
3138 : 13693617 : if (TREE_USED (olddecl))
3139 : 459396 : TREE_USED (newdecl) = 1;
3140 : 13234221 : else if (TREE_USED (newdecl))
3141 : 58 : TREE_USED (olddecl) = 1;
3142 : :
3143 : 13693617 : if (VAR_P (newdecl))
3144 : : {
3145 : 362163 : if (DECL_READ_P (olddecl))
3146 : 31538 : DECL_READ_P (newdecl) = 1;
3147 : 330625 : else if (DECL_READ_P (newdecl))
3148 : 0 : DECL_READ_P (olddecl) = 1;
3149 : : }
3150 : :
3151 : 13693617 : if (DECL_PRESERVE_P (olddecl))
3152 : 56 : DECL_PRESERVE_P (newdecl) = 1;
3153 : 13693561 : else if (DECL_PRESERVE_P (newdecl))
3154 : 70 : DECL_PRESERVE_P (olddecl) = 1;
3155 : :
3156 : : /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
3157 : : to olddecl and deleted. */
3158 : 13693617 : if (TREE_CODE (newdecl) == FUNCTION_DECL
3159 : 27002296 : && DECL_FUNCTION_VERSIONED (olddecl))
3160 : : {
3161 : : /* Set the flag for newdecl so that it gets copied to olddecl. */
3162 : 284 : DECL_FUNCTION_VERSIONED (newdecl) = 1;
3163 : : /* newdecl will be purged after copying to olddecl and is no longer
3164 : : a version. */
3165 : 284 : cgraph_node::delete_function_version_by_decl (newdecl);
3166 : : }
3167 : :
3168 : 13693617 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3169 : : {
3170 : 13308679 : int function_size;
3171 : 13308679 : struct symtab_node *snode = symtab_node::get (olddecl);
3172 : :
3173 : 13308679 : function_size = sizeof (struct tree_decl_common);
3174 : :
3175 : 13308679 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3176 : : (char *) newdecl + sizeof (struct tree_common),
3177 : : function_size - sizeof (struct tree_common));
3178 : :
3179 : 13308679 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3180 : : (char *) newdecl + sizeof (struct tree_decl_common),
3181 : : sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
3182 : :
3183 : : /* Preserve symtab node mapping. */
3184 : 13308679 : olddecl->decl_with_vis.symtab_node = snode;
3185 : :
3186 : 13308679 : if (new_template_info)
3187 : : /* If newdecl is a template instantiation, it is possible that
3188 : : the following sequence of events has occurred:
3189 : :
3190 : : o A friend function was declared in a class template. The
3191 : : class template was instantiated.
3192 : :
3193 : : o The instantiation of the friend declaration was
3194 : : recorded on the instantiation list, and is newdecl.
3195 : :
3196 : : o Later, however, instantiate_class_template called pushdecl
3197 : : on the newdecl to perform name injection. But, pushdecl in
3198 : : turn called duplicate_decls when it discovered that another
3199 : : declaration of a global function with the same name already
3200 : : existed.
3201 : :
3202 : : o Here, in duplicate_decls, we decided to clobber newdecl.
3203 : :
3204 : : If we're going to do that, we'd better make sure that
3205 : : olddecl, and not newdecl, is on the list of
3206 : : instantiations so that if we try to do the instantiation
3207 : : again we won't get the clobbered declaration. */
3208 : 553789 : reregister_specialization (newdecl,
3209 : : new_template_info,
3210 : : olddecl);
3211 : : }
3212 : : else
3213 : : {
3214 : 384938 : size_t size = tree_code_size (TREE_CODE (newdecl));
3215 : :
3216 : 384938 : memcpy ((char *) olddecl + sizeof (struct tree_common),
3217 : : (char *) newdecl + sizeof (struct tree_common),
3218 : : sizeof (struct tree_decl_common) - sizeof (struct tree_common));
3219 : :
3220 : 384938 : switch (TREE_CODE (newdecl))
3221 : : {
3222 : 384938 : case LABEL_DECL:
3223 : 384938 : case VAR_DECL:
3224 : 384938 : case RESULT_DECL:
3225 : 384938 : case PARM_DECL:
3226 : 384938 : case FIELD_DECL:
3227 : 384938 : case TYPE_DECL:
3228 : 384938 : case CONST_DECL:
3229 : 384938 : {
3230 : 384938 : struct symtab_node *snode = NULL;
3231 : :
3232 : 384938 : if (VAR_P (newdecl)
3233 : 384938 : && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3234 : 3 : || DECL_EXTERNAL (olddecl)))
3235 : 362163 : snode = symtab_node::get (olddecl);
3236 : 384938 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3237 : : (char *) newdecl + sizeof (struct tree_decl_common),
3238 : : size - sizeof (struct tree_decl_common)
3239 : 384938 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3240 : 384938 : if (VAR_P (newdecl))
3241 : 362163 : olddecl->decl_with_vis.symtab_node = snode;
3242 : : }
3243 : : break;
3244 : 0 : default:
3245 : 0 : memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3246 : : (char *) newdecl + sizeof (struct tree_decl_common),
3247 : : sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3248 : 0 : + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3249 : 0 : break;
3250 : : }
3251 : : }
3252 : :
3253 : 13693617 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3254 : : {
3255 : 13670842 : if (DECL_EXTERNAL (olddecl)
3256 : : || TREE_PUBLIC (olddecl)
3257 : 13670842 : || TREE_STATIC (olddecl))
3258 : : {
3259 : : /* Merge the section attribute.
3260 : : We want to issue an error if the sections conflict but that must be
3261 : : done later in decl_attributes since we are called before attributes
3262 : : are assigned. */
3263 : 13670842 : if (DECL_SECTION_NAME (newdecl) != NULL)
3264 : 7 : set_decl_section_name (olddecl, newdecl);
3265 : :
3266 : 13670842 : if (DECL_ONE_ONLY (newdecl))
3267 : : {
3268 : 0 : struct symtab_node *oldsym, *newsym;
3269 : 0 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3270 : 0 : oldsym = cgraph_node::get_create (olddecl);
3271 : : else
3272 : 0 : oldsym = varpool_node::get_create (olddecl);
3273 : 0 : newsym = symtab_node::get (newdecl);
3274 : 0 : oldsym->set_comdat_group (newsym->get_comdat_group ());
3275 : : }
3276 : : }
3277 : :
3278 : 13670842 : if (VAR_P (newdecl)
3279 : 13670842 : && CP_DECL_THREAD_LOCAL_P (newdecl))
3280 : : {
3281 : 383 : CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3282 : 383 : if (!processing_template_decl)
3283 : 367 : set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3284 : : }
3285 : : }
3286 : :
3287 : 13693617 : DECL_UID (olddecl) = olddecl_uid;
3288 : :
3289 : : /* NEWDECL contains the merged attribute lists.
3290 : : Update OLDDECL to be the same. */
3291 : 13693617 : DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3292 : :
3293 : : /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3294 : : so that encode_section_info has a chance to look at the new decl
3295 : : flags and attributes. */
3296 : 13693617 : if (DECL_RTL_SET_P (olddecl)
3297 : 13693617 : && (TREE_CODE (olddecl) == FUNCTION_DECL
3298 : 0 : || (VAR_P (olddecl)
3299 : 0 : && TREE_STATIC (olddecl))))
3300 : 0 : make_decl_rtl (olddecl);
3301 : :
3302 : : /* The NEWDECL will no longer be needed. Because every out-of-class
3303 : : declaration of a member results in a call to duplicate_decls,
3304 : : freeing these nodes represents in a significant savings.
3305 : :
3306 : : Before releasing the node, be sore to remove function from symbol
3307 : : table that might have been inserted there to record comdat group.
3308 : : Be sure to however do not free DECL_STRUCT_FUNCTION because this
3309 : : structure is shared in between newdecl and oldecl. */
3310 : 13693617 : if (TREE_CODE (newdecl) == FUNCTION_DECL)
3311 : 13308679 : DECL_STRUCT_FUNCTION (newdecl) = NULL;
3312 : 13693617 : if (VAR_OR_FUNCTION_DECL_P (newdecl))
3313 : : {
3314 : 13670842 : struct symtab_node *snode = symtab_node::get (newdecl);
3315 : 13670842 : if (snode)
3316 : 392 : snode->remove ();
3317 : : }
3318 : :
3319 : 13693617 : if (TREE_CODE (olddecl) == FUNCTION_DECL)
3320 : : {
3321 : 13308679 : tree clone;
3322 : 13794660 : FOR_EACH_CLONE (clone, olddecl)
3323 : : {
3324 : 485981 : DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3325 : 485981 : DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3326 : : }
3327 : : }
3328 : :
3329 : : /* Remove the associated constraints for newdecl, if any, before
3330 : : reclaiming memory. */
3331 : 13693617 : if (flag_concepts)
3332 : 2937703 : remove_constraints (newdecl);
3333 : :
3334 : 13693617 : ggc_free (newdecl);
3335 : :
3336 : 13693617 : return olddecl;
3337 : : }
3338 : :
3339 : : /* Return zero if the declaration NEWDECL is valid
3340 : : when the declaration OLDDECL (assumed to be for the same name)
3341 : : has already been seen.
3342 : : Otherwise return an error message format string with a %s
3343 : : where the identifier should go. */
3344 : :
3345 : : static const char *
3346 : 13426277 : redeclaration_error_message (tree newdecl, tree olddecl)
3347 : : {
3348 : 13426308 : if (TREE_CODE (newdecl) == TYPE_DECL)
3349 : : {
3350 : : /* Because C++ can put things into name space for free,
3351 : : constructs like "typedef struct foo { ... } foo"
3352 : : would look like an erroneous redeclaration. */
3353 : 58518 : if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3354 : : return NULL;
3355 : : else
3356 : : return G_("redefinition of %q#D");
3357 : : }
3358 : 13367790 : else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3359 : : {
3360 : : /* If this is a pure function, its olddecl will actually be
3361 : : the original initialization to `0' (which we force to call
3362 : : abort()). Don't complain about redefinition in this case. */
3363 : 19486762 : if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3364 : 9743405 : && DECL_INITIAL (olddecl) == NULL_TREE)
3365 : : return NULL;
3366 : :
3367 : : /* If both functions come from different namespaces, this is not
3368 : : a redeclaration - this is a conflict with a used function. */
3369 : 19486722 : if (DECL_NAMESPACE_SCOPE_P (olddecl)
3370 : 1494640 : && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3371 : 9743365 : && ! decls_match (olddecl, newdecl))
3372 : : return G_("%qD conflicts with used function");
3373 : :
3374 : : /* We'll complain about linkage mismatches in
3375 : : warn_extern_redeclared_static. */
3376 : :
3377 : : /* Defining the same name twice is no good. */
3378 : 9743361 : if (decl_defined_p (olddecl)
3379 : 9743361 : && decl_defined_p (newdecl))
3380 : : {
3381 : 380 : if (DECL_NAME (olddecl) == NULL_TREE)
3382 : : return G_("%q#D not declared in class");
3383 : 544 : else if (!GNU_INLINE_P (olddecl)
3384 : 492 : || GNU_INLINE_P (newdecl))
3385 : 272 : return G_("redefinition of %q#D");
3386 : : }
3387 : :
3388 : 10368268 : if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3389 : : {
3390 : 581247 : bool olda = GNU_INLINE_P (olddecl);
3391 : 581247 : bool newa = GNU_INLINE_P (newdecl);
3392 : :
3393 : 581247 : if (olda != newa)
3394 : : {
3395 : 20 : if (newa)
3396 : : return G_("%q+D redeclared inline with "
3397 : : "%<gnu_inline%> attribute");
3398 : : else
3399 : : return G_("%q+D redeclared inline without "
3400 : : "%<gnu_inline%> attribute");
3401 : : }
3402 : : }
3403 : :
3404 : : /* [class.compare.default]: A definition of a comparison operator as
3405 : : defaulted that appears in a class shall be the first declaration of
3406 : : that function. */
3407 : 9743069 : special_function_kind sfk = special_function_p (olddecl);
3408 : 9743069 : if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3409 : : return G_("comparison operator %q+D defaulted after "
3410 : : "its first declaration");
3411 : :
3412 : 9743068 : check_abi_tag_redeclaration
3413 : 9743068 : (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3414 : 9743068 : lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3415 : :
3416 : 9743068 : return NULL;
3417 : : }
3418 : 3624409 : else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3419 : : {
3420 : 3261983 : tree nt, ot;
3421 : :
3422 : 3261983 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3423 : : return G_("redefinition of %q#D");
3424 : :
3425 : 3261981 : if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3426 : 31 : return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3427 : 31 : DECL_TEMPLATE_RESULT (olddecl));
3428 : :
3429 : 3261950 : if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3430 : : return NULL;
3431 : :
3432 : 3261937 : nt = DECL_TEMPLATE_RESULT (newdecl);
3433 : 3261937 : if (DECL_TEMPLATE_INFO (nt))
3434 : 430866 : nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3435 : 3261937 : ot = DECL_TEMPLATE_RESULT (olddecl);
3436 : 3261937 : if (DECL_TEMPLATE_INFO (ot))
3437 : 3261937 : ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3438 : 5787024 : if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3439 : 3261986 : && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3440 : 17 : return G_("redefinition of %q#D");
3441 : :
3442 : 4355749 : if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3443 : : {
3444 : 667869 : bool olda = GNU_INLINE_P (ot);
3445 : 667869 : bool newa = GNU_INLINE_P (nt);
3446 : :
3447 : 667869 : if (olda != newa)
3448 : : {
3449 : 0 : if (newa)
3450 : : return G_("%q+D redeclared inline with "
3451 : : "%<gnu_inline%> attribute");
3452 : : else
3453 : : return G_("%q+D redeclared inline without "
3454 : : "%<gnu_inline%> attribute");
3455 : : }
3456 : : }
3457 : :
3458 : : /* Core issue #226 (C++11):
3459 : :
3460 : : If a friend function template declaration specifies a
3461 : : default template-argument, that declaration shall be a
3462 : : definition and shall be the only declaration of the
3463 : : function template in the translation unit. */
3464 : 3261920 : if ((cxx_dialect != cxx98)
3465 : 3246860 : && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3466 : 3464655 : && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3467 : : /*is_primary=*/true,
3468 : : /*is_partial=*/false,
3469 : : /*is_friend_decl=*/2))
3470 : : return G_("redeclaration of friend %q#D "
3471 : : "may not have default template arguments");
3472 : :
3473 : 3261917 : return NULL;
3474 : : }
3475 : 362426 : else if (VAR_P (newdecl)
3476 : 362331 : && (CP_DECL_THREAD_LOCAL_P (newdecl)
3477 : 362331 : != CP_DECL_THREAD_LOCAL_P (olddecl))
3478 : 362471 : && (! DECL_LANG_SPECIFIC (olddecl)
3479 : 41 : || ! CP_DECL_THREADPRIVATE_P (olddecl)
3480 : 37 : || CP_DECL_THREAD_LOCAL_P (newdecl)))
3481 : : {
3482 : : /* Only variables can be thread-local, and all declarations must
3483 : : agree on this property. */
3484 : 8 : if (CP_DECL_THREAD_LOCAL_P (newdecl))
3485 : : return G_("thread-local declaration of %q#D follows "
3486 : : "non-thread-local declaration");
3487 : : else
3488 : 4 : return G_("non-thread-local declaration of %q#D follows "
3489 : : "thread-local declaration");
3490 : : }
3491 : 362418 : else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3492 : : {
3493 : : /* The objects have been declared at namespace scope. If either
3494 : : is a member of an anonymous union, then this is an invalid
3495 : : redeclaration. For example:
3496 : :
3497 : : int i;
3498 : : union { int i; };
3499 : :
3500 : : is invalid. */
3501 : 362258 : if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3502 : 724596 : || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3503 : : return G_("redeclaration of %q#D");
3504 : : /* If at least one declaration is a reference, there is no
3505 : : conflict. For example:
3506 : :
3507 : : int i = 3;
3508 : : extern int i;
3509 : :
3510 : : is valid. */
3511 : 362334 : if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3512 : : return NULL;
3513 : :
3514 : : /* Static data member declared outside a class definition
3515 : : if the variable is defined within the class with constexpr
3516 : : specifier is declaration rather than definition (and
3517 : : deprecated). */
3518 : 3077 : if (cxx_dialect >= cxx17
3519 : 3004 : && VAR_P (olddecl)
3520 : 2955 : && DECL_CLASS_SCOPE_P (olddecl)
3521 : 2931 : && DECL_DECLARED_CONSTEXPR_P (olddecl)
3522 : 5984 : && !DECL_INITIAL (newdecl))
3523 : : {
3524 : 2907 : DECL_EXTERNAL (newdecl) = 1;
3525 : : /* For now, only warn with explicit -Wdeprecated. */
3526 : 2907 : if (OPTION_SET_P (warn_deprecated))
3527 : : {
3528 : 32 : auto_diagnostic_group d;
3529 : 32 : if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3530 : : "redundant redeclaration of %<constexpr%> "
3531 : : "static data member %qD", newdecl))
3532 : 8 : inform (DECL_SOURCE_LOCATION (olddecl),
3533 : : "previous declaration of %qD", olddecl);
3534 : 32 : }
3535 : 2907 : return NULL;
3536 : : }
3537 : :
3538 : : /* Reject two definitions. */
3539 : : return G_("redefinition of %q#D");
3540 : : }
3541 : : else
3542 : : {
3543 : : /* Objects declared with block scope: */
3544 : : /* Reject two definitions, and reject a definition
3545 : : together with an external reference. */
3546 : 68 : if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3547 : : return G_("redeclaration of %q#D");
3548 : : return NULL;
3549 : : }
3550 : : }
3551 : :
3552 : :
3553 : : /* Hash and equality functions for the named_label table. */
3554 : :
3555 : : hashval_t
3556 : 65913 : named_label_hash::hash (const value_type entry)
3557 : : {
3558 : 65913 : return IDENTIFIER_HASH_VALUE (entry->name);
3559 : : }
3560 : :
3561 : : bool
3562 : 66310 : named_label_hash::equal (const value_type entry, compare_type name)
3563 : : {
3564 : 66310 : return name == entry->name;
3565 : : }
3566 : :
3567 : : /* Look for a label named ID in the current function. If one cannot
3568 : : be found, create one. Return the named_label_entry, or NULL on
3569 : : failure. */
3570 : :
3571 : : static named_label_entry *
3572 : 18892 : lookup_label_1 (tree id, bool making_local_p)
3573 : : {
3574 : 18892 : auto_cond_timevar tv (TV_NAME_LOOKUP);
3575 : :
3576 : : /* You can't use labels at global scope. */
3577 : 18892 : if (current_function_decl == NULL_TREE)
3578 : : {
3579 : 4 : error ("label %qE referenced outside of any function", id);
3580 : 4 : return NULL;
3581 : : }
3582 : :
3583 : 18888 : if (!named_labels)
3584 : 2643 : named_labels = hash_table<named_label_hash>::create_ggc (13);
3585 : :
3586 : 18888 : hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3587 : 18888 : named_label_entry **slot
3588 : 18888 : = named_labels->find_slot_with_hash (id, hash, INSERT);
3589 : 18888 : named_label_entry *old = *slot;
3590 : :
3591 : 18888 : if (old && old->label_decl)
3592 : : {
3593 : 2672 : if (!making_local_p)
3594 : : return old;
3595 : :
3596 : 12 : if (old->binding_level == current_binding_level)
3597 : : {
3598 : 0 : error ("local label %qE conflicts with existing label", id);
3599 : 0 : inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3600 : 0 : return NULL;
3601 : : }
3602 : : }
3603 : :
3604 : : /* We are making a new decl, create or reuse the named_label_entry */
3605 : 16240 : named_label_entry *ent = NULL;
3606 : 28 : if (old && !old->label_decl)
3607 : : ent = old;
3608 : : else
3609 : : {
3610 : 16212 : ent = ggc_cleared_alloc<named_label_entry> ();
3611 : 16212 : ent->name = id;
3612 : 16212 : ent->outer = old;
3613 : 16212 : *slot = ent;
3614 : : }
3615 : :
3616 : : /* Now create the LABEL_DECL. */
3617 : 16228 : tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3618 : :
3619 : 16228 : DECL_CONTEXT (decl) = current_function_decl;
3620 : 16228 : SET_DECL_MODE (decl, VOIDmode);
3621 : 16228 : if (making_local_p)
3622 : : {
3623 : 260 : C_DECLARED_LABEL_FLAG (decl) = true;
3624 : 260 : DECL_CHAIN (decl) = current_binding_level->names;
3625 : 260 : current_binding_level->names = decl;
3626 : : }
3627 : :
3628 : 16228 : ent->label_decl = decl;
3629 : :
3630 : 16228 : return ent;
3631 : 18892 : }
3632 : :
3633 : : /* Wrapper for lookup_label_1. */
3634 : :
3635 : : tree
3636 : 2137 : lookup_label (tree id)
3637 : : {
3638 : 2137 : named_label_entry *ent = lookup_label_1 (id, false);
3639 : 2137 : return ent ? ent->label_decl : NULL_TREE;
3640 : : }
3641 : :
3642 : : /* Remember that we've seen &&ID. */
3643 : :
3644 : : void
3645 : 267 : mark_label_addressed (tree id)
3646 : : {
3647 : 267 : named_label_entry *ent = lookup_label_1 (id, false);
3648 : 267 : ent->addressed = true;
3649 : 267 : }
3650 : :
3651 : : tree
3652 : 260 : declare_local_label (tree id)
3653 : : {
3654 : 260 : named_label_entry *ent = lookup_label_1 (id, true);
3655 : 260 : return ent ? ent->label_decl : NULL_TREE;
3656 : : }
3657 : :
3658 : : /* Returns true if it is ill-formed to jump past the declaration of DECL. */
3659 : :
3660 : : static bool
3661 : 13630 : decl_jump_unsafe (tree decl)
3662 : : {
3663 : : /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3664 : : with automatic storage duration is not in scope to a point where it is
3665 : : in scope is ill-formed unless the variable has scalar type, class type
3666 : : with a trivial default constructor and a trivial destructor, a
3667 : : cv-qualified version of one of these types, or an array of one of the
3668 : : preceding types and is declared without an initializer (8.5). */
3669 : 13630 : tree type = TREE_TYPE (decl);
3670 : :
3671 : 13630 : return (type != error_mark_node
3672 : : && VAR_P (decl)
3673 : 13606 : && !TREE_STATIC (decl)
3674 : 26000 : && (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3675 : 11063 : || variably_modified_type_p (type, NULL_TREE)));
3676 : : }
3677 : :
3678 : : /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3679 : : to the user. */
3680 : :
3681 : : static bool
3682 : 327 : identify_goto (tree decl, location_t loc, const location_t *locus,
3683 : : diagnostic_t diag_kind, bool computed)
3684 : : {
3685 : 327 : if (computed)
3686 : 31 : diag_kind = DK_WARNING;
3687 : 327 : bool complained
3688 : 439 : = emit_diagnostic (diag_kind, loc, 0,
3689 : : decl ? G_("jump to label %qD")
3690 : : : G_("jump to case label"), decl);
3691 : 327 : if (complained && locus)
3692 : : {
3693 : 215 : if (computed)
3694 : 31 : inform (*locus, " as a possible target of computed goto");
3695 : : else
3696 : 184 : inform (*locus, " from here");
3697 : : }
3698 : 327 : return complained;
3699 : : }
3700 : :
3701 : : /* Check that a single previously seen jump to a newly defined label
3702 : : is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3703 : : the jump context; NAMES are the names in scope in LEVEL at the jump
3704 : : context; LOCUS is the source position of the jump or 0. COMPUTED
3705 : : is a vec of decls if the jump is a computed goto. Returns
3706 : : true if all is well. */
3707 : :
3708 : : static bool
3709 : 1567689 : check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3710 : : bool exited_omp, const location_t *locus,
3711 : : vec<tree,va_gc> *computed)
3712 : : {
3713 : 1567689 : cp_binding_level *b;
3714 : 1567689 : bool complained = false;
3715 : 1567689 : int identified = 0;
3716 : 1567689 : bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3717 : 1567689 : bool saw_ceif = false, saw_se = false;
3718 : :
3719 : 1567689 : if (exited_omp)
3720 : : {
3721 : 16 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3722 : : computed);
3723 : 16 : if (complained)
3724 : 16 : inform (input_location, " exits OpenMP structured block");
3725 : : saw_omp = true;
3726 : : identified = 2;
3727 : : }
3728 : :
3729 : 3134604 : for (b = current_binding_level; b ; b = b->level_chain)
3730 : : {
3731 : 3134604 : tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3732 : :
3733 : 3144508 : for (new_decls = b->names; new_decls != old_decls;
3734 : 10184 : new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3735 : 280 : : TREE_CHAIN (new_decls)))
3736 : : {
3737 : 9904 : bool problem = decl_jump_unsafe (new_decls);
3738 : 9904 : if (! problem)
3739 : 9846 : continue;
3740 : :
3741 : 58 : if (!identified)
3742 : : {
3743 : 58 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3744 : : computed);
3745 : 58 : identified = 2;
3746 : : }
3747 : 58 : if (complained)
3748 : 58 : inform (DECL_SOURCE_LOCATION (new_decls),
3749 : : " crosses initialization of %q#D", new_decls);
3750 : : }
3751 : :
3752 : 3134604 : if (b == level)
3753 : : break;
3754 : :
3755 : 1566915 : const char *inf = NULL;
3756 : 1566915 : location_t loc = input_location;
3757 : 1566915 : switch (b->kind)
3758 : : {
3759 : 4 : case sk_try:
3760 : 4 : if (!saw_eh)
3761 : 1566915 : inf = G_(" enters %<try%> block");
3762 : : saw_eh = true;
3763 : : break;
3764 : :
3765 : 12 : case sk_catch:
3766 : 12 : if (!saw_eh)
3767 : 1566915 : inf = G_(" enters %<catch%> block");
3768 : : saw_eh = true;
3769 : : break;
3770 : :
3771 : 136 : case sk_omp:
3772 : 136 : if (!saw_omp)
3773 : 108 : inf = G_(" enters OpenMP structured block");
3774 : : saw_omp = true;
3775 : : break;
3776 : :
3777 : 4 : case sk_transaction:
3778 : 4 : if (!saw_tm)
3779 : 4 : inf = G_(" enters synchronized or atomic statement");
3780 : : saw_tm = true;
3781 : : break;
3782 : :
3783 : 15 : case sk_stmt_expr:
3784 : 15 : if (!saw_se)
3785 : 15 : inf = G_(" enters statement expression");
3786 : : saw_se = true;
3787 : : break;
3788 : :
3789 : 1566642 : case sk_block:
3790 : 1566642 : if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3791 : : {
3792 : 9 : inf = G_(" enters %<constexpr if%> statement");
3793 : 9 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3794 : : saw_cxif = true;
3795 : : }
3796 : 1566633 : else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3797 : : {
3798 : 6 : inf = G_(" enters %<consteval if%> statement");
3799 : 6 : loc = EXPR_LOCATION (b->level_chain->this_entity);
3800 : : saw_ceif = true;
3801 : : }
3802 : : break;
3803 : :
3804 : : default:
3805 : : break;
3806 : : }
3807 : :
3808 : 1566915 : if (inf)
3809 : : {
3810 : 158 : if (identified < 2)
3811 : 158 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3812 : : computed);
3813 : 158 : identified = 2;
3814 : 158 : if (complained)
3815 : 158 : inform (loc, inf);
3816 : : }
3817 : : }
3818 : :
3819 : 1567689 : if (!vec_safe_is_empty (computed))
3820 : : {
3821 : 12 : if (!identified)
3822 : 8 : complained = identify_goto (decl, input_location, locus, DK_ERROR,
3823 : : computed);
3824 : 12 : identified = 2;
3825 : 12 : if (complained)
3826 : 24 : for (tree d : computed)
3827 : : {
3828 : 12 : if (DECL_P (d))
3829 : 8 : inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d);
3830 : 4 : else if (d == get_identifier ("catch"))
3831 : 4 : inform (*locus, " does not clean up handled exception");
3832 : : }
3833 : : }
3834 : :
3835 : 1567689 : return !identified;
3836 : : }
3837 : :
3838 : : static void
3839 : 1339 : check_previous_goto (tree decl, struct named_label_use_entry *use)
3840 : : {
3841 : 1339 : check_previous_goto_1 (decl, use->binding_level,
3842 : 1339 : use->names_in_scope, use->in_omp_scope,
3843 : 1339 : &use->o_goto_locus, use->computed_goto);
3844 : 1339 : }
3845 : :
3846 : : static bool
3847 : 1566350 : check_switch_goto (cp_binding_level* level)
3848 : : {
3849 : 1566350 : return check_previous_goto_1 (NULL_TREE, level, level->names,
3850 : 1566350 : false, NULL, nullptr);
3851 : : }
3852 : :
3853 : : /* Check that a new jump to a label ENT is OK. COMPUTED is true
3854 : : if this is a possible target of a computed goto. */
3855 : :
3856 : : void
3857 : 2004 : check_goto_1 (named_label_entry *ent, bool computed)
3858 : : {
3859 : 2004 : tree decl = ent->label_decl;
3860 : :
3861 : : /* If the label hasn't been defined yet, defer checking. */
3862 : 2004 : if (! DECL_INITIAL (decl))
3863 : : {
3864 : : /* Don't bother creating another use if the last goto had the
3865 : : same data, and will therefore create the same set of errors. */
3866 : 1544 : if (ent->uses
3867 : 322 : && ent->uses->names_in_scope == current_binding_level->names)
3868 : 2004 : return;
3869 : :
3870 : 1347 : named_label_use_entry *new_use
3871 : 1347 : = ggc_alloc<named_label_use_entry> ();
3872 : 1347 : new_use->binding_level = current_binding_level;
3873 : 1347 : new_use->names_in_scope = current_binding_level->names;
3874 : 1347 : new_use->o_goto_locus = input_location;
3875 : 1347 : new_use->in_omp_scope = false;
3876 : 1347 : new_use->computed_goto = computed ? make_tree_vector () : nullptr;
3877 : :
3878 : 1347 : new_use->next = ent->uses;
3879 : 1347 : ent->uses = new_use;
3880 : 1347 : return;
3881 : : }
3882 : :
3883 : 460 : bool saw_catch = false, complained = false;
3884 : 460 : int identified = 0;
3885 : 460 : tree bad;
3886 : 460 : unsigned ix;
3887 : :
3888 : 448 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3889 : 440 : || ent->in_constexpr_if || ent->in_consteval_if
3890 : 436 : || ent->in_omp_scope || ent->in_stmt_expr
3891 : 888 : || !vec_safe_is_empty (ent->bad_decls))
3892 : : {
3893 : 40 : diagnostic_t diag_kind = DK_PERMERROR;
3894 : 40 : if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3895 : 21 : || ent->in_consteval_if || ent->in_transaction_scope
3896 : 16 : || ent->in_omp_scope || ent->in_stmt_expr)
3897 : 32 : diag_kind = DK_ERROR;
3898 : 40 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3899 : : &input_location, diag_kind, computed);
3900 : 40 : identified = 1 + (diag_kind == DK_ERROR);
3901 : : }
3902 : :
3903 : 468 : FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3904 : : {
3905 : 8 : bool problem = decl_jump_unsafe (bad);
3906 : :
3907 : 8 : if (problem && DECL_ARTIFICIAL (bad))
3908 : : {
3909 : : /* Can't skip init of __exception_info. */
3910 : 0 : if (identified == 1)
3911 : : {
3912 : 0 : complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3913 : : &input_location, DK_ERROR, computed);
3914 : 0 : identified = 2;
3915 : : }
3916 : 0 : if (complained)
3917 : 0 : inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3918 : : saw_catch = true;
3919 : : }
3920 : 8 : else if (complained)
3921 : 8 : inform (DECL_SOURCE_LOCATION (bad),
3922 : : " skips initialization of %q#D", bad);
3923 : : }
3924 : :
3925 : 460 : if (complained)
3926 : : {
3927 : 40 : if (ent->in_try_scope)
3928 : 12 : inform (input_location, " enters %<try%> block");
3929 : 28 : else if (ent->in_catch_scope && !saw_catch)
3930 : 4 : inform (input_location, " enters %<catch%> block");
3931 : 24 : else if (ent->in_transaction_scope)
3932 : 4 : inform (input_location, " enters synchronized or atomic statement");
3933 : 20 : else if (ent->in_constexpr_if)
3934 : 3 : inform (input_location, " enters %<constexpr if%> statement");
3935 : 17 : else if (ent->in_consteval_if)
3936 : 1 : inform (input_location, " enters %<consteval if%> statement");
3937 : 16 : else if (ent->in_stmt_expr)
3938 : 4 : inform (input_location, " enters statement expression");
3939 : : }
3940 : :
3941 : 460 : if (ent->in_omp_scope)
3942 : : {
3943 : 4 : if (complained)
3944 : 4 : inform (input_location, " enters OpenMP structured block");
3945 : : }
3946 : 456 : else if (flag_openmp)
3947 : 50 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3948 : : {
3949 : 50 : if (b == ent->binding_level)
3950 : : break;
3951 : 44 : if (b->kind == sk_omp)
3952 : : {
3953 : 28 : if (identified < 2)
3954 : : {
3955 : 56 : complained = identify_goto (decl,
3956 : 28 : DECL_SOURCE_LOCATION (decl),
3957 : : &input_location, DK_ERROR,
3958 : : computed);
3959 : 28 : identified = 2;
3960 : : }
3961 : 28 : if (complained)
3962 : 28 : inform (input_location, " exits OpenMP structured block");
3963 : : break;
3964 : : }
3965 : : }
3966 : :
3967 : : /* Warn if a computed goto might involve a local variable going out of scope
3968 : : without being cleaned up. */
3969 : 460 : if (computed)
3970 : : {
3971 : 70 : auto level = ent->binding_level;
3972 : 70 : auto names = ent->names_in_scope;
3973 : 70 : for (auto b = current_binding_level; ; b = b->level_chain)
3974 : : {
3975 : 121 : if (b->kind == sk_catch)
3976 : : {
3977 : 8 : if (!identified)
3978 : : {
3979 : 8 : complained
3980 : 8 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3981 : : &input_location, DK_ERROR, computed);
3982 : 8 : identified = 2;
3983 : : }
3984 : 8 : if (complained)
3985 : 8 : inform (input_location,
3986 : : " does not clean up handled exception");
3987 : : }
3988 : 121 : tree end = b == level ? names : NULL_TREE;
3989 : 136 : for (tree d = b->names; d != end; d = DECL_CHAIN (d))
3990 : : {
3991 : 15 : if (TREE_CODE (d) == VAR_DECL && !TREE_STATIC (d)
3992 : 15 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d)))
3993 : : {
3994 : 11 : if (!identified)
3995 : : {
3996 : 11 : complained
3997 : 11 : = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3998 : : &input_location, DK_ERROR, computed);
3999 : 11 : identified = 2;
4000 : : }
4001 : 11 : if (complained)
4002 : 11 : inform (DECL_SOURCE_LOCATION (d),
4003 : : " does not destroy %qD", d);
4004 : : }
4005 : : }
4006 : 121 : if (b == level)
4007 : : break;
4008 : 51 : }
4009 : : }
4010 : : }
4011 : :
4012 : : /* Check that a new jump to a label DECL is OK. Called by
4013 : : finish_goto_stmt. */
4014 : :
4015 : : void
4016 : 1961 : check_goto (tree decl)
4017 : : {
4018 : 1961 : if (!named_labels)
4019 : : return;
4020 : 1941 : if (TREE_CODE (decl) != LABEL_DECL)
4021 : : {
4022 : : /* We don't know where a computed goto is jumping,
4023 : : so check all addressable labels. */
4024 : 329 : for (auto iter = named_labels->begin ();
4025 : 444 : iter != named_labels->end ();
4026 : 214 : ++iter)
4027 : : {
4028 : 214 : auto ent = *iter;
4029 : 214 : if (ent->addressed)
4030 : 178 : check_goto_1 (ent, true);
4031 : : }
4032 : : }
4033 : : else
4034 : : {
4035 : 1826 : hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
4036 : 1826 : named_label_entry **slot
4037 : 1826 : = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
4038 : 1826 : named_label_entry *ent = *slot;
4039 : 1826 : check_goto_1 (ent, false);
4040 : : }
4041 : : }
4042 : :
4043 : : /* Check that a return is ok wrt OpenMP structured blocks.
4044 : : Called by finish_return_stmt. Returns true if all is well. */
4045 : :
4046 : : bool
4047 : 51516 : check_omp_return (void)
4048 : : {
4049 : 150246 : for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
4050 : 150246 : if (b->kind == sk_omp)
4051 : : {
4052 : 20 : error ("invalid exit from OpenMP structured block");
4053 : 20 : return false;
4054 : : }
4055 : 150226 : else if (b->kind == sk_function_parms)
4056 : : break;
4057 : : return true;
4058 : : }
4059 : :
4060 : : /* Define a label, specifying the location in the source file.
4061 : : Return the LABEL_DECL node for the label. */
4062 : :
4063 : : tree
4064 : 16228 : define_label (location_t location, tree name)
4065 : : {
4066 : 16228 : auto_cond_timevar tv (TV_NAME_LOOKUP);
4067 : :
4068 : : /* After labels, make any new cleanups in the function go into their
4069 : : own new (temporary) binding contour. */
4070 : 16228 : for (cp_binding_level *p = current_binding_level;
4071 : 36282 : p->kind != sk_function_parms;
4072 : 20054 : p = p->level_chain)
4073 : 20054 : p->more_cleanups_ok = 0;
4074 : :
4075 : 16228 : named_label_entry *ent = lookup_label_1 (name, false);
4076 : 16228 : tree decl = ent->label_decl;
4077 : :
4078 : 16228 : if (DECL_INITIAL (decl) != NULL_TREE)
4079 : : {
4080 : 8 : error ("duplicate label %qD", decl);
4081 : 8 : return error_mark_node;
4082 : : }
4083 : : else
4084 : : {
4085 : : /* Mark label as having been defined. */
4086 : 16220 : DECL_INITIAL (decl) = error_mark_node;
4087 : : /* Say where in the source. */
4088 : 16220 : DECL_SOURCE_LOCATION (decl) = location;
4089 : :
4090 : 16220 : ent->binding_level = current_binding_level;
4091 : 16220 : ent->names_in_scope = current_binding_level->names;
4092 : :
4093 : 17559 : for (named_label_use_entry *use = ent->uses; use; use = use->next)
4094 : 1339 : check_previous_goto (decl, use);
4095 : 16220 : ent->uses = NULL;
4096 : : }
4097 : :
4098 : 16220 : return decl;
4099 : 16228 : }
4100 : :
4101 : : struct cp_switch
4102 : : {
4103 : : cp_binding_level *level;
4104 : : struct cp_switch *next;
4105 : : /* The SWITCH_STMT being built. */
4106 : : tree switch_stmt;
4107 : : /* A splay-tree mapping the low element of a case range to the high
4108 : : element, or NULL_TREE if there is no high element. Used to
4109 : : determine whether or not a new case label duplicates an old case
4110 : : label. We need a tree, rather than simply a hash table, because
4111 : : of the GNU case range extension. */
4112 : : splay_tree cases;
4113 : : /* Remember whether a default: case label has been seen. */
4114 : : bool has_default_p;
4115 : : /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
4116 : : bool break_stmt_seen_p;
4117 : : /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
4118 : : where BREAK_STMT doesn't belong to the SWITCH_STMT. */
4119 : : bool in_loop_body_p;
4120 : : };
4121 : :
4122 : : /* A stack of the currently active switch statements. The innermost
4123 : : switch statement is on the top of the stack. There is no need to
4124 : : mark the stack for garbage collection because it is only active
4125 : : during the processing of the body of a function, and we never
4126 : : collect at that point. */
4127 : :
4128 : : static struct cp_switch *switch_stack;
4129 : :
4130 : : /* Called right after a switch-statement condition is parsed.
4131 : : SWITCH_STMT is the switch statement being parsed. */
4132 : :
4133 : : void
4134 : 415288 : push_switch (tree switch_stmt)
4135 : : {
4136 : 415288 : struct cp_switch *p = XNEW (struct cp_switch);
4137 : 415288 : p->level = current_binding_level;
4138 : 415288 : p->next = switch_stack;
4139 : 415288 : p->switch_stmt = switch_stmt;
4140 : 415288 : p->cases = splay_tree_new (case_compare, NULL, NULL);
4141 : 415288 : p->has_default_p = false;
4142 : 415288 : p->break_stmt_seen_p = false;
4143 : 415288 : p->in_loop_body_p = false;
4144 : 415288 : switch_stack = p;
4145 : 415288 : }
4146 : :
4147 : : void
4148 : 415288 : pop_switch (void)
4149 : : {
4150 : 415288 : struct cp_switch *cs = switch_stack;
4151 : :
4152 : : /* Emit warnings as needed. */
4153 : 415288 : location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
4154 : 415288 : tree cond = SWITCH_STMT_COND (cs->switch_stmt);
4155 : 415288 : const bool bool_cond_p
4156 : 415288 : = (SWITCH_STMT_TYPE (cs->switch_stmt)
4157 : 415288 : && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
4158 : 415288 : if (!processing_template_decl)
4159 : 258547 : c_do_switch_warnings (cs->cases, switch_location,
4160 : 258547 : SWITCH_STMT_TYPE (cs->switch_stmt), cond,
4161 : : bool_cond_p);
4162 : :
4163 : : /* For the benefit of block_may_fallthru remember if the switch body
4164 : : case labels cover all possible values and if there are break; stmts. */
4165 : 415288 : if (cs->has_default_p
4166 : 415288 : || (!processing_template_decl
4167 : 38224 : && c_switch_covers_all_cases_p (cs->cases,
4168 : 38224 : SWITCH_STMT_TYPE (cs->switch_stmt))))
4169 : 321223 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
4170 : 415288 : if (!cs->break_stmt_seen_p)
4171 : 210441 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
4172 : : /* Now that we're done with the switch warnings, set the switch type
4173 : : to the type of the condition if the index type was of scoped enum type.
4174 : : (Such types don't participate in the integer promotions.) We do this
4175 : : because of bit-fields whose declared type is a scoped enum type:
4176 : : gimplification will use the lowered index type, but convert the
4177 : : case values to SWITCH_STMT_TYPE, which would have been the declared type
4178 : : and verify_gimple_switch doesn't accept that. */
4179 : 415288 : if (is_bitfield_expr_with_lowered_type (cond))
4180 : 13 : SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
4181 : 415288 : gcc_assert (!cs->in_loop_body_p);
4182 : 415288 : splay_tree_delete (cs->cases);
4183 : 415288 : switch_stack = switch_stack->next;
4184 : 415288 : free (cs);
4185 : 415288 : }
4186 : :
4187 : : /* Note that a BREAK_STMT is about to be added. If it is inside of
4188 : : a SWITCH_STMT and not inside of a loop body inside of it, note
4189 : : in switch_stack we've seen a BREAK_STMT. */
4190 : :
4191 : : void
4192 : 3190661 : note_break_stmt (void)
4193 : : {
4194 : 3190661 : if (switch_stack && !switch_stack->in_loop_body_p)
4195 : 1319142 : switch_stack->break_stmt_seen_p = true;
4196 : 3190661 : }
4197 : :
4198 : : /* Note the start of processing of an iteration statement's body.
4199 : : The note_break_stmt function will do nothing while processing it.
4200 : : Return a flag that should be passed to note_iteration_stmt_body_end. */
4201 : :
4202 : : bool
4203 : 13665892 : note_iteration_stmt_body_start (void)
4204 : : {
4205 : 13665892 : if (!switch_stack)
4206 : : return false;
4207 : 102007 : bool ret = switch_stack->in_loop_body_p;
4208 : 102007 : switch_stack->in_loop_body_p = true;
4209 : 102007 : return ret;
4210 : : }
4211 : :
4212 : : /* Note the end of processing of an iteration statement's body. */
4213 : :
4214 : : void
4215 : 13665892 : note_iteration_stmt_body_end (bool prev)
4216 : : {
4217 : 13665892 : if (switch_stack)
4218 : 102007 : switch_stack->in_loop_body_p = prev;
4219 : 13665892 : }
4220 : :
4221 : : /* Convert a case constant VALUE in a switch to the type TYPE of the switch
4222 : : condition. Note that if TYPE and VALUE are already integral we don't
4223 : : really do the conversion because the language-independent
4224 : : warning/optimization code will work better that way. */
4225 : :
4226 : : static tree
4227 : 3132436 : case_conversion (tree type, tree value)
4228 : : {
4229 : 3132436 : if (value == NULL_TREE)
4230 : : return value;
4231 : :
4232 : 1346088 : value = mark_rvalue_use (value);
4233 : :
4234 : 1346088 : if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
4235 : 1333662 : type = type_promotes_to (type);
4236 : :
4237 : 1346088 : tree ovalue = value;
4238 : : /* The constant-expression VALUE shall be a converted constant expression
4239 : : of the adjusted type of the switch condition, which doesn't allow
4240 : : narrowing conversions. */
4241 : 1346088 : value = build_converted_constant_expr (type, value, tf_warning_or_error);
4242 : :
4243 : 1346088 : if (cxx_dialect >= cxx11
4244 : 1346088 : && (SCOPED_ENUM_P (type)
4245 : 1330949 : || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
4246 : : /* Use the converted value. */;
4247 : : else
4248 : : /* The already integral case. */
4249 : : value = ovalue;
4250 : :
4251 : 1346088 : return cxx_constant_value (value);
4252 : : }
4253 : :
4254 : : /* Note that we've seen a definition of a case label, and complain if this
4255 : : is a bad place for one. */
4256 : :
4257 : : tree
4258 : 2993681 : finish_case_label (location_t loc, tree low_value, tree high_value)
4259 : : {
4260 : 2993681 : tree cond, r;
4261 : 2993681 : cp_binding_level *p;
4262 : 2993681 : tree type;
4263 : :
4264 : 2993681 : if (low_value == NULL_TREE && high_value == NULL_TREE)
4265 : 321183 : switch_stack->has_default_p = true;
4266 : :
4267 : 2993681 : if (processing_template_decl)
4268 : : {
4269 : 1427331 : tree label;
4270 : :
4271 : : /* For templates, just add the case label; we'll do semantic
4272 : : analysis at instantiation-time. */
4273 : 1427331 : label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
4274 : 1427331 : return add_stmt (build_case_label (low_value, high_value, label));
4275 : : }
4276 : :
4277 : : /* Find the condition on which this switch statement depends. */
4278 : 1566350 : cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
4279 : 1566350 : if (cond && TREE_CODE (cond) == TREE_LIST)
4280 : 0 : cond = TREE_VALUE (cond);
4281 : :
4282 : 1566350 : if (!check_switch_goto (switch_stack->level))
4283 : 112 : return error_mark_node;
4284 : :
4285 : 1566238 : type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
4286 : 1566238 : if (type == error_mark_node)
4287 : : return error_mark_node;
4288 : :
4289 : 1566218 : low_value = case_conversion (type, low_value);
4290 : 1566218 : high_value = case_conversion (type, high_value);
4291 : :
4292 : 1566218 : r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
4293 : :
4294 : : /* After labels, make any new cleanups in the function go into their
4295 : : own new (temporary) binding contour. */
4296 : 1566218 : for (p = current_binding_level;
4297 : 6643971 : p->kind != sk_function_parms;
4298 : 5077753 : p = p->level_chain)
4299 : 5077753 : p->more_cleanups_ok = 0;
4300 : :
4301 : : return r;
4302 : : }
4303 : :
4304 : : struct typename_info {
4305 : : tree scope;
4306 : : tree name;
4307 : : tree template_id;
4308 : : bool enum_p;
4309 : : bool class_p;
4310 : : };
4311 : :
4312 : : struct typename_hasher : ggc_ptr_hash<tree_node>
4313 : : {
4314 : : typedef typename_info *compare_type;
4315 : :
4316 : : /* Hash a TYPENAME_TYPE. */
4317 : :
4318 : : static hashval_t
4319 : 409912060 : hash (tree context, tree fullname)
4320 : : {
4321 : 409912060 : hashval_t hash = 0;
4322 : 409912060 : hash = iterative_hash_object (context, hash);
4323 : 409912060 : hash = iterative_hash_object (fullname, hash);
4324 : 409912060 : return hash;
4325 : : }
4326 : :
4327 : : static hashval_t
4328 : 60557303 : hash (const typename_info *ti)
4329 : : {
4330 : 60557303 : return typename_hasher::hash (ti->scope, ti->template_id);
4331 : : }
4332 : :
4333 : : static hashval_t
4334 : 349354757 : hash (tree t)
4335 : : {
4336 : 349354757 : return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t));
4337 : : }
4338 : :
4339 : : /* Compare two TYPENAME_TYPEs. */
4340 : :
4341 : : static bool
4342 : 415709740 : equal (tree t1, const typename_info *t2)
4343 : : {
4344 : 415709740 : return (TYPE_IDENTIFIER (t1) == t2->name
4345 : 135393603 : && TYPE_CONTEXT (t1) == t2->scope
4346 : 35370180 : && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4347 : 35345241 : && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4348 : 451054925 : && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4349 : : }
4350 : : };
4351 : :
4352 : : /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4353 : : the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4354 : :
4355 : : Returns the new TYPENAME_TYPE. */
4356 : :
4357 : : static GTY (()) hash_table<typename_hasher> *typename_htab;
4358 : :
4359 : : tree
4360 : 60557303 : build_typename_type (tree context, tree name, tree fullname,
4361 : : enum tag_types tag_type)
4362 : : {
4363 : 60557303 : typename_info ti;
4364 : :
4365 : 60557303 : if (typename_htab == NULL)
4366 : 17847 : typename_htab = hash_table<typename_hasher>::create_ggc (61);
4367 : :
4368 : 60557303 : ti.scope = FROB_CONTEXT (context);
4369 : 60557303 : ti.name = name;
4370 : 60557303 : ti.template_id = fullname;
4371 : 60557303 : ti.enum_p = tag_type == enum_type;
4372 : 60557303 : ti.class_p = (tag_type == class_type
4373 : : || tag_type == record_type
4374 : 60557303 : || tag_type == union_type);
4375 : 60557303 : hashval_t hash = typename_hasher::hash (&ti);
4376 : :
4377 : : /* See if we already have this type. */
4378 : 60557303 : tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4379 : 60557303 : tree t = *e;
4380 : 60557303 : if (*e)
4381 : : t = *e;
4382 : : else
4383 : : {
4384 : : /* Build the TYPENAME_TYPE. */
4385 : 25212150 : t = cxx_make_type (TYPENAME_TYPE);
4386 : 25212150 : TYPE_CONTEXT (t) = ti.scope;
4387 : 25212150 : TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4388 : 25212150 : TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4389 : 25212150 : TYPENAME_IS_CLASS_P (t) = ti.class_p;
4390 : :
4391 : : /* Build the corresponding TYPE_DECL. */
4392 : 25212150 : tree d = build_decl (input_location, TYPE_DECL, name, t);
4393 : 25212150 : TYPE_NAME (t) = d;
4394 : 25212150 : TYPE_STUB_DECL (t) = d;
4395 : 25212150 : DECL_CONTEXT (d) = ti.scope;
4396 : 25212150 : DECL_ARTIFICIAL (d) = 1;
4397 : :
4398 : : /* Store it in the hash table. */
4399 : 25212150 : *e = t;
4400 : :
4401 : : /* TYPENAME_TYPEs must always be compared structurally, because
4402 : : they may or may not resolve down to another type depending on
4403 : : the currently open classes. */
4404 : 25212150 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4405 : : }
4406 : :
4407 : 60557303 : return t;
4408 : : }
4409 : :
4410 : : /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4411 : : provided to name the type. Returns an appropriate type, unless an
4412 : : error occurs, in which case error_mark_node is returned. If we
4413 : : locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4414 : : return that, rather than the _TYPE it corresponds to, in other
4415 : : cases we look through the type decl. If TF_ERROR is set, complain
4416 : : about errors, otherwise be quiet. */
4417 : :
4418 : : tree
4419 : 156778954 : make_typename_type (tree context, tree name, enum tag_types tag_type,
4420 : : tsubst_flags_t complain)
4421 : : {
4422 : 156778954 : tree fullname;
4423 : 156778954 : tree t;
4424 : 156778954 : bool want_template;
4425 : :
4426 : 156778954 : if (name == error_mark_node
4427 : 156778952 : || context == NULL_TREE
4428 : 156778952 : || context == error_mark_node)
4429 : : return error_mark_node;
4430 : :
4431 : 156778937 : if (TYPE_P (name))
4432 : : {
4433 : 0 : if (!(TYPE_LANG_SPECIFIC (name)
4434 : 0 : && (CLASSTYPE_IS_TEMPLATE (name)
4435 : 0 : || CLASSTYPE_USE_TEMPLATE (name))))
4436 : 0 : name = TYPE_IDENTIFIER (name);
4437 : : else
4438 : : /* Create a TEMPLATE_ID_EXPR for the type. */
4439 : 0 : name = build_nt (TEMPLATE_ID_EXPR,
4440 : 0 : CLASSTYPE_TI_TEMPLATE (name),
4441 : 0 : CLASSTYPE_TI_ARGS (name));
4442 : : }
4443 : 156778937 : else if (TREE_CODE (name) == TYPE_DECL)
4444 : 0 : name = DECL_NAME (name);
4445 : :
4446 : 156778937 : fullname = name;
4447 : :
4448 : 156778937 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4449 : : {
4450 : 4542822 : name = TREE_OPERAND (name, 0);
4451 : 4542822 : if (DECL_TYPE_TEMPLATE_P (name))
4452 : 0 : name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4453 : 4542822 : if (TREE_CODE (name) != IDENTIFIER_NODE)
4454 : : {
4455 : 0 : if (complain & tf_error)
4456 : 0 : error ("%qD is not a type", name);
4457 : 0 : return error_mark_node;
4458 : : }
4459 : : }
4460 : 156778937 : if (TREE_CODE (name) == TEMPLATE_DECL)
4461 : : {
4462 : 0 : if (complain & tf_error)
4463 : 0 : error ("%qD used without template arguments", name);
4464 : 0 : return error_mark_node;
4465 : : }
4466 : 156778937 : else if (is_overloaded_fn (name))
4467 : : {
4468 : 1 : if (complain & tf_error)
4469 : 1 : error ("%qD is a function, not a type", name);
4470 : 1 : return error_mark_node;
4471 : : }
4472 : 156778936 : gcc_assert (identifier_p (name));
4473 : 156778936 : gcc_assert (TYPE_P (context));
4474 : :
4475 : 156778936 : if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4476 : : /* This can happen for C++17 variadic using (c++/88986). */;
4477 : 146018282 : else if (!MAYBE_CLASS_TYPE_P (context))
4478 : : {
4479 : 6 : if (complain & tf_error)
4480 : 6 : error ("%q#T is not a class", context);
4481 : 6 : return error_mark_node;
4482 : : }
4483 : :
4484 : : /* When the CONTEXT is a dependent type, NAME could refer to a
4485 : : dependent base class of CONTEXT. But look inside it anyway
4486 : : if CONTEXT is a currently open scope, in case it refers to a
4487 : : member of the current instantiation or a non-dependent base;
4488 : : lookup will stop when we hit a dependent base. */
4489 : 156778930 : if (!dependent_scope_p (context))
4490 : : {
4491 : : /* We generally don't ignore non-types during TYPENAME_TYPE lookup
4492 : : (as per [temp.res.general]/3), unless
4493 : : - the tag corresponds to a class-key or 'enum' so
4494 : : [basic.lookup.elab] applies, or
4495 : : - the tag corresponds to scope_type or tf_qualifying_scope is
4496 : : set so [basic.lookup.qual]/1 applies.
4497 : : TODO: If we'd set/track the scope_type tag thoroughly on all
4498 : : TYPENAME_TYPEs that are followed by :: then we wouldn't need the
4499 : : tf_qualifying_scope flag. */
4500 : 218101176 : bool want_type = (tag_type != none_type && tag_type != typename_type)
4501 : 109050588 : || (complain & tf_qualifying_scope);
4502 : 109050588 : t = lookup_member (context, name, /*protect=*/2, want_type, complain);
4503 : : }
4504 : : else
4505 : : t = NULL_TREE;
4506 : :
4507 : 172230624 : if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4508 : 47729413 : return build_typename_type (context, name, fullname, tag_type);
4509 : :
4510 : 109049517 : want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4511 : :
4512 : 109049517 : if (!t)
4513 : : {
4514 : 15450623 : if (complain & tf_error)
4515 : : {
4516 : 236 : if (!COMPLETE_TYPE_P (context))
4517 : 22 : cxx_incomplete_type_error (NULL_TREE, context);
4518 : : else
4519 : 427 : error (want_template ? G_("no class template named %q#T in %q#T")
4520 : : : G_("no type named %q#T in %q#T"), name, context);
4521 : : }
4522 : 15450623 : return error_mark_node;
4523 : : }
4524 : :
4525 : : /* Pull out the template from an injected-class-name (or multiple). */
4526 : 93598894 : if (want_template)
4527 : 3444328 : t = maybe_get_template_decl_from_type_decl (t);
4528 : :
4529 : 93598894 : if (TREE_CODE (t) == TREE_LIST)
4530 : : {
4531 : 4 : if (complain & tf_error)
4532 : : {
4533 : 4 : error ("lookup of %qT in %qT is ambiguous", name, context);
4534 : 4 : print_candidates (t);
4535 : : }
4536 : 4 : return error_mark_node;
4537 : : }
4538 : :
4539 : 93598890 : if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4540 : : {
4541 : 7 : if (complain & tf_error)
4542 : 7 : error ("%<typename %T::%D%> names %q#T, which is not a class template",
4543 : : context, name, t);
4544 : 7 : return error_mark_node;
4545 : : }
4546 : 93598883 : if (!want_template && TREE_CODE (t) != TYPE_DECL)
4547 : : {
4548 : 16 : if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4549 : 42 : && DECL_TYPE_TEMPLATE_P (t))
4550 : : /* The caller permits this typename-specifier to name a template
4551 : : (because it appears in a CTAD-enabled context). */;
4552 : : else
4553 : : {
4554 : 24 : if (complain & tf_error)
4555 : 16 : error ("%<typename %T::%D%> names %q#D, which is not a type",
4556 : : context, name, t);
4557 : 24 : return error_mark_node;
4558 : : }
4559 : : }
4560 : :
4561 : 93598859 : if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4562 : : context, complain))
4563 : 26 : return error_mark_node;
4564 : :
4565 : 93598833 : if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4566 : 8 : return make_template_placeholder (t);
4567 : :
4568 : 93598825 : if (want_template)
4569 : : {
4570 : 3444321 : t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4571 : : NULL_TREE, context,
4572 : : /*entering_scope=*/0,
4573 : : complain | tf_user);
4574 : 3444321 : if (t == error_mark_node)
4575 : : return error_mark_node;
4576 : 3444316 : t = TYPE_NAME (t);
4577 : : }
4578 : :
4579 : 93598820 : if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4580 : 2602072 : t = TREE_TYPE (t);
4581 : :
4582 : 93598820 : maybe_record_typedef_use (t);
4583 : :
4584 : 93598820 : return t;
4585 : : }
4586 : :
4587 : : /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4588 : : can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4589 : : in which case error_mark_node is returned.
4590 : :
4591 : : If PARM_LIST is non-NULL, also make sure that the template parameter
4592 : : list of TEMPLATE_DECL matches.
4593 : :
4594 : : If COMPLAIN zero, don't complain about any errors that occur. */
4595 : :
4596 : : tree
4597 : 70016 : make_unbound_class_template (tree context, tree name, tree parm_list,
4598 : : tsubst_flags_t complain)
4599 : : {
4600 : 70016 : if (TYPE_P (name))
4601 : 0 : name = TYPE_IDENTIFIER (name);
4602 : 70016 : else if (DECL_P (name))
4603 : 0 : name = DECL_NAME (name);
4604 : 70016 : gcc_assert (identifier_p (name));
4605 : :
4606 : 70016 : if (!dependent_type_p (context)
4607 : 70016 : || currently_open_class (context))
4608 : : {
4609 : 21377 : tree tmpl = NULL_TREE;
4610 : :
4611 : 21377 : if (MAYBE_CLASS_TYPE_P (context))
4612 : 21369 : tmpl = lookup_field (context, name, 0, false);
4613 : :
4614 : 21369 : if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4615 : 4 : tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4616 : :
4617 : 21365 : if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4618 : : {
4619 : 12 : if (complain & tf_error)
4620 : 8 : error ("no class template named %q#T in %q#T", name, context);
4621 : 12 : return error_mark_node;
4622 : : }
4623 : :
4624 : 21365 : if (parm_list
4625 : 21365 : && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4626 : : {
4627 : 0 : if (complain & tf_error)
4628 : : {
4629 : 0 : error ("template parameters do not match template %qD", tmpl);
4630 : 0 : inform (DECL_SOURCE_LOCATION (tmpl),
4631 : : "%qD declared here", tmpl);
4632 : : }
4633 : 0 : return error_mark_node;
4634 : : }
4635 : :
4636 : 21365 : if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4637 : : complain))
4638 : 0 : return error_mark_node;
4639 : :
4640 : : return tmpl;
4641 : : }
4642 : :
4643 : 48639 : return make_unbound_class_template_raw (context, name, parm_list);
4644 : : }
4645 : :
4646 : : /* Build an UNBOUND_CLASS_TEMPLATE. */
4647 : :
4648 : : tree
4649 : 48708 : make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4650 : : {
4651 : : /* Build the UNBOUND_CLASS_TEMPLATE. */
4652 : 48708 : tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4653 : 48708 : TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4654 : 48708 : TREE_TYPE (t) = NULL_TREE;
4655 : 48708 : SET_TYPE_STRUCTURAL_EQUALITY (t);
4656 : :
4657 : : /* Build the corresponding TEMPLATE_DECL. */
4658 : 48708 : tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4659 : 48708 : TYPE_NAME (t) = d;
4660 : 48708 : TYPE_STUB_DECL (t) = d;
4661 : 48708 : DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4662 : 48708 : DECL_ARTIFICIAL (d) = 1;
4663 : 48708 : DECL_TEMPLATE_PARMS (d) = parm_list;
4664 : :
4665 : 48708 : return t;
4666 : : }
4667 : :
4668 : :
4669 : :
4670 : : /* Push the declarations of builtin types into the global namespace.
4671 : : RID_INDEX is the index of the builtin type in the array
4672 : : RID_POINTERS. NAME is the name used when looking up the builtin
4673 : : type. TYPE is the _TYPE node for the builtin type.
4674 : :
4675 : : The calls to set_global_binding below should be
4676 : : eliminated. Built-in types should not be looked up name; their
4677 : : names are keywords that the parser can recognize. However, there
4678 : : is code in c-common.cc that uses identifier_global_value to look up
4679 : : built-in types by name. */
4680 : :
4681 : : void
4682 : 4040628 : record_builtin_type (enum rid rid_index,
4683 : : const char* name,
4684 : : tree type)
4685 : : {
4686 : 4040628 : tree decl = NULL_TREE;
4687 : :
4688 : 4040628 : if (name)
4689 : : {
4690 : 2423460 : tree tname = get_identifier (name);
4691 : 2423460 : tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4692 : 2423460 : DECL_ARTIFICIAL (tdecl) = 1;
4693 : 2423460 : set_global_binding (tdecl);
4694 : 2423460 : decl = tdecl;
4695 : : }
4696 : :
4697 : 4040628 : if ((int) rid_index < (int) RID_MAX)
4698 : 2423460 : if (tree rname = ridpointers[(int) rid_index])
4699 : 1918095 : if (!decl || DECL_NAME (decl) != rname)
4700 : : {
4701 : 1415022 : tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4702 : 1415022 : DECL_ARTIFICIAL (rdecl) = 1;
4703 : 1415022 : set_global_binding (rdecl);
4704 : 1415022 : if (!decl)
4705 : : decl = rdecl;
4706 : : }
4707 : :
4708 : 3737409 : if (decl)
4709 : : {
4710 : 3535263 : if (!TYPE_NAME (type))
4711 : 2928825 : TYPE_NAME (type) = decl;
4712 : 3535263 : debug_hooks->type_decl (decl, 0);
4713 : : }
4714 : 4040628 : }
4715 : :
4716 : : /* Push a type into the namespace so that the back ends ignore it. */
4717 : :
4718 : : static void
4719 : 303219 : record_unknown_type (tree type, const char* name)
4720 : : {
4721 : 303219 : tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4722 : : TYPE_DECL, get_identifier (name), type));
4723 : : /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4724 : 303219 : DECL_IGNORED_P (decl) = 1;
4725 : 303219 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4726 : 303219 : TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4727 : 303219 : SET_TYPE_ALIGN (type, 1);
4728 : 303219 : TYPE_USER_ALIGN (type) = 0;
4729 : 303219 : SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4730 : 303219 : }
4731 : :
4732 : : /* Create all the predefined identifiers. */
4733 : :
4734 : : static void
4735 : 101073 : initialize_predefined_identifiers (void)
4736 : : {
4737 : 101073 : struct predefined_identifier
4738 : : {
4739 : : const char *name; /* Name. */
4740 : : tree *node; /* Node to store it in. */
4741 : : cp_identifier_kind kind; /* Kind of identifier. */
4742 : : };
4743 : :
4744 : : /* A table of identifiers to create at startup. */
4745 : 101073 : static const predefined_identifier predefined_identifiers[] = {
4746 : : {"C++", &lang_name_cplusplus, cik_normal},
4747 : : {"C", &lang_name_c, cik_normal},
4748 : : /* Some of these names have a trailing space so that it is
4749 : : impossible for them to conflict with names written by users. */
4750 : : {"__ct ", &ctor_identifier, cik_ctor},
4751 : : {"__ct_base ", &base_ctor_identifier, cik_ctor},
4752 : : {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4753 : : {"__dt ", &dtor_identifier, cik_dtor},
4754 : : {"__dt_base ", &base_dtor_identifier, cik_dtor},
4755 : : {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4756 : : {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4757 : : {"__conv_op ", &conv_op_identifier, cik_conv_op},
4758 : : {"__in_chrg", &in_charge_identifier, cik_normal},
4759 : : {"__as_base ", &as_base_identifier, cik_normal},
4760 : : {"this", &this_identifier, cik_normal},
4761 : : {"__delta", &delta_identifier, cik_normal},
4762 : : {"__pfn", &pfn_identifier, cik_normal},
4763 : : {"_vptr", &vptr_identifier, cik_normal},
4764 : : {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4765 : : {"::", &global_identifier, cik_normal},
4766 : : /* The demangler expects anonymous namespaces to be called
4767 : : something starting with '_GLOBAL__N_'. It no longer needs
4768 : : to be unique to the TU. */
4769 : : {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4770 : : {"auto", &auto_identifier, cik_normal},
4771 : : {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4772 : : {"initializer_list", &init_list_identifier, cik_normal},
4773 : : {"__for_range ", &for_range__identifier, cik_normal},
4774 : : {"__for_begin ", &for_begin__identifier, cik_normal},
4775 : : {"__for_end ", &for_end__identifier, cik_normal},
4776 : : {"__for_range", &for_range_identifier, cik_normal},
4777 : : {"__for_begin", &for_begin_identifier, cik_normal},
4778 : : {"__for_end", &for_end_identifier, cik_normal},
4779 : : {"abi_tag", &abi_tag_identifier, cik_normal},
4780 : : {"aligned", &aligned_identifier, cik_normal},
4781 : : {"begin", &begin_identifier, cik_normal},
4782 : : {"end", &end_identifier, cik_normal},
4783 : : {"get", &get__identifier, cik_normal},
4784 : : {"gnu", &gnu_identifier, cik_normal},
4785 : : {"tuple_element", &tuple_element_identifier, cik_normal},
4786 : : {"tuple_size", &tuple_size_identifier, cik_normal},
4787 : : {"type", &type_identifier, cik_normal},
4788 : : {"value", &value_identifier, cik_normal},
4789 : : {"_FUN", &fun_identifier, cik_normal},
4790 : : {"__closure", &closure_identifier, cik_normal},
4791 : : {"heap uninit", &heap_uninit_identifier, cik_normal},
4792 : : {"heap ", &heap_identifier, cik_normal},
4793 : : {"heap deleted", &heap_deleted_identifier, cik_normal},
4794 : : {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4795 : : {"heap []", &heap_vec_identifier, cik_normal},
4796 : : {"omp", &omp_identifier, cik_normal},
4797 : : {NULL, NULL, cik_normal}
4798 : : };
4799 : :
4800 : 4750431 : for (const predefined_identifier *pid = predefined_identifiers;
4801 : 4750431 : pid->name; ++pid)
4802 : : {
4803 : 4649358 : *pid->node = get_identifier (pid->name);
4804 : : /* Some of these identifiers already have a special kind. */
4805 : 4649358 : if (pid->kind != cik_normal)
4806 : 808584 : set_identifier_kind (*pid->node, pid->kind);
4807 : : }
4808 : 101073 : }
4809 : :
4810 : : /* Create the predefined scalar types of C,
4811 : : and some nodes representing standard constants (0, 1, (void *)0).
4812 : : Initialize the global binding level.
4813 : : Make definitions for built-in primitive functions. */
4814 : :
4815 : : void
4816 : 101073 : cxx_init_decl_processing (void)
4817 : : {
4818 : 101073 : tree void_ftype;
4819 : 101073 : tree void_ftype_ptr;
4820 : :
4821 : : /* Create all the identifiers we need. */
4822 : 101073 : initialize_predefined_identifiers ();
4823 : :
4824 : : /* Create the global variables. */
4825 : 101073 : push_to_top_level ();
4826 : :
4827 : 101073 : current_function_decl = NULL_TREE;
4828 : 101073 : current_binding_level = NULL;
4829 : : /* Enter the global namespace. */
4830 : 101073 : gcc_assert (global_namespace == NULL_TREE);
4831 : 101073 : global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4832 : : void_type_node);
4833 : 101073 : TREE_PUBLIC (global_namespace) = true;
4834 : 101073 : DECL_MODULE_EXPORT_P (global_namespace) = true;
4835 : 101073 : DECL_CONTEXT (global_namespace)
4836 : 101073 : = build_translation_unit_decl (get_identifier (main_input_filename));
4837 : : /* Remember whether we want the empty class passing ABI change warning
4838 : : in this TU. */
4839 : 101073 : TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4840 : 101724 : = warn_abi && abi_version_crosses (12);
4841 : 101073 : debug_hooks->register_main_translation_unit
4842 : 101073 : (DECL_CONTEXT (global_namespace));
4843 : 101073 : begin_scope (sk_namespace, global_namespace);
4844 : 101073 : current_namespace = global_namespace;
4845 : :
4846 : 101073 : if (flag_visibility_ms_compat)
4847 : 8 : default_visibility = VISIBILITY_HIDDEN;
4848 : :
4849 : : /* Initially, C. */
4850 : 101073 : current_lang_name = lang_name_c;
4851 : :
4852 : : /* Create the `std' namespace. */
4853 : 101073 : push_namespace (get_identifier ("std"));
4854 : 101073 : std_node = current_namespace;
4855 : 101073 : pop_namespace ();
4856 : :
4857 : 101073 : flag_noexcept_type = (cxx_dialect >= cxx17);
4858 : :
4859 : 101073 : c_common_nodes_and_builtins ();
4860 : :
4861 : 101073 : tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4862 : 101073 : tree decl
4863 : 101073 : = add_builtin_function ("__builtin_is_constant_evaluated",
4864 : : bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4865 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4866 : 101073 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4867 : :
4868 : : /* The concrete return type of __builtin_source_location is
4869 : : const std::source_location::__impl*, but we can't form the type
4870 : : at this point. So we initially declare it with an auto return
4871 : : type which we then "deduce" from require_deduced_type upon first use. */
4872 : 101073 : tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE);
4873 : 101073 : decl = add_builtin_function ("__builtin_source_location",
4874 : : auto_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4875 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4876 : 101073 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4877 : :
4878 : 101073 : tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4879 : : NULL_TREE);
4880 : 101073 : decl
4881 : 101073 : = add_builtin_function ("__builtin_is_corresponding_member",
4882 : : bool_vaftype,
4883 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4884 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4885 : 101073 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4886 : :
4887 : 101073 : decl
4888 : 101073 : = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4889 : : bool_vaftype,
4890 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4891 : : BUILT_IN_FRONTEND, NULL, NULL_TREE);
4892 : 101073 : set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4893 : :
4894 : 101073 : integer_two_node = build_int_cst (NULL_TREE, 2);
4895 : :
4896 : : /* Guess at the initial static decls size. */
4897 : 101073 : vec_alloc (static_decls, 500);
4898 : :
4899 : : /* ... and keyed classes. */
4900 : 101073 : vec_alloc (keyed_classes, 100);
4901 : :
4902 : 101073 : record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4903 : 101073 : truthvalue_type_node = boolean_type_node;
4904 : 101073 : truthvalue_false_node = boolean_false_node;
4905 : 101073 : truthvalue_true_node = boolean_true_node;
4906 : :
4907 : 101073 : empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4908 : 101073 : noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4909 : 101073 : noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4910 : 101073 : noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4911 : : NULL_TREE);
4912 : :
4913 : : #if 0
4914 : : record_builtin_type (RID_MAX, NULL, string_type_node);
4915 : : #endif
4916 : :
4917 : 101073 : delta_type_node = ptrdiff_type_node;
4918 : 101073 : vtable_index_type = ptrdiff_type_node;
4919 : :
4920 : 101073 : vtt_parm_type = build_pointer_type (const_ptr_type_node);
4921 : 101073 : void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4922 : 101073 : void_ftype_ptr = build_function_type_list (void_type_node,
4923 : : ptr_type_node, NULL_TREE);
4924 : 101073 : void_ftype_ptr
4925 : 101073 : = build_exception_variant (void_ftype_ptr, empty_except_spec);
4926 : :
4927 : : /* Create the conversion operator marker. This operator's DECL_NAME
4928 : : is in the identifier table, so we can use identifier equality to
4929 : : find it. */
4930 : 101073 : conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4931 : : void_ftype);
4932 : :
4933 : : /* C++ extensions */
4934 : :
4935 : 101073 : unknown_type_node = make_node (LANG_TYPE);
4936 : 101073 : record_unknown_type (unknown_type_node, "unknown type");
4937 : :
4938 : : /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4939 : 101073 : TREE_TYPE (unknown_type_node) = unknown_type_node;
4940 : :
4941 : : /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4942 : : result. */
4943 : 101073 : TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4944 : 101073 : TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4945 : :
4946 : 101073 : init_list_type_node = make_node (LANG_TYPE);
4947 : 101073 : record_unknown_type (init_list_type_node, "init list");
4948 : :
4949 : : /* Used when parsing to distinguish parameter-lists () and (void). */
4950 : 101073 : explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node);
4951 : :
4952 : 101073 : {
4953 : : /* Make sure we get a unique function type, so we can give
4954 : : its pointer type a name. (This wins for gdb.) */
4955 : 101073 : tree vfunc_type = make_node (FUNCTION_TYPE);
4956 : 101073 : TREE_TYPE (vfunc_type) = integer_type_node;
4957 : 101073 : TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4958 : 101073 : layout_type (vfunc_type);
4959 : :
4960 : 101073 : vtable_entry_type = build_pointer_type (vfunc_type);
4961 : : }
4962 : 101073 : record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4963 : :
4964 : 101073 : vtbl_type_node
4965 : 101073 : = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4966 : 101073 : layout_type (vtbl_type_node);
4967 : 101073 : vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4968 : 101073 : record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4969 : 101073 : vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4970 : 101073 : layout_type (vtbl_ptr_type_node);
4971 : 101073 : record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4972 : :
4973 : 101073 : push_namespace (get_identifier ("__cxxabiv1"));
4974 : 101073 : abi_node = current_namespace;
4975 : 101073 : pop_namespace ();
4976 : :
4977 : 101073 : any_targ_node = make_node (LANG_TYPE);
4978 : 101073 : record_unknown_type (any_targ_node, "any type");
4979 : :
4980 : : /* Now, C++. */
4981 : 101073 : current_lang_name = lang_name_cplusplus;
4982 : :
4983 : 101073 : if (aligned_new_threshold > 1
4984 : 101073 : && !pow2p_hwi (aligned_new_threshold))
4985 : : {
4986 : 0 : error ("%<-faligned-new=%d%> is not a power of two",
4987 : : aligned_new_threshold);
4988 : 0 : aligned_new_threshold = 1;
4989 : : }
4990 : 101073 : if (aligned_new_threshold == -1)
4991 : 134040 : aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4992 : 101073 : if (aligned_new_threshold == 1)
4993 : 68091 : aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4994 : :
4995 : 101073 : {
4996 : 101073 : tree newattrs, extvisattr;
4997 : 101073 : tree newtype, deltype;
4998 : 101073 : tree ptr_ftype_sizetype;
4999 : 101073 : tree new_eh_spec;
5000 : :
5001 : 101073 : ptr_ftype_sizetype
5002 : 101073 : = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
5003 : 101073 : if (cxx_dialect == cxx98)
5004 : : {
5005 : 13505 : tree bad_alloc_id;
5006 : 13505 : tree bad_alloc_type_node;
5007 : 13505 : tree bad_alloc_decl;
5008 : :
5009 : 13505 : push_nested_namespace (std_node);
5010 : 13505 : bad_alloc_id = get_identifier ("bad_alloc");
5011 : 13505 : bad_alloc_type_node = make_class_type (RECORD_TYPE);
5012 : 13505 : TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
5013 : 13505 : bad_alloc_decl
5014 : 13505 : = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
5015 : 13505 : DECL_CONTEXT (bad_alloc_decl) = current_namespace;
5016 : 13505 : pop_nested_namespace (std_node);
5017 : :
5018 : 13505 : new_eh_spec
5019 : 13505 : = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
5020 : : }
5021 : : else
5022 : 87568 : new_eh_spec = noexcept_false_spec;
5023 : :
5024 : : /* Ensure attribs.cc is initialized. */
5025 : 101073 : init_attributes ();
5026 : :
5027 : 101073 : extvisattr = build_tree_list (get_identifier ("externally_visible"),
5028 : : NULL_TREE);
5029 : 101073 : newattrs = tree_cons (get_identifier ("alloc_size"),
5030 : : build_tree_list (NULL_TREE, integer_one_node),
5031 : : extvisattr);
5032 : 101073 : newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
5033 : 101073 : newtype = build_exception_variant (newtype, new_eh_spec);
5034 : 101073 : deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
5035 : 101073 : deltype = build_exception_variant (deltype, empty_except_spec);
5036 : 101073 : tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5037 : 101073 : DECL_IS_MALLOC (opnew) = 1;
5038 : 101073 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5039 : 101073 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5040 : 101073 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5041 : 101073 : DECL_IS_MALLOC (opnew) = 1;
5042 : 101073 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5043 : 101073 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5044 : 101073 : tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5045 : 101073 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5046 : 101073 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5047 : 101073 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5048 : 101073 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5049 : 101073 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5050 : 101073 : if (flag_sized_deallocation)
5051 : : {
5052 : : /* Also push the sized deallocation variants:
5053 : : void operator delete(void*, std::size_t) throw();
5054 : : void operator delete[](void*, std::size_t) throw(); */
5055 : 86711 : tree void_ftype_ptr_size
5056 : 86711 : = build_function_type_list (void_type_node, ptr_type_node,
5057 : : size_type_node, NULL_TREE);
5058 : 86711 : deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
5059 : : extvisattr);
5060 : 86711 : deltype = build_exception_variant (deltype, empty_except_spec);
5061 : 86711 : opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5062 : 86711 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5063 : 86711 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5064 : 86711 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5065 : 86711 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5066 : 86711 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5067 : : }
5068 : :
5069 : 101073 : if (aligned_new_threshold)
5070 : : {
5071 : 68093 : push_nested_namespace (std_node);
5072 : 68093 : tree align_id = get_identifier ("align_val_t");
5073 : 68093 : align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
5074 : : NULL_TREE, /*scoped*/true, NULL);
5075 : 68093 : pop_nested_namespace (std_node);
5076 : :
5077 : : /* operator new (size_t, align_val_t); */
5078 : 68093 : newtype = build_function_type_list (ptr_type_node, size_type_node,
5079 : : align_type_node, NULL_TREE);
5080 : 68093 : newtype = cp_build_type_attribute_variant (newtype, newattrs);
5081 : 68093 : newtype = build_exception_variant (newtype, new_eh_spec);
5082 : 68093 : opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
5083 : 68093 : DECL_IS_MALLOC (opnew) = 1;
5084 : 68093 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5085 : 68093 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5086 : 68093 : opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
5087 : 68093 : DECL_IS_MALLOC (opnew) = 1;
5088 : 68093 : DECL_SET_IS_OPERATOR_NEW (opnew, true);
5089 : 68093 : DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
5090 : :
5091 : : /* operator delete (void *, align_val_t); */
5092 : 68093 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5093 : : align_type_node, NULL_TREE);
5094 : 68093 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5095 : 68093 : deltype = build_exception_variant (deltype, empty_except_spec);
5096 : 68093 : opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5097 : 68093 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5098 : 68093 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5099 : 68093 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5100 : 68093 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5101 : 68093 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5102 : :
5103 : 68093 : if (flag_sized_deallocation)
5104 : : {
5105 : : /* operator delete (void *, size_t, align_val_t); */
5106 : 68090 : deltype = build_function_type_list (void_type_node, ptr_type_node,
5107 : : size_type_node, align_type_node,
5108 : : NULL_TREE);
5109 : 68090 : deltype = cp_build_type_attribute_variant (deltype, extvisattr);
5110 : 68090 : deltype = build_exception_variant (deltype, empty_except_spec);
5111 : 68090 : opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
5112 : 68090 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5113 : 68090 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5114 : 68090 : opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
5115 : 68090 : DECL_SET_IS_OPERATOR_DELETE (opdel, true);
5116 : 68090 : DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
5117 : : }
5118 : : }
5119 : :
5120 : : /* C++-specific nullptr initialization. */
5121 : 101073 : if (abi_version_at_least (9))
5122 : 100883 : SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
5123 : 101073 : record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
5124 : : }
5125 : :
5126 : 101073 : if (! supports_one_only ())
5127 : 0 : flag_weak = 0;
5128 : :
5129 : 101073 : abort_fndecl
5130 : 101073 : = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
5131 : : ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
5132 : 101073 : if (flag_weak)
5133 : : /* If no definition is available, resolve references to NULL. */
5134 : 101036 : declare_weak (abort_fndecl);
5135 : :
5136 : : /* Perform other language dependent initializations. */
5137 : 101073 : init_class_processing ();
5138 : 101073 : init_rtti_processing ();
5139 : 101073 : init_template_processing ();
5140 : :
5141 : 101073 : if (flag_exceptions)
5142 : 99978 : init_exception_processing ();
5143 : :
5144 : 101073 : if (modules_p ())
5145 : 3229 : init_modules (parse_in);
5146 : :
5147 : 101073 : make_fname_decl = cp_make_fname_decl;
5148 : 101073 : start_fname_decls ();
5149 : :
5150 : : /* Show we use EH for cleanups. */
5151 : 101073 : if (flag_exceptions)
5152 : 99978 : using_eh_for_cleanups ();
5153 : :
5154 : : /* Check that the hardware interference sizes are at least
5155 : : alignof(max_align_t), as required by the standard. */
5156 : 101073 : const int max_align = max_align_t_align () / BITS_PER_UNIT;
5157 : 101073 : if (OPTION_SET_P (param_destruct_interfere_size))
5158 : : {
5159 : 0 : if (param_destruct_interfere_size < max_align)
5160 : 0 : error ("%<--param destructive-interference-size=%d%> is less than "
5161 : : "%d", param_destruct_interfere_size, max_align);
5162 : 0 : else if (param_destruct_interfere_size < param_l1_cache_line_size)
5163 : 0 : warning (OPT_Winterference_size,
5164 : : "%<--param destructive-interference-size=%d%> "
5165 : : "is less than %<--param l1-cache-line-size=%d%>",
5166 : : param_destruct_interfere_size, param_l1_cache_line_size);
5167 : : }
5168 : 101073 : else if (param_destruct_interfere_size)
5169 : : /* Assume the internal value is OK. */;
5170 : 0 : else if (param_l1_cache_line_size >= max_align)
5171 : 0 : param_destruct_interfere_size = param_l1_cache_line_size;
5172 : : /* else leave it unset. */
5173 : :
5174 : 101073 : if (OPTION_SET_P (param_construct_interfere_size))
5175 : : {
5176 : 0 : if (param_construct_interfere_size < max_align)
5177 : 0 : error ("%<--param constructive-interference-size=%d%> is less than "
5178 : : "%d", param_construct_interfere_size, max_align);
5179 : 0 : else if (param_construct_interfere_size > param_l1_cache_line_size
5180 : 0 : && param_l1_cache_line_size >= max_align)
5181 : 0 : warning (OPT_Winterference_size,
5182 : : "%<--param constructive-interference-size=%d%> "
5183 : : "is greater than %<--param l1-cache-line-size=%d%>",
5184 : : param_construct_interfere_size, param_l1_cache_line_size);
5185 : : }
5186 : 101073 : else if (param_construct_interfere_size)
5187 : : /* Assume the internal value is OK. */;
5188 : 0 : else if (param_l1_cache_line_size >= max_align)
5189 : 0 : param_construct_interfere_size = param_l1_cache_line_size;
5190 : 101073 : }
5191 : :
5192 : : /* Enter an abi node in global-module context. returns a cookie to
5193 : : give to pop_abi_namespace. */
5194 : :
5195 : : unsigned
5196 : 57793 : push_abi_namespace (tree node)
5197 : : {
5198 : 57793 : push_nested_namespace (node);
5199 : 57793 : push_visibility ("default", 2);
5200 : 57793 : unsigned flags = module_kind;
5201 : 57793 : module_kind = 0;
5202 : 57793 : return flags;
5203 : : }
5204 : :
5205 : : /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
5206 : : you. */
5207 : :
5208 : : void
5209 : 57793 : pop_abi_namespace (unsigned flags, tree node)
5210 : : {
5211 : 57793 : module_kind = flags;
5212 : 57793 : pop_visibility (2);
5213 : 57793 : pop_nested_namespace (node);
5214 : 57793 : }
5215 : :
5216 : : /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
5217 : : the decl, LOC is the location to give the decl, NAME is the
5218 : : initialization string and TYPE_DEP indicates whether NAME depended
5219 : : on the type of the function. We make use of that to detect
5220 : : __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
5221 : : at the point of first use, so we mustn't push the decl now. */
5222 : :
5223 : : static tree
5224 : 56538 : cp_make_fname_decl (location_t loc, tree id, int type_dep)
5225 : : {
5226 : 56538 : tree domain = NULL_TREE;
5227 : 56538 : tree init = NULL_TREE;
5228 : :
5229 : 56538 : if (!(type_dep && current_function_decl && in_template_context))
5230 : : {
5231 : 19039 : const char *name = NULL;
5232 : 19039 : bool release_name = false;
5233 : :
5234 : 19039 : if (current_function_decl == NULL_TREE)
5235 : : name = "top level";
5236 : 19019 : else if (type_dep == 0)
5237 : : {
5238 : : /* __FUNCTION__ */
5239 : 465 : name = fname_as_string (type_dep);
5240 : 465 : release_name = true;
5241 : : }
5242 : : else
5243 : : {
5244 : : /* __PRETTY_FUNCTION__ */
5245 : 18554 : gcc_checking_assert (type_dep == 1);
5246 : 18554 : name = cxx_printable_name (current_function_decl, 2);
5247 : : }
5248 : :
5249 : 19039 : size_t length = strlen (name);
5250 : 19039 : domain = build_index_type (size_int (length));
5251 : 19039 : init = build_string (length + 1, name);
5252 : 19039 : if (release_name)
5253 : 465 : free (const_cast<char *> (name));
5254 : : }
5255 : :
5256 : 56538 : tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
5257 : 56538 : type = build_cplus_array_type (type, domain);
5258 : :
5259 : 56538 : if (init)
5260 : 19039 : TREE_TYPE (init) = type;
5261 : : else
5262 : 37499 : init = error_mark_node;
5263 : :
5264 : 56538 : tree decl = build_decl (loc, VAR_DECL, id, type);
5265 : :
5266 : 56538 : TREE_READONLY (decl) = 1;
5267 : 56538 : DECL_ARTIFICIAL (decl) = 1;
5268 : 56538 : DECL_DECLARED_CONSTEXPR_P (decl) = 1;
5269 : 56538 : TREE_STATIC (decl) = 1;
5270 : :
5271 : 56538 : TREE_USED (decl) = 1;
5272 : :
5273 : 56538 : SET_DECL_VALUE_EXPR (decl, init);
5274 : 56538 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
5275 : : /* For decl_constant_var_p. */
5276 : 56538 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5277 : :
5278 : 56538 : if (current_function_decl)
5279 : : {
5280 : 56518 : DECL_CONTEXT (decl) = current_function_decl;
5281 : 56518 : decl = pushdecl_outermost_localscope (decl);
5282 : 56518 : if (decl != error_mark_node)
5283 : 56514 : add_decl_expr (decl);
5284 : : }
5285 : : else
5286 : : {
5287 : 20 : DECL_THIS_STATIC (decl) = true;
5288 : 20 : decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
5289 : : }
5290 : :
5291 : 56538 : return decl;
5292 : : }
5293 : :
5294 : : /* Install DECL as a builtin function at current global scope. Return
5295 : : the new decl (if we found an existing version). Also installs it
5296 : : into ::std, if it's not '_*'. */
5297 : :
5298 : : tree
5299 : 220412053 : cxx_builtin_function (tree decl)
5300 : : {
5301 : 220412053 : retrofit_lang_decl (decl);
5302 : :
5303 : 220412053 : DECL_ARTIFICIAL (decl) = 1;
5304 : 220412053 : SET_DECL_LANGUAGE (decl, lang_c);
5305 : : /* Runtime library routines are, by definition, available in an
5306 : : external shared object. */
5307 : 220412053 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5308 : 220412053 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
5309 : :
5310 : 220412053 : tree id = DECL_NAME (decl);
5311 : 220412053 : const char *name = IDENTIFIER_POINTER (id);
5312 : 220412053 : bool hiding = false;
5313 : 220412053 : if (name[0] != '_' || name[1] != '_')
5314 : : /* In the user's namespace, it must be declared before use. */
5315 : : hiding = true;
5316 : 183757679 : else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
5317 : 183757679 : && !startswith (name + 2, "builtin_")
5318 : 207367170 : && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
5319 : : "_chk", strlen ("_chk") + 1))
5320 : : /* Treat __*_chk fortification functions as anticipated as well,
5321 : : unless they are __builtin_*_chk. */
5322 : : hiding = true;
5323 : :
5324 : : /* All builtins that don't begin with an '_' should additionally
5325 : : go in the 'std' namespace. */
5326 : 220412053 : if (name[0] != '_')
5327 : : {
5328 : 36514706 : tree std_decl = copy_decl (decl);
5329 : :
5330 : 36514706 : push_nested_namespace (std_node);
5331 : 36514706 : DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5332 : 36514706 : pushdecl (std_decl, hiding);
5333 : 36514706 : pop_nested_namespace (std_node);
5334 : : }
5335 : :
5336 : 220412053 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5337 : 220412053 : decl = pushdecl (decl, hiding);
5338 : :
5339 : 220412053 : return decl;
5340 : : }
5341 : :
5342 : : /* Like cxx_builtin_function, but guarantee the function is added to the global
5343 : : scope. This is to allow function specific options to add new machine
5344 : : dependent builtins when the target ISA changes via attribute((target(...)))
5345 : : which saves space on program startup if the program does not use non-generic
5346 : : ISAs. */
5347 : :
5348 : : tree
5349 : 674967 : cxx_builtin_function_ext_scope (tree decl)
5350 : : {
5351 : 674967 : push_nested_namespace (global_namespace);
5352 : 674967 : decl = cxx_builtin_function (decl);
5353 : 674967 : pop_nested_namespace (global_namespace);
5354 : :
5355 : 674967 : return decl;
5356 : : }
5357 : :
5358 : : /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5359 : :
5360 : : tree
5361 : 0 : cxx_simulate_builtin_function_decl (tree decl)
5362 : : {
5363 : 0 : retrofit_lang_decl (decl);
5364 : :
5365 : 0 : DECL_ARTIFICIAL (decl) = 1;
5366 : 0 : SET_DECL_LANGUAGE (decl, lang_cplusplus);
5367 : 0 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5368 : 0 : return pushdecl (decl);
5369 : : }
5370 : :
5371 : : /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5372 : : function. Not called directly. */
5373 : :
5374 : : static tree
5375 : 1567114 : build_library_fn (tree name, enum tree_code operator_code, tree type,
5376 : : int ecf_flags)
5377 : : {
5378 : 1567114 : tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5379 : 1567114 : DECL_EXTERNAL (fn) = 1;
5380 : 1567114 : TREE_PUBLIC (fn) = 1;
5381 : 1567114 : DECL_ARTIFICIAL (fn) = 1;
5382 : 1567114 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5383 : 1567114 : = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5384 : 1567114 : SET_DECL_LANGUAGE (fn, lang_c);
5385 : : /* Runtime library routines are, by definition, available in an
5386 : : external shared object. */
5387 : 1567114 : DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5388 : 1567114 : DECL_VISIBILITY_SPECIFIED (fn) = 1;
5389 : 1567114 : set_call_expr_flags (fn, ecf_flags);
5390 : 1567114 : return fn;
5391 : : }
5392 : :
5393 : : /* Returns the _DECL for a library function with C++ linkage. */
5394 : :
5395 : : static tree
5396 : 1086317 : build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5397 : : int ecf_flags)
5398 : : {
5399 : 1086317 : tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5400 : 1086317 : DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5401 : 1086317 : SET_DECL_LANGUAGE (fn, lang_cplusplus);
5402 : 1086317 : return fn;
5403 : : }
5404 : :
5405 : : /* Like build_library_fn, but takes a C string instead of an
5406 : : IDENTIFIER_NODE. */
5407 : :
5408 : : tree
5409 : 106536 : build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5410 : : {
5411 : 106536 : return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5412 : : }
5413 : :
5414 : : /* Like build_cp_library_fn, but takes a C string instead of an
5415 : : IDENTIFIER_NODE. */
5416 : :
5417 : : tree
5418 : 100051 : build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5419 : : {
5420 : 100051 : return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5421 : 100051 : ecf_flags);
5422 : : }
5423 : :
5424 : : /* Like build_library_fn, but also pushes the function so that we will
5425 : : be able to find it via get_global_binding. Also, the function
5426 : : may throw exceptions listed in RAISES. */
5427 : :
5428 : : tree
5429 : 374261 : push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5430 : : {
5431 : 374261 : if (raises)
5432 : 39140 : type = build_exception_variant (type, raises);
5433 : :
5434 : 374261 : tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5435 : 374261 : return pushdecl_top_level (fn);
5436 : : }
5437 : :
5438 : : /* Like build_cp_library_fn, but also pushes the function so that it
5439 : : will be found by normal lookup. */
5440 : :
5441 : : static tree
5442 : 986266 : push_cp_library_fn (enum tree_code operator_code, tree type,
5443 : : int ecf_flags)
5444 : : {
5445 : 986266 : tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5446 : : operator_code, type, ecf_flags);
5447 : 986266 : pushdecl (fn);
5448 : 986266 : if (flag_tm)
5449 : 4084 : apply_tm_attr (fn, get_identifier ("transaction_safe"));
5450 : 986266 : return fn;
5451 : : }
5452 : :
5453 : : /* Like push_library_fn, but also note that this function throws
5454 : : and does not return. Used for __throw_foo and the like. */
5455 : :
5456 : : tree
5457 : 110558 : push_throw_library_fn (tree name, tree type)
5458 : : {
5459 : 110558 : tree fn = push_library_fn (name, type, NULL_TREE,
5460 : : ECF_NORETURN | ECF_XTHROW | ECF_COLD);
5461 : 110558 : return fn;
5462 : : }
5463 : :
5464 : : /* When we call finish_struct for an anonymous union, we create
5465 : : default copy constructors and such. But, an anonymous union
5466 : : shouldn't have such things; this function undoes the damage to the
5467 : : anonymous union type T.
5468 : :
5469 : : (The reason that we create the synthesized methods is that we don't
5470 : : distinguish `union { int i; }' from `typedef union { int i; } U'.
5471 : : The first is an anonymous union; the second is just an ordinary
5472 : : union type.) */
5473 : :
5474 : : void
5475 : 88473 : fixup_anonymous_aggr (tree t)
5476 : : {
5477 : : /* Wipe out memory of synthesized methods. */
5478 : 88473 : TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5479 : 88473 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5480 : 88473 : TYPE_HAS_COPY_CTOR (t) = 0;
5481 : 88473 : TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5482 : 88473 : TYPE_HAS_COPY_ASSIGN (t) = 0;
5483 : 88473 : TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5484 : :
5485 : : /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5486 : : invalid members. */
5487 : 458998 : for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5488 : : {
5489 : 370525 : if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5490 : 0 : *prev_p = DECL_CHAIN (probe);
5491 : : else
5492 : 370525 : prev_p = &DECL_CHAIN (probe);
5493 : :
5494 : 370525 : if (DECL_ARTIFICIAL (probe)
5495 : 370525 : && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5496 : 40158 : || TYPE_ANON_P (TREE_TYPE (probe))))
5497 : 133037 : continue;
5498 : :
5499 : 237488 : if (TREE_CODE (probe) != FIELD_DECL
5500 : 237488 : || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5501 : : {
5502 : : /* We already complained about static data members in
5503 : : finish_static_data_member_decl. */
5504 : 87 : if (!VAR_P (probe))
5505 : : {
5506 : 67 : auto_diagnostic_group d;
5507 : 67 : if (permerror (DECL_SOURCE_LOCATION (probe),
5508 : 67 : TREE_CODE (t) == UNION_TYPE
5509 : : ? "%q#D invalid; an anonymous union may "
5510 : : "only have public non-static data members"
5511 : : : "%q#D invalid; an anonymous struct may "
5512 : : "only have public non-static data members", probe))
5513 : : {
5514 : 67 : static bool hint;
5515 : 67 : if (flag_permissive && !hint)
5516 : : {
5517 : 8 : hint = true;
5518 : 8 : inform (DECL_SOURCE_LOCATION (probe),
5519 : : "this flexibility is deprecated and will be "
5520 : : "removed");
5521 : : }
5522 : : }
5523 : 67 : }
5524 : : }
5525 : : }
5526 : :
5527 : : /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5528 : 88473 : vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5529 : 88473 : unsigned store = 0;
5530 : 191958 : for (tree elt : vec)
5531 : 103485 : if (!is_overloaded_fn (elt))
5532 : 103458 : (*vec)[store++] = elt;
5533 : 88473 : vec_safe_truncate (vec, store);
5534 : :
5535 : : /* Wipe RTTI info. */
5536 : 88473 : CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5537 : :
5538 : : /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5539 : : assignment operators (because they cannot have these methods themselves).
5540 : : For anonymous unions this is already checked because they are not allowed
5541 : : in any union, otherwise we have to check it. */
5542 : 88473 : if (TREE_CODE (t) != UNION_TYPE)
5543 : : {
5544 : 19037 : tree field, type;
5545 : :
5546 : 19037 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5547 : : {
5548 : 12 : error_at (location_of (t), "anonymous struct with base classes");
5549 : : /* Avoid ICE after error on anon-struct9.C. */
5550 : 12 : TYPE_NEEDS_CONSTRUCTING (t) = false;
5551 : : }
5552 : :
5553 : 86046 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5554 : 67009 : if (TREE_CODE (field) == FIELD_DECL)
5555 : : {
5556 : 47853 : type = TREE_TYPE (field);
5557 : 47853 : if (CLASS_TYPE_P (type))
5558 : : {
5559 : 61 : if (TYPE_NEEDS_CONSTRUCTING (type))
5560 : 4 : error ("member %q+#D with constructor not allowed "
5561 : : "in anonymous aggregate", field);
5562 : 61 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5563 : 0 : error ("member %q+#D with destructor not allowed "
5564 : : "in anonymous aggregate", field);
5565 : 61 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5566 : 0 : error ("member %q+#D with copy assignment operator "
5567 : : "not allowed in anonymous aggregate", field);
5568 : : }
5569 : : }
5570 : : }
5571 : 88473 : }
5572 : :
5573 : : /* Warn for an attribute located at LOCATION that appertains to the
5574 : : class type CLASS_TYPE that has not been properly placed after its
5575 : : class-key, in it class-specifier. */
5576 : :
5577 : : void
5578 : 31 : warn_misplaced_attr_for_class_type (location_t location,
5579 : : tree class_type)
5580 : : {
5581 : 31 : gcc_assert (OVERLOAD_TYPE_P (class_type));
5582 : :
5583 : 31 : auto_diagnostic_group d;
5584 : 31 : if (warning_at (location, OPT_Wattributes,
5585 : : "attribute ignored in declaration "
5586 : : "of %q#T", class_type))
5587 : 31 : inform (location,
5588 : : "attribute for %q#T must follow the %qs keyword",
5589 : : class_type, class_key_or_enum_as_string (class_type));
5590 : 31 : }
5591 : :
5592 : : /* Returns the cv-qualifiers that apply to the type specified
5593 : : by the DECLSPECS. */
5594 : :
5595 : : static int
5596 : 837599932 : get_type_quals (const cp_decl_specifier_seq *declspecs)
5597 : : {
5598 : 837599932 : int type_quals = TYPE_UNQUALIFIED;
5599 : :
5600 : 837599932 : if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5601 : 92124993 : type_quals |= TYPE_QUAL_CONST;
5602 : 837599932 : if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5603 : 1455276 : type_quals |= TYPE_QUAL_VOLATILE;
5604 : 837599932 : if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5605 : 39 : type_quals |= TYPE_QUAL_RESTRICT;
5606 : :
5607 : 837599932 : return type_quals;
5608 : : }
5609 : :
5610 : : /* Make sure that a declaration with no declarator is well-formed, i.e.
5611 : : just declares a tagged type or anonymous union.
5612 : :
5613 : : Returns the type declared; or NULL_TREE if none. */
5614 : :
5615 : : tree
5616 : 30115858 : check_tag_decl (cp_decl_specifier_seq *declspecs,
5617 : : bool explicit_type_instantiation_p)
5618 : : {
5619 : 30115858 : int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5620 : 30115858 : int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5621 : : /* If a class, struct, or enum type is declared by the DECLSPECS
5622 : : (i.e, if a class-specifier, enum-specifier, or non-typename
5623 : : elaborated-type-specifier appears in the DECLSPECS),
5624 : : DECLARED_TYPE is set to the corresponding type. */
5625 : 30115858 : tree declared_type = NULL_TREE;
5626 : 30115858 : bool error_p = false;
5627 : :
5628 : 30115858 : if (declspecs->multiple_types_p)
5629 : 31 : error_at (smallest_type_location (declspecs),
5630 : : "multiple types in one declaration");
5631 : 30115827 : else if (declspecs->redefined_builtin_type)
5632 : : {
5633 : 26 : location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5634 : 26 : if (!in_system_header_at (loc))
5635 : 10 : permerror (loc, "redeclaration of C++ built-in type %qT",
5636 : : declspecs->redefined_builtin_type);
5637 : 26 : return NULL_TREE;
5638 : : }
5639 : :
5640 : 30115832 : if (declspecs->type
5641 : 30115815 : && TYPE_P (declspecs->type)
5642 : 60102682 : && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5643 : 29985689 : && MAYBE_CLASS_TYPE_P (declspecs->type))
5644 : 1531412 : || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5645 : : declared_type = declspecs->type;
5646 : 130190 : else if (declspecs->type == error_mark_node)
5647 : 853 : error_p = true;
5648 : :
5649 : 30115832 : if (type_uses_auto (declared_type))
5650 : : {
5651 : 9 : error_at (declspecs->locations[ds_type_spec],
5652 : : "%<auto%> can only be specified for variables "
5653 : : "or function declarations");
5654 : 9 : return error_mark_node;
5655 : : }
5656 : :
5657 : 30115823 : if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5658 : : declared_type = NULL_TREE;
5659 : :
5660 : 30115823 : if (!declared_type && !saw_friend && !error_p)
5661 : 107 : permerror (input_location, "declaration does not declare anything");
5662 : : /* Check for an anonymous union. */
5663 : 29985601 : else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5664 : 87114991 : && TYPE_UNNAMED_P (declared_type))
5665 : : {
5666 : : /* 7/3 In a simple-declaration, the optional init-declarator-list
5667 : : can be omitted only when declaring a class (clause 9) or
5668 : : enumeration (7.2), that is, when the decl-specifier-seq contains
5669 : : either a class-specifier, an elaborated-type-specifier with
5670 : : a class-key (9.1), or an enum-specifier. In these cases and
5671 : : whenever a class-specifier or enum-specifier is present in the
5672 : : decl-specifier-seq, the identifiers in these specifiers are among
5673 : : the names being declared by the declaration (as class-name,
5674 : : enum-names, or enumerators, depending on the syntax). In such
5675 : : cases, and except for the declaration of an unnamed bit-field (9.6),
5676 : : the decl-specifier-seq shall introduce one or more names into the
5677 : : program, or shall redeclare a name introduced by a previous
5678 : : declaration. [Example:
5679 : : enum { }; // ill-formed
5680 : : typedef class { }; // ill-formed
5681 : : --end example] */
5682 : 88481 : if (saw_typedef)
5683 : : {
5684 : 8 : error_at (declspecs->locations[ds_typedef],
5685 : : "missing type-name in typedef-declaration");
5686 : 8 : return NULL_TREE;
5687 : : }
5688 : 88473 : /* Anonymous unions are objects, so they can have specifiers. */;
5689 : 88473 : SET_ANON_AGGR_TYPE_P (declared_type);
5690 : :
5691 : 88473 : if (TREE_CODE (declared_type) != UNION_TYPE)
5692 : 19037 : pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5693 : : OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5694 : : }
5695 : :
5696 : : else
5697 : : {
5698 : 30027235 : if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5699 : 11 : error_at (declspecs->locations[ds_inline],
5700 : : "%<inline%> can only be specified for functions");
5701 : 30027224 : else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5702 : 3 : error_at (declspecs->locations[ds_virtual],
5703 : : "%<virtual%> can only be specified for functions");
5704 : 30027221 : else if (saw_friend
5705 : 30027221 : && (!current_class_type
5706 : 1401071 : || current_scope () != current_class_type))
5707 : 0 : error_at (declspecs->locations[ds_friend],
5708 : : "%<friend%> can only be specified inside a class");
5709 : 30027221 : else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5710 : 3 : error_at (declspecs->locations[ds_explicit],
5711 : : "%<explicit%> can only be specified for constructors");
5712 : 30027218 : else if (declspecs->storage_class)
5713 : 3 : error_at (declspecs->locations[ds_storage_class],
5714 : : "a storage class can only be specified for objects "
5715 : : "and functions");
5716 : 30027215 : else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5717 : 11 : error_at (declspecs->locations[ds_const],
5718 : : "%<const%> can only be specified for objects and "
5719 : : "functions");
5720 : 30027204 : else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5721 : 3 : error_at (declspecs->locations[ds_volatile],
5722 : : "%<volatile%> can only be specified for objects and "
5723 : : "functions");
5724 : 30027201 : else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5725 : 3 : error_at (declspecs->locations[ds_restrict],
5726 : : "%<__restrict%> can only be specified for objects and "
5727 : : "functions");
5728 : 30027198 : else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5729 : 3 : error_at (declspecs->locations[ds_thread],
5730 : : "%<__thread%> can only be specified for objects "
5731 : : "and functions");
5732 : 30027195 : else if (saw_typedef)
5733 : 31 : warning_at (declspecs->locations[ds_typedef], 0,
5734 : : "%<typedef%> was ignored in this declaration");
5735 : 30027164 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5736 : 9 : error_at (declspecs->locations[ds_constexpr],
5737 : : "%qs cannot be used for type declarations", "constexpr");
5738 : 30027155 : else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5739 : 1 : error_at (declspecs->locations[ds_constinit],
5740 : : "%qs cannot be used for type declarations", "constinit");
5741 : 30027154 : else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5742 : 1 : error_at (declspecs->locations[ds_consteval],
5743 : : "%qs cannot be used for type declarations", "consteval");
5744 : : }
5745 : :
5746 : 30115815 : if (declspecs->attributes && warn_attributes && declared_type)
5747 : : {
5748 : 32 : location_t loc;
5749 : 25 : if (!CLASS_TYPE_P (declared_type)
5750 : 57 : || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5751 : : /* For a non-template class, use the name location. */
5752 : 22 : loc = location_of (declared_type);
5753 : : else
5754 : : /* For a template class (an explicit instantiation), use the
5755 : : current location. */
5756 : 10 : loc = input_location;
5757 : :
5758 : 32 : if (explicit_type_instantiation_p)
5759 : : /* [dcl.attr.grammar]/4:
5760 : :
5761 : : No attribute-specifier-seq shall appertain to an explicit
5762 : : instantiation. */
5763 : : {
5764 : 7 : if (warning_at (loc, OPT_Wattributes,
5765 : : "attribute ignored in explicit instantiation %q#T",
5766 : : declared_type))
5767 : 7 : inform (loc,
5768 : : "no attribute can be applied to "
5769 : : "an explicit instantiation");
5770 : : }
5771 : : else
5772 : 25 : warn_misplaced_attr_for_class_type (loc, declared_type);
5773 : : }
5774 : :
5775 : : /* Diagnose invalid application of contracts, if any. */
5776 : 30115815 : if (find_contract (declspecs->attributes))
5777 : 1 : diagnose_misapplied_contracts (declspecs->attributes);
5778 : : else
5779 : 30115814 : diagnose_misapplied_contracts (declspecs->std_attributes);
5780 : :
5781 : : return declared_type;
5782 : : }
5783 : :
5784 : : /* Called when a declaration is seen that contains no names to declare.
5785 : : If its type is a reference to a structure, union or enum inherited
5786 : : from a containing scope, shadow that tag name for the current scope
5787 : : with a forward reference.
5788 : : If its type defines a new named structure or union
5789 : : or defines an enum, it is valid but we need not do anything here.
5790 : : Otherwise, it is an error.
5791 : :
5792 : : C++: may have to grok the declspecs to learn about static,
5793 : : complain for anonymous unions.
5794 : :
5795 : : Returns the TYPE declared -- or NULL_TREE if none. */
5796 : :
5797 : : tree
5798 : 26540085 : shadow_tag (cp_decl_specifier_seq *declspecs)
5799 : : {
5800 : 26540085 : tree t = check_tag_decl (declspecs,
5801 : : /*explicit_type_instantiation_p=*/false);
5802 : :
5803 : 26540085 : if (!t)
5804 : : return NULL_TREE;
5805 : :
5806 : 26539214 : t = maybe_process_partial_specialization (t);
5807 : 26539214 : if (t == error_mark_node)
5808 : : return NULL_TREE;
5809 : :
5810 : : /* This is where the variables in an anonymous union are
5811 : : declared. An anonymous union declaration looks like:
5812 : : union { ... } ;
5813 : : because there is no declarator after the union, the parser
5814 : : sends that declaration here. */
5815 : 26537704 : if (ANON_AGGR_TYPE_P (t))
5816 : : {
5817 : 285 : fixup_anonymous_aggr (t);
5818 : :
5819 : 285 : if (TYPE_FIELDS (t))
5820 : : {
5821 : 285 : tree decl = grokdeclarator (/*declarator=*/NULL,
5822 : : declspecs, NORMAL, 0, NULL);
5823 : 285 : finish_anon_union (decl);
5824 : : }
5825 : : }
5826 : :
5827 : : return t;
5828 : : }
5829 : :
5830 : : /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5831 : :
5832 : : tree
5833 : 306935391 : groktypename (cp_decl_specifier_seq *type_specifiers,
5834 : : const cp_declarator *declarator,
5835 : : bool is_template_arg)
5836 : : {
5837 : 306935391 : tree attrs;
5838 : 306935391 : tree type;
5839 : 613870782 : enum decl_context context
5840 : 306935391 : = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5841 : 306935391 : attrs = type_specifiers->attributes;
5842 : 306935391 : type_specifiers->attributes = NULL_TREE;
5843 : 306935391 : type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5844 : 306935391 : if (attrs && type != error_mark_node)
5845 : : {
5846 : 645 : if (CLASS_TYPE_P (type))
5847 : 7 : warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5848 : : "outside of definition", type);
5849 : 638 : else if (MAYBE_CLASS_TYPE_P (type))
5850 : : /* A template type parameter or other dependent type. */
5851 : 10 : warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5852 : : "type %qT without an associated declaration", type);
5853 : : else
5854 : 628 : cplus_decl_attributes (&type, attrs, 0);
5855 : : }
5856 : 306935391 : return type;
5857 : : }
5858 : :
5859 : : /* Process a DECLARATOR for a function-scope or namespace-scope
5860 : : variable or function declaration.
5861 : : (Function definitions go through start_function; class member
5862 : : declarations appearing in the body of the class go through
5863 : : grokfield.) The DECL corresponding to the DECLARATOR is returned.
5864 : : If an error occurs, the error_mark_node is returned instead.
5865 : :
5866 : : DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5867 : : SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5868 : : for an explicitly defaulted function, or SD_DELETED for an explicitly
5869 : : deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5870 : : implicitly initialized via a default constructor. It can also be
5871 : : SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5872 : : mark the new decl as DECL_DECOMPOSITION_P.
5873 : :
5874 : : ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5875 : : declaration.
5876 : :
5877 : : The scope represented by the context of the returned DECL is pushed
5878 : : (if it is not the global namespace) and is assigned to
5879 : : *PUSHED_SCOPE_P. The caller is then responsible for calling
5880 : : pop_scope on *PUSHED_SCOPE_P if it is set. */
5881 : :
5882 : : tree
5883 : 102922898 : start_decl (const cp_declarator *declarator,
5884 : : cp_decl_specifier_seq *declspecs,
5885 : : int initialized,
5886 : : tree attributes,
5887 : : tree prefix_attributes,
5888 : : tree *pushed_scope_p)
5889 : : {
5890 : 102922898 : tree decl;
5891 : 102922898 : tree context;
5892 : 102922898 : bool was_public;
5893 : 102922898 : int flags;
5894 : 102922898 : bool alias;
5895 : 102922898 : tree initial;
5896 : :
5897 : 102922898 : *pushed_scope_p = NULL_TREE;
5898 : :
5899 : 102922898 : if (prefix_attributes != error_mark_node)
5900 : 102922895 : attributes = attr_chainon (attributes, prefix_attributes);
5901 : :
5902 : 102922898 : decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5903 : : &attributes);
5904 : :
5905 : 102922898 : if (decl == NULL_TREE || VOID_TYPE_P (decl)
5906 : 102922898 : || decl == error_mark_node
5907 : 102920267 : || prefix_attributes == error_mark_node)
5908 : 2634 : return error_mark_node;
5909 : :
5910 : 102920264 : context = CP_DECL_CONTEXT (decl);
5911 : 102920264 : if (context != global_namespace)
5912 : 15916238 : *pushed_scope_p = push_scope (context);
5913 : :
5914 : 102920264 : if (initialized && TREE_CODE (decl) == TYPE_DECL)
5915 : : {
5916 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
5917 : : "typedef %qD is initialized (use %qs instead)",
5918 : : decl, "decltype");
5919 : 12 : return error_mark_node;
5920 : : }
5921 : :
5922 : : /* Save the DECL_INITIAL value in case it gets clobbered to assist
5923 : : with attribute validation. */
5924 : 102920252 : initial = DECL_INITIAL (decl);
5925 : :
5926 : 102920252 : if (initialized)
5927 : : {
5928 : 45227050 : if (! toplevel_bindings_p ()
5929 : 45227050 : && DECL_EXTERNAL (decl))
5930 : 4 : warning (0, "declaration of %q#D has %<extern%> and is initialized",
5931 : : decl);
5932 : 45227050 : DECL_EXTERNAL (decl) = 0;
5933 : 45227050 : if (toplevel_bindings_p ())
5934 : 3807110 : TREE_STATIC (decl) = 1;
5935 : : /* Tell 'cplus_decl_attributes' this is an initialized decl,
5936 : : even though we might not yet have the initializer expression. */
5937 : 45227050 : if (!DECL_INITIAL (decl))
5938 : 44840458 : DECL_INITIAL (decl) = error_mark_node;
5939 : : }
5940 : 102920252 : alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5941 : :
5942 : 102920252 : if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5943 : 3835 : record_key_method_defined (decl);
5944 : :
5945 : : /* If this is a typedef that names the class for linkage purposes
5946 : : (7.1.3p8), apply any attributes directly to the type. */
5947 : 102920252 : if (TREE_CODE (decl) == TYPE_DECL
5948 : 18011534 : && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5949 : 110102052 : && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5950 : : flags = ATTR_FLAG_TYPE_IN_PLACE;
5951 : : else
5952 : : flags = 0;
5953 : :
5954 : : /* Set attributes here so if duplicate decl, will have proper attributes. */
5955 : 102920252 : cplus_decl_attributes (&decl, attributes, flags);
5956 : :
5957 : : /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5958 : : assist with attribute validation. */
5959 : 102920252 : DECL_INITIAL (decl) = initial;
5960 : :
5961 : : /* Dllimported symbols cannot be defined. Static data members (which
5962 : : can be initialized in-class and dllimported) go through grokfield,
5963 : : not here, so we don't need to exclude those decls when checking for
5964 : : a definition. */
5965 : 102920252 : if (initialized && DECL_DLLIMPORT_P (decl))
5966 : : {
5967 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
5968 : : "definition of %q#D is marked %<dllimport%>", decl);
5969 : 0 : DECL_DLLIMPORT_P (decl) = 0;
5970 : : }
5971 : :
5972 : : /* If #pragma weak was used, mark the decl weak now. */
5973 : 102920252 : if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5974 : 39503985 : maybe_apply_pragma_weak (decl);
5975 : :
5976 : 102920252 : if (TREE_CODE (decl) == FUNCTION_DECL
5977 : 34200427 : && DECL_DECLARED_INLINE_P (decl)
5978 : 754905 : && DECL_UNINLINABLE (decl)
5979 : 102920252 : && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5980 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
5981 : : "inline function %qD given attribute %qs", decl, "noinline");
5982 : :
5983 : 102920252 : if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5984 : : {
5985 : 705263 : bool this_tmpl = (current_template_depth
5986 : 705263 : > template_class_depth (context));
5987 : 705263 : if (VAR_P (decl))
5988 : : {
5989 : 358541 : tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5990 : 358541 : if (field == NULL_TREE
5991 : 358541 : || !(VAR_P (field) || variable_template_p (field)))
5992 : 8 : error ("%q+#D is not a static data member of %q#T", decl, context);
5993 : 358533 : else if (variable_template_p (field)
5994 : 358533 : && (DECL_LANG_SPECIFIC (decl)
5995 : 85 : && DECL_TEMPLATE_SPECIALIZATION (decl)))
5996 : : /* OK, specialization was already checked. */;
5997 : 358480 : else if (variable_template_p (field) && !this_tmpl)
5998 : : {
5999 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
6000 : : "non-member-template declaration of %qD", decl);
6001 : 3 : inform (DECL_SOURCE_LOCATION (field), "does not match "
6002 : : "member template declaration here");
6003 : 3 : return error_mark_node;
6004 : : }
6005 : : else
6006 : : {
6007 : 358477 : if (variable_template_p (field))
6008 : 29 : field = DECL_TEMPLATE_RESULT (field);
6009 : :
6010 : 358477 : if (DECL_CONTEXT (field) != context)
6011 : : {
6012 : 4 : if (!same_type_p (DECL_CONTEXT (field), context))
6013 : 4 : permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
6014 : : "to be defined as %<%T::%D%>",
6015 : 4 : DECL_CONTEXT (field), DECL_NAME (decl),
6016 : 4 : context, DECL_NAME (decl));
6017 : 4 : DECL_CONTEXT (decl) = DECL_CONTEXT (field);
6018 : : }
6019 : : /* Static data member are tricky; an in-class initialization
6020 : : still doesn't provide a definition, so the in-class
6021 : : declaration will have DECL_EXTERNAL set, but will have an
6022 : : initialization. Thus, duplicate_decls won't warn
6023 : : about this situation, and so we check here. */
6024 : 358477 : if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
6025 : 12 : error ("duplicate initialization of %qD", decl);
6026 : 358477 : field = duplicate_decls (decl, field);
6027 : 358477 : if (field == error_mark_node)
6028 : : return error_mark_node;
6029 : 358420 : else if (field)
6030 : 358420 : decl = field;
6031 : : }
6032 : : }
6033 : : else
6034 : : {
6035 : 346736 : tree field = check_classfn (context, decl,
6036 : : this_tmpl
6037 : 14 : ? current_template_parms
6038 : : : NULL_TREE);
6039 : 346640 : if (field && field != error_mark_node
6040 : 693362 : && duplicate_decls (decl, field))
6041 : 346639 : decl = field;
6042 : : }
6043 : :
6044 : : /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
6045 : 705203 : DECL_IN_AGGR_P (decl) = 0;
6046 : : /* Do not mark DECL as an explicit specialization if it was not
6047 : : already marked as an instantiation; a declaration should
6048 : : never be marked as a specialization unless we know what
6049 : : template is being specialized. */
6050 : 705203 : if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
6051 : : {
6052 : 344918 : SET_DECL_TEMPLATE_SPECIALIZATION (decl);
6053 : 344918 : if (TREE_CODE (decl) == FUNCTION_DECL)
6054 : 325052 : DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
6055 : 325052 : && DECL_DECLARED_INLINE_P (decl));
6056 : : else
6057 : 19866 : DECL_COMDAT (decl) = false;
6058 : :
6059 : : /* [temp.expl.spec] An explicit specialization of a static data
6060 : : member of a template is a definition if the declaration
6061 : : includes an initializer; otherwise, it is a declaration.
6062 : :
6063 : : We check for processing_specialization so this only applies
6064 : : to the new specialization syntax. */
6065 : 344918 : if (!initialized && processing_specialization)
6066 : 344718 : DECL_EXTERNAL (decl) = 1;
6067 : : }
6068 : :
6069 : 1050003 : if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
6070 : : /* Aliases are definitions. */
6071 : 705272 : && !alias)
6072 : : {
6073 : 33 : if (DECL_VIRTUAL_P (decl) || !flag_contracts)
6074 : 14 : permerror (declarator->id_loc,
6075 : : "declaration of %q#D outside of class is not definition",
6076 : : decl);
6077 : 19 : else if (flag_contract_strict_declarations)
6078 : 4 : warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
6079 : : "declaration of %q#D outside of class is not definition",
6080 : : decl);
6081 : : }
6082 : : }
6083 : :
6084 : : /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
6085 : 102920192 : if (initialized == SD_DECOMPOSITION)
6086 : 111902 : fit_decomposition_lang_decl (decl, NULL_TREE);
6087 : :
6088 : 102920192 : was_public = TREE_PUBLIC (decl);
6089 : :
6090 : 171465635 : if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
6091 : 103306808 : && current_function_decl)
6092 : : {
6093 : : /* A function-scope decl of some namespace-scope decl. */
6094 : 31235 : DECL_LOCAL_DECL_P (decl) = true;
6095 : 31235 : if (named_module_attach_p ())
6096 : 3 : error_at (declarator->id_loc,
6097 : : "block-scope extern declaration %q#D must not be"
6098 : : " attached to a named module", decl);
6099 : : }
6100 : :
6101 : : /* Enter this declaration into the symbol table. Don't push the plain
6102 : : VAR_DECL for a variable template. */
6103 : 102920192 : if (!template_parm_scope_p ()
6104 : 102920192 : || !VAR_P (decl))
6105 : 100562628 : decl = maybe_push_decl (decl);
6106 : :
6107 : 102920192 : if (processing_template_decl)
6108 : 63412282 : decl = push_template_decl (decl);
6109 : :
6110 : 102920192 : if (decl == error_mark_node)
6111 : : return error_mark_node;
6112 : :
6113 : 102919840 : if (VAR_P (decl)
6114 : 50708032 : && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
6115 : 227346 : && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
6116 : : /* But not templated variables. */
6117 : 103056889 : && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
6118 : : {
6119 : : /* This is a const variable with implicit 'static'. Set
6120 : : DECL_THIS_STATIC so we can tell it from variables that are
6121 : : !TREE_PUBLIC because of the anonymous namespace. */
6122 : 26473 : gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
6123 : 26473 : DECL_THIS_STATIC (decl) = 1;
6124 : : }
6125 : :
6126 : 56519947 : if (current_function_decl && VAR_P (decl)
6127 : 46057975 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
6128 : 107301137 : && cxx_dialect < cxx23)
6129 : : {
6130 : 2764386 : bool ok = false;
6131 : 2764386 : if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
6132 : 20 : error_at (DECL_SOURCE_LOCATION (decl),
6133 : : "%qD defined %<thread_local%> in %qs function only "
6134 : : "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
6135 : 10 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6136 : : ? "consteval" : "constexpr");
6137 : 2764376 : else if (TREE_STATIC (decl))
6138 : 86 : error_at (DECL_SOURCE_LOCATION (decl),
6139 : : "%qD defined %<static%> in %qs function only available "
6140 : : "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
6141 : 43 : DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
6142 : : ? "consteval" : "constexpr");
6143 : : else
6144 : : ok = true;
6145 : 53 : if (!ok)
6146 : 53 : cp_function_chain->invalid_constexpr = true;
6147 : : }
6148 : :
6149 : 102919840 : if (!processing_template_decl && VAR_P (decl))
6150 : 4949266 : start_decl_1 (decl, initialized);
6151 : :
6152 : 102919836 : return decl;
6153 : : }
6154 : :
6155 : : /* Process the declaration of a variable DECL. INITIALIZED is true
6156 : : iff DECL is explicitly initialized. (INITIALIZED is false if the
6157 : : variable is initialized via an implicitly-called constructor.)
6158 : : This function must be called for ordinary variables (including, for
6159 : : example, implicit instantiations of templates), but must not be
6160 : : called for template declarations. */
6161 : :
6162 : : void
6163 : 4956766 : start_decl_1 (tree decl, bool initialized)
6164 : : {
6165 : 4956766 : gcc_checking_assert (!processing_template_decl);
6166 : :
6167 : 4956766 : if (error_operand_p (decl))
6168 : : return;
6169 : :
6170 : 4956751 : gcc_checking_assert (VAR_P (decl));
6171 : :
6172 : 4956751 : tree type = TREE_TYPE (decl);
6173 : 4956751 : bool complete_p = COMPLETE_TYPE_P (type);
6174 : 4956751 : bool aggregate_definition_p
6175 : 4956751 : = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
6176 : :
6177 : : /* If an explicit initializer is present, or if this is a definition
6178 : : of an aggregate, then we need a complete type at this point.
6179 : : (Scalars are always complete types, so there is nothing to
6180 : : check.) This code just sets COMPLETE_P; errors (if necessary)
6181 : : are issued below. */
6182 : 4956751 : if ((initialized || aggregate_definition_p)
6183 : 3948163 : && !complete_p
6184 : 5794510 : && COMPLETE_TYPE_P (complete_type (type)))
6185 : : {
6186 : 316780 : complete_p = true;
6187 : : /* We will not yet have set TREE_READONLY on DECL if the type
6188 : : was "const", but incomplete, before this point. But, now, we
6189 : : have a complete type, so we can try again. */
6190 : 316780 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6191 : : }
6192 : :
6193 : 4956747 : if (initialized)
6194 : : /* Is it valid for this decl to have an initializer at all? */
6195 : : {
6196 : : /* Don't allow initializations for incomplete types except for
6197 : : arrays which might be completed by the initialization. */
6198 : 3364621 : if (complete_p)
6199 : : ; /* A complete type is ok. */
6200 : 520747 : else if (type_uses_auto (type))
6201 : : ; /* An auto type is ok. */
6202 : 58615 : else if (TREE_CODE (type) != ARRAY_TYPE)
6203 : : {
6204 : 29 : auto_diagnostic_group d;
6205 : 29 : error ("variable %q#D has initializer but incomplete type", decl);
6206 : 51 : maybe_suggest_missing_header (input_location,
6207 : 29 : TYPE_IDENTIFIER (type),
6208 : 29 : CP_TYPE_CONTEXT (type));
6209 : 29 : type = TREE_TYPE (decl) = error_mark_node;
6210 : 29 : }
6211 : 58586 : else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
6212 : : {
6213 : 0 : if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
6214 : 0 : error ("elements of array %q#D have incomplete type", decl);
6215 : : /* else we already gave an error in start_decl. */
6216 : : }
6217 : : }
6218 : 1592126 : else if (aggregate_definition_p && !complete_p)
6219 : : {
6220 : 228 : if (type_uses_auto (type))
6221 : 45 : gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
6222 : : else
6223 : : {
6224 : 183 : auto_diagnostic_group d;
6225 : 183 : error ("aggregate %q#D has incomplete type and cannot be defined",
6226 : : decl);
6227 : 315 : maybe_suggest_missing_header (input_location,
6228 : 183 : TYPE_IDENTIFIER (type),
6229 : 183 : CP_TYPE_CONTEXT (type));
6230 : : /* Change the type so that assemble_variable will give
6231 : : DECL an rtl we can live with: (mem (const_int 0)). */
6232 : 183 : type = TREE_TYPE (decl) = error_mark_node;
6233 : 183 : }
6234 : : }
6235 : :
6236 : : /* Create a new scope to hold this declaration if necessary.
6237 : : Whether or not a new scope is necessary cannot be determined
6238 : : until after the type has been completed; if the type is a
6239 : : specialization of a class template it is not until after
6240 : : instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6241 : : will be set correctly. */
6242 : 4956747 : maybe_push_cleanup_level (type);
6243 : : }
6244 : :
6245 : : /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
6246 : : C++20 P0960. TYPE is the type of the object we're initializing. */
6247 : :
6248 : : tree
6249 : 110 : do_aggregate_paren_init (tree init, tree type)
6250 : : {
6251 : 110 : tree val = TREE_VALUE (init);
6252 : :
6253 : 110 : if (TREE_CHAIN (init) == NULL_TREE)
6254 : : {
6255 : : /* If the list has a single element and it's a string literal,
6256 : : then it's the initializer for the array as a whole. */
6257 : 71 : if (TREE_CODE (type) == ARRAY_TYPE
6258 : 71 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6259 : 90 : && TREE_CODE (tree_strip_any_location_wrapper (val))
6260 : : == STRING_CST)
6261 : : return val;
6262 : : /* Handle non-standard extensions like compound literals. This also
6263 : : prevents triggering aggregate parenthesized-initialization in
6264 : : compiler-generated code for =default. */
6265 : 54 : else if (same_type_ignoring_top_level_qualifiers_p (type,
6266 : 54 : TREE_TYPE (val)))
6267 : : return val;
6268 : : }
6269 : :
6270 : 71 : init = build_constructor_from_list (init_list_type_node, init);
6271 : 71 : CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6272 : 71 : CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6273 : 71 : return init;
6274 : : }
6275 : :
6276 : : /* Handle initialization of references. DECL, TYPE, and INIT have the
6277 : : same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
6278 : : but will be set to a new CLEANUP_STMT if a temporary is created
6279 : : that must be destroyed subsequently.
6280 : :
6281 : : Returns an initializer expression to use to initialize DECL, or
6282 : : NULL if the initialization can be performed statically.
6283 : :
6284 : : Quotes on semantics can be found in ARM 8.4.3. */
6285 : :
6286 : : static tree
6287 : 263658 : grok_reference_init (tree decl, tree type, tree init, int flags)
6288 : : {
6289 : 263658 : if (init == NULL_TREE)
6290 : : {
6291 : 16 : if ((DECL_LANG_SPECIFIC (decl) == 0
6292 : 4 : || DECL_IN_AGGR_P (decl) == 0)
6293 : 20 : && ! DECL_THIS_EXTERN (decl))
6294 : 16 : error_at (DECL_SOURCE_LOCATION (decl),
6295 : : "%qD declared as reference but not initialized", decl);
6296 : 16 : return NULL_TREE;
6297 : : }
6298 : :
6299 : 263642 : tree ttype = TREE_TYPE (type);
6300 : 263642 : if (TREE_CODE (init) == TREE_LIST)
6301 : : {
6302 : : /* This handles (C++20 only) code like
6303 : :
6304 : : const A& r(1, 2, 3);
6305 : :
6306 : : where we treat the parenthesized list as a CONSTRUCTOR. */
6307 : 1213 : if (TREE_TYPE (init) == NULL_TREE
6308 : 1213 : && CP_AGGREGATE_TYPE_P (ttype)
6309 : 29 : && !DECL_DECOMPOSITION_P (decl)
6310 : 1239 : && (cxx_dialect >= cxx20))
6311 : : {
6312 : : /* We don't know yet if we should treat const A& r(1) as
6313 : : const A& r{1}. */
6314 : 14 : if (list_length (init) == 1)
6315 : : {
6316 : 7 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
6317 : 7 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6318 : : tf_warning_or_error);
6319 : : }
6320 : : /* If the list had more than one element, the code is ill-formed
6321 : : pre-C++20, so we can build a constructor right away. */
6322 : : else
6323 : 7 : init = do_aggregate_paren_init (init, ttype);
6324 : : }
6325 : : else
6326 : 1199 : init = build_x_compound_expr_from_list (init, ELK_INIT,
6327 : : tf_warning_or_error);
6328 : : }
6329 : :
6330 : 263642 : if (TREE_CODE (ttype) != ARRAY_TYPE
6331 : 263642 : && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
6332 : : /* Note: default conversion is only called in very special cases. */
6333 : 15 : init = decay_conversion (init, tf_warning_or_error);
6334 : :
6335 : : /* check_initializer handles this for non-reference variables, but for
6336 : : references we need to do it here or the initializer will get the
6337 : : incomplete array type and confuse later calls to
6338 : : cp_complete_array_type. */
6339 : 263642 : if (TREE_CODE (ttype) == ARRAY_TYPE
6340 : 4272 : && TYPE_DOMAIN (ttype) == NULL_TREE
6341 : 263703 : && (BRACE_ENCLOSED_INITIALIZER_P (init)
6342 : 44 : || TREE_CODE (init) == STRING_CST))
6343 : : {
6344 : 17 : cp_complete_array_type (&ttype, init, false);
6345 : 17 : if (ttype != TREE_TYPE (type))
6346 : 17 : type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6347 : : }
6348 : :
6349 : : /* Convert INIT to the reference type TYPE. This may involve the
6350 : : creation of a temporary, whose lifetime must be the same as that
6351 : : of the reference. If so, a DECL_EXPR for the temporary will be
6352 : : added just after the DECL_EXPR for DECL. That's why we don't set
6353 : : DECL_INITIAL for local references (instead assigning to them
6354 : : explicitly); we need to allow the temporary to be initialized
6355 : : first. */
6356 : 263642 : return initialize_reference (type, init, flags,
6357 : 263642 : tf_warning_or_error);
6358 : : }
6359 : :
6360 : : /* Designated initializers in arrays are not supported in GNU C++.
6361 : : The parser cannot detect this error since it does not know whether
6362 : : a given brace-enclosed initializer is for a class type or for an
6363 : : array. This function checks that CE does not use a designated
6364 : : initializer. If it does, an error is issued. Returns true if CE
6365 : : is valid, i.e., does not have a designated initializer. */
6366 : :
6367 : : bool
6368 : 26648593 : check_array_designated_initializer (constructor_elt *ce,
6369 : : unsigned HOST_WIDE_INT index)
6370 : : {
6371 : : /* Designated initializers for array elements are not supported. */
6372 : 26648593 : if (ce->index)
6373 : : {
6374 : : /* The parser only allows identifiers as designated
6375 : : initializers. */
6376 : 16827537 : if (ce->index == error_mark_node)
6377 : : {
6378 : 0 : error ("name used in a GNU-style designated "
6379 : : "initializer for an array");
6380 : 0 : return false;
6381 : : }
6382 : 16827537 : else if (identifier_p (ce->index))
6383 : : {
6384 : 23 : error ("name %qD used in a GNU-style designated "
6385 : : "initializer for an array", ce->index);
6386 : 23 : return false;
6387 : : }
6388 : :
6389 : 16827514 : tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6390 : : ce->index, true);
6391 : 16827514 : if (ce_index
6392 : 16827510 : && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6393 : 33655021 : && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6394 : : == INTEGER_CST))
6395 : : {
6396 : : /* A C99 designator is OK if it matches the current index. */
6397 : 16827507 : if (wi::to_wide (ce_index) == index)
6398 : : {
6399 : 16827499 : ce->index = ce_index;
6400 : 16827499 : return true;
6401 : : }
6402 : : else
6403 : 8 : sorry ("non-trivial designated initializers not supported");
6404 : : }
6405 : : else
6406 : 7 : error_at (cp_expr_loc_or_input_loc (ce->index),
6407 : : "C99 designator %qE is not an integral constant-expression",
6408 : : ce->index);
6409 : :
6410 : 15 : return false;
6411 : : }
6412 : :
6413 : : return true;
6414 : : }
6415 : :
6416 : : /* When parsing `int a[] = {1, 2};' we don't know the size of the
6417 : : array until we finish parsing the initializer. If that's the
6418 : : situation we're in, update DECL accordingly. */
6419 : :
6420 : : static void
6421 : 49117500 : maybe_deduce_size_from_array_init (tree decl, tree init)
6422 : : {
6423 : 49117500 : tree type = TREE_TYPE (decl);
6424 : :
6425 : 49117500 : if (TREE_CODE (type) == ARRAY_TYPE
6426 : 733462 : && TYPE_DOMAIN (type) == NULL_TREE
6427 : 49573956 : && TREE_CODE (decl) != TYPE_DECL)
6428 : : {
6429 : : /* do_default is really a C-ism to deal with tentative definitions.
6430 : : But let's leave it here to ease the eventual merge. */
6431 : 456456 : int do_default = !DECL_EXTERNAL (decl);
6432 : 456456 : tree initializer = init ? init : DECL_INITIAL (decl);
6433 : 456456 : int failure = 0;
6434 : :
6435 : : /* Check that there are no designated initializers in INIT, as
6436 : : those are not supported in GNU C++, and as the middle-end
6437 : : will crash if presented with a non-numeric designated
6438 : : initializer. */
6439 : 456456 : if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6440 : : {
6441 : 67075 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6442 : 67075 : constructor_elt *ce;
6443 : 67075 : HOST_WIDE_INT i;
6444 : 8230007 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6445 : : {
6446 : 8163040 : if (instantiation_dependent_expression_p (ce->index))
6447 : 49117500 : return;
6448 : 8163032 : if (!check_array_designated_initializer (ce, i))
6449 : 3 : failure = 1;
6450 : : /* If an un-designated initializer is type-dependent, we can't
6451 : : check brace elision yet. */
6452 : 8163032 : if (ce->index == NULL_TREE
6453 : 8163032 : && type_dependent_expression_p (ce->value))
6454 : : return;
6455 : : }
6456 : : }
6457 : :
6458 : 66967 : if (failure)
6459 : 3 : TREE_TYPE (decl) = error_mark_node;
6460 : : else
6461 : : {
6462 : 456345 : failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6463 : : do_default);
6464 : 456345 : if (failure == 1)
6465 : : {
6466 : 44 : error_at (cp_expr_loc_or_loc (initializer,
6467 : 40 : DECL_SOURCE_LOCATION (decl)),
6468 : : "initializer fails to determine size of %qD", decl);
6469 : : }
6470 : 456305 : else if (failure == 2)
6471 : : {
6472 : 33 : if (do_default)
6473 : : {
6474 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
6475 : : "array size missing in %qD", decl);
6476 : : }
6477 : : /* If a `static' var's size isn't known, make it extern as
6478 : : well as static, so it does not get allocated. If it's not
6479 : : `static', then don't mark it extern; finish_incomplete_decl
6480 : : will give it a default size and it will get allocated. */
6481 : 25 : else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6482 : 0 : DECL_EXTERNAL (decl) = 1;
6483 : : }
6484 : 456272 : else if (failure == 3)
6485 : : {
6486 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
6487 : : "zero-size array %qD", decl);
6488 : : }
6489 : : }
6490 : :
6491 : 456348 : cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6492 : :
6493 : 456348 : relayout_decl (decl);
6494 : : }
6495 : : }
6496 : :
6497 : : /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6498 : : any appropriate error messages regarding the layout. */
6499 : :
6500 : : static void
6501 : 41267337 : layout_var_decl (tree decl)
6502 : : {
6503 : 41267337 : tree type;
6504 : :
6505 : 41267337 : type = TREE_TYPE (decl);
6506 : 41267337 : if (type == error_mark_node)
6507 : : return;
6508 : :
6509 : : /* If we haven't already laid out this declaration, do so now.
6510 : : Note that we must not call complete type for an external object
6511 : : because it's type might involve templates that we are not
6512 : : supposed to instantiate yet. (And it's perfectly valid to say
6513 : : `extern X x' for some incomplete type `X'.) */
6514 : 41267284 : if (!DECL_EXTERNAL (decl))
6515 : 33250585 : complete_type (type);
6516 : 41267284 : if (!DECL_SIZE (decl)
6517 : 1942537 : && TREE_TYPE (decl) != error_mark_node
6518 : 43209821 : && complete_or_array_type_p (type))
6519 : 1908927 : layout_decl (decl, 0);
6520 : :
6521 : 41267284 : if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6522 : : {
6523 : : /* An automatic variable with an incomplete type: that is an error.
6524 : : Don't talk about array types here, since we took care of that
6525 : : message in grokdeclarator. */
6526 : 22 : error_at (DECL_SOURCE_LOCATION (decl),
6527 : : "storage size of %qD isn%'t known", decl);
6528 : 22 : TREE_TYPE (decl) = error_mark_node;
6529 : : }
6530 : : #if 0
6531 : : /* Keep this code around in case we later want to control debug info
6532 : : based on whether a type is "used". (jason 1999-11-11) */
6533 : :
6534 : : else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6535 : : /* Let debugger know it should output info for this type. */
6536 : : note_debug_info_needed (ttype);
6537 : :
6538 : : if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6539 : : note_debug_info_needed (DECL_CONTEXT (decl));
6540 : : #endif
6541 : :
6542 : 74517869 : if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6543 : 20476733 : && DECL_SIZE (decl) != NULL_TREE
6544 : 61583313 : && ! TREE_CONSTANT (DECL_SIZE (decl)))
6545 : : {
6546 : 8 : if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6547 : 8 : && !DECL_LOCAL_DECL_P (decl))
6548 : 0 : constant_expression_warning (DECL_SIZE (decl));
6549 : : else
6550 : : {
6551 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
6552 : : "storage size of %qD isn%'t constant", decl);
6553 : 8 : TREE_TYPE (decl) = error_mark_node;
6554 : 8 : type = error_mark_node;
6555 : : }
6556 : : }
6557 : :
6558 : : /* If the final element initializes a flexible array field, add the size of
6559 : : that initializer to DECL's size. */
6560 : 41267284 : if (type != error_mark_node
6561 : 41267276 : && DECL_INITIAL (decl)
6562 : 20333875 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6563 : 3325991 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6564 : 2810099 : && DECL_SIZE (decl) != NULL_TREE
6565 : 2810099 : && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6566 : 2810099 : && COMPLETE_TYPE_P (type)
6567 : 2810099 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6568 : 44077383 : && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6569 : : {
6570 : 2810099 : constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6571 : 2810099 : if (elt.index)
6572 : : {
6573 : 2611006 : tree itype = TREE_TYPE (elt.index);
6574 : 2611006 : tree vtype = TREE_TYPE (elt.value);
6575 : 2611006 : if (TREE_CODE (itype) == ARRAY_TYPE
6576 : 45397 : && TYPE_DOMAIN (itype) == NULL
6577 : 294 : && TREE_CODE (vtype) == ARRAY_TYPE
6578 : 2611300 : && COMPLETE_TYPE_P (vtype))
6579 : : {
6580 : 294 : DECL_SIZE (decl)
6581 : 294 : = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6582 : 294 : DECL_SIZE_UNIT (decl)
6583 : 588 : = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6584 : : TYPE_SIZE_UNIT (vtype));
6585 : : }
6586 : : }
6587 : : }
6588 : : }
6589 : :
6590 : : /* If a local static variable is declared in an inline function, or if
6591 : : we have a weak definition, we must endeavor to create only one
6592 : : instance of the variable at link-time. */
6593 : :
6594 : : void
6595 : 45166214 : maybe_commonize_var (tree decl)
6596 : : {
6597 : : /* Don't mess with __FUNCTION__ and similar. But do handle structured
6598 : : bindings. */
6599 : 45166214 : if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl))
6600 : : return;
6601 : :
6602 : : /* Static data in a function with comdat linkage also has comdat
6603 : : linkage. */
6604 : 40615401 : if ((TREE_STATIC (decl)
6605 : 19429082 : && DECL_FUNCTION_SCOPE_P (decl)
6606 : 117251 : && vague_linkage_p (DECL_CONTEXT (decl)))
6607 : 79206278 : || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6608 : : {
6609 : 15472765 : if (flag_weak)
6610 : : {
6611 : : /* With weak symbols, we simply make the variable COMDAT;
6612 : : that will cause copies in multiple translations units to
6613 : : be merged. */
6614 : 15472739 : comdat_linkage (decl);
6615 : : }
6616 : : else
6617 : : {
6618 : 26 : if (DECL_INITIAL (decl) == NULL_TREE
6619 : 26 : || DECL_INITIAL (decl) == error_mark_node)
6620 : : {
6621 : : /* Without weak symbols, we can use COMMON to merge
6622 : : uninitialized variables. */
6623 : 24 : TREE_PUBLIC (decl) = 1;
6624 : 24 : DECL_COMMON (decl) = 1;
6625 : : }
6626 : : else
6627 : : {
6628 : : /* While for initialized variables, we must use internal
6629 : : linkage -- which means that multiple copies will not
6630 : : be merged. */
6631 : 2 : TREE_PUBLIC (decl) = 0;
6632 : 2 : DECL_COMMON (decl) = 0;
6633 : 2 : DECL_INTERFACE_KNOWN (decl) = 1;
6634 : 2 : const char *msg;
6635 : 2 : if (DECL_INLINE_VAR_P (decl))
6636 : : msg = G_("sorry: semantics of inline variable "
6637 : : "%q#D are wrong (you%'ll wind up with "
6638 : : "multiple copies)");
6639 : : else
6640 : : msg = G_("sorry: semantics of inline function "
6641 : : "static data %q#D are wrong (you%'ll wind "
6642 : : "up with multiple copies)");
6643 : 2 : if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6644 : : msg, decl))
6645 : 2 : inform (DECL_SOURCE_LOCATION (decl),
6646 : : "you can work around this by removing the initializer");
6647 : : }
6648 : : }
6649 : : }
6650 : : }
6651 : :
6652 : : /* Issue an error message if DECL is an uninitialized const variable.
6653 : : CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6654 : : context from potential_constant_expression. Returns true if all is well,
6655 : : false otherwise. */
6656 : :
6657 : : bool
6658 : 5604454 : check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6659 : : tsubst_flags_t complain)
6660 : : {
6661 : 5604454 : tree type = strip_array_types (TREE_TYPE (decl));
6662 : :
6663 : : /* ``Unless explicitly declared extern, a const object does not have
6664 : : external linkage and must be initialized. ($8.4; $12.1)'' ARM
6665 : : 7.1.6 */
6666 : 5604454 : if (VAR_P (decl)
6667 : 5604454 : && !TYPE_REF_P (type)
6668 : 5585528 : && (CP_TYPE_CONST_P (type)
6669 : : /* C++20 permits trivial default initialization in constexpr
6670 : : context (P1331R2). */
6671 : 4632446 : || (cxx_dialect < cxx20
6672 : 2761819 : && (constexpr_context_p
6673 : 2470796 : || var_in_constexpr_fn (decl))))
6674 : 6958042 : && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6675 : : {
6676 : 606722 : tree field = default_init_uninitialized_part (type);
6677 : 606722 : if (!field)
6678 : : return true;
6679 : :
6680 : 111428 : bool show_notes = true;
6681 : :
6682 : 111428 : if (!constexpr_context_p || cxx_dialect >= cxx20)
6683 : : {
6684 : 107703 : if (CP_TYPE_CONST_P (type))
6685 : : {
6686 : 77 : if (complain & tf_error)
6687 : 75 : show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6688 : : "uninitialized %<const %D%>", decl);
6689 : : }
6690 : : else
6691 : : {
6692 : 107626 : if (!is_instantiation_of_constexpr (current_function_decl)
6693 : 107626 : && (complain & tf_error))
6694 : 21 : error_at (DECL_SOURCE_LOCATION (decl),
6695 : : "uninitialized variable %qD in %<constexpr%> "
6696 : : "function", decl);
6697 : : else
6698 : : show_notes = false;
6699 : 107626 : cp_function_chain->invalid_constexpr = true;
6700 : : }
6701 : : }
6702 : 3725 : else if (complain & tf_error)
6703 : 2 : error_at (DECL_SOURCE_LOCATION (decl),
6704 : : "uninitialized variable %qD in %<constexpr%> context",
6705 : : decl);
6706 : :
6707 : 111428 : if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6708 : : {
6709 : 59 : tree defaulted_ctor;
6710 : :
6711 : 59 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6712 : : "%q#T has no user-provided default constructor", type);
6713 : 59 : defaulted_ctor = in_class_defaulted_default_constructor (type);
6714 : 59 : if (defaulted_ctor)
6715 : 23 : inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6716 : : "constructor is not user-provided because it is "
6717 : : "explicitly defaulted in the class body");
6718 : 59 : inform (DECL_SOURCE_LOCATION (field),
6719 : : "and the implicitly-defined constructor does not "
6720 : : "initialize %q#D", field);
6721 : : }
6722 : :
6723 : 111428 : return false;
6724 : : }
6725 : :
6726 : : return true;
6727 : : }
6728 : :
6729 : : /* Structure holding the current initializer being processed by reshape_init.
6730 : : CUR is a pointer to the current element being processed, END is a pointer
6731 : : after the last element present in the initializer. */
6732 : : struct reshape_iter
6733 : : {
6734 : : constructor_elt *cur;
6735 : : constructor_elt *end;
6736 : : };
6737 : :
6738 : : static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6739 : :
6740 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6741 : : returned is the next FIELD_DECL (possibly FIELD itself) that can be
6742 : : initialized as if for an aggregate class. If there are no more such fields,
6743 : : the return value will be NULL. */
6744 : :
6745 : : tree
6746 : 11802593 : next_aggregate_field (tree field)
6747 : : {
6748 : 11802593 : while (field
6749 : 40944319 : && (TREE_CODE (field) != FIELD_DECL
6750 : 7555066 : || DECL_UNNAMED_BIT_FIELD (field)
6751 : 7554956 : || (DECL_ARTIFICIAL (field)
6752 : : /* In C++17, aggregates can have bases. */
6753 : 58884 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)))))
6754 : 29141726 : field = DECL_CHAIN (field);
6755 : :
6756 : 11802593 : return field;
6757 : : }
6758 : :
6759 : : /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6760 : : returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6761 : : to a subobject. If there are no more such fields, the return value will be
6762 : : NULL. */
6763 : :
6764 : : tree
6765 : 1285535 : next_subobject_field (tree field)
6766 : : {
6767 : 1285535 : while (field
6768 : 22664548 : && (TREE_CODE (field) != FIELD_DECL
6769 : 703660 : || DECL_UNNAMED_BIT_FIELD (field)
6770 : 703649 : || (DECL_ARTIFICIAL (field)
6771 : 91154 : && !DECL_FIELD_IS_BASE (field)
6772 : 593 : && !DECL_VIRTUAL_P (field))))
6773 : 21379013 : field = DECL_CHAIN (field);
6774 : :
6775 : 1285535 : return field;
6776 : : }
6777 : :
6778 : : /* Return true for [dcl.init.list] direct-list-initialization from
6779 : : single element of enumeration with a fixed underlying type. */
6780 : :
6781 : : bool
6782 : 104795751 : is_direct_enum_init (tree type, tree init)
6783 : : {
6784 : 104795751 : if (cxx_dialect >= cxx17
6785 : 102065074 : && TREE_CODE (type) == ENUMERAL_TYPE
6786 : 5398005 : && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6787 : 2976672 : && TREE_CODE (init) == CONSTRUCTOR
6788 : 363741 : && CONSTRUCTOR_IS_DIRECT_INIT (init)
6789 : 177545 : && CONSTRUCTOR_NELTS (init) == 1
6790 : : /* DR 2374: The single element needs to be implicitly
6791 : : convertible to the underlying type of the enum. */
6792 : 296 : && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value)
6793 : 104796045 : && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6794 : 294 : TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6795 : 294 : CONSTRUCTOR_ELT (init, 0)->value,
6796 : : LOOKUP_IMPLICIT, tf_none))
6797 : : return true;
6798 : : return false;
6799 : : }
6800 : :
6801 : : /* Subroutine of reshape_init_array and reshape_init_vector, which does
6802 : : the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6803 : : INTEGER_CST representing the size of the array minus one (the maximum index),
6804 : : or NULL_TREE if the array was declared without specifying the size. D is
6805 : : the iterator within the constructor. */
6806 : :
6807 : : static tree
6808 : 326494 : reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6809 : : tree first_initializer_p, tsubst_flags_t complain)
6810 : : {
6811 : 326494 : tree new_init;
6812 : 326494 : bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6813 : 326494 : unsigned HOST_WIDE_INT max_index_cst = 0;
6814 : 326494 : unsigned HOST_WIDE_INT index;
6815 : :
6816 : : /* The initializer for an array is always a CONSTRUCTOR. If this is the
6817 : : outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6818 : : to build a new one. But don't reuse if not complaining; if this is
6819 : : tentative, we might also reshape to another type (95319). */
6820 : 326494 : bool reuse = (first_initializer_p
6821 : 282998 : && (complain & tf_error)
6822 : 282304 : && !CP_AGGREGATE_TYPE_P (elt_type)
6823 : 563112 : && !TREE_SIDE_EFFECTS (first_initializer_p));
6824 : 91782 : if (reuse)
6825 : : new_init = first_initializer_p;
6826 : : else
6827 : 91782 : new_init = build_constructor (init_list_type_node, NULL);
6828 : :
6829 : 326494 : if (sized_array_p)
6830 : : {
6831 : : /* Minus 1 is used for zero sized arrays. */
6832 : 245065 : if (integer_all_onesp (max_index))
6833 : : return new_init;
6834 : :
6835 : 245034 : if (tree_fits_uhwi_p (max_index))
6836 : 245034 : max_index_cst = tree_to_uhwi (max_index);
6837 : : /* sizetype is sign extended, not zero extended. */
6838 : : else
6839 : 0 : max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6840 : : }
6841 : :
6842 : : /* Loop until there are no more initializers. */
6843 : 10189603 : for (index = 0;
6844 : 10189603 : d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6845 : : ++index)
6846 : : {
6847 : 9863282 : tree elt_init;
6848 : 9863282 : constructor_elt *old_cur = d->cur;
6849 : :
6850 : 9863282 : if (d->cur->index)
6851 : 1214844 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6852 : 9863282 : check_array_designated_initializer (d->cur, index);
6853 : 9863282 : elt_init = reshape_init_r (elt_type, d,
6854 : : /*first_initializer_p=*/NULL_TREE,
6855 : : complain);
6856 : 9863282 : if (elt_init == error_mark_node)
6857 : 138 : return error_mark_node;
6858 : 9863144 : tree idx = size_int (index);
6859 : 9863144 : if (reuse)
6860 : : {
6861 : 8536247 : old_cur->index = idx;
6862 : 8536247 : old_cur->value = elt_init;
6863 : : }
6864 : : else
6865 : 1326897 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6866 : : idx, elt_init);
6867 : 9863144 : if (!TREE_CONSTANT (elt_init))
6868 : 125189 : TREE_CONSTANT (new_init) = false;
6869 : :
6870 : : /* This can happen with an invalid initializer (c++/54501). */
6871 : 9863144 : if (d->cur == old_cur && !sized_array_p)
6872 : : break;
6873 : : }
6874 : :
6875 : : return new_init;
6876 : : }
6877 : :
6878 : : /* Subroutine of reshape_init_r, processes the initializers for arrays.
6879 : : Parameters are the same of reshape_init_r. */
6880 : :
6881 : : static tree
6882 : 285142 : reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6883 : : tsubst_flags_t complain)
6884 : : {
6885 : 285142 : tree max_index = NULL_TREE;
6886 : :
6887 : 285142 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6888 : :
6889 : 285142 : if (TYPE_DOMAIN (type))
6890 : 203765 : max_index = array_type_nelts (type);
6891 : :
6892 : 285142 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6893 : 285142 : first_initializer_p, complain);
6894 : : }
6895 : :
6896 : : /* Subroutine of reshape_init_r, processes the initializers for vectors.
6897 : : Parameters are the same of reshape_init_r. */
6898 : :
6899 : : static tree
6900 : 41352 : reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6901 : : {
6902 : 41352 : tree max_index = NULL_TREE;
6903 : :
6904 : 41352 : gcc_assert (VECTOR_TYPE_P (type));
6905 : :
6906 : 41352 : if (COMPOUND_LITERAL_P (d->cur->value))
6907 : : {
6908 : 0 : tree value = d->cur->value;
6909 : 0 : if (!same_type_p (TREE_TYPE (value), type))
6910 : : {
6911 : 0 : if (complain & tf_error)
6912 : 0 : error ("invalid type %qT as initializer for a vector of type %qT",
6913 : 0 : TREE_TYPE (d->cur->value), type);
6914 : 0 : value = error_mark_node;
6915 : : }
6916 : 0 : ++d->cur;
6917 : 0 : return value;
6918 : : }
6919 : :
6920 : : /* For a vector, we initialize it as an array of the appropriate size. */
6921 : 41352 : if (VECTOR_TYPE_P (type))
6922 : 41352 : max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6923 : :
6924 : 41352 : return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6925 : 41352 : NULL_TREE, complain);
6926 : : }
6927 : :
6928 : : /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6929 : : INIT, in isolation from any designator or other initializers. */
6930 : :
6931 : : static tree
6932 : 459187 : reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6933 : : {
6934 : : /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6935 : : calling reshape_init, but this way can just live on the stack. */
6936 : 459187 : constructor_elt elt = { /*index=*/NULL_TREE, init };
6937 : 459187 : reshape_iter iter = { &elt, &elt + 1 };
6938 : 459187 : return reshape_init_r (type, &iter,
6939 : : /*first_initializer_p=*/NULL_TREE,
6940 : 459187 : complain);
6941 : : }
6942 : :
6943 : : /* Subroutine of reshape_init_r, processes the initializers for classes
6944 : : or union. Parameters are the same of reshape_init_r. */
6945 : :
6946 : : static tree
6947 : 2967416 : reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6948 : : tsubst_flags_t complain)
6949 : : {
6950 : 2967416 : tree field;
6951 : 2967416 : tree new_init;
6952 : :
6953 : 2967416 : gcc_assert (CLASS_TYPE_P (type));
6954 : :
6955 : : /* The initializer for a class is always a CONSTRUCTOR. */
6956 : 2967416 : new_init = build_constructor (init_list_type_node, NULL);
6957 : :
6958 : 2967416 : int binfo_idx = -1;
6959 : 2967416 : tree binfo = TYPE_BINFO (type);
6960 : 2967416 : tree base_binfo = NULL_TREE;
6961 : 2967416 : if (cxx_dialect >= cxx17 && uses_template_parms (type))
6962 : : {
6963 : : /* We get here from maybe_aggr_guide for C++20 class template argument
6964 : : deduction. In this case we need to look through the binfo because a
6965 : : template doesn't have base fields. */
6966 : 182 : binfo_idx = 0;
6967 : 182 : BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6968 : : }
6969 : 44 : if (base_binfo)
6970 : : field = base_binfo;
6971 : : else
6972 : 2967372 : field = next_aggregate_field (TYPE_FIELDS (type));
6973 : :
6974 : 2967416 : if (!field)
6975 : : {
6976 : : /* [dcl.init.aggr]
6977 : :
6978 : : An initializer for an aggregate member that is an
6979 : : empty class shall have the form of an empty
6980 : : initializer-list {}. */
6981 : 265 : if (!first_initializer_p)
6982 : : {
6983 : 224 : if (complain & tf_error)
6984 : 15 : error ("initializer for %qT must be brace-enclosed", type);
6985 : 224 : return error_mark_node;
6986 : : }
6987 : : return new_init;
6988 : : }
6989 : :
6990 : : /* For C++20 CTAD, handle pack expansions in the base list. */
6991 : : tree last_was_pack_expansion = NULL_TREE;
6992 : :
6993 : : /* Loop through the initializable fields, gathering initializers. */
6994 : 9073243 : while (d->cur != d->end)
6995 : : {
6996 : 6211363 : tree field_init;
6997 : 6211363 : constructor_elt *old_cur = d->cur;
6998 : 6211363 : bool direct_desig = false;
6999 : :
7000 : : /* Handle C++20 designated initializers. */
7001 : 6211363 : if (d->cur->index)
7002 : : {
7003 : 459294 : if (d->cur->index == error_mark_node)
7004 : 7 : return error_mark_node;
7005 : :
7006 : 459287 : if (TREE_CODE (d->cur->index) == FIELD_DECL)
7007 : : {
7008 : : /* We already reshaped this; we should have returned early from
7009 : : reshape_init. */
7010 : 0 : gcc_checking_assert (false);
7011 : : if (field != d->cur->index)
7012 : : {
7013 : : if (tree id = DECL_NAME (d->cur->index))
7014 : : gcc_checking_assert (d->cur->index
7015 : : == get_class_binding (type, id));
7016 : : field = d->cur->index;
7017 : : }
7018 : : }
7019 : 459287 : else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7020 : : {
7021 : 459279 : CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7022 : 459279 : field = get_class_binding (type, d->cur->index);
7023 : 459279 : direct_desig = true;
7024 : : }
7025 : : else
7026 : : {
7027 : 8 : if (complain & tf_error)
7028 : 8 : error ("%<[%E] =%> used in a GNU-style designated initializer"
7029 : : " for class %qT", d->cur->index, type);
7030 : 8 : return error_mark_node;
7031 : : }
7032 : :
7033 : 459279 : if (!field && ANON_AGGR_TYPE_P (type))
7034 : : /* Apparently the designator isn't for a member of this anonymous
7035 : : struct, so head back to the enclosing class. */
7036 : : break;
7037 : :
7038 : 459276 : if (!field || TREE_CODE (field) != FIELD_DECL)
7039 : : {
7040 : 17 : if (complain & tf_error)
7041 : : {
7042 : 7 : if (field && TREE_CODE (field) == TREE_LIST)
7043 : : {
7044 : 3 : error ("request for member %qD is ambiguous",
7045 : 3 : d->cur->index);
7046 : 3 : print_candidates (field);
7047 : : }
7048 : : else
7049 : 4 : error ("%qT has no non-static data member named %qD", type,
7050 : 4 : d->cur->index);
7051 : : }
7052 : 17 : return error_mark_node;
7053 : : }
7054 : :
7055 : : /* If the element is an anonymous union object and the initializer
7056 : : list is a designated-initializer-list, the anonymous union object
7057 : : is initialized by the designated-initializer-list { D }, where D
7058 : : is the designated-initializer-clause naming a member of the
7059 : : anonymous union object. */
7060 : 459259 : tree ictx = DECL_CONTEXT (field);
7061 : 459259 : if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
7062 : : {
7063 : : /* Find the anon aggr that is a direct member of TYPE. */
7064 : 82 : while (ANON_AGGR_TYPE_P (ictx))
7065 : : {
7066 : 81 : tree cctx = TYPE_CONTEXT (ictx);
7067 : 81 : if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
7068 : 71 : goto found;
7069 : : ictx = cctx;
7070 : : }
7071 : :
7072 : : /* Not found, e.g. FIELD is a member of a base class. */
7073 : 1 : if (complain & tf_error)
7074 : 1 : error ("%qD is not a direct member of %qT", field, type);
7075 : 1 : return error_mark_node;
7076 : :
7077 : 71 : found:
7078 : : /* Now find the TYPE member with that anon aggr type. */
7079 : 71 : tree aafield = TYPE_FIELDS (type);
7080 : 456 : for (; aafield; aafield = TREE_CHAIN (aafield))
7081 : 456 : if (TREE_TYPE (aafield) == ictx)
7082 : : break;
7083 : 0 : gcc_assert (aafield);
7084 : : field = aafield;
7085 : : direct_desig = false;
7086 : : }
7087 : : }
7088 : :
7089 : : /* If we processed all the member of the class, we are done. */
7090 : 6211256 : if (!field)
7091 : : break;
7092 : :
7093 : 12422004 : last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
7094 : 6211006 : ? field : NULL_TREE);
7095 : 6211006 : if (last_was_pack_expansion)
7096 : : /* Each non-trailing aggregate element that is a pack expansion is
7097 : : assumed to correspond to no elements of the initializer list. */
7098 : 8 : goto continue_;
7099 : :
7100 : 6210998 : if (direct_desig)
7101 : : {
7102 : : /* The designated field F is initialized from this one element.
7103 : :
7104 : : Note that we don't want to do this if we found the designator
7105 : : inside an anon aggr above; we use the normal code to implement:
7106 : :
7107 : : "If the element is an anonymous union member and the initializer
7108 : : list is a brace-enclosed designated- initializer-list, the element
7109 : : is initialized by the designated-initializer-list { D }, where D
7110 : : is the designated- initializer-clause naming a member of the
7111 : : anonymous union member." */
7112 : 459187 : field_init = reshape_single_init (TREE_TYPE (field),
7113 : 459187 : d->cur->value, complain);
7114 : 459187 : d->cur++;
7115 : : }
7116 : : else
7117 : 5751811 : field_init = reshape_init_r (TREE_TYPE (field), d,
7118 : : /*first_initializer_p=*/NULL_TREE,
7119 : : complain);
7120 : :
7121 : 6210998 : if (field_init == error_mark_node)
7122 : 246 : return error_mark_node;
7123 : :
7124 : 6210752 : if (d->cur == old_cur && d->cur->index)
7125 : : {
7126 : : /* This can happen with an invalid initializer for a flexible
7127 : : array member (c++/54441). */
7128 : 0 : if (complain & tf_error)
7129 : 0 : error ("invalid initializer for %q#D", field);
7130 : 0 : return error_mark_node;
7131 : : }
7132 : :
7133 : 6210752 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
7134 : :
7135 : : /* [dcl.init.aggr]
7136 : :
7137 : : When a union is initialized with a brace-enclosed
7138 : : initializer, the braces shall only contain an
7139 : : initializer for the first member of the union. */
7140 : 6210752 : if (TREE_CODE (type) == UNION_TYPE)
7141 : : break;
7142 : :
7143 : 6106084 : continue_:
7144 : 6106092 : if (base_binfo)
7145 : : {
7146 : 47 : if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
7147 : : field = base_binfo;
7148 : : else
7149 : 12 : field = next_aggregate_field (TYPE_FIELDS (type));
7150 : : }
7151 : : else
7152 : 6106045 : field = next_aggregate_field (DECL_CHAIN (field));
7153 : : }
7154 : :
7155 : : /* A trailing aggregate element that is a pack expansion is assumed to
7156 : : correspond to all remaining elements of the initializer list (if any). */
7157 : 2966872 : if (last_was_pack_expansion)
7158 : : {
7159 : 7 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
7160 : : last_was_pack_expansion, d->cur->value);
7161 : 20 : while (d->cur != d->end)
7162 : 13 : d->cur++;
7163 : : }
7164 : :
7165 : : return new_init;
7166 : : }
7167 : :
7168 : : /* Subroutine of reshape_init_r. We're in a context where C99 initializer
7169 : : designators are not valid; either complain or return true to indicate
7170 : : that reshape_init_r should return error_mark_node. */
7171 : :
7172 : : static bool
7173 : 50494 : has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
7174 : : {
7175 : 50494 : if (d->cur->index)
7176 : : {
7177 : 40 : if (complain & tf_error)
7178 : 60 : error_at (cp_expr_loc_or_input_loc (d->cur->index),
7179 : : "C99 designator %qE outside aggregate initializer",
7180 : : d->cur->index);
7181 : : else
7182 : : return true;
7183 : : }
7184 : : return false;
7185 : : }
7186 : :
7187 : : /* Subroutine of reshape_init, which processes a single initializer (part of
7188 : : a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
7189 : : iterator within the CONSTRUCTOR which points to the initializer to process.
7190 : : If this is the first initializer of the outermost CONSTRUCTOR node,
7191 : : FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
7192 : :
7193 : : static tree
7194 : 19415752 : reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
7195 : : tsubst_flags_t complain)
7196 : : {
7197 : 19415752 : tree init = d->cur->value;
7198 : :
7199 : 19415752 : if (error_operand_p (init))
7200 : 131 : return error_mark_node;
7201 : :
7202 : 3341345 : if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
7203 : 19465947 : && has_designator_problem (d, complain))
7204 : 0 : return error_mark_node;
7205 : :
7206 : 19415621 : tree stripped_init = tree_strip_any_location_wrapper (init);
7207 : :
7208 : 19415621 : if (TREE_CODE (type) == COMPLEX_TYPE)
7209 : : {
7210 : : /* A complex type can be initialized from one or two initializers,
7211 : : but braces are not elided. */
7212 : 566 : d->cur++;
7213 : 566 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
7214 : : {
7215 : 10 : if (CONSTRUCTOR_NELTS (stripped_init) > 2)
7216 : : {
7217 : 0 : if (complain & tf_error)
7218 : 0 : error ("too many initializers for %qT", type);
7219 : : else
7220 : 0 : return error_mark_node;
7221 : : }
7222 : : }
7223 : 556 : else if (first_initializer_p && d->cur != d->end)
7224 : : {
7225 : 37 : vec<constructor_elt, va_gc> *v = 0;
7226 : 37 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
7227 : 37 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
7228 : 37 : if (has_designator_problem (d, complain))
7229 : 0 : return error_mark_node;
7230 : 37 : d->cur++;
7231 : 37 : init = build_constructor (init_list_type_node, v);
7232 : : }
7233 : 566 : return init;
7234 : : }
7235 : :
7236 : : /* A non-aggregate type is always initialized with a single
7237 : : initializer. */
7238 : 38780048 : if (!CP_AGGREGATE_TYPE_P (type)
7239 : : /* As is an array with dependent bound, which we can see
7240 : : during C++20 aggregate CTAD. */
7241 : 24294157 : || (cxx_dialect >= cxx20
7242 : 2738346 : && TREE_CODE (type) == ARRAY_TYPE
7243 : 112197 : && uses_template_parms (TYPE_DOMAIN (type))))
7244 : : {
7245 : : /* It is invalid to initialize a non-aggregate type with a
7246 : : brace-enclosed initializer before C++0x.
7247 : : We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
7248 : : of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
7249 : : a CONSTRUCTOR (with a record type). */
7250 : 14485895 : if (TREE_CODE (stripped_init) == CONSTRUCTOR
7251 : : /* Don't complain about a capture-init. */
7252 : 287570 : && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
7253 : 14764208 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
7254 : : {
7255 : 278266 : if (SCALAR_TYPE_P (type))
7256 : : {
7257 : 722 : if (cxx_dialect < cxx11)
7258 : : {
7259 : 18 : if (complain & tf_error)
7260 : 18 : error ("braces around scalar initializer for type %qT",
7261 : : type);
7262 : 18 : init = error_mark_node;
7263 : : }
7264 : 704 : else if (first_initializer_p
7265 : 704 : || (CONSTRUCTOR_NELTS (stripped_init) > 0
7266 : 545 : && (BRACE_ENCLOSED_INITIALIZER_P
7267 : : (CONSTRUCTOR_ELT (stripped_init,0)->value))))
7268 : : {
7269 : 18 : if (complain & tf_error)
7270 : 18 : error ("too many braces around scalar initializer "
7271 : : "for type %qT", type);
7272 : 18 : init = error_mark_node;
7273 : : }
7274 : : }
7275 : : else
7276 : 277544 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7277 : : }
7278 : :
7279 : 14485895 : d->cur++;
7280 : 14485895 : return init;
7281 : : }
7282 : :
7283 : : /* "If T is a class type and the initializer list has a single element of
7284 : : type cv U, where U is T or a class derived from T, the object is
7285 : : initialized from that element." Even if T is an aggregate. */
7286 : 4907336 : if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
7287 : 4504986 : && first_initializer_p
7288 : : /* But not if it's a designated init. */
7289 : 2995359 : && !d->cur->index
7290 : 2794933 : && d->end - d->cur == 1
7291 : 5310548 : && reference_related_p (type, TREE_TYPE (init)))
7292 : : {
7293 : 376 : d->cur++;
7294 : 376 : return init;
7295 : : }
7296 : :
7297 : : /* [dcl.init.aggr]
7298 : :
7299 : : All implicit type conversions (clause _conv_) are considered when
7300 : : initializing the aggregate member with an initializer from an
7301 : : initializer-list. If the initializer can initialize a member,
7302 : : the member is initialized. Otherwise, if the member is itself a
7303 : : non-empty subaggregate, brace elision is assumed and the
7304 : : initializer is considered for the initialization of the first
7305 : : member of the subaggregate. */
7306 : 2142484 : if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
7307 : : /* But don't try this for the first initializer, since that would be
7308 : : looking through the outermost braces; A a2 = { a1 }; is not a
7309 : : valid aggregate initialization. */
7310 : 2786365 : && !first_initializer_p
7311 : 4935109 : && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
7312 : 3816 : || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
7313 : : complain)))
7314 : : {
7315 : 2547 : d->cur++;
7316 : 2547 : return init;
7317 : : }
7318 : :
7319 : : /* [dcl.init.string]
7320 : :
7321 : : A char array (whether plain char, signed char, or unsigned char)
7322 : : can be initialized by a string-literal (optionally enclosed in
7323 : : braces); a wchar_t array can be initialized by a wide
7324 : : string-literal (optionally enclosed in braces). */
7325 : 4926237 : if (TREE_CODE (type) == ARRAY_TYPE
7326 : 4926237 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7327 : : {
7328 : 74503 : tree str_init = init;
7329 : 74503 : tree stripped_str_init = stripped_init;
7330 : 74503 : reshape_iter stripd = {};
7331 : :
7332 : : /* Strip one level of braces if and only if they enclose a single
7333 : : element (as allowed by [dcl.init.string]). */
7334 : 74503 : if (!first_initializer_p
7335 : 10808 : && TREE_CODE (stripped_str_init) == CONSTRUCTOR
7336 : 84711 : && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
7337 : : {
7338 : 330 : stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
7339 : 330 : str_init = stripd.cur->value;
7340 : 330 : stripped_str_init = tree_strip_any_location_wrapper (str_init);
7341 : : }
7342 : :
7343 : : /* If it's a string literal, then it's the initializer for the array
7344 : : as a whole. Otherwise, continue with normal initialization for
7345 : : array types (one value per array element). */
7346 : 74503 : if (TREE_CODE (stripped_str_init) == STRING_CST)
7347 : : {
7348 : 58 : if ((first_initializer_p && has_designator_problem (d, complain))
7349 : 659 : || (stripd.cur && has_designator_problem (&stripd, complain)))
7350 : 601 : return error_mark_node;
7351 : 601 : d->cur++;
7352 : 601 : return str_init;
7353 : : }
7354 : : }
7355 : :
7356 : : /* The following cases are about aggregates. If we are not within a full
7357 : : initializer already, and there is not a CONSTRUCTOR, it means that there
7358 : : is a missing set of braces (that is, we are processing the case for
7359 : : which reshape_init exists). */
7360 : 4925636 : bool braces_elided_p = false;
7361 : 4925636 : if (!first_initializer_p)
7362 : : {
7363 : 1635051 : if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7364 : : {
7365 : 1631749 : tree init_type = TREE_TYPE (init);
7366 : 1631749 : if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7367 : : /* There is no need to call reshape_init for pointer-to-member
7368 : : function initializers, as they are always constructed correctly
7369 : : by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7370 : : which is missing outermost braces. We should warn below, and
7371 : : one of the routines below will wrap it in additional { }. */;
7372 : : /* For a nested compound literal, proceed to specialized routines,
7373 : : to handle initialization of arrays and similar. */
7374 : 1631737 : else if (COMPOUND_LITERAL_P (stripped_init))
7375 : 6 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7376 : : /* If we have an unresolved designator, we need to find the member it
7377 : : designates within TYPE, so proceed to the routines below. For
7378 : : FIELD_DECL or INTEGER_CST designators, we're already initializing
7379 : : the designated element. */
7380 : 1631731 : else if (d->cur->index
7381 : 17 : && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7382 : : /* Brace elision with designators is only permitted for anonymous
7383 : : aggregates. */
7384 : 5 : gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7385 : : /* A CONSTRUCTOR of the target's type is a previously
7386 : : digested initializer. */
7387 : 1631726 : else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7388 : : {
7389 : 0 : ++d->cur;
7390 : 0 : return init;
7391 : : }
7392 : : else
7393 : : {
7394 : : /* Something that hasn't been reshaped yet. */
7395 : 1631726 : ++d->cur;
7396 : 1631726 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7397 : 1631726 : return reshape_init (type, init, complain);
7398 : : }
7399 : : }
7400 : :
7401 : 3325 : if (complain & tf_warning)
7402 : 3093 : warning (OPT_Wmissing_braces,
7403 : : "missing braces around initializer for %qT",
7404 : : type);
7405 : : braces_elided_p = true;
7406 : : }
7407 : :
7408 : : /* Dispatch to specialized routines. */
7409 : 3293910 : tree new_init;
7410 : 3293910 : if (CLASS_TYPE_P (type))
7411 : 2967416 : new_init = reshape_init_class (type, d, first_initializer_p, complain);
7412 : 326494 : else if (TREE_CODE (type) == ARRAY_TYPE)
7413 : 285142 : new_init = reshape_init_array (type, d, first_initializer_p, complain);
7414 : 41352 : else if (VECTOR_TYPE_P (type))
7415 : 41352 : new_init = reshape_init_vector (type, d, complain);
7416 : : else
7417 : 0 : gcc_unreachable();
7418 : :
7419 : 3293910 : if (braces_elided_p
7420 : 3325 : && TREE_CODE (new_init) == CONSTRUCTOR)
7421 : 3094 : CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7422 : :
7423 : : return new_init;
7424 : : }
7425 : :
7426 : : /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7427 : : brace-enclosed aggregate initializer.
7428 : :
7429 : : INIT is the CONSTRUCTOR containing the list of initializers describing
7430 : : a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7431 : : It may not presently match the shape of the TYPE; for example:
7432 : :
7433 : : struct S { int a; int b; };
7434 : : struct S a[] = { 1, 2, 3, 4 };
7435 : :
7436 : : Here INIT will hold a vector of four elements, rather than a
7437 : : vector of two elements, each itself a vector of two elements. This
7438 : : routine transforms INIT from the former form into the latter. The
7439 : : revised CONSTRUCTOR node is returned. */
7440 : :
7441 : : tree
7442 : 8016748 : reshape_init (tree type, tree init, tsubst_flags_t complain)
7443 : : {
7444 : 8016748 : vec<constructor_elt, va_gc> *v;
7445 : 8016748 : reshape_iter d;
7446 : 8016748 : tree new_init;
7447 : :
7448 : 8016748 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7449 : :
7450 : 8016748 : v = CONSTRUCTOR_ELTS (init);
7451 : :
7452 : : /* An empty constructor does not need reshaping, and it is always a valid
7453 : : initializer. */
7454 : 8017041 : if (vec_safe_is_empty (v))
7455 : : return init;
7456 : :
7457 : 3342006 : if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL)
7458 : : /* Already reshaped. */
7459 : : return init;
7460 : :
7461 : : /* Brace elision is not performed for a CONSTRUCTOR representing
7462 : : parenthesized aggregate initialization. */
7463 : 3341895 : if (CONSTRUCTOR_IS_PAREN_INIT (init))
7464 : : {
7465 : 195 : tree elt = (*v)[0].value;
7466 : : /* If we're initializing a char array from a string-literal that is
7467 : : enclosed in braces, unwrap it here. */
7468 : 195 : if (TREE_CODE (type) == ARRAY_TYPE
7469 : 109 : && vec_safe_length (v) == 1
7470 : 63 : && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7471 : 210 : && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7472 : : return elt;
7473 : 182 : return init;
7474 : : }
7475 : :
7476 : : /* Handle [dcl.init.list] direct-list-initialization from
7477 : : single element of enumeration with a fixed underlying type. */
7478 : 3341700 : if (is_direct_enum_init (type, init))
7479 : : {
7480 : 228 : tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7481 : 228 : type = cv_unqualified (type);
7482 : 228 : if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7483 : : {
7484 : 198 : warning_sentinel w (warn_useless_cast);
7485 : 198 : warning_sentinel w2 (warn_ignored_qualifiers);
7486 : 198 : return cp_build_c_cast (input_location, type, elt,
7487 : : tf_warning_or_error);
7488 : 198 : }
7489 : : else
7490 : 30 : return error_mark_node;
7491 : : }
7492 : :
7493 : : /* Recurse on this CONSTRUCTOR. */
7494 : 3341472 : d.cur = &(*v)[0];
7495 : 3341472 : d.end = d.cur + v->length ();
7496 : :
7497 : 3341472 : new_init = reshape_init_r (type, &d, init, complain);
7498 : 3341472 : if (new_init == error_mark_node)
7499 : : return error_mark_node;
7500 : :
7501 : : /* Make sure all the element of the constructor were used. Otherwise,
7502 : : issue an error about exceeding initializers. */
7503 : 3340927 : if (d.cur != d.end)
7504 : : {
7505 : 121 : if (complain & tf_error)
7506 : 67 : error ("too many initializers for %qT", type);
7507 : 121 : return error_mark_node;
7508 : : }
7509 : :
7510 : 3340806 : if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7511 : 3340806 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7512 : 374860 : CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7513 : 3340806 : if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7514 : 3340806 : && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7515 : 26597 : gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init)
7516 : : || seen_error ());
7517 : :
7518 : : return new_init;
7519 : : }
7520 : :
7521 : : /* Verify array initializer. Returns true if errors have been reported. */
7522 : :
7523 : : bool
7524 : 761228 : check_array_initializer (tree decl, tree type, tree init)
7525 : : {
7526 : 761228 : tree element_type = TREE_TYPE (type);
7527 : :
7528 : : /* Structured binding when initialized with an array type needs
7529 : : to have complete type. */
7530 : 761228 : if (decl
7531 : 757835 : && DECL_DECOMPOSITION_P (decl)
7532 : 149 : && !DECL_DECOMP_BASE (decl)
7533 : 761377 : && !COMPLETE_TYPE_P (type))
7534 : : {
7535 : 0 : error_at (DECL_SOURCE_LOCATION (decl),
7536 : : "structured binding has incomplete type %qT", type);
7537 : 0 : TREE_TYPE (decl) = error_mark_node;
7538 : 0 : return true;
7539 : : }
7540 : :
7541 : : /* The array type itself need not be complete, because the
7542 : : initializer may tell us how many elements are in the array.
7543 : : But, the elements of the array must be complete. */
7544 : 761228 : if (!COMPLETE_TYPE_P (complete_type (element_type)))
7545 : : {
7546 : 19 : if (decl)
7547 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
7548 : : "elements of array %q#D have incomplete type", decl);
7549 : : else
7550 : 8 : error ("elements of array %q#T have incomplete type", type);
7551 : 19 : return true;
7552 : : }
7553 : :
7554 : 761209 : location_t loc = (decl ? location_of (decl) : input_location);
7555 : 761209 : if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7556 : : return true;
7557 : :
7558 : : /* A compound literal can't have variable size. */
7559 : 761209 : if (init && !decl
7560 : 761209 : && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7561 : 3381 : || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7562 : : {
7563 : 4 : error ("variable-sized compound literal");
7564 : 4 : return true;
7565 : : }
7566 : : return false;
7567 : : }
7568 : :
7569 : : /* Subroutine of check_initializer; args are passed down from that function.
7570 : : Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7571 : :
7572 : : static tree
7573 : 1868814 : build_aggr_init_full_exprs (tree decl, tree init, int flags)
7574 : : {
7575 : 1868814 : gcc_assert (stmts_are_full_exprs_p ());
7576 : 1868814 : if (init)
7577 : 1332169 : maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
7578 : 1868814 : return build_aggr_init (decl, init, flags, tf_warning_or_error);
7579 : : }
7580 : :
7581 : : /* Verify INIT (the initializer for DECL), and record the
7582 : : initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7583 : : grok_reference_init.
7584 : :
7585 : : If the return value is non-NULL, it is an expression that must be
7586 : : evaluated dynamically to initialize DECL. */
7587 : :
7588 : : static tree
7589 : 35868940 : check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7590 : : {
7591 : 35868940 : tree type;
7592 : 35868940 : tree init_code = NULL;
7593 : 35868940 : tree core_type;
7594 : :
7595 : : /* Things that are going to be initialized need to have complete
7596 : : type. */
7597 : 35868940 : TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7598 : :
7599 : 35868940 : if (DECL_HAS_VALUE_EXPR_P (decl))
7600 : : {
7601 : : /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7602 : : it doesn't have storage to be initialized. */
7603 : 150 : gcc_assert (init == NULL_TREE);
7604 : : return NULL_TREE;
7605 : : }
7606 : :
7607 : 35868790 : if (type == error_mark_node)
7608 : : /* We will have already complained. */
7609 : : return NULL_TREE;
7610 : :
7611 : 35868790 : if (TREE_CODE (type) == ARRAY_TYPE)
7612 : : {
7613 : 757835 : if (check_array_initializer (decl, type, init))
7614 : : return NULL_TREE;
7615 : : }
7616 : 35110955 : else if (!COMPLETE_TYPE_P (type))
7617 : : {
7618 : 38 : error_at (DECL_SOURCE_LOCATION (decl),
7619 : : "%q#D has incomplete type", decl);
7620 : 38 : TREE_TYPE (decl) = error_mark_node;
7621 : 38 : return NULL_TREE;
7622 : : }
7623 : : else
7624 : : /* There is no way to make a variable-sized class type in GNU C++. */
7625 : 35110917 : gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7626 : :
7627 : 35868741 : if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7628 : : {
7629 : 1221284 : int init_len = CONSTRUCTOR_NELTS (init);
7630 : 1221284 : if (SCALAR_TYPE_P (type))
7631 : : {
7632 : 40180 : if (init_len == 0)
7633 : : {
7634 : 34538 : maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7635 : 34538 : init = build_zero_init (type, NULL_TREE, false);
7636 : : }
7637 : 5642 : else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7638 : : {
7639 : 16 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7640 : : "scalar object %qD requires one element in "
7641 : : "initializer", decl);
7642 : 8 : TREE_TYPE (decl) = error_mark_node;
7643 : 8 : return NULL_TREE;
7644 : : }
7645 : : }
7646 : : }
7647 : :
7648 : 35868733 : if (TREE_CODE (decl) == CONST_DECL)
7649 : : {
7650 : 0 : gcc_assert (!TYPE_REF_P (type));
7651 : :
7652 : 0 : DECL_INITIAL (decl) = init;
7653 : :
7654 : 0 : gcc_assert (init != NULL_TREE);
7655 : : init = NULL_TREE;
7656 : : }
7657 : 35868733 : else if (!init && DECL_REALLY_EXTERN (decl))
7658 : : ;
7659 : 3953633 : else if (init || type_build_ctor_call (type)
7660 : 39285717 : || TYPE_REF_P (type))
7661 : : {
7662 : 32451765 : if (TYPE_REF_P (type))
7663 : : {
7664 : 263658 : init = grok_reference_init (decl, type, init, flags);
7665 : 263658 : flags |= LOOKUP_ALREADY_DIGESTED;
7666 : : }
7667 : 32188107 : else if (!init)
7668 : 536649 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7669 : : tf_warning_or_error);
7670 : : /* Do not reshape constructors of vectors (they don't need to be
7671 : : reshaped. */
7672 : 31651458 : else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7673 : : {
7674 : 1186461 : if (is_std_init_list (type))
7675 : : {
7676 : 245 : init = perform_implicit_conversion (type, init,
7677 : : tf_warning_or_error);
7678 : 245 : flags |= LOOKUP_ALREADY_DIGESTED;
7679 : : }
7680 : 1186216 : else if (TYPE_NON_AGGREGATE_CLASS (type))
7681 : : {
7682 : : /* Don't reshape if the class has constructors. */
7683 : 165630 : if (cxx_dialect == cxx98)
7684 : 6 : error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7685 : : "in C++98 %qD must be initialized by "
7686 : : "constructor, not by %<{...}%>",
7687 : : decl);
7688 : : }
7689 : 1020586 : else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7690 : : {
7691 : 0 : error ("opaque vector types cannot be initialized");
7692 : 0 : init = error_mark_node;
7693 : : }
7694 : : else
7695 : : {
7696 : 1020586 : init = reshape_init (type, init, tf_warning_or_error);
7697 : 1020586 : flags |= LOOKUP_NO_NARROWING;
7698 : : }
7699 : : }
7700 : : /* [dcl.init] "Otherwise, if the destination type is an array, the object
7701 : : is initialized as follows..." So handle things like
7702 : :
7703 : : int a[](1, 2, 3);
7704 : :
7705 : : which is permitted in C++20 by P0960. */
7706 : 30464997 : else if (TREE_CODE (init) == TREE_LIST
7707 : 503086 : && TREE_TYPE (init) == NULL_TREE
7708 : 503086 : && TREE_CODE (type) == ARRAY_TYPE
7709 : 86 : && !DECL_DECOMPOSITION_P (decl)
7710 : 30465075 : && (cxx_dialect >= cxx20))
7711 : 48 : init = do_aggregate_paren_init (init, type);
7712 : 30464949 : else if (TREE_CODE (init) == TREE_LIST
7713 : 503038 : && TREE_TYPE (init) != unknown_type_node
7714 : 30967987 : && !MAYBE_CLASS_TYPE_P (type))
7715 : : {
7716 : 125518 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7717 : :
7718 : : /* We get here with code like `int a (2);' */
7719 : 125518 : init = build_x_compound_expr_from_list (init, ELK_INIT,
7720 : : tf_warning_or_error);
7721 : : }
7722 : :
7723 : : /* If DECL has an array type without a specific bound, deduce the
7724 : : array size from the initializer. */
7725 : 32451765 : maybe_deduce_size_from_array_init (decl, init);
7726 : 32451765 : type = TREE_TYPE (decl);
7727 : 32451765 : if (type == error_mark_node)
7728 : : return NULL_TREE;
7729 : :
7730 : 62640329 : if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7731 : 2764913 : && !(flags & LOOKUP_ALREADY_DIGESTED)
7732 : 2764668 : && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7733 : 1062702 : && CP_AGGREGATE_TYPE_P (type)
7734 : 897072 : && (CLASS_TYPE_P (type)
7735 : : /* The call to build_aggr_init below could end up
7736 : : calling build_vec_init, which may break when we
7737 : : are processing a template. */
7738 : 1218 : || processing_template_decl
7739 : 1155 : || !TYPE_NEEDS_CONSTRUCTING (type)
7740 : 1116 : || type_has_extended_temps (type))))
7741 : 63034840 : || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7742 : : {
7743 : 1868814 : init_code = build_aggr_init_full_exprs (decl, init, flags);
7744 : :
7745 : : /* A constructor call is a non-trivial initializer even if
7746 : : it isn't explicitly written. */
7747 : 1868814 : if (TREE_SIDE_EFFECTS (init_code))
7748 : 1860842 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7749 : :
7750 : : /* If this is a constexpr initializer, expand_default_init will
7751 : : have returned an INIT_EXPR rather than a CALL_EXPR. In that
7752 : : case, pull the initializer back out and pass it down into
7753 : : store_init_value. */
7754 : 4799590 : while (true)
7755 : : {
7756 : 4799590 : if (TREE_CODE (init_code) == EXPR_STMT
7757 : 2942749 : || TREE_CODE (init_code) == STMT_EXPR
7758 : 2938821 : || TREE_CODE (init_code) == CONVERT_EXPR)
7759 : 2926881 : init_code = TREE_OPERAND (init_code, 0);
7760 : 1872709 : else if (TREE_CODE (init_code) == BIND_EXPR)
7761 : 3895 : init_code = BIND_EXPR_BODY (init_code);
7762 : : else
7763 : : break;
7764 : : }
7765 : 1868814 : if (TREE_CODE (init_code) == INIT_EXPR)
7766 : : {
7767 : : /* In C++20, the call to build_aggr_init could have created
7768 : : an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7769 : : A(1, 2). */
7770 : 1072687 : tree rhs = TREE_OPERAND (init_code, 1);
7771 : 1072687 : if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7772 : : /* Avoid leaking TARGET_EXPR into template trees. */
7773 : 3879 : rhs = build_implicit_conv_flags (type, init, flags);
7774 : 1072687 : init = rhs;
7775 : :
7776 : 1072687 : init_code = NULL_TREE;
7777 : : /* Don't call digest_init; it's unnecessary and will complain
7778 : : about aggregate initialization of non-aggregate classes. */
7779 : 1072687 : flags |= LOOKUP_ALREADY_DIGESTED;
7780 : : }
7781 : 796127 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
7782 : 1591891 : || DECL_DECLARED_CONSTINIT_P (decl))
7783 : : {
7784 : : /* Declared constexpr or constinit, but no suitable initializer;
7785 : : massage init appropriately so we can pass it into
7786 : : store_init_value for the error. */
7787 : 368 : tree new_init = NULL_TREE;
7788 : 368 : if (!processing_template_decl
7789 : 322 : && TREE_CODE (init_code) == CALL_EXPR)
7790 : 282 : new_init = build_cplus_new (type, init_code, tf_none);
7791 : 64 : else if (CLASS_TYPE_P (type)
7792 : 150 : && (!init || TREE_CODE (init) == TREE_LIST))
7793 : 19 : new_init = build_functional_cast (input_location, type,
7794 : : init, tf_none);
7795 : 301 : if (new_init)
7796 : : {
7797 : 301 : init = new_init;
7798 : 301 : if (TREE_CODE (init) == TARGET_EXPR
7799 : 289 : && !(flags & LOOKUP_ONLYCONVERTING))
7800 : 289 : TARGET_EXPR_DIRECT_INIT_P (init) = true;
7801 : : }
7802 : : init_code = NULL_TREE;
7803 : : }
7804 : : else
7805 : : init = NULL_TREE;
7806 : : }
7807 : :
7808 : 32451758 : if (init && TREE_CODE (init) != TREE_VEC)
7809 : : {
7810 : 31655983 : init_code = store_init_value (decl, init, cleanups, flags);
7811 : :
7812 : 31645199 : if (DECL_INITIAL (decl)
7813 : 19939937 : && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7814 : 32976135 : && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7815 : : {
7816 : 814995 : tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7817 : 814995 : if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7818 : 814995 : && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7819 : 48 : cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7820 : : }
7821 : :
7822 : 683998 : if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7823 : 54055 : && DECL_INITIAL (decl)
7824 : 53883 : && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7825 : 31696706 : && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7826 : 8 : warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7827 : 4 : DECL_SOURCE_LOCATION (decl)),
7828 : : 0, "array %qD initialized by parenthesized "
7829 : : "string literal %qE",
7830 : 4 : decl, DECL_INITIAL (decl));
7831 : : init = NULL_TREE;
7832 : : }
7833 : : }
7834 : : else
7835 : : {
7836 : 6833936 : if (CLASS_TYPE_P (core_type = strip_array_types (type))
7837 : 4019989 : && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7838 : 603007 : || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7839 : 21 : diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7840 : : /*complain=*/true);
7841 : :
7842 : 3416968 : check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7843 : : tf_warning_or_error);
7844 : : }
7845 : :
7846 : 0 : if (init && init != error_mark_node)
7847 : 0 : init_code = cp_build_init_expr (decl, init);
7848 : :
7849 : 12601947 : if (init_code && !TREE_SIDE_EFFECTS (init_code)
7850 : 35872218 : && init_code != error_mark_node)
7851 : : init_code = NULL_TREE;
7852 : :
7853 : 35845827 : if (init_code)
7854 : : {
7855 : : /* We might have set these in cp_finish_decl. */
7856 : 12589832 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7857 : 12589832 : TREE_CONSTANT (decl) = false;
7858 : : }
7859 : :
7860 : 12589832 : if (init_code
7861 : 12589832 : && DECL_IN_AGGR_P (decl)
7862 : 7 : && DECL_INITIALIZED_IN_CLASS_P (decl))
7863 : : {
7864 : 0 : static int explained = 0;
7865 : :
7866 : 0 : if (cxx_dialect < cxx11)
7867 : 0 : error ("initializer invalid for static member with constructor");
7868 : 0 : else if (cxx_dialect < cxx17)
7869 : 0 : error ("non-constant in-class initialization invalid for static "
7870 : : "member %qD", decl);
7871 : : else
7872 : 0 : error ("non-constant in-class initialization invalid for non-inline "
7873 : : "static member %qD", decl);
7874 : 0 : if (!explained)
7875 : : {
7876 : 0 : inform (input_location,
7877 : : "(an out of class initialization is required)");
7878 : 0 : explained = 1;
7879 : : }
7880 : 0 : return NULL_TREE;
7881 : : }
7882 : :
7883 : : return init_code;
7884 : : }
7885 : :
7886 : : /* If DECL is not a local variable, give it RTL. */
7887 : :
7888 : : static void
7889 : 89614448 : make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7890 : : {
7891 : 89614448 : int toplev = toplevel_bindings_p ();
7892 : 89614448 : int defer_p;
7893 : :
7894 : : /* Set the DECL_ASSEMBLER_NAME for the object. */
7895 : 89614448 : if (asmspec)
7896 : : {
7897 : : /* The `register' keyword, when used together with an
7898 : : asm-specification, indicates that the variable should be
7899 : : placed in a particular register. */
7900 : 929937 : if (VAR_P (decl) && DECL_REGISTER (decl))
7901 : : {
7902 : 102 : set_user_assembler_name (decl, asmspec);
7903 : 102 : DECL_HARD_REGISTER (decl) = 1;
7904 : : }
7905 : : else
7906 : : {
7907 : 929835 : if (TREE_CODE (decl) == FUNCTION_DECL
7908 : 929835 : && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7909 : 74128 : set_builtin_user_assembler_name (decl, asmspec);
7910 : 929835 : set_user_assembler_name (decl, asmspec);
7911 : 929835 : if (DECL_LOCAL_DECL_P (decl))
7912 : 8 : if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7913 : : /* We have to propagate the name to the ns-alias.
7914 : : This is horrible, as we're affecting a
7915 : : possibly-shared decl. Again, a one-true-decl
7916 : : model breaks down. */
7917 : 8 : if (ns_decl != error_mark_node)
7918 : 8 : set_user_assembler_name (ns_decl, asmspec);
7919 : : }
7920 : : }
7921 : :
7922 : : /* Handle non-variables up front. */
7923 : 89614448 : if (!VAR_P (decl))
7924 : : {
7925 : 48378736 : rest_of_decl_compilation (decl, toplev, at_eof);
7926 : 48378736 : return;
7927 : : }
7928 : :
7929 : : /* If we see a class member here, it should be a static data
7930 : : member. */
7931 : 41235712 : if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7932 : : {
7933 : 3718298 : gcc_assert (TREE_STATIC (decl));
7934 : : /* An in-class declaration of a static data member should be
7935 : : external; it is only a declaration, and not a definition. */
7936 : 3718298 : if (init == NULL_TREE)
7937 : 3718291 : gcc_assert (DECL_EXTERNAL (decl)
7938 : : || !TREE_PUBLIC (decl));
7939 : : }
7940 : :
7941 : : /* We don't create any RTL for local variables. */
7942 : 41235712 : if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7943 : : return;
7944 : :
7945 : : /* We defer emission of local statics until the corresponding
7946 : : DECL_EXPR is expanded. But with constexpr its function might never
7947 : : be expanded, so go ahead and tell cgraph about the variable now. */
7948 : 40890290 : defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7949 : 117336 : && !var_in_maybe_constexpr_fn (decl))
7950 : 40773054 : || DECL_VIRTUAL_P (decl));
7951 : :
7952 : : /* Defer template instantiations. */
7953 : 20445145 : if (DECL_LANG_SPECIFIC (decl)
7954 : 20445145 : && DECL_IMPLICIT_INSTANTIATION (decl))
7955 : : defer_p = 1;
7956 : :
7957 : : /* If we're not deferring, go ahead and assemble the variable. */
7958 : 13882237 : if (!defer_p)
7959 : 11726343 : rest_of_decl_compilation (decl, toplev, at_eof);
7960 : : }
7961 : :
7962 : : /* walk_tree helper for wrap_temporary_cleanups, below. */
7963 : :
7964 : : static tree
7965 : 8747291 : wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7966 : : {
7967 : : /* Stop at types or full-expression boundaries. */
7968 : 8747291 : if (TYPE_P (*stmt_p)
7969 : 8747253 : || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7970 : : {
7971 : 280 : *walk_subtrees = 0;
7972 : 280 : return NULL_TREE;
7973 : : }
7974 : :
7975 : 8747011 : if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7976 : : {
7977 : 100266 : tree guard = (tree)data;
7978 : 100266 : tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7979 : :
7980 : 61661 : if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
7981 : 152256 : && !expr_noexcept_p (tcleanup, tf_none))
7982 : : {
7983 : 116 : tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7984 : : /* Tell honor_protect_cleanup_actions to handle this as a separate
7985 : : cleanup. */
7986 : 116 : TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7987 : 116 : TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7988 : : }
7989 : : }
7990 : :
7991 : : return NULL_TREE;
7992 : : }
7993 : :
7994 : : /* We're initializing a local variable which has a cleanup GUARD. If there
7995 : : are any temporaries used in the initializer INIT of this variable, we
7996 : : need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7997 : : variable will be cleaned up properly if one of them throws.
7998 : :
7999 : : Unfortunately, there's no way to express this properly in terms of
8000 : : nesting, as the regions for the temporaries overlap the region for the
8001 : : variable itself; if there are two temporaries, the variable needs to be
8002 : : the first thing destroyed if either of them throws. However, we only
8003 : : want to run the variable's cleanup if it actually got constructed. So
8004 : : we need to guard the temporary cleanups with the variable's cleanup if
8005 : : they are run on the normal path, but not if they are run on the
8006 : : exceptional path. We implement this by telling
8007 : : honor_protect_cleanup_actions to strip the variable cleanup from the
8008 : : exceptional path.
8009 : :
8010 : : Another approach could be to make the variable cleanup region enclose
8011 : : initialization, but depend on a flag to indicate that the variable is
8012 : : initialized; that's effectively what we do for arrays. But the current
8013 : : approach works fine for non-arrays, and has no code overhead in the usual
8014 : : case where the temporary destructors are noexcept. */
8015 : :
8016 : : static void
8017 : 857834 : wrap_temporary_cleanups (tree init, tree guard)
8018 : : {
8019 : 857834 : if (TREE_CODE (guard) == BIND_EXPR)
8020 : : {
8021 : : /* An array cleanup region already encloses any temporary cleanups,
8022 : : don't wrap it around them again. */
8023 : 411 : gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
8024 : : return;
8025 : : }
8026 : 857423 : cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
8027 : : }
8028 : :
8029 : : /* Generate code to initialize DECL (a local variable). */
8030 : :
8031 : : static void
8032 : 20790547 : initialize_local_var (tree decl, tree init)
8033 : : {
8034 : 20790547 : tree type = TREE_TYPE (decl);
8035 : 20790547 : tree cleanup;
8036 : 20790547 : int already_used;
8037 : :
8038 : 20790547 : gcc_assert (VAR_P (decl)
8039 : : || TREE_CODE (decl) == RESULT_DECL);
8040 : 20790547 : gcc_assert (!TREE_STATIC (decl));
8041 : :
8042 : 20790547 : if (DECL_SIZE (decl) == NULL_TREE)
8043 : : {
8044 : : /* If we used it already as memory, it must stay in memory. */
8045 : 0 : DECL_INITIAL (decl) = NULL_TREE;
8046 : 0 : TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8047 : 0 : return;
8048 : : }
8049 : :
8050 : 20790547 : if (type == error_mark_node)
8051 : : return;
8052 : :
8053 : : /* Compute and store the initial value. */
8054 : 20790547 : already_used = TREE_USED (decl) || TREE_USED (type);
8055 : 20790547 : if (TREE_USED (type))
8056 : 41 : DECL_READ_P (decl) = 1;
8057 : :
8058 : : /* Generate a cleanup, if necessary. */
8059 : 20790547 : cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8060 : :
8061 : : /* Perform the initialization. */
8062 : 20790547 : if (init)
8063 : : {
8064 : 12577178 : tree rinit = (TREE_CODE (init) == INIT_EXPR
8065 : 12577178 : ? TREE_OPERAND (init, 1) : NULL_TREE);
8066 : 11580894 : if (rinit && !TREE_SIDE_EFFECTS (rinit)
8067 : 18139797 : && TREE_OPERAND (init, 0) == decl)
8068 : : {
8069 : : /* Stick simple initializers in DECL_INITIAL so that
8070 : : -Wno-init-self works (c++/34772). */
8071 : 6558900 : DECL_INITIAL (decl) = rinit;
8072 : :
8073 : 6558900 : if (warn_init_self && TYPE_REF_P (type))
8074 : : {
8075 : 2306 : STRIP_NOPS (rinit);
8076 : 2306 : if (rinit == decl)
8077 : 4 : warning_at (DECL_SOURCE_LOCATION (decl),
8078 : : OPT_Winit_self,
8079 : : "reference %qD is initialized with itself", decl);
8080 : : }
8081 : : }
8082 : : else
8083 : : {
8084 : 6018278 : int saved_stmts_are_full_exprs_p;
8085 : :
8086 : : /* If we're only initializing a single object, guard the
8087 : : destructors of any temporaries used in its initializer with
8088 : : its destructor. */
8089 : 6018278 : if (cleanup)
8090 : 853303 : wrap_temporary_cleanups (init, cleanup);
8091 : :
8092 : 6018278 : gcc_assert (building_stmt_list_p ());
8093 : 6018278 : saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8094 : 6018278 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8095 : 6018278 : finish_expr_stmt (init);
8096 : 6018278 : current_stmt_tree ()->stmts_are_full_exprs_p =
8097 : : saved_stmts_are_full_exprs_p;
8098 : : }
8099 : : }
8100 : :
8101 : : /* Set this to 0 so we can tell whether an aggregate which was
8102 : : initialized was ever used. Don't do this if it has a
8103 : : destructor, so we don't complain about the 'resource
8104 : : allocation is initialization' idiom. Now set
8105 : : attribute((unused)) on types so decls of that type will be
8106 : : marked used. (see TREE_USED, above.) */
8107 : 20790547 : if (TYPE_NEEDS_CONSTRUCTING (type)
8108 : 1529363 : && ! already_used
8109 : 4066 : && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8110 : 20794191 : && DECL_NAME (decl))
8111 : 3639 : TREE_USED (decl) = 0;
8112 : 20786908 : else if (already_used)
8113 : 18710092 : TREE_USED (decl) = 1;
8114 : :
8115 : 20790547 : if (cleanup)
8116 : 864683 : finish_decl_cleanup (decl, cleanup);
8117 : : }
8118 : :
8119 : : /* DECL is a VAR_DECL for a compiler-generated variable with static
8120 : : storage duration (like a virtual table) whose initializer is a
8121 : : compile-time constant. Initialize the variable and provide it to the
8122 : : back end. */
8123 : :
8124 : : void
8125 : 1999001 : initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
8126 : : {
8127 : 1999001 : tree init;
8128 : 1999001 : gcc_assert (DECL_ARTIFICIAL (decl));
8129 : 1999001 : init = build_constructor (TREE_TYPE (decl), v);
8130 : 1999001 : gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
8131 : 1999001 : DECL_INITIAL (decl) = init;
8132 : 1999001 : DECL_INITIALIZED_P (decl) = 1;
8133 : : /* Mark the decl as constexpr so that we can access its content
8134 : : at compile time. */
8135 : 1999001 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
8136 : 1999001 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
8137 : 1999001 : determine_visibility (decl);
8138 : 1999001 : layout_var_decl (decl);
8139 : 1999001 : maybe_commonize_var (decl);
8140 : 1999001 : make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
8141 : 1999001 : }
8142 : :
8143 : : /* INIT is the initializer for a variable, as represented by the
8144 : : parser. Returns true iff INIT is value-dependent. */
8145 : :
8146 : : static bool
8147 : 7009768 : value_dependent_init_p (tree init)
8148 : : {
8149 : 7009768 : if (TREE_CODE (init) == TREE_LIST)
8150 : : /* A parenthesized initializer, e.g.: int i (3, 2); ? */
8151 : 47 : return any_value_dependent_elements_p (init);
8152 : 7009721 : else if (TREE_CODE (init) == CONSTRUCTOR)
8153 : : /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
8154 : : {
8155 : 34380 : if (dependent_type_p (TREE_TYPE (init)))
8156 : : return true;
8157 : :
8158 : 34374 : vec<constructor_elt, va_gc> *elts;
8159 : 34374 : size_t nelts;
8160 : 34374 : size_t i;
8161 : :
8162 : 34374 : elts = CONSTRUCTOR_ELTS (init);
8163 : 34374 : nelts = vec_safe_length (elts);
8164 : 518699 : for (i = 0; i < nelts; ++i)
8165 : 495475 : if (value_dependent_init_p ((*elts)[i].value))
8166 : : return true;
8167 : : }
8168 : : else
8169 : : /* It must be a simple expression, e.g., int i = 3; */
8170 : 6975341 : return value_dependent_expression_p (init);
8171 : :
8172 : : return false;
8173 : : }
8174 : :
8175 : : // Returns true if a DECL is VAR_DECL with the concept specifier.
8176 : : static inline bool
8177 : 1550349 : is_concept_var (tree decl)
8178 : : {
8179 : 1550349 : return (VAR_P (decl)
8180 : : // Not all variables have DECL_LANG_SPECIFIC.
8181 : 1550349 : && DECL_LANG_SPECIFIC (decl)
8182 : 1931883 : && DECL_DECLARED_CONCEPT_P (decl));
8183 : : }
8184 : :
8185 : : /* A helper function to be called via walk_tree. If any label exists
8186 : : under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
8187 : :
8188 : : static tree
8189 : 439 : notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
8190 : : {
8191 : 439 : if (TYPE_P (*tp))
8192 : 0 : *walk_subtrees = 0;
8193 : 439 : if (TREE_CODE (*tp) == LABEL_DECL)
8194 : 8 : cfun->has_forced_label_in_static = 1;
8195 : 439 : return NULL_TREE;
8196 : : }
8197 : :
8198 : : /* Return true if DECL has either a trivial destructor, or for C++20
8199 : : is constexpr and has a constexpr destructor. */
8200 : :
8201 : : static bool
8202 : 28443787 : decl_maybe_constant_destruction (tree decl, tree type)
8203 : : {
8204 : 28443787 : return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8205 : 28443787 : || (cxx_dialect >= cxx20
8206 : 1010152 : && VAR_P (decl)
8207 : 1009842 : && DECL_DECLARED_CONSTEXPR_P (decl)
8208 : 148 : && type_has_constexpr_destructor (strip_array_types (type))));
8209 : : }
8210 : :
8211 : : static tree declare_simd_adjust_this (tree *, int *, void *);
8212 : :
8213 : : /* Helper function of omp_declare_variant_finalize. Finalize one
8214 : : "omp declare variant base" attribute. Return true if it should be
8215 : : removed. */
8216 : :
8217 : : static bool
8218 : 1234 : omp_declare_variant_finalize_one (tree decl, tree attr)
8219 : : {
8220 : 1234 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8221 : : {
8222 : 124 : walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
8223 : : DECL_ARGUMENTS (decl), NULL);
8224 : 124 : walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
8225 : : DECL_ARGUMENTS (decl), NULL);
8226 : : }
8227 : :
8228 : 1234 : tree ctx = TREE_VALUE (TREE_VALUE (attr));
8229 : 1234 : tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
8230 : : OMP_TRAIT_CONSTRUCT_SIMD);
8231 : 1234 : if (simd)
8232 : : {
8233 : 68 : TREE_VALUE (simd)
8234 : 68 : = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
8235 : 68 : OMP_TS_PROPERTIES (simd));
8236 : : /* FIXME, adjusting simd args unimplemented. */
8237 : 68 : return true;
8238 : : }
8239 : :
8240 : 1166 : tree chain = TREE_CHAIN (TREE_VALUE (attr));
8241 : 1166 : location_t varid_loc
8242 : 1166 : = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
8243 : 1166 : location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
8244 : 1166 : cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
8245 : 1166 : tree variant = TREE_PURPOSE (TREE_VALUE (attr));
8246 : :
8247 : 1166 : location_t save_loc = input_location;
8248 : 1166 : input_location = varid_loc;
8249 : :
8250 : 1166 : releasing_vec args;
8251 : 1166 : tree parm = DECL_ARGUMENTS (decl);
8252 : 1166 : if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8253 : 124 : parm = DECL_CHAIN (parm);
8254 : 1873 : for (; parm; parm = DECL_CHAIN (parm))
8255 : 707 : if (type_dependent_expression_p (parm))
8256 : 24 : vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
8257 : 683 : else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
8258 : 48 : vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
8259 : : else
8260 : 635 : vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
8261 : :
8262 : 1166 : bool koenig_p = false;
8263 : 1166 : if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
8264 : : {
8265 : 1158 : if (identifier_p (variant)
8266 : : /* In C++20, we may need to perform ADL for a template
8267 : : name. */
8268 : 1142 : || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
8269 : 24 : && identifier_p (TREE_OPERAND (variant, 0))))
8270 : : {
8271 : 16 : if (!args->is_empty ())
8272 : : {
8273 : 16 : koenig_p = true;
8274 : 16 : if (!any_type_dependent_arguments_p (args))
8275 : 16 : variant = perform_koenig_lookup (variant, args,
8276 : : tf_warning_or_error);
8277 : : }
8278 : : else
8279 : 0 : variant = unqualified_fn_lookup_error (variant);
8280 : : }
8281 : 1142 : else if (!args->is_empty () && is_overloaded_fn (variant))
8282 : : {
8283 : 395 : tree fn = get_first_fn (variant);
8284 : 395 : fn = STRIP_TEMPLATE (fn);
8285 : 395 : if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
8286 : 395 : || DECL_FUNCTION_MEMBER_P (fn)
8287 : 279 : || DECL_LOCAL_DECL_P (fn)))
8288 : : {
8289 : 279 : koenig_p = true;
8290 : 279 : if (!any_type_dependent_arguments_p (args))
8291 : 279 : variant = perform_koenig_lookup (variant, args,
8292 : : tf_warning_or_error);
8293 : : }
8294 : : }
8295 : : }
8296 : :
8297 : 1166 : if (idk == CP_ID_KIND_QUALIFIED)
8298 : 8 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
8299 : : koenig_p, tf_warning_or_error);
8300 : : else
8301 : 1158 : variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
8302 : : koenig_p, tf_warning_or_error);
8303 : 1166 : if (variant == error_mark_node && !processing_template_decl)
8304 : : return true;
8305 : :
8306 : 1134 : variant = cp_get_callee_fndecl_nofold (variant);
8307 : 1134 : input_location = save_loc;
8308 : :
8309 : 1134 : if (variant)
8310 : : {
8311 : 1102 : const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
8312 : 1102 : if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
8313 : : {
8314 : 72 : error_at (varid_loc, "variant %qD and base %qD have incompatible "
8315 : : "types", variant, decl);
8316 : 72 : return true;
8317 : : }
8318 : 1030 : if (fndecl_built_in_p (variant)
8319 : 1030 : && (startswith (varname, "__builtin_")
8320 : 0 : || startswith (varname, "__sync_")
8321 : 0 : || startswith (varname, "__atomic_")))
8322 : : {
8323 : 8 : error_at (varid_loc, "variant %qD is a built-in", variant);
8324 : 8 : return true;
8325 : : }
8326 : : else
8327 : : {
8328 : 1022 : tree construct
8329 : 1022 : = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
8330 : 1022 : omp_mark_declare_variant (match_loc, variant, construct);
8331 : 1022 : if (!omp_context_selector_matches (ctx))
8332 : : return true;
8333 : 591 : TREE_PURPOSE (TREE_VALUE (attr)) = variant;
8334 : : }
8335 : : }
8336 : 32 : else if (!processing_template_decl)
8337 : : {
8338 : 0 : error_at (varid_loc, "could not find variant declaration");
8339 : 0 : return true;
8340 : : }
8341 : :
8342 : : return false;
8343 : 1166 : }
8344 : :
8345 : : /* Helper function, finish up "omp declare variant base" attribute
8346 : : now that there is a DECL. ATTR is the first "omp declare variant base"
8347 : : attribute. */
8348 : :
8349 : : void
8350 : 1126 : omp_declare_variant_finalize (tree decl, tree attr)
8351 : : {
8352 : 1126 : size_t attr_len = strlen ("omp declare variant base");
8353 : 1126 : tree *list = &DECL_ATTRIBUTES (decl);
8354 : 1126 : bool remove_all = false;
8355 : 1126 : location_t match_loc = DECL_SOURCE_LOCATION (decl);
8356 : 1126 : if (TREE_CHAIN (TREE_VALUE (attr))
8357 : 1126 : && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
8358 : 2252 : && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
8359 : 1126 : match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
8360 : 2252 : if (DECL_CONSTRUCTOR_P (decl))
8361 : : {
8362 : 36 : error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8363 : 36 : remove_all = true;
8364 : : }
8365 : 1090 : else if (DECL_DESTRUCTOR_P (decl))
8366 : : {
8367 : 12 : error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8368 : 12 : remove_all = true;
8369 : : }
8370 : 1078 : else if (DECL_DEFAULTED_FN (decl))
8371 : : {
8372 : 3 : error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8373 : 3 : remove_all = true;
8374 : : }
8375 : 1075 : else if (DECL_DELETED_FN (decl))
8376 : : {
8377 : 3 : error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8378 : 3 : remove_all = true;
8379 : : }
8380 : 1072 : else if (DECL_VIRTUAL_P (decl))
8381 : : {
8382 : 0 : error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8383 : 0 : remove_all = true;
8384 : : }
8385 : : /* This loop is like private_lookup_attribute, except that it works
8386 : : with tree * rather than tree, as we might want to remove the
8387 : : attributes that are diagnosed as errorneous. */
8388 : 2426 : while (*list)
8389 : : {
8390 : 1300 : tree attr = get_attribute_name (*list);
8391 : 1300 : size_t ident_len = IDENTIFIER_LENGTH (attr);
8392 : 3223 : if (cmp_attribs ("omp declare variant base", attr_len,
8393 : 1300 : IDENTIFIER_POINTER (attr), ident_len))
8394 : : {
8395 : 1288 : if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8396 : : {
8397 : 665 : *list = TREE_CHAIN (*list);
8398 : 665 : continue;
8399 : : }
8400 : : }
8401 : 635 : list = &TREE_CHAIN (*list);
8402 : : }
8403 : 1126 : }
8404 : :
8405 : : static void cp_maybe_mangle_decomp (tree, cp_decomp *);
8406 : :
8407 : : /* Finish processing of a declaration;
8408 : : install its line number and initial value.
8409 : : If the length of an array type is not known before,
8410 : : it must be determined now, from the initial value, or it is an error.
8411 : :
8412 : : INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8413 : : true, then INIT is an integral constant expression.
8414 : :
8415 : : FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8416 : : if the (init) syntax was used.
8417 : :
8418 : : DECOMP is first identifier's DECL and identifier count in a structured
8419 : : bindings, nullptr if not a structured binding. */
8420 : :
8421 : : void
8422 : 248932147 : cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8423 : : tree asmspec_tree, int flags, cp_decomp *decomp)
8424 : : {
8425 : 248932147 : vec<tree, va_gc> *cleanups = NULL;
8426 : 248932147 : const char *asmspec = NULL;
8427 : 248932147 : int was_readonly = 0;
8428 : 248932147 : bool var_definition_p = false;
8429 : 248932147 : tree auto_node;
8430 : :
8431 : 248932147 : if (decl == error_mark_node)
8432 : 153452345 : return;
8433 : 248932123 : else if (! decl)
8434 : : {
8435 : 0 : if (init)
8436 : 0 : error ("assignment (not initialization) in declaration");
8437 : 0 : return;
8438 : : }
8439 : :
8440 : 248932123 : gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8441 : : /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8442 : 248932123 : gcc_assert (TREE_CODE (decl) != PARM_DECL);
8443 : :
8444 : 248932123 : tree type = TREE_TYPE (decl);
8445 : 248932123 : if (type == error_mark_node)
8446 : : return;
8447 : :
8448 : 248931631 : if (VAR_P (decl) && is_copy_initialization (init))
8449 : 67487667 : flags |= LOOKUP_ONLYCONVERTING;
8450 : :
8451 : : /* Warn about register storage specifiers except when in GNU global
8452 : : or local register variable extension. */
8453 : 248931631 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8454 : : {
8455 : 2568 : if (cxx_dialect >= cxx17)
8456 : 1276 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8457 : : "ISO C++17 does not allow %<register%> storage "
8458 : : "class specifier");
8459 : : else
8460 : 1292 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8461 : : "%<register%> storage class specifier used");
8462 : : }
8463 : :
8464 : : /* If a name was specified, get the string. */
8465 : 248931631 : if (at_namespace_scope_p ())
8466 : 51036607 : asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8467 : 248931631 : if (asmspec_tree && asmspec_tree != error_mark_node)
8468 : 929941 : asmspec = TREE_STRING_POINTER (asmspec_tree);
8469 : :
8470 : 248931631 : bool in_class_decl
8471 : 248931631 : = (current_class_type
8472 : 153568918 : && CP_DECL_CONTEXT (decl) == current_class_type
8473 : 104124716 : && TYPE_BEING_DEFINED (current_class_type)
8474 : 351651938 : && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8475 : :
8476 : 99773471 : if (in_class_decl
8477 : 99773471 : && (DECL_INITIAL (decl) || init))
8478 : 71450881 : DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8479 : :
8480 : 248931631 : if (VAR_P (decl)
8481 : 248931631 : && (auto_node = type_uses_auto (type)))
8482 : : {
8483 : 6886338 : tree d_init;
8484 : 6886338 : if (init == NULL_TREE)
8485 : : {
8486 : 118 : if (DECL_LANG_SPECIFIC (decl)
8487 : 65 : && DECL_TEMPLATE_INSTANTIATION (decl)
8488 : 181 : && !DECL_TEMPLATE_INSTANTIATED (decl))
8489 : : {
8490 : : /* init is null because we're deferring instantiating the
8491 : : initializer until we need it. Well, we need it now. */
8492 : 58 : instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8493 : 58 : return;
8494 : : }
8495 : :
8496 : 60 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8497 : : /* Class deduction with no initializer is OK. */;
8498 : : else
8499 : : {
8500 : : /* Ordinary auto deduction without an initializer, a situation
8501 : : which grokdeclarator already detects and rejects for the most
8502 : : part. But we can still get here if we're instantiating a
8503 : : variable template before we've fully parsed (and attached) its
8504 : : initializer, e.g. template<class> auto x = x<int>; */
8505 : 3 : error_at (DECL_SOURCE_LOCATION (decl),
8506 : : "declaration of %q#D has no initializer", decl);
8507 : 3 : TREE_TYPE (decl) = error_mark_node;
8508 : 3 : return;
8509 : : }
8510 : : }
8511 : 6886277 : d_init = init;
8512 : 6886277 : if (d_init)
8513 : : {
8514 : 6886220 : if (TREE_CODE (d_init) == TREE_LIST
8515 : 6903849 : && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8516 : 11332 : d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8517 : : tf_warning_or_error);
8518 : 6886220 : d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8519 : : /* Force auto deduction now. Use tf_none to avoid redundant warnings
8520 : : on deprecated-14.C. */
8521 : 6886220 : mark_single_function (d_init, tf_none);
8522 : : }
8523 : 6886277 : enum auto_deduction_context adc = adc_variable_type;
8524 : 6886277 : if (DECL_DECOMPOSITION_P (decl))
8525 : : adc = adc_decomp_type;
8526 : 6886277 : tree outer_targs = NULL_TREE;
8527 : 6886277 : if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8528 : 178 : && DECL_LANG_SPECIFIC (decl)
8529 : 42 : && DECL_TEMPLATE_INFO (decl)
8530 : 6886296 : && !DECL_FUNCTION_SCOPE_P (decl))
8531 : : /* The outer template arguments might be needed for satisfaction.
8532 : : (For function scope variables, do_auto_deduction will obtain the
8533 : : outer template arguments from current_function_decl.) */
8534 : 19 : outer_targs = DECL_TI_ARGS (decl);
8535 : 6886277 : type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8536 : : tf_warning_or_error, adc,
8537 : : outer_targs, flags);
8538 : 6886277 : if (type == error_mark_node)
8539 : : return;
8540 : 6885722 : if (TREE_CODE (type) == FUNCTION_TYPE)
8541 : : {
8542 : 3 : error ("initializer for %<decltype(auto) %D%> has function type; "
8543 : : "did you forget the %<()%>?", decl);
8544 : 3 : TREE_TYPE (decl) = error_mark_node;
8545 : 3 : return;
8546 : : }
8547 : : /* As in start_decl_1, complete so TREE_READONLY is set properly. */
8548 : 6885719 : if (!processing_template_decl
8549 : 1567917 : && !type_uses_auto (type)
8550 : 8453629 : && !COMPLETE_TYPE_P (complete_type (type)))
8551 : : {
8552 : 12 : error_at (location_of (decl),
8553 : : "deduced type %qT for %qD is incomplete", type, decl);
8554 : 12 : cxx_incomplete_type_inform (type);
8555 : 12 : TREE_TYPE (decl) = error_mark_node;
8556 : 12 : return;
8557 : : }
8558 : 6885707 : cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8559 : :
8560 : : /* Update the type of the corresponding TEMPLATE_DECL to match. */
8561 : 6885707 : if (DECL_LANG_SPECIFIC (decl)
8562 : 105645 : && DECL_TEMPLATE_INFO (decl)
8563 : 6888026 : && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl)
8564 : 2181 : TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type;
8565 : : }
8566 : :
8567 : 248931000 : if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8568 : : {
8569 : 48 : DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8570 : 48 : if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8571 : : {
8572 : 6 : init = NULL_TREE;
8573 : 6 : DECL_EXTERNAL (decl) = 1;
8574 : : }
8575 : : }
8576 : :
8577 : 248931000 : if (VAR_P (decl)
8578 : 86762027 : && DECL_CLASS_SCOPE_P (decl)
8579 : 13185502 : && verify_type_context (DECL_SOURCE_LOCATION (decl),
8580 : : TCTX_STATIC_STORAGE, type)
8581 : 262116502 : && DECL_INITIALIZED_IN_CLASS_P (decl))
8582 : 12133562 : check_static_variable_definition (decl, type);
8583 : :
8584 : 248931000 : if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8585 : : {
8586 : 18456928 : type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8587 : 18456928 : ? TCTX_THREAD_STORAGE
8588 : 18456928 : : TCTX_STATIC_STORAGE);
8589 : 18456928 : verify_type_context (input_location, context, TREE_TYPE (decl));
8590 : : }
8591 : :
8592 : 248931000 : if (init && TREE_CODE (decl) == FUNCTION_DECL)
8593 : : {
8594 : 386605 : tree clone;
8595 : 386605 : if (init == ridpointers[(int)RID_DELETE])
8596 : : {
8597 : : /* FIXME check this is 1st decl. */
8598 : 364966 : DECL_DELETED_FN (decl) = 1;
8599 : 364966 : DECL_DECLARED_INLINE_P (decl) = 1;
8600 : 364966 : DECL_INITIAL (decl) = error_mark_node;
8601 : 364984 : FOR_EACH_CLONE (clone, decl)
8602 : : {
8603 : 18 : DECL_DELETED_FN (clone) = 1;
8604 : 18 : DECL_DECLARED_INLINE_P (clone) = 1;
8605 : 18 : DECL_INITIAL (clone) = error_mark_node;
8606 : : }
8607 : 364966 : init = NULL_TREE;
8608 : : }
8609 : 21639 : else if (init == ridpointers[(int)RID_DEFAULT])
8610 : : {
8611 : 21623 : if (defaultable_fn_check (decl))
8612 : 21609 : DECL_DEFAULTED_FN (decl) = 1;
8613 : : else
8614 : 14 : DECL_INITIAL (decl) = NULL_TREE;
8615 : : }
8616 : : }
8617 : :
8618 : 248931000 : if (init && VAR_P (decl))
8619 : : {
8620 : 73389554 : DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8621 : : /* If DECL is a reference, then we want to know whether init is a
8622 : : reference constant; init_const_expr_p as passed tells us whether
8623 : : it's an rvalue constant. */
8624 : 73389554 : if (TYPE_REF_P (type))
8625 : 1932008 : init_const_expr_p = potential_constant_expression (init);
8626 : 73389554 : if (init_const_expr_p)
8627 : : {
8628 : : /* Set these flags now for templates. We'll update the flags in
8629 : : store_init_value for instantiations. */
8630 : 60520241 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8631 : 60520241 : if (decl_maybe_constant_var_p (decl)
8632 : : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8633 : 60520241 : && !TYPE_REF_P (type))
8634 : 20982581 : TREE_CONSTANT (decl) = true;
8635 : : }
8636 : : /* This is handled mostly by gimplify.cc, but we have to deal with
8637 : : not warning about int x = x; as it is a GCC extension to turn off
8638 : : this warning but only if warn_init_self is zero. */
8639 : 73389554 : if (!DECL_EXTERNAL (decl)
8640 : 71966514 : && !TREE_STATIC (decl)
8641 : 56187622 : && decl == tree_strip_any_location_wrapper (init)
8642 : 73393017 : && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
8643 : 3431 : suppress_warning (decl, OPT_Winit_self);
8644 : : }
8645 : 175541446 : else if (VAR_P (decl)
8646 : 13372473 : && COMPLETE_TYPE_P (type)
8647 : 10549057 : && !TYPE_REF_P (type)
8648 : 10548686 : && !dependent_type_p (type)
8649 : 185847027 : && is_really_empty_class (type, /*ignore_vptr*/false))
8650 : : /* We have no initializer but there's nothing to initialize anyway.
8651 : : Treat DECL as constant due to c++/109876. */
8652 : 446521 : TREE_CONSTANT (decl) = true;
8653 : :
8654 : 248931000 : if (flag_openmp
8655 : 236563 : && TREE_CODE (decl) == FUNCTION_DECL
8656 : : /* #pragma omp declare variant on methods handled in finish_struct
8657 : : instead. */
8658 : 249035516 : && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)
8659 : 25591 : || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8660 : 79133 : if (tree attr = lookup_attribute ("omp declare variant base",
8661 : 79133 : DECL_ATTRIBUTES (decl)))
8662 : 834 : omp_declare_variant_finalize (decl, attr);
8663 : :
8664 : 248931000 : if (processing_template_decl)
8665 : : {
8666 : 144961940 : bool type_dependent_p;
8667 : :
8668 : : /* Add this declaration to the statement-tree. */
8669 : 144961940 : if (at_function_scope_p ())
8670 : 53353364 : add_decl_expr (decl);
8671 : :
8672 : 144961940 : type_dependent_p = dependent_type_p (type);
8673 : :
8674 : 144961940 : if (check_for_bare_parameter_packs (init))
8675 : : {
8676 : 3 : init = NULL_TREE;
8677 : 3 : DECL_INITIAL (decl) = NULL_TREE;
8678 : : }
8679 : :
8680 : : /* Generally, initializers in templates are expanded when the
8681 : : template is instantiated. But, if DECL is a variable constant
8682 : : then it can be used in future constant expressions, so its value
8683 : : must be available. */
8684 : :
8685 : 144961940 : bool dep_init = false;
8686 : :
8687 : 144961940 : if (!VAR_P (decl) || type_dependent_p)
8688 : : /* We can't do anything if the decl has dependent type. */;
8689 : 18272224 : else if (!init && is_concept_var (decl))
8690 : : {
8691 : 2 : error ("variable concept has no initializer");
8692 : 2 : init = boolean_true_node;
8693 : : }
8694 : 18272222 : else if (init
8695 : 16721875 : && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8696 : 12612862 : && !TYPE_REF_P (type)
8697 : 12590248 : && decl_maybe_constant_var_p (decl)
8698 : 24786515 : && !(dep_init = value_dependent_init_p (init)))
8699 : : {
8700 : : /* This variable seems to be a non-dependent constant, so process
8701 : : its initializer. If check_initializer returns non-null the
8702 : : initialization wasn't constant after all. */
8703 : 1606487 : tree init_code;
8704 : 1606487 : cleanups = make_tree_vector ();
8705 : 1606487 : init_code = check_initializer (decl, init, flags, &cleanups);
8706 : 1606487 : if (init_code == NULL_TREE)
8707 : 1606487 : init = NULL_TREE;
8708 : 1606487 : release_tree_vector (cleanups);
8709 : : }
8710 : : else
8711 : : {
8712 : 16665735 : gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8713 : : /* Try to deduce array size. */
8714 : 16665735 : maybe_deduce_size_from_array_init (decl, init);
8715 : : /* And complain about multiple initializers. */
8716 : 15115388 : if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8717 : 16715862 : && !MAYBE_CLASS_TYPE_P (type))
8718 : 11 : init = build_x_compound_expr_from_list (init, ELK_INIT,
8719 : : tf_warning_or_error);
8720 : : }
8721 : :
8722 : 144961940 : if (init)
8723 : 42011808 : DECL_INITIAL (decl) = init;
8724 : :
8725 : 144961940 : if (dep_init)
8726 : : {
8727 : 4907806 : retrofit_lang_decl (decl);
8728 : 4907806 : SET_DECL_DEPENDENT_INIT_P (decl, true);
8729 : : }
8730 : :
8731 : 144961940 : if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8732 : : {
8733 : 4 : set_user_assembler_name (decl, asmspec);
8734 : 4 : DECL_HARD_REGISTER (decl) = 1;
8735 : : }
8736 : 144961940 : return;
8737 : : }
8738 : :
8739 : : /* Just store non-static data member initializers for later. */
8740 : 103969060 : if (init && TREE_CODE (decl) == FIELD_DECL)
8741 : 340173 : DECL_INITIAL (decl) = init;
8742 : :
8743 : : /* Take care of TYPE_DECLs up front. */
8744 : 103969060 : if (TREE_CODE (decl) == TYPE_DECL)
8745 : : {
8746 : 8489254 : if (type != error_mark_node
8747 : 8489254 : && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8748 : : {
8749 : 3991345 : if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8750 : 0 : warning (0, "shadowing previous type declaration of %q#D", decl);
8751 : 3991345 : set_identifier_type_value (DECL_NAME (decl), decl);
8752 : : }
8753 : :
8754 : : /* If we have installed this as the canonical typedef for this
8755 : : type, and that type has not been defined yet, delay emitting
8756 : : the debug information for it, as we will emit it later. */
8757 : 8489254 : if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8758 : 8489254 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8759 : 518516 : TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8760 : :
8761 : 8489254 : rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8762 : : at_eof);
8763 : 8489254 : return;
8764 : : }
8765 : :
8766 : : /* A reference will be modified here, as it is initialized. */
8767 : 95479806 : if (! DECL_EXTERNAL (decl)
8768 : 41208157 : && TREE_READONLY (decl)
8769 : 115266846 : && TYPE_REF_P (type))
8770 : : {
8771 : 204647 : was_readonly = 1;
8772 : 204647 : TREE_READONLY (decl) = 0;
8773 : : }
8774 : :
8775 : : /* This needs to happen before extend_ref_init_temps. */
8776 : 95479806 : if (VAR_OR_FUNCTION_DECL_P (decl))
8777 : : {
8778 : 87626235 : if (VAR_P (decl))
8779 : 39247499 : maybe_commonize_var (decl);
8780 : 87626235 : determine_visibility (decl);
8781 : : }
8782 : :
8783 : 95479806 : if (VAR_P (decl))
8784 : : {
8785 : 39247499 : duration_kind dk = decl_storage_duration (decl);
8786 : : /* [dcl.constinit]/1 "The constinit specifier shall be applied
8787 : : only to a declaration of a variable with static or thread storage
8788 : : duration." */
8789 : 39247499 : if (DECL_DECLARED_CONSTINIT_P (decl)
8790 : 39247499 : && !(dk == dk_thread || dk == dk_static))
8791 : : {
8792 : 4 : error_at (DECL_SOURCE_LOCATION (decl),
8793 : : "%<constinit%> can only be applied to a variable with "
8794 : : "static or thread storage duration");
8795 : 4 : return;
8796 : : }
8797 : :
8798 : 39247495 : if (decomp)
8799 : 26447 : cp_maybe_mangle_decomp (decl, decomp);
8800 : :
8801 : : /* If this is a local variable that will need a mangled name,
8802 : : register it now. We must do this before processing the
8803 : : initializer for the variable, since the initialization might
8804 : : require a guard variable, and since the mangled name of the
8805 : : guard variable will depend on the mangled name of this
8806 : : variable. */
8807 : 78494990 : if (DECL_FUNCTION_SCOPE_P (decl)
8808 : 20907903 : && TREE_STATIC (decl)
8809 : 39364831 : && !DECL_ARTIFICIAL (decl))
8810 : : {
8811 : : /* The variable holding an anonymous union will have had its
8812 : : discriminator set in finish_anon_union, after which it's
8813 : : NAME will have been cleared. */
8814 : 117105 : if (DECL_NAME (decl))
8815 : 117050 : determine_local_discriminator (decl);
8816 : : /* Normally has_forced_label_in_static is set during GIMPLE
8817 : : lowering, but [cd]tors are never actually compiled directly.
8818 : : We need to set this early so we can deal with the label
8819 : : address extension. */
8820 : 117105 : if ((DECL_CONSTRUCTOR_P (current_function_decl)
8821 : 117069 : || DECL_DESTRUCTOR_P (current_function_decl))
8822 : 117116 : && init)
8823 : : {
8824 : 36 : walk_tree (&init, notice_forced_label_r, NULL, NULL);
8825 : 36 : add_local_decl (cfun, decl);
8826 : : }
8827 : : /* And make sure it's in the symbol table for
8828 : : c_parse_final_cleanups to find. */
8829 : 117105 : varpool_node::get_create (decl);
8830 : : }
8831 : :
8832 : : /* Convert the initializer to the type of DECL, if we have not
8833 : : already initialized DECL. */
8834 : 39247495 : if (!DECL_INITIALIZED_P (decl)
8835 : : /* If !DECL_EXTERNAL then DECL is being defined. In the
8836 : : case of a static data member initialized inside the
8837 : : class-specifier, there can be an initializer even if DECL
8838 : : is *not* defined. */
8839 : 39247495 : && (!DECL_EXTERNAL (decl) || init))
8840 : : {
8841 : 34262453 : cleanups = make_tree_vector ();
8842 : 34262453 : init = check_initializer (decl, init, flags, &cleanups);
8843 : :
8844 : : /* Handle:
8845 : :
8846 : : [dcl.init]
8847 : :
8848 : : The memory occupied by any object of static storage
8849 : : duration is zero-initialized at program startup before
8850 : : any other initialization takes place.
8851 : :
8852 : : We cannot create an appropriate initializer until after
8853 : : the type of DECL is finalized. If DECL_INITIAL is set,
8854 : : then the DECL is statically initialized, and any
8855 : : necessary zero-initialization has already been performed. */
8856 : 34251669 : if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8857 : 383324 : DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8858 : : /*nelts=*/NULL_TREE,
8859 : : /*static_storage_p=*/true);
8860 : : /* Remember that the initialization for this variable has
8861 : : taken place. */
8862 : 34251669 : DECL_INITIALIZED_P (decl) = 1;
8863 : : /* This declaration is the definition of this variable,
8864 : : unless we are initializing a static data member within
8865 : : the class specifier. */
8866 : 34251669 : if (!DECL_EXTERNAL (decl))
8867 : 39236711 : var_definition_p = true;
8868 : : }
8869 : : /* If the variable has an array type, lay out the type, even if
8870 : : there is no initializer. It is valid to index through the
8871 : : array, and we must get TYPE_ALIGN set correctly on the array
8872 : : type. */
8873 : 4985042 : else if (TREE_CODE (type) == ARRAY_TYPE)
8874 : 208126 : layout_type (type);
8875 : :
8876 : 39236711 : if (TREE_STATIC (decl)
8877 : 17903092 : && !at_function_scope_p ()
8878 : 57022467 : && current_function_decl == NULL)
8879 : : /* So decl is a global variable or a static member of a
8880 : : non local class. Record the types it uses
8881 : : so that we can decide later to emit debug info for them. */
8882 : 17785740 : record_types_used_by_current_var_decl (decl);
8883 : : }
8884 : :
8885 : : /* Add this declaration to the statement-tree. This needs to happen
8886 : : after the call to check_initializer so that the DECL_EXPR for a
8887 : : reference temp is added before the DECL_EXPR for the reference itself. */
8888 : 95469018 : if (DECL_FUNCTION_SCOPE_P (decl))
8889 : : {
8890 : : /* If we're building a variable sized type, and we might be
8891 : : reachable other than via the top of the current binding
8892 : : level, then create a new BIND_EXPR so that we deallocate
8893 : : the object at the right time. */
8894 : 20907903 : if (VAR_P (decl)
8895 : 20907903 : && DECL_SIZE (decl)
8896 : 19411514 : && !TREE_CONSTANT (DECL_SIZE (decl))
8897 : 20909014 : && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8898 : : {
8899 : 4 : tree bind;
8900 : 4 : bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8901 : 4 : TREE_SIDE_EFFECTS (bind) = 1;
8902 : 4 : add_stmt (bind);
8903 : 4 : BIND_EXPR_BODY (bind) = push_stmt_list ();
8904 : : }
8905 : 20907903 : add_decl_expr (decl);
8906 : : }
8907 : :
8908 : : /* Let the middle end know about variables and functions -- but not
8909 : : static data members in uninstantiated class templates. */
8910 : 95469018 : if (VAR_OR_FUNCTION_DECL_P (decl))
8911 : : {
8912 : 87615447 : if (VAR_P (decl))
8913 : : {
8914 : 39236711 : layout_var_decl (decl);
8915 : 39236711 : if (!flag_weak)
8916 : : /* Check again now that we have an initializer. */
8917 : 58 : maybe_commonize_var (decl);
8918 : : /* A class-scope constexpr variable with an out-of-class declaration.
8919 : : C++17 makes them implicitly inline, but still force it out. */
8920 : 53476229 : if (DECL_INLINE_VAR_P (decl)
8921 : 12024064 : && !DECL_VAR_DECLARED_INLINE_P (decl)
8922 : 7783293 : && !DECL_TEMPLATE_INSTANTIATION (decl)
8923 : 24508242 : && !in_class_decl)
8924 : 54 : mark_needed (decl);
8925 : : }
8926 : :
8927 : 87615447 : if (var_definition_p
8928 : : /* With -fmerge-all-constants, gimplify_init_constructor
8929 : : might add TREE_STATIC to aggregate variables. */
8930 : 33249351 : && (TREE_STATIC (decl)
8931 : 20790567 : || (flag_merge_constants >= 2
8932 : 39 : && AGGREGATE_TYPE_P (type))))
8933 : : {
8934 : : /* If a TREE_READONLY variable needs initialization
8935 : : at runtime, it is no longer readonly and we need to
8936 : : avoid MEM_READONLY_P being set on RTL created for it. */
8937 : 12458788 : if (init)
8938 : : {
8939 : 12647 : if (TREE_READONLY (decl))
8940 : 520 : TREE_READONLY (decl) = 0;
8941 : : was_readonly = 0;
8942 : : }
8943 : 12446141 : else if (was_readonly)
8944 : 1356 : TREE_READONLY (decl) = 1;
8945 : :
8946 : : /* Likewise if it needs destruction. */
8947 : 12458788 : if (!decl_maybe_constant_destruction (decl, type))
8948 : 2974 : TREE_READONLY (decl) = 0;
8949 : : }
8950 : 75156659 : else if (VAR_P (decl)
8951 : 26777923 : && CP_DECL_THREAD_LOCAL_P (decl)
8952 : 18839 : && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
8953 : 18839 : && (was_readonly || TREE_READONLY (decl))
8954 : 75156671 : && var_needs_tls_wrapper (decl))
8955 : : {
8956 : : /* TLS variables need dynamic initialization by the TLS wrapper
8957 : : function, we don't want to hoist accesses to it before the
8958 : : wrapper. */
8959 : 6 : was_readonly = 0;
8960 : 6 : TREE_READONLY (decl) = 0;
8961 : : }
8962 : :
8963 : 87615447 : make_rtl_for_nonlocal_decl (decl, init, asmspec);
8964 : :
8965 : : /* Check for abstractness of the type. */
8966 : 87615447 : if (var_definition_p)
8967 : 33249351 : abstract_virtuals_error (decl, type);
8968 : :
8969 : 87615447 : if (TREE_TYPE (decl) == error_mark_node)
8970 : : /* No initialization required. */
8971 : : ;
8972 : 87615364 : else if (TREE_CODE (decl) == FUNCTION_DECL)
8973 : : {
8974 : 48378736 : if (init)
8975 : : {
8976 : 557 : if (init == ridpointers[(int)RID_DEFAULT])
8977 : : {
8978 : : /* An out-of-class default definition is defined at
8979 : : the point where it is explicitly defaulted. */
8980 : 541 : if (DECL_DELETED_FN (decl))
8981 : 6 : maybe_explain_implicit_delete (decl);
8982 : 535 : else if (DECL_INITIAL (decl) == error_mark_node)
8983 : 521 : synthesize_method (decl);
8984 : : }
8985 : : else
8986 : 32 : error_at (cp_expr_loc_or_loc (init,
8987 : 16 : DECL_SOURCE_LOCATION (decl)),
8988 : : "function %q#D is initialized like a variable",
8989 : : decl);
8990 : : }
8991 : : /* else no initialization required. */
8992 : : }
8993 : 39236628 : else if (DECL_EXTERNAL (decl)
8994 : 39236628 : && ! (DECL_LANG_SPECIFIC (decl)
8995 : 5687313 : && DECL_NOT_REALLY_EXTERN (decl)))
8996 : : {
8997 : : /* check_initializer will have done any constant initialization. */
8998 : : }
8999 : : /* A variable definition. */
9000 : 34958294 : else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
9001 : : /* Initialize the local variable. */
9002 : 20790547 : initialize_local_var (decl, init);
9003 : :
9004 : : /* If a variable is defined, and then a subsequent
9005 : : definition with external linkage is encountered, we will
9006 : : get here twice for the same variable. We want to avoid
9007 : : calling expand_static_init more than once. For variables
9008 : : that are not static data members, we can call
9009 : : expand_static_init only when we actually process the
9010 : : initializer. It is not legal to redeclare a static data
9011 : : member, so this issue does not arise in that case. */
9012 : 14167747 : else if (var_definition_p && TREE_STATIC (decl))
9013 : 12458734 : expand_static_init (decl, init);
9014 : : }
9015 : :
9016 : : /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9017 : : reference, insert it in the statement-tree now. */
9018 : 95469018 : if (cleanups)
9019 : : {
9020 : 34256200 : for (tree t : *cleanups)
9021 : : {
9022 : 4531 : push_cleanup (NULL_TREE, t, false);
9023 : : /* As in initialize_local_var. */
9024 : 4531 : wrap_temporary_cleanups (init, t);
9025 : : }
9026 : 34251669 : release_tree_vector (cleanups);
9027 : : }
9028 : :
9029 : 95469018 : if (was_readonly)
9030 : 204188 : TREE_READONLY (decl) = 1;
9031 : :
9032 : 95469018 : if (flag_openmp
9033 : 146946 : && VAR_P (decl)
9034 : 95516253 : && lookup_attribute ("omp declare target implicit",
9035 : 47235 : DECL_ATTRIBUTES (decl)))
9036 : : {
9037 : 65 : DECL_ATTRIBUTES (decl)
9038 : 65 : = remove_attribute ("omp declare target implicit",
9039 : 65 : DECL_ATTRIBUTES (decl));
9040 : 65 : complete_type (TREE_TYPE (decl));
9041 : 65 : if (!omp_mappable_type (TREE_TYPE (decl)))
9042 : : {
9043 : 40 : error ("%q+D in declare target directive does not have mappable"
9044 : : " type", decl);
9045 : 40 : if (TREE_TYPE (decl) != error_mark_node
9046 : 40 : && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
9047 : 40 : cxx_incomplete_type_inform (TREE_TYPE (decl));
9048 : : }
9049 : 25 : else if (!lookup_attribute ("omp declare target",
9050 : 25 : DECL_ATTRIBUTES (decl))
9051 : 50 : && !lookup_attribute ("omp declare target link",
9052 : 25 : DECL_ATTRIBUTES (decl)))
9053 : : {
9054 : 25 : DECL_ATTRIBUTES (decl)
9055 : 25 : = tree_cons (get_identifier ("omp declare target"),
9056 : 25 : NULL_TREE, DECL_ATTRIBUTES (decl));
9057 : 25 : symtab_node *node = symtab_node::get (decl);
9058 : 25 : if (node != NULL)
9059 : : {
9060 : 21 : node->offloadable = 1;
9061 : 21 : if (ENABLE_OFFLOADING)
9062 : : {
9063 : : g->have_offload = true;
9064 : : if (is_a <varpool_node *> (node))
9065 : : vec_safe_push (offload_vars, decl);
9066 : : }
9067 : : }
9068 : : }
9069 : : }
9070 : :
9071 : : /* This is the last point we can lower alignment so give the target the
9072 : : chance to do so. */
9073 : 95469018 : if (VAR_P (decl)
9074 : 39236711 : && !is_global_var (decl)
9075 : 116259585 : && !DECL_HARD_REGISTER (decl))
9076 : 20790507 : targetm.lower_local_decl_alignment (decl);
9077 : :
9078 : 95469018 : invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
9079 : : }
9080 : :
9081 : : /* For class TYPE return itself or some its bases that contain
9082 : : any direct non-static data members. Return error_mark_node if an
9083 : : error has been diagnosed. */
9084 : :
9085 : : static tree
9086 : 16020 : find_decomp_class_base (location_t loc, tree type, tree ret)
9087 : : {
9088 : 16020 : bool member_seen = false;
9089 : 213581 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9090 : 362950 : if (TREE_CODE (field) != FIELD_DECL
9091 : 32317 : || DECL_ARTIFICIAL (field)
9092 : 229820 : || DECL_UNNAMED_BIT_FIELD (field))
9093 : 165368 : continue;
9094 : 32214 : else if (ret)
9095 : : return type;
9096 : 32197 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9097 : : {
9098 : 2 : if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
9099 : 1 : error_at (loc, "cannot decompose class type %qT because it has an "
9100 : : "anonymous struct member", type);
9101 : : else
9102 : 1 : error_at (loc, "cannot decompose class type %qT because it has an "
9103 : : "anonymous union member", type);
9104 : 2 : inform (DECL_SOURCE_LOCATION (field), "declared here");
9105 : 2 : return error_mark_node;
9106 : : }
9107 : 32195 : else if (!accessible_p (type, field, true))
9108 : : {
9109 : 2 : error_at (loc, "cannot decompose inaccessible member %qD of %qT",
9110 : : field, type);
9111 : 2 : inform (DECL_SOURCE_LOCATION (field),
9112 : 2 : TREE_PRIVATE (field)
9113 : : ? G_("declared private here")
9114 : : : G_("declared protected here"));
9115 : 2 : return error_mark_node;
9116 : : }
9117 : : else
9118 : : member_seen = true;
9119 : :
9120 : 15999 : tree base_binfo, binfo;
9121 : 15999 : tree orig_ret = ret;
9122 : 15999 : int i;
9123 : 15999 : if (member_seen)
9124 : 15954 : ret = type;
9125 : 16053 : for (binfo = TYPE_BINFO (type), i = 0;
9126 : 16053 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9127 : : {
9128 : 62 : tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
9129 : 62 : if (t == error_mark_node)
9130 : 0 : return error_mark_node;
9131 : 62 : if (t != NULL_TREE && t != ret)
9132 : : {
9133 : 35 : if (ret == type)
9134 : : {
9135 : 4 : error_at (loc, "cannot decompose class type %qT: both it and "
9136 : : "its base class %qT have non-static data members",
9137 : : type, t);
9138 : 4 : return error_mark_node;
9139 : : }
9140 : 31 : else if (orig_ret != NULL_TREE)
9141 : 0 : return t;
9142 : 31 : else if (ret != NULL_TREE)
9143 : : {
9144 : 4 : error_at (loc, "cannot decompose class type %qT: its base "
9145 : : "classes %qT and %qT have non-static data "
9146 : : "members", type, ret, t);
9147 : 4 : return error_mark_node;
9148 : : }
9149 : : else
9150 : : ret = t;
9151 : : }
9152 : : }
9153 : : return ret;
9154 : : }
9155 : :
9156 : : /* Return std::tuple_size<TYPE>::value. */
9157 : :
9158 : : static tree
9159 : 32241 : get_tuple_size (tree type)
9160 : : {
9161 : 32241 : tree args = make_tree_vec (1);
9162 : 32241 : TREE_VEC_ELT (args, 0) = type;
9163 : 32241 : tree inst = lookup_template_class (tuple_size_identifier, args,
9164 : : /*in_decl*/NULL_TREE,
9165 : : /*context*/std_node,
9166 : : /*entering_scope*/false, tf_none);
9167 : 32241 : inst = complete_type (inst);
9168 : 32241 : if (inst == error_mark_node
9169 : 31868 : || !COMPLETE_TYPE_P (inst)
9170 : 48506 : || !CLASS_TYPE_P (type))
9171 : : return NULL_TREE;
9172 : 16265 : tree val = lookup_qualified_name (inst, value_identifier,
9173 : : LOOK_want::NORMAL, /*complain*/false);
9174 : 16265 : if (val == error_mark_node)
9175 : : return NULL_TREE;
9176 : 16261 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
9177 : 16261 : val = maybe_constant_value (val);
9178 : 16261 : if (TREE_CODE (val) == INTEGER_CST)
9179 : : return val;
9180 : : else
9181 : 0 : return error_mark_node;
9182 : : }
9183 : :
9184 : : /* Return std::tuple_element<I,TYPE>::type. */
9185 : :
9186 : : static tree
9187 : 32507 : get_tuple_element_type (tree type, unsigned i)
9188 : : {
9189 : 32507 : tree args = make_tree_vec (2);
9190 : 32507 : TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
9191 : 32507 : TREE_VEC_ELT (args, 1) = type;
9192 : 32507 : tree inst = lookup_template_class (tuple_element_identifier, args,
9193 : : /*in_decl*/NULL_TREE,
9194 : : /*context*/std_node,
9195 : : /*entering_scope*/false,
9196 : : tf_warning_or_error);
9197 : 32507 : return make_typename_type (inst, type_identifier,
9198 : 32507 : none_type, tf_warning_or_error);
9199 : : }
9200 : :
9201 : : /* Return e.get<i>() or get<i>(e). */
9202 : :
9203 : : static tree
9204 : 32513 : get_tuple_decomp_init (tree decl, unsigned i)
9205 : : {
9206 : 32513 : tree targs = make_tree_vec (1);
9207 : 32513 : TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
9208 : :
9209 : 32513 : tree etype = TREE_TYPE (decl);
9210 : 32513 : tree e = convert_from_reference (decl);
9211 : :
9212 : : /* [The id-expression] e is an lvalue if the type of the entity e is an
9213 : : lvalue reference and an xvalue otherwise. */
9214 : 32513 : if (!TYPE_REF_P (etype)
9215 : 32513 : || TYPE_REF_IS_RVALUE (etype))
9216 : 32421 : e = move (e);
9217 : :
9218 : 32513 : tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
9219 : : LOOK_want::NORMAL, /*complain*/false);
9220 : 32513 : bool use_member_get = false;
9221 : :
9222 : : /* To use a member get, member lookup must find at least one
9223 : : declaration that is a function template
9224 : : whose first template parameter is a non-type parameter. */
9225 : 97253 : for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
9226 : : {
9227 : 32513 : tree fn = *iter;
9228 : 32513 : if (TREE_CODE (fn) == TEMPLATE_DECL)
9229 : : {
9230 : 288 : tree tparms = DECL_TEMPLATE_PARMS (fn);
9231 : 288 : tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
9232 : 288 : if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
9233 : : {
9234 : : use_member_get = true;
9235 : : break;
9236 : : }
9237 : : }
9238 : : }
9239 : :
9240 : 32513 : if (use_member_get)
9241 : : {
9242 : 286 : fns = lookup_template_function (fns, targs);
9243 : 286 : return build_new_method_call (e, fns, /*args*/NULL,
9244 : : /*path*/NULL_TREE, LOOKUP_NORMAL,
9245 : 286 : /*fn_p*/NULL, tf_warning_or_error);
9246 : : }
9247 : : else
9248 : : {
9249 : 32227 : releasing_vec args (make_tree_vector_single (e));
9250 : 32227 : fns = lookup_template_function (get__identifier, targs);
9251 : 32227 : fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
9252 : 32227 : return finish_call_expr (fns, &args, /*novirt*/false,
9253 : : /*koenig*/true, tf_warning_or_error);
9254 : 32227 : }
9255 : : }
9256 : :
9257 : : /* It's impossible to recover the decltype of a tuple decomposition variable
9258 : : based on the actual type of the variable, so store it in a hash table. */
9259 : :
9260 : : static GTY((cache)) decl_tree_cache_map *decomp_type_table;
9261 : :
9262 : : tree
9263 : 41 : lookup_decomp_type (tree v)
9264 : : {
9265 : 41 : if (tree *slot = decomp_type_table->get (v))
9266 : 41 : return *slot;
9267 : : return NULL_TREE;
9268 : : }
9269 : :
9270 : : /* Mangle a decomposition declaration if needed. Arguments like
9271 : : in cp_finish_decomp. */
9272 : :
9273 : : static void
9274 : 26447 : cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp)
9275 : : {
9276 : 26447 : if (!processing_template_decl
9277 : 26447 : && !error_operand_p (decl)
9278 : 52894 : && TREE_STATIC (decl))
9279 : : {
9280 : 190 : auto_vec<tree, 16> v;
9281 : 190 : v.safe_grow (decomp->count, true);
9282 : 190 : tree d = decomp->decl;
9283 : 636 : for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d))
9284 : 446 : v[decomp->count - i - 1] = d;
9285 : 190 : if (DECL_FUNCTION_SCOPE_P (decl))
9286 : : {
9287 : : size_t sz = 3;
9288 : 226 : for (unsigned int i = 0; i < decomp->count; ++i)
9289 : 155 : sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1;
9290 : 71 : char *name = XALLOCAVEC (char, sz);
9291 : 71 : name[0] = 'D';
9292 : 71 : name[1] = 'C';
9293 : 71 : char *p = name + 2;
9294 : 226 : for (unsigned int i = 0; i < decomp->count; ++i)
9295 : : {
9296 : 155 : size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i]));
9297 : 155 : *p++ = ' ';
9298 : 155 : memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len);
9299 : 155 : p += len;
9300 : : }
9301 : 71 : *p = '\0';
9302 : 71 : determine_local_discriminator (decl, get_identifier (name));
9303 : : }
9304 : 190 : SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
9305 : 190 : maybe_apply_pragma_weak (decl);
9306 : 190 : }
9307 : 26447 : }
9308 : :
9309 : : /* Finish a decomposition declaration. DECL is the underlying declaration
9310 : : "e", FIRST is the head of a chain of decls for the individual identifiers
9311 : : chained through DECL_CHAIN in reverse order and COUNT is the number of
9312 : : those decls. */
9313 : :
9314 : : void
9315 : 78439 : cp_finish_decomp (tree decl, cp_decomp *decomp)
9316 : : {
9317 : 78439 : tree first = decomp->decl;
9318 : 78439 : unsigned count = decomp->count;
9319 : 78439 : if (error_operand_p (decl))
9320 : : {
9321 : 31 : error_out:
9322 : 276 : while (count--)
9323 : : {
9324 : 169 : TREE_TYPE (first) = error_mark_node;
9325 : 169 : if (DECL_HAS_VALUE_EXPR_P (first))
9326 : : {
9327 : 9 : SET_DECL_VALUE_EXPR (first, NULL_TREE);
9328 : 9 : DECL_HAS_VALUE_EXPR_P (first) = 0;
9329 : : }
9330 : 169 : first = DECL_CHAIN (first);
9331 : : }
9332 : 107 : if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
9333 : 43 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
9334 : 46015 : return;
9335 : : }
9336 : :
9337 : 78408 : location_t loc = DECL_SOURCE_LOCATION (decl);
9338 : 78408 : if (type_dependent_expression_p (decl)
9339 : : /* This happens for range for when not in templates.
9340 : : Still add the DECL_VALUE_EXPRs for later processing. */
9341 : 78408 : || (!processing_template_decl
9342 : 26501 : && type_uses_auto (TREE_TYPE (decl))))
9343 : : {
9344 : 137845 : for (unsigned int i = 0; i < count; i++)
9345 : : {
9346 : 91937 : if (!DECL_HAS_VALUE_EXPR_P (first))
9347 : : {
9348 : 91937 : tree v = build_nt (ARRAY_REF, decl,
9349 : 91937 : size_int (count - i - 1),
9350 : : NULL_TREE, NULL_TREE);
9351 : 91937 : SET_DECL_VALUE_EXPR (first, v);
9352 : 91937 : DECL_HAS_VALUE_EXPR_P (first) = 1;
9353 : : }
9354 : 91937 : if (processing_template_decl)
9355 : 91722 : fit_decomposition_lang_decl (first, decl);
9356 : 91937 : first = DECL_CHAIN (first);
9357 : : }
9358 : : return;
9359 : : }
9360 : :
9361 : 64924 : auto_vec<tree, 16> v;
9362 : 32500 : v.safe_grow (count, true);
9363 : 32500 : tree d = first;
9364 : 97922 : for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
9365 : : {
9366 : 65422 : v[count - i - 1] = d;
9367 : 65422 : fit_decomposition_lang_decl (d, decl);
9368 : : }
9369 : :
9370 : 32500 : tree type = TREE_TYPE (decl);
9371 : 32500 : tree dexp = decl;
9372 : :
9373 : 32500 : if (TYPE_REF_P (type))
9374 : : {
9375 : 281 : dexp = convert_from_reference (dexp);
9376 : 281 : type = complete_type (TREE_TYPE (type));
9377 : 281 : if (type == error_mark_node)
9378 : 76 : goto error_out;
9379 : 281 : if (!COMPLETE_TYPE_P (type))
9380 : : {
9381 : 3 : error_at (loc, "structured binding refers to incomplete type %qT",
9382 : : type);
9383 : 3 : goto error_out;
9384 : : }
9385 : : }
9386 : :
9387 : 32497 : tree eltype = NULL_TREE;
9388 : 32497 : unsigned HOST_WIDE_INT eltscnt = 0;
9389 : 32497 : if (TREE_CODE (type) == ARRAY_TYPE)
9390 : : {
9391 : 238 : tree nelts;
9392 : 238 : nelts = array_type_nelts_top (type);
9393 : 238 : if (nelts == error_mark_node)
9394 : 0 : goto error_out;
9395 : 238 : if (!tree_fits_uhwi_p (nelts))
9396 : : {
9397 : 0 : error_at (loc, "cannot decompose variable length array %qT", type);
9398 : 0 : goto error_out;
9399 : : }
9400 : 238 : eltscnt = tree_to_uhwi (nelts);
9401 : 238 : if (count != eltscnt)
9402 : : {
9403 : 8 : cnt_mismatch:
9404 : 15 : if (count > eltscnt)
9405 : 9 : error_n (loc, count,
9406 : : "%u name provided for structured binding",
9407 : : "%u names provided for structured binding", count);
9408 : : else
9409 : 6 : error_n (loc, count,
9410 : : "only %u name provided for structured binding",
9411 : : "only %u names provided for structured binding", count);
9412 : 15 : inform_n (loc, eltscnt,
9413 : : "while %qT decomposes into %wu element",
9414 : : "while %qT decomposes into %wu elements",
9415 : : type, eltscnt);
9416 : 15 : goto error_out;
9417 : : }
9418 : 230 : eltype = TREE_TYPE (type);
9419 : 814 : for (unsigned int i = 0; i < count; i++)
9420 : : {
9421 : 584 : TREE_TYPE (v[i]) = eltype;
9422 : 584 : layout_decl (v[i], 0);
9423 : 584 : if (processing_template_decl)
9424 : 52 : continue;
9425 : 532 : tree t = unshare_expr (dexp);
9426 : 532 : t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9427 : 532 : SET_DECL_VALUE_EXPR (v[i], t);
9428 : 532 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9429 : : }
9430 : : }
9431 : : /* 2 GNU extensions. */
9432 : 32259 : else if (TREE_CODE (type) == COMPLEX_TYPE)
9433 : : {
9434 : 12 : eltscnt = 2;
9435 : 12 : if (count != eltscnt)
9436 : 0 : goto cnt_mismatch;
9437 : 12 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9438 : 36 : for (unsigned int i = 0; i < count; i++)
9439 : : {
9440 : 24 : TREE_TYPE (v[i]) = eltype;
9441 : 24 : layout_decl (v[i], 0);
9442 : 24 : if (processing_template_decl)
9443 : 0 : continue;
9444 : 24 : tree t = unshare_expr (dexp);
9445 : 36 : t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t);
9446 : 24 : SET_DECL_VALUE_EXPR (v[i], t);
9447 : 24 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9448 : : }
9449 : : }
9450 : 32247 : else if (TREE_CODE (type) == VECTOR_TYPE)
9451 : : {
9452 : 6 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9453 : : {
9454 : : error_at (loc, "cannot decompose variable length vector %qT", type);
9455 : : goto error_out;
9456 : : }
9457 : 6 : if (count != eltscnt)
9458 : 0 : goto cnt_mismatch;
9459 : 6 : eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9460 : 30 : for (unsigned int i = 0; i < count; i++)
9461 : : {
9462 : 24 : TREE_TYPE (v[i]) = eltype;
9463 : 24 : layout_decl (v[i], 0);
9464 : 24 : if (processing_template_decl)
9465 : 0 : continue;
9466 : 24 : tree t = unshare_expr (dexp);
9467 : 24 : convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9468 : : &t, size_int (i));
9469 : 24 : t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE);
9470 : 24 : SET_DECL_VALUE_EXPR (v[i], t);
9471 : 24 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9472 : : }
9473 : : }
9474 : 32241 : else if (tree tsize = get_tuple_size (type))
9475 : : {
9476 : 16261 : if (tsize == error_mark_node)
9477 : : {
9478 : 0 : error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9479 : : "constant expression", type);
9480 : 0 : goto error_out;
9481 : : }
9482 : 16261 : if (!tree_fits_uhwi_p (tsize))
9483 : : {
9484 : 12 : error_n (loc, count,
9485 : : "%u name provided for structured binding",
9486 : : "%u names provided for structured binding", count);
9487 : 12 : inform (loc, "while %qT decomposes into %E elements",
9488 : : type, tsize);
9489 : 12 : goto error_out;
9490 : : }
9491 : 16249 : eltscnt = tree_to_uhwi (tsize);
9492 : 16249 : if (count != eltscnt)
9493 : 2 : goto cnt_mismatch;
9494 : 16247 : int save_read = DECL_READ_P (decl);
9495 : 48746 : for (unsigned i = 0; i < count; ++i)
9496 : : {
9497 : 32513 : location_t sloc = input_location;
9498 : 32513 : location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9499 : :
9500 : 32513 : input_location = dloc;
9501 : 32513 : tree init = get_tuple_decomp_init (decl, i);
9502 : 32513 : tree eltype = (init == error_mark_node ? error_mark_node
9503 : 32507 : : get_tuple_element_type (type, i));
9504 : 32513 : input_location = sloc;
9505 : :
9506 : 32513 : if (VOID_TYPE_P (eltype))
9507 : : {
9508 : 2 : error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9509 : : i, type);
9510 : 2 : eltype = error_mark_node;
9511 : : }
9512 : 32513 : if (init == error_mark_node || eltype == error_mark_node)
9513 : : {
9514 : 14 : inform (dloc, "in initialization of structured binding "
9515 : 14 : "variable %qD", v[i]);
9516 : 14 : goto error_out;
9517 : : }
9518 : : /* Save the decltype away before reference collapse. */
9519 : 32499 : hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9520 : 32499 : eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9521 : 32499 : TREE_TYPE (v[i]) = eltype;
9522 : 32499 : layout_decl (v[i], 0);
9523 : 32499 : if (DECL_HAS_VALUE_EXPR_P (v[i]))
9524 : : {
9525 : : /* In this case the names are variables, not just proxies. */
9526 : 178 : SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9527 : 178 : DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9528 : : }
9529 : 32499 : if (!processing_template_decl)
9530 : : {
9531 : 28411 : copy_linkage (v[i], decl);
9532 : 28411 : cp_finish_decl (v[i], init, /*constexpr*/false,
9533 : : /*asm*/NULL_TREE, LOOKUP_NORMAL);
9534 : : }
9535 : : }
9536 : : /* Ignore reads from the underlying decl performed during initialization
9537 : : of the individual variables. If those will be read, we'll mark
9538 : : the underlying decl as read at that point. */
9539 : 16233 : DECL_READ_P (decl) = save_read;
9540 : : }
9541 : 15980 : else if (TREE_CODE (type) == UNION_TYPE)
9542 : : {
9543 : 0 : error_at (loc, "cannot decompose union type %qT", type);
9544 : 0 : goto error_out;
9545 : : }
9546 : 15980 : else if (!CLASS_TYPE_P (type))
9547 : : {
9548 : 14 : error_at (loc, "cannot decompose non-array non-class type %qT", type);
9549 : 14 : goto error_out;
9550 : : }
9551 : 31932 : else if (LAMBDA_TYPE_P (type))
9552 : : {
9553 : 6 : error_at (loc, "cannot decompose lambda closure type %qT", type);
9554 : 6 : goto error_out;
9555 : : }
9556 : 15960 : else if (processing_template_decl && complete_type (type) == error_mark_node)
9557 : 0 : goto error_out;
9558 : 15960 : else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9559 : 2 : pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9560 : : type);
9561 : : else
9562 : : {
9563 : 15958 : tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9564 : 15958 : if (btype == error_mark_node)
9565 : 12 : goto error_out;
9566 : 15946 : else if (btype == NULL_TREE)
9567 : : {
9568 : 0 : error_at (loc, "cannot decompose class type %qT without non-static "
9569 : : "data members", type);
9570 : 0 : goto error_out;
9571 : : }
9572 : 212481 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9573 : 360888 : if (TREE_CODE (field) != FIELD_DECL
9574 : 32199 : || DECL_ARTIFICIAL (field)
9575 : 228729 : || DECL_UNNAMED_BIT_FIELD (field))
9576 : 164353 : continue;
9577 : : else
9578 : 32182 : eltscnt++;
9579 : 15946 : if (count != eltscnt)
9580 : 5 : goto cnt_mismatch;
9581 : 15941 : tree t = dexp;
9582 : 15941 : if (type != btype)
9583 : : {
9584 : 17 : t = convert_to_base (t, btype, /*check_access*/true,
9585 : : /*nonnull*/false, tf_warning_or_error);
9586 : 17 : type = btype;
9587 : : }
9588 : 15941 : unsigned int i = 0;
9589 : 212400 : for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9590 : 360747 : if (TREE_CODE (field) != FIELD_DECL
9591 : 32188 : || DECL_ARTIFICIAL (field)
9592 : 228642 : || DECL_UNNAMED_BIT_FIELD (field))
9593 : 164288 : continue;
9594 : : else
9595 : : {
9596 : 32171 : tree tt = finish_non_static_data_member (field, unshare_expr (t),
9597 : : NULL_TREE);
9598 : 32171 : if (REFERENCE_REF_P (tt))
9599 : 19 : tt = TREE_OPERAND (tt, 0);
9600 : 32171 : TREE_TYPE (v[i]) = TREE_TYPE (tt);
9601 : 32171 : layout_decl (v[i], 0);
9602 : 32171 : if (!processing_template_decl)
9603 : : {
9604 : 24169 : SET_DECL_VALUE_EXPR (v[i], tt);
9605 : 24169 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9606 : : }
9607 : 32171 : i++;
9608 : : }
9609 : : }
9610 : 32424 : if (processing_template_decl)
9611 : : {
9612 : 18193 : for (unsigned int i = 0; i < count; i++)
9613 : 12144 : if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9614 : : {
9615 : 12020 : tree a = build_nt (ARRAY_REF, decl, size_int (i),
9616 : : NULL_TREE, NULL_TREE);
9617 : 12020 : SET_DECL_VALUE_EXPR (v[i], a);
9618 : 12020 : DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9619 : : }
9620 : : }
9621 : : }
9622 : :
9623 : : /* Returns a declaration for a VAR_DECL as if:
9624 : :
9625 : : extern "C" TYPE NAME;
9626 : :
9627 : : had been seen. Used to create compiler-generated global
9628 : : variables. */
9629 : :
9630 : : static tree
9631 : 2003 : declare_global_var (tree name, tree type)
9632 : : {
9633 : 2003 : auto cookie = push_abi_namespace (global_namespace);
9634 : 2003 : tree decl = build_decl (input_location, VAR_DECL, name, type);
9635 : 2003 : TREE_PUBLIC (decl) = 1;
9636 : 2003 : DECL_EXTERNAL (decl) = 1;
9637 : 2003 : DECL_ARTIFICIAL (decl) = 1;
9638 : 2003 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9639 : : /* If the user has explicitly declared this variable (perhaps
9640 : : because the code we are compiling is part of a low-level runtime
9641 : : library), then it is possible that our declaration will be merged
9642 : : with theirs by pushdecl. */
9643 : 2003 : decl = pushdecl (decl);
9644 : 2003 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9645 : 2003 : pop_abi_namespace (cookie, global_namespace);
9646 : :
9647 : 2003 : return decl;
9648 : : }
9649 : :
9650 : : /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
9651 : : if "__cxa_atexit" is not being used) corresponding to the function
9652 : : to be called when the program exits. */
9653 : :
9654 : : static tree
9655 : 4990 : get_atexit_fn_ptr_type (void)
9656 : : {
9657 : 4990 : tree fn_type;
9658 : :
9659 : 4990 : if (!atexit_fn_ptr_type_node)
9660 : : {
9661 : 2015 : tree arg_type;
9662 : 2015 : if (flag_use_cxa_atexit
9663 : 2015 : && !targetm.cxx.use_atexit_for_cxa_atexit ())
9664 : : /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
9665 : 2003 : arg_type = ptr_type_node;
9666 : : else
9667 : : /* The parameter to "atexit" is "void (*)(void)". */
9668 : : arg_type = NULL_TREE;
9669 : :
9670 : 2015 : fn_type = build_function_type_list (void_type_node,
9671 : : arg_type, NULL_TREE);
9672 : 2015 : atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9673 : : }
9674 : :
9675 : 4990 : return atexit_fn_ptr_type_node;
9676 : : }
9677 : :
9678 : : /* Returns a pointer to the `atexit' function. Note that if
9679 : : FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9680 : : `__cxa_atexit' function specified in the IA64 C++ ABI. */
9681 : :
9682 : : static tree
9683 : 2908 : get_atexit_node (void)
9684 : : {
9685 : 2908 : tree atexit_fndecl;
9686 : 2908 : tree fn_type;
9687 : 2908 : tree fn_ptr_type;
9688 : 2908 : const char *name;
9689 : 2908 : bool use_aeabi_atexit;
9690 : 2908 : tree ctx = global_namespace;
9691 : :
9692 : 2908 : if (atexit_node)
9693 : : return atexit_node;
9694 : :
9695 : 1978 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9696 : : {
9697 : : /* The declaration for `__cxa_atexit' is:
9698 : :
9699 : : int __cxa_atexit (void (*)(void *), void *, void *)
9700 : :
9701 : : We build up the argument types and then the function type
9702 : : itself. */
9703 : 1966 : tree argtype0, argtype1, argtype2;
9704 : :
9705 : 1966 : use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9706 : : /* First, build the pointer-to-function type for the first
9707 : : argument. */
9708 : 1966 : fn_ptr_type = get_atexit_fn_ptr_type ();
9709 : : /* Then, build the rest of the argument types. */
9710 : 1966 : argtype2 = ptr_type_node;
9711 : 1966 : if (use_aeabi_atexit)
9712 : : {
9713 : : argtype1 = fn_ptr_type;
9714 : : argtype0 = ptr_type_node;
9715 : : }
9716 : : else
9717 : : {
9718 : 1966 : argtype1 = ptr_type_node;
9719 : 1966 : argtype0 = fn_ptr_type;
9720 : : }
9721 : : /* And the final __cxa_atexit type. */
9722 : 1966 : fn_type = build_function_type_list (integer_type_node,
9723 : : argtype0, argtype1, argtype2,
9724 : : NULL_TREE);
9725 : : /* ... which needs noexcept. */
9726 : 1966 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9727 : 1966 : if (use_aeabi_atexit)
9728 : : {
9729 : 0 : name = "__aeabi_atexit";
9730 : 0 : push_to_top_level ();
9731 : 0 : int n = push_namespace (get_identifier ("__aeabiv1"), false);
9732 : 0 : ctx = current_namespace;
9733 : 0 : while (n--)
9734 : 0 : pop_namespace ();
9735 : 0 : pop_from_top_level ();
9736 : : }
9737 : : else
9738 : : {
9739 : 1966 : name = "__cxa_atexit";
9740 : 1966 : ctx = abi_node;
9741 : : }
9742 : : }
9743 : : else
9744 : : {
9745 : : /* The declaration for `atexit' is:
9746 : :
9747 : : int atexit (void (*)());
9748 : :
9749 : : We build up the argument types and then the function type
9750 : : itself. */
9751 : 12 : fn_ptr_type = get_atexit_fn_ptr_type ();
9752 : : /* Build the final atexit type. */
9753 : 12 : fn_type = build_function_type_list (integer_type_node,
9754 : : fn_ptr_type, NULL_TREE);
9755 : : /* ... which needs noexcept. */
9756 : 12 : fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9757 : 12 : name = "atexit";
9758 : : }
9759 : :
9760 : : /* Now, build the function declaration. */
9761 : 1978 : push_lang_context (lang_name_c);
9762 : 1978 : auto cookie = push_abi_namespace (ctx);
9763 : 1978 : atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9764 : 1978 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9765 : : /* Install as hidden builtin so we're (a) more relaxed about
9766 : : exception spec matching and (b) will not give a confusing location
9767 : : in diagnostic and (c) won't magically appear in user-visible name
9768 : : lookups. */
9769 : 1978 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9770 : 1978 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9771 : 1978 : pop_abi_namespace (cookie, ctx);
9772 : 1978 : mark_used (atexit_fndecl);
9773 : 1978 : pop_lang_context ();
9774 : 1978 : atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9775 : :
9776 : 1978 : return atexit_node;
9777 : : }
9778 : :
9779 : : /* Like get_atexit_node, but for thread-local cleanups. */
9780 : :
9781 : : static tree
9782 : 57 : get_thread_atexit_node (void)
9783 : : {
9784 : 57 : if (thread_atexit_node)
9785 : : return thread_atexit_node;
9786 : :
9787 : : /* The declaration for `__cxa_thread_atexit' is:
9788 : :
9789 : : int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9790 : 47 : tree fn_type = build_function_type_list (integer_type_node,
9791 : : get_atexit_fn_ptr_type (),
9792 : : ptr_type_node, ptr_type_node,
9793 : : NULL_TREE);
9794 : :
9795 : : /* Now, build the function declaration, as with __cxa_atexit. */
9796 : 47 : unsigned flags = push_abi_namespace ();
9797 : 47 : tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9798 : : ECF_LEAF | ECF_NOTHROW);
9799 : 47 : DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9800 : 47 : DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9801 : 47 : atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9802 : 47 : pop_abi_namespace (flags);
9803 : 47 : mark_used (atexit_fndecl);
9804 : 47 : thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9805 : :
9806 : 47 : return thread_atexit_node;
9807 : : }
9808 : :
9809 : : /* Returns the __dso_handle VAR_DECL. */
9810 : :
9811 : : static tree
9812 : 2953 : get_dso_handle_node (void)
9813 : : {
9814 : 2953 : if (dso_handle_node)
9815 : : return dso_handle_node;
9816 : :
9817 : : /* Declare the variable. */
9818 : 2003 : dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9819 : : ptr_type_node);
9820 : :
9821 : : #ifdef HAVE_GAS_HIDDEN
9822 : 2003 : if (dso_handle_node != error_mark_node)
9823 : : {
9824 : 1999 : DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9825 : 1999 : DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9826 : : }
9827 : : #endif
9828 : :
9829 : 2003 : return dso_handle_node;
9830 : : }
9831 : :
9832 : : /* Begin a new function with internal linkage whose job will be simply
9833 : : to destroy some particular variable. */
9834 : :
9835 : : static GTY(()) int start_cleanup_cnt;
9836 : :
9837 : : static tree
9838 : 372 : start_cleanup_fn (void)
9839 : : {
9840 : 372 : char name[32];
9841 : :
9842 : 372 : push_to_top_level ();
9843 : :
9844 : : /* No need to mangle this. */
9845 : 372 : push_lang_context (lang_name_c);
9846 : :
9847 : : /* Build the name of the function. */
9848 : 372 : sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9849 : : /* Build the function declaration. */
9850 : 372 : tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9851 : 372 : tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9852 : 372 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9853 : : /* It's a function with internal linkage, generated by the
9854 : : compiler. */
9855 : 372 : TREE_PUBLIC (fndecl) = 0;
9856 : 372 : DECL_ARTIFICIAL (fndecl) = 1;
9857 : : /* Make the function `inline' so that it is only emitted if it is
9858 : : actually needed. It is unlikely that it will be inlined, since
9859 : : it is only called via a function pointer, but we avoid unnecessary
9860 : : emissions this way. */
9861 : 372 : DECL_DECLARED_INLINE_P (fndecl) = 1;
9862 : 372 : DECL_INTERFACE_KNOWN (fndecl) = 1;
9863 : 372 : if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9864 : : {
9865 : : /* Build the parameter. */
9866 : 360 : tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9867 : 360 : TREE_USED (parmdecl) = 1;
9868 : 360 : DECL_READ_P (parmdecl) = 1;
9869 : 360 : DECL_ARGUMENTS (fndecl) = parmdecl;
9870 : : }
9871 : :
9872 : 372 : fndecl = pushdecl (fndecl, /*hidden=*/true);
9873 : 372 : start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9874 : :
9875 : 372 : pop_lang_context ();
9876 : :
9877 : 372 : return current_function_decl;
9878 : : }
9879 : :
9880 : : /* Finish the cleanup function begun by start_cleanup_fn. */
9881 : :
9882 : : static void
9883 : 372 : end_cleanup_fn (void)
9884 : : {
9885 : 372 : expand_or_defer_fn (finish_function (/*inline_p=*/false));
9886 : :
9887 : 372 : pop_from_top_level ();
9888 : 372 : }
9889 : :
9890 : : /* Generate code to handle the destruction of DECL, an object with
9891 : : static storage duration. */
9892 : :
9893 : : tree
9894 : 13106 : register_dtor_fn (tree decl)
9895 : : {
9896 : 13106 : tree cleanup;
9897 : 13106 : tree addr;
9898 : 13106 : tree compound_stmt;
9899 : 13106 : tree fcall;
9900 : 13106 : tree type;
9901 : 13106 : bool ob_parm, dso_parm, use_dtor;
9902 : 13106 : tree arg0, arg1, arg2;
9903 : 13106 : tree atex_node;
9904 : :
9905 : 13106 : type = TREE_TYPE (decl);
9906 : 13106 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9907 : 10141 : return void_node;
9908 : :
9909 : 2965 : if (decl_maybe_constant_destruction (decl, type)
9910 : 2965 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9911 : : {
9912 : 0 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9913 : 0 : return void_node;
9914 : : }
9915 : :
9916 : : /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9917 : : "__aeabi_atexit"), and DECL is a class object, we can just pass the
9918 : : destructor to "__cxa_atexit"; we don't have to build a temporary
9919 : : function to do the cleanup. */
9920 : 5930 : dso_parm = (flag_use_cxa_atexit
9921 : 2965 : && !targetm.cxx.use_atexit_for_cxa_atexit ());
9922 : 2965 : ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9923 : 2953 : use_dtor = ob_parm && CLASS_TYPE_P (type);
9924 : 2965 : if (use_dtor)
9925 : : {
9926 : 2593 : cleanup = get_class_binding (type, complete_dtor_identifier);
9927 : :
9928 : : /* Make sure it is accessible. */
9929 : 2593 : perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9930 : : tf_warning_or_error);
9931 : : }
9932 : : else
9933 : : {
9934 : : /* Call build_cleanup before we enter the anonymous function so
9935 : : that any access checks will be done relative to the current
9936 : : scope, rather than the scope of the anonymous function. */
9937 : 372 : build_cleanup (decl);
9938 : :
9939 : : /* Now start the function. */
9940 : 372 : cleanup = start_cleanup_fn ();
9941 : :
9942 : : /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9943 : : to the original function, rather than the anonymous one. That
9944 : : will make the back end think that nested functions are in use,
9945 : : which causes confusion. */
9946 : 372 : push_deferring_access_checks (dk_no_check);
9947 : 372 : fcall = build_cleanup (decl);
9948 : 372 : pop_deferring_access_checks ();
9949 : :
9950 : : /* Create the body of the anonymous function. */
9951 : 372 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9952 : 372 : finish_expr_stmt (fcall);
9953 : 372 : finish_compound_stmt (compound_stmt);
9954 : 372 : end_cleanup_fn ();
9955 : : }
9956 : :
9957 : : /* Call atexit with the cleanup function. */
9958 : 2965 : mark_used (cleanup);
9959 : 2965 : cleanup = build_address (cleanup);
9960 : :
9961 : 2965 : if (CP_DECL_THREAD_LOCAL_P (decl))
9962 : 57 : atex_node = get_thread_atexit_node ();
9963 : : else
9964 : 2908 : atex_node = get_atexit_node ();
9965 : :
9966 : 2965 : if (use_dtor)
9967 : : {
9968 : : /* We must convert CLEANUP to the type that "__cxa_atexit"
9969 : : expects. */
9970 : 2593 : cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9971 : : /* "__cxa_atexit" will pass the address of DECL to the
9972 : : cleanup function. */
9973 : 2593 : mark_used (decl);
9974 : 2593 : addr = build_address (decl);
9975 : : /* The declared type of the parameter to "__cxa_atexit" is
9976 : : "void *". For plain "T*", we could just let the
9977 : : machinery in cp_build_function_call convert it -- but if the
9978 : : type is "cv-qualified T *", then we need to convert it
9979 : : before passing it in, to avoid spurious errors. */
9980 : 2593 : addr = build_nop (ptr_type_node, addr);
9981 : : }
9982 : : else
9983 : : /* Since the cleanup functions we build ignore the address
9984 : : they're given, there's no reason to pass the actual address
9985 : : in, and, in general, it's cheaper to pass NULL than any
9986 : : other value. */
9987 : 372 : addr = null_pointer_node;
9988 : :
9989 : 2965 : if (dso_parm)
9990 : 2953 : arg2 = cp_build_addr_expr (get_dso_handle_node (),
9991 : : tf_warning_or_error);
9992 : 12 : else if (ob_parm)
9993 : : /* Just pass NULL to the dso handle parm if we don't actually
9994 : : have a DSO handle on this target. */
9995 : 0 : arg2 = null_pointer_node;
9996 : : else
9997 : : arg2 = NULL_TREE;
9998 : :
9999 : 2953 : if (ob_parm)
10000 : : {
10001 : 2953 : if (!CP_DECL_THREAD_LOCAL_P (decl)
10002 : 2953 : && targetm.cxx.use_aeabi_atexit ())
10003 : : {
10004 : : arg1 = cleanup;
10005 : : arg0 = addr;
10006 : : }
10007 : : else
10008 : : {
10009 : : arg1 = addr;
10010 : : arg0 = cleanup;
10011 : : }
10012 : : }
10013 : : else
10014 : : {
10015 : : arg0 = cleanup;
10016 : : arg1 = NULL_TREE;
10017 : : }
10018 : 2965 : return cp_build_function_call_nary (atex_node, tf_warning_or_error,
10019 : 2965 : arg0, arg1, arg2, NULL_TREE);
10020 : : }
10021 : :
10022 : : /* DECL is a VAR_DECL with static storage duration. INIT, if present,
10023 : : is its initializer. Generate code to handle the construction
10024 : : and destruction of DECL. */
10025 : :
10026 : : static void
10027 : 12458734 : expand_static_init (tree decl, tree init)
10028 : : {
10029 : 12458734 : gcc_assert (VAR_P (decl));
10030 : 12458734 : gcc_assert (TREE_STATIC (decl));
10031 : :
10032 : : /* Some variables require no dynamic initialization. */
10033 : 12458734 : if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
10034 : : {
10035 : : /* Make sure the destructor is callable. */
10036 : 12455764 : cxx_maybe_build_cleanup (decl, tf_warning_or_error);
10037 : 12455764 : if (!init)
10038 : : return;
10039 : : }
10040 : :
10041 : 13741 : if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
10042 : 13098 : && !DECL_FUNCTION_SCOPE_P (decl))
10043 : : {
10044 : 18 : location_t dloc = DECL_SOURCE_LOCATION (decl);
10045 : 18 : if (init)
10046 : 15 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
10047 : : "needs dynamic initialization", decl);
10048 : : else
10049 : 3 : error_at (dloc, "non-local variable %qD declared %<__thread%> "
10050 : : "has a non-trivial destructor", decl);
10051 : 18 : static bool informed;
10052 : 18 : if (!informed)
10053 : : {
10054 : 7 : inform (dloc, "C++11 %<thread_local%> allows dynamic "
10055 : : "initialization and destruction");
10056 : 7 : informed = true;
10057 : : }
10058 : 18 : return;
10059 : : }
10060 : :
10061 : 13062 : if (DECL_FUNCTION_SCOPE_P (decl))
10062 : : {
10063 : : /* Emit code to perform this initialization but once. */
10064 : 3366 : tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
10065 : 3366 : tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
10066 : 3366 : tree guard, guard_addr;
10067 : 3366 : tree flag, begin;
10068 : : /* We don't need thread-safety code for thread-local vars. */
10069 : 3366 : bool thread_guard = (flag_threadsafe_statics
10070 : 3366 : && !CP_DECL_THREAD_LOCAL_P (decl));
10071 : :
10072 : : /* Emit code to perform this initialization but once. This code
10073 : : looks like:
10074 : :
10075 : : static <type> guard;
10076 : : if (!__atomic_load (guard.first_byte)) {
10077 : : if (__cxa_guard_acquire (&guard)) {
10078 : : bool flag = false;
10079 : : try {
10080 : : // Do initialization.
10081 : : flag = true; __cxa_guard_release (&guard);
10082 : : // Register variable for destruction at end of program.
10083 : : } catch {
10084 : : if (!flag) __cxa_guard_abort (&guard);
10085 : : }
10086 : : }
10087 : : }
10088 : :
10089 : : Note that the `flag' variable is only set to 1 *after* the
10090 : : initialization is complete. This ensures that an exception,
10091 : : thrown during the construction, will cause the variable to
10092 : : reinitialized when we pass through this code again, as per:
10093 : :
10094 : : [stmt.dcl]
10095 : :
10096 : : If the initialization exits by throwing an exception, the
10097 : : initialization is not complete, so it will be tried again
10098 : : the next time control enters the declaration.
10099 : :
10100 : : This process should be thread-safe, too; multiple threads
10101 : : should not be able to initialize the variable more than
10102 : : once. */
10103 : :
10104 : : /* Create the guard variable. */
10105 : 3366 : guard = get_guard (decl);
10106 : :
10107 : : /* Begin the conditional initialization. */
10108 : 3366 : if_stmt = begin_if_stmt ();
10109 : :
10110 : 3366 : finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
10111 : 3366 : then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10112 : :
10113 : 3366 : if (thread_guard)
10114 : : {
10115 : 3312 : tree vfntype = NULL_TREE;
10116 : 3312 : tree acquire_name, release_name, abort_name;
10117 : 3312 : tree acquire_fn, release_fn, abort_fn;
10118 : 3312 : guard_addr = build_address (guard);
10119 : :
10120 : 3312 : acquire_name = get_identifier ("__cxa_guard_acquire");
10121 : 3312 : release_name = get_identifier ("__cxa_guard_release");
10122 : 3312 : abort_name = get_identifier ("__cxa_guard_abort");
10123 : 3312 : acquire_fn = get_global_binding (acquire_name);
10124 : 3312 : release_fn = get_global_binding (release_name);
10125 : 3312 : abort_fn = get_global_binding (abort_name);
10126 : 3312 : if (!acquire_fn)
10127 : 1842 : acquire_fn = push_library_fn
10128 : 1842 : (acquire_name, build_function_type_list (integer_type_node,
10129 : 1842 : TREE_TYPE (guard_addr),
10130 : : NULL_TREE),
10131 : : NULL_TREE, ECF_NOTHROW);
10132 : 3312 : if (!release_fn || !abort_fn)
10133 : 1846 : vfntype = build_function_type_list (void_type_node,
10134 : 1846 : TREE_TYPE (guard_addr),
10135 : : NULL_TREE);
10136 : 1846 : if (!release_fn)
10137 : 1846 : release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
10138 : : ECF_NOTHROW);
10139 : 3312 : if (!abort_fn)
10140 : 1846 : abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
10141 : : ECF_NOTHROW | ECF_LEAF);
10142 : :
10143 : 3312 : inner_if_stmt = begin_if_stmt ();
10144 : 3312 : finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
10145 : : inner_if_stmt);
10146 : :
10147 : 3312 : inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
10148 : 3312 : begin = get_target_expr (boolean_false_node);
10149 : 3312 : flag = TARGET_EXPR_SLOT (begin);
10150 : :
10151 : 3312 : TARGET_EXPR_CLEANUP (begin)
10152 : 3312 : = build3 (COND_EXPR, void_type_node, flag,
10153 : : void_node,
10154 : : build_call_n (abort_fn, 1, guard_addr));
10155 : 3312 : CLEANUP_EH_ONLY (begin) = 1;
10156 : :
10157 : : /* Do the initialization itself. */
10158 : 3312 : init = add_stmt_to_compound (begin, init);
10159 : 3312 : init = add_stmt_to_compound (init,
10160 : : build2 (MODIFY_EXPR, void_type_node,
10161 : : flag, boolean_true_node));
10162 : :
10163 : : /* Use atexit to register a function for destroying this static
10164 : : variable. Do this before calling __cxa_guard_release. */
10165 : 3312 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
10166 : :
10167 : 3312 : init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
10168 : : guard_addr));
10169 : : }
10170 : : else
10171 : : {
10172 : 54 : init = add_stmt_to_compound (init, set_guard (guard));
10173 : :
10174 : : /* Use atexit to register a function for destroying this static
10175 : : variable. */
10176 : 54 : init = add_stmt_to_compound (init, register_dtor_fn (decl));
10177 : : }
10178 : :
10179 : 3366 : finish_expr_stmt (init);
10180 : :
10181 : 3366 : if (thread_guard)
10182 : : {
10183 : 3312 : finish_compound_stmt (inner_then_clause);
10184 : 3312 : finish_then_clause (inner_if_stmt);
10185 : 3312 : finish_if_stmt (inner_if_stmt);
10186 : : }
10187 : :
10188 : 3366 : finish_compound_stmt (then_clause);
10189 : 3366 : finish_then_clause (if_stmt);
10190 : 3366 : finish_if_stmt (if_stmt);
10191 : : }
10192 : 9696 : else if (CP_DECL_THREAD_LOCAL_P (decl))
10193 : 593 : tls_aggregates = tree_cons (init, decl, tls_aggregates);
10194 : : else
10195 : 9103 : static_aggregates = tree_cons (init, decl, static_aggregates);
10196 : : }
10197 : :
10198 : :
10199 : : /* Make TYPE a complete type based on INITIAL_VALUE.
10200 : : Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10201 : : 2 if there was no information (in which case assume 0 if DO_DEFAULT),
10202 : : 3 if the initializer list is empty (in pedantic mode). */
10203 : :
10204 : : int
10205 : 457405 : cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
10206 : : {
10207 : 457405 : int failure;
10208 : 457405 : tree type, elt_type;
10209 : :
10210 : : /* Don't get confused by a CONSTRUCTOR for some other type. */
10211 : 457372 : if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
10212 : 67962 : && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
10213 : 457727 : && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
10214 : : return 1;
10215 : :
10216 : 457401 : if (initial_value)
10217 : : {
10218 : : /* An array of character type can be initialized from a
10219 : : brace-enclosed string constant so call reshape_init to
10220 : : remove the optional braces from a braced string literal. */
10221 : 457368 : if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
10222 : 457368 : && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
10223 : 24877 : initial_value = reshape_init (*ptype, initial_value,
10224 : : tf_warning_or_error);
10225 : :
10226 : : /* If any of the elements are parameter packs, we can't actually
10227 : : complete this type now because the array size is dependent. */
10228 : 457368 : if (TREE_CODE (initial_value) == CONSTRUCTOR)
10229 : 8368862 : for (auto &e: CONSTRUCTOR_ELTS (initial_value))
10230 : 8165406 : if (PACK_EXPANSION_P (e.value))
10231 : : return 0;
10232 : : }
10233 : :
10234 : 457401 : failure = complete_array_type (ptype, initial_value, do_default);
10235 : :
10236 : : /* We can create the array before the element type is complete, which
10237 : : means that we didn't have these two bits set in the original type
10238 : : either. In completing the type, we are expected to propagate these
10239 : : bits. See also complete_type which does the same thing for arrays
10240 : : of fixed size. */
10241 : 457401 : type = *ptype;
10242 : 914798 : if (type != error_mark_node && TYPE_DOMAIN (type))
10243 : : {
10244 : 457372 : elt_type = TREE_TYPE (type);
10245 : 457372 : TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
10246 : 914744 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
10247 : 457372 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
10248 : : }
10249 : :
10250 : : return failure;
10251 : : }
10252 : :
10253 : : /* As above, but either give an error or reject zero-size arrays, depending
10254 : : on COMPLAIN. */
10255 : :
10256 : : int
10257 : 575 : cp_complete_array_type_or_error (tree *ptype, tree initial_value,
10258 : : bool do_default, tsubst_flags_t complain)
10259 : : {
10260 : 575 : int failure;
10261 : 575 : bool sfinae = !(complain & tf_error);
10262 : : /* In SFINAE context we can't be lenient about zero-size arrays. */
10263 : 575 : if (sfinae)
10264 : 3 : ++pedantic;
10265 : 575 : failure = cp_complete_array_type (ptype, initial_value, do_default);
10266 : 575 : if (sfinae)
10267 : 3 : --pedantic;
10268 : 575 : if (failure)
10269 : : {
10270 : 3 : if (sfinae)
10271 : : /* Not an error. */;
10272 : 0 : else if (failure == 1)
10273 : 0 : error ("initializer fails to determine size of %qT", *ptype);
10274 : 0 : else if (failure == 2)
10275 : : {
10276 : 0 : if (do_default)
10277 : 0 : error ("array size missing in %qT", *ptype);
10278 : : }
10279 : 0 : else if (failure == 3)
10280 : 0 : error ("zero-size array %qT", *ptype);
10281 : 3 : *ptype = error_mark_node;
10282 : : }
10283 : 575 : return failure;
10284 : : }
10285 : :
10286 : : /* Return zero if something is declared to be a member of type
10287 : : CTYPE when in the context of CUR_TYPE. STRING is the error
10288 : : message to print in that case. Otherwise, quietly return 1. */
10289 : :
10290 : : static int
10291 : 21967545 : member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
10292 : : {
10293 : 21967545 : if (ctype && ctype != cur_type)
10294 : : {
10295 : 0 : if (flags == DTOR_FLAG)
10296 : 0 : error ("destructor for alien class %qT cannot be a member", ctype);
10297 : : else
10298 : 0 : error ("constructor for alien class %qT cannot be a member", ctype);
10299 : 0 : return 0;
10300 : : }
10301 : : return 1;
10302 : : }
10303 : :
10304 : : /* Subroutine of `grokdeclarator'. */
10305 : :
10306 : : /* Generate errors possibly applicable for a given set of specifiers.
10307 : : This is for ARM $7.1.2. */
10308 : :
10309 : : static void
10310 : 371534926 : bad_specifiers (tree object,
10311 : : enum bad_spec_place type,
10312 : : int virtualp,
10313 : : int quals,
10314 : : int inlinep,
10315 : : int friendp,
10316 : : int raises,
10317 : : const location_t* locations)
10318 : : {
10319 : 371534926 : switch (type)
10320 : : {
10321 : 51170778 : case BSP_VAR:
10322 : 51170778 : if (virtualp)
10323 : 0 : error_at (locations[ds_virtual],
10324 : : "%qD declared as a %<virtual%> variable", object);
10325 : 51170778 : if (quals)
10326 : 4 : error ("%<const%> and %<volatile%> function specifiers on "
10327 : : "%qD invalid in variable declaration", object);
10328 : : break;
10329 : 256097551 : case BSP_PARM:
10330 : 256097551 : if (virtualp)
10331 : 0 : error_at (locations[ds_virtual],
10332 : : "%qD declared as a %<virtual%> parameter", object);
10333 : 256097551 : if (inlinep)
10334 : 7 : error_at (locations[ds_inline],
10335 : : "%qD declared as an %<inline%> parameter", object);
10336 : 256097551 : if (quals)
10337 : 3 : error ("%<const%> and %<volatile%> function specifiers on "
10338 : : "%qD invalid in parameter declaration", object);
10339 : : break;
10340 : 39766232 : case BSP_TYPE:
10341 : 39766232 : if (virtualp)
10342 : 0 : error_at (locations[ds_virtual],
10343 : : "%qD declared as a %<virtual%> type", object);
10344 : 39766232 : if (inlinep)
10345 : 7 : error_at (locations[ds_inline],
10346 : : "%qD declared as an %<inline%> type", object);
10347 : 39766232 : if (quals)
10348 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
10349 : : "%qD invalid in type declaration", object);
10350 : : break;
10351 : 24500365 : case BSP_FIELD:
10352 : 24500365 : if (virtualp)
10353 : 7 : error_at (locations[ds_virtual],
10354 : : "%qD declared as a %<virtual%> field", object);
10355 : 24500365 : if (inlinep)
10356 : 7 : error_at (locations[ds_inline],
10357 : : "%qD declared as an %<inline%> field", object);
10358 : 24500365 : if (quals)
10359 : 0 : error ("%<const%> and %<volatile%> function specifiers on "
10360 : : "%qD invalid in field declaration", object);
10361 : : break;
10362 : 0 : default:
10363 : 0 : gcc_unreachable();
10364 : : }
10365 : 371534926 : if (friendp)
10366 : 0 : error ("%q+D declared as a friend", object);
10367 : 371534926 : if (raises
10368 : 23571 : && !flag_noexcept_type
10369 : 371534951 : && (TREE_CODE (object) == TYPE_DECL
10370 : 19 : || (!TYPE_PTRFN_P (TREE_TYPE (object))
10371 : 8 : && !TYPE_REFFN_P (TREE_TYPE (object))
10372 : 6 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
10373 : 6 : error ("%q+D declared with an exception specification", object);
10374 : 371534926 : }
10375 : :
10376 : : /* DECL is a member function or static data member and is presently
10377 : : being defined. Check that the definition is taking place in a
10378 : : valid namespace. */
10379 : :
10380 : : static void
10381 : 76260351 : check_class_member_definition_namespace (tree decl)
10382 : : {
10383 : : /* These checks only apply to member functions and static data
10384 : : members. */
10385 : 76260351 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
10386 : : /* We check for problems with specializations in pt.cc in
10387 : : check_specialization_namespace, where we can issue better
10388 : : diagnostics. */
10389 : 76260351 : if (processing_specialization)
10390 : : return;
10391 : : /* We check this in check_explicit_instantiation_namespace. */
10392 : 76110903 : if (processing_explicit_instantiation)
10393 : : return;
10394 : : /* [class.mfct]
10395 : :
10396 : : A member function definition that appears outside of the
10397 : : class definition shall appear in a namespace scope enclosing
10398 : : the class definition.
10399 : :
10400 : : [class.static.data]
10401 : :
10402 : : The definition for a static data member shall appear in a
10403 : : namespace scope enclosing the member's class definition. */
10404 : 76110856 : if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
10405 : 24 : permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
10406 : 24 : decl, DECL_CONTEXT (decl));
10407 : : }
10408 : :
10409 : : /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
10410 : : METHOD_TYPE for a non-static member function; QUALS are the
10411 : : cv-qualifiers that apply to the function. */
10412 : :
10413 : : tree
10414 : 187266262 : build_this_parm (tree fn, tree type, cp_cv_quals quals)
10415 : : {
10416 : 187266262 : tree this_type;
10417 : 187266262 : tree qual_type;
10418 : 187266262 : tree parm;
10419 : 187266262 : cp_cv_quals this_quals;
10420 : :
10421 : 187266262 : if (CLASS_TYPE_P (type))
10422 : : {
10423 : 84056270 : this_type
10424 : 84056270 : = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
10425 : 84056270 : this_type = build_pointer_type (this_type);
10426 : : }
10427 : : else
10428 : 103209992 : this_type = type_of_this_parm (type);
10429 : : /* The `this' parameter is implicitly `const'; it cannot be
10430 : : assigned to. */
10431 : 187266262 : this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
10432 : 187266262 : qual_type = cp_build_qualified_type (this_type, this_quals);
10433 : 187266262 : parm = build_artificial_parm (fn, this_identifier, qual_type);
10434 : 187266262 : cp_apply_type_quals_to_decl (this_quals, parm);
10435 : 187266262 : return parm;
10436 : : }
10437 : :
10438 : : /* DECL is a static member function. Complain if it was declared
10439 : : with function-cv-quals. */
10440 : :
10441 : : static void
10442 : 8358310 : check_static_quals (tree decl, cp_cv_quals quals)
10443 : : {
10444 : 0 : if (quals != TYPE_UNQUALIFIED)
10445 : 7 : error ("static member function %q#D declared with type qualifiers",
10446 : : decl);
10447 : 0 : }
10448 : :
10449 : : // Check that FN takes no arguments and returns bool.
10450 : : static void
10451 : 129 : check_concept_fn (tree fn)
10452 : : {
10453 : : // A constraint is nullary.
10454 : 129 : if (DECL_ARGUMENTS (fn))
10455 : 1 : error_at (DECL_SOURCE_LOCATION (fn),
10456 : : "concept %q#D declared with function parameters", fn);
10457 : :
10458 : : // The declared return type of the concept shall be bool, and
10459 : : // it shall not be deduced from it definition.
10460 : 129 : tree type = TREE_TYPE (TREE_TYPE (fn));
10461 : 129 : if (is_auto (type))
10462 : 1 : error_at (DECL_SOURCE_LOCATION (fn),
10463 : : "concept %q#D declared with a deduced return type", fn);
10464 : 128 : else if (type != boolean_type_node)
10465 : 2 : error_at (DECL_SOURCE_LOCATION (fn),
10466 : : "concept %q#D with non-%<bool%> return type %qT", fn, type);
10467 : 129 : }
10468 : :
10469 : : /* Helper function. Replace the temporary this parameter injected
10470 : : during cp_finish_omp_declare_simd with the real this parameter. */
10471 : :
10472 : : static tree
10473 : 3078 : declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10474 : : {
10475 : 3078 : tree this_parm = (tree) data;
10476 : 3078 : if (TREE_CODE (*tp) == PARM_DECL
10477 : 562 : && DECL_NAME (*tp) == this_identifier
10478 : 3406 : && *tp != this_parm)
10479 : 328 : *tp = this_parm;
10480 : 2750 : else if (TYPE_P (*tp))
10481 : 44 : *walk_subtrees = 0;
10482 : 3078 : return NULL_TREE;
10483 : : }
10484 : :
10485 : : /* CTYPE is class type, or null if non-class.
10486 : : TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10487 : : or METHOD_TYPE.
10488 : : DECLARATOR is the function's name.
10489 : : PARMS is a chain of PARM_DECLs for the function.
10490 : : VIRTUALP is truthvalue of whether the function is virtual or not.
10491 : : FLAGS are to be passed through to `grokclassfn'.
10492 : : QUALS are qualifiers indicating whether the function is `const'
10493 : : or `volatile'.
10494 : : RAISES is a list of exceptions that this function can raise.
10495 : : CHECK is 1 if we must find this method in CTYPE, 0 if we should
10496 : : not look, and -1 if we should not call `grokclassfn' at all.
10497 : :
10498 : : SFK is the kind of special function (if any) for the new function.
10499 : :
10500 : : Returns `NULL_TREE' if something goes wrong, after issuing
10501 : : applicable error messages. */
10502 : :
10503 : : static tree
10504 : 157750061 : grokfndecl (tree ctype,
10505 : : tree type,
10506 : : tree declarator,
10507 : : tree parms,
10508 : : tree orig_declarator,
10509 : : const cp_decl_specifier_seq *declspecs,
10510 : : tree decl_reqs,
10511 : : int virtualp,
10512 : : enum overload_flags flags,
10513 : : cp_cv_quals quals,
10514 : : cp_ref_qualifier rqual,
10515 : : tree raises,
10516 : : int check,
10517 : : int friendp,
10518 : : int publicp,
10519 : : int inlinep,
10520 : : bool deletedp,
10521 : : bool xobj_func_p,
10522 : : special_function_kind sfk,
10523 : : bool funcdef_flag,
10524 : : bool late_return_type_p,
10525 : : int template_count,
10526 : : tree in_namespace,
10527 : : tree* attrlist,
10528 : : location_t location)
10529 : : {
10530 : 157750061 : tree decl;
10531 : 157750061 : tree t;
10532 : :
10533 : 157750061 : if (location == UNKNOWN_LOCATION)
10534 : 0 : location = input_location;
10535 : :
10536 : : /* Was the concept specifier present? */
10537 : 157750061 : bool concept_p = inlinep & 4;
10538 : :
10539 : : /* Concept declarations must have a corresponding definition. */
10540 : 157750061 : if (concept_p && !funcdef_flag)
10541 : : {
10542 : 1 : error_at (location, "concept %qD has no definition", declarator);
10543 : 1 : return NULL_TREE;
10544 : : }
10545 : :
10546 : 157750060 : type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10547 : :
10548 : 157750060 : decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10549 : :
10550 : : /* Set the constraints on the declaration. */
10551 : 157750060 : if (flag_concepts)
10552 : : {
10553 : 38307590 : tree tmpl_reqs = NULL_TREE;
10554 : 38307590 : tree ctx = friendp ? current_class_type : ctype;
10555 : 38307590 : bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10556 : 38307590 : bool memtmpl = (!block_local
10557 : 38307590 : && (current_template_depth
10558 : 38301106 : > template_class_depth (ctx)));
10559 : 10816522 : if (memtmpl)
10560 : : {
10561 : 10816522 : if (!current_template_parms)
10562 : : /* If there are no template parameters, something must have
10563 : : gone wrong. */
10564 : 0 : gcc_assert (seen_error ());
10565 : : else
10566 : 10816522 : tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10567 : : }
10568 : 38307590 : tree ci = build_constraints (tmpl_reqs, decl_reqs);
10569 : 38307590 : if (concept_p && ci)
10570 : : {
10571 : 2 : error_at (location, "a function concept cannot be constrained");
10572 : 2 : ci = NULL_TREE;
10573 : : }
10574 : : /* C++20 CA378: Remove non-templated constrained functions. */
10575 : : /* [temp.friend]/9 A non-template friend declaration with a
10576 : : requires-clause shall be a definition. A friend function template with
10577 : : a constraint that depends on a template parameter from an enclosing
10578 : : template shall be a definition. */
10579 : 38307590 : if (ci
10580 : 2211638 : && (block_local
10581 : 2211636 : || (!flag_concepts_ts
10582 : 2209383 : && (!processing_template_decl
10583 : 2209370 : || (friendp && !memtmpl && !funcdef_flag)))))
10584 : : {
10585 : 18 : if (!friendp || !processing_template_decl)
10586 : 15 : error_at (location, "constraints on a non-templated function");
10587 : : else
10588 : 3 : error_at (location, "constrained non-template friend declaration"
10589 : : " must be a definition");
10590 : : ci = NULL_TREE;
10591 : : }
10592 : 38307590 : set_constraints (decl, ci);
10593 : 322016 : if (ci && friendp && memtmpl && !funcdef_flag
10594 : 38311427 : && uses_outer_template_parms_in_constraints (decl, ctx))
10595 : 2 : error_at (location, "friend function template with constraints that "
10596 : : "depend on outer template parameters must be a definition");
10597 : : }
10598 : :
10599 : 157750060 : if (TREE_CODE (type) == METHOD_TYPE)
10600 : : {
10601 : 82308596 : tree parm = build_this_parm (decl, type, quals);
10602 : 82308596 : DECL_CHAIN (parm) = parms;
10603 : 82308596 : parms = parm;
10604 : :
10605 : : /* Allocate space to hold the vptr bit if needed. */
10606 : 82308596 : SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10607 : : }
10608 : :
10609 : 157750060 : DECL_ARGUMENTS (decl) = parms;
10610 : 481738531 : for (t = parms; t; t = DECL_CHAIN (t))
10611 : 323988471 : DECL_CONTEXT (t) = decl;
10612 : :
10613 : : /* Propagate volatile out from type to decl. */
10614 : 157750060 : if (TYPE_VOLATILE (type))
10615 : 0 : TREE_THIS_VOLATILE (decl) = 1;
10616 : :
10617 : : /* Setup decl according to sfk. */
10618 : 157750060 : switch (sfk)
10619 : : {
10620 : 20068171 : case sfk_constructor:
10621 : 20068171 : case sfk_copy_constructor:
10622 : 20068171 : case sfk_move_constructor:
10623 : 20068171 : DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10624 : 20068171 : DECL_NAME (decl) = ctor_identifier;
10625 : 20068171 : break;
10626 : 2816619 : case sfk_destructor:
10627 : 2816619 : DECL_CXX_DESTRUCTOR_P (decl) = 1;
10628 : 2816619 : DECL_NAME (decl) = dtor_identifier;
10629 : 2816619 : break;
10630 : 811371 : case sfk_deduction_guide:
10631 : : /* Give deduction guides a definition even though they don't really
10632 : : have one: the restriction that you can't repeat a deduction guide
10633 : : makes them more like a definition anyway. */
10634 : 811371 : DECL_INITIAL (decl) = void_node;
10635 : 811371 : break;
10636 : : default:
10637 : : break;
10638 : : }
10639 : :
10640 : 157750060 : if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10641 : : {
10642 : 11015 : if (funcdef_flag)
10643 : 4 : error_at (location,
10644 : : "defining explicit specialization %qD in friend declaration",
10645 : : orig_declarator);
10646 : : else
10647 : : {
10648 : 11011 : tree fns = TREE_OPERAND (orig_declarator, 0);
10649 : 11011 : tree args = TREE_OPERAND (orig_declarator, 1);
10650 : :
10651 : 11011 : if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10652 : : {
10653 : : /* Something like `template <class T> friend void f<T>()'. */
10654 : 8 : error_at (location,
10655 : : "invalid use of template-id %qD in declaration "
10656 : : "of primary template",
10657 : : orig_declarator);
10658 : 8 : return NULL_TREE;
10659 : : }
10660 : :
10661 : : /* A friend declaration of the form friend void f<>(). Record
10662 : : the information in the TEMPLATE_ID_EXPR. */
10663 : 11003 : SET_DECL_IMPLICIT_INSTANTIATION (decl);
10664 : :
10665 : 11003 : gcc_assert (identifier_p (fns) || OVL_P (fns));
10666 : 11003 : DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10667 : :
10668 : 54537 : for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10669 : 43539 : if (TREE_PURPOSE (t)
10670 : 43539 : && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10671 : : {
10672 : 5 : error_at (defparse_location (TREE_PURPOSE (t)),
10673 : : "default arguments are not allowed in declaration "
10674 : : "of friend template specialization %qD",
10675 : : decl);
10676 : 5 : return NULL_TREE;
10677 : : }
10678 : :
10679 : 10998 : if (inlinep & 1)
10680 : : {
10681 : 5 : error_at (declspecs->locations[ds_inline],
10682 : : "%<inline%> is not allowed in declaration of friend "
10683 : : "template specialization %qD",
10684 : : decl);
10685 : 5 : return NULL_TREE;
10686 : : }
10687 : : }
10688 : : }
10689 : :
10690 : : /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10691 : : expression, that declaration shall be a definition..." */
10692 : 157750042 : if (friendp && !funcdef_flag)
10693 : : {
10694 : 1164194 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10695 : 3461364 : t && t != void_list_node; t = TREE_CHAIN (t))
10696 : 2297202 : if (TREE_PURPOSE (t))
10697 : : {
10698 : 32 : permerror (DECL_SOURCE_LOCATION (decl),
10699 : : "friend declaration of %qD specifies default "
10700 : : "arguments and isn%'t a definition", decl);
10701 : 32 : break;
10702 : : }
10703 : : }
10704 : :
10705 : : /* If this decl has namespace scope, set that up. */
10706 : 157750042 : if (in_namespace)
10707 : 86157 : set_decl_namespace (decl, in_namespace, friendp);
10708 : 157663885 : else if (ctype)
10709 : 90531511 : DECL_CONTEXT (decl) = ctype;
10710 : : else
10711 : 67132374 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10712 : :
10713 : : /* `main' and builtins have implicit 'C' linkage. */
10714 : 157750042 : if (ctype == NULL_TREE
10715 : 67218531 : && DECL_FILE_SCOPE_P (decl)
10716 : 28818497 : && current_lang_name == lang_name_cplusplus
10717 : 160420694 : && (MAIN_NAME_P (declarator)
10718 : 2635790 : || (IDENTIFIER_LENGTH (declarator) > 10
10719 : 1820958 : && IDENTIFIER_POINTER (declarator)[0] == '_'
10720 : 1388787 : && IDENTIFIER_POINTER (declarator)[1] == '_'
10721 : 334151 : && startswith (IDENTIFIER_POINTER (declarator) + 2,
10722 : : "builtin_"))
10723 : 2635766 : || (targetcm.cxx_implicit_extern_c
10724 : 0 : && (targetcm.cxx_implicit_extern_c
10725 : 0 : (IDENTIFIER_POINTER (declarator))))))
10726 : 34886 : SET_DECL_LANGUAGE (decl, lang_c);
10727 : :
10728 : 157750042 : DECL_STATIC_FUNCTION_P (decl)
10729 : 157750042 : = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE;
10730 : 157750042 : DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p;
10731 : :
10732 : 157750042 : if (deletedp)
10733 : 3218835 : DECL_DELETED_FN (decl) = 1;
10734 : :
10735 : 157750042 : if (ctype && funcdef_flag)
10736 : 75901757 : check_class_member_definition_namespace (decl);
10737 : :
10738 : 157750042 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10739 : : {
10740 : 34858 : if (PROCESSING_REAL_TEMPLATE_DECL_P())
10741 : 12 : error_at (location, "cannot declare %<::main%> to be a template");
10742 : 34858 : if (inlinep & 1)
10743 : 4 : error_at (declspecs->locations[ds_inline],
10744 : : "cannot declare %<::main%> to be inline");
10745 : 34858 : if (inlinep & 2)
10746 : 6 : error_at (declspecs->locations[ds_constexpr],
10747 : : "cannot declare %<::main%> to be %qs", "constexpr");
10748 : 34858 : if (inlinep & 8)
10749 : 1 : error_at (declspecs->locations[ds_consteval],
10750 : : "cannot declare %<::main%> to be %qs", "consteval");
10751 : 34858 : if (!publicp)
10752 : 0 : error_at (location, "cannot declare %<::main%> to be static");
10753 : : inlinep = 0;
10754 : : publicp = 1;
10755 : : }
10756 : :
10757 : : /* Members of anonymous types and local classes have no linkage; make
10758 : : them internal. If a typedef is made later, this will be changed. */
10759 : 157750042 : if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
10760 : : publicp = 0;
10761 : 157723507 : else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
10762 : : /* But members of local classes in a module CMI should have their
10763 : : definitions exported, in case they are (directly or indirectly)
10764 : : used by an importer. We don't just use module_has_cmi_p here
10765 : : because for entities in the GMF we don't yet know whether this
10766 : : module will have a CMI, so we'll conservatively assume it might. */
10767 : 918373 : publicp = module_maybe_has_cmi_p ();
10768 : :
10769 : 157723507 : if (publicp && cxx_dialect == cxx98)
10770 : : {
10771 : : /* [basic.link]: A name with no linkage (notably, the name of a class
10772 : : or enumeration declared in a local scope) shall not be used to
10773 : : declare an entity with linkage.
10774 : :
10775 : : DR 757 relaxes this restriction for C++0x. */
10776 : 907105 : no_linkage_error (decl);
10777 : : }
10778 : :
10779 : 157750042 : TREE_PUBLIC (decl) = publicp;
10780 : 157750042 : if (! publicp)
10781 : : {
10782 : 1437719 : DECL_INTERFACE_KNOWN (decl) = 1;
10783 : 1437719 : DECL_NOT_REALLY_EXTERN (decl) = 1;
10784 : : }
10785 : :
10786 : : /* If the declaration was declared inline, mark it as such. */
10787 : 157750042 : if (inlinep)
10788 : : {
10789 : 45492298 : DECL_DECLARED_INLINE_P (decl) = 1;
10790 : 45492298 : if (publicp)
10791 : 44965455 : DECL_COMDAT (decl) = 1;
10792 : : }
10793 : 45492298 : if (inlinep & 2)
10794 : 25731480 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
10795 : 132018562 : else if (inlinep & 8)
10796 : : {
10797 : 62165 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
10798 : 62165 : SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10799 : : }
10800 : :
10801 : : // If the concept declaration specifier was found, check
10802 : : // that the declaration satisfies the necessary requirements.
10803 : 157750042 : if (concept_p)
10804 : : {
10805 : 129 : DECL_DECLARED_CONCEPT_P (decl) = true;
10806 : 129 : check_concept_fn (decl);
10807 : : }
10808 : :
10809 : 157750042 : DECL_EXTERNAL (decl) = 1;
10810 : 157750042 : if (TREE_CODE (type) == FUNCTION_TYPE)
10811 : : {
10812 : 75441446 : if (quals || rqual)
10813 : 277 : TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10814 : : TYPE_UNQUALIFIED,
10815 : : REF_QUAL_NONE);
10816 : 75441446 : auto_diagnostic_group d;
10817 : 75441446 : if (quals)
10818 : 428 : error (!ctype
10819 : : ? G_("non-member function %qD cannot have cv-qualifier")
10820 : : : !xobj_func_p
10821 : 198 : ? G_("static member function %qD cannot have cv-qualifier")
10822 : : : G_("explicit object member function "
10823 : : "%qD cannot have cv-qualifier"),
10824 : : decl);
10825 : 75441446 : if (rqual)
10826 : 355 : error (!ctype
10827 : : ? G_("non-member function %qD cannot have ref-qualifier")
10828 : : : !xobj_func_p
10829 : 176 : ? G_("static member function %qD cannot have ref-qualifier")
10830 : : : G_("explicit object member function "
10831 : : "%qD cannot have ref-qualifier"),
10832 : : decl);
10833 : :
10834 : 75441446 : if (xobj_func_p && (quals || rqual))
10835 : 242 : inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)),
10836 : : "explicit object parameter declared here");
10837 : 75441446 : quals = TYPE_UNQUALIFIED;
10838 : 75441446 : rqual = REF_QUAL_NONE;
10839 : :
10840 : 75441446 : }
10841 : :
10842 : 157750042 : if (deduction_guide_p (decl))
10843 : : {
10844 : 811371 : tree type = TREE_TYPE (DECL_NAME (decl));
10845 : 811371 : if (in_namespace == NULL_TREE
10846 : 811371 : && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10847 : : {
10848 : 2 : error_at (location, "deduction guide %qD must be declared in the "
10849 : : "same scope as %qT", decl, type);
10850 : 2 : inform (location_of (type), " declared here");
10851 : 2 : return NULL_TREE;
10852 : : }
10853 : 1622738 : if (DECL_CLASS_SCOPE_P (decl)
10854 : 811386 : && current_access_specifier != declared_access (TYPE_NAME (type)))
10855 : : {
10856 : 2 : error_at (location, "deduction guide %qD must have the same access "
10857 : : "as %qT", decl, type);
10858 : 2 : inform (location_of (type), " declared here");
10859 : : }
10860 : 811369 : if (funcdef_flag)
10861 : 2 : error_at (location,
10862 : : "deduction guide %qD must not have a function body", decl);
10863 : : }
10864 : 185445499 : else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10865 : 185445499 : && !grok_op_properties (decl, /*complain=*/true))
10866 : : return NULL_TREE;
10867 : 156938483 : else if (UDLIT_OPER_P (DECL_NAME (decl)))
10868 : : {
10869 : 263836 : bool long_long_unsigned_p;
10870 : 263836 : bool long_double_p;
10871 : 263836 : const char *suffix = NULL;
10872 : : /* [over.literal]/6: Literal operators shall not have C linkage. */
10873 : 263836 : if (DECL_LANGUAGE (decl) == lang_c)
10874 : : {
10875 : 6 : error_at (location, "literal operator with C linkage");
10876 : 6 : maybe_show_extern_c_location ();
10877 : 54 : return NULL_TREE;
10878 : : }
10879 : :
10880 : 263830 : if (DECL_NAMESPACE_SCOPE_P (decl))
10881 : : {
10882 : 263824 : if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10883 : : &long_double_p))
10884 : : {
10885 : 42 : error_at (location, "%qD has invalid argument list", decl);
10886 : 42 : return NULL_TREE;
10887 : : }
10888 : :
10889 : 263782 : suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10890 : 263782 : if (long_long_unsigned_p)
10891 : : {
10892 : 31540 : if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10893 : 68 : warning_at (location, 0, "integer suffix %qs"
10894 : : " shadowed by implementation", suffix);
10895 : : }
10896 : 232242 : else if (long_double_p)
10897 : : {
10898 : 84032 : if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10899 : 80 : warning_at (location, 0, "floating-point suffix %qs"
10900 : : " shadowed by implementation", suffix);
10901 : : }
10902 : : /* 17.6.3.3.5 */
10903 : 263782 : if (suffix[0] != '_'
10904 : 263278 : && !current_function_decl && !(friendp && !funcdef_flag))
10905 : 263272 : warning_at (location, OPT_Wliteral_suffix,
10906 : : "literal operator suffixes not preceded by %<_%>"
10907 : : " are reserved for future standardization");
10908 : : }
10909 : : else
10910 : : {
10911 : 6 : error_at (location, "%qD must be a non-member function", decl);
10912 : 6 : return NULL_TREE;
10913 : : }
10914 : : }
10915 : :
10916 : 156938431 : if (funcdef_flag)
10917 : : /* Make the init_value nonzero so pushdecl knows this is not
10918 : : tentative. error_mark_node is replaced later with the BLOCK. */
10919 : 106951939 : DECL_INITIAL (decl) = error_mark_node;
10920 : :
10921 : 157749798 : if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10922 : 58241044 : TREE_NOTHROW (decl) = 1;
10923 : :
10924 : 157749798 : if (flag_openmp || flag_openmp_simd)
10925 : : {
10926 : : /* Adjust "omp declare simd" attributes. */
10927 : 153828 : tree ods = lookup_attribute ("omp declare simd", *attrlist);
10928 : 153828 : if (ods)
10929 : : {
10930 : : tree attr;
10931 : 2492 : for (attr = ods; attr;
10932 : 1311 : attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10933 : : {
10934 : 1311 : if (TREE_CODE (type) == METHOD_TYPE)
10935 : 268 : walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10936 : : DECL_ARGUMENTS (decl), NULL);
10937 : 1311 : if (TREE_VALUE (attr) != NULL_TREE)
10938 : : {
10939 : 1067 : tree cl = TREE_VALUE (TREE_VALUE (attr));
10940 : 1067 : cl = c_omp_declare_simd_clauses_to_numbers
10941 : 1067 : (DECL_ARGUMENTS (decl), cl);
10942 : 1067 : if (cl)
10943 : 1067 : TREE_VALUE (TREE_VALUE (attr)) = cl;
10944 : : else
10945 : 0 : TREE_VALUE (attr) = NULL_TREE;
10946 : : }
10947 : : }
10948 : : }
10949 : : }
10950 : :
10951 : : /* Caller will do the rest of this. */
10952 : 157749798 : if (check < 0)
10953 : 3362977 : return decl;
10954 : :
10955 : 154386821 : if (ctype != NULL_TREE)
10956 : 90531138 : grokclassfn (ctype, decl, flags);
10957 : :
10958 : : /* 12.4/3 */
10959 : 154386821 : if (cxx_dialect >= cxx11
10960 : 306950454 : && DECL_DESTRUCTOR_P (decl)
10961 : 2799605 : && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10962 : 154548422 : && !processing_template_decl)
10963 : 81652 : deduce_noexcept_on_destructor (decl);
10964 : :
10965 : 154386821 : set_originating_module (decl);
10966 : :
10967 : 463160463 : decl = check_explicit_specialization (orig_declarator, decl,
10968 : : template_count,
10969 : 308773642 : 2 * funcdef_flag +
10970 : 154386821 : 4 * (friendp != 0) +
10971 : 154386821 : 8 * concept_p,
10972 : : *attrlist);
10973 : 154386821 : if (decl == error_mark_node)
10974 : : return NULL_TREE;
10975 : :
10976 : 154386541 : if (DECL_STATIC_FUNCTION_P (decl))
10977 : 8214993 : check_static_quals (decl, quals);
10978 : :
10979 : 154386541 : if (attrlist)
10980 : : {
10981 : 154386541 : cplus_decl_attributes (&decl, *attrlist, 0);
10982 : 154386541 : *attrlist = NULL_TREE;
10983 : : }
10984 : :
10985 : 154386541 : if (DECL_HAS_CONTRACTS_P (decl))
10986 : 449 : rebuild_postconditions (decl);
10987 : :
10988 : : /* Check main's type after attributes have been applied. */
10989 : 154386541 : if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10990 : : {
10991 : 34842 : if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10992 : : integer_type_node))
10993 : : {
10994 : 26 : tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10995 : 26 : tree newtype;
10996 : 26 : error_at (declspecs->locations[ds_type_spec],
10997 : : "%<::main%> must return %<int%>");
10998 : 26 : newtype = build_function_type (integer_type_node, oldtypeargs);
10999 : 26 : TREE_TYPE (decl) = newtype;
11000 : : }
11001 : 34842 : if (warn_main)
11002 : 34820 : check_main_parameter_types (decl);
11003 : : }
11004 : :
11005 : 154386541 : if (ctype != NULL_TREE && check)
11006 : : {
11007 : 8643138 : tree old_decl = check_classfn (ctype, decl,
11008 : 8643138 : (current_template_depth
11009 : 8643138 : > template_class_depth (ctype))
11010 : 2015946 : ? current_template_parms
11011 : : : NULL_TREE);
11012 : :
11013 : 8643138 : if (old_decl == error_mark_node)
11014 : : return NULL_TREE;
11015 : :
11016 : 8643041 : if (old_decl)
11017 : : {
11018 : 8254428 : tree ok;
11019 : 8254428 : tree pushed_scope;
11020 : :
11021 : 8254428 : if (TREE_CODE (old_decl) == TEMPLATE_DECL)
11022 : : /* Because grokfndecl is always supposed to return a
11023 : : FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
11024 : : here. We depend on our callers to figure out that its
11025 : : really a template that's being returned. */
11026 : 2015930 : old_decl = DECL_TEMPLATE_RESULT (old_decl);
11027 : :
11028 : 8254428 : if (DECL_STATIC_FUNCTION_P (old_decl)
11029 : 8254428 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
11030 : : {
11031 : : /* Remove the `this' parm added by grokclassfn. */
11032 : 143317 : revert_static_member_fn (decl);
11033 : 143317 : check_static_quals (decl, quals);
11034 : : }
11035 : 8254428 : if (DECL_ARTIFICIAL (old_decl))
11036 : : {
11037 : 12 : error ("definition of implicitly-declared %qD", old_decl);
11038 : 12 : return NULL_TREE;
11039 : : }
11040 : 8254416 : else if (DECL_DEFAULTED_FN (old_decl))
11041 : : {
11042 : 3 : error ("definition of explicitly-defaulted %q+D", decl);
11043 : 3 : inform (DECL_SOURCE_LOCATION (old_decl),
11044 : : "%q#D explicitly defaulted here", old_decl);
11045 : 3 : return NULL_TREE;
11046 : : }
11047 : :
11048 : : /* Since we've smashed OLD_DECL to its
11049 : : DECL_TEMPLATE_RESULT, we must do the same to DECL. */
11050 : 8254413 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11051 : 937 : decl = DECL_TEMPLATE_RESULT (decl);
11052 : :
11053 : : /* Attempt to merge the declarations. This can fail, in
11054 : : the case of some invalid specialization declarations. */
11055 : 8254413 : pushed_scope = push_scope (ctype);
11056 : 8254413 : ok = duplicate_decls (decl, old_decl);
11057 : 8254413 : if (pushed_scope)
11058 : 8254413 : pop_scope (pushed_scope);
11059 : 8254413 : if (!ok)
11060 : : {
11061 : 0 : error ("no %q#D member function declared in class %qT",
11062 : : decl, ctype);
11063 : 0 : return NULL_TREE;
11064 : : }
11065 : 8254413 : if (ok == error_mark_node)
11066 : : return NULL_TREE;
11067 : : return old_decl;
11068 : : }
11069 : : }
11070 : :
11071 : 292264032 : if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
11072 : : return NULL_TREE;
11073 : :
11074 : 146132012 : if (ctype == NULL_TREE || check)
11075 : 64244083 : return decl;
11076 : :
11077 : 81887929 : if (virtualp)
11078 : 3811100 : DECL_VIRTUAL_P (decl) = 1;
11079 : :
11080 : 81887929 : return decl;
11081 : : }
11082 : :
11083 : : /* decl is a FUNCTION_DECL.
11084 : : specifiers are the parsed virt-specifiers.
11085 : :
11086 : : Set flags to reflect the virt-specifiers.
11087 : :
11088 : : Returns decl. */
11089 : :
11090 : : static tree
11091 : 85251023 : set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
11092 : : {
11093 : 85251023 : if (decl == NULL_TREE)
11094 : : return decl;
11095 : 85250906 : if (specifiers & VIRT_SPEC_OVERRIDE)
11096 : 144325 : DECL_OVERRIDE_P (decl) = 1;
11097 : 85250906 : if (specifiers & VIRT_SPEC_FINAL)
11098 : 2365 : DECL_FINAL_P (decl) = 1;
11099 : : return decl;
11100 : : }
11101 : :
11102 : : /* DECL is a VAR_DECL for a static data member. Set flags to reflect
11103 : : the linkage that DECL will receive in the object file. */
11104 : :
11105 : : static void
11106 : 9500014 : set_linkage_for_static_data_member (tree decl)
11107 : : {
11108 : : /* A static data member always has static storage duration and
11109 : : external linkage. Note that static data members are forbidden in
11110 : : local classes -- the only situation in which a class has
11111 : : non-external linkage. */
11112 : 9500014 : TREE_PUBLIC (decl) = 1;
11113 : 9500014 : TREE_STATIC (decl) = 1;
11114 : : /* For non-template classes, static data members are always put
11115 : : out in exactly those files where they are defined, just as
11116 : : with ordinary namespace-scope variables. */
11117 : 9500014 : if (!processing_template_decl)
11118 : 7153148 : DECL_INTERFACE_KNOWN (decl) = 1;
11119 : 9500014 : }
11120 : :
11121 : : /* Create a VAR_DECL named NAME with the indicated TYPE.
11122 : :
11123 : : If SCOPE is non-NULL, it is the class type or namespace containing
11124 : : the variable. If SCOPE is NULL, the variable should is created in
11125 : : the innermost enclosing scope. */
11126 : :
11127 : : static tree
11128 : 51170799 : grokvardecl (tree type,
11129 : : tree name,
11130 : : tree orig_declarator,
11131 : : const cp_decl_specifier_seq *declspecs,
11132 : : int initialized,
11133 : : int type_quals,
11134 : : int inlinep,
11135 : : bool conceptp,
11136 : : int template_count,
11137 : : tree scope,
11138 : : location_t location)
11139 : : {
11140 : 51170799 : tree decl;
11141 : 51170799 : tree explicit_scope;
11142 : :
11143 : 51170799 : gcc_assert (!name || identifier_p (name));
11144 : :
11145 : 51170799 : bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
11146 : 51170799 : bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
11147 : :
11148 : : /* Compute the scope in which to place the variable, but remember
11149 : : whether or not that scope was explicitly specified by the user. */
11150 : 51170799 : explicit_scope = scope;
11151 : 51170799 : if (!scope)
11152 : : {
11153 : : /* An explicit "extern" specifier indicates a namespace-scope
11154 : : variable. */
11155 : 50809649 : if (declspecs->storage_class == sc_extern)
11156 : 541496 : scope = current_decl_namespace ();
11157 : 50268153 : else if (!at_function_scope_p ())
11158 : 3749180 : scope = current_scope ();
11159 : : }
11160 : :
11161 : 50809649 : if (scope
11162 : 4651826 : && (/* If the variable is a namespace-scope variable declared in a
11163 : : template, we need DECL_LANG_SPECIFIC. */
11164 : 4651826 : (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
11165 : : /* Similarly for namespace-scope variables with language linkage
11166 : : other than C++. */
11167 : 4293232 : || (TREE_CODE (scope) == NAMESPACE_DECL
11168 : 2199113 : && current_lang_name != lang_name_cplusplus)
11169 : : /* Similarly for static data members. */
11170 : 2333193 : || TYPE_P (scope)
11171 : : /* Similarly for explicit specializations. */
11172 : 1974599 : || (orig_declarator
11173 : 1974374 : && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
11174 : 2940804 : decl = build_lang_decl_loc (location, VAR_DECL, name, type);
11175 : : else
11176 : 48229995 : decl = build_decl (location, VAR_DECL, name, type);
11177 : :
11178 : 51170799 : if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
11179 : 2556 : set_decl_namespace (decl, explicit_scope, 0);
11180 : : else
11181 : 51168243 : DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
11182 : :
11183 : 51170799 : if (declspecs->storage_class == sc_extern)
11184 : : {
11185 : 541496 : DECL_THIS_EXTERN (decl) = 1;
11186 : 541496 : DECL_EXTERNAL (decl) = !initialized;
11187 : : }
11188 : :
11189 : 51170799 : if (DECL_CLASS_SCOPE_P (decl))
11190 : : {
11191 : 358594 : set_linkage_for_static_data_member (decl);
11192 : : /* This function is only called with out-of-class definitions. */
11193 : 358594 : DECL_EXTERNAL (decl) = 0;
11194 : 358594 : check_class_member_definition_namespace (decl);
11195 : : }
11196 : : /* At top level, either `static' or no s.c. makes a definition
11197 : : (perhaps tentative), and absence of `static' makes it public. */
11198 : 50812205 : else if (toplevel_bindings_p ())
11199 : : {
11200 : 8583614 : TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
11201 : 4291807 : && (DECL_THIS_EXTERN (decl)
11202 : 3661349 : || ! constp
11203 : 3661349 : || volatilep
11204 : 3587038 : || inlinep
11205 : 137184 : || module_attach_p ()));
11206 : 4291807 : TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
11207 : : }
11208 : : /* Not at top level, only `static' makes a static definition. */
11209 : : else
11210 : : {
11211 : 46520398 : TREE_STATIC (decl) = declspecs->storage_class == sc_static;
11212 : 46520398 : TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
11213 : : }
11214 : :
11215 : 51170799 : set_originating_module (decl);
11216 : :
11217 : 51170799 : if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
11218 : : {
11219 : 19390 : if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
11220 : : {
11221 : 19380 : CP_DECL_THREAD_LOCAL_P (decl) = true;
11222 : 19380 : if (!processing_template_decl)
11223 : 19350 : set_decl_tls_model (decl, decl_default_tls_model (decl));
11224 : : }
11225 : 19390 : if (declspecs->gnu_thread_keyword_p)
11226 : 18665 : SET_DECL_GNU_TLS_P (decl);
11227 : : }
11228 : :
11229 : : /* If the type of the decl has no linkage, make sure that we'll
11230 : : notice that in mark_used. */
11231 : 51170799 : if (cxx_dialect > cxx98
11232 : 50931631 : && decl_linkage (decl) != lk_none
11233 : 50432473 : && DECL_LANG_SPECIFIC (decl) == NULL
11234 : 47379393 : && !DECL_EXTERN_C_P (decl)
11235 : 98550192 : && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
11236 : 3505 : retrofit_lang_decl (decl);
11237 : :
11238 : 51170799 : if (TREE_PUBLIC (decl))
11239 : : {
11240 : : /* [basic.link]: A name with no linkage (notably, the name of a class
11241 : : or enumeration declared in a local scope) shall not be used to
11242 : : declare an entity with linkage.
11243 : :
11244 : : DR 757 relaxes this restriction for C++0x. */
11245 : 4424274 : if (cxx_dialect < cxx11)
11246 : 19946 : no_linkage_error (decl);
11247 : : }
11248 : : else
11249 : 46746525 : DECL_INTERFACE_KNOWN (decl) = 1;
11250 : :
11251 : 51170799 : if (DECL_NAME (decl)
11252 : 50664624 : && MAIN_NAME_P (DECL_NAME (decl))
11253 : 51170811 : && scope == global_namespace)
11254 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
11255 : : "cannot declare %<::main%> to be a global variable");
11256 : :
11257 : : /* Check that the variable can be safely declared as a concept.
11258 : : Note that this also forbids explicit specializations. */
11259 : 51170799 : if (conceptp)
11260 : : {
11261 : 127 : if (!processing_template_decl)
11262 : : {
11263 : 6 : error_at (declspecs->locations[ds_concept],
11264 : : "a non-template variable cannot be %<concept%>");
11265 : 6 : return NULL_TREE;
11266 : : }
11267 : 121 : else if (!at_namespace_scope_p ())
11268 : : {
11269 : 1 : error_at (declspecs->locations[ds_concept],
11270 : : "concept must be defined at namespace scope");
11271 : 1 : return NULL_TREE;
11272 : : }
11273 : : else
11274 : 120 : DECL_DECLARED_CONCEPT_P (decl) = true;
11275 : 120 : if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
11276 : 1 : error_at (declspecs->locations[ds_type_spec],
11277 : : "concept must have type %<bool%>");
11278 : 120 : if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
11279 : : {
11280 : 1 : error_at (location, "a variable concept cannot be constrained");
11281 : 1 : TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
11282 : : }
11283 : : }
11284 : 51170672 : else if (flag_concepts
11285 : 51170672 : && current_template_depth > template_class_depth (scope))
11286 : : {
11287 : 11805656 : tree ci = current_template_constraints ();
11288 : 11805656 : set_constraints (decl, ci);
11289 : : }
11290 : :
11291 : : // Handle explicit specializations and instantiations of variable templates.
11292 : 51170792 : if (orig_declarator)
11293 : 50664617 : decl = check_explicit_specialization (orig_declarator, decl,
11294 : : template_count, conceptp * 8);
11295 : :
11296 : 51170792 : return decl != error_mark_node ? decl : NULL_TREE;
11297 : : }
11298 : :
11299 : : /* Create and return a canonical pointer to member function type, for
11300 : : TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
11301 : :
11302 : : tree
11303 : 3424212 : build_ptrmemfunc_type (tree type)
11304 : : {
11305 : 3424212 : tree field, fields;
11306 : 3424212 : tree t;
11307 : :
11308 : 3424212 : if (type == error_mark_node)
11309 : : return type;
11310 : :
11311 : : /* Make sure that we always have the unqualified pointer-to-member
11312 : : type first. */
11313 : 3424212 : if (cp_cv_quals quals = cp_type_quals (type))
11314 : : {
11315 : 395 : tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
11316 : 395 : return cp_build_qualified_type (unqual, quals);
11317 : : }
11318 : :
11319 : : /* If a canonical type already exists for this type, use it. We use
11320 : : this method instead of type_hash_canon, because it only does a
11321 : : simple equality check on the list of field members. */
11322 : :
11323 : 3423817 : t = TYPE_PTRMEMFUNC_TYPE (type);
11324 : 3423817 : if (t)
11325 : : return t;
11326 : :
11327 : 1070276 : t = make_node (RECORD_TYPE);
11328 : :
11329 : : /* Let the front end know this is a pointer to member function. */
11330 : 1070276 : TYPE_PTRMEMFUNC_FLAG (t) = 1;
11331 : :
11332 : 1070276 : field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
11333 : 1070276 : DECL_NONADDRESSABLE_P (field) = 1;
11334 : 1070276 : fields = field;
11335 : :
11336 : 1070276 : field = build_decl (input_location, FIELD_DECL, delta_identifier,
11337 : : delta_type_node);
11338 : 1070276 : DECL_NONADDRESSABLE_P (field) = 1;
11339 : 1070276 : DECL_CHAIN (field) = fields;
11340 : 1070276 : fields = field;
11341 : :
11342 : 1070276 : finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
11343 : :
11344 : : /* Zap out the name so that the back end will give us the debugging
11345 : : information for this anonymous RECORD_TYPE. */
11346 : 1070276 : TYPE_NAME (t) = NULL_TREE;
11347 : :
11348 : : /* Cache this pointer-to-member type so that we can find it again
11349 : : later. */
11350 : 1070276 : TYPE_PTRMEMFUNC_TYPE (type) = t;
11351 : :
11352 : 1070276 : if (TYPE_STRUCTURAL_EQUALITY_P (type))
11353 : 579605 : SET_TYPE_STRUCTURAL_EQUALITY (t);
11354 : 490671 : else if (TYPE_CANONICAL (type) != type)
11355 : 317335 : TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
11356 : :
11357 : : return t;
11358 : : }
11359 : :
11360 : : /* Create and return a pointer to data member type. */
11361 : :
11362 : : tree
11363 : 296088 : build_ptrmem_type (tree class_type, tree member_type)
11364 : : {
11365 : 296088 : if (TREE_CODE (member_type) == METHOD_TYPE)
11366 : : {
11367 : 59288 : cp_cv_quals quals = type_memfn_quals (member_type);
11368 : 59288 : cp_ref_qualifier rqual = type_memfn_rqual (member_type);
11369 : 59288 : member_type = build_memfn_type (member_type, class_type, quals, rqual);
11370 : 59288 : return build_ptrmemfunc_type (build_pointer_type (member_type));
11371 : : }
11372 : : else
11373 : : {
11374 : 236800 : gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
11375 : 236800 : return build_offset_type (class_type, member_type);
11376 : : }
11377 : : }
11378 : :
11379 : : /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
11380 : : Check to see that the definition is valid. Issue appropriate error
11381 : : messages. */
11382 : :
11383 : : static void
11384 : 12133562 : check_static_variable_definition (tree decl, tree type)
11385 : : {
11386 : : /* Avoid redundant diagnostics on out-of-class definitions. */
11387 : 12133562 : if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
11388 : : ;
11389 : : /* Can't check yet if we don't know the type. */
11390 : 11280744 : else if (dependent_type_p (type))
11391 : : ;
11392 : : /* If DECL is declared constexpr, we'll do the appropriate checks
11393 : : in check_initializer. Similarly for inline static data members. */
11394 : 11085482 : else if (DECL_P (decl)
11395 : 11085482 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11396 : 2239525 : || DECL_VAR_DECLARED_INLINE_P (decl)))
11397 : : ;
11398 : 2239196 : else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11399 : : {
11400 : 66 : if (!COMPLETE_TYPE_P (type))
11401 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
11402 : : "in-class initialization of static data member %q#D of "
11403 : : "incomplete type", decl);
11404 : 51 : else if (literal_type_p (type))
11405 : 45 : permerror (DECL_SOURCE_LOCATION (decl),
11406 : : "%<constexpr%> needed for in-class initialization of "
11407 : : "static data member %q#D of non-integral type", decl);
11408 : : else
11409 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
11410 : : "in-class initialization of static data member %q#D of "
11411 : : "non-literal type", decl);
11412 : : }
11413 : : /* Motion 10 at San Diego: If a static const integral data member is
11414 : : initialized with an integral constant expression, the initializer
11415 : : may appear either in the declaration (within the class), or in
11416 : : the definition, but not both. If it appears in the class, the
11417 : : member is a member constant. The file-scope definition is always
11418 : : required. */
11419 : 2239130 : else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
11420 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
11421 : : "invalid in-class initialization of static data member "
11422 : : "of non-integral type %qT",
11423 : : type);
11424 : 2239119 : else if (!CP_TYPE_CONST_P (type))
11425 : 5 : error_at (DECL_SOURCE_LOCATION (decl),
11426 : : "ISO C++ forbids in-class initialization of non-const "
11427 : : "static member %qD",
11428 : : decl);
11429 : 2239114 : else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
11430 : 5 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
11431 : : "ISO C++ forbids initialization of member constant "
11432 : : "%qD of non-integral type %qT", decl, type);
11433 : 12133562 : }
11434 : :
11435 : : /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
11436 : : SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
11437 : : expressions out into temporary variables so that walk_tree doesn't
11438 : : step into them (c++/15764). */
11439 : :
11440 : : static tree
11441 : 3666 : stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
11442 : : {
11443 : 3666 : hash_set<tree> *pset = (hash_set<tree> *)data;
11444 : 3666 : tree expr = *expr_p;
11445 : 3666 : if (TREE_CODE (expr) == SAVE_EXPR)
11446 : : {
11447 : 1559 : tree op = TREE_OPERAND (expr, 0);
11448 : 1559 : cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
11449 : 1559 : if (TREE_SIDE_EFFECTS (op))
11450 : 69 : TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
11451 : 1559 : *walk_subtrees = 0;
11452 : : }
11453 : 2107 : else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
11454 : 1884 : *walk_subtrees = 0;
11455 : 3666 : return NULL;
11456 : : }
11457 : :
11458 : : /* Entry point for the above. */
11459 : :
11460 : : static void
11461 : 1630 : stabilize_vla_size (tree size)
11462 : : {
11463 : 1630 : hash_set<tree> pset;
11464 : : /* Break out any function calls into temporary variables. */
11465 : 1630 : cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
11466 : 1630 : }
11467 : :
11468 : : /* Reduce a SIZEOF_EXPR to its value. */
11469 : :
11470 : : tree
11471 : 10277346 : fold_sizeof_expr (tree t)
11472 : : {
11473 : 10277346 : tree r;
11474 : 10277346 : if (SIZEOF_EXPR_TYPE_P (t))
11475 : 9625513 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11476 : 9625513 : TREE_TYPE (TREE_OPERAND (t, 0)),
11477 : : SIZEOF_EXPR, false, false);
11478 : 651833 : else if (TYPE_P (TREE_OPERAND (t, 0)))
11479 : 0 : r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11480 : 0 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
11481 : : false, false);
11482 : : else
11483 : 651833 : r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
11484 : 651833 : TREE_OPERAND (t, 0), SIZEOF_EXPR,
11485 : : false, false);
11486 : 10277346 : if (r == error_mark_node)
11487 : 60 : r = size_one_node;
11488 : 10277346 : return r;
11489 : : }
11490 : :
11491 : : /* Given the SIZE (i.e., number of elements) in an array, compute
11492 : : an appropriate index type for the array. If non-NULL, NAME is
11493 : : the name of the entity being declared. */
11494 : :
11495 : : static tree
11496 : 2547081 : compute_array_index_type_loc (location_t name_loc, tree name, tree size,
11497 : : tsubst_flags_t complain)
11498 : : {
11499 : 2547081 : if (error_operand_p (size))
11500 : 35 : return error_mark_node;
11501 : :
11502 : : /* The type of the index being computed. */
11503 : 2547046 : tree itype;
11504 : :
11505 : : /* The original numeric size as seen in the source code before
11506 : : conversion to size_t. */
11507 : 2547046 : tree origsize = size;
11508 : :
11509 : 2547046 : location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11510 : :
11511 : 2547046 : if (!type_dependent_expression_p (size))
11512 : : {
11513 : 2513681 : origsize = size = mark_rvalue_use (size);
11514 : :
11515 : 33583 : if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11516 : 2513854 : && TREE_SIDE_EFFECTS (size))
11517 : : /* In C++98, we mark a non-constant array bound with a magic
11518 : : NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11519 : : else
11520 : : {
11521 : 2513680 : size = build_converted_constant_expr (size_type_node, size, complain);
11522 : : /* Pedantically a constant expression is required here and so
11523 : : __builtin_is_constant_evaluated () should fold to true if it
11524 : : is successfully folded into a constant. */
11525 : 2513680 : size = fold_non_dependent_expr (size, complain,
11526 : : /*manifestly_const_eval=*/true);
11527 : :
11528 : 2513680 : if (!TREE_CONSTANT (size))
11529 : 87913 : size = origsize;
11530 : : }
11531 : :
11532 : 2513681 : if (error_operand_p (size))
11533 : 3 : return error_mark_node;
11534 : :
11535 : : /* The array bound must be an integer type. */
11536 : 2513678 : tree type = TREE_TYPE (size);
11537 : 2513678 : if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11538 : : {
11539 : 25 : if (!(complain & tf_error))
11540 : 0 : return error_mark_node;
11541 : 25 : if (name)
11542 : 21 : error_at (loc, "size of array %qD has non-integral type %qT",
11543 : : name, type);
11544 : : else
11545 : 4 : error_at (loc, "size of array has non-integral type %qT", type);
11546 : 25 : size = integer_one_node;
11547 : : }
11548 : : }
11549 : :
11550 : : /* A type is dependent if it is...an array type constructed from any
11551 : : dependent type or whose size is specified by a constant expression
11552 : : that is value-dependent. */
11553 : : /* We can only call value_dependent_expression_p on integral constant
11554 : : expressions. */
11555 : 2547043 : if (processing_template_decl
11556 : 1095214 : && potential_constant_expression (size)
11557 : 3642229 : && value_dependent_expression_p (size))
11558 : : {
11559 : : /* Just build the index type and mark that it requires
11560 : : structural equality checks. */
11561 : 657354 : in_template:
11562 : 657354 : itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11563 : : size, size_one_node));
11564 : 657354 : TYPE_DEPENDENT_P (itype) = 1;
11565 : 657354 : TYPE_DEPENDENT_P_VALID (itype) = 1;
11566 : 657354 : SET_TYPE_STRUCTURAL_EQUALITY (itype);
11567 : 657354 : return itype;
11568 : : }
11569 : :
11570 : 1889758 : if (TREE_CODE (size) != INTEGER_CST)
11571 : : {
11572 : 2199 : tree folded = cp_fully_fold (size);
11573 : 2199 : if (TREE_CODE (folded) == INTEGER_CST)
11574 : : {
11575 : 307 : if (name)
11576 : 199 : pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11577 : : "integral constant-expression", name);
11578 : : else
11579 : 108 : pedwarn (loc, OPT_Wpedantic,
11580 : : "size of array is not an integral constant-expression");
11581 : : }
11582 : 2199 : if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11583 : : /* We might have lost the TREE_CONSTANT flag e.g. when we are
11584 : : folding a conversion from a pointer to integral type. In that
11585 : : case issue an error below and don't treat this as a VLA. */;
11586 : : else
11587 : : /* Use the folded result for VLAs, too; it will have resolved
11588 : : SIZEOF_EXPR. */
11589 : : size = folded;
11590 : : }
11591 : :
11592 : : /* Normally, the array-bound will be a constant. */
11593 : 1889758 : if (TREE_CODE (size) == INTEGER_CST)
11594 : : {
11595 : : /* The size to use in diagnostics that reflects the constant
11596 : : size used in the source, rather than SIZE massaged above. */
11597 : 1887866 : tree diagsize = size;
11598 : :
11599 : : /* If the original size before conversion to size_t was signed
11600 : : and negative, convert it to ssizetype to restore the sign. */
11601 : 1887866 : if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11602 : : && TREE_CODE (size) == INTEGER_CST
11603 : 1887866 : && tree_int_cst_sign_bit (size))
11604 : : {
11605 : 320 : diagsize = fold_convert (ssizetype, size);
11606 : :
11607 : : /* Clear the overflow bit that may have been set as a result
11608 : : of the conversion from the sizetype of the new size to
11609 : : ssizetype. */
11610 : 320 : TREE_OVERFLOW (diagsize) = false;
11611 : : }
11612 : :
11613 : : /* Verify that the array has a positive number of elements
11614 : : and issue the appropriate diagnostic if it doesn't. */
11615 : 1887866 : if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11616 : : {
11617 : 623 : if (!(complain & tf_error))
11618 : 11 : return error_mark_node;
11619 : 612 : size = integer_one_node;
11620 : : }
11621 : : /* As an extension we allow zero-sized arrays. */
11622 : 1887243 : else if (integer_zerop (size))
11623 : : {
11624 : 3190 : if (!(complain & tf_error))
11625 : : /* We must fail if performing argument deduction (as
11626 : : indicated by the state of complain), so that
11627 : : another substitution can be found. */
11628 : 8 : return error_mark_node;
11629 : 3182 : else if (name)
11630 : 3007 : pedwarn (loc, OPT_Wpedantic,
11631 : : "ISO C++ forbids zero-size array %qD", name);
11632 : : else
11633 : 175 : pedwarn (loc, OPT_Wpedantic,
11634 : : "ISO C++ forbids zero-size array");
11635 : : }
11636 : : }
11637 : 1892 : else if (TREE_CONSTANT (size)
11638 : : /* We don't allow VLAs at non-function scopes, or during
11639 : : tentative template substitution. */
11640 : 1879 : || !at_function_scope_p ()
11641 : 3593 : || !(complain & tf_error))
11642 : : {
11643 : 193 : if (!(complain & tf_error))
11644 : 138 : return error_mark_node;
11645 : : /* `(int) &fn' is not a valid array bound. */
11646 : 55 : if (name)
11647 : 44 : error_at (loc,
11648 : : "size of array %qD is not an integral constant-expression",
11649 : : name);
11650 : : else
11651 : 11 : error_at (loc, "size of array is not an integral constant-expression");
11652 : 55 : size = integer_one_node;
11653 : : }
11654 : 1699 : else if (pedantic && warn_vla != 0)
11655 : : {
11656 : 39 : if (name)
11657 : 19 : pedwarn (name_loc, OPT_Wvla,
11658 : : "ISO C++ forbids variable length array %qD", name);
11659 : : else
11660 : 20 : pedwarn (input_location, OPT_Wvla,
11661 : : "ISO C++ forbids variable length array");
11662 : : }
11663 : 1660 : else if (warn_vla > 0)
11664 : : {
11665 : 4 : if (name)
11666 : 4 : warning_at (name_loc, OPT_Wvla,
11667 : : "variable length array %qD is used", name);
11668 : : else
11669 : 0 : warning (OPT_Wvla,
11670 : : "variable length array is used");
11671 : : }
11672 : :
11673 : 1889601 : if (processing_template_decl && !TREE_CONSTANT (size))
11674 : 69 : goto in_template;
11675 : : else
11676 : : {
11677 : 1889532 : if (!TREE_CONSTANT (size))
11678 : : {
11679 : : /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11680 : : of the MINUS_EXPR, which allows the -1 to get folded with the +1
11681 : : that happens when building TYPE_SIZE. */
11682 : 1630 : size = variable_size (size);
11683 : 1630 : stabilize_vla_size (size);
11684 : : }
11685 : :
11686 : : /* Compute the index of the largest element in the array. It is
11687 : : one less than the number of elements in the array. We save
11688 : : and restore PROCESSING_TEMPLATE_DECL so that computations in
11689 : : cp_build_binary_op will be appropriately folded. */
11690 : 1889532 : {
11691 : 1889532 : processing_template_decl_sentinel s;
11692 : 1889532 : itype = cp_build_binary_op (input_location,
11693 : : MINUS_EXPR,
11694 : : cp_convert (ssizetype, size, complain),
11695 : : cp_convert (ssizetype, integer_one_node,
11696 : : complain),
11697 : : complain);
11698 : 1889532 : itype = maybe_constant_value (itype, NULL_TREE, mce_true);
11699 : 1889532 : }
11700 : :
11701 : 1889532 : if (!TREE_CONSTANT (itype))
11702 : : {
11703 : 1630 : if (sanitize_flags_p (SANITIZE_VLA)
11704 : 1630 : && current_function_decl != NULL_TREE)
11705 : : {
11706 : : /* We have to add 1 -- in the ubsan routine we generate
11707 : : LE_EXPR rather than LT_EXPR. */
11708 : 92 : tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11709 : : build_one_cst (TREE_TYPE (itype)));
11710 : 92 : t = ubsan_instrument_vla (input_location, t);
11711 : 92 : finish_expr_stmt (t);
11712 : : }
11713 : : }
11714 : : /* Make sure that there was no overflow when creating to a signed
11715 : : index type. (For example, on a 32-bit machine, an array with
11716 : : size 2^32 - 1 is too big.) */
11717 : 1887902 : else if (TREE_CODE (itype) == INTEGER_CST
11718 : 1887902 : && TREE_OVERFLOW (itype))
11719 : : {
11720 : 0 : if (!(complain & tf_error))
11721 : 0 : return error_mark_node;
11722 : 0 : error ("overflow in array dimension");
11723 : 0 : TREE_OVERFLOW (itype) = 0;
11724 : : }
11725 : : }
11726 : :
11727 : : /* Create and return the appropriate index type. */
11728 : 1889532 : itype = build_index_type (itype);
11729 : :
11730 : : /* If the index type were dependent, we would have returned early, so
11731 : : remember that it isn't. */
11732 : 1889532 : TYPE_DEPENDENT_P (itype) = 0;
11733 : 1889532 : TYPE_DEPENDENT_P_VALID (itype) = 1;
11734 : 1889532 : return itype;
11735 : : }
11736 : :
11737 : : tree
11738 : 246222 : compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11739 : : {
11740 : 246222 : return compute_array_index_type_loc (input_location, name, size, complain);
11741 : : }
11742 : :
11743 : : /* Returns the scope (if any) in which the entity declared by
11744 : : DECLARATOR will be located. If the entity was declared with an
11745 : : unqualified name, NULL_TREE is returned. */
11746 : :
11747 : : tree
11748 : 627544196 : get_scope_of_declarator (const cp_declarator *declarator)
11749 : : {
11750 : 733420881 : while (declarator && declarator->kind != cdk_id)
11751 : 105876685 : declarator = declarator->declarator;
11752 : :
11753 : : /* If the declarator-id is a SCOPE_REF, the scope in which the
11754 : : declaration occurs is the first operand. */
11755 : 627544196 : if (declarator
11756 : 626328858 : && declarator->u.id.qualifying_scope)
11757 : : return declarator->u.id.qualifying_scope;
11758 : :
11759 : : /* Otherwise, the declarator is not a qualified name; the entity will
11760 : : be declared in the current scope. */
11761 : : return NULL_TREE;
11762 : : }
11763 : :
11764 : : /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11765 : : indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11766 : : with this type. */
11767 : :
11768 : : static tree
11769 : 3034634 : create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11770 : : {
11771 : 3034634 : tree itype = NULL_TREE;
11772 : :
11773 : : /* If things have already gone awry, bail now. */
11774 : 3034634 : if (type == error_mark_node || size == error_mark_node)
11775 : : return error_mark_node;
11776 : :
11777 : : /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11778 : : for a deduced class type. */
11779 : 3034558 : if (template_placeholder_p (type))
11780 : : {
11781 : 4 : if (name)
11782 : 0 : error_at (loc, "%qD declared as array of template placeholder "
11783 : : "type %qT", name, type);
11784 : : else
11785 : 4 : error ("creating array of template placeholder type %qT", type);
11786 : 4 : return error_mark_node;
11787 : : }
11788 : :
11789 : : /* If there are some types which cannot be array elements,
11790 : : issue an error-message and return. */
11791 : 3034554 : switch (TREE_CODE (type))
11792 : : {
11793 : 4 : case VOID_TYPE:
11794 : 4 : if (name)
11795 : 0 : error_at (loc, "declaration of %qD as array of void", name);
11796 : : else
11797 : 4 : error ("creating array of void");
11798 : 4 : return error_mark_node;
11799 : :
11800 : 4 : case FUNCTION_TYPE:
11801 : 4 : if (name)
11802 : 4 : error_at (loc, "declaration of %qD as array of functions", name);
11803 : : else
11804 : 0 : error ("creating array of functions");
11805 : 4 : return error_mark_node;
11806 : :
11807 : 27 : case REFERENCE_TYPE:
11808 : 27 : if (name)
11809 : 19 : error_at (loc, "declaration of %qD as array of references", name);
11810 : : else
11811 : 8 : error ("creating array of references");
11812 : 27 : return error_mark_node;
11813 : :
11814 : 0 : case METHOD_TYPE:
11815 : 0 : if (name)
11816 : 0 : error_at (loc, "declaration of %qD as array of function members",
11817 : : name);
11818 : : else
11819 : 0 : error ("creating array of function members");
11820 : 0 : return error_mark_node;
11821 : :
11822 : 3034519 : default:
11823 : 3034519 : break;
11824 : : }
11825 : :
11826 : 3034519 : if (!verify_type_context (name ? loc : input_location,
11827 : : TCTX_ARRAY_ELEMENT, type))
11828 : 0 : return error_mark_node;
11829 : :
11830 : : /* [dcl.array]
11831 : :
11832 : : The constant expressions that specify the bounds of the arrays
11833 : : can be omitted only for the first member of the sequence. */
11834 : 3034519 : if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11835 : : {
11836 : 40 : if (name)
11837 : 32 : error_at (loc, "declaration of %qD as multidimensional array must "
11838 : : "have bounds for all dimensions except the first",
11839 : : name);
11840 : : else
11841 : 8 : error ("multidimensional array must have bounds for all "
11842 : : "dimensions except the first");
11843 : :
11844 : 40 : return error_mark_node;
11845 : : }
11846 : :
11847 : : /* Figure out the index type for the array. */
11848 : 3034479 : if (size)
11849 : : {
11850 : 2300859 : itype = compute_array_index_type_loc (loc, name, size,
11851 : : tf_warning_or_error);
11852 : 2300859 : if (type_uses_auto (type)
11853 : 2300859 : && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11854 : : {
11855 : 9 : sorry_at (loc, "variable-length array of %<auto%>");
11856 : 9 : return error_mark_node;
11857 : : }
11858 : : }
11859 : :
11860 : 3034470 : return build_cplus_array_type (type, itype);
11861 : : }
11862 : :
11863 : : /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11864 : :
11865 : : static location_t
11866 : 839055483 : min_location (location_t loca, location_t locb)
11867 : : {
11868 : 839055483 : if (loca == UNKNOWN_LOCATION
11869 : 839055483 : || (locb != UNKNOWN_LOCATION
11870 : 93382268 : && linemap_location_before_p (line_table, locb, loca)))
11871 : 746829304 : return locb;
11872 : : return loca;
11873 : : }
11874 : :
11875 : : /* Returns the smallest location != UNKNOWN_LOCATION among the
11876 : : three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11877 : : and LOCATIONS[ds_restrict]. */
11878 : :
11879 : : static location_t
11880 : 837600052 : smallest_type_quals_location (int type_quals, const location_t* locations)
11881 : : {
11882 : 837600052 : location_t loc = UNKNOWN_LOCATION;
11883 : :
11884 : 837600052 : if (type_quals & TYPE_QUAL_CONST)
11885 : 92125033 : loc = locations[ds_const];
11886 : :
11887 : 837600052 : if (type_quals & TYPE_QUAL_VOLATILE)
11888 : 1455312 : loc = min_location (loc, locations[ds_volatile]);
11889 : :
11890 : 837600052 : if (type_quals & TYPE_QUAL_RESTRICT)
11891 : 51 : loc = min_location (loc, locations[ds_restrict]);
11892 : :
11893 : 837600052 : return loc;
11894 : : }
11895 : :
11896 : : /* Returns the smallest among the latter and locations[ds_type_spec]. */
11897 : :
11898 : : static location_t
11899 : 837600028 : smallest_type_location (int type_quals, const location_t* locations)
11900 : : {
11901 : 837600028 : location_t loc = smallest_type_quals_location (type_quals, locations);
11902 : 837600028 : return min_location (loc, locations[ds_type_spec]);
11903 : : }
11904 : :
11905 : : static location_t
11906 : 31 : smallest_type_location (const cp_decl_specifier_seq *declspecs)
11907 : : {
11908 : 31 : int type_quals = get_type_quals (declspecs);
11909 : 31 : return smallest_type_location (type_quals, declspecs->locations);
11910 : : }
11911 : :
11912 : : /* Check that it's OK to declare a function with the indicated TYPE
11913 : : and TYPE_QUALS. SFK indicates the kind of special function (if any)
11914 : : that this function is. OPTYPE is the type given in a conversion
11915 : : operator declaration, or the class type for a constructor/destructor.
11916 : : Returns the actual return type of the function; that may be different
11917 : : than TYPE if an error occurs, or for certain special functions. */
11918 : :
11919 : : static tree
11920 : 24215440 : check_special_function_return_type (special_function_kind sfk,
11921 : : tree type,
11922 : : tree optype,
11923 : : int type_quals,
11924 : : const location_t* locations)
11925 : : {
11926 : 24215440 : switch (sfk)
11927 : : {
11928 : 20068215 : case sfk_constructor:
11929 : 20068215 : if (type)
11930 : 32 : error_at (smallest_type_location (type_quals, locations),
11931 : : "return type specification for constructor invalid");
11932 : 20068183 : else if (type_quals != TYPE_UNQUALIFIED)
11933 : 20 : error_at (smallest_type_quals_location (type_quals, locations),
11934 : : "qualifiers are not allowed on constructor declaration");
11935 : :
11936 : 20068215 : if (targetm.cxx.cdtor_returns_this ())
11937 : 0 : type = build_pointer_type (optype);
11938 : : else
11939 : 20068215 : type = void_type_node;
11940 : : break;
11941 : :
11942 : 2816670 : case sfk_destructor:
11943 : 2816670 : if (type)
11944 : 28 : error_at (smallest_type_location (type_quals, locations),
11945 : : "return type specification for destructor invalid");
11946 : 2816642 : else if (type_quals != TYPE_UNQUALIFIED)
11947 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
11948 : : "qualifiers are not allowed on destructor declaration");
11949 : :
11950 : : /* We can't use the proper return type here because we run into
11951 : : problems with ambiguous bases and covariant returns. */
11952 : 2816670 : if (targetm.cxx.cdtor_returns_this ())
11953 : 0 : type = build_pointer_type (void_type_node);
11954 : : else
11955 : 2816670 : type = void_type_node;
11956 : : break;
11957 : :
11958 : 519180 : case sfk_conversion:
11959 : 519180 : if (type)
11960 : 36 : error_at (smallest_type_location (type_quals, locations),
11961 : : "return type specified for %<operator %T%>", optype);
11962 : 519144 : else if (type_quals != TYPE_UNQUALIFIED)
11963 : 4 : error_at (smallest_type_quals_location (type_quals, locations),
11964 : : "qualifiers are not allowed on declaration of "
11965 : : "%<operator %T%>", optype);
11966 : :
11967 : : type = optype;
11968 : : break;
11969 : :
11970 : 811375 : case sfk_deduction_guide:
11971 : 811375 : if (type)
11972 : 0 : error_at (smallest_type_location (type_quals, locations),
11973 : : "return type specified for deduction guide");
11974 : 811375 : else if (type_quals != TYPE_UNQUALIFIED)
11975 : 0 : error_at (smallest_type_quals_location (type_quals, locations),
11976 : : "qualifiers are not allowed on declaration of "
11977 : : "deduction guide");
11978 : 811375 : if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11979 : : {
11980 : 2 : error ("template template parameter %qT in declaration of "
11981 : : "deduction guide", optype);
11982 : 2 : type = error_mark_node;
11983 : : }
11984 : : else
11985 : 811373 : type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11986 : 21907125 : for (int i = 0; i < ds_last; ++i)
11987 : 21095750 : if (i != ds_explicit && locations[i])
11988 : 2 : error_at (locations[i],
11989 : : "%<decl-specifier%> in declaration of deduction guide");
11990 : : break;
11991 : :
11992 : 0 : default:
11993 : 0 : gcc_unreachable ();
11994 : : }
11995 : :
11996 : 24215440 : return type;
11997 : : }
11998 : :
11999 : : /* A variable or data member (whose unqualified name is IDENTIFIER)
12000 : : has been declared with the indicated TYPE. If the TYPE is not
12001 : : acceptable, issue an error message and return a type to use for
12002 : : error-recovery purposes. */
12003 : :
12004 : : tree
12005 : 256972019 : check_var_type (tree identifier, tree type, location_t loc)
12006 : : {
12007 : 256972019 : if (VOID_TYPE_P (type))
12008 : : {
12009 : 100 : if (!identifier)
12010 : 0 : error_at (loc, "unnamed variable or field declared void");
12011 : 100 : else if (identifier_p (identifier))
12012 : : {
12013 : 100 : gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
12014 : 100 : error_at (loc, "variable or field %qE declared void",
12015 : : identifier);
12016 : : }
12017 : : else
12018 : 0 : error_at (loc, "variable or field declared void");
12019 : 100 : type = error_mark_node;
12020 : : }
12021 : :
12022 : 256972019 : return type;
12023 : : }
12024 : :
12025 : : /* Handle declaring DECL as an inline variable. */
12026 : :
12027 : : static void
12028 : 3485001 : mark_inline_variable (tree decl, location_t loc)
12029 : : {
12030 : 3485001 : bool inlinep = true;
12031 : 3485001 : if (! toplevel_bindings_p ())
12032 : : {
12033 : 6 : error_at (loc, "%<inline%> specifier invalid for variable "
12034 : : "%qD declared at block scope", decl);
12035 : 6 : inlinep = false;
12036 : : }
12037 : 3484995 : else if (cxx_dialect < cxx17)
12038 : 60 : pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
12039 : : "with %<-std=c++17%> or %<-std=gnu++17%>");
12040 : 66 : if (inlinep)
12041 : : {
12042 : 3484995 : retrofit_lang_decl (decl);
12043 : 3484995 : SET_DECL_VAR_DECLARED_INLINE_P (decl);
12044 : : }
12045 : 3485001 : }
12046 : :
12047 : :
12048 : : /* Assign a typedef-given name to a class or enumeration type declared
12049 : : as anonymous at first. This was split out of grokdeclarator
12050 : : because it is also used in libcc1. */
12051 : :
12052 : : void
12053 : 367421 : name_unnamed_type (tree type, tree decl)
12054 : : {
12055 : 1102263 : gcc_assert (TYPE_UNNAMED_P (type));
12056 : :
12057 : : /* Replace the anonymous decl with the real decl. Be careful not to
12058 : : rename other typedefs (such as the self-reference) of type. */
12059 : 367421 : tree orig = TYPE_NAME (type);
12060 : 1100623 : for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12061 : 733202 : if (TYPE_NAME (t) == orig)
12062 : 367421 : TYPE_NAME (t) = decl;
12063 : :
12064 : : /* If this is a typedef within a template class, the nested
12065 : : type is a (non-primary) template. The name for the
12066 : : template needs updating as well. */
12067 : 367421 : if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
12068 : 63 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
12069 : :
12070 : : /* Adjust linkage now that we aren't unnamed anymore. */
12071 : 367421 : reset_type_linkage (type);
12072 : :
12073 : : /* FIXME remangle member functions; member functions of a
12074 : : type with external linkage have external linkage. */
12075 : :
12076 : : /* Check that our job is done, and that it would fail if we
12077 : : attempted to do it again. */
12078 : 734842 : gcc_assert (!TYPE_UNNAMED_P (type));
12079 : 367421 : }
12080 : :
12081 : : /* Check that decltype(auto) was well-formed: only plain decltype(auto)
12082 : : is allowed. TYPE might contain a decltype(auto). Returns true if
12083 : : there was a problem, false otherwise. */
12084 : :
12085 : : static bool
12086 : 531591949 : check_decltype_auto (location_t loc, tree type)
12087 : : {
12088 : 531591949 : if (tree a = type_uses_auto (type))
12089 : : {
12090 : 7744310 : if (AUTO_IS_DECLTYPE (a))
12091 : : {
12092 : 275713 : if (a != type)
12093 : : {
12094 : 33 : error_at (loc, "%qT as type rather than plain "
12095 : : "%<decltype(auto)%>", type);
12096 : 33 : return true;
12097 : : }
12098 : 275680 : else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
12099 : : {
12100 : 35 : error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
12101 : 35 : return true;
12102 : : }
12103 : : }
12104 : : }
12105 : : return false;
12106 : : }
12107 : :
12108 : : /* Given declspecs and a declarator (abstract or otherwise), determine
12109 : : the name and type of the object declared and construct a DECL node
12110 : : for it.
12111 : :
12112 : : DECLSPECS points to the representation of declaration-specifier
12113 : : sequence that precedes declarator.
12114 : :
12115 : : DECL_CONTEXT says which syntactic context this declaration is in:
12116 : : NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
12117 : : FUNCDEF for a function definition. Like NORMAL but a few different
12118 : : error messages in each case. Return value may be zero meaning
12119 : : this definition is too screwy to try to parse.
12120 : : MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
12121 : : handle member functions (which have FIELD context).
12122 : : Return value may be zero meaning this definition is too screwy to
12123 : : try to parse.
12124 : : PARM for a parameter declaration (either within a function prototype
12125 : : or before a function body). Make a PARM_DECL, or return void_type_node.
12126 : : TPARM for a template parameter declaration.
12127 : : CATCHPARM for a parameter declaration before a catch clause.
12128 : : TYPENAME if for a typename (in a cast or sizeof).
12129 : : Don't make a DECL node; just return the ..._TYPE node.
12130 : : FIELD for a struct or union field; make a FIELD_DECL.
12131 : : BITFIELD for a field with specified width.
12132 : :
12133 : : INITIALIZED is as for start_decl.
12134 : :
12135 : : ATTRLIST is a pointer to the list of attributes, which may be NULL
12136 : : if there are none; *ATTRLIST may be modified if attributes from inside
12137 : : the declarator should be applied to the declaration.
12138 : :
12139 : : When this function is called, scoping variables (such as
12140 : : CURRENT_CLASS_TYPE) should reflect the scope in which the
12141 : : declaration occurs, not the scope in which the new declaration will
12142 : : be placed. For example, on:
12143 : :
12144 : : void S::f() { ... }
12145 : :
12146 : : when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
12147 : : should not be `S'.
12148 : :
12149 : : Returns a DECL (if a declarator is present), a TYPE (if there is no
12150 : : declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
12151 : : error occurs. */
12152 : :
12153 : : tree
12154 : 837599901 : grokdeclarator (const cp_declarator *declarator,
12155 : : cp_decl_specifier_seq *declspecs,
12156 : : enum decl_context decl_context,
12157 : : int initialized,
12158 : : tree* attrlist)
12159 : : {
12160 : 837599901 : tree type = NULL_TREE;
12161 : 837599901 : int longlong = 0;
12162 : 837599901 : int explicit_intN = 0;
12163 : 837599901 : int int_n_alt = 0;
12164 : 837599901 : int virtualp, explicitp, friendp, inlinep, staticp;
12165 : 837599901 : int explicit_int = 0;
12166 : 837599901 : int explicit_char = 0;
12167 : 837599901 : int defaulted_int = 0;
12168 : :
12169 : 837599901 : tree typedef_decl = NULL_TREE;
12170 : 837599901 : const char *name = NULL;
12171 : 837599901 : tree typedef_type = NULL_TREE;
12172 : : /* True if this declarator is a function definition. */
12173 : 837599901 : bool funcdef_flag = false;
12174 : 837599901 : cp_declarator_kind innermost_code = cdk_error;
12175 : 837599901 : int bitfield = 0;
12176 : : #if 0
12177 : : /* See the code below that used this. */
12178 : : tree decl_attr = NULL_TREE;
12179 : : #endif
12180 : :
12181 : : /* Keep track of what sort of function is being processed
12182 : : so that we can warn about default return values, or explicit
12183 : : return values which do not match prescribed defaults. */
12184 : 837599901 : special_function_kind sfk = sfk_none;
12185 : :
12186 : 837599901 : tree dname = NULL_TREE;
12187 : 837599901 : tree ctor_return_type = NULL_TREE;
12188 : 837599901 : enum overload_flags flags = NO_SPECIAL;
12189 : : /* cv-qualifiers that apply to the declarator, for a declaration of
12190 : : a member function. */
12191 : 837599901 : cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
12192 : : /* virt-specifiers that apply to the declarator, for a declaration of
12193 : : a member function. */
12194 : 837599901 : cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
12195 : : /* ref-qualifier that applies to the declarator, for a declaration of
12196 : : a member function. */
12197 : 837599901 : cp_ref_qualifier rqual = REF_QUAL_NONE;
12198 : : /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
12199 : 837599901 : int type_quals = get_type_quals (declspecs);
12200 : 837599901 : tree raises = NULL_TREE;
12201 : 837599901 : int template_count = 0;
12202 : 837599901 : tree returned_attrs = NULL_TREE;
12203 : 837599901 : tree parms = NULL_TREE;
12204 : 837599901 : const cp_declarator *id_declarator;
12205 : : /* The unqualified name of the declarator; either an
12206 : : IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
12207 : 837599901 : tree unqualified_id;
12208 : : /* The class type, if any, in which this entity is located,
12209 : : or NULL_TREE if none. Note that this value may be different from
12210 : : the current class type; for example if an attempt is made to declare
12211 : : "A::f" inside "B", this value will be "A". */
12212 : 837599901 : tree ctype = current_class_type;
12213 : : /* The NAMESPACE_DECL for the namespace in which this entity is
12214 : : located. If an unqualified name is used to declare the entity,
12215 : : this value will be NULL_TREE, even if the entity is located at
12216 : : namespace scope. */
12217 : 837599901 : tree in_namespace = NULL_TREE;
12218 : 837599901 : cp_storage_class storage_class;
12219 : 837599901 : bool unsigned_p, signed_p, short_p, long_p, thread_p;
12220 : 837599901 : bool type_was_error_mark_node = false;
12221 : 837599901 : bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
12222 : 837599901 : bool template_type_arg = false;
12223 : 837599901 : bool template_parm_flag = false;
12224 : 837599901 : bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
12225 : 837599901 : bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
12226 : 837599901 : bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
12227 : 837599901 : bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
12228 : 837599901 : bool late_return_type_p = false;
12229 : 837599901 : bool array_parameter_p = false;
12230 : 837599901 : tree reqs = NULL_TREE;
12231 : :
12232 : 837599901 : signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
12233 : 837599901 : unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
12234 : 837599901 : short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
12235 : 837599901 : long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
12236 : 837599901 : longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
12237 : 837599901 : explicit_intN = declspecs->explicit_intN_p;
12238 : 837599901 : int_n_alt = declspecs->int_n_alt;
12239 : 837599901 : thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
12240 : :
12241 : : // Was concept_p specified? Note that ds_concept
12242 : : // implies ds_constexpr!
12243 : 837599901 : bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
12244 : 837599901 : if (concept_p)
12245 : 283 : constexpr_p = true;
12246 : :
12247 : 837599901 : if (decl_context == FUNCDEF)
12248 : : funcdef_flag = true, decl_context = NORMAL;
12249 : : else if (decl_context == MEMFUNCDEF)
12250 : : funcdef_flag = true, decl_context = FIELD;
12251 : : else if (decl_context == BITFIELD)
12252 : : bitfield = 1, decl_context = FIELD;
12253 : : else if (decl_context == TEMPLATE_TYPE_ARG)
12254 : : template_type_arg = true, decl_context = TYPENAME;
12255 : : else if (decl_context == TPARM)
12256 : 11408993 : template_parm_flag = true, decl_context = PARM;
12257 : :
12258 : 837599901 : if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
12259 : 8402033 : funcdef_flag = true;
12260 : :
12261 : 837599901 : location_t typespec_loc = loc_or_input_loc (smallest_type_location
12262 : : (type_quals,
12263 : 837599901 : declspecs->locations));
12264 : 837599901 : location_t id_loc;
12265 : 837599901 : location_t init_loc;
12266 : 837599901 : if (declarator)
12267 : : {
12268 : 521958352 : id_loc = loc_or_input_loc (declarator->id_loc);
12269 : 521958352 : init_loc = loc_or_input_loc (declarator->init_loc);
12270 : : }
12271 : : else
12272 : 315641549 : init_loc = id_loc = input_location;
12273 : :
12274 : : /* Look inside a declarator for the name being declared
12275 : : and get it as a string, for an error message. */
12276 : 837599901 : for (id_declarator = declarator;
12277 : 1167444153 : id_declarator;
12278 : 329844252 : id_declarator = id_declarator->declarator)
12279 : : {
12280 : 818141810 : if (id_declarator->kind != cdk_id)
12281 : 329844285 : innermost_code = id_declarator->kind;
12282 : :
12283 : 818141810 : switch (id_declarator->kind)
12284 : : {
12285 : 160194659 : case cdk_function:
12286 : 160194659 : if (id_declarator->declarator
12287 : 159721864 : && id_declarator->declarator->kind == cdk_id)
12288 : : {
12289 : 157887743 : sfk = id_declarator->declarator->u.id.sfk;
12290 : 157887743 : if (sfk == sfk_destructor)
12291 : 2816674 : flags = DTOR_FLAG;
12292 : : }
12293 : : break;
12294 : :
12295 : 488297525 : case cdk_id:
12296 : 488297525 : {
12297 : 488297525 : tree qualifying_scope = id_declarator->u.id.qualifying_scope;
12298 : 488297525 : tree decl = id_declarator->u.id.unqualified_name;
12299 : 488297525 : if (!decl)
12300 : : break;
12301 : 488297525 : if (qualifying_scope)
12302 : : {
12303 : 9091128 : if (check_for_bare_parameter_packs (qualifying_scope,
12304 : 9091128 : id_declarator->id_loc))
12305 : 6 : return error_mark_node;
12306 : 9091122 : if (at_function_scope_p ())
12307 : : {
12308 : : /* [dcl.meaning]
12309 : :
12310 : : A declarator-id shall not be qualified except
12311 : : for ...
12312 : :
12313 : : None of the cases are permitted in block
12314 : : scope. */
12315 : 0 : if (qualifying_scope == global_namespace)
12316 : 0 : error ("invalid use of qualified-name %<::%D%>",
12317 : : decl);
12318 : 0 : else if (TYPE_P (qualifying_scope))
12319 : 0 : error ("invalid use of qualified-name %<%T::%D%>",
12320 : : qualifying_scope, decl);
12321 : : else
12322 : 0 : error ("invalid use of qualified-name %<%D::%D%>",
12323 : : qualifying_scope, decl);
12324 : 0 : return error_mark_node;
12325 : : }
12326 : 9091122 : else if (TYPE_P (qualifying_scope))
12327 : : {
12328 : 9002357 : ctype = qualifying_scope;
12329 : 9002357 : if (!MAYBE_CLASS_TYPE_P (ctype))
12330 : : {
12331 : 7 : error_at (id_declarator->id_loc,
12332 : : "%q#T is not a class or namespace", ctype);
12333 : 7 : ctype = NULL_TREE;
12334 : : }
12335 : 9002350 : else if (innermost_code != cdk_function
12336 : 358661 : && current_class_type
12337 : 9002369 : && !uniquely_derived_from_p (ctype,
12338 : : current_class_type))
12339 : : {
12340 : 11 : error_at (id_declarator->id_loc,
12341 : : "invalid use of qualified-name %<%T::%D%>",
12342 : : qualifying_scope, decl);
12343 : 11 : return error_mark_node;
12344 : : }
12345 : : }
12346 : 88765 : else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
12347 : 488297508 : in_namespace = qualifying_scope;
12348 : : }
12349 : 488297508 : switch (TREE_CODE (decl))
12350 : : {
12351 : 2816694 : case BIT_NOT_EXPR:
12352 : 2816694 : {
12353 : 2816694 : if (innermost_code != cdk_function)
12354 : : {
12355 : 8 : error_at (EXPR_LOCATION (decl),
12356 : : "declaration of %qE as non-function", decl);
12357 : 8 : return error_mark_node;
12358 : : }
12359 : 2816686 : else if (!qualifying_scope
12360 : 2816686 : && !(current_class_type && at_class_scope_p ()))
12361 : : {
12362 : 12 : error_at (EXPR_LOCATION (decl),
12363 : : "declaration of %qE as non-member", decl);
12364 : 12 : return error_mark_node;
12365 : : }
12366 : :
12367 : 2816674 : tree type = TREE_OPERAND (decl, 0);
12368 : 2816674 : if (TYPE_P (type))
12369 : 2816670 : type = constructor_name (type);
12370 : 2816674 : name = identifier_to_locale (IDENTIFIER_POINTER (type));
12371 : 2816674 : dname = decl;
12372 : : }
12373 : 2816674 : break;
12374 : :
12375 : 1633670 : case TEMPLATE_ID_EXPR:
12376 : 1633670 : {
12377 : 1633670 : tree fns = TREE_OPERAND (decl, 0);
12378 : :
12379 : 1633670 : dname = fns;
12380 : 2460024 : if (!identifier_p (dname))
12381 : 1633659 : dname = OVL_NAME (dname);
12382 : : }
12383 : : /* Fall through. */
12384 : :
12385 : 485480814 : case IDENTIFIER_NODE:
12386 : 485480814 : if (identifier_p (decl))
12387 : 483847144 : dname = decl;
12388 : :
12389 : 485480814 : if (IDENTIFIER_KEYWORD_P (dname))
12390 : : {
12391 : 0 : error ("declarator-id missing; using reserved word %qD",
12392 : : dname);
12393 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12394 : : }
12395 : 942454727 : else if (!IDENTIFIER_CONV_OP_P (dname))
12396 : 484961631 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12397 : : else
12398 : : {
12399 : 519183 : gcc_assert (flags == NO_SPECIAL);
12400 : 519183 : flags = TYPENAME_FLAG;
12401 : 519183 : sfk = sfk_conversion;
12402 : 519183 : tree glob = get_global_binding (dname);
12403 : 519183 : if (glob && TREE_CODE (glob) == TYPE_DECL)
12404 : 0 : name = identifier_to_locale (IDENTIFIER_POINTER (dname));
12405 : : else
12406 : : name = "<invalid operator>";
12407 : : }
12408 : : break;
12409 : :
12410 : 0 : default:
12411 : 0 : gcc_unreachable ();
12412 : : }
12413 : : break;
12414 : : }
12415 : :
12416 : : case cdk_array:
12417 : : case cdk_pointer:
12418 : : case cdk_reference:
12419 : : case cdk_ptrmem:
12420 : : break;
12421 : :
12422 : : case cdk_decomp:
12423 : 818141740 : name = "structured binding";
12424 : : break;
12425 : :
12426 : 33 : case cdk_error:
12427 : 33 : return error_mark_node;
12428 : :
12429 : 0 : default:
12430 : 0 : gcc_unreachable ();
12431 : : }
12432 : 818141740 : if (id_declarator->kind == cdk_id)
12433 : : break;
12434 : : }
12435 : :
12436 : : /* [dcl.fct.edf]
12437 : :
12438 : : The declarator in a function-definition shall have the form
12439 : : D1 ( parameter-declaration-clause) ... */
12440 : 837599831 : if (funcdef_flag && innermost_code != cdk_function)
12441 : : {
12442 : 4 : error_at (id_loc, "function definition does not declare parameters");
12443 : 4 : return error_mark_node;
12444 : : }
12445 : :
12446 : 837599827 : if (flags == TYPENAME_FLAG
12447 : 837599827 : && innermost_code != cdk_function
12448 : 3 : && ! (ctype && !declspecs->any_specifiers_p))
12449 : : {
12450 : 3 : error_at (id_loc, "declaration of %qD as non-function", dname);
12451 : 3 : return error_mark_node;
12452 : : }
12453 : :
12454 : 837599824 : if (dname && identifier_p (dname))
12455 : : {
12456 : 485480807 : if (UDLIT_OPER_P (dname)
12457 : 485480807 : && innermost_code != cdk_function)
12458 : : {
12459 : 6 : error_at (id_loc, "declaration of %qD as non-function", dname);
12460 : 6 : return error_mark_node;
12461 : : }
12462 : :
12463 : 485480801 : if (IDENTIFIER_ANY_OP_P (dname))
12464 : : {
12465 : 28506898 : if (typedef_p)
12466 : : {
12467 : 4 : error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
12468 : 4 : return error_mark_node;
12469 : : }
12470 : 28506894 : else if (decl_context == PARM || decl_context == CATCHPARM)
12471 : : {
12472 : 20 : error_at (id_loc, "declaration of %qD as parameter", dname);
12473 : 20 : return error_mark_node;
12474 : : }
12475 : : }
12476 : : }
12477 : :
12478 : : /* Anything declared one level down from the top level
12479 : : must be one of the parameters of a function
12480 : : (because the body is at least two levels down). */
12481 : :
12482 : : /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12483 : : by not allowing C++ class definitions to specify their parameters
12484 : : with xdecls (must be spec.d in the parmlist).
12485 : :
12486 : : Since we now wait to push a class scope until we are sure that
12487 : : we are in a legitimate method context, we must set oldcname
12488 : : explicitly (since current_class_name is not yet alive).
12489 : :
12490 : : We also want to avoid calling this a PARM if it is in a namespace. */
12491 : :
12492 : 837599794 : if (decl_context == NORMAL && !toplevel_bindings_p ())
12493 : : {
12494 : 56520374 : cp_binding_level *b = current_binding_level;
12495 : 56520374 : current_binding_level = b->level_chain;
12496 : 56520374 : if (current_binding_level != 0 && toplevel_bindings_p ())
12497 : : decl_context = PARM;
12498 : 56520374 : current_binding_level = b;
12499 : : }
12500 : :
12501 : 837599794 : if (name == NULL)
12502 : 349246557 : name = decl_context == PARM ? "parameter" : "type name";
12503 : :
12504 : 837599794 : if (consteval_p && constexpr_p)
12505 : : {
12506 : 2 : error_at (declspecs->locations[ds_consteval],
12507 : : "both %qs and %qs specified", "constexpr", "consteval");
12508 : 2 : return error_mark_node;
12509 : : }
12510 : :
12511 : 837599792 : if (concept_p && typedef_p)
12512 : : {
12513 : 3 : error_at (declspecs->locations[ds_concept],
12514 : : "%qs cannot appear in a typedef declaration", "concept");
12515 : 3 : return error_mark_node;
12516 : : }
12517 : :
12518 : 837599789 : if (constexpr_p && typedef_p)
12519 : : {
12520 : 3 : error_at (declspecs->locations[ds_constexpr],
12521 : : "%qs cannot appear in a typedef declaration", "constexpr");
12522 : 3 : return error_mark_node;
12523 : : }
12524 : :
12525 : 837599786 : if (consteval_p && typedef_p)
12526 : : {
12527 : 1 : error_at (declspecs->locations[ds_consteval],
12528 : : "%qs cannot appear in a typedef declaration", "consteval");
12529 : 1 : return error_mark_node;
12530 : : }
12531 : :
12532 : 837599785 : if (constinit_p && typedef_p)
12533 : : {
12534 : 1 : error_at (declspecs->locations[ds_constinit],
12535 : : "%qs cannot appear in a typedef declaration", "constinit");
12536 : 1 : return error_mark_node;
12537 : : }
12538 : :
12539 : : /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12540 : : keywords shall appear in a decl-specifier-seq." */
12541 : 837599784 : if (constinit_p && constexpr_p)
12542 : : {
12543 : 2 : gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12544 : 2 : richloc.add_range (declspecs->locations[ds_constexpr]);
12545 : 2 : error_at (&richloc,
12546 : : "can use at most one of the %<constinit%> and %<constexpr%> "
12547 : : "specifiers");
12548 : 2 : return error_mark_node;
12549 : 2 : }
12550 : :
12551 : : /* If there were multiple types specified in the decl-specifier-seq,
12552 : : issue an error message. */
12553 : 837599782 : if (declspecs->multiple_types_p)
12554 : : {
12555 : 1975 : error_at (typespec_loc,
12556 : : "two or more data types in declaration of %qs", name);
12557 : 1975 : return error_mark_node;
12558 : : }
12559 : :
12560 : 837597807 : if (declspecs->conflicting_specifiers_p)
12561 : 38 : return error_mark_node;
12562 : :
12563 : : /* Extract the basic type from the decl-specifier-seq. */
12564 : 837597769 : type = declspecs->type;
12565 : 837597769 : if (type == error_mark_node)
12566 : : {
12567 : 1078 : type = NULL_TREE;
12568 : 1078 : type_was_error_mark_node = true;
12569 : : }
12570 : :
12571 : : /* Ignore erroneous attributes. */
12572 : 837597769 : if (attrlist && *attrlist == error_mark_node)
12573 : 12 : *attrlist = NULL_TREE;
12574 : :
12575 : : /* An object declared as __attribute__((unavailable)) suppresses
12576 : : any reports of being declared with unavailable or deprecated
12577 : : items. An object declared as __attribute__((deprecated))
12578 : : suppresses warnings of uses of other deprecated items. */
12579 : 837597769 : auto ds = make_temp_override (deprecated_state);
12580 : 837597769 : if (attrlist && lookup_attribute ("unavailable", *attrlist))
12581 : 198 : deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12582 : 837597571 : else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12583 : 653365 : deprecated_state = DEPRECATED_SUPPRESS;
12584 : :
12585 : 837597769 : cp_handle_deprecated_or_unavailable (type);
12586 : 837597769 : if (type && TREE_CODE (type) == TYPE_DECL)
12587 : : {
12588 : 576504001 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12589 : 576504001 : typedef_decl = type;
12590 : 576504001 : type = TREE_TYPE (typedef_decl);
12591 : 576504001 : if (DECL_ARTIFICIAL (typedef_decl))
12592 : 415622987 : cp_handle_deprecated_or_unavailable (type);
12593 : : }
12594 : : /* No type at all: default to `int', and set DEFAULTED_INT
12595 : : because it was not a user-defined typedef. */
12596 : 837597769 : if (type == NULL_TREE)
12597 : : {
12598 : 31893202 : if (signed_p || unsigned_p || long_p || short_p)
12599 : : {
12600 : : /* These imply 'int'. */
12601 : 7676286 : type = integer_type_node;
12602 : 7676286 : defaulted_int = 1;
12603 : : }
12604 : : /* If we just have "complex", it is equivalent to "complex double". */
12605 : 24216916 : else if (!longlong && !explicit_intN
12606 : 24216916 : && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12607 : : {
12608 : 70 : type = double_type_node;
12609 : 70 : pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12610 : : "ISO C++ does not support plain %<complex%> meaning "
12611 : : "%<double complex%>");
12612 : : }
12613 : : }
12614 : : /* Gather flags. */
12615 : 837597769 : explicit_int = declspecs->explicit_int_p;
12616 : 837597769 : explicit_char = declspecs->explicit_char_p;
12617 : :
12618 : : #if 0
12619 : : /* See the code below that used this. */
12620 : : if (typedef_decl)
12621 : : decl_attr = DECL_ATTRIBUTES (typedef_decl);
12622 : : #endif
12623 : 837597769 : typedef_type = type;
12624 : :
12625 : 837597769 : if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12626 : 1330555 : ctor_return_type = TREE_TYPE (dname);
12627 : : else
12628 : : ctor_return_type = ctype;
12629 : :
12630 : 837597769 : if (sfk != sfk_none)
12631 : : {
12632 : 24215440 : type = check_special_function_return_type (sfk, type,
12633 : : ctor_return_type,
12634 : : type_quals,
12635 : : declspecs->locations);
12636 : 24215440 : type_quals = TYPE_UNQUALIFIED;
12637 : : }
12638 : 813382329 : else if (type == NULL_TREE)
12639 : : {
12640 : 1502 : int is_main;
12641 : :
12642 : 1502 : explicit_int = -1;
12643 : :
12644 : : /* We handle `main' specially here, because 'main () { }' is so
12645 : : common. With no options, it is allowed. With -Wreturn-type,
12646 : : it is a warning. It is only an error with -pedantic-errors. */
12647 : 3004 : is_main = (funcdef_flag
12648 : 3268 : && dname && identifier_p (dname)
12649 : 264 : && MAIN_NAME_P (dname)
12650 : 86 : && ctype == NULL_TREE
12651 : 86 : && in_namespace == NULL_TREE
12652 : 1588 : && current_namespace == global_namespace);
12653 : :
12654 : 1502 : if (type_was_error_mark_node)
12655 : : /* We've already issued an error, don't complain more. */;
12656 : 424 : else if (in_system_header_at (id_loc) || flag_ms_extensions)
12657 : : /* Allow it, sigh. */;
12658 : 354 : else if (! is_main)
12659 : 274 : permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12660 : : name);
12661 : 80 : else if (pedantic)
12662 : 5 : pedwarn (id_loc, OPT_Wpedantic,
12663 : : "ISO C++ forbids declaration of %qs with no type", name);
12664 : : else
12665 : 75 : warning_at (id_loc, OPT_Wreturn_type,
12666 : : "ISO C++ forbids declaration of %qs with no type", name);
12667 : :
12668 : 1502 : if (type_was_error_mark_node && template_parm_flag)
12669 : : /* FIXME we should be able to propagate the error_mark_node as is
12670 : : for other contexts too. */
12671 : 120 : type = error_mark_node;
12672 : : else
12673 : 1382 : type = integer_type_node;
12674 : : }
12675 : :
12676 : 837597769 : ctype = NULL_TREE;
12677 : :
12678 : 837597769 : if (explicit_intN)
12679 : : {
12680 : 828616 : if (! int_n_enabled_p[declspecs->int_n_idx])
12681 : : {
12682 : 0 : error_at (declspecs->locations[ds_type_spec],
12683 : : "%<__int%d%> is not supported by this target",
12684 : 0 : int_n_data[declspecs->int_n_idx].bitsize);
12685 : 0 : explicit_intN = false;
12686 : : }
12687 : : /* Don't pedwarn if the alternate "__intN__" form has been used instead
12688 : : of "__intN". */
12689 : 828616 : else if (!int_n_alt && pedantic)
12690 : 3330 : pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12691 : : "ISO C++ does not support %<__int%d%> for %qs",
12692 : 3330 : int_n_data[declspecs->int_n_idx].bitsize, name);
12693 : : }
12694 : :
12695 : : /* Now process the modifiers that were specified
12696 : : and check for invalid combinations. */
12697 : :
12698 : : /* Long double is a special combination. */
12699 : 837597769 : if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12700 : : {
12701 : 8230124 : long_p = false;
12702 : 8230124 : type = cp_build_qualified_type (long_double_type_node,
12703 : : cp_type_quals (type));
12704 : : }
12705 : :
12706 : : /* Check all other uses of type modifiers. */
12707 : :
12708 : 837597769 : if (unsigned_p || signed_p || long_p || short_p)
12709 : : {
12710 : 17644523 : location_t loc;
12711 : 17644523 : const char *key;
12712 : 17644523 : if (unsigned_p)
12713 : : {
12714 : 11841585 : key = "unsigned";
12715 : 11841585 : loc = declspecs->locations[ds_unsigned];
12716 : : }
12717 : 5802938 : else if (signed_p)
12718 : : {
12719 : 984291 : key = "signed";
12720 : 984291 : loc = declspecs->locations[ds_signed];
12721 : : }
12722 : 4818647 : else if (longlong)
12723 : : {
12724 : 1527975 : key = "long long";
12725 : 1527975 : loc = declspecs->locations[ds_long_long];
12726 : : }
12727 : 3290672 : else if (long_p)
12728 : : {
12729 : 2816149 : key = "long";
12730 : 2816149 : loc = declspecs->locations[ds_long];
12731 : : }
12732 : : else /* if (short_p) */
12733 : : {
12734 : 474523 : key = "short";
12735 : 474523 : loc = declspecs->locations[ds_short];
12736 : : }
12737 : :
12738 : 17644523 : int ok = 0;
12739 : :
12740 : 17644523 : if (signed_p && unsigned_p)
12741 : : {
12742 : 72 : gcc_rich_location richloc (declspecs->locations[ds_signed]);
12743 : 72 : richloc.add_range (declspecs->locations[ds_unsigned]);
12744 : 72 : error_at (&richloc,
12745 : : "%<signed%> and %<unsigned%> specified together");
12746 : 72 : }
12747 : 17644451 : else if (long_p && short_p)
12748 : : {
12749 : 8 : gcc_rich_location richloc (declspecs->locations[ds_long]);
12750 : 8 : richloc.add_range (declspecs->locations[ds_short]);
12751 : 8 : error_at (&richloc, "%<long%> and %<short%> specified together");
12752 : 8 : }
12753 : 17644443 : else if (TREE_CODE (type) != INTEGER_TYPE
12754 : 17644426 : || type == char8_type_node
12755 : 17644394 : || type == char16_type_node
12756 : 17644370 : || type == char32_type_node
12757 : 17644346 : || ((long_p || short_p)
12758 : 9552710 : && (explicit_char || explicit_intN)))
12759 : 541 : error_at (loc, "%qs specified with %qT", key, type);
12760 : 17643902 : else if (!explicit_int && !defaulted_int
12761 : 17643902 : && !explicit_char && !explicit_intN)
12762 : : {
12763 : 32 : if (typedef_decl)
12764 : : {
12765 : 28 : pedwarn (loc, OPT_Wpedantic,
12766 : : "%qs specified with typedef-name %qD",
12767 : : key, typedef_decl);
12768 : 28 : ok = !flag_pedantic_errors;
12769 : : /* PR108099: __int128_t comes from c_common_nodes_and_builtins,
12770 : : and is not built as a typedef. */
12771 : 28 : if (is_typedef_decl (typedef_decl))
12772 : 19 : type = DECL_ORIGINAL_TYPE (typedef_decl);
12773 : : }
12774 : 4 : else if (declspecs->decltype_p)
12775 : 0 : error_at (loc, "%qs specified with %<decltype%>", key);
12776 : : else
12777 : 4 : error_at (loc, "%qs specified with %<typeof%>", key);
12778 : : }
12779 : : else
12780 : : ok = 1;
12781 : :
12782 : : /* Discard the type modifiers if they are invalid. */
12783 : 653 : if (! ok)
12784 : : {
12785 : : unsigned_p = false;
12786 : : signed_p = false;
12787 : : long_p = false;
12788 : : short_p = false;
12789 : : longlong = 0;
12790 : : }
12791 : : }
12792 : :
12793 : : /* Decide whether an integer type is signed or not.
12794 : : Optionally treat bitfields as signed by default. */
12795 : 837597769 : if (unsigned_p
12796 : : /* [class.bit]
12797 : :
12798 : : It is implementation-defined whether a plain (neither
12799 : : explicitly signed or unsigned) char, short, int, or long
12800 : : bit-field is signed or unsigned.
12801 : :
12802 : : Naturally, we extend this to long long as well. Note that
12803 : : this does not include wchar_t. */
12804 : 837597769 : || (bitfield && !flag_signed_bitfields
12805 : 22 : && !signed_p
12806 : : /* A typedef for plain `int' without `signed' can be
12807 : : controlled just like plain `int', but a typedef for
12808 : : `signed int' cannot be so controlled. */
12809 : 22 : && !(typedef_decl
12810 : 22 : && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12811 : 16 : && TREE_CODE (type) == INTEGER_TYPE
12812 : 12 : && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12813 : : {
12814 : 11841303 : if (explicit_intN)
12815 : 387842 : type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12816 : 11453461 : else if (longlong)
12817 : 1368120 : type = long_long_unsigned_type_node;
12818 : 10085341 : else if (long_p)
12819 : 1997589 : type = long_unsigned_type_node;
12820 : 8087752 : else if (short_p)
12821 : 1143051 : type = short_unsigned_type_node;
12822 : 6944701 : else if (type == char_type_node)
12823 : 1356591 : type = unsigned_char_type_node;
12824 : 5588110 : else if (typedef_decl)
12825 : 28 : type = c_common_unsigned_type (type);
12826 : : else
12827 : 5588082 : type = unsigned_type_node;
12828 : : }
12829 : 825756466 : else if (signed_p && type == char_type_node)
12830 : 616492 : type = signed_char_type_node;
12831 : 825139974 : else if (explicit_intN)
12832 : 440774 : type = int_n_trees[declspecs->int_n_idx].signed_type;
12833 : 824699200 : else if (longlong)
12834 : 1595232 : type = long_long_integer_type_node;
12835 : 823103968 : else if (long_p)
12836 : 2892556 : type = long_integer_type_node;
12837 : 820211412 : else if (short_p)
12838 : 555718 : type = short_integer_type_node;
12839 : 819655694 : else if (signed_p && typedef_decl)
12840 : 9 : type = c_common_signed_type (type);
12841 : :
12842 : 837597769 : if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12843 : : {
12844 : 1275374 : if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12845 : 4 : error_at (declspecs->locations[ds_complex],
12846 : : "complex invalid for %qs", name);
12847 : : /* If a modifier is specified, the resulting complex is the complex
12848 : : form of TYPE. E.g, "complex short" is "complex short int". */
12849 : 1275370 : else if (type == integer_type_node)
12850 : 182 : type = complex_integer_type_node;
12851 : 1275188 : else if (type == float_type_node)
12852 : 340970 : type = complex_float_type_node;
12853 : 934218 : else if (type == double_type_node)
12854 : 341535 : type = complex_double_type_node;
12855 : 592683 : else if (type == long_double_type_node)
12856 : 341004 : type = complex_long_double_type_node;
12857 : : else
12858 : 251679 : type = build_complex_type (type);
12859 : : }
12860 : :
12861 : : /* If we're using the injected-class-name to form a compound type or a
12862 : : declaration, replace it with the underlying class so we don't get
12863 : : redundant typedefs in the debug output. But if we are returning the
12864 : : type unchanged, leave it alone so that it's available to
12865 : : maybe_get_template_decl_from_type_decl. */
12866 : 168940130 : if (CLASS_TYPE_P (type)
12867 : 168894183 : && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12868 : 25232187 : && type == TREE_TYPE (TYPE_NAME (type))
12869 : 862829956 : && (declarator || type_quals))
12870 : 24359147 : type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12871 : :
12872 : 837597769 : type_quals |= cp_type_quals (type);
12873 : 1675195538 : type = cp_build_qualified_type
12874 : 1675195538 : (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12875 : 676716755 : || declspecs->decltype_p)
12876 : : ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12877 : : /* We might have ignored or rejected some of the qualifiers. */
12878 : 837597769 : type_quals = cp_type_quals (type);
12879 : :
12880 : 817161298 : if (cxx_dialect >= cxx17 && type && is_auto (type)
12881 : 10559581 : && innermost_code != cdk_function
12882 : : /* Placeholder in parm gets a better error below. */
12883 : 6317483 : && !(decl_context == PARM || decl_context == CATCHPARM)
12884 : 843912931 : && id_declarator && declarator != id_declarator)
12885 : 802021 : if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12886 : : {
12887 : 12 : auto_diagnostic_group g;
12888 : 12 : gcc_rich_location richloc (typespec_loc);
12889 : 12 : richloc.add_fixit_insert_after ("<>");
12890 : 12 : error_at (&richloc, "missing template argument list after %qE; "
12891 : : "for deduction, template placeholder must be followed "
12892 : : "by a simple declarator-id", tmpl);
12893 : 12 : inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12894 : 12 : type = error_mark_node;
12895 : 12 : }
12896 : :
12897 : 837597769 : staticp = 0;
12898 : 837597769 : inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12899 : 837597769 : virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12900 : 837597769 : explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12901 : :
12902 : 837597769 : storage_class = declspecs->storage_class;
12903 : 837597769 : if (storage_class == sc_static)
12904 : 18092045 : staticp = 1 + (decl_context == FIELD);
12905 : 819505724 : else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12906 : : /* Treat class-scope deduction guides as static member functions
12907 : : so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12908 : 17345949 : staticp = 2;
12909 : :
12910 : 837597769 : if (virtualp)
12911 : : {
12912 : 3811179 : if (staticp == 2)
12913 : : {
12914 : 27 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12915 : 27 : richloc.add_range (declspecs->locations[ds_storage_class]);
12916 : 27 : error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12917 : : "and %<static%>", dname);
12918 : 27 : storage_class = sc_none;
12919 : 27 : staticp = 0;
12920 : 27 : }
12921 : 3811179 : if (constexpr_p && pedantic && cxx_dialect < cxx20)
12922 : : {
12923 : 6 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12924 : 6 : richloc.add_range (declspecs->locations[ds_constexpr]);
12925 : 6 : pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12926 : : "declared both %<virtual%> and %<constexpr%> only in "
12927 : : "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12928 : 6 : }
12929 : : }
12930 : 837597769 : friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12931 : :
12932 : : /* Issue errors about use of storage classes for parameters. */
12933 : 837597769 : if (decl_context == PARM)
12934 : : {
12935 : 256097700 : if (typedef_p)
12936 : : {
12937 : 32 : error_at (declspecs->locations[ds_typedef],
12938 : : "typedef declaration invalid in parameter declaration");
12939 : 32 : return error_mark_node;
12940 : : }
12941 : 256097668 : else if (template_parm_flag && storage_class != sc_none)
12942 : : {
12943 : 21 : error_at (min_location (declspecs->locations[ds_thread],
12944 : : declspecs->locations[ds_storage_class]),
12945 : : "storage class specified for template parameter %qs",
12946 : : name);
12947 : 21 : return error_mark_node;
12948 : : }
12949 : 256097647 : else if (storage_class == sc_static
12950 : 256097647 : || storage_class == sc_extern
12951 : 256097643 : || thread_p)
12952 : : {
12953 : 12 : error_at (min_location (declspecs->locations[ds_thread],
12954 : : declspecs->locations[ds_storage_class]),
12955 : : "storage class specified for parameter %qs", name);
12956 : 12 : return error_mark_node;
12957 : : }
12958 : :
12959 : : /* Function parameters cannot be concept. */
12960 : 256097635 : if (concept_p)
12961 : : {
12962 : 3 : error_at (declspecs->locations[ds_concept],
12963 : : "a parameter cannot be declared %qs", "concept");
12964 : 3 : concept_p = 0;
12965 : 3 : constexpr_p = 0;
12966 : : }
12967 : : /* Function parameters cannot be constexpr. If we saw one, moan
12968 : : and pretend it wasn't there. */
12969 : 256097632 : else if (constexpr_p)
12970 : : {
12971 : 9 : error_at (declspecs->locations[ds_constexpr],
12972 : : "a parameter cannot be declared %qs", "constexpr");
12973 : 9 : constexpr_p = 0;
12974 : : }
12975 : 256097635 : if (constinit_p)
12976 : : {
12977 : 2 : error_at (declspecs->locations[ds_constinit],
12978 : : "a parameter cannot be declared %qs", "constinit");
12979 : 2 : constinit_p = 0;
12980 : : }
12981 : 256097635 : if (consteval_p)
12982 : : {
12983 : 1 : error_at (declspecs->locations[ds_consteval],
12984 : : "a parameter cannot be declared %qs", "consteval");
12985 : 1 : consteval_p = 0;
12986 : : }
12987 : : }
12988 : :
12989 : : /* Give error if `virtual' is used outside of class declaration. */
12990 : 837597704 : if (virtualp
12991 : 3811179 : && (current_class_name == NULL_TREE || decl_context != FIELD))
12992 : : {
12993 : 21 : error_at (declspecs->locations[ds_virtual],
12994 : : "%<virtual%> outside class declaration");
12995 : 21 : virtualp = 0;
12996 : : }
12997 : :
12998 : 837597704 : if (innermost_code == cdk_decomp)
12999 : : {
13000 : 111572 : location_t loc = (declarator->kind == cdk_reference
13001 : 55786 : ? declarator->declarator->id_loc : declarator->id_loc);
13002 : 55786 : if (inlinep)
13003 : 1 : error_at (declspecs->locations[ds_inline],
13004 : : "structured binding declaration cannot be %qs", "inline");
13005 : 55786 : if (typedef_p)
13006 : 1 : error_at (declspecs->locations[ds_typedef],
13007 : : "structured binding declaration cannot be %qs", "typedef");
13008 : 55786 : if (constexpr_p && !concept_p)
13009 : 1 : error_at (declspecs->locations[ds_constexpr], "structured "
13010 : : "binding declaration cannot be %qs", "constexpr");
13011 : 55786 : if (consteval_p)
13012 : 1 : error_at (declspecs->locations[ds_consteval], "structured "
13013 : : "binding declaration cannot be %qs", "consteval");
13014 : 55786 : if (thread_p && cxx_dialect < cxx20)
13015 : 10 : pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
13016 : : "structured binding declaration can be %qs only in "
13017 : : "%<-std=c++20%> or %<-std=gnu++20%>",
13018 : 10 : declspecs->gnu_thread_keyword_p
13019 : : ? "__thread" : "thread_local");
13020 : 55786 : if (concept_p)
13021 : 0 : error_at (declspecs->locations[ds_concept],
13022 : : "structured binding declaration cannot be %qs", "concept");
13023 : : /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
13024 : 55786 : if (type_quals & TYPE_QUAL_VOLATILE)
13025 : 14 : warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
13026 : : "%<volatile%>-qualified structured binding is deprecated");
13027 : 55786 : switch (storage_class)
13028 : : {
13029 : : case sc_none:
13030 : : break;
13031 : 0 : case sc_register:
13032 : 0 : error_at (loc, "structured binding declaration cannot be %qs",
13033 : : "register");
13034 : 0 : break;
13035 : 79 : case sc_static:
13036 : 79 : if (cxx_dialect < cxx20)
13037 : 53 : pedwarn (loc, OPT_Wc__20_extensions,
13038 : : "structured binding declaration can be %qs only in "
13039 : : "%<-std=c++20%> or %<-std=gnu++20%>", "static");
13040 : : break;
13041 : 1 : case sc_extern:
13042 : 1 : error_at (loc, "structured binding declaration cannot be %qs",
13043 : : "extern");
13044 : 1 : break;
13045 : 0 : case sc_mutable:
13046 : 0 : error_at (loc, "structured binding declaration cannot be %qs",
13047 : : "mutable");
13048 : 0 : break;
13049 : 0 : case sc_auto:
13050 : 0 : error_at (loc, "structured binding declaration cannot be "
13051 : : "C++98 %<auto%>");
13052 : 0 : break;
13053 : 0 : default:
13054 : 0 : gcc_unreachable ();
13055 : : }
13056 : 55786 : if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
13057 : 55786 : || TYPE_IDENTIFIER (type) != auto_identifier)
13058 : : {
13059 : 3 : if (type != error_mark_node)
13060 : : {
13061 : 3 : auto_diagnostic_group d;
13062 : 3 : error_at (loc, "structured binding declaration cannot have "
13063 : : "type %qT", type);
13064 : 3 : inform (loc,
13065 : : "type must be cv-qualified %<auto%> or reference to "
13066 : : "cv-qualified %<auto%>");
13067 : 3 : }
13068 : 3 : type = build_qualified_type (make_auto (), type_quals);
13069 : 3 : declspecs->type = type;
13070 : : }
13071 : 55783 : else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type))
13072 : 17 : pedwarn (loc, OPT_Wpedantic,
13073 : : "structured binding declaration cannot have constrained "
13074 : : "%<auto%> type %qT", type);
13075 : 55786 : inlinep = 0;
13076 : 55786 : typedef_p = 0;
13077 : 55786 : constexpr_p = 0;
13078 : 55786 : consteval_p = 0;
13079 : 55786 : concept_p = 0;
13080 : 55786 : if (storage_class != sc_static)
13081 : : {
13082 : 55707 : storage_class = sc_none;
13083 : 55707 : declspecs->storage_class = sc_none;
13084 : : }
13085 : : }
13086 : :
13087 : : /* Static anonymous unions are dealt with here. */
13088 : 837597704 : if (staticp && decl_context == TYPENAME
13089 : 0 : && declspecs->type
13090 : 837597704 : && ANON_AGGR_TYPE_P (declspecs->type))
13091 : : decl_context = FIELD;
13092 : :
13093 : : /* Warn about storage classes that are invalid for certain
13094 : : kinds of declarations (parameters, typenames, etc.). */
13095 : 837597704 : if (thread_p
13096 : 19819 : && ((storage_class
13097 : 19819 : && storage_class != sc_extern
13098 : 500 : && storage_class != sc_static)
13099 : 19805 : || typedef_p))
13100 : : {
13101 : 22 : location_t loc
13102 : 22 : = min_location (declspecs->locations[ds_thread],
13103 : : declspecs->locations[ds_storage_class]);
13104 : 22 : error_at (loc, "multiple storage classes in declaration of %qs", name);
13105 : 22 : thread_p = false;
13106 : : }
13107 : 837597704 : if (decl_context != NORMAL
13108 : 696377865 : && ((storage_class != sc_none
13109 : 696377865 : && storage_class != sc_mutable)
13110 : 679031889 : || thread_p))
13111 : : {
13112 : 17345980 : if ((decl_context == PARM || decl_context == CATCHPARM)
13113 : 54 : && (storage_class == sc_register
13114 : 54 : || storage_class == sc_auto))
13115 : : ;
13116 : 17345926 : else if (typedef_p)
13117 : : ;
13118 : 17345926 : else if (decl_context == FIELD
13119 : : /* C++ allows static class elements. */
13120 : 17345926 : && storage_class == sc_static)
13121 : : /* C++ also allows inlines and signed and unsigned elements,
13122 : : but in those cases we don't come in here. */
13123 : : ;
13124 : : else
13125 : : {
13126 : 21 : location_t loc
13127 : 21 : = min_location (declspecs->locations[ds_thread],
13128 : : declspecs->locations[ds_storage_class]);
13129 : 21 : if (decl_context == FIELD)
13130 : 21 : error_at (loc, "storage class specified for %qs", name);
13131 : 0 : else if (decl_context == PARM || decl_context == CATCHPARM)
13132 : 0 : error_at (loc, "storage class specified for parameter %qs", name);
13133 : : else
13134 : 0 : error_at (loc, "storage class specified for typename");
13135 : 21 : if (storage_class == sc_register
13136 : 21 : || storage_class == sc_auto
13137 : 21 : || storage_class == sc_extern
13138 : 4 : || thread_p)
13139 : 21 : storage_class = sc_none;
13140 : : }
13141 : : }
13142 : 820251724 : else if (storage_class == sc_extern && funcdef_flag
13143 : 820251724 : && ! toplevel_bindings_p ())
13144 : 0 : error ("nested function %qs declared %<extern%>", name);
13145 : 820251724 : else if (toplevel_bindings_p ())
13146 : : {
13147 : 378471003 : if (storage_class == sc_auto)
13148 : 2 : error_at (declspecs->locations[ds_storage_class],
13149 : : "top-level declaration of %qs specifies %<auto%>", name);
13150 : : }
13151 : 441780721 : else if (thread_p
13152 : 441780721 : && storage_class != sc_extern
13153 : 87 : && storage_class != sc_static)
13154 : : {
13155 : 55 : if (declspecs->gnu_thread_keyword_p)
13156 : 4 : pedwarn (declspecs->locations[ds_thread],
13157 : : 0, "function-scope %qs implicitly auto and "
13158 : : "declared %<__thread%>", name);
13159 : :
13160 : : /* When thread_local is applied to a variable of block scope the
13161 : : storage-class-specifier static is implied if it does not appear
13162 : : explicitly. */
13163 : 55 : storage_class = declspecs->storage_class = sc_static;
13164 : 55 : staticp = 1;
13165 : : }
13166 : :
13167 : 837597704 : if (storage_class && friendp)
13168 : : {
13169 : 16 : error_at (min_location (declspecs->locations[ds_thread],
13170 : : declspecs->locations[ds_storage_class]),
13171 : : "storage class specifiers invalid in friend function "
13172 : : "declarations");
13173 : 16 : storage_class = sc_none;
13174 : 16 : staticp = 0;
13175 : : }
13176 : :
13177 : 837597704 : if (!id_declarator)
13178 : : unqualified_id = NULL_TREE;
13179 : : else
13180 : : {
13181 : 488295454 : unqualified_id = id_declarator->u.id.unqualified_name;
13182 : 488295454 : switch (TREE_CODE (unqualified_id))
13183 : : {
13184 : 2816674 : case BIT_NOT_EXPR:
13185 : 2816674 : unqualified_id = TREE_OPERAND (unqualified_id, 0);
13186 : 2816674 : if (TYPE_P (unqualified_id))
13187 : 2816670 : unqualified_id = constructor_name (unqualified_id);
13188 : : break;
13189 : :
13190 : : case IDENTIFIER_NODE:
13191 : : case TEMPLATE_ID_EXPR:
13192 : : break;
13193 : :
13194 : 0 : default:
13195 : 0 : gcc_unreachable ();
13196 : : }
13197 : : }
13198 : :
13199 : 837597704 : if (declspecs->std_attributes
13200 : 837597704 : && !diagnose_misapplied_contracts (declspecs->std_attributes))
13201 : : {
13202 : 36 : location_t attr_loc = declspecs->locations[ds_std_attribute];
13203 : 36 : if (any_nonignored_attribute_p (declspecs->std_attributes)
13204 : 36 : && warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
13205 : 33 : inform (attr_loc, "an attribute that appertains to a type-specifier "
13206 : : "is ignored");
13207 : : }
13208 : :
13209 : 837597704 : if (attrlist)
13210 : 825522636 : diagnose_misapplied_contracts (*attrlist);
13211 : :
13212 : : /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
13213 : : bool is_xobj_member_function = false;
13214 : : /* Determine the type of the entity declared by recurring on the
13215 : : declarator. */
13216 : 1167383849 : for (; declarator; declarator = declarator->declarator)
13217 : : {
13218 : 818137411 : const cp_declarator *inner_declarator;
13219 : 818137411 : tree attrs;
13220 : :
13221 : 818137411 : if (type == error_mark_node)
13222 : 617 : return error_mark_node;
13223 : :
13224 : 818137054 : attrs = declarator->attributes;
13225 : 818137054 : if (attrs)
13226 : : {
13227 : 13122 : int attr_flags;
13228 : :
13229 : 13122 : attr_flags = 0;
13230 : 13122 : if (declarator->kind == cdk_id)
13231 : 12354 : attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
13232 : 13122 : if (declarator->kind == cdk_function)
13233 : 743 : attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
13234 : 13122 : if (declarator->kind == cdk_array)
13235 : 2 : attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
13236 : 13122 : tree late_attrs = NULL_TREE;
13237 : 13122 : if (decl_context != PARM && decl_context != TYPENAME)
13238 : : /* Assume that any attributes that get applied late to
13239 : : templates will DTRT when applied to the declaration
13240 : : as a whole. */
13241 : 13110 : late_attrs = splice_template_attributes (&attrs, type);
13242 : 13122 : returned_attrs = decl_attributes (&type,
13243 : : attr_chainon (returned_attrs,
13244 : : attrs),
13245 : : attr_flags);
13246 : 13122 : returned_attrs = attr_chainon (late_attrs, returned_attrs);
13247 : : }
13248 : :
13249 : 818137054 : inner_declarator = declarator->declarator;
13250 : :
13251 : : /* Check that contracts aren't misapplied. */
13252 : 818137054 : if (tree contract_attr = find_contract (declarator->std_attributes))
13253 : 470 : if (declarator->kind != cdk_function
13254 : 465 : || innermost_code != cdk_function)
13255 : 7 : diagnose_misapplied_contracts (contract_attr);
13256 : :
13257 : : /* We don't want to warn in parameter context because we don't
13258 : : yet know if the parse will succeed, and this might turn out
13259 : : to be a constructor call. */
13260 : 818137054 : if (decl_context != PARM
13261 : 818137054 : && decl_context != TYPENAME
13262 : 466407456 : && !typedef_p
13263 : 425389882 : && declarator->parenthesized != UNKNOWN_LOCATION
13264 : : /* If the type is class-like and the inner name used a
13265 : : global namespace qualifier, we need the parens.
13266 : : Unfortunately all we can tell is whether a qualified name
13267 : : was used or not. */
13268 : 818137267 : && !(inner_declarator
13269 : 145 : && inner_declarator->kind == cdk_id
13270 : 124 : && inner_declarator->u.id.qualifying_scope
13271 : 18 : && (MAYBE_CLASS_TYPE_P (type)
13272 : 12 : || TREE_CODE (type) == ENUMERAL_TYPE)))
13273 : : {
13274 : 201 : if (warning_at (declarator->parenthesized, OPT_Wparentheses,
13275 : : "unnecessary parentheses in declaration of %qs",
13276 : : name))
13277 : : {
13278 : 54 : gcc_rich_location iloc (declarator->parenthesized);
13279 : 54 : iloc.add_fixit_remove (get_start (declarator->parenthesized));
13280 : 54 : iloc.add_fixit_remove (get_finish (declarator->parenthesized));
13281 : 54 : inform (&iloc, "remove parentheses");
13282 : 54 : }
13283 : : }
13284 : 818137054 : if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
13285 : : break;
13286 : :
13287 : 329786405 : switch (declarator->kind)
13288 : : {
13289 : 3034634 : case cdk_array:
13290 : 6069268 : type = create_array_type_for_decl (dname, type,
13291 : 3034634 : declarator->u.array.bounds,
13292 : 3034634 : declarator->id_loc);
13293 : 3034634 : if (!valid_array_size_p (dname
13294 : : ? declarator->id_loc : input_location,
13295 : : type, dname))
13296 : 356 : type = error_mark_node;
13297 : :
13298 : 3034634 : if (declarator->std_attributes)
13299 : : /* [dcl.array]/1:
13300 : :
13301 : : The optional attribute-specifier-seq appertains to the
13302 : : array. */
13303 : 22 : returned_attrs = attr_chainon (returned_attrs,
13304 : : declarator->std_attributes);
13305 : : break;
13306 : :
13307 : 160194622 : case cdk_function:
13308 : 160194622 : {
13309 : 160194622 : tree arg_types;
13310 : 160194622 : int funcdecl_p;
13311 : :
13312 : : /* Declaring a function type. */
13313 : :
13314 : : /* Pick up type qualifiers which should be applied to `this'. */
13315 : 160194622 : memfn_quals = declarator->u.function.qualifiers;
13316 : : /* Pick up virt-specifiers. */
13317 : 160194622 : virt_specifiers = declarator->u.function.virt_specifiers;
13318 : : /* And ref-qualifier, too */
13319 : 160194622 : rqual = declarator->u.function.ref_qualifier;
13320 : : /* And tx-qualifier. */
13321 : 160194622 : tree tx_qual = declarator->u.function.tx_qualifier;
13322 : : /* Pick up the exception specifications. */
13323 : 160194622 : raises = declarator->u.function.exception_specification;
13324 : : /* If the exception-specification is ill-formed, let's pretend
13325 : : there wasn't one. */
13326 : 160194622 : if (raises == error_mark_node)
13327 : 9 : raises = NULL_TREE;
13328 : :
13329 : 320389244 : auto find_xobj_parm = [](tree parm_list)
13330 : : {
13331 : : /* There is no need to iterate over the list,
13332 : : only the first parm can be a valid xobj parm. */
13333 : 320139026 : if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier)
13334 : : return NULL_TREE;
13335 : : /* If we make it here, we are looking at an xobj parm.
13336 : :
13337 : : Non-null 'purpose' usually means the parm has a default
13338 : : argument, we don't want to violate this assumption. */
13339 : 8973 : TREE_PURPOSE (parm_list) = NULL_TREE;
13340 : 8973 : return TREE_VALUE (parm_list);
13341 : : };
13342 : :
13343 : 160194622 : tree xobj_parm
13344 : 160194622 : = find_xobj_parm (declarator->u.function.parameters);
13345 : 160194622 : is_xobj_member_function = xobj_parm;
13346 : :
13347 : 160194622 : if (xobj_parm && cxx_dialect < cxx23)
13348 : 34 : pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions,
13349 : : "explicit object member function only available "
13350 : : "with %<-std=c++23%> or %<-std=gnu++23%>");
13351 : :
13352 : 160194622 : if (xobj_parm && decl_context == TYPENAME)
13353 : : {
13354 : : /* We inform in every case, just differently depending on what
13355 : : case it is. */
13356 : 6 : auto_diagnostic_group d;
13357 : 6 : bool ptr_type = true;
13358 : : /* If declarator->kind is cdk_function and we are at the end of
13359 : : the declarator chain, we are looking at a function type. */
13360 : 6 : if (!declarator->declarator)
13361 : : {
13362 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13363 : : "a function type cannot "
13364 : : "have an explicit object parameter");
13365 : 2 : ptr_type = false;
13366 : : }
13367 : 4 : else if (declarator->declarator->kind == cdk_pointer)
13368 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13369 : : "a pointer to function type cannot "
13370 : : "have an explicit object parameter");
13371 : 2 : else if (declarator->declarator->kind == cdk_ptrmem)
13372 : 2 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13373 : : "a pointer to member function type "
13374 : : "cannot have an explicit object parameter");
13375 : : else
13376 : 0 : gcc_unreachable ();
13377 : :
13378 : : /* The locations being used here are probably not correct. */
13379 : 4 : if (ptr_type)
13380 : 4 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13381 : : "the type of a pointer to explicit object member "
13382 : : "function is a regular pointer to function type");
13383 : : else
13384 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13385 : : "the type of an explicit object "
13386 : : "member function is a regular function type");
13387 : : /* Ideally we should synthesize the correct syntax
13388 : : for the user, perhaps this could be added later. */
13389 : 6 : }
13390 : : /* Since a valid xobj parm has its purpose cleared in find_xobj_parm
13391 : : the first parm node will never erroneously be detected here. */
13392 : 160194622 : {
13393 : 160194622 : auto_diagnostic_group d;
13394 : 160194622 : bool bad_xobj_parm_encountered = false;
13395 : 160194622 : for (tree parm = declarator->u.function.parameters;
13396 : 405641363 : parm && parm != void_list_node;
13397 : 245446741 : parm = TREE_CHAIN (parm))
13398 : : {
13399 : 245446741 : if (TREE_PURPOSE (parm) != this_identifier)
13400 : 245446735 : continue;
13401 : 6 : bad_xobj_parm_encountered = true;
13402 : 6 : TREE_PURPOSE (parm) = NULL_TREE;
13403 : 6 : gcc_rich_location bad_xobj_parm
13404 : 6 : (DECL_SOURCE_LOCATION (TREE_VALUE (parm)));
13405 : 6 : error_at (&bad_xobj_parm,
13406 : : "only the first parameter of a member function "
13407 : : "can be declared as an explicit object parameter");
13408 : 6 : }
13409 : 160194622 : if (bad_xobj_parm_encountered && xobj_parm)
13410 : 0 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13411 : : "valid explicit object parameter declared here");
13412 : 160194622 : }
13413 : :
13414 : 160194622 : if (reqs)
13415 : 1 : error_at (location_of (reqs), "requires-clause on return type");
13416 : 160194622 : reqs = declarator->u.function.requires_clause;
13417 : :
13418 : : /* Say it's a definition only for the CALL_EXPR
13419 : : closest to the identifier. */
13420 : 160194622 : funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
13421 : :
13422 : : /* Handle a late-specified return type. */
13423 : 160194622 : tree late_return_type = declarator->u.function.late_return_type;
13424 : 160194622 : if (tree auto_node = type_uses_auto (type))
13425 : : {
13426 : 4265477 : if (!late_return_type)
13427 : : {
13428 : 1671574 : if (!funcdecl_p)
13429 : : /* auto (*fp)() = f; is OK. */;
13430 : 1671541 : else if (current_class_type
13431 : 3057592 : && LAMBDA_TYPE_P (current_class_type))
13432 : : /* OK for C++11 lambdas. */;
13433 : 1212777 : else if (cxx_dialect < cxx14)
13434 : : {
13435 : 1 : error_at (typespec_loc, "%qs function uses "
13436 : : "%<auto%> type specifier without "
13437 : : "trailing return type", name);
13438 : 1 : inform (typespec_loc,
13439 : : "deduced return type only available "
13440 : : "with %<-std=c++14%> or %<-std=gnu++14%>");
13441 : : }
13442 : 1212776 : else if (virtualp)
13443 : : {
13444 : 6 : error_at (typespec_loc, "virtual function "
13445 : : "cannot have deduced return type");
13446 : 6 : virtualp = false;
13447 : : }
13448 : : }
13449 : 2593903 : else if (!is_auto (type) && sfk != sfk_conversion)
13450 : : {
13451 : 15 : error_at (typespec_loc, "%qs function with trailing "
13452 : : "return type has %qT as its type rather "
13453 : : "than plain %<auto%>", name, type);
13454 : 256 : return error_mark_node;
13455 : : }
13456 : 2593888 : else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
13457 : : {
13458 : 9 : if (funcdecl_p)
13459 : 3 : error_at (typespec_loc,
13460 : : "%qs function with trailing return type "
13461 : : "has %<decltype(auto)%> as its type "
13462 : : "rather than plain %<auto%>", name);
13463 : : else
13464 : 6 : error_at (typespec_loc,
13465 : : "invalid use of %<decltype(auto)%>");
13466 : 9 : return error_mark_node;
13467 : : }
13468 : 4265453 : tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
13469 : 4265453 : if (!tmpl)
13470 : 3454074 : if (tree late_auto = type_uses_auto (late_return_type))
13471 : 13651 : tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
13472 : 3454074 : if (tmpl)
13473 : : {
13474 : 811383 : if (!funcdecl_p || !dguide_name_p (unqualified_id))
13475 : : {
13476 : 10 : auto_diagnostic_group g;
13477 : 10 : error_at (typespec_loc, "deduced class "
13478 : : "type %qD in function return type",
13479 : 10 : DECL_NAME (tmpl));
13480 : 10 : inform (DECL_SOURCE_LOCATION (tmpl),
13481 : : "%qD declared here", tmpl);
13482 : 10 : return error_mark_node;
13483 : 10 : }
13484 : 811373 : else if (!late_return_type)
13485 : : {
13486 : 2 : error_at (declarator->id_loc, "deduction guide "
13487 : : "for %qT must have trailing return "
13488 : 2 : "type", TREE_TYPE (tmpl));
13489 : 2 : inform (DECL_SOURCE_LOCATION (tmpl),
13490 : : "%qD declared here", tmpl);
13491 : 2 : return error_mark_node;
13492 : : }
13493 : 811371 : else if (CLASS_TYPE_P (late_return_type)
13494 : 811371 : && CLASSTYPE_TEMPLATE_INFO (late_return_type)
13495 : 1622742 : && (CLASSTYPE_TI_TEMPLATE (late_return_type)
13496 : : == tmpl))
13497 : : /* OK */;
13498 : : else
13499 : 0 : error ("trailing return type %qT of deduction guide "
13500 : : "is not a specialization of %qT",
13501 : 0 : late_return_type, TREE_TYPE (tmpl));
13502 : : }
13503 : : }
13504 : 155929145 : else if (late_return_type
13505 : 155929145 : && sfk != sfk_conversion)
13506 : : {
13507 : 37 : if (late_return_type == error_mark_node)
13508 : : return error_mark_node;
13509 : 37 : if (cxx_dialect < cxx11)
13510 : : /* Not using maybe_warn_cpp0x because this should
13511 : : always be an error. */
13512 : 1 : error_at (typespec_loc,
13513 : : "trailing return type only available "
13514 : : "with %<-std=c++11%> or %<-std=gnu++11%>");
13515 : : else
13516 : 36 : error_at (typespec_loc, "%qs function with trailing "
13517 : : "return type not declared with %<auto%> "
13518 : : "type specifier", name);
13519 : 37 : return error_mark_node;
13520 : : }
13521 : 160194549 : if (late_return_type && sfk == sfk_conversion)
13522 : : {
13523 : 15 : error ("a conversion function cannot have a trailing return type");
13524 : 15 : return error_mark_node;
13525 : : }
13526 : 160194534 : type = splice_late_return_type (type, late_return_type);
13527 : 160194534 : if (type == error_mark_node)
13528 : 72 : return error_mark_node;
13529 : :
13530 : 160194462 : if (late_return_type)
13531 : : {
13532 : 2593794 : late_return_type_p = true;
13533 : 2593794 : type_quals = cp_type_quals (type);
13534 : : }
13535 : :
13536 : 160194462 : if (type_quals != TYPE_UNQUALIFIED)
13537 : : {
13538 : : /* It's wrong, for instance, to issue a -Wignored-qualifiers
13539 : : warning for
13540 : : static_assert(!is_same_v<void(*)(), const void(*)()>);
13541 : : because there the qualifier matters. */
13542 : 12490 : if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)))
13543 : 271 : warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
13544 : : "qualifiers ignored on function return type");
13545 : : /* [dcl.fct] "A volatile-qualified return type is
13546 : : deprecated." */
13547 : 12490 : if (type_quals & TYPE_QUAL_VOLATILE)
13548 : 714 : warning_at (typespec_loc, OPT_Wvolatile,
13549 : : "%<volatile%>-qualified return type is "
13550 : : "deprecated");
13551 : :
13552 : : /* We now know that the TYPE_QUALS don't apply to the
13553 : : decl, but to its return type. */
13554 : : type_quals = TYPE_UNQUALIFIED;
13555 : : }
13556 : :
13557 : : /* Error about some types functions can't return. */
13558 : :
13559 : 160194462 : if (TREE_CODE (type) == FUNCTION_TYPE)
13560 : : {
13561 : 36 : error_at (typespec_loc, "%qs declared as function returning "
13562 : : "a function", name);
13563 : 36 : return error_mark_node;
13564 : : }
13565 : 160194426 : if (TREE_CODE (type) == ARRAY_TYPE)
13566 : : {
13567 : 6 : error_at (typespec_loc, "%qs declared as function returning "
13568 : : "an array", name);
13569 : 6 : return error_mark_node;
13570 : : }
13571 : 160194420 : if (constinit_p && funcdecl_p)
13572 : : {
13573 : 5 : error_at (declspecs->locations[ds_constinit],
13574 : : "%<constinit%> on function return type is not "
13575 : : "allowed");
13576 : 5 : return error_mark_node;
13577 : : }
13578 : :
13579 : 160194415 : if (check_decltype_auto (typespec_loc, type))
13580 : 49 : return error_mark_node;
13581 : :
13582 : 160194366 : if (ctype == NULL_TREE
13583 : 160194366 : && decl_context == FIELD
13584 : : && funcdecl_p
13585 : 85534232 : && friendp == 0)
13586 : 81888255 : ctype = current_class_type;
13587 : :
13588 : 160194366 : if (ctype && (sfk == sfk_constructor
13589 : 81888255 : || sfk == sfk_destructor))
13590 : : {
13591 : : /* We are within a class's scope. If our declarator name
13592 : : is the same as the class name, and we are defining
13593 : : a function, then it is a constructor/destructor, and
13594 : : therefore returns a void type. */
13595 : :
13596 : : /* ISO C++ 12.4/2. A destructor may not be declared
13597 : : const or volatile. A destructor may not be static.
13598 : : A destructor may not be declared with ref-qualifier.
13599 : :
13600 : : ISO C++ 12.1. A constructor may not be declared
13601 : : const or volatile. A constructor may not be
13602 : : virtual. A constructor may not be static.
13603 : : A constructor may not be declared with ref-qualifier. */
13604 : 21967545 : if (staticp == 2)
13605 : 12 : error_at (declspecs->locations[ds_storage_class],
13606 : : (flags == DTOR_FLAG)
13607 : : ? G_("destructor cannot be static member "
13608 : : "function")
13609 : : : G_("constructor cannot be static member "
13610 : : "function"));
13611 : 21967545 : if (memfn_quals)
13612 : : {
13613 : 12 : error ((flags == DTOR_FLAG)
13614 : : ? G_("destructors may not be cv-qualified")
13615 : : : G_("constructors may not be cv-qualified"));
13616 : 12 : memfn_quals = TYPE_UNQUALIFIED;
13617 : : }
13618 : :
13619 : 21967545 : if (rqual)
13620 : : {
13621 : 6 : maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13622 : 6 : error ((flags == DTOR_FLAG)
13623 : : ? G_("destructors may not be ref-qualified")
13624 : : : G_("constructors may not be ref-qualified"));
13625 : 6 : rqual = REF_QUAL_NONE;
13626 : : }
13627 : :
13628 : 21967545 : if (decl_context == FIELD
13629 : 21967545 : && !member_function_or_else (ctype,
13630 : : current_class_type,
13631 : : flags))
13632 : 0 : return error_mark_node;
13633 : :
13634 : 21967545 : if (flags != DTOR_FLAG)
13635 : : {
13636 : : /* It's a constructor. */
13637 : 19313840 : if (explicitp == 1)
13638 : 4692818 : explicitp = 2;
13639 : 19313840 : if (virtualp)
13640 : : {
13641 : 3 : permerror (declspecs->locations[ds_virtual],
13642 : : "constructors cannot be declared %<virtual%>");
13643 : 3 : virtualp = 0;
13644 : : }
13645 : 19313840 : if (decl_context == FIELD
13646 : 19313840 : && sfk != sfk_constructor)
13647 : 0 : return error_mark_node;
13648 : : }
13649 : 21967545 : if (decl_context == FIELD)
13650 : 21967545 : staticp = 0;
13651 : : }
13652 : 138226821 : else if (friendp)
13653 : : {
13654 : 3363090 : if (virtualp)
13655 : : {
13656 : : /* Cannot be both friend and virtual. */
13657 : 14 : gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13658 : 14 : richloc.add_range (declspecs->locations[ds_friend]);
13659 : 14 : error_at (&richloc, "virtual functions cannot be friends");
13660 : 14 : friendp = 0;
13661 : 14 : }
13662 : 3363090 : if (decl_context == NORMAL)
13663 : 0 : error_at (declarator->id_loc,
13664 : : "friend declaration not in class definition");
13665 : 3363090 : if (current_function_decl && funcdef_flag)
13666 : : {
13667 : 11 : error_at (declarator->id_loc,
13668 : : "cannot define friend function %qs in a local "
13669 : : "class definition", name);
13670 : 11 : friendp = 0;
13671 : : }
13672 : : /* [class.friend]/6: A function can be defined in a friend
13673 : : declaration if the function name is unqualified. */
13674 : 3363090 : if (funcdef_flag && in_namespace)
13675 : : {
13676 : 8 : if (in_namespace == global_namespace)
13677 : 4 : error_at (declarator->id_loc,
13678 : : "friend function definition %qs cannot have "
13679 : : "a name qualified with %<::%>", name);
13680 : : else
13681 : 4 : error_at (declarator->id_loc,
13682 : : "friend function definition %qs cannot have "
13683 : : "a name qualified with %<%D::%>", name,
13684 : : in_namespace);
13685 : : }
13686 : : }
13687 : 134863731 : else if (ctype && sfk == sfk_conversion)
13688 : : {
13689 : 518698 : if (explicitp == 1)
13690 : : {
13691 : 223789 : maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13692 : 223789 : explicitp = 2;
13693 : : }
13694 : : }
13695 : 134345033 : else if (sfk == sfk_deduction_guide)
13696 : : {
13697 : 811371 : if (explicitp == 1)
13698 : 160194366 : explicitp = 2;
13699 : : }
13700 : :
13701 : 160194366 : if (xobj_parm)
13702 : : {
13703 : 8973 : if (!ctype
13704 : 8973 : && decl_context == NORMAL
13705 : 49 : && (in_namespace
13706 : 49 : || !declarator->declarator->u.id.qualifying_scope))
13707 : 4 : error_at (DECL_SOURCE_LOCATION (xobj_parm),
13708 : : "a non-member function cannot have "
13709 : : "an explicit object parameter");
13710 : : else
13711 : : {
13712 : 8969 : if (virtualp)
13713 : : {
13714 : 16 : auto_diagnostic_group d;
13715 : 16 : error_at (declspecs->locations[ds_virtual],
13716 : : "an explicit object member function cannot "
13717 : : "be %<virtual%>");
13718 : 16 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13719 : : "explicit object parameter declared here");
13720 : 16 : virtualp = false;
13721 : 16 : }
13722 : 8969 : if (staticp >= 2)
13723 : : {
13724 : 2 : auto_diagnostic_group d;
13725 : 2 : error_at (declspecs->locations[ds_storage_class],
13726 : : "an explicit object member function cannot "
13727 : : "be %<static%>");
13728 : 2 : inform (DECL_SOURCE_LOCATION (xobj_parm),
13729 : : "explicit object parameter declared here");
13730 : 2 : }
13731 : : }
13732 : : }
13733 : 160194366 : tree pushed_scope = NULL_TREE;
13734 : 160194366 : if (funcdecl_p
13735 : 160194366 : && decl_context != FIELD
13736 : 72636149 : && inner_declarator->u.id.qualifying_scope
13737 : 168838880 : && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13738 : 8643252 : pushed_scope
13739 : 8643252 : = push_scope (inner_declarator->u.id.qualifying_scope);
13740 : :
13741 : 160194366 : arg_types = grokparms (declarator->u.function.parameters, &parms);
13742 : :
13743 : 160194366 : if (pushed_scope)
13744 : 8643252 : pop_scope (pushed_scope);
13745 : :
13746 : 160194366 : if (inner_declarator
13747 : 159721586 : && inner_declarator->kind == cdk_id
13748 : 157887494 : && inner_declarator->u.id.sfk == sfk_destructor
13749 : 2816665 : && arg_types != void_list_node)
13750 : : {
13751 : 26 : error_at (declarator->id_loc,
13752 : : "destructors may not have parameters");
13753 : 26 : arg_types = void_list_node;
13754 : 26 : parms = NULL_TREE;
13755 : 26 : is_xobj_member_function = false;
13756 : : }
13757 : :
13758 : 160194366 : type = build_function_type (type, arg_types);
13759 : :
13760 : 160194366 : tree attrs = declarator->std_attributes;
13761 : 160194366 : if (tx_qual)
13762 : : {
13763 : 152 : tree att = build_tree_list (tx_qual, NULL_TREE);
13764 : : /* transaction_safe applies to the type, but
13765 : : transaction_safe_dynamic applies to the function. */
13766 : 152 : if (is_attribute_p ("transaction_safe", tx_qual))
13767 : 121 : attrs = attr_chainon (attrs, att);
13768 : : else
13769 : 31 : returned_attrs = attr_chainon (returned_attrs, att);
13770 : : }
13771 : :
13772 : : /* Actually apply the contract attributes to the declaration. */
13773 : 160195164 : for (tree *p = &attrs; *p;)
13774 : : {
13775 : 798 : tree l = *p;
13776 : 798 : if (cxx_contract_attribute_p (l))
13777 : : {
13778 : 650 : *p = TREE_CHAIN (l);
13779 : : /* Intentionally reverse order of contracts so they're
13780 : : reversed back into their lexical order. */
13781 : 650 : TREE_CHAIN (l) = NULL_TREE;
13782 : 650 : returned_attrs = chainon (l, returned_attrs);
13783 : : }
13784 : : else
13785 : 148 : p = &TREE_CHAIN (l);
13786 : : }
13787 : :
13788 : 160194366 : if (attrs)
13789 : : /* [dcl.fct]/2:
13790 : :
13791 : : The optional attribute-specifier-seq appertains to
13792 : : the function type. */
13793 : 148 : cplus_decl_attributes (&type, attrs, 0);
13794 : :
13795 : 160194366 : if (raises)
13796 : 61652833 : type = build_exception_variant (type, raises);
13797 : : }
13798 : 160194366 : break;
13799 : :
13800 : 166557149 : case cdk_pointer:
13801 : 166557149 : case cdk_reference:
13802 : 166557149 : case cdk_ptrmem:
13803 : : /* Filter out pointers-to-references and references-to-references.
13804 : : We can get these if a TYPE_DECL is used. */
13805 : :
13806 : 166557149 : if (TYPE_REF_P (type))
13807 : : {
13808 : 236 : if (declarator->kind != cdk_reference)
13809 : : {
13810 : 4 : error ("cannot declare pointer to %q#T", type);
13811 : 4 : type = TREE_TYPE (type);
13812 : : }
13813 : :
13814 : : /* In C++0x, we allow reference to reference declarations
13815 : : that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13816 : : and template type arguments [14.3.1/4 temp.arg.type]. The
13817 : : check for direct reference to reference declarations, which
13818 : : are still forbidden, occurs below. Reasoning behind the change
13819 : : can be found in DR106, DR540, and the rvalue reference
13820 : : proposals. */
13821 : 232 : else if (cxx_dialect == cxx98)
13822 : : {
13823 : 0 : error ("cannot declare reference to %q#T", type);
13824 : 0 : type = TREE_TYPE (type);
13825 : : }
13826 : : }
13827 : 166556913 : else if (VOID_TYPE_P (type))
13828 : : {
13829 : 4245715 : if (declarator->kind == cdk_reference)
13830 : 3 : error ("cannot declare reference to %q#T", type);
13831 : 4245712 : else if (declarator->kind == cdk_ptrmem)
13832 : 4 : error ("cannot declare pointer to %q#T member", type);
13833 : : }
13834 : :
13835 : : /* We now know that the TYPE_QUALS don't apply to the decl,
13836 : : but to the target of the pointer. */
13837 : 166557149 : type_quals = TYPE_UNQUALIFIED;
13838 : :
13839 : : /* This code used to handle METHOD_TYPE, but I don't think it's
13840 : : possible to get it here anymore. */
13841 : 166557149 : gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13842 : 166557149 : if (declarator->kind == cdk_ptrmem
13843 : 1111595 : && TREE_CODE (type) == FUNCTION_TYPE)
13844 : : {
13845 : 926807 : memfn_quals |= type_memfn_quals (type);
13846 : 1853614 : type = build_memfn_type (type,
13847 : 926807 : declarator->u.pointer.class_type,
13848 : : memfn_quals,
13849 : : rqual);
13850 : 926807 : if (type == error_mark_node)
13851 : 4 : return error_mark_node;
13852 : :
13853 : : rqual = REF_QUAL_NONE;
13854 : : memfn_quals = TYPE_UNQUALIFIED;
13855 : : }
13856 : :
13857 : 166557145 : if (TREE_CODE (type) == FUNCTION_TYPE
13858 : 166557145 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13859 : 962569 : || type_memfn_rqual (type) != REF_QUAL_NONE))
13860 : 17 : error (declarator->kind == cdk_reference
13861 : : ? G_("cannot declare reference to qualified function type %qT")
13862 : : : G_("cannot declare pointer to qualified function type %qT"),
13863 : : type);
13864 : :
13865 : : /* When the pointed-to type involves components of variable size,
13866 : : care must be taken to ensure that the size evaluation code is
13867 : : emitted early enough to dominate all the possible later uses
13868 : : and late enough for the variables on which it depends to have
13869 : : been assigned.
13870 : :
13871 : : This is expected to happen automatically when the pointed-to
13872 : : type has a name/declaration of it's own, but special attention
13873 : : is required if the type is anonymous.
13874 : :
13875 : : We handle the NORMAL and FIELD contexts here by inserting a
13876 : : dummy statement that just evaluates the size at a safe point
13877 : : and ensures it is not deferred until e.g. within a deeper
13878 : : conditional context (c++/43555).
13879 : :
13880 : : We expect nothing to be needed here for PARM or TYPENAME.
13881 : : Evaluating the size at this point for TYPENAME would
13882 : : actually be incorrect, as we might be in the middle of an
13883 : : expression with side effects on the pointed-to type size
13884 : : "arguments" prior to the pointer declaration point and the
13885 : : size evaluation could end up prior to the side effects. */
13886 : :
13887 : 166557145 : if (!TYPE_NAME (type)
13888 : 5057893 : && (decl_context == NORMAL || decl_context == FIELD)
13889 : 555567 : && at_function_scope_p ()
13890 : 166596902 : && variably_modified_type_p (type, NULL_TREE))
13891 : : {
13892 : 111 : TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13893 : : NULL_TREE, type);
13894 : 111 : add_decl_expr (TYPE_NAME (type));
13895 : : }
13896 : :
13897 : 166557145 : if (declarator->kind == cdk_reference)
13898 : : {
13899 : : /* In C++0x, the type we are creating a reference to might be
13900 : : a typedef which is itself a reference type. In that case,
13901 : : we follow the reference collapsing rules in
13902 : : [7.1.3/8 dcl.typedef] to create the final reference type:
13903 : :
13904 : : "If a typedef TD names a type that is a reference to a type
13905 : : T, an attempt to create the type 'lvalue reference to cv TD'
13906 : : creates the type 'lvalue reference to T,' while an attempt
13907 : : to create the type "rvalue reference to cv TD' creates the
13908 : : type TD."
13909 : : */
13910 : 107838185 : if (VOID_TYPE_P (type))
13911 : : /* We already gave an error. */;
13912 : 107838182 : else if (TYPE_REF_P (type))
13913 : : {
13914 : 232 : if (declarator->u.reference.rvalue_ref)
13915 : : /* Leave type alone. */;
13916 : : else
13917 : 115 : type = cp_build_reference_type (TREE_TYPE (type), false);
13918 : : }
13919 : : else
13920 : 107837950 : type = cp_build_reference_type
13921 : 107837950 : (type, declarator->u.reference.rvalue_ref);
13922 : :
13923 : : /* In C++0x, we need this check for direct reference to
13924 : : reference declarations, which are forbidden by
13925 : : [8.3.2/5 dcl.ref]. Reference to reference declarations
13926 : : are only allowed indirectly through typedefs and template
13927 : : type arguments. Example:
13928 : :
13929 : : void foo(int & &); // invalid ref-to-ref decl
13930 : :
13931 : : typedef int & int_ref;
13932 : : void foo(int_ref &); // valid ref-to-ref decl
13933 : : */
13934 : 107838185 : if (inner_declarator && inner_declarator->kind == cdk_reference)
13935 : 0 : error ("cannot declare reference to %q#T, which is not "
13936 : : "a typedef or a template type argument", type);
13937 : : }
13938 : 58718960 : else if (TREE_CODE (type) == METHOD_TYPE)
13939 : 926803 : type = build_ptrmemfunc_type (build_pointer_type (type));
13940 : 57792157 : else if (declarator->kind == cdk_ptrmem)
13941 : : {
13942 : 184788 : gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13943 : : != NAMESPACE_DECL);
13944 : 184788 : if (declarator->u.pointer.class_type == error_mark_node)
13945 : : /* We will already have complained. */
13946 : 4 : type = error_mark_node;
13947 : : else
13948 : 184784 : type = build_ptrmem_type (declarator->u.pointer.class_type,
13949 : : type);
13950 : : }
13951 : : else
13952 : 57607369 : type = build_pointer_type (type);
13953 : :
13954 : : /* Process a list of type modifier keywords (such as
13955 : : const or volatile) that were given inside the `*' or `&'. */
13956 : :
13957 : 166557145 : if (declarator->u.pointer.qualifiers)
13958 : : {
13959 : 8019805 : type
13960 : 8019805 : = cp_build_qualified_type (type,
13961 : : declarator->u.pointer.qualifiers);
13962 : 8019805 : type_quals = cp_type_quals (type);
13963 : : }
13964 : :
13965 : : /* Apply C++11 attributes to the pointer, and not to the
13966 : : type pointed to. This is unlike what is done for GNU
13967 : : attributes above. It is to comply with [dcl.ptr]/1:
13968 : :
13969 : : [the optional attribute-specifier-seq (7.6.1) appertains
13970 : : to the pointer and not to the object pointed to]. */
13971 : 166557145 : if (declarator->std_attributes)
13972 : 12 : decl_attributes (&type, declarator->std_attributes,
13973 : : 0);
13974 : :
13975 : : ctype = NULL_TREE;
13976 : : break;
13977 : :
13978 : : case cdk_error:
13979 : : break;
13980 : :
13981 : 0 : default:
13982 : 0 : gcc_unreachable ();
13983 : : }
13984 : : }
13985 : :
13986 : 837597087 : id_loc = declarator ? declarator->id_loc : input_location;
13987 : :
13988 : 837597087 : if (innermost_code != cdk_function
13989 : : /* Don't check this if it can be the artifical decltype(auto)
13990 : : we created when building a constraint in a compound-requirement:
13991 : : that the type-constraint is plain is going to be checked in
13992 : : cp_parser_compound_requirement. */
13993 : 837597087 : && decl_context != TYPENAME
13994 : 837597087 : && check_decltype_auto (id_loc, type))
13995 : 19 : return error_mark_node;
13996 : :
13997 : : /* A `constexpr' specifier used in an object declaration declares
13998 : : the object as `const'. */
13999 : 837597068 : if (constexpr_p && innermost_code != cdk_function)
14000 : : {
14001 : : /* DR1688 says that a `constexpr' specifier in combination with
14002 : : `volatile' is valid. */
14003 : :
14004 : 11936181 : if (!TYPE_REF_P (type))
14005 : : {
14006 : 11926789 : type_quals |= TYPE_QUAL_CONST;
14007 : 11926789 : type = cp_build_qualified_type (type, type_quals);
14008 : : }
14009 : : }
14010 : :
14011 : 488294847 : if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
14012 : 1633668 : && !FUNC_OR_METHOD_TYPE_P (type)
14013 : 838404372 : && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
14014 : : {
14015 : 0 : error ("template-id %qD used as a declarator",
14016 : : unqualified_id);
14017 : 0 : unqualified_id = dname;
14018 : : }
14019 : :
14020 : : /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
14021 : : qualified with a class-name, turn it into a METHOD_TYPE, unless
14022 : : we know that the function is static. We take advantage of this
14023 : : opportunity to do other processing that pertains to entities
14024 : : explicitly declared to be class members. Note that if DECLARATOR
14025 : : is non-NULL, we know it is a cdk_id declarator; otherwise, we
14026 : : would not have exited the loop above. */
14027 : 837597068 : if (declarator
14028 : 488350633 : && declarator->kind == cdk_id
14029 : 488294847 : && declarator->u.id.qualifying_scope
14030 : 846688179 : && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
14031 : : {
14032 : 9002339 : ctype = declarator->u.id.qualifying_scope;
14033 : 9002339 : ctype = TYPE_MAIN_VARIANT (ctype);
14034 : 9002339 : template_count = num_template_headers_for_class (ctype);
14035 : :
14036 : 9002339 : if (ctype == current_class_type)
14037 : : {
14038 : 72 : if (friendp)
14039 : : {
14040 : 20 : permerror (declspecs->locations[ds_friend],
14041 : : "member functions are implicitly "
14042 : : "friends of their class");
14043 : 20 : friendp = 0;
14044 : : }
14045 : : else
14046 : 52 : permerror (id_loc, "extra qualification %<%T::%> on member %qs",
14047 : : ctype, name);
14048 : : }
14049 : 9002267 : else if (/* If the qualifying type is already complete, then we
14050 : : can skip the following checks. */
14051 : 9002267 : !COMPLETE_TYPE_P (ctype)
14052 : 199 : && (/* If the function is being defined, then
14053 : : qualifying type must certainly be complete. */
14054 : : funcdef_flag
14055 : : /* A friend declaration of "T::f" is OK, even if
14056 : : "T" is a template parameter. But, if this
14057 : : function is not a friend, the qualifying type
14058 : : must be a class. */
14059 : 146 : || (!friendp && !CLASS_TYPE_P (ctype))
14060 : : /* For a declaration, the type need not be
14061 : : complete, if either it is dependent (since there
14062 : : is no meaningful definition of complete in that
14063 : : case) or the qualifying class is currently being
14064 : : defined. */
14065 : 163 : || !(dependent_type_p (ctype)
14066 : 32 : || currently_open_class (ctype)))
14067 : : /* Check that the qualifying type is complete. */
14068 : 9002355 : && !complete_type_or_else (ctype, NULL_TREE))
14069 : 88 : return error_mark_node;
14070 : 9002179 : else if (TREE_CODE (type) == FUNCTION_TYPE)
14071 : : {
14072 : 8643556 : if (current_class_type
14073 : 340 : && (!friendp || funcdef_flag || initialized))
14074 : : {
14075 : 74 : error_at (id_loc, funcdef_flag || initialized
14076 : : ? G_("cannot define member function %<%T::%s%> "
14077 : : "within %qT")
14078 : : : G_("cannot declare member function %<%T::%s%> "
14079 : : "within %qT"),
14080 : : ctype, name, current_class_type);
14081 : 61 : return error_mark_node;
14082 : : }
14083 : : }
14084 : 358623 : else if (typedef_p && current_class_type)
14085 : : {
14086 : 0 : error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
14087 : : ctype, name, current_class_type);
14088 : 0 : return error_mark_node;
14089 : : }
14090 : : }
14091 : :
14092 : 837596919 : if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
14093 : 46254882 : ctype = current_class_type;
14094 : :
14095 : : /* Now TYPE has the actual type. */
14096 : :
14097 : 837596919 : if (returned_attrs)
14098 : : {
14099 : 3481 : if (attrlist)
14100 : 3481 : *attrlist = attr_chainon (returned_attrs, *attrlist);
14101 : : else
14102 : : attrlist = &returned_attrs;
14103 : : }
14104 : :
14105 : 837596919 : if (declarator
14106 : 488350484 : && declarator->kind == cdk_id
14107 : 488294698 : && declarator->std_attributes
14108 : 140389 : && attrlist != NULL)
14109 : : {
14110 : : /* [dcl.meaning]/1: The optional attribute-specifier-seq following
14111 : : a declarator-id appertains to the entity that is declared. */
14112 : 140386 : if (declarator->std_attributes != error_mark_node)
14113 : 140368 : *attrlist = attr_chainon (declarator->std_attributes, *attrlist);
14114 : : else
14115 : : /* We should have already diagnosed the issue (c++/78344). */
14116 : 18 : gcc_assert (seen_error ());
14117 : : }
14118 : :
14119 : : /* Handle parameter packs. */
14120 : 837596919 : if (parameter_pack_p)
14121 : : {
14122 : 2681453 : if (decl_context == PARM)
14123 : : /* Turn the type into a pack expansion.*/
14124 : 2681453 : type = make_pack_expansion (type);
14125 : : else
14126 : 0 : error ("non-parameter %qs cannot be a parameter pack", name);
14127 : : }
14128 : :
14129 : 837596919 : if ((decl_context == FIELD || decl_context == PARM)
14130 : 387603737 : && !processing_template_decl
14131 : 962194633 : && variably_modified_type_p (type, NULL_TREE))
14132 : : {
14133 : 16 : if (decl_context == FIELD)
14134 : 8 : error_at (id_loc,
14135 : : "data member may not have variably modified type %qT", type);
14136 : : else
14137 : 8 : error_at (id_loc,
14138 : : "parameter may not have variably modified type %qT", type);
14139 : 16 : type = error_mark_node;
14140 : : }
14141 : :
14142 : 837596919 : if (explicitp == 1 || (explicitp && friendp))
14143 : : {
14144 : : /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
14145 : : in the declaration of a constructor or conversion function within
14146 : : a class definition. */
14147 : 32 : if (!current_class_type)
14148 : 14 : error_at (declspecs->locations[ds_explicit],
14149 : : "%<explicit%> outside class declaration");
14150 : 18 : else if (friendp)
14151 : 15 : error_at (declspecs->locations[ds_explicit],
14152 : : "%<explicit%> in friend declaration");
14153 : : else
14154 : 3 : error_at (declspecs->locations[ds_explicit],
14155 : : "only declarations of constructors and conversion operators "
14156 : : "can be %<explicit%>");
14157 : : explicitp = 0;
14158 : : }
14159 : :
14160 : 837596919 : if (storage_class == sc_mutable)
14161 : : {
14162 : 146944 : location_t sloc = declspecs->locations[ds_storage_class];
14163 : 146944 : if (decl_context != FIELD || friendp)
14164 : : {
14165 : 16 : error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
14166 : : name);
14167 : 16 : storage_class = sc_none;
14168 : : }
14169 : 146928 : else if (decl_context == TYPENAME || typedef_p)
14170 : : {
14171 : 0 : error_at (sloc,
14172 : : "non-object member %qs cannot be declared %<mutable%>",
14173 : : name);
14174 : 0 : storage_class = sc_none;
14175 : : }
14176 : 146928 : else if (FUNC_OR_METHOD_TYPE_P (type))
14177 : : {
14178 : 4 : error_at (sloc, "function %qs cannot be declared %<mutable%>",
14179 : : name);
14180 : 4 : storage_class = sc_none;
14181 : : }
14182 : 146924 : else if (staticp)
14183 : : {
14184 : 0 : error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
14185 : : name);
14186 : 0 : storage_class = sc_none;
14187 : : }
14188 : 146924 : else if (type_quals & TYPE_QUAL_CONST)
14189 : : {
14190 : 12 : error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
14191 : : name);
14192 : 12 : storage_class = sc_none;
14193 : : }
14194 : 146912 : else if (TYPE_REF_P (type))
14195 : : {
14196 : 8 : permerror (sloc, "reference %qs cannot be declared %<mutable%>",
14197 : : name);
14198 : 8 : storage_class = sc_none;
14199 : : }
14200 : : }
14201 : :
14202 : : /* If this is declaring a typedef name, return a TYPE_DECL. */
14203 : 837596919 : if (typedef_p && decl_context != TYPENAME)
14204 : : {
14205 : 39766240 : bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
14206 : 39766240 : tree decl;
14207 : :
14208 : 39766240 : if (funcdef_flag)
14209 : : {
14210 : 8 : if (decl_context == NORMAL)
14211 : 4 : error_at (id_loc,
14212 : : "typedef may not be a function definition");
14213 : : else
14214 : 4 : error_at (id_loc,
14215 : : "typedef may not be a member function definition");
14216 : 8 : return error_mark_node;
14217 : : }
14218 : :
14219 : : /* This declaration:
14220 : :
14221 : : typedef void f(int) const;
14222 : :
14223 : : declares a function type which is not a member of any
14224 : : particular class, but which is cv-qualified; for
14225 : : example "f S::*" declares a pointer to a const-qualified
14226 : : member function of S. We record the cv-qualification in the
14227 : : function type. */
14228 : 39766232 : if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
14229 : : {
14230 : 78 : type = apply_memfn_quals (type, memfn_quals, rqual);
14231 : :
14232 : : /* We have now dealt with these qualifiers. */
14233 : 78 : memfn_quals = TYPE_UNQUALIFIED;
14234 : 78 : rqual = REF_QUAL_NONE;
14235 : : }
14236 : :
14237 : 39766232 : if (type_uses_auto (type))
14238 : : {
14239 : 38 : if (alias_p)
14240 : 17 : error_at (declspecs->locations[ds_type_spec],
14241 : : "%<auto%> not allowed in alias declaration");
14242 : : else
14243 : 21 : error_at (declspecs->locations[ds_type_spec],
14244 : : "typedef declared %<auto%>");
14245 : 38 : type = error_mark_node;
14246 : : }
14247 : :
14248 : 39766232 : if (reqs)
14249 : 1 : error_at (location_of (reqs), "requires-clause on typedef");
14250 : :
14251 : 39766232 : if (id_declarator && declarator->u.id.qualifying_scope)
14252 : : {
14253 : 29 : error_at (id_loc, "typedef name may not be a nested-name-specifier");
14254 : 29 : type = error_mark_node;
14255 : : }
14256 : :
14257 : 39766232 : if (decl_context == FIELD)
14258 : 21754686 : decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
14259 : : else
14260 : 18011546 : decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
14261 : :
14262 : 39766232 : if (decl_context != FIELD)
14263 : : {
14264 : 18011546 : if (!current_function_decl)
14265 : 7579358 : DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
14266 : 10432188 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
14267 : : /* The TYPE_DECL is "abstract" because there will be
14268 : : clones of this constructor/destructor, and there will
14269 : : be copies of this TYPE_DECL generated in those
14270 : : clones. The decloning optimization (for space) may
14271 : : revert this subsequently if it determines that
14272 : : the clones should share a common implementation. */
14273 : 121930 : DECL_ABSTRACT_P (decl) = true;
14274 : :
14275 : 18011546 : set_originating_module (decl);
14276 : : }
14277 : 21754686 : else if (current_class_type
14278 : 21754686 : && constructor_name_p (unqualified_id, current_class_type))
14279 : 3 : permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
14280 : : "as enclosing class",
14281 : : unqualified_id);
14282 : :
14283 : : /* If the user declares "typedef struct {...} foo" then the
14284 : : struct will have an anonymous name. Fill that name in now.
14285 : : Nothing can refer to it, so nothing needs know about the name
14286 : : change. */
14287 : 39766232 : if (type != error_mark_node
14288 : 39766165 : && unqualified_id
14289 : 39766161 : && TYPE_NAME (type)
14290 : 37304979 : && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14291 : 74728827 : && TYPE_UNNAMED_P (type)
14292 : 367492 : && declspecs->type_definition_p
14293 : 367450 : && attributes_naming_typedef_ok (*attrlist)
14294 : 40133678 : && cp_type_quals (type) == TYPE_UNQUALIFIED)
14295 : 367421 : name_unnamed_type (type, decl);
14296 : :
14297 : 39766232 : if (signed_p
14298 : 39766232 : || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
14299 : 337245 : C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
14300 : :
14301 : 39766232 : bad_specifiers (decl, BSP_TYPE, virtualp,
14302 : : memfn_quals != TYPE_UNQUALIFIED,
14303 : : inlinep, friendp, raises != NULL_TREE,
14304 : : declspecs->locations);
14305 : :
14306 : 39766232 : if (alias_p)
14307 : : /* Acknowledge that this was written:
14308 : : `using analias = atype;'. */
14309 : 14244244 : TYPE_DECL_ALIAS_P (decl) = 1;
14310 : :
14311 : 39766232 : return decl;
14312 : : }
14313 : :
14314 : : /* Detect the case of an array type of unspecified size
14315 : : which came, as such, direct from a typedef name.
14316 : : We must copy the type, so that the array's domain can be
14317 : : individually set by the object's initializer. */
14318 : :
14319 : 797830679 : if (type && typedef_type
14320 : 773614033 : && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
14321 : 798381449 : && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
14322 : 92 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14323 : :
14324 : : /* Detect where we're using a typedef of function type to declare a
14325 : : function. PARMS will not be set, so we must create it now. */
14326 : :
14327 : 797830679 : if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
14328 : : {
14329 : 15106 : tree decls = NULL_TREE;
14330 : 15106 : tree args;
14331 : :
14332 : 15106 : for (args = TYPE_ARG_TYPES (type);
14333 : 33308 : args && args != void_list_node;
14334 : 18202 : args = TREE_CHAIN (args))
14335 : : {
14336 : 18202 : tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
14337 : 18202 : TREE_VALUE (args));
14338 : :
14339 : 18202 : DECL_CHAIN (decl) = decls;
14340 : 18202 : decls = decl;
14341 : : }
14342 : :
14343 : 15106 : parms = nreverse (decls);
14344 : :
14345 : 15106 : if (decl_context != TYPENAME)
14346 : : {
14347 : : /* The qualifiers on the function type become the qualifiers on
14348 : : the non-static member function. */
14349 : 14448 : memfn_quals |= type_memfn_quals (type);
14350 : 14448 : rqual = type_memfn_rqual (type);
14351 : 14448 : type_quals = TYPE_UNQUALIFIED;
14352 : 14448 : raises = TYPE_RAISES_EXCEPTIONS (type);
14353 : : }
14354 : : }
14355 : :
14356 : : /* If this is a type name (such as, in a cast or sizeof),
14357 : : compute the type and return it now. */
14358 : :
14359 : 797830679 : if (decl_context == TYPENAME)
14360 : : {
14361 : : /* Note that here we don't care about type_quals. */
14362 : :
14363 : : /* Special case: "friend class foo" looks like a TYPENAME context. */
14364 : 308311702 : if (friendp)
14365 : : {
14366 : 0 : if (inlinep)
14367 : : {
14368 : 0 : error ("%<inline%> specified for friend class declaration");
14369 : 0 : inlinep = 0;
14370 : : }
14371 : :
14372 : 0 : if (!current_aggr)
14373 : : {
14374 : : /* Don't allow friend declaration without a class-key. */
14375 : 0 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14376 : 0 : permerror (input_location, "template parameters cannot be friends");
14377 : 0 : else if (TREE_CODE (type) == TYPENAME_TYPE)
14378 : 0 : permerror (input_location, "friend declaration requires class-key, "
14379 : : "i.e. %<friend class %T::%D%>",
14380 : 0 : TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
14381 : : else
14382 : 0 : permerror (input_location, "friend declaration requires class-key, "
14383 : : "i.e. %<friend %#T%>",
14384 : : type);
14385 : : }
14386 : :
14387 : : /* Only try to do this stuff if we didn't already give up. */
14388 : 0 : if (type != integer_type_node)
14389 : : {
14390 : : /* A friendly class? */
14391 : 0 : if (current_class_type)
14392 : 0 : make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
14393 : : /*complain=*/true);
14394 : : else
14395 : 0 : error ("trying to make class %qT a friend of global scope",
14396 : : type);
14397 : :
14398 : 0 : type = void_type_node;
14399 : : }
14400 : : }
14401 : 308311702 : else if (memfn_quals || rqual)
14402 : : {
14403 : 62797 : if (ctype == NULL_TREE
14404 : 62797 : && TREE_CODE (type) == METHOD_TYPE)
14405 : 0 : ctype = TYPE_METHOD_BASETYPE (type);
14406 : :
14407 : 62797 : if (ctype)
14408 : 0 : type = build_memfn_type (type, ctype, memfn_quals, rqual);
14409 : : /* Core issue #547: need to allow this in template type args.
14410 : : Allow it in general in C++11 for alias-declarations. */
14411 : 62797 : else if ((template_type_arg || cxx_dialect >= cxx11)
14412 : 62795 : && TREE_CODE (type) == FUNCTION_TYPE)
14413 : 62792 : type = apply_memfn_quals (type, memfn_quals, rqual);
14414 : : else
14415 : 5 : error ("invalid qualifiers on non-member function type");
14416 : : }
14417 : :
14418 : 308311702 : if (reqs)
14419 : 1 : error_at (location_of (reqs), "requires-clause on type-id");
14420 : :
14421 : 308311702 : return type;
14422 : : }
14423 : 489518977 : else if (unqualified_id == NULL_TREE && decl_context != PARM
14424 : 635043 : && decl_context != CATCHPARM
14425 : 184929 : && TREE_CODE (type) != UNION_TYPE
14426 : 184654 : && ! bitfield
14427 : 184654 : && innermost_code != cdk_decomp)
14428 : : {
14429 : 10 : error ("abstract declarator %qT used as declaration", type);
14430 : 10 : return error_mark_node;
14431 : : }
14432 : :
14433 : 489518967 : if (!FUNC_OR_METHOD_TYPE_P (type))
14434 : : {
14435 : : /* Only functions may be declared using an operator-function-id. */
14436 : 622345823 : if (dname && IDENTIFIER_ANY_OP_P (dname))
14437 : : {
14438 : 11 : error_at (id_loc, "declaration of %qD as non-function", dname);
14439 : 11 : return error_mark_node;
14440 : : }
14441 : :
14442 : 331667966 : if (reqs)
14443 : 5 : error_at (location_of (reqs),
14444 : : "requires-clause on declaration of non-function type %qT",
14445 : : type);
14446 : : }
14447 : :
14448 : : /* We don't check parameter types here because we can emit a better
14449 : : error message later. */
14450 : 489518956 : if (decl_context != PARM)
14451 : : {
14452 : 233421405 : type = check_var_type (unqualified_id, type, id_loc);
14453 : 233421405 : if (type == error_mark_node)
14454 : : return error_mark_node;
14455 : : }
14456 : :
14457 : : /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
14458 : : or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
14459 : :
14460 : 489518861 : if (decl_context == PARM || decl_context == CATCHPARM)
14461 : : {
14462 : 256559676 : if (ctype || in_namespace)
14463 : 0 : error ("cannot use %<::%> in parameter declaration");
14464 : :
14465 : 256559676 : tree auto_node = type_uses_auto (type);
14466 : 256559676 : if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
14467 : : {
14468 : 77 : bool err_p = true;
14469 : 77 : if (cxx_dialect >= cxx14)
14470 : : {
14471 : 142 : if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
14472 : 4 : error_at (typespec_loc,
14473 : : "cannot declare a parameter with %<decltype(auto)%>");
14474 : 73 : else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14475 : : {
14476 : 3 : auto_diagnostic_group g;
14477 : 3 : gcc_rich_location richloc (typespec_loc);
14478 : 3 : richloc.add_fixit_insert_after ("<>");
14479 : 3 : error_at (&richloc,
14480 : : "missing template argument list after %qE; template "
14481 : : "placeholder not permitted in parameter", c);
14482 : 3 : if (decl_context == PARM && cxx_dialect >= cxx20)
14483 : 2 : inform (typespec_loc, "or use %<auto%> for an "
14484 : : "abbreviated function template");
14485 : 3 : inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c);
14486 : 3 : }
14487 : 70 : else if (decl_context == CATCHPARM || template_parm_flag)
14488 : 14 : error_at (typespec_loc,
14489 : : "%<auto%> parameter not permitted in this context");
14490 : : else
14491 : : /* Do not issue an error while tentatively parsing a function
14492 : : parameter: for T t(auto(a), 42);, when we just saw the 1st
14493 : : parameter, we don't know yet that this construct won't be
14494 : : a function declaration. Defer the checking to
14495 : : cp_parser_parameter_declaration_clause. */
14496 : : err_p = false;
14497 : : }
14498 : : else
14499 : 0 : error_at (typespec_loc, "parameter declared %<auto%>");
14500 : 21 : if (err_p)
14501 : 21 : type = error_mark_node;
14502 : : }
14503 : :
14504 : : /* A parameter declared as an array of T is really a pointer to T.
14505 : : One declared as a function is really a pointer to a function.
14506 : : One declared as a member is really a pointer to member. */
14507 : :
14508 : 256559676 : if (TREE_CODE (type) == ARRAY_TYPE)
14509 : : {
14510 : : /* Transfer const-ness of array into that of type pointed to. */
14511 : 701110 : type = build_pointer_type (TREE_TYPE (type));
14512 : 701110 : type_quals = TYPE_UNQUALIFIED;
14513 : 701110 : array_parameter_p = true;
14514 : : }
14515 : 255858566 : else if (TREE_CODE (type) == FUNCTION_TYPE)
14516 : 100874 : type = build_pointer_type (type);
14517 : : }
14518 : :
14519 : 115390514 : if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
14520 : : /* Don't convert xobj member functions to METHOD_TYPE. */
14521 : 90531547 : && !is_xobj_member_function
14522 : 571836975 : && !(unqualified_id
14523 : 164626425 : && identifier_p (unqualified_id)
14524 : 146996225 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
14525 : : {
14526 : 82308632 : cp_cv_quals real_quals = memfn_quals;
14527 : 82308632 : if (cxx_dialect < cxx14 && constexpr_p
14528 : 631152 : && sfk != sfk_constructor && sfk != sfk_destructor)
14529 : 1703 : real_quals |= TYPE_QUAL_CONST;
14530 : 82308632 : type = build_memfn_type (type, ctype, real_quals, rqual);
14531 : : }
14532 : :
14533 : 489518861 : {
14534 : 489518861 : tree decl = NULL_TREE;
14535 : :
14536 : 489518861 : if (decl_context == PARM)
14537 : : {
14538 : 256097551 : decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
14539 : 256097551 : DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
14540 : :
14541 : 256097551 : bad_specifiers (decl, BSP_PARM, virtualp,
14542 : : memfn_quals != TYPE_UNQUALIFIED,
14543 : : inlinep, friendp, raises != NULL_TREE,
14544 : : declspecs->locations);
14545 : : }
14546 : 233421310 : else if (decl_context == FIELD)
14547 : : {
14548 : 109751473 : if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
14549 : 15358953 : if (tree auto_node = type_uses_auto (type))
14550 : : {
14551 : 21 : location_t tloc = declspecs->locations[ds_type_spec];
14552 : 21 : if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14553 : 6 : error_at (tloc, "invalid use of template-name %qE without an "
14554 : : "argument list",
14555 : 6 : CLASS_PLACEHOLDER_TEMPLATE (auto_node));
14556 : : else
14557 : 15 : error_at (tloc, "non-static data member declared with "
14558 : : "placeholder %qT", auto_node);
14559 : 21 : type = error_mark_node;
14560 : : }
14561 : :
14562 : : /* The C99 flexible array extension. */
14563 : 92405579 : if (!staticp && TREE_CODE (type) == ARRAY_TYPE
14564 : 110786089 : && TYPE_DOMAIN (type) == NULL_TREE)
14565 : : {
14566 : 1542 : if (ctype
14567 : 1542 : && (TREE_CODE (ctype) == UNION_TYPE
14568 : 1542 : || TREE_CODE (ctype) == QUAL_UNION_TYPE))
14569 : : {
14570 : 32 : error_at (id_loc, "flexible array member in union");
14571 : 32 : type = error_mark_node;
14572 : : }
14573 : : else
14574 : : {
14575 : : /* Array is a flexible member. */
14576 : 1510 : if (name)
14577 : 1510 : pedwarn (id_loc, OPT_Wpedantic,
14578 : : "ISO C++ forbids flexible array member %qs", name);
14579 : : else
14580 : : pedwarn (input_location, OPT_Wpedantic,
14581 : : "ISO C++ forbids flexible array members");
14582 : :
14583 : : /* Flexible array member has a null domain. */
14584 : 1510 : type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
14585 : : }
14586 : : }
14587 : :
14588 : 109751473 : if (type == error_mark_node)
14589 : : {
14590 : : /* Happens when declaring arrays of sizes which
14591 : : are error_mark_node, for example. */
14592 : 53 : decl = NULL_TREE;
14593 : : }
14594 : 109751420 : else if (in_namespace && !friendp)
14595 : : {
14596 : : /* Something like struct S { int N::j; }; */
14597 : 8 : error_at (id_loc, "invalid use of %<::%>");
14598 : 8 : return error_mark_node;
14599 : : }
14600 : 109751412 : else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
14601 : : {
14602 : 85251070 : int publicp = 0;
14603 : 85251070 : tree function_context;
14604 : :
14605 : 85251070 : if (friendp == 0)
14606 : : {
14607 : : /* This should never happen in pure C++ (the check
14608 : : could be an assert). It could happen in
14609 : : Objective-C++ if someone writes invalid code that
14610 : : uses a function declaration for an instance
14611 : : variable or property (instance variables and
14612 : : properties are parsed as FIELD_DECLs, but they are
14613 : : part of an Objective-C class, not a C++ class).
14614 : : That code is invalid and is caught by this
14615 : : check. */
14616 : 81888052 : if (!ctype)
14617 : : {
14618 : 0 : error ("declaration of function %qD in invalid context",
14619 : : unqualified_id);
14620 : 0 : return error_mark_node;
14621 : : }
14622 : :
14623 : : /* ``A union may [ ... ] not [ have ] virtual functions.''
14624 : : ARM 9.5 */
14625 : 81888052 : if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
14626 : : {
14627 : 3 : error_at (declspecs->locations[ds_virtual],
14628 : : "function %qD declared %<virtual%> inside a union",
14629 : : unqualified_id);
14630 : 3 : return error_mark_node;
14631 : : }
14632 : :
14633 : 3811111 : if (virtualp
14634 : 89062178 : && identifier_p (unqualified_id)
14635 : 7621668 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14636 : : {
14637 : 11 : error_at (declspecs->locations[ds_virtual],
14638 : : "%qD cannot be declared %<virtual%>, since it "
14639 : : "is always static", unqualified_id);
14640 : 11 : virtualp = 0;
14641 : : }
14642 : : }
14643 : :
14644 : : /* Check that the name used for a destructor makes sense. */
14645 : 85251067 : if (sfk == sfk_destructor)
14646 : : {
14647 : 2653753 : tree uqname = id_declarator->u.id.unqualified_name;
14648 : :
14649 : 2653753 : if (!ctype)
14650 : : {
14651 : 16 : gcc_assert (friendp);
14652 : 16 : error_at (id_loc, "expected qualified name in friend "
14653 : : "declaration for destructor %qD", uqname);
14654 : 16 : return error_mark_node;
14655 : : }
14656 : :
14657 : 2653737 : if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
14658 : : {
14659 : 12 : error_at (id_loc, "declaration of %qD as member of %qT",
14660 : : uqname, ctype);
14661 : 12 : return error_mark_node;
14662 : : }
14663 : 2653725 : if (concept_p)
14664 : : {
14665 : 3 : error_at (declspecs->locations[ds_concept],
14666 : : "a destructor cannot be %qs", "concept");
14667 : 3 : return error_mark_node;
14668 : : }
14669 : 2653722 : if (constexpr_p && cxx_dialect < cxx20)
14670 : : {
14671 : 6 : error_at (declspecs->locations[ds_constexpr],
14672 : : "%<constexpr%> destructors only available"
14673 : : " with %<-std=c++20%> or %<-std=gnu++20%>");
14674 : 6 : return error_mark_node;
14675 : : }
14676 : 2653716 : if (consteval_p)
14677 : : {
14678 : 1 : error_at (declspecs->locations[ds_consteval],
14679 : : "a destructor cannot be %qs", "consteval");
14680 : 1 : return error_mark_node;
14681 : : }
14682 : : }
14683 : 82597314 : else if (sfk == sfk_constructor && friendp && !ctype)
14684 : : {
14685 : 0 : error ("expected qualified name in friend declaration "
14686 : : "for constructor %qD",
14687 : 0 : id_declarator->u.id.unqualified_name);
14688 : 0 : return error_mark_node;
14689 : : }
14690 : 85251029 : if (sfk == sfk_constructor)
14691 : 19313868 : if (concept_p)
14692 : : {
14693 : 3 : error_at (declspecs->locations[ds_concept],
14694 : : "a constructor cannot be %<concept%>");
14695 : 3 : return error_mark_node;
14696 : : }
14697 : 65937161 : if (concept_p)
14698 : : {
14699 : 5 : error_at (declspecs->locations[ds_concept],
14700 : : "a concept cannot be a member function");
14701 : 5 : concept_p = false;
14702 : : }
14703 : 85251021 : else if (consteval_p
14704 : 85296055 : && identifier_p (unqualified_id)
14705 : 85341064 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14706 : : {
14707 : 3 : error_at (declspecs->locations[ds_consteval],
14708 : : "%qD cannot be %qs", unqualified_id, "consteval");
14709 : 3 : consteval_p = false;
14710 : : }
14711 : :
14712 : 85251026 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14713 : : {
14714 : 11042 : tree tmpl = TREE_OPERAND (unqualified_id, 0);
14715 : 11042 : if (variable_template_p (tmpl))
14716 : : {
14717 : 3 : error_at (id_loc, "specialization of variable template "
14718 : : "%qD declared as function", tmpl);
14719 : 3 : inform (DECL_SOURCE_LOCATION (tmpl),
14720 : : "variable template declared here");
14721 : 3 : return error_mark_node;
14722 : : }
14723 : : }
14724 : :
14725 : : /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14726 : 85251023 : function_context
14727 : 85251023 : = (ctype != NULL_TREE
14728 : 85251023 : ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14729 : 170502046 : publicp = ((! friendp || ! staticp)
14730 : 85251023 : && function_context == NULL_TREE);
14731 : :
14732 : 85251023 : decl = grokfndecl (ctype, type,
14733 : 85251023 : TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14734 : : ? unqualified_id : dname,
14735 : : parms,
14736 : : unqualified_id,
14737 : : declspecs,
14738 : : reqs,
14739 : : virtualp, flags, memfn_quals, rqual, raises,
14740 : : friendp ? -1 : 0, friendp, publicp,
14741 : 85251023 : inlinep | (2 * constexpr_p) | (4 * concept_p)
14742 : 85251023 : | (8 * consteval_p),
14743 : : initialized == SD_DELETED,
14744 : : is_xobj_member_function, sfk,
14745 : : funcdef_flag, late_return_type_p,
14746 : : template_count, in_namespace,
14747 : : attrlist, id_loc);
14748 : 85251023 : decl = set_virt_specifiers (decl, virt_specifiers);
14749 : 85251023 : if (decl == NULL_TREE)
14750 : 117 : return error_mark_node;
14751 : : #if 0
14752 : : /* This clobbers the attrs stored in `decl' from `attrlist'. */
14753 : : /* The decl and setting of decl_attr is also turned off. */
14754 : : decl = build_decl_attribute_variant (decl, decl_attr);
14755 : : #endif
14756 : :
14757 : : /* [class.conv.ctor]
14758 : :
14759 : : A constructor declared without the function-specifier
14760 : : explicit that can be called with a single parameter
14761 : : specifies a conversion from the type of its first
14762 : : parameter to the type of its class. Such a constructor
14763 : : is called a converting constructor. */
14764 : 85250906 : if (explicitp == 2)
14765 : 4916607 : DECL_NONCONVERTING_P (decl) = 1;
14766 : :
14767 : 85250906 : if (declspecs->explicit_specifier)
14768 : 97786 : store_explicit_specifier (decl, declspecs->explicit_specifier);
14769 : : }
14770 : 24500342 : else if (!staticp
14771 : 24500342 : && ((current_class_type
14772 : 15358922 : && same_type_p (type, current_class_type))
14773 : 15358825 : || (!dependent_type_p (type)
14774 : 9696232 : && !COMPLETE_TYPE_P (complete_type (type))
14775 : 1558 : && (!complete_or_array_type_p (type)
14776 : 1475 : || initialized == SD_UNINITIALIZED))))
14777 : : {
14778 : 1625 : if (TREE_CODE (type) != ARRAY_TYPE
14779 : 1625 : || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14780 : : {
14781 : 180 : if (unqualified_id)
14782 : : {
14783 : 124 : error_at (id_loc, "field %qD has incomplete type %qT",
14784 : : unqualified_id, type);
14785 : 124 : cxx_incomplete_type_inform (strip_array_types (type));
14786 : : }
14787 : : else
14788 : 56 : error ("name %qT has incomplete type", type);
14789 : :
14790 : 180 : type = error_mark_node;
14791 : 180 : decl = NULL_TREE;
14792 : : }
14793 : : }
14794 : 39855998 : else if (!verify_type_context (input_location,
14795 : : staticp
14796 : : ? TCTX_STATIC_STORAGE
14797 : : : TCTX_FIELD, type))
14798 : : {
14799 : 0 : type = error_mark_node;
14800 : 0 : decl = NULL_TREE;
14801 : : }
14802 : : else
14803 : : {
14804 : 24498709 : if (friendp)
14805 : : {
14806 : 18 : if (unqualified_id)
14807 : 14 : error_at (id_loc,
14808 : : "%qE is neither function nor member function; "
14809 : : "cannot be declared friend", unqualified_id);
14810 : : else
14811 : 4 : error ("unnamed field is neither function nor member "
14812 : : "function; cannot be declared friend");
14813 : 18 : return error_mark_node;
14814 : : }
14815 : 24498691 : decl = NULL_TREE;
14816 : : }
14817 : :
14818 : 109751275 : if (friendp)
14819 : : {
14820 : : /* Packages tend to use GNU attributes on friends, so we only
14821 : : warn for standard attributes. */
14822 : 3362981 : if (attrlist
14823 : 3362981 : && !funcdef_flag
14824 : 1164194 : && cxx11_attribute_p (*attrlist)
14825 : 3363011 : && !all_attributes_are_contracts_p (*attrlist))
14826 : : {
14827 : 27 : *attrlist = NULL_TREE;
14828 : 27 : if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14829 : 27 : inform (id_loc, "an attribute that appertains to a friend "
14830 : : "declaration that is not a definition is ignored");
14831 : : }
14832 : : /* Friends are treated specially. */
14833 : 3362981 : if (ctype == current_class_type)
14834 : : ; /* We already issued a permerror. */
14835 : 3362981 : else if (decl && DECL_NAME (decl))
14836 : : {
14837 : 3362977 : set_originating_module (decl, true);
14838 : :
14839 : 3362977 : if (initialized)
14840 : : /* Kludge: We need funcdef_flag to be true in do_friend for
14841 : : in-class defaulted functions, but that breaks grokfndecl.
14842 : : So set it here. */
14843 : 39793 : funcdef_flag = true;
14844 : :
14845 : 3362977 : cplus_decl_attributes (&decl, *attrlist, 0);
14846 : 3362977 : *attrlist = NULL_TREE;
14847 : :
14848 : 3362977 : tree scope = ctype ? ctype : in_namespace;
14849 : 3362977 : decl = do_friend (scope, unqualified_id, decl,
14850 : : flags, funcdef_flag);
14851 : 3362977 : return decl;
14852 : : }
14853 : : else
14854 : 4 : return error_mark_node;
14855 : : }
14856 : :
14857 : : /* Structure field. It may not be a function, except for C++. */
14858 : :
14859 : 106388294 : if (decl == NULL_TREE)
14860 : : {
14861 : 24500365 : if (staticp)
14862 : : {
14863 : : /* C++ allows static class members. All other work
14864 : : for this is done by grokfield. */
14865 : 9141420 : decl = build_lang_decl_loc (id_loc, VAR_DECL,
14866 : : dname, type);
14867 : 9141420 : if (unqualified_id
14868 : 9141416 : && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14869 : : {
14870 : 15 : decl = check_explicit_specialization (unqualified_id, decl,
14871 : : template_count,
14872 : : concept_p * 8);
14873 : 15 : if (decl == error_mark_node)
14874 : : return error_mark_node;
14875 : : }
14876 : 9141420 : set_linkage_for_static_data_member (decl);
14877 : 9141420 : if (concept_p)
14878 : 5 : error_at (declspecs->locations[ds_concept],
14879 : : "static data member %qE declared %qs",
14880 : : unqualified_id, "concept");
14881 : 9141415 : else if (constexpr_p && !initialized)
14882 : : {
14883 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
14884 : : "%<constexpr%> static data member %qD must "
14885 : : "have an initializer", decl);
14886 : 9 : constexpr_p = false;
14887 : : }
14888 : 9141420 : if (consteval_p)
14889 : 1 : error_at (declspecs->locations[ds_consteval],
14890 : : "static data member %qE declared %qs",
14891 : : unqualified_id, "consteval");
14892 : :
14893 : 9141420 : if (inlinep)
14894 : 1568 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
14895 : :
14896 : 9141420 : if (!DECL_VAR_DECLARED_INLINE_P (decl)
14897 : 9141420 : && !(cxx_dialect >= cxx17 && constexpr_p))
14898 : : /* Even if there is an in-class initialization, DECL
14899 : : is considered undefined until an out-of-class
14900 : : definition is provided, unless this is an inline
14901 : : variable. */
14902 : 1633808 : DECL_EXTERNAL (decl) = 1;
14903 : :
14904 : 9141420 : if (thread_p)
14905 : : {
14906 : 397 : CP_DECL_THREAD_LOCAL_P (decl) = true;
14907 : 397 : if (!processing_template_decl)
14908 : 366 : set_decl_tls_model (decl, decl_default_tls_model (decl));
14909 : 397 : if (declspecs->gnu_thread_keyword_p)
14910 : 39 : SET_DECL_GNU_TLS_P (decl);
14911 : : }
14912 : :
14913 : : /* Set the constraints on the declaration. */
14914 : 9141420 : bool memtmpl = (current_template_depth
14915 : 9141420 : > template_class_depth (current_class_type));
14916 : 9141420 : if (memtmpl)
14917 : : {
14918 : 91117 : tree ci = current_template_constraints ();
14919 : 91117 : set_constraints (decl, ci);
14920 : : }
14921 : : }
14922 : : else
14923 : : {
14924 : 15358945 : if (concept_p)
14925 : : {
14926 : 4 : error_at (declspecs->locations[ds_concept],
14927 : : "non-static data member %qE declared %qs",
14928 : : unqualified_id, "concept");
14929 : 4 : concept_p = false;
14930 : 4 : constexpr_p = false;
14931 : : }
14932 : 15358941 : else if (constexpr_p)
14933 : : {
14934 : 9 : error_at (declspecs->locations[ds_constexpr],
14935 : : "non-static data member %qE declared %qs",
14936 : : unqualified_id, "constexpr");
14937 : 9 : constexpr_p = false;
14938 : : }
14939 : 15358945 : if (constinit_p)
14940 : : {
14941 : 2 : error_at (declspecs->locations[ds_constinit],
14942 : : "non-static data member %qE declared %qs",
14943 : : unqualified_id, "constinit");
14944 : 2 : constinit_p = false;
14945 : : }
14946 : 15358945 : if (consteval_p)
14947 : : {
14948 : 1 : error_at (declspecs->locations[ds_consteval],
14949 : : "non-static data member %qE declared %qs",
14950 : : unqualified_id, "consteval");
14951 : 1 : consteval_p = false;
14952 : : }
14953 : 15358945 : decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14954 : 15358945 : DECL_NONADDRESSABLE_P (decl) = bitfield;
14955 : 15358945 : if (bitfield && !unqualified_id)
14956 : 128846 : DECL_PADDING_P (decl) = 1;
14957 : :
14958 : 15358945 : if (storage_class == sc_mutable)
14959 : : {
14960 : 146904 : DECL_MUTABLE_P (decl) = 1;
14961 : 146904 : storage_class = sc_none;
14962 : : }
14963 : :
14964 : 15358945 : if (initialized)
14965 : : {
14966 : : /* An attempt is being made to initialize a non-static
14967 : : member. This is new in C++11. */
14968 : 856519 : maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
14969 : :
14970 : : /* If this has been parsed with static storage class, but
14971 : : errors forced staticp to be cleared, ensure NSDMI is
14972 : : not present. */
14973 : 856519 : if (declspecs->storage_class == sc_static)
14974 : 3 : DECL_INITIAL (decl) = error_mark_node;
14975 : : }
14976 : : }
14977 : :
14978 : 39859310 : bad_specifiers (decl, BSP_FIELD, virtualp,
14979 : : memfn_quals != TYPE_UNQUALIFIED,
14980 : : staticp ? false : inlinep, friendp,
14981 : : raises != NULL_TREE,
14982 : : declspecs->locations);
14983 : : }
14984 : : }
14985 : 123669837 : else if (FUNC_OR_METHOD_TYPE_P (type))
14986 : : {
14987 : 72499038 : tree original_name;
14988 : 72499038 : int publicp = 0;
14989 : :
14990 : 72499038 : if (!unqualified_id)
14991 : 0 : return error_mark_node;
14992 : :
14993 : 72499038 : if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14994 : : original_name = dname;
14995 : : else
14996 : 71683723 : original_name = unqualified_id;
14997 : : // FIXME:gcc_assert (original_name == dname);
14998 : :
14999 : 72499038 : if (storage_class == sc_auto)
15000 : 1 : error_at (declspecs->locations[ds_storage_class],
15001 : : "storage class %<auto%> invalid for function %qs", name);
15002 : 72499037 : else if (storage_class == sc_register)
15003 : 10 : error_at (declspecs->locations[ds_storage_class],
15004 : : "storage class %<register%> invalid for function %qs",
15005 : : name);
15006 : 72499027 : else if (thread_p)
15007 : : {
15008 : 16 : if (declspecs->gnu_thread_keyword_p)
15009 : 16 : error_at (declspecs->locations[ds_thread],
15010 : : "storage class %<__thread%> invalid for function %qs",
15011 : : name);
15012 : : else
15013 : 0 : error_at (declspecs->locations[ds_thread],
15014 : : "storage class %<thread_local%> invalid for "
15015 : : "function %qs", name);
15016 : : }
15017 : 72499011 : else if (constinit_p)
15018 : 1 : error_at (declspecs->locations[ds_constinit],
15019 : : "%<constinit%> specifier invalid for function %qs", name);
15020 : :
15021 : 72499038 : if (virt_specifiers)
15022 : 6 : error ("virt-specifiers in %qs not allowed outside a class "
15023 : : "definition", name);
15024 : : /* Function declaration not at top level.
15025 : : Storage classes other than `extern' are not allowed
15026 : : and `extern' makes no difference. */
15027 : 72499038 : if (! toplevel_bindings_p ()
15028 : 29818 : && (storage_class == sc_static
15029 : 29807 : || decl_spec_seq_has_spec_p (declspecs, ds_inline))
15030 : 72499052 : && pedantic)
15031 : : {
15032 : 10 : if (storage_class == sc_static)
15033 : 7 : pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
15034 : : "%<static%> specifier invalid for function %qs "
15035 : : "declared out of global scope", name);
15036 : : else
15037 : 3 : pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
15038 : : "%<inline%> specifier invalid for function %qs "
15039 : : "declared out of global scope", name);
15040 : : }
15041 : :
15042 : 72499038 : if (ctype == NULL_TREE)
15043 : : {
15044 : 63855830 : if (virtualp)
15045 : : {
15046 : 0 : error ("virtual non-class function %qs", name);
15047 : 0 : virtualp = 0;
15048 : : }
15049 : 63855830 : else if (sfk == sfk_constructor
15050 : 63855830 : || sfk == sfk_destructor)
15051 : : {
15052 : 4 : error (funcdef_flag
15053 : : ? G_("%qs defined in a non-class scope")
15054 : : : G_("%qs declared in a non-class scope"), name);
15055 : 4 : sfk = sfk_none;
15056 : : }
15057 : : }
15058 : 72499038 : if (consteval_p
15059 : 72516181 : && identifier_p (unqualified_id)
15060 : 72533319 : && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
15061 : : {
15062 : 3 : error_at (declspecs->locations[ds_consteval],
15063 : : "%qD cannot be %qs", unqualified_id, "consteval");
15064 : 3 : consteval_p = false;
15065 : : }
15066 : :
15067 : : /* Record whether the function is public. */
15068 : 72499038 : publicp = (ctype != NULL_TREE
15069 : 72499038 : || storage_class != sc_static);
15070 : :
15071 : 144998076 : decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
15072 : : declspecs,
15073 : : reqs, virtualp, flags, memfn_quals, rqual, raises,
15074 : : 1, friendp,
15075 : : publicp,
15076 : 72499038 : inlinep | (2 * constexpr_p) | (4 * concept_p)
15077 : 72499038 : | (8 * consteval_p),
15078 : : initialized == SD_DELETED,
15079 : : is_xobj_member_function, sfk,
15080 : : funcdef_flag,
15081 : : late_return_type_p,
15082 : : template_count, in_namespace, attrlist,
15083 : : id_loc);
15084 : 72499038 : if (decl == NULL_TREE)
15085 : 569 : return error_mark_node;
15086 : :
15087 : 72498469 : if (explicitp == 2)
15088 : 1958 : DECL_NONCONVERTING_P (decl) = 1;
15089 : 72498469 : if (staticp == 1)
15090 : : {
15091 : 498289 : int invalid_static = 0;
15092 : :
15093 : : /* Don't allow a static member function in a class, and forbid
15094 : : declaring main to be static. */
15095 : 498289 : if (TREE_CODE (type) == METHOD_TYPE)
15096 : : {
15097 : 0 : permerror (input_location, "cannot declare member function %qD to have "
15098 : : "static linkage", decl);
15099 : 0 : invalid_static = 1;
15100 : : }
15101 : 498289 : else if (current_function_decl)
15102 : : {
15103 : : /* 7.1.1: There can be no static function declarations within a
15104 : : block. */
15105 : 11 : error_at (declspecs->locations[ds_storage_class],
15106 : : "cannot declare static function inside another function");
15107 : 11 : invalid_static = 1;
15108 : : }
15109 : :
15110 : 11 : if (invalid_static)
15111 : : {
15112 : 72498469 : staticp = 0;
15113 : : storage_class = sc_none;
15114 : : }
15115 : : }
15116 : 72498469 : if (declspecs->explicit_specifier)
15117 : 1 : store_explicit_specifier (decl, declspecs->explicit_specifier);
15118 : : }
15119 : : else
15120 : : {
15121 : : /* It's a variable. */
15122 : :
15123 : : /* An uninitialized decl with `extern' is a reference. */
15124 : 101983004 : decl = grokvardecl (type, dname, unqualified_id,
15125 : : declspecs,
15126 : : initialized,
15127 : : type_quals,
15128 : : inlinep,
15129 : : concept_p,
15130 : : template_count,
15131 : : ctype ? ctype : in_namespace,
15132 : : id_loc);
15133 : 51170799 : if (decl == NULL_TREE)
15134 : 21 : return error_mark_node;
15135 : :
15136 : 51170778 : bad_specifiers (decl, BSP_VAR, virtualp,
15137 : : memfn_quals != TYPE_UNQUALIFIED,
15138 : : inlinep, friendp, raises != NULL_TREE,
15139 : : declspecs->locations);
15140 : :
15141 : 51170778 : if (ctype)
15142 : : {
15143 : 358592 : DECL_CONTEXT (decl) = ctype;
15144 : 358592 : if (staticp == 1)
15145 : : {
15146 : 8 : permerror (declspecs->locations[ds_storage_class],
15147 : : "%<static%> may not be used when defining "
15148 : : "(as opposed to declaring) a static data member");
15149 : 8 : staticp = 0;
15150 : 8 : storage_class = sc_none;
15151 : : }
15152 : 358592 : if (storage_class == sc_register && TREE_STATIC (decl))
15153 : : {
15154 : 0 : error ("static member %qD declared %<register%>", decl);
15155 : 0 : storage_class = sc_none;
15156 : : }
15157 : 358592 : if (storage_class == sc_extern && pedantic)
15158 : : {
15159 : 0 : pedwarn (input_location, OPT_Wpedantic,
15160 : : "cannot explicitly declare member %q#D to have "
15161 : : "extern linkage", decl);
15162 : 0 : storage_class = sc_none;
15163 : : }
15164 : : }
15165 : 50812186 : else if (constexpr_p && DECL_EXTERNAL (decl))
15166 : : {
15167 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
15168 : : "declaration of %<constexpr%> variable %qD "
15169 : : "is not a definition", decl);
15170 : 12 : constexpr_p = false;
15171 : : }
15172 : 51170778 : if (consteval_p)
15173 : : {
15174 : 1 : error_at (DECL_SOURCE_LOCATION (decl),
15175 : : "a variable cannot be declared %<consteval%>");
15176 : 1 : consteval_p = false;
15177 : : }
15178 : :
15179 : 51170778 : if (inlinep)
15180 : 3483433 : mark_inline_variable (decl, declspecs->locations[ds_inline]);
15181 : 51170778 : if (innermost_code == cdk_decomp)
15182 : : {
15183 : 55786 : gcc_assert (declarator && declarator->kind == cdk_decomp);
15184 : 55786 : DECL_SOURCE_LOCATION (decl) = id_loc;
15185 : 55786 : DECL_ARTIFICIAL (decl) = 1;
15186 : 55786 : fit_decomposition_lang_decl (decl, NULL_TREE);
15187 : : }
15188 : : }
15189 : :
15190 : 486155092 : if (VAR_P (decl) && !initialized)
15191 : 6410231 : if (tree auto_node = type_uses_auto (type))
15192 : 71 : if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15193 : : {
15194 : 18 : location_t loc = declspecs->locations[ds_type_spec];
15195 : 18 : error_at (loc, "declaration of %q#D has no initializer", decl);
15196 : 18 : TREE_TYPE (decl) = error_mark_node;
15197 : : }
15198 : :
15199 : 486155092 : if (storage_class == sc_extern && initialized && !funcdef_flag)
15200 : : {
15201 : 255 : if (toplevel_bindings_p ())
15202 : : {
15203 : : /* It's common practice (and completely valid) to have a const
15204 : : be initialized and declared extern. */
15205 : 247 : if (!(type_quals & TYPE_QUAL_CONST))
15206 : 26 : warning_at (DECL_SOURCE_LOCATION (decl), 0,
15207 : : "%qs initialized and declared %<extern%>", name);
15208 : : }
15209 : : else
15210 : : {
15211 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
15212 : : "%qs has both %<extern%> and initializer", name);
15213 : 8 : return error_mark_node;
15214 : : }
15215 : : }
15216 : :
15217 : : /* Record `register' declaration for warnings on &
15218 : : and in case doing stupid register allocation. */
15219 : :
15220 : 486155084 : if (storage_class == sc_register)
15221 : : {
15222 : 2724 : DECL_REGISTER (decl) = 1;
15223 : : /* Warn about register storage specifiers on PARM_DECLs. */
15224 : 2724 : if (TREE_CODE (decl) == PARM_DECL)
15225 : : {
15226 : 48 : if (cxx_dialect >= cxx17)
15227 : 22 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15228 : : "ISO C++17 does not allow %<register%> storage "
15229 : : "class specifier");
15230 : : else
15231 : 26 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
15232 : : "%<register%> storage class specifier used");
15233 : : }
15234 : : }
15235 : 486152360 : else if (storage_class == sc_extern)
15236 : 28295474 : DECL_THIS_EXTERN (decl) = 1;
15237 : 457856886 : else if (storage_class == sc_static)
15238 : 18091979 : DECL_THIS_STATIC (decl) = 1;
15239 : :
15240 : 486155084 : if (VAR_P (decl))
15241 : : {
15242 : : /* Set constexpr flag on vars (functions got it in grokfndecl). */
15243 : 60312190 : if (constexpr_p)
15244 : 11936140 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
15245 : : /* And the constinit flag (which only applies to variables). */
15246 : 48376050 : else if (constinit_p)
15247 : 157 : DECL_DECLARED_CONSTINIT_P (decl) = true;
15248 : : }
15249 : 425842894 : else if (TREE_CODE (decl) == FUNCTION_DECL)
15250 : : {
15251 : : /* If we saw a return type, record its location. */
15252 : 154386398 : location_t loc = declspecs->locations[ds_type_spec];
15253 : 154386398 : if (loc == UNKNOWN_LOCATION)
15254 : : /* Build DECL_RESULT in start_preparsed_function. */;
15255 : 128426774 : else if (!DECL_RESULT (decl))
15256 : : {
15257 : 128392508 : tree restype = TREE_TYPE (TREE_TYPE (decl));
15258 : 128392508 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
15259 : 128392508 : DECL_ARTIFICIAL (resdecl) = 1;
15260 : 128392508 : DECL_IGNORED_P (resdecl) = 1;
15261 : 128392508 : DECL_RESULT (decl) = resdecl;
15262 : : }
15263 : 34266 : else if (funcdef_flag)
15264 : 4 : DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
15265 : : }
15266 : :
15267 : : /* Record constancy and volatility on the DECL itself . There's
15268 : : no need to do this when processing a template; we'll do this
15269 : : for the instantiated declaration based on the type of DECL. */
15270 : 486155084 : if (!processing_template_decl
15271 : : /* Don't do it for instantiated variable templates either,
15272 : : cp_apply_type_quals_to_decl should have been called on it
15273 : : already and might have been overridden in cp_finish_decl
15274 : : if initializer needs runtime initialization. */
15275 : 486155084 : && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl)))
15276 : 165468363 : cp_apply_type_quals_to_decl (type_quals, decl);
15277 : :
15278 : 486155084 : return decl;
15279 : : }
15280 : 837597761 : }
15281 : :
15282 : : /* Subroutine of start_function. Ensure that each of the parameter
15283 : : types (as listed in PARMS) is complete, as is required for a
15284 : : function definition. */
15285 : :
15286 : : static void
15287 : 128402104 : require_complete_types_for_parms (tree parms)
15288 : : {
15289 : 384655584 : for (; parms; parms = DECL_CHAIN (parms))
15290 : : {
15291 : 256253480 : if (dependent_type_p (TREE_TYPE (parms)))
15292 : 148085787 : continue;
15293 : 108167693 : if (!VOID_TYPE_P (TREE_TYPE (parms))
15294 : 108167693 : && complete_type_or_else (TREE_TYPE (parms), parms))
15295 : : {
15296 : 108167605 : relayout_decl (parms);
15297 : 108167605 : DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
15298 : :
15299 : 108167605 : abstract_virtuals_error (parms, TREE_TYPE (parms));
15300 : 108167605 : maybe_warn_parm_abi (TREE_TYPE (parms),
15301 : 108167605 : DECL_SOURCE_LOCATION (parms));
15302 : : }
15303 : : else
15304 : : /* grokparms or complete_type_or_else will have already issued
15305 : : an error. */
15306 : 88 : TREE_TYPE (parms) = error_mark_node;
15307 : : }
15308 : 128402104 : }
15309 : :
15310 : : /* Returns nonzero if T is a local variable. */
15311 : :
15312 : : int
15313 : 129919433 : local_variable_p (const_tree t)
15314 : : {
15315 : 129919433 : if ((VAR_P (t)
15316 : 59804389 : && (DECL_LOCAL_DECL_P (t)
15317 : 59804389 : || !DECL_CONTEXT (t)
15318 : 59764212 : || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
15319 : 132870839 : || (TREE_CODE (t) == PARM_DECL))
15320 : 115838121 : return 1;
15321 : :
15322 : : return 0;
15323 : : }
15324 : :
15325 : : /* Like local_variable_p, but suitable for use as a tree-walking
15326 : : function. */
15327 : :
15328 : : static tree
15329 : 4624492 : local_variable_p_walkfn (tree *tp, int *walk_subtrees,
15330 : : void * /*data*/)
15331 : : {
15332 : 4624492 : if (unevaluated_p (TREE_CODE (*tp)))
15333 : : {
15334 : : /* DR 2082 permits local variables in unevaluated contexts
15335 : : within a default argument. */
15336 : 16 : *walk_subtrees = 0;
15337 : 16 : return NULL_TREE;
15338 : : }
15339 : :
15340 : 4624476 : if (local_variable_p (*tp)
15341 : 4624476 : && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
15342 : : return *tp;
15343 : 4624476 : else if (TYPE_P (*tp))
15344 : 16 : *walk_subtrees = 0;
15345 : :
15346 : : return NULL_TREE;
15347 : : }
15348 : :
15349 : : /* Check that ARG, which is a default-argument expression for a
15350 : : parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
15351 : : something goes wrong. DECL may also be a _TYPE node, rather than a
15352 : : DECL, if there is no DECL available. */
15353 : :
15354 : : tree
15355 : 2375792 : check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
15356 : : {
15357 : 2375792 : tree var;
15358 : 2375792 : tree decl_type;
15359 : :
15360 : 2375792 : if (TREE_CODE (arg) == DEFERRED_PARSE)
15361 : : /* We get a DEFERRED_PARSE when looking at an in-class declaration
15362 : : with a default argument. Ignore the argument for now; we'll
15363 : : deal with it after the class is complete. */
15364 : : return arg;
15365 : :
15366 : 1708113 : if (TYPE_P (decl))
15367 : : {
15368 : : decl_type = decl;
15369 : : decl = NULL_TREE;
15370 : : }
15371 : : else
15372 : 518256 : decl_type = TREE_TYPE (decl);
15373 : :
15374 : 1708113 : if (arg == error_mark_node
15375 : 1707977 : || decl == error_mark_node
15376 : 1707965 : || TREE_TYPE (arg) == error_mark_node
15377 : 3416078 : || decl_type == error_mark_node)
15378 : : /* Something already went wrong. There's no need to check
15379 : : further. */
15380 : : return error_mark_node;
15381 : :
15382 : : /* [dcl.fct.default]
15383 : :
15384 : : A default argument expression is implicitly converted to the
15385 : : parameter type. */
15386 : 1707965 : ++cp_unevaluated_operand;
15387 : : /* Avoid digest_init clobbering the initializer. */
15388 : 1707965 : tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
15389 : 1707965 : perform_implicit_conversion_flags (decl_type, carg, complain,
15390 : : LOOKUP_IMPLICIT);
15391 : 1707965 : --cp_unevaluated_operand;
15392 : :
15393 : : /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
15394 : : the call sites. */
15395 : 1263195 : if (TYPE_PTR_OR_PTRMEM_P (decl_type)
15396 : 444782 : && null_ptr_cst_p (arg)
15397 : : /* Don't lose side-effects as in PR90473. */
15398 : 2036814 : && !TREE_SIDE_EFFECTS (arg))
15399 : 328846 : return nullptr_node;
15400 : :
15401 : : /* [dcl.fct.default]
15402 : :
15403 : : Local variables shall not be used in default argument
15404 : : expressions.
15405 : :
15406 : : The keyword `this' shall not be used in a default argument of a
15407 : : member function. */
15408 : 1379119 : var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
15409 : 1379119 : if (var)
15410 : : {
15411 : 0 : if (complain & tf_warning_or_error)
15412 : : {
15413 : 0 : if (DECL_NAME (var) == this_identifier)
15414 : 0 : permerror (input_location, "default argument %qE uses %qD",
15415 : : arg, var);
15416 : : else
15417 : 0 : error ("default argument %qE uses local variable %qD", arg, var);
15418 : : }
15419 : 0 : return error_mark_node;
15420 : : }
15421 : :
15422 : : /* All is well. */
15423 : 1379119 : return arg;
15424 : : }
15425 : :
15426 : : /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
15427 : :
15428 : : static tree
15429 : 243288467 : type_is_deprecated (tree type)
15430 : : {
15431 : 359798335 : enum tree_code code;
15432 : 359798335 : if (TREE_DEPRECATED (type))
15433 : : return type;
15434 : 359798250 : if (TYPE_NAME (type))
15435 : : {
15436 : 239353783 : if (TREE_DEPRECATED (TYPE_NAME (type)))
15437 : : return type;
15438 : : else
15439 : : {
15440 : 239353775 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
15441 : 239353775 : return NULL_TREE;
15442 : : }
15443 : : }
15444 : :
15445 : : /* Do warn about using typedefs to a deprecated class. */
15446 : 120444467 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
15447 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
15448 : :
15449 : 120444467 : code = TREE_CODE (type);
15450 : :
15451 : 120444467 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
15452 : 120444467 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
15453 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
15454 : 116406535 : return type_is_deprecated (TREE_TYPE (type));
15455 : :
15456 : 103333 : if (TYPE_PTRMEMFUNC_P (type))
15457 : 103333 : return type_is_deprecated
15458 : 103333 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
15459 : :
15460 : : return NULL_TREE;
15461 : : }
15462 : :
15463 : : /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
15464 : :
15465 : : static tree
15466 : 244089773 : type_is_unavailable (tree type)
15467 : : {
15468 : 361083893 : enum tree_code code;
15469 : 361083893 : if (TREE_UNAVAILABLE (type))
15470 : : return type;
15471 : 361083885 : if (TYPE_NAME (type))
15472 : : {
15473 : 240126066 : if (TREE_UNAVAILABLE (TYPE_NAME (type)))
15474 : : return type;
15475 : : else
15476 : : {
15477 : 240126058 : cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
15478 : 240126058 : return NULL_TREE;
15479 : : }
15480 : : }
15481 : :
15482 : : /* Do warn about using typedefs to a deprecated class. */
15483 : 120957819 : if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
15484 : 0 : return type_is_deprecated (TYPE_MAIN_VARIANT (type));
15485 : :
15486 : 120957819 : code = TREE_CODE (type);
15487 : :
15488 : 120957819 : if (code == POINTER_TYPE || code == REFERENCE_TYPE
15489 : 120957819 : || code == OFFSET_TYPE || code == FUNCTION_TYPE
15490 : : || code == METHOD_TYPE || code == ARRAY_TYPE)
15491 : 116792963 : return type_is_unavailable (TREE_TYPE (type));
15492 : :
15493 : 201157 : if (TYPE_PTRMEMFUNC_P (type))
15494 : 201157 : return type_is_unavailable
15495 : 201157 : (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
15496 : :
15497 : : return NULL_TREE;
15498 : : }
15499 : :
15500 : : /* Decode the list of parameter types for a function type.
15501 : : Given the list of things declared inside the parens,
15502 : : return a list of types.
15503 : :
15504 : : If this parameter does not end with an ellipsis, we append
15505 : : void_list_node.
15506 : :
15507 : : *PARMS is set to the chain of PARM_DECLs created. */
15508 : :
15509 : : tree
15510 : 160702187 : grokparms (tree parmlist, tree *parms)
15511 : : {
15512 : 160702187 : tree result = NULL_TREE;
15513 : 160702187 : tree decls = NULL_TREE;
15514 : 160702187 : tree parm;
15515 : 160702187 : int any_error = 0;
15516 : :
15517 : 405390396 : for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
15518 : : {
15519 : 404072692 : tree type = NULL_TREE;
15520 : 404072692 : tree init = TREE_PURPOSE (parm);
15521 : 404072692 : tree decl = TREE_VALUE (parm);
15522 : :
15523 : 404072692 : if (parm == void_list_node || parm == explicit_void_list_node)
15524 : : break;
15525 : :
15526 : 244688225 : if (! decl || TREE_TYPE (decl) == error_mark_node)
15527 : : {
15528 : 159 : any_error = 1;
15529 : 159 : continue;
15530 : : }
15531 : :
15532 : 244688066 : type = TREE_TYPE (decl);
15533 : 244688066 : if (VOID_TYPE_P (type))
15534 : : {
15535 : 102 : if (same_type_p (type, void_type_node)
15536 : 91 : && !init
15537 : 75 : && !DECL_NAME (decl) && !result
15538 : 134 : && TREE_CHAIN (parm) == void_list_node)
15539 : : /* DR 577: A parameter list consisting of a single
15540 : : unnamed parameter of non-dependent type 'void'. */
15541 : : break;
15542 : 86 : else if (cv_qualified_p (type))
15543 : 11 : error_at (DECL_SOURCE_LOCATION (decl),
15544 : : "invalid use of cv-qualified type %qT in "
15545 : : "parameter declaration", type);
15546 : : else
15547 : 75 : error_at (DECL_SOURCE_LOCATION (decl),
15548 : : "invalid use of type %<void%> in parameter "
15549 : : "declaration");
15550 : : /* It's not a good idea to actually create parameters of
15551 : : type `void'; other parts of the compiler assume that a
15552 : : void type terminates the parameter list. */
15553 : 86 : type = error_mark_node;
15554 : 86 : TREE_TYPE (decl) = error_mark_node;
15555 : : }
15556 : :
15557 : 244688050 : if (type != error_mark_node)
15558 : : {
15559 : 244687964 : if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
15560 : : {
15561 : 244089773 : tree unavailtype = type_is_unavailable (type);
15562 : 244089773 : if (unavailtype)
15563 : 16 : cp_handle_deprecated_or_unavailable (unavailtype);
15564 : : }
15565 : 244687964 : if (deprecated_state != DEPRECATED_SUPPRESS
15566 : 244687964 : && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
15567 : : {
15568 : 243288467 : tree deptype = type_is_deprecated (type);
15569 : 243288467 : if (deptype)
15570 : 93 : cp_handle_deprecated_or_unavailable (deptype);
15571 : : }
15572 : :
15573 : : /* [dcl.fct] "A parameter with volatile-qualified type is
15574 : : deprecated." */
15575 : 244687964 : if (CP_TYPE_VOLATILE_P (type))
15576 : 619 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
15577 : : "%<volatile%>-qualified parameter is "
15578 : : "deprecated");
15579 : :
15580 : : /* Top-level qualifiers on the parameters are
15581 : : ignored for function types. */
15582 : 244687964 : type = cp_build_qualified_type (type, 0);
15583 : 244687964 : if (TREE_CODE (type) == METHOD_TYPE)
15584 : : {
15585 : 0 : error ("parameter %qD invalidly declared method type", decl);
15586 : 0 : type = build_pointer_type (type);
15587 : 0 : TREE_TYPE (decl) = type;
15588 : : }
15589 : 244687964 : else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
15590 : : {
15591 : : /* Before C++17 DR 393:
15592 : : [dcl.fct]/6, parameter types cannot contain pointers
15593 : : (references) to arrays of unknown bound. */
15594 : 2548574 : tree t = TREE_TYPE (type);
15595 : 2548574 : int ptr = TYPE_PTR_P (type);
15596 : :
15597 : 2787492 : while (1)
15598 : : {
15599 : 2668033 : if (TYPE_PTR_P (t))
15600 : : ptr = 1;
15601 : 2553401 : else if (TREE_CODE (t) != ARRAY_TYPE)
15602 : : break;
15603 : 4840 : else if (!TYPE_DOMAIN (t))
15604 : : break;
15605 : 119459 : t = TREE_TYPE (t);
15606 : : }
15607 : 2548574 : if (TREE_CODE (t) == ARRAY_TYPE)
15608 : 20 : pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
15609 : : ptr
15610 : : ? G_("parameter %qD includes pointer to array of "
15611 : : "unknown bound %qT")
15612 : : : G_("parameter %qD includes reference to array of "
15613 : : "unknown bound %qT"),
15614 : : decl, t);
15615 : : }
15616 : :
15617 : 244687964 : if (init && !processing_template_decl)
15618 : 1185923 : init = check_default_argument (decl, init, tf_warning_or_error);
15619 : : }
15620 : :
15621 : 244688050 : DECL_CHAIN (decl) = decls;
15622 : 244688050 : decls = decl;
15623 : 244688050 : result = tree_cons (init, type, result);
15624 : : }
15625 : 160702187 : decls = nreverse (decls);
15626 : 160702187 : result = nreverse (result);
15627 : 160702187 : if (parm)
15628 : 159384483 : result = chainon (result, void_list_node);
15629 : 160702187 : *parms = decls;
15630 : 160702187 : if (any_error)
15631 : 141 : result = NULL_TREE;
15632 : :
15633 : 141 : if (any_error)
15634 : : /* We had parm errors, recover by giving the function (...) type. */
15635 : 141 : result = NULL_TREE;
15636 : :
15637 : 160702187 : return result;
15638 : : }
15639 : :
15640 : :
15641 : : /* D is a constructor or overloaded `operator='.
15642 : :
15643 : : Let T be the class in which D is declared. Then, this function
15644 : : returns:
15645 : :
15646 : : -1 if D's is an ill-formed constructor or copy assignment operator
15647 : : whose first parameter is of type `T'.
15648 : : 0 if D is not a copy constructor or copy assignment
15649 : : operator.
15650 : : 1 if D is a copy constructor or copy assignment operator whose
15651 : : first parameter is a reference to non-const qualified T.
15652 : : 2 if D is a copy constructor or copy assignment operator whose
15653 : : first parameter is a reference to const qualified T.
15654 : :
15655 : : This function can be used as a predicate. Positive values indicate
15656 : : a copy constructor and nonzero values indicate a copy assignment
15657 : : operator. */
15658 : :
15659 : : int
15660 : 263756714 : copy_fn_p (const_tree d)
15661 : : {
15662 : 263756714 : tree args;
15663 : 263756714 : tree arg_type;
15664 : 263756714 : int result = 1;
15665 : :
15666 : 263756714 : gcc_assert (DECL_FUNCTION_MEMBER_P (d));
15667 : :
15668 : 263756714 : if (TREE_CODE (d) == TEMPLATE_DECL
15669 : 263756714 : || (DECL_TEMPLATE_INFO (d)
15670 : 124754134 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15671 : : /* Instantiations of template member functions are never copy
15672 : : functions. Note that member functions of templated classes are
15673 : : represented as template functions internally, and we must
15674 : : accept those as copy functions. */
15675 : : return 0;
15676 : :
15677 : 220077803 : if (!DECL_CONSTRUCTOR_P (d)
15678 : 220077803 : && DECL_NAME (d) != assign_op_identifier)
15679 : : return 0;
15680 : :
15681 : 218635633 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
15682 : : {
15683 : 130 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
15684 : 130 : if (!TYPE_REF_P (object_param)
15685 : 124 : || TYPE_REF_IS_RVALUE (object_param)
15686 : : /* Reject unrelated object parameters. */
15687 : 112 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
15688 : 242 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
15689 : 24 : return 0;
15690 : 106 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
15691 : : }
15692 : : else
15693 : 218635503 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
15694 : 218635609 : if (!args)
15695 : : return 0;
15696 : :
15697 : 218628420 : arg_type = TREE_VALUE (args);
15698 : 218628420 : if (arg_type == error_mark_node)
15699 : : return 0;
15700 : :
15701 : 218628374 : if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
15702 : : {
15703 : : /* Pass by value copy assignment operator. */
15704 : : result = -1;
15705 : : }
15706 : 218626781 : else if (TYPE_REF_P (arg_type)
15707 : 135103820 : && !TYPE_REF_IS_RVALUE (arg_type)
15708 : 305137454 : && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
15709 : : {
15710 : 67873550 : if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
15711 : 67816572 : result = 2;
15712 : : }
15713 : : else
15714 : : return 0;
15715 : :
15716 : 67875143 : args = TREE_CHAIN (args);
15717 : :
15718 : 70297994 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15719 : : /* There are more non-optional args. */
15720 : : return 0;
15721 : :
15722 : : return result;
15723 : : }
15724 : :
15725 : : /* D is a constructor or overloaded `operator='.
15726 : :
15727 : : Let T be the class in which D is declared. Then, this function
15728 : : returns true when D is a move constructor or move assignment
15729 : : operator, false otherwise. */
15730 : :
15731 : : bool
15732 : 104413410 : move_fn_p (const_tree d)
15733 : : {
15734 : 104413410 : if (cxx_dialect == cxx98)
15735 : : /* There are no move constructors if we are in C++98 mode. */
15736 : : return false;
15737 : :
15738 : 104350002 : if (TREE_CODE (d) == TEMPLATE_DECL
15739 : 104350002 : || (DECL_TEMPLATE_INFO (d)
15740 : 44278455 : && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15741 : : /* Instantiations of template member functions are never move
15742 : : functions. Note that member functions of templated classes are
15743 : : represented as template functions internally, and we must
15744 : : accept those as move functions. */
15745 : : return 0;
15746 : :
15747 : 83972333 : return move_signature_fn_p (d);
15748 : : }
15749 : :
15750 : : /* D is a constructor or overloaded `operator='.
15751 : :
15752 : : Then, this function returns true when D has the same signature as a move
15753 : : constructor or move assignment operator (because either it is such a
15754 : : ctor/op= or it is a template specialization with the same signature),
15755 : : false otherwise. */
15756 : :
15757 : : bool
15758 : 83972333 : move_signature_fn_p (const_tree d)
15759 : : {
15760 : 83972333 : tree args;
15761 : 83972333 : tree arg_type;
15762 : 83972333 : bool result = false;
15763 : :
15764 : 83972333 : if (!DECL_CONSTRUCTOR_P (d)
15765 : 83972333 : && DECL_NAME (d) != assign_op_identifier)
15766 : : return 0;
15767 : :
15768 : 83972279 : if (DECL_XOBJ_MEMBER_FUNCTION_P (d))
15769 : : {
15770 : 84 : tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d)));
15771 : 84 : if (!TYPE_REF_P (object_param)
15772 : 80 : || TYPE_REF_IS_RVALUE (object_param)
15773 : : /* Reject unrelated object parameters. */
15774 : 72 : || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d)
15775 : 156 : || CP_TYPE_CONST_P (TREE_TYPE (object_param)))
15776 : 16 : return 0;
15777 : 68 : args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d)));
15778 : : }
15779 : : else
15780 : 83972195 : args = FUNCTION_FIRST_USER_PARMTYPE (d);
15781 : 83972263 : if (!args)
15782 : : return 0;
15783 : :
15784 : 83969686 : arg_type = TREE_VALUE (args);
15785 : 83969686 : if (arg_type == error_mark_node)
15786 : : return 0;
15787 : :
15788 : 83969674 : if (TYPE_REF_P (arg_type)
15789 : 41886024 : && TYPE_REF_IS_RVALUE (arg_type)
15790 : 116788922 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15791 : : DECL_CONTEXT (d)))
15792 : : result = true;
15793 : :
15794 : 83969674 : args = TREE_CHAIN (args);
15795 : :
15796 : 92992247 : if (args && args != void_list_node && !TREE_PURPOSE (args))
15797 : : /* There are more non-optional args. */
15798 : : return false;
15799 : :
15800 : : return result;
15801 : : }
15802 : :
15803 : : /* Remember any special properties of member function DECL. */
15804 : :
15805 : : void
15806 : 267191688 : grok_special_member_properties (tree decl)
15807 : : {
15808 : 267191688 : tree class_type;
15809 : :
15810 : 267191688 : if (TREE_CODE (decl) == USING_DECL
15811 : 267191688 : || !DECL_OBJECT_MEMBER_FUNCTION_P (decl))
15812 : : return;
15813 : :
15814 : 250484991 : class_type = DECL_CONTEXT (decl);
15815 : 250484991 : if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15816 : : {
15817 : 110453460 : int ctor = copy_fn_p (decl);
15818 : :
15819 : 110453460 : if (!DECL_ARTIFICIAL (decl))
15820 : 82739349 : TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15821 : :
15822 : 110453460 : if (ctor > 0)
15823 : : {
15824 : : /* [class.copy]
15825 : :
15826 : : A non-template constructor for class X is a copy
15827 : : constructor if its first parameter is of type X&, const
15828 : : X&, volatile X& or const volatile X&, and either there
15829 : : are no other parameters or else all other parameters have
15830 : : default arguments. */
15831 : 21720372 : TYPE_HAS_COPY_CTOR (class_type) = 1;
15832 : 21720372 : if (ctor > 1)
15833 : 21707040 : TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15834 : : }
15835 : :
15836 : 110453460 : if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15837 : 23256608 : TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15838 : :
15839 : 110453460 : if (is_list_ctor (decl))
15840 : 1308814 : TYPE_HAS_LIST_CTOR (class_type) = 1;
15841 : :
15842 : 110453460 : if (maybe_constexpr_fn (decl)
15843 : 110453460 : && !ctor && !move_fn_p (decl))
15844 : 25379737 : TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15845 : : }
15846 : 140031531 : else if (DECL_NAME (decl) == assign_op_identifier)
15847 : : {
15848 : : /* [class.copy]
15849 : :
15850 : : A non-template assignment operator for class X is a copy
15851 : : assignment operator if its parameter is of type X, X&, const
15852 : : X&, volatile X& or const volatile X&. */
15853 : :
15854 : 12184161 : int assop = copy_fn_p (decl);
15855 : :
15856 : 12184161 : if (assop)
15857 : : {
15858 : 6073091 : TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15859 : 6073091 : if (assop != 1)
15860 : 6044969 : TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15861 : : }
15862 : : }
15863 : 234630779 : else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15864 : 1655666 : TYPE_HAS_CONVERSION (class_type) = true;
15865 : :
15866 : : /* Destructors are handled in check_methods. */
15867 : : }
15868 : :
15869 : : /* Check a constructor DECL has the correct form. Complains
15870 : : if the class has a constructor of the form X(X). */
15871 : :
15872 : : bool
15873 : 38432915 : grok_ctor_properties (const_tree ctype, const_tree decl)
15874 : : {
15875 : 38432915 : int ctor_parm = copy_fn_p (decl);
15876 : :
15877 : 38432915 : if (ctor_parm < 0)
15878 : : {
15879 : : /* [class.copy]
15880 : :
15881 : : A declaration of a constructor for a class X is ill-formed if
15882 : : its first parameter is of type (optionally cv-qualified) X
15883 : : and either there are no other parameters or else all other
15884 : : parameters have default arguments.
15885 : :
15886 : : We *don't* complain about member template instantiations that
15887 : : have this form, though; they can occur as we try to decide
15888 : : what constructor to use during overload resolution. Since
15889 : : overload resolution will never prefer such a constructor to
15890 : : the non-template copy constructor (which is either explicitly
15891 : : or implicitly defined), there's no need to worry about their
15892 : : existence. Theoretically, they should never even be
15893 : : instantiated, but that's hard to forestall. */
15894 : 12 : error_at (DECL_SOURCE_LOCATION (decl),
15895 : : "invalid constructor; you probably meant %<%T (const %T&)%>",
15896 : : ctype, ctype);
15897 : 12 : return false;
15898 : : }
15899 : :
15900 : : return true;
15901 : : }
15902 : :
15903 : : /* DECL is a declaration for an overloaded or conversion operator. If
15904 : : COMPLAIN is true, errors are issued for invalid declarations. */
15905 : :
15906 : : bool
15907 : 45846704 : grok_op_properties (tree decl, bool complain)
15908 : : {
15909 : 45846704 : tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15910 : 45846704 : bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl);
15911 : 45846704 : tree name = DECL_NAME (decl);
15912 : 45846704 : location_t loc = DECL_SOURCE_LOCATION (decl);
15913 : :
15914 : 45846704 : tree class_type = DECL_CONTEXT (decl);
15915 : 45846704 : if (class_type && !CLASS_TYPE_P (class_type))
15916 : : class_type = NULL_TREE;
15917 : :
15918 : 45846704 : tree_code operator_code;
15919 : 45846704 : unsigned op_flags;
15920 : 45846704 : if (IDENTIFIER_CONV_OP_P (name))
15921 : : {
15922 : : /* Conversion operators are TYPE_EXPR for the purposes of this
15923 : : function. */
15924 : : operator_code = TYPE_EXPR;
15925 : : op_flags = OVL_OP_FLAG_UNARY;
15926 : : }
15927 : : else
15928 : : {
15929 : 44491435 : const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15930 : :
15931 : 44491435 : operator_code = ovl_op->tree_code;
15932 : 44491435 : op_flags = ovl_op->flags;
15933 : 44491435 : gcc_checking_assert (operator_code != ERROR_MARK);
15934 : 44491435 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15935 : : }
15936 : :
15937 : 45846704 : if (op_flags & OVL_OP_FLAG_ALLOC)
15938 : : {
15939 : : /* operator new and operator delete are quite special. */
15940 : 306345 : if (class_type)
15941 : 10552 : switch (op_flags)
15942 : : {
15943 : 7337 : case OVL_OP_FLAG_ALLOC:
15944 : 7337 : TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15945 : 7337 : break;
15946 : :
15947 : 2895 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
15948 : 2895 : TYPE_GETS_DELETE (class_type) |= 1;
15949 : 2895 : break;
15950 : :
15951 : 162 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
15952 : 162 : TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
15953 : 162 : break;
15954 : :
15955 : 158 : case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
15956 : 158 : TYPE_GETS_DELETE (class_type) |= 2;
15957 : 158 : break;
15958 : :
15959 : 0 : default:
15960 : 0 : gcc_unreachable ();
15961 : : }
15962 : :
15963 : : /* [basic.std.dynamic.allocation]/1:
15964 : :
15965 : : A program is ill-formed if an allocation function is declared
15966 : : in a namespace scope other than global scope or declared
15967 : : static in global scope.
15968 : :
15969 : : The same also holds true for deallocation functions. */
15970 : 306345 : if (DECL_NAMESPACE_SCOPE_P (decl))
15971 : : {
15972 : 295793 : if (CP_DECL_CONTEXT (decl) != global_namespace)
15973 : : {
15974 : 8 : error_at (loc, "%qD may not be declared within a namespace",
15975 : : decl);
15976 : 8 : return false;
15977 : : }
15978 : :
15979 : 295785 : if (!TREE_PUBLIC (decl))
15980 : : {
15981 : 8 : error_at (loc, "%qD may not be declared as static", decl);
15982 : 8 : return false;
15983 : : }
15984 : : }
15985 : :
15986 : 306329 : if (op_flags & OVL_OP_FLAG_DELETE)
15987 : : {
15988 : 174196 : DECL_SET_IS_OPERATOR_DELETE (decl, true);
15989 : 174196 : coerce_delete_type (decl, loc);
15990 : : }
15991 : : else
15992 : : {
15993 : 132133 : DECL_SET_IS_OPERATOR_NEW (decl, true);
15994 : 132133 : TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15995 : : }
15996 : :
15997 : 306329 : return true;
15998 : : }
15999 : :
16000 : : /* An operator function must either be a non-static member function
16001 : : or have at least one parameter of a class, a reference to a class,
16002 : : an enumeration, or a reference to an enumeration. 13.4.0.6 */
16003 : 45540359 : if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl))
16004 : : {
16005 : 12754940 : if (operator_code == TYPE_EXPR
16006 : 12754940 : || operator_code == COMPONENT_REF
16007 : 12754912 : || operator_code == NOP_EXPR)
16008 : : {
16009 : 40 : error_at (loc, "%qD must be a non-static member function", decl);
16010 : 40 : return false;
16011 : : }
16012 : :
16013 : 12754900 : if (operator_code == CALL_EXPR || operator_code == ARRAY_REF)
16014 : : {
16015 : 107 : if (! DECL_STATIC_FUNCTION_P (decl))
16016 : : {
16017 : 16 : error_at (loc, "%qD must be a member function", decl);
16018 : 16 : return false;
16019 : : }
16020 : 91 : if (cxx_dialect < cxx23
16021 : : /* For lambdas we diagnose static lambda specifier elsewhere. */
16022 : 99 : && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl))
16023 : : /* For instantiations, we have diagnosed this already. */
16024 : 126 : && ! DECL_USE_TEMPLATE (decl))
16025 : 29 : pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member "
16026 : : "function only with %<-std=c++23%> or %<-std=gnu++23%>",
16027 : : decl);
16028 : 91 : if (operator_code == ARRAY_REF)
16029 : : /* static operator[] should have exactly one argument
16030 : : for C++20 and earlier, so that it isn't multidimensional. */
16031 : : op_flags = OVL_OP_FLAG_UNARY;
16032 : : }
16033 : 12754793 : else if (DECL_STATIC_FUNCTION_P (decl))
16034 : : {
16035 : 9 : error_at (loc, "%qD must be either a non-static member "
16036 : : "function or a non-member function", decl);
16037 : 9 : return false;
16038 : : }
16039 : : else
16040 : 403734 : for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
16041 : : {
16042 : 13158518 : if (!arg || arg == void_list_node)
16043 : : {
16044 : 24 : if (complain)
16045 : 20 : error_at (loc, "%qD must have an argument of class or "
16046 : : "enumerated type", decl);
16047 : 24 : return false;
16048 : : }
16049 : :
16050 : 13158494 : tree type = non_reference (TREE_VALUE (arg));
16051 : 13158494 : if (type == error_mark_node)
16052 : : return false;
16053 : :
16054 : : /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
16055 : : because these checks are performed even on template
16056 : : functions. */
16057 : 13158490 : if (MAYBE_CLASS_TYPE_P (type)
16058 : 1289677 : || TREE_CODE (type) == ENUMERAL_TYPE)
16059 : : break;
16060 : 403734 : }
16061 : : }
16062 : :
16063 : 45540229 : if (operator_code == CALL_EXPR)
16064 : : /* There are no further restrictions on the arguments to an overloaded
16065 : : "operator ()". */
16066 : : return true;
16067 : :
16068 : : /* C++23 allows an arbitrary number of parameters and default arguments for
16069 : : operator[], and none of the other checks below apply. */
16070 : 40170872 : if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
16071 : : return true;
16072 : :
16073 : 39947128 : if (operator_code == COND_EXPR)
16074 : : {
16075 : : /* 13.4.0.3 */
16076 : 8 : error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
16077 : 8 : return false;
16078 : : }
16079 : :
16080 : : /* Count the number of arguments and check for ellipsis. */
16081 : : int arity = 0;
16082 : 114486312 : for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
16083 : : {
16084 : 74539212 : if (!arg)
16085 : : {
16086 : 20 : error_at (loc, "%qD must not have variable number of arguments",
16087 : : decl);
16088 : 20 : return false;
16089 : : }
16090 : 74539192 : ++arity;
16091 : : }
16092 : : /* FIXME: We need tests for these errors with xobj member functions. */
16093 : : /* Verify correct number of arguments. */
16094 : 39947100 : switch (op_flags)
16095 : : {
16096 : 6910954 : case OVL_OP_FLAG_AMBIARY:
16097 : 6910954 : if (arity == 1)
16098 : : {
16099 : : /* We have a unary instance of an ambi-ary op. Remap to the
16100 : : unary one. */
16101 : 3136630 : unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
16102 : 3136630 : const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
16103 : 3136630 : gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
16104 : 3136630 : operator_code = ovl_op->tree_code;
16105 : 3136630 : DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
16106 : : }
16107 : 3774324 : else if (arity != 2)
16108 : : {
16109 : : /* This was an ambiguous operator but is invalid. */
16110 : 4 : error_at (loc,
16111 : : methodp
16112 : : ? G_("%qD must have either zero or one argument")
16113 : : : G_("%qD must have either one or two arguments"), decl);
16114 : 4 : return false;
16115 : : }
16116 : 3774320 : else if ((operator_code == POSTINCREMENT_EXPR
16117 : 3774320 : || operator_code == POSTDECREMENT_EXPR)
16118 : 1914665 : && ! processing_template_decl
16119 : : /* x++ and x--'s second argument must be an int. */
16120 : 5110165 : && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
16121 : : integer_type_node))
16122 : : {
16123 : 20 : error_at (loc,
16124 : : methodp
16125 : : ? G_("postfix %qD must have %<int%> as its argument")
16126 : : : G_("postfix %qD must have %<int%> as its second argument"),
16127 : : decl);
16128 : 20 : return false;
16129 : : }
16130 : : break;
16131 : :
16132 : 2218414 : case OVL_OP_FLAG_UNARY:
16133 : 2218414 : if (arity != 1)
16134 : : {
16135 : 14 : error_at (loc,
16136 : : methodp
16137 : : ? G_("%qD must have no arguments")
16138 : : : G_("%qD must have exactly one argument"), decl);
16139 : 14 : return false;
16140 : : }
16141 : : break;
16142 : :
16143 : 30817732 : case OVL_OP_FLAG_BINARY:
16144 : 30817732 : if (arity != 2)
16145 : : {
16146 : 1 : error_at (loc,
16147 : : methodp
16148 : : ? G_("%qD must have exactly one argument")
16149 : : : G_("%qD must have exactly two arguments"), decl);
16150 : 1 : return false;
16151 : : }
16152 : : break;
16153 : :
16154 : 0 : default:
16155 : 0 : gcc_unreachable ();
16156 : : }
16157 : :
16158 : : /* There can be no default arguments. */
16159 : 114486133 : for (tree arg = argtypes; arg && arg != void_list_node;
16160 : 74539072 : arg = TREE_CHAIN (arg))
16161 : 74539092 : if (TREE_PURPOSE (arg))
16162 : : {
16163 : 20 : TREE_PURPOSE (arg) = NULL_TREE;
16164 : 20 : error_at (loc, "%qD cannot have default arguments", decl);
16165 : 20 : return false;
16166 : : }
16167 : :
16168 : : /* At this point the declaration is well-formed. It may not be
16169 : : sensible though. */
16170 : :
16171 : : /* Check member function warnings only on the in-class declaration.
16172 : : There's no point warning on an out-of-class definition. */
16173 : 39947041 : if (class_type && class_type != current_class_type)
16174 : : return true;
16175 : :
16176 : : /* Warn about conversion operators that will never be used. */
16177 : 38107403 : if (IDENTIFIER_CONV_OP_P (name)
16178 : 1354790 : && ! DECL_TEMPLATE_INFO (decl)
16179 : 38626089 : && warn_class_conversion)
16180 : : {
16181 : 518642 : tree t = TREE_TYPE (name);
16182 : 518642 : int ref = TYPE_REF_P (t);
16183 : :
16184 : 518642 : if (ref)
16185 : 21904 : t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
16186 : :
16187 : 518642 : if (VOID_TYPE_P (t))
16188 : 12 : warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
16189 : : "will never use a type conversion operator", class_type);
16190 : 518630 : else if (class_type)
16191 : : {
16192 : 518630 : if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
16193 : 32 : warning_at (loc, OPT_Wclass_conversion,
16194 : : ref
16195 : : ? G_("converting %qT to a reference to the same type "
16196 : : "will never use a type conversion operator")
16197 : : : G_("converting %qT to the same type "
16198 : : "will never use a type conversion operator"),
16199 : : class_type);
16200 : : /* Don't force t to be complete here. */
16201 : 518610 : else if (MAYBE_CLASS_TYPE_P (t)
16202 : 223340 : && COMPLETE_TYPE_P (t)
16203 : 48395 : && DERIVED_FROM_P (t, class_type))
16204 : 40 : warning_at (loc, OPT_Wclass_conversion,
16205 : : ref
16206 : : ? G_("converting %qT to a reference to a base class "
16207 : : "%qT will never use a type conversion operator")
16208 : : : G_("converting %qT to a base class %qT "
16209 : : "will never use a type conversion operator"),
16210 : : class_type, t);
16211 : : }
16212 : : }
16213 : :
16214 : 38107403 : if (!warn_ecpp)
16215 : : return true;
16216 : :
16217 : : /* Effective C++ rules below. */
16218 : :
16219 : : /* More Effective C++ rule 7. */
16220 : 72 : if (operator_code == TRUTH_ANDIF_EXPR
16221 : 72 : || operator_code == TRUTH_ORIF_EXPR
16222 : 72 : || operator_code == COMPOUND_EXPR)
16223 : 0 : warning_at (loc, OPT_Weffc__,
16224 : : "user-defined %qD always evaluates both arguments", decl);
16225 : :
16226 : : /* More Effective C++ rule 6. */
16227 : 72 : if (operator_code == POSTINCREMENT_EXPR
16228 : : || operator_code == POSTDECREMENT_EXPR
16229 : : || operator_code == PREINCREMENT_EXPR
16230 : 72 : || operator_code == PREDECREMENT_EXPR)
16231 : : {
16232 : 0 : tree arg = TREE_VALUE (argtypes);
16233 : 0 : tree ret = TREE_TYPE (TREE_TYPE (decl));
16234 : 0 : if (methodp || TYPE_REF_P (arg))
16235 : 0 : arg = TREE_TYPE (arg);
16236 : 0 : arg = TYPE_MAIN_VARIANT (arg);
16237 : :
16238 : 0 : if (operator_code == PREINCREMENT_EXPR
16239 : : || operator_code == PREDECREMENT_EXPR)
16240 : : {
16241 : 0 : if (!TYPE_REF_P (ret)
16242 : 0 : || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
16243 : 0 : warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
16244 : : build_reference_type (arg));
16245 : : }
16246 : : else
16247 : : {
16248 : 0 : if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
16249 : 0 : warning_at (loc, OPT_Weffc__,
16250 : : "postfix %qD should return %qT", decl, arg);
16251 : : }
16252 : : }
16253 : :
16254 : : /* Effective C++ rule 23. */
16255 : 72 : if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
16256 : : && (operator_code == PLUS_EXPR
16257 : 12 : || operator_code == MINUS_EXPR
16258 : 12 : || operator_code == TRUNC_DIV_EXPR
16259 : : || operator_code == MULT_EXPR
16260 : : || operator_code == TRUNC_MOD_EXPR)
16261 : 80 : && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
16262 : 4 : warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
16263 : :
16264 : : return true;
16265 : : }
16266 : :
16267 : : /* Return a string giving the keyword associate with CODE. */
16268 : :
16269 : : static const char *
16270 : 139 : tag_name (enum tag_types code)
16271 : : {
16272 : 139 : switch (code)
16273 : : {
16274 : : case record_type:
16275 : : return "struct";
16276 : 75 : case class_type:
16277 : 75 : return "class";
16278 : 0 : case union_type:
16279 : 0 : return "union";
16280 : 4 : case enum_type:
16281 : 4 : return "enum";
16282 : 0 : case typename_type:
16283 : 0 : return "typename";
16284 : 0 : default:
16285 : 0 : gcc_unreachable ();
16286 : : }
16287 : : }
16288 : :
16289 : : /* Name lookup in an elaborated-type-specifier (after the keyword
16290 : : indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
16291 : : elaborated-type-specifier is invalid, issue a diagnostic and return
16292 : : error_mark_node; otherwise, return the *_TYPE to which it referred.
16293 : : If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
16294 : :
16295 : : tree
16296 : 10391810 : check_elaborated_type_specifier (enum tag_types tag_code,
16297 : : tree decl,
16298 : : bool allow_template_p)
16299 : : {
16300 : 10391810 : tree type;
16301 : :
16302 : : /* In the case of:
16303 : :
16304 : : struct S { struct S *p; };
16305 : :
16306 : : name lookup will find the TYPE_DECL for the implicit "S::S"
16307 : : typedef. Adjust for that here. */
16308 : 10391810 : if (DECL_SELF_REFERENCE_P (decl))
16309 : 283464 : decl = TYPE_NAME (TREE_TYPE (decl));
16310 : :
16311 : 10391810 : type = TREE_TYPE (decl);
16312 : :
16313 : : /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
16314 : : is false for this case as well. */
16315 : 10391810 : if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16316 : : {
16317 : 12 : error ("using template type parameter %qT after %qs",
16318 : : type, tag_name (tag_code));
16319 : 12 : return error_mark_node;
16320 : : }
16321 : : /* Accept template template parameters. */
16322 : 10391798 : else if (allow_template_p
16323 : 5876385 : && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
16324 : 5876370 : || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
16325 : : ;
16326 : : /* [dcl.type.elab]
16327 : :
16328 : : If the identifier resolves to a typedef-name or the
16329 : : simple-template-id resolves to an alias template
16330 : : specialization, the elaborated-type-specifier is ill-formed.
16331 : :
16332 : : In other words, the only legitimate declaration to use in the
16333 : : elaborated type specifier is the implicit typedef created when
16334 : : the type is declared. */
16335 : 10391780 : else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
16336 : 283596 : && !DECL_SELF_REFERENCE_P (decl)
16337 : 10391912 : && tag_code != typename_type)
16338 : : {
16339 : 95 : if (alias_template_specialization_p (type, nt_opaque))
16340 : 21 : error ("using alias template specialization %qT after %qs",
16341 : : type, tag_name (tag_code));
16342 : : else
16343 : 74 : error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
16344 : 95 : inform (DECL_SOURCE_LOCATION (decl),
16345 : : "%qD has a previous declaration here", decl);
16346 : 95 : return error_mark_node;
16347 : : }
16348 : 10391685 : else if (TREE_CODE (type) != RECORD_TYPE
16349 : 10391685 : && TREE_CODE (type) != UNION_TYPE
16350 : : && tag_code != enum_type
16351 : 60690 : && tag_code != typename_type)
16352 : : {
16353 : 8 : error ("%qT referred to as %qs", type, tag_name (tag_code));
16354 : 8 : inform (location_of (type), "%qT has a previous declaration here", type);
16355 : 8 : return error_mark_node;
16356 : : }
16357 : 10391677 : else if (TREE_CODE (type) != ENUMERAL_TYPE
16358 : 10331029 : && tag_code == enum_type)
16359 : : {
16360 : 4 : error ("%qT referred to as enum", type);
16361 : 4 : inform (location_of (type), "%qT has a previous declaration here", type);
16362 : 4 : return error_mark_node;
16363 : : }
16364 : 10391673 : else if (!allow_template_p
16365 : 4515335 : && TREE_CODE (type) == RECORD_TYPE
16366 : 14794774 : && CLASSTYPE_IS_TEMPLATE (type))
16367 : : {
16368 : : /* If a class template appears as elaborated type specifier
16369 : : without a template header such as:
16370 : :
16371 : : template <class T> class C {};
16372 : : void f(class C); // No template header here
16373 : :
16374 : : then the required template argument is missing. */
16375 : 24 : error ("template argument required for %<%s %T%>",
16376 : : tag_name (tag_code),
16377 : 24 : DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
16378 : 24 : return error_mark_node;
16379 : : }
16380 : :
16381 : : return type;
16382 : : }
16383 : :
16384 : : /* Lookup NAME of an elaborated type specifier according to SCOPE and
16385 : : issue diagnostics if necessary. Return *_TYPE node upon success,
16386 : : NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
16387 : : error. */
16388 : :
16389 : : static tree
16390 : 20376924 : lookup_and_check_tag (enum tag_types tag_code, tree name,
16391 : : TAG_how how, bool template_header_p)
16392 : : {
16393 : 20376924 : tree decl;
16394 : 20376924 : if (how == TAG_how::GLOBAL)
16395 : : {
16396 : : /* First try ordinary name lookup, ignoring hidden class name
16397 : : injected via friend declaration. */
16398 : 1785097 : decl = lookup_name (name, LOOK_want::TYPE);
16399 : 1785097 : decl = strip_using_decl (decl);
16400 : : /* If that fails, the name will be placed in the smallest
16401 : : non-class, non-function-prototype scope according to 3.3.1/5.
16402 : : We may already have a hidden name declared as friend in this
16403 : : scope. So lookup again but not ignoring hidden names.
16404 : : If we find one, that name will be made visible rather than
16405 : : creating a new tag. */
16406 : 1785097 : if (!decl)
16407 : 17916 : decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
16408 : : }
16409 : : else
16410 : 18591827 : decl = lookup_elaborated_type (name, how);
16411 : :
16412 : 18609743 : if (!decl)
16413 : : /* We found nothing. */
16414 : : return NULL_TREE;
16415 : :
16416 : 4446762 : if (TREE_CODE (decl) == TREE_LIST)
16417 : : {
16418 : 4 : error ("reference to %qD is ambiguous", name);
16419 : 4 : print_candidates (decl);
16420 : 4 : return error_mark_node;
16421 : : }
16422 : :
16423 : 1784715 : if (DECL_CLASS_TEMPLATE_P (decl)
16424 : 1784708 : && !template_header_p
16425 : 6231466 : && how == TAG_how::CURRENT_ONLY)
16426 : : {
16427 : 16 : error ("class template %qD redeclared as non-template", name);
16428 : 16 : inform (location_of (decl), "previous declaration here");
16429 : 16 : CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
16430 : 16 : return error_mark_node;
16431 : : }
16432 : :
16433 : 1784699 : if (DECL_CLASS_TEMPLATE_P (decl)
16434 : : /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
16435 : : so ignore a template template parameter. */
16436 : 4446749 : || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
16437 : 1784695 : decl = DECL_TEMPLATE_RESULT (decl);
16438 : :
16439 : 4446742 : if (TREE_CODE (decl) != TYPE_DECL)
16440 : : /* Found not-a-type. */
16441 : : return NULL_TREE;
16442 : :
16443 : : /* Look for invalid nested type:
16444 : : class C {
16445 : : class C {};
16446 : : }; */
16447 : 4446698 : if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
16448 : : {
16449 : 8 : error ("%qD has the same name as the class in which it is "
16450 : : "declared", decl);
16451 : 8 : return error_mark_node;
16452 : : }
16453 : :
16454 : : /* Two cases we need to consider when deciding if a class
16455 : : template is allowed as an elaborated type specifier:
16456 : : 1. It is a self reference to its own class.
16457 : : 2. It comes with a template header.
16458 : :
16459 : : For example:
16460 : :
16461 : : template <class T> class C {
16462 : : class C *c1; // DECL_SELF_REFERENCE_P is true
16463 : : class D;
16464 : : };
16465 : : template <class U> class C; // template_header_p is true
16466 : : template <class T> class C<T>::D {
16467 : : class C *c2; // DECL_SELF_REFERENCE_P is true
16468 : : }; */
16469 : :
16470 : 13340070 : tree t = check_elaborated_type_specifier (tag_code, decl,
16471 : : template_header_p
16472 : 8609936 : | DECL_SELF_REFERENCE_P (decl));
16473 : 1882809 : if (template_header_p && t && CLASS_TYPE_P (t)
16474 : 6329478 : && (!CLASSTYPE_TEMPLATE_INFO (t)
16475 : 1882764 : || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
16476 : : {
16477 : 28 : error ("%qT is not a template", t);
16478 : 28 : inform (location_of (t), "previous declaration here");
16479 : 56 : if (TYPE_CLASS_SCOPE_P (t)
16480 : 32 : && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
16481 : 4 : inform (input_location,
16482 : : "perhaps you want to explicitly add %<%T::%>",
16483 : 4 : TYPE_CONTEXT (t));
16484 : 28 : return error_mark_node;
16485 : : }
16486 : :
16487 : : return t;
16488 : : }
16489 : :
16490 : : /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
16491 : : Define the tag as a forward-reference if it is not defined.
16492 : :
16493 : : If a declaration is given, process it here, and report an error if
16494 : : multiple declarations are not identical.
16495 : :
16496 : : SCOPE is TS_CURRENT when this is also a definition. Only look in
16497 : : the current frame for the name (since C++ allows new names in any
16498 : : scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
16499 : : declaration. Only look beginning from the current scope outward up
16500 : : till the nearest non-class scope. Otherwise it is TS_GLOBAL.
16501 : :
16502 : : TEMPLATE_HEADER_P is true when this declaration is preceded by
16503 : : a set of template parameters. */
16504 : :
16505 : : tree
16506 : 19763098 : xref_tag (enum tag_types tag_code, tree name,
16507 : : TAG_how how, bool template_header_p)
16508 : : {
16509 : 19763098 : enum tree_code code;
16510 : 19763098 : tree context = NULL_TREE;
16511 : :
16512 : 19763098 : auto_cond_timevar tv (TV_NAME_LOOKUP);
16513 : :
16514 : 19763098 : gcc_assert (identifier_p (name));
16515 : :
16516 : 19763098 : switch (tag_code)
16517 : : {
16518 : : case record_type:
16519 : : case class_type:
16520 : : code = RECORD_TYPE;
16521 : : break;
16522 : : case union_type:
16523 : : code = UNION_TYPE;
16524 : : break;
16525 : : case enum_type:
16526 : : code = ENUMERAL_TYPE;
16527 : : break;
16528 : 0 : default:
16529 : 0 : gcc_unreachable ();
16530 : : }
16531 : :
16532 : : /* In case of anonymous name, xref_tag is only called to
16533 : : make type node and push name. Name lookup is not required. */
16534 : 19763098 : tree t = NULL_TREE;
16535 : 19763098 : if (!IDENTIFIER_ANON_P (name))
16536 : 18449912 : t = lookup_and_check_tag (tag_code, name, how, template_header_p);
16537 : :
16538 : 19763098 : if (t == error_mark_node)
16539 : : return error_mark_node;
16540 : :
16541 : 2292561 : if (how != TAG_how::CURRENT_ONLY && t && current_class_type
16542 : 958843 : && template_class_depth (current_class_type)
16543 : 20119950 : && template_header_p)
16544 : : {
16545 : 243921 : if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
16546 : : return t;
16547 : :
16548 : : /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
16549 : : a definition of this tag. Since, in addition, we are
16550 : : currently processing a (member) template declaration of a
16551 : : template class, we must be very careful; consider:
16552 : :
16553 : : template <class X> struct S1
16554 : :
16555 : : template <class U> struct S2
16556 : : {
16557 : : template <class V> friend struct S1;
16558 : : };
16559 : :
16560 : : Here, the S2::S1 declaration should not be confused with the
16561 : : outer declaration. In particular, the inner version should
16562 : : have a template parameter of level 2, not level 1.
16563 : :
16564 : : On the other hand, when presented with:
16565 : :
16566 : : template <class T> struct S1
16567 : : {
16568 : : template <class U> struct S2 {};
16569 : : template <class U> friend struct S2;
16570 : : };
16571 : :
16572 : : the friend must find S1::S2 eventually. We accomplish this
16573 : : by making sure that the new type we create to represent this
16574 : : declaration has the right TYPE_CONTEXT. */
16575 : 243918 : context = TYPE_CONTEXT (t);
16576 : 243918 : t = NULL_TREE;
16577 : : }
16578 : :
16579 : 19762948 : if (! t)
16580 : : {
16581 : : /* If no such tag is yet defined, create a forward-reference node
16582 : : and record it as the "definition".
16583 : : When a real declaration of this type is found,
16584 : : the forward-reference will be altered into a real type. */
16585 : 15592578 : if (code == ENUMERAL_TYPE)
16586 : : {
16587 : 17 : error ("use of enum %q#D without previous declaration", name);
16588 : 17 : return error_mark_node;
16589 : : }
16590 : :
16591 : 15592561 : t = make_class_type (code);
16592 : 15592561 : TYPE_CONTEXT (t) = context;
16593 : 15592561 : if (IDENTIFIER_LAMBDA_P (name))
16594 : : /* Mark it as a lambda type right now. Our caller will
16595 : : correct the value. */
16596 : 667138 : CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
16597 : 15592561 : t = pushtag (name, t, how);
16598 : : }
16599 : : else
16600 : : {
16601 : 4170370 : if (template_header_p && MAYBE_CLASS_TYPE_P (t))
16602 : : {
16603 : : /* Check that we aren't trying to overload a class with different
16604 : : constraints. */
16605 : 1638842 : if (!redeclare_class_template (t, current_template_parms,
16606 : : current_template_constraints ()))
16607 : 60 : return error_mark_node;
16608 : : }
16609 : 2531528 : else if (!processing_template_decl
16610 : 2398554 : && CLASS_TYPE_P (t)
16611 : 4903903 : && CLASSTYPE_IS_TEMPLATE (t))
16612 : : {
16613 : 0 : error ("redeclaration of %qT as a non-template", t);
16614 : 0 : inform (location_of (t), "previous declaration %qD", t);
16615 : 0 : return error_mark_node;
16616 : : }
16617 : :
16618 : 4170310 : if (modules_p ()
16619 : 4170310 : && how == TAG_how::CURRENT_ONLY)
16620 : : {
16621 : 12530 : tree decl = TYPE_NAME (t);
16622 : 12530 : if (!module_may_redeclare (decl))
16623 : : {
16624 : 6 : auto_diagnostic_group d;
16625 : 6 : error ("cannot declare %qD in a different module", decl);
16626 : 6 : inform (DECL_SOURCE_LOCATION (decl), "previously declared here");
16627 : 6 : return error_mark_node;
16628 : 6 : }
16629 : :
16630 : 12524 : tree not_tmpl = STRIP_TEMPLATE (decl);
16631 : 12524 : if (DECL_LANG_SPECIFIC (not_tmpl)
16632 : 6088 : && DECL_MODULE_ATTACH_P (not_tmpl)
16633 : 27 : && !DECL_MODULE_EXPORT_P (not_tmpl)
16634 : 12551 : && module_exporting_p ())
16635 : : {
16636 : 6 : auto_diagnostic_group d;
16637 : 6 : error ("conflicting exporting for declaration %qD", decl);
16638 : 6 : inform (DECL_SOURCE_LOCATION (decl),
16639 : : "previously declared here without exporting");
16640 : 6 : }
16641 : :
16642 : 12524 : tree maybe_tmpl = decl;
16643 : 12524 : if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
16644 : 9177 : maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
16645 : :
16646 : 12524 : if (DECL_LANG_SPECIFIC (decl)
16647 : 6088 : && DECL_MODULE_IMPORT_P (decl)
16648 : 12563 : && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16649 : : {
16650 : : /* Push it into this TU's symbol slot. */
16651 : 39 : gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
16652 : 39 : if (maybe_tmpl != decl)
16653 : : /* We're in the template parm binding level.
16654 : : Pushtag has logic to slide under that, but we're
16655 : : not pushing a *new* type. */
16656 : 15 : push_nested_namespace (CP_DECL_CONTEXT (decl));
16657 : :
16658 : 39 : pushdecl (maybe_tmpl);
16659 : 39 : if (maybe_tmpl != decl)
16660 : 15 : pop_nested_namespace (CP_DECL_CONTEXT (decl));
16661 : : }
16662 : :
16663 : 12524 : set_instantiating_module (maybe_tmpl);
16664 : : }
16665 : : }
16666 : :
16667 : : return t;
16668 : 19763098 : }
16669 : :
16670 : : /* Create the binfo hierarchy for REF with (possibly NULL) base list
16671 : : BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
16672 : : access_* node, and the TREE_VALUE is the type of the base-class.
16673 : : Non-NULL TREE_TYPE indicates virtual inheritance. */
16674 : :
16675 : : void
16676 : 51976998 : xref_basetypes (tree ref, tree base_list)
16677 : : {
16678 : 51976998 : tree *basep;
16679 : 51976998 : tree binfo, base_binfo;
16680 : 51976998 : unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
16681 : 51976998 : unsigned max_bases = 0; /* Maximum direct bases. */
16682 : 51976998 : unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
16683 : 51976998 : int i;
16684 : 51976998 : tree default_access;
16685 : 51976998 : tree igo_prev; /* Track Inheritance Graph Order. */
16686 : :
16687 : 51976998 : if (ref == error_mark_node)
16688 : 16 : return;
16689 : :
16690 : : /* The base of a derived class is private by default, all others are
16691 : : public. */
16692 : 103953996 : default_access = (TREE_CODE (ref) == RECORD_TYPE
16693 : 51468962 : && CLASSTYPE_DECLARED_CLASS (ref)
16694 : 103445960 : ? access_private_node : access_public_node);
16695 : :
16696 : : /* First, make sure that any templates in base-classes are
16697 : : instantiated. This ensures that if we call ourselves recursively
16698 : : we do not get confused about which classes are marked and which
16699 : : are not. */
16700 : 51976998 : basep = &base_list;
16701 : 79236655 : while (*basep)
16702 : : {
16703 : 27259657 : tree basetype = TREE_VALUE (*basep);
16704 : :
16705 : : /* The dependent_type_p call below should really be dependent_scope_p
16706 : : so that we give a hard error about using an incomplete type as a
16707 : : base, but we allow it with a pedwarn for backward
16708 : : compatibility. */
16709 : 27259657 : if (processing_template_decl
16710 : 27259657 : && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
16711 : 4 : cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
16712 : 27259657 : if (!dependent_type_p (basetype)
16713 : 27259657 : && !complete_type_or_else (basetype, NULL))
16714 : : /* An incomplete type. Remove it from the list. */
16715 : 16 : *basep = TREE_CHAIN (*basep);
16716 : : else
16717 : : {
16718 : 27259641 : max_bases++;
16719 : 27259641 : if (TREE_TYPE (*basep))
16720 : 67454 : max_dvbases++;
16721 : 27259641 : if (CLASS_TYPE_P (basetype))
16722 : 26393349 : max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16723 : 27259641 : basep = &TREE_CHAIN (*basep);
16724 : : }
16725 : : }
16726 : 51976998 : max_vbases += max_dvbases;
16727 : :
16728 : 51976998 : TYPE_MARKED_P (ref) = 1;
16729 : :
16730 : : /* The binfo slot should be empty, unless this is an (ill-formed)
16731 : : redefinition. */
16732 : 51976998 : gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
16733 : :
16734 : 51976998 : gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
16735 : :
16736 : 51976998 : binfo = make_tree_binfo (max_bases);
16737 : :
16738 : 51976998 : TYPE_BINFO (ref) = binfo;
16739 : 51976998 : BINFO_OFFSET (binfo) = size_zero_node;
16740 : 51976998 : BINFO_TYPE (binfo) = ref;
16741 : :
16742 : : /* Apply base-class info set up to the variants of this type. */
16743 : 51976998 : fixup_type_variants (ref);
16744 : :
16745 : 51976998 : if (max_bases)
16746 : : {
16747 : 26297342 : vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
16748 : : /* A C++98 POD cannot have base classes. */
16749 : 26297342 : CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
16750 : :
16751 : 26297342 : if (TREE_CODE (ref) == UNION_TYPE)
16752 : : {
16753 : 16 : error ("derived union %qT invalid", ref);
16754 : 16 : return;
16755 : : }
16756 : : }
16757 : :
16758 : 26297326 : if (max_bases > 1)
16759 : 681375 : warning (OPT_Wmultiple_inheritance,
16760 : : "%qT defined with multiple direct bases", ref);
16761 : :
16762 : 51976982 : if (max_vbases)
16763 : : {
16764 : : /* An aggregate can't have virtual base classes. */
16765 : 201814 : CLASSTYPE_NON_AGGREGATE (ref) = true;
16766 : :
16767 : 201814 : vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16768 : :
16769 : 201814 : if (max_dvbases)
16770 : 66290 : warning (OPT_Wvirtual_inheritance,
16771 : : "%qT defined with direct virtual base", ref);
16772 : : }
16773 : :
16774 : 79236607 : for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16775 : : {
16776 : 27259625 : tree access = TREE_PURPOSE (base_list);
16777 : 27259625 : int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16778 : 27259625 : tree basetype = TREE_VALUE (base_list);
16779 : :
16780 : 27259625 : if (access == access_default_node)
16781 : 4164970 : access = default_access;
16782 : :
16783 : : /* Before C++17, an aggregate cannot have base classes. In C++17, an
16784 : : aggregate can't have virtual, private, or protected base classes. */
16785 : 27259625 : if (cxx_dialect < cxx17
16786 : 26921186 : || access != access_public_node
16787 : 25875364 : || via_virtual)
16788 : 1447400 : CLASSTYPE_NON_AGGREGATE (ref) = true;
16789 : :
16790 : 27259625 : if (PACK_EXPANSION_P (basetype))
16791 : 18504 : basetype = PACK_EXPANSION_PATTERN (basetype);
16792 : 27259625 : if (TREE_CODE (basetype) == TYPE_DECL)
16793 : 0 : basetype = TREE_TYPE (basetype);
16794 : 27259625 : if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16795 : : {
16796 : 30 : error ("base type %qT fails to be a struct or class type",
16797 : : basetype);
16798 : 30 : goto dropped_base;
16799 : : }
16800 : :
16801 : 27259595 : base_binfo = NULL_TREE;
16802 : 27259595 : if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16803 : : {
16804 : 21058433 : base_binfo = TYPE_BINFO (basetype);
16805 : : /* The original basetype could have been a typedef'd type. */
16806 : 21058433 : basetype = BINFO_TYPE (base_binfo);
16807 : :
16808 : : /* Inherit flags from the base. */
16809 : 63175299 : TYPE_HAS_NEW_OPERATOR (ref)
16810 : 21058433 : |= TYPE_HAS_NEW_OPERATOR (basetype);
16811 : 63175299 : TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16812 : 21058433 : |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16813 : 21058433 : TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16814 : 21058433 : TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16815 : 63175299 : CLASSTYPE_DIAMOND_SHAPED_P (ref)
16816 : 21058433 : |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16817 : 63175299 : CLASSTYPE_REPEATED_BASE_P (ref)
16818 : 21058433 : |= CLASSTYPE_REPEATED_BASE_P (basetype);
16819 : : }
16820 : :
16821 : : /* We must do this test after we've seen through a typedef
16822 : : type. */
16823 : 27259595 : if (TYPE_MARKED_P (basetype))
16824 : : {
16825 : 24 : if (basetype == ref)
16826 : 0 : error ("recursive type %qT undefined", basetype);
16827 : : else
16828 : 24 : error ("duplicate base type %qT invalid", basetype);
16829 : 24 : goto dropped_base;
16830 : : }
16831 : :
16832 : 27259571 : if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16833 : : /* Regenerate the pack expansion for the bases. */
16834 : 18504 : basetype = make_pack_expansion (basetype);
16835 : :
16836 : 27259571 : TYPE_MARKED_P (basetype) = 1;
16837 : :
16838 : 27259571 : base_binfo = copy_binfo (base_binfo, basetype, ref,
16839 : : &igo_prev, via_virtual);
16840 : 27259571 : if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16841 : 27192136 : BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16842 : :
16843 : 27259571 : BINFO_BASE_APPEND (binfo, base_binfo);
16844 : 27259571 : BINFO_BASE_ACCESS_APPEND (binfo, access);
16845 : 27259571 : continue;
16846 : :
16847 : 54 : dropped_base:
16848 : : /* Update max_vbases to reflect the reality that we are dropping
16849 : : this base: if it reaches zero we want to undo the vec_alloc
16850 : : above to avoid inconsistencies during error-recovery: eg, in
16851 : : build_special_member_call, CLASSTYPE_VBASECLASSES non null
16852 : : and vtt null (c++/27952). */
16853 : 54 : if (via_virtual)
16854 : 15 : max_vbases--;
16855 : 54 : if (CLASS_TYPE_P (basetype))
16856 : 40 : max_vbases
16857 : 44 : -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16858 : 27259571 : }
16859 : :
16860 : 51976982 : if (CLASSTYPE_VBASECLASSES (ref)
16861 : 51976982 : && max_vbases == 0)
16862 : 30 : vec_free (CLASSTYPE_VBASECLASSES (ref));
16863 : :
16864 : 52178781 : if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16865 : : /* If we didn't get max_vbases vbases, we must have shared at
16866 : : least one of them, and are therefore diamond shaped. */
16867 : 21754 : CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16868 : :
16869 : : /* Unmark all the types. */
16870 : 79236553 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16871 : 27259571 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16872 : 51976982 : TYPE_MARKED_P (ref) = 0;
16873 : :
16874 : : /* Now see if we have a repeated base type. */
16875 : 51976982 : if (!CLASSTYPE_REPEATED_BASE_P (ref))
16876 : : {
16877 : 135320129 : for (base_binfo = binfo; base_binfo;
16878 : 83343648 : base_binfo = TREE_CHAIN (base_binfo))
16879 : : {
16880 : 83344442 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16881 : : {
16882 : 794 : CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16883 : 794 : break;
16884 : : }
16885 : 83343648 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16886 : : }
16887 : 135320129 : for (base_binfo = binfo; base_binfo;
16888 : 83343648 : base_binfo = TREE_CHAIN (base_binfo))
16889 : 83344442 : if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16890 : 83343648 : TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16891 : : else
16892 : : break;
16893 : : }
16894 : : }
16895 : :
16896 : :
16897 : : /* Copies the enum-related properties from type SRC to type DST.
16898 : : Used with the underlying type of an enum and the enum itself. */
16899 : : static void
16900 : 1622064 : copy_type_enum (tree dst, tree src)
16901 : : {
16902 : 1622064 : tree t;
16903 : 3244135 : for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16904 : : {
16905 : 1622071 : TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16906 : 1622071 : TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16907 : 1622071 : TYPE_SIZE (t) = TYPE_SIZE (src);
16908 : 1622071 : TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16909 : 1622071 : SET_TYPE_MODE (dst, TYPE_MODE (src));
16910 : 1622071 : TYPE_PRECISION (t) = TYPE_PRECISION (src);
16911 : 1622071 : unsigned valign = TYPE_ALIGN (src);
16912 : 1622071 : if (TYPE_USER_ALIGN (t))
16913 : 4 : valign = MAX (valign, TYPE_ALIGN (t));
16914 : : else
16915 : 1622067 : TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16916 : 1622071 : SET_TYPE_ALIGN (t, valign);
16917 : 1622071 : TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16918 : : }
16919 : 1622064 : }
16920 : :
16921 : : /* Begin compiling the definition of an enumeration type.
16922 : : NAME is its name,
16923 : :
16924 : : if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16925 : :
16926 : : UNDERLYING_TYPE is the type that will be used as the storage for
16927 : : the enumeration type. This should be NULL_TREE if no storage type
16928 : : was specified.
16929 : :
16930 : : ATTRIBUTES are any attributes specified after the enum-key.
16931 : :
16932 : : SCOPED_ENUM_P is true if this is a scoped enumeration type.
16933 : :
16934 : : if IS_NEW is not NULL, gets TRUE iff a new type is created.
16935 : :
16936 : : Returns the type object, as yet incomplete.
16937 : : Also records info about it so that build_enumerator
16938 : : may be used to declare the individual values as they are read. */
16939 : :
16940 : : tree
16941 : 1927143 : start_enum (tree name, tree enumtype, tree underlying_type,
16942 : : tree attributes, bool scoped_enum_p, bool *is_new)
16943 : : {
16944 : 1927143 : tree prevtype = NULL_TREE;
16945 : 1927143 : gcc_assert (identifier_p (name));
16946 : :
16947 : 1927143 : if (is_new)
16948 : 1551874 : *is_new = false;
16949 : : /* [C++0x dcl.enum]p5:
16950 : :
16951 : : If not explicitly specified, the underlying type of a scoped
16952 : : enumeration type is int. */
16953 : 1927143 : if (!underlying_type && scoped_enum_p)
16954 : 106804 : underlying_type = integer_type_node;
16955 : :
16956 : 1927143 : if (underlying_type)
16957 : 379085 : underlying_type = cv_unqualified (underlying_type);
16958 : :
16959 : : /* If this is the real definition for a previous forward reference,
16960 : : fill in the contents in the same object that used to be the
16961 : : forward reference. */
16962 : 1927143 : if (!enumtype)
16963 : 1927012 : enumtype = lookup_and_check_tag (enum_type, name,
16964 : : /*tag_scope=*/TAG_how::CURRENT_ONLY,
16965 : : /*template_header_p=*/false);
16966 : :
16967 : : /* In case of a template_decl, the only check that should be deferred
16968 : : to instantiation time is the comparison of underlying types. */
16969 : 1927143 : if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
16970 : : {
16971 : : /* Attempt to set the declaring module. */
16972 : 32407 : if (modules_p ())
16973 : : {
16974 : 228 : tree decl = TYPE_NAME (enumtype);
16975 : 228 : if (!module_may_redeclare (decl))
16976 : : {
16977 : 3 : auto_diagnostic_group d;
16978 : 3 : error ("cannot declare %qD in different module", decl);
16979 : 3 : inform (DECL_SOURCE_LOCATION (decl), "previously declared here");
16980 : 3 : enumtype = error_mark_node;
16981 : 3 : }
16982 : : else
16983 : 225 : set_instantiating_module (decl);
16984 : : }
16985 : :
16986 : 32407 : if (enumtype == error_mark_node)
16987 : : ;
16988 : 32583 : else if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
16989 : : {
16990 : 6 : auto_diagnostic_group d;
16991 : 6 : error_at (input_location, "scoped/unscoped mismatch "
16992 : : "in enum %q#T", enumtype);
16993 : 6 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16994 : : "previous definition here");
16995 : 6 : enumtype = error_mark_node;
16996 : 6 : }
16997 : 32398 : else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
16998 : : {
16999 : 3 : auto_diagnostic_group d;
17000 : 3 : error_at (input_location, "underlying type mismatch "
17001 : : "in enum %q#T", enumtype);
17002 : 3 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17003 : : "previous definition here");
17004 : 3 : enumtype = error_mark_node;
17005 : 3 : }
17006 : 32390 : else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
17007 : 64785 : && !same_type_p (underlying_type,
17008 : : ENUM_UNDERLYING_TYPE (enumtype)))
17009 : : {
17010 : 27 : auto_diagnostic_group d;
17011 : 27 : error_at (input_location, "different underlying type "
17012 : : "in enum %q#T", enumtype);
17013 : 27 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17014 : : "previous definition here");
17015 : 27 : underlying_type = NULL_TREE;
17016 : 27 : }
17017 : : }
17018 : :
17019 : 1927143 : if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
17020 : 32395 : || processing_template_decl)
17021 : : {
17022 : : /* In case of error, make a dummy enum to allow parsing to
17023 : : continue. */
17024 : 1894835 : if (enumtype == error_mark_node)
17025 : : {
17026 : 12 : name = make_anon_name ();
17027 : 12 : enumtype = NULL_TREE;
17028 : : }
17029 : :
17030 : : /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
17031 : : of an opaque enum, or an opaque enum of an already defined
17032 : : enumeration (C++11).
17033 : : In any other case, it'll be NULL_TREE. */
17034 : 1894835 : if (!enumtype)
17035 : : {
17036 : 1894744 : if (is_new)
17037 : 1519475 : *is_new = true;
17038 : : }
17039 : 1894835 : prevtype = enumtype;
17040 : :
17041 : : /* Do not push the decl more than once. */
17042 : 1894835 : if (!enumtype
17043 : 91 : || TREE_CODE (enumtype) != ENUMERAL_TYPE)
17044 : : {
17045 : 1894748 : enumtype = cxx_make_type (ENUMERAL_TYPE);
17046 : 1894748 : enumtype = pushtag (name, enumtype);
17047 : :
17048 : : /* std::byte aliases anything. */
17049 : 1894748 : if (enumtype != error_mark_node
17050 : 1894725 : && TYPE_CONTEXT (enumtype) == std_node
17051 : 2491026 : && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
17052 : 11907 : TYPE_ALIAS_SET (enumtype) = 0;
17053 : : }
17054 : : else
17055 : 87 : enumtype = xref_tag (enum_type, name);
17056 : :
17057 : 1894835 : if (enumtype == error_mark_node)
17058 : : return error_mark_node;
17059 : :
17060 : : /* The enum is considered opaque until the opening '{' of the
17061 : : enumerator list. */
17062 : 1894812 : SET_OPAQUE_ENUM_P (enumtype, true);
17063 : 1894812 : ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
17064 : : }
17065 : :
17066 : 1927120 : SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
17067 : :
17068 : 1927120 : cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
17069 : :
17070 : 1927120 : if (underlying_type)
17071 : : {
17072 : 379055 : if (ENUM_UNDERLYING_TYPE (enumtype))
17073 : : /* We already checked that it matches, don't change it to a different
17074 : : typedef variant. */;
17075 : 346692 : else if (CP_INTEGRAL_TYPE_P (underlying_type))
17076 : : {
17077 : 346629 : copy_type_enum (enumtype, underlying_type);
17078 : 346629 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17079 : : }
17080 : 63 : else if (dependent_type_p (underlying_type))
17081 : 51 : ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
17082 : : else
17083 : : {
17084 : 12 : error ("underlying type %qT of %qT must be an integral type",
17085 : : underlying_type, enumtype);
17086 : 12 : ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node;
17087 : : }
17088 : : }
17089 : :
17090 : : /* If into a template class, the returned enum is always the first
17091 : : declaration (opaque or not) seen. This way all the references to
17092 : : this type will be to the same declaration. The following ones are used
17093 : : only to check for definition errors. */
17094 : 1927120 : if (prevtype && processing_template_decl)
17095 : : return prevtype;
17096 : : else
17097 : 1927033 : return enumtype;
17098 : : }
17099 : :
17100 : : /* After processing and defining all the values of an enumeration type,
17101 : : install their decls in the enumeration type.
17102 : : ENUMTYPE is the type object. */
17103 : :
17104 : : void
17105 : 1836090 : finish_enum_value_list (tree enumtype)
17106 : : {
17107 : 1836090 : tree values;
17108 : 1836090 : tree underlying_type;
17109 : 1836090 : tree decl;
17110 : 1836090 : tree value;
17111 : 1836090 : tree minnode, maxnode;
17112 : 1836090 : tree t;
17113 : :
17114 : 1836090 : bool fixed_underlying_type_p
17115 : 1836090 : = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
17116 : :
17117 : : /* We built up the VALUES in reverse order. */
17118 : 1836090 : TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
17119 : :
17120 : : /* For an enum defined in a template, just set the type of the values;
17121 : : all further processing is postponed until the template is
17122 : : instantiated. We need to set the type so that tsubst of a CONST_DECL
17123 : : works. */
17124 : 1836090 : if (processing_template_decl)
17125 : : {
17126 : 338589 : for (values = TYPE_VALUES (enumtype);
17127 : 629449 : values;
17128 : 338589 : values = TREE_CHAIN (values))
17129 : 338589 : TREE_TYPE (TREE_VALUE (values)) = enumtype;
17130 : : return;
17131 : : }
17132 : :
17133 : : /* Determine the minimum and maximum values of the enumerators. */
17134 : 1545230 : if (TYPE_VALUES (enumtype))
17135 : : {
17136 : : minnode = maxnode = NULL_TREE;
17137 : :
17138 : 9850015 : for (values = TYPE_VALUES (enumtype);
17139 : 11369337 : values;
17140 : 9850015 : values = TREE_CHAIN (values))
17141 : : {
17142 : 9850015 : decl = TREE_VALUE (values);
17143 : :
17144 : : /* [dcl.enum]: Following the closing brace of an enum-specifier,
17145 : : each enumerator has the type of its enumeration. Prior to the
17146 : : closing brace, the type of each enumerator is the type of its
17147 : : initializing value. */
17148 : 9850015 : TREE_TYPE (decl) = enumtype;
17149 : :
17150 : : /* Update the minimum and maximum values, if appropriate. */
17151 : 9850015 : value = DECL_INITIAL (decl);
17152 : 9850015 : if (TREE_CODE (value) != INTEGER_CST)
17153 : 6 : value = integer_zero_node;
17154 : : /* Figure out what the minimum and maximum values of the
17155 : : enumerators are. */
17156 : 9850015 : if (!minnode)
17157 : : minnode = maxnode = value;
17158 : 8330693 : else if (tree_int_cst_lt (maxnode, value))
17159 : : maxnode = value;
17160 : 1276155 : else if (tree_int_cst_lt (value, minnode))
17161 : 133830 : minnode = value;
17162 : : }
17163 : : }
17164 : : else
17165 : : /* [dcl.enum]
17166 : :
17167 : : If the enumerator-list is empty, the underlying type is as if
17168 : : the enumeration had a single enumerator with value 0. */
17169 : 25908 : minnode = maxnode = integer_zero_node;
17170 : :
17171 : 1545230 : if (!fixed_underlying_type_p)
17172 : : {
17173 : : /* Compute the number of bits require to represent all values of the
17174 : : enumeration. We must do this before the type of MINNODE and
17175 : : MAXNODE are transformed, since tree_int_cst_min_precision relies
17176 : : on the TREE_TYPE of the value it is passed. */
17177 : 1275435 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
17178 : 1275435 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
17179 : 1275435 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
17180 : 1275435 : int precision = MAX (lowprec, highprec);
17181 : 1275435 : unsigned int itk;
17182 : 1275435 : bool use_short_enum;
17183 : :
17184 : : /* Determine the underlying type of the enumeration.
17185 : :
17186 : : [dcl.enum]
17187 : :
17188 : : The underlying type of an enumeration is an integral type that
17189 : : can represent all the enumerator values defined in the
17190 : : enumeration. It is implementation-defined which integral type is
17191 : : used as the underlying type for an enumeration except that the
17192 : : underlying type shall not be larger than int unless the value of
17193 : : an enumerator cannot fit in an int or unsigned int.
17194 : :
17195 : : We use "int" or an "unsigned int" as the underlying type, even if
17196 : : a smaller integral type would work, unless the user has
17197 : : explicitly requested that we use the smallest possible type. The
17198 : : user can request that for all enumerations with a command line
17199 : : flag, or for just one enumeration with an attribute. */
17200 : :
17201 : 2550870 : use_short_enum = flag_short_enums
17202 : 1275435 : || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
17203 : :
17204 : : /* If the precision of the type was specified with an attribute and it
17205 : : was too small, give an error. Otherwise, use it. */
17206 : 1275435 : if (TYPE_PRECISION (enumtype))
17207 : : {
17208 : 12 : if (precision > TYPE_PRECISION (enumtype))
17209 : 0 : error ("specified mode too small for enumerated values");
17210 : : else
17211 : : {
17212 : 12 : use_short_enum = true;
17213 : 12 : precision = TYPE_PRECISION (enumtype);
17214 : : }
17215 : : }
17216 : :
17217 : 3754184 : for (itk = (use_short_enum ? itk_char : itk_int);
17218 : 2478843 : itk != itk_none;
17219 : : itk++)
17220 : : {
17221 : 2478839 : underlying_type = integer_types[itk];
17222 : 2478839 : if (underlying_type != NULL_TREE
17223 : 2478815 : && TYPE_PRECISION (underlying_type) >= precision
17224 : 4955719 : && TYPE_SIGN (underlying_type) == sgn)
17225 : : break;
17226 : : }
17227 : 1275435 : if (itk == itk_none)
17228 : : {
17229 : : /* DR 377
17230 : :
17231 : : IF no integral type can represent all the enumerator values, the
17232 : : enumeration is ill-formed. */
17233 : 4 : error ("no integral type can represent all of the enumerator values "
17234 : : "for %qT", enumtype);
17235 : 4 : precision = TYPE_PRECISION (long_long_integer_type_node);
17236 : 4 : underlying_type = integer_types[itk_unsigned_long_long];
17237 : : }
17238 : :
17239 : : /* [dcl.enum]
17240 : :
17241 : : The value of sizeof() applied to an enumeration type, an object
17242 : : of an enumeration type, or an enumerator, is the value of sizeof()
17243 : : applied to the underlying type. */
17244 : 1275435 : copy_type_enum (enumtype, underlying_type);
17245 : :
17246 : : /* Compute the minimum and maximum values for the type.
17247 : :
17248 : : [dcl.enum]
17249 : :
17250 : : For an enumeration where emin is the smallest enumerator and emax
17251 : : is the largest, the values of the enumeration are the values of the
17252 : : underlying type in the range bmin to bmax, where bmin and bmax are,
17253 : : respectively, the smallest and largest values of the smallest bit-
17254 : : field that can store emin and emax. */
17255 : :
17256 : : /* The middle-end currently assumes that types with TYPE_PRECISION
17257 : : narrower than their underlying type are suitably zero or sign
17258 : : extended to fill their mode. Similarly, it assumes that the front
17259 : : end assures that a value of a particular type must be within
17260 : : TYPE_MIN_VALUE and TYPE_MAX_VALUE.
17261 : :
17262 : : We used to set these fields based on bmin and bmax, but that led
17263 : : to invalid assumptions like optimizing away bounds checking. So
17264 : : now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
17265 : : TYPE_MAX_VALUE to the values for the mode above and only restrict
17266 : : the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
17267 : 1275435 : ENUM_UNDERLYING_TYPE (enumtype)
17268 : 1275435 : = build_distinct_type_copy (underlying_type);
17269 : 1275435 : TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
17270 : 1275435 : set_min_and_max_values_for_integral_type
17271 : 1275435 : (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
17272 : :
17273 : : /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
17274 : 1275435 : if (flag_strict_enums)
17275 : 39 : set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
17276 : :
17277 : 1275435 : if (use_short_enum)
17278 : : {
17279 : 94 : TYPE_PACKED (enumtype) = use_short_enum;
17280 : 94 : fixup_attribute_variants (enumtype);
17281 : : }
17282 : : }
17283 : : else
17284 : 269795 : underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
17285 : :
17286 : : /* If the enum is exported, mark the consts too. */
17287 : 1545230 : bool export_p = (UNSCOPED_ENUM_P (enumtype)
17288 : 1333337 : && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
17289 : 1546280 : && at_namespace_scope_p ());
17290 : :
17291 : : /* Convert each of the enumerators to the type of the underlying
17292 : : type of the enumeration. */
17293 : 11395245 : for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
17294 : : {
17295 : 9850015 : decl = TREE_VALUE (values);
17296 : 9850015 : iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
17297 : 9850015 : if (fixed_underlying_type_p)
17298 : : /* If the enumeration type has a fixed underlying type, we
17299 : : already checked all of the enumerator values. */
17300 : 2610856 : value = DECL_INITIAL (decl);
17301 : : else
17302 : 7239159 : value = perform_implicit_conversion (underlying_type,
17303 : 7239159 : DECL_INITIAL (decl),
17304 : : tf_warning_or_error);
17305 : : /* Do not clobber shared ints. */
17306 : 9850015 : if (value != error_mark_node)
17307 : : {
17308 : 9850012 : value = copy_node (value);
17309 : :
17310 : 9850012 : TREE_TYPE (value) = enumtype;
17311 : : }
17312 : 9850015 : DECL_INITIAL (decl) = value;
17313 : 9850015 : if (export_p)
17314 : 9926 : DECL_MODULE_EXPORT_P (decl) = true;
17315 : 9850015 : }
17316 : :
17317 : : /* Fix up all variant types of this enum type. */
17318 : 3090467 : for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
17319 : 1545237 : TYPE_VALUES (t) = TYPE_VALUES (enumtype);
17320 : :
17321 : 1545230 : if (at_class_scope_p ()
17322 : 847176 : && COMPLETE_TYPE_P (current_class_type)
17323 : 1545290 : && UNSCOPED_ENUM_P (enumtype))
17324 : : {
17325 : 42 : insert_late_enum_def_bindings (current_class_type, enumtype);
17326 : : /* TYPE_FIELDS needs fixup. */
17327 : 42 : fixup_type_variants (current_class_type);
17328 : : }
17329 : :
17330 : : /* Finish debugging output for this type. */
17331 : 1545230 : rest_of_type_compilation (enumtype, namespace_bindings_p ());
17332 : :
17333 : : /* Each enumerator now has the type of its enumeration. Clear the cache
17334 : : so that this change in types doesn't confuse us later on. */
17335 : 1545230 : clear_cv_and_fold_caches ();
17336 : : }
17337 : :
17338 : : /* Finishes the enum type. This is called only the first time an
17339 : : enumeration is seen, be it opaque or odinary.
17340 : : ENUMTYPE is the type object. */
17341 : :
17342 : : void
17343 : 1826567 : finish_enum (tree enumtype)
17344 : : {
17345 : 1826567 : if (processing_template_decl)
17346 : : {
17347 : 290887 : if (at_function_scope_p ())
17348 : 2104 : add_stmt (build_min (TAG_DEFN, enumtype));
17349 : 290887 : return;
17350 : : }
17351 : :
17352 : : /* If this is a forward declaration, there should not be any variants,
17353 : : though we can get a variant in the middle of an enum-specifier with
17354 : : wacky code like 'enum E { e = sizeof(const E*) };' */
17355 : 3071360 : gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
17356 : : && (TYPE_VALUES (enumtype)
17357 : : || !TYPE_NEXT_VARIANT (enumtype)));
17358 : : }
17359 : :
17360 : : /* Build and install a CONST_DECL for an enumeration constant of the
17361 : : enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
17362 : : Apply ATTRIBUTES if available. LOC is the location of NAME.
17363 : : Assignment of sequential values by default is handled here. */
17364 : :
17365 : : tree
17366 : 10188589 : build_enumerator (tree name, tree value, tree enumtype, tree attributes,
17367 : : location_t loc)
17368 : : {
17369 : 10188589 : tree decl;
17370 : 10188589 : tree context;
17371 : 10188589 : tree type;
17372 : :
17373 : : /* scalar_constant_value will pull out this expression, so make sure
17374 : : it's folded as appropriate. */
17375 : 10188589 : if (processing_template_decl)
17376 : 338589 : value = fold_non_dependent_expr (value);
17377 : :
17378 : : /* If the VALUE was erroneous, pretend it wasn't there; that will
17379 : : result in the enum being assigned the next value in sequence. */
17380 : 10188589 : if (value == error_mark_node)
17381 : : value = NULL_TREE;
17382 : :
17383 : : /* Remove no-op casts from the value. */
17384 : 10188544 : if (value)
17385 : 7599225 : STRIP_TYPE_NOPS (value);
17386 : :
17387 : 10188589 : if (! processing_template_decl)
17388 : : {
17389 : : /* Validate and default VALUE. */
17390 : 9850000 : if (value != NULL_TREE)
17391 : : {
17392 : 4318153 : if (!ENUM_UNDERLYING_TYPE (enumtype))
17393 : : {
17394 : 2416933 : tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
17395 : : value, true);
17396 : 2416933 : if (tmp_value)
17397 : 4318153 : value = tmp_value;
17398 : : }
17399 : 1901220 : else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
17400 : : (TREE_TYPE (value)))
17401 : 39 : value = perform_implicit_conversion_flags
17402 : 39 : (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
17403 : : LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
17404 : :
17405 : 4318153 : if (value == error_mark_node)
17406 : : value = NULL_TREE;
17407 : :
17408 : 4318132 : if (value != NULL_TREE)
17409 : : {
17410 : 4318132 : if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
17411 : : (TREE_TYPE (value)))
17412 : : {
17413 : 19 : error_at (cp_expr_loc_or_input_loc (value),
17414 : : "enumerator value for %qD must have integral or "
17415 : : "unscoped enumeration type", name);
17416 : 19 : value = NULL_TREE;
17417 : : }
17418 : : else
17419 : : {
17420 : 4318113 : value = cxx_constant_value (value);
17421 : :
17422 : 4318113 : if (TREE_CODE (value) != INTEGER_CST)
17423 : : {
17424 : 53 : error ("enumerator value for %qD is not an integer "
17425 : : "constant", name);
17426 : 53 : value = NULL_TREE;
17427 : : }
17428 : : }
17429 : : }
17430 : : }
17431 : :
17432 : : /* Default based on previous value. */
17433 : 72 : if (value == NULL_TREE)
17434 : : {
17435 : 5531940 : if (TYPE_VALUES (enumtype))
17436 : : {
17437 : 5117714 : tree prev_value;
17438 : :
17439 : : /* C++03 7.2/4: If no initializer is specified for the first
17440 : : enumerator, the type is an unspecified integral
17441 : : type. Otherwise the type is the same as the type of the
17442 : : initializing value of the preceding enumerator unless the
17443 : : incremented value is not representable in that type, in
17444 : : which case the type is an unspecified integral type
17445 : : sufficient to contain the incremented value. */
17446 : 5117714 : prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
17447 : 5117714 : if (TREE_CODE (prev_value) != INTEGER_CST)
17448 : 3 : value = error_mark_node;
17449 : : else
17450 : : {
17451 : 5117711 : wi::overflow_type overflowed;
17452 : 5117711 : tree type = TREE_TYPE (prev_value);
17453 : 5117711 : signop sgn = TYPE_SIGN (type);
17454 : 5117711 : widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
17455 : 5117711 : &overflowed);
17456 : 5117711 : if (!overflowed)
17457 : : {
17458 : 5117711 : bool pos = !wi::neg_p (wi, sgn);
17459 : 5117711 : if (!wi::fits_to_tree_p (wi, type))
17460 : : {
17461 : : unsigned int itk;
17462 : 63 : for (itk = itk_int; itk != itk_none; itk++)
17463 : : {
17464 : 63 : type = integer_types[itk];
17465 : 63 : if (type != NULL_TREE
17466 : 63 : && (pos || !TYPE_UNSIGNED (type))
17467 : 126 : && wi::fits_to_tree_p (wi, type))
17468 : : break;
17469 : : }
17470 : 21 : if (type && cxx_dialect < cxx11
17471 : 6 : && itk > itk_unsigned_long)
17472 : 3 : pedwarn (input_location, OPT_Wlong_long,
17473 : : pos ? G_("\
17474 : : incremented enumerator value is too large for %<unsigned long%>") : G_("\
17475 : : incremented enumerator value is too large for %<long%>"));
17476 : : }
17477 : 5117711 : if (type == NULL_TREE)
17478 : 0 : overflowed = wi::OVF_UNKNOWN;
17479 : : else
17480 : 5117711 : value = wide_int_to_tree (type, wi);
17481 : : }
17482 : :
17483 : 5117711 : if (overflowed)
17484 : : {
17485 : 0 : error ("overflow in enumeration values at %qD", name);
17486 : 0 : value = error_mark_node;
17487 : : }
17488 : 5117711 : }
17489 : : }
17490 : : else
17491 : 414226 : value = integer_zero_node;
17492 : : }
17493 : :
17494 : : /* Remove no-op casts from the value. */
17495 : 9850000 : STRIP_TYPE_NOPS (value);
17496 : :
17497 : : /* If the underlying type of the enum is fixed, check whether
17498 : : the enumerator values fits in the underlying type. If it
17499 : : does not fit, the program is ill-formed [C++0x dcl.enum]. */
17500 : 9850000 : if (ENUM_UNDERLYING_TYPE (enumtype)
17501 : : && value
17502 : 9850000 : && TREE_CODE (value) == INTEGER_CST)
17503 : : {
17504 : 2610838 : if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
17505 : 9 : error ("enumerator value %qE is outside the range of underlying "
17506 : 9 : "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
17507 : :
17508 : : /* Convert the value to the appropriate type. */
17509 : 2610838 : value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
17510 : : }
17511 : : }
17512 : :
17513 : : /* C++ associates enums with global, function, or class declarations. */
17514 : 10188589 : context = current_scope ();
17515 : :
17516 : : /* Build the actual enumeration constant. Note that the enumeration
17517 : : constants have the underlying type of the enum (if it is fixed)
17518 : : or the type of their initializer (if the underlying type of the
17519 : : enum is not fixed):
17520 : :
17521 : : [ C++0x dcl.enum ]
17522 : :
17523 : : If the underlying type is fixed, the type of each enumerator
17524 : : prior to the closing brace is the underlying type; if the
17525 : : initializing value of an enumerator cannot be represented by
17526 : : the underlying type, the program is ill-formed. If the
17527 : : underlying type is not fixed, the type of each enumerator is
17528 : : the type of its initializing value.
17529 : :
17530 : : If the underlying type is not fixed, it will be computed by
17531 : : finish_enum and we will reset the type of this enumerator. Of
17532 : : course, if we're processing a template, there may be no value. */
17533 : 10188589 : type = value ? TREE_TYPE (value) : NULL_TREE;
17534 : :
17535 : 10188589 : decl = build_decl (loc, CONST_DECL, name, type);
17536 : :
17537 : 10188589 : DECL_CONTEXT (decl) = enumtype;
17538 : 10188589 : TREE_CONSTANT (decl) = 1;
17539 : 10188589 : TREE_READONLY (decl) = 1;
17540 : 10188589 : DECL_INITIAL (decl) = value;
17541 : :
17542 : 10188589 : if (attributes)
17543 : 139 : cplus_decl_attributes (&decl, attributes, 0);
17544 : :
17545 : 10188589 : if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
17546 : : {
17547 : : /* In something like `struct S { enum E { i = 7 }; };' we put `i'
17548 : : on the TYPE_FIELDS list for `S'. (That's so that you can say
17549 : : things like `S::i' later.) */
17550 : :
17551 : : /* The enumerator may be getting declared outside of its enclosing
17552 : : class, like so:
17553 : :
17554 : : class S { public: enum E : int; }; enum S::E : int { i = 7; };
17555 : :
17556 : : For which case we need to make sure that the access of `S::i'
17557 : : matches the access of `S::E'. */
17558 : 1813338 : auto cas = make_temp_override (current_access_specifier);
17559 : 1813338 : set_current_access_from_decl (TYPE_NAME (enumtype));
17560 : 1813338 : finish_member_declaration (decl);
17561 : 1813338 : }
17562 : : else
17563 : 8375251 : pushdecl (decl);
17564 : :
17565 : : /* Add this enumeration constant to the list for this type. */
17566 : 10188589 : TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
17567 : :
17568 : 10188589 : return decl;
17569 : : }
17570 : :
17571 : : /* Look for an enumerator with the given NAME within the enumeration
17572 : : type ENUMTYPE. This routine is used primarily for qualified name
17573 : : lookup into an enumerator in C++0x, e.g.,
17574 : :
17575 : : enum class Color { Red, Green, Blue };
17576 : :
17577 : : Color color = Color::Red;
17578 : :
17579 : : Returns the value corresponding to the enumerator, or
17580 : : NULL_TREE if no such enumerator was found. */
17581 : : tree
17582 : 6619987 : lookup_enumerator (tree enumtype, tree name)
17583 : : {
17584 : 6619987 : tree e;
17585 : 6619987 : gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
17586 : :
17587 : 6619987 : e = purpose_member (name, TYPE_VALUES (enumtype));
17588 : 13239877 : return e? TREE_VALUE (e) : NULL_TREE;
17589 : : }
17590 : :
17591 : : /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
17592 : :
17593 : : tree
17594 : 0 : cxx_simulate_enum_decl (location_t loc, const char *name,
17595 : : vec<string_int_pair> *values)
17596 : : {
17597 : 0 : location_t saved_loc = input_location;
17598 : 0 : input_location = loc;
17599 : :
17600 : 0 : tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
17601 : : NULL_TREE, false, NULL);
17602 : 0 : if (!OPAQUE_ENUM_P (enumtype))
17603 : : {
17604 : 0 : error_at (loc, "multiple definition of %q#T", enumtype);
17605 : 0 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
17606 : : "previous definition here");
17607 : 0 : return enumtype;
17608 : : }
17609 : 0 : SET_OPAQUE_ENUM_P (enumtype, false);
17610 : 0 : DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
17611 : :
17612 : 0 : for (const string_int_pair &value : values)
17613 : 0 : build_enumerator (get_identifier (value.first),
17614 : 0 : build_int_cst (integer_type_node, value.second),
17615 : : enumtype, NULL_TREE, loc);
17616 : :
17617 : 0 : finish_enum_value_list (enumtype);
17618 : 0 : finish_enum (enumtype);
17619 : :
17620 : 0 : input_location = saved_loc;
17621 : 0 : return enumtype;
17622 : : }
17623 : :
17624 : : /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
17625 : :
17626 : : tree
17627 : 0 : cxx_simulate_record_decl (location_t loc, const char *name,
17628 : : array_slice<const tree> fields)
17629 : : {
17630 : 0 : iloc_sentinel ils (loc);
17631 : :
17632 : 0 : tree ident = get_identifier (name);
17633 : 0 : tree type = xref_tag (/*tag_code=*/record_type, ident);
17634 : 0 : if (type != error_mark_node
17635 : 0 : && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
17636 : : {
17637 : 0 : error ("redefinition of %q#T", type);
17638 : 0 : type = error_mark_node;
17639 : : }
17640 : 0 : if (type == error_mark_node)
17641 : 0 : return lhd_simulate_record_decl (loc, name, fields);
17642 : :
17643 : 0 : xref_basetypes (type, NULL_TREE);
17644 : 0 : type = begin_class_definition (type);
17645 : 0 : if (type == error_mark_node)
17646 : 0 : return lhd_simulate_record_decl (loc, name, fields);
17647 : :
17648 : 0 : for (tree field : fields)
17649 : 0 : finish_member_declaration (field);
17650 : :
17651 : 0 : type = finish_struct (type, NULL_TREE);
17652 : :
17653 : 0 : tree decl = build_decl (loc, TYPE_DECL, ident, type);
17654 : 0 : set_underlying_type (decl);
17655 : 0 : lang_hooks.decls.pushdecl (decl);
17656 : :
17657 : 0 : return type;
17658 : 0 : }
17659 : :
17660 : : /* We're defining DECL. Make sure that its type is OK. */
17661 : :
17662 : : static void
17663 : 128402104 : check_function_type (tree decl, tree current_function_parms)
17664 : : {
17665 : 128402104 : tree fntype = TREE_TYPE (decl);
17666 : 128402104 : tree return_type = complete_type (TREE_TYPE (fntype));
17667 : :
17668 : : /* In a function definition, arg types must be complete. */
17669 : 128402104 : require_complete_types_for_parms (current_function_parms);
17670 : :
17671 : 128402104 : if (dependent_type_p (return_type)
17672 : 128402104 : || type_uses_auto (return_type))
17673 : 38860449 : return;
17674 : 89541655 : if (!COMPLETE_OR_VOID_TYPE_P (return_type))
17675 : : {
17676 : 15 : tree args = TYPE_ARG_TYPES (fntype);
17677 : :
17678 : 15 : error ("return type %q#T is incomplete", return_type);
17679 : :
17680 : : /* Make it return void instead. */
17681 : 15 : if (TREE_CODE (fntype) == METHOD_TYPE)
17682 : 4 : fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
17683 : : void_type_node,
17684 : 4 : TREE_CHAIN (args));
17685 : : else
17686 : 11 : fntype = build_function_type (void_type_node, args);
17687 : 15 : fntype = (cp_build_type_attribute_variant
17688 : 15 : (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
17689 : 15 : fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
17690 : 15 : TREE_TYPE (decl) = fntype;
17691 : : }
17692 : : else
17693 : : {
17694 : 89541640 : abstract_virtuals_error (decl, TREE_TYPE (fntype));
17695 : 89541640 : maybe_warn_parm_abi (TREE_TYPE (fntype),
17696 : 89541640 : DECL_SOURCE_LOCATION (decl));
17697 : : }
17698 : : }
17699 : :
17700 : : /* True iff FN is an implicitly-defined default constructor. */
17701 : :
17702 : : static bool
17703 : 4284036 : implicit_default_ctor_p (tree fn)
17704 : : {
17705 : 4284036 : return (DECL_CONSTRUCTOR_P (fn)
17706 : 4284036 : && !user_provided_p (fn)
17707 : 4857280 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
17708 : : }
17709 : :
17710 : : /* Clobber the contents of *this to let the back end know that the object
17711 : : storage is dead when we enter the constructor or leave the destructor. */
17712 : :
17713 : : static tree
17714 : 5108559 : build_clobber_this (clobber_kind kind)
17715 : : {
17716 : : /* Clobbering an empty base is pointless, and harmful if its one byte
17717 : : TYPE_SIZE overlays real data. */
17718 : 5108559 : if (is_empty_class (current_class_type))
17719 : 0 : return void_node;
17720 : :
17721 : : /* If we have virtual bases, clobber the whole object, but only if we're in
17722 : : charge. If we don't have virtual bases, clobber the as-base type so we
17723 : : don't mess with tail padding. */
17724 : 5108559 : bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
17725 : :
17726 : 5108559 : tree ctype = current_class_type;
17727 : 5108559 : if (!vbases)
17728 : 5092435 : ctype = CLASSTYPE_AS_BASE (ctype);
17729 : :
17730 : 5108559 : tree clobber = build_clobber (ctype, kind);
17731 : :
17732 : 5108559 : tree thisref = current_class_ref;
17733 : 5108559 : if (ctype != current_class_type)
17734 : : {
17735 : 560561 : thisref = build_nop (build_reference_type (ctype), current_class_ptr);
17736 : 560561 : thisref = convert_from_reference (thisref);
17737 : : }
17738 : :
17739 : 5108559 : tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
17740 : 5108559 : if (vbases)
17741 : 16124 : exprstmt = build_if_in_charge (exprstmt);
17742 : :
17743 : : return exprstmt;
17744 : : }
17745 : :
17746 : : /* Create the FUNCTION_DECL for a function definition.
17747 : : DECLSPECS and DECLARATOR are the parts of the declaration;
17748 : : they describe the function's name and the type it returns,
17749 : : but twisted together in a fashion that parallels the syntax of C.
17750 : :
17751 : : FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17752 : : DECLARATOR is really the DECL for the function we are about to
17753 : : process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17754 : : indicating that the function is an inline defined in-class.
17755 : :
17756 : : This function creates a binding context for the function body
17757 : : as well as setting up the FUNCTION_DECL in current_function_decl.
17758 : :
17759 : : For C++, we must first check whether that datum makes any sense.
17760 : : For example, "class A local_a(1,2);" means that variable local_a
17761 : : is an aggregate of type A, which should have a constructor
17762 : : applied to it with the argument list [1, 2].
17763 : :
17764 : : On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17765 : : or may be a BLOCK if the function has been defined previously
17766 : : in this translation unit. On exit, DECL_INITIAL (decl1) will be
17767 : : error_mark_node if the function has never been defined, or
17768 : : a BLOCK if the function has been defined somewhere. */
17769 : :
17770 : : bool
17771 : 128402144 : start_preparsed_function (tree decl1, tree attrs, int flags)
17772 : : {
17773 : 128402144 : tree ctype = NULL_TREE;
17774 : 128402144 : bool doing_friend = false;
17775 : :
17776 : : /* Sanity check. */
17777 : 128402144 : gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
17778 : 128402144 : gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
17779 : :
17780 : 128402144 : tree fntype = TREE_TYPE (decl1);
17781 : 128402144 : if (DECL_CLASS_SCOPE_P (decl1))
17782 : 92459087 : ctype = DECL_CONTEXT (decl1);
17783 : : else
17784 : : {
17785 : 71886114 : ctype = DECL_FRIEND_CONTEXT (decl1);
17786 : :
17787 : 35943057 : if (ctype)
17788 : 128402144 : doing_friend = true;
17789 : : }
17790 : :
17791 : 128402144 : if (DECL_DECLARED_INLINE_P (decl1)
17792 : 128402144 : && lookup_attribute ("noinline", attrs))
17793 : 0 : warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17794 : : "inline function %qD given attribute %qs", decl1, "noinline");
17795 : :
17796 : : /* Handle gnu_inline attribute. */
17797 : 128402144 : if (GNU_INLINE_P (decl1))
17798 : : {
17799 : 1488476 : DECL_EXTERNAL (decl1) = 1;
17800 : 1488476 : DECL_NOT_REALLY_EXTERN (decl1) = 0;
17801 : 1488476 : DECL_INTERFACE_KNOWN (decl1) = 1;
17802 : 1488476 : DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17803 : : }
17804 : :
17805 : 128402144 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17806 : : /* This is a constructor, we must ensure that any default args
17807 : : introduced by this definition are propagated to the clones
17808 : : now. The clones are used directly in overload resolution. */
17809 : 16682442 : adjust_clone_args (decl1);
17810 : :
17811 : : /* Sometimes we don't notice that a function is a static member, and
17812 : : build a METHOD_TYPE for it. Fix that up now. */
17813 : 128402144 : gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1)
17814 : : && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17815 : :
17816 : : /* Set up current_class_type, and enter the scope of the class, if
17817 : : appropriate. */
17818 : 128402144 : if (ctype)
17819 : 94663746 : push_nested_class (ctype);
17820 : :
17821 : : /* Now that we have entered the scope of the class, we must restore
17822 : : the bindings for any template parameters surrounding DECL1, if it
17823 : : is an inline member template. (Order is important; consider the
17824 : : case where a template parameter has the same name as a field of
17825 : : the class.) It is not until after this point that
17826 : : PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17827 : 128402144 : if (flags & SF_INCLASS_INLINE)
17828 : 62371793 : maybe_begin_member_template_processing (decl1);
17829 : :
17830 : : /* Effective C++ rule 15. */
17831 : 128402144 : if (warn_ecpp
17832 : 288 : && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17833 : 40 : && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17834 : 128402184 : && VOID_TYPE_P (TREE_TYPE (fntype)))
17835 : 0 : warning (OPT_Weffc__,
17836 : : "%<operator=%> should return a reference to %<*this%>");
17837 : :
17838 : : /* Make the init_value nonzero so pushdecl knows this is not tentative.
17839 : : error_mark_node is replaced below (in poplevel) with the BLOCK. */
17840 : 128402144 : if (!DECL_INITIAL (decl1))
17841 : 25863216 : DECL_INITIAL (decl1) = error_mark_node;
17842 : :
17843 : : /* This function exists in static storage.
17844 : : (This does not mean `static' in the C sense!) */
17845 : 128402144 : TREE_STATIC (decl1) = 1;
17846 : :
17847 : : /* We must call push_template_decl after current_class_type is set
17848 : : up. (If we are processing inline definitions after exiting a
17849 : : class scope, current_class_type will be NULL_TREE until set above
17850 : : by push_nested_class.) */
17851 : 128402144 : if (processing_template_decl)
17852 : : {
17853 : 77853979 : tree newdecl1 = push_template_decl (decl1, doing_friend);
17854 : 77853979 : if (newdecl1 == error_mark_node)
17855 : : {
17856 : 40 : if (ctype)
17857 : 12 : pop_nested_class ();
17858 : 40 : return false;
17859 : : }
17860 : : decl1 = newdecl1;
17861 : : }
17862 : :
17863 : : /* Make sure the parameter and return types are reasonable. When
17864 : : you declare a function, these types can be incomplete, but they
17865 : : must be complete when you define the function. */
17866 : 128402104 : check_function_type (decl1, DECL_ARGUMENTS (decl1));
17867 : :
17868 : : /* Build the return declaration for the function. */
17869 : 128402104 : tree restype = TREE_TYPE (fntype);
17870 : :
17871 : 128402104 : if (DECL_RESULT (decl1) == NULL_TREE)
17872 : : {
17873 : : /* In a template instantiation, copy the return type location. When
17874 : : parsing, the location will be set in grokdeclarator. */
17875 : 49462889 : location_t loc = input_location;
17876 : 49462889 : if (DECL_TEMPLATE_INSTANTIATION (decl1))
17877 : : {
17878 : 23112328 : tree tmpl = template_for_substitution (decl1);
17879 : 23112328 : if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl)))
17880 : 22234807 : loc = DECL_SOURCE_LOCATION (res);
17881 : : }
17882 : :
17883 : 49462889 : tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
17884 : 49462889 : DECL_ARTIFICIAL (resdecl) = 1;
17885 : 49462889 : DECL_IGNORED_P (resdecl) = 1;
17886 : 49462889 : DECL_RESULT (decl1) = resdecl;
17887 : :
17888 : 49462889 : cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17889 : : }
17890 : :
17891 : : /* Record the decl so that the function name is defined.
17892 : : If we already have a decl for this name, and it is a FUNCTION_DECL,
17893 : : use the old decl. */
17894 : 128402104 : if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17895 : : {
17896 : : /* A specialization is not used to guide overload resolution. */
17897 : 18474545 : if (!DECL_FUNCTION_MEMBER_P (decl1)
17898 : 18462262 : && !(DECL_USE_TEMPLATE (decl1) &&
17899 : 680 : PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17900 : : {
17901 : 8854450 : tree olddecl = pushdecl (decl1);
17902 : :
17903 : 8854450 : if (olddecl == error_mark_node)
17904 : : /* If something went wrong when registering the declaration,
17905 : : use DECL1; we have to have a FUNCTION_DECL to use when
17906 : : parsing the body of the function. */
17907 : : ;
17908 : : else
17909 : : {
17910 : : /* Otherwise, OLDDECL is either a previous declaration
17911 : : of the same function or DECL1 itself. */
17912 : :
17913 : 8854182 : if (warn_missing_declarations
17914 : 28 : && olddecl == decl1
17915 : 20 : && !DECL_MAIN_P (decl1)
17916 : 20 : && TREE_PUBLIC (decl1)
17917 : 8854198 : && !DECL_DECLARED_INLINE_P (decl1))
17918 : : {
17919 : 12 : tree context;
17920 : :
17921 : : /* Check whether DECL1 is in an anonymous
17922 : : namespace. */
17923 : 12 : for (context = DECL_CONTEXT (decl1);
17924 : 24 : context;
17925 : 12 : context = DECL_CONTEXT (context))
17926 : : {
17927 : 16 : if (TREE_CODE (context) == NAMESPACE_DECL
17928 : 16 : && DECL_NAME (context) == NULL_TREE)
17929 : : break;
17930 : : }
17931 : :
17932 : 12 : if (context == NULL)
17933 : 8 : warning_at (DECL_SOURCE_LOCATION (decl1),
17934 : : OPT_Wmissing_declarations,
17935 : : "no previous declaration for %qD", decl1);
17936 : : }
17937 : :
17938 : : decl1 = olddecl;
17939 : : }
17940 : : }
17941 : : else
17942 : : {
17943 : : /* We need to set the DECL_CONTEXT. */
17944 : 752682 : if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17945 : 0 : DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17946 : : }
17947 : 9607132 : fntype = TREE_TYPE (decl1);
17948 : 9607132 : restype = TREE_TYPE (fntype);
17949 : :
17950 : : /* If #pragma weak applies, mark the decl appropriately now.
17951 : : The pragma only applies to global functions. Because
17952 : : determining whether or not the #pragma applies involves
17953 : : computing the mangled name for the declaration, we cannot
17954 : : apply the pragma until after we have merged this declaration
17955 : : with any previous declarations; if the original declaration
17956 : : has a linkage specification, that specification applies to
17957 : : the definition as well, and may affect the mangled name. */
17958 : 9607132 : if (DECL_FILE_SCOPE_P (decl1))
17959 : 2243467 : maybe_apply_pragma_weak (decl1);
17960 : : }
17961 : :
17962 : : /* We are now in the scope of the function being defined. */
17963 : 128402104 : current_function_decl = decl1;
17964 : :
17965 : : /* Save the parm names or decls from this function's declarator
17966 : : where store_parm_decls will find them. */
17967 : 128402104 : tree current_function_parms = DECL_ARGUMENTS (decl1);
17968 : :
17969 : : /* Let the user know we're compiling this function. */
17970 : 128402104 : announce_function (decl1);
17971 : :
17972 : 128402104 : gcc_assert (DECL_INITIAL (decl1));
17973 : :
17974 : : /* This function may already have been parsed, in which case just
17975 : : return; our caller will skip over the body without parsing. */
17976 : 128402104 : if (DECL_INITIAL (decl1) != error_mark_node)
17977 : : return true;
17978 : :
17979 : : /* Initialize RTL machinery. We cannot do this until
17980 : : CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
17981 : : even when processing a template; this is how we get
17982 : : CFUN set up, and our per-function variables initialized.
17983 : : FIXME factor out the non-RTL stuff. */
17984 : 128402104 : cp_binding_level *bl = current_binding_level;
17985 : 128402104 : allocate_struct_function (decl1, processing_template_decl);
17986 : :
17987 : : /* Initialize the language data structures. Whenever we start
17988 : : a new function, we destroy temporaries in the usual way. */
17989 : 128402104 : cfun->language = ggc_cleared_alloc<language_function> ();
17990 : 128402104 : current_stmt_tree ()->stmts_are_full_exprs_p = 1;
17991 : 128402104 : current_binding_level = bl;
17992 : :
17993 : : /* If we are (erroneously) defining a function that we have already
17994 : : defined before, wipe out what we knew before. */
17995 : 128402104 : gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
17996 : 128402104 : FNDECL_USED_AUTO (decl1) = false;
17997 : 128402104 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
17998 : :
17999 : 128402104 : if (!processing_template_decl && type_uses_auto (restype))
18000 : : {
18001 : 543869 : FNDECL_USED_AUTO (decl1) = true;
18002 : 543869 : DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
18003 : : }
18004 : :
18005 : : /* Start the statement-tree, start the tree now. */
18006 : 128402104 : DECL_SAVED_TREE (decl1) = push_stmt_list ();
18007 : :
18008 : 128402104 : if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1))
18009 : : {
18010 : : /* We know that this was set up by `grokclassfn'. We do not
18011 : : wait until `store_parm_decls', since evil parse errors may
18012 : : never get us to that point. Here we keep the consistency
18013 : : between `current_class_type' and `current_class_ptr'. */
18014 : 82228587 : tree t = DECL_ARGUMENTS (decl1);
18015 : :
18016 : 82228587 : gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
18017 : 82228587 : gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
18018 : :
18019 : 82228587 : cp_function_chain->x_current_class_ref
18020 : 82228587 : = cp_build_fold_indirect_ref (t);
18021 : : /* Set this second to avoid shortcut in cp_build_indirect_ref. */
18022 : 82228587 : cp_function_chain->x_current_class_ptr = t;
18023 : :
18024 : : /* Constructors and destructors need to know whether they're "in
18025 : : charge" of initializing virtual base classes. */
18026 : 82228587 : t = DECL_CHAIN (t);
18027 : 82228587 : if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
18028 : : {
18029 : 1384687 : current_in_charge_parm = t;
18030 : 1384687 : t = DECL_CHAIN (t);
18031 : : }
18032 : 82228587 : if (DECL_HAS_VTT_PARM_P (decl1))
18033 : : {
18034 : 37345 : gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
18035 : 37345 : current_vtt_parm = t;
18036 : : }
18037 : : }
18038 : :
18039 : 128402104 : bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
18040 : : /* Implicitly-defined methods (like the
18041 : : destructor for a class in which no destructor
18042 : : is explicitly declared) must not be defined
18043 : : until their definition is needed. So, we
18044 : : ignore interface specifications for
18045 : : compiler-generated functions. */
18046 : 128402104 : && !DECL_ARTIFICIAL (decl1));
18047 : 128402104 : struct c_fileinfo *finfo
18048 : 128402104 : = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
18049 : :
18050 : 128402104 : if (processing_template_decl)
18051 : : /* Don't mess with interface flags. */;
18052 : 50548165 : else if (DECL_INTERFACE_KNOWN (decl1))
18053 : : {
18054 : 16547490 : tree ctx = decl_function_context (decl1);
18055 : :
18056 : 16547490 : if (DECL_NOT_REALLY_EXTERN (decl1))
18057 : 13309974 : DECL_EXTERNAL (decl1) = 0;
18058 : :
18059 : 16547490 : if (ctx != NULL_TREE && vague_linkage_p (ctx))
18060 : : /* This is a function in a local class in an extern inline
18061 : : or template function. */
18062 : 2511216 : comdat_linkage (decl1);
18063 : : }
18064 : : /* If this function belongs to an interface, it is public.
18065 : : If it belongs to someone else's interface, it is also external.
18066 : : This only affects inlines and template instantiations. */
18067 : 34000675 : else if (!finfo->interface_unknown && honor_interface)
18068 : : {
18069 : 140 : if (DECL_DECLARED_INLINE_P (decl1)
18070 : 140 : || DECL_TEMPLATE_INSTANTIATION (decl1))
18071 : : {
18072 : 112 : DECL_EXTERNAL (decl1)
18073 : 224 : = (finfo->interface_only
18074 : 112 : || (DECL_DECLARED_INLINE_P (decl1)
18075 : 53 : && ! flag_implement_inlines
18076 : 0 : && !DECL_VINDEX (decl1)));
18077 : :
18078 : : /* For WIN32 we also want to put these in linkonce sections. */
18079 : 112 : maybe_make_one_only (decl1);
18080 : : }
18081 : : else
18082 : 28 : DECL_EXTERNAL (decl1) = 0;
18083 : 140 : DECL_INTERFACE_KNOWN (decl1) = 1;
18084 : : /* If this function is in an interface implemented in this file,
18085 : : make sure that the back end knows to emit this function
18086 : : here. */
18087 : 140 : if (!DECL_EXTERNAL (decl1))
18088 : 81 : mark_needed (decl1);
18089 : : }
18090 : 34000535 : else if (finfo->interface_unknown && finfo->interface_only
18091 : 0 : && honor_interface)
18092 : : {
18093 : : /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
18094 : : interface, we will have both finfo->interface_unknown and
18095 : : finfo->interface_only set. In that case, we don't want to
18096 : : use the normal heuristics because someone will supply a
18097 : : #pragma implementation elsewhere, and deducing it here would
18098 : : produce a conflict. */
18099 : 0 : comdat_linkage (decl1);
18100 : 0 : DECL_EXTERNAL (decl1) = 0;
18101 : 0 : DECL_INTERFACE_KNOWN (decl1) = 1;
18102 : 0 : DECL_DEFER_OUTPUT (decl1) = 1;
18103 : : }
18104 : : else
18105 : : {
18106 : : /* This is a definition, not a reference.
18107 : : So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
18108 : 34000535 : if (!GNU_INLINE_P (decl1))
18109 : 34000535 : DECL_EXTERNAL (decl1) = 0;
18110 : :
18111 : 34000535 : if ((DECL_DECLARED_INLINE_P (decl1)
18112 : 1943759 : || DECL_TEMPLATE_INSTANTIATION (decl1))
18113 : 35706707 : && ! DECL_INTERFACE_KNOWN (decl1))
18114 : 33762948 : DECL_DEFER_OUTPUT (decl1) = 1;
18115 : : else
18116 : 237587 : DECL_INTERFACE_KNOWN (decl1) = 1;
18117 : : }
18118 : :
18119 : : /* Determine the ELF visibility attribute for the function. We must not
18120 : : do this before calling "pushdecl", as we must allow "duplicate_decls"
18121 : : to merge any attributes appropriately. We also need to wait until
18122 : : linkage is set. */
18123 : 128402104 : if (!DECL_CLONED_FUNCTION_P (decl1))
18124 : 115784288 : determine_visibility (decl1);
18125 : :
18126 : 128402104 : if (!processing_template_decl)
18127 : 50548165 : maybe_instantiate_noexcept (decl1);
18128 : :
18129 : 128402104 : begin_scope (sk_function_parms, decl1);
18130 : :
18131 : 128402104 : ++function_depth;
18132 : :
18133 : 128402104 : start_fname_decls ();
18134 : :
18135 : 128402104 : store_parm_decls (current_function_parms);
18136 : :
18137 : 128402104 : start_function_contracts (decl1);
18138 : :
18139 : 128402104 : if (!processing_template_decl
18140 : 50548165 : && (flag_lifetime_dse > 1)
18141 : 101072106 : && DECL_CONSTRUCTOR_P (decl1)
18142 : 14419002 : && !DECL_CLONED_FUNCTION_P (decl1)
18143 : : /* Clobbering an empty base is harmful if it overlays real data. */
18144 : 4801046 : && !is_empty_class (current_class_type)
18145 : : /* We can't clobber safely for an implicitly-defined default constructor
18146 : : because part of the initialization might happen before we enter the
18147 : : constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
18148 : 132685586 : && !implicit_default_ctor_p (decl1))
18149 : 3917958 : finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN));
18150 : :
18151 : 128402104 : if (!processing_template_decl
18152 : 101096330 : && DECL_CONSTRUCTOR_P (decl1)
18153 : 14422350 : && sanitize_flags_p (SANITIZE_VPTR)
18154 : 1662 : && !DECL_CLONED_FUNCTION_P (decl1)
18155 : 128402658 : && !implicit_default_ctor_p (decl1))
18156 : 431 : cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
18157 : :
18158 : 128402104 : if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
18159 : 128401416 : start_lambda_scope (decl1);
18160 : :
18161 : : return true;
18162 : : }
18163 : :
18164 : :
18165 : : /* Like start_preparsed_function, except that instead of a
18166 : : FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
18167 : :
18168 : : Returns true on success. If the DECLARATOR is not suitable
18169 : : for a function, we return false, which tells the parser to
18170 : : skip the entire function. */
18171 : :
18172 : : bool
18173 : 36389629 : start_function (cp_decl_specifier_seq *declspecs,
18174 : : const cp_declarator *declarator,
18175 : : tree attrs)
18176 : : {
18177 : 36389629 : tree decl1;
18178 : :
18179 : 36389629 : decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
18180 : 36389629 : invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
18181 : 36389629 : if (decl1 == error_mark_node)
18182 : : return false;
18183 : :
18184 : 36389313 : if (DECL_MAIN_P (decl1))
18185 : : /* main must return int. grokfndecl should have corrected it
18186 : : (and issued a diagnostic) if the user got it wrong. */
18187 : 34808 : gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
18188 : : integer_type_node));
18189 : :
18190 : 36389313 : return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
18191 : : }
18192 : :
18193 : : /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
18194 : : FN. */
18195 : :
18196 : : static bool
18197 : 244188769 : use_eh_spec_block (tree fn)
18198 : : {
18199 : 242312150 : return (flag_exceptions && flag_enforce_eh_specs
18200 : 242312150 : && !processing_template_decl
18201 : : /* We insert the EH_SPEC_BLOCK only in the original
18202 : : function; then, it is copied automatically to the
18203 : : clones. */
18204 : 86979652 : && !DECL_CLONED_FUNCTION_P (fn)
18205 : : /* Implicitly-generated constructors and destructors have
18206 : : exception specifications. However, those specifications
18207 : : are the union of the possible exceptions specified by the
18208 : : constructors/destructors for bases and members, so no
18209 : : unallowed exception will ever reach this function. By
18210 : : not creating the EH_SPEC_BLOCK we save a little memory,
18211 : : and we avoid spurious warnings about unreachable
18212 : : code. */
18213 : 74404735 : && !DECL_DEFAULTED_FN (fn)
18214 : 316522566 : && !type_throw_all_p (TREE_TYPE (fn)));
18215 : : }
18216 : :
18217 : : /* Helper function to push ARGS into the current lexical scope. DECL
18218 : : is the function declaration. NONPARMS is used to handle enum
18219 : : constants. */
18220 : :
18221 : : void
18222 : 125636935 : do_push_parm_decls (tree decl, tree args, tree *nonparms)
18223 : : {
18224 : : /* If we're doing semantic analysis, then we'll call pushdecl
18225 : : for each of these. We must do them in reverse order so that
18226 : : they end in the correct forward order. */
18227 : 125636935 : args = nreverse (args);
18228 : :
18229 : 125636935 : tree next;
18230 : 387092820 : for (tree parm = args; parm; parm = next)
18231 : : {
18232 : 261455885 : next = DECL_CHAIN (parm);
18233 : 261455885 : if (TREE_CODE (parm) == PARM_DECL)
18234 : 261455885 : pushdecl (parm);
18235 : 0 : else if (nonparms)
18236 : : {
18237 : : /* If we find an enum constant or a type tag, put it aside for
18238 : : the moment. */
18239 : 0 : TREE_CHAIN (parm) = NULL_TREE;
18240 : 0 : *nonparms = chainon (*nonparms, parm);
18241 : : }
18242 : : }
18243 : :
18244 : : /* Get the decls in their original chain order and record in the
18245 : : function. This is all and only the PARM_DECLs that were
18246 : : pushed into scope by the loop above. */
18247 : 125636935 : DECL_ARGUMENTS (decl) = get_local_decls ();
18248 : 125636935 : }
18249 : :
18250 : : /* Store the parameter declarations into the current function declaration.
18251 : : This is called after parsing the parameter declarations, before
18252 : : digesting the body of the function.
18253 : :
18254 : : Also install to binding contour return value identifier, if any. */
18255 : :
18256 : : static void
18257 : 128404534 : store_parm_decls (tree current_function_parms)
18258 : : {
18259 : 128404534 : tree fndecl = current_function_decl;
18260 : :
18261 : : /* This is a chain of any other decls that came in among the parm
18262 : : declarations. If a parm is declared with enum {foo, bar} x;
18263 : : then CONST_DECLs for foo and bar are put here. */
18264 : 128404534 : tree nonparms = NULL_TREE;
18265 : :
18266 : 128404534 : if (current_function_parms)
18267 : : {
18268 : : /* This case is when the function was defined with an ANSI prototype.
18269 : : The parms already have decls, so we need not do anything here
18270 : : except record them as in effect
18271 : : and complain if any redundant old-style parm decls were written. */
18272 : :
18273 : 123307142 : tree specparms = current_function_parms;
18274 : :
18275 : : /* Must clear this because it might contain TYPE_DECLs declared
18276 : : at class level. */
18277 : 123307142 : current_binding_level->names = NULL;
18278 : :
18279 : 123307142 : do_push_parm_decls (fndecl, specparms, &nonparms);
18280 : : }
18281 : : else
18282 : 5097392 : DECL_ARGUMENTS (fndecl) = NULL_TREE;
18283 : :
18284 : : /* Now store the final chain of decls for the arguments
18285 : : as the decl-chain of the current lexical scope.
18286 : : Put the enumerators in as well, at the front so that
18287 : : DECL_ARGUMENTS is not modified. */
18288 : 128404534 : current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
18289 : :
18290 : 128404534 : if (use_eh_spec_block (current_function_decl))
18291 : 16733587 : current_eh_spec_block = begin_eh_spec_block ();
18292 : 128404534 : }
18293 : :
18294 : :
18295 : : /* Mark CDTOR's implicit THIS argument for returning, if required by
18296 : : the ABI.. Return the decl for THIS, if it is to be returned, and
18297 : : NULL otherwise. */
18298 : :
18299 : : tree
18300 : 102985167 : maybe_prepare_return_this (tree cdtor)
18301 : : {
18302 : 102985167 : if (targetm.cxx.cdtor_returns_this ())
18303 : 0 : if (tree val = DECL_ARGUMENTS (cdtor))
18304 : : {
18305 : 0 : suppress_warning (val, OPT_Wuse_after_free);
18306 : 0 : return val;
18307 : : }
18308 : :
18309 : : return NULL_TREE;
18310 : : }
18311 : :
18312 : : /* Set the return value of the [cd]tor if the ABI wants that. */
18313 : :
18314 : : void
18315 : 6420318 : maybe_return_this ()
18316 : : {
18317 : 6420318 : if (tree val = maybe_prepare_return_this (current_function_decl))
18318 : : {
18319 : : /* Return the address of the object. */
18320 : 0 : val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val);
18321 : 0 : val = build2 (MODIFY_EXPR, TREE_TYPE (val),
18322 : 0 : DECL_RESULT (current_function_decl), val);
18323 : 0 : tree exprstmt = build_stmt (input_location, RETURN_EXPR, val);
18324 : 0 : add_stmt (exprstmt);
18325 : : }
18326 : 6420318 : }
18327 : :
18328 : : /* Do all the processing for the beginning of a destructor; set up the
18329 : : vtable pointers and cleanups for bases and members. */
18330 : :
18331 : : static void
18332 : 1374066 : begin_destructor_body (void)
18333 : : {
18334 : 1374066 : tree compound_stmt;
18335 : :
18336 : : /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
18337 : : issued an error message. We still want to try to process the
18338 : : body of the function, but initialize_vtbl_ptrs will crash if
18339 : : TYPE_BINFO is NULL. */
18340 : 1374066 : if (COMPLETE_TYPE_P (current_class_type))
18341 : : {
18342 : 1374066 : compound_stmt = begin_compound_stmt (0);
18343 : : /* Make all virtual function table pointers in non-virtual base
18344 : : classes point to CURRENT_CLASS_TYPE's virtual function
18345 : : tables. */
18346 : 1374066 : initialize_vtbl_ptrs (current_class_ptr);
18347 : 1374066 : finish_compound_stmt (compound_stmt);
18348 : :
18349 : 1374066 : if (flag_lifetime_dse
18350 : : /* Clobbering an empty base is harmful if it overlays real data. */
18351 : 1374066 : && !is_empty_class (current_class_type))
18352 : : {
18353 : 1190680 : if (sanitize_flags_p (SANITIZE_VPTR)
18354 : 239 : && (flag_sanitize_recover & SANITIZE_VPTR) == 0
18355 : 1190759 : && TYPE_CONTAINS_VPTR_P (current_class_type))
18356 : : {
18357 : 79 : tree binfo = TYPE_BINFO (current_class_type);
18358 : 79 : tree ref
18359 : 79 : = cp_build_fold_indirect_ref (current_class_ptr);
18360 : :
18361 : 79 : tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
18362 : 79 : tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
18363 : 79 : tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
18364 : : NOP_EXPR, vtbl,
18365 : : tf_warning_or_error);
18366 : : /* If the vptr is shared with some virtual nearly empty base,
18367 : : don't clear it if not in charge, the dtor of the virtual
18368 : : nearly empty base will do that later. */
18369 : 79 : if (CLASSTYPE_VBASECLASSES (current_class_type))
18370 : : {
18371 : : tree c = current_class_type;
18372 : 82 : while (CLASSTYPE_PRIMARY_BINFO (c))
18373 : : {
18374 : 78 : if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
18375 : : {
18376 : 45 : stmt = convert_to_void (stmt, ICV_STATEMENT,
18377 : : tf_warning_or_error);
18378 : 45 : stmt = build_if_in_charge (stmt);
18379 : 45 : break;
18380 : : }
18381 : 33 : c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
18382 : : }
18383 : : }
18384 : 79 : finish_decl_cleanup (NULL_TREE, stmt);
18385 : : }
18386 : : else
18387 : 1190601 : finish_decl_cleanup (NULL_TREE,
18388 : : build_clobber_this (CLOBBER_OBJECT_END));
18389 : : }
18390 : :
18391 : : /* And insert cleanups for our bases and members so that they
18392 : : will be properly destroyed if we throw. */
18393 : 1374066 : push_base_cleanups ();
18394 : : }
18395 : 1374066 : }
18396 : :
18397 : : /* Do the necessary processing for the beginning of a function body, which
18398 : : in this case includes member-initializers, but not the catch clauses of
18399 : : a function-try-block. Currently, this means opening a binding level
18400 : : for the member-initializers (in a ctor), member cleanups (in a dtor),
18401 : : and capture proxies (in a lambda operator()). */
18402 : :
18403 : : tree
18404 : 103005894 : begin_function_body (void)
18405 : : {
18406 : 210349760 : if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
18407 : : return NULL_TREE;
18408 : :
18409 : 19961076 : if (processing_template_decl)
18410 : : /* Do nothing now. */;
18411 : : else
18412 : : /* Always keep the BLOCK node associated with the outermost pair of
18413 : : curly braces of a function. These are needed for correct
18414 : : operation of dwarfout.c. */
18415 : 6513178 : keep_next_level (true);
18416 : :
18417 : 19961076 : tree stmt = begin_compound_stmt (BCS_FN_BODY);
18418 : 19961076 : current_binding_level->artificial = 1;
18419 : :
18420 : 19961076 : if (processing_template_decl)
18421 : : /* Do nothing now. */;
18422 : 13026356 : else if (DECL_DESTRUCTOR_P (current_function_decl))
18423 : 1374066 : begin_destructor_body ();
18424 : :
18425 : : return stmt;
18426 : : }
18427 : :
18428 : : /* Do the processing for the end of a function body. Currently, this means
18429 : : closing out the cleanups for fully-constructed bases and members, and in
18430 : : the case of the destructor, deleting the object if desired. Again, this
18431 : : is only meaningful for [cd]tors, since they are the only functions where
18432 : : there is a significant distinction between the main body and any
18433 : : function catch clauses. Handling, say, main() return semantics here
18434 : : would be wrong, as flowing off the end of a function catch clause for
18435 : : main() would also need to return 0. */
18436 : :
18437 : : void
18438 : 103005869 : finish_function_body (tree compstmt)
18439 : : {
18440 : 103005869 : if (compstmt == NULL_TREE)
18441 : : return;
18442 : :
18443 : : /* Close the block. */
18444 : 19961076 : finish_compound_stmt (compstmt);
18445 : :
18446 : 19961076 : if (processing_template_decl)
18447 : : /* Do nothing now. */;
18448 : 6513178 : else if (DECL_CONSTRUCTOR_P (current_function_decl)
18449 : 6513178 : || DECL_DESTRUCTOR_P (current_function_decl))
18450 : 6176228 : maybe_return_this ();
18451 : : }
18452 : :
18453 : : /* Given a function, returns the BLOCK corresponding to the outermost level
18454 : : of curly braces, skipping the artificial block created for constructor
18455 : : initializers. */
18456 : :
18457 : : tree
18458 : 275859 : outer_curly_brace_block (tree fndecl)
18459 : : {
18460 : 275859 : tree block = DECL_INITIAL (fndecl);
18461 : 275859 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
18462 : : return block;
18463 : 157 : block = BLOCK_SUBBLOCKS (block);
18464 : 157 : if (BLOCK_OUTER_CURLY_BRACE_P (block))
18465 : : return block;
18466 : 0 : block = BLOCK_SUBBLOCKS (block);
18467 : 0 : gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
18468 : : return block;
18469 : : }
18470 : :
18471 : : /* If FNDECL is a class's key method, add the class to the list of
18472 : : keyed classes that should be emitted. */
18473 : :
18474 : : static void
18475 : 128405899 : record_key_method_defined (tree fndecl)
18476 : : {
18477 : 174583179 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)
18478 : 82237423 : && DECL_VIRTUAL_P (fndecl)
18479 : 130779981 : && !processing_template_decl)
18480 : : {
18481 : 1127907 : tree fnclass = DECL_CONTEXT (fndecl);
18482 : 1127907 : if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
18483 : 1913 : vec_safe_push (keyed_classes, fnclass);
18484 : : }
18485 : 128405899 : }
18486 : :
18487 : : /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
18488 : : of "return *this;" immediately before its location, using FNDECL's
18489 : : first statement (if any) to give the indentation, if appropriate. */
18490 : :
18491 : : static void
18492 : 24 : add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
18493 : : {
18494 : 24 : location_t indent = UNKNOWN_LOCATION;
18495 : 24 : tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
18496 : 24 : if (stmts)
18497 : 12 : indent = EXPR_LOCATION (stmts);
18498 : 24 : richloc->add_fixit_insert_formatted ("return *this;",
18499 : : richloc->get_loc (),
18500 : : indent);
18501 : 24 : }
18502 : :
18503 : : /* This function carries out the subset of finish_function operations needed
18504 : : to emit the compiler-generated outlined helper functions used by the
18505 : : coroutines implementation. */
18506 : :
18507 : : static void
18508 : 2430 : emit_coro_helper (tree helper)
18509 : : {
18510 : : /* This is a partial set of the operations done by finish_function()
18511 : : plus emitting the result. */
18512 : 2430 : set_cfun (NULL);
18513 : 2430 : current_function_decl = helper;
18514 : 2430 : begin_scope (sk_function_parms, NULL);
18515 : 2430 : store_parm_decls (DECL_ARGUMENTS (helper));
18516 : 2430 : announce_function (helper);
18517 : 2430 : allocate_struct_function (helper, false);
18518 : 2430 : cfun->language = ggc_cleared_alloc<language_function> ();
18519 : 2430 : poplevel (1, 0, 1);
18520 : 2430 : maybe_save_constexpr_fundef (helper);
18521 : : /* We must start each function with a clear fold cache. */
18522 : 2430 : clear_fold_cache ();
18523 : 2430 : cp_fold_function (helper);
18524 : 2430 : DECL_CONTEXT (DECL_RESULT (helper)) = helper;
18525 : 2430 : BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
18526 : : /* This function has coroutine IFNs that we should handle in middle
18527 : : end lowering. */
18528 : 2430 : cfun->coroutine_component = true;
18529 : 2430 : cp_genericize (helper);
18530 : 2430 : expand_or_defer_fn (helper);
18531 : 2430 : }
18532 : :
18533 : : /* Finish up a function declaration and compile that function
18534 : : all the way to assembler language output. The free the storage
18535 : : for the function definition. INLINE_P is TRUE if we just
18536 : : finished processing the body of an in-class inline function
18537 : : definition. (This processing will have taken place after the
18538 : : class definition is complete.) */
18539 : :
18540 : : tree
18541 : 128402064 : finish_function (bool inline_p)
18542 : : {
18543 : 128402064 : tree fndecl = current_function_decl;
18544 : 128402064 : tree fntype, ctype = NULL_TREE;
18545 : 128402064 : tree resumer = NULL_TREE, destroyer = NULL_TREE;
18546 : 128402064 : bool coro_p = flag_coroutines
18547 : 35169700 : && !processing_template_decl
18548 : 143520206 : && DECL_COROUTINE_P (fndecl);
18549 : 128402064 : bool coro_emit_helpers = false;
18550 : :
18551 : : /* When we get some parse errors, we can end up without a
18552 : : current_function_decl, so cope. */
18553 : 128402064 : if (fndecl == NULL_TREE)
18554 : 0 : return error_mark_node;
18555 : :
18556 : 128402064 : if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18557 : 128401376 : finish_lambda_scope ();
18558 : :
18559 : 128402064 : if (c_dialect_objc ())
18560 : 0 : objc_finish_function ();
18561 : :
18562 : 128402064 : record_key_method_defined (fndecl);
18563 : :
18564 : 128402064 : fntype = TREE_TYPE (fndecl);
18565 : :
18566 : : /* TREE_READONLY (fndecl) = 1;
18567 : : This caused &foo to be of type ptr-to-const-function
18568 : : which then got a warning when stored in a ptr-to-function variable. */
18569 : :
18570 : 128402064 : gcc_assert (building_stmt_list_p ());
18571 : : /* The current function is being defined, so its DECL_INITIAL should
18572 : : be set, and unless there's a multiple definition, it should be
18573 : : error_mark_node. */
18574 : 128402064 : gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
18575 : :
18576 : 128402064 : if (coro_p)
18577 : : {
18578 : : /* Only try to emit the coroutine outlined helper functions if the
18579 : : transforms succeeded. Otherwise, treat errors in the same way as
18580 : : a regular function. */
18581 : 1228 : coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
18582 : :
18583 : : /* We should handle coroutine IFNs in middle end lowering. */
18584 : 1228 : cfun->coroutine_component = true;
18585 : :
18586 : : /* Do not try to process the ramp's EH unless outlining succeeded. */
18587 : 1228 : if (coro_emit_helpers && use_eh_spec_block (fndecl))
18588 : 381 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
18589 : : (TREE_TYPE (fndecl)),
18590 : 381 : current_eh_spec_block);
18591 : : }
18592 : : else
18593 : : /* For a cloned function, we've already got all the code we need;
18594 : : there's no need to add any extra bits. */
18595 : 128400836 : if (!DECL_CLONED_FUNCTION_P (fndecl))
18596 : : {
18597 : : /* Make it so that `main' always returns 0 by default. */
18598 : 115783020 : if (DECL_MAIN_FREESTANDING_P (current_function_decl)
18599 : 115817814 : && !TREE_THIS_VOLATILE (current_function_decl))
18600 : 34794 : finish_return_stmt (integer_zero_node);
18601 : :
18602 : 115783020 : if (use_eh_spec_block (current_function_decl))
18603 : 16733206 : finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
18604 : : (TREE_TYPE (current_function_decl)),
18605 : 16733206 : current_eh_spec_block);
18606 : : }
18607 : :
18608 : : /* If we're saving up tree structure, tie off the function now. */
18609 : 128402064 : DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
18610 : :
18611 : 128402064 : finish_fname_decls ();
18612 : :
18613 : : /* This must come after expand_function_end because cleanups might
18614 : : have declarations (from inline functions) that need to go into
18615 : : this function's blocks. */
18616 : :
18617 : : /* If the current binding level isn't the outermost binding level
18618 : : for this function, either there is a bug, or we have experienced
18619 : : syntax errors and the statement tree is malformed. */
18620 : 128402064 : if (current_binding_level->kind != sk_function_parms)
18621 : : {
18622 : : /* Make sure we have already experienced errors. */
18623 : 4 : gcc_assert (errorcount);
18624 : :
18625 : : /* Throw away the broken statement tree and extra binding
18626 : : levels. */
18627 : 4 : DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
18628 : :
18629 : 8 : while (current_binding_level->kind != sk_function_parms)
18630 : : {
18631 : 4 : if (current_binding_level->kind == sk_class)
18632 : 0 : pop_nested_class ();
18633 : : else
18634 : 4 : poplevel (0, 0, 0);
18635 : : }
18636 : : }
18637 : 128402064 : poplevel (1, 0, 1);
18638 : :
18639 : : /* Statements should always be full-expressions at the outermost set
18640 : : of curly braces for a function. */
18641 : 128402064 : gcc_assert (stmts_are_full_exprs_p ());
18642 : :
18643 : : /* If there are no return statements in a function with auto return type,
18644 : : the return type is void. But if the declared type is something like
18645 : : auto*, this is an error. */
18646 : 50548125 : if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
18647 : 128945933 : && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
18648 : : {
18649 : 163869 : if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
18650 : 163854 : && !current_function_returns_value
18651 : 163869 : && !current_function_returns_null)
18652 : : {
18653 : : /* We haven't applied return type deduction because we haven't
18654 : : seen any return statements. Do that now. */
18655 : 163712 : tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
18656 : 163712 : do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
18657 : : void_node, node, tf_warning_or_error,
18658 : : adc_return_type);
18659 : :
18660 : 163712 : apply_deduced_return_type (fndecl, void_type_node);
18661 : 163712 : fntype = TREE_TYPE (fndecl);
18662 : : }
18663 : 157 : else if (!current_function_returns_value
18664 : 157 : && !current_function_returns_null)
18665 : : {
18666 : 12 : error ("no return statements in function returning %qT",
18667 : 6 : DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
18668 : 6 : inform (input_location, "only plain %<auto%> return type can be "
18669 : : "deduced to %<void%>");
18670 : : }
18671 : : }
18672 : :
18673 : : /* Remember that we were in class scope. */
18674 : 128402064 : if (current_class_name)
18675 : 94663730 : ctype = current_class_type;
18676 : :
18677 : 128402064 : if (DECL_DELETED_FN (fndecl))
18678 : : {
18679 : 27 : DECL_INITIAL (fndecl) = error_mark_node;
18680 : 27 : DECL_SAVED_TREE (fndecl) = NULL_TREE;
18681 : 27 : goto cleanup;
18682 : : }
18683 : :
18684 : : // If this is a concept, check that the definition is reasonable.
18685 : 128402037 : if (DECL_DECLARED_CONCEPT_P (fndecl))
18686 : 129 : check_function_concept (fndecl);
18687 : :
18688 : 128402037 : if (flag_openmp)
18689 : 73021 : if (tree attr = lookup_attribute ("omp declare variant base",
18690 : 73021 : DECL_ATTRIBUTES (fndecl)))
18691 : 114 : omp_declare_variant_finalize (fndecl, attr);
18692 : :
18693 : : /* Complain if there's just no return statement. */
18694 : 128402037 : if ((warn_return_type
18695 : 43824 : || (cxx_dialect >= cxx14
18696 : 36374 : && DECL_DECLARED_CONSTEXPR_P (fndecl)))
18697 : 128366342 : && !VOID_TYPE_P (TREE_TYPE (fntype))
18698 : 78914905 : && !dependent_type_p (TREE_TYPE (fntype))
18699 : 40605318 : && !current_function_returns_value && !current_function_returns_null
18700 : : /* Don't complain if we abort or throw. */
18701 : : && !current_function_returns_abnormally
18702 : : /* Don't complain if there's an infinite loop. */
18703 : 40605318 : && !current_function_infinite_loop
18704 : : /* Don't complain if we are declared noreturn. */
18705 : 863 : && !TREE_THIS_VOLATILE (fndecl)
18706 : 855 : && !DECL_NAME (DECL_RESULT (fndecl))
18707 : 855 : && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
18708 : : /* Structor return values (if any) are set by the compiler. */
18709 : 1392 : && !DECL_CONSTRUCTOR_P (fndecl)
18710 : 696 : && !DECL_DESTRUCTOR_P (fndecl)
18711 : 128402733 : && targetm.warn_func_return (fndecl))
18712 : : {
18713 : 688 : gcc_rich_location richloc (input_location);
18714 : : /* Potentially add a "return *this;" fix-it hint for
18715 : : assignment operators. */
18716 : 1270 : if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
18717 : : {
18718 : 49 : tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
18719 : 49 : if (TREE_CODE (valtype) == REFERENCE_TYPE
18720 : 40 : && current_class_ref
18721 : 32 : && same_type_ignoring_top_level_qualifiers_p
18722 : 32 : (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
18723 : 73 : && global_dc->option_enabled_p (OPT_Wreturn_type))
18724 : 24 : add_return_star_this_fixit (&richloc, fndecl);
18725 : : }
18726 : 688 : if (cxx_dialect >= cxx14
18727 : 688 : && DECL_DECLARED_CONSTEXPR_P (fndecl))
18728 : 2 : error_at (&richloc, "no return statement in %<constexpr%> function "
18729 : : "returning non-void");
18730 : 686 : else if (warning_at (&richloc, OPT_Wreturn_type,
18731 : : "no return statement in function returning "
18732 : : "non-void"))
18733 : 209 : suppress_warning (fndecl, OPT_Wreturn_type);
18734 : 688 : }
18735 : :
18736 : : /* Lambda closure members are implicitly constexpr if possible. */
18737 : 128402037 : if (cxx_dialect >= cxx17
18738 : 219215501 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
18739 : 851708 : DECL_DECLARED_CONSTEXPR_P (fndecl)
18740 : 851708 : = ((processing_template_decl
18741 : 388816 : || is_valid_constexpr_fn (fndecl, /*complain*/false))
18742 : 1702900 : && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
18743 : :
18744 : : /* Save constexpr function body before it gets munged by
18745 : : the NRV transformation. */
18746 : 128402037 : maybe_save_constexpr_fundef (fndecl);
18747 : :
18748 : : /* Invoke the pre-genericize plugin before we start munging things. */
18749 : 128402037 : if (!processing_template_decl)
18750 : 50548098 : invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
18751 : :
18752 : : /* Perform delayed folding before NRV transformation. */
18753 : 128402037 : if (!processing_template_decl
18754 : 101096196 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18755 : 178849237 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18756 : 50446680 : cp_fold_function (fndecl);
18757 : :
18758 : : /* Set up the named return value optimization, if we can. Candidate
18759 : : variables are selected in check_return_expr. */
18760 : 128402037 : if (tree r = current_function_return_value)
18761 : : {
18762 : 31372407 : if (r != error_mark_node)
18763 : 159043 : finalize_nrv (fndecl, r);
18764 : 31372407 : current_function_return_value = NULL_TREE;
18765 : : }
18766 : :
18767 : : /* Must mark the RESULT_DECL as being in this function. */
18768 : 128402037 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
18769 : :
18770 : : /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18771 : : to the FUNCTION_DECL node itself. */
18772 : 128402037 : BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
18773 : :
18774 : : /* Store the end of the function, so that we get good line number
18775 : : info for the epilogue. */
18776 : 128402037 : cfun->function_end_locus = input_location;
18777 : :
18778 : : /* Complain about parameters that are only set, but never otherwise used. */
18779 : 128402037 : if (warn_unused_but_set_parameter
18780 : 865299 : && !processing_template_decl
18781 : 437731 : && errorcount == unused_but_set_errorcount
18782 : 128839768 : && !DECL_CLONED_FUNCTION_P (fndecl))
18783 : : {
18784 : 342171 : tree decl;
18785 : :
18786 : 342171 : for (decl = DECL_ARGUMENTS (fndecl);
18787 : 925540 : decl;
18788 : 583369 : decl = DECL_CHAIN (decl))
18789 : 583369 : if (TREE_USED (decl)
18790 : 583369 : && TREE_CODE (decl) == PARM_DECL
18791 : 470429 : && !DECL_READ_P (decl)
18792 : 8 : && DECL_NAME (decl)
18793 : 8 : && !DECL_ARTIFICIAL (decl)
18794 : 8 : && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18795 : 8 : && !DECL_IN_SYSTEM_HEADER (decl)
18796 : 8 : && TREE_TYPE (decl) != error_mark_node
18797 : 8 : && !TYPE_REF_P (TREE_TYPE (decl))
18798 : 583377 : && (!CLASS_TYPE_P (TREE_TYPE (decl))
18799 : 0 : || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18800 : 8 : warning_at (DECL_SOURCE_LOCATION (decl),
18801 : : OPT_Wunused_but_set_parameter,
18802 : : "parameter %qD set but not used", decl);
18803 : 342171 : unused_but_set_errorcount = errorcount;
18804 : : }
18805 : :
18806 : : /* Complain about locally defined typedefs that are not used in this
18807 : : function. */
18808 : 128402037 : maybe_warn_unused_local_typedefs ();
18809 : :
18810 : : /* Possibly warn about unused parameters. */
18811 : 128402037 : if (warn_unused_parameter
18812 : 604898 : && !processing_template_decl
18813 : 128687831 : && !DECL_CLONED_FUNCTION_P (fndecl))
18814 : 218570 : do_warn_unused_parameter (fndecl);
18815 : :
18816 : : /* Genericize before inlining. */
18817 : 128402037 : if (!processing_template_decl
18818 : 101096196 : && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18819 : 178849187 : && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18820 : 50446630 : cp_genericize (fndecl);
18821 : :
18822 : : /* If this function can't throw any exceptions, remember that. */
18823 : 128402037 : if (!processing_template_decl
18824 : 50548098 : && !cp_function_chain->can_throw
18825 : 38267380 : && !flag_non_call_exceptions
18826 : 166664226 : && !decl_replaceable_p (fndecl,
18827 : 38262189 : opt_for_fn (fndecl, flag_semantic_interposition)))
18828 : 38024368 : TREE_NOTHROW (fndecl) = 1;
18829 : :
18830 : : /* Emit the resumer and destroyer functions now, providing that we have
18831 : : not encountered some fatal error. */
18832 : 128402037 : if (coro_emit_helpers)
18833 : : {
18834 : 1215 : emit_coro_helper (resumer);
18835 : 1215 : emit_coro_helper (destroyer);
18836 : : }
18837 : :
18838 : 128400822 : cleanup:
18839 : : /* We're leaving the context of this function, so zap cfun. It's still in
18840 : : DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18841 : 128402064 : set_cfun (NULL);
18842 : 128402064 : current_function_decl = NULL;
18843 : :
18844 : : /* If this is an in-class inline definition, we may have to pop the
18845 : : bindings for the template parameters that we added in
18846 : : maybe_begin_member_template_processing when start_function was
18847 : : called. */
18848 : 128402064 : if (inline_p)
18849 : 62371793 : maybe_end_member_template_processing ();
18850 : :
18851 : : /* Leave the scope of the class. */
18852 : 128402064 : if (ctype)
18853 : 94663730 : pop_nested_class ();
18854 : :
18855 : 128402064 : --function_depth;
18856 : :
18857 : : /* Clean up. */
18858 : 128402064 : current_function_decl = NULL_TREE;
18859 : :
18860 : 128402064 : invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18861 : :
18862 : 128402064 : finish_function_contracts (fndecl);
18863 : :
18864 : 128402064 : return fndecl;
18865 : : }
18866 : :
18867 : : /* Create the FUNCTION_DECL for a function definition.
18868 : : DECLSPECS and DECLARATOR are the parts of the declaration;
18869 : : they describe the return type and the name of the function,
18870 : : but twisted together in a fashion that parallels the syntax of C.
18871 : :
18872 : : This function creates a binding context for the function body
18873 : : as well as setting up the FUNCTION_DECL in current_function_decl.
18874 : :
18875 : : Returns a FUNCTION_DECL on success.
18876 : :
18877 : : If the DECLARATOR is not suitable for a function (it defines a datum
18878 : : instead), we return 0, which tells yyparse to report a parse error.
18879 : :
18880 : : May return void_type_node indicating that this method is actually
18881 : : a friend. See grokfield for more details.
18882 : :
18883 : : Came here with a `.pushlevel' .
18884 : :
18885 : : DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18886 : : CHANGES TO CODE IN `grokfield'. */
18887 : :
18888 : : tree
18889 : 62160548 : grokmethod (cp_decl_specifier_seq *declspecs,
18890 : : const cp_declarator *declarator, tree attrlist)
18891 : : {
18892 : 62160548 : tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18893 : 62160548 : &attrlist);
18894 : :
18895 : 62160548 : if (fndecl == error_mark_node)
18896 : : return error_mark_node;
18897 : :
18898 : 62160439 : if (attrlist)
18899 : 0 : cplus_decl_attributes (&fndecl, attrlist, 0);
18900 : :
18901 : : /* Pass friends other than inline friend functions back. */
18902 : 62160439 : if (fndecl == void_type_node)
18903 : : return fndecl;
18904 : :
18905 : 62160439 : if (DECL_IN_AGGR_P (fndecl))
18906 : : {
18907 : 0 : if (DECL_CLASS_SCOPE_P (fndecl))
18908 : 0 : error ("%qD is already defined in class %qT", fndecl,
18909 : 0 : DECL_CONTEXT (fndecl));
18910 : 0 : return error_mark_node;
18911 : : }
18912 : :
18913 : 62160439 : check_template_shadow (fndecl);
18914 : :
18915 : : /* p1779 ABI-Isolation makes inline not a default for in-class
18916 : : definitions attached to a named module. If the user explicitly
18917 : : made it inline, grokdeclarator will already have done the right
18918 : : things. */
18919 : 62160439 : if ((!named_module_attach_p ()
18920 : 318 : || flag_module_implicit_inline
18921 : : /* Lambda's operator function remains inline. */
18922 : 621 : || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18923 : : /* If the user explicitly asked for this to be inline, we don't
18924 : : need to do more, but more importantly we want to warn if we
18925 : : can't inline it. */
18926 : 62160145 : && !DECL_DECLARED_INLINE_P (fndecl))
18927 : : {
18928 : 46633465 : if (TREE_PUBLIC (fndecl))
18929 : 45803313 : DECL_COMDAT (fndecl) = 1;
18930 : 46633465 : DECL_DECLARED_INLINE_P (fndecl) = 1;
18931 : : /* It's ok if we can't inline this. */
18932 : 46633465 : DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18933 : : }
18934 : :
18935 : : /* We process method specializations in finish_struct_1. */
18936 : 62160439 : if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18937 : : {
18938 : : /* Avoid calling decl_spec_seq... until we have to. */
18939 : 51039224 : bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18940 : 51039224 : fndecl = push_template_decl (fndecl, friendp);
18941 : 51039224 : if (fndecl == error_mark_node)
18942 : : return fndecl;
18943 : : }
18944 : :
18945 : 62160430 : if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18946 : : {
18947 : 0 : fndecl = copy_node (fndecl);
18948 : 0 : TREE_CHAIN (fndecl) = NULL_TREE;
18949 : : }
18950 : :
18951 : 62160430 : cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
18952 : :
18953 : 62160430 : DECL_IN_AGGR_P (fndecl) = 1;
18954 : 62160430 : return fndecl;
18955 : : }
18956 : :
18957 : :
18958 : : /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
18959 : : we can lay it out later, when and if its type becomes complete.
18960 : :
18961 : : Also handle constexpr variables where the initializer involves
18962 : : an unlowered PTRMEM_CST because the class isn't complete yet. */
18963 : :
18964 : : void
18965 : 78868096 : maybe_register_incomplete_var (tree var)
18966 : : {
18967 : 78868096 : gcc_assert (VAR_P (var));
18968 : :
18969 : : /* Keep track of variables with incomplete types. */
18970 : 34890458 : if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
18971 : 113756789 : && DECL_EXTERNAL (var))
18972 : : {
18973 : 5259031 : tree inner_type = TREE_TYPE (var);
18974 : :
18975 : 5407959 : while (TREE_CODE (inner_type) == ARRAY_TYPE)
18976 : 148928 : inner_type = TREE_TYPE (inner_type);
18977 : 5259031 : inner_type = TYPE_MAIN_VARIANT (inner_type);
18978 : :
18979 : 5292342 : if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
18980 : : /* RTTI TD entries are created while defining the type_info. */
18981 : 5259031 : || (TYPE_LANG_SPECIFIC (inner_type)
18982 : 2097126 : && TYPE_BEING_DEFINED (inner_type)))
18983 : : {
18984 : 33311 : incomplete_var iv = {var, inner_type};
18985 : 33311 : vec_safe_push (incomplete_vars, iv);
18986 : : }
18987 : 10153932 : else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
18988 : 3241246 : && DECL_CLASS_SCOPE_P (var)
18989 : 1034938 : && TYPE_BEING_DEFINED (DECL_CONTEXT (var))
18990 : 1034938 : && decl_constant_var_p (var)
18991 : 5907567 : && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
18992 : : {
18993 : : /* When the outermost open class is complete we can resolve any
18994 : : pointers-to-members. */
18995 : 13 : tree context = outermost_open_class ();
18996 : 13 : incomplete_var iv = {var, context};
18997 : 13 : vec_safe_push (incomplete_vars, iv);
18998 : : }
18999 : : }
19000 : 78868096 : }
19001 : :
19002 : : /* Called when a class type (given by TYPE) is defined. If there are
19003 : : any existing VAR_DECLs whose type has been completed by this
19004 : : declaration, update them now. */
19005 : :
19006 : : void
19007 : 51866204 : complete_vars (tree type)
19008 : : {
19009 : 51866204 : unsigned ix;
19010 : 51866204 : incomplete_var *iv;
19011 : :
19012 : 53728126 : for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
19013 : : {
19014 : 1861922 : if (same_type_p (type, iv->incomplete_type))
19015 : : {
19016 : 31662 : tree var = iv->decl;
19017 : 31662 : tree type = TREE_TYPE (var);
19018 : :
19019 : 31662 : if (type != error_mark_node
19020 : 31662 : && (TYPE_MAIN_VARIANT (strip_array_types (type))
19021 : 31658 : == iv->incomplete_type))
19022 : : {
19023 : : /* Complete the type of the variable. */
19024 : 31645 : complete_type (type);
19025 : 31645 : cp_apply_type_quals_to_decl (cp_type_quals (type), var);
19026 : 31645 : if (COMPLETE_TYPE_P (type))
19027 : 31625 : layout_var_decl (var);
19028 : : }
19029 : :
19030 : : /* Remove this entry from the list. */
19031 : 31662 : incomplete_vars->unordered_remove (ix);
19032 : : }
19033 : : else
19034 : 1830260 : ix++;
19035 : : }
19036 : 51866204 : }
19037 : :
19038 : : /* If DECL is of a type which needs a cleanup, build and return an
19039 : : expression to perform that cleanup here. Return NULL_TREE if no
19040 : : cleanup need be done. DECL can also be a _REF when called from
19041 : : split_nonconstant_init_1. */
19042 : :
19043 : : tree
19044 : 53968751 : cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
19045 : : {
19046 : 53968751 : tree type;
19047 : 53968751 : tree attr;
19048 : 53968751 : tree cleanup;
19049 : :
19050 : : /* Assume no cleanup is required. */
19051 : 53968751 : cleanup = NULL_TREE;
19052 : :
19053 : 53968751 : if (error_operand_p (decl))
19054 : : return cleanup;
19055 : :
19056 : : /* Handle "__attribute__((cleanup))". We run the cleanup function
19057 : : before the destructor since the destructor is what actually
19058 : : terminates the lifetime of the object. */
19059 : 53968751 : if (DECL_P (decl))
19060 : 53681117 : attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
19061 : : else
19062 : : attr = NULL_TREE;
19063 : 53681117 : if (attr)
19064 : : {
19065 : 190 : tree id;
19066 : 190 : tree fn;
19067 : 190 : tree arg;
19068 : :
19069 : : /* Get the name specified by the user for the cleanup function. */
19070 : 190 : id = TREE_VALUE (TREE_VALUE (attr));
19071 : : /* Look up the name to find the cleanup function to call. It is
19072 : : important to use lookup_name here because that is what is
19073 : : used in c-common.cc:handle_cleanup_attribute when performing
19074 : : initial checks on the attribute. Note that those checks
19075 : : include ensuring that the function found is not an overloaded
19076 : : function, or an object with an overloaded call operator,
19077 : : etc.; we can rely on the fact that the function found is an
19078 : : ordinary FUNCTION_DECL. */
19079 : 190 : fn = lookup_name (id);
19080 : 190 : arg = build_address (decl);
19081 : 190 : if (!mark_used (decl, complain) && !(complain & tf_error))
19082 : 0 : return error_mark_node;
19083 : 190 : cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
19084 : 190 : if (cleanup == error_mark_node)
19085 : : return error_mark_node;
19086 : : }
19087 : : /* Handle ordinary C++ destructors. */
19088 : 53968743 : type = TREE_TYPE (decl);
19089 : 53968743 : if (type_build_dtor_call (type))
19090 : : {
19091 : 4899265 : int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
19092 : 4899265 : tree addr;
19093 : 4899265 : tree call;
19094 : :
19095 : 4899265 : if (TREE_CODE (type) == ARRAY_TYPE)
19096 : : addr = decl;
19097 : : else
19098 : 4897611 : addr = build_address (decl);
19099 : :
19100 : 4899265 : call = build_delete (input_location, TREE_TYPE (addr), addr,
19101 : : sfk_complete_destructor, flags, 0, complain);
19102 : 4899265 : if (call == error_mark_node)
19103 : : cleanup = error_mark_node;
19104 : 4899229 : else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
19105 : : /* Discard the call. */;
19106 : 3523300 : else if (decl_maybe_constant_destruction (decl, type)
19107 : 3523360 : && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
19108 : 60 : cxx_constant_dtor (call, decl);
19109 : 3523240 : else if (cleanup)
19110 : 4 : cleanup = cp_build_compound_expr (cleanup, call, complain);
19111 : : else
19112 : : cleanup = call;
19113 : : }
19114 : :
19115 : : /* build_delete sets the location of the destructor call to the
19116 : : current location, even though the destructor is going to be
19117 : : called later, at the end of the current scope. This can lead to
19118 : : a "jumpy" behavior for users of debuggers when they step around
19119 : : the end of the block. So let's unset the location of the
19120 : : destructor call instead. */
19121 : 53968743 : protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
19122 : 53968743 : if (cleanup && CONVERT_EXPR_P (cleanup))
19123 : 0 : protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
19124 : :
19125 : 0 : if (cleanup
19126 : 3523454 : && DECL_P (decl)
19127 : 3522276 : && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
19128 : : /* Treat objects with destructors as used; the destructor may do
19129 : : something substantive. */
19130 : 3522268 : && !mark_used (decl, complain) && !(complain & tf_error))
19131 : 0 : return error_mark_node;
19132 : :
19133 : 3523454 : if (cleanup && cfun && !processing_template_decl
19134 : 57085631 : && !expr_noexcept_p (cleanup, tf_none))
19135 : 4294 : cp_function_chain->throwing_cleanup = true;
19136 : :
19137 : : return cleanup;
19138 : : }
19139 : :
19140 : :
19141 : : /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
19142 : : FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
19143 : : METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
19144 : :
19145 : : tree
19146 : 2047156 : static_fn_type (tree memfntype)
19147 : : {
19148 : 2047156 : tree fntype;
19149 : 2047156 : tree args;
19150 : :
19151 : 2047156 : if (TYPE_PTRMEMFUNC_P (memfntype))
19152 : 20387 : memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
19153 : 2047156 : if (INDIRECT_TYPE_P (memfntype)
19154 : 1797436 : || TREE_CODE (memfntype) == FUNCTION_DECL)
19155 : 1879152 : memfntype = TREE_TYPE (memfntype);
19156 : 2047156 : if (TREE_CODE (memfntype) == FUNCTION_TYPE)
19157 : : return memfntype;
19158 : 473865 : gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
19159 : 473865 : args = TYPE_ARG_TYPES (memfntype);
19160 : 473865 : fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
19161 : 473865 : fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
19162 : 473865 : fntype = (cp_build_type_attribute_variant
19163 : 473865 : (fntype, TYPE_ATTRIBUTES (memfntype)));
19164 : 473865 : fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
19165 : 473865 : return fntype;
19166 : : }
19167 : :
19168 : : /* DECL was originally constructed as a non-static member function,
19169 : : but turned out to be static. Update it accordingly. */
19170 : :
19171 : : void
19172 : 144332 : revert_static_member_fn (tree decl)
19173 : : {
19174 : 144332 : tree stype = static_fn_type (decl);
19175 : 144332 : cp_cv_quals quals = type_memfn_quals (stype);
19176 : 144332 : cp_ref_qualifier rqual = type_memfn_rqual (stype);
19177 : :
19178 : 144332 : if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
19179 : 7 : stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
19180 : :
19181 : 144332 : TREE_TYPE (decl) = stype;
19182 : :
19183 : 144332 : if (DECL_ARGUMENTS (decl))
19184 : 144332 : DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
19185 : 144332 : DECL_STATIC_FUNCTION_P (decl) = 1;
19186 : 144332 : }
19187 : :
19188 : : /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
19189 : : one of the language-independent trees. */
19190 : :
19191 : : enum cp_tree_node_structure_enum
19192 : 30673455516 : cp_tree_node_structure (union lang_tree_node * t)
19193 : : {
19194 : 30673455516 : switch (TREE_CODE (&t->generic))
19195 : : {
19196 : : case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
19197 : 133097904 : case BASELINK: return TS_CP_BASELINK;
19198 : 22436533 : case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
19199 : 9481448 : case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
19200 : 3265532 : case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
19201 : 1807653179 : case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
19202 : 1650196 : case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
19203 : 379617 : case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
19204 : 581461281 : case OVERLOAD: return TS_CP_OVERLOAD;
19205 : 1013 : case PTRMEM_CST: return TS_CP_PTRMEM;
19206 : 13125204 : case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
19207 : 580879575 : case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
19208 : 1328367064 : case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
19209 : 580142366 : case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
19210 : 11738890 : case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
19211 : 2390659 : case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
19212 : 25597385055 : default: return TS_CP_GENERIC;
19213 : : }
19214 : : }
19215 : :
19216 : : bool
19217 : 12401 : cp_missing_noreturn_ok_p (tree decl)
19218 : : {
19219 : : /* A missing noreturn is ok for the `main' function. */
19220 : 12401 : return DECL_MAIN_P (decl);
19221 : : }
19222 : :
19223 : : /* Return the decl used to identify the COMDAT group into which DECL should
19224 : : be placed. */
19225 : :
19226 : : tree
19227 : 68380881 : cxx_comdat_group (tree decl)
19228 : : {
19229 : : /* Virtual tables, construction virtual tables, and virtual table
19230 : : tables all go in a single COMDAT group, named after the primary
19231 : : virtual table. */
19232 : 68380881 : if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
19233 : 396502 : decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
19234 : : /* For all other DECLs, the COMDAT group is the mangled name of the
19235 : : declaration itself. */
19236 : : else
19237 : : {
19238 : 67984379 : while (DECL_THUNK_P (decl))
19239 : : {
19240 : : /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
19241 : : into the same section as the target function. In that case
19242 : : we must return target's name. */
19243 : 0 : tree target = THUNK_TARGET (decl);
19244 : 0 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
19245 : 0 : && DECL_SECTION_NAME (target) != NULL
19246 : 0 : && DECL_ONE_ONLY (target))
19247 : : decl = target;
19248 : : else
19249 : : break;
19250 : : }
19251 : : }
19252 : :
19253 : 68380881 : return decl;
19254 : : }
19255 : :
19256 : : /* Returns the return type for FN as written by the user, which may include
19257 : : a placeholder for a deduced return type. */
19258 : :
19259 : : tree
19260 : 436128399 : fndecl_declared_return_type (tree fn)
19261 : : {
19262 : 436128399 : fn = STRIP_TEMPLATE (fn);
19263 : 436128399 : if (FNDECL_USED_AUTO (fn))
19264 : 3823 : return DECL_SAVED_AUTO_RETURN_TYPE (fn);
19265 : :
19266 : 436124576 : return TREE_TYPE (TREE_TYPE (fn));
19267 : : }
19268 : :
19269 : : /* Returns true iff DECL is a variable or function declared with an auto type
19270 : : that has not yet been deduced to a real type. */
19271 : :
19272 : : bool
19273 : 550292530 : undeduced_auto_decl (tree decl)
19274 : : {
19275 : 550292530 : if (cxx_dialect < cxx11)
19276 : : return false;
19277 : 548211526 : STRIP_ANY_LOCATION_WRAPPER (decl);
19278 : 387176238 : return ((VAR_OR_FUNCTION_DECL_P (decl)
19279 : 211852797 : || TREE_CODE (decl) == TEMPLATE_DECL)
19280 : 723575385 : && type_uses_auto (TREE_TYPE (decl)));
19281 : : }
19282 : :
19283 : : /* Complain if DECL has an undeduced return type. */
19284 : :
19285 : : bool
19286 : 319771848 : require_deduced_type (tree decl, tsubst_flags_t complain)
19287 : : {
19288 : 319771848 : if (undeduced_auto_decl (decl))
19289 : : {
19290 : 2098 : if (TREE_CODE (decl) == FUNCTION_DECL
19291 : 2072 : && fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
19292 : 4034 : && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION)
19293 : : {
19294 : : /* Set the return type of __builtin_source_location. */
19295 : 1936 : tree type = get_source_location_impl_type ();
19296 : 1936 : if (type == error_mark_node)
19297 : : {
19298 : 11 : inform (input_location, "using %qs", "__builtin_source_location");
19299 : 11 : return false;
19300 : : }
19301 : 1925 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
19302 : 1925 : type = build_pointer_type (type);
19303 : 1925 : apply_deduced_return_type (decl, type);
19304 : 1925 : return true;
19305 : : }
19306 : :
19307 : 162 : if (warning_suppressed_p (decl) && seen_error ())
19308 : : /* We probably already complained about deduction failure. */;
19309 : 65 : else if (complain & tf_error)
19310 : 52 : error ("use of %qD before deduction of %<auto%>", decl);
19311 : 162 : note_failed_type_completion_for_satisfaction (decl);
19312 : 162 : return false;
19313 : : }
19314 : : return true;
19315 : : }
19316 : :
19317 : : /* Create a representation of the explicit-specifier with
19318 : : constant-expression of EXPR. COMPLAIN is as for tsubst. */
19319 : :
19320 : : tree
19321 : 5465227 : build_explicit_specifier (tree expr, tsubst_flags_t complain)
19322 : : {
19323 : 5465227 : if (check_for_bare_parameter_packs (expr))
19324 : 1 : return error_mark_node;
19325 : :
19326 : 5465226 : if (instantiation_dependent_expression_p (expr))
19327 : : /* Wait for instantiation, tsubst_function_decl will handle it. */
19328 : : return expr;
19329 : :
19330 : 5044113 : expr = build_converted_constant_bool_expr (expr, complain);
19331 : 5044113 : expr = instantiate_non_dependent_expr (expr, complain);
19332 : 5044113 : expr = cxx_constant_value (expr, complain);
19333 : 5044113 : return expr;
19334 : : }
19335 : :
19336 : : #include "gt-cp-decl.h"
|