98ed79f3579e6aeff744cf1dc41b128921777a36
[gcc.git] / gcc / cp / decl.c
1 /* Process declarations and variables for -*- C++ -*- compiler.
2 Copyright (C) 1988-2020 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 "omp-general.h"
57
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
59 enum bad_spec_place {
60 BSP_VAR, /* variable */
61 BSP_PARM, /* parameter */
62 BSP_TYPE, /* type */
63 BSP_FIELD /* field */
64 };
65
66 static const char *redeclaration_error_message (tree, tree);
67
68 static int decl_jump_unsafe (tree);
69 static void require_complete_types_for_parms (tree);
70 static tree grok_reference_init (tree, tree, tree, int);
71 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
72 int, int, int, bool, int, tree, location_t);
73 static void check_static_variable_definition (tree, tree);
74 static void record_unknown_type (tree, const char *);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static tree local_variable_p_walkfn (tree *, int *, void *);
77 static const char *tag_name (enum tag_types);
78 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
79 static void maybe_deduce_size_from_array_init (tree, tree);
80 static void layout_var_decl (tree);
81 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
82 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
83 static void copy_type_enum (tree , tree);
84 static void check_function_type (tree, tree);
85 static void finish_constructor_body (void);
86 static void begin_destructor_body (void);
87 static void finish_destructor_body (void);
88 static void record_key_method_defined (tree);
89 static tree create_array_type_for_decl (tree, tree, tree, location_t);
90 static tree get_atexit_node (void);
91 static tree get_dso_handle_node (void);
92 static tree start_cleanup_fn (void);
93 static void end_cleanup_fn (void);
94 static tree cp_make_fname_decl (location_t, tree, int);
95 static void initialize_predefined_identifiers (void);
96 static tree check_special_function_return_type
97 (special_function_kind, tree, tree, int, const location_t*);
98 static tree push_cp_library_fn (enum tree_code, tree, int);
99 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
100 static void store_parm_decls (tree);
101 static void initialize_local_var (tree, tree);
102 static void expand_static_init (tree, tree);
103 static location_t smallest_type_location (const cp_decl_specifier_seq*);
104
105 /* The following symbols are subsumed in the cp_global_trees array, and
106 listed here individually for documentation purposes.
107
108 C++ extensions
109 tree wchar_decl_node;
110
111 tree vtable_entry_type;
112 tree delta_type_node;
113 tree __t_desc_type_node;
114
115 tree class_type_node;
116 tree unknown_type_node;
117
118 Array type `vtable_entry_type[]'
119
120 tree vtbl_type_node;
121 tree vtbl_ptr_type_node;
122
123 Namespaces,
124
125 tree std_node;
126 tree abi_node;
127
128 A FUNCTION_DECL which can call `abort'. Not necessarily the
129 one that the user will declare, but sufficient to be called
130 by routines that want to abort the program.
131
132 tree abort_fndecl;
133
134 Used by RTTI
135 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 tree tinfo_var_id; */
137
138 tree cp_global_trees[CPTI_MAX];
139
140 /* A list of objects which have constructors or destructors
141 which reside in the global scope. The decl is stored in
142 the TREE_VALUE slot and the initializer is stored
143 in the TREE_PURPOSE slot. */
144 tree static_aggregates;
145
146 /* Like static_aggregates, but for thread_local variables. */
147 tree tls_aggregates;
148
149 /* -- end of C++ */
150
151 /* A node for the integer constant 2. */
152
153 tree integer_two_node;
154
155 /* vector of static decls. */
156 vec<tree, va_gc> *static_decls;
157
158 /* vector of keyed classes. */
159 vec<tree, va_gc> *keyed_classes;
160
161 /* Used only for jumps to as-yet undefined labels, since jumps to
162 defined labels can have their validity checked immediately. */
163
164 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
165 struct named_label_use_entry *next;
166 /* The binding level to which this entry is *currently* attached.
167 This is initially the binding level in which the goto appeared,
168 but is modified as scopes are closed. */
169 cp_binding_level *binding_level;
170 /* The head of the names list that was current when the goto appeared,
171 or the inner scope popped. These are the decls that will *not* be
172 skipped when jumping to the label. */
173 tree names_in_scope;
174 /* The location of the goto, for error reporting. */
175 location_t o_goto_locus;
176 /* True if an OpenMP structured block scope has been closed since
177 the goto appeared. This means that the branch from the label will
178 illegally exit an OpenMP scope. */
179 bool in_omp_scope;
180 };
181
182 /* A list of all LABEL_DECLs in the function that have names. Here so
183 we can clear out their names' definitions at the end of the
184 function, and so we can check the validity of jumps to these labels. */
185
186 struct GTY((for_user)) named_label_entry {
187
188 tree name; /* Name of decl. */
189
190 tree label_decl; /* LABEL_DECL, unless deleted local label. */
191
192 named_label_entry *outer; /* Outer shadowed chain. */
193
194 /* The binding level to which the label is *currently* attached.
195 This is initially set to the binding level in which the label
196 is defined, but is modified as scopes are closed. */
197 cp_binding_level *binding_level;
198
199 /* The head of the names list that was current when the label was
200 defined, or the inner scope popped. These are the decls that will
201 be skipped when jumping to the label. */
202 tree names_in_scope;
203
204 /* A vector of all decls from all binding levels that would be
205 crossed by a backward branch to the label. */
206 vec<tree, va_gc> *bad_decls;
207
208 /* A list of uses of the label, before the label is defined. */
209 named_label_use_entry *uses;
210
211 /* The following bits are set after the label is defined, and are
212 updated as scopes are popped. They indicate that a jump to the
213 label will illegally enter a scope of the given flavor. */
214 bool in_try_scope;
215 bool in_catch_scope;
216 bool in_omp_scope;
217 bool in_transaction_scope;
218 bool in_constexpr_if;
219 };
220
221 #define named_labels cp_function_chain->x_named_labels
222 \f
223 /* The number of function bodies which we are currently processing.
224 (Zero if we are at namespace scope, one inside the body of a
225 function, two inside the body of a function in a local class, etc.) */
226 int function_depth;
227
228 /* Whether the exception-specifier is part of a function type (i.e. C++17). */
229 bool flag_noexcept_type;
230
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
234 deprecated items. */
235 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
236
237 \f
238 /* A list of VAR_DECLs whose type was incomplete at the time the
239 variable was declared. */
240
241 struct GTY(()) incomplete_var {
242 tree decl;
243 tree incomplete_type;
244 };
245
246
247 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
248 \f
249 /* Returns the kind of template specialization we are currently
250 processing, given that it's declaration contained N_CLASS_SCOPES
251 explicit scope qualifications. */
252
253 tmpl_spec_kind
254 current_tmpl_spec_kind (int n_class_scopes)
255 {
256 int n_template_parm_scopes = 0;
257 int seen_specialization_p = 0;
258 int innermost_specialization_p = 0;
259 cp_binding_level *b;
260
261 /* Scan through the template parameter scopes. */
262 for (b = current_binding_level;
263 b->kind == sk_template_parms;
264 b = b->level_chain)
265 {
266 /* If we see a specialization scope inside a parameter scope,
267 then something is wrong. That corresponds to a declaration
268 like:
269
270 template <class T> template <> ...
271
272 which is always invalid since [temp.expl.spec] forbids the
273 specialization of a class member template if the enclosing
274 class templates are not explicitly specialized as well. */
275 if (b->explicit_spec_p)
276 {
277 if (n_template_parm_scopes == 0)
278 innermost_specialization_p = 1;
279 else
280 seen_specialization_p = 1;
281 }
282 else if (seen_specialization_p == 1)
283 return tsk_invalid_member_spec;
284
285 ++n_template_parm_scopes;
286 }
287
288 /* Handle explicit instantiations. */
289 if (processing_explicit_instantiation)
290 {
291 if (n_template_parm_scopes != 0)
292 /* We've seen a template parameter list during an explicit
293 instantiation. For example:
294
295 template <class T> template void f(int);
296
297 This is erroneous. */
298 return tsk_invalid_expl_inst;
299 else
300 return tsk_expl_inst;
301 }
302
303 if (n_template_parm_scopes < n_class_scopes)
304 /* We've not seen enough template headers to match all the
305 specialized classes present. For example:
306
307 template <class T> void R<T>::S<T>::f(int);
308
309 This is invalid; there needs to be one set of template
310 parameters for each class. */
311 return tsk_insufficient_parms;
312 else if (n_template_parm_scopes == n_class_scopes)
313 /* We're processing a non-template declaration (even though it may
314 be a member of a template class.) For example:
315
316 template <class T> void S<T>::f(int);
317
318 The `class T' matches the `S<T>', leaving no template headers
319 corresponding to the `f'. */
320 return tsk_none;
321 else if (n_template_parm_scopes > n_class_scopes + 1)
322 /* We've got too many template headers. For example:
323
324 template <> template <class T> void f (T);
325
326 There need to be more enclosing classes. */
327 return tsk_excessive_parms;
328 else
329 /* This must be a template. It's of the form:
330
331 template <class T> template <class U> void S<T>::f(U);
332
333 This is a specialization if the innermost level was a
334 specialization; otherwise it's just a definition of the
335 template. */
336 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
337 }
338
339 /* Exit the current scope. */
340
341 void
342 finish_scope (void)
343 {
344 poplevel (0, 0, 0);
345 }
346
347 /* When a label goes out of scope, check to see if that label was used
348 in a valid manner, and issue any appropriate warnings or errors. */
349
350 static void
351 check_label_used (tree label)
352 {
353 if (!processing_template_decl)
354 {
355 if (DECL_INITIAL (label) == NULL_TREE)
356 {
357 location_t location;
358
359 error ("label %q+D used but not defined", label);
360 location = input_location;
361 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
362 /* Avoid crashing later. */
363 define_label (location, DECL_NAME (label));
364 }
365 else
366 warn_for_unused_label (label);
367 }
368 }
369
370 /* Helper function to sort named label entries in a vector by DECL_UID. */
371
372 static int
373 sort_labels (const void *a, const void *b)
374 {
375 tree label1 = *(tree const *) a;
376 tree label2 = *(tree const *) b;
377
378 /* DECL_UIDs can never be equal. */
379 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
380 }
381
382 /* At the end of a function, all labels declared within the function
383 go out of scope. BLOCK is the top-level block for the
384 function. */
385
386 static void
387 pop_labels (tree block)
388 {
389 if (!named_labels)
390 return;
391
392 /* We need to add the labels to the block chain, so debug
393 information is emitted. But, we want the order to be stable so
394 need to sort them first. Otherwise the debug output could be
395 randomly ordered. I guess it's mostly stable, unless the hash
396 table implementation changes. */
397 auto_vec<tree, 32> labels (named_labels->elements ());
398 hash_table<named_label_hash>::iterator end (named_labels->end ());
399 for (hash_table<named_label_hash>::iterator iter
400 (named_labels->begin ()); iter != end; ++iter)
401 {
402 named_label_entry *ent = *iter;
403
404 gcc_checking_assert (!ent->outer);
405 if (ent->label_decl)
406 labels.quick_push (ent->label_decl);
407 ggc_free (ent);
408 }
409 named_labels = NULL;
410 labels.qsort (sort_labels);
411
412 while (labels.length ())
413 {
414 tree label = labels.pop ();
415
416 DECL_CHAIN (label) = BLOCK_VARS (block);
417 BLOCK_VARS (block) = label;
418
419 check_label_used (label);
420 }
421 }
422
423 /* At the end of a block with local labels, restore the outer definition. */
424
425 static void
426 pop_local_label (tree id, tree label)
427 {
428 check_label_used (label);
429 named_label_entry **slot = named_labels->find_slot_with_hash
430 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
431 named_label_entry *ent = *slot;
432
433 if (ent->outer)
434 ent = ent->outer;
435 else
436 {
437 ent = ggc_cleared_alloc<named_label_entry> ();
438 ent->name = id;
439 }
440 *slot = ent;
441 }
442
443 /* The following two routines are used to interface to Objective-C++.
444 The binding level is purposely treated as an opaque type. */
445
446 void *
447 objc_get_current_scope (void)
448 {
449 return current_binding_level;
450 }
451
452 /* The following routine is used by the NeXT-style SJLJ exceptions;
453 variables get marked 'volatile' so as to not be clobbered by
454 _setjmp()/_longjmp() calls. All variables in the current scope,
455 as well as parent scopes up to (but not including) ENCLOSING_BLK
456 shall be thusly marked. */
457
458 void
459 objc_mark_locals_volatile (void *enclosing_blk)
460 {
461 cp_binding_level *scope;
462
463 for (scope = current_binding_level;
464 scope && scope != enclosing_blk;
465 scope = scope->level_chain)
466 {
467 tree decl;
468
469 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
470 objc_volatilize_decl (decl);
471
472 /* Do not climb up past the current function. */
473 if (scope->kind == sk_function_parms)
474 break;
475 }
476 }
477
478 /* True if B is the level for the condition of a constexpr if. */
479
480 static bool
481 level_for_constexpr_if (cp_binding_level *b)
482 {
483 return (b->kind == sk_cond && b->this_entity
484 && TREE_CODE (b->this_entity) == IF_STMT
485 && IF_STMT_CONSTEXPR_P (b->this_entity));
486 }
487
488 /* Update data for defined and undefined labels when leaving a scope. */
489
490 int
491 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
492 {
493 named_label_entry *ent = *slot;
494 cp_binding_level *obl = bl->level_chain;
495
496 if (ent->binding_level == bl)
497 {
498 tree decl;
499
500 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
501 TREE_LISTs representing OVERLOADs, so be careful. */
502 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
503 ? DECL_CHAIN (decl)
504 : TREE_CHAIN (decl)))
505 if (decl_jump_unsafe (decl))
506 vec_safe_push (ent->bad_decls, decl);
507
508 ent->binding_level = obl;
509 ent->names_in_scope = obl->names;
510 switch (bl->kind)
511 {
512 case sk_try:
513 ent->in_try_scope = true;
514 break;
515 case sk_catch:
516 ent->in_catch_scope = true;
517 break;
518 case sk_omp:
519 ent->in_omp_scope = true;
520 break;
521 case sk_transaction:
522 ent->in_transaction_scope = true;
523 break;
524 case sk_block:
525 if (level_for_constexpr_if (bl->level_chain))
526 ent->in_constexpr_if = true;
527 break;
528 default:
529 break;
530 }
531 }
532 else if (ent->uses)
533 {
534 struct named_label_use_entry *use;
535
536 for (use = ent->uses; use ; use = use->next)
537 if (use->binding_level == bl)
538 {
539 use->binding_level = obl;
540 use->names_in_scope = obl->names;
541 if (bl->kind == sk_omp)
542 use->in_omp_scope = true;
543 }
544 }
545
546 return 1;
547 }
548
549 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
550 when errors were reported, except for -Werror-unused-but-set-*. */
551 static int unused_but_set_errorcount;
552
553 /* Exit a binding level.
554 Pop the level off, and restore the state of the identifier-decl mappings
555 that were in effect when this level was entered.
556
557 If KEEP == 1, this level had explicit declarations, so
558 and create a "block" (a BLOCK node) for the level
559 to record its declarations and subblocks for symbol table output.
560
561 If FUNCTIONBODY is nonzero, this level is the body of a function,
562 so create a block as if KEEP were set and also clear out all
563 label names.
564
565 If REVERSE is nonzero, reverse the order of decls before putting
566 them into the BLOCK. */
567
568 tree
569 poplevel (int keep, int reverse, int functionbody)
570 {
571 tree link;
572 /* The chain of decls was accumulated in reverse order.
573 Put it into forward order, just for cleanliness. */
574 tree decls;
575 tree subblocks;
576 tree block;
577 tree decl;
578 scope_kind kind;
579
580 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
581 restart:
582
583 block = NULL_TREE;
584
585 gcc_assert (current_binding_level->kind != sk_class
586 && current_binding_level->kind != sk_namespace);
587
588 if (current_binding_level->kind == sk_cleanup)
589 functionbody = 0;
590 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
591
592 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
593
594 /* We used to use KEEP == 2 to indicate that the new block should go
595 at the beginning of the list of blocks at this binding level,
596 rather than the end. This hack is no longer used. */
597 gcc_assert (keep == 0 || keep == 1);
598
599 if (current_binding_level->keep)
600 keep = 1;
601
602 /* Any uses of undefined labels, and any defined labels, now operate
603 under constraints of next binding contour. */
604 if (cfun && !functionbody && named_labels)
605 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
606 (current_binding_level);
607
608 /* Get the decls in the order they were written.
609 Usually current_binding_level->names is in reverse order.
610 But parameter decls were previously put in forward order. */
611
612 decls = current_binding_level->names;
613 if (reverse)
614 {
615 decls = nreverse (decls);
616 current_binding_level->names = decls;
617 }
618
619 /* If there were any declarations or structure tags in that level,
620 or if this level is a function body,
621 create a BLOCK to record them for the life of this function. */
622 block = NULL_TREE;
623 /* Avoid function body block if possible. */
624 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
625 keep = 0;
626 else if (keep == 1 || functionbody)
627 block = make_node (BLOCK);
628 if (block != NULL_TREE)
629 {
630 BLOCK_VARS (block) = decls;
631 BLOCK_SUBBLOCKS (block) = subblocks;
632 }
633
634 /* In each subblock, record that this is its superior. */
635 if (keep >= 0)
636 for (link = subblocks; link; link = BLOCK_CHAIN (link))
637 BLOCK_SUPERCONTEXT (link) = block;
638
639 /* Before we remove the declarations first check for unused variables. */
640 if ((warn_unused_variable || warn_unused_but_set_variable)
641 && current_binding_level->kind != sk_template_parms
642 && !processing_template_decl)
643 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
644 {
645 /* There are cases where D itself is a TREE_LIST. See in
646 push_local_binding where the list of decls returned by
647 getdecls is built. */
648 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
649
650 tree type = TREE_TYPE (decl);
651 if (VAR_P (decl)
652 && (! TREE_USED (decl) || !DECL_READ_P (decl))
653 && ! DECL_IN_SYSTEM_HEADER (decl)
654 /* For structured bindings, consider only real variables, not
655 subobjects. */
656 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
657 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
658 && type != error_mark_node
659 && (!CLASS_TYPE_P (type)
660 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
661 || lookup_attribute ("warn_unused",
662 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
663 {
664 if (! TREE_USED (decl))
665 {
666 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
667 warning_at (DECL_SOURCE_LOCATION (decl),
668 OPT_Wunused_variable,
669 "unused structured binding declaration");
670 else
671 warning_at (DECL_SOURCE_LOCATION (decl),
672 OPT_Wunused_variable, "unused variable %qD", decl);
673 }
674 else if (DECL_CONTEXT (decl) == current_function_decl
675 // For -Wunused-but-set-variable leave references alone.
676 && !TYPE_REF_P (TREE_TYPE (decl))
677 && errorcount == unused_but_set_errorcount)
678 {
679 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
680 warning_at (DECL_SOURCE_LOCATION (decl),
681 OPT_Wunused_but_set_variable, "structured "
682 "binding declaration set but not used");
683 else
684 warning_at (DECL_SOURCE_LOCATION (decl),
685 OPT_Wunused_but_set_variable,
686 "variable %qD set but not used", decl);
687 unused_but_set_errorcount = errorcount;
688 }
689 }
690 }
691
692 /* Remove declarations for all the DECLs in this level. */
693 for (link = decls; link; link = TREE_CHAIN (link))
694 {
695 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
696 tree name = OVL_NAME (decl);
697
698 /* Remove the binding. */
699 if (TREE_CODE (decl) == LABEL_DECL)
700 pop_local_label (name, decl);
701 else
702 pop_local_binding (name, decl);
703 }
704
705 /* Restore the IDENTIFIER_TYPE_VALUEs. */
706 for (link = current_binding_level->type_shadowed;
707 link; link = TREE_CHAIN (link))
708 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
709
710 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
711 list if a `using' declaration put them there. The debugging
712 back ends won't understand OVERLOAD, so we remove them here.
713 Because the BLOCK_VARS are (temporarily) shared with
714 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
715 popped all the bindings. Also remove undeduced 'auto' decls,
716 which LTO doesn't understand, and can't have been used by anything. */
717 if (block)
718 {
719 tree* d;
720
721 for (d = &BLOCK_VARS (block); *d; )
722 {
723 if (TREE_CODE (*d) == TREE_LIST
724 || (!processing_template_decl
725 && undeduced_auto_decl (*d)))
726 *d = TREE_CHAIN (*d);
727 else
728 d = &DECL_CHAIN (*d);
729 }
730 }
731
732 /* If the level being exited is the top level of a function,
733 check over all the labels. */
734 if (functionbody)
735 {
736 if (block)
737 {
738 /* Since this is the top level block of a function, the vars are
739 the function's parameters. Don't leave them in the BLOCK
740 because they are found in the FUNCTION_DECL instead. */
741 BLOCK_VARS (block) = 0;
742 pop_labels (block);
743 }
744 else
745 pop_labels (subblocks);
746 }
747
748 kind = current_binding_level->kind;
749 if (kind == sk_cleanup)
750 {
751 tree stmt;
752
753 /* If this is a temporary binding created for a cleanup, then we'll
754 have pushed a statement list level. Pop that, create a new
755 BIND_EXPR for the block, and insert it into the stream. */
756 stmt = pop_stmt_list (current_binding_level->statement_list);
757 stmt = c_build_bind_expr (input_location, block, stmt);
758 add_stmt (stmt);
759 }
760
761 leave_scope ();
762 if (functionbody)
763 {
764 /* The current function is being defined, so its DECL_INITIAL
765 should be error_mark_node. */
766 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
767 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
768 if (subblocks)
769 {
770 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
771 {
772 if (BLOCK_SUBBLOCKS (subblocks))
773 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
774 }
775 else
776 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
777 }
778 }
779 else if (block)
780 current_binding_level->blocks
781 = block_chainon (current_binding_level->blocks, block);
782
783 /* If we did not make a block for the level just exited,
784 any blocks made for inner levels
785 (since they cannot be recorded as subblocks in that level)
786 must be carried forward so they will later become subblocks
787 of something else. */
788 else if (subblocks)
789 current_binding_level->blocks
790 = block_chainon (current_binding_level->blocks, subblocks);
791
792 /* Each and every BLOCK node created here in `poplevel' is important
793 (e.g. for proper debugging information) so if we created one
794 earlier, mark it as "used". */
795 if (block)
796 TREE_USED (block) = 1;
797
798 /* All temporary bindings created for cleanups are popped silently. */
799 if (kind == sk_cleanup)
800 goto restart;
801
802 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
803 return block;
804 }
805
806 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
807 /* Diagnose odr-used extern inline variables without definitions
808 in the current TU. */
809
810 int
811 wrapup_namespace_globals ()
812 {
813 if (vec<tree, va_gc> *statics = static_decls)
814 {
815 tree decl;
816 unsigned int i;
817 FOR_EACH_VEC_ELT (*statics, i, decl)
818 {
819 if (warn_unused_function
820 && TREE_CODE (decl) == FUNCTION_DECL
821 && DECL_INITIAL (decl) == 0
822 && DECL_EXTERNAL (decl)
823 && !TREE_PUBLIC (decl)
824 && !DECL_ARTIFICIAL (decl)
825 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
826 && !TREE_NO_WARNING (decl))
827 warning_at (DECL_SOURCE_LOCATION (decl),
828 OPT_Wunused_function,
829 "%qF declared %<static%> but never defined", decl);
830
831 if (VAR_P (decl)
832 && DECL_EXTERNAL (decl)
833 && DECL_INLINE_VAR_P (decl)
834 && DECL_ODR_USED (decl))
835 error_at (DECL_SOURCE_LOCATION (decl),
836 "odr-used inline variable %qD is not defined", decl);
837 }
838
839 /* Clear out the list, so we don't rescan next time. */
840 static_decls = NULL;
841
842 /* Write out any globals that need to be output. */
843 return wrapup_global_declarations (statics->address (),
844 statics->length ());
845 }
846 return 0;
847 }
848 \f
849 /* In C++, you don't have to write `struct S' to refer to `S'; you
850 can just use `S'. We accomplish this by creating a TYPE_DECL as
851 if the user had written `typedef struct S S'. Create and return
852 the TYPE_DECL for TYPE. */
853
854 tree
855 create_implicit_typedef (tree name, tree type)
856 {
857 tree decl;
858
859 decl = build_decl (input_location, TYPE_DECL, name, type);
860 DECL_ARTIFICIAL (decl) = 1;
861 /* There are other implicit type declarations, like the one *within*
862 a class that allows you to write `S::S'. We must distinguish
863 amongst these. */
864 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
865 TYPE_NAME (type) = decl;
866 TYPE_STUB_DECL (type) = decl;
867
868 return decl;
869 }
870
871 /* Function-scope local entities that need discriminators. Each entry
872 is a {decl,name} pair. VAR_DECLs for anon unions get their name
873 smashed, so we cannot rely on DECL_NAME. */
874
875 static GTY((deletable)) vec<tree, va_gc> *local_entities;
876
877 /* Determine the mangling discriminator of local DECL. There are
878 generally very few of these in any particular function. */
879
880 void
881 determine_local_discriminator (tree decl)
882 {
883 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
884 retrofit_lang_decl (decl);
885 tree ctx = DECL_CONTEXT (decl);
886 tree name = (TREE_CODE (decl) == TYPE_DECL
887 && TYPE_UNNAMED_P (TREE_TYPE (decl))
888 ? NULL_TREE : DECL_NAME (decl));
889 size_t nelts = vec_safe_length (local_entities);
890 for (size_t i = 0; i < nelts; i += 2)
891 {
892 tree *pair = &(*local_entities)[i];
893 tree d = pair[0];
894 tree n = pair[1];
895 gcc_checking_assert (d != decl);
896 if (name == n
897 && TREE_CODE (decl) == TREE_CODE (d)
898 && ctx == DECL_CONTEXT (d))
899 {
900 tree disc = integer_one_node;
901 if (DECL_DISCRIMINATOR (d))
902 disc = build_int_cst (TREE_TYPE (disc),
903 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1);
904 DECL_DISCRIMINATOR (decl) = disc;
905 /* Replace the saved decl. */
906 pair[0] = decl;
907 decl = NULL_TREE;
908 break;
909 }
910 }
911
912 if (decl)
913 {
914 vec_safe_reserve (local_entities, 2);
915 local_entities->quick_push (decl);
916 local_entities->quick_push (name);
917 }
918
919 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
920 }
921
922 \f
923
924 /* Returns true if functions FN1 and FN2 have equivalent trailing
925 requires clauses. */
926
927 static bool
928 function_requirements_equivalent_p (tree newfn, tree oldfn)
929 {
930 /* In the concepts TS, the combined constraints are compared. */
931 if (cxx_dialect < cxx2a)
932 {
933 tree ci1 = get_constraints (oldfn);
934 tree ci2 = get_constraints (newfn);
935 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE;
936 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE;
937 return cp_tree_equal (req1, req2);
938 }
939
940 /* Compare only trailing requirements. */
941 tree reqs1 = get_trailing_function_requirements (newfn);
942 tree reqs2 = get_trailing_function_requirements (oldfn);
943 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE))
944 return false;
945 return cp_tree_equal (reqs1, reqs2);
946 }
947
948 /* Subroutine of duplicate_decls: return truthvalue of whether
949 or not types of these decls match.
950
951 For C++, we must compare the parameter list so that `int' can match
952 `int&' in a parameter position, but `int&' is not confused with
953 `const int&'. */
954
955 int
956 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
957 {
958 int types_match;
959
960 if (newdecl == olddecl)
961 return 1;
962
963 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
964 /* If the two DECLs are not even the same kind of thing, we're not
965 interested in their types. */
966 return 0;
967
968 gcc_assert (DECL_P (newdecl));
969
970 if (TREE_CODE (newdecl) == FUNCTION_DECL)
971 {
972 tree f1 = TREE_TYPE (newdecl);
973 tree f2 = TREE_TYPE (olddecl);
974 tree p1 = TYPE_ARG_TYPES (f1);
975 tree p2 = TYPE_ARG_TYPES (f2);
976 tree r2;
977
978 /* Specializations of different templates are different functions
979 even if they have the same type. */
980 tree t1 = (DECL_USE_TEMPLATE (newdecl)
981 ? DECL_TI_TEMPLATE (newdecl)
982 : NULL_TREE);
983 tree t2 = (DECL_USE_TEMPLATE (olddecl)
984 ? DECL_TI_TEMPLATE (olddecl)
985 : NULL_TREE);
986 if (t1 != t2)
987 return 0;
988
989 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
990 && ! (DECL_EXTERN_C_P (newdecl)
991 && DECL_EXTERN_C_P (olddecl)))
992 return 0;
993
994 /* A new declaration doesn't match a built-in one unless it
995 is also extern "C". */
996 if (DECL_IS_BUILTIN (olddecl)
997 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
998 return 0;
999
1000 if (TREE_CODE (f1) != TREE_CODE (f2))
1001 return 0;
1002
1003 /* A declaration with deduced return type should use its pre-deduction
1004 type for declaration matching. */
1005 r2 = fndecl_declared_return_type (olddecl);
1006
1007 if (same_type_p (TREE_TYPE (f1), r2))
1008 {
1009 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1010 && fndecl_built_in_p (olddecl))
1011 {
1012 types_match = self_promoting_args_p (p1);
1013 if (p1 == void_list_node)
1014 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1015 }
1016 else
1017 types_match =
1018 compparms (p1, p2)
1019 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1020 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1021 || comp_type_attributes (TREE_TYPE (newdecl),
1022 TREE_TYPE (olddecl)) != 0);
1023 }
1024 else
1025 types_match = 0;
1026
1027 /* Two function declarations match if either has a requires-clause
1028 then both have a requires-clause and their constraints-expressions
1029 are equivalent. */
1030 if (types_match && flag_concepts)
1031 types_match = function_requirements_equivalent_p (newdecl, olddecl);
1032
1033 /* The decls dont match if they correspond to two different versions
1034 of the same function. Disallow extern "C" functions to be
1035 versions for now. */
1036 if (types_match
1037 && !DECL_EXTERN_C_P (newdecl)
1038 && !DECL_EXTERN_C_P (olddecl)
1039 && record_versions
1040 && maybe_version_functions (newdecl, olddecl,
1041 (!DECL_FUNCTION_VERSIONED (newdecl)
1042 || !DECL_FUNCTION_VERSIONED (olddecl))))
1043 return 0;
1044 }
1045 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1046 {
1047 if (!template_heads_equivalent_p (newdecl, olddecl))
1048 return 0;
1049
1050 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1051 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1052
1053 if (TREE_CODE (newres) != TREE_CODE (oldres))
1054 return 0;
1055
1056 /* Two template types match if they are the same. Otherwise, compare
1057 the underlying declarations. */
1058 if (TREE_CODE (newres) == TYPE_DECL)
1059 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres));
1060 else
1061 types_match = decls_match (newres, oldres);
1062 }
1063 else
1064 {
1065 /* Need to check scope for variable declaration (VAR_DECL).
1066 For typedef (TYPE_DECL), scope is ignored. */
1067 if (VAR_P (newdecl)
1068 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1069 /* [dcl.link]
1070 Two declarations for an object with C language linkage
1071 with the same name (ignoring the namespace that qualify
1072 it) that appear in different namespace scopes refer to
1073 the same object. */
1074 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1075 return 0;
1076
1077 if (TREE_TYPE (newdecl) == error_mark_node)
1078 types_match = TREE_TYPE (olddecl) == error_mark_node;
1079 else if (TREE_TYPE (olddecl) == NULL_TREE)
1080 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1081 else if (TREE_TYPE (newdecl) == NULL_TREE)
1082 types_match = 0;
1083 else
1084 types_match = comptypes (TREE_TYPE (newdecl),
1085 TREE_TYPE (olddecl),
1086 COMPARE_REDECLARATION);
1087 }
1088
1089 return types_match;
1090 }
1091
1092 /* NEWDECL and OLDDECL have identical signatures. If they are
1093 different versions adjust them and return true.
1094 If RECORD is set to true, record function versions. */
1095
1096 bool
1097 maybe_version_functions (tree newdecl, tree olddecl, bool record)
1098 {
1099 if (!targetm.target_option.function_versions (newdecl, olddecl))
1100 return false;
1101
1102 if (!DECL_FUNCTION_VERSIONED (olddecl))
1103 {
1104 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1105 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1106 mangle_decl (olddecl);
1107 }
1108
1109 if (!DECL_FUNCTION_VERSIONED (newdecl))
1110 {
1111 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1112 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1113 mangle_decl (newdecl);
1114 }
1115
1116 if (record)
1117 cgraph_node::record_function_versions (olddecl, newdecl);
1118
1119 return true;
1120 }
1121
1122 /* If NEWDECL is `static' and an `extern' was seen previously,
1123 warn about it. OLDDECL is the previous declaration.
1124
1125 Note that this does not apply to the C++ case of declaring
1126 a variable `extern const' and then later `const'.
1127
1128 Don't complain about built-in functions, since they are beyond
1129 the user's control. */
1130
1131 void
1132 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1133 {
1134 if (TREE_CODE (newdecl) == TYPE_DECL
1135 || TREE_CODE (newdecl) == TEMPLATE_DECL
1136 || TREE_CODE (newdecl) == CONST_DECL
1137 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1138 return;
1139
1140 /* Don't get confused by static member functions; that's a different
1141 use of `static'. */
1142 if (TREE_CODE (newdecl) == FUNCTION_DECL
1143 && DECL_STATIC_FUNCTION_P (newdecl))
1144 return;
1145
1146 /* If the old declaration was `static', or the new one isn't, then
1147 everything is OK. */
1148 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1149 return;
1150
1151 /* It's OK to declare a builtin function as `static'. */
1152 if (TREE_CODE (olddecl) == FUNCTION_DECL
1153 && DECL_ARTIFICIAL (olddecl))
1154 return;
1155
1156 auto_diagnostic_group d;
1157 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1158 "%qD was declared %<extern%> and later %<static%>", newdecl))
1159 inform (DECL_SOURCE_LOCATION (olddecl),
1160 "previous declaration of %qD", olddecl);
1161 }
1162
1163 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1164 function templates. If their exception specifications do not
1165 match, issue a diagnostic. */
1166
1167 static void
1168 check_redeclaration_exception_specification (tree new_decl,
1169 tree old_decl)
1170 {
1171 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1172 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1173
1174 /* Two default specs are equivalent, don't force evaluation. */
1175 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1176 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1177 return;
1178
1179 if (!type_dependent_expression_p (old_decl))
1180 {
1181 maybe_instantiate_noexcept (new_decl);
1182 maybe_instantiate_noexcept (old_decl);
1183 }
1184 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1185 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1186
1187 /* [except.spec]
1188
1189 If any declaration of a function has an exception-specification,
1190 all declarations, including the definition and an explicit
1191 specialization, of that function shall have an
1192 exception-specification with the same set of type-ids. */
1193 if (! DECL_IS_BUILTIN (old_decl)
1194 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1195 {
1196 const char *const msg
1197 = G_("declaration of %qF has a different exception specifier");
1198 bool complained = true;
1199 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1200 auto_diagnostic_group d;
1201 if (DECL_IN_SYSTEM_HEADER (old_decl))
1202 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1203 else if (!flag_exceptions)
1204 /* We used to silently permit mismatched eh specs with
1205 -fno-exceptions, so make them a pedwarn now. */
1206 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1207 else
1208 error_at (new_loc, msg, new_decl);
1209 if (complained)
1210 inform (DECL_SOURCE_LOCATION (old_decl),
1211 "from previous declaration %qF", old_decl);
1212 }
1213 }
1214
1215 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1216 Otherwise issue diagnostics. */
1217
1218 static bool
1219 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1220 {
1221 old_decl = STRIP_TEMPLATE (old_decl);
1222 new_decl = STRIP_TEMPLATE (new_decl);
1223 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1224 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1225 return true;
1226 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1227 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1228 {
1229 if (TREE_CODE (old_decl) != FUNCTION_DECL)
1230 return true;
1231 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1232 == DECL_IMMEDIATE_FUNCTION_P (new_decl))
1233 return true;
1234 }
1235 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1236 {
1237 if (fndecl_built_in_p (old_decl))
1238 {
1239 /* Hide a built-in declaration. */
1240 DECL_DECLARED_CONSTEXPR_P (old_decl)
1241 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1242 if (DECL_IMMEDIATE_FUNCTION_P (new_decl))
1243 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl);
1244 return true;
1245 }
1246 /* 7.1.5 [dcl.constexpr]
1247 Note: An explicit specialization can differ from the template
1248 declaration with respect to the constexpr specifier. */
1249 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1250 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1251 return true;
1252
1253 const char *kind = "constexpr";
1254 if (DECL_IMMEDIATE_FUNCTION_P (old_decl)
1255 || DECL_IMMEDIATE_FUNCTION_P (new_decl))
1256 kind = "consteval";
1257 error_at (DECL_SOURCE_LOCATION (new_decl),
1258 "redeclaration %qD differs in %qs "
1259 "from previous declaration", new_decl,
1260 kind);
1261 inform (DECL_SOURCE_LOCATION (old_decl),
1262 "previous declaration %qD", old_decl);
1263 return false;
1264 }
1265 return true;
1266 }
1267
1268 // If OLDDECL and NEWDECL are concept declarations with the same type
1269 // (i.e., and template parameters), but different requirements,
1270 // emit diagnostics and return true. Otherwise, return false.
1271 static inline bool
1272 check_concept_refinement (tree olddecl, tree newdecl)
1273 {
1274 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1275 return false;
1276
1277 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1278 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1279 if (TREE_CODE (d1) != TREE_CODE (d2))
1280 return false;
1281
1282 tree t1 = TREE_TYPE (d1);
1283 tree t2 = TREE_TYPE (d2);
1284 if (TREE_CODE (d1) == FUNCTION_DECL)
1285 {
1286 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1287 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1288 DECL_TEMPLATE_PARMS (newdecl))
1289 && !equivalently_constrained (olddecl, newdecl))
1290 {
1291 error ("cannot specialize concept %q#D", olddecl);
1292 return true;
1293 }
1294 }
1295 return false;
1296 }
1297
1298 /* DECL is a redeclaration of a function or function template. If
1299 it does have default arguments issue a diagnostic. Note: this
1300 function is used to enforce the requirements in C++11 8.3.6 about
1301 no default arguments in redeclarations. */
1302
1303 static void
1304 check_redeclaration_no_default_args (tree decl)
1305 {
1306 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1307
1308 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1309 t && t != void_list_node; t = TREE_CHAIN (t))
1310 if (TREE_PURPOSE (t))
1311 {
1312 permerror (DECL_SOURCE_LOCATION (decl),
1313 "redeclaration of %q#D may not have default "
1314 "arguments", decl);
1315 return;
1316 }
1317 }
1318
1319 /* NEWDECL is a redeclaration of a function or function template OLDDECL,
1320 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of
1321 the TEMPLATE_DECLs in case of function templates). This function is used
1322 to enforce the final part of C++17 11.3.6/4, about a single declaration:
1323 "If a friend declaration specifies a default argument expression, that
1324 declaration shall be a definition and shall be the only declaration of
1325 the function or function template in the translation unit." */
1326
1327 static void
1328 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl,
1329 bool olddecl_hidden_friend_p)
1330 {
1331 if (!olddecl_hidden_friend_p && !DECL_FRIEND_P (newdecl))
1332 return;
1333
1334 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1335 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1336
1337 for (; t1 && t1 != void_list_node;
1338 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1339 if ((olddecl_hidden_friend_p && TREE_PURPOSE (t1))
1340 || (DECL_FRIEND_P (newdecl) && TREE_PURPOSE (t2)))
1341 {
1342 auto_diagnostic_group d;
1343 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1344 "friend declaration of %q#D specifies default "
1345 "arguments and isn%'t the only declaration", newdecl))
1346 inform (DECL_SOURCE_LOCATION (olddecl),
1347 "previous declaration of %q#D", olddecl);
1348 return;
1349 }
1350 }
1351
1352 /* Merge tree bits that correspond to attributes noreturn, nothrow,
1353 const, malloc, and pure from NEWDECL with those of OLDDECL. */
1354
1355 static void
1356 merge_attribute_bits (tree newdecl, tree olddecl)
1357 {
1358 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1359 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl);
1360 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1361 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl);
1362 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1363 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl);
1364 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1365 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl);
1366 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1367 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl);
1368 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl);
1369 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl);
1370 }
1371
1372 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1373 && lookup_attribute ("gnu_inline", \
1374 DECL_ATTRIBUTES (fn)))
1375
1376 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl
1377 ambiguates olddecl. Returns true if an error occurs. */
1378
1379 static bool
1380 duplicate_function_template_decls (tree newdecl, tree olddecl)
1381 {
1382
1383 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1384 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1385 /* Function template declarations can be differentiated by parameter
1386 and return type. */
1387 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)),
1388 TYPE_ARG_TYPES (TREE_TYPE (newres)))
1389 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1390 TREE_TYPE (TREE_TYPE (olddecl))))
1391 {
1392 /* ... and also by their template-heads and requires-clauses. */
1393 if (template_heads_equivalent_p (newdecl, olddecl)
1394 && function_requirements_equivalent_p (newres, oldres))
1395 {
1396 error ("ambiguating new declaration %q+#D", newdecl);
1397 inform (DECL_SOURCE_LOCATION (olddecl),
1398 "old declaration %q#D", olddecl);
1399 return true;
1400 }
1401
1402 /* FIXME: The types are the same but the are differences
1403 in either the template heads or function requirements.
1404 We should be able to diagnose a set of common errors
1405 stemming from these declarations. For example:
1406
1407 template<typename T> requires C void f(...);
1408 template<typename T> void f(...) requires C;
1409
1410 These are functionally equivalent but not equivalent. */
1411 }
1412
1413 return false;
1414 }
1415
1416 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1417 If the redeclaration is invalid, a diagnostic is issued, and the
1418 error_mark_node is returned. Otherwise, OLDDECL is returned.
1419
1420 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1421 returned.
1422
1423 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1424
1425 tree
1426 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1427 {
1428 unsigned olddecl_uid = DECL_UID (olddecl);
1429 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1430 int olddecl_hidden_friend = 0;
1431 int new_defines_function = 0;
1432 tree new_template_info;
1433 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl);
1434 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl);
1435
1436 if (newdecl == olddecl)
1437 return olddecl;
1438
1439 types_match = decls_match (newdecl, olddecl);
1440
1441 /* If either the type of the new decl or the type of the old decl is an
1442 error_mark_node, then that implies that we have already issued an
1443 error (earlier) for some bogus type specification, and in that case,
1444 it is rather pointless to harass the user with yet more error message
1445 about the same declaration, so just pretend the types match here. */
1446 if (TREE_TYPE (newdecl) == error_mark_node
1447 || TREE_TYPE (olddecl) == error_mark_node)
1448 return error_mark_node;
1449
1450 /* Check for redeclaration and other discrepancies. */
1451 if (TREE_CODE (olddecl) == FUNCTION_DECL
1452 && DECL_ARTIFICIAL (olddecl))
1453 {
1454 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1455 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1456 {
1457 /* Avoid warnings redeclaring built-ins which have not been
1458 explicitly declared. */
1459 if (DECL_ANTICIPATED (olddecl))
1460 {
1461 if (TREE_PUBLIC (newdecl)
1462 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1463 warning_at (newdecl_loc,
1464 OPT_Wbuiltin_declaration_mismatch,
1465 "built-in function %qD declared as non-function",
1466 newdecl);
1467 return NULL_TREE;
1468 }
1469
1470 /* If you declare a built-in or predefined function name as static,
1471 the old definition is overridden, but optionally warn this was a
1472 bad choice of name. */
1473 if (! TREE_PUBLIC (newdecl))
1474 {
1475 warning_at (newdecl_loc,
1476 OPT_Wshadow,
1477 fndecl_built_in_p (olddecl)
1478 ? G_("shadowing built-in function %q#D")
1479 : G_("shadowing library function %q#D"), olddecl);
1480 /* Discard the old built-in function. */
1481 return NULL_TREE;
1482 }
1483 /* If the built-in is not ansi, then programs can override
1484 it even globally without an error. */
1485 else if (! fndecl_built_in_p (olddecl))
1486 warning_at (newdecl_loc, 0,
1487 "library function %q#D redeclared as non-function %q#D",
1488 olddecl, newdecl);
1489 else
1490 error_at (newdecl_loc,
1491 "declaration of %q#D conflicts with built-in "
1492 "declaration %q#D", newdecl, olddecl);
1493 return NULL_TREE;
1494 }
1495 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1496 {
1497 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1498 error_at (newdecl_loc,
1499 "redeclaration of %<pragma omp declare reduction%>");
1500 inform (olddecl_loc,
1501 "previous %<pragma omp declare reduction%> declaration");
1502 return error_mark_node;
1503 }
1504 else if (!types_match)
1505 {
1506 /* Avoid warnings redeclaring built-ins which have not been
1507 explicitly declared. */
1508 if (DECL_ANTICIPATED (olddecl))
1509 {
1510 tree t1, t2;
1511
1512 /* A new declaration doesn't match a built-in one unless it
1513 is also extern "C". */
1514 gcc_assert (DECL_IS_BUILTIN (olddecl));
1515 gcc_assert (DECL_EXTERN_C_P (olddecl));
1516 if (!DECL_EXTERN_C_P (newdecl))
1517 return NULL_TREE;
1518
1519 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1520 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1521 t1 || t2;
1522 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1523 {
1524 if (!t1 || !t2)
1525 break;
1526 /* FILE, tm types are not known at the time
1527 we create the builtins. */
1528 for (unsigned i = 0;
1529 i < sizeof (builtin_structptr_types)
1530 / sizeof (builtin_structptr_type);
1531 ++i)
1532 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1533 {
1534 tree t = TREE_VALUE (t1);
1535
1536 if (TYPE_PTR_P (t)
1537 && TYPE_IDENTIFIER (TREE_TYPE (t))
1538 == get_identifier (builtin_structptr_types[i].str)
1539 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1540 {
1541 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1542
1543 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1544 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1545 types_match = decls_match (newdecl, olddecl);
1546 if (types_match)
1547 return duplicate_decls (newdecl, olddecl,
1548 newdecl_is_friend);
1549 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1550 }
1551 goto next_arg;
1552 }
1553
1554 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1555 break;
1556 next_arg:;
1557 }
1558
1559 warning_at (newdecl_loc,
1560 OPT_Wbuiltin_declaration_mismatch,
1561 "declaration of %q#D conflicts with built-in "
1562 "declaration %q#D", newdecl, olddecl);
1563 }
1564 else if ((DECL_EXTERN_C_P (newdecl)
1565 && DECL_EXTERN_C_P (olddecl))
1566 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1567 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1568 {
1569 /* Don't really override olddecl for __* prefixed builtins
1570 except for __[^b]*_chk, the compiler might be using those
1571 explicitly. */
1572 if (fndecl_built_in_p (olddecl))
1573 {
1574 tree id = DECL_NAME (olddecl);
1575 const char *name = IDENTIFIER_POINTER (id);
1576 size_t len;
1577
1578 if (name[0] == '_'
1579 && name[1] == '_'
1580 && (strncmp (name + 2, "builtin_",
1581 strlen ("builtin_")) == 0
1582 || (len = strlen (name)) <= strlen ("___chk")
1583 || memcmp (name + len - strlen ("_chk"),
1584 "_chk", strlen ("_chk") + 1) != 0))
1585 {
1586 if (DECL_INITIAL (newdecl))
1587 {
1588 error_at (newdecl_loc,
1589 "definition of %q#D ambiguates built-in "
1590 "declaration %q#D", newdecl, olddecl);
1591 return error_mark_node;
1592 }
1593 auto_diagnostic_group d;
1594 if (permerror (newdecl_loc,
1595 "new declaration %q#D ambiguates built-in"
1596 " declaration %q#D", newdecl, olddecl)
1597 && flag_permissive)
1598 inform (newdecl_loc,
1599 "ignoring the %q#D declaration", newdecl);
1600 return flag_permissive ? olddecl : error_mark_node;
1601 }
1602 }
1603
1604 /* A near match; override the builtin. */
1605
1606 if (TREE_PUBLIC (newdecl))
1607 warning_at (newdecl_loc,
1608 OPT_Wbuiltin_declaration_mismatch,
1609 "new declaration %q#D ambiguates built-in "
1610 "declaration %q#D", newdecl, olddecl);
1611 else
1612 warning (OPT_Wshadow,
1613 fndecl_built_in_p (olddecl)
1614 ? G_("shadowing built-in function %q#D")
1615 : G_("shadowing library function %q#D"), olddecl);
1616 }
1617 else
1618 /* Discard the old built-in function. */
1619 return NULL_TREE;
1620
1621 /* Replace the old RTL to avoid problems with inlining. */
1622 COPY_DECL_RTL (newdecl, olddecl);
1623 }
1624 /* Even if the types match, prefer the new declarations type for
1625 built-ins which have not been explicitly declared, for
1626 exception lists, etc... */
1627 else if (DECL_IS_BUILTIN (olddecl))
1628 {
1629 tree type = TREE_TYPE (newdecl);
1630 tree attribs = (*targetm.merge_type_attributes)
1631 (TREE_TYPE (olddecl), type);
1632
1633 type = cp_build_type_attribute_variant (type, attribs);
1634 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1635 }
1636
1637 /* If a function is explicitly declared "throw ()", propagate that to
1638 the corresponding builtin. */
1639 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1640 && DECL_ANTICIPATED (olddecl)
1641 && TREE_NOTHROW (newdecl)
1642 && !TREE_NOTHROW (olddecl))
1643 {
1644 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1645 tree tmpdecl = builtin_decl_explicit (fncode);
1646 if (tmpdecl && tmpdecl != olddecl && types_match)
1647 TREE_NOTHROW (tmpdecl) = 1;
1648 }
1649
1650 /* Whether or not the builtin can throw exceptions has no
1651 bearing on this declarator. */
1652 TREE_NOTHROW (olddecl) = 0;
1653
1654 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1655 {
1656 /* If a builtin function is redeclared as `static', merge
1657 the declarations, but make the original one static. */
1658 DECL_THIS_STATIC (olddecl) = 1;
1659 TREE_PUBLIC (olddecl) = 0;
1660
1661 /* Make the old declaration consistent with the new one so
1662 that all remnants of the builtin-ness of this function
1663 will be banished. */
1664 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1665 COPY_DECL_RTL (newdecl, olddecl);
1666 }
1667 }
1668 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1669 {
1670 /* C++ Standard, 3.3, clause 4:
1671 "[Note: a namespace name or a class template name must be unique
1672 in its declarative region (7.3.2, clause 14). ]" */
1673 if (TREE_CODE (olddecl) == NAMESPACE_DECL
1674 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1675 /* Namespace conflicts with not namespace. */;
1676 else if (DECL_TYPE_TEMPLATE_P (olddecl)
1677 || DECL_TYPE_TEMPLATE_P (newdecl))
1678 /* Class template conflicts. */;
1679 else if ((TREE_CODE (newdecl) == FUNCTION_DECL
1680 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1681 || (TREE_CODE (olddecl) == FUNCTION_DECL
1682 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1683 {
1684 /* One is a function and the other is a template
1685 function. */
1686 if (!UDLIT_OPER_P (DECL_NAME (newdecl)))
1687 return NULL_TREE;
1688
1689 /* There can only be one! */
1690 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1691 && check_raw_literal_operator (olddecl))
1692 error_at (newdecl_loc,
1693 "literal operator %q#D conflicts with"
1694 " raw literal operator", newdecl);
1695 else if (check_raw_literal_operator (newdecl))
1696 error_at (newdecl_loc,
1697 "raw literal operator %q#D conflicts with"
1698 " literal operator template", newdecl);
1699 else
1700 return NULL_TREE;
1701
1702 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1703 return error_mark_node;
1704 }
1705 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1706 || DECL_IMPLICIT_TYPEDEF_P (newdecl))
1707 /* One is an implicit typedef, that's ok. */
1708 return NULL_TREE;
1709
1710 error ("%q#D redeclared as different kind of entity", newdecl);
1711 inform (olddecl_loc, "previous declaration %q#D", olddecl);
1712
1713 return error_mark_node;
1714 }
1715 else if (!types_match)
1716 {
1717 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1718 /* These are certainly not duplicate declarations; they're
1719 from different scopes. */
1720 return NULL_TREE;
1721
1722 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1723 {
1724 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1725 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1726
1727 /* The name of a class template may not be declared to refer to
1728 any other template, class, function, object, namespace, value,
1729 or type in the same scope. */
1730 if (TREE_CODE (oldres) == TYPE_DECL
1731 || TREE_CODE (newres) == TYPE_DECL)
1732 {
1733 error_at (newdecl_loc,
1734 "conflicting declaration of template %q#D", newdecl);
1735 inform (olddecl_loc,
1736 "previous declaration %q#D", olddecl);
1737 return error_mark_node;
1738 }
1739
1740 else if (TREE_CODE (oldres) == FUNCTION_DECL
1741 && TREE_CODE (newres) == FUNCTION_DECL)
1742 {
1743 if (duplicate_function_template_decls (newdecl, olddecl))
1744 return error_mark_node;
1745 return NULL_TREE;
1746 }
1747 else if (check_concept_refinement (olddecl, newdecl))
1748 return error_mark_node;
1749 return NULL_TREE;
1750 }
1751 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1752 {
1753 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1754 {
1755 error_at (newdecl_loc,
1756 "conflicting declaration of C function %q#D",
1757 newdecl);
1758 inform (olddecl_loc,
1759 "previous declaration %q#D", olddecl);
1760 return NULL_TREE;
1761 }
1762 /* For function versions, params and types match, but they
1763 are not ambiguous. */
1764 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1765 && !DECL_FUNCTION_VERSIONED (olddecl))
1766 // The functions have the same parameter types.
1767 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1768 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1769 // And the same constraints.
1770 && equivalently_constrained (newdecl, olddecl))
1771 {
1772 error_at (newdecl_loc,
1773 "ambiguating new declaration of %q#D", newdecl);
1774 inform (olddecl_loc,
1775 "old declaration %q#D", olddecl);
1776 return error_mark_node;
1777 }
1778 else
1779 return NULL_TREE;
1780 }
1781 else
1782 {
1783 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl);
1784 inform (olddecl_loc,
1785 "previous declaration as %q#D", olddecl);
1786 return error_mark_node;
1787 }
1788 }
1789 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1790 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1791 && (!DECL_TEMPLATE_INFO (newdecl)
1792 || (DECL_TI_TEMPLATE (newdecl)
1793 != DECL_TI_TEMPLATE (olddecl))))
1794 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1795 && (!DECL_TEMPLATE_INFO (olddecl)
1796 || (DECL_TI_TEMPLATE (olddecl)
1797 != DECL_TI_TEMPLATE (newdecl))))))
1798 /* It's OK to have a template specialization and a non-template
1799 with the same type, or to have specializations of two
1800 different templates with the same type. Note that if one is a
1801 specialization, and the other is an instantiation of the same
1802 template, that we do not exit at this point. That situation
1803 can occur if we instantiate a template class, and then
1804 specialize one of its methods. This situation is valid, but
1805 the declarations must be merged in the usual way. */
1806 return NULL_TREE;
1807 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1808 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1809 && !DECL_USE_TEMPLATE (newdecl))
1810 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1811 && !DECL_USE_TEMPLATE (olddecl))))
1812 /* One of the declarations is a template instantiation, and the
1813 other is not a template at all. That's OK. */
1814 return NULL_TREE;
1815 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1816 {
1817 /* In [namespace.alias] we have:
1818
1819 In a declarative region, a namespace-alias-definition can be
1820 used to redefine a namespace-alias declared in that declarative
1821 region to refer only to the namespace to which it already
1822 refers.
1823
1824 Therefore, if we encounter a second alias directive for the same
1825 alias, we can just ignore the second directive. */
1826 if (DECL_NAMESPACE_ALIAS (newdecl)
1827 && (DECL_NAMESPACE_ALIAS (newdecl)
1828 == DECL_NAMESPACE_ALIAS (olddecl)))
1829 return olddecl;
1830
1831 /* Leave it to update_binding to merge or report error. */
1832 return NULL_TREE;
1833 }
1834 else
1835 {
1836 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1837 if (errmsg)
1838 {
1839 auto_diagnostic_group d;
1840 error_at (newdecl_loc, errmsg, newdecl);
1841 if (DECL_NAME (olddecl) != NULL_TREE)
1842 inform (olddecl_loc,
1843 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1844 ? G_("%q#D previously defined here")
1845 : G_("%q#D previously declared here"), olddecl);
1846 return error_mark_node;
1847 }
1848 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1849 && DECL_INITIAL (olddecl) != NULL_TREE
1850 && !prototype_p (TREE_TYPE (olddecl))
1851 && prototype_p (TREE_TYPE (newdecl)))
1852 {
1853 /* Prototype decl follows defn w/o prototype. */
1854 auto_diagnostic_group d;
1855 if (warning_at (newdecl_loc, 0,
1856 "prototype specified for %q#D", newdecl))
1857 inform (olddecl_loc,
1858 "previous non-prototype definition here");
1859 }
1860 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1861 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1862 {
1863 /* [dcl.link]
1864 If two declarations of the same function or object
1865 specify different linkage-specifications ..., the program
1866 is ill-formed.... Except for functions with C++ linkage,
1867 a function declaration without a linkage specification
1868 shall not precede the first linkage specification for
1869 that function. A function can be declared without a
1870 linkage specification after an explicit linkage
1871 specification has been seen; the linkage explicitly
1872 specified in the earlier declaration is not affected by
1873 such a function declaration.
1874
1875 DR 563 raises the question why the restrictions on
1876 functions should not also apply to objects. Older
1877 versions of G++ silently ignore the linkage-specification
1878 for this example:
1879
1880 namespace N {
1881 extern int i;
1882 extern "C" int i;
1883 }
1884
1885 which is clearly wrong. Therefore, we now treat objects
1886 like functions. */
1887 if (current_lang_depth () == 0)
1888 {
1889 /* There is no explicit linkage-specification, so we use
1890 the linkage from the previous declaration. */
1891 retrofit_lang_decl (newdecl);
1892 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1893 }
1894 else
1895 {
1896 auto_diagnostic_group d;
1897 error_at (newdecl_loc,
1898 "conflicting declaration of %q#D with %qL linkage",
1899 newdecl, DECL_LANGUAGE (newdecl));
1900 inform (olddecl_loc,
1901 "previous declaration with %qL linkage",
1902 DECL_LANGUAGE (olddecl));
1903 }
1904 }
1905
1906 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1907 ;
1908 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1909 {
1910 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1911 if (DECL_FUNCTION_MEMBER_P (olddecl)
1912 && (/* grokfndecl passes member function templates too
1913 as FUNCTION_DECLs. */
1914 DECL_TEMPLATE_INFO (olddecl)
1915 /* C++11 8.3.6/6.
1916 Default arguments for a member function of a class
1917 template shall be specified on the initial declaration
1918 of the member function within the class template. */
1919 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1920 check_redeclaration_no_default_args (newdecl);
1921 else
1922 {
1923 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1924 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1925 int i = 1;
1926
1927 for (; t1 && t1 != void_list_node;
1928 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1929 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1930 {
1931 if (simple_cst_equal (TREE_PURPOSE (t1),
1932 TREE_PURPOSE (t2)) == 1)
1933 {
1934 auto_diagnostic_group d;
1935 if (permerror (newdecl_loc,
1936 "default argument given for parameter "
1937 "%d of %q#D", i, newdecl))
1938 inform (olddecl_loc,
1939 "previous specification in %q#D here",
1940 olddecl);
1941 }
1942 else
1943 {
1944 auto_diagnostic_group d;
1945 error_at (newdecl_loc,
1946 "default argument given for parameter %d "
1947 "of %q#D", i, newdecl);
1948 inform (olddecl_loc,
1949 "previous specification in %q#D here",
1950 olddecl);
1951 }
1952 }
1953
1954 /* C++17 11.3.6/4: "If a friend declaration specifies a default
1955 argument expression, that declaration... shall be the only
1956 declaration of the function or function template in the
1957 translation unit." */
1958 check_no_redeclaration_friend_default_args
1959 (olddecl, newdecl, DECL_HIDDEN_FRIEND_P (olddecl));
1960 }
1961 }
1962 }
1963
1964 /* Do not merge an implicit typedef with an explicit one. In:
1965
1966 class A;
1967 ...
1968 typedef class A A __attribute__ ((foo));
1969
1970 the attribute should apply only to the typedef. */
1971 if (TREE_CODE (olddecl) == TYPE_DECL
1972 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1973 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1974 return NULL_TREE;
1975
1976 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1977 return error_mark_node;
1978
1979 /* We have committed to returning OLDDECL at this point. */
1980
1981 /* If new decl is `static' and an `extern' was seen previously,
1982 warn about it. */
1983 warn_extern_redeclared_static (newdecl, olddecl);
1984
1985 /* True to merge attributes between the declarations, false to
1986 set OLDDECL's attributes to those of NEWDECL (for template
1987 explicit specializations that specify their own attributes
1988 independent of those specified for the primary template). */
1989 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL
1990 || !DECL_TEMPLATE_SPECIALIZATION (newdecl)
1991 || DECL_TEMPLATE_SPECIALIZATION (olddecl));
1992
1993 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1994 {
1995 if (merge_attr)
1996 {
1997 if (diagnose_mismatched_attributes (olddecl, newdecl))
1998 inform (olddecl_loc, DECL_INITIAL (olddecl)
1999 ? G_("previous definition of %qD here")
2000 : G_("previous declaration of %qD here"), olddecl);
2001
2002 /* [dcl.attr.noreturn]: The first declaration of a function shall
2003 specify the noreturn attribute if any declaration of that function
2004 specifies the noreturn attribute. */
2005 tree a;
2006 if (TREE_THIS_VOLATILE (newdecl)
2007 && !TREE_THIS_VOLATILE (olddecl)
2008 /* This applies to [[noreturn]] only, not its GNU variants. */
2009 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)))
2010 && cxx11_attribute_p (a)
2011 && get_attribute_namespace (a) == NULL_TREE)
2012 {
2013 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> "
2014 "but its first declaration was not", newdecl);
2015 inform (olddecl_loc, "previous declaration of %qD", olddecl);
2016 }
2017 }
2018
2019 /* Now that functions must hold information normally held
2020 by field decls, there is extra work to do so that
2021 declaration information does not get destroyed during
2022 definition. */
2023 if (DECL_VINDEX (olddecl))
2024 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2025 if (DECL_CONTEXT (olddecl))
2026 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2027 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2028 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2029 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
2030 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
2031 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
2032 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
2033 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
2034 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
2035 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
2036 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
2037 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
2038 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
2039
2040 /* Optionally warn about more than one declaration for the same
2041 name, but don't warn about a function declaration followed by a
2042 definition. */
2043 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
2044 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2045 /* Don't warn about extern decl followed by definition. */
2046 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
2047 /* Don't warn about friends, let add_friend take care of it. */
2048 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
2049 /* Don't warn about declaration followed by specialization. */
2050 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
2051 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
2052 {
2053 auto_diagnostic_group d;
2054 if (warning_at (newdecl_loc,
2055 OPT_Wredundant_decls,
2056 "redundant redeclaration of %qD in same scope",
2057 newdecl))
2058 inform (olddecl_loc,
2059 "previous declaration of %qD", olddecl);
2060 }
2061
2062 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
2063 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
2064 {
2065 if (DECL_DELETED_FN (newdecl))
2066 {
2067 auto_diagnostic_group d;
2068 error_at (newdecl_loc, "deleted definition of %qD", newdecl);
2069 inform (olddecl_loc,
2070 "previous declaration of %qD", olddecl);
2071 }
2072 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
2073 }
2074 }
2075
2076 /* Deal with C++: must preserve virtual function table size. */
2077 if (TREE_CODE (olddecl) == TYPE_DECL)
2078 {
2079 tree newtype = TREE_TYPE (newdecl);
2080 tree oldtype = TREE_TYPE (olddecl);
2081
2082 if (newtype != error_mark_node && oldtype != error_mark_node
2083 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2084 CLASSTYPE_FRIEND_CLASSES (newtype)
2085 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2086
2087 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
2088 }
2089
2090 /* Copy all the DECL_... slots specified in the new decl except for
2091 any that we copy here from the old type. */
2092 if (merge_attr)
2093 DECL_ATTRIBUTES (newdecl)
2094 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
2095 else
2096 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2097
2098 if (DECL_DECLARES_FUNCTION_P (olddecl))
2099 {
2100 olddecl_friend = DECL_FRIEND_P (olddecl);
2101 olddecl_hidden_friend = DECL_HIDDEN_FRIEND_P (olddecl);
2102 hidden_friend = (DECL_ANTICIPATED (olddecl)
2103 && DECL_HIDDEN_FRIEND_P (olddecl)
2104 && newdecl_is_friend);
2105 if (!hidden_friend)
2106 {
2107 DECL_ANTICIPATED (olddecl) = 0;
2108 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
2109 }
2110 }
2111
2112 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2113 {
2114 tree old_result = DECL_TEMPLATE_RESULT (olddecl);
2115 tree new_result = DECL_TEMPLATE_RESULT (newdecl);
2116 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2117
2118 /* The new decl should not already have gathered any
2119 specializations. */
2120 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2121
2122 DECL_ATTRIBUTES (old_result)
2123 = (*targetm.merge_decl_attributes) (old_result, new_result);
2124
2125 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2126 {
2127 if (DECL_SOURCE_LOCATION (newdecl)
2128 != DECL_SOURCE_LOCATION (olddecl))
2129 {
2130 /* Per C++11 8.3.6/4, default arguments cannot be added in
2131 later declarations of a function template. */
2132 check_redeclaration_no_default_args (newdecl);
2133 /* C++17 11.3.6/4: "If a friend declaration specifies a default
2134 argument expression, that declaration... shall be the only
2135 declaration of the function or function template in the
2136 translation unit." */
2137 check_no_redeclaration_friend_default_args
2138 (old_result, new_result, olddecl_hidden_friend);
2139 }
2140
2141 check_default_args (newdecl);
2142
2143 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2144 && DECL_INITIAL (new_result))
2145 {
2146 if (DECL_INITIAL (old_result))
2147 DECL_UNINLINABLE (old_result) = 1;
2148 else
2149 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2150 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2151 DECL_NOT_REALLY_EXTERN (old_result)
2152 = DECL_NOT_REALLY_EXTERN (new_result);
2153 DECL_INTERFACE_KNOWN (old_result)
2154 = DECL_INTERFACE_KNOWN (new_result);
2155 DECL_DECLARED_INLINE_P (old_result)
2156 = DECL_DECLARED_INLINE_P (new_result);
2157 DECL_DISREGARD_INLINE_LIMITS (old_result)
2158 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2159
2160 }
2161 else
2162 {
2163 DECL_DECLARED_INLINE_P (old_result)
2164 |= DECL_DECLARED_INLINE_P (new_result);
2165 DECL_DISREGARD_INLINE_LIMITS (old_result)
2166 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2167 check_redeclaration_exception_specification (newdecl, olddecl);
2168
2169 merge_attribute_bits (new_result, old_result);
2170 }
2171 }
2172
2173 /* If the new declaration is a definition, update the file and
2174 line information on the declaration, and also make
2175 the old declaration the same definition. */
2176 if (DECL_INITIAL (new_result) != NULL_TREE)
2177 {
2178 DECL_SOURCE_LOCATION (olddecl)
2179 = DECL_SOURCE_LOCATION (old_result)
2180 = DECL_SOURCE_LOCATION (newdecl);
2181 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2182 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2183 {
2184 tree parm;
2185 DECL_ARGUMENTS (old_result)
2186 = DECL_ARGUMENTS (new_result);
2187 for (parm = DECL_ARGUMENTS (old_result); parm;
2188 parm = DECL_CHAIN (parm))
2189 DECL_CONTEXT (parm) = old_result;
2190 }
2191 }
2192
2193 return olddecl;
2194 }
2195
2196 if (types_match)
2197 {
2198 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2199 check_redeclaration_exception_specification (newdecl, olddecl);
2200
2201 /* Automatically handles default parameters. */
2202 tree oldtype = TREE_TYPE (olddecl);
2203 tree newtype;
2204
2205 /* For typedefs use the old type, as the new type's DECL_NAME points
2206 at newdecl, which will be ggc_freed. */
2207 if (TREE_CODE (newdecl) == TYPE_DECL)
2208 {
2209 /* But NEWTYPE might have an attribute, honor that. */
2210 tree tem = TREE_TYPE (newdecl);
2211 newtype = oldtype;
2212
2213 if (TYPE_USER_ALIGN (tem))
2214 {
2215 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2216 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2217 TYPE_USER_ALIGN (newtype) = true;
2218 }
2219
2220 /* And remove the new type from the variants list. */
2221 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2222 {
2223 tree remove = TREE_TYPE (newdecl);
2224 if (TYPE_MAIN_VARIANT (remove) == remove)
2225 {
2226 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2227 /* If remove is the main variant, no need to remove that
2228 from the list. One of the DECL_ORIGINAL_TYPE
2229 variants, e.g. created for aligned attribute, might still
2230 refer to the newdecl TYPE_DECL though, so remove that one
2231 in that case. */
2232 if (tree orig = DECL_ORIGINAL_TYPE (newdecl))
2233 if (orig != remove)
2234 for (tree t = TYPE_MAIN_VARIANT (orig); t;
2235 t = TYPE_MAIN_VARIANT (t))
2236 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2237 {
2238 TYPE_NEXT_VARIANT (t)
2239 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2240 break;
2241 }
2242 }
2243 else
2244 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2245 t = TYPE_NEXT_VARIANT (t))
2246 if (TYPE_NEXT_VARIANT (t) == remove)
2247 {
2248 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2249 break;
2250 }
2251 }
2252 }
2253 else if (merge_attr)
2254 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2255 else
2256 newtype = TREE_TYPE (newdecl);
2257
2258 if (VAR_P (newdecl))
2259 {
2260 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2261 /* For already initialized vars, TREE_READONLY could have been
2262 cleared in cp_finish_decl, because the var needs runtime
2263 initialization or destruction. Make sure not to set
2264 TREE_READONLY on it again. */
2265 if (DECL_INITIALIZED_P (olddecl)
2266 && !DECL_EXTERNAL (olddecl)
2267 && !TREE_READONLY (olddecl))
2268 TREE_READONLY (newdecl) = 0;
2269 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2270 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2271 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2272 if (DECL_DEPENDENT_INIT_P (olddecl))
2273 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2274 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2275 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2276 DECL_DECLARED_CONSTEXPR_P (newdecl)
2277 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2278
2279 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2280 if (DECL_LANG_SPECIFIC (olddecl)
2281 && CP_DECL_THREADPRIVATE_P (olddecl))
2282 {
2283 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2284 retrofit_lang_decl (newdecl);
2285 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2286 }
2287 }
2288
2289 /* An explicit specialization of a function template or of a member
2290 function of a class template can be declared transaction_safe
2291 independently of whether the corresponding template entity is declared
2292 transaction_safe. */
2293 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2294 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2295 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2296 && tx_safe_fn_type_p (newtype)
2297 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2298 newtype = tx_unsafe_fn_variant (newtype);
2299
2300 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2301
2302 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2303 check_default_args (newdecl);
2304
2305 /* Lay the type out, unless already done. */
2306 if (! same_type_p (newtype, oldtype)
2307 && TREE_TYPE (newdecl) != error_mark_node
2308 && !(processing_template_decl && uses_template_parms (newdecl)))
2309 layout_type (TREE_TYPE (newdecl));
2310
2311 if ((VAR_P (newdecl)
2312 || TREE_CODE (newdecl) == PARM_DECL
2313 || TREE_CODE (newdecl) == RESULT_DECL
2314 || TREE_CODE (newdecl) == FIELD_DECL
2315 || TREE_CODE (newdecl) == TYPE_DECL)
2316 && !(processing_template_decl && uses_template_parms (newdecl)))
2317 layout_decl (newdecl, 0);
2318
2319 /* Merge deprecatedness. */
2320 if (TREE_DEPRECATED (newdecl))
2321 TREE_DEPRECATED (olddecl) = 1;
2322
2323 /* Preserve function specific target and optimization options */
2324 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2325 {
2326 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2327 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2328 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2329 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2330
2331 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2332 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2333 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2334 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2335 }
2336 else
2337 {
2338 /* Merge the const type qualifier. */
2339 if (TREE_READONLY (newdecl))
2340 TREE_READONLY (olddecl) = 1;
2341 /* Merge the volatile type qualifier. */
2342 if (TREE_THIS_VOLATILE (newdecl))
2343 TREE_THIS_VOLATILE (olddecl) = 1;
2344 }
2345
2346 /* Merge the initialization information. */
2347 if (DECL_INITIAL (newdecl) == NULL_TREE
2348 && DECL_INITIAL (olddecl) != NULL_TREE)
2349 {
2350 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2351 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2352 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2353 {
2354 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2355 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2356 }
2357 }
2358
2359 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2360 {
2361 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2362 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2363 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2364 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2365 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2366 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2367 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2368
2369 if (merge_attr)
2370 merge_attribute_bits (newdecl, olddecl);
2371 else
2372 {
2373 /* Merge the noreturn bit. */
2374 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2375 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2376 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2377 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl);
2378 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl);
2379 }
2380 /* Keep the old RTL. */
2381 COPY_DECL_RTL (olddecl, newdecl);
2382 }
2383 else if (VAR_P (newdecl)
2384 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2385 {
2386 /* Keep the old RTL. We cannot keep the old RTL if the old
2387 declaration was for an incomplete object and the new
2388 declaration is not since many attributes of the RTL will
2389 change. */
2390 COPY_DECL_RTL (olddecl, newdecl);
2391 }
2392 }
2393 /* If cannot merge, then use the new type and qualifiers,
2394 and don't preserve the old rtl. */
2395 else
2396 {
2397 /* Clean out any memory we had of the old declaration. */
2398 tree oldstatic = value_member (olddecl, static_aggregates);
2399 if (oldstatic)
2400 TREE_VALUE (oldstatic) = error_mark_node;
2401
2402 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2403 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2404 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2405 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2406 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2407 }
2408
2409 /* Merge the storage class information. */
2410 merge_weak (newdecl, olddecl);
2411
2412 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2413 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2414 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2415 if (! DECL_EXTERNAL (olddecl))
2416 DECL_EXTERNAL (newdecl) = 0;
2417 if (! DECL_COMDAT (olddecl))
2418 DECL_COMDAT (newdecl) = 0;
2419
2420 new_template_info = NULL_TREE;
2421 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2422 {
2423 bool new_redefines_gnu_inline = false;
2424
2425 if (new_defines_function
2426 && ((DECL_INTERFACE_KNOWN (olddecl)
2427 && TREE_CODE (olddecl) == FUNCTION_DECL)
2428 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2429 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2430 == FUNCTION_DECL))))
2431 {
2432 tree fn = olddecl;
2433
2434 if (TREE_CODE (fn) == TEMPLATE_DECL)
2435 fn = DECL_TEMPLATE_RESULT (olddecl);
2436
2437 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2438 }
2439
2440 if (!new_redefines_gnu_inline)
2441 {
2442 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2443 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2444 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2445 }
2446 DECL_TEMPLATE_INSTANTIATED (newdecl)
2447 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2448 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2449
2450 /* If the OLDDECL is an instantiation and/or specialization,
2451 then the NEWDECL must be too. But, it may not yet be marked
2452 as such if the caller has created NEWDECL, but has not yet
2453 figured out that it is a redeclaration. */
2454 if (!DECL_USE_TEMPLATE (newdecl))
2455 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2456
2457 /* Don't really know how much of the language-specific
2458 values we should copy from old to new. */
2459 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2460 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2461 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2462
2463 if (LANG_DECL_HAS_MIN (newdecl))
2464 {
2465 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2466 if (DECL_TEMPLATE_INFO (newdecl))
2467 {
2468 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2469 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2470 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2471 /* Remember the presence of explicit specialization args. */
2472 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2473 = TINFO_USED_TEMPLATE_ID (new_template_info);
2474 }
2475 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2476 }
2477
2478 if (DECL_DECLARES_FUNCTION_P (newdecl))
2479 {
2480 /* Only functions have these fields. */
2481 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2482 DECL_BEFRIENDING_CLASSES (newdecl)
2483 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2484 DECL_BEFRIENDING_CLASSES (olddecl));
2485 /* DECL_THUNKS is only valid for virtual functions,
2486 otherwise it is a DECL_FRIEND_CONTEXT. */
2487 if (DECL_VIRTUAL_P (newdecl))
2488 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2489 }
2490 else if (VAR_P (newdecl))
2491 {
2492 /* Only variables have this field. */
2493 if (VAR_HAD_UNKNOWN_BOUND (olddecl))
2494 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2495 }
2496 }
2497
2498 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2499 {
2500 tree parm;
2501
2502 /* Merge parameter attributes. */
2503 tree oldarg, newarg;
2504 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl);
2505 oldarg && newarg;
2506 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg))
2507 {
2508 DECL_ATTRIBUTES (newarg)
2509 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2510 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2511 }
2512
2513 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2514 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2515 {
2516 /* If newdecl is not a specialization, then it is not a
2517 template-related function at all. And that means that we
2518 should have exited above, returning 0. */
2519 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2520
2521 if (DECL_ODR_USED (olddecl))
2522 /* From [temp.expl.spec]:
2523
2524 If a template, a member template or the member of a class
2525 template is explicitly specialized then that
2526 specialization shall be declared before the first use of
2527 that specialization that would cause an implicit
2528 instantiation to take place, in every translation unit in
2529 which such a use occurs. */
2530 error ("explicit specialization of %qD after first use",
2531 olddecl);
2532
2533 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2534 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2535 && DECL_DECLARED_INLINE_P (newdecl));
2536
2537 /* Don't propagate visibility from the template to the
2538 specialization here. We'll do that in determine_visibility if
2539 appropriate. */
2540 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2541
2542 /* [temp.expl.spec/14] We don't inline explicit specialization
2543 just because the primary template says so. */
2544 gcc_assert (!merge_attr);
2545
2546 DECL_DECLARED_INLINE_P (olddecl)
2547 = DECL_DECLARED_INLINE_P (newdecl);
2548
2549 DECL_DISREGARD_INLINE_LIMITS (olddecl)
2550 = DECL_DISREGARD_INLINE_LIMITS (newdecl);
2551
2552 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl);
2553 }
2554 else if (new_defines_function && DECL_INITIAL (olddecl))
2555 {
2556 /* Never inline re-defined extern inline functions.
2557 FIXME: this could be better handled by keeping both
2558 function as separate declarations. */
2559 DECL_UNINLINABLE (newdecl) = 1;
2560 }
2561 else
2562 {
2563 if (DECL_PENDING_INLINE_P (olddecl))
2564 {
2565 DECL_PENDING_INLINE_P (newdecl) = 1;
2566 DECL_PENDING_INLINE_INFO (newdecl)
2567 = DECL_PENDING_INLINE_INFO (olddecl);
2568 }
2569 else if (DECL_PENDING_INLINE_P (newdecl))
2570 ;
2571 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL)
2572 DECL_SAVED_AUTO_RETURN_TYPE (newdecl)
2573 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl);
2574
2575 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2576
2577 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2578 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2579
2580 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2581 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2582 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2583 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2584 }
2585
2586 /* Preserve abstractness on cloned [cd]tors. */
2587 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2588
2589 /* Update newdecl's parms to point at olddecl. */
2590 for (parm = DECL_ARGUMENTS (newdecl); parm;
2591 parm = DECL_CHAIN (parm))
2592 DECL_CONTEXT (parm) = olddecl;
2593
2594 if (! types_match)
2595 {
2596 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2597 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2598 COPY_DECL_RTL (newdecl, olddecl);
2599 }
2600 if (! types_match || new_defines_function)
2601 {
2602 /* These need to be copied so that the names are available.
2603 Note that if the types do match, we'll preserve inline
2604 info and other bits, but if not, we won't. */
2605 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2606 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2607 }
2608 /* If redeclaring a builtin function, it stays built in
2609 if newdecl is a gnu_inline definition, or if newdecl is just
2610 a declaration. */
2611 if (fndecl_built_in_p (olddecl)
2612 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2613 {
2614 copy_decl_built_in_function (newdecl, olddecl);
2615 /* If we're keeping the built-in definition, keep the rtl,
2616 regardless of declaration matches. */
2617 COPY_DECL_RTL (olddecl, newdecl);
2618 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2619 {
2620 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2621 switch (fncode)
2622 {
2623 /* If a compatible prototype of these builtin functions
2624 is seen, assume the runtime implements it with the
2625 expected semantics. */
2626 case BUILT_IN_STPCPY:
2627 if (builtin_decl_explicit_p (fncode))
2628 set_builtin_decl_implicit_p (fncode, true);
2629 break;
2630 default:
2631 if (builtin_decl_explicit_p (fncode))
2632 set_builtin_decl_declared_p (fncode, true);
2633 break;
2634 }
2635
2636 copy_attributes_to_builtin (newdecl);
2637 }
2638 }
2639 if (new_defines_function)
2640 /* If defining a function declared with other language
2641 linkage, use the previously declared language linkage. */
2642 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2643 else if (types_match)
2644 {
2645 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2646 /* Don't clear out the arguments if we're just redeclaring a
2647 function. */
2648 if (DECL_ARGUMENTS (olddecl))
2649 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2650 }
2651 }
2652 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2653 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2654
2655 /* Now preserve various other info from the definition. */
2656 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2657 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2658 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2659 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2660
2661 /* Warn about conflicting visibility specifications. */
2662 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2663 && DECL_VISIBILITY_SPECIFIED (newdecl)
2664 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2665 {
2666 auto_diagnostic_group d;
2667 if (warning_at (newdecl_loc, OPT_Wattributes,
2668 "%qD: visibility attribute ignored because it "
2669 "conflicts with previous declaration", newdecl))
2670 inform (olddecl_loc,
2671 "previous declaration of %qD", olddecl);
2672 }
2673 /* Choose the declaration which specified visibility. */
2674 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2675 {
2676 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2677 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2678 }
2679 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2680 so keep this behavior. */
2681 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2682 {
2683 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2684 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2685 }
2686 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2687 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2688 {
2689 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2690 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2691 }
2692 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2693 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2694 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2695 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2696 DECL_WARN_IF_NOT_ALIGN (olddecl));
2697 if (TREE_CODE (newdecl) == FIELD_DECL)
2698 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2699
2700 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2701 with that from NEWDECL below. */
2702 if (DECL_LANG_SPECIFIC (olddecl))
2703 {
2704 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2705 != DECL_LANG_SPECIFIC (newdecl));
2706 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2707 }
2708
2709 /* Merge the USED information. */
2710 if (TREE_USED (olddecl))
2711 TREE_USED (newdecl) = 1;
2712 else if (TREE_USED (newdecl))
2713 TREE_USED (olddecl) = 1;
2714 if (VAR_P (newdecl))
2715 {
2716 if (DECL_READ_P (olddecl))
2717 DECL_READ_P (newdecl) = 1;
2718 else if (DECL_READ_P (newdecl))
2719 DECL_READ_P (olddecl) = 1;
2720 }
2721 if (DECL_PRESERVE_P (olddecl))
2722 DECL_PRESERVE_P (newdecl) = 1;
2723 else if (DECL_PRESERVE_P (newdecl))
2724 DECL_PRESERVE_P (olddecl) = 1;
2725
2726 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2727 to olddecl and deleted. */
2728 if (TREE_CODE (newdecl) == FUNCTION_DECL
2729 && DECL_FUNCTION_VERSIONED (olddecl))
2730 {
2731 /* Set the flag for newdecl so that it gets copied to olddecl. */
2732 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2733 /* newdecl will be purged after copying to olddecl and is no longer
2734 a version. */
2735 cgraph_node::delete_function_version_by_decl (newdecl);
2736 }
2737
2738 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2739 {
2740 int function_size;
2741 struct symtab_node *snode = symtab_node::get (olddecl);
2742
2743 function_size = sizeof (struct tree_decl_common);
2744
2745 memcpy ((char *) olddecl + sizeof (struct tree_common),
2746 (char *) newdecl + sizeof (struct tree_common),
2747 function_size - sizeof (struct tree_common));
2748
2749 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2750 (char *) newdecl + sizeof (struct tree_decl_common),
2751 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2752
2753 /* Preserve symtab node mapping. */
2754 olddecl->decl_with_vis.symtab_node = snode;
2755
2756 if (new_template_info)
2757 /* If newdecl is a template instantiation, it is possible that
2758 the following sequence of events has occurred:
2759
2760 o A friend function was declared in a class template. The
2761 class template was instantiated.
2762
2763 o The instantiation of the friend declaration was
2764 recorded on the instantiation list, and is newdecl.
2765
2766 o Later, however, instantiate_class_template called pushdecl
2767 on the newdecl to perform name injection. But, pushdecl in
2768 turn called duplicate_decls when it discovered that another
2769 declaration of a global function with the same name already
2770 existed.
2771
2772 o Here, in duplicate_decls, we decided to clobber newdecl.
2773
2774 If we're going to do that, we'd better make sure that
2775 olddecl, and not newdecl, is on the list of
2776 instantiations so that if we try to do the instantiation
2777 again we won't get the clobbered declaration. */
2778 reregister_specialization (newdecl,
2779 new_template_info,
2780 olddecl);
2781 }
2782 else
2783 {
2784 size_t size = tree_code_size (TREE_CODE (newdecl));
2785
2786 memcpy ((char *) olddecl + sizeof (struct tree_common),
2787 (char *) newdecl + sizeof (struct tree_common),
2788 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2789 switch (TREE_CODE (newdecl))
2790 {
2791 case LABEL_DECL:
2792 case VAR_DECL:
2793 case RESULT_DECL:
2794 case PARM_DECL:
2795 case FIELD_DECL:
2796 case TYPE_DECL:
2797 case CONST_DECL:
2798 {
2799 struct symtab_node *snode = NULL;
2800
2801 if (VAR_P (newdecl)
2802 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2803 || DECL_EXTERNAL (olddecl)))
2804 snode = symtab_node::get (olddecl);
2805 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2806 (char *) newdecl + sizeof (struct tree_decl_common),
2807 size - sizeof (struct tree_decl_common)
2808 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2809 if (VAR_P (newdecl))
2810 olddecl->decl_with_vis.symtab_node = snode;
2811 }
2812 break;
2813 default:
2814 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2815 (char *) newdecl + sizeof (struct tree_decl_common),
2816 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2817 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2818 break;
2819 }
2820 }
2821
2822 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2823 {
2824 if (DECL_EXTERNAL (olddecl)
2825 || TREE_PUBLIC (olddecl)
2826 || TREE_STATIC (olddecl))
2827 {
2828 /* Merge the section attribute.
2829 We want to issue an error if the sections conflict but that must be
2830 done later in decl_attributes since we are called before attributes
2831 are assigned. */
2832 if (DECL_SECTION_NAME (newdecl) != NULL)
2833 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2834
2835 if (DECL_ONE_ONLY (newdecl))
2836 {
2837 struct symtab_node *oldsym, *newsym;
2838 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2839 oldsym = cgraph_node::get_create (olddecl);
2840 else
2841 oldsym = varpool_node::get_create (olddecl);
2842 newsym = symtab_node::get (newdecl);
2843 oldsym->set_comdat_group (newsym->get_comdat_group ());
2844 }
2845 }
2846
2847 if (VAR_P (newdecl)
2848 && CP_DECL_THREAD_LOCAL_P (newdecl))
2849 {
2850 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2851 if (!processing_template_decl)
2852 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2853 }
2854 }
2855
2856 DECL_UID (olddecl) = olddecl_uid;
2857 if (olddecl_friend)
2858 DECL_FRIEND_P (olddecl) = 1;
2859 if (hidden_friend)
2860 {
2861 DECL_ANTICIPATED (olddecl) = 1;
2862 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2863 }
2864
2865 /* NEWDECL contains the merged attribute lists.
2866 Update OLDDECL to be the same. */
2867 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2868
2869 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2870 so that encode_section_info has a chance to look at the new decl
2871 flags and attributes. */
2872 if (DECL_RTL_SET_P (olddecl)
2873 && (TREE_CODE (olddecl) == FUNCTION_DECL
2874 || (VAR_P (olddecl)
2875 && TREE_STATIC (olddecl))))
2876 make_decl_rtl (olddecl);
2877
2878 /* The NEWDECL will no longer be needed. Because every out-of-class
2879 declaration of a member results in a call to duplicate_decls,
2880 freeing these nodes represents in a significant savings.
2881
2882 Before releasing the node, be sore to remove function from symbol
2883 table that might have been inserted there to record comdat group.
2884 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2885 structure is shared in between newdecl and oldecl. */
2886 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2887 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2888 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2889 {
2890 struct symtab_node *snode = symtab_node::get (newdecl);
2891 if (snode)
2892 snode->remove ();
2893 }
2894
2895 /* Remove the associated constraints for newdecl, if any, before
2896 reclaiming memory. */
2897 if (flag_concepts)
2898 remove_constraints (newdecl);
2899
2900 ggc_free (newdecl);
2901
2902 return olddecl;
2903 }
2904 \f
2905 /* Return zero if the declaration NEWDECL is valid
2906 when the declaration OLDDECL (assumed to be for the same name)
2907 has already been seen.
2908 Otherwise return an error message format string with a %s
2909 where the identifier should go. */
2910
2911 static const char *
2912 redeclaration_error_message (tree newdecl, tree olddecl)
2913 {
2914 if (TREE_CODE (newdecl) == TYPE_DECL)
2915 {
2916 /* Because C++ can put things into name space for free,
2917 constructs like "typedef struct foo { ... } foo"
2918 would look like an erroneous redeclaration. */
2919 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2920 return NULL;
2921 else
2922 return G_("redefinition of %q#D");
2923 }
2924 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2925 {
2926 /* If this is a pure function, its olddecl will actually be
2927 the original initialization to `0' (which we force to call
2928 abort()). Don't complain about redefinition in this case. */
2929 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2930 && DECL_INITIAL (olddecl) == NULL_TREE)
2931 return NULL;
2932
2933 /* If both functions come from different namespaces, this is not
2934 a redeclaration - this is a conflict with a used function. */
2935 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2936 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2937 && ! decls_match (olddecl, newdecl))
2938 return G_("%qD conflicts with used function");
2939
2940 /* We'll complain about linkage mismatches in
2941 warn_extern_redeclared_static. */
2942
2943 /* Defining the same name twice is no good. */
2944 if (decl_defined_p (olddecl)
2945 && decl_defined_p (newdecl))
2946 {
2947 if (DECL_NAME (olddecl) == NULL_TREE)
2948 return G_("%q#D not declared in class");
2949 else if (!GNU_INLINE_P (olddecl)
2950 || GNU_INLINE_P (newdecl))
2951 return G_("redefinition of %q#D");
2952 }
2953
2954 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2955 {
2956 bool olda = GNU_INLINE_P (olddecl);
2957 bool newa = GNU_INLINE_P (newdecl);
2958
2959 if (olda != newa)
2960 {
2961 if (newa)
2962 return G_("%q+D redeclared inline with "
2963 "%<gnu_inline%> attribute");
2964 else
2965 return G_("%q+D redeclared inline without "
2966 "%<gnu_inline%> attribute");
2967 }
2968 }
2969
2970 check_abi_tag_redeclaration
2971 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2972 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2973
2974 return NULL;
2975 }
2976 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2977 {
2978 tree nt, ot;
2979
2980 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL)
2981 return G_("redefinition of %q#D");
2982
2983 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL)
2984 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl),
2985 DECL_TEMPLATE_RESULT (olddecl));
2986
2987 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl))
2988 return NULL;
2989
2990 nt = DECL_TEMPLATE_RESULT (newdecl);
2991 if (DECL_TEMPLATE_INFO (nt))
2992 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2993 ot = DECL_TEMPLATE_RESULT (olddecl);
2994 if (DECL_TEMPLATE_INFO (ot))
2995 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2996 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2997 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2998 return G_("redefinition of %q#D");
2999
3000 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
3001 {
3002 bool olda = GNU_INLINE_P (ot);
3003 bool newa = GNU_INLINE_P (nt);
3004
3005 if (olda != newa)
3006 {
3007 if (newa)
3008 return G_("%q+D redeclared inline with "
3009 "%<gnu_inline%> attribute");
3010 else
3011 return G_("%q+D redeclared inline without "
3012 "%<gnu_inline%> attribute");
3013 }
3014 }
3015
3016 /* Core issue #226 (C++0x):
3017
3018 If a friend function template declaration specifies a
3019 default template-argument, that declaration shall be a
3020 definition and shall be the only declaration of the
3021 function template in the translation unit. */
3022 if ((cxx_dialect != cxx98)
3023 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
3024 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
3025 /*is_primary=*/true,
3026 /*is_partial=*/false,
3027 /*is_friend_decl=*/2))
3028 return G_("redeclaration of friend %q#D "
3029 "may not have default template arguments");
3030
3031 return NULL;
3032 }
3033 else if (VAR_P (newdecl)
3034 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
3035 && (! DECL_LANG_SPECIFIC (olddecl)
3036 || ! CP_DECL_THREADPRIVATE_P (olddecl)
3037 || CP_DECL_THREAD_LOCAL_P (newdecl)))
3038 {
3039 /* Only variables can be thread-local, and all declarations must
3040 agree on this property. */
3041 if (CP_DECL_THREAD_LOCAL_P (newdecl))
3042 return G_("thread-local declaration of %q#D follows "
3043 "non-thread-local declaration");
3044 else
3045 return G_("non-thread-local declaration of %q#D follows "
3046 "thread-local declaration");
3047 }
3048 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
3049 {
3050 /* The objects have been declared at namespace scope. If either
3051 is a member of an anonymous union, then this is an invalid
3052 redeclaration. For example:
3053
3054 int i;
3055 union { int i; };
3056
3057 is invalid. */
3058 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
3059 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
3060 return G_("redeclaration of %q#D");
3061 /* If at least one declaration is a reference, there is no
3062 conflict. For example:
3063
3064 int i = 3;
3065 extern int i;
3066
3067 is valid. */
3068 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3069 return NULL;
3070
3071 /* Static data member declared outside a class definition
3072 if the variable is defined within the class with constexpr
3073 specifier is declaration rather than definition (and
3074 deprecated). */
3075 if (cxx_dialect >= cxx17
3076 && VAR_P (olddecl)
3077 && DECL_CLASS_SCOPE_P (olddecl)
3078 && DECL_DECLARED_CONSTEXPR_P (olddecl)
3079 && !DECL_INITIAL (newdecl))
3080 {
3081 DECL_EXTERNAL (newdecl) = 1;
3082 /* For now, only warn with explicit -Wdeprecated. */
3083 if (global_options_set.x_warn_deprecated)
3084 {
3085 auto_diagnostic_group d;
3086 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
3087 "redundant redeclaration of %<constexpr%> "
3088 "static data member %qD", newdecl))
3089 inform (DECL_SOURCE_LOCATION (olddecl),
3090 "previous declaration of %qD", olddecl);
3091 }
3092 return NULL;
3093 }
3094
3095 /* Reject two definitions. */
3096 return G_("redefinition of %q#D");
3097 }
3098 else
3099 {
3100 /* Objects declared with block scope: */
3101 /* Reject two definitions, and reject a definition
3102 together with an external reference. */
3103 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3104 return G_("redeclaration of %q#D");
3105 return NULL;
3106 }
3107 }
3108 \f
3109
3110 /* Hash and equality functions for the named_label table. */
3111
3112 hashval_t
3113 named_label_hash::hash (const value_type entry)
3114 {
3115 return IDENTIFIER_HASH_VALUE (entry->name);
3116 }
3117
3118 bool
3119 named_label_hash::equal (const value_type entry, compare_type name)
3120 {
3121 return name == entry->name;
3122 }
3123
3124 /* Look for a label named ID in the current function. If one cannot
3125 be found, create one. Return the named_label_entry, or NULL on
3126 failure. */
3127
3128 static named_label_entry *
3129 lookup_label_1 (tree id, bool making_local_p)
3130 {
3131 /* You can't use labels at global scope. */
3132 if (current_function_decl == NULL_TREE)
3133 {
3134 error ("label %qE referenced outside of any function", id);
3135 return NULL;
3136 }
3137
3138 if (!named_labels)
3139 named_labels = hash_table<named_label_hash>::create_ggc (13);
3140
3141 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
3142 named_label_entry **slot
3143 = named_labels->find_slot_with_hash (id, hash, INSERT);
3144 named_label_entry *old = *slot;
3145
3146 if (old && old->label_decl)
3147 {
3148 if (!making_local_p)
3149 return old;
3150
3151 if (old->binding_level == current_binding_level)
3152 {
3153 error ("local label %qE conflicts with existing label", id);
3154 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3155 return NULL;
3156 }
3157 }
3158
3159 /* We are making a new decl, create or reuse the named_label_entry */
3160 named_label_entry *ent = NULL;
3161 if (old && !old->label_decl)
3162 ent = old;
3163 else
3164 {
3165 ent = ggc_cleared_alloc<named_label_entry> ();
3166 ent->name = id;
3167 ent->outer = old;
3168 *slot = ent;
3169 }
3170
3171 /* Now create the LABEL_DECL. */
3172 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3173
3174 DECL_CONTEXT (decl) = current_function_decl;
3175 SET_DECL_MODE (decl, VOIDmode);
3176 if (making_local_p)
3177 {
3178 C_DECLARED_LABEL_FLAG (decl) = true;
3179 DECL_CHAIN (decl) = current_binding_level->names;
3180 current_binding_level->names = decl;
3181 }
3182
3183 ent->label_decl = decl;
3184
3185 return ent;
3186 }
3187
3188 /* Wrapper for lookup_label_1. */
3189
3190 tree
3191 lookup_label (tree id)
3192 {
3193 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3194 named_label_entry *ent = lookup_label_1 (id, false);
3195 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3196 return ent ? ent->label_decl : NULL_TREE;
3197 }
3198
3199 tree
3200 declare_local_label (tree id)
3201 {
3202 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3203 named_label_entry *ent = lookup_label_1 (id, true);
3204 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3205 return ent ? ent->label_decl : NULL_TREE;
3206 }
3207
3208 /* Returns nonzero if it is ill-formed to jump past the declaration of
3209 DECL. Returns 2 if it's also a real problem. */
3210
3211 static int
3212 decl_jump_unsafe (tree decl)
3213 {
3214 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3215 with automatic storage duration is not in scope to a point where it is
3216 in scope is ill-formed unless the variable has scalar type, class type
3217 with a trivial default constructor and a trivial destructor, a
3218 cv-qualified version of one of these types, or an array of one of the
3219 preceding types and is declared without an initializer (8.5). */
3220 tree type = TREE_TYPE (decl);
3221
3222 if (!VAR_P (decl) || TREE_STATIC (decl)
3223 || type == error_mark_node)
3224 return 0;
3225
3226 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3227 || variably_modified_type_p (type, NULL_TREE))
3228 return 2;
3229
3230 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3231 return 1;
3232
3233 return 0;
3234 }
3235
3236 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3237 to the user. */
3238
3239 static bool
3240 identify_goto (tree decl, location_t loc, const location_t *locus,
3241 diagnostic_t diag_kind)
3242 {
3243 bool complained
3244 = emit_diagnostic (diag_kind, loc, 0,
3245 decl ? N_("jump to label %qD")
3246 : N_("jump to case label"), decl);
3247 if (complained && locus)
3248 inform (*locus, " from here");
3249 return complained;
3250 }
3251
3252 /* Check that a single previously seen jump to a newly defined label
3253 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3254 the jump context; NAMES are the names in scope in LEVEL at the jump
3255 context; LOCUS is the source position of the jump or 0. Returns
3256 true if all is well. */
3257
3258 static bool
3259 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3260 bool exited_omp, const location_t *locus)
3261 {
3262 cp_binding_level *b;
3263 bool complained = false;
3264 int identified = 0;
3265 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3266
3267 if (exited_omp)
3268 {
3269 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3270 if (complained)
3271 inform (input_location, " exits OpenMP structured block");
3272 saw_omp = true;
3273 identified = 2;
3274 }
3275
3276 for (b = current_binding_level; b ; b = b->level_chain)
3277 {
3278 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3279
3280 for (new_decls = b->names; new_decls != old_decls;
3281 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3282 : TREE_CHAIN (new_decls)))
3283 {
3284 int problem = decl_jump_unsafe (new_decls);
3285 if (! problem)
3286 continue;
3287
3288 if (!identified)
3289 {
3290 complained = identify_goto (decl, input_location, locus,
3291 problem > 1
3292 ? DK_ERROR : DK_PERMERROR);
3293 identified = 1;
3294 }
3295 if (complained)
3296 {
3297 if (problem > 1)
3298 inform (DECL_SOURCE_LOCATION (new_decls),
3299 " crosses initialization of %q#D", new_decls);
3300 else
3301 inform (DECL_SOURCE_LOCATION (new_decls),
3302 " enters scope of %q#D, which has "
3303 "non-trivial destructor", new_decls);
3304 }
3305 }
3306
3307 if (b == level)
3308 break;
3309
3310 const char *inf = NULL;
3311 location_t loc = input_location;
3312 switch (b->kind)
3313 {
3314 case sk_try:
3315 if (!saw_eh)
3316 inf = G_(" enters %<try%> block");
3317 saw_eh = true;
3318 break;
3319
3320 case sk_catch:
3321 if (!saw_eh)
3322 inf = G_(" enters %<catch%> block");
3323 saw_eh = true;
3324 break;
3325
3326 case sk_omp:
3327 if (!saw_omp)
3328 inf = G_(" enters OpenMP structured block");
3329 saw_omp = true;
3330 break;
3331
3332 case sk_transaction:
3333 if (!saw_tm)
3334 inf = G_(" enters synchronized or atomic statement");
3335 saw_tm = true;
3336 break;
3337
3338 case sk_block:
3339 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3340 {
3341 inf = G_(" enters %<constexpr if%> statement");
3342 loc = EXPR_LOCATION (b->level_chain->this_entity);
3343 saw_cxif = true;
3344 }
3345 break;
3346
3347 default:
3348 break;
3349 }
3350
3351 if (inf)
3352 {
3353 if (identified < 2)
3354 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3355 identified = 2;
3356 if (complained)
3357 inform (loc, inf);
3358 }
3359 }
3360
3361 return !identified;
3362 }
3363
3364 static void
3365 check_previous_goto (tree decl, struct named_label_use_entry *use)
3366 {
3367 check_previous_goto_1 (decl, use->binding_level,
3368 use->names_in_scope, use->in_omp_scope,
3369 &use->o_goto_locus);
3370 }
3371
3372 static bool
3373 check_switch_goto (cp_binding_level* level)
3374 {
3375 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3376 }
3377
3378 /* Check that a new jump to a label DECL is OK. Called by
3379 finish_goto_stmt. */
3380
3381 void
3382 check_goto (tree decl)
3383 {
3384 /* We can't know where a computed goto is jumping.
3385 So we assume that it's OK. */
3386 if (TREE_CODE (decl) != LABEL_DECL)
3387 return;
3388
3389 /* We didn't record any information about this label when we created it,
3390 and there's not much point since it's trivial to analyze as a return. */
3391 if (decl == cdtor_label)
3392 return;
3393
3394 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3395 named_label_entry **slot
3396 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3397 named_label_entry *ent = *slot;
3398
3399 /* If the label hasn't been defined yet, defer checking. */
3400 if (! DECL_INITIAL (decl))
3401 {
3402 /* Don't bother creating another use if the last goto had the
3403 same data, and will therefore create the same set of errors. */
3404 if (ent->uses
3405 && ent->uses->names_in_scope == current_binding_level->names)
3406 return;
3407
3408 named_label_use_entry *new_use
3409 = ggc_alloc<named_label_use_entry> ();
3410 new_use->binding_level = current_binding_level;
3411 new_use->names_in_scope = current_binding_level->names;
3412 new_use->o_goto_locus = input_location;
3413 new_use->in_omp_scope = false;
3414
3415 new_use->next = ent->uses;
3416 ent->uses = new_use;
3417 return;
3418 }
3419
3420 bool saw_catch = false, complained = false;
3421 int identified = 0;
3422 tree bad;
3423 unsigned ix;
3424
3425 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3426 || ent->in_constexpr_if
3427 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3428 {
3429 diagnostic_t diag_kind = DK_PERMERROR;
3430 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3431 || ent->in_transaction_scope || ent->in_omp_scope)
3432 diag_kind = DK_ERROR;
3433 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3434 &input_location, diag_kind);
3435 identified = 1 + (diag_kind == DK_ERROR);
3436 }
3437
3438 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3439 {
3440 int u = decl_jump_unsafe (bad);
3441
3442 if (u > 1 && DECL_ARTIFICIAL (bad))
3443 {
3444 /* Can't skip init of __exception_info. */
3445 if (identified == 1)
3446 {
3447 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3448 &input_location, DK_ERROR);
3449 identified = 2;
3450 }
3451 if (complained)
3452 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block");
3453 saw_catch = true;
3454 }
3455 else if (complained)
3456 {
3457 if (u > 1)
3458 inform (DECL_SOURCE_LOCATION (bad),
3459 " skips initialization of %q#D", bad);
3460 else
3461 inform (DECL_SOURCE_LOCATION (bad),
3462 " enters scope of %q#D which has "
3463 "non-trivial destructor", bad);
3464 }
3465 }
3466
3467 if (complained)
3468 {
3469 if (ent->in_try_scope)
3470 inform (input_location, " enters %<try%> block");
3471 else if (ent->in_catch_scope && !saw_catch)
3472 inform (input_location, " enters %<catch%> block");
3473 else if (ent->in_transaction_scope)
3474 inform (input_location, " enters synchronized or atomic statement");
3475 else if (ent->in_constexpr_if)
3476 inform (input_location, " enters %<constexpr if%> statement");
3477 }
3478
3479 if (ent->in_omp_scope)
3480 {
3481 if (complained)
3482 inform (input_location, " enters OpenMP structured block");
3483 }
3484 else if (flag_openmp)
3485 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3486 {
3487 if (b == ent->binding_level)
3488 break;
3489 if (b->kind == sk_omp)
3490 {
3491 if (identified < 2)
3492 {
3493 complained = identify_goto (decl,
3494 DECL_SOURCE_LOCATION (decl),
3495 &input_location, DK_ERROR);
3496 identified = 2;
3497 }
3498 if (complained)
3499 inform (input_location, " exits OpenMP structured block");
3500 break;
3501 }
3502 }
3503 }
3504
3505 /* Check that a return is ok wrt OpenMP structured blocks.
3506 Called by finish_return_stmt. Returns true if all is well. */
3507
3508 bool
3509 check_omp_return (void)
3510 {
3511 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3512 if (b->kind == sk_omp)
3513 {
3514 error ("invalid exit from OpenMP structured block");
3515 return false;
3516 }
3517 else if (b->kind == sk_function_parms)
3518 break;
3519 return true;
3520 }
3521
3522 /* Define a label, specifying the location in the source file.
3523 Return the LABEL_DECL node for the label. */
3524
3525 static tree
3526 define_label_1 (location_t location, tree name)
3527 {
3528 /* After labels, make any new cleanups in the function go into their
3529 own new (temporary) binding contour. */
3530 for (cp_binding_level *p = current_binding_level;
3531 p->kind != sk_function_parms;
3532 p = p->level_chain)
3533 p->more_cleanups_ok = 0;
3534
3535 named_label_entry *ent = lookup_label_1 (name, false);
3536 tree decl = ent->label_decl;
3537
3538 if (DECL_INITIAL (decl) != NULL_TREE)
3539 {
3540 error ("duplicate label %qD", decl);
3541 return error_mark_node;
3542 }
3543 else
3544 {
3545 /* Mark label as having been defined. */
3546 DECL_INITIAL (decl) = error_mark_node;
3547 /* Say where in the source. */
3548 DECL_SOURCE_LOCATION (decl) = location;
3549
3550 ent->binding_level = current_binding_level;
3551 ent->names_in_scope = current_binding_level->names;
3552
3553 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3554 check_previous_goto (decl, use);
3555 ent->uses = NULL;
3556 }
3557
3558 return decl;
3559 }
3560
3561 /* Wrapper for define_label_1. */
3562
3563 tree
3564 define_label (location_t location, tree name)
3565 {
3566 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3567 tree ret = define_label_1 (location, name);
3568 timevar_cond_stop (TV_NAME_LOOKUP, running);
3569 return ret;
3570 }
3571
3572
3573 struct cp_switch
3574 {
3575 cp_binding_level *level;
3576 struct cp_switch *next;
3577 /* The SWITCH_STMT being built. */
3578 tree switch_stmt;
3579 /* A splay-tree mapping the low element of a case range to the high
3580 element, or NULL_TREE if there is no high element. Used to
3581 determine whether or not a new case label duplicates an old case
3582 label. We need a tree, rather than simply a hash table, because
3583 of the GNU case range extension. */
3584 splay_tree cases;
3585 /* Remember whether a default: case label has been seen. */
3586 bool has_default_p;
3587 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3588 bool break_stmt_seen_p;
3589 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3590 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3591 bool in_loop_body_p;
3592 };
3593
3594 /* A stack of the currently active switch statements. The innermost
3595 switch statement is on the top of the stack. There is no need to
3596 mark the stack for garbage collection because it is only active
3597 during the processing of the body of a function, and we never
3598 collect at that point. */
3599
3600 static struct cp_switch *switch_stack;
3601
3602 /* Called right after a switch-statement condition is parsed.
3603 SWITCH_STMT is the switch statement being parsed. */
3604
3605 void
3606 push_switch (tree switch_stmt)
3607 {
3608 struct cp_switch *p = XNEW (struct cp_switch);
3609 p->level = current_binding_level;
3610 p->next = switch_stack;
3611 p->switch_stmt = switch_stmt;
3612 p->cases = splay_tree_new (case_compare, NULL, NULL);
3613 p->has_default_p = false;
3614 p->break_stmt_seen_p = false;
3615 p->in_loop_body_p = false;
3616 switch_stack = p;
3617 }
3618
3619 void
3620 pop_switch (void)
3621 {
3622 struct cp_switch *cs = switch_stack;
3623 location_t switch_location;
3624
3625 /* Emit warnings as needed. */
3626 switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt);
3627 const bool bool_cond_p
3628 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3629 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3630 if (!processing_template_decl)
3631 c_do_switch_warnings (cs->cases, switch_location,
3632 SWITCH_STMT_TYPE (cs->switch_stmt),
3633 SWITCH_STMT_COND (cs->switch_stmt), bool_cond_p);
3634
3635 /* For the benefit of block_may_fallthru remember if the switch body
3636 case labels cover all possible values and if there are break; stmts. */
3637 if (cs->has_default_p
3638 || (!processing_template_decl
3639 && c_switch_covers_all_cases_p (cs->cases,
3640 SWITCH_STMT_TYPE (cs->switch_stmt))))
3641 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3642 if (!cs->break_stmt_seen_p)
3643 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3644 gcc_assert (!cs->in_loop_body_p);
3645 splay_tree_delete (cs->cases);
3646 switch_stack = switch_stack->next;
3647 free (cs);
3648 }
3649
3650 /* Note that a BREAK_STMT is about to be added. If it is inside of
3651 a SWITCH_STMT and not inside of a loop body inside of it, note
3652 in switch_stack we've seen a BREAK_STMT. */
3653
3654 void
3655 note_break_stmt (void)
3656 {
3657 if (switch_stack && !switch_stack->in_loop_body_p)
3658 switch_stack->break_stmt_seen_p = true;
3659 }
3660
3661 /* Note the start of processing of an iteration statement's body.
3662 The note_break_stmt function will do nothing while processing it.
3663 Return a flag that should be passed to note_iteration_stmt_body_end. */
3664
3665 bool
3666 note_iteration_stmt_body_start (void)
3667 {
3668 if (!switch_stack)
3669 return false;
3670 bool ret = switch_stack->in_loop_body_p;
3671 switch_stack->in_loop_body_p = true;
3672 return ret;
3673 }
3674
3675 /* Note the end of processing of an iteration statement's body. */
3676
3677 void
3678 note_iteration_stmt_body_end (bool prev)
3679 {
3680 if (switch_stack)
3681 switch_stack->in_loop_body_p = prev;
3682 }
3683
3684 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3685 condition. Note that if TYPE and VALUE are already integral we don't
3686 really do the conversion because the language-independent
3687 warning/optimization code will work better that way. */
3688
3689 static tree
3690 case_conversion (tree type, tree value)
3691 {
3692 if (value == NULL_TREE)
3693 return value;
3694
3695 value = mark_rvalue_use (value);
3696
3697 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3698 type = type_promotes_to (type);
3699
3700 tree ovalue = value;
3701 /* The constant-expression VALUE shall be a converted constant expression
3702 of the adjusted type of the switch condition, which doesn't allow
3703 narrowing conversions. */
3704 value = build_converted_constant_expr (type, value, tf_warning_or_error);
3705
3706 if (cxx_dialect >= cxx11
3707 && (SCOPED_ENUM_P (type)
3708 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue))))
3709 /* Use the converted value. */;
3710 else
3711 /* The already integral case. */
3712 value = ovalue;
3713
3714 return cxx_constant_value (value);
3715 }
3716
3717 /* Note that we've seen a definition of a case label, and complain if this
3718 is a bad place for one. */
3719
3720 tree
3721 finish_case_label (location_t loc, tree low_value, tree high_value)
3722 {
3723 tree cond, r;
3724 cp_binding_level *p;
3725 tree type;
3726
3727 if (low_value == NULL_TREE && high_value == NULL_TREE)
3728 switch_stack->has_default_p = true;
3729
3730 if (processing_template_decl)
3731 {
3732 tree label;
3733
3734 /* For templates, just add the case label; we'll do semantic
3735 analysis at instantiation-time. */
3736 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node);
3737 return add_stmt (build_case_label (low_value, high_value, label));
3738 }
3739
3740 /* Find the condition on which this switch statement depends. */
3741 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3742 if (cond && TREE_CODE (cond) == TREE_LIST)
3743 cond = TREE_VALUE (cond);
3744
3745 if (!check_switch_goto (switch_stack->level))
3746 return error_mark_node;
3747
3748 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3749 if (type == error_mark_node)
3750 return error_mark_node;
3751
3752 low_value = case_conversion (type, low_value);
3753 high_value = case_conversion (type, high_value);
3754
3755 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value);
3756
3757 /* After labels, make any new cleanups in the function go into their
3758 own new (temporary) binding contour. */
3759 for (p = current_binding_level;
3760 p->kind != sk_function_parms;
3761 p = p->level_chain)
3762 p->more_cleanups_ok = 0;
3763
3764 return r;
3765 }
3766 \f
3767 struct typename_info {
3768 tree scope;
3769 tree name;
3770 tree template_id;
3771 bool enum_p;
3772 bool class_p;
3773 };
3774
3775 struct typename_hasher : ggc_ptr_hash<tree_node>
3776 {
3777 typedef typename_info *compare_type;
3778
3779 /* Hash a TYPENAME_TYPE. */
3780
3781 static hashval_t
3782 hash (tree t)
3783 {
3784 hashval_t hash;
3785
3786 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3787 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3788
3789 return hash;
3790 }
3791
3792 /* Compare two TYPENAME_TYPEs. */
3793
3794 static bool
3795 equal (tree t1, const typename_info *t2)
3796 {
3797 return (TYPE_IDENTIFIER (t1) == t2->name
3798 && TYPE_CONTEXT (t1) == t2->scope
3799 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3800 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3801 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3802 }
3803 };
3804
3805 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3806 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3807
3808 Returns the new TYPENAME_TYPE. */
3809
3810 static GTY (()) hash_table<typename_hasher> *typename_htab;
3811
3812 tree
3813 build_typename_type (tree context, tree name, tree fullname,
3814 enum tag_types tag_type)
3815 {
3816 tree t;
3817 tree d;
3818 typename_info ti;
3819 tree *e;
3820 hashval_t hash;
3821
3822 if (typename_htab == NULL)
3823 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3824
3825 ti.scope = FROB_CONTEXT (context);
3826 ti.name = name;
3827 ti.template_id = fullname;
3828 ti.enum_p = tag_type == enum_type;
3829 ti.class_p = (tag_type == class_type
3830 || tag_type == record_type
3831 || tag_type == union_type);
3832 hash = (htab_hash_pointer (ti.scope)
3833 ^ htab_hash_pointer (ti.name));
3834
3835 /* See if we already have this type. */
3836 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3837 if (*e)
3838 t = *e;
3839 else
3840 {
3841 /* Build the TYPENAME_TYPE. */
3842 t = cxx_make_type (TYPENAME_TYPE);
3843 TYPE_CONTEXT (t) = ti.scope;
3844 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3845 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3846 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3847
3848 /* Build the corresponding TYPE_DECL. */
3849 d = build_decl (input_location, TYPE_DECL, name, t);
3850 TYPE_NAME (TREE_TYPE (d)) = d;
3851 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3852 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3853 DECL_ARTIFICIAL (d) = 1;
3854
3855 /* Store it in the hash table. */
3856 *e = t;
3857
3858 /* TYPENAME_TYPEs must always be compared structurally, because
3859 they may or may not resolve down to another type depending on
3860 the currently open classes. */
3861 SET_TYPE_STRUCTURAL_EQUALITY (t);
3862 }
3863
3864 return t;
3865 }
3866
3867 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3868 provided to name the type. Returns an appropriate type, unless an
3869 error occurs, in which case error_mark_node is returned. If we
3870 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3871 return that, rather than the _TYPE it corresponds to, in other
3872 cases we look through the type decl. If TF_ERROR is set, complain
3873 about errors, otherwise be quiet. */
3874
3875 tree
3876 make_typename_type (tree context, tree name, enum tag_types tag_type,
3877 tsubst_flags_t complain)
3878 {
3879 tree fullname;
3880 tree t;
3881 bool want_template;
3882
3883 if (name == error_mark_node
3884 || context == NULL_TREE
3885 || context == error_mark_node)
3886 return error_mark_node;
3887
3888 if (TYPE_P (name))
3889 {
3890 if (!(TYPE_LANG_SPECIFIC (name)
3891 && (CLASSTYPE_IS_TEMPLATE (name)
3892 || CLASSTYPE_USE_TEMPLATE (name))))
3893 name = TYPE_IDENTIFIER (name);
3894 else
3895 /* Create a TEMPLATE_ID_EXPR for the type. */
3896 name = build_nt (TEMPLATE_ID_EXPR,
3897 CLASSTYPE_TI_TEMPLATE (name),
3898 CLASSTYPE_TI_ARGS (name));
3899 }
3900 else if (TREE_CODE (name) == TYPE_DECL)
3901 name = DECL_NAME (name);
3902
3903 fullname = name;
3904
3905 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3906 {
3907 name = TREE_OPERAND (name, 0);
3908 if (DECL_TYPE_TEMPLATE_P (name))
3909 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3910 if (TREE_CODE (name) != IDENTIFIER_NODE)
3911 {
3912 if (complain & tf_error)
3913 error ("%qD is not a type", name);
3914 return error_mark_node;
3915 }
3916 }
3917 if (TREE_CODE (name) == TEMPLATE_DECL)
3918 {
3919 if (complain & tf_error)
3920 error ("%qD used without template arguments", name);
3921 return error_mark_node;
3922 }
3923 gcc_assert (identifier_p (name));
3924 gcc_assert (TYPE_P (context));
3925
3926 if (TREE_CODE (context) == TYPE_PACK_EXPANSION)
3927 /* This can happen for C++17 variadic using (c++/88986). */;
3928 else if (!MAYBE_CLASS_TYPE_P (context))
3929 {
3930 if (complain & tf_error)
3931 error ("%q#T is not a class", context);
3932 return error_mark_node;
3933 }
3934
3935 /* When the CONTEXT is a dependent type, NAME could refer to a
3936 dependent base class of CONTEXT. But look inside it anyway
3937 if CONTEXT is a currently open scope, in case it refers to a
3938 member of the current instantiation or a non-dependent base;
3939 lookup will stop when we hit a dependent base. */
3940 if (!dependent_scope_p (context))
3941 /* We should only set WANT_TYPE when we're a nested typename type.
3942 Then we can give better diagnostics if we find a non-type. */
3943 t = lookup_field (context, name, 2, /*want_type=*/true);
3944 else
3945 t = NULL_TREE;
3946
3947 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3948 return build_typename_type (context, name, fullname, tag_type);
3949
3950 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3951
3952 if (!t)
3953 {
3954 if (complain & tf_error)
3955 {
3956 if (!COMPLETE_TYPE_P (context))
3957 cxx_incomplete_type_error (NULL_TREE, context);
3958 else
3959 error (want_template ? G_("no class template named %q#T in %q#T")
3960 : G_("no type named %q#T in %q#T"), name, context);
3961 }
3962 return error_mark_node;
3963 }
3964
3965 /* Pull out the template from an injected-class-name (or multiple). */
3966 if (want_template)
3967 t = maybe_get_template_decl_from_type_decl (t);
3968
3969 if (TREE_CODE (t) == TREE_LIST)
3970 {
3971 if (complain & tf_error)
3972 {
3973 error ("lookup of %qT in %qT is ambiguous", name, context);
3974 print_candidates (t);
3975 }
3976 return error_mark_node;
3977 }
3978
3979 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3980 {
3981 if (complain & tf_error)
3982 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3983 context, name, t);
3984 return error_mark_node;
3985 }
3986 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3987 {
3988 if (complain & tf_error)
3989 error ("%<typename %T::%D%> names %q#T, which is not a type",
3990 context, name, t);
3991 return error_mark_node;
3992 }
3993
3994 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3995 return error_mark_node;
3996
3997 /* If we are currently parsing a template and if T is a typedef accessed
3998 through CONTEXT then we need to remember and check access of T at
3999 template instantiation time. */
4000 add_typedef_to_current_template_for_access_check (t, context, input_location);
4001
4002 if (want_template)
4003 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
4004 NULL_TREE, context,
4005 /*entering_scope=*/0,
4006 complain | tf_user);
4007
4008 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
4009 t = TREE_TYPE (t);
4010
4011 maybe_record_typedef_use (t);
4012
4013 return t;
4014 }
4015
4016 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
4017 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
4018 in which case error_mark_node is returned.
4019
4020 If PARM_LIST is non-NULL, also make sure that the template parameter
4021 list of TEMPLATE_DECL matches.
4022
4023 If COMPLAIN zero, don't complain about any errors that occur. */
4024
4025 tree
4026 make_unbound_class_template (tree context, tree name, tree parm_list,
4027 tsubst_flags_t complain)
4028 {
4029 tree t;
4030 tree d;
4031
4032 if (TYPE_P (name))
4033 name = TYPE_IDENTIFIER (name);
4034 else if (DECL_P (name))
4035 name = DECL_NAME (name);
4036 gcc_assert (identifier_p (name));
4037
4038 if (!dependent_type_p (context)
4039 || currently_open_class (context))
4040 {
4041 tree tmpl = NULL_TREE;
4042
4043 if (MAYBE_CLASS_TYPE_P (context))
4044 tmpl = lookup_field (context, name, 0, false);
4045
4046 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
4047 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4048
4049 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
4050 {
4051 if (complain & tf_error)
4052 error ("no class template named %q#T in %q#T", name, context);
4053 return error_mark_node;
4054 }
4055
4056 if (parm_list
4057 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
4058 {
4059 if (complain & tf_error)
4060 {
4061 error ("template parameters do not match template %qD", tmpl);
4062 inform (DECL_SOURCE_LOCATION (tmpl),
4063 "%qD declared here", tmpl);
4064 }
4065 return error_mark_node;
4066 }
4067
4068 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
4069 complain))
4070 return error_mark_node;
4071
4072 return tmpl;
4073 }
4074
4075 /* Build the UNBOUND_CLASS_TEMPLATE. */
4076 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
4077 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4078 TREE_TYPE (t) = NULL_TREE;
4079 SET_TYPE_STRUCTURAL_EQUALITY (t);
4080
4081 /* Build the corresponding TEMPLATE_DECL. */
4082 d = build_decl (input_location, TEMPLATE_DECL, name, t);
4083 TYPE_NAME (TREE_TYPE (d)) = d;
4084 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
4085 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4086 DECL_ARTIFICIAL (d) = 1;
4087 DECL_TEMPLATE_PARMS (d) = parm_list;
4088
4089 return t;
4090 }
4091
4092 \f
4093
4094 /* Push the declarations of builtin types into the global namespace.
4095 RID_INDEX is the index of the builtin type in the array
4096 RID_POINTERS. NAME is the name used when looking up the builtin
4097 type. TYPE is the _TYPE node for the builtin type.
4098
4099 The calls to set_global_binding below should be
4100 eliminated. Built-in types should not be looked up name; their
4101 names are keywords that the parser can recognize. However, there
4102 is code in c-common.c that uses identifier_global_value to look up
4103 built-in types by name. */
4104
4105 void
4106 record_builtin_type (enum rid rid_index,
4107 const char* name,
4108 tree type)
4109 {
4110 tree decl = NULL_TREE;
4111
4112 if (name)
4113 {
4114 tree tname = get_identifier (name);
4115 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
4116 DECL_ARTIFICIAL (tdecl) = 1;
4117 set_global_binding (tdecl);
4118 decl = tdecl;
4119 }
4120
4121 if ((int) rid_index < (int) RID_MAX)
4122 if (tree rname = ridpointers[(int) rid_index])
4123 if (!decl || DECL_NAME (decl) != rname)
4124 {
4125 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
4126 DECL_ARTIFICIAL (rdecl) = 1;
4127 set_global_binding (rdecl);
4128 if (!decl)
4129 decl = rdecl;
4130 }
4131
4132 if (decl)
4133 {
4134 if (!TYPE_NAME (type))
4135 TYPE_NAME (type) = decl;
4136 debug_hooks->type_decl (decl, 0);
4137 }
4138 }
4139
4140 /* Push a type into the namespace so that the back ends ignore it. */
4141
4142 static void
4143 record_unknown_type (tree type, const char* name)
4144 {
4145 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
4146 TYPE_DECL, get_identifier (name), type));
4147 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
4148 DECL_IGNORED_P (decl) = 1;
4149 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
4150 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
4151 SET_TYPE_ALIGN (type, 1);
4152 TYPE_USER_ALIGN (type) = 0;
4153 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
4154 }
4155
4156 /* Create all the predefined identifiers. */
4157
4158 static void
4159 initialize_predefined_identifiers (void)
4160 {
4161 struct predefined_identifier
4162 {
4163 const char *name; /* Name. */
4164 tree *node; /* Node to store it in. */
4165 cp_identifier_kind kind; /* Kind of identifier. */
4166 };
4167
4168 /* A table of identifiers to create at startup. */
4169 static const predefined_identifier predefined_identifiers[] = {
4170 {"C++", &lang_name_cplusplus, cik_normal},
4171 {"C", &lang_name_c, cik_normal},
4172 /* Some of these names have a trailing space so that it is
4173 impossible for them to conflict with names written by users. */
4174 {"__ct ", &ctor_identifier, cik_ctor},
4175 {"__ct_base ", &base_ctor_identifier, cik_ctor},
4176 {"__ct_comp ", &complete_ctor_identifier, cik_ctor},
4177 {"__dt ", &dtor_identifier, cik_dtor},
4178 {"__dt_base ", &base_dtor_identifier, cik_dtor},
4179 {"__dt_comp ", &complete_dtor_identifier, cik_dtor},
4180 {"__dt_del ", &deleting_dtor_identifier, cik_dtor},
4181 {"__conv_op ", &conv_op_identifier, cik_conv_op},
4182 {"__in_chrg", &in_charge_identifier, cik_normal},
4183 {"this", &this_identifier, cik_normal},
4184 {"__delta", &delta_identifier, cik_normal},
4185 {"__pfn", &pfn_identifier, cik_normal},
4186 {"_vptr", &vptr_identifier, cik_normal},
4187 {"__vtt_parm", &vtt_parm_identifier, cik_normal},
4188 {"::", &global_identifier, cik_normal},
4189 /* The demangler expects anonymous namespaces to be called
4190 something starting with '_GLOBAL__N_'. It no longer needs
4191 to be unique to the TU. */
4192 {"_GLOBAL__N_1", &anon_identifier, cik_normal},
4193 {"auto", &auto_identifier, cik_normal},
4194 {"decltype(auto)", &decltype_auto_identifier, cik_normal},
4195 {"initializer_list", &init_list_identifier, cik_normal},
4196 {"__for_range ", &for_range__identifier, cik_normal},
4197 {"__for_begin ", &for_begin__identifier, cik_normal},
4198 {"__for_end ", &for_end__identifier, cik_normal},
4199 {"__for_range", &for_range_identifier, cik_normal},
4200 {"__for_begin", &for_begin_identifier, cik_normal},
4201 {"__for_end", &for_end_identifier, cik_normal},
4202 {"abi_tag", &abi_tag_identifier, cik_normal},
4203 {"aligned", &aligned_identifier, cik_normal},
4204 {"begin", &begin_identifier, cik_normal},
4205 {"end", &end_identifier, cik_normal},
4206 {"get", &get__identifier, cik_normal},
4207 {"gnu", &gnu_identifier, cik_normal},
4208 {"tuple_element", &tuple_element_identifier, cik_normal},
4209 {"tuple_size", &tuple_size_identifier, cik_normal},
4210 {"type", &type_identifier, cik_normal},
4211 {"value", &value_identifier, cik_normal},
4212 {"_FUN", &fun_identifier, cik_normal},
4213 {"__closure", &closure_identifier, cik_normal},
4214 {"heap uninit", &heap_uninit_identifier, cik_normal},
4215 {"heap ", &heap_identifier, cik_normal},
4216 {"heap deleted", &heap_deleted_identifier, cik_normal},
4217 {NULL, NULL, cik_normal}
4218 };
4219
4220 for (const predefined_identifier *pid = predefined_identifiers;
4221 pid->name; ++pid)
4222 {
4223 *pid->node = get_identifier (pid->name);
4224 /* Some of these identifiers already have a special kind. */
4225 if (pid->kind != cik_normal)
4226 set_identifier_kind (*pid->node, pid->kind);
4227 }
4228 }
4229
4230 /* Create the predefined scalar types of C,
4231 and some nodes representing standard constants (0, 1, (void *)0).
4232 Initialize the global binding level.
4233 Make definitions for built-in primitive functions. */
4234
4235 void
4236 cxx_init_decl_processing (void)
4237 {
4238 tree void_ftype;
4239 tree void_ftype_ptr;
4240
4241 /* Create all the identifiers we need. */
4242 initialize_predefined_identifiers ();
4243
4244 /* Create the global variables. */
4245 push_to_top_level ();
4246
4247 current_function_decl = NULL_TREE;
4248 current_binding_level = NULL;
4249 /* Enter the global namespace. */
4250 gcc_assert (global_namespace == NULL_TREE);
4251 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier,
4252 void_type_node);
4253 TREE_PUBLIC (global_namespace) = 1;
4254 DECL_CONTEXT (global_namespace)
4255 = build_translation_unit_decl (get_identifier (main_input_filename));
4256 /* Remember whether we want the empty class passing ABI change warning
4257 in this TU. */
4258 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace))
4259 = warn_abi && abi_version_crosses (12);
4260 debug_hooks->register_main_translation_unit
4261 (DECL_CONTEXT (global_namespace));
4262 begin_scope (sk_namespace, global_namespace);
4263 current_namespace = global_namespace;
4264
4265 if (flag_visibility_ms_compat)
4266 default_visibility = VISIBILITY_HIDDEN;
4267
4268 /* Initially, C. */
4269 current_lang_name = lang_name_c;
4270
4271 /* Create the `std' namespace. */
4272 push_namespace (get_identifier ("std"));
4273 std_node = current_namespace;
4274 pop_namespace ();
4275
4276 flag_noexcept_type = (cxx_dialect >= cxx17);
4277
4278 c_common_nodes_and_builtins ();
4279
4280 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE);
4281 tree decl
4282 = add_builtin_function ("__builtin_is_constant_evaluated",
4283 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
4284 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4285 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4286
4287 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE);
4288 decl = add_builtin_function ("__builtin_source_location",
4289 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION,
4290 BUILT_IN_FRONTEND, NULL, NULL_TREE);
4291 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF);
4292
4293 integer_two_node = build_int_cst (NULL_TREE, 2);
4294
4295 /* Guess at the initial static decls size. */
4296 vec_alloc (static_decls, 500);
4297
4298 /* ... and keyed classes. */
4299 vec_alloc (keyed_classes, 100);
4300
4301 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4302 truthvalue_type_node = boolean_type_node;
4303 truthvalue_false_node = boolean_false_node;
4304 truthvalue_true_node = boolean_true_node;
4305
4306 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4307 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4308 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4309 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT),
4310 NULL_TREE);
4311
4312 #if 0
4313 record_builtin_type (RID_MAX, NULL, string_type_node);
4314 #endif
4315
4316 delta_type_node = ptrdiff_type_node;
4317 vtable_index_type = ptrdiff_type_node;
4318
4319 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4320 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4321 void_ftype_ptr = build_function_type_list (void_type_node,
4322 ptr_type_node, NULL_TREE);
4323 void_ftype_ptr
4324 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4325
4326 /* Create the conversion operator marker. This operator's DECL_NAME
4327 is in the identifier table, so we can use identifier equality to
4328 find it. */
4329 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier,
4330 void_ftype);
4331
4332 /* C++ extensions */
4333
4334 unknown_type_node = make_node (LANG_TYPE);
4335 record_unknown_type (unknown_type_node, "unknown type");
4336
4337 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4338 TREE_TYPE (unknown_type_node) = unknown_type_node;
4339
4340 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4341 result. */
4342 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4343 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4344
4345 init_list_type_node = make_node (LANG_TYPE);
4346 record_unknown_type (init_list_type_node, "init list");
4347
4348 {
4349 /* Make sure we get a unique function type, so we can give
4350 its pointer type a name. (This wins for gdb.) */
4351 tree vfunc_type = make_node (FUNCTION_TYPE);
4352 TREE_TYPE (vfunc_type) = integer_type_node;
4353 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4354 layout_type (vfunc_type);
4355
4356 vtable_entry_type = build_pointer_type (vfunc_type);
4357 }
4358 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type);
4359
4360 vtbl_type_node
4361 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4362 layout_type (vtbl_type_node);
4363 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4364 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4365 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4366 layout_type (vtbl_ptr_type_node);
4367 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4368
4369 push_namespace (get_identifier ("__cxxabiv1"));
4370 abi_node = current_namespace;
4371 pop_namespace ();
4372
4373 global_type_node = make_node (LANG_TYPE);
4374 record_unknown_type (global_type_node, "global type");
4375
4376 any_targ_node = make_node (LANG_TYPE);
4377 record_unknown_type (any_targ_node, "any type");
4378
4379 /* Now, C++. */
4380 current_lang_name = lang_name_cplusplus;
4381
4382 if (aligned_new_threshold > 1
4383 && !pow2p_hwi (aligned_new_threshold))
4384 {
4385 error ("%<-faligned-new=%d%> is not a power of two",
4386 aligned_new_threshold);
4387 aligned_new_threshold = 1;
4388 }
4389 if (aligned_new_threshold == -1)
4390 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0;
4391 if (aligned_new_threshold == 1)
4392 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT;
4393
4394 {
4395 tree newattrs, extvisattr;
4396 tree newtype, deltype;
4397 tree ptr_ftype_sizetype;
4398 tree new_eh_spec;
4399
4400 ptr_ftype_sizetype
4401 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4402 if (cxx_dialect == cxx98)
4403 {
4404 tree bad_alloc_id;
4405 tree bad_alloc_type_node;
4406 tree bad_alloc_decl;
4407
4408 push_nested_namespace (std_node);
4409 bad_alloc_id = get_identifier ("bad_alloc");
4410 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4411 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4412 bad_alloc_decl
4413 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4414 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4415 pop_nested_namespace (std_node);
4416
4417 new_eh_spec
4418 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4419 }
4420 else
4421 new_eh_spec = noexcept_false_spec;
4422
4423 /* Ensure attribs.c is initialized. */
4424 init_attributes ();
4425
4426 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4427 NULL_TREE);
4428 newattrs = tree_cons (get_identifier ("alloc_size"),
4429 build_tree_list (NULL_TREE, integer_one_node),
4430 extvisattr);
4431 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4432 newtype = build_exception_variant (newtype, new_eh_spec);
4433 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4434 deltype = build_exception_variant (deltype, empty_except_spec);
4435 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4436 DECL_IS_MALLOC (opnew) = 1;
4437 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4438 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4439 DECL_IS_MALLOC (opnew) = 1;
4440 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4441 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4442 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4443 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4444 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4445 if (flag_sized_deallocation)
4446 {
4447 /* Also push the sized deallocation variants:
4448 void operator delete(void*, std::size_t) throw();
4449 void operator delete[](void*, std::size_t) throw(); */
4450 tree void_ftype_ptr_size
4451 = build_function_type_list (void_type_node, ptr_type_node,
4452 size_type_node, NULL_TREE);
4453 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4454 extvisattr);
4455 deltype = build_exception_variant (deltype, empty_except_spec);
4456 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4457 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4458 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4459 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4460 }
4461
4462 if (aligned_new_threshold)
4463 {
4464 push_nested_namespace (std_node);
4465 tree align_id = get_identifier ("align_val_t");
4466 align_type_node = start_enum (align_id, NULL_TREE, size_type_node,
4467 NULL_TREE, /*scoped*/true, NULL);
4468 pop_nested_namespace (std_node);
4469
4470 /* operator new (size_t, align_val_t); */
4471 newtype = build_function_type_list (ptr_type_node, size_type_node,
4472 align_type_node, NULL_TREE);
4473 newtype = cp_build_type_attribute_variant (newtype, newattrs);
4474 newtype = build_exception_variant (newtype, new_eh_spec);
4475 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4476 DECL_IS_MALLOC (opnew) = 1;
4477 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4478 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4479 DECL_IS_MALLOC (opnew) = 1;
4480 DECL_SET_IS_OPERATOR_NEW (opnew, true);
4481
4482 /* operator delete (void *, align_val_t); */
4483 deltype = build_function_type_list (void_type_node, ptr_type_node,
4484 align_type_node, NULL_TREE);
4485 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4486 deltype = build_exception_variant (deltype, empty_except_spec);
4487 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4488 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4489 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4490 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4491
4492 if (flag_sized_deallocation)
4493 {
4494 /* operator delete (void *, size_t, align_val_t); */
4495 deltype = build_function_type_list (void_type_node, ptr_type_node,
4496 size_type_node, align_type_node,
4497 NULL_TREE);
4498 deltype = cp_build_type_attribute_variant (deltype, extvisattr);
4499 deltype = build_exception_variant (deltype, empty_except_spec);
4500 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4501 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4502 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4503 DECL_SET_IS_OPERATOR_DELETE (opdel, true);
4504 }
4505 }
4506
4507 nullptr_type_node = make_node (NULLPTR_TYPE);
4508 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4509 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4510 TYPE_UNSIGNED (nullptr_type_node) = 1;
4511 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4512 if (abi_version_at_least (9))
4513 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4514 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4515 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4516 nullptr_node = build_int_cst (nullptr_type_node, 0);
4517 }
4518
4519 abort_fndecl
4520 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4521 ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
4522
4523 /* Perform other language dependent initializations. */
4524 init_class_processing ();
4525 init_rtti_processing ();
4526 init_template_processing ();
4527
4528 if (flag_exceptions)
4529 init_exception_processing ();
4530
4531 if (! supports_one_only ())
4532 flag_weak = 0;
4533
4534 make_fname_decl = cp_make_fname_decl;
4535 start_fname_decls ();
4536
4537 /* Show we use EH for cleanups. */
4538 if (flag_exceptions)
4539 using_eh_for_cleanups ();
4540 }
4541
4542 /* Generate an initializer for a function naming variable from
4543 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4544 filled in with the type of the init. */
4545
4546 tree
4547 cp_fname_init (const char* name, tree *type_p)
4548 {
4549 tree domain = NULL_TREE;
4550 tree type;
4551 tree init = NULL_TREE;
4552 size_t length = 0;
4553
4554 if (name)
4555 {
4556 length = strlen (name);
4557 domain = build_index_type (size_int (length));
4558 init = build_string (length + 1, name);
4559 }
4560
4561 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4562 type = build_cplus_array_type (type, domain);
4563
4564 *type_p = type;
4565
4566 if (init)
4567 TREE_TYPE (init) = type;
4568 else
4569 init = error_mark_node;
4570
4571 return init;
4572 }
4573
4574 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4575 the decl, LOC is the location to give the decl, NAME is the
4576 initialization string and TYPE_DEP indicates whether NAME depended
4577 on the type of the function. We make use of that to detect
4578 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4579 at the point of first use, so we mustn't push the decl now. */
4580
4581 static tree
4582 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4583 {
4584 const char * name = NULL;
4585 bool release_name = false;
4586 if (!(type_dep && in_template_function ()))
4587 {
4588 if (current_function_decl == NULL_TREE)
4589 name = "top level";
4590 else if (type_dep == 1) /* __PRETTY_FUNCTION__ */
4591 name = cxx_printable_name (current_function_decl, 2);
4592 else if (type_dep == 0) /* __FUNCTION__ */
4593 {
4594 name = fname_as_string (type_dep);
4595 release_name = true;
4596 }
4597 else
4598 gcc_unreachable ();
4599 }
4600 tree type;
4601 tree init = cp_fname_init (name, &type);
4602 tree decl = build_decl (loc, VAR_DECL, id, type);
4603
4604 if (release_name)
4605 free (CONST_CAST (char *, name));
4606
4607 /* As we're using pushdecl_with_scope, we must set the context. */
4608 DECL_CONTEXT (decl) = current_function_decl;
4609
4610 TREE_READONLY (decl) = 1;
4611 DECL_ARTIFICIAL (decl) = 1;
4612 DECL_DECLARED_CONSTEXPR_P (decl) = 1;
4613 TREE_STATIC (decl) = 1;
4614
4615 TREE_USED (decl) = 1;
4616
4617 if (init)
4618 {
4619 SET_DECL_VALUE_EXPR (decl, init);
4620 DECL_HAS_VALUE_EXPR_P (decl) = 1;
4621 /* For decl_constant_var_p. */
4622 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
4623 }
4624
4625 if (current_function_decl)
4626 {
4627 DECL_CONTEXT (decl) = current_function_decl;
4628 decl = pushdecl_outermost_localscope (decl);
4629 if (decl != error_mark_node)
4630 add_decl_expr (decl);
4631 }
4632 else
4633 {
4634 DECL_THIS_STATIC (decl) = true;
4635 pushdecl_top_level_and_finish (decl, NULL_TREE);
4636 }
4637
4638 return decl;
4639 }
4640
4641 /* Install DECL as a builtin function at current (global) scope.
4642 Return the new decl (if we found an existing version). Also
4643 installs it into ::std, if it's not '_*'. */
4644
4645 tree
4646 cxx_builtin_function (tree decl)
4647 {
4648 retrofit_lang_decl (decl);
4649
4650 DECL_ARTIFICIAL (decl) = 1;
4651 SET_DECL_LANGUAGE (decl, lang_c);
4652 /* Runtime library routines are, by definition, available in an
4653 external shared object. */
4654 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4655 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4656
4657 tree id = DECL_NAME (decl);
4658 const char *name = IDENTIFIER_POINTER (id);
4659 if (name[0] != '_' || name[1] != '_')
4660 /* In the user's namespace, it must be declared before use. */
4661 DECL_ANTICIPATED (decl) = 1;
4662 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk")
4663 && 0 != strncmp (name + 2, "builtin_", strlen ("builtin_"))
4664 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"),
4665 "_chk", strlen ("_chk") + 1))
4666 /* Treat __*_chk fortification functions as anticipated as well,
4667 unless they are __builtin_*_chk. */
4668 DECL_ANTICIPATED (decl) = 1;
4669
4670 /* All builtins that don't begin with an '_' should additionally
4671 go in the 'std' namespace. */
4672 if (name[0] != '_')
4673 {
4674 tree std_decl = copy_decl (decl);
4675
4676 push_nested_namespace (std_node);
4677 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node);
4678 pushdecl (std_decl);
4679 pop_nested_namespace (std_node);
4680 }
4681
4682 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4683 decl = pushdecl (decl);
4684
4685 return decl;
4686 }
4687
4688 /* Like cxx_builtin_function, but guarantee the function is added to the global
4689 scope. This is to allow function specific options to add new machine
4690 dependent builtins when the target ISA changes via attribute((target(...)))
4691 which saves space on program startup if the program does not use non-generic
4692 ISAs. */
4693
4694 tree
4695 cxx_builtin_function_ext_scope (tree decl)
4696 {
4697 push_nested_namespace (global_namespace);
4698 decl = cxx_builtin_function (decl);
4699 pop_nested_namespace (global_namespace);
4700
4701 return decl;
4702 }
4703
4704 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4705
4706 tree
4707 cxx_simulate_builtin_function_decl (tree decl)
4708 {
4709 retrofit_lang_decl (decl);
4710
4711 DECL_ARTIFICIAL (decl) = 1;
4712 SET_DECL_LANGUAGE (decl, lang_cplusplus);
4713 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4714 return pushdecl (decl);
4715 }
4716
4717 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4718 function. Not called directly. */
4719
4720 static tree
4721 build_library_fn (tree name, enum tree_code operator_code, tree type,
4722 int ecf_flags)
4723 {
4724 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4725 DECL_EXTERNAL (fn) = 1;
4726 TREE_PUBLIC (fn) = 1;
4727 DECL_ARTIFICIAL (fn) = 1;
4728 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn)
4729 = OVL_OP_INFO (false, operator_code)->ovl_op_code;
4730 SET_DECL_LANGUAGE (fn, lang_c);
4731 /* Runtime library routines are, by definition, available in an
4732 external shared object. */
4733 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4734 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4735 set_call_expr_flags (fn, ecf_flags);
4736 return fn;
4737 }
4738
4739 /* Returns the _DECL for a library function with C++ linkage. */
4740
4741 static tree
4742 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4743 int ecf_flags)
4744 {
4745 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4746 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4747 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4748 return fn;
4749 }
4750
4751 /* Like build_library_fn, but takes a C string instead of an
4752 IDENTIFIER_NODE. */
4753
4754 tree
4755 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4756 {
4757 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4758 }
4759
4760 /* Like build_cp_library_fn, but takes a C string instead of an
4761 IDENTIFIER_NODE. */
4762
4763 tree
4764 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4765 {
4766 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4767 ecf_flags);
4768 }
4769
4770 /* Like build_library_fn, but also pushes the function so that we will
4771 be able to find it via get_global_binding. Also, the function
4772 may throw exceptions listed in RAISES. */
4773
4774 tree
4775 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4776 {
4777 tree fn;
4778
4779 if (raises)
4780 type = build_exception_variant (type, raises);
4781
4782 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4783 pushdecl_top_level (fn);
4784 return fn;
4785 }
4786
4787 /* Like build_cp_library_fn, but also pushes the function so that it
4788 will be found by normal lookup. */
4789
4790 static tree
4791 push_cp_library_fn (enum tree_code operator_code, tree type,
4792 int ecf_flags)
4793 {
4794 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code),
4795 operator_code, type, ecf_flags);
4796 pushdecl (fn);
4797 if (flag_tm)
4798 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4799 return fn;
4800 }
4801
4802 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4803 a FUNCTION_TYPE. */
4804
4805 tree
4806 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4807 {
4808 tree type = build_function_type (void_type_node, parmtypes);
4809 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4810 }
4811
4812 /* Like push_library_fn, but also note that this function throws
4813 and does not return. Used for __throw_foo and the like. */
4814
4815 tree
4816 push_throw_library_fn (tree name, tree type)
4817 {
4818 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD);
4819 return fn;
4820 }
4821 \f
4822 /* When we call finish_struct for an anonymous union, we create
4823 default copy constructors and such. But, an anonymous union
4824 shouldn't have such things; this function undoes the damage to the
4825 anonymous union type T.
4826
4827 (The reason that we create the synthesized methods is that we don't
4828 distinguish `union { int i; }' from `typedef union { int i; } U'.
4829 The first is an anonymous union; the second is just an ordinary
4830 union type.) */
4831
4832 void
4833 fixup_anonymous_aggr (tree t)
4834 {
4835 /* Wipe out memory of synthesized methods. */
4836 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4837 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4838 TYPE_HAS_COPY_CTOR (t) = 0;
4839 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4840 TYPE_HAS_COPY_ASSIGN (t) = 0;
4841 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4842
4843 /* Splice the implicitly generated functions out of TYPE_FIELDS. */
4844 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);)
4845 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe))
4846 *prev_p = DECL_CHAIN (probe);
4847 else
4848 prev_p = &DECL_CHAIN (probe);
4849
4850 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4851 assignment operators (because they cannot have these methods themselves).
4852 For anonymous unions this is already checked because they are not allowed
4853 in any union, otherwise we have to check it. */
4854 if (TREE_CODE (t) != UNION_TYPE)
4855 {
4856 tree field, type;
4857
4858 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4859 if (TREE_CODE (field) == FIELD_DECL)
4860 {
4861 type = TREE_TYPE (field);
4862 if (CLASS_TYPE_P (type))
4863 {
4864 if (TYPE_NEEDS_CONSTRUCTING (type))
4865 error ("member %q+#D with constructor not allowed "
4866 "in anonymous aggregate", field);
4867 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4868 error ("member %q+#D with destructor not allowed "
4869 "in anonymous aggregate", field);
4870 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4871 error ("member %q+#D with copy assignment operator "
4872 "not allowed in anonymous aggregate", field);
4873 }
4874 }
4875 }
4876 }
4877
4878 /* Warn for an attribute located at LOCATION that appertains to the
4879 class type CLASS_TYPE that has not been properly placed after its
4880 class-key, in it class-specifier. */
4881
4882 void
4883 warn_misplaced_attr_for_class_type (location_t location,
4884 tree class_type)
4885 {
4886 gcc_assert (OVERLOAD_TYPE_P (class_type));
4887
4888 auto_diagnostic_group d;
4889 if (warning_at (location, OPT_Wattributes,
4890 "attribute ignored in declaration "
4891 "of %q#T", class_type))
4892 inform (location,
4893 "attribute for %q#T must follow the %qs keyword",
4894 class_type, class_key_or_enum_as_string (class_type));
4895 }
4896
4897 /* Returns the cv-qualifiers that apply to the type specified
4898 by the DECLSPECS. */
4899
4900 static int
4901 get_type_quals (const cp_decl_specifier_seq *declspecs)
4902 {
4903 int type_quals = TYPE_UNQUALIFIED;
4904
4905 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4906 type_quals |= TYPE_QUAL_CONST;
4907 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4908 type_quals |= TYPE_QUAL_VOLATILE;
4909 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4910 type_quals |= TYPE_QUAL_RESTRICT;
4911
4912 return type_quals;
4913 }
4914
4915 /* Make sure that a declaration with no declarator is well-formed, i.e.
4916 just declares a tagged type or anonymous union.
4917
4918 Returns the type declared; or NULL_TREE if none. */
4919
4920 tree
4921 check_tag_decl (cp_decl_specifier_seq *declspecs,
4922 bool explicit_type_instantiation_p)
4923 {
4924 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4925 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4926 /* If a class, struct, or enum type is declared by the DECLSPECS
4927 (i.e, if a class-specifier, enum-specifier, or non-typename
4928 elaborated-type-specifier appears in the DECLSPECS),
4929 DECLARED_TYPE is set to the corresponding type. */
4930 tree declared_type = NULL_TREE;
4931 bool error_p = false;
4932
4933 if (declspecs->multiple_types_p)
4934 error_at (smallest_type_location (declspecs),
4935 "multiple types in one declaration");
4936 else if (declspecs->redefined_builtin_type)
4937 {
4938 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec];
4939 if (!in_system_header_at (loc))
4940 permerror (loc, "redeclaration of C++ built-in type %qT",
4941 declspecs->redefined_builtin_type);
4942 return NULL_TREE;
4943 }
4944
4945 if (declspecs->type
4946 && TYPE_P (declspecs->type)
4947 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4948 && MAYBE_CLASS_TYPE_P (declspecs->type))
4949 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4950 declared_type = declspecs->type;
4951 else if (declspecs->type == error_mark_node)
4952 error_p = true;
4953
4954 if (type_uses_auto (declared_type))
4955 {
4956 error_at (declspecs->locations[ds_type_spec],
4957 "%<auto%> can only be specified for variables "
4958 "or function declarations");
4959 return error_mark_node;
4960 }
4961
4962 if (declared_type && !OVERLOAD_TYPE_P (declared_type))
4963 declared_type = NULL_TREE;
4964
4965 if (!declared_type && !saw_friend && !error_p)
4966 permerror (input_location, "declaration does not declare anything");
4967 /* Check for an anonymous union. */
4968 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4969 && TYPE_UNNAMED_P (declared_type))
4970 {
4971 /* 7/3 In a simple-declaration, the optional init-declarator-list
4972 can be omitted only when declaring a class (clause 9) or
4973 enumeration (7.2), that is, when the decl-specifier-seq contains
4974 either a class-specifier, an elaborated-type-specifier with
4975 a class-key (9.1), or an enum-specifier. In these cases and
4976 whenever a class-specifier or enum-specifier is present in the
4977 decl-specifier-seq, the identifiers in these specifiers are among
4978 the names being declared by the declaration (as class-name,
4979 enum-names, or enumerators, depending on the syntax). In such
4980 cases, and except for the declaration of an unnamed bit-field (9.6),
4981 the decl-specifier-seq shall introduce one or more names into the
4982 program, or shall redeclare a name introduced by a previous
4983 declaration. [Example:
4984 enum { }; // ill-formed
4985 typedef class { }; // ill-formed
4986 --end example] */
4987 if (saw_typedef)
4988 {
4989 error_at (declspecs->locations[ds_typedef],
4990 "missing type-name in typedef-declaration");
4991 return NULL_TREE;
4992 }
4993 /* Anonymous unions are objects, so they can have specifiers. */;
4994 SET_ANON_AGGR_TYPE_P (declared_type);
4995
4996 if (TREE_CODE (declared_type) != UNION_TYPE)
4997 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)),
4998 OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4999 }
5000
5001 else
5002 {
5003 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
5004 error_at (declspecs->locations[ds_inline],
5005 "%<inline%> can only be specified for functions");
5006 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
5007 error_at (declspecs->locations[ds_virtual],
5008 "%<virtual%> can only be specified for functions");
5009 else if (saw_friend
5010 && (!current_class_type
5011 || current_scope () != current_class_type))
5012 error_at (declspecs->locations[ds_friend],
5013 "%<friend%> can only be specified inside a class");
5014 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
5015 error_at (declspecs->locations[ds_explicit],
5016 "%<explicit%> can only be specified for constructors");
5017 else if (declspecs->storage_class)
5018 error_at (declspecs->locations[ds_storage_class],
5019 "a storage class can only be specified for objects "
5020 "and functions");
5021 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
5022 error_at (declspecs->locations[ds_const],
5023 "%<const%> can only be specified for objects and "
5024 "functions");
5025 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
5026 error_at (declspecs->locations[ds_volatile],
5027 "%<volatile%> can only be specified for objects and "
5028 "functions");
5029 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
5030 error_at (declspecs->locations[ds_restrict],
5031 "%<__restrict%> can only be specified for objects and "
5032 "functions");
5033 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
5034 error_at (declspecs->locations[ds_thread],
5035 "%<__thread%> can only be specified for objects "
5036 "and functions");
5037 else if (saw_typedef)
5038 warning_at (declspecs->locations[ds_typedef], 0,
5039 "%<typedef%> was ignored in this declaration");
5040 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
5041 error_at (declspecs->locations[ds_constexpr],
5042 "%qs cannot be used for type declarations", "constexpr");
5043 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5044 error_at (declspecs->locations[ds_constinit],
5045 "%qs cannot be used for type declarations", "constinit");
5046 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval))
5047 error_at (declspecs->locations[ds_consteval],
5048 "%qs cannot be used for type declarations", "consteval");
5049 }
5050
5051 if (declspecs->attributes && warn_attributes && declared_type)
5052 {
5053 location_t loc;
5054 if (!CLASS_TYPE_P (declared_type)
5055 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
5056 /* For a non-template class, use the name location. */
5057 loc = location_of (declared_type);
5058 else
5059 /* For a template class (an explicit instantiation), use the
5060 current location. */
5061 loc = input_location;
5062
5063 if (explicit_type_instantiation_p)
5064 /* [dcl.attr.grammar]/4:
5065
5066 No attribute-specifier-seq shall appertain to an explicit
5067 instantiation. */
5068 {
5069 if (warning_at (loc, OPT_Wattributes,
5070 "attribute ignored in explicit instantiation %q#T",
5071 declared_type))
5072 inform (loc,
5073 "no attribute can be applied to "
5074 "an explicit instantiation");
5075 }
5076 else
5077 warn_misplaced_attr_for_class_type (loc, declared_type);
5078 }
5079
5080 return declared_type;
5081 }
5082
5083 /* Called when a declaration is seen that contains no names to declare.
5084 If its type is a reference to a structure, union or enum inherited
5085 from a containing scope, shadow that tag name for the current scope
5086 with a forward reference.
5087 If its type defines a new named structure or union
5088 or defines an enum, it is valid but we need not do anything here.
5089 Otherwise, it is an error.
5090
5091 C++: may have to grok the declspecs to learn about static,
5092 complain for anonymous unions.
5093
5094 Returns the TYPE declared -- or NULL_TREE if none. */
5095
5096 tree
5097 shadow_tag (cp_decl_specifier_seq *declspecs)
5098 {
5099 tree t = check_tag_decl (declspecs,
5100 /*explicit_type_instantiation_p=*/false);
5101
5102 if (!t)
5103 return NULL_TREE;
5104
5105 if (maybe_process_partial_specialization (t) == error_mark_node)
5106 return NULL_TREE;
5107
5108 /* This is where the variables in an anonymous union are
5109 declared. An anonymous union declaration looks like:
5110 union { ... } ;
5111 because there is no declarator after the union, the parser
5112 sends that declaration here. */
5113 if (ANON_AGGR_TYPE_P (t))
5114 {
5115 fixup_anonymous_aggr (t);
5116
5117 if (TYPE_FIELDS (t))
5118 {
5119 tree decl = grokdeclarator (/*declarator=*/NULL,
5120 declspecs, NORMAL, 0, NULL);
5121 finish_anon_union (decl);
5122 }
5123 }
5124
5125 return t;
5126 }
5127 \f
5128 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
5129
5130 tree
5131 groktypename (cp_decl_specifier_seq *type_specifiers,
5132 const cp_declarator *declarator,
5133 bool is_template_arg)
5134 {
5135 tree attrs;
5136 tree type;
5137 enum decl_context context
5138 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
5139 attrs = type_specifiers->attributes;
5140 type_specifiers->attributes = NULL_TREE;
5141 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
5142 if (attrs && type != error_mark_node)
5143 {
5144 if (CLASS_TYPE_P (type))
5145 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
5146 "outside of definition", type);
5147 else if (MAYBE_CLASS_TYPE_P (type))
5148 /* A template type parameter or other dependent type. */
5149 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
5150 "type %qT without an associated declaration", type);
5151 else
5152 cplus_decl_attributes (&type, attrs, 0);
5153 }
5154 return type;
5155 }
5156
5157 /* Process a DECLARATOR for a function-scope variable declaration,
5158 namespace-scope variable declaration, or function declaration.
5159 (Function definitions go through start_function; class member
5160 declarations appearing in the body of the class go through
5161 grokfield.) The DECL corresponding to the DECLARATOR is returned.
5162 If an error occurs, the error_mark_node is returned instead.
5163
5164 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
5165 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
5166 for an explicitly defaulted function, or SD_DELETED for an explicitly
5167 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
5168 implicitly initialized via a default constructor. ATTRIBUTES and
5169 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
5170
5171 The scope represented by the context of the returned DECL is pushed
5172 (if it is not the global namespace) and is assigned to
5173 *PUSHED_SCOPE_P. The caller is then responsible for calling
5174 pop_scope on *PUSHED_SCOPE_P if it is set. */
5175
5176 tree
5177 start_decl (const cp_declarator *declarator,
5178 cp_decl_specifier_seq *declspecs,
5179 int initialized,
5180 tree attributes,
5181 tree prefix_attributes,
5182 tree *pushed_scope_p)
5183 {
5184 tree decl;
5185 tree context;
5186 bool was_public;
5187 int flags;
5188 bool alias;
5189
5190 *pushed_scope_p = NULL_TREE;
5191
5192 /* An object declared as __attribute__((deprecated)) suppresses
5193 warnings of uses of other deprecated items. */
5194 if (lookup_attribute ("deprecated", attributes))
5195 deprecated_state = DEPRECATED_SUPPRESS;
5196
5197 attributes = chainon (attributes, prefix_attributes);
5198
5199 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
5200 &attributes);
5201
5202 deprecated_state = DEPRECATED_NORMAL;
5203
5204 if (decl == NULL_TREE || VOID_TYPE_P (decl)
5205 || decl == error_mark_node)
5206 return error_mark_node;
5207
5208 context = CP_DECL_CONTEXT (decl);
5209 if (context != global_namespace)
5210 *pushed_scope_p = push_scope (context);
5211
5212 /* Is it valid for this decl to have an initializer at all?
5213 If not, set INITIALIZED to zero, which will indirectly
5214 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
5215 if (initialized
5216 && TREE_CODE (decl) == TYPE_DECL)
5217 {
5218 error_at (DECL_SOURCE_LOCATION (decl),
5219 "typedef %qD is initialized (use %qs instead)",
5220 decl, "decltype");
5221 return error_mark_node;
5222 }
5223
5224 if (initialized)
5225 {
5226 if (! toplevel_bindings_p ()
5227 && DECL_EXTERNAL (decl))
5228 warning (0, "declaration of %q#D has %<extern%> and is initialized",
5229 decl);
5230 DECL_EXTERNAL (decl) = 0;
5231 if (toplevel_bindings_p ())
5232 TREE_STATIC (decl) = 1;
5233 }
5234 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
5235
5236 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
5237 record_key_method_defined (decl);
5238
5239 /* If this is a typedef that names the class for linkage purposes
5240 (7.1.3p8), apply any attributes directly to the type. */
5241 if (TREE_CODE (decl) == TYPE_DECL
5242 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
5243 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5244 flags = ATTR_FLAG_TYPE_IN_PLACE;
5245 else
5246 flags = 0;
5247
5248 /* Set attributes here so if duplicate decl, will have proper attributes. */
5249 cplus_decl_attributes (&decl, attributes, flags);
5250
5251 /* Dllimported symbols cannot be defined. Static data members (which
5252 can be initialized in-class and dllimported) go through grokfield,
5253 not here, so we don't need to exclude those decls when checking for
5254 a definition. */
5255 if (initialized && DECL_DLLIMPORT_P (decl))
5256 {
5257 error_at (DECL_SOURCE_LOCATION (decl),
5258 "definition of %q#D is marked %<dllimport%>", decl);
5259 DECL_DLLIMPORT_P (decl) = 0;
5260 }
5261
5262 /* If #pragma weak was used, mark the decl weak now. */
5263 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl))
5264 maybe_apply_pragma_weak (decl);
5265
5266 if (TREE_CODE (decl) == FUNCTION_DECL
5267 && DECL_DECLARED_INLINE_P (decl)
5268 && DECL_UNINLINABLE (decl)
5269 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5270 warning_at (DECL_SOURCE_LOCATION (decl), 0,
5271 "inline function %qD given attribute %qs", decl, "noinline");
5272
5273 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
5274 {
5275 bool this_tmpl = (processing_template_decl
5276 > template_class_depth (context));
5277 if (VAR_P (decl))
5278 {
5279 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
5280 if (field == NULL_TREE
5281 || !(VAR_P (field) || variable_template_p (field)))
5282 error ("%q+#D is not a static data member of %q#T", decl, context);
5283 else if (variable_template_p (field)
5284 && (DECL_LANG_SPECIFIC (decl)
5285 && DECL_TEMPLATE_SPECIALIZATION (decl)))
5286 /* OK, specialization was already checked. */;
5287 else if (variable_template_p (field) && !this_tmpl)
5288 {
5289 error_at (DECL_SOURCE_LOCATION (decl),
5290 "non-member-template declaration of %qD", decl);
5291 inform (DECL_SOURCE_LOCATION (field), "does not match "
5292 "member template declaration here");
5293 return error_mark_node;
5294 }
5295 else
5296 {
5297 if (variable_template_p (field))
5298 field = DECL_TEMPLATE_RESULT (field);
5299
5300 if (DECL_CONTEXT (field) != context)
5301 {
5302 if (!same_type_p (DECL_CONTEXT (field), context))
5303 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
5304 "to be defined as %<%T::%D%>",
5305 DECL_CONTEXT (field), DECL_NAME (decl),
5306 context, DECL_NAME (decl));
5307 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
5308 }
5309 /* Static data member are tricky; an in-class initialization
5310 still doesn't provide a definition, so the in-class
5311 declaration will have DECL_EXTERNAL set, but will have an
5312 initialization. Thus, duplicate_decls won't warn
5313 about this situation, and so we check here. */
5314 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
5315 error ("duplicate initialization of %qD", decl);
5316 field = duplicate_decls (decl, field,
5317 /*newdecl_is_friend=*/false);
5318 if (field == error_mark_node)
5319 return error_mark_node;
5320 else if (field)
5321 decl = field;
5322 }
5323 }
5324 else
5325 {
5326 tree field = check_classfn (context, decl,
5327 this_tmpl
5328 ? current_template_parms
5329 : NULL_TREE);
5330 if (field && field != error_mark_node
5331 && duplicate_decls (decl, field,
5332 /*newdecl_is_friend=*/false))
5333 decl = field;
5334 }
5335
5336 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
5337 DECL_IN_AGGR_P (decl) = 0;
5338 /* Do not mark DECL as an explicit specialization if it was not
5339 already marked as an instantiation; a declaration should
5340 never be marked as a specialization unless we know what
5341 template is being specialized. */
5342 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
5343 {
5344 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
5345 if (TREE_CODE (decl) == FUNCTION_DECL)
5346 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
5347 && DECL_DECLARED_INLINE_P (decl));
5348 else
5349 DECL_COMDAT (decl) = false;
5350
5351 /* [temp.expl.spec] An explicit specialization of a static data
5352 member of a template is a definition if the declaration
5353 includes an initializer; otherwise, it is a declaration.
5354
5355 We check for processing_specialization so this only applies
5356 to the new specialization syntax. */
5357 if (!initialized && processing_specialization)
5358 DECL_EXTERNAL (decl) = 1;
5359 }
5360
5361 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5362 /* Aliases are definitions. */
5363 && !alias)
5364 permerror (declarator->id_loc,
5365 "declaration of %q#D outside of class is not definition",
5366 decl);
5367 }
5368
5369 was_public = TREE_PUBLIC (decl);
5370
5371 /* Enter this declaration into the symbol table. Don't push the plain
5372 VAR_DECL for a variable template. */
5373 if (!template_parm_scope_p ()
5374 || !VAR_P (decl))
5375 decl = maybe_push_decl (decl);
5376
5377 if (processing_template_decl)
5378 {
5379 /* Make sure that for a `constinit' decl push_template_decl creates
5380 a DECL_TEMPLATE_INFO info for us, so that cp_finish_decl can then set
5381 TINFO_VAR_DECLARED_CONSTINIT. */
5382 if (decl_spec_seq_has_spec_p (declspecs, ds_constinit))
5383 retrofit_lang_decl (decl);
5384 decl = push_template_decl (decl);
5385 }
5386 if (decl == error_mark_node)
5387 return error_mark_node;
5388
5389 if (VAR_P (decl)
5390 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5391 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5392 {
5393 /* This is a const variable with implicit 'static'. Set
5394 DECL_THIS_STATIC so we can tell it from variables that are
5395 !TREE_PUBLIC because of the anonymous namespace. */
5396 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5397 DECL_THIS_STATIC (decl) = 1;
5398 }
5399
5400 if (current_function_decl && VAR_P (decl)
5401 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5402 {
5403 bool ok = false;
5404 if (CP_DECL_THREAD_LOCAL_P (decl))
5405 error_at (DECL_SOURCE_LOCATION (decl),
5406 "%qD declared %<thread_local%> in %qs function", decl,
5407 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5408 ? "consteval" : "constexpr");
5409 else if (TREE_STATIC (decl))
5410 error_at (DECL_SOURCE_LOCATION (decl),
5411 "%qD declared %<static%> in %qs function", decl,
5412 DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
5413 ? "consteval" : "constexpr");
5414 else
5415 ok = true;
5416 if (!ok)
5417 cp_function_chain->invalid_constexpr = true;
5418 }
5419
5420 if (!processing_template_decl && VAR_P (decl))
5421 start_decl_1 (decl, initialized);
5422
5423 return decl;
5424 }
5425
5426 /* Process the declaration of a variable DECL. INITIALIZED is true
5427 iff DECL is explicitly initialized. (INITIALIZED is false if the
5428 variable is initialized via an implicitly-called constructor.)
5429 This function must be called for ordinary variables (including, for
5430 example, implicit instantiations of templates), but must not be
5431 called for template declarations. */
5432
5433 void
5434 start_decl_1 (tree decl, bool initialized)
5435 {
5436 tree type;
5437 bool complete_p;
5438 bool aggregate_definition_p;
5439
5440 gcc_assert (!processing_template_decl);
5441
5442 if (error_operand_p (decl))
5443 return;
5444
5445 gcc_assert (VAR_P (decl));
5446
5447 type = TREE_TYPE (decl);
5448 complete_p = COMPLETE_TYPE_P (type);
5449 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5450
5451 /* If an explicit initializer is present, or if this is a definition
5452 of an aggregate, then we need a complete type at this point.
5453 (Scalars are always complete types, so there is nothing to
5454 check.) This code just sets COMPLETE_P; errors (if necessary)
5455 are issued below. */
5456 if ((initialized || aggregate_definition_p)
5457 && !complete_p
5458 && COMPLETE_TYPE_P (complete_type (type)))
5459 {
5460 complete_p = true;
5461 /* We will not yet have set TREE_READONLY on DECL if the type
5462 was "const", but incomplete, before this point. But, now, we
5463 have a complete type, so we can try again. */
5464 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5465 }
5466
5467 if (is_global_var (decl))
5468 {
5469 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5470 ? TCTX_THREAD_STORAGE
5471 : TCTX_STATIC_STORAGE);
5472 verify_type_context (input_location, context, TREE_TYPE (decl));
5473 }
5474 if (initialized)
5475 /* Is it valid for this decl to have an initializer at all? */
5476 {
5477 /* Don't allow initializations for incomplete types except for
5478 arrays which might be completed by the initialization. */
5479 if (complete_p)
5480 ; /* A complete type is ok. */
5481 else if (type_uses_auto (type))
5482 ; /* An auto type is ok. */
5483 else if (TREE_CODE (type) != ARRAY_TYPE)
5484 {
5485 error ("variable %q#D has initializer but incomplete type", decl);
5486 type = TREE_TYPE (decl) = error_mark_node;
5487 }
5488 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5489 {
5490 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5491 error ("elements of array %q#D have incomplete type", decl);
5492 /* else we already gave an error in start_decl. */
5493 }
5494 }
5495 else if (aggregate_definition_p && !complete_p)
5496 {
5497 if (type_uses_auto (type))
5498 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type));
5499 else
5500 {
5501 error ("aggregate %q#D has incomplete type and cannot be defined",
5502 decl);
5503 /* Change the type so that assemble_variable will give
5504 DECL an rtl we can live with: (mem (const_int 0)). */
5505 type = TREE_TYPE (decl) = error_mark_node;
5506 }
5507 }
5508
5509 /* Create a new scope to hold this declaration if necessary.
5510 Whether or not a new scope is necessary cannot be determined
5511 until after the type has been completed; if the type is a
5512 specialization of a class template it is not until after
5513 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5514 will be set correctly. */
5515 maybe_push_cleanup_level (type);
5516 }
5517
5518 /* Handle initialization of references. DECL, TYPE, and INIT have the
5519 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5520 but will be set to a new CLEANUP_STMT if a temporary is created
5521 that must be destroyed subsequently.
5522
5523 Returns an initializer expression to use to initialize DECL, or
5524 NULL if the initialization can be performed statically.
5525
5526 Quotes on semantics can be found in ARM 8.4.3. */
5527
5528 static tree
5529 grok_reference_init (tree decl, tree type, tree init, int flags)
5530 {
5531 if (init == NULL_TREE)
5532 {
5533 if ((DECL_LANG_SPECIFIC (decl) == 0
5534 || DECL_IN_AGGR_P (decl) == 0)
5535 && ! DECL_THIS_EXTERN (decl))
5536 error_at (DECL_SOURCE_LOCATION (decl),
5537 "%qD declared as reference but not initialized", decl);
5538 return NULL_TREE;
5539 }
5540
5541 tree ttype = TREE_TYPE (type);
5542 if (TREE_CODE (init) == TREE_LIST)
5543 {
5544 /* This handles (C++20 only) code like
5545
5546 const A& r(1, 2, 3);
5547
5548 where we treat the parenthesized list as a CONSTRUCTOR. */
5549 if (TREE_TYPE (init) == NULL_TREE
5550 && CP_AGGREGATE_TYPE_P (ttype)
5551 && !DECL_DECOMPOSITION_P (decl)
5552 && (cxx_dialect >= cxx2a))
5553 {
5554 init = build_constructor_from_list (init_list_type_node, init);
5555 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
5556 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
5557 }
5558 else
5559 init = build_x_compound_expr_from_list (init, ELK_INIT,
5560 tf_warning_or_error);
5561 }
5562
5563 if (TREE_CODE (ttype) != ARRAY_TYPE
5564 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5565 /* Note: default conversion is only called in very special cases. */
5566 init = decay_conversion (init, tf_warning_or_error);
5567
5568 /* check_initializer handles this for non-reference variables, but for
5569 references we need to do it here or the initializer will get the
5570 incomplete array type and confuse later calls to
5571 cp_complete_array_type. */
5572 if (TREE_CODE (ttype) == ARRAY_TYPE
5573 && TYPE_DOMAIN (ttype) == NULL_TREE
5574 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5575 || TREE_CODE (init) == STRING_CST))
5576 {
5577 cp_complete_array_type (&ttype, init, false);
5578 if (ttype != TREE_TYPE (type))
5579 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5580 }
5581
5582 /* Convert INIT to the reference type TYPE. This may involve the
5583 creation of a temporary, whose lifetime must be the same as that
5584 of the reference. If so, a DECL_EXPR for the temporary will be
5585 added just after the DECL_EXPR for DECL. That's why we don't set
5586 DECL_INITIAL for local references (instead assigning to them
5587 explicitly); we need to allow the temporary to be initialized
5588 first. */
5589 return initialize_reference (type, init, flags,
5590 tf_warning_or_error);
5591 }
5592
5593 /* Designated initializers in arrays are not supported in GNU C++.
5594 The parser cannot detect this error since it does not know whether
5595 a given brace-enclosed initializer is for a class type or for an
5596 array. This function checks that CE does not use a designated
5597 initializer. If it does, an error is issued. Returns true if CE
5598 is valid, i.e., does not have a designated initializer. */
5599
5600 bool
5601 check_array_designated_initializer (constructor_elt *ce,
5602 unsigned HOST_WIDE_INT index)
5603 {
5604 /* Designated initializers for array elements are not supported. */
5605 if (ce->index)
5606 {
5607 /* The parser only allows identifiers as designated
5608 initializers. */
5609 if (ce->index == error_mark_node)
5610 {
5611 error ("name used in a GNU-style designated "
5612 "initializer for an array");
5613 return false;
5614 }
5615 else if (identifier_p (ce->index))
5616 {
5617 error ("name %qD used in a GNU-style designated "
5618 "initializer for an array", ce->index);
5619 return false;
5620 }
5621
5622 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5623 ce->index, true);
5624 if (ce_index
5625 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5626 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index))
5627 == INTEGER_CST))
5628 {
5629 /* A C99 designator is OK if it matches the current index. */
5630 if (wi::to_wide (ce_index) == index)
5631 {
5632 ce->index = ce_index;
5633 return true;
5634 }
5635 else
5636 sorry ("non-trivial designated initializers not supported");
5637 }
5638 else
5639 error_at (cp_expr_loc_or_input_loc (ce->index),
5640 "C99 designator %qE is not an integral constant-expression",
5641 ce->index);
5642
5643 return false;
5644 }
5645
5646 return true;
5647 }
5648
5649 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5650 array until we finish parsing the initializer. If that's the
5651 situation we're in, update DECL accordingly. */
5652
5653 static void
5654 maybe_deduce_size_from_array_init (tree decl, tree init)
5655 {
5656 tree type = TREE_TYPE (decl);
5657
5658 if (TREE_CODE (type) == ARRAY_TYPE
5659 && TYPE_DOMAIN (type) == NULL_TREE
5660 && TREE_CODE (decl) != TYPE_DECL)
5661 {
5662 /* do_default is really a C-ism to deal with tentative definitions.
5663 But let's leave it here to ease the eventual merge. */
5664 int do_default = !DECL_EXTERNAL (decl);
5665 tree initializer = init ? init : DECL_INITIAL (decl);
5666 int failure = 0;
5667
5668 /* Check that there are no designated initializers in INIT, as
5669 those are not supported in GNU C++, and as the middle-end
5670 will crash if presented with a non-numeric designated
5671 initializer. */
5672 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5673 {
5674 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5675 constructor_elt *ce;
5676 HOST_WIDE_INT i;
5677 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5678 {
5679 if (instantiation_dependent_expression_p (ce->index))
5680 return;
5681 if (!check_array_designated_initializer (ce, i))
5682 failure = 1;
5683 }
5684 }
5685
5686 if (failure)
5687 TREE_TYPE (decl) = error_mark_node;
5688 else
5689 {
5690 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5691 do_default);
5692 if (failure == 1)
5693 {
5694 error_at (cp_expr_loc_or_loc (initializer,
5695 DECL_SOURCE_LOCATION (decl)),
5696 "initializer fails to determine size of %qD", decl);
5697 }
5698 else if (failure == 2)
5699 {
5700 if (do_default)
5701 {
5702 error_at (DECL_SOURCE_LOCATION (decl),
5703 "array size missing in %qD", decl);
5704 }
5705 /* If a `static' var's size isn't known, make it extern as
5706 well as static, so it does not get allocated. If it's not
5707 `static', then don't mark it extern; finish_incomplete_decl
5708 will give it a default size and it will get allocated. */
5709 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5710 DECL_EXTERNAL (decl) = 1;
5711 }
5712 else if (failure == 3)
5713 {
5714 error_at (DECL_SOURCE_LOCATION (decl),
5715 "zero-size array %qD", decl);
5716 }
5717 }
5718
5719 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5720
5721 relayout_decl (decl);
5722 }
5723 }
5724
5725 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5726 any appropriate error messages regarding the layout. */
5727
5728 static void
5729 layout_var_decl (tree decl)
5730 {
5731 tree type;
5732
5733 type = TREE_TYPE (decl);
5734 if (type == error_mark_node)
5735 return;
5736
5737 /* If we haven't already laid out this declaration, do so now.
5738 Note that we must not call complete type for an external object
5739 because it's type might involve templates that we are not
5740 supposed to instantiate yet. (And it's perfectly valid to say
5741 `extern X x' for some incomplete type `X'.) */
5742 if (!DECL_EXTERNAL (decl))
5743 complete_type (type);
5744 if (!DECL_SIZE (decl)
5745 && TREE_TYPE (decl) != error_mark_node
5746 && complete_or_array_type_p (type))
5747 layout_decl (decl, 0);
5748
5749 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5750 {
5751 /* An automatic variable with an incomplete type: that is an error.
5752 Don't talk about array types here, since we took care of that
5753 message in grokdeclarator. */
5754 error_at (DECL_SOURCE_LOCATION (decl),
5755 "storage size of %qD isn%'t known", decl);
5756 TREE_TYPE (decl) = error_mark_node;
5757 }
5758 #if 0
5759 /* Keep this code around in case we later want to control debug info
5760 based on whether a type is "used". (jason 1999-11-11) */
5761
5762 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5763 /* Let debugger know it should output info for this type. */
5764 note_debug_info_needed (ttype);
5765
5766 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5767 note_debug_info_needed (DECL_CONTEXT (decl));
5768 #endif
5769
5770 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5771 && DECL_SIZE (decl) != NULL_TREE
5772 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5773 {
5774 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5775 constant_expression_warning (DECL_SIZE (decl));
5776 else
5777 {
5778 error_at (DECL_SOURCE_LOCATION (decl),
5779 "storage size of %qD isn%'t constant", decl);
5780 TREE_TYPE (decl) = error_mark_node;
5781 }
5782 }
5783 }
5784
5785 /* If a local static variable is declared in an inline function, or if
5786 we have a weak definition, we must endeavor to create only one
5787 instance of the variable at link-time. */
5788
5789 void
5790 maybe_commonize_var (tree decl)
5791 {
5792 /* Don't mess with __FUNCTION__ and similar. */
5793 if (DECL_ARTIFICIAL (decl))
5794 return;
5795
5796 /* Static data in a function with comdat linkage also has comdat
5797 linkage. */
5798 if ((TREE_STATIC (decl)
5799 && DECL_FUNCTION_SCOPE_P (decl)
5800 && vague_linkage_p (DECL_CONTEXT (decl)))
5801 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl)))
5802 {
5803 if (flag_weak)
5804 {
5805 /* With weak symbols, we simply make the variable COMDAT;
5806 that will cause copies in multiple translations units to
5807 be merged. */
5808 comdat_linkage (decl);
5809 }
5810 else
5811 {
5812 if (DECL_INITIAL (decl) == NULL_TREE
5813 || DECL_INITIAL (decl) == error_mark_node)
5814 {
5815 /* Without weak symbols, we can use COMMON to merge
5816 uninitialized variables. */
5817 TREE_PUBLIC (decl) = 1;
5818 DECL_COMMON (decl) = 1;
5819 }
5820 else
5821 {
5822 /* While for initialized variables, we must use internal
5823 linkage -- which means that multiple copies will not
5824 be merged. */
5825 TREE_PUBLIC (decl) = 0;
5826 DECL_COMMON (decl) = 0;
5827 DECL_INTERFACE_KNOWN (decl) = 1;
5828 const char *msg;
5829 if (DECL_INLINE_VAR_P (decl))
5830 msg = G_("sorry: semantics of inline variable "
5831 "%q#D are wrong (you%'ll wind up with "
5832 "multiple copies)");
5833 else
5834 msg = G_("sorry: semantics of inline function "
5835 "static data %q#D are wrong (you%'ll wind "
5836 "up with multiple copies)");
5837 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5838 msg, decl))
5839 inform (DECL_SOURCE_LOCATION (decl),
5840 "you can work around this by removing the initializer");
5841 }
5842 }
5843 }
5844 }
5845
5846 /* Issue an error message if DECL is an uninitialized const variable.
5847 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr
5848 context from potential_constant_expression. Returns true if all is well,
5849 false otherwise. */
5850
5851 bool
5852 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p,
5853 tsubst_flags_t complain)
5854 {
5855 tree type = strip_array_types (TREE_TYPE (decl));
5856
5857 /* ``Unless explicitly declared extern, a const object does not have
5858 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5859 7.1.6 */
5860 if (VAR_P (decl)
5861 && !TYPE_REF_P (type)
5862 && (CP_TYPE_CONST_P (type)
5863 /* C++20 permits trivial default initialization in constexpr
5864 context (P1331R2). */
5865 || (cxx_dialect < cxx2a
5866 && (constexpr_context_p
5867 || var_in_constexpr_fn (decl))))
5868 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl))
5869 {
5870 tree field = default_init_uninitialized_part (type);
5871 if (!field)
5872 return true;
5873
5874 bool show_notes = true;
5875
5876 if (!constexpr_context_p || cxx_dialect >= cxx2a)
5877 {
5878 if (CP_TYPE_CONST_P (type))
5879 {
5880 if (complain & tf_error)
5881 show_notes = permerror (DECL_SOURCE_LOCATION (decl),
5882 "uninitialized %<const %D%>", decl);
5883 }
5884 else
5885 {
5886 if (!is_instantiation_of_constexpr (current_function_decl)
5887 && (complain & tf_error))
5888 error_at (DECL_SOURCE_LOCATION (decl),
5889 "uninitialized variable %qD in %<constexpr%> "
5890 "function", decl);
5891 else
5892 show_notes = false;
5893 cp_function_chain->invalid_constexpr = true;
5894 }
5895 }
5896 else if (complain & tf_error)
5897 error_at (DECL_SOURCE_LOCATION (decl),
5898 "uninitialized variable %qD in %<constexpr%> context",
5899 decl);
5900
5901 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error))
5902 {
5903 tree defaulted_ctor;
5904
5905 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5906 "%q#T has no user-provided default constructor", type);
5907 defaulted_ctor = in_class_defaulted_default_constructor (type);
5908 if (defaulted_ctor)
5909 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5910 "constructor is not user-provided because it is "
5911 "explicitly defaulted in the class body");
5912 inform (DECL_SOURCE_LOCATION (field),
5913 "and the implicitly-defined constructor does not "
5914 "initialize %q#D", field);
5915 }
5916
5917 return false;
5918 }
5919
5920 return true;
5921 }
5922 \f
5923 /* Structure holding the current initializer being processed by reshape_init.
5924 CUR is a pointer to the current element being processed, END is a pointer
5925 after the last element present in the initializer. */
5926 struct reshape_iter
5927 {
5928 constructor_elt *cur;
5929 constructor_elt *end;
5930 };
5931
5932 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5933
5934 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5935 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5936 initialized. If there are no more such fields, the return value
5937 will be NULL. */
5938
5939 tree
5940 next_initializable_field (tree field)
5941 {
5942 while (field
5943 && (TREE_CODE (field) != FIELD_DECL
5944 || DECL_UNNAMED_BIT_FIELD (field)
5945 || (DECL_ARTIFICIAL (field)
5946 /* In C++17, don't skip base class fields. */
5947 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))
5948 /* Don't skip vptr fields. We might see them when we're
5949 called from reduced_constant_expression_p. */
5950 && !DECL_VIRTUAL_P (field))))
5951 field = DECL_CHAIN (field);
5952
5953 return field;
5954 }
5955
5956 /* Return true for [dcl.init.list] direct-list-initialization from
5957 single element of enumeration with a fixed underlying type. */
5958
5959 bool
5960 is_direct_enum_init (tree type, tree init)
5961 {
5962 if (cxx_dialect >= cxx17
5963 && TREE_CODE (type) == ENUMERAL_TYPE
5964 && ENUM_FIXED_UNDERLYING_TYPE_P (type)
5965 && TREE_CODE (init) == CONSTRUCTOR
5966 && CONSTRUCTOR_IS_DIRECT_INIT (init)
5967 && CONSTRUCTOR_NELTS (init) == 1)
5968 return true;
5969 return false;
5970 }
5971
5972 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5973 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5974 INTEGER_CST representing the size of the array minus one (the maximum index),
5975 or NULL_TREE if the array was declared without specifying the size. D is
5976 the iterator within the constructor. */
5977
5978 static tree
5979 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5980 tsubst_flags_t complain)
5981 {
5982 tree new_init;
5983 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5984 unsigned HOST_WIDE_INT max_index_cst = 0;
5985 unsigned HOST_WIDE_INT index;
5986
5987 /* The initializer for an array is always a CONSTRUCTOR. */
5988 new_init = build_constructor (init_list_type_node, NULL);
5989
5990 if (sized_array_p)
5991 {
5992 /* Minus 1 is used for zero sized arrays. */
5993 if (integer_all_onesp (max_index))
5994 return new_init;
5995
5996 if (tree_fits_uhwi_p (max_index))
5997 max_index_cst = tree_to_uhwi (max_index);
5998 /* sizetype is sign extended, not zero extended. */
5999 else
6000 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
6001 }
6002
6003 /* Set to the index of the last element with a non-zero initializer.
6004 Zero initializers for elements past this one can be dropped. */
6005 unsigned HOST_WIDE_INT last_nonzero = -1;
6006 /* Loop until there are no more initializers. */
6007 for (index = 0;
6008 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
6009 ++index)
6010 {
6011 tree elt_init;
6012 constructor_elt *old_cur = d->cur;
6013
6014 check_array_designated_initializer (d->cur, index);
6015 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
6016 complain);
6017 if (elt_init == error_mark_node)
6018 return error_mark_node;
6019 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
6020 size_int (index), elt_init);
6021 if (!TREE_CONSTANT (elt_init))
6022 TREE_CONSTANT (new_init) = false;
6023
6024 /* Pointers initialized to strings must be treated as non-zero
6025 even if the string is empty. */
6026 tree init_type = TREE_TYPE (elt_init);
6027 if (POINTER_TYPE_P (elt_type) != POINTER_TYPE_P (init_type)
6028 || !type_initializer_zero_p (elt_type, elt_init))
6029 last_nonzero = index;
6030
6031 /* This can happen with an invalid initializer (c++/54501). */
6032 if (d->cur == old_cur && !sized_array_p)
6033 break;
6034 }
6035
6036 if (sized_array_p && trivial_type_p (elt_type))
6037 {
6038 /* Strip trailing zero-initializers from an array of a trivial
6039 type of known size. They are redundant and get in the way
6040 of telling them apart from those with implicit zero value. */
6041 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (new_init);
6042 if (last_nonzero > nelts)
6043 nelts = 0;
6044 else if (last_nonzero < nelts - 1)
6045 nelts = last_nonzero + 1;
6046
6047 vec_safe_truncate (CONSTRUCTOR_ELTS (new_init), nelts);
6048 }
6049
6050 return new_init;
6051 }
6052
6053 /* Subroutine of reshape_init_r, processes the initializers for arrays.
6054 Parameters are the same of reshape_init_r. */
6055
6056 static tree
6057 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
6058 {
6059 tree max_index = NULL_TREE;
6060
6061 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6062
6063 if (TYPE_DOMAIN (type))
6064 max_index = array_type_nelts (type);
6065
6066 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
6067 }
6068
6069 /* Subroutine of reshape_init_r, processes the initializers for vectors.
6070 Parameters are the same of reshape_init_r. */
6071
6072 static tree
6073 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
6074 {
6075 tree max_index = NULL_TREE;
6076
6077 gcc_assert (VECTOR_TYPE_P (type));
6078
6079 if (COMPOUND_LITERAL_P (d->cur->value))
6080 {
6081 tree value = d->cur->value;
6082 if (!same_type_p (TREE_TYPE (value), type))
6083 {
6084 if (complain & tf_error)
6085 error ("invalid type %qT as initializer for a vector of type %qT",
6086 TREE_TYPE (d->cur->value), type);
6087 value = error_mark_node;
6088 }
6089 ++d->cur;
6090 return value;
6091 }
6092
6093 /* For a vector, we initialize it as an array of the appropriate size. */
6094 if (VECTOR_TYPE_P (type))
6095 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
6096
6097 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
6098 }
6099
6100 /* Subroutine of reshape_init_r, processes the initializers for classes
6101 or union. Parameters are the same of reshape_init_r. */
6102
6103 static tree
6104 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
6105 tsubst_flags_t complain)
6106 {
6107 tree field;
6108 tree new_init;
6109
6110 gcc_assert (CLASS_TYPE_P (type));
6111
6112 /* The initializer for a class is always a CONSTRUCTOR. */
6113 new_init = build_constructor (init_list_type_node, NULL);
6114 field = next_initializable_field (TYPE_FIELDS (type));
6115
6116 if (!field)
6117 {
6118 /* [dcl.init.aggr]
6119
6120 An initializer for an aggregate member that is an
6121 empty class shall have the form of an empty
6122 initializer-list {}. */
6123 if (!first_initializer_p)
6124 {
6125 if (complain & tf_error)
6126 error ("initializer for %qT must be brace-enclosed", type);
6127 return error_mark_node;
6128 }
6129 return new_init;
6130 }
6131
6132 /* Loop through the initializable fields, gathering initializers. */
6133 while (d->cur != d->end)
6134 {
6135 tree field_init;
6136 constructor_elt *old_cur = d->cur;
6137
6138 /* Handle designated initializers, as an extension. */
6139 if (d->cur->index)
6140 {
6141 if (d->cur->index == error_mark_node)
6142 return error_mark_node;
6143
6144 if (TREE_CODE (d->cur->index) == FIELD_DECL)
6145 {
6146 /* We already reshaped this. */
6147 if (field != d->cur->index)
6148 {
6149 tree id = DECL_NAME (d->cur->index);
6150 gcc_assert (id);
6151 gcc_checking_assert (d->cur->index
6152 == get_class_binding (type, id));
6153 field = d->cur->index;
6154 }
6155 }
6156 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
6157 field = get_class_binding (type, d->cur->index);
6158 else
6159 {
6160 if (complain & tf_error)
6161 error ("%<[%E] =%> used in a GNU-style designated initializer"
6162 " for class %qT", d->cur->index, type);
6163 return error_mark_node;
6164 }
6165
6166 if (!field || TREE_CODE (field) != FIELD_DECL)
6167 {
6168 if (complain & tf_error)
6169 error ("%qT has no non-static data member named %qD", type,
6170 d->cur->index);
6171 return error_mark_node;
6172 }
6173 }
6174
6175 /* If we processed all the member of the class, we are done. */
6176 if (!field)
6177 break;
6178
6179 field_init = reshape_init_r (TREE_TYPE (field), d,
6180 /*first_initializer_p=*/false, complain);
6181 if (field_init == error_mark_node)
6182 return error_mark_node;
6183
6184 if (d->cur == old_cur && d->cur->index)
6185 {
6186 /* This can happen with an invalid initializer for a flexible
6187 array member (c++/54441). */
6188 if (complain & tf_error)
6189 error ("invalid initializer for %q#D", field);
6190 return error_mark_node;
6191 }
6192
6193 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
6194
6195 /* [dcl.init.aggr]
6196
6197 When a union is initialized with a brace-enclosed
6198 initializer, the braces shall only contain an
6199 initializer for the first member of the union. */
6200 if (TREE_CODE (type) == UNION_TYPE)
6201 break;
6202
6203 field = next_initializable_field (DECL_CHAIN (field));
6204 }
6205
6206 return new_init;
6207 }
6208
6209 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
6210 designators are not valid; either complain or return true to indicate
6211 that reshape_init_r should return error_mark_node. */
6212
6213 static bool
6214 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
6215 {
6216 if (d->cur->index)
6217 {
6218 if (complain & tf_error)
6219 error_at (cp_expr_loc_or_input_loc (d->cur->index),
6220 "C99 designator %qE outside aggregate initializer",
6221 d->cur->index);
6222 else
6223 return true;
6224 }
6225 return false;
6226 }
6227
6228 /* Subroutine of reshape_init, which processes a single initializer (part of
6229 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
6230 iterator within the CONSTRUCTOR which points to the initializer to process.
6231 FIRST_INITIALIZER_P is true if this is the first initializer of the
6232 outermost CONSTRUCTOR node. */
6233
6234 static tree
6235 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
6236 tsubst_flags_t complain)
6237 {
6238 tree init = d->cur->value;
6239
6240 if (error_operand_p (init))
6241 return error_mark_node;
6242
6243 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
6244 && has_designator_problem (d, complain))
6245 return error_mark_node;
6246
6247 tree stripped_init = tree_strip_any_location_wrapper (init);
6248
6249 if (TREE_CODE (type) == COMPLEX_TYPE)
6250 {
6251 /* A complex type can be initialized from one or two initializers,
6252 but braces are not elided. */
6253 d->cur++;
6254 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
6255 {
6256 if (CONSTRUCTOR_NELTS (stripped_init) > 2)
6257 {
6258 if (complain & tf_error)
6259 error ("too many initializers for %qT", type);
6260 else
6261 return error_mark_node;
6262 }
6263 }
6264 else if (first_initializer_p && d->cur != d->end)
6265 {
6266 vec<constructor_elt, va_gc> *v = 0;
6267 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
6268 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
6269 if (has_designator_problem (d, complain))
6270 return error_mark_node;
6271 d->cur++;
6272 init = build_constructor (init_list_type_node, v);
6273 }
6274 return init;
6275 }
6276
6277 /* A non-aggregate type is always initialized with a single
6278 initializer. */
6279 if (!CP_AGGREGATE_TYPE_P (type))
6280 {
6281 /* It is invalid to initialize a non-aggregate type with a
6282 brace-enclosed initializer before C++0x.
6283 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
6284 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
6285 a CONSTRUCTOR (with a record type). */
6286 if (TREE_CODE (stripped_init) == CONSTRUCTOR
6287 /* Don't complain about a capture-init. */
6288 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init)
6289 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */
6290 {
6291 if (SCALAR_TYPE_P (type))
6292 {
6293 if (cxx_dialect < cxx11)
6294 {
6295 if (complain & tf_error)
6296 error ("braces around scalar initializer for type %qT",
6297 type);
6298 init = error_mark_node;
6299 }
6300 else if (first_initializer_p
6301 || (CONSTRUCTOR_NELTS (stripped_init) > 0
6302 && (BRACE_ENCLOSED_INITIALIZER_P
6303 (CONSTRUCTOR_ELT (stripped_init,0)->value))))
6304 {
6305 if (complain & tf_error)
6306 error ("too many braces around scalar initializer "
6307 "for type %qT", type);
6308 init = error_mark_node;
6309 }
6310 }
6311 else
6312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6313 }
6314
6315 d->cur++;
6316 return init;
6317 }
6318
6319 /* "If T is a class type and the initializer list has a single element of
6320 type cv U, where U is T or a class derived from T, the object is
6321 initialized from that element." Even if T is an aggregate. */
6322 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
6323 && first_initializer_p
6324 && d->end - d->cur == 1
6325 && reference_related_p (type, TREE_TYPE (init)))
6326 {
6327 d->cur++;
6328 return init;
6329 }
6330
6331 /* [dcl.init.aggr]
6332
6333 All implicit type conversions (clause _conv_) are considered when
6334 initializing the aggregate member with an initializer from an
6335 initializer-list. If the initializer can initialize a member,
6336 the member is initialized. Otherwise, if the member is itself a
6337 non-empty subaggregate, brace elision is assumed and the
6338 initializer is considered for the initialization of the first
6339 member of the subaggregate. */
6340 if (TREE_CODE (init) != CONSTRUCTOR
6341 /* But don't try this for the first initializer, since that would be
6342 looking through the outermost braces; A a2 = { a1 }; is not a
6343 valid aggregate initialization. */
6344 && !first_initializer_p
6345 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
6346 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
6347 complain)))
6348 {
6349 d->cur++;
6350 return init;
6351 }
6352
6353 /* [dcl.init.string]
6354
6355 A char array (whether plain char, signed char, or unsigned char)
6356 can be initialized by a string-literal (optionally enclosed in
6357 braces); a wchar_t array can be initialized by a wide
6358 string-literal (optionally enclosed in braces). */
6359 if (TREE_CODE (type) == ARRAY_TYPE
6360 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
6361 {
6362 tree str_init = init;
6363 tree stripped_str_init = stripped_init;
6364
6365 /* Strip one level of braces if and only if they enclose a single
6366 element (as allowed by [dcl.init.string]). */
6367 if (!first_initializer_p
6368 && TREE_CODE (stripped_str_init) == CONSTRUCTOR
6369 && CONSTRUCTOR_NELTS (stripped_str_init) == 1)
6370 {
6371 str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value;
6372 stripped_str_init = tree_strip_any_location_wrapper (str_init);
6373 }
6374
6375 /* If it's a string literal, then it's the initializer for the array
6376 as a whole. Otherwise, continue with normal initialization for
6377 array types (one value per array element). */
6378 if (TREE_CODE (stripped_str_init) == STRING_CST)
6379 {
6380 if (has_designator_problem (d, complain))
6381 return error_mark_node;
6382 d->cur++;
6383 return str_init;
6384 }
6385 }
6386
6387 /* The following cases are about aggregates. If we are not within a full
6388 initializer already, and there is not a CONSTRUCTOR, it means that there
6389 is a missing set of braces (that is, we are processing the case for
6390 which reshape_init exists). */
6391 if (!first_initializer_p)
6392 {
6393 if (TREE_CODE (stripped_init) == CONSTRUCTOR)
6394 {
6395 tree init_type = TREE_TYPE (init);
6396 if (init_type && TYPE_PTRMEMFUNC_P (init_type))
6397 /* There is no need to call reshape_init for pointer-to-member
6398 function initializers, as they are always constructed correctly
6399 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0},
6400 which is missing outermost braces. We should warn below, and
6401 one of the routines below will wrap it in additional { }. */;
6402 /* For a nested compound literal, proceed to specialized routines,
6403 to handle initialization of arrays and similar. */
6404 else if (COMPOUND_LITERAL_P (stripped_init))
6405 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6406 /* A CONSTRUCTOR of the target's type is a previously
6407 digested initializer. */
6408 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type))
6409 {
6410 ++d->cur;
6411 return init;
6412 }
6413 else
6414 {
6415 /* Something that hasn't been reshaped yet. */
6416 ++d->cur;
6417 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init));
6418 return reshape_init (type, init, complain);
6419 }
6420 }
6421
6422 if (complain & tf_warning)
6423 warning (OPT_Wmissing_braces,
6424 "missing braces around initializer for %qT",
6425 type);
6426 }
6427
6428 /* Dispatch to specialized routines. */
6429 if (CLASS_TYPE_P (type))
6430 return reshape_init_class (type, d, first_initializer_p, complain);
6431 else if (TREE_CODE (type) == ARRAY_TYPE)
6432 return reshape_init_array (type, d, complain);
6433 else if (VECTOR_TYPE_P (type))
6434 return reshape_init_vector (type, d, complain);
6435 else
6436 gcc_unreachable();
6437 }
6438
6439 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
6440 brace-enclosed aggregate initializer.
6441
6442 INIT is the CONSTRUCTOR containing the list of initializers describing
6443 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
6444 It may not presently match the shape of the TYPE; for example:
6445
6446 struct S { int a; int b; };
6447 struct S a[] = { 1, 2, 3, 4 };
6448
6449 Here INIT will hold a vector of four elements, rather than a
6450 vector of two elements, each itself a vector of two elements. This
6451 routine transforms INIT from the former form into the latter. The
6452 revised CONSTRUCTOR node is returned. */
6453
6454 tree
6455 reshape_init (tree type, tree init, tsubst_flags_t complain)
6456 {
6457 vec<constructor_elt, va_gc> *v;
6458 reshape_iter d;
6459 tree new_init;
6460
6461 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
6462
6463 v = CONSTRUCTOR_ELTS (init);
6464
6465 /* An empty constructor does not need reshaping, and it is always a valid
6466 initializer. */
6467 if (vec_safe_is_empty (v))
6468 return init;
6469
6470 /* Brace elision is not performed for a CONSTRUCTOR representing
6471 parenthesized aggregate initialization. */
6472 if (CONSTRUCTOR_IS_PAREN_INIT (init))
6473 return init;
6474
6475 /* Handle [dcl.init.list] direct-list-initialization from
6476 single element of enumeration with a fixed underlying type. */
6477 if (is_direct_enum_init (type, init))
6478 {
6479 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
6480 type = cv_unqualified (type);
6481 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
6482 {
6483 warning_sentinel w (warn_useless_cast);
6484 warning_sentinel w2 (warn_ignored_qualifiers);
6485 return cp_build_c_cast (input_location, type, elt,
6486 tf_warning_or_error);
6487 }
6488 else
6489 return error_mark_node;
6490 }
6491
6492 /* Recurse on this CONSTRUCTOR. */
6493 d.cur = &(*v)[0];
6494 d.end = d.cur + v->length ();
6495
6496 new_init = reshape_init_r (type, &d, true, complain);
6497 if (new_init == error_mark_node)
6498 return error_mark_node;
6499
6500 /* Make sure all the element of the constructor were used. Otherwise,
6501 issue an error about exceeding initializers. */
6502 if (d.cur != d.end)
6503 {
6504 if (complain & tf_error)
6505 error ("too many initializers for %qT", type);
6506 return error_mark_node;
6507 }
6508
6509 if (CONSTRUCTOR_IS_DIRECT_INIT (init)
6510 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6511 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true;
6512 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init)
6513 && BRACE_ENCLOSED_INITIALIZER_P (new_init))
6514 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true;
6515
6516 return new_init;
6517 }
6518
6519 /* Verify array initializer. Returns true if errors have been reported. */
6520
6521 bool
6522 check_array_initializer (tree decl, tree type, tree init)
6523 {
6524 tree element_type = TREE_TYPE (type);
6525
6526 /* The array type itself need not be complete, because the
6527 initializer may tell us how many elements are in the array.
6528 But, the elements of the array must be complete. */
6529 if (!COMPLETE_TYPE_P (complete_type (element_type)))
6530 {
6531 if (decl)
6532 error_at (DECL_SOURCE_LOCATION (decl),
6533 "elements of array %q#D have incomplete type", decl);
6534 else
6535 error ("elements of array %q#T have incomplete type", type);
6536 return true;
6537 }
6538
6539 location_t loc = (decl ? location_of (decl) : input_location);
6540 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type))
6541 return true;
6542
6543 /* A compound literal can't have variable size. */
6544 if (init && !decl
6545 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
6546 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
6547 {
6548 error ("variable-sized compound literal");
6549 return true;
6550 }
6551 return false;
6552 }
6553
6554 /* Subroutine of check_initializer; args are passed down from that function.
6555 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6556
6557 static tree
6558 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6559
6560 {
6561 gcc_assert (stmts_are_full_exprs_p ());
6562 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6563 }
6564
6565 /* Verify INIT (the initializer for DECL), and record the
6566 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6567 grok_reference_init.
6568
6569 If the return value is non-NULL, it is an expression that must be
6570 evaluated dynamically to initialize DECL. */
6571
6572 static tree
6573 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6574 {
6575 tree type;
6576 tree init_code = NULL;
6577 tree core_type;
6578
6579 /* Things that are going to be initialized need to have complete
6580 type. */
6581 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6582
6583 if (DECL_HAS_VALUE_EXPR_P (decl))
6584 {
6585 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6586 it doesn't have storage to be initialized. */
6587 gcc_assert (init == NULL_TREE);
6588 return NULL_TREE;
6589 }
6590
6591 if (type == error_mark_node)
6592 /* We will have already complained. */
6593 return NULL_TREE;
6594
6595 if (TREE_CODE (type) == ARRAY_TYPE)
6596 {
6597 if (check_array_initializer (decl, type, init))
6598 return NULL_TREE;
6599 }
6600 else if (!COMPLETE_TYPE_P (type))
6601 {
6602 error_at (DECL_SOURCE_LOCATION (decl),
6603 "%q#D has incomplete type", decl);
6604 TREE_TYPE (decl) = error_mark_node;
6605 return NULL_TREE;
6606 }
6607 else
6608 /* There is no way to make a variable-sized class type in GNU C++. */
6609 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6610
6611 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6612 {
6613 int init_len = CONSTRUCTOR_NELTS (init);
6614 if (SCALAR_TYPE_P (type))
6615 {
6616 if (init_len == 0)
6617 {
6618 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6619 init = build_zero_init (type, NULL_TREE, false);
6620 }
6621 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6622 {
6623 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6624 "scalar object %qD requires one element in "
6625 "initializer", decl);
6626 TREE_TYPE (decl) = error_mark_node;
6627 return NULL_TREE;
6628 }
6629 }
6630 }
6631
6632 if (TREE_CODE (decl) == CONST_DECL)
6633 {
6634 gcc_assert (!TYPE_REF_P (type));
6635
6636 DECL_INITIAL (decl) = init;
6637
6638 gcc_assert (init != NULL_TREE);
6639 init = NULL_TREE;
6640 }
6641 else if (!init && DECL_REALLY_EXTERN (decl))
6642 ;
6643 else if (init || type_build_ctor_call (type)
6644 || TYPE_REF_P (type))
6645 {
6646 if (TYPE_REF_P (type))
6647 {
6648 init = grok_reference_init (decl, type, init, flags);
6649 flags |= LOOKUP_ALREADY_DIGESTED;
6650 }
6651 else if (!init)
6652 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6653 tf_warning_or_error);
6654 /* Do not reshape constructors of vectors (they don't need to be
6655 reshaped. */
6656 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6657 {
6658 if (is_std_init_list (type))
6659 {
6660 init = perform_implicit_conversion (type, init,
6661 tf_warning_or_error);
6662 flags |= LOOKUP_ALREADY_DIGESTED;
6663 }
6664 else if (TYPE_NON_AGGREGATE_CLASS (type))
6665 {
6666 /* Don't reshape if the class has constructors. */
6667 if (cxx_dialect == cxx98)
6668 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)),
6669 "in C++98 %qD must be initialized by "
6670 "constructor, not by %<{...}%>",
6671 decl);
6672 }
6673 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6674 {
6675 error ("opaque vector types cannot be initialized");
6676 init = error_mark_node;
6677 }
6678 else
6679 {
6680 init = reshape_init (type, init, tf_warning_or_error);
6681 flags |= LOOKUP_NO_NARROWING;
6682 }
6683 }
6684 /* [dcl.init] "Otherwise, if the destination type is an array, the object
6685 is initialized as follows..." So handle things like
6686
6687 int a[](1, 2, 3);
6688
6689 which is permitted in C++20 by P0960. */
6690 else if (TREE_CODE (init) == TREE_LIST
6691 && TREE_TYPE (init) == NULL_TREE
6692 && TREE_CODE (type) == ARRAY_TYPE
6693 && !DECL_DECOMPOSITION_P (decl)
6694 && (cxx_dialect >= cxx2a))
6695 {
6696 /* [dcl.init.string] "An array of ordinary character type [...]
6697 can be initialized by an ordinary string literal [...] by an
6698 appropriately-typed string literal enclosed in braces" only
6699 talks about braces, but GCC has always accepted
6700
6701 char a[]("foobar");
6702
6703 so we continue to do so. */
6704 tree val = TREE_VALUE (init);
6705 if (TREE_CHAIN (init) == NULL_TREE
6706 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
6707 && TREE_CODE (tree_strip_any_location_wrapper (val))
6708 == STRING_CST)
6709 /* If the list has a single element and it's a string literal,
6710 then it's the initializer for the array as a whole. */
6711 init = val;
6712 else
6713 {
6714 init = build_constructor_from_list (init_list_type_node, init);
6715 CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
6716 CONSTRUCTOR_IS_PAREN_INIT (init) = true;
6717 }
6718 }
6719 else if (TREE_CODE (init) == TREE_LIST
6720 && TREE_TYPE (init) != unknown_type_node
6721 && !MAYBE_CLASS_TYPE_P (type))
6722 {
6723 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6724
6725 /* We get here with code like `int a (2);' */
6726 init = build_x_compound_expr_from_list (init, ELK_INIT,
6727 tf_warning_or_error);
6728 }
6729
6730 /* If DECL has an array type without a specific bound, deduce the
6731 array size from the initializer. */
6732 maybe_deduce_size_from_array_init (decl, init);
6733 type = TREE_TYPE (decl);
6734 if (type == error_mark_node)
6735 return NULL_TREE;
6736
6737 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6738 && !(flags & LOOKUP_ALREADY_DIGESTED)
6739 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6740 && CP_AGGREGATE_TYPE_P (type)
6741 && (CLASS_TYPE_P (type)
6742 || !TYPE_NEEDS_CONSTRUCTING (type)
6743 || type_has_extended_temps (type))))
6744 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE))
6745 {
6746 init_code = build_aggr_init_full_exprs (decl, init, flags);
6747
6748 /* A constructor call is a non-trivial initializer even if
6749 it isn't explicitly written. */
6750 if (TREE_SIDE_EFFECTS (init_code))
6751 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6752
6753 /* If this is a constexpr initializer, expand_default_init will
6754 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6755 case, pull the initializer back out and pass it down into
6756 store_init_value. */
6757 while (TREE_CODE (init_code) == EXPR_STMT
6758 || TREE_CODE (init_code) == CONVERT_EXPR)
6759 init_code = TREE_OPERAND (init_code, 0);
6760 if (TREE_CODE (init_code) == INIT_EXPR)
6761 {
6762 /* In C++20, the call to build_aggr_init could have created
6763 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
6764 A(1, 2). */
6765 init = TREE_OPERAND (init_code, 1);
6766 init_code = NULL_TREE;
6767 /* Don't call digest_init; it's unnecessary and will complain
6768 about aggregate initialization of non-aggregate classes. */
6769 flags |= LOOKUP_ALREADY_DIGESTED;
6770 }
6771 else if (DECL_DECLARED_CONSTEXPR_P (decl)
6772 || (flags & LOOKUP_CONSTINIT))
6773 {
6774 /* Declared constexpr or constinit, but no suitable initializer;
6775 massage init appropriately so we can pass it into
6776 store_init_value for the error. */
6777 if (CLASS_TYPE_P (type)
6778 && (!init || TREE_CODE (init) == TREE_LIST))
6779 {
6780 init = build_functional_cast (input_location, type,
6781 init, tf_none);
6782 if (TREE_CODE (init) == TARGET_EXPR)
6783 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6784 }
6785 init_code = NULL_TREE;
6786 }
6787 else
6788 init = NULL_TREE;
6789 }
6790
6791 if (init && TREE_CODE (init) != TREE_VEC)
6792 {
6793 /* In aggregate initialization of a variable, each element
6794 initialization is a full-expression because there is no
6795 enclosing expression. */
6796 gcc_assert (stmts_are_full_exprs_p ());
6797
6798 init_code = store_init_value (decl, init, cleanups, flags);
6799
6800 if (DECL_INITIAL (decl)
6801 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
6802 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))))
6803 {
6804 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value;
6805 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE
6806 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE)
6807 cp_complete_array_type (&TREE_TYPE (elt), elt, false);
6808 }
6809
6810 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6811 && DECL_INITIAL (decl)
6812 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6813 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6814 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl),
6815 DECL_SOURCE_LOCATION (decl)),
6816 0, "array %qD initialized by parenthesized "
6817 "string literal %qE",
6818 decl, DECL_INITIAL (decl));
6819 init = NULL_TREE;
6820 }
6821 }
6822 else
6823 {
6824 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6825 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6826 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6827 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6828 /*complain=*/true);
6829
6830 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false,
6831 tf_warning_or_error);
6832 }
6833
6834 if (init && init != error_mark_node)
6835 init_code = build2 (INIT_EXPR, type, decl, init);
6836
6837 if (init_code)
6838 {
6839 /* We might have set these in cp_finish_decl. */
6840 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6841 TREE_CONSTANT (decl) = false;
6842 }
6843
6844 if (init_code
6845 && DECL_IN_AGGR_P (decl)
6846 && DECL_INITIALIZED_IN_CLASS_P (decl))
6847 {
6848 static int explained = 0;
6849
6850 if (cxx_dialect < cxx11)
6851 error ("initializer invalid for static member with constructor");
6852 else if (cxx_dialect < cxx17)
6853 error ("non-constant in-class initialization invalid for static "
6854 "member %qD", decl);
6855 else
6856 error ("non-constant in-class initialization invalid for non-inline "
6857 "static member %qD", decl);
6858 if (!explained)
6859 {
6860 inform (input_location,
6861 "(an out of class initialization is required)");
6862 explained = 1;
6863 }
6864 return NULL_TREE;
6865 }
6866
6867 return init_code;
6868 }
6869
6870 /* If DECL is not a local variable, give it RTL. */
6871
6872 static void
6873 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6874 {
6875 int toplev = toplevel_bindings_p ();
6876 int defer_p;
6877
6878 /* Set the DECL_ASSEMBLER_NAME for the object. */
6879 if (asmspec)
6880 {
6881 /* The `register' keyword, when used together with an
6882 asm-specification, indicates that the variable should be
6883 placed in a particular register. */
6884 if (VAR_P (decl) && DECL_REGISTER (decl))
6885 {
6886 set_user_assembler_name (decl, asmspec);
6887 DECL_HARD_REGISTER (decl) = 1;
6888 }
6889 else
6890 {
6891 if (TREE_CODE (decl) == FUNCTION_DECL
6892 && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
6893 set_builtin_user_assembler_name (decl, asmspec);
6894 set_user_assembler_name (decl, asmspec);
6895 }
6896 }
6897
6898 /* Handle non-variables up front. */
6899 if (!VAR_P (decl))
6900 {
6901 rest_of_decl_compilation (decl, toplev, at_eof);
6902 return;
6903 }
6904
6905 /* If we see a class member here, it should be a static data
6906 member. */
6907 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6908 {
6909 gcc_assert (TREE_STATIC (decl));
6910 /* An in-class declaration of a static data member should be
6911 external; it is only a declaration, and not a definition. */
6912 if (init == NULL_TREE)
6913 gcc_assert (DECL_EXTERNAL (decl)
6914 || !TREE_PUBLIC (decl));
6915 }
6916
6917 /* We don't create any RTL for local variables. */
6918 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6919 return;
6920
6921 /* We defer emission of local statics until the corresponding
6922 DECL_EXPR is expanded. But with constexpr its function might never
6923 be expanded, so go ahead and tell cgraph about the variable now. */
6924 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6925 && !var_in_maybe_constexpr_fn (decl))
6926 || DECL_VIRTUAL_P (decl));
6927
6928 /* Defer template instantiations. */
6929 if (DECL_LANG_SPECIFIC (decl)
6930 && DECL_IMPLICIT_INSTANTIATION (decl))
6931 defer_p = 1;
6932
6933 /* If we're not deferring, go ahead and assemble the variable. */
6934 if (!defer_p)
6935 rest_of_decl_compilation (decl, toplev, at_eof);
6936 }
6937
6938 /* walk_tree helper for wrap_temporary_cleanups, below. */
6939
6940 static tree
6941 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6942 {
6943 /* Stop at types or full-expression boundaries. */
6944 if (TYPE_P (*stmt_p)
6945 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6946 {
6947 *walk_subtrees = 0;
6948 return NULL_TREE;
6949 }
6950
6951 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6952 {
6953 tree guard = (tree)data;
6954 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6955
6956 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6957 /* Tell honor_protect_cleanup_actions to handle this as a separate
6958 cleanup. */
6959 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6960
6961 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6962 }
6963
6964 return NULL_TREE;
6965 }
6966
6967 /* We're initializing a local variable which has a cleanup GUARD. If there
6968 are any temporaries used in the initializer INIT of this variable, we
6969 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6970 variable will be cleaned up properly if one of them throws.
6971
6972 Unfortunately, there's no way to express this properly in terms of
6973 nesting, as the regions for the temporaries overlap the region for the
6974 variable itself; if there are two temporaries, the variable needs to be
6975 the first thing destroyed if either of them throws. However, we only
6976 want to run the variable's cleanup if it actually got constructed. So
6977 we need to guard the temporary cleanups with the variable's cleanup if
6978 they are run on the normal path, but not if they are run on the
6979 exceptional path. We implement this by telling
6980 honor_protect_cleanup_actions to strip the variable cleanup from the
6981 exceptional path. */
6982
6983 static void
6984 wrap_temporary_cleanups (tree init, tree guard)
6985 {
6986 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6987 }
6988
6989 /* Generate code to initialize DECL (a local variable). */
6990
6991 static void
6992 initialize_local_var (tree decl, tree init)
6993 {
6994 tree type = TREE_TYPE (decl);
6995 tree cleanup;
6996 int already_used;
6997
6998 gcc_assert (VAR_P (decl)
6999 || TREE_CODE (decl) == RESULT_DECL);
7000 gcc_assert (!TREE_STATIC (decl));
7001
7002 if (DECL_SIZE (decl) == NULL_TREE)
7003 {
7004 /* If we used it already as memory, it must stay in memory. */
7005 DECL_INITIAL (decl) = NULL_TREE;
7006 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7007 return;
7008 }
7009
7010 if (type == error_mark_node)
7011 return;
7012
7013 /* Compute and store the initial value. */
7014 already_used = TREE_USED (decl) || TREE_USED (type);
7015 if (TREE_USED (type))
7016 DECL_READ_P (decl) = 1;
7017
7018 /* Generate a cleanup, if necessary. */
7019 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7020
7021 /* Perform the initialization. */
7022 if (init)
7023 {
7024 tree rinit = (TREE_CODE (init) == INIT_EXPR
7025 ? TREE_OPERAND (init, 1) : NULL_TREE);
7026 if (rinit && !TREE_SIDE_EFFECTS (rinit))
7027 {
7028 /* Stick simple initializers in DECL_INITIAL so that
7029 -Wno-init-self works (c++/34772). */
7030 gcc_assert (TREE_OPERAND (init, 0) == decl);
7031 DECL_INITIAL (decl) = rinit;
7032
7033 if (warn_init_self && TYPE_REF_P (type))
7034 {
7035 STRIP_NOPS (rinit);
7036 if (rinit == decl)
7037 warning_at (DECL_SOURCE_LOCATION (decl),
7038 OPT_Winit_self,
7039 "reference %qD is initialized with itself", decl);
7040 }
7041 }
7042 else
7043 {
7044 int saved_stmts_are_full_exprs_p;
7045
7046 /* If we're only initializing a single object, guard the
7047 destructors of any temporaries used in its initializer with
7048 its destructor. This isn't right for arrays because each
7049 element initialization is a full-expression. */
7050 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
7051 wrap_temporary_cleanups (init, cleanup);
7052
7053 gcc_assert (building_stmt_list_p ());
7054 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7055 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7056 finish_expr_stmt (init);
7057 current_stmt_tree ()->stmts_are_full_exprs_p =
7058 saved_stmts_are_full_exprs_p;
7059 }
7060 }
7061
7062 /* Set this to 0 so we can tell whether an aggregate which was
7063 initialized was ever used. Don't do this if it has a
7064 destructor, so we don't complain about the 'resource
7065 allocation is initialization' idiom. Now set
7066 attribute((unused)) on types so decls of that type will be
7067 marked used. (see TREE_USED, above.) */
7068 if (TYPE_NEEDS_CONSTRUCTING (type)
7069 && ! already_used
7070 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7071 && DECL_NAME (decl))
7072 TREE_USED (decl) = 0;
7073 else if (already_used)
7074 TREE_USED (decl) = 1;
7075
7076 if (cleanup)
7077 finish_decl_cleanup (decl, cleanup);
7078 }
7079
7080 /* DECL is a VAR_DECL for a compiler-generated variable with static
7081 storage duration (like a virtual table) whose initializer is a
7082 compile-time constant. Initialize the variable and provide it to the
7083 back end. */
7084
7085 void
7086 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
7087 {
7088 tree init;
7089 gcc_assert (DECL_ARTIFICIAL (decl));
7090 init = build_constructor (TREE_TYPE (decl), v);
7091 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
7092 DECL_INITIAL (decl) = init;
7093 DECL_INITIALIZED_P (decl) = 1;
7094 /* Mark the decl as constexpr so that we can access its content
7095 at compile time. */
7096 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
7097 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7098 determine_visibility (decl);
7099 layout_var_decl (decl);
7100 maybe_commonize_var (decl);
7101 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
7102 }
7103
7104 /* INIT is the initializer for a variable, as represented by the
7105 parser. Returns true iff INIT is value-dependent. */
7106
7107 static bool
7108 value_dependent_init_p (tree init)
7109 {
7110 if (TREE_CODE (init) == TREE_LIST)
7111 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
7112 return any_value_dependent_elements_p (init);
7113 else if (TREE_CODE (init) == CONSTRUCTOR)
7114 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
7115 {
7116 if (dependent_type_p (TREE_TYPE (init)))
7117 return true;
7118
7119 vec<constructor_elt, va_gc> *elts;
7120 size_t nelts;
7121 size_t i;
7122
7123 elts = CONSTRUCTOR_ELTS (init);
7124 nelts = vec_safe_length (elts);
7125 for (i = 0; i < nelts; ++i)
7126 if (value_dependent_init_p ((*elts)[i].value))
7127 return true;
7128 }
7129 else
7130 /* It must be a simple expression, e.g., int i = 3; */
7131 return value_dependent_expression_p (init);
7132
7133 return false;
7134 }
7135
7136 // Returns true if a DECL is VAR_DECL with the concept specifier.
7137 static inline bool
7138 is_concept_var (tree decl)
7139 {
7140 return (VAR_P (decl)
7141 // Not all variables have DECL_LANG_SPECIFIC.
7142 && DECL_LANG_SPECIFIC (decl)
7143 && DECL_DECLARED_CONCEPT_P (decl));
7144 }
7145
7146 /* A helper function to be called via walk_tree. If any label exists
7147 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
7148
7149 static tree
7150 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
7151 {
7152 if (TYPE_P (*tp))
7153 *walk_subtrees = 0;
7154 if (TREE_CODE (*tp) == LABEL_DECL)
7155 cfun->has_forced_label_in_static = 1;
7156 return NULL_TREE;
7157 }
7158
7159 /* Return true if DECL has either a trivial destructor, or for C++2A
7160 is constexpr and has a constexpr destructor. */
7161
7162 static bool
7163 decl_maybe_constant_destruction (tree decl, tree type)
7164 {
7165 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7166 || (cxx_dialect >= cxx2a
7167 && VAR_P (decl)
7168 && DECL_DECLARED_CONSTEXPR_P (decl)
7169 && type_has_constexpr_destructor (strip_array_types (type))));
7170 }
7171
7172 static tree declare_simd_adjust_this (tree *, int *, void *);
7173
7174 /* Helper function of omp_declare_variant_finalize. Finalize one
7175 "omp declare variant base" attribute. Return true if it should be
7176 removed. */
7177
7178 static bool
7179 omp_declare_variant_finalize_one (tree decl, tree attr)
7180 {
7181 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7182 {
7183 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this,
7184 DECL_ARGUMENTS (decl), NULL);
7185 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this,
7186 DECL_ARGUMENTS (decl), NULL);
7187 }
7188
7189 tree ctx = TREE_VALUE (TREE_VALUE (attr));
7190 tree simd = omp_get_context_selector (ctx, "construct", "simd");
7191 if (simd)
7192 {
7193 TREE_VALUE (simd)
7194 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
7195 TREE_VALUE (simd));
7196 /* FIXME, adjusting simd args unimplemented. */
7197 return true;
7198 }
7199
7200 tree chain = TREE_CHAIN (TREE_VALUE (attr));
7201 location_t varid_loc
7202 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain)));
7203 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
7204 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain));
7205 tree variant = TREE_PURPOSE (TREE_VALUE (attr));
7206
7207 location_t save_loc = input_location;
7208 input_location = varid_loc;
7209
7210 releasing_vec args;
7211 tree parm = DECL_ARGUMENTS (decl);
7212 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
7213 parm = DECL_CHAIN (parm);
7214 for (; parm; parm = DECL_CHAIN (parm))
7215 if (type_dependent_expression_p (parm))
7216 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL));
7217 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm)))
7218 vec_safe_push (args, build_local_temp (TREE_TYPE (parm)));
7219 else
7220 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm)));
7221
7222 bool koenig_p = false;
7223 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID)
7224 {
7225 if (identifier_p (variant)
7226 /* In C++2A, we may need to perform ADL for a template
7227 name. */
7228 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR
7229 && identifier_p (TREE_OPERAND (variant, 0))))
7230 {
7231 if (!args->is_empty ())
7232 {
7233 koenig_p = true;
7234 if (!any_type_dependent_arguments_p (args))
7235 variant = perform_koenig_lookup (variant, args,
7236 tf_warning_or_error);
7237 }
7238 else
7239 variant = unqualified_fn_lookup_error (variant);
7240 }
7241 else if (!args->is_empty () && is_overloaded_fn (variant))
7242 {
7243 tree fn = get_first_fn (variant);
7244 fn = STRIP_TEMPLATE (fn);
7245 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7246 || DECL_FUNCTION_MEMBER_P (fn)
7247 || DECL_LOCAL_FUNCTION_P (fn)))
7248 {
7249 koenig_p = true;
7250 if (!any_type_dependent_arguments_p (args))
7251 variant = perform_koenig_lookup (variant, args,
7252 tf_warning_or_error);
7253 }
7254 }
7255 }
7256
7257 if (idk == CP_ID_KIND_QUALIFIED)
7258 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true,
7259 koenig_p, tf_warning_or_error);
7260 else
7261 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false,
7262 koenig_p, tf_warning_or_error);
7263 if (variant == error_mark_node && !processing_template_decl)
7264 return true;
7265
7266 variant = cp_get_callee (variant);
7267 if (variant)
7268 {
7269 if (TREE_CODE (variant) == FUNCTION_DECL)
7270 ;
7271 else if (TREE_TYPE (variant) && INDIRECT_TYPE_P (TREE_TYPE (variant)))
7272 variant = cp_get_fndecl_from_callee (variant, false);
7273 else
7274 variant = NULL_TREE;
7275 }
7276
7277 input_location = save_loc;
7278
7279 if (variant)
7280 {
7281 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant));
7282 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0))
7283 {
7284 error_at (varid_loc, "variant %qD and base %qD have incompatible "
7285 "types", variant, decl);
7286 return true;
7287 }
7288 if (fndecl_built_in_p (variant)
7289 && (strncmp (varname, "__builtin_", strlen ("__builtin_")) == 0
7290 || strncmp (varname, "__sync_", strlen ("__sync_")) == 0
7291 || strncmp (varname, "__atomic_", strlen ("__atomic_")) == 0))
7292 {
7293 error_at (varid_loc, "variant %qD is a built-in", variant);
7294 return true;
7295 }
7296 else
7297 {
7298 tree construct = omp_get_context_selector (ctx, "construct", NULL);
7299 c_omp_mark_declare_variant (match_loc, variant, construct);
7300 if (!omp_context_selector_matches (ctx))
7301 return true;
7302 TREE_PURPOSE (TREE_VALUE (attr)) = variant;
7303 }
7304 }
7305 else if (!processing_template_decl)
7306 {
7307 error_at (varid_loc, "could not find variant declaration");
7308 return true;
7309 }
7310
7311 return false;
7312 }
7313
7314 /* Helper function, finish up "omp declare variant base" attribute
7315 now that there is a DECL. ATTR is the first "omp declare variant base"
7316 attribute. */
7317
7318 void
7319 omp_declare_variant_finalize (tree decl, tree attr)
7320 {
7321 size_t attr_len = strlen ("omp declare variant base");
7322 tree *list = &DECL_ATTRIBUTES (decl);
7323 bool remove_all = false;
7324 location_t match_loc = DECL_SOURCE_LOCATION (decl);
7325 if (TREE_CHAIN (TREE_VALUE (attr))
7326 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))
7327 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))))
7328 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))));
7329 if (DECL_CONSTRUCTOR_P (decl))
7330 {
7331 error_at (match_loc, "%<declare variant%> on constructor %qD", decl);
7332 remove_all = true;
7333 }
7334 else if (DECL_DESTRUCTOR_P (decl))
7335 {
7336 error_at (match_loc, "%<declare variant%> on destructor %qD", decl);
7337 remove_all = true;
7338 }
7339 else if (DECL_DEFAULTED_FN (decl))
7340 {
7341 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl);
7342 remove_all = true;
7343 }
7344 else if (DECL_DELETED_FN (decl))
7345 {
7346 error_at (match_loc, "%<declare variant%> on deleted %qD", decl);
7347 remove_all = true;
7348 }
7349 else if (DECL_VIRTUAL_P (decl))
7350 {
7351 error_at (match_loc, "%<declare variant%> on virtual %qD", decl);
7352 remove_all = true;
7353 }
7354 /* This loop is like private_lookup_attribute, except that it works
7355 with tree * rather than tree, as we might want to remove the
7356 attributes that are diagnosed as errorneous. */
7357 while (*list)
7358 {
7359 tree attr = get_attribute_name (*list);
7360 size_t ident_len = IDENTIFIER_LENGTH (attr);
7361 if (cmp_attribs ("omp declare variant base", attr_len,
7362 IDENTIFIER_POINTER (attr), ident_len))
7363 {
7364 if (remove_all || omp_declare_variant_finalize_one (decl, *list))
7365 {
7366 *list = TREE_CHAIN (*list);
7367 continue;
7368 }
7369 }
7370 list = &TREE_CHAIN (*list);
7371 }
7372 }
7373
7374 /* Finish processing of a declaration;
7375 install its line number and initial value.
7376 If the length of an array type is not known before,
7377 it must be determined now, from the initial value, or it is an error.
7378
7379 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
7380 true, then INIT is an integral constant expression.
7381
7382 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7383 if the (init) syntax was used. */
7384
7385 void
7386 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
7387 tree asmspec_tree, int flags)
7388 {
7389 tree type;
7390 vec<tree, va_gc> *cleanups = NULL;
7391 const char *asmspec = NULL;
7392 int was_readonly = 0;
7393 bool var_definition_p = false;
7394 tree auto_node;
7395
7396 if (decl == error_mark_node)
7397 return;
7398 else if (! decl)
7399 {
7400 if (init)
7401 error ("assignment (not initialization) in declaration");
7402 return;
7403 }
7404
7405 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
7406 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
7407 gcc_assert (TREE_CODE (decl) != PARM_DECL);
7408
7409 type = TREE_TYPE (decl);
7410 if (type == error_mark_node)
7411 return;
7412
7413 /* Warn about register storage specifiers except when in GNU global
7414 or local register variable extension. */
7415 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE)
7416 {
7417 if (cxx_dialect >= cxx17)
7418 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7419 "ISO C++17 does not allow %<register%> storage "
7420 "class specifier");
7421 else
7422 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
7423 "%<register%> storage class specifier used");
7424 }
7425
7426 /* If a name was specified, get the string. */
7427 if (at_namespace_scope_p ())
7428 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7429 if (asmspec_tree && asmspec_tree != error_mark_node)
7430 asmspec = TREE_STRING_POINTER (asmspec_tree);
7431
7432 if (current_class_type
7433 && CP_DECL_CONTEXT (decl) == current_class_type
7434 && TYPE_BEING_DEFINED (current_class_type)
7435 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
7436 && (DECL_INITIAL (decl) || init))
7437 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
7438
7439 /* Do auto deduction unless decl is a function or an uninstantiated
7440 template specialization. */
7441 if (TREE_CODE (decl) != FUNCTION_DECL
7442 && !(init == NULL_TREE
7443 && DECL_LANG_SPECIFIC (decl)
7444 && DECL_TEMPLATE_INSTANTIATION (decl)
7445 && !DECL_TEMPLATE_INSTANTIATED (decl))
7446 && (auto_node = type_uses_auto (type)))
7447 {
7448 tree d_init;
7449 if (init == NULL_TREE)
7450 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node));
7451 d_init = init;
7452 if (d_init)
7453 {
7454 if (TREE_CODE (d_init) == TREE_LIST
7455 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node))
7456 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
7457 tf_warning_or_error);
7458 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
7459 }
7460 enum auto_deduction_context adc = adc_variable_type;
7461 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
7462 adc = adc_decomp_type;
7463 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node,
7464 tf_warning_or_error, adc,
7465 NULL_TREE, flags);
7466 if (type == error_mark_node)
7467 return;
7468 if (TREE_CODE (type) == FUNCTION_TYPE)
7469 {
7470 error ("initializer for %<decltype(auto) %D%> has function type; "
7471 "did you forget the %<()%>?", decl);
7472 TREE_TYPE (decl) = error_mark_node;
7473 return;
7474 }
7475 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
7476 }
7477
7478 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node)
7479 {
7480 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
7481 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl))
7482 {
7483 init = NULL_TREE;
7484 DECL_EXTERNAL (decl) = 1;
7485 }
7486 }
7487
7488 if (VAR_P (decl)
7489 && DECL_CLASS_SCOPE_P (decl)
7490 && verify_type_context (DECL_SOURCE_LOCATION (decl),
7491 TCTX_STATIC_STORAGE, type)
7492 && DECL_INITIALIZED_IN_CLASS_P (decl))
7493 check_static_variable_definition (decl, type);
7494
7495 if (init && TREE_CODE (decl) == FUNCTION_DECL)
7496 {
7497 tree clone;
7498 if (init == ridpointers[(int)RID_DELETE])
7499 {
7500 /* FIXME check this is 1st decl. */
7501 DECL_DELETED_FN (decl) = 1;
7502 DECL_DECLARED_INLINE_P (decl) = 1;
7503 DECL_INITIAL (decl) = error_mark_node;
7504 FOR_EACH_CLONE (clone, decl)
7505 {
7506 DECL_DELETED_FN (clone) = 1;
7507 DECL_DECLARED_INLINE_P (clone) = 1;
7508 DECL_INITIAL (clone) = error_mark_node;
7509 }
7510 init = NULL_TREE;
7511 }
7512 else if (init == ridpointers[(int)RID_DEFAULT])
7513 {
7514 if (defaultable_fn_check (decl))
7515 DECL_DEFAULTED_FN (decl) = 1;
7516 else
7517 DECL_INITIAL (decl) = NULL_TREE;
7518 }
7519 }
7520
7521 if (init && VAR_P (decl))
7522 {
7523 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
7524 /* If DECL is a reference, then we want to know whether init is a
7525 reference constant; init_const_expr_p as passed tells us whether
7526 it's an rvalue constant. */
7527 if (TYPE_REF_P (type))
7528 init_const_expr_p = potential_constant_expression (init);
7529 if (init_const_expr_p)
7530 {
7531 /* Set these flags now for templates. We'll update the flags in
7532 store_init_value for instantiations. */
7533 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
7534 if (decl_maybe_constant_var_p (decl)
7535 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
7536 && !TYPE_REF_P (type))
7537 TREE_CONSTANT (decl) = 1;
7538 }
7539 }
7540
7541 if (flag_openmp
7542 && TREE_CODE (decl) == FUNCTION_DECL
7543 /* #pragma omp declare variant on methods handled in finish_struct
7544 instead. */
7545 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
7546 || COMPLETE_TYPE_P (DECL_CONTEXT (decl))))
7547 if (tree attr = lookup_attribute ("omp declare variant base",
7548 DECL_ATTRIBUTES (decl)))
7549 omp_declare_variant_finalize (decl, attr);
7550
7551 if (processing_template_decl)
7552 {
7553 bool type_dependent_p;
7554
7555 /* Add this declaration to the statement-tree. */
7556 if (at_function_scope_p ())
7557 add_decl_expr (decl);
7558
7559 type_dependent_p = dependent_type_p (type);
7560
7561 if (check_for_bare_parameter_packs (init))
7562 {
7563 init = NULL_TREE;
7564 DECL_INITIAL (decl) = NULL_TREE;
7565 }
7566
7567 /* Handle `constinit' on variable templates. */
7568 if (flags & LOOKUP_CONSTINIT)
7569 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)) = true;
7570
7571 /* Generally, initializers in templates are expanded when the
7572 template is instantiated. But, if DECL is a variable constant
7573 then it can be used in future constant expressions, so its value
7574 must be available. */
7575
7576 bool dep_init = false;
7577
7578 if (!VAR_P (decl) || type_dependent_p)
7579 /* We can't do anything if the decl has dependent type. */;
7580 else if (!init && is_concept_var (decl))
7581 {
7582 error ("variable concept has no initializer");
7583 init = boolean_true_node;
7584 }
7585 else if (init
7586 && init_const_expr_p
7587 && !TYPE_REF_P (type)
7588 && decl_maybe_constant_var_p (decl)
7589 && !(dep_init = value_dependent_init_p (init)))
7590 {
7591 /* This variable seems to be a non-dependent constant, so process
7592 its initializer. If check_initializer returns non-null the
7593 initialization wasn't constant after all. */
7594 tree init_code;
7595 cleanups = make_tree_vector ();
7596 init_code = check_initializer (decl, init, flags, &cleanups);
7597 if (init_code == NULL_TREE)
7598 init = NULL_TREE;
7599 release_tree_vector (cleanups);
7600 }
7601 else
7602 {
7603 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl));
7604 /* Deduce array size even if the initializer is dependent. */
7605 maybe_deduce_size_from_array_init (decl, init);
7606 /* And complain about multiple initializers. */
7607 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
7608 && !MAYBE_CLASS_TYPE_P (type))
7609 init = build_x_compound_expr_from_list (init, ELK_INIT,
7610 tf_warning_or_error);
7611 }
7612
7613 if (init)
7614 DECL_INITIAL (decl) = init;
7615
7616 if (dep_init)
7617 {
7618 retrofit_lang_decl (decl);
7619 SET_DECL_DEPENDENT_INIT_P (decl, true);
7620 }
7621 return;
7622 }
7623
7624 /* Just store non-static data member initializers for later. */
7625 if (init && TREE_CODE (decl) == FIELD_DECL)
7626 DECL_INITIAL (decl) = init;
7627
7628 /* Take care of TYPE_DECLs up front. */
7629 if (TREE_CODE (decl) == TYPE_DECL)
7630 {
7631 if (type != error_mark_node
7632 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
7633 {
7634 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7635 warning (0, "shadowing previous type declaration of %q#D", decl);
7636 set_identifier_type_value (DECL_NAME (decl), decl);
7637 }
7638
7639 /* If we have installed this as the canonical typedef for this
7640 type, and that type has not been defined yet, delay emitting
7641 the debug information for it, as we will emit it later. */
7642 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7643 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
7644 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7645
7646 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
7647 at_eof);
7648 return;
7649 }
7650
7651 /* A reference will be modified here, as it is initialized. */
7652 if (! DECL_EXTERNAL (decl)
7653 && TREE_READONLY (decl)
7654 && TYPE_REF_P (type))
7655 {
7656 was_readonly = 1;
7657 TREE_READONLY (decl) = 0;
7658 }
7659
7660 /* This needs to happen before extend_ref_init_temps. */
7661 if (VAR_OR_FUNCTION_DECL_P (decl))
7662 {
7663 if (VAR_P (decl))
7664 maybe_commonize_var (decl);
7665 determine_visibility (decl);
7666 }
7667
7668 if (VAR_P (decl))
7669 {
7670 duration_kind dk = decl_storage_duration (decl);
7671 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7672 only to a declaration of a variable with static or thread storage
7673 duration." */
7674 if ((flags & LOOKUP_CONSTINIT)
7675 && !(dk == dk_thread || dk == dk_static))
7676 {
7677 error_at (DECL_SOURCE_LOCATION (decl),
7678 "%<constinit%> can only be applied to a variable with "
7679 "static or thread storage duration");
7680 return;
7681 }
7682
7683 /* If this is a local variable that will need a mangled name,
7684 register it now. We must do this before processing the
7685 initializer for the variable, since the initialization might
7686 require a guard variable, and since the mangled name of the
7687 guard variable will depend on the mangled name of this
7688 variable. */
7689 if (DECL_FUNCTION_SCOPE_P (decl)
7690 && TREE_STATIC (decl)
7691 && !DECL_ARTIFICIAL (decl))
7692 {
7693 /* The variable holding an anonymous union will have had its
7694 discriminator set in finish_anon_union, after which it's
7695 NAME will have been cleared. */
7696 if (DECL_NAME (decl))
7697 determine_local_discriminator (decl);
7698 /* Normally has_forced_label_in_static is set during GIMPLE
7699 lowering, but [cd]tors are never actually compiled directly.
7700 We need to set this early so we can deal with the label
7701 address extension. */
7702 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7703 || DECL_DESTRUCTOR_P (current_function_decl))
7704 && init)
7705 {
7706 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7707 add_local_decl (cfun, decl);
7708 }
7709 /* And make sure it's in the symbol table for
7710 c_parse_final_cleanups to find. */
7711 varpool_node::get_create (decl);
7712 }
7713
7714 /* Convert the initializer to the type of DECL, if we have not
7715 already initialized DECL. */
7716 if (!DECL_INITIALIZED_P (decl)
7717 /* If !DECL_EXTERNAL then DECL is being defined. In the
7718 case of a static data member initialized inside the
7719 class-specifier, there can be an initializer even if DECL
7720 is *not* defined. */
7721 && (!DECL_EXTERNAL (decl) || init))
7722 {
7723 cleanups = make_tree_vector ();
7724 init = check_initializer (decl, init, flags, &cleanups);
7725
7726 /* Handle:
7727
7728 [dcl.init]
7729
7730 The memory occupied by any object of static storage
7731 duration is zero-initialized at program startup before
7732 any other initialization takes place.
7733
7734 We cannot create an appropriate initializer until after
7735 the type of DECL is finalized. If DECL_INITIAL is set,
7736 then the DECL is statically initialized, and any
7737 necessary zero-initialization has already been performed. */
7738 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7739 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7740 /*nelts=*/NULL_TREE,
7741 /*static_storage_p=*/true);
7742 /* Remember that the initialization for this variable has
7743 taken place. */
7744 DECL_INITIALIZED_P (decl) = 1;
7745 /* This declaration is the definition of this variable,
7746 unless we are initializing a static data member within
7747 the class specifier. */
7748 if (!DECL_EXTERNAL (decl))
7749 var_definition_p = true;
7750 }
7751 /* If the variable has an array type, lay out the type, even if
7752 there is no initializer. It is valid to index through the
7753 array, and we must get TYPE_ALIGN set correctly on the array
7754 type. */
7755 else if (TREE_CODE (type) == ARRAY_TYPE)
7756 layout_type (type);
7757
7758 if (TREE_STATIC (decl)
7759 && !at_function_scope_p ()
7760 && current_function_decl == NULL)
7761 /* So decl is a global variable or a static member of a
7762 non local class. Record the types it uses
7763 so that we can decide later to emit debug info for them. */
7764 record_types_used_by_current_var_decl (decl);
7765 }
7766
7767 /* Add this declaration to the statement-tree. This needs to happen
7768 after the call to check_initializer so that the DECL_EXPR for a
7769 reference temp is added before the DECL_EXPR for the reference itself. */
7770 if (DECL_FUNCTION_SCOPE_P (decl))
7771 {
7772 /* If we're building a variable sized type, and we might be
7773 reachable other than via the top of the current binding
7774 level, then create a new BIND_EXPR so that we deallocate
7775 the object at the right time. */
7776 if (VAR_P (decl)
7777 && DECL_SIZE (decl)
7778 && !TREE_CONSTANT (DECL_SIZE (decl))
7779 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7780 {
7781 tree bind;
7782 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7783 TREE_SIDE_EFFECTS (bind) = 1;
7784 add_stmt (bind);
7785 BIND_EXPR_BODY (bind) = push_stmt_list ();
7786 }
7787 add_decl_expr (decl);
7788 }
7789
7790 /* Let the middle end know about variables and functions -- but not
7791 static data members in uninstantiated class templates. */
7792 if (VAR_OR_FUNCTION_DECL_P (decl))
7793 {
7794 if (VAR_P (decl))
7795 {
7796 layout_var_decl (decl);
7797 if (!flag_weak)
7798 /* Check again now that we have an initializer. */
7799 maybe_commonize_var (decl);
7800 }
7801
7802 if (var_definition_p && TREE_STATIC (decl))
7803 {
7804 /* If a TREE_READONLY variable needs initialization
7805 at runtime, it is no longer readonly and we need to
7806 avoid MEM_READONLY_P being set on RTL created for it. */
7807 if (init)
7808 {
7809 if (TREE_READONLY (decl))
7810 TREE_READONLY (decl) = 0;
7811 was_readonly = 0;
7812 }
7813 else if (was_readonly)
7814 TREE_READONLY (decl) = 1;
7815
7816 /* Likewise if it needs destruction. */
7817 if (!decl_maybe_constant_destruction (decl, type))
7818 TREE_READONLY (decl) = 0;
7819 }
7820
7821 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7822
7823 /* Check for abstractness of the type. Notice that there is no
7824 need to strip array types here since the check for those types
7825 is already done within create_array_type_for_decl. */
7826 abstract_virtuals_error (decl, type);
7827
7828 if (TREE_TYPE (decl) == error_mark_node)
7829 /* No initialization required. */
7830 ;
7831 else if (TREE_CODE (decl) == FUNCTION_DECL)
7832 {
7833 if (init)
7834 {
7835 if (init == ridpointers[(int)RID_DEFAULT])
7836 {
7837 /* An out-of-class default definition is defined at
7838 the point where it is explicitly defaulted. */
7839 if (DECL_DELETED_FN (decl))
7840 maybe_explain_implicit_delete (decl);
7841 else if (DECL_INITIAL (decl) == error_mark_node)
7842 synthesize_method (decl);
7843 }
7844 else
7845 error_at (cp_expr_loc_or_loc (init,
7846 DECL_SOURCE_LOCATION (decl)),
7847 "function %q#D is initialized like a variable",
7848 decl);
7849 }
7850 /* else no initialization required. */
7851 }
7852 else if (DECL_EXTERNAL (decl)
7853 && ! (DECL_LANG_SPECIFIC (decl)
7854 && DECL_NOT_REALLY_EXTERN (decl)))
7855 {
7856 /* check_initializer will have done any constant initialization. */
7857 }
7858 /* A variable definition. */
7859 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7860 /* Initialize the local variable. */
7861 initialize_local_var (decl, init);
7862
7863 /* If a variable is defined, and then a subsequent
7864 definition with external linkage is encountered, we will
7865 get here twice for the same variable. We want to avoid
7866 calling expand_static_init more than once. For variables
7867 that are not static data members, we can call
7868 expand_static_init only when we actually process the
7869 initializer. It is not legal to redeclare a static data
7870 member, so this issue does not arise in that case. */
7871 else if (var_definition_p && TREE_STATIC (decl))
7872 expand_static_init (decl, init);
7873 }
7874
7875 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7876 reference, insert it in the statement-tree now. */
7877 if (cleanups)
7878 {
7879 unsigned i; tree t;
7880 FOR_EACH_VEC_ELT (*cleanups, i, t)
7881 push_cleanup (decl, t, false);
7882 release_tree_vector (cleanups);
7883 }
7884
7885 if (was_readonly)
7886 TREE_READONLY (decl) = 1;
7887
7888 if (flag_openmp
7889 && VAR_P (decl)
7890 && lookup_attribute ("omp declare target implicit",
7891 DECL_ATTRIBUTES (decl)))
7892 {
7893 DECL_ATTRIBUTES (decl)
7894 = remove_attribute ("omp declare target implicit",
7895 DECL_ATTRIBUTES (decl));
7896 complete_type (TREE_TYPE (decl));
7897 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
7898 {
7899 error ("%q+D in declare target directive does not have mappable"
7900 " type", decl);
7901 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
7902 }
7903 else if (!lookup_attribute ("omp declare target",
7904 DECL_ATTRIBUTES (decl))
7905 && !lookup_attribute ("omp declare target link",
7906 DECL_ATTRIBUTES (decl)))
7907 DECL_ATTRIBUTES (decl)
7908 = tree_cons (get_identifier ("omp declare target"),
7909 NULL_TREE, DECL_ATTRIBUTES (decl));
7910 }
7911
7912 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7913 }
7914
7915 /* For class TYPE return itself or some its bases that contain
7916 any direct non-static data members. Return error_mark_node if an
7917 error has been diagnosed. */
7918
7919 static tree
7920 find_decomp_class_base (location_t loc, tree type, tree ret)
7921 {
7922 bool member_seen = false;
7923 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7924 if (TREE_CODE (field) != FIELD_DECL
7925 || DECL_ARTIFICIAL (field)
7926 || DECL_UNNAMED_BIT_FIELD (field))
7927 continue;
7928 else if (ret)
7929 return type;
7930 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7931 {
7932 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7933 error_at (loc, "cannot decompose class type %qT because it has an "
7934 "anonymous struct member", type);
7935 else
7936 error_at (loc, "cannot decompose class type %qT because it has an "
7937 "anonymous union member", type);
7938 inform (DECL_SOURCE_LOCATION (field), "declared here");
7939 return error_mark_node;
7940 }
7941 else if (!accessible_p (type, field, true))
7942 {
7943 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
7944 field, type);
7945 inform (DECL_SOURCE_LOCATION (field),
7946 TREE_PRIVATE (field)
7947 ? G_("declared private here")
7948 : G_("declared protected here"));
7949 return error_mark_node;
7950 }
7951 else
7952 member_seen = true;
7953
7954 tree base_binfo, binfo;
7955 tree orig_ret = ret;
7956 int i;
7957 if (member_seen)
7958 ret = type;
7959 for (binfo = TYPE_BINFO (type), i = 0;
7960 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7961 {
7962 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7963 if (t == error_mark_node)
7964 return error_mark_node;
7965 if (t != NULL_TREE && t != ret)
7966 {
7967 if (ret == type)
7968 {
7969 error_at (loc, "cannot decompose class type %qT: both it and "
7970 "its base class %qT have non-static data members",
7971 type, t);
7972 return error_mark_node;
7973 }
7974 else if (orig_ret != NULL_TREE)
7975 return t;
7976 else if (ret != NULL_TREE)
7977 {
7978 error_at (loc, "cannot decompose class type %qT: its base "
7979 "classes %qT and %qT have non-static data "
7980 "members", type, ret, t);
7981 return error_mark_node;
7982 }
7983 else
7984 ret = t;
7985 }
7986 }
7987 return ret;
7988 }
7989
7990 /* Return std::tuple_size<TYPE>::value. */
7991
7992 static tree
7993 get_tuple_size (tree type)
7994 {
7995 tree args = make_tree_vec (1);
7996 TREE_VEC_ELT (args, 0) = type;
7997 tree inst = lookup_template_class (tuple_size_identifier, args,
7998 /*in_decl*/NULL_TREE,
7999 /*context*/std_node,
8000 /*entering_scope*/false, tf_none);
8001 inst = complete_type (inst);
8002 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
8003 return NULL_TREE;
8004 tree val = lookup_qualified_name (inst, value_identifier,
8005 /*type*/false, /*complain*/false);
8006 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8007 val = maybe_constant_value (val);
8008 if (TREE_CODE (val) == INTEGER_CST)
8009 return val;
8010 else
8011 return error_mark_node;
8012 }
8013
8014 /* Return std::tuple_element<I,TYPE>::type. */
8015
8016 static tree
8017 get_tuple_element_type (tree type, unsigned i)
8018 {
8019 tree args = make_tree_vec (2);
8020 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8021 TREE_VEC_ELT (args, 1) = type;
8022 tree inst = lookup_template_class (tuple_element_identifier, args,
8023 /*in_decl*/NULL_TREE,
8024 /*context*/std_node,
8025 /*entering_scope*/false,
8026 tf_warning_or_error);
8027 return make_typename_type (inst, type_identifier,
8028 none_type, tf_warning_or_error);
8029 }
8030
8031 /* Return e.get<i>() or get<i>(e). */
8032
8033 static tree
8034 get_tuple_decomp_init (tree decl, unsigned i)
8035 {
8036 tree targs = make_tree_vec (1);
8037 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8038
8039 tree etype = TREE_TYPE (decl);
8040 tree e = convert_from_reference (decl);
8041
8042 /* [The id-expression] e is an lvalue if the type of the entity e is an
8043 lvalue reference and an xvalue otherwise. */
8044 if (!TYPE_REF_P (etype)
8045 || TYPE_REF_IS_RVALUE (etype))
8046 e = move (e);
8047
8048 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8049 /*type*/false, /*complain*/false);
8050 bool use_member_get = false;
8051
8052 /* To use a member get, member lookup must find at least one
8053 declaration that is a function template
8054 whose first template parameter is a non-type parameter. */
8055 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8056 {
8057 tree fn = *iter;
8058 if (TREE_CODE (fn) == TEMPLATE_DECL)
8059 {
8060 tree tparms = DECL_TEMPLATE_PARMS (fn);
8061 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8062 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8063 {
8064 use_member_get = true;
8065 break;
8066 }
8067 }
8068 }
8069
8070 if (use_member_get)
8071 {
8072 fns = lookup_template_function (fns, targs);
8073 return build_new_method_call (e, fns, /*args*/NULL,
8074 /*path*/NULL_TREE, LOOKUP_NORMAL,
8075 /*fn_p*/NULL, tf_warning_or_error);
8076 }
8077 else
8078 {
8079 releasing_vec args (make_tree_vector_single (e));
8080 fns = lookup_template_function (get__identifier, targs);
8081 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8082 return finish_call_expr (fns, &args, /*novirt*/false,
8083 /*koenig*/true, tf_warning_or_error);
8084 }
8085 }
8086
8087 /* It's impossible to recover the decltype of a tuple decomposition variable
8088 based on the actual type of the variable, so store it in a hash table. */
8089
8090 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8091
8092 tree
8093 lookup_decomp_type (tree v)
8094 {
8095 return *decomp_type_table->get (v);
8096 }
8097
8098 /* Mangle a decomposition declaration if needed. Arguments like
8099 in cp_finish_decomp. */
8100
8101 void
8102 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8103 {
8104 if (!processing_template_decl
8105 && !error_operand_p (decl)
8106 && TREE_STATIC (decl))
8107 {
8108 auto_vec<tree, 16> v;
8109 v.safe_grow (count);
8110 tree d = first;
8111 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8112 v[count - i - 1] = d;
8113 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8114 maybe_apply_pragma_weak (decl);
8115 }
8116 }
8117
8118 /* Finish a decomposition declaration. DECL is the underlying declaration
8119 "e", FIRST is the head of a chain of decls for the individual identifiers
8120 chained through DECL_CHAIN in reverse order and COUNT is the number of
8121 those decls. */
8122
8123 void
8124 cp_finish_decomp (tree decl, tree first, unsigned int count)
8125 {
8126 if (error_operand_p (decl))
8127 {
8128 error_out:
8129 while (count--)
8130 {
8131 TREE_TYPE (first) = error_mark_node;
8132 if (DECL_HAS_VALUE_EXPR_P (first))
8133 {
8134 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8135 DECL_HAS_VALUE_EXPR_P (first) = 0;
8136 }
8137 first = DECL_CHAIN (first);
8138 }
8139 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8140 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8141 return;
8142 }
8143
8144 location_t loc = DECL_SOURCE_LOCATION (decl);
8145 if (type_dependent_expression_p (decl)
8146 /* This happens for range for when not in templates.
8147 Still add the DECL_VALUE_EXPRs for later processing. */
8148 || (!processing_template_decl
8149 && type_uses_auto (TREE_TYPE (decl))))
8150 {
8151 for (unsigned int i = 0; i < count; i++)
8152 {
8153 if (!DECL_HAS_VALUE_EXPR_P (first))
8154 {
8155 tree v = build_nt (ARRAY_REF, decl,
8156 size_int (count - i - 1),
8157 NULL_TREE, NULL_TREE);
8158 SET_DECL_VALUE_EXPR (first, v);
8159 DECL_HAS_VALUE_EXPR_P (first) = 1;
8160 }
8161 if (processing_template_decl)
8162 fit_decomposition_lang_decl (first, decl);
8163 first = DECL_CHAIN (first);
8164 }
8165 return;
8166 }
8167
8168 auto_vec<tree, 16> v;
8169 v.safe_grow (count);
8170 tree d = first;
8171 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8172 {
8173 v[count - i - 1] = d;
8174 fit_decomposition_lang_decl (d, decl);
8175 }
8176
8177 tree type = TREE_TYPE (decl);
8178 tree dexp = decl;
8179
8180 if (TYPE_REF_P (type))
8181 {
8182 dexp = convert_from_reference (dexp);
8183 type = complete_type (TREE_TYPE (type));
8184 if (type == error_mark_node)
8185 goto error_out;
8186 if (!COMPLETE_TYPE_P (type))
8187 {
8188 error_at (loc, "structured binding refers to incomplete type %qT",
8189 type);
8190 goto error_out;
8191 }
8192 }
8193
8194 tree eltype = NULL_TREE;
8195 unsigned HOST_WIDE_INT eltscnt = 0;
8196 if (TREE_CODE (type) == ARRAY_TYPE)
8197 {
8198 tree nelts;
8199 nelts = array_type_nelts_top (type);
8200 if (nelts == error_mark_node)
8201 goto error_out;
8202 if (!tree_fits_uhwi_p (nelts))
8203 {
8204 error_at (loc, "cannot decompose variable length array %qT", type);
8205 goto error_out;
8206 }
8207 eltscnt = tree_to_uhwi (nelts);
8208 if (count != eltscnt)
8209 {
8210 cnt_mismatch:
8211 if (count > eltscnt)
8212 error_n (loc, count,
8213 "%u name provided for structured binding",
8214 "%u names provided for structured binding", count);
8215 else
8216 error_n (loc, count,
8217 "only %u name provided for structured binding",
8218 "only %u names provided for structured binding", count);
8219 inform_n (loc, eltscnt,
8220 "while %qT decomposes into %wu element",
8221 "while %qT decomposes into %wu elements",
8222 type, eltscnt);
8223 goto error_out;
8224 }
8225 eltype = TREE_TYPE (type);
8226 for (unsigned int i = 0; i < count; i++)
8227 {
8228 TREE_TYPE (v[i]) = eltype;
8229 layout_decl (v[i], 0);
8230 if (processing_template_decl)
8231 continue;
8232 tree t = unshare_expr (dexp);
8233 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8234 eltype, t, size_int (i), NULL_TREE,
8235 NULL_TREE);
8236 SET_DECL_VALUE_EXPR (v[i], t);
8237 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8238 }
8239 }
8240 /* 2 GNU extensions. */
8241 else if (TREE_CODE (type) == COMPLEX_TYPE)
8242 {
8243 eltscnt = 2;
8244 if (count != eltscnt)
8245 goto cnt_mismatch;
8246 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8247 for (unsigned int i = 0; i < count; i++)
8248 {
8249 TREE_TYPE (v[i]) = eltype;
8250 layout_decl (v[i], 0);
8251 if (processing_template_decl)
8252 continue;
8253 tree t = unshare_expr (dexp);
8254 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8255 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8256 t);
8257 SET_DECL_VALUE_EXPR (v[i], t);
8258 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8259 }
8260 }
8261 else if (TREE_CODE (type) == VECTOR_TYPE)
8262 {
8263 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8264 {
8265 error_at (loc, "cannot decompose variable length vector %qT", type);
8266 goto error_out;
8267 }
8268 if (count != eltscnt)
8269 goto cnt_mismatch;
8270 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8271 for (unsigned int i = 0; i < count; i++)
8272 {
8273 TREE_TYPE (v[i]) = eltype;
8274 layout_decl (v[i], 0);
8275 if (processing_template_decl)
8276 continue;
8277 tree t = unshare_expr (dexp);
8278 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
8279 &t, size_int (i));
8280 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8281 eltype, t, size_int (i), NULL_TREE,
8282 NULL_TREE);
8283 SET_DECL_VALUE_EXPR (v[i], t);
8284 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8285 }
8286 }
8287 else if (tree tsize = get_tuple_size (type))
8288 {
8289 if (tsize == error_mark_node)
8290 {
8291 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
8292 "constant expression", type);
8293 goto error_out;
8294 }
8295 if (!tree_fits_uhwi_p (tsize))
8296 {
8297 error_n (loc, count,
8298 "%u name provided for structured binding",
8299 "%u names provided for structured binding", count);
8300 inform (loc, "while %qT decomposes into %E elements",
8301 type, tsize);
8302 goto error_out;
8303 }
8304 eltscnt = tree_to_uhwi (tsize);
8305 if (count != eltscnt)
8306 goto cnt_mismatch;
8307 int save_read = DECL_READ_P (decl);
8308 for (unsigned i = 0; i < count; ++i)
8309 {
8310 location_t sloc = input_location;
8311 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8312
8313 input_location = dloc;
8314 tree init = get_tuple_decomp_init (decl, i);
8315 tree eltype = (init == error_mark_node ? error_mark_node
8316 : get_tuple_element_type (type, i));
8317 input_location = sloc;
8318
8319 if (init == error_mark_node || eltype == error_mark_node)
8320 {
8321 inform (dloc, "in initialization of structured binding "
8322 "variable %qD", v[i]);
8323 goto error_out;
8324 }
8325 /* Save the decltype away before reference collapse. */
8326 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8327 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8328 TREE_TYPE (v[i]) = eltype;
8329 layout_decl (v[i], 0);
8330 if (DECL_HAS_VALUE_EXPR_P (v[i]))
8331 {
8332 /* In this case the names are variables, not just proxies. */
8333 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8334 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8335 }
8336 if (!processing_template_decl)
8337 {
8338 copy_linkage (v[i], decl);
8339 cp_finish_decl (v[i], init, /*constexpr*/false,
8340 /*asm*/NULL_TREE, LOOKUP_NORMAL);
8341 }
8342 }
8343 /* Ignore reads from the underlying decl performed during initialization
8344 of the individual variables. If those will be read, we'll mark
8345 the underlying decl as read at that point. */
8346 DECL_READ_P (decl) = save_read;
8347 }
8348 else if (TREE_CODE (type) == UNION_TYPE)
8349 {
8350 error_at (loc, "cannot decompose union type %qT", type);
8351 goto error_out;
8352 }
8353 else if (!CLASS_TYPE_P (type))
8354 {
8355 error_at (loc, "cannot decompose non-array non-class type %qT", type);
8356 goto error_out;
8357 }
8358 else if (LAMBDA_TYPE_P (type))
8359 {
8360 error_at (loc, "cannot decompose lambda closure type %qT", type);
8361 goto error_out;
8362 }
8363 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8364 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8365 type);
8366 else
8367 {
8368 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8369 if (btype == error_mark_node)
8370 goto error_out;
8371 else if (btype == NULL_TREE)
8372 {
8373 error_at (loc, "cannot decompose class type %qT without non-static "
8374 "data members", type);
8375 goto error_out;
8376 }
8377 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8378 if (TREE_CODE (field) != FIELD_DECL
8379 || DECL_ARTIFICIAL (field)
8380 || DECL_UNNAMED_BIT_FIELD (field))
8381 continue;
8382 else
8383 eltscnt++;
8384 if (count != eltscnt)
8385 goto cnt_mismatch;
8386 tree t = dexp;
8387 if (type != btype)
8388 {
8389 t = convert_to_base (t, btype, /*check_access*/true,
8390 /*nonnull*/false, tf_warning_or_error);
8391 type = btype;
8392 }
8393 unsigned int i = 0;
8394 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8395 if (TREE_CODE (field) != FIELD_DECL
8396 || DECL_ARTIFICIAL (field)
8397 || DECL_UNNAMED_BIT_FIELD (field))
8398 continue;
8399 else
8400 {
8401 tree tt = finish_non_static_data_member (field, unshare_expr (t),
8402 NULL_TREE);
8403 if (REFERENCE_REF_P (tt))
8404 tt = TREE_OPERAND (tt, 0);
8405 TREE_TYPE (v[i]) = TREE_TYPE (tt);
8406 layout_decl (v[i], 0);
8407 if (!processing_template_decl)
8408 {
8409 SET_DECL_VALUE_EXPR (v[i], tt);
8410 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8411 }
8412 i++;
8413 }
8414 }
8415 if (processing_template_decl)
8416 {
8417 for (unsigned int i = 0; i < count; i++)
8418 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8419 {
8420 tree a = build_nt (ARRAY_REF, decl, size_int (i),
8421 NULL_TREE, NULL_TREE);
8422 SET_DECL_VALUE_EXPR (v[i], a);
8423 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8424 }
8425 }
8426 }
8427
8428 /* Returns a declaration for a VAR_DECL as if:
8429
8430 extern "C" TYPE NAME;
8431
8432 had been seen. Used to create compiler-generated global
8433 variables. */
8434
8435 static tree
8436 declare_global_var (tree name, tree type)
8437 {
8438 tree decl;
8439
8440 push_to_top_level ();
8441 decl = build_decl (input_location, VAR_DECL, name, type);
8442 TREE_PUBLIC (decl) = 1;
8443 DECL_EXTERNAL (decl) = 1;
8444 DECL_ARTIFICIAL (decl) = 1;
8445 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
8446 /* If the user has explicitly declared this variable (perhaps
8447 because the code we are compiling is part of a low-level runtime
8448 library), then it is possible that our declaration will be merged
8449 with theirs by pushdecl. */
8450 decl = pushdecl (decl);
8451 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8452 pop_from_top_level ();
8453
8454 return decl;
8455 }
8456
8457 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8458 if "__cxa_atexit" is not being used) corresponding to the function
8459 to be called when the program exits. */
8460
8461 static tree
8462 get_atexit_fn_ptr_type (void)
8463 {
8464 tree fn_type;
8465
8466 if (!atexit_fn_ptr_type_node)
8467 {
8468 tree arg_type;
8469 if (flag_use_cxa_atexit
8470 && !targetm.cxx.use_atexit_for_cxa_atexit ())
8471 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8472 arg_type = ptr_type_node;
8473 else
8474 /* The parameter to "atexit" is "void (*)(void)". */
8475 arg_type = NULL_TREE;
8476
8477 fn_type = build_function_type_list (void_type_node,
8478 arg_type, NULL_TREE);
8479 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
8480 }
8481
8482 return atexit_fn_ptr_type_node;
8483 }
8484
8485 /* Returns a pointer to the `atexit' function. Note that if
8486 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8487 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8488
8489 static tree
8490 get_atexit_node (void)
8491 {
8492 tree atexit_fndecl;
8493 tree fn_type;
8494 tree fn_ptr_type;
8495 const char *name;
8496 bool use_aeabi_atexit;
8497
8498 if (atexit_node)
8499 return atexit_node;
8500
8501 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
8502 {
8503 /* The declaration for `__cxa_atexit' is:
8504
8505 int __cxa_atexit (void (*)(void *), void *, void *)
8506
8507 We build up the argument types and then the function type
8508 itself. */
8509 tree argtype0, argtype1, argtype2;
8510
8511 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
8512 /* First, build the pointer-to-function type for the first
8513 argument. */
8514 fn_ptr_type = get_atexit_fn_ptr_type ();
8515 /* Then, build the rest of the argument types. */
8516 argtype2 = ptr_type_node;
8517 if (use_aeabi_atexit)
8518 {
8519 argtype1 = fn_ptr_type;
8520 argtype0 = ptr_type_node;
8521 }
8522 else
8523 {
8524 argtype1 = ptr_type_node;
8525 argtype0 = fn_ptr_type;
8526 }
8527 /* And the final __cxa_atexit type. */
8528 fn_type = build_function_type_list (integer_type_node,
8529 argtype0, argtype1, argtype2,
8530 NULL_TREE);
8531 if (use_aeabi_atexit)
8532 name = "__aeabi_atexit";
8533 else
8534 name = "__cxa_atexit";
8535 }
8536 else
8537 {
8538 /* The declaration for `atexit' is:
8539
8540 int atexit (void (*)());
8541
8542 We build up the argument types and then the function type
8543 itself. */
8544 fn_ptr_type = get_atexit_fn_ptr_type ();
8545 /* Build the final atexit type. */
8546 fn_type = build_function_type_list (integer_type_node,
8547 fn_ptr_type, NULL_TREE);
8548 name = "atexit";
8549 }
8550
8551 /* Now, build the function declaration. */
8552 push_lang_context (lang_name_c);
8553 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8554 mark_used (atexit_fndecl);
8555 pop_lang_context ();
8556 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8557
8558 return atexit_node;
8559 }
8560
8561 /* Like get_atexit_node, but for thread-local cleanups. */
8562
8563 static tree
8564 get_thread_atexit_node (void)
8565 {
8566 /* The declaration for `__cxa_thread_atexit' is:
8567
8568 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8569 tree fn_type = build_function_type_list (integer_type_node,
8570 get_atexit_fn_ptr_type (),
8571 ptr_type_node, ptr_type_node,
8572 NULL_TREE);
8573
8574 /* Now, build the function declaration. */
8575 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8576 ECF_LEAF | ECF_NOTHROW);
8577 return decay_conversion (atexit_fndecl, tf_warning_or_error);
8578 }
8579
8580 /* Returns the __dso_handle VAR_DECL. */
8581
8582 static tree
8583 get_dso_handle_node (void)
8584 {
8585 if (dso_handle_node)
8586 return dso_handle_node;
8587
8588 /* Declare the variable. */
8589 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8590 ptr_type_node);
8591
8592 #ifdef HAVE_GAS_HIDDEN
8593 if (dso_handle_node != error_mark_node)
8594 {
8595 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8596 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8597 }
8598 #endif
8599
8600 return dso_handle_node;
8601 }
8602
8603 /* Begin a new function with internal linkage whose job will be simply
8604 to destroy some particular variable. */
8605
8606 static GTY(()) int start_cleanup_cnt;
8607
8608 static tree
8609 start_cleanup_fn (void)
8610 {
8611 char name[32];
8612 tree fntype;
8613 tree fndecl;
8614 bool use_cxa_atexit = flag_use_cxa_atexit
8615 && !targetm.cxx.use_atexit_for_cxa_atexit ();
8616
8617 push_to_top_level ();
8618
8619 /* No need to mangle this. */
8620 push_lang_context (lang_name_c);
8621
8622 /* Build the name of the function. */
8623 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8624 /* Build the function declaration. */
8625 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8626 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8627 /* It's a function with internal linkage, generated by the
8628 compiler. */
8629 TREE_PUBLIC (fndecl) = 0;
8630 DECL_ARTIFICIAL (fndecl) = 1;
8631 /* Make the function `inline' so that it is only emitted if it is
8632 actually needed. It is unlikely that it will be inlined, since
8633 it is only called via a function pointer, but we avoid unnecessary
8634 emissions this way. */
8635 DECL_DECLARED_INLINE_P (fndecl) = 1;
8636 DECL_INTERFACE_KNOWN (fndecl) = 1;
8637 /* Build the parameter. */
8638 if (use_cxa_atexit)
8639 {
8640 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8641 TREE_USED (parmdecl) = 1;
8642 DECL_READ_P (parmdecl) = 1;
8643 DECL_ARGUMENTS (fndecl) = parmdecl;
8644 }
8645
8646 pushdecl (fndecl);
8647 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8648
8649 pop_lang_context ();
8650
8651 return current_function_decl;
8652 }
8653
8654 /* Finish the cleanup function begun by start_cleanup_fn. */
8655
8656 static void
8657 end_cleanup_fn (void)
8658 {
8659 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8660
8661 pop_from_top_level ();
8662 }
8663
8664 /* Generate code to handle the destruction of DECL, an object with
8665 static storage duration. */
8666
8667 tree
8668 register_dtor_fn (tree decl)
8669 {
8670 tree cleanup;
8671 tree addr;
8672 tree compound_stmt;
8673 tree fcall;
8674 tree type;
8675 bool ob_parm, dso_parm, use_dtor;
8676 tree arg0, arg1, arg2;
8677 tree atex_node;
8678
8679 type = TREE_TYPE (decl);
8680 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8681 return void_node;
8682
8683 if (decl_maybe_constant_destruction (decl, type)
8684 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
8685 {
8686 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8687 return void_node;
8688 }
8689
8690 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8691 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8692 destructor to "__cxa_atexit"; we don't have to build a temporary
8693 function to do the cleanup. */
8694 dso_parm = (flag_use_cxa_atexit
8695 && !targetm.cxx.use_atexit_for_cxa_atexit ());
8696 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8697 use_dtor = ob_parm && CLASS_TYPE_P (type);
8698 if (use_dtor)
8699 {
8700 cleanup = get_class_binding (type, complete_dtor_identifier);
8701
8702 /* Make sure it is accessible. */
8703 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8704 tf_warning_or_error);
8705 }
8706 else
8707 {
8708 /* Call build_cleanup before we enter the anonymous function so
8709 that any access checks will be done relative to the current
8710 scope, rather than the scope of the anonymous function. */
8711 build_cleanup (decl);
8712
8713 /* Now start the function. */
8714 cleanup = start_cleanup_fn ();
8715
8716 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8717 to the original function, rather than the anonymous one. That
8718 will make the back end think that nested functions are in use,
8719 which causes confusion. */
8720 push_deferring_access_checks (dk_no_check);
8721 fcall = build_cleanup (decl);
8722 pop_deferring_access_checks ();
8723
8724 /* Create the body of the anonymous function. */
8725 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8726 finish_expr_stmt (fcall);
8727 finish_compound_stmt (compound_stmt);
8728 end_cleanup_fn ();
8729 }
8730
8731 /* Call atexit with the cleanup function. */
8732 mark_used (cleanup);
8733 cleanup = build_address (cleanup);
8734
8735 if (CP_DECL_THREAD_LOCAL_P (decl))
8736 atex_node = get_thread_atexit_node ();
8737 else
8738 atex_node = get_atexit_node ();
8739
8740 if (use_dtor)
8741 {
8742 /* We must convert CLEANUP to the type that "__cxa_atexit"
8743 expects. */
8744 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8745 /* "__cxa_atexit" will pass the address of DECL to the
8746 cleanup function. */
8747 mark_used (decl);
8748 addr = build_address (decl);
8749 /* The declared type of the parameter to "__cxa_atexit" is
8750 "void *". For plain "T*", we could just let the
8751 machinery in cp_build_function_call convert it -- but if the
8752 type is "cv-qualified T *", then we need to convert it
8753 before passing it in, to avoid spurious errors. */
8754 addr = build_nop (ptr_type_node, addr);
8755 }
8756 else
8757 /* Since the cleanup functions we build ignore the address
8758 they're given, there's no reason to pass the actual address
8759 in, and, in general, it's cheaper to pass NULL than any
8760 other value. */
8761 addr = null_pointer_node;
8762
8763 if (dso_parm)
8764 arg2 = cp_build_addr_expr (get_dso_handle_node (),
8765 tf_warning_or_error);
8766 else if (ob_parm)
8767 /* Just pass NULL to the dso handle parm if we don't actually
8768 have a DSO handle on this target. */
8769 arg2 = null_pointer_node;
8770 else
8771 arg2 = NULL_TREE;
8772
8773 if (ob_parm)
8774 {
8775 if (!CP_DECL_THREAD_LOCAL_P (decl)
8776 && targetm.cxx.use_aeabi_atexit ())
8777 {
8778 arg1 = cleanup;
8779 arg0 = addr;
8780 }
8781 else
8782 {
8783 arg1 = addr;
8784 arg0 = cleanup;
8785 }
8786 }
8787 else
8788 {
8789 arg0 = cleanup;
8790 arg1 = NULL_TREE;
8791 }
8792 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8793 arg0, arg1, arg2, NULL_TREE);
8794 }
8795
8796 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8797 is its initializer. Generate code to handle the construction
8798 and destruction of DECL. */
8799
8800 static void
8801 expand_static_init (tree decl, tree init)
8802 {
8803 gcc_assert (VAR_P (decl));
8804 gcc_assert (TREE_STATIC (decl));
8805
8806 /* Some variables require no dynamic initialization. */
8807 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
8808 {
8809 /* Make sure the destructor is callable. */
8810 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8811 if (!init)
8812 return;
8813 }
8814
8815 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8816 && !DECL_FUNCTION_SCOPE_P (decl))
8817 {
8818 location_t dloc = DECL_SOURCE_LOCATION (decl);
8819 if (init)
8820 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8821 "needs dynamic initialization", decl);
8822 else
8823 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8824 "has a non-trivial destructor", decl);
8825 static bool informed;
8826 if (!informed)
8827 {
8828 inform (dloc, "C++11 %<thread_local%> allows dynamic "
8829 "initialization and destruction");
8830 informed = true;
8831 }
8832 return;
8833 }
8834
8835 if (DECL_FUNCTION_SCOPE_P (decl))
8836 {
8837 /* Emit code to perform this initialization but once. */
8838 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8839 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8840 tree guard, guard_addr;
8841 tree flag, begin;
8842 /* We don't need thread-safety code for thread-local vars. */
8843 bool thread_guard = (flag_threadsafe_statics
8844 && !CP_DECL_THREAD_LOCAL_P (decl));
8845
8846 /* Emit code to perform this initialization but once. This code
8847 looks like:
8848
8849 static <type> guard;
8850 if (!__atomic_load (guard.first_byte)) {
8851 if (__cxa_guard_acquire (&guard)) {
8852 bool flag = false;
8853 try {
8854 // Do initialization.
8855 flag = true; __cxa_guard_release (&guard);
8856 // Register variable for destruction at end of program.
8857 } catch {
8858 if (!flag) __cxa_guard_abort (&guard);
8859 }
8860 }
8861 }
8862
8863 Note that the `flag' variable is only set to 1 *after* the
8864 initialization is complete. This ensures that an exception,
8865 thrown during the construction, will cause the variable to
8866 reinitialized when we pass through this code again, as per:
8867
8868 [stmt.dcl]
8869
8870 If the initialization exits by throwing an exception, the
8871 initialization is not complete, so it will be tried again
8872 the next time control enters the declaration.
8873
8874 This process should be thread-safe, too; multiple threads
8875 should not be able to initialize the variable more than
8876 once. */
8877
8878 /* Create the guard variable. */
8879 guard = get_guard (decl);
8880
8881 /* Begin the conditional initialization. */
8882 if_stmt = begin_if_stmt ();
8883
8884 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8885 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8886
8887 if (thread_guard)
8888 {
8889 tree vfntype = NULL_TREE;
8890 tree acquire_name, release_name, abort_name;
8891 tree acquire_fn, release_fn, abort_fn;
8892 guard_addr = build_address (guard);
8893
8894 acquire_name = get_identifier ("__cxa_guard_acquire");
8895 release_name = get_identifier ("__cxa_guard_release");
8896 abort_name = get_identifier ("__cxa_guard_abort");
8897 acquire_fn = get_global_binding (acquire_name);
8898 release_fn = get_global_binding (release_name);
8899 abort_fn = get_global_binding (abort_name);
8900 if (!acquire_fn)
8901 acquire_fn = push_library_fn
8902 (acquire_name, build_function_type_list (integer_type_node,
8903 TREE_TYPE (guard_addr),
8904 NULL_TREE),
8905 NULL_TREE, ECF_NOTHROW);
8906 if (!release_fn || !abort_fn)
8907 vfntype = build_function_type_list (void_type_node,
8908 TREE_TYPE (guard_addr),
8909 NULL_TREE);
8910 if (!release_fn)
8911 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8912 ECF_NOTHROW);
8913 if (!abort_fn)
8914 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8915 ECF_NOTHROW | ECF_LEAF);
8916
8917 inner_if_stmt = begin_if_stmt ();
8918 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8919 inner_if_stmt);
8920
8921 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8922 begin = get_target_expr (boolean_false_node);
8923 flag = TARGET_EXPR_SLOT (begin);
8924
8925 TARGET_EXPR_CLEANUP (begin)
8926 = build3 (COND_EXPR, void_type_node, flag,
8927 void_node,
8928 build_call_n (abort_fn, 1, guard_addr));
8929 CLEANUP_EH_ONLY (begin) = 1;
8930
8931 /* Do the initialization itself. */
8932 init = add_stmt_to_compound (begin, init);
8933 init = add_stmt_to_compound
8934 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8935 init = add_stmt_to_compound
8936 (init, build_call_n (release_fn, 1, guard_addr));
8937 }
8938 else
8939 init = add_stmt_to_compound (init, set_guard (guard));
8940
8941 /* Use atexit to register a function for destroying this static
8942 variable. */
8943 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8944
8945 finish_expr_stmt (init);
8946
8947 if (thread_guard)
8948 {
8949 finish_compound_stmt (inner_then_clause);
8950 finish_then_clause (inner_if_stmt);
8951 finish_if_stmt (inner_if_stmt);
8952 }
8953
8954 finish_compound_stmt (then_clause);
8955 finish_then_clause (if_stmt);
8956 finish_if_stmt (if_stmt);
8957 }
8958 else if (CP_DECL_THREAD_LOCAL_P (decl))
8959 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8960 else
8961 static_aggregates = tree_cons (init, decl, static_aggregates);
8962 }
8963
8964 \f
8965 /* Make TYPE a complete type based on INITIAL_VALUE.
8966 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8967 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8968 3 if the initializer list is empty (in pedantic mode). */
8969
8970 int
8971 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8972 {
8973 int failure;
8974 tree type, elt_type;
8975
8976 /* Don't get confused by a CONSTRUCTOR for some other type. */
8977 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8978 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8979 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8980 return 1;
8981
8982 if (initial_value)
8983 {
8984 unsigned HOST_WIDE_INT i;
8985 tree value;
8986
8987 /* An array of character type can be initialized from a
8988 brace-enclosed string constant.
8989
8990 FIXME: this code is duplicated from reshape_init. Probably
8991 we should just call reshape_init here? */
8992 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
8993 && TREE_CODE (initial_value) == CONSTRUCTOR
8994 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
8995 {
8996 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
8997 tree value = (*v)[0].value;
8998 STRIP_ANY_LOCATION_WRAPPER (value);
8999
9000 if (TREE_CODE (value) == STRING_CST
9001 && v->length () == 1)
9002 initial_value = value;
9003 }
9004
9005 /* If any of the elements are parameter packs, we can't actually
9006 complete this type now because the array size is dependent. */
9007 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9008 {
9009 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
9010 i, value)
9011 {
9012 if (PACK_EXPANSION_P (value))
9013 return 0;
9014 }
9015 }
9016 }
9017
9018 failure = complete_array_type (ptype, initial_value, do_default);
9019
9020 /* We can create the array before the element type is complete, which
9021 means that we didn't have these two bits set in the original type
9022 either. In completing the type, we are expected to propagate these
9023 bits. See also complete_type which does the same thing for arrays
9024 of fixed size. */
9025 type = *ptype;
9026 if (type != error_mark_node && TYPE_DOMAIN (type))
9027 {
9028 elt_type = TREE_TYPE (type);
9029 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9030 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9031 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9032 }
9033
9034 return failure;
9035 }
9036
9037 /* As above, but either give an error or reject zero-size arrays, depending
9038 on COMPLAIN. */
9039
9040 int
9041 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9042 bool do_default, tsubst_flags_t complain)
9043 {
9044 int failure;
9045 bool sfinae = !(complain & tf_error);
9046 /* In SFINAE context we can't be lenient about zero-size arrays. */
9047 if (sfinae)
9048 ++pedantic;
9049 failure = cp_complete_array_type (ptype, initial_value, do_default);
9050 if (sfinae)
9051 --pedantic;
9052 if (failure)
9053 {
9054 if (sfinae)
9055 /* Not an error. */;
9056 else if (failure == 1)
9057 error ("initializer fails to determine size of %qT", *ptype);
9058 else if (failure == 2)
9059 {
9060 if (do_default)
9061 error ("array size missing in %qT", *ptype);
9062 }
9063 else if (failure == 3)
9064 error ("zero-size array %qT", *ptype);
9065 *ptype = error_mark_node;
9066 }
9067 return failure;
9068 }
9069 \f
9070 /* Return zero if something is declared to be a member of type
9071 CTYPE when in the context of CUR_TYPE. STRING is the error
9072 message to print in that case. Otherwise, quietly return 1. */
9073
9074 static int
9075 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9076 {
9077 if (ctype && ctype != cur_type)
9078 {
9079 if (flags == DTOR_FLAG)
9080 error ("destructor for alien class %qT cannot be a member", ctype);
9081 else
9082 error ("constructor for alien class %qT cannot be a member", ctype);
9083 return 0;
9084 }
9085 return 1;
9086 }
9087 \f
9088 /* Subroutine of `grokdeclarator'. */
9089
9090 /* Generate errors possibly applicable for a given set of specifiers.
9091 This is for ARM $7.1.2. */
9092
9093 static void
9094 bad_specifiers (tree object,
9095 enum bad_spec_place type,
9096 int virtualp,
9097 int quals,
9098 int inlinep,
9099 int friendp,
9100 int raises,
9101 const location_t* locations)
9102 {
9103 switch (type)
9104 {
9105 case BSP_VAR:
9106 if (virtualp)
9107 error_at (locations[ds_virtual],
9108 "%qD declared as a %<virtual%> variable", object);
9109 if (quals)
9110 error ("%<const%> and %<volatile%> function specifiers on "
9111 "%qD invalid in variable declaration", object);
9112 break;
9113 case BSP_PARM:
9114 if (virtualp)
9115 error_at (locations[ds_virtual],
9116 "%qD declared as a %<virtual%> parameter", object);
9117 if (inlinep)
9118 error_at (locations[ds_inline],
9119 "%qD declared as an %<inline%> parameter", object);
9120 if (quals)
9121 error ("%<const%> and %<volatile%> function specifiers on "
9122 "%qD invalid in parameter declaration", object);
9123 break;
9124 case BSP_TYPE:
9125 if (virtualp)
9126 error_at (locations[ds_virtual],
9127 "%qD declared as a %<virtual%> type", object);
9128 if (inlinep)
9129 error_at (locations[ds_inline],
9130 "%qD declared as an %<inline%> type", object);
9131 if (quals)
9132 error ("%<const%> and %<volatile%> function specifiers on "
9133 "%qD invalid in type declaration", object);
9134 break;
9135 case BSP_FIELD:
9136 if (virtualp)
9137 error_at (locations[ds_virtual],
9138 "%qD declared as a %<virtual%> field", object);
9139 if (inlinep)
9140 error_at (locations[ds_inline],
9141 "%qD declared as an %<inline%> field", object);
9142 if (quals)
9143 error ("%<const%> and %<volatile%> function specifiers on "
9144 "%qD invalid in field declaration", object);
9145 break;
9146 default:
9147 gcc_unreachable();
9148 }
9149 if (friendp)
9150 error ("%q+D declared as a friend", object);
9151 if (raises
9152 && !flag_noexcept_type
9153 && (TREE_CODE (object) == TYPE_DECL
9154 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9155 && !TYPE_REFFN_P (TREE_TYPE (object))
9156 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9157 error ("%q+D declared with an exception specification", object);
9158 }
9159
9160 /* DECL is a member function or static data member and is presently
9161 being defined. Check that the definition is taking place in a
9162 valid namespace. */
9163
9164 static void
9165 check_class_member_definition_namespace (tree decl)
9166 {
9167 /* These checks only apply to member functions and static data
9168 members. */
9169 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9170 /* We check for problems with specializations in pt.c in
9171 check_specialization_namespace, where we can issue better
9172 diagnostics. */
9173 if (processing_specialization)
9174 return;
9175 /* We check this in check_explicit_instantiation_namespace. */
9176 if (processing_explicit_instantiation)
9177 return;
9178 /* [class.mfct]
9179
9180 A member function definition that appears outside of the
9181 class definition shall appear in a namespace scope enclosing
9182 the class definition.
9183
9184 [class.static.data]
9185
9186 The definition for a static data member shall appear in a
9187 namespace scope enclosing the member's class definition. */
9188 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9189 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9190 decl, DECL_CONTEXT (decl));
9191 }
9192
9193 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9194 METHOD_TYPE for a non-static member function; QUALS are the
9195 cv-qualifiers that apply to the function. */
9196
9197 tree
9198 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9199 {
9200 tree this_type;
9201 tree qual_type;
9202 tree parm;
9203 cp_cv_quals this_quals;
9204
9205 if (CLASS_TYPE_P (type))
9206 {
9207 this_type
9208 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9209 this_type = build_pointer_type (this_type);
9210 }
9211 else
9212 this_type = type_of_this_parm (type);
9213 /* The `this' parameter is implicitly `const'; it cannot be
9214 assigned to. */
9215 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9216 qual_type = cp_build_qualified_type (this_type, this_quals);
9217 parm = build_artificial_parm (fn, this_identifier, qual_type);
9218 cp_apply_type_quals_to_decl (this_quals, parm);
9219 return parm;
9220 }
9221
9222 /* DECL is a static member function. Complain if it was declared
9223 with function-cv-quals. */
9224
9225 static void
9226 check_static_quals (tree decl, cp_cv_quals quals)
9227 {
9228 if (quals != TYPE_UNQUALIFIED)
9229 error ("static member function %q#D declared with type qualifiers",
9230 decl);
9231 }
9232
9233 // Check that FN takes no arguments and returns bool.
9234 static void
9235 check_concept_fn (tree fn)
9236 {
9237 // A constraint is nullary.
9238 if (DECL_ARGUMENTS (fn))
9239 error_at (DECL_SOURCE_LOCATION (fn),
9240 "concept %q#D declared with function parameters", fn);
9241
9242 // The declared return type of the concept shall be bool, and
9243 // it shall not be deduced from it definition.
9244 tree type = TREE_TYPE (TREE_TYPE (fn));
9245 if (is_auto (type))
9246 error_at (DECL_SOURCE_LOCATION (fn),
9247 "concept %q#D declared with a deduced return type", fn);
9248 else if (type != boolean_type_node)
9249 error_at (DECL_SOURCE_LOCATION (fn),
9250 "concept %q#D with non-%<bool%> return type %qT", fn, type);
9251 }
9252
9253 /* Helper function. Replace the temporary this parameter injected
9254 during cp_finish_omp_declare_simd with the real this parameter. */
9255
9256 static tree
9257 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
9258 {
9259 tree this_parm = (tree) data;
9260 if (TREE_CODE (*tp) == PARM_DECL
9261 && DECL_NAME (*tp) == this_identifier
9262 && *tp != this_parm)
9263 *tp = this_parm;
9264 else if (TYPE_P (*tp))
9265 *walk_subtrees = 0;
9266 return NULL_TREE;
9267 }
9268
9269 /* CTYPE is class type, or null if non-class.
9270 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9271 or METHOD_TYPE.
9272 DECLARATOR is the function's name.
9273 PARMS is a chain of PARM_DECLs for the function.
9274 VIRTUALP is truthvalue of whether the function is virtual or not.
9275 FLAGS are to be passed through to `grokclassfn'.
9276 QUALS are qualifiers indicating whether the function is `const'
9277 or `volatile'.
9278 RAISES is a list of exceptions that this function can raise.
9279 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9280 not look, and -1 if we should not call `grokclassfn' at all.
9281
9282 SFK is the kind of special function (if any) for the new function.
9283
9284 Returns `NULL_TREE' if something goes wrong, after issuing
9285 applicable error messages. */
9286
9287 static tree
9288 grokfndecl (tree ctype,
9289 tree type,
9290 tree declarator,
9291 tree parms,
9292 tree orig_declarator,
9293 const cp_decl_specifier_seq *declspecs,
9294 tree decl_reqs,
9295 int virtualp,
9296 enum overload_flags flags,
9297 cp_cv_quals quals,
9298 cp_ref_qualifier rqual,
9299 tree raises,
9300 int check,
9301 int friendp,
9302 int publicp,
9303 int inlinep,
9304 bool deletedp,
9305 special_function_kind sfk,
9306 bool funcdef_flag,
9307 bool late_return_type_p,
9308 int template_count,
9309 tree in_namespace,
9310 tree* attrlist,
9311 location_t location)
9312 {
9313 tree decl;
9314 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9315 tree t;
9316
9317 if (location == UNKNOWN_LOCATION)
9318 location = input_location;
9319
9320 /* Was the concept specifier present? */
9321 bool concept_p = inlinep & 4;
9322
9323 /* Concept declarations must have a corresponding definition. */
9324 if (concept_p && !funcdef_flag)
9325 {
9326 error_at (location, "concept %qD has no definition", declarator);
9327 return NULL_TREE;
9328 }
9329
9330 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9331
9332 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9333
9334 /* Set the constraints on the declaration. */
9335 if (flag_concepts)
9336 {
9337 tree tmpl_reqs = NULL_TREE;
9338 tree ctx = friendp ? current_class_type : ctype;
9339 bool memtmpl = (processing_template_decl > template_class_depth (ctx));
9340 if (memtmpl)
9341 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9342 tree ci = build_constraints (tmpl_reqs, decl_reqs);
9343 if (concept_p && ci)
9344 {
9345 error_at (location, "a function concept cannot be constrained");
9346 ci = NULL_TREE;
9347 }
9348 /* C++20 CA378: Remove non-templated constrained functions. */
9349 if (ci && !flag_concepts_ts
9350 && (!processing_template_decl
9351 || (friendp && !memtmpl && !funcdef_flag)))
9352 {
9353 error_at (location, "constraints on a non-templated function");
9354 ci = NULL_TREE;
9355 }
9356 set_constraints (decl, ci);
9357 }
9358
9359 if (TREE_CODE (type) == METHOD_TYPE)
9360 {
9361 tree parm = build_this_parm (decl, type, quals);
9362 DECL_CHAIN (parm) = parms;
9363 parms = parm;
9364
9365 /* Allocate space to hold the vptr bit if needed. */
9366 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9367 }
9368
9369 DECL_ARGUMENTS (decl) = parms;
9370 for (t = parms; t; t = DECL_CHAIN (t))
9371 DECL_CONTEXT (t) = decl;
9372
9373 /* Propagate volatile out from type to decl. */
9374 if (TYPE_VOLATILE (type))
9375 TREE_THIS_VOLATILE (decl) = 1;
9376
9377 /* Setup decl according to sfk. */
9378 switch (sfk)
9379 {
9380 case sfk_constructor:
9381 case sfk_copy_constructor:
9382 case sfk_move_constructor:
9383 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9384 DECL_NAME (decl) = ctor_identifier;
9385 break;
9386 case sfk_destructor:
9387 DECL_CXX_DESTRUCTOR_P (decl) = 1;
9388 DECL_NAME (decl) = dtor_identifier;
9389 break;
9390 default:
9391 break;
9392 }
9393
9394 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9395 {
9396 if (funcdef_flag)
9397 error_at (location,
9398 "defining explicit specialization %qD in friend declaration",
9399 orig_declarator);
9400 else
9401 {
9402 tree fns = TREE_OPERAND (orig_declarator, 0);
9403 tree args = TREE_OPERAND (orig_declarator, 1);
9404
9405 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9406 {
9407 /* Something like `template <class T> friend void f<T>()'. */
9408 error_at (location,
9409 "invalid use of template-id %qD in declaration "
9410 "of primary template",
9411 orig_declarator);
9412 return NULL_TREE;
9413 }
9414
9415
9416 /* A friend declaration of the form friend void f<>(). Record
9417 the information in the TEMPLATE_ID_EXPR. */
9418 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9419
9420 gcc_assert (identifier_p (fns) || OVL_P (fns));
9421 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9422
9423 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9424 if (TREE_PURPOSE (t)
9425 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9426 {
9427 error_at (defparse_location (TREE_PURPOSE (t)),
9428 "default arguments are not allowed in declaration "
9429 "of friend template specialization %qD",
9430 decl);
9431 return NULL_TREE;
9432 }
9433
9434 if (inlinep & 1)
9435 {
9436 error_at (declspecs->locations[ds_inline],
9437 "%<inline%> is not allowed in declaration of friend "
9438 "template specialization %qD",
9439 decl);
9440 return NULL_TREE;
9441 }
9442 }
9443 }
9444
9445 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9446 expression, that declaration shall be a definition..." */
9447 if (friendp && !funcdef_flag)
9448 {
9449 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
9450 t && t != void_list_node; t = TREE_CHAIN (t))
9451 if (TREE_PURPOSE (t))
9452 {
9453 permerror (DECL_SOURCE_LOCATION (decl),
9454 "friend declaration of %qD specifies default "
9455 "arguments and isn%'t a definition", decl);
9456 break;
9457 }
9458 }
9459
9460 /* FIXME: For now. */
9461 if (virtualp && (inlinep & 8) != 0)
9462 {
9463 sorry_at (DECL_SOURCE_LOCATION (decl),
9464 "%<virtual%> %<consteval%> method %qD not supported yet",
9465 decl);
9466 inlinep &= ~8;
9467 }
9468
9469 /* If this decl has namespace scope, set that up. */
9470 if (in_namespace)
9471 set_decl_namespace (decl, in_namespace, friendp);
9472 else if (!ctype)
9473 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
9474
9475 /* `main' and builtins have implicit 'C' linkage. */
9476 if (ctype == NULL_TREE
9477 && DECL_FILE_SCOPE_P (decl)
9478 && current_lang_name == lang_name_cplusplus
9479 && (MAIN_NAME_P (declarator)
9480 || (IDENTIFIER_LENGTH (declarator) > 10
9481 && IDENTIFIER_POINTER (declarator)[0] == '_'
9482 && IDENTIFIER_POINTER (declarator)[1] == '_'
9483 && strncmp (IDENTIFIER_POINTER (declarator)+2,
9484 "builtin_", 8) == 0)
9485 || (targetcm.cxx_implicit_extern_c
9486 && (targetcm.cxx_implicit_extern_c
9487 (IDENTIFIER_POINTER (declarator))))))
9488 SET_DECL_LANGUAGE (decl, lang_c);
9489
9490 /* Should probably propagate const out from type to decl I bet (mrs). */
9491 if (staticp)
9492 {
9493 DECL_STATIC_FUNCTION_P (decl) = 1;
9494 DECL_CONTEXT (decl) = ctype;
9495 }
9496
9497 if (deletedp)
9498 DECL_DELETED_FN (decl) = 1;
9499
9500 if (ctype)
9501 {
9502 DECL_CONTEXT (decl) = ctype;
9503 if (funcdef_flag)
9504 check_class_member_definition_namespace (decl);
9505 }
9506
9507 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9508 {
9509 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9510 error_at (location, "cannot declare %<::main%> to be a template");
9511 if (inlinep & 1)
9512 error_at (declspecs->locations[ds_inline],
9513 "cannot declare %<::main%> to be inline");
9514 if (inlinep & 2)
9515 error_at (declspecs->locations[ds_constexpr],
9516 "cannot declare %<::main%> to be %qs", "constexpr");
9517 if (inlinep & 8)
9518 error_at (declspecs->locations[ds_consteval],
9519 "cannot declare %<::main%> to be %qs", "consteval");
9520 if (!publicp)
9521 error_at (location, "cannot declare %<::main%> to be static");
9522 inlinep = 0;
9523 publicp = 1;
9524 }
9525
9526 /* Members of anonymous types and local classes have no linkage; make
9527 them internal. If a typedef is made later, this will be changed. */
9528 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
9529 || decl_function_context (TYPE_MAIN_DECL (ctype))))
9530 publicp = 0;
9531
9532 if (publicp && cxx_dialect == cxx98)
9533 {
9534 /* [basic.link]: A name with no linkage (notably, the name of a class
9535 or enumeration declared in a local scope) shall not be used to
9536 declare an entity with linkage.
9537
9538 DR 757 relaxes this restriction for C++0x. */
9539 no_linkage_error (decl);
9540 }
9541
9542 TREE_PUBLIC (decl) = publicp;
9543 if (! publicp)
9544 {
9545 DECL_INTERFACE_KNOWN (decl) = 1;
9546 DECL_NOT_REALLY_EXTERN (decl) = 1;
9547 }
9548
9549 /* If the declaration was declared inline, mark it as such. */
9550 if (inlinep)
9551 {
9552 DECL_DECLARED_INLINE_P (decl) = 1;
9553 if (publicp)
9554 DECL_COMDAT (decl) = 1;
9555 }
9556 if (inlinep & 2)
9557 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9558 else if (inlinep & 8)
9559 {
9560 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9561 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
9562 }
9563
9564 // If the concept declaration specifier was found, check
9565 // that the declaration satisfies the necessary requirements.
9566 if (concept_p)
9567 {
9568 DECL_DECLARED_CONCEPT_P (decl) = true;
9569 check_concept_fn (decl);
9570 }
9571
9572 DECL_EXTERNAL (decl) = 1;
9573 if (TREE_CODE (type) == FUNCTION_TYPE)
9574 {
9575 if (quals || rqual)
9576 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
9577 TYPE_UNQUALIFIED,
9578 REF_QUAL_NONE);
9579
9580 if (quals)
9581 {
9582 error (ctype
9583 ? G_("static member function %qD cannot have cv-qualifier")
9584 : G_("non-member function %qD cannot have cv-qualifier"),
9585 decl);
9586 quals = TYPE_UNQUALIFIED;
9587 }
9588
9589 if (rqual)
9590 {
9591 error (ctype
9592 ? G_("static member function %qD cannot have ref-qualifier")
9593 : G_("non-member function %qD cannot have ref-qualifier"),
9594 decl);
9595 rqual = REF_QUAL_NONE;
9596 }
9597 }
9598
9599 if (deduction_guide_p (decl))
9600 {
9601 if (!DECL_NAMESPACE_SCOPE_P (decl))
9602 {
9603 error_at (location, "deduction guide %qD must be declared at "
9604 "namespace scope", decl);
9605 return NULL_TREE;
9606 }
9607 if (funcdef_flag)
9608 error_at (location,
9609 "deduction guide %qD must not have a function body", decl);
9610 }
9611 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
9612 && !grok_op_properties (decl, /*complain=*/true))
9613 return NULL_TREE;
9614 else if (UDLIT_OPER_P (DECL_NAME (decl)))
9615 {
9616 bool long_long_unsigned_p;
9617 bool long_double_p;
9618 const char *suffix = NULL;
9619 /* [over.literal]/6: Literal operators shall not have C linkage. */
9620 if (DECL_LANGUAGE (decl) == lang_c)
9621 {
9622 error_at (location, "literal operator with C linkage");
9623 maybe_show_extern_c_location ();
9624 return NULL_TREE;
9625 }
9626
9627 if (DECL_NAMESPACE_SCOPE_P (decl))
9628 {
9629 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9630 &long_double_p))
9631 {
9632 error_at (location, "%qD has invalid argument list", decl);
9633 return NULL_TREE;
9634 }
9635
9636 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9637 if (long_long_unsigned_p)
9638 {
9639 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9640 warning_at (location, 0, "integer suffix %qs"
9641 " shadowed by implementation", suffix);
9642 }
9643 else if (long_double_p)
9644 {
9645 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9646 warning_at (location, 0, "floating-point suffix %qs"
9647 " shadowed by implementation", suffix);
9648 }
9649 /* 17.6.3.3.5 */
9650 if (suffix[0] != '_'
9651 && !current_function_decl && !(friendp && !funcdef_flag))
9652 warning_at (location, OPT_Wliteral_suffix,
9653 "literal operator suffixes not preceded by %<_%>"
9654 " are reserved for future standardization");
9655 }
9656 else
9657 {
9658 error_at (location, "%qD must be a non-member function", decl);
9659 return NULL_TREE;
9660 }
9661 }
9662
9663 if (funcdef_flag)
9664 /* Make the init_value nonzero so pushdecl knows this is not
9665 tentative. error_mark_node is replaced later with the BLOCK. */
9666 DECL_INITIAL (decl) = error_mark_node;
9667
9668 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9669 TREE_NOTHROW (decl) = 1;
9670
9671 if (flag_openmp || flag_openmp_simd)
9672 {
9673 /* Adjust "omp declare simd" attributes. */
9674 tree ods = lookup_attribute ("omp declare simd", *attrlist);
9675 if (ods)
9676 {
9677 tree attr;
9678 for (attr = ods; attr;
9679 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9680 {
9681 if (TREE_CODE (type) == METHOD_TYPE)
9682 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9683 DECL_ARGUMENTS (decl), NULL);
9684 if (TREE_VALUE (attr) != NULL_TREE)
9685 {
9686 tree cl = TREE_VALUE (TREE_VALUE (attr));
9687 cl = c_omp_declare_simd_clauses_to_numbers
9688 (DECL_ARGUMENTS (decl), cl);
9689 if (cl)
9690 TREE_VALUE (TREE_VALUE (attr)) = cl;
9691 else
9692 TREE_VALUE (attr) = NULL_TREE;
9693 }
9694 }
9695 }
9696 }
9697
9698 /* Caller will do the rest of this. */
9699 if (check < 0)
9700 return decl;
9701
9702 if (ctype != NULL_TREE)
9703 grokclassfn (ctype, decl, flags);
9704
9705 /* 12.4/3 */
9706 if (cxx_dialect >= cxx11
9707 && DECL_DESTRUCTOR_P (decl)
9708 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9709 && !processing_template_decl)
9710 deduce_noexcept_on_destructor (decl);
9711
9712 decl = check_explicit_specialization (orig_declarator, decl,
9713 template_count,
9714 2 * funcdef_flag +
9715 4 * (friendp != 0) +
9716 8 * concept_p,
9717 *attrlist);
9718 if (decl == error_mark_node)
9719 return NULL_TREE;
9720
9721 if (DECL_STATIC_FUNCTION_P (decl))
9722 check_static_quals (decl, quals);
9723
9724 if (attrlist)
9725 {
9726 cplus_decl_attributes (&decl, *attrlist, 0);
9727 *attrlist = NULL_TREE;
9728 }
9729
9730 /* Check main's type after attributes have been applied. */
9731 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9732 {
9733 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9734 integer_type_node))
9735 {
9736 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9737 tree newtype;
9738 error_at (declspecs->locations[ds_type_spec],
9739 "%<::main%> must return %<int%>");
9740 newtype = build_function_type (integer_type_node, oldtypeargs);
9741 TREE_TYPE (decl) = newtype;
9742 }
9743 if (warn_main)
9744 check_main_parameter_types (decl);
9745 }
9746
9747 if (ctype != NULL_TREE && check)
9748 {
9749 tree old_decl = check_classfn (ctype, decl,
9750 (processing_template_decl
9751 > template_class_depth (ctype))
9752 ? current_template_parms
9753 : NULL_TREE);
9754
9755 if (old_decl == error_mark_node)
9756 return NULL_TREE;
9757
9758 if (old_decl)
9759 {
9760 tree ok;
9761 tree pushed_scope;
9762
9763 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9764 /* Because grokfndecl is always supposed to return a
9765 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9766 here. We depend on our callers to figure out that its
9767 really a template that's being returned. */
9768 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9769
9770 if (DECL_STATIC_FUNCTION_P (old_decl)
9771 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9772 {
9773 /* Remove the `this' parm added by grokclassfn. */
9774 revert_static_member_fn (decl);
9775 check_static_quals (decl, quals);
9776 }
9777 if (DECL_ARTIFICIAL (old_decl))
9778 {
9779 error ("definition of implicitly-declared %qD", old_decl);
9780 return NULL_TREE;
9781 }
9782 else if (DECL_DEFAULTED_FN (old_decl))
9783 {
9784 error ("definition of explicitly-defaulted %q+D", decl);
9785 inform (DECL_SOURCE_LOCATION (old_decl),
9786 "%q#D explicitly defaulted here", old_decl);
9787 return NULL_TREE;
9788 }
9789
9790 /* Since we've smashed OLD_DECL to its
9791 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9792 if (TREE_CODE (decl) == TEMPLATE_DECL)
9793 decl = DECL_TEMPLATE_RESULT (decl);
9794
9795 /* Attempt to merge the declarations. This can fail, in
9796 the case of some invalid specialization declarations. */
9797 pushed_scope = push_scope (ctype);
9798 ok = duplicate_decls (decl, old_decl, friendp);
9799 if (pushed_scope)
9800 pop_scope (pushed_scope);
9801 if (!ok)
9802 {
9803 error ("no %q#D member function declared in class %qT",
9804 decl, ctype);
9805 return NULL_TREE;
9806 }
9807 if (ok == error_mark_node)
9808 return NULL_TREE;
9809 return old_decl;
9810 }
9811 }
9812
9813 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9814 return NULL_TREE;
9815
9816 if (ctype == NULL_TREE || check)
9817 return decl;
9818
9819 if (virtualp)
9820 DECL_VIRTUAL_P (decl) = 1;
9821
9822 return decl;
9823 }
9824
9825 /* decl is a FUNCTION_DECL.
9826 specifiers are the parsed virt-specifiers.
9827
9828 Set flags to reflect the virt-specifiers.
9829
9830 Returns decl. */
9831
9832 static tree
9833 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9834 {
9835 if (decl == NULL_TREE)
9836 return decl;
9837 if (specifiers & VIRT_SPEC_OVERRIDE)
9838 DECL_OVERRIDE_P (decl) = 1;
9839 if (specifiers & VIRT_SPEC_FINAL)
9840 DECL_FINAL_P (decl) = 1;
9841 return decl;
9842 }
9843
9844 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9845 the linkage that DECL will receive in the object file. */
9846
9847 static void
9848 set_linkage_for_static_data_member (tree decl)
9849 {
9850 /* A static data member always has static storage duration and
9851 external linkage. Note that static data members are forbidden in
9852 local classes -- the only situation in which a class has
9853 non-external linkage. */
9854 TREE_PUBLIC (decl) = 1;
9855 TREE_STATIC (decl) = 1;
9856 /* For non-template classes, static data members are always put
9857 out in exactly those files where they are defined, just as
9858 with ordinary namespace-scope variables. */
9859 if (!processing_template_decl)
9860 DECL_INTERFACE_KNOWN (decl) = 1;
9861 }
9862
9863 /* Create a VAR_DECL named NAME with the indicated TYPE.
9864
9865 If SCOPE is non-NULL, it is the class type or namespace containing
9866 the variable. If SCOPE is NULL, the variable should is created in
9867 the innermost enclosing scope. */
9868
9869 static tree
9870 grokvardecl (tree type,
9871 tree name,
9872 tree orig_declarator,
9873 const cp_decl_specifier_seq *declspecs,
9874 int initialized,
9875 int type_quals,
9876 int inlinep,
9877 bool conceptp,
9878 int template_count,
9879 tree scope,
9880 location_t location)
9881 {
9882 tree decl;
9883 tree explicit_scope;
9884
9885 gcc_assert (!name || identifier_p (name));
9886
9887 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9888 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9889
9890 /* Compute the scope in which to place the variable, but remember
9891 whether or not that scope was explicitly specified by the user. */
9892 explicit_scope = scope;
9893 if (!scope)
9894 {
9895 /* An explicit "extern" specifier indicates a namespace-scope
9896 variable. */
9897 if (declspecs->storage_class == sc_extern)
9898 scope = current_decl_namespace ();
9899 else if (!at_function_scope_p ())
9900 scope = current_scope ();
9901 }
9902
9903 if (scope
9904 && (/* If the variable is a namespace-scope variable declared in a
9905 template, we need DECL_LANG_SPECIFIC. */
9906 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9907 /* Similarly for namespace-scope variables with language linkage
9908 other than C++. */
9909 || (TREE_CODE (scope) == NAMESPACE_DECL
9910 && current_lang_name != lang_name_cplusplus)
9911 /* Similarly for static data members. */
9912 || TYPE_P (scope)
9913 /* Similarly for explicit specializations. */
9914 || (orig_declarator
9915 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9916 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
9917 else
9918 decl = build_decl (location, VAR_DECL, name, type);
9919
9920 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9921 set_decl_namespace (decl, explicit_scope, 0);
9922 else
9923 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9924
9925 if (declspecs->storage_class == sc_extern)
9926 {
9927 DECL_THIS_EXTERN (decl) = 1;
9928 DECL_EXTERNAL (decl) = !initialized;
9929 }
9930
9931 if (DECL_CLASS_SCOPE_P (decl))
9932 {
9933 set_linkage_for_static_data_member (decl);
9934 /* This function is only called with out-of-class definitions. */
9935 DECL_EXTERNAL (decl) = 0;
9936 check_class_member_definition_namespace (decl);
9937 }
9938 /* At top level, either `static' or no s.c. makes a definition
9939 (perhaps tentative), and absence of `static' makes it public. */
9940 else if (toplevel_bindings_p ())
9941 {
9942 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9943 && (DECL_THIS_EXTERN (decl)
9944 || ! constp
9945 || volatilep
9946 || inlinep));
9947 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9948 }
9949 /* Not at top level, only `static' makes a static definition. */
9950 else
9951 {
9952 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9953 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9954 }
9955
9956 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9957 {
9958 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9959 {
9960 CP_DECL_THREAD_LOCAL_P (decl) = true;
9961 if (!processing_template_decl)
9962 set_decl_tls_model (decl, decl_default_tls_model (decl));
9963 }
9964 if (declspecs->gnu_thread_keyword_p)
9965 SET_DECL_GNU_TLS_P (decl);
9966 }
9967
9968 /* If the type of the decl has no linkage, make sure that we'll
9969 notice that in mark_used. */
9970 if (cxx_dialect > cxx98
9971 && decl_linkage (decl) != lk_none
9972 && DECL_LANG_SPECIFIC (decl) == NULL
9973 && !DECL_EXTERN_C_P (decl)
9974 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9975 retrofit_lang_decl (decl);
9976
9977 if (TREE_PUBLIC (decl))
9978 {
9979 /* [basic.link]: A name with no linkage (notably, the name of a class
9980 or enumeration declared in a local scope) shall not be used to
9981 declare an entity with linkage.
9982
9983 DR 757 relaxes this restriction for C++0x. */
9984 if (cxx_dialect < cxx11)
9985 no_linkage_error (decl);
9986 }
9987 else
9988 DECL_INTERFACE_KNOWN (decl) = 1;
9989
9990 if (DECL_NAME (decl)
9991 && MAIN_NAME_P (DECL_NAME (decl))
9992 && scope == global_namespace)
9993 error_at (DECL_SOURCE_LOCATION (decl),
9994 "cannot declare %<::main%> to be a global variable");
9995
9996 /* Check that the variable can be safely declared as a concept.
9997 Note that this also forbids explicit specializations. */
9998 if (conceptp)
9999 {
10000 if (!processing_template_decl)
10001 {
10002 error_at (declspecs->locations[ds_concept],
10003 "a non-template variable cannot be %<concept%>");
10004 return NULL_TREE;
10005 }
10006 else
10007 DECL_DECLARED_CONCEPT_P (decl) = true;
10008 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10009 error_at (declspecs->locations[ds_type_spec],
10010 "concept must have type %<bool%>");
10011 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10012 {
10013 error_at (location, "a variable concept cannot be constrained");
10014 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10015 }
10016 }
10017 else if (flag_concepts
10018 && processing_template_decl > template_class_depth (scope))
10019 {
10020 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10021 tree ci = build_constraints (reqs, NULL_TREE);
10022
10023 set_constraints (decl, ci);
10024 }
10025
10026 // Handle explicit specializations and instantiations of variable templates.
10027 if (orig_declarator)
10028 decl = check_explicit_specialization (orig_declarator, decl,
10029 template_count, conceptp * 8);
10030
10031 return decl != error_mark_node ? decl : NULL_TREE;
10032 }
10033
10034 /* Create and return a canonical pointer to member function type, for
10035 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10036
10037 tree
10038 build_ptrmemfunc_type (tree type)
10039 {
10040 tree field, fields;
10041 tree t;
10042
10043 if (type == error_mark_node)
10044 return type;
10045
10046 /* Make sure that we always have the unqualified pointer-to-member
10047 type first. */
10048 if (cp_cv_quals quals = cp_type_quals (type))
10049 {
10050 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10051 return cp_build_qualified_type (unqual, quals);
10052 }
10053
10054 /* If a canonical type already exists for this type, use it. We use
10055 this method instead of type_hash_canon, because it only does a
10056 simple equality check on the list of field members. */
10057
10058 t = TYPE_PTRMEMFUNC_TYPE (type);
10059 if (t)
10060 return t;
10061
10062 t = make_node (RECORD_TYPE);
10063
10064 /* Let the front end know this is a pointer to member function. */
10065 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10066
10067 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10068 DECL_NONADDRESSABLE_P (field) = 1;
10069 fields = field;
10070
10071 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10072 delta_type_node);
10073 DECL_NONADDRESSABLE_P (field) = 1;
10074 DECL_CHAIN (field) = fields;
10075 fields = field;
10076
10077 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10078
10079 /* Zap out the name so that the back end will give us the debugging
10080 information for this anonymous RECORD_TYPE. */
10081 TYPE_NAME (t) = NULL_TREE;
10082
10083 /* Cache this pointer-to-member type so that we can find it again
10084 later. */
10085 TYPE_PTRMEMFUNC_TYPE (type) = t;
10086
10087 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10088 SET_TYPE_STRUCTURAL_EQUALITY (t);
10089 else if (TYPE_CANONICAL (type) != type)
10090 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10091
10092 return t;
10093 }
10094
10095 /* Create and return a pointer to data member type. */
10096
10097 tree
10098 build_ptrmem_type (tree class_type, tree member_type)
10099 {
10100 if (TREE_CODE (member_type) == METHOD_TYPE)
10101 {
10102 cp_cv_quals quals = type_memfn_quals (member_type);
10103 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10104 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10105 return build_ptrmemfunc_type (build_pointer_type (member_type));
10106 }
10107 else
10108 {
10109 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10110 return build_offset_type (class_type, member_type);
10111 }
10112 }
10113
10114 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10115 Check to see that the definition is valid. Issue appropriate error
10116 messages. */
10117
10118 static void
10119 check_static_variable_definition (tree decl, tree type)
10120 {
10121 /* Avoid redundant diagnostics on out-of-class definitions. */
10122 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10123 ;
10124 /* Can't check yet if we don't know the type. */
10125 else if (dependent_type_p (type))
10126 ;
10127 /* If DECL is declared constexpr, we'll do the appropriate checks
10128 in check_initializer. Similarly for inline static data members. */
10129 else if (DECL_P (decl)
10130 && (DECL_DECLARED_CONSTEXPR_P (decl)
10131 || undeduced_auto_decl (decl)
10132 || DECL_VAR_DECLARED_INLINE_P (decl)))
10133 ;
10134 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10135 {
10136 if (!COMPLETE_TYPE_P (type))
10137 error_at (DECL_SOURCE_LOCATION (decl),
10138 "in-class initialization of static data member %q#D of "
10139 "incomplete type", decl);
10140 else if (literal_type_p (type))
10141 permerror (DECL_SOURCE_LOCATION (decl),
10142 "%<constexpr%> needed for in-class initialization of "
10143 "static data member %q#D of non-integral type", decl);
10144 else
10145 error_at (DECL_SOURCE_LOCATION (decl),
10146 "in-class initialization of static data member %q#D of "
10147 "non-literal type", decl);
10148 }
10149 /* Motion 10 at San Diego: If a static const integral data member is
10150 initialized with an integral constant expression, the initializer
10151 may appear either in the declaration (within the class), or in
10152 the definition, but not both. If it appears in the class, the
10153 member is a member constant. The file-scope definition is always
10154 required. */
10155 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10156 error_at (DECL_SOURCE_LOCATION (decl),
10157 "invalid in-class initialization of static data member "
10158 "of non-integral type %qT",
10159 type);
10160 else if (!CP_TYPE_CONST_P (type))
10161 error_at (DECL_SOURCE_LOCATION (decl),
10162 "ISO C++ forbids in-class initialization of non-const "
10163 "static member %qD",
10164 decl);
10165 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10166 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10167 "ISO C++ forbids initialization of member constant "
10168 "%qD of non-integral type %qT", decl, type);
10169 }
10170
10171 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10172 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10173 expressions out into temporary variables so that walk_tree doesn't
10174 step into them (c++/15764). */
10175
10176 static tree
10177 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10178 {
10179 hash_set<tree> *pset = (hash_set<tree> *)data;
10180 tree expr = *expr_p;
10181 if (TREE_CODE (expr) == SAVE_EXPR)
10182 {
10183 tree op = TREE_OPERAND (expr, 0);
10184 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10185 if (TREE_SIDE_EFFECTS (op))
10186 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10187 *walk_subtrees = 0;
10188 }
10189 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10190 *walk_subtrees = 0;
10191 return NULL;
10192 }
10193
10194 /* Entry point for the above. */
10195
10196 static void
10197 stabilize_vla_size (tree size)
10198 {
10199 hash_set<tree> pset;
10200 /* Break out any function calls into temporary variables. */
10201 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10202 }
10203
10204 /* Reduce a SIZEOF_EXPR to its value. */
10205
10206 tree
10207 fold_sizeof_expr (tree t)
10208 {
10209 tree r;
10210 if (SIZEOF_EXPR_TYPE_P (t))
10211 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10212 TREE_TYPE (TREE_OPERAND (t, 0)),
10213 SIZEOF_EXPR, false, false);
10214 else if (TYPE_P (TREE_OPERAND (t, 0)))
10215 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10216 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10217 false, false);
10218 else
10219 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10220 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10221 false);
10222 if (r == error_mark_node)
10223 r = size_one_node;
10224 return r;
10225 }
10226
10227 /* Given the SIZE (i.e., number of elements) in an array, compute
10228 an appropriate index type for the array. If non-NULL, NAME is
10229 the name of the entity being declared. */
10230
10231 static tree
10232 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10233 tsubst_flags_t complain)
10234 {
10235 if (error_operand_p (size))
10236 return error_mark_node;
10237
10238 /* The type of the index being computed. */
10239 tree itype;
10240
10241 /* The original numeric size as seen in the source code before
10242 conversion to size_t. */
10243 tree origsize = size;
10244
10245 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
10246
10247 if (!type_dependent_expression_p (size))
10248 {
10249 origsize = size = mark_rvalue_use (size);
10250
10251 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
10252 && TREE_SIDE_EFFECTS (size))
10253 /* In C++98, we mark a non-constant array bound with a magic
10254 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10255 else
10256 {
10257 size = instantiate_non_dependent_expr_sfinae (size, complain);
10258 size = build_converted_constant_expr (size_type_node, size, complain);
10259 /* Pedantically a constant expression is required here and so
10260 __builtin_is_constant_evaluated () should fold to true if it
10261 is successfully folded into a constant. */
10262 size = maybe_constant_value (size, NULL_TREE,
10263 /*manifestly_const_eval=*/true);
10264
10265 if (!TREE_CONSTANT (size))
10266 size = origsize;
10267 }
10268
10269 if (error_operand_p (size))
10270 return error_mark_node;
10271
10272 /* The array bound must be an integer type. */
10273 tree type = TREE_TYPE (size);
10274 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
10275 {
10276 if (!(complain & tf_error))
10277 return error_mark_node;
10278 if (name)
10279 error_at (loc, "size of array %qD has non-integral type %qT",
10280 name, type);
10281 else
10282 error_at (loc, "size of array has non-integral type %qT", type);
10283 size = integer_one_node;
10284 }
10285 }
10286
10287 /* A type is dependent if it is...an array type constructed from any
10288 dependent type or whose size is specified by a constant expression
10289 that is value-dependent. */
10290 /* We can only call value_dependent_expression_p on integral constant
10291 expressions; treat non-constant expressions as dependent, too. */
10292 if (processing_template_decl
10293 && (type_dependent_expression_p (size)
10294 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
10295 {
10296 /* We cannot do any checking for a SIZE that isn't known to be
10297 constant. Just build the index type and mark that it requires
10298 structural equality checks. */
10299 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
10300 size, size_one_node));
10301 TYPE_DEPENDENT_P (itype) = 1;
10302 TYPE_DEPENDENT_P_VALID (itype) = 1;
10303 SET_TYPE_STRUCTURAL_EQUALITY (itype);
10304 return itype;
10305 }
10306
10307 if (TREE_CODE (size) != INTEGER_CST)
10308 {
10309 tree folded = cp_fully_fold (size);
10310 if (TREE_CODE (folded) == INTEGER_CST)
10311 {
10312 if (name)
10313 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
10314 "integral constant-expression", name);
10315 else
10316 pedwarn (loc, OPT_Wpedantic,
10317 "size of array is not an integral constant-expression");
10318 }
10319 /* Use the folded result for VLAs, too; it will have resolved
10320 SIZEOF_EXPR. */
10321 size = folded;
10322 }
10323
10324 /* Normally, the array-bound will be a constant. */
10325 if (TREE_CODE (size) == INTEGER_CST)
10326 {
10327 /* The size to use in diagnostics that reflects the constant
10328 size used in the source, rather than SIZE massaged above. */
10329 tree diagsize = size;
10330
10331 /* If the original size before conversion to size_t was signed
10332 and negative, convert it to ssizetype to restore the sign. */
10333 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10334 && TREE_CODE (size) == INTEGER_CST
10335 && tree_int_cst_sign_bit (size))
10336 {
10337 diagsize = fold_convert (ssizetype, size);
10338
10339 /* Clear the overflow bit that may have been set as a result
10340 of the conversion from the sizetype of the new size to
10341 ssizetype. */
10342 TREE_OVERFLOW (diagsize) = false;
10343 }
10344
10345 /* Verify that the array has a positive number of elements
10346 and issue the appropriate diagnostic if it doesn't. */
10347 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10348 {
10349 if (!(complain & tf_error))
10350 return error_mark_node;
10351 size = integer_one_node;
10352 }
10353 /* As an extension we allow zero-sized arrays. */
10354 else if (integer_zerop (size))
10355 {
10356 if (!(complain & tf_error))
10357 /* We must fail if performing argument deduction (as
10358 indicated by the state of complain), so that
10359 another substitution can be found. */
10360 return error_mark_node;
10361 else if (name)
10362 pedwarn (loc, OPT_Wpedantic,
10363 "ISO C++ forbids zero-size array %qD", name);
10364 else
10365 pedwarn (loc, OPT_Wpedantic,
10366 "ISO C++ forbids zero-size array");
10367 }
10368 }
10369 else if (TREE_CONSTANT (size)
10370 /* We don't allow VLAs at non-function scopes, or during
10371 tentative template substitution. */
10372 || !at_function_scope_p ()
10373 || !(complain & tf_error))
10374 {
10375 if (!(complain & tf_error))
10376 return error_mark_node;
10377 /* `(int) &fn' is not a valid array bound. */
10378 if (name)
10379 error_at (loc,
10380 "size of array %qD is not an integral constant-expression",
10381 name);
10382 else
10383 error_at (loc, "size of array is not an integral constant-expression");
10384 size = integer_one_node;
10385 }
10386 else if (pedantic && warn_vla != 0)
10387 {
10388 if (name)
10389 pedwarn (name_loc, OPT_Wvla,
10390 "ISO C++ forbids variable length array %qD", name);
10391 else
10392 pedwarn (input_location, OPT_Wvla,
10393 "ISO C++ forbids variable length array");
10394 }
10395 else if (warn_vla > 0)
10396 {
10397 if (name)
10398 warning_at (name_loc, OPT_Wvla,
10399 "variable length array %qD is used", name);
10400 else
10401 warning (OPT_Wvla,
10402 "variable length array is used");
10403 }
10404
10405 if (processing_template_decl && !TREE_CONSTANT (size))
10406 /* A variable sized array. */
10407 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
10408 else
10409 {
10410 /* Compute the index of the largest element in the array. It is
10411 one less than the number of elements in the array. We save
10412 and restore PROCESSING_TEMPLATE_DECL so that computations in
10413 cp_build_binary_op will be appropriately folded. */
10414 {
10415 processing_template_decl_sentinel s;
10416 itype = cp_build_binary_op (input_location,
10417 MINUS_EXPR,
10418 cp_convert (ssizetype, size, complain),
10419 cp_convert (ssizetype, integer_one_node,
10420 complain),
10421 complain);
10422 itype = maybe_constant_value (itype);
10423 }
10424
10425 if (!TREE_CONSTANT (itype))
10426 {
10427 /* A variable sized array. */
10428 itype = variable_size (itype);
10429
10430 stabilize_vla_size (itype);
10431
10432 if (sanitize_flags_p (SANITIZE_VLA)
10433 && current_function_decl != NULL_TREE)
10434 {
10435 /* We have to add 1 -- in the ubsan routine we generate
10436 LE_EXPR rather than LT_EXPR. */
10437 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
10438 build_one_cst (TREE_TYPE (itype)));
10439 t = ubsan_instrument_vla (input_location, t);
10440 finish_expr_stmt (t);
10441 }
10442 }
10443 /* Make sure that there was no overflow when creating to a signed
10444 index type. (For example, on a 32-bit machine, an array with
10445 size 2^32 - 1 is too big.) */
10446 else if (TREE_CODE (itype) == INTEGER_CST
10447 && TREE_OVERFLOW (itype))
10448 {
10449 if (!(complain & tf_error))
10450 return error_mark_node;
10451 error ("overflow in array dimension");
10452 TREE_OVERFLOW (itype) = 0;
10453 }
10454 }
10455
10456 /* Create and return the appropriate index type. */
10457 itype = build_index_type (itype);
10458
10459 /* If the index type were dependent, we would have returned early, so
10460 remember that it isn't. */
10461 TYPE_DEPENDENT_P (itype) = 0;
10462 TYPE_DEPENDENT_P_VALID (itype) = 1;
10463 return itype;
10464 }
10465
10466 tree
10467 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
10468 {
10469 return compute_array_index_type_loc (input_location, name, size, complain);
10470 }
10471
10472 /* Returns the scope (if any) in which the entity declared by
10473 DECLARATOR will be located. If the entity was declared with an
10474 unqualified name, NULL_TREE is returned. */
10475
10476 tree
10477 get_scope_of_declarator (const cp_declarator *declarator)
10478 {
10479 while (declarator && declarator->kind != cdk_id)
10480 declarator = declarator->declarator;
10481
10482 /* If the declarator-id is a SCOPE_REF, the scope in which the
10483 declaration occurs is the first operand. */
10484 if (declarator
10485 && declarator->u.id.qualifying_scope)
10486 return declarator->u.id.qualifying_scope;
10487
10488 /* Otherwise, the declarator is not a qualified name; the entity will
10489 be declared in the current scope. */
10490 return NULL_TREE;
10491 }
10492
10493 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10494 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10495 with this type. */
10496
10497 static tree
10498 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
10499 {
10500 tree itype = NULL_TREE;
10501
10502 /* If things have already gone awry, bail now. */
10503 if (type == error_mark_node || size == error_mark_node)
10504 return error_mark_node;
10505
10506 /* 8.3.4/1: If the type of the identifier of D contains the auto
10507 type-specifier, the program is ill-formed. */
10508 if (type_uses_auto (type))
10509 {
10510 if (name)
10511 error_at (loc, "%qD declared as array of %qT", name, type);
10512 else
10513 error ("creating array of %qT", type);
10514 return error_mark_node;
10515 }
10516
10517 /* If there are some types which cannot be array elements,
10518 issue an error-message and return. */
10519 switch (TREE_CODE (type))
10520 {
10521 case VOID_TYPE:
10522 if (name)
10523 error_at (loc, "declaration of %qD as array of void", name);
10524 else
10525 error ("creating array of void");
10526 return error_mark_node;
10527
10528 case FUNCTION_TYPE:
10529 if (name)
10530 error_at (loc, "declaration of %qD as array of functions", name);
10531 else
10532 error ("creating array of functions");
10533 return error_mark_node;
10534
10535 case REFERENCE_TYPE:
10536 if (name)
10537 error_at (loc, "declaration of %qD as array of references", name);
10538 else
10539 error ("creating array of references");
10540 return error_mark_node;
10541
10542 case METHOD_TYPE:
10543 if (name)
10544 error_at (loc, "declaration of %qD as array of function members",
10545 name);
10546 else
10547 error ("creating array of function members");
10548 return error_mark_node;
10549
10550 default:
10551 break;
10552 }
10553
10554 if (!verify_type_context (name ? loc : input_location,
10555 TCTX_ARRAY_ELEMENT, type))
10556 return error_mark_node;
10557
10558 /* [dcl.array]
10559
10560 The constant expressions that specify the bounds of the arrays
10561 can be omitted only for the first member of the sequence. */
10562 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10563 {
10564 if (name)
10565 error_at (loc, "declaration of %qD as multidimensional array must "
10566 "have bounds for all dimensions except the first",
10567 name);
10568 else
10569 error ("multidimensional array must have bounds for all "
10570 "dimensions except the first");
10571
10572 return error_mark_node;
10573 }
10574
10575 /* Figure out the index type for the array. */
10576 if (size)
10577 itype = compute_array_index_type_loc (loc, name, size,
10578 tf_warning_or_error);
10579
10580 /* [dcl.array]
10581 T is called the array element type; this type shall not be [...] an
10582 abstract class type. */
10583 abstract_virtuals_error (name, type);
10584
10585 return build_cplus_array_type (type, itype);
10586 }
10587
10588 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10589
10590 static location_t
10591 min_location (location_t loca, location_t locb)
10592 {
10593 if (loca == UNKNOWN_LOCATION
10594 || (locb != UNKNOWN_LOCATION
10595 && linemap_location_before_p (line_table, locb, loca)))
10596 return locb;
10597 return loca;
10598 }
10599
10600 /* Returns the smallest location != UNKNOWN_LOCATION among the
10601 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10602 and LOCATIONS[ds_restrict]. */
10603
10604 static location_t
10605 smallest_type_quals_location (int type_quals, const location_t* locations)
10606 {
10607 location_t loc = UNKNOWN_LOCATION;
10608
10609 if (type_quals & TYPE_QUAL_CONST)
10610 loc = locations[ds_const];
10611
10612 if (type_quals & TYPE_QUAL_VOLATILE)
10613 loc = min_location (loc, locations[ds_volatile]);
10614
10615 if (type_quals & TYPE_QUAL_RESTRICT)
10616 loc = min_location (loc, locations[ds_restrict]);
10617
10618 return loc;
10619 }
10620
10621 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10622
10623 static location_t
10624 smallest_type_location (int type_quals, const location_t* locations)
10625 {
10626 location_t loc = smallest_type_quals_location (type_quals, locations);
10627 return min_location (loc, locations[ds_type_spec]);
10628 }
10629
10630 static location_t
10631 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10632 {
10633 int type_quals = get_type_quals (declspecs);
10634 return smallest_type_location (type_quals, declspecs->locations);
10635 }
10636
10637 /* Check that it's OK to declare a function with the indicated TYPE
10638 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10639 that this function is. OPTYPE is the type given in a conversion
10640 operator declaration, or the class type for a constructor/destructor.
10641 Returns the actual return type of the function; that may be different
10642 than TYPE if an error occurs, or for certain special functions. */
10643
10644 static tree
10645 check_special_function_return_type (special_function_kind sfk,
10646 tree type,
10647 tree optype,
10648 int type_quals,
10649 const location_t* locations)
10650 {
10651 switch (sfk)
10652 {
10653 case sfk_constructor:
10654 if (type)
10655 error_at (smallest_type_location (type_quals, locations),
10656 "return type specification for constructor invalid");
10657 else if (type_quals != TYPE_UNQUALIFIED)
10658 error_at (smallest_type_quals_location (type_quals, locations),
10659 "qualifiers are not allowed on constructor declaration");
10660
10661 if (targetm.cxx.cdtor_returns_this ())
10662 type = build_pointer_type (optype);
10663 else
10664 type = void_type_node;
10665 break;
10666
10667 case sfk_destructor:
10668 if (type)
10669 error_at (smallest_type_location (type_quals, locations),
10670 "return type specification for destructor invalid");
10671 else if (type_quals != TYPE_UNQUALIFIED)
10672 error_at (smallest_type_quals_location (type_quals, locations),
10673 "qualifiers are not allowed on destructor declaration");
10674
10675 /* We can't use the proper return type here because we run into
10676 problems with ambiguous bases and covariant returns. */
10677 if (targetm.cxx.cdtor_returns_this ())
10678 type = build_pointer_type (void_type_node);
10679 else
10680 type = void_type_node;
10681 break;
10682
10683 case sfk_conversion:
10684 if (type)
10685 error_at (smallest_type_location (type_quals, locations),
10686 "return type specified for %<operator %T%>", optype);
10687 else if (type_quals != TYPE_UNQUALIFIED)
10688 error_at (smallest_type_quals_location (type_quals, locations),
10689 "qualifiers are not allowed on declaration of "
10690 "%<operator %T%>", optype);
10691
10692 type = optype;
10693 break;
10694
10695 case sfk_deduction_guide:
10696 if (type)
10697 error_at (smallest_type_location (type_quals, locations),
10698 "return type specified for deduction guide");
10699 else if (type_quals != TYPE_UNQUALIFIED)
10700 error_at (smallest_type_quals_location (type_quals, locations),
10701 "qualifiers are not allowed on declaration of "
10702 "deduction guide");
10703 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10704 {
10705 error ("template template parameter %qT in declaration of "
10706 "deduction guide", optype);
10707 type = error_mark_node;
10708 }
10709 else
10710 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10711 for (int i = 0; i < ds_last; ++i)
10712 if (i != ds_explicit && locations[i])
10713 error_at (locations[i],
10714 "%<decl-specifier%> in declaration of deduction guide");
10715 break;
10716
10717 default:
10718 gcc_unreachable ();
10719 }
10720
10721 return type;
10722 }
10723
10724 /* A variable or data member (whose unqualified name is IDENTIFIER)
10725 has been declared with the indicated TYPE. If the TYPE is not
10726 acceptable, issue an error message and return a type to use for
10727 error-recovery purposes. */
10728
10729 tree
10730 check_var_type (tree identifier, tree type, location_t loc)
10731 {
10732 if (VOID_TYPE_P (type))
10733 {
10734 if (!identifier)
10735 error_at (loc, "unnamed variable or field declared void");
10736 else if (identifier_p (identifier))
10737 {
10738 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10739 error_at (loc, "variable or field %qE declared void",
10740 identifier);
10741 }
10742 else
10743 error_at (loc, "variable or field declared void");
10744 type = error_mark_node;
10745 }
10746
10747 return type;
10748 }
10749
10750 /* Handle declaring DECL as an inline variable. */
10751
10752 static void
10753 mark_inline_variable (tree decl, location_t loc)
10754 {
10755 bool inlinep = true;
10756 if (! toplevel_bindings_p ())
10757 {
10758 error_at (loc, "%<inline%> specifier invalid for variable "
10759 "%qD declared at block scope", decl);
10760 inlinep = false;
10761 }
10762 else if (cxx_dialect < cxx17)
10763 pedwarn (loc, 0, "inline variables are only available "
10764 "with %<-std=c++17%> or %<-std=gnu++17%>");
10765 if (inlinep)
10766 {
10767 retrofit_lang_decl (decl);
10768 SET_DECL_VAR_DECLARED_INLINE_P (decl);
10769 }
10770 }
10771
10772
10773 /* Assign a typedef-given name to a class or enumeration type declared
10774 as anonymous at first. This was split out of grokdeclarator
10775 because it is also used in libcc1. */
10776
10777 void
10778 name_unnamed_type (tree type, tree decl)
10779 {
10780 gcc_assert (TYPE_UNNAMED_P (type));
10781
10782 /* Replace the anonymous name with the real name everywhere. */
10783 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10784 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
10785 /* We do not rename the debug info representing the unnamed
10786 tagged type because the standard says in [dcl.typedef] that
10787 the naming applies only for linkage purposes. */
10788 /*debug_hooks->set_name (t, decl);*/
10789 TYPE_NAME (t) = decl;
10790
10791 /* If this is a typedef within a template class, the nested
10792 type is a (non-primary) template. The name for the
10793 template needs updating as well. */
10794 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10795 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10796 = TYPE_IDENTIFIER (type);
10797
10798 /* Adjust linkage now that we aren't unnamed anymore. */
10799 reset_type_linkage (type);
10800
10801 /* FIXME remangle member functions; member functions of a
10802 type with external linkage have external linkage. */
10803
10804 /* Check that our job is done, and that it would fail if we
10805 attempted to do it again. */
10806 gcc_assert (!TYPE_UNNAMED_P (type));
10807 }
10808
10809 /* Given declspecs and a declarator (abstract or otherwise), determine
10810 the name and type of the object declared and construct a DECL node
10811 for it.
10812
10813 DECLSPECS points to the representation of declaration-specifier
10814 sequence that precedes declarator.
10815
10816 DECL_CONTEXT says which syntactic context this declaration is in:
10817 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10818 FUNCDEF for a function definition. Like NORMAL but a few different
10819 error messages in each case. Return value may be zero meaning
10820 this definition is too screwy to try to parse.
10821 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10822 handle member functions (which have FIELD context).
10823 Return value may be zero meaning this definition is too screwy to
10824 try to parse.
10825 PARM for a parameter declaration (either within a function prototype
10826 or before a function body). Make a PARM_DECL, or return void_type_node.
10827 TPARM for a template parameter declaration.
10828 CATCHPARM for a parameter declaration before a catch clause.
10829 TYPENAME if for a typename (in a cast or sizeof).
10830 Don't make a DECL node; just return the ..._TYPE node.
10831 FIELD for a struct or union field; make a FIELD_DECL.
10832 BITFIELD for a field with specified width.
10833
10834 INITIALIZED is as for start_decl.
10835
10836 ATTRLIST is a pointer to the list of attributes, which may be NULL
10837 if there are none; *ATTRLIST may be modified if attributes from inside
10838 the declarator should be applied to the declaration.
10839
10840 When this function is called, scoping variables (such as
10841 CURRENT_CLASS_TYPE) should reflect the scope in which the
10842 declaration occurs, not the scope in which the new declaration will
10843 be placed. For example, on:
10844
10845 void S::f() { ... }
10846
10847 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10848 should not be `S'.
10849
10850 Returns a DECL (if a declarator is present), a TYPE (if there is no
10851 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10852 error occurs. */
10853
10854 tree
10855 grokdeclarator (const cp_declarator *declarator,
10856 cp_decl_specifier_seq *declspecs,
10857 enum decl_context decl_context,
10858 int initialized,
10859 tree* attrlist)
10860 {
10861 tree type = NULL_TREE;
10862 int longlong = 0;
10863 int explicit_intN = 0;
10864 int int_n_alt = 0;
10865 int virtualp, explicitp, friendp, inlinep, staticp;
10866 int explicit_int = 0;
10867 int explicit_char = 0;
10868 int defaulted_int = 0;
10869
10870 tree typedef_decl = NULL_TREE;
10871 const char *name = NULL;
10872 tree typedef_type = NULL_TREE;
10873 /* True if this declarator is a function definition. */
10874 bool funcdef_flag = false;
10875 cp_declarator_kind innermost_code = cdk_error;
10876 int bitfield = 0;
10877 #if 0
10878 /* See the code below that used this. */
10879 tree decl_attr = NULL_TREE;
10880 #endif
10881
10882 /* Keep track of what sort of function is being processed
10883 so that we can warn about default return values, or explicit
10884 return values which do not match prescribed defaults. */
10885 special_function_kind sfk = sfk_none;
10886
10887 tree dname = NULL_TREE;
10888 tree ctor_return_type = NULL_TREE;
10889 enum overload_flags flags = NO_SPECIAL;
10890 /* cv-qualifiers that apply to the declarator, for a declaration of
10891 a member function. */
10892 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
10893 /* virt-specifiers that apply to the declarator, for a declaration of
10894 a member function. */
10895 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
10896 /* ref-qualifier that applies to the declarator, for a declaration of
10897 a member function. */
10898 cp_ref_qualifier rqual = REF_QUAL_NONE;
10899 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
10900 int type_quals = get_type_quals (declspecs);
10901 tree raises = NULL_TREE;
10902 int template_count = 0;
10903 tree returned_attrs = NULL_TREE;
10904 tree parms = NULL_TREE;
10905 const cp_declarator *id_declarator;
10906 /* The unqualified name of the declarator; either an
10907 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
10908 tree unqualified_id;
10909 /* The class type, if any, in which this entity is located,
10910 or NULL_TREE if none. Note that this value may be different from
10911 the current class type; for example if an attempt is made to declare
10912 "A::f" inside "B", this value will be "A". */
10913 tree ctype = current_class_type;
10914 /* The NAMESPACE_DECL for the namespace in which this entity is
10915 located. If an unqualified name is used to declare the entity,
10916 this value will be NULL_TREE, even if the entity is located at
10917 namespace scope. */
10918 tree in_namespace = NULL_TREE;
10919 cp_storage_class storage_class;
10920 bool unsigned_p, signed_p, short_p, long_p, thread_p;
10921 bool type_was_error_mark_node = false;
10922 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
10923 bool template_type_arg = false;
10924 bool template_parm_flag = false;
10925 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
10926 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
10927 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
10928 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
10929 bool late_return_type_p = false;
10930 bool array_parameter_p = false;
10931 tree reqs = NULL_TREE;
10932
10933 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
10934 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
10935 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
10936 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
10937 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
10938 explicit_intN = declspecs->explicit_intN_p;
10939 int_n_alt = declspecs->int_n_alt;
10940 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
10941
10942 // Was concept_p specified? Note that ds_concept
10943 // implies ds_constexpr!
10944 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
10945 if (concept_p)
10946 constexpr_p = true;
10947
10948 if (decl_context == FUNCDEF)
10949 funcdef_flag = true, decl_context = NORMAL;
10950 else if (decl_context == MEMFUNCDEF)
10951 funcdef_flag = true, decl_context = FIELD;
10952 else if (decl_context == BITFIELD)
10953 bitfield = 1, decl_context = FIELD;
10954 else if (decl_context == TEMPLATE_TYPE_ARG)
10955 template_type_arg = true, decl_context = TYPENAME;
10956 else if (decl_context == TPARM)
10957 template_parm_flag = true, decl_context = PARM;
10958
10959 if (initialized > 1)
10960 funcdef_flag = true;
10961
10962 location_t typespec_loc = smallest_type_location (type_quals,
10963 declspecs->locations);
10964 if (typespec_loc == UNKNOWN_LOCATION)
10965 typespec_loc = input_location;
10966
10967 location_t id_loc = declarator ? declarator->id_loc : input_location;
10968 if (id_loc == UNKNOWN_LOCATION)
10969 id_loc = input_location;
10970
10971 /* Look inside a declarator for the name being declared
10972 and get it as a string, for an error message. */
10973 for (id_declarator = declarator;
10974 id_declarator;
10975 id_declarator = id_declarator->declarator)
10976 {
10977 if (id_declarator->kind != cdk_id)
10978 innermost_code = id_declarator->kind;
10979
10980 switch (id_declarator->kind)
10981 {
10982 case cdk_function:
10983 if (id_declarator->declarator
10984 && id_declarator->declarator->kind == cdk_id)
10985 {
10986 sfk = id_declarator->declarator->u.id.sfk;
10987 if (sfk == sfk_destructor)
10988 flags = DTOR_FLAG;
10989 }
10990 break;
10991
10992 case cdk_id:
10993 {
10994 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
10995 tree decl = id_declarator->u.id.unqualified_name;
10996 if (!decl)
10997 break;
10998 if (qualifying_scope)
10999 {
11000 if (check_for_bare_parameter_packs (qualifying_scope,
11001 id_declarator->id_loc))
11002 return error_mark_node;
11003 if (at_function_scope_p ())
11004 {
11005 /* [dcl.meaning]
11006
11007 A declarator-id shall not be qualified except
11008 for ...
11009
11010 None of the cases are permitted in block
11011 scope. */
11012 if (qualifying_scope == global_namespace)
11013 error ("invalid use of qualified-name %<::%D%>",
11014 decl);
11015 else if (TYPE_P (qualifying_scope))
11016 error ("invalid use of qualified-name %<%T::%D%>",
11017 qualifying_scope, decl);
11018 else
11019 error ("invalid use of qualified-name %<%D::%D%>",
11020 qualifying_scope, decl);
11021 return error_mark_node;
11022 }
11023 else if (TYPE_P (qualifying_scope))
11024 {
11025 ctype = qualifying_scope;
11026 if (!MAYBE_CLASS_TYPE_P (ctype))
11027 {
11028 error_at (id_declarator->id_loc,
11029 "%q#T is not a class or namespace", ctype);
11030 ctype = NULL_TREE;
11031 }
11032 else if (innermost_code != cdk_function
11033 && current_class_type
11034 && !uniquely_derived_from_p (ctype,
11035 current_class_type))
11036 {
11037 error_at (id_declarator->id_loc,
11038 "invalid use of qualified-name %<%T::%D%>",
11039 qualifying_scope, decl);
11040 return error_mark_node;
11041 }
11042 }
11043 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11044 in_namespace = qualifying_scope;
11045 }
11046 switch (TREE_CODE (decl))
11047 {
11048 case BIT_NOT_EXPR:
11049 {
11050 if (innermost_code != cdk_function)
11051 {
11052 error_at (EXPR_LOCATION (decl),
11053 "declaration of %qE as non-function", decl);
11054 return error_mark_node;
11055 }
11056 else if (!qualifying_scope
11057 && !(current_class_type && at_class_scope_p ()))
11058 {
11059 error_at (EXPR_LOCATION (decl),
11060 "declaration of %qE as non-member", decl);
11061 return error_mark_node;
11062 }
11063
11064 tree type = TREE_OPERAND (decl, 0);
11065 if (TYPE_P (type))
11066 type = constructor_name (type);
11067 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11068 dname = decl;
11069 }
11070 break;
11071
11072 case TEMPLATE_ID_EXPR:
11073 {
11074 tree fns = TREE_OPERAND (decl, 0);
11075
11076 dname = fns;
11077 if (!identifier_p (dname))
11078 dname = OVL_NAME (dname);
11079 }
11080 /* Fall through. */
11081
11082 case IDENTIFIER_NODE:
11083 if (identifier_p (decl))
11084 dname = decl;
11085
11086 if (IDENTIFIER_KEYWORD_P (dname))
11087 {
11088 error ("declarator-id missing; using reserved word %qD",
11089 dname);
11090 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11091 }
11092 else if (!IDENTIFIER_CONV_OP_P (dname))
11093 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11094 else
11095 {
11096 gcc_assert (flags == NO_SPECIAL);
11097 flags = TYPENAME_FLAG;
11098 sfk = sfk_conversion;
11099 tree glob = get_global_binding (dname);
11100 if (glob && TREE_CODE (glob) == TYPE_DECL)
11101 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11102 else
11103 name = "<invalid operator>";
11104 }
11105 break;
11106
11107 default:
11108 gcc_unreachable ();
11109 }
11110 break;
11111 }
11112
11113 case cdk_array:
11114 case cdk_pointer:
11115 case cdk_reference:
11116 case cdk_ptrmem:
11117 break;
11118
11119 case cdk_decomp:
11120 name = "structured binding";
11121 break;
11122
11123 case cdk_error:
11124 return error_mark_node;
11125
11126 default:
11127 gcc_unreachable ();
11128 }
11129 if (id_declarator->kind == cdk_id)
11130 break;
11131 }
11132
11133 /* [dcl.fct.edf]
11134
11135 The declarator in a function-definition shall have the form
11136 D1 ( parameter-declaration-clause) ... */
11137 if (funcdef_flag && innermost_code != cdk_function)
11138 {
11139 error_at (id_loc, "function definition does not declare parameters");
11140 return error_mark_node;
11141 }
11142
11143 if (flags == TYPENAME_FLAG
11144 && innermost_code != cdk_function
11145 && ! (ctype && !declspecs->any_specifiers_p))
11146 {
11147 error_at (id_loc, "declaration of %qD as non-function", dname);
11148 return error_mark_node;
11149 }
11150
11151 if (dname && identifier_p (dname))
11152 {
11153 if (UDLIT_OPER_P (dname)
11154 && innermost_code != cdk_function)
11155 {
11156 error_at (id_loc, "declaration of %qD as non-function", dname);
11157 return error_mark_node;
11158 }
11159
11160 if (IDENTIFIER_ANY_OP_P (dname))
11161 {
11162 if (typedef_p)
11163 {
11164 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11165 return error_mark_node;
11166 }
11167 else if (decl_context == PARM || decl_context == CATCHPARM)
11168 {
11169 error_at (id_loc, "declaration of %qD as parameter", dname);
11170 return error_mark_node;
11171 }
11172 }
11173 }
11174
11175 /* Anything declared one level down from the top level
11176 must be one of the parameters of a function
11177 (because the body is at least two levels down). */
11178
11179 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11180 by not allowing C++ class definitions to specify their parameters
11181 with xdecls (must be spec.d in the parmlist).
11182
11183 Since we now wait to push a class scope until we are sure that
11184 we are in a legitimate method context, we must set oldcname
11185 explicitly (since current_class_name is not yet alive).
11186
11187 We also want to avoid calling this a PARM if it is in a namespace. */
11188
11189 if (decl_context == NORMAL && !toplevel_bindings_p ())
11190 {
11191 cp_binding_level *b = current_binding_level;
11192 current_binding_level = b->level_chain;
11193 if (current_binding_level != 0 && toplevel_bindings_p ())
11194 decl_context = PARM;
11195 current_binding_level = b;
11196 }
11197
11198 if (name == NULL)
11199 name = decl_context == PARM ? "parameter" : "type name";
11200
11201 if (consteval_p && constexpr_p)
11202 {
11203 error_at (declspecs->locations[ds_consteval],
11204 "both %qs and %qs specified", "constexpr", "consteval");
11205 return error_mark_node;
11206 }
11207
11208 if (concept_p && typedef_p)
11209 {
11210 error_at (declspecs->locations[ds_concept],
11211 "%qs cannot appear in a typedef declaration", "concept");
11212 return error_mark_node;
11213 }
11214
11215 if (constexpr_p && typedef_p)
11216 {
11217 error_at (declspecs->locations[ds_constexpr],
11218 "%qs cannot appear in a typedef declaration", "constexpr");
11219 return error_mark_node;
11220 }
11221
11222 if (consteval_p && typedef_p)
11223 {
11224 error_at (declspecs->locations[ds_consteval],
11225 "%qs cannot appear in a typedef declaration", "consteval");
11226 return error_mark_node;
11227 }
11228
11229 if (constinit_p && typedef_p)
11230 {
11231 error_at (declspecs->locations[ds_constinit],
11232 "%<constinit%> cannot appear in a typedef declaration");
11233 return error_mark_node;
11234 }
11235
11236 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11237 keywords shall appear in a decl-specifier-seq." */
11238 if (constinit_p && constexpr_p)
11239 {
11240 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11241 richloc.add_range (declspecs->locations[ds_constexpr]);
11242 error_at (&richloc,
11243 "can use at most one of the %<constinit%> and %<constexpr%> "
11244 "specifiers");
11245 return error_mark_node;
11246 }
11247
11248 /* If there were multiple types specified in the decl-specifier-seq,
11249 issue an error message. */
11250 if (declspecs->multiple_types_p)
11251 {
11252 error_at (typespec_loc,
11253 "two or more data types in declaration of %qs", name);
11254 return error_mark_node;
11255 }
11256
11257 if (declspecs->conflicting_specifiers_p)
11258 {
11259 error_at (min_location (declspecs->locations[ds_typedef],
11260 declspecs->locations[ds_storage_class]),
11261 "conflicting specifiers in declaration of %qs", name);
11262 return error_mark_node;
11263 }
11264
11265 /* Extract the basic type from the decl-specifier-seq. */
11266 type = declspecs->type;
11267 if (type == error_mark_node)
11268 {
11269 type = NULL_TREE;
11270 type_was_error_mark_node = true;
11271 }
11272 cp_warn_deprecated_use (type);
11273 if (type && TREE_CODE (type) == TYPE_DECL)
11274 {
11275 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11276 typedef_decl = type;
11277 type = TREE_TYPE (typedef_decl);
11278 if (DECL_ARTIFICIAL (typedef_decl))
11279 cp_warn_deprecated_use (type);
11280 }
11281 /* No type at all: default to `int', and set DEFAULTED_INT
11282 because it was not a user-defined typedef. */
11283 if (type == NULL_TREE)
11284 {
11285 if (signed_p || unsigned_p || long_p || short_p)
11286 {
11287 /* These imply 'int'. */
11288 type = integer_type_node;
11289 defaulted_int = 1;
11290 }
11291 /* If we just have "complex", it is equivalent to "complex double". */
11292 else if (!longlong && !explicit_intN
11293 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11294 {
11295 type = double_type_node;
11296 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11297 "ISO C++ does not support plain %<complex%> meaning "
11298 "%<double complex%>");
11299 }
11300 }
11301 /* Gather flags. */
11302 explicit_int = declspecs->explicit_int_p;
11303 explicit_char = declspecs->explicit_char_p;
11304
11305 #if 0
11306 /* See the code below that used this. */
11307 if (typedef_decl)
11308 decl_attr = DECL_ATTRIBUTES (typedef_decl);
11309 #endif
11310 typedef_type = type;
11311
11312 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11313 ctor_return_type = TREE_TYPE (dname);
11314 else
11315 ctor_return_type = ctype;
11316
11317 if (sfk != sfk_none)
11318 {
11319 type = check_special_function_return_type (sfk, type,
11320 ctor_return_type,
11321 type_quals,
11322 declspecs->locations);
11323 type_quals = TYPE_UNQUALIFIED;
11324 }
11325 else if (type == NULL_TREE)
11326 {
11327 int is_main;
11328
11329 explicit_int = -1;
11330
11331 /* We handle `main' specially here, because 'main () { }' is so
11332 common. With no options, it is allowed. With -Wreturn-type,
11333 it is a warning. It is only an error with -pedantic-errors. */
11334 is_main = (funcdef_flag
11335 && dname && identifier_p (dname)
11336 && MAIN_NAME_P (dname)
11337 && ctype == NULL_TREE
11338 && in_namespace == NULL_TREE
11339 && current_namespace == global_namespace);
11340
11341 if (type_was_error_mark_node)
11342 /* We've already issued an error, don't complain more. */;
11343 else if (in_system_header_at (id_loc) || flag_ms_extensions)
11344 /* Allow it, sigh. */;
11345 else if (! is_main)
11346 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11347 name);
11348 else if (pedantic)
11349 pedwarn (id_loc, OPT_Wpedantic,
11350 "ISO C++ forbids declaration of %qs with no type", name);
11351 else
11352 warning_at (id_loc, OPT_Wreturn_type,
11353 "ISO C++ forbids declaration of %qs with no type", name);
11354
11355 if (type_was_error_mark_node && template_parm_flag)
11356 /* FIXME we should be able to propagate the error_mark_node as is
11357 for other contexts too. */
11358 type = error_mark_node;
11359 else
11360 type = integer_type_node;
11361 }
11362
11363 ctype = NULL_TREE;
11364
11365 if (explicit_intN)
11366 {
11367 if (! int_n_enabled_p[declspecs->int_n_idx])
11368 {
11369 error_at (declspecs->locations[ds_type_spec],
11370 "%<__int%d%> is not supported by this target",
11371 int_n_data[declspecs->int_n_idx].bitsize);
11372 explicit_intN = false;
11373 }
11374 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11375 of "__intN". */
11376 else if (!int_n_alt && pedantic)
11377 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11378 "ISO C++ does not support %<__int%d%> for %qs",
11379 int_n_data[declspecs->int_n_idx].bitsize, name);
11380 }
11381
11382 /* Now process the modifiers that were specified
11383 and check for invalid combinations. */
11384
11385 /* Long double is a special combination. */
11386 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11387 {
11388 long_p = false;
11389 type = cp_build_qualified_type (long_double_type_node,
11390 cp_type_quals (type));
11391 }
11392
11393 /* Check all other uses of type modifiers. */
11394
11395 if (unsigned_p || signed_p || long_p || short_p)
11396 {
11397 location_t loc;
11398 const char *key;
11399 if (unsigned_p)
11400 {
11401 key = "unsigned";
11402 loc = declspecs->locations[ds_unsigned];
11403 }
11404 else if (signed_p)
11405 {
11406 key = "signed";
11407 loc = declspecs->locations[ds_signed];
11408 }
11409 else if (longlong)
11410 {
11411 key = "long long";
11412 loc = declspecs->locations[ds_long_long];
11413 }
11414 else if (long_p)
11415 {
11416 key = "long";
11417 loc = declspecs->locations[ds_long];
11418 }
11419 else /* if (short_p) */
11420 {
11421 key = "short";
11422 loc = declspecs->locations[ds_short];
11423 }
11424
11425 int ok = 0;
11426
11427 if (signed_p && unsigned_p)
11428 {
11429 gcc_rich_location richloc (declspecs->locations[ds_signed]);
11430 richloc.add_range (declspecs->locations[ds_unsigned]);
11431 error_at (&richloc,
11432 "%<signed%> and %<unsigned%> specified together");
11433 }
11434 else if (long_p && short_p)
11435 {
11436 gcc_rich_location richloc (declspecs->locations[ds_long]);
11437 richloc.add_range (declspecs->locations[ds_short]);
11438 error_at (&richloc, "%<long%> and %<short%> specified together");
11439 }
11440 else if (TREE_CODE (type) != INTEGER_TYPE
11441 || type == char8_type_node
11442 || type == char16_type_node
11443 || type == char32_type_node
11444 || ((long_p || short_p)
11445 && (explicit_char || explicit_intN)))
11446 error_at (loc, "%qs specified with %qT", key, type);
11447 else if (!explicit_int && !defaulted_int
11448 && !explicit_char && !explicit_intN)
11449 {
11450 if (typedef_decl)
11451 {
11452 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11453 key, type);
11454 ok = !flag_pedantic_errors;
11455 }
11456 else if (declspecs->decltype_p)
11457 error_at (loc, "%qs specified with %<decltype%>", key);
11458 else
11459 error_at (loc, "%qs specified with %<typeof%>", key);
11460 }
11461 else
11462 ok = 1;
11463
11464 /* Discard the type modifiers if they are invalid. */
11465 if (! ok)
11466 {
11467 unsigned_p = false;
11468 signed_p = false;
11469 long_p = false;
11470 short_p = false;
11471 longlong = 0;
11472 }
11473 }
11474
11475 /* Decide whether an integer type is signed or not.
11476 Optionally treat bitfields as signed by default. */
11477 if (unsigned_p
11478 /* [class.bit]
11479
11480 It is implementation-defined whether a plain (neither
11481 explicitly signed or unsigned) char, short, int, or long
11482 bit-field is signed or unsigned.
11483
11484 Naturally, we extend this to long long as well. Note that
11485 this does not include wchar_t. */
11486 || (bitfield && !flag_signed_bitfields
11487 && !signed_p
11488 /* A typedef for plain `int' without `signed' can be
11489 controlled just like plain `int', but a typedef for
11490 `signed int' cannot be so controlled. */
11491 && !(typedef_decl
11492 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11493 && TREE_CODE (type) == INTEGER_TYPE
11494 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11495 {
11496 if (explicit_intN)
11497 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11498 else if (longlong)
11499 type = long_long_unsigned_type_node;
11500 else if (long_p)
11501 type = long_unsigned_type_node;
11502 else if (short_p)
11503 type = short_unsigned_type_node;
11504 else if (type == char_type_node)
11505 type = unsigned_char_type_node;
11506 else if (typedef_decl)
11507 type = unsigned_type_for (type);
11508 else
11509 type = unsigned_type_node;
11510 }
11511 else if (signed_p && type == char_type_node)
11512 type = signed_char_type_node;
11513 else if (explicit_intN)
11514 type = int_n_trees[declspecs->int_n_idx].signed_type;
11515 else if (longlong)
11516 type = long_long_integer_type_node;
11517 else if (long_p)
11518 type = long_integer_type_node;
11519 else if (short_p)
11520 type = short_integer_type_node;
11521
11522 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11523 {
11524 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11525 error_at (declspecs->locations[ds_complex],
11526 "complex invalid for %qs", name);
11527 /* If a modifier is specified, the resulting complex is the complex
11528 form of TYPE. E.g, "complex short" is "complex short int". */
11529 else if (type == integer_type_node)
11530 type = complex_integer_type_node;
11531 else if (type == float_type_node)
11532 type = complex_float_type_node;
11533 else if (type == double_type_node)
11534 type = complex_double_type_node;
11535 else if (type == long_double_type_node)
11536 type = complex_long_double_type_node;
11537 else
11538 type = build_complex_type (type);
11539 }
11540
11541 /* If we're using the injected-class-name to form a compound type or a
11542 declaration, replace it with the underlying class so we don't get
11543 redundant typedefs in the debug output. But if we are returning the
11544 type unchanged, leave it alone so that it's available to
11545 maybe_get_template_decl_from_type_decl. */
11546 if (CLASS_TYPE_P (type)
11547 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11548 && type == TREE_TYPE (TYPE_NAME (type))
11549 && (declarator || type_quals))
11550 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11551
11552 type_quals |= cp_type_quals (type);
11553 type = cp_build_qualified_type_real
11554 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11555 || declspecs->decltype_p)
11556 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11557 /* We might have ignored or rejected some of the qualifiers. */
11558 type_quals = cp_type_quals (type);
11559
11560 if (cxx_dialect >= cxx17 && type && is_auto (type)
11561 && innermost_code != cdk_function
11562 && id_declarator && declarator != id_declarator)
11563 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11564 {
11565 error_at (typespec_loc, "template placeholder type %qT must be followed "
11566 "by a simple declarator-id", type);
11567 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11568 type = error_mark_node;
11569 }
11570
11571 staticp = 0;
11572 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11573 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11574 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11575
11576 storage_class = declspecs->storage_class;
11577 if (storage_class == sc_static)
11578 staticp = 1 + (decl_context == FIELD);
11579
11580 if (virtualp)
11581 {
11582 if (staticp == 2)
11583 {
11584 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11585 richloc.add_range (declspecs->locations[ds_storage_class]);
11586 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11587 "and %<static%>", dname);
11588 storage_class = sc_none;
11589 staticp = 0;
11590 }
11591 if (constexpr_p && cxx_dialect < cxx2a)
11592 {
11593 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11594 richloc.add_range (declspecs->locations[ds_constexpr]);
11595 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11596 "%<virtual%> and %<constexpr%> only in %<-std=c++2a%> or "
11597 "%<-std=gnu++2a%>", dname);
11598 }
11599 }
11600 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11601
11602 /* Issue errors about use of storage classes for parameters. */
11603 if (decl_context == PARM)
11604 {
11605 if (typedef_p)
11606 {
11607 error_at (declspecs->locations[ds_typedef],
11608 "typedef declaration invalid in parameter declaration");
11609 return error_mark_node;
11610 }
11611 else if (template_parm_flag && storage_class != sc_none)
11612 {
11613 error_at (min_location (declspecs->locations[ds_thread],
11614 declspecs->locations[ds_storage_class]),
11615 "storage class specified for template parameter %qs",
11616 name);
11617 return error_mark_node;
11618 }
11619 else if (storage_class == sc_static
11620 || storage_class == sc_extern
11621 || thread_p)
11622 {
11623 error_at (min_location (declspecs->locations[ds_thread],
11624 declspecs->locations[ds_storage_class]),
11625 "storage class specified for parameter %qs", name);
11626 return error_mark_node;
11627 }
11628
11629 /* Function parameters cannot be concept. */
11630 if (concept_p)
11631 {
11632 error_at (declspecs->locations[ds_concept],
11633 "a parameter cannot be declared %qs", "concept");
11634 concept_p = 0;
11635 constexpr_p = 0;
11636 }
11637 /* Function parameters cannot be constexpr. If we saw one, moan
11638 and pretend it wasn't there. */
11639 else if (constexpr_p)
11640 {
11641 error_at (declspecs->locations[ds_constexpr],
11642 "a parameter cannot be declared %qs", "constexpr");
11643 constexpr_p = 0;
11644 }
11645 if (constinit_p)
11646 {
11647 error_at (declspecs->locations[ds_constinit],
11648 "a parameter cannot be declared %qs", "constinit");
11649 constinit_p = 0;
11650 }
11651 if (consteval_p)
11652 {
11653 error_at (declspecs->locations[ds_consteval],
11654 "a parameter cannot be declared %qs", "consteval");
11655 consteval_p = 0;
11656 }
11657 }
11658
11659 /* Give error if `virtual' is used outside of class declaration. */
11660 if (virtualp
11661 && (current_class_name == NULL_TREE || decl_context != FIELD))
11662 {
11663 error_at (declspecs->locations[ds_virtual],
11664 "%<virtual%> outside class declaration");
11665 virtualp = 0;
11666 }
11667
11668 if (innermost_code == cdk_decomp)
11669 {
11670 location_t loc = (declarator->kind == cdk_reference
11671 ? declarator->declarator->id_loc : declarator->id_loc);
11672 if (inlinep)
11673 error_at (declspecs->locations[ds_inline],
11674 "structured binding declaration cannot be %qs", "inline");
11675 if (typedef_p)
11676 error_at (declspecs->locations[ds_typedef],
11677 "structured binding declaration cannot be %qs", "typedef");
11678 if (constexpr_p && !concept_p)
11679 error_at (declspecs->locations[ds_constexpr], "structured "
11680 "binding declaration cannot be %qs", "constexpr");
11681 if (consteval_p)
11682 error_at (declspecs->locations[ds_consteval], "structured "
11683 "binding declaration cannot be %qs", "consteval");
11684 if (thread_p && cxx_dialect < cxx2a)
11685 pedwarn (declspecs->locations[ds_thread], 0,
11686 "structured binding declaration can be %qs only in "
11687 "%<-std=c++2a%> or %<-std=gnu++2a%>",
11688 declspecs->gnu_thread_keyword_p
11689 ? "__thread" : "thread_local");
11690 if (concept_p)
11691 error_at (declspecs->locations[ds_concept],
11692 "structured binding declaration cannot be %qs", "concept");
11693 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11694 if (type_quals & TYPE_QUAL_VOLATILE)
11695 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11696 "%<volatile%>-qualified structured binding is deprecated");
11697 switch (storage_class)
11698 {
11699 case sc_none:
11700 break;
11701 case sc_register:
11702 error_at (loc, "structured binding declaration cannot be %qs",
11703 "register");
11704 break;
11705 case sc_static:
11706 if (cxx_dialect < cxx2a)
11707 pedwarn (loc, 0,
11708 "structured binding declaration can be %qs only in "
11709 "%<-std=c++2a%> or %<-std=gnu++2a%>", "static");
11710 break;
11711 case sc_extern:
11712 error_at (loc, "structured binding declaration cannot be %qs",
11713 "extern");
11714 break;
11715 case sc_mutable:
11716 error_at (loc, "structured binding declaration cannot be %qs",
11717 "mutable");
11718 break;
11719 case sc_auto:
11720 error_at (loc, "structured binding declaration cannot be "
11721 "C++98 %<auto%>");
11722 break;
11723 default:
11724 gcc_unreachable ();
11725 }
11726 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11727 || TYPE_IDENTIFIER (type) != auto_identifier)
11728 {
11729 if (type != error_mark_node)
11730 {
11731 error_at (loc, "structured binding declaration cannot have "
11732 "type %qT", type);
11733 inform (loc,
11734 "type must be cv-qualified %<auto%> or reference to "
11735 "cv-qualified %<auto%>");
11736 }
11737 type = build_qualified_type (make_auto (), type_quals);
11738 declspecs->type = type;
11739 }
11740 inlinep = 0;
11741 typedef_p = 0;
11742 constexpr_p = 0;
11743 consteval_p = 0;
11744 concept_p = 0;
11745 if (storage_class != sc_static)
11746 {
11747 storage_class = sc_none;
11748 declspecs->storage_class = sc_none;
11749 }
11750 }
11751
11752 /* Static anonymous unions are dealt with here. */
11753 if (staticp && decl_context == TYPENAME
11754 && declspecs->type
11755 && ANON_AGGR_TYPE_P (declspecs->type))
11756 decl_context = FIELD;
11757
11758 /* Warn about storage classes that are invalid for certain
11759 kinds of declarations (parameters, typenames, etc.). */
11760 if (thread_p
11761 && ((storage_class
11762 && storage_class != sc_extern
11763 && storage_class != sc_static)
11764 || typedef_p))
11765 {
11766 location_t loc
11767 = min_location (declspecs->locations[ds_thread],
11768 declspecs->locations[ds_storage_class]);
11769 error_at (loc, "multiple storage classes in declaration of %qs", name);
11770 thread_p = false;
11771 }
11772 if (decl_context != NORMAL
11773 && ((storage_class != sc_none
11774 && storage_class != sc_mutable)
11775 || thread_p))
11776 {
11777 if ((decl_context == PARM || decl_context == CATCHPARM)
11778 && (storage_class == sc_register
11779 || storage_class == sc_auto))
11780 ;
11781 else if (typedef_p)
11782 ;
11783 else if (decl_context == FIELD
11784 /* C++ allows static class elements. */
11785 && storage_class == sc_static)
11786 /* C++ also allows inlines and signed and unsigned elements,
11787 but in those cases we don't come in here. */
11788 ;
11789 else
11790 {
11791 location_t loc
11792 = min_location (declspecs->locations[ds_thread],
11793 declspecs->locations[ds_storage_class]);
11794 if (decl_context == FIELD)
11795 error_at (loc, "storage class specified for %qs", name);
11796 else if (decl_context == PARM || decl_context == CATCHPARM)
11797 error_at (loc, "storage class specified for parameter %qs", name);
11798 else
11799 error_at (loc, "storage class specified for typename");
11800 if (storage_class == sc_register
11801 || storage_class == sc_auto
11802 || storage_class == sc_extern
11803 || thread_p)
11804 storage_class = sc_none;
11805 }
11806 }
11807 else if (storage_class == sc_extern && funcdef_flag
11808 && ! toplevel_bindings_p ())
11809 error ("nested function %qs declared %<extern%>", name);
11810 else if (toplevel_bindings_p ())
11811 {
11812 if (storage_class == sc_auto)
11813 error_at (declspecs->locations[ds_storage_class],
11814 "top-level declaration of %qs specifies %<auto%>", name);
11815 }
11816 else if (thread_p
11817 && storage_class != sc_extern
11818 && storage_class != sc_static)
11819 {
11820 if (declspecs->gnu_thread_keyword_p)
11821 pedwarn (declspecs->locations[ds_thread],
11822 0, "function-scope %qs implicitly auto and "
11823 "declared %<__thread%>", name);
11824
11825 /* When thread_local is applied to a variable of block scope the
11826 storage-class-specifier static is implied if it does not appear
11827 explicitly. */
11828 storage_class = declspecs->storage_class = sc_static;
11829 staticp = 1;
11830 }
11831
11832 if (storage_class && friendp)
11833 {
11834 error_at (min_location (declspecs->locations[ds_thread],
11835 declspecs->locations[ds_storage_class]),
11836 "storage class specifiers invalid in friend function "
11837 "declarations");
11838 storage_class = sc_none;
11839 staticp = 0;
11840 }
11841
11842 if (!id_declarator)
11843 unqualified_id = NULL_TREE;
11844 else
11845 {
11846 unqualified_id = id_declarator->u.id.unqualified_name;
11847 switch (TREE_CODE (unqualified_id))
11848 {
11849 case BIT_NOT_EXPR:
11850 unqualified_id = TREE_OPERAND (unqualified_id, 0);
11851 if (TYPE_P (unqualified_id))
11852 unqualified_id = constructor_name (unqualified_id);
11853 break;
11854
11855 case IDENTIFIER_NODE:
11856 case TEMPLATE_ID_EXPR:
11857 break;
11858
11859 default:
11860 gcc_unreachable ();
11861 }
11862 }
11863
11864 if (declspecs->std_attributes)
11865 {
11866 location_t attr_loc = declspecs->locations[ds_std_attribute];
11867 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
11868 inform (attr_loc, "an attribute that appertains to a type-specifier "
11869 "is ignored");
11870 }
11871
11872 /* Determine the type of the entity declared by recurring on the
11873 declarator. */
11874 for (; declarator; declarator = declarator->declarator)
11875 {
11876 const cp_declarator *inner_declarator;
11877 tree attrs;
11878
11879 if (type == error_mark_node)
11880 return error_mark_node;
11881
11882 attrs = declarator->attributes;
11883 if (attrs)
11884 {
11885 int attr_flags;
11886
11887 attr_flags = 0;
11888 if (declarator == NULL || declarator->kind == cdk_id)
11889 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11890 if (declarator->kind == cdk_function)
11891 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11892 if (declarator->kind == cdk_array)
11893 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11894 returned_attrs = decl_attributes (&type,
11895 chainon (returned_attrs, attrs),
11896 attr_flags);
11897 }
11898
11899 inner_declarator = declarator->declarator;
11900
11901 /* We don't want to warn in parameter context because we don't
11902 yet know if the parse will succeed, and this might turn out
11903 to be a constructor call. */
11904 if (decl_context != PARM
11905 && decl_context != TYPENAME
11906 && !typedef_p
11907 && declarator->parenthesized != UNKNOWN_LOCATION
11908 /* If the type is class-like and the inner name used a
11909 global namespace qualifier, we need the parens.
11910 Unfortunately all we can tell is whether a qualified name
11911 was used or not. */
11912 && !(inner_declarator
11913 && inner_declarator->kind == cdk_id
11914 && inner_declarator->u.id.qualifying_scope
11915 && (MAYBE_CLASS_TYPE_P (type)
11916 || TREE_CODE (type) == ENUMERAL_TYPE)))
11917 warning_at (declarator->parenthesized, OPT_Wparentheses,
11918 "unnecessary parentheses in declaration of %qs", name);
11919 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
11920 break;
11921
11922 switch (declarator->kind)
11923 {
11924 case cdk_array:
11925 type = create_array_type_for_decl (dname, type,
11926 declarator->u.array.bounds,
11927 declarator->id_loc);
11928 if (!valid_array_size_p (dname
11929 ? declarator->id_loc : input_location,
11930 type, dname))
11931 type = error_mark_node;
11932
11933 if (declarator->std_attributes)
11934 /* [dcl.array]/1:
11935
11936 The optional attribute-specifier-seq appertains to the
11937 array. */
11938 returned_attrs = chainon (returned_attrs,
11939 declarator->std_attributes);
11940 break;
11941
11942 case cdk_function:
11943 {
11944 tree arg_types;
11945 int funcdecl_p;
11946
11947 /* Declaring a function type. */
11948
11949 {
11950 iloc_sentinel ils (declspecs->locations[ds_type_spec]);
11951 abstract_virtuals_error (ACU_RETURN, type);
11952 }
11953
11954 /* Pick up type qualifiers which should be applied to `this'. */
11955 memfn_quals = declarator->u.function.qualifiers;
11956 /* Pick up virt-specifiers. */
11957 virt_specifiers = declarator->u.function.virt_specifiers;
11958 /* And ref-qualifier, too */
11959 rqual = declarator->u.function.ref_qualifier;
11960 /* And tx-qualifier. */
11961 tree tx_qual = declarator->u.function.tx_qualifier;
11962 /* Pick up the exception specifications. */
11963 raises = declarator->u.function.exception_specification;
11964 /* If the exception-specification is ill-formed, let's pretend
11965 there wasn't one. */
11966 if (raises == error_mark_node)
11967 raises = NULL_TREE;
11968
11969 if (reqs)
11970 error_at (location_of (reqs), "requires-clause on return type");
11971 reqs = declarator->u.function.requires_clause;
11972
11973 /* Say it's a definition only for the CALL_EXPR
11974 closest to the identifier. */
11975 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
11976
11977 /* Handle a late-specified return type. */
11978 tree late_return_type = declarator->u.function.late_return_type;
11979 if (funcdecl_p
11980 /* This is the case e.g. for
11981 using T = auto () -> int. */
11982 || inner_declarator == NULL)
11983 {
11984 if (tree auto_node = type_uses_auto (type))
11985 {
11986 if (!late_return_type)
11987 {
11988 if (current_class_type
11989 && LAMBDA_TYPE_P (current_class_type))
11990 /* OK for C++11 lambdas. */;
11991 else if (cxx_dialect < cxx14)
11992 {
11993 error_at (typespec_loc, "%qs function uses "
11994 "%<auto%> type specifier without "
11995 "trailing return type", name);
11996 inform (typespec_loc,
11997 "deduced return type only available "
11998 "with %<-std=c++14%> or %<-std=gnu++14%>");
11999 }
12000 else if (virtualp)
12001 {
12002 error_at (typespec_loc, "virtual function "
12003 "cannot have deduced return type");
12004 virtualp = false;
12005 }
12006 }
12007 else if (!is_auto (type) && sfk != sfk_conversion)
12008 {
12009 error_at (typespec_loc, "%qs function with trailing "
12010 "return type has %qT as its type rather "
12011 "than plain %<auto%>", name, type);
12012 return error_mark_node;
12013 }
12014 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12015 {
12016 if (funcdecl_p)
12017 error_at (typespec_loc,
12018 "%qs function with trailing return type "
12019 "has %<decltype(auto)%> as its type "
12020 "rather than plain %<auto%>", name);
12021 else
12022 error_at (typespec_loc,
12023 "invalid use of %<decltype(auto)%>");
12024 return error_mark_node;
12025 }
12026 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12027 if (!tmpl)
12028 if (tree late_auto = type_uses_auto (late_return_type))
12029 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12030 if (tmpl && funcdecl_p)
12031 {
12032 if (!dguide_name_p (unqualified_id))
12033 {
12034 error_at (declarator->id_loc, "deduced class "
12035 "type %qD in function return type",
12036 DECL_NAME (tmpl));
12037 inform (DECL_SOURCE_LOCATION (tmpl),
12038 "%qD declared here", tmpl);
12039 return error_mark_node;
12040 }
12041 else if (!late_return_type)
12042 {
12043 error_at (declarator->id_loc, "deduction guide "
12044 "for %qT must have trailing return "
12045 "type", TREE_TYPE (tmpl));
12046 inform (DECL_SOURCE_LOCATION (tmpl),
12047 "%qD declared here", tmpl);
12048 return error_mark_node;
12049 }
12050 else if (CLASS_TYPE_P (late_return_type)
12051 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12052 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12053 == tmpl))
12054 /* OK */;
12055 else
12056 error ("trailing return type %qT of deduction guide "
12057 "is not a specialization of %qT",
12058 late_return_type, TREE_TYPE (tmpl));
12059 }
12060 }
12061 else if (late_return_type
12062 && sfk != sfk_conversion)
12063 {
12064 if (late_return_type == error_mark_node)
12065 return error_mark_node;
12066 if (cxx_dialect < cxx11)
12067 /* Not using maybe_warn_cpp0x because this should
12068 always be an error. */
12069 error_at (typespec_loc,
12070 "trailing return type only available "
12071 "with %<-std=c++11%> or %<-std=gnu++11%>");
12072 else
12073 error_at (typespec_loc, "%qs function with trailing "
12074 "return type not declared with %<auto%> "
12075 "type specifier", name);
12076 return error_mark_node;
12077 }
12078 }
12079 type = splice_late_return_type (type, late_return_type);
12080 if (type == error_mark_node)
12081 return error_mark_node;
12082
12083 if (late_return_type)
12084 {
12085 late_return_type_p = true;
12086 type_quals = cp_type_quals (type);
12087 }
12088
12089 if (type_quals != TYPE_UNQUALIFIED)
12090 {
12091 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12092 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12093 "qualifiers ignored on function return type");
12094 /* [dcl.fct] "A volatile-qualified return type is
12095 deprecated." */
12096 if (type_quals & TYPE_QUAL_VOLATILE)
12097 warning_at (typespec_loc, OPT_Wvolatile,
12098 "%<volatile%>-qualified return type is "
12099 "deprecated");
12100
12101 /* We now know that the TYPE_QUALS don't apply to the
12102 decl, but to its return type. */
12103 type_quals = TYPE_UNQUALIFIED;
12104 }
12105
12106 /* Error about some types functions can't return. */
12107
12108 if (TREE_CODE (type) == FUNCTION_TYPE)
12109 {
12110 error_at (typespec_loc, "%qs declared as function returning "
12111 "a function", name);
12112 return error_mark_node;
12113 }
12114 if (TREE_CODE (type) == ARRAY_TYPE)
12115 {
12116 error_at (typespec_loc, "%qs declared as function returning "
12117 "an array", name);
12118 return error_mark_node;
12119 }
12120 if (constinit_p)
12121 {
12122 error_at (declspecs->locations[ds_constinit],
12123 "%<constinit%> on function return type is not "
12124 "allowed");
12125 return error_mark_node;
12126 }
12127 /* Only plain decltype(auto) is allowed. */
12128 if (tree a = type_uses_auto (type))
12129 {
12130 if (AUTO_IS_DECLTYPE (a) && a != type)
12131 {
12132 error_at (typespec_loc, "%qT as type rather than "
12133 "plain %<decltype(auto)%>", type);
12134 return error_mark_node;
12135 }
12136 }
12137
12138 if (ctype == NULL_TREE
12139 && decl_context == FIELD
12140 && funcdecl_p
12141 && friendp == 0)
12142 ctype = current_class_type;
12143
12144 if (ctype && (sfk == sfk_constructor
12145 || sfk == sfk_destructor))
12146 {
12147 /* We are within a class's scope. If our declarator name
12148 is the same as the class name, and we are defining
12149 a function, then it is a constructor/destructor, and
12150 therefore returns a void type. */
12151
12152 /* ISO C++ 12.4/2. A destructor may not be declared
12153 const or volatile. A destructor may not be static.
12154 A destructor may not be declared with ref-qualifier.
12155
12156 ISO C++ 12.1. A constructor may not be declared
12157 const or volatile. A constructor may not be
12158 virtual. A constructor may not be static.
12159 A constructor may not be declared with ref-qualifier. */
12160 if (staticp == 2)
12161 error_at (declspecs->locations[ds_storage_class],
12162 (flags == DTOR_FLAG)
12163 ? G_("destructor cannot be static member "
12164 "function")
12165 : G_("constructor cannot be static member "
12166 "function"));
12167 if (memfn_quals)
12168 {
12169 error ((flags == DTOR_FLAG)
12170 ? G_("destructors may not be cv-qualified")
12171 : G_("constructors may not be cv-qualified"));
12172 memfn_quals = TYPE_UNQUALIFIED;
12173 }
12174
12175 if (rqual)
12176 {
12177 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12178 error ((flags == DTOR_FLAG)
12179 ? G_("destructors may not be ref-qualified")
12180 : G_("constructors may not be ref-qualified"));
12181 rqual = REF_QUAL_NONE;
12182 }
12183
12184 if (decl_context == FIELD
12185 && !member_function_or_else (ctype,
12186 current_class_type,
12187 flags))
12188 return error_mark_node;
12189
12190 if (flags != DTOR_FLAG)
12191 {
12192 /* It's a constructor. */
12193 if (explicitp == 1)
12194 explicitp = 2;
12195 if (virtualp)
12196 {
12197 permerror (declspecs->locations[ds_virtual],
12198 "constructors cannot be declared %<virtual%>");
12199 virtualp = 0;
12200 }
12201 if (decl_context == FIELD
12202 && sfk != sfk_constructor)
12203 return error_mark_node;
12204 }
12205 if (decl_context == FIELD)
12206 staticp = 0;
12207 }
12208 else if (friendp)
12209 {
12210 if (virtualp)
12211 {
12212 /* Cannot be both friend and virtual. */
12213 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12214 richloc.add_range (declspecs->locations[ds_friend]);
12215 error_at (&richloc, "virtual functions cannot be friends");
12216 friendp = 0;
12217 }
12218 if (decl_context == NORMAL)
12219 error_at (declarator->id_loc,
12220 "friend declaration not in class definition");
12221 if (current_function_decl && funcdef_flag)
12222 {
12223 error_at (declarator->id_loc,
12224 "cannot define friend function %qs in a local "
12225 "class definition", name);
12226 friendp = 0;
12227 }
12228 /* [class.friend]/6: A function can be defined in a friend
12229 declaration if the function name is unqualified. */
12230 if (funcdef_flag && in_namespace)
12231 {
12232 if (in_namespace == global_namespace)
12233 error_at (declarator->id_loc,
12234 "friend function definition %qs cannot have "
12235 "a name qualified with %<::%>", name);
12236 else
12237 error_at (declarator->id_loc,
12238 "friend function definition %qs cannot have "
12239 "a name qualified with %<%D::%>", name,
12240 in_namespace);
12241 }
12242 }
12243 else if (ctype && sfk == sfk_conversion)
12244 {
12245 if (explicitp == 1)
12246 {
12247 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12248 explicitp = 2;
12249 }
12250 if (late_return_type_p)
12251 error ("a conversion function cannot have a trailing return type");
12252 }
12253 else if (sfk == sfk_deduction_guide)
12254 {
12255 if (explicitp == 1)
12256 explicitp = 2;
12257 }
12258
12259 tree pushed_scope = NULL_TREE;
12260 if (funcdecl_p
12261 && decl_context != FIELD
12262 && inner_declarator->u.id.qualifying_scope
12263 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12264 pushed_scope
12265 = push_scope (inner_declarator->u.id.qualifying_scope);
12266
12267 arg_types = grokparms (declarator->u.function.parameters, &parms);
12268
12269 if (pushed_scope)
12270 pop_scope (pushed_scope);
12271
12272 if (inner_declarator
12273 && inner_declarator->kind == cdk_id
12274 && inner_declarator->u.id.sfk == sfk_destructor
12275 && arg_types != void_list_node)
12276 {
12277 error_at (declarator->id_loc,
12278 "destructors may not have parameters");
12279 arg_types = void_list_node;
12280 parms = NULL_TREE;
12281 }
12282
12283 type = build_function_type (type, arg_types);
12284
12285 tree attrs = declarator->std_attributes;
12286 if (tx_qual)
12287 {
12288 tree att = build_tree_list (tx_qual, NULL_TREE);
12289 /* transaction_safe applies to the type, but
12290 transaction_safe_dynamic applies to the function. */
12291 if (is_attribute_p ("transaction_safe", tx_qual))
12292 attrs = chainon (attrs, att);
12293 else
12294 returned_attrs = chainon (returned_attrs, att);
12295 }
12296 if (attrs)
12297 /* [dcl.fct]/2:
12298
12299 The optional attribute-specifier-seq appertains to
12300 the function type. */
12301 decl_attributes (&type, attrs, 0);
12302
12303 if (raises)
12304 type = build_exception_variant (type, raises);
12305 }
12306 break;
12307
12308 case cdk_pointer:
12309 case cdk_reference:
12310 case cdk_ptrmem:
12311 /* Filter out pointers-to-references and references-to-references.
12312 We can get these if a TYPE_DECL is used. */
12313
12314 if (TYPE_REF_P (type))
12315 {
12316 if (declarator->kind != cdk_reference)
12317 {
12318 error ("cannot declare pointer to %q#T", type);
12319 type = TREE_TYPE (type);
12320 }
12321
12322 /* In C++0x, we allow reference to reference declarations
12323 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12324 and template type arguments [14.3.1/4 temp.arg.type]. The
12325 check for direct reference to reference declarations, which
12326 are still forbidden, occurs below. Reasoning behind the change
12327 can be found in DR106, DR540, and the rvalue reference
12328 proposals. */
12329 else if (cxx_dialect == cxx98)
12330 {
12331 error ("cannot declare reference to %q#T", type);
12332 type = TREE_TYPE (type);
12333 }
12334 }
12335 else if (VOID_TYPE_P (type))
12336 {
12337 if (declarator->kind == cdk_reference)
12338 error ("cannot declare reference to %q#T", type);
12339 else if (declarator->kind == cdk_ptrmem)
12340 error ("cannot declare pointer to %q#T member", type);
12341 }
12342
12343 /* We now know that the TYPE_QUALS don't apply to the decl,
12344 but to the target of the pointer. */
12345 type_quals = TYPE_UNQUALIFIED;
12346
12347 /* This code used to handle METHOD_TYPE, but I don't think it's
12348 possible to get it here anymore. */
12349 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12350 if (declarator->kind == cdk_ptrmem
12351 && TREE_CODE (type) == FUNCTION_TYPE)
12352 {
12353 memfn_quals |= type_memfn_quals (type);
12354 type = build_memfn_type (type,
12355 declarator->u.pointer.class_type,
12356 memfn_quals,
12357 rqual);
12358 if (type == error_mark_node)
12359 return error_mark_node;
12360
12361 rqual = REF_QUAL_NONE;
12362 memfn_quals = TYPE_UNQUALIFIED;
12363 }
12364
12365 if (TREE_CODE (type) == FUNCTION_TYPE
12366 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12367 || type_memfn_rqual (type) != REF_QUAL_NONE))
12368 error (declarator->kind == cdk_reference
12369 ? G_("cannot declare reference to qualified function type %qT")
12370 : G_("cannot declare pointer to qualified function type %qT"),
12371 type);
12372
12373 /* When the pointed-to type involves components of variable size,
12374 care must be taken to ensure that the size evaluation code is
12375 emitted early enough to dominate all the possible later uses
12376 and late enough for the variables on which it depends to have
12377 been assigned.
12378
12379 This is expected to happen automatically when the pointed-to
12380 type has a name/declaration of it's own, but special attention
12381 is required if the type is anonymous.
12382
12383 We handle the NORMAL and FIELD contexts here by inserting a
12384 dummy statement that just evaluates the size at a safe point
12385 and ensures it is not deferred until e.g. within a deeper
12386 conditional context (c++/43555).
12387
12388 We expect nothing to be needed here for PARM or TYPENAME.
12389 Evaluating the size at this point for TYPENAME would
12390 actually be incorrect, as we might be in the middle of an
12391 expression with side effects on the pointed-to type size
12392 "arguments" prior to the pointer declaration point and the
12393 size evaluation could end up prior to the side effects. */
12394
12395 if (!TYPE_NAME (type)
12396 && (decl_context == NORMAL || decl_context == FIELD)
12397 && at_function_scope_p ()
12398 && variably_modified_type_p (type, NULL_TREE))
12399 {
12400 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12401 NULL_TREE, type);
12402 add_decl_expr (TYPE_NAME (type));
12403 }
12404
12405 if (declarator->kind == cdk_reference)
12406 {
12407 /* In C++0x, the type we are creating a reference to might be
12408 a typedef which is itself a reference type. In that case,
12409 we follow the reference collapsing rules in
12410 [7.1.3/8 dcl.typedef] to create the final reference type:
12411
12412 "If a typedef TD names a type that is a reference to a type
12413 T, an attempt to create the type 'lvalue reference to cv TD'
12414 creates the type 'lvalue reference to T,' while an attempt
12415 to create the type "rvalue reference to cv TD' creates the
12416 type TD."
12417 */
12418 if (VOID_TYPE_P (type))
12419 /* We already gave an error. */;
12420 else if (TYPE_REF_P (type))
12421 {
12422 if (declarator->u.reference.rvalue_ref)
12423 /* Leave type alone. */;
12424 else
12425 type = cp_build_reference_type (TREE_TYPE (type), false);
12426 }
12427 else
12428 type = cp_build_reference_type
12429 (type, declarator->u.reference.rvalue_ref);
12430
12431 /* In C++0x, we need this check for direct reference to
12432 reference declarations, which are forbidden by
12433 [8.3.2/5 dcl.ref]. Reference to reference declarations
12434 are only allowed indirectly through typedefs and template
12435 type arguments. Example:
12436
12437 void foo(int & &); // invalid ref-to-ref decl
12438
12439 typedef int & int_ref;
12440 void foo(int_ref &); // valid ref-to-ref decl
12441 */
12442 if (inner_declarator && inner_declarator->kind == cdk_reference)
12443 error ("cannot declare reference to %q#T, which is not "
12444 "a typedef or a template type argument", type);
12445 }
12446 else if (TREE_CODE (type) == METHOD_TYPE)
12447 type = build_ptrmemfunc_type (build_pointer_type (type));
12448 else if (declarator->kind == cdk_ptrmem)
12449 {
12450 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12451 != NAMESPACE_DECL);
12452 if (declarator->u.pointer.class_type == error_mark_node)
12453 /* We will already have complained. */
12454 type = error_mark_node;
12455 else
12456 type = build_ptrmem_type (declarator->u.pointer.class_type,
12457 type);
12458 }
12459 else
12460 type = build_pointer_type (type);
12461
12462 /* Process a list of type modifier keywords (such as
12463 const or volatile) that were given inside the `*' or `&'. */
12464
12465 if (declarator->u.pointer.qualifiers)
12466 {
12467 type
12468 = cp_build_qualified_type (type,
12469 declarator->u.pointer.qualifiers);
12470 type_quals = cp_type_quals (type);
12471 }
12472
12473 /* Apply C++11 attributes to the pointer, and not to the
12474 type pointed to. This is unlike what is done for GNU
12475 attributes above. It is to comply with [dcl.ptr]/1:
12476
12477 [the optional attribute-specifier-seq (7.6.1) appertains
12478 to the pointer and not to the object pointed to]. */
12479 if (declarator->std_attributes)
12480 decl_attributes (&type, declarator->std_attributes,
12481 0);
12482
12483 ctype = NULL_TREE;
12484 break;
12485
12486 case cdk_error:
12487 break;
12488
12489 default:
12490 gcc_unreachable ();
12491 }
12492 }
12493
12494 id_loc = declarator ? declarator->id_loc : input_location;
12495
12496 /* A `constexpr' specifier used in an object declaration declares
12497 the object as `const'. */
12498 if (constexpr_p && innermost_code != cdk_function)
12499 {
12500 /* DR1688 says that a `constexpr' specifier in combination with
12501 `volatile' is valid. */
12502
12503 if (!TYPE_REF_P (type))
12504 {
12505 type_quals |= TYPE_QUAL_CONST;
12506 type = cp_build_qualified_type (type, type_quals);
12507 }
12508 }
12509
12510 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12511 && !FUNC_OR_METHOD_TYPE_P (type)
12512 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12513 {
12514 error ("template-id %qD used as a declarator",
12515 unqualified_id);
12516 unqualified_id = dname;
12517 }
12518
12519 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12520 qualified with a class-name, turn it into a METHOD_TYPE, unless
12521 we know that the function is static. We take advantage of this
12522 opportunity to do other processing that pertains to entities
12523 explicitly declared to be class members. Note that if DECLARATOR
12524 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12525 would not have exited the loop above. */
12526 if (declarator
12527 && declarator->kind == cdk_id
12528 && declarator->u.id.qualifying_scope
12529 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12530 {
12531 ctype = declarator->u.id.qualifying_scope;
12532 ctype = TYPE_MAIN_VARIANT (ctype);
12533 template_count = num_template_headers_for_class (ctype);
12534
12535 if (ctype == current_class_type)
12536 {
12537 if (friendp)
12538 {
12539 permerror (declspecs->locations[ds_friend],
12540 "member functions are implicitly "
12541 "friends of their class");
12542 friendp = 0;
12543 }
12544 else
12545 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12546 ctype, name);
12547 }
12548 else if (/* If the qualifying type is already complete, then we
12549 can skip the following checks. */
12550 !COMPLETE_TYPE_P (ctype)
12551 && (/* If the function is being defined, then
12552 qualifying type must certainly be complete. */
12553 funcdef_flag
12554 /* A friend declaration of "T::f" is OK, even if
12555 "T" is a template parameter. But, if this
12556 function is not a friend, the qualifying type
12557 must be a class. */
12558 || (!friendp && !CLASS_TYPE_P (ctype))
12559 /* For a declaration, the type need not be
12560 complete, if either it is dependent (since there
12561 is no meaningful definition of complete in that
12562 case) or the qualifying class is currently being
12563 defined. */
12564 || !(dependent_type_p (ctype)
12565 || currently_open_class (ctype)))
12566 /* Check that the qualifying type is complete. */
12567 && !complete_type_or_else (ctype, NULL_TREE))
12568 return error_mark_node;
12569 else if (TREE_CODE (type) == FUNCTION_TYPE)
12570 {
12571 if (current_class_type
12572 && (!friendp || funcdef_flag || initialized))
12573 {
12574 error_at (id_loc, funcdef_flag || initialized
12575 ? G_("cannot define member function %<%T::%s%> "
12576 "within %qT")
12577 : G_("cannot declare member function %<%T::%s%> "
12578 "within %qT"),
12579 ctype, name, current_class_type);
12580 return error_mark_node;
12581 }
12582 }
12583 else if (typedef_p && current_class_type)
12584 {
12585 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12586 ctype, name, current_class_type);
12587 return error_mark_node;
12588 }
12589 }
12590
12591 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12592 ctype = current_class_type;
12593
12594 /* Now TYPE has the actual type. */
12595
12596 if (returned_attrs)
12597 {
12598 if (attrlist)
12599 *attrlist = chainon (returned_attrs, *attrlist);
12600 else
12601 attrlist = &returned_attrs;
12602 }
12603
12604 if (declarator
12605 && declarator->kind == cdk_id
12606 && declarator->std_attributes
12607 && attrlist != NULL)
12608 {
12609 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12610 a declarator-id appertains to the entity that is declared. */
12611 if (declarator->std_attributes != error_mark_node)
12612 *attrlist = chainon (*attrlist, declarator->std_attributes);
12613 else
12614 /* We should have already diagnosed the issue (c++/78344). */
12615 gcc_assert (seen_error ());
12616 }
12617
12618 /* Handle parameter packs. */
12619 if (parameter_pack_p)
12620 {
12621 if (decl_context == PARM)
12622 /* Turn the type into a pack expansion.*/
12623 type = make_pack_expansion (type);
12624 else
12625 error ("non-parameter %qs cannot be a parameter pack", name);
12626 }
12627
12628 if ((decl_context == FIELD || decl_context == PARM)
12629 && !processing_template_decl
12630 && variably_modified_type_p (type, NULL_TREE))
12631 {
12632 if (decl_context == FIELD)
12633 error_at (id_loc,
12634 "data member may not have variably modified type %qT", type);
12635 else
12636 error_at (id_loc,
12637 "parameter may not have variably modified type %qT", type);
12638 type = error_mark_node;
12639 }
12640
12641 if (explicitp == 1 || (explicitp && friendp))
12642 {
12643 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12644 in the declaration of a constructor or conversion function within
12645 a class definition. */
12646 if (!current_class_type)
12647 error_at (declspecs->locations[ds_explicit],
12648 "%<explicit%> outside class declaration");
12649 else if (friendp)
12650 error_at (declspecs->locations[ds_explicit],
12651 "%<explicit%> in friend declaration");
12652 else
12653 error_at (declspecs->locations[ds_explicit],
12654 "only declarations of constructors and conversion operators "
12655 "can be %<explicit%>");
12656 explicitp = 0;
12657 }
12658
12659 if (storage_class == sc_mutable)
12660 {
12661 location_t sloc = declspecs->locations[ds_storage_class];
12662 if (decl_context != FIELD || friendp)
12663 {
12664 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12665 name);
12666 storage_class = sc_none;
12667 }
12668 else if (decl_context == TYPENAME || typedef_p)
12669 {
12670 error_at (sloc,
12671 "non-object member %qs cannot be declared %<mutable%>",
12672 name);
12673 storage_class = sc_none;
12674 }
12675 else if (FUNC_OR_METHOD_TYPE_P (type))
12676 {
12677 error_at (sloc, "function %qs cannot be declared %<mutable%>",
12678 name);
12679 storage_class = sc_none;
12680 }
12681 else if (staticp)
12682 {
12683 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12684 name);
12685 storage_class = sc_none;
12686 }
12687 else if (type_quals & TYPE_QUAL_CONST)
12688 {
12689 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12690 name);
12691 storage_class = sc_none;
12692 }
12693 else if (TYPE_REF_P (type))
12694 {
12695 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12696 name);
12697 storage_class = sc_none;
12698 }
12699 }
12700
12701 /* If this is declaring a typedef name, return a TYPE_DECL. */
12702 if (typedef_p && decl_context != TYPENAME)
12703 {
12704 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12705 tree decl;
12706
12707 if (funcdef_flag)
12708 {
12709 if (decl_context == NORMAL)
12710 error_at (id_loc,
12711 "typedef may not be a function definition");
12712 else
12713 error_at (id_loc,
12714 "typedef may not be a member function definition");
12715 return error_mark_node;
12716 }
12717
12718 /* This declaration:
12719
12720 typedef void f(int) const;
12721
12722 declares a function type which is not a member of any
12723 particular class, but which is cv-qualified; for
12724 example "f S::*" declares a pointer to a const-qualified
12725 member function of S. We record the cv-qualification in the
12726 function type. */
12727 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12728 {
12729 type = apply_memfn_quals (type, memfn_quals, rqual);
12730
12731 /* We have now dealt with these qualifiers. */
12732 memfn_quals = TYPE_UNQUALIFIED;
12733 rqual = REF_QUAL_NONE;
12734 }
12735
12736 if (type_uses_auto (type))
12737 {
12738 if (alias_p)
12739 error_at (declspecs->locations[ds_type_spec],
12740 "%<auto%> not allowed in alias declaration");
12741 else
12742 error_at (declspecs->locations[ds_type_spec],
12743 "typedef declared %<auto%>");
12744 type = error_mark_node;
12745 }
12746
12747 if (reqs)
12748 error_at (location_of (reqs), "requires-clause on typedef");
12749
12750 if (id_declarator && declarator->u.id.qualifying_scope)
12751 {
12752 error_at (id_loc, "typedef name may not be a nested-name-specifier");
12753 type = error_mark_node;
12754 }
12755
12756 if (decl_context == FIELD)
12757 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
12758 else
12759 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
12760
12761 if (decl_context != FIELD)
12762 {
12763 if (!current_function_decl)
12764 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12765 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
12766 /* The TYPE_DECL is "abstract" because there will be
12767 clones of this constructor/destructor, and there will
12768 be copies of this TYPE_DECL generated in those
12769 clones. The decloning optimization (for space) may
12770 revert this subsequently if it determines that
12771 the clones should share a common implementation. */
12772 DECL_ABSTRACT_P (decl) = true;
12773 }
12774 else if (current_class_type
12775 && constructor_name_p (unqualified_id, current_class_type))
12776 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
12777 "as enclosing class",
12778 unqualified_id);
12779
12780 /* If the user declares "typedef struct {...} foo" then the
12781 struct will have an anonymous name. Fill that name in now.
12782 Nothing can refer to it, so nothing needs know about the name
12783 change. */
12784 if (type != error_mark_node
12785 && unqualified_id
12786 && TYPE_NAME (type)
12787 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12788 && TYPE_UNNAMED_P (type)
12789 && declspecs->type_definition_p
12790 && attributes_naming_typedef_ok (*attrlist)
12791 && cp_type_quals (type) == TYPE_UNQUALIFIED)
12792 name_unnamed_type (type, decl);
12793
12794 if (signed_p
12795 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12796 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12797
12798 bad_specifiers (decl, BSP_TYPE, virtualp,
12799 memfn_quals != TYPE_UNQUALIFIED,
12800 inlinep, friendp, raises != NULL_TREE,
12801 declspecs->locations);
12802
12803 if (alias_p)
12804 /* Acknowledge that this was written:
12805 `using analias = atype;'. */
12806 TYPE_DECL_ALIAS_P (decl) = 1;
12807
12808 return decl;
12809 }
12810
12811 /* Detect the case of an array type of unspecified size
12812 which came, as such, direct from a typedef name.
12813 We must copy the type, so that the array's domain can be
12814 individually set by the object's initializer. */
12815
12816 if (type && typedef_type
12817 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12818 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12819 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12820
12821 /* Detect where we're using a typedef of function type to declare a
12822 function. PARMS will not be set, so we must create it now. */
12823
12824 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12825 {
12826 tree decls = NULL_TREE;
12827 tree args;
12828
12829 for (args = TYPE_ARG_TYPES (type);
12830 args && args != void_list_node;
12831 args = TREE_CHAIN (args))
12832 {
12833 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
12834 TREE_VALUE (args));
12835
12836 DECL_CHAIN (decl) = decls;
12837 decls = decl;
12838 }
12839
12840 parms = nreverse (decls);
12841
12842 if (decl_context != TYPENAME)
12843 {
12844 /* The qualifiers on the function type become the qualifiers on
12845 the non-static member function. */
12846 memfn_quals |= type_memfn_quals (type);
12847 rqual = type_memfn_rqual (type);
12848 type_quals = TYPE_UNQUALIFIED;
12849 }
12850 }
12851
12852 /* If this is a type name (such as, in a cast or sizeof),
12853 compute the type and return it now. */
12854
12855 if (decl_context == TYPENAME)
12856 {
12857 /* Note that here we don't care about type_quals. */
12858
12859 /* Special case: "friend class foo" looks like a TYPENAME context. */
12860 if (friendp)
12861 {
12862 if (inlinep)
12863 {
12864 error ("%<inline%> specified for friend class declaration");
12865 inlinep = 0;
12866 }
12867
12868 if (!current_aggr)
12869 {
12870 /* Don't allow friend declaration without a class-key. */
12871 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12872 permerror (input_location, "template parameters cannot be friends");
12873 else if (TREE_CODE (type) == TYPENAME_TYPE)
12874 permerror (input_location, "friend declaration requires class-key, "
12875 "i.e. %<friend class %T::%D%>",
12876 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
12877 else
12878 permerror (input_location, "friend declaration requires class-key, "
12879 "i.e. %<friend %#T%>",
12880 type);
12881 }
12882
12883 /* Only try to do this stuff if we didn't already give up. */
12884 if (type != integer_type_node)
12885 {
12886 /* A friendly class? */
12887 if (current_class_type)
12888 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
12889 /*complain=*/true);
12890 else
12891 error ("trying to make class %qT a friend of global scope",
12892 type);
12893
12894 type = void_type_node;
12895 }
12896 }
12897 else if (memfn_quals || rqual)
12898 {
12899 if (ctype == NULL_TREE
12900 && TREE_CODE (type) == METHOD_TYPE)
12901 ctype = TYPE_METHOD_BASETYPE (type);
12902
12903 if (ctype)
12904 type = build_memfn_type (type, ctype, memfn_quals, rqual);
12905 /* Core issue #547: need to allow this in template type args.
12906 Allow it in general in C++11 for alias-declarations. */
12907 else if ((template_type_arg || cxx_dialect >= cxx11)
12908 && TREE_CODE (type) == FUNCTION_TYPE)
12909 type = apply_memfn_quals (type, memfn_quals, rqual);
12910 else
12911 error ("invalid qualifiers on non-member function type");
12912 }
12913
12914 if (reqs)
12915 error_at (location_of (reqs), "requires-clause on type-id");
12916
12917 return type;
12918 }
12919 else if (unqualified_id == NULL_TREE && decl_context != PARM
12920 && decl_context != CATCHPARM
12921 && TREE_CODE (type) != UNION_TYPE
12922 && ! bitfield
12923 && innermost_code != cdk_decomp)
12924 {
12925 error ("abstract declarator %qT used as declaration", type);
12926 return error_mark_node;
12927 }
12928
12929 if (!FUNC_OR_METHOD_TYPE_P (type))
12930 {
12931 /* Only functions may be declared using an operator-function-id. */
12932 if (dname && IDENTIFIER_ANY_OP_P (dname))
12933 {
12934 error_at (id_loc, "declaration of %qD as non-function", dname);
12935 return error_mark_node;
12936 }
12937
12938 if (reqs)
12939 error_at (location_of (reqs),
12940 "requires-clause on declaration of non-function type %qT",
12941 type);
12942 }
12943
12944 /* We don't check parameter types here because we can emit a better
12945 error message later. */
12946 if (decl_context != PARM)
12947 {
12948 type = check_var_type (unqualified_id, type, id_loc);
12949 if (type == error_mark_node)
12950 return error_mark_node;
12951 }
12952
12953 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12954 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
12955
12956 if (decl_context == PARM || decl_context == CATCHPARM)
12957 {
12958 if (ctype || in_namespace)
12959 error ("cannot use %<::%> in parameter declaration");
12960
12961 tree auto_node = type_uses_auto (type);
12962 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
12963 {
12964 if (cxx_dialect >= cxx14)
12965 {
12966 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
12967 error_at (typespec_loc,
12968 "cannot declare a parameter with %<decltype(auto)%>");
12969 else
12970 error_at (typespec_loc,
12971 "%<auto%> parameter not permitted in this context");
12972 }
12973 else
12974 error_at (typespec_loc, "parameter declared %<auto%>");
12975 type = error_mark_node;
12976 }
12977
12978 /* A parameter declared as an array of T is really a pointer to T.
12979 One declared as a function is really a pointer to a function.
12980 One declared as a member is really a pointer to member. */
12981
12982 if (TREE_CODE (type) == ARRAY_TYPE)
12983 {
12984 /* Transfer const-ness of array into that of type pointed to. */
12985 type = build_pointer_type (TREE_TYPE (type));
12986 type_quals = TYPE_UNQUALIFIED;
12987 array_parameter_p = true;
12988 }
12989 else if (TREE_CODE (type) == FUNCTION_TYPE)
12990 type = build_pointer_type (type);
12991 }
12992
12993 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
12994 && !(unqualified_id
12995 && identifier_p (unqualified_id)
12996 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
12997 {
12998 cp_cv_quals real_quals = memfn_quals;
12999 if (cxx_dialect < cxx14 && constexpr_p
13000 && sfk != sfk_constructor && sfk != sfk_destructor)
13001 real_quals |= TYPE_QUAL_CONST;
13002 type = build_memfn_type (type, ctype, real_quals, rqual);
13003 }
13004
13005 {
13006 tree decl = NULL_TREE;
13007
13008 if (decl_context == PARM)
13009 {
13010 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13011 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13012
13013 bad_specifiers (decl, BSP_PARM, virtualp,
13014 memfn_quals != TYPE_UNQUALIFIED,
13015 inlinep, friendp, raises != NULL_TREE,
13016 declspecs->locations);
13017 }
13018 else if (decl_context == FIELD)
13019 {
13020 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13021 if (tree auto_node = type_uses_auto (type))
13022 {
13023 location_t tloc = declspecs->locations[ds_type_spec];
13024 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13025 error_at (tloc, "invalid use of template-name %qE without an "
13026 "argument list",
13027 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13028 else
13029 error_at (tloc, "non-static data member declared with "
13030 "placeholder %qT", auto_node);
13031 type = error_mark_node;
13032 }
13033
13034 /* The C99 flexible array extension. */
13035 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13036 && TYPE_DOMAIN (type) == NULL_TREE)
13037 {
13038 if (ctype
13039 && (TREE_CODE (ctype) == UNION_TYPE
13040 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13041 {
13042 error_at (id_loc, "flexible array member in union");
13043 type = error_mark_node;
13044 }
13045 else
13046 {
13047 /* Array is a flexible member. */
13048 if (name)
13049 pedwarn (id_loc, OPT_Wpedantic,
13050 "ISO C++ forbids flexible array member %qs", name);
13051 else
13052 pedwarn (input_location, OPT_Wpedantic,
13053 "ISO C++ forbids flexible array members");
13054
13055 /* Flexible array member has a null domain. */
13056 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13057 }
13058 }
13059
13060 if (type == error_mark_node)
13061 {
13062 /* Happens when declaring arrays of sizes which
13063 are error_mark_node, for example. */
13064 decl = NULL_TREE;
13065 }
13066 else if (in_namespace && !friendp)
13067 {
13068 /* Something like struct S { int N::j; }; */
13069 error_at (id_loc, "invalid use of %<::%>");
13070 return error_mark_node;
13071 }
13072 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13073 {
13074 int publicp = 0;
13075 tree function_context;
13076
13077 if (friendp == 0)
13078 {
13079 /* This should never happen in pure C++ (the check
13080 could be an assert). It could happen in
13081 Objective-C++ if someone writes invalid code that
13082 uses a function declaration for an instance
13083 variable or property (instance variables and
13084 properties are parsed as FIELD_DECLs, but they are
13085 part of an Objective-C class, not a C++ class).
13086 That code is invalid and is caught by this
13087 check. */
13088 if (!ctype)
13089 {
13090 error ("declaration of function %qD in invalid context",
13091 unqualified_id);
13092 return error_mark_node;
13093 }
13094
13095 /* ``A union may [ ... ] not [ have ] virtual functions.''
13096 ARM 9.5 */
13097 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13098 {
13099 error_at (declspecs->locations[ds_virtual],
13100 "function %qD declared %<virtual%> inside a union",
13101 unqualified_id);
13102 return error_mark_node;
13103 }
13104
13105 if (virtualp
13106 && identifier_p (unqualified_id)
13107 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13108 {
13109 error_at (declspecs->locations[ds_virtual],
13110 "%qD cannot be declared %<virtual%>, since it "
13111 "is always static", unqualified_id);
13112 virtualp = 0;
13113 }
13114 }
13115
13116 /* Check that the name used for a destructor makes sense. */
13117 if (sfk == sfk_destructor)
13118 {
13119 tree uqname = id_declarator->u.id.unqualified_name;
13120
13121 if (!ctype)
13122 {
13123 gcc_assert (friendp);
13124 error_at (id_loc, "expected qualified name in friend "
13125 "declaration for destructor %qD", uqname);
13126 return error_mark_node;
13127 }
13128
13129 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13130 {
13131 error_at (id_loc, "declaration of %qD as member of %qT",
13132 uqname, ctype);
13133 return error_mark_node;
13134 }
13135 if (concept_p)
13136 {
13137 error_at (declspecs->locations[ds_concept],
13138 "a destructor cannot be %qs", "concept");
13139 return error_mark_node;
13140 }
13141 if (constexpr_p && cxx_dialect < cxx2a)
13142 {
13143 error_at (declspecs->locations[ds_constexpr],
13144 "%<constexpr%> destructors only available"
13145 " with %<-std=c++2a%> or %<-std=gnu++2a%>");
13146 return error_mark_node;
13147 }
13148 if (consteval_p)
13149 {
13150 error_at (declspecs->locations[ds_consteval],
13151 "a destructor cannot be %qs", "consteval");
13152 return error_mark_node;
13153 }
13154 }
13155 else if (sfk == sfk_constructor && friendp && !ctype)
13156 {
13157 error ("expected qualified name in friend declaration "
13158 "for constructor %qD",
13159 id_declarator->u.id.unqualified_name);
13160 return error_mark_node;
13161 }
13162 if (sfk == sfk_constructor)
13163 if (concept_p)
13164 {
13165 error_at (declspecs->locations[ds_concept],
13166 "a constructor cannot be %<concept%>");
13167 return error_mark_node;
13168 }
13169 if (concept_p)
13170 {
13171 error_at (declspecs->locations[ds_concept],
13172 "a concept cannot be a member function");
13173 concept_p = false;
13174 }
13175 else if (consteval_p
13176 && identifier_p (unqualified_id)
13177 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13178 {
13179 error_at (declspecs->locations[ds_consteval],
13180 "%qD cannot be %qs", unqualified_id, "consteval");
13181 consteval_p = false;
13182 }
13183
13184 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13185 {
13186 tree tmpl = TREE_OPERAND (unqualified_id, 0);
13187 if (variable_template_p (tmpl))
13188 {
13189 error_at (id_loc, "specialization of variable template "
13190 "%qD declared as function", tmpl);
13191 inform (DECL_SOURCE_LOCATION (tmpl),
13192 "variable template declared here");
13193 return error_mark_node;
13194 }
13195 }
13196
13197 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13198 function_context
13199 = (ctype != NULL_TREE
13200 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13201 publicp = ((! friendp || ! staticp)
13202 && function_context == NULL_TREE);
13203
13204 decl = grokfndecl (ctype, type,
13205 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13206 ? unqualified_id : dname,
13207 parms,
13208 unqualified_id,
13209 declspecs,
13210 reqs,
13211 virtualp, flags, memfn_quals, rqual, raises,
13212 friendp ? -1 : 0, friendp, publicp,
13213 inlinep | (2 * constexpr_p) | (4 * concept_p)
13214 | (8 * consteval_p),
13215 initialized == SD_DELETED, sfk,
13216 funcdef_flag, late_return_type_p,
13217 template_count, in_namespace,
13218 attrlist, id_loc);
13219 decl = set_virt_specifiers (decl, virt_specifiers);
13220 if (decl == NULL_TREE)
13221 return error_mark_node;
13222 #if 0
13223 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13224 /* The decl and setting of decl_attr is also turned off. */
13225 decl = build_decl_attribute_variant (decl, decl_attr);
13226 #endif
13227
13228 /* [class.conv.ctor]
13229
13230 A constructor declared without the function-specifier
13231 explicit that can be called with a single parameter
13232 specifies a conversion from the type of its first
13233 parameter to the type of its class. Such a constructor
13234 is called a converting constructor. */
13235 if (explicitp == 2)
13236 DECL_NONCONVERTING_P (decl) = 1;
13237
13238 if (declspecs->explicit_specifier)
13239 store_explicit_specifier (decl, declspecs->explicit_specifier);
13240 }
13241 else if (!staticp && !dependent_type_p (type)
13242 && !COMPLETE_TYPE_P (complete_type (type))
13243 && (!complete_or_array_type_p (type)
13244 || initialized == 0))
13245 {
13246 if (TREE_CODE (type) != ARRAY_TYPE
13247 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
13248 {
13249 if (unqualified_id)
13250 {
13251 error_at (id_loc, "field %qD has incomplete type %qT",
13252 unqualified_id, type);
13253 cxx_incomplete_type_inform (strip_array_types (type));
13254 }
13255 else
13256 error ("name %qT has incomplete type", type);
13257
13258 type = error_mark_node;
13259 decl = NULL_TREE;
13260 }
13261 }
13262 else if (!verify_type_context (input_location,
13263 staticp
13264 ? TCTX_STATIC_STORAGE
13265 : TCTX_FIELD, type))
13266 {
13267 type = error_mark_node;
13268 decl = NULL_TREE;
13269 }
13270 else
13271 {
13272 if (friendp)
13273 {
13274 if (unqualified_id)
13275 error_at (id_loc,
13276 "%qE is neither function nor member function; "
13277 "cannot be declared friend", unqualified_id);
13278 else
13279 error ("unnamed field is neither function nor member "
13280 "function; cannot be declared friend");
13281 return error_mark_node;
13282 }
13283 decl = NULL_TREE;
13284 }
13285
13286 if (friendp)
13287 {
13288 /* Friends are treated specially. */
13289 if (ctype == current_class_type)
13290 ; /* We already issued a permerror. */
13291 else if (decl && DECL_NAME (decl))
13292 {
13293 if (initialized)
13294 /* Kludge: We need funcdef_flag to be true in do_friend for
13295 in-class defaulted functions, but that breaks grokfndecl.
13296 So set it here. */
13297 funcdef_flag = true;
13298 if (template_class_depth (current_class_type) == 0)
13299 {
13300 decl = check_explicit_specialization
13301 (unqualified_id, decl, template_count,
13302 2 * funcdef_flag + 4);
13303 if (decl == error_mark_node)
13304 return error_mark_node;
13305 }
13306
13307 decl = do_friend (ctype, unqualified_id, decl,
13308 *attrlist, flags,
13309 funcdef_flag);
13310 return decl;
13311 }
13312 else
13313 return error_mark_node;
13314 }
13315
13316 /* Structure field. It may not be a function, except for C++. */
13317
13318 if (decl == NULL_TREE)
13319 {
13320 if (staticp)
13321 {
13322 /* C++ allows static class members. All other work
13323 for this is done by grokfield. */
13324 decl = build_lang_decl_loc (id_loc, VAR_DECL,
13325 unqualified_id, type);
13326 set_linkage_for_static_data_member (decl);
13327 if (concept_p)
13328 error_at (declspecs->locations[ds_concept],
13329 "static data member %qE declared %qs",
13330 unqualified_id, "concept");
13331 else if (constexpr_p && !initialized)
13332 {
13333 error_at (DECL_SOURCE_LOCATION (decl),
13334 "%<constexpr%> static data member %qD must "
13335 "have an initializer", decl);
13336 constexpr_p = false;
13337 }
13338 if (consteval_p)
13339 error_at (declspecs->locations[ds_consteval],
13340 "static data member %qE declared %qs",
13341 unqualified_id, "consteval");
13342
13343 if (inlinep)
13344 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13345
13346 if (!DECL_VAR_DECLARED_INLINE_P (decl)
13347 && !(cxx_dialect >= cxx17 && constexpr_p))
13348 /* Even if there is an in-class initialization, DECL
13349 is considered undefined until an out-of-class
13350 definition is provided, unless this is an inline
13351 variable. */
13352 DECL_EXTERNAL (decl) = 1;
13353
13354 if (thread_p)
13355 {
13356 CP_DECL_THREAD_LOCAL_P (decl) = true;
13357 if (!processing_template_decl)
13358 set_decl_tls_model (decl, decl_default_tls_model (decl));
13359 if (declspecs->gnu_thread_keyword_p)
13360 SET_DECL_GNU_TLS_P (decl);
13361 }
13362 }
13363 else
13364 {
13365 if (concept_p)
13366 {
13367 error_at (declspecs->locations[ds_concept],
13368 "non-static data member %qE declared %qs",
13369 unqualified_id, "concept");
13370 concept_p = false;
13371 constexpr_p = false;
13372 }
13373 else if (constexpr_p)
13374 {
13375 error_at (declspecs->locations[ds_constexpr],
13376 "non-static data member %qE declared %qs",
13377 unqualified_id, "constexpr");
13378 constexpr_p = false;
13379 }
13380 if (constinit_p)
13381 {
13382 error_at (declspecs->locations[ds_constinit],
13383 "non-static data member %qE declared %qs",
13384 unqualified_id, "constinit");
13385 constinit_p = false;
13386 }
13387 if (consteval_p)
13388 {
13389 error_at (declspecs->locations[ds_consteval],
13390 "non-static data member %qE declared %qs",
13391 unqualified_id, "consteval");
13392 consteval_p = false;
13393 }
13394 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13395 DECL_NONADDRESSABLE_P (decl) = bitfield;
13396 if (bitfield && !unqualified_id)
13397 {
13398 TREE_NO_WARNING (decl) = 1;
13399 DECL_PADDING_P (decl) = 1;
13400 }
13401
13402 if (storage_class == sc_mutable)
13403 {
13404 DECL_MUTABLE_P (decl) = 1;
13405 storage_class = sc_none;
13406 }
13407
13408 if (initialized)
13409 {
13410 /* An attempt is being made to initialize a non-static
13411 member. This is new in C++11. */
13412 maybe_warn_cpp0x (CPP0X_NSDMI);
13413
13414 /* If this has been parsed with static storage class, but
13415 errors forced staticp to be cleared, ensure NSDMI is
13416 not present. */
13417 if (declspecs->storage_class == sc_static)
13418 DECL_INITIAL (decl) = error_mark_node;
13419 }
13420 }
13421
13422 bad_specifiers (decl, BSP_FIELD, virtualp,
13423 memfn_quals != TYPE_UNQUALIFIED,
13424 staticp ? false : inlinep, friendp,
13425 raises != NULL_TREE,
13426 declspecs->locations);
13427 }
13428 }
13429 else if (FUNC_OR_METHOD_TYPE_P (type))
13430 {
13431 tree original_name;
13432 int publicp = 0;
13433
13434 if (!unqualified_id)
13435 return error_mark_node;
13436
13437 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13438 original_name = dname;
13439 else
13440 original_name = unqualified_id;
13441 // FIXME:gcc_assert (original_name == dname);
13442
13443 if (storage_class == sc_auto)
13444 error_at (declspecs->locations[ds_storage_class],
13445 "storage class %<auto%> invalid for function %qs", name);
13446 else if (storage_class == sc_register)
13447 error_at (declspecs->locations[ds_storage_class],
13448 "storage class %<register%> invalid for function %qs",
13449 name);
13450 else if (thread_p)
13451 {
13452 if (declspecs->gnu_thread_keyword_p)
13453 error_at (declspecs->locations[ds_thread],
13454 "storage class %<__thread%> invalid for function %qs",
13455 name);
13456 else
13457 error_at (declspecs->locations[ds_thread],
13458 "storage class %<thread_local%> invalid for "
13459 "function %qs", name);
13460 }
13461
13462 if (virt_specifiers)
13463 error ("virt-specifiers in %qs not allowed outside a class "
13464 "definition", name);
13465 /* Function declaration not at top level.
13466 Storage classes other than `extern' are not allowed
13467 and `extern' makes no difference. */
13468 if (! toplevel_bindings_p ()
13469 && (storage_class == sc_static
13470 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
13471 && pedantic)
13472 {
13473 if (storage_class == sc_static)
13474 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13475 "%<static%> specifier invalid for function %qs "
13476 "declared out of global scope", name);
13477 else
13478 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13479 "%<inline%> specifier invalid for function %qs "
13480 "declared out of global scope", name);
13481 }
13482
13483 if (ctype == NULL_TREE)
13484 {
13485 if (virtualp)
13486 {
13487 error ("virtual non-class function %qs", name);
13488 virtualp = 0;
13489 }
13490 else if (sfk == sfk_constructor
13491 || sfk == sfk_destructor)
13492 {
13493 error (funcdef_flag
13494 ? G_("%qs defined in a non-class scope")
13495 : G_("%qs declared in a non-class scope"), name);
13496 sfk = sfk_none;
13497 }
13498 }
13499 if (consteval_p
13500 && identifier_p (unqualified_id)
13501 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13502 {
13503 error_at (declspecs->locations[ds_consteval],
13504 "%qD cannot be %qs", unqualified_id, "consteval");
13505 consteval_p = false;
13506 }
13507
13508 /* Record whether the function is public. */
13509 publicp = (ctype != NULL_TREE
13510 || storage_class != sc_static);
13511
13512 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13513 declspecs,
13514 reqs, virtualp, flags, memfn_quals, rqual, raises,
13515 1, friendp,
13516 publicp,
13517 inlinep | (2 * constexpr_p) | (4 * concept_p)
13518 | (8 * consteval_p),
13519 initialized == SD_DELETED,
13520 sfk,
13521 funcdef_flag,
13522 late_return_type_p,
13523 template_count, in_namespace, attrlist,
13524 id_loc);
13525 if (decl == NULL_TREE)
13526 return error_mark_node;
13527
13528 if (explicitp == 2)
13529 DECL_NONCONVERTING_P (decl) = 1;
13530 if (staticp == 1)
13531 {
13532 int invalid_static = 0;
13533
13534 /* Don't allow a static member function in a class, and forbid
13535 declaring main to be static. */
13536 if (TREE_CODE (type) == METHOD_TYPE)
13537 {
13538 permerror (input_location, "cannot declare member function %qD to have "
13539 "static linkage", decl);
13540 invalid_static = 1;
13541 }
13542 else if (current_function_decl)
13543 {
13544 /* 7.1.1: There can be no static function declarations within a
13545 block. */
13546 error_at (declspecs->locations[ds_storage_class],
13547 "cannot declare static function inside another function");
13548 invalid_static = 1;
13549 }
13550
13551 if (invalid_static)
13552 {
13553 staticp = 0;
13554 storage_class = sc_none;
13555 }
13556 }
13557 }
13558 else
13559 {
13560 /* It's a variable. */
13561
13562 /* An uninitialized decl with `extern' is a reference. */
13563 decl = grokvardecl (type, dname, unqualified_id,
13564 declspecs,
13565 initialized,
13566 type_quals,
13567 inlinep,
13568 concept_p,
13569 template_count,
13570 ctype ? ctype : in_namespace,
13571 id_loc);
13572 if (decl == NULL_TREE)
13573 return error_mark_node;
13574
13575 bad_specifiers (decl, BSP_VAR, virtualp,
13576 memfn_quals != TYPE_UNQUALIFIED,
13577 inlinep, friendp, raises != NULL_TREE,
13578 declspecs->locations);
13579
13580 if (ctype)
13581 {
13582 DECL_CONTEXT (decl) = ctype;
13583 if (staticp == 1)
13584 {
13585 permerror (declspecs->locations[ds_storage_class],
13586 "%<static%> may not be used when defining "
13587 "(as opposed to declaring) a static data member");
13588 staticp = 0;
13589 storage_class = sc_none;
13590 }
13591 if (storage_class == sc_register && TREE_STATIC (decl))
13592 {
13593 error ("static member %qD declared %<register%>", decl);
13594 storage_class = sc_none;
13595 }
13596 if (storage_class == sc_extern && pedantic)
13597 {
13598 pedwarn (input_location, OPT_Wpedantic,
13599 "cannot explicitly declare member %q#D to have "
13600 "extern linkage", decl);
13601 storage_class = sc_none;
13602 }
13603 }
13604 else if (constexpr_p && DECL_EXTERNAL (decl))
13605 {
13606 error_at (DECL_SOURCE_LOCATION (decl),
13607 "declaration of %<constexpr%> variable %qD "
13608 "is not a definition", decl);
13609 constexpr_p = false;
13610 }
13611 if (consteval_p)
13612 {
13613 error_at (DECL_SOURCE_LOCATION (decl),
13614 "a variable cannot be declared %<consteval%>");
13615 consteval_p = false;
13616 }
13617
13618 if (inlinep)
13619 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13620 if (innermost_code == cdk_decomp)
13621 {
13622 gcc_assert (declarator && declarator->kind == cdk_decomp);
13623 DECL_SOURCE_LOCATION (decl) = id_loc;
13624 DECL_ARTIFICIAL (decl) = 1;
13625 fit_decomposition_lang_decl (decl, NULL_TREE);
13626 }
13627 }
13628
13629 if (VAR_P (decl) && !initialized)
13630 if (tree auto_node = type_uses_auto (type))
13631 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13632 {
13633 location_t loc = declspecs->locations[ds_type_spec];
13634 error_at (loc, "declaration of %q#D has no initializer", decl);
13635 TREE_TYPE (decl) = error_mark_node;
13636 }
13637
13638 if (storage_class == sc_extern && initialized && !funcdef_flag)
13639 {
13640 if (toplevel_bindings_p ())
13641 {
13642 /* It's common practice (and completely valid) to have a const
13643 be initialized and declared extern. */
13644 if (!(type_quals & TYPE_QUAL_CONST))
13645 warning_at (DECL_SOURCE_LOCATION (decl), 0,
13646 "%qs initialized and declared %<extern%>", name);
13647 }
13648 else
13649 {
13650 error_at (DECL_SOURCE_LOCATION (decl),
13651 "%qs has both %<extern%> and initializer", name);
13652 return error_mark_node;
13653 }
13654 }
13655
13656 /* Record `register' declaration for warnings on &
13657 and in case doing stupid register allocation. */
13658
13659 if (storage_class == sc_register)
13660 {
13661 DECL_REGISTER (decl) = 1;
13662 /* Warn about register storage specifiers on PARM_DECLs. */
13663 if (TREE_CODE (decl) == PARM_DECL)
13664 {
13665 if (cxx_dialect >= cxx17)
13666 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13667 "ISO C++17 does not allow %<register%> storage "
13668 "class specifier");
13669 else
13670 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13671 "%<register%> storage class specifier used");
13672 }
13673 }
13674 else if (storage_class == sc_extern)
13675 DECL_THIS_EXTERN (decl) = 1;
13676 else if (storage_class == sc_static)
13677 DECL_THIS_STATIC (decl) = 1;
13678
13679 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13680 if (constexpr_p && VAR_P (decl))
13681 DECL_DECLARED_CONSTEXPR_P (decl) = true;
13682
13683 /* Record constancy and volatility on the DECL itself . There's
13684 no need to do this when processing a template; we'll do this
13685 for the instantiated declaration based on the type of DECL. */
13686 if (!processing_template_decl)
13687 cp_apply_type_quals_to_decl (type_quals, decl);
13688
13689 return decl;
13690 }
13691 }
13692 \f
13693 /* Subroutine of start_function. Ensure that each of the parameter
13694 types (as listed in PARMS) is complete, as is required for a
13695 function definition. */
13696
13697 static void
13698 require_complete_types_for_parms (tree parms)
13699 {
13700 for (; parms; parms = DECL_CHAIN (parms))
13701 {
13702 if (dependent_type_p (TREE_TYPE (parms)))
13703 continue;
13704 if (!VOID_TYPE_P (TREE_TYPE (parms))
13705 && complete_type_or_else (TREE_TYPE (parms), parms))
13706 {
13707 relayout_decl (parms);
13708 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
13709
13710 maybe_warn_parm_abi (TREE_TYPE (parms),
13711 DECL_SOURCE_LOCATION (parms));
13712 }
13713 else
13714 /* grokparms or complete_type_or_else will have already issued
13715 an error. */
13716 TREE_TYPE (parms) = error_mark_node;
13717 }
13718 }
13719
13720 /* Returns nonzero if T is a local variable. */
13721
13722 int
13723 local_variable_p (const_tree t)
13724 {
13725 if ((VAR_P (t)
13726 /* A VAR_DECL with a context that is a _TYPE is a static data
13727 member. */
13728 && !TYPE_P (CP_DECL_CONTEXT (t))
13729 /* Any other non-local variable must be at namespace scope. */
13730 && !DECL_NAMESPACE_SCOPE_P (t))
13731 || (TREE_CODE (t) == PARM_DECL))
13732 return 1;
13733
13734 return 0;
13735 }
13736
13737 /* Like local_variable_p, but suitable for use as a tree-walking
13738 function. */
13739
13740 static tree
13741 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
13742 void * /*data*/)
13743 {
13744 if (local_variable_p (*tp)
13745 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
13746 return *tp;
13747 else if (TYPE_P (*tp))
13748 *walk_subtrees = 0;
13749
13750 return NULL_TREE;
13751 }
13752
13753 /* Check that ARG, which is a default-argument expression for a
13754 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13755 something goes wrong. DECL may also be a _TYPE node, rather than a
13756 DECL, if there is no DECL available. */
13757
13758 tree
13759 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
13760 {
13761 tree var;
13762 tree decl_type;
13763
13764 if (TREE_CODE (arg) == DEFERRED_PARSE)
13765 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13766 with a default argument. Ignore the argument for now; we'll
13767 deal with it after the class is complete. */
13768 return arg;
13769
13770 if (TYPE_P (decl))
13771 {
13772 decl_type = decl;
13773 decl = NULL_TREE;
13774 }
13775 else
13776 decl_type = TREE_TYPE (decl);
13777
13778 if (arg == error_mark_node
13779 || decl == error_mark_node
13780 || TREE_TYPE (arg) == error_mark_node
13781 || decl_type == error_mark_node)
13782 /* Something already went wrong. There's no need to check
13783 further. */
13784 return error_mark_node;
13785
13786 /* [dcl.fct.default]
13787
13788 A default argument expression is implicitly converted to the
13789 parameter type. */
13790 ++cp_unevaluated_operand;
13791 /* Avoid digest_init clobbering the initializer. */
13792 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
13793 perform_implicit_conversion_flags (decl_type, carg, complain,
13794 LOOKUP_IMPLICIT);
13795 --cp_unevaluated_operand;
13796
13797 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13798 the call sites. */
13799 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
13800 && null_ptr_cst_p (arg)
13801 /* Don't lose side-effects as in PR90473. */
13802 && !TREE_SIDE_EFFECTS (arg))
13803 return nullptr_node;
13804
13805 /* [dcl.fct.default]
13806
13807 Local variables shall not be used in default argument
13808 expressions.
13809
13810 The keyword `this' shall not be used in a default argument of a
13811 member function. */
13812 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
13813 if (var)
13814 {
13815 if (complain & tf_warning_or_error)
13816 {
13817 if (DECL_NAME (var) == this_identifier)
13818 permerror (input_location, "default argument %qE uses %qD",
13819 arg, var);
13820 else
13821 error ("default argument %qE uses local variable %qD", arg, var);
13822 }
13823 return error_mark_node;
13824 }
13825
13826 /* All is well. */
13827 return arg;
13828 }
13829
13830 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
13831
13832 static tree
13833 type_is_deprecated (tree type)
13834 {
13835 enum tree_code code;
13836 if (TREE_DEPRECATED (type))
13837 return type;
13838 if (TYPE_NAME (type))
13839 {
13840 if (TREE_DEPRECATED (TYPE_NAME (type)))
13841 return type;
13842 else
13843 {
13844 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
13845 return NULL_TREE;
13846 }
13847 }
13848
13849 /* Do warn about using typedefs to a deprecated class. */
13850 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
13851 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
13852
13853 code = TREE_CODE (type);
13854
13855 if (code == POINTER_TYPE || code == REFERENCE_TYPE
13856 || code == OFFSET_TYPE || code == FUNCTION_TYPE
13857 || code == METHOD_TYPE || code == ARRAY_TYPE)
13858 return type_is_deprecated (TREE_TYPE (type));
13859
13860 if (TYPE_PTRMEMFUNC_P (type))
13861 return type_is_deprecated
13862 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
13863
13864 return NULL_TREE;
13865 }
13866
13867 /* Decode the list of parameter types for a function type.
13868 Given the list of things declared inside the parens,
13869 return a list of types.
13870
13871 If this parameter does not end with an ellipsis, we append
13872 void_list_node.
13873
13874 *PARMS is set to the chain of PARM_DECLs created. */
13875
13876 tree
13877 grokparms (tree parmlist, tree *parms)
13878 {
13879 tree result = NULL_TREE;
13880 tree decls = NULL_TREE;
13881 tree parm;
13882 int any_error = 0;
13883
13884 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
13885 {
13886 tree type = NULL_TREE;
13887 tree init = TREE_PURPOSE (parm);
13888 tree decl = TREE_VALUE (parm);
13889
13890 if (parm == void_list_node)
13891 break;
13892
13893 if (! decl || TREE_TYPE (decl) == error_mark_node)
13894 continue;
13895
13896 type = TREE_TYPE (decl);
13897 if (VOID_TYPE_P (type))
13898 {
13899 if (same_type_p (type, void_type_node)
13900 && !init
13901 && !DECL_NAME (decl) && !result
13902 && TREE_CHAIN (parm) == void_list_node)
13903 /* DR 577: A parameter list consisting of a single
13904 unnamed parameter of non-dependent type 'void'. */
13905 break;
13906 else if (cv_qualified_p (type))
13907 error_at (DECL_SOURCE_LOCATION (decl),
13908 "invalid use of cv-qualified type %qT in "
13909 "parameter declaration", type);
13910 else
13911 error_at (DECL_SOURCE_LOCATION (decl),
13912 "invalid use of type %<void%> in parameter "
13913 "declaration");
13914 /* It's not a good idea to actually create parameters of
13915 type `void'; other parts of the compiler assume that a
13916 void type terminates the parameter list. */
13917 type = error_mark_node;
13918 TREE_TYPE (decl) = error_mark_node;
13919 }
13920
13921 if (type != error_mark_node)
13922 {
13923 if (deprecated_state != DEPRECATED_SUPPRESS)
13924 {
13925 tree deptype = type_is_deprecated (type);
13926 if (deptype)
13927 cp_warn_deprecated_use (deptype);
13928 }
13929
13930 /* [dcl.fct] "A parameter with volatile-qualified type is
13931 deprecated." */
13932 if (CP_TYPE_VOLATILE_P (type))
13933 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
13934 "%<volatile%>-qualified parameter is "
13935 "deprecated");
13936
13937 /* Top-level qualifiers on the parameters are
13938 ignored for function types. */
13939 type = cp_build_qualified_type (type, 0);
13940 if (TREE_CODE (type) == METHOD_TYPE)
13941 {
13942 error ("parameter %qD invalidly declared method type", decl);
13943 type = build_pointer_type (type);
13944 TREE_TYPE (decl) = type;
13945 }
13946 else if (abstract_virtuals_error (decl, type))
13947 any_error = 1; /* Seems like a good idea. */
13948 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
13949 {
13950 /* Before C++17 DR 393:
13951 [dcl.fct]/6, parameter types cannot contain pointers
13952 (references) to arrays of unknown bound. */
13953 tree t = TREE_TYPE (type);
13954 int ptr = TYPE_PTR_P (type);
13955
13956 while (1)
13957 {
13958 if (TYPE_PTR_P (t))
13959 ptr = 1;
13960 else if (TREE_CODE (t) != ARRAY_TYPE)
13961 break;
13962 else if (!TYPE_DOMAIN (t))
13963 break;
13964 t = TREE_TYPE (t);
13965 }
13966 if (TREE_CODE (t) == ARRAY_TYPE)
13967 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13968 ptr
13969 ? G_("parameter %qD includes pointer to array of "
13970 "unknown bound %qT")
13971 : G_("parameter %qD includes reference to array of "
13972 "unknown bound %qT"),
13973 decl, t);
13974 }
13975
13976 if (any_error)
13977 init = NULL_TREE;
13978 else if (init && !processing_template_decl)
13979 init = check_default_argument (decl, init, tf_warning_or_error);
13980 }
13981
13982 DECL_CHAIN (decl) = decls;
13983 decls = decl;
13984 result = tree_cons (init, type, result);
13985 }
13986 decls = nreverse (decls);
13987 result = nreverse (result);
13988 if (parm)
13989 result = chainon (result, void_list_node);
13990 *parms = decls;
13991
13992 return result;
13993 }
13994
13995 \f
13996 /* D is a constructor or overloaded `operator='.
13997
13998 Let T be the class in which D is declared. Then, this function
13999 returns:
14000
14001 -1 if D's is an ill-formed constructor or copy assignment operator
14002 whose first parameter is of type `T'.
14003 0 if D is not a copy constructor or copy assignment
14004 operator.
14005 1 if D is a copy constructor or copy assignment operator whose
14006 first parameter is a reference to non-const qualified T.
14007 2 if D is a copy constructor or copy assignment operator whose
14008 first parameter is a reference to const qualified T.
14009
14010 This function can be used as a predicate. Positive values indicate
14011 a copy constructor and nonzero values indicate a copy assignment
14012 operator. */
14013
14014 int
14015 copy_fn_p (const_tree d)
14016 {
14017 tree args;
14018 tree arg_type;
14019 int result = 1;
14020
14021 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14022
14023 if (TREE_CODE (d) == TEMPLATE_DECL
14024 || (DECL_TEMPLATE_INFO (d)
14025 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14026 /* Instantiations of template member functions are never copy
14027 functions. Note that member functions of templated classes are
14028 represented as template functions internally, and we must
14029 accept those as copy functions. */
14030 return 0;
14031
14032 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14033 if (!args)
14034 return 0;
14035
14036 arg_type = TREE_VALUE (args);
14037 if (arg_type == error_mark_node)
14038 return 0;
14039
14040 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14041 {
14042 /* Pass by value copy assignment operator. */
14043 result = -1;
14044 }
14045 else if (TYPE_REF_P (arg_type)
14046 && !TYPE_REF_IS_RVALUE (arg_type)
14047 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14048 {
14049 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14050 result = 2;
14051 }
14052 else
14053 return 0;
14054
14055 args = TREE_CHAIN (args);
14056
14057 if (args && args != void_list_node && !TREE_PURPOSE (args))
14058 /* There are more non-optional args. */
14059 return 0;
14060
14061 return result;
14062 }
14063
14064 /* D is a constructor or overloaded `operator='.
14065
14066 Let T be the class in which D is declared. Then, this function
14067 returns true when D is a move constructor or move assignment
14068 operator, false otherwise. */
14069
14070 bool
14071 move_fn_p (const_tree d)
14072 {
14073 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14074
14075 if (cxx_dialect == cxx98)
14076 /* There are no move constructors if we are in C++98 mode. */
14077 return false;
14078
14079 if (TREE_CODE (d) == TEMPLATE_DECL
14080 || (DECL_TEMPLATE_INFO (d)
14081 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14082 /* Instantiations of template member functions are never move
14083 functions. Note that member functions of templated classes are
14084 represented as template functions internally, and we must
14085 accept those as move functions. */
14086 return 0;
14087
14088 return move_signature_fn_p (d);
14089 }
14090
14091 /* D is a constructor or overloaded `operator='.
14092
14093 Then, this function returns true when D has the same signature as a move
14094 constructor or move assignment operator (because either it is such a
14095 ctor/op= or it is a template specialization with the same signature),
14096 false otherwise. */
14097
14098 bool
14099 move_signature_fn_p (const_tree d)
14100 {
14101 tree args;
14102 tree arg_type;
14103 bool result = false;
14104
14105 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14106 if (!args)
14107 return 0;
14108
14109 arg_type = TREE_VALUE (args);
14110 if (arg_type == error_mark_node)
14111 return 0;
14112
14113 if (TYPE_REF_P (arg_type)
14114 && TYPE_REF_IS_RVALUE (arg_type)
14115 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14116 DECL_CONTEXT (d)))
14117 result = true;
14118
14119 args = TREE_CHAIN (args);
14120
14121 if (args && args != void_list_node && !TREE_PURPOSE (args))
14122 /* There are more non-optional args. */
14123 return false;
14124
14125 return result;
14126 }
14127
14128 /* Remember any special properties of member function DECL. */
14129
14130 void
14131 grok_special_member_properties (tree decl)
14132 {
14133 tree class_type;
14134
14135 if (TREE_CODE (decl) == USING_DECL
14136 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14137 return;
14138
14139 class_type = DECL_CONTEXT (decl);
14140 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14141 {
14142 int ctor = copy_fn_p (decl);
14143
14144 if (!DECL_ARTIFICIAL (decl))
14145 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14146
14147 if (ctor > 0)
14148 {
14149 /* [class.copy]
14150
14151 A non-template constructor for class X is a copy
14152 constructor if its first parameter is of type X&, const
14153 X&, volatile X& or const volatile X&, and either there
14154 are no other parameters or else all other parameters have
14155 default arguments. */
14156 TYPE_HAS_COPY_CTOR (class_type) = 1;
14157 if (ctor > 1)
14158 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14159 }
14160 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14161 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14162 else if (is_list_ctor (decl))
14163 TYPE_HAS_LIST_CTOR (class_type) = 1;
14164
14165 if (DECL_DECLARED_CONSTEXPR_P (decl)
14166 && !ctor && !move_fn_p (decl))
14167 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14168 }
14169 else if (DECL_NAME (decl) == assign_op_identifier)
14170 {
14171 /* [class.copy]
14172
14173 A non-template assignment operator for class X is a copy
14174 assignment operator if its parameter is of type X, X&, const
14175 X&, volatile X& or const volatile X&. */
14176
14177 int assop = copy_fn_p (decl);
14178
14179 if (assop)
14180 {
14181 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14182 if (assop != 1)
14183 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14184 }
14185 }
14186 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14187 TYPE_HAS_CONVERSION (class_type) = true;
14188
14189 /* Destructors are handled in check_methods. */
14190 }
14191
14192 /* Check a constructor DECL has the correct form. Complains
14193 if the class has a constructor of the form X(X). */
14194
14195 bool
14196 grok_ctor_properties (const_tree ctype, const_tree decl)
14197 {
14198 int ctor_parm = copy_fn_p (decl);
14199
14200 if (ctor_parm < 0)
14201 {
14202 /* [class.copy]
14203
14204 A declaration of a constructor for a class X is ill-formed if
14205 its first parameter is of type (optionally cv-qualified) X
14206 and either there are no other parameters or else all other
14207 parameters have default arguments.
14208
14209 We *don't* complain about member template instantiations that
14210 have this form, though; they can occur as we try to decide
14211 what constructor to use during overload resolution. Since
14212 overload resolution will never prefer such a constructor to
14213 the non-template copy constructor (which is either explicitly
14214 or implicitly defined), there's no need to worry about their
14215 existence. Theoretically, they should never even be
14216 instantiated, but that's hard to forestall. */
14217 error_at (DECL_SOURCE_LOCATION (decl),
14218 "invalid constructor; you probably meant %<%T (const %T&)%>",
14219 ctype, ctype);
14220 return false;
14221 }
14222
14223 return true;
14224 }
14225
14226 /* DECL is a declaration for an overloaded or conversion operator. If
14227 COMPLAIN is true, errors are issued for invalid declarations. */
14228
14229 bool
14230 grok_op_properties (tree decl, bool complain)
14231 {
14232 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14233 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14234 tree name = DECL_NAME (decl);
14235 location_t loc = DECL_SOURCE_LOCATION (decl);
14236
14237 tree class_type = DECL_CONTEXT (decl);
14238 if (class_type && !CLASS_TYPE_P (class_type))
14239 class_type = NULL_TREE;
14240
14241 tree_code operator_code;
14242 unsigned op_flags;
14243 if (IDENTIFIER_CONV_OP_P (name))
14244 {
14245 /* Conversion operators are TYPE_EXPR for the purposes of this
14246 function. */
14247 operator_code = TYPE_EXPR;
14248 op_flags = OVL_OP_FLAG_UNARY;
14249 }
14250 else
14251 {
14252 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14253
14254 operator_code = ovl_op->tree_code;
14255 op_flags = ovl_op->flags;
14256 gcc_checking_assert (operator_code != ERROR_MARK);
14257 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14258 }
14259
14260 if (op_flags & OVL_OP_FLAG_ALLOC)
14261 {
14262 /* operator new and operator delete are quite special. */
14263 if (class_type)
14264 switch (op_flags)
14265 {
14266 case OVL_OP_FLAG_ALLOC:
14267 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14268 break;
14269
14270 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14271 TYPE_GETS_DELETE (class_type) |= 1;
14272 break;
14273
14274 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14275 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14276 break;
14277
14278 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14279 TYPE_GETS_DELETE (class_type) |= 2;
14280 break;
14281
14282 default:
14283 gcc_unreachable ();
14284 }
14285
14286 /* [basic.std.dynamic.allocation]/1:
14287
14288 A program is ill-formed if an allocation function is declared
14289 in a namespace scope other than global scope or declared
14290 static in global scope.
14291
14292 The same also holds true for deallocation functions. */
14293 if (DECL_NAMESPACE_SCOPE_P (decl))
14294 {
14295 if (CP_DECL_CONTEXT (decl) != global_namespace)
14296 {
14297 error_at (loc, "%qD may not be declared within a namespace",
14298 decl);
14299 return false;
14300 }
14301
14302 if (!TREE_PUBLIC (decl))
14303 {
14304 error_at (loc, "%qD may not be declared as static", decl);
14305 return false;
14306 }
14307 }
14308
14309 if (op_flags & OVL_OP_FLAG_DELETE)
14310 {
14311 DECL_SET_IS_OPERATOR_DELETE (decl, true);
14312 coerce_delete_type (decl, loc);
14313 }
14314 else
14315 {
14316 DECL_SET_IS_OPERATOR_NEW (decl, true);
14317 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
14318 }
14319
14320 return true;
14321 }
14322
14323 /* An operator function must either be a non-static member function
14324 or have at least one parameter of a class, a reference to a class,
14325 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14326 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
14327 {
14328 if (operator_code == TYPE_EXPR
14329 || operator_code == CALL_EXPR
14330 || operator_code == COMPONENT_REF
14331 || operator_code == ARRAY_REF
14332 || operator_code == NOP_EXPR)
14333 {
14334 error_at (loc, "%qD must be a nonstatic member function", decl);
14335 return false;
14336 }
14337
14338 if (DECL_STATIC_FUNCTION_P (decl))
14339 {
14340 error_at (loc, "%qD must be either a non-static member "
14341 "function or a non-member function", decl);
14342 return false;
14343 }
14344
14345 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
14346 {
14347 if (!arg || arg == void_list_node)
14348 {
14349 if (complain)
14350 error_at(loc, "%qD must have an argument of class or "
14351 "enumerated type", decl);
14352 return false;
14353 }
14354
14355 tree type = non_reference (TREE_VALUE (arg));
14356 if (type == error_mark_node)
14357 return false;
14358
14359 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14360 because these checks are performed even on template
14361 functions. */
14362 if (MAYBE_CLASS_TYPE_P (type)
14363 || TREE_CODE (type) == ENUMERAL_TYPE)
14364 break;
14365 }
14366 }
14367
14368 if (operator_code == CALL_EXPR)
14369 /* There are no further restrictions on the arguments to an overloaded
14370 "operator ()". */
14371 return true;
14372
14373 if (operator_code == COND_EXPR)
14374 {
14375 /* 13.4.0.3 */
14376 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
14377 return false;
14378 }
14379
14380 /* Count the number of arguments and check for ellipsis. */
14381 int arity = 0;
14382 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14383 {
14384 if (!arg)
14385 {
14386 /* Variadic. */
14387 error_at (loc, "%qD must not have variable number of arguments",
14388 decl);
14389 return false;
14390 }
14391 ++arity;
14392 }
14393
14394 /* Verify correct number of arguments. */
14395 switch (op_flags)
14396 {
14397 case OVL_OP_FLAG_AMBIARY:
14398 if (arity == 1)
14399 {
14400 /* We have a unary instance of an ambi-ary op. Remap to the
14401 unary one. */
14402 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
14403 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
14404 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14405 operator_code = ovl_op->tree_code;
14406 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14407 }
14408 else if (arity != 2)
14409 {
14410 /* This was an ambiguous operator but is invalid. */
14411 error_at (loc,
14412 methodp
14413 ? G_("%qD must have either zero or one argument")
14414 : G_("%qD must have either one or two arguments"), decl);
14415 return false;
14416 }
14417 else if ((operator_code == POSTINCREMENT_EXPR
14418 || operator_code == POSTDECREMENT_EXPR)
14419 && ! processing_template_decl
14420 /* x++ and x--'s second argument must be an int. */
14421 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14422 integer_type_node))
14423 {
14424 error_at (loc,
14425 methodp
14426 ? G_("postfix %qD must have %<int%> as its argument")
14427 : G_("postfix %qD must have %<int%> as its second argument"),
14428 decl);
14429 return false;
14430 }
14431 break;
14432
14433 case OVL_OP_FLAG_UNARY:
14434 if (arity != 1)
14435 {
14436 error_at (loc,
14437 methodp
14438 ? G_("%qD must have no arguments")
14439 : G_("%qD must have exactly one argument"), decl);
14440 return false;
14441 }
14442 break;
14443
14444 case OVL_OP_FLAG_BINARY:
14445 if (arity != 2)
14446 {
14447 error_at (loc,
14448 methodp
14449 ? G_("%qD must have exactly one argument")
14450 : G_("%qD must have exactly two arguments"), decl);
14451 return false;
14452 }
14453 break;
14454
14455 default:
14456 gcc_unreachable ();
14457 }
14458
14459 /* There can be no default arguments. */
14460 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14461 if (TREE_PURPOSE (arg))
14462 {
14463 TREE_PURPOSE (arg) = NULL_TREE;
14464 error_at (loc, "%qD cannot have default arguments", decl);
14465 return false;
14466 }
14467
14468 /* At this point the declaration is well-formed. It may not be
14469 sensible though. */
14470
14471 /* Check member function warnings only on the in-class declaration.
14472 There's no point warning on an out-of-class definition. */
14473 if (class_type && class_type != current_class_type)
14474 return true;
14475
14476 /* Warn about conversion operators that will never be used. */
14477 if (IDENTIFIER_CONV_OP_P (name)
14478 && ! DECL_TEMPLATE_INFO (decl)
14479 && warn_class_conversion)
14480 {
14481 tree t = TREE_TYPE (name);
14482 int ref = TYPE_REF_P (t);
14483
14484 if (ref)
14485 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14486
14487 if (VOID_TYPE_P (t))
14488 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14489 "will never use a type conversion operator", class_type);
14490 else if (class_type)
14491 {
14492 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14493 warning_at (loc, OPT_Wclass_conversion,
14494 ref
14495 ? G_("converting %qT to a reference to the same type "
14496 "will never use a type conversion operator")
14497 : G_("converting %qT to the same type "
14498 "will never use a type conversion operator"),
14499 class_type);
14500 /* Don't force t to be complete here. */
14501 else if (MAYBE_CLASS_TYPE_P (t)
14502 && COMPLETE_TYPE_P (t)
14503 && DERIVED_FROM_P (t, class_type))
14504 warning_at (loc, OPT_Wclass_conversion,
14505 ref
14506 ? G_("converting %qT to a reference to a base class "
14507 "%qT will never use a type conversion operator")
14508 : G_("converting %qT to a base class %qT "
14509 "will never use a type conversion operator"),
14510 class_type, t);
14511 }
14512 }
14513
14514 if (!warn_ecpp)
14515 return true;
14516
14517 /* Effective C++ rules below. */
14518
14519 /* More Effective C++ rule 7. */
14520 if (operator_code == TRUTH_ANDIF_EXPR
14521 || operator_code == TRUTH_ORIF_EXPR
14522 || operator_code == COMPOUND_EXPR)
14523 warning_at (loc, OPT_Weffc__,
14524 "user-defined %qD always evaluates both arguments", decl);
14525
14526 /* More Effective C++ rule 6. */
14527 if (operator_code == POSTINCREMENT_EXPR
14528 || operator_code == POSTDECREMENT_EXPR
14529 || operator_code == PREINCREMENT_EXPR
14530 || operator_code == PREDECREMENT_EXPR)
14531 {
14532 tree arg = TREE_VALUE (argtypes);
14533 tree ret = TREE_TYPE (TREE_TYPE (decl));
14534 if (methodp || TYPE_REF_P (arg))
14535 arg = TREE_TYPE (arg);
14536 arg = TYPE_MAIN_VARIANT (arg);
14537
14538 if (operator_code == PREINCREMENT_EXPR
14539 || operator_code == PREDECREMENT_EXPR)
14540 {
14541 if (!TYPE_REF_P (ret)
14542 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14543 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14544 build_reference_type (arg));
14545 }
14546 else
14547 {
14548 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14549 warning_at (loc, OPT_Weffc__,
14550 "postfix %qD should return %qT", decl, arg);
14551 }
14552 }
14553
14554 /* Effective C++ rule 23. */
14555 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14556 && (operator_code == PLUS_EXPR
14557 || operator_code == MINUS_EXPR
14558 || operator_code == TRUNC_DIV_EXPR
14559 || operator_code == MULT_EXPR
14560 || operator_code == TRUNC_MOD_EXPR)
14561 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14562 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14563
14564 return true;
14565 }
14566 \f
14567 /* Return a string giving the keyword associate with CODE. */
14568
14569 static const char *
14570 tag_name (enum tag_types code)
14571 {
14572 switch (code)
14573 {
14574 case record_type:
14575 return "struct";
14576 case class_type:
14577 return "class";
14578 case union_type:
14579 return "union";
14580 case enum_type:
14581 return "enum";
14582 case typename_type:
14583 return "typename";
14584 default:
14585 gcc_unreachable ();
14586 }
14587 }
14588
14589 /* Name lookup in an elaborated-type-specifier (after the keyword
14590 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14591 elaborated-type-specifier is invalid, issue a diagnostic and return
14592 error_mark_node; otherwise, return the *_TYPE to which it referred.
14593 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14594
14595 tree
14596 check_elaborated_type_specifier (enum tag_types tag_code,
14597 tree decl,
14598 bool allow_template_p)
14599 {
14600 tree type;
14601
14602 /* In the case of:
14603
14604 struct S { struct S *p; };
14605
14606 name lookup will find the TYPE_DECL for the implicit "S::S"
14607 typedef. Adjust for that here. */
14608 if (DECL_SELF_REFERENCE_P (decl))
14609 decl = TYPE_NAME (TREE_TYPE (decl));
14610
14611 type = TREE_TYPE (decl);
14612
14613 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14614 is false for this case as well. */
14615 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14616 {
14617 error ("using template type parameter %qT after %qs",
14618 type, tag_name (tag_code));
14619 return error_mark_node;
14620 }
14621 /* Accept template template parameters. */
14622 else if (allow_template_p
14623 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14624 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14625 ;
14626 /* [dcl.type.elab]
14627
14628 If the identifier resolves to a typedef-name or the
14629 simple-template-id resolves to an alias template
14630 specialization, the elaborated-type-specifier is ill-formed.
14631
14632 In other words, the only legitimate declaration to use in the
14633 elaborated type specifier is the implicit typedef created when
14634 the type is declared. */
14635 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14636 && !DECL_SELF_REFERENCE_P (decl)
14637 && tag_code != typename_type)
14638 {
14639 if (alias_template_specialization_p (type, nt_opaque))
14640 error ("using alias template specialization %qT after %qs",
14641 type, tag_name (tag_code));
14642 else
14643 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14644 inform (DECL_SOURCE_LOCATION (decl),
14645 "%qD has a previous declaration here", decl);
14646 return error_mark_node;
14647 }
14648 else if (TREE_CODE (type) != RECORD_TYPE
14649 && TREE_CODE (type) != UNION_TYPE
14650 && tag_code != enum_type
14651 && tag_code != typename_type)
14652 {
14653 error ("%qT referred to as %qs", type, tag_name (tag_code));
14654 inform (location_of (type), "%qT has a previous declaration here", type);
14655 return error_mark_node;
14656 }
14657 else if (TREE_CODE (type) != ENUMERAL_TYPE
14658 && tag_code == enum_type)
14659 {
14660 error ("%qT referred to as enum", type);
14661 inform (location_of (type), "%qT has a previous declaration here", type);
14662 return error_mark_node;
14663 }
14664 else if (!allow_template_p
14665 && TREE_CODE (type) == RECORD_TYPE
14666 && CLASSTYPE_IS_TEMPLATE (type))
14667 {
14668 /* If a class template appears as elaborated type specifier
14669 without a template header such as:
14670
14671 template <class T> class C {};
14672 void f(class C); // No template header here
14673
14674 then the required template argument is missing. */
14675 error ("template argument required for %<%s %T%>",
14676 tag_name (tag_code),
14677 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14678 return error_mark_node;
14679 }
14680
14681 return type;
14682 }
14683
14684 /* Lookup NAME in elaborate type specifier in scope according to
14685 SCOPE and issue diagnostics if necessary.
14686 Return *_TYPE node upon success, NULL_TREE when the NAME is not
14687 found, and ERROR_MARK_NODE for type error. */
14688
14689 static tree
14690 lookup_and_check_tag (enum tag_types tag_code, tree name,
14691 tag_scope scope, bool template_header_p)
14692 {
14693 tree t;
14694 tree decl;
14695 if (scope == ts_global)
14696 {
14697 /* First try ordinary name lookup, ignoring hidden class name
14698 injected via friend declaration. */
14699 decl = lookup_name_prefer_type (name, 2);
14700 decl = strip_using_decl (decl);
14701 /* If that fails, the name will be placed in the smallest
14702 non-class, non-function-prototype scope according to 3.3.1/5.
14703 We may already have a hidden name declared as friend in this
14704 scope. So lookup again but not ignoring hidden names.
14705 If we find one, that name will be made visible rather than
14706 creating a new tag. */
14707 if (!decl)
14708 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
14709 }
14710 else
14711 decl = lookup_type_scope (name, scope);
14712
14713 if (decl
14714 && (DECL_CLASS_TEMPLATE_P (decl)
14715 /* If scope is ts_current we're defining a class, so ignore a
14716 template template parameter. */
14717 || (scope != ts_current
14718 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
14719 decl = DECL_TEMPLATE_RESULT (decl);
14720
14721 if (decl && TREE_CODE (decl) == TYPE_DECL)
14722 {
14723 /* Look for invalid nested type:
14724 class C {
14725 class C {};
14726 }; */
14727 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
14728 {
14729 error ("%qD has the same name as the class in which it is "
14730 "declared",
14731 decl);
14732 return error_mark_node;
14733 }
14734
14735 /* Two cases we need to consider when deciding if a class
14736 template is allowed as an elaborated type specifier:
14737 1. It is a self reference to its own class.
14738 2. It comes with a template header.
14739
14740 For example:
14741
14742 template <class T> class C {
14743 class C *c1; // DECL_SELF_REFERENCE_P is true
14744 class D;
14745 };
14746 template <class U> class C; // template_header_p is true
14747 template <class T> class C<T>::D {
14748 class C *c2; // DECL_SELF_REFERENCE_P is true
14749 }; */
14750
14751 t = check_elaborated_type_specifier (tag_code,
14752 decl,
14753 template_header_p
14754 | DECL_SELF_REFERENCE_P (decl));
14755 if (template_header_p && t && CLASS_TYPE_P (t)
14756 && (!CLASSTYPE_TEMPLATE_INFO (t)
14757 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
14758 {
14759 error ("%qT is not a template", t);
14760 inform (location_of (t), "previous declaration here");
14761 if (TYPE_CLASS_SCOPE_P (t)
14762 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
14763 inform (input_location,
14764 "perhaps you want to explicitly add %<%T::%>",
14765 TYPE_CONTEXT (t));
14766 t = error_mark_node;
14767 }
14768
14769 return t;
14770 }
14771 else if (decl && TREE_CODE (decl) == TREE_LIST)
14772 {
14773 error ("reference to %qD is ambiguous", name);
14774 print_candidates (decl);
14775 return error_mark_node;
14776 }
14777 else
14778 return NULL_TREE;
14779 }
14780
14781 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14782 Define the tag as a forward-reference if it is not defined.
14783
14784 If a declaration is given, process it here, and report an error if
14785 multiple declarations are not identical.
14786
14787 SCOPE is TS_CURRENT when this is also a definition. Only look in
14788 the current frame for the name (since C++ allows new names in any
14789 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14790 declaration. Only look beginning from the current scope outward up
14791 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14792
14793 TEMPLATE_HEADER_P is true when this declaration is preceded by
14794 a set of template parameters. */
14795
14796 static tree
14797 xref_tag_1 (enum tag_types tag_code, tree name,
14798 tag_scope scope, bool template_header_p)
14799 {
14800 enum tree_code code;
14801 tree context = NULL_TREE;
14802
14803 gcc_assert (identifier_p (name));
14804
14805 switch (tag_code)
14806 {
14807 case record_type:
14808 case class_type:
14809 code = RECORD_TYPE;
14810 break;
14811 case union_type:
14812 code = UNION_TYPE;
14813 break;
14814 case enum_type:
14815 code = ENUMERAL_TYPE;
14816 break;
14817 default:
14818 gcc_unreachable ();
14819 }
14820
14821 /* In case of anonymous name, xref_tag is only called to
14822 make type node and push name. Name lookup is not required. */
14823 tree t = NULL_TREE;
14824 if (scope != ts_lambda && !IDENTIFIER_ANON_P (name))
14825 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
14826
14827 if (t == error_mark_node)
14828 return error_mark_node;
14829
14830 if (scope != ts_current && t && current_class_type
14831 && template_class_depth (current_class_type)
14832 && template_header_p)
14833 {
14834 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
14835 return t;
14836
14837 /* Since SCOPE is not TS_CURRENT, we are not looking at a
14838 definition of this tag. Since, in addition, we are currently
14839 processing a (member) template declaration of a template
14840 class, we must be very careful; consider:
14841
14842 template <class X> struct S1
14843
14844 template <class U> struct S2
14845 {
14846 template <class V> friend struct S1;
14847 };
14848
14849 Here, the S2::S1 declaration should not be confused with the
14850 outer declaration. In particular, the inner version should
14851 have a template parameter of level 2, not level 1.
14852
14853 On the other hand, when presented with:
14854
14855 template <class T> struct S1
14856 {
14857 template <class U> struct S2 {};
14858 template <class U> friend struct S2;
14859 };
14860
14861 the friend must find S1::S2 eventually. We accomplish this
14862 by making sure that the new type we create to represent this
14863 declaration has the right TYPE_CONTEXT. */
14864 context = TYPE_CONTEXT (t);
14865 t = NULL_TREE;
14866 }
14867
14868 if (! t)
14869 {
14870 /* If no such tag is yet defined, create a forward-reference node
14871 and record it as the "definition".
14872 When a real declaration of this type is found,
14873 the forward-reference will be altered into a real type. */
14874 if (code == ENUMERAL_TYPE)
14875 {
14876 error ("use of enum %q#D without previous declaration", name);
14877 return error_mark_node;
14878 }
14879 else
14880 {
14881 t = make_class_type (code);
14882 TYPE_CONTEXT (t) = context;
14883 if (scope == ts_lambda)
14884 {
14885 /* Mark it as a lambda type. */
14886 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
14887 /* And push it into current scope. */
14888 scope = ts_current;
14889 }
14890 t = pushtag (name, t, scope);
14891 }
14892 }
14893 else
14894 {
14895 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
14896 {
14897 /* Check that we aren't trying to overload a class with different
14898 constraints. */
14899 tree constr = NULL_TREE;
14900 if (current_template_parms)
14901 {
14902 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
14903 constr = build_constraints (reqs, NULL_TREE);
14904 }
14905 if (!redeclare_class_template (t, current_template_parms, constr))
14906 return error_mark_node;
14907 }
14908 else if (!processing_template_decl
14909 && CLASS_TYPE_P (t)
14910 && CLASSTYPE_IS_TEMPLATE (t))
14911 {
14912 error ("redeclaration of %qT as a non-template", t);
14913 inform (location_of (t), "previous declaration %qD", t);
14914 return error_mark_node;
14915 }
14916
14917 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
14918 {
14919 /* This is no longer an invisible friend. Make it
14920 visible. */
14921 tree decl = TYPE_NAME (t);
14922
14923 DECL_ANTICIPATED (decl) = false;
14924 DECL_FRIEND_P (decl) = false;
14925
14926 if (TYPE_TEMPLATE_INFO (t))
14927 {
14928 tree tmpl = TYPE_TI_TEMPLATE (t);
14929 DECL_ANTICIPATED (tmpl) = false;
14930 DECL_FRIEND_P (tmpl) = false;
14931 }
14932 }
14933 }
14934
14935 return t;
14936 }
14937
14938 /* Wrapper for xref_tag_1. */
14939
14940 tree
14941 xref_tag (enum tag_types tag_code, tree name,
14942 tag_scope scope, bool template_header_p)
14943 {
14944 tree ret;
14945 bool subtime;
14946 subtime = timevar_cond_start (TV_NAME_LOOKUP);
14947 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
14948 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
14949 return ret;
14950 }
14951
14952
14953 tree
14954 xref_tag_from_type (tree old, tree id, tag_scope scope)
14955 {
14956 enum tag_types tag_kind;
14957
14958 if (TREE_CODE (old) == RECORD_TYPE)
14959 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
14960 else
14961 tag_kind = union_type;
14962
14963 if (id == NULL_TREE)
14964 id = TYPE_IDENTIFIER (old);
14965
14966 return xref_tag (tag_kind, id, scope, false);
14967 }
14968
14969 /* Create the binfo hierarchy for REF with (possibly NULL) base list
14970 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
14971 access_* node, and the TREE_VALUE is the type of the base-class.
14972 Non-NULL TREE_TYPE indicates virtual inheritance. */
14973
14974 void
14975 xref_basetypes (tree ref, tree base_list)
14976 {
14977 tree *basep;
14978 tree binfo, base_binfo;
14979 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
14980 unsigned max_bases = 0; /* Maximum direct bases. */
14981 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
14982 int i;
14983 tree default_access;
14984 tree igo_prev; /* Track Inheritance Graph Order. */
14985
14986 if (ref == error_mark_node)
14987 return;
14988
14989 /* The base of a derived class is private by default, all others are
14990 public. */
14991 default_access = (TREE_CODE (ref) == RECORD_TYPE
14992 && CLASSTYPE_DECLARED_CLASS (ref)
14993 ? access_private_node : access_public_node);
14994
14995 /* First, make sure that any templates in base-classes are
14996 instantiated. This ensures that if we call ourselves recursively
14997 we do not get confused about which classes are marked and which
14998 are not. */
14999 basep = &base_list;
15000 while (*basep)
15001 {
15002 tree basetype = TREE_VALUE (*basep);
15003
15004 /* The dependent_type_p call below should really be dependent_scope_p
15005 so that we give a hard error about using an incomplete type as a
15006 base, but we allow it with a pedwarn for backward
15007 compatibility. */
15008 if (processing_template_decl
15009 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15010 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15011 if (!dependent_type_p (basetype)
15012 && !complete_type_or_else (basetype, NULL))
15013 /* An incomplete type. Remove it from the list. */
15014 *basep = TREE_CHAIN (*basep);
15015 else
15016 {
15017 max_bases++;
15018 if (TREE_TYPE (*basep))
15019 max_dvbases++;
15020 if (CLASS_TYPE_P (basetype))
15021 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15022 basep = &TREE_CHAIN (*basep);
15023 }
15024 }
15025 max_vbases += max_dvbases;
15026
15027 TYPE_MARKED_P (ref) = 1;
15028
15029 /* The binfo slot should be empty, unless this is an (ill-formed)
15030 redefinition. */
15031 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15032
15033 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15034
15035 binfo = make_tree_binfo (max_bases);
15036
15037 TYPE_BINFO (ref) = binfo;
15038 BINFO_OFFSET (binfo) = size_zero_node;
15039 BINFO_TYPE (binfo) = ref;
15040
15041 /* Apply base-class info set up to the variants of this type. */
15042 fixup_type_variants (ref);
15043
15044 if (max_bases)
15045 {
15046 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15047 /* A C++98 POD cannot have base classes. */
15048 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15049
15050 if (TREE_CODE (ref) == UNION_TYPE)
15051 {
15052 error ("derived union %qT invalid", ref);
15053 return;
15054 }
15055 }
15056
15057 if (max_bases > 1)
15058 warning (OPT_Wmultiple_inheritance,
15059 "%qT defined with multiple direct bases", ref);
15060
15061 if (max_vbases)
15062 {
15063 /* An aggregate can't have virtual base classes. */
15064 CLASSTYPE_NON_AGGREGATE (ref) = true;
15065
15066 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15067
15068 if (max_dvbases)
15069 warning (OPT_Wvirtual_inheritance,
15070 "%qT defined with direct virtual base", ref);
15071 }
15072
15073 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15074 {
15075 tree access = TREE_PURPOSE (base_list);
15076 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15077 tree basetype = TREE_VALUE (base_list);
15078
15079 if (access == access_default_node)
15080 access = default_access;
15081
15082 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15083 aggregate can't have virtual, private, or protected base classes. */
15084 if (cxx_dialect < cxx17
15085 || access != access_public_node
15086 || via_virtual)
15087 CLASSTYPE_NON_AGGREGATE (ref) = true;
15088
15089 if (PACK_EXPANSION_P (basetype))
15090 basetype = PACK_EXPANSION_PATTERN (basetype);
15091 if (TREE_CODE (basetype) == TYPE_DECL)
15092 basetype = TREE_TYPE (basetype);
15093 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15094 {
15095 error ("base type %qT fails to be a struct or class type",
15096 basetype);
15097 goto dropped_base;
15098 }
15099
15100 base_binfo = NULL_TREE;
15101 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15102 {
15103 base_binfo = TYPE_BINFO (basetype);
15104 /* The original basetype could have been a typedef'd type. */
15105 basetype = BINFO_TYPE (base_binfo);
15106
15107 /* Inherit flags from the base. */
15108 TYPE_HAS_NEW_OPERATOR (ref)
15109 |= TYPE_HAS_NEW_OPERATOR (basetype);
15110 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15111 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15112 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15113 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15114 CLASSTYPE_DIAMOND_SHAPED_P (ref)
15115 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15116 CLASSTYPE_REPEATED_BASE_P (ref)
15117 |= CLASSTYPE_REPEATED_BASE_P (basetype);
15118 }
15119
15120 /* We must do this test after we've seen through a typedef
15121 type. */
15122 if (TYPE_MARKED_P (basetype))
15123 {
15124 if (basetype == ref)
15125 error ("recursive type %qT undefined", basetype);
15126 else
15127 error ("duplicate base type %qT invalid", basetype);
15128 goto dropped_base;
15129 }
15130
15131 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15132 /* Regenerate the pack expansion for the bases. */
15133 basetype = make_pack_expansion (basetype);
15134
15135 TYPE_MARKED_P (basetype) = 1;
15136
15137 base_binfo = copy_binfo (base_binfo, basetype, ref,
15138 &igo_prev, via_virtual);
15139 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15140 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15141
15142 BINFO_BASE_APPEND (binfo, base_binfo);
15143 BINFO_BASE_ACCESS_APPEND (binfo, access);
15144 continue;
15145
15146 dropped_base:
15147 /* Update max_vbases to reflect the reality that we are dropping
15148 this base: if it reaches zero we want to undo the vec_alloc
15149 above to avoid inconsistencies during error-recovery: eg, in
15150 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15151 and vtt null (c++/27952). */
15152 if (via_virtual)
15153 max_vbases--;
15154 if (CLASS_TYPE_P (basetype))
15155 max_vbases
15156 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15157 }
15158
15159 if (CLASSTYPE_VBASECLASSES (ref)
15160 && max_vbases == 0)
15161 vec_free (CLASSTYPE_VBASECLASSES (ref));
15162
15163 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15164 /* If we didn't get max_vbases vbases, we must have shared at
15165 least one of them, and are therefore diamond shaped. */
15166 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15167
15168 /* Unmark all the types. */
15169 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15170 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15171 TYPE_MARKED_P (ref) = 0;
15172
15173 /* Now see if we have a repeated base type. */
15174 if (!CLASSTYPE_REPEATED_BASE_P (ref))
15175 {
15176 for (base_binfo = binfo; base_binfo;
15177 base_binfo = TREE_CHAIN (base_binfo))
15178 {
15179 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15180 {
15181 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15182 break;
15183 }
15184 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15185 }
15186 for (base_binfo = binfo; base_binfo;
15187 base_binfo = TREE_CHAIN (base_binfo))
15188 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15189 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15190 else
15191 break;
15192 }
15193 }
15194
15195 \f
15196 /* Copies the enum-related properties from type SRC to type DST.
15197 Used with the underlying type of an enum and the enum itself. */
15198 static void
15199 copy_type_enum (tree dst, tree src)
15200 {
15201 tree t;
15202 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15203 {
15204 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15205 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15206 TYPE_SIZE (t) = TYPE_SIZE (src);
15207 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15208 SET_TYPE_MODE (dst, TYPE_MODE (src));
15209 TYPE_PRECISION (t) = TYPE_PRECISION (src);
15210 unsigned valign = TYPE_ALIGN (src);
15211 if (TYPE_USER_ALIGN (t))
15212 valign = MAX (valign, TYPE_ALIGN (t));
15213 else
15214 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15215 SET_TYPE_ALIGN (t, valign);
15216 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15217 }
15218 }
15219
15220 /* Begin compiling the definition of an enumeration type.
15221 NAME is its name,
15222
15223 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15224
15225 UNDERLYING_TYPE is the type that will be used as the storage for
15226 the enumeration type. This should be NULL_TREE if no storage type
15227 was specified.
15228
15229 ATTRIBUTES are any attributes specified after the enum-key.
15230
15231 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15232
15233 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15234
15235 Returns the type object, as yet incomplete.
15236 Also records info about it so that build_enumerator
15237 may be used to declare the individual values as they are read. */
15238
15239 tree
15240 start_enum (tree name, tree enumtype, tree underlying_type,
15241 tree attributes, bool scoped_enum_p, bool *is_new)
15242 {
15243 tree prevtype = NULL_TREE;
15244 gcc_assert (identifier_p (name));
15245
15246 if (is_new)
15247 *is_new = false;
15248 /* [C++0x dcl.enum]p5:
15249
15250 If not explicitly specified, the underlying type of a scoped
15251 enumeration type is int. */
15252 if (!underlying_type && scoped_enum_p)
15253 underlying_type = integer_type_node;
15254
15255 if (underlying_type)
15256 underlying_type = cv_unqualified (underlying_type);
15257
15258 /* If this is the real definition for a previous forward reference,
15259 fill in the contents in the same object that used to be the
15260 forward reference. */
15261 if (!enumtype)
15262 enumtype = lookup_and_check_tag (enum_type, name,
15263 /*tag_scope=*/ts_current,
15264 /*template_header_p=*/false);
15265
15266 /* In case of a template_decl, the only check that should be deferred
15267 to instantiation time is the comparison of underlying types. */
15268 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15269 {
15270 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15271 {
15272 error_at (input_location, "scoped/unscoped mismatch "
15273 "in enum %q#T", enumtype);
15274 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15275 "previous definition here");
15276 enumtype = error_mark_node;
15277 }
15278 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15279 {
15280 error_at (input_location, "underlying type mismatch "
15281 "in enum %q#T", enumtype);
15282 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15283 "previous definition here");
15284 enumtype = error_mark_node;
15285 }
15286 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15287 && !same_type_p (underlying_type,
15288 ENUM_UNDERLYING_TYPE (enumtype)))
15289 {
15290 error_at (input_location, "different underlying type "
15291 "in enum %q#T", enumtype);
15292 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15293 "previous definition here");
15294 underlying_type = NULL_TREE;
15295 }
15296 }
15297
15298 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
15299 || processing_template_decl)
15300 {
15301 /* In case of error, make a dummy enum to allow parsing to
15302 continue. */
15303 if (enumtype == error_mark_node)
15304 {
15305 name = make_anon_name ();
15306 enumtype = NULL_TREE;
15307 }
15308
15309 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15310 of an opaque enum, or an opaque enum of an already defined
15311 enumeration (C++11).
15312 In any other case, it'll be NULL_TREE. */
15313 if (!enumtype)
15314 {
15315 if (is_new)
15316 *is_new = true;
15317 }
15318 prevtype = enumtype;
15319
15320 /* Do not push the decl more than once. */
15321 if (!enumtype
15322 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
15323 {
15324 enumtype = cxx_make_type (ENUMERAL_TYPE);
15325 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
15326
15327 /* std::byte aliases anything. */
15328 if (enumtype != error_mark_node
15329 && TYPE_CONTEXT (enumtype) == std_node
15330 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
15331 TYPE_ALIAS_SET (enumtype) = 0;
15332 }
15333 else
15334 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
15335 false);
15336
15337 if (enumtype == error_mark_node)
15338 return error_mark_node;
15339
15340 /* The enum is considered opaque until the opening '{' of the
15341 enumerator list. */
15342 SET_OPAQUE_ENUM_P (enumtype, true);
15343 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
15344 }
15345
15346 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
15347
15348 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
15349
15350 if (underlying_type)
15351 {
15352 if (ENUM_UNDERLYING_TYPE (enumtype))
15353 /* We already checked that it matches, don't change it to a different
15354 typedef variant. */;
15355 else if (CP_INTEGRAL_TYPE_P (underlying_type))
15356 {
15357 copy_type_enum (enumtype, underlying_type);
15358 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15359 }
15360 else if (dependent_type_p (underlying_type))
15361 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15362 else
15363 error ("underlying type %qT of %qT must be an integral type",
15364 underlying_type, enumtype);
15365 }
15366
15367 /* If into a template class, the returned enum is always the first
15368 declaration (opaque or not) seen. This way all the references to
15369 this type will be to the same declaration. The following ones are used
15370 only to check for definition errors. */
15371 if (prevtype && processing_template_decl)
15372 return prevtype;
15373 else
15374 return enumtype;
15375 }
15376
15377 /* After processing and defining all the values of an enumeration type,
15378 install their decls in the enumeration type.
15379 ENUMTYPE is the type object. */
15380
15381 void
15382 finish_enum_value_list (tree enumtype)
15383 {
15384 tree values;
15385 tree underlying_type;
15386 tree decl;
15387 tree value;
15388 tree minnode, maxnode;
15389 tree t;
15390
15391 bool fixed_underlying_type_p
15392 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
15393
15394 /* We built up the VALUES in reverse order. */
15395 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
15396
15397 /* For an enum defined in a template, just set the type of the values;
15398 all further processing is postponed until the template is
15399 instantiated. We need to set the type so that tsubst of a CONST_DECL
15400 works. */
15401 if (processing_template_decl)
15402 {
15403 for (values = TYPE_VALUES (enumtype);
15404 values;
15405 values = TREE_CHAIN (values))
15406 TREE_TYPE (TREE_VALUE (values)) = enumtype;
15407 return;
15408 }
15409
15410 /* Determine the minimum and maximum values of the enumerators. */
15411 if (TYPE_VALUES (enumtype))
15412 {
15413 minnode = maxnode = NULL_TREE;
15414
15415 for (values = TYPE_VALUES (enumtype);
15416 values;
15417 values = TREE_CHAIN (values))
15418 {
15419 decl = TREE_VALUE (values);
15420
15421 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15422 each enumerator has the type of its enumeration. Prior to the
15423 closing brace, the type of each enumerator is the type of its
15424 initializing value. */
15425 TREE_TYPE (decl) = enumtype;
15426
15427 /* Update the minimum and maximum values, if appropriate. */
15428 value = DECL_INITIAL (decl);
15429 if (value == error_mark_node)
15430 value = integer_zero_node;
15431 /* Figure out what the minimum and maximum values of the
15432 enumerators are. */
15433 if (!minnode)
15434 minnode = maxnode = value;
15435 else if (tree_int_cst_lt (maxnode, value))
15436 maxnode = value;
15437 else if (tree_int_cst_lt (value, minnode))
15438 minnode = value;
15439 }
15440 }
15441 else
15442 /* [dcl.enum]
15443
15444 If the enumerator-list is empty, the underlying type is as if
15445 the enumeration had a single enumerator with value 0. */
15446 minnode = maxnode = integer_zero_node;
15447
15448 if (!fixed_underlying_type_p)
15449 {
15450 /* Compute the number of bits require to represent all values of the
15451 enumeration. We must do this before the type of MINNODE and
15452 MAXNODE are transformed, since tree_int_cst_min_precision relies
15453 on the TREE_TYPE of the value it is passed. */
15454 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15455 int lowprec = tree_int_cst_min_precision (minnode, sgn);
15456 int highprec = tree_int_cst_min_precision (maxnode, sgn);
15457 int precision = MAX (lowprec, highprec);
15458 unsigned int itk;
15459 bool use_short_enum;
15460
15461 /* Determine the underlying type of the enumeration.
15462
15463 [dcl.enum]
15464
15465 The underlying type of an enumeration is an integral type that
15466 can represent all the enumerator values defined in the
15467 enumeration. It is implementation-defined which integral type is
15468 used as the underlying type for an enumeration except that the
15469 underlying type shall not be larger than int unless the value of
15470 an enumerator cannot fit in an int or unsigned int.
15471
15472 We use "int" or an "unsigned int" as the underlying type, even if
15473 a smaller integral type would work, unless the user has
15474 explicitly requested that we use the smallest possible type. The
15475 user can request that for all enumerations with a command line
15476 flag, or for just one enumeration with an attribute. */
15477
15478 use_short_enum = flag_short_enums
15479 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15480
15481 /* If the precision of the type was specified with an attribute and it
15482 was too small, give an error. Otherwise, use it. */
15483 if (TYPE_PRECISION (enumtype))
15484 {
15485 if (precision > TYPE_PRECISION (enumtype))
15486 error ("specified mode too small for enumerated values");
15487 else
15488 {
15489 use_short_enum = true;
15490 precision = TYPE_PRECISION (enumtype);
15491 }
15492 }
15493
15494 for (itk = (use_short_enum ? itk_char : itk_int);
15495 itk != itk_none;
15496 itk++)
15497 {
15498 underlying_type = integer_types[itk];
15499 if (underlying_type != NULL_TREE
15500 && TYPE_PRECISION (underlying_type) >= precision
15501 && TYPE_SIGN (underlying_type) == sgn)
15502 break;
15503 }
15504 if (itk == itk_none)
15505 {
15506 /* DR 377
15507
15508 IF no integral type can represent all the enumerator values, the
15509 enumeration is ill-formed. */
15510 error ("no integral type can represent all of the enumerator values "
15511 "for %qT", enumtype);
15512 precision = TYPE_PRECISION (long_long_integer_type_node);
15513 underlying_type = integer_types[itk_unsigned_long_long];
15514 }
15515
15516 /* [dcl.enum]
15517
15518 The value of sizeof() applied to an enumeration type, an object
15519 of an enumeration type, or an enumerator, is the value of sizeof()
15520 applied to the underlying type. */
15521 copy_type_enum (enumtype, underlying_type);
15522
15523 /* Compute the minimum and maximum values for the type.
15524
15525 [dcl.enum]
15526
15527 For an enumeration where emin is the smallest enumerator and emax
15528 is the largest, the values of the enumeration are the values of the
15529 underlying type in the range bmin to bmax, where bmin and bmax are,
15530 respectively, the smallest and largest values of the smallest bit-
15531 field that can store emin and emax. */
15532
15533 /* The middle-end currently assumes that types with TYPE_PRECISION
15534 narrower than their underlying type are suitably zero or sign
15535 extended to fill their mode. Similarly, it assumes that the front
15536 end assures that a value of a particular type must be within
15537 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15538
15539 We used to set these fields based on bmin and bmax, but that led
15540 to invalid assumptions like optimizing away bounds checking. So
15541 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15542 TYPE_MAX_VALUE to the values for the mode above and only restrict
15543 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15544 ENUM_UNDERLYING_TYPE (enumtype)
15545 = build_distinct_type_copy (underlying_type);
15546 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15547 set_min_and_max_values_for_integral_type
15548 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15549
15550 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15551 if (flag_strict_enums)
15552 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15553 }
15554 else
15555 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15556
15557 /* Convert each of the enumerators to the type of the underlying
15558 type of the enumeration. */
15559 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15560 {
15561 decl = TREE_VALUE (values);
15562 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15563 if (fixed_underlying_type_p)
15564 /* If the enumeration type has a fixed underlying type, we
15565 already checked all of the enumerator values. */
15566 value = DECL_INITIAL (decl);
15567 else
15568 value = perform_implicit_conversion (underlying_type,
15569 DECL_INITIAL (decl),
15570 tf_warning_or_error);
15571 /* Do not clobber shared ints. */
15572 if (value != error_mark_node)
15573 {
15574 value = copy_node (value);
15575
15576 TREE_TYPE (value) = enumtype;
15577 }
15578 DECL_INITIAL (decl) = value;
15579 }
15580
15581 /* Fix up all variant types of this enum type. */
15582 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15583 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15584
15585 if (at_class_scope_p ()
15586 && COMPLETE_TYPE_P (current_class_type)
15587 && UNSCOPED_ENUM_P (enumtype))
15588 {
15589 insert_late_enum_def_bindings (current_class_type, enumtype);
15590 /* TYPE_FIELDS needs fixup. */
15591 fixup_type_variants (current_class_type);
15592 }
15593
15594 /* Finish debugging output for this type. */
15595 rest_of_type_compilation (enumtype, namespace_bindings_p ());
15596
15597 /* Each enumerator now has the type of its enumeration. Clear the cache
15598 so that this change in types doesn't confuse us later on. */
15599 clear_cv_and_fold_caches ();
15600 }
15601
15602 /* Finishes the enum type. This is called only the first time an
15603 enumeration is seen, be it opaque or odinary.
15604 ENUMTYPE is the type object. */
15605
15606 void
15607 finish_enum (tree enumtype)
15608 {
15609 if (processing_template_decl)
15610 {
15611 if (at_function_scope_p ())
15612 add_stmt (build_min (TAG_DEFN, enumtype));
15613 return;
15614 }
15615
15616 /* If this is a forward declaration, there should not be any variants,
15617 though we can get a variant in the middle of an enum-specifier with
15618 wacky code like 'enum E { e = sizeof(const E*) };' */
15619 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15620 && (TYPE_VALUES (enumtype)
15621 || !TYPE_NEXT_VARIANT (enumtype)));
15622 }
15623
15624 /* Build and install a CONST_DECL for an enumeration constant of the
15625 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15626 Apply ATTRIBUTES if available. LOC is the location of NAME.
15627 Assignment of sequential values by default is handled here. */
15628
15629 void
15630 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15631 location_t loc)
15632 {
15633 tree decl;
15634 tree context;
15635 tree type;
15636
15637 /* scalar_constant_value will pull out this expression, so make sure
15638 it's folded as appropriate. */
15639 if (processing_template_decl)
15640 value = fold_non_dependent_expr (value);
15641
15642 /* If the VALUE was erroneous, pretend it wasn't there; that will
15643 result in the enum being assigned the next value in sequence. */
15644 if (value == error_mark_node)
15645 value = NULL_TREE;
15646
15647 /* Remove no-op casts from the value. */
15648 if (value)
15649 STRIP_TYPE_NOPS (value);
15650
15651 if (! processing_template_decl)
15652 {
15653 /* Validate and default VALUE. */
15654 if (value != NULL_TREE)
15655 {
15656 if (!ENUM_UNDERLYING_TYPE (enumtype))
15657 {
15658 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15659 value, true);
15660 if (tmp_value)
15661 value = tmp_value;
15662 }
15663 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15664 (TREE_TYPE (value)))
15665 value = perform_implicit_conversion_flags
15666 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15667 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15668
15669 if (value == error_mark_node)
15670 value = NULL_TREE;
15671
15672 if (value != NULL_TREE)
15673 {
15674 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15675 (TREE_TYPE (value)))
15676 {
15677 error_at (cp_expr_loc_or_input_loc (value),
15678 "enumerator value for %qD must have integral or "
15679 "unscoped enumeration type", name);
15680 value = NULL_TREE;
15681 }
15682 else
15683 {
15684 value = cxx_constant_value (value);
15685
15686 if (TREE_CODE (value) != INTEGER_CST)
15687 {
15688 error ("enumerator value for %qD is not an integer "
15689 "constant", name);
15690 value = NULL_TREE;
15691 }
15692 }
15693 }
15694 }
15695
15696 /* Default based on previous value. */
15697 if (value == NULL_TREE)
15698 {
15699 if (TYPE_VALUES (enumtype))
15700 {
15701 tree prev_value;
15702
15703 /* C++03 7.2/4: If no initializer is specified for the first
15704 enumerator, the type is an unspecified integral
15705 type. Otherwise the type is the same as the type of the
15706 initializing value of the preceding enumerator unless the
15707 incremented value is not representable in that type, in
15708 which case the type is an unspecified integral type
15709 sufficient to contain the incremented value. */
15710 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
15711 if (error_operand_p (prev_value))
15712 value = error_mark_node;
15713 else
15714 {
15715 wi::overflow_type overflowed;
15716 tree type = TREE_TYPE (prev_value);
15717 signop sgn = TYPE_SIGN (type);
15718 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
15719 &overflowed);
15720 if (!overflowed)
15721 {
15722 bool pos = !wi::neg_p (wi, sgn);
15723 if (!wi::fits_to_tree_p (wi, type))
15724 {
15725 unsigned int itk;
15726 for (itk = itk_int; itk != itk_none; itk++)
15727 {
15728 type = integer_types[itk];
15729 if (type != NULL_TREE
15730 && (pos || !TYPE_UNSIGNED (type))
15731 && wi::fits_to_tree_p (wi, type))
15732 break;
15733 }
15734 if (type && cxx_dialect < cxx11
15735 && itk > itk_unsigned_long)
15736 pedwarn (input_location, OPT_Wlong_long,
15737 pos ? G_("\
15738 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15739 incremented enumerator value is too large for %<long%>"));
15740 }
15741 if (type == NULL_TREE)
15742 overflowed = wi::OVF_UNKNOWN;
15743 else
15744 value = wide_int_to_tree (type, wi);
15745 }
15746
15747 if (overflowed)
15748 {
15749 error ("overflow in enumeration values at %qD", name);
15750 value = error_mark_node;
15751 }
15752 }
15753 }
15754 else
15755 value = integer_zero_node;
15756 }
15757
15758 /* Remove no-op casts from the value. */
15759 STRIP_TYPE_NOPS (value);
15760
15761 /* If the underlying type of the enum is fixed, check whether
15762 the enumerator values fits in the underlying type. If it
15763 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15764 if (ENUM_UNDERLYING_TYPE (enumtype)
15765 && value
15766 && TREE_CODE (value) == INTEGER_CST)
15767 {
15768 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
15769 error ("enumerator value %qE is outside the range of underlying "
15770 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
15771
15772 /* Convert the value to the appropriate type. */
15773 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
15774 }
15775 }
15776
15777 /* C++ associates enums with global, function, or class declarations. */
15778 context = current_scope ();
15779
15780 /* Build the actual enumeration constant. Note that the enumeration
15781 constants have the underlying type of the enum (if it is fixed)
15782 or the type of their initializer (if the underlying type of the
15783 enum is not fixed):
15784
15785 [ C++0x dcl.enum ]
15786
15787 If the underlying type is fixed, the type of each enumerator
15788 prior to the closing brace is the underlying type; if the
15789 initializing value of an enumerator cannot be represented by
15790 the underlying type, the program is ill-formed. If the
15791 underlying type is not fixed, the type of each enumerator is
15792 the type of its initializing value.
15793
15794 If the underlying type is not fixed, it will be computed by
15795 finish_enum and we will reset the type of this enumerator. Of
15796 course, if we're processing a template, there may be no value. */
15797 type = value ? TREE_TYPE (value) : NULL_TREE;
15798
15799 decl = build_decl (loc, CONST_DECL, name, type);
15800
15801 DECL_CONTEXT (decl) = enumtype;
15802 TREE_CONSTANT (decl) = 1;
15803 TREE_READONLY (decl) = 1;
15804 DECL_INITIAL (decl) = value;
15805
15806 if (attributes)
15807 cplus_decl_attributes (&decl, attributes, 0);
15808
15809 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
15810 {
15811 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15812 on the TYPE_FIELDS list for `S'. (That's so that you can say
15813 things like `S::i' later.) */
15814
15815 /* The enumerator may be getting declared outside of its enclosing
15816 class, like so:
15817
15818 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15819
15820 For which case we need to make sure that the access of `S::i'
15821 matches the access of `S::E'. */
15822 tree saved_cas = current_access_specifier;
15823 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
15824 current_access_specifier = access_private_node;
15825 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
15826 current_access_specifier = access_protected_node;
15827 else
15828 current_access_specifier = access_public_node;
15829
15830 finish_member_declaration (decl);
15831
15832 current_access_specifier = saved_cas;
15833 }
15834 else
15835 pushdecl (decl);
15836
15837 /* Add this enumeration constant to the list for this type. */
15838 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
15839 }
15840
15841 /* Look for an enumerator with the given NAME within the enumeration
15842 type ENUMTYPE. This routine is used primarily for qualified name
15843 lookup into an enumerator in C++0x, e.g.,
15844
15845 enum class Color { Red, Green, Blue };
15846
15847 Color color = Color::Red;
15848
15849 Returns the value corresponding to the enumerator, or
15850 NULL_TREE if no such enumerator was found. */
15851 tree
15852 lookup_enumerator (tree enumtype, tree name)
15853 {
15854 tree e;
15855 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
15856
15857 e = purpose_member (name, TYPE_VALUES (enumtype));
15858 return e? TREE_VALUE (e) : NULL_TREE;
15859 }
15860
15861 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
15862
15863 tree
15864 cxx_simulate_enum_decl (location_t loc, const char *name,
15865 vec<string_int_pair> values)
15866 {
15867 location_t saved_loc = input_location;
15868 input_location = loc;
15869
15870 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
15871 NULL_TREE, false, NULL);
15872 if (!OPAQUE_ENUM_P (enumtype))
15873 {
15874 error_at (loc, "multiple definition of %q#T", enumtype);
15875 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15876 "previous definition here");
15877 return enumtype;
15878 }
15879 SET_OPAQUE_ENUM_P (enumtype, false);
15880 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
15881
15882 string_int_pair *value;
15883 unsigned int i;
15884 FOR_EACH_VEC_ELT (values, i, value)
15885 build_enumerator (get_identifier (value->first),
15886 build_int_cst (integer_type_node, value->second),
15887 enumtype, NULL_TREE, loc);
15888
15889 finish_enum_value_list (enumtype);
15890 finish_enum (enumtype);
15891
15892 input_location = saved_loc;
15893 return enumtype;
15894 }
15895 \f
15896 /* We're defining DECL. Make sure that its type is OK. */
15897
15898 static void
15899 check_function_type (tree decl, tree current_function_parms)
15900 {
15901 tree fntype = TREE_TYPE (decl);
15902 tree return_type = complete_type (TREE_TYPE (fntype));
15903
15904 /* In a function definition, arg types must be complete. */
15905 require_complete_types_for_parms (current_function_parms);
15906
15907 if (dependent_type_p (return_type)
15908 || type_uses_auto (return_type))
15909 return;
15910 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
15911 {
15912 tree args = TYPE_ARG_TYPES (fntype);
15913
15914 error ("return type %q#T is incomplete", return_type);
15915
15916 /* Make it return void instead. */
15917 if (TREE_CODE (fntype) == METHOD_TYPE)
15918 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
15919 void_type_node,
15920 TREE_CHAIN (args));
15921 else
15922 fntype = build_function_type (void_type_node, args);
15923 fntype = (cp_build_type_attribute_variant
15924 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
15925 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
15926 TREE_TYPE (decl) = fntype;
15927 }
15928 else
15929 {
15930 abstract_virtuals_error (decl, TREE_TYPE (fntype));
15931 maybe_warn_parm_abi (TREE_TYPE (fntype),
15932 DECL_SOURCE_LOCATION (decl));
15933 }
15934 }
15935
15936 /* True iff FN is an implicitly-defined default constructor. */
15937
15938 static bool
15939 implicit_default_ctor_p (tree fn)
15940 {
15941 return (DECL_CONSTRUCTOR_P (fn)
15942 && !user_provided_p (fn)
15943 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
15944 }
15945
15946 /* Clobber the contents of *this to let the back end know that the object
15947 storage is dead when we enter the constructor or leave the destructor. */
15948
15949 static tree
15950 build_clobber_this ()
15951 {
15952 /* Clobbering an empty base is pointless, and harmful if its one byte
15953 TYPE_SIZE overlays real data. */
15954 if (is_empty_class (current_class_type))
15955 return void_node;
15956
15957 /* If we have virtual bases, clobber the whole object, but only if we're in
15958 charge. If we don't have virtual bases, clobber the as-base type so we
15959 don't mess with tail padding. */
15960 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
15961
15962 tree ctype = current_class_type;
15963 if (!vbases)
15964 ctype = CLASSTYPE_AS_BASE (ctype);
15965
15966 tree clobber = build_clobber (ctype);
15967
15968 tree thisref = current_class_ref;
15969 if (ctype != current_class_type)
15970 {
15971 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
15972 thisref = convert_from_reference (thisref);
15973 }
15974
15975 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
15976 if (vbases)
15977 exprstmt = build_if_in_charge (exprstmt);
15978
15979 return exprstmt;
15980 }
15981
15982 /* Create the FUNCTION_DECL for a function definition.
15983 DECLSPECS and DECLARATOR are the parts of the declaration;
15984 they describe the function's name and the type it returns,
15985 but twisted together in a fashion that parallels the syntax of C.
15986
15987 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
15988 DECLARATOR is really the DECL for the function we are about to
15989 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
15990 indicating that the function is an inline defined in-class.
15991
15992 This function creates a binding context for the function body
15993 as well as setting up the FUNCTION_DECL in current_function_decl.
15994
15995 For C++, we must first check whether that datum makes any sense.
15996 For example, "class A local_a(1,2);" means that variable local_a
15997 is an aggregate of type A, which should have a constructor
15998 applied to it with the argument list [1, 2].
15999
16000 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16001 or may be a BLOCK if the function has been defined previously
16002 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16003 error_mark_node if the function has never been defined, or
16004 a BLOCK if the function has been defined somewhere. */
16005
16006 bool
16007 start_preparsed_function (tree decl1, tree attrs, int flags)
16008 {
16009 tree ctype = NULL_TREE;
16010 tree fntype;
16011 tree restype;
16012 int doing_friend = 0;
16013 cp_binding_level *bl;
16014 tree current_function_parms;
16015 struct c_fileinfo *finfo
16016 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16017 bool honor_interface;
16018
16019 /* Sanity check. */
16020 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16021 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16022
16023 fntype = TREE_TYPE (decl1);
16024 if (TREE_CODE (fntype) == METHOD_TYPE)
16025 ctype = TYPE_METHOD_BASETYPE (fntype);
16026
16027 /* ISO C++ 11.4/5. A friend function defined in a class is in
16028 the (lexical) scope of the class in which it is defined. */
16029 if (!ctype && DECL_FRIEND_P (decl1))
16030 {
16031 ctype = DECL_FRIEND_CONTEXT (decl1);
16032
16033 /* CTYPE could be null here if we're dealing with a template;
16034 for example, `inline friend float foo()' inside a template
16035 will have no CTYPE set. */
16036 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
16037 ctype = NULL_TREE;
16038 else
16039 doing_friend = 1;
16040 }
16041
16042 if (DECL_DECLARED_INLINE_P (decl1)
16043 && lookup_attribute ("noinline", attrs))
16044 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16045 "inline function %qD given attribute %qs", decl1, "noinline");
16046
16047 /* Handle gnu_inline attribute. */
16048 if (GNU_INLINE_P (decl1))
16049 {
16050 DECL_EXTERNAL (decl1) = 1;
16051 DECL_NOT_REALLY_EXTERN (decl1) = 0;
16052 DECL_INTERFACE_KNOWN (decl1) = 1;
16053 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16054 }
16055
16056 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16057 /* This is a constructor, we must ensure that any default args
16058 introduced by this definition are propagated to the clones
16059 now. The clones are used directly in overload resolution. */
16060 adjust_clone_args (decl1);
16061
16062 /* Sometimes we don't notice that a function is a static member, and
16063 build a METHOD_TYPE for it. Fix that up now. */
16064 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16065 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16066
16067 /* Set up current_class_type, and enter the scope of the class, if
16068 appropriate. */
16069 if (ctype)
16070 push_nested_class (ctype);
16071 else if (DECL_STATIC_FUNCTION_P (decl1))
16072 push_nested_class (DECL_CONTEXT (decl1));
16073
16074 /* Now that we have entered the scope of the class, we must restore
16075 the bindings for any template parameters surrounding DECL1, if it
16076 is an inline member template. (Order is important; consider the
16077 case where a template parameter has the same name as a field of
16078 the class.) It is not until after this point that
16079 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16080 if (flags & SF_INCLASS_INLINE)
16081 maybe_begin_member_template_processing (decl1);
16082
16083 /* Effective C++ rule 15. */
16084 if (warn_ecpp
16085 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16086 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16087 && VOID_TYPE_P (TREE_TYPE (fntype)))
16088 warning (OPT_Weffc__,
16089 "%<operator=%> should return a reference to %<*this%>");
16090
16091 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16092 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16093 if (!DECL_INITIAL (decl1))
16094 DECL_INITIAL (decl1) = error_mark_node;
16095
16096 /* This function exists in static storage.
16097 (This does not mean `static' in the C sense!) */
16098 TREE_STATIC (decl1) = 1;
16099
16100 /* We must call push_template_decl after current_class_type is set
16101 up. (If we are processing inline definitions after exiting a
16102 class scope, current_class_type will be NULL_TREE until set above
16103 by push_nested_class.) */
16104 if (processing_template_decl)
16105 {
16106 tree newdecl1 = push_template_decl (decl1);
16107 if (newdecl1 == error_mark_node)
16108 {
16109 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16110 pop_nested_class ();
16111 return false;
16112 }
16113 decl1 = newdecl1;
16114 }
16115
16116 /* Make sure the parameter and return types are reasonable. When
16117 you declare a function, these types can be incomplete, but they
16118 must be complete when you define the function. */
16119 check_function_type (decl1, DECL_ARGUMENTS (decl1));
16120
16121 /* Build the return declaration for the function. */
16122 restype = TREE_TYPE (fntype);
16123
16124 if (DECL_RESULT (decl1) == NULL_TREE)
16125 {
16126 tree resdecl;
16127
16128 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16129 DECL_ARTIFICIAL (resdecl) = 1;
16130 DECL_IGNORED_P (resdecl) = 1;
16131 DECL_RESULT (decl1) = resdecl;
16132
16133 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16134 }
16135
16136 /* Record the decl so that the function name is defined.
16137 If we already have a decl for this name, and it is a FUNCTION_DECL,
16138 use the old decl. */
16139 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16140 {
16141 /* A specialization is not used to guide overload resolution. */
16142 if (!DECL_FUNCTION_MEMBER_P (decl1)
16143 && !(DECL_USE_TEMPLATE (decl1) &&
16144 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16145 {
16146 tree olddecl = pushdecl (decl1);
16147
16148 if (olddecl == error_mark_node)
16149 /* If something went wrong when registering the declaration,
16150 use DECL1; we have to have a FUNCTION_DECL to use when
16151 parsing the body of the function. */
16152 ;
16153 else
16154 {
16155 /* Otherwise, OLDDECL is either a previous declaration
16156 of the same function or DECL1 itself. */
16157
16158 if (warn_missing_declarations
16159 && olddecl == decl1
16160 && !DECL_MAIN_P (decl1)
16161 && TREE_PUBLIC (decl1)
16162 && !DECL_DECLARED_INLINE_P (decl1))
16163 {
16164 tree context;
16165
16166 /* Check whether DECL1 is in an anonymous
16167 namespace. */
16168 for (context = DECL_CONTEXT (decl1);
16169 context;
16170 context = DECL_CONTEXT (context))
16171 {
16172 if (TREE_CODE (context) == NAMESPACE_DECL
16173 && DECL_NAME (context) == NULL_TREE)
16174 break;
16175 }
16176
16177 if (context == NULL)
16178 warning_at (DECL_SOURCE_LOCATION (decl1),
16179 OPT_Wmissing_declarations,
16180 "no previous declaration for %qD", decl1);
16181 }
16182
16183 decl1 = olddecl;
16184 }
16185 }
16186 else
16187 {
16188 /* We need to set the DECL_CONTEXT. */
16189 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16190 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16191 }
16192 fntype = TREE_TYPE (decl1);
16193 restype = TREE_TYPE (fntype);
16194
16195 /* If #pragma weak applies, mark the decl appropriately now.
16196 The pragma only applies to global functions. Because
16197 determining whether or not the #pragma applies involves
16198 computing the mangled name for the declaration, we cannot
16199 apply the pragma until after we have merged this declaration
16200 with any previous declarations; if the original declaration
16201 has a linkage specification, that specification applies to
16202 the definition as well, and may affect the mangled name. */
16203 if (DECL_FILE_SCOPE_P (decl1))
16204 maybe_apply_pragma_weak (decl1);
16205 }
16206
16207 /* We are now in the scope of the function being defined. */
16208 current_function_decl = decl1;
16209
16210 /* Save the parm names or decls from this function's declarator
16211 where store_parm_decls will find them. */
16212 current_function_parms = DECL_ARGUMENTS (decl1);
16213
16214 /* Let the user know we're compiling this function. */
16215 announce_function (decl1);
16216
16217 gcc_assert (DECL_INITIAL (decl1));
16218
16219 /* This function may already have been parsed, in which case just
16220 return; our caller will skip over the body without parsing. */
16221 if (DECL_INITIAL (decl1) != error_mark_node)
16222 return true;
16223
16224 /* Initialize RTL machinery. We cannot do this until
16225 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16226 even when processing a template; this is how we get
16227 CFUN set up, and our per-function variables initialized.
16228 FIXME factor out the non-RTL stuff. */
16229 bl = current_binding_level;
16230 allocate_struct_function (decl1, processing_template_decl);
16231
16232 /* Initialize the language data structures. Whenever we start
16233 a new function, we destroy temporaries in the usual way. */
16234 cfun->language = ggc_cleared_alloc<language_function> ();
16235 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16236 current_binding_level = bl;
16237
16238 /* If we are (erroneously) defining a function that we have already
16239 defined before, wipe out what we knew before. */
16240 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16241 FNDECL_USED_AUTO (decl1) = false;
16242 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16243
16244 if (!processing_template_decl && type_uses_auto (restype))
16245 {
16246 FNDECL_USED_AUTO (decl1) = true;
16247 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16248 }
16249
16250 /* Start the statement-tree, start the tree now. */
16251 DECL_SAVED_TREE (decl1) = push_stmt_list ();
16252
16253 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16254 {
16255 /* We know that this was set up by `grokclassfn'. We do not
16256 wait until `store_parm_decls', since evil parse errors may
16257 never get us to that point. Here we keep the consistency
16258 between `current_class_type' and `current_class_ptr'. */
16259 tree t = DECL_ARGUMENTS (decl1);
16260
16261 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16262 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16263
16264 cp_function_chain->x_current_class_ref
16265 = cp_build_fold_indirect_ref (t);
16266 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16267 cp_function_chain->x_current_class_ptr = t;
16268
16269 /* Constructors and destructors need to know whether they're "in
16270 charge" of initializing virtual base classes. */
16271 t = DECL_CHAIN (t);
16272 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16273 {
16274 current_in_charge_parm = t;
16275 t = DECL_CHAIN (t);
16276 }
16277 if (DECL_HAS_VTT_PARM_P (decl1))
16278 {
16279 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16280 current_vtt_parm = t;
16281 }
16282 }
16283
16284 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16285 /* Implicitly-defined methods (like the
16286 destructor for a class in which no destructor
16287 is explicitly declared) must not be defined
16288 until their definition is needed. So, we
16289 ignore interface specifications for
16290 compiler-generated functions. */
16291 && !DECL_ARTIFICIAL (decl1));
16292
16293 if (processing_template_decl)
16294 /* Don't mess with interface flags. */;
16295 else if (DECL_INTERFACE_KNOWN (decl1))
16296 {
16297 tree ctx = decl_function_context (decl1);
16298
16299 if (DECL_NOT_REALLY_EXTERN (decl1))
16300 DECL_EXTERNAL (decl1) = 0;
16301
16302 if (ctx != NULL_TREE && vague_linkage_p (ctx))
16303 /* This is a function in a local class in an extern inline
16304 or template function. */
16305 comdat_linkage (decl1);
16306 }
16307 /* If this function belongs to an interface, it is public.
16308 If it belongs to someone else's interface, it is also external.
16309 This only affects inlines and template instantiations. */
16310 else if (!finfo->interface_unknown && honor_interface)
16311 {
16312 if (DECL_DECLARED_INLINE_P (decl1)
16313 || DECL_TEMPLATE_INSTANTIATION (decl1))
16314 {
16315 DECL_EXTERNAL (decl1)
16316 = (finfo->interface_only
16317 || (DECL_DECLARED_INLINE_P (decl1)
16318 && ! flag_implement_inlines
16319 && !DECL_VINDEX (decl1)));
16320
16321 /* For WIN32 we also want to put these in linkonce sections. */
16322 maybe_make_one_only (decl1);
16323 }
16324 else
16325 DECL_EXTERNAL (decl1) = 0;
16326 DECL_INTERFACE_KNOWN (decl1) = 1;
16327 /* If this function is in an interface implemented in this file,
16328 make sure that the back end knows to emit this function
16329 here. */
16330 if (!DECL_EXTERNAL (decl1))
16331 mark_needed (decl1);
16332 }
16333 else if (finfo->interface_unknown && finfo->interface_only
16334 && honor_interface)
16335 {
16336 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16337 interface, we will have both finfo->interface_unknown and
16338 finfo->interface_only set. In that case, we don't want to
16339 use the normal heuristics because someone will supply a
16340 #pragma implementation elsewhere, and deducing it here would
16341 produce a conflict. */
16342 comdat_linkage (decl1);
16343 DECL_EXTERNAL (decl1) = 0;
16344 DECL_INTERFACE_KNOWN (decl1) = 1;
16345 DECL_DEFER_OUTPUT (decl1) = 1;
16346 }
16347 else
16348 {
16349 /* This is a definition, not a reference.
16350 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16351 if (!GNU_INLINE_P (decl1))
16352 DECL_EXTERNAL (decl1) = 0;
16353
16354 if ((DECL_DECLARED_INLINE_P (decl1)
16355 || DECL_TEMPLATE_INSTANTIATION (decl1))
16356 && ! DECL_INTERFACE_KNOWN (decl1))
16357 DECL_DEFER_OUTPUT (decl1) = 1;
16358 else
16359 DECL_INTERFACE_KNOWN (decl1) = 1;
16360 }
16361
16362 /* Determine the ELF visibility attribute for the function. We must not
16363 do this before calling "pushdecl", as we must allow "duplicate_decls"
16364 to merge any attributes appropriately. We also need to wait until
16365 linkage is set. */
16366 if (!DECL_CLONED_FUNCTION_P (decl1))
16367 determine_visibility (decl1);
16368
16369 if (!processing_template_decl)
16370 maybe_instantiate_noexcept (decl1);
16371
16372 begin_scope (sk_function_parms, decl1);
16373
16374 ++function_depth;
16375
16376 if (DECL_DESTRUCTOR_P (decl1)
16377 || (DECL_CONSTRUCTOR_P (decl1)
16378 && targetm.cxx.cdtor_returns_this ()))
16379 {
16380 cdtor_label = create_artificial_label (input_location);
16381 LABEL_DECL_CDTOR (cdtor_label) = true;
16382 }
16383
16384 start_fname_decls ();
16385
16386 store_parm_decls (current_function_parms);
16387
16388 push_operator_bindings ();
16389
16390 if (!processing_template_decl
16391 && (flag_lifetime_dse > 1)
16392 && DECL_CONSTRUCTOR_P (decl1)
16393 && !DECL_CLONED_FUNCTION_P (decl1)
16394 /* Clobbering an empty base is harmful if it overlays real data. */
16395 && !is_empty_class (current_class_type)
16396 /* We can't clobber safely for an implicitly-defined default constructor
16397 because part of the initialization might happen before we enter the
16398 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16399 && !implicit_default_ctor_p (decl1))
16400 finish_expr_stmt (build_clobber_this ());
16401
16402 if (!processing_template_decl
16403 && DECL_CONSTRUCTOR_P (decl1)
16404 && sanitize_flags_p (SANITIZE_VPTR)
16405 && !DECL_CLONED_FUNCTION_P (decl1)
16406 && !implicit_default_ctor_p (decl1))
16407 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
16408
16409 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
16410 start_lambda_scope (decl1);
16411
16412 return true;
16413 }
16414
16415
16416 /* Like start_preparsed_function, except that instead of a
16417 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16418
16419 Returns true on success. If the DECLARATOR is not suitable
16420 for a function, we return false, which tells the parser to
16421 skip the entire function. */
16422
16423 bool
16424 start_function (cp_decl_specifier_seq *declspecs,
16425 const cp_declarator *declarator,
16426 tree attrs)
16427 {
16428 tree decl1;
16429
16430 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
16431 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
16432 if (decl1 == error_mark_node)
16433 return false;
16434
16435 if (DECL_MAIN_P (decl1))
16436 /* main must return int. grokfndecl should have corrected it
16437 (and issued a diagnostic) if the user got it wrong. */
16438 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
16439 integer_type_node));
16440
16441 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16442 }
16443 \f
16444 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16445 FN. */
16446
16447 static bool
16448 use_eh_spec_block (tree fn)
16449 {
16450 return (flag_exceptions && flag_enforce_eh_specs
16451 && !processing_template_decl
16452 && !type_throw_all_p (TREE_TYPE (fn))
16453 /* We insert the EH_SPEC_BLOCK only in the original
16454 function; then, it is copied automatically to the
16455 clones. */
16456 && !DECL_CLONED_FUNCTION_P (fn)
16457 /* Implicitly-generated constructors and destructors have
16458 exception specifications. However, those specifications
16459 are the union of the possible exceptions specified by the
16460 constructors/destructors for bases and members, so no
16461 unallowed exception will ever reach this function. By
16462 not creating the EH_SPEC_BLOCK we save a little memory,
16463 and we avoid spurious warnings about unreachable
16464 code. */
16465 && !DECL_DEFAULTED_FN (fn));
16466 }
16467
16468 /* Helper function to push ARGS into the current lexical scope. DECL
16469 is the function declaration. NONPARMS is used to handle enum
16470 constants. */
16471
16472 void
16473 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16474 {
16475 /* If we're doing semantic analysis, then we'll call pushdecl
16476 for each of these. We must do them in reverse order so that
16477 they end in the correct forward order. */
16478 args = nreverse (args);
16479
16480 tree next;
16481 for (tree parm = args; parm; parm = next)
16482 {
16483 next = DECL_CHAIN (parm);
16484 if (TREE_CODE (parm) == PARM_DECL)
16485 pushdecl (parm);
16486 else if (nonparms)
16487 {
16488 /* If we find an enum constant or a type tag, put it aside for
16489 the moment. */
16490 TREE_CHAIN (parm) = NULL_TREE;
16491 *nonparms = chainon (*nonparms, parm);
16492 }
16493 }
16494
16495 /* Get the decls in their original chain order and record in the
16496 function. This is all and only the PARM_DECLs that were
16497 pushed into scope by the loop above. */
16498 DECL_ARGUMENTS (decl) = get_local_decls ();
16499 }
16500
16501 /* Store the parameter declarations into the current function declaration.
16502 This is called after parsing the parameter declarations, before
16503 digesting the body of the function.
16504
16505 Also install to binding contour return value identifier, if any. */
16506
16507 static void
16508 store_parm_decls (tree current_function_parms)
16509 {
16510 tree fndecl = current_function_decl;
16511
16512 /* This is a chain of any other decls that came in among the parm
16513 declarations. If a parm is declared with enum {foo, bar} x;
16514 then CONST_DECLs for foo and bar are put here. */
16515 tree nonparms = NULL_TREE;
16516
16517 if (current_function_parms)
16518 {
16519 /* This case is when the function was defined with an ANSI prototype.
16520 The parms already have decls, so we need not do anything here
16521 except record them as in effect
16522 and complain if any redundant old-style parm decls were written. */
16523
16524 tree specparms = current_function_parms;
16525
16526 /* Must clear this because it might contain TYPE_DECLs declared
16527 at class level. */
16528 current_binding_level->names = NULL;
16529
16530 do_push_parm_decls (fndecl, specparms, &nonparms);
16531 }
16532 else
16533 DECL_ARGUMENTS (fndecl) = NULL_TREE;
16534
16535 /* Now store the final chain of decls for the arguments
16536 as the decl-chain of the current lexical scope.
16537 Put the enumerators in as well, at the front so that
16538 DECL_ARGUMENTS is not modified. */
16539 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16540
16541 if (use_eh_spec_block (current_function_decl))
16542 current_eh_spec_block = begin_eh_spec_block ();
16543 }
16544
16545 \f
16546 /* Set the return value of the constructor (if present). */
16547
16548 static void
16549 finish_constructor_body (void)
16550 {
16551 tree val;
16552 tree exprstmt;
16553
16554 if (targetm.cxx.cdtor_returns_this ())
16555 {
16556 /* Any return from a constructor will end up here. */
16557 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16558
16559 val = DECL_ARGUMENTS (current_function_decl);
16560 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16561 DECL_RESULT (current_function_decl), val);
16562 /* Return the address of the object. */
16563 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16564 add_stmt (exprstmt);
16565 }
16566 }
16567
16568 /* Do all the processing for the beginning of a destructor; set up the
16569 vtable pointers and cleanups for bases and members. */
16570
16571 static void
16572 begin_destructor_body (void)
16573 {
16574 tree compound_stmt;
16575
16576 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16577 issued an error message. We still want to try to process the
16578 body of the function, but initialize_vtbl_ptrs will crash if
16579 TYPE_BINFO is NULL. */
16580 if (COMPLETE_TYPE_P (current_class_type))
16581 {
16582 compound_stmt = begin_compound_stmt (0);
16583 /* Make all virtual function table pointers in non-virtual base
16584 classes point to CURRENT_CLASS_TYPE's virtual function
16585 tables. */
16586 initialize_vtbl_ptrs (current_class_ptr);
16587 finish_compound_stmt (compound_stmt);
16588
16589 if (flag_lifetime_dse
16590 /* Clobbering an empty base is harmful if it overlays real data. */
16591 && !is_empty_class (current_class_type))
16592 {
16593 if (sanitize_flags_p (SANITIZE_VPTR)
16594 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16595 && TYPE_CONTAINS_VPTR_P (current_class_type))
16596 {
16597 tree binfo = TYPE_BINFO (current_class_type);
16598 tree ref
16599 = cp_build_fold_indirect_ref (current_class_ptr);
16600
16601 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16602 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16603 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16604 NOP_EXPR, vtbl,
16605 tf_warning_or_error);
16606 /* If the vptr is shared with some virtual nearly empty base,
16607 don't clear it if not in charge, the dtor of the virtual
16608 nearly empty base will do that later. */
16609 if (CLASSTYPE_VBASECLASSES (current_class_type)
16610 && CLASSTYPE_PRIMARY_BINFO (current_class_type)
16611 && BINFO_VIRTUAL_P
16612 (CLASSTYPE_PRIMARY_BINFO (current_class_type)))
16613 {
16614 stmt = convert_to_void (stmt, ICV_STATEMENT,
16615 tf_warning_or_error);
16616 stmt = build_if_in_charge (stmt);
16617 }
16618 finish_decl_cleanup (NULL_TREE, stmt);
16619 }
16620 else
16621 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16622 }
16623
16624 /* And insert cleanups for our bases and members so that they
16625 will be properly destroyed if we throw. */
16626 push_base_cleanups ();
16627 }
16628 }
16629
16630 /* At the end of every destructor we generate code to delete the object if
16631 necessary. Do that now. */
16632
16633 static void
16634 finish_destructor_body (void)
16635 {
16636 tree exprstmt;
16637
16638 /* Any return from a destructor will end up here; that way all base
16639 and member cleanups will be run when the function returns. */
16640 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16641
16642 if (targetm.cxx.cdtor_returns_this ())
16643 {
16644 tree val;
16645
16646 val = DECL_ARGUMENTS (current_function_decl);
16647 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16648 DECL_RESULT (current_function_decl), val);
16649 /* Return the address of the object. */
16650 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16651 add_stmt (exprstmt);
16652 }
16653 }
16654
16655 /* Do the necessary processing for the beginning of a function body, which
16656 in this case includes member-initializers, but not the catch clauses of
16657 a function-try-block. Currently, this means opening a binding level
16658 for the member-initializers (in a ctor), member cleanups (in a dtor),
16659 and capture proxies (in a lambda operator()). */
16660
16661 tree
16662 begin_function_body (void)
16663 {
16664 tree stmt;
16665
16666 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16667 return NULL_TREE;
16668
16669 if (processing_template_decl)
16670 /* Do nothing now. */;
16671 else
16672 /* Always keep the BLOCK node associated with the outermost pair of
16673 curly braces of a function. These are needed for correct
16674 operation of dwarfout.c. */
16675 keep_next_level (true);
16676
16677 stmt = begin_compound_stmt (BCS_FN_BODY);
16678
16679 if (processing_template_decl)
16680 /* Do nothing now. */;
16681 else if (DECL_DESTRUCTOR_P (current_function_decl))
16682 begin_destructor_body ();
16683
16684 return stmt;
16685 }
16686
16687 /* Do the processing for the end of a function body. Currently, this means
16688 closing out the cleanups for fully-constructed bases and members, and in
16689 the case of the destructor, deleting the object if desired. Again, this
16690 is only meaningful for [cd]tors, since they are the only functions where
16691 there is a significant distinction between the main body and any
16692 function catch clauses. Handling, say, main() return semantics here
16693 would be wrong, as flowing off the end of a function catch clause for
16694 main() would also need to return 0. */
16695
16696 void
16697 finish_function_body (tree compstmt)
16698 {
16699 if (compstmt == NULL_TREE)
16700 return;
16701
16702 /* Close the block. */
16703 finish_compound_stmt (compstmt);
16704
16705 if (processing_template_decl)
16706 /* Do nothing now. */;
16707 else if (DECL_CONSTRUCTOR_P (current_function_decl))
16708 finish_constructor_body ();
16709 else if (DECL_DESTRUCTOR_P (current_function_decl))
16710 finish_destructor_body ();
16711 }
16712
16713 /* Given a function, returns the BLOCK corresponding to the outermost level
16714 of curly braces, skipping the artificial block created for constructor
16715 initializers. */
16716
16717 tree
16718 outer_curly_brace_block (tree fndecl)
16719 {
16720 tree block = DECL_INITIAL (fndecl);
16721 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16722 return block;
16723 block = BLOCK_SUBBLOCKS (block);
16724 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16725 return block;
16726 block = BLOCK_SUBBLOCKS (block);
16727 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
16728 return block;
16729 }
16730
16731 /* If FNDECL is a class's key method, add the class to the list of
16732 keyed classes that should be emitted. */
16733
16734 static void
16735 record_key_method_defined (tree fndecl)
16736 {
16737 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
16738 && DECL_VIRTUAL_P (fndecl)
16739 && !processing_template_decl)
16740 {
16741 tree fnclass = DECL_CONTEXT (fndecl);
16742 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
16743 vec_safe_push (keyed_classes, fnclass);
16744 }
16745 }
16746
16747 /* Subroutine of finish_function.
16748 Save the body of constexpr functions for possible
16749 future compile time evaluation. */
16750
16751 static void
16752 maybe_save_function_definition (tree fun)
16753 {
16754 if (!processing_template_decl
16755 && DECL_DECLARED_CONSTEXPR_P (fun)
16756 && !cp_function_chain->invalid_constexpr
16757 && !DECL_CLONED_FUNCTION_P (fun))
16758 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
16759 }
16760
16761 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16762 of "return *this;" immediately before its location, using FNDECL's
16763 first statement (if any) to give the indentation, if appropriate. */
16764
16765 static void
16766 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
16767 {
16768 location_t indent = UNKNOWN_LOCATION;
16769 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
16770 if (stmts)
16771 indent = EXPR_LOCATION (stmts);
16772 richloc->add_fixit_insert_formatted ("return *this;",
16773 richloc->get_loc (),
16774 indent);
16775 }
16776
16777 /* This function carries out the subset of finish_function operations needed
16778 to emit the compiler-generated outlined helper functions used by the
16779 coroutines implementation. */
16780
16781 static void
16782 emit_coro_helper (tree helper)
16783 {
16784 /* This is a partial set of the operations done by finish_function()
16785 plus emitting the result. */
16786 set_cfun (NULL);
16787 current_function_decl = helper;
16788 begin_scope (sk_function_parms, NULL);
16789 store_parm_decls (DECL_ARGUMENTS (helper));
16790 announce_function (helper);
16791 allocate_struct_function (helper, false);
16792 cfun->language = ggc_cleared_alloc<language_function> ();
16793 poplevel (1, 0, 1);
16794 maybe_save_function_definition (helper);
16795 /* We must start each function with a clear fold cache. */
16796 clear_fold_cache ();
16797 cp_fold_function (helper);
16798 DECL_CONTEXT (DECL_RESULT (helper)) = helper;
16799 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper;
16800 /* This function has coroutine IFNs that we should handle in middle
16801 end lowering. */
16802 cfun->coroutine_component = true;
16803 cp_genericize (helper);
16804 expand_or_defer_fn (helper);
16805 }
16806
16807 /* Finish up a function declaration and compile that function
16808 all the way to assembler language output. The free the storage
16809 for the function definition. INLINE_P is TRUE if we just
16810 finished processing the body of an in-class inline function
16811 definition. (This processing will have taken place after the
16812 class definition is complete.) */
16813
16814 tree
16815 finish_function (bool inline_p)
16816 {
16817 tree fndecl = current_function_decl;
16818 tree fntype, ctype = NULL_TREE;
16819 tree resumer = NULL_TREE, destroyer = NULL_TREE;
16820 bool coro_p = flag_coroutines
16821 && !processing_template_decl
16822 && DECL_COROUTINE_P (fndecl);
16823
16824 /* When we get some parse errors, we can end up without a
16825 current_function_decl, so cope. */
16826 if (fndecl == NULL_TREE)
16827 return error_mark_node;
16828
16829 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
16830 finish_lambda_scope ();
16831
16832 if (c_dialect_objc ())
16833 objc_finish_function ();
16834
16835 record_key_method_defined (fndecl);
16836
16837 fntype = TREE_TYPE (fndecl);
16838
16839 /* TREE_READONLY (fndecl) = 1;
16840 This caused &foo to be of type ptr-to-const-function
16841 which then got a warning when stored in a ptr-to-function variable. */
16842
16843 gcc_assert (building_stmt_list_p ());
16844 /* The current function is being defined, so its DECL_INITIAL should
16845 be set, and unless there's a multiple definition, it should be
16846 error_mark_node. */
16847 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
16848
16849 if (coro_p)
16850 {
16851 if (!morph_fn_to_coro (fndecl, &resumer, &destroyer))
16852 {
16853 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
16854 poplevel (1, 0, 1);
16855 DECL_SAVED_TREE (fndecl) = error_mark_node;
16856 return fndecl;
16857 }
16858
16859 /* We should handle coroutine IFNs in middle end lowering. */
16860 cfun->coroutine_component = true;
16861
16862 if (use_eh_spec_block (fndecl))
16863 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16864 (TREE_TYPE (fndecl)),
16865 current_eh_spec_block);
16866 }
16867 else
16868 /* For a cloned function, we've already got all the code we need;
16869 there's no need to add any extra bits. */
16870 if (!DECL_CLONED_FUNCTION_P (fndecl))
16871 {
16872 /* Make it so that `main' always returns 0 by default. */
16873 if (DECL_MAIN_P (current_function_decl))
16874 finish_return_stmt (integer_zero_node);
16875
16876 if (use_eh_spec_block (current_function_decl))
16877 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16878 (TREE_TYPE (current_function_decl)),
16879 current_eh_spec_block);
16880 }
16881
16882 /* If we're saving up tree structure, tie off the function now. */
16883 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
16884
16885 finish_fname_decls ();
16886
16887 /* If this function can't throw any exceptions, remember that. */
16888 if (!processing_template_decl
16889 && !cp_function_chain->can_throw
16890 && !flag_non_call_exceptions
16891 && !decl_replaceable_p (fndecl))
16892 TREE_NOTHROW (fndecl) = 1;
16893
16894 /* This must come after expand_function_end because cleanups might
16895 have declarations (from inline functions) that need to go into
16896 this function's blocks. */
16897
16898 /* If the current binding level isn't the outermost binding level
16899 for this function, either there is a bug, or we have experienced
16900 syntax errors and the statement tree is malformed. */
16901 if (current_binding_level->kind != sk_function_parms)
16902 {
16903 /* Make sure we have already experienced errors. */
16904 gcc_assert (errorcount);
16905
16906 /* Throw away the broken statement tree and extra binding
16907 levels. */
16908 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
16909
16910 while (current_binding_level->kind != sk_function_parms)
16911 {
16912 if (current_binding_level->kind == sk_class)
16913 pop_nested_class ();
16914 else
16915 poplevel (0, 0, 0);
16916 }
16917 }
16918 poplevel (1, 0, 1);
16919
16920 /* Statements should always be full-expressions at the outermost set
16921 of curly braces for a function. */
16922 gcc_assert (stmts_are_full_exprs_p ());
16923
16924 /* If there are no return statements in a function with auto return type,
16925 the return type is void. But if the declared type is something like
16926 auto*, this is an error. */
16927 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
16928 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
16929 {
16930 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
16931 && !current_function_returns_value
16932 && !current_function_returns_null)
16933 {
16934 /* We haven't applied return type deduction because we haven't
16935 seen any return statements. Do that now. */
16936 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
16937 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
16938 void_node, node, tf_warning_or_error,
16939 adc_return_type);
16940
16941 apply_deduced_return_type (fndecl, void_type_node);
16942 fntype = TREE_TYPE (fndecl);
16943 }
16944 else if (!current_function_returns_value
16945 && !current_function_returns_null)
16946 {
16947 error ("no return statements in function returning %qT",
16948 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
16949 inform (input_location, "only plain %<auto%> return type can be "
16950 "deduced to %<void%>");
16951 }
16952 }
16953
16954 /* Remember that we were in class scope. */
16955 if (current_class_name)
16956 ctype = current_class_type;
16957
16958 if (DECL_DELETED_FN (fndecl))
16959 {
16960 DECL_INITIAL (fndecl) = error_mark_node;
16961 DECL_SAVED_TREE (fndecl) = NULL_TREE;
16962 goto cleanup;
16963 }
16964
16965 // If this is a concept, check that the definition is reasonable.
16966 if (DECL_DECLARED_CONCEPT_P (fndecl))
16967 check_function_concept (fndecl);
16968
16969 if (flag_openmp)
16970 if (tree attr = lookup_attribute ("omp declare variant base",
16971 DECL_ATTRIBUTES (fndecl)))
16972 omp_declare_variant_finalize (fndecl, attr);
16973
16974 /* Lambda closure members are implicitly constexpr if possible. */
16975 if (cxx_dialect >= cxx17
16976 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
16977 DECL_DECLARED_CONSTEXPR_P (fndecl)
16978 = ((processing_template_decl
16979 || is_valid_constexpr_fn (fndecl, /*complain*/false))
16980 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
16981
16982 /* Save constexpr function body before it gets munged by
16983 the NRV transformation. */
16984 maybe_save_function_definition (fndecl);
16985
16986 /* Invoke the pre-genericize plugin before we start munging things. */
16987 if (!processing_template_decl)
16988 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
16989
16990 /* Perform delayed folding before NRV transformation. */
16991 if (!processing_template_decl
16992 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
16993 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
16994 cp_fold_function (fndecl);
16995
16996 /* Set up the named return value optimization, if we can. Candidate
16997 variables are selected in check_return_expr. */
16998 if (current_function_return_value)
16999 {
17000 tree r = current_function_return_value;
17001 tree outer;
17002
17003 if (r != error_mark_node
17004 /* This is only worth doing for fns that return in memory--and
17005 simpler, since we don't have to worry about promoted modes. */
17006 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
17007 /* Only allow this for variables declared in the outer scope of
17008 the function so we know that their lifetime always ends with a
17009 return; see g++.dg/opt/nrv6.C. We could be more flexible if
17010 we were to do this optimization in tree-ssa. */
17011 && (outer = outer_curly_brace_block (fndecl))
17012 && chain_member (r, BLOCK_VARS (outer)))
17013 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
17014
17015 current_function_return_value = NULL_TREE;
17016 }
17017
17018 /* Must mark the RESULT_DECL as being in this function. */
17019 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
17020
17021 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
17022 to the FUNCTION_DECL node itself. */
17023 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
17024
17025 /* Complain if there's just no return statement. */
17026 if (warn_return_type
17027 && !VOID_TYPE_P (TREE_TYPE (fntype))
17028 && !dependent_type_p (TREE_TYPE (fntype))
17029 && !current_function_returns_value && !current_function_returns_null
17030 /* Don't complain if we abort or throw. */
17031 && !current_function_returns_abnormally
17032 /* Don't complain if there's an infinite loop. */
17033 && !current_function_infinite_loop
17034 /* Don't complain if we are declared noreturn. */
17035 && !TREE_THIS_VOLATILE (fndecl)
17036 && !DECL_NAME (DECL_RESULT (fndecl))
17037 && !TREE_NO_WARNING (fndecl)
17038 /* Structor return values (if any) are set by the compiler. */
17039 && !DECL_CONSTRUCTOR_P (fndecl)
17040 && !DECL_DESTRUCTOR_P (fndecl)
17041 && targetm.warn_func_return (fndecl))
17042 {
17043 gcc_rich_location richloc (input_location);
17044 /* Potentially add a "return *this;" fix-it hint for
17045 assignment operators. */
17046 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17047 {
17048 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17049 if (TREE_CODE (valtype) == REFERENCE_TYPE
17050 && current_class_ref
17051 && same_type_ignoring_top_level_qualifiers_p
17052 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17053 && global_dc->option_enabled (OPT_Wreturn_type,
17054 global_dc->lang_mask,
17055 global_dc->option_state))
17056 add_return_star_this_fixit (&richloc, fndecl);
17057 }
17058 if (warning_at (&richloc, OPT_Wreturn_type,
17059 "no return statement in function returning non-void"))
17060 TREE_NO_WARNING (fndecl) = 1;
17061 }
17062
17063 /* Store the end of the function, so that we get good line number
17064 info for the epilogue. */
17065 cfun->function_end_locus = input_location;
17066
17067 /* Complain about parameters that are only set, but never otherwise used. */
17068 if (warn_unused_but_set_parameter
17069 && !processing_template_decl
17070 && errorcount == unused_but_set_errorcount
17071 && !DECL_CLONED_FUNCTION_P (fndecl))
17072 {
17073 tree decl;
17074
17075 for (decl = DECL_ARGUMENTS (fndecl);
17076 decl;
17077 decl = DECL_CHAIN (decl))
17078 if (TREE_USED (decl)
17079 && TREE_CODE (decl) == PARM_DECL
17080 && !DECL_READ_P (decl)
17081 && DECL_NAME (decl)
17082 && !DECL_ARTIFICIAL (decl)
17083 && !TREE_NO_WARNING (decl)
17084 && !DECL_IN_SYSTEM_HEADER (decl)
17085 && TREE_TYPE (decl) != error_mark_node
17086 && !TYPE_REF_P (TREE_TYPE (decl))
17087 && (!CLASS_TYPE_P (TREE_TYPE (decl))
17088 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17089 warning_at (DECL_SOURCE_LOCATION (decl),
17090 OPT_Wunused_but_set_parameter,
17091 "parameter %qD set but not used", decl);
17092 unused_but_set_errorcount = errorcount;
17093 }
17094
17095 /* Complain about locally defined typedefs that are not used in this
17096 function. */
17097 maybe_warn_unused_local_typedefs ();
17098
17099 /* Possibly warn about unused parameters. */
17100 if (warn_unused_parameter
17101 && !processing_template_decl
17102 && !DECL_CLONED_FUNCTION_P (fndecl))
17103 do_warn_unused_parameter (fndecl);
17104
17105 /* Genericize before inlining. */
17106 if (!processing_template_decl
17107 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17108 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17109 cp_genericize (fndecl);
17110
17111 /* Emit the resumer and destroyer functions now. */
17112 if (coro_p)
17113 {
17114 emit_coro_helper (resumer);
17115 emit_coro_helper (destroyer);
17116 }
17117
17118 cleanup:
17119 /* We're leaving the context of this function, so zap cfun. It's still in
17120 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17121 set_cfun (NULL);
17122 current_function_decl = NULL;
17123
17124 /* If this is an in-class inline definition, we may have to pop the
17125 bindings for the template parameters that we added in
17126 maybe_begin_member_template_processing when start_function was
17127 called. */
17128 if (inline_p)
17129 maybe_end_member_template_processing ();
17130
17131 /* Leave the scope of the class. */
17132 if (ctype)
17133 pop_nested_class ();
17134
17135 --function_depth;
17136
17137 /* Clean up. */
17138 current_function_decl = NULL_TREE;
17139
17140 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17141 return fndecl;
17142 }
17143 \f
17144 /* Create the FUNCTION_DECL for a function definition.
17145 DECLSPECS and DECLARATOR are the parts of the declaration;
17146 they describe the return type and the name of the function,
17147 but twisted together in a fashion that parallels the syntax of C.
17148
17149 This function creates a binding context for the function body
17150 as well as setting up the FUNCTION_DECL in current_function_decl.
17151
17152 Returns a FUNCTION_DECL on success.
17153
17154 If the DECLARATOR is not suitable for a function (it defines a datum
17155 instead), we return 0, which tells yyparse to report a parse error.
17156
17157 May return void_type_node indicating that this method is actually
17158 a friend. See grokfield for more details.
17159
17160 Came here with a `.pushlevel' .
17161
17162 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17163 CHANGES TO CODE IN `grokfield'. */
17164
17165 tree
17166 grokmethod (cp_decl_specifier_seq *declspecs,
17167 const cp_declarator *declarator, tree attrlist)
17168 {
17169 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17170 &attrlist);
17171
17172 if (fndecl == error_mark_node)
17173 return error_mark_node;
17174
17175 if (attrlist)
17176 cplus_decl_attributes (&fndecl, attrlist, 0);
17177
17178 /* Pass friends other than inline friend functions back. */
17179 if (fndecl == void_type_node)
17180 return fndecl;
17181
17182 if (DECL_IN_AGGR_P (fndecl))
17183 {
17184 if (DECL_CLASS_SCOPE_P (fndecl))
17185 error ("%qD is already defined in class %qT", fndecl,
17186 DECL_CONTEXT (fndecl));
17187 return error_mark_node;
17188 }
17189
17190 check_template_shadow (fndecl);
17191
17192 if (TREE_PUBLIC (fndecl))
17193 DECL_COMDAT (fndecl) = 1;
17194 DECL_DECLARED_INLINE_P (fndecl) = 1;
17195 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17196
17197 /* We process method specializations in finish_struct_1. */
17198 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17199 {
17200 fndecl = push_template_decl (fndecl);
17201 if (fndecl == error_mark_node)
17202 return fndecl;
17203 }
17204
17205 if (! DECL_FRIEND_P (fndecl))
17206 {
17207 if (DECL_CHAIN (fndecl))
17208 {
17209 fndecl = copy_node (fndecl);
17210 TREE_CHAIN (fndecl) = NULL_TREE;
17211 }
17212 }
17213
17214 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17215
17216 DECL_IN_AGGR_P (fndecl) = 1;
17217 return fndecl;
17218 }
17219 \f
17220
17221 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17222 we can lay it out later, when and if its type becomes complete.
17223
17224 Also handle constexpr variables where the initializer involves
17225 an unlowered PTRMEM_CST because the class isn't complete yet. */
17226
17227 void
17228 maybe_register_incomplete_var (tree var)
17229 {
17230 gcc_assert (VAR_P (var));
17231
17232 /* Keep track of variables with incomplete types. */
17233 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17234 && DECL_EXTERNAL (var))
17235 {
17236 tree inner_type = TREE_TYPE (var);
17237
17238 while (TREE_CODE (inner_type) == ARRAY_TYPE)
17239 inner_type = TREE_TYPE (inner_type);
17240 inner_type = TYPE_MAIN_VARIANT (inner_type);
17241
17242 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17243 /* RTTI TD entries are created while defining the type_info. */
17244 || (TYPE_LANG_SPECIFIC (inner_type)
17245 && TYPE_BEING_DEFINED (inner_type)))
17246 {
17247 incomplete_var iv = {var, inner_type};
17248 vec_safe_push (incomplete_vars, iv);
17249 }
17250 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17251 && decl_constant_var_p (var)
17252 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17253 {
17254 /* When the outermost open class is complete we can resolve any
17255 pointers-to-members. */
17256 tree context = outermost_open_class ();
17257 incomplete_var iv = {var, context};
17258 vec_safe_push (incomplete_vars, iv);
17259 }
17260 }
17261 }
17262
17263 /* Called when a class type (given by TYPE) is defined. If there are
17264 any existing VAR_DECLs whose type has been completed by this
17265 declaration, update them now. */
17266
17267 void
17268 complete_vars (tree type)
17269 {
17270 unsigned ix;
17271 incomplete_var *iv;
17272
17273 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17274 {
17275 if (same_type_p (type, iv->incomplete_type))
17276 {
17277 tree var = iv->decl;
17278 tree type = TREE_TYPE (var);
17279
17280 if (type != error_mark_node
17281 && (TYPE_MAIN_VARIANT (strip_array_types (type))
17282 == iv->incomplete_type))
17283 {
17284 /* Complete the type of the variable. The VAR_DECL itself
17285 will be laid out in expand_expr. */
17286 complete_type (type);
17287 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17288 }
17289
17290 /* Remove this entry from the list. */
17291 incomplete_vars->unordered_remove (ix);
17292 }
17293 else
17294 ix++;
17295 }
17296
17297 /* Check for pending declarations which may have abstract type. */
17298 complete_type_check_abstract (type);
17299 }
17300
17301 /* If DECL is of a type which needs a cleanup, build and return an
17302 expression to perform that cleanup here. Return NULL_TREE if no
17303 cleanup need be done. DECL can also be a _REF when called from
17304 split_nonconstant_init_1. */
17305
17306 tree
17307 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
17308 {
17309 tree type;
17310 tree attr;
17311 tree cleanup;
17312
17313 /* Assume no cleanup is required. */
17314 cleanup = NULL_TREE;
17315
17316 if (error_operand_p (decl))
17317 return cleanup;
17318
17319 /* Handle "__attribute__((cleanup))". We run the cleanup function
17320 before the destructor since the destructor is what actually
17321 terminates the lifetime of the object. */
17322 if (DECL_P (decl))
17323 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
17324 else
17325 attr = NULL_TREE;
17326 if (attr)
17327 {
17328 tree id;
17329 tree fn;
17330 tree arg;
17331
17332 /* Get the name specified by the user for the cleanup function. */
17333 id = TREE_VALUE (TREE_VALUE (attr));
17334 /* Look up the name to find the cleanup function to call. It is
17335 important to use lookup_name here because that is what is
17336 used in c-common.c:handle_cleanup_attribute when performing
17337 initial checks on the attribute. Note that those checks
17338 include ensuring that the function found is not an overloaded
17339 function, or an object with an overloaded call operator,
17340 etc.; we can rely on the fact that the function found is an
17341 ordinary FUNCTION_DECL. */
17342 fn = lookup_name (id);
17343 arg = build_address (decl);
17344 if (!mark_used (decl, complain) && !(complain & tf_error))
17345 return error_mark_node;
17346 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
17347 if (cleanup == error_mark_node)
17348 return error_mark_node;
17349 }
17350 /* Handle ordinary C++ destructors. */
17351 type = TREE_TYPE (decl);
17352 if (type_build_dtor_call (type))
17353 {
17354 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
17355 tree addr;
17356 tree call;
17357
17358 if (TREE_CODE (type) == ARRAY_TYPE)
17359 addr = decl;
17360 else
17361 addr = build_address (decl);
17362
17363 call = build_delete (input_location, TREE_TYPE (addr), addr,
17364 sfk_complete_destructor, flags, 0, complain);
17365 if (call == error_mark_node)
17366 cleanup = error_mark_node;
17367 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
17368 /* Discard the call. */;
17369 else if (decl_maybe_constant_destruction (decl, type)
17370 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
17371 cxx_constant_dtor (call, decl);
17372 else if (cleanup)
17373 cleanup = cp_build_compound_expr (cleanup, call, complain);
17374 else
17375 cleanup = call;
17376 }
17377
17378 /* build_delete sets the location of the destructor call to the
17379 current location, even though the destructor is going to be
17380 called later, at the end of the current scope. This can lead to
17381 a "jumpy" behavior for users of debuggers when they step around
17382 the end of the block. So let's unset the location of the
17383 destructor call instead. */
17384 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
17385 if (cleanup && CONVERT_EXPR_P (cleanup))
17386 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
17387
17388 if (cleanup
17389 && DECL_P (decl)
17390 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
17391 /* Treat objects with destructors as used; the destructor may do
17392 something substantive. */
17393 && !mark_used (decl, complain) && !(complain & tf_error))
17394 return error_mark_node;
17395
17396 if (cleanup && cfun && !processing_template_decl
17397 && !expr_noexcept_p (cleanup, tf_none))
17398 cp_function_chain->throwing_cleanup = true;
17399
17400 return cleanup;
17401 }
17402
17403 \f
17404 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17405 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17406 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17407
17408 tree
17409 static_fn_type (tree memfntype)
17410 {
17411 tree fntype;
17412 tree args;
17413
17414 if (TYPE_PTRMEMFUNC_P (memfntype))
17415 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
17416 if (INDIRECT_TYPE_P (memfntype)
17417 || TREE_CODE (memfntype) == FUNCTION_DECL)
17418 memfntype = TREE_TYPE (memfntype);
17419 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
17420 return memfntype;
17421 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
17422 args = TYPE_ARG_TYPES (memfntype);
17423 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
17424 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
17425 fntype = (cp_build_type_attribute_variant
17426 (fntype, TYPE_ATTRIBUTES (memfntype)));
17427 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
17428 return fntype;
17429 }
17430
17431 /* DECL was originally constructed as a non-static member function,
17432 but turned out to be static. Update it accordingly. */
17433
17434 void
17435 revert_static_member_fn (tree decl)
17436 {
17437 tree stype = static_fn_type (decl);
17438 cp_cv_quals quals = type_memfn_quals (stype);
17439 cp_ref_qualifier rqual = type_memfn_rqual (stype);
17440
17441 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
17442 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
17443
17444 TREE_TYPE (decl) = stype;
17445
17446 if (DECL_ARGUMENTS (decl))
17447 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
17448 DECL_STATIC_FUNCTION_P (decl) = 1;
17449 }
17450
17451 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17452 one of the language-independent trees. */
17453
17454 enum cp_tree_node_structure_enum
17455 cp_tree_node_structure (union lang_tree_node * t)
17456 {
17457 switch (TREE_CODE (&t->generic))
17458 {
17459 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
17460 case BASELINK: return TS_CP_BASELINK;
17461 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
17462 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
17463 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
17464 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
17465 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
17466 case OVERLOAD: return TS_CP_OVERLOAD;
17467 case PTRMEM_CST: return TS_CP_PTRMEM;
17468 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
17469 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
17470 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
17471 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
17472 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
17473 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
17474 default: return TS_CP_GENERIC;
17475 }
17476 }
17477
17478 /* Build the void_list_node (void_type_node having been created). */
17479 tree
17480 build_void_list_node (void)
17481 {
17482 tree t = build_tree_list (NULL_TREE, void_type_node);
17483 return t;
17484 }
17485
17486 bool
17487 cp_missing_noreturn_ok_p (tree decl)
17488 {
17489 /* A missing noreturn is ok for the `main' function. */
17490 return DECL_MAIN_P (decl);
17491 }
17492
17493 /* Return the decl used to identify the COMDAT group into which DECL should
17494 be placed. */
17495
17496 tree
17497 cxx_comdat_group (tree decl)
17498 {
17499 /* Virtual tables, construction virtual tables, and virtual table
17500 tables all go in a single COMDAT group, named after the primary
17501 virtual table. */
17502 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
17503 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
17504 /* For all other DECLs, the COMDAT group is the mangled name of the
17505 declaration itself. */
17506 else
17507 {
17508 while (DECL_THUNK_P (decl))
17509 {
17510 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17511 into the same section as the target function. In that case
17512 we must return target's name. */
17513 tree target = THUNK_TARGET (decl);
17514 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
17515 && DECL_SECTION_NAME (target) != NULL
17516 && DECL_ONE_ONLY (target))
17517 decl = target;
17518 else
17519 break;
17520 }
17521 }
17522
17523 return decl;
17524 }
17525
17526 /* Returns the return type for FN as written by the user, which may include
17527 a placeholder for a deduced return type. */
17528
17529 tree
17530 fndecl_declared_return_type (tree fn)
17531 {
17532 fn = STRIP_TEMPLATE (fn);
17533 if (FNDECL_USED_AUTO (fn))
17534 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17535
17536 return TREE_TYPE (TREE_TYPE (fn));
17537 }
17538
17539 /* Returns true iff DECL is a variable or function declared with an auto type
17540 that has not yet been deduced to a real type. */
17541
17542 bool
17543 undeduced_auto_decl (tree decl)
17544 {
17545 if (cxx_dialect < cxx11)
17546 return false;
17547 STRIP_ANY_LOCATION_WRAPPER (decl);
17548 return ((VAR_OR_FUNCTION_DECL_P (decl)
17549 || TREE_CODE (decl) == TEMPLATE_DECL)
17550 && type_uses_auto (TREE_TYPE (decl)));
17551 }
17552
17553 /* Complain if DECL has an undeduced return type. */
17554
17555 bool
17556 require_deduced_type (tree decl, tsubst_flags_t complain)
17557 {
17558 if (undeduced_auto_decl (decl))
17559 {
17560 if (TREE_NO_WARNING (decl) && seen_error ())
17561 /* We probably already complained about deduction failure. */;
17562 else if (complain & tf_error)
17563 error ("use of %qD before deduction of %<auto%>", decl);
17564 return false;
17565 }
17566 return true;
17567 }
17568
17569 /* Create a representation of the explicit-specifier with
17570 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17571
17572 tree
17573 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17574 {
17575 if (instantiation_dependent_expression_p (expr))
17576 /* Wait for instantiation, tsubst_function_decl will handle it. */
17577 return expr;
17578
17579 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17580 /* Don't let convert_like_real create more template codes. */
17581 processing_template_decl_sentinel s;
17582 expr = build_converted_constant_bool_expr (expr, complain);
17583 expr = cxx_constant_value (expr);
17584 return expr;
17585 }
17586
17587 #include "gt-cp-decl.h"