1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2022 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "c-family/c-target.h"
34 #include "cp-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "flags.h"
42 #include "tree-iterator.h"
43 #include "decl.h"
44 #include "intl.h"
45 #include "toplev.h"
46 #include "c-family/c-objc.h"
47 #include "c-family/c-pragma.h"
48 #include "c-family/c-ubsan.h"
49 #include "debug.h"
50 #include "plugin.h"
51 #include "builtins.h"
52 #include "gimplify.h"
53 #include "asan.h"
54 #include "gcc-rich-location.h"
55 #include "langhooks.h"
56 #include "context.h" /* For 'g'. */
57 #include "omp-general.h"
58 #include "omp-offload.h" /* For offload_vars. */
59 #include "opts.h"
60 #include "langhooks-def.h" /* For lhd_simulate_record_decl */
61
62 /* Possible cases of bad specifiers type used by bad_specifiers. */
63 enum bad_spec_place {
64 BSP_VAR, /* variable */
65 BSP_PARM, /* parameter */
66 BSP_TYPE, /* type */
67 BSP_FIELD /* field */
68 };
69
70 static const char *redeclaration_error_message (tree, tree);
71
72 static int decl_jump_unsafe (tree);
73 static void require_complete_types_for_parms (tree);
74 static tree grok_reference_init (tree, tree, tree, int);
75 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
76 int, int, int, bool, int, tree, location_t);
77 static void check_static_variable_definition (tree, tree);
78 static void record_unknown_type (tree, const char *);
79 static int member_function_or_else (tree, tree, enum overload_flags);
80 static tree local_variable_p_walkfn (tree *, int *, void *);
81 static const char *tag_name (enum tag_types);
82 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
86 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
87 static void copy_type_enum (tree , tree);
88 static void check_function_type (tree, tree);
89 static void finish_constructor_body (void);
90 static void begin_destructor_body (void);
91 static void finish_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
109 /* The following symbols are subsumed in the cp_global_trees array, and
110 listed here individually for documentation purposes.
111
112 C++ extensions
113 tree wchar_decl_node;
114
115 tree vtable_entry_type;
116 tree delta_type_node;
117 tree __t_desc_type_node;
118
119 tree class_type_node;
120 tree unknown_type_node;
121
122 Array type `vtable_entry_type[]'
123
124 tree vtbl_type_node;
125 tree vtbl_ptr_type_node;
126
127 Namespaces,
128
129 tree std_node;
130 tree abi_node;
131
132 A FUNCTION_DECL which can call `abort'. Not necessarily the
133 one that the user will declare, but sufficient to be called
134 by routines that want to abort the program.
135
136 tree abort_fndecl;
137
138 Used by RTTI
139 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
140 tree tinfo_var_id; */
141
142 tree cp_global_trees[CPTI_MAX];
143
144 /* A list of objects which have constructors or destructors
145 which reside in namespace scope. The decl is stored in
146 the TREE_VALUE slot and the initializer is stored
147 in the TREE_PURPOSE slot. */
148 tree static_aggregates;
149
150 /* Like static_aggregates, but for thread_local variables. */
151 tree tls_aggregates;
152
153 /* A hash-map mapping from variable decls to the dynamic initializer for
154 the decl. This is currently only used by OpenMP. */
155 decl_tree_map *dynamic_initializers;
156
157 /* -- end of C++ */
158
159 /* A node for the integer constant 2. */
160
161 tree integer_two_node;
162
163 /* vector of static decls. */
164 vec<tree, va_gc> *static_decls;
165
166 /* vector of keyed classes. */
167 vec<tree, va_gc> *keyed_classes;
168
169 /* Used only for jumps to as-yet undefined labels, since jumps to
170 defined labels can have their validity checked immediately. */
171
172 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
173 struct named_label_use_entry *next;
174 /* The binding level to which this entry is *currently* attached.
175 This is initially the binding level in which the goto appeared,
176 but is modified as scopes are closed. */
177 cp_binding_level *binding_level;
178 /* The head of the names list that was current when the goto appeared,
179 or the inner scope popped. These are the decls that will *not* be
180 skipped when jumping to the label. */
181 tree names_in_scope;
182 /* The location of the goto, for error reporting. */
183 location_t o_goto_locus;
184 /* True if an OpenMP structured block scope has been closed since
185 the goto appeared. This means that the branch from the label will
186 illegally exit an OpenMP scope. */
187 bool in_omp_scope;
188 };
189
190 /* A list of all LABEL_DECLs in the function that have names. Here so
191 we can clear out their names' definitions at the end of the
192 function, and so we can check the validity of jumps to these labels. */
193
194 struct GTY((for_user)) named_label_entry {
195
196 tree name; /* Name of decl. */
197
198 tree label_decl; /* LABEL_DECL, unless deleted local label. */
199
200 named_label_entry *outer; /* Outer shadowed chain. */
201
202 /* The binding level to which the label is *currently* attached.
203 This is initially set to the binding level in which the label
204 is defined, but is modified as scopes are closed. */
205 cp_binding_level *binding_level;
206
207 /* The head of the names list that was current when the label was
208 defined, or the inner scope popped. These are the decls that will
209 be skipped when jumping to the label. */
210 tree names_in_scope;
211
212 /* A vector of all decls from all binding levels that would be
213 crossed by a backward branch to the label. */
214 vec<tree, va_gc> *bad_decls;
215
216 /* A list of uses of the label, before the label is defined. */
217 named_label_use_entry *uses;
218
219 /* The following bits are set after the label is defined, and are
220 updated as scopes are popped. They indicate that a jump to the
221 label will illegally enter a scope of the given flavor. */
222 bool in_try_scope;
223 bool in_catch_scope;
224 bool in_omp_scope;
225 bool in_transaction_scope;
226 bool in_constexpr_if;
227 bool in_consteval_if;
228 };
229
230 #define named_labels cp_function_chain->x_named_labels
231
232 /* The number of function bodies which we are currently processing.
233 (Zero if we are at namespace scope, one inside the body of a
234 function, two inside the body of a function in a local class, etc.) */
235 int function_depth;
236
237 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
238 bool flag_noexcept_type;
239
240 /* States indicating how grokdeclarator() should handle declspecs marked
241 with __attribute__((deprecated)). An object declared as
242 __attribute__((deprecated)) suppresses warnings of uses of other
243 deprecated items. */
244 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
245
246
247 /* A list of VAR_DECLs whose type was incomplete at the time the
248 variable was declared. */
249
250 struct GTY(()) incomplete_var {
251 tree decl;
252 tree incomplete_type;
253 };
254
255
256 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
257
258 /* Returns the kind of template specialization we are currently
259 processing, given that it's declaration contained N_CLASS_SCOPES
260 explicit scope qualifications. */
261
262 tmpl_spec_kind
current_tmpl_spec_kind(int n_class_scopes)263 current_tmpl_spec_kind (int n_class_scopes)
264 {
265 int n_template_parm_scopes = 0;
266 int seen_specialization_p = 0;
267 int innermost_specialization_p = 0;
268 cp_binding_level *b;
269
270 /* Scan through the template parameter scopes. */
271 for (b = current_binding_level;
272 b->kind == sk_template_parms;
273 b = b->level_chain)
274 {
275 /* If we see a specialization scope inside a parameter scope,
276 then something is wrong. That corresponds to a declaration
277 like:
278
279 template <class T> template <> ...
280
281 which is always invalid since [temp.expl.spec] forbids the
282 specialization of a class member template if the enclosing
283 class templates are not explicitly specialized as well. */
284 if (b->explicit_spec_p)
285 {
286 if (n_template_parm_scopes == 0)
287 innermost_specialization_p = 1;
288 else
289 seen_specialization_p = 1;
290 }
291 else if (seen_specialization_p == 1)
292 return tsk_invalid_member_spec;
293
294 ++n_template_parm_scopes;
295 }
296
297 /* Handle explicit instantiations. */
298 if (processing_explicit_instantiation)
299 {
300 if (n_template_parm_scopes != 0)
301 /* We've seen a template parameter list during an explicit
302 instantiation. For example:
303
304 template <class T> template void f(int);
305
306 This is erroneous. */
307 return tsk_invalid_expl_inst;
308 else
309 return tsk_expl_inst;
310 }
311
312 if (n_template_parm_scopes < n_class_scopes)
313 /* We've not seen enough template headers to match all the
314 specialized classes present. For example:
315
316 template <class T> void R<T>::S<T>::f(int);
317
318 This is invalid; there needs to be one set of template
319 parameters for each class. */
320 return tsk_insufficient_parms;
321 else if (n_template_parm_scopes == n_class_scopes)
322 /* We're processing a non-template declaration (even though it may
323 be a member of a template class.) For example:
324
325 template <class T> void S<T>::f(int);
326
327 The `class T' matches the `S<T>', leaving no template headers
328 corresponding to the `f'. */
329 return tsk_none;
330 else if (n_template_parm_scopes > n_class_scopes + 1)
331 /* We've got too many template headers. For example:
332
333 template <> template <class T> void f (T);
334
335 There need to be more enclosing classes. */
336 return tsk_excessive_parms;
337 else
338 /* This must be a template. It's of the form:
339
340 template <class T> template <class U> void S<T>::f(U);
341
342 This is a specialization if the innermost level was a
343 specialization; otherwise it's just a definition of the
344 template. */
345 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
346 }
347
348 /* Exit the current scope. */
349
350 void
finish_scope(void)351 finish_scope (void)
352 {
353 poplevel (0, 0, 0);
354 }
355
356 /* When a label goes out of scope, check to see if that label was used
357 in a valid manner, and issue any appropriate warnings or errors. */
358
359 static void
check_label_used(tree label)360 check_label_used (tree label)
361 {
362 if (!processing_template_decl)
363 {
364 if (DECL_INITIAL (label) == NULL_TREE)
365 {
366 location_t location;
367
368 error ("label %q+D used but not defined", label);
369 location = input_location;
370 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
371 /* Avoid crashing later. */
372 define_label (location, DECL_NAME (label));
373 }
374 else
375 warn_for_unused_label (label);
376 }
377 }
378
379 /* Helper function to sort named label entries in a vector by DECL_UID. */
380
381 static int
sort_labels(const void * a,const void * b)382 sort_labels (const void *a, const void *b)
383 {
384 tree label1 = *(tree const *) a;
385 tree label2 = *(tree const *) b;
386
387 /* DECL_UIDs can never be equal. */
388 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
389 }
390
391 /* At the end of a function, all labels declared within the function
392 go out of scope. BLOCK is the top-level block for the
393 function. */
394
395 static void
pop_labels(tree block)396 pop_labels (tree block)
397 {
398 if (!named_labels)
399 return;
400
401 /* We need to add the labels to the block chain, so debug
402 information is emitted. But, we want the order to be stable so
403 need to sort them first. Otherwise the debug output could be
404 randomly ordered. I guess it's mostly stable, unless the hash
405 table implementation changes. */
406 auto_vec<tree, 32> labels (named_labels->elements ());
407 hash_table<named_label_hash>::iterator end (named_labels->end ());
408 for (hash_table<named_label_hash>::iterator iter
409 (named_labels->begin ()); iter != end; ++iter)
410 {
411 named_label_entry *ent = *iter;
412
413 gcc_checking_assert (!ent->outer);
414 if (ent->label_decl)
415 labels.quick_push (ent->label_decl);
416 ggc_free (ent);
417 }
418 named_labels = NULL;
419 labels.qsort (sort_labels);
420
421 while (labels.length ())
422 {
423 tree label = labels.pop ();
424
425 DECL_CHAIN (label) = BLOCK_VARS (block);
426 BLOCK_VARS (block) = label;
427
428 check_label_used (label);
429 }
430 }
431
432 /* At the end of a block with local labels, restore the outer definition. */
433
434 static void
pop_local_label(tree id,tree label)435 pop_local_label (tree id, tree label)
436 {
437 check_label_used (label);
438 named_label_entry **slot = named_labels->find_slot_with_hash
439 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
440 named_label_entry *ent = *slot;
441
442 if (ent->outer)
443 ent = ent->outer;
444 else
445 {
446 ent = ggc_cleared_alloc<named_label_entry> ();
447 ent->name = id;
448 }
449 *slot = ent;
450 }
451
452 /* The following two routines are used to interface to Objective-C++.
453 The binding level is purposely treated as an opaque type. */
454
455 void *
objc_get_current_scope(void)456 objc_get_current_scope (void)
457 {
458 return current_binding_level;
459 }
460
461 /* The following routine is used by the NeXT-style SJLJ exceptions;
462 variables get marked 'volatile' so as to not be clobbered by
463 _setjmp()/_longjmp() calls. All variables in the current scope,
464 as well as parent scopes up to (but not including) ENCLOSING_BLK
465 shall be thusly marked. */
466
467 void
objc_mark_locals_volatile(void * enclosing_blk)468 objc_mark_locals_volatile (void *enclosing_blk)
469 {
470 cp_binding_level *scope;
471
472 for (scope = current_binding_level;
473 scope && scope != enclosing_blk;
474 scope = scope->level_chain)
475 {
476 tree decl;
477
478 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
479 objc_volatilize_decl (decl);
480
481 /* Do not climb up past the current function. */
482 if (scope->kind == sk_function_parms)
483 break;
484 }
485 }
486
487 /* True if B is the level for the condition of a constexpr if. */
488
489 static bool
level_for_constexpr_if(cp_binding_level * b)490 level_for_constexpr_if (cp_binding_level *b)
491 {
492 return (b->kind == sk_cond && b->this_entity
493 && TREE_CODE (b->this_entity) == IF_STMT
494 && IF_STMT_CONSTEXPR_P (b->this_entity));
495 }
496
497 /* True if B is the level for the condition of a consteval if. */
498
499 static bool
level_for_consteval_if(cp_binding_level * b)500 level_for_consteval_if (cp_binding_level *b)
501 {
502 return (b->kind == sk_cond && b->this_entity
503 && TREE_CODE (b->this_entity) == IF_STMT
504 && IF_STMT_CONSTEVAL_P (b->this_entity));
505 }
506
507 /* Update data for defined and undefined labels when leaving a scope. */
508
509 int
poplevel_named_label_1(named_label_entry ** slot,cp_binding_level * bl)510 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
511 {
512 named_label_entry *ent = *slot;
513 cp_binding_level *obl = bl->level_chain;
514
515 if (ent->binding_level == bl)
516 {
517 tree decl;
518
519 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
520 TREE_LISTs representing OVERLOADs, so be careful. */
521 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
522 ? DECL_CHAIN (decl)
523 : TREE_CHAIN (decl)))
524 if (decl_jump_unsafe (decl))
525 vec_safe_push (ent->bad_decls, decl);
526
527 ent->binding_level = obl;
528 ent->names_in_scope = obl->names;
529 switch (bl->kind)
530 {
531 case sk_try:
532 ent->in_try_scope = true;
533 break;
534 case sk_catch:
535 ent->in_catch_scope = true;
536 break;
537 case sk_omp:
538 ent->in_omp_scope = true;
539 break;
540 case sk_transaction:
541 ent->in_transaction_scope = true;
542 break;
543 case sk_block:
544 if (level_for_constexpr_if (bl->level_chain))
545 ent->in_constexpr_if = true;
546 else if (level_for_consteval_if (bl->level_chain))
547 ent->in_consteval_if = true;
548 break;
549 default:
550 break;
551 }
552 }
553 else if (ent->uses)
554 {
555 struct named_label_use_entry *use;
556
557 for (use = ent->uses; use ; use = use->next)
558 if (use->binding_level == bl)
559 {
560 use->binding_level = obl;
561 use->names_in_scope = obl->names;
562 if (bl->kind == sk_omp)
563 use->in_omp_scope = true;
564 }
565 }
566
567 return 1;
568 }
569
570 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
571 when errors were reported, except for -Werror-unused-but-set-*. */
572 static int unused_but_set_errorcount;
573
574 /* Exit a binding level.
575 Pop the level off, and restore the state of the identifier-decl mappings
576 that were in effect when this level was entered.
577
578 If KEEP == 1, this level had explicit declarations, so
579 and create a "block" (a BLOCK node) for the level
580 to record its declarations and subblocks for symbol table output.
581
582 If FUNCTIONBODY is nonzero, this level is the body of a function,
583 so create a block as if KEEP were set and also clear out all
584 label names.
585
586 If REVERSE is nonzero, reverse the order of decls before putting
587 them into the BLOCK. */
588
589 tree
poplevel(int keep,int reverse,int functionbody)590 poplevel (int keep, int reverse, int functionbody)
591 {
592 tree link;
593 /* The chain of decls was accumulated in reverse order.
594 Put it into forward order, just for cleanliness. */
595 tree decls;
596 tree subblocks;
597 tree block;
598 tree decl;
599 scope_kind kind;
600
601 auto_cond_timevar tv (TV_NAME_LOOKUP);
602 restart:
603
604 block = NULL_TREE;
605
606 gcc_assert (current_binding_level->kind != sk_class
607 && current_binding_level->kind != sk_namespace);
608
609 if (current_binding_level->kind == sk_cleanup)
610 functionbody = 0;
611 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
612
613 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
614
615 /* We used to use KEEP == 2 to indicate that the new block should go
616 at the beginning of the list of blocks at this binding level,
617 rather than the end. This hack is no longer used. */
618 gcc_assert (keep == 0 || keep == 1);
619
620 if (current_binding_level->keep)
621 keep = 1;
622
623 /* Any uses of undefined labels, and any defined labels, now operate
624 under constraints of next binding contour. */
625 if (cfun && !functionbody && named_labels)
626 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
627 (current_binding_level);
628
629 /* Get the decls in the order they were written.
630 Usually current_binding_level->names is in reverse order.
631 But parameter decls were previously put in forward order. */
632
633 decls = current_binding_level->names;
634 if (reverse)
635 {
636 decls = nreverse (decls);
637 current_binding_level->names = decls;
638 }
639
640 /* If there were any declarations or structure tags in that level,
641 or if this level is a function body,
642 create a BLOCK to record them for the life of this function. */
643 block = NULL_TREE;
644 /* Avoid function body block if possible. */
645 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
646 keep = 0;
647 else if (keep == 1 || functionbody)
648 block = make_node (BLOCK);
649 if (block != NULL_TREE)
650 {
651 BLOCK_VARS (block) = decls;
652 BLOCK_SUBBLOCKS (block) = subblocks;
653 }
654
655 /* In each subblock, record that this is its superior. */
656 if (keep >= 0)
657 for (link = subblocks; link; link = BLOCK_CHAIN (link))
658 BLOCK_SUPERCONTEXT (link) = block;
659
660 /* Before we remove the declarations first check for unused variables. */
661 if ((warn_unused_variable || warn_unused_but_set_variable)
662 && current_binding_level->kind != sk_template_parms
663 && !processing_template_decl)
664 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
665 {
666 /* There are cases where D itself is a TREE_LIST. See in
667 push_local_binding where the list of decls returned by
668 getdecls is built. */
669 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
670
671 tree type = TREE_TYPE (decl);
672 if (VAR_P (decl)
673 && (! TREE_USED (decl) || !DECL_READ_P (decl))
674 && ! DECL_IN_SYSTEM_HEADER (decl)
675 /* For structured bindings, consider only real variables, not
676 subobjects. */
677 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
678 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
679 && type != error_mark_node
680 && (!CLASS_TYPE_P (type)
681 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
682 || lookup_attribute ("warn_unused",
683 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
684 {
685 if (! TREE_USED (decl))
686 {
687 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
688 warning_at (DECL_SOURCE_LOCATION (decl),
689 OPT_Wunused_variable,
690 "unused structured binding declaration");
691 else
692 warning_at (DECL_SOURCE_LOCATION (decl),
693 OPT_Wunused_variable, "unused variable %qD", decl);
694 suppress_warning (decl, OPT_Wunused_variable);
695 }
696 else if (DECL_CONTEXT (decl) == current_function_decl
697 // For -Wunused-but-set-variable leave references alone.
698 && !TYPE_REF_P (TREE_TYPE (decl))
699 && errorcount == unused_but_set_errorcount)
700 {
701 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
702 warning_at (DECL_SOURCE_LOCATION (decl),
703 OPT_Wunused_but_set_variable, "structured "
704 "binding declaration set but not used");
705 else
706 warning_at (DECL_SOURCE_LOCATION (decl),
707 OPT_Wunused_but_set_variable,
708 "variable %qD set but not used", decl);
709 unused_but_set_errorcount = errorcount;
710 }
711 }
712 }
713
714 /* Remove declarations for all the DECLs in this level. */
715 for (link = decls; link; link = TREE_CHAIN (link))
716 {
717 tree name;
718 if (TREE_CODE (link) == TREE_LIST)
719 {
720 decl = TREE_VALUE (link);
721 name = TREE_PURPOSE (link);
722 gcc_checking_assert (name);
723 }
724 else
725 {
726 decl = link;
727 name = DECL_NAME (decl);
728 }
729
730 /* Remove the binding. */
731 if (TREE_CODE (decl) == LABEL_DECL)
732 pop_local_label (name, decl);
733 else
734 pop_local_binding (name, decl);
735 }
736
737 /* Restore the IDENTIFIER_TYPE_VALUEs. */
738 for (link = current_binding_level->type_shadowed;
739 link; link = TREE_CHAIN (link))
740 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
741
742 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
743 list if a `using' declaration put them there. The debugging
744 back ends won't understand OVERLOAD, so we remove them here.
745 Because the BLOCK_VARS are (temporarily) shared with
746 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
747 popped all the bindings. Also remove undeduced 'auto' decls,
748 which LTO doesn't understand, and can't have been used by anything. */
749 if (block)
750 {
751 tree* d;
752
753 for (d = &BLOCK_VARS (block); *d; )
754 {
755 if (TREE_CODE (*d) == TREE_LIST
756 || (!processing_template_decl
757 && undeduced_auto_decl (*d)))
758 *d = TREE_CHAIN (*d);
759 else
760 d = &DECL_CHAIN (*d);
761 }
762 }
763
764 /* If the level being exited is the top level of a function,
765 check over all the labels. */
766 if (functionbody)
767 {
768 if (block)
769 {
770 /* Since this is the top level block of a function, the vars are
771 the function's parameters. Don't leave them in the BLOCK
772 because they are found in the FUNCTION_DECL instead. */
773 BLOCK_VARS (block) = 0;
774 pop_labels (block);
775 }
776 else
777 pop_labels (subblocks);
778 }
779
780 kind = current_binding_level->kind;
781 if (kind == sk_cleanup)
782 {
783 tree stmt;
784
785 /* If this is a temporary binding created for a cleanup, then we'll
786 have pushed a statement list level. Pop that, create a new
787 BIND_EXPR for the block, and insert it into the stream. */
788 stmt = pop_stmt_list (current_binding_level->statement_list);
789 stmt = c_build_bind_expr (input_location, block, stmt);
790 add_stmt (stmt);
791 }
792
793 leave_scope ();
794 if (functionbody)
795 {
796 /* The current function is being defined, so its DECL_INITIAL
797 should be error_mark_node. */
798 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
799 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
800 if (subblocks)
801 {
802 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
803 {
804 if (BLOCK_SUBBLOCKS (subblocks))
805 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
806 }
807 else
808 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
809 }
810 }
811 else if (block)
812 current_binding_level->blocks
813 = block_chainon (current_binding_level->blocks, block);
814
815 /* If we did not make a block for the level just exited,
816 any blocks made for inner levels
817 (since they cannot be recorded as subblocks in that level)
818 must be carried forward so they will later become subblocks
819 of something else. */
820 else if (subblocks)
821 current_binding_level->blocks
822 = block_chainon (current_binding_level->blocks, subblocks);
823
824 /* Each and every BLOCK node created here in `poplevel' is important
825 (e.g. for proper debugging information) so if we created one
826 earlier, mark it as "used". */
827 if (block)
828 TREE_USED (block) = 1;
829
830 /* All temporary bindings created for cleanups are popped silently. */
831 if (kind == sk_cleanup)
832 goto restart;
833
834 return block;
835 }
836
837 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
838 /* Diagnose odr-used extern inline variables without definitions
839 in the current TU. */
840
841 int
wrapup_namespace_globals()842 wrapup_namespace_globals ()
843 {
844 if (vec<tree, va_gc> *statics = static_decls)
845 {
846 for (tree decl : *statics)
847 {
848 if (warn_unused_function
849 && TREE_CODE (decl) == FUNCTION_DECL
850 && DECL_INITIAL (decl) == 0
851 && DECL_EXTERNAL (decl)
852 && !TREE_PUBLIC (decl)
853 && !DECL_ARTIFICIAL (decl)
854 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
855 && !warning_suppressed_p (decl, OPT_Wunused_function))
856 warning_at (DECL_SOURCE_LOCATION (decl),
857 OPT_Wunused_function,
858 "%qF declared %<static%> but never defined", decl);
859
860 if (VAR_P (decl)
861 && DECL_EXTERNAL (decl)
862 && DECL_INLINE_VAR_P (decl)
863 && DECL_ODR_USED (decl))
864 error_at (DECL_SOURCE_LOCATION (decl),
865 "odr-used inline variable %qD is not defined", decl);
866 }
867
868 /* Clear out the list, so we don't rescan next time. */
869 static_decls = NULL;
870
871 /* Write out any globals that need to be output. */
872 return wrapup_global_declarations (statics->address (),
873 statics->length ());
874 }
875 return 0;
876 }
877
878 /* In C++, you don't have to write `struct S' to refer to `S'; you
879 can just use `S'. We accomplish this by creating a TYPE_DECL as
880 if the user had written `typedef struct S S'. Create and return
881 the TYPE_DECL for TYPE. */
882
883 tree
create_implicit_typedef(tree name,tree type)884 create_implicit_typedef (tree name, tree type)
885 {
886 tree decl;
887
888 decl = build_decl (input_location, TYPE_DECL, name, type);
889 DECL_ARTIFICIAL (decl) = 1;
890 /* There are other implicit type declarations, like the one *within*
891 a class that allows you to write `S::S'. We must distinguish
892 amongst these. */
893 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
894 TYPE_NAME (type) = decl;
895 TYPE_STUB_DECL (type) = decl;
896
897 return decl;
898 }
899
900 /* Function-scope local entities that need discriminators. Each entry
901 is a {decl,name} pair. VAR_DECLs for anon unions get their name
902 smashed, so we cannot rely on DECL_NAME. */
903
904 static GTY((deletable)) vec<tree, va_gc> *local_entities;
905
906 /* Determine the mangling discriminator of local DECL. There are
907 generally very few of these in any particular function. */
908
909 void
determine_local_discriminator(tree decl)910 determine_local_discriminator (tree decl)
911 {
912 auto_cond_timevar tv (TV_NAME_LOOKUP);
913 retrofit_lang_decl (decl);
914 tree ctx = DECL_CONTEXT (decl);
915 tree name = (TREE_CODE (decl) == TYPE_DECL
916 && TYPE_UNNAMED_P (TREE_TYPE (decl))
917 ? NULL_TREE : DECL_NAME (decl));
918 size_t nelts = vec_safe_length (local_entities);
919 for (size_t i = 0; i < nelts; i += 2)
920 {
921 tree *pair = &(*local_entities)[i];
922 tree d = pair[0];
923 tree n = pair[1];
924 gcc_checking_assert (d != decl);
925 if (name == n
926 && TREE_CODE (decl) == TREE_CODE (d)
927 && ctx == DECL_CONTEXT (d))
928 {
929 tree disc = integer_one_node;
930 if (DECL_DISCRIMINATOR (d))
931 disc = build_int_cst (TREE_TYPE (disc),
932 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
933 DECL_DISCRIMINATOR (decl) = disc;
934 /* Replace the saved decl. */
935 pair[0] = decl;
936 decl = NULL_TREE;
937 break;
938 }
939 }
940
941 if (decl)
942 {
943 vec_safe_reserve (local_entities, 2);
944 local_entities->quick_push (decl);
945 local_entities->quick_push (name);
946 }
947 }
948
949
950
951 /* Returns true if functions FN1 and FN2 have equivalent trailing
952 requires clauses. */
953
954 static bool
function_requirements_equivalent_p(tree newfn,tree oldfn)955 function_requirements_equivalent_p (tree newfn, tree oldfn)
956 {
957 /* In the concepts TS, the combined constraints are compared. */
958 if (cxx_dialect < cxx20)
959 {
960 tree ci1 = get_constraints (oldfn);
961 tree ci2 = get_constraints (newfn);
962 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
963 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
964 return cp_tree_equal (req1, req2);
965 }
966
967 /* Compare only trailing requirements. */
968 tree reqs1 = get_trailing_function_requirements (newfn);
969 tree reqs2 = get_trailing_function_requirements (oldfn);
970 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
971 return false;
972
973 /* Substitution is needed when friends are involved. */
974 reqs1 = maybe_substitute_reqs_for (reqs1, newfn);
975 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn);
976
977 return cp_tree_equal (reqs1, reqs2);
978 }
979
980 /* Subroutine of duplicate_decls: return truthvalue of whether
981 or not types of these decls match.
982
983 For C++, we must compare the parameter list so that `int' can match
984 `int&' in a parameter position, but `int&' is not confused with
985 `const int&'. */
986
987 int
decls_match(tree newdecl,tree olddecl,bool record_versions)988 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
989 {
990 int types_match;
991
992 if (newdecl == olddecl)
993 return 1;
994
995 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
996 /* If the two DECLs are not even the same kind of thing, we're not
997 interested in their types. */
998 return 0;
999
1000 gcc_assert (DECL_P (newdecl));
1001
1002 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1003 {
1004 /* Specializations of different templates are different functions
1005 even if they have the same type. */
1006 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1007 ? DECL_TI_TEMPLATE (newdecl)
1008 : NULL_TREE);
1009 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1010 ? DECL_TI_TEMPLATE (olddecl)
1011 : NULL_TREE);
1012 if (t1 != t2)
1013 return 0;
1014
1015 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1016 && ! (DECL_EXTERN_C_P (newdecl)
1017 && DECL_EXTERN_C_P (olddecl)))
1018 return 0;
1019
1020 /* A new declaration doesn't match a built-in one unless it
1021 is also extern "C". */
1022 if (DECL_IS_UNDECLARED_BUILTIN (olddecl)
1023 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1024 return 0;
1025
1026 tree f1 = TREE_TYPE (newdecl);
1027 tree f2 = TREE_TYPE (olddecl);
1028 if (TREE_CODE (f1) != TREE_CODE (f2))
1029 return 0;
1030
1031 /* A declaration with deduced return type should use its pre-deduction
1032 type for declaration matching. */
1033 tree r2 = fndecl_declared_return_type (olddecl);
1034 tree r1 = fndecl_declared_return_type (newdecl);
1035
1036 tree p1 = TYPE_ARG_TYPES (f1);
1037 tree p2 = TYPE_ARG_TYPES (f2);
1038
1039 if (same_type_p (r1, r2))
1040 {
1041 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1042 && fndecl_built_in_p (olddecl))
1043 {
1044 types_match = self_promoting_args_p (p1);
1045 if (p1 == void_list_node)
1046 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1047 }
1048 else
1049 types_match =
1050 compparms (p1, p2)
1051 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1052 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1053 || comp_type_attributes (TREE_TYPE (newdecl),
1054 TREE_TYPE (olddecl)) != 0);
1055 }
1056 else
1057 types_match = 0;
1058
1059 /* Two function declarations match if either has a requires-clause
1060 then both have a requires-clause and their constraints-expressions
1061 are equivalent. */
1062 if (types_match && flag_concepts)
1063 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1064
1065 /* The decls dont match if they correspond to two different versions
1066 of the same function. Disallow extern "C" functions to be
1067 versions for now. */
1068 if (types_match
1069 && !DECL_EXTERN_C_P (newdecl)
1070 && !DECL_EXTERN_C_P (olddecl)
1071 && targetm.target_option.function_versions (newdecl, olddecl))
1072 {
1073 if (record_versions)
1074 maybe_version_functions (newdecl, olddecl,
1075 (!DECL_FUNCTION_VERSIONED (newdecl)
1076 || !DECL_FUNCTION_VERSIONED (olddecl)));
1077 return 0;
1078 }
1079 }
1080 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1081 {
1082 if (!template_heads_equivalent_p (newdecl, olddecl))
1083 return 0;
1084
1085 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1086 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1087
1088 if (TREE_CODE (newres) != TREE_CODE (oldres))
1089 return 0;
1090
1091 /* Two template types match if they are the same. Otherwise, compare
1092 the underlying declarations. */
1093 if (TREE_CODE (newres) == TYPE_DECL)
1094 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1095 else
1096 types_match = decls_match (newres, oldres);
1097 }
1098 else
1099 {
1100 /* Need to check scope for variable declaration (VAR_DECL).
1101 For typedef (TYPE_DECL), scope is ignored. */
1102 if (VAR_P (newdecl)
1103 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1104 /* [dcl.link]
1105 Two declarations for an object with C language linkage
1106 with the same name (ignoring the namespace that qualify
1107 it) that appear in different namespace scopes refer to
1108 the same object. */
1109 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1110 return 0;
1111
1112 if (TREE_TYPE (newdecl) == error_mark_node)
1113 types_match = TREE_TYPE (olddecl) == error_mark_node;
1114 else if (TREE_TYPE (olddecl) == NULL_TREE)
1115 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1116 else if (TREE_TYPE (newdecl) == NULL_TREE)
1117 types_match = 0;
1118 else
1119 types_match = comptypes (TREE_TYPE (newdecl),
1120 TREE_TYPE (olddecl),
1121 COMPARE_REDECLARATION);
1122 }
1123
1124 return types_match;
1125 }
1126
1127 /* Mark DECL as versioned if it isn't already. */
1128
1129 static void
maybe_mark_function_versioned(tree decl)1130 maybe_mark_function_versioned (tree decl)
1131 {
1132 if (!DECL_FUNCTION_VERSIONED (decl))
1133 {
1134 DECL_FUNCTION_VERSIONED (decl) = 1;
1135 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle
1136 to include the version marker. */
1137 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1138 mangle_decl (decl);
1139 }
1140 }
1141
1142 /* NEWDECL and OLDDECL have identical signatures. If they are
1143 different versions adjust them and return true.
1144 If RECORD is set to true, record function versions. */
1145
1146 bool
maybe_version_functions(tree newdecl,tree olddecl,bool record)1147 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1148 {
1149 if (!targetm.target_option.function_versions (newdecl, olddecl))
1150 return false;
1151
1152 maybe_mark_function_versioned (olddecl);
1153 if (DECL_LOCAL_DECL_P (olddecl))
1154 {
1155 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl);
1156 maybe_mark_function_versioned (olddecl);
1157 }
1158
1159 maybe_mark_function_versioned (newdecl);
1160 if (DECL_LOCAL_DECL_P (newdecl))
1161 {
1162 /* Unfortunately, we can get here before pushdecl naturally calls
1163 push_local_extern_decl_alias, so we need to call it directly. */
1164 if (!DECL_LOCAL_DECL_ALIAS (newdecl))
1165 push_local_extern_decl_alias (newdecl);
1166 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl);
1167 maybe_mark_function_versioned (newdecl);
1168 }
1169
1170 if (record)
1171 cgraph_node::record_function_versions (olddecl, newdecl);
1172
1173 return true;
1174 }
1175
1176 /* If NEWDECL is `static' and an `extern' was seen previously,
1177 warn about it. OLDDECL is the previous declaration.
1178
1179 Note that this does not apply to the C++ case of declaring
1180 a variable `extern const' and then later `const'.
1181
1182 Don't complain about built-in functions, since they are beyond
1183 the user's control. */
1184
1185 void
warn_extern_redeclared_static(tree newdecl,tree olddecl)1186 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1187 {
1188 if (TREE_CODE (newdecl) == TYPE_DECL
1189 || TREE_CODE (newdecl) == TEMPLATE_DECL
1190 || TREE_CODE (newdecl) == CONST_DECL
1191 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1192 return;
1193
1194 /* Don't get confused by static member functions; that's a different
1195 use of `static'. */
1196 if (TREE_CODE (newdecl) == FUNCTION_DECL
1197 && DECL_STATIC_FUNCTION_P (newdecl))
1198 return;
1199
1200 /* If the old declaration was `static', or the new one isn't, then
1201 everything is OK. */
1202 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1203 return;
1204
1205 /* It's OK to declare a builtin function as `static'. */
1206 if (TREE_CODE (olddecl) == FUNCTION_DECL
1207 && DECL_ARTIFICIAL (olddecl))
1208 return;
1209
1210 auto_diagnostic_group d;
1211 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1212 "%qD was declared %<extern%> and later %<static%>", newdecl))
1213 inform (DECL_SOURCE_LOCATION (olddecl),
1214 "previous declaration of %qD", olddecl);
1215 }
1216
1217 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1218 function templates. If their exception specifications do not
1219 match, issue a diagnostic. */
1220
1221 static void
check_redeclaration_exception_specification(tree new_decl,tree old_decl)1222 check_redeclaration_exception_specification (tree new_decl,
1223 tree old_decl)
1224 {
1225 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1226 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1227
1228 /* Two default specs are equivalent, don't force evaluation. */
1229 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1230 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1231 return;
1232
1233 if (!type_dependent_expression_p (old_decl))
1234 {
1235 maybe_instantiate_noexcept (new_decl);
1236 maybe_instantiate_noexcept (old_decl);
1237 }
1238 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1239 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1240
1241 /* [except.spec]
1242
1243 If any declaration of a function has an exception-specification,
1244 all declarations, including the definition and an explicit
1245 specialization, of that function shall have an
1246 exception-specification with the same set of type-ids. */
1247 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl)
1248 && !DECL_IS_UNDECLARED_BUILTIN (new_decl)
1249 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1250 {
1251 const char *const msg
1252 = G_("declaration of %qF has a different exception specifier");
1253 bool complained = true;
1254 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1255 auto_diagnostic_group d;
1256 if (DECL_IN_SYSTEM_HEADER (old_decl))
1257 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1258 else if (!flag_exceptions)
1259 /* We used to silently permit mismatched eh specs with
1260 -fno-exceptions, so make them a pedwarn now. */
1261 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1262 else
1263 error_at (new_loc, msg, new_decl);
1264 if (complained)
1265 inform (DECL_SOURCE_LOCATION (old_decl),
1266 "from previous declaration %qF", old_decl);
1267 }
1268 }
1269
1270 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1271 Otherwise issue diagnostics. */
1272
1273 static bool
validate_constexpr_redeclaration(tree old_decl,tree new_decl)1274 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1275 {
1276 old_decl = STRIP_TEMPLATE (old_decl);
1277 new_decl = STRIP_TEMPLATE (new_decl);
1278 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1279 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1280 return true;
1281 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1282 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1283 {
1284 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1285 return true;
1286 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1287 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1288 return true;
1289 }
1290 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1291 {
1292 /* With -fimplicit-constexpr, ignore changes in the constexpr
1293 keyword. */
1294 if (flag_implicit_constexpr
1295 && (DECL_IMMEDIATE_FUNCTION_P (new_decl)
1296 == DECL_IMMEDIATE_FUNCTION_P (old_decl)))
1297 return true;
1298 if (fndecl_built_in_p (old_decl))
1299 {
1300 /* Hide a built-in declaration. */
1301 DECL_DECLARED_CONSTEXPR_P (old_decl)
1302 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1303 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1304 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1305 return true;
1306 }
1307 /* 7.1.5 [dcl.constexpr]
1308 Note: An explicit specialization can differ from the template
1309 declaration with respect to the constexpr specifier. */
1310 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1311 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1312 return true;
1313
1314 const char *kind = "constexpr";
1315 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1316 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1317 kind = "consteval";
1318 error_at (DECL_SOURCE_LOCATION (new_decl),
1319 "redeclaration %qD differs in %qs "
1320 "from previous declaration", new_decl,
1321 kind);
1322 inform (DECL_SOURCE_LOCATION (old_decl),
1323 "previous declaration %qD", old_decl);
1324 return false;
1325 }
1326 return true;
1327 }
1328
1329 // If OLDDECL and NEWDECL are concept declarations with the same type
1330 // (i.e., and template parameters), but different requirements,
1331 // emit diagnostics and return true. Otherwise, return false.
1332 static inline bool
check_concept_refinement(tree olddecl,tree newdecl)1333 check_concept_refinement (tree olddecl, tree newdecl)
1334 {
1335 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1336 return false;
1337
1338 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1339 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1340 if (TREE_CODE (d1) != TREE_CODE (d2))
1341 return false;
1342
1343 tree t1 = TREE_TYPE (d1);
1344 tree t2 = TREE_TYPE (d2);
1345 if (TREE_CODE (d1) == FUNCTION_DECL)
1346 {
1347 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1348 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1349 DECL_TEMPLATE_PARMS (newdecl))
1350 && !equivalently_constrained (olddecl, newdecl))
1351 {
1352 error ("cannot specialize concept %q#D", olddecl);
1353 return true;
1354 }
1355 }
1356 return false;
1357 }
1358
1359 /* DECL is a redeclaration of a function or function template. If
1360 it does have default arguments issue a diagnostic. Note: this
1361 function is used to enforce the requirements in C++11 8.3.6 about
1362 no default arguments in redeclarations. */
1363
1364 static void
check_redeclaration_no_default_args(tree decl)1365 check_redeclaration_no_default_args (tree decl)
1366 {
1367 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1368
1369 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1370 t && t != void_list_node; t = TREE_CHAIN (t))
1371 if (TREE_PURPOSE (t))
1372 {
1373 permerror (DECL_SOURCE_LOCATION (decl),
1374 "redeclaration of %q#D may not have default "
1375 "arguments", decl);
1376 return;
1377 }
1378 }
1379
1380 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1381 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1382 the TEMPLATE_DECLs in case of function templates). This function is used
1383 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1384 "If a friend declaration specifies a default argument expression, that
1385 declaration shall be a definition and shall be the only declaration of
1386 the function or function template in the translation unit." */
1387
1388 static void
check_no_redeclaration_friend_default_args(tree olddecl,tree newdecl)1389 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl)
1390 {
1391 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl))
1392 return;
1393
1394 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl),
1395 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1396 t1 && t1 != void_list_node;
1397 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1398 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1))
1399 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1400 {
1401 auto_diagnostic_group d;
1402 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1403 "friend declaration of %q#D specifies default "
1404 "arguments and isn%'t the only declaration", newdecl))
1405 inform (DECL_SOURCE_LOCATION (olddecl),
1406 "previous declaration of %q#D", olddecl);
1407 return;
1408 }
1409 }
1410
1411 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1412 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1413
1414 static void
merge_attribute_bits(tree newdecl,tree olddecl)1415 merge_attribute_bits (tree newdecl, tree olddecl)
1416 {
1417 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1418 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1419 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1420 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1421 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1422 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1423 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1424 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1425 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1426 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1427 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1428 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1429 }
1430
1431 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1432 && lookup_attribute ("gnu_inline", \
1433 DECL_ATTRIBUTES (fn)))
1434
1435 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1436 ambiguates olddecl. Returns true if an error occurs. */
1437
1438 static bool
duplicate_function_template_decls(tree newdecl,tree olddecl)1439 duplicate_function_template_decls (tree newdecl, tree olddecl)
1440 {
1441
1442 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1443 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1444 /* Function template declarations can be differentiated by parameter
1445 and return type. */
1446 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1447 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1448 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1449 TREE_TYPE (TREE_TYPE (olddecl))))
1450 {
1451 /* ... and also by their template-heads and requires-clauses. */
1452 if (template_heads_equivalent_p (newdecl, olddecl)
1453 && function_requirements_equivalent_p (newres, oldres))
1454 {
1455 error ("ambiguating new declaration %q+#D", newdecl);
1456 inform (DECL_SOURCE_LOCATION (olddecl),
1457 "old declaration %q#D", olddecl);
1458 return true;
1459 }
1460
1461 /* FIXME: The types are the same but the are differences
1462 in either the template heads or function requirements.
1463 We should be able to diagnose a set of common errors
1464 stemming from these declarations. For example:
1465
1466 template<typename T> requires C void f(...);
1467 template<typename T> void f(...) requires C;
1468
1469 These are functionally equivalent but not equivalent. */
1470 }
1471
1472 return false;
1473 }
1474
1475 /* OLD_PARMS is the innermost set of template parameters for some template
1476 declaration, and NEW_PARMS is the corresponding set of template parameters
1477 for a redeclaration of that template. Merge the default arguments within
1478 these two sets of parameters. CLASS_P is true iff the template in
1479 question is a class template. */
1480
1481 bool
merge_default_template_args(tree new_parms,tree old_parms,bool class_p)1482 merge_default_template_args (tree new_parms, tree old_parms, bool class_p)
1483 {
1484 gcc_checking_assert (TREE_VEC_LENGTH (new_parms)
1485 == TREE_VEC_LENGTH (old_parms));
1486 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++)
1487 {
1488 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i));
1489 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i));
1490 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i));
1491 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i));
1492 if (error_operand_p (new_parm) || error_operand_p (old_parm))
1493 return false;
1494 if (new_default != NULL_TREE && old_default != NULL_TREE)
1495 {
1496 auto_diagnostic_group d;
1497 error ("redefinition of default argument for %q+#D", new_parm);
1498 inform (DECL_SOURCE_LOCATION (old_parm),
1499 "original definition appeared here");
1500 return false;
1501 }
1502 else if (new_default != NULL_TREE)
1503 /* Update the previous template parameters (which are the ones
1504 that will really count) with the new default value. */
1505 old_default = new_default;
1506 else if (class_p && old_default != NULL_TREE)
1507 /* Update the new parameters, too; they'll be used as the
1508 parameters for any members. */
1509 new_default = old_default;
1510 }
1511 return true;
1512 }
1513
1514 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1515 If the redeclaration is invalid, a diagnostic is issued, and the
1516 error_mark_node is returned. Otherwise, OLDDECL is returned.
1517
1518 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1519 returned.
1520
1521 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true
1522 if the old decl was hidden.
1523
1524 Hidden decls can be anticipated builtins, injected friends, or
1525 (coming soon) injected from a local-extern decl. */
1526
1527 tree
duplicate_decls(tree newdecl,tree olddecl,bool hiding,bool was_hidden)1528 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
1529 {
1530 unsigned olddecl_uid = DECL_UID (olddecl);
1531 int types_match = 0;
1532 int new_defines_function = 0;
1533 tree new_template_info;
1534 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1535 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1536
1537 if (newdecl == olddecl)
1538 return olddecl;
1539
1540 types_match = decls_match (newdecl, olddecl);
1541
1542 /* If either the type of the new decl or the type of the old decl is an
1543 error_mark_node, then that implies that we have already issued an
1544 error (earlier) for some bogus type specification, and in that case,
1545 it is rather pointless to harass the user with yet more error message
1546 about the same declaration, so just pretend the types match here. */
1547 if (TREE_TYPE (newdecl) == error_mark_node
1548 || TREE_TYPE (olddecl) == error_mark_node)
1549 return error_mark_node;
1550
1551 /* Check for redeclaration and other discrepancies. */
1552 if (TREE_CODE (olddecl) == FUNCTION_DECL
1553 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
1554 {
1555 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1556 {
1557 /* Avoid warnings redeclaring built-ins which have not been
1558 explicitly declared. */
1559 if (was_hidden)
1560 {
1561 if (TREE_PUBLIC (newdecl)
1562 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1563 warning_at (newdecl_loc,
1564 OPT_Wbuiltin_declaration_mismatch,
1565 "built-in function %qD declared as non-function",
1566 newdecl);
1567 return NULL_TREE;
1568 }
1569
1570 /* If you declare a built-in or predefined function name as static,
1571 the old definition is overridden, but optionally warn this was a
1572 bad choice of name. */
1573 if (! TREE_PUBLIC (newdecl))
1574 {
1575 warning_at (newdecl_loc,
1576 OPT_Wshadow,
1577 fndecl_built_in_p (olddecl)
1578 ? G_("shadowing built-in function %q#D")
1579 : G_("shadowing library function %q#D"), olddecl);
1580 /* Discard the old built-in function. */
1581 return NULL_TREE;
1582 }
1583 /* If the built-in is not ansi, then programs can override
1584 it even globally without an error. */
1585 else if (! fndecl_built_in_p (olddecl))
1586 warning_at (newdecl_loc, 0,
1587 "library function %q#D redeclared as non-function %q#D",
1588 olddecl, newdecl);
1589 else
1590 error_at (newdecl_loc,
1591 "declaration of %q#D conflicts with built-in "
1592 "declaration %q#D", newdecl, olddecl);
1593 return NULL_TREE;
1594 }
1595 else if (!types_match)
1596 {
1597 /* Avoid warnings redeclaring built-ins which have not been
1598 explicitly declared. */
1599 if (was_hidden)
1600 {
1601 tree t1, t2;
1602
1603 /* A new declaration doesn't match a built-in one unless it
1604 is also extern "C". */
1605 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl));
1606 gcc_assert (DECL_EXTERN_C_P (olddecl));
1607 if (!DECL_EXTERN_C_P (newdecl))
1608 return NULL_TREE;
1609
1610 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1611 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1612 t1 || t2;
1613 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1614 {
1615 if (!t1 || !t2)
1616 break;
1617 /* FILE, tm types are not known at the time
1618 we create the builtins. */
1619 for (unsigned i = 0;
1620 i < sizeof (builtin_structptr_types)
1621 / sizeof (builtin_structptr_type);
1622 ++i)
1623 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1624 {
1625 tree t = TREE_VALUE (t1);
1626
1627 if (TYPE_PTR_P (t)
1628 && TYPE_IDENTIFIER (TREE_TYPE (t))
1629 == get_identifier (builtin_structptr_types[i].str)
1630 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1631 {
1632 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1633
1634 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1635 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1636 types_match = decls_match (newdecl, olddecl);
1637 if (types_match)
1638 return duplicate_decls (newdecl, olddecl,
1639 hiding, was_hidden);
1640 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1641 }
1642 goto next_arg;
1643 }
1644
1645 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1646 break;
1647 next_arg:;
1648 }
1649
1650 warning_at (newdecl_loc,
1651 OPT_Wbuiltin_declaration_mismatch,
1652 "declaration of %q#D conflicts with built-in "
1653 "declaration %q#D", newdecl, olddecl);
1654 }
1655 else if ((DECL_EXTERN_C_P (newdecl)
1656 && DECL_EXTERN_C_P (olddecl))
1657 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1658 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1659 {
1660 /* Don't really override olddecl for __* prefixed builtins
1661 except for __[^b]*_chk, the compiler might be using those
1662 explicitly. */
1663 if (fndecl_built_in_p (olddecl))
1664 {
1665 tree id = DECL_NAME (olddecl);
1666 const char *name = IDENTIFIER_POINTER (id);
1667 size_t len;
1668
1669 if (name[0] == '_'
1670 && name[1] == '_'
1671 && (startswith (name + 2, "builtin_")
1672 || (len = strlen (name)) <= strlen ("___chk")
1673 || memcmp (name + len - strlen ("_chk"),
1674 "_chk", strlen ("_chk") + 1) != 0))
1675 {
1676 if (DECL_INITIAL (newdecl))
1677 {
1678 error_at (newdecl_loc,
1679 "definition of %q#D ambiguates built-in "
1680 "declaration %q#D", newdecl, olddecl);
1681 return error_mark_node;
1682 }
1683 auto_diagnostic_group d;
1684 if (permerror (newdecl_loc,
1685 "new declaration %q#D ambiguates built-in"
1686 " declaration %q#D", newdecl, olddecl)
1687 && flag_permissive)
1688 inform (newdecl_loc,
1689 "ignoring the %q#D declaration", newdecl);
1690 return flag_permissive ? olddecl : error_mark_node;
1691 }
1692 }
1693
1694 /* A near match; override the builtin. */
1695
1696 if (TREE_PUBLIC (newdecl))
1697 warning_at (newdecl_loc,
1698 OPT_Wbuiltin_declaration_mismatch,
1699 "new declaration %q#D ambiguates built-in "
1700 "declaration %q#D", newdecl, olddecl);
1701 else
1702 warning (OPT_Wshadow,
1703 fndecl_built_in_p (olddecl)
1704 ? G_("shadowing built-in function %q#D")
1705 : G_("shadowing library function %q#D"), olddecl);
1706 }
1707 else
1708 /* Discard the old built-in function. */
1709 return NULL_TREE;
1710
1711 /* Replace the old RTL to avoid problems with inlining. */
1712 COPY_DECL_RTL (newdecl, olddecl);
1713 }
1714 else
1715 {
1716 /* Even if the types match, prefer the new declarations type
1717 for built-ins which have not been explicitly declared,
1718 for exception lists, etc... */
1719 tree type = TREE_TYPE (newdecl);
1720 tree attribs = (*targetm.merge_type_attributes)
1721 (TREE_TYPE (olddecl), type);
1722
1723 type = cp_build_type_attribute_variant (type, attribs);
1724 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1725 }
1726
1727 /* If a function is explicitly declared "throw ()", propagate that to
1728 the corresponding builtin. */
1729 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1730 && was_hidden
1731 && TREE_NOTHROW (newdecl)
1732 && !TREE_NOTHROW (olddecl))
1733 {
1734 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1735 tree tmpdecl = builtin_decl_explicit (fncode);
1736 if (tmpdecl && tmpdecl != olddecl && types_match)
1737 TREE_NOTHROW (tmpdecl) = 1;
1738 }
1739
1740 /* Whether or not the builtin can throw exceptions has no
1741 bearing on this declarator. */
1742 TREE_NOTHROW (olddecl) = 0;
1743
1744 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1745 {
1746 /* If a builtin function is redeclared as `static', merge
1747 the declarations, but make the original one static. */
1748 DECL_THIS_STATIC (olddecl) = 1;
1749 TREE_PUBLIC (olddecl) = 0;
1750
1751 /* Make the old declaration consistent with the new one so
1752 that all remnants of the builtin-ness of this function
1753 will be banished. */
1754 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1755 COPY_DECL_RTL (newdecl, olddecl);
1756 }
1757 }
1758 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1759 {
1760 /* C++ Standard, 3.3, clause 4:
1761 "[Note: a namespace name or a class template name must be unique
1762 in its declarative region (7.3.2, clause 14). ]" */
1763 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1764 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1765 /* Namespace conflicts with not namespace. */;
1766 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1767 || DECL_TYPE_TEMPLATE_P (newdecl))
1768 /* Class template conflicts. */;
1769 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL
1770 && DECL_TEMPLATE_RESULT (olddecl)
1771 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL)
1772 || (TREE_CODE (newdecl) == TEMPLATE_DECL
1773 && DECL_TEMPLATE_RESULT (newdecl)
1774 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL))
1775 /* Variable template conflicts. */;
1776 else if (concept_definition_p (olddecl)
1777 || concept_definition_p (newdecl))
1778 /* Concept conflicts. */;
1779 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1780 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1781 || (TREE_CODE (olddecl) == FUNCTION_DECL
1782 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1783 {
1784 /* One is a function and the other is a template
1785 function. */
1786 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1787 return NULL_TREE;
1788
1789 /* There can only be one! */
1790 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1791 && check_raw_literal_operator (olddecl))
1792 error_at (newdecl_loc,
1793 "literal operator %q#D conflicts with"
1794 " raw literal operator", newdecl);
1795 else if (check_raw_literal_operator (newdecl))
1796 error_at (newdecl_loc,
1797 "raw literal operator %q#D conflicts with"
1798 " literal operator template", newdecl);
1799 else
1800 return NULL_TREE;
1801
1802 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1803 return error_mark_node;
1804 }
1805 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl))
1806 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl)))
1807 /* A structured binding must be unique in its declarative region. */;
1808 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1809 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1810 /* One is an implicit typedef, that's ok. */
1811 return NULL_TREE;
1812
1813 error ("%q#D redeclared as different kind of entity", newdecl);
1814 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1815
1816 return error_mark_node;
1817 }
1818 else if (!types_match)
1819 {
1820 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1821 /* These are certainly not duplicate declarations; they're
1822 from different scopes. */
1823 return NULL_TREE;
1824
1825 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1826 {
1827 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1828 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1829
1830 /* The name of a class template may not be declared to refer to
1831 any other template, class, function, object, namespace, value,
1832 or type in the same scope. */
1833 if (TREE_CODE (oldres) == TYPE_DECL
1834 || TREE_CODE (newres) == TYPE_DECL)
1835 {
1836 error_at (newdecl_loc,
1837 "conflicting declaration of template %q#D", newdecl);
1838 inform (olddecl_loc,
1839 "previous declaration %q#D", olddecl);
1840 return error_mark_node;
1841 }
1842
1843 else if (TREE_CODE (oldres) == FUNCTION_DECL
1844 && TREE_CODE (newres) == FUNCTION_DECL)
1845 {
1846 if (duplicate_function_template_decls (newdecl, olddecl))
1847 return error_mark_node;
1848 return NULL_TREE;
1849 }
1850 else if (check_concept_refinement (olddecl, newdecl))
1851 return error_mark_node;
1852 return NULL_TREE;
1853 }
1854 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1855 {
1856 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1857 {
1858 error_at (newdecl_loc,
1859 "conflicting declaration of C function %q#D",
1860 newdecl);
1861 inform (olddecl_loc,
1862 "previous declaration %q#D", olddecl);
1863 return error_mark_node;
1864 }
1865 /* For function versions, params and types match, but they
1866 are not ambiguous. */
1867 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1868 && !DECL_FUNCTION_VERSIONED (olddecl))
1869 // The functions have the same parameter types.
1870 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1871 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1872 // And the same constraints.
1873 && equivalently_constrained (newdecl, olddecl))
1874 {
1875 error_at (newdecl_loc,
1876 "ambiguating new declaration of %q#D", newdecl);
1877 inform (olddecl_loc,
1878 "old declaration %q#D", olddecl);
1879 return error_mark_node;
1880 }
1881 else
1882 return NULL_TREE;
1883 }
1884 else
1885 {
1886 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1887 inform (olddecl_loc,
1888 "previous declaration as %q#D", olddecl);
1889 return error_mark_node;
1890 }
1891 }
1892 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1893 && DECL_OMP_DECLARE_REDUCTION_P (newdecl))
1894 {
1895 /* OMP UDRs are never duplicates. */
1896 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl));
1897 error_at (newdecl_loc,
1898 "redeclaration of %<pragma omp declare reduction%>");
1899 inform (olddecl_loc,
1900 "previous %<pragma omp declare reduction%> declaration");
1901 return error_mark_node;
1902 }
1903 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1904 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1905 && (!DECL_TEMPLATE_INFO (newdecl)
1906 || (DECL_TI_TEMPLATE (newdecl)
1907 != DECL_TI_TEMPLATE (olddecl))))
1908 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1909 && (!DECL_TEMPLATE_INFO (olddecl)
1910 || (DECL_TI_TEMPLATE (olddecl)
1911 != DECL_TI_TEMPLATE (newdecl))))))
1912 /* It's OK to have a template specialization and a non-template
1913 with the same type, or to have specializations of two
1914 different templates with the same type. Note that if one is a
1915 specialization, and the other is an instantiation of the same
1916 template, that we do not exit at this point. That situation
1917 can occur if we instantiate a template class, and then
1918 specialize one of its methods. This situation is valid, but
1919 the declarations must be merged in the usual way. */
1920 return NULL_TREE;
1921 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1922 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1923 && !DECL_USE_TEMPLATE (newdecl))
1924 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1925 && !DECL_USE_TEMPLATE (olddecl))))
1926 /* One of the declarations is a template instantiation, and the
1927 other is not a template at all. That's OK. */
1928 return NULL_TREE;
1929 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1930 {
1931 /* In [namespace.alias] we have:
1932
1933 In a declarative region, a namespace-alias-definition can be
1934 used to redefine a namespace-alias declared in that declarative
1935 region to refer only to the namespace to which it already
1936 refers.
1937
1938 Therefore, if we encounter a second alias directive for the same
1939 alias, we can just ignore the second directive. */
1940 if (DECL_NAMESPACE_ALIAS (newdecl)
1941 && (DECL_NAMESPACE_ALIAS (newdecl)
1942 == DECL_NAMESPACE_ALIAS (olddecl)))
1943 return olddecl;
1944
1945 /* Leave it to update_binding to merge or report error. */
1946 return NULL_TREE;
1947 }
1948 else
1949 {
1950 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1951 if (errmsg)
1952 {
1953 auto_diagnostic_group d;
1954 error_at (newdecl_loc, errmsg, newdecl);
1955 if (DECL_NAME (olddecl) != NULL_TREE)
1956 inform (olddecl_loc,
1957 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1958 ? G_("%q#D previously defined here")
1959 : G_("%q#D previously declared here"), olddecl);
1960 return error_mark_node;
1961 }
1962 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1963 && DECL_INITIAL (olddecl) != NULL_TREE
1964 && !prototype_p (TREE_TYPE (olddecl))
1965 && prototype_p (TREE_TYPE (newdecl)))
1966 {
1967 /* Prototype decl follows defn w/o prototype. */
1968 auto_diagnostic_group d;
1969 if (warning_at (newdecl_loc, 0,
1970 "prototype specified for %q#D", newdecl))
1971 inform (olddecl_loc,
1972 "previous non-prototype definition here");
1973 }
1974 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1975 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1976 {
1977 /* [dcl.link]
1978 If two declarations of the same function or object
1979 specify different linkage-specifications ..., the program
1980 is ill-formed.... Except for functions with C++ linkage,
1981 a function declaration without a linkage specification
1982 shall not precede the first linkage specification for
1983 that function. A function can be declared without a
1984 linkage specification after an explicit linkage
1985 specification has been seen; the linkage explicitly
1986 specified in the earlier declaration is not affected by
1987 such a function declaration.
1988
1989 DR 563 raises the question why the restrictions on
1990 functions should not also apply to objects. Older
1991 versions of G++ silently ignore the linkage-specification
1992 for this example:
1993
1994 namespace N {
1995 extern int i;
1996 extern "C" int i;
1997 }
1998
1999 which is clearly wrong. Therefore, we now treat objects
2000 like functions. */
2001 if (current_lang_depth () == 0)
2002 {
2003 /* There is no explicit linkage-specification, so we use
2004 the linkage from the previous declaration. */
2005 retrofit_lang_decl (newdecl);
2006 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2007 }
2008 else
2009 {
2010 auto_diagnostic_group d;
2011 error_at (newdecl_loc,
2012 "conflicting declaration of %q#D with %qL linkage",
2013 newdecl, DECL_LANGUAGE (newdecl));
2014 inform (olddecl_loc,
2015 "previous declaration with %qL linkage",
2016 DECL_LANGUAGE (olddecl));
2017 }
2018 }
2019
2020 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
2021 ;
2022 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
2023 {
2024 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
2025 if (DECL_FUNCTION_MEMBER_P (olddecl)
2026 && (/* grokfndecl passes member function templates too
2027 as FUNCTION_DECLs. */
2028 DECL_TEMPLATE_INFO (olddecl)
2029 /* C++11 8.3.6/6.
2030 Default arguments for a member function of a class
2031 template shall be specified on the initial declaration
2032 of the member function within the class template. */
2033 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
2034 {
2035 check_redeclaration_no_default_args (newdecl);
2036
2037 if (DECL_TEMPLATE_INFO (olddecl)
2038 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl)))
2039 {
2040 tree new_parms = DECL_TEMPLATE_INFO (newdecl)
2041 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl))
2042 : INNERMOST_TEMPLATE_PARMS (current_template_parms);
2043 tree old_parms
2044 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl));
2045 merge_default_template_args (new_parms, old_parms,
2046 /*class_p=*/false);
2047 }
2048 }
2049 else
2050 {
2051 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
2052 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
2053 int i = 1;
2054
2055 for (; t1 && t1 != void_list_node;
2056 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2057 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2058 {
2059 if (simple_cst_equal (TREE_PURPOSE (t1),
2060 TREE_PURPOSE (t2)) == 1)
2061 {
2062 auto_diagnostic_group d;
2063 if (permerror (newdecl_loc,
2064 "default argument given for parameter "
2065 "%d of %q#D", i, newdecl))
2066 inform (olddecl_loc,
2067 "previous specification in %q#D here",
2068 olddecl);
2069 }
2070 else
2071 {
2072 auto_diagnostic_group d;
2073 error_at (newdecl_loc,
2074 "default argument given for parameter %d "
2075 "of %q#D", i, newdecl);
2076 inform (olddecl_loc,
2077 "previous specification in %q#D here",
2078 olddecl);
2079 }
2080 }
2081
2082 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2083 argument expression, that declaration... shall be the only
2084 declaration of the function or function template in the
2085 translation unit." */
2086 check_no_redeclaration_friend_default_args (olddecl, newdecl);
2087 }
2088 }
2089 }
2090
2091 /* Do not merge an implicit typedef with an explicit one. In:
2092
2093 class A;
2094 ...
2095 typedef class A A __attribute__ ((foo));
2096
2097 the attribute should apply only to the typedef. */
2098 if (TREE_CODE (olddecl) == TYPE_DECL
2099 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
2100 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
2101 return NULL_TREE;
2102
2103 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl))
2104 return NULL_TREE;
2105
2106 if (!validate_constexpr_redeclaration (olddecl, newdecl))
2107 return error_mark_node;
2108
2109 if (modules_p ()
2110 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL
2111 && TREE_CODE (olddecl) != NAMESPACE_DECL
2112 && !hiding)
2113 {
2114 if (DECL_ARTIFICIAL (olddecl))
2115 {
2116 if (!(global_purview_p () || not_module_p ()))
2117 error ("declaration %qD conflicts with builtin", newdecl);
2118 else
2119 DECL_MODULE_EXPORT_P (olddecl) = DECL_MODULE_EXPORT_P (newdecl);
2120 }
2121 else
2122 {
2123 if (!module_may_redeclare (olddecl))
2124 {
2125 error ("declaration %qD conflicts with import", newdecl);
2126 inform (olddecl_loc, "import declared %q#D here", olddecl);
2127
2128 return error_mark_node;
2129 }
2130
2131 if (DECL_MODULE_EXPORT_P (newdecl)
2132 && !DECL_MODULE_EXPORT_P (olddecl))
2133 {
2134 error ("conflicting exporting declaration %qD", newdecl);
2135 inform (olddecl_loc, "previous declaration %q#D here", olddecl);
2136 }
2137 }
2138 }
2139
2140 /* We have committed to returning OLDDECL at this point. */
2141
2142 /* If new decl is `static' and an `extern' was seen previously,
2143 warn about it. */
2144 warn_extern_redeclared_static (newdecl, olddecl);
2145
2146 /* True to merge attributes between the declarations, false to
2147 set OLDDECL's attributes to those of NEWDECL (for template
2148 explicit specializations that specify their own attributes
2149 independent of those specified for the primary template). */
2150 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
2151 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
2152 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
2153
2154 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2155 {
2156 if (merge_attr)
2157 {
2158 if (diagnose_mismatched_attributes (olddecl, newdecl))
2159 inform (olddecl_loc, DECL_INITIAL (olddecl)
2160 ? G_("previous definition of %qD here")
2161 : G_("previous declaration of %qD here"), olddecl);
2162
2163 /* [dcl.attr.noreturn]: The first declaration of a function shall
2164 specify the noreturn attribute if any declaration of that function
2165 specifies the noreturn attribute. */
2166 tree a;
2167 if (TREE_THIS_VOLATILE (newdecl)
2168 && !TREE_THIS_VOLATILE (olddecl)
2169 /* This applies to [[noreturn]] only, not its GNU variants. */
2170 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2171 && cxx11_attribute_p (a)
2172 && get_attribute_namespace (a) == NULL_TREE)
2173 {
2174 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2175 "but its first declaration was not", newdecl);
2176 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2177 }
2178 }
2179
2180 /* Now that functions must hold information normally held
2181 by field decls, there is extra work to do so that
2182 declaration information does not get destroyed during
2183 definition. */
2184 if (DECL_VINDEX (olddecl))
2185 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2186 if (DECL_CONTEXT (olddecl))
2187 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2188 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2189 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2190 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2191 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2192 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2193 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2194 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2195 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2196 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl)
2197 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl);
2198 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2199 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2200 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2201 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2202
2203 /* Optionally warn about more than one declaration for the same
2204 name, but don't warn about a function declaration followed by a
2205 definition. */
2206 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2207 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2208 /* Don't warn about extern decl followed by definition. */
2209 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2210 /* Don't warn if at least one is/was hidden. */
2211 && !(hiding || was_hidden)
2212 /* Don't warn about declaration followed by specialization. */
2213 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2214 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2215 {
2216 auto_diagnostic_group d;
2217 if (warning_at (newdecl_loc,
2218 OPT_Wredundant_decls,
2219 "redundant redeclaration of %qD in same scope",
2220 newdecl))
2221 inform (olddecl_loc,
2222 "previous declaration of %qD", olddecl);
2223 }
2224
2225 /* [dcl.fct.def.delete] A deleted definition of a function shall be the
2226 first declaration of the function or, for an explicit specialization
2227 of a function template, the first declaration of that
2228 specialization. */
2229 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2230 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2231 {
2232 if (DECL_DELETED_FN (newdecl))
2233 {
2234 auto_diagnostic_group d;
2235 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD "
2236 "is not first declaration", newdecl))
2237 inform (olddecl_loc,
2238 "previous declaration of %qD", olddecl);
2239 }
2240 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2241 }
2242 }
2243
2244 /* Deal with C++: must preserve virtual function table size. */
2245 if (TREE_CODE (olddecl) == TYPE_DECL)
2246 {
2247 tree newtype = TREE_TYPE (newdecl);
2248 tree oldtype = TREE_TYPE (olddecl);
2249
2250 if (newtype != error_mark_node && oldtype != error_mark_node
2251 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2252 CLASSTYPE_FRIEND_CLASSES (newtype)
2253 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2254
2255 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2256 }
2257
2258 /* Copy all the DECL_... slots specified in the new decl except for
2259 any that we copy here from the old type. */
2260 if (merge_attr)
2261 DECL_ATTRIBUTES (newdecl)
2262 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2263 else
2264 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2265
2266 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2267 {
2268 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2269 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2270 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2271
2272 /* The new decl should not already have gathered any
2273 specializations. */
2274 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2275
2276 DECL_ATTRIBUTES (old_result)
2277 = (*targetm.merge_decl_attributes) (old_result, new_result);
2278
2279 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2280 {
2281 if (DECL_SOURCE_LOCATION (newdecl)
2282 != DECL_SOURCE_LOCATION (olddecl))
2283 {
2284 /* Per C++11 8.3.6/4, default arguments cannot be added in
2285 later declarations of a function template. */
2286 check_redeclaration_no_default_args (newdecl);
2287 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2288 argument expression, that declaration... shall be the only
2289 declaration of the function or function template in the
2290 translation unit." */
2291 check_no_redeclaration_friend_default_args
2292 (old_result, new_result);
2293
2294 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl);
2295 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl);
2296 merge_default_template_args (new_parms, old_parms,
2297 /*class_p=*/false);
2298 }
2299 if (!DECL_UNIQUE_FRIEND_P (new_result))
2300 DECL_UNIQUE_FRIEND_P (old_result) = false;
2301
2302 check_default_args (newdecl);
2303
2304 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2305 && DECL_INITIAL (new_result))
2306 {
2307 if (DECL_INITIAL (old_result))
2308 DECL_UNINLINABLE (old_result) = 1;
2309 else
2310 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2311 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2312 DECL_NOT_REALLY_EXTERN (old_result)
2313 = DECL_NOT_REALLY_EXTERN (new_result);
2314 DECL_INTERFACE_KNOWN (old_result)
2315 = DECL_INTERFACE_KNOWN (new_result);
2316 DECL_DECLARED_INLINE_P (old_result)
2317 = DECL_DECLARED_INLINE_P (new_result);
2318 DECL_DISREGARD_INLINE_LIMITS (old_result)
2319 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2320
2321 }
2322 else
2323 {
2324 DECL_DECLARED_INLINE_P (old_result)
2325 |= DECL_DECLARED_INLINE_P (new_result);
2326 DECL_DISREGARD_INLINE_LIMITS (old_result)
2327 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2328 check_redeclaration_exception_specification (newdecl, olddecl);
2329
2330 merge_attribute_bits (new_result, old_result);
2331 }
2332 }
2333
2334 /* If the new declaration is a definition, update the file and
2335 line information on the declaration, and also make
2336 the old declaration the same definition. */
2337 if (DECL_INITIAL (new_result) != NULL_TREE)
2338 {
2339 DECL_SOURCE_LOCATION (olddecl)
2340 = DECL_SOURCE_LOCATION (old_result)
2341 = DECL_SOURCE_LOCATION (newdecl);
2342 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2343 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2344 {
2345 tree parm;
2346 DECL_ARGUMENTS (old_result)
2347 = DECL_ARGUMENTS (new_result);
2348 for (parm = DECL_ARGUMENTS (old_result); parm;
2349 parm = DECL_CHAIN (parm))
2350 DECL_CONTEXT (parm) = old_result;
2351
2352 if (tree fc = DECL_FRIEND_CONTEXT (new_result))
2353 SET_DECL_FRIEND_CONTEXT (old_result, fc);
2354 }
2355 }
2356
2357 return olddecl;
2358 }
2359
2360 if (types_match)
2361 {
2362 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2363 check_redeclaration_exception_specification (newdecl, olddecl);
2364
2365 /* Automatically handles default parameters. */
2366 tree oldtype = TREE_TYPE (olddecl);
2367 tree newtype;
2368
2369 /* For typedefs use the old type, as the new type's DECL_NAME points
2370 at newdecl, which will be ggc_freed. */
2371 if (TREE_CODE (newdecl) == TYPE_DECL)
2372 {
2373 /* But NEWTYPE might have an attribute, honor that. */
2374 tree tem = TREE_TYPE (newdecl);
2375 newtype = oldtype;
2376
2377 if (TYPE_USER_ALIGN (tem))
2378 {
2379 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2380 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2381 TYPE_USER_ALIGN (newtype) = true;
2382 }
2383
2384 /* And remove the new type from the variants list. */
2385 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2386 {
2387 tree remove = TREE_TYPE (newdecl);
2388 if (TYPE_MAIN_VARIANT (remove) == remove)
2389 {
2390 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2391 /* If remove is the main variant, no need to remove that
2392 from the list. One of the DECL_ORIGINAL_TYPE
2393 variants, e.g. created for aligned attribute, might still
2394 refer to the newdecl TYPE_DECL though, so remove that one
2395 in that case. */
2396 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2397 if (orig != remove)
2398 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2399 t = TYPE_MAIN_VARIANT (t))
2400 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2401 {
2402 TYPE_NEXT_VARIANT (t)
2403 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2404 break;
2405 }
2406 }
2407 else
2408 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2409 t = TYPE_NEXT_VARIANT (t))
2410 if (TYPE_NEXT_VARIANT (t) == remove)
2411 {
2412 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2413 break;
2414 }
2415 }
2416 }
2417 else if (merge_attr)
2418 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2419 else
2420 newtype = TREE_TYPE (newdecl);
2421
2422 if (VAR_P (newdecl))
2423 {
2424 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2425 /* For already initialized vars, TREE_READONLY could have been
2426 cleared in cp_finish_decl, because the var needs runtime
2427 initialization or destruction. Make sure not to set
2428 TREE_READONLY on it again. */
2429 if (DECL_INITIALIZED_P (olddecl)
2430 && !DECL_EXTERNAL (olddecl)
2431 && !TREE_READONLY (olddecl))
2432 TREE_READONLY (newdecl) = 0;
2433 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2434 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2435 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2436 if (DECL_DEPENDENT_INIT_P (olddecl))
2437 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2438 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2439 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2440 DECL_DECLARED_CONSTEXPR_P (newdecl)
2441 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2442 DECL_DECLARED_CONSTINIT_P (newdecl)
2443 |= DECL_DECLARED_CONSTINIT_P (olddecl);
2444
2445 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2446 if (DECL_LANG_SPECIFIC (olddecl)
2447 && CP_DECL_THREADPRIVATE_P (olddecl))
2448 {
2449 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2450 retrofit_lang_decl (newdecl);
2451 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2452 }
2453 }
2454
2455 /* An explicit specialization of a function template or of a member
2456 function of a class template can be declared transaction_safe
2457 independently of whether the corresponding template entity is declared
2458 transaction_safe. */
2459 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2460 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2461 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2462 && tx_safe_fn_type_p (newtype)
2463 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2464 newtype = tx_unsafe_fn_variant (newtype);
2465
2466 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2467
2468 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2469 check_default_args (newdecl);
2470
2471 /* Lay the type out, unless already done. */
2472 if (! same_type_p (newtype, oldtype)
2473 && TREE_TYPE (newdecl) != error_mark_node
2474 && !(processing_template_decl && uses_template_parms (newdecl)))
2475 layout_type (TREE_TYPE (newdecl));
2476
2477 if ((VAR_P (newdecl)
2478 || TREE_CODE (newdecl) == PARM_DECL
2479 || TREE_CODE (newdecl) == RESULT_DECL
2480 || TREE_CODE (newdecl) == FIELD_DECL
2481 || TREE_CODE (newdecl) == TYPE_DECL)
2482 && !(processing_template_decl && uses_template_parms (newdecl)))
2483 layout_decl (newdecl, 0);
2484
2485 /* Merge deprecatedness. */
2486 if (TREE_DEPRECATED (newdecl))
2487 TREE_DEPRECATED (olddecl) = 1;
2488
2489 /* Merge unavailability. */
2490 if (TREE_UNAVAILABLE (newdecl))
2491 TREE_UNAVAILABLE (olddecl) = 1;
2492
2493 /* Preserve function specific target and optimization options */
2494 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2495 {
2496 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2497 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2498 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2499 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2500
2501 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2502 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2503 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2504 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2505
2506 if (!DECL_UNIQUE_FRIEND_P (olddecl))
2507 DECL_UNIQUE_FRIEND_P (newdecl) = false;
2508 }
2509 else
2510 {
2511 /* Merge the const type qualifier. */
2512 if (TREE_READONLY (newdecl))
2513 TREE_READONLY (olddecl) = 1;
2514 /* Merge the volatile type qualifier. */
2515 if (TREE_THIS_VOLATILE (newdecl))
2516 TREE_THIS_VOLATILE (olddecl) = 1;
2517 }
2518
2519 /* Merge the initialization information. */
2520 if (DECL_INITIAL (newdecl) == NULL_TREE
2521 && DECL_INITIAL (olddecl) != NULL_TREE)
2522 {
2523 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2524 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2525 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2526 {
2527 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2528 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2529 }
2530 }
2531
2532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2533 {
2534 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2535 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2536 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2537 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2538 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2539 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2540 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2541 DECL_IS_REPLACEABLE_OPERATOR (newdecl)
2542 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl);
2543
2544 if (merge_attr)
2545 merge_attribute_bits (newdecl, olddecl);
2546 else
2547 {
2548 /* Merge the noreturn bit. */
2549 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2550 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2551 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2552 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2553 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2554 }
2555 /* Keep the old RTL. */
2556 COPY_DECL_RTL (olddecl, newdecl);
2557 }
2558 else if (VAR_P (newdecl)
2559 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2560 {
2561 /* Keep the old RTL. We cannot keep the old RTL if the old
2562 declaration was for an incomplete object and the new
2563 declaration is not since many attributes of the RTL will
2564 change. */
2565 COPY_DECL_RTL (olddecl, newdecl);
2566 }
2567 }
2568 /* If cannot merge, then use the new type and qualifiers,
2569 and don't preserve the old rtl. */
2570 else
2571 {
2572 /* Clean out any memory we had of the old declaration. */
2573 tree oldstatic = value_member (olddecl, static_aggregates);
2574 if (oldstatic)
2575 TREE_VALUE (oldstatic) = error_mark_node;
2576
2577 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2578 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2579 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2580 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2581 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2582 }
2583
2584 /* Merge the storage class information. */
2585 merge_weak (newdecl, olddecl);
2586
2587 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2588 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2589 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2590 if (! DECL_EXTERNAL (olddecl))
2591 DECL_EXTERNAL (newdecl) = 0;
2592 if (! DECL_COMDAT (olddecl))
2593 DECL_COMDAT (newdecl) = 0;
2594
2595 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl))
2596 {
2597 if (!DECL_LOCAL_DECL_P (olddecl))
2598 /* This can happen if olddecl was brought in from the
2599 enclosing namespace via a using-decl. The new decl is
2600 then not a block-scope extern at all. */
2601 DECL_LOCAL_DECL_P (newdecl) = false;
2602 else
2603 {
2604 retrofit_lang_decl (newdecl);
2605 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
2606 = DECL_LOCAL_DECL_ALIAS (olddecl);
2607 DECL_ATTRIBUTES (alias)
2608 = (*targetm.merge_decl_attributes) (alias, newdecl);
2609 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2610 merge_attribute_bits (newdecl, alias);
2611 }
2612 }
2613
2614 new_template_info = NULL_TREE;
2615 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2616 {
2617 bool new_redefines_gnu_inline = false;
2618
2619 if (new_defines_function
2620 && ((DECL_INTERFACE_KNOWN (olddecl)
2621 && TREE_CODE (olddecl) == FUNCTION_DECL)
2622 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2623 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2624 == FUNCTION_DECL))))
2625 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl));
2626
2627 if (!new_redefines_gnu_inline)
2628 {
2629 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2630 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2631 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2632 }
2633
2634 if (TREE_CODE (newdecl) != TYPE_DECL)
2635 {
2636 DECL_TEMPLATE_INSTANTIATED (newdecl)
2637 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2638 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2639
2640 /* If the OLDDECL is an instantiation and/or specialization,
2641 then the NEWDECL must be too. But, it may not yet be marked
2642 as such if the caller has created NEWDECL, but has not yet
2643 figured out that it is a redeclaration. */
2644 if (!DECL_USE_TEMPLATE (newdecl))
2645 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2646
2647 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl))
2648 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2649 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2650 }
2651
2652 /* Don't really know how much of the language-specific
2653 values we should copy from old to new. */
2654 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2655
2656 if (LANG_DECL_HAS_MIN (newdecl))
2657 {
2658 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2659 if (DECL_TEMPLATE_INFO (newdecl))
2660 {
2661 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2662 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2663 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2664 /* Remember the presence of explicit specialization args. */
2665 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2666 = TINFO_USED_TEMPLATE_ID (new_template_info);
2667 }
2668
2669 if (non_templated_friend_p (olddecl))
2670 /* Don't copy tinfo from a non-templated friend (PR105761). */;
2671 else
2672 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2673 }
2674
2675 if (DECL_DECLARES_FUNCTION_P (newdecl))
2676 {
2677 /* Only functions have these fields. */
2678 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2679 DECL_BEFRIENDING_CLASSES (newdecl)
2680 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2681 DECL_BEFRIENDING_CLASSES (olddecl));
2682 /* DECL_THUNKS is only valid for virtual functions,
2683 otherwise it is a DECL_FRIEND_CONTEXT. */
2684 if (DECL_VIRTUAL_P (newdecl))
2685 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2686 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
2687 SET_DECL_FRIEND_CONTEXT (olddecl, fc);
2688 }
2689 else if (VAR_P (newdecl))
2690 {
2691 /* Only variables have this field. */
2692 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2693 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2694 }
2695 }
2696
2697 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2698 {
2699 tree parm;
2700
2701 /* Merge parameter attributes. */
2702 tree oldarg, newarg;
2703 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2704 oldarg && newarg;
2705 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2706 {
2707 DECL_ATTRIBUTES (newarg)
2708 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2709 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2710 }
2711
2712 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2713 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2714 {
2715 /* If newdecl is not a specialization, then it is not a
2716 template-related function at all. And that means that we
2717 should have exited above, returning 0. */
2718 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2719
2720 if (DECL_ODR_USED (olddecl))
2721 /* From [temp.expl.spec]:
2722
2723 If a template, a member template or the member of a class
2724 template is explicitly specialized then that
2725 specialization shall be declared before the first use of
2726 that specialization that would cause an implicit
2727 instantiation to take place, in every translation unit in
2728 which such a use occurs. */
2729 error ("explicit specialization of %qD after first use",
2730 olddecl);
2731
2732 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2733 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2734 && DECL_DECLARED_INLINE_P (newdecl));
2735
2736 /* Don't propagate visibility from the template to the
2737 specialization here. We'll do that in determine_visibility if
2738 appropriate. */
2739 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2740
2741 /* [temp.expl.spec/14] We don't inline explicit specialization
2742 just because the primary template says so. */
2743 gcc_assert (!merge_attr);
2744
2745 DECL_DECLARED_INLINE_P (olddecl)
2746 = DECL_DECLARED_INLINE_P (newdecl);
2747
2748 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2749 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2750
2751 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2752 }
2753 else if (new_defines_function && DECL_INITIAL (olddecl))
2754 {
2755 /* Never inline re-defined extern inline functions.
2756 FIXME: this could be better handled by keeping both
2757 function as separate declarations. */
2758 DECL_UNINLINABLE (newdecl) = 1;
2759 }
2760 else
2761 {
2762 if (DECL_PENDING_INLINE_P (olddecl))
2763 {
2764 DECL_PENDING_INLINE_P (newdecl) = 1;
2765 DECL_PENDING_INLINE_INFO (newdecl)
2766 = DECL_PENDING_INLINE_INFO (olddecl);
2767 }
2768 else if (DECL_PENDING_INLINE_P (newdecl))
2769 ;
2770 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2771 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2772 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2773
2774 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2775
2776 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2777 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2778
2779 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2780 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2781 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2782 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2783 }
2784
2785 /* Preserve abstractness on cloned [cd]tors. */
2786 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2787
2788 /* Update newdecl's parms to point at olddecl. */
2789 for (parm = DECL_ARGUMENTS (newdecl); parm;
2790 parm = DECL_CHAIN (parm))
2791 DECL_CONTEXT (parm) = olddecl;
2792
2793 if (! types_match)
2794 {
2795 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2796 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2797 COPY_DECL_RTL (newdecl, olddecl);
2798 }
2799 if (! types_match || new_defines_function)
2800 {
2801 /* These need to be copied so that the names are available.
2802 Note that if the types do match, we'll preserve inline
2803 info and other bits, but if not, we won't. */
2804 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2805 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2806 }
2807 /* If redeclaring a builtin function, it stays built in
2808 if newdecl is a gnu_inline definition, or if newdecl is just
2809 a declaration. */
2810 if (fndecl_built_in_p (olddecl)
2811 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2812 {
2813 copy_decl_built_in_function (newdecl, olddecl);
2814 /* If we're keeping the built-in definition, keep the rtl,
2815 regardless of declaration matches. */
2816 COPY_DECL_RTL (olddecl, newdecl);
2817 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2818 {
2819 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2820 if (builtin_decl_explicit_p (fncode))
2821 {
2822 /* A compatible prototype of these builtin functions
2823 is seen, assume the runtime implements it with
2824 the expected semantics. */
2825 switch (fncode)
2826 {
2827 case BUILT_IN_STPCPY:
2828 set_builtin_decl_implicit_p (fncode, true);
2829 break;
2830 default:
2831 set_builtin_decl_declared_p (fncode, true);
2832 break;
2833 }
2834 }
2835
2836 copy_attributes_to_builtin (newdecl);
2837 }
2838 }
2839 if (new_defines_function)
2840 /* If defining a function declared with other language
2841 linkage, use the previously declared language linkage. */
2842 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2843 else if (types_match)
2844 {
2845 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2846 /* Don't clear out the arguments if we're just redeclaring a
2847 function. */
2848 if (DECL_ARGUMENTS (olddecl))
2849 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2850 }
2851 }
2852 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2853 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2854
2855 /* Now preserve various other info from the definition. */
2856 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2857 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2858 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2859 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2860
2861 /* Warn about conflicting visibility specifications. */
2862 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2863 && DECL_VISIBILITY_SPECIFIED (newdecl)
2864 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2865 {
2866 auto_diagnostic_group d;
2867 if (warning_at (newdecl_loc, OPT_Wattributes,
2868 "%qD: visibility attribute ignored because it "
2869 "conflicts with previous declaration", newdecl))
2870 inform (olddecl_loc,
2871 "previous declaration of %qD", olddecl);
2872 }
2873 /* Choose the declaration which specified visibility. */
2874 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2875 {
2876 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2877 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2878 }
2879 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2880 so keep this behavior. */
2881 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2882 {
2883 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2884 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2885 }
2886 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2887 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2888 {
2889 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2890 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2891 }
2892 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2893 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2894 DECL_USER_ALIGN (newdecl) = 1;
2895
2896 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2897 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2898 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2899 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2900 DECL_WARN_IF_NOT_ALIGN (olddecl));
2901 if (TREE_CODE (newdecl) == FIELD_DECL)
2902 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2903
2904 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2905 with that from NEWDECL below. */
2906 if (DECL_LANG_SPECIFIC (olddecl))
2907 {
2908 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl)
2909 != DECL_LANG_SPECIFIC (newdecl));
2910 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2911 }
2912
2913 /* Merge the USED information. */
2914 if (TREE_USED (olddecl))
2915 TREE_USED (newdecl) = 1;
2916 else if (TREE_USED (newdecl))
2917 TREE_USED (olddecl) = 1;
2918
2919 if (VAR_P (newdecl))
2920 {
2921 if (DECL_READ_P (olddecl))
2922 DECL_READ_P (newdecl) = 1;
2923 else if (DECL_READ_P (newdecl))
2924 DECL_READ_P (olddecl) = 1;
2925 }
2926
2927 if (DECL_PRESERVE_P (olddecl))
2928 DECL_PRESERVE_P (newdecl) = 1;
2929 else if (DECL_PRESERVE_P (newdecl))
2930 DECL_PRESERVE_P (olddecl) = 1;
2931
2932 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2933 to olddecl and deleted. */
2934 if (TREE_CODE (newdecl) == FUNCTION_DECL
2935 && DECL_FUNCTION_VERSIONED (olddecl))
2936 {
2937 /* Set the flag for newdecl so that it gets copied to olddecl. */
2938 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2939 /* newdecl will be purged after copying to olddecl and is no longer
2940 a version. */
2941 cgraph_node::delete_function_version_by_decl (newdecl);
2942 }
2943
2944 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2945 {
2946 int function_size;
2947 struct symtab_node *snode = symtab_node::get (olddecl);
2948
2949 function_size = sizeof (struct tree_decl_common);
2950
2951 memcpy ((char *) olddecl + sizeof (struct tree_common),
2952 (char *) newdecl + sizeof (struct tree_common),
2953 function_size - sizeof (struct tree_common));
2954
2955 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2956 (char *) newdecl + sizeof (struct tree_decl_common),
2957 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2958
2959 /* Preserve symtab node mapping. */
2960 olddecl->decl_with_vis.symtab_node = snode;
2961
2962 if (new_template_info)
2963 /* If newdecl is a template instantiation, it is possible that
2964 the following sequence of events has occurred:
2965
2966 o A friend function was declared in a class template. The
2967 class template was instantiated.
2968
2969 o The instantiation of the friend declaration was
2970 recorded on the instantiation list, and is newdecl.
2971
2972 o Later, however, instantiate_class_template called pushdecl
2973 on the newdecl to perform name injection. But, pushdecl in
2974 turn called duplicate_decls when it discovered that another
2975 declaration of a global function with the same name already
2976 existed.
2977
2978 o Here, in duplicate_decls, we decided to clobber newdecl.
2979
2980 If we're going to do that, we'd better make sure that
2981 olddecl, and not newdecl, is on the list of
2982 instantiations so that if we try to do the instantiation
2983 again we won't get the clobbered declaration. */
2984 reregister_specialization (newdecl,
2985 new_template_info,
2986 olddecl);
2987 }
2988 else
2989 {
2990 size_t size = tree_code_size (TREE_CODE (newdecl));
2991
2992 memcpy ((char *) olddecl + sizeof (struct tree_common),
2993 (char *) newdecl + sizeof (struct tree_common),
2994 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2995
2996 switch (TREE_CODE (newdecl))
2997 {
2998 case LABEL_DECL:
2999 case VAR_DECL:
3000 case RESULT_DECL:
3001 case PARM_DECL:
3002 case FIELD_DECL:
3003 case TYPE_DECL:
3004 case CONST_DECL:
3005 {
3006 struct symtab_node *snode = NULL;
3007
3008 if (VAR_P (newdecl)
3009 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
3010 || DECL_EXTERNAL (olddecl)))
3011 snode = symtab_node::get (olddecl);
3012 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3013 (char *) newdecl + sizeof (struct tree_decl_common),
3014 size - sizeof (struct tree_decl_common)
3015 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3016 if (VAR_P (newdecl))
3017 olddecl->decl_with_vis.symtab_node = snode;
3018 }
3019 break;
3020 default:
3021 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3022 (char *) newdecl + sizeof (struct tree_decl_common),
3023 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
3024 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3025 break;
3026 }
3027 }
3028
3029 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3030 {
3031 if (DECL_EXTERNAL (olddecl)
3032 || TREE_PUBLIC (olddecl)
3033 || TREE_STATIC (olddecl))
3034 {
3035 /* Merge the section attribute.
3036 We want to issue an error if the sections conflict but that must be
3037 done later in decl_attributes since we are called before attributes
3038 are assigned. */
3039 if (DECL_SECTION_NAME (newdecl) != NULL)
3040 set_decl_section_name (olddecl, newdecl);
3041
3042 if (DECL_ONE_ONLY (newdecl))
3043 {
3044 struct symtab_node *oldsym, *newsym;
3045 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3046 oldsym = cgraph_node::get_create (olddecl);
3047 else
3048 oldsym = varpool_node::get_create (olddecl);
3049 newsym = symtab_node::get (newdecl);
3050 oldsym->set_comdat_group (newsym->get_comdat_group ());
3051 }
3052 }
3053
3054 if (VAR_P (newdecl)
3055 && CP_DECL_THREAD_LOCAL_P (newdecl))
3056 {
3057 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
3058 if (!processing_template_decl)
3059 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3060 }
3061 }
3062
3063 DECL_UID (olddecl) = olddecl_uid;
3064
3065 /* NEWDECL contains the merged attribute lists.
3066 Update OLDDECL to be the same. */
3067 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3068
3069 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3070 so that encode_section_info has a chance to look at the new decl
3071 flags and attributes. */
3072 if (DECL_RTL_SET_P (olddecl)
3073 && (TREE_CODE (olddecl) == FUNCTION_DECL
3074 || (VAR_P (olddecl)
3075 && TREE_STATIC (olddecl))))
3076 make_decl_rtl (olddecl);
3077
3078 /* The NEWDECL will no longer be needed. Because every out-of-class
3079 declaration of a member results in a call to duplicate_decls,
3080 freeing these nodes represents in a significant savings.
3081
3082 Before releasing the node, be sore to remove function from symbol
3083 table that might have been inserted there to record comdat group.
3084 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3085 structure is shared in between newdecl and oldecl. */
3086 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3087 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3088 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3089 {
3090 struct symtab_node *snode = symtab_node::get (newdecl);
3091 if (snode)
3092 snode->remove ();
3093 }
3094
3095 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3096 {
3097 tree clone;
3098 FOR_EACH_CLONE (clone, olddecl)
3099 {
3100 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl);
3101 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl);
3102 }
3103 }
3104
3105 /* Remove the associated constraints for newdecl, if any, before
3106 reclaiming memory. */
3107 if (flag_concepts)
3108 remove_constraints (newdecl);
3109
3110 ggc_free (newdecl);
3111
3112 return olddecl;
3113 }
3114
3115 /* Return zero if the declaration NEWDECL is valid
3116 when the declaration OLDDECL (assumed to be for the same name)
3117 has already been seen.
3118 Otherwise return an error message format string with a %s
3119 where the identifier should go. */
3120
3121 static const char *
redeclaration_error_message(tree newdecl,tree olddecl)3122 redeclaration_error_message (tree newdecl, tree olddecl)
3123 {
3124 if (TREE_CODE (newdecl) == TYPE_DECL)
3125 {
3126 /* Because C++ can put things into name space for free,
3127 constructs like "typedef struct foo { ... } foo"
3128 would look like an erroneous redeclaration. */
3129 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3130 return NULL;
3131 else
3132 return G_("redefinition of %q#D");
3133 }
3134 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3135 {
3136 /* If this is a pure function, its olddecl will actually be
3137 the original initialization to `0' (which we force to call
3138 abort()). Don't complain about redefinition in this case. */
3139 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
3140 && DECL_INITIAL (olddecl) == NULL_TREE)
3141 return NULL;
3142
3143 /* If both functions come from different namespaces, this is not
3144 a redeclaration - this is a conflict with a used function. */
3145 if (DECL_NAMESPACE_SCOPE_P (olddecl)
3146 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
3147 && ! decls_match (olddecl, newdecl))
3148 return G_("%qD conflicts with used function");
3149
3150 /* We'll complain about linkage mismatches in
3151 warn_extern_redeclared_static. */
3152
3153 /* Defining the same name twice is no good. */
3154 if (decl_defined_p (olddecl)
3155 && decl_defined_p (newdecl))
3156 {
3157 if (DECL_NAME (olddecl) == NULL_TREE)
3158 return G_("%q#D not declared in class");
3159 else if (!GNU_INLINE_P (olddecl)
3160 || GNU_INLINE_P (newdecl))
3161 return G_("redefinition of %q#D");
3162 }
3163
3164 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
3165 {
3166 bool olda = GNU_INLINE_P (olddecl);
3167 bool newa = GNU_INLINE_P (newdecl);
3168
3169 if (olda != newa)
3170 {
3171 if (newa)
3172 return G_("%q+D redeclared inline with "
3173 "%<gnu_inline%> attribute");
3174 else
3175 return G_("%q+D redeclared inline without "
3176 "%<gnu_inline%> attribute");
3177 }
3178 }
3179
3180 if (deduction_guide_p (olddecl)
3181 && deduction_guide_p (newdecl))
3182 return G_("deduction guide %q+D redeclared");
3183
3184 /* [class.compare.default]: A definition of a comparison operator as
3185 defaulted that appears in a class shall be the first declaration of
3186 that function. */
3187 special_function_kind sfk = special_function_p (olddecl);
3188 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
3189 return G_("comparison operator %q+D defaulted after "
3190 "its first declaration");
3191
3192 check_abi_tag_redeclaration
3193 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
3194 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
3195
3196 return NULL;
3197 }
3198 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3199 {
3200 tree nt, ot;
3201
3202 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
3203 return G_("redefinition of %q#D");
3204
3205 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
3206 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
3207 DECL_TEMPLATE_RESULT (olddecl));
3208
3209 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
3210 return NULL;
3211
3212 nt = DECL_TEMPLATE_RESULT (newdecl);
3213 if (DECL_TEMPLATE_INFO (nt))
3214 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
3215 ot = DECL_TEMPLATE_RESULT (olddecl);
3216 if (DECL_TEMPLATE_INFO (ot))
3217 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
3218 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
3219 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
3220 return G_("redefinition of %q#D");
3221
3222 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3223 {
3224 bool olda = GNU_INLINE_P (ot);
3225 bool newa = GNU_INLINE_P (nt);
3226
3227 if (olda != newa)
3228 {
3229 if (newa)
3230 return G_("%q+D redeclared inline with "
3231 "%<gnu_inline%> attribute");
3232 else
3233 return G_("%q+D redeclared inline without "
3234 "%<gnu_inline%> attribute");
3235 }
3236 }
3237
3238 if (deduction_guide_p (olddecl)
3239 && deduction_guide_p (newdecl))
3240 return G_("deduction guide %q+D redeclared");
3241
3242 /* Core issue #226 (C++11):
3243
3244 If a friend function template declaration specifies a
3245 default template-argument, that declaration shall be a
3246 definition and shall be the only declaration of the
3247 function template in the translation unit. */
3248 if ((cxx_dialect != cxx98)
3249 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot)
3250 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3251 /*is_primary=*/true,
3252 /*is_partial=*/false,
3253 /*is_friend_decl=*/2))
3254 return G_("redeclaration of friend %q#D "
3255 "may not have default template arguments");
3256
3257 return NULL;
3258 }
3259 else if (VAR_P (newdecl)
3260 && (CP_DECL_THREAD_LOCAL_P (newdecl)
3261 != CP_DECL_THREAD_LOCAL_P (olddecl))
3262 && (! DECL_LANG_SPECIFIC (olddecl)
3263 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3264 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3265 {
3266 /* Only variables can be thread-local, and all declarations must
3267 agree on this property. */
3268 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3269 return G_("thread-local declaration of %q#D follows "
3270 "non-thread-local declaration");
3271 else
3272 return G_("non-thread-local declaration of %q#D follows "
3273 "thread-local declaration");
3274 }
3275 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3276 {
3277 /* The objects have been declared at namespace scope. If either
3278 is a member of an anonymous union, then this is an invalid
3279 redeclaration. For example:
3280
3281 int i;
3282 union { int i; };
3283
3284 is invalid. */
3285 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3286 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3287 return G_("redeclaration of %q#D");
3288 /* If at least one declaration is a reference, there is no
3289 conflict. For example:
3290
3291 int i = 3;
3292 extern int i;
3293
3294 is valid. */
3295 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3296 return NULL;
3297
3298 /* Static data member declared outside a class definition
3299 if the variable is defined within the class with constexpr
3300 specifier is declaration rather than definition (and
3301 deprecated). */
3302 if (cxx_dialect >= cxx17
3303 && VAR_P (olddecl)
3304 && DECL_CLASS_SCOPE_P (olddecl)
3305 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3306 && !DECL_INITIAL (newdecl))
3307 {
3308 DECL_EXTERNAL (newdecl) = 1;
3309 /* For now, only warn with explicit -Wdeprecated. */
3310 if (OPTION_SET_P (warn_deprecated))
3311 {
3312 auto_diagnostic_group d;
3313 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3314 "redundant redeclaration of %<constexpr%> "
3315 "static data member %qD", newdecl))
3316 inform (DECL_SOURCE_LOCATION (olddecl),
3317 "previous declaration of %qD", olddecl);
3318 }
3319 return NULL;
3320 }
3321
3322 /* Reject two definitions. */
3323 return G_("redefinition of %q#D");
3324 }
3325 else
3326 {
3327 /* Objects declared with block scope: */
3328 /* Reject two definitions, and reject a definition
3329 together with an external reference. */
3330 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3331 return G_("redeclaration of %q#D");
3332 return NULL;
3333 }
3334 }
3335
3336
3337 /* Hash and equality functions for the named_label table. */
3338
3339 hashval_t
hash(const value_type entry)3340 named_label_hash::hash (const value_type entry)
3341 {
3342 return IDENTIFIER_HASH_VALUE (entry->name);
3343 }
3344
3345 bool
equal(const value_type entry,compare_type name)3346 named_label_hash::equal (const value_type entry, compare_type name)
3347 {
3348 return name == entry->name;
3349 }
3350
3351 /* Look for a label named ID in the current function. If one cannot
3352 be found, create one. Return the named_label_entry, or NULL on
3353 failure. */
3354
3355 static named_label_entry *
lookup_label_1(tree id,bool making_local_p)3356 lookup_label_1 (tree id, bool making_local_p)
3357 {
3358 auto_cond_timevar tv (TV_NAME_LOOKUP);
3359
3360 /* You can't use labels at global scope. */
3361 if (current_function_decl == NULL_TREE)
3362 {
3363 error ("label %qE referenced outside of any function", id);
3364 return NULL;
3365 }
3366
3367 if (!named_labels)
3368 named_labels = hash_table<named_label_hash>::create_ggc (13);
3369
3370 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3371 named_label_entry **slot
3372 = named_labels->find_slot_with_hash (id, hash, INSERT);
3373 named_label_entry *old = *slot;
3374
3375 if (old && old->label_decl)
3376 {
3377 if (!making_local_p)
3378 return old;
3379
3380 if (old->binding_level == current_binding_level)
3381 {
3382 error ("local label %qE conflicts with existing label", id);
3383 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3384 return NULL;
3385 }
3386 }
3387
3388 /* We are making a new decl, create or reuse the named_label_entry */
3389 named_label_entry *ent = NULL;
3390 if (old && !old->label_decl)
3391 ent = old;
3392 else
3393 {
3394 ent = ggc_cleared_alloc<named_label_entry> ();
3395 ent->name = id;
3396 ent->outer = old;
3397 *slot = ent;
3398 }
3399
3400 /* Now create the LABEL_DECL. */
3401 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3402
3403 DECL_CONTEXT (decl) = current_function_decl;
3404 SET_DECL_MODE (decl, VOIDmode);
3405 if (making_local_p)
3406 {
3407 C_DECLARED_LABEL_FLAG (decl) = true;
3408 DECL_CHAIN (decl) = current_binding_level->names;
3409 current_binding_level->names = decl;
3410 }
3411
3412 ent->label_decl = decl;
3413
3414 return ent;
3415 }
3416
3417 /* Wrapper for lookup_label_1. */
3418
3419 tree
lookup_label(tree id)3420 lookup_label (tree id)
3421 {
3422 named_label_entry *ent = lookup_label_1 (id, false);
3423 return ent ? ent->label_decl : NULL_TREE;
3424 }
3425
3426 tree
declare_local_label(tree id)3427 declare_local_label (tree id)
3428 {
3429 named_label_entry *ent = lookup_label_1 (id, true);
3430 return ent ? ent->label_decl : NULL_TREE;
3431 }
3432
3433 /* Returns nonzero if it is ill-formed to jump past the declaration of
3434 DECL. Returns 2 if it's also a real problem. */
3435
3436 static int
decl_jump_unsafe(tree decl)3437 decl_jump_unsafe (tree decl)
3438 {
3439 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3440 with automatic storage duration is not in scope to a point where it is
3441 in scope is ill-formed unless the variable has scalar type, class type
3442 with a trivial default constructor and a trivial destructor, a
3443 cv-qualified version of one of these types, or an array of one of the
3444 preceding types and is declared without an initializer (8.5). */
3445 tree type = TREE_TYPE (decl);
3446
3447 if (!VAR_P (decl) || TREE_STATIC (decl)
3448 || type == error_mark_node)
3449 return 0;
3450
3451 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3452 || variably_modified_type_p (type, NULL_TREE))
3453 return 2;
3454
3455 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3456 return 1;
3457
3458 return 0;
3459 }
3460
3461 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3462 to the user. */
3463
3464 static bool
identify_goto(tree decl,location_t loc,const location_t * locus,diagnostic_t diag_kind)3465 identify_goto (tree decl, location_t loc, const location_t *locus,
3466 diagnostic_t diag_kind)
3467 {
3468 bool complained
3469 = emit_diagnostic (diag_kind, loc, 0,
3470 decl ? N_("jump to label %qD")
3471 : N_("jump to case label"), decl);
3472 if (complained && locus)
3473 inform (*locus, " from here");
3474 return complained;
3475 }
3476
3477 /* Check that a single previously seen jump to a newly defined label
3478 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3479 the jump context; NAMES are the names in scope in LEVEL at the jump
3480 context; LOCUS is the source position of the jump or 0. Returns
3481 true if all is well. */
3482
3483 static bool
check_previous_goto_1(tree decl,cp_binding_level * level,tree names,bool exited_omp,const location_t * locus)3484 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3485 bool exited_omp, const location_t *locus)
3486 {
3487 cp_binding_level *b;
3488 bool complained = false;
3489 int identified = 0;
3490 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3491 bool saw_ceif = false;
3492
3493 if (exited_omp)
3494 {
3495 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3496 if (complained)
3497 inform (input_location, " exits OpenMP structured block");
3498 saw_omp = true;
3499 identified = 2;
3500 }
3501
3502 for (b = current_binding_level; b ; b = b->level_chain)
3503 {
3504 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3505
3506 for (new_decls = b->names; new_decls != old_decls;
3507 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3508 : TREE_CHAIN (new_decls)))
3509 {
3510 int problem = decl_jump_unsafe (new_decls);
3511 if (! problem)
3512 continue;
3513
3514 if (!identified)
3515 {
3516 complained = identify_goto (decl, input_location, locus,
3517 problem > 1
3518 ? DK_ERROR : DK_PERMERROR);
3519 identified = 1;
3520 }
3521 if (complained)
3522 {
3523 if (problem > 1)
3524 inform (DECL_SOURCE_LOCATION (new_decls),
3525 " crosses initialization of %q#D", new_decls);
3526 else
3527 inform (DECL_SOURCE_LOCATION (new_decls),
3528 " enters scope of %q#D, which has "
3529 "non-trivial destructor", new_decls);
3530 }
3531 }
3532
3533 if (b == level)
3534 break;
3535
3536 const char *inf = NULL;
3537 location_t loc = input_location;
3538 switch (b->kind)
3539 {
3540 case sk_try:
3541 if (!saw_eh)
3542 inf = G_(" enters %<try%> block");
3543 saw_eh = true;
3544 break;
3545
3546 case sk_catch:
3547 if (!saw_eh)
3548 inf = G_(" enters %<catch%> block");
3549 saw_eh = true;
3550 break;
3551
3552 case sk_omp:
3553 if (!saw_omp)
3554 inf = G_(" enters OpenMP structured block");
3555 saw_omp = true;
3556 break;
3557
3558 case sk_transaction:
3559 if (!saw_tm)
3560 inf = G_(" enters synchronized or atomic statement");
3561 saw_tm = true;
3562 break;
3563
3564 case sk_block:
3565 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3566 {
3567 inf = G_(" enters %<constexpr if%> statement");
3568 loc = EXPR_LOCATION (b->level_chain->this_entity);
3569 saw_cxif = true;
3570 }
3571 else if (!saw_ceif && level_for_consteval_if (b->level_chain))
3572 {
3573 inf = G_(" enters %<consteval if%> statement");
3574 loc = EXPR_LOCATION (b->level_chain->this_entity);
3575 saw_ceif = true;
3576 }
3577 break;
3578
3579 default:
3580 break;
3581 }
3582
3583 if (inf)
3584 {
3585 if (identified < 2)
3586 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3587 identified = 2;
3588 if (complained)
3589 inform (loc, inf);
3590 }
3591 }
3592
3593 return !identified;
3594 }
3595
3596 static void
check_previous_goto(tree decl,struct named_label_use_entry * use)3597 check_previous_goto (tree decl, struct named_label_use_entry *use)
3598 {
3599 check_previous_goto_1 (decl, use->binding_level,
3600 use->names_in_scope, use->in_omp_scope,
3601 &use->o_goto_locus);
3602 }
3603
3604 static bool
check_switch_goto(cp_binding_level * level)3605 check_switch_goto (cp_binding_level* level)
3606 {
3607 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3608 }
3609
3610 /* Check that a new jump to a label DECL is OK. Called by
3611 finish_goto_stmt. */
3612
3613 void
check_goto(tree decl)3614 check_goto (tree decl)
3615 {
3616 /* We can't know where a computed goto is jumping.
3617 So we assume that it's OK. */
3618 if (TREE_CODE (decl) != LABEL_DECL)
3619 return;
3620
3621 /* We didn't record any information about this label when we created it,
3622 and there's not much point since it's trivial to analyze as a return. */
3623 if (decl == cdtor_label)
3624 return;
3625
3626 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3627 named_label_entry **slot
3628 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3629 named_label_entry *ent = *slot;
3630
3631 /* If the label hasn't been defined yet, defer checking. */
3632 if (! DECL_INITIAL (decl))
3633 {
3634 /* Don't bother creating another use if the last goto had the
3635 same data, and will therefore create the same set of errors. */
3636 if (ent->uses
3637 && ent->uses->names_in_scope == current_binding_level->names)
3638 return;
3639
3640 named_label_use_entry *new_use
3641 = ggc_alloc<named_label_use_entry> ();
3642 new_use->binding_level = current_binding_level;
3643 new_use->names_in_scope = current_binding_level->names;
3644 new_use->o_goto_locus = input_location;
3645 new_use->in_omp_scope = false;
3646
3647 new_use->next = ent->uses;
3648 ent->uses = new_use;
3649 return;
3650 }
3651
3652 bool saw_catch = false, complained = false;
3653 int identified = 0;
3654 tree bad;
3655 unsigned ix;
3656
3657 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3658 || ent->in_constexpr_if || ent->in_consteval_if
3659 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3660 {
3661 diagnostic_t diag_kind = DK_PERMERROR;
3662 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3663 || ent->in_consteval_if || ent->in_transaction_scope
3664 || ent->in_omp_scope)
3665 diag_kind = DK_ERROR;
3666 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3667 &input_location, diag_kind);
3668 identified = 1 + (diag_kind == DK_ERROR);
3669 }
3670
3671 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3672 {
3673 int u = decl_jump_unsafe (bad);
3674
3675 if (u > 1 && DECL_ARTIFICIAL (bad))
3676 {
3677 /* Can't skip init of __exception_info. */
3678 if (identified == 1)
3679 {
3680 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3681 &input_location, DK_ERROR);
3682 identified = 2;
3683 }
3684 if (complained)
3685 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3686 saw_catch = true;
3687 }
3688 else if (complained)
3689 {
3690 if (u > 1)
3691 inform (DECL_SOURCE_LOCATION (bad),
3692 " skips initialization of %q#D", bad);
3693 else
3694 inform (DECL_SOURCE_LOCATION (bad),
3695 " enters scope of %q#D which has "
3696 "non-trivial destructor", bad);
3697 }
3698 }
3699
3700 if (complained)
3701 {
3702 if (ent->in_try_scope)
3703 inform (input_location, " enters %<try%> block");
3704 else if (ent->in_catch_scope && !saw_catch)
3705 inform (input_location, " enters %<catch%> block");
3706 else if (ent->in_transaction_scope)
3707 inform (input_location, " enters synchronized or atomic statement");
3708 else if (ent->in_constexpr_if)
3709 inform (input_location, " enters %<constexpr if%> statement");
3710 else if (ent->in_consteval_if)
3711 inform (input_location, " enters %<consteval if%> statement");
3712 }
3713
3714 if (ent->in_omp_scope)
3715 {
3716 if (complained)
3717 inform (input_location, " enters OpenMP structured block");
3718 }
3719 else if (flag_openmp)
3720 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3721 {
3722 if (b == ent->binding_level)
3723 break;
3724 if (b->kind == sk_omp)
3725 {
3726 if (identified < 2)
3727 {
3728 complained = identify_goto (decl,
3729 DECL_SOURCE_LOCATION (decl),
3730 &input_location, DK_ERROR);
3731 identified = 2;
3732 }
3733 if (complained)
3734 inform (input_location, " exits OpenMP structured block");
3735 break;
3736 }
3737 }
3738 }
3739
3740 /* Check that a return is ok wrt OpenMP structured blocks.
3741 Called by finish_return_stmt. Returns true if all is well. */
3742
3743 bool
check_omp_return(void)3744 check_omp_return (void)
3745 {
3746 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3747 if (b->kind == sk_omp)
3748 {
3749 error ("invalid exit from OpenMP structured block");
3750 return false;
3751 }
3752 else if (b->kind == sk_function_parms)
3753 break;
3754 return true;
3755 }
3756
3757 /* Define a label, specifying the location in the source file.
3758 Return the LABEL_DECL node for the label. */
3759
3760 tree
define_label(location_t location,tree name)3761 define_label (location_t location, tree name)
3762 {
3763 auto_cond_timevar tv (TV_NAME_LOOKUP);
3764
3765 /* After labels, make any new cleanups in the function go into their
3766 own new (temporary) binding contour. */
3767 for (cp_binding_level *p = current_binding_level;
3768 p->kind != sk_function_parms;
3769 p = p->level_chain)
3770 p->more_cleanups_ok = 0;
3771
3772 named_label_entry *ent = lookup_label_1 (name, false);
3773 tree decl = ent->label_decl;
3774
3775 if (DECL_INITIAL (decl) != NULL_TREE)
3776 {
3777 error ("duplicate label %qD", decl);
3778 return error_mark_node;
3779 }
3780 else
3781 {
3782 /* Mark label as having been defined. */
3783 DECL_INITIAL (decl) = error_mark_node;
3784 /* Say where in the source. */
3785 DECL_SOURCE_LOCATION (decl) = location;
3786
3787 ent->binding_level = current_binding_level;
3788 ent->names_in_scope = current_binding_level->names;
3789
3790 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3791 check_previous_goto (decl, use);
3792 ent->uses = NULL;
3793 }
3794
3795 return decl;
3796 }
3797
3798 struct cp_switch
3799 {
3800 cp_binding_level *level;
3801 struct cp_switch *next;
3802 /* The SWITCH_STMT being built. */
3803 tree switch_stmt;
3804 /* A splay-tree mapping the low element of a case range to the high
3805 element, or NULL_TREE if there is no high element. Used to
3806 determine whether or not a new case label duplicates an old case
3807 label. We need a tree, rather than simply a hash table, because
3808 of the GNU case range extension. */
3809 splay_tree cases;
3810 /* Remember whether a default: case label has been seen. */
3811 bool has_default_p;
3812 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3813 bool break_stmt_seen_p;
3814 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3815 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3816 bool in_loop_body_p;
3817 };
3818
3819 /* A stack of the currently active switch statements. The innermost
3820 switch statement is on the top of the stack. There is no need to
3821 mark the stack for garbage collection because it is only active
3822 during the processing of the body of a function, and we never
3823 collect at that point. */
3824
3825 static struct cp_switch *switch_stack;
3826
3827 /* Called right after a switch-statement condition is parsed.
3828 SWITCH_STMT is the switch statement being parsed. */
3829
3830 void
push_switch(tree switch_stmt)3831 push_switch (tree switch_stmt)
3832 {
3833 struct cp_switch *p = XNEW (struct cp_switch);
3834 p->level = current_binding_level;
3835 p->next = switch_stack;
3836 p->switch_stmt = switch_stmt;
3837 p->cases = splay_tree_new (case_compare, NULL, NULL);
3838 p->has_default_p = false;
3839 p->break_stmt_seen_p = false;
3840 p->in_loop_body_p = false;
3841 switch_stack = p;
3842 }
3843
3844 void
pop_switch(void)3845 pop_switch (void)
3846 {
3847 struct cp_switch *cs = switch_stack;
3848
3849 /* Emit warnings as needed. */
3850 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3851 tree cond = SWITCH_STMT_COND (cs->switch_stmt);
3852 const bool bool_cond_p
3853 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3854 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3855 if (!processing_template_decl)
3856 c_do_switch_warnings (cs->cases, switch_location,
3857 SWITCH_STMT_TYPE (cs->switch_stmt), cond,
3858 bool_cond_p);
3859
3860 /* For the benefit of block_may_fallthru remember if the switch body
3861 case labels cover all possible values and if there are break; stmts. */
3862 if (cs->has_default_p
3863 || (!processing_template_decl
3864 && c_switch_covers_all_cases_p (cs->cases,
3865 SWITCH_STMT_TYPE (cs->switch_stmt))))
3866 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3867 if (!cs->break_stmt_seen_p)
3868 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3869 /* Now that we're done with the switch warnings, set the switch type
3870 to the type of the condition if the index type was of scoped enum type.
3871 (Such types don't participate in the integer promotions.) We do this
3872 because of bit-fields whose declared type is a scoped enum type:
3873 gimplification will use the lowered index type, but convert the
3874 case values to SWITCH_STMT_TYPE, which would have been the declared type
3875 and verify_gimple_switch doesn't accept that. */
3876 if (is_bitfield_expr_with_lowered_type (cond))
3877 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond);
3878 gcc_assert (!cs->in_loop_body_p);
3879 splay_tree_delete (cs->cases);
3880 switch_stack = switch_stack->next;
3881 free (cs);
3882 }
3883
3884 /* Note that a BREAK_STMT is about to be added. If it is inside of
3885 a SWITCH_STMT and not inside of a loop body inside of it, note
3886 in switch_stack we've seen a BREAK_STMT. */
3887
3888 void
note_break_stmt(void)3889 note_break_stmt (void)
3890 {
3891 if (switch_stack && !switch_stack->in_loop_body_p)
3892 switch_stack->break_stmt_seen_p = true;
3893 }
3894
3895 /* Note the start of processing of an iteration statement's body.
3896 The note_break_stmt function will do nothing while processing it.
3897 Return a flag that should be passed to note_iteration_stmt_body_end. */
3898
3899 bool
note_iteration_stmt_body_start(void)3900 note_iteration_stmt_body_start (void)
3901 {
3902 if (!switch_stack)
3903 return false;
3904 bool ret = switch_stack->in_loop_body_p;
3905 switch_stack->in_loop_body_p = true;
3906 return ret;
3907 }
3908
3909 /* Note the end of processing of an iteration statement's body. */
3910
3911 void
note_iteration_stmt_body_end(bool prev)3912 note_iteration_stmt_body_end (bool prev)
3913 {
3914 if (switch_stack)
3915 switch_stack->in_loop_body_p = prev;
3916 }
3917
3918 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3919 condition. Note that if TYPE and VALUE are already integral we don't
3920 really do the conversion because the language-independent
3921 warning/optimization code will work better that way. */
3922
3923 static tree
case_conversion(tree type,tree value)3924 case_conversion (tree type, tree value)
3925 {
3926 if (value == NULL_TREE)
3927 return value;
3928
3929 value = mark_rvalue_use (value);
3930
3931 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3932 type = type_promotes_to (type);
3933
3934 tree ovalue = value;
3935 /* The constant-expression VALUE shall be a converted constant expression
3936 of the adjusted type of the switch condition, which doesn't allow
3937 narrowing conversions. */
3938 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3939
3940 if (cxx_dialect >= cxx11
3941 && (SCOPED_ENUM_P (type)
3942 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3943 /* Use the converted value. */;
3944 else
3945 /* The already integral case. */
3946 value = ovalue;
3947
3948 return cxx_constant_value (value);
3949 }
3950
3951 /* Note that we've seen a definition of a case label, and complain if this
3952 is a bad place for one. */
3953
3954 tree
finish_case_label(location_t loc,tree low_value,tree high_value)3955 finish_case_label (location_t loc, tree low_value, tree high_value)
3956 {
3957 tree cond, r;
3958 cp_binding_level *p;
3959 tree type;
3960
3961 if (low_value == NULL_TREE && high_value == NULL_TREE)
3962 switch_stack->has_default_p = true;
3963
3964 if (processing_template_decl)
3965 {
3966 tree label;
3967
3968 /* For templates, just add the case label; we'll do semantic
3969 analysis at instantiation-time. */
3970 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3971 return add_stmt (build_case_label (low_value, high_value, label));
3972 }
3973
3974 /* Find the condition on which this switch statement depends. */
3975 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3976 if (cond && TREE_CODE (cond) == TREE_LIST)
3977 cond = TREE_VALUE (cond);
3978
3979 if (!check_switch_goto (switch_stack->level))
3980 return error_mark_node;
3981
3982 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3983 if (type == error_mark_node)
3984 return error_mark_node;
3985
3986 low_value = case_conversion (type, low_value);
3987 high_value = case_conversion (type, high_value);
3988
3989 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3990
3991 /* After labels, make any new cleanups in the function go into their
3992 own new (temporary) binding contour. */
3993 for (p = current_binding_level;
3994 p->kind != sk_function_parms;
3995 p = p->level_chain)
3996 p->more_cleanups_ok = 0;
3997
3998 return r;
3999 }
4000
4001 struct typename_info {
4002 tree scope;
4003 tree name;
4004 tree template_id;
4005 bool enum_p;
4006 bool class_p;
4007 };
4008
4009 struct typename_hasher : ggc_ptr_hash<tree_node>
4010 {
4011 typedef typename_info *compare_type;
4012
4013 /* Hash a TYPENAME_TYPE. */
4014
4015 static hashval_t
hashtypename_hasher4016 hash (tree t)
4017 {
4018 hashval_t hash;
4019
4020 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
4021 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
4022
4023 return hash;
4024 }
4025
4026 /* Compare two TYPENAME_TYPEs. */
4027
4028 static bool
equaltypename_hasher4029 equal (tree t1, const typename_info *t2)
4030 {
4031 return (TYPE_IDENTIFIER (t1) == t2->name
4032 && TYPE_CONTEXT (t1) == t2->scope
4033 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
4034 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
4035 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
4036 }
4037 };
4038
4039 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
4040 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
4041
4042 Returns the new TYPENAME_TYPE. */
4043
4044 static GTY (()) hash_table<typename_hasher> *typename_htab;
4045
4046 tree
build_typename_type(tree context,tree name,tree fullname,enum tag_types tag_type)4047 build_typename_type (tree context, tree name, tree fullname,
4048 enum tag_types tag_type)
4049 {
4050 typename_info ti;
4051
4052 if (typename_htab == NULL)
4053 typename_htab = hash_table<typename_hasher>::create_ggc (61);
4054
4055 ti.scope = FROB_CONTEXT (context);
4056 ti.name = name;
4057 ti.template_id = fullname;
4058 ti.enum_p = tag_type == enum_type;
4059 ti.class_p = (tag_type == class_type
4060 || tag_type == record_type
4061 || tag_type == union_type);
4062 hashval_t hash = (htab_hash_pointer (ti.scope)
4063 ^ htab_hash_pointer (ti.name));
4064
4065 /* See if we already have this type. */
4066 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
4067 tree t = *e;
4068 if (*e)
4069 t = *e;
4070 else
4071 {
4072 /* Build the TYPENAME_TYPE. */
4073 t = cxx_make_type (TYPENAME_TYPE);
4074 TYPE_CONTEXT (t) = ti.scope;
4075 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
4076 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
4077 TYPENAME_IS_CLASS_P (t) = ti.class_p;
4078
4079 /* Build the corresponding TYPE_DECL. */
4080 tree d = build_decl (input_location, TYPE_DECL, name, t);
4081 TYPE_NAME (t) = d;
4082 TYPE_STUB_DECL (t) = d;
4083 DECL_CONTEXT (d) = ti.scope;
4084 DECL_ARTIFICIAL (d) = 1;
4085
4086 /* Store it in the hash table. */
4087 *e = t;
4088
4089 /* TYPENAME_TYPEs must always be compared structurally, because
4090 they may or may not resolve down to another type depending on
4091 the currently open classes. */
4092 SET_TYPE_STRUCTURAL_EQUALITY (t);
4093 }
4094
4095 return t;
4096 }
4097
4098 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
4099 provided to name the type. Returns an appropriate type, unless an
4100 error occurs, in which case error_mark_node is returned. If we
4101 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
4102 return that, rather than the _TYPE it corresponds to, in other
4103 cases we look through the type decl. If TF_ERROR is set, complain
4104 about errors, otherwise be quiet. */
4105
4106 tree
make_typename_type(tree context,tree name,enum tag_types tag_type,tsubst_flags_t complain)4107 make_typename_type (tree context, tree name, enum tag_types tag_type,
4108 tsubst_flags_t complain)
4109 {
4110 tree fullname;
4111 tree t;
4112 bool want_template;
4113
4114 if (name == error_mark_node
4115 || context == NULL_TREE
4116 || context == error_mark_node)
4117 return error_mark_node;
4118
4119 if (TYPE_P (name))
4120 {
4121 if (!(TYPE_LANG_SPECIFIC (name)
4122 && (CLASSTYPE_IS_TEMPLATE (name)
4123 || CLASSTYPE_USE_TEMPLATE (name))))
4124 name = TYPE_IDENTIFIER (name);
4125 else
4126 /* Create a TEMPLATE_ID_EXPR for the type. */
4127 name = build_nt (TEMPLATE_ID_EXPR,
4128 CLASSTYPE_TI_TEMPLATE (name),
4129 CLASSTYPE_TI_ARGS (name));
4130 }
4131 else if (TREE_CODE (name) == TYPE_DECL)
4132 name = DECL_NAME (name);
4133
4134 fullname = name;
4135
4136 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4137 {
4138 name = TREE_OPERAND (name, 0);
4139 if (DECL_TYPE_TEMPLATE_P (name))
4140 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
4141 if (TREE_CODE (name) != IDENTIFIER_NODE)
4142 {
4143 if (complain & tf_error)
4144 error ("%qD is not a type", name);
4145 return error_mark_node;
4146 }
4147 }
4148 if (TREE_CODE (name) == TEMPLATE_DECL)
4149 {
4150 if (complain & tf_error)
4151 error ("%qD used without template arguments", name);
4152 return error_mark_node;
4153 }
4154 else if (is_overloaded_fn (name))
4155 {
4156 if (complain & tf_error)
4157 error ("%qD is a function, not a type", name);
4158 return error_mark_node;
4159 }
4160 gcc_assert (identifier_p (name));
4161 gcc_assert (TYPE_P (context));
4162
4163 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
4164 /* This can happen for C++17 variadic using (c++/88986). */;
4165 else if (!MAYBE_CLASS_TYPE_P (context))
4166 {
4167 if (complain & tf_error)
4168 error ("%q#T is not a class", context);
4169 return error_mark_node;
4170 }
4171
4172 /* When the CONTEXT is a dependent type, NAME could refer to a
4173 dependent base class of CONTEXT. But look inside it anyway
4174 if CONTEXT is a currently open scope, in case it refers to a
4175 member of the current instantiation or a non-dependent base;
4176 lookup will stop when we hit a dependent base. */
4177 if (!dependent_scope_p (context))
4178 /* We should only set WANT_TYPE when we're a nested typename type.
4179 Then we can give better diagnostics if we find a non-type. */
4180 t = lookup_field (context, name, 2, /*want_type=*/true);
4181 else
4182 t = NULL_TREE;
4183
4184 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
4185 return build_typename_type (context, name, fullname, tag_type);
4186
4187 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
4188
4189 if (!t)
4190 {
4191 if (complain & tf_error)
4192 {
4193 if (!COMPLETE_TYPE_P (context))
4194 cxx_incomplete_type_error (NULL_TREE, context);
4195 else
4196 error (want_template ? G_("no class template named %q#T in %q#T")
4197 : G_("no type named %q#T in %q#T"), name, context);
4198 }
4199 return error_mark_node;
4200 }
4201
4202 /* Pull out the template from an injected-class-name (or multiple). */
4203 if (want_template)
4204 t = maybe_get_template_decl_from_type_decl (t);
4205
4206 if (TREE_CODE (t) == TREE_LIST)
4207 {
4208 if (complain & tf_error)
4209 {
4210 error ("lookup of %qT in %qT is ambiguous", name, context);
4211 print_candidates (t);
4212 }
4213 return error_mark_node;
4214 }
4215
4216 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
4217 {
4218 if (complain & tf_error)
4219 error ("%<typename %T::%D%> names %q#T, which is not a class template",
4220 context, name, t);
4221 return error_mark_node;
4222 }
4223 if (!want_template && TREE_CODE (t) != TYPE_DECL)
4224 {
4225 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17
4226 && DECL_TYPE_TEMPLATE_P (t))
4227 /* The caller permits this typename-specifier to name a template
4228 (because it appears in a CTAD-enabled context). */;
4229 else
4230 {
4231 if (complain & tf_error)
4232 error ("%<typename %T::%D%> names %q#T, which is not a type",
4233 context, name, t);
4234 return error_mark_node;
4235 }
4236 }
4237
4238 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE,
4239 context, complain))
4240 return error_mark_node;
4241
4242 if (!want_template && DECL_TYPE_TEMPLATE_P (t))
4243 return make_template_placeholder (t);
4244
4245 if (want_template)
4246 {
4247 t = lookup_template_class (t, TREE_OPERAND (fullname, 1),
4248 NULL_TREE, context,
4249 /*entering_scope=*/0,
4250 complain | tf_user);
4251 if (t == error_mark_node)
4252 return error_mark_node;
4253 t = TYPE_NAME (t);
4254 }
4255
4256 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4257 t = TREE_TYPE (t);
4258
4259 maybe_record_typedef_use (t);
4260
4261 return t;
4262 }
4263
4264 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4265 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4266 in which case error_mark_node is returned.
4267
4268 If PARM_LIST is non-NULL, also make sure that the template parameter
4269 list of TEMPLATE_DECL matches.
4270
4271 If COMPLAIN zero, don't complain about any errors that occur. */
4272
4273 tree
make_unbound_class_template(tree context,tree name,tree parm_list,tsubst_flags_t complain)4274 make_unbound_class_template (tree context, tree name, tree parm_list,
4275 tsubst_flags_t complain)
4276 {
4277 if (TYPE_P (name))
4278 name = TYPE_IDENTIFIER (name);
4279 else if (DECL_P (name))
4280 name = DECL_NAME (name);
4281 gcc_assert (identifier_p (name));
4282
4283 if (!dependent_type_p (context)
4284 || currently_open_class (context))
4285 {
4286 tree tmpl = NULL_TREE;
4287
4288 if (MAYBE_CLASS_TYPE_P (context))
4289 tmpl = lookup_field (context, name, 0, false);
4290
4291 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4292 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4293
4294 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4295 {
4296 if (complain & tf_error)
4297 error ("no class template named %q#T in %q#T", name, context);
4298 return error_mark_node;
4299 }
4300
4301 if (parm_list
4302 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4303 {
4304 if (complain & tf_error)
4305 {
4306 error ("template parameters do not match template %qD", tmpl);
4307 inform (DECL_SOURCE_LOCATION (tmpl),
4308 "%qD declared here", tmpl);
4309 }
4310 return error_mark_node;
4311 }
4312
4313 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4314 complain))
4315 return error_mark_node;
4316
4317 return tmpl;
4318 }
4319
4320 return make_unbound_class_template_raw (context, name, parm_list);
4321 }
4322
4323 /* Build an UNBOUND_CLASS_TEMPLATE. */
4324
4325 tree
make_unbound_class_template_raw(tree context,tree name,tree parm_list)4326 make_unbound_class_template_raw (tree context, tree name, tree parm_list)
4327 {
4328 /* Build the UNBOUND_CLASS_TEMPLATE. */
4329 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4330 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4331 TREE_TYPE (t) = NULL_TREE;
4332 SET_TYPE_STRUCTURAL_EQUALITY (t);
4333
4334 /* Build the corresponding TEMPLATE_DECL. */
4335 tree d = build_decl (input_location, TEMPLATE_DECL, name, t);
4336 TYPE_NAME (t) = d;
4337 TYPE_STUB_DECL (t) = d;
4338 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
4339 DECL_ARTIFICIAL (d) = 1;
4340 DECL_TEMPLATE_PARMS (d) = parm_list;
4341
4342 return t;
4343 }
4344
4345
4346
4347 /* Push the declarations of builtin types into the global namespace.
4348 RID_INDEX is the index of the builtin type in the array
4349 RID_POINTERS. NAME is the name used when looking up the builtin
4350 type. TYPE is the _TYPE node for the builtin type.
4351
4352 The calls to set_global_binding below should be
4353 eliminated. Built-in types should not be looked up name; their
4354 names are keywords that the parser can recognize. However, there
4355 is code in c-common.cc that uses identifier_global_value to look up
4356 built-in types by name. */
4357
4358 void
record_builtin_type(enum rid rid_index,const char * name,tree type)4359 record_builtin_type (enum rid rid_index,
4360 const char* name,
4361 tree type)
4362 {
4363 tree decl = NULL_TREE;
4364
4365 if (name)
4366 {
4367 tree tname = get_identifier (name);
4368 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4369 DECL_ARTIFICIAL (tdecl) = 1;
4370 set_global_binding (tdecl);
4371 decl = tdecl;
4372 }
4373
4374 if ((int) rid_index < (int) RID_MAX)
4375 if (tree rname = ridpointers[(int) rid_index])
4376 if (!decl || DECL_NAME (decl) != rname)
4377 {
4378 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4379 DECL_ARTIFICIAL (rdecl) = 1;
4380 set_global_binding (rdecl);
4381 if (!decl)
4382 decl = rdecl;
4383 }
4384
4385 if (decl)
4386 {
4387 if (!TYPE_NAME (type))
4388 TYPE_NAME (type) = decl;
4389 debug_hooks->type_decl (decl, 0);
4390 }
4391 }
4392
4393 /* Push a type into the namespace so that the back ends ignore it. */
4394
4395 static void
record_unknown_type(tree type,const char * name)4396 record_unknown_type (tree type, const char* name)
4397 {
4398 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4399 TYPE_DECL, get_identifier (name), type));
4400 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4401 DECL_IGNORED_P (decl) = 1;
4402 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4403 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4404 SET_TYPE_ALIGN (type, 1);
4405 TYPE_USER_ALIGN (type) = 0;
4406 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4407 }
4408
4409 /* Create all the predefined identifiers. */
4410
4411 static void
initialize_predefined_identifiers(void)4412 initialize_predefined_identifiers (void)
4413 {
4414 struct predefined_identifier
4415 {
4416 const char *name; /* Name. */
4417 tree *node; /* Node to store it in. */
4418 cp_identifier_kind kind; /* Kind of identifier. */
4419 };
4420
4421 /* A table of identifiers to create at startup. */
4422 static const predefined_identifier predefined_identifiers[] = {
4423 {"C++", &lang_name_cplusplus, cik_normal},
4424 {"C", &lang_name_c, cik_normal},
4425 /* Some of these names have a trailing space so that it is
4426 impossible for them to conflict with names written by users. */
4427 {"__ct ", &ctor_identifier, cik_ctor},
4428 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4429 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4430 {"__dt ", &dtor_identifier, cik_dtor},
4431 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4432 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4433 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4434 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4435 {"__in_chrg", &in_charge_identifier, cik_normal},
4436 {"__as_base ", &as_base_identifier, cik_normal},
4437 {"this", &this_identifier, cik_normal},
4438 {"__delta", &delta_identifier, cik_normal},
4439 {"__pfn", &pfn_identifier, cik_normal},
4440 {"_vptr", &vptr_identifier, cik_normal},
4441 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4442 {"::", &global_identifier, cik_normal},
4443 /* The demangler expects anonymous namespaces to be called
4444 something starting with '_GLOBAL__N_'. It no longer needs
4445 to be unique to the TU. */
4446 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4447 {"auto", &auto_identifier, cik_normal},
4448 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4449 {"initializer_list", &init_list_identifier, cik_normal},
4450 {"__for_range ", &for_range__identifier, cik_normal},
4451 {"__for_begin ", &for_begin__identifier, cik_normal},
4452 {"__for_end ", &for_end__identifier, cik_normal},
4453 {"__for_range", &for_range_identifier, cik_normal},
4454 {"__for_begin", &for_begin_identifier, cik_normal},
4455 {"__for_end", &for_end_identifier, cik_normal},
4456 {"abi_tag", &abi_tag_identifier, cik_normal},
4457 {"aligned", &aligned_identifier, cik_normal},
4458 {"begin", &begin_identifier, cik_normal},
4459 {"end", &end_identifier, cik_normal},
4460 {"get", &get__identifier, cik_normal},
4461 {"gnu", &gnu_identifier, cik_normal},
4462 {"tuple_element", &tuple_element_identifier, cik_normal},
4463 {"tuple_size", &tuple_size_identifier, cik_normal},
4464 {"type", &type_identifier, cik_normal},
4465 {"value", &value_identifier, cik_normal},
4466 {"_FUN", &fun_identifier, cik_normal},
4467 {"__closure", &closure_identifier, cik_normal},
4468 {"heap uninit", &heap_uninit_identifier, cik_normal},
4469 {"heap ", &heap_identifier, cik_normal},
4470 {"heap deleted", &heap_deleted_identifier, cik_normal},
4471 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal},
4472 {"heap []", &heap_vec_identifier, cik_normal},
4473 {"omp", &omp_identifier, cik_normal},
4474 {NULL, NULL, cik_normal}
4475 };
4476
4477 for (const predefined_identifier *pid = predefined_identifiers;
4478 pid->name; ++pid)
4479 {
4480 *pid->node = get_identifier (pid->name);
4481 /* Some of these identifiers already have a special kind. */
4482 if (pid->kind != cik_normal)
4483 set_identifier_kind (*pid->node, pid->kind);
4484 }
4485 }
4486
4487 /* Create the predefined scalar types of C,
4488 and some nodes representing standard constants (0, 1, (void *)0).
4489 Initialize the global binding level.
4490 Make definitions for built-in primitive functions. */
4491
4492 void
cxx_init_decl_processing(void)4493 cxx_init_decl_processing (void)
4494 {
4495 tree void_ftype;
4496 tree void_ftype_ptr;
4497
4498 /* Create all the identifiers we need. */
4499 initialize_predefined_identifiers ();
4500
4501 /* Create the global variables. */
4502 push_to_top_level ();
4503
4504 current_function_decl = NULL_TREE;
4505 current_binding_level = NULL;
4506 /* Enter the global namespace. */
4507 gcc_assert (global_namespace == NULL_TREE);
4508 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4509 void_type_node);
4510 TREE_PUBLIC (global_namespace) = true;
4511 DECL_MODULE_EXPORT_P (global_namespace) = true;
4512 DECL_CONTEXT (global_namespace)
4513 = build_translation_unit_decl (get_identifier (main_input_filename));
4514 /* Remember whether we want the empty class passing ABI change warning
4515 in this TU. */
4516 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4517 = warn_abi && abi_version_crosses (12);
4518 debug_hooks->register_main_translation_unit
4519 (DECL_CONTEXT (global_namespace));
4520 begin_scope (sk_namespace, global_namespace);
4521 current_namespace = global_namespace;
4522
4523 if (flag_visibility_ms_compat)
4524 default_visibility = VISIBILITY_HIDDEN;
4525
4526 /* Initially, C. */
4527 current_lang_name = lang_name_c;
4528
4529 /* Create the `std' namespace. */
4530 push_namespace (get_identifier ("std"));
4531 std_node = current_namespace;
4532 pop_namespace ();
4533
4534 flag_noexcept_type = (cxx_dialect >= cxx17);
4535
4536 c_common_nodes_and_builtins ();
4537
4538 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4539 tree decl
4540 = add_builtin_function ("__builtin_is_constant_evaluated",
4541 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4542 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4543 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4544
4545 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4546 decl = add_builtin_function ("__builtin_source_location",
4547 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4548 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4549 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4550
4551 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node,
4552 NULL_TREE);
4553 decl
4554 = add_builtin_function ("__builtin_is_corresponding_member",
4555 bool_vaftype,
4556 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
4557 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4558 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4559
4560 decl
4561 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class",
4562 bool_vaftype,
4563 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
4564 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4565 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4566
4567 integer_two_node = build_int_cst (NULL_TREE, 2);
4568
4569 /* Guess at the initial static decls size. */
4570 vec_alloc (static_decls, 500);
4571
4572 /* ... and keyed classes. */
4573 vec_alloc (keyed_classes, 100);
4574
4575 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4576 truthvalue_type_node = boolean_type_node;
4577 truthvalue_false_node = boolean_false_node;
4578 truthvalue_true_node = boolean_true_node;
4579
4580 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4581 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4582 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4583 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4584 NULL_TREE);
4585
4586 #if 0
4587 record_builtin_type (RID_MAX, NULL, string_type_node);
4588 #endif
4589
4590 delta_type_node = ptrdiff_type_node;
4591 vtable_index_type = ptrdiff_type_node;
4592
4593 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4594 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4595 void_ftype_ptr = build_function_type_list (void_type_node,
4596 ptr_type_node, NULL_TREE);
4597 void_ftype_ptr
4598 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4599
4600 /* Create the conversion operator marker. This operator's DECL_NAME
4601 is in the identifier table, so we can use identifier equality to
4602 find it. */
4603 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4604 void_ftype);
4605
4606 /* C++ extensions */
4607
4608 unknown_type_node = make_node (LANG_TYPE);
4609 record_unknown_type (unknown_type_node, "unknown type");
4610
4611 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4612 TREE_TYPE (unknown_type_node) = unknown_type_node;
4613
4614 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4615 result. */
4616 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4617 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4618
4619 init_list_type_node = make_node (LANG_TYPE);
4620 record_unknown_type (init_list_type_node, "init list");
4621
4622 /* Used when parsing to distinguish parameter-lists () and (void). */
4623 explicit_void_list_node = build_void_list_node ();
4624
4625 {
4626 /* Make sure we get a unique function type, so we can give
4627 its pointer type a name. (This wins for gdb.) */
4628 tree vfunc_type = make_node (FUNCTION_TYPE);
4629 TREE_TYPE (vfunc_type) = integer_type_node;
4630 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4631 layout_type (vfunc_type);
4632
4633 vtable_entry_type = build_pointer_type (vfunc_type);
4634 }
4635 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4636
4637 vtbl_type_node
4638 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4639 layout_type (vtbl_type_node);
4640 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4641 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4642 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4643 layout_type (vtbl_ptr_type_node);
4644 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4645
4646 push_namespace (get_identifier ("__cxxabiv1"));
4647 abi_node = current_namespace;
4648 pop_namespace ();
4649
4650 any_targ_node = make_node (LANG_TYPE);
4651 record_unknown_type (any_targ_node, "any type");
4652
4653 /* Now, C++. */
4654 current_lang_name = lang_name_cplusplus;
4655
4656 if (aligned_new_threshold > 1
4657 && !pow2p_hwi (aligned_new_threshold))
4658 {
4659 error ("%<-faligned-new=%d%> is not a power of two",
4660 aligned_new_threshold);
4661 aligned_new_threshold = 1;
4662 }
4663 if (aligned_new_threshold == -1)
4664 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4665 if (aligned_new_threshold == 1)
4666 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4667
4668 {
4669 tree newattrs, extvisattr;
4670 tree newtype, deltype;
4671 tree ptr_ftype_sizetype;
4672 tree new_eh_spec;
4673
4674 ptr_ftype_sizetype
4675 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4676 if (cxx_dialect == cxx98)
4677 {
4678 tree bad_alloc_id;
4679 tree bad_alloc_type_node;
4680 tree bad_alloc_decl;
4681
4682 push_nested_namespace (std_node);
4683 bad_alloc_id = get_identifier ("bad_alloc");
4684 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4685 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4686 bad_alloc_decl
4687 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4688 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4689 pop_nested_namespace (std_node);
4690
4691 new_eh_spec
4692 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4693 }
4694 else
4695 new_eh_spec = noexcept_false_spec;
4696
4697 /* Ensure attribs.cc is initialized. */
4698 init_attributes ();
4699
4700 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4701 NULL_TREE);
4702 newattrs = tree_cons (get_identifier ("alloc_size"),
4703 build_tree_list (NULL_TREE, integer_one_node),
4704 extvisattr);
4705 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4706 newtype = build_exception_variant (newtype, new_eh_spec);
4707 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4708 deltype = build_exception_variant (deltype, empty_except_spec);
4709 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4710 DECL_IS_MALLOC (opnew) = 1;
4711 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4712 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4713 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4714 DECL_IS_MALLOC (opnew) = 1;
4715 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4716 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4717 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4718 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4719 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4720 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4721 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4722 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4723 if (flag_sized_deallocation)
4724 {
4725 /* Also push the sized deallocation variants:
4726 void operator delete(void*, std::size_t) throw();
4727 void operator delete[](void*, std::size_t) throw(); */
4728 tree void_ftype_ptr_size
4729 = build_function_type_list (void_type_node, ptr_type_node,
4730 size_type_node, NULL_TREE);
4731 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4732 extvisattr);
4733 deltype = build_exception_variant (deltype, empty_except_spec);
4734 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4735 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4736 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4737 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4738 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4739 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4740 }
4741
4742 if (aligned_new_threshold)
4743 {
4744 push_nested_namespace (std_node);
4745 tree align_id = get_identifier ("align_val_t");
4746 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4747 NULL_TREE, /*scoped*/true, NULL);
4748 pop_nested_namespace (std_node);
4749
4750 /* operator new (size_t, align_val_t); */
4751 newtype = build_function_type_list (ptr_type_node, size_type_node,
4752 align_type_node, NULL_TREE);
4753 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4754 newtype = build_exception_variant (newtype, new_eh_spec);
4755 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4756 DECL_IS_MALLOC (opnew) = 1;
4757 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4758 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4759 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4760 DECL_IS_MALLOC (opnew) = 1;
4761 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4762 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1;
4763
4764 /* operator delete (void *, align_val_t); */
4765 deltype = build_function_type_list (void_type_node, ptr_type_node,
4766 align_type_node, NULL_TREE);
4767 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4768 deltype = build_exception_variant (deltype, empty_except_spec);
4769 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4770 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4771 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4772 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4773 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4774 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4775
4776 if (flag_sized_deallocation)
4777 {
4778 /* operator delete (void *, size_t, align_val_t); */
4779 deltype = build_function_type_list (void_type_node, ptr_type_node,
4780 size_type_node, align_type_node,
4781 NULL_TREE);
4782 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4783 deltype = build_exception_variant (deltype, empty_except_spec);
4784 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4785 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4786 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4787 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4788 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4789 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1;
4790 }
4791 }
4792
4793 nullptr_type_node = make_node (NULLPTR_TYPE);
4794 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4795 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4796 TYPE_UNSIGNED (nullptr_type_node) = 1;
4797 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4798 if (abi_version_at_least (9))
4799 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4800 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4801 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4802 nullptr_node = build_int_cst (nullptr_type_node, 0);
4803 }
4804
4805 if (! supports_one_only ())
4806 flag_weak = 0;
4807
4808 abort_fndecl
4809 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4810 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4811 if (flag_weak)
4812 /* If no definition is available, resolve references to NULL. */
4813 declare_weak (abort_fndecl);
4814
4815 /* Perform other language dependent initializations. */
4816 init_class_processing ();
4817 init_rtti_processing ();
4818 init_template_processing ();
4819
4820 if (flag_exceptions)
4821 init_exception_processing ();
4822
4823 if (modules_p ())
4824 init_modules (parse_in);
4825
4826 make_fname_decl = cp_make_fname_decl;
4827 start_fname_decls ();
4828
4829 /* Show we use EH for cleanups. */
4830 if (flag_exceptions)
4831 using_eh_for_cleanups ();
4832
4833 /* Check that the hardware interference sizes are at least
4834 alignof(max_align_t), as required by the standard. */
4835 const int max_align = max_align_t_align () / BITS_PER_UNIT;
4836 if (OPTION_SET_P (param_destruct_interfere_size))
4837 {
4838 if (param_destruct_interfere_size < max_align)
4839 error ("%<--param destructive-interference-size=%d%> is less than "
4840 "%d", param_destruct_interfere_size, max_align);
4841 else if (param_destruct_interfere_size < param_l1_cache_line_size)
4842 warning (OPT_Winterference_size,
4843 "%<--param destructive-interference-size=%d%> "
4844 "is less than %<--param l1-cache-line-size=%d%>",
4845 param_destruct_interfere_size, param_l1_cache_line_size);
4846 }
4847 else if (param_destruct_interfere_size)
4848 /* Assume the internal value is OK. */;
4849 else if (param_l1_cache_line_size >= max_align)
4850 param_destruct_interfere_size = param_l1_cache_line_size;
4851 /* else leave it unset. */
4852
4853 if (OPTION_SET_P (param_construct_interfere_size))
4854 {
4855 if (param_construct_interfere_size < max_align)
4856 error ("%<--param constructive-interference-size=%d%> is less than "
4857 "%d", param_construct_interfere_size, max_align);
4858 else if (param_construct_interfere_size > param_l1_cache_line_size
4859 && param_l1_cache_line_size >= max_align)
4860 warning (OPT_Winterference_size,
4861 "%<--param constructive-interference-size=%d%> "
4862 "is greater than %<--param l1-cache-line-size=%d%>",
4863 param_construct_interfere_size, param_l1_cache_line_size);
4864 }
4865 else if (param_construct_interfere_size)
4866 /* Assume the internal value is OK. */;
4867 else if (param_l1_cache_line_size >= max_align)
4868 param_construct_interfere_size = param_l1_cache_line_size;
4869 }
4870
4871 /* Enter an abi node in global-module context. returns a cookie to
4872 give to pop_abi_namespace. */
4873
4874 unsigned
push_abi_namespace(tree node)4875 push_abi_namespace (tree node)
4876 {
4877 push_nested_namespace (node);
4878 push_visibility ("default", 2);
4879 unsigned flags = module_kind;
4880 module_kind = 0;
4881 return flags;
4882 }
4883
4884 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave
4885 you. */
4886
4887 void
pop_abi_namespace(unsigned flags,tree node)4888 pop_abi_namespace (unsigned flags, tree node)
4889 {
4890 module_kind = flags;
4891 pop_visibility (2);
4892 pop_nested_namespace (node);
4893 }
4894
4895 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4896 the decl, LOC is the location to give the decl, NAME is the
4897 initialization string and TYPE_DEP indicates whether NAME depended
4898 on the type of the function. We make use of that to detect
4899 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4900 at the point of first use, so we mustn't push the decl now. */
4901
4902 static tree
cp_make_fname_decl(location_t loc,tree id,int type_dep)4903 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4904 {
4905 tree domain = NULL_TREE;
4906 tree init = NULL_TREE;
4907
4908 if (!(type_dep && in_template_function ()))
4909 {
4910 const char *name = NULL;
4911 bool release_name = false;
4912
4913 if (current_function_decl == NULL_TREE)
4914 name = "top level";
4915 else if (type_dep == 0)
4916 {
4917 /* __FUNCTION__ */
4918 name = fname_as_string (type_dep);
4919 release_name = true;
4920 }
4921 else
4922 {
4923 /* __PRETTY_FUNCTION__ */
4924 gcc_checking_assert (type_dep == 1);
4925 name = cxx_printable_name (current_function_decl, 2);
4926 }
4927
4928 size_t length = strlen (name);
4929 domain = build_index_type (size_int (length));
4930 init = build_string (length + 1, name);
4931 if (release_name)
4932 free (const_cast<char *> (name));
4933 }
4934
4935 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4936 type = build_cplus_array_type (type, domain);
4937
4938 if (init)
4939 TREE_TYPE (init) = type;
4940 else
4941 init = error_mark_node;
4942
4943 tree decl = build_decl (loc, VAR_DECL, id, type);
4944
4945 TREE_READONLY (decl) = 1;
4946 DECL_ARTIFICIAL (decl) = 1;
4947 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4948 TREE_STATIC (decl) = 1;
4949
4950 TREE_USED (decl) = 1;
4951
4952 SET_DECL_VALUE_EXPR (decl, init);
4953 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4954 /* For decl_constant_var_p. */
4955 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4956
4957 if (current_function_decl)
4958 {
4959 DECL_CONTEXT (decl) = current_function_decl;
4960 decl = pushdecl_outermost_localscope (decl);
4961 if (decl != error_mark_node)
4962 add_decl_expr (decl);
4963 }
4964 else
4965 {
4966 DECL_THIS_STATIC (decl) = true;
4967 decl = pushdecl_top_level_and_finish (decl, NULL_TREE);
4968 }
4969
4970 return decl;
4971 }
4972
4973 /* Install DECL as a builtin function at current global scope. Return
4974 the new decl (if we found an existing version). Also installs it
4975 into ::std, if it's not '_*'. */
4976
4977 tree
cxx_builtin_function(tree decl)4978 cxx_builtin_function (tree decl)
4979 {
4980 retrofit_lang_decl (decl);
4981
4982 DECL_ARTIFICIAL (decl) = 1;
4983 SET_DECL_LANGUAGE (decl, lang_c);
4984 /* Runtime library routines are, by definition, available in an
4985 external shared object. */
4986 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4987 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4988
4989 tree id = DECL_NAME (decl);
4990 const char *name = IDENTIFIER_POINTER (id);
4991 bool hiding = false;
4992 if (name[0] != '_' || name[1] != '_')
4993 /* In the user's namespace, it must be declared before use. */
4994 hiding = true;
4995 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4996 && !startswith (name + 2, "builtin_")
4997 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4998 "_chk", strlen ("_chk") + 1))
4999 /* Treat __*_chk fortification functions as anticipated as well,
5000 unless they are __builtin_*_chk. */
5001 hiding = true;
5002
5003 /* All builtins that don't begin with an '_' should additionally
5004 go in the 'std' namespace. */
5005 if (name[0] != '_')
5006 {
5007 tree std_decl = copy_decl (decl);
5008
5009 push_nested_namespace (std_node);
5010 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
5011 pushdecl (std_decl, hiding);
5012 pop_nested_namespace (std_node);
5013 }
5014
5015 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5016 decl = pushdecl (decl, hiding);
5017
5018 return decl;
5019 }
5020
5021 /* Like cxx_builtin_function, but guarantee the function is added to the global
5022 scope. This is to allow function specific options to add new machine
5023 dependent builtins when the target ISA changes via attribute((target(...)))
5024 which saves space on program startup if the program does not use non-generic
5025 ISAs. */
5026
5027 tree
cxx_builtin_function_ext_scope(tree decl)5028 cxx_builtin_function_ext_scope (tree decl)
5029 {
5030 push_nested_namespace (global_namespace);
5031 decl = cxx_builtin_function (decl);
5032 pop_nested_namespace (global_namespace);
5033
5034 return decl;
5035 }
5036
5037 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
5038
5039 tree
cxx_simulate_builtin_function_decl(tree decl)5040 cxx_simulate_builtin_function_decl (tree decl)
5041 {
5042 retrofit_lang_decl (decl);
5043
5044 DECL_ARTIFICIAL (decl) = 1;
5045 SET_DECL_LANGUAGE (decl, lang_cplusplus);
5046 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5047 return pushdecl (decl);
5048 }
5049
5050 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
5051 function. Not called directly. */
5052
5053 static tree
build_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)5054 build_library_fn (tree name, enum tree_code operator_code, tree type,
5055 int ecf_flags)
5056 {
5057 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
5058 DECL_EXTERNAL (fn) = 1;
5059 TREE_PUBLIC (fn) = 1;
5060 DECL_ARTIFICIAL (fn) = 1;
5061 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
5062 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
5063 SET_DECL_LANGUAGE (fn, lang_c);
5064 /* Runtime library routines are, by definition, available in an
5065 external shared object. */
5066 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
5067 DECL_VISIBILITY_SPECIFIED (fn) = 1;
5068 set_call_expr_flags (fn, ecf_flags);
5069 return fn;
5070 }
5071
5072 /* Returns the _DECL for a library function with C++ linkage. */
5073
5074 static tree
build_cp_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)5075 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
5076 int ecf_flags)
5077 {
5078 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
5079 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
5080 SET_DECL_LANGUAGE (fn, lang_cplusplus);
5081 return fn;
5082 }
5083
5084 /* Like build_library_fn, but takes a C string instead of an
5085 IDENTIFIER_NODE. */
5086
5087 tree
build_library_fn_ptr(const char * name,tree type,int ecf_flags)5088 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
5089 {
5090 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
5091 }
5092
5093 /* Like build_cp_library_fn, but takes a C string instead of an
5094 IDENTIFIER_NODE. */
5095
5096 tree
build_cp_library_fn_ptr(const char * name,tree type,int ecf_flags)5097 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
5098 {
5099 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
5100 ecf_flags);
5101 }
5102
5103 /* Like build_library_fn, but also pushes the function so that we will
5104 be able to find it via get_global_binding. Also, the function
5105 may throw exceptions listed in RAISES. */
5106
5107 tree
push_library_fn(tree name,tree type,tree raises,int ecf_flags)5108 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
5109 {
5110 if (raises)
5111 type = build_exception_variant (type, raises);
5112
5113 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
5114 return pushdecl_top_level (fn);
5115 }
5116
5117 /* Like build_cp_library_fn, but also pushes the function so that it
5118 will be found by normal lookup. */
5119
5120 static tree
push_cp_library_fn(enum tree_code operator_code,tree type,int ecf_flags)5121 push_cp_library_fn (enum tree_code operator_code, tree type,
5122 int ecf_flags)
5123 {
5124 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
5125 operator_code, type, ecf_flags);
5126 pushdecl (fn);
5127 if (flag_tm)
5128 apply_tm_attr (fn, get_identifier ("transaction_safe"));
5129 return fn;
5130 }
5131
5132 /* Like push_library_fn, but also note that this function throws
5133 and does not return. Used for __throw_foo and the like. */
5134
5135 tree
push_throw_library_fn(tree name,tree type)5136 push_throw_library_fn (tree name, tree type)
5137 {
5138 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
5139 return fn;
5140 }
5141
5142 /* When we call finish_struct for an anonymous union, we create
5143 default copy constructors and such. But, an anonymous union
5144 shouldn't have such things; this function undoes the damage to the
5145 anonymous union type T.
5146
5147 (The reason that we create the synthesized methods is that we don't
5148 distinguish `union { int i; }' from `typedef union { int i; } U'.
5149 The first is an anonymous union; the second is just an ordinary
5150 union type.) */
5151
5152 void
fixup_anonymous_aggr(tree t)5153 fixup_anonymous_aggr (tree t)
5154 {
5155 /* Wipe out memory of synthesized methods. */
5156 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
5157 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
5158 TYPE_HAS_COPY_CTOR (t) = 0;
5159 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
5160 TYPE_HAS_COPY_ASSIGN (t) = 0;
5161 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
5162
5163 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose
5164 invalid members. */
5165 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
5166 {
5167 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
5168 *prev_p = DECL_CHAIN (probe);
5169 else
5170 prev_p = &DECL_CHAIN (probe);
5171
5172 if (DECL_ARTIFICIAL (probe)
5173 && (!DECL_IMPLICIT_TYPEDEF_P (probe)
5174 || TYPE_ANON_P (TREE_TYPE (probe))))
5175 continue;
5176
5177 if (TREE_CODE (probe) != FIELD_DECL
5178 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe)))
5179 {
5180 /* We already complained about static data members in
5181 finish_static_data_member_decl. */
5182 if (!VAR_P (probe))
5183 {
5184 auto_diagnostic_group d;
5185 if (permerror (DECL_SOURCE_LOCATION (probe),
5186 TREE_CODE (t) == UNION_TYPE
5187 ? "%q#D invalid; an anonymous union may "
5188 "only have public non-static data members"
5189 : "%q#D invalid; an anonymous struct may "
5190 "only have public non-static data members", probe))
5191 {
5192 static bool hint;
5193 if (flag_permissive && !hint)
5194 {
5195 hint = true;
5196 inform (DECL_SOURCE_LOCATION (probe),
5197 "this flexibility is deprecated and will be "
5198 "removed");
5199 }
5200 }
5201 }
5202 }
5203 }
5204
5205 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */
5206 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t);
5207 unsigned store = 0;
5208 for (tree elt : vec)
5209 if (!is_overloaded_fn (elt))
5210 (*vec)[store++] = elt;
5211 vec_safe_truncate (vec, store);
5212
5213 /* Wipe RTTI info. */
5214 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE;
5215
5216 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
5217 assignment operators (because they cannot have these methods themselves).
5218 For anonymous unions this is already checked because they are not allowed
5219 in any union, otherwise we have to check it. */
5220 if (TREE_CODE (t) != UNION_TYPE)
5221 {
5222 tree field, type;
5223
5224 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5225 {
5226 error_at (location_of (t), "anonymous struct with base classes");
5227 /* Avoid ICE after error on anon-struct9.C. */
5228 TYPE_NEEDS_CONSTRUCTING (t) = false;
5229 }
5230
5231 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5232 if (TREE_CODE (field) == FIELD_DECL)
5233 {
5234 type = TREE_TYPE (field);
5235 if (CLASS_TYPE_P (type))
5236 {
5237 if (TYPE_NEEDS_CONSTRUCTING (type))
5238 error ("member %q+#D with constructor not allowed "
5239 "in anonymous aggregate", field);
5240 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5241 error ("member %q+#D with destructor not allowed "
5242 "in anonymous aggregate", field);
5243 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
5244 error ("member %q+#D with copy assignment operator "
5245 "not allowed in anonymous aggregate", field);
5246 }
5247 }
5248 }
5249 }
5250
5251 /* Warn for an attribute located at LOCATION that appertains to the
5252 class type CLASS_TYPE that has not been properly placed after its
5253 class-key, in it class-specifier. */
5254
5255 void
warn_misplaced_attr_for_class_type(location_t location,tree class_type)5256 warn_misplaced_attr_for_class_type (location_t location,
5257 tree class_type)
5258 {
5259 gcc_assert (OVERLOAD_TYPE_P (class_type));
5260
5261 auto_diagnostic_group d;
5262 if (warning_at (location, OPT_Wattributes,
5263 "attribute ignored in declaration "
5264 "of %q#T", class_type))
5265 inform (location,
5266 "attribute for %q#T must follow the %qs keyword",
5267 class_type, class_key_or_enum_as_string (class_type));
5268 }
5269
5270 /* Returns the cv-qualifiers that apply to the type specified
5271 by the DECLSPECS. */
5272
5273 static int
get_type_quals(const cp_decl_specifier_seq * declspecs)5274 get_type_quals (const cp_decl_specifier_seq *declspecs)
5275 {
5276 int type_quals = TYPE_UNQUALIFIED;
5277
5278 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5279 type_quals |= TYPE_QUAL_CONST;
5280 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5281 type_quals |= TYPE_QUAL_VOLATILE;
5282 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5283 type_quals |= TYPE_QUAL_RESTRICT;
5284
5285 return type_quals;
5286 }
5287
5288 /* Make sure that a declaration with no declarator is well-formed, i.e.
5289 just declares a tagged type or anonymous union.
5290
5291 Returns the type declared; or NULL_TREE if none. */
5292
5293 tree
check_tag_decl(cp_decl_specifier_seq * declspecs,bool explicit_type_instantiation_p)5294 check_tag_decl (cp_decl_specifier_seq *declspecs,
5295 bool explicit_type_instantiation_p)
5296 {
5297 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
5298 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
5299 /* If a class, struct, or enum type is declared by the DECLSPECS
5300 (i.e, if a class-specifier, enum-specifier, or non-typename
5301 elaborated-type-specifier appears in the DECLSPECS),
5302 DECLARED_TYPE is set to the corresponding type. */
5303 tree declared_type = NULL_TREE;
5304 bool error_p = false;
5305
5306 if (declspecs->multiple_types_p)
5307 error_at (smallest_type_location (declspecs),
5308 "multiple types in one declaration");
5309 else if (declspecs->redefined_builtin_type)
5310 {
5311 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
5312 if (!in_system_header_at (loc))
5313 permerror (loc, "redeclaration of C++ built-in type %qT",
5314 declspecs->redefined_builtin_type);
5315 return NULL_TREE;
5316 }
5317
5318 if (declspecs->type
5319 && TYPE_P (declspecs->type)
5320 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
5321 && MAYBE_CLASS_TYPE_P (declspecs->type))
5322 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
5323 declared_type = declspecs->type;
5324 else if (declspecs->type == error_mark_node)
5325 error_p = true;
5326
5327 if (type_uses_auto (declared_type))
5328 {
5329 error_at (declspecs->locations[ds_type_spec],
5330 "%<auto%> can only be specified for variables "
5331 "or function declarations");
5332 return error_mark_node;
5333 }
5334
5335 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
5336 declared_type = NULL_TREE;
5337
5338 if (!declared_type && !saw_friend && !error_p)
5339 permerror (input_location, "declaration does not declare anything");
5340 /* Check for an anonymous union. */
5341 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
5342 && TYPE_UNNAMED_P (declared_type))
5343 {
5344 /* 7/3 In a simple-declaration, the optional init-declarator-list
5345 can be omitted only when declaring a class (clause 9) or
5346 enumeration (7.2), that is, when the decl-specifier-seq contains
5347 either a class-specifier, an elaborated-type-specifier with
5348 a class-key (9.1), or an enum-specifier. In these cases and
5349 whenever a class-specifier or enum-specifier is present in the
5350 decl-specifier-seq, the identifiers in these specifiers are among
5351 the names being declared by the declaration (as class-name,
5352 enum-names, or enumerators, depending on the syntax). In such
5353 cases, and except for the declaration of an unnamed bit-field (9.6),
5354 the decl-specifier-seq shall introduce one or more names into the
5355 program, or shall redeclare a name introduced by a previous
5356 declaration. [Example:
5357 enum { }; // ill-formed
5358 typedef class { }; // ill-formed
5359 --end example] */
5360 if (saw_typedef)
5361 {
5362 error_at (declspecs->locations[ds_typedef],
5363 "missing type-name in typedef-declaration");
5364 return NULL_TREE;
5365 }
5366 /* Anonymous unions are objects, so they can have specifiers. */;
5367 SET_ANON_AGGR_TYPE_P (declared_type);
5368
5369 if (TREE_CODE (declared_type) != UNION_TYPE)
5370 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
5371 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
5372 }
5373
5374 else
5375 {
5376 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5377 error_at (declspecs->locations[ds_inline],
5378 "%<inline%> can only be specified for functions");
5379 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5380 error_at (declspecs->locations[ds_virtual],
5381 "%<virtual%> can only be specified for functions");
5382 else if (saw_friend
5383 && (!current_class_type
5384 || current_scope () != current_class_type))
5385 error_at (declspecs->locations[ds_friend],
5386 "%<friend%> can only be specified inside a class");
5387 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5388 error_at (declspecs->locations[ds_explicit],
5389 "%<explicit%> can only be specified for constructors");
5390 else if (declspecs->storage_class)
5391 error_at (declspecs->locations[ds_storage_class],
5392 "a storage class can only be specified for objects "
5393 "and functions");
5394 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5395 error_at (declspecs->locations[ds_const],
5396 "%<const%> can only be specified for objects and "
5397 "functions");
5398 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5399 error_at (declspecs->locations[ds_volatile],
5400 "%<volatile%> can only be specified for objects and "
5401 "functions");
5402 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5403 error_at (declspecs->locations[ds_restrict],
5404 "%<__restrict%> can only be specified for objects and "
5405 "functions");
5406 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5407 error_at (declspecs->locations[ds_thread],
5408 "%<__thread%> can only be specified for objects "
5409 "and functions");
5410 else if (saw_typedef)
5411 warning_at (declspecs->locations[ds_typedef], 0,
5412 "%<typedef%> was ignored in this declaration");
5413 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5414 error_at (declspecs->locations[ds_constexpr],
5415 "%qs cannot be used for type declarations", "constexpr");
5416 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5417 error_at (declspecs->locations[ds_constinit],
5418 "%qs cannot be used for type declarations", "constinit");
5419 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5420 error_at (declspecs->locations[ds_consteval],
5421 "%qs cannot be used for type declarations", "consteval");
5422 }
5423
5424 if (declspecs->attributes && warn_attributes && declared_type)
5425 {
5426 location_t loc;
5427 if (!CLASS_TYPE_P (declared_type)
5428 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5429 /* For a non-template class, use the name location. */
5430 loc = location_of (declared_type);
5431 else
5432 /* For a template class (an explicit instantiation), use the
5433 current location. */
5434 loc = input_location;
5435
5436 if (explicit_type_instantiation_p)
5437 /* [dcl.attr.grammar]/4:
5438
5439 No attribute-specifier-seq shall appertain to an explicit
5440 instantiation. */
5441 {
5442 if (warning_at (loc, OPT_Wattributes,
5443 "attribute ignored in explicit instantiation %q#T",
5444 declared_type))
5445 inform (loc,
5446 "no attribute can be applied to "
5447 "an explicit instantiation");
5448 }
5449 else
5450 warn_misplaced_attr_for_class_type (loc, declared_type);
5451 }
5452
5453 return declared_type;
5454 }
5455
5456 /* Called when a declaration is seen that contains no names to declare.
5457 If its type is a reference to a structure, union or enum inherited
5458 from a containing scope, shadow that tag name for the current scope
5459 with a forward reference.
5460 If its type defines a new named structure or union
5461 or defines an enum, it is valid but we need not do anything here.
5462 Otherwise, it is an error.
5463
5464 C++: may have to grok the declspecs to learn about static,
5465 complain for anonymous unions.
5466
5467 Returns the TYPE declared -- or NULL_TREE if none. */
5468
5469 tree
shadow_tag(cp_decl_specifier_seq * declspecs)5470 shadow_tag (cp_decl_specifier_seq *declspecs)
5471 {
5472 tree t = check_tag_decl (declspecs,
5473 /*explicit_type_instantiation_p=*/false);
5474
5475 if (!t)
5476 return NULL_TREE;
5477
5478 t = maybe_process_partial_specialization (t);
5479 if (t == error_mark_node)
5480 return NULL_TREE;
5481
5482 /* This is where the variables in an anonymous union are
5483 declared. An anonymous union declaration looks like:
5484 union { ... } ;
5485 because there is no declarator after the union, the parser
5486 sends that declaration here. */
5487 if (ANON_AGGR_TYPE_P (t))
5488 {
5489 fixup_anonymous_aggr (t);
5490
5491 if (TYPE_FIELDS (t))
5492 {
5493 tree decl = grokdeclarator (/*declarator=*/NULL,
5494 declspecs, NORMAL, 0, NULL);
5495 finish_anon_union (decl);
5496 }
5497 }
5498
5499 return t;
5500 }
5501
5502 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5503
5504 tree
groktypename(cp_decl_specifier_seq * type_specifiers,const cp_declarator * declarator,bool is_template_arg)5505 groktypename (cp_decl_specifier_seq *type_specifiers,
5506 const cp_declarator *declarator,
5507 bool is_template_arg)
5508 {
5509 tree attrs;
5510 tree type;
5511 enum decl_context context
5512 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5513 attrs = type_specifiers->attributes;
5514 type_specifiers->attributes = NULL_TREE;
5515 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5516 if (attrs && type != error_mark_node)
5517 {
5518 if (CLASS_TYPE_P (type))
5519 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5520 "outside of definition", type);
5521 else if (MAYBE_CLASS_TYPE_P (type))
5522 /* A template type parameter or other dependent type. */
5523 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5524 "type %qT without an associated declaration", type);
5525 else
5526 cplus_decl_attributes (&type, attrs, 0);
5527 }
5528 return type;
5529 }
5530
5531 /* Process a DECLARATOR for a function-scope or namespace-scope
5532 variable or function declaration.
5533 (Function definitions go through start_function; class member
5534 declarations appearing in the body of the class go through
5535 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5536 If an error occurs, the error_mark_node is returned instead.
5537
5538 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5539 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5540 for an explicitly defaulted function, or SD_DELETED for an explicitly
5541 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5542 implicitly initialized via a default constructor. It can also be
5543 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also
5544 mark the new decl as DECL_DECOMPOSITION_P.
5545
5546 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this
5547 declaration.
5548
5549 The scope represented by the context of the returned DECL is pushed
5550 (if it is not the global namespace) and is assigned to
5551 *PUSHED_SCOPE_P. The caller is then responsible for calling
5552 pop_scope on *PUSHED_SCOPE_P if it is set. */
5553
5554 tree
start_decl(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,int initialized,tree attributes,tree prefix_attributes,tree * pushed_scope_p)5555 start_decl (const cp_declarator *declarator,
5556 cp_decl_specifier_seq *declspecs,
5557 int initialized,
5558 tree attributes,
5559 tree prefix_attributes,
5560 tree *pushed_scope_p)
5561 {
5562 tree decl;
5563 tree context;
5564 bool was_public;
5565 int flags;
5566 bool alias;
5567 tree initial;
5568
5569 *pushed_scope_p = NULL_TREE;
5570
5571 if (prefix_attributes != error_mark_node)
5572 attributes = chainon (attributes, prefix_attributes);
5573
5574 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5575 &attributes);
5576
5577 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5578 || decl == error_mark_node
5579 || prefix_attributes == error_mark_node)
5580 return error_mark_node;
5581
5582 context = CP_DECL_CONTEXT (decl);
5583 if (context != global_namespace)
5584 *pushed_scope_p = push_scope (context);
5585
5586 if (initialized && TREE_CODE (decl) == TYPE_DECL)
5587 {
5588 error_at (DECL_SOURCE_LOCATION (decl),
5589 "typedef %qD is initialized (use %qs instead)",
5590 decl, "decltype");
5591 return error_mark_node;
5592 }
5593
5594 /* Save the DECL_INITIAL value in case it gets clobbered to assist
5595 with attribute validation. */
5596 initial = DECL_INITIAL (decl);
5597
5598 if (initialized)
5599 {
5600 if (! toplevel_bindings_p ()
5601 && DECL_EXTERNAL (decl))
5602 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5603 decl);
5604 DECL_EXTERNAL (decl) = 0;
5605 if (toplevel_bindings_p ())
5606 TREE_STATIC (decl) = 1;
5607 /* Tell 'cplus_decl_attributes' this is an initialized decl,
5608 even though we might not yet have the initializer expression. */
5609 if (!DECL_INITIAL (decl))
5610 DECL_INITIAL (decl) = error_mark_node;
5611 }
5612 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5613
5614 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5615 record_key_method_defined (decl);
5616
5617 /* If this is a typedef that names the class for linkage purposes
5618 (7.1.3p8), apply any attributes directly to the type. */
5619 if (TREE_CODE (decl) == TYPE_DECL
5620 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5621 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5622 flags = ATTR_FLAG_TYPE_IN_PLACE;
5623 else
5624 flags = 0;
5625
5626 /* Set attributes here so if duplicate decl, will have proper attributes. */
5627 cplus_decl_attributes (&decl, attributes, flags);
5628
5629 /* Restore the original DECL_INITIAL that we may have clobbered earlier to
5630 assist with attribute validation. */
5631 DECL_INITIAL (decl) = initial;
5632
5633 /* Dllimported symbols cannot be defined. Static data members (which
5634 can be initialized in-class and dllimported) go through grokfield,
5635 not here, so we don't need to exclude those decls when checking for
5636 a definition. */
5637 if (initialized && DECL_DLLIMPORT_P (decl))
5638 {
5639 error_at (DECL_SOURCE_LOCATION (decl),
5640 "definition of %q#D is marked %<dllimport%>", decl);
5641 DECL_DLLIMPORT_P (decl) = 0;
5642 }
5643
5644 /* If #pragma weak was used, mark the decl weak now. */
5645 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5646 maybe_apply_pragma_weak (decl);
5647
5648 if (TREE_CODE (decl) == FUNCTION_DECL
5649 && DECL_DECLARED_INLINE_P (decl)
5650 && DECL_UNINLINABLE (decl)
5651 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5652 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5653 "inline function %qD given attribute %qs", decl, "noinline");
5654
5655 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5656 {
5657 bool this_tmpl = (current_template_depth
5658 > template_class_depth (context));
5659 if (VAR_P (decl))
5660 {
5661 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5662 if (field == NULL_TREE
5663 || !(VAR_P (field) || variable_template_p (field)))
5664 error ("%q+#D is not a static data member of %q#T", decl, context);
5665 else if (variable_template_p (field)
5666 && (DECL_LANG_SPECIFIC (decl)
5667 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5668 /* OK, specialization was already checked. */;
5669 else if (variable_template_p (field) && !this_tmpl)
5670 {
5671 error_at (DECL_SOURCE_LOCATION (decl),
5672 "non-member-template declaration of %qD", decl);
5673 inform (DECL_SOURCE_LOCATION (field), "does not match "
5674 "member template declaration here");
5675 return error_mark_node;
5676 }
5677 else
5678 {
5679 if (variable_template_p (field))
5680 field = DECL_TEMPLATE_RESULT (field);
5681
5682 if (DECL_CONTEXT (field) != context)
5683 {
5684 if (!same_type_p (DECL_CONTEXT (field), context))
5685 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5686 "to be defined as %<%T::%D%>",
5687 DECL_CONTEXT (field), DECL_NAME (decl),
5688 context, DECL_NAME (decl));
5689 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5690 }
5691 /* Static data member are tricky; an in-class initialization
5692 still doesn't provide a definition, so the in-class
5693 declaration will have DECL_EXTERNAL set, but will have an
5694 initialization. Thus, duplicate_decls won't warn
5695 about this situation, and so we check here. */
5696 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5697 error ("duplicate initialization of %qD", decl);
5698 field = duplicate_decls (decl, field);
5699 if (field == error_mark_node)
5700 return error_mark_node;
5701 else if (field)
5702 decl = field;
5703 }
5704 }
5705 else
5706 {
5707 tree field = check_classfn (context, decl,
5708 this_tmpl
5709 ? current_template_parms
5710 : NULL_TREE);
5711 if (field && field != error_mark_node
5712 && duplicate_decls (decl, field))
5713 decl = field;
5714 }
5715
5716 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5717 DECL_IN_AGGR_P (decl) = 0;
5718 /* Do not mark DECL as an explicit specialization if it was not
5719 already marked as an instantiation; a declaration should
5720 never be marked as a specialization unless we know what
5721 template is being specialized. */
5722 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5723 {
5724 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5725 if (TREE_CODE (decl) == FUNCTION_DECL)
5726 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5727 && DECL_DECLARED_INLINE_P (decl));
5728 else
5729 DECL_COMDAT (decl) = false;
5730
5731 /* [temp.expl.spec] An explicit specialization of a static data
5732 member of a template is a definition if the declaration
5733 includes an initializer; otherwise, it is a declaration.
5734
5735 We check for processing_specialization so this only applies
5736 to the new specialization syntax. */
5737 if (!initialized && processing_specialization)
5738 DECL_EXTERNAL (decl) = 1;
5739 }
5740
5741 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5742 /* Aliases are definitions. */
5743 && !alias)
5744 permerror (declarator->id_loc,
5745 "declaration of %q#D outside of class is not definition",
5746 decl);
5747 }
5748
5749 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */
5750 if (initialized == SD_DECOMPOSITION)
5751 fit_decomposition_lang_decl (decl, NULL_TREE);
5752
5753 was_public = TREE_PUBLIC (decl);
5754
5755 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL)
5756 && current_function_decl)
5757 {
5758 /* A function-scope decl of some namespace-scope decl. */
5759 DECL_LOCAL_DECL_P (decl) = true;
5760 if (named_module_purview_p ())
5761 error_at (declarator->id_loc,
5762 "block-scope extern declaration %q#D not permitted"
5763 " in module purview", decl);
5764 }
5765
5766 /* Enter this declaration into the symbol table. Don't push the plain
5767 VAR_DECL for a variable template. */
5768 if (!template_parm_scope_p ()
5769 || !VAR_P (decl))
5770 decl = maybe_push_decl (decl);
5771
5772 if (processing_template_decl)
5773 decl = push_template_decl (decl);
5774
5775 if (decl == error_mark_node)
5776 return error_mark_node;
5777
5778 if (VAR_P (decl)
5779 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5780 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl)
5781 /* But not templated variables. */
5782 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
5783 {
5784 /* This is a const variable with implicit 'static'. Set
5785 DECL_THIS_STATIC so we can tell it from variables that are
5786 !TREE_PUBLIC because of the anonymous namespace. */
5787 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5788 DECL_THIS_STATIC (decl) = 1;
5789 }
5790
5791 if (current_function_decl && VAR_P (decl)
5792 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
5793 && cxx_dialect < cxx23)
5794 {
5795 bool ok = false;
5796 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
5797 error_at (DECL_SOURCE_LOCATION (decl),
5798 "%qD defined %<thread_local%> in %qs function only "
5799 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5800 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5801 ? "consteval" : "constexpr");
5802 else if (TREE_STATIC (decl))
5803 error_at (DECL_SOURCE_LOCATION (decl),
5804 "%qD defined %<static%> in %qs function only available "
5805 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
5806 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5807 ? "consteval" : "constexpr");
5808 else
5809 ok = true;
5810 if (!ok)
5811 cp_function_chain->invalid_constexpr = true;
5812 }
5813
5814 if (!processing_template_decl && VAR_P (decl))
5815 start_decl_1 (decl, initialized);
5816
5817 return decl;
5818 }
5819
5820 /* Process the declaration of a variable DECL. INITIALIZED is true
5821 iff DECL is explicitly initialized. (INITIALIZED is false if the
5822 variable is initialized via an implicitly-called constructor.)
5823 This function must be called for ordinary variables (including, for
5824 example, implicit instantiations of templates), but must not be
5825 called for template declarations. */
5826
5827 void
start_decl_1(tree decl,bool initialized)5828 start_decl_1 (tree decl, bool initialized)
5829 {
5830 gcc_checking_assert (!processing_template_decl);
5831
5832 if (error_operand_p (decl))
5833 return;
5834
5835 gcc_checking_assert (VAR_P (decl));
5836
5837 tree type = TREE_TYPE (decl);
5838 bool complete_p = COMPLETE_TYPE_P (type);
5839 bool aggregate_definition_p
5840 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5841
5842 /* If an explicit initializer is present, or if this is a definition
5843 of an aggregate, then we need a complete type at this point.
5844 (Scalars are always complete types, so there is nothing to
5845 check.) This code just sets COMPLETE_P; errors (if necessary)
5846 are issued below. */
5847 if ((initialized || aggregate_definition_p)
5848 && !complete_p
5849 && COMPLETE_TYPE_P (complete_type (type)))
5850 {
5851 complete_p = true;
5852 /* We will not yet have set TREE_READONLY on DECL if the type
5853 was "const", but incomplete, before this point. But, now, we
5854 have a complete type, so we can try again. */
5855 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5856 }
5857
5858 if (initialized)
5859 /* Is it valid for this decl to have an initializer at all? */
5860 {
5861 /* Don't allow initializations for incomplete types except for
5862 arrays which might be completed by the initialization. */
5863 if (complete_p)
5864 ; /* A complete type is ok. */
5865 else if (type_uses_auto (type))
5866 ; /* An auto type is ok. */
5867 else if (TREE_CODE (type) != ARRAY_TYPE)
5868 {
5869 error ("variable %q#D has initializer but incomplete type", decl);
5870 type = TREE_TYPE (decl) = error_mark_node;
5871 }
5872 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5873 {
5874 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5875 error ("elements of array %q#D have incomplete type", decl);
5876 /* else we already gave an error in start_decl. */
5877 }
5878 }
5879 else if (aggregate_definition_p && !complete_p)
5880 {
5881 if (type_uses_auto (type))
5882 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5883 else
5884 {
5885 error ("aggregate %q#D has incomplete type and cannot be defined",
5886 decl);
5887 /* Change the type so that assemble_variable will give
5888 DECL an rtl we can live with: (mem (const_int 0)). */
5889 type = TREE_TYPE (decl) = error_mark_node;
5890 }
5891 }
5892
5893 /* Create a new scope to hold this declaration if necessary.
5894 Whether or not a new scope is necessary cannot be determined
5895 until after the type has been completed; if the type is a
5896 specialization of a class template it is not until after
5897 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5898 will be set correctly. */
5899 maybe_push_cleanup_level (type);
5900 }
5901
5902 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle
5903 C++20 P0960. TYPE is the type of the object we're initializing. */
5904
5905 tree
do_aggregate_paren_init(tree init,tree type)5906 do_aggregate_paren_init (tree init, tree type)
5907 {
5908 tree val = TREE_VALUE (init);
5909
5910 if (TREE_CHAIN (init) == NULL_TREE)
5911 {
5912 /* If the list has a single element and it's a string literal,
5913 then it's the initializer for the array as a whole. */
5914 if (TREE_CODE (type) == ARRAY_TYPE
5915 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
5916 && TREE_CODE (tree_strip_any_location_wrapper (val))
5917 == STRING_CST)
5918 return val;
5919 /* Handle non-standard extensions like compound literals. This also
5920 prevents triggering aggregate parenthesized-initialization in
5921 compiler-generated code for =default. */
5922 else if (same_type_ignoring_top_level_qualifiers_p (type,
5923 TREE_TYPE (val)))
5924 return val;
5925 }
5926
5927 init = build_constructor_from_list (init_list_type_node, init);
5928 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5929 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5930 return init;
5931 }
5932
5933 /* Handle initialization of references. DECL, TYPE, and INIT have the
5934 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5935 but will be set to a new CLEANUP_STMT if a temporary is created
5936 that must be destroyed subsequently.
5937
5938 Returns an initializer expression to use to initialize DECL, or
5939 NULL if the initialization can be performed statically.
5940
5941 Quotes on semantics can be found in ARM 8.4.3. */
5942
5943 static tree
grok_reference_init(tree decl,tree type,tree init,int flags)5944 grok_reference_init (tree decl, tree type, tree init, int flags)
5945 {
5946 if (init == NULL_TREE)
5947 {
5948 if ((DECL_LANG_SPECIFIC (decl) == 0
5949 || DECL_IN_AGGR_P (decl) == 0)
5950 && ! DECL_THIS_EXTERN (decl))
5951 error_at (DECL_SOURCE_LOCATION (decl),
5952 "%qD declared as reference but not initialized", decl);
5953 return NULL_TREE;
5954 }
5955
5956 tree ttype = TREE_TYPE (type);
5957 if (TREE_CODE (init) == TREE_LIST)
5958 {
5959 /* This handles (C++20 only) code like
5960
5961 const A& r(1, 2, 3);
5962
5963 where we treat the parenthesized list as a CONSTRUCTOR. */
5964 if (TREE_TYPE (init) == NULL_TREE
5965 && CP_AGGREGATE_TYPE_P (ttype)
5966 && !DECL_DECOMPOSITION_P (decl)
5967 && (cxx_dialect >= cxx20))
5968 {
5969 /* We don't know yet if we should treat const A& r(1) as
5970 const A& r{1}. */
5971 if (list_length (init) == 1)
5972 {
5973 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
5974 init = build_x_compound_expr_from_list (init, ELK_INIT,
5975 tf_warning_or_error);
5976 }
5977 /* If the list had more than one element, the code is ill-formed
5978 pre-C++20, so we can build a constructor right away. */
5979 else
5980 init = do_aggregate_paren_init (init, ttype);
5981 }
5982 else
5983 init = build_x_compound_expr_from_list (init, ELK_INIT,
5984 tf_warning_or_error);
5985 }
5986
5987 if (TREE_CODE (ttype) != ARRAY_TYPE
5988 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5989 /* Note: default conversion is only called in very special cases. */
5990 init = decay_conversion (init, tf_warning_or_error);
5991
5992 /* check_initializer handles this for non-reference variables, but for
5993 references we need to do it here or the initializer will get the
5994 incomplete array type and confuse later calls to
5995 cp_complete_array_type. */
5996 if (TREE_CODE (ttype) == ARRAY_TYPE
5997 && TYPE_DOMAIN (ttype) == NULL_TREE
5998 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5999 || TREE_CODE (init) == STRING_CST))
6000 {
6001 cp_complete_array_type (&ttype, init, false);
6002 if (ttype != TREE_TYPE (type))
6003 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
6004 }
6005
6006 /* Convert INIT to the reference type TYPE. This may involve the
6007 creation of a temporary, whose lifetime must be the same as that
6008 of the reference. If so, a DECL_EXPR for the temporary will be
6009 added just after the DECL_EXPR for DECL. That's why we don't set
6010 DECL_INITIAL for local references (instead assigning to them
6011 explicitly); we need to allow the temporary to be initialized
6012 first. */
6013 return initialize_reference (type, init, flags,
6014 tf_warning_or_error);
6015 }
6016
6017 /* Designated initializers in arrays are not supported in GNU C++.
6018 The parser cannot detect this error since it does not know whether
6019 a given brace-enclosed initializer is for a class type or for an
6020 array. This function checks that CE does not use a designated
6021 initializer. If it does, an error is issued. Returns true if CE
6022 is valid, i.e., does not have a designated initializer. */
6023
6024 bool
check_array_designated_initializer(constructor_elt * ce,unsigned HOST_WIDE_INT index)6025 check_array_designated_initializer (constructor_elt *ce,
6026 unsigned HOST_WIDE_INT index)
6027 {
6028 /* Designated initializers for array elements are not supported. */
6029 if (ce->index)
6030 {
6031 /* The parser only allows identifiers as designated
6032 initializers. */
6033 if (ce->index == error_mark_node)
6034 {
6035 error ("name used in a GNU-style designated "
6036 "initializer for an array");
6037 return false;
6038 }
6039 else if (identifier_p (ce->index))
6040 {
6041 error ("name %qD used in a GNU-style designated "
6042 "initializer for an array", ce->index);
6043 return false;
6044 }
6045
6046 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6047 ce->index, true);
6048 if (ce_index
6049 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
6050 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
6051 == INTEGER_CST))
6052 {
6053 /* A C99 designator is OK if it matches the current index. */
6054 if (wi::to_wide (ce_index) == index)
6055 {
6056 ce->index = ce_index;
6057 return true;
6058 }
6059 else
6060 sorry ("non-trivial designated initializers not supported");
6061 }
6062 else
6063 error_at (cp_expr_loc_or_input_loc (ce->index),
6064 "C99 designator %qE is not an integral constant-expression",
6065 ce->index);
6066
6067 return false;
6068 }
6069
6070 return true;
6071 }
6072
6073 /* When parsing `int a[] = {1, 2};' we don't know the size of the
6074 array until we finish parsing the initializer. If that's the
6075 situation we're in, update DECL accordingly. */
6076
6077 static void
maybe_deduce_size_from_array_init(tree decl,tree init)6078 maybe_deduce_size_from_array_init (tree decl, tree init)
6079 {
6080 tree type = TREE_TYPE (decl);
6081
6082 if (TREE_CODE (type) == ARRAY_TYPE
6083 && TYPE_DOMAIN (type) == NULL_TREE
6084 && TREE_CODE (decl) != TYPE_DECL)
6085 {
6086 /* do_default is really a C-ism to deal with tentative definitions.
6087 But let's leave it here to ease the eventual merge. */
6088 int do_default = !DECL_EXTERNAL (decl);
6089 tree initializer = init ? init : DECL_INITIAL (decl);
6090 int failure = 0;
6091
6092 /* Check that there are no designated initializers in INIT, as
6093 those are not supported in GNU C++, and as the middle-end
6094 will crash if presented with a non-numeric designated
6095 initializer. */
6096 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
6097 {
6098 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
6099 constructor_elt *ce;
6100 HOST_WIDE_INT i;
6101 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
6102 {
6103 if (instantiation_dependent_expression_p (ce->index))
6104 return;
6105 if (!check_array_designated_initializer (ce, i))
6106 failure = 1;
6107 /* If an un-designated initializer is type-dependent, we can't
6108 check brace elision yet. */
6109 if (ce->index == NULL_TREE
6110 && type_dependent_expression_p (ce->value))
6111 return;
6112 }
6113 }
6114
6115 if (failure)
6116 TREE_TYPE (decl) = error_mark_node;
6117 else
6118 {
6119 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
6120 do_default);
6121 if (failure == 1)
6122 {
6123 error_at (cp_expr_loc_or_loc (initializer,
6124 DECL_SOURCE_LOCATION (decl)),
6125 "initializer fails to determine size of %qD", decl);
6126 }
6127 else if (failure == 2)
6128 {
6129 if (do_default)
6130 {
6131 error_at (DECL_SOURCE_LOCATION (decl),
6132 "array size missing in %qD", decl);
6133 }
6134 /* If a `static' var's size isn't known, make it extern as
6135 well as static, so it does not get allocated. If it's not
6136 `static', then don't mark it extern; finish_incomplete_decl
6137 will give it a default size and it will get allocated. */
6138 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
6139 DECL_EXTERNAL (decl) = 1;
6140 }
6141 else if (failure == 3)
6142 {
6143 error_at (DECL_SOURCE_LOCATION (decl),
6144 "zero-size array %qD", decl);
6145 }
6146 }
6147
6148 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
6149
6150 relayout_decl (decl);
6151 }
6152 }
6153
6154 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
6155 any appropriate error messages regarding the layout. */
6156
6157 static void
layout_var_decl(tree decl)6158 layout_var_decl (tree decl)
6159 {
6160 tree type;
6161
6162 type = TREE_TYPE (decl);
6163 if (type == error_mark_node)
6164 return;
6165
6166 /* If we haven't already laid out this declaration, do so now.
6167 Note that we must not call complete type for an external object
6168 because it's type might involve templates that we are not
6169 supposed to instantiate yet. (And it's perfectly valid to say
6170 `extern X x' for some incomplete type `X'.) */
6171 if (!DECL_EXTERNAL (decl))
6172 complete_type (type);
6173 if (!DECL_SIZE (decl)
6174 && TREE_TYPE (decl) != error_mark_node
6175 && complete_or_array_type_p (type))
6176 layout_decl (decl, 0);
6177
6178 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
6179 {
6180 /* An automatic variable with an incomplete type: that is an error.
6181 Don't talk about array types here, since we took care of that
6182 message in grokdeclarator. */
6183 error_at (DECL_SOURCE_LOCATION (decl),
6184 "storage size of %qD isn%'t known", decl);
6185 TREE_TYPE (decl) = error_mark_node;
6186 }
6187 #if 0
6188 /* Keep this code around in case we later want to control debug info
6189 based on whether a type is "used". (jason 1999-11-11) */
6190
6191 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
6192 /* Let debugger know it should output info for this type. */
6193 note_debug_info_needed (ttype);
6194
6195 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
6196 note_debug_info_needed (DECL_CONTEXT (decl));
6197 #endif
6198
6199 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
6200 && DECL_SIZE (decl) != NULL_TREE
6201 && ! TREE_CONSTANT (DECL_SIZE (decl)))
6202 {
6203 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6204 && !DECL_LOCAL_DECL_P (decl))
6205 constant_expression_warning (DECL_SIZE (decl));
6206 else
6207 {
6208 error_at (DECL_SOURCE_LOCATION (decl),
6209 "storage size of %qD isn%'t constant", decl);
6210 TREE_TYPE (decl) = error_mark_node;
6211 type = error_mark_node;
6212 }
6213 }
6214
6215 /* If the final element initializes a flexible array field, add the size of
6216 that initializer to DECL's size. */
6217 if (type != error_mark_node
6218 && DECL_INITIAL (decl)
6219 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6220 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))
6221 && DECL_SIZE (decl) != NULL_TREE
6222 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
6223 && TYPE_SIZE (type) != NULL_TREE
6224 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
6225 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type)))
6226 {
6227 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ();
6228 if (elt.index)
6229 {
6230 tree itype = TREE_TYPE (elt.index);
6231 tree vtype = TREE_TYPE (elt.value);
6232 if (TREE_CODE (itype) == ARRAY_TYPE
6233 && TYPE_DOMAIN (itype) == NULL
6234 && TREE_CODE (vtype) == ARRAY_TYPE
6235 && COMPLETE_TYPE_P (vtype))
6236 {
6237 DECL_SIZE (decl)
6238 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype));
6239 DECL_SIZE_UNIT (decl)
6240 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
6241 TYPE_SIZE_UNIT (vtype));
6242 }
6243 }
6244 }
6245 }
6246
6247 /* If a local static variable is declared in an inline function, or if
6248 we have a weak definition, we must endeavor to create only one
6249 instance of the variable at link-time. */
6250
6251 void
maybe_commonize_var(tree decl)6252 maybe_commonize_var (tree decl)
6253 {
6254 /* Don't mess with __FUNCTION__ and similar. */
6255 if (DECL_ARTIFICIAL (decl))
6256 return;
6257
6258 /* Static data in a function with comdat linkage also has comdat
6259 linkage. */
6260 if ((TREE_STATIC (decl)
6261 && DECL_FUNCTION_SCOPE_P (decl)
6262 && vague_linkage_p (DECL_CONTEXT (decl)))
6263 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
6264 {
6265 if (flag_weak)
6266 {
6267 /* With weak symbols, we simply make the variable COMDAT;
6268 that will cause copies in multiple translations units to
6269 be merged. */
6270 comdat_linkage (decl);
6271 }
6272 else
6273 {
6274 if (DECL_INITIAL (decl) == NULL_TREE
6275 || DECL_INITIAL (decl) == error_mark_node)
6276 {
6277 /* Without weak symbols, we can use COMMON to merge
6278 uninitialized variables. */
6279 TREE_PUBLIC (decl) = 1;
6280 DECL_COMMON (decl) = 1;
6281 }
6282 else
6283 {
6284 /* While for initialized variables, we must use internal
6285 linkage -- which means that multiple copies will not
6286 be merged. */
6287 TREE_PUBLIC (decl) = 0;
6288 DECL_COMMON (decl) = 0;
6289 DECL_INTERFACE_KNOWN (decl) = 1;
6290 const char *msg;
6291 if (DECL_INLINE_VAR_P (decl))
6292 msg = G_("sorry: semantics of inline variable "
6293 "%q#D are wrong (you%'ll wind up with "
6294 "multiple copies)");
6295 else
6296 msg = G_("sorry: semantics of inline function "
6297 "static data %q#D are wrong (you%'ll wind "
6298 "up with multiple copies)");
6299 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
6300 msg, decl))
6301 inform (DECL_SOURCE_LOCATION (decl),
6302 "you can work around this by removing the initializer");
6303 }
6304 }
6305 }
6306 }
6307
6308 /* Issue an error message if DECL is an uninitialized const variable.
6309 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
6310 context from potential_constant_expression. Returns true if all is well,
6311 false otherwise. */
6312
6313 bool
check_for_uninitialized_const_var(tree decl,bool constexpr_context_p,tsubst_flags_t complain)6314 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
6315 tsubst_flags_t complain)
6316 {
6317 tree type = strip_array_types (TREE_TYPE (decl));
6318
6319 /* ``Unless explicitly declared extern, a const object does not have
6320 external linkage and must be initialized. ($8.4; $12.1)'' ARM
6321 7.1.6 */
6322 if (VAR_P (decl)
6323 && !TYPE_REF_P (type)
6324 && (CP_TYPE_CONST_P (type)
6325 /* C++20 permits trivial default initialization in constexpr
6326 context (P1331R2). */
6327 || (cxx_dialect < cxx20
6328 && (constexpr_context_p
6329 || var_in_constexpr_fn (decl))))
6330 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
6331 {
6332 tree field = default_init_uninitialized_part (type);
6333 if (!field)
6334 return true;
6335
6336 bool show_notes = true;
6337
6338 if (!constexpr_context_p || cxx_dialect >= cxx20)
6339 {
6340 if (CP_TYPE_CONST_P (type))
6341 {
6342 if (complain & tf_error)
6343 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
6344 "uninitialized %<const %D%>", decl);
6345 }
6346 else
6347 {
6348 if (!is_instantiation_of_constexpr (current_function_decl)
6349 && (complain & tf_error))
6350 error_at (DECL_SOURCE_LOCATION (decl),
6351 "uninitialized variable %qD in %<constexpr%> "
6352 "function", decl);
6353 else
6354 show_notes = false;
6355 cp_function_chain->invalid_constexpr = true;
6356 }
6357 }
6358 else if (complain & tf_error)
6359 error_at (DECL_SOURCE_LOCATION (decl),
6360 "uninitialized variable %qD in %<constexpr%> context",
6361 decl);
6362
6363 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
6364 {
6365 tree defaulted_ctor;
6366
6367 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
6368 "%q#T has no user-provided default constructor", type);
6369 defaulted_ctor = in_class_defaulted_default_constructor (type);
6370 if (defaulted_ctor)
6371 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
6372 "constructor is not user-provided because it is "
6373 "explicitly defaulted in the class body");
6374 inform (DECL_SOURCE_LOCATION (field),
6375 "and the implicitly-defined constructor does not "
6376 "initialize %q#D", field);
6377 }
6378
6379 return false;
6380 }
6381
6382 return true;
6383 }
6384
6385 /* Structure holding the current initializer being processed by reshape_init.
6386 CUR is a pointer to the current element being processed, END is a pointer
6387 after the last element present in the initializer. */
6388 struct reshape_iter
6389 {
6390 constructor_elt *cur;
6391 constructor_elt *end;
6392 };
6393
6394 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t);
6395
6396 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6397 returned is the next FIELD_DECL (possibly FIELD itself) that can be
6398 initialized. If there are no more such fields, the return value
6399 will be NULL. */
6400
6401 tree
next_initializable_field(tree field)6402 next_initializable_field (tree field)
6403 {
6404 while (field
6405 && (TREE_CODE (field) != FIELD_DECL
6406 || DECL_UNNAMED_BIT_FIELD (field)
6407 || (DECL_ARTIFICIAL (field)
6408 /* In C++17, don't skip base class fields. */
6409 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
6410 /* Don't skip vptr fields. We might see them when we're
6411 called from reduced_constant_expression_p. */
6412 && !DECL_VIRTUAL_P (field))))
6413 field = DECL_CHAIN (field);
6414
6415 return field;
6416 }
6417
6418 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value
6419 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds
6420 to a subobject. If there are no more such fields, the return value will be
6421 NULL. */
6422
6423 tree
next_subobject_field(tree field)6424 next_subobject_field (tree field)
6425 {
6426 while (field
6427 && (TREE_CODE (field) != FIELD_DECL
6428 || DECL_UNNAMED_BIT_FIELD (field)
6429 || (DECL_ARTIFICIAL (field)
6430 && !DECL_FIELD_IS_BASE (field)
6431 && !DECL_VIRTUAL_P (field))))
6432 field = DECL_CHAIN (field);
6433
6434 return field;
6435 }
6436
6437 /* Return true for [dcl.init.list] direct-list-initialization from
6438 single element of enumeration with a fixed underlying type. */
6439
6440 bool
is_direct_enum_init(tree type,tree init)6441 is_direct_enum_init (tree type, tree init)
6442 {
6443 if (cxx_dialect >= cxx17
6444 && TREE_CODE (type) == ENUMERAL_TYPE
6445 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
6446 && TREE_CODE (init) == CONSTRUCTOR
6447 && CONSTRUCTOR_IS_DIRECT_INIT (init)
6448 && CONSTRUCTOR_NELTS (init) == 1
6449 /* DR 2374: The single element needs to be implicitly
6450 convertible to the underlying type of the enum. */
6451 && can_convert_arg (ENUM_UNDERLYING_TYPE (type),
6452 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value),
6453 CONSTRUCTOR_ELT (init, 0)->value,
6454 LOOKUP_IMPLICIT, tf_none))
6455 return true;
6456 return false;
6457 }
6458
6459 /* Subroutine of reshape_init_array and reshape_init_vector, which does
6460 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
6461 INTEGER_CST representing the size of the array minus one (the maximum index),
6462 or NULL_TREE if the array was declared without specifying the size. D is
6463 the iterator within the constructor. */
6464
6465 static tree
reshape_init_array_1(tree elt_type,tree max_index,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6466 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
6467 tree first_initializer_p, tsubst_flags_t complain)
6468 {
6469 tree new_init;
6470 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
6471 unsigned HOST_WIDE_INT max_index_cst = 0;
6472 unsigned HOST_WIDE_INT index;
6473
6474 /* The initializer for an array is always a CONSTRUCTOR. If this is the
6475 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need
6476 to build a new one. But don't reuse if not complaining; if this is
6477 tentative, we might also reshape to another type (95319). */
6478 bool reuse = (first_initializer_p
6479 && (complain & tf_error)
6480 && !CP_AGGREGATE_TYPE_P (elt_type)
6481 && !TREE_SIDE_EFFECTS (first_initializer_p));
6482 if (reuse)
6483 new_init = first_initializer_p;
6484 else
6485 new_init = build_constructor (init_list_type_node, NULL);
6486
6487 if (sized_array_p)
6488 {
6489 /* Minus 1 is used for zero sized arrays. */
6490 if (integer_all_onesp (max_index))
6491 return new_init;
6492
6493 if (tree_fits_uhwi_p (max_index))
6494 max_index_cst = tree_to_uhwi (max_index);
6495 /* sizetype is sign extended, not zero extended. */
6496 else
6497 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6498 }
6499
6500 /* Loop until there are no more initializers. */
6501 for (index = 0;
6502 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6503 ++index)
6504 {
6505 tree elt_init;
6506 constructor_elt *old_cur = d->cur;
6507
6508 if (d->cur->index)
6509 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6510 check_array_designated_initializer (d->cur, index);
6511 elt_init = reshape_init_r (elt_type, d,
6512 /*first_initializer_p=*/NULL_TREE,
6513 complain);
6514 if (elt_init == error_mark_node)
6515 return error_mark_node;
6516 tree idx = size_int (index);
6517 if (reuse)
6518 {
6519 old_cur->index = idx;
6520 old_cur->value = elt_init;
6521 }
6522 else
6523 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6524 idx, elt_init);
6525 if (!TREE_CONSTANT (elt_init))
6526 TREE_CONSTANT (new_init) = false;
6527
6528 /* This can happen with an invalid initializer (c++/54501). */
6529 if (d->cur == old_cur && !sized_array_p)
6530 break;
6531 }
6532
6533 return new_init;
6534 }
6535
6536 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6537 Parameters are the same of reshape_init_r. */
6538
6539 static tree
reshape_init_array(tree type,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6540 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p,
6541 tsubst_flags_t complain)
6542 {
6543 tree max_index = NULL_TREE;
6544
6545 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6546
6547 if (TYPE_DOMAIN (type))
6548 max_index = array_type_nelts (type);
6549
6550 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6551 first_initializer_p, complain);
6552 }
6553
6554 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6555 Parameters are the same of reshape_init_r. */
6556
6557 static tree
reshape_init_vector(tree type,reshape_iter * d,tsubst_flags_t complain)6558 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6559 {
6560 tree max_index = NULL_TREE;
6561
6562 gcc_assert (VECTOR_TYPE_P (type));
6563
6564 if (COMPOUND_LITERAL_P (d->cur->value))
6565 {
6566 tree value = d->cur->value;
6567 if (!same_type_p (TREE_TYPE (value), type))
6568 {
6569 if (complain & tf_error)
6570 error ("invalid type %qT as initializer for a vector of type %qT",
6571 TREE_TYPE (d->cur->value), type);
6572 value = error_mark_node;
6573 }
6574 ++d->cur;
6575 return value;
6576 }
6577
6578 /* For a vector, we initialize it as an array of the appropriate size. */
6579 if (VECTOR_TYPE_P (type))
6580 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6581
6582 return reshape_init_array_1 (TREE_TYPE (type), max_index, d,
6583 NULL_TREE, complain);
6584 }
6585
6586 /* Subroutine of reshape_init*: We're initializing an element with TYPE from
6587 INIT, in isolation from any designator or other initializers. */
6588
6589 static tree
reshape_single_init(tree type,tree init,tsubst_flags_t complain)6590 reshape_single_init (tree type, tree init, tsubst_flags_t complain)
6591 {
6592 /* We could also implement this by wrapping init in a new CONSTRUCTOR and
6593 calling reshape_init, but this way can just live on the stack. */
6594 constructor_elt elt = { /*index=*/NULL_TREE, init };
6595 reshape_iter iter = { &elt, &elt + 1 };
6596 return reshape_init_r (type, &iter,
6597 /*first_initializer_p=*/NULL_TREE,
6598 complain);
6599 }
6600
6601 /* Subroutine of reshape_init_r, processes the initializers for classes
6602 or union. Parameters are the same of reshape_init_r. */
6603
6604 static tree
reshape_init_class(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)6605 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6606 tsubst_flags_t complain)
6607 {
6608 tree field;
6609 tree new_init;
6610
6611 gcc_assert (CLASS_TYPE_P (type));
6612
6613 /* The initializer for a class is always a CONSTRUCTOR. */
6614 new_init = build_constructor (init_list_type_node, NULL);
6615
6616 int binfo_idx = -1;
6617 tree binfo = TYPE_BINFO (type);
6618 tree base_binfo = NULL_TREE;
6619 if (cxx_dialect >= cxx17 && uses_template_parms (type))
6620 {
6621 /* We get here from maybe_aggr_guide for C++20 class template argument
6622 deduction. In this case we need to look through the binfo because a
6623 template doesn't have base fields. */
6624 binfo_idx = 0;
6625 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo);
6626 }
6627 if (base_binfo)
6628 field = base_binfo;
6629 else
6630 field = next_initializable_field (TYPE_FIELDS (type));
6631
6632 if (!field)
6633 {
6634 /* [dcl.init.aggr]
6635
6636 An initializer for an aggregate member that is an
6637 empty class shall have the form of an empty
6638 initializer-list {}. */
6639 if (!first_initializer_p)
6640 {
6641 if (complain & tf_error)
6642 error ("initializer for %qT must be brace-enclosed", type);
6643 return error_mark_node;
6644 }
6645 return new_init;
6646 }
6647
6648 /* For C++20 CTAD, handle pack expansions in the base list. */
6649 tree last_was_pack_expansion = NULL_TREE;
6650
6651 /* Loop through the initializable fields, gathering initializers. */
6652 while (d->cur != d->end)
6653 {
6654 tree field_init;
6655 constructor_elt *old_cur = d->cur;
6656 bool direct_desig = false;
6657
6658 /* Handle C++20 designated initializers. */
6659 if (d->cur->index)
6660 {
6661 if (d->cur->index == error_mark_node)
6662 return error_mark_node;
6663
6664 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6665 {
6666 /* We already reshaped this. */
6667 if (field != d->cur->index)
6668 {
6669 if (tree id = DECL_NAME (d->cur->index))
6670 gcc_checking_assert (d->cur->index
6671 == get_class_binding (type, id));
6672 field = d->cur->index;
6673 }
6674 }
6675 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6676 {
6677 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6678 field = get_class_binding (type, d->cur->index);
6679 direct_desig = true;
6680 }
6681 else
6682 {
6683 if (complain & tf_error)
6684 error ("%<[%E] =%> used in a GNU-style designated initializer"
6685 " for class %qT", d->cur->index, type);
6686 return error_mark_node;
6687 }
6688
6689 if (!field && ANON_AGGR_TYPE_P (type))
6690 /* Apparently the designator isn't for a member of this anonymous
6691 struct, so head back to the enclosing class. */
6692 break;
6693
6694 if (!field || TREE_CODE (field) != FIELD_DECL)
6695 {
6696 if (complain & tf_error)
6697 error ("%qT has no non-static data member named %qD", type,
6698 d->cur->index);
6699 return error_mark_node;
6700 }
6701
6702 /* If the element is an anonymous union object and the initializer
6703 list is a designated-initializer-list, the anonymous union object
6704 is initialized by the designated-initializer-list { D }, where D
6705 is the designated-initializer-clause naming a member of the
6706 anonymous union object. */
6707 tree ictx = DECL_CONTEXT (field);
6708 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
6709 {
6710 /* Find the anon aggr that is a direct member of TYPE. */
6711 while (ANON_AGGR_TYPE_P (ictx))
6712 {
6713 tree cctx = TYPE_CONTEXT (ictx);
6714 if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
6715 goto found;
6716 ictx = cctx;
6717 }
6718
6719 /* Not found, e.g. FIELD is a member of a base class. */
6720 if (complain & tf_error)
6721 error ("%qD is not a direct member of %qT", field, type);
6722 return error_mark_node;
6723
6724 found:
6725 /* Now find the TYPE member with that anon aggr type. */
6726 tree aafield = TYPE_FIELDS (type);
6727 for (; aafield; aafield = TREE_CHAIN (aafield))
6728 if (TREE_TYPE (aafield) == ictx)
6729 break;
6730 gcc_assert (aafield);
6731 field = aafield;
6732 direct_desig = false;
6733 }
6734 }
6735
6736 /* If we processed all the member of the class, we are done. */
6737 if (!field)
6738 break;
6739
6740 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field))
6741 ? field : NULL_TREE);
6742 if (last_was_pack_expansion)
6743 /* Each non-trailing aggregate element that is a pack expansion is
6744 assumed to correspond to no elements of the initializer list. */
6745 goto continue_;
6746
6747 if (direct_desig)
6748 {
6749 /* The designated field F is initialized from this one element.
6750
6751 Note that we don't want to do this if we found the designator
6752 inside an anon aggr above; we use the normal code to implement:
6753
6754 "If the element is an anonymous union member and the initializer
6755 list is a brace-enclosed designated- initializer-list, the element
6756 is initialized by the designated-initializer-list { D }, where D
6757 is the designated- initializer-clause naming a member of the
6758 anonymous union member." */
6759 field_init = reshape_single_init (TREE_TYPE (field),
6760 d->cur->value, complain);
6761 d->cur++;
6762 }
6763 else
6764 field_init = reshape_init_r (TREE_TYPE (field), d,
6765 /*first_initializer_p=*/NULL_TREE,
6766 complain);
6767
6768 if (field_init == error_mark_node)
6769 return error_mark_node;
6770
6771 if (d->cur == old_cur && d->cur->index)
6772 {
6773 /* This can happen with an invalid initializer for a flexible
6774 array member (c++/54441). */
6775 if (complain & tf_error)
6776 error ("invalid initializer for %q#D", field);
6777 return error_mark_node;
6778 }
6779
6780 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6781
6782 /* [dcl.init.aggr]
6783
6784 When a union is initialized with a brace-enclosed
6785 initializer, the braces shall only contain an
6786 initializer for the first member of the union. */
6787 if (TREE_CODE (type) == UNION_TYPE)
6788 break;
6789
6790 continue_:
6791 if (base_binfo)
6792 {
6793 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo))
6794 field = base_binfo;
6795 else
6796 field = next_initializable_field (TYPE_FIELDS (type));
6797 }
6798 else
6799 field = next_initializable_field (DECL_CHAIN (field));
6800 }
6801
6802 /* A trailing aggregate element that is a pack expansion is assumed to
6803 correspond to all remaining elements of the initializer list (if any). */
6804 if (last_was_pack_expansion)
6805 {
6806 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6807 last_was_pack_expansion, d->cur->value);
6808 while (d->cur != d->end)
6809 d->cur++;
6810 }
6811
6812 return new_init;
6813 }
6814
6815 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6816 designators are not valid; either complain or return true to indicate
6817 that reshape_init_r should return error_mark_node. */
6818
6819 static bool
has_designator_problem(reshape_iter * d,tsubst_flags_t complain)6820 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6821 {
6822 if (d->cur->index)
6823 {
6824 if (complain & tf_error)
6825 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6826 "C99 designator %qE outside aggregate initializer",
6827 d->cur->index);
6828 else
6829 return true;
6830 }
6831 return false;
6832 }
6833
6834 /* Subroutine of reshape_init, which processes a single initializer (part of
6835 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6836 iterator within the CONSTRUCTOR which points to the initializer to process.
6837 If this is the first initializer of the outermost CONSTRUCTOR node,
6838 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */
6839
6840 static tree
reshape_init_r(tree type,reshape_iter * d,tree first_initializer_p,tsubst_flags_t complain)6841 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p,
6842 tsubst_flags_t complain)
6843 {
6844 tree init = d->cur->value;
6845
6846 if (error_operand_p (init))
6847 return error_mark_node;
6848
6849 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6850 && has_designator_problem (d, complain))
6851 return error_mark_node;
6852
6853 tree stripped_init = tree_strip_any_location_wrapper (init);
6854
6855 if (TREE_CODE (type) == COMPLEX_TYPE)
6856 {
6857 /* A complex type can be initialized from one or two initializers,
6858 but braces are not elided. */
6859 d->cur++;
6860 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6861 {
6862 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6863 {
6864 if (complain & tf_error)
6865 error ("too many initializers for %qT", type);
6866 else
6867 return error_mark_node;
6868 }
6869 }
6870 else if (first_initializer_p && d->cur != d->end)
6871 {
6872 vec<constructor_elt, va_gc> *v = 0;
6873 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6874 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6875 if (has_designator_problem (d, complain))
6876 return error_mark_node;
6877 d->cur++;
6878 init = build_constructor (init_list_type_node, v);
6879 }
6880 return init;
6881 }
6882
6883 /* A non-aggregate type is always initialized with a single
6884 initializer. */
6885 if (!CP_AGGREGATE_TYPE_P (type)
6886 /* As is an array with dependent bound, which we can see
6887 during C++20 aggregate CTAD. */
6888 || (cxx_dialect >= cxx20
6889 && TREE_CODE (type) == ARRAY_TYPE
6890 && uses_template_parms (TYPE_DOMAIN (type))))
6891 {
6892 /* It is invalid to initialize a non-aggregate type with a
6893 brace-enclosed initializer before C++0x.
6894 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6895 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6896 a CONSTRUCTOR (with a record type). */
6897 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6898 /* Don't complain about a capture-init. */
6899 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6900 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6901 {
6902 if (SCALAR_TYPE_P (type))
6903 {
6904 if (cxx_dialect < cxx11)
6905 {
6906 if (complain & tf_error)
6907 error ("braces around scalar initializer for type %qT",
6908 type);
6909 init = error_mark_node;
6910 }
6911 else if (first_initializer_p
6912 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6913 && (BRACE_ENCLOSED_INITIALIZER_P
6914 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6915 {
6916 if (complain & tf_error)
6917 error ("too many braces around scalar initializer "
6918 "for type %qT", type);
6919 init = error_mark_node;
6920 }
6921 }
6922 else
6923 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6924 }
6925
6926 d->cur++;
6927 return init;
6928 }
6929
6930 /* "If T is a class type and the initializer list has a single element of
6931 type cv U, where U is T or a class derived from T, the object is
6932 initialized from that element." Even if T is an aggregate. */
6933 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6934 && first_initializer_p
6935 /* But not if it's a designated init. */
6936 && !d->cur->index
6937 && d->end - d->cur == 1
6938 && reference_related_p (type, TREE_TYPE (init)))
6939 {
6940 d->cur++;
6941 return init;
6942 }
6943
6944 /* [dcl.init.aggr]
6945
6946 All implicit type conversions (clause _conv_) are considered when
6947 initializing the aggregate member with an initializer from an
6948 initializer-list. If the initializer can initialize a member,
6949 the member is initialized. Otherwise, if the member is itself a
6950 non-empty subaggregate, brace elision is assumed and the
6951 initializer is considered for the initialization of the first
6952 member of the subaggregate. */
6953 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init))
6954 /* But don't try this for the first initializer, since that would be
6955 looking through the outermost braces; A a2 = { a1 }; is not a
6956 valid aggregate initialization. */
6957 && !first_initializer_p
6958 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6959 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6960 complain)))
6961 {
6962 d->cur++;
6963 return init;
6964 }
6965
6966 /* [dcl.init.string]
6967
6968 A char array (whether plain char, signed char, or unsigned char)
6969 can be initialized by a string-literal (optionally enclosed in
6970 braces); a wchar_t array can be initialized by a wide
6971 string-literal (optionally enclosed in braces). */
6972 if (TREE_CODE (type) == ARRAY_TYPE
6973 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6974 {
6975 tree str_init = init;
6976 tree stripped_str_init = stripped_init;
6977 reshape_iter stripd = {};
6978
6979 /* Strip one level of braces if and only if they enclose a single
6980 element (as allowed by [dcl.init.string]). */
6981 if (!first_initializer_p
6982 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6983 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6984 {
6985 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0);
6986 str_init = stripd.cur->value;
6987 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6988 }
6989
6990 /* If it's a string literal, then it's the initializer for the array
6991 as a whole. Otherwise, continue with normal initialization for
6992 array types (one value per array element). */
6993 if (TREE_CODE (stripped_str_init) == STRING_CST)
6994 {
6995 if ((first_initializer_p && has_designator_problem (d, complain))
6996 || (stripd.cur && has_designator_problem (&stripd, complain)))
6997 return error_mark_node;
6998 d->cur++;
6999 return str_init;
7000 }
7001 }
7002
7003 /* The following cases are about aggregates. If we are not within a full
7004 initializer already, and there is not a CONSTRUCTOR, it means that there
7005 is a missing set of braces (that is, we are processing the case for
7006 which reshape_init exists). */
7007 bool braces_elided_p = false;
7008 if (!first_initializer_p)
7009 {
7010 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
7011 {
7012 tree init_type = TREE_TYPE (init);
7013 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
7014 /* There is no need to call reshape_init for pointer-to-member
7015 function initializers, as they are always constructed correctly
7016 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
7017 which is missing outermost braces. We should warn below, and
7018 one of the routines below will wrap it in additional { }. */;
7019 /* For a nested compound literal, proceed to specialized routines,
7020 to handle initialization of arrays and similar. */
7021 else if (COMPOUND_LITERAL_P (stripped_init))
7022 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7023 /* If we have an unresolved designator, we need to find the member it
7024 designates within TYPE, so proceed to the routines below. For
7025 FIELD_DECL or INTEGER_CST designators, we're already initializing
7026 the designated element. */
7027 else if (d->cur->index
7028 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
7029 /* Brace elision with designators is only permitted for anonymous
7030 aggregates. */
7031 gcc_checking_assert (ANON_AGGR_TYPE_P (type));
7032 /* A CONSTRUCTOR of the target's type is a previously
7033 digested initializer. */
7034 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
7035 {
7036 ++d->cur;
7037 return init;
7038 }
7039 else
7040 {
7041 /* Something that hasn't been reshaped yet. */
7042 ++d->cur;
7043 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
7044 return reshape_init (type, init, complain);
7045 }
7046 }
7047
7048 if (complain & tf_warning)
7049 warning (OPT_Wmissing_braces,
7050 "missing braces around initializer for %qT",
7051 type);
7052 braces_elided_p = true;
7053 }
7054
7055 /* Dispatch to specialized routines. */
7056 tree new_init;
7057 if (CLASS_TYPE_P (type))
7058 new_init = reshape_init_class (type, d, first_initializer_p, complain);
7059 else if (TREE_CODE (type) == ARRAY_TYPE)
7060 new_init = reshape_init_array (type, d, first_initializer_p, complain);
7061 else if (VECTOR_TYPE_P (type))
7062 new_init = reshape_init_vector (type, d, complain);
7063 else
7064 gcc_unreachable();
7065
7066 if (braces_elided_p
7067 && TREE_CODE (new_init) == CONSTRUCTOR)
7068 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true;
7069
7070 return new_init;
7071 }
7072
7073 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7074 brace-enclosed aggregate initializer.
7075
7076 INIT is the CONSTRUCTOR containing the list of initializers describing
7077 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
7078 It may not presently match the shape of the TYPE; for example:
7079
7080 struct S { int a; int b; };
7081 struct S a[] = { 1, 2, 3, 4 };
7082
7083 Here INIT will hold a vector of four elements, rather than a
7084 vector of two elements, each itself a vector of two elements. This
7085 routine transforms INIT from the former form into the latter. The
7086 revised CONSTRUCTOR node is returned. */
7087
7088 tree
reshape_init(tree type,tree init,tsubst_flags_t complain)7089 reshape_init (tree type, tree init, tsubst_flags_t complain)
7090 {
7091 vec<constructor_elt, va_gc> *v;
7092 reshape_iter d;
7093 tree new_init;
7094
7095 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
7096
7097 v = CONSTRUCTOR_ELTS (init);
7098
7099 /* An empty constructor does not need reshaping, and it is always a valid
7100 initializer. */
7101 if (vec_safe_is_empty (v))
7102 return init;
7103
7104 /* Brace elision is not performed for a CONSTRUCTOR representing
7105 parenthesized aggregate initialization. */
7106 if (CONSTRUCTOR_IS_PAREN_INIT (init))
7107 {
7108 tree elt = (*v)[0].value;
7109 /* If we're initializing a char array from a string-literal that is
7110 enclosed in braces, unwrap it here. */
7111 if (TREE_CODE (type) == ARRAY_TYPE
7112 && vec_safe_length (v) == 1
7113 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
7114 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST)
7115 return elt;
7116 return init;
7117 }
7118
7119 /* Handle [dcl.init.list] direct-list-initialization from
7120 single element of enumeration with a fixed underlying type. */
7121 if (is_direct_enum_init (type, init))
7122 {
7123 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
7124 type = cv_unqualified (type);
7125 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
7126 {
7127 warning_sentinel w (warn_useless_cast);
7128 warning_sentinel w2 (warn_ignored_qualifiers);
7129 return cp_build_c_cast (input_location, type, elt,
7130 tf_warning_or_error);
7131 }
7132 else
7133 return error_mark_node;
7134 }
7135
7136 /* Recurse on this CONSTRUCTOR. */
7137 d.cur = &(*v)[0];
7138 d.end = d.cur + v->length ();
7139
7140 new_init = reshape_init_r (type, &d, init, complain);
7141 if (new_init == error_mark_node)
7142 return error_mark_node;
7143
7144 /* Make sure all the element of the constructor were used. Otherwise,
7145 issue an error about exceeding initializers. */
7146 if (d.cur != d.end)
7147 {
7148 if (complain & tf_error)
7149 error ("too many initializers for %qT", type);
7150 return error_mark_node;
7151 }
7152
7153 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
7154 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7155 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
7156 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
7157 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
7158 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
7159
7160 return new_init;
7161 }
7162
7163 /* Verify array initializer. Returns true if errors have been reported. */
7164
7165 bool
check_array_initializer(tree decl,tree type,tree init)7166 check_array_initializer (tree decl, tree type, tree init)
7167 {
7168 tree element_type = TREE_TYPE (type);
7169
7170 /* Structured binding when initialized with an array type needs
7171 to have complete type. */
7172 if (decl
7173 && DECL_DECOMPOSITION_P (decl)
7174 && !DECL_DECOMP_BASE (decl)
7175 && !COMPLETE_TYPE_P (type))
7176 {
7177 error_at (DECL_SOURCE_LOCATION (decl),
7178 "structured binding has incomplete type %qT", type);
7179 TREE_TYPE (decl) = error_mark_node;
7180 return true;
7181 }
7182
7183 /* The array type itself need not be complete, because the
7184 initializer may tell us how many elements are in the array.
7185 But, the elements of the array must be complete. */
7186 if (!COMPLETE_TYPE_P (complete_type (element_type)))
7187 {
7188 if (decl)
7189 error_at (DECL_SOURCE_LOCATION (decl),
7190 "elements of array %q#D have incomplete type", decl);
7191 else
7192 error ("elements of array %q#T have incomplete type", type);
7193 return true;
7194 }
7195
7196 location_t loc = (decl ? location_of (decl) : input_location);
7197 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
7198 return true;
7199
7200 /* A compound literal can't have variable size. */
7201 if (init && !decl
7202 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
7203 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
7204 {
7205 error ("variable-sized compound literal");
7206 return true;
7207 }
7208 return false;
7209 }
7210
7211 /* Subroutine of check_initializer; args are passed down from that function.
7212 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
7213
7214 static tree
build_aggr_init_full_exprs(tree decl,tree init,int flags)7215 build_aggr_init_full_exprs (tree decl, tree init, int flags)
7216
7217 {
7218 gcc_assert (stmts_are_full_exprs_p ());
7219 return build_aggr_init (decl, init, flags, tf_warning_or_error);
7220 }
7221
7222 /* Verify INIT (the initializer for DECL), and record the
7223 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
7224 grok_reference_init.
7225
7226 If the return value is non-NULL, it is an expression that must be
7227 evaluated dynamically to initialize DECL. */
7228
7229 static tree
check_initializer(tree decl,tree init,int flags,vec<tree,va_gc> ** cleanups)7230 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
7231 {
7232 tree type;
7233 tree init_code = NULL;
7234 tree core_type;
7235
7236 /* Things that are going to be initialized need to have complete
7237 type. */
7238 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7239
7240 if (DECL_HAS_VALUE_EXPR_P (decl))
7241 {
7242 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
7243 it doesn't have storage to be initialized. */
7244 gcc_assert (init == NULL_TREE);
7245 return NULL_TREE;
7246 }
7247
7248 if (type == error_mark_node)
7249 /* We will have already complained. */
7250 return NULL_TREE;
7251
7252 if (TREE_CODE (type) == ARRAY_TYPE)
7253 {
7254 if (check_array_initializer (decl, type, init))
7255 return NULL_TREE;
7256 }
7257 else if (!COMPLETE_TYPE_P (type))
7258 {
7259 error_at (DECL_SOURCE_LOCATION (decl),
7260 "%q#D has incomplete type", decl);
7261 TREE_TYPE (decl) = error_mark_node;
7262 return NULL_TREE;
7263 }
7264 else
7265 /* There is no way to make a variable-sized class type in GNU C++. */
7266 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
7267
7268 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
7269 {
7270 int init_len = CONSTRUCTOR_NELTS (init);
7271 if (SCALAR_TYPE_P (type))
7272 {
7273 if (init_len == 0)
7274 {
7275 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7276 init = build_zero_init (type, NULL_TREE, false);
7277 }
7278 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
7279 {
7280 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7281 "scalar object %qD requires one element in "
7282 "initializer", decl);
7283 TREE_TYPE (decl) = error_mark_node;
7284 return NULL_TREE;
7285 }
7286 }
7287 }
7288
7289 if (TREE_CODE (decl) == CONST_DECL)
7290 {
7291 gcc_assert (!TYPE_REF_P (type));
7292
7293 DECL_INITIAL (decl) = init;
7294
7295 gcc_assert (init != NULL_TREE);
7296 init = NULL_TREE;
7297 }
7298 else if (!init && DECL_REALLY_EXTERN (decl))
7299 ;
7300 else if (init || type_build_ctor_call (type)
7301 || TYPE_REF_P (type))
7302 {
7303 if (TYPE_REF_P (type))
7304 {
7305 init = grok_reference_init (decl, type, init, flags);
7306 flags |= LOOKUP_ALREADY_DIGESTED;
7307 }
7308 else if (!init)
7309 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7310 tf_warning_or_error);
7311 /* Do not reshape constructors of vectors (they don't need to be
7312 reshaped. */
7313 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
7314 {
7315 if (is_std_init_list (type))
7316 {
7317 init = perform_implicit_conversion (type, init,
7318 tf_warning_or_error);
7319 flags |= LOOKUP_ALREADY_DIGESTED;
7320 }
7321 else if (TYPE_NON_AGGREGATE_CLASS (type))
7322 {
7323 /* Don't reshape if the class has constructors. */
7324 if (cxx_dialect == cxx98)
7325 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
7326 "in C++98 %qD must be initialized by "
7327 "constructor, not by %<{...}%>",
7328 decl);
7329 }
7330 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
7331 {
7332 error ("opaque vector types cannot be initialized");
7333 init = error_mark_node;
7334 }
7335 else
7336 {
7337 init = reshape_init (type, init, tf_warning_or_error);
7338 flags |= LOOKUP_NO_NARROWING;
7339 }
7340 }
7341 /* [dcl.init] "Otherwise, if the destination type is an array, the object
7342 is initialized as follows..." So handle things like
7343
7344 int a[](1, 2, 3);
7345
7346 which is permitted in C++20 by P0960. */
7347 else if (TREE_CODE (init) == TREE_LIST
7348 && TREE_TYPE (init) == NULL_TREE
7349 && TREE_CODE (type) == ARRAY_TYPE
7350 && !DECL_DECOMPOSITION_P (decl)
7351 && (cxx_dialect >= cxx20))
7352 init = do_aggregate_paren_init (init, type);
7353 else if (TREE_CODE (init) == TREE_LIST
7354 && TREE_TYPE (init) != unknown_type_node
7355 && !MAYBE_CLASS_TYPE_P (type))
7356 {
7357 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7358
7359 /* We get here with code like `int a (2);' */
7360 init = build_x_compound_expr_from_list (init, ELK_INIT,
7361 tf_warning_or_error);
7362 }
7363
7364 /* If DECL has an array type without a specific bound, deduce the
7365 array size from the initializer. */
7366 maybe_deduce_size_from_array_init (decl, init);
7367 type = TREE_TYPE (decl);
7368 if (type == error_mark_node)
7369 return NULL_TREE;
7370
7371 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
7372 && !(flags & LOOKUP_ALREADY_DIGESTED)
7373 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
7374 && CP_AGGREGATE_TYPE_P (type)
7375 && (CLASS_TYPE_P (type)
7376 /* The call to build_aggr_init below could end up
7377 calling build_vec_init, which may break when we
7378 are processing a template. */
7379 || processing_template_decl
7380 || !TYPE_NEEDS_CONSTRUCTING (type)
7381 || type_has_extended_temps (type))))
7382 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
7383 {
7384 init_code = build_aggr_init_full_exprs (decl, init, flags);
7385
7386 /* A constructor call is a non-trivial initializer even if
7387 it isn't explicitly written. */
7388 if (TREE_SIDE_EFFECTS (init_code))
7389 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
7390
7391 /* If this is a constexpr initializer, expand_default_init will
7392 have returned an INIT_EXPR rather than a CALL_EXPR. In that
7393 case, pull the initializer back out and pass it down into
7394 store_init_value. */
7395 while (true)
7396 {
7397 if (TREE_CODE (init_code) == EXPR_STMT
7398 || TREE_CODE (init_code) == STMT_EXPR
7399 || TREE_CODE (init_code) == CONVERT_EXPR)
7400 init_code = TREE_OPERAND (init_code, 0);
7401 else if (TREE_CODE (init_code) == BIND_EXPR)
7402 init_code = BIND_EXPR_BODY (init_code);
7403 else
7404 break;
7405 }
7406 if (TREE_CODE (init_code) == INIT_EXPR)
7407 {
7408 /* In C++20, the call to build_aggr_init could have created
7409 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
7410 A(1, 2). */
7411 tree rhs = TREE_OPERAND (init_code, 1);
7412 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
7413 /* Avoid leaking TARGET_EXPR into template trees. */
7414 rhs = build_implicit_conv_flags (type, init, flags);
7415 init = rhs;
7416
7417 init_code = NULL_TREE;
7418 /* Don't call digest_init; it's unnecessary and will complain
7419 about aggregate initialization of non-aggregate classes. */
7420 flags |= LOOKUP_ALREADY_DIGESTED;
7421 }
7422 else if (DECL_DECLARED_CONSTEXPR_P (decl)
7423 || DECL_DECLARED_CONSTINIT_P (decl))
7424 {
7425 /* Declared constexpr or constinit, but no suitable initializer;
7426 massage init appropriately so we can pass it into
7427 store_init_value for the error. */
7428 tree new_init = NULL_TREE;
7429 if (!processing_template_decl
7430 && TREE_CODE (init_code) == CALL_EXPR)
7431 new_init = build_cplus_new (type, init_code, tf_none);
7432 else if (CLASS_TYPE_P (type)
7433 && (!init || TREE_CODE (init) == TREE_LIST))
7434 new_init = build_functional_cast (input_location, type,
7435 init, tf_none);
7436 if (new_init)
7437 {
7438 init = new_init;
7439 if (TREE_CODE (init) == TARGET_EXPR
7440 && !(flags & LOOKUP_ONLYCONVERTING))
7441 TARGET_EXPR_DIRECT_INIT_P (init) = true;
7442 }
7443 init_code = NULL_TREE;
7444 }
7445 else
7446 init = NULL_TREE;
7447 }
7448
7449 if (init && TREE_CODE (init) != TREE_VEC)
7450 {
7451 init_code = store_init_value (decl, init, cleanups, flags);
7452
7453 if (DECL_INITIAL (decl)
7454 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
7455 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
7456 {
7457 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
7458 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
7459 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
7460 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
7461 }
7462
7463 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
7464 && DECL_INITIAL (decl)
7465 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7466 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
7467 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
7468 DECL_SOURCE_LOCATION (decl)),
7469 0, "array %qD initialized by parenthesized "
7470 "string literal %qE",
7471 decl, DECL_INITIAL (decl));
7472 init = NULL_TREE;
7473 }
7474 }
7475 else
7476 {
7477 if (CLASS_TYPE_P (core_type = strip_array_types (type))
7478 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
7479 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
7480 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
7481 /*complain=*/true);
7482
7483 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
7484 tf_warning_or_error);
7485 }
7486
7487 if (init && init != error_mark_node)
7488 init_code = build2 (INIT_EXPR, type, decl, init);
7489
7490 if (init_code && !TREE_SIDE_EFFECTS (init_code)
7491 && init_code != error_mark_node)
7492 init_code = NULL_TREE;
7493
7494 if (init_code)
7495 {
7496 /* We might have set these in cp_finish_decl. */
7497 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
7498 TREE_CONSTANT (decl) = false;
7499 }
7500
7501 if (init_code
7502 && DECL_IN_AGGR_P (decl)
7503 && DECL_INITIALIZED_IN_CLASS_P (decl))
7504 {
7505 static int explained = 0;
7506
7507 if (cxx_dialect < cxx11)
7508 error ("initializer invalid for static member with constructor");
7509 else if (cxx_dialect < cxx17)
7510 error ("non-constant in-class initialization invalid for static "
7511 "member %qD", decl);
7512 else
7513 error ("non-constant in-class initialization invalid for non-inline "
7514 "static member %qD", decl);
7515 if (!explained)
7516 {
7517 inform (input_location,
7518 "(an out of class initialization is required)");
7519 explained = 1;
7520 }
7521 return NULL_TREE;
7522 }
7523
7524 return init_code;
7525 }
7526
7527 /* If DECL is not a local variable, give it RTL. */
7528
7529 static void
make_rtl_for_nonlocal_decl(tree decl,tree init,const char * asmspec)7530 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7531 {
7532 int toplev = toplevel_bindings_p ();
7533 int defer_p;
7534
7535 /* Set the DECL_ASSEMBLER_NAME for the object. */
7536 if (asmspec)
7537 {
7538 /* The `register' keyword, when used together with an
7539 asm-specification, indicates that the variable should be
7540 placed in a particular register. */
7541 if (VAR_P (decl) && DECL_REGISTER (decl))
7542 {
7543 set_user_assembler_name (decl, asmspec);
7544 DECL_HARD_REGISTER (decl) = 1;
7545 }
7546 else
7547 {
7548 if (TREE_CODE (decl) == FUNCTION_DECL
7549 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
7550 set_builtin_user_assembler_name (decl, asmspec);
7551 set_user_assembler_name (decl, asmspec);
7552 if (DECL_LOCAL_DECL_P (decl))
7553 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl))
7554 /* We have to propagate the name to the ns-alias.
7555 This is horrible, as we're affecting a
7556 possibly-shared decl. Again, a one-true-decl
7557 model breaks down. */
7558 if (ns_decl != error_mark_node)
7559 set_user_assembler_name (ns_decl, asmspec);
7560 }
7561 }
7562
7563 /* Handle non-variables up front. */
7564 if (!VAR_P (decl))
7565 {
7566 rest_of_decl_compilation (decl, toplev, at_eof);
7567 return;
7568 }
7569
7570 /* If we see a class member here, it should be a static data
7571 member. */
7572 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7573 {
7574 gcc_assert (TREE_STATIC (decl));
7575 /* An in-class declaration of a static data member should be
7576 external; it is only a declaration, and not a definition. */
7577 if (init == NULL_TREE)
7578 gcc_assert (DECL_EXTERNAL (decl)
7579 || !TREE_PUBLIC (decl));
7580 }
7581
7582 /* We don't create any RTL for local variables. */
7583 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7584 return;
7585
7586 /* We defer emission of local statics until the corresponding
7587 DECL_EXPR is expanded. But with constexpr its function might never
7588 be expanded, so go ahead and tell cgraph about the variable now. */
7589 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
7590 && !var_in_maybe_constexpr_fn (decl))
7591 || DECL_VIRTUAL_P (decl));
7592
7593 /* Defer template instantiations. */
7594 if (DECL_LANG_SPECIFIC (decl)
7595 && DECL_IMPLICIT_INSTANTIATION (decl))
7596 defer_p = 1;
7597
7598 /* If we're not deferring, go ahead and assemble the variable. */
7599 if (!defer_p)
7600 rest_of_decl_compilation (decl, toplev, at_eof);
7601 }
7602
7603 /* walk_tree helper for wrap_temporary_cleanups, below. */
7604
7605 static tree
wrap_cleanups_r(tree * stmt_p,int * walk_subtrees,void * data)7606 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
7607 {
7608 /* Stop at types or full-expression boundaries. */
7609 if (TYPE_P (*stmt_p)
7610 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
7611 {
7612 *walk_subtrees = 0;
7613 return NULL_TREE;
7614 }
7615
7616 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
7617 {
7618 tree guard = (tree)data;
7619 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
7620
7621 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p)
7622 && !expr_noexcept_p (tcleanup, tf_none))
7623 {
7624 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
7625 /* Tell honor_protect_cleanup_actions to handle this as a separate
7626 cleanup. */
7627 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
7628 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
7629 }
7630 }
7631
7632 return NULL_TREE;
7633 }
7634
7635 /* We're initializing a local variable which has a cleanup GUARD. If there
7636 are any temporaries used in the initializer INIT of this variable, we
7637 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
7638 variable will be cleaned up properly if one of them throws.
7639
7640 Unfortunately, there's no way to express this properly in terms of
7641 nesting, as the regions for the temporaries overlap the region for the
7642 variable itself; if there are two temporaries, the variable needs to be
7643 the first thing destroyed if either of them throws. However, we only
7644 want to run the variable's cleanup if it actually got constructed. So
7645 we need to guard the temporary cleanups with the variable's cleanup if
7646 they are run on the normal path, but not if they are run on the
7647 exceptional path. We implement this by telling
7648 honor_protect_cleanup_actions to strip the variable cleanup from the
7649 exceptional path.
7650
7651 Another approach could be to make the variable cleanup region enclose
7652 initialization, but depend on a flag to indicate that the variable is
7653 initialized; that's effectively what we do for arrays. But the current
7654 approach works fine for non-arrays, and has no code overhead in the usual
7655 case where the temporary destructors are noexcept. */
7656
7657 static void
wrap_temporary_cleanups(tree init,tree guard)7658 wrap_temporary_cleanups (tree init, tree guard)
7659 {
7660 if (TREE_CODE (guard) == BIND_EXPR)
7661 {
7662 /* An array cleanup region already encloses any temporary cleanups,
7663 don't wrap it around them again. */
7664 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard));
7665 return;
7666 }
7667 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
7668 }
7669
7670 /* Generate code to initialize DECL (a local variable). */
7671
7672 static void
initialize_local_var(tree decl,tree init)7673 initialize_local_var (tree decl, tree init)
7674 {
7675 tree type = TREE_TYPE (decl);
7676 tree cleanup;
7677 int already_used;
7678
7679 gcc_assert (VAR_P (decl)
7680 || TREE_CODE (decl) == RESULT_DECL);
7681 gcc_assert (!TREE_STATIC (decl));
7682
7683 if (DECL_SIZE (decl) == NULL_TREE)
7684 {
7685 /* If we used it already as memory, it must stay in memory. */
7686 DECL_INITIAL (decl) = NULL_TREE;
7687 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7688 return;
7689 }
7690
7691 if (type == error_mark_node)
7692 return;
7693
7694 /* Compute and store the initial value. */
7695 already_used = TREE_USED (decl) || TREE_USED (type);
7696 if (TREE_USED (type))
7697 DECL_READ_P (decl) = 1;
7698
7699 /* Generate a cleanup, if necessary. */
7700 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7701
7702 /* Perform the initialization. */
7703 if (init)
7704 {
7705 tree rinit = (TREE_CODE (init) == INIT_EXPR
7706 ? TREE_OPERAND (init, 1) : NULL_TREE);
7707 if (rinit && !TREE_SIDE_EFFECTS (rinit)
7708 && TREE_OPERAND (init, 0) == decl)
7709 {
7710 /* Stick simple initializers in DECL_INITIAL so that
7711 -Wno-init-self works (c++/34772). */
7712 DECL_INITIAL (decl) = rinit;
7713
7714 if (warn_init_self && TYPE_REF_P (type))
7715 {
7716 STRIP_NOPS (rinit);
7717 if (rinit == decl)
7718 warning_at (DECL_SOURCE_LOCATION (decl),
7719 OPT_Winit_self,
7720 "reference %qD is initialized with itself", decl);
7721 }
7722 }
7723 else
7724 {
7725 int saved_stmts_are_full_exprs_p;
7726
7727 /* If we're only initializing a single object, guard the
7728 destructors of any temporaries used in its initializer with
7729 its destructor. */
7730 if (cleanup)
7731 wrap_temporary_cleanups (init, cleanup);
7732
7733 gcc_assert (building_stmt_list_p ());
7734 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7735 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7736 finish_expr_stmt (init);
7737 current_stmt_tree ()->stmts_are_full_exprs_p =
7738 saved_stmts_are_full_exprs_p;
7739 }
7740 }
7741
7742 /* Set this to 0 so we can tell whether an aggregate which was
7743 initialized was ever used. Don't do this if it has a
7744 destructor, so we don't complain about the 'resource
7745 allocation is initialization' idiom. Now set
7746 attribute((unused)) on types so decls of that type will be
7747 marked used. (see TREE_USED, above.) */
7748 if (TYPE_NEEDS_CONSTRUCTING (type)
7749 && ! already_used
7750 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7751 && DECL_NAME (decl))
7752 TREE_USED (decl) = 0;
7753 else if (already_used)
7754 TREE_USED (decl) = 1;
7755
7756 if (cleanup)
7757 finish_decl_cleanup (decl, cleanup);
7758 }
7759
7760 /* DECL is a VAR_DECL for a compiler-generated variable with static
7761 storage duration (like a virtual table) whose initializer is a
7762 compile-time constant. Initialize the variable and provide it to the
7763 back end. */
7764
7765 void
initialize_artificial_var(tree decl,vec<constructor_elt,va_gc> * v)7766 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7767 {
7768 tree init;
7769 gcc_assert (DECL_ARTIFICIAL (decl));
7770 init = build_constructor (TREE_TYPE (decl), v);
7771 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7772 DECL_INITIAL (decl) = init;
7773 DECL_INITIALIZED_P (decl) = 1;
7774 /* Mark the decl as constexpr so that we can access its content
7775 at compile time. */
7776 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7777 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7778 determine_visibility (decl);
7779 layout_var_decl (decl);
7780 maybe_commonize_var (decl);
7781 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7782 }
7783
7784 /* INIT is the initializer for a variable, as represented by the
7785 parser. Returns true iff INIT is value-dependent. */
7786
7787 static bool
value_dependent_init_p(tree init)7788 value_dependent_init_p (tree init)
7789 {
7790 if (TREE_CODE (init) == TREE_LIST)
7791 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7792 return any_value_dependent_elements_p (init);
7793 else if (TREE_CODE (init) == CONSTRUCTOR)
7794 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7795 {
7796 if (dependent_type_p (TREE_TYPE (init)))
7797 return true;
7798
7799 vec<constructor_elt, va_gc> *elts;
7800 size_t nelts;
7801 size_t i;
7802
7803 elts = CONSTRUCTOR_ELTS (init);
7804 nelts = vec_safe_length (elts);
7805 for (i = 0; i < nelts; ++i)
7806 if (value_dependent_init_p ((*elts)[i].value))
7807 return true;
7808 }
7809 else
7810 /* It must be a simple expression, e.g., int i = 3; */
7811 return value_dependent_expression_p (init);
7812
7813 return false;
7814 }
7815
7816 // Returns true if a DECL is VAR_DECL with the concept specifier.
7817 static inline bool
is_concept_var(tree decl)7818 is_concept_var (tree decl)
7819 {
7820 return (VAR_P (decl)
7821 // Not all variables have DECL_LANG_SPECIFIC.
7822 && DECL_LANG_SPECIFIC (decl)
7823 && DECL_DECLARED_CONCEPT_P (decl));
7824 }
7825
7826 /* A helper function to be called via walk_tree. If any label exists
7827 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7828
7829 static tree
notice_forced_label_r(tree * tp,int * walk_subtrees,void *)7830 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7831 {
7832 if (TYPE_P (*tp))
7833 *walk_subtrees = 0;
7834 if (TREE_CODE (*tp) == LABEL_DECL)
7835 cfun->has_forced_label_in_static = 1;
7836 return NULL_TREE;
7837 }
7838
7839 /* Return true if DECL has either a trivial destructor, or for C++20
7840 is constexpr and has a constexpr destructor. */
7841
7842 static bool
decl_maybe_constant_destruction(tree decl,tree type)7843 decl_maybe_constant_destruction (tree decl, tree type)
7844 {
7845 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7846 || (cxx_dialect >= cxx20
7847 && VAR_P (decl)
7848 && DECL_DECLARED_CONSTEXPR_P (decl)
7849 && type_has_constexpr_destructor (strip_array_types (type))));
7850 }
7851
7852 static tree declare_simd_adjust_this (tree *, int *, void *);
7853
7854 /* Helper function of omp_declare_variant_finalize. Finalize one
7855 "omp declare variant base" attribute. Return true if it should be
7856 removed. */
7857
7858 static bool
omp_declare_variant_finalize_one(tree decl,tree attr)7859 omp_declare_variant_finalize_one (tree decl, tree attr)
7860 {
7861 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7862 {
7863 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7864 DECL_ARGUMENTS (decl), NULL);
7865 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7866 DECL_ARGUMENTS (decl), NULL);
7867 }
7868
7869 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7870 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7871 if (simd)
7872 {
7873 TREE_VALUE (simd)
7874 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7875 TREE_VALUE (simd));
7876 /* FIXME, adjusting simd args unimplemented. */
7877 return true;
7878 }
7879
7880 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7881 location_t varid_loc
7882 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7883 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7884 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7885 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7886
7887 location_t save_loc = input_location;
7888 input_location = varid_loc;
7889
7890 releasing_vec args;
7891 tree parm = DECL_ARGUMENTS (decl);
7892 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7893 parm = DECL_CHAIN (parm);
7894 for (; parm; parm = DECL_CHAIN (parm))
7895 if (type_dependent_expression_p (parm))
7896 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7897 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7898 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7899 else
7900 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7901
7902 bool koenig_p = false;
7903 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7904 {
7905 if (identifier_p (variant)
7906 /* In C++20, we may need to perform ADL for a template
7907 name. */
7908 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7909 && identifier_p (TREE_OPERAND (variant, 0))))
7910 {
7911 if (!args->is_empty ())
7912 {
7913 koenig_p = true;
7914 if (!any_type_dependent_arguments_p (args))
7915 variant = perform_koenig_lookup (variant, args,
7916 tf_warning_or_error);
7917 }
7918 else
7919 variant = unqualified_fn_lookup_error (variant);
7920 }
7921 else if (!args->is_empty () && is_overloaded_fn (variant))
7922 {
7923 tree fn = get_first_fn (variant);
7924 fn = STRIP_TEMPLATE (fn);
7925 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7926 || DECL_FUNCTION_MEMBER_P (fn)
7927 || DECL_LOCAL_DECL_P (fn)))
7928 {
7929 koenig_p = true;
7930 if (!any_type_dependent_arguments_p (args))
7931 variant = perform_koenig_lookup (variant, args,
7932 tf_warning_or_error);
7933 }
7934 }
7935 }
7936
7937 if (idk == CP_ID_KIND_QUALIFIED)
7938 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7939 koenig_p, tf_warning_or_error);
7940 else
7941 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7942 koenig_p, tf_warning_or_error);
7943 if (variant == error_mark_node && !processing_template_decl)
7944 return true;
7945
7946 variant = cp_get_callee_fndecl_nofold (variant);
7947 input_location = save_loc;
7948
7949 if (variant)
7950 {
7951 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7952 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7953 {
7954 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7955 "types", variant, decl);
7956 return true;
7957 }
7958 if (fndecl_built_in_p (variant)
7959 && (startswith (varname, "__builtin_")
7960 || startswith (varname, "__sync_")
7961 || startswith (varname, "__atomic_")))
7962 {
7963 error_at (varid_loc, "variant %qD is a built-in", variant);
7964 return true;
7965 }
7966 else
7967 {
7968 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7969 omp_mark_declare_variant (match_loc, variant, construct);
7970 if (!omp_context_selector_matches (ctx))
7971 return true;
7972 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7973 }
7974 }
7975 else if (!processing_template_decl)
7976 {
7977 error_at (varid_loc, "could not find variant declaration");
7978 return true;
7979 }
7980
7981 return false;
7982 }
7983
7984 /* Helper function, finish up "omp declare variant base" attribute
7985 now that there is a DECL. ATTR is the first "omp declare variant base"
7986 attribute. */
7987
7988 void
omp_declare_variant_finalize(tree decl,tree attr)7989 omp_declare_variant_finalize (tree decl, tree attr)
7990 {
7991 size_t attr_len = strlen ("omp declare variant base");
7992 tree *list = &DECL_ATTRIBUTES (decl);
7993 bool remove_all = false;
7994 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7995 if (TREE_CHAIN (TREE_VALUE (attr))
7996 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7997 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7998 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7999 if (DECL_CONSTRUCTOR_P (decl))
8000 {
8001 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
8002 remove_all = true;
8003 }
8004 else if (DECL_DESTRUCTOR_P (decl))
8005 {
8006 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
8007 remove_all = true;
8008 }
8009 else if (DECL_DEFAULTED_FN (decl))
8010 {
8011 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
8012 remove_all = true;
8013 }
8014 else if (DECL_DELETED_FN (decl))
8015 {
8016 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
8017 remove_all = true;
8018 }
8019 else if (DECL_VIRTUAL_P (decl))
8020 {
8021 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
8022 remove_all = true;
8023 }
8024 /* This loop is like private_lookup_attribute, except that it works
8025 with tree * rather than tree, as we might want to remove the
8026 attributes that are diagnosed as errorneous. */
8027 while (*list)
8028 {
8029 tree attr = get_attribute_name (*list);
8030 size_t ident_len = IDENTIFIER_LENGTH (attr);
8031 if (cmp_attribs ("omp declare variant base", attr_len,
8032 IDENTIFIER_POINTER (attr), ident_len))
8033 {
8034 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
8035 {
8036 *list = TREE_CHAIN (*list);
8037 continue;
8038 }
8039 }
8040 list = &TREE_CHAIN (*list);
8041 }
8042 }
8043
8044 /* Finish processing of a declaration;
8045 install its line number and initial value.
8046 If the length of an array type is not known before,
8047 it must be determined now, from the initial value, or it is an error.
8048
8049 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
8050 true, then INIT is an integral constant expression.
8051
8052 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8053 if the (init) syntax was used. */
8054
8055 void
cp_finish_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)8056 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
8057 tree asmspec_tree, int flags)
8058 {
8059 tree type;
8060 vec<tree, va_gc> *cleanups = NULL;
8061 const char *asmspec = NULL;
8062 int was_readonly = 0;
8063 bool var_definition_p = false;
8064 tree auto_node;
8065
8066 if (decl == error_mark_node)
8067 return;
8068 else if (! decl)
8069 {
8070 if (init)
8071 error ("assignment (not initialization) in declaration");
8072 return;
8073 }
8074
8075 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
8076 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
8077 gcc_assert (TREE_CODE (decl) != PARM_DECL);
8078
8079 type = TREE_TYPE (decl);
8080 if (type == error_mark_node)
8081 return;
8082
8083 if (VAR_P (decl) && is_copy_initialization (init))
8084 flags |= LOOKUP_ONLYCONVERTING;
8085
8086 /* Warn about register storage specifiers except when in GNU global
8087 or local register variable extension. */
8088 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
8089 {
8090 if (cxx_dialect >= cxx17)
8091 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8092 "ISO C++17 does not allow %<register%> storage "
8093 "class specifier");
8094 else
8095 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
8096 "%<register%> storage class specifier used");
8097 }
8098
8099 /* If a name was specified, get the string. */
8100 if (at_namespace_scope_p ())
8101 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8102 if (asmspec_tree && asmspec_tree != error_mark_node)
8103 asmspec = TREE_STRING_POINTER (asmspec_tree);
8104
8105 bool in_class_decl
8106 = (current_class_type
8107 && CP_DECL_CONTEXT (decl) == current_class_type
8108 && TYPE_BEING_DEFINED (current_class_type)
8109 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type));
8110
8111 if (in_class_decl
8112 && (DECL_INITIAL (decl) || init))
8113 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8114
8115 if (TREE_CODE (decl) != FUNCTION_DECL
8116 && (auto_node = type_uses_auto (type)))
8117 {
8118 tree d_init;
8119 if (init == NULL_TREE)
8120 {
8121 if (DECL_LANG_SPECIFIC (decl)
8122 && DECL_TEMPLATE_INSTANTIATION (decl)
8123 && !DECL_TEMPLATE_INSTANTIATED (decl))
8124 {
8125 /* init is null because we're deferring instantiating the
8126 initializer until we need it. Well, we need it now. */
8127 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
8128 return;
8129 }
8130
8131 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
8132 }
8133 d_init = init;
8134 if (d_init)
8135 {
8136 if (TREE_CODE (d_init) == TREE_LIST
8137 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
8138 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
8139 tf_warning_or_error);
8140 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
8141 /* Force auto deduction now. Use tf_none to avoid redundant warnings
8142 on deprecated-14.C. */
8143 mark_single_function (d_init, tf_none);
8144 }
8145 enum auto_deduction_context adc = adc_variable_type;
8146 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
8147 adc = adc_decomp_type;
8148 tree outer_targs = NULL_TREE;
8149 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)
8150 && VAR_P (decl)
8151 && DECL_LANG_SPECIFIC (decl)
8152 && DECL_TEMPLATE_INFO (decl)
8153 && !DECL_FUNCTION_SCOPE_P (decl))
8154 /* The outer template arguments might be needed for satisfaction.
8155 (For function scope variables, do_auto_deduction will obtain the
8156 outer template arguments from current_function_decl.) */
8157 outer_targs = DECL_TI_ARGS (decl);
8158 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
8159 tf_warning_or_error, adc,
8160 outer_targs, flags);
8161 if (type == error_mark_node)
8162 return;
8163 if (TREE_CODE (type) == FUNCTION_TYPE)
8164 {
8165 error ("initializer for %<decltype(auto) %D%> has function type; "
8166 "did you forget the %<()%>?", decl);
8167 TREE_TYPE (decl) = error_mark_node;
8168 return;
8169 }
8170 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
8171 }
8172
8173 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
8174 {
8175 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
8176 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
8177 {
8178 init = NULL_TREE;
8179 DECL_EXTERNAL (decl) = 1;
8180 }
8181 }
8182
8183 if (VAR_P (decl)
8184 && DECL_CLASS_SCOPE_P (decl)
8185 && verify_type_context (DECL_SOURCE_LOCATION (decl),
8186 TCTX_STATIC_STORAGE, type)
8187 && DECL_INITIALIZED_IN_CLASS_P (decl))
8188 check_static_variable_definition (decl, type);
8189
8190 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
8191 {
8192 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
8193 ? TCTX_THREAD_STORAGE
8194 : TCTX_STATIC_STORAGE);
8195 verify_type_context (input_location, context, TREE_TYPE (decl));
8196 }
8197
8198 if (init && TREE_CODE (decl) == FUNCTION_DECL)
8199 {
8200 tree clone;
8201 if (init == ridpointers[(int)RID_DELETE])
8202 {
8203 /* FIXME check this is 1st decl. */
8204 DECL_DELETED_FN (decl) = 1;
8205 DECL_DECLARED_INLINE_P (decl) = 1;
8206 DECL_INITIAL (decl) = error_mark_node;
8207 FOR_EACH_CLONE (clone, decl)
8208 {
8209 DECL_DELETED_FN (clone) = 1;
8210 DECL_DECLARED_INLINE_P (clone) = 1;
8211 DECL_INITIAL (clone) = error_mark_node;
8212 }
8213 init = NULL_TREE;
8214 }
8215 else if (init == ridpointers[(int)RID_DEFAULT])
8216 {
8217 if (defaultable_fn_check (decl))
8218 DECL_DEFAULTED_FN (decl) = 1;
8219 else
8220 DECL_INITIAL (decl) = NULL_TREE;
8221 }
8222 }
8223
8224 if (init && VAR_P (decl))
8225 {
8226 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
8227 /* If DECL is a reference, then we want to know whether init is a
8228 reference constant; init_const_expr_p as passed tells us whether
8229 it's an rvalue constant. */
8230 if (TYPE_REF_P (type))
8231 init_const_expr_p = potential_constant_expression (init);
8232 if (init_const_expr_p)
8233 {
8234 /* Set these flags now for templates. We'll update the flags in
8235 store_init_value for instantiations. */
8236 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
8237 if (decl_maybe_constant_var_p (decl)
8238 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
8239 && !TYPE_REF_P (type))
8240 TREE_CONSTANT (decl) = 1;
8241 }
8242 /* This is handled mostly by gimplify.cc, but we have to deal with
8243 not warning about int x = x; as it is a GCC extension to turn off
8244 this warning but only if warn_init_self is zero. */
8245 if (!DECL_EXTERNAL (decl)
8246 && !TREE_STATIC (decl)
8247 && decl == tree_strip_any_location_wrapper (init)
8248 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
8249 suppress_warning (decl, OPT_Winit_self);
8250 }
8251
8252 if (flag_openmp
8253 && TREE_CODE (decl) == FUNCTION_DECL
8254 /* #pragma omp declare variant on methods handled in finish_struct
8255 instead. */
8256 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
8257 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
8258 if (tree attr = lookup_attribute ("omp declare variant base",
8259 DECL_ATTRIBUTES (decl)))
8260 omp_declare_variant_finalize (decl, attr);
8261
8262 if (processing_template_decl)
8263 {
8264 bool type_dependent_p;
8265
8266 /* Add this declaration to the statement-tree. */
8267 if (at_function_scope_p ())
8268 add_decl_expr (decl);
8269
8270 type_dependent_p = dependent_type_p (type);
8271
8272 if (check_for_bare_parameter_packs (init))
8273 {
8274 init = NULL_TREE;
8275 DECL_INITIAL (decl) = NULL_TREE;
8276 }
8277
8278 /* Generally, initializers in templates are expanded when the
8279 template is instantiated. But, if DECL is a variable constant
8280 then it can be used in future constant expressions, so its value
8281 must be available. */
8282
8283 bool dep_init = false;
8284
8285 if (!VAR_P (decl) || type_dependent_p)
8286 /* We can't do anything if the decl has dependent type. */;
8287 else if (!init && is_concept_var (decl))
8288 {
8289 error ("variable concept has no initializer");
8290 init = boolean_true_node;
8291 }
8292 else if (init
8293 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
8294 && !TYPE_REF_P (type)
8295 && decl_maybe_constant_var_p (decl)
8296 && !(dep_init = value_dependent_init_p (init)))
8297 {
8298 /* This variable seems to be a non-dependent constant, so process
8299 its initializer. If check_initializer returns non-null the
8300 initialization wasn't constant after all. */
8301 tree init_code;
8302 cleanups = make_tree_vector ();
8303 init_code = check_initializer (decl, init, flags, &cleanups);
8304 if (init_code == NULL_TREE)
8305 init = NULL_TREE;
8306 release_tree_vector (cleanups);
8307 }
8308 else
8309 {
8310 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
8311 /* Try to deduce array size. */
8312 maybe_deduce_size_from_array_init (decl, init);
8313 /* And complain about multiple initializers. */
8314 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
8315 && !MAYBE_CLASS_TYPE_P (type))
8316 init = build_x_compound_expr_from_list (init, ELK_INIT,
8317 tf_warning_or_error);
8318 }
8319
8320 if (init)
8321 DECL_INITIAL (decl) = init;
8322
8323 if (dep_init)
8324 {
8325 retrofit_lang_decl (decl);
8326 SET_DECL_DEPENDENT_INIT_P (decl, true);
8327 }
8328
8329 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec)
8330 {
8331 set_user_assembler_name (decl, asmspec);
8332 DECL_HARD_REGISTER (decl) = 1;
8333 }
8334 return;
8335 }
8336
8337 /* Just store non-static data member initializers for later. */
8338 if (init && TREE_CODE (decl) == FIELD_DECL)
8339 DECL_INITIAL (decl) = init;
8340
8341 /* Take care of TYPE_DECLs up front. */
8342 if (TREE_CODE (decl) == TYPE_DECL)
8343 {
8344 if (type != error_mark_node
8345 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
8346 {
8347 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8348 warning (0, "shadowing previous type declaration of %q#D", decl);
8349 set_identifier_type_value (DECL_NAME (decl), decl);
8350 }
8351
8352 /* If we have installed this as the canonical typedef for this
8353 type, and that type has not been defined yet, delay emitting
8354 the debug information for it, as we will emit it later. */
8355 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8356 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8357 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8358
8359 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
8360 at_eof);
8361 return;
8362 }
8363
8364 /* A reference will be modified here, as it is initialized. */
8365 if (! DECL_EXTERNAL (decl)
8366 && TREE_READONLY (decl)
8367 && TYPE_REF_P (type))
8368 {
8369 was_readonly = 1;
8370 TREE_READONLY (decl) = 0;
8371 }
8372
8373 /* This needs to happen before extend_ref_init_temps. */
8374 if (VAR_OR_FUNCTION_DECL_P (decl))
8375 {
8376 if (VAR_P (decl))
8377 maybe_commonize_var (decl);
8378 determine_visibility (decl);
8379 }
8380
8381 if (VAR_P (decl))
8382 {
8383 duration_kind dk = decl_storage_duration (decl);
8384 /* [dcl.constinit]/1 "The constinit specifier shall be applied
8385 only to a declaration of a variable with static or thread storage
8386 duration." */
8387 if (DECL_DECLARED_CONSTINIT_P (decl)
8388 && !(dk == dk_thread || dk == dk_static))
8389 {
8390 error_at (DECL_SOURCE_LOCATION (decl),
8391 "%<constinit%> can only be applied to a variable with "
8392 "static or thread storage duration");
8393 return;
8394 }
8395
8396 /* If this is a local variable that will need a mangled name,
8397 register it now. We must do this before processing the
8398 initializer for the variable, since the initialization might
8399 require a guard variable, and since the mangled name of the
8400 guard variable will depend on the mangled name of this
8401 variable. */
8402 if (DECL_FUNCTION_SCOPE_P (decl)
8403 && TREE_STATIC (decl)
8404 && !DECL_ARTIFICIAL (decl))
8405 {
8406 /* The variable holding an anonymous union will have had its
8407 discriminator set in finish_anon_union, after which it's
8408 NAME will have been cleared. */
8409 if (DECL_NAME (decl))
8410 determine_local_discriminator (decl);
8411 /* Normally has_forced_label_in_static is set during GIMPLE
8412 lowering, but [cd]tors are never actually compiled directly.
8413 We need to set this early so we can deal with the label
8414 address extension. */
8415 if ((DECL_CONSTRUCTOR_P (current_function_decl)
8416 || DECL_DESTRUCTOR_P (current_function_decl))
8417 && init)
8418 {
8419 walk_tree (&init, notice_forced_label_r, NULL, NULL);
8420 add_local_decl (cfun, decl);
8421 }
8422 /* And make sure it's in the symbol table for
8423 c_parse_final_cleanups to find. */
8424 varpool_node::get_create (decl);
8425 }
8426
8427 /* Convert the initializer to the type of DECL, if we have not
8428 already initialized DECL. */
8429 if (!DECL_INITIALIZED_P (decl)
8430 /* If !DECL_EXTERNAL then DECL is being defined. In the
8431 case of a static data member initialized inside the
8432 class-specifier, there can be an initializer even if DECL
8433 is *not* defined. */
8434 && (!DECL_EXTERNAL (decl) || init))
8435 {
8436 cleanups = make_tree_vector ();
8437 init = check_initializer (decl, init, flags, &cleanups);
8438
8439 /* Handle:
8440
8441 [dcl.init]
8442
8443 The memory occupied by any object of static storage
8444 duration is zero-initialized at program startup before
8445 any other initialization takes place.
8446
8447 We cannot create an appropriate initializer until after
8448 the type of DECL is finalized. If DECL_INITIAL is set,
8449 then the DECL is statically initialized, and any
8450 necessary zero-initialization has already been performed. */
8451 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8452 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8453 /*nelts=*/NULL_TREE,
8454 /*static_storage_p=*/true);
8455 /* Remember that the initialization for this variable has
8456 taken place. */
8457 DECL_INITIALIZED_P (decl) = 1;
8458 /* This declaration is the definition of this variable,
8459 unless we are initializing a static data member within
8460 the class specifier. */
8461 if (!DECL_EXTERNAL (decl))
8462 var_definition_p = true;
8463 }
8464 /* If the variable has an array type, lay out the type, even if
8465 there is no initializer. It is valid to index through the
8466 array, and we must get TYPE_ALIGN set correctly on the array
8467 type. */
8468 else if (TREE_CODE (type) == ARRAY_TYPE)
8469 layout_type (type);
8470
8471 if (TREE_STATIC (decl)
8472 && !at_function_scope_p ()
8473 && current_function_decl == NULL)
8474 /* So decl is a global variable or a static member of a
8475 non local class. Record the types it uses
8476 so that we can decide later to emit debug info for them. */
8477 record_types_used_by_current_var_decl (decl);
8478 }
8479
8480 /* Add this declaration to the statement-tree. This needs to happen
8481 after the call to check_initializer so that the DECL_EXPR for a
8482 reference temp is added before the DECL_EXPR for the reference itself. */
8483 if (DECL_FUNCTION_SCOPE_P (decl))
8484 {
8485 /* If we're building a variable sized type, and we might be
8486 reachable other than via the top of the current binding
8487 level, then create a new BIND_EXPR so that we deallocate
8488 the object at the right time. */
8489 if (VAR_P (decl)
8490 && DECL_SIZE (decl)
8491 && !TREE_CONSTANT (DECL_SIZE (decl))
8492 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
8493 {
8494 tree bind;
8495 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8496 TREE_SIDE_EFFECTS (bind) = 1;
8497 add_stmt (bind);
8498 BIND_EXPR_BODY (bind) = push_stmt_list ();
8499 }
8500 add_decl_expr (decl);
8501 }
8502
8503 /* Let the middle end know about variables and functions -- but not
8504 static data members in uninstantiated class templates. */
8505 if (VAR_OR_FUNCTION_DECL_P (decl))
8506 {
8507 if (VAR_P (decl))
8508 {
8509 layout_var_decl (decl);
8510 if (!flag_weak)
8511 /* Check again now that we have an initializer. */
8512 maybe_commonize_var (decl);
8513 /* A class-scope constexpr variable with an out-of-class declaration.
8514 C++17 makes them implicitly inline, but still force it out. */
8515 if (DECL_INLINE_VAR_P (decl)
8516 && !DECL_VAR_DECLARED_INLINE_P (decl)
8517 && !DECL_TEMPLATE_INSTANTIATION (decl)
8518 && !in_class_decl)
8519 mark_needed (decl);
8520 }
8521
8522 if (var_definition_p
8523 /* With -fmerge-all-constants, gimplify_init_constructor
8524 might add TREE_STATIC to aggregate variables. */
8525 && (TREE_STATIC (decl)
8526 || (flag_merge_constants >= 2
8527 && AGGREGATE_TYPE_P (type))))
8528 {
8529 /* If a TREE_READONLY variable needs initialization
8530 at runtime, it is no longer readonly and we need to
8531 avoid MEM_READONLY_P being set on RTL created for it. */
8532 if (init)
8533 {
8534 if (TREE_READONLY (decl))
8535 TREE_READONLY (decl) = 0;
8536 was_readonly = 0;
8537 }
8538 else if (was_readonly)
8539 TREE_READONLY (decl) = 1;
8540
8541 /* Likewise if it needs destruction. */
8542 if (!decl_maybe_constant_destruction (decl, type))
8543 TREE_READONLY (decl) = 0;
8544 }
8545 else if (VAR_P (decl)
8546 && CP_DECL_THREAD_LOCAL_P (decl)
8547 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
8548 && (was_readonly || TREE_READONLY (decl))
8549 && var_needs_tls_wrapper (decl))
8550 {
8551 /* TLS variables need dynamic initialization by the TLS wrapper
8552 function, we don't want to hoist accesses to it before the
8553 wrapper. */
8554 was_readonly = 0;
8555 TREE_READONLY (decl) = 0;
8556 }
8557
8558 make_rtl_for_nonlocal_decl (decl, init, asmspec);
8559
8560 /* Check for abstractness of the type. */
8561 if (var_definition_p)
8562 abstract_virtuals_error (decl, type);
8563
8564 if (TREE_TYPE (decl) == error_mark_node)
8565 /* No initialization required. */
8566 ;
8567 else if (TREE_CODE (decl) == FUNCTION_DECL)
8568 {
8569 if (init)
8570 {
8571 if (init == ridpointers[(int)RID_DEFAULT])
8572 {
8573 /* An out-of-class default definition is defined at
8574 the point where it is explicitly defaulted. */
8575 if (DECL_DELETED_FN (decl))
8576 maybe_explain_implicit_delete (decl);
8577 else if (DECL_INITIAL (decl) == error_mark_node)
8578 synthesize_method (decl);
8579 }
8580 else
8581 error_at (cp_expr_loc_or_loc (init,
8582 DECL_SOURCE_LOCATION (decl)),
8583 "function %q#D is initialized like a variable",
8584 decl);
8585 }
8586 /* else no initialization required. */
8587 }
8588 else if (DECL_EXTERNAL (decl)
8589 && ! (DECL_LANG_SPECIFIC (decl)
8590 && DECL_NOT_REALLY_EXTERN (decl)))
8591 {
8592 /* check_initializer will have done any constant initialization. */
8593 }
8594 /* A variable definition. */
8595 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8596 /* Initialize the local variable. */
8597 initialize_local_var (decl, init);
8598
8599 /* If a variable is defined, and then a subsequent
8600 definition with external linkage is encountered, we will
8601 get here twice for the same variable. We want to avoid
8602 calling expand_static_init more than once. For variables
8603 that are not static data members, we can call
8604 expand_static_init only when we actually process the
8605 initializer. It is not legal to redeclare a static data
8606 member, so this issue does not arise in that case. */
8607 else if (var_definition_p && TREE_STATIC (decl))
8608 expand_static_init (decl, init);
8609 }
8610
8611 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
8612 reference, insert it in the statement-tree now. */
8613 if (cleanups)
8614 {
8615 for (tree t : *cleanups)
8616 {
8617 push_cleanup (NULL_TREE, t, false);
8618 /* As in initialize_local_var. */
8619 wrap_temporary_cleanups (init, t);
8620 }
8621 release_tree_vector (cleanups);
8622 }
8623
8624 if (was_readonly)
8625 TREE_READONLY (decl) = 1;
8626
8627 if (flag_openmp
8628 && VAR_P (decl)
8629 && lookup_attribute ("omp declare target implicit",
8630 DECL_ATTRIBUTES (decl)))
8631 {
8632 DECL_ATTRIBUTES (decl)
8633 = remove_attribute ("omp declare target implicit",
8634 DECL_ATTRIBUTES (decl));
8635 complete_type (TREE_TYPE (decl));
8636 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
8637 {
8638 error ("%q+D in declare target directive does not have mappable"
8639 " type", decl);
8640 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
8641 }
8642 else if (!lookup_attribute ("omp declare target",
8643 DECL_ATTRIBUTES (decl))
8644 && !lookup_attribute ("omp declare target link",
8645 DECL_ATTRIBUTES (decl)))
8646 {
8647 DECL_ATTRIBUTES (decl)
8648 = tree_cons (get_identifier ("omp declare target"),
8649 NULL_TREE, DECL_ATTRIBUTES (decl));
8650 symtab_node *node = symtab_node::get (decl);
8651 if (node != NULL)
8652 {
8653 node->offloadable = 1;
8654 if (ENABLE_OFFLOADING)
8655 {
8656 g->have_offload = true;
8657 if (is_a <varpool_node *> (node))
8658 vec_safe_push (offload_vars, decl);
8659 }
8660 }
8661 }
8662 }
8663
8664 /* This is the last point we can lower alignment so give the target the
8665 chance to do so. */
8666 if (VAR_P (decl)
8667 && !is_global_var (decl)
8668 && !DECL_HARD_REGISTER (decl))
8669 targetm.lower_local_decl_alignment (decl);
8670
8671 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
8672 }
8673
8674 /* For class TYPE return itself or some its bases that contain
8675 any direct non-static data members. Return error_mark_node if an
8676 error has been diagnosed. */
8677
8678 static tree
find_decomp_class_base(location_t loc,tree type,tree ret)8679 find_decomp_class_base (location_t loc, tree type, tree ret)
8680 {
8681 bool member_seen = false;
8682 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8683 if (TREE_CODE (field) != FIELD_DECL
8684 || DECL_ARTIFICIAL (field)
8685 || DECL_UNNAMED_BIT_FIELD (field))
8686 continue;
8687 else if (ret)
8688 return type;
8689 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
8690 {
8691 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
8692 error_at (loc, "cannot decompose class type %qT because it has an "
8693 "anonymous struct member", type);
8694 else
8695 error_at (loc, "cannot decompose class type %qT because it has an "
8696 "anonymous union member", type);
8697 inform (DECL_SOURCE_LOCATION (field), "declared here");
8698 return error_mark_node;
8699 }
8700 else if (!accessible_p (type, field, true))
8701 {
8702 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
8703 field, type);
8704 inform (DECL_SOURCE_LOCATION (field),
8705 TREE_PRIVATE (field)
8706 ? G_("declared private here")
8707 : G_("declared protected here"));
8708 return error_mark_node;
8709 }
8710 else
8711 member_seen = true;
8712
8713 tree base_binfo, binfo;
8714 tree orig_ret = ret;
8715 int i;
8716 if (member_seen)
8717 ret = type;
8718 for (binfo = TYPE_BINFO (type), i = 0;
8719 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8720 {
8721 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
8722 if (t == error_mark_node)
8723 return error_mark_node;
8724 if (t != NULL_TREE && t != ret)
8725 {
8726 if (ret == type)
8727 {
8728 error_at (loc, "cannot decompose class type %qT: both it and "
8729 "its base class %qT have non-static data members",
8730 type, t);
8731 return error_mark_node;
8732 }
8733 else if (orig_ret != NULL_TREE)
8734 return t;
8735 else if (ret != NULL_TREE)
8736 {
8737 error_at (loc, "cannot decompose class type %qT: its base "
8738 "classes %qT and %qT have non-static data "
8739 "members", type, ret, t);
8740 return error_mark_node;
8741 }
8742 else
8743 ret = t;
8744 }
8745 }
8746 return ret;
8747 }
8748
8749 /* Return std::tuple_size<TYPE>::value. */
8750
8751 static tree
get_tuple_size(tree type)8752 get_tuple_size (tree type)
8753 {
8754 tree args = make_tree_vec (1);
8755 TREE_VEC_ELT (args, 0) = type;
8756 tree inst = lookup_template_class (tuple_size_identifier, args,
8757 /*in_decl*/NULL_TREE,
8758 /*context*/std_node,
8759 /*entering_scope*/false, tf_none);
8760 inst = complete_type (inst);
8761 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8762 return NULL_TREE;
8763 tree val = lookup_qualified_name (inst, value_identifier,
8764 LOOK_want::NORMAL, /*complain*/false);
8765 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8766 val = maybe_constant_value (val);
8767 if (TREE_CODE (val) == INTEGER_CST)
8768 return val;
8769 else
8770 return error_mark_node;
8771 }
8772
8773 /* Return std::tuple_element<I,TYPE>::type. */
8774
8775 static tree
get_tuple_element_type(tree type,unsigned i)8776 get_tuple_element_type (tree type, unsigned i)
8777 {
8778 tree args = make_tree_vec (2);
8779 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8780 TREE_VEC_ELT (args, 1) = type;
8781 tree inst = lookup_template_class (tuple_element_identifier, args,
8782 /*in_decl*/NULL_TREE,
8783 /*context*/std_node,
8784 /*entering_scope*/false,
8785 tf_warning_or_error);
8786 return make_typename_type (inst, type_identifier,
8787 none_type, tf_warning_or_error);
8788 }
8789
8790 /* Return e.get<i>() or get<i>(e). */
8791
8792 static tree
get_tuple_decomp_init(tree decl,unsigned i)8793 get_tuple_decomp_init (tree decl, unsigned i)
8794 {
8795 tree targs = make_tree_vec (1);
8796 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8797
8798 tree etype = TREE_TYPE (decl);
8799 tree e = convert_from_reference (decl);
8800
8801 /* [The id-expression] e is an lvalue if the type of the entity e is an
8802 lvalue reference and an xvalue otherwise. */
8803 if (!TYPE_REF_P (etype)
8804 || TYPE_REF_IS_RVALUE (etype))
8805 e = move (e);
8806
8807 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8808 LOOK_want::NORMAL, /*complain*/false);
8809 bool use_member_get = false;
8810
8811 /* To use a member get, member lookup must find at least one
8812 declaration that is a function template
8813 whose first template parameter is a non-type parameter. */
8814 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8815 {
8816 tree fn = *iter;
8817 if (TREE_CODE (fn) == TEMPLATE_DECL)
8818 {
8819 tree tparms = DECL_TEMPLATE_PARMS (fn);
8820 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8821 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8822 {
8823 use_member_get = true;
8824 break;
8825 }
8826 }
8827 }
8828
8829 if (use_member_get)
8830 {
8831 fns = lookup_template_function (fns, targs);
8832 return build_new_method_call (e, fns, /*args*/NULL,
8833 /*path*/NULL_TREE, LOOKUP_NORMAL,
8834 /*fn_p*/NULL, tf_warning_or_error);
8835 }
8836 else
8837 {
8838 releasing_vec args (make_tree_vector_single (e));
8839 fns = lookup_template_function (get__identifier, targs);
8840 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8841 return finish_call_expr (fns, &args, /*novirt*/false,
8842 /*koenig*/true, tf_warning_or_error);
8843 }
8844 }
8845
8846 /* It's impossible to recover the decltype of a tuple decomposition variable
8847 based on the actual type of the variable, so store it in a hash table. */
8848
8849 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8850
8851 tree
lookup_decomp_type(tree v)8852 lookup_decomp_type (tree v)
8853 {
8854 return *decomp_type_table->get (v);
8855 }
8856
8857 /* Mangle a decomposition declaration if needed. Arguments like
8858 in cp_finish_decomp. */
8859
8860 void
cp_maybe_mangle_decomp(tree decl,tree first,unsigned int count)8861 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8862 {
8863 if (!processing_template_decl
8864 && !error_operand_p (decl)
8865 && TREE_STATIC (decl))
8866 {
8867 auto_vec<tree, 16> v;
8868 v.safe_grow (count, true);
8869 tree d = first;
8870 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8871 v[count - i - 1] = d;
8872 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8873 maybe_apply_pragma_weak (decl);
8874 }
8875 }
8876
8877 /* Finish a decomposition declaration. DECL is the underlying declaration
8878 "e", FIRST is the head of a chain of decls for the individual identifiers
8879 chained through DECL_CHAIN in reverse order and COUNT is the number of
8880 those decls. */
8881
8882 void
cp_finish_decomp(tree decl,tree first,unsigned int count)8883 cp_finish_decomp (tree decl, tree first, unsigned int count)
8884 {
8885 if (error_operand_p (decl))
8886 {
8887 error_out:
8888 while (count--)
8889 {
8890 TREE_TYPE (first) = error_mark_node;
8891 if (DECL_HAS_VALUE_EXPR_P (first))
8892 {
8893 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8894 DECL_HAS_VALUE_EXPR_P (first) = 0;
8895 }
8896 first = DECL_CHAIN (first);
8897 }
8898 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8899 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8900 return;
8901 }
8902
8903 location_t loc = DECL_SOURCE_LOCATION (decl);
8904 if (type_dependent_expression_p (decl)
8905 /* This happens for range for when not in templates.
8906 Still add the DECL_VALUE_EXPRs for later processing. */
8907 || (!processing_template_decl
8908 && type_uses_auto (TREE_TYPE (decl))))
8909 {
8910 for (unsigned int i = 0; i < count; i++)
8911 {
8912 if (!DECL_HAS_VALUE_EXPR_P (first))
8913 {
8914 tree v = build_nt (ARRAY_REF, decl,
8915 size_int (count - i - 1),
8916 NULL_TREE, NULL_TREE);
8917 SET_DECL_VALUE_EXPR (first, v);
8918 DECL_HAS_VALUE_EXPR_P (first) = 1;
8919 }
8920 if (processing_template_decl)
8921 fit_decomposition_lang_decl (first, decl);
8922 first = DECL_CHAIN (first);
8923 }
8924 return;
8925 }
8926
8927 auto_vec<tree, 16> v;
8928 v.safe_grow (count, true);
8929 tree d = first;
8930 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8931 {
8932 v[count - i - 1] = d;
8933 fit_decomposition_lang_decl (d, decl);
8934 }
8935
8936 tree type = TREE_TYPE (decl);
8937 tree dexp = decl;
8938
8939 if (TYPE_REF_P (type))
8940 {
8941 dexp = convert_from_reference (dexp);
8942 type = complete_type (TREE_TYPE (type));
8943 if (type == error_mark_node)
8944 goto error_out;
8945 if (!COMPLETE_TYPE_P (type))
8946 {
8947 error_at (loc, "structured binding refers to incomplete type %qT",
8948 type);
8949 goto error_out;
8950 }
8951 }
8952
8953 tree eltype = NULL_TREE;
8954 unsigned HOST_WIDE_INT eltscnt = 0;
8955 if (TREE_CODE (type) == ARRAY_TYPE)
8956 {
8957 tree nelts;
8958 nelts = array_type_nelts_top (type);
8959 if (nelts == error_mark_node)
8960 goto error_out;
8961 if (!tree_fits_uhwi_p (nelts))
8962 {
8963 error_at (loc, "cannot decompose variable length array %qT", type);
8964 goto error_out;
8965 }
8966 eltscnt = tree_to_uhwi (nelts);
8967 if (count != eltscnt)
8968 {
8969 cnt_mismatch:
8970 if (count > eltscnt)
8971 error_n (loc, count,
8972 "%u name provided for structured binding",
8973 "%u names provided for structured binding", count);
8974 else
8975 error_n (loc, count,
8976 "only %u name provided for structured binding",
8977 "only %u names provided for structured binding", count);
8978 inform_n (loc, eltscnt,
8979 "while %qT decomposes into %wu element",
8980 "while %qT decomposes into %wu elements",
8981 type, eltscnt);
8982 goto error_out;
8983 }
8984 eltype = TREE_TYPE (type);
8985 for (unsigned int i = 0; i < count; i++)
8986 {
8987 TREE_TYPE (v[i]) = eltype;
8988 layout_decl (v[i], 0);
8989 if (processing_template_decl)
8990 continue;
8991 tree t = unshare_expr (dexp);
8992 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8993 eltype, t, size_int (i), NULL_TREE,
8994 NULL_TREE);
8995 SET_DECL_VALUE_EXPR (v[i], t);
8996 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8997 }
8998 }
8999 /* 2 GNU extensions. */
9000 else if (TREE_CODE (type) == COMPLEX_TYPE)
9001 {
9002 eltscnt = 2;
9003 if (count != eltscnt)
9004 goto cnt_mismatch;
9005 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9006 for (unsigned int i = 0; i < count; i++)
9007 {
9008 TREE_TYPE (v[i]) = eltype;
9009 layout_decl (v[i], 0);
9010 if (processing_template_decl)
9011 continue;
9012 tree t = unshare_expr (dexp);
9013 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
9014 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
9015 t);
9016 SET_DECL_VALUE_EXPR (v[i], t);
9017 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9018 }
9019 }
9020 else if (TREE_CODE (type) == VECTOR_TYPE)
9021 {
9022 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
9023 {
9024 error_at (loc, "cannot decompose variable length vector %qT", type);
9025 goto error_out;
9026 }
9027 if (count != eltscnt)
9028 goto cnt_mismatch;
9029 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9030 for (unsigned int i = 0; i < count; i++)
9031 {
9032 TREE_TYPE (v[i]) = eltype;
9033 layout_decl (v[i], 0);
9034 if (processing_template_decl)
9035 continue;
9036 tree t = unshare_expr (dexp);
9037 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
9038 &t, size_int (i));
9039 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
9040 eltype, t, size_int (i), NULL_TREE,
9041 NULL_TREE);
9042 SET_DECL_VALUE_EXPR (v[i], t);
9043 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9044 }
9045 }
9046 else if (tree tsize = get_tuple_size (type))
9047 {
9048 if (tsize == error_mark_node)
9049 {
9050 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
9051 "constant expression", type);
9052 goto error_out;
9053 }
9054 if (!tree_fits_uhwi_p (tsize))
9055 {
9056 error_n (loc, count,
9057 "%u name provided for structured binding",
9058 "%u names provided for structured binding", count);
9059 inform (loc, "while %qT decomposes into %E elements",
9060 type, tsize);
9061 goto error_out;
9062 }
9063 eltscnt = tree_to_uhwi (tsize);
9064 if (count != eltscnt)
9065 goto cnt_mismatch;
9066 int save_read = DECL_READ_P (decl);
9067 for (unsigned i = 0; i < count; ++i)
9068 {
9069 location_t sloc = input_location;
9070 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
9071
9072 input_location = dloc;
9073 tree init = get_tuple_decomp_init (decl, i);
9074 tree eltype = (init == error_mark_node ? error_mark_node
9075 : get_tuple_element_type (type, i));
9076 input_location = sloc;
9077
9078 if (VOID_TYPE_P (eltype))
9079 {
9080 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>",
9081 i, type);
9082 eltype = error_mark_node;
9083 }
9084 if (init == error_mark_node || eltype == error_mark_node)
9085 {
9086 inform (dloc, "in initialization of structured binding "
9087 "variable %qD", v[i]);
9088 goto error_out;
9089 }
9090 /* Save the decltype away before reference collapse. */
9091 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
9092 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
9093 TREE_TYPE (v[i]) = eltype;
9094 layout_decl (v[i], 0);
9095 if (DECL_HAS_VALUE_EXPR_P (v[i]))
9096 {
9097 /* In this case the names are variables, not just proxies. */
9098 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
9099 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
9100 }
9101 if (!processing_template_decl)
9102 {
9103 copy_linkage (v[i], decl);
9104 cp_finish_decl (v[i], init, /*constexpr*/false,
9105 /*asm*/NULL_TREE, LOOKUP_NORMAL);
9106 }
9107 }
9108 /* Ignore reads from the underlying decl performed during initialization
9109 of the individual variables. If those will be read, we'll mark
9110 the underlying decl as read at that point. */
9111 DECL_READ_P (decl) = save_read;
9112 }
9113 else if (TREE_CODE (type) == UNION_TYPE)
9114 {
9115 error_at (loc, "cannot decompose union type %qT", type);
9116 goto error_out;
9117 }
9118 else if (!CLASS_TYPE_P (type))
9119 {
9120 error_at (loc, "cannot decompose non-array non-class type %qT", type);
9121 goto error_out;
9122 }
9123 else if (LAMBDA_TYPE_P (type))
9124 {
9125 error_at (loc, "cannot decompose lambda closure type %qT", type);
9126 goto error_out;
9127 }
9128 else if (processing_template_decl && complete_type (type) == error_mark_node)
9129 goto error_out;
9130 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
9131 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
9132 type);
9133 else
9134 {
9135 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
9136 if (btype == error_mark_node)
9137 goto error_out;
9138 else if (btype == NULL_TREE)
9139 {
9140 error_at (loc, "cannot decompose class type %qT without non-static "
9141 "data members", type);
9142 goto error_out;
9143 }
9144 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9145 if (TREE_CODE (field) != FIELD_DECL
9146 || DECL_ARTIFICIAL (field)
9147 || DECL_UNNAMED_BIT_FIELD (field))
9148 continue;
9149 else
9150 eltscnt++;
9151 if (count != eltscnt)
9152 goto cnt_mismatch;
9153 tree t = dexp;
9154 if (type != btype)
9155 {
9156 t = convert_to_base (t, btype, /*check_access*/true,
9157 /*nonnull*/false, tf_warning_or_error);
9158 type = btype;
9159 }
9160 unsigned int i = 0;
9161 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
9162 if (TREE_CODE (field) != FIELD_DECL
9163 || DECL_ARTIFICIAL (field)
9164 || DECL_UNNAMED_BIT_FIELD (field))
9165 continue;
9166 else
9167 {
9168 tree tt = finish_non_static_data_member (field, unshare_expr (t),
9169 NULL_TREE);
9170 if (REFERENCE_REF_P (tt))
9171 tt = TREE_OPERAND (tt, 0);
9172 TREE_TYPE (v[i]) = TREE_TYPE (tt);
9173 layout_decl (v[i], 0);
9174 if (!processing_template_decl)
9175 {
9176 SET_DECL_VALUE_EXPR (v[i], tt);
9177 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9178 }
9179 i++;
9180 }
9181 }
9182 if (processing_template_decl)
9183 {
9184 for (unsigned int i = 0; i < count; i++)
9185 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
9186 {
9187 tree a = build_nt (ARRAY_REF, decl, size_int (i),
9188 NULL_TREE, NULL_TREE);
9189 SET_DECL_VALUE_EXPR (v[i], a);
9190 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
9191 }
9192 }
9193 }
9194
9195 /* Returns a declaration for a VAR_DECL as if:
9196
9197 extern "C" TYPE NAME;
9198
9199 had been seen. Used to create compiler-generated global
9200 variables. */
9201
9202 static tree
declare_global_var(tree name,tree type)9203 declare_global_var (tree name, tree type)
9204 {
9205 auto cookie = push_abi_namespace (global_namespace);
9206 tree decl = build_decl (input_location, VAR_DECL, name, type);
9207 TREE_PUBLIC (decl) = 1;
9208 DECL_EXTERNAL (decl) = 1;
9209 DECL_ARTIFICIAL (decl) = 1;
9210 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9211 /* If the user has explicitly declared this variable (perhaps
9212 because the code we are compiling is part of a low-level runtime
9213 library), then it is possible that our declaration will be merged
9214 with theirs by pushdecl. */
9215 decl = pushdecl (decl);
9216 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
9217 pop_abi_namespace (cookie, global_namespace);
9218
9219 return decl;
9220 }
9221
9222 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
9223 if "__cxa_atexit" is not being used) corresponding to the function
9224 to be called when the program exits. */
9225
9226 static tree
get_atexit_fn_ptr_type(void)9227 get_atexit_fn_ptr_type (void)
9228 {
9229 tree fn_type;
9230
9231 if (!atexit_fn_ptr_type_node)
9232 {
9233 tree arg_type;
9234 if (flag_use_cxa_atexit
9235 && !targetm.cxx.use_atexit_for_cxa_atexit ())
9236 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
9237 arg_type = ptr_type_node;
9238 else
9239 /* The parameter to "atexit" is "void (*)(void)". */
9240 arg_type = NULL_TREE;
9241
9242 fn_type = build_function_type_list (void_type_node,
9243 arg_type, NULL_TREE);
9244 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
9245 }
9246
9247 return atexit_fn_ptr_type_node;
9248 }
9249
9250 /* Returns a pointer to the `atexit' function. Note that if
9251 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9252 `__cxa_atexit' function specified in the IA64 C++ ABI. */
9253
9254 static tree
get_atexit_node(void)9255 get_atexit_node (void)
9256 {
9257 tree atexit_fndecl;
9258 tree fn_type;
9259 tree fn_ptr_type;
9260 const char *name;
9261 bool use_aeabi_atexit;
9262 tree ctx = global_namespace;
9263
9264 if (atexit_node)
9265 return atexit_node;
9266
9267 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9268 {
9269 /* The declaration for `__cxa_atexit' is:
9270
9271 int __cxa_atexit (void (*)(void *), void *, void *)
9272
9273 We build up the argument types and then the function type
9274 itself. */
9275 tree argtype0, argtype1, argtype2;
9276
9277 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
9278 /* First, build the pointer-to-function type for the first
9279 argument. */
9280 fn_ptr_type = get_atexit_fn_ptr_type ();
9281 /* Then, build the rest of the argument types. */
9282 argtype2 = ptr_type_node;
9283 if (use_aeabi_atexit)
9284 {
9285 argtype1 = fn_ptr_type;
9286 argtype0 = ptr_type_node;
9287 }
9288 else
9289 {
9290 argtype1 = ptr_type_node;
9291 argtype0 = fn_ptr_type;
9292 }
9293 /* And the final __cxa_atexit type. */
9294 fn_type = build_function_type_list (integer_type_node,
9295 argtype0, argtype1, argtype2,
9296 NULL_TREE);
9297 /* ... which needs noexcept. */
9298 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9299 if (use_aeabi_atexit)
9300 {
9301 name = "__aeabi_atexit";
9302 push_to_top_level ();
9303 int n = push_namespace (get_identifier ("__aeabiv1"), false);
9304 ctx = current_namespace;
9305 while (n--)
9306 pop_namespace ();
9307 pop_from_top_level ();
9308 }
9309 else
9310 {
9311 name = "__cxa_atexit";
9312 ctx = abi_node;
9313 }
9314 }
9315 else
9316 {
9317 /* The declaration for `atexit' is:
9318
9319 int atexit (void (*)());
9320
9321 We build up the argument types and then the function type
9322 itself. */
9323 fn_ptr_type = get_atexit_fn_ptr_type ();
9324 /* Build the final atexit type. */
9325 fn_type = build_function_type_list (integer_type_node,
9326 fn_ptr_type, NULL_TREE);
9327 /* ... which needs noexcept. */
9328 fn_type = build_exception_variant (fn_type, noexcept_true_spec);
9329 name = "atexit";
9330 }
9331
9332 /* Now, build the function declaration. */
9333 push_lang_context (lang_name_c);
9334 auto cookie = push_abi_namespace (ctx);
9335 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
9336 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace);
9337 /* Install as hidden builtin so we're (a) more relaxed about
9338 exception spec matching and (b) will not give a confusing location
9339 in diagnostic and (c) won't magically appear in user-visible name
9340 lookups. */
9341 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION;
9342 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true);
9343 pop_abi_namespace (cookie, ctx);
9344 mark_used (atexit_fndecl);
9345 pop_lang_context ();
9346 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
9347
9348 return atexit_node;
9349 }
9350
9351 /* Like get_atexit_node, but for thread-local cleanups. */
9352
9353 static tree
get_thread_atexit_node(void)9354 get_thread_atexit_node (void)
9355 {
9356 /* The declaration for `__cxa_thread_atexit' is:
9357
9358 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
9359 tree fn_type = build_function_type_list (integer_type_node,
9360 get_atexit_fn_ptr_type (),
9361 ptr_type_node, ptr_type_node,
9362 NULL_TREE);
9363
9364 /* Now, build the function declaration. */
9365 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
9366 ECF_LEAF | ECF_NOTHROW);
9367 return decay_conversion (atexit_fndecl, tf_warning_or_error);
9368 }
9369
9370 /* Returns the __dso_handle VAR_DECL. */
9371
9372 static tree
get_dso_handle_node(void)9373 get_dso_handle_node (void)
9374 {
9375 if (dso_handle_node)
9376 return dso_handle_node;
9377
9378 /* Declare the variable. */
9379 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9380 ptr_type_node);
9381
9382 #ifdef HAVE_GAS_HIDDEN
9383 if (dso_handle_node != error_mark_node)
9384 {
9385 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
9386 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
9387 }
9388 #endif
9389
9390 return dso_handle_node;
9391 }
9392
9393 /* Begin a new function with internal linkage whose job will be simply
9394 to destroy some particular variable. */
9395
9396 static GTY(()) int start_cleanup_cnt;
9397
9398 static tree
start_cleanup_fn(void)9399 start_cleanup_fn (void)
9400 {
9401 char name[32];
9402
9403 push_to_top_level ();
9404
9405 /* No need to mangle this. */
9406 push_lang_context (lang_name_c);
9407
9408 /* Build the name of the function. */
9409 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
9410 /* Build the function declaration. */
9411 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
9412 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9413 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace);
9414 /* It's a function with internal linkage, generated by the
9415 compiler. */
9416 TREE_PUBLIC (fndecl) = 0;
9417 DECL_ARTIFICIAL (fndecl) = 1;
9418 /* Make the function `inline' so that it is only emitted if it is
9419 actually needed. It is unlikely that it will be inlined, since
9420 it is only called via a function pointer, but we avoid unnecessary
9421 emissions this way. */
9422 DECL_DECLARED_INLINE_P (fndecl) = 1;
9423 DECL_INTERFACE_KNOWN (fndecl) = 1;
9424 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
9425 {
9426 /* Build the parameter. */
9427 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
9428 TREE_USED (parmdecl) = 1;
9429 DECL_READ_P (parmdecl) = 1;
9430 DECL_ARGUMENTS (fndecl) = parmdecl;
9431 }
9432
9433 fndecl = pushdecl (fndecl, /*hidden=*/true);
9434 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
9435
9436 pop_lang_context ();
9437
9438 return current_function_decl;
9439 }
9440
9441 /* Finish the cleanup function begun by start_cleanup_fn. */
9442
9443 static void
end_cleanup_fn(void)9444 end_cleanup_fn (void)
9445 {
9446 expand_or_defer_fn (finish_function (/*inline_p=*/false));
9447
9448 pop_from_top_level ();
9449 }
9450
9451 /* Generate code to handle the destruction of DECL, an object with
9452 static storage duration. */
9453
9454 tree
register_dtor_fn(tree decl)9455 register_dtor_fn (tree decl)
9456 {
9457 tree cleanup;
9458 tree addr;
9459 tree compound_stmt;
9460 tree fcall;
9461 tree type;
9462 bool ob_parm, dso_parm, use_dtor;
9463 tree arg0, arg1, arg2;
9464 tree atex_node;
9465
9466 type = TREE_TYPE (decl);
9467 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
9468 return void_node;
9469
9470 if (decl_maybe_constant_destruction (decl, type)
9471 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
9472 {
9473 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9474 return void_node;
9475 }
9476
9477 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
9478 "__aeabi_atexit"), and DECL is a class object, we can just pass the
9479 destructor to "__cxa_atexit"; we don't have to build a temporary
9480 function to do the cleanup. */
9481 dso_parm = (flag_use_cxa_atexit
9482 && !targetm.cxx.use_atexit_for_cxa_atexit ());
9483 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
9484 use_dtor = ob_parm && CLASS_TYPE_P (type);
9485 if (use_dtor)
9486 {
9487 cleanup = get_class_binding (type, complete_dtor_identifier);
9488
9489 /* Make sure it is accessible. */
9490 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
9491 tf_warning_or_error);
9492 }
9493 else
9494 {
9495 /* Call build_cleanup before we enter the anonymous function so
9496 that any access checks will be done relative to the current
9497 scope, rather than the scope of the anonymous function. */
9498 build_cleanup (decl);
9499
9500 /* Now start the function. */
9501 cleanup = start_cleanup_fn ();
9502
9503 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
9504 to the original function, rather than the anonymous one. That
9505 will make the back end think that nested functions are in use,
9506 which causes confusion. */
9507 push_deferring_access_checks (dk_no_check);
9508 fcall = build_cleanup (decl);
9509 pop_deferring_access_checks ();
9510
9511 /* Create the body of the anonymous function. */
9512 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
9513 finish_expr_stmt (fcall);
9514 finish_compound_stmt (compound_stmt);
9515 end_cleanup_fn ();
9516 }
9517
9518 /* Call atexit with the cleanup function. */
9519 mark_used (cleanup);
9520 cleanup = build_address (cleanup);
9521
9522 if (CP_DECL_THREAD_LOCAL_P (decl))
9523 atex_node = get_thread_atexit_node ();
9524 else
9525 atex_node = get_atexit_node ();
9526
9527 if (use_dtor)
9528 {
9529 /* We must convert CLEANUP to the type that "__cxa_atexit"
9530 expects. */
9531 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
9532 /* "__cxa_atexit" will pass the address of DECL to the
9533 cleanup function. */
9534 mark_used (decl);
9535 addr = build_address (decl);
9536 /* The declared type of the parameter to "__cxa_atexit" is
9537 "void *". For plain "T*", we could just let the
9538 machinery in cp_build_function_call convert it -- but if the
9539 type is "cv-qualified T *", then we need to convert it
9540 before passing it in, to avoid spurious errors. */
9541 addr = build_nop (ptr_type_node, addr);
9542 }
9543 else
9544 /* Since the cleanup functions we build ignore the address
9545 they're given, there's no reason to pass the actual address
9546 in, and, in general, it's cheaper to pass NULL than any
9547 other value. */
9548 addr = null_pointer_node;
9549
9550 if (dso_parm)
9551 arg2 = cp_build_addr_expr (get_dso_handle_node (),
9552 tf_warning_or_error);
9553 else if (ob_parm)
9554 /* Just pass NULL to the dso handle parm if we don't actually
9555 have a DSO handle on this target. */
9556 arg2 = null_pointer_node;
9557 else
9558 arg2 = NULL_TREE;
9559
9560 if (ob_parm)
9561 {
9562 if (!CP_DECL_THREAD_LOCAL_P (decl)
9563 && targetm.cxx.use_aeabi_atexit ())
9564 {
9565 arg1 = cleanup;
9566 arg0 = addr;
9567 }
9568 else
9569 {
9570 arg1 = addr;
9571 arg0 = cleanup;
9572 }
9573 }
9574 else
9575 {
9576 arg0 = cleanup;
9577 arg1 = NULL_TREE;
9578 }
9579 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
9580 arg0, arg1, arg2, NULL_TREE);
9581 }
9582
9583 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
9584 is its initializer. Generate code to handle the construction
9585 and destruction of DECL. */
9586
9587 static void
expand_static_init(tree decl,tree init)9588 expand_static_init (tree decl, tree init)
9589 {
9590 gcc_assert (VAR_P (decl));
9591 gcc_assert (TREE_STATIC (decl));
9592
9593 /* Some variables require no dynamic initialization. */
9594 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
9595 {
9596 /* Make sure the destructor is callable. */
9597 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
9598 if (!init)
9599 return;
9600 }
9601
9602 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
9603 && !DECL_FUNCTION_SCOPE_P (decl))
9604 {
9605 location_t dloc = DECL_SOURCE_LOCATION (decl);
9606 if (init)
9607 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9608 "needs dynamic initialization", decl);
9609 else
9610 error_at (dloc, "non-local variable %qD declared %<__thread%> "
9611 "has a non-trivial destructor", decl);
9612 static bool informed;
9613 if (!informed)
9614 {
9615 inform (dloc, "C++11 %<thread_local%> allows dynamic "
9616 "initialization and destruction");
9617 informed = true;
9618 }
9619 return;
9620 }
9621
9622 if (DECL_FUNCTION_SCOPE_P (decl))
9623 {
9624 /* Emit code to perform this initialization but once. */
9625 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
9626 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
9627 tree guard, guard_addr;
9628 tree flag, begin;
9629 /* We don't need thread-safety code for thread-local vars. */
9630 bool thread_guard = (flag_threadsafe_statics
9631 && !CP_DECL_THREAD_LOCAL_P (decl));
9632
9633 /* Emit code to perform this initialization but once. This code
9634 looks like:
9635
9636 static <type> guard;
9637 if (!__atomic_load (guard.first_byte)) {
9638 if (__cxa_guard_acquire (&guard)) {
9639 bool flag = false;
9640 try {
9641 // Do initialization.
9642 flag = true; __cxa_guard_release (&guard);
9643 // Register variable for destruction at end of program.
9644 } catch {
9645 if (!flag) __cxa_guard_abort (&guard);
9646 }
9647 }
9648 }
9649
9650 Note that the `flag' variable is only set to 1 *after* the
9651 initialization is complete. This ensures that an exception,
9652 thrown during the construction, will cause the variable to
9653 reinitialized when we pass through this code again, as per:
9654
9655 [stmt.dcl]
9656
9657 If the initialization exits by throwing an exception, the
9658 initialization is not complete, so it will be tried again
9659 the next time control enters the declaration.
9660
9661 This process should be thread-safe, too; multiple threads
9662 should not be able to initialize the variable more than
9663 once. */
9664
9665 /* Create the guard variable. */
9666 guard = get_guard (decl);
9667
9668 /* Begin the conditional initialization. */
9669 if_stmt = begin_if_stmt ();
9670
9671 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
9672 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9673
9674 if (thread_guard)
9675 {
9676 tree vfntype = NULL_TREE;
9677 tree acquire_name, release_name, abort_name;
9678 tree acquire_fn, release_fn, abort_fn;
9679 guard_addr = build_address (guard);
9680
9681 acquire_name = get_identifier ("__cxa_guard_acquire");
9682 release_name = get_identifier ("__cxa_guard_release");
9683 abort_name = get_identifier ("__cxa_guard_abort");
9684 acquire_fn = get_global_binding (acquire_name);
9685 release_fn = get_global_binding (release_name);
9686 abort_fn = get_global_binding (abort_name);
9687 if (!acquire_fn)
9688 acquire_fn = push_library_fn
9689 (acquire_name, build_function_type_list (integer_type_node,
9690 TREE_TYPE (guard_addr),
9691 NULL_TREE),
9692 NULL_TREE, ECF_NOTHROW);
9693 if (!release_fn || !abort_fn)
9694 vfntype = build_function_type_list (void_type_node,
9695 TREE_TYPE (guard_addr),
9696 NULL_TREE);
9697 if (!release_fn)
9698 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
9699 ECF_NOTHROW);
9700 if (!abort_fn)
9701 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
9702 ECF_NOTHROW | ECF_LEAF);
9703
9704 inner_if_stmt = begin_if_stmt ();
9705 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
9706 inner_if_stmt);
9707
9708 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
9709 begin = get_target_expr (boolean_false_node);
9710 flag = TARGET_EXPR_SLOT (begin);
9711
9712 TARGET_EXPR_CLEANUP (begin)
9713 = build3 (COND_EXPR, void_type_node, flag,
9714 void_node,
9715 build_call_n (abort_fn, 1, guard_addr));
9716 CLEANUP_EH_ONLY (begin) = 1;
9717
9718 /* Do the initialization itself. */
9719 init = add_stmt_to_compound (begin, init);
9720 init = add_stmt_to_compound (init,
9721 build2 (MODIFY_EXPR, void_type_node,
9722 flag, boolean_true_node));
9723
9724 /* Use atexit to register a function for destroying this static
9725 variable. Do this before calling __cxa_guard_release. */
9726 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9727
9728 init = add_stmt_to_compound (init, build_call_n (release_fn, 1,
9729 guard_addr));
9730 }
9731 else
9732 {
9733 init = add_stmt_to_compound (init, set_guard (guard));
9734
9735 /* Use atexit to register a function for destroying this static
9736 variable. */
9737 init = add_stmt_to_compound (init, register_dtor_fn (decl));
9738 }
9739
9740 finish_expr_stmt (init);
9741
9742 if (thread_guard)
9743 {
9744 finish_compound_stmt (inner_then_clause);
9745 finish_then_clause (inner_if_stmt);
9746 finish_if_stmt (inner_if_stmt);
9747 }
9748
9749 finish_compound_stmt (then_clause);
9750 finish_then_clause (if_stmt);
9751 finish_if_stmt (if_stmt);
9752 }
9753 else if (CP_DECL_THREAD_LOCAL_P (decl))
9754 tls_aggregates = tree_cons (init, decl, tls_aggregates);
9755 else
9756 static_aggregates = tree_cons (init, decl, static_aggregates);
9757 }
9758
9759
9760 /* Make TYPE a complete type based on INITIAL_VALUE.
9761 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9762 2 if there was no information (in which case assume 0 if DO_DEFAULT),
9763 3 if the initializer list is empty (in pedantic mode). */
9764
9765 int
cp_complete_array_type(tree * ptype,tree initial_value,bool do_default)9766 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
9767 {
9768 int failure;
9769 tree type, elt_type;
9770
9771 /* Don't get confused by a CONSTRUCTOR for some other type. */
9772 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
9773 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
9774 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
9775 return 1;
9776
9777 if (initial_value)
9778 {
9779 /* An array of character type can be initialized from a
9780 brace-enclosed string constant so call reshape_init to
9781 remove the optional braces from a braced string literal. */
9782 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9783 && BRACE_ENCLOSED_INITIALIZER_P (initial_value))
9784 initial_value = reshape_init (*ptype, initial_value,
9785 tf_warning_or_error);
9786
9787 /* If any of the elements are parameter packs, we can't actually
9788 complete this type now because the array size is dependent. */
9789 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9790 for (auto &e: CONSTRUCTOR_ELTS (initial_value))
9791 if (PACK_EXPANSION_P (e.value))
9792 return 0;
9793 }
9794
9795 failure = complete_array_type (ptype, initial_value, do_default);
9796
9797 /* We can create the array before the element type is complete, which
9798 means that we didn't have these two bits set in the original type
9799 either. In completing the type, we are expected to propagate these
9800 bits. See also complete_type which does the same thing for arrays
9801 of fixed size. */
9802 type = *ptype;
9803 if (type != error_mark_node && TYPE_DOMAIN (type))
9804 {
9805 elt_type = TREE_TYPE (type);
9806 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9807 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9808 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9809 }
9810
9811 return failure;
9812 }
9813
9814 /* As above, but either give an error or reject zero-size arrays, depending
9815 on COMPLAIN. */
9816
9817 int
cp_complete_array_type_or_error(tree * ptype,tree initial_value,bool do_default,tsubst_flags_t complain)9818 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9819 bool do_default, tsubst_flags_t complain)
9820 {
9821 int failure;
9822 bool sfinae = !(complain & tf_error);
9823 /* In SFINAE context we can't be lenient about zero-size arrays. */
9824 if (sfinae)
9825 ++pedantic;
9826 failure = cp_complete_array_type (ptype, initial_value, do_default);
9827 if (sfinae)
9828 --pedantic;
9829 if (failure)
9830 {
9831 if (sfinae)
9832 /* Not an error. */;
9833 else if (failure == 1)
9834 error ("initializer fails to determine size of %qT", *ptype);
9835 else if (failure == 2)
9836 {
9837 if (do_default)
9838 error ("array size missing in %qT", *ptype);
9839 }
9840 else if (failure == 3)
9841 error ("zero-size array %qT", *ptype);
9842 *ptype = error_mark_node;
9843 }
9844 return failure;
9845 }
9846
9847 /* Return zero if something is declared to be a member of type
9848 CTYPE when in the context of CUR_TYPE. STRING is the error
9849 message to print in that case. Otherwise, quietly return 1. */
9850
9851 static int
member_function_or_else(tree ctype,tree cur_type,enum overload_flags flags)9852 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9853 {
9854 if (ctype && ctype != cur_type)
9855 {
9856 if (flags == DTOR_FLAG)
9857 error ("destructor for alien class %qT cannot be a member", ctype);
9858 else
9859 error ("constructor for alien class %qT cannot be a member", ctype);
9860 return 0;
9861 }
9862 return 1;
9863 }
9864
9865 /* Subroutine of `grokdeclarator'. */
9866
9867 /* Generate errors possibly applicable for a given set of specifiers.
9868 This is for ARM $7.1.2. */
9869
9870 static void
bad_specifiers(tree object,enum bad_spec_place type,int virtualp,int quals,int inlinep,int friendp,int raises,const location_t * locations)9871 bad_specifiers (tree object,
9872 enum bad_spec_place type,
9873 int virtualp,
9874 int quals,
9875 int inlinep,
9876 int friendp,
9877 int raises,
9878 const location_t* locations)
9879 {
9880 switch (type)
9881 {
9882 case BSP_VAR:
9883 if (virtualp)
9884 error_at (locations[ds_virtual],
9885 "%qD declared as a %<virtual%> variable", object);
9886 if (quals)
9887 error ("%<const%> and %<volatile%> function specifiers on "
9888 "%qD invalid in variable declaration", object);
9889 break;
9890 case BSP_PARM:
9891 if (virtualp)
9892 error_at (locations[ds_virtual],
9893 "%qD declared as a %<virtual%> parameter", object);
9894 if (inlinep)
9895 error_at (locations[ds_inline],
9896 "%qD declared as an %<inline%> parameter", object);
9897 if (quals)
9898 error ("%<const%> and %<volatile%> function specifiers on "
9899 "%qD invalid in parameter declaration", object);
9900 break;
9901 case BSP_TYPE:
9902 if (virtualp)
9903 error_at (locations[ds_virtual],
9904 "%qD declared as a %<virtual%> type", object);
9905 if (inlinep)
9906 error_at (locations[ds_inline],
9907 "%qD declared as an %<inline%> type", object);
9908 if (quals)
9909 error ("%<const%> and %<volatile%> function specifiers on "
9910 "%qD invalid in type declaration", object);
9911 break;
9912 case BSP_FIELD:
9913 if (virtualp)
9914 error_at (locations[ds_virtual],
9915 "%qD declared as a %<virtual%> field", object);
9916 if (inlinep)
9917 error_at (locations[ds_inline],
9918 "%qD declared as an %<inline%> field", object);
9919 if (quals)
9920 error ("%<const%> and %<volatile%> function specifiers on "
9921 "%qD invalid in field declaration", object);
9922 break;
9923 default:
9924 gcc_unreachable();
9925 }
9926 if (friendp)
9927 error ("%q+D declared as a friend", object);
9928 if (raises
9929 && !flag_noexcept_type
9930 && (TREE_CODE (object) == TYPE_DECL
9931 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9932 && !TYPE_REFFN_P (TREE_TYPE (object))
9933 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9934 error ("%q+D declared with an exception specification", object);
9935 }
9936
9937 /* DECL is a member function or static data member and is presently
9938 being defined. Check that the definition is taking place in a
9939 valid namespace. */
9940
9941 static void
check_class_member_definition_namespace(tree decl)9942 check_class_member_definition_namespace (tree decl)
9943 {
9944 /* These checks only apply to member functions and static data
9945 members. */
9946 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9947 /* We check for problems with specializations in pt.cc in
9948 check_specialization_namespace, where we can issue better
9949 diagnostics. */
9950 if (processing_specialization)
9951 return;
9952 /* We check this in check_explicit_instantiation_namespace. */
9953 if (processing_explicit_instantiation)
9954 return;
9955 /* [class.mfct]
9956
9957 A member function definition that appears outside of the
9958 class definition shall appear in a namespace scope enclosing
9959 the class definition.
9960
9961 [class.static.data]
9962
9963 The definition for a static data member shall appear in a
9964 namespace scope enclosing the member's class definition. */
9965 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9966 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9967 decl, DECL_CONTEXT (decl));
9968 }
9969
9970 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9971 METHOD_TYPE for a non-static member function; QUALS are the
9972 cv-qualifiers that apply to the function. */
9973
9974 tree
build_this_parm(tree fn,tree type,cp_cv_quals quals)9975 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9976 {
9977 tree this_type;
9978 tree qual_type;
9979 tree parm;
9980 cp_cv_quals this_quals;
9981
9982 if (CLASS_TYPE_P (type))
9983 {
9984 this_type
9985 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9986 this_type = build_pointer_type (this_type);
9987 }
9988 else
9989 this_type = type_of_this_parm (type);
9990 /* The `this' parameter is implicitly `const'; it cannot be
9991 assigned to. */
9992 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9993 qual_type = cp_build_qualified_type (this_type, this_quals);
9994 parm = build_artificial_parm (fn, this_identifier, qual_type);
9995 cp_apply_type_quals_to_decl (this_quals, parm);
9996 return parm;
9997 }
9998
9999 /* DECL is a static member function. Complain if it was declared
10000 with function-cv-quals. */
10001
10002 static void
check_static_quals(tree decl,cp_cv_quals quals)10003 check_static_quals (tree decl, cp_cv_quals quals)
10004 {
10005 if (quals != TYPE_UNQUALIFIED)
10006 error ("static member function %q#D declared with type qualifiers",
10007 decl);
10008 }
10009
10010 // Check that FN takes no arguments and returns bool.
10011 static void
check_concept_fn(tree fn)10012 check_concept_fn (tree fn)
10013 {
10014 // A constraint is nullary.
10015 if (DECL_ARGUMENTS (fn))
10016 error_at (DECL_SOURCE_LOCATION (fn),
10017 "concept %q#D declared with function parameters", fn);
10018
10019 // The declared return type of the concept shall be bool, and
10020 // it shall not be deduced from it definition.
10021 tree type = TREE_TYPE (TREE_TYPE (fn));
10022 if (is_auto (type))
10023 error_at (DECL_SOURCE_LOCATION (fn),
10024 "concept %q#D declared with a deduced return type", fn);
10025 else if (type != boolean_type_node)
10026 error_at (DECL_SOURCE_LOCATION (fn),
10027 "concept %q#D with non-%<bool%> return type %qT", fn, type);
10028 }
10029
10030 /* Helper function. Replace the temporary this parameter injected
10031 during cp_finish_omp_declare_simd with the real this parameter. */
10032
10033 static tree
declare_simd_adjust_this(tree * tp,int * walk_subtrees,void * data)10034 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
10035 {
10036 tree this_parm = (tree) data;
10037 if (TREE_CODE (*tp) == PARM_DECL
10038 && DECL_NAME (*tp) == this_identifier
10039 && *tp != this_parm)
10040 *tp = this_parm;
10041 else if (TYPE_P (*tp))
10042 *walk_subtrees = 0;
10043 return NULL_TREE;
10044 }
10045
10046 /* CTYPE is class type, or null if non-class.
10047 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
10048 or METHOD_TYPE.
10049 DECLARATOR is the function's name.
10050 PARMS is a chain of PARM_DECLs for the function.
10051 VIRTUALP is truthvalue of whether the function is virtual or not.
10052 FLAGS are to be passed through to `grokclassfn'.
10053 QUALS are qualifiers indicating whether the function is `const'
10054 or `volatile'.
10055 RAISES is a list of exceptions that this function can raise.
10056 CHECK is 1 if we must find this method in CTYPE, 0 if we should
10057 not look, and -1 if we should not call `grokclassfn' at all.
10058
10059 SFK is the kind of special function (if any) for the new function.
10060
10061 Returns `NULL_TREE' if something goes wrong, after issuing
10062 applicable error messages. */
10063
10064 static tree
grokfndecl(tree ctype,tree type,tree declarator,tree parms,tree orig_declarator,const cp_decl_specifier_seq * declspecs,tree decl_reqs,int virtualp,enum overload_flags flags,cp_cv_quals quals,cp_ref_qualifier rqual,tree raises,int check,int friendp,int publicp,int inlinep,bool deletedp,special_function_kind sfk,bool funcdef_flag,bool late_return_type_p,int template_count,tree in_namespace,tree * attrlist,location_t location)10065 grokfndecl (tree ctype,
10066 tree type,
10067 tree declarator,
10068 tree parms,
10069 tree orig_declarator,
10070 const cp_decl_specifier_seq *declspecs,
10071 tree decl_reqs,
10072 int virtualp,
10073 enum overload_flags flags,
10074 cp_cv_quals quals,
10075 cp_ref_qualifier rqual,
10076 tree raises,
10077 int check,
10078 int friendp,
10079 int publicp,
10080 int inlinep,
10081 bool deletedp,
10082 special_function_kind sfk,
10083 bool funcdef_flag,
10084 bool late_return_type_p,
10085 int template_count,
10086 tree in_namespace,
10087 tree* attrlist,
10088 location_t location)
10089 {
10090 tree decl;
10091 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
10092 tree t;
10093
10094 if (location == UNKNOWN_LOCATION)
10095 location = input_location;
10096
10097 /* Was the concept specifier present? */
10098 bool concept_p = inlinep & 4;
10099
10100 /* Concept declarations must have a corresponding definition. */
10101 if (concept_p && !funcdef_flag)
10102 {
10103 error_at (location, "concept %qD has no definition", declarator);
10104 return NULL_TREE;
10105 }
10106
10107 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
10108
10109 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
10110
10111 /* Set the constraints on the declaration. */
10112 if (flag_concepts)
10113 {
10114 tree tmpl_reqs = NULL_TREE;
10115 tree ctx = friendp ? current_class_type : ctype;
10116 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL;
10117 bool memtmpl = (!block_local
10118 && (current_template_depth
10119 > template_class_depth (ctx)));
10120 if (memtmpl)
10121 {
10122 if (!current_template_parms)
10123 /* If there are no template parameters, something must have
10124 gone wrong. */
10125 gcc_assert (seen_error ());
10126 else
10127 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10128 }
10129 tree ci = build_constraints (tmpl_reqs, decl_reqs);
10130 if (concept_p && ci)
10131 {
10132 error_at (location, "a function concept cannot be constrained");
10133 ci = NULL_TREE;
10134 }
10135 /* C++20 CA378: Remove non-templated constrained functions. */
10136 if (ci
10137 && (block_local
10138 || (!flag_concepts_ts
10139 && (!processing_template_decl
10140 || (friendp && !memtmpl && !funcdef_flag)))))
10141 {
10142 error_at (location, "constraints on a non-templated function");
10143 ci = NULL_TREE;
10144 }
10145 set_constraints (decl, ci);
10146 }
10147
10148 if (TREE_CODE (type) == METHOD_TYPE)
10149 {
10150 tree parm = build_this_parm (decl, type, quals);
10151 DECL_CHAIN (parm) = parms;
10152 parms = parm;
10153
10154 /* Allocate space to hold the vptr bit if needed. */
10155 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
10156 }
10157
10158 DECL_ARGUMENTS (decl) = parms;
10159 for (t = parms; t; t = DECL_CHAIN (t))
10160 DECL_CONTEXT (t) = decl;
10161
10162 /* Propagate volatile out from type to decl. */
10163 if (TYPE_VOLATILE (type))
10164 TREE_THIS_VOLATILE (decl) = 1;
10165
10166 /* Setup decl according to sfk. */
10167 switch (sfk)
10168 {
10169 case sfk_constructor:
10170 case sfk_copy_constructor:
10171 case sfk_move_constructor:
10172 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
10173 DECL_NAME (decl) = ctor_identifier;
10174 break;
10175 case sfk_destructor:
10176 DECL_CXX_DESTRUCTOR_P (decl) = 1;
10177 DECL_NAME (decl) = dtor_identifier;
10178 break;
10179 default:
10180 break;
10181 }
10182
10183 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
10184 {
10185 if (funcdef_flag)
10186 error_at (location,
10187 "defining explicit specialization %qD in friend declaration",
10188 orig_declarator);
10189 else
10190 {
10191 tree fns = TREE_OPERAND (orig_declarator, 0);
10192 tree args = TREE_OPERAND (orig_declarator, 1);
10193
10194 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10195 {
10196 /* Something like `template <class T> friend void f<T>()'. */
10197 error_at (location,
10198 "invalid use of template-id %qD in declaration "
10199 "of primary template",
10200 orig_declarator);
10201 return NULL_TREE;
10202 }
10203
10204
10205 /* A friend declaration of the form friend void f<>(). Record
10206 the information in the TEMPLATE_ID_EXPR. */
10207 SET_DECL_IMPLICIT_INSTANTIATION (decl);
10208
10209 gcc_assert (identifier_p (fns) || OVL_P (fns));
10210 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
10211
10212 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
10213 if (TREE_PURPOSE (t)
10214 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
10215 {
10216 error_at (defparse_location (TREE_PURPOSE (t)),
10217 "default arguments are not allowed in declaration "
10218 "of friend template specialization %qD",
10219 decl);
10220 return NULL_TREE;
10221 }
10222
10223 if (inlinep & 1)
10224 {
10225 error_at (declspecs->locations[ds_inline],
10226 "%<inline%> is not allowed in declaration of friend "
10227 "template specialization %qD",
10228 decl);
10229 return NULL_TREE;
10230 }
10231 }
10232 }
10233
10234 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
10235 expression, that declaration shall be a definition..." */
10236 if (friendp && !funcdef_flag)
10237 {
10238 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
10239 t && t != void_list_node; t = TREE_CHAIN (t))
10240 if (TREE_PURPOSE (t))
10241 {
10242 permerror (DECL_SOURCE_LOCATION (decl),
10243 "friend declaration of %qD specifies default "
10244 "arguments and isn%'t a definition", decl);
10245 break;
10246 }
10247 }
10248
10249 /* If this decl has namespace scope, set that up. */
10250 if (in_namespace)
10251 set_decl_namespace (decl, in_namespace, friendp);
10252 else if (ctype)
10253 DECL_CONTEXT (decl) = ctype;
10254 else
10255 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
10256
10257 /* `main' and builtins have implicit 'C' linkage. */
10258 if (ctype == NULL_TREE
10259 && DECL_FILE_SCOPE_P (decl)
10260 && current_lang_name == lang_name_cplusplus
10261 && (MAIN_NAME_P (declarator)
10262 || (IDENTIFIER_LENGTH (declarator) > 10
10263 && IDENTIFIER_POINTER (declarator)[0] == '_'
10264 && IDENTIFIER_POINTER (declarator)[1] == '_'
10265 && startswith (IDENTIFIER_POINTER (declarator) + 2,
10266 "builtin_"))
10267 || (targetcm.cxx_implicit_extern_c
10268 && (targetcm.cxx_implicit_extern_c
10269 (IDENTIFIER_POINTER (declarator))))))
10270 SET_DECL_LANGUAGE (decl, lang_c);
10271
10272 /* Should probably propagate const out from type to decl I bet (mrs). */
10273 if (staticp)
10274 {
10275 DECL_STATIC_FUNCTION_P (decl) = 1;
10276 DECL_CONTEXT (decl) = ctype;
10277 }
10278
10279 if (deletedp)
10280 DECL_DELETED_FN (decl) = 1;
10281
10282 if (ctype && funcdef_flag)
10283 check_class_member_definition_namespace (decl);
10284
10285 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10286 {
10287 if (PROCESSING_REAL_TEMPLATE_DECL_P())
10288 error_at (location, "cannot declare %<::main%> to be a template");
10289 if (inlinep & 1)
10290 error_at (declspecs->locations[ds_inline],
10291 "cannot declare %<::main%> to be inline");
10292 if (inlinep & 2)
10293 error_at (declspecs->locations[ds_constexpr],
10294 "cannot declare %<::main%> to be %qs", "constexpr");
10295 if (inlinep & 8)
10296 error_at (declspecs->locations[ds_consteval],
10297 "cannot declare %<::main%> to be %qs", "consteval");
10298 if (!publicp)
10299 error_at (location, "cannot declare %<::main%> to be static");
10300 inlinep = 0;
10301 publicp = 1;
10302 }
10303
10304 /* Members of anonymous types and local classes have no linkage; make
10305 them internal. If a typedef is made later, this will be changed. */
10306 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
10307 || decl_function_context (TYPE_MAIN_DECL (ctype))))
10308 publicp = 0;
10309
10310 if (publicp && cxx_dialect == cxx98)
10311 {
10312 /* [basic.link]: A name with no linkage (notably, the name of a class
10313 or enumeration declared in a local scope) shall not be used to
10314 declare an entity with linkage.
10315
10316 DR 757 relaxes this restriction for C++0x. */
10317 no_linkage_error (decl);
10318 }
10319
10320 TREE_PUBLIC (decl) = publicp;
10321 if (! publicp)
10322 {
10323 DECL_INTERFACE_KNOWN (decl) = 1;
10324 DECL_NOT_REALLY_EXTERN (decl) = 1;
10325 }
10326
10327 /* If the declaration was declared inline, mark it as such. */
10328 if (inlinep)
10329 {
10330 DECL_DECLARED_INLINE_P (decl) = 1;
10331 if (publicp)
10332 DECL_COMDAT (decl) = 1;
10333 }
10334 if (inlinep & 2)
10335 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10336 else if (inlinep & 8)
10337 {
10338 DECL_DECLARED_CONSTEXPR_P (decl) = true;
10339 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
10340 }
10341
10342 // If the concept declaration specifier was found, check
10343 // that the declaration satisfies the necessary requirements.
10344 if (concept_p)
10345 {
10346 DECL_DECLARED_CONCEPT_P (decl) = true;
10347 check_concept_fn (decl);
10348 }
10349
10350 DECL_EXTERNAL (decl) = 1;
10351 if (TREE_CODE (type) == FUNCTION_TYPE)
10352 {
10353 if (quals || rqual)
10354 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
10355 TYPE_UNQUALIFIED,
10356 REF_QUAL_NONE);
10357
10358 if (quals)
10359 {
10360 error (ctype
10361 ? G_("static member function %qD cannot have cv-qualifier")
10362 : G_("non-member function %qD cannot have cv-qualifier"),
10363 decl);
10364 quals = TYPE_UNQUALIFIED;
10365 }
10366
10367 if (rqual)
10368 {
10369 error (ctype
10370 ? G_("static member function %qD cannot have ref-qualifier")
10371 : G_("non-member function %qD cannot have ref-qualifier"),
10372 decl);
10373 rqual = REF_QUAL_NONE;
10374 }
10375 }
10376
10377 if (deduction_guide_p (decl))
10378 {
10379 tree type = TREE_TYPE (DECL_NAME (decl));
10380 if (in_namespace == NULL_TREE
10381 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type))
10382 {
10383 error_at (location, "deduction guide %qD must be declared in the "
10384 "same scope as %qT", decl, type);
10385 inform (location_of (type), " declared here");
10386 return NULL_TREE;
10387 }
10388 if (DECL_CLASS_SCOPE_P (decl)
10389 && current_access_specifier != declared_access (TYPE_NAME (type)))
10390 {
10391 error_at (location, "deduction guide %qD must have the same access "
10392 "as %qT", decl, type);
10393 inform (location_of (type), " declared here");
10394 }
10395 if (funcdef_flag)
10396 error_at (location,
10397 "deduction guide %qD must not have a function body", decl);
10398 }
10399 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
10400 && !grok_op_properties (decl, /*complain=*/true))
10401 return NULL_TREE;
10402 else if (UDLIT_OPER_P (DECL_NAME (decl)))
10403 {
10404 bool long_long_unsigned_p;
10405 bool long_double_p;
10406 const char *suffix = NULL;
10407 /* [over.literal]/6: Literal operators shall not have C linkage. */
10408 if (DECL_LANGUAGE (decl) == lang_c)
10409 {
10410 error_at (location, "literal operator with C linkage");
10411 maybe_show_extern_c_location ();
10412 return NULL_TREE;
10413 }
10414
10415 if (DECL_NAMESPACE_SCOPE_P (decl))
10416 {
10417 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
10418 &long_double_p))
10419 {
10420 error_at (location, "%qD has invalid argument list", decl);
10421 return NULL_TREE;
10422 }
10423
10424 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
10425 if (long_long_unsigned_p)
10426 {
10427 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
10428 warning_at (location, 0, "integer suffix %qs"
10429 " shadowed by implementation", suffix);
10430 }
10431 else if (long_double_p)
10432 {
10433 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
10434 warning_at (location, 0, "floating-point suffix %qs"
10435 " shadowed by implementation", suffix);
10436 }
10437 /* 17.6.3.3.5 */
10438 if (suffix[0] != '_'
10439 && !current_function_decl && !(friendp && !funcdef_flag))
10440 warning_at (location, OPT_Wliteral_suffix,
10441 "literal operator suffixes not preceded by %<_%>"
10442 " are reserved for future standardization");
10443 }
10444 else
10445 {
10446 error_at (location, "%qD must be a non-member function", decl);
10447 return NULL_TREE;
10448 }
10449 }
10450
10451 if (funcdef_flag)
10452 /* Make the init_value nonzero so pushdecl knows this is not
10453 tentative. error_mark_node is replaced later with the BLOCK. */
10454 DECL_INITIAL (decl) = error_mark_node;
10455
10456 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
10457 TREE_NOTHROW (decl) = 1;
10458
10459 if (flag_openmp || flag_openmp_simd)
10460 {
10461 /* Adjust "omp declare simd" attributes. */
10462 tree ods = lookup_attribute ("omp declare simd", *attrlist);
10463 if (ods)
10464 {
10465 tree attr;
10466 for (attr = ods; attr;
10467 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
10468 {
10469 if (TREE_CODE (type) == METHOD_TYPE)
10470 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
10471 DECL_ARGUMENTS (decl), NULL);
10472 if (TREE_VALUE (attr) != NULL_TREE)
10473 {
10474 tree cl = TREE_VALUE (TREE_VALUE (attr));
10475 cl = c_omp_declare_simd_clauses_to_numbers
10476 (DECL_ARGUMENTS (decl), cl);
10477 if (cl)
10478 TREE_VALUE (TREE_VALUE (attr)) = cl;
10479 else
10480 TREE_VALUE (attr) = NULL_TREE;
10481 }
10482 }
10483 }
10484 }
10485
10486 /* Caller will do the rest of this. */
10487 if (check < 0)
10488 return decl;
10489
10490 if (ctype != NULL_TREE)
10491 grokclassfn (ctype, decl, flags);
10492
10493 /* 12.4/3 */
10494 if (cxx_dialect >= cxx11
10495 && DECL_DESTRUCTOR_P (decl)
10496 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
10497 && !processing_template_decl)
10498 deduce_noexcept_on_destructor (decl);
10499
10500 set_originating_module (decl);
10501
10502 decl = check_explicit_specialization (orig_declarator, decl,
10503 template_count,
10504 2 * funcdef_flag +
10505 4 * (friendp != 0) +
10506 8 * concept_p,
10507 *attrlist);
10508 if (decl == error_mark_node)
10509 return NULL_TREE;
10510
10511 if (DECL_STATIC_FUNCTION_P (decl))
10512 check_static_quals (decl, quals);
10513
10514 if (attrlist)
10515 {
10516 cplus_decl_attributes (&decl, *attrlist, 0);
10517 *attrlist = NULL_TREE;
10518 }
10519
10520 /* Check main's type after attributes have been applied. */
10521 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
10522 {
10523 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
10524 integer_type_node))
10525 {
10526 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
10527 tree newtype;
10528 error_at (declspecs->locations[ds_type_spec],
10529 "%<::main%> must return %<int%>");
10530 newtype = build_function_type (integer_type_node, oldtypeargs);
10531 TREE_TYPE (decl) = newtype;
10532 }
10533 if (warn_main)
10534 check_main_parameter_types (decl);
10535 }
10536
10537 if (ctype != NULL_TREE && check)
10538 {
10539 tree old_decl = check_classfn (ctype, decl,
10540 (current_template_depth
10541 > template_class_depth (ctype))
10542 ? current_template_parms
10543 : NULL_TREE);
10544
10545 if (old_decl == error_mark_node)
10546 return NULL_TREE;
10547
10548 if (old_decl)
10549 {
10550 tree ok;
10551 tree pushed_scope;
10552
10553 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
10554 /* Because grokfndecl is always supposed to return a
10555 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
10556 here. We depend on our callers to figure out that its
10557 really a template that's being returned. */
10558 old_decl = DECL_TEMPLATE_RESULT (old_decl);
10559
10560 if (DECL_STATIC_FUNCTION_P (old_decl)
10561 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
10562 {
10563 /* Remove the `this' parm added by grokclassfn. */
10564 revert_static_member_fn (decl);
10565 check_static_quals (decl, quals);
10566 }
10567 if (DECL_ARTIFICIAL (old_decl))
10568 {
10569 error ("definition of implicitly-declared %qD", old_decl);
10570 return NULL_TREE;
10571 }
10572 else if (DECL_DEFAULTED_FN (old_decl))
10573 {
10574 error ("definition of explicitly-defaulted %q+D", decl);
10575 inform (DECL_SOURCE_LOCATION (old_decl),
10576 "%q#D explicitly defaulted here", old_decl);
10577 return NULL_TREE;
10578 }
10579
10580 /* Since we've smashed OLD_DECL to its
10581 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
10582 if (TREE_CODE (decl) == TEMPLATE_DECL)
10583 decl = DECL_TEMPLATE_RESULT (decl);
10584
10585 /* Attempt to merge the declarations. This can fail, in
10586 the case of some invalid specialization declarations. */
10587 pushed_scope = push_scope (ctype);
10588 ok = duplicate_decls (decl, old_decl);
10589 if (pushed_scope)
10590 pop_scope (pushed_scope);
10591 if (!ok)
10592 {
10593 error ("no %q#D member function declared in class %qT",
10594 decl, ctype);
10595 return NULL_TREE;
10596 }
10597 if (ok == error_mark_node)
10598 return NULL_TREE;
10599 return old_decl;
10600 }
10601 }
10602
10603 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
10604 return NULL_TREE;
10605
10606 if (ctype == NULL_TREE || check)
10607 return decl;
10608
10609 if (virtualp)
10610 DECL_VIRTUAL_P (decl) = 1;
10611
10612 return decl;
10613 }
10614
10615 /* decl is a FUNCTION_DECL.
10616 specifiers are the parsed virt-specifiers.
10617
10618 Set flags to reflect the virt-specifiers.
10619
10620 Returns decl. */
10621
10622 static tree
set_virt_specifiers(tree decl,cp_virt_specifiers specifiers)10623 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
10624 {
10625 if (decl == NULL_TREE)
10626 return decl;
10627 if (specifiers & VIRT_SPEC_OVERRIDE)
10628 DECL_OVERRIDE_P (decl) = 1;
10629 if (specifiers & VIRT_SPEC_FINAL)
10630 DECL_FINAL_P (decl) = 1;
10631 return decl;
10632 }
10633
10634 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
10635 the linkage that DECL will receive in the object file. */
10636
10637 static void
set_linkage_for_static_data_member(tree decl)10638 set_linkage_for_static_data_member (tree decl)
10639 {
10640 /* A static data member always has static storage duration and
10641 external linkage. Note that static data members are forbidden in
10642 local classes -- the only situation in which a class has
10643 non-external linkage. */
10644 TREE_PUBLIC (decl) = 1;
10645 TREE_STATIC (decl) = 1;
10646 /* For non-template classes, static data members are always put
10647 out in exactly those files where they are defined, just as
10648 with ordinary namespace-scope variables. */
10649 if (!processing_template_decl)
10650 DECL_INTERFACE_KNOWN (decl) = 1;
10651 }
10652
10653 /* Create a VAR_DECL named NAME with the indicated TYPE.
10654
10655 If SCOPE is non-NULL, it is the class type or namespace containing
10656 the variable. If SCOPE is NULL, the variable should is created in
10657 the innermost enclosing scope. */
10658
10659 static tree
grokvardecl(tree type,tree name,tree orig_declarator,const cp_decl_specifier_seq * declspecs,int initialized,int type_quals,int inlinep,bool conceptp,int template_count,tree scope,location_t location)10660 grokvardecl (tree type,
10661 tree name,
10662 tree orig_declarator,
10663 const cp_decl_specifier_seq *declspecs,
10664 int initialized,
10665 int type_quals,
10666 int inlinep,
10667 bool conceptp,
10668 int template_count,
10669 tree scope,
10670 location_t location)
10671 {
10672 tree decl;
10673 tree explicit_scope;
10674
10675 gcc_assert (!name || identifier_p (name));
10676
10677 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
10678 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
10679
10680 /* Compute the scope in which to place the variable, but remember
10681 whether or not that scope was explicitly specified by the user. */
10682 explicit_scope = scope;
10683 if (!scope)
10684 {
10685 /* An explicit "extern" specifier indicates a namespace-scope
10686 variable. */
10687 if (declspecs->storage_class == sc_extern)
10688 scope = current_decl_namespace ();
10689 else if (!at_function_scope_p ())
10690 scope = current_scope ();
10691 }
10692
10693 if (scope
10694 && (/* If the variable is a namespace-scope variable declared in a
10695 template, we need DECL_LANG_SPECIFIC. */
10696 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
10697 /* Similarly for namespace-scope variables with language linkage
10698 other than C++. */
10699 || (TREE_CODE (scope) == NAMESPACE_DECL
10700 && current_lang_name != lang_name_cplusplus)
10701 /* Similarly for static data members. */
10702 || TYPE_P (scope)
10703 /* Similarly for explicit specializations. */
10704 || (orig_declarator
10705 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
10706 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
10707 else
10708 decl = build_decl (location, VAR_DECL, name, type);
10709
10710 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
10711 set_decl_namespace (decl, explicit_scope, 0);
10712 else
10713 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
10714
10715 if (declspecs->storage_class == sc_extern)
10716 {
10717 DECL_THIS_EXTERN (decl) = 1;
10718 DECL_EXTERNAL (decl) = !initialized;
10719 }
10720
10721 if (DECL_CLASS_SCOPE_P (decl))
10722 {
10723 set_linkage_for_static_data_member (decl);
10724 /* This function is only called with out-of-class definitions. */
10725 DECL_EXTERNAL (decl) = 0;
10726 check_class_member_definition_namespace (decl);
10727 }
10728 /* At top level, either `static' or no s.c. makes a definition
10729 (perhaps tentative), and absence of `static' makes it public. */
10730 else if (toplevel_bindings_p ())
10731 {
10732 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
10733 && (DECL_THIS_EXTERN (decl)
10734 || ! constp
10735 || volatilep
10736 || inlinep));
10737 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10738 }
10739 /* Not at top level, only `static' makes a static definition. */
10740 else
10741 {
10742 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
10743 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10744 }
10745
10746 set_originating_module (decl);
10747
10748 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
10749 {
10750 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
10751 {
10752 CP_DECL_THREAD_LOCAL_P (decl) = true;
10753 if (!processing_template_decl)
10754 set_decl_tls_model (decl, decl_default_tls_model (decl));
10755 }
10756 if (declspecs->gnu_thread_keyword_p)
10757 SET_DECL_GNU_TLS_P (decl);
10758 }
10759
10760 /* If the type of the decl has no linkage, make sure that we'll
10761 notice that in mark_used. */
10762 if (cxx_dialect > cxx98
10763 && decl_linkage (decl) != lk_none
10764 && DECL_LANG_SPECIFIC (decl) == NULL
10765 && !DECL_EXTERN_C_P (decl)
10766 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
10767 retrofit_lang_decl (decl);
10768
10769 if (TREE_PUBLIC (decl))
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 if (cxx_dialect < cxx11)
10777 no_linkage_error (decl);
10778 }
10779 else
10780 DECL_INTERFACE_KNOWN (decl) = 1;
10781
10782 if (DECL_NAME (decl)
10783 && MAIN_NAME_P (DECL_NAME (decl))
10784 && scope == global_namespace)
10785 error_at (DECL_SOURCE_LOCATION (decl),
10786 "cannot declare %<::main%> to be a global variable");
10787
10788 /* Check that the variable can be safely declared as a concept.
10789 Note that this also forbids explicit specializations. */
10790 if (conceptp)
10791 {
10792 if (!processing_template_decl)
10793 {
10794 error_at (declspecs->locations[ds_concept],
10795 "a non-template variable cannot be %<concept%>");
10796 return NULL_TREE;
10797 }
10798 else if (!at_namespace_scope_p ())
10799 {
10800 error_at (declspecs->locations[ds_concept],
10801 "concept must be defined at namespace scope");
10802 return NULL_TREE;
10803 }
10804 else
10805 DECL_DECLARED_CONCEPT_P (decl) = true;
10806 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10807 error_at (declspecs->locations[ds_type_spec],
10808 "concept must have type %<bool%>");
10809 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10810 {
10811 error_at (location, "a variable concept cannot be constrained");
10812 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10813 }
10814 }
10815 else if (flag_concepts
10816 && current_template_depth > template_class_depth (scope))
10817 {
10818 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10819 tree ci = build_constraints (reqs, NULL_TREE);
10820
10821 set_constraints (decl, ci);
10822 }
10823
10824 // Handle explicit specializations and instantiations of variable templates.
10825 if (orig_declarator)
10826 decl = check_explicit_specialization (orig_declarator, decl,
10827 template_count, conceptp * 8);
10828
10829 return decl != error_mark_node ? decl : NULL_TREE;
10830 }
10831
10832 /* Create and return a canonical pointer to member function type, for
10833 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10834
10835 tree
build_ptrmemfunc_type(tree type)10836 build_ptrmemfunc_type (tree type)
10837 {
10838 tree field, fields;
10839 tree t;
10840
10841 if (type == error_mark_node)
10842 return type;
10843
10844 /* Make sure that we always have the unqualified pointer-to-member
10845 type first. */
10846 if (cp_cv_quals quals = cp_type_quals (type))
10847 {
10848 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10849 return cp_build_qualified_type (unqual, quals);
10850 }
10851
10852 /* If a canonical type already exists for this type, use it. We use
10853 this method instead of type_hash_canon, because it only does a
10854 simple equality check on the list of field members. */
10855
10856 t = TYPE_PTRMEMFUNC_TYPE (type);
10857 if (t)
10858 return t;
10859
10860 t = make_node (RECORD_TYPE);
10861
10862 /* Let the front end know this is a pointer to member function. */
10863 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10864
10865 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10866 DECL_NONADDRESSABLE_P (field) = 1;
10867 fields = field;
10868
10869 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10870 delta_type_node);
10871 DECL_NONADDRESSABLE_P (field) = 1;
10872 DECL_CHAIN (field) = fields;
10873 fields = field;
10874
10875 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10876
10877 /* Zap out the name so that the back end will give us the debugging
10878 information for this anonymous RECORD_TYPE. */
10879 TYPE_NAME (t) = NULL_TREE;
10880
10881 /* Cache this pointer-to-member type so that we can find it again
10882 later. */
10883 TYPE_PTRMEMFUNC_TYPE (type) = t;
10884
10885 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10886 SET_TYPE_STRUCTURAL_EQUALITY (t);
10887 else if (TYPE_CANONICAL (type) != type)
10888 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10889
10890 return t;
10891 }
10892
10893 /* Create and return a pointer to data member type. */
10894
10895 tree
build_ptrmem_type(tree class_type,tree member_type)10896 build_ptrmem_type (tree class_type, tree member_type)
10897 {
10898 if (TREE_CODE (member_type) == METHOD_TYPE)
10899 {
10900 cp_cv_quals quals = type_memfn_quals (member_type);
10901 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10902 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10903 return build_ptrmemfunc_type (build_pointer_type (member_type));
10904 }
10905 else
10906 {
10907 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10908 return build_offset_type (class_type, member_type);
10909 }
10910 }
10911
10912 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10913 Check to see that the definition is valid. Issue appropriate error
10914 messages. */
10915
10916 static void
check_static_variable_definition(tree decl,tree type)10917 check_static_variable_definition (tree decl, tree type)
10918 {
10919 /* Avoid redundant diagnostics on out-of-class definitions. */
10920 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10921 ;
10922 /* Can't check yet if we don't know the type. */
10923 else if (dependent_type_p (type))
10924 ;
10925 /* If DECL is declared constexpr, we'll do the appropriate checks
10926 in check_initializer. Similarly for inline static data members. */
10927 else if (DECL_P (decl)
10928 && (DECL_DECLARED_CONSTEXPR_P (decl)
10929 || DECL_VAR_DECLARED_INLINE_P (decl)))
10930 ;
10931 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10932 {
10933 if (!COMPLETE_TYPE_P (type))
10934 error_at (DECL_SOURCE_LOCATION (decl),
10935 "in-class initialization of static data member %q#D of "
10936 "incomplete type", decl);
10937 else if (literal_type_p (type))
10938 permerror (DECL_SOURCE_LOCATION (decl),
10939 "%<constexpr%> needed for in-class initialization of "
10940 "static data member %q#D of non-integral type", decl);
10941 else
10942 error_at (DECL_SOURCE_LOCATION (decl),
10943 "in-class initialization of static data member %q#D of "
10944 "non-literal type", decl);
10945 }
10946 /* Motion 10 at San Diego: If a static const integral data member is
10947 initialized with an integral constant expression, the initializer
10948 may appear either in the declaration (within the class), or in
10949 the definition, but not both. If it appears in the class, the
10950 member is a member constant. The file-scope definition is always
10951 required. */
10952 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10953 error_at (DECL_SOURCE_LOCATION (decl),
10954 "invalid in-class initialization of static data member "
10955 "of non-integral type %qT",
10956 type);
10957 else if (!CP_TYPE_CONST_P (type))
10958 error_at (DECL_SOURCE_LOCATION (decl),
10959 "ISO C++ forbids in-class initialization of non-const "
10960 "static member %qD",
10961 decl);
10962 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10963 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10964 "ISO C++ forbids initialization of member constant "
10965 "%qD of non-integral type %qT", decl, type);
10966 }
10967
10968 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10969 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10970 expressions out into temporary variables so that walk_tree doesn't
10971 step into them (c++/15764). */
10972
10973 static tree
stabilize_save_expr_r(tree * expr_p,int * walk_subtrees,void * data)10974 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10975 {
10976 hash_set<tree> *pset = (hash_set<tree> *)data;
10977 tree expr = *expr_p;
10978 if (TREE_CODE (expr) == SAVE_EXPR)
10979 {
10980 tree op = TREE_OPERAND (expr, 0);
10981 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10982 if (TREE_SIDE_EFFECTS (op))
10983 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10984 *walk_subtrees = 0;
10985 }
10986 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10987 *walk_subtrees = 0;
10988 return NULL;
10989 }
10990
10991 /* Entry point for the above. */
10992
10993 static void
stabilize_vla_size(tree size)10994 stabilize_vla_size (tree size)
10995 {
10996 hash_set<tree> pset;
10997 /* Break out any function calls into temporary variables. */
10998 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10999 }
11000
11001 /* Reduce a SIZEOF_EXPR to its value. */
11002
11003 tree
fold_sizeof_expr(tree t)11004 fold_sizeof_expr (tree t)
11005 {
11006 tree r;
11007 if (SIZEOF_EXPR_TYPE_P (t))
11008 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11009 TREE_TYPE (TREE_OPERAND (t, 0)),
11010 SIZEOF_EXPR, false, false);
11011 else if (TYPE_P (TREE_OPERAND (t, 0)))
11012 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
11013 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11014 false, false);
11015 else
11016 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
11017 TREE_OPERAND (t, 0), SIZEOF_EXPR,
11018 false, false);
11019 if (r == error_mark_node)
11020 r = size_one_node;
11021 return r;
11022 }
11023
11024 /* Given the SIZE (i.e., number of elements) in an array, compute
11025 an appropriate index type for the array. If non-NULL, NAME is
11026 the name of the entity being declared. */
11027
11028 static tree
compute_array_index_type_loc(location_t name_loc,tree name,tree size,tsubst_flags_t complain)11029 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
11030 tsubst_flags_t complain)
11031 {
11032 if (error_operand_p (size))
11033 return error_mark_node;
11034
11035 /* The type of the index being computed. */
11036 tree itype;
11037
11038 /* The original numeric size as seen in the source code before
11039 conversion to size_t. */
11040 tree origsize = size;
11041
11042 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
11043
11044 if (!type_dependent_expression_p (size))
11045 {
11046 origsize = size = mark_rvalue_use (size);
11047
11048 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
11049 && TREE_SIDE_EFFECTS (size))
11050 /* In C++98, we mark a non-constant array bound with a magic
11051 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
11052 else
11053 {
11054 size = build_converted_constant_expr (size_type_node, size, complain);
11055 /* Pedantically a constant expression is required here and so
11056 __builtin_is_constant_evaluated () should fold to true if it
11057 is successfully folded into a constant. */
11058 size = fold_non_dependent_expr (size, complain,
11059 /*manifestly_const_eval=*/true);
11060
11061 if (!TREE_CONSTANT (size))
11062 size = origsize;
11063 }
11064
11065 if (error_operand_p (size))
11066 return error_mark_node;
11067
11068 /* The array bound must be an integer type. */
11069 tree type = TREE_TYPE (size);
11070 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
11071 {
11072 if (!(complain & tf_error))
11073 return error_mark_node;
11074 if (name)
11075 error_at (loc, "size of array %qD has non-integral type %qT",
11076 name, type);
11077 else
11078 error_at (loc, "size of array has non-integral type %qT", type);
11079 size = integer_one_node;
11080 }
11081 }
11082
11083 /* A type is dependent if it is...an array type constructed from any
11084 dependent type or whose size is specified by a constant expression
11085 that is value-dependent. */
11086 /* We can only call value_dependent_expression_p on integral constant
11087 expressions. */
11088 if (processing_template_decl
11089 && potential_constant_expression (size)
11090 && value_dependent_expression_p (size))
11091 {
11092 /* Just build the index type and mark that it requires
11093 structural equality checks. */
11094 in_template:
11095 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
11096 size, size_one_node));
11097 TYPE_DEPENDENT_P (itype) = 1;
11098 TYPE_DEPENDENT_P_VALID (itype) = 1;
11099 SET_TYPE_STRUCTURAL_EQUALITY (itype);
11100 return itype;
11101 }
11102
11103 if (TREE_CODE (size) != INTEGER_CST)
11104 {
11105 tree folded = cp_fully_fold (size);
11106 if (TREE_CODE (folded) == INTEGER_CST)
11107 {
11108 if (name)
11109 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
11110 "integral constant-expression", name);
11111 else
11112 pedwarn (loc, OPT_Wpedantic,
11113 "size of array is not an integral constant-expression");
11114 }
11115 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded))
11116 /* We might have lost the TREE_CONSTANT flag e.g. when we are
11117 folding a conversion from a pointer to integral type. In that
11118 case issue an error below and don't treat this as a VLA. */;
11119 else
11120 /* Use the folded result for VLAs, too; it will have resolved
11121 SIZEOF_EXPR. */
11122 size = folded;
11123 }
11124
11125 /* Normally, the array-bound will be a constant. */
11126 if (TREE_CODE (size) == INTEGER_CST)
11127 {
11128 /* The size to use in diagnostics that reflects the constant
11129 size used in the source, rather than SIZE massaged above. */
11130 tree diagsize = size;
11131
11132 /* If the original size before conversion to size_t was signed
11133 and negative, convert it to ssizetype to restore the sign. */
11134 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
11135 && TREE_CODE (size) == INTEGER_CST
11136 && tree_int_cst_sign_bit (size))
11137 {
11138 diagsize = fold_convert (ssizetype, size);
11139
11140 /* Clear the overflow bit that may have been set as a result
11141 of the conversion from the sizetype of the new size to
11142 ssizetype. */
11143 TREE_OVERFLOW (diagsize) = false;
11144 }
11145
11146 /* Verify that the array has a positive number of elements
11147 and issue the appropriate diagnostic if it doesn't. */
11148 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
11149 {
11150 if (!(complain & tf_error))
11151 return error_mark_node;
11152 size = integer_one_node;
11153 }
11154 /* As an extension we allow zero-sized arrays. */
11155 else if (integer_zerop (size))
11156 {
11157 if (!(complain & tf_error))
11158 /* We must fail if performing argument deduction (as
11159 indicated by the state of complain), so that
11160 another substitution can be found. */
11161 return error_mark_node;
11162 else if (name)
11163 pedwarn (loc, OPT_Wpedantic,
11164 "ISO C++ forbids zero-size array %qD", name);
11165 else
11166 pedwarn (loc, OPT_Wpedantic,
11167 "ISO C++ forbids zero-size array");
11168 }
11169 }
11170 else if (TREE_CONSTANT (size)
11171 /* We don't allow VLAs at non-function scopes, or during
11172 tentative template substitution. */
11173 || !at_function_scope_p ()
11174 || !(complain & tf_error))
11175 {
11176 if (!(complain & tf_error))
11177 return error_mark_node;
11178 /* `(int) &fn' is not a valid array bound. */
11179 if (name)
11180 error_at (loc,
11181 "size of array %qD is not an integral constant-expression",
11182 name);
11183 else
11184 error_at (loc, "size of array is not an integral constant-expression");
11185 size = integer_one_node;
11186 }
11187 else if (pedantic && warn_vla != 0)
11188 {
11189 if (name)
11190 pedwarn (name_loc, OPT_Wvla,
11191 "ISO C++ forbids variable length array %qD", name);
11192 else
11193 pedwarn (input_location, OPT_Wvla,
11194 "ISO C++ forbids variable length array");
11195 }
11196 else if (warn_vla > 0)
11197 {
11198 if (name)
11199 warning_at (name_loc, OPT_Wvla,
11200 "variable length array %qD is used", name);
11201 else
11202 warning (OPT_Wvla,
11203 "variable length array is used");
11204 }
11205
11206 if (processing_template_decl && !TREE_CONSTANT (size))
11207 goto in_template;
11208 else
11209 {
11210 if (!TREE_CONSTANT (size))
11211 {
11212 /* A variable sized array. Arrange for the SAVE_EXPR on the inside
11213 of the MINUS_EXPR, which allows the -1 to get folded with the +1
11214 that happens when building TYPE_SIZE. */
11215 size = variable_size (size);
11216 stabilize_vla_size (size);
11217 }
11218
11219 /* Compute the index of the largest element in the array. It is
11220 one less than the number of elements in the array. We save
11221 and restore PROCESSING_TEMPLATE_DECL so that computations in
11222 cp_build_binary_op will be appropriately folded. */
11223 {
11224 processing_template_decl_sentinel s;
11225 itype = cp_build_binary_op (input_location,
11226 MINUS_EXPR,
11227 cp_convert (ssizetype, size, complain),
11228 cp_convert (ssizetype, integer_one_node,
11229 complain),
11230 complain);
11231 itype = maybe_constant_value (itype, NULL_TREE, true);
11232 }
11233
11234 if (!TREE_CONSTANT (itype))
11235 {
11236 if (sanitize_flags_p (SANITIZE_VLA)
11237 && current_function_decl != NULL_TREE)
11238 {
11239 /* We have to add 1 -- in the ubsan routine we generate
11240 LE_EXPR rather than LT_EXPR. */
11241 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
11242 build_one_cst (TREE_TYPE (itype)));
11243 t = ubsan_instrument_vla (input_location, t);
11244 finish_expr_stmt (t);
11245 }
11246 }
11247 /* Make sure that there was no overflow when creating to a signed
11248 index type. (For example, on a 32-bit machine, an array with
11249 size 2^32 - 1 is too big.) */
11250 else if (TREE_CODE (itype) == INTEGER_CST
11251 && TREE_OVERFLOW (itype))
11252 {
11253 if (!(complain & tf_error))
11254 return error_mark_node;
11255 error ("overflow in array dimension");
11256 TREE_OVERFLOW (itype) = 0;
11257 }
11258 }
11259
11260 /* Create and return the appropriate index type. */
11261 itype = build_index_type (itype);
11262
11263 /* If the index type were dependent, we would have returned early, so
11264 remember that it isn't. */
11265 TYPE_DEPENDENT_P (itype) = 0;
11266 TYPE_DEPENDENT_P_VALID (itype) = 1;
11267 return itype;
11268 }
11269
11270 tree
compute_array_index_type(tree name,tree size,tsubst_flags_t complain)11271 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
11272 {
11273 return compute_array_index_type_loc (input_location, name, size, complain);
11274 }
11275
11276 /* Returns the scope (if any) in which the entity declared by
11277 DECLARATOR will be located. If the entity was declared with an
11278 unqualified name, NULL_TREE is returned. */
11279
11280 tree
get_scope_of_declarator(const cp_declarator * declarator)11281 get_scope_of_declarator (const cp_declarator *declarator)
11282 {
11283 while (declarator && declarator->kind != cdk_id)
11284 declarator = declarator->declarator;
11285
11286 /* If the declarator-id is a SCOPE_REF, the scope in which the
11287 declaration occurs is the first operand. */
11288 if (declarator
11289 && declarator->u.id.qualifying_scope)
11290 return declarator->u.id.qualifying_scope;
11291
11292 /* Otherwise, the declarator is not a qualified name; the entity will
11293 be declared in the current scope. */
11294 return NULL_TREE;
11295 }
11296
11297 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
11298 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
11299 with this type. */
11300
11301 static tree
create_array_type_for_decl(tree name,tree type,tree size,location_t loc)11302 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
11303 {
11304 tree itype = NULL_TREE;
11305
11306 /* If things have already gone awry, bail now. */
11307 if (type == error_mark_node || size == error_mark_node)
11308 return error_mark_node;
11309
11310 /* [dcl.type.class.deduct] prohibits forming an array of placeholder
11311 for a deduced class type. */
11312 if (template_placeholder_p (type))
11313 {
11314 if (name)
11315 error_at (loc, "%qD declared as array of template placeholder "
11316 "type %qT", name, type);
11317 else
11318 error ("creating array of template placeholder type %qT", type);
11319 return error_mark_node;
11320 }
11321
11322 /* If there are some types which cannot be array elements,
11323 issue an error-message and return. */
11324 switch (TREE_CODE (type))
11325 {
11326 case VOID_TYPE:
11327 if (name)
11328 error_at (loc, "declaration of %qD as array of void", name);
11329 else
11330 error ("creating array of void");
11331 return error_mark_node;
11332
11333 case FUNCTION_TYPE:
11334 if (name)
11335 error_at (loc, "declaration of %qD as array of functions", name);
11336 else
11337 error ("creating array of functions");
11338 return error_mark_node;
11339
11340 case REFERENCE_TYPE:
11341 if (name)
11342 error_at (loc, "declaration of %qD as array of references", name);
11343 else
11344 error ("creating array of references");
11345 return error_mark_node;
11346
11347 case METHOD_TYPE:
11348 if (name)
11349 error_at (loc, "declaration of %qD as array of function members",
11350 name);
11351 else
11352 error ("creating array of function members");
11353 return error_mark_node;
11354
11355 default:
11356 break;
11357 }
11358
11359 if (!verify_type_context (name ? loc : input_location,
11360 TCTX_ARRAY_ELEMENT, type))
11361 return error_mark_node;
11362
11363 /* [dcl.array]
11364
11365 The constant expressions that specify the bounds of the arrays
11366 can be omitted only for the first member of the sequence. */
11367 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
11368 {
11369 if (name)
11370 error_at (loc, "declaration of %qD as multidimensional array must "
11371 "have bounds for all dimensions except the first",
11372 name);
11373 else
11374 error ("multidimensional array must have bounds for all "
11375 "dimensions except the first");
11376
11377 return error_mark_node;
11378 }
11379
11380 /* Figure out the index type for the array. */
11381 if (size)
11382 {
11383 itype = compute_array_index_type_loc (loc, name, size,
11384 tf_warning_or_error);
11385 if (type_uses_auto (type)
11386 && variably_modified_type_p (itype, /*fn=*/NULL_TREE))
11387 {
11388 sorry_at (loc, "variable-length array of %<auto%>");
11389 return error_mark_node;
11390 }
11391 }
11392
11393 return build_cplus_array_type (type, itype);
11394 }
11395
11396 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
11397
11398 static location_t
min_location(location_t loca,location_t locb)11399 min_location (location_t loca, location_t locb)
11400 {
11401 if (loca == UNKNOWN_LOCATION
11402 || (locb != UNKNOWN_LOCATION
11403 && linemap_location_before_p (line_table, locb, loca)))
11404 return locb;
11405 return loca;
11406 }
11407
11408 /* Returns the smallest location != UNKNOWN_LOCATION among the
11409 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
11410 and LOCATIONS[ds_restrict]. */
11411
11412 static location_t
smallest_type_quals_location(int type_quals,const location_t * locations)11413 smallest_type_quals_location (int type_quals, const location_t* locations)
11414 {
11415 location_t loc = UNKNOWN_LOCATION;
11416
11417 if (type_quals & TYPE_QUAL_CONST)
11418 loc = locations[ds_const];
11419
11420 if (type_quals & TYPE_QUAL_VOLATILE)
11421 loc = min_location (loc, locations[ds_volatile]);
11422
11423 if (type_quals & TYPE_QUAL_RESTRICT)
11424 loc = min_location (loc, locations[ds_restrict]);
11425
11426 return loc;
11427 }
11428
11429 /* Returns the smallest among the latter and locations[ds_type_spec]. */
11430
11431 static location_t
smallest_type_location(int type_quals,const location_t * locations)11432 smallest_type_location (int type_quals, const location_t* locations)
11433 {
11434 location_t loc = smallest_type_quals_location (type_quals, locations);
11435 return min_location (loc, locations[ds_type_spec]);
11436 }
11437
11438 static location_t
smallest_type_location(const cp_decl_specifier_seq * declspecs)11439 smallest_type_location (const cp_decl_specifier_seq *declspecs)
11440 {
11441 int type_quals = get_type_quals (declspecs);
11442 return smallest_type_location (type_quals, declspecs->locations);
11443 }
11444
11445 /* Check that it's OK to declare a function with the indicated TYPE
11446 and TYPE_QUALS. SFK indicates the kind of special function (if any)
11447 that this function is. OPTYPE is the type given in a conversion
11448 operator declaration, or the class type for a constructor/destructor.
11449 Returns the actual return type of the function; that may be different
11450 than TYPE if an error occurs, or for certain special functions. */
11451
11452 static tree
check_special_function_return_type(special_function_kind sfk,tree type,tree optype,int type_quals,const location_t * locations)11453 check_special_function_return_type (special_function_kind sfk,
11454 tree type,
11455 tree optype,
11456 int type_quals,
11457 const location_t* locations)
11458 {
11459 switch (sfk)
11460 {
11461 case sfk_constructor:
11462 if (type)
11463 error_at (smallest_type_location (type_quals, locations),
11464 "return type specification for constructor invalid");
11465 else if (type_quals != TYPE_UNQUALIFIED)
11466 error_at (smallest_type_quals_location (type_quals, locations),
11467 "qualifiers are not allowed on constructor declaration");
11468
11469 if (targetm.cxx.cdtor_returns_this ())
11470 type = build_pointer_type (optype);
11471 else
11472 type = void_type_node;
11473 break;
11474
11475 case sfk_destructor:
11476 if (type)
11477 error_at (smallest_type_location (type_quals, locations),
11478 "return type specification for destructor invalid");
11479 else if (type_quals != TYPE_UNQUALIFIED)
11480 error_at (smallest_type_quals_location (type_quals, locations),
11481 "qualifiers are not allowed on destructor declaration");
11482
11483 /* We can't use the proper return type here because we run into
11484 problems with ambiguous bases and covariant returns. */
11485 if (targetm.cxx.cdtor_returns_this ())
11486 type = build_pointer_type (void_type_node);
11487 else
11488 type = void_type_node;
11489 break;
11490
11491 case sfk_conversion:
11492 if (type)
11493 error_at (smallest_type_location (type_quals, locations),
11494 "return type specified for %<operator %T%>", optype);
11495 else if (type_quals != TYPE_UNQUALIFIED)
11496 error_at (smallest_type_quals_location (type_quals, locations),
11497 "qualifiers are not allowed on declaration of "
11498 "%<operator %T%>", optype);
11499
11500 type = optype;
11501 break;
11502
11503 case sfk_deduction_guide:
11504 if (type)
11505 error_at (smallest_type_location (type_quals, locations),
11506 "return type specified for deduction guide");
11507 else if (type_quals != TYPE_UNQUALIFIED)
11508 error_at (smallest_type_quals_location (type_quals, locations),
11509 "qualifiers are not allowed on declaration of "
11510 "deduction guide");
11511 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
11512 {
11513 error ("template template parameter %qT in declaration of "
11514 "deduction guide", optype);
11515 type = error_mark_node;
11516 }
11517 else
11518 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
11519 for (int i = 0; i < ds_last; ++i)
11520 if (i != ds_explicit && locations[i])
11521 error_at (locations[i],
11522 "%<decl-specifier%> in declaration of deduction guide");
11523 break;
11524
11525 default:
11526 gcc_unreachable ();
11527 }
11528
11529 return type;
11530 }
11531
11532 /* A variable or data member (whose unqualified name is IDENTIFIER)
11533 has been declared with the indicated TYPE. If the TYPE is not
11534 acceptable, issue an error message and return a type to use for
11535 error-recovery purposes. */
11536
11537 tree
check_var_type(tree identifier,tree type,location_t loc)11538 check_var_type (tree identifier, tree type, location_t loc)
11539 {
11540 if (VOID_TYPE_P (type))
11541 {
11542 if (!identifier)
11543 error_at (loc, "unnamed variable or field declared void");
11544 else if (identifier_p (identifier))
11545 {
11546 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
11547 error_at (loc, "variable or field %qE declared void",
11548 identifier);
11549 }
11550 else
11551 error_at (loc, "variable or field declared void");
11552 type = error_mark_node;
11553 }
11554
11555 return type;
11556 }
11557
11558 /* Handle declaring DECL as an inline variable. */
11559
11560 static void
mark_inline_variable(tree decl,location_t loc)11561 mark_inline_variable (tree decl, location_t loc)
11562 {
11563 bool inlinep = true;
11564 if (! toplevel_bindings_p ())
11565 {
11566 error_at (loc, "%<inline%> specifier invalid for variable "
11567 "%qD declared at block scope", decl);
11568 inlinep = false;
11569 }
11570 else if (cxx_dialect < cxx17)
11571 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available "
11572 "with %<-std=c++17%> or %<-std=gnu++17%>");
11573 if (inlinep)
11574 {
11575 retrofit_lang_decl (decl);
11576 SET_DECL_VAR_DECLARED_INLINE_P (decl);
11577 }
11578 }
11579
11580
11581 /* Assign a typedef-given name to a class or enumeration type declared
11582 as anonymous at first. This was split out of grokdeclarator
11583 because it is also used in libcc1. */
11584
11585 void
name_unnamed_type(tree type,tree decl)11586 name_unnamed_type (tree type, tree decl)
11587 {
11588 gcc_assert (TYPE_UNNAMED_P (type));
11589
11590 /* Replace the anonymous decl with the real decl. Be careful not to
11591 rename other typedefs (such as the self-reference) of type. */
11592 tree orig = TYPE_NAME (type);
11593 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11594 if (TYPE_NAME (t) == orig)
11595 TYPE_NAME (t) = decl;
11596
11597 /* If this is a typedef within a template class, the nested
11598 type is a (non-primary) template. The name for the
11599 template needs updating as well. */
11600 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11601 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl);
11602
11603 /* Adjust linkage now that we aren't unnamed anymore. */
11604 reset_type_linkage (type);
11605
11606 /* FIXME remangle member functions; member functions of a
11607 type with external linkage have external linkage. */
11608
11609 /* Check that our job is done, and that it would fail if we
11610 attempted to do it again. */
11611 gcc_assert (!TYPE_UNNAMED_P (type));
11612 }
11613
11614 /* Check that decltype(auto) was well-formed: only plain decltype(auto)
11615 is allowed. TYPE might contain a decltype(auto). Returns true if
11616 there was a problem, false otherwise. */
11617
11618 static bool
check_decltype_auto(location_t loc,tree type)11619 check_decltype_auto (location_t loc, tree type)
11620 {
11621 if (tree a = type_uses_auto (type))
11622 {
11623 if (AUTO_IS_DECLTYPE (a))
11624 {
11625 if (a != type)
11626 {
11627 error_at (loc, "%qT as type rather than plain "
11628 "%<decltype(auto)%>", type);
11629 return true;
11630 }
11631 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
11632 {
11633 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified");
11634 return true;
11635 }
11636 }
11637 }
11638 return false;
11639 }
11640
11641 /* Given declspecs and a declarator (abstract or otherwise), determine
11642 the name and type of the object declared and construct a DECL node
11643 for it.
11644
11645 DECLSPECS points to the representation of declaration-specifier
11646 sequence that precedes declarator.
11647
11648 DECL_CONTEXT says which syntactic context this declaration is in:
11649 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
11650 FUNCDEF for a function definition. Like NORMAL but a few different
11651 error messages in each case. Return value may be zero meaning
11652 this definition is too screwy to try to parse.
11653 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
11654 handle member functions (which have FIELD context).
11655 Return value may be zero meaning this definition is too screwy to
11656 try to parse.
11657 PARM for a parameter declaration (either within a function prototype
11658 or before a function body). Make a PARM_DECL, or return void_type_node.
11659 TPARM for a template parameter declaration.
11660 CATCHPARM for a parameter declaration before a catch clause.
11661 TYPENAME if for a typename (in a cast or sizeof).
11662 Don't make a DECL node; just return the ..._TYPE node.
11663 FIELD for a struct or union field; make a FIELD_DECL.
11664 BITFIELD for a field with specified width.
11665
11666 INITIALIZED is as for start_decl.
11667
11668 ATTRLIST is a pointer to the list of attributes, which may be NULL
11669 if there are none; *ATTRLIST may be modified if attributes from inside
11670 the declarator should be applied to the declaration.
11671
11672 When this function is called, scoping variables (such as
11673 CURRENT_CLASS_TYPE) should reflect the scope in which the
11674 declaration occurs, not the scope in which the new declaration will
11675 be placed. For example, on:
11676
11677 void S::f() { ... }
11678
11679 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
11680 should not be `S'.
11681
11682 Returns a DECL (if a declarator is present), a TYPE (if there is no
11683 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
11684 error occurs. */
11685
11686 tree
grokdeclarator(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,enum decl_context decl_context,int initialized,tree * attrlist)11687 grokdeclarator (const cp_declarator *declarator,
11688 cp_decl_specifier_seq *declspecs,
11689 enum decl_context decl_context,
11690 int initialized,
11691 tree* attrlist)
11692 {
11693 tree type = NULL_TREE;
11694 int longlong = 0;
11695 int explicit_intN = 0;
11696 int int_n_alt = 0;
11697 int virtualp, explicitp, friendp, inlinep, staticp;
11698 int explicit_int = 0;
11699 int explicit_char = 0;
11700 int defaulted_int = 0;
11701
11702 tree typedef_decl = NULL_TREE;
11703 const char *name = NULL;
11704 tree typedef_type = NULL_TREE;
11705 /* True if this declarator is a function definition. */
11706 bool funcdef_flag = false;
11707 cp_declarator_kind innermost_code = cdk_error;
11708 int bitfield = 0;
11709 #if 0
11710 /* See the code below that used this. */
11711 tree decl_attr = NULL_TREE;
11712 #endif
11713
11714 /* Keep track of what sort of function is being processed
11715 so that we can warn about default return values, or explicit
11716 return values which do not match prescribed defaults. */
11717 special_function_kind sfk = sfk_none;
11718
11719 tree dname = NULL_TREE;
11720 tree ctor_return_type = NULL_TREE;
11721 enum overload_flags flags = NO_SPECIAL;
11722 /* cv-qualifiers that apply to the declarator, for a declaration of
11723 a member function. */
11724 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
11725 /* virt-specifiers that apply to the declarator, for a declaration of
11726 a member function. */
11727 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
11728 /* ref-qualifier that applies to the declarator, for a declaration of
11729 a member function. */
11730 cp_ref_qualifier rqual = REF_QUAL_NONE;
11731 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
11732 int type_quals = get_type_quals (declspecs);
11733 tree raises = NULL_TREE;
11734 int template_count = 0;
11735 tree returned_attrs = NULL_TREE;
11736 tree parms = NULL_TREE;
11737 const cp_declarator *id_declarator;
11738 /* The unqualified name of the declarator; either an
11739 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
11740 tree unqualified_id;
11741 /* The class type, if any, in which this entity is located,
11742 or NULL_TREE if none. Note that this value may be different from
11743 the current class type; for example if an attempt is made to declare
11744 "A::f" inside "B", this value will be "A". */
11745 tree ctype = current_class_type;
11746 /* The NAMESPACE_DECL for the namespace in which this entity is
11747 located. If an unqualified name is used to declare the entity,
11748 this value will be NULL_TREE, even if the entity is located at
11749 namespace scope. */
11750 tree in_namespace = NULL_TREE;
11751 cp_storage_class storage_class;
11752 bool unsigned_p, signed_p, short_p, long_p, thread_p;
11753 bool type_was_error_mark_node = false;
11754 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
11755 bool template_type_arg = false;
11756 bool template_parm_flag = false;
11757 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
11758 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
11759 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
11760 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
11761 bool late_return_type_p = false;
11762 bool array_parameter_p = false;
11763 tree reqs = NULL_TREE;
11764
11765 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
11766 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
11767 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
11768 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
11769 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
11770 explicit_intN = declspecs->explicit_intN_p;
11771 int_n_alt = declspecs->int_n_alt;
11772 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
11773
11774 // Was concept_p specified? Note that ds_concept
11775 // implies ds_constexpr!
11776 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
11777 if (concept_p)
11778 constexpr_p = true;
11779
11780 if (decl_context == FUNCDEF)
11781 funcdef_flag = true, decl_context = NORMAL;
11782 else if (decl_context == MEMFUNCDEF)
11783 funcdef_flag = true, decl_context = FIELD;
11784 else if (decl_context == BITFIELD)
11785 bitfield = 1, decl_context = FIELD;
11786 else if (decl_context == TEMPLATE_TYPE_ARG)
11787 template_type_arg = true, decl_context = TYPENAME;
11788 else if (decl_context == TPARM)
11789 template_parm_flag = true, decl_context = PARM;
11790
11791 if (initialized == SD_DEFAULTED || initialized == SD_DELETED)
11792 funcdef_flag = true;
11793
11794 location_t typespec_loc = loc_or_input_loc (smallest_type_location
11795 (type_quals,
11796 declspecs->locations));
11797 location_t id_loc;
11798 location_t init_loc;
11799 if (declarator)
11800 {
11801 id_loc = loc_or_input_loc (declarator->id_loc);
11802 init_loc = loc_or_input_loc (declarator->init_loc);
11803 }
11804 else
11805 init_loc = id_loc = input_location;
11806
11807 /* Look inside a declarator for the name being declared
11808 and get it as a string, for an error message. */
11809 for (id_declarator = declarator;
11810 id_declarator;
11811 id_declarator = id_declarator->declarator)
11812 {
11813 if (id_declarator->kind != cdk_id)
11814 innermost_code = id_declarator->kind;
11815
11816 switch (id_declarator->kind)
11817 {
11818 case cdk_function:
11819 if (id_declarator->declarator
11820 && id_declarator->declarator->kind == cdk_id)
11821 {
11822 sfk = id_declarator->declarator->u.id.sfk;
11823 if (sfk == sfk_destructor)
11824 flags = DTOR_FLAG;
11825 }
11826 break;
11827
11828 case cdk_id:
11829 {
11830 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11831 tree decl = id_declarator->u.id.unqualified_name;
11832 if (!decl)
11833 break;
11834 if (qualifying_scope)
11835 {
11836 if (check_for_bare_parameter_packs (qualifying_scope,
11837 id_declarator->id_loc))
11838 return error_mark_node;
11839 if (at_function_scope_p ())
11840 {
11841 /* [dcl.meaning]
11842
11843 A declarator-id shall not be qualified except
11844 for ...
11845
11846 None of the cases are permitted in block
11847 scope. */
11848 if (qualifying_scope == global_namespace)
11849 error ("invalid use of qualified-name %<::%D%>",
11850 decl);
11851 else if (TYPE_P (qualifying_scope))
11852 error ("invalid use of qualified-name %<%T::%D%>",
11853 qualifying_scope, decl);
11854 else
11855 error ("invalid use of qualified-name %<%D::%D%>",
11856 qualifying_scope, decl);
11857 return error_mark_node;
11858 }
11859 else if (TYPE_P (qualifying_scope))
11860 {
11861 ctype = qualifying_scope;
11862 if (!MAYBE_CLASS_TYPE_P (ctype))
11863 {
11864 error_at (id_declarator->id_loc,
11865 "%q#T is not a class or namespace", ctype);
11866 ctype = NULL_TREE;
11867 }
11868 else if (innermost_code != cdk_function
11869 && current_class_type
11870 && !uniquely_derived_from_p (ctype,
11871 current_class_type))
11872 {
11873 error_at (id_declarator->id_loc,
11874 "invalid use of qualified-name %<%T::%D%>",
11875 qualifying_scope, decl);
11876 return error_mark_node;
11877 }
11878 }
11879 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11880 in_namespace = qualifying_scope;
11881 }
11882 switch (TREE_CODE (decl))
11883 {
11884 case BIT_NOT_EXPR:
11885 {
11886 if (innermost_code != cdk_function)
11887 {
11888 error_at (EXPR_LOCATION (decl),
11889 "declaration of %qE as non-function", decl);
11890 return error_mark_node;
11891 }
11892 else if (!qualifying_scope
11893 && !(current_class_type && at_class_scope_p ()))
11894 {
11895 error_at (EXPR_LOCATION (decl),
11896 "declaration of %qE as non-member", decl);
11897 return error_mark_node;
11898 }
11899
11900 tree type = TREE_OPERAND (decl, 0);
11901 if (TYPE_P (type))
11902 type = constructor_name (type);
11903 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11904 dname = decl;
11905 }
11906 break;
11907
11908 case TEMPLATE_ID_EXPR:
11909 {
11910 tree fns = TREE_OPERAND (decl, 0);
11911
11912 dname = fns;
11913 if (!identifier_p (dname))
11914 dname = OVL_NAME (dname);
11915 }
11916 /* Fall through. */
11917
11918 case IDENTIFIER_NODE:
11919 if (identifier_p (decl))
11920 dname = decl;
11921
11922 if (IDENTIFIER_KEYWORD_P (dname))
11923 {
11924 error ("declarator-id missing; using reserved word %qD",
11925 dname);
11926 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11927 }
11928 else if (!IDENTIFIER_CONV_OP_P (dname))
11929 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11930 else
11931 {
11932 gcc_assert (flags == NO_SPECIAL);
11933 flags = TYPENAME_FLAG;
11934 sfk = sfk_conversion;
11935 tree glob = get_global_binding (dname);
11936 if (glob && TREE_CODE (glob) == TYPE_DECL)
11937 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11938 else
11939 name = "<invalid operator>";
11940 }
11941 break;
11942
11943 default:
11944 gcc_unreachable ();
11945 }
11946 break;
11947 }
11948
11949 case cdk_array:
11950 case cdk_pointer:
11951 case cdk_reference:
11952 case cdk_ptrmem:
11953 break;
11954
11955 case cdk_decomp:
11956 name = "structured binding";
11957 break;
11958
11959 case cdk_error:
11960 return error_mark_node;
11961
11962 default:
11963 gcc_unreachable ();
11964 }
11965 if (id_declarator->kind == cdk_id)
11966 break;
11967 }
11968
11969 /* [dcl.fct.edf]
11970
11971 The declarator in a function-definition shall have the form
11972 D1 ( parameter-declaration-clause) ... */
11973 if (funcdef_flag && innermost_code != cdk_function)
11974 {
11975 error_at (id_loc, "function definition does not declare parameters");
11976 return error_mark_node;
11977 }
11978
11979 if (flags == TYPENAME_FLAG
11980 && innermost_code != cdk_function
11981 && ! (ctype && !declspecs->any_specifiers_p))
11982 {
11983 error_at (id_loc, "declaration of %qD as non-function", dname);
11984 return error_mark_node;
11985 }
11986
11987 if (dname && identifier_p (dname))
11988 {
11989 if (UDLIT_OPER_P (dname)
11990 && innermost_code != cdk_function)
11991 {
11992 error_at (id_loc, "declaration of %qD as non-function", dname);
11993 return error_mark_node;
11994 }
11995
11996 if (IDENTIFIER_ANY_OP_P (dname))
11997 {
11998 if (typedef_p)
11999 {
12000 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
12001 return error_mark_node;
12002 }
12003 else if (decl_context == PARM || decl_context == CATCHPARM)
12004 {
12005 error_at (id_loc, "declaration of %qD as parameter", dname);
12006 return error_mark_node;
12007 }
12008 }
12009 }
12010
12011 /* Anything declared one level down from the top level
12012 must be one of the parameters of a function
12013 (because the body is at least two levels down). */
12014
12015 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
12016 by not allowing C++ class definitions to specify their parameters
12017 with xdecls (must be spec.d in the parmlist).
12018
12019 Since we now wait to push a class scope until we are sure that
12020 we are in a legitimate method context, we must set oldcname
12021 explicitly (since current_class_name is not yet alive).
12022
12023 We also want to avoid calling this a PARM if it is in a namespace. */
12024
12025 if (decl_context == NORMAL && !toplevel_bindings_p ())
12026 {
12027 cp_binding_level *b = current_binding_level;
12028 current_binding_level = b->level_chain;
12029 if (current_binding_level != 0 && toplevel_bindings_p ())
12030 decl_context = PARM;
12031 current_binding_level = b;
12032 }
12033
12034 if (name == NULL)
12035 name = decl_context == PARM ? "parameter" : "type name";
12036
12037 if (consteval_p && constexpr_p)
12038 {
12039 error_at (declspecs->locations[ds_consteval],
12040 "both %qs and %qs specified", "constexpr", "consteval");
12041 return error_mark_node;
12042 }
12043
12044 if (concept_p && typedef_p)
12045 {
12046 error_at (declspecs->locations[ds_concept],
12047 "%qs cannot appear in a typedef declaration", "concept");
12048 return error_mark_node;
12049 }
12050
12051 if (constexpr_p && typedef_p)
12052 {
12053 error_at (declspecs->locations[ds_constexpr],
12054 "%qs cannot appear in a typedef declaration", "constexpr");
12055 return error_mark_node;
12056 }
12057
12058 if (consteval_p && typedef_p)
12059 {
12060 error_at (declspecs->locations[ds_consteval],
12061 "%qs cannot appear in a typedef declaration", "consteval");
12062 return error_mark_node;
12063 }
12064
12065 if (constinit_p && typedef_p)
12066 {
12067 error_at (declspecs->locations[ds_constinit],
12068 "%qs cannot appear in a typedef declaration", "constinit");
12069 return error_mark_node;
12070 }
12071
12072 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
12073 keywords shall appear in a decl-specifier-seq." */
12074 if (constinit_p && constexpr_p)
12075 {
12076 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
12077 richloc.add_range (declspecs->locations[ds_constexpr]);
12078 error_at (&richloc,
12079 "can use at most one of the %<constinit%> and %<constexpr%> "
12080 "specifiers");
12081 return error_mark_node;
12082 }
12083
12084 /* If there were multiple types specified in the decl-specifier-seq,
12085 issue an error message. */
12086 if (declspecs->multiple_types_p)
12087 {
12088 error_at (typespec_loc,
12089 "two or more data types in declaration of %qs", name);
12090 return error_mark_node;
12091 }
12092
12093 if (declspecs->conflicting_specifiers_p)
12094 {
12095 error_at (min_location (declspecs->locations[ds_typedef],
12096 declspecs->locations[ds_storage_class]),
12097 "conflicting specifiers in declaration of %qs", name);
12098 return error_mark_node;
12099 }
12100
12101 /* Extract the basic type from the decl-specifier-seq. */
12102 type = declspecs->type;
12103 if (type == error_mark_node)
12104 {
12105 type = NULL_TREE;
12106 type_was_error_mark_node = true;
12107 }
12108
12109 /* Ignore erroneous attributes. */
12110 if (attrlist && *attrlist == error_mark_node)
12111 *attrlist = NULL_TREE;
12112
12113 /* An object declared as __attribute__((unavailable)) suppresses
12114 any reports of being declared with unavailable or deprecated
12115 items. An object declared as __attribute__((deprecated))
12116 suppresses warnings of uses of other deprecated items. */
12117 auto ds = make_temp_override (deprecated_state);
12118 if (attrlist && lookup_attribute ("unavailable", *attrlist))
12119 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
12120 else if (attrlist && lookup_attribute ("deprecated", *attrlist))
12121 deprecated_state = DEPRECATED_SUPPRESS;
12122
12123 cp_handle_deprecated_or_unavailable (type);
12124 if (type && TREE_CODE (type) == TYPE_DECL)
12125 {
12126 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
12127 typedef_decl = type;
12128 type = TREE_TYPE (typedef_decl);
12129 if (DECL_ARTIFICIAL (typedef_decl))
12130 cp_handle_deprecated_or_unavailable (type);
12131 }
12132 /* No type at all: default to `int', and set DEFAULTED_INT
12133 because it was not a user-defined typedef. */
12134 if (type == NULL_TREE)
12135 {
12136 if (signed_p || unsigned_p || long_p || short_p)
12137 {
12138 /* These imply 'int'. */
12139 type = integer_type_node;
12140 defaulted_int = 1;
12141 }
12142 /* If we just have "complex", it is equivalent to "complex double". */
12143 else if (!longlong && !explicit_intN
12144 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
12145 {
12146 type = double_type_node;
12147 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
12148 "ISO C++ does not support plain %<complex%> meaning "
12149 "%<double complex%>");
12150 }
12151 }
12152 /* Gather flags. */
12153 explicit_int = declspecs->explicit_int_p;
12154 explicit_char = declspecs->explicit_char_p;
12155
12156 #if 0
12157 /* See the code below that used this. */
12158 if (typedef_decl)
12159 decl_attr = DECL_ATTRIBUTES (typedef_decl);
12160 #endif
12161 typedef_type = type;
12162
12163 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
12164 ctor_return_type = TREE_TYPE (dname);
12165 else
12166 ctor_return_type = ctype;
12167
12168 if (sfk != sfk_none)
12169 {
12170 type = check_special_function_return_type (sfk, type,
12171 ctor_return_type,
12172 type_quals,
12173 declspecs->locations);
12174 type_quals = TYPE_UNQUALIFIED;
12175 }
12176 else if (type == NULL_TREE)
12177 {
12178 int is_main;
12179
12180 explicit_int = -1;
12181
12182 /* We handle `main' specially here, because 'main () { }' is so
12183 common. With no options, it is allowed. With -Wreturn-type,
12184 it is a warning. It is only an error with -pedantic-errors. */
12185 is_main = (funcdef_flag
12186 && dname && identifier_p (dname)
12187 && MAIN_NAME_P (dname)
12188 && ctype == NULL_TREE
12189 && in_namespace == NULL_TREE
12190 && current_namespace == global_namespace);
12191
12192 if (type_was_error_mark_node)
12193 /* We've already issued an error, don't complain more. */;
12194 else if (in_system_header_at (id_loc) || flag_ms_extensions)
12195 /* Allow it, sigh. */;
12196 else if (! is_main)
12197 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
12198 name);
12199 else if (pedantic)
12200 pedwarn (id_loc, OPT_Wpedantic,
12201 "ISO C++ forbids declaration of %qs with no type", name);
12202 else
12203 warning_at (id_loc, OPT_Wreturn_type,
12204 "ISO C++ forbids declaration of %qs with no type", name);
12205
12206 if (type_was_error_mark_node && template_parm_flag)
12207 /* FIXME we should be able to propagate the error_mark_node as is
12208 for other contexts too. */
12209 type = error_mark_node;
12210 else
12211 type = integer_type_node;
12212 }
12213
12214 ctype = NULL_TREE;
12215
12216 if (explicit_intN)
12217 {
12218 if (! int_n_enabled_p[declspecs->int_n_idx])
12219 {
12220 error_at (declspecs->locations[ds_type_spec],
12221 "%<__int%d%> is not supported by this target",
12222 int_n_data[declspecs->int_n_idx].bitsize);
12223 explicit_intN = false;
12224 }
12225 /* Don't pedwarn if the alternate "__intN__" form has been used instead
12226 of "__intN". */
12227 else if (!int_n_alt && pedantic)
12228 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
12229 "ISO C++ does not support %<__int%d%> for %qs",
12230 int_n_data[declspecs->int_n_idx].bitsize, name);
12231 }
12232
12233 /* Now process the modifiers that were specified
12234 and check for invalid combinations. */
12235
12236 /* Long double is a special combination. */
12237 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
12238 {
12239 long_p = false;
12240 type = cp_build_qualified_type (long_double_type_node,
12241 cp_type_quals (type));
12242 }
12243
12244 /* Check all other uses of type modifiers. */
12245
12246 if (unsigned_p || signed_p || long_p || short_p)
12247 {
12248 location_t loc;
12249 const char *key;
12250 if (unsigned_p)
12251 {
12252 key = "unsigned";
12253 loc = declspecs->locations[ds_unsigned];
12254 }
12255 else if (signed_p)
12256 {
12257 key = "signed";
12258 loc = declspecs->locations[ds_signed];
12259 }
12260 else if (longlong)
12261 {
12262 key = "long long";
12263 loc = declspecs->locations[ds_long_long];
12264 }
12265 else if (long_p)
12266 {
12267 key = "long";
12268 loc = declspecs->locations[ds_long];
12269 }
12270 else /* if (short_p) */
12271 {
12272 key = "short";
12273 loc = declspecs->locations[ds_short];
12274 }
12275
12276 int ok = 0;
12277
12278 if (signed_p && unsigned_p)
12279 {
12280 gcc_rich_location richloc (declspecs->locations[ds_signed]);
12281 richloc.add_range (declspecs->locations[ds_unsigned]);
12282 error_at (&richloc,
12283 "%<signed%> and %<unsigned%> specified together");
12284 }
12285 else if (long_p && short_p)
12286 {
12287 gcc_rich_location richloc (declspecs->locations[ds_long]);
12288 richloc.add_range (declspecs->locations[ds_short]);
12289 error_at (&richloc, "%<long%> and %<short%> specified together");
12290 }
12291 else if (TREE_CODE (type) != INTEGER_TYPE
12292 || type == char8_type_node
12293 || type == char16_type_node
12294 || type == char32_type_node
12295 || ((long_p || short_p)
12296 && (explicit_char || explicit_intN)))
12297 error_at (loc, "%qs specified with %qT", key, type);
12298 else if (!explicit_int && !defaulted_int
12299 && !explicit_char && !explicit_intN)
12300 {
12301 if (typedef_decl)
12302 {
12303 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
12304 key, type);
12305 ok = !flag_pedantic_errors;
12306 type = DECL_ORIGINAL_TYPE (typedef_decl);
12307 typedef_decl = NULL_TREE;
12308 }
12309 else if (declspecs->decltype_p)
12310 error_at (loc, "%qs specified with %<decltype%>", key);
12311 else
12312 error_at (loc, "%qs specified with %<typeof%>", key);
12313 }
12314 else
12315 ok = 1;
12316
12317 /* Discard the type modifiers if they are invalid. */
12318 if (! ok)
12319 {
12320 unsigned_p = false;
12321 signed_p = false;
12322 long_p = false;
12323 short_p = false;
12324 longlong = 0;
12325 }
12326 }
12327
12328 /* Decide whether an integer type is signed or not.
12329 Optionally treat bitfields as signed by default. */
12330 if (unsigned_p
12331 /* [class.bit]
12332
12333 It is implementation-defined whether a plain (neither
12334 explicitly signed or unsigned) char, short, int, or long
12335 bit-field is signed or unsigned.
12336
12337 Naturally, we extend this to long long as well. Note that
12338 this does not include wchar_t. */
12339 || (bitfield && !flag_signed_bitfields
12340 && !signed_p
12341 /* A typedef for plain `int' without `signed' can be
12342 controlled just like plain `int', but a typedef for
12343 `signed int' cannot be so controlled. */
12344 && !(typedef_decl
12345 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
12346 && TREE_CODE (type) == INTEGER_TYPE
12347 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
12348 {
12349 if (explicit_intN)
12350 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
12351 else if (longlong)
12352 type = long_long_unsigned_type_node;
12353 else if (long_p)
12354 type = long_unsigned_type_node;
12355 else if (short_p)
12356 type = short_unsigned_type_node;
12357 else if (type == char_type_node)
12358 type = unsigned_char_type_node;
12359 else if (typedef_decl)
12360 type = unsigned_type_for (type);
12361 else
12362 type = unsigned_type_node;
12363 }
12364 else if (signed_p && type == char_type_node)
12365 type = signed_char_type_node;
12366 else if (explicit_intN)
12367 type = int_n_trees[declspecs->int_n_idx].signed_type;
12368 else if (longlong)
12369 type = long_long_integer_type_node;
12370 else if (long_p)
12371 type = long_integer_type_node;
12372 else if (short_p)
12373 type = short_integer_type_node;
12374
12375 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
12376 {
12377 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
12378 error_at (declspecs->locations[ds_complex],
12379 "complex invalid for %qs", name);
12380 /* If a modifier is specified, the resulting complex is the complex
12381 form of TYPE. E.g, "complex short" is "complex short int". */
12382 else if (type == integer_type_node)
12383 type = complex_integer_type_node;
12384 else if (type == float_type_node)
12385 type = complex_float_type_node;
12386 else if (type == double_type_node)
12387 type = complex_double_type_node;
12388 else if (type == long_double_type_node)
12389 type = complex_long_double_type_node;
12390 else
12391 type = build_complex_type (type);
12392 }
12393
12394 /* If we're using the injected-class-name to form a compound type or a
12395 declaration, replace it with the underlying class so we don't get
12396 redundant typedefs in the debug output. But if we are returning the
12397 type unchanged, leave it alone so that it's available to
12398 maybe_get_template_decl_from_type_decl. */
12399 if (CLASS_TYPE_P (type)
12400 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
12401 && type == TREE_TYPE (TYPE_NAME (type))
12402 && (declarator || type_quals))
12403 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
12404
12405 type_quals |= cp_type_quals (type);
12406 type = cp_build_qualified_type_real
12407 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
12408 || declspecs->decltype_p)
12409 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
12410 /* We might have ignored or rejected some of the qualifiers. */
12411 type_quals = cp_type_quals (type);
12412
12413 if (cxx_dialect >= cxx17 && type && is_auto (type)
12414 && innermost_code != cdk_function
12415 && id_declarator && declarator != id_declarator)
12416 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
12417 {
12418 error_at (typespec_loc, "template placeholder type %qT must be followed "
12419 "by a simple declarator-id", type);
12420 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
12421 type = error_mark_node;
12422 }
12423
12424 staticp = 0;
12425 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
12426 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
12427 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
12428
12429 storage_class = declspecs->storage_class;
12430 if (storage_class == sc_static)
12431 staticp = 1 + (decl_context == FIELD);
12432 else if (decl_context == FIELD && sfk == sfk_deduction_guide)
12433 /* Treat class-scope deduction guides as static member functions
12434 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */
12435 staticp = 2;
12436
12437 if (virtualp)
12438 {
12439 if (staticp == 2)
12440 {
12441 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12442 richloc.add_range (declspecs->locations[ds_storage_class]);
12443 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
12444 "and %<static%>", dname);
12445 storage_class = sc_none;
12446 staticp = 0;
12447 }
12448 if (constexpr_p && pedantic && cxx_dialect < cxx20)
12449 {
12450 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12451 richloc.add_range (declspecs->locations[ds_constexpr]);
12452 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be "
12453 "declared both %<virtual%> and %<constexpr%> only in "
12454 "%<-std=c++20%> or %<-std=gnu++20%>", dname);
12455 }
12456 }
12457 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
12458
12459 /* Issue errors about use of storage classes for parameters. */
12460 if (decl_context == PARM)
12461 {
12462 if (typedef_p)
12463 {
12464 error_at (declspecs->locations[ds_typedef],
12465 "typedef declaration invalid in parameter declaration");
12466 return error_mark_node;
12467 }
12468 else if (template_parm_flag && storage_class != sc_none)
12469 {
12470 error_at (min_location (declspecs->locations[ds_thread],
12471 declspecs->locations[ds_storage_class]),
12472 "storage class specified for template parameter %qs",
12473 name);
12474 return error_mark_node;
12475 }
12476 else if (storage_class == sc_static
12477 || storage_class == sc_extern
12478 || thread_p)
12479 {
12480 error_at (min_location (declspecs->locations[ds_thread],
12481 declspecs->locations[ds_storage_class]),
12482 "storage class specified for parameter %qs", name);
12483 return error_mark_node;
12484 }
12485
12486 /* Function parameters cannot be concept. */
12487 if (concept_p)
12488 {
12489 error_at (declspecs->locations[ds_concept],
12490 "a parameter cannot be declared %qs", "concept");
12491 concept_p = 0;
12492 constexpr_p = 0;
12493 }
12494 /* Function parameters cannot be constexpr. If we saw one, moan
12495 and pretend it wasn't there. */
12496 else if (constexpr_p)
12497 {
12498 error_at (declspecs->locations[ds_constexpr],
12499 "a parameter cannot be declared %qs", "constexpr");
12500 constexpr_p = 0;
12501 }
12502 if (constinit_p)
12503 {
12504 error_at (declspecs->locations[ds_constinit],
12505 "a parameter cannot be declared %qs", "constinit");
12506 constinit_p = 0;
12507 }
12508 if (consteval_p)
12509 {
12510 error_at (declspecs->locations[ds_consteval],
12511 "a parameter cannot be declared %qs", "consteval");
12512 consteval_p = 0;
12513 }
12514 }
12515
12516 /* Give error if `virtual' is used outside of class declaration. */
12517 if (virtualp
12518 && (current_class_name == NULL_TREE || decl_context != FIELD))
12519 {
12520 error_at (declspecs->locations[ds_virtual],
12521 "%<virtual%> outside class declaration");
12522 virtualp = 0;
12523 }
12524
12525 if (innermost_code == cdk_decomp)
12526 {
12527 location_t loc = (declarator->kind == cdk_reference
12528 ? declarator->declarator->id_loc : declarator->id_loc);
12529 if (inlinep)
12530 error_at (declspecs->locations[ds_inline],
12531 "structured binding declaration cannot be %qs", "inline");
12532 if (typedef_p)
12533 error_at (declspecs->locations[ds_typedef],
12534 "structured binding declaration cannot be %qs", "typedef");
12535 if (constexpr_p && !concept_p)
12536 error_at (declspecs->locations[ds_constexpr], "structured "
12537 "binding declaration cannot be %qs", "constexpr");
12538 if (consteval_p)
12539 error_at (declspecs->locations[ds_consteval], "structured "
12540 "binding declaration cannot be %qs", "consteval");
12541 if (thread_p && cxx_dialect < cxx20)
12542 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions,
12543 "structured binding declaration can be %qs only in "
12544 "%<-std=c++20%> or %<-std=gnu++20%>",
12545 declspecs->gnu_thread_keyword_p
12546 ? "__thread" : "thread_local");
12547 if (concept_p)
12548 error_at (declspecs->locations[ds_concept],
12549 "structured binding declaration cannot be %qs", "concept");
12550 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
12551 if (type_quals & TYPE_QUAL_VOLATILE)
12552 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
12553 "%<volatile%>-qualified structured binding is deprecated");
12554 switch (storage_class)
12555 {
12556 case sc_none:
12557 break;
12558 case sc_register:
12559 error_at (loc, "structured binding declaration cannot be %qs",
12560 "register");
12561 break;
12562 case sc_static:
12563 if (cxx_dialect < cxx20)
12564 pedwarn (loc, OPT_Wc__20_extensions,
12565 "structured binding declaration can be %qs only in "
12566 "%<-std=c++20%> or %<-std=gnu++20%>", "static");
12567 break;
12568 case sc_extern:
12569 error_at (loc, "structured binding declaration cannot be %qs",
12570 "extern");
12571 break;
12572 case sc_mutable:
12573 error_at (loc, "structured binding declaration cannot be %qs",
12574 "mutable");
12575 break;
12576 case sc_auto:
12577 error_at (loc, "structured binding declaration cannot be "
12578 "C++98 %<auto%>");
12579 break;
12580 default:
12581 gcc_unreachable ();
12582 }
12583 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
12584 || TYPE_IDENTIFIER (type) != auto_identifier)
12585 {
12586 if (type != error_mark_node)
12587 {
12588 error_at (loc, "structured binding declaration cannot have "
12589 "type %qT", type);
12590 inform (loc,
12591 "type must be cv-qualified %<auto%> or reference to "
12592 "cv-qualified %<auto%>");
12593 }
12594 type = build_qualified_type (make_auto (), type_quals);
12595 declspecs->type = type;
12596 }
12597 inlinep = 0;
12598 typedef_p = 0;
12599 constexpr_p = 0;
12600 consteval_p = 0;
12601 concept_p = 0;
12602 if (storage_class != sc_static)
12603 {
12604 storage_class = sc_none;
12605 declspecs->storage_class = sc_none;
12606 }
12607 }
12608
12609 /* Static anonymous unions are dealt with here. */
12610 if (staticp && decl_context == TYPENAME
12611 && declspecs->type
12612 && ANON_AGGR_TYPE_P (declspecs->type))
12613 decl_context = FIELD;
12614
12615 /* Warn about storage classes that are invalid for certain
12616 kinds of declarations (parameters, typenames, etc.). */
12617 if (thread_p
12618 && ((storage_class
12619 && storage_class != sc_extern
12620 && storage_class != sc_static)
12621 || typedef_p))
12622 {
12623 location_t loc
12624 = min_location (declspecs->locations[ds_thread],
12625 declspecs->locations[ds_storage_class]);
12626 error_at (loc, "multiple storage classes in declaration of %qs", name);
12627 thread_p = false;
12628 }
12629 if (decl_context != NORMAL
12630 && ((storage_class != sc_none
12631 && storage_class != sc_mutable)
12632 || thread_p))
12633 {
12634 if ((decl_context == PARM || decl_context == CATCHPARM)
12635 && (storage_class == sc_register
12636 || storage_class == sc_auto))
12637 ;
12638 else if (typedef_p)
12639 ;
12640 else if (decl_context == FIELD
12641 /* C++ allows static class elements. */
12642 && storage_class == sc_static)
12643 /* C++ also allows inlines and signed and unsigned elements,
12644 but in those cases we don't come in here. */
12645 ;
12646 else
12647 {
12648 location_t loc
12649 = min_location (declspecs->locations[ds_thread],
12650 declspecs->locations[ds_storage_class]);
12651 if (decl_context == FIELD)
12652 error_at (loc, "storage class specified for %qs", name);
12653 else if (decl_context == PARM || decl_context == CATCHPARM)
12654 error_at (loc, "storage class specified for parameter %qs", name);
12655 else
12656 error_at (loc, "storage class specified for typename");
12657 if (storage_class == sc_register
12658 || storage_class == sc_auto
12659 || storage_class == sc_extern
12660 || thread_p)
12661 storage_class = sc_none;
12662 }
12663 }
12664 else if (storage_class == sc_extern && funcdef_flag
12665 && ! toplevel_bindings_p ())
12666 error ("nested function %qs declared %<extern%>", name);
12667 else if (toplevel_bindings_p ())
12668 {
12669 if (storage_class == sc_auto)
12670 error_at (declspecs->locations[ds_storage_class],
12671 "top-level declaration of %qs specifies %<auto%>", name);
12672 }
12673 else if (thread_p
12674 && storage_class != sc_extern
12675 && storage_class != sc_static)
12676 {
12677 if (declspecs->gnu_thread_keyword_p)
12678 pedwarn (declspecs->locations[ds_thread],
12679 0, "function-scope %qs implicitly auto and "
12680 "declared %<__thread%>", name);
12681
12682 /* When thread_local is applied to a variable of block scope the
12683 storage-class-specifier static is implied if it does not appear
12684 explicitly. */
12685 storage_class = declspecs->storage_class = sc_static;
12686 staticp = 1;
12687 }
12688
12689 if (storage_class && friendp)
12690 {
12691 error_at (min_location (declspecs->locations[ds_thread],
12692 declspecs->locations[ds_storage_class]),
12693 "storage class specifiers invalid in friend function "
12694 "declarations");
12695 storage_class = sc_none;
12696 staticp = 0;
12697 }
12698
12699 if (!id_declarator)
12700 unqualified_id = NULL_TREE;
12701 else
12702 {
12703 unqualified_id = id_declarator->u.id.unqualified_name;
12704 switch (TREE_CODE (unqualified_id))
12705 {
12706 case BIT_NOT_EXPR:
12707 unqualified_id = TREE_OPERAND (unqualified_id, 0);
12708 if (TYPE_P (unqualified_id))
12709 unqualified_id = constructor_name (unqualified_id);
12710 break;
12711
12712 case IDENTIFIER_NODE:
12713 case TEMPLATE_ID_EXPR:
12714 break;
12715
12716 default:
12717 gcc_unreachable ();
12718 }
12719 }
12720
12721 if (declspecs->std_attributes)
12722 {
12723 location_t attr_loc = declspecs->locations[ds_std_attribute];
12724 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
12725 inform (attr_loc, "an attribute that appertains to a type-specifier "
12726 "is ignored");
12727 }
12728
12729 /* Determine the type of the entity declared by recurring on the
12730 declarator. */
12731 for (; declarator; declarator = declarator->declarator)
12732 {
12733 const cp_declarator *inner_declarator;
12734 tree attrs;
12735
12736 if (type == error_mark_node)
12737 return error_mark_node;
12738
12739 attrs = declarator->attributes;
12740 if (attrs)
12741 {
12742 int attr_flags;
12743
12744 attr_flags = 0;
12745 if (declarator->kind == cdk_id)
12746 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
12747 if (declarator->kind == cdk_function)
12748 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
12749 if (declarator->kind == cdk_array)
12750 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
12751 tree late_attrs = NULL_TREE;
12752 if (decl_context != PARM && decl_context != TYPENAME)
12753 /* Assume that any attributes that get applied late to
12754 templates will DTRT when applied to the declaration
12755 as a whole. */
12756 late_attrs = splice_template_attributes (&attrs, type);
12757 returned_attrs = decl_attributes (&type,
12758 chainon (returned_attrs, attrs),
12759 attr_flags);
12760 returned_attrs = chainon (late_attrs, returned_attrs);
12761 }
12762
12763 inner_declarator = declarator->declarator;
12764
12765 /* We don't want to warn in parameter context because we don't
12766 yet know if the parse will succeed, and this might turn out
12767 to be a constructor call. */
12768 if (decl_context != PARM
12769 && decl_context != TYPENAME
12770 && !typedef_p
12771 && declarator->parenthesized != UNKNOWN_LOCATION
12772 /* If the type is class-like and the inner name used a
12773 global namespace qualifier, we need the parens.
12774 Unfortunately all we can tell is whether a qualified name
12775 was used or not. */
12776 && !(inner_declarator
12777 && inner_declarator->kind == cdk_id
12778 && inner_declarator->u.id.qualifying_scope
12779 && (MAYBE_CLASS_TYPE_P (type)
12780 || TREE_CODE (type) == ENUMERAL_TYPE)))
12781 {
12782 if (warning_at (declarator->parenthesized, OPT_Wparentheses,
12783 "unnecessary parentheses in declaration of %qs",
12784 name))
12785 {
12786 gcc_rich_location iloc (declarator->parenthesized);
12787 iloc.add_fixit_remove (get_start (declarator->parenthesized));
12788 iloc.add_fixit_remove (get_finish (declarator->parenthesized));
12789 inform (&iloc, "remove parentheses");
12790 }
12791 }
12792 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
12793 break;
12794
12795 switch (declarator->kind)
12796 {
12797 case cdk_array:
12798 type = create_array_type_for_decl (dname, type,
12799 declarator->u.array.bounds,
12800 declarator->id_loc);
12801 if (!valid_array_size_p (dname
12802 ? declarator->id_loc : input_location,
12803 type, dname))
12804 type = error_mark_node;
12805
12806 if (declarator->std_attributes)
12807 /* [dcl.array]/1:
12808
12809 The optional attribute-specifier-seq appertains to the
12810 array. */
12811 returned_attrs = chainon (returned_attrs,
12812 declarator->std_attributes);
12813 break;
12814
12815 case cdk_function:
12816 {
12817 tree arg_types;
12818 int funcdecl_p;
12819
12820 /* Declaring a function type. */
12821
12822 /* Pick up type qualifiers which should be applied to `this'. */
12823 memfn_quals = declarator->u.function.qualifiers;
12824 /* Pick up virt-specifiers. */
12825 virt_specifiers = declarator->u.function.virt_specifiers;
12826 /* And ref-qualifier, too */
12827 rqual = declarator->u.function.ref_qualifier;
12828 /* And tx-qualifier. */
12829 tree tx_qual = declarator->u.function.tx_qualifier;
12830 /* Pick up the exception specifications. */
12831 raises = declarator->u.function.exception_specification;
12832 /* If the exception-specification is ill-formed, let's pretend
12833 there wasn't one. */
12834 if (raises == error_mark_node)
12835 raises = NULL_TREE;
12836
12837 if (reqs)
12838 error_at (location_of (reqs), "requires-clause on return type");
12839 reqs = declarator->u.function.requires_clause;
12840
12841 /* Say it's a definition only for the CALL_EXPR
12842 closest to the identifier. */
12843 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
12844
12845 /* Handle a late-specified return type. */
12846 tree late_return_type = declarator->u.function.late_return_type;
12847 if (tree auto_node = type_uses_auto (type))
12848 {
12849 if (!late_return_type)
12850 {
12851 if (!funcdecl_p)
12852 /* auto (*fp)() = f; is OK. */;
12853 else if (current_class_type
12854 && LAMBDA_TYPE_P (current_class_type))
12855 /* OK for C++11 lambdas. */;
12856 else if (cxx_dialect < cxx14)
12857 {
12858 error_at (typespec_loc, "%qs function uses "
12859 "%<auto%> type specifier without "
12860 "trailing return type", name);
12861 inform (typespec_loc,
12862 "deduced return type only available "
12863 "with %<-std=c++14%> or %<-std=gnu++14%>");
12864 }
12865 else if (virtualp)
12866 {
12867 error_at (typespec_loc, "virtual function "
12868 "cannot have deduced return type");
12869 virtualp = false;
12870 }
12871 }
12872 else if (!is_auto (type) && sfk != sfk_conversion)
12873 {
12874 error_at (typespec_loc, "%qs function with trailing "
12875 "return type has %qT as its type rather "
12876 "than plain %<auto%>", name, type);
12877 return error_mark_node;
12878 }
12879 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12880 {
12881 if (funcdecl_p)
12882 error_at (typespec_loc,
12883 "%qs function with trailing return type "
12884 "has %<decltype(auto)%> as its type "
12885 "rather than plain %<auto%>", name);
12886 else
12887 error_at (typespec_loc,
12888 "invalid use of %<decltype(auto)%>");
12889 return error_mark_node;
12890 }
12891 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12892 if (!tmpl)
12893 if (tree late_auto = type_uses_auto (late_return_type))
12894 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12895 if (tmpl)
12896 {
12897 if (!funcdecl_p || !dguide_name_p (unqualified_id))
12898 {
12899 error_at (typespec_loc, "deduced class "
12900 "type %qD in function return type",
12901 DECL_NAME (tmpl));
12902 inform (DECL_SOURCE_LOCATION (tmpl),
12903 "%qD declared here", tmpl);
12904 return error_mark_node;
12905 }
12906 else if (!late_return_type)
12907 {
12908 error_at (declarator->id_loc, "deduction guide "
12909 "for %qT must have trailing return "
12910 "type", TREE_TYPE (tmpl));
12911 inform (DECL_SOURCE_LOCATION (tmpl),
12912 "%qD declared here", tmpl);
12913 return error_mark_node;
12914 }
12915 else if (CLASS_TYPE_P (late_return_type)
12916 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12917 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12918 == tmpl))
12919 /* OK */;
12920 else
12921 error ("trailing return type %qT of deduction guide "
12922 "is not a specialization of %qT",
12923 late_return_type, TREE_TYPE (tmpl));
12924 }
12925 }
12926 else if (late_return_type
12927 && sfk != sfk_conversion)
12928 {
12929 if (late_return_type == error_mark_node)
12930 return error_mark_node;
12931 if (cxx_dialect < cxx11)
12932 /* Not using maybe_warn_cpp0x because this should
12933 always be an error. */
12934 error_at (typespec_loc,
12935 "trailing return type only available "
12936 "with %<-std=c++11%> or %<-std=gnu++11%>");
12937 else
12938 error_at (typespec_loc, "%qs function with trailing "
12939 "return type not declared with %<auto%> "
12940 "type specifier", name);
12941 return error_mark_node;
12942 }
12943 if (late_return_type && sfk == sfk_conversion)
12944 {
12945 error ("a conversion function cannot have a trailing return type");
12946 return error_mark_node;
12947 }
12948 type = splice_late_return_type (type, late_return_type);
12949 if (type == error_mark_node)
12950 return error_mark_node;
12951
12952 if (late_return_type)
12953 {
12954 late_return_type_p = true;
12955 type_quals = cp_type_quals (type);
12956 }
12957
12958 if (type_quals != TYPE_UNQUALIFIED)
12959 {
12960 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12961 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12962 "qualifiers ignored on function return type");
12963 /* [dcl.fct] "A volatile-qualified return type is
12964 deprecated." */
12965 if (type_quals & TYPE_QUAL_VOLATILE)
12966 warning_at (typespec_loc, OPT_Wvolatile,
12967 "%<volatile%>-qualified return type is "
12968 "deprecated");
12969
12970 /* We now know that the TYPE_QUALS don't apply to the
12971 decl, but to its return type. */
12972 type_quals = TYPE_UNQUALIFIED;
12973 }
12974
12975 /* Error about some types functions can't return. */
12976
12977 if (TREE_CODE (type) == FUNCTION_TYPE)
12978 {
12979 error_at (typespec_loc, "%qs declared as function returning "
12980 "a function", name);
12981 return error_mark_node;
12982 }
12983 if (TREE_CODE (type) == ARRAY_TYPE)
12984 {
12985 error_at (typespec_loc, "%qs declared as function returning "
12986 "an array", name);
12987 return error_mark_node;
12988 }
12989 if (constinit_p && funcdecl_p)
12990 {
12991 error_at (declspecs->locations[ds_constinit],
12992 "%<constinit%> on function return type is not "
12993 "allowed");
12994 return error_mark_node;
12995 }
12996
12997 if (check_decltype_auto (typespec_loc, type))
12998 return error_mark_node;
12999
13000 if (ctype == NULL_TREE
13001 && decl_context == FIELD
13002 && funcdecl_p
13003 && friendp == 0)
13004 ctype = current_class_type;
13005
13006 if (ctype && (sfk == sfk_constructor
13007 || sfk == sfk_destructor))
13008 {
13009 /* We are within a class's scope. If our declarator name
13010 is the same as the class name, and we are defining
13011 a function, then it is a constructor/destructor, and
13012 therefore returns a void type. */
13013
13014 /* ISO C++ 12.4/2. A destructor may not be declared
13015 const or volatile. A destructor may not be static.
13016 A destructor may not be declared with ref-qualifier.
13017
13018 ISO C++ 12.1. A constructor may not be declared
13019 const or volatile. A constructor may not be
13020 virtual. A constructor may not be static.
13021 A constructor may not be declared with ref-qualifier. */
13022 if (staticp == 2)
13023 error_at (declspecs->locations[ds_storage_class],
13024 (flags == DTOR_FLAG)
13025 ? G_("destructor cannot be static member "
13026 "function")
13027 : G_("constructor cannot be static member "
13028 "function"));
13029 if (memfn_quals)
13030 {
13031 error ((flags == DTOR_FLAG)
13032 ? G_("destructors may not be cv-qualified")
13033 : G_("constructors may not be cv-qualified"));
13034 memfn_quals = TYPE_UNQUALIFIED;
13035 }
13036
13037 if (rqual)
13038 {
13039 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
13040 error ((flags == DTOR_FLAG)
13041 ? G_("destructors may not be ref-qualified")
13042 : G_("constructors may not be ref-qualified"));
13043 rqual = REF_QUAL_NONE;
13044 }
13045
13046 if (decl_context == FIELD
13047 && !member_function_or_else (ctype,
13048 current_class_type,
13049 flags))
13050 return error_mark_node;
13051
13052 if (flags != DTOR_FLAG)
13053 {
13054 /* It's a constructor. */
13055 if (explicitp == 1)
13056 explicitp = 2;
13057 if (virtualp)
13058 {
13059 permerror (declspecs->locations[ds_virtual],
13060 "constructors cannot be declared %<virtual%>");
13061 virtualp = 0;
13062 }
13063 if (decl_context == FIELD
13064 && sfk != sfk_constructor)
13065 return error_mark_node;
13066 }
13067 if (decl_context == FIELD)
13068 staticp = 0;
13069 }
13070 else if (friendp)
13071 {
13072 if (virtualp)
13073 {
13074 /* Cannot be both friend and virtual. */
13075 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
13076 richloc.add_range (declspecs->locations[ds_friend]);
13077 error_at (&richloc, "virtual functions cannot be friends");
13078 friendp = 0;
13079 }
13080 if (decl_context == NORMAL)
13081 error_at (declarator->id_loc,
13082 "friend declaration not in class definition");
13083 if (current_function_decl && funcdef_flag)
13084 {
13085 error_at (declarator->id_loc,
13086 "cannot define friend function %qs in a local "
13087 "class definition", name);
13088 friendp = 0;
13089 }
13090 /* [class.friend]/6: A function can be defined in a friend
13091 declaration if the function name is unqualified. */
13092 if (funcdef_flag && in_namespace)
13093 {
13094 if (in_namespace == global_namespace)
13095 error_at (declarator->id_loc,
13096 "friend function definition %qs cannot have "
13097 "a name qualified with %<::%>", name);
13098 else
13099 error_at (declarator->id_loc,
13100 "friend function definition %qs cannot have "
13101 "a name qualified with %<%D::%>", name,
13102 in_namespace);
13103 }
13104 }
13105 else if (ctype && sfk == sfk_conversion)
13106 {
13107 if (explicitp == 1)
13108 {
13109 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
13110 explicitp = 2;
13111 }
13112 }
13113 else if (sfk == sfk_deduction_guide)
13114 {
13115 if (explicitp == 1)
13116 explicitp = 2;
13117 }
13118
13119 tree pushed_scope = NULL_TREE;
13120 if (funcdecl_p
13121 && decl_context != FIELD
13122 && inner_declarator->u.id.qualifying_scope
13123 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
13124 pushed_scope
13125 = push_scope (inner_declarator->u.id.qualifying_scope);
13126
13127 arg_types = grokparms (declarator->u.function.parameters, &parms);
13128
13129 if (pushed_scope)
13130 pop_scope (pushed_scope);
13131
13132 if (inner_declarator
13133 && inner_declarator->kind == cdk_id
13134 && inner_declarator->u.id.sfk == sfk_destructor
13135 && arg_types != void_list_node)
13136 {
13137 error_at (declarator->id_loc,
13138 "destructors may not have parameters");
13139 arg_types = void_list_node;
13140 parms = NULL_TREE;
13141 }
13142
13143 type = build_function_type (type, arg_types);
13144
13145 tree attrs = declarator->std_attributes;
13146 if (tx_qual)
13147 {
13148 tree att = build_tree_list (tx_qual, NULL_TREE);
13149 /* transaction_safe applies to the type, but
13150 transaction_safe_dynamic applies to the function. */
13151 if (is_attribute_p ("transaction_safe", tx_qual))
13152 attrs = chainon (attrs, att);
13153 else
13154 returned_attrs = chainon (returned_attrs, att);
13155 }
13156 if (attrs)
13157 /* [dcl.fct]/2:
13158
13159 The optional attribute-specifier-seq appertains to
13160 the function type. */
13161 cplus_decl_attributes (&type, attrs, 0);
13162
13163 if (raises)
13164 type = build_exception_variant (type, raises);
13165 }
13166 break;
13167
13168 case cdk_pointer:
13169 case cdk_reference:
13170 case cdk_ptrmem:
13171 /* Filter out pointers-to-references and references-to-references.
13172 We can get these if a TYPE_DECL is used. */
13173
13174 if (TYPE_REF_P (type))
13175 {
13176 if (declarator->kind != cdk_reference)
13177 {
13178 error ("cannot declare pointer to %q#T", type);
13179 type = TREE_TYPE (type);
13180 }
13181
13182 /* In C++0x, we allow reference to reference declarations
13183 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
13184 and template type arguments [14.3.1/4 temp.arg.type]. The
13185 check for direct reference to reference declarations, which
13186 are still forbidden, occurs below. Reasoning behind the change
13187 can be found in DR106, DR540, and the rvalue reference
13188 proposals. */
13189 else if (cxx_dialect == cxx98)
13190 {
13191 error ("cannot declare reference to %q#T", type);
13192 type = TREE_TYPE (type);
13193 }
13194 }
13195 else if (VOID_TYPE_P (type))
13196 {
13197 if (declarator->kind == cdk_reference)
13198 error ("cannot declare reference to %q#T", type);
13199 else if (declarator->kind == cdk_ptrmem)
13200 error ("cannot declare pointer to %q#T member", type);
13201 }
13202
13203 /* We now know that the TYPE_QUALS don't apply to the decl,
13204 but to the target of the pointer. */
13205 type_quals = TYPE_UNQUALIFIED;
13206
13207 /* This code used to handle METHOD_TYPE, but I don't think it's
13208 possible to get it here anymore. */
13209 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13210 if (declarator->kind == cdk_ptrmem
13211 && TREE_CODE (type) == FUNCTION_TYPE)
13212 {
13213 memfn_quals |= type_memfn_quals (type);
13214 type = build_memfn_type (type,
13215 declarator->u.pointer.class_type,
13216 memfn_quals,
13217 rqual);
13218 if (type == error_mark_node)
13219 return error_mark_node;
13220
13221 rqual = REF_QUAL_NONE;
13222 memfn_quals = TYPE_UNQUALIFIED;
13223 }
13224
13225 if (TREE_CODE (type) == FUNCTION_TYPE
13226 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13227 || type_memfn_rqual (type) != REF_QUAL_NONE))
13228 error (declarator->kind == cdk_reference
13229 ? G_("cannot declare reference to qualified function type %qT")
13230 : G_("cannot declare pointer to qualified function type %qT"),
13231 type);
13232
13233 /* When the pointed-to type involves components of variable size,
13234 care must be taken to ensure that the size evaluation code is
13235 emitted early enough to dominate all the possible later uses
13236 and late enough for the variables on which it depends to have
13237 been assigned.
13238
13239 This is expected to happen automatically when the pointed-to
13240 type has a name/declaration of it's own, but special attention
13241 is required if the type is anonymous.
13242
13243 We handle the NORMAL and FIELD contexts here by inserting a
13244 dummy statement that just evaluates the size at a safe point
13245 and ensures it is not deferred until e.g. within a deeper
13246 conditional context (c++/43555).
13247
13248 We expect nothing to be needed here for PARM or TYPENAME.
13249 Evaluating the size at this point for TYPENAME would
13250 actually be incorrect, as we might be in the middle of an
13251 expression with side effects on the pointed-to type size
13252 "arguments" prior to the pointer declaration point and the
13253 size evaluation could end up prior to the side effects. */
13254
13255 if (!TYPE_NAME (type)
13256 && (decl_context == NORMAL || decl_context == FIELD)
13257 && at_function_scope_p ()
13258 && variably_modified_type_p (type, NULL_TREE))
13259 {
13260 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
13261 NULL_TREE, type);
13262 add_decl_expr (TYPE_NAME (type));
13263 }
13264
13265 if (declarator->kind == cdk_reference)
13266 {
13267 /* In C++0x, the type we are creating a reference to might be
13268 a typedef which is itself a reference type. In that case,
13269 we follow the reference collapsing rules in
13270 [7.1.3/8 dcl.typedef] to create the final reference type:
13271
13272 "If a typedef TD names a type that is a reference to a type
13273 T, an attempt to create the type 'lvalue reference to cv TD'
13274 creates the type 'lvalue reference to T,' while an attempt
13275 to create the type "rvalue reference to cv TD' creates the
13276 type TD."
13277 */
13278 if (VOID_TYPE_P (type))
13279 /* We already gave an error. */;
13280 else if (TYPE_REF_P (type))
13281 {
13282 if (declarator->u.reference.rvalue_ref)
13283 /* Leave type alone. */;
13284 else
13285 type = cp_build_reference_type (TREE_TYPE (type), false);
13286 }
13287 else
13288 type = cp_build_reference_type
13289 (type, declarator->u.reference.rvalue_ref);
13290
13291 /* In C++0x, we need this check for direct reference to
13292 reference declarations, which are forbidden by
13293 [8.3.2/5 dcl.ref]. Reference to reference declarations
13294 are only allowed indirectly through typedefs and template
13295 type arguments. Example:
13296
13297 void foo(int & &); // invalid ref-to-ref decl
13298
13299 typedef int & int_ref;
13300 void foo(int_ref &); // valid ref-to-ref decl
13301 */
13302 if (inner_declarator && inner_declarator->kind == cdk_reference)
13303 error ("cannot declare reference to %q#T, which is not "
13304 "a typedef or a template type argument", type);
13305 }
13306 else if (TREE_CODE (type) == METHOD_TYPE)
13307 type = build_ptrmemfunc_type (build_pointer_type (type));
13308 else if (declarator->kind == cdk_ptrmem)
13309 {
13310 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
13311 != NAMESPACE_DECL);
13312 if (declarator->u.pointer.class_type == error_mark_node)
13313 /* We will already have complained. */
13314 type = error_mark_node;
13315 else
13316 type = build_ptrmem_type (declarator->u.pointer.class_type,
13317 type);
13318 }
13319 else
13320 type = build_pointer_type (type);
13321
13322 /* Process a list of type modifier keywords (such as
13323 const or volatile) that were given inside the `*' or `&'. */
13324
13325 if (declarator->u.pointer.qualifiers)
13326 {
13327 type
13328 = cp_build_qualified_type (type,
13329 declarator->u.pointer.qualifiers);
13330 type_quals = cp_type_quals (type);
13331 }
13332
13333 /* Apply C++11 attributes to the pointer, and not to the
13334 type pointed to. This is unlike what is done for GNU
13335 attributes above. It is to comply with [dcl.ptr]/1:
13336
13337 [the optional attribute-specifier-seq (7.6.1) appertains
13338 to the pointer and not to the object pointed to]. */
13339 if (declarator->std_attributes)
13340 decl_attributes (&type, declarator->std_attributes,
13341 0);
13342
13343 ctype = NULL_TREE;
13344 break;
13345
13346 case cdk_error:
13347 break;
13348
13349 default:
13350 gcc_unreachable ();
13351 }
13352 }
13353
13354 id_loc = declarator ? declarator->id_loc : input_location;
13355
13356 if (innermost_code != cdk_function
13357 /* Don't check this if it can be the artifical decltype(auto)
13358 we created when building a constraint in a compound-requirement:
13359 that the type-constraint is plain is going to be checked in
13360 cp_parser_compound_requirement. */
13361 && decl_context != TYPENAME
13362 && check_decltype_auto (id_loc, type))
13363 return error_mark_node;
13364
13365 /* A `constexpr' specifier used in an object declaration declares
13366 the object as `const'. */
13367 if (constexpr_p && innermost_code != cdk_function)
13368 {
13369 /* DR1688 says that a `constexpr' specifier in combination with
13370 `volatile' is valid. */
13371
13372 if (!TYPE_REF_P (type))
13373 {
13374 type_quals |= TYPE_QUAL_CONST;
13375 type = cp_build_qualified_type (type, type_quals);
13376 }
13377 }
13378
13379 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
13380 && !FUNC_OR_METHOD_TYPE_P (type)
13381 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
13382 {
13383 error ("template-id %qD used as a declarator",
13384 unqualified_id);
13385 unqualified_id = dname;
13386 }
13387
13388 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
13389 qualified with a class-name, turn it into a METHOD_TYPE, unless
13390 we know that the function is static. We take advantage of this
13391 opportunity to do other processing that pertains to entities
13392 explicitly declared to be class members. Note that if DECLARATOR
13393 is non-NULL, we know it is a cdk_id declarator; otherwise, we
13394 would not have exited the loop above. */
13395 if (declarator
13396 && declarator->kind == cdk_id
13397 && declarator->u.id.qualifying_scope
13398 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
13399 {
13400 ctype = declarator->u.id.qualifying_scope;
13401 ctype = TYPE_MAIN_VARIANT (ctype);
13402 template_count = num_template_headers_for_class (ctype);
13403
13404 if (ctype == current_class_type)
13405 {
13406 if (friendp)
13407 {
13408 permerror (declspecs->locations[ds_friend],
13409 "member functions are implicitly "
13410 "friends of their class");
13411 friendp = 0;
13412 }
13413 else
13414 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
13415 ctype, name);
13416 }
13417 else if (/* If the qualifying type is already complete, then we
13418 can skip the following checks. */
13419 !COMPLETE_TYPE_P (ctype)
13420 && (/* If the function is being defined, then
13421 qualifying type must certainly be complete. */
13422 funcdef_flag
13423 /* A friend declaration of "T::f" is OK, even if
13424 "T" is a template parameter. But, if this
13425 function is not a friend, the qualifying type
13426 must be a class. */
13427 || (!friendp && !CLASS_TYPE_P (ctype))
13428 /* For a declaration, the type need not be
13429 complete, if either it is dependent (since there
13430 is no meaningful definition of complete in that
13431 case) or the qualifying class is currently being
13432 defined. */
13433 || !(dependent_type_p (ctype)
13434 || currently_open_class (ctype)))
13435 /* Check that the qualifying type is complete. */
13436 && !complete_type_or_else (ctype, NULL_TREE))
13437 return error_mark_node;
13438 else if (TREE_CODE (type) == FUNCTION_TYPE)
13439 {
13440 if (current_class_type
13441 && (!friendp || funcdef_flag || initialized))
13442 {
13443 error_at (id_loc, funcdef_flag || initialized
13444 ? G_("cannot define member function %<%T::%s%> "
13445 "within %qT")
13446 : G_("cannot declare member function %<%T::%s%> "
13447 "within %qT"),
13448 ctype, name, current_class_type);
13449 return error_mark_node;
13450 }
13451 }
13452 else if (typedef_p && current_class_type)
13453 {
13454 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
13455 ctype, name, current_class_type);
13456 return error_mark_node;
13457 }
13458 }
13459
13460 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
13461 ctype = current_class_type;
13462
13463 /* Now TYPE has the actual type. */
13464
13465 if (returned_attrs)
13466 {
13467 if (attrlist)
13468 *attrlist = chainon (returned_attrs, *attrlist);
13469 else
13470 attrlist = &returned_attrs;
13471 }
13472
13473 if (declarator
13474 && declarator->kind == cdk_id
13475 && declarator->std_attributes
13476 && attrlist != NULL)
13477 {
13478 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
13479 a declarator-id appertains to the entity that is declared. */
13480 if (declarator->std_attributes != error_mark_node)
13481 *attrlist = chainon (*attrlist, declarator->std_attributes);
13482 else
13483 /* We should have already diagnosed the issue (c++/78344). */
13484 gcc_assert (seen_error ());
13485 }
13486
13487 /* Handle parameter packs. */
13488 if (parameter_pack_p)
13489 {
13490 if (decl_context == PARM)
13491 /* Turn the type into a pack expansion.*/
13492 type = make_pack_expansion (type);
13493 else
13494 error ("non-parameter %qs cannot be a parameter pack", name);
13495 }
13496
13497 if ((decl_context == FIELD || decl_context == PARM)
13498 && !processing_template_decl
13499 && variably_modified_type_p (type, NULL_TREE))
13500 {
13501 if (decl_context == FIELD)
13502 error_at (id_loc,
13503 "data member may not have variably modified type %qT", type);
13504 else
13505 error_at (id_loc,
13506 "parameter may not have variably modified type %qT", type);
13507 type = error_mark_node;
13508 }
13509
13510 if (explicitp == 1 || (explicitp && friendp))
13511 {
13512 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
13513 in the declaration of a constructor or conversion function within
13514 a class definition. */
13515 if (!current_class_type)
13516 error_at (declspecs->locations[ds_explicit],
13517 "%<explicit%> outside class declaration");
13518 else if (friendp)
13519 error_at (declspecs->locations[ds_explicit],
13520 "%<explicit%> in friend declaration");
13521 else
13522 error_at (declspecs->locations[ds_explicit],
13523 "only declarations of constructors and conversion operators "
13524 "can be %<explicit%>");
13525 explicitp = 0;
13526 }
13527
13528 if (storage_class == sc_mutable)
13529 {
13530 location_t sloc = declspecs->locations[ds_storage_class];
13531 if (decl_context != FIELD || friendp)
13532 {
13533 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
13534 name);
13535 storage_class = sc_none;
13536 }
13537 else if (decl_context == TYPENAME || typedef_p)
13538 {
13539 error_at (sloc,
13540 "non-object member %qs cannot be declared %<mutable%>",
13541 name);
13542 storage_class = sc_none;
13543 }
13544 else if (FUNC_OR_METHOD_TYPE_P (type))
13545 {
13546 error_at (sloc, "function %qs cannot be declared %<mutable%>",
13547 name);
13548 storage_class = sc_none;
13549 }
13550 else if (staticp)
13551 {
13552 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
13553 name);
13554 storage_class = sc_none;
13555 }
13556 else if (type_quals & TYPE_QUAL_CONST)
13557 {
13558 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
13559 name);
13560 storage_class = sc_none;
13561 }
13562 else if (TYPE_REF_P (type))
13563 {
13564 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
13565 name);
13566 storage_class = sc_none;
13567 }
13568 }
13569
13570 /* If this is declaring a typedef name, return a TYPE_DECL. */
13571 if (typedef_p && decl_context != TYPENAME)
13572 {
13573 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
13574 tree decl;
13575
13576 if (funcdef_flag)
13577 {
13578 if (decl_context == NORMAL)
13579 error_at (id_loc,
13580 "typedef may not be a function definition");
13581 else
13582 error_at (id_loc,
13583 "typedef may not be a member function definition");
13584 return error_mark_node;
13585 }
13586
13587 /* This declaration:
13588
13589 typedef void f(int) const;
13590
13591 declares a function type which is not a member of any
13592 particular class, but which is cv-qualified; for
13593 example "f S::*" declares a pointer to a const-qualified
13594 member function of S. We record the cv-qualification in the
13595 function type. */
13596 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
13597 {
13598 type = apply_memfn_quals (type, memfn_quals, rqual);
13599
13600 /* We have now dealt with these qualifiers. */
13601 memfn_quals = TYPE_UNQUALIFIED;
13602 rqual = REF_QUAL_NONE;
13603 }
13604
13605 if (type_uses_auto (type))
13606 {
13607 if (alias_p)
13608 error_at (declspecs->locations[ds_type_spec],
13609 "%<auto%> not allowed in alias declaration");
13610 else
13611 error_at (declspecs->locations[ds_type_spec],
13612 "typedef declared %<auto%>");
13613 type = error_mark_node;
13614 }
13615
13616 if (reqs)
13617 error_at (location_of (reqs), "requires-clause on typedef");
13618
13619 if (id_declarator && declarator->u.id.qualifying_scope)
13620 {
13621 error_at (id_loc, "typedef name may not be a nested-name-specifier");
13622 type = error_mark_node;
13623 }
13624
13625 if (decl_context == FIELD)
13626 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
13627 else
13628 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
13629
13630 if (decl_context != FIELD)
13631 {
13632 if (!current_function_decl)
13633 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
13634 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
13635 /* The TYPE_DECL is "abstract" because there will be
13636 clones of this constructor/destructor, and there will
13637 be copies of this TYPE_DECL generated in those
13638 clones. The decloning optimization (for space) may
13639 revert this subsequently if it determines that
13640 the clones should share a common implementation. */
13641 DECL_ABSTRACT_P (decl) = true;
13642
13643 set_originating_module (decl);
13644 }
13645 else if (current_class_type
13646 && constructor_name_p (unqualified_id, current_class_type))
13647 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
13648 "as enclosing class",
13649 unqualified_id);
13650
13651 /* If the user declares "typedef struct {...} foo" then the
13652 struct will have an anonymous name. Fill that name in now.
13653 Nothing can refer to it, so nothing needs know about the name
13654 change. */
13655 if (type != error_mark_node
13656 && unqualified_id
13657 && TYPE_NAME (type)
13658 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13659 && TYPE_UNNAMED_P (type)
13660 && declspecs->type_definition_p
13661 && attributes_naming_typedef_ok (*attrlist)
13662 && cp_type_quals (type) == TYPE_UNQUALIFIED)
13663 name_unnamed_type (type, decl);
13664
13665 if (signed_p
13666 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
13667 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
13668
13669 bad_specifiers (decl, BSP_TYPE, virtualp,
13670 memfn_quals != TYPE_UNQUALIFIED,
13671 inlinep, friendp, raises != NULL_TREE,
13672 declspecs->locations);
13673
13674 if (alias_p)
13675 /* Acknowledge that this was written:
13676 `using analias = atype;'. */
13677 TYPE_DECL_ALIAS_P (decl) = 1;
13678
13679 return decl;
13680 }
13681
13682 /* Detect the case of an array type of unspecified size
13683 which came, as such, direct from a typedef name.
13684 We must copy the type, so that the array's domain can be
13685 individually set by the object's initializer. */
13686
13687 if (type && typedef_type
13688 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
13689 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
13690 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13691
13692 /* Detect where we're using a typedef of function type to declare a
13693 function. PARMS will not be set, so we must create it now. */
13694
13695 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
13696 {
13697 tree decls = NULL_TREE;
13698 tree args;
13699
13700 for (args = TYPE_ARG_TYPES (type);
13701 args && args != void_list_node;
13702 args = TREE_CHAIN (args))
13703 {
13704 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
13705 TREE_VALUE (args));
13706
13707 DECL_CHAIN (decl) = decls;
13708 decls = decl;
13709 }
13710
13711 parms = nreverse (decls);
13712
13713 if (decl_context != TYPENAME)
13714 {
13715 /* The qualifiers on the function type become the qualifiers on
13716 the non-static member function. */
13717 memfn_quals |= type_memfn_quals (type);
13718 rqual = type_memfn_rqual (type);
13719 type_quals = TYPE_UNQUALIFIED;
13720 raises = TYPE_RAISES_EXCEPTIONS (type);
13721 }
13722 }
13723
13724 /* If this is a type name (such as, in a cast or sizeof),
13725 compute the type and return it now. */
13726
13727 if (decl_context == TYPENAME)
13728 {
13729 /* Note that here we don't care about type_quals. */
13730
13731 /* Special case: "friend class foo" looks like a TYPENAME context. */
13732 if (friendp)
13733 {
13734 if (inlinep)
13735 {
13736 error ("%<inline%> specified for friend class declaration");
13737 inlinep = 0;
13738 }
13739
13740 if (!current_aggr)
13741 {
13742 /* Don't allow friend declaration without a class-key. */
13743 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13744 permerror (input_location, "template parameters cannot be friends");
13745 else if (TREE_CODE (type) == TYPENAME_TYPE)
13746 permerror (input_location, "friend declaration requires class-key, "
13747 "i.e. %<friend class %T::%D%>",
13748 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
13749 else
13750 permerror (input_location, "friend declaration requires class-key, "
13751 "i.e. %<friend %#T%>",
13752 type);
13753 }
13754
13755 /* Only try to do this stuff if we didn't already give up. */
13756 if (type != integer_type_node)
13757 {
13758 /* A friendly class? */
13759 if (current_class_type)
13760 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
13761 /*complain=*/true);
13762 else
13763 error ("trying to make class %qT a friend of global scope",
13764 type);
13765
13766 type = void_type_node;
13767 }
13768 }
13769 else if (memfn_quals || rqual)
13770 {
13771 if (ctype == NULL_TREE
13772 && TREE_CODE (type) == METHOD_TYPE)
13773 ctype = TYPE_METHOD_BASETYPE (type);
13774
13775 if (ctype)
13776 type = build_memfn_type (type, ctype, memfn_quals, rqual);
13777 /* Core issue #547: need to allow this in template type args.
13778 Allow it in general in C++11 for alias-declarations. */
13779 else if ((template_type_arg || cxx_dialect >= cxx11)
13780 && TREE_CODE (type) == FUNCTION_TYPE)
13781 type = apply_memfn_quals (type, memfn_quals, rqual);
13782 else
13783 error ("invalid qualifiers on non-member function type");
13784 }
13785
13786 if (reqs)
13787 error_at (location_of (reqs), "requires-clause on type-id");
13788
13789 return type;
13790 }
13791 else if (unqualified_id == NULL_TREE && decl_context != PARM
13792 && decl_context != CATCHPARM
13793 && TREE_CODE (type) != UNION_TYPE
13794 && ! bitfield
13795 && innermost_code != cdk_decomp)
13796 {
13797 error ("abstract declarator %qT used as declaration", type);
13798 return error_mark_node;
13799 }
13800
13801 if (!FUNC_OR_METHOD_TYPE_P (type))
13802 {
13803 /* Only functions may be declared using an operator-function-id. */
13804 if (dname && IDENTIFIER_ANY_OP_P (dname))
13805 {
13806 error_at (id_loc, "declaration of %qD as non-function", dname);
13807 return error_mark_node;
13808 }
13809
13810 if (reqs)
13811 error_at (location_of (reqs),
13812 "requires-clause on declaration of non-function type %qT",
13813 type);
13814 }
13815
13816 /* We don't check parameter types here because we can emit a better
13817 error message later. */
13818 if (decl_context != PARM)
13819 {
13820 type = check_var_type (unqualified_id, type, id_loc);
13821 if (type == error_mark_node)
13822 return error_mark_node;
13823 }
13824
13825 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
13826 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
13827
13828 if (decl_context == PARM || decl_context == CATCHPARM)
13829 {
13830 if (ctype || in_namespace)
13831 error ("cannot use %<::%> in parameter declaration");
13832
13833 tree auto_node = type_uses_auto (type);
13834 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
13835 {
13836 if (cxx_dialect >= cxx14)
13837 {
13838 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
13839 error_at (typespec_loc,
13840 "cannot declare a parameter with %<decltype(auto)%>");
13841 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13842 {
13843 auto_diagnostic_group g;
13844 error_at (typespec_loc,
13845 "class template placeholder %qE not permitted "
13846 "in this context", c);
13847 if (decl_context == PARM && cxx_dialect >= cxx20)
13848 inform (typespec_loc, "use %<auto%> for an "
13849 "abbreviated function template");
13850 }
13851 else
13852 error_at (typespec_loc,
13853 "%<auto%> parameter not permitted in this context");
13854 }
13855 else
13856 error_at (typespec_loc, "parameter declared %<auto%>");
13857 type = error_mark_node;
13858 }
13859
13860 /* A parameter declared as an array of T is really a pointer to T.
13861 One declared as a function is really a pointer to a function.
13862 One declared as a member is really a pointer to member. */
13863
13864 if (TREE_CODE (type) == ARRAY_TYPE)
13865 {
13866 /* Transfer const-ness of array into that of type pointed to. */
13867 type = build_pointer_type (TREE_TYPE (type));
13868 type_quals = TYPE_UNQUALIFIED;
13869 array_parameter_p = true;
13870 }
13871 else if (TREE_CODE (type) == FUNCTION_TYPE)
13872 type = build_pointer_type (type);
13873 }
13874
13875 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13876 && !(unqualified_id
13877 && identifier_p (unqualified_id)
13878 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13879 {
13880 cp_cv_quals real_quals = memfn_quals;
13881 if (cxx_dialect < cxx14 && constexpr_p
13882 && sfk != sfk_constructor && sfk != sfk_destructor)
13883 real_quals |= TYPE_QUAL_CONST;
13884 type = build_memfn_type (type, ctype, real_quals, rqual);
13885 }
13886
13887 {
13888 tree decl = NULL_TREE;
13889
13890 if (decl_context == PARM)
13891 {
13892 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13893 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13894
13895 bad_specifiers (decl, BSP_PARM, virtualp,
13896 memfn_quals != TYPE_UNQUALIFIED,
13897 inlinep, friendp, raises != NULL_TREE,
13898 declspecs->locations);
13899 }
13900 else if (decl_context == FIELD)
13901 {
13902 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type))
13903 if (tree auto_node = type_uses_auto (type))
13904 {
13905 location_t tloc = declspecs->locations[ds_type_spec];
13906 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13907 error_at (tloc, "invalid use of template-name %qE without an "
13908 "argument list",
13909 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13910 else
13911 error_at (tloc, "non-static data member declared with "
13912 "placeholder %qT", auto_node);
13913 type = error_mark_node;
13914 }
13915
13916 /* The C99 flexible array extension. */
13917 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13918 && TYPE_DOMAIN (type) == NULL_TREE)
13919 {
13920 if (ctype
13921 && (TREE_CODE (ctype) == UNION_TYPE
13922 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13923 {
13924 error_at (id_loc, "flexible array member in union");
13925 type = error_mark_node;
13926 }
13927 else
13928 {
13929 /* Array is a flexible member. */
13930 if (name)
13931 pedwarn (id_loc, OPT_Wpedantic,
13932 "ISO C++ forbids flexible array member %qs", name);
13933 else
13934 pedwarn (input_location, OPT_Wpedantic,
13935 "ISO C++ forbids flexible array members");
13936
13937 /* Flexible array member has a null domain. */
13938 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13939 }
13940 }
13941
13942 if (type == error_mark_node)
13943 {
13944 /* Happens when declaring arrays of sizes which
13945 are error_mark_node, for example. */
13946 decl = NULL_TREE;
13947 }
13948 else if (in_namespace && !friendp)
13949 {
13950 /* Something like struct S { int N::j; }; */
13951 error_at (id_loc, "invalid use of %<::%>");
13952 return error_mark_node;
13953 }
13954 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13955 {
13956 int publicp = 0;
13957 tree function_context;
13958
13959 if (friendp == 0)
13960 {
13961 /* This should never happen in pure C++ (the check
13962 could be an assert). It could happen in
13963 Objective-C++ if someone writes invalid code that
13964 uses a function declaration for an instance
13965 variable or property (instance variables and
13966 properties are parsed as FIELD_DECLs, but they are
13967 part of an Objective-C class, not a C++ class).
13968 That code is invalid and is caught by this
13969 check. */
13970 if (!ctype)
13971 {
13972 error ("declaration of function %qD in invalid context",
13973 unqualified_id);
13974 return error_mark_node;
13975 }
13976
13977 /* ``A union may [ ... ] not [ have ] virtual functions.''
13978 ARM 9.5 */
13979 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13980 {
13981 error_at (declspecs->locations[ds_virtual],
13982 "function %qD declared %<virtual%> inside a union",
13983 unqualified_id);
13984 return error_mark_node;
13985 }
13986
13987 if (virtualp
13988 && identifier_p (unqualified_id)
13989 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13990 {
13991 error_at (declspecs->locations[ds_virtual],
13992 "%qD cannot be declared %<virtual%>, since it "
13993 "is always static", unqualified_id);
13994 virtualp = 0;
13995 }
13996 }
13997
13998 /* Check that the name used for a destructor makes sense. */
13999 if (sfk == sfk_destructor)
14000 {
14001 tree uqname = id_declarator->u.id.unqualified_name;
14002
14003 if (!ctype)
14004 {
14005 gcc_assert (friendp);
14006 error_at (id_loc, "expected qualified name in friend "
14007 "declaration for destructor %qD", uqname);
14008 return error_mark_node;
14009 }
14010
14011 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
14012 {
14013 error_at (id_loc, "declaration of %qD as member of %qT",
14014 uqname, ctype);
14015 return error_mark_node;
14016 }
14017 if (concept_p)
14018 {
14019 error_at (declspecs->locations[ds_concept],
14020 "a destructor cannot be %qs", "concept");
14021 return error_mark_node;
14022 }
14023 if (constexpr_p && cxx_dialect < cxx20)
14024 {
14025 error_at (declspecs->locations[ds_constexpr],
14026 "%<constexpr%> destructors only available"
14027 " with %<-std=c++20%> or %<-std=gnu++20%>");
14028 return error_mark_node;
14029 }
14030 if (consteval_p)
14031 {
14032 error_at (declspecs->locations[ds_consteval],
14033 "a destructor cannot be %qs", "consteval");
14034 return error_mark_node;
14035 }
14036 }
14037 else if (sfk == sfk_constructor && friendp && !ctype)
14038 {
14039 error ("expected qualified name in friend declaration "
14040 "for constructor %qD",
14041 id_declarator->u.id.unqualified_name);
14042 return error_mark_node;
14043 }
14044 if (sfk == sfk_constructor)
14045 if (concept_p)
14046 {
14047 error_at (declspecs->locations[ds_concept],
14048 "a constructor cannot be %<concept%>");
14049 return error_mark_node;
14050 }
14051 if (concept_p)
14052 {
14053 error_at (declspecs->locations[ds_concept],
14054 "a concept cannot be a member function");
14055 concept_p = false;
14056 }
14057 else if (consteval_p
14058 && identifier_p (unqualified_id)
14059 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14060 {
14061 error_at (declspecs->locations[ds_consteval],
14062 "%qD cannot be %qs", unqualified_id, "consteval");
14063 consteval_p = false;
14064 }
14065
14066 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14067 {
14068 tree tmpl = TREE_OPERAND (unqualified_id, 0);
14069 if (variable_template_p (tmpl))
14070 {
14071 error_at (id_loc, "specialization of variable template "
14072 "%qD declared as function", tmpl);
14073 inform (DECL_SOURCE_LOCATION (tmpl),
14074 "variable template declared here");
14075 return error_mark_node;
14076 }
14077 }
14078
14079 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
14080 function_context
14081 = (ctype != NULL_TREE
14082 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
14083 publicp = ((! friendp || ! staticp)
14084 && function_context == NULL_TREE);
14085
14086 decl = grokfndecl (ctype, type,
14087 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
14088 ? unqualified_id : dname,
14089 parms,
14090 unqualified_id,
14091 declspecs,
14092 reqs,
14093 virtualp, flags, memfn_quals, rqual, raises,
14094 friendp ? -1 : 0, friendp, publicp,
14095 inlinep | (2 * constexpr_p) | (4 * concept_p)
14096 | (8 * consteval_p),
14097 initialized == SD_DELETED, sfk,
14098 funcdef_flag, late_return_type_p,
14099 template_count, in_namespace,
14100 attrlist, id_loc);
14101 decl = set_virt_specifiers (decl, virt_specifiers);
14102 if (decl == NULL_TREE)
14103 return error_mark_node;
14104 #if 0
14105 /* This clobbers the attrs stored in `decl' from `attrlist'. */
14106 /* The decl and setting of decl_attr is also turned off. */
14107 decl = build_decl_attribute_variant (decl, decl_attr);
14108 #endif
14109
14110 /* [class.conv.ctor]
14111
14112 A constructor declared without the function-specifier
14113 explicit that can be called with a single parameter
14114 specifies a conversion from the type of its first
14115 parameter to the type of its class. Such a constructor
14116 is called a converting constructor. */
14117 if (explicitp == 2)
14118 DECL_NONCONVERTING_P (decl) = 1;
14119
14120 if (declspecs->explicit_specifier)
14121 store_explicit_specifier (decl, declspecs->explicit_specifier);
14122 }
14123 else if (!staticp
14124 && ((current_class_type
14125 && same_type_p (type, current_class_type))
14126 || (!dependent_type_p (type)
14127 && !COMPLETE_TYPE_P (complete_type (type))
14128 && (!complete_or_array_type_p (type)
14129 || initialized == SD_UNINITIALIZED))))
14130 {
14131 if (TREE_CODE (type) != ARRAY_TYPE
14132 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
14133 {
14134 if (unqualified_id)
14135 {
14136 error_at (id_loc, "field %qD has incomplete type %qT",
14137 unqualified_id, type);
14138 cxx_incomplete_type_inform (strip_array_types (type));
14139 }
14140 else
14141 error ("name %qT has incomplete type", type);
14142
14143 type = error_mark_node;
14144 decl = NULL_TREE;
14145 }
14146 }
14147 else if (!verify_type_context (input_location,
14148 staticp
14149 ? TCTX_STATIC_STORAGE
14150 : TCTX_FIELD, type))
14151 {
14152 type = error_mark_node;
14153 decl = NULL_TREE;
14154 }
14155 else
14156 {
14157 if (friendp)
14158 {
14159 if (unqualified_id)
14160 error_at (id_loc,
14161 "%qE is neither function nor member function; "
14162 "cannot be declared friend", unqualified_id);
14163 else
14164 error ("unnamed field is neither function nor member "
14165 "function; cannot be declared friend");
14166 return error_mark_node;
14167 }
14168 decl = NULL_TREE;
14169 }
14170
14171 if (friendp)
14172 {
14173 /* Packages tend to use GNU attributes on friends, so we only
14174 warn for standard attributes. */
14175 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist))
14176 {
14177 *attrlist = NULL_TREE;
14178 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored"))
14179 inform (id_loc, "an attribute that appertains to a friend "
14180 "declaration that is not a definition is ignored");
14181 }
14182 /* Friends are treated specially. */
14183 if (ctype == current_class_type)
14184 ; /* We already issued a permerror. */
14185 else if (decl && DECL_NAME (decl))
14186 {
14187 set_originating_module (decl, true);
14188
14189 if (initialized)
14190 /* Kludge: We need funcdef_flag to be true in do_friend for
14191 in-class defaulted functions, but that breaks grokfndecl.
14192 So set it here. */
14193 funcdef_flag = true;
14194
14195 if (template_class_depth (current_class_type) == 0)
14196 {
14197 decl = check_explicit_specialization
14198 (unqualified_id, decl, template_count,
14199 2 * funcdef_flag + 4);
14200 if (decl == error_mark_node)
14201 return error_mark_node;
14202 }
14203
14204 tree scope = ctype ? ctype : in_namespace;
14205 decl = do_friend (scope, unqualified_id, decl,
14206 flags, funcdef_flag);
14207 return decl;
14208 }
14209 else
14210 return error_mark_node;
14211 }
14212
14213 /* Structure field. It may not be a function, except for C++. */
14214
14215 if (decl == NULL_TREE)
14216 {
14217 if (staticp)
14218 {
14219 /* C++ allows static class members. All other work
14220 for this is done by grokfield. */
14221 decl = build_lang_decl_loc (id_loc, VAR_DECL,
14222 unqualified_id, type);
14223 set_linkage_for_static_data_member (decl);
14224 if (concept_p)
14225 error_at (declspecs->locations[ds_concept],
14226 "static data member %qE declared %qs",
14227 unqualified_id, "concept");
14228 else if (constexpr_p && !initialized)
14229 {
14230 error_at (DECL_SOURCE_LOCATION (decl),
14231 "%<constexpr%> static data member %qD must "
14232 "have an initializer", decl);
14233 constexpr_p = false;
14234 }
14235 if (consteval_p)
14236 error_at (declspecs->locations[ds_consteval],
14237 "static data member %qE declared %qs",
14238 unqualified_id, "consteval");
14239
14240 if (inlinep)
14241 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14242
14243 if (!DECL_VAR_DECLARED_INLINE_P (decl)
14244 && !(cxx_dialect >= cxx17 && constexpr_p))
14245 /* Even if there is an in-class initialization, DECL
14246 is considered undefined until an out-of-class
14247 definition is provided, unless this is an inline
14248 variable. */
14249 DECL_EXTERNAL (decl) = 1;
14250
14251 if (thread_p)
14252 {
14253 CP_DECL_THREAD_LOCAL_P (decl) = true;
14254 if (!processing_template_decl)
14255 set_decl_tls_model (decl, decl_default_tls_model (decl));
14256 if (declspecs->gnu_thread_keyword_p)
14257 SET_DECL_GNU_TLS_P (decl);
14258 }
14259
14260 /* Set the constraints on the declaration. */
14261 bool memtmpl = (current_template_depth
14262 > template_class_depth (current_class_type));
14263 if (memtmpl)
14264 {
14265 tree tmpl_reqs
14266 = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
14267 tree ci = build_constraints (tmpl_reqs, NULL_TREE);
14268 set_constraints (decl, ci);
14269 }
14270 }
14271 else
14272 {
14273 if (concept_p)
14274 {
14275 error_at (declspecs->locations[ds_concept],
14276 "non-static data member %qE declared %qs",
14277 unqualified_id, "concept");
14278 concept_p = false;
14279 constexpr_p = false;
14280 }
14281 else if (constexpr_p)
14282 {
14283 error_at (declspecs->locations[ds_constexpr],
14284 "non-static data member %qE declared %qs",
14285 unqualified_id, "constexpr");
14286 constexpr_p = false;
14287 }
14288 if (constinit_p)
14289 {
14290 error_at (declspecs->locations[ds_constinit],
14291 "non-static data member %qE declared %qs",
14292 unqualified_id, "constinit");
14293 constinit_p = false;
14294 }
14295 if (consteval_p)
14296 {
14297 error_at (declspecs->locations[ds_consteval],
14298 "non-static data member %qE declared %qs",
14299 unqualified_id, "consteval");
14300 consteval_p = false;
14301 }
14302 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
14303 DECL_NONADDRESSABLE_P (decl) = bitfield;
14304 if (bitfield && !unqualified_id)
14305 DECL_PADDING_P (decl) = 1;
14306
14307 if (storage_class == sc_mutable)
14308 {
14309 DECL_MUTABLE_P (decl) = 1;
14310 storage_class = sc_none;
14311 }
14312
14313 if (initialized)
14314 {
14315 /* An attempt is being made to initialize a non-static
14316 member. This is new in C++11. */
14317 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc);
14318
14319 /* If this has been parsed with static storage class, but
14320 errors forced staticp to be cleared, ensure NSDMI is
14321 not present. */
14322 if (declspecs->storage_class == sc_static)
14323 DECL_INITIAL (decl) = error_mark_node;
14324 }
14325 }
14326
14327 bad_specifiers (decl, BSP_FIELD, virtualp,
14328 memfn_quals != TYPE_UNQUALIFIED,
14329 staticp ? false : inlinep, friendp,
14330 raises != NULL_TREE,
14331 declspecs->locations);
14332 }
14333 }
14334 else if (FUNC_OR_METHOD_TYPE_P (type))
14335 {
14336 tree original_name;
14337 int publicp = 0;
14338
14339 if (!unqualified_id)
14340 return error_mark_node;
14341
14342 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
14343 original_name = dname;
14344 else
14345 original_name = unqualified_id;
14346 // FIXME:gcc_assert (original_name == dname);
14347
14348 if (storage_class == sc_auto)
14349 error_at (declspecs->locations[ds_storage_class],
14350 "storage class %<auto%> invalid for function %qs", name);
14351 else if (storage_class == sc_register)
14352 error_at (declspecs->locations[ds_storage_class],
14353 "storage class %<register%> invalid for function %qs",
14354 name);
14355 else if (thread_p)
14356 {
14357 if (declspecs->gnu_thread_keyword_p)
14358 error_at (declspecs->locations[ds_thread],
14359 "storage class %<__thread%> invalid for function %qs",
14360 name);
14361 else
14362 error_at (declspecs->locations[ds_thread],
14363 "storage class %<thread_local%> invalid for "
14364 "function %qs", name);
14365 }
14366
14367 if (virt_specifiers)
14368 error ("virt-specifiers in %qs not allowed outside a class "
14369 "definition", name);
14370 /* Function declaration not at top level.
14371 Storage classes other than `extern' are not allowed
14372 and `extern' makes no difference. */
14373 if (! toplevel_bindings_p ()
14374 && (storage_class == sc_static
14375 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
14376 && pedantic)
14377 {
14378 if (storage_class == sc_static)
14379 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
14380 "%<static%> specifier invalid for function %qs "
14381 "declared out of global scope", name);
14382 else
14383 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
14384 "%<inline%> specifier invalid for function %qs "
14385 "declared out of global scope", name);
14386 }
14387
14388 if (ctype == NULL_TREE)
14389 {
14390 if (virtualp)
14391 {
14392 error ("virtual non-class function %qs", name);
14393 virtualp = 0;
14394 }
14395 else if (sfk == sfk_constructor
14396 || sfk == sfk_destructor)
14397 {
14398 error (funcdef_flag
14399 ? G_("%qs defined in a non-class scope")
14400 : G_("%qs declared in a non-class scope"), name);
14401 sfk = sfk_none;
14402 }
14403 }
14404 if (consteval_p
14405 && identifier_p (unqualified_id)
14406 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
14407 {
14408 error_at (declspecs->locations[ds_consteval],
14409 "%qD cannot be %qs", unqualified_id, "consteval");
14410 consteval_p = false;
14411 }
14412
14413 /* Record whether the function is public. */
14414 publicp = (ctype != NULL_TREE
14415 || storage_class != sc_static);
14416
14417 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
14418 declspecs,
14419 reqs, virtualp, flags, memfn_quals, rqual, raises,
14420 1, friendp,
14421 publicp,
14422 inlinep | (2 * constexpr_p) | (4 * concept_p)
14423 | (8 * consteval_p),
14424 initialized == SD_DELETED,
14425 sfk,
14426 funcdef_flag,
14427 late_return_type_p,
14428 template_count, in_namespace, attrlist,
14429 id_loc);
14430 if (decl == NULL_TREE)
14431 return error_mark_node;
14432
14433 if (explicitp == 2)
14434 DECL_NONCONVERTING_P (decl) = 1;
14435 if (staticp == 1)
14436 {
14437 int invalid_static = 0;
14438
14439 /* Don't allow a static member function in a class, and forbid
14440 declaring main to be static. */
14441 if (TREE_CODE (type) == METHOD_TYPE)
14442 {
14443 permerror (input_location, "cannot declare member function %qD to have "
14444 "static linkage", decl);
14445 invalid_static = 1;
14446 }
14447 else if (current_function_decl)
14448 {
14449 /* 7.1.1: There can be no static function declarations within a
14450 block. */
14451 error_at (declspecs->locations[ds_storage_class],
14452 "cannot declare static function inside another function");
14453 invalid_static = 1;
14454 }
14455
14456 if (invalid_static)
14457 {
14458 staticp = 0;
14459 storage_class = sc_none;
14460 }
14461 }
14462 if (declspecs->explicit_specifier)
14463 store_explicit_specifier (decl, declspecs->explicit_specifier);
14464 }
14465 else
14466 {
14467 /* It's a variable. */
14468
14469 /* An uninitialized decl with `extern' is a reference. */
14470 decl = grokvardecl (type, dname, unqualified_id,
14471 declspecs,
14472 initialized,
14473 type_quals,
14474 inlinep,
14475 concept_p,
14476 template_count,
14477 ctype ? ctype : in_namespace,
14478 id_loc);
14479 if (decl == NULL_TREE)
14480 return error_mark_node;
14481
14482 bad_specifiers (decl, BSP_VAR, virtualp,
14483 memfn_quals != TYPE_UNQUALIFIED,
14484 inlinep, friendp, raises != NULL_TREE,
14485 declspecs->locations);
14486
14487 if (ctype)
14488 {
14489 DECL_CONTEXT (decl) = ctype;
14490 if (staticp == 1)
14491 {
14492 permerror (declspecs->locations[ds_storage_class],
14493 "%<static%> may not be used when defining "
14494 "(as opposed to declaring) a static data member");
14495 staticp = 0;
14496 storage_class = sc_none;
14497 }
14498 if (storage_class == sc_register && TREE_STATIC (decl))
14499 {
14500 error ("static member %qD declared %<register%>", decl);
14501 storage_class = sc_none;
14502 }
14503 if (storage_class == sc_extern && pedantic)
14504 {
14505 pedwarn (input_location, OPT_Wpedantic,
14506 "cannot explicitly declare member %q#D to have "
14507 "extern linkage", decl);
14508 storage_class = sc_none;
14509 }
14510 }
14511 else if (constexpr_p && DECL_EXTERNAL (decl))
14512 {
14513 error_at (DECL_SOURCE_LOCATION (decl),
14514 "declaration of %<constexpr%> variable %qD "
14515 "is not a definition", decl);
14516 constexpr_p = false;
14517 }
14518 if (consteval_p)
14519 {
14520 error_at (DECL_SOURCE_LOCATION (decl),
14521 "a variable cannot be declared %<consteval%>");
14522 consteval_p = false;
14523 }
14524
14525 if (inlinep)
14526 mark_inline_variable (decl, declspecs->locations[ds_inline]);
14527 if (innermost_code == cdk_decomp)
14528 {
14529 gcc_assert (declarator && declarator->kind == cdk_decomp);
14530 DECL_SOURCE_LOCATION (decl) = id_loc;
14531 DECL_ARTIFICIAL (decl) = 1;
14532 fit_decomposition_lang_decl (decl, NULL_TREE);
14533 }
14534 }
14535
14536 if (VAR_P (decl) && !initialized)
14537 if (tree auto_node = type_uses_auto (type))
14538 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
14539 {
14540 location_t loc = declspecs->locations[ds_type_spec];
14541 error_at (loc, "declaration of %q#D has no initializer", decl);
14542 TREE_TYPE (decl) = error_mark_node;
14543 }
14544
14545 if (storage_class == sc_extern && initialized && !funcdef_flag)
14546 {
14547 if (toplevel_bindings_p ())
14548 {
14549 /* It's common practice (and completely valid) to have a const
14550 be initialized and declared extern. */
14551 if (!(type_quals & TYPE_QUAL_CONST))
14552 warning_at (DECL_SOURCE_LOCATION (decl), 0,
14553 "%qs initialized and declared %<extern%>", name);
14554 }
14555 else
14556 {
14557 error_at (DECL_SOURCE_LOCATION (decl),
14558 "%qs has both %<extern%> and initializer", name);
14559 return error_mark_node;
14560 }
14561 }
14562
14563 /* Record `register' declaration for warnings on &
14564 and in case doing stupid register allocation. */
14565
14566 if (storage_class == sc_register)
14567 {
14568 DECL_REGISTER (decl) = 1;
14569 /* Warn about register storage specifiers on PARM_DECLs. */
14570 if (TREE_CODE (decl) == PARM_DECL)
14571 {
14572 if (cxx_dialect >= cxx17)
14573 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14574 "ISO C++17 does not allow %<register%> storage "
14575 "class specifier");
14576 else
14577 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
14578 "%<register%> storage class specifier used");
14579 }
14580 }
14581 else if (storage_class == sc_extern)
14582 DECL_THIS_EXTERN (decl) = 1;
14583 else if (storage_class == sc_static)
14584 DECL_THIS_STATIC (decl) = 1;
14585
14586 if (VAR_P (decl))
14587 {
14588 /* Set constexpr flag on vars (functions got it in grokfndecl). */
14589 if (constexpr_p)
14590 DECL_DECLARED_CONSTEXPR_P (decl) = true;
14591 /* And the constinit flag (which only applies to variables). */
14592 else if (constinit_p)
14593 DECL_DECLARED_CONSTINIT_P (decl) = true;
14594 }
14595
14596 /* Record constancy and volatility on the DECL itself . There's
14597 no need to do this when processing a template; we'll do this
14598 for the instantiated declaration based on the type of DECL. */
14599 if (!processing_template_decl)
14600 cp_apply_type_quals_to_decl (type_quals, decl);
14601
14602 return decl;
14603 }
14604 }
14605
14606 /* Subroutine of start_function. Ensure that each of the parameter
14607 types (as listed in PARMS) is complete, as is required for a
14608 function definition. */
14609
14610 static void
require_complete_types_for_parms(tree parms)14611 require_complete_types_for_parms (tree parms)
14612 {
14613 for (; parms; parms = DECL_CHAIN (parms))
14614 {
14615 if (dependent_type_p (TREE_TYPE (parms)))
14616 continue;
14617 if (!VOID_TYPE_P (TREE_TYPE (parms))
14618 && complete_type_or_else (TREE_TYPE (parms), parms))
14619 {
14620 relayout_decl (parms);
14621 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
14622
14623 abstract_virtuals_error (parms, TREE_TYPE (parms));
14624 maybe_warn_parm_abi (TREE_TYPE (parms),
14625 DECL_SOURCE_LOCATION (parms));
14626 }
14627 else
14628 /* grokparms or complete_type_or_else will have already issued
14629 an error. */
14630 TREE_TYPE (parms) = error_mark_node;
14631 }
14632 }
14633
14634 /* Returns nonzero if T is a local variable. */
14635
14636 int
local_variable_p(const_tree t)14637 local_variable_p (const_tree t)
14638 {
14639 if ((VAR_P (t)
14640 && (DECL_LOCAL_DECL_P (t)
14641 || !DECL_CONTEXT (t)
14642 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL))
14643 || (TREE_CODE (t) == PARM_DECL))
14644 return 1;
14645
14646 return 0;
14647 }
14648
14649 /* Like local_variable_p, but suitable for use as a tree-walking
14650 function. */
14651
14652 static tree
local_variable_p_walkfn(tree * tp,int * walk_subtrees,void *)14653 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
14654 void * /*data*/)
14655 {
14656 if (unevaluated_p (TREE_CODE (*tp)))
14657 {
14658 /* DR 2082 permits local variables in unevaluated contexts
14659 within a default argument. */
14660 *walk_subtrees = 0;
14661 return NULL_TREE;
14662 }
14663
14664 if (local_variable_p (*tp)
14665 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
14666 return *tp;
14667 else if (TYPE_P (*tp))
14668 *walk_subtrees = 0;
14669
14670 return NULL_TREE;
14671 }
14672
14673 /* Check that ARG, which is a default-argument expression for a
14674 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
14675 something goes wrong. DECL may also be a _TYPE node, rather than a
14676 DECL, if there is no DECL available. */
14677
14678 tree
check_default_argument(tree decl,tree arg,tsubst_flags_t complain)14679 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
14680 {
14681 tree var;
14682 tree decl_type;
14683
14684 if (TREE_CODE (arg) == DEFERRED_PARSE)
14685 /* We get a DEFERRED_PARSE when looking at an in-class declaration
14686 with a default argument. Ignore the argument for now; we'll
14687 deal with it after the class is complete. */
14688 return arg;
14689
14690 if (TYPE_P (decl))
14691 {
14692 decl_type = decl;
14693 decl = NULL_TREE;
14694 }
14695 else
14696 decl_type = TREE_TYPE (decl);
14697
14698 if (arg == error_mark_node
14699 || decl == error_mark_node
14700 || TREE_TYPE (arg) == error_mark_node
14701 || decl_type == error_mark_node)
14702 /* Something already went wrong. There's no need to check
14703 further. */
14704 return error_mark_node;
14705
14706 /* [dcl.fct.default]
14707
14708 A default argument expression is implicitly converted to the
14709 parameter type. */
14710 ++cp_unevaluated_operand;
14711 /* Avoid digest_init clobbering the initializer. */
14712 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
14713 perform_implicit_conversion_flags (decl_type, carg, complain,
14714 LOOKUP_IMPLICIT);
14715 --cp_unevaluated_operand;
14716
14717 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
14718 the call sites. */
14719 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
14720 && null_ptr_cst_p (arg)
14721 /* Don't lose side-effects as in PR90473. */
14722 && !TREE_SIDE_EFFECTS (arg))
14723 return nullptr_node;
14724
14725 /* [dcl.fct.default]
14726
14727 Local variables shall not be used in default argument
14728 expressions.
14729
14730 The keyword `this' shall not be used in a default argument of a
14731 member function. */
14732 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
14733 if (var)
14734 {
14735 if (complain & tf_warning_or_error)
14736 {
14737 if (DECL_NAME (var) == this_identifier)
14738 permerror (input_location, "default argument %qE uses %qD",
14739 arg, var);
14740 else
14741 error ("default argument %qE uses local variable %qD", arg, var);
14742 }
14743 return error_mark_node;
14744 }
14745
14746 /* All is well. */
14747 return arg;
14748 }
14749
14750 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
14751
14752 static tree
type_is_deprecated(tree type)14753 type_is_deprecated (tree type)
14754 {
14755 enum tree_code code;
14756 if (TREE_DEPRECATED (type))
14757 return type;
14758 if (TYPE_NAME (type))
14759 {
14760 if (TREE_DEPRECATED (TYPE_NAME (type)))
14761 return type;
14762 else
14763 {
14764 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14765 return NULL_TREE;
14766 }
14767 }
14768
14769 /* Do warn about using typedefs to a deprecated class. */
14770 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14771 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14772
14773 code = TREE_CODE (type);
14774
14775 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14776 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14777 || code == METHOD_TYPE || code == ARRAY_TYPE)
14778 return type_is_deprecated (TREE_TYPE (type));
14779
14780 if (TYPE_PTRMEMFUNC_P (type))
14781 return type_is_deprecated
14782 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14783
14784 return NULL_TREE;
14785 }
14786
14787 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */
14788
14789 static tree
type_is_unavailable(tree type)14790 type_is_unavailable (tree type)
14791 {
14792 enum tree_code code;
14793 if (TREE_UNAVAILABLE (type))
14794 return type;
14795 if (TYPE_NAME (type))
14796 {
14797 if (TREE_UNAVAILABLE (TYPE_NAME (type)))
14798 return type;
14799 else
14800 {
14801 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
14802 return NULL_TREE;
14803 }
14804 }
14805
14806 /* Do warn about using typedefs to a deprecated class. */
14807 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
14808 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
14809
14810 code = TREE_CODE (type);
14811
14812 if (code == POINTER_TYPE || code == REFERENCE_TYPE
14813 || code == OFFSET_TYPE || code == FUNCTION_TYPE
14814 || code == METHOD_TYPE || code == ARRAY_TYPE)
14815 return type_is_unavailable (TREE_TYPE (type));
14816
14817 if (TYPE_PTRMEMFUNC_P (type))
14818 return type_is_unavailable
14819 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
14820
14821 return NULL_TREE;
14822 }
14823
14824 /* Decode the list of parameter types for a function type.
14825 Given the list of things declared inside the parens,
14826 return a list of types.
14827
14828 If this parameter does not end with an ellipsis, we append
14829 void_list_node.
14830
14831 *PARMS is set to the chain of PARM_DECLs created. */
14832
14833 tree
grokparms(tree parmlist,tree * parms)14834 grokparms (tree parmlist, tree *parms)
14835 {
14836 tree result = NULL_TREE;
14837 tree decls = NULL_TREE;
14838 tree parm;
14839 int any_error = 0;
14840
14841 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
14842 {
14843 tree type = NULL_TREE;
14844 tree init = TREE_PURPOSE (parm);
14845 tree decl = TREE_VALUE (parm);
14846
14847 if (parm == void_list_node || parm == explicit_void_list_node)
14848 break;
14849
14850 if (! decl || TREE_TYPE (decl) == error_mark_node)
14851 {
14852 any_error = 1;
14853 continue;
14854 }
14855
14856 type = TREE_TYPE (decl);
14857 if (VOID_TYPE_P (type))
14858 {
14859 if (same_type_p (type, void_type_node)
14860 && !init
14861 && !DECL_NAME (decl) && !result
14862 && TREE_CHAIN (parm) == void_list_node)
14863 /* DR 577: A parameter list consisting of a single
14864 unnamed parameter of non-dependent type 'void'. */
14865 break;
14866 else if (cv_qualified_p (type))
14867 error_at (DECL_SOURCE_LOCATION (decl),
14868 "invalid use of cv-qualified type %qT in "
14869 "parameter declaration", type);
14870 else
14871 error_at (DECL_SOURCE_LOCATION (decl),
14872 "invalid use of type %<void%> in parameter "
14873 "declaration");
14874 /* It's not a good idea to actually create parameters of
14875 type `void'; other parts of the compiler assume that a
14876 void type terminates the parameter list. */
14877 type = error_mark_node;
14878 TREE_TYPE (decl) = error_mark_node;
14879 }
14880
14881 if (type != error_mark_node)
14882 {
14883 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14884 {
14885 tree unavailtype = type_is_unavailable (type);
14886 if (unavailtype)
14887 cp_handle_deprecated_or_unavailable (unavailtype);
14888 }
14889 if (deprecated_state != DEPRECATED_SUPPRESS
14890 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
14891 {
14892 tree deptype = type_is_deprecated (type);
14893 if (deptype)
14894 cp_handle_deprecated_or_unavailable (deptype);
14895 }
14896
14897 /* [dcl.fct] "A parameter with volatile-qualified type is
14898 deprecated." */
14899 if (CP_TYPE_VOLATILE_P (type))
14900 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
14901 "%<volatile%>-qualified parameter is "
14902 "deprecated");
14903
14904 /* Top-level qualifiers on the parameters are
14905 ignored for function types. */
14906 type = cp_build_qualified_type (type, 0);
14907 if (TREE_CODE (type) == METHOD_TYPE)
14908 {
14909 error ("parameter %qD invalidly declared method type", decl);
14910 type = build_pointer_type (type);
14911 TREE_TYPE (decl) = type;
14912 }
14913 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
14914 {
14915 /* Before C++17 DR 393:
14916 [dcl.fct]/6, parameter types cannot contain pointers
14917 (references) to arrays of unknown bound. */
14918 tree t = TREE_TYPE (type);
14919 int ptr = TYPE_PTR_P (type);
14920
14921 while (1)
14922 {
14923 if (TYPE_PTR_P (t))
14924 ptr = 1;
14925 else if (TREE_CODE (t) != ARRAY_TYPE)
14926 break;
14927 else if (!TYPE_DOMAIN (t))
14928 break;
14929 t = TREE_TYPE (t);
14930 }
14931 if (TREE_CODE (t) == ARRAY_TYPE)
14932 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
14933 ptr
14934 ? G_("parameter %qD includes pointer to array of "
14935 "unknown bound %qT")
14936 : G_("parameter %qD includes reference to array of "
14937 "unknown bound %qT"),
14938 decl, t);
14939 }
14940
14941 if (init && !processing_template_decl)
14942 init = check_default_argument (decl, init, tf_warning_or_error);
14943 }
14944
14945 DECL_CHAIN (decl) = decls;
14946 decls = decl;
14947 result = tree_cons (init, type, result);
14948 }
14949 decls = nreverse (decls);
14950 result = nreverse (result);
14951 if (parm)
14952 result = chainon (result, void_list_node);
14953 *parms = decls;
14954 if (any_error)
14955 result = NULL_TREE;
14956
14957 if (any_error)
14958 /* We had parm errors, recover by giving the function (...) type. */
14959 result = NULL_TREE;
14960
14961 return result;
14962 }
14963
14964
14965 /* D is a constructor or overloaded `operator='.
14966
14967 Let T be the class in which D is declared. Then, this function
14968 returns:
14969
14970 -1 if D's is an ill-formed constructor or copy assignment operator
14971 whose first parameter is of type `T'.
14972 0 if D is not a copy constructor or copy assignment
14973 operator.
14974 1 if D is a copy constructor or copy assignment operator whose
14975 first parameter is a reference to non-const qualified T.
14976 2 if D is a copy constructor or copy assignment operator whose
14977 first parameter is a reference to const qualified T.
14978
14979 This function can be used as a predicate. Positive values indicate
14980 a copy constructor and nonzero values indicate a copy assignment
14981 operator. */
14982
14983 int
copy_fn_p(const_tree d)14984 copy_fn_p (const_tree d)
14985 {
14986 tree args;
14987 tree arg_type;
14988 int result = 1;
14989
14990 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14991
14992 if (TREE_CODE (d) == TEMPLATE_DECL
14993 || (DECL_TEMPLATE_INFO (d)
14994 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14995 /* Instantiations of template member functions are never copy
14996 functions. Note that member functions of templated classes are
14997 represented as template functions internally, and we must
14998 accept those as copy functions. */
14999 return 0;
15000
15001 if (!DECL_CONSTRUCTOR_P (d)
15002 && DECL_NAME (d) != assign_op_identifier)
15003 return 0;
15004
15005 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15006 if (!args)
15007 return 0;
15008
15009 arg_type = TREE_VALUE (args);
15010 if (arg_type == error_mark_node)
15011 return 0;
15012
15013 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
15014 {
15015 /* Pass by value copy assignment operator. */
15016 result = -1;
15017 }
15018 else if (TYPE_REF_P (arg_type)
15019 && !TYPE_REF_IS_RVALUE (arg_type)
15020 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
15021 {
15022 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
15023 result = 2;
15024 }
15025 else
15026 return 0;
15027
15028 args = TREE_CHAIN (args);
15029
15030 if (args && args != void_list_node && !TREE_PURPOSE (args))
15031 /* There are more non-optional args. */
15032 return 0;
15033
15034 return result;
15035 }
15036
15037 /* D is a constructor or overloaded `operator='.
15038
15039 Let T be the class in which D is declared. Then, this function
15040 returns true when D is a move constructor or move assignment
15041 operator, false otherwise. */
15042
15043 bool
move_fn_p(const_tree d)15044 move_fn_p (const_tree d)
15045 {
15046 if (cxx_dialect == cxx98)
15047 /* There are no move constructors if we are in C++98 mode. */
15048 return false;
15049
15050 if (TREE_CODE (d) == TEMPLATE_DECL
15051 || (DECL_TEMPLATE_INFO (d)
15052 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
15053 /* Instantiations of template member functions are never move
15054 functions. Note that member functions of templated classes are
15055 represented as template functions internally, and we must
15056 accept those as move functions. */
15057 return 0;
15058
15059 return move_signature_fn_p (d);
15060 }
15061
15062 /* D is a constructor or overloaded `operator='.
15063
15064 Then, this function returns true when D has the same signature as a move
15065 constructor or move assignment operator (because either it is such a
15066 ctor/op= or it is a template specialization with the same signature),
15067 false otherwise. */
15068
15069 bool
move_signature_fn_p(const_tree d)15070 move_signature_fn_p (const_tree d)
15071 {
15072 tree args;
15073 tree arg_type;
15074 bool result = false;
15075
15076 if (!DECL_CONSTRUCTOR_P (d)
15077 && DECL_NAME (d) != assign_op_identifier)
15078 return 0;
15079
15080 args = FUNCTION_FIRST_USER_PARMTYPE (d);
15081 if (!args)
15082 return 0;
15083
15084 arg_type = TREE_VALUE (args);
15085 if (arg_type == error_mark_node)
15086 return 0;
15087
15088 if (TYPE_REF_P (arg_type)
15089 && TYPE_REF_IS_RVALUE (arg_type)
15090 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
15091 DECL_CONTEXT (d)))
15092 result = true;
15093
15094 args = TREE_CHAIN (args);
15095
15096 if (args && args != void_list_node && !TREE_PURPOSE (args))
15097 /* There are more non-optional args. */
15098 return false;
15099
15100 return result;
15101 }
15102
15103 /* Remember any special properties of member function DECL. */
15104
15105 void
grok_special_member_properties(tree decl)15106 grok_special_member_properties (tree decl)
15107 {
15108 tree class_type;
15109
15110 if (TREE_CODE (decl) == USING_DECL
15111 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
15112 return;
15113
15114 class_type = DECL_CONTEXT (decl);
15115 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
15116 {
15117 int ctor = copy_fn_p (decl);
15118
15119 if (!DECL_ARTIFICIAL (decl))
15120 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
15121
15122 if (ctor > 0)
15123 {
15124 /* [class.copy]
15125
15126 A non-template constructor for class X is a copy
15127 constructor if its first parameter is of type X&, const
15128 X&, volatile X& or const volatile X&, and either there
15129 are no other parameters or else all other parameters have
15130 default arguments. */
15131 TYPE_HAS_COPY_CTOR (class_type) = 1;
15132 if (ctor > 1)
15133 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
15134 }
15135
15136 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
15137 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
15138
15139 if (is_list_ctor (decl))
15140 TYPE_HAS_LIST_CTOR (class_type) = 1;
15141
15142 if (maybe_constexpr_fn (decl)
15143 && !ctor && !move_fn_p (decl))
15144 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
15145 }
15146 else if (DECL_NAME (decl) == assign_op_identifier)
15147 {
15148 /* [class.copy]
15149
15150 A non-template assignment operator for class X is a copy
15151 assignment operator if its parameter is of type X, X&, const
15152 X&, volatile X& or const volatile X&. */
15153
15154 int assop = copy_fn_p (decl);
15155
15156 if (assop)
15157 {
15158 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
15159 if (assop != 1)
15160 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
15161 }
15162 }
15163 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
15164 TYPE_HAS_CONVERSION (class_type) = true;
15165
15166 /* Destructors are handled in check_methods. */
15167 }
15168
15169 /* Check a constructor DECL has the correct form. Complains
15170 if the class has a constructor of the form X(X). */
15171
15172 bool
grok_ctor_properties(const_tree ctype,const_tree decl)15173 grok_ctor_properties (const_tree ctype, const_tree decl)
15174 {
15175 int ctor_parm = copy_fn_p (decl);
15176
15177 if (ctor_parm < 0)
15178 {
15179 /* [class.copy]
15180
15181 A declaration of a constructor for a class X is ill-formed if
15182 its first parameter is of type (optionally cv-qualified) X
15183 and either there are no other parameters or else all other
15184 parameters have default arguments.
15185
15186 We *don't* complain about member template instantiations that
15187 have this form, though; they can occur as we try to decide
15188 what constructor to use during overload resolution. Since
15189 overload resolution will never prefer such a constructor to
15190 the non-template copy constructor (which is either explicitly
15191 or implicitly defined), there's no need to worry about their
15192 existence. Theoretically, they should never even be
15193 instantiated, but that's hard to forestall. */
15194 error_at (DECL_SOURCE_LOCATION (decl),
15195 "invalid constructor; you probably meant %<%T (const %T&)%>",
15196 ctype, ctype);
15197 return false;
15198 }
15199
15200 return true;
15201 }
15202
15203 /* DECL is a declaration for an overloaded or conversion operator. If
15204 COMPLAIN is true, errors are issued for invalid declarations. */
15205
15206 bool
grok_op_properties(tree decl,bool complain)15207 grok_op_properties (tree decl, bool complain)
15208 {
15209 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
15210 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
15211 tree name = DECL_NAME (decl);
15212 location_t loc = DECL_SOURCE_LOCATION (decl);
15213
15214 tree class_type = DECL_CONTEXT (decl);
15215 if (class_type && !CLASS_TYPE_P (class_type))
15216 class_type = NULL_TREE;
15217
15218 tree_code operator_code;
15219 unsigned op_flags;
15220 if (IDENTIFIER_CONV_OP_P (name))
15221 {
15222 /* Conversion operators are TYPE_EXPR for the purposes of this
15223 function. */
15224 operator_code = TYPE_EXPR;
15225 op_flags = OVL_OP_FLAG_UNARY;
15226 }
15227 else
15228 {
15229 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
15230
15231 operator_code = ovl_op->tree_code;
15232 op_flags = ovl_op->flags;
15233 gcc_checking_assert (operator_code != ERROR_MARK);
15234 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15235 }
15236
15237 if (op_flags & OVL_OP_FLAG_ALLOC)
15238 {
15239 /* operator new and operator delete are quite special. */
15240 if (class_type)
15241 switch (op_flags)
15242 {
15243 case OVL_OP_FLAG_ALLOC:
15244 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
15245 break;
15246
15247 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
15248 TYPE_GETS_DELETE (class_type) |= 1;
15249 break;
15250
15251 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
15252 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
15253 break;
15254
15255 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
15256 TYPE_GETS_DELETE (class_type) |= 2;
15257 break;
15258
15259 default:
15260 gcc_unreachable ();
15261 }
15262
15263 /* [basic.std.dynamic.allocation]/1:
15264
15265 A program is ill-formed if an allocation function is declared
15266 in a namespace scope other than global scope or declared
15267 static in global scope.
15268
15269 The same also holds true for deallocation functions. */
15270 if (DECL_NAMESPACE_SCOPE_P (decl))
15271 {
15272 if (CP_DECL_CONTEXT (decl) != global_namespace)
15273 {
15274 error_at (loc, "%qD may not be declared within a namespace",
15275 decl);
15276 return false;
15277 }
15278
15279 if (!TREE_PUBLIC (decl))
15280 {
15281 error_at (loc, "%qD may not be declared as static", decl);
15282 return false;
15283 }
15284 }
15285
15286 if (op_flags & OVL_OP_FLAG_DELETE)
15287 {
15288 DECL_SET_IS_OPERATOR_DELETE (decl, true);
15289 coerce_delete_type (decl, loc);
15290 }
15291 else
15292 {
15293 DECL_SET_IS_OPERATOR_NEW (decl, true);
15294 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
15295 }
15296
15297 return true;
15298 }
15299
15300 /* An operator function must either be a non-static member function
15301 or have at least one parameter of a class, a reference to a class,
15302 an enumeration, or a reference to an enumeration. 13.4.0.6 */
15303 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
15304 {
15305 if (operator_code == TYPE_EXPR
15306 || operator_code == CALL_EXPR
15307 || operator_code == COMPONENT_REF
15308 || operator_code == ARRAY_REF
15309 || operator_code == NOP_EXPR)
15310 {
15311 error_at (loc, "%qD must be a non-static member function", decl);
15312 return false;
15313 }
15314
15315 if (DECL_STATIC_FUNCTION_P (decl))
15316 {
15317 error_at (loc, "%qD must be either a non-static member "
15318 "function or a non-member function", decl);
15319 return false;
15320 }
15321
15322 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
15323 {
15324 if (!arg || arg == void_list_node)
15325 {
15326 if (complain)
15327 error_at(loc, "%qD must have an argument of class or "
15328 "enumerated type", decl);
15329 return false;
15330 }
15331
15332 tree type = non_reference (TREE_VALUE (arg));
15333 if (type == error_mark_node)
15334 return false;
15335
15336 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
15337 because these checks are performed even on template
15338 functions. */
15339 if (MAYBE_CLASS_TYPE_P (type)
15340 || TREE_CODE (type) == ENUMERAL_TYPE)
15341 break;
15342 }
15343 }
15344
15345 if (operator_code == CALL_EXPR)
15346 /* There are no further restrictions on the arguments to an overloaded
15347 "operator ()". */
15348 return true;
15349
15350 if (operator_code == COND_EXPR)
15351 {
15352 /* 13.4.0.3 */
15353 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
15354 return false;
15355 }
15356
15357 /* Count the number of arguments and check for ellipsis. */
15358 int arity = 0;
15359 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
15360 {
15361 if (!arg)
15362 {
15363 /* Variadic. */
15364 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15365 break;
15366
15367 error_at (loc, "%qD must not have variable number of arguments",
15368 decl);
15369 return false;
15370 }
15371 ++arity;
15372 }
15373
15374 /* Verify correct number of arguments. */
15375 switch (op_flags)
15376 {
15377 case OVL_OP_FLAG_AMBIARY:
15378 if (arity == 1)
15379 {
15380 /* We have a unary instance of an ambi-ary op. Remap to the
15381 unary one. */
15382 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
15383 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
15384 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
15385 operator_code = ovl_op->tree_code;
15386 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
15387 }
15388 else if (arity != 2)
15389 {
15390 /* This was an ambiguous operator but is invalid. */
15391 error_at (loc,
15392 methodp
15393 ? G_("%qD must have either zero or one argument")
15394 : G_("%qD must have either one or two arguments"), decl);
15395 return false;
15396 }
15397 else if ((operator_code == POSTINCREMENT_EXPR
15398 || operator_code == POSTDECREMENT_EXPR)
15399 && ! processing_template_decl
15400 /* x++ and x--'s second argument must be an int. */
15401 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
15402 integer_type_node))
15403 {
15404 error_at (loc,
15405 methodp
15406 ? G_("postfix %qD must have %<int%> as its argument")
15407 : G_("postfix %qD must have %<int%> as its second argument"),
15408 decl);
15409 return false;
15410 }
15411 break;
15412
15413 case OVL_OP_FLAG_UNARY:
15414 if (arity != 1)
15415 {
15416 error_at (loc,
15417 methodp
15418 ? G_("%qD must have no arguments")
15419 : G_("%qD must have exactly one argument"), decl);
15420 return false;
15421 }
15422 break;
15423
15424 case OVL_OP_FLAG_BINARY:
15425 if (arity != 2)
15426 {
15427 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23)
15428 break;
15429 error_at (loc,
15430 methodp
15431 ? G_("%qD must have exactly one argument")
15432 : G_("%qD must have exactly two arguments"), decl);
15433 return false;
15434 }
15435 break;
15436
15437 default:
15438 gcc_unreachable ();
15439 }
15440
15441 /* There can be no default arguments. */
15442 for (tree arg = argtypes; arg && arg != void_list_node;
15443 arg = TREE_CHAIN (arg))
15444 if (TREE_PURPOSE (arg))
15445 {
15446 TREE_PURPOSE (arg) = NULL_TREE;
15447 error_at (loc, "%qD cannot have default arguments", decl);
15448 return false;
15449 }
15450
15451 /* At this point the declaration is well-formed. It may not be
15452 sensible though. */
15453
15454 /* Check member function warnings only on the in-class declaration.
15455 There's no point warning on an out-of-class definition. */
15456 if (class_type && class_type != current_class_type)
15457 return true;
15458
15459 /* Warn about conversion operators that will never be used. */
15460 if (IDENTIFIER_CONV_OP_P (name)
15461 && ! DECL_TEMPLATE_INFO (decl)
15462 && warn_class_conversion)
15463 {
15464 tree t = TREE_TYPE (name);
15465 int ref = TYPE_REF_P (t);
15466
15467 if (ref)
15468 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
15469
15470 if (VOID_TYPE_P (t))
15471 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
15472 "will never use a type conversion operator", class_type);
15473 else if (class_type)
15474 {
15475 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
15476 warning_at (loc, OPT_Wclass_conversion,
15477 ref
15478 ? G_("converting %qT to a reference to the same type "
15479 "will never use a type conversion operator")
15480 : G_("converting %qT to the same type "
15481 "will never use a type conversion operator"),
15482 class_type);
15483 /* Don't force t to be complete here. */
15484 else if (MAYBE_CLASS_TYPE_P (t)
15485 && COMPLETE_TYPE_P (t)
15486 && DERIVED_FROM_P (t, class_type))
15487 warning_at (loc, OPT_Wclass_conversion,
15488 ref
15489 ? G_("converting %qT to a reference to a base class "
15490 "%qT will never use a type conversion operator")
15491 : G_("converting %qT to a base class %qT "
15492 "will never use a type conversion operator"),
15493 class_type, t);
15494 }
15495 }
15496
15497 if (!warn_ecpp)
15498 return true;
15499
15500 /* Effective C++ rules below. */
15501
15502 /* More Effective C++ rule 7. */
15503 if (operator_code == TRUTH_ANDIF_EXPR
15504 || operator_code == TRUTH_ORIF_EXPR
15505 || operator_code == COMPOUND_EXPR)
15506 warning_at (loc, OPT_Weffc__,
15507 "user-defined %qD always evaluates both arguments", decl);
15508
15509 /* More Effective C++ rule 6. */
15510 if (operator_code == POSTINCREMENT_EXPR
15511 || operator_code == POSTDECREMENT_EXPR
15512 || operator_code == PREINCREMENT_EXPR
15513 || operator_code == PREDECREMENT_EXPR)
15514 {
15515 tree arg = TREE_VALUE (argtypes);
15516 tree ret = TREE_TYPE (TREE_TYPE (decl));
15517 if (methodp || TYPE_REF_P (arg))
15518 arg = TREE_TYPE (arg);
15519 arg = TYPE_MAIN_VARIANT (arg);
15520
15521 if (operator_code == PREINCREMENT_EXPR
15522 || operator_code == PREDECREMENT_EXPR)
15523 {
15524 if (!TYPE_REF_P (ret)
15525 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
15526 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
15527 build_reference_type (arg));
15528 }
15529 else
15530 {
15531 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
15532 warning_at (loc, OPT_Weffc__,
15533 "postfix %qD should return %qT", decl, arg);
15534 }
15535 }
15536
15537 /* Effective C++ rule 23. */
15538 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
15539 && (operator_code == PLUS_EXPR
15540 || operator_code == MINUS_EXPR
15541 || operator_code == TRUNC_DIV_EXPR
15542 || operator_code == MULT_EXPR
15543 || operator_code == TRUNC_MOD_EXPR)
15544 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
15545 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
15546
15547 return true;
15548 }
15549
15550 /* Return a string giving the keyword associate with CODE. */
15551
15552 static const char *
tag_name(enum tag_types code)15553 tag_name (enum tag_types code)
15554 {
15555 switch (code)
15556 {
15557 case record_type:
15558 return "struct";
15559 case class_type:
15560 return "class";
15561 case union_type:
15562 return "union";
15563 case enum_type:
15564 return "enum";
15565 case typename_type:
15566 return "typename";
15567 default:
15568 gcc_unreachable ();
15569 }
15570 }
15571
15572 /* Name lookup in an elaborated-type-specifier (after the keyword
15573 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
15574 elaborated-type-specifier is invalid, issue a diagnostic and return
15575 error_mark_node; otherwise, return the *_TYPE to which it referred.
15576 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
15577
15578 tree
check_elaborated_type_specifier(enum tag_types tag_code,tree decl,bool allow_template_p)15579 check_elaborated_type_specifier (enum tag_types tag_code,
15580 tree decl,
15581 bool allow_template_p)
15582 {
15583 tree type;
15584
15585 /* In the case of:
15586
15587 struct S { struct S *p; };
15588
15589 name lookup will find the TYPE_DECL for the implicit "S::S"
15590 typedef. Adjust for that here. */
15591 if (DECL_SELF_REFERENCE_P (decl))
15592 decl = TYPE_NAME (TREE_TYPE (decl));
15593
15594 type = TREE_TYPE (decl);
15595
15596 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
15597 is false for this case as well. */
15598 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15599 {
15600 error ("using template type parameter %qT after %qs",
15601 type, tag_name (tag_code));
15602 return error_mark_node;
15603 }
15604 /* Accept template template parameters. */
15605 else if (allow_template_p
15606 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
15607 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
15608 ;
15609 /* [dcl.type.elab]
15610
15611 If the identifier resolves to a typedef-name or the
15612 simple-template-id resolves to an alias template
15613 specialization, the elaborated-type-specifier is ill-formed.
15614
15615 In other words, the only legitimate declaration to use in the
15616 elaborated type specifier is the implicit typedef created when
15617 the type is declared. */
15618 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
15619 && !DECL_SELF_REFERENCE_P (decl)
15620 && tag_code != typename_type)
15621 {
15622 if (alias_template_specialization_p (type, nt_opaque))
15623 error ("using alias template specialization %qT after %qs",
15624 type, tag_name (tag_code));
15625 else
15626 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
15627 inform (DECL_SOURCE_LOCATION (decl),
15628 "%qD has a previous declaration here", decl);
15629 return error_mark_node;
15630 }
15631 else if (TREE_CODE (type) != RECORD_TYPE
15632 && TREE_CODE (type) != UNION_TYPE
15633 && tag_code != enum_type
15634 && tag_code != typename_type)
15635 {
15636 error ("%qT referred to as %qs", type, tag_name (tag_code));
15637 inform (location_of (type), "%qT has a previous declaration here", type);
15638 return error_mark_node;
15639 }
15640 else if (TREE_CODE (type) != ENUMERAL_TYPE
15641 && tag_code == enum_type)
15642 {
15643 error ("%qT referred to as enum", type);
15644 inform (location_of (type), "%qT has a previous declaration here", type);
15645 return error_mark_node;
15646 }
15647 else if (!allow_template_p
15648 && TREE_CODE (type) == RECORD_TYPE
15649 && CLASSTYPE_IS_TEMPLATE (type))
15650 {
15651 /* If a class template appears as elaborated type specifier
15652 without a template header such as:
15653
15654 template <class T> class C {};
15655 void f(class C); // No template header here
15656
15657 then the required template argument is missing. */
15658 error ("template argument required for %<%s %T%>",
15659 tag_name (tag_code),
15660 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
15661 return error_mark_node;
15662 }
15663
15664 return type;
15665 }
15666
15667 /* Lookup NAME of an elaborated type specifier according to SCOPE and
15668 issue diagnostics if necessary. Return *_TYPE node upon success,
15669 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type
15670 error. */
15671
15672 static tree
lookup_and_check_tag(enum tag_types tag_code,tree name,TAG_how how,bool template_header_p)15673 lookup_and_check_tag (enum tag_types tag_code, tree name,
15674 TAG_how how, bool template_header_p)
15675 {
15676 tree decl;
15677 if (how == TAG_how::GLOBAL)
15678 {
15679 /* First try ordinary name lookup, ignoring hidden class name
15680 injected via friend declaration. */
15681 decl = lookup_name (name, LOOK_want::TYPE);
15682 decl = strip_using_decl (decl);
15683 /* If that fails, the name will be placed in the smallest
15684 non-class, non-function-prototype scope according to 3.3.1/5.
15685 We may already have a hidden name declared as friend in this
15686 scope. So lookup again but not ignoring hidden names.
15687 If we find one, that name will be made visible rather than
15688 creating a new tag. */
15689 if (!decl)
15690 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS);
15691 }
15692 else
15693 decl = lookup_elaborated_type (name, how);
15694
15695 if (!decl)
15696 /* We found nothing. */
15697 return NULL_TREE;
15698
15699 if (TREE_CODE (decl) == TREE_LIST)
15700 {
15701 error ("reference to %qD is ambiguous", name);
15702 print_candidates (decl);
15703 return error_mark_node;
15704 }
15705
15706 if (DECL_CLASS_TEMPLATE_P (decl)
15707 && !template_header_p
15708 && how == TAG_how::CURRENT_ONLY)
15709 {
15710 error ("class template %qD redeclared as non-template", name);
15711 inform (location_of (decl), "previous declaration here");
15712 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true;
15713 return error_mark_node;
15714 }
15715
15716 if (DECL_CLASS_TEMPLATE_P (decl)
15717 /* If scope is TAG_how::CURRENT_ONLY we're defining a class,
15718 so ignore a template template parameter. */
15719 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl)))
15720 decl = DECL_TEMPLATE_RESULT (decl);
15721
15722 if (TREE_CODE (decl) != TYPE_DECL)
15723 /* Found not-a-type. */
15724 return NULL_TREE;
15725
15726 /* Look for invalid nested type:
15727 class C {
15728 class C {};
15729 }; */
15730 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl))
15731 {
15732 error ("%qD has the same name as the class in which it is "
15733 "declared", decl);
15734 return error_mark_node;
15735 }
15736
15737 /* Two cases we need to consider when deciding if a class
15738 template is allowed as an elaborated type specifier:
15739 1. It is a self reference to its own class.
15740 2. It comes with a template header.
15741
15742 For example:
15743
15744 template <class T> class C {
15745 class C *c1; // DECL_SELF_REFERENCE_P is true
15746 class D;
15747 };
15748 template <class U> class C; // template_header_p is true
15749 template <class T> class C<T>::D {
15750 class C *c2; // DECL_SELF_REFERENCE_P is true
15751 }; */
15752
15753 tree t = check_elaborated_type_specifier (tag_code, decl,
15754 template_header_p
15755 | DECL_SELF_REFERENCE_P (decl));
15756 if (template_header_p && t && CLASS_TYPE_P (t)
15757 && (!CLASSTYPE_TEMPLATE_INFO (t)
15758 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
15759 {
15760 error ("%qT is not a template", t);
15761 inform (location_of (t), "previous declaration here");
15762 if (TYPE_CLASS_SCOPE_P (t)
15763 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
15764 inform (input_location,
15765 "perhaps you want to explicitly add %<%T::%>",
15766 TYPE_CONTEXT (t));
15767 return error_mark_node;
15768 }
15769
15770 return t;
15771 }
15772
15773 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
15774 Define the tag as a forward-reference if it is not defined.
15775
15776 If a declaration is given, process it here, and report an error if
15777 multiple declarations are not identical.
15778
15779 SCOPE is TS_CURRENT when this is also a definition. Only look in
15780 the current frame for the name (since C++ allows new names in any
15781 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
15782 declaration. Only look beginning from the current scope outward up
15783 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
15784
15785 TEMPLATE_HEADER_P is true when this declaration is preceded by
15786 a set of template parameters. */
15787
15788 tree
xref_tag(enum tag_types tag_code,tree name,TAG_how how,bool template_header_p)15789 xref_tag (enum tag_types tag_code, tree name,
15790 TAG_how how, bool template_header_p)
15791 {
15792 enum tree_code code;
15793 tree context = NULL_TREE;
15794
15795 auto_cond_timevar tv (TV_NAME_LOOKUP);
15796
15797 gcc_assert (identifier_p (name));
15798
15799 switch (tag_code)
15800 {
15801 case record_type:
15802 case class_type:
15803 code = RECORD_TYPE;
15804 break;
15805 case union_type:
15806 code = UNION_TYPE;
15807 break;
15808 case enum_type:
15809 code = ENUMERAL_TYPE;
15810 break;
15811 default:
15812 gcc_unreachable ();
15813 }
15814
15815 /* In case of anonymous name, xref_tag is only called to
15816 make type node and push name. Name lookup is not required. */
15817 tree t = NULL_TREE;
15818 if (!IDENTIFIER_ANON_P (name))
15819 t = lookup_and_check_tag (tag_code, name, how, template_header_p);
15820
15821 if (t == error_mark_node)
15822 return error_mark_node;
15823
15824 if (how != TAG_how::CURRENT_ONLY && t && current_class_type
15825 && template_class_depth (current_class_type)
15826 && template_header_p)
15827 {
15828 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
15829 return t;
15830
15831 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at
15832 a definition of this tag. Since, in addition, we are
15833 currently processing a (member) template declaration of a
15834 template class, we must be very careful; consider:
15835
15836 template <class X> struct S1
15837
15838 template <class U> struct S2
15839 {
15840 template <class V> friend struct S1;
15841 };
15842
15843 Here, the S2::S1 declaration should not be confused with the
15844 outer declaration. In particular, the inner version should
15845 have a template parameter of level 2, not level 1.
15846
15847 On the other hand, when presented with:
15848
15849 template <class T> struct S1
15850 {
15851 template <class U> struct S2 {};
15852 template <class U> friend struct S2;
15853 };
15854
15855 the friend must find S1::S2 eventually. We accomplish this
15856 by making sure that the new type we create to represent this
15857 declaration has the right TYPE_CONTEXT. */
15858 context = TYPE_CONTEXT (t);
15859 t = NULL_TREE;
15860 }
15861
15862 if (! t)
15863 {
15864 /* If no such tag is yet defined, create a forward-reference node
15865 and record it as the "definition".
15866 When a real declaration of this type is found,
15867 the forward-reference will be altered into a real type. */
15868 if (code == ENUMERAL_TYPE)
15869 {
15870 error ("use of enum %q#D without previous declaration", name);
15871 return error_mark_node;
15872 }
15873
15874 t = make_class_type (code);
15875 TYPE_CONTEXT (t) = context;
15876 if (IDENTIFIER_LAMBDA_P (name))
15877 /* Mark it as a lambda type right now. Our caller will
15878 correct the value. */
15879 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
15880 t = pushtag (name, t, how);
15881 }
15882 else
15883 {
15884 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
15885 {
15886 /* Check that we aren't trying to overload a class with different
15887 constraints. */
15888 tree constr = NULL_TREE;
15889 if (current_template_parms)
15890 {
15891 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
15892 constr = build_constraints (reqs, NULL_TREE);
15893 }
15894 if (!redeclare_class_template (t, current_template_parms, constr))
15895 return error_mark_node;
15896 }
15897 else if (!processing_template_decl
15898 && CLASS_TYPE_P (t)
15899 && CLASSTYPE_IS_TEMPLATE (t))
15900 {
15901 error ("redeclaration of %qT as a non-template", t);
15902 inform (location_of (t), "previous declaration %qD", t);
15903 return error_mark_node;
15904 }
15905
15906 if (modules_p ()
15907 && how == TAG_how::CURRENT_ONLY)
15908 {
15909 tree decl = TYPE_NAME (t);
15910 if (!module_may_redeclare (decl))
15911 {
15912 error ("cannot declare %qD in a different module", decl);
15913 inform (DECL_SOURCE_LOCATION (decl), "declared here");
15914 return error_mark_node;
15915 }
15916
15917 tree maybe_tmpl = decl;
15918 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t))
15919 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t);
15920
15921 if (DECL_LANG_SPECIFIC (decl)
15922 && DECL_MODULE_IMPORT_P (decl)
15923 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15924 {
15925 /* Push it into this TU's symbol slot. */
15926 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl));
15927 if (maybe_tmpl != decl)
15928 /* We're in the template parm binding level.
15929 Pushtag has logic to slide under that, but we're
15930 not pushing a *new* type. */
15931 push_nested_namespace (CP_DECL_CONTEXT (decl));
15932
15933 pushdecl (maybe_tmpl);
15934 if (maybe_tmpl != decl)
15935 pop_nested_namespace (CP_DECL_CONTEXT (decl));
15936 }
15937
15938 set_instantiating_module (maybe_tmpl);
15939 }
15940 }
15941
15942 return t;
15943 }
15944
15945 /* Create the binfo hierarchy for REF with (possibly NULL) base list
15946 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
15947 access_* node, and the TREE_VALUE is the type of the base-class.
15948 Non-NULL TREE_TYPE indicates virtual inheritance. */
15949
15950 void
xref_basetypes(tree ref,tree base_list)15951 xref_basetypes (tree ref, tree base_list)
15952 {
15953 tree *basep;
15954 tree binfo, base_binfo;
15955 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
15956 unsigned max_bases = 0; /* Maximum direct bases. */
15957 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
15958 int i;
15959 tree default_access;
15960 tree igo_prev; /* Track Inheritance Graph Order. */
15961
15962 if (ref == error_mark_node)
15963 return;
15964
15965 /* The base of a derived class is private by default, all others are
15966 public. */
15967 default_access = (TREE_CODE (ref) == RECORD_TYPE
15968 && CLASSTYPE_DECLARED_CLASS (ref)
15969 ? access_private_node : access_public_node);
15970
15971 /* First, make sure that any templates in base-classes are
15972 instantiated. This ensures that if we call ourselves recursively
15973 we do not get confused about which classes are marked and which
15974 are not. */
15975 basep = &base_list;
15976 while (*basep)
15977 {
15978 tree basetype = TREE_VALUE (*basep);
15979
15980 /* The dependent_type_p call below should really be dependent_scope_p
15981 so that we give a hard error about using an incomplete type as a
15982 base, but we allow it with a pedwarn for backward
15983 compatibility. */
15984 if (processing_template_decl
15985 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15986 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15987 if (!dependent_type_p (basetype)
15988 && !complete_type_or_else (basetype, NULL))
15989 /* An incomplete type. Remove it from the list. */
15990 *basep = TREE_CHAIN (*basep);
15991 else
15992 {
15993 max_bases++;
15994 if (TREE_TYPE (*basep))
15995 max_dvbases++;
15996 if (CLASS_TYPE_P (basetype))
15997 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15998 basep = &TREE_CHAIN (*basep);
15999 }
16000 }
16001 max_vbases += max_dvbases;
16002
16003 TYPE_MARKED_P (ref) = 1;
16004
16005 /* The binfo slot should be empty, unless this is an (ill-formed)
16006 redefinition. */
16007 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
16008
16009 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
16010
16011 binfo = make_tree_binfo (max_bases);
16012
16013 TYPE_BINFO (ref) = binfo;
16014 BINFO_OFFSET (binfo) = size_zero_node;
16015 BINFO_TYPE (binfo) = ref;
16016
16017 /* Apply base-class info set up to the variants of this type. */
16018 fixup_type_variants (ref);
16019
16020 if (max_bases)
16021 {
16022 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
16023 /* A C++98 POD cannot have base classes. */
16024 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
16025
16026 if (TREE_CODE (ref) == UNION_TYPE)
16027 {
16028 error ("derived union %qT invalid", ref);
16029 return;
16030 }
16031 }
16032
16033 if (max_bases > 1)
16034 warning (OPT_Wmultiple_inheritance,
16035 "%qT defined with multiple direct bases", ref);
16036
16037 if (max_vbases)
16038 {
16039 /* An aggregate can't have virtual base classes. */
16040 CLASSTYPE_NON_AGGREGATE (ref) = true;
16041
16042 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
16043
16044 if (max_dvbases)
16045 warning (OPT_Wvirtual_inheritance,
16046 "%qT defined with direct virtual base", ref);
16047 }
16048
16049 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
16050 {
16051 tree access = TREE_PURPOSE (base_list);
16052 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
16053 tree basetype = TREE_VALUE (base_list);
16054
16055 if (access == access_default_node)
16056 access = default_access;
16057
16058 /* Before C++17, an aggregate cannot have base classes. In C++17, an
16059 aggregate can't have virtual, private, or protected base classes. */
16060 if (cxx_dialect < cxx17
16061 || access != access_public_node
16062 || via_virtual)
16063 CLASSTYPE_NON_AGGREGATE (ref) = true;
16064
16065 if (PACK_EXPANSION_P (basetype))
16066 basetype = PACK_EXPANSION_PATTERN (basetype);
16067 if (TREE_CODE (basetype) == TYPE_DECL)
16068 basetype = TREE_TYPE (basetype);
16069 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
16070 {
16071 error ("base type %qT fails to be a struct or class type",
16072 basetype);
16073 goto dropped_base;
16074 }
16075
16076 base_binfo = NULL_TREE;
16077 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
16078 {
16079 base_binfo = TYPE_BINFO (basetype);
16080 /* The original basetype could have been a typedef'd type. */
16081 basetype = BINFO_TYPE (base_binfo);
16082
16083 /* Inherit flags from the base. */
16084 TYPE_HAS_NEW_OPERATOR (ref)
16085 |= TYPE_HAS_NEW_OPERATOR (basetype);
16086 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
16087 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
16088 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
16089 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
16090 CLASSTYPE_DIAMOND_SHAPED_P (ref)
16091 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
16092 CLASSTYPE_REPEATED_BASE_P (ref)
16093 |= CLASSTYPE_REPEATED_BASE_P (basetype);
16094 }
16095
16096 /* We must do this test after we've seen through a typedef
16097 type. */
16098 if (TYPE_MARKED_P (basetype))
16099 {
16100 if (basetype == ref)
16101 error ("recursive type %qT undefined", basetype);
16102 else
16103 error ("duplicate base type %qT invalid", basetype);
16104 goto dropped_base;
16105 }
16106
16107 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
16108 /* Regenerate the pack expansion for the bases. */
16109 basetype = make_pack_expansion (basetype);
16110
16111 TYPE_MARKED_P (basetype) = 1;
16112
16113 base_binfo = copy_binfo (base_binfo, basetype, ref,
16114 &igo_prev, via_virtual);
16115 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
16116 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
16117
16118 BINFO_BASE_APPEND (binfo, base_binfo);
16119 BINFO_BASE_ACCESS_APPEND (binfo, access);
16120 continue;
16121
16122 dropped_base:
16123 /* Update max_vbases to reflect the reality that we are dropping
16124 this base: if it reaches zero we want to undo the vec_alloc
16125 above to avoid inconsistencies during error-recovery: eg, in
16126 build_special_member_call, CLASSTYPE_VBASECLASSES non null
16127 and vtt null (c++/27952). */
16128 if (via_virtual)
16129 max_vbases--;
16130 if (CLASS_TYPE_P (basetype))
16131 max_vbases
16132 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
16133 }
16134
16135 if (CLASSTYPE_VBASECLASSES (ref)
16136 && max_vbases == 0)
16137 vec_free (CLASSTYPE_VBASECLASSES (ref));
16138
16139 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
16140 /* If we didn't get max_vbases vbases, we must have shared at
16141 least one of them, and are therefore diamond shaped. */
16142 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
16143
16144 /* Unmark all the types. */
16145 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
16146 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16147 TYPE_MARKED_P (ref) = 0;
16148
16149 /* Now see if we have a repeated base type. */
16150 if (!CLASSTYPE_REPEATED_BASE_P (ref))
16151 {
16152 for (base_binfo = binfo; base_binfo;
16153 base_binfo = TREE_CHAIN (base_binfo))
16154 {
16155 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16156 {
16157 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
16158 break;
16159 }
16160 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
16161 }
16162 for (base_binfo = binfo; base_binfo;
16163 base_binfo = TREE_CHAIN (base_binfo))
16164 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
16165 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
16166 else
16167 break;
16168 }
16169 }
16170
16171
16172 /* Copies the enum-related properties from type SRC to type DST.
16173 Used with the underlying type of an enum and the enum itself. */
16174 static void
copy_type_enum(tree dst,tree src)16175 copy_type_enum (tree dst, tree src)
16176 {
16177 tree t;
16178 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
16179 {
16180 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
16181 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
16182 TYPE_SIZE (t) = TYPE_SIZE (src);
16183 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
16184 SET_TYPE_MODE (dst, TYPE_MODE (src));
16185 TYPE_PRECISION (t) = TYPE_PRECISION (src);
16186 unsigned valign = TYPE_ALIGN (src);
16187 if (TYPE_USER_ALIGN (t))
16188 valign = MAX (valign, TYPE_ALIGN (t));
16189 else
16190 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
16191 SET_TYPE_ALIGN (t, valign);
16192 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
16193 }
16194 }
16195
16196 /* Begin compiling the definition of an enumeration type.
16197 NAME is its name,
16198
16199 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
16200
16201 UNDERLYING_TYPE is the type that will be used as the storage for
16202 the enumeration type. This should be NULL_TREE if no storage type
16203 was specified.
16204
16205 ATTRIBUTES are any attributes specified after the enum-key.
16206
16207 SCOPED_ENUM_P is true if this is a scoped enumeration type.
16208
16209 if IS_NEW is not NULL, gets TRUE iff a new type is created.
16210
16211 Returns the type object, as yet incomplete.
16212 Also records info about it so that build_enumerator
16213 may be used to declare the individual values as they are read. */
16214
16215 tree
start_enum(tree name,tree enumtype,tree underlying_type,tree attributes,bool scoped_enum_p,bool * is_new)16216 start_enum (tree name, tree enumtype, tree underlying_type,
16217 tree attributes, bool scoped_enum_p, bool *is_new)
16218 {
16219 tree prevtype = NULL_TREE;
16220 gcc_assert (identifier_p (name));
16221
16222 if (is_new)
16223 *is_new = false;
16224 /* [C++0x dcl.enum]p5:
16225
16226 If not explicitly specified, the underlying type of a scoped
16227 enumeration type is int. */
16228 if (!underlying_type && scoped_enum_p)
16229 underlying_type = integer_type_node;
16230
16231 if (underlying_type)
16232 underlying_type = cv_unqualified (underlying_type);
16233
16234 /* If this is the real definition for a previous forward reference,
16235 fill in the contents in the same object that used to be the
16236 forward reference. */
16237 if (!enumtype)
16238 enumtype = lookup_and_check_tag (enum_type, name,
16239 /*tag_scope=*/TAG_how::CURRENT_ONLY,
16240 /*template_header_p=*/false);
16241
16242 /* In case of a template_decl, the only check that should be deferred
16243 to instantiation time is the comparison of underlying types. */
16244 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
16245 {
16246 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
16247 {
16248 error_at (input_location, "scoped/unscoped mismatch "
16249 "in enum %q#T", enumtype);
16250 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16251 "previous definition here");
16252 enumtype = error_mark_node;
16253 }
16254 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
16255 {
16256 error_at (input_location, "underlying type mismatch "
16257 "in enum %q#T", enumtype);
16258 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16259 "previous definition here");
16260 enumtype = error_mark_node;
16261 }
16262 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
16263 && !same_type_p (underlying_type,
16264 ENUM_UNDERLYING_TYPE (enumtype)))
16265 {
16266 error_at (input_location, "different underlying type "
16267 "in enum %q#T", enumtype);
16268 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16269 "previous definition here");
16270 underlying_type = NULL_TREE;
16271 }
16272
16273 if (modules_p ())
16274 {
16275 if (!module_may_redeclare (TYPE_NAME (enumtype)))
16276 {
16277 error ("cannot define %qD in different module",
16278 TYPE_NAME (enumtype));
16279 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)),
16280 "declared here");
16281 enumtype = error_mark_node;
16282 }
16283 set_instantiating_module (TYPE_NAME (enumtype));
16284 }
16285 }
16286
16287 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
16288 || processing_template_decl)
16289 {
16290 /* In case of error, make a dummy enum to allow parsing to
16291 continue. */
16292 if (enumtype == error_mark_node)
16293 {
16294 name = make_anon_name ();
16295 enumtype = NULL_TREE;
16296 }
16297
16298 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
16299 of an opaque enum, or an opaque enum of an already defined
16300 enumeration (C++11).
16301 In any other case, it'll be NULL_TREE. */
16302 if (!enumtype)
16303 {
16304 if (is_new)
16305 *is_new = true;
16306 }
16307 prevtype = enumtype;
16308
16309 /* Do not push the decl more than once. */
16310 if (!enumtype
16311 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
16312 {
16313 enumtype = cxx_make_type (ENUMERAL_TYPE);
16314 enumtype = pushtag (name, enumtype);
16315
16316 /* std::byte aliases anything. */
16317 if (enumtype != error_mark_node
16318 && TYPE_CONTEXT (enumtype) == std_node
16319 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
16320 TYPE_ALIAS_SET (enumtype) = 0;
16321 }
16322 else
16323 enumtype = xref_tag (enum_type, name);
16324
16325 if (enumtype == error_mark_node)
16326 return error_mark_node;
16327
16328 /* The enum is considered opaque until the opening '{' of the
16329 enumerator list. */
16330 SET_OPAQUE_ENUM_P (enumtype, true);
16331 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
16332 }
16333
16334 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
16335
16336 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
16337
16338 if (underlying_type)
16339 {
16340 if (ENUM_UNDERLYING_TYPE (enumtype))
16341 /* We already checked that it matches, don't change it to a different
16342 typedef variant. */;
16343 else if (CP_INTEGRAL_TYPE_P (underlying_type))
16344 {
16345 copy_type_enum (enumtype, underlying_type);
16346 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16347 }
16348 else if (dependent_type_p (underlying_type))
16349 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
16350 else
16351 error ("underlying type %qT of %qT must be an integral type",
16352 underlying_type, enumtype);
16353 }
16354
16355 /* If into a template class, the returned enum is always the first
16356 declaration (opaque or not) seen. This way all the references to
16357 this type will be to the same declaration. The following ones are used
16358 only to check for definition errors. */
16359 if (prevtype && processing_template_decl)
16360 return prevtype;
16361 else
16362 return enumtype;
16363 }
16364
16365 /* After processing and defining all the values of an enumeration type,
16366 install their decls in the enumeration type.
16367 ENUMTYPE is the type object. */
16368
16369 void
finish_enum_value_list(tree enumtype)16370 finish_enum_value_list (tree enumtype)
16371 {
16372 tree values;
16373 tree underlying_type;
16374 tree decl;
16375 tree value;
16376 tree minnode, maxnode;
16377 tree t;
16378
16379 bool fixed_underlying_type_p
16380 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
16381
16382 /* We built up the VALUES in reverse order. */
16383 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
16384
16385 /* For an enum defined in a template, just set the type of the values;
16386 all further processing is postponed until the template is
16387 instantiated. We need to set the type so that tsubst of a CONST_DECL
16388 works. */
16389 if (processing_template_decl)
16390 {
16391 for (values = TYPE_VALUES (enumtype);
16392 values;
16393 values = TREE_CHAIN (values))
16394 TREE_TYPE (TREE_VALUE (values)) = enumtype;
16395 return;
16396 }
16397
16398 /* Determine the minimum and maximum values of the enumerators. */
16399 if (TYPE_VALUES (enumtype))
16400 {
16401 minnode = maxnode = NULL_TREE;
16402
16403 for (values = TYPE_VALUES (enumtype);
16404 values;
16405 values = TREE_CHAIN (values))
16406 {
16407 decl = TREE_VALUE (values);
16408
16409 /* [dcl.enum]: Following the closing brace of an enum-specifier,
16410 each enumerator has the type of its enumeration. Prior to the
16411 closing brace, the type of each enumerator is the type of its
16412 initializing value. */
16413 TREE_TYPE (decl) = enumtype;
16414
16415 /* Update the minimum and maximum values, if appropriate. */
16416 value = DECL_INITIAL (decl);
16417 if (TREE_CODE (value) != INTEGER_CST)
16418 value = integer_zero_node;
16419 /* Figure out what the minimum and maximum values of the
16420 enumerators are. */
16421 if (!minnode)
16422 minnode = maxnode = value;
16423 else if (tree_int_cst_lt (maxnode, value))
16424 maxnode = value;
16425 else if (tree_int_cst_lt (value, minnode))
16426 minnode = value;
16427 }
16428 }
16429 else
16430 /* [dcl.enum]
16431
16432 If the enumerator-list is empty, the underlying type is as if
16433 the enumeration had a single enumerator with value 0. */
16434 minnode = maxnode = integer_zero_node;
16435
16436 if (!fixed_underlying_type_p)
16437 {
16438 /* Compute the number of bits require to represent all values of the
16439 enumeration. We must do this before the type of MINNODE and
16440 MAXNODE are transformed, since tree_int_cst_min_precision relies
16441 on the TREE_TYPE of the value it is passed. */
16442 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
16443 int lowprec = tree_int_cst_min_precision (minnode, sgn);
16444 int highprec = tree_int_cst_min_precision (maxnode, sgn);
16445 int precision = MAX (lowprec, highprec);
16446 unsigned int itk;
16447 bool use_short_enum;
16448
16449 /* Determine the underlying type of the enumeration.
16450
16451 [dcl.enum]
16452
16453 The underlying type of an enumeration is an integral type that
16454 can represent all the enumerator values defined in the
16455 enumeration. It is implementation-defined which integral type is
16456 used as the underlying type for an enumeration except that the
16457 underlying type shall not be larger than int unless the value of
16458 an enumerator cannot fit in an int or unsigned int.
16459
16460 We use "int" or an "unsigned int" as the underlying type, even if
16461 a smaller integral type would work, unless the user has
16462 explicitly requested that we use the smallest possible type. The
16463 user can request that for all enumerations with a command line
16464 flag, or for just one enumeration with an attribute. */
16465
16466 use_short_enum = flag_short_enums
16467 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
16468
16469 /* If the precision of the type was specified with an attribute and it
16470 was too small, give an error. Otherwise, use it. */
16471 if (TYPE_PRECISION (enumtype))
16472 {
16473 if (precision > TYPE_PRECISION (enumtype))
16474 error ("specified mode too small for enumerated values");
16475 else
16476 {
16477 use_short_enum = true;
16478 precision = TYPE_PRECISION (enumtype);
16479 }
16480 }
16481
16482 for (itk = (use_short_enum ? itk_char : itk_int);
16483 itk != itk_none;
16484 itk++)
16485 {
16486 underlying_type = integer_types[itk];
16487 if (underlying_type != NULL_TREE
16488 && TYPE_PRECISION (underlying_type) >= precision
16489 && TYPE_SIGN (underlying_type) == sgn)
16490 break;
16491 }
16492 if (itk == itk_none)
16493 {
16494 /* DR 377
16495
16496 IF no integral type can represent all the enumerator values, the
16497 enumeration is ill-formed. */
16498 error ("no integral type can represent all of the enumerator values "
16499 "for %qT", enumtype);
16500 precision = TYPE_PRECISION (long_long_integer_type_node);
16501 underlying_type = integer_types[itk_unsigned_long_long];
16502 }
16503
16504 /* [dcl.enum]
16505
16506 The value of sizeof() applied to an enumeration type, an object
16507 of an enumeration type, or an enumerator, is the value of sizeof()
16508 applied to the underlying type. */
16509 copy_type_enum (enumtype, underlying_type);
16510
16511 /* Compute the minimum and maximum values for the type.
16512
16513 [dcl.enum]
16514
16515 For an enumeration where emin is the smallest enumerator and emax
16516 is the largest, the values of the enumeration are the values of the
16517 underlying type in the range bmin to bmax, where bmin and bmax are,
16518 respectively, the smallest and largest values of the smallest bit-
16519 field that can store emin and emax. */
16520
16521 /* The middle-end currently assumes that types with TYPE_PRECISION
16522 narrower than their underlying type are suitably zero or sign
16523 extended to fill their mode. Similarly, it assumes that the front
16524 end assures that a value of a particular type must be within
16525 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
16526
16527 We used to set these fields based on bmin and bmax, but that led
16528 to invalid assumptions like optimizing away bounds checking. So
16529 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
16530 TYPE_MAX_VALUE to the values for the mode above and only restrict
16531 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
16532 ENUM_UNDERLYING_TYPE (enumtype)
16533 = build_distinct_type_copy (underlying_type);
16534 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
16535 set_min_and_max_values_for_integral_type
16536 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
16537
16538 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
16539 if (flag_strict_enums)
16540 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
16541 }
16542 else
16543 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
16544
16545 /* If the enum is exported, mark the consts too. */
16546 bool export_p = (UNSCOPED_ENUM_P (enumtype)
16547 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype))
16548 && at_namespace_scope_p ());
16549
16550 /* Convert each of the enumerators to the type of the underlying
16551 type of the enumeration. */
16552 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
16553 {
16554 decl = TREE_VALUE (values);
16555 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
16556 if (fixed_underlying_type_p)
16557 /* If the enumeration type has a fixed underlying type, we
16558 already checked all of the enumerator values. */
16559 value = DECL_INITIAL (decl);
16560 else
16561 value = perform_implicit_conversion (underlying_type,
16562 DECL_INITIAL (decl),
16563 tf_warning_or_error);
16564 /* Do not clobber shared ints. */
16565 if (value != error_mark_node)
16566 {
16567 value = copy_node (value);
16568
16569 TREE_TYPE (value) = enumtype;
16570 }
16571 DECL_INITIAL (decl) = value;
16572 if (export_p)
16573 DECL_MODULE_EXPORT_P (decl) = true;
16574 }
16575
16576 /* Fix up all variant types of this enum type. */
16577 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
16578 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
16579
16580 if (at_class_scope_p ()
16581 && COMPLETE_TYPE_P (current_class_type)
16582 && UNSCOPED_ENUM_P (enumtype))
16583 {
16584 insert_late_enum_def_bindings (current_class_type, enumtype);
16585 /* TYPE_FIELDS needs fixup. */
16586 fixup_type_variants (current_class_type);
16587 }
16588
16589 /* Finish debugging output for this type. */
16590 rest_of_type_compilation (enumtype, namespace_bindings_p ());
16591
16592 /* Each enumerator now has the type of its enumeration. Clear the cache
16593 so that this change in types doesn't confuse us later on. */
16594 clear_cv_and_fold_caches ();
16595 }
16596
16597 /* Finishes the enum type. This is called only the first time an
16598 enumeration is seen, be it opaque or odinary.
16599 ENUMTYPE is the type object. */
16600
16601 void
finish_enum(tree enumtype)16602 finish_enum (tree enumtype)
16603 {
16604 if (processing_template_decl)
16605 {
16606 if (at_function_scope_p ())
16607 add_stmt (build_min (TAG_DEFN, enumtype));
16608 return;
16609 }
16610
16611 /* If this is a forward declaration, there should not be any variants,
16612 though we can get a variant in the middle of an enum-specifier with
16613 wacky code like 'enum E { e = sizeof(const E*) };' */
16614 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
16615 && (TYPE_VALUES (enumtype)
16616 || !TYPE_NEXT_VARIANT (enumtype)));
16617 }
16618
16619 /* Build and install a CONST_DECL for an enumeration constant of the
16620 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
16621 Apply ATTRIBUTES if available. LOC is the location of NAME.
16622 Assignment of sequential values by default is handled here. */
16623
16624 tree
build_enumerator(tree name,tree value,tree enumtype,tree attributes,location_t loc)16625 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
16626 location_t loc)
16627 {
16628 tree decl;
16629 tree context;
16630 tree type;
16631
16632 /* scalar_constant_value will pull out this expression, so make sure
16633 it's folded as appropriate. */
16634 if (processing_template_decl)
16635 value = fold_non_dependent_expr (value);
16636
16637 /* If the VALUE was erroneous, pretend it wasn't there; that will
16638 result in the enum being assigned the next value in sequence. */
16639 if (value == error_mark_node)
16640 value = NULL_TREE;
16641
16642 /* Remove no-op casts from the value. */
16643 if (value)
16644 STRIP_TYPE_NOPS (value);
16645
16646 if (! processing_template_decl)
16647 {
16648 /* Validate and default VALUE. */
16649 if (value != NULL_TREE)
16650 {
16651 if (!ENUM_UNDERLYING_TYPE (enumtype))
16652 {
16653 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
16654 value, true);
16655 if (tmp_value)
16656 value = tmp_value;
16657 }
16658 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16659 (TREE_TYPE (value)))
16660 value = perform_implicit_conversion_flags
16661 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
16662 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
16663
16664 if (value == error_mark_node)
16665 value = NULL_TREE;
16666
16667 if (value != NULL_TREE)
16668 {
16669 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
16670 (TREE_TYPE (value)))
16671 {
16672 error_at (cp_expr_loc_or_input_loc (value),
16673 "enumerator value for %qD must have integral or "
16674 "unscoped enumeration type", name);
16675 value = NULL_TREE;
16676 }
16677 else
16678 {
16679 value = cxx_constant_value (value);
16680
16681 if (TREE_CODE (value) != INTEGER_CST)
16682 {
16683 error ("enumerator value for %qD is not an integer "
16684 "constant", name);
16685 value = NULL_TREE;
16686 }
16687 }
16688 }
16689 }
16690
16691 /* Default based on previous value. */
16692 if (value == NULL_TREE)
16693 {
16694 if (TYPE_VALUES (enumtype))
16695 {
16696 tree prev_value;
16697
16698 /* C++03 7.2/4: If no initializer is specified for the first
16699 enumerator, the type is an unspecified integral
16700 type. Otherwise the type is the same as the type of the
16701 initializing value of the preceding enumerator unless the
16702 incremented value is not representable in that type, in
16703 which case the type is an unspecified integral type
16704 sufficient to contain the incremented value. */
16705 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
16706 if (TREE_CODE (prev_value) != INTEGER_CST)
16707 value = error_mark_node;
16708 else
16709 {
16710 wi::overflow_type overflowed;
16711 tree type = TREE_TYPE (prev_value);
16712 signop sgn = TYPE_SIGN (type);
16713 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
16714 &overflowed);
16715 if (!overflowed)
16716 {
16717 bool pos = !wi::neg_p (wi, sgn);
16718 if (!wi::fits_to_tree_p (wi, type))
16719 {
16720 unsigned int itk;
16721 for (itk = itk_int; itk != itk_none; itk++)
16722 {
16723 type = integer_types[itk];
16724 if (type != NULL_TREE
16725 && (pos || !TYPE_UNSIGNED (type))
16726 && wi::fits_to_tree_p (wi, type))
16727 break;
16728 }
16729 if (type && cxx_dialect < cxx11
16730 && itk > itk_unsigned_long)
16731 pedwarn (input_location, OPT_Wlong_long,
16732 pos ? G_("\
16733 incremented enumerator value is too large for %<unsigned long%>") : G_("\
16734 incremented enumerator value is too large for %<long%>"));
16735 }
16736 if (type == NULL_TREE)
16737 overflowed = wi::OVF_UNKNOWN;
16738 else
16739 value = wide_int_to_tree (type, wi);
16740 }
16741
16742 if (overflowed)
16743 {
16744 error ("overflow in enumeration values at %qD", name);
16745 value = error_mark_node;
16746 }
16747 }
16748 }
16749 else
16750 value = integer_zero_node;
16751 }
16752
16753 /* Remove no-op casts from the value. */
16754 STRIP_TYPE_NOPS (value);
16755
16756 /* If the underlying type of the enum is fixed, check whether
16757 the enumerator values fits in the underlying type. If it
16758 does not fit, the program is ill-formed [C++0x dcl.enum]. */
16759 if (ENUM_UNDERLYING_TYPE (enumtype)
16760 && value
16761 && TREE_CODE (value) == INTEGER_CST)
16762 {
16763 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
16764 error ("enumerator value %qE is outside the range of underlying "
16765 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
16766
16767 /* Convert the value to the appropriate type. */
16768 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
16769 }
16770 }
16771
16772 /* C++ associates enums with global, function, or class declarations. */
16773 context = current_scope ();
16774
16775 /* Build the actual enumeration constant. Note that the enumeration
16776 constants have the underlying type of the enum (if it is fixed)
16777 or the type of their initializer (if the underlying type of the
16778 enum is not fixed):
16779
16780 [ C++0x dcl.enum ]
16781
16782 If the underlying type is fixed, the type of each enumerator
16783 prior to the closing brace is the underlying type; if the
16784 initializing value of an enumerator cannot be represented by
16785 the underlying type, the program is ill-formed. If the
16786 underlying type is not fixed, the type of each enumerator is
16787 the type of its initializing value.
16788
16789 If the underlying type is not fixed, it will be computed by
16790 finish_enum and we will reset the type of this enumerator. Of
16791 course, if we're processing a template, there may be no value. */
16792 type = value ? TREE_TYPE (value) : NULL_TREE;
16793
16794 decl = build_decl (loc, CONST_DECL, name, type);
16795
16796 DECL_CONTEXT (decl) = enumtype;
16797 TREE_CONSTANT (decl) = 1;
16798 TREE_READONLY (decl) = 1;
16799 DECL_INITIAL (decl) = value;
16800
16801 if (attributes)
16802 cplus_decl_attributes (&decl, attributes, 0);
16803
16804 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
16805 {
16806 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
16807 on the TYPE_FIELDS list for `S'. (That's so that you can say
16808 things like `S::i' later.) */
16809
16810 /* The enumerator may be getting declared outside of its enclosing
16811 class, like so:
16812
16813 class S { public: enum E : int; }; enum S::E : int { i = 7; };
16814
16815 For which case we need to make sure that the access of `S::i'
16816 matches the access of `S::E'. */
16817 auto cas = make_temp_override (current_access_specifier);
16818 set_current_access_from_decl (TYPE_NAME (enumtype));
16819 finish_member_declaration (decl);
16820 }
16821 else
16822 pushdecl (decl);
16823
16824 /* Add this enumeration constant to the list for this type. */
16825 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
16826
16827 return decl;
16828 }
16829
16830 /* Look for an enumerator with the given NAME within the enumeration
16831 type ENUMTYPE. This routine is used primarily for qualified name
16832 lookup into an enumerator in C++0x, e.g.,
16833
16834 enum class Color { Red, Green, Blue };
16835
16836 Color color = Color::Red;
16837
16838 Returns the value corresponding to the enumerator, or
16839 NULL_TREE if no such enumerator was found. */
16840 tree
lookup_enumerator(tree enumtype,tree name)16841 lookup_enumerator (tree enumtype, tree name)
16842 {
16843 tree e;
16844 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
16845
16846 e = purpose_member (name, TYPE_VALUES (enumtype));
16847 return e? TREE_VALUE (e) : NULL_TREE;
16848 }
16849
16850 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
16851
16852 tree
cxx_simulate_enum_decl(location_t loc,const char * name,vec<string_int_pair> * values)16853 cxx_simulate_enum_decl (location_t loc, const char *name,
16854 vec<string_int_pair> *values)
16855 {
16856 location_t saved_loc = input_location;
16857 input_location = loc;
16858
16859 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
16860 NULL_TREE, false, NULL);
16861 if (!OPAQUE_ENUM_P (enumtype))
16862 {
16863 error_at (loc, "multiple definition of %q#T", enumtype);
16864 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
16865 "previous definition here");
16866 return enumtype;
16867 }
16868 SET_OPAQUE_ENUM_P (enumtype, false);
16869 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
16870
16871 for (const string_int_pair &value : values)
16872 build_enumerator (get_identifier (value.first),
16873 build_int_cst (integer_type_node, value.second),
16874 enumtype, NULL_TREE, loc);
16875
16876 finish_enum_value_list (enumtype);
16877 finish_enum (enumtype);
16878
16879 input_location = saved_loc;
16880 return enumtype;
16881 }
16882
16883 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
16884
16885 tree
cxx_simulate_record_decl(location_t loc,const char * name,array_slice<const tree> fields)16886 cxx_simulate_record_decl (location_t loc, const char *name,
16887 array_slice<const tree> fields)
16888 {
16889 iloc_sentinel ils (loc);
16890
16891 tree ident = get_identifier (name);
16892 tree type = xref_tag (/*tag_code=*/record_type, ident);
16893 if (type != error_mark_node
16894 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type)))
16895 {
16896 error ("redefinition of %q#T", type);
16897 type = error_mark_node;
16898 }
16899 if (type == error_mark_node)
16900 return lhd_simulate_record_decl (loc, name, fields);
16901
16902 xref_basetypes (type, NULL_TREE);
16903 type = begin_class_definition (type);
16904 if (type == error_mark_node)
16905 return lhd_simulate_record_decl (loc, name, fields);
16906
16907 for (tree field : fields)
16908 finish_member_declaration (field);
16909
16910 type = finish_struct (type, NULL_TREE);
16911
16912 tree decl = build_decl (loc, TYPE_DECL, ident, type);
16913 set_underlying_type (decl);
16914 lang_hooks.decls.pushdecl (decl);
16915
16916 return type;
16917 }
16918
16919 /* We're defining DECL. Make sure that its type is OK. */
16920
16921 static void
check_function_type(tree decl,tree current_function_parms)16922 check_function_type (tree decl, tree current_function_parms)
16923 {
16924 tree fntype = TREE_TYPE (decl);
16925 tree return_type = complete_type (TREE_TYPE (fntype));
16926
16927 /* In a function definition, arg types must be complete. */
16928 require_complete_types_for_parms (current_function_parms);
16929
16930 if (dependent_type_p (return_type)
16931 || type_uses_auto (return_type))
16932 return;
16933 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
16934 {
16935 tree args = TYPE_ARG_TYPES (fntype);
16936
16937 error ("return type %q#T is incomplete", return_type);
16938
16939 /* Make it return void instead. */
16940 if (TREE_CODE (fntype) == METHOD_TYPE)
16941 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
16942 void_type_node,
16943 TREE_CHAIN (args));
16944 else
16945 fntype = build_function_type (void_type_node, args);
16946 fntype = (cp_build_type_attribute_variant
16947 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
16948 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
16949 TREE_TYPE (decl) = fntype;
16950 }
16951 else
16952 {
16953 abstract_virtuals_error (decl, TREE_TYPE (fntype));
16954 maybe_warn_parm_abi (TREE_TYPE (fntype),
16955 DECL_SOURCE_LOCATION (decl));
16956 }
16957 }
16958
16959 /* True iff FN is an implicitly-defined default constructor. */
16960
16961 static bool
implicit_default_ctor_p(tree fn)16962 implicit_default_ctor_p (tree fn)
16963 {
16964 return (DECL_CONSTRUCTOR_P (fn)
16965 && !user_provided_p (fn)
16966 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
16967 }
16968
16969 /* Clobber the contents of *this to let the back end know that the object
16970 storage is dead when we enter the constructor or leave the destructor. */
16971
16972 static tree
build_clobber_this()16973 build_clobber_this ()
16974 {
16975 /* Clobbering an empty base is pointless, and harmful if its one byte
16976 TYPE_SIZE overlays real data. */
16977 if (is_empty_class (current_class_type))
16978 return void_node;
16979
16980 /* If we have virtual bases, clobber the whole object, but only if we're in
16981 charge. If we don't have virtual bases, clobber the as-base type so we
16982 don't mess with tail padding. */
16983 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
16984
16985 tree ctype = current_class_type;
16986 if (!vbases)
16987 ctype = CLASSTYPE_AS_BASE (ctype);
16988
16989 tree clobber = build_clobber (ctype);
16990
16991 tree thisref = current_class_ref;
16992 if (ctype != current_class_type)
16993 {
16994 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
16995 thisref = convert_from_reference (thisref);
16996 }
16997
16998 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
16999 if (vbases)
17000 exprstmt = build_if_in_charge (exprstmt);
17001
17002 return exprstmt;
17003 }
17004
17005 /* Create the FUNCTION_DECL for a function definition.
17006 DECLSPECS and DECLARATOR are the parts of the declaration;
17007 they describe the function's name and the type it returns,
17008 but twisted together in a fashion that parallels the syntax of C.
17009
17010 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
17011 DECLARATOR is really the DECL for the function we are about to
17012 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
17013 indicating that the function is an inline defined in-class.
17014
17015 This function creates a binding context for the function body
17016 as well as setting up the FUNCTION_DECL in current_function_decl.
17017
17018 For C++, we must first check whether that datum makes any sense.
17019 For example, "class A local_a(1,2);" means that variable local_a
17020 is an aggregate of type A, which should have a constructor
17021 applied to it with the argument list [1, 2].
17022
17023 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
17024 or may be a BLOCK if the function has been defined previously
17025 in this translation unit. On exit, DECL_INITIAL (decl1) will be
17026 error_mark_node if the function has never been defined, or
17027 a BLOCK if the function has been defined somewhere. */
17028
17029 bool
start_preparsed_function(tree decl1,tree attrs,int flags)17030 start_preparsed_function (tree decl1, tree attrs, int flags)
17031 {
17032 tree ctype = NULL_TREE;
17033 bool doing_friend = false;
17034
17035 /* Sanity check. */
17036 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
17037 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
17038
17039 tree fntype = TREE_TYPE (decl1);
17040 if (TREE_CODE (fntype) == METHOD_TYPE)
17041 ctype = TYPE_METHOD_BASETYPE (fntype);
17042 else
17043 {
17044 ctype = DECL_FRIEND_CONTEXT (decl1);
17045
17046 if (ctype)
17047 doing_friend = true;
17048 }
17049
17050 if (DECL_DECLARED_INLINE_P (decl1)
17051 && lookup_attribute ("noinline", attrs))
17052 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
17053 "inline function %qD given attribute %qs", decl1, "noinline");
17054
17055 /* Handle gnu_inline attribute. */
17056 if (GNU_INLINE_P (decl1))
17057 {
17058 DECL_EXTERNAL (decl1) = 1;
17059 DECL_NOT_REALLY_EXTERN (decl1) = 0;
17060 DECL_INTERFACE_KNOWN (decl1) = 1;
17061 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
17062 }
17063
17064 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
17065 /* This is a constructor, we must ensure that any default args
17066 introduced by this definition are propagated to the clones
17067 now. The clones are used directly in overload resolution. */
17068 adjust_clone_args (decl1);
17069
17070 /* Sometimes we don't notice that a function is a static member, and
17071 build a METHOD_TYPE for it. Fix that up now. */
17072 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
17073 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
17074
17075 /* Set up current_class_type, and enter the scope of the class, if
17076 appropriate. */
17077 if (ctype)
17078 push_nested_class (ctype);
17079 else if (DECL_STATIC_FUNCTION_P (decl1))
17080 push_nested_class (DECL_CONTEXT (decl1));
17081
17082 /* Now that we have entered the scope of the class, we must restore
17083 the bindings for any template parameters surrounding DECL1, if it
17084 is an inline member template. (Order is important; consider the
17085 case where a template parameter has the same name as a field of
17086 the class.) It is not until after this point that
17087 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
17088 if (flags & SF_INCLASS_INLINE)
17089 maybe_begin_member_template_processing (decl1);
17090
17091 /* Effective C++ rule 15. */
17092 if (warn_ecpp
17093 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
17094 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
17095 && VOID_TYPE_P (TREE_TYPE (fntype)))
17096 warning (OPT_Weffc__,
17097 "%<operator=%> should return a reference to %<*this%>");
17098
17099 /* Make the init_value nonzero so pushdecl knows this is not tentative.
17100 error_mark_node is replaced below (in poplevel) with the BLOCK. */
17101 if (!DECL_INITIAL (decl1))
17102 DECL_INITIAL (decl1) = error_mark_node;
17103
17104 /* This function exists in static storage.
17105 (This does not mean `static' in the C sense!) */
17106 TREE_STATIC (decl1) = 1;
17107
17108 /* We must call push_template_decl after current_class_type is set
17109 up. (If we are processing inline definitions after exiting a
17110 class scope, current_class_type will be NULL_TREE until set above
17111 by push_nested_class.) */
17112 if (processing_template_decl)
17113 {
17114 tree newdecl1 = push_template_decl (decl1, doing_friend);
17115 if (newdecl1 == error_mark_node)
17116 {
17117 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
17118 pop_nested_class ();
17119 return false;
17120 }
17121 decl1 = newdecl1;
17122 }
17123
17124 /* Make sure the parameter and return types are reasonable. When
17125 you declare a function, these types can be incomplete, but they
17126 must be complete when you define the function. */
17127 check_function_type (decl1, DECL_ARGUMENTS (decl1));
17128
17129 /* Build the return declaration for the function. */
17130 tree restype = TREE_TYPE (fntype);
17131
17132 if (DECL_RESULT (decl1) == NULL_TREE)
17133 {
17134 tree resdecl;
17135
17136 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
17137 DECL_ARTIFICIAL (resdecl) = 1;
17138 DECL_IGNORED_P (resdecl) = 1;
17139 DECL_RESULT (decl1) = resdecl;
17140
17141 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
17142 }
17143
17144 /* Record the decl so that the function name is defined.
17145 If we already have a decl for this name, and it is a FUNCTION_DECL,
17146 use the old decl. */
17147 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
17148 {
17149 /* A specialization is not used to guide overload resolution. */
17150 if (!DECL_FUNCTION_MEMBER_P (decl1)
17151 && !(DECL_USE_TEMPLATE (decl1) &&
17152 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
17153 {
17154 tree olddecl = pushdecl (decl1);
17155
17156 if (olddecl == error_mark_node)
17157 /* If something went wrong when registering the declaration,
17158 use DECL1; we have to have a FUNCTION_DECL to use when
17159 parsing the body of the function. */
17160 ;
17161 else
17162 {
17163 /* Otherwise, OLDDECL is either a previous declaration
17164 of the same function or DECL1 itself. */
17165
17166 if (warn_missing_declarations
17167 && olddecl == decl1
17168 && !DECL_MAIN_P (decl1)
17169 && TREE_PUBLIC (decl1)
17170 && !DECL_DECLARED_INLINE_P (decl1))
17171 {
17172 tree context;
17173
17174 /* Check whether DECL1 is in an anonymous
17175 namespace. */
17176 for (context = DECL_CONTEXT (decl1);
17177 context;
17178 context = DECL_CONTEXT (context))
17179 {
17180 if (TREE_CODE (context) == NAMESPACE_DECL
17181 && DECL_NAME (context) == NULL_TREE)
17182 break;
17183 }
17184
17185 if (context == NULL)
17186 warning_at (DECL_SOURCE_LOCATION (decl1),
17187 OPT_Wmissing_declarations,
17188 "no previous declaration for %qD", decl1);
17189 }
17190
17191 decl1 = olddecl;
17192 }
17193 }
17194 else
17195 {
17196 /* We need to set the DECL_CONTEXT. */
17197 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
17198 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
17199 }
17200 fntype = TREE_TYPE (decl1);
17201 restype = TREE_TYPE (fntype);
17202
17203 /* If #pragma weak applies, mark the decl appropriately now.
17204 The pragma only applies to global functions. Because
17205 determining whether or not the #pragma applies involves
17206 computing the mangled name for the declaration, we cannot
17207 apply the pragma until after we have merged this declaration
17208 with any previous declarations; if the original declaration
17209 has a linkage specification, that specification applies to
17210 the definition as well, and may affect the mangled name. */
17211 if (DECL_FILE_SCOPE_P (decl1))
17212 maybe_apply_pragma_weak (decl1);
17213 }
17214
17215 /* We are now in the scope of the function being defined. */
17216 current_function_decl = decl1;
17217
17218 /* Save the parm names or decls from this function's declarator
17219 where store_parm_decls will find them. */
17220 tree current_function_parms = DECL_ARGUMENTS (decl1);
17221
17222 /* Let the user know we're compiling this function. */
17223 announce_function (decl1);
17224
17225 gcc_assert (DECL_INITIAL (decl1));
17226
17227 /* This function may already have been parsed, in which case just
17228 return; our caller will skip over the body without parsing. */
17229 if (DECL_INITIAL (decl1) != error_mark_node)
17230 return true;
17231
17232 /* Initialize RTL machinery. We cannot do this until
17233 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
17234 even when processing a template; this is how we get
17235 CFUN set up, and our per-function variables initialized.
17236 FIXME factor out the non-RTL stuff. */
17237 cp_binding_level *bl = current_binding_level;
17238 allocate_struct_function (decl1, processing_template_decl);
17239
17240 /* Initialize the language data structures. Whenever we start
17241 a new function, we destroy temporaries in the usual way. */
17242 cfun->language = ggc_cleared_alloc<language_function> ();
17243 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
17244 current_binding_level = bl;
17245
17246 /* If we are (erroneously) defining a function that we have already
17247 defined before, wipe out what we knew before. */
17248 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
17249 FNDECL_USED_AUTO (decl1) = false;
17250 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
17251
17252 if (!processing_template_decl && type_uses_auto (restype))
17253 {
17254 FNDECL_USED_AUTO (decl1) = true;
17255 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
17256 }
17257
17258 /* Start the statement-tree, start the tree now. */
17259 DECL_SAVED_TREE (decl1) = push_stmt_list ();
17260
17261 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
17262 {
17263 /* We know that this was set up by `grokclassfn'. We do not
17264 wait until `store_parm_decls', since evil parse errors may
17265 never get us to that point. Here we keep the consistency
17266 between `current_class_type' and `current_class_ptr'. */
17267 tree t = DECL_ARGUMENTS (decl1);
17268
17269 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
17270 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
17271
17272 cp_function_chain->x_current_class_ref
17273 = cp_build_fold_indirect_ref (t);
17274 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
17275 cp_function_chain->x_current_class_ptr = t;
17276
17277 /* Constructors and destructors need to know whether they're "in
17278 charge" of initializing virtual base classes. */
17279 t = DECL_CHAIN (t);
17280 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
17281 {
17282 current_in_charge_parm = t;
17283 t = DECL_CHAIN (t);
17284 }
17285 if (DECL_HAS_VTT_PARM_P (decl1))
17286 {
17287 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
17288 current_vtt_parm = t;
17289 }
17290 }
17291
17292 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
17293 /* Implicitly-defined methods (like the
17294 destructor for a class in which no destructor
17295 is explicitly declared) must not be defined
17296 until their definition is needed. So, we
17297 ignore interface specifications for
17298 compiler-generated functions. */
17299 && !DECL_ARTIFICIAL (decl1));
17300 struct c_fileinfo *finfo
17301 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
17302
17303 if (processing_template_decl)
17304 /* Don't mess with interface flags. */;
17305 else if (DECL_INTERFACE_KNOWN (decl1))
17306 {
17307 tree ctx = decl_function_context (decl1);
17308
17309 if (DECL_NOT_REALLY_EXTERN (decl1))
17310 DECL_EXTERNAL (decl1) = 0;
17311
17312 if (ctx != NULL_TREE && vague_linkage_p (ctx))
17313 /* This is a function in a local class in an extern inline
17314 or template function. */
17315 comdat_linkage (decl1);
17316 }
17317 /* If this function belongs to an interface, it is public.
17318 If it belongs to someone else's interface, it is also external.
17319 This only affects inlines and template instantiations. */
17320 else if (!finfo->interface_unknown && honor_interface)
17321 {
17322 if (DECL_DECLARED_INLINE_P (decl1)
17323 || DECL_TEMPLATE_INSTANTIATION (decl1))
17324 {
17325 DECL_EXTERNAL (decl1)
17326 = (finfo->interface_only
17327 || (DECL_DECLARED_INLINE_P (decl1)
17328 && ! flag_implement_inlines
17329 && !DECL_VINDEX (decl1)));
17330
17331 /* For WIN32 we also want to put these in linkonce sections. */
17332 maybe_make_one_only (decl1);
17333 }
17334 else
17335 DECL_EXTERNAL (decl1) = 0;
17336 DECL_INTERFACE_KNOWN (decl1) = 1;
17337 /* If this function is in an interface implemented in this file,
17338 make sure that the back end knows to emit this function
17339 here. */
17340 if (!DECL_EXTERNAL (decl1))
17341 mark_needed (decl1);
17342 }
17343 else if (finfo->interface_unknown && finfo->interface_only
17344 && honor_interface)
17345 {
17346 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
17347 interface, we will have both finfo->interface_unknown and
17348 finfo->interface_only set. In that case, we don't want to
17349 use the normal heuristics because someone will supply a
17350 #pragma implementation elsewhere, and deducing it here would
17351 produce a conflict. */
17352 comdat_linkage (decl1);
17353 DECL_EXTERNAL (decl1) = 0;
17354 DECL_INTERFACE_KNOWN (decl1) = 1;
17355 DECL_DEFER_OUTPUT (decl1) = 1;
17356 }
17357 else
17358 {
17359 /* This is a definition, not a reference.
17360 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
17361 if (!GNU_INLINE_P (decl1))
17362 DECL_EXTERNAL (decl1) = 0;
17363
17364 if ((DECL_DECLARED_INLINE_P (decl1)
17365 || DECL_TEMPLATE_INSTANTIATION (decl1))
17366 && ! DECL_INTERFACE_KNOWN (decl1))
17367 DECL_DEFER_OUTPUT (decl1) = 1;
17368 else
17369 DECL_INTERFACE_KNOWN (decl1) = 1;
17370 }
17371
17372 /* Determine the ELF visibility attribute for the function. We must not
17373 do this before calling "pushdecl", as we must allow "duplicate_decls"
17374 to merge any attributes appropriately. We also need to wait until
17375 linkage is set. */
17376 if (!DECL_CLONED_FUNCTION_P (decl1))
17377 determine_visibility (decl1);
17378
17379 if (!processing_template_decl)
17380 maybe_instantiate_noexcept (decl1);
17381
17382 begin_scope (sk_function_parms, decl1);
17383
17384 ++function_depth;
17385
17386 if (DECL_DESTRUCTOR_P (decl1)
17387 || (DECL_CONSTRUCTOR_P (decl1)
17388 && targetm.cxx.cdtor_returns_this ()))
17389 {
17390 cdtor_label = create_artificial_label (input_location);
17391 LABEL_DECL_CDTOR (cdtor_label) = true;
17392 }
17393
17394 start_fname_decls ();
17395
17396 store_parm_decls (current_function_parms);
17397
17398 if (!processing_template_decl
17399 && (flag_lifetime_dse > 1)
17400 && DECL_CONSTRUCTOR_P (decl1)
17401 && !DECL_CLONED_FUNCTION_P (decl1)
17402 /* Clobbering an empty base is harmful if it overlays real data. */
17403 && !is_empty_class (current_class_type)
17404 /* We can't clobber safely for an implicitly-defined default constructor
17405 because part of the initialization might happen before we enter the
17406 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
17407 && !implicit_default_ctor_p (decl1))
17408 finish_expr_stmt (build_clobber_this ());
17409
17410 if (!processing_template_decl
17411 && DECL_CONSTRUCTOR_P (decl1)
17412 && sanitize_flags_p (SANITIZE_VPTR)
17413 && !DECL_CLONED_FUNCTION_P (decl1)
17414 && !implicit_default_ctor_p (decl1))
17415 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
17416
17417 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
17418 start_lambda_scope (decl1);
17419
17420 return true;
17421 }
17422
17423
17424 /* Like start_preparsed_function, except that instead of a
17425 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
17426
17427 Returns true on success. If the DECLARATOR is not suitable
17428 for a function, we return false, which tells the parser to
17429 skip the entire function. */
17430
17431 bool
start_function(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrs)17432 start_function (cp_decl_specifier_seq *declspecs,
17433 const cp_declarator *declarator,
17434 tree attrs)
17435 {
17436 tree decl1;
17437
17438 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
17439 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
17440 if (decl1 == error_mark_node)
17441 return false;
17442
17443 if (DECL_MAIN_P (decl1))
17444 /* main must return int. grokfndecl should have corrected it
17445 (and issued a diagnostic) if the user got it wrong. */
17446 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
17447 integer_type_node));
17448
17449 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
17450 }
17451
17452 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
17453 FN. */
17454
17455 static bool
use_eh_spec_block(tree fn)17456 use_eh_spec_block (tree fn)
17457 {
17458 return (flag_exceptions && flag_enforce_eh_specs
17459 && !processing_template_decl
17460 /* We insert the EH_SPEC_BLOCK only in the original
17461 function; then, it is copied automatically to the
17462 clones. */
17463 && !DECL_CLONED_FUNCTION_P (fn)
17464 /* Implicitly-generated constructors and destructors have
17465 exception specifications. However, those specifications
17466 are the union of the possible exceptions specified by the
17467 constructors/destructors for bases and members, so no
17468 unallowed exception will ever reach this function. By
17469 not creating the EH_SPEC_BLOCK we save a little memory,
17470 and we avoid spurious warnings about unreachable
17471 code. */
17472 && !DECL_DEFAULTED_FN (fn)
17473 && !type_throw_all_p (TREE_TYPE (fn)));
17474 }
17475
17476 /* Helper function to push ARGS into the current lexical scope. DECL
17477 is the function declaration. NONPARMS is used to handle enum
17478 constants. */
17479
17480 void
do_push_parm_decls(tree decl,tree args,tree * nonparms)17481 do_push_parm_decls (tree decl, tree args, tree *nonparms)
17482 {
17483 /* If we're doing semantic analysis, then we'll call pushdecl
17484 for each of these. We must do them in reverse order so that
17485 they end in the correct forward order. */
17486 args = nreverse (args);
17487
17488 tree next;
17489 for (tree parm = args; parm; parm = next)
17490 {
17491 next = DECL_CHAIN (parm);
17492 if (TREE_CODE (parm) == PARM_DECL)
17493 pushdecl (parm);
17494 else if (nonparms)
17495 {
17496 /* If we find an enum constant or a type tag, put it aside for
17497 the moment. */
17498 TREE_CHAIN (parm) = NULL_TREE;
17499 *nonparms = chainon (*nonparms, parm);
17500 }
17501 }
17502
17503 /* Get the decls in their original chain order and record in the
17504 function. This is all and only the PARM_DECLs that were
17505 pushed into scope by the loop above. */
17506 DECL_ARGUMENTS (decl) = get_local_decls ();
17507 }
17508
17509 /* Store the parameter declarations into the current function declaration.
17510 This is called after parsing the parameter declarations, before
17511 digesting the body of the function.
17512
17513 Also install to binding contour return value identifier, if any. */
17514
17515 static void
store_parm_decls(tree current_function_parms)17516 store_parm_decls (tree current_function_parms)
17517 {
17518 tree fndecl = current_function_decl;
17519
17520 /* This is a chain of any other decls that came in among the parm
17521 declarations. If a parm is declared with enum {foo, bar} x;
17522 then CONST_DECLs for foo and bar are put here. */
17523 tree nonparms = NULL_TREE;
17524
17525 if (current_function_parms)
17526 {
17527 /* This case is when the function was defined with an ANSI prototype.
17528 The parms already have decls, so we need not do anything here
17529 except record them as in effect
17530 and complain if any redundant old-style parm decls were written. */
17531
17532 tree specparms = current_function_parms;
17533
17534 /* Must clear this because it might contain TYPE_DECLs declared
17535 at class level. */
17536 current_binding_level->names = NULL;
17537
17538 do_push_parm_decls (fndecl, specparms, &nonparms);
17539 }
17540 else
17541 DECL_ARGUMENTS (fndecl) = NULL_TREE;
17542
17543 /* Now store the final chain of decls for the arguments
17544 as the decl-chain of the current lexical scope.
17545 Put the enumerators in as well, at the front so that
17546 DECL_ARGUMENTS is not modified. */
17547 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
17548
17549 if (use_eh_spec_block (current_function_decl))
17550 current_eh_spec_block = begin_eh_spec_block ();
17551 }
17552
17553
17554 /* Set the return value of the constructor (if present). */
17555
17556 static void
finish_constructor_body(void)17557 finish_constructor_body (void)
17558 {
17559 tree val;
17560 tree exprstmt;
17561
17562 if (targetm.cxx.cdtor_returns_this ())
17563 {
17564 /* Any return from a constructor will end up here. */
17565 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
17566
17567 val = DECL_ARGUMENTS (current_function_decl);
17568 suppress_warning (val, OPT_Wuse_after_free);
17569 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17570 DECL_RESULT (current_function_decl), val);
17571 /* Return the address of the object. */
17572 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17573 add_stmt (exprstmt);
17574 }
17575 }
17576
17577 /* Do all the processing for the beginning of a destructor; set up the
17578 vtable pointers and cleanups for bases and members. */
17579
17580 static void
begin_destructor_body(void)17581 begin_destructor_body (void)
17582 {
17583 tree compound_stmt;
17584
17585 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
17586 issued an error message. We still want to try to process the
17587 body of the function, but initialize_vtbl_ptrs will crash if
17588 TYPE_BINFO is NULL. */
17589 if (COMPLETE_TYPE_P (current_class_type))
17590 {
17591 compound_stmt = begin_compound_stmt (0);
17592 /* Make all virtual function table pointers in non-virtual base
17593 classes point to CURRENT_CLASS_TYPE's virtual function
17594 tables. */
17595 initialize_vtbl_ptrs (current_class_ptr);
17596 finish_compound_stmt (compound_stmt);
17597
17598 if (flag_lifetime_dse
17599 /* Clobbering an empty base is harmful if it overlays real data. */
17600 && !is_empty_class (current_class_type))
17601 {
17602 if (sanitize_flags_p (SANITIZE_VPTR)
17603 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
17604 && TYPE_CONTAINS_VPTR_P (current_class_type))
17605 {
17606 tree binfo = TYPE_BINFO (current_class_type);
17607 tree ref
17608 = cp_build_fold_indirect_ref (current_class_ptr);
17609
17610 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
17611 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
17612 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
17613 NOP_EXPR, vtbl,
17614 tf_warning_or_error);
17615 /* If the vptr is shared with some virtual nearly empty base,
17616 don't clear it if not in charge, the dtor of the virtual
17617 nearly empty base will do that later. */
17618 if (CLASSTYPE_VBASECLASSES (current_class_type))
17619 {
17620 tree c = current_class_type;
17621 while (CLASSTYPE_PRIMARY_BINFO (c))
17622 {
17623 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c)))
17624 {
17625 stmt = convert_to_void (stmt, ICV_STATEMENT,
17626 tf_warning_or_error);
17627 stmt = build_if_in_charge (stmt);
17628 break;
17629 }
17630 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c));
17631 }
17632 }
17633 finish_decl_cleanup (NULL_TREE, stmt);
17634 }
17635 else
17636 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
17637 }
17638
17639 /* And insert cleanups for our bases and members so that they
17640 will be properly destroyed if we throw. */
17641 push_base_cleanups ();
17642 }
17643 }
17644
17645 /* At the end of every destructor we generate code to delete the object if
17646 necessary. Do that now. */
17647
17648 static void
finish_destructor_body(void)17649 finish_destructor_body (void)
17650 {
17651 tree exprstmt;
17652
17653 /* Any return from a destructor will end up here; that way all base
17654 and member cleanups will be run when the function returns. */
17655 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
17656
17657 if (targetm.cxx.cdtor_returns_this ())
17658 {
17659 tree val;
17660
17661 val = DECL_ARGUMENTS (current_function_decl);
17662 suppress_warning (val, OPT_Wuse_after_free);
17663 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
17664 DECL_RESULT (current_function_decl), val);
17665 /* Return the address of the object. */
17666 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
17667 add_stmt (exprstmt);
17668 }
17669 }
17670
17671 /* Do the necessary processing for the beginning of a function body, which
17672 in this case includes member-initializers, but not the catch clauses of
17673 a function-try-block. Currently, this means opening a binding level
17674 for the member-initializers (in a ctor), member cleanups (in a dtor),
17675 and capture proxies (in a lambda operator()). */
17676
17677 tree
begin_function_body(void)17678 begin_function_body (void)
17679 {
17680 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
17681 return NULL_TREE;
17682
17683 if (processing_template_decl)
17684 /* Do nothing now. */;
17685 else
17686 /* Always keep the BLOCK node associated with the outermost pair of
17687 curly braces of a function. These are needed for correct
17688 operation of dwarfout.c. */
17689 keep_next_level (true);
17690
17691 tree stmt = begin_compound_stmt (BCS_FN_BODY);
17692
17693 if (processing_template_decl)
17694 /* Do nothing now. */;
17695 else if (DECL_DESTRUCTOR_P (current_function_decl))
17696 begin_destructor_body ();
17697
17698 return stmt;
17699 }
17700
17701 /* Do the processing for the end of a function body. Currently, this means
17702 closing out the cleanups for fully-constructed bases and members, and in
17703 the case of the destructor, deleting the object if desired. Again, this
17704 is only meaningful for [cd]tors, since they are the only functions where
17705 there is a significant distinction between the main body and any
17706 function catch clauses. Handling, say, main() return semantics here
17707 would be wrong, as flowing off the end of a function catch clause for
17708 main() would also need to return 0. */
17709
17710 void
finish_function_body(tree compstmt)17711 finish_function_body (tree compstmt)
17712 {
17713 if (compstmt == NULL_TREE)
17714 return;
17715
17716 /* Close the block. */
17717 finish_compound_stmt (compstmt);
17718
17719 if (processing_template_decl)
17720 /* Do nothing now. */;
17721 else if (DECL_CONSTRUCTOR_P (current_function_decl))
17722 finish_constructor_body ();
17723 else if (DECL_DESTRUCTOR_P (current_function_decl))
17724 finish_destructor_body ();
17725 }
17726
17727 /* Given a function, returns the BLOCK corresponding to the outermost level
17728 of curly braces, skipping the artificial block created for constructor
17729 initializers. */
17730
17731 tree
outer_curly_brace_block(tree fndecl)17732 outer_curly_brace_block (tree fndecl)
17733 {
17734 tree block = DECL_INITIAL (fndecl);
17735 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17736 return block;
17737 block = BLOCK_SUBBLOCKS (block);
17738 if (BLOCK_OUTER_CURLY_BRACE_P (block))
17739 return block;
17740 block = BLOCK_SUBBLOCKS (block);
17741 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
17742 return block;
17743 }
17744
17745 /* If FNDECL is a class's key method, add the class to the list of
17746 keyed classes that should be emitted. */
17747
17748 static void
record_key_method_defined(tree fndecl)17749 record_key_method_defined (tree fndecl)
17750 {
17751 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
17752 && DECL_VIRTUAL_P (fndecl)
17753 && !processing_template_decl)
17754 {
17755 tree fnclass = DECL_CONTEXT (fndecl);
17756 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
17757 vec_safe_push (keyed_classes, fnclass);
17758 }
17759 }
17760
17761 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
17762 of "return *this;" immediately before its location, using FNDECL's
17763 first statement (if any) to give the indentation, if appropriate. */
17764
17765 static void
add_return_star_this_fixit(gcc_rich_location * richloc,tree fndecl)17766 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
17767 {
17768 location_t indent = UNKNOWN_LOCATION;
17769 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
17770 if (stmts)
17771 indent = EXPR_LOCATION (stmts);
17772 richloc->add_fixit_insert_formatted ("return *this;",
17773 richloc->get_loc (),
17774 indent);
17775 }
17776
17777 /* This function carries out the subset of finish_function operations needed
17778 to emit the compiler-generated outlined helper functions used by the
17779 coroutines implementation. */
17780
17781 static void
emit_coro_helper(tree helper)17782 emit_coro_helper (tree helper)
17783 {
17784 /* This is a partial set of the operations done by finish_function()
17785 plus emitting the result. */
17786 set_cfun (NULL);
17787 current_function_decl = helper;
17788 begin_scope (sk_function_parms, NULL);
17789 store_parm_decls (DECL_ARGUMENTS (helper));
17790 announce_function (helper);
17791 allocate_struct_function (helper, false);
17792 cfun->language = ggc_cleared_alloc<language_function> ();
17793 poplevel (1, 0, 1);
17794 maybe_save_constexpr_fundef (helper);
17795 /* We must start each function with a clear fold cache. */
17796 clear_fold_cache ();
17797 cp_fold_function (helper);
17798 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
17799 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
17800 /* This function has coroutine IFNs that we should handle in middle
17801 end lowering. */
17802 cfun->coroutine_component = true;
17803 cp_genericize (helper);
17804 expand_or_defer_fn (helper);
17805 }
17806
17807 /* Finish up a function declaration and compile that function
17808 all the way to assembler language output. The free the storage
17809 for the function definition. INLINE_P is TRUE if we just
17810 finished processing the body of an in-class inline function
17811 definition. (This processing will have taken place after the
17812 class definition is complete.) */
17813
17814 tree
finish_function(bool inline_p)17815 finish_function (bool inline_p)
17816 {
17817 tree fndecl = current_function_decl;
17818 tree fntype, ctype = NULL_TREE;
17819 tree resumer = NULL_TREE, destroyer = NULL_TREE;
17820 bool coro_p = flag_coroutines
17821 && !processing_template_decl
17822 && DECL_COROUTINE_P (fndecl);
17823 bool coro_emit_helpers = false;
17824
17825 /* When we get some parse errors, we can end up without a
17826 current_function_decl, so cope. */
17827 if (fndecl == NULL_TREE)
17828 return error_mark_node;
17829
17830 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17831 finish_lambda_scope ();
17832
17833 if (c_dialect_objc ())
17834 objc_finish_function ();
17835
17836 record_key_method_defined (fndecl);
17837
17838 fntype = TREE_TYPE (fndecl);
17839
17840 /* TREE_READONLY (fndecl) = 1;
17841 This caused &foo to be of type ptr-to-const-function
17842 which then got a warning when stored in a ptr-to-function variable. */
17843
17844 gcc_assert (building_stmt_list_p ());
17845 /* The current function is being defined, so its DECL_INITIAL should
17846 be set, and unless there's a multiple definition, it should be
17847 error_mark_node. */
17848 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
17849
17850 if (coro_p)
17851 {
17852 /* Only try to emit the coroutine outlined helper functions if the
17853 transforms succeeded. Otherwise, treat errors in the same way as
17854 a regular function. */
17855 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer);
17856
17857 /* We should handle coroutine IFNs in middle end lowering. */
17858 cfun->coroutine_component = true;
17859
17860 /* Do not try to process the ramp's EH unless outlining succeeded. */
17861 if (coro_emit_helpers && use_eh_spec_block (fndecl))
17862 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17863 (TREE_TYPE (fndecl)),
17864 current_eh_spec_block);
17865 }
17866 else
17867 /* For a cloned function, we've already got all the code we need;
17868 there's no need to add any extra bits. */
17869 if (!DECL_CLONED_FUNCTION_P (fndecl))
17870 {
17871 /* Make it so that `main' always returns 0 by default. */
17872 if (DECL_MAIN_P (current_function_decl))
17873 finish_return_stmt (integer_zero_node);
17874
17875 if (use_eh_spec_block (current_function_decl))
17876 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
17877 (TREE_TYPE (current_function_decl)),
17878 current_eh_spec_block);
17879 }
17880
17881 /* If we're saving up tree structure, tie off the function now. */
17882 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
17883
17884 finish_fname_decls ();
17885
17886 /* If this function can't throw any exceptions, remember that. */
17887 if (!processing_template_decl
17888 && !cp_function_chain->can_throw
17889 && !flag_non_call_exceptions
17890 && !decl_replaceable_p (fndecl,
17891 opt_for_fn (fndecl, flag_semantic_interposition)))
17892 TREE_NOTHROW (fndecl) = 1;
17893
17894 /* This must come after expand_function_end because cleanups might
17895 have declarations (from inline functions) that need to go into
17896 this function's blocks. */
17897
17898 /* If the current binding level isn't the outermost binding level
17899 for this function, either there is a bug, or we have experienced
17900 syntax errors and the statement tree is malformed. */
17901 if (current_binding_level->kind != sk_function_parms)
17902 {
17903 /* Make sure we have already experienced errors. */
17904 gcc_assert (errorcount);
17905
17906 /* Throw away the broken statement tree and extra binding
17907 levels. */
17908 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
17909
17910 while (current_binding_level->kind != sk_function_parms)
17911 {
17912 if (current_binding_level->kind == sk_class)
17913 pop_nested_class ();
17914 else
17915 poplevel (0, 0, 0);
17916 }
17917 }
17918 poplevel (1, 0, 1);
17919
17920 /* Statements should always be full-expressions at the outermost set
17921 of curly braces for a function. */
17922 gcc_assert (stmts_are_full_exprs_p ());
17923
17924 /* If there are no return statements in a function with auto return type,
17925 the return type is void. But if the declared type is something like
17926 auto*, this is an error. */
17927 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
17928 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17929 {
17930 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
17931 && !current_function_returns_value
17932 && !current_function_returns_null)
17933 {
17934 /* We haven't applied return type deduction because we haven't
17935 seen any return statements. Do that now. */
17936 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17937 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
17938 void_node, node, tf_warning_or_error,
17939 adc_return_type);
17940
17941 apply_deduced_return_type (fndecl, void_type_node);
17942 fntype = TREE_TYPE (fndecl);
17943 }
17944 else if (!current_function_returns_value
17945 && !current_function_returns_null)
17946 {
17947 error ("no return statements in function returning %qT",
17948 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
17949 inform (input_location, "only plain %<auto%> return type can be "
17950 "deduced to %<void%>");
17951 }
17952 }
17953
17954 /* Remember that we were in class scope. */
17955 if (current_class_name)
17956 ctype = current_class_type;
17957
17958 if (DECL_DELETED_FN (fndecl))
17959 {
17960 DECL_INITIAL (fndecl) = error_mark_node;
17961 DECL_SAVED_TREE (fndecl) = NULL_TREE;
17962 goto cleanup;
17963 }
17964
17965 // If this is a concept, check that the definition is reasonable.
17966 if (DECL_DECLARED_CONCEPT_P (fndecl))
17967 check_function_concept (fndecl);
17968
17969 if (flag_openmp)
17970 if (tree attr = lookup_attribute ("omp declare variant base",
17971 DECL_ATTRIBUTES (fndecl)))
17972 omp_declare_variant_finalize (fndecl, attr);
17973
17974 /* Complain if there's just no return statement. */
17975 if ((warn_return_type
17976 || (cxx_dialect >= cxx14
17977 && DECL_DECLARED_CONSTEXPR_P (fndecl)))
17978 && !VOID_TYPE_P (TREE_TYPE (fntype))
17979 && !dependent_type_p (TREE_TYPE (fntype))
17980 && !current_function_returns_value && !current_function_returns_null
17981 /* Don't complain if we abort or throw. */
17982 && !current_function_returns_abnormally
17983 /* Don't complain if there's an infinite loop. */
17984 && !current_function_infinite_loop
17985 /* Don't complain if we are declared noreturn. */
17986 && !TREE_THIS_VOLATILE (fndecl)
17987 && !DECL_NAME (DECL_RESULT (fndecl))
17988 && !warning_suppressed_p (fndecl, OPT_Wreturn_type)
17989 /* Structor return values (if any) are set by the compiler. */
17990 && !DECL_CONSTRUCTOR_P (fndecl)
17991 && !DECL_DESTRUCTOR_P (fndecl)
17992 && targetm.warn_func_return (fndecl))
17993 {
17994 gcc_rich_location richloc (input_location);
17995 /* Potentially add a "return *this;" fix-it hint for
17996 assignment operators. */
17997 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17998 {
17999 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
18000 if (TREE_CODE (valtype) == REFERENCE_TYPE
18001 && current_class_ref
18002 && same_type_ignoring_top_level_qualifiers_p
18003 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
18004 && global_dc->option_enabled (OPT_Wreturn_type,
18005 global_dc->lang_mask,
18006 global_dc->option_state))
18007 add_return_star_this_fixit (&richloc, fndecl);
18008 }
18009 if (cxx_dialect >= cxx14
18010 && DECL_DECLARED_CONSTEXPR_P (fndecl))
18011 error_at (&richloc, "no return statement in %<constexpr%> function "
18012 "returning non-void");
18013 else if (warning_at (&richloc, OPT_Wreturn_type,
18014 "no return statement in function returning "
18015 "non-void"))
18016 suppress_warning (fndecl, OPT_Wreturn_type);
18017 }
18018
18019 /* Lambda closure members are implicitly constexpr if possible. */
18020 if (cxx_dialect >= cxx17
18021 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
18022 DECL_DECLARED_CONSTEXPR_P (fndecl)
18023 = ((processing_template_decl
18024 || is_valid_constexpr_fn (fndecl, /*complain*/false))
18025 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
18026
18027 /* Save constexpr function body before it gets munged by
18028 the NRV transformation. */
18029 maybe_save_constexpr_fundef (fndecl);
18030
18031 /* Invoke the pre-genericize plugin before we start munging things. */
18032 if (!processing_template_decl)
18033 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
18034
18035 /* Perform delayed folding before NRV transformation. */
18036 if (!processing_template_decl
18037 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18038 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18039 cp_fold_function (fndecl);
18040
18041 /* Set up the named return value optimization, if we can. Candidate
18042 variables are selected in check_return_expr. */
18043 if (current_function_return_value)
18044 {
18045 tree r = current_function_return_value;
18046 tree outer;
18047
18048 if (r != error_mark_node
18049 /* This is only worth doing for fns that return in memory--and
18050 simpler, since we don't have to worry about promoted modes. */
18051 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
18052 /* Only allow this for variables declared in the outer scope of
18053 the function so we know that their lifetime always ends with a
18054 return; see g++.dg/opt/nrv6.C. We could be more flexible if
18055 we were to do this optimization in tree-ssa. */
18056 && (outer = outer_curly_brace_block (fndecl))
18057 && chain_member (r, BLOCK_VARS (outer)))
18058 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
18059
18060 current_function_return_value = NULL_TREE;
18061 }
18062
18063 /* Must mark the RESULT_DECL as being in this function. */
18064 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
18065
18066 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
18067 to the FUNCTION_DECL node itself. */
18068 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
18069
18070 /* Store the end of the function, so that we get good line number
18071 info for the epilogue. */
18072 cfun->function_end_locus = input_location;
18073
18074 /* Complain about parameters that are only set, but never otherwise used. */
18075 if (warn_unused_but_set_parameter
18076 && !processing_template_decl
18077 && errorcount == unused_but_set_errorcount
18078 && !DECL_CLONED_FUNCTION_P (fndecl))
18079 {
18080 tree decl;
18081
18082 for (decl = DECL_ARGUMENTS (fndecl);
18083 decl;
18084 decl = DECL_CHAIN (decl))
18085 if (TREE_USED (decl)
18086 && TREE_CODE (decl) == PARM_DECL
18087 && !DECL_READ_P (decl)
18088 && DECL_NAME (decl)
18089 && !DECL_ARTIFICIAL (decl)
18090 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter)
18091 && !DECL_IN_SYSTEM_HEADER (decl)
18092 && TREE_TYPE (decl) != error_mark_node
18093 && !TYPE_REF_P (TREE_TYPE (decl))
18094 && (!CLASS_TYPE_P (TREE_TYPE (decl))
18095 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
18096 warning_at (DECL_SOURCE_LOCATION (decl),
18097 OPT_Wunused_but_set_parameter,
18098 "parameter %qD set but not used", decl);
18099 unused_but_set_errorcount = errorcount;
18100 }
18101
18102 /* Complain about locally defined typedefs that are not used in this
18103 function. */
18104 maybe_warn_unused_local_typedefs ();
18105
18106 /* Possibly warn about unused parameters. */
18107 if (warn_unused_parameter
18108 && !processing_template_decl
18109 && !DECL_CLONED_FUNCTION_P (fndecl))
18110 do_warn_unused_parameter (fndecl);
18111
18112 /* Genericize before inlining. */
18113 if (!processing_template_decl
18114 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
18115 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
18116 cp_genericize (fndecl);
18117
18118 /* Emit the resumer and destroyer functions now, providing that we have
18119 not encountered some fatal error. */
18120 if (coro_emit_helpers)
18121 {
18122 emit_coro_helper (resumer);
18123 emit_coro_helper (destroyer);
18124 }
18125
18126 cleanup:
18127 /* We're leaving the context of this function, so zap cfun. It's still in
18128 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
18129 set_cfun (NULL);
18130 current_function_decl = NULL;
18131
18132 /* If this is an in-class inline definition, we may have to pop the
18133 bindings for the template parameters that we added in
18134 maybe_begin_member_template_processing when start_function was
18135 called. */
18136 if (inline_p)
18137 maybe_end_member_template_processing ();
18138
18139 /* Leave the scope of the class. */
18140 if (ctype)
18141 pop_nested_class ();
18142
18143 --function_depth;
18144
18145 /* Clean up. */
18146 current_function_decl = NULL_TREE;
18147
18148 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
18149 return fndecl;
18150 }
18151
18152 /* Create the FUNCTION_DECL for a function definition.
18153 DECLSPECS and DECLARATOR are the parts of the declaration;
18154 they describe the return type and the name of the function,
18155 but twisted together in a fashion that parallels the syntax of C.
18156
18157 This function creates a binding context for the function body
18158 as well as setting up the FUNCTION_DECL in current_function_decl.
18159
18160 Returns a FUNCTION_DECL on success.
18161
18162 If the DECLARATOR is not suitable for a function (it defines a datum
18163 instead), we return 0, which tells yyparse to report a parse error.
18164
18165 May return void_type_node indicating that this method is actually
18166 a friend. See grokfield for more details.
18167
18168 Came here with a `.pushlevel' .
18169
18170 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
18171 CHANGES TO CODE IN `grokfield'. */
18172
18173 tree
grokmethod(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrlist)18174 grokmethod (cp_decl_specifier_seq *declspecs,
18175 const cp_declarator *declarator, tree attrlist)
18176 {
18177 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
18178 &attrlist);
18179
18180 if (fndecl == error_mark_node)
18181 return error_mark_node;
18182
18183 if (attrlist)
18184 cplus_decl_attributes (&fndecl, attrlist, 0);
18185
18186 /* Pass friends other than inline friend functions back. */
18187 if (fndecl == void_type_node)
18188 return fndecl;
18189
18190 if (DECL_IN_AGGR_P (fndecl))
18191 {
18192 if (DECL_CLASS_SCOPE_P (fndecl))
18193 error ("%qD is already defined in class %qT", fndecl,
18194 DECL_CONTEXT (fndecl));
18195 return error_mark_node;
18196 }
18197
18198 check_template_shadow (fndecl);
18199
18200 /* p1779 ABI-Isolation makes inline not a default for in-class
18201 definitions in named module purview. If the user explicitly
18202 made it inline, grokdeclarator will already have done the right
18203 things. */
18204 if ((!named_module_purview_p ()
18205 || flag_module_implicit_inline
18206 /* Lambda's operator function remains inline. */
18207 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)))
18208 /* If the user explicitly asked for this to be inline, we don't
18209 need to do more, but more importantly we want to warn if we
18210 can't inline it. */
18211 && !DECL_DECLARED_INLINE_P (fndecl))
18212 {
18213 if (TREE_PUBLIC (fndecl))
18214 DECL_COMDAT (fndecl) = 1;
18215 DECL_DECLARED_INLINE_P (fndecl) = 1;
18216 /* It's ok if we can't inline this. */
18217 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
18218 }
18219
18220 /* We process method specializations in finish_struct_1. */
18221 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
18222 {
18223 /* Avoid calling decl_spec_seq... until we have to. */
18224 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
18225 fndecl = push_template_decl (fndecl, friendp);
18226 if (fndecl == error_mark_node)
18227 return fndecl;
18228 }
18229
18230 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend))
18231 {
18232 fndecl = copy_node (fndecl);
18233 TREE_CHAIN (fndecl) = NULL_TREE;
18234 }
18235
18236 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
18237
18238 DECL_IN_AGGR_P (fndecl) = 1;
18239 return fndecl;
18240 }
18241
18242
18243 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
18244 we can lay it out later, when and if its type becomes complete.
18245
18246 Also handle constexpr variables where the initializer involves
18247 an unlowered PTRMEM_CST because the class isn't complete yet. */
18248
18249 void
maybe_register_incomplete_var(tree var)18250 maybe_register_incomplete_var (tree var)
18251 {
18252 gcc_assert (VAR_P (var));
18253
18254 /* Keep track of variables with incomplete types. */
18255 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
18256 && DECL_EXTERNAL (var))
18257 {
18258 tree inner_type = TREE_TYPE (var);
18259
18260 while (TREE_CODE (inner_type) == ARRAY_TYPE)
18261 inner_type = TREE_TYPE (inner_type);
18262 inner_type = TYPE_MAIN_VARIANT (inner_type);
18263
18264 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
18265 /* RTTI TD entries are created while defining the type_info. */
18266 || (TYPE_LANG_SPECIFIC (inner_type)
18267 && TYPE_BEING_DEFINED (inner_type)))
18268 {
18269 incomplete_var iv = {var, inner_type};
18270 vec_safe_push (incomplete_vars, iv);
18271 }
18272 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
18273 && decl_constant_var_p (var)
18274 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
18275 {
18276 /* When the outermost open class is complete we can resolve any
18277 pointers-to-members. */
18278 tree context = outermost_open_class ();
18279 incomplete_var iv = {var, context};
18280 vec_safe_push (incomplete_vars, iv);
18281 }
18282 }
18283 }
18284
18285 /* Called when a class type (given by TYPE) is defined. If there are
18286 any existing VAR_DECLs whose type has been completed by this
18287 declaration, update them now. */
18288
18289 void
complete_vars(tree type)18290 complete_vars (tree type)
18291 {
18292 unsigned ix;
18293 incomplete_var *iv;
18294
18295 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
18296 {
18297 if (same_type_p (type, iv->incomplete_type))
18298 {
18299 tree var = iv->decl;
18300 tree type = TREE_TYPE (var);
18301
18302 if (type != error_mark_node
18303 && (TYPE_MAIN_VARIANT (strip_array_types (type))
18304 == iv->incomplete_type))
18305 {
18306 /* Complete the type of the variable. */
18307 complete_type (type);
18308 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
18309 if (COMPLETE_TYPE_P (type))
18310 layout_var_decl (var);
18311 }
18312
18313 /* Remove this entry from the list. */
18314 incomplete_vars->unordered_remove (ix);
18315 }
18316 else
18317 ix++;
18318 }
18319 }
18320
18321 /* If DECL is of a type which needs a cleanup, build and return an
18322 expression to perform that cleanup here. Return NULL_TREE if no
18323 cleanup need be done. DECL can also be a _REF when called from
18324 split_nonconstant_init_1. */
18325
18326 tree
cxx_maybe_build_cleanup(tree decl,tsubst_flags_t complain)18327 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
18328 {
18329 tree type;
18330 tree attr;
18331 tree cleanup;
18332
18333 /* Assume no cleanup is required. */
18334 cleanup = NULL_TREE;
18335
18336 if (error_operand_p (decl))
18337 return cleanup;
18338
18339 /* Handle "__attribute__((cleanup))". We run the cleanup function
18340 before the destructor since the destructor is what actually
18341 terminates the lifetime of the object. */
18342 if (DECL_P (decl))
18343 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
18344 else
18345 attr = NULL_TREE;
18346 if (attr)
18347 {
18348 tree id;
18349 tree fn;
18350 tree arg;
18351
18352 /* Get the name specified by the user for the cleanup function. */
18353 id = TREE_VALUE (TREE_VALUE (attr));
18354 /* Look up the name to find the cleanup function to call. It is
18355 important to use lookup_name here because that is what is
18356 used in c-common.cc:handle_cleanup_attribute when performing
18357 initial checks on the attribute. Note that those checks
18358 include ensuring that the function found is not an overloaded
18359 function, or an object with an overloaded call operator,
18360 etc.; we can rely on the fact that the function found is an
18361 ordinary FUNCTION_DECL. */
18362 fn = lookup_name (id);
18363 arg = build_address (decl);
18364 if (!mark_used (decl, complain) && !(complain & tf_error))
18365 return error_mark_node;
18366 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
18367 if (cleanup == error_mark_node)
18368 return error_mark_node;
18369 }
18370 /* Handle ordinary C++ destructors. */
18371 type = TREE_TYPE (decl);
18372 if (type_build_dtor_call (type))
18373 {
18374 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
18375 tree addr;
18376 tree call;
18377
18378 if (TREE_CODE (type) == ARRAY_TYPE)
18379 addr = decl;
18380 else
18381 addr = build_address (decl);
18382
18383 call = build_delete (input_location, TREE_TYPE (addr), addr,
18384 sfk_complete_destructor, flags, 0, complain);
18385 if (call == error_mark_node)
18386 cleanup = error_mark_node;
18387 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
18388 /* Discard the call. */;
18389 else if (decl_maybe_constant_destruction (decl, type)
18390 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
18391 cxx_constant_dtor (call, decl);
18392 else if (cleanup)
18393 cleanup = cp_build_compound_expr (cleanup, call, complain);
18394 else
18395 cleanup = call;
18396 }
18397
18398 /* build_delete sets the location of the destructor call to the
18399 current location, even though the destructor is going to be
18400 called later, at the end of the current scope. This can lead to
18401 a "jumpy" behavior for users of debuggers when they step around
18402 the end of the block. So let's unset the location of the
18403 destructor call instead. */
18404 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
18405 if (cleanup && CONVERT_EXPR_P (cleanup))
18406 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
18407
18408 if (cleanup
18409 && DECL_P (decl)
18410 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
18411 /* Treat objects with destructors as used; the destructor may do
18412 something substantive. */
18413 && !mark_used (decl, complain) && !(complain & tf_error))
18414 return error_mark_node;
18415
18416 if (cleanup && cfun && !processing_template_decl
18417 && !expr_noexcept_p (cleanup, tf_none))
18418 cp_function_chain->throwing_cleanup = true;
18419
18420 return cleanup;
18421 }
18422
18423
18424 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
18425 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
18426 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
18427
18428 tree
static_fn_type(tree memfntype)18429 static_fn_type (tree memfntype)
18430 {
18431 tree fntype;
18432 tree args;
18433
18434 if (TYPE_PTRMEMFUNC_P (memfntype))
18435 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
18436 if (INDIRECT_TYPE_P (memfntype)
18437 || TREE_CODE (memfntype) == FUNCTION_DECL)
18438 memfntype = TREE_TYPE (memfntype);
18439 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
18440 return memfntype;
18441 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
18442 args = TYPE_ARG_TYPES (memfntype);
18443 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
18444 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
18445 fntype = (cp_build_type_attribute_variant
18446 (fntype, TYPE_ATTRIBUTES (memfntype)));
18447 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
18448 return fntype;
18449 }
18450
18451 /* DECL was originally constructed as a non-static member function,
18452 but turned out to be static. Update it accordingly. */
18453
18454 void
revert_static_member_fn(tree decl)18455 revert_static_member_fn (tree decl)
18456 {
18457 tree stype = static_fn_type (decl);
18458 cp_cv_quals quals = type_memfn_quals (stype);
18459 cp_ref_qualifier rqual = type_memfn_rqual (stype);
18460
18461 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
18462 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
18463
18464 TREE_TYPE (decl) = stype;
18465
18466 if (DECL_ARGUMENTS (decl))
18467 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
18468 DECL_STATIC_FUNCTION_P (decl) = 1;
18469 }
18470
18471 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
18472 one of the language-independent trees. */
18473
18474 enum cp_tree_node_structure_enum
cp_tree_node_structure(union lang_tree_node * t)18475 cp_tree_node_structure (union lang_tree_node * t)
18476 {
18477 switch (TREE_CODE (&t->generic))
18478 {
18479 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
18480 case BASELINK: return TS_CP_BASELINK;
18481 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
18482 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
18483 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
18484 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
18485 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
18486 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR;
18487 case OVERLOAD: return TS_CP_OVERLOAD;
18488 case PTRMEM_CST: return TS_CP_PTRMEM;
18489 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
18490 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
18491 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
18492 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
18493 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
18494 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
18495 default: return TS_CP_GENERIC;
18496 }
18497 }
18498
18499 /* Build the void_list_node (void_type_node having been created). */
18500 tree
build_void_list_node(void)18501 build_void_list_node (void)
18502 {
18503 tree t = build_tree_list (NULL_TREE, void_type_node);
18504 return t;
18505 }
18506
18507 bool
cp_missing_noreturn_ok_p(tree decl)18508 cp_missing_noreturn_ok_p (tree decl)
18509 {
18510 /* A missing noreturn is ok for the `main' function. */
18511 return DECL_MAIN_P (decl);
18512 }
18513
18514 /* Return the decl used to identify the COMDAT group into which DECL should
18515 be placed. */
18516
18517 tree
cxx_comdat_group(tree decl)18518 cxx_comdat_group (tree decl)
18519 {
18520 /* Virtual tables, construction virtual tables, and virtual table
18521 tables all go in a single COMDAT group, named after the primary
18522 virtual table. */
18523 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
18524 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
18525 /* For all other DECLs, the COMDAT group is the mangled name of the
18526 declaration itself. */
18527 else
18528 {
18529 while (DECL_THUNK_P (decl))
18530 {
18531 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
18532 into the same section as the target function. In that case
18533 we must return target's name. */
18534 tree target = THUNK_TARGET (decl);
18535 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
18536 && DECL_SECTION_NAME (target) != NULL
18537 && DECL_ONE_ONLY (target))
18538 decl = target;
18539 else
18540 break;
18541 }
18542 }
18543
18544 return decl;
18545 }
18546
18547 /* Returns the return type for FN as written by the user, which may include
18548 a placeholder for a deduced return type. */
18549
18550 tree
fndecl_declared_return_type(tree fn)18551 fndecl_declared_return_type (tree fn)
18552 {
18553 fn = STRIP_TEMPLATE (fn);
18554 if (FNDECL_USED_AUTO (fn))
18555 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
18556
18557 return TREE_TYPE (TREE_TYPE (fn));
18558 }
18559
18560 /* Returns true iff DECL is a variable or function declared with an auto type
18561 that has not yet been deduced to a real type. */
18562
18563 bool
undeduced_auto_decl(tree decl)18564 undeduced_auto_decl (tree decl)
18565 {
18566 if (cxx_dialect < cxx11)
18567 return false;
18568 STRIP_ANY_LOCATION_WRAPPER (decl);
18569 return ((VAR_OR_FUNCTION_DECL_P (decl)
18570 || TREE_CODE (decl) == TEMPLATE_DECL)
18571 && type_uses_auto (TREE_TYPE (decl)));
18572 }
18573
18574 /* Complain if DECL has an undeduced return type. */
18575
18576 bool
require_deduced_type(tree decl,tsubst_flags_t complain)18577 require_deduced_type (tree decl, tsubst_flags_t complain)
18578 {
18579 if (undeduced_auto_decl (decl))
18580 {
18581 if (warning_suppressed_p (decl) && seen_error ())
18582 /* We probably already complained about deduction failure. */;
18583 else if (complain & tf_error)
18584 error ("use of %qD before deduction of %<auto%>", decl);
18585 note_failed_type_completion_for_satisfaction (decl);
18586 return false;
18587 }
18588 return true;
18589 }
18590
18591 /* Create a representation of the explicit-specifier with
18592 constant-expression of EXPR. COMPLAIN is as for tsubst. */
18593
18594 tree
build_explicit_specifier(tree expr,tsubst_flags_t complain)18595 build_explicit_specifier (tree expr, tsubst_flags_t complain)
18596 {
18597 if (check_for_bare_parameter_packs (expr))
18598 return error_mark_node;
18599
18600 if (instantiation_dependent_expression_p (expr))
18601 /* Wait for instantiation, tsubst_function_decl will handle it. */
18602 return expr;
18603
18604 expr = build_converted_constant_bool_expr (expr, complain);
18605 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
18606 expr = cxx_constant_value (expr);
18607 return expr;
18608 }
18609
18610 #include "gt-cp-decl.h"
18611