]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/decl.c
arm: Remove yet another unused variable.
[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 if (VAR_P (decl))
7661 {
7662 duration_kind dk = decl_storage_duration (decl);
7663 /* [dcl.constinit]/1 "The constinit specifier shall be applied
7664 only to a declaration of a variable with static or thread storage
7665 duration." */
7666 if ((flags & LOOKUP_CONSTINIT)
7667 && !(dk == dk_thread || dk == dk_static))
7668 {
7669 error_at (DECL_SOURCE_LOCATION (decl),
7670 "%<constinit%> can only be applied to a variable with "
7671 "static or thread storage duration");
7672 return;
7673 }
7674
7675 /* If this is a local variable that will need a mangled name,
7676 register it now. We must do this before processing the
7677 initializer for the variable, since the initialization might
7678 require a guard variable, and since the mangled name of the
7679 guard variable will depend on the mangled name of this
7680 variable. */
7681 if (DECL_FUNCTION_SCOPE_P (decl)
7682 && TREE_STATIC (decl)
7683 && !DECL_ARTIFICIAL (decl))
7684 {
7685 /* The variable holding an anonymous union will have had its
7686 discriminator set in finish_anon_union, after which it's
7687 NAME will have been cleared. */
7688 if (DECL_NAME (decl))
7689 determine_local_discriminator (decl);
7690 /* Normally has_forced_label_in_static is set during GIMPLE
7691 lowering, but [cd]tors are never actually compiled directly.
7692 We need to set this early so we can deal with the label
7693 address extension. */
7694 if ((DECL_CONSTRUCTOR_P (current_function_decl)
7695 || DECL_DESTRUCTOR_P (current_function_decl))
7696 && init)
7697 {
7698 walk_tree (&init, notice_forced_label_r, NULL, NULL);
7699 add_local_decl (cfun, decl);
7700 }
7701 /* And make sure it's in the symbol table for
7702 c_parse_final_cleanups to find. */
7703 varpool_node::get_create (decl);
7704 }
7705
7706 /* Convert the initializer to the type of DECL, if we have not
7707 already initialized DECL. */
7708 if (!DECL_INITIALIZED_P (decl)
7709 /* If !DECL_EXTERNAL then DECL is being defined. In the
7710 case of a static data member initialized inside the
7711 class-specifier, there can be an initializer even if DECL
7712 is *not* defined. */
7713 && (!DECL_EXTERNAL (decl) || init))
7714 {
7715 cleanups = make_tree_vector ();
7716 init = check_initializer (decl, init, flags, &cleanups);
7717
7718 /* Handle:
7719
7720 [dcl.init]
7721
7722 The memory occupied by any object of static storage
7723 duration is zero-initialized at program startup before
7724 any other initialization takes place.
7725
7726 We cannot create an appropriate initializer until after
7727 the type of DECL is finalized. If DECL_INITIAL is set,
7728 then the DECL is statically initialized, and any
7729 necessary zero-initialization has already been performed. */
7730 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
7731 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
7732 /*nelts=*/NULL_TREE,
7733 /*static_storage_p=*/true);
7734 /* Remember that the initialization for this variable has
7735 taken place. */
7736 DECL_INITIALIZED_P (decl) = 1;
7737 /* This declaration is the definition of this variable,
7738 unless we are initializing a static data member within
7739 the class specifier. */
7740 if (!DECL_EXTERNAL (decl))
7741 var_definition_p = true;
7742 }
7743 /* If the variable has an array type, lay out the type, even if
7744 there is no initializer. It is valid to index through the
7745 array, and we must get TYPE_ALIGN set correctly on the array
7746 type. */
7747 else if (TREE_CODE (type) == ARRAY_TYPE)
7748 layout_type (type);
7749
7750 if (TREE_STATIC (decl)
7751 && !at_function_scope_p ()
7752 && current_function_decl == NULL)
7753 /* So decl is a global variable or a static member of a
7754 non local class. Record the types it uses
7755 so that we can decide later to emit debug info for them. */
7756 record_types_used_by_current_var_decl (decl);
7757 }
7758
7759 /* Add this declaration to the statement-tree. This needs to happen
7760 after the call to check_initializer so that the DECL_EXPR for a
7761 reference temp is added before the DECL_EXPR for the reference itself. */
7762 if (DECL_FUNCTION_SCOPE_P (decl))
7763 {
7764 /* If we're building a variable sized type, and we might be
7765 reachable other than via the top of the current binding
7766 level, then create a new BIND_EXPR so that we deallocate
7767 the object at the right time. */
7768 if (VAR_P (decl)
7769 && DECL_SIZE (decl)
7770 && !TREE_CONSTANT (DECL_SIZE (decl))
7771 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
7772 {
7773 tree bind;
7774 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7775 TREE_SIDE_EFFECTS (bind) = 1;
7776 add_stmt (bind);
7777 BIND_EXPR_BODY (bind) = push_stmt_list ();
7778 }
7779 add_decl_expr (decl);
7780 }
7781
7782 /* Let the middle end know about variables and functions -- but not
7783 static data members in uninstantiated class templates. */
7784 if (VAR_OR_FUNCTION_DECL_P (decl))
7785 {
7786 if (VAR_P (decl))
7787 {
7788 layout_var_decl (decl);
7789 maybe_commonize_var (decl);
7790 }
7791
7792 /* This needs to happen after the linkage is set. */
7793 determine_visibility (decl);
7794
7795 if (var_definition_p && TREE_STATIC (decl))
7796 {
7797 /* If a TREE_READONLY variable needs initialization
7798 at runtime, it is no longer readonly and we need to
7799 avoid MEM_READONLY_P being set on RTL created for it. */
7800 if (init)
7801 {
7802 if (TREE_READONLY (decl))
7803 TREE_READONLY (decl) = 0;
7804 was_readonly = 0;
7805 }
7806 else if (was_readonly)
7807 TREE_READONLY (decl) = 1;
7808
7809 /* Likewise if it needs destruction. */
7810 if (!decl_maybe_constant_destruction (decl, type))
7811 TREE_READONLY (decl) = 0;
7812 }
7813
7814 make_rtl_for_nonlocal_decl (decl, init, asmspec);
7815
7816 /* Check for abstractness of the type. Notice that there is no
7817 need to strip array types here since the check for those types
7818 is already done within create_array_type_for_decl. */
7819 abstract_virtuals_error (decl, type);
7820
7821 if (TREE_TYPE (decl) == error_mark_node)
7822 /* No initialization required. */
7823 ;
7824 else if (TREE_CODE (decl) == FUNCTION_DECL)
7825 {
7826 if (init)
7827 {
7828 if (init == ridpointers[(int)RID_DEFAULT])
7829 {
7830 /* An out-of-class default definition is defined at
7831 the point where it is explicitly defaulted. */
7832 if (DECL_DELETED_FN (decl))
7833 maybe_explain_implicit_delete (decl);
7834 else if (DECL_INITIAL (decl) == error_mark_node)
7835 synthesize_method (decl);
7836 }
7837 else
7838 error_at (cp_expr_loc_or_loc (init,
7839 DECL_SOURCE_LOCATION (decl)),
7840 "function %q#D is initialized like a variable",
7841 decl);
7842 }
7843 /* else no initialization required. */
7844 }
7845 else if (DECL_EXTERNAL (decl)
7846 && ! (DECL_LANG_SPECIFIC (decl)
7847 && DECL_NOT_REALLY_EXTERN (decl)))
7848 {
7849 /* check_initializer will have done any constant initialization. */
7850 }
7851 /* A variable definition. */
7852 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7853 /* Initialize the local variable. */
7854 initialize_local_var (decl, init);
7855
7856 /* If a variable is defined, and then a subsequent
7857 definition with external linkage is encountered, we will
7858 get here twice for the same variable. We want to avoid
7859 calling expand_static_init more than once. For variables
7860 that are not static data members, we can call
7861 expand_static_init only when we actually process the
7862 initializer. It is not legal to redeclare a static data
7863 member, so this issue does not arise in that case. */
7864 else if (var_definition_p && TREE_STATIC (decl))
7865 expand_static_init (decl, init);
7866 }
7867
7868 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7869 reference, insert it in the statement-tree now. */
7870 if (cleanups)
7871 {
7872 unsigned i; tree t;
7873 FOR_EACH_VEC_ELT (*cleanups, i, t)
7874 push_cleanup (decl, t, false);
7875 release_tree_vector (cleanups);
7876 }
7877
7878 if (was_readonly)
7879 TREE_READONLY (decl) = 1;
7880
7881 if (flag_openmp
7882 && VAR_P (decl)
7883 && lookup_attribute ("omp declare target implicit",
7884 DECL_ATTRIBUTES (decl)))
7885 {
7886 DECL_ATTRIBUTES (decl)
7887 = remove_attribute ("omp declare target implicit",
7888 DECL_ATTRIBUTES (decl));
7889 complete_type (TREE_TYPE (decl));
7890 if (!cp_omp_mappable_type (TREE_TYPE (decl)))
7891 {
7892 error ("%q+D in declare target directive does not have mappable"
7893 " type", decl);
7894 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl));
7895 }
7896 else if (!lookup_attribute ("omp declare target",
7897 DECL_ATTRIBUTES (decl))
7898 && !lookup_attribute ("omp declare target link",
7899 DECL_ATTRIBUTES (decl)))
7900 DECL_ATTRIBUTES (decl)
7901 = tree_cons (get_identifier ("omp declare target"),
7902 NULL_TREE, DECL_ATTRIBUTES (decl));
7903 }
7904
7905 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7906 }
7907
7908 /* For class TYPE return itself or some its bases that contain
7909 any direct non-static data members. Return error_mark_node if an
7910 error has been diagnosed. */
7911
7912 static tree
7913 find_decomp_class_base (location_t loc, tree type, tree ret)
7914 {
7915 bool member_seen = false;
7916 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7917 if (TREE_CODE (field) != FIELD_DECL
7918 || DECL_ARTIFICIAL (field)
7919 || DECL_UNNAMED_BIT_FIELD (field))
7920 continue;
7921 else if (ret)
7922 return type;
7923 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7924 {
7925 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
7926 error_at (loc, "cannot decompose class type %qT because it has an "
7927 "anonymous struct member", type);
7928 else
7929 error_at (loc, "cannot decompose class type %qT because it has an "
7930 "anonymous union member", type);
7931 inform (DECL_SOURCE_LOCATION (field), "declared here");
7932 return error_mark_node;
7933 }
7934 else if (!accessible_p (type, field, true))
7935 {
7936 error_at (loc, "cannot decompose inaccessible member %qD of %qT",
7937 field, type);
7938 inform (DECL_SOURCE_LOCATION (field),
7939 TREE_PRIVATE (field)
7940 ? G_("declared private here")
7941 : G_("declared protected here"));
7942 return error_mark_node;
7943 }
7944 else
7945 member_seen = true;
7946
7947 tree base_binfo, binfo;
7948 tree orig_ret = ret;
7949 int i;
7950 if (member_seen)
7951 ret = type;
7952 for (binfo = TYPE_BINFO (type), i = 0;
7953 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7954 {
7955 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret);
7956 if (t == error_mark_node)
7957 return error_mark_node;
7958 if (t != NULL_TREE && t != ret)
7959 {
7960 if (ret == type)
7961 {
7962 error_at (loc, "cannot decompose class type %qT: both it and "
7963 "its base class %qT have non-static data members",
7964 type, t);
7965 return error_mark_node;
7966 }
7967 else if (orig_ret != NULL_TREE)
7968 return t;
7969 else if (ret != NULL_TREE)
7970 {
7971 error_at (loc, "cannot decompose class type %qT: its base "
7972 "classes %qT and %qT have non-static data "
7973 "members", type, ret, t);
7974 return error_mark_node;
7975 }
7976 else
7977 ret = t;
7978 }
7979 }
7980 return ret;
7981 }
7982
7983 /* Return std::tuple_size<TYPE>::value. */
7984
7985 static tree
7986 get_tuple_size (tree type)
7987 {
7988 tree args = make_tree_vec (1);
7989 TREE_VEC_ELT (args, 0) = type;
7990 tree inst = lookup_template_class (tuple_size_identifier, args,
7991 /*in_decl*/NULL_TREE,
7992 /*context*/std_node,
7993 /*entering_scope*/false, tf_none);
7994 inst = complete_type (inst);
7995 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
7996 return NULL_TREE;
7997 tree val = lookup_qualified_name (inst, value_identifier,
7998 /*type*/false, /*complain*/false);
7999 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL)
8000 val = maybe_constant_value (val);
8001 if (TREE_CODE (val) == INTEGER_CST)
8002 return val;
8003 else
8004 return error_mark_node;
8005 }
8006
8007 /* Return std::tuple_element<I,TYPE>::type. */
8008
8009 static tree
8010 get_tuple_element_type (tree type, unsigned i)
8011 {
8012 tree args = make_tree_vec (2);
8013 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i);
8014 TREE_VEC_ELT (args, 1) = type;
8015 tree inst = lookup_template_class (tuple_element_identifier, args,
8016 /*in_decl*/NULL_TREE,
8017 /*context*/std_node,
8018 /*entering_scope*/false,
8019 tf_warning_or_error);
8020 return make_typename_type (inst, type_identifier,
8021 none_type, tf_warning_or_error);
8022 }
8023
8024 /* Return e.get<i>() or get<i>(e). */
8025
8026 static tree
8027 get_tuple_decomp_init (tree decl, unsigned i)
8028 {
8029 tree targs = make_tree_vec (1);
8030 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i);
8031
8032 tree etype = TREE_TYPE (decl);
8033 tree e = convert_from_reference (decl);
8034
8035 /* [The id-expression] e is an lvalue if the type of the entity e is an
8036 lvalue reference and an xvalue otherwise. */
8037 if (!TYPE_REF_P (etype)
8038 || TYPE_REF_IS_RVALUE (etype))
8039 e = move (e);
8040
8041 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier,
8042 /*type*/false, /*complain*/false);
8043 bool use_member_get = false;
8044
8045 /* To use a member get, member lookup must find at least one
8046 declaration that is a function template
8047 whose first template parameter is a non-type parameter. */
8048 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter)
8049 {
8050 tree fn = *iter;
8051 if (TREE_CODE (fn) == TEMPLATE_DECL)
8052 {
8053 tree tparms = DECL_TEMPLATE_PARMS (fn);
8054 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0);
8055 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
8056 {
8057 use_member_get = true;
8058 break;
8059 }
8060 }
8061 }
8062
8063 if (use_member_get)
8064 {
8065 fns = lookup_template_function (fns, targs);
8066 return build_new_method_call (e, fns, /*args*/NULL,
8067 /*path*/NULL_TREE, LOOKUP_NORMAL,
8068 /*fn_p*/NULL, tf_warning_or_error);
8069 }
8070 else
8071 {
8072 releasing_vec args (make_tree_vector_single (e));
8073 fns = lookup_template_function (get__identifier, targs);
8074 fns = perform_koenig_lookup (fns, args, tf_warning_or_error);
8075 return finish_call_expr (fns, &args, /*novirt*/false,
8076 /*koenig*/true, tf_warning_or_error);
8077 }
8078 }
8079
8080 /* It's impossible to recover the decltype of a tuple decomposition variable
8081 based on the actual type of the variable, so store it in a hash table. */
8082
8083 static GTY((cache)) decl_tree_cache_map *decomp_type_table;
8084
8085 tree
8086 lookup_decomp_type (tree v)
8087 {
8088 return *decomp_type_table->get (v);
8089 }
8090
8091 /* Mangle a decomposition declaration if needed. Arguments like
8092 in cp_finish_decomp. */
8093
8094 void
8095 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count)
8096 {
8097 if (!processing_template_decl
8098 && !error_operand_p (decl)
8099 && TREE_STATIC (decl))
8100 {
8101 auto_vec<tree, 16> v;
8102 v.safe_grow (count);
8103 tree d = first;
8104 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8105 v[count - i - 1] = d;
8106 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v));
8107 maybe_apply_pragma_weak (decl);
8108 }
8109 }
8110
8111 /* Finish a decomposition declaration. DECL is the underlying declaration
8112 "e", FIRST is the head of a chain of decls for the individual identifiers
8113 chained through DECL_CHAIN in reverse order and COUNT is the number of
8114 those decls. */
8115
8116 void
8117 cp_finish_decomp (tree decl, tree first, unsigned int count)
8118 {
8119 if (error_operand_p (decl))
8120 {
8121 error_out:
8122 while (count--)
8123 {
8124 TREE_TYPE (first) = error_mark_node;
8125 if (DECL_HAS_VALUE_EXPR_P (first))
8126 {
8127 SET_DECL_VALUE_EXPR (first, NULL_TREE);
8128 DECL_HAS_VALUE_EXPR_P (first) = 0;
8129 }
8130 first = DECL_CHAIN (first);
8131 }
8132 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl))
8133 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>"));
8134 return;
8135 }
8136
8137 location_t loc = DECL_SOURCE_LOCATION (decl);
8138 if (type_dependent_expression_p (decl)
8139 /* This happens for range for when not in templates.
8140 Still add the DECL_VALUE_EXPRs for later processing. */
8141 || (!processing_template_decl
8142 && type_uses_auto (TREE_TYPE (decl))))
8143 {
8144 for (unsigned int i = 0; i < count; i++)
8145 {
8146 if (!DECL_HAS_VALUE_EXPR_P (first))
8147 {
8148 tree v = build_nt (ARRAY_REF, decl,
8149 size_int (count - i - 1),
8150 NULL_TREE, NULL_TREE);
8151 SET_DECL_VALUE_EXPR (first, v);
8152 DECL_HAS_VALUE_EXPR_P (first) = 1;
8153 }
8154 if (processing_template_decl)
8155 fit_decomposition_lang_decl (first, decl);
8156 first = DECL_CHAIN (first);
8157 }
8158 return;
8159 }
8160
8161 auto_vec<tree, 16> v;
8162 v.safe_grow (count);
8163 tree d = first;
8164 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d))
8165 {
8166 v[count - i - 1] = d;
8167 fit_decomposition_lang_decl (d, decl);
8168 }
8169
8170 tree type = TREE_TYPE (decl);
8171 tree dexp = decl;
8172
8173 if (TYPE_REF_P (type))
8174 {
8175 dexp = convert_from_reference (dexp);
8176 type = complete_type (TREE_TYPE (type));
8177 if (type == error_mark_node)
8178 goto error_out;
8179 if (!COMPLETE_TYPE_P (type))
8180 {
8181 error_at (loc, "structured binding refers to incomplete type %qT",
8182 type);
8183 goto error_out;
8184 }
8185 }
8186
8187 tree eltype = NULL_TREE;
8188 unsigned HOST_WIDE_INT eltscnt = 0;
8189 if (TREE_CODE (type) == ARRAY_TYPE)
8190 {
8191 tree nelts;
8192 nelts = array_type_nelts_top (type);
8193 if (nelts == error_mark_node)
8194 goto error_out;
8195 if (!tree_fits_uhwi_p (nelts))
8196 {
8197 error_at (loc, "cannot decompose variable length array %qT", type);
8198 goto error_out;
8199 }
8200 eltscnt = tree_to_uhwi (nelts);
8201 if (count != eltscnt)
8202 {
8203 cnt_mismatch:
8204 if (count > eltscnt)
8205 error_n (loc, count,
8206 "%u name provided for structured binding",
8207 "%u names provided for structured binding", count);
8208 else
8209 error_n (loc, count,
8210 "only %u name provided for structured binding",
8211 "only %u names provided for structured binding", count);
8212 inform_n (loc, eltscnt,
8213 "while %qT decomposes into %wu element",
8214 "while %qT decomposes into %wu elements",
8215 type, eltscnt);
8216 goto error_out;
8217 }
8218 eltype = TREE_TYPE (type);
8219 for (unsigned int i = 0; i < count; i++)
8220 {
8221 TREE_TYPE (v[i]) = eltype;
8222 layout_decl (v[i], 0);
8223 if (processing_template_decl)
8224 continue;
8225 tree t = unshare_expr (dexp);
8226 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8227 eltype, t, size_int (i), NULL_TREE,
8228 NULL_TREE);
8229 SET_DECL_VALUE_EXPR (v[i], t);
8230 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8231 }
8232 }
8233 /* 2 GNU extensions. */
8234 else if (TREE_CODE (type) == COMPLEX_TYPE)
8235 {
8236 eltscnt = 2;
8237 if (count != eltscnt)
8238 goto cnt_mismatch;
8239 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8240 for (unsigned int i = 0; i < count; i++)
8241 {
8242 TREE_TYPE (v[i]) = eltype;
8243 layout_decl (v[i], 0);
8244 if (processing_template_decl)
8245 continue;
8246 tree t = unshare_expr (dexp);
8247 t = build1_loc (DECL_SOURCE_LOCATION (v[i]),
8248 i ? IMAGPART_EXPR : REALPART_EXPR, eltype,
8249 t);
8250 SET_DECL_VALUE_EXPR (v[i], t);
8251 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8252 }
8253 }
8254 else if (TREE_CODE (type) == VECTOR_TYPE)
8255 {
8256 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt))
8257 {
8258 error_at (loc, "cannot decompose variable length vector %qT", type);
8259 goto error_out;
8260 }
8261 if (count != eltscnt)
8262 goto cnt_mismatch;
8263 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
8264 for (unsigned int i = 0; i < count; i++)
8265 {
8266 TREE_TYPE (v[i]) = eltype;
8267 layout_decl (v[i], 0);
8268 if (processing_template_decl)
8269 continue;
8270 tree t = unshare_expr (dexp);
8271 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]),
8272 &t, size_int (i));
8273 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF,
8274 eltype, t, size_int (i), NULL_TREE,
8275 NULL_TREE);
8276 SET_DECL_VALUE_EXPR (v[i], t);
8277 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8278 }
8279 }
8280 else if (tree tsize = get_tuple_size (type))
8281 {
8282 if (tsize == error_mark_node)
8283 {
8284 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral "
8285 "constant expression", type);
8286 goto error_out;
8287 }
8288 if (!tree_fits_uhwi_p (tsize))
8289 {
8290 error_n (loc, count,
8291 "%u name provided for structured binding",
8292 "%u names provided for structured binding", count);
8293 inform (loc, "while %qT decomposes into %E elements",
8294 type, tsize);
8295 goto error_out;
8296 }
8297 eltscnt = tree_to_uhwi (tsize);
8298 if (count != eltscnt)
8299 goto cnt_mismatch;
8300 int save_read = DECL_READ_P (decl);
8301 for (unsigned i = 0; i < count; ++i)
8302 {
8303 location_t sloc = input_location;
8304 location_t dloc = DECL_SOURCE_LOCATION (v[i]);
8305
8306 input_location = dloc;
8307 tree init = get_tuple_decomp_init (decl, i);
8308 tree eltype = (init == error_mark_node ? error_mark_node
8309 : get_tuple_element_type (type, i));
8310 input_location = sloc;
8311
8312 if (init == error_mark_node || eltype == error_mark_node)
8313 {
8314 inform (dloc, "in initialization of structured binding "
8315 "variable %qD", v[i]);
8316 goto error_out;
8317 }
8318 /* Save the decltype away before reference collapse. */
8319 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype);
8320 eltype = cp_build_reference_type (eltype, !lvalue_p (init));
8321 TREE_TYPE (v[i]) = eltype;
8322 layout_decl (v[i], 0);
8323 if (DECL_HAS_VALUE_EXPR_P (v[i]))
8324 {
8325 /* In this case the names are variables, not just proxies. */
8326 SET_DECL_VALUE_EXPR (v[i], NULL_TREE);
8327 DECL_HAS_VALUE_EXPR_P (v[i]) = 0;
8328 }
8329 if (!processing_template_decl)
8330 {
8331 TREE_PUBLIC (v[i]) = TREE_PUBLIC (decl);
8332 TREE_STATIC (v[i]) = TREE_STATIC (decl);
8333 DECL_COMMON (v[i]) = DECL_COMMON (decl);
8334 DECL_COMDAT (v[i]) = DECL_COMDAT (decl);
8335 if (TREE_STATIC (v[i]))
8336 {
8337 CP_DECL_THREAD_LOCAL_P (v[i])
8338 = CP_DECL_THREAD_LOCAL_P (decl);
8339 set_decl_tls_model (v[i], DECL_TLS_MODEL (decl));
8340 if (DECL_ONE_ONLY (decl))
8341 make_decl_one_only (v[i], cxx_comdat_group (v[i]));
8342 if (TREE_PUBLIC (decl))
8343 DECL_WEAK (v[i]) = DECL_WEAK (decl);
8344 DECL_VISIBILITY (v[i]) = DECL_VISIBILITY (decl);
8345 DECL_VISIBILITY_SPECIFIED (v[i])
8346 = DECL_VISIBILITY_SPECIFIED (decl);
8347 }
8348 cp_finish_decl (v[i], init, /*constexpr*/false,
8349 /*asm*/NULL_TREE, LOOKUP_NORMAL);
8350 }
8351 }
8352 /* Ignore reads from the underlying decl performed during initialization
8353 of the individual variables. If those will be read, we'll mark
8354 the underlying decl as read at that point. */
8355 DECL_READ_P (decl) = save_read;
8356 }
8357 else if (TREE_CODE (type) == UNION_TYPE)
8358 {
8359 error_at (loc, "cannot decompose union type %qT", type);
8360 goto error_out;
8361 }
8362 else if (!CLASS_TYPE_P (type))
8363 {
8364 error_at (loc, "cannot decompose non-array non-class type %qT", type);
8365 goto error_out;
8366 }
8367 else if (LAMBDA_TYPE_P (type))
8368 {
8369 error_at (loc, "cannot decompose lambda closure type %qT", type);
8370 goto error_out;
8371 }
8372 else if (processing_template_decl && !COMPLETE_TYPE_P (type))
8373 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT",
8374 type);
8375 else
8376 {
8377 tree btype = find_decomp_class_base (loc, type, NULL_TREE);
8378 if (btype == error_mark_node)
8379 goto error_out;
8380 else if (btype == NULL_TREE)
8381 {
8382 error_at (loc, "cannot decompose class type %qT without non-static "
8383 "data members", type);
8384 goto error_out;
8385 }
8386 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8387 if (TREE_CODE (field) != FIELD_DECL
8388 || DECL_ARTIFICIAL (field)
8389 || DECL_UNNAMED_BIT_FIELD (field))
8390 continue;
8391 else
8392 eltscnt++;
8393 if (count != eltscnt)
8394 goto cnt_mismatch;
8395 tree t = dexp;
8396 if (type != btype)
8397 {
8398 t = convert_to_base (t, btype, /*check_access*/true,
8399 /*nonnull*/false, tf_warning_or_error);
8400 type = btype;
8401 }
8402 unsigned int i = 0;
8403 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field))
8404 if (TREE_CODE (field) != FIELD_DECL
8405 || DECL_ARTIFICIAL (field)
8406 || DECL_UNNAMED_BIT_FIELD (field))
8407 continue;
8408 else
8409 {
8410 tree tt = finish_non_static_data_member (field, unshare_expr (t),
8411 NULL_TREE);
8412 if (REFERENCE_REF_P (tt))
8413 tt = TREE_OPERAND (tt, 0);
8414 TREE_TYPE (v[i]) = TREE_TYPE (tt);
8415 layout_decl (v[i], 0);
8416 if (!processing_template_decl)
8417 {
8418 SET_DECL_VALUE_EXPR (v[i], tt);
8419 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8420 }
8421 i++;
8422 }
8423 }
8424 if (processing_template_decl)
8425 {
8426 for (unsigned int i = 0; i < count; i++)
8427 if (!DECL_HAS_VALUE_EXPR_P (v[i]))
8428 {
8429 tree a = build_nt (ARRAY_REF, decl, size_int (i),
8430 NULL_TREE, NULL_TREE);
8431 SET_DECL_VALUE_EXPR (v[i], a);
8432 DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
8433 }
8434 }
8435 }
8436
8437 /* Returns a declaration for a VAR_DECL as if:
8438
8439 extern "C" TYPE NAME;
8440
8441 had been seen. Used to create compiler-generated global
8442 variables. */
8443
8444 static tree
8445 declare_global_var (tree name, tree type)
8446 {
8447 tree decl;
8448
8449 push_to_top_level ();
8450 decl = build_decl (input_location, VAR_DECL, name, type);
8451 TREE_PUBLIC (decl) = 1;
8452 DECL_EXTERNAL (decl) = 1;
8453 DECL_ARTIFICIAL (decl) = 1;
8454 DECL_CONTEXT (decl) = FROB_CONTEXT (global_namespace);
8455 /* If the user has explicitly declared this variable (perhaps
8456 because the code we are compiling is part of a low-level runtime
8457 library), then it is possible that our declaration will be merged
8458 with theirs by pushdecl. */
8459 decl = pushdecl (decl);
8460 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
8461 pop_from_top_level ();
8462
8463 return decl;
8464 }
8465
8466 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
8467 if "__cxa_atexit" is not being used) corresponding to the function
8468 to be called when the program exits. */
8469
8470 static tree
8471 get_atexit_fn_ptr_type (void)
8472 {
8473 tree fn_type;
8474
8475 if (!atexit_fn_ptr_type_node)
8476 {
8477 tree arg_type;
8478 if (flag_use_cxa_atexit
8479 && !targetm.cxx.use_atexit_for_cxa_atexit ())
8480 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
8481 arg_type = ptr_type_node;
8482 else
8483 /* The parameter to "atexit" is "void (*)(void)". */
8484 arg_type = NULL_TREE;
8485
8486 fn_type = build_function_type_list (void_type_node,
8487 arg_type, NULL_TREE);
8488 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
8489 }
8490
8491 return atexit_fn_ptr_type_node;
8492 }
8493
8494 /* Returns a pointer to the `atexit' function. Note that if
8495 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8496 `__cxa_atexit' function specified in the IA64 C++ ABI. */
8497
8498 static tree
8499 get_atexit_node (void)
8500 {
8501 tree atexit_fndecl;
8502 tree fn_type;
8503 tree fn_ptr_type;
8504 const char *name;
8505 bool use_aeabi_atexit;
8506
8507 if (atexit_node)
8508 return atexit_node;
8509
8510 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
8511 {
8512 /* The declaration for `__cxa_atexit' is:
8513
8514 int __cxa_atexit (void (*)(void *), void *, void *)
8515
8516 We build up the argument types and then the function type
8517 itself. */
8518 tree argtype0, argtype1, argtype2;
8519
8520 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
8521 /* First, build the pointer-to-function type for the first
8522 argument. */
8523 fn_ptr_type = get_atexit_fn_ptr_type ();
8524 /* Then, build the rest of the argument types. */
8525 argtype2 = ptr_type_node;
8526 if (use_aeabi_atexit)
8527 {
8528 argtype1 = fn_ptr_type;
8529 argtype0 = ptr_type_node;
8530 }
8531 else
8532 {
8533 argtype1 = ptr_type_node;
8534 argtype0 = fn_ptr_type;
8535 }
8536 /* And the final __cxa_atexit type. */
8537 fn_type = build_function_type_list (integer_type_node,
8538 argtype0, argtype1, argtype2,
8539 NULL_TREE);
8540 if (use_aeabi_atexit)
8541 name = "__aeabi_atexit";
8542 else
8543 name = "__cxa_atexit";
8544 }
8545 else
8546 {
8547 /* The declaration for `atexit' is:
8548
8549 int atexit (void (*)());
8550
8551 We build up the argument types and then the function type
8552 itself. */
8553 fn_ptr_type = get_atexit_fn_ptr_type ();
8554 /* Build the final atexit type. */
8555 fn_type = build_function_type_list (integer_type_node,
8556 fn_ptr_type, NULL_TREE);
8557 name = "atexit";
8558 }
8559
8560 /* Now, build the function declaration. */
8561 push_lang_context (lang_name_c);
8562 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
8563 mark_used (atexit_fndecl);
8564 pop_lang_context ();
8565 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
8566
8567 return atexit_node;
8568 }
8569
8570 /* Like get_atexit_node, but for thread-local cleanups. */
8571
8572 static tree
8573 get_thread_atexit_node (void)
8574 {
8575 /* The declaration for `__cxa_thread_atexit' is:
8576
8577 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
8578 tree fn_type = build_function_type_list (integer_type_node,
8579 get_atexit_fn_ptr_type (),
8580 ptr_type_node, ptr_type_node,
8581 NULL_TREE);
8582
8583 /* Now, build the function declaration. */
8584 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
8585 ECF_LEAF | ECF_NOTHROW);
8586 return decay_conversion (atexit_fndecl, tf_warning_or_error);
8587 }
8588
8589 /* Returns the __dso_handle VAR_DECL. */
8590
8591 static tree
8592 get_dso_handle_node (void)
8593 {
8594 if (dso_handle_node)
8595 return dso_handle_node;
8596
8597 /* Declare the variable. */
8598 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8599 ptr_type_node);
8600
8601 #ifdef HAVE_GAS_HIDDEN
8602 if (dso_handle_node != error_mark_node)
8603 {
8604 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
8605 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
8606 }
8607 #endif
8608
8609 return dso_handle_node;
8610 }
8611
8612 /* Begin a new function with internal linkage whose job will be simply
8613 to destroy some particular variable. */
8614
8615 static GTY(()) int start_cleanup_cnt;
8616
8617 static tree
8618 start_cleanup_fn (void)
8619 {
8620 char name[32];
8621 tree fntype;
8622 tree fndecl;
8623 bool use_cxa_atexit = flag_use_cxa_atexit
8624 && !targetm.cxx.use_atexit_for_cxa_atexit ();
8625
8626 push_to_top_level ();
8627
8628 /* No need to mangle this. */
8629 push_lang_context (lang_name_c);
8630
8631 /* Build the name of the function. */
8632 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8633 /* Build the function declaration. */
8634 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
8635 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8636 /* It's a function with internal linkage, generated by the
8637 compiler. */
8638 TREE_PUBLIC (fndecl) = 0;
8639 DECL_ARTIFICIAL (fndecl) = 1;
8640 /* Make the function `inline' so that it is only emitted if it is
8641 actually needed. It is unlikely that it will be inlined, since
8642 it is only called via a function pointer, but we avoid unnecessary
8643 emissions this way. */
8644 DECL_DECLARED_INLINE_P (fndecl) = 1;
8645 DECL_INTERFACE_KNOWN (fndecl) = 1;
8646 /* Build the parameter. */
8647 if (use_cxa_atexit)
8648 {
8649 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node);
8650 TREE_USED (parmdecl) = 1;
8651 DECL_READ_P (parmdecl) = 1;
8652 DECL_ARGUMENTS (fndecl) = parmdecl;
8653 }
8654
8655 pushdecl (fndecl);
8656 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
8657
8658 pop_lang_context ();
8659
8660 return current_function_decl;
8661 }
8662
8663 /* Finish the cleanup function begun by start_cleanup_fn. */
8664
8665 static void
8666 end_cleanup_fn (void)
8667 {
8668 expand_or_defer_fn (finish_function (/*inline_p=*/false));
8669
8670 pop_from_top_level ();
8671 }
8672
8673 /* Generate code to handle the destruction of DECL, an object with
8674 static storage duration. */
8675
8676 tree
8677 register_dtor_fn (tree decl)
8678 {
8679 tree cleanup;
8680 tree addr;
8681 tree compound_stmt;
8682 tree fcall;
8683 tree type;
8684 bool ob_parm, dso_parm, use_dtor;
8685 tree arg0, arg1, arg2;
8686 tree atex_node;
8687
8688 type = TREE_TYPE (decl);
8689 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8690 return void_node;
8691
8692 if (decl_maybe_constant_destruction (decl, type)
8693 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
8694 {
8695 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8696 return void_node;
8697 }
8698
8699 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
8700 "__aeabi_atexit"), and DECL is a class object, we can just pass the
8701 destructor to "__cxa_atexit"; we don't have to build a temporary
8702 function to do the cleanup. */
8703 dso_parm = (flag_use_cxa_atexit
8704 && !targetm.cxx.use_atexit_for_cxa_atexit ());
8705 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
8706 use_dtor = ob_parm && CLASS_TYPE_P (type);
8707 if (use_dtor)
8708 {
8709 cleanup = get_class_binding (type, complete_dtor_identifier);
8710
8711 /* Make sure it is accessible. */
8712 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
8713 tf_warning_or_error);
8714 }
8715 else
8716 {
8717 /* Call build_cleanup before we enter the anonymous function so
8718 that any access checks will be done relative to the current
8719 scope, rather than the scope of the anonymous function. */
8720 build_cleanup (decl);
8721
8722 /* Now start the function. */
8723 cleanup = start_cleanup_fn ();
8724
8725 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
8726 to the original function, rather than the anonymous one. That
8727 will make the back end think that nested functions are in use,
8728 which causes confusion. */
8729 push_deferring_access_checks (dk_no_check);
8730 fcall = build_cleanup (decl);
8731 pop_deferring_access_checks ();
8732
8733 /* Create the body of the anonymous function. */
8734 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
8735 finish_expr_stmt (fcall);
8736 finish_compound_stmt (compound_stmt);
8737 end_cleanup_fn ();
8738 }
8739
8740 /* Call atexit with the cleanup function. */
8741 mark_used (cleanup);
8742 cleanup = build_address (cleanup);
8743
8744 if (CP_DECL_THREAD_LOCAL_P (decl))
8745 atex_node = get_thread_atexit_node ();
8746 else
8747 atex_node = get_atexit_node ();
8748
8749 if (use_dtor)
8750 {
8751 /* We must convert CLEANUP to the type that "__cxa_atexit"
8752 expects. */
8753 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
8754 /* "__cxa_atexit" will pass the address of DECL to the
8755 cleanup function. */
8756 mark_used (decl);
8757 addr = build_address (decl);
8758 /* The declared type of the parameter to "__cxa_atexit" is
8759 "void *". For plain "T*", we could just let the
8760 machinery in cp_build_function_call convert it -- but if the
8761 type is "cv-qualified T *", then we need to convert it
8762 before passing it in, to avoid spurious errors. */
8763 addr = build_nop (ptr_type_node, addr);
8764 }
8765 else
8766 /* Since the cleanup functions we build ignore the address
8767 they're given, there's no reason to pass the actual address
8768 in, and, in general, it's cheaper to pass NULL than any
8769 other value. */
8770 addr = null_pointer_node;
8771
8772 if (dso_parm)
8773 arg2 = cp_build_addr_expr (get_dso_handle_node (),
8774 tf_warning_or_error);
8775 else if (ob_parm)
8776 /* Just pass NULL to the dso handle parm if we don't actually
8777 have a DSO handle on this target. */
8778 arg2 = null_pointer_node;
8779 else
8780 arg2 = NULL_TREE;
8781
8782 if (ob_parm)
8783 {
8784 if (!CP_DECL_THREAD_LOCAL_P (decl)
8785 && targetm.cxx.use_aeabi_atexit ())
8786 {
8787 arg1 = cleanup;
8788 arg0 = addr;
8789 }
8790 else
8791 {
8792 arg1 = addr;
8793 arg0 = cleanup;
8794 }
8795 }
8796 else
8797 {
8798 arg0 = cleanup;
8799 arg1 = NULL_TREE;
8800 }
8801 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
8802 arg0, arg1, arg2, NULL_TREE);
8803 }
8804
8805 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
8806 is its initializer. Generate code to handle the construction
8807 and destruction of DECL. */
8808
8809 static void
8810 expand_static_init (tree decl, tree init)
8811 {
8812 gcc_assert (VAR_P (decl));
8813 gcc_assert (TREE_STATIC (decl));
8814
8815 /* Some variables require no dynamic initialization. */
8816 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl)))
8817 {
8818 /* Make sure the destructor is callable. */
8819 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
8820 if (!init)
8821 return;
8822 }
8823
8824 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
8825 && !DECL_FUNCTION_SCOPE_P (decl))
8826 {
8827 location_t dloc = DECL_SOURCE_LOCATION (decl);
8828 if (init)
8829 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8830 "needs dynamic initialization", decl);
8831 else
8832 error_at (dloc, "non-local variable %qD declared %<__thread%> "
8833 "has a non-trivial destructor", decl);
8834 static bool informed;
8835 if (!informed)
8836 {
8837 inform (dloc, "C++11 %<thread_local%> allows dynamic "
8838 "initialization and destruction");
8839 informed = true;
8840 }
8841 return;
8842 }
8843
8844 if (DECL_FUNCTION_SCOPE_P (decl))
8845 {
8846 /* Emit code to perform this initialization but once. */
8847 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
8848 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
8849 tree guard, guard_addr;
8850 tree flag, begin;
8851 /* We don't need thread-safety code for thread-local vars. */
8852 bool thread_guard = (flag_threadsafe_statics
8853 && !CP_DECL_THREAD_LOCAL_P (decl));
8854
8855 /* Emit code to perform this initialization but once. This code
8856 looks like:
8857
8858 static <type> guard;
8859 if (!__atomic_load (guard.first_byte)) {
8860 if (__cxa_guard_acquire (&guard)) {
8861 bool flag = false;
8862 try {
8863 // Do initialization.
8864 flag = true; __cxa_guard_release (&guard);
8865 // Register variable for destruction at end of program.
8866 } catch {
8867 if (!flag) __cxa_guard_abort (&guard);
8868 }
8869 }
8870 }
8871
8872 Note that the `flag' variable is only set to 1 *after* the
8873 initialization is complete. This ensures that an exception,
8874 thrown during the construction, will cause the variable to
8875 reinitialized when we pass through this code again, as per:
8876
8877 [stmt.dcl]
8878
8879 If the initialization exits by throwing an exception, the
8880 initialization is not complete, so it will be tried again
8881 the next time control enters the declaration.
8882
8883 This process should be thread-safe, too; multiple threads
8884 should not be able to initialize the variable more than
8885 once. */
8886
8887 /* Create the guard variable. */
8888 guard = get_guard (decl);
8889
8890 /* Begin the conditional initialization. */
8891 if_stmt = begin_if_stmt ();
8892
8893 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
8894 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8895
8896 if (thread_guard)
8897 {
8898 tree vfntype = NULL_TREE;
8899 tree acquire_name, release_name, abort_name;
8900 tree acquire_fn, release_fn, abort_fn;
8901 guard_addr = build_address (guard);
8902
8903 acquire_name = get_identifier ("__cxa_guard_acquire");
8904 release_name = get_identifier ("__cxa_guard_release");
8905 abort_name = get_identifier ("__cxa_guard_abort");
8906 acquire_fn = get_global_binding (acquire_name);
8907 release_fn = get_global_binding (release_name);
8908 abort_fn = get_global_binding (abort_name);
8909 if (!acquire_fn)
8910 acquire_fn = push_library_fn
8911 (acquire_name, build_function_type_list (integer_type_node,
8912 TREE_TYPE (guard_addr),
8913 NULL_TREE),
8914 NULL_TREE, ECF_NOTHROW);
8915 if (!release_fn || !abort_fn)
8916 vfntype = build_function_type_list (void_type_node,
8917 TREE_TYPE (guard_addr),
8918 NULL_TREE);
8919 if (!release_fn)
8920 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
8921 ECF_NOTHROW);
8922 if (!abort_fn)
8923 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
8924 ECF_NOTHROW | ECF_LEAF);
8925
8926 inner_if_stmt = begin_if_stmt ();
8927 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
8928 inner_if_stmt);
8929
8930 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
8931 begin = get_target_expr (boolean_false_node);
8932 flag = TARGET_EXPR_SLOT (begin);
8933
8934 TARGET_EXPR_CLEANUP (begin)
8935 = build3 (COND_EXPR, void_type_node, flag,
8936 void_node,
8937 build_call_n (abort_fn, 1, guard_addr));
8938 CLEANUP_EH_ONLY (begin) = 1;
8939
8940 /* Do the initialization itself. */
8941 init = add_stmt_to_compound (begin, init);
8942 init = add_stmt_to_compound
8943 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
8944 init = add_stmt_to_compound
8945 (init, build_call_n (release_fn, 1, guard_addr));
8946 }
8947 else
8948 init = add_stmt_to_compound (init, set_guard (guard));
8949
8950 /* Use atexit to register a function for destroying this static
8951 variable. */
8952 init = add_stmt_to_compound (init, register_dtor_fn (decl));
8953
8954 finish_expr_stmt (init);
8955
8956 if (thread_guard)
8957 {
8958 finish_compound_stmt (inner_then_clause);
8959 finish_then_clause (inner_if_stmt);
8960 finish_if_stmt (inner_if_stmt);
8961 }
8962
8963 finish_compound_stmt (then_clause);
8964 finish_then_clause (if_stmt);
8965 finish_if_stmt (if_stmt);
8966 }
8967 else if (CP_DECL_THREAD_LOCAL_P (decl))
8968 tls_aggregates = tree_cons (init, decl, tls_aggregates);
8969 else
8970 static_aggregates = tree_cons (init, decl, static_aggregates);
8971 }
8972
8973 \f
8974 /* Make TYPE a complete type based on INITIAL_VALUE.
8975 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8976 2 if there was no information (in which case assume 0 if DO_DEFAULT),
8977 3 if the initializer list is empty (in pedantic mode). */
8978
8979 int
8980 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
8981 {
8982 int failure;
8983 tree type, elt_type;
8984
8985 /* Don't get confused by a CONSTRUCTOR for some other type. */
8986 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
8987 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
8988 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
8989 return 1;
8990
8991 if (initial_value)
8992 {
8993 unsigned HOST_WIDE_INT i;
8994 tree value;
8995
8996 /* An array of character type can be initialized from a
8997 brace-enclosed string constant.
8998
8999 FIXME: this code is duplicated from reshape_init. Probably
9000 we should just call reshape_init here? */
9001 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
9002 && TREE_CODE (initial_value) == CONSTRUCTOR
9003 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
9004 {
9005 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
9006 tree value = (*v)[0].value;
9007 STRIP_ANY_LOCATION_WRAPPER (value);
9008
9009 if (TREE_CODE (value) == STRING_CST
9010 && v->length () == 1)
9011 initial_value = value;
9012 }
9013
9014 /* If any of the elements are parameter packs, we can't actually
9015 complete this type now because the array size is dependent. */
9016 if (TREE_CODE (initial_value) == CONSTRUCTOR)
9017 {
9018 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
9019 i, value)
9020 {
9021 if (PACK_EXPANSION_P (value))
9022 return 0;
9023 }
9024 }
9025 }
9026
9027 failure = complete_array_type (ptype, initial_value, do_default);
9028
9029 /* We can create the array before the element type is complete, which
9030 means that we didn't have these two bits set in the original type
9031 either. In completing the type, we are expected to propagate these
9032 bits. See also complete_type which does the same thing for arrays
9033 of fixed size. */
9034 type = *ptype;
9035 if (type != error_mark_node && TYPE_DOMAIN (type))
9036 {
9037 elt_type = TREE_TYPE (type);
9038 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
9039 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
9040 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
9041 }
9042
9043 return failure;
9044 }
9045
9046 /* As above, but either give an error or reject zero-size arrays, depending
9047 on COMPLAIN. */
9048
9049 int
9050 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
9051 bool do_default, tsubst_flags_t complain)
9052 {
9053 int failure;
9054 bool sfinae = !(complain & tf_error);
9055 /* In SFINAE context we can't be lenient about zero-size arrays. */
9056 if (sfinae)
9057 ++pedantic;
9058 failure = cp_complete_array_type (ptype, initial_value, do_default);
9059 if (sfinae)
9060 --pedantic;
9061 if (failure)
9062 {
9063 if (sfinae)
9064 /* Not an error. */;
9065 else if (failure == 1)
9066 error ("initializer fails to determine size of %qT", *ptype);
9067 else if (failure == 2)
9068 {
9069 if (do_default)
9070 error ("array size missing in %qT", *ptype);
9071 }
9072 else if (failure == 3)
9073 error ("zero-size array %qT", *ptype);
9074 *ptype = error_mark_node;
9075 }
9076 return failure;
9077 }
9078 \f
9079 /* Return zero if something is declared to be a member of type
9080 CTYPE when in the context of CUR_TYPE. STRING is the error
9081 message to print in that case. Otherwise, quietly return 1. */
9082
9083 static int
9084 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
9085 {
9086 if (ctype && ctype != cur_type)
9087 {
9088 if (flags == DTOR_FLAG)
9089 error ("destructor for alien class %qT cannot be a member", ctype);
9090 else
9091 error ("constructor for alien class %qT cannot be a member", ctype);
9092 return 0;
9093 }
9094 return 1;
9095 }
9096 \f
9097 /* Subroutine of `grokdeclarator'. */
9098
9099 /* Generate errors possibly applicable for a given set of specifiers.
9100 This is for ARM $7.1.2. */
9101
9102 static void
9103 bad_specifiers (tree object,
9104 enum bad_spec_place type,
9105 int virtualp,
9106 int quals,
9107 int inlinep,
9108 int friendp,
9109 int raises,
9110 const location_t* locations)
9111 {
9112 switch (type)
9113 {
9114 case BSP_VAR:
9115 if (virtualp)
9116 error_at (locations[ds_virtual],
9117 "%qD declared as a %<virtual%> variable", object);
9118 if (quals)
9119 error ("%<const%> and %<volatile%> function specifiers on "
9120 "%qD invalid in variable declaration", object);
9121 break;
9122 case BSP_PARM:
9123 if (virtualp)
9124 error_at (locations[ds_virtual],
9125 "%qD declared as a %<virtual%> parameter", object);
9126 if (inlinep)
9127 error_at (locations[ds_inline],
9128 "%qD declared as an %<inline%> parameter", object);
9129 if (quals)
9130 error ("%<const%> and %<volatile%> function specifiers on "
9131 "%qD invalid in parameter declaration", object);
9132 break;
9133 case BSP_TYPE:
9134 if (virtualp)
9135 error_at (locations[ds_virtual],
9136 "%qD declared as a %<virtual%> type", object);
9137 if (inlinep)
9138 error_at (locations[ds_inline],
9139 "%qD declared as an %<inline%> type", object);
9140 if (quals)
9141 error ("%<const%> and %<volatile%> function specifiers on "
9142 "%qD invalid in type declaration", object);
9143 break;
9144 case BSP_FIELD:
9145 if (virtualp)
9146 error_at (locations[ds_virtual],
9147 "%qD declared as a %<virtual%> field", object);
9148 if (inlinep)
9149 error_at (locations[ds_inline],
9150 "%qD declared as an %<inline%> field", object);
9151 if (quals)
9152 error ("%<const%> and %<volatile%> function specifiers on "
9153 "%qD invalid in field declaration", object);
9154 break;
9155 default:
9156 gcc_unreachable();
9157 }
9158 if (friendp)
9159 error ("%q+D declared as a friend", object);
9160 if (raises
9161 && !flag_noexcept_type
9162 && (TREE_CODE (object) == TYPE_DECL
9163 || (!TYPE_PTRFN_P (TREE_TYPE (object))
9164 && !TYPE_REFFN_P (TREE_TYPE (object))
9165 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9166 error ("%q+D declared with an exception specification", object);
9167 }
9168
9169 /* DECL is a member function or static data member and is presently
9170 being defined. Check that the definition is taking place in a
9171 valid namespace. */
9172
9173 static void
9174 check_class_member_definition_namespace (tree decl)
9175 {
9176 /* These checks only apply to member functions and static data
9177 members. */
9178 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
9179 /* We check for problems with specializations in pt.c in
9180 check_specialization_namespace, where we can issue better
9181 diagnostics. */
9182 if (processing_specialization)
9183 return;
9184 /* We check this in check_explicit_instantiation_namespace. */
9185 if (processing_explicit_instantiation)
9186 return;
9187 /* [class.mfct]
9188
9189 A member function definition that appears outside of the
9190 class definition shall appear in a namespace scope enclosing
9191 the class definition.
9192
9193 [class.static.data]
9194
9195 The definition for a static data member shall appear in a
9196 namespace scope enclosing the member's class definition. */
9197 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
9198 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
9199 decl, DECL_CONTEXT (decl));
9200 }
9201
9202 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the
9203 METHOD_TYPE for a non-static member function; QUALS are the
9204 cv-qualifiers that apply to the function. */
9205
9206 tree
9207 build_this_parm (tree fn, tree type, cp_cv_quals quals)
9208 {
9209 tree this_type;
9210 tree qual_type;
9211 tree parm;
9212 cp_cv_quals this_quals;
9213
9214 if (CLASS_TYPE_P (type))
9215 {
9216 this_type
9217 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
9218 this_type = build_pointer_type (this_type);
9219 }
9220 else
9221 this_type = type_of_this_parm (type);
9222 /* The `this' parameter is implicitly `const'; it cannot be
9223 assigned to. */
9224 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
9225 qual_type = cp_build_qualified_type (this_type, this_quals);
9226 parm = build_artificial_parm (fn, this_identifier, qual_type);
9227 cp_apply_type_quals_to_decl (this_quals, parm);
9228 return parm;
9229 }
9230
9231 /* DECL is a static member function. Complain if it was declared
9232 with function-cv-quals. */
9233
9234 static void
9235 check_static_quals (tree decl, cp_cv_quals quals)
9236 {
9237 if (quals != TYPE_UNQUALIFIED)
9238 error ("static member function %q#D declared with type qualifiers",
9239 decl);
9240 }
9241
9242 // Check that FN takes no arguments and returns bool.
9243 static void
9244 check_concept_fn (tree fn)
9245 {
9246 // A constraint is nullary.
9247 if (DECL_ARGUMENTS (fn))
9248 error_at (DECL_SOURCE_LOCATION (fn),
9249 "concept %q#D declared with function parameters", fn);
9250
9251 // The declared return type of the concept shall be bool, and
9252 // it shall not be deduced from it definition.
9253 tree type = TREE_TYPE (TREE_TYPE (fn));
9254 if (is_auto (type))
9255 error_at (DECL_SOURCE_LOCATION (fn),
9256 "concept %q#D declared with a deduced return type", fn);
9257 else if (type != boolean_type_node)
9258 error_at (DECL_SOURCE_LOCATION (fn),
9259 "concept %q#D with non-%<bool%> return type %qT", fn, type);
9260 }
9261
9262 /* Helper function. Replace the temporary this parameter injected
9263 during cp_finish_omp_declare_simd with the real this parameter. */
9264
9265 static tree
9266 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
9267 {
9268 tree this_parm = (tree) data;
9269 if (TREE_CODE (*tp) == PARM_DECL
9270 && DECL_NAME (*tp) == this_identifier
9271 && *tp != this_parm)
9272 *tp = this_parm;
9273 else if (TYPE_P (*tp))
9274 *walk_subtrees = 0;
9275 return NULL_TREE;
9276 }
9277
9278 /* CTYPE is class type, or null if non-class.
9279 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9280 or METHOD_TYPE.
9281 DECLARATOR is the function's name.
9282 PARMS is a chain of PARM_DECLs for the function.
9283 VIRTUALP is truthvalue of whether the function is virtual or not.
9284 FLAGS are to be passed through to `grokclassfn'.
9285 QUALS are qualifiers indicating whether the function is `const'
9286 or `volatile'.
9287 RAISES is a list of exceptions that this function can raise.
9288 CHECK is 1 if we must find this method in CTYPE, 0 if we should
9289 not look, and -1 if we should not call `grokclassfn' at all.
9290
9291 SFK is the kind of special function (if any) for the new function.
9292
9293 Returns `NULL_TREE' if something goes wrong, after issuing
9294 applicable error messages. */
9295
9296 static tree
9297 grokfndecl (tree ctype,
9298 tree type,
9299 tree declarator,
9300 tree parms,
9301 tree orig_declarator,
9302 const cp_decl_specifier_seq *declspecs,
9303 tree decl_reqs,
9304 int virtualp,
9305 enum overload_flags flags,
9306 cp_cv_quals quals,
9307 cp_ref_qualifier rqual,
9308 tree raises,
9309 int check,
9310 int friendp,
9311 int publicp,
9312 int inlinep,
9313 bool deletedp,
9314 special_function_kind sfk,
9315 bool funcdef_flag,
9316 bool late_return_type_p,
9317 int template_count,
9318 tree in_namespace,
9319 tree* attrlist,
9320 location_t location)
9321 {
9322 tree decl;
9323 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9324 tree t;
9325
9326 if (location == UNKNOWN_LOCATION)
9327 location = input_location;
9328
9329 /* Was the concept specifier present? */
9330 bool concept_p = inlinep & 4;
9331
9332 /* Concept declarations must have a corresponding definition. */
9333 if (concept_p && !funcdef_flag)
9334 {
9335 error_at (location, "concept %qD has no definition", declarator);
9336 return NULL_TREE;
9337 }
9338
9339 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p);
9340
9341 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type);
9342
9343 /* Set the constraints on the declaration. */
9344 if (flag_concepts)
9345 {
9346 tree tmpl_reqs = NULL_TREE;
9347 tree ctx = friendp ? current_class_type : ctype;
9348 bool memtmpl = (processing_template_decl > template_class_depth (ctx));
9349 if (memtmpl)
9350 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
9351 tree ci = build_constraints (tmpl_reqs, decl_reqs);
9352 if (concept_p && ci)
9353 {
9354 error_at (location, "a function concept cannot be constrained");
9355 ci = NULL_TREE;
9356 }
9357 /* C++20 CA378: Remove non-templated constrained functions. */
9358 if (ci && !flag_concepts_ts
9359 && (!processing_template_decl
9360 || (friendp && !memtmpl && !funcdef_flag)))
9361 {
9362 error_at (location, "constraints on a non-templated function");
9363 ci = NULL_TREE;
9364 }
9365 set_constraints (decl, ci);
9366 }
9367
9368 if (TREE_CODE (type) == METHOD_TYPE)
9369 {
9370 tree parm = build_this_parm (decl, type, quals);
9371 DECL_CHAIN (parm) = parms;
9372 parms = parm;
9373
9374 /* Allocate space to hold the vptr bit if needed. */
9375 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY);
9376 }
9377
9378 DECL_ARGUMENTS (decl) = parms;
9379 for (t = parms; t; t = DECL_CHAIN (t))
9380 DECL_CONTEXT (t) = decl;
9381
9382 /* Propagate volatile out from type to decl. */
9383 if (TYPE_VOLATILE (type))
9384 TREE_THIS_VOLATILE (decl) = 1;
9385
9386 /* Setup decl according to sfk. */
9387 switch (sfk)
9388 {
9389 case sfk_constructor:
9390 case sfk_copy_constructor:
9391 case sfk_move_constructor:
9392 DECL_CXX_CONSTRUCTOR_P (decl) = 1;
9393 DECL_NAME (decl) = ctor_identifier;
9394 break;
9395 case sfk_destructor:
9396 DECL_CXX_DESTRUCTOR_P (decl) = 1;
9397 DECL_NAME (decl) = dtor_identifier;
9398 break;
9399 default:
9400 break;
9401 }
9402
9403 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9404 {
9405 if (funcdef_flag)
9406 error_at (location,
9407 "defining explicit specialization %qD in friend declaration",
9408 orig_declarator);
9409 else
9410 {
9411 tree fns = TREE_OPERAND (orig_declarator, 0);
9412 tree args = TREE_OPERAND (orig_declarator, 1);
9413
9414 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9415 {
9416 /* Something like `template <class T> friend void f<T>()'. */
9417 error_at (location,
9418 "invalid use of template-id %qD in declaration "
9419 "of primary template",
9420 orig_declarator);
9421 return NULL_TREE;
9422 }
9423
9424
9425 /* A friend declaration of the form friend void f<>(). Record
9426 the information in the TEMPLATE_ID_EXPR. */
9427 SET_DECL_IMPLICIT_INSTANTIATION (decl);
9428
9429 gcc_assert (identifier_p (fns) || OVL_P (fns));
9430 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
9431
9432 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9433 if (TREE_PURPOSE (t)
9434 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE)
9435 {
9436 error_at (defparse_location (TREE_PURPOSE (t)),
9437 "default arguments are not allowed in declaration "
9438 "of friend template specialization %qD",
9439 decl);
9440 return NULL_TREE;
9441 }
9442
9443 if (inlinep & 1)
9444 {
9445 error_at (declspecs->locations[ds_inline],
9446 "%<inline%> is not allowed in declaration of friend "
9447 "template specialization %qD",
9448 decl);
9449 return NULL_TREE;
9450 }
9451 }
9452 }
9453
9454 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument
9455 expression, that declaration shall be a definition..." */
9456 if (friendp && !funcdef_flag)
9457 {
9458 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
9459 t && t != void_list_node; t = TREE_CHAIN (t))
9460 if (TREE_PURPOSE (t))
9461 {
9462 permerror (DECL_SOURCE_LOCATION (decl),
9463 "friend declaration of %qD specifies default "
9464 "arguments and isn%'t a definition", decl);
9465 break;
9466 }
9467 }
9468
9469 /* FIXME: For now. */
9470 if (virtualp && (inlinep & 8) != 0)
9471 {
9472 sorry_at (DECL_SOURCE_LOCATION (decl),
9473 "%<virtual%> %<consteval%> method %qD not supported yet",
9474 decl);
9475 inlinep &= ~8;
9476 }
9477
9478 /* If this decl has namespace scope, set that up. */
9479 if (in_namespace)
9480 set_decl_namespace (decl, in_namespace, friendp);
9481 else if (!ctype)
9482 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
9483
9484 /* `main' and builtins have implicit 'C' linkage. */
9485 if (ctype == NULL_TREE
9486 && DECL_FILE_SCOPE_P (decl)
9487 && current_lang_name == lang_name_cplusplus
9488 && (MAIN_NAME_P (declarator)
9489 || (IDENTIFIER_LENGTH (declarator) > 10
9490 && IDENTIFIER_POINTER (declarator)[0] == '_'
9491 && IDENTIFIER_POINTER (declarator)[1] == '_'
9492 && strncmp (IDENTIFIER_POINTER (declarator)+2,
9493 "builtin_", 8) == 0)
9494 || (targetcm.cxx_implicit_extern_c
9495 && (targetcm.cxx_implicit_extern_c
9496 (IDENTIFIER_POINTER (declarator))))))
9497 SET_DECL_LANGUAGE (decl, lang_c);
9498
9499 /* Should probably propagate const out from type to decl I bet (mrs). */
9500 if (staticp)
9501 {
9502 DECL_STATIC_FUNCTION_P (decl) = 1;
9503 DECL_CONTEXT (decl) = ctype;
9504 }
9505
9506 if (deletedp)
9507 DECL_DELETED_FN (decl) = 1;
9508
9509 if (ctype)
9510 {
9511 DECL_CONTEXT (decl) = ctype;
9512 if (funcdef_flag)
9513 check_class_member_definition_namespace (decl);
9514 }
9515
9516 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9517 {
9518 if (PROCESSING_REAL_TEMPLATE_DECL_P())
9519 error_at (location, "cannot declare %<::main%> to be a template");
9520 if (inlinep & 1)
9521 error_at (declspecs->locations[ds_inline],
9522 "cannot declare %<::main%> to be inline");
9523 if (inlinep & 2)
9524 error_at (declspecs->locations[ds_constexpr],
9525 "cannot declare %<::main%> to be %qs", "constexpr");
9526 if (inlinep & 8)
9527 error_at (declspecs->locations[ds_consteval],
9528 "cannot declare %<::main%> to be %qs", "consteval");
9529 if (!publicp)
9530 error_at (location, "cannot declare %<::main%> to be static");
9531 inlinep = 0;
9532 publicp = 1;
9533 }
9534
9535 /* Members of anonymous types and local classes have no linkage; make
9536 them internal. If a typedef is made later, this will be changed. */
9537 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
9538 || decl_function_context (TYPE_MAIN_DECL (ctype))))
9539 publicp = 0;
9540
9541 if (publicp && cxx_dialect == cxx98)
9542 {
9543 /* [basic.link]: A name with no linkage (notably, the name of a class
9544 or enumeration declared in a local scope) shall not be used to
9545 declare an entity with linkage.
9546
9547 DR 757 relaxes this restriction for C++0x. */
9548 no_linkage_error (decl);
9549 }
9550
9551 TREE_PUBLIC (decl) = publicp;
9552 if (! publicp)
9553 {
9554 DECL_INTERFACE_KNOWN (decl) = 1;
9555 DECL_NOT_REALLY_EXTERN (decl) = 1;
9556 }
9557
9558 /* If the declaration was declared inline, mark it as such. */
9559 if (inlinep)
9560 {
9561 DECL_DECLARED_INLINE_P (decl) = 1;
9562 if (publicp)
9563 DECL_COMDAT (decl) = 1;
9564 }
9565 if (inlinep & 2)
9566 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9567 else if (inlinep & 8)
9568 {
9569 DECL_DECLARED_CONSTEXPR_P (decl) = true;
9570 SET_DECL_IMMEDIATE_FUNCTION_P (decl);
9571 }
9572
9573 // If the concept declaration specifier was found, check
9574 // that the declaration satisfies the necessary requirements.
9575 if (concept_p)
9576 {
9577 DECL_DECLARED_CONCEPT_P (decl) = true;
9578 check_concept_fn (decl);
9579 }
9580
9581 DECL_EXTERNAL (decl) = 1;
9582 if (TREE_CODE (type) == FUNCTION_TYPE)
9583 {
9584 if (quals || rqual)
9585 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
9586 TYPE_UNQUALIFIED,
9587 REF_QUAL_NONE);
9588
9589 if (quals)
9590 {
9591 error (ctype
9592 ? G_("static member function %qD cannot have cv-qualifier")
9593 : G_("non-member function %qD cannot have cv-qualifier"),
9594 decl);
9595 quals = TYPE_UNQUALIFIED;
9596 }
9597
9598 if (rqual)
9599 {
9600 error (ctype
9601 ? G_("static member function %qD cannot have ref-qualifier")
9602 : G_("non-member function %qD cannot have ref-qualifier"),
9603 decl);
9604 rqual = REF_QUAL_NONE;
9605 }
9606 }
9607
9608 if (deduction_guide_p (decl))
9609 {
9610 if (!DECL_NAMESPACE_SCOPE_P (decl))
9611 {
9612 error_at (location, "deduction guide %qD must be declared at "
9613 "namespace scope", decl);
9614 return NULL_TREE;
9615 }
9616 if (funcdef_flag)
9617 error_at (location,
9618 "deduction guide %qD must not have a function body", decl);
9619 }
9620 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl))
9621 && !grok_op_properties (decl, /*complain=*/true))
9622 return NULL_TREE;
9623 else if (UDLIT_OPER_P (DECL_NAME (decl)))
9624 {
9625 bool long_long_unsigned_p;
9626 bool long_double_p;
9627 const char *suffix = NULL;
9628 /* [over.literal]/6: Literal operators shall not have C linkage. */
9629 if (DECL_LANGUAGE (decl) == lang_c)
9630 {
9631 error_at (location, "literal operator with C linkage");
9632 maybe_show_extern_c_location ();
9633 return NULL_TREE;
9634 }
9635
9636 if (DECL_NAMESPACE_SCOPE_P (decl))
9637 {
9638 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
9639 &long_double_p))
9640 {
9641 error_at (location, "%qD has invalid argument list", decl);
9642 return NULL_TREE;
9643 }
9644
9645 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
9646 if (long_long_unsigned_p)
9647 {
9648 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
9649 warning_at (location, 0, "integer suffix %qs"
9650 " shadowed by implementation", suffix);
9651 }
9652 else if (long_double_p)
9653 {
9654 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
9655 warning_at (location, 0, "floating-point suffix %qs"
9656 " shadowed by implementation", suffix);
9657 }
9658 /* 17.6.3.3.5 */
9659 if (suffix[0] != '_'
9660 && !current_function_decl && !(friendp && !funcdef_flag))
9661 warning_at (location, OPT_Wliteral_suffix,
9662 "literal operator suffixes not preceded by %<_%>"
9663 " are reserved for future standardization");
9664 }
9665 else
9666 {
9667 error_at (location, "%qD must be a non-member function", decl);
9668 return NULL_TREE;
9669 }
9670 }
9671
9672 if (funcdef_flag)
9673 /* Make the init_value nonzero so pushdecl knows this is not
9674 tentative. error_mark_node is replaced later with the BLOCK. */
9675 DECL_INITIAL (decl) = error_mark_node;
9676
9677 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9678 TREE_NOTHROW (decl) = 1;
9679
9680 if (flag_openmp || flag_openmp_simd)
9681 {
9682 /* Adjust "omp declare simd" attributes. */
9683 tree ods = lookup_attribute ("omp declare simd", *attrlist);
9684 if (ods)
9685 {
9686 tree attr;
9687 for (attr = ods; attr;
9688 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
9689 {
9690 if (TREE_CODE (type) == METHOD_TYPE)
9691 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
9692 DECL_ARGUMENTS (decl), NULL);
9693 if (TREE_VALUE (attr) != NULL_TREE)
9694 {
9695 tree cl = TREE_VALUE (TREE_VALUE (attr));
9696 cl = c_omp_declare_simd_clauses_to_numbers
9697 (DECL_ARGUMENTS (decl), cl);
9698 if (cl)
9699 TREE_VALUE (TREE_VALUE (attr)) = cl;
9700 else
9701 TREE_VALUE (attr) = NULL_TREE;
9702 }
9703 }
9704 }
9705 }
9706
9707 /* Caller will do the rest of this. */
9708 if (check < 0)
9709 return decl;
9710
9711 if (ctype != NULL_TREE)
9712 grokclassfn (ctype, decl, flags);
9713
9714 /* 12.4/3 */
9715 if (cxx_dialect >= cxx11
9716 && DECL_DESTRUCTOR_P (decl)
9717 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
9718 && !processing_template_decl)
9719 deduce_noexcept_on_destructor (decl);
9720
9721 decl = check_explicit_specialization (orig_declarator, decl,
9722 template_count,
9723 2 * funcdef_flag +
9724 4 * (friendp != 0) +
9725 8 * concept_p,
9726 *attrlist);
9727 if (decl == error_mark_node)
9728 return NULL_TREE;
9729
9730 if (DECL_STATIC_FUNCTION_P (decl))
9731 check_static_quals (decl, quals);
9732
9733 if (attrlist)
9734 {
9735 cplus_decl_attributes (&decl, *attrlist, 0);
9736 *attrlist = NULL_TREE;
9737 }
9738
9739 /* Check main's type after attributes have been applied. */
9740 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9741 {
9742 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9743 integer_type_node))
9744 {
9745 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
9746 tree newtype;
9747 error_at (declspecs->locations[ds_type_spec],
9748 "%<::main%> must return %<int%>");
9749 newtype = build_function_type (integer_type_node, oldtypeargs);
9750 TREE_TYPE (decl) = newtype;
9751 }
9752 if (warn_main)
9753 check_main_parameter_types (decl);
9754 }
9755
9756 if (ctype != NULL_TREE && check)
9757 {
9758 tree old_decl = check_classfn (ctype, decl,
9759 (processing_template_decl
9760 > template_class_depth (ctype))
9761 ? current_template_parms
9762 : NULL_TREE);
9763
9764 if (old_decl == error_mark_node)
9765 return NULL_TREE;
9766
9767 if (old_decl)
9768 {
9769 tree ok;
9770 tree pushed_scope;
9771
9772 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
9773 /* Because grokfndecl is always supposed to return a
9774 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9775 here. We depend on our callers to figure out that its
9776 really a template that's being returned. */
9777 old_decl = DECL_TEMPLATE_RESULT (old_decl);
9778
9779 if (DECL_STATIC_FUNCTION_P (old_decl)
9780 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9781 {
9782 /* Remove the `this' parm added by grokclassfn. */
9783 revert_static_member_fn (decl);
9784 check_static_quals (decl, quals);
9785 }
9786 if (DECL_ARTIFICIAL (old_decl))
9787 {
9788 error ("definition of implicitly-declared %qD", old_decl);
9789 return NULL_TREE;
9790 }
9791 else if (DECL_DEFAULTED_FN (old_decl))
9792 {
9793 error ("definition of explicitly-defaulted %q+D", decl);
9794 inform (DECL_SOURCE_LOCATION (old_decl),
9795 "%q#D explicitly defaulted here", old_decl);
9796 return NULL_TREE;
9797 }
9798
9799 /* Since we've smashed OLD_DECL to its
9800 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
9801 if (TREE_CODE (decl) == TEMPLATE_DECL)
9802 decl = DECL_TEMPLATE_RESULT (decl);
9803
9804 /* Attempt to merge the declarations. This can fail, in
9805 the case of some invalid specialization declarations. */
9806 pushed_scope = push_scope (ctype);
9807 ok = duplicate_decls (decl, old_decl, friendp);
9808 if (pushed_scope)
9809 pop_scope (pushed_scope);
9810 if (!ok)
9811 {
9812 error ("no %q#D member function declared in class %qT",
9813 decl, ctype);
9814 return NULL_TREE;
9815 }
9816 if (ok == error_mark_node)
9817 return NULL_TREE;
9818 return old_decl;
9819 }
9820 }
9821
9822 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9823 return NULL_TREE;
9824
9825 if (ctype == NULL_TREE || check)
9826 return decl;
9827
9828 if (virtualp)
9829 DECL_VIRTUAL_P (decl) = 1;
9830
9831 return decl;
9832 }
9833
9834 /* decl is a FUNCTION_DECL.
9835 specifiers are the parsed virt-specifiers.
9836
9837 Set flags to reflect the virt-specifiers.
9838
9839 Returns decl. */
9840
9841 static tree
9842 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
9843 {
9844 if (decl == NULL_TREE)
9845 return decl;
9846 if (specifiers & VIRT_SPEC_OVERRIDE)
9847 DECL_OVERRIDE_P (decl) = 1;
9848 if (specifiers & VIRT_SPEC_FINAL)
9849 DECL_FINAL_P (decl) = 1;
9850 return decl;
9851 }
9852
9853 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
9854 the linkage that DECL will receive in the object file. */
9855
9856 static void
9857 set_linkage_for_static_data_member (tree decl)
9858 {
9859 /* A static data member always has static storage duration and
9860 external linkage. Note that static data members are forbidden in
9861 local classes -- the only situation in which a class has
9862 non-external linkage. */
9863 TREE_PUBLIC (decl) = 1;
9864 TREE_STATIC (decl) = 1;
9865 /* For non-template classes, static data members are always put
9866 out in exactly those files where they are defined, just as
9867 with ordinary namespace-scope variables. */
9868 if (!processing_template_decl)
9869 DECL_INTERFACE_KNOWN (decl) = 1;
9870 }
9871
9872 /* Create a VAR_DECL named NAME with the indicated TYPE.
9873
9874 If SCOPE is non-NULL, it is the class type or namespace containing
9875 the variable. If SCOPE is NULL, the variable should is created in
9876 the innermost enclosing scope. */
9877
9878 static tree
9879 grokvardecl (tree type,
9880 tree name,
9881 tree orig_declarator,
9882 const cp_decl_specifier_seq *declspecs,
9883 int initialized,
9884 int type_quals,
9885 int inlinep,
9886 bool conceptp,
9887 int template_count,
9888 tree scope,
9889 location_t location)
9890 {
9891 tree decl;
9892 tree explicit_scope;
9893
9894 gcc_assert (!name || identifier_p (name));
9895
9896 bool constp = (type_quals & TYPE_QUAL_CONST) != 0;
9897 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0;
9898
9899 /* Compute the scope in which to place the variable, but remember
9900 whether or not that scope was explicitly specified by the user. */
9901 explicit_scope = scope;
9902 if (!scope)
9903 {
9904 /* An explicit "extern" specifier indicates a namespace-scope
9905 variable. */
9906 if (declspecs->storage_class == sc_extern)
9907 scope = current_decl_namespace ();
9908 else if (!at_function_scope_p ())
9909 scope = current_scope ();
9910 }
9911
9912 if (scope
9913 && (/* If the variable is a namespace-scope variable declared in a
9914 template, we need DECL_LANG_SPECIFIC. */
9915 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9916 /* Similarly for namespace-scope variables with language linkage
9917 other than C++. */
9918 || (TREE_CODE (scope) == NAMESPACE_DECL
9919 && current_lang_name != lang_name_cplusplus)
9920 /* Similarly for static data members. */
9921 || TYPE_P (scope)
9922 /* Similarly for explicit specializations. */
9923 || (orig_declarator
9924 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
9925 decl = build_lang_decl_loc (location, VAR_DECL, name, type);
9926 else
9927 decl = build_decl (location, VAR_DECL, name, type);
9928
9929 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
9930 set_decl_namespace (decl, explicit_scope, 0);
9931 else
9932 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
9933
9934 if (declspecs->storage_class == sc_extern)
9935 {
9936 DECL_THIS_EXTERN (decl) = 1;
9937 DECL_EXTERNAL (decl) = !initialized;
9938 }
9939
9940 if (DECL_CLASS_SCOPE_P (decl))
9941 {
9942 set_linkage_for_static_data_member (decl);
9943 /* This function is only called with out-of-class definitions. */
9944 DECL_EXTERNAL (decl) = 0;
9945 check_class_member_definition_namespace (decl);
9946 }
9947 /* At top level, either `static' or no s.c. makes a definition
9948 (perhaps tentative), and absence of `static' makes it public. */
9949 else if (toplevel_bindings_p ())
9950 {
9951 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
9952 && (DECL_THIS_EXTERN (decl)
9953 || ! constp
9954 || volatilep
9955 || inlinep));
9956 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9957 }
9958 /* Not at top level, only `static' makes a static definition. */
9959 else
9960 {
9961 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
9962 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9963 }
9964
9965 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
9966 {
9967 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
9968 {
9969 CP_DECL_THREAD_LOCAL_P (decl) = true;
9970 if (!processing_template_decl)
9971 set_decl_tls_model (decl, decl_default_tls_model (decl));
9972 }
9973 if (declspecs->gnu_thread_keyword_p)
9974 SET_DECL_GNU_TLS_P (decl);
9975 }
9976
9977 /* If the type of the decl has no linkage, make sure that we'll
9978 notice that in mark_used. */
9979 if (cxx_dialect > cxx98
9980 && decl_linkage (decl) != lk_none
9981 && DECL_LANG_SPECIFIC (decl) == NULL
9982 && !DECL_EXTERN_C_P (decl)
9983 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
9984 retrofit_lang_decl (decl);
9985
9986 if (TREE_PUBLIC (decl))
9987 {
9988 /* [basic.link]: A name with no linkage (notably, the name of a class
9989 or enumeration declared in a local scope) shall not be used to
9990 declare an entity with linkage.
9991
9992 DR 757 relaxes this restriction for C++0x. */
9993 if (cxx_dialect < cxx11)
9994 no_linkage_error (decl);
9995 }
9996 else
9997 DECL_INTERFACE_KNOWN (decl) = 1;
9998
9999 if (DECL_NAME (decl)
10000 && MAIN_NAME_P (DECL_NAME (decl))
10001 && scope == global_namespace)
10002 error_at (DECL_SOURCE_LOCATION (decl),
10003 "cannot declare %<::main%> to be a global variable");
10004
10005 /* Check that the variable can be safely declared as a concept.
10006 Note that this also forbids explicit specializations. */
10007 if (conceptp)
10008 {
10009 if (!processing_template_decl)
10010 {
10011 error_at (declspecs->locations[ds_concept],
10012 "a non-template variable cannot be %<concept%>");
10013 return NULL_TREE;
10014 }
10015 else
10016 DECL_DECLARED_CONCEPT_P (decl) = true;
10017 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
10018 error_at (declspecs->locations[ds_type_spec],
10019 "concept must have type %<bool%>");
10020 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
10021 {
10022 error_at (location, "a variable concept cannot be constrained");
10023 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
10024 }
10025 }
10026 else if (flag_concepts
10027 && processing_template_decl > template_class_depth (scope))
10028 {
10029 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
10030 tree ci = build_constraints (reqs, NULL_TREE);
10031
10032 set_constraints (decl, ci);
10033 }
10034
10035 // Handle explicit specializations and instantiations of variable templates.
10036 if (orig_declarator)
10037 decl = check_explicit_specialization (orig_declarator, decl,
10038 template_count, conceptp * 8);
10039
10040 return decl != error_mark_node ? decl : NULL_TREE;
10041 }
10042
10043 /* Create and return a canonical pointer to member function type, for
10044 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
10045
10046 tree
10047 build_ptrmemfunc_type (tree type)
10048 {
10049 tree field, fields;
10050 tree t;
10051
10052 if (type == error_mark_node)
10053 return type;
10054
10055 /* Make sure that we always have the unqualified pointer-to-member
10056 type first. */
10057 if (cp_cv_quals quals = cp_type_quals (type))
10058 {
10059 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10060 return cp_build_qualified_type (unqual, quals);
10061 }
10062
10063 /* If a canonical type already exists for this type, use it. We use
10064 this method instead of type_hash_canon, because it only does a
10065 simple equality check on the list of field members. */
10066
10067 t = TYPE_PTRMEMFUNC_TYPE (type);
10068 if (t)
10069 return t;
10070
10071 t = make_node (RECORD_TYPE);
10072
10073 /* Let the front end know this is a pointer to member function. */
10074 TYPE_PTRMEMFUNC_FLAG (t) = 1;
10075
10076 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
10077 DECL_NONADDRESSABLE_P (field) = 1;
10078 fields = field;
10079
10080 field = build_decl (input_location, FIELD_DECL, delta_identifier,
10081 delta_type_node);
10082 DECL_NONADDRESSABLE_P (field) = 1;
10083 DECL_CHAIN (field) = fields;
10084 fields = field;
10085
10086 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
10087
10088 /* Zap out the name so that the back end will give us the debugging
10089 information for this anonymous RECORD_TYPE. */
10090 TYPE_NAME (t) = NULL_TREE;
10091
10092 /* Cache this pointer-to-member type so that we can find it again
10093 later. */
10094 TYPE_PTRMEMFUNC_TYPE (type) = t;
10095
10096 if (TYPE_STRUCTURAL_EQUALITY_P (type))
10097 SET_TYPE_STRUCTURAL_EQUALITY (t);
10098 else if (TYPE_CANONICAL (type) != type)
10099 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
10100
10101 return t;
10102 }
10103
10104 /* Create and return a pointer to data member type. */
10105
10106 tree
10107 build_ptrmem_type (tree class_type, tree member_type)
10108 {
10109 if (TREE_CODE (member_type) == METHOD_TYPE)
10110 {
10111 cp_cv_quals quals = type_memfn_quals (member_type);
10112 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
10113 member_type = build_memfn_type (member_type, class_type, quals, rqual);
10114 return build_ptrmemfunc_type (build_pointer_type (member_type));
10115 }
10116 else
10117 {
10118 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
10119 return build_offset_type (class_type, member_type);
10120 }
10121 }
10122
10123 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10124 Check to see that the definition is valid. Issue appropriate error
10125 messages. */
10126
10127 static void
10128 check_static_variable_definition (tree decl, tree type)
10129 {
10130 /* Avoid redundant diagnostics on out-of-class definitions. */
10131 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
10132 ;
10133 /* Can't check yet if we don't know the type. */
10134 else if (dependent_type_p (type))
10135 ;
10136 /* If DECL is declared constexpr, we'll do the appropriate checks
10137 in check_initializer. Similarly for inline static data members. */
10138 else if (DECL_P (decl)
10139 && (DECL_DECLARED_CONSTEXPR_P (decl)
10140 || undeduced_auto_decl (decl)
10141 || DECL_VAR_DECLARED_INLINE_P (decl)))
10142 ;
10143 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10144 {
10145 if (!COMPLETE_TYPE_P (type))
10146 error_at (DECL_SOURCE_LOCATION (decl),
10147 "in-class initialization of static data member %q#D of "
10148 "incomplete type", decl);
10149 else if (literal_type_p (type))
10150 permerror (DECL_SOURCE_LOCATION (decl),
10151 "%<constexpr%> needed for in-class initialization of "
10152 "static data member %q#D of non-integral type", decl);
10153 else
10154 error_at (DECL_SOURCE_LOCATION (decl),
10155 "in-class initialization of static data member %q#D of "
10156 "non-literal type", decl);
10157 }
10158 /* Motion 10 at San Diego: If a static const integral data member is
10159 initialized with an integral constant expression, the initializer
10160 may appear either in the declaration (within the class), or in
10161 the definition, but not both. If it appears in the class, the
10162 member is a member constant. The file-scope definition is always
10163 required. */
10164 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10165 error_at (DECL_SOURCE_LOCATION (decl),
10166 "invalid in-class initialization of static data member "
10167 "of non-integral type %qT",
10168 type);
10169 else if (!CP_TYPE_CONST_P (type))
10170 error_at (DECL_SOURCE_LOCATION (decl),
10171 "ISO C++ forbids in-class initialization of non-const "
10172 "static member %qD",
10173 decl);
10174 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
10175 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10176 "ISO C++ forbids initialization of member constant "
10177 "%qD of non-integral type %qT", decl, type);
10178 }
10179
10180 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
10181 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
10182 expressions out into temporary variables so that walk_tree doesn't
10183 step into them (c++/15764). */
10184
10185 static tree
10186 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
10187 {
10188 hash_set<tree> *pset = (hash_set<tree> *)data;
10189 tree expr = *expr_p;
10190 if (TREE_CODE (expr) == SAVE_EXPR)
10191 {
10192 tree op = TREE_OPERAND (expr, 0);
10193 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
10194 if (TREE_SIDE_EFFECTS (op))
10195 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
10196 *walk_subtrees = 0;
10197 }
10198 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
10199 *walk_subtrees = 0;
10200 return NULL;
10201 }
10202
10203 /* Entry point for the above. */
10204
10205 static void
10206 stabilize_vla_size (tree size)
10207 {
10208 hash_set<tree> pset;
10209 /* Break out any function calls into temporary variables. */
10210 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
10211 }
10212
10213 /* Reduce a SIZEOF_EXPR to its value. */
10214
10215 tree
10216 fold_sizeof_expr (tree t)
10217 {
10218 tree r;
10219 if (SIZEOF_EXPR_TYPE_P (t))
10220 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10221 TREE_TYPE (TREE_OPERAND (t, 0)),
10222 SIZEOF_EXPR, false, false);
10223 else if (TYPE_P (TREE_OPERAND (t, 0)))
10224 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t),
10225 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10226 false, false);
10227 else
10228 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t),
10229 TREE_OPERAND (t, 0), SIZEOF_EXPR,
10230 false);
10231 if (r == error_mark_node)
10232 r = size_one_node;
10233 return r;
10234 }
10235
10236 /* Given the SIZE (i.e., number of elements) in an array, compute
10237 an appropriate index type for the array. If non-NULL, NAME is
10238 the name of the entity being declared. */
10239
10240 static tree
10241 compute_array_index_type_loc (location_t name_loc, tree name, tree size,
10242 tsubst_flags_t complain)
10243 {
10244 if (error_operand_p (size))
10245 return error_mark_node;
10246
10247 /* The type of the index being computed. */
10248 tree itype;
10249
10250 /* The original numeric size as seen in the source code before
10251 conversion to size_t. */
10252 tree origsize = size;
10253
10254 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location);
10255
10256 if (!type_dependent_expression_p (size))
10257 {
10258 origsize = size = mark_rvalue_use (size);
10259
10260 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
10261 && TREE_SIDE_EFFECTS (size))
10262 /* In C++98, we mark a non-constant array bound with a magic
10263 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
10264 else
10265 {
10266 size = instantiate_non_dependent_expr_sfinae (size, complain);
10267 size = build_converted_constant_expr (size_type_node, size, complain);
10268 /* Pedantically a constant expression is required here and so
10269 __builtin_is_constant_evaluated () should fold to true if it
10270 is successfully folded into a constant. */
10271 size = maybe_constant_value (size, NULL_TREE,
10272 /*manifestly_const_eval=*/true);
10273
10274 if (!TREE_CONSTANT (size))
10275 size = origsize;
10276 }
10277
10278 if (error_operand_p (size))
10279 return error_mark_node;
10280
10281 /* The array bound must be an integer type. */
10282 tree type = TREE_TYPE (size);
10283 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
10284 {
10285 if (!(complain & tf_error))
10286 return error_mark_node;
10287 if (name)
10288 error_at (loc, "size of array %qD has non-integral type %qT",
10289 name, type);
10290 else
10291 error_at (loc, "size of array has non-integral type %qT", type);
10292 size = integer_one_node;
10293 }
10294 }
10295
10296 /* A type is dependent if it is...an array type constructed from any
10297 dependent type or whose size is specified by a constant expression
10298 that is value-dependent. */
10299 /* We can only call value_dependent_expression_p on integral constant
10300 expressions; treat non-constant expressions as dependent, too. */
10301 if (processing_template_decl
10302 && (type_dependent_expression_p (size)
10303 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
10304 {
10305 /* We cannot do any checking for a SIZE that isn't known to be
10306 constant. Just build the index type and mark that it requires
10307 structural equality checks. */
10308 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
10309 size, size_one_node));
10310 TYPE_DEPENDENT_P (itype) = 1;
10311 TYPE_DEPENDENT_P_VALID (itype) = 1;
10312 SET_TYPE_STRUCTURAL_EQUALITY (itype);
10313 return itype;
10314 }
10315
10316 if (TREE_CODE (size) != INTEGER_CST)
10317 {
10318 tree folded = cp_fully_fold (size);
10319 if (TREE_CODE (folded) == INTEGER_CST)
10320 {
10321 if (name)
10322 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an "
10323 "integral constant-expression", name);
10324 else
10325 pedwarn (loc, OPT_Wpedantic,
10326 "size of array is not an integral constant-expression");
10327 }
10328 /* Use the folded result for VLAs, too; it will have resolved
10329 SIZEOF_EXPR. */
10330 size = folded;
10331 }
10332
10333 /* Normally, the array-bound will be a constant. */
10334 if (TREE_CODE (size) == INTEGER_CST)
10335 {
10336 /* The size to use in diagnostics that reflects the constant
10337 size used in the source, rather than SIZE massaged above. */
10338 tree diagsize = size;
10339
10340 /* If the original size before conversion to size_t was signed
10341 and negative, convert it to ssizetype to restore the sign. */
10342 if (!TYPE_UNSIGNED (TREE_TYPE (origsize))
10343 && TREE_CODE (size) == INTEGER_CST
10344 && tree_int_cst_sign_bit (size))
10345 {
10346 diagsize = fold_convert (ssizetype, size);
10347
10348 /* Clear the overflow bit that may have been set as a result
10349 of the conversion from the sizetype of the new size to
10350 ssizetype. */
10351 TREE_OVERFLOW (diagsize) = false;
10352 }
10353
10354 /* Verify that the array has a positive number of elements
10355 and issue the appropriate diagnostic if it doesn't. */
10356 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error)))
10357 {
10358 if (!(complain & tf_error))
10359 return error_mark_node;
10360 size = integer_one_node;
10361 }
10362 /* As an extension we allow zero-sized arrays. */
10363 else if (integer_zerop (size))
10364 {
10365 if (!(complain & tf_error))
10366 /* We must fail if performing argument deduction (as
10367 indicated by the state of complain), so that
10368 another substitution can be found. */
10369 return error_mark_node;
10370 else if (name)
10371 pedwarn (loc, OPT_Wpedantic,
10372 "ISO C++ forbids zero-size array %qD", name);
10373 else
10374 pedwarn (loc, OPT_Wpedantic,
10375 "ISO C++ forbids zero-size array");
10376 }
10377 }
10378 else if (TREE_CONSTANT (size)
10379 /* We don't allow VLAs at non-function scopes, or during
10380 tentative template substitution. */
10381 || !at_function_scope_p ()
10382 || !(complain & tf_error))
10383 {
10384 if (!(complain & tf_error))
10385 return error_mark_node;
10386 /* `(int) &fn' is not a valid array bound. */
10387 if (name)
10388 error_at (loc,
10389 "size of array %qD is not an integral constant-expression",
10390 name);
10391 else
10392 error_at (loc, "size of array is not an integral constant-expression");
10393 size = integer_one_node;
10394 }
10395 else if (pedantic && warn_vla != 0)
10396 {
10397 if (name)
10398 pedwarn (name_loc, OPT_Wvla,
10399 "ISO C++ forbids variable length array %qD", name);
10400 else
10401 pedwarn (input_location, OPT_Wvla,
10402 "ISO C++ forbids variable length array");
10403 }
10404 else if (warn_vla > 0)
10405 {
10406 if (name)
10407 warning_at (name_loc, OPT_Wvla,
10408 "variable length array %qD is used", name);
10409 else
10410 warning (OPT_Wvla,
10411 "variable length array is used");
10412 }
10413
10414 if (processing_template_decl && !TREE_CONSTANT (size))
10415 /* A variable sized array. */
10416 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
10417 else
10418 {
10419 /* Compute the index of the largest element in the array. It is
10420 one less than the number of elements in the array. We save
10421 and restore PROCESSING_TEMPLATE_DECL so that computations in
10422 cp_build_binary_op will be appropriately folded. */
10423 {
10424 processing_template_decl_sentinel s;
10425 itype = cp_build_binary_op (input_location,
10426 MINUS_EXPR,
10427 cp_convert (ssizetype, size, complain),
10428 cp_convert (ssizetype, integer_one_node,
10429 complain),
10430 complain);
10431 itype = maybe_constant_value (itype);
10432 }
10433
10434 if (!TREE_CONSTANT (itype))
10435 {
10436 /* A variable sized array. */
10437 itype = variable_size (itype);
10438
10439 stabilize_vla_size (itype);
10440
10441 if (sanitize_flags_p (SANITIZE_VLA)
10442 && current_function_decl != NULL_TREE)
10443 {
10444 /* We have to add 1 -- in the ubsan routine we generate
10445 LE_EXPR rather than LT_EXPR. */
10446 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
10447 build_one_cst (TREE_TYPE (itype)));
10448 t = ubsan_instrument_vla (input_location, t);
10449 finish_expr_stmt (t);
10450 }
10451 }
10452 /* Make sure that there was no overflow when creating to a signed
10453 index type. (For example, on a 32-bit machine, an array with
10454 size 2^32 - 1 is too big.) */
10455 else if (TREE_CODE (itype) == INTEGER_CST
10456 && TREE_OVERFLOW (itype))
10457 {
10458 if (!(complain & tf_error))
10459 return error_mark_node;
10460 error ("overflow in array dimension");
10461 TREE_OVERFLOW (itype) = 0;
10462 }
10463 }
10464
10465 /* Create and return the appropriate index type. */
10466 itype = build_index_type (itype);
10467
10468 /* If the index type were dependent, we would have returned early, so
10469 remember that it isn't. */
10470 TYPE_DEPENDENT_P (itype) = 0;
10471 TYPE_DEPENDENT_P_VALID (itype) = 1;
10472 return itype;
10473 }
10474
10475 tree
10476 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
10477 {
10478 return compute_array_index_type_loc (input_location, name, size, complain);
10479 }
10480
10481 /* Returns the scope (if any) in which the entity declared by
10482 DECLARATOR will be located. If the entity was declared with an
10483 unqualified name, NULL_TREE is returned. */
10484
10485 tree
10486 get_scope_of_declarator (const cp_declarator *declarator)
10487 {
10488 while (declarator && declarator->kind != cdk_id)
10489 declarator = declarator->declarator;
10490
10491 /* If the declarator-id is a SCOPE_REF, the scope in which the
10492 declaration occurs is the first operand. */
10493 if (declarator
10494 && declarator->u.id.qualifying_scope)
10495 return declarator->u.id.qualifying_scope;
10496
10497 /* Otherwise, the declarator is not a qualified name; the entity will
10498 be declared in the current scope. */
10499 return NULL_TREE;
10500 }
10501
10502 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
10503 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
10504 with this type. */
10505
10506 static tree
10507 create_array_type_for_decl (tree name, tree type, tree size, location_t loc)
10508 {
10509 tree itype = NULL_TREE;
10510
10511 /* If things have already gone awry, bail now. */
10512 if (type == error_mark_node || size == error_mark_node)
10513 return error_mark_node;
10514
10515 /* 8.3.4/1: If the type of the identifier of D contains the auto
10516 type-specifier, the program is ill-formed. */
10517 if (type_uses_auto (type))
10518 {
10519 if (name)
10520 error_at (loc, "%qD declared as array of %qT", name, type);
10521 else
10522 error ("creating array of %qT", type);
10523 return error_mark_node;
10524 }
10525
10526 /* If there are some types which cannot be array elements,
10527 issue an error-message and return. */
10528 switch (TREE_CODE (type))
10529 {
10530 case VOID_TYPE:
10531 if (name)
10532 error_at (loc, "declaration of %qD as array of void", name);
10533 else
10534 error ("creating array of void");
10535 return error_mark_node;
10536
10537 case FUNCTION_TYPE:
10538 if (name)
10539 error_at (loc, "declaration of %qD as array of functions", name);
10540 else
10541 error ("creating array of functions");
10542 return error_mark_node;
10543
10544 case REFERENCE_TYPE:
10545 if (name)
10546 error_at (loc, "declaration of %qD as array of references", name);
10547 else
10548 error ("creating array of references");
10549 return error_mark_node;
10550
10551 case METHOD_TYPE:
10552 if (name)
10553 error_at (loc, "declaration of %qD as array of function members",
10554 name);
10555 else
10556 error ("creating array of function members");
10557 return error_mark_node;
10558
10559 default:
10560 break;
10561 }
10562
10563 if (!verify_type_context (name ? loc : input_location,
10564 TCTX_ARRAY_ELEMENT, type))
10565 return error_mark_node;
10566
10567 /* [dcl.array]
10568
10569 The constant expressions that specify the bounds of the arrays
10570 can be omitted only for the first member of the sequence. */
10571 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10572 {
10573 if (name)
10574 error_at (loc, "declaration of %qD as multidimensional array must "
10575 "have bounds for all dimensions except the first",
10576 name);
10577 else
10578 error ("multidimensional array must have bounds for all "
10579 "dimensions except the first");
10580
10581 return error_mark_node;
10582 }
10583
10584 /* Figure out the index type for the array. */
10585 if (size)
10586 itype = compute_array_index_type_loc (loc, name, size,
10587 tf_warning_or_error);
10588
10589 /* [dcl.array]
10590 T is called the array element type; this type shall not be [...] an
10591 abstract class type. */
10592 abstract_virtuals_error (name, type);
10593
10594 return build_cplus_array_type (type, itype);
10595 }
10596
10597 /* Returns the smallest location that is not UNKNOWN_LOCATION. */
10598
10599 static location_t
10600 min_location (location_t loca, location_t locb)
10601 {
10602 if (loca == UNKNOWN_LOCATION
10603 || (locb != UNKNOWN_LOCATION
10604 && linemap_location_before_p (line_table, locb, loca)))
10605 return locb;
10606 return loca;
10607 }
10608
10609 /* Returns the smallest location != UNKNOWN_LOCATION among the
10610 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
10611 and LOCATIONS[ds_restrict]. */
10612
10613 static location_t
10614 smallest_type_quals_location (int type_quals, const location_t* locations)
10615 {
10616 location_t loc = UNKNOWN_LOCATION;
10617
10618 if (type_quals & TYPE_QUAL_CONST)
10619 loc = locations[ds_const];
10620
10621 if (type_quals & TYPE_QUAL_VOLATILE)
10622 loc = min_location (loc, locations[ds_volatile]);
10623
10624 if (type_quals & TYPE_QUAL_RESTRICT)
10625 loc = min_location (loc, locations[ds_restrict]);
10626
10627 return loc;
10628 }
10629
10630 /* Returns the smallest among the latter and locations[ds_type_spec]. */
10631
10632 static location_t
10633 smallest_type_location (int type_quals, const location_t* locations)
10634 {
10635 location_t loc = smallest_type_quals_location (type_quals, locations);
10636 return min_location (loc, locations[ds_type_spec]);
10637 }
10638
10639 static location_t
10640 smallest_type_location (const cp_decl_specifier_seq *declspecs)
10641 {
10642 int type_quals = get_type_quals (declspecs);
10643 return smallest_type_location (type_quals, declspecs->locations);
10644 }
10645
10646 /* Check that it's OK to declare a function with the indicated TYPE
10647 and TYPE_QUALS. SFK indicates the kind of special function (if any)
10648 that this function is. OPTYPE is the type given in a conversion
10649 operator declaration, or the class type for a constructor/destructor.
10650 Returns the actual return type of the function; that may be different
10651 than TYPE if an error occurs, or for certain special functions. */
10652
10653 static tree
10654 check_special_function_return_type (special_function_kind sfk,
10655 tree type,
10656 tree optype,
10657 int type_quals,
10658 const location_t* locations)
10659 {
10660 switch (sfk)
10661 {
10662 case sfk_constructor:
10663 if (type)
10664 error_at (smallest_type_location (type_quals, locations),
10665 "return type specification for constructor invalid");
10666 else if (type_quals != TYPE_UNQUALIFIED)
10667 error_at (smallest_type_quals_location (type_quals, locations),
10668 "qualifiers are not allowed on constructor declaration");
10669
10670 if (targetm.cxx.cdtor_returns_this ())
10671 type = build_pointer_type (optype);
10672 else
10673 type = void_type_node;
10674 break;
10675
10676 case sfk_destructor:
10677 if (type)
10678 error_at (smallest_type_location (type_quals, locations),
10679 "return type specification for destructor invalid");
10680 else if (type_quals != TYPE_UNQUALIFIED)
10681 error_at (smallest_type_quals_location (type_quals, locations),
10682 "qualifiers are not allowed on destructor declaration");
10683
10684 /* We can't use the proper return type here because we run into
10685 problems with ambiguous bases and covariant returns. */
10686 if (targetm.cxx.cdtor_returns_this ())
10687 type = build_pointer_type (void_type_node);
10688 else
10689 type = void_type_node;
10690 break;
10691
10692 case sfk_conversion:
10693 if (type)
10694 error_at (smallest_type_location (type_quals, locations),
10695 "return type specified for %<operator %T%>", optype);
10696 else if (type_quals != TYPE_UNQUALIFIED)
10697 error_at (smallest_type_quals_location (type_quals, locations),
10698 "qualifiers are not allowed on declaration of "
10699 "%<operator %T%>", optype);
10700
10701 type = optype;
10702 break;
10703
10704 case sfk_deduction_guide:
10705 if (type)
10706 error_at (smallest_type_location (type_quals, locations),
10707 "return type specified for deduction guide");
10708 else if (type_quals != TYPE_UNQUALIFIED)
10709 error_at (smallest_type_quals_location (type_quals, locations),
10710 "qualifiers are not allowed on declaration of "
10711 "deduction guide");
10712 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM)
10713 {
10714 error ("template template parameter %qT in declaration of "
10715 "deduction guide", optype);
10716 type = error_mark_node;
10717 }
10718 else
10719 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype));
10720 for (int i = 0; i < ds_last; ++i)
10721 if (i != ds_explicit && locations[i])
10722 error_at (locations[i],
10723 "%<decl-specifier%> in declaration of deduction guide");
10724 break;
10725
10726 default:
10727 gcc_unreachable ();
10728 }
10729
10730 return type;
10731 }
10732
10733 /* A variable or data member (whose unqualified name is IDENTIFIER)
10734 has been declared with the indicated TYPE. If the TYPE is not
10735 acceptable, issue an error message and return a type to use for
10736 error-recovery purposes. */
10737
10738 tree
10739 check_var_type (tree identifier, tree type, location_t loc)
10740 {
10741 if (VOID_TYPE_P (type))
10742 {
10743 if (!identifier)
10744 error_at (loc, "unnamed variable or field declared void");
10745 else if (identifier_p (identifier))
10746 {
10747 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier));
10748 error_at (loc, "variable or field %qE declared void",
10749 identifier);
10750 }
10751 else
10752 error_at (loc, "variable or field declared void");
10753 type = error_mark_node;
10754 }
10755
10756 return type;
10757 }
10758
10759 /* Handle declaring DECL as an inline variable. */
10760
10761 static void
10762 mark_inline_variable (tree decl, location_t loc)
10763 {
10764 bool inlinep = true;
10765 if (! toplevel_bindings_p ())
10766 {
10767 error_at (loc, "%<inline%> specifier invalid for variable "
10768 "%qD declared at block scope", decl);
10769 inlinep = false;
10770 }
10771 else if (cxx_dialect < cxx17)
10772 pedwarn (loc, 0, "inline variables are only available "
10773 "with %<-std=c++17%> or %<-std=gnu++17%>");
10774 if (inlinep)
10775 {
10776 retrofit_lang_decl (decl);
10777 SET_DECL_VAR_DECLARED_INLINE_P (decl);
10778 }
10779 }
10780
10781
10782 /* Assign a typedef-given name to a class or enumeration type declared
10783 as anonymous at first. This was split out of grokdeclarator
10784 because it is also used in libcc1. */
10785
10786 void
10787 name_unnamed_type (tree type, tree decl)
10788 {
10789 gcc_assert (TYPE_UNNAMED_P (type));
10790
10791 /* Replace the anonymous name with the real name everywhere. */
10792 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10793 if (IDENTIFIER_ANON_P (TYPE_IDENTIFIER (t)))
10794 /* We do not rename the debug info representing the unnamed
10795 tagged type because the standard says in [dcl.typedef] that
10796 the naming applies only for linkage purposes. */
10797 /*debug_hooks->set_name (t, decl);*/
10798 TYPE_NAME (t) = decl;
10799
10800 /* If this is a typedef within a template class, the nested
10801 type is a (non-primary) template. The name for the
10802 template needs updating as well. */
10803 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10804 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10805 = TYPE_IDENTIFIER (type);
10806
10807 /* Adjust linkage now that we aren't unnamed anymore. */
10808 reset_type_linkage (type);
10809
10810 /* FIXME remangle member functions; member functions of a
10811 type with external linkage have external linkage. */
10812
10813 /* Check that our job is done, and that it would fail if we
10814 attempted to do it again. */
10815 gcc_assert (!TYPE_UNNAMED_P (type));
10816 }
10817
10818 /* Given declspecs and a declarator (abstract or otherwise), determine
10819 the name and type of the object declared and construct a DECL node
10820 for it.
10821
10822 DECLSPECS points to the representation of declaration-specifier
10823 sequence that precedes declarator.
10824
10825 DECL_CONTEXT says which syntactic context this declaration is in:
10826 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10827 FUNCDEF for a function definition. Like NORMAL but a few different
10828 error messages in each case. Return value may be zero meaning
10829 this definition is too screwy to try to parse.
10830 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
10831 handle member functions (which have FIELD context).
10832 Return value may be zero meaning this definition is too screwy to
10833 try to parse.
10834 PARM for a parameter declaration (either within a function prototype
10835 or before a function body). Make a PARM_DECL, or return void_type_node.
10836 TPARM for a template parameter declaration.
10837 CATCHPARM for a parameter declaration before a catch clause.
10838 TYPENAME if for a typename (in a cast or sizeof).
10839 Don't make a DECL node; just return the ..._TYPE node.
10840 FIELD for a struct or union field; make a FIELD_DECL.
10841 BITFIELD for a field with specified width.
10842
10843 INITIALIZED is as for start_decl.
10844
10845 ATTRLIST is a pointer to the list of attributes, which may be NULL
10846 if there are none; *ATTRLIST may be modified if attributes from inside
10847 the declarator should be applied to the declaration.
10848
10849 When this function is called, scoping variables (such as
10850 CURRENT_CLASS_TYPE) should reflect the scope in which the
10851 declaration occurs, not the scope in which the new declaration will
10852 be placed. For example, on:
10853
10854 void S::f() { ... }
10855
10856 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
10857 should not be `S'.
10858
10859 Returns a DECL (if a declarator is present), a TYPE (if there is no
10860 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
10861 error occurs. */
10862
10863 tree
10864 grokdeclarator (const cp_declarator *declarator,
10865 cp_decl_specifier_seq *declspecs,
10866 enum decl_context decl_context,
10867 int initialized,
10868 tree* attrlist)
10869 {
10870 tree type = NULL_TREE;
10871 int longlong = 0;
10872 int explicit_intN = 0;
10873 int int_n_alt = 0;
10874 int virtualp, explicitp, friendp, inlinep, staticp;
10875 int explicit_int = 0;
10876 int explicit_char = 0;
10877 int defaulted_int = 0;
10878
10879 tree typedef_decl = NULL_TREE;
10880 const char *name = NULL;
10881 tree typedef_type = NULL_TREE;
10882 /* True if this declarator is a function definition. */
10883 bool funcdef_flag = false;
10884 cp_declarator_kind innermost_code = cdk_error;
10885 int bitfield = 0;
10886 #if 0
10887 /* See the code below that used this. */
10888 tree decl_attr = NULL_TREE;
10889 #endif
10890
10891 /* Keep track of what sort of function is being processed
10892 so that we can warn about default return values, or explicit
10893 return values which do not match prescribed defaults. */
10894 special_function_kind sfk = sfk_none;
10895
10896 tree dname = NULL_TREE;
10897 tree ctor_return_type = NULL_TREE;
10898 enum overload_flags flags = NO_SPECIAL;
10899 /* cv-qualifiers that apply to the declarator, for a declaration of
10900 a member function. */
10901 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
10902 /* virt-specifiers that apply to the declarator, for a declaration of
10903 a member function. */
10904 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
10905 /* ref-qualifier that applies to the declarator, for a declaration of
10906 a member function. */
10907 cp_ref_qualifier rqual = REF_QUAL_NONE;
10908 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
10909 int type_quals = get_type_quals (declspecs);
10910 tree raises = NULL_TREE;
10911 int template_count = 0;
10912 tree returned_attrs = NULL_TREE;
10913 tree parms = NULL_TREE;
10914 const cp_declarator *id_declarator;
10915 /* The unqualified name of the declarator; either an
10916 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
10917 tree unqualified_id;
10918 /* The class type, if any, in which this entity is located,
10919 or NULL_TREE if none. Note that this value may be different from
10920 the current class type; for example if an attempt is made to declare
10921 "A::f" inside "B", this value will be "A". */
10922 tree ctype = current_class_type;
10923 /* The NAMESPACE_DECL for the namespace in which this entity is
10924 located. If an unqualified name is used to declare the entity,
10925 this value will be NULL_TREE, even if the entity is located at
10926 namespace scope. */
10927 tree in_namespace = NULL_TREE;
10928 cp_storage_class storage_class;
10929 bool unsigned_p, signed_p, short_p, long_p, thread_p;
10930 bool type_was_error_mark_node = false;
10931 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false;
10932 bool template_type_arg = false;
10933 bool template_parm_flag = false;
10934 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
10935 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
10936 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit);
10937 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval);
10938 bool late_return_type_p = false;
10939 bool array_parameter_p = false;
10940 tree reqs = NULL_TREE;
10941
10942 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
10943 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
10944 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
10945 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
10946 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
10947 explicit_intN = declspecs->explicit_intN_p;
10948 int_n_alt = declspecs->int_n_alt;
10949 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
10950
10951 // Was concept_p specified? Note that ds_concept
10952 // implies ds_constexpr!
10953 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
10954 if (concept_p)
10955 constexpr_p = true;
10956
10957 if (decl_context == FUNCDEF)
10958 funcdef_flag = true, decl_context = NORMAL;
10959 else if (decl_context == MEMFUNCDEF)
10960 funcdef_flag = true, decl_context = FIELD;
10961 else if (decl_context == BITFIELD)
10962 bitfield = 1, decl_context = FIELD;
10963 else if (decl_context == TEMPLATE_TYPE_ARG)
10964 template_type_arg = true, decl_context = TYPENAME;
10965 else if (decl_context == TPARM)
10966 template_parm_flag = true, decl_context = PARM;
10967
10968 if (initialized > 1)
10969 funcdef_flag = true;
10970
10971 location_t typespec_loc = smallest_type_location (type_quals,
10972 declspecs->locations);
10973 if (typespec_loc == UNKNOWN_LOCATION)
10974 typespec_loc = input_location;
10975
10976 location_t id_loc = declarator ? declarator->id_loc : input_location;
10977 if (id_loc == UNKNOWN_LOCATION)
10978 id_loc = input_location;
10979
10980 /* Look inside a declarator for the name being declared
10981 and get it as a string, for an error message. */
10982 for (id_declarator = declarator;
10983 id_declarator;
10984 id_declarator = id_declarator->declarator)
10985 {
10986 if (id_declarator->kind != cdk_id)
10987 innermost_code = id_declarator->kind;
10988
10989 switch (id_declarator->kind)
10990 {
10991 case cdk_function:
10992 if (id_declarator->declarator
10993 && id_declarator->declarator->kind == cdk_id)
10994 {
10995 sfk = id_declarator->declarator->u.id.sfk;
10996 if (sfk == sfk_destructor)
10997 flags = DTOR_FLAG;
10998 }
10999 break;
11000
11001 case cdk_id:
11002 {
11003 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
11004 tree decl = id_declarator->u.id.unqualified_name;
11005 if (!decl)
11006 break;
11007 if (qualifying_scope)
11008 {
11009 if (check_for_bare_parameter_packs (qualifying_scope,
11010 id_declarator->id_loc))
11011 return error_mark_node;
11012 if (at_function_scope_p ())
11013 {
11014 /* [dcl.meaning]
11015
11016 A declarator-id shall not be qualified except
11017 for ...
11018
11019 None of the cases are permitted in block
11020 scope. */
11021 if (qualifying_scope == global_namespace)
11022 error ("invalid use of qualified-name %<::%D%>",
11023 decl);
11024 else if (TYPE_P (qualifying_scope))
11025 error ("invalid use of qualified-name %<%T::%D%>",
11026 qualifying_scope, decl);
11027 else
11028 error ("invalid use of qualified-name %<%D::%D%>",
11029 qualifying_scope, decl);
11030 return error_mark_node;
11031 }
11032 else if (TYPE_P (qualifying_scope))
11033 {
11034 ctype = qualifying_scope;
11035 if (!MAYBE_CLASS_TYPE_P (ctype))
11036 {
11037 error_at (id_declarator->id_loc,
11038 "%q#T is not a class or namespace", ctype);
11039 ctype = NULL_TREE;
11040 }
11041 else if (innermost_code != cdk_function
11042 && current_class_type
11043 && !uniquely_derived_from_p (ctype,
11044 current_class_type))
11045 {
11046 error_at (id_declarator->id_loc,
11047 "invalid use of qualified-name %<%T::%D%>",
11048 qualifying_scope, decl);
11049 return error_mark_node;
11050 }
11051 }
11052 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
11053 in_namespace = qualifying_scope;
11054 }
11055 switch (TREE_CODE (decl))
11056 {
11057 case BIT_NOT_EXPR:
11058 {
11059 if (innermost_code != cdk_function)
11060 {
11061 error_at (EXPR_LOCATION (decl),
11062 "declaration of %qE as non-function", decl);
11063 return error_mark_node;
11064 }
11065 else if (!qualifying_scope
11066 && !(current_class_type && at_class_scope_p ()))
11067 {
11068 error_at (EXPR_LOCATION (decl),
11069 "declaration of %qE as non-member", decl);
11070 return error_mark_node;
11071 }
11072
11073 tree type = TREE_OPERAND (decl, 0);
11074 if (TYPE_P (type))
11075 type = constructor_name (type);
11076 name = identifier_to_locale (IDENTIFIER_POINTER (type));
11077 dname = decl;
11078 }
11079 break;
11080
11081 case TEMPLATE_ID_EXPR:
11082 {
11083 tree fns = TREE_OPERAND (decl, 0);
11084
11085 dname = fns;
11086 if (!identifier_p (dname))
11087 dname = OVL_NAME (dname);
11088 }
11089 /* Fall through. */
11090
11091 case IDENTIFIER_NODE:
11092 if (identifier_p (decl))
11093 dname = decl;
11094
11095 if (IDENTIFIER_KEYWORD_P (dname))
11096 {
11097 error ("declarator-id missing; using reserved word %qD",
11098 dname);
11099 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11100 }
11101 else if (!IDENTIFIER_CONV_OP_P (dname))
11102 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11103 else
11104 {
11105 gcc_assert (flags == NO_SPECIAL);
11106 flags = TYPENAME_FLAG;
11107 sfk = sfk_conversion;
11108 tree glob = get_global_binding (dname);
11109 if (glob && TREE_CODE (glob) == TYPE_DECL)
11110 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
11111 else
11112 name = "<invalid operator>";
11113 }
11114 break;
11115
11116 default:
11117 gcc_unreachable ();
11118 }
11119 break;
11120 }
11121
11122 case cdk_array:
11123 case cdk_pointer:
11124 case cdk_reference:
11125 case cdk_ptrmem:
11126 break;
11127
11128 case cdk_decomp:
11129 name = "structured binding";
11130 break;
11131
11132 case cdk_error:
11133 return error_mark_node;
11134
11135 default:
11136 gcc_unreachable ();
11137 }
11138 if (id_declarator->kind == cdk_id)
11139 break;
11140 }
11141
11142 /* [dcl.fct.edf]
11143
11144 The declarator in a function-definition shall have the form
11145 D1 ( parameter-declaration-clause) ... */
11146 if (funcdef_flag && innermost_code != cdk_function)
11147 {
11148 error_at (id_loc, "function definition does not declare parameters");
11149 return error_mark_node;
11150 }
11151
11152 if (flags == TYPENAME_FLAG
11153 && innermost_code != cdk_function
11154 && ! (ctype && !declspecs->any_specifiers_p))
11155 {
11156 error_at (id_loc, "declaration of %qD as non-function", dname);
11157 return error_mark_node;
11158 }
11159
11160 if (dname && identifier_p (dname))
11161 {
11162 if (UDLIT_OPER_P (dname)
11163 && innermost_code != cdk_function)
11164 {
11165 error_at (id_loc, "declaration of %qD as non-function", dname);
11166 return error_mark_node;
11167 }
11168
11169 if (IDENTIFIER_ANY_OP_P (dname))
11170 {
11171 if (typedef_p)
11172 {
11173 error_at (id_loc, "declaration of %qD as %<typedef%>", dname);
11174 return error_mark_node;
11175 }
11176 else if (decl_context == PARM || decl_context == CATCHPARM)
11177 {
11178 error_at (id_loc, "declaration of %qD as parameter", dname);
11179 return error_mark_node;
11180 }
11181 }
11182 }
11183
11184 /* Anything declared one level down from the top level
11185 must be one of the parameters of a function
11186 (because the body is at least two levels down). */
11187
11188 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
11189 by not allowing C++ class definitions to specify their parameters
11190 with xdecls (must be spec.d in the parmlist).
11191
11192 Since we now wait to push a class scope until we are sure that
11193 we are in a legitimate method context, we must set oldcname
11194 explicitly (since current_class_name is not yet alive).
11195
11196 We also want to avoid calling this a PARM if it is in a namespace. */
11197
11198 if (decl_context == NORMAL && !toplevel_bindings_p ())
11199 {
11200 cp_binding_level *b = current_binding_level;
11201 current_binding_level = b->level_chain;
11202 if (current_binding_level != 0 && toplevel_bindings_p ())
11203 decl_context = PARM;
11204 current_binding_level = b;
11205 }
11206
11207 if (name == NULL)
11208 name = decl_context == PARM ? "parameter" : "type name";
11209
11210 if (consteval_p && constexpr_p)
11211 {
11212 error_at (declspecs->locations[ds_consteval],
11213 "both %qs and %qs specified", "constexpr", "consteval");
11214 return error_mark_node;
11215 }
11216
11217 if (concept_p && typedef_p)
11218 {
11219 error_at (declspecs->locations[ds_concept],
11220 "%qs cannot appear in a typedef declaration", "concept");
11221 return error_mark_node;
11222 }
11223
11224 if (constexpr_p && typedef_p)
11225 {
11226 error_at (declspecs->locations[ds_constexpr],
11227 "%qs cannot appear in a typedef declaration", "constexpr");
11228 return error_mark_node;
11229 }
11230
11231 if (consteval_p && typedef_p)
11232 {
11233 error_at (declspecs->locations[ds_consteval],
11234 "%qs cannot appear in a typedef declaration", "consteval");
11235 return error_mark_node;
11236 }
11237
11238 if (constinit_p && typedef_p)
11239 {
11240 error_at (declspecs->locations[ds_constinit],
11241 "%<constinit%> cannot appear in a typedef declaration");
11242 return error_mark_node;
11243 }
11244
11245 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit
11246 keywords shall appear in a decl-specifier-seq." */
11247 if (constinit_p && constexpr_p)
11248 {
11249 gcc_rich_location richloc (declspecs->locations[ds_constinit]);
11250 richloc.add_range (declspecs->locations[ds_constexpr]);
11251 error_at (&richloc,
11252 "can use at most one of the %<constinit%> and %<constexpr%> "
11253 "specifiers");
11254 return error_mark_node;
11255 }
11256
11257 /* If there were multiple types specified in the decl-specifier-seq,
11258 issue an error message. */
11259 if (declspecs->multiple_types_p)
11260 {
11261 error_at (typespec_loc,
11262 "two or more data types in declaration of %qs", name);
11263 return error_mark_node;
11264 }
11265
11266 if (declspecs->conflicting_specifiers_p)
11267 {
11268 error_at (min_location (declspecs->locations[ds_typedef],
11269 declspecs->locations[ds_storage_class]),
11270 "conflicting specifiers in declaration of %qs", name);
11271 return error_mark_node;
11272 }
11273
11274 /* Extract the basic type from the decl-specifier-seq. */
11275 type = declspecs->type;
11276 if (type == error_mark_node)
11277 {
11278 type = NULL_TREE;
11279 type_was_error_mark_node = true;
11280 }
11281 cp_warn_deprecated_use (type);
11282 if (type && TREE_CODE (type) == TYPE_DECL)
11283 {
11284 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type));
11285 typedef_decl = type;
11286 type = TREE_TYPE (typedef_decl);
11287 if (DECL_ARTIFICIAL (typedef_decl))
11288 cp_warn_deprecated_use (type);
11289 }
11290 /* No type at all: default to `int', and set DEFAULTED_INT
11291 because it was not a user-defined typedef. */
11292 if (type == NULL_TREE)
11293 {
11294 if (signed_p || unsigned_p || long_p || short_p)
11295 {
11296 /* These imply 'int'. */
11297 type = integer_type_node;
11298 defaulted_int = 1;
11299 }
11300 /* If we just have "complex", it is equivalent to "complex double". */
11301 else if (!longlong && !explicit_intN
11302 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
11303 {
11304 type = double_type_node;
11305 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
11306 "ISO C++ does not support plain %<complex%> meaning "
11307 "%<double complex%>");
11308 }
11309 }
11310 /* Gather flags. */
11311 explicit_int = declspecs->explicit_int_p;
11312 explicit_char = declspecs->explicit_char_p;
11313
11314 #if 0
11315 /* See the code below that used this. */
11316 if (typedef_decl)
11317 decl_attr = DECL_ATTRIBUTES (typedef_decl);
11318 #endif
11319 typedef_type = type;
11320
11321 if (sfk == sfk_conversion || sfk == sfk_deduction_guide)
11322 ctor_return_type = TREE_TYPE (dname);
11323 else
11324 ctor_return_type = ctype;
11325
11326 if (sfk != sfk_none)
11327 {
11328 type = check_special_function_return_type (sfk, type,
11329 ctor_return_type,
11330 type_quals,
11331 declspecs->locations);
11332 type_quals = TYPE_UNQUALIFIED;
11333 }
11334 else if (type == NULL_TREE)
11335 {
11336 int is_main;
11337
11338 explicit_int = -1;
11339
11340 /* We handle `main' specially here, because 'main () { }' is so
11341 common. With no options, it is allowed. With -Wreturn-type,
11342 it is a warning. It is only an error with -pedantic-errors. */
11343 is_main = (funcdef_flag
11344 && dname && identifier_p (dname)
11345 && MAIN_NAME_P (dname)
11346 && ctype == NULL_TREE
11347 && in_namespace == NULL_TREE
11348 && current_namespace == global_namespace);
11349
11350 if (type_was_error_mark_node)
11351 /* We've already issued an error, don't complain more. */;
11352 else if (in_system_header_at (id_loc) || flag_ms_extensions)
11353 /* Allow it, sigh. */;
11354 else if (! is_main)
11355 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type",
11356 name);
11357 else if (pedantic)
11358 pedwarn (id_loc, OPT_Wpedantic,
11359 "ISO C++ forbids declaration of %qs with no type", name);
11360 else
11361 warning_at (id_loc, OPT_Wreturn_type,
11362 "ISO C++ forbids declaration of %qs with no type", name);
11363
11364 if (type_was_error_mark_node && template_parm_flag)
11365 /* FIXME we should be able to propagate the error_mark_node as is
11366 for other contexts too. */
11367 type = error_mark_node;
11368 else
11369 type = integer_type_node;
11370 }
11371
11372 ctype = NULL_TREE;
11373
11374 if (explicit_intN)
11375 {
11376 if (! int_n_enabled_p[declspecs->int_n_idx])
11377 {
11378 error_at (declspecs->locations[ds_type_spec],
11379 "%<__int%d%> is not supported by this target",
11380 int_n_data[declspecs->int_n_idx].bitsize);
11381 explicit_intN = false;
11382 }
11383 /* Don't pedwarn if the alternate "__intN__" form has been used instead
11384 of "__intN". */
11385 else if (!int_n_alt && pedantic)
11386 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic,
11387 "ISO C++ does not support %<__int%d%> for %qs",
11388 int_n_data[declspecs->int_n_idx].bitsize, name);
11389 }
11390
11391 /* Now process the modifiers that were specified
11392 and check for invalid combinations. */
11393
11394 /* Long double is a special combination. */
11395 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
11396 {
11397 long_p = false;
11398 type = cp_build_qualified_type (long_double_type_node,
11399 cp_type_quals (type));
11400 }
11401
11402 /* Check all other uses of type modifiers. */
11403
11404 if (unsigned_p || signed_p || long_p || short_p)
11405 {
11406 location_t loc;
11407 const char *key;
11408 if (unsigned_p)
11409 {
11410 key = "unsigned";
11411 loc = declspecs->locations[ds_unsigned];
11412 }
11413 else if (signed_p)
11414 {
11415 key = "signed";
11416 loc = declspecs->locations[ds_signed];
11417 }
11418 else if (longlong)
11419 {
11420 key = "long long";
11421 loc = declspecs->locations[ds_long_long];
11422 }
11423 else if (long_p)
11424 {
11425 key = "long";
11426 loc = declspecs->locations[ds_long];
11427 }
11428 else /* if (short_p) */
11429 {
11430 key = "short";
11431 loc = declspecs->locations[ds_short];
11432 }
11433
11434 int ok = 0;
11435
11436 if (signed_p && unsigned_p)
11437 {
11438 gcc_rich_location richloc (declspecs->locations[ds_signed]);
11439 richloc.add_range (declspecs->locations[ds_unsigned]);
11440 error_at (&richloc,
11441 "%<signed%> and %<unsigned%> specified together");
11442 }
11443 else if (long_p && short_p)
11444 {
11445 gcc_rich_location richloc (declspecs->locations[ds_long]);
11446 richloc.add_range (declspecs->locations[ds_short]);
11447 error_at (&richloc, "%<long%> and %<short%> specified together");
11448 }
11449 else if (TREE_CODE (type) != INTEGER_TYPE
11450 || type == char8_type_node
11451 || type == char16_type_node
11452 || type == char32_type_node
11453 || ((long_p || short_p)
11454 && (explicit_char || explicit_intN)))
11455 error_at (loc, "%qs specified with %qT", key, type);
11456 else if (!explicit_int && !defaulted_int
11457 && !explicit_char && !explicit_intN)
11458 {
11459 if (typedef_decl)
11460 {
11461 pedwarn (loc, OPT_Wpedantic, "%qs specified with %qT",
11462 key, type);
11463 ok = !flag_pedantic_errors;
11464 }
11465 else if (declspecs->decltype_p)
11466 error_at (loc, "%qs specified with %<decltype%>", key);
11467 else
11468 error_at (loc, "%qs specified with %<typeof%>", key);
11469 }
11470 else
11471 ok = 1;
11472
11473 /* Discard the type modifiers if they are invalid. */
11474 if (! ok)
11475 {
11476 unsigned_p = false;
11477 signed_p = false;
11478 long_p = false;
11479 short_p = false;
11480 longlong = 0;
11481 }
11482 }
11483
11484 /* Decide whether an integer type is signed or not.
11485 Optionally treat bitfields as signed by default. */
11486 if (unsigned_p
11487 /* [class.bit]
11488
11489 It is implementation-defined whether a plain (neither
11490 explicitly signed or unsigned) char, short, int, or long
11491 bit-field is signed or unsigned.
11492
11493 Naturally, we extend this to long long as well. Note that
11494 this does not include wchar_t. */
11495 || (bitfield && !flag_signed_bitfields
11496 && !signed_p
11497 /* A typedef for plain `int' without `signed' can be
11498 controlled just like plain `int', but a typedef for
11499 `signed int' cannot be so controlled. */
11500 && !(typedef_decl
11501 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11502 && TREE_CODE (type) == INTEGER_TYPE
11503 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11504 {
11505 if (explicit_intN)
11506 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
11507 else if (longlong)
11508 type = long_long_unsigned_type_node;
11509 else if (long_p)
11510 type = long_unsigned_type_node;
11511 else if (short_p)
11512 type = short_unsigned_type_node;
11513 else if (type == char_type_node)
11514 type = unsigned_char_type_node;
11515 else if (typedef_decl)
11516 type = unsigned_type_for (type);
11517 else
11518 type = unsigned_type_node;
11519 }
11520 else if (signed_p && type == char_type_node)
11521 type = signed_char_type_node;
11522 else if (explicit_intN)
11523 type = int_n_trees[declspecs->int_n_idx].signed_type;
11524 else if (longlong)
11525 type = long_long_integer_type_node;
11526 else if (long_p)
11527 type = long_integer_type_node;
11528 else if (short_p)
11529 type = short_integer_type_node;
11530
11531 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
11532 {
11533 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11534 error_at (declspecs->locations[ds_complex],
11535 "complex invalid for %qs", name);
11536 /* If a modifier is specified, the resulting complex is the complex
11537 form of TYPE. E.g, "complex short" is "complex short int". */
11538 else if (type == integer_type_node)
11539 type = complex_integer_type_node;
11540 else if (type == float_type_node)
11541 type = complex_float_type_node;
11542 else if (type == double_type_node)
11543 type = complex_double_type_node;
11544 else if (type == long_double_type_node)
11545 type = complex_long_double_type_node;
11546 else
11547 type = build_complex_type (type);
11548 }
11549
11550 /* If we're using the injected-class-name to form a compound type or a
11551 declaration, replace it with the underlying class so we don't get
11552 redundant typedefs in the debug output. But if we are returning the
11553 type unchanged, leave it alone so that it's available to
11554 maybe_get_template_decl_from_type_decl. */
11555 if (CLASS_TYPE_P (type)
11556 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
11557 && type == TREE_TYPE (TYPE_NAME (type))
11558 && (declarator || type_quals))
11559 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
11560
11561 type_quals |= cp_type_quals (type);
11562 type = cp_build_qualified_type_real
11563 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
11564 || declspecs->decltype_p)
11565 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
11566 /* We might have ignored or rejected some of the qualifiers. */
11567 type_quals = cp_type_quals (type);
11568
11569 if (cxx_dialect >= cxx17 && type && is_auto (type)
11570 && innermost_code != cdk_function
11571 && id_declarator && declarator != id_declarator)
11572 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
11573 {
11574 error_at (typespec_loc, "template placeholder type %qT must be followed "
11575 "by a simple declarator-id", type);
11576 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl);
11577 type = error_mark_node;
11578 }
11579
11580 staticp = 0;
11581 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
11582 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
11583 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
11584
11585 storage_class = declspecs->storage_class;
11586 if (storage_class == sc_static)
11587 staticp = 1 + (decl_context == FIELD);
11588
11589 if (virtualp)
11590 {
11591 if (staticp == 2)
11592 {
11593 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11594 richloc.add_range (declspecs->locations[ds_storage_class]);
11595 error_at (&richloc, "member %qD cannot be declared both %<virtual%> "
11596 "and %<static%>", dname);
11597 storage_class = sc_none;
11598 staticp = 0;
11599 }
11600 if (constexpr_p && cxx_dialect < cxx2a)
11601 {
11602 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
11603 richloc.add_range (declspecs->locations[ds_constexpr]);
11604 pedwarn (&richloc, OPT_Wpedantic, "member %qD can be declared both "
11605 "%<virtual%> and %<constexpr%> only in %<-std=c++2a%> or "
11606 "%<-std=gnu++2a%>", dname);
11607 }
11608 }
11609 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
11610
11611 /* Issue errors about use of storage classes for parameters. */
11612 if (decl_context == PARM)
11613 {
11614 if (typedef_p)
11615 {
11616 error_at (declspecs->locations[ds_typedef],
11617 "typedef declaration invalid in parameter declaration");
11618 return error_mark_node;
11619 }
11620 else if (template_parm_flag && storage_class != sc_none)
11621 {
11622 error_at (min_location (declspecs->locations[ds_thread],
11623 declspecs->locations[ds_storage_class]),
11624 "storage class specified for template parameter %qs",
11625 name);
11626 return error_mark_node;
11627 }
11628 else if (storage_class == sc_static
11629 || storage_class == sc_extern
11630 || thread_p)
11631 {
11632 error_at (min_location (declspecs->locations[ds_thread],
11633 declspecs->locations[ds_storage_class]),
11634 "storage class specified for parameter %qs", name);
11635 return error_mark_node;
11636 }
11637
11638 /* Function parameters cannot be concept. */
11639 if (concept_p)
11640 {
11641 error_at (declspecs->locations[ds_concept],
11642 "a parameter cannot be declared %qs", "concept");
11643 concept_p = 0;
11644 constexpr_p = 0;
11645 }
11646 /* Function parameters cannot be constexpr. If we saw one, moan
11647 and pretend it wasn't there. */
11648 else if (constexpr_p)
11649 {
11650 error_at (declspecs->locations[ds_constexpr],
11651 "a parameter cannot be declared %qs", "constexpr");
11652 constexpr_p = 0;
11653 }
11654 if (constinit_p)
11655 {
11656 error_at (declspecs->locations[ds_constinit],
11657 "a parameter cannot be declared %qs", "constinit");
11658 constinit_p = 0;
11659 }
11660 if (consteval_p)
11661 {
11662 error_at (declspecs->locations[ds_consteval],
11663 "a parameter cannot be declared %qs", "consteval");
11664 consteval_p = 0;
11665 }
11666 }
11667
11668 /* Give error if `virtual' is used outside of class declaration. */
11669 if (virtualp
11670 && (current_class_name == NULL_TREE || decl_context != FIELD))
11671 {
11672 error_at (declspecs->locations[ds_virtual],
11673 "%<virtual%> outside class declaration");
11674 virtualp = 0;
11675 }
11676
11677 if (innermost_code == cdk_decomp)
11678 {
11679 location_t loc = (declarator->kind == cdk_reference
11680 ? declarator->declarator->id_loc : declarator->id_loc);
11681 if (inlinep)
11682 error_at (declspecs->locations[ds_inline],
11683 "structured binding declaration cannot be %qs", "inline");
11684 if (typedef_p)
11685 error_at (declspecs->locations[ds_typedef],
11686 "structured binding declaration cannot be %qs", "typedef");
11687 if (constexpr_p && !concept_p)
11688 error_at (declspecs->locations[ds_constexpr], "structured "
11689 "binding declaration cannot be %qs", "constexpr");
11690 if (consteval_p)
11691 error_at (declspecs->locations[ds_consteval], "structured "
11692 "binding declaration cannot be %qs", "consteval");
11693 if (thread_p && cxx_dialect < cxx2a)
11694 pedwarn (declspecs->locations[ds_thread], 0,
11695 "structured binding declaration can be %qs only in "
11696 "%<-std=c++2a%> or %<-std=gnu++2a%>",
11697 declspecs->gnu_thread_keyword_p
11698 ? "__thread" : "thread_local");
11699 if (concept_p)
11700 error_at (declspecs->locations[ds_concept],
11701 "structured binding declaration cannot be %qs", "concept");
11702 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */
11703 if (type_quals & TYPE_QUAL_VOLATILE)
11704 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile,
11705 "%<volatile%>-qualified structured binding is deprecated");
11706 switch (storage_class)
11707 {
11708 case sc_none:
11709 break;
11710 case sc_register:
11711 error_at (loc, "structured binding declaration cannot be %qs",
11712 "register");
11713 break;
11714 case sc_static:
11715 if (cxx_dialect < cxx2a)
11716 pedwarn (loc, 0,
11717 "structured binding declaration can be %qs only in "
11718 "%<-std=c++2a%> or %<-std=gnu++2a%>", "static");
11719 break;
11720 case sc_extern:
11721 error_at (loc, "structured binding declaration cannot be %qs",
11722 "extern");
11723 break;
11724 case sc_mutable:
11725 error_at (loc, "structured binding declaration cannot be %qs",
11726 "mutable");
11727 break;
11728 case sc_auto:
11729 error_at (loc, "structured binding declaration cannot be "
11730 "C++98 %<auto%>");
11731 break;
11732 default:
11733 gcc_unreachable ();
11734 }
11735 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM
11736 || TYPE_IDENTIFIER (type) != auto_identifier)
11737 {
11738 if (type != error_mark_node)
11739 {
11740 error_at (loc, "structured binding declaration cannot have "
11741 "type %qT", type);
11742 inform (loc,
11743 "type must be cv-qualified %<auto%> or reference to "
11744 "cv-qualified %<auto%>");
11745 }
11746 type = build_qualified_type (make_auto (), type_quals);
11747 declspecs->type = type;
11748 }
11749 inlinep = 0;
11750 typedef_p = 0;
11751 constexpr_p = 0;
11752 consteval_p = 0;
11753 concept_p = 0;
11754 if (storage_class != sc_static)
11755 {
11756 storage_class = sc_none;
11757 declspecs->storage_class = sc_none;
11758 }
11759 }
11760
11761 /* Static anonymous unions are dealt with here. */
11762 if (staticp && decl_context == TYPENAME
11763 && declspecs->type
11764 && ANON_AGGR_TYPE_P (declspecs->type))
11765 decl_context = FIELD;
11766
11767 /* Warn about storage classes that are invalid for certain
11768 kinds of declarations (parameters, typenames, etc.). */
11769 if (thread_p
11770 && ((storage_class
11771 && storage_class != sc_extern
11772 && storage_class != sc_static)
11773 || typedef_p))
11774 {
11775 location_t loc
11776 = min_location (declspecs->locations[ds_thread],
11777 declspecs->locations[ds_storage_class]);
11778 error_at (loc, "multiple storage classes in declaration of %qs", name);
11779 thread_p = false;
11780 }
11781 if (decl_context != NORMAL
11782 && ((storage_class != sc_none
11783 && storage_class != sc_mutable)
11784 || thread_p))
11785 {
11786 if ((decl_context == PARM || decl_context == CATCHPARM)
11787 && (storage_class == sc_register
11788 || storage_class == sc_auto))
11789 ;
11790 else if (typedef_p)
11791 ;
11792 else if (decl_context == FIELD
11793 /* C++ allows static class elements. */
11794 && storage_class == sc_static)
11795 /* C++ also allows inlines and signed and unsigned elements,
11796 but in those cases we don't come in here. */
11797 ;
11798 else
11799 {
11800 location_t loc
11801 = min_location (declspecs->locations[ds_thread],
11802 declspecs->locations[ds_storage_class]);
11803 if (decl_context == FIELD)
11804 error_at (loc, "storage class specified for %qs", name);
11805 else if (decl_context == PARM || decl_context == CATCHPARM)
11806 error_at (loc, "storage class specified for parameter %qs", name);
11807 else
11808 error_at (loc, "storage class specified for typename");
11809 if (storage_class == sc_register
11810 || storage_class == sc_auto
11811 || storage_class == sc_extern
11812 || thread_p)
11813 storage_class = sc_none;
11814 }
11815 }
11816 else if (storage_class == sc_extern && funcdef_flag
11817 && ! toplevel_bindings_p ())
11818 error ("nested function %qs declared %<extern%>", name);
11819 else if (toplevel_bindings_p ())
11820 {
11821 if (storage_class == sc_auto)
11822 error_at (declspecs->locations[ds_storage_class],
11823 "top-level declaration of %qs specifies %<auto%>", name);
11824 }
11825 else if (thread_p
11826 && storage_class != sc_extern
11827 && storage_class != sc_static)
11828 {
11829 if (declspecs->gnu_thread_keyword_p)
11830 pedwarn (declspecs->locations[ds_thread],
11831 0, "function-scope %qs implicitly auto and "
11832 "declared %<__thread%>", name);
11833
11834 /* When thread_local is applied to a variable of block scope the
11835 storage-class-specifier static is implied if it does not appear
11836 explicitly. */
11837 storage_class = declspecs->storage_class = sc_static;
11838 staticp = 1;
11839 }
11840
11841 if (storage_class && friendp)
11842 {
11843 error_at (min_location (declspecs->locations[ds_thread],
11844 declspecs->locations[ds_storage_class]),
11845 "storage class specifiers invalid in friend function "
11846 "declarations");
11847 storage_class = sc_none;
11848 staticp = 0;
11849 }
11850
11851 if (!id_declarator)
11852 unqualified_id = NULL_TREE;
11853 else
11854 {
11855 unqualified_id = id_declarator->u.id.unqualified_name;
11856 switch (TREE_CODE (unqualified_id))
11857 {
11858 case BIT_NOT_EXPR:
11859 unqualified_id = TREE_OPERAND (unqualified_id, 0);
11860 if (TYPE_P (unqualified_id))
11861 unqualified_id = constructor_name (unqualified_id);
11862 break;
11863
11864 case IDENTIFIER_NODE:
11865 case TEMPLATE_ID_EXPR:
11866 break;
11867
11868 default:
11869 gcc_unreachable ();
11870 }
11871 }
11872
11873 if (declspecs->std_attributes)
11874 {
11875 location_t attr_loc = declspecs->locations[ds_std_attribute];
11876 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored"))
11877 inform (attr_loc, "an attribute that appertains to a type-specifier "
11878 "is ignored");
11879 }
11880
11881 /* Determine the type of the entity declared by recurring on the
11882 declarator. */
11883 for (; declarator; declarator = declarator->declarator)
11884 {
11885 const cp_declarator *inner_declarator;
11886 tree attrs;
11887
11888 if (type == error_mark_node)
11889 return error_mark_node;
11890
11891 attrs = declarator->attributes;
11892 if (attrs)
11893 {
11894 int attr_flags;
11895
11896 attr_flags = 0;
11897 if (declarator == NULL || declarator->kind == cdk_id)
11898 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11899 if (declarator->kind == cdk_function)
11900 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11901 if (declarator->kind == cdk_array)
11902 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11903 returned_attrs = decl_attributes (&type,
11904 chainon (returned_attrs, attrs),
11905 attr_flags);
11906 }
11907
11908 inner_declarator = declarator->declarator;
11909
11910 /* We don't want to warn in parameter context because we don't
11911 yet know if the parse will succeed, and this might turn out
11912 to be a constructor call. */
11913 if (decl_context != PARM
11914 && decl_context != TYPENAME
11915 && !typedef_p
11916 && declarator->parenthesized != UNKNOWN_LOCATION
11917 /* If the type is class-like and the inner name used a
11918 global namespace qualifier, we need the parens.
11919 Unfortunately all we can tell is whether a qualified name
11920 was used or not. */
11921 && !(inner_declarator
11922 && inner_declarator->kind == cdk_id
11923 && inner_declarator->u.id.qualifying_scope
11924 && (MAYBE_CLASS_TYPE_P (type)
11925 || TREE_CODE (type) == ENUMERAL_TYPE)))
11926 warning_at (declarator->parenthesized, OPT_Wparentheses,
11927 "unnecessary parentheses in declaration of %qs", name);
11928 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
11929 break;
11930
11931 switch (declarator->kind)
11932 {
11933 case cdk_array:
11934 type = create_array_type_for_decl (dname, type,
11935 declarator->u.array.bounds,
11936 declarator->id_loc);
11937 if (!valid_array_size_p (dname
11938 ? declarator->id_loc : input_location,
11939 type, dname))
11940 type = error_mark_node;
11941
11942 if (declarator->std_attributes)
11943 /* [dcl.array]/1:
11944
11945 The optional attribute-specifier-seq appertains to the
11946 array. */
11947 returned_attrs = chainon (returned_attrs,
11948 declarator->std_attributes);
11949 break;
11950
11951 case cdk_function:
11952 {
11953 tree arg_types;
11954 int funcdecl_p;
11955
11956 /* Declaring a function type. */
11957
11958 {
11959 iloc_sentinel ils (declspecs->locations[ds_type_spec]);
11960 abstract_virtuals_error (ACU_RETURN, type);
11961 }
11962
11963 /* Pick up type qualifiers which should be applied to `this'. */
11964 memfn_quals = declarator->u.function.qualifiers;
11965 /* Pick up virt-specifiers. */
11966 virt_specifiers = declarator->u.function.virt_specifiers;
11967 /* And ref-qualifier, too */
11968 rqual = declarator->u.function.ref_qualifier;
11969 /* And tx-qualifier. */
11970 tree tx_qual = declarator->u.function.tx_qualifier;
11971 /* Pick up the exception specifications. */
11972 raises = declarator->u.function.exception_specification;
11973 /* If the exception-specification is ill-formed, let's pretend
11974 there wasn't one. */
11975 if (raises == error_mark_node)
11976 raises = NULL_TREE;
11977
11978 if (reqs)
11979 error_at (location_of (reqs), "requires-clause on return type");
11980 reqs = declarator->u.function.requires_clause;
11981
11982 /* Say it's a definition only for the CALL_EXPR
11983 closest to the identifier. */
11984 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
11985
11986 /* Handle a late-specified return type. */
11987 tree late_return_type = declarator->u.function.late_return_type;
11988 if (funcdecl_p
11989 /* This is the case e.g. for
11990 using T = auto () -> int. */
11991 || inner_declarator == NULL)
11992 {
11993 if (tree auto_node = type_uses_auto (type))
11994 {
11995 if (!late_return_type)
11996 {
11997 if (current_class_type
11998 && LAMBDA_TYPE_P (current_class_type))
11999 /* OK for C++11 lambdas. */;
12000 else if (cxx_dialect < cxx14)
12001 {
12002 error_at (typespec_loc, "%qs function uses "
12003 "%<auto%> type specifier without "
12004 "trailing return type", name);
12005 inform (typespec_loc,
12006 "deduced return type only available "
12007 "with %<-std=c++14%> or %<-std=gnu++14%>");
12008 }
12009 else if (virtualp)
12010 {
12011 error_at (typespec_loc, "virtual function "
12012 "cannot have deduced return type");
12013 virtualp = false;
12014 }
12015 }
12016 else if (!is_auto (type) && sfk != sfk_conversion)
12017 {
12018 error_at (typespec_loc, "%qs function with trailing "
12019 "return type has %qT as its type rather "
12020 "than plain %<auto%>", name, type);
12021 return error_mark_node;
12022 }
12023 else if (is_auto (type) && AUTO_IS_DECLTYPE (type))
12024 {
12025 if (funcdecl_p)
12026 error_at (typespec_loc,
12027 "%qs function with trailing return type "
12028 "has %<decltype(auto)%> as its type "
12029 "rather than plain %<auto%>", name);
12030 else
12031 error_at (typespec_loc,
12032 "invalid use of %<decltype(auto)%>");
12033 return error_mark_node;
12034 }
12035 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node);
12036 if (!tmpl)
12037 if (tree late_auto = type_uses_auto (late_return_type))
12038 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto);
12039 if (tmpl && funcdecl_p)
12040 {
12041 if (!dguide_name_p (unqualified_id))
12042 {
12043 error_at (declarator->id_loc, "deduced class "
12044 "type %qD in function return type",
12045 DECL_NAME (tmpl));
12046 inform (DECL_SOURCE_LOCATION (tmpl),
12047 "%qD declared here", tmpl);
12048 return error_mark_node;
12049 }
12050 else if (!late_return_type)
12051 {
12052 error_at (declarator->id_loc, "deduction guide "
12053 "for %qT must have trailing return "
12054 "type", TREE_TYPE (tmpl));
12055 inform (DECL_SOURCE_LOCATION (tmpl),
12056 "%qD declared here", tmpl);
12057 return error_mark_node;
12058 }
12059 else if (CLASS_TYPE_P (late_return_type)
12060 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
12061 && (CLASSTYPE_TI_TEMPLATE (late_return_type)
12062 == tmpl))
12063 /* OK */;
12064 else
12065 error ("trailing return type %qT of deduction guide "
12066 "is not a specialization of %qT",
12067 late_return_type, TREE_TYPE (tmpl));
12068 }
12069 }
12070 else if (late_return_type
12071 && sfk != sfk_conversion)
12072 {
12073 if (late_return_type == error_mark_node)
12074 return error_mark_node;
12075 if (cxx_dialect < cxx11)
12076 /* Not using maybe_warn_cpp0x because this should
12077 always be an error. */
12078 error_at (typespec_loc,
12079 "trailing return type only available "
12080 "with %<-std=c++11%> or %<-std=gnu++11%>");
12081 else
12082 error_at (typespec_loc, "%qs function with trailing "
12083 "return type not declared with %<auto%> "
12084 "type specifier", name);
12085 return error_mark_node;
12086 }
12087 }
12088 type = splice_late_return_type (type, late_return_type);
12089 if (type == error_mark_node)
12090 return error_mark_node;
12091
12092 if (late_return_type)
12093 {
12094 late_return_type_p = true;
12095 type_quals = cp_type_quals (type);
12096 }
12097
12098 if (type_quals != TYPE_UNQUALIFIED)
12099 {
12100 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
12101 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type "
12102 "qualifiers ignored on function return type");
12103 /* [dcl.fct] "A volatile-qualified return type is
12104 deprecated." */
12105 if (type_quals & TYPE_QUAL_VOLATILE)
12106 warning_at (typespec_loc, OPT_Wvolatile,
12107 "%<volatile%>-qualified return type is "
12108 "deprecated");
12109
12110 /* We now know that the TYPE_QUALS don't apply to the
12111 decl, but to its return type. */
12112 type_quals = TYPE_UNQUALIFIED;
12113 }
12114
12115 /* Error about some types functions can't return. */
12116
12117 if (TREE_CODE (type) == FUNCTION_TYPE)
12118 {
12119 error_at (typespec_loc, "%qs declared as function returning "
12120 "a function", name);
12121 return error_mark_node;
12122 }
12123 if (TREE_CODE (type) == ARRAY_TYPE)
12124 {
12125 error_at (typespec_loc, "%qs declared as function returning "
12126 "an array", name);
12127 return error_mark_node;
12128 }
12129 if (constinit_p)
12130 {
12131 error_at (declspecs->locations[ds_constinit],
12132 "%<constinit%> on function return type is not "
12133 "allowed");
12134 return error_mark_node;
12135 }
12136 /* Only plain decltype(auto) is allowed. */
12137 if (tree a = type_uses_auto (type))
12138 {
12139 if (AUTO_IS_DECLTYPE (a) && a != type)
12140 {
12141 error_at (typespec_loc, "%qT as type rather than "
12142 "plain %<decltype(auto)%>", type);
12143 return error_mark_node;
12144 }
12145 }
12146
12147 if (ctype == NULL_TREE
12148 && decl_context == FIELD
12149 && funcdecl_p
12150 && friendp == 0)
12151 ctype = current_class_type;
12152
12153 if (ctype && (sfk == sfk_constructor
12154 || sfk == sfk_destructor))
12155 {
12156 /* We are within a class's scope. If our declarator name
12157 is the same as the class name, and we are defining
12158 a function, then it is a constructor/destructor, and
12159 therefore returns a void type. */
12160
12161 /* ISO C++ 12.4/2. A destructor may not be declared
12162 const or volatile. A destructor may not be static.
12163 A destructor may not be declared with ref-qualifier.
12164
12165 ISO C++ 12.1. A constructor may not be declared
12166 const or volatile. A constructor may not be
12167 virtual. A constructor may not be static.
12168 A constructor may not be declared with ref-qualifier. */
12169 if (staticp == 2)
12170 error_at (declspecs->locations[ds_storage_class],
12171 (flags == DTOR_FLAG)
12172 ? G_("destructor cannot be static member "
12173 "function")
12174 : G_("constructor cannot be static member "
12175 "function"));
12176 if (memfn_quals)
12177 {
12178 error ((flags == DTOR_FLAG)
12179 ? G_("destructors may not be cv-qualified")
12180 : G_("constructors may not be cv-qualified"));
12181 memfn_quals = TYPE_UNQUALIFIED;
12182 }
12183
12184 if (rqual)
12185 {
12186 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
12187 error ((flags == DTOR_FLAG)
12188 ? G_("destructors may not be ref-qualified")
12189 : G_("constructors may not be ref-qualified"));
12190 rqual = REF_QUAL_NONE;
12191 }
12192
12193 if (decl_context == FIELD
12194 && !member_function_or_else (ctype,
12195 current_class_type,
12196 flags))
12197 return error_mark_node;
12198
12199 if (flags != DTOR_FLAG)
12200 {
12201 /* It's a constructor. */
12202 if (explicitp == 1)
12203 explicitp = 2;
12204 if (virtualp)
12205 {
12206 permerror (declspecs->locations[ds_virtual],
12207 "constructors cannot be declared %<virtual%>");
12208 virtualp = 0;
12209 }
12210 if (decl_context == FIELD
12211 && sfk != sfk_constructor)
12212 return error_mark_node;
12213 }
12214 if (decl_context == FIELD)
12215 staticp = 0;
12216 }
12217 else if (friendp)
12218 {
12219 if (virtualp)
12220 {
12221 /* Cannot be both friend and virtual. */
12222 gcc_rich_location richloc (declspecs->locations[ds_virtual]);
12223 richloc.add_range (declspecs->locations[ds_friend]);
12224 error_at (&richloc, "virtual functions cannot be friends");
12225 friendp = 0;
12226 }
12227 if (decl_context == NORMAL)
12228 error_at (declarator->id_loc,
12229 "friend declaration not in class definition");
12230 if (current_function_decl && funcdef_flag)
12231 {
12232 error_at (declarator->id_loc,
12233 "cannot define friend function %qs in a local "
12234 "class definition", name);
12235 friendp = 0;
12236 }
12237 /* [class.friend]/6: A function can be defined in a friend
12238 declaration if the function name is unqualified. */
12239 if (funcdef_flag && in_namespace)
12240 {
12241 if (in_namespace == global_namespace)
12242 error_at (declarator->id_loc,
12243 "friend function definition %qs cannot have "
12244 "a name qualified with %<::%>", name);
12245 else
12246 error_at (declarator->id_loc,
12247 "friend function definition %qs cannot have "
12248 "a name qualified with %<%D::%>", name,
12249 in_namespace);
12250 }
12251 }
12252 else if (ctype && sfk == sfk_conversion)
12253 {
12254 if (explicitp == 1)
12255 {
12256 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
12257 explicitp = 2;
12258 }
12259 if (late_return_type_p)
12260 error ("a conversion function cannot have a trailing return type");
12261 }
12262 else if (sfk == sfk_deduction_guide)
12263 {
12264 if (explicitp == 1)
12265 explicitp = 2;
12266 }
12267
12268 tree pushed_scope = NULL_TREE;
12269 if (funcdecl_p
12270 && decl_context != FIELD
12271 && inner_declarator->u.id.qualifying_scope
12272 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope))
12273 pushed_scope
12274 = push_scope (inner_declarator->u.id.qualifying_scope);
12275
12276 arg_types = grokparms (declarator->u.function.parameters, &parms);
12277
12278 if (pushed_scope)
12279 pop_scope (pushed_scope);
12280
12281 if (inner_declarator
12282 && inner_declarator->kind == cdk_id
12283 && inner_declarator->u.id.sfk == sfk_destructor
12284 && arg_types != void_list_node)
12285 {
12286 error_at (declarator->id_loc,
12287 "destructors may not have parameters");
12288 arg_types = void_list_node;
12289 parms = NULL_TREE;
12290 }
12291
12292 type = build_function_type (type, arg_types);
12293
12294 tree attrs = declarator->std_attributes;
12295 if (tx_qual)
12296 {
12297 tree att = build_tree_list (tx_qual, NULL_TREE);
12298 /* transaction_safe applies to the type, but
12299 transaction_safe_dynamic applies to the function. */
12300 if (is_attribute_p ("transaction_safe", tx_qual))
12301 attrs = chainon (attrs, att);
12302 else
12303 returned_attrs = chainon (returned_attrs, att);
12304 }
12305 if (attrs)
12306 /* [dcl.fct]/2:
12307
12308 The optional attribute-specifier-seq appertains to
12309 the function type. */
12310 decl_attributes (&type, attrs, 0);
12311
12312 if (raises)
12313 type = build_exception_variant (type, raises);
12314 }
12315 break;
12316
12317 case cdk_pointer:
12318 case cdk_reference:
12319 case cdk_ptrmem:
12320 /* Filter out pointers-to-references and references-to-references.
12321 We can get these if a TYPE_DECL is used. */
12322
12323 if (TYPE_REF_P (type))
12324 {
12325 if (declarator->kind != cdk_reference)
12326 {
12327 error ("cannot declare pointer to %q#T", type);
12328 type = TREE_TYPE (type);
12329 }
12330
12331 /* In C++0x, we allow reference to reference declarations
12332 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
12333 and template type arguments [14.3.1/4 temp.arg.type]. The
12334 check for direct reference to reference declarations, which
12335 are still forbidden, occurs below. Reasoning behind the change
12336 can be found in DR106, DR540, and the rvalue reference
12337 proposals. */
12338 else if (cxx_dialect == cxx98)
12339 {
12340 error ("cannot declare reference to %q#T", type);
12341 type = TREE_TYPE (type);
12342 }
12343 }
12344 else if (VOID_TYPE_P (type))
12345 {
12346 if (declarator->kind == cdk_reference)
12347 error ("cannot declare reference to %q#T", type);
12348 else if (declarator->kind == cdk_ptrmem)
12349 error ("cannot declare pointer to %q#T member", type);
12350 }
12351
12352 /* We now know that the TYPE_QUALS don't apply to the decl,
12353 but to the target of the pointer. */
12354 type_quals = TYPE_UNQUALIFIED;
12355
12356 /* This code used to handle METHOD_TYPE, but I don't think it's
12357 possible to get it here anymore. */
12358 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12359 if (declarator->kind == cdk_ptrmem
12360 && TREE_CODE (type) == FUNCTION_TYPE)
12361 {
12362 memfn_quals |= type_memfn_quals (type);
12363 type = build_memfn_type (type,
12364 declarator->u.pointer.class_type,
12365 memfn_quals,
12366 rqual);
12367 if (type == error_mark_node)
12368 return error_mark_node;
12369
12370 rqual = REF_QUAL_NONE;
12371 memfn_quals = TYPE_UNQUALIFIED;
12372 }
12373
12374 if (TREE_CODE (type) == FUNCTION_TYPE
12375 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12376 || type_memfn_rqual (type) != REF_QUAL_NONE))
12377 error (declarator->kind == cdk_reference
12378 ? G_("cannot declare reference to qualified function type %qT")
12379 : G_("cannot declare pointer to qualified function type %qT"),
12380 type);
12381
12382 /* When the pointed-to type involves components of variable size,
12383 care must be taken to ensure that the size evaluation code is
12384 emitted early enough to dominate all the possible later uses
12385 and late enough for the variables on which it depends to have
12386 been assigned.
12387
12388 This is expected to happen automatically when the pointed-to
12389 type has a name/declaration of it's own, but special attention
12390 is required if the type is anonymous.
12391
12392 We handle the NORMAL and FIELD contexts here by inserting a
12393 dummy statement that just evaluates the size at a safe point
12394 and ensures it is not deferred until e.g. within a deeper
12395 conditional context (c++/43555).
12396
12397 We expect nothing to be needed here for PARM or TYPENAME.
12398 Evaluating the size at this point for TYPENAME would
12399 actually be incorrect, as we might be in the middle of an
12400 expression with side effects on the pointed-to type size
12401 "arguments" prior to the pointer declaration point and the
12402 size evaluation could end up prior to the side effects. */
12403
12404 if (!TYPE_NAME (type)
12405 && (decl_context == NORMAL || decl_context == FIELD)
12406 && at_function_scope_p ()
12407 && variably_modified_type_p (type, NULL_TREE))
12408 {
12409 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
12410 NULL_TREE, type);
12411 add_decl_expr (TYPE_NAME (type));
12412 }
12413
12414 if (declarator->kind == cdk_reference)
12415 {
12416 /* In C++0x, the type we are creating a reference to might be
12417 a typedef which is itself a reference type. In that case,
12418 we follow the reference collapsing rules in
12419 [7.1.3/8 dcl.typedef] to create the final reference type:
12420
12421 "If a typedef TD names a type that is a reference to a type
12422 T, an attempt to create the type 'lvalue reference to cv TD'
12423 creates the type 'lvalue reference to T,' while an attempt
12424 to create the type "rvalue reference to cv TD' creates the
12425 type TD."
12426 */
12427 if (VOID_TYPE_P (type))
12428 /* We already gave an error. */;
12429 else if (TYPE_REF_P (type))
12430 {
12431 if (declarator->u.reference.rvalue_ref)
12432 /* Leave type alone. */;
12433 else
12434 type = cp_build_reference_type (TREE_TYPE (type), false);
12435 }
12436 else
12437 type = cp_build_reference_type
12438 (type, declarator->u.reference.rvalue_ref);
12439
12440 /* In C++0x, we need this check for direct reference to
12441 reference declarations, which are forbidden by
12442 [8.3.2/5 dcl.ref]. Reference to reference declarations
12443 are only allowed indirectly through typedefs and template
12444 type arguments. Example:
12445
12446 void foo(int & &); // invalid ref-to-ref decl
12447
12448 typedef int & int_ref;
12449 void foo(int_ref &); // valid ref-to-ref decl
12450 */
12451 if (inner_declarator && inner_declarator->kind == cdk_reference)
12452 error ("cannot declare reference to %q#T, which is not "
12453 "a typedef or a template type argument", type);
12454 }
12455 else if (TREE_CODE (type) == METHOD_TYPE)
12456 type = build_ptrmemfunc_type (build_pointer_type (type));
12457 else if (declarator->kind == cdk_ptrmem)
12458 {
12459 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
12460 != NAMESPACE_DECL);
12461 if (declarator->u.pointer.class_type == error_mark_node)
12462 /* We will already have complained. */
12463 type = error_mark_node;
12464 else
12465 type = build_ptrmem_type (declarator->u.pointer.class_type,
12466 type);
12467 }
12468 else
12469 type = build_pointer_type (type);
12470
12471 /* Process a list of type modifier keywords (such as
12472 const or volatile) that were given inside the `*' or `&'. */
12473
12474 if (declarator->u.pointer.qualifiers)
12475 {
12476 type
12477 = cp_build_qualified_type (type,
12478 declarator->u.pointer.qualifiers);
12479 type_quals = cp_type_quals (type);
12480 }
12481
12482 /* Apply C++11 attributes to the pointer, and not to the
12483 type pointed to. This is unlike what is done for GNU
12484 attributes above. It is to comply with [dcl.ptr]/1:
12485
12486 [the optional attribute-specifier-seq (7.6.1) appertains
12487 to the pointer and not to the object pointed to]. */
12488 if (declarator->std_attributes)
12489 decl_attributes (&type, declarator->std_attributes,
12490 0);
12491
12492 ctype = NULL_TREE;
12493 break;
12494
12495 case cdk_error:
12496 break;
12497
12498 default:
12499 gcc_unreachable ();
12500 }
12501 }
12502
12503 id_loc = declarator ? declarator->id_loc : input_location;
12504
12505 /* A `constexpr' specifier used in an object declaration declares
12506 the object as `const'. */
12507 if (constexpr_p && innermost_code != cdk_function)
12508 {
12509 /* DR1688 says that a `constexpr' specifier in combination with
12510 `volatile' is valid. */
12511
12512 if (!TYPE_REF_P (type))
12513 {
12514 type_quals |= TYPE_QUAL_CONST;
12515 type = cp_build_qualified_type (type, type_quals);
12516 }
12517 }
12518
12519 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
12520 && !FUNC_OR_METHOD_TYPE_P (type)
12521 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
12522 {
12523 error ("template-id %qD used as a declarator",
12524 unqualified_id);
12525 unqualified_id = dname;
12526 }
12527
12528 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
12529 qualified with a class-name, turn it into a METHOD_TYPE, unless
12530 we know that the function is static. We take advantage of this
12531 opportunity to do other processing that pertains to entities
12532 explicitly declared to be class members. Note that if DECLARATOR
12533 is non-NULL, we know it is a cdk_id declarator; otherwise, we
12534 would not have exited the loop above. */
12535 if (declarator
12536 && declarator->kind == cdk_id
12537 && declarator->u.id.qualifying_scope
12538 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
12539 {
12540 ctype = declarator->u.id.qualifying_scope;
12541 ctype = TYPE_MAIN_VARIANT (ctype);
12542 template_count = num_template_headers_for_class (ctype);
12543
12544 if (ctype == current_class_type)
12545 {
12546 if (friendp)
12547 {
12548 permerror (declspecs->locations[ds_friend],
12549 "member functions are implicitly "
12550 "friends of their class");
12551 friendp = 0;
12552 }
12553 else
12554 permerror (id_loc, "extra qualification %<%T::%> on member %qs",
12555 ctype, name);
12556 }
12557 else if (/* If the qualifying type is already complete, then we
12558 can skip the following checks. */
12559 !COMPLETE_TYPE_P (ctype)
12560 && (/* If the function is being defined, then
12561 qualifying type must certainly be complete. */
12562 funcdef_flag
12563 /* A friend declaration of "T::f" is OK, even if
12564 "T" is a template parameter. But, if this
12565 function is not a friend, the qualifying type
12566 must be a class. */
12567 || (!friendp && !CLASS_TYPE_P (ctype))
12568 /* For a declaration, the type need not be
12569 complete, if either it is dependent (since there
12570 is no meaningful definition of complete in that
12571 case) or the qualifying class is currently being
12572 defined. */
12573 || !(dependent_type_p (ctype)
12574 || currently_open_class (ctype)))
12575 /* Check that the qualifying type is complete. */
12576 && !complete_type_or_else (ctype, NULL_TREE))
12577 return error_mark_node;
12578 else if (TREE_CODE (type) == FUNCTION_TYPE)
12579 {
12580 if (current_class_type
12581 && (!friendp || funcdef_flag || initialized))
12582 {
12583 error_at (id_loc, funcdef_flag || initialized
12584 ? G_("cannot define member function %<%T::%s%> "
12585 "within %qT")
12586 : G_("cannot declare member function %<%T::%s%> "
12587 "within %qT"),
12588 ctype, name, current_class_type);
12589 return error_mark_node;
12590 }
12591 }
12592 else if (typedef_p && current_class_type)
12593 {
12594 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT",
12595 ctype, name, current_class_type);
12596 return error_mark_node;
12597 }
12598 }
12599
12600 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
12601 ctype = current_class_type;
12602
12603 /* Now TYPE has the actual type. */
12604
12605 if (returned_attrs)
12606 {
12607 if (attrlist)
12608 *attrlist = chainon (returned_attrs, *attrlist);
12609 else
12610 attrlist = &returned_attrs;
12611 }
12612
12613 if (declarator
12614 && declarator->kind == cdk_id
12615 && declarator->std_attributes
12616 && attrlist != NULL)
12617 {
12618 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
12619 a declarator-id appertains to the entity that is declared. */
12620 if (declarator->std_attributes != error_mark_node)
12621 *attrlist = chainon (*attrlist, declarator->std_attributes);
12622 else
12623 /* We should have already diagnosed the issue (c++/78344). */
12624 gcc_assert (seen_error ());
12625 }
12626
12627 /* Handle parameter packs. */
12628 if (parameter_pack_p)
12629 {
12630 if (decl_context == PARM)
12631 /* Turn the type into a pack expansion.*/
12632 type = make_pack_expansion (type);
12633 else
12634 error ("non-parameter %qs cannot be a parameter pack", name);
12635 }
12636
12637 if ((decl_context == FIELD || decl_context == PARM)
12638 && !processing_template_decl
12639 && variably_modified_type_p (type, NULL_TREE))
12640 {
12641 if (decl_context == FIELD)
12642 error_at (id_loc,
12643 "data member may not have variably modified type %qT", type);
12644 else
12645 error_at (id_loc,
12646 "parameter may not have variably modified type %qT", type);
12647 type = error_mark_node;
12648 }
12649
12650 if (explicitp == 1 || (explicitp && friendp))
12651 {
12652 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
12653 in the declaration of a constructor or conversion function within
12654 a class definition. */
12655 if (!current_class_type)
12656 error_at (declspecs->locations[ds_explicit],
12657 "%<explicit%> outside class declaration");
12658 else if (friendp)
12659 error_at (declspecs->locations[ds_explicit],
12660 "%<explicit%> in friend declaration");
12661 else
12662 error_at (declspecs->locations[ds_explicit],
12663 "only declarations of constructors and conversion operators "
12664 "can be %<explicit%>");
12665 explicitp = 0;
12666 }
12667
12668 if (storage_class == sc_mutable)
12669 {
12670 location_t sloc = declspecs->locations[ds_storage_class];
12671 if (decl_context != FIELD || friendp)
12672 {
12673 error_at (sloc, "non-member %qs cannot be declared %<mutable%>",
12674 name);
12675 storage_class = sc_none;
12676 }
12677 else if (decl_context == TYPENAME || typedef_p)
12678 {
12679 error_at (sloc,
12680 "non-object member %qs cannot be declared %<mutable%>",
12681 name);
12682 storage_class = sc_none;
12683 }
12684 else if (FUNC_OR_METHOD_TYPE_P (type))
12685 {
12686 error_at (sloc, "function %qs cannot be declared %<mutable%>",
12687 name);
12688 storage_class = sc_none;
12689 }
12690 else if (staticp)
12691 {
12692 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>",
12693 name);
12694 storage_class = sc_none;
12695 }
12696 else if (type_quals & TYPE_QUAL_CONST)
12697 {
12698 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>",
12699 name);
12700 storage_class = sc_none;
12701 }
12702 else if (TYPE_REF_P (type))
12703 {
12704 permerror (sloc, "reference %qs cannot be declared %<mutable%>",
12705 name);
12706 storage_class = sc_none;
12707 }
12708 }
12709
12710 /* If this is declaring a typedef name, return a TYPE_DECL. */
12711 if (typedef_p && decl_context != TYPENAME)
12712 {
12713 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias);
12714 tree decl;
12715
12716 if (funcdef_flag)
12717 {
12718 if (decl_context == NORMAL)
12719 error_at (id_loc,
12720 "typedef may not be a function definition");
12721 else
12722 error_at (id_loc,
12723 "typedef may not be a member function definition");
12724 return error_mark_node;
12725 }
12726
12727 /* This declaration:
12728
12729 typedef void f(int) const;
12730
12731 declares a function type which is not a member of any
12732 particular class, but which is cv-qualified; for
12733 example "f S::*" declares a pointer to a const-qualified
12734 member function of S. We record the cv-qualification in the
12735 function type. */
12736 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
12737 {
12738 type = apply_memfn_quals (type, memfn_quals, rqual);
12739
12740 /* We have now dealt with these qualifiers. */
12741 memfn_quals = TYPE_UNQUALIFIED;
12742 rqual = REF_QUAL_NONE;
12743 }
12744
12745 if (type_uses_auto (type))
12746 {
12747 if (alias_p)
12748 error_at (declspecs->locations[ds_type_spec],
12749 "%<auto%> not allowed in alias declaration");
12750 else
12751 error_at (declspecs->locations[ds_type_spec],
12752 "typedef declared %<auto%>");
12753 type = error_mark_node;
12754 }
12755
12756 if (reqs)
12757 error_at (location_of (reqs), "requires-clause on typedef");
12758
12759 if (id_declarator && declarator->u.id.qualifying_scope)
12760 {
12761 error_at (id_loc, "typedef name may not be a nested-name-specifier");
12762 type = error_mark_node;
12763 }
12764
12765 if (decl_context == FIELD)
12766 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type);
12767 else
12768 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type);
12769
12770 if (decl_context != FIELD)
12771 {
12772 if (!current_function_decl)
12773 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12774 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl))
12775 /* The TYPE_DECL is "abstract" because there will be
12776 clones of this constructor/destructor, and there will
12777 be copies of this TYPE_DECL generated in those
12778 clones. The decloning optimization (for space) may
12779 revert this subsequently if it determines that
12780 the clones should share a common implementation. */
12781 DECL_ABSTRACT_P (decl) = true;
12782 }
12783 else if (current_class_type
12784 && constructor_name_p (unqualified_id, current_class_type))
12785 permerror (id_loc, "ISO C++ forbids nested type %qD with same name "
12786 "as enclosing class",
12787 unqualified_id);
12788
12789 /* If the user declares "typedef struct {...} foo" then the
12790 struct will have an anonymous name. Fill that name in now.
12791 Nothing can refer to it, so nothing needs know about the name
12792 change. */
12793 if (type != error_mark_node
12794 && unqualified_id
12795 && TYPE_NAME (type)
12796 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12797 && TYPE_UNNAMED_P (type)
12798 && declspecs->type_definition_p
12799 && attributes_naming_typedef_ok (*attrlist)
12800 && cp_type_quals (type) == TYPE_UNQUALIFIED)
12801 name_unnamed_type (type, decl);
12802
12803 if (signed_p
12804 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12805 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12806
12807 bad_specifiers (decl, BSP_TYPE, virtualp,
12808 memfn_quals != TYPE_UNQUALIFIED,
12809 inlinep, friendp, raises != NULL_TREE,
12810 declspecs->locations);
12811
12812 if (alias_p)
12813 /* Acknowledge that this was written:
12814 `using analias = atype;'. */
12815 TYPE_DECL_ALIAS_P (decl) = 1;
12816
12817 return decl;
12818 }
12819
12820 /* Detect the case of an array type of unspecified size
12821 which came, as such, direct from a typedef name.
12822 We must copy the type, so that the array's domain can be
12823 individually set by the object's initializer. */
12824
12825 if (type && typedef_type
12826 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12827 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12828 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12829
12830 /* Detect where we're using a typedef of function type to declare a
12831 function. PARMS will not be set, so we must create it now. */
12832
12833 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12834 {
12835 tree decls = NULL_TREE;
12836 tree args;
12837
12838 for (args = TYPE_ARG_TYPES (type);
12839 args && args != void_list_node;
12840 args = TREE_CHAIN (args))
12841 {
12842 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE,
12843 TREE_VALUE (args));
12844
12845 DECL_CHAIN (decl) = decls;
12846 decls = decl;
12847 }
12848
12849 parms = nreverse (decls);
12850
12851 if (decl_context != TYPENAME)
12852 {
12853 /* The qualifiers on the function type become the qualifiers on
12854 the non-static member function. */
12855 memfn_quals |= type_memfn_quals (type);
12856 rqual = type_memfn_rqual (type);
12857 type_quals = TYPE_UNQUALIFIED;
12858 }
12859 }
12860
12861 /* If this is a type name (such as, in a cast or sizeof),
12862 compute the type and return it now. */
12863
12864 if (decl_context == TYPENAME)
12865 {
12866 /* Note that here we don't care about type_quals. */
12867
12868 /* Special case: "friend class foo" looks like a TYPENAME context. */
12869 if (friendp)
12870 {
12871 if (inlinep)
12872 {
12873 error ("%<inline%> specified for friend class declaration");
12874 inlinep = 0;
12875 }
12876
12877 if (!current_aggr)
12878 {
12879 /* Don't allow friend declaration without a class-key. */
12880 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12881 permerror (input_location, "template parameters cannot be friends");
12882 else if (TREE_CODE (type) == TYPENAME_TYPE)
12883 permerror (input_location, "friend declaration requires class-key, "
12884 "i.e. %<friend class %T::%D%>",
12885 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
12886 else
12887 permerror (input_location, "friend declaration requires class-key, "
12888 "i.e. %<friend %#T%>",
12889 type);
12890 }
12891
12892 /* Only try to do this stuff if we didn't already give up. */
12893 if (type != integer_type_node)
12894 {
12895 /* A friendly class? */
12896 if (current_class_type)
12897 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
12898 /*complain=*/true);
12899 else
12900 error ("trying to make class %qT a friend of global scope",
12901 type);
12902
12903 type = void_type_node;
12904 }
12905 }
12906 else if (memfn_quals || rqual)
12907 {
12908 if (ctype == NULL_TREE
12909 && TREE_CODE (type) == METHOD_TYPE)
12910 ctype = TYPE_METHOD_BASETYPE (type);
12911
12912 if (ctype)
12913 type = build_memfn_type (type, ctype, memfn_quals, rqual);
12914 /* Core issue #547: need to allow this in template type args.
12915 Allow it in general in C++11 for alias-declarations. */
12916 else if ((template_type_arg || cxx_dialect >= cxx11)
12917 && TREE_CODE (type) == FUNCTION_TYPE)
12918 type = apply_memfn_quals (type, memfn_quals, rqual);
12919 else
12920 error ("invalid qualifiers on non-member function type");
12921 }
12922
12923 if (reqs)
12924 error_at (location_of (reqs), "requires-clause on type-id");
12925
12926 return type;
12927 }
12928 else if (unqualified_id == NULL_TREE && decl_context != PARM
12929 && decl_context != CATCHPARM
12930 && TREE_CODE (type) != UNION_TYPE
12931 && ! bitfield
12932 && innermost_code != cdk_decomp)
12933 {
12934 error ("abstract declarator %qT used as declaration", type);
12935 return error_mark_node;
12936 }
12937
12938 if (!FUNC_OR_METHOD_TYPE_P (type))
12939 {
12940 /* Only functions may be declared using an operator-function-id. */
12941 if (dname && IDENTIFIER_ANY_OP_P (dname))
12942 {
12943 error_at (id_loc, "declaration of %qD as non-function", dname);
12944 return error_mark_node;
12945 }
12946
12947 if (reqs)
12948 error_at (location_of (reqs),
12949 "requires-clause on declaration of non-function type %qT",
12950 type);
12951 }
12952
12953 /* We don't check parameter types here because we can emit a better
12954 error message later. */
12955 if (decl_context != PARM)
12956 {
12957 type = check_var_type (unqualified_id, type, id_loc);
12958 if (type == error_mark_node)
12959 return error_mark_node;
12960 }
12961
12962 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12963 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
12964
12965 if (decl_context == PARM || decl_context == CATCHPARM)
12966 {
12967 if (ctype || in_namespace)
12968 error ("cannot use %<::%> in parameter declaration");
12969
12970 tree auto_node = type_uses_auto (type);
12971 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag))
12972 {
12973 if (cxx_dialect >= cxx14)
12974 {
12975 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node))
12976 error_at (typespec_loc,
12977 "cannot declare a parameter with %<decltype(auto)%>");
12978 else
12979 error_at (typespec_loc,
12980 "%<auto%> parameter not permitted in this context");
12981 }
12982 else
12983 error_at (typespec_loc, "parameter declared %<auto%>");
12984 type = error_mark_node;
12985 }
12986
12987 /* A parameter declared as an array of T is really a pointer to T.
12988 One declared as a function is really a pointer to a function.
12989 One declared as a member is really a pointer to member. */
12990
12991 if (TREE_CODE (type) == ARRAY_TYPE)
12992 {
12993 /* Transfer const-ness of array into that of type pointed to. */
12994 type = build_pointer_type (TREE_TYPE (type));
12995 type_quals = TYPE_UNQUALIFIED;
12996 array_parameter_p = true;
12997 }
12998 else if (TREE_CODE (type) == FUNCTION_TYPE)
12999 type = build_pointer_type (type);
13000 }
13001
13002 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
13003 && !(unqualified_id
13004 && identifier_p (unqualified_id)
13005 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)))
13006 {
13007 cp_cv_quals real_quals = memfn_quals;
13008 if (cxx_dialect < cxx14 && constexpr_p
13009 && sfk != sfk_constructor && sfk != sfk_destructor)
13010 real_quals |= TYPE_QUAL_CONST;
13011 type = build_memfn_type (type, ctype, real_quals, rqual);
13012 }
13013
13014 {
13015 tree decl = NULL_TREE;
13016
13017 if (decl_context == PARM)
13018 {
13019 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type);
13020 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
13021
13022 bad_specifiers (decl, BSP_PARM, virtualp,
13023 memfn_quals != TYPE_UNQUALIFIED,
13024 inlinep, friendp, raises != NULL_TREE,
13025 declspecs->locations);
13026 }
13027 else if (decl_context == FIELD)
13028 {
13029 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE)
13030 if (tree auto_node = type_uses_auto (type))
13031 {
13032 location_t tloc = declspecs->locations[ds_type_spec];
13033 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13034 error_at (tloc, "invalid use of template-name %qE without an "
13035 "argument list",
13036 CLASS_PLACEHOLDER_TEMPLATE (auto_node));
13037 else
13038 error_at (tloc, "non-static data member declared with "
13039 "placeholder %qT", auto_node);
13040 type = error_mark_node;
13041 }
13042
13043 /* The C99 flexible array extension. */
13044 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
13045 && TYPE_DOMAIN (type) == NULL_TREE)
13046 {
13047 if (ctype
13048 && (TREE_CODE (ctype) == UNION_TYPE
13049 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
13050 {
13051 error_at (id_loc, "flexible array member in union");
13052 type = error_mark_node;
13053 }
13054 else
13055 {
13056 /* Array is a flexible member. */
13057 if (name)
13058 pedwarn (id_loc, OPT_Wpedantic,
13059 "ISO C++ forbids flexible array member %qs", name);
13060 else
13061 pedwarn (input_location, OPT_Wpedantic,
13062 "ISO C++ forbids flexible array members");
13063
13064 /* Flexible array member has a null domain. */
13065 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
13066 }
13067 }
13068
13069 if (type == error_mark_node)
13070 {
13071 /* Happens when declaring arrays of sizes which
13072 are error_mark_node, for example. */
13073 decl = NULL_TREE;
13074 }
13075 else if (in_namespace && !friendp)
13076 {
13077 /* Something like struct S { int N::j; }; */
13078 error_at (id_loc, "invalid use of %<::%>");
13079 return error_mark_node;
13080 }
13081 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id)
13082 {
13083 int publicp = 0;
13084 tree function_context;
13085
13086 if (friendp == 0)
13087 {
13088 /* This should never happen in pure C++ (the check
13089 could be an assert). It could happen in
13090 Objective-C++ if someone writes invalid code that
13091 uses a function declaration for an instance
13092 variable or property (instance variables and
13093 properties are parsed as FIELD_DECLs, but they are
13094 part of an Objective-C class, not a C++ class).
13095 That code is invalid and is caught by this
13096 check. */
13097 if (!ctype)
13098 {
13099 error ("declaration of function %qD in invalid context",
13100 unqualified_id);
13101 return error_mark_node;
13102 }
13103
13104 /* ``A union may [ ... ] not [ have ] virtual functions.''
13105 ARM 9.5 */
13106 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
13107 {
13108 error_at (declspecs->locations[ds_virtual],
13109 "function %qD declared %<virtual%> inside a union",
13110 unqualified_id);
13111 return error_mark_node;
13112 }
13113
13114 if (virtualp
13115 && identifier_p (unqualified_id)
13116 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13117 {
13118 error_at (declspecs->locations[ds_virtual],
13119 "%qD cannot be declared %<virtual%>, since it "
13120 "is always static", unqualified_id);
13121 virtualp = 0;
13122 }
13123 }
13124
13125 /* Check that the name used for a destructor makes sense. */
13126 if (sfk == sfk_destructor)
13127 {
13128 tree uqname = id_declarator->u.id.unqualified_name;
13129
13130 if (!ctype)
13131 {
13132 gcc_assert (friendp);
13133 error_at (id_loc, "expected qualified name in friend "
13134 "declaration for destructor %qD", uqname);
13135 return error_mark_node;
13136 }
13137
13138 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
13139 {
13140 error_at (id_loc, "declaration of %qD as member of %qT",
13141 uqname, ctype);
13142 return error_mark_node;
13143 }
13144 if (concept_p)
13145 {
13146 error_at (declspecs->locations[ds_concept],
13147 "a destructor cannot be %qs", "concept");
13148 return error_mark_node;
13149 }
13150 if (constexpr_p && cxx_dialect < cxx2a)
13151 {
13152 error_at (declspecs->locations[ds_constexpr],
13153 "%<constexpr%> destructors only available"
13154 " with %<-std=c++2a%> or %<-std=gnu++2a%>");
13155 return error_mark_node;
13156 }
13157 if (consteval_p)
13158 {
13159 error_at (declspecs->locations[ds_consteval],
13160 "a destructor cannot be %qs", "consteval");
13161 return error_mark_node;
13162 }
13163 }
13164 else if (sfk == sfk_constructor && friendp && !ctype)
13165 {
13166 error ("expected qualified name in friend declaration "
13167 "for constructor %qD",
13168 id_declarator->u.id.unqualified_name);
13169 return error_mark_node;
13170 }
13171 if (sfk == sfk_constructor)
13172 if (concept_p)
13173 {
13174 error_at (declspecs->locations[ds_concept],
13175 "a constructor cannot be %<concept%>");
13176 return error_mark_node;
13177 }
13178 if (concept_p)
13179 {
13180 error_at (declspecs->locations[ds_concept],
13181 "a concept cannot be a member function");
13182 concept_p = false;
13183 }
13184 else if (consteval_p
13185 && identifier_p (unqualified_id)
13186 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13187 {
13188 error_at (declspecs->locations[ds_consteval],
13189 "%qD cannot be %qs", unqualified_id, "consteval");
13190 consteval_p = false;
13191 }
13192
13193 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13194 {
13195 tree tmpl = TREE_OPERAND (unqualified_id, 0);
13196 if (variable_template_p (tmpl))
13197 {
13198 error_at (id_loc, "specialization of variable template "
13199 "%qD declared as function", tmpl);
13200 inform (DECL_SOURCE_LOCATION (tmpl),
13201 "variable template declared here");
13202 return error_mark_node;
13203 }
13204 }
13205
13206 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
13207 function_context
13208 = (ctype != NULL_TREE
13209 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE);
13210 publicp = ((! friendp || ! staticp)
13211 && function_context == NULL_TREE);
13212
13213 decl = grokfndecl (ctype, type,
13214 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
13215 ? unqualified_id : dname,
13216 parms,
13217 unqualified_id,
13218 declspecs,
13219 reqs,
13220 virtualp, flags, memfn_quals, rqual, raises,
13221 friendp ? -1 : 0, friendp, publicp,
13222 inlinep | (2 * constexpr_p) | (4 * concept_p)
13223 | (8 * consteval_p),
13224 initialized == SD_DELETED, sfk,
13225 funcdef_flag, late_return_type_p,
13226 template_count, in_namespace,
13227 attrlist, id_loc);
13228 decl = set_virt_specifiers (decl, virt_specifiers);
13229 if (decl == NULL_TREE)
13230 return error_mark_node;
13231 #if 0
13232 /* This clobbers the attrs stored in `decl' from `attrlist'. */
13233 /* The decl and setting of decl_attr is also turned off. */
13234 decl = build_decl_attribute_variant (decl, decl_attr);
13235 #endif
13236
13237 /* [class.conv.ctor]
13238
13239 A constructor declared without the function-specifier
13240 explicit that can be called with a single parameter
13241 specifies a conversion from the type of its first
13242 parameter to the type of its class. Such a constructor
13243 is called a converting constructor. */
13244 if (explicitp == 2)
13245 DECL_NONCONVERTING_P (decl) = 1;
13246
13247 if (declspecs->explicit_specifier)
13248 store_explicit_specifier (decl, declspecs->explicit_specifier);
13249 }
13250 else if (!staticp && !dependent_type_p (type)
13251 && !COMPLETE_TYPE_P (complete_type (type))
13252 && (!complete_or_array_type_p (type)
13253 || initialized == 0))
13254 {
13255 if (TREE_CODE (type) != ARRAY_TYPE
13256 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
13257 {
13258 if (unqualified_id)
13259 {
13260 error_at (id_loc, "field %qD has incomplete type %qT",
13261 unqualified_id, type);
13262 cxx_incomplete_type_inform (strip_array_types (type));
13263 }
13264 else
13265 error ("name %qT has incomplete type", type);
13266
13267 type = error_mark_node;
13268 decl = NULL_TREE;
13269 }
13270 }
13271 else if (!verify_type_context (input_location,
13272 staticp
13273 ? TCTX_STATIC_STORAGE
13274 : TCTX_FIELD, type))
13275 {
13276 type = error_mark_node;
13277 decl = NULL_TREE;
13278 }
13279 else
13280 {
13281 if (friendp)
13282 {
13283 if (unqualified_id)
13284 error_at (id_loc,
13285 "%qE is neither function nor member function; "
13286 "cannot be declared friend", unqualified_id);
13287 else
13288 error ("unnamed field is neither function nor member "
13289 "function; cannot be declared friend");
13290 return error_mark_node;
13291 }
13292 decl = NULL_TREE;
13293 }
13294
13295 if (friendp)
13296 {
13297 /* Friends are treated specially. */
13298 if (ctype == current_class_type)
13299 ; /* We already issued a permerror. */
13300 else if (decl && DECL_NAME (decl))
13301 {
13302 if (initialized)
13303 /* Kludge: We need funcdef_flag to be true in do_friend for
13304 in-class defaulted functions, but that breaks grokfndecl.
13305 So set it here. */
13306 funcdef_flag = true;
13307 if (template_class_depth (current_class_type) == 0)
13308 {
13309 decl = check_explicit_specialization
13310 (unqualified_id, decl, template_count,
13311 2 * funcdef_flag + 4);
13312 if (decl == error_mark_node)
13313 return error_mark_node;
13314 }
13315
13316 decl = do_friend (ctype, unqualified_id, decl,
13317 *attrlist, flags,
13318 funcdef_flag);
13319 return decl;
13320 }
13321 else
13322 return error_mark_node;
13323 }
13324
13325 /* Structure field. It may not be a function, except for C++. */
13326
13327 if (decl == NULL_TREE)
13328 {
13329 if (staticp)
13330 {
13331 /* C++ allows static class members. All other work
13332 for this is done by grokfield. */
13333 decl = build_lang_decl_loc (id_loc, VAR_DECL,
13334 unqualified_id, type);
13335 set_linkage_for_static_data_member (decl);
13336 if (concept_p)
13337 error_at (declspecs->locations[ds_concept],
13338 "static data member %qE declared %qs",
13339 unqualified_id, "concept");
13340 else if (constexpr_p && !initialized)
13341 {
13342 error_at (DECL_SOURCE_LOCATION (decl),
13343 "%<constexpr%> static data member %qD must "
13344 "have an initializer", decl);
13345 constexpr_p = false;
13346 }
13347 if (consteval_p)
13348 error_at (declspecs->locations[ds_consteval],
13349 "static data member %qE declared %qs",
13350 unqualified_id, "consteval");
13351
13352 if (inlinep)
13353 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13354
13355 if (!DECL_VAR_DECLARED_INLINE_P (decl)
13356 && !(cxx_dialect >= cxx17 && constexpr_p))
13357 /* Even if there is an in-class initialization, DECL
13358 is considered undefined until an out-of-class
13359 definition is provided, unless this is an inline
13360 variable. */
13361 DECL_EXTERNAL (decl) = 1;
13362
13363 if (thread_p)
13364 {
13365 CP_DECL_THREAD_LOCAL_P (decl) = true;
13366 if (!processing_template_decl)
13367 set_decl_tls_model (decl, decl_default_tls_model (decl));
13368 if (declspecs->gnu_thread_keyword_p)
13369 SET_DECL_GNU_TLS_P (decl);
13370 }
13371 }
13372 else
13373 {
13374 if (concept_p)
13375 {
13376 error_at (declspecs->locations[ds_concept],
13377 "non-static data member %qE declared %qs",
13378 unqualified_id, "concept");
13379 concept_p = false;
13380 constexpr_p = false;
13381 }
13382 else if (constexpr_p)
13383 {
13384 error_at (declspecs->locations[ds_constexpr],
13385 "non-static data member %qE declared %qs",
13386 unqualified_id, "constexpr");
13387 constexpr_p = false;
13388 }
13389 if (constinit_p)
13390 {
13391 error_at (declspecs->locations[ds_constinit],
13392 "non-static data member %qE declared %qs",
13393 unqualified_id, "constinit");
13394 constinit_p = false;
13395 }
13396 if (consteval_p)
13397 {
13398 error_at (declspecs->locations[ds_consteval],
13399 "non-static data member %qE declared %qs",
13400 unqualified_id, "consteval");
13401 consteval_p = false;
13402 }
13403 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type);
13404 DECL_NONADDRESSABLE_P (decl) = bitfield;
13405 if (bitfield && !unqualified_id)
13406 {
13407 TREE_NO_WARNING (decl) = 1;
13408 DECL_PADDING_P (decl) = 1;
13409 }
13410
13411 if (storage_class == sc_mutable)
13412 {
13413 DECL_MUTABLE_P (decl) = 1;
13414 storage_class = sc_none;
13415 }
13416
13417 if (initialized)
13418 {
13419 /* An attempt is being made to initialize a non-static
13420 member. This is new in C++11. */
13421 maybe_warn_cpp0x (CPP0X_NSDMI);
13422
13423 /* If this has been parsed with static storage class, but
13424 errors forced staticp to be cleared, ensure NSDMI is
13425 not present. */
13426 if (declspecs->storage_class == sc_static)
13427 DECL_INITIAL (decl) = error_mark_node;
13428 }
13429 }
13430
13431 bad_specifiers (decl, BSP_FIELD, virtualp,
13432 memfn_quals != TYPE_UNQUALIFIED,
13433 staticp ? false : inlinep, friendp,
13434 raises != NULL_TREE,
13435 declspecs->locations);
13436 }
13437 }
13438 else if (FUNC_OR_METHOD_TYPE_P (type))
13439 {
13440 tree original_name;
13441 int publicp = 0;
13442
13443 if (!unqualified_id)
13444 return error_mark_node;
13445
13446 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
13447 original_name = dname;
13448 else
13449 original_name = unqualified_id;
13450 // FIXME:gcc_assert (original_name == dname);
13451
13452 if (storage_class == sc_auto)
13453 error_at (declspecs->locations[ds_storage_class],
13454 "storage class %<auto%> invalid for function %qs", name);
13455 else if (storage_class == sc_register)
13456 error_at (declspecs->locations[ds_storage_class],
13457 "storage class %<register%> invalid for function %qs",
13458 name);
13459 else if (thread_p)
13460 {
13461 if (declspecs->gnu_thread_keyword_p)
13462 error_at (declspecs->locations[ds_thread],
13463 "storage class %<__thread%> invalid for function %qs",
13464 name);
13465 else
13466 error_at (declspecs->locations[ds_thread],
13467 "storage class %<thread_local%> invalid for "
13468 "function %qs", name);
13469 }
13470
13471 if (virt_specifiers)
13472 error ("virt-specifiers in %qs not allowed outside a class "
13473 "definition", name);
13474 /* Function declaration not at top level.
13475 Storage classes other than `extern' are not allowed
13476 and `extern' makes no difference. */
13477 if (! toplevel_bindings_p ()
13478 && (storage_class == sc_static
13479 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
13480 && pedantic)
13481 {
13482 if (storage_class == sc_static)
13483 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic,
13484 "%<static%> specifier invalid for function %qs "
13485 "declared out of global scope", name);
13486 else
13487 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic,
13488 "%<inline%> specifier invalid for function %qs "
13489 "declared out of global scope", name);
13490 }
13491
13492 if (ctype == NULL_TREE)
13493 {
13494 if (virtualp)
13495 {
13496 error ("virtual non-class function %qs", name);
13497 virtualp = 0;
13498 }
13499 else if (sfk == sfk_constructor
13500 || sfk == sfk_destructor)
13501 {
13502 error (funcdef_flag
13503 ? G_("%qs defined in a non-class scope")
13504 : G_("%qs declared in a non-class scope"), name);
13505 sfk = sfk_none;
13506 }
13507 }
13508 if (consteval_p
13509 && identifier_p (unqualified_id)
13510 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
13511 {
13512 error_at (declspecs->locations[ds_consteval],
13513 "%qD cannot be %qs", unqualified_id, "consteval");
13514 consteval_p = false;
13515 }
13516
13517 /* Record whether the function is public. */
13518 publicp = (ctype != NULL_TREE
13519 || storage_class != sc_static);
13520
13521 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
13522 declspecs,
13523 reqs, virtualp, flags, memfn_quals, rqual, raises,
13524 1, friendp,
13525 publicp,
13526 inlinep | (2 * constexpr_p) | (4 * concept_p)
13527 | (8 * consteval_p),
13528 initialized == SD_DELETED,
13529 sfk,
13530 funcdef_flag,
13531 late_return_type_p,
13532 template_count, in_namespace, attrlist,
13533 id_loc);
13534 if (decl == NULL_TREE)
13535 return error_mark_node;
13536
13537 if (explicitp == 2)
13538 DECL_NONCONVERTING_P (decl) = 1;
13539 if (staticp == 1)
13540 {
13541 int invalid_static = 0;
13542
13543 /* Don't allow a static member function in a class, and forbid
13544 declaring main to be static. */
13545 if (TREE_CODE (type) == METHOD_TYPE)
13546 {
13547 permerror (input_location, "cannot declare member function %qD to have "
13548 "static linkage", decl);
13549 invalid_static = 1;
13550 }
13551 else if (current_function_decl)
13552 {
13553 /* 7.1.1: There can be no static function declarations within a
13554 block. */
13555 error_at (declspecs->locations[ds_storage_class],
13556 "cannot declare static function inside another function");
13557 invalid_static = 1;
13558 }
13559
13560 if (invalid_static)
13561 {
13562 staticp = 0;
13563 storage_class = sc_none;
13564 }
13565 }
13566 }
13567 else
13568 {
13569 /* It's a variable. */
13570
13571 /* An uninitialized decl with `extern' is a reference. */
13572 decl = grokvardecl (type, dname, unqualified_id,
13573 declspecs,
13574 initialized,
13575 type_quals,
13576 inlinep,
13577 concept_p,
13578 template_count,
13579 ctype ? ctype : in_namespace,
13580 id_loc);
13581 if (decl == NULL_TREE)
13582 return error_mark_node;
13583
13584 bad_specifiers (decl, BSP_VAR, virtualp,
13585 memfn_quals != TYPE_UNQUALIFIED,
13586 inlinep, friendp, raises != NULL_TREE,
13587 declspecs->locations);
13588
13589 if (ctype)
13590 {
13591 DECL_CONTEXT (decl) = ctype;
13592 if (staticp == 1)
13593 {
13594 permerror (declspecs->locations[ds_storage_class],
13595 "%<static%> may not be used when defining "
13596 "(as opposed to declaring) a static data member");
13597 staticp = 0;
13598 storage_class = sc_none;
13599 }
13600 if (storage_class == sc_register && TREE_STATIC (decl))
13601 {
13602 error ("static member %qD declared %<register%>", decl);
13603 storage_class = sc_none;
13604 }
13605 if (storage_class == sc_extern && pedantic)
13606 {
13607 pedwarn (input_location, OPT_Wpedantic,
13608 "cannot explicitly declare member %q#D to have "
13609 "extern linkage", decl);
13610 storage_class = sc_none;
13611 }
13612 }
13613 else if (constexpr_p && DECL_EXTERNAL (decl))
13614 {
13615 error_at (DECL_SOURCE_LOCATION (decl),
13616 "declaration of %<constexpr%> variable %qD "
13617 "is not a definition", decl);
13618 constexpr_p = false;
13619 }
13620 if (consteval_p)
13621 {
13622 error_at (DECL_SOURCE_LOCATION (decl),
13623 "a variable cannot be declared %<consteval%>");
13624 consteval_p = false;
13625 }
13626
13627 if (inlinep)
13628 mark_inline_variable (decl, declspecs->locations[ds_inline]);
13629 if (innermost_code == cdk_decomp)
13630 {
13631 gcc_assert (declarator && declarator->kind == cdk_decomp);
13632 DECL_SOURCE_LOCATION (decl) = id_loc;
13633 DECL_ARTIFICIAL (decl) = 1;
13634 fit_decomposition_lang_decl (decl, NULL_TREE);
13635 }
13636 }
13637
13638 if (VAR_P (decl) && !initialized)
13639 if (tree auto_node = type_uses_auto (type))
13640 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
13641 {
13642 location_t loc = declspecs->locations[ds_type_spec];
13643 error_at (loc, "declaration of %q#D has no initializer", decl);
13644 TREE_TYPE (decl) = error_mark_node;
13645 }
13646
13647 if (storage_class == sc_extern && initialized && !funcdef_flag)
13648 {
13649 if (toplevel_bindings_p ())
13650 {
13651 /* It's common practice (and completely valid) to have a const
13652 be initialized and declared extern. */
13653 if (!(type_quals & TYPE_QUAL_CONST))
13654 warning_at (DECL_SOURCE_LOCATION (decl), 0,
13655 "%qs initialized and declared %<extern%>", name);
13656 }
13657 else
13658 {
13659 error_at (DECL_SOURCE_LOCATION (decl),
13660 "%qs has both %<extern%> and initializer", name);
13661 return error_mark_node;
13662 }
13663 }
13664
13665 /* Record `register' declaration for warnings on &
13666 and in case doing stupid register allocation. */
13667
13668 if (storage_class == sc_register)
13669 {
13670 DECL_REGISTER (decl) = 1;
13671 /* Warn about register storage specifiers on PARM_DECLs. */
13672 if (TREE_CODE (decl) == PARM_DECL)
13673 {
13674 if (cxx_dialect >= cxx17)
13675 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13676 "ISO C++17 does not allow %<register%> storage "
13677 "class specifier");
13678 else
13679 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister,
13680 "%<register%> storage class specifier used");
13681 }
13682 }
13683 else if (storage_class == sc_extern)
13684 DECL_THIS_EXTERN (decl) = 1;
13685 else if (storage_class == sc_static)
13686 DECL_THIS_STATIC (decl) = 1;
13687
13688 /* Set constexpr flag on vars (functions got it in grokfndecl). */
13689 if (constexpr_p && VAR_P (decl))
13690 DECL_DECLARED_CONSTEXPR_P (decl) = true;
13691
13692 /* Record constancy and volatility on the DECL itself . There's
13693 no need to do this when processing a template; we'll do this
13694 for the instantiated declaration based on the type of DECL. */
13695 if (!processing_template_decl)
13696 cp_apply_type_quals_to_decl (type_quals, decl);
13697
13698 return decl;
13699 }
13700 }
13701 \f
13702 /* Subroutine of start_function. Ensure that each of the parameter
13703 types (as listed in PARMS) is complete, as is required for a
13704 function definition. */
13705
13706 static void
13707 require_complete_types_for_parms (tree parms)
13708 {
13709 for (; parms; parms = DECL_CHAIN (parms))
13710 {
13711 if (dependent_type_p (TREE_TYPE (parms)))
13712 continue;
13713 if (!VOID_TYPE_P (TREE_TYPE (parms))
13714 && complete_type_or_else (TREE_TYPE (parms), parms))
13715 {
13716 relayout_decl (parms);
13717 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
13718
13719 maybe_warn_parm_abi (TREE_TYPE (parms),
13720 DECL_SOURCE_LOCATION (parms));
13721 }
13722 else
13723 /* grokparms or complete_type_or_else will have already issued
13724 an error. */
13725 TREE_TYPE (parms) = error_mark_node;
13726 }
13727 }
13728
13729 /* Returns nonzero if T is a local variable. */
13730
13731 int
13732 local_variable_p (const_tree t)
13733 {
13734 if ((VAR_P (t)
13735 /* A VAR_DECL with a context that is a _TYPE is a static data
13736 member. */
13737 && !TYPE_P (CP_DECL_CONTEXT (t))
13738 /* Any other non-local variable must be at namespace scope. */
13739 && !DECL_NAMESPACE_SCOPE_P (t))
13740 || (TREE_CODE (t) == PARM_DECL))
13741 return 1;
13742
13743 return 0;
13744 }
13745
13746 /* Like local_variable_p, but suitable for use as a tree-walking
13747 function. */
13748
13749 static tree
13750 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
13751 void * /*data*/)
13752 {
13753 if (local_variable_p (*tp)
13754 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
13755 return *tp;
13756 else if (TYPE_P (*tp))
13757 *walk_subtrees = 0;
13758
13759 return NULL_TREE;
13760 }
13761
13762 /* Check that ARG, which is a default-argument expression for a
13763 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
13764 something goes wrong. DECL may also be a _TYPE node, rather than a
13765 DECL, if there is no DECL available. */
13766
13767 tree
13768 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
13769 {
13770 tree var;
13771 tree decl_type;
13772
13773 if (TREE_CODE (arg) == DEFERRED_PARSE)
13774 /* We get a DEFERRED_PARSE when looking at an in-class declaration
13775 with a default argument. Ignore the argument for now; we'll
13776 deal with it after the class is complete. */
13777 return arg;
13778
13779 if (TYPE_P (decl))
13780 {
13781 decl_type = decl;
13782 decl = NULL_TREE;
13783 }
13784 else
13785 decl_type = TREE_TYPE (decl);
13786
13787 if (arg == error_mark_node
13788 || decl == error_mark_node
13789 || TREE_TYPE (arg) == error_mark_node
13790 || decl_type == error_mark_node)
13791 /* Something already went wrong. There's no need to check
13792 further. */
13793 return error_mark_node;
13794
13795 /* [dcl.fct.default]
13796
13797 A default argument expression is implicitly converted to the
13798 parameter type. */
13799 ++cp_unevaluated_operand;
13800 /* Avoid digest_init clobbering the initializer. */
13801 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg;
13802 perform_implicit_conversion_flags (decl_type, carg, complain,
13803 LOOKUP_IMPLICIT);
13804 --cp_unevaluated_operand;
13805
13806 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
13807 the call sites. */
13808 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
13809 && null_ptr_cst_p (arg)
13810 /* Don't lose side-effects as in PR90473. */
13811 && !TREE_SIDE_EFFECTS (arg))
13812 return nullptr_node;
13813
13814 /* [dcl.fct.default]
13815
13816 Local variables shall not be used in default argument
13817 expressions.
13818
13819 The keyword `this' shall not be used in a default argument of a
13820 member function. */
13821 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
13822 if (var)
13823 {
13824 if (complain & tf_warning_or_error)
13825 {
13826 if (DECL_NAME (var) == this_identifier)
13827 permerror (input_location, "default argument %qE uses %qD",
13828 arg, var);
13829 else
13830 error ("default argument %qE uses local variable %qD", arg, var);
13831 }
13832 return error_mark_node;
13833 }
13834
13835 /* All is well. */
13836 return arg;
13837 }
13838
13839 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
13840
13841 static tree
13842 type_is_deprecated (tree type)
13843 {
13844 enum tree_code code;
13845 if (TREE_DEPRECATED (type))
13846 return type;
13847 if (TYPE_NAME (type))
13848 {
13849 if (TREE_DEPRECATED (TYPE_NAME (type)))
13850 return type;
13851 else
13852 {
13853 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type)));
13854 return NULL_TREE;
13855 }
13856 }
13857
13858 /* Do warn about using typedefs to a deprecated class. */
13859 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
13860 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
13861
13862 code = TREE_CODE (type);
13863
13864 if (code == POINTER_TYPE || code == REFERENCE_TYPE
13865 || code == OFFSET_TYPE || code == FUNCTION_TYPE
13866 || code == METHOD_TYPE || code == ARRAY_TYPE)
13867 return type_is_deprecated (TREE_TYPE (type));
13868
13869 if (TYPE_PTRMEMFUNC_P (type))
13870 return type_is_deprecated
13871 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
13872
13873 return NULL_TREE;
13874 }
13875
13876 /* Decode the list of parameter types for a function type.
13877 Given the list of things declared inside the parens,
13878 return a list of types.
13879
13880 If this parameter does not end with an ellipsis, we append
13881 void_list_node.
13882
13883 *PARMS is set to the chain of PARM_DECLs created. */
13884
13885 tree
13886 grokparms (tree parmlist, tree *parms)
13887 {
13888 tree result = NULL_TREE;
13889 tree decls = NULL_TREE;
13890 tree parm;
13891 int any_error = 0;
13892
13893 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
13894 {
13895 tree type = NULL_TREE;
13896 tree init = TREE_PURPOSE (parm);
13897 tree decl = TREE_VALUE (parm);
13898
13899 if (parm == void_list_node)
13900 break;
13901
13902 if (! decl || TREE_TYPE (decl) == error_mark_node)
13903 continue;
13904
13905 type = TREE_TYPE (decl);
13906 if (VOID_TYPE_P (type))
13907 {
13908 if (same_type_p (type, void_type_node)
13909 && !init
13910 && !DECL_NAME (decl) && !result
13911 && TREE_CHAIN (parm) == void_list_node)
13912 /* DR 577: A parameter list consisting of a single
13913 unnamed parameter of non-dependent type 'void'. */
13914 break;
13915 else if (cv_qualified_p (type))
13916 error_at (DECL_SOURCE_LOCATION (decl),
13917 "invalid use of cv-qualified type %qT in "
13918 "parameter declaration", type);
13919 else
13920 error_at (DECL_SOURCE_LOCATION (decl),
13921 "invalid use of type %<void%> in parameter "
13922 "declaration");
13923 /* It's not a good idea to actually create parameters of
13924 type `void'; other parts of the compiler assume that a
13925 void type terminates the parameter list. */
13926 type = error_mark_node;
13927 TREE_TYPE (decl) = error_mark_node;
13928 }
13929
13930 if (type != error_mark_node)
13931 {
13932 if (deprecated_state != DEPRECATED_SUPPRESS)
13933 {
13934 tree deptype = type_is_deprecated (type);
13935 if (deptype)
13936 cp_warn_deprecated_use (deptype);
13937 }
13938
13939 /* [dcl.fct] "A parameter with volatile-qualified type is
13940 deprecated." */
13941 if (CP_TYPE_VOLATILE_P (type))
13942 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile,
13943 "%<volatile%>-qualified parameter is "
13944 "deprecated");
13945
13946 /* Top-level qualifiers on the parameters are
13947 ignored for function types. */
13948 type = cp_build_qualified_type (type, 0);
13949 if (TREE_CODE (type) == METHOD_TYPE)
13950 {
13951 error ("parameter %qD invalidly declared method type", decl);
13952 type = build_pointer_type (type);
13953 TREE_TYPE (decl) = type;
13954 }
13955 else if (abstract_virtuals_error (decl, type))
13956 any_error = 1; /* Seems like a good idea. */
13957 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type))
13958 {
13959 /* Before C++17 DR 393:
13960 [dcl.fct]/6, parameter types cannot contain pointers
13961 (references) to arrays of unknown bound. */
13962 tree t = TREE_TYPE (type);
13963 int ptr = TYPE_PTR_P (type);
13964
13965 while (1)
13966 {
13967 if (TYPE_PTR_P (t))
13968 ptr = 1;
13969 else if (TREE_CODE (t) != ARRAY_TYPE)
13970 break;
13971 else if (!TYPE_DOMAIN (t))
13972 break;
13973 t = TREE_TYPE (t);
13974 }
13975 if (TREE_CODE (t) == ARRAY_TYPE)
13976 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
13977 ptr
13978 ? G_("parameter %qD includes pointer to array of "
13979 "unknown bound %qT")
13980 : G_("parameter %qD includes reference to array of "
13981 "unknown bound %qT"),
13982 decl, t);
13983 }
13984
13985 if (any_error)
13986 init = NULL_TREE;
13987 else if (init && !processing_template_decl)
13988 init = check_default_argument (decl, init, tf_warning_or_error);
13989 }
13990
13991 DECL_CHAIN (decl) = decls;
13992 decls = decl;
13993 result = tree_cons (init, type, result);
13994 }
13995 decls = nreverse (decls);
13996 result = nreverse (result);
13997 if (parm)
13998 result = chainon (result, void_list_node);
13999 *parms = decls;
14000
14001 return result;
14002 }
14003
14004 \f
14005 /* D is a constructor or overloaded `operator='.
14006
14007 Let T be the class in which D is declared. Then, this function
14008 returns:
14009
14010 -1 if D's is an ill-formed constructor or copy assignment operator
14011 whose first parameter is of type `T'.
14012 0 if D is not a copy constructor or copy assignment
14013 operator.
14014 1 if D is a copy constructor or copy assignment operator whose
14015 first parameter is a reference to non-const qualified T.
14016 2 if D is a copy constructor or copy assignment operator whose
14017 first parameter is a reference to const qualified T.
14018
14019 This function can be used as a predicate. Positive values indicate
14020 a copy constructor and nonzero values indicate a copy assignment
14021 operator. */
14022
14023 int
14024 copy_fn_p (const_tree d)
14025 {
14026 tree args;
14027 tree arg_type;
14028 int result = 1;
14029
14030 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14031
14032 if (TREE_CODE (d) == TEMPLATE_DECL
14033 || (DECL_TEMPLATE_INFO (d)
14034 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14035 /* Instantiations of template member functions are never copy
14036 functions. Note that member functions of templated classes are
14037 represented as template functions internally, and we must
14038 accept those as copy functions. */
14039 return 0;
14040
14041 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14042 if (!args)
14043 return 0;
14044
14045 arg_type = TREE_VALUE (args);
14046 if (arg_type == error_mark_node)
14047 return 0;
14048
14049 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
14050 {
14051 /* Pass by value copy assignment operator. */
14052 result = -1;
14053 }
14054 else if (TYPE_REF_P (arg_type)
14055 && !TYPE_REF_IS_RVALUE (arg_type)
14056 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
14057 {
14058 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
14059 result = 2;
14060 }
14061 else
14062 return 0;
14063
14064 args = TREE_CHAIN (args);
14065
14066 if (args && args != void_list_node && !TREE_PURPOSE (args))
14067 /* There are more non-optional args. */
14068 return 0;
14069
14070 return result;
14071 }
14072
14073 /* D is a constructor or overloaded `operator='.
14074
14075 Let T be the class in which D is declared. Then, this function
14076 returns true when D is a move constructor or move assignment
14077 operator, false otherwise. */
14078
14079 bool
14080 move_fn_p (const_tree d)
14081 {
14082 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
14083
14084 if (cxx_dialect == cxx98)
14085 /* There are no move constructors if we are in C++98 mode. */
14086 return false;
14087
14088 if (TREE_CODE (d) == TEMPLATE_DECL
14089 || (DECL_TEMPLATE_INFO (d)
14090 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
14091 /* Instantiations of template member functions are never move
14092 functions. Note that member functions of templated classes are
14093 represented as template functions internally, and we must
14094 accept those as move functions. */
14095 return 0;
14096
14097 return move_signature_fn_p (d);
14098 }
14099
14100 /* D is a constructor or overloaded `operator='.
14101
14102 Then, this function returns true when D has the same signature as a move
14103 constructor or move assignment operator (because either it is such a
14104 ctor/op= or it is a template specialization with the same signature),
14105 false otherwise. */
14106
14107 bool
14108 move_signature_fn_p (const_tree d)
14109 {
14110 tree args;
14111 tree arg_type;
14112 bool result = false;
14113
14114 args = FUNCTION_FIRST_USER_PARMTYPE (d);
14115 if (!args)
14116 return 0;
14117
14118 arg_type = TREE_VALUE (args);
14119 if (arg_type == error_mark_node)
14120 return 0;
14121
14122 if (TYPE_REF_P (arg_type)
14123 && TYPE_REF_IS_RVALUE (arg_type)
14124 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
14125 DECL_CONTEXT (d)))
14126 result = true;
14127
14128 args = TREE_CHAIN (args);
14129
14130 if (args && args != void_list_node && !TREE_PURPOSE (args))
14131 /* There are more non-optional args. */
14132 return false;
14133
14134 return result;
14135 }
14136
14137 /* Remember any special properties of member function DECL. */
14138
14139 void
14140 grok_special_member_properties (tree decl)
14141 {
14142 tree class_type;
14143
14144 if (TREE_CODE (decl) == USING_DECL
14145 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
14146 return;
14147
14148 class_type = DECL_CONTEXT (decl);
14149 if (IDENTIFIER_CTOR_P (DECL_NAME (decl)))
14150 {
14151 int ctor = copy_fn_p (decl);
14152
14153 if (!DECL_ARTIFICIAL (decl))
14154 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
14155
14156 if (ctor > 0)
14157 {
14158 /* [class.copy]
14159
14160 A non-template constructor for class X is a copy
14161 constructor if its first parameter is of type X&, const
14162 X&, volatile X& or const volatile X&, and either there
14163 are no other parameters or else all other parameters have
14164 default arguments. */
14165 TYPE_HAS_COPY_CTOR (class_type) = 1;
14166 if (ctor > 1)
14167 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
14168 }
14169 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
14170 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
14171 else if (is_list_ctor (decl))
14172 TYPE_HAS_LIST_CTOR (class_type) = 1;
14173
14174 if (DECL_DECLARED_CONSTEXPR_P (decl)
14175 && !ctor && !move_fn_p (decl))
14176 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
14177 }
14178 else if (DECL_NAME (decl) == assign_op_identifier)
14179 {
14180 /* [class.copy]
14181
14182 A non-template assignment operator for class X is a copy
14183 assignment operator if its parameter is of type X, X&, const
14184 X&, volatile X& or const volatile X&. */
14185
14186 int assop = copy_fn_p (decl);
14187
14188 if (assop)
14189 {
14190 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
14191 if (assop != 1)
14192 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
14193 }
14194 }
14195 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl)))
14196 TYPE_HAS_CONVERSION (class_type) = true;
14197
14198 /* Destructors are handled in check_methods. */
14199 }
14200
14201 /* Check a constructor DECL has the correct form. Complains
14202 if the class has a constructor of the form X(X). */
14203
14204 bool
14205 grok_ctor_properties (const_tree ctype, const_tree decl)
14206 {
14207 int ctor_parm = copy_fn_p (decl);
14208
14209 if (ctor_parm < 0)
14210 {
14211 /* [class.copy]
14212
14213 A declaration of a constructor for a class X is ill-formed if
14214 its first parameter is of type (optionally cv-qualified) X
14215 and either there are no other parameters or else all other
14216 parameters have default arguments.
14217
14218 We *don't* complain about member template instantiations that
14219 have this form, though; they can occur as we try to decide
14220 what constructor to use during overload resolution. Since
14221 overload resolution will never prefer such a constructor to
14222 the non-template copy constructor (which is either explicitly
14223 or implicitly defined), there's no need to worry about their
14224 existence. Theoretically, they should never even be
14225 instantiated, but that's hard to forestall. */
14226 error_at (DECL_SOURCE_LOCATION (decl),
14227 "invalid constructor; you probably meant %<%T (const %T&)%>",
14228 ctype, ctype);
14229 return false;
14230 }
14231
14232 return true;
14233 }
14234
14235 /* DECL is a declaration for an overloaded or conversion operator. If
14236 COMPLAIN is true, errors are issued for invalid declarations. */
14237
14238 bool
14239 grok_op_properties (tree decl, bool complain)
14240 {
14241 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
14242 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE;
14243 tree name = DECL_NAME (decl);
14244 location_t loc = DECL_SOURCE_LOCATION (decl);
14245
14246 tree class_type = DECL_CONTEXT (decl);
14247 if (class_type && !CLASS_TYPE_P (class_type))
14248 class_type = NULL_TREE;
14249
14250 tree_code operator_code;
14251 unsigned op_flags;
14252 if (IDENTIFIER_CONV_OP_P (name))
14253 {
14254 /* Conversion operators are TYPE_EXPR for the purposes of this
14255 function. */
14256 operator_code = TYPE_EXPR;
14257 op_flags = OVL_OP_FLAG_UNARY;
14258 }
14259 else
14260 {
14261 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name);
14262
14263 operator_code = ovl_op->tree_code;
14264 op_flags = ovl_op->flags;
14265 gcc_checking_assert (operator_code != ERROR_MARK);
14266 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14267 }
14268
14269 if (op_flags & OVL_OP_FLAG_ALLOC)
14270 {
14271 /* operator new and operator delete are quite special. */
14272 if (class_type)
14273 switch (op_flags)
14274 {
14275 case OVL_OP_FLAG_ALLOC:
14276 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
14277 break;
14278
14279 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE:
14280 TYPE_GETS_DELETE (class_type) |= 1;
14281 break;
14282
14283 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC:
14284 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
14285 break;
14286
14287 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC:
14288 TYPE_GETS_DELETE (class_type) |= 2;
14289 break;
14290
14291 default:
14292 gcc_unreachable ();
14293 }
14294
14295 /* [basic.std.dynamic.allocation]/1:
14296
14297 A program is ill-formed if an allocation function is declared
14298 in a namespace scope other than global scope or declared
14299 static in global scope.
14300
14301 The same also holds true for deallocation functions. */
14302 if (DECL_NAMESPACE_SCOPE_P (decl))
14303 {
14304 if (CP_DECL_CONTEXT (decl) != global_namespace)
14305 {
14306 error_at (loc, "%qD may not be declared within a namespace",
14307 decl);
14308 return false;
14309 }
14310
14311 if (!TREE_PUBLIC (decl))
14312 {
14313 error_at (loc, "%qD may not be declared as static", decl);
14314 return false;
14315 }
14316 }
14317
14318 if (op_flags & OVL_OP_FLAG_DELETE)
14319 {
14320 DECL_SET_IS_OPERATOR_DELETE (decl, true);
14321 coerce_delete_type (decl, loc);
14322 }
14323 else
14324 {
14325 DECL_SET_IS_OPERATOR_NEW (decl, true);
14326 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc);
14327 }
14328
14329 return true;
14330 }
14331
14332 /* An operator function must either be a non-static member function
14333 or have at least one parameter of a class, a reference to a class,
14334 an enumeration, or a reference to an enumeration. 13.4.0.6 */
14335 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
14336 {
14337 if (operator_code == TYPE_EXPR
14338 || operator_code == CALL_EXPR
14339 || operator_code == COMPONENT_REF
14340 || operator_code == ARRAY_REF
14341 || operator_code == NOP_EXPR)
14342 {
14343 error_at (loc, "%qD must be a nonstatic member function", decl);
14344 return false;
14345 }
14346
14347 if (DECL_STATIC_FUNCTION_P (decl))
14348 {
14349 error_at (loc, "%qD must be either a non-static member "
14350 "function or a non-member function", decl);
14351 return false;
14352 }
14353
14354 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg))
14355 {
14356 if (!arg || arg == void_list_node)
14357 {
14358 if (complain)
14359 error_at(loc, "%qD must have an argument of class or "
14360 "enumerated type", decl);
14361 return false;
14362 }
14363
14364 tree type = non_reference (TREE_VALUE (arg));
14365 if (type == error_mark_node)
14366 return false;
14367
14368 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
14369 because these checks are performed even on template
14370 functions. */
14371 if (MAYBE_CLASS_TYPE_P (type)
14372 || TREE_CODE (type) == ENUMERAL_TYPE)
14373 break;
14374 }
14375 }
14376
14377 if (operator_code == CALL_EXPR)
14378 /* There are no further restrictions on the arguments to an overloaded
14379 "operator ()". */
14380 return true;
14381
14382 if (operator_code == COND_EXPR)
14383 {
14384 /* 13.4.0.3 */
14385 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>");
14386 return false;
14387 }
14388
14389 /* Count the number of arguments and check for ellipsis. */
14390 int arity = 0;
14391 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14392 {
14393 if (!arg)
14394 {
14395 /* Variadic. */
14396 error_at (loc, "%qD must not have variable number of arguments",
14397 decl);
14398 return false;
14399 }
14400 ++arity;
14401 }
14402
14403 /* Verify correct number of arguments. */
14404 switch (op_flags)
14405 {
14406 case OVL_OP_FLAG_AMBIARY:
14407 if (arity == 1)
14408 {
14409 /* We have a unary instance of an ambi-ary op. Remap to the
14410 unary one. */
14411 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]];
14412 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt];
14413 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY);
14414 operator_code = ovl_op->tree_code;
14415 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code;
14416 }
14417 else if (arity != 2)
14418 {
14419 /* This was an ambiguous operator but is invalid. */
14420 error_at (loc,
14421 methodp
14422 ? G_("%qD must have either zero or one argument")
14423 : G_("%qD must have either one or two arguments"), decl);
14424 return false;
14425 }
14426 else if ((operator_code == POSTINCREMENT_EXPR
14427 || operator_code == POSTDECREMENT_EXPR)
14428 && ! processing_template_decl
14429 /* x++ and x--'s second argument must be an int. */
14430 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)),
14431 integer_type_node))
14432 {
14433 error_at (loc,
14434 methodp
14435 ? G_("postfix %qD must have %<int%> as its argument")
14436 : G_("postfix %qD must have %<int%> as its second argument"),
14437 decl);
14438 return false;
14439 }
14440 break;
14441
14442 case OVL_OP_FLAG_UNARY:
14443 if (arity != 1)
14444 {
14445 error_at (loc,
14446 methodp
14447 ? G_("%qD must have no arguments")
14448 : G_("%qD must have exactly one argument"), decl);
14449 return false;
14450 }
14451 break;
14452
14453 case OVL_OP_FLAG_BINARY:
14454 if (arity != 2)
14455 {
14456 error_at (loc,
14457 methodp
14458 ? G_("%qD must have exactly one argument")
14459 : G_("%qD must have exactly two arguments"), decl);
14460 return false;
14461 }
14462 break;
14463
14464 default:
14465 gcc_unreachable ();
14466 }
14467
14468 /* There can be no default arguments. */
14469 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg))
14470 if (TREE_PURPOSE (arg))
14471 {
14472 TREE_PURPOSE (arg) = NULL_TREE;
14473 error_at (loc, "%qD cannot have default arguments", decl);
14474 return false;
14475 }
14476
14477 /* At this point the declaration is well-formed. It may not be
14478 sensible though. */
14479
14480 /* Check member function warnings only on the in-class declaration.
14481 There's no point warning on an out-of-class definition. */
14482 if (class_type && class_type != current_class_type)
14483 return true;
14484
14485 /* Warn about conversion operators that will never be used. */
14486 if (IDENTIFIER_CONV_OP_P (name)
14487 && ! DECL_TEMPLATE_INFO (decl)
14488 && warn_class_conversion)
14489 {
14490 tree t = TREE_TYPE (name);
14491 int ref = TYPE_REF_P (t);
14492
14493 if (ref)
14494 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
14495
14496 if (VOID_TYPE_P (t))
14497 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> "
14498 "will never use a type conversion operator", class_type);
14499 else if (class_type)
14500 {
14501 if (same_type_ignoring_top_level_qualifiers_p (t, class_type))
14502 warning_at (loc, OPT_Wclass_conversion,
14503 ref
14504 ? G_("converting %qT to a reference to the same type "
14505 "will never use a type conversion operator")
14506 : G_("converting %qT to the same type "
14507 "will never use a type conversion operator"),
14508 class_type);
14509 /* Don't force t to be complete here. */
14510 else if (MAYBE_CLASS_TYPE_P (t)
14511 && COMPLETE_TYPE_P (t)
14512 && DERIVED_FROM_P (t, class_type))
14513 warning_at (loc, OPT_Wclass_conversion,
14514 ref
14515 ? G_("converting %qT to a reference to a base class "
14516 "%qT will never use a type conversion operator")
14517 : G_("converting %qT to a base class %qT "
14518 "will never use a type conversion operator"),
14519 class_type, t);
14520 }
14521 }
14522
14523 if (!warn_ecpp)
14524 return true;
14525
14526 /* Effective C++ rules below. */
14527
14528 /* More Effective C++ rule 7. */
14529 if (operator_code == TRUTH_ANDIF_EXPR
14530 || operator_code == TRUTH_ORIF_EXPR
14531 || operator_code == COMPOUND_EXPR)
14532 warning_at (loc, OPT_Weffc__,
14533 "user-defined %qD always evaluates both arguments", decl);
14534
14535 /* More Effective C++ rule 6. */
14536 if (operator_code == POSTINCREMENT_EXPR
14537 || operator_code == POSTDECREMENT_EXPR
14538 || operator_code == PREINCREMENT_EXPR
14539 || operator_code == PREDECREMENT_EXPR)
14540 {
14541 tree arg = TREE_VALUE (argtypes);
14542 tree ret = TREE_TYPE (TREE_TYPE (decl));
14543 if (methodp || TYPE_REF_P (arg))
14544 arg = TREE_TYPE (arg);
14545 arg = TYPE_MAIN_VARIANT (arg);
14546
14547 if (operator_code == PREINCREMENT_EXPR
14548 || operator_code == PREDECREMENT_EXPR)
14549 {
14550 if (!TYPE_REF_P (ret)
14551 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg))
14552 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl,
14553 build_reference_type (arg));
14554 }
14555 else
14556 {
14557 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
14558 warning_at (loc, OPT_Weffc__,
14559 "postfix %qD should return %qT", decl, arg);
14560 }
14561 }
14562
14563 /* Effective C++ rule 23. */
14564 if (!DECL_ASSIGNMENT_OPERATOR_P (decl)
14565 && (operator_code == PLUS_EXPR
14566 || operator_code == MINUS_EXPR
14567 || operator_code == TRUNC_DIV_EXPR
14568 || operator_code == MULT_EXPR
14569 || operator_code == TRUNC_MOD_EXPR)
14570 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl))))
14571 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl);
14572
14573 return true;
14574 }
14575 \f
14576 /* Return a string giving the keyword associate with CODE. */
14577
14578 static const char *
14579 tag_name (enum tag_types code)
14580 {
14581 switch (code)
14582 {
14583 case record_type:
14584 return "struct";
14585 case class_type:
14586 return "class";
14587 case union_type:
14588 return "union";
14589 case enum_type:
14590 return "enum";
14591 case typename_type:
14592 return "typename";
14593 default:
14594 gcc_unreachable ();
14595 }
14596 }
14597
14598 /* Name lookup in an elaborated-type-specifier (after the keyword
14599 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
14600 elaborated-type-specifier is invalid, issue a diagnostic and return
14601 error_mark_node; otherwise, return the *_TYPE to which it referred.
14602 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
14603
14604 tree
14605 check_elaborated_type_specifier (enum tag_types tag_code,
14606 tree decl,
14607 bool allow_template_p)
14608 {
14609 tree type;
14610
14611 /* In the case of:
14612
14613 struct S { struct S *p; };
14614
14615 name lookup will find the TYPE_DECL for the implicit "S::S"
14616 typedef. Adjust for that here. */
14617 if (DECL_SELF_REFERENCE_P (decl))
14618 decl = TYPE_NAME (TREE_TYPE (decl));
14619
14620 type = TREE_TYPE (decl);
14621
14622 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
14623 is false for this case as well. */
14624 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14625 {
14626 error ("using template type parameter %qT after %qs",
14627 type, tag_name (tag_code));
14628 return error_mark_node;
14629 }
14630 /* Accept template template parameters. */
14631 else if (allow_template_p
14632 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
14633 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
14634 ;
14635 /* [dcl.type.elab]
14636
14637 If the identifier resolves to a typedef-name or the
14638 simple-template-id resolves to an alias template
14639 specialization, the elaborated-type-specifier is ill-formed.
14640
14641 In other words, the only legitimate declaration to use in the
14642 elaborated type specifier is the implicit typedef created when
14643 the type is declared. */
14644 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
14645 && !DECL_SELF_REFERENCE_P (decl)
14646 && tag_code != typename_type)
14647 {
14648 if (alias_template_specialization_p (type, nt_opaque))
14649 error ("using alias template specialization %qT after %qs",
14650 type, tag_name (tag_code));
14651 else
14652 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
14653 inform (DECL_SOURCE_LOCATION (decl),
14654 "%qD has a previous declaration here", decl);
14655 return error_mark_node;
14656 }
14657 else if (TREE_CODE (type) != RECORD_TYPE
14658 && TREE_CODE (type) != UNION_TYPE
14659 && tag_code != enum_type
14660 && tag_code != typename_type)
14661 {
14662 error ("%qT referred to as %qs", type, tag_name (tag_code));
14663 inform (location_of (type), "%qT has a previous declaration here", type);
14664 return error_mark_node;
14665 }
14666 else if (TREE_CODE (type) != ENUMERAL_TYPE
14667 && tag_code == enum_type)
14668 {
14669 error ("%qT referred to as enum", type);
14670 inform (location_of (type), "%qT has a previous declaration here", type);
14671 return error_mark_node;
14672 }
14673 else if (!allow_template_p
14674 && TREE_CODE (type) == RECORD_TYPE
14675 && CLASSTYPE_IS_TEMPLATE (type))
14676 {
14677 /* If a class template appears as elaborated type specifier
14678 without a template header such as:
14679
14680 template <class T> class C {};
14681 void f(class C); // No template header here
14682
14683 then the required template argument is missing. */
14684 error ("template argument required for %<%s %T%>",
14685 tag_name (tag_code),
14686 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
14687 return error_mark_node;
14688 }
14689
14690 return type;
14691 }
14692
14693 /* Lookup NAME in elaborate type specifier in scope according to
14694 SCOPE and issue diagnostics if necessary.
14695 Return *_TYPE node upon success, NULL_TREE when the NAME is not
14696 found, and ERROR_MARK_NODE for type error. */
14697
14698 static tree
14699 lookup_and_check_tag (enum tag_types tag_code, tree name,
14700 tag_scope scope, bool template_header_p)
14701 {
14702 tree t;
14703 tree decl;
14704 if (scope == ts_global)
14705 {
14706 /* First try ordinary name lookup, ignoring hidden class name
14707 injected via friend declaration. */
14708 decl = lookup_name_prefer_type (name, 2);
14709 decl = strip_using_decl (decl);
14710 /* If that fails, the name will be placed in the smallest
14711 non-class, non-function-prototype scope according to 3.3.1/5.
14712 We may already have a hidden name declared as friend in this
14713 scope. So lookup again but not ignoring hidden names.
14714 If we find one, that name will be made visible rather than
14715 creating a new tag. */
14716 if (!decl)
14717 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
14718 }
14719 else
14720 decl = lookup_type_scope (name, scope);
14721
14722 if (decl
14723 && (DECL_CLASS_TEMPLATE_P (decl)
14724 /* If scope is ts_current we're defining a class, so ignore a
14725 template template parameter. */
14726 || (scope != ts_current
14727 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
14728 decl = DECL_TEMPLATE_RESULT (decl);
14729
14730 if (decl && TREE_CODE (decl) == TYPE_DECL)
14731 {
14732 /* Look for invalid nested type:
14733 class C {
14734 class C {};
14735 }; */
14736 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
14737 {
14738 error ("%qD has the same name as the class in which it is "
14739 "declared",
14740 decl);
14741 return error_mark_node;
14742 }
14743
14744 /* Two cases we need to consider when deciding if a class
14745 template is allowed as an elaborated type specifier:
14746 1. It is a self reference to its own class.
14747 2. It comes with a template header.
14748
14749 For example:
14750
14751 template <class T> class C {
14752 class C *c1; // DECL_SELF_REFERENCE_P is true
14753 class D;
14754 };
14755 template <class U> class C; // template_header_p is true
14756 template <class T> class C<T>::D {
14757 class C *c2; // DECL_SELF_REFERENCE_P is true
14758 }; */
14759
14760 t = check_elaborated_type_specifier (tag_code,
14761 decl,
14762 template_header_p
14763 | DECL_SELF_REFERENCE_P (decl));
14764 if (template_header_p && t && CLASS_TYPE_P (t)
14765 && (!CLASSTYPE_TEMPLATE_INFO (t)
14766 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
14767 {
14768 error ("%qT is not a template", t);
14769 inform (location_of (t), "previous declaration here");
14770 if (TYPE_CLASS_SCOPE_P (t)
14771 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
14772 inform (input_location,
14773 "perhaps you want to explicitly add %<%T::%>",
14774 TYPE_CONTEXT (t));
14775 t = error_mark_node;
14776 }
14777
14778 return t;
14779 }
14780 else if (decl && TREE_CODE (decl) == TREE_LIST)
14781 {
14782 error ("reference to %qD is ambiguous", name);
14783 print_candidates (decl);
14784 return error_mark_node;
14785 }
14786 else
14787 return NULL_TREE;
14788 }
14789
14790 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
14791 Define the tag as a forward-reference if it is not defined.
14792
14793 If a declaration is given, process it here, and report an error if
14794 multiple declarations are not identical.
14795
14796 SCOPE is TS_CURRENT when this is also a definition. Only look in
14797 the current frame for the name (since C++ allows new names in any
14798 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
14799 declaration. Only look beginning from the current scope outward up
14800 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
14801
14802 TEMPLATE_HEADER_P is true when this declaration is preceded by
14803 a set of template parameters. */
14804
14805 static tree
14806 xref_tag_1 (enum tag_types tag_code, tree name,
14807 tag_scope scope, bool template_header_p)
14808 {
14809 enum tree_code code;
14810 tree context = NULL_TREE;
14811
14812 gcc_assert (identifier_p (name));
14813
14814 switch (tag_code)
14815 {
14816 case record_type:
14817 case class_type:
14818 code = RECORD_TYPE;
14819 break;
14820 case union_type:
14821 code = UNION_TYPE;
14822 break;
14823 case enum_type:
14824 code = ENUMERAL_TYPE;
14825 break;
14826 default:
14827 gcc_unreachable ();
14828 }
14829
14830 /* In case of anonymous name, xref_tag is only called to
14831 make type node and push name. Name lookup is not required. */
14832 tree t = NULL_TREE;
14833 if (scope != ts_lambda && !IDENTIFIER_ANON_P (name))
14834 t = lookup_and_check_tag (tag_code, name, scope, template_header_p);
14835
14836 if (t == error_mark_node)
14837 return error_mark_node;
14838
14839 if (scope != ts_current && t && current_class_type
14840 && template_class_depth (current_class_type)
14841 && template_header_p)
14842 {
14843 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
14844 return t;
14845
14846 /* Since SCOPE is not TS_CURRENT, we are not looking at a
14847 definition of this tag. Since, in addition, we are currently
14848 processing a (member) template declaration of a template
14849 class, we must be very careful; consider:
14850
14851 template <class X> struct S1
14852
14853 template <class U> struct S2
14854 {
14855 template <class V> friend struct S1;
14856 };
14857
14858 Here, the S2::S1 declaration should not be confused with the
14859 outer declaration. In particular, the inner version should
14860 have a template parameter of level 2, not level 1.
14861
14862 On the other hand, when presented with:
14863
14864 template <class T> struct S1
14865 {
14866 template <class U> struct S2 {};
14867 template <class U> friend struct S2;
14868 };
14869
14870 the friend must find S1::S2 eventually. We accomplish this
14871 by making sure that the new type we create to represent this
14872 declaration has the right TYPE_CONTEXT. */
14873 context = TYPE_CONTEXT (t);
14874 t = NULL_TREE;
14875 }
14876
14877 if (! t)
14878 {
14879 /* If no such tag is yet defined, create a forward-reference node
14880 and record it as the "definition".
14881 When a real declaration of this type is found,
14882 the forward-reference will be altered into a real type. */
14883 if (code == ENUMERAL_TYPE)
14884 {
14885 error ("use of enum %q#D without previous declaration", name);
14886 return error_mark_node;
14887 }
14888 else
14889 {
14890 t = make_class_type (code);
14891 TYPE_CONTEXT (t) = context;
14892 if (scope == ts_lambda)
14893 {
14894 /* Mark it as a lambda type. */
14895 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
14896 /* And push it into current scope. */
14897 scope = ts_current;
14898 }
14899 t = pushtag (name, t, scope);
14900 }
14901 }
14902 else
14903 {
14904 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
14905 {
14906 /* Check that we aren't trying to overload a class with different
14907 constraints. */
14908 tree constr = NULL_TREE;
14909 if (current_template_parms)
14910 {
14911 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
14912 constr = build_constraints (reqs, NULL_TREE);
14913 }
14914 if (!redeclare_class_template (t, current_template_parms, constr))
14915 return error_mark_node;
14916 }
14917 else if (!processing_template_decl
14918 && CLASS_TYPE_P (t)
14919 && CLASSTYPE_IS_TEMPLATE (t))
14920 {
14921 error ("redeclaration of %qT as a non-template", t);
14922 inform (location_of (t), "previous declaration %qD", t);
14923 return error_mark_node;
14924 }
14925
14926 if (scope != ts_within_enclosing_non_class && TYPE_HIDDEN_P (t))
14927 {
14928 /* This is no longer an invisible friend. Make it
14929 visible. */
14930 tree decl = TYPE_NAME (t);
14931
14932 DECL_ANTICIPATED (decl) = false;
14933 DECL_FRIEND_P (decl) = false;
14934
14935 if (TYPE_TEMPLATE_INFO (t))
14936 {
14937 tree tmpl = TYPE_TI_TEMPLATE (t);
14938 DECL_ANTICIPATED (tmpl) = false;
14939 DECL_FRIEND_P (tmpl) = false;
14940 }
14941 }
14942 }
14943
14944 return t;
14945 }
14946
14947 /* Wrapper for xref_tag_1. */
14948
14949 tree
14950 xref_tag (enum tag_types tag_code, tree name,
14951 tag_scope scope, bool template_header_p)
14952 {
14953 tree ret;
14954 bool subtime;
14955 subtime = timevar_cond_start (TV_NAME_LOOKUP);
14956 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
14957 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
14958 return ret;
14959 }
14960
14961
14962 tree
14963 xref_tag_from_type (tree old, tree id, tag_scope scope)
14964 {
14965 enum tag_types tag_kind;
14966
14967 if (TREE_CODE (old) == RECORD_TYPE)
14968 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
14969 else
14970 tag_kind = union_type;
14971
14972 if (id == NULL_TREE)
14973 id = TYPE_IDENTIFIER (old);
14974
14975 return xref_tag (tag_kind, id, scope, false);
14976 }
14977
14978 /* Create the binfo hierarchy for REF with (possibly NULL) base list
14979 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
14980 access_* node, and the TREE_VALUE is the type of the base-class.
14981 Non-NULL TREE_TYPE indicates virtual inheritance. */
14982
14983 void
14984 xref_basetypes (tree ref, tree base_list)
14985 {
14986 tree *basep;
14987 tree binfo, base_binfo;
14988 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
14989 unsigned max_bases = 0; /* Maximum direct bases. */
14990 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
14991 int i;
14992 tree default_access;
14993 tree igo_prev; /* Track Inheritance Graph Order. */
14994
14995 if (ref == error_mark_node)
14996 return;
14997
14998 /* The base of a derived class is private by default, all others are
14999 public. */
15000 default_access = (TREE_CODE (ref) == RECORD_TYPE
15001 && CLASSTYPE_DECLARED_CLASS (ref)
15002 ? access_private_node : access_public_node);
15003
15004 /* First, make sure that any templates in base-classes are
15005 instantiated. This ensures that if we call ourselves recursively
15006 we do not get confused about which classes are marked and which
15007 are not. */
15008 basep = &base_list;
15009 while (*basep)
15010 {
15011 tree basetype = TREE_VALUE (*basep);
15012
15013 /* The dependent_type_p call below should really be dependent_scope_p
15014 so that we give a hard error about using an incomplete type as a
15015 base, but we allow it with a pedwarn for backward
15016 compatibility. */
15017 if (processing_template_decl
15018 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
15019 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
15020 if (!dependent_type_p (basetype)
15021 && !complete_type_or_else (basetype, NULL))
15022 /* An incomplete type. Remove it from the list. */
15023 *basep = TREE_CHAIN (*basep);
15024 else
15025 {
15026 max_bases++;
15027 if (TREE_TYPE (*basep))
15028 max_dvbases++;
15029 if (CLASS_TYPE_P (basetype))
15030 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15031 basep = &TREE_CHAIN (*basep);
15032 }
15033 }
15034 max_vbases += max_dvbases;
15035
15036 TYPE_MARKED_P (ref) = 1;
15037
15038 /* The binfo slot should be empty, unless this is an (ill-formed)
15039 redefinition. */
15040 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
15041
15042 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
15043
15044 binfo = make_tree_binfo (max_bases);
15045
15046 TYPE_BINFO (ref) = binfo;
15047 BINFO_OFFSET (binfo) = size_zero_node;
15048 BINFO_TYPE (binfo) = ref;
15049
15050 /* Apply base-class info set up to the variants of this type. */
15051 fixup_type_variants (ref);
15052
15053 if (max_bases)
15054 {
15055 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
15056 /* A C++98 POD cannot have base classes. */
15057 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true;
15058
15059 if (TREE_CODE (ref) == UNION_TYPE)
15060 {
15061 error ("derived union %qT invalid", ref);
15062 return;
15063 }
15064 }
15065
15066 if (max_bases > 1)
15067 warning (OPT_Wmultiple_inheritance,
15068 "%qT defined with multiple direct bases", ref);
15069
15070 if (max_vbases)
15071 {
15072 /* An aggregate can't have virtual base classes. */
15073 CLASSTYPE_NON_AGGREGATE (ref) = true;
15074
15075 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
15076
15077 if (max_dvbases)
15078 warning (OPT_Wvirtual_inheritance,
15079 "%qT defined with direct virtual base", ref);
15080 }
15081
15082 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
15083 {
15084 tree access = TREE_PURPOSE (base_list);
15085 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
15086 tree basetype = TREE_VALUE (base_list);
15087
15088 if (access == access_default_node)
15089 access = default_access;
15090
15091 /* Before C++17, an aggregate cannot have base classes. In C++17, an
15092 aggregate can't have virtual, private, or protected base classes. */
15093 if (cxx_dialect < cxx17
15094 || access != access_public_node
15095 || via_virtual)
15096 CLASSTYPE_NON_AGGREGATE (ref) = true;
15097
15098 if (PACK_EXPANSION_P (basetype))
15099 basetype = PACK_EXPANSION_PATTERN (basetype);
15100 if (TREE_CODE (basetype) == TYPE_DECL)
15101 basetype = TREE_TYPE (basetype);
15102 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
15103 {
15104 error ("base type %qT fails to be a struct or class type",
15105 basetype);
15106 goto dropped_base;
15107 }
15108
15109 base_binfo = NULL_TREE;
15110 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
15111 {
15112 base_binfo = TYPE_BINFO (basetype);
15113 /* The original basetype could have been a typedef'd type. */
15114 basetype = BINFO_TYPE (base_binfo);
15115
15116 /* Inherit flags from the base. */
15117 TYPE_HAS_NEW_OPERATOR (ref)
15118 |= TYPE_HAS_NEW_OPERATOR (basetype);
15119 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
15120 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
15121 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
15122 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
15123 CLASSTYPE_DIAMOND_SHAPED_P (ref)
15124 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
15125 CLASSTYPE_REPEATED_BASE_P (ref)
15126 |= CLASSTYPE_REPEATED_BASE_P (basetype);
15127 }
15128
15129 /* We must do this test after we've seen through a typedef
15130 type. */
15131 if (TYPE_MARKED_P (basetype))
15132 {
15133 if (basetype == ref)
15134 error ("recursive type %qT undefined", basetype);
15135 else
15136 error ("duplicate base type %qT invalid", basetype);
15137 goto dropped_base;
15138 }
15139
15140 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
15141 /* Regenerate the pack expansion for the bases. */
15142 basetype = make_pack_expansion (basetype);
15143
15144 TYPE_MARKED_P (basetype) = 1;
15145
15146 base_binfo = copy_binfo (base_binfo, basetype, ref,
15147 &igo_prev, via_virtual);
15148 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
15149 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
15150
15151 BINFO_BASE_APPEND (binfo, base_binfo);
15152 BINFO_BASE_ACCESS_APPEND (binfo, access);
15153 continue;
15154
15155 dropped_base:
15156 /* Update max_vbases to reflect the reality that we are dropping
15157 this base: if it reaches zero we want to undo the vec_alloc
15158 above to avoid inconsistencies during error-recovery: eg, in
15159 build_special_member_call, CLASSTYPE_VBASECLASSES non null
15160 and vtt null (c++/27952). */
15161 if (via_virtual)
15162 max_vbases--;
15163 if (CLASS_TYPE_P (basetype))
15164 max_vbases
15165 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
15166 }
15167
15168 if (CLASSTYPE_VBASECLASSES (ref)
15169 && max_vbases == 0)
15170 vec_free (CLASSTYPE_VBASECLASSES (ref));
15171
15172 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
15173 /* If we didn't get max_vbases vbases, we must have shared at
15174 least one of them, and are therefore diamond shaped. */
15175 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
15176
15177 /* Unmark all the types. */
15178 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
15179 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15180 TYPE_MARKED_P (ref) = 0;
15181
15182 /* Now see if we have a repeated base type. */
15183 if (!CLASSTYPE_REPEATED_BASE_P (ref))
15184 {
15185 for (base_binfo = binfo; base_binfo;
15186 base_binfo = TREE_CHAIN (base_binfo))
15187 {
15188 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15189 {
15190 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
15191 break;
15192 }
15193 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
15194 }
15195 for (base_binfo = binfo; base_binfo;
15196 base_binfo = TREE_CHAIN (base_binfo))
15197 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
15198 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
15199 else
15200 break;
15201 }
15202 }
15203
15204 \f
15205 /* Copies the enum-related properties from type SRC to type DST.
15206 Used with the underlying type of an enum and the enum itself. */
15207 static void
15208 copy_type_enum (tree dst, tree src)
15209 {
15210 tree t;
15211 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
15212 {
15213 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
15214 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
15215 TYPE_SIZE (t) = TYPE_SIZE (src);
15216 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
15217 SET_TYPE_MODE (dst, TYPE_MODE (src));
15218 TYPE_PRECISION (t) = TYPE_PRECISION (src);
15219 unsigned valign = TYPE_ALIGN (src);
15220 if (TYPE_USER_ALIGN (t))
15221 valign = MAX (valign, TYPE_ALIGN (t));
15222 else
15223 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
15224 SET_TYPE_ALIGN (t, valign);
15225 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
15226 }
15227 }
15228
15229 /* Begin compiling the definition of an enumeration type.
15230 NAME is its name,
15231
15232 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
15233
15234 UNDERLYING_TYPE is the type that will be used as the storage for
15235 the enumeration type. This should be NULL_TREE if no storage type
15236 was specified.
15237
15238 ATTRIBUTES are any attributes specified after the enum-key.
15239
15240 SCOPED_ENUM_P is true if this is a scoped enumeration type.
15241
15242 if IS_NEW is not NULL, gets TRUE iff a new type is created.
15243
15244 Returns the type object, as yet incomplete.
15245 Also records info about it so that build_enumerator
15246 may be used to declare the individual values as they are read. */
15247
15248 tree
15249 start_enum (tree name, tree enumtype, tree underlying_type,
15250 tree attributes, bool scoped_enum_p, bool *is_new)
15251 {
15252 tree prevtype = NULL_TREE;
15253 gcc_assert (identifier_p (name));
15254
15255 if (is_new)
15256 *is_new = false;
15257 /* [C++0x dcl.enum]p5:
15258
15259 If not explicitly specified, the underlying type of a scoped
15260 enumeration type is int. */
15261 if (!underlying_type && scoped_enum_p)
15262 underlying_type = integer_type_node;
15263
15264 if (underlying_type)
15265 underlying_type = cv_unqualified (underlying_type);
15266
15267 /* If this is the real definition for a previous forward reference,
15268 fill in the contents in the same object that used to be the
15269 forward reference. */
15270 if (!enumtype)
15271 enumtype = lookup_and_check_tag (enum_type, name,
15272 /*tag_scope=*/ts_current,
15273 /*template_header_p=*/false);
15274
15275 /* In case of a template_decl, the only check that should be deferred
15276 to instantiation time is the comparison of underlying types. */
15277 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
15278 {
15279 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
15280 {
15281 error_at (input_location, "scoped/unscoped mismatch "
15282 "in enum %q#T", enumtype);
15283 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15284 "previous definition here");
15285 enumtype = error_mark_node;
15286 }
15287 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
15288 {
15289 error_at (input_location, "underlying type mismatch "
15290 "in enum %q#T", enumtype);
15291 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15292 "previous definition here");
15293 enumtype = error_mark_node;
15294 }
15295 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
15296 && !same_type_p (underlying_type,
15297 ENUM_UNDERLYING_TYPE (enumtype)))
15298 {
15299 error_at (input_location, "different underlying type "
15300 "in enum %q#T", enumtype);
15301 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15302 "previous definition here");
15303 underlying_type = NULL_TREE;
15304 }
15305 }
15306
15307 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
15308 || processing_template_decl)
15309 {
15310 /* In case of error, make a dummy enum to allow parsing to
15311 continue. */
15312 if (enumtype == error_mark_node)
15313 {
15314 name = make_anon_name ();
15315 enumtype = NULL_TREE;
15316 }
15317
15318 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
15319 of an opaque enum, or an opaque enum of an already defined
15320 enumeration (C++11).
15321 In any other case, it'll be NULL_TREE. */
15322 if (!enumtype)
15323 {
15324 if (is_new)
15325 *is_new = true;
15326 }
15327 prevtype = enumtype;
15328
15329 /* Do not push the decl more than once. */
15330 if (!enumtype
15331 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
15332 {
15333 enumtype = cxx_make_type (ENUMERAL_TYPE);
15334 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
15335
15336 /* std::byte aliases anything. */
15337 if (enumtype != error_mark_node
15338 && TYPE_CONTEXT (enumtype) == std_node
15339 && !strcmp ("byte", TYPE_NAME_STRING (enumtype)))
15340 TYPE_ALIAS_SET (enumtype) = 0;
15341 }
15342 else
15343 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
15344 false);
15345
15346 if (enumtype == error_mark_node)
15347 return error_mark_node;
15348
15349 /* The enum is considered opaque until the opening '{' of the
15350 enumerator list. */
15351 SET_OPAQUE_ENUM_P (enumtype, true);
15352 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
15353 }
15354
15355 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
15356
15357 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
15358
15359 if (underlying_type)
15360 {
15361 if (ENUM_UNDERLYING_TYPE (enumtype))
15362 /* We already checked that it matches, don't change it to a different
15363 typedef variant. */;
15364 else if (CP_INTEGRAL_TYPE_P (underlying_type))
15365 {
15366 copy_type_enum (enumtype, underlying_type);
15367 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15368 }
15369 else if (dependent_type_p (underlying_type))
15370 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
15371 else
15372 error ("underlying type %qT of %qT must be an integral type",
15373 underlying_type, enumtype);
15374 }
15375
15376 /* If into a template class, the returned enum is always the first
15377 declaration (opaque or not) seen. This way all the references to
15378 this type will be to the same declaration. The following ones are used
15379 only to check for definition errors. */
15380 if (prevtype && processing_template_decl)
15381 return prevtype;
15382 else
15383 return enumtype;
15384 }
15385
15386 /* After processing and defining all the values of an enumeration type,
15387 install their decls in the enumeration type.
15388 ENUMTYPE is the type object. */
15389
15390 void
15391 finish_enum_value_list (tree enumtype)
15392 {
15393 tree values;
15394 tree underlying_type;
15395 tree decl;
15396 tree value;
15397 tree minnode, maxnode;
15398 tree t;
15399
15400 bool fixed_underlying_type_p
15401 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
15402
15403 /* We built up the VALUES in reverse order. */
15404 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
15405
15406 /* For an enum defined in a template, just set the type of the values;
15407 all further processing is postponed until the template is
15408 instantiated. We need to set the type so that tsubst of a CONST_DECL
15409 works. */
15410 if (processing_template_decl)
15411 {
15412 for (values = TYPE_VALUES (enumtype);
15413 values;
15414 values = TREE_CHAIN (values))
15415 TREE_TYPE (TREE_VALUE (values)) = enumtype;
15416 return;
15417 }
15418
15419 /* Determine the minimum and maximum values of the enumerators. */
15420 if (TYPE_VALUES (enumtype))
15421 {
15422 minnode = maxnode = NULL_TREE;
15423
15424 for (values = TYPE_VALUES (enumtype);
15425 values;
15426 values = TREE_CHAIN (values))
15427 {
15428 decl = TREE_VALUE (values);
15429
15430 /* [dcl.enum]: Following the closing brace of an enum-specifier,
15431 each enumerator has the type of its enumeration. Prior to the
15432 closing brace, the type of each enumerator is the type of its
15433 initializing value. */
15434 TREE_TYPE (decl) = enumtype;
15435
15436 /* Update the minimum and maximum values, if appropriate. */
15437 value = DECL_INITIAL (decl);
15438 if (value == error_mark_node)
15439 value = integer_zero_node;
15440 /* Figure out what the minimum and maximum values of the
15441 enumerators are. */
15442 if (!minnode)
15443 minnode = maxnode = value;
15444 else if (tree_int_cst_lt (maxnode, value))
15445 maxnode = value;
15446 else if (tree_int_cst_lt (value, minnode))
15447 minnode = value;
15448 }
15449 }
15450 else
15451 /* [dcl.enum]
15452
15453 If the enumerator-list is empty, the underlying type is as if
15454 the enumeration had a single enumerator with value 0. */
15455 minnode = maxnode = integer_zero_node;
15456
15457 if (!fixed_underlying_type_p)
15458 {
15459 /* Compute the number of bits require to represent all values of the
15460 enumeration. We must do this before the type of MINNODE and
15461 MAXNODE are transformed, since tree_int_cst_min_precision relies
15462 on the TREE_TYPE of the value it is passed. */
15463 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
15464 int lowprec = tree_int_cst_min_precision (minnode, sgn);
15465 int highprec = tree_int_cst_min_precision (maxnode, sgn);
15466 int precision = MAX (lowprec, highprec);
15467 unsigned int itk;
15468 bool use_short_enum;
15469
15470 /* Determine the underlying type of the enumeration.
15471
15472 [dcl.enum]
15473
15474 The underlying type of an enumeration is an integral type that
15475 can represent all the enumerator values defined in the
15476 enumeration. It is implementation-defined which integral type is
15477 used as the underlying type for an enumeration except that the
15478 underlying type shall not be larger than int unless the value of
15479 an enumerator cannot fit in an int or unsigned int.
15480
15481 We use "int" or an "unsigned int" as the underlying type, even if
15482 a smaller integral type would work, unless the user has
15483 explicitly requested that we use the smallest possible type. The
15484 user can request that for all enumerations with a command line
15485 flag, or for just one enumeration with an attribute. */
15486
15487 use_short_enum = flag_short_enums
15488 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
15489
15490 /* If the precision of the type was specified with an attribute and it
15491 was too small, give an error. Otherwise, use it. */
15492 if (TYPE_PRECISION (enumtype))
15493 {
15494 if (precision > TYPE_PRECISION (enumtype))
15495 error ("specified mode too small for enumerated values");
15496 else
15497 {
15498 use_short_enum = true;
15499 precision = TYPE_PRECISION (enumtype);
15500 }
15501 }
15502
15503 for (itk = (use_short_enum ? itk_char : itk_int);
15504 itk != itk_none;
15505 itk++)
15506 {
15507 underlying_type = integer_types[itk];
15508 if (underlying_type != NULL_TREE
15509 && TYPE_PRECISION (underlying_type) >= precision
15510 && TYPE_SIGN (underlying_type) == sgn)
15511 break;
15512 }
15513 if (itk == itk_none)
15514 {
15515 /* DR 377
15516
15517 IF no integral type can represent all the enumerator values, the
15518 enumeration is ill-formed. */
15519 error ("no integral type can represent all of the enumerator values "
15520 "for %qT", enumtype);
15521 precision = TYPE_PRECISION (long_long_integer_type_node);
15522 underlying_type = integer_types[itk_unsigned_long_long];
15523 }
15524
15525 /* [dcl.enum]
15526
15527 The value of sizeof() applied to an enumeration type, an object
15528 of an enumeration type, or an enumerator, is the value of sizeof()
15529 applied to the underlying type. */
15530 copy_type_enum (enumtype, underlying_type);
15531
15532 /* Compute the minimum and maximum values for the type.
15533
15534 [dcl.enum]
15535
15536 For an enumeration where emin is the smallest enumerator and emax
15537 is the largest, the values of the enumeration are the values of the
15538 underlying type in the range bmin to bmax, where bmin and bmax are,
15539 respectively, the smallest and largest values of the smallest bit-
15540 field that can store emin and emax. */
15541
15542 /* The middle-end currently assumes that types with TYPE_PRECISION
15543 narrower than their underlying type are suitably zero or sign
15544 extended to fill their mode. Similarly, it assumes that the front
15545 end assures that a value of a particular type must be within
15546 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
15547
15548 We used to set these fields based on bmin and bmax, but that led
15549 to invalid assumptions like optimizing away bounds checking. So
15550 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
15551 TYPE_MAX_VALUE to the values for the mode above and only restrict
15552 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
15553 ENUM_UNDERLYING_TYPE (enumtype)
15554 = build_distinct_type_copy (underlying_type);
15555 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
15556 set_min_and_max_values_for_integral_type
15557 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
15558
15559 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
15560 if (flag_strict_enums)
15561 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
15562 }
15563 else
15564 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
15565
15566 /* Convert each of the enumerators to the type of the underlying
15567 type of the enumeration. */
15568 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
15569 {
15570 decl = TREE_VALUE (values);
15571 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl));
15572 if (fixed_underlying_type_p)
15573 /* If the enumeration type has a fixed underlying type, we
15574 already checked all of the enumerator values. */
15575 value = DECL_INITIAL (decl);
15576 else
15577 value = perform_implicit_conversion (underlying_type,
15578 DECL_INITIAL (decl),
15579 tf_warning_or_error);
15580 /* Do not clobber shared ints. */
15581 if (value != error_mark_node)
15582 {
15583 value = copy_node (value);
15584
15585 TREE_TYPE (value) = enumtype;
15586 }
15587 DECL_INITIAL (decl) = value;
15588 }
15589
15590 /* Fix up all variant types of this enum type. */
15591 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
15592 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
15593
15594 if (at_class_scope_p ()
15595 && COMPLETE_TYPE_P (current_class_type)
15596 && UNSCOPED_ENUM_P (enumtype))
15597 {
15598 insert_late_enum_def_bindings (current_class_type, enumtype);
15599 /* TYPE_FIELDS needs fixup. */
15600 fixup_type_variants (current_class_type);
15601 }
15602
15603 /* Finish debugging output for this type. */
15604 rest_of_type_compilation (enumtype, namespace_bindings_p ());
15605
15606 /* Each enumerator now has the type of its enumeration. Clear the cache
15607 so that this change in types doesn't confuse us later on. */
15608 clear_cv_and_fold_caches ();
15609 }
15610
15611 /* Finishes the enum type. This is called only the first time an
15612 enumeration is seen, be it opaque or odinary.
15613 ENUMTYPE is the type object. */
15614
15615 void
15616 finish_enum (tree enumtype)
15617 {
15618 if (processing_template_decl)
15619 {
15620 if (at_function_scope_p ())
15621 add_stmt (build_min (TAG_DEFN, enumtype));
15622 return;
15623 }
15624
15625 /* If this is a forward declaration, there should not be any variants,
15626 though we can get a variant in the middle of an enum-specifier with
15627 wacky code like 'enum E { e = sizeof(const E*) };' */
15628 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
15629 && (TYPE_VALUES (enumtype)
15630 || !TYPE_NEXT_VARIANT (enumtype)));
15631 }
15632
15633 /* Build and install a CONST_DECL for an enumeration constant of the
15634 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
15635 Apply ATTRIBUTES if available. LOC is the location of NAME.
15636 Assignment of sequential values by default is handled here. */
15637
15638 void
15639 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
15640 location_t loc)
15641 {
15642 tree decl;
15643 tree context;
15644 tree type;
15645
15646 /* scalar_constant_value will pull out this expression, so make sure
15647 it's folded as appropriate. */
15648 if (processing_template_decl)
15649 value = fold_non_dependent_expr (value);
15650
15651 /* If the VALUE was erroneous, pretend it wasn't there; that will
15652 result in the enum being assigned the next value in sequence. */
15653 if (value == error_mark_node)
15654 value = NULL_TREE;
15655
15656 /* Remove no-op casts from the value. */
15657 if (value)
15658 STRIP_TYPE_NOPS (value);
15659
15660 if (! processing_template_decl)
15661 {
15662 /* Validate and default VALUE. */
15663 if (value != NULL_TREE)
15664 {
15665 if (!ENUM_UNDERLYING_TYPE (enumtype))
15666 {
15667 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
15668 value, true);
15669 if (tmp_value)
15670 value = tmp_value;
15671 }
15672 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15673 (TREE_TYPE (value)))
15674 value = perform_implicit_conversion_flags
15675 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
15676 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
15677
15678 if (value == error_mark_node)
15679 value = NULL_TREE;
15680
15681 if (value != NULL_TREE)
15682 {
15683 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
15684 (TREE_TYPE (value)))
15685 {
15686 error_at (cp_expr_loc_or_input_loc (value),
15687 "enumerator value for %qD must have integral or "
15688 "unscoped enumeration type", name);
15689 value = NULL_TREE;
15690 }
15691 else
15692 {
15693 value = cxx_constant_value (value);
15694
15695 if (TREE_CODE (value) != INTEGER_CST)
15696 {
15697 error ("enumerator value for %qD is not an integer "
15698 "constant", name);
15699 value = NULL_TREE;
15700 }
15701 }
15702 }
15703 }
15704
15705 /* Default based on previous value. */
15706 if (value == NULL_TREE)
15707 {
15708 if (TYPE_VALUES (enumtype))
15709 {
15710 tree prev_value;
15711
15712 /* C++03 7.2/4: If no initializer is specified for the first
15713 enumerator, the type is an unspecified integral
15714 type. Otherwise the type is the same as the type of the
15715 initializing value of the preceding enumerator unless the
15716 incremented value is not representable in that type, in
15717 which case the type is an unspecified integral type
15718 sufficient to contain the incremented value. */
15719 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
15720 if (error_operand_p (prev_value))
15721 value = error_mark_node;
15722 else
15723 {
15724 wi::overflow_type overflowed;
15725 tree type = TREE_TYPE (prev_value);
15726 signop sgn = TYPE_SIGN (type);
15727 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
15728 &overflowed);
15729 if (!overflowed)
15730 {
15731 bool pos = !wi::neg_p (wi, sgn);
15732 if (!wi::fits_to_tree_p (wi, type))
15733 {
15734 unsigned int itk;
15735 for (itk = itk_int; itk != itk_none; itk++)
15736 {
15737 type = integer_types[itk];
15738 if (type != NULL_TREE
15739 && (pos || !TYPE_UNSIGNED (type))
15740 && wi::fits_to_tree_p (wi, type))
15741 break;
15742 }
15743 if (type && cxx_dialect < cxx11
15744 && itk > itk_unsigned_long)
15745 pedwarn (input_location, OPT_Wlong_long,
15746 pos ? G_("\
15747 incremented enumerator value is too large for %<unsigned long%>") : G_("\
15748 incremented enumerator value is too large for %<long%>"));
15749 }
15750 if (type == NULL_TREE)
15751 overflowed = wi::OVF_UNKNOWN;
15752 else
15753 value = wide_int_to_tree (type, wi);
15754 }
15755
15756 if (overflowed)
15757 {
15758 error ("overflow in enumeration values at %qD", name);
15759 value = error_mark_node;
15760 }
15761 }
15762 }
15763 else
15764 value = integer_zero_node;
15765 }
15766
15767 /* Remove no-op casts from the value. */
15768 STRIP_TYPE_NOPS (value);
15769
15770 /* If the underlying type of the enum is fixed, check whether
15771 the enumerator values fits in the underlying type. If it
15772 does not fit, the program is ill-formed [C++0x dcl.enum]. */
15773 if (ENUM_UNDERLYING_TYPE (enumtype)
15774 && value
15775 && TREE_CODE (value) == INTEGER_CST)
15776 {
15777 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
15778 error ("enumerator value %qE is outside the range of underlying "
15779 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype));
15780
15781 /* Convert the value to the appropriate type. */
15782 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
15783 }
15784 }
15785
15786 /* C++ associates enums with global, function, or class declarations. */
15787 context = current_scope ();
15788
15789 /* Build the actual enumeration constant. Note that the enumeration
15790 constants have the underlying type of the enum (if it is fixed)
15791 or the type of their initializer (if the underlying type of the
15792 enum is not fixed):
15793
15794 [ C++0x dcl.enum ]
15795
15796 If the underlying type is fixed, the type of each enumerator
15797 prior to the closing brace is the underlying type; if the
15798 initializing value of an enumerator cannot be represented by
15799 the underlying type, the program is ill-formed. If the
15800 underlying type is not fixed, the type of each enumerator is
15801 the type of its initializing value.
15802
15803 If the underlying type is not fixed, it will be computed by
15804 finish_enum and we will reset the type of this enumerator. Of
15805 course, if we're processing a template, there may be no value. */
15806 type = value ? TREE_TYPE (value) : NULL_TREE;
15807
15808 decl = build_decl (loc, CONST_DECL, name, type);
15809
15810 DECL_CONTEXT (decl) = enumtype;
15811 TREE_CONSTANT (decl) = 1;
15812 TREE_READONLY (decl) = 1;
15813 DECL_INITIAL (decl) = value;
15814
15815 if (attributes)
15816 cplus_decl_attributes (&decl, attributes, 0);
15817
15818 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
15819 {
15820 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
15821 on the TYPE_FIELDS list for `S'. (That's so that you can say
15822 things like `S::i' later.) */
15823
15824 /* The enumerator may be getting declared outside of its enclosing
15825 class, like so:
15826
15827 class S { public: enum E : int; }; enum S::E : int { i = 7; };
15828
15829 For which case we need to make sure that the access of `S::i'
15830 matches the access of `S::E'. */
15831 tree saved_cas = current_access_specifier;
15832 if (TREE_PRIVATE (TYPE_NAME (enumtype)))
15833 current_access_specifier = access_private_node;
15834 else if (TREE_PROTECTED (TYPE_NAME (enumtype)))
15835 current_access_specifier = access_protected_node;
15836 else
15837 current_access_specifier = access_public_node;
15838
15839 finish_member_declaration (decl);
15840
15841 current_access_specifier = saved_cas;
15842 }
15843 else
15844 pushdecl (decl);
15845
15846 /* Add this enumeration constant to the list for this type. */
15847 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
15848 }
15849
15850 /* Look for an enumerator with the given NAME within the enumeration
15851 type ENUMTYPE. This routine is used primarily for qualified name
15852 lookup into an enumerator in C++0x, e.g.,
15853
15854 enum class Color { Red, Green, Blue };
15855
15856 Color color = Color::Red;
15857
15858 Returns the value corresponding to the enumerator, or
15859 NULL_TREE if no such enumerator was found. */
15860 tree
15861 lookup_enumerator (tree enumtype, tree name)
15862 {
15863 tree e;
15864 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
15865
15866 e = purpose_member (name, TYPE_VALUES (enumtype));
15867 return e? TREE_VALUE (e) : NULL_TREE;
15868 }
15869
15870 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
15871
15872 tree
15873 cxx_simulate_enum_decl (location_t loc, const char *name,
15874 vec<string_int_pair> values)
15875 {
15876 location_t saved_loc = input_location;
15877 input_location = loc;
15878
15879 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE,
15880 NULL_TREE, false, NULL);
15881 if (!OPAQUE_ENUM_P (enumtype))
15882 {
15883 error_at (loc, "multiple definition of %q#T", enumtype);
15884 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
15885 "previous definition here");
15886 return enumtype;
15887 }
15888 SET_OPAQUE_ENUM_P (enumtype, false);
15889 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc;
15890
15891 string_int_pair *value;
15892 unsigned int i;
15893 FOR_EACH_VEC_ELT (values, i, value)
15894 build_enumerator (get_identifier (value->first),
15895 build_int_cst (integer_type_node, value->second),
15896 enumtype, NULL_TREE, loc);
15897
15898 finish_enum_value_list (enumtype);
15899 finish_enum (enumtype);
15900
15901 input_location = saved_loc;
15902 return enumtype;
15903 }
15904 \f
15905 /* We're defining DECL. Make sure that its type is OK. */
15906
15907 static void
15908 check_function_type (tree decl, tree current_function_parms)
15909 {
15910 tree fntype = TREE_TYPE (decl);
15911 tree return_type = complete_type (TREE_TYPE (fntype));
15912
15913 /* In a function definition, arg types must be complete. */
15914 require_complete_types_for_parms (current_function_parms);
15915
15916 if (dependent_type_p (return_type)
15917 || type_uses_auto (return_type))
15918 return;
15919 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
15920 {
15921 tree args = TYPE_ARG_TYPES (fntype);
15922
15923 error ("return type %q#T is incomplete", return_type);
15924
15925 /* Make it return void instead. */
15926 if (TREE_CODE (fntype) == METHOD_TYPE)
15927 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
15928 void_type_node,
15929 TREE_CHAIN (args));
15930 else
15931 fntype = build_function_type (void_type_node, args);
15932 fntype = (cp_build_type_attribute_variant
15933 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
15934 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl));
15935 TREE_TYPE (decl) = fntype;
15936 }
15937 else
15938 {
15939 abstract_virtuals_error (decl, TREE_TYPE (fntype));
15940 maybe_warn_parm_abi (TREE_TYPE (fntype),
15941 DECL_SOURCE_LOCATION (decl));
15942 }
15943 }
15944
15945 /* True iff FN is an implicitly-defined default constructor. */
15946
15947 static bool
15948 implicit_default_ctor_p (tree fn)
15949 {
15950 return (DECL_CONSTRUCTOR_P (fn)
15951 && !user_provided_p (fn)
15952 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
15953 }
15954
15955 /* Clobber the contents of *this to let the back end know that the object
15956 storage is dead when we enter the constructor or leave the destructor. */
15957
15958 static tree
15959 build_clobber_this ()
15960 {
15961 /* Clobbering an empty base is pointless, and harmful if its one byte
15962 TYPE_SIZE overlays real data. */
15963 if (is_empty_class (current_class_type))
15964 return void_node;
15965
15966 /* If we have virtual bases, clobber the whole object, but only if we're in
15967 charge. If we don't have virtual bases, clobber the as-base type so we
15968 don't mess with tail padding. */
15969 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
15970
15971 tree ctype = current_class_type;
15972 if (!vbases)
15973 ctype = CLASSTYPE_AS_BASE (ctype);
15974
15975 tree clobber = build_clobber (ctype);
15976
15977 tree thisref = current_class_ref;
15978 if (ctype != current_class_type)
15979 {
15980 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
15981 thisref = convert_from_reference (thisref);
15982 }
15983
15984 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
15985 if (vbases)
15986 exprstmt = build_if_in_charge (exprstmt);
15987
15988 return exprstmt;
15989 }
15990
15991 /* Create the FUNCTION_DECL for a function definition.
15992 DECLSPECS and DECLARATOR are the parts of the declaration;
15993 they describe the function's name and the type it returns,
15994 but twisted together in a fashion that parallels the syntax of C.
15995
15996 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
15997 DECLARATOR is really the DECL for the function we are about to
15998 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
15999 indicating that the function is an inline defined in-class.
16000
16001 This function creates a binding context for the function body
16002 as well as setting up the FUNCTION_DECL in current_function_decl.
16003
16004 For C++, we must first check whether that datum makes any sense.
16005 For example, "class A local_a(1,2);" means that variable local_a
16006 is an aggregate of type A, which should have a constructor
16007 applied to it with the argument list [1, 2].
16008
16009 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
16010 or may be a BLOCK if the function has been defined previously
16011 in this translation unit. On exit, DECL_INITIAL (decl1) will be
16012 error_mark_node if the function has never been defined, or
16013 a BLOCK if the function has been defined somewhere. */
16014
16015 bool
16016 start_preparsed_function (tree decl1, tree attrs, int flags)
16017 {
16018 tree ctype = NULL_TREE;
16019 tree fntype;
16020 tree restype;
16021 int doing_friend = 0;
16022 cp_binding_level *bl;
16023 tree current_function_parms;
16024 struct c_fileinfo *finfo
16025 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
16026 bool honor_interface;
16027
16028 /* Sanity check. */
16029 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
16030 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
16031
16032 fntype = TREE_TYPE (decl1);
16033 if (TREE_CODE (fntype) == METHOD_TYPE)
16034 ctype = TYPE_METHOD_BASETYPE (fntype);
16035
16036 /* ISO C++ 11.4/5. A friend function defined in a class is in
16037 the (lexical) scope of the class in which it is defined. */
16038 if (!ctype && DECL_FRIEND_P (decl1))
16039 {
16040 ctype = DECL_FRIEND_CONTEXT (decl1);
16041
16042 /* CTYPE could be null here if we're dealing with a template;
16043 for example, `inline friend float foo()' inside a template
16044 will have no CTYPE set. */
16045 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
16046 ctype = NULL_TREE;
16047 else
16048 doing_friend = 1;
16049 }
16050
16051 if (DECL_DECLARED_INLINE_P (decl1)
16052 && lookup_attribute ("noinline", attrs))
16053 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
16054 "inline function %qD given attribute %qs", decl1, "noinline");
16055
16056 /* Handle gnu_inline attribute. */
16057 if (GNU_INLINE_P (decl1))
16058 {
16059 DECL_EXTERNAL (decl1) = 1;
16060 DECL_NOT_REALLY_EXTERN (decl1) = 0;
16061 DECL_INTERFACE_KNOWN (decl1) = 1;
16062 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
16063 }
16064
16065 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
16066 /* This is a constructor, we must ensure that any default args
16067 introduced by this definition are propagated to the clones
16068 now. The clones are used directly in overload resolution. */
16069 adjust_clone_args (decl1);
16070
16071 /* Sometimes we don't notice that a function is a static member, and
16072 build a METHOD_TYPE for it. Fix that up now. */
16073 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
16074 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
16075
16076 /* Set up current_class_type, and enter the scope of the class, if
16077 appropriate. */
16078 if (ctype)
16079 push_nested_class (ctype);
16080 else if (DECL_STATIC_FUNCTION_P (decl1))
16081 push_nested_class (DECL_CONTEXT (decl1));
16082
16083 /* Now that we have entered the scope of the class, we must restore
16084 the bindings for any template parameters surrounding DECL1, if it
16085 is an inline member template. (Order is important; consider the
16086 case where a template parameter has the same name as a field of
16087 the class.) It is not until after this point that
16088 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
16089 if (flags & SF_INCLASS_INLINE)
16090 maybe_begin_member_template_processing (decl1);
16091
16092 /* Effective C++ rule 15. */
16093 if (warn_ecpp
16094 && DECL_ASSIGNMENT_OPERATOR_P (decl1)
16095 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR)
16096 && VOID_TYPE_P (TREE_TYPE (fntype)))
16097 warning (OPT_Weffc__,
16098 "%<operator=%> should return a reference to %<*this%>");
16099
16100 /* Make the init_value nonzero so pushdecl knows this is not tentative.
16101 error_mark_node is replaced below (in poplevel) with the BLOCK. */
16102 if (!DECL_INITIAL (decl1))
16103 DECL_INITIAL (decl1) = error_mark_node;
16104
16105 /* This function exists in static storage.
16106 (This does not mean `static' in the C sense!) */
16107 TREE_STATIC (decl1) = 1;
16108
16109 /* We must call push_template_decl after current_class_type is set
16110 up. (If we are processing inline definitions after exiting a
16111 class scope, current_class_type will be NULL_TREE until set above
16112 by push_nested_class.) */
16113 if (processing_template_decl)
16114 {
16115 tree newdecl1 = push_template_decl (decl1);
16116 if (newdecl1 == error_mark_node)
16117 {
16118 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
16119 pop_nested_class ();
16120 return false;
16121 }
16122 decl1 = newdecl1;
16123 }
16124
16125 /* Make sure the parameter and return types are reasonable. When
16126 you declare a function, these types can be incomplete, but they
16127 must be complete when you define the function. */
16128 check_function_type (decl1, DECL_ARGUMENTS (decl1));
16129
16130 /* Build the return declaration for the function. */
16131 restype = TREE_TYPE (fntype);
16132
16133 if (DECL_RESULT (decl1) == NULL_TREE)
16134 {
16135 tree resdecl;
16136
16137 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
16138 DECL_ARTIFICIAL (resdecl) = 1;
16139 DECL_IGNORED_P (resdecl) = 1;
16140 DECL_RESULT (decl1) = resdecl;
16141
16142 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
16143 }
16144
16145 /* Record the decl so that the function name is defined.
16146 If we already have a decl for this name, and it is a FUNCTION_DECL,
16147 use the old decl. */
16148 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
16149 {
16150 /* A specialization is not used to guide overload resolution. */
16151 if (!DECL_FUNCTION_MEMBER_P (decl1)
16152 && !(DECL_USE_TEMPLATE (decl1) &&
16153 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
16154 {
16155 tree olddecl = pushdecl (decl1);
16156
16157 if (olddecl == error_mark_node)
16158 /* If something went wrong when registering the declaration,
16159 use DECL1; we have to have a FUNCTION_DECL to use when
16160 parsing the body of the function. */
16161 ;
16162 else
16163 {
16164 /* Otherwise, OLDDECL is either a previous declaration
16165 of the same function or DECL1 itself. */
16166
16167 if (warn_missing_declarations
16168 && olddecl == decl1
16169 && !DECL_MAIN_P (decl1)
16170 && TREE_PUBLIC (decl1)
16171 && !DECL_DECLARED_INLINE_P (decl1))
16172 {
16173 tree context;
16174
16175 /* Check whether DECL1 is in an anonymous
16176 namespace. */
16177 for (context = DECL_CONTEXT (decl1);
16178 context;
16179 context = DECL_CONTEXT (context))
16180 {
16181 if (TREE_CODE (context) == NAMESPACE_DECL
16182 && DECL_NAME (context) == NULL_TREE)
16183 break;
16184 }
16185
16186 if (context == NULL)
16187 warning_at (DECL_SOURCE_LOCATION (decl1),
16188 OPT_Wmissing_declarations,
16189 "no previous declaration for %qD", decl1);
16190 }
16191
16192 decl1 = olddecl;
16193 }
16194 }
16195 else
16196 {
16197 /* We need to set the DECL_CONTEXT. */
16198 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
16199 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
16200 }
16201 fntype = TREE_TYPE (decl1);
16202 restype = TREE_TYPE (fntype);
16203
16204 /* If #pragma weak applies, mark the decl appropriately now.
16205 The pragma only applies to global functions. Because
16206 determining whether or not the #pragma applies involves
16207 computing the mangled name for the declaration, we cannot
16208 apply the pragma until after we have merged this declaration
16209 with any previous declarations; if the original declaration
16210 has a linkage specification, that specification applies to
16211 the definition as well, and may affect the mangled name. */
16212 if (DECL_FILE_SCOPE_P (decl1))
16213 maybe_apply_pragma_weak (decl1);
16214 }
16215
16216 /* We are now in the scope of the function being defined. */
16217 current_function_decl = decl1;
16218
16219 /* Save the parm names or decls from this function's declarator
16220 where store_parm_decls will find them. */
16221 current_function_parms = DECL_ARGUMENTS (decl1);
16222
16223 /* Let the user know we're compiling this function. */
16224 announce_function (decl1);
16225
16226 gcc_assert (DECL_INITIAL (decl1));
16227
16228 /* This function may already have been parsed, in which case just
16229 return; our caller will skip over the body without parsing. */
16230 if (DECL_INITIAL (decl1) != error_mark_node)
16231 return true;
16232
16233 /* Initialize RTL machinery. We cannot do this until
16234 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
16235 even when processing a template; this is how we get
16236 CFUN set up, and our per-function variables initialized.
16237 FIXME factor out the non-RTL stuff. */
16238 bl = current_binding_level;
16239 allocate_struct_function (decl1, processing_template_decl);
16240
16241 /* Initialize the language data structures. Whenever we start
16242 a new function, we destroy temporaries in the usual way. */
16243 cfun->language = ggc_cleared_alloc<language_function> ();
16244 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
16245 current_binding_level = bl;
16246
16247 /* If we are (erroneously) defining a function that we have already
16248 defined before, wipe out what we knew before. */
16249 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1));
16250 FNDECL_USED_AUTO (decl1) = false;
16251 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL;
16252
16253 if (!processing_template_decl && type_uses_auto (restype))
16254 {
16255 FNDECL_USED_AUTO (decl1) = true;
16256 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype;
16257 }
16258
16259 /* Start the statement-tree, start the tree now. */
16260 DECL_SAVED_TREE (decl1) = push_stmt_list ();
16261
16262 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
16263 {
16264 /* We know that this was set up by `grokclassfn'. We do not
16265 wait until `store_parm_decls', since evil parse errors may
16266 never get us to that point. Here we keep the consistency
16267 between `current_class_type' and `current_class_ptr'. */
16268 tree t = DECL_ARGUMENTS (decl1);
16269
16270 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
16271 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
16272
16273 cp_function_chain->x_current_class_ref
16274 = cp_build_fold_indirect_ref (t);
16275 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
16276 cp_function_chain->x_current_class_ptr = t;
16277
16278 /* Constructors and destructors need to know whether they're "in
16279 charge" of initializing virtual base classes. */
16280 t = DECL_CHAIN (t);
16281 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
16282 {
16283 current_in_charge_parm = t;
16284 t = DECL_CHAIN (t);
16285 }
16286 if (DECL_HAS_VTT_PARM_P (decl1))
16287 {
16288 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
16289 current_vtt_parm = t;
16290 }
16291 }
16292
16293 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
16294 /* Implicitly-defined methods (like the
16295 destructor for a class in which no destructor
16296 is explicitly declared) must not be defined
16297 until their definition is needed. So, we
16298 ignore interface specifications for
16299 compiler-generated functions. */
16300 && !DECL_ARTIFICIAL (decl1));
16301
16302 if (processing_template_decl)
16303 /* Don't mess with interface flags. */;
16304 else if (DECL_INTERFACE_KNOWN (decl1))
16305 {
16306 tree ctx = decl_function_context (decl1);
16307
16308 if (DECL_NOT_REALLY_EXTERN (decl1))
16309 DECL_EXTERNAL (decl1) = 0;
16310
16311 if (ctx != NULL_TREE && vague_linkage_p (ctx))
16312 /* This is a function in a local class in an extern inline
16313 or template function. */
16314 comdat_linkage (decl1);
16315 }
16316 /* If this function belongs to an interface, it is public.
16317 If it belongs to someone else's interface, it is also external.
16318 This only affects inlines and template instantiations. */
16319 else if (!finfo->interface_unknown && honor_interface)
16320 {
16321 if (DECL_DECLARED_INLINE_P (decl1)
16322 || DECL_TEMPLATE_INSTANTIATION (decl1))
16323 {
16324 DECL_EXTERNAL (decl1)
16325 = (finfo->interface_only
16326 || (DECL_DECLARED_INLINE_P (decl1)
16327 && ! flag_implement_inlines
16328 && !DECL_VINDEX (decl1)));
16329
16330 /* For WIN32 we also want to put these in linkonce sections. */
16331 maybe_make_one_only (decl1);
16332 }
16333 else
16334 DECL_EXTERNAL (decl1) = 0;
16335 DECL_INTERFACE_KNOWN (decl1) = 1;
16336 /* If this function is in an interface implemented in this file,
16337 make sure that the back end knows to emit this function
16338 here. */
16339 if (!DECL_EXTERNAL (decl1))
16340 mark_needed (decl1);
16341 }
16342 else if (finfo->interface_unknown && finfo->interface_only
16343 && honor_interface)
16344 {
16345 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
16346 interface, we will have both finfo->interface_unknown and
16347 finfo->interface_only set. In that case, we don't want to
16348 use the normal heuristics because someone will supply a
16349 #pragma implementation elsewhere, and deducing it here would
16350 produce a conflict. */
16351 comdat_linkage (decl1);
16352 DECL_EXTERNAL (decl1) = 0;
16353 DECL_INTERFACE_KNOWN (decl1) = 1;
16354 DECL_DEFER_OUTPUT (decl1) = 1;
16355 }
16356 else
16357 {
16358 /* This is a definition, not a reference.
16359 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
16360 if (!GNU_INLINE_P (decl1))
16361 DECL_EXTERNAL (decl1) = 0;
16362
16363 if ((DECL_DECLARED_INLINE_P (decl1)
16364 || DECL_TEMPLATE_INSTANTIATION (decl1))
16365 && ! DECL_INTERFACE_KNOWN (decl1))
16366 DECL_DEFER_OUTPUT (decl1) = 1;
16367 else
16368 DECL_INTERFACE_KNOWN (decl1) = 1;
16369 }
16370
16371 /* Determine the ELF visibility attribute for the function. We must not
16372 do this before calling "pushdecl", as we must allow "duplicate_decls"
16373 to merge any attributes appropriately. We also need to wait until
16374 linkage is set. */
16375 if (!DECL_CLONED_FUNCTION_P (decl1))
16376 determine_visibility (decl1);
16377
16378 if (!processing_template_decl)
16379 maybe_instantiate_noexcept (decl1);
16380
16381 begin_scope (sk_function_parms, decl1);
16382
16383 ++function_depth;
16384
16385 if (DECL_DESTRUCTOR_P (decl1)
16386 || (DECL_CONSTRUCTOR_P (decl1)
16387 && targetm.cxx.cdtor_returns_this ()))
16388 {
16389 cdtor_label = create_artificial_label (input_location);
16390 LABEL_DECL_CDTOR (cdtor_label) = true;
16391 }
16392
16393 start_fname_decls ();
16394
16395 store_parm_decls (current_function_parms);
16396
16397 push_operator_bindings ();
16398
16399 if (!processing_template_decl
16400 && (flag_lifetime_dse > 1)
16401 && DECL_CONSTRUCTOR_P (decl1)
16402 && !DECL_CLONED_FUNCTION_P (decl1)
16403 /* Clobbering an empty base is harmful if it overlays real data. */
16404 && !is_empty_class (current_class_type)
16405 /* We can't clobber safely for an implicitly-defined default constructor
16406 because part of the initialization might happen before we enter the
16407 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
16408 && !implicit_default_ctor_p (decl1))
16409 finish_expr_stmt (build_clobber_this ());
16410
16411 if (!processing_template_decl
16412 && DECL_CONSTRUCTOR_P (decl1)
16413 && sanitize_flags_p (SANITIZE_VPTR)
16414 && !DECL_CLONED_FUNCTION_P (decl1)
16415 && !implicit_default_ctor_p (decl1))
16416 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
16417
16418 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1))
16419 start_lambda_scope (decl1);
16420
16421 return true;
16422 }
16423
16424
16425 /* Like start_preparsed_function, except that instead of a
16426 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
16427
16428 Returns true on success. If the DECLARATOR is not suitable
16429 for a function, we return false, which tells the parser to
16430 skip the entire function. */
16431
16432 bool
16433 start_function (cp_decl_specifier_seq *declspecs,
16434 const cp_declarator *declarator,
16435 tree attrs)
16436 {
16437 tree decl1;
16438
16439 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
16440 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
16441 if (decl1 == error_mark_node)
16442 return false;
16443
16444 if (DECL_MAIN_P (decl1))
16445 /* main must return int. grokfndecl should have corrected it
16446 (and issued a diagnostic) if the user got it wrong. */
16447 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
16448 integer_type_node));
16449
16450 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
16451 }
16452 \f
16453 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
16454 FN. */
16455
16456 static bool
16457 use_eh_spec_block (tree fn)
16458 {
16459 return (flag_exceptions && flag_enforce_eh_specs
16460 && !processing_template_decl
16461 && !type_throw_all_p (TREE_TYPE (fn))
16462 /* We insert the EH_SPEC_BLOCK only in the original
16463 function; then, it is copied automatically to the
16464 clones. */
16465 && !DECL_CLONED_FUNCTION_P (fn)
16466 /* Implicitly-generated constructors and destructors have
16467 exception specifications. However, those specifications
16468 are the union of the possible exceptions specified by the
16469 constructors/destructors for bases and members, so no
16470 unallowed exception will ever reach this function. By
16471 not creating the EH_SPEC_BLOCK we save a little memory,
16472 and we avoid spurious warnings about unreachable
16473 code. */
16474 && !DECL_DEFAULTED_FN (fn));
16475 }
16476
16477 /* Helper function to push ARGS into the current lexical scope. DECL
16478 is the function declaration. NONPARMS is used to handle enum
16479 constants. */
16480
16481 void
16482 do_push_parm_decls (tree decl, tree args, tree *nonparms)
16483 {
16484 /* If we're doing semantic analysis, then we'll call pushdecl
16485 for each of these. We must do them in reverse order so that
16486 they end in the correct forward order. */
16487 args = nreverse (args);
16488
16489 tree next;
16490 for (tree parm = args; parm; parm = next)
16491 {
16492 next = DECL_CHAIN (parm);
16493 if (TREE_CODE (parm) == PARM_DECL)
16494 pushdecl (parm);
16495 else if (nonparms)
16496 {
16497 /* If we find an enum constant or a type tag, put it aside for
16498 the moment. */
16499 TREE_CHAIN (parm) = NULL_TREE;
16500 *nonparms = chainon (*nonparms, parm);
16501 }
16502 }
16503
16504 /* Get the decls in their original chain order and record in the
16505 function. This is all and only the PARM_DECLs that were
16506 pushed into scope by the loop above. */
16507 DECL_ARGUMENTS (decl) = get_local_decls ();
16508 }
16509
16510 /* Store the parameter declarations into the current function declaration.
16511 This is called after parsing the parameter declarations, before
16512 digesting the body of the function.
16513
16514 Also install to binding contour return value identifier, if any. */
16515
16516 static void
16517 store_parm_decls (tree current_function_parms)
16518 {
16519 tree fndecl = current_function_decl;
16520
16521 /* This is a chain of any other decls that came in among the parm
16522 declarations. If a parm is declared with enum {foo, bar} x;
16523 then CONST_DECLs for foo and bar are put here. */
16524 tree nonparms = NULL_TREE;
16525
16526 if (current_function_parms)
16527 {
16528 /* This case is when the function was defined with an ANSI prototype.
16529 The parms already have decls, so we need not do anything here
16530 except record them as in effect
16531 and complain if any redundant old-style parm decls were written. */
16532
16533 tree specparms = current_function_parms;
16534
16535 /* Must clear this because it might contain TYPE_DECLs declared
16536 at class level. */
16537 current_binding_level->names = NULL;
16538
16539 do_push_parm_decls (fndecl, specparms, &nonparms);
16540 }
16541 else
16542 DECL_ARGUMENTS (fndecl) = NULL_TREE;
16543
16544 /* Now store the final chain of decls for the arguments
16545 as the decl-chain of the current lexical scope.
16546 Put the enumerators in as well, at the front so that
16547 DECL_ARGUMENTS is not modified. */
16548 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
16549
16550 if (use_eh_spec_block (current_function_decl))
16551 current_eh_spec_block = begin_eh_spec_block ();
16552 }
16553
16554 \f
16555 /* Set the return value of the constructor (if present). */
16556
16557 static void
16558 finish_constructor_body (void)
16559 {
16560 tree val;
16561 tree exprstmt;
16562
16563 if (targetm.cxx.cdtor_returns_this ())
16564 {
16565 /* Any return from a constructor will end up here. */
16566 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16567
16568 val = DECL_ARGUMENTS (current_function_decl);
16569 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16570 DECL_RESULT (current_function_decl), val);
16571 /* Return the address of the object. */
16572 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16573 add_stmt (exprstmt);
16574 }
16575 }
16576
16577 /* Do all the processing for the beginning of a destructor; set up the
16578 vtable pointers and cleanups for bases and members. */
16579
16580 static void
16581 begin_destructor_body (void)
16582 {
16583 tree compound_stmt;
16584
16585 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
16586 issued an error message. We still want to try to process the
16587 body of the function, but initialize_vtbl_ptrs will crash if
16588 TYPE_BINFO is NULL. */
16589 if (COMPLETE_TYPE_P (current_class_type))
16590 {
16591 compound_stmt = begin_compound_stmt (0);
16592 /* Make all virtual function table pointers in non-virtual base
16593 classes point to CURRENT_CLASS_TYPE's virtual function
16594 tables. */
16595 initialize_vtbl_ptrs (current_class_ptr);
16596 finish_compound_stmt (compound_stmt);
16597
16598 if (flag_lifetime_dse
16599 /* Clobbering an empty base is harmful if it overlays real data. */
16600 && !is_empty_class (current_class_type))
16601 {
16602 if (sanitize_flags_p (SANITIZE_VPTR)
16603 && (flag_sanitize_recover & SANITIZE_VPTR) == 0
16604 && TYPE_CONTAINS_VPTR_P (current_class_type))
16605 {
16606 tree binfo = TYPE_BINFO (current_class_type);
16607 tree ref
16608 = cp_build_fold_indirect_ref (current_class_ptr);
16609
16610 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo));
16611 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr));
16612 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr,
16613 NOP_EXPR, vtbl,
16614 tf_warning_or_error);
16615 /* If the vptr is shared with some virtual nearly empty base,
16616 don't clear it if not in charge, the dtor of the virtual
16617 nearly empty base will do that later. */
16618 if (CLASSTYPE_VBASECLASSES (current_class_type)
16619 && CLASSTYPE_PRIMARY_BINFO (current_class_type)
16620 && BINFO_VIRTUAL_P
16621 (CLASSTYPE_PRIMARY_BINFO (current_class_type)))
16622 {
16623 stmt = convert_to_void (stmt, ICV_STATEMENT,
16624 tf_warning_or_error);
16625 stmt = build_if_in_charge (stmt);
16626 }
16627 finish_decl_cleanup (NULL_TREE, stmt);
16628 }
16629 else
16630 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
16631 }
16632
16633 /* And insert cleanups for our bases and members so that they
16634 will be properly destroyed if we throw. */
16635 push_base_cleanups ();
16636 }
16637 }
16638
16639 /* At the end of every destructor we generate code to delete the object if
16640 necessary. Do that now. */
16641
16642 static void
16643 finish_destructor_body (void)
16644 {
16645 tree exprstmt;
16646
16647 /* Any return from a destructor will end up here; that way all base
16648 and member cleanups will be run when the function returns. */
16649 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
16650
16651 if (targetm.cxx.cdtor_returns_this ())
16652 {
16653 tree val;
16654
16655 val = DECL_ARGUMENTS (current_function_decl);
16656 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
16657 DECL_RESULT (current_function_decl), val);
16658 /* Return the address of the object. */
16659 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
16660 add_stmt (exprstmt);
16661 }
16662 }
16663
16664 /* Do the necessary processing for the beginning of a function body, which
16665 in this case includes member-initializers, but not the catch clauses of
16666 a function-try-block. Currently, this means opening a binding level
16667 for the member-initializers (in a ctor), member cleanups (in a dtor),
16668 and capture proxies (in a lambda operator()). */
16669
16670 tree
16671 begin_function_body (void)
16672 {
16673 tree stmt;
16674
16675 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
16676 return NULL_TREE;
16677
16678 if (processing_template_decl)
16679 /* Do nothing now. */;
16680 else
16681 /* Always keep the BLOCK node associated with the outermost pair of
16682 curly braces of a function. These are needed for correct
16683 operation of dwarfout.c. */
16684 keep_next_level (true);
16685
16686 stmt = begin_compound_stmt (BCS_FN_BODY);
16687
16688 if (processing_template_decl)
16689 /* Do nothing now. */;
16690 else if (DECL_DESTRUCTOR_P (current_function_decl))
16691 begin_destructor_body ();
16692
16693 return stmt;
16694 }
16695
16696 /* Do the processing for the end of a function body. Currently, this means
16697 closing out the cleanups for fully-constructed bases and members, and in
16698 the case of the destructor, deleting the object if desired. Again, this
16699 is only meaningful for [cd]tors, since they are the only functions where
16700 there is a significant distinction between the main body and any
16701 function catch clauses. Handling, say, main() return semantics here
16702 would be wrong, as flowing off the end of a function catch clause for
16703 main() would also need to return 0. */
16704
16705 void
16706 finish_function_body (tree compstmt)
16707 {
16708 if (compstmt == NULL_TREE)
16709 return;
16710
16711 /* Close the block. */
16712 finish_compound_stmt (compstmt);
16713
16714 if (processing_template_decl)
16715 /* Do nothing now. */;
16716 else if (DECL_CONSTRUCTOR_P (current_function_decl))
16717 finish_constructor_body ();
16718 else if (DECL_DESTRUCTOR_P (current_function_decl))
16719 finish_destructor_body ();
16720 }
16721
16722 /* Given a function, returns the BLOCK corresponding to the outermost level
16723 of curly braces, skipping the artificial block created for constructor
16724 initializers. */
16725
16726 tree
16727 outer_curly_brace_block (tree fndecl)
16728 {
16729 tree block = DECL_INITIAL (fndecl);
16730 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16731 return block;
16732 block = BLOCK_SUBBLOCKS (block);
16733 if (BLOCK_OUTER_CURLY_BRACE_P (block))
16734 return block;
16735 block = BLOCK_SUBBLOCKS (block);
16736 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
16737 return block;
16738 }
16739
16740 /* If FNDECL is a class's key method, add the class to the list of
16741 keyed classes that should be emitted. */
16742
16743 static void
16744 record_key_method_defined (tree fndecl)
16745 {
16746 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
16747 && DECL_VIRTUAL_P (fndecl)
16748 && !processing_template_decl)
16749 {
16750 tree fnclass = DECL_CONTEXT (fndecl);
16751 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
16752 vec_safe_push (keyed_classes, fnclass);
16753 }
16754 }
16755
16756 /* Subroutine of finish_function.
16757 Save the body of constexpr functions for possible
16758 future compile time evaluation. */
16759
16760 static void
16761 maybe_save_function_definition (tree fun)
16762 {
16763 if (!processing_template_decl
16764 && DECL_DECLARED_CONSTEXPR_P (fun)
16765 && !cp_function_chain->invalid_constexpr
16766 && !DECL_CLONED_FUNCTION_P (fun))
16767 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
16768 }
16769
16770 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion
16771 of "return *this;" immediately before its location, using FNDECL's
16772 first statement (if any) to give the indentation, if appropriate. */
16773
16774 static void
16775 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl)
16776 {
16777 location_t indent = UNKNOWN_LOCATION;
16778 tree stmts = expr_first (DECL_SAVED_TREE (fndecl));
16779 if (stmts)
16780 indent = EXPR_LOCATION (stmts);
16781 richloc->add_fixit_insert_formatted ("return *this;",
16782 richloc->get_loc (),
16783 indent);
16784 }
16785
16786 /* Finish up a function declaration and compile that function
16787 all the way to assembler language output. The free the storage
16788 for the function definition. INLINE_P is TRUE if we just
16789 finished processing the body of an in-class inline function
16790 definition. (This processing will have taken place after the
16791 class definition is complete.) */
16792
16793 tree
16794 finish_function (bool inline_p)
16795 {
16796 tree fndecl = current_function_decl;
16797 tree fntype, ctype = NULL_TREE;
16798
16799 /* When we get some parse errors, we can end up without a
16800 current_function_decl, so cope. */
16801 if (fndecl == NULL_TREE)
16802 return error_mark_node;
16803
16804 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
16805 finish_lambda_scope ();
16806
16807 if (c_dialect_objc ())
16808 objc_finish_function ();
16809
16810 record_key_method_defined (fndecl);
16811
16812 fntype = TREE_TYPE (fndecl);
16813
16814 /* TREE_READONLY (fndecl) = 1;
16815 This caused &foo to be of type ptr-to-const-function
16816 which then got a warning when stored in a ptr-to-function variable. */
16817
16818 gcc_assert (building_stmt_list_p ());
16819 /* The current function is being defined, so its DECL_INITIAL should
16820 be set, and unless there's a multiple definition, it should be
16821 error_mark_node. */
16822 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
16823
16824 /* For a cloned function, we've already got all the code we need;
16825 there's no need to add any extra bits. */
16826 if (!DECL_CLONED_FUNCTION_P (fndecl))
16827 {
16828 /* Make it so that `main' always returns 0 by default. */
16829 if (DECL_MAIN_P (current_function_decl))
16830 finish_return_stmt (integer_zero_node);
16831
16832 if (use_eh_spec_block (current_function_decl))
16833 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
16834 (TREE_TYPE (current_function_decl)),
16835 current_eh_spec_block);
16836 }
16837
16838 /* If we're saving up tree structure, tie off the function now. */
16839 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
16840
16841 finish_fname_decls ();
16842
16843 /* If this function can't throw any exceptions, remember that. */
16844 if (!processing_template_decl
16845 && !cp_function_chain->can_throw
16846 && !flag_non_call_exceptions
16847 && !decl_replaceable_p (fndecl))
16848 TREE_NOTHROW (fndecl) = 1;
16849
16850 /* This must come after expand_function_end because cleanups might
16851 have declarations (from inline functions) that need to go into
16852 this function's blocks. */
16853
16854 /* If the current binding level isn't the outermost binding level
16855 for this function, either there is a bug, or we have experienced
16856 syntax errors and the statement tree is malformed. */
16857 if (current_binding_level->kind != sk_function_parms)
16858 {
16859 /* Make sure we have already experienced errors. */
16860 gcc_assert (errorcount);
16861
16862 /* Throw away the broken statement tree and extra binding
16863 levels. */
16864 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
16865
16866 while (current_binding_level->kind != sk_function_parms)
16867 {
16868 if (current_binding_level->kind == sk_class)
16869 pop_nested_class ();
16870 else
16871 poplevel (0, 0, 0);
16872 }
16873 }
16874 poplevel (1, 0, 1);
16875
16876 /* Statements should always be full-expressions at the outermost set
16877 of curly braces for a function. */
16878 gcc_assert (stmts_are_full_exprs_p ());
16879
16880 /* If there are no return statements in a function with auto return type,
16881 the return type is void. But if the declared type is something like
16882 auto*, this is an error. */
16883 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
16884 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
16885 {
16886 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl))
16887 && !current_function_returns_value
16888 && !current_function_returns_null)
16889 {
16890 /* We haven't applied return type deduction because we haven't
16891 seen any return statements. Do that now. */
16892 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
16893 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl),
16894 void_node, node, tf_warning_or_error,
16895 adc_return_type);
16896
16897 apply_deduced_return_type (fndecl, void_type_node);
16898 fntype = TREE_TYPE (fndecl);
16899 }
16900 else if (!current_function_returns_value
16901 && !current_function_returns_null)
16902 {
16903 error ("no return statements in function returning %qT",
16904 DECL_SAVED_AUTO_RETURN_TYPE (fndecl));
16905 inform (input_location, "only plain %<auto%> return type can be "
16906 "deduced to %<void%>");
16907 }
16908 }
16909
16910 /* Remember that we were in class scope. */
16911 if (current_class_name)
16912 ctype = current_class_type;
16913
16914 if (DECL_DELETED_FN (fndecl))
16915 {
16916 DECL_INITIAL (fndecl) = error_mark_node;
16917 DECL_SAVED_TREE (fndecl) = NULL_TREE;
16918 goto cleanup;
16919 }
16920
16921 // If this is a concept, check that the definition is reasonable.
16922 if (DECL_DECLARED_CONCEPT_P (fndecl))
16923 check_function_concept (fndecl);
16924
16925 if (flag_openmp)
16926 if (tree attr = lookup_attribute ("omp declare variant base",
16927 DECL_ATTRIBUTES (fndecl)))
16928 omp_declare_variant_finalize (fndecl, attr);
16929
16930 /* Lambda closure members are implicitly constexpr if possible. */
16931 if (cxx_dialect >= cxx17
16932 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl)))
16933 DECL_DECLARED_CONSTEXPR_P (fndecl)
16934 = ((processing_template_decl
16935 || is_valid_constexpr_fn (fndecl, /*complain*/false))
16936 && potential_constant_expression (DECL_SAVED_TREE (fndecl)));
16937
16938 /* Save constexpr function body before it gets munged by
16939 the NRV transformation. */
16940 maybe_save_function_definition (fndecl);
16941
16942 /* Invoke the pre-genericize plugin before we start munging things. */
16943 if (!processing_template_decl)
16944 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
16945
16946 /* Perform delayed folding before NRV transformation. */
16947 if (!processing_template_decl
16948 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
16949 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
16950 cp_fold_function (fndecl);
16951
16952 /* Set up the named return value optimization, if we can. Candidate
16953 variables are selected in check_return_expr. */
16954 if (current_function_return_value)
16955 {
16956 tree r = current_function_return_value;
16957 tree outer;
16958
16959 if (r != error_mark_node
16960 /* This is only worth doing for fns that return in memory--and
16961 simpler, since we don't have to worry about promoted modes. */
16962 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
16963 /* Only allow this for variables declared in the outer scope of
16964 the function so we know that their lifetime always ends with a
16965 return; see g++.dg/opt/nrv6.C. We could be more flexible if
16966 we were to do this optimization in tree-ssa. */
16967 && (outer = outer_curly_brace_block (fndecl))
16968 && chain_member (r, BLOCK_VARS (outer)))
16969 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
16970
16971 current_function_return_value = NULL_TREE;
16972 }
16973
16974 /* Must mark the RESULT_DECL as being in this function. */
16975 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
16976
16977 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
16978 to the FUNCTION_DECL node itself. */
16979 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
16980
16981 /* Complain if there's just no return statement. */
16982 if (warn_return_type
16983 && !VOID_TYPE_P (TREE_TYPE (fntype))
16984 && !dependent_type_p (TREE_TYPE (fntype))
16985 && !current_function_returns_value && !current_function_returns_null
16986 /* Don't complain if we abort or throw. */
16987 && !current_function_returns_abnormally
16988 /* Don't complain if there's an infinite loop. */
16989 && !current_function_infinite_loop
16990 /* Don't complain if we are declared noreturn. */
16991 && !TREE_THIS_VOLATILE (fndecl)
16992 && !DECL_NAME (DECL_RESULT (fndecl))
16993 && !TREE_NO_WARNING (fndecl)
16994 /* Structor return values (if any) are set by the compiler. */
16995 && !DECL_CONSTRUCTOR_P (fndecl)
16996 && !DECL_DESTRUCTOR_P (fndecl)
16997 && targetm.warn_func_return (fndecl))
16998 {
16999 gcc_rich_location richloc (input_location);
17000 /* Potentially add a "return *this;" fix-it hint for
17001 assignment operators. */
17002 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl)))
17003 {
17004 tree valtype = TREE_TYPE (DECL_RESULT (fndecl));
17005 if (TREE_CODE (valtype) == REFERENCE_TYPE
17006 && current_class_ref
17007 && same_type_ignoring_top_level_qualifiers_p
17008 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))
17009 && global_dc->option_enabled (OPT_Wreturn_type,
17010 global_dc->lang_mask,
17011 global_dc->option_state))
17012 add_return_star_this_fixit (&richloc, fndecl);
17013 }
17014 if (warning_at (&richloc, OPT_Wreturn_type,
17015 "no return statement in function returning non-void"))
17016 TREE_NO_WARNING (fndecl) = 1;
17017 }
17018
17019 /* Store the end of the function, so that we get good line number
17020 info for the epilogue. */
17021 cfun->function_end_locus = input_location;
17022
17023 /* Complain about parameters that are only set, but never otherwise used. */
17024 if (warn_unused_but_set_parameter
17025 && !processing_template_decl
17026 && errorcount == unused_but_set_errorcount
17027 && !DECL_CLONED_FUNCTION_P (fndecl))
17028 {
17029 tree decl;
17030
17031 for (decl = DECL_ARGUMENTS (fndecl);
17032 decl;
17033 decl = DECL_CHAIN (decl))
17034 if (TREE_USED (decl)
17035 && TREE_CODE (decl) == PARM_DECL
17036 && !DECL_READ_P (decl)
17037 && DECL_NAME (decl)
17038 && !DECL_ARTIFICIAL (decl)
17039 && !TREE_NO_WARNING (decl)
17040 && !DECL_IN_SYSTEM_HEADER (decl)
17041 && TREE_TYPE (decl) != error_mark_node
17042 && !TYPE_REF_P (TREE_TYPE (decl))
17043 && (!CLASS_TYPE_P (TREE_TYPE (decl))
17044 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
17045 warning_at (DECL_SOURCE_LOCATION (decl),
17046 OPT_Wunused_but_set_parameter,
17047 "parameter %qD set but not used", decl);
17048 unused_but_set_errorcount = errorcount;
17049 }
17050
17051 /* Complain about locally defined typedefs that are not used in this
17052 function. */
17053 maybe_warn_unused_local_typedefs ();
17054
17055 /* Possibly warn about unused parameters. */
17056 if (warn_unused_parameter
17057 && !processing_template_decl
17058 && !DECL_CLONED_FUNCTION_P (fndecl))
17059 do_warn_unused_parameter (fndecl);
17060
17061 /* Genericize before inlining. */
17062 if (!processing_template_decl
17063 && !DECL_IMMEDIATE_FUNCTION_P (fndecl)
17064 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl))
17065 cp_genericize (fndecl);
17066
17067 cleanup:
17068 /* We're leaving the context of this function, so zap cfun. It's still in
17069 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
17070 set_cfun (NULL);
17071 current_function_decl = NULL;
17072
17073 /* If this is an in-class inline definition, we may have to pop the
17074 bindings for the template parameters that we added in
17075 maybe_begin_member_template_processing when start_function was
17076 called. */
17077 if (inline_p)
17078 maybe_end_member_template_processing ();
17079
17080 /* Leave the scope of the class. */
17081 if (ctype)
17082 pop_nested_class ();
17083
17084 --function_depth;
17085
17086 /* Clean up. */
17087 current_function_decl = NULL_TREE;
17088
17089 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
17090 return fndecl;
17091 }
17092 \f
17093 /* Create the FUNCTION_DECL for a function definition.
17094 DECLSPECS and DECLARATOR are the parts of the declaration;
17095 they describe the return type and the name of the function,
17096 but twisted together in a fashion that parallels the syntax of C.
17097
17098 This function creates a binding context for the function body
17099 as well as setting up the FUNCTION_DECL in current_function_decl.
17100
17101 Returns a FUNCTION_DECL on success.
17102
17103 If the DECLARATOR is not suitable for a function (it defines a datum
17104 instead), we return 0, which tells yyparse to report a parse error.
17105
17106 May return void_type_node indicating that this method is actually
17107 a friend. See grokfield for more details.
17108
17109 Came here with a `.pushlevel' .
17110
17111 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
17112 CHANGES TO CODE IN `grokfield'. */
17113
17114 tree
17115 grokmethod (cp_decl_specifier_seq *declspecs,
17116 const cp_declarator *declarator, tree attrlist)
17117 {
17118 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
17119 &attrlist);
17120
17121 if (fndecl == error_mark_node)
17122 return error_mark_node;
17123
17124 if (attrlist)
17125 cplus_decl_attributes (&fndecl, attrlist, 0);
17126
17127 /* Pass friends other than inline friend functions back. */
17128 if (fndecl == void_type_node)
17129 return fndecl;
17130
17131 if (DECL_IN_AGGR_P (fndecl))
17132 {
17133 if (DECL_CLASS_SCOPE_P (fndecl))
17134 error ("%qD is already defined in class %qT", fndecl,
17135 DECL_CONTEXT (fndecl));
17136 return error_mark_node;
17137 }
17138
17139 check_template_shadow (fndecl);
17140
17141 if (TREE_PUBLIC (fndecl))
17142 DECL_COMDAT (fndecl) = 1;
17143 DECL_DECLARED_INLINE_P (fndecl) = 1;
17144 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
17145
17146 /* We process method specializations in finish_struct_1. */
17147 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
17148 {
17149 fndecl = push_template_decl (fndecl);
17150 if (fndecl == error_mark_node)
17151 return fndecl;
17152 }
17153
17154 if (! DECL_FRIEND_P (fndecl))
17155 {
17156 if (DECL_CHAIN (fndecl))
17157 {
17158 fndecl = copy_node (fndecl);
17159 TREE_CHAIN (fndecl) = NULL_TREE;
17160 }
17161 }
17162
17163 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
17164
17165 DECL_IN_AGGR_P (fndecl) = 1;
17166 return fndecl;
17167 }
17168 \f
17169
17170 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
17171 we can lay it out later, when and if its type becomes complete.
17172
17173 Also handle constexpr variables where the initializer involves
17174 an unlowered PTRMEM_CST because the class isn't complete yet. */
17175
17176 void
17177 maybe_register_incomplete_var (tree var)
17178 {
17179 gcc_assert (VAR_P (var));
17180
17181 /* Keep track of variables with incomplete types. */
17182 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
17183 && DECL_EXTERNAL (var))
17184 {
17185 tree inner_type = TREE_TYPE (var);
17186
17187 while (TREE_CODE (inner_type) == ARRAY_TYPE)
17188 inner_type = TREE_TYPE (inner_type);
17189 inner_type = TYPE_MAIN_VARIANT (inner_type);
17190
17191 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
17192 /* RTTI TD entries are created while defining the type_info. */
17193 || (TYPE_LANG_SPECIFIC (inner_type)
17194 && TYPE_BEING_DEFINED (inner_type)))
17195 {
17196 incomplete_var iv = {var, inner_type};
17197 vec_safe_push (incomplete_vars, iv);
17198 }
17199 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
17200 && decl_constant_var_p (var)
17201 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
17202 {
17203 /* When the outermost open class is complete we can resolve any
17204 pointers-to-members. */
17205 tree context = outermost_open_class ();
17206 incomplete_var iv = {var, context};
17207 vec_safe_push (incomplete_vars, iv);
17208 }
17209 }
17210 }
17211
17212 /* Called when a class type (given by TYPE) is defined. If there are
17213 any existing VAR_DECLs whose type has been completed by this
17214 declaration, update them now. */
17215
17216 void
17217 complete_vars (tree type)
17218 {
17219 unsigned ix;
17220 incomplete_var *iv;
17221
17222 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
17223 {
17224 if (same_type_p (type, iv->incomplete_type))
17225 {
17226 tree var = iv->decl;
17227 tree type = TREE_TYPE (var);
17228
17229 if (type != error_mark_node
17230 && (TYPE_MAIN_VARIANT (strip_array_types (type))
17231 == iv->incomplete_type))
17232 {
17233 /* Complete the type of the variable. The VAR_DECL itself
17234 will be laid out in expand_expr. */
17235 complete_type (type);
17236 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
17237 }
17238
17239 /* Remove this entry from the list. */
17240 incomplete_vars->unordered_remove (ix);
17241 }
17242 else
17243 ix++;
17244 }
17245
17246 /* Check for pending declarations which may have abstract type. */
17247 complete_type_check_abstract (type);
17248 }
17249
17250 /* If DECL is of a type which needs a cleanup, build and return an
17251 expression to perform that cleanup here. Return NULL_TREE if no
17252 cleanup need be done. DECL can also be a _REF when called from
17253 split_nonconstant_init_1. */
17254
17255 tree
17256 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
17257 {
17258 tree type;
17259 tree attr;
17260 tree cleanup;
17261
17262 /* Assume no cleanup is required. */
17263 cleanup = NULL_TREE;
17264
17265 if (error_operand_p (decl))
17266 return cleanup;
17267
17268 /* Handle "__attribute__((cleanup))". We run the cleanup function
17269 before the destructor since the destructor is what actually
17270 terminates the lifetime of the object. */
17271 if (DECL_P (decl))
17272 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
17273 else
17274 attr = NULL_TREE;
17275 if (attr)
17276 {
17277 tree id;
17278 tree fn;
17279 tree arg;
17280
17281 /* Get the name specified by the user for the cleanup function. */
17282 id = TREE_VALUE (TREE_VALUE (attr));
17283 /* Look up the name to find the cleanup function to call. It is
17284 important to use lookup_name here because that is what is
17285 used in c-common.c:handle_cleanup_attribute when performing
17286 initial checks on the attribute. Note that those checks
17287 include ensuring that the function found is not an overloaded
17288 function, or an object with an overloaded call operator,
17289 etc.; we can rely on the fact that the function found is an
17290 ordinary FUNCTION_DECL. */
17291 fn = lookup_name (id);
17292 arg = build_address (decl);
17293 if (!mark_used (decl, complain) && !(complain & tf_error))
17294 return error_mark_node;
17295 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
17296 if (cleanup == error_mark_node)
17297 return error_mark_node;
17298 }
17299 /* Handle ordinary C++ destructors. */
17300 type = TREE_TYPE (decl);
17301 if (type_build_dtor_call (type))
17302 {
17303 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
17304 tree addr;
17305 tree call;
17306
17307 if (TREE_CODE (type) == ARRAY_TYPE)
17308 addr = decl;
17309 else
17310 addr = build_address (decl);
17311
17312 call = build_delete (input_location, TREE_TYPE (addr), addr,
17313 sfk_complete_destructor, flags, 0, complain);
17314 if (call == error_mark_node)
17315 cleanup = error_mark_node;
17316 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
17317 /* Discard the call. */;
17318 else if (decl_maybe_constant_destruction (decl, type)
17319 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
17320 cxx_constant_dtor (call, decl);
17321 else if (cleanup)
17322 cleanup = cp_build_compound_expr (cleanup, call, complain);
17323 else
17324 cleanup = call;
17325 }
17326
17327 /* build_delete sets the location of the destructor call to the
17328 current location, even though the destructor is going to be
17329 called later, at the end of the current scope. This can lead to
17330 a "jumpy" behavior for users of debuggers when they step around
17331 the end of the block. So let's unset the location of the
17332 destructor call instead. */
17333 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
17334 if (cleanup && CONVERT_EXPR_P (cleanup))
17335 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION);
17336
17337 if (cleanup
17338 && DECL_P (decl)
17339 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
17340 /* Treat objects with destructors as used; the destructor may do
17341 something substantive. */
17342 && !mark_used (decl, complain) && !(complain & tf_error))
17343 return error_mark_node;
17344
17345 return cleanup;
17346 }
17347
17348 \f
17349 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
17350 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
17351 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
17352
17353 tree
17354 static_fn_type (tree memfntype)
17355 {
17356 tree fntype;
17357 tree args;
17358
17359 if (TYPE_PTRMEMFUNC_P (memfntype))
17360 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
17361 if (INDIRECT_TYPE_P (memfntype)
17362 || TREE_CODE (memfntype) == FUNCTION_DECL)
17363 memfntype = TREE_TYPE (memfntype);
17364 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
17365 return memfntype;
17366 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
17367 args = TYPE_ARG_TYPES (memfntype);
17368 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
17369 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype));
17370 fntype = (cp_build_type_attribute_variant
17371 (fntype, TYPE_ATTRIBUTES (memfntype)));
17372 fntype = cxx_copy_lang_qualifiers (fntype, memfntype);
17373 return fntype;
17374 }
17375
17376 /* DECL was originally constructed as a non-static member function,
17377 but turned out to be static. Update it accordingly. */
17378
17379 void
17380 revert_static_member_fn (tree decl)
17381 {
17382 tree stype = static_fn_type (decl);
17383 cp_cv_quals quals = type_memfn_quals (stype);
17384 cp_ref_qualifier rqual = type_memfn_rqual (stype);
17385
17386 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
17387 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
17388
17389 TREE_TYPE (decl) = stype;
17390
17391 if (DECL_ARGUMENTS (decl))
17392 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
17393 DECL_STATIC_FUNCTION_P (decl) = 1;
17394 }
17395
17396 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
17397 one of the language-independent trees. */
17398
17399 enum cp_tree_node_structure_enum
17400 cp_tree_node_structure (union lang_tree_node * t)
17401 {
17402 switch (TREE_CODE (&t->generic))
17403 {
17404 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
17405 case BASELINK: return TS_CP_BASELINK;
17406 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
17407 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
17408 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE;
17409 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
17410 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
17411 case OVERLOAD: return TS_CP_OVERLOAD;
17412 case PTRMEM_CST: return TS_CP_PTRMEM;
17413 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
17414 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
17415 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
17416 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
17417 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
17418 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
17419 default: return TS_CP_GENERIC;
17420 }
17421 }
17422
17423 /* Build the void_list_node (void_type_node having been created). */
17424 tree
17425 build_void_list_node (void)
17426 {
17427 tree t = build_tree_list (NULL_TREE, void_type_node);
17428 return t;
17429 }
17430
17431 bool
17432 cp_missing_noreturn_ok_p (tree decl)
17433 {
17434 /* A missing noreturn is ok for the `main' function. */
17435 return DECL_MAIN_P (decl);
17436 }
17437
17438 /* Return the decl used to identify the COMDAT group into which DECL should
17439 be placed. */
17440
17441 tree
17442 cxx_comdat_group (tree decl)
17443 {
17444 /* Virtual tables, construction virtual tables, and virtual table
17445 tables all go in a single COMDAT group, named after the primary
17446 virtual table. */
17447 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
17448 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
17449 /* For all other DECLs, the COMDAT group is the mangled name of the
17450 declaration itself. */
17451 else
17452 {
17453 while (DECL_THUNK_P (decl))
17454 {
17455 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
17456 into the same section as the target function. In that case
17457 we must return target's name. */
17458 tree target = THUNK_TARGET (decl);
17459 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
17460 && DECL_SECTION_NAME (target) != NULL
17461 && DECL_ONE_ONLY (target))
17462 decl = target;
17463 else
17464 break;
17465 }
17466 }
17467
17468 return decl;
17469 }
17470
17471 /* Returns the return type for FN as written by the user, which may include
17472 a placeholder for a deduced return type. */
17473
17474 tree
17475 fndecl_declared_return_type (tree fn)
17476 {
17477 fn = STRIP_TEMPLATE (fn);
17478 if (FNDECL_USED_AUTO (fn))
17479 return DECL_SAVED_AUTO_RETURN_TYPE (fn);
17480
17481 return TREE_TYPE (TREE_TYPE (fn));
17482 }
17483
17484 /* Returns true iff DECL is a variable or function declared with an auto type
17485 that has not yet been deduced to a real type. */
17486
17487 bool
17488 undeduced_auto_decl (tree decl)
17489 {
17490 if (cxx_dialect < cxx11)
17491 return false;
17492 STRIP_ANY_LOCATION_WRAPPER (decl);
17493 return ((VAR_OR_FUNCTION_DECL_P (decl)
17494 || TREE_CODE (decl) == TEMPLATE_DECL)
17495 && type_uses_auto (TREE_TYPE (decl)));
17496 }
17497
17498 /* Complain if DECL has an undeduced return type. */
17499
17500 bool
17501 require_deduced_type (tree decl, tsubst_flags_t complain)
17502 {
17503 if (undeduced_auto_decl (decl))
17504 {
17505 if (TREE_NO_WARNING (decl) && seen_error ())
17506 /* We probably already complained about deduction failure. */;
17507 else if (complain & tf_error)
17508 error ("use of %qD before deduction of %<auto%>", decl);
17509 return false;
17510 }
17511 return true;
17512 }
17513
17514 /* Create a representation of the explicit-specifier with
17515 constant-expression of EXPR. COMPLAIN is as for tsubst. */
17516
17517 tree
17518 build_explicit_specifier (tree expr, tsubst_flags_t complain)
17519 {
17520 if (instantiation_dependent_expression_p (expr))
17521 /* Wait for instantiation, tsubst_function_decl will handle it. */
17522 return expr;
17523
17524 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
17525 /* Don't let convert_like_real create more template codes. */
17526 processing_template_decl_sentinel s;
17527 expr = build_converted_constant_bool_expr (expr, complain);
17528 expr = cxx_constant_value (expr);
17529 return expr;
17530 }
17531
17532 #include "gt-cp-decl.h"
This page took 0.817683 seconds and 5 git commands to generate.